Daily bump.
[official-gcc.git] / gcc / cp / method.c
blob38f2a5427cc78d783e1aad1d81962918982ac511
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);
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 exp = build_static_cast (type, exp, tf_warning_or_error);
488 if (DECL_PACK_P (parm))
489 exp = make_pack_expansion (exp);
490 return exp;
493 /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO
494 given the parameter or parameters PARM, possibly inherited constructor
495 base INH, or move flag MOVE_P. */
497 static tree
498 add_one_base_init (tree binfo, tree parm, bool move_p, tree inh,
499 tree member_init_list)
501 tree init;
502 if (inh)
504 /* An inheriting constructor only has a mem-initializer for
505 the base it inherits from. */
506 if (BINFO_TYPE (binfo) != inh)
507 return member_init_list;
509 tree *p = &init;
510 init = NULL_TREE;
511 for (; parm; parm = DECL_CHAIN (parm))
513 tree exp = forward_parm (parm);
514 *p = build_tree_list (NULL_TREE, exp);
515 p = &TREE_CHAIN (*p);
518 else
520 init = build_base_path (PLUS_EXPR, parm, binfo, 1,
521 tf_warning_or_error);
522 if (move_p)
523 init = move (init);
524 init = build_tree_list (NULL_TREE, init);
526 return tree_cons (binfo, init, member_init_list);
529 /* Generate code for default X(X&) or X(X&&) constructor or an inheriting
530 constructor. */
532 static void
533 do_build_copy_constructor (tree fndecl)
535 tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
536 bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
537 bool trivial = trivial_fn_p (fndecl);
538 tree inh = DECL_INHERITED_CTOR_BASE (fndecl);
540 if (!inh)
541 parm = convert_from_reference (parm);
543 if (trivial
544 && is_empty_class (current_class_type))
545 /* Don't copy the padding byte; it might not have been allocated
546 if *this is a base subobject. */;
547 else if (trivial)
549 tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm);
550 finish_expr_stmt (t);
552 else
554 tree fields = TYPE_FIELDS (current_class_type);
555 tree member_init_list = NULL_TREE;
556 int cvquals = cp_type_quals (TREE_TYPE (parm));
557 int i;
558 tree binfo, base_binfo;
559 tree init;
560 vec<tree, va_gc> *vbases;
562 /* Initialize all the base-classes with the parameter converted
563 to their type so that we get their copy constructor and not
564 another constructor that takes current_class_type. We must
565 deal with the binfo's directly as a direct base might be
566 inaccessible due to ambiguity. */
567 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
568 vec_safe_iterate (vbases, i, &binfo); i++)
570 member_init_list = add_one_base_init (binfo, parm, move_p, inh,
571 member_init_list);
574 for (binfo = TYPE_BINFO (current_class_type), i = 0;
575 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
577 if (BINFO_VIRTUAL_P (base_binfo))
578 continue;
579 member_init_list = add_one_base_init (base_binfo, parm, move_p,
580 inh, member_init_list);
583 for (; fields; fields = DECL_CHAIN (fields))
585 tree field = fields;
586 tree expr_type;
588 if (TREE_CODE (field) != FIELD_DECL)
589 continue;
590 if (inh)
591 continue;
593 expr_type = TREE_TYPE (field);
594 if (DECL_NAME (field))
596 if (VFIELD_NAME_P (DECL_NAME (field)))
597 continue;
599 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
600 /* Just use the field; anonymous types can't have
601 nontrivial copy ctors or assignment ops or this
602 function would be deleted. */;
603 else
604 continue;
606 /* Compute the type of "init->field". If the copy-constructor
607 parameter is, for example, "const S&", and the type of
608 the field is "T", then the type will usually be "const
609 T". (There are no cv-qualified variants of reference
610 types.) */
611 if (TREE_CODE (expr_type) != REFERENCE_TYPE)
613 int quals = cvquals;
615 if (DECL_MUTABLE_P (field))
616 quals &= ~TYPE_QUAL_CONST;
617 quals |= cp_type_quals (expr_type);
618 expr_type = cp_build_qualified_type (expr_type, quals);
621 init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
622 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
623 /* 'move' breaks bit-fields, and has no effect for scalars. */
624 && !scalarish_type_p (expr_type))
625 init = move (init);
626 init = build_tree_list (NULL_TREE, init);
628 member_init_list = tree_cons (field, init, member_init_list);
630 finish_mem_initializers (member_init_list);
634 static void
635 do_build_copy_assign (tree fndecl)
637 tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl));
638 tree compound_stmt;
639 bool move_p = move_fn_p (fndecl);
640 bool trivial = trivial_fn_p (fndecl);
641 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
643 compound_stmt = begin_compound_stmt (0);
644 parm = convert_from_reference (parm);
646 if (trivial
647 && is_empty_class (current_class_type))
648 /* Don't copy the padding byte; it might not have been allocated
649 if *this is a base subobject. */;
650 else if (trivial)
652 tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
653 finish_expr_stmt (t);
655 else
657 tree fields;
658 int cvquals = cp_type_quals (TREE_TYPE (parm));
659 int i;
660 tree binfo, base_binfo;
662 /* Assign to each of the direct base classes. */
663 for (binfo = TYPE_BINFO (current_class_type), i = 0;
664 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
666 tree converted_parm;
667 vec<tree, va_gc> *parmvec;
669 /* We must convert PARM directly to the base class
670 explicitly since the base class may be ambiguous. */
671 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
672 tf_warning_or_error);
673 if (move_p)
674 converted_parm = move (converted_parm);
675 /* Call the base class assignment operator. */
676 parmvec = make_tree_vector_single (converted_parm);
677 finish_expr_stmt
678 (build_special_member_call (current_class_ref,
679 ansi_assopname (NOP_EXPR),
680 &parmvec,
681 base_binfo,
682 flags,
683 tf_warning_or_error));
684 release_tree_vector (parmvec);
687 /* Assign to each of the non-static data members. */
688 for (fields = TYPE_FIELDS (current_class_type);
689 fields;
690 fields = DECL_CHAIN (fields))
692 tree comp = current_class_ref;
693 tree init = parm;
694 tree field = fields;
695 tree expr_type;
696 int quals;
698 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
699 continue;
701 expr_type = TREE_TYPE (field);
703 if (CP_TYPE_CONST_P (expr_type))
705 error ("non-static const member %q#D, can%'t use default "
706 "assignment operator", field);
707 continue;
709 else if (TREE_CODE (expr_type) == REFERENCE_TYPE)
711 error ("non-static reference member %q#D, can%'t use "
712 "default assignment operator", field);
713 continue;
716 if (DECL_NAME (field))
718 if (VFIELD_NAME_P (DECL_NAME (field)))
719 continue;
721 else if (ANON_AGGR_TYPE_P (expr_type)
722 && TYPE_FIELDS (expr_type) != NULL_TREE)
723 /* Just use the field; anonymous types can't have
724 nontrivial copy ctors or assignment ops or this
725 function would be deleted. */;
726 else
727 continue;
729 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
731 /* Compute the type of init->field */
732 quals = cvquals;
733 if (DECL_MUTABLE_P (field))
734 quals &= ~TYPE_QUAL_CONST;
735 expr_type = cp_build_qualified_type (expr_type, quals);
737 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
738 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
739 /* 'move' breaks bit-fields, and has no effect for scalars. */
740 && !scalarish_type_p (expr_type))
741 init = move (init);
743 if (DECL_NAME (field))
744 init = cp_build_modify_expr (comp, NOP_EXPR, init,
745 tf_warning_or_error);
746 else
747 init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
748 finish_expr_stmt (init);
751 finish_return_stmt (current_class_ref);
752 finish_compound_stmt (compound_stmt);
755 /* Synthesize FNDECL, a non-static member function. */
757 void
758 synthesize_method (tree fndecl)
760 bool nested = (current_function_decl != NULL_TREE);
761 tree context = decl_function_context (fndecl);
762 bool need_body = true;
763 tree stmt;
764 location_t save_input_location = input_location;
765 int error_count = errorcount;
766 int warning_count = warningcount + werrorcount;
768 /* Reset the source location, we might have been previously
769 deferred, and thus have saved where we were first needed. */
770 DECL_SOURCE_LOCATION (fndecl)
771 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
773 /* If we've been asked to synthesize a clone, just synthesize the
774 cloned function instead. Doing so will automatically fill in the
775 body for the clone. */
776 if (DECL_CLONED_FUNCTION_P (fndecl))
777 fndecl = DECL_CLONED_FUNCTION (fndecl);
779 /* We may be in the middle of deferred access check. Disable
780 it now. */
781 push_deferring_access_checks (dk_no_deferred);
783 if (! context)
784 push_to_top_level ();
785 else if (nested)
786 push_function_context ();
788 input_location = DECL_SOURCE_LOCATION (fndecl);
790 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
791 stmt = begin_function_body ();
793 if (DECL_OVERLOADED_OPERATOR_P (fndecl) == NOP_EXPR)
795 do_build_copy_assign (fndecl);
796 need_body = false;
798 else if (DECL_CONSTRUCTOR_P (fndecl))
800 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
801 if (arg_chain != void_list_node)
802 do_build_copy_constructor (fndecl);
803 else
804 finish_mem_initializers (NULL_TREE);
807 /* If we haven't yet generated the body of the function, just
808 generate an empty compound statement. */
809 if (need_body)
811 tree compound_stmt;
812 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
813 finish_compound_stmt (compound_stmt);
816 finish_function_body (stmt);
817 expand_or_defer_fn (finish_function (0));
819 input_location = save_input_location;
821 if (! context)
822 pop_from_top_level ();
823 else if (nested)
824 pop_function_context ();
826 pop_deferring_access_checks ();
828 if (error_count != errorcount || warning_count != warningcount + werrorcount)
829 inform (input_location, "synthesized method %qD first required here ",
830 fndecl);
833 /* Build a reference to type TYPE with cv-quals QUALS, which is an
834 rvalue if RVALUE is true. */
836 static tree
837 build_stub_type (tree type, int quals, bool rvalue)
839 tree argtype = cp_build_qualified_type (type, quals);
840 return cp_build_reference_type (argtype, rvalue);
843 /* Build a dummy glvalue from dereferencing a dummy reference of type
844 REFTYPE. */
846 static tree
847 build_stub_object (tree reftype)
849 if (TREE_CODE (reftype) != REFERENCE_TYPE)
850 reftype = cp_build_reference_type (reftype, /*rval*/true);
851 tree stub = build1 (CONVERT_EXPR, reftype, integer_one_node);
852 return convert_from_reference (stub);
855 /* Determine which function will be called when looking up NAME in TYPE,
856 called with a single ARGTYPE argument, or no argument if ARGTYPE is
857 null. FLAGS and COMPLAIN are as for build_new_method_call.
859 Returns a FUNCTION_DECL if all is well.
860 Returns NULL_TREE if overload resolution failed.
861 Returns error_mark_node if the chosen function cannot be called. */
863 static tree
864 locate_fn_flags (tree type, tree name, tree argtype, int flags,
865 tsubst_flags_t complain)
867 tree ob, fn, fns, binfo, rval;
868 vec<tree, va_gc> *args;
870 if (TYPE_P (type))
871 binfo = TYPE_BINFO (type);
872 else
874 binfo = type;
875 type = BINFO_TYPE (binfo);
878 ob = build_stub_object (cp_build_reference_type (type, false));
879 args = make_tree_vector ();
880 if (argtype)
882 if (TREE_CODE (argtype) == TREE_LIST)
884 for (tree elt = argtype; elt != void_list_node;
885 elt = TREE_CHAIN (elt))
887 tree type = TREE_VALUE (elt);
888 tree arg = build_stub_object (type);
889 vec_safe_push (args, arg);
892 else
894 tree arg = build_stub_object (argtype);
895 args->quick_push (arg);
899 fns = lookup_fnfields (binfo, name, 0);
900 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
902 release_tree_vector (args);
903 if (fn && rval == error_mark_node)
904 return rval;
905 else
906 return fn;
909 /* Locate the dtor of TYPE. */
911 tree
912 get_dtor (tree type, tsubst_flags_t complain)
914 tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
915 LOOKUP_NORMAL, complain);
916 if (fn == error_mark_node)
917 return NULL_TREE;
918 return fn;
921 /* Locate the default ctor of TYPE. */
923 tree
924 locate_ctor (tree type)
926 tree fn;
928 push_deferring_access_checks (dk_no_check);
929 fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
930 LOOKUP_SPECULATIVE, tf_none);
931 pop_deferring_access_checks ();
932 if (fn == error_mark_node)
933 return NULL_TREE;
934 return fn;
937 /* Likewise, but give any appropriate errors. */
939 tree
940 get_default_ctor (tree type)
942 tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
943 LOOKUP_NORMAL, tf_warning_or_error);
944 if (fn == error_mark_node)
945 return NULL_TREE;
946 return fn;
949 /* Locate the copy ctor of TYPE. */
951 tree
952 get_copy_ctor (tree type, tsubst_flags_t complain)
954 int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
955 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
956 tree argtype = build_stub_type (type, quals, false);
957 tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
958 LOOKUP_NORMAL, complain);
959 if (fn == error_mark_node)
960 return NULL_TREE;
961 return fn;
964 /* Locate the copy assignment operator of TYPE. */
966 tree
967 get_copy_assign (tree type)
969 int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
970 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
971 tree argtype = build_stub_type (type, quals, false);
972 tree fn = locate_fn_flags (type, ansi_assopname (NOP_EXPR), argtype,
973 LOOKUP_NORMAL, tf_warning_or_error);
974 if (fn == error_mark_node)
975 return NULL_TREE;
976 return fn;
979 /* Locate the inherited constructor of constructor CTOR. */
981 tree
982 get_inherited_ctor (tree ctor)
984 gcc_assert (DECL_INHERITED_CTOR_BASE (ctor));
986 push_deferring_access_checks (dk_no_check);
987 tree fn = locate_fn_flags (DECL_INHERITED_CTOR_BASE (ctor),
988 complete_ctor_identifier,
989 FUNCTION_FIRST_USER_PARMTYPE (ctor),
990 LOOKUP_NORMAL|LOOKUP_SPECULATIVE,
991 tf_none);
992 pop_deferring_access_checks ();
993 if (fn == error_mark_node)
994 return NULL_TREE;
995 return fn;
998 /* walk_tree helper function for is_trivially_xible. If *TP is a call,
999 return it if it calls something other than a trivial special member
1000 function. */
1002 static tree
1003 check_nontriv (tree *tp, int *, void *)
1005 tree fn;
1006 if (TREE_CODE (*tp) == CALL_EXPR)
1007 fn = CALL_EXPR_FN (*tp);
1008 else if (TREE_CODE (*tp) == AGGR_INIT_EXPR)
1009 fn = AGGR_INIT_EXPR_FN (*tp);
1010 else
1011 return NULL_TREE;
1013 if (TREE_CODE (fn) == ADDR_EXPR)
1014 fn = TREE_OPERAND (fn, 0);
1016 if (TREE_CODE (fn) != FUNCTION_DECL
1017 || !trivial_fn_p (fn))
1018 return fn;
1019 return NULL_TREE;
1022 /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */
1024 static tree
1025 assignable_expr (tree to, tree from)
1027 ++cp_unevaluated_operand;
1028 to = build_stub_object (to);
1029 from = build_stub_object (from);
1030 tree r = cp_build_modify_expr (to, NOP_EXPR, from, tf_none);
1031 --cp_unevaluated_operand;
1032 return r;
1035 /* The predicate condition for a template specialization
1036 is_constructible<T, Args...> shall be satisfied if and only if the
1037 following variable definition would be well-formed for some invented
1038 variable t: T t(create<Args>()...);
1040 Return something equivalent in well-formedness and triviality. */
1042 static tree
1043 constructible_expr (tree to, tree from)
1045 tree expr;
1046 if (CLASS_TYPE_P (to))
1048 tree ctype = to;
1049 vec<tree, va_gc> *args = NULL;
1050 if (TREE_CODE (to) != REFERENCE_TYPE)
1051 to = cp_build_reference_type (to, /*rval*/false);
1052 tree ob = build_stub_object (to);
1053 for (; from; from = TREE_CHAIN (from))
1054 vec_safe_push (args, build_stub_object (TREE_VALUE (from)));
1055 expr = build_special_member_call (ob, complete_ctor_identifier, &args,
1056 ctype, LOOKUP_NORMAL, tf_none);
1057 if (expr == error_mark_node)
1058 return error_mark_node;
1059 /* The current state of the standard vis-a-vis LWG 2116 is that
1060 is_*constructible involves destruction as well. */
1061 if (type_build_dtor_call (ctype))
1063 tree dtor = build_special_member_call (ob, complete_dtor_identifier,
1064 NULL, ctype, LOOKUP_NORMAL,
1065 tf_none);
1066 if (dtor == error_mark_node)
1067 return error_mark_node;
1068 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
1069 expr = build2 (COMPOUND_EXPR, void_type_node, expr, dtor);
1072 else
1074 if (from == NULL_TREE)
1075 return build_value_init (to, tf_none);
1076 else if (TREE_CHAIN (from))
1077 return error_mark_node; // too many initializers
1078 from = build_stub_object (TREE_VALUE (from));
1079 expr = perform_direct_initialization_if_possible (to, from,
1080 /*cast*/false,
1081 tf_none);
1083 return expr;
1086 /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or
1087 constructible (otherwise) from FROM, which is a single type for
1088 assignment or a list of types for construction. */
1090 bool
1091 is_trivially_xible (enum tree_code code, tree to, tree from)
1093 tree expr;
1094 if (code == MODIFY_EXPR)
1095 expr = assignable_expr (to, from);
1096 else if (from && TREE_CHAIN (from))
1097 return false; // only 0- and 1-argument ctors can be trivial
1098 else
1099 expr = constructible_expr (to, from);
1101 if (expr == error_mark_node)
1102 return false;
1103 tree nt = cp_walk_tree_without_duplicates (&expr, check_nontriv, NULL);
1104 return !nt;
1107 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
1108 DELETED_P or give an error message MSG with argument ARG. */
1110 static void
1111 process_subob_fn (tree fn, tree *spec_p, bool *trivial_p,
1112 bool *deleted_p, bool *constexpr_p,
1113 bool diag, tree arg, bool dtor_from_ctor = false)
1115 if (!fn || fn == error_mark_node)
1116 goto bad;
1118 if (spec_p)
1120 maybe_instantiate_noexcept (fn);
1121 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
1122 *spec_p = merge_exception_specifiers (*spec_p, raises);
1125 if (!trivial_fn_p (fn) && !dtor_from_ctor)
1127 if (trivial_p)
1128 *trivial_p = false;
1129 if (TREE_CODE (arg) == FIELD_DECL
1130 && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
1132 if (deleted_p)
1133 *deleted_p = true;
1134 if (diag)
1135 error ("union member %q+D with non-trivial %qD", arg, fn);
1139 if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
1141 *constexpr_p = false;
1142 if (diag)
1144 inform (DECL_SOURCE_LOCATION (fn),
1145 "defaulted constructor calls non-constexpr %qD", fn);
1146 explain_invalid_constexpr_fn (fn);
1150 return;
1152 bad:
1153 if (deleted_p)
1154 *deleted_p = true;
1157 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
1158 aggregates. If DTOR_FROM_CTOR is true, we're walking subobject destructors
1159 called from a synthesized constructor, in which case we don't consider
1160 the triviality of the subobject destructor. */
1162 static void
1163 walk_field_subobs (tree fields, tree fnname, special_function_kind sfk,
1164 int quals, bool copy_arg_p, bool move_p,
1165 bool assign_p, tree *spec_p, bool *trivial_p,
1166 bool *deleted_p, bool *constexpr_p,
1167 bool diag, int flags, tsubst_flags_t complain,
1168 bool dtor_from_ctor)
1170 tree field;
1171 for (field = fields; field; field = DECL_CHAIN (field))
1173 tree mem_type, argtype, rval;
1175 if (TREE_CODE (field) != FIELD_DECL
1176 || DECL_ARTIFICIAL (field))
1177 continue;
1179 mem_type = strip_array_types (TREE_TYPE (field));
1180 if (assign_p)
1182 bool bad = true;
1183 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
1185 if (diag)
1186 error ("non-static const member %q#D, can%'t use default "
1187 "assignment operator", field);
1189 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1191 if (diag)
1192 error ("non-static reference member %q#D, can%'t use "
1193 "default assignment operator", field);
1195 else
1196 bad = false;
1198 if (bad && deleted_p)
1199 *deleted_p = true;
1201 else if (sfk == sfk_constructor)
1203 bool bad;
1205 if (DECL_INITIAL (field))
1207 if (diag && DECL_INITIAL (field) == error_mark_node)
1208 inform (DECL_SOURCE_LOCATION (field),
1209 "initializer for %q#D is invalid", field);
1210 if (trivial_p)
1211 *trivial_p = false;
1212 /* Core 1351: If the field has an NSDMI that could throw, the
1213 default constructor is noexcept(false). */
1214 if (spec_p)
1216 tree nsdmi = get_nsdmi (field, /*ctor*/false);
1217 if (!expr_noexcept_p (nsdmi, complain))
1218 *spec_p = noexcept_false_spec;
1220 /* Don't do the normal processing. */
1221 continue;
1224 bad = false;
1225 if (CP_TYPE_CONST_P (mem_type)
1226 && default_init_uninitialized_part (mem_type))
1228 if (diag)
1230 error ("uninitialized const member in %q#T",
1231 current_class_type);
1232 inform (DECL_SOURCE_LOCATION (field),
1233 "%q#D should be initialized", field);
1235 bad = true;
1237 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1239 if (diag)
1241 error ("uninitialized reference member in %q#T",
1242 current_class_type);
1243 inform (DECL_SOURCE_LOCATION (field),
1244 "%q#D should be initialized", field);
1246 bad = true;
1249 if (bad && deleted_p)
1250 *deleted_p = true;
1252 /* For an implicitly-defined default constructor to be constexpr,
1253 every member must have a user-provided default constructor or
1254 an explicit initializer. */
1255 if (constexpr_p && !CLASS_TYPE_P (mem_type)
1256 && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE)
1258 *constexpr_p = false;
1259 if (diag)
1260 inform (DECL_SOURCE_LOCATION (field),
1261 "defaulted default constructor does not "
1262 "initialize %q#D", field);
1265 else if (sfk == sfk_copy_constructor)
1267 /* 12.8p11b5 */
1268 if (TREE_CODE (mem_type) == REFERENCE_TYPE
1269 && TYPE_REF_IS_RVALUE (mem_type))
1271 if (diag)
1272 error ("copying non-static data member %q#D of rvalue "
1273 "reference type", field);
1274 if (deleted_p)
1275 *deleted_p = true;
1279 if (!CLASS_TYPE_P (mem_type))
1280 continue;
1282 if (ANON_AGGR_TYPE_P (mem_type))
1284 walk_field_subobs (TYPE_FIELDS (mem_type), fnname, sfk, quals,
1285 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1286 deleted_p, constexpr_p,
1287 diag, flags, complain, dtor_from_ctor);
1288 continue;
1291 if (copy_arg_p)
1293 int mem_quals = cp_type_quals (mem_type) | quals;
1294 if (DECL_MUTABLE_P (field))
1295 mem_quals &= ~TYPE_QUAL_CONST;
1296 argtype = build_stub_type (mem_type, mem_quals, move_p);
1298 else
1299 argtype = NULL_TREE;
1301 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
1303 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1304 constexpr_p, diag, field, dtor_from_ctor);
1308 /* The caller wants to generate an implicit declaration of SFK for CTYPE
1309 which is const if relevant and CONST_P is set. If spec_p, trivial_p and
1310 deleted_p are non-null, set their referent appropriately. If diag is
1311 true, we're either being called from maybe_explain_implicit_delete to
1312 give errors, or if constexpr_p is non-null, from
1313 explain_invalid_constexpr_fn. */
1315 static void
1316 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
1317 tree *spec_p, bool *trivial_p, bool *deleted_p,
1318 bool *constexpr_p, bool diag,
1319 tree inherited_base, tree inherited_parms)
1321 tree binfo, base_binfo, scope, fnname, rval, argtype;
1322 bool move_p, copy_arg_p, assign_p, expected_trivial, check_vdtor;
1323 vec<tree, va_gc> *vbases;
1324 int i, quals, flags;
1325 tsubst_flags_t complain;
1326 bool ctor_p;
1328 if (spec_p)
1329 *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec);
1331 if (deleted_p)
1333 /* "The closure type associated with a lambda-expression has a deleted
1334 default constructor and a deleted copy assignment operator."
1335 This is diagnosed in maybe_explain_implicit_delete. */
1336 if (LAMBDA_TYPE_P (ctype)
1337 && (sfk == sfk_constructor
1338 || sfk == sfk_copy_assignment))
1340 *deleted_p = true;
1341 return;
1344 *deleted_p = false;
1347 ctor_p = false;
1348 assign_p = false;
1349 check_vdtor = false;
1350 switch (sfk)
1352 case sfk_move_assignment:
1353 case sfk_copy_assignment:
1354 assign_p = true;
1355 fnname = ansi_assopname (NOP_EXPR);
1356 break;
1358 case sfk_destructor:
1359 check_vdtor = true;
1360 /* The synthesized method will call base dtors, but check complete
1361 here to avoid having to deal with VTT. */
1362 fnname = complete_dtor_identifier;
1363 break;
1365 case sfk_constructor:
1366 case sfk_move_constructor:
1367 case sfk_copy_constructor:
1368 case sfk_inheriting_constructor:
1369 ctor_p = true;
1370 fnname = complete_ctor_identifier;
1371 break;
1373 default:
1374 gcc_unreachable ();
1377 gcc_assert ((sfk == sfk_inheriting_constructor)
1378 == (inherited_base != NULL_TREE));
1380 /* If that user-written default constructor would satisfy the
1381 requirements of a constexpr constructor (7.1.5), the
1382 implicitly-defined default constructor is constexpr.
1384 The implicitly-defined copy/move assignment operator is constexpr if
1385 - X is a literal type, and
1386 - the assignment operator selected to copy/move each direct base class
1387 subobject is a constexpr function, and
1388 - for each non-static data member of X that is of class type (or array
1389 thereof), the assignment operator selected to copy/move that member is a
1390 constexpr function. */
1391 if (constexpr_p)
1392 *constexpr_p = ctor_p
1393 || (assign_p && cxx_dialect >= cxx14);
1395 move_p = false;
1396 switch (sfk)
1398 case sfk_constructor:
1399 case sfk_destructor:
1400 case sfk_inheriting_constructor:
1401 copy_arg_p = false;
1402 break;
1404 case sfk_move_constructor:
1405 case sfk_move_assignment:
1406 move_p = true;
1407 case sfk_copy_constructor:
1408 case sfk_copy_assignment:
1409 copy_arg_p = true;
1410 break;
1412 default:
1413 gcc_unreachable ();
1416 expected_trivial = type_has_trivial_fn (ctype, sfk);
1417 if (trivial_p)
1418 *trivial_p = expected_trivial;
1420 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
1421 class versions and other properties of the type. But a subobject
1422 class can be trivially copyable and yet have overload resolution
1423 choose a template constructor for initialization, depending on
1424 rvalueness and cv-quals. And furthermore, a member in a base might
1425 be trivial but deleted or otherwise not callable. So we can't exit
1426 early in C++0x. The same considerations apply in C++98/03, but
1427 there the definition of triviality does not consider overload
1428 resolution, so a constructor can be trivial even if it would otherwise
1429 call a non-trivial constructor. */
1430 if (expected_trivial
1431 && (!copy_arg_p || cxx_dialect < cxx11))
1433 if (constexpr_p && sfk == sfk_constructor)
1435 bool cx = trivial_default_constructor_is_constexpr (ctype);
1436 *constexpr_p = cx;
1437 if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
1438 /* A trivial constructor doesn't have any NSDMI. */
1439 inform (input_location, "defaulted default constructor does "
1440 "not initialize any non-static data member");
1442 if (!diag && cxx_dialect < cxx11)
1443 return;
1446 ++cp_unevaluated_operand;
1447 ++c_inhibit_evaluation_warnings;
1448 push_deferring_access_checks (dk_no_deferred);
1450 scope = push_scope (ctype);
1452 flags = LOOKUP_NORMAL|LOOKUP_SPECULATIVE;
1453 if (!inherited_base)
1454 flags |= LOOKUP_DEFAULTED;
1456 complain = diag ? tf_warning_or_error : tf_none;
1458 if (const_p)
1459 quals = TYPE_QUAL_CONST;
1460 else
1461 quals = TYPE_UNQUALIFIED;
1462 argtype = NULL_TREE;
1464 for (binfo = TYPE_BINFO (ctype), i = 0;
1465 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
1467 tree basetype = BINFO_TYPE (base_binfo);
1469 if (!assign_p && BINFO_VIRTUAL_P (base_binfo))
1470 /* We'll handle virtual bases below. */
1471 continue;
1473 if (copy_arg_p)
1474 argtype = build_stub_type (basetype, quals, move_p);
1475 else if (basetype == inherited_base)
1476 argtype = inherited_parms;
1477 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1478 if (inherited_base)
1479 argtype = NULL_TREE;
1481 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1482 constexpr_p, diag, basetype);
1483 if (ctor_p)
1485 /* In a constructor we also need to check the subobject
1486 destructors for cleanup of partially constructed objects. */
1487 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1488 NULL_TREE, flags, complain);
1489 /* Note that we don't pass down trivial_p; the subobject
1490 destructors don't affect triviality of the constructor. Nor
1491 do they affect constexpr-ness (a constant expression doesn't
1492 throw) or exception-specification (a throw from one of the
1493 dtors would be a double-fault). */
1494 process_subob_fn (rval, NULL, NULL,
1495 deleted_p, NULL, false,
1496 basetype, /*dtor_from_ctor*/true);
1499 if (check_vdtor && type_has_virtual_destructor (basetype))
1501 rval = locate_fn_flags (ctype, ansi_opname (DELETE_EXPR),
1502 ptr_type_node, flags, complain);
1503 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
1504 to have a null rval (no class-specific op delete). */
1505 if (rval && rval == error_mark_node && deleted_p)
1506 *deleted_p = true;
1507 check_vdtor = false;
1510 if (diag && assign_p && move_p
1511 && BINFO_VIRTUAL_P (base_binfo)
1512 && rval && TREE_CODE (rval) == FUNCTION_DECL
1513 && move_fn_p (rval) && !trivial_fn_p (rval)
1514 && vbase_has_user_provided_move_assign (basetype))
1515 warning (OPT_Wvirtual_move_assign,
1516 "defaulted move assignment for %qT calls a non-trivial "
1517 "move assignment operator for virtual base %qT",
1518 ctype, basetype);
1521 vbases = CLASSTYPE_VBASECLASSES (ctype);
1522 if (vec_safe_is_empty (vbases))
1523 /* No virtual bases to worry about. */;
1524 else if (!assign_p)
1526 if (constexpr_p)
1527 *constexpr_p = false;
1528 FOR_EACH_VEC_ELT (*vbases, i, base_binfo)
1530 tree basetype = BINFO_TYPE (base_binfo);
1531 if (copy_arg_p)
1532 argtype = build_stub_type (basetype, quals, move_p);
1533 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1535 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1536 constexpr_p, diag, basetype);
1537 if (ctor_p && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype))
1539 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1540 NULL_TREE, flags, complain);
1541 process_subob_fn (rval, NULL, NULL,
1542 deleted_p, NULL, false,
1543 basetype, /*dtor_from_ctor*/true);
1548 /* Now handle the non-static data members. */
1549 walk_field_subobs (TYPE_FIELDS (ctype), fnname, sfk, quals,
1550 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1551 deleted_p, constexpr_p,
1552 diag, flags, complain, /*dtor_from_ctor*/false);
1553 if (ctor_p)
1554 walk_field_subobs (TYPE_FIELDS (ctype), complete_dtor_identifier,
1555 sfk_destructor, TYPE_UNQUALIFIED, false,
1556 false, false, NULL, NULL,
1557 deleted_p, NULL,
1558 false, flags, complain, /*dtor_from_ctor*/true);
1560 pop_scope (scope);
1562 pop_deferring_access_checks ();
1563 --cp_unevaluated_operand;
1564 --c_inhibit_evaluation_warnings;
1567 /* DECL is a defaulted function whose exception specification is now
1568 needed. Return what it should be. */
1570 tree
1571 get_defaulted_eh_spec (tree decl)
1573 if (DECL_CLONED_FUNCTION_P (decl))
1574 decl = DECL_CLONED_FUNCTION (decl);
1575 special_function_kind sfk = special_function_p (decl);
1576 tree ctype = DECL_CONTEXT (decl);
1577 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1578 tree parm_type = TREE_VALUE (parms);
1579 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1580 tree spec = empty_except_spec;
1581 synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL,
1582 NULL, false, DECL_INHERITED_CTOR_BASE (decl),
1583 parms);
1584 return spec;
1587 /* DECL is a deleted function. If it's implicitly deleted, explain why and
1588 return true; else return false. */
1590 bool
1591 maybe_explain_implicit_delete (tree decl)
1593 /* If decl is a clone, get the primary variant. */
1594 decl = DECL_ORIGIN (decl);
1595 gcc_assert (DECL_DELETED_FN (decl));
1596 if (DECL_DEFAULTED_FN (decl))
1598 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
1599 static hash_set<tree> *explained;
1601 special_function_kind sfk;
1602 location_t loc;
1603 bool informed;
1604 tree ctype;
1606 if (!explained)
1607 explained = new hash_set<tree>;
1608 if (explained->add (decl))
1609 return true;
1611 sfk = special_function_p (decl);
1612 ctype = DECL_CONTEXT (decl);
1613 loc = input_location;
1614 input_location = DECL_SOURCE_LOCATION (decl);
1616 informed = false;
1617 if (LAMBDA_TYPE_P (ctype))
1619 informed = true;
1620 if (sfk == sfk_constructor)
1621 inform (DECL_SOURCE_LOCATION (decl),
1622 "a lambda closure type has a deleted default constructor");
1623 else if (sfk == sfk_copy_assignment)
1624 inform (DECL_SOURCE_LOCATION (decl),
1625 "a lambda closure type has a deleted copy assignment operator");
1626 else
1627 informed = false;
1629 else if (DECL_ARTIFICIAL (decl)
1630 && (sfk == sfk_copy_assignment
1631 || sfk == sfk_copy_constructor)
1632 && (type_has_user_declared_move_constructor (ctype)
1633 || type_has_user_declared_move_assign (ctype)))
1635 inform (DECL_SOURCE_LOCATION (decl),
1636 "%q#D is implicitly declared as deleted because %qT "
1637 "declares a move constructor or move assignment operator",
1638 decl, ctype);
1639 informed = true;
1641 if (!informed)
1643 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1644 tree parm_type = TREE_VALUE (parms);
1645 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1646 tree raises = NULL_TREE;
1647 bool deleted_p = false;
1648 tree scope = push_scope (ctype);
1650 synthesized_method_walk (ctype, sfk, const_p,
1651 &raises, NULL, &deleted_p, NULL, false,
1652 DECL_INHERITED_CTOR_BASE (decl), parms);
1653 if (deleted_p)
1655 inform (DECL_SOURCE_LOCATION (decl),
1656 "%q#D is implicitly deleted because the default "
1657 "definition would be ill-formed:", decl);
1658 synthesized_method_walk (ctype, sfk, const_p,
1659 NULL, NULL, NULL, NULL, true,
1660 DECL_INHERITED_CTOR_BASE (decl), parms);
1662 else if (!comp_except_specs
1663 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
1664 raises, ce_normal))
1665 inform (DECL_SOURCE_LOCATION (decl), "%q#F is implicitly "
1666 "deleted because its exception-specification does not "
1667 "match the implicit exception-specification %qX",
1668 decl, raises);
1669 else if (flag_checking)
1670 gcc_unreachable ();
1672 pop_scope (scope);
1675 input_location = loc;
1676 return true;
1678 return false;
1681 /* DECL is a defaulted function which was declared constexpr. Explain why
1682 it can't be constexpr. */
1684 void
1685 explain_implicit_non_constexpr (tree decl)
1687 tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl));
1688 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1689 bool dummy;
1690 synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
1691 special_function_p (decl), const_p,
1692 NULL, NULL, NULL, &dummy, true,
1693 DECL_INHERITED_CTOR_BASE (decl),
1694 FUNCTION_FIRST_USER_PARMTYPE (decl));
1697 /* DECL is an instantiation of an inheriting constructor template. Deduce
1698 the correct exception-specification and deletedness for this particular
1699 specialization. */
1701 void
1702 deduce_inheriting_ctor (tree decl)
1704 gcc_assert (DECL_INHERITED_CTOR_BASE (decl));
1705 tree spec;
1706 bool trivial, constexpr_, deleted;
1707 synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor,
1708 false, &spec, &trivial, &deleted, &constexpr_,
1709 /*diag*/false,
1710 DECL_INHERITED_CTOR_BASE (decl),
1711 FUNCTION_FIRST_USER_PARMTYPE (decl));
1712 DECL_DELETED_FN (decl) = deleted;
1713 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
1716 /* Implicitly declare the special function indicated by KIND, as a
1717 member of TYPE. For copy constructors and assignment operators,
1718 CONST_P indicates whether these functions should take a const
1719 reference argument or a non-const reference. Returns the
1720 FUNCTION_DECL for the implicitly declared function. */
1722 tree
1723 implicitly_declare_fn (special_function_kind kind, tree type,
1724 bool const_p, tree inherited_ctor,
1725 tree inherited_parms)
1727 tree fn;
1728 tree parameter_types = void_list_node;
1729 tree return_type;
1730 tree fn_type;
1731 tree raises = empty_except_spec;
1732 tree rhs_parm_type = NULL_TREE;
1733 tree this_parm;
1734 tree name;
1735 HOST_WIDE_INT saved_processing_template_decl;
1736 bool deleted_p;
1737 bool constexpr_p;
1739 /* Because we create declarations for implicitly declared functions
1740 lazily, we may be creating the declaration for a member of TYPE
1741 while in some completely different context. However, TYPE will
1742 never be a dependent class (because we never want to do lookups
1743 for implicitly defined functions in a dependent class).
1744 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
1745 because we only create clones for constructors and destructors
1746 when not in a template. */
1747 gcc_assert (!dependent_type_p (type));
1748 saved_processing_template_decl = processing_template_decl;
1749 processing_template_decl = 0;
1751 type = TYPE_MAIN_VARIANT (type);
1753 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (type))
1755 if (kind == sfk_destructor)
1756 /* See comment in check_special_function_return_type. */
1757 return_type = build_pointer_type (void_type_node);
1758 else
1759 return_type = build_pointer_type (type);
1761 else
1762 return_type = void_type_node;
1764 switch (kind)
1766 case sfk_destructor:
1767 /* Destructor. */
1768 name = constructor_name (type);
1769 break;
1771 case sfk_constructor:
1772 /* Default constructor. */
1773 name = constructor_name (type);
1774 break;
1776 case sfk_copy_constructor:
1777 case sfk_copy_assignment:
1778 case sfk_move_constructor:
1779 case sfk_move_assignment:
1780 case sfk_inheriting_constructor:
1782 bool move_p;
1783 if (kind == sfk_copy_assignment
1784 || kind == sfk_move_assignment)
1786 return_type = build_reference_type (type);
1787 name = ansi_assopname (NOP_EXPR);
1789 else
1790 name = constructor_name (type);
1792 if (kind == sfk_inheriting_constructor)
1793 parameter_types = inherited_parms;
1794 else
1796 if (const_p)
1797 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
1798 else
1799 rhs_parm_type = type;
1800 move_p = (kind == sfk_move_assignment
1801 || kind == sfk_move_constructor);
1802 rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
1804 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
1806 break;
1808 default:
1809 gcc_unreachable ();
1812 tree inherited_base = (inherited_ctor
1813 ? DECL_CONTEXT (inherited_ctor)
1814 : NULL_TREE);
1815 bool trivial_p = false;
1817 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
1819 /* For an inheriting constructor template, just copy these flags from
1820 the inherited constructor template for now. */
1821 raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor));
1822 deleted_p = DECL_DELETED_FN (inherited_ctor);
1823 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
1825 else if (cxx_dialect >= cxx11)
1827 raises = unevaluated_noexcept_spec ();
1828 synthesized_method_walk (type, kind, const_p, NULL, &trivial_p,
1829 &deleted_p, &constexpr_p, false,
1830 inherited_base, inherited_parms);
1832 else
1833 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
1834 &deleted_p, &constexpr_p, false,
1835 inherited_base, inherited_parms);
1836 /* Don't bother marking a deleted constructor as constexpr. */
1837 if (deleted_p)
1838 constexpr_p = false;
1839 /* A trivial copy/move constructor is also a constexpr constructor,
1840 unless the class has virtual bases (7.1.5p4). */
1841 else if (trivial_p && cxx_dialect >= cxx11
1842 && (kind == sfk_copy_constructor
1843 || kind == sfk_move_constructor)
1844 && !CLASSTYPE_VBASECLASSES (type))
1845 gcc_assert (constexpr_p);
1847 if (!trivial_p && type_has_trivial_fn (type, kind))
1848 type_set_nontrivial_flag (type, kind);
1850 /* Create the function. */
1851 fn_type = build_method_type_directly (type, return_type, parameter_types);
1852 if (raises)
1853 fn_type = build_exception_variant (fn_type, raises);
1854 fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
1855 if (kind != sfk_inheriting_constructor)
1856 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
1857 if (kind == sfk_constructor || kind == sfk_copy_constructor
1858 || kind == sfk_move_constructor || kind == sfk_inheriting_constructor)
1859 DECL_CONSTRUCTOR_P (fn) = 1;
1860 else if (kind == sfk_destructor)
1861 DECL_DESTRUCTOR_P (fn) = 1;
1862 else
1864 DECL_ASSIGNMENT_OPERATOR_P (fn) = 1;
1865 SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR);
1868 DECL_ALIGN (fn) = MINIMUM_METHOD_BOUNDARY;
1870 /* Create the explicit arguments. */
1871 if (rhs_parm_type)
1873 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
1874 want its type to be included in the mangled function
1875 name. */
1876 tree decl = cp_build_parm_decl (NULL_TREE, rhs_parm_type);
1877 TREE_READONLY (decl) = 1;
1878 retrofit_lang_decl (decl);
1879 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
1880 DECL_ARGUMENTS (fn) = decl;
1882 else if (kind == sfk_inheriting_constructor)
1884 tree *p = &DECL_ARGUMENTS (fn);
1885 int index = 1;
1886 for (tree parm = inherited_parms; parm != void_list_node;
1887 parm = TREE_CHAIN (parm))
1889 *p = cp_build_parm_decl (NULL_TREE, TREE_VALUE (parm));
1890 retrofit_lang_decl (*p);
1891 DECL_PARM_LEVEL (*p) = 1;
1892 DECL_PARM_INDEX (*p) = index++;
1893 DECL_CONTEXT (*p) = fn;
1894 p = &DECL_CHAIN (*p);
1896 SET_DECL_INHERITED_CTOR_BASE (fn, inherited_base);
1897 DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor);
1898 /* A constructor so declared has the same access as the corresponding
1899 constructor in X. */
1900 TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor);
1901 TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor);
1902 /* Copy constexpr from the inherited constructor even if the
1903 inheriting constructor doesn't satisfy the requirements. */
1904 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
1906 /* Add the "this" parameter. */
1907 this_parm = build_this_parm (fn_type, TYPE_UNQUALIFIED);
1908 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
1909 DECL_ARGUMENTS (fn) = this_parm;
1911 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
1912 DECL_IN_AGGR_P (fn) = 1;
1913 DECL_ARTIFICIAL (fn) = 1;
1914 DECL_DEFAULTED_FN (fn) = 1;
1915 if (cxx_dialect >= cxx11)
1917 /* "The closure type associated with a lambda-expression has a deleted
1918 default constructor and a deleted copy assignment operator." */
1919 if ((kind == sfk_constructor
1920 || kind == sfk_copy_assignment)
1921 && LAMBDA_TYPE_P (type))
1922 deleted_p = true;
1923 DECL_DELETED_FN (fn) = deleted_p;
1924 DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
1926 DECL_EXTERNAL (fn) = true;
1927 DECL_NOT_REALLY_EXTERN (fn) = 1;
1928 DECL_DECLARED_INLINE_P (fn) = 1;
1929 set_linkage_according_to_type (type, fn);
1930 if (TREE_PUBLIC (fn))
1931 DECL_COMDAT (fn) = 1;
1932 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
1933 gcc_assert (!TREE_USED (fn));
1935 /* Propagate constraints from the inherited constructor. */
1936 if (flag_concepts && inherited_ctor)
1937 if (tree orig_ci = get_constraints (inherited_ctor))
1939 tree new_ci = copy_node (orig_ci);
1940 set_constraints (fn, new_ci);
1943 /* Restore PROCESSING_TEMPLATE_DECL. */
1944 processing_template_decl = saved_processing_template_decl;
1946 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
1947 fn = add_inherited_template_parms (fn, inherited_ctor);
1949 /* Warn about calling a non-trivial move assignment in a virtual base. */
1950 if (kind == sfk_move_assignment && !deleted_p && !trivial_p
1951 && CLASSTYPE_VBASECLASSES (type))
1953 location_t loc = input_location;
1954 input_location = DECL_SOURCE_LOCATION (fn);
1955 synthesized_method_walk (type, kind, const_p,
1956 NULL, NULL, NULL, NULL, true,
1957 NULL_TREE, NULL_TREE);
1958 input_location = loc;
1961 return fn;
1964 /* Gives any errors about defaulted functions which need to be deferred
1965 until the containing class is complete. */
1967 void
1968 defaulted_late_check (tree fn)
1970 /* Complain about invalid signature for defaulted fn. */
1971 tree ctx = DECL_CONTEXT (fn);
1972 special_function_kind kind = special_function_p (fn);
1973 bool fn_const_p = (copy_fn_p (fn) == 2);
1974 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
1975 NULL, NULL);
1976 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
1978 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
1979 TREE_TYPE (TREE_TYPE (implicit_fn)))
1980 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1981 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn))))
1983 error ("defaulted declaration %q+D", fn);
1984 error_at (DECL_SOURCE_LOCATION (fn),
1985 "does not match expected signature %qD", implicit_fn);
1988 /* 8.4.2/2: An explicitly-defaulted function (...) may have an explicit
1989 exception-specification only if it is compatible (15.4) with the
1990 exception-specification on the implicit declaration. If a function
1991 is explicitly defaulted on its first declaration, (...) it is
1992 implicitly considered to have the same exception-specification as if
1993 it had been implicitly declared. */
1994 maybe_instantiate_noexcept (fn);
1995 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
1996 if (!fn_spec)
1998 if (DECL_DEFAULTED_IN_CLASS_P (fn))
1999 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
2001 else if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
2002 /* Equivalent to the implicit spec. */;
2003 else if (DECL_DEFAULTED_IN_CLASS_P (fn)
2004 && !CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
2005 /* We can't compare an explicit exception-specification on a
2006 constructor defaulted in the class body to the implicit
2007 exception-specification until after we've parsed any NSDMI; see
2008 after_nsdmi_defaulted_late_checks. */;
2009 else
2011 tree eh_spec = get_defaulted_eh_spec (fn);
2012 if (!comp_except_specs (fn_spec, eh_spec, ce_normal))
2014 if (DECL_DEFAULTED_IN_CLASS_P (fn))
2015 DECL_DELETED_FN (fn) = true;
2016 else
2017 error ("function %q+D defaulted on its redeclaration "
2018 "with an exception-specification that differs from "
2019 "the implicit exception-specification %qX", fn, eh_spec);
2023 if (DECL_DEFAULTED_IN_CLASS_P (fn)
2024 && DECL_DECLARED_CONSTEXPR_P (implicit_fn))
2026 /* Hmm...should we do this for out-of-class too? Should it be OK to
2027 add constexpr later like inline, rather than requiring
2028 declarations to match? */
2029 DECL_DECLARED_CONSTEXPR_P (fn) = true;
2030 if (kind == sfk_constructor)
2031 TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
2034 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
2035 && DECL_DECLARED_CONSTEXPR_P (fn))
2037 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
2039 error ("explicitly defaulted function %q+D cannot be declared "
2040 "as constexpr because the implicit declaration is not "
2041 "constexpr:", fn);
2042 explain_implicit_non_constexpr (fn);
2044 DECL_DECLARED_CONSTEXPR_P (fn) = false;
2047 if (DECL_DELETED_FN (implicit_fn))
2048 DECL_DELETED_FN (fn) = 1;
2051 /* OK, we've parsed the NSDMI for class T, now we can check any explicit
2052 exception-specifications on functions defaulted in the class body. */
2054 void
2055 after_nsdmi_defaulted_late_checks (tree t)
2057 if (uses_template_parms (t))
2058 return;
2059 if (t == error_mark_node)
2060 return;
2061 for (tree fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
2062 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
2064 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2065 if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
2066 continue;
2068 tree eh_spec = get_defaulted_eh_spec (fn);
2069 if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)),
2070 eh_spec, ce_normal))
2071 DECL_DELETED_FN (fn) = true;
2075 /* Returns true iff FN can be explicitly defaulted, and gives any
2076 errors if defaulting FN is ill-formed. */
2078 bool
2079 defaultable_fn_check (tree fn)
2081 special_function_kind kind = sfk_none;
2083 if (template_parm_scope_p ())
2085 error ("a template cannot be defaulted");
2086 return false;
2089 if (DECL_CONSTRUCTOR_P (fn))
2091 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
2092 kind = sfk_constructor;
2093 else if (copy_fn_p (fn) > 0
2094 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
2095 == void_list_node))
2096 kind = sfk_copy_constructor;
2097 else if (move_fn_p (fn))
2098 kind = sfk_move_constructor;
2100 else if (DECL_DESTRUCTOR_P (fn))
2101 kind = sfk_destructor;
2102 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2103 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
2105 if (copy_fn_p (fn))
2106 kind = sfk_copy_assignment;
2107 else if (move_fn_p (fn))
2108 kind = sfk_move_assignment;
2111 if (kind == sfk_none)
2113 error ("%qD cannot be defaulted", fn);
2114 return false;
2116 else
2118 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
2119 t && t != void_list_node; t = TREE_CHAIN (t))
2120 if (TREE_PURPOSE (t))
2122 error ("defaulted function %q+D with default argument", fn);
2123 break;
2126 /* Avoid do_warn_unused_parameter warnings. */
2127 for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p))
2128 if (DECL_NAME (p))
2129 TREE_NO_WARNING (p) = 1;
2131 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn)))
2132 /* Defer checking. */;
2133 else if (!processing_template_decl)
2134 defaulted_late_check (fn);
2136 return true;
2140 /* Add an implicit declaration to TYPE for the kind of function
2141 indicated by SFK. Return the FUNCTION_DECL for the new implicit
2142 declaration. */
2144 tree
2145 lazily_declare_fn (special_function_kind sfk, tree type)
2147 tree fn;
2148 /* Whether or not the argument has a const reference type. */
2149 bool const_p = false;
2151 type = TYPE_MAIN_VARIANT (type);
2153 switch (sfk)
2155 case sfk_constructor:
2156 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
2157 break;
2158 case sfk_copy_constructor:
2159 const_p = TYPE_HAS_CONST_COPY_CTOR (type);
2160 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
2161 break;
2162 case sfk_move_constructor:
2163 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
2164 break;
2165 case sfk_copy_assignment:
2166 const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
2167 CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
2168 break;
2169 case sfk_move_assignment:
2170 CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
2171 break;
2172 case sfk_destructor:
2173 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
2174 break;
2175 default:
2176 gcc_unreachable ();
2179 /* Declare the function. */
2180 fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL);
2182 /* [class.copy]/8 If the class definition declares a move constructor or
2183 move assignment operator, the implicitly declared copy constructor is
2184 defined as deleted.... */
2185 if ((sfk == sfk_copy_assignment
2186 || sfk == sfk_copy_constructor)
2187 && (type_has_user_declared_move_constructor (type)
2188 || type_has_user_declared_move_assign (type)))
2189 DECL_DELETED_FN (fn) = true;
2191 /* A destructor may be virtual. */
2192 if (sfk == sfk_destructor
2193 || sfk == sfk_move_assignment
2194 || sfk == sfk_copy_assignment)
2195 check_for_override (fn, type);
2196 /* Add it to CLASSTYPE_METHOD_VEC. */
2197 add_method (type, fn, NULL_TREE);
2198 /* Add it to TYPE_METHODS. */
2199 if (sfk == sfk_destructor
2200 && DECL_VIRTUAL_P (fn))
2201 /* The ABI requires that a virtual destructor go at the end of the
2202 vtable. */
2203 TYPE_METHODS (type) = chainon (TYPE_METHODS (type), fn);
2204 else
2206 DECL_CHAIN (fn) = TYPE_METHODS (type);
2207 TYPE_METHODS (type) = fn;
2209 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
2210 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
2211 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
2212 /* Create appropriate clones. */
2213 clone_function_decl (fn, /*update_method_vec=*/true);
2215 return fn;
2218 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
2219 as there are artificial parms in FN. */
2221 tree
2222 skip_artificial_parms_for (const_tree fn, tree list)
2224 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2225 list = TREE_CHAIN (list);
2226 else
2227 return list;
2229 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2230 list = TREE_CHAIN (list);
2231 if (DECL_HAS_VTT_PARM_P (fn))
2232 list = TREE_CHAIN (list);
2233 return list;
2236 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
2237 artificial parms in FN. */
2240 num_artificial_parms_for (const_tree fn)
2242 int count = 0;
2244 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2245 count++;
2246 else
2247 return 0;
2249 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2250 count++;
2251 if (DECL_HAS_VTT_PARM_P (fn))
2252 count++;
2253 return count;
2257 #include "gt-cp-method.h"