PR c/7652
[official-gcc.git] / gcc / cp / method.c
blob75342ae5d28f2ea3158227f85724e2a5898dad18
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 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 /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO
496 given the parameter or parameters PARM, possibly inherited constructor
497 base INH, or move flag MOVE_P. */
499 static tree
500 add_one_base_init (tree binfo, tree parm, bool move_p, tree inh,
501 tree member_init_list)
503 tree init;
504 if (inh)
506 /* An inheriting constructor only has a mem-initializer for
507 the base it inherits from. */
508 if (BINFO_TYPE (binfo) != inh)
509 return member_init_list;
511 tree *p = &init;
512 init = NULL_TREE;
513 for (; parm; parm = DECL_CHAIN (parm))
515 tree exp = forward_parm (parm);
516 *p = build_tree_list (NULL_TREE, exp);
517 p = &TREE_CHAIN (*p);
520 else
522 init = build_base_path (PLUS_EXPR, parm, binfo, 1,
523 tf_warning_or_error);
524 if (move_p)
525 init = move (init);
526 init = build_tree_list (NULL_TREE, init);
528 return tree_cons (binfo, init, member_init_list);
531 /* Generate code for default X(X&) or X(X&&) constructor or an inheriting
532 constructor. */
534 static void
535 do_build_copy_constructor (tree fndecl)
537 tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
538 bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
539 bool trivial = trivial_fn_p (fndecl);
540 tree inh = DECL_INHERITED_CTOR_BASE (fndecl);
542 if (!inh)
543 parm = convert_from_reference (parm);
545 if (trivial)
547 if (is_empty_class (current_class_type))
548 /* Don't copy the padding byte; it might not have been allocated
549 if *this is a base subobject. */;
550 else if (tree_int_cst_equal (TYPE_SIZE (current_class_type),
551 CLASSTYPE_SIZE (current_class_type)))
553 tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm);
554 finish_expr_stmt (t);
556 else
558 /* We must only copy the non-tail padding parts. */
559 tree base_size = CLASSTYPE_SIZE_UNIT (current_class_type);
560 base_size = size_binop (MINUS_EXPR, base_size, size_int (1));
561 tree array_type = build_array_type (unsigned_char_type_node,
562 build_index_type (base_size));
563 tree alias_set = build_int_cst (TREE_TYPE (current_class_ptr), 0);
564 tree lhs = build2 (MEM_REF, array_type,
565 current_class_ptr, alias_set);
566 tree rhs = build2 (MEM_REF, array_type,
567 TREE_OPERAND (parm, 0), alias_set);
568 tree t = build2 (INIT_EXPR, void_type_node, lhs, rhs);
569 finish_expr_stmt (t);
572 else
574 tree fields = TYPE_FIELDS (current_class_type);
575 tree member_init_list = NULL_TREE;
576 int cvquals = cp_type_quals (TREE_TYPE (parm));
577 int i;
578 tree binfo, base_binfo;
579 tree init;
580 vec<tree, va_gc> *vbases;
582 /* Initialize all the base-classes with the parameter converted
583 to their type so that we get their copy constructor and not
584 another constructor that takes current_class_type. We must
585 deal with the binfo's directly as a direct base might be
586 inaccessible due to ambiguity. */
587 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
588 vec_safe_iterate (vbases, i, &binfo); i++)
590 member_init_list = add_one_base_init (binfo, parm, move_p, inh,
591 member_init_list);
594 for (binfo = TYPE_BINFO (current_class_type), i = 0;
595 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
597 if (BINFO_VIRTUAL_P (base_binfo))
598 continue;
599 member_init_list = add_one_base_init (base_binfo, parm, move_p,
600 inh, member_init_list);
603 for (; fields; fields = DECL_CHAIN (fields))
605 tree field = fields;
606 tree expr_type;
608 if (TREE_CODE (field) != FIELD_DECL)
609 continue;
610 if (inh)
611 continue;
613 expr_type = TREE_TYPE (field);
614 if (DECL_NAME (field))
616 if (VFIELD_NAME_P (DECL_NAME (field)))
617 continue;
619 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
620 /* Just use the field; anonymous types can't have
621 nontrivial copy ctors or assignment ops or this
622 function would be deleted. */;
623 else
624 continue;
626 /* Compute the type of "init->field". If the copy-constructor
627 parameter is, for example, "const S&", and the type of
628 the field is "T", then the type will usually be "const
629 T". (There are no cv-qualified variants of reference
630 types.) */
631 if (TREE_CODE (expr_type) != REFERENCE_TYPE)
633 int quals = cvquals;
635 if (DECL_MUTABLE_P (field))
636 quals &= ~TYPE_QUAL_CONST;
637 quals |= cp_type_quals (expr_type);
638 expr_type = cp_build_qualified_type (expr_type, quals);
641 init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
642 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
643 /* 'move' breaks bit-fields, and has no effect for scalars. */
644 && !scalarish_type_p (expr_type))
645 init = move (init);
646 init = build_tree_list (NULL_TREE, init);
648 member_init_list = tree_cons (field, init, member_init_list);
650 finish_mem_initializers (member_init_list);
654 static void
655 do_build_copy_assign (tree fndecl)
657 tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl));
658 tree compound_stmt;
659 bool move_p = move_fn_p (fndecl);
660 bool trivial = trivial_fn_p (fndecl);
661 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
663 compound_stmt = begin_compound_stmt (0);
664 parm = convert_from_reference (parm);
666 if (trivial
667 && is_empty_class (current_class_type))
668 /* Don't copy the padding byte; it might not have been allocated
669 if *this is a base subobject. */;
670 else if (trivial)
672 tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
673 finish_expr_stmt (t);
675 else
677 tree fields;
678 int cvquals = cp_type_quals (TREE_TYPE (parm));
679 int i;
680 tree binfo, base_binfo;
682 /* Assign to each of the direct base classes. */
683 for (binfo = TYPE_BINFO (current_class_type), i = 0;
684 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
686 tree converted_parm;
687 vec<tree, va_gc> *parmvec;
689 /* We must convert PARM directly to the base class
690 explicitly since the base class may be ambiguous. */
691 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
692 tf_warning_or_error);
693 if (move_p)
694 converted_parm = move (converted_parm);
695 /* Call the base class assignment operator. */
696 parmvec = make_tree_vector_single (converted_parm);
697 finish_expr_stmt
698 (build_special_member_call (current_class_ref,
699 ansi_assopname (NOP_EXPR),
700 &parmvec,
701 base_binfo,
702 flags,
703 tf_warning_or_error));
704 release_tree_vector (parmvec);
707 /* Assign to each of the non-static data members. */
708 for (fields = TYPE_FIELDS (current_class_type);
709 fields;
710 fields = DECL_CHAIN (fields))
712 tree comp = current_class_ref;
713 tree init = parm;
714 tree field = fields;
715 tree expr_type;
716 int quals;
718 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
719 continue;
721 expr_type = TREE_TYPE (field);
723 if (CP_TYPE_CONST_P (expr_type))
725 error ("non-static const member %q#D, can%'t use default "
726 "assignment operator", field);
727 continue;
729 else if (TREE_CODE (expr_type) == REFERENCE_TYPE)
731 error ("non-static reference member %q#D, can%'t use "
732 "default assignment operator", field);
733 continue;
736 if (DECL_NAME (field))
738 if (VFIELD_NAME_P (DECL_NAME (field)))
739 continue;
741 else if (ANON_AGGR_TYPE_P (expr_type)
742 && TYPE_FIELDS (expr_type) != NULL_TREE)
743 /* Just use the field; anonymous types can't have
744 nontrivial copy ctors or assignment ops or this
745 function would be deleted. */;
746 else
747 continue;
749 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
751 /* Compute the type of init->field */
752 quals = cvquals;
753 if (DECL_MUTABLE_P (field))
754 quals &= ~TYPE_QUAL_CONST;
755 expr_type = cp_build_qualified_type (expr_type, quals);
757 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
758 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
759 /* 'move' breaks bit-fields, and has no effect for scalars. */
760 && !scalarish_type_p (expr_type))
761 init = move (init);
763 if (DECL_NAME (field))
764 init = cp_build_modify_expr (input_location, comp, NOP_EXPR, init,
765 tf_warning_or_error);
766 else
767 init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
768 finish_expr_stmt (init);
771 finish_return_stmt (current_class_ref);
772 finish_compound_stmt (compound_stmt);
775 /* Synthesize FNDECL, a non-static member function. */
777 void
778 synthesize_method (tree fndecl)
780 bool nested = (current_function_decl != NULL_TREE);
781 tree context = decl_function_context (fndecl);
782 bool need_body = true;
783 tree stmt;
784 location_t save_input_location = input_location;
785 int error_count = errorcount;
786 int warning_count = warningcount + werrorcount;
788 /* Reset the source location, we might have been previously
789 deferred, and thus have saved where we were first needed. */
790 DECL_SOURCE_LOCATION (fndecl)
791 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
793 /* If we've been asked to synthesize a clone, just synthesize the
794 cloned function instead. Doing so will automatically fill in the
795 body for the clone. */
796 if (DECL_CLONED_FUNCTION_P (fndecl))
797 fndecl = DECL_CLONED_FUNCTION (fndecl);
799 /* We may be in the middle of deferred access check. Disable
800 it now. */
801 push_deferring_access_checks (dk_no_deferred);
803 if (! context)
804 push_to_top_level ();
805 else if (nested)
806 push_function_context ();
808 input_location = DECL_SOURCE_LOCATION (fndecl);
810 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
811 stmt = begin_function_body ();
813 if (DECL_OVERLOADED_OPERATOR_P (fndecl) == NOP_EXPR)
815 do_build_copy_assign (fndecl);
816 need_body = false;
818 else if (DECL_CONSTRUCTOR_P (fndecl))
820 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
821 if (arg_chain != void_list_node)
822 do_build_copy_constructor (fndecl);
823 else
824 finish_mem_initializers (NULL_TREE);
827 /* If we haven't yet generated the body of the function, just
828 generate an empty compound statement. */
829 if (need_body)
831 tree compound_stmt;
832 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
833 finish_compound_stmt (compound_stmt);
836 finish_function_body (stmt);
837 expand_or_defer_fn (finish_function (0));
839 input_location = save_input_location;
841 if (! context)
842 pop_from_top_level ();
843 else if (nested)
844 pop_function_context ();
846 pop_deferring_access_checks ();
848 if (error_count != errorcount || warning_count != warningcount + werrorcount)
849 inform (input_location, "synthesized method %qD first required here ",
850 fndecl);
853 /* Build a reference to type TYPE with cv-quals QUALS, which is an
854 rvalue if RVALUE is true. */
856 static tree
857 build_stub_type (tree type, int quals, bool rvalue)
859 tree argtype = cp_build_qualified_type (type, quals);
860 return cp_build_reference_type (argtype, rvalue);
863 /* Build a dummy glvalue from dereferencing a dummy reference of type
864 REFTYPE. */
866 static tree
867 build_stub_object (tree reftype)
869 if (TREE_CODE (reftype) != REFERENCE_TYPE)
870 reftype = cp_build_reference_type (reftype, /*rval*/true);
871 tree stub = build1 (CONVERT_EXPR, reftype, integer_one_node);
872 return convert_from_reference (stub);
875 /* Determine which function will be called when looking up NAME in TYPE,
876 called with a single ARGTYPE argument, or no argument if ARGTYPE is
877 null. FLAGS and COMPLAIN are as for build_new_method_call.
879 Returns a FUNCTION_DECL if all is well.
880 Returns NULL_TREE if overload resolution failed.
881 Returns error_mark_node if the chosen function cannot be called. */
883 static tree
884 locate_fn_flags (tree type, tree name, tree argtype, int flags,
885 tsubst_flags_t complain)
887 tree ob, fn, fns, binfo, rval;
888 vec<tree, va_gc> *args;
890 if (TYPE_P (type))
891 binfo = TYPE_BINFO (type);
892 else
894 binfo = type;
895 type = BINFO_TYPE (binfo);
898 ob = build_stub_object (cp_build_reference_type (type, false));
899 args = make_tree_vector ();
900 if (argtype)
902 if (TREE_CODE (argtype) == TREE_LIST)
904 for (tree elt = argtype; elt != void_list_node;
905 elt = TREE_CHAIN (elt))
907 tree type = TREE_VALUE (elt);
908 tree arg = build_stub_object (type);
909 vec_safe_push (args, arg);
912 else
914 tree arg = build_stub_object (argtype);
915 args->quick_push (arg);
919 fns = lookup_fnfields (binfo, name, 0);
920 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
922 release_tree_vector (args);
923 if (fn && rval == error_mark_node)
924 return rval;
925 else
926 return fn;
929 /* Locate the dtor of TYPE. */
931 tree
932 get_dtor (tree type, tsubst_flags_t complain)
934 tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
935 LOOKUP_NORMAL, complain);
936 if (fn == error_mark_node)
937 return NULL_TREE;
938 return fn;
941 /* Locate the default ctor of TYPE. */
943 tree
944 locate_ctor (tree type)
946 tree fn;
948 push_deferring_access_checks (dk_no_check);
949 fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
950 LOOKUP_SPECULATIVE, tf_none);
951 pop_deferring_access_checks ();
952 if (fn == error_mark_node)
953 return NULL_TREE;
954 return fn;
957 /* Likewise, but give any appropriate errors. */
959 tree
960 get_default_ctor (tree type)
962 tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
963 LOOKUP_NORMAL, tf_warning_or_error);
964 if (fn == error_mark_node)
965 return NULL_TREE;
966 return fn;
969 /* Locate the copy ctor of TYPE. */
971 tree
972 get_copy_ctor (tree type, tsubst_flags_t complain)
974 int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
975 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
976 tree argtype = build_stub_type (type, quals, false);
977 tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
978 LOOKUP_NORMAL, complain);
979 if (fn == error_mark_node)
980 return NULL_TREE;
981 return fn;
984 /* Locate the copy assignment operator of TYPE. */
986 tree
987 get_copy_assign (tree type)
989 int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
990 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
991 tree argtype = build_stub_type (type, quals, false);
992 tree fn = locate_fn_flags (type, ansi_assopname (NOP_EXPR), argtype,
993 LOOKUP_NORMAL, tf_warning_or_error);
994 if (fn == error_mark_node)
995 return NULL_TREE;
996 return fn;
999 /* Locate the inherited constructor of constructor CTOR. */
1001 tree
1002 get_inherited_ctor (tree ctor)
1004 gcc_assert (DECL_INHERITED_CTOR_BASE (ctor));
1006 push_deferring_access_checks (dk_no_check);
1007 tree fn = locate_fn_flags (DECL_INHERITED_CTOR_BASE (ctor),
1008 complete_ctor_identifier,
1009 FUNCTION_FIRST_USER_PARMTYPE (ctor),
1010 LOOKUP_NORMAL|LOOKUP_SPECULATIVE,
1011 tf_none);
1012 pop_deferring_access_checks ();
1013 if (fn == error_mark_node)
1014 return NULL_TREE;
1015 return fn;
1018 /* walk_tree helper function for is_trivially_xible. If *TP is a call,
1019 return it if it calls something other than a trivial special member
1020 function. */
1022 static tree
1023 check_nontriv (tree *tp, int *, void *)
1025 tree fn = cp_get_callee (*tp);
1026 if (fn == NULL_TREE)
1027 return NULL_TREE;
1029 if (TREE_CODE (fn) == ADDR_EXPR)
1030 fn = TREE_OPERAND (fn, 0);
1032 if (TREE_CODE (fn) != FUNCTION_DECL
1033 || !trivial_fn_p (fn))
1034 return fn;
1035 return NULL_TREE;
1038 /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */
1040 static tree
1041 assignable_expr (tree to, tree from)
1043 ++cp_unevaluated_operand;
1044 to = build_stub_object (to);
1045 from = build_stub_object (from);
1046 tree r = cp_build_modify_expr (input_location, to, NOP_EXPR, from, tf_none);
1047 --cp_unevaluated_operand;
1048 return r;
1051 /* The predicate condition for a template specialization
1052 is_constructible<T, Args...> shall be satisfied if and only if the
1053 following variable definition would be well-formed for some invented
1054 variable t: T t(create<Args>()...);
1056 Return something equivalent in well-formedness and triviality. */
1058 static tree
1059 constructible_expr (tree to, tree from)
1061 tree expr;
1062 if (CLASS_TYPE_P (to))
1064 tree ctype = to;
1065 vec<tree, va_gc> *args = NULL;
1066 if (TREE_CODE (to) != REFERENCE_TYPE)
1067 to = cp_build_reference_type (to, /*rval*/false);
1068 tree ob = build_stub_object (to);
1069 for (; from; from = TREE_CHAIN (from))
1070 vec_safe_push (args, build_stub_object (TREE_VALUE (from)));
1071 expr = build_special_member_call (ob, complete_ctor_identifier, &args,
1072 ctype, LOOKUP_NORMAL, tf_none);
1073 if (expr == error_mark_node)
1074 return error_mark_node;
1075 /* The current state of the standard vis-a-vis LWG 2116 is that
1076 is_*constructible involves destruction as well. */
1077 if (type_build_dtor_call (ctype))
1079 tree dtor = build_special_member_call (ob, complete_dtor_identifier,
1080 NULL, ctype, LOOKUP_NORMAL,
1081 tf_none);
1082 if (dtor == error_mark_node)
1083 return error_mark_node;
1084 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
1085 expr = build2 (COMPOUND_EXPR, void_type_node, expr, dtor);
1088 else
1090 if (from == NULL_TREE)
1091 return build_value_init (to, tf_none);
1092 else if (TREE_CHAIN (from))
1093 return error_mark_node; // too many initializers
1094 from = build_stub_object (TREE_VALUE (from));
1095 expr = perform_direct_initialization_if_possible (to, from,
1096 /*cast*/false,
1097 tf_none);
1099 return expr;
1102 /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or
1103 constructible (otherwise) from FROM, which is a single type for
1104 assignment or a list of types for construction. */
1106 bool
1107 is_trivially_xible (enum tree_code code, tree to, tree from)
1109 tree expr;
1110 if (code == MODIFY_EXPR)
1111 expr = assignable_expr (to, from);
1112 else if (from && TREE_CHAIN (from))
1113 return false; // only 0- and 1-argument ctors can be trivial
1114 else
1115 expr = constructible_expr (to, from);
1117 if (expr == error_mark_node)
1118 return false;
1119 tree nt = cp_walk_tree_without_duplicates (&expr, check_nontriv, NULL);
1120 return !nt;
1123 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
1124 DELETED_P or give an error message MSG with argument ARG. */
1126 static void
1127 process_subob_fn (tree fn, tree *spec_p, bool *trivial_p,
1128 bool *deleted_p, bool *constexpr_p,
1129 bool diag, tree arg, bool dtor_from_ctor = false)
1131 if (!fn || fn == error_mark_node)
1132 goto bad;
1134 if (spec_p)
1136 maybe_instantiate_noexcept (fn);
1137 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
1138 *spec_p = merge_exception_specifiers (*spec_p, raises);
1141 if (!trivial_fn_p (fn) && !dtor_from_ctor)
1143 if (trivial_p)
1144 *trivial_p = false;
1145 if (TREE_CODE (arg) == FIELD_DECL
1146 && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
1148 if (deleted_p)
1149 *deleted_p = true;
1150 if (diag)
1151 error ("union member %q+D with non-trivial %qD", arg, fn);
1155 if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
1157 *constexpr_p = false;
1158 if (diag)
1160 inform (DECL_SOURCE_LOCATION (fn),
1161 "defaulted constructor calls non-constexpr %qD", fn);
1162 explain_invalid_constexpr_fn (fn);
1166 return;
1168 bad:
1169 if (deleted_p)
1170 *deleted_p = true;
1173 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
1174 aggregates. If DTOR_FROM_CTOR is true, we're walking subobject destructors
1175 called from a synthesized constructor, in which case we don't consider
1176 the triviality of the subobject destructor. */
1178 static void
1179 walk_field_subobs (tree fields, tree fnname, special_function_kind sfk,
1180 int quals, bool copy_arg_p, bool move_p,
1181 bool assign_p, tree *spec_p, bool *trivial_p,
1182 bool *deleted_p, bool *constexpr_p,
1183 bool diag, int flags, tsubst_flags_t complain,
1184 bool dtor_from_ctor)
1186 tree field;
1187 for (field = fields; field; field = DECL_CHAIN (field))
1189 tree mem_type, argtype, rval;
1191 if (TREE_CODE (field) != FIELD_DECL
1192 || DECL_ARTIFICIAL (field))
1193 continue;
1195 mem_type = strip_array_types (TREE_TYPE (field));
1196 if (assign_p)
1198 bool bad = true;
1199 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
1201 if (diag)
1202 error ("non-static const member %q#D, can%'t use default "
1203 "assignment operator", field);
1205 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1207 if (diag)
1208 error ("non-static reference member %q#D, can%'t use "
1209 "default assignment operator", field);
1211 else
1212 bad = false;
1214 if (bad && deleted_p)
1215 *deleted_p = true;
1217 else if (sfk == sfk_constructor)
1219 bool bad;
1221 if (DECL_INITIAL (field))
1223 if (diag && DECL_INITIAL (field) == error_mark_node)
1224 inform (DECL_SOURCE_LOCATION (field),
1225 "initializer for %q#D is invalid", field);
1226 if (trivial_p)
1227 *trivial_p = false;
1228 /* Core 1351: If the field has an NSDMI that could throw, the
1229 default constructor is noexcept(false). */
1230 if (spec_p)
1232 tree nsdmi = get_nsdmi (field, /*ctor*/false);
1233 if (!expr_noexcept_p (nsdmi, complain))
1234 *spec_p = noexcept_false_spec;
1236 /* Don't do the normal processing. */
1237 continue;
1240 bad = false;
1241 if (CP_TYPE_CONST_P (mem_type)
1242 && default_init_uninitialized_part (mem_type))
1244 if (diag)
1246 error ("uninitialized const member in %q#T",
1247 current_class_type);
1248 inform (DECL_SOURCE_LOCATION (field),
1249 "%q#D should be initialized", field);
1251 bad = true;
1253 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1255 if (diag)
1257 error ("uninitialized reference member in %q#T",
1258 current_class_type);
1259 inform (DECL_SOURCE_LOCATION (field),
1260 "%q#D should be initialized", field);
1262 bad = true;
1265 if (bad && deleted_p)
1266 *deleted_p = true;
1268 /* For an implicitly-defined default constructor to be constexpr,
1269 every member must have a user-provided default constructor or
1270 an explicit initializer. */
1271 if (constexpr_p && !CLASS_TYPE_P (mem_type)
1272 && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE)
1274 *constexpr_p = false;
1275 if (diag)
1276 inform (DECL_SOURCE_LOCATION (field),
1277 "defaulted default constructor does not "
1278 "initialize %q#D", field);
1281 else if (sfk == sfk_copy_constructor)
1283 /* 12.8p11b5 */
1284 if (TREE_CODE (mem_type) == REFERENCE_TYPE
1285 && TYPE_REF_IS_RVALUE (mem_type))
1287 if (diag)
1288 error ("copying non-static data member %q#D of rvalue "
1289 "reference type", field);
1290 if (deleted_p)
1291 *deleted_p = true;
1295 if (!CLASS_TYPE_P (mem_type))
1296 continue;
1298 if (ANON_AGGR_TYPE_P (mem_type))
1300 walk_field_subobs (TYPE_FIELDS (mem_type), fnname, sfk, quals,
1301 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1302 deleted_p, constexpr_p,
1303 diag, flags, complain, dtor_from_ctor);
1304 continue;
1307 if (copy_arg_p)
1309 int mem_quals = cp_type_quals (mem_type) | quals;
1310 if (DECL_MUTABLE_P (field))
1311 mem_quals &= ~TYPE_QUAL_CONST;
1312 argtype = build_stub_type (mem_type, mem_quals, move_p);
1314 else
1315 argtype = NULL_TREE;
1317 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
1319 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1320 constexpr_p, diag, field, dtor_from_ctor);
1324 /* The caller wants to generate an implicit declaration of SFK for CTYPE
1325 which is const if relevant and CONST_P is set. If spec_p, trivial_p and
1326 deleted_p are non-null, set their referent appropriately. If diag is
1327 true, we're either being called from maybe_explain_implicit_delete to
1328 give errors, or if constexpr_p is non-null, from
1329 explain_invalid_constexpr_fn. */
1331 static void
1332 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
1333 tree *spec_p, bool *trivial_p, bool *deleted_p,
1334 bool *constexpr_p, bool diag,
1335 tree inherited_base, tree inherited_parms)
1337 tree binfo, base_binfo, scope, fnname, rval, argtype;
1338 bool move_p, copy_arg_p, assign_p, expected_trivial, check_vdtor;
1339 vec<tree, va_gc> *vbases;
1340 int i, quals, flags;
1341 tsubst_flags_t complain;
1342 bool ctor_p;
1344 if (spec_p)
1345 *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec);
1347 if (deleted_p)
1349 /* "The closure type associated with a lambda-expression has a deleted
1350 default constructor and a deleted copy assignment operator."
1351 This is diagnosed in maybe_explain_implicit_delete. */
1352 if (LAMBDA_TYPE_P (ctype)
1353 && (sfk == sfk_constructor
1354 || sfk == sfk_copy_assignment))
1356 *deleted_p = true;
1357 return;
1360 *deleted_p = false;
1363 ctor_p = false;
1364 assign_p = false;
1365 check_vdtor = false;
1366 switch (sfk)
1368 case sfk_move_assignment:
1369 case sfk_copy_assignment:
1370 assign_p = true;
1371 fnname = ansi_assopname (NOP_EXPR);
1372 break;
1374 case sfk_destructor:
1375 check_vdtor = true;
1376 /* The synthesized method will call base dtors, but check complete
1377 here to avoid having to deal with VTT. */
1378 fnname = complete_dtor_identifier;
1379 break;
1381 case sfk_constructor:
1382 case sfk_move_constructor:
1383 case sfk_copy_constructor:
1384 case sfk_inheriting_constructor:
1385 ctor_p = true;
1386 fnname = complete_ctor_identifier;
1387 break;
1389 default:
1390 gcc_unreachable ();
1393 gcc_assert ((sfk == sfk_inheriting_constructor)
1394 == (inherited_base != NULL_TREE));
1396 /* If that user-written default constructor would satisfy the
1397 requirements of a constexpr constructor (7.1.5), the
1398 implicitly-defined default constructor is constexpr.
1400 The implicitly-defined copy/move assignment operator is constexpr if
1401 - X is a literal type, and
1402 - the assignment operator selected to copy/move each direct base class
1403 subobject is a constexpr function, and
1404 - for each non-static data member of X that is of class type (or array
1405 thereof), the assignment operator selected to copy/move that member is a
1406 constexpr function. */
1407 if (constexpr_p)
1408 *constexpr_p = ctor_p
1409 || (assign_p && cxx_dialect >= cxx14);
1411 move_p = false;
1412 switch (sfk)
1414 case sfk_constructor:
1415 case sfk_destructor:
1416 case sfk_inheriting_constructor:
1417 copy_arg_p = false;
1418 break;
1420 case sfk_move_constructor:
1421 case sfk_move_assignment:
1422 move_p = true;
1423 /* FALLTHRU */
1424 case sfk_copy_constructor:
1425 case sfk_copy_assignment:
1426 copy_arg_p = true;
1427 break;
1429 default:
1430 gcc_unreachable ();
1433 expected_trivial = type_has_trivial_fn (ctype, sfk);
1434 if (trivial_p)
1435 *trivial_p = expected_trivial;
1437 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
1438 class versions and other properties of the type. But a subobject
1439 class can be trivially copyable and yet have overload resolution
1440 choose a template constructor for initialization, depending on
1441 rvalueness and cv-quals. And furthermore, a member in a base might
1442 be trivial but deleted or otherwise not callable. So we can't exit
1443 early in C++0x. The same considerations apply in C++98/03, but
1444 there the definition of triviality does not consider overload
1445 resolution, so a constructor can be trivial even if it would otherwise
1446 call a non-trivial constructor. */
1447 if (expected_trivial
1448 && (!copy_arg_p || cxx_dialect < cxx11))
1450 if (constexpr_p && sfk == sfk_constructor)
1452 bool cx = trivial_default_constructor_is_constexpr (ctype);
1453 *constexpr_p = cx;
1454 if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
1455 /* A trivial constructor doesn't have any NSDMI. */
1456 inform (input_location, "defaulted default constructor does "
1457 "not initialize any non-static data member");
1459 if (!diag && cxx_dialect < cxx11)
1460 return;
1463 ++cp_unevaluated_operand;
1464 ++c_inhibit_evaluation_warnings;
1465 push_deferring_access_checks (dk_no_deferred);
1467 scope = push_scope (ctype);
1469 flags = LOOKUP_NORMAL|LOOKUP_SPECULATIVE;
1470 if (!inherited_base)
1471 flags |= LOOKUP_DEFAULTED;
1473 complain = diag ? tf_warning_or_error : tf_none;
1475 if (const_p)
1476 quals = TYPE_QUAL_CONST;
1477 else
1478 quals = TYPE_UNQUALIFIED;
1479 argtype = NULL_TREE;
1481 for (binfo = TYPE_BINFO (ctype), i = 0;
1482 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
1484 tree basetype = BINFO_TYPE (base_binfo);
1486 if (!assign_p && BINFO_VIRTUAL_P (base_binfo))
1487 /* We'll handle virtual bases below. */
1488 continue;
1490 if (copy_arg_p)
1491 argtype = build_stub_type (basetype, quals, move_p);
1492 else if (basetype == inherited_base)
1493 argtype = inherited_parms;
1494 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1495 if (inherited_base)
1496 argtype = NULL_TREE;
1498 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1499 constexpr_p, diag, basetype);
1500 if (ctor_p)
1502 /* In a constructor we also need to check the subobject
1503 destructors for cleanup of partially constructed objects. */
1504 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1505 NULL_TREE, flags, complain);
1506 /* Note that we don't pass down trivial_p; the subobject
1507 destructors don't affect triviality of the constructor. Nor
1508 do they affect constexpr-ness (a constant expression doesn't
1509 throw) or exception-specification (a throw from one of the
1510 dtors would be a double-fault). */
1511 process_subob_fn (rval, NULL, NULL,
1512 deleted_p, NULL, false,
1513 basetype, /*dtor_from_ctor*/true);
1516 if (check_vdtor && type_has_virtual_destructor (basetype))
1518 rval = locate_fn_flags (ctype, ansi_opname (DELETE_EXPR),
1519 ptr_type_node, flags, complain);
1520 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
1521 to have a null rval (no class-specific op delete). */
1522 if (rval && rval == error_mark_node && deleted_p)
1523 *deleted_p = true;
1524 check_vdtor = false;
1527 if (diag && assign_p && move_p
1528 && BINFO_VIRTUAL_P (base_binfo)
1529 && rval && TREE_CODE (rval) == FUNCTION_DECL
1530 && move_fn_p (rval) && !trivial_fn_p (rval)
1531 && vbase_has_user_provided_move_assign (basetype))
1532 warning (OPT_Wvirtual_move_assign,
1533 "defaulted move assignment for %qT calls a non-trivial "
1534 "move assignment operator for virtual base %qT",
1535 ctype, basetype);
1538 vbases = CLASSTYPE_VBASECLASSES (ctype);
1539 if (vec_safe_is_empty (vbases))
1540 /* No virtual bases to worry about. */;
1541 else if (!assign_p)
1543 if (constexpr_p)
1544 *constexpr_p = false;
1545 FOR_EACH_VEC_ELT (*vbases, i, base_binfo)
1547 tree basetype = BINFO_TYPE (base_binfo);
1548 if (copy_arg_p)
1549 argtype = build_stub_type (basetype, quals, move_p);
1550 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1552 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1553 constexpr_p, diag, basetype);
1554 if (ctor_p && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype))
1556 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1557 NULL_TREE, flags, complain);
1558 process_subob_fn (rval, NULL, NULL,
1559 deleted_p, NULL, false,
1560 basetype, /*dtor_from_ctor*/true);
1565 /* Now handle the non-static data members. */
1566 walk_field_subobs (TYPE_FIELDS (ctype), fnname, sfk, quals,
1567 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1568 deleted_p, constexpr_p,
1569 diag, flags, complain, /*dtor_from_ctor*/false);
1570 if (ctor_p)
1571 walk_field_subobs (TYPE_FIELDS (ctype), complete_dtor_identifier,
1572 sfk_destructor, TYPE_UNQUALIFIED, false,
1573 false, false, NULL, NULL,
1574 deleted_p, NULL,
1575 false, flags, complain, /*dtor_from_ctor*/true);
1577 pop_scope (scope);
1579 pop_deferring_access_checks ();
1580 --cp_unevaluated_operand;
1581 --c_inhibit_evaluation_warnings;
1584 /* DECL is a defaulted function whose exception specification is now
1585 needed. Return what it should be. */
1587 tree
1588 get_defaulted_eh_spec (tree decl)
1590 if (DECL_CLONED_FUNCTION_P (decl))
1591 decl = DECL_CLONED_FUNCTION (decl);
1592 special_function_kind sfk = special_function_p (decl);
1593 tree ctype = DECL_CONTEXT (decl);
1594 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1595 tree parm_type = TREE_VALUE (parms);
1596 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1597 tree spec = empty_except_spec;
1598 synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL,
1599 NULL, false, DECL_INHERITED_CTOR_BASE (decl),
1600 parms);
1601 return spec;
1604 /* DECL is a deleted function. If it's implicitly deleted, explain why and
1605 return true; else return false. */
1607 bool
1608 maybe_explain_implicit_delete (tree decl)
1610 /* If decl is a clone, get the primary variant. */
1611 decl = DECL_ORIGIN (decl);
1612 gcc_assert (DECL_DELETED_FN (decl));
1613 if (DECL_DEFAULTED_FN (decl))
1615 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
1616 static hash_set<tree> *explained;
1618 special_function_kind sfk;
1619 location_t loc;
1620 bool informed;
1621 tree ctype;
1623 if (!explained)
1624 explained = new hash_set<tree>;
1625 if (explained->add (decl))
1626 return true;
1628 sfk = special_function_p (decl);
1629 ctype = DECL_CONTEXT (decl);
1630 loc = input_location;
1631 input_location = DECL_SOURCE_LOCATION (decl);
1633 informed = false;
1634 if (LAMBDA_TYPE_P (ctype))
1636 informed = true;
1637 if (sfk == sfk_constructor)
1638 inform (DECL_SOURCE_LOCATION (decl),
1639 "a lambda closure type has a deleted default constructor");
1640 else if (sfk == sfk_copy_assignment)
1641 inform (DECL_SOURCE_LOCATION (decl),
1642 "a lambda closure type has a deleted copy assignment operator");
1643 else
1644 informed = false;
1646 else if (DECL_ARTIFICIAL (decl)
1647 && (sfk == sfk_copy_assignment
1648 || sfk == sfk_copy_constructor)
1649 && (type_has_user_declared_move_constructor (ctype)
1650 || type_has_user_declared_move_assign (ctype)))
1652 inform (DECL_SOURCE_LOCATION (decl),
1653 "%q#D is implicitly declared as deleted because %qT "
1654 "declares a move constructor or move assignment operator",
1655 decl, ctype);
1656 informed = true;
1658 if (!informed)
1660 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1661 tree parm_type = TREE_VALUE (parms);
1662 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1663 tree raises = NULL_TREE;
1664 bool deleted_p = false;
1665 tree scope = push_scope (ctype);
1667 synthesized_method_walk (ctype, sfk, const_p,
1668 &raises, NULL, &deleted_p, NULL, false,
1669 DECL_INHERITED_CTOR_BASE (decl), parms);
1670 if (deleted_p)
1672 inform (DECL_SOURCE_LOCATION (decl),
1673 "%q#D is implicitly deleted because the default "
1674 "definition would be ill-formed:", decl);
1675 synthesized_method_walk (ctype, sfk, const_p,
1676 NULL, NULL, NULL, NULL, true,
1677 DECL_INHERITED_CTOR_BASE (decl), parms);
1679 else if (!comp_except_specs
1680 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
1681 raises, ce_normal))
1682 inform (DECL_SOURCE_LOCATION (decl), "%q#F is implicitly "
1683 "deleted because its exception-specification does not "
1684 "match the implicit exception-specification %qX",
1685 decl, raises);
1686 else if (flag_checking)
1687 gcc_unreachable ();
1689 pop_scope (scope);
1692 input_location = loc;
1693 return true;
1695 return false;
1698 /* DECL is a defaulted function which was declared constexpr. Explain why
1699 it can't be constexpr. */
1701 void
1702 explain_implicit_non_constexpr (tree decl)
1704 tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl));
1705 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1706 bool dummy;
1707 synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
1708 special_function_p (decl), const_p,
1709 NULL, NULL, NULL, &dummy, true,
1710 DECL_INHERITED_CTOR_BASE (decl),
1711 FUNCTION_FIRST_USER_PARMTYPE (decl));
1714 /* DECL is an instantiation of an inheriting constructor template. Deduce
1715 the correct exception-specification and deletedness for this particular
1716 specialization. */
1718 void
1719 deduce_inheriting_ctor (tree decl)
1721 gcc_assert (DECL_INHERITED_CTOR_BASE (decl));
1722 tree spec;
1723 bool trivial, constexpr_, deleted;
1724 synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor,
1725 false, &spec, &trivial, &deleted, &constexpr_,
1726 /*diag*/false,
1727 DECL_INHERITED_CTOR_BASE (decl),
1728 FUNCTION_FIRST_USER_PARMTYPE (decl));
1729 DECL_DELETED_FN (decl) = deleted;
1730 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
1733 /* Implicitly declare the special function indicated by KIND, as a
1734 member of TYPE. For copy constructors and assignment operators,
1735 CONST_P indicates whether these functions should take a const
1736 reference argument or a non-const reference. Returns the
1737 FUNCTION_DECL for the implicitly declared function. */
1739 tree
1740 implicitly_declare_fn (special_function_kind kind, tree type,
1741 bool const_p, tree inherited_ctor,
1742 tree inherited_parms)
1744 tree fn;
1745 tree parameter_types = void_list_node;
1746 tree return_type;
1747 tree fn_type;
1748 tree raises = empty_except_spec;
1749 tree rhs_parm_type = NULL_TREE;
1750 tree this_parm;
1751 tree name;
1752 HOST_WIDE_INT saved_processing_template_decl;
1753 bool deleted_p;
1754 bool constexpr_p;
1756 /* Because we create declarations for implicitly declared functions
1757 lazily, we may be creating the declaration for a member of TYPE
1758 while in some completely different context. However, TYPE will
1759 never be a dependent class (because we never want to do lookups
1760 for implicitly defined functions in a dependent class).
1761 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
1762 because we only create clones for constructors and destructors
1763 when not in a template. */
1764 gcc_assert (!dependent_type_p (type));
1765 saved_processing_template_decl = processing_template_decl;
1766 processing_template_decl = 0;
1768 type = TYPE_MAIN_VARIANT (type);
1770 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (type))
1772 if (kind == sfk_destructor)
1773 /* See comment in check_special_function_return_type. */
1774 return_type = build_pointer_type (void_type_node);
1775 else
1776 return_type = build_pointer_type (type);
1778 else
1779 return_type = void_type_node;
1781 switch (kind)
1783 case sfk_destructor:
1784 /* Destructor. */
1785 name = constructor_name (type);
1786 break;
1788 case sfk_constructor:
1789 /* Default constructor. */
1790 name = constructor_name (type);
1791 break;
1793 case sfk_copy_constructor:
1794 case sfk_copy_assignment:
1795 case sfk_move_constructor:
1796 case sfk_move_assignment:
1797 case sfk_inheriting_constructor:
1799 bool move_p;
1800 if (kind == sfk_copy_assignment
1801 || kind == sfk_move_assignment)
1803 return_type = build_reference_type (type);
1804 name = ansi_assopname (NOP_EXPR);
1806 else
1807 name = constructor_name (type);
1809 if (kind == sfk_inheriting_constructor)
1810 parameter_types = inherited_parms;
1811 else
1813 if (const_p)
1814 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
1815 else
1816 rhs_parm_type = type;
1817 move_p = (kind == sfk_move_assignment
1818 || kind == sfk_move_constructor);
1819 rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
1821 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
1823 break;
1825 default:
1826 gcc_unreachable ();
1829 tree inherited_base = (inherited_ctor
1830 ? DECL_CONTEXT (inherited_ctor)
1831 : NULL_TREE);
1832 bool trivial_p = false;
1834 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
1836 /* For an inheriting constructor template, just copy these flags from
1837 the inherited constructor template for now. */
1838 raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor));
1839 deleted_p = DECL_DELETED_FN (inherited_ctor);
1840 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
1842 else if (cxx_dialect >= cxx11)
1844 raises = unevaluated_noexcept_spec ();
1845 synthesized_method_walk (type, kind, const_p, NULL, &trivial_p,
1846 &deleted_p, &constexpr_p, false,
1847 inherited_base, inherited_parms);
1849 else
1850 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
1851 &deleted_p, &constexpr_p, false,
1852 inherited_base, inherited_parms);
1853 /* Don't bother marking a deleted constructor as constexpr. */
1854 if (deleted_p)
1855 constexpr_p = false;
1856 /* A trivial copy/move constructor is also a constexpr constructor,
1857 unless the class has virtual bases (7.1.5p4). */
1858 else if (trivial_p && cxx_dialect >= cxx11
1859 && (kind == sfk_copy_constructor
1860 || kind == sfk_move_constructor)
1861 && !CLASSTYPE_VBASECLASSES (type))
1862 gcc_assert (constexpr_p);
1864 if (!trivial_p && type_has_trivial_fn (type, kind))
1865 type_set_nontrivial_flag (type, kind);
1867 /* Create the function. */
1868 fn_type = build_method_type_directly (type, return_type, parameter_types);
1869 if (raises)
1870 fn_type = build_exception_variant (fn_type, raises);
1871 fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
1872 if (kind != sfk_inheriting_constructor)
1873 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
1874 if (kind == sfk_constructor || kind == sfk_copy_constructor
1875 || kind == sfk_move_constructor || kind == sfk_inheriting_constructor)
1876 DECL_CONSTRUCTOR_P (fn) = 1;
1877 else if (kind == sfk_destructor)
1878 DECL_DESTRUCTOR_P (fn) = 1;
1879 else
1881 DECL_ASSIGNMENT_OPERATOR_P (fn) = 1;
1882 SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR);
1885 SET_DECL_ALIGN (fn, MINIMUM_METHOD_BOUNDARY);
1887 /* Create the explicit arguments. */
1888 if (rhs_parm_type)
1890 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
1891 want its type to be included in the mangled function
1892 name. */
1893 tree decl = cp_build_parm_decl (NULL_TREE, rhs_parm_type);
1894 TREE_READONLY (decl) = 1;
1895 retrofit_lang_decl (decl);
1896 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
1897 DECL_ARGUMENTS (fn) = decl;
1899 else if (kind == sfk_inheriting_constructor)
1901 tree *p = &DECL_ARGUMENTS (fn);
1902 int index = 1;
1903 for (tree parm = inherited_parms; parm != void_list_node;
1904 parm = TREE_CHAIN (parm))
1906 *p = cp_build_parm_decl (NULL_TREE, TREE_VALUE (parm));
1907 retrofit_lang_decl (*p);
1908 DECL_PARM_LEVEL (*p) = 1;
1909 DECL_PARM_INDEX (*p) = index++;
1910 DECL_CONTEXT (*p) = fn;
1911 p = &DECL_CHAIN (*p);
1913 SET_DECL_INHERITED_CTOR_BASE (fn, inherited_base);
1914 DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor);
1915 /* A constructor so declared has the same access as the corresponding
1916 constructor in X. */
1917 TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor);
1918 TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor);
1919 /* Copy constexpr from the inherited constructor even if the
1920 inheriting constructor doesn't satisfy the requirements. */
1921 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
1923 /* Add the "this" parameter. */
1924 this_parm = build_this_parm (fn_type, TYPE_UNQUALIFIED);
1925 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
1926 DECL_ARGUMENTS (fn) = this_parm;
1928 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
1929 DECL_IN_AGGR_P (fn) = 1;
1930 DECL_ARTIFICIAL (fn) = 1;
1931 DECL_DEFAULTED_FN (fn) = 1;
1932 if (cxx_dialect >= cxx11)
1934 /* "The closure type associated with a lambda-expression has a deleted
1935 default constructor and a deleted copy assignment operator." */
1936 if ((kind == sfk_constructor
1937 || kind == sfk_copy_assignment)
1938 && LAMBDA_TYPE_P (type))
1939 deleted_p = true;
1940 DECL_DELETED_FN (fn) = deleted_p;
1941 DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
1943 DECL_EXTERNAL (fn) = true;
1944 DECL_NOT_REALLY_EXTERN (fn) = 1;
1945 DECL_DECLARED_INLINE_P (fn) = 1;
1946 set_linkage_according_to_type (type, fn);
1947 if (TREE_PUBLIC (fn))
1948 DECL_COMDAT (fn) = 1;
1949 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
1950 gcc_assert (!TREE_USED (fn));
1952 /* Propagate constraints from the inherited constructor. */
1953 if (flag_concepts && inherited_ctor)
1954 if (tree orig_ci = get_constraints (inherited_ctor))
1956 tree new_ci = copy_node (orig_ci);
1957 set_constraints (fn, new_ci);
1960 /* Restore PROCESSING_TEMPLATE_DECL. */
1961 processing_template_decl = saved_processing_template_decl;
1963 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
1964 fn = add_inherited_template_parms (fn, inherited_ctor);
1966 /* Warn about calling a non-trivial move assignment in a virtual base. */
1967 if (kind == sfk_move_assignment && !deleted_p && !trivial_p
1968 && CLASSTYPE_VBASECLASSES (type))
1970 location_t loc = input_location;
1971 input_location = DECL_SOURCE_LOCATION (fn);
1972 synthesized_method_walk (type, kind, const_p,
1973 NULL, NULL, NULL, NULL, true,
1974 NULL_TREE, NULL_TREE);
1975 input_location = loc;
1978 return fn;
1981 /* Gives any errors about defaulted functions which need to be deferred
1982 until the containing class is complete. */
1984 void
1985 defaulted_late_check (tree fn)
1987 /* Complain about invalid signature for defaulted fn. */
1988 tree ctx = DECL_CONTEXT (fn);
1989 special_function_kind kind = special_function_p (fn);
1990 bool fn_const_p = (copy_fn_p (fn) == 2);
1991 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
1992 NULL, NULL);
1993 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
1995 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
1996 TREE_TYPE (TREE_TYPE (implicit_fn)))
1997 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1998 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn))))
2000 error ("defaulted declaration %q+D", fn);
2001 error_at (DECL_SOURCE_LOCATION (fn),
2002 "does not match expected signature %qD", implicit_fn);
2005 /* 8.4.2/2: An explicitly-defaulted function (...) may have an explicit
2006 exception-specification only if it is compatible (15.4) with the
2007 exception-specification on the implicit declaration. If a function
2008 is explicitly defaulted on its first declaration, (...) it is
2009 implicitly considered to have the same exception-specification as if
2010 it had been implicitly declared. */
2011 maybe_instantiate_noexcept (fn);
2012 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2013 if (!fn_spec)
2015 if (DECL_DEFAULTED_IN_CLASS_P (fn))
2016 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
2018 else if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
2019 /* Equivalent to the implicit spec. */;
2020 else if (DECL_DEFAULTED_IN_CLASS_P (fn)
2021 && !CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
2022 /* We can't compare an explicit exception-specification on a
2023 constructor defaulted in the class body to the implicit
2024 exception-specification until after we've parsed any NSDMI; see
2025 after_nsdmi_defaulted_late_checks. */;
2026 else
2028 tree eh_spec = get_defaulted_eh_spec (fn);
2029 if (!comp_except_specs (fn_spec, eh_spec, ce_normal))
2031 if (DECL_DEFAULTED_IN_CLASS_P (fn))
2032 DECL_DELETED_FN (fn) = true;
2033 else
2034 error ("function %q+D defaulted on its redeclaration "
2035 "with an exception-specification that differs from "
2036 "the implicit exception-specification %qX", fn, eh_spec);
2040 if (DECL_DEFAULTED_IN_CLASS_P (fn)
2041 && DECL_DECLARED_CONSTEXPR_P (implicit_fn))
2043 /* Hmm...should we do this for out-of-class too? Should it be OK to
2044 add constexpr later like inline, rather than requiring
2045 declarations to match? */
2046 DECL_DECLARED_CONSTEXPR_P (fn) = true;
2047 if (kind == sfk_constructor)
2048 TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
2051 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
2052 && DECL_DECLARED_CONSTEXPR_P (fn))
2054 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
2056 error ("explicitly defaulted function %q+D cannot be declared "
2057 "as constexpr because the implicit declaration is not "
2058 "constexpr:", fn);
2059 explain_implicit_non_constexpr (fn);
2061 DECL_DECLARED_CONSTEXPR_P (fn) = false;
2064 if (DECL_DELETED_FN (implicit_fn))
2065 DECL_DELETED_FN (fn) = 1;
2068 /* OK, we've parsed the NSDMI for class T, now we can check any explicit
2069 exception-specifications on functions defaulted in the class body. */
2071 void
2072 after_nsdmi_defaulted_late_checks (tree t)
2074 if (uses_template_parms (t))
2075 return;
2076 if (t == error_mark_node)
2077 return;
2078 for (tree fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
2079 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
2081 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2082 if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
2083 continue;
2085 tree eh_spec = get_defaulted_eh_spec (fn);
2086 if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)),
2087 eh_spec, ce_normal))
2088 DECL_DELETED_FN (fn) = true;
2092 /* Returns true iff FN can be explicitly defaulted, and gives any
2093 errors if defaulting FN is ill-formed. */
2095 bool
2096 defaultable_fn_check (tree fn)
2098 special_function_kind kind = sfk_none;
2100 if (template_parm_scope_p ())
2102 error ("a template cannot be defaulted");
2103 return false;
2106 if (DECL_CONSTRUCTOR_P (fn))
2108 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
2109 kind = sfk_constructor;
2110 else if (copy_fn_p (fn) > 0
2111 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
2112 == void_list_node))
2113 kind = sfk_copy_constructor;
2114 else if (move_fn_p (fn))
2115 kind = sfk_move_constructor;
2117 else if (DECL_DESTRUCTOR_P (fn))
2118 kind = sfk_destructor;
2119 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2120 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
2122 if (copy_fn_p (fn))
2123 kind = sfk_copy_assignment;
2124 else if (move_fn_p (fn))
2125 kind = sfk_move_assignment;
2128 if (kind == sfk_none)
2130 error ("%qD cannot be defaulted", fn);
2131 return false;
2133 else
2135 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
2136 t && t != void_list_node; t = TREE_CHAIN (t))
2137 if (TREE_PURPOSE (t))
2139 error ("defaulted function %q+D with default argument", fn);
2140 break;
2143 /* Avoid do_warn_unused_parameter warnings. */
2144 for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p))
2145 if (DECL_NAME (p))
2146 TREE_NO_WARNING (p) = 1;
2148 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn)))
2149 /* Defer checking. */;
2150 else if (!processing_template_decl)
2151 defaulted_late_check (fn);
2153 return true;
2157 /* Add an implicit declaration to TYPE for the kind of function
2158 indicated by SFK. Return the FUNCTION_DECL for the new implicit
2159 declaration. */
2161 tree
2162 lazily_declare_fn (special_function_kind sfk, tree type)
2164 tree fn;
2165 /* Whether or not the argument has a const reference type. */
2166 bool const_p = false;
2168 type = TYPE_MAIN_VARIANT (type);
2170 switch (sfk)
2172 case sfk_constructor:
2173 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
2174 break;
2175 case sfk_copy_constructor:
2176 const_p = TYPE_HAS_CONST_COPY_CTOR (type);
2177 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
2178 break;
2179 case sfk_move_constructor:
2180 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
2181 break;
2182 case sfk_copy_assignment:
2183 const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
2184 CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
2185 break;
2186 case sfk_move_assignment:
2187 CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
2188 break;
2189 case sfk_destructor:
2190 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
2191 break;
2192 default:
2193 gcc_unreachable ();
2196 /* Declare the function. */
2197 fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL);
2199 /* [class.copy]/8 If the class definition declares a move constructor or
2200 move assignment operator, the implicitly declared copy constructor is
2201 defined as deleted.... */
2202 if ((sfk == sfk_copy_assignment
2203 || sfk == sfk_copy_constructor)
2204 && (type_has_user_declared_move_constructor (type)
2205 || type_has_user_declared_move_assign (type)))
2206 DECL_DELETED_FN (fn) = true;
2208 /* A destructor may be virtual. */
2209 if (sfk == sfk_destructor
2210 || sfk == sfk_move_assignment
2211 || sfk == sfk_copy_assignment)
2212 check_for_override (fn, type);
2213 /* Add it to CLASSTYPE_METHOD_VEC. */
2214 add_method (type, fn, NULL_TREE);
2215 /* Add it to TYPE_METHODS. */
2216 if (sfk == sfk_destructor
2217 && DECL_VIRTUAL_P (fn))
2218 /* The ABI requires that a virtual destructor go at the end of the
2219 vtable. */
2220 TYPE_METHODS (type) = chainon (TYPE_METHODS (type), fn);
2221 else
2223 DECL_CHAIN (fn) = TYPE_METHODS (type);
2224 TYPE_METHODS (type) = fn;
2226 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
2227 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
2228 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
2229 /* Create appropriate clones. */
2230 clone_function_decl (fn, /*update_method_vec=*/true);
2232 return fn;
2235 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
2236 as there are artificial parms in FN. */
2238 tree
2239 skip_artificial_parms_for (const_tree fn, tree list)
2241 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2242 list = TREE_CHAIN (list);
2243 else
2244 return list;
2246 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2247 list = TREE_CHAIN (list);
2248 if (DECL_HAS_VTT_PARM_P (fn))
2249 list = TREE_CHAIN (list);
2250 return list;
2253 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
2254 artificial parms in FN. */
2257 num_artificial_parms_for (const_tree fn)
2259 int count = 0;
2261 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2262 count++;
2263 else
2264 return 0;
2266 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2267 count++;
2268 if (DECL_HAS_VTT_PARM_P (fn))
2269 count++;
2270 return count;
2274 #include "gt-cp-method.h"