1/* Handle the hair of processing (but not expanding) inline functions.
2 Also manage function and variable name overloading.
3 Copyright (C) 1987-2017 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 3, or (at your option)
11any later version.
12
13GCC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
21
22
23/* Handle method declarations. */
24#include "config.h"
25#include "system.h"
26#include "coretypes.h"
27#include "target.h"
28#include "cp-tree.h"
29#include "stringpool.h"
30#include "cgraph.h"
31#include "varasm.h"
32#include "toplev.h"
33#include "common/common-target.h"
34
35/* Various flags to control the mangling process. */
36
37enum mangling_flags
38{
39 /* No flags. */
40 mf_none = 0,
41 /* The thing we are presently mangling is part of a template type,
42 rather than a fully instantiated type. Therefore, we may see
43 complex expressions where we would normally expect to see a
44 simple integer constant. */
45 mf_maybe_uninstantiated = 1,
46 /* When mangling a numeric value, use the form `_XX_' (instead of
47 just `XX') if the value has more than one digit. */
48 mf_use_underscores_around_value = 2
49};
50
51static void do_build_copy_assign (tree);
52static void do_build_copy_constructor (tree);
53static tree make_alias_for_thunk (tree);
54
55/* Called once to initialize method.c. */
56
57void
58init_method (void)
59{
60 init_mangle ();
61}
62
63/* Return a this or result adjusting thunk to FUNCTION. THIS_ADJUSTING
64 indicates whether it is a this or result adjusting thunk.
65 FIXED_OFFSET and VIRTUAL_OFFSET indicate how to do the adjustment
66 (see thunk_adjust). VIRTUAL_OFFSET can be NULL, but FIXED_OFFSET
67 never is. VIRTUAL_OFFSET is the /index/ into the vtable for this
68 adjusting thunks, we scale it to a byte offset. For covariant
69 thunks VIRTUAL_OFFSET is the virtual binfo. You must post process
70 the returned thunk with finish_thunk. */
71
72tree
73make_thunk (tree function, bool this_adjusting,
74 tree fixed_offset, tree virtual_offset)
75{
76 HOST_WIDE_INT d;
77 tree thunk;
78
79 gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
80 /* We can have this thunks to covariant thunks, but not vice versa. */
81 gcc_assert (!DECL_THIS_THUNK_P (function));
82 gcc_assert (!DECL_RESULT_THUNK_P (function) || this_adjusting);
83
84 /* Scale the VIRTUAL_OFFSET to be in terms of bytes. */
85 if (this_adjusting && virtual_offset)
86 virtual_offset
87 = size_binop (MULT_EXPR,
88 virtual_offset,
89 convert (ssizetype,
90 TYPE_SIZE_UNIT (vtable_entry_type)));
91
92 d = tree_to_shwi (fixed_offset);
93
94 /* See if we already have the thunk in question. For this_adjusting
95 thunks VIRTUAL_OFFSET will be an INTEGER_CST, for covariant thunks it
96 will be a BINFO. */
97 for (thunk = DECL_THUNKS (function); thunk; thunk = DECL_CHAIN (thunk))
98 if (DECL_THIS_THUNK_P (thunk) == this_adjusting
99 && THUNK_FIXED_OFFSET (thunk) == d
100 && !virtual_offset == !THUNK_VIRTUAL_OFFSET (thunk)
101 && (!virtual_offset
102 || (this_adjusting
103 ? tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk),
104 virtual_offset)
105 : THUNK_VIRTUAL_OFFSET (thunk) == virtual_offset)))
106 return thunk;
107
108 /* All thunks must be created before FUNCTION is actually emitted;
109 the ABI requires that all thunks be emitted together with the
110 function to which they transfer control. */
111 gcc_assert (!TREE_ASM_WRITTEN (function));
112 /* Likewise, we can only be adding thunks to a function declared in
113 the class currently being laid out. */
114 gcc_assert (TYPE_SIZE (DECL_CONTEXT (function))
115 && TYPE_BEING_DEFINED (DECL_CONTEXT (function)));
116
117 thunk = build_decl (DECL_SOURCE_LOCATION (function),
118 FUNCTION_DECL, NULL_TREE, TREE_TYPE (function));
119 DECL_LANG_SPECIFIC (thunk) = DECL_LANG_SPECIFIC (function);
120 cxx_dup_lang_specific_decl (thunk);
121 DECL_VIRTUAL_P (thunk) = true;
122 SET_DECL_THUNKS (thunk, NULL_TREE);
123
124 DECL_CONTEXT (thunk) = DECL_CONTEXT (function);
125 TREE_READONLY (thunk) = TREE_READONLY (function);
126 TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (function);
127 TREE_PUBLIC (thunk) = TREE_PUBLIC (function);
128 SET_DECL_THUNK_P (thunk, this_adjusting);
129 THUNK_TARGET (thunk) = function;
130 THUNK_FIXED_OFFSET (thunk) = d;
131 THUNK_VIRTUAL_OFFSET (thunk) = virtual_offset;
132 THUNK_ALIAS (thunk) = NULL_TREE;
133
134 DECL_INTERFACE_KNOWN (thunk) = 1;
135 DECL_NOT_REALLY_EXTERN (thunk) = 1;
136 DECL_COMDAT (thunk) = DECL_COMDAT (function);
137 DECL_SAVED_FUNCTION_DATA (thunk) = NULL;
138 /* The thunk itself is not a constructor or destructor, even if
139 the thing it is thunking to is. */
140 DECL_CXX_DESTRUCTOR_P (thunk) = 0;
141 DECL_CXX_CONSTRUCTOR_P (thunk) = 0;
142 DECL_EXTERNAL (thunk) = 1;
143 DECL_ARTIFICIAL (thunk) = 1;
144 /* The THUNK is not a pending inline, even if the FUNCTION is. */
145 DECL_PENDING_INLINE_P (thunk) = 0;
146 DECL_DECLARED_INLINE_P (thunk) = 0;
147 /* Nor is it a template instantiation. */
148 DECL_USE_TEMPLATE (thunk) = 0;
149 DECL_TEMPLATE_INFO (thunk) = NULL;
150
151 /* Add it to the list of thunks associated with FUNCTION. */
152 DECL_CHAIN (thunk) = DECL_THUNKS (function);
153 SET_DECL_THUNKS (function, thunk);
154
155 return thunk;
156}
157
158/* Finish THUNK, a thunk decl. */
159
160void
161finish_thunk (tree thunk)
162{
163 tree function, name;
164 tree fixed_offset = ssize_int (THUNK_FIXED_OFFSET (thunk));
165 tree virtual_offset = THUNK_VIRTUAL_OFFSET (thunk);
166
167 gcc_assert (!DECL_NAME (thunk) && DECL_THUNK_P (thunk));
168 if (virtual_offset && DECL_RESULT_THUNK_P (thunk))
169 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
170 function = THUNK_TARGET (thunk);
171 name = mangle_thunk (function, DECL_THIS_THUNK_P (thunk),
172 fixed_offset, virtual_offset, thunk);
173
174 /* We can end up with declarations of (logically) different
175 covariant thunks, that do identical adjustments. The two thunks
176 will be adjusting between within different hierarchies, which
177 happen to have the same layout. We must nullify one of them to
178 refer to the other. */
179 if (DECL_RESULT_THUNK_P (thunk))
180 {
181 tree cov_probe;
182
183 for (cov_probe = DECL_THUNKS (function);
184 cov_probe; cov_probe = DECL_CHAIN (cov_probe))
185 if (DECL_NAME (cov_probe) == name)
186 {
187 gcc_assert (!DECL_THUNKS (thunk));
188 THUNK_ALIAS (thunk) = (THUNK_ALIAS (cov_probe)
189 ? THUNK_ALIAS (cov_probe) : cov_probe);
190 break;
191 }
192 }
193
194 DECL_NAME (thunk) = name;
195 SET_DECL_ASSEMBLER_NAME (thunk, name);
196}
197
198static GTY (()) int thunk_labelno;
199
200/* Create a static alias to target. */
201
202tree
203make_alias_for (tree target, tree newid)
204{
205 tree alias = build_decl (DECL_SOURCE_LOCATION (target),
206 TREE_CODE (target), newid, TREE_TYPE (target));
207 DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (target);
208 cxx_dup_lang_specific_decl (alias);
209 DECL_CONTEXT (alias) = NULL;
210 TREE_READONLY (alias) = TREE_READONLY (target);
211 TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (target);
212 TREE_PUBLIC (alias) = 0;
213 DECL_INTERFACE_KNOWN (alias) = 1;
214 if (DECL_LANG_SPECIFIC (alias))
215 {
216 DECL_NOT_REALLY_EXTERN (alias) = 1;
217 DECL_USE_TEMPLATE (alias) = 0;
218 DECL_TEMPLATE_INFO (alias) = NULL;
219 }
220 DECL_EXTERNAL (alias) = 0;
221 DECL_ARTIFICIAL (alias) = 1;
222 DECL_TEMPLATE_INSTANTIATED (alias) = 0;
223 if (TREE_CODE (alias) == FUNCTION_DECL)
224 {
225 DECL_SAVED_FUNCTION_DATA (alias) = NULL;
226 DECL_CXX_DESTRUCTOR_P (alias) = 0;
227 DECL_CXX_CONSTRUCTOR_P (alias) = 0;
228 DECL_PENDING_INLINE_P (alias) = 0;
229 DECL_DECLARED_INLINE_P (alias) = 0;
230 DECL_INITIAL (alias) = error_mark_node;
231 DECL_ARGUMENTS (alias) = copy_list (DECL_ARGUMENTS (target));
232 }
233 else
234 TREE_STATIC (alias) = 1;
235 TREE_ADDRESSABLE (alias) = 1;
236 TREE_USED (alias) = 1;
237 SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
238 return alias;
239}
240
241static tree
242make_alias_for_thunk (tree function)
243{
244 tree alias;
245 char buf[256];
246
247 targetm.asm_out.generate_internal_label (buf, "LTHUNK", thunk_labelno);
248 thunk_labelno++;
249
250 alias = make_alias_for (function, get_identifier (buf));
251
252 if (!flag_syntax_only)
253 {
254 struct cgraph_node *funcn, *aliasn;
255 funcn = cgraph_node::get (function);
256 gcc_checking_assert (funcn);
257 aliasn = cgraph_node::create_same_body_alias (alias, function);
258 DECL_ASSEMBLER_NAME (function);
259 gcc_assert (aliasn != NULL);
260 }
261
262 return alias;
263}
264
265/* Emit the definition of a C++ multiple inheritance or covariant
266 return vtable thunk. If EMIT_P is nonzero, the thunk is emitted
267 immediately. */
268
269void
270use_thunk (tree thunk_fndecl, bool emit_p)
271{
272 tree a, t, function, alias;
273 tree virtual_offset;
274 HOST_WIDE_INT fixed_offset, virtual_value;
275 bool this_adjusting = DECL_THIS_THUNK_P (thunk_fndecl);
276 struct cgraph_node *funcn, *thunk_node;
277
278 /* We should have called finish_thunk to give it a name. */
279 gcc_assert (DECL_NAME (thunk_fndecl));
280
281 /* We should never be using an alias, always refer to the
282 aliased thunk. */
283 gcc_assert (!THUNK_ALIAS (thunk_fndecl));
284
285 if (TREE_ASM_WRITTEN (thunk_fndecl))
286 return;
287
288 function = THUNK_TARGET (thunk_fndecl);
289 if (DECL_RESULT (thunk_fndecl))
290 /* We already turned this thunk into an ordinary function.
291 There's no need to process this thunk again. */
292 return;
293
294 if (DECL_THUNK_P (function))
295 /* The target is itself a thunk, process it now. */
296 use_thunk (function, emit_p);
297
298 /* Thunks are always addressable; they only appear in vtables. */
299 TREE_ADDRESSABLE (thunk_fndecl) = 1;
300
301 /* Figure out what function is being thunked to. It's referenced in
302 this translation unit. */
303 TREE_ADDRESSABLE (function) = 1;
304 mark_used (function);
305 if (!emit_p)
306 return;
307
308 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function))
309 alias = make_alias_for_thunk (function);
310 else
311 alias = function;
312
313 fixed_offset = THUNK_FIXED_OFFSET (thunk_fndecl);
314 virtual_offset = THUNK_VIRTUAL_OFFSET (thunk_fndecl);
315
316 if (virtual_offset)
317 {
318 if (!this_adjusting)
319 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
320 virtual_value = tree_to_shwi (virtual_offset);
321 gcc_assert (virtual_value);
322 }
323 else
324 virtual_value = 0;
325
326 /* And, if we need to emit the thunk, it's used. */
327 mark_used (thunk_fndecl);
328 /* This thunk is actually defined. */
329 DECL_EXTERNAL (thunk_fndecl) = 0;
330 /* The linkage of the function may have changed. FIXME in linkage
331 rewrite. */
332 gcc_assert (DECL_INTERFACE_KNOWN (function));
333 TREE_PUBLIC (thunk_fndecl) = TREE_PUBLIC (function);
334 DECL_VISIBILITY (thunk_fndecl) = DECL_VISIBILITY (function);
335 DECL_VISIBILITY_SPECIFIED (thunk_fndecl)
336 = DECL_VISIBILITY_SPECIFIED (function);
337 DECL_COMDAT (thunk_fndecl) = DECL_COMDAT (function);
338 DECL_WEAK (thunk_fndecl) = DECL_WEAK (function);
339
340 if (flag_syntax_only)
341 {
342 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
343 return;
344 }
345
346 push_to_top_level ();
347
348 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function)
349 && targetm_common.have_named_sections)
350 {
351 tree fn = function;
352 struct symtab_node *symbol;
353
354 if ((symbol = symtab_node::get (function))
355 && symbol->alias)
356 {
357 if (symbol->analyzed)
358 fn = symtab_node::get (function)->ultimate_alias_target ()->decl;
359 else
360 fn = symtab_node::get (function)->alias_target;
361 }
362 resolve_unique_section (fn, 0, flag_function_sections);
363
364 if (DECL_SECTION_NAME (fn) != NULL && DECL_ONE_ONLY (fn))
365 {
366 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
367
368 /* Output the thunk into the same section as function. */
369 set_decl_section_name (thunk_fndecl, DECL_SECTION_NAME (fn));
370 symtab_node::get (thunk_fndecl)->implicit_section
371 = symtab_node::get (fn)->implicit_section;
372 }
373 }
374
375 /* Set up cloned argument trees for the thunk. */
376 t = NULL_TREE;
377 for (a = DECL_ARGUMENTS (function); a; a = DECL_CHAIN (a))
378 {
379 tree x = copy_node (a);
380 DECL_CHAIN (x) = t;
381 DECL_CONTEXT (x) = thunk_fndecl;
382 SET_DECL_RTL (x, NULL);
383 DECL_HAS_VALUE_EXPR_P (x) = 0;
384 TREE_ADDRESSABLE (x) = 0;
385 t = x;
386 }
387 a = nreverse (t);
388 DECL_ARGUMENTS (thunk_fndecl) = a;
389 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
390 funcn = cgraph_node::get (function);
391 gcc_checking_assert (funcn);
392 thunk_node = funcn->create_thunk (thunk_fndecl, function,
393 this_adjusting, fixed_offset, virtual_value,
394 virtual_offset, alias);
395 if (DECL_ONE_ONLY (function))
396 thunk_node->add_to_same_comdat_group (funcn);
397
398 pop_from_top_level ();
399}
400
401/* Code for synthesizing methods which have default semantics defined. */
402
403/* True iff CTYPE has a trivial SFK. */
404
405static bool
406type_has_trivial_fn (tree ctype, special_function_kind sfk)
407{
408 switch (sfk)
409 {
410 case sfk_constructor:
411 return !TYPE_HAS_COMPLEX_DFLT (ctype);
412 case sfk_copy_constructor:
413 return !TYPE_HAS_COMPLEX_COPY_CTOR (ctype);
414 case sfk_move_constructor:
415 return !TYPE_HAS_COMPLEX_MOVE_CTOR (ctype);
416 case sfk_copy_assignment:
417 return !TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype);
418 case sfk_move_assignment:
419 return !TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype);
420 case sfk_destructor:
421 return !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype);
422 case sfk_inheriting_constructor:
423 return false;
424 default:
425 gcc_unreachable ();
426 }
427}
428
429/* Note that CTYPE has a non-trivial SFK even though we previously thought
430 it was trivial. */
431
432static void
433type_set_nontrivial_flag (tree ctype, special_function_kind sfk)
434{
435 switch (sfk)
436 {
437 case sfk_constructor:
438 TYPE_HAS_COMPLEX_DFLT (ctype) = true;
439 return;
440 case sfk_copy_constructor:
441 TYPE_HAS_COMPLEX_COPY_CTOR (ctype) = true;
442 return;
443 case sfk_move_constructor:
444 TYPE_HAS_COMPLEX_MOVE_CTOR (ctype) = true;
445 return;
446 case sfk_copy_assignment:
447 TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype) = true;
448 return;
449 case sfk_move_assignment:
450 TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype) = true;
451 return;
452 case sfk_destructor:
453 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
454 return;
455 case sfk_inheriting_constructor:
456 default:
457 gcc_unreachable ();
458 }
459}
460
461/* True iff FN is a trivial defaulted member function ([cd]tor, op=). */
462
463bool
464trivial_fn_p (tree fn)
465{
466 if (TREE_CODE (fn) == TEMPLATE_DECL)
467 return false;
468 if (!DECL_DEFAULTED_FN (fn))
469 return false;
470
471 /* If fn is a clone, get the primary variant. */
472 if (tree prim = DECL_CLONED_FUNCTION (fn))
473 fn = prim;
474 return type_has_trivial_fn (DECL_CONTEXT (fn), special_function_p (fn));
475}
476
477/* PARM is a PARM_DECL for a function which we want to forward to another
478 function without changing its value category, a la std::forward. */
479
480tree
481forward_parm (tree parm)
482{
483 tree exp = convert_from_reference (parm);
484 tree type = TREE_TYPE (parm);
485 if (DECL_PACK_P (parm))
486 type = PACK_EXPANSION_PATTERN (type);
487 if (TREE_CODE (type) != REFERENCE_TYPE)
488 type = cp_build_reference_type (type, /*rval=*/true);
489 warning_sentinel w (warn_useless_cast);
490 exp = build_static_cast (type, exp, tf_warning_or_error);
491 if (DECL_PACK_P (parm))
492 exp = make_pack_expansion (exp);
493 return exp;
494}
495
496/* Strip all inheriting constructors, if any, to return the original
497 constructor from a (possibly indirect) base class. */
498
499tree
500strip_inheriting_ctors (tree dfn)
501{
502 if (!flag_new_inheriting_ctors)
503 return dfn;
504 tree fn = dfn;
505 while (tree inh = DECL_INHERITED_CTOR (fn))
506 fn = OVL_FIRST (inh);
507
508 if (TREE_CODE (fn) == TEMPLATE_DECL
509 && TREE_CODE (dfn) == FUNCTION_DECL)
510 fn = DECL_TEMPLATE_RESULT (fn);
511 return fn;
512}
513
514/* Find the binfo for the base subobject of BINFO being initialized by
515 inherited constructor FNDECL (a member of a direct base of BINFO). */
516
517static tree inherited_ctor_binfo (tree, tree);
518static tree
519inherited_ctor_binfo_1 (tree binfo, tree fndecl)
520{
521 tree base = DECL_CONTEXT (fndecl);
522 tree base_binfo;
523 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
524 if (BINFO_TYPE (base_binfo) == base)
525 return inherited_ctor_binfo (base_binfo, fndecl);
526
527 gcc_unreachable();
528}
529
530/* Find the binfo for the base subobject of BINFO being initialized by
531 inheriting constructor FNDECL (a member of BINFO), or BINFO if FNDECL is not
532 an inheriting constructor. */
533
534static tree
535inherited_ctor_binfo (tree binfo, tree fndecl)
536{
537 tree inh = DECL_INHERITED_CTOR (fndecl);
538 if (!inh)
539 return binfo;
540
541 tree results = NULL_TREE;
542 for (ovl_iterator iter (inh); iter; ++iter)
543 {
544 tree one = inherited_ctor_binfo_1 (binfo, *iter);
545 if (!results)
546 results = one;
547 else if (one != results)
548 results = tree_cons (NULL_TREE, one, results);
549 }
550 return results;
551}
552
553/* Find the binfo for the base subobject being initialized by inheriting
554 constructor FNDECL, or NULL_TREE if FNDECL is not an inheriting
555 constructor. */
556
557tree
558inherited_ctor_binfo (tree fndecl)
559{
560 if (!DECL_INHERITED_CTOR (fndecl))
561 return NULL_TREE;
562 tree binfo = TYPE_BINFO (DECL_CONTEXT (fndecl));
563 return inherited_ctor_binfo (binfo, fndecl);
564}
565
566/* True if we should omit all user-declared parameters from constructor FN,
567 because it is a base clone of a ctor inherited from a virtual base. */
568
569bool
570ctor_omit_inherited_parms (tree fn)
571{
572 if (!flag_new_inheriting_ctors)
573 /* We only optimize away the parameters in the new model. */
574 return false;
575 if (!DECL_BASE_CONSTRUCTOR_P (fn)
576 || !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
577 return false;
578 if (FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn)) == void_list_node)
579 /* No user-declared parameters to omit. */
580 return false;
581 tree binfo = inherited_ctor_binfo (fn);
582 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
583 if (BINFO_VIRTUAL_P (binfo))
584 return true;
585 return false;
586}
587
588/* True iff constructor(s) INH inherited into BINFO initializes INIT_BINFO.
589 This can be true for multiple virtual bases as well as one direct
590 non-virtual base. */
591
592static bool
593binfo_inherited_from (tree binfo, tree init_binfo, tree inh)
594{
595 /* inh is an OVERLOAD if we inherited the same constructor along
596 multiple paths, check all of them. */
597 for (ovl_iterator iter (inh); iter; ++iter)
598 {
599 tree fn = *iter;
600 tree base = DECL_CONTEXT (fn);
601 tree base_binfo = NULL_TREE;
602 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
603 if (BINFO_TYPE (base_binfo) == base)
604 break;
605 if (base_binfo == init_binfo
606 || (flag_new_inheriting_ctors
607 && binfo_inherited_from (base_binfo, init_binfo,
608 DECL_INHERITED_CTOR (fn))))
609 return true;
610 }
611 return false;
612}
613
614/* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO
615 given the parameter or parameters PARM, possibly inherited constructor
616 base INH, or move flag MOVE_P. */
617
618static tree
619add_one_base_init (tree binfo, tree parm, bool move_p, tree inh,
620 tree member_init_list)
621{
622 tree init;
623 if (inh)
624 {
625 /* An inheriting constructor only has a mem-initializer for
626 the base it inherits from. */
627 if (!binfo_inherited_from (TYPE_BINFO (current_class_type), binfo, inh))
628 return member_init_list;
629
630 tree *p = &init;
631 init = NULL_TREE;
632 for (; parm; parm = DECL_CHAIN (parm))
633 {
634 tree exp = forward_parm (parm);
635 *p = build_tree_list (NULL_TREE, exp);
636 p = &TREE_CHAIN (*p);
637 }
638 }
639 else
640 {
641 init = build_base_path (PLUS_EXPR, parm, binfo, 1,
642 tf_warning_or_error);
643 if (move_p)
644 init = move (init);
645 init = build_tree_list (NULL_TREE, init);
646 }
647 return tree_cons (binfo, init, member_init_list);
648}
649
650/* Generate code for default X(X&) or X(X&&) constructor or an inheriting
651 constructor. */
652
653static void
654do_build_copy_constructor (tree fndecl)
655{
656 tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
657 bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
658 bool trivial = trivial_fn_p (fndecl);
659 tree inh = DECL_INHERITED_CTOR (fndecl);
660
661 if (!inh)
662 parm = convert_from_reference (parm);
663
664 if (trivial)
665 {
666 if (is_empty_class (current_class_type))
667 /* Don't copy the padding byte; it might not have been allocated
668 if *this is a base subobject. */;
669 else if (tree_int_cst_equal (TYPE_SIZE (current_class_type),
670 CLASSTYPE_SIZE (current_class_type)))
671 {
672 tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm);
673 finish_expr_stmt (t);
674 }
675 else
676 {
677 /* We must only copy the non-tail padding parts. */
678 tree base_size = CLASSTYPE_SIZE_UNIT (current_class_type);
679 base_size = size_binop (MINUS_EXPR, base_size, size_int (1));
680 tree array_type = build_array_type (unsigned_char_type_node,
681 build_index_type (base_size));
682 tree alias_set = build_int_cst (TREE_TYPE (current_class_ptr), 0);
683 tree lhs = build2 (MEM_REF, array_type,
684 current_class_ptr, alias_set);
685 tree rhs = build2 (MEM_REF, array_type,
686 TREE_OPERAND (parm, 0), alias_set);
687 tree t = build2 (INIT_EXPR, void_type_node, lhs, rhs);
688 finish_expr_stmt (t);
689 }
690 }
691 else
692 {
693 tree fields = TYPE_FIELDS (current_class_type);
694 tree member_init_list = NULL_TREE;
695 int cvquals = cp_type_quals (TREE_TYPE (parm));
696 int i;
697 tree binfo, base_binfo;
698 tree init;
699 vec<tree, va_gc> *vbases;
700
701 /* Initialize all the base-classes with the parameter converted
702 to their type so that we get their copy constructor and not
703 another constructor that takes current_class_type. We must
704 deal with the binfo's directly as a direct base might be
705 inaccessible due to ambiguity. */
706 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
707 vec_safe_iterate (vbases, i, &binfo); i++)
708 {
709 member_init_list = add_one_base_init (binfo, parm, move_p, inh,
710 member_init_list);
711 }
712
713 for (binfo = TYPE_BINFO (current_class_type), i = 0;
714 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
715 {
716 if (BINFO_VIRTUAL_P (base_binfo))
717 continue;
718 member_init_list = add_one_base_init (base_binfo, parm, move_p,
719 inh, member_init_list);
720 }
721
722 for (; fields; fields = DECL_CHAIN (fields))
723 {
724 tree field = fields;
725 tree expr_type;
726
727 if (TREE_CODE (field) != FIELD_DECL)
728 continue;
729 if (inh)
730 continue;
731
732 expr_type = TREE_TYPE (field);
733 if (DECL_NAME (field))
734 {
735 if (VFIELD_NAME_P (DECL_NAME (field)))
736 continue;
737 }
738 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
739 /* Just use the field; anonymous types can't have
740 nontrivial copy ctors or assignment ops or this
741 function would be deleted. */;
742 else
743 continue;
744
745 /* Compute the type of "init->field". If the copy-constructor
746 parameter is, for example, "const S&", and the type of
747 the field is "T", then the type will usually be "const
748 T". (There are no cv-qualified variants of reference
749 types.) */
750 if (TREE_CODE (expr_type) != REFERENCE_TYPE)
751 {
752 int quals = cvquals;
753
754 if (DECL_MUTABLE_P (field))
755 quals &= ~TYPE_QUAL_CONST;
756 quals |= cp_type_quals (expr_type);
757 expr_type = cp_build_qualified_type (expr_type, quals);
758 }
759
760 init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
761 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
762 /* 'move' breaks bit-fields, and has no effect for scalars. */
763 && !scalarish_type_p (expr_type))
764 init = move (init);
765 init = build_tree_list (NULL_TREE, init);
766
767 member_init_list = tree_cons (field, init, member_init_list);
768 }
769 finish_mem_initializers (member_init_list);
770 }
771}
772
773static void
774do_build_copy_assign (tree fndecl)
775{
776 tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl));
777 tree compound_stmt;
778 bool move_p = move_fn_p (fndecl);
779 bool trivial = trivial_fn_p (fndecl);
780 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
781
782 compound_stmt = begin_compound_stmt (0);
783 parm = convert_from_reference (parm);
784
785 if (trivial
786 && is_empty_class (current_class_type))
787 /* Don't copy the padding byte; it might not have been allocated
788 if *this is a base subobject. */;
789 else if (trivial)
790 {
791 tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
792 finish_expr_stmt (t);
793 }
794 else
795 {
796 tree fields;
797 int cvquals = cp_type_quals (TREE_TYPE (parm));
798 int i;
799 tree binfo, base_binfo;
800
801 /* Assign to each of the direct base classes. */
802 for (binfo = TYPE_BINFO (current_class_type), i = 0;
803 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
804 {
805 tree converted_parm;
806 vec<tree, va_gc> *parmvec;
807
808 /* We must convert PARM directly to the base class
809 explicitly since the base class may be ambiguous. */
810 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
811 tf_warning_or_error);
812 if (move_p)
813 converted_parm = move (converted_parm);
814 /* Call the base class assignment operator. */
815 parmvec = make_tree_vector_single (converted_parm);
816 finish_expr_stmt
817 (build_special_member_call (current_class_ref,
818 assign_op_identifier,
819 &parmvec,
820 base_binfo,
821 flags,
822 tf_warning_or_error));
823 release_tree_vector (parmvec);
824 }
825
826 /* Assign to each of the non-static data members. */
827 for (fields = TYPE_FIELDS (current_class_type);
828 fields;
829 fields = DECL_CHAIN (fields))
830 {
831 tree comp = current_class_ref;
832 tree init = parm;
833 tree field = fields;
834 tree expr_type;
835 int quals;
836
837 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
838 continue;
839
840 expr_type = TREE_TYPE (field);
841
842 if (CP_TYPE_CONST_P (expr_type))
843 {
844 error ("non-static const member %q#D, can%'t use default "
845 "assignment operator", field);
846 continue;
847 }
848 else if (TREE_CODE (expr_type) == REFERENCE_TYPE)
849 {
850 error ("non-static reference member %q#D, can%'t use "
851 "default assignment operator", field);
852 continue;
853 }
854
855 if (DECL_NAME (field))
856 {
857 if (VFIELD_NAME_P (DECL_NAME (field)))
858 continue;
859 }
860 else if (ANON_AGGR_TYPE_P (expr_type)
861 && TYPE_FIELDS (expr_type) != NULL_TREE)
862 /* Just use the field; anonymous types can't have
863 nontrivial copy ctors or assignment ops or this
864 function would be deleted. */;
865 else
866 continue;
867
868 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
869
870 /* Compute the type of init->field */
871 quals = cvquals;
872 if (DECL_MUTABLE_P (field))
873 quals &= ~TYPE_QUAL_CONST;
874 expr_type = cp_build_qualified_type (expr_type, quals);
875
876 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
877 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
878 /* 'move' breaks bit-fields, and has no effect for scalars. */
879 && !scalarish_type_p (expr_type))
880 init = move (init);
881
882 if (DECL_NAME (field))
883 init = cp_build_modify_expr (input_location, comp, NOP_EXPR, init,
884 tf_warning_or_error);
885 else
886 init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
887 finish_expr_stmt (init);
888 }
889 }
890 finish_return_stmt (current_class_ref);
891 finish_compound_stmt (compound_stmt);
892}
893
894/* Synthesize FNDECL, a non-static member function. */
895
896void
897synthesize_method (tree fndecl)
898{
899 bool nested = (current_function_decl != NULL_TREE);
900 tree context = decl_function_context (fndecl);
901 bool need_body = true;
902 tree stmt;
903 location_t save_input_location = input_location;
904 int error_count = errorcount;
905 int warning_count = warningcount + werrorcount;
906
907 /* Reset the source location, we might have been previously
908 deferred, and thus have saved where we were first needed. */
909 DECL_SOURCE_LOCATION (fndecl)
910 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
911
912 /* If we've been asked to synthesize a clone, just synthesize the
913 cloned function instead. Doing so will automatically fill in the
914 body for the clone. */
915 if (DECL_CLONED_FUNCTION_P (fndecl))
916 fndecl = DECL_CLONED_FUNCTION (fndecl);
917
918 /* We may be in the middle of deferred access check. Disable
919 it now. */
920 push_deferring_access_checks (dk_no_deferred);
921
922 if (! context)
923 push_to_top_level ();
924 else if (nested)
925 push_function_context ();
926
927 input_location = DECL_SOURCE_LOCATION (fndecl);
928
929 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
930 stmt = begin_function_body ();
931
932 if (DECL_ASSIGNMENT_OPERATOR_P (fndecl)
933 && DECL_OVERLOADED_OPERATOR_IS (fndecl, NOP_EXPR))
934 {
935 do_build_copy_assign (fndecl);
936 need_body = false;
937 }
938 else if (DECL_CONSTRUCTOR_P (fndecl))
939 {
940 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
941 if (arg_chain != void_list_node)
942 do_build_copy_constructor (fndecl);
943 else
944 finish_mem_initializers (NULL_TREE);
945 }
946
947 /* If we haven't yet generated the body of the function, just
948 generate an empty compound statement. */
949 if (need_body)
950 {
951 tree compound_stmt;
952 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
953 finish_compound_stmt (compound_stmt);
954 }
955
956 finish_function_body (stmt);
957 expand_or_defer_fn (finish_function (/*inline_p=*/false));
958
959 input_location = save_input_location;
960
961 if (! context)
962 pop_from_top_level ();
963 else if (nested)
964 pop_function_context ();
965
966 pop_deferring_access_checks ();
967
968 if (error_count != errorcount || warning_count != warningcount + werrorcount)
969 inform (input_location, "synthesized method %qD first required here ",
970 fndecl);
971}
972
973/* Build a reference to type TYPE with cv-quals QUALS, which is an
974 rvalue if RVALUE is true. */
975
976static tree
977build_stub_type (tree type, int quals, bool rvalue)
978{
979 tree argtype = cp_build_qualified_type (type, quals);
980 return cp_build_reference_type (argtype, rvalue);
981}
982
983/* Build a dummy glvalue from dereferencing a dummy reference of type
984 REFTYPE. */
985
986static tree
987build_stub_object (tree reftype)
988{
989 if (TREE_CODE (reftype) != REFERENCE_TYPE)
990 reftype = cp_build_reference_type (reftype, /*rval*/true);
991 tree stub = build1 (CONVERT_EXPR, reftype, integer_one_node);
992 return convert_from_reference (stub);
993}
994
995/* Determine which function will be called when looking up NAME in TYPE,
996 called with a single ARGTYPE argument, or no argument if ARGTYPE is
997 null. FLAGS and COMPLAIN are as for build_new_method_call.
998
999 Returns a FUNCTION_DECL if all is well.
1000 Returns NULL_TREE if overload resolution failed.
1001 Returns error_mark_node if the chosen function cannot be called. */
1002
1003static tree
1004locate_fn_flags (tree type, tree name, tree argtype, int flags,
1005 tsubst_flags_t complain)
1006{
1007 tree ob, fn, fns, binfo, rval;
1008 vec<tree, va_gc> *args;
1009
1010 if (TYPE_P (type))
1011 binfo = TYPE_BINFO (type);
1012 else
1013 {
1014 binfo = type;
1015 type = BINFO_TYPE (binfo);
1016 }
1017
1018 ob = build_stub_object (cp_build_reference_type (type, false));
1019 args = make_tree_vector ();
1020 if (argtype)
1021 {
1022 if (TREE_CODE (argtype) == TREE_LIST)
1023 {
1024 for (tree elt = argtype; elt && elt != void_list_node;
1025 elt = TREE_CHAIN (elt))
1026 {
1027 tree type = TREE_VALUE (elt);
1028 tree arg = build_stub_object (type);
1029 vec_safe_push (args, arg);
1030 }
1031 }
1032 else
1033 {
1034 tree arg = build_stub_object (argtype);
1035 args->quick_push (arg);
1036 }
1037 }
1038
1039 fns = lookup_fnfields (binfo, name, 0);
1040 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
1041
1042 release_tree_vector (args);
1043 if (fn && rval == error_mark_node)
1044 return rval;
1045 else
1046 return fn;
1047}
1048
1049/* Locate the dtor of TYPE. */
1050
1051tree
1052get_dtor (tree type, tsubst_flags_t complain)
1053{
1054 tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
1055 LOOKUP_NORMAL, complain);
1056 if (fn == error_mark_node)
1057 return NULL_TREE;
1058 return fn;
1059}
1060
1061/* Locate the default ctor of TYPE. */
1062
1063tree
1064locate_ctor (tree type)
1065{
1066 tree fn;
1067
1068 push_deferring_access_checks (dk_no_check);
1069 fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
1070 LOOKUP_SPECULATIVE, tf_none);
1071 pop_deferring_access_checks ();
1072 if (fn == error_mark_node)
1073 return NULL_TREE;
1074 return fn;
1075}
1076
1077/* Likewise, but give any appropriate errors. */
1078
1079tree
1080get_default_ctor (tree type)
1081{
1082 tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
1083 LOOKUP_NORMAL, tf_warning_or_error);
1084 if (fn == error_mark_node)
1085 return NULL_TREE;
1086 return fn;
1087}
1088
1089/* Locate the copy ctor of TYPE. */
1090
1091tree
1092get_copy_ctor (tree type, tsubst_flags_t complain)
1093{
1094 int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
1095 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
1096 tree argtype = build_stub_type (type, quals, false);
1097 tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
1098 LOOKUP_NORMAL, complain);
1099 if (fn == error_mark_node)
1100 return NULL_TREE;
1101 return fn;
1102}
1103
1104/* Locate the copy assignment operator of TYPE. */
1105
1106tree
1107get_copy_assign (tree type)
1108{
1109 int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
1110 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
1111 tree argtype = build_stub_type (type, quals, false);
1112 tree fn = locate_fn_flags (type, assign_op_identifier, argtype,
1113 LOOKUP_NORMAL, tf_warning_or_error);
1114 if (fn == error_mark_node)
1115 return NULL_TREE;
1116 return fn;
1117}
1118
1119/* walk_tree helper function for is_trivially_xible. If *TP is a call,
1120 return it if it calls something other than a trivial special member
1121 function. */
1122
1123static tree
1124check_nontriv (tree *tp, int *, void *)
1125{
1126 tree fn = cp_get_callee (*tp);
1127 if (fn == NULL_TREE)
1128 return NULL_TREE;
1129
1130 if (TREE_CODE (fn) == ADDR_EXPR)
1131 fn = TREE_OPERAND (fn, 0);
1132
1133 if (TREE_CODE (fn) != FUNCTION_DECL
1134 || !trivial_fn_p (fn))
1135 return fn;
1136 return NULL_TREE;
1137}
1138
1139/* Return declval<T>() = declval<U>() treated as an unevaluated operand. */
1140
1141static tree
1142assignable_expr (tree to, tree from)
1143{
1144 ++cp_unevaluated_operand;
1145 to = build_stub_object (to);
1146 from = build_stub_object (from);
1147 tree r = cp_build_modify_expr (input_location, to, NOP_EXPR, from, tf_none);
1148 --cp_unevaluated_operand;
1149 return r;
1150}
1151
1152/* The predicate condition for a template specialization
1153 is_constructible<T, Args...> shall be satisfied if and only if the
1154 following variable definition would be well-formed for some invented
1155 variable t: T t(create<Args>()...);
1156
1157 Return something equivalent in well-formedness and triviality. */
1158
1159static tree
1160constructible_expr (tree to, tree from)
1161{
1162 tree expr;
1163 if (CLASS_TYPE_P (to))
1164 {
1165 tree ctype = to;
1166 vec<tree, va_gc> *args = NULL;
1167 cp_unevaluated cp_uneval_guard;
1168 if (TREE_CODE (to) != REFERENCE_TYPE)
1169 to = cp_build_reference_type (to, /*rval*/false);
1170 tree ob = build_stub_object (to);
1171 for (; from; from = TREE_CHAIN (from))
1172 vec_safe_push (args, build_stub_object (TREE_VALUE (from)));
1173 expr = build_special_member_call (ob, complete_ctor_identifier, &args,
1174 ctype, LOOKUP_NORMAL, tf_none);
1175 if (expr == error_mark_node)
1176 return error_mark_node;
1177 /* The current state of the standard vis-a-vis LWG 2116 is that
1178 is_*constructible involves destruction as well. */
1179 if (type_build_dtor_call (ctype))
1180 {
1181 tree dtor = build_special_member_call (ob, complete_dtor_identifier,
1182 NULL, ctype, LOOKUP_NORMAL,
1183 tf_none);
1184 if (dtor == error_mark_node)
1185 return error_mark_node;
1186 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
1187 expr = build2 (COMPOUND_EXPR, void_type_node, expr, dtor);
1188 }
1189 }
1190 else
1191 {
1192 if (from == NULL_TREE)
1193 return build_value_init (strip_array_types (to), tf_none);
1194 else if (TREE_CHAIN (from))
1195 return error_mark_node; // too many initializers
1196 from = build_stub_object (TREE_VALUE (from));
1197 expr = perform_direct_initialization_if_possible (to, from,
1198 /*cast*/false,
1199 tf_none);
1200 }
1201 return expr;
1202}
1203
1204/* Returns a tree iff TO is assignable (if CODE is MODIFY_EXPR) or
1205 constructible (otherwise) from FROM, which is a single type for
1206 assignment or a list of types for construction. */
1207
1208static tree
1209is_xible_helper (enum tree_code code, tree to, tree from, bool trivial)
1210{
1211 if (VOID_TYPE_P (to) || ABSTRACT_CLASS_TYPE_P (to)
1212 || (from && FUNC_OR_METHOD_TYPE_P (from)
1213 && (TYPE_READONLY (from) || FUNCTION_REF_QUALIFIED (from))))
1214 return error_mark_node;
1215 tree expr;
1216 if (code == MODIFY_EXPR)
1217 expr = assignable_expr (to, from);
1218 else if (trivial && from && TREE_CHAIN (from))
1219 return error_mark_node; // only 0- and 1-argument ctors can be trivial
1220 else
1221 expr = constructible_expr (to, from);
1222 return expr;
1223}
1224
1225/* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or
1226 constructible (otherwise) from FROM, which is a single type for
1227 assignment or a list of types for construction. */
1228
1229bool
1230is_trivially_xible (enum tree_code code, tree to, tree from)
1231{
1232 tree expr;
1233 expr = is_xible_helper (code, to, from, /*trivial*/true);
1234
1235 if (expr == error_mark_node)
1236 return false;
1237 tree nt = cp_walk_tree_without_duplicates (&expr, check_nontriv, NULL);
1238 return !nt;
1239}
1240
1241/* Returns true iff TO is assignable (if CODE is MODIFY_EXPR) or
1242 constructible (otherwise) from FROM, which is a single type for
1243 assignment or a list of types for construction. */
1244
1245bool
1246is_xible (enum tree_code code, tree to, tree from)
1247{
1248 tree expr = is_xible_helper (code, to, from, /*trivial*/false);
1249 if (expr == error_mark_node)
1250 return false;
1251 return !!expr;
1252}
1253
1254/* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
1255 DELETED_P or give an error message MSG with argument ARG. */
1256
1257static void
1258process_subob_fn (tree fn, tree *spec_p, bool *trivial_p,
1259 bool *deleted_p, bool *constexpr_p,
1260 bool diag, tree arg, bool dtor_from_ctor = false)
1261{
1262 if (!fn || fn == error_mark_node)
1263 {
1264 if (deleted_p)
1265 *deleted_p = true;
1266 return;
1267 }
1268
1269 if (spec_p)
1270 {
1271 maybe_instantiate_noexcept (fn);
1272 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
1273 *spec_p = merge_exception_specifiers (*spec_p, raises);
1274 }
1275
1276 if (!trivial_fn_p (fn) && !dtor_from_ctor)
1277 {
1278 if (trivial_p)
1279 *trivial_p = false;
1280 if (TREE_CODE (arg) == FIELD_DECL
1281 && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
1282 {
1283 if (deleted_p)
1284 *deleted_p = true;
1285 if (diag)
1286 error ("union member %q+D with non-trivial %qD", arg, fn);
1287 }
1288 }
1289
1290 if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
1291 {
1292 *constexpr_p = false;
1293 if (diag)
1294 {
1295 inform (DECL_SOURCE_LOCATION (fn),
1296 "defaulted constructor calls non-%<constexpr%> %qD", fn);
1297 explain_invalid_constexpr_fn (fn);
1298 }
1299 }
1300}
1301
1302/* Subroutine of synthesized_method_walk to allow recursion into anonymous
1303 aggregates. If DTOR_FROM_CTOR is true, we're walking subobject destructors
1304 called from a synthesized constructor, in which case we don't consider
1305 the triviality of the subobject destructor. */
1306
1307static void
1308walk_field_subobs (tree fields, tree fnname, special_function_kind sfk,
1309 int quals, bool copy_arg_p, bool move_p,
1310 bool assign_p, tree *spec_p, bool *trivial_p,
1311 bool *deleted_p, bool *constexpr_p,
1312 bool diag, int flags, tsubst_flags_t complain,
1313 bool dtor_from_ctor)
1314{
1315 tree field;
1316 for (field = fields; field; field = DECL_CHAIN (field))
1317 {
1318 tree mem_type, argtype, rval;
1319
1320 if (TREE_CODE (field) != FIELD_DECL
1321 || DECL_ARTIFICIAL (field))
1322 continue;
1323
1324 mem_type = strip_array_types (TREE_TYPE (field));
1325 if (assign_p)
1326 {
1327 bool bad = true;
1328 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
1329 {
1330 if (diag)
1331 error ("non-static const member %q#D, can%'t use default "
1332 "assignment operator", field);
1333 }
1334 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1335 {
1336 if (diag)
1337 error ("non-static reference member %q#D, can%'t use "
1338 "default assignment operator", field);
1339 }
1340 else
1341 bad = false;
1342
1343 if (bad && deleted_p)
1344 *deleted_p = true;
1345 }
1346 else if (sfk == sfk_constructor || sfk == sfk_inheriting_constructor)
1347 {
1348 bool bad;
1349
1350 if (DECL_INITIAL (field))
1351 {
1352 if (diag && DECL_INITIAL (field) == error_mark_node)
1353 inform (DECL_SOURCE_LOCATION (field),
1354 "initializer for %q#D is invalid", field);
1355 if (trivial_p)
1356 *trivial_p = false;
1357 /* Core 1351: If the field has an NSDMI that could throw, the
1358 default constructor is noexcept(false). */
1359 if (spec_p)
1360 {
1361 tree nsdmi = get_nsdmi (field, /*ctor*/false, complain);
1362 if (!expr_noexcept_p (nsdmi, complain))
1363 *spec_p = noexcept_false_spec;
1364 }
1365 /* Don't do the normal processing. */
1366 continue;
1367 }
1368
1369 bad = false;
1370 if (CP_TYPE_CONST_P (mem_type)
1371 && default_init_uninitialized_part (mem_type))
1372 {
1373 if (diag)
1374 {
1375 error ("uninitialized const member in %q#T",
1376 current_class_type);
1377 inform (DECL_SOURCE_LOCATION (field),
1378 "%q#D should be initialized", field);
1379 }
1380 bad = true;
1381 }
1382 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1383 {
1384 if (diag)
1385 {
1386 error ("uninitialized reference member in %q#T",
1387 current_class_type);
1388 inform (DECL_SOURCE_LOCATION (field),
1389 "%q#D should be initialized", field);
1390 }
1391 bad = true;
1392 }
1393
1394 if (bad && deleted_p)
1395 *deleted_p = true;
1396
1397 /* For an implicitly-defined default constructor to be constexpr,
1398 every member must have a user-provided default constructor or
1399 an explicit initializer. */
1400 if (constexpr_p && !CLASS_TYPE_P (mem_type)
1401 && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE)
1402 {
1403 *constexpr_p = false;
1404 if (diag)
1405 inform (DECL_SOURCE_LOCATION (field),
1406 "defaulted default constructor does not "
1407 "initialize %q#D", field);
1408 }
1409 }
1410 else if (sfk == sfk_copy_constructor)
1411 {
1412 /* 12.8p11b5 */
1413 if (TREE_CODE (mem_type) == REFERENCE_TYPE
1414 && TYPE_REF_IS_RVALUE (mem_type))
1415 {
1416 if (diag)
1417 error ("copying non-static data member %q#D of rvalue "
1418 "reference type", field);
1419 if (deleted_p)
1420 *deleted_p = true;
1421 }
1422 }
1423
1424 if (!CLASS_TYPE_P (mem_type))
1425 continue;
1426
1427 if (ANON_AGGR_TYPE_P (mem_type))
1428 {
1429 walk_field_subobs (TYPE_FIELDS (mem_type), fnname, sfk, quals,
1430 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1431 deleted_p, constexpr_p,
1432 diag, flags, complain, dtor_from_ctor);
1433 continue;
1434 }
1435
1436 if (copy_arg_p)
1437 {
1438 int mem_quals = cp_type_quals (mem_type) | quals;
1439 if (DECL_MUTABLE_P (field))
1440 mem_quals &= ~TYPE_QUAL_CONST;
1441 argtype = build_stub_type (mem_type, mem_quals, move_p);
1442 }
1443 else
1444 argtype = NULL_TREE;
1445
1446 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
1447
1448 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1449 constexpr_p, diag, field, dtor_from_ctor);
1450 }
1451}
1452
1453/* Base walker helper for synthesized_method_walk. Inspect a direct
1454 or virtual base. BINFO is the parent type's binfo. BASE_BINFO is
1455 the base binfo of interests. All other parms are as for
1456 synthesized_method_walk, or its local vars. */
1457
1458static tree
1459synthesized_method_base_walk (tree binfo, tree base_binfo,
1460 int quals, bool copy_arg_p,
1461 bool move_p, bool ctor_p,
1462 tree *inheriting_ctor, tree inherited_parms,
1463 tree fnname, int flags, bool diag,
1464 tree *spec_p, bool *trivial_p,
1465 bool *deleted_p, bool *constexpr_p)
1466{
1467 bool inherited_binfo = false;
1468 tree argtype = NULL_TREE;
1469 deferring_kind defer = dk_no_deferred;
1470
1471 if (copy_arg_p)
1472 argtype = build_stub_type (BINFO_TYPE (base_binfo), quals, move_p);
1473 else if (inheriting_ctor
1474 && (inherited_binfo
1475 = binfo_inherited_from (binfo, base_binfo, *inheriting_ctor)))
1476 {
1477 argtype = inherited_parms;
1478 /* Don't check access on the inherited constructor. */
1479 if (flag_new_inheriting_ctors)
1480 defer = dk_deferred;
1481 }
1482 /* To be conservative, ignore access to the base dtor that
1483 DR1658 instructs us to ignore. See the comment in
1484 synthesized_method_walk. */
1485 else if (cxx_dialect >= cxx14 && fnname == complete_dtor_identifier
1486 && BINFO_VIRTUAL_P (base_binfo)
1487 && ABSTRACT_CLASS_TYPE_P (BINFO_TYPE (binfo)))
1488 defer = dk_no_check;
1489
1490 if (defer != dk_no_deferred)
1491 push_deferring_access_checks (defer);
1492 tree rval = locate_fn_flags (base_binfo, fnname, argtype, flags,
1493 diag ? tf_warning_or_error : tf_none);
1494 if (defer != dk_no_deferred)
1495 pop_deferring_access_checks ();
1496
1497 /* Replace an inherited template with the appropriate specialization. */
1498 if (inherited_binfo && rval
1499 && DECL_P (*inheriting_ctor) && DECL_P (rval)
1500 && DECL_CONTEXT (*inheriting_ctor) == DECL_CONTEXT (rval))
1501 *inheriting_ctor = DECL_CLONED_FUNCTION (rval);
1502
1503 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1504 constexpr_p, diag, BINFO_TYPE (base_binfo));
1505 if (ctor_p &&
1506 (!BINFO_VIRTUAL_P (base_binfo)
1507 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))))
1508 {
1509 /* In a constructor we also need to check the subobject
1510 destructors for cleanup of partially constructed objects. */
1511 tree dtor = locate_fn_flags (base_binfo, complete_dtor_identifier,
1512 NULL_TREE, flags,
1513 diag ? tf_warning_or_error : tf_none);
1514 /* Note that we don't pass down trivial_p; the subobject
1515 destructors don't affect triviality of the constructor. Nor
1516 do they affect constexpr-ness (a constant expression doesn't
1517 throw) or exception-specification (a throw from one of the
1518 dtors would be a double-fault). */
1519 process_subob_fn (dtor, NULL, NULL, deleted_p, NULL, false,
1520 BINFO_TYPE (base_binfo), /*dtor_from_ctor*/true);
1521 }
1522
1523 return rval;
1524}
1525
1526/* The caller wants to generate an implicit declaration of SFK for
1527 CTYPE which is const if relevant and CONST_P is set. If SPEC_P,
1528 TRIVIAL_P, DELETED_P or CONSTEXPR_P are non-null, set their
1529 referent appropriately. If DIAG is true, we're either being called
1530 from maybe_explain_implicit_delete to give errors, or if
1531 CONSTEXPR_P is non-null, from explain_invalid_constexpr_fn. */
1532
1533static void
1534synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
1535 tree *spec_p, bool *trivial_p, bool *deleted_p,
1536 bool *constexpr_p, bool diag,
1537 tree *inheriting_ctor, tree inherited_parms)
1538{
1539 tree binfo, base_binfo, fnname;
1540 int i;
1541
1542 if (spec_p)
1543 *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec);
1544
1545 if (deleted_p)
1546 {
1547 /* "The closure type associated with a lambda-expression has a deleted
1548 default constructor and a deleted copy assignment operator."
1549 This is diagnosed in maybe_explain_implicit_delete. */
1550 if (LAMBDA_TYPE_P (ctype)
1551 && (sfk == sfk_constructor
1552 || sfk == sfk_copy_assignment))
1553 {
1554 *deleted_p = true;
1555 return;
1556 }
1557
1558 *deleted_p = false;
1559 }
1560
1561 bool ctor_p = false;
1562 bool assign_p = false;
1563 bool check_vdtor = false;
1564 switch (sfk)
1565 {
1566 case sfk_move_assignment:
1567 case sfk_copy_assignment:
1568 assign_p = true;
1569 fnname = assign_op_identifier;
1570 break;
1571
1572 case sfk_destructor:
1573 check_vdtor = true;
1574 /* The synthesized method will call base dtors, but check complete
1575 here to avoid having to deal with VTT. */
1576 fnname = complete_dtor_identifier;
1577 break;
1578
1579 case sfk_constructor:
1580 case sfk_move_constructor:
1581 case sfk_copy_constructor:
1582 case sfk_inheriting_constructor:
1583 ctor_p = true;
1584 fnname = complete_ctor_identifier;
1585 break;
1586
1587 default:
1588 gcc_unreachable ();
1589 }
1590
1591 gcc_assert ((sfk == sfk_inheriting_constructor)
1592 == (inheriting_ctor && *inheriting_ctor != NULL_TREE));
1593
1594 /* If that user-written default constructor would satisfy the
1595 requirements of a constexpr constructor (7.1.5), the
1596 implicitly-defined default constructor is constexpr.
1597
1598 The implicitly-defined copy/move assignment operator is constexpr if
1599 - X is a literal type, and
1600 - the assignment operator selected to copy/move each direct base class
1601 subobject is a constexpr function, and
1602 - for each non-static data member of X that is of class type (or array
1603 thereof), the assignment operator selected to copy/move that
1604 member is a constexpr function. */
1605 if (constexpr_p)
1606 *constexpr_p = ctor_p || (assign_p && cxx_dialect >= cxx14);
1607
1608 bool move_p = false;
1609 bool copy_arg_p = false;
1610 switch (sfk)
1611 {
1612 case sfk_constructor:
1613 case sfk_destructor:
1614 case sfk_inheriting_constructor:
1615 break;
1616
1617 case sfk_move_constructor:
1618 case sfk_move_assignment:
1619 move_p = true;
1620 /* FALLTHRU */
1621 case sfk_copy_constructor:
1622 case sfk_copy_assignment:
1623 copy_arg_p = true;
1624 break;
1625
1626 default:
1627 gcc_unreachable ();
1628 }
1629
1630 bool expected_trivial = type_has_trivial_fn (ctype, sfk);
1631 if (trivial_p)
1632 *trivial_p = expected_trivial;
1633
1634 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
1635 class versions and other properties of the type. But a subobject
1636 class can be trivially copyable and yet have overload resolution
1637 choose a template constructor for initialization, depending on
1638 rvalueness and cv-quals. And furthermore, a member in a base might
1639 be trivial but deleted or otherwise not callable. So we can't exit
1640 early in C++0x. The same considerations apply in C++98/03, but
1641 there the definition of triviality does not consider overload
1642 resolution, so a constructor can be trivial even if it would otherwise
1643 call a non-trivial constructor. */
1644 if (expected_trivial
1645 && (!copy_arg_p || cxx_dialect < cxx11))
1646 {
1647 if (constexpr_p && sfk == sfk_constructor)
1648 {
1649 bool cx = trivial_default_constructor_is_constexpr (ctype);
1650 *constexpr_p = cx;
1651 if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
1652 /* A trivial constructor doesn't have any NSDMI. */
1653 inform (input_location, "defaulted default constructor does "
1654 "not initialize any non-static data member");
1655 }
1656 if (!diag && cxx_dialect < cxx11)
1657 return;
1658 }
1659
1660 ++cp_unevaluated_operand;
1661 ++c_inhibit_evaluation_warnings;
1662 push_deferring_access_checks (dk_no_deferred);
1663
1664 tree scope = push_scope (ctype);
1665
1666 int flags = LOOKUP_NORMAL | LOOKUP_SPECULATIVE;
1667 if (sfk != sfk_inheriting_constructor)
1668 flags |= LOOKUP_DEFAULTED;
1669
1670 tsubst_flags_t complain = diag ? tf_warning_or_error : tf_none;
1671 if (diag && spec_p)
1672 /* We're in get_defaulted_eh_spec; we don't actually want any walking
1673 diagnostics, we just want complain set. */
1674 diag = false;
1675 int quals = const_p ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED;
1676
1677 for (binfo = TYPE_BINFO (ctype), i = 0;
1678 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
1679 {
1680 if (!assign_p && BINFO_VIRTUAL_P (base_binfo))
1681 /* We'll handle virtual bases below. */
1682 continue;
1683
1684 tree fn = synthesized_method_base_walk (binfo, base_binfo, quals,
1685 copy_arg_p, move_p, ctor_p,
1686 inheriting_ctor,
1687 inherited_parms,
1688 fnname, flags, diag,
1689 spec_p, trivial_p,
1690 deleted_p, constexpr_p);
1691
1692 if (diag && assign_p && move_p
1693 && BINFO_VIRTUAL_P (base_binfo)
1694 && fn && TREE_CODE (fn) == FUNCTION_DECL
1695 && move_fn_p (fn) && !trivial_fn_p (fn)
1696 && vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
1697 warning (OPT_Wvirtual_move_assign,
1698 "defaulted move assignment for %qT calls a non-trivial "
1699 "move assignment operator for virtual base %qT",
1700 ctype, BINFO_TYPE (base_binfo));
1701
1702 if (check_vdtor && type_has_virtual_destructor (BINFO_TYPE (base_binfo)))
1703 {
1704 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
1705 to have a null fn (no class-specific op delete). */
1706 fn = locate_fn_flags (ctype, ovl_op_identifier (false, DELETE_EXPR),
1707 ptr_type_node, flags, tf_none);
1708 if (fn && fn == error_mark_node)
1709 {
1710 if (complain & tf_error)
1711 locate_fn_flags (ctype, ovl_op_identifier (false, DELETE_EXPR),
1712 ptr_type_node, flags, complain);
1713 if (deleted_p)
1714 *deleted_p = true;
1715 }
1716 check_vdtor = false;
1717 }
1718 }
1719
1720 vec<tree, va_gc> *vbases = CLASSTYPE_VBASECLASSES (ctype);
1721 if (assign_p)
1722 /* Already examined vbases above. */;
1723 else if (vec_safe_is_empty (vbases))
1724 /* No virtual bases to worry about. */;
1725 else if (ABSTRACT_CLASS_TYPE_P (ctype) && cxx_dialect >= cxx14
1726 /* DR 1658 specifies that vbases of abstract classes are
1727 ignored for both ctors and dtors. However, that breaks
1728 virtual dtor overriding when the ignored base has a
1729 throwing destructor. So, ignore that piece of 1658. A
1730 defect has been filed (no number yet). */
1731 && sfk != sfk_destructor)
1732 /* Vbase cdtors are not relevant. */;
1733 else
1734 {
1735 if (constexpr_p)
1736 *constexpr_p = false;
1737
1738 FOR_EACH_VEC_ELT (*vbases, i, base_binfo)
1739 synthesized_method_base_walk (binfo, base_binfo, quals,
1740 copy_arg_p, move_p, ctor_p,
1741 inheriting_ctor, inherited_parms,
1742 fnname, flags, diag,
1743 spec_p, trivial_p,
1744 deleted_p, constexpr_p);
1745 }
1746
1747 /* Now handle the non-static data members. */
1748 walk_field_subobs (TYPE_FIELDS (ctype), fnname, sfk, quals,
1749 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1750 deleted_p, constexpr_p,
1751 diag, flags, complain, /*dtor_from_ctor*/false);
1752 if (ctor_p)
1753 walk_field_subobs (TYPE_FIELDS (ctype), complete_dtor_identifier,
1754 sfk_destructor, TYPE_UNQUALIFIED, false,
1755 false, false, NULL, NULL,
1756 deleted_p, NULL,
1757 false, flags, complain, /*dtor_from_ctor*/true);
1758
1759 pop_scope (scope);
1760
1761 pop_deferring_access_checks ();
1762 --cp_unevaluated_operand;
1763 --c_inhibit_evaluation_warnings;
1764}
1765
1766/* DECL is a defaulted function whose exception specification is now
1767 needed. Return what it should be. */
1768
1769tree
1770get_defaulted_eh_spec (tree decl, tsubst_flags_t complain)
1771{
1772 if (DECL_CLONED_FUNCTION_P (decl))
1773 decl = DECL_CLONED_FUNCTION (decl);
1774 special_function_kind sfk = special_function_p (decl);
1775 tree ctype = DECL_CONTEXT (decl);
1776 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1777 tree parm_type = TREE_VALUE (parms);
1778 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1779 tree spec = empty_except_spec;
1780 bool diag = !DECL_DELETED_FN (decl) && (complain & tf_error);
1781 tree inh = DECL_INHERITED_CTOR (decl);
1782 synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL,
1783 NULL, diag, &inh, parms);
1784 return spec;
1785}
1786
1787/* DECL is a deleted function. If it's implicitly deleted, explain why and
1788 return true; else return false. */
1789
1790bool
1791maybe_explain_implicit_delete (tree decl)
1792{
1793 /* If decl is a clone, get the primary variant. */
1794 decl = DECL_ORIGIN (decl);
1795 gcc_assert (DECL_DELETED_FN (decl));
1796 if (DECL_DEFAULTED_FN (decl))
1797 {
1798 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
1799 static hash_set<tree> *explained;
1800
1801 special_function_kind sfk;
1802 location_t loc;
1803 bool informed;
1804 tree ctype;
1805
1806 if (!explained)
1807 explained = new hash_set<tree>;
1808 if (explained->add (decl))
1809 return true;
1810
1811 sfk = special_function_p (decl);
1812 ctype = DECL_CONTEXT (decl);
1813 loc = input_location;
1814 input_location = DECL_SOURCE_LOCATION (decl);
1815
1816 informed = false;
1817 if (LAMBDA_TYPE_P (ctype))
1818 {
1819 informed = true;
1820 if (sfk == sfk_constructor)
1821 inform (DECL_SOURCE_LOCATION (decl),
1822 "a lambda closure type has a deleted default constructor");
1823 else if (sfk == sfk_copy_assignment)
1824 inform (DECL_SOURCE_LOCATION (decl),
1825 "a lambda closure type has a deleted copy assignment operator");
1826 else
1827 informed = false;
1828 }
1829 else if (DECL_ARTIFICIAL (decl)
1830 && (sfk == sfk_copy_assignment || sfk == sfk_copy_constructor)
1831 && classtype_has_move_assign_or_move_ctor_p (ctype, true))
1832 {
1833 inform (DECL_SOURCE_LOCATION (decl),
1834 "%q#D is implicitly declared as deleted because %qT "
1835 "declares a move constructor or move assignment operator",
1836 decl, ctype);
1837 informed = true;
1838 }
1839 else if (sfk == sfk_inheriting_constructor)
1840 {
1841 tree binfo = inherited_ctor_binfo (decl);
1842 if (TREE_CODE (binfo) != TREE_BINFO)
1843 {
1844 inform (DECL_SOURCE_LOCATION (decl),
1845 "%q#D inherits from multiple base subobjects",
1846 decl);
1847 informed = true;
1848 }
1849 }
1850 if (!informed)
1851 {
1852 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1853 tree parm_type = TREE_VALUE (parms);
1854 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1855 tree raises = NULL_TREE;
1856 bool deleted_p = false;
1857 tree scope = push_scope (ctype);
1858 tree inh = DECL_INHERITED_CTOR (decl);
1859
1860 synthesized_method_walk (ctype, sfk, const_p,
1861 &raises, NULL, &deleted_p, NULL, false,
1862 &inh, parms);
1863 if (deleted_p)
1864 {
1865 inform (DECL_SOURCE_LOCATION (decl),
1866 "%q#D is implicitly deleted because the default "
1867 "definition would be ill-formed:", decl);
1868 synthesized_method_walk (ctype, sfk, const_p,
1869 NULL, NULL, NULL, NULL, true,
1870 &inh, parms);
1871 }
1872 else if (!comp_except_specs
1873 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
1874 raises, ce_normal))
1875 inform (DECL_SOURCE_LOCATION (decl), "%q#F is implicitly "
1876 "deleted because its exception-specification does not "
1877 "match the implicit exception-specification %qX",
1878 decl, raises);
1879 else if (flag_checking)
1880 gcc_unreachable ();
1881
1882 pop_scope (scope);
1883 }
1884
1885 input_location = loc;
1886 return true;
1887 }
1888 return false;
1889}
1890
1891/* DECL is a defaulted function which was declared constexpr. Explain why
1892 it can't be constexpr. */
1893
1894void
1895explain_implicit_non_constexpr (tree decl)
1896{
1897 tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl));
1898 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1899 tree inh = DECL_INHERITED_CTOR (decl);
1900 bool dummy;
1901 synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
1902 special_function_p (decl), const_p,
1903 NULL, NULL, NULL, &dummy, true,
1904 &inh,
1905 FUNCTION_FIRST_USER_PARMTYPE (decl));
1906}
1907
1908/* DECL is an instantiation of an inheriting constructor template. Deduce
1909 the correct exception-specification and deletedness for this particular
1910 specialization. */
1911
1912void
1913deduce_inheriting_ctor (tree decl)
1914{
1915 decl = DECL_ORIGIN (decl);
1916 gcc_assert (DECL_INHERITED_CTOR (decl));
1917 tree spec;
1918 bool trivial, constexpr_, deleted;
1919 tree inh = DECL_INHERITED_CTOR (decl);
1920 synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor,
1921 false, &spec, &trivial, &deleted, &constexpr_,
1922 /*diag*/false,
1923 &inh,
1924 FUNCTION_FIRST_USER_PARMTYPE (decl));
1925 if (TREE_CODE (inherited_ctor_binfo (decl)) != TREE_BINFO)
1926 /* Inherited the same constructor from different base subobjects. */
1927 deleted = true;
1928 DECL_DELETED_FN (decl) = deleted;
1929 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
1930 SET_DECL_INHERITED_CTOR (decl, inh);
1931
1932 tree clone;
1933 FOR_EACH_CLONE (clone, decl)
1934 {
1935 DECL_DELETED_FN (clone) = deleted;
1936 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
1937 SET_DECL_INHERITED_CTOR (clone, inh);
1938 }
1939}
1940
1941/* Implicitly declare the special function indicated by KIND, as a
1942 member of TYPE. For copy constructors and assignment operators,
1943 CONST_P indicates whether these functions should take a const
1944 reference argument or a non-const reference. Returns the
1945 FUNCTION_DECL for the implicitly declared function. */
1946
1947tree
1948implicitly_declare_fn (special_function_kind kind, tree type,
1949 bool const_p, tree inherited_ctor,
1950 tree inherited_parms)
1951{
1952 tree fn;
1953 tree parameter_types = void_list_node;
1954 tree return_type;
1955 tree fn_type;
1956 tree raises = empty_except_spec;
1957 tree rhs_parm_type = NULL_TREE;
1958 tree this_parm;
1959 tree name;
1960 HOST_WIDE_INT saved_processing_template_decl;
1961 bool deleted_p;
1962 bool constexpr_p;
1963
1964 /* Because we create declarations for implicitly declared functions
1965 lazily, we may be creating the declaration for a member of TYPE
1966 while in some completely different context. However, TYPE will
1967 never be a dependent class (because we never want to do lookups
1968 for implicitly defined functions in a dependent class).
1969 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
1970 because we only create clones for constructors and destructors
1971 when not in a template. */
1972 gcc_assert (!dependent_type_p (type));
1973 saved_processing_template_decl = processing_template_decl;
1974 processing_template_decl = 0;
1975
1976 type = TYPE_MAIN_VARIANT (type);
1977
1978 if (targetm.cxx.cdtor_returns_this ())
1979 {
1980 if (kind == sfk_destructor)
1981 /* See comment in check_special_function_return_type. */
1982 return_type = build_pointer_type (void_type_node);
1983 else
1984 return_type = build_pointer_type (type);
1985 }
1986 else
1987 return_type = void_type_node;
1988
1989 switch (kind)
1990 {
1991 case sfk_destructor:
1992 /* Destructor. */
1993 name = dtor_identifier;
1994 break;
1995
1996 case sfk_constructor:
1997 /* Default constructor. */
1998 name = ctor_identifier;
1999 break;
2000
2001 case sfk_copy_constructor:
2002 case sfk_copy_assignment:
2003 case sfk_move_constructor:
2004 case sfk_move_assignment:
2005 case sfk_inheriting_constructor:
2006 {
2007 if (kind == sfk_copy_assignment
2008 || kind == sfk_move_assignment)
2009 {
2010 return_type = build_reference_type (type);
2011 name = assign_op_identifier;
2012 }
2013 else
2014 name = ctor_identifier;
2015
2016 if (kind == sfk_inheriting_constructor)
2017 parameter_types = inherited_parms;
2018 else
2019 {
2020 if (const_p)
2021 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
2022 else
2023 rhs_parm_type = type;
2024 bool move_p = (kind == sfk_move_assignment
2025 || kind == sfk_move_constructor);
2026 rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
2027
2028 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
2029 }
2030 break;
2031 }
2032 default:
2033 gcc_unreachable ();
2034 }
2035
2036 bool trivial_p = false;
2037
2038 if (inherited_ctor)
2039 {
2040 /* For an inheriting constructor, just copy these flags from the
2041 inherited constructor until deduce_inheriting_ctor. */
2042 raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor));
2043 deleted_p = DECL_DELETED_FN (inherited_ctor);
2044 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
2045 }
2046 else if (cxx_dialect >= cxx11)
2047 {
2048 raises = noexcept_deferred_spec;
2049 synthesized_method_walk (type, kind, const_p, NULL, &trivial_p,
2050 &deleted_p, &constexpr_p, false,
2051 &inherited_ctor, inherited_parms);
2052 }
2053 else
2054 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
2055 &deleted_p, &constexpr_p, false,
2056 &inherited_ctor, inherited_parms);
2057 /* Don't bother marking a deleted constructor as constexpr. */
2058 if (deleted_p)
2059 constexpr_p = false;
2060 /* A trivial copy/move constructor is also a constexpr constructor,
2061 unless the class has virtual bases (7.1.5p4). */
2062 else if (trivial_p && cxx_dialect >= cxx11
2063 && (kind == sfk_copy_constructor
2064 || kind == sfk_move_constructor)
2065 && !CLASSTYPE_VBASECLASSES (type))
2066 gcc_assert (constexpr_p);
2067
2068 if (!trivial_p && type_has_trivial_fn (type, kind))
2069 type_set_nontrivial_flag (type, kind);
2070
2071 /* Create the function. */
2072 fn_type = build_method_type_directly (type, return_type, parameter_types);
2073 if (raises)
2074 fn_type = build_exception_variant (fn_type, raises);
2075 fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
2076 if (kind != sfk_inheriting_constructor)
2077 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
2078
2079 if (!IDENTIFIER_CDTOR_P (name))
2080 /* Assignment operator. */
2081 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) = OVL_OP_NOP_EXPR;
2082 else if (IDENTIFIER_CTOR_P (name))
2083 DECL_CXX_CONSTRUCTOR_P (fn) = true;
2084 else
2085 DECL_CXX_DESTRUCTOR_P (fn) = true;
2086
2087 SET_DECL_ALIGN (fn, MINIMUM_METHOD_BOUNDARY);
2088
2089 /* Create the explicit arguments. */
2090 if (rhs_parm_type)
2091 {
2092 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
2093 want its type to be included in the mangled function
2094 name. */
2095 tree decl = cp_build_parm_decl (fn, NULL_TREE, rhs_parm_type);
2096 TREE_READONLY (decl) = 1;
2097 retrofit_lang_decl (decl);
2098 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
2099 DECL_ARGUMENTS (fn) = decl;
2100 }
2101 else if (kind == sfk_inheriting_constructor)
2102 {
2103 tree *p = &DECL_ARGUMENTS (fn);
2104 int index = 1;
2105 for (tree parm = inherited_parms; parm && parm != void_list_node;
2106 parm = TREE_CHAIN (parm))
2107 {
2108 *p = cp_build_parm_decl (fn, NULL_TREE, TREE_VALUE (parm));
2109 retrofit_lang_decl (*p);
2110 DECL_PARM_LEVEL (*p) = 1;
2111 DECL_PARM_INDEX (*p) = index++;
2112 p = &DECL_CHAIN (*p);
2113 }
2114 SET_DECL_INHERITED_CTOR (fn, inherited_ctor);
2115 DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor);
2116 /* A constructor so declared has the same access as the corresponding
2117 constructor in X. */
2118 TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor);
2119 TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor);
2120 /* Copy constexpr from the inherited constructor even if the
2121 inheriting constructor doesn't satisfy the requirements. */
2122 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
2123 }
2124 /* Add the "this" parameter. */
2125 this_parm = build_this_parm (fn, fn_type, TYPE_UNQUALIFIED);
2126 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
2127 DECL_ARGUMENTS (fn) = this_parm;
2128
2129 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
2130 DECL_IN_AGGR_P (fn) = 1;
2131 DECL_ARTIFICIAL (fn) = 1;
2132 DECL_DEFAULTED_FN (fn) = 1;
2133 if (cxx_dialect >= cxx11)
2134 {
2135 DECL_DELETED_FN (fn) = deleted_p;
2136 DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
2137 }
2138 DECL_EXTERNAL (fn) = true;
2139 DECL_NOT_REALLY_EXTERN (fn) = 1;
2140 DECL_DECLARED_INLINE_P (fn) = 1;
2141 set_linkage_according_to_type (type, fn);
2142 if (TREE_PUBLIC (fn))
2143 DECL_COMDAT (fn) = 1;
2144 rest_of_decl_compilation (fn, namespace_bindings_p (), at_eof);
2145 gcc_assert (!TREE_USED (fn));
2146
2147 /* Propagate constraints from the inherited constructor. */
2148 if (flag_concepts && inherited_ctor)
2149 if (tree orig_ci = get_constraints (inherited_ctor))
2150 {
2151 tree new_ci = copy_node (orig_ci);
2152 set_constraints (fn, new_ci);
2153 }
2154
2155 /* Restore PROCESSING_TEMPLATE_DECL. */
2156 processing_template_decl = saved_processing_template_decl;
2157
2158 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
2159 fn = add_inherited_template_parms (fn, inherited_ctor);
2160
2161 /* Warn about calling a non-trivial move assignment in a virtual base. */
2162 if (kind == sfk_move_assignment && !deleted_p && !trivial_p
2163 && CLASSTYPE_VBASECLASSES (type))
2164 {
2165 location_t loc = input_location;
2166 input_location = DECL_SOURCE_LOCATION (fn);
2167 synthesized_method_walk (type, kind, const_p,
2168 NULL, NULL, NULL, NULL, true,
2169 NULL, NULL_TREE);
2170 input_location = loc;
2171 }
2172
2173 return fn;
2174}
2175
2176/* Gives any errors about defaulted functions which need to be deferred
2177 until the containing class is complete. */
2178
2179void
2180defaulted_late_check (tree fn)
2181{
2182 /* Complain about invalid signature for defaulted fn. */
2183 tree ctx = DECL_CONTEXT (fn);
2184 special_function_kind kind = special_function_p (fn);
2185 bool fn_const_p = (copy_fn_p (fn) == 2);
2186 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
2187 NULL, NULL);
2188 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
2189
2190 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
2191 TREE_TYPE (TREE_TYPE (implicit_fn)))
2192 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2193 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn))))
2194 {
2195 error ("defaulted declaration %q+D does not match the "
2196 "expected signature", fn);
2197 inform (DECL_SOURCE_LOCATION (fn),
2198 "expected signature: %qD", implicit_fn);
2199 return;
2200 }
2201
2202 if (DECL_DELETED_FN (implicit_fn))
2203 {
2204 DECL_DELETED_FN (fn) = 1;
2205 return;
2206 }
2207
2208 /* 8.4.2/2: An explicitly-defaulted function (...) may have an explicit
2209 exception-specification only if it is compatible (15.4) with the
2210 exception-specification on the implicit declaration. If a function
2211 is explicitly defaulted on its first declaration, (...) it is
2212 implicitly considered to have the same exception-specification as if
2213 it had been implicitly declared. */
2214 maybe_instantiate_noexcept (fn);
2215 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2216 if (!fn_spec)
2217 {
2218 if (DECL_DEFAULTED_IN_CLASS_P (fn))
2219 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
2220 }
2221 else if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
2222 /* Equivalent to the implicit spec. */;
2223 else if (DECL_DEFAULTED_IN_CLASS_P (fn)
2224 && !CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
2225 /* We can't compare an explicit exception-specification on a
2226 constructor defaulted in the class body to the implicit
2227 exception-specification until after we've parsed any NSDMI; see
2228 after_nsdmi_defaulted_late_checks. */;
2229 else
2230 {
2231 tree eh_spec = get_defaulted_eh_spec (fn);
2232 if (!comp_except_specs (fn_spec, eh_spec, ce_normal))
2233 {
2234 if (DECL_DEFAULTED_IN_CLASS_P (fn))
2235 DECL_DELETED_FN (fn) = true;
2236 else
2237 error ("function %q+D defaulted on its redeclaration "
2238 "with an exception-specification that differs from "
2239 "the implicit exception-specification %qX", fn, eh_spec);
2240 }
2241 }
2242
2243 if (DECL_DEFAULTED_IN_CLASS_P (fn)
2244 && DECL_DECLARED_CONSTEXPR_P (implicit_fn))
2245 {
2246 /* Hmm...should we do this for out-of-class too? Should it be OK to
2247 add constexpr later like inline, rather than requiring
2248 declarations to match? */
2249 DECL_DECLARED_CONSTEXPR_P (fn) = true;
2250 if (kind == sfk_constructor)
2251 TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
2252 }
2253
2254 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
2255 && DECL_DECLARED_CONSTEXPR_P (fn))
2256 {
2257 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
2258 {
2259 error ("explicitly defaulted function %q+D cannot be declared "
2260 "as %<constexpr%> because the implicit declaration is not "
2261 "%<constexpr%>:", fn);
2262 explain_implicit_non_constexpr (fn);
2263 }
2264 DECL_DECLARED_CONSTEXPR_P (fn) = false;
2265 }
2266}
2267
2268/* OK, we've parsed the NSDMI for class T, now we can check any explicit
2269 exception-specifications on functions defaulted in the class body. */
2270
2271void
2272after_nsdmi_defaulted_late_checks (tree t)
2273{
2274 if (uses_template_parms (t))
2275 return;
2276 if (t == error_mark_node)
2277 return;
2278 for (tree fn = TYPE_FIELDS (t); fn; fn = DECL_CHAIN (fn))
2279 if (!DECL_ARTIFICIAL (fn)
2280 && DECL_DECLARES_FUNCTION_P (fn)
2281 && DECL_DEFAULTED_IN_CLASS_P (fn))
2282 {
2283 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2284 if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
2285 continue;
2286
2287 tree eh_spec = get_defaulted_eh_spec (fn);
2288 if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)),
2289 eh_spec, ce_normal))
2290 DECL_DELETED_FN (fn) = true;
2291 }
2292}
2293
2294/* Returns true iff FN can be explicitly defaulted, and gives any
2295 errors if defaulting FN is ill-formed. */
2296
2297bool
2298defaultable_fn_check (tree fn)
2299{
2300 special_function_kind kind = sfk_none;
2301
2302 if (template_parm_scope_p ())
2303 {
2304 error ("a template cannot be defaulted");
2305 return false;
2306 }
2307
2308 if (DECL_CONSTRUCTOR_P (fn))
2309 {
2310 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
2311 kind = sfk_constructor;
2312 else if (copy_fn_p (fn) > 0
2313 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
2314 == void_list_node))
2315 kind = sfk_copy_constructor;
2316 else if (move_fn_p (fn))
2317 kind = sfk_move_constructor;
2318 }
2319 else if (DECL_DESTRUCTOR_P (fn))
2320 kind = sfk_destructor;
2321 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2322 && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR))
2323 {
2324 if (copy_fn_p (fn))
2325 kind = sfk_copy_assignment;
2326 else if (move_fn_p (fn))
2327 kind = sfk_move_assignment;
2328 }
2329
2330 if (kind == sfk_none)
2331 {
2332 error ("%qD cannot be defaulted", fn);
2333 return false;
2334 }
2335 else
2336 {
2337 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
2338 t && t != void_list_node; t = TREE_CHAIN (t))
2339 if (TREE_PURPOSE (t))
2340 {
2341 error ("defaulted function %q+D with default argument", fn);
2342 break;
2343 }
2344
2345 /* Avoid do_warn_unused_parameter warnings. */
2346 for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p))
2347 if (DECL_NAME (p))
2348 TREE_NO_WARNING (p) = 1;
2349
2350 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn)))
2351 /* Defer checking. */;
2352 else if (!processing_template_decl)
2353 defaulted_late_check (fn);
2354
2355 return true;
2356 }
2357}
2358
2359/* Add an implicit declaration to TYPE for the kind of function
2360 indicated by SFK. Return the FUNCTION_DECL for the new implicit
2361 declaration. */
2362
2363tree
2364lazily_declare_fn (special_function_kind sfk, tree type)
2365{
2366 tree fn;
2367 /* Whether or not the argument has a const reference type. */
2368 bool const_p = false;
2369
2370 type = TYPE_MAIN_VARIANT (type);
2371
2372 switch (sfk)
2373 {
2374 case sfk_constructor:
2375 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
2376 break;
2377 case sfk_copy_constructor:
2378 const_p = TYPE_HAS_CONST_COPY_CTOR (type);
2379 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
2380 break;
2381 case sfk_move_constructor:
2382 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
2383 break;
2384 case sfk_copy_assignment:
2385 const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
2386 CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
2387 break;
2388 case sfk_move_assignment:
2389 CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
2390 break;
2391 case sfk_destructor:
2392 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
2393 break;
2394 default:
2395 gcc_unreachable ();
2396 }
2397
2398 /* Declare the function. */
2399 fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL);
2400
2401 /* [class.copy]/8 If the class definition declares a move constructor or
2402 move assignment operator, the implicitly declared copy constructor is
2403 defined as deleted.... */
2404 if ((sfk == sfk_copy_assignment || sfk == sfk_copy_constructor)
2405 && classtype_has_move_assign_or_move_ctor_p (type, true))
2406 DECL_DELETED_FN (fn) = true;
2407
2408 /* Destructors and assignment operators may be virtual. */
2409 if (sfk == sfk_destructor
2410 || sfk == sfk_move_assignment
2411 || sfk == sfk_copy_assignment)
2412 check_for_override (fn, type);
2413
2414 /* Add it to the class */
2415 bool added = add_method (type, fn, false);
2416 gcc_assert (added);
2417
2418 /* Add it to TYPE_FIELDS. */
2419 if (sfk == sfk_destructor
2420 && DECL_VIRTUAL_P (fn))
2421 /* The ABI requires that a virtual destructor go at the end of the
2422 vtable. */
2423 TYPE_FIELDS (type) = chainon (TYPE_FIELDS (type), fn);
2424 else
2425 {
2426 DECL_CHAIN (fn) = TYPE_FIELDS (type);
2427 TYPE_FIELDS (type) = fn;
2428 }
2429 /* Propagate TYPE_FIELDS. */
2430 fixup_type_variants (type);
2431
2432 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
2433 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
2434 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
2435 /* Create appropriate clones. */
2436 clone_function_decl (fn, /*update_methods=*/true);
2437
2438 return fn;
2439}
2440
2441/* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
2442 as there are artificial parms in FN. */
2443
2444tree
2445skip_artificial_parms_for (const_tree fn, tree list)
2446{
2447 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2448 list = TREE_CHAIN (list);
2449 else
2450 return list;
2451
2452 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2453 list = TREE_CHAIN (list);
2454 if (DECL_HAS_VTT_PARM_P (fn))
2455 list = TREE_CHAIN (list);
2456 return list;
2457}
2458
2459/* Given a FUNCTION_DECL FN and a chain LIST, return the number of
2460 artificial parms in FN. */
2461
2462int
2463num_artificial_parms_for (const_tree fn)
2464{
2465 int count = 0;
2466
2467 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2468 count++;
2469 else
2470 return 0;
2471
2472 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2473 count++;
2474 if (DECL_HAS_VTT_PARM_P (fn))
2475 count++;
2476 return count;
2477}
2478
2479
2480#include "gt-cp-method.h"
2481