PR libstdc++/67078
[official-gcc.git] / gcc / cp / method.c
blob7d7ec7da187b5b58c331155b072a738df1dc2e38
1 /* Handle the hair of processing (but not expanding) inline functions.
2 Also manage function and variable name overloading.
3 Copyright (C) 1987-2015 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 "tm.h"
28 #include "alias.h"
29 #include "tree.h"
30 #include "stringpool.h"
31 #include "varasm.h"
32 #include "cp-tree.h"
33 #include "flags.h"
34 #include "toplev.h"
35 #include "tm_p.h"
36 #include "target.h"
37 #include "common/common-target.h"
38 #include "diagnostic.h"
39 #include "hard-reg-set.h"
40 #include "function.h"
41 #include "cgraph.h"
43 /* Various flags to control the mangling process. */
45 enum mangling_flags
47 /* No flags. */
48 mf_none = 0,
49 /* The thing we are presently mangling is part of a template type,
50 rather than a fully instantiated type. Therefore, we may see
51 complex expressions where we would normally expect to see a
52 simple integer constant. */
53 mf_maybe_uninstantiated = 1,
54 /* When mangling a numeric value, use the form `_XX_' (instead of
55 just `XX') if the value has more than one digit. */
56 mf_use_underscores_around_value = 2
59 typedef enum mangling_flags mangling_flags;
61 static void do_build_copy_assign (tree);
62 static void do_build_copy_constructor (tree);
63 static tree make_alias_for_thunk (tree);
65 /* Called once to initialize method.c. */
67 void
68 init_method (void)
70 init_mangle ();
73 /* Return a this or result adjusting thunk to FUNCTION. THIS_ADJUSTING
74 indicates whether it is a this or result adjusting thunk.
75 FIXED_OFFSET and VIRTUAL_OFFSET indicate how to do the adjustment
76 (see thunk_adjust). VIRTUAL_OFFSET can be NULL, but FIXED_OFFSET
77 never is. VIRTUAL_OFFSET is the /index/ into the vtable for this
78 adjusting thunks, we scale it to a byte offset. For covariant
79 thunks VIRTUAL_OFFSET is the virtual binfo. You must post process
80 the returned thunk with finish_thunk. */
82 tree
83 make_thunk (tree function, bool this_adjusting,
84 tree fixed_offset, tree virtual_offset)
86 HOST_WIDE_INT d;
87 tree thunk;
89 gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
90 /* We can have this thunks to covariant thunks, but not vice versa. */
91 gcc_assert (!DECL_THIS_THUNK_P (function));
92 gcc_assert (!DECL_RESULT_THUNK_P (function) || this_adjusting);
94 /* Scale the VIRTUAL_OFFSET to be in terms of bytes. */
95 if (this_adjusting && virtual_offset)
96 virtual_offset
97 = size_binop (MULT_EXPR,
98 virtual_offset,
99 convert (ssizetype,
100 TYPE_SIZE_UNIT (vtable_entry_type)));
102 d = tree_to_shwi (fixed_offset);
104 /* See if we already have the thunk in question. For this_adjusting
105 thunks VIRTUAL_OFFSET will be an INTEGER_CST, for covariant thunks it
106 will be a BINFO. */
107 for (thunk = DECL_THUNKS (function); thunk; thunk = DECL_CHAIN (thunk))
108 if (DECL_THIS_THUNK_P (thunk) == this_adjusting
109 && THUNK_FIXED_OFFSET (thunk) == d
110 && !virtual_offset == !THUNK_VIRTUAL_OFFSET (thunk)
111 && (!virtual_offset
112 || (this_adjusting
113 ? tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk),
114 virtual_offset)
115 : THUNK_VIRTUAL_OFFSET (thunk) == virtual_offset)))
116 return thunk;
118 /* All thunks must be created before FUNCTION is actually emitted;
119 the ABI requires that all thunks be emitted together with the
120 function to which they transfer control. */
121 gcc_assert (!TREE_ASM_WRITTEN (function));
122 /* Likewise, we can only be adding thunks to a function declared in
123 the class currently being laid out. */
124 gcc_assert (TYPE_SIZE (DECL_CONTEXT (function))
125 && TYPE_BEING_DEFINED (DECL_CONTEXT (function)));
127 thunk = build_decl (DECL_SOURCE_LOCATION (function),
128 FUNCTION_DECL, NULL_TREE, TREE_TYPE (function));
129 DECL_LANG_SPECIFIC (thunk) = DECL_LANG_SPECIFIC (function);
130 cxx_dup_lang_specific_decl (thunk);
131 DECL_VIRTUAL_P (thunk) = true;
132 SET_DECL_THUNKS (thunk, NULL_TREE);
134 DECL_CONTEXT (thunk) = DECL_CONTEXT (function);
135 TREE_READONLY (thunk) = TREE_READONLY (function);
136 TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (function);
137 TREE_PUBLIC (thunk) = TREE_PUBLIC (function);
138 SET_DECL_THUNK_P (thunk, this_adjusting);
139 THUNK_TARGET (thunk) = function;
140 THUNK_FIXED_OFFSET (thunk) = d;
141 THUNK_VIRTUAL_OFFSET (thunk) = virtual_offset;
142 THUNK_ALIAS (thunk) = NULL_TREE;
144 DECL_INTERFACE_KNOWN (thunk) = 1;
145 DECL_NOT_REALLY_EXTERN (thunk) = 1;
146 DECL_COMDAT (thunk) = DECL_COMDAT (function);
147 DECL_SAVED_FUNCTION_DATA (thunk) = NULL;
148 /* The thunk itself is not a constructor or destructor, even if
149 the thing it is thunking to is. */
150 DECL_DESTRUCTOR_P (thunk) = 0;
151 DECL_CONSTRUCTOR_P (thunk) = 0;
152 DECL_EXTERNAL (thunk) = 1;
153 DECL_ARTIFICIAL (thunk) = 1;
154 /* The THUNK is not a pending inline, even if the FUNCTION is. */
155 DECL_PENDING_INLINE_P (thunk) = 0;
156 DECL_DECLARED_INLINE_P (thunk) = 0;
157 /* Nor is it a template instantiation. */
158 DECL_USE_TEMPLATE (thunk) = 0;
159 DECL_TEMPLATE_INFO (thunk) = NULL;
161 /* Add it to the list of thunks associated with FUNCTION. */
162 DECL_CHAIN (thunk) = DECL_THUNKS (function);
163 SET_DECL_THUNKS (function, thunk);
165 return thunk;
168 /* Finish THUNK, a thunk decl. */
170 void
171 finish_thunk (tree thunk)
173 tree function, name;
174 tree fixed_offset = ssize_int (THUNK_FIXED_OFFSET (thunk));
175 tree virtual_offset = THUNK_VIRTUAL_OFFSET (thunk);
177 gcc_assert (!DECL_NAME (thunk) && DECL_THUNK_P (thunk));
178 if (virtual_offset && DECL_RESULT_THUNK_P (thunk))
179 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
180 function = THUNK_TARGET (thunk);
181 name = mangle_thunk (function, DECL_THIS_THUNK_P (thunk),
182 fixed_offset, virtual_offset);
184 /* We can end up with declarations of (logically) different
185 covariant thunks, that do identical adjustments. The two thunks
186 will be adjusting between within different hierarchies, which
187 happen to have the same layout. We must nullify one of them to
188 refer to the other. */
189 if (DECL_RESULT_THUNK_P (thunk))
191 tree cov_probe;
193 for (cov_probe = DECL_THUNKS (function);
194 cov_probe; cov_probe = DECL_CHAIN (cov_probe))
195 if (DECL_NAME (cov_probe) == name)
197 gcc_assert (!DECL_THUNKS (thunk));
198 THUNK_ALIAS (thunk) = (THUNK_ALIAS (cov_probe)
199 ? THUNK_ALIAS (cov_probe) : cov_probe);
200 break;
204 DECL_NAME (thunk) = name;
205 SET_DECL_ASSEMBLER_NAME (thunk, name);
208 static GTY (()) int thunk_labelno;
210 /* Create a static alias to target. */
212 tree
213 make_alias_for (tree target, tree newid)
215 tree alias = build_decl (DECL_SOURCE_LOCATION (target),
216 TREE_CODE (target), newid, TREE_TYPE (target));
217 DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (target);
218 cxx_dup_lang_specific_decl (alias);
219 DECL_CONTEXT (alias) = NULL;
220 TREE_READONLY (alias) = TREE_READONLY (target);
221 TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (target);
222 TREE_PUBLIC (alias) = 0;
223 DECL_INTERFACE_KNOWN (alias) = 1;
224 if (DECL_LANG_SPECIFIC (alias))
226 DECL_NOT_REALLY_EXTERN (alias) = 1;
227 DECL_USE_TEMPLATE (alias) = 0;
228 DECL_TEMPLATE_INFO (alias) = NULL;
230 DECL_EXTERNAL (alias) = 0;
231 DECL_ARTIFICIAL (alias) = 1;
232 DECL_TEMPLATE_INSTANTIATED (alias) = 0;
233 if (TREE_CODE (alias) == FUNCTION_DECL)
235 DECL_SAVED_FUNCTION_DATA (alias) = NULL;
236 DECL_DESTRUCTOR_P (alias) = 0;
237 DECL_CONSTRUCTOR_P (alias) = 0;
238 DECL_PENDING_INLINE_P (alias) = 0;
239 DECL_DECLARED_INLINE_P (alias) = 0;
240 DECL_INITIAL (alias) = error_mark_node;
241 DECL_ARGUMENTS (alias) = copy_list (DECL_ARGUMENTS (target));
243 else
244 TREE_STATIC (alias) = 1;
245 TREE_ADDRESSABLE (alias) = 1;
246 TREE_USED (alias) = 1;
247 SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
248 return alias;
251 static tree
252 make_alias_for_thunk (tree function)
254 tree alias;
255 char buf[256];
257 targetm.asm_out.generate_internal_label (buf, "LTHUNK", thunk_labelno);
258 thunk_labelno++;
260 alias = make_alias_for (function, get_identifier (buf));
262 if (!flag_syntax_only)
264 struct cgraph_node *funcn, *aliasn;
265 funcn = cgraph_node::get (function);
266 gcc_checking_assert (funcn);
267 aliasn = cgraph_node::create_same_body_alias (alias, function);
268 DECL_ASSEMBLER_NAME (function);
269 gcc_assert (aliasn != NULL);
272 return alias;
275 /* Emit the definition of a C++ multiple inheritance or covariant
276 return vtable thunk. If EMIT_P is nonzero, the thunk is emitted
277 immediately. */
279 void
280 use_thunk (tree thunk_fndecl, bool emit_p)
282 tree a, t, function, alias;
283 tree virtual_offset;
284 HOST_WIDE_INT fixed_offset, virtual_value;
285 bool this_adjusting = DECL_THIS_THUNK_P (thunk_fndecl);
286 struct cgraph_node *funcn, *thunk_node;
288 /* We should have called finish_thunk to give it a name. */
289 gcc_assert (DECL_NAME (thunk_fndecl));
291 /* We should never be using an alias, always refer to the
292 aliased thunk. */
293 gcc_assert (!THUNK_ALIAS (thunk_fndecl));
295 if (TREE_ASM_WRITTEN (thunk_fndecl))
296 return;
298 function = THUNK_TARGET (thunk_fndecl);
299 if (DECL_RESULT (thunk_fndecl))
300 /* We already turned this thunk into an ordinary function.
301 There's no need to process this thunk again. */
302 return;
304 if (DECL_THUNK_P (function))
305 /* The target is itself a thunk, process it now. */
306 use_thunk (function, emit_p);
308 /* Thunks are always addressable; they only appear in vtables. */
309 TREE_ADDRESSABLE (thunk_fndecl) = 1;
311 /* Figure out what function is being thunked to. It's referenced in
312 this translation unit. */
313 TREE_ADDRESSABLE (function) = 1;
314 mark_used (function);
315 if (!emit_p)
316 return;
318 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function))
319 alias = make_alias_for_thunk (function);
320 else
321 alias = function;
323 fixed_offset = THUNK_FIXED_OFFSET (thunk_fndecl);
324 virtual_offset = THUNK_VIRTUAL_OFFSET (thunk_fndecl);
326 if (virtual_offset)
328 if (!this_adjusting)
329 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
330 virtual_value = tree_to_shwi (virtual_offset);
331 gcc_assert (virtual_value);
333 else
334 virtual_value = 0;
336 /* And, if we need to emit the thunk, it's used. */
337 mark_used (thunk_fndecl);
338 /* This thunk is actually defined. */
339 DECL_EXTERNAL (thunk_fndecl) = 0;
340 /* The linkage of the function may have changed. FIXME in linkage
341 rewrite. */
342 gcc_assert (DECL_INTERFACE_KNOWN (function));
343 TREE_PUBLIC (thunk_fndecl) = TREE_PUBLIC (function);
344 DECL_VISIBILITY (thunk_fndecl) = DECL_VISIBILITY (function);
345 DECL_VISIBILITY_SPECIFIED (thunk_fndecl)
346 = DECL_VISIBILITY_SPECIFIED (function);
347 DECL_COMDAT (thunk_fndecl) = DECL_COMDAT (function);
348 DECL_WEAK (thunk_fndecl) = DECL_WEAK (function);
350 if (flag_syntax_only)
352 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
353 return;
356 push_to_top_level ();
358 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function)
359 && targetm_common.have_named_sections)
361 tree fn = function;
362 struct symtab_node *symbol;
364 if ((symbol = symtab_node::get (function))
365 && symbol->alias)
367 if (symbol->analyzed)
368 fn = symtab_node::get (function)->ultimate_alias_target ()->decl;
369 else
370 fn = symtab_node::get (function)->alias_target;
372 resolve_unique_section (fn, 0, flag_function_sections);
374 if (DECL_SECTION_NAME (fn) != NULL && DECL_ONE_ONLY (fn))
376 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
378 /* Output the thunk into the same section as function. */
379 set_decl_section_name (thunk_fndecl, DECL_SECTION_NAME (fn));
380 symtab_node::get (thunk_fndecl)->implicit_section
381 = symtab_node::get (fn)->implicit_section;
385 /* Set up cloned argument trees for the thunk. */
386 t = NULL_TREE;
387 for (a = DECL_ARGUMENTS (function); a; a = DECL_CHAIN (a))
389 tree x = copy_node (a);
390 DECL_CHAIN (x) = t;
391 DECL_CONTEXT (x) = thunk_fndecl;
392 SET_DECL_RTL (x, NULL);
393 DECL_HAS_VALUE_EXPR_P (x) = 0;
394 TREE_ADDRESSABLE (x) = 0;
395 t = x;
397 a = nreverse (t);
398 DECL_ARGUMENTS (thunk_fndecl) = a;
399 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
400 funcn = cgraph_node::get (function);
401 gcc_checking_assert (funcn);
402 thunk_node = funcn->create_thunk (thunk_fndecl, function,
403 this_adjusting, fixed_offset, virtual_value,
404 virtual_offset, alias);
405 if (DECL_ONE_ONLY (function))
406 thunk_node->add_to_same_comdat_group (funcn);
408 pop_from_top_level ();
411 /* Code for synthesizing methods which have default semantics defined. */
413 /* True iff CTYPE has a trivial SFK. */
415 static bool
416 type_has_trivial_fn (tree ctype, special_function_kind sfk)
418 switch (sfk)
420 case sfk_constructor:
421 return !TYPE_HAS_COMPLEX_DFLT (ctype);
422 case sfk_copy_constructor:
423 return !TYPE_HAS_COMPLEX_COPY_CTOR (ctype);
424 case sfk_move_constructor:
425 return !TYPE_HAS_COMPLEX_MOVE_CTOR (ctype);
426 case sfk_copy_assignment:
427 return !TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype);
428 case sfk_move_assignment:
429 return !TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype);
430 case sfk_destructor:
431 return !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype);
432 case sfk_inheriting_constructor:
433 return false;
434 default:
435 gcc_unreachable ();
439 /* Note that CTYPE has a non-trivial SFK even though we previously thought
440 it was trivial. */
442 static void
443 type_set_nontrivial_flag (tree ctype, special_function_kind sfk)
445 switch (sfk)
447 case sfk_constructor:
448 TYPE_HAS_COMPLEX_DFLT (ctype) = true;
449 return;
450 case sfk_copy_constructor:
451 TYPE_HAS_COMPLEX_COPY_CTOR (ctype) = true;
452 return;
453 case sfk_move_constructor:
454 TYPE_HAS_COMPLEX_MOVE_CTOR (ctype) = true;
455 return;
456 case sfk_copy_assignment:
457 TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype) = true;
458 return;
459 case sfk_move_assignment:
460 TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype) = true;
461 return;
462 case sfk_destructor:
463 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
464 return;
465 case sfk_inheriting_constructor:
466 default:
467 gcc_unreachable ();
471 /* True iff FN is a trivial defaulted member function ([cd]tor, op=). */
473 bool
474 trivial_fn_p (tree fn)
476 if (TREE_CODE (fn) == TEMPLATE_DECL)
477 return false;
478 if (!DECL_DEFAULTED_FN (fn))
479 return false;
481 /* If fn is a clone, get the primary variant. */
482 if (tree prim = DECL_CLONED_FUNCTION (fn))
483 fn = prim;
484 return type_has_trivial_fn (DECL_CONTEXT (fn), special_function_p (fn));
487 /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO
488 given the parameter or parameters PARM, possibly inherited constructor
489 base INH, or move flag MOVE_P. */
491 static tree
492 add_one_base_init (tree binfo, tree parm, bool move_p, tree inh,
493 tree member_init_list)
495 tree init;
496 if (inh)
498 /* An inheriting constructor only has a mem-initializer for
499 the base it inherits from. */
500 if (BINFO_TYPE (binfo) != inh)
501 return member_init_list;
503 tree *p = &init;
504 init = NULL_TREE;
505 for (; parm; parm = DECL_CHAIN (parm))
507 tree exp = convert_from_reference (parm);
508 if (TREE_CODE (TREE_TYPE (parm)) != REFERENCE_TYPE
509 || TYPE_REF_IS_RVALUE (TREE_TYPE (parm)))
510 exp = move (exp);
511 *p = build_tree_list (NULL_TREE, exp);
512 p = &TREE_CHAIN (*p);
515 else
517 init = build_base_path (PLUS_EXPR, parm, binfo, 1,
518 tf_warning_or_error);
519 if (move_p)
520 init = move (init);
521 init = build_tree_list (NULL_TREE, init);
523 return tree_cons (binfo, init, member_init_list);
526 /* Generate code for default X(X&) or X(X&&) constructor or an inheriting
527 constructor. */
529 static void
530 do_build_copy_constructor (tree fndecl)
532 tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
533 bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
534 bool trivial = trivial_fn_p (fndecl);
535 tree inh = DECL_INHERITED_CTOR_BASE (fndecl);
537 if (!inh)
538 parm = convert_from_reference (parm);
540 if (trivial
541 && is_empty_class (current_class_type))
542 /* Don't copy the padding byte; it might not have been allocated
543 if *this is a base subobject. */;
544 else if (trivial)
546 tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm);
547 finish_expr_stmt (t);
549 else
551 tree fields = TYPE_FIELDS (current_class_type);
552 tree member_init_list = NULL_TREE;
553 int cvquals = cp_type_quals (TREE_TYPE (parm));
554 int i;
555 tree binfo, base_binfo;
556 tree init;
557 vec<tree, va_gc> *vbases;
559 /* Initialize all the base-classes with the parameter converted
560 to their type so that we get their copy constructor and not
561 another constructor that takes current_class_type. We must
562 deal with the binfo's directly as a direct base might be
563 inaccessible due to ambiguity. */
564 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
565 vec_safe_iterate (vbases, i, &binfo); i++)
567 member_init_list = add_one_base_init (binfo, parm, move_p, inh,
568 member_init_list);
571 for (binfo = TYPE_BINFO (current_class_type), i = 0;
572 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
574 if (BINFO_VIRTUAL_P (base_binfo))
575 continue;
576 member_init_list = add_one_base_init (base_binfo, parm, move_p,
577 inh, member_init_list);
580 for (; fields; fields = DECL_CHAIN (fields))
582 tree field = fields;
583 tree expr_type;
585 if (TREE_CODE (field) != FIELD_DECL)
586 continue;
587 if (inh)
588 continue;
590 expr_type = TREE_TYPE (field);
591 if (DECL_NAME (field))
593 if (VFIELD_NAME_P (DECL_NAME (field)))
594 continue;
596 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
597 /* Just use the field; anonymous types can't have
598 nontrivial copy ctors or assignment ops or this
599 function would be deleted. */;
600 else
601 continue;
603 /* Compute the type of "init->field". If the copy-constructor
604 parameter is, for example, "const S&", and the type of
605 the field is "T", then the type will usually be "const
606 T". (There are no cv-qualified variants of reference
607 types.) */
608 if (TREE_CODE (expr_type) != REFERENCE_TYPE)
610 int quals = cvquals;
612 if (DECL_MUTABLE_P (field))
613 quals &= ~TYPE_QUAL_CONST;
614 quals |= cp_type_quals (expr_type);
615 expr_type = cp_build_qualified_type (expr_type, quals);
618 init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
619 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
620 /* 'move' breaks bit-fields, and has no effect for scalars. */
621 && !scalarish_type_p (expr_type))
622 init = move (init);
623 init = build_tree_list (NULL_TREE, init);
625 member_init_list = tree_cons (field, init, member_init_list);
627 finish_mem_initializers (member_init_list);
631 static void
632 do_build_copy_assign (tree fndecl)
634 tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl));
635 tree compound_stmt;
636 bool move_p = move_fn_p (fndecl);
637 bool trivial = trivial_fn_p (fndecl);
638 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
640 compound_stmt = begin_compound_stmt (0);
641 parm = convert_from_reference (parm);
643 if (trivial
644 && is_empty_class (current_class_type))
645 /* Don't copy the padding byte; it might not have been allocated
646 if *this is a base subobject. */;
647 else if (trivial)
649 tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
650 finish_expr_stmt (t);
652 else
654 tree fields;
655 int cvquals = cp_type_quals (TREE_TYPE (parm));
656 int i;
657 tree binfo, base_binfo;
659 /* Assign to each of the direct base classes. */
660 for (binfo = TYPE_BINFO (current_class_type), i = 0;
661 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
663 tree converted_parm;
664 vec<tree, va_gc> *parmvec;
666 /* We must convert PARM directly to the base class
667 explicitly since the base class may be ambiguous. */
668 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
669 tf_warning_or_error);
670 if (move_p)
671 converted_parm = move (converted_parm);
672 /* Call the base class assignment operator. */
673 parmvec = make_tree_vector_single (converted_parm);
674 finish_expr_stmt
675 (build_special_member_call (current_class_ref,
676 ansi_assopname (NOP_EXPR),
677 &parmvec,
678 base_binfo,
679 flags,
680 tf_warning_or_error));
681 release_tree_vector (parmvec);
684 /* Assign to each of the non-static data members. */
685 for (fields = TYPE_FIELDS (current_class_type);
686 fields;
687 fields = DECL_CHAIN (fields))
689 tree comp = current_class_ref;
690 tree init = parm;
691 tree field = fields;
692 tree expr_type;
693 int quals;
695 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
696 continue;
698 expr_type = TREE_TYPE (field);
700 if (CP_TYPE_CONST_P (expr_type))
702 error ("non-static const member %q#D, can%'t use default "
703 "assignment operator", field);
704 continue;
706 else if (TREE_CODE (expr_type) == REFERENCE_TYPE)
708 error ("non-static reference member %q#D, can%'t use "
709 "default assignment operator", field);
710 continue;
713 if (DECL_NAME (field))
715 if (VFIELD_NAME_P (DECL_NAME (field)))
716 continue;
718 else if (ANON_AGGR_TYPE_P (expr_type)
719 && TYPE_FIELDS (expr_type) != NULL_TREE)
720 /* Just use the field; anonymous types can't have
721 nontrivial copy ctors or assignment ops or this
722 function would be deleted. */;
723 else
724 continue;
726 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
728 /* Compute the type of init->field */
729 quals = cvquals;
730 if (DECL_MUTABLE_P (field))
731 quals &= ~TYPE_QUAL_CONST;
732 expr_type = cp_build_qualified_type (expr_type, quals);
734 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
735 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
736 /* 'move' breaks bit-fields, and has no effect for scalars. */
737 && !scalarish_type_p (expr_type))
738 init = move (init);
740 if (DECL_NAME (field))
741 init = cp_build_modify_expr (comp, NOP_EXPR, init,
742 tf_warning_or_error);
743 else
744 init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
745 finish_expr_stmt (init);
748 finish_return_stmt (current_class_ref);
749 finish_compound_stmt (compound_stmt);
752 /* Synthesize FNDECL, a non-static member function. */
754 void
755 synthesize_method (tree fndecl)
757 bool nested = (current_function_decl != NULL_TREE);
758 tree context = decl_function_context (fndecl);
759 bool need_body = true;
760 tree stmt;
761 location_t save_input_location = input_location;
762 int error_count = errorcount;
763 int warning_count = warningcount + werrorcount;
765 /* Reset the source location, we might have been previously
766 deferred, and thus have saved where we were first needed. */
767 DECL_SOURCE_LOCATION (fndecl)
768 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
770 /* If we've been asked to synthesize a clone, just synthesize the
771 cloned function instead. Doing so will automatically fill in the
772 body for the clone. */
773 if (DECL_CLONED_FUNCTION_P (fndecl))
774 fndecl = DECL_CLONED_FUNCTION (fndecl);
776 /* We may be in the middle of deferred access check. Disable
777 it now. */
778 push_deferring_access_checks (dk_no_deferred);
780 if (! context)
781 push_to_top_level ();
782 else if (nested)
783 push_function_context ();
785 input_location = DECL_SOURCE_LOCATION (fndecl);
787 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
788 stmt = begin_function_body ();
790 if (DECL_OVERLOADED_OPERATOR_P (fndecl) == NOP_EXPR)
792 do_build_copy_assign (fndecl);
793 need_body = false;
795 else if (DECL_CONSTRUCTOR_P (fndecl))
797 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
798 if (arg_chain != void_list_node)
799 do_build_copy_constructor (fndecl);
800 else
801 finish_mem_initializers (NULL_TREE);
804 /* If we haven't yet generated the body of the function, just
805 generate an empty compound statement. */
806 if (need_body)
808 tree compound_stmt;
809 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
810 finish_compound_stmt (compound_stmt);
813 finish_function_body (stmt);
814 expand_or_defer_fn (finish_function (0));
816 input_location = save_input_location;
818 if (! context)
819 pop_from_top_level ();
820 else if (nested)
821 pop_function_context ();
823 pop_deferring_access_checks ();
825 if (error_count != errorcount || warning_count != warningcount + werrorcount)
826 inform (input_location, "synthesized method %qD first required here ",
827 fndecl);
830 /* Build a reference to type TYPE with cv-quals QUALS, which is an
831 rvalue if RVALUE is true. */
833 static tree
834 build_stub_type (tree type, int quals, bool rvalue)
836 tree argtype = cp_build_qualified_type (type, quals);
837 return cp_build_reference_type (argtype, rvalue);
840 /* Build a dummy glvalue from dereferencing a dummy reference of type
841 REFTYPE. */
843 static tree
844 build_stub_object (tree reftype)
846 if (TREE_CODE (reftype) != REFERENCE_TYPE)
847 reftype = cp_build_reference_type (reftype, /*rval*/true);
848 tree stub = build1 (CONVERT_EXPR, reftype, integer_one_node);
849 return convert_from_reference (stub);
852 /* Determine which function will be called when looking up NAME in TYPE,
853 called with a single ARGTYPE argument, or no argument if ARGTYPE is
854 null. FLAGS and COMPLAIN are as for build_new_method_call.
856 Returns a FUNCTION_DECL if all is well.
857 Returns NULL_TREE if overload resolution failed.
858 Returns error_mark_node if the chosen function cannot be called. */
860 static tree
861 locate_fn_flags (tree type, tree name, tree argtype, int flags,
862 tsubst_flags_t complain)
864 tree ob, fn, fns, binfo, rval;
865 vec<tree, va_gc> *args;
867 if (TYPE_P (type))
868 binfo = TYPE_BINFO (type);
869 else
871 binfo = type;
872 type = BINFO_TYPE (binfo);
875 ob = build_stub_object (cp_build_reference_type (type, false));
876 args = make_tree_vector ();
877 if (argtype)
879 if (TREE_CODE (argtype) == TREE_LIST)
881 for (tree elt = argtype; elt != void_list_node;
882 elt = TREE_CHAIN (elt))
884 tree type = TREE_VALUE (elt);
885 tree arg = build_stub_object (type);
886 vec_safe_push (args, arg);
889 else
891 tree arg = build_stub_object (argtype);
892 args->quick_push (arg);
896 fns = lookup_fnfields (binfo, name, 0);
897 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
899 release_tree_vector (args);
900 if (fn && rval == error_mark_node)
901 return rval;
902 else
903 return fn;
906 /* Locate the dtor of TYPE. */
908 tree
909 get_dtor (tree type, tsubst_flags_t complain)
911 tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
912 LOOKUP_NORMAL, complain);
913 if (fn == error_mark_node)
914 return NULL_TREE;
915 return fn;
918 /* Locate the default ctor of TYPE. */
920 tree
921 locate_ctor (tree type)
923 tree fn;
925 push_deferring_access_checks (dk_no_check);
926 fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
927 LOOKUP_SPECULATIVE, tf_none);
928 pop_deferring_access_checks ();
929 if (fn == error_mark_node)
930 return NULL_TREE;
931 return fn;
934 /* Likewise, but give any appropriate errors. */
936 tree
937 get_default_ctor (tree type)
939 tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
940 LOOKUP_NORMAL, tf_warning_or_error);
941 if (fn == error_mark_node)
942 return NULL_TREE;
943 return fn;
946 /* Locate the copy ctor of TYPE. */
948 tree
949 get_copy_ctor (tree type, tsubst_flags_t complain)
951 int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
952 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
953 tree argtype = build_stub_type (type, quals, false);
954 tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
955 LOOKUP_NORMAL, complain);
956 if (fn == error_mark_node)
957 return NULL_TREE;
958 return fn;
961 /* Locate the copy assignment operator of TYPE. */
963 tree
964 get_copy_assign (tree type)
966 int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
967 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
968 tree argtype = build_stub_type (type, quals, false);
969 tree fn = locate_fn_flags (type, ansi_assopname (NOP_EXPR), argtype,
970 LOOKUP_NORMAL, tf_warning_or_error);
971 if (fn == error_mark_node)
972 return NULL_TREE;
973 return fn;
976 /* Locate the inherited constructor of constructor CTOR. */
978 tree
979 get_inherited_ctor (tree ctor)
981 gcc_assert (DECL_INHERITED_CTOR_BASE (ctor));
983 push_deferring_access_checks (dk_no_check);
984 tree fn = locate_fn_flags (DECL_INHERITED_CTOR_BASE (ctor),
985 complete_ctor_identifier,
986 FUNCTION_FIRST_USER_PARMTYPE (ctor),
987 LOOKUP_NORMAL|LOOKUP_SPECULATIVE,
988 tf_none);
989 pop_deferring_access_checks ();
990 if (fn == error_mark_node)
991 return NULL_TREE;
992 return fn;
995 /* walk_tree helper function for is_trivially_xible. If *TP is a call,
996 return it if it calls something other than a trivial special member
997 function. */
999 static tree
1000 check_nontriv (tree *tp, int *, void *)
1002 tree fn;
1003 if (TREE_CODE (*tp) == CALL_EXPR)
1004 fn = CALL_EXPR_FN (*tp);
1005 else if (TREE_CODE (*tp) == AGGR_INIT_EXPR)
1006 fn = AGGR_INIT_EXPR_FN (*tp);
1007 else
1008 return NULL_TREE;
1010 if (TREE_CODE (fn) == ADDR_EXPR)
1011 fn = TREE_OPERAND (fn, 0);
1013 if (TREE_CODE (fn) != FUNCTION_DECL
1014 || !trivial_fn_p (fn))
1015 return fn;
1016 return NULL_TREE;
1019 /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */
1021 static tree
1022 assignable_expr (tree to, tree from)
1024 ++cp_unevaluated_operand;
1025 to = build_stub_object (to);
1026 from = build_stub_object (from);
1027 tree r = cp_build_modify_expr (to, NOP_EXPR, from, tf_none);
1028 --cp_unevaluated_operand;
1029 return r;
1032 /* The predicate condition for a template specialization
1033 is_constructible<T, Args...> shall be satisfied if and only if the
1034 following variable definition would be well-formed for some invented
1035 variable t: T t(create<Args>()...);
1037 Return something equivalent in well-formedness and triviality. */
1039 static tree
1040 constructible_expr (tree to, tree from)
1042 tree expr;
1043 if (CLASS_TYPE_P (to))
1045 tree ctype = to;
1046 vec<tree, va_gc> *args = NULL;
1047 if (TREE_CODE (to) != REFERENCE_TYPE)
1048 to = cp_build_reference_type (to, /*rval*/false);
1049 tree ob = build_stub_object (to);
1050 for (; from; from = TREE_CHAIN (from))
1051 vec_safe_push (args, build_stub_object (TREE_VALUE (from)));
1052 expr = build_special_member_call (ob, complete_ctor_identifier, &args,
1053 ctype, LOOKUP_NORMAL, tf_none);
1054 if (expr == error_mark_node)
1055 return error_mark_node;
1056 /* The current state of the standard vis-a-vis LWG 2116 is that
1057 is_*constructible involves destruction as well. */
1058 if (type_build_dtor_call (ctype))
1060 tree dtor = build_special_member_call (ob, complete_dtor_identifier,
1061 NULL, ctype, LOOKUP_NORMAL,
1062 tf_none);
1063 if (dtor == error_mark_node)
1064 return error_mark_node;
1065 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
1066 expr = build2 (COMPOUND_EXPR, void_type_node, expr, dtor);
1069 else
1071 if (from == NULL_TREE)
1072 return build_value_init (to, tf_none);
1073 else if (TREE_CHAIN (from))
1074 return error_mark_node; // too many initializers
1075 from = build_stub_object (TREE_VALUE (from));
1076 expr = perform_direct_initialization_if_possible (to, from,
1077 /*cast*/false,
1078 tf_none);
1080 return expr;
1083 /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or
1084 constructible (otherwise) from FROM, which is a single type for
1085 assignment or a list of types for construction. */
1087 bool
1088 is_trivially_xible (enum tree_code code, tree to, tree from)
1090 tree expr;
1091 if (code == MODIFY_EXPR)
1092 expr = assignable_expr (to, from);
1093 else if (from && TREE_CHAIN (from))
1094 return false; // only 0- and 1-argument ctors can be trivial
1095 else
1096 expr = constructible_expr (to, from);
1098 if (expr == error_mark_node)
1099 return false;
1100 tree nt = cp_walk_tree_without_duplicates (&expr, check_nontriv, NULL);
1101 return !nt;
1104 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
1105 DELETED_P or give an error message MSG with argument ARG. */
1107 static void
1108 process_subob_fn (tree fn, tree *spec_p, bool *trivial_p,
1109 bool *deleted_p, bool *constexpr_p,
1110 bool diag, tree arg)
1112 if (!fn || fn == error_mark_node)
1113 goto bad;
1115 if (spec_p)
1117 maybe_instantiate_noexcept (fn);
1118 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
1119 *spec_p = merge_exception_specifiers (*spec_p, raises);
1122 if (!trivial_fn_p (fn))
1124 if (trivial_p)
1125 *trivial_p = false;
1126 if (TREE_CODE (arg) == FIELD_DECL
1127 && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
1129 if (deleted_p)
1130 *deleted_p = true;
1131 if (diag)
1132 error ("union member %q+D with non-trivial %qD", arg, fn);
1136 if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
1138 *constexpr_p = false;
1139 if (diag)
1141 inform (DECL_SOURCE_LOCATION (fn),
1142 "defaulted constructor calls non-constexpr %qD", fn);
1143 explain_invalid_constexpr_fn (fn);
1147 return;
1149 bad:
1150 if (deleted_p)
1151 *deleted_p = true;
1154 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
1155 aggregates. */
1157 static void
1158 walk_field_subobs (tree fields, tree fnname, special_function_kind sfk,
1159 int quals, bool copy_arg_p, bool move_p,
1160 bool assign_p, tree *spec_p, bool *trivial_p,
1161 bool *deleted_p, bool *constexpr_p,
1162 bool diag, int flags, tsubst_flags_t complain)
1164 tree field;
1165 for (field = fields; field; field = DECL_CHAIN (field))
1167 tree mem_type, argtype, rval;
1169 if (TREE_CODE (field) != FIELD_DECL
1170 || DECL_ARTIFICIAL (field))
1171 continue;
1173 mem_type = strip_array_types (TREE_TYPE (field));
1174 if (assign_p)
1176 bool bad = true;
1177 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
1179 if (diag)
1180 error ("non-static const member %q#D, can%'t use default "
1181 "assignment operator", field);
1183 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1185 if (diag)
1186 error ("non-static reference member %q#D, can%'t use "
1187 "default assignment operator", field);
1189 else
1190 bad = false;
1192 if (bad && deleted_p)
1193 *deleted_p = true;
1195 else if (sfk == sfk_constructor)
1197 bool bad;
1199 if (DECL_INITIAL (field))
1201 if (diag && DECL_INITIAL (field) == error_mark_node)
1202 inform (DECL_SOURCE_LOCATION (field),
1203 "initializer for %q#D is invalid", field);
1204 if (trivial_p)
1205 *trivial_p = false;
1206 /* Core 1351: If the field has an NSDMI that could throw, the
1207 default constructor is noexcept(false). */
1208 if (spec_p)
1210 tree nsdmi = get_nsdmi (field, /*ctor*/false);
1211 if (!expr_noexcept_p (nsdmi, complain))
1212 *spec_p = noexcept_false_spec;
1214 /* Don't do the normal processing. */
1215 continue;
1218 bad = false;
1219 if (CP_TYPE_CONST_P (mem_type)
1220 && default_init_uninitialized_part (mem_type))
1222 if (diag)
1224 error ("uninitialized const member in %q#T",
1225 current_class_type);
1226 inform (DECL_SOURCE_LOCATION (field),
1227 "%q#D should be initialized", field);
1229 bad = true;
1231 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1233 if (diag)
1235 error ("uninitialized reference member in %q#T",
1236 current_class_type);
1237 inform (DECL_SOURCE_LOCATION (field),
1238 "%q#D should be initialized", field);
1240 bad = true;
1243 if (bad && deleted_p)
1244 *deleted_p = true;
1246 /* For an implicitly-defined default constructor to be constexpr,
1247 every member must have a user-provided default constructor or
1248 an explicit initializer. */
1249 if (constexpr_p && !CLASS_TYPE_P (mem_type)
1250 && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE)
1252 *constexpr_p = false;
1253 if (diag)
1254 inform (DECL_SOURCE_LOCATION (field),
1255 "defaulted default constructor does not "
1256 "initialize %q#D", field);
1259 else if (sfk == sfk_copy_constructor)
1261 /* 12.8p11b5 */
1262 if (TREE_CODE (mem_type) == REFERENCE_TYPE
1263 && TYPE_REF_IS_RVALUE (mem_type))
1265 if (diag)
1266 error ("copying non-static data member %q#D of rvalue "
1267 "reference type", field);
1268 if (deleted_p)
1269 *deleted_p = true;
1273 if (!CLASS_TYPE_P (mem_type))
1274 continue;
1276 if (ANON_AGGR_TYPE_P (mem_type))
1278 walk_field_subobs (TYPE_FIELDS (mem_type), fnname, sfk, quals,
1279 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1280 deleted_p, constexpr_p,
1281 diag, flags, complain);
1282 continue;
1285 if (copy_arg_p)
1287 int mem_quals = cp_type_quals (mem_type) | quals;
1288 if (DECL_MUTABLE_P (field))
1289 mem_quals &= ~TYPE_QUAL_CONST;
1290 argtype = build_stub_type (mem_type, mem_quals, move_p);
1292 else
1293 argtype = NULL_TREE;
1295 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
1297 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1298 constexpr_p, diag, field);
1302 /* The caller wants to generate an implicit declaration of SFK for CTYPE
1303 which is const if relevant and CONST_P is set. If spec_p, trivial_p and
1304 deleted_p are non-null, set their referent appropriately. If diag is
1305 true, we're either being called from maybe_explain_implicit_delete to
1306 give errors, or if constexpr_p is non-null, from
1307 explain_invalid_constexpr_fn. */
1309 static void
1310 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
1311 tree *spec_p, bool *trivial_p, bool *deleted_p,
1312 bool *constexpr_p, bool diag,
1313 tree inherited_base, tree inherited_parms)
1315 tree binfo, base_binfo, scope, fnname, rval, argtype;
1316 bool move_p, copy_arg_p, assign_p, expected_trivial, check_vdtor;
1317 vec<tree, va_gc> *vbases;
1318 int i, quals, flags;
1319 tsubst_flags_t complain;
1320 bool ctor_p;
1322 if (spec_p)
1323 *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec);
1325 if (deleted_p)
1327 /* "The closure type associated with a lambda-expression has a deleted
1328 default constructor and a deleted copy assignment operator."
1329 This is diagnosed in maybe_explain_implicit_delete. */
1330 if (LAMBDA_TYPE_P (ctype)
1331 && (sfk == sfk_constructor
1332 || sfk == sfk_copy_assignment))
1334 *deleted_p = true;
1335 return;
1338 *deleted_p = false;
1341 ctor_p = false;
1342 assign_p = false;
1343 check_vdtor = false;
1344 switch (sfk)
1346 case sfk_move_assignment:
1347 case sfk_copy_assignment:
1348 assign_p = true;
1349 fnname = ansi_assopname (NOP_EXPR);
1350 break;
1352 case sfk_destructor:
1353 check_vdtor = true;
1354 /* The synthesized method will call base dtors, but check complete
1355 here to avoid having to deal with VTT. */
1356 fnname = complete_dtor_identifier;
1357 break;
1359 case sfk_constructor:
1360 case sfk_move_constructor:
1361 case sfk_copy_constructor:
1362 case sfk_inheriting_constructor:
1363 ctor_p = true;
1364 fnname = complete_ctor_identifier;
1365 break;
1367 default:
1368 gcc_unreachable ();
1371 gcc_assert ((sfk == sfk_inheriting_constructor)
1372 == (inherited_base != NULL_TREE));
1374 /* If that user-written default constructor would satisfy the
1375 requirements of a constexpr constructor (7.1.5), the
1376 implicitly-defined default constructor is constexpr. */
1377 if (constexpr_p)
1378 *constexpr_p = ctor_p;
1380 move_p = false;
1381 switch (sfk)
1383 case sfk_constructor:
1384 case sfk_destructor:
1385 case sfk_inheriting_constructor:
1386 copy_arg_p = false;
1387 break;
1389 case sfk_move_constructor:
1390 case sfk_move_assignment:
1391 move_p = true;
1392 case sfk_copy_constructor:
1393 case sfk_copy_assignment:
1394 copy_arg_p = true;
1395 break;
1397 default:
1398 gcc_unreachable ();
1401 expected_trivial = type_has_trivial_fn (ctype, sfk);
1402 if (trivial_p)
1403 *trivial_p = expected_trivial;
1405 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
1406 class versions and other properties of the type. But a subobject
1407 class can be trivially copyable and yet have overload resolution
1408 choose a template constructor for initialization, depending on
1409 rvalueness and cv-quals. And furthermore, a member in a base might
1410 be trivial but deleted or otherwise not callable. So we can't exit
1411 early in C++0x. The same considerations apply in C++98/03, but
1412 there the definition of triviality does not consider overload
1413 resolution, so a constructor can be trivial even if it would otherwise
1414 call a non-trivial constructor. */
1415 if (expected_trivial
1416 && (!copy_arg_p || cxx_dialect < cxx11))
1418 if (constexpr_p && sfk == sfk_constructor)
1420 bool cx = trivial_default_constructor_is_constexpr (ctype);
1421 *constexpr_p = cx;
1422 if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
1423 /* A trivial constructor doesn't have any NSDMI. */
1424 inform (input_location, "defaulted default constructor does "
1425 "not initialize any non-static data member");
1427 if (!diag && cxx_dialect < cxx11)
1428 return;
1431 ++cp_unevaluated_operand;
1432 ++c_inhibit_evaluation_warnings;
1433 push_deferring_access_checks (dk_no_deferred);
1435 scope = push_scope (ctype);
1437 flags = LOOKUP_NORMAL|LOOKUP_SPECULATIVE;
1438 if (!inherited_base)
1439 flags |= LOOKUP_DEFAULTED;
1441 complain = diag ? tf_warning_or_error : tf_none;
1443 if (const_p)
1444 quals = TYPE_QUAL_CONST;
1445 else
1446 quals = TYPE_UNQUALIFIED;
1447 argtype = NULL_TREE;
1449 for (binfo = TYPE_BINFO (ctype), i = 0;
1450 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
1452 tree basetype = BINFO_TYPE (base_binfo);
1454 if (!assign_p && BINFO_VIRTUAL_P (base_binfo))
1455 /* We'll handle virtual bases below. */
1456 continue;
1458 if (copy_arg_p)
1459 argtype = build_stub_type (basetype, quals, move_p);
1460 else if (basetype == inherited_base)
1461 argtype = inherited_parms;
1462 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1463 if (inherited_base)
1464 argtype = NULL_TREE;
1466 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1467 constexpr_p, diag, basetype);
1468 if (ctor_p)
1470 /* In a constructor we also need to check the subobject
1471 destructors for cleanup of partially constructed objects. */
1472 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1473 NULL_TREE, flags, complain);
1474 /* Note that we don't pass down trivial_p; the subobject
1475 destructors don't affect triviality of the constructor. Nor
1476 do they affect constexpr-ness (a constant expression doesn't
1477 throw) or exception-specification (a throw from one of the
1478 dtors would be a double-fault). */
1479 process_subob_fn (rval, NULL, NULL,
1480 deleted_p, NULL, false,
1481 basetype);
1484 if (check_vdtor && type_has_virtual_destructor (basetype))
1486 rval = locate_fn_flags (ctype, ansi_opname (DELETE_EXPR),
1487 ptr_type_node, flags, complain);
1488 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
1489 to have a null rval (no class-specific op delete). */
1490 if (rval && rval == error_mark_node && deleted_p)
1491 *deleted_p = true;
1492 check_vdtor = false;
1495 if (diag && assign_p && move_p
1496 && BINFO_VIRTUAL_P (base_binfo)
1497 && rval && TREE_CODE (rval) == FUNCTION_DECL
1498 && move_fn_p (rval) && !trivial_fn_p (rval)
1499 && vbase_has_user_provided_move_assign (basetype))
1500 warning (OPT_Wvirtual_move_assign,
1501 "defaulted move assignment for %qT calls a non-trivial "
1502 "move assignment operator for virtual base %qT",
1503 ctype, basetype);
1506 vbases = CLASSTYPE_VBASECLASSES (ctype);
1507 if (vec_safe_is_empty (vbases))
1508 /* No virtual bases to worry about. */;
1509 else if (!assign_p)
1511 if (constexpr_p)
1512 *constexpr_p = false;
1513 FOR_EACH_VEC_ELT (*vbases, i, base_binfo)
1515 tree basetype = BINFO_TYPE (base_binfo);
1516 if (copy_arg_p)
1517 argtype = build_stub_type (basetype, quals, move_p);
1518 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1520 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1521 constexpr_p, diag, basetype);
1522 if (ctor_p && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype))
1524 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1525 NULL_TREE, flags, complain);
1526 process_subob_fn (rval, NULL, NULL,
1527 deleted_p, NULL, false,
1528 basetype);
1533 /* Now handle the non-static data members. */
1534 walk_field_subobs (TYPE_FIELDS (ctype), fnname, sfk, quals,
1535 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1536 deleted_p, constexpr_p,
1537 diag, flags, complain);
1538 if (ctor_p)
1539 walk_field_subobs (TYPE_FIELDS (ctype), complete_dtor_identifier,
1540 sfk_destructor, TYPE_UNQUALIFIED, false,
1541 false, false, NULL, NULL,
1542 deleted_p, NULL,
1543 false, flags, complain);
1545 pop_scope (scope);
1547 pop_deferring_access_checks ();
1548 --cp_unevaluated_operand;
1549 --c_inhibit_evaluation_warnings;
1552 /* DECL is a defaulted function whose exception specification is now
1553 needed. Return what it should be. */
1555 tree
1556 get_defaulted_eh_spec (tree decl)
1558 if (DECL_CLONED_FUNCTION_P (decl))
1559 decl = DECL_CLONED_FUNCTION (decl);
1560 special_function_kind sfk = special_function_p (decl);
1561 tree ctype = DECL_CONTEXT (decl);
1562 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1563 tree parm_type = TREE_VALUE (parms);
1564 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1565 tree spec = empty_except_spec;
1566 synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL,
1567 NULL, false, DECL_INHERITED_CTOR_BASE (decl),
1568 parms);
1569 return spec;
1572 /* DECL is a deleted function. If it's implicitly deleted, explain why and
1573 return true; else return false. */
1575 bool
1576 maybe_explain_implicit_delete (tree decl)
1578 /* If decl is a clone, get the primary variant. */
1579 decl = DECL_ORIGIN (decl);
1580 gcc_assert (DECL_DELETED_FN (decl));
1581 if (DECL_DEFAULTED_FN (decl))
1583 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
1584 static hash_set<tree> *explained;
1586 special_function_kind sfk;
1587 location_t loc;
1588 bool informed;
1589 tree ctype;
1591 if (!explained)
1592 explained = new hash_set<tree>;
1593 if (explained->add (decl))
1594 return true;
1596 sfk = special_function_p (decl);
1597 ctype = DECL_CONTEXT (decl);
1598 loc = input_location;
1599 input_location = DECL_SOURCE_LOCATION (decl);
1601 informed = false;
1602 if (LAMBDA_TYPE_P (ctype))
1604 informed = true;
1605 if (sfk == sfk_constructor)
1606 inform (DECL_SOURCE_LOCATION (decl),
1607 "a lambda closure type has a deleted default constructor");
1608 else if (sfk == sfk_copy_assignment)
1609 inform (DECL_SOURCE_LOCATION (decl),
1610 "a lambda closure type has a deleted copy assignment operator");
1611 else
1612 informed = false;
1614 else if (DECL_ARTIFICIAL (decl)
1615 && (sfk == sfk_copy_assignment
1616 || sfk == sfk_copy_constructor)
1617 && (type_has_user_declared_move_constructor (ctype)
1618 || type_has_user_declared_move_assign (ctype)))
1620 inform (DECL_SOURCE_LOCATION (decl),
1621 "%q#D is implicitly declared as deleted because %qT "
1622 "declares a move constructor or move assignment operator",
1623 decl, ctype);
1624 informed = true;
1626 if (!informed)
1628 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1629 tree parm_type = TREE_VALUE (parms);
1630 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1631 tree raises = NULL_TREE;
1632 bool deleted_p = false;
1633 tree scope = push_scope (ctype);
1635 synthesized_method_walk (ctype, sfk, const_p,
1636 &raises, NULL, &deleted_p, NULL, false,
1637 DECL_INHERITED_CTOR_BASE (decl), parms);
1638 if (deleted_p)
1640 inform (DECL_SOURCE_LOCATION (decl),
1641 "%q#D is implicitly deleted because the default "
1642 "definition would be ill-formed:", decl);
1643 synthesized_method_walk (ctype, sfk, const_p,
1644 NULL, NULL, NULL, NULL, true,
1645 DECL_INHERITED_CTOR_BASE (decl), parms);
1647 else if (!comp_except_specs
1648 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
1649 raises, ce_normal))
1650 inform (DECL_SOURCE_LOCATION (decl), "%q#F is implicitly "
1651 "deleted because its exception-specification does not "
1652 "match the implicit exception-specification %qX",
1653 decl, raises);
1654 #ifdef ENABLE_CHECKING
1655 else
1656 gcc_unreachable ();
1657 #endif
1659 pop_scope (scope);
1662 input_location = loc;
1663 return true;
1665 return false;
1668 /* DECL is a defaulted function which was declared constexpr. Explain why
1669 it can't be constexpr. */
1671 void
1672 explain_implicit_non_constexpr (tree decl)
1674 tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl));
1675 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1676 bool dummy;
1677 synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
1678 special_function_p (decl), const_p,
1679 NULL, NULL, NULL, &dummy, true,
1680 DECL_INHERITED_CTOR_BASE (decl),
1681 FUNCTION_FIRST_USER_PARMTYPE (decl));
1684 /* DECL is an instantiation of an inheriting constructor template. Deduce
1685 the correct exception-specification and deletedness for this particular
1686 specialization. */
1688 void
1689 deduce_inheriting_ctor (tree decl)
1691 gcc_assert (DECL_INHERITED_CTOR_BASE (decl));
1692 tree spec;
1693 bool trivial, constexpr_, deleted;
1694 synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor,
1695 false, &spec, &trivial, &deleted, &constexpr_,
1696 /*diag*/false,
1697 DECL_INHERITED_CTOR_BASE (decl),
1698 FUNCTION_FIRST_USER_PARMTYPE (decl));
1699 DECL_DELETED_FN (decl) = deleted;
1700 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
1703 /* Implicitly declare the special function indicated by KIND, as a
1704 member of TYPE. For copy constructors and assignment operators,
1705 CONST_P indicates whether these functions should take a const
1706 reference argument or a non-const reference. Returns the
1707 FUNCTION_DECL for the implicitly declared function. */
1709 tree
1710 implicitly_declare_fn (special_function_kind kind, tree type,
1711 bool const_p, tree inherited_ctor,
1712 tree inherited_parms)
1714 tree fn;
1715 tree parameter_types = void_list_node;
1716 tree return_type;
1717 tree fn_type;
1718 tree raises = empty_except_spec;
1719 tree rhs_parm_type = NULL_TREE;
1720 tree this_parm;
1721 tree name;
1722 HOST_WIDE_INT saved_processing_template_decl;
1723 bool deleted_p;
1724 bool constexpr_p;
1726 /* Because we create declarations for implicitly declared functions
1727 lazily, we may be creating the declaration for a member of TYPE
1728 while in some completely different context. However, TYPE will
1729 never be a dependent class (because we never want to do lookups
1730 for implicitly defined functions in a dependent class).
1731 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
1732 because we only create clones for constructors and destructors
1733 when not in a template. */
1734 gcc_assert (!dependent_type_p (type));
1735 saved_processing_template_decl = processing_template_decl;
1736 processing_template_decl = 0;
1738 type = TYPE_MAIN_VARIANT (type);
1740 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (type))
1742 if (kind == sfk_destructor)
1743 /* See comment in check_special_function_return_type. */
1744 return_type = build_pointer_type (void_type_node);
1745 else
1746 return_type = build_pointer_type (type);
1748 else
1749 return_type = void_type_node;
1751 switch (kind)
1753 case sfk_destructor:
1754 /* Destructor. */
1755 name = constructor_name (type);
1756 break;
1758 case sfk_constructor:
1759 /* Default constructor. */
1760 name = constructor_name (type);
1761 break;
1763 case sfk_copy_constructor:
1764 case sfk_copy_assignment:
1765 case sfk_move_constructor:
1766 case sfk_move_assignment:
1767 case sfk_inheriting_constructor:
1769 bool move_p;
1770 if (kind == sfk_copy_assignment
1771 || kind == sfk_move_assignment)
1773 return_type = build_reference_type (type);
1774 name = ansi_assopname (NOP_EXPR);
1776 else
1777 name = constructor_name (type);
1779 if (kind == sfk_inheriting_constructor)
1780 parameter_types = inherited_parms;
1781 else
1783 if (const_p)
1784 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
1785 else
1786 rhs_parm_type = type;
1787 move_p = (kind == sfk_move_assignment
1788 || kind == sfk_move_constructor);
1789 rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
1791 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
1793 break;
1795 default:
1796 gcc_unreachable ();
1799 tree inherited_base = (inherited_ctor
1800 ? DECL_CONTEXT (inherited_ctor)
1801 : NULL_TREE);
1802 bool trivial_p = false;
1804 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
1806 /* For an inheriting constructor template, just copy these flags from
1807 the inherited constructor template for now. */
1808 raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor));
1809 deleted_p = DECL_DELETED_FN (inherited_ctor);
1810 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
1812 else if (cxx_dialect >= cxx11)
1814 raises = unevaluated_noexcept_spec ();
1815 synthesized_method_walk (type, kind, const_p, NULL, &trivial_p,
1816 &deleted_p, &constexpr_p, false,
1817 inherited_base, inherited_parms);
1819 else
1820 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
1821 &deleted_p, &constexpr_p, false,
1822 inherited_base, inherited_parms);
1823 /* Don't bother marking a deleted constructor as constexpr. */
1824 if (deleted_p)
1825 constexpr_p = false;
1826 /* A trivial copy/move constructor is also a constexpr constructor,
1827 unless the class has virtual bases (7.1.5p4). */
1828 else if (trivial_p && cxx_dialect >= cxx11
1829 && (kind == sfk_copy_constructor
1830 || kind == sfk_move_constructor)
1831 && !CLASSTYPE_VBASECLASSES (type))
1832 gcc_assert (constexpr_p);
1834 if (!trivial_p && type_has_trivial_fn (type, kind))
1835 type_set_nontrivial_flag (type, kind);
1837 /* Create the function. */
1838 fn_type = build_method_type_directly (type, return_type, parameter_types);
1839 if (raises)
1840 fn_type = build_exception_variant (fn_type, raises);
1841 fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
1842 if (kind != sfk_inheriting_constructor)
1843 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
1844 if (kind == sfk_constructor || kind == sfk_copy_constructor
1845 || kind == sfk_move_constructor || kind == sfk_inheriting_constructor)
1846 DECL_CONSTRUCTOR_P (fn) = 1;
1847 else if (kind == sfk_destructor)
1848 DECL_DESTRUCTOR_P (fn) = 1;
1849 else
1851 DECL_ASSIGNMENT_OPERATOR_P (fn) = 1;
1852 SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR);
1855 /* If pointers to member functions use the least significant bit to
1856 indicate whether a function is virtual, ensure a pointer
1857 to this function will have that bit clear. */
1858 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
1859 && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
1860 DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
1862 /* Create the explicit arguments. */
1863 if (rhs_parm_type)
1865 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
1866 want its type to be included in the mangled function
1867 name. */
1868 tree decl = cp_build_parm_decl (NULL_TREE, rhs_parm_type);
1869 TREE_READONLY (decl) = 1;
1870 retrofit_lang_decl (decl);
1871 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
1872 DECL_ARGUMENTS (fn) = decl;
1874 else if (kind == sfk_inheriting_constructor)
1876 tree *p = &DECL_ARGUMENTS (fn);
1877 int index = 1;
1878 for (tree parm = inherited_parms; parm != void_list_node;
1879 parm = TREE_CHAIN (parm))
1881 *p = cp_build_parm_decl (NULL_TREE, TREE_VALUE (parm));
1882 retrofit_lang_decl (*p);
1883 DECL_PARM_LEVEL (*p) = 1;
1884 DECL_PARM_INDEX (*p) = index++;
1885 DECL_CONTEXT (*p) = fn;
1886 p = &DECL_CHAIN (*p);
1888 SET_DECL_INHERITED_CTOR_BASE (fn, inherited_base);
1889 DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor);
1890 /* A constructor so declared has the same access as the corresponding
1891 constructor in X. */
1892 TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor);
1893 TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor);
1894 /* Copy constexpr from the inherited constructor even if the
1895 inheriting constructor doesn't satisfy the requirements. */
1896 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
1898 /* Add the "this" parameter. */
1899 this_parm = build_this_parm (fn_type, TYPE_UNQUALIFIED);
1900 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
1901 DECL_ARGUMENTS (fn) = this_parm;
1903 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
1904 DECL_IN_AGGR_P (fn) = 1;
1905 DECL_ARTIFICIAL (fn) = 1;
1906 DECL_DEFAULTED_FN (fn) = 1;
1907 if (cxx_dialect >= cxx11)
1909 /* "The closure type associated with a lambda-expression has a deleted
1910 default constructor and a deleted copy assignment operator." */
1911 if ((kind == sfk_constructor
1912 || kind == sfk_copy_assignment)
1913 && LAMBDA_TYPE_P (type))
1914 deleted_p = true;
1915 DECL_DELETED_FN (fn) = deleted_p;
1916 DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
1918 DECL_EXTERNAL (fn) = true;
1919 DECL_NOT_REALLY_EXTERN (fn) = 1;
1920 DECL_DECLARED_INLINE_P (fn) = 1;
1921 set_linkage_according_to_type (type, fn);
1922 if (TREE_PUBLIC (fn))
1923 DECL_COMDAT (fn) = 1;
1924 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
1925 gcc_assert (!TREE_USED (fn));
1927 /* Restore PROCESSING_TEMPLATE_DECL. */
1928 processing_template_decl = saved_processing_template_decl;
1930 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
1931 fn = add_inherited_template_parms (fn, inherited_ctor);
1933 /* Warn about calling a non-trivial move assignment in a virtual base. */
1934 if (kind == sfk_move_assignment && !deleted_p && !trivial_p
1935 && CLASSTYPE_VBASECLASSES (type))
1937 location_t loc = input_location;
1938 input_location = DECL_SOURCE_LOCATION (fn);
1939 synthesized_method_walk (type, kind, const_p,
1940 NULL, NULL, NULL, NULL, true,
1941 NULL_TREE, NULL_TREE);
1942 input_location = loc;
1945 return fn;
1948 /* Gives any errors about defaulted functions which need to be deferred
1949 until the containing class is complete. */
1951 void
1952 defaulted_late_check (tree fn)
1954 /* Complain about invalid signature for defaulted fn. */
1955 tree ctx = DECL_CONTEXT (fn);
1956 special_function_kind kind = special_function_p (fn);
1957 bool fn_const_p = (copy_fn_p (fn) == 2);
1958 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
1959 NULL, NULL);
1960 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
1962 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
1963 TREE_TYPE (TREE_TYPE (implicit_fn)))
1964 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1965 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn))))
1967 error ("defaulted declaration %q+D", fn);
1968 error_at (DECL_SOURCE_LOCATION (fn),
1969 "does not match expected signature %qD", implicit_fn);
1972 /* 8.4.2/2: An explicitly-defaulted function (...) may have an explicit
1973 exception-specification only if it is compatible (15.4) with the
1974 exception-specification on the implicit declaration. If a function
1975 is explicitly defaulted on its first declaration, (...) it is
1976 implicitly considered to have the same exception-specification as if
1977 it had been implicitly declared. */
1978 maybe_instantiate_noexcept (fn);
1979 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
1980 if (!fn_spec)
1982 if (DECL_DEFAULTED_IN_CLASS_P (fn))
1983 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
1985 else if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
1986 /* Equivalent to the implicit spec. */;
1987 else if (DECL_DEFAULTED_IN_CLASS_P (fn)
1988 && !CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
1989 /* We can't compare an explicit exception-specification on a
1990 constructor defaulted in the class body to the implicit
1991 exception-specification until after we've parsed any NSDMI; see
1992 after_nsdmi_defaulted_late_checks. */;
1993 else
1995 tree eh_spec = get_defaulted_eh_spec (fn);
1996 if (!comp_except_specs (fn_spec, eh_spec, ce_normal))
1998 if (DECL_DEFAULTED_IN_CLASS_P (fn))
1999 DECL_DELETED_FN (fn) = true;
2000 else
2001 error ("function %q+D defaulted on its redeclaration "
2002 "with an exception-specification that differs from "
2003 "the implicit exception-specification %qX", fn, eh_spec);
2007 if (DECL_DEFAULTED_IN_CLASS_P (fn)
2008 && DECL_DECLARED_CONSTEXPR_P (implicit_fn))
2010 /* Hmm...should we do this for out-of-class too? Should it be OK to
2011 add constexpr later like inline, rather than requiring
2012 declarations to match? */
2013 DECL_DECLARED_CONSTEXPR_P (fn) = true;
2014 if (kind == sfk_constructor)
2015 TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
2018 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
2019 && DECL_DECLARED_CONSTEXPR_P (fn))
2021 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
2023 error ("explicitly defaulted function %q+D cannot be declared "
2024 "as constexpr because the implicit declaration is not "
2025 "constexpr:", fn);
2026 explain_implicit_non_constexpr (fn);
2028 DECL_DECLARED_CONSTEXPR_P (fn) = false;
2031 if (DECL_DELETED_FN (implicit_fn))
2032 DECL_DELETED_FN (fn) = 1;
2035 /* OK, we've parsed the NSDMI for class T, now we can check any explicit
2036 exception-specifications on functions defaulted in the class body. */
2038 void
2039 after_nsdmi_defaulted_late_checks (tree t)
2041 if (uses_template_parms (t))
2042 return;
2043 if (t == error_mark_node)
2044 return;
2045 for (tree fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
2046 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
2048 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2049 if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
2050 continue;
2052 tree eh_spec = get_defaulted_eh_spec (fn);
2053 if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)),
2054 eh_spec, ce_normal))
2055 DECL_DELETED_FN (fn) = true;
2059 /* Returns true iff FN can be explicitly defaulted, and gives any
2060 errors if defaulting FN is ill-formed. */
2062 bool
2063 defaultable_fn_check (tree fn)
2065 special_function_kind kind = sfk_none;
2067 if (template_parm_scope_p ())
2069 error ("a template cannot be defaulted");
2070 return false;
2073 if (DECL_CONSTRUCTOR_P (fn))
2075 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
2076 kind = sfk_constructor;
2077 else if (copy_fn_p (fn) > 0
2078 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
2079 == void_list_node))
2080 kind = sfk_copy_constructor;
2081 else if (move_fn_p (fn))
2082 kind = sfk_move_constructor;
2084 else if (DECL_DESTRUCTOR_P (fn))
2085 kind = sfk_destructor;
2086 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2087 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
2089 if (copy_fn_p (fn))
2090 kind = sfk_copy_assignment;
2091 else if (move_fn_p (fn))
2092 kind = sfk_move_assignment;
2095 if (kind == sfk_none)
2097 error ("%qD cannot be defaulted", fn);
2098 return false;
2100 else
2102 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
2103 t && t != void_list_node; t = TREE_CHAIN (t))
2104 if (TREE_PURPOSE (t))
2106 error ("defaulted function %q+D with default argument", fn);
2107 break;
2110 /* Avoid do_warn_unused_parameter warnings. */
2111 for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p))
2112 if (DECL_NAME (p))
2113 TREE_NO_WARNING (p) = 1;
2115 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn)))
2116 /* Defer checking. */;
2117 else if (!processing_template_decl)
2118 defaulted_late_check (fn);
2120 return true;
2124 /* Add an implicit declaration to TYPE for the kind of function
2125 indicated by SFK. Return the FUNCTION_DECL for the new implicit
2126 declaration. */
2128 tree
2129 lazily_declare_fn (special_function_kind sfk, tree type)
2131 tree fn;
2132 /* Whether or not the argument has a const reference type. */
2133 bool const_p = false;
2135 type = TYPE_MAIN_VARIANT (type);
2137 switch (sfk)
2139 case sfk_constructor:
2140 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
2141 break;
2142 case sfk_copy_constructor:
2143 const_p = TYPE_HAS_CONST_COPY_CTOR (type);
2144 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
2145 break;
2146 case sfk_move_constructor:
2147 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
2148 break;
2149 case sfk_copy_assignment:
2150 const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
2151 CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
2152 break;
2153 case sfk_move_assignment:
2154 CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
2155 break;
2156 case sfk_destructor:
2157 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
2158 break;
2159 default:
2160 gcc_unreachable ();
2163 /* Declare the function. */
2164 fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL);
2166 /* [class.copy]/8 If the class definition declares a move constructor or
2167 move assignment operator, the implicitly declared copy constructor is
2168 defined as deleted.... */
2169 if ((sfk == sfk_copy_assignment
2170 || sfk == sfk_copy_constructor)
2171 && (type_has_user_declared_move_constructor (type)
2172 || type_has_user_declared_move_assign (type)))
2173 DECL_DELETED_FN (fn) = true;
2175 /* A destructor may be virtual. */
2176 if (sfk == sfk_destructor
2177 || sfk == sfk_move_assignment
2178 || sfk == sfk_copy_assignment)
2179 check_for_override (fn, type);
2180 /* Add it to CLASSTYPE_METHOD_VEC. */
2181 add_method (type, fn, NULL_TREE);
2182 /* Add it to TYPE_METHODS. */
2183 if (sfk == sfk_destructor
2184 && DECL_VIRTUAL_P (fn))
2185 /* The ABI requires that a virtual destructor go at the end of the
2186 vtable. */
2187 TYPE_METHODS (type) = chainon (TYPE_METHODS (type), fn);
2188 else
2190 DECL_CHAIN (fn) = TYPE_METHODS (type);
2191 TYPE_METHODS (type) = fn;
2193 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
2194 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
2195 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
2196 /* Create appropriate clones. */
2197 clone_function_decl (fn, /*update_method_vec=*/true);
2199 return fn;
2202 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
2203 as there are artificial parms in FN. */
2205 tree
2206 skip_artificial_parms_for (const_tree fn, tree list)
2208 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2209 list = TREE_CHAIN (list);
2210 else
2211 return list;
2213 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2214 list = TREE_CHAIN (list);
2215 if (DECL_HAS_VTT_PARM_P (fn))
2216 list = TREE_CHAIN (list);
2217 return list;
2220 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
2221 artificial parms in FN. */
2224 num_artificial_parms_for (const_tree fn)
2226 int count = 0;
2228 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2229 count++;
2230 else
2231 return 0;
2233 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2234 count++;
2235 if (DECL_HAS_VTT_PARM_P (fn))
2236 count++;
2237 return count;
2241 #include "gt-cp-method.h"