* config/spu/spu.md (floatunsdidf2): Remove unused local variable.
[official-gcc.git] / gcc / cp / method.c
blob73d42b19d55af4aa45f68072eb84a078d2721369
1 /* Handle the hair of processing (but not expanding) inline functions.
2 Also manage function and variable name overloading.
3 Copyright (C) 1987-2016 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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"
35 /* Various flags to control the mangling process. */
37 enum mangling_flags
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
51 static void do_build_copy_assign (tree);
52 static void do_build_copy_constructor (tree);
53 static tree make_alias_for_thunk (tree);
55 /* Called once to initialize method.c. */
57 void
58 init_method (void)
60 init_mangle ();
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. */
72 tree
73 make_thunk (tree function, bool this_adjusting,
74 tree fixed_offset, tree virtual_offset)
76 HOST_WIDE_INT d;
77 tree thunk;
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);
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)));
92 d = tree_to_shwi (fixed_offset);
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;
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)));
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);
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;
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_DESTRUCTOR_P (thunk) = 0;
141 DECL_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;
151 /* Add it to the list of thunks associated with FUNCTION. */
152 DECL_CHAIN (thunk) = DECL_THUNKS (function);
153 SET_DECL_THUNKS (function, thunk);
155 return thunk;
158 /* Finish THUNK, a thunk decl. */
160 void
161 finish_thunk (tree thunk)
163 tree function, name;
164 tree fixed_offset = ssize_int (THUNK_FIXED_OFFSET (thunk));
165 tree virtual_offset = THUNK_VIRTUAL_OFFSET (thunk);
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);
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))
181 tree cov_probe;
183 for (cov_probe = DECL_THUNKS (function);
184 cov_probe; cov_probe = DECL_CHAIN (cov_probe))
185 if (DECL_NAME (cov_probe) == name)
187 gcc_assert (!DECL_THUNKS (thunk));
188 THUNK_ALIAS (thunk) = (THUNK_ALIAS (cov_probe)
189 ? THUNK_ALIAS (cov_probe) : cov_probe);
190 break;
194 DECL_NAME (thunk) = name;
195 SET_DECL_ASSEMBLER_NAME (thunk, name);
198 static GTY (()) int thunk_labelno;
200 /* Create a static alias to target. */
202 tree
203 make_alias_for (tree target, tree newid)
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))
216 DECL_NOT_REALLY_EXTERN (alias) = 1;
217 DECL_USE_TEMPLATE (alias) = 0;
218 DECL_TEMPLATE_INFO (alias) = NULL;
220 DECL_EXTERNAL (alias) = 0;
221 DECL_ARTIFICIAL (alias) = 1;
222 DECL_TEMPLATE_INSTANTIATED (alias) = 0;
223 if (TREE_CODE (alias) == FUNCTION_DECL)
225 DECL_SAVED_FUNCTION_DATA (alias) = NULL;
226 DECL_DESTRUCTOR_P (alias) = 0;
227 DECL_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));
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;
241 static tree
242 make_alias_for_thunk (tree function)
244 tree alias;
245 char buf[256];
247 targetm.asm_out.generate_internal_label (buf, "LTHUNK", thunk_labelno);
248 thunk_labelno++;
250 alias = make_alias_for (function, get_identifier (buf));
252 if (!flag_syntax_only)
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);
262 return alias;
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. */
269 void
270 use_thunk (tree thunk_fndecl, bool emit_p)
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;
278 /* We should have called finish_thunk to give it a name. */
279 gcc_assert (DECL_NAME (thunk_fndecl));
281 /* We should never be using an alias, always refer to the
282 aliased thunk. */
283 gcc_assert (!THUNK_ALIAS (thunk_fndecl));
285 if (TREE_ASM_WRITTEN (thunk_fndecl))
286 return;
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;
294 if (DECL_THUNK_P (function))
295 /* The target is itself a thunk, process it now. */
296 use_thunk (function, emit_p);
298 /* Thunks are always addressable; they only appear in vtables. */
299 TREE_ADDRESSABLE (thunk_fndecl) = 1;
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;
308 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function))
309 alias = make_alias_for_thunk (function);
310 else
311 alias = function;
313 fixed_offset = THUNK_FIXED_OFFSET (thunk_fndecl);
314 virtual_offset = THUNK_VIRTUAL_OFFSET (thunk_fndecl);
316 if (virtual_offset)
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);
323 else
324 virtual_value = 0;
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);
340 if (flag_syntax_only)
342 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
343 return;
346 push_to_top_level ();
348 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function)
349 && targetm_common.have_named_sections)
351 tree fn = function;
352 struct symtab_node *symbol;
354 if ((symbol = symtab_node::get (function))
355 && symbol->alias)
357 if (symbol->analyzed)
358 fn = symtab_node::get (function)->ultimate_alias_target ()->decl;
359 else
360 fn = symtab_node::get (function)->alias_target;
362 resolve_unique_section (fn, 0, flag_function_sections);
364 if (DECL_SECTION_NAME (fn) != NULL && DECL_ONE_ONLY (fn))
366 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
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;
375 /* Set up cloned argument trees for the thunk. */
376 t = NULL_TREE;
377 for (a = DECL_ARGUMENTS (function); a; a = DECL_CHAIN (a))
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;
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);
398 pop_from_top_level ();
401 /* Code for synthesizing methods which have default semantics defined. */
403 /* True iff CTYPE has a trivial SFK. */
405 static bool
406 type_has_trivial_fn (tree ctype, special_function_kind sfk)
408 switch (sfk)
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 ();
429 /* Note that CTYPE has a non-trivial SFK even though we previously thought
430 it was trivial. */
432 static void
433 type_set_nontrivial_flag (tree ctype, special_function_kind sfk)
435 switch (sfk)
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 ();
461 /* True iff FN is a trivial defaulted member function ([cd]tor, op=). */
463 bool
464 trivial_fn_p (tree fn)
466 if (TREE_CODE (fn) == TEMPLATE_DECL)
467 return false;
468 if (!DECL_DEFAULTED_FN (fn))
469 return false;
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));
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. */
480 tree
481 forward_parm (tree parm)
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 exp = build_static_cast (type, exp, tf_warning_or_error);
490 if (DECL_PACK_P (parm))
491 exp = make_pack_expansion (exp);
492 return exp;
495 /* Strip all inheriting constructors, if any, to return the original
496 constructor from a (possibly indirect) base class. */
498 tree
499 strip_inheriting_ctors (tree fn)
501 gcc_assert (flag_new_inheriting_ctors);
502 while (tree inh = DECL_INHERITED_CTOR (fn))
504 inh = OVL_CURRENT (inh);
505 fn = inh;
507 return fn;
510 /* Find the binfo for the base subobject of BINFO being initialized by
511 inherited constructor FNDECL (a member of a direct base of BINFO). */
513 static tree inherited_ctor_binfo (tree, tree);
514 static tree
515 inherited_ctor_binfo_1 (tree binfo, tree fndecl)
517 tree base = DECL_CONTEXT (fndecl);
518 tree base_binfo;
519 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
520 if (BINFO_TYPE (base_binfo) == base)
521 return inherited_ctor_binfo (base_binfo, fndecl);
523 gcc_unreachable();
526 /* Find the binfo for the base subobject of BINFO being initialized by
527 inheriting constructor FNDECL (a member of BINFO), or BINFO if FNDECL is not
528 an inheriting constructor. */
530 static tree
531 inherited_ctor_binfo (tree binfo, tree fndecl)
533 tree inh = DECL_INHERITED_CTOR (fndecl);
534 if (!inh)
535 return binfo;
537 tree results = NULL_TREE;
538 for (; inh; inh = OVL_NEXT (inh))
540 tree one = inherited_ctor_binfo_1 (binfo, OVL_CURRENT (inh));
541 if (!results)
542 results = one;
543 else if (one != results)
544 results = tree_cons (NULL_TREE, one, results);
546 return results;
549 /* Find the binfo for the base subobject being initialized by inheriting
550 constructor FNDECL, or NULL_TREE if FNDECL is not an inheriting
551 constructor. */
553 tree
554 inherited_ctor_binfo (tree fndecl)
556 if (!DECL_INHERITED_CTOR (fndecl))
557 return NULL_TREE;
558 tree binfo = TYPE_BINFO (DECL_CONTEXT (fndecl));
559 return inherited_ctor_binfo (binfo, fndecl);
562 /* True if we should omit all user-declared parameters from constructor FN,
563 because it is a base clone of a ctor inherited from a virtual base. */
565 bool
566 ctor_omit_inherited_parms (tree fn)
568 if (!flag_new_inheriting_ctors)
569 /* We only optimize away the parameters in the new model. */
570 return false;
571 if (!DECL_BASE_CONSTRUCTOR_P (fn)
572 || !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
573 return false;
574 tree binfo = inherited_ctor_binfo (fn);
575 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
576 if (BINFO_VIRTUAL_P (binfo))
577 return true;
578 return false;
581 /* True iff constructor(s) INH inherited into BINFO initializes INIT_BINFO.
582 This can be true for multiple virtual bases as well as one direct
583 non-virtual base. */
585 static bool
586 binfo_inherited_from (tree binfo, tree init_binfo, tree inh)
588 /* inh is an OVERLOAD if we inherited the same constructor along
589 multiple paths, check all of them. */
590 for (; inh; inh = OVL_NEXT (inh))
592 tree fn = OVL_CURRENT (inh);
593 tree base = DECL_CONTEXT (fn);
594 tree base_binfo = NULL_TREE;
595 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
596 if (BINFO_TYPE (base_binfo) == base)
597 break;
598 if (base_binfo == init_binfo
599 || (flag_new_inheriting_ctors
600 && binfo_inherited_from (base_binfo, init_binfo,
601 DECL_INHERITED_CTOR (fn))))
602 return true;
604 return false;
607 /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO
608 given the parameter or parameters PARM, possibly inherited constructor
609 base INH, or move flag MOVE_P. */
611 static tree
612 add_one_base_init (tree binfo, tree parm, bool move_p, tree inh,
613 tree member_init_list)
615 tree init;
616 if (inh)
618 /* An inheriting constructor only has a mem-initializer for
619 the base it inherits from. */
620 if (!binfo_inherited_from (TYPE_BINFO (current_class_type), binfo, inh))
621 return member_init_list;
623 tree *p = &init;
624 init = NULL_TREE;
625 for (; parm; parm = DECL_CHAIN (parm))
627 tree exp = forward_parm (parm);
628 *p = build_tree_list (NULL_TREE, exp);
629 p = &TREE_CHAIN (*p);
632 else
634 init = build_base_path (PLUS_EXPR, parm, binfo, 1,
635 tf_warning_or_error);
636 if (move_p)
637 init = move (init);
638 init = build_tree_list (NULL_TREE, init);
640 return tree_cons (binfo, init, member_init_list);
643 /* Generate code for default X(X&) or X(X&&) constructor or an inheriting
644 constructor. */
646 static void
647 do_build_copy_constructor (tree fndecl)
649 tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
650 bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
651 bool trivial = trivial_fn_p (fndecl);
652 tree inh = DECL_INHERITED_CTOR (fndecl);
654 if (!inh)
655 parm = convert_from_reference (parm);
657 if (trivial)
659 if (is_empty_class (current_class_type))
660 /* Don't copy the padding byte; it might not have been allocated
661 if *this is a base subobject. */;
662 else if (tree_int_cst_equal (TYPE_SIZE (current_class_type),
663 CLASSTYPE_SIZE (current_class_type)))
665 tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm);
666 finish_expr_stmt (t);
668 else
670 /* We must only copy the non-tail padding parts. */
671 tree base_size = CLASSTYPE_SIZE_UNIT (current_class_type);
672 base_size = size_binop (MINUS_EXPR, base_size, size_int (1));
673 tree array_type = build_array_type (unsigned_char_type_node,
674 build_index_type (base_size));
675 tree alias_set = build_int_cst (TREE_TYPE (current_class_ptr), 0);
676 tree lhs = build2 (MEM_REF, array_type,
677 current_class_ptr, alias_set);
678 tree rhs = build2 (MEM_REF, array_type,
679 TREE_OPERAND (parm, 0), alias_set);
680 tree t = build2 (INIT_EXPR, void_type_node, lhs, rhs);
681 finish_expr_stmt (t);
684 else
686 tree fields = TYPE_FIELDS (current_class_type);
687 tree member_init_list = NULL_TREE;
688 int cvquals = cp_type_quals (TREE_TYPE (parm));
689 int i;
690 tree binfo, base_binfo;
691 tree init;
692 vec<tree, va_gc> *vbases;
694 /* Initialize all the base-classes with the parameter converted
695 to their type so that we get their copy constructor and not
696 another constructor that takes current_class_type. We must
697 deal with the binfo's directly as a direct base might be
698 inaccessible due to ambiguity. */
699 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
700 vec_safe_iterate (vbases, i, &binfo); i++)
702 member_init_list = add_one_base_init (binfo, parm, move_p, inh,
703 member_init_list);
706 for (binfo = TYPE_BINFO (current_class_type), i = 0;
707 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
709 if (BINFO_VIRTUAL_P (base_binfo))
710 continue;
711 member_init_list = add_one_base_init (base_binfo, parm, move_p,
712 inh, member_init_list);
715 for (; fields; fields = DECL_CHAIN (fields))
717 tree field = fields;
718 tree expr_type;
720 if (TREE_CODE (field) != FIELD_DECL)
721 continue;
722 if (inh)
723 continue;
725 expr_type = TREE_TYPE (field);
726 if (DECL_NAME (field))
728 if (VFIELD_NAME_P (DECL_NAME (field)))
729 continue;
731 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
732 /* Just use the field; anonymous types can't have
733 nontrivial copy ctors or assignment ops or this
734 function would be deleted. */;
735 else
736 continue;
738 /* Compute the type of "init->field". If the copy-constructor
739 parameter is, for example, "const S&", and the type of
740 the field is "T", then the type will usually be "const
741 T". (There are no cv-qualified variants of reference
742 types.) */
743 if (TREE_CODE (expr_type) != REFERENCE_TYPE)
745 int quals = cvquals;
747 if (DECL_MUTABLE_P (field))
748 quals &= ~TYPE_QUAL_CONST;
749 quals |= cp_type_quals (expr_type);
750 expr_type = cp_build_qualified_type (expr_type, quals);
753 init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
754 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
755 /* 'move' breaks bit-fields, and has no effect for scalars. */
756 && !scalarish_type_p (expr_type))
757 init = move (init);
758 init = build_tree_list (NULL_TREE, init);
760 member_init_list = tree_cons (field, init, member_init_list);
762 finish_mem_initializers (member_init_list);
766 static void
767 do_build_copy_assign (tree fndecl)
769 tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl));
770 tree compound_stmt;
771 bool move_p = move_fn_p (fndecl);
772 bool trivial = trivial_fn_p (fndecl);
773 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
775 compound_stmt = begin_compound_stmt (0);
776 parm = convert_from_reference (parm);
778 if (trivial
779 && is_empty_class (current_class_type))
780 /* Don't copy the padding byte; it might not have been allocated
781 if *this is a base subobject. */;
782 else if (trivial)
784 tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
785 finish_expr_stmt (t);
787 else
789 tree fields;
790 int cvquals = cp_type_quals (TREE_TYPE (parm));
791 int i;
792 tree binfo, base_binfo;
794 /* Assign to each of the direct base classes. */
795 for (binfo = TYPE_BINFO (current_class_type), i = 0;
796 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
798 tree converted_parm;
799 vec<tree, va_gc> *parmvec;
801 /* We must convert PARM directly to the base class
802 explicitly since the base class may be ambiguous. */
803 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
804 tf_warning_or_error);
805 if (move_p)
806 converted_parm = move (converted_parm);
807 /* Call the base class assignment operator. */
808 parmvec = make_tree_vector_single (converted_parm);
809 finish_expr_stmt
810 (build_special_member_call (current_class_ref,
811 ansi_assopname (NOP_EXPR),
812 &parmvec,
813 base_binfo,
814 flags,
815 tf_warning_or_error));
816 release_tree_vector (parmvec);
819 /* Assign to each of the non-static data members. */
820 for (fields = TYPE_FIELDS (current_class_type);
821 fields;
822 fields = DECL_CHAIN (fields))
824 tree comp = current_class_ref;
825 tree init = parm;
826 tree field = fields;
827 tree expr_type;
828 int quals;
830 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
831 continue;
833 expr_type = TREE_TYPE (field);
835 if (CP_TYPE_CONST_P (expr_type))
837 error ("non-static const member %q#D, can%'t use default "
838 "assignment operator", field);
839 continue;
841 else if (TREE_CODE (expr_type) == REFERENCE_TYPE)
843 error ("non-static reference member %q#D, can%'t use "
844 "default assignment operator", field);
845 continue;
848 if (DECL_NAME (field))
850 if (VFIELD_NAME_P (DECL_NAME (field)))
851 continue;
853 else if (ANON_AGGR_TYPE_P (expr_type)
854 && TYPE_FIELDS (expr_type) != NULL_TREE)
855 /* Just use the field; anonymous types can't have
856 nontrivial copy ctors or assignment ops or this
857 function would be deleted. */;
858 else
859 continue;
861 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
863 /* Compute the type of init->field */
864 quals = cvquals;
865 if (DECL_MUTABLE_P (field))
866 quals &= ~TYPE_QUAL_CONST;
867 expr_type = cp_build_qualified_type (expr_type, quals);
869 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
870 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
871 /* 'move' breaks bit-fields, and has no effect for scalars. */
872 && !scalarish_type_p (expr_type))
873 init = move (init);
875 if (DECL_NAME (field))
876 init = cp_build_modify_expr (input_location, comp, NOP_EXPR, init,
877 tf_warning_or_error);
878 else
879 init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
880 finish_expr_stmt (init);
883 finish_return_stmt (current_class_ref);
884 finish_compound_stmt (compound_stmt);
887 /* Synthesize FNDECL, a non-static member function. */
889 void
890 synthesize_method (tree fndecl)
892 bool nested = (current_function_decl != NULL_TREE);
893 tree context = decl_function_context (fndecl);
894 bool need_body = true;
895 tree stmt;
896 location_t save_input_location = input_location;
897 int error_count = errorcount;
898 int warning_count = warningcount + werrorcount;
900 /* Reset the source location, we might have been previously
901 deferred, and thus have saved where we were first needed. */
902 DECL_SOURCE_LOCATION (fndecl)
903 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
905 /* If we've been asked to synthesize a clone, just synthesize the
906 cloned function instead. Doing so will automatically fill in the
907 body for the clone. */
908 if (DECL_CLONED_FUNCTION_P (fndecl))
909 fndecl = DECL_CLONED_FUNCTION (fndecl);
911 /* We may be in the middle of deferred access check. Disable
912 it now. */
913 push_deferring_access_checks (dk_no_deferred);
915 if (! context)
916 push_to_top_level ();
917 else if (nested)
918 push_function_context ();
920 input_location = DECL_SOURCE_LOCATION (fndecl);
922 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
923 stmt = begin_function_body ();
925 if (DECL_OVERLOADED_OPERATOR_P (fndecl) == NOP_EXPR)
927 do_build_copy_assign (fndecl);
928 need_body = false;
930 else if (DECL_CONSTRUCTOR_P (fndecl))
932 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
933 if (arg_chain != void_list_node)
934 do_build_copy_constructor (fndecl);
935 else
936 finish_mem_initializers (NULL_TREE);
939 /* If we haven't yet generated the body of the function, just
940 generate an empty compound statement. */
941 if (need_body)
943 tree compound_stmt;
944 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
945 finish_compound_stmt (compound_stmt);
948 finish_function_body (stmt);
949 expand_or_defer_fn (finish_function (0));
951 input_location = save_input_location;
953 if (! context)
954 pop_from_top_level ();
955 else if (nested)
956 pop_function_context ();
958 pop_deferring_access_checks ();
960 if (error_count != errorcount || warning_count != warningcount + werrorcount)
961 inform (input_location, "synthesized method %qD first required here ",
962 fndecl);
965 /* Build a reference to type TYPE with cv-quals QUALS, which is an
966 rvalue if RVALUE is true. */
968 static tree
969 build_stub_type (tree type, int quals, bool rvalue)
971 tree argtype = cp_build_qualified_type (type, quals);
972 return cp_build_reference_type (argtype, rvalue);
975 /* Build a dummy glvalue from dereferencing a dummy reference of type
976 REFTYPE. */
978 static tree
979 build_stub_object (tree reftype)
981 if (TREE_CODE (reftype) != REFERENCE_TYPE)
982 reftype = cp_build_reference_type (reftype, /*rval*/true);
983 tree stub = build1 (CONVERT_EXPR, reftype, integer_one_node);
984 return convert_from_reference (stub);
987 /* Determine which function will be called when looking up NAME in TYPE,
988 called with a single ARGTYPE argument, or no argument if ARGTYPE is
989 null. FLAGS and COMPLAIN are as for build_new_method_call.
991 Returns a FUNCTION_DECL if all is well.
992 Returns NULL_TREE if overload resolution failed.
993 Returns error_mark_node if the chosen function cannot be called. */
995 static tree
996 locate_fn_flags (tree type, tree name, tree argtype, int flags,
997 tsubst_flags_t complain)
999 tree ob, fn, fns, binfo, rval;
1000 vec<tree, va_gc> *args;
1002 if (TYPE_P (type))
1003 binfo = TYPE_BINFO (type);
1004 else
1006 binfo = type;
1007 type = BINFO_TYPE (binfo);
1010 ob = build_stub_object (cp_build_reference_type (type, false));
1011 args = make_tree_vector ();
1012 if (argtype)
1014 if (TREE_CODE (argtype) == TREE_LIST)
1016 for (tree elt = argtype; elt && elt != void_list_node;
1017 elt = TREE_CHAIN (elt))
1019 tree type = TREE_VALUE (elt);
1020 tree arg = build_stub_object (type);
1021 vec_safe_push (args, arg);
1024 else
1026 tree arg = build_stub_object (argtype);
1027 args->quick_push (arg);
1031 fns = lookup_fnfields (binfo, name, 0);
1032 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
1034 release_tree_vector (args);
1035 if (fn && rval == error_mark_node)
1036 return rval;
1037 else
1038 return fn;
1041 /* Locate the dtor of TYPE. */
1043 tree
1044 get_dtor (tree type, tsubst_flags_t complain)
1046 tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
1047 LOOKUP_NORMAL, complain);
1048 if (fn == error_mark_node)
1049 return NULL_TREE;
1050 return fn;
1053 /* Locate the default ctor of TYPE. */
1055 tree
1056 locate_ctor (tree type)
1058 tree fn;
1060 push_deferring_access_checks (dk_no_check);
1061 fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
1062 LOOKUP_SPECULATIVE, tf_none);
1063 pop_deferring_access_checks ();
1064 if (fn == error_mark_node)
1065 return NULL_TREE;
1066 return fn;
1069 /* Likewise, but give any appropriate errors. */
1071 tree
1072 get_default_ctor (tree type)
1074 tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
1075 LOOKUP_NORMAL, tf_warning_or_error);
1076 if (fn == error_mark_node)
1077 return NULL_TREE;
1078 return fn;
1081 /* Locate the copy ctor of TYPE. */
1083 tree
1084 get_copy_ctor (tree type, tsubst_flags_t complain)
1086 int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
1087 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
1088 tree argtype = build_stub_type (type, quals, false);
1089 tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
1090 LOOKUP_NORMAL, complain);
1091 if (fn == error_mark_node)
1092 return NULL_TREE;
1093 return fn;
1096 /* Locate the copy assignment operator of TYPE. */
1098 tree
1099 get_copy_assign (tree type)
1101 int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
1102 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
1103 tree argtype = build_stub_type (type, quals, false);
1104 tree fn = locate_fn_flags (type, ansi_assopname (NOP_EXPR), argtype,
1105 LOOKUP_NORMAL, tf_warning_or_error);
1106 if (fn == error_mark_node)
1107 return NULL_TREE;
1108 return fn;
1111 /* walk_tree helper function for is_trivially_xible. If *TP is a call,
1112 return it if it calls something other than a trivial special member
1113 function. */
1115 static tree
1116 check_nontriv (tree *tp, int *, void *)
1118 tree fn = cp_get_callee (*tp);
1119 if (fn == NULL_TREE)
1120 return NULL_TREE;
1122 if (TREE_CODE (fn) == ADDR_EXPR)
1123 fn = TREE_OPERAND (fn, 0);
1125 if (TREE_CODE (fn) != FUNCTION_DECL
1126 || !trivial_fn_p (fn))
1127 return fn;
1128 return NULL_TREE;
1131 /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */
1133 static tree
1134 assignable_expr (tree to, tree from)
1136 ++cp_unevaluated_operand;
1137 to = build_stub_object (to);
1138 from = build_stub_object (from);
1139 tree r = cp_build_modify_expr (input_location, to, NOP_EXPR, from, tf_none);
1140 --cp_unevaluated_operand;
1141 return r;
1144 /* The predicate condition for a template specialization
1145 is_constructible<T, Args...> shall be satisfied if and only if the
1146 following variable definition would be well-formed for some invented
1147 variable t: T t(create<Args>()...);
1149 Return something equivalent in well-formedness and triviality. */
1151 static tree
1152 constructible_expr (tree to, tree from)
1154 tree expr;
1155 if (CLASS_TYPE_P (to))
1157 tree ctype = to;
1158 vec<tree, va_gc> *args = NULL;
1159 if (TREE_CODE (to) != REFERENCE_TYPE)
1160 to = cp_build_reference_type (to, /*rval*/false);
1161 tree ob = build_stub_object (to);
1162 for (; from; from = TREE_CHAIN (from))
1163 vec_safe_push (args, build_stub_object (TREE_VALUE (from)));
1164 expr = build_special_member_call (ob, complete_ctor_identifier, &args,
1165 ctype, LOOKUP_NORMAL, tf_none);
1166 if (expr == error_mark_node)
1167 return error_mark_node;
1168 /* The current state of the standard vis-a-vis LWG 2116 is that
1169 is_*constructible involves destruction as well. */
1170 if (type_build_dtor_call (ctype))
1172 tree dtor = build_special_member_call (ob, complete_dtor_identifier,
1173 NULL, ctype, LOOKUP_NORMAL,
1174 tf_none);
1175 if (dtor == error_mark_node)
1176 return error_mark_node;
1177 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
1178 expr = build2 (COMPOUND_EXPR, void_type_node, expr, dtor);
1181 else
1183 if (from == NULL_TREE)
1184 return build_value_init (to, tf_none);
1185 else if (TREE_CHAIN (from))
1186 return error_mark_node; // too many initializers
1187 from = build_stub_object (TREE_VALUE (from));
1188 expr = perform_direct_initialization_if_possible (to, from,
1189 /*cast*/false,
1190 tf_none);
1192 return expr;
1195 /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or
1196 constructible (otherwise) from FROM, which is a single type for
1197 assignment or a list of types for construction. */
1199 bool
1200 is_trivially_xible (enum tree_code code, tree to, tree from)
1202 tree expr;
1203 if (code == MODIFY_EXPR)
1204 expr = assignable_expr (to, from);
1205 else if (from && TREE_CHAIN (from))
1206 return false; // only 0- and 1-argument ctors can be trivial
1207 else
1208 expr = constructible_expr (to, from);
1210 if (expr == error_mark_node)
1211 return false;
1212 tree nt = cp_walk_tree_without_duplicates (&expr, check_nontriv, NULL);
1213 return !nt;
1216 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
1217 DELETED_P or give an error message MSG with argument ARG. */
1219 static void
1220 process_subob_fn (tree fn, tree *spec_p, bool *trivial_p,
1221 bool *deleted_p, bool *constexpr_p,
1222 bool diag, tree arg, bool dtor_from_ctor = false)
1224 if (!fn || fn == error_mark_node)
1226 if (deleted_p)
1227 *deleted_p = true;
1228 return;
1231 if (spec_p)
1233 maybe_instantiate_noexcept (fn);
1234 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
1235 *spec_p = merge_exception_specifiers (*spec_p, raises);
1238 if (!trivial_fn_p (fn) && !dtor_from_ctor)
1240 if (trivial_p)
1241 *trivial_p = false;
1242 if (TREE_CODE (arg) == FIELD_DECL
1243 && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
1245 if (deleted_p)
1246 *deleted_p = true;
1247 if (diag)
1248 error ("union member %q+D with non-trivial %qD", arg, fn);
1252 if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
1254 *constexpr_p = false;
1255 if (diag)
1257 inform (DECL_SOURCE_LOCATION (fn),
1258 "defaulted constructor calls non-constexpr %qD", fn);
1259 explain_invalid_constexpr_fn (fn);
1264 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
1265 aggregates. If DTOR_FROM_CTOR is true, we're walking subobject destructors
1266 called from a synthesized constructor, in which case we don't consider
1267 the triviality of the subobject destructor. */
1269 static void
1270 walk_field_subobs (tree fields, tree fnname, special_function_kind sfk,
1271 int quals, bool copy_arg_p, bool move_p,
1272 bool assign_p, tree *spec_p, bool *trivial_p,
1273 bool *deleted_p, bool *constexpr_p,
1274 bool diag, int flags, tsubst_flags_t complain,
1275 bool dtor_from_ctor)
1277 tree field;
1278 for (field = fields; field; field = DECL_CHAIN (field))
1280 tree mem_type, argtype, rval;
1282 if (TREE_CODE (field) != FIELD_DECL
1283 || DECL_ARTIFICIAL (field))
1284 continue;
1286 mem_type = strip_array_types (TREE_TYPE (field));
1287 if (assign_p)
1289 bool bad = true;
1290 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
1292 if (diag)
1293 error ("non-static const member %q#D, can%'t use default "
1294 "assignment operator", field);
1296 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1298 if (diag)
1299 error ("non-static reference member %q#D, can%'t use "
1300 "default assignment operator", field);
1302 else
1303 bad = false;
1305 if (bad && deleted_p)
1306 *deleted_p = true;
1308 else if (sfk == sfk_constructor)
1310 bool bad;
1312 if (DECL_INITIAL (field))
1314 if (diag && DECL_INITIAL (field) == error_mark_node)
1315 inform (DECL_SOURCE_LOCATION (field),
1316 "initializer for %q#D is invalid", field);
1317 if (trivial_p)
1318 *trivial_p = false;
1319 /* Core 1351: If the field has an NSDMI that could throw, the
1320 default constructor is noexcept(false). */
1321 if (spec_p)
1323 tree nsdmi = get_nsdmi (field, /*ctor*/false);
1324 if (!expr_noexcept_p (nsdmi, complain))
1325 *spec_p = noexcept_false_spec;
1327 /* Don't do the normal processing. */
1328 continue;
1331 bad = false;
1332 if (CP_TYPE_CONST_P (mem_type)
1333 && default_init_uninitialized_part (mem_type))
1335 if (diag)
1337 error ("uninitialized const member in %q#T",
1338 current_class_type);
1339 inform (DECL_SOURCE_LOCATION (field),
1340 "%q#D should be initialized", field);
1342 bad = true;
1344 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1346 if (diag)
1348 error ("uninitialized reference member in %q#T",
1349 current_class_type);
1350 inform (DECL_SOURCE_LOCATION (field),
1351 "%q#D should be initialized", field);
1353 bad = true;
1356 if (bad && deleted_p)
1357 *deleted_p = true;
1359 /* For an implicitly-defined default constructor to be constexpr,
1360 every member must have a user-provided default constructor or
1361 an explicit initializer. */
1362 if (constexpr_p && !CLASS_TYPE_P (mem_type)
1363 && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE)
1365 *constexpr_p = false;
1366 if (diag)
1367 inform (DECL_SOURCE_LOCATION (field),
1368 "defaulted default constructor does not "
1369 "initialize %q#D", field);
1372 else if (sfk == sfk_copy_constructor)
1374 /* 12.8p11b5 */
1375 if (TREE_CODE (mem_type) == REFERENCE_TYPE
1376 && TYPE_REF_IS_RVALUE (mem_type))
1378 if (diag)
1379 error ("copying non-static data member %q#D of rvalue "
1380 "reference type", field);
1381 if (deleted_p)
1382 *deleted_p = true;
1386 if (!CLASS_TYPE_P (mem_type))
1387 continue;
1389 if (ANON_AGGR_TYPE_P (mem_type))
1391 walk_field_subobs (TYPE_FIELDS (mem_type), fnname, sfk, quals,
1392 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1393 deleted_p, constexpr_p,
1394 diag, flags, complain, dtor_from_ctor);
1395 continue;
1398 if (copy_arg_p)
1400 int mem_quals = cp_type_quals (mem_type) | quals;
1401 if (DECL_MUTABLE_P (field))
1402 mem_quals &= ~TYPE_QUAL_CONST;
1403 argtype = build_stub_type (mem_type, mem_quals, move_p);
1405 else
1406 argtype = NULL_TREE;
1408 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
1410 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1411 constexpr_p, diag, field, dtor_from_ctor);
1415 /* The caller wants to generate an implicit declaration of SFK for
1416 CTYPE which is const if relevant and CONST_P is set. If SPEC_P,
1417 TRIVIAL_P, DELETED_P or CONSTEXPR_P are non-null, set their
1418 referent appropriately. If DIAG is true, we're either being called
1419 from maybe_explain_implicit_delete to give errors, or if
1420 CONSTEXPR_P is non-null, from explain_invalid_constexpr_fn. */
1422 static void
1423 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
1424 tree *spec_p, bool *trivial_p, bool *deleted_p,
1425 bool *constexpr_p, bool diag,
1426 tree inheriting_ctor, tree inherited_parms)
1428 tree binfo, base_binfo, scope, fnname, rval, argtype;
1429 bool move_p, copy_arg_p, assign_p, expected_trivial, check_vdtor;
1430 vec<tree, va_gc> *vbases;
1431 int i, quals, flags;
1432 tsubst_flags_t complain;
1433 bool ctor_p;
1435 if (spec_p)
1436 *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec);
1438 if (deleted_p)
1440 /* "The closure type associated with a lambda-expression has a deleted
1441 default constructor and a deleted copy assignment operator."
1442 This is diagnosed in maybe_explain_implicit_delete. */
1443 if (LAMBDA_TYPE_P (ctype)
1444 && (sfk == sfk_constructor
1445 || sfk == sfk_copy_assignment))
1447 *deleted_p = true;
1448 return;
1451 *deleted_p = false;
1454 ctor_p = false;
1455 assign_p = false;
1456 check_vdtor = false;
1457 switch (sfk)
1459 case sfk_move_assignment:
1460 case sfk_copy_assignment:
1461 assign_p = true;
1462 fnname = ansi_assopname (NOP_EXPR);
1463 break;
1465 case sfk_destructor:
1466 check_vdtor = true;
1467 /* The synthesized method will call base dtors, but check complete
1468 here to avoid having to deal with VTT. */
1469 fnname = complete_dtor_identifier;
1470 break;
1472 case sfk_constructor:
1473 case sfk_move_constructor:
1474 case sfk_copy_constructor:
1475 case sfk_inheriting_constructor:
1476 ctor_p = true;
1477 fnname = complete_ctor_identifier;
1478 break;
1480 default:
1481 gcc_unreachable ();
1484 gcc_assert ((sfk == sfk_inheriting_constructor)
1485 == (inheriting_ctor != NULL_TREE));
1487 /* If that user-written default constructor would satisfy the
1488 requirements of a constexpr constructor (7.1.5), the
1489 implicitly-defined default constructor is constexpr.
1491 The implicitly-defined copy/move assignment operator is constexpr if
1492 - X is a literal type, and
1493 - the assignment operator selected to copy/move each direct base class
1494 subobject is a constexpr function, and
1495 - for each non-static data member of X that is of class type (or array
1496 thereof), the assignment operator selected to copy/move that member is a
1497 constexpr function. */
1498 if (constexpr_p)
1499 *constexpr_p = ctor_p
1500 || (assign_p && cxx_dialect >= cxx14);
1502 move_p = false;
1503 switch (sfk)
1505 case sfk_constructor:
1506 case sfk_destructor:
1507 case sfk_inheriting_constructor:
1508 copy_arg_p = false;
1509 break;
1511 case sfk_move_constructor:
1512 case sfk_move_assignment:
1513 move_p = true;
1514 /* FALLTHRU */
1515 case sfk_copy_constructor:
1516 case sfk_copy_assignment:
1517 copy_arg_p = true;
1518 break;
1520 default:
1521 gcc_unreachable ();
1524 expected_trivial = type_has_trivial_fn (ctype, sfk);
1525 if (trivial_p)
1526 *trivial_p = expected_trivial;
1528 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
1529 class versions and other properties of the type. But a subobject
1530 class can be trivially copyable and yet have overload resolution
1531 choose a template constructor for initialization, depending on
1532 rvalueness and cv-quals. And furthermore, a member in a base might
1533 be trivial but deleted or otherwise not callable. So we can't exit
1534 early in C++0x. The same considerations apply in C++98/03, but
1535 there the definition of triviality does not consider overload
1536 resolution, so a constructor can be trivial even if it would otherwise
1537 call a non-trivial constructor. */
1538 if (expected_trivial
1539 && (!copy_arg_p || cxx_dialect < cxx11))
1541 if (constexpr_p && sfk == sfk_constructor)
1543 bool cx = trivial_default_constructor_is_constexpr (ctype);
1544 *constexpr_p = cx;
1545 if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
1546 /* A trivial constructor doesn't have any NSDMI. */
1547 inform (input_location, "defaulted default constructor does "
1548 "not initialize any non-static data member");
1550 if (!diag && cxx_dialect < cxx11)
1551 return;
1554 ++cp_unevaluated_operand;
1555 ++c_inhibit_evaluation_warnings;
1556 push_deferring_access_checks (dk_no_deferred);
1558 scope = push_scope (ctype);
1560 flags = LOOKUP_NORMAL|LOOKUP_SPECULATIVE;
1561 if (!inheriting_ctor)
1562 flags |= LOOKUP_DEFAULTED;
1564 complain = diag ? tf_warning_or_error : tf_none;
1566 if (const_p)
1567 quals = TYPE_QUAL_CONST;
1568 else
1569 quals = TYPE_UNQUALIFIED;
1570 argtype = NULL_TREE;
1572 for (binfo = TYPE_BINFO (ctype), i = 0;
1573 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
1575 tree basetype = BINFO_TYPE (base_binfo);
1577 if (!assign_p && BINFO_VIRTUAL_P (base_binfo))
1578 /* We'll handle virtual bases below. */
1579 continue;
1581 bool inherited_binfo = false;
1583 if (copy_arg_p)
1584 argtype = build_stub_type (basetype, quals, move_p);
1585 else if ((inherited_binfo
1586 = binfo_inherited_from (binfo, base_binfo, inheriting_ctor)))
1588 /* Don't check access on the inherited constructor. */
1589 argtype = inherited_parms;
1590 if (flag_new_inheriting_ctors)
1591 push_deferring_access_checks (dk_deferred);
1593 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1594 if (inherited_binfo)
1596 if (flag_new_inheriting_ctors)
1597 pop_deferring_access_checks ();
1598 argtype = NULL_TREE;
1601 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1602 constexpr_p, diag, basetype);
1603 if (ctor_p)
1605 /* In a constructor we also need to check the subobject
1606 destructors for cleanup of partially constructed objects. */
1607 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1608 NULL_TREE, flags, complain);
1609 /* Note that we don't pass down trivial_p; the subobject
1610 destructors don't affect triviality of the constructor. Nor
1611 do they affect constexpr-ness (a constant expression doesn't
1612 throw) or exception-specification (a throw from one of the
1613 dtors would be a double-fault). */
1614 process_subob_fn (rval, NULL, NULL,
1615 deleted_p, NULL, false,
1616 basetype, /*dtor_from_ctor*/true);
1619 if (check_vdtor && type_has_virtual_destructor (basetype))
1621 rval = locate_fn_flags (ctype, ansi_opname (DELETE_EXPR),
1622 ptr_type_node, flags, complain);
1623 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
1624 to have a null rval (no class-specific op delete). */
1625 if (rval && rval == error_mark_node && deleted_p)
1626 *deleted_p = true;
1627 check_vdtor = false;
1630 if (diag && assign_p && move_p
1631 && BINFO_VIRTUAL_P (base_binfo)
1632 && rval && TREE_CODE (rval) == FUNCTION_DECL
1633 && move_fn_p (rval) && !trivial_fn_p (rval)
1634 && vbase_has_user_provided_move_assign (basetype))
1635 warning (OPT_Wvirtual_move_assign,
1636 "defaulted move assignment for %qT calls a non-trivial "
1637 "move assignment operator for virtual base %qT",
1638 ctype, basetype);
1641 vbases = CLASSTYPE_VBASECLASSES (ctype);
1642 if (assign_p)
1643 /* No need to examine vbases here. */;
1644 else if (vec_safe_is_empty (vbases))
1645 /* No virtual bases to worry about. */;
1646 else if (ABSTRACT_CLASS_TYPE_P (ctype) && cxx_dialect >= cxx14)
1647 /* Vbase cdtors are not relevant. */;
1648 else
1650 if (constexpr_p)
1651 *constexpr_p = false;
1652 FOR_EACH_VEC_ELT (*vbases, i, base_binfo)
1654 tree basetype = BINFO_TYPE (base_binfo);
1655 bool inherited_binfo = false;
1657 if (copy_arg_p)
1658 argtype = build_stub_type (basetype, quals, move_p);
1659 else if ((inherited_binfo
1660 = binfo_inherited_from (binfo, base_binfo, inheriting_ctor)))
1662 argtype = inherited_parms;
1663 if (flag_new_inheriting_ctors)
1664 push_deferring_access_checks (dk_deferred);
1666 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1667 if (inherited_binfo)
1669 if (flag_new_inheriting_ctors)
1670 pop_deferring_access_checks ();
1671 argtype = NULL_TREE;
1674 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1675 constexpr_p, diag, basetype);
1676 if (ctor_p && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype))
1678 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1679 NULL_TREE, flags, complain);
1680 process_subob_fn (rval, NULL, NULL,
1681 deleted_p, NULL, false,
1682 basetype, /*dtor_from_ctor*/true);
1687 /* Now handle the non-static data members. */
1688 walk_field_subobs (TYPE_FIELDS (ctype), fnname, sfk, quals,
1689 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1690 deleted_p, constexpr_p,
1691 diag, flags, complain, /*dtor_from_ctor*/false);
1692 if (ctor_p)
1693 walk_field_subobs (TYPE_FIELDS (ctype), complete_dtor_identifier,
1694 sfk_destructor, TYPE_UNQUALIFIED, false,
1695 false, false, NULL, NULL,
1696 deleted_p, NULL,
1697 false, flags, complain, /*dtor_from_ctor*/true);
1699 pop_scope (scope);
1701 pop_deferring_access_checks ();
1702 --cp_unevaluated_operand;
1703 --c_inhibit_evaluation_warnings;
1706 /* DECL is a defaulted function whose exception specification is now
1707 needed. Return what it should be. */
1709 tree
1710 get_defaulted_eh_spec (tree decl)
1712 if (DECL_CLONED_FUNCTION_P (decl))
1713 decl = DECL_CLONED_FUNCTION (decl);
1714 special_function_kind sfk = special_function_p (decl);
1715 tree ctype = DECL_CONTEXT (decl);
1716 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1717 tree parm_type = TREE_VALUE (parms);
1718 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1719 tree spec = empty_except_spec;
1720 synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL,
1721 NULL, false, DECL_INHERITED_CTOR (decl),
1722 parms);
1723 return spec;
1726 /* DECL is a deleted function. If it's implicitly deleted, explain why and
1727 return true; else return false. */
1729 bool
1730 maybe_explain_implicit_delete (tree decl)
1732 /* If decl is a clone, get the primary variant. */
1733 decl = DECL_ORIGIN (decl);
1734 gcc_assert (DECL_DELETED_FN (decl));
1735 if (DECL_DEFAULTED_FN (decl))
1737 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
1738 static hash_set<tree> *explained;
1740 special_function_kind sfk;
1741 location_t loc;
1742 bool informed;
1743 tree ctype;
1745 if (!explained)
1746 explained = new hash_set<tree>;
1747 if (explained->add (decl))
1748 return true;
1750 sfk = special_function_p (decl);
1751 ctype = DECL_CONTEXT (decl);
1752 loc = input_location;
1753 input_location = DECL_SOURCE_LOCATION (decl);
1755 informed = false;
1756 if (LAMBDA_TYPE_P (ctype))
1758 informed = true;
1759 if (sfk == sfk_constructor)
1760 inform (DECL_SOURCE_LOCATION (decl),
1761 "a lambda closure type has a deleted default constructor");
1762 else if (sfk == sfk_copy_assignment)
1763 inform (DECL_SOURCE_LOCATION (decl),
1764 "a lambda closure type has a deleted copy assignment operator");
1765 else
1766 informed = false;
1768 else if (DECL_ARTIFICIAL (decl)
1769 && (sfk == sfk_copy_assignment
1770 || sfk == sfk_copy_constructor)
1771 && (type_has_user_declared_move_constructor (ctype)
1772 || type_has_user_declared_move_assign (ctype)))
1774 inform (DECL_SOURCE_LOCATION (decl),
1775 "%q#D is implicitly declared as deleted because %qT "
1776 "declares a move constructor or move assignment operator",
1777 decl, ctype);
1778 informed = true;
1780 else if (sfk == sfk_inheriting_constructor)
1782 tree binfo = inherited_ctor_binfo (decl);
1783 if (TREE_CODE (binfo) != TREE_BINFO)
1785 inform (DECL_SOURCE_LOCATION (decl),
1786 "%q#D inherits from multiple base subobjects",
1787 decl);
1788 informed = true;
1791 if (!informed)
1793 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1794 tree parm_type = TREE_VALUE (parms);
1795 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1796 tree raises = NULL_TREE;
1797 bool deleted_p = false;
1798 tree scope = push_scope (ctype);
1800 synthesized_method_walk (ctype, sfk, const_p,
1801 &raises, NULL, &deleted_p, NULL, false,
1802 DECL_INHERITED_CTOR (decl), parms);
1803 if (deleted_p)
1805 inform (DECL_SOURCE_LOCATION (decl),
1806 "%q#D is implicitly deleted because the default "
1807 "definition would be ill-formed:", decl);
1808 synthesized_method_walk (ctype, sfk, const_p,
1809 NULL, NULL, NULL, NULL, true,
1810 DECL_INHERITED_CTOR (decl), parms);
1812 else if (!comp_except_specs
1813 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
1814 raises, ce_normal))
1815 inform (DECL_SOURCE_LOCATION (decl), "%q#F is implicitly "
1816 "deleted because its exception-specification does not "
1817 "match the implicit exception-specification %qX",
1818 decl, raises);
1819 else if (flag_checking)
1820 gcc_unreachable ();
1822 pop_scope (scope);
1825 input_location = loc;
1826 return true;
1828 return false;
1831 /* DECL is a defaulted function which was declared constexpr. Explain why
1832 it can't be constexpr. */
1834 void
1835 explain_implicit_non_constexpr (tree decl)
1837 tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl));
1838 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1839 bool dummy;
1840 synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
1841 special_function_p (decl), const_p,
1842 NULL, NULL, NULL, &dummy, true,
1843 DECL_INHERITED_CTOR (decl),
1844 FUNCTION_FIRST_USER_PARMTYPE (decl));
1847 /* DECL is an instantiation of an inheriting constructor template. Deduce
1848 the correct exception-specification and deletedness for this particular
1849 specialization. */
1851 void
1852 deduce_inheriting_ctor (tree decl)
1854 gcc_assert (DECL_INHERITED_CTOR (decl));
1855 tree spec;
1856 bool trivial, constexpr_, deleted;
1857 synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor,
1858 false, &spec, &trivial, &deleted, &constexpr_,
1859 /*diag*/false,
1860 DECL_INHERITED_CTOR (decl),
1861 FUNCTION_FIRST_USER_PARMTYPE (decl));
1862 if (TREE_CODE (inherited_ctor_binfo (decl)) != TREE_BINFO)
1863 /* Inherited the same constructor from different base subobjects. */
1864 deleted = true;
1865 DECL_DELETED_FN (decl) = deleted;
1866 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
1869 /* Implicitly declare the special function indicated by KIND, as a
1870 member of TYPE. For copy constructors and assignment operators,
1871 CONST_P indicates whether these functions should take a const
1872 reference argument or a non-const reference. Returns the
1873 FUNCTION_DECL for the implicitly declared function. */
1875 tree
1876 implicitly_declare_fn (special_function_kind kind, tree type,
1877 bool const_p, tree inherited_ctor,
1878 tree inherited_parms)
1880 tree fn;
1881 tree parameter_types = void_list_node;
1882 tree return_type;
1883 tree fn_type;
1884 tree raises = empty_except_spec;
1885 tree rhs_parm_type = NULL_TREE;
1886 tree this_parm;
1887 tree name;
1888 HOST_WIDE_INT saved_processing_template_decl;
1889 bool deleted_p;
1890 bool constexpr_p;
1892 /* Because we create declarations for implicitly declared functions
1893 lazily, we may be creating the declaration for a member of TYPE
1894 while in some completely different context. However, TYPE will
1895 never be a dependent class (because we never want to do lookups
1896 for implicitly defined functions in a dependent class).
1897 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
1898 because we only create clones for constructors and destructors
1899 when not in a template. */
1900 gcc_assert (!dependent_type_p (type));
1901 saved_processing_template_decl = processing_template_decl;
1902 processing_template_decl = 0;
1904 type = TYPE_MAIN_VARIANT (type);
1906 if (targetm.cxx.cdtor_returns_this ())
1908 if (kind == sfk_destructor)
1909 /* See comment in check_special_function_return_type. */
1910 return_type = build_pointer_type (void_type_node);
1911 else
1912 return_type = build_pointer_type (type);
1914 else
1915 return_type = void_type_node;
1917 switch (kind)
1919 case sfk_destructor:
1920 /* Destructor. */
1921 name = constructor_name (type);
1922 break;
1924 case sfk_constructor:
1925 /* Default constructor. */
1926 name = constructor_name (type);
1927 break;
1929 case sfk_copy_constructor:
1930 case sfk_copy_assignment:
1931 case sfk_move_constructor:
1932 case sfk_move_assignment:
1933 case sfk_inheriting_constructor:
1935 bool move_p;
1936 if (kind == sfk_copy_assignment
1937 || kind == sfk_move_assignment)
1939 return_type = build_reference_type (type);
1940 name = ansi_assopname (NOP_EXPR);
1942 else
1943 name = constructor_name (type);
1945 if (kind == sfk_inheriting_constructor)
1946 parameter_types = inherited_parms;
1947 else
1949 if (const_p)
1950 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
1951 else
1952 rhs_parm_type = type;
1953 move_p = (kind == sfk_move_assignment
1954 || kind == sfk_move_constructor);
1955 rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
1957 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
1959 break;
1961 default:
1962 gcc_unreachable ();
1965 bool trivial_p = false;
1967 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
1969 /* For an inheriting constructor template, just copy these flags from
1970 the inherited constructor template for now. */
1971 raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor));
1972 deleted_p = DECL_DELETED_FN (inherited_ctor);
1973 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
1975 else if (cxx_dialect >= cxx11)
1977 raises = unevaluated_noexcept_spec ();
1978 synthesized_method_walk (type, kind, const_p, NULL, &trivial_p,
1979 &deleted_p, &constexpr_p, false,
1980 inherited_ctor, inherited_parms);
1982 else
1983 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
1984 &deleted_p, &constexpr_p, false,
1985 inherited_ctor, inherited_parms);
1986 /* Don't bother marking a deleted constructor as constexpr. */
1987 if (deleted_p)
1988 constexpr_p = false;
1989 /* A trivial copy/move constructor is also a constexpr constructor,
1990 unless the class has virtual bases (7.1.5p4). */
1991 else if (trivial_p && cxx_dialect >= cxx11
1992 && (kind == sfk_copy_constructor
1993 || kind == sfk_move_constructor)
1994 && !CLASSTYPE_VBASECLASSES (type))
1995 gcc_assert (constexpr_p);
1997 if (!trivial_p && type_has_trivial_fn (type, kind))
1998 type_set_nontrivial_flag (type, kind);
2000 /* Create the function. */
2001 fn_type = build_method_type_directly (type, return_type, parameter_types);
2002 if (raises)
2003 fn_type = build_exception_variant (fn_type, raises);
2004 fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
2005 if (kind != sfk_inheriting_constructor)
2006 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
2007 if (kind == sfk_constructor || kind == sfk_copy_constructor
2008 || kind == sfk_move_constructor || kind == sfk_inheriting_constructor)
2009 DECL_CONSTRUCTOR_P (fn) = 1;
2010 else if (kind == sfk_destructor)
2011 DECL_DESTRUCTOR_P (fn) = 1;
2012 else
2014 DECL_ASSIGNMENT_OPERATOR_P (fn) = 1;
2015 SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR);
2018 SET_DECL_ALIGN (fn, MINIMUM_METHOD_BOUNDARY);
2020 /* Create the explicit arguments. */
2021 if (rhs_parm_type)
2023 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
2024 want its type to be included in the mangled function
2025 name. */
2026 tree decl = cp_build_parm_decl (NULL_TREE, rhs_parm_type);
2027 TREE_READONLY (decl) = 1;
2028 retrofit_lang_decl (decl);
2029 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
2030 DECL_ARGUMENTS (fn) = decl;
2032 else if (kind == sfk_inheriting_constructor)
2034 tree *p = &DECL_ARGUMENTS (fn);
2035 int index = 1;
2036 for (tree parm = inherited_parms; parm && parm != void_list_node;
2037 parm = TREE_CHAIN (parm))
2039 *p = cp_build_parm_decl (NULL_TREE, TREE_VALUE (parm));
2040 retrofit_lang_decl (*p);
2041 DECL_PARM_LEVEL (*p) = 1;
2042 DECL_PARM_INDEX (*p) = index++;
2043 DECL_CONTEXT (*p) = fn;
2044 p = &DECL_CHAIN (*p);
2046 SET_DECL_INHERITED_CTOR (fn, inherited_ctor);
2047 DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor);
2048 /* A constructor so declared has the same access as the corresponding
2049 constructor in X. */
2050 TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor);
2051 TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor);
2052 /* Copy constexpr from the inherited constructor even if the
2053 inheriting constructor doesn't satisfy the requirements. */
2054 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
2056 /* Add the "this" parameter. */
2057 this_parm = build_this_parm (fn_type, TYPE_UNQUALIFIED);
2058 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
2059 DECL_ARGUMENTS (fn) = this_parm;
2061 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
2062 DECL_IN_AGGR_P (fn) = 1;
2063 DECL_ARTIFICIAL (fn) = 1;
2064 DECL_DEFAULTED_FN (fn) = 1;
2065 if (cxx_dialect >= cxx11)
2067 DECL_DELETED_FN (fn) = deleted_p;
2068 DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
2070 DECL_EXTERNAL (fn) = true;
2071 DECL_NOT_REALLY_EXTERN (fn) = 1;
2072 DECL_DECLARED_INLINE_P (fn) = 1;
2073 set_linkage_according_to_type (type, fn);
2074 if (TREE_PUBLIC (fn))
2075 DECL_COMDAT (fn) = 1;
2076 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
2077 gcc_assert (!TREE_USED (fn));
2079 /* Propagate constraints from the inherited constructor. */
2080 if (flag_concepts && inherited_ctor)
2081 if (tree orig_ci = get_constraints (inherited_ctor))
2083 tree new_ci = copy_node (orig_ci);
2084 set_constraints (fn, new_ci);
2087 /* Restore PROCESSING_TEMPLATE_DECL. */
2088 processing_template_decl = saved_processing_template_decl;
2090 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
2091 fn = add_inherited_template_parms (fn, inherited_ctor);
2093 /* Warn about calling a non-trivial move assignment in a virtual base. */
2094 if (kind == sfk_move_assignment && !deleted_p && !trivial_p
2095 && CLASSTYPE_VBASECLASSES (type))
2097 location_t loc = input_location;
2098 input_location = DECL_SOURCE_LOCATION (fn);
2099 synthesized_method_walk (type, kind, const_p,
2100 NULL, NULL, NULL, NULL, true,
2101 NULL_TREE, NULL_TREE);
2102 input_location = loc;
2105 return fn;
2108 /* Gives any errors about defaulted functions which need to be deferred
2109 until the containing class is complete. */
2111 void
2112 defaulted_late_check (tree fn)
2114 /* Complain about invalid signature for defaulted fn. */
2115 tree ctx = DECL_CONTEXT (fn);
2116 special_function_kind kind = special_function_p (fn);
2117 bool fn_const_p = (copy_fn_p (fn) == 2);
2118 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
2119 NULL, NULL);
2120 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
2122 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
2123 TREE_TYPE (TREE_TYPE (implicit_fn)))
2124 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2125 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn))))
2127 error ("defaulted declaration %q+D", fn);
2128 error_at (DECL_SOURCE_LOCATION (fn),
2129 "does not match expected signature %qD", implicit_fn);
2132 /* 8.4.2/2: An explicitly-defaulted function (...) may have an explicit
2133 exception-specification only if it is compatible (15.4) with the
2134 exception-specification on the implicit declaration. If a function
2135 is explicitly defaulted on its first declaration, (...) it is
2136 implicitly considered to have the same exception-specification as if
2137 it had been implicitly declared. */
2138 maybe_instantiate_noexcept (fn);
2139 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2140 if (!fn_spec)
2142 if (DECL_DEFAULTED_IN_CLASS_P (fn))
2143 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
2145 else if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
2146 /* Equivalent to the implicit spec. */;
2147 else if (DECL_DEFAULTED_IN_CLASS_P (fn)
2148 && !CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
2149 /* We can't compare an explicit exception-specification on a
2150 constructor defaulted in the class body to the implicit
2151 exception-specification until after we've parsed any NSDMI; see
2152 after_nsdmi_defaulted_late_checks. */;
2153 else
2155 tree eh_spec = get_defaulted_eh_spec (fn);
2156 if (!comp_except_specs (fn_spec, eh_spec, ce_normal))
2158 if (DECL_DEFAULTED_IN_CLASS_P (fn))
2159 DECL_DELETED_FN (fn) = true;
2160 else
2161 error ("function %q+D defaulted on its redeclaration "
2162 "with an exception-specification that differs from "
2163 "the implicit exception-specification %qX", fn, eh_spec);
2167 if (DECL_DEFAULTED_IN_CLASS_P (fn)
2168 && DECL_DECLARED_CONSTEXPR_P (implicit_fn))
2170 /* Hmm...should we do this for out-of-class too? Should it be OK to
2171 add constexpr later like inline, rather than requiring
2172 declarations to match? */
2173 DECL_DECLARED_CONSTEXPR_P (fn) = true;
2174 if (kind == sfk_constructor)
2175 TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
2178 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
2179 && DECL_DECLARED_CONSTEXPR_P (fn))
2181 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
2183 error ("explicitly defaulted function %q+D cannot be declared "
2184 "as constexpr because the implicit declaration is not "
2185 "constexpr:", fn);
2186 explain_implicit_non_constexpr (fn);
2188 DECL_DECLARED_CONSTEXPR_P (fn) = false;
2191 if (DECL_DELETED_FN (implicit_fn))
2192 DECL_DELETED_FN (fn) = 1;
2195 /* OK, we've parsed the NSDMI for class T, now we can check any explicit
2196 exception-specifications on functions defaulted in the class body. */
2198 void
2199 after_nsdmi_defaulted_late_checks (tree t)
2201 if (uses_template_parms (t))
2202 return;
2203 if (t == error_mark_node)
2204 return;
2205 for (tree fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
2206 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
2208 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2209 if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
2210 continue;
2212 tree eh_spec = get_defaulted_eh_spec (fn);
2213 if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)),
2214 eh_spec, ce_normal))
2215 DECL_DELETED_FN (fn) = true;
2219 /* Returns true iff FN can be explicitly defaulted, and gives any
2220 errors if defaulting FN is ill-formed. */
2222 bool
2223 defaultable_fn_check (tree fn)
2225 special_function_kind kind = sfk_none;
2227 if (template_parm_scope_p ())
2229 error ("a template cannot be defaulted");
2230 return false;
2233 if (DECL_CONSTRUCTOR_P (fn))
2235 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
2236 kind = sfk_constructor;
2237 else if (copy_fn_p (fn) > 0
2238 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
2239 == void_list_node))
2240 kind = sfk_copy_constructor;
2241 else if (move_fn_p (fn))
2242 kind = sfk_move_constructor;
2244 else if (DECL_DESTRUCTOR_P (fn))
2245 kind = sfk_destructor;
2246 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2247 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
2249 if (copy_fn_p (fn))
2250 kind = sfk_copy_assignment;
2251 else if (move_fn_p (fn))
2252 kind = sfk_move_assignment;
2255 if (kind == sfk_none)
2257 error ("%qD cannot be defaulted", fn);
2258 return false;
2260 else
2262 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
2263 t && t != void_list_node; t = TREE_CHAIN (t))
2264 if (TREE_PURPOSE (t))
2266 error ("defaulted function %q+D with default argument", fn);
2267 break;
2270 /* Avoid do_warn_unused_parameter warnings. */
2271 for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p))
2272 if (DECL_NAME (p))
2273 TREE_NO_WARNING (p) = 1;
2275 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn)))
2276 /* Defer checking. */;
2277 else if (!processing_template_decl)
2278 defaulted_late_check (fn);
2280 return true;
2284 /* Add an implicit declaration to TYPE for the kind of function
2285 indicated by SFK. Return the FUNCTION_DECL for the new implicit
2286 declaration. */
2288 tree
2289 lazily_declare_fn (special_function_kind sfk, tree type)
2291 tree fn;
2292 /* Whether or not the argument has a const reference type. */
2293 bool const_p = false;
2295 type = TYPE_MAIN_VARIANT (type);
2297 switch (sfk)
2299 case sfk_constructor:
2300 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
2301 break;
2302 case sfk_copy_constructor:
2303 const_p = TYPE_HAS_CONST_COPY_CTOR (type);
2304 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
2305 break;
2306 case sfk_move_constructor:
2307 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
2308 break;
2309 case sfk_copy_assignment:
2310 const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
2311 CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
2312 break;
2313 case sfk_move_assignment:
2314 CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
2315 break;
2316 case sfk_destructor:
2317 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
2318 break;
2319 default:
2320 gcc_unreachable ();
2323 /* Declare the function. */
2324 fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL);
2326 /* [class.copy]/8 If the class definition declares a move constructor or
2327 move assignment operator, the implicitly declared copy constructor is
2328 defined as deleted.... */
2329 if ((sfk == sfk_copy_assignment
2330 || sfk == sfk_copy_constructor)
2331 && (type_has_user_declared_move_constructor (type)
2332 || type_has_user_declared_move_assign (type)))
2333 DECL_DELETED_FN (fn) = true;
2335 /* A destructor may be virtual. */
2336 if (sfk == sfk_destructor
2337 || sfk == sfk_move_assignment
2338 || sfk == sfk_copy_assignment)
2339 check_for_override (fn, type);
2340 /* Add it to CLASSTYPE_METHOD_VEC. */
2341 add_method (type, fn, NULL_TREE);
2342 /* Add it to TYPE_METHODS. */
2343 if (sfk == sfk_destructor
2344 && DECL_VIRTUAL_P (fn))
2345 /* The ABI requires that a virtual destructor go at the end of the
2346 vtable. */
2347 TYPE_METHODS (type) = chainon (TYPE_METHODS (type), fn);
2348 else
2350 DECL_CHAIN (fn) = TYPE_METHODS (type);
2351 TYPE_METHODS (type) = fn;
2353 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
2354 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
2355 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
2356 /* Create appropriate clones. */
2357 clone_function_decl (fn, /*update_method_vec=*/true);
2359 return fn;
2362 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
2363 as there are artificial parms in FN. */
2365 tree
2366 skip_artificial_parms_for (const_tree fn, tree list)
2368 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2369 list = TREE_CHAIN (list);
2370 else
2371 return list;
2373 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2374 list = TREE_CHAIN (list);
2375 if (DECL_HAS_VTT_PARM_P (fn))
2376 list = TREE_CHAIN (list);
2377 return list;
2380 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
2381 artificial parms in FN. */
2384 num_artificial_parms_for (const_tree fn)
2386 int count = 0;
2388 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2389 count++;
2390 else
2391 return 0;
2393 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2394 count++;
2395 if (DECL_HAS_VTT_PARM_P (fn))
2396 count++;
2397 return count;
2401 #include "gt-cp-method.h"