jit: document union types
[official-gcc.git] / gcc / cp / method.c
blob2d5aa636a1158d3f45b79864e858b3ed24fb21ff
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 "symtab.h"
30 #include "tree.h"
31 #include "stringpool.h"
32 #include "varasm.h"
33 #include "cp-tree.h"
34 #include "flags.h"
35 #include "toplev.h"
36 #include "tm_p.h"
37 #include "target.h"
38 #include "common/common-target.h"
39 #include "diagnostic.h"
40 #include "hard-reg-set.h"
41 #include "function.h"
42 #include "cgraph.h"
44 /* Various flags to control the mangling process. */
46 enum mangling_flags
48 /* No flags. */
49 mf_none = 0,
50 /* The thing we are presently mangling is part of a template type,
51 rather than a fully instantiated type. Therefore, we may see
52 complex expressions where we would normally expect to see a
53 simple integer constant. */
54 mf_maybe_uninstantiated = 1,
55 /* When mangling a numeric value, use the form `_XX_' (instead of
56 just `XX') if the value has more than one digit. */
57 mf_use_underscores_around_value = 2
60 typedef enum mangling_flags mangling_flags;
62 static void do_build_copy_assign (tree);
63 static void do_build_copy_constructor (tree);
64 static tree make_alias_for_thunk (tree);
66 /* Called once to initialize method.c. */
68 void
69 init_method (void)
71 init_mangle ();
74 /* Return a this or result adjusting thunk to FUNCTION. THIS_ADJUSTING
75 indicates whether it is a this or result adjusting thunk.
76 FIXED_OFFSET and VIRTUAL_OFFSET indicate how to do the adjustment
77 (see thunk_adjust). VIRTUAL_OFFSET can be NULL, but FIXED_OFFSET
78 never is. VIRTUAL_OFFSET is the /index/ into the vtable for this
79 adjusting thunks, we scale it to a byte offset. For covariant
80 thunks VIRTUAL_OFFSET is the virtual binfo. You must post process
81 the returned thunk with finish_thunk. */
83 tree
84 make_thunk (tree function, bool this_adjusting,
85 tree fixed_offset, tree virtual_offset)
87 HOST_WIDE_INT d;
88 tree thunk;
90 gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
91 /* We can have this thunks to covariant thunks, but not vice versa. */
92 gcc_assert (!DECL_THIS_THUNK_P (function));
93 gcc_assert (!DECL_RESULT_THUNK_P (function) || this_adjusting);
95 /* Scale the VIRTUAL_OFFSET to be in terms of bytes. */
96 if (this_adjusting && virtual_offset)
97 virtual_offset
98 = size_binop (MULT_EXPR,
99 virtual_offset,
100 convert (ssizetype,
101 TYPE_SIZE_UNIT (vtable_entry_type)));
103 d = tree_to_shwi (fixed_offset);
105 /* See if we already have the thunk in question. For this_adjusting
106 thunks VIRTUAL_OFFSET will be an INTEGER_CST, for covariant thunks it
107 will be a BINFO. */
108 for (thunk = DECL_THUNKS (function); thunk; thunk = DECL_CHAIN (thunk))
109 if (DECL_THIS_THUNK_P (thunk) == this_adjusting
110 && THUNK_FIXED_OFFSET (thunk) == d
111 && !virtual_offset == !THUNK_VIRTUAL_OFFSET (thunk)
112 && (!virtual_offset
113 || (this_adjusting
114 ? tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk),
115 virtual_offset)
116 : THUNK_VIRTUAL_OFFSET (thunk) == virtual_offset)))
117 return thunk;
119 /* All thunks must be created before FUNCTION is actually emitted;
120 the ABI requires that all thunks be emitted together with the
121 function to which they transfer control. */
122 gcc_assert (!TREE_ASM_WRITTEN (function));
123 /* Likewise, we can only be adding thunks to a function declared in
124 the class currently being laid out. */
125 gcc_assert (TYPE_SIZE (DECL_CONTEXT (function))
126 && TYPE_BEING_DEFINED (DECL_CONTEXT (function)));
128 thunk = build_decl (DECL_SOURCE_LOCATION (function),
129 FUNCTION_DECL, NULL_TREE, TREE_TYPE (function));
130 DECL_LANG_SPECIFIC (thunk) = DECL_LANG_SPECIFIC (function);
131 cxx_dup_lang_specific_decl (thunk);
132 DECL_VIRTUAL_P (thunk) = true;
133 SET_DECL_THUNKS (thunk, NULL_TREE);
135 DECL_CONTEXT (thunk) = DECL_CONTEXT (function);
136 TREE_READONLY (thunk) = TREE_READONLY (function);
137 TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (function);
138 TREE_PUBLIC (thunk) = TREE_PUBLIC (function);
139 SET_DECL_THUNK_P (thunk, this_adjusting);
140 THUNK_TARGET (thunk) = function;
141 THUNK_FIXED_OFFSET (thunk) = d;
142 THUNK_VIRTUAL_OFFSET (thunk) = virtual_offset;
143 THUNK_ALIAS (thunk) = NULL_TREE;
145 DECL_INTERFACE_KNOWN (thunk) = 1;
146 DECL_NOT_REALLY_EXTERN (thunk) = 1;
147 DECL_COMDAT (thunk) = DECL_COMDAT (function);
148 DECL_SAVED_FUNCTION_DATA (thunk) = NULL;
149 /* The thunk itself is not a constructor or destructor, even if
150 the thing it is thunking to is. */
151 DECL_DESTRUCTOR_P (thunk) = 0;
152 DECL_CONSTRUCTOR_P (thunk) = 0;
153 DECL_EXTERNAL (thunk) = 1;
154 DECL_ARTIFICIAL (thunk) = 1;
155 /* The THUNK is not a pending inline, even if the FUNCTION is. */
156 DECL_PENDING_INLINE_P (thunk) = 0;
157 DECL_DECLARED_INLINE_P (thunk) = 0;
158 /* Nor is it a template instantiation. */
159 DECL_USE_TEMPLATE (thunk) = 0;
160 DECL_TEMPLATE_INFO (thunk) = NULL;
162 /* Add it to the list of thunks associated with FUNCTION. */
163 DECL_CHAIN (thunk) = DECL_THUNKS (function);
164 SET_DECL_THUNKS (function, thunk);
166 return thunk;
169 /* Finish THUNK, a thunk decl. */
171 void
172 finish_thunk (tree thunk)
174 tree function, name;
175 tree fixed_offset = ssize_int (THUNK_FIXED_OFFSET (thunk));
176 tree virtual_offset = THUNK_VIRTUAL_OFFSET (thunk);
178 gcc_assert (!DECL_NAME (thunk) && DECL_THUNK_P (thunk));
179 if (virtual_offset && DECL_RESULT_THUNK_P (thunk))
180 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
181 function = THUNK_TARGET (thunk);
182 name = mangle_thunk (function, DECL_THIS_THUNK_P (thunk),
183 fixed_offset, virtual_offset);
185 /* We can end up with declarations of (logically) different
186 covariant thunks, that do identical adjustments. The two thunks
187 will be adjusting between within different hierarchies, which
188 happen to have the same layout. We must nullify one of them to
189 refer to the other. */
190 if (DECL_RESULT_THUNK_P (thunk))
192 tree cov_probe;
194 for (cov_probe = DECL_THUNKS (function);
195 cov_probe; cov_probe = DECL_CHAIN (cov_probe))
196 if (DECL_NAME (cov_probe) == name)
198 gcc_assert (!DECL_THUNKS (thunk));
199 THUNK_ALIAS (thunk) = (THUNK_ALIAS (cov_probe)
200 ? THUNK_ALIAS (cov_probe) : cov_probe);
201 break;
205 DECL_NAME (thunk) = name;
206 SET_DECL_ASSEMBLER_NAME (thunk, name);
209 static GTY (()) int thunk_labelno;
211 /* Create a static alias to target. */
213 tree
214 make_alias_for (tree target, tree newid)
216 tree alias = build_decl (DECL_SOURCE_LOCATION (target),
217 TREE_CODE (target), newid, TREE_TYPE (target));
218 DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (target);
219 cxx_dup_lang_specific_decl (alias);
220 DECL_CONTEXT (alias) = NULL;
221 TREE_READONLY (alias) = TREE_READONLY (target);
222 TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (target);
223 TREE_PUBLIC (alias) = 0;
224 DECL_INTERFACE_KNOWN (alias) = 1;
225 if (DECL_LANG_SPECIFIC (alias))
227 DECL_NOT_REALLY_EXTERN (alias) = 1;
228 DECL_USE_TEMPLATE (alias) = 0;
229 DECL_TEMPLATE_INFO (alias) = NULL;
231 DECL_EXTERNAL (alias) = 0;
232 DECL_ARTIFICIAL (alias) = 1;
233 DECL_TEMPLATE_INSTANTIATED (alias) = 0;
234 if (TREE_CODE (alias) == FUNCTION_DECL)
236 DECL_SAVED_FUNCTION_DATA (alias) = NULL;
237 DECL_DESTRUCTOR_P (alias) = 0;
238 DECL_CONSTRUCTOR_P (alias) = 0;
239 DECL_PENDING_INLINE_P (alias) = 0;
240 DECL_DECLARED_INLINE_P (alias) = 0;
241 DECL_INITIAL (alias) = error_mark_node;
242 DECL_ARGUMENTS (alias) = copy_list (DECL_ARGUMENTS (target));
244 else
245 TREE_STATIC (alias) = 1;
246 TREE_ADDRESSABLE (alias) = 1;
247 TREE_USED (alias) = 1;
248 SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
249 return alias;
252 static tree
253 make_alias_for_thunk (tree function)
255 tree alias;
256 char buf[256];
258 targetm.asm_out.generate_internal_label (buf, "LTHUNK", thunk_labelno);
259 thunk_labelno++;
261 alias = make_alias_for (function, get_identifier (buf));
263 if (!flag_syntax_only)
265 struct cgraph_node *funcn, *aliasn;
266 funcn = cgraph_node::get (function);
267 gcc_checking_assert (funcn);
268 aliasn = cgraph_node::create_same_body_alias (alias, function);
269 DECL_ASSEMBLER_NAME (function);
270 gcc_assert (aliasn != NULL);
273 return alias;
276 /* Emit the definition of a C++ multiple inheritance or covariant
277 return vtable thunk. If EMIT_P is nonzero, the thunk is emitted
278 immediately. */
280 void
281 use_thunk (tree thunk_fndecl, bool emit_p)
283 tree a, t, function, alias;
284 tree virtual_offset;
285 HOST_WIDE_INT fixed_offset, virtual_value;
286 bool this_adjusting = DECL_THIS_THUNK_P (thunk_fndecl);
287 struct cgraph_node *funcn, *thunk_node;
289 /* We should have called finish_thunk to give it a name. */
290 gcc_assert (DECL_NAME (thunk_fndecl));
292 /* We should never be using an alias, always refer to the
293 aliased thunk. */
294 gcc_assert (!THUNK_ALIAS (thunk_fndecl));
296 if (TREE_ASM_WRITTEN (thunk_fndecl))
297 return;
299 function = THUNK_TARGET (thunk_fndecl);
300 if (DECL_RESULT (thunk_fndecl))
301 /* We already turned this thunk into an ordinary function.
302 There's no need to process this thunk again. */
303 return;
305 if (DECL_THUNK_P (function))
306 /* The target is itself a thunk, process it now. */
307 use_thunk (function, emit_p);
309 /* Thunks are always addressable; they only appear in vtables. */
310 TREE_ADDRESSABLE (thunk_fndecl) = 1;
312 /* Figure out what function is being thunked to. It's referenced in
313 this translation unit. */
314 TREE_ADDRESSABLE (function) = 1;
315 mark_used (function);
316 if (!emit_p)
317 return;
319 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function))
320 alias = make_alias_for_thunk (function);
321 else
322 alias = function;
324 fixed_offset = THUNK_FIXED_OFFSET (thunk_fndecl);
325 virtual_offset = THUNK_VIRTUAL_OFFSET (thunk_fndecl);
327 if (virtual_offset)
329 if (!this_adjusting)
330 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
331 virtual_value = tree_to_shwi (virtual_offset);
332 gcc_assert (virtual_value);
334 else
335 virtual_value = 0;
337 /* And, if we need to emit the thunk, it's used. */
338 mark_used (thunk_fndecl);
339 /* This thunk is actually defined. */
340 DECL_EXTERNAL (thunk_fndecl) = 0;
341 /* The linkage of the function may have changed. FIXME in linkage
342 rewrite. */
343 gcc_assert (DECL_INTERFACE_KNOWN (function));
344 TREE_PUBLIC (thunk_fndecl) = TREE_PUBLIC (function);
345 DECL_VISIBILITY (thunk_fndecl) = DECL_VISIBILITY (function);
346 DECL_VISIBILITY_SPECIFIED (thunk_fndecl)
347 = DECL_VISIBILITY_SPECIFIED (function);
348 DECL_COMDAT (thunk_fndecl) = DECL_COMDAT (function);
349 DECL_WEAK (thunk_fndecl) = DECL_WEAK (function);
351 if (flag_syntax_only)
353 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
354 return;
357 push_to_top_level ();
359 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function)
360 && targetm_common.have_named_sections)
362 tree fn = function;
363 struct symtab_node *symbol;
365 if ((symbol = symtab_node::get (function))
366 && symbol->alias)
368 if (symbol->analyzed)
369 fn = symtab_node::get (function)->ultimate_alias_target ()->decl;
370 else
371 fn = symtab_node::get (function)->alias_target;
373 resolve_unique_section (fn, 0, flag_function_sections);
375 if (DECL_SECTION_NAME (fn) != NULL && DECL_ONE_ONLY (fn))
377 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
379 /* Output the thunk into the same section as function. */
380 set_decl_section_name (thunk_fndecl, DECL_SECTION_NAME (fn));
381 symtab_node::get (thunk_fndecl)->implicit_section
382 = symtab_node::get (fn)->implicit_section;
386 /* Set up cloned argument trees for the thunk. */
387 t = NULL_TREE;
388 for (a = DECL_ARGUMENTS (function); a; a = DECL_CHAIN (a))
390 tree x = copy_node (a);
391 DECL_CHAIN (x) = t;
392 DECL_CONTEXT (x) = thunk_fndecl;
393 SET_DECL_RTL (x, NULL);
394 DECL_HAS_VALUE_EXPR_P (x) = 0;
395 TREE_ADDRESSABLE (x) = 0;
396 t = x;
398 a = nreverse (t);
399 DECL_ARGUMENTS (thunk_fndecl) = a;
400 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
401 funcn = cgraph_node::get (function);
402 gcc_checking_assert (funcn);
403 thunk_node = funcn->create_thunk (thunk_fndecl, function,
404 this_adjusting, fixed_offset, virtual_value,
405 virtual_offset, alias);
406 if (DECL_ONE_ONLY (function))
407 thunk_node->add_to_same_comdat_group (funcn);
409 pop_from_top_level ();
412 /* Code for synthesizing methods which have default semantics defined. */
414 /* True iff CTYPE has a trivial SFK. */
416 static bool
417 type_has_trivial_fn (tree ctype, special_function_kind sfk)
419 switch (sfk)
421 case sfk_constructor:
422 return !TYPE_HAS_COMPLEX_DFLT (ctype);
423 case sfk_copy_constructor:
424 return !TYPE_HAS_COMPLEX_COPY_CTOR (ctype);
425 case sfk_move_constructor:
426 return !TYPE_HAS_COMPLEX_MOVE_CTOR (ctype);
427 case sfk_copy_assignment:
428 return !TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype);
429 case sfk_move_assignment:
430 return !TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype);
431 case sfk_destructor:
432 return !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype);
433 case sfk_inheriting_constructor:
434 return false;
435 default:
436 gcc_unreachable ();
440 /* Note that CTYPE has a non-trivial SFK even though we previously thought
441 it was trivial. */
443 static void
444 type_set_nontrivial_flag (tree ctype, special_function_kind sfk)
446 switch (sfk)
448 case sfk_constructor:
449 TYPE_HAS_COMPLEX_DFLT (ctype) = true;
450 return;
451 case sfk_copy_constructor:
452 TYPE_HAS_COMPLEX_COPY_CTOR (ctype) = true;
453 return;
454 case sfk_move_constructor:
455 TYPE_HAS_COMPLEX_MOVE_CTOR (ctype) = true;
456 return;
457 case sfk_copy_assignment:
458 TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype) = true;
459 return;
460 case sfk_move_assignment:
461 TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype) = true;
462 return;
463 case sfk_destructor:
464 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
465 return;
466 case sfk_inheriting_constructor:
467 default:
468 gcc_unreachable ();
472 /* True iff FN is a trivial defaulted member function ([cd]tor, op=). */
474 bool
475 trivial_fn_p (tree fn)
477 if (TREE_CODE (fn) == TEMPLATE_DECL)
478 return false;
479 if (!DECL_DEFAULTED_FN (fn))
480 return false;
482 /* If fn is a clone, get the primary variant. */
483 if (tree prim = DECL_CLONED_FUNCTION (fn))
484 fn = prim;
485 return type_has_trivial_fn (DECL_CONTEXT (fn), special_function_p (fn));
488 /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO
489 given the parameter or parameters PARM, possibly inherited constructor
490 base INH, or move flag MOVE_P. */
492 static tree
493 add_one_base_init (tree binfo, tree parm, bool move_p, tree inh,
494 tree member_init_list)
496 tree init;
497 if (inh)
499 /* An inheriting constructor only has a mem-initializer for
500 the base it inherits from. */
501 if (BINFO_TYPE (binfo) != inh)
502 return member_init_list;
504 tree *p = &init;
505 init = NULL_TREE;
506 for (; parm; parm = DECL_CHAIN (parm))
508 tree exp = convert_from_reference (parm);
509 if (TREE_CODE (TREE_TYPE (parm)) != REFERENCE_TYPE
510 || TYPE_REF_IS_RVALUE (TREE_TYPE (parm)))
511 exp = move (exp);
512 *p = build_tree_list (NULL_TREE, exp);
513 p = &TREE_CHAIN (*p);
516 else
518 init = build_base_path (PLUS_EXPR, parm, binfo, 1,
519 tf_warning_or_error);
520 if (move_p)
521 init = move (init);
522 init = build_tree_list (NULL_TREE, init);
524 return tree_cons (binfo, init, member_init_list);
527 /* Generate code for default X(X&) or X(X&&) constructor or an inheriting
528 constructor. */
530 static void
531 do_build_copy_constructor (tree fndecl)
533 tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
534 bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
535 bool trivial = trivial_fn_p (fndecl);
536 tree inh = DECL_INHERITED_CTOR_BASE (fndecl);
538 if (!inh)
539 parm = convert_from_reference (parm);
541 if (trivial
542 && is_empty_class (current_class_type))
543 /* Don't copy the padding byte; it might not have been allocated
544 if *this is a base subobject. */;
545 else if (trivial)
547 tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm);
548 finish_expr_stmt (t);
550 else
552 tree fields = TYPE_FIELDS (current_class_type);
553 tree member_init_list = NULL_TREE;
554 int cvquals = cp_type_quals (TREE_TYPE (parm));
555 int i;
556 tree binfo, base_binfo;
557 tree init;
558 vec<tree, va_gc> *vbases;
560 /* Initialize all the base-classes with the parameter converted
561 to their type so that we get their copy constructor and not
562 another constructor that takes current_class_type. We must
563 deal with the binfo's directly as a direct base might be
564 inaccessible due to ambiguity. */
565 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
566 vec_safe_iterate (vbases, i, &binfo); i++)
568 member_init_list = add_one_base_init (binfo, parm, move_p, inh,
569 member_init_list);
572 for (binfo = TYPE_BINFO (current_class_type), i = 0;
573 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
575 if (BINFO_VIRTUAL_P (base_binfo))
576 continue;
577 member_init_list = add_one_base_init (base_binfo, parm, move_p,
578 inh, member_init_list);
581 for (; fields; fields = DECL_CHAIN (fields))
583 tree field = fields;
584 tree expr_type;
586 if (TREE_CODE (field) != FIELD_DECL)
587 continue;
588 if (inh)
589 continue;
591 expr_type = TREE_TYPE (field);
592 if (DECL_NAME (field))
594 if (VFIELD_NAME_P (DECL_NAME (field)))
595 continue;
597 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
598 /* Just use the field; anonymous types can't have
599 nontrivial copy ctors or assignment ops or this
600 function would be deleted. */;
601 else
602 continue;
604 /* Compute the type of "init->field". If the copy-constructor
605 parameter is, for example, "const S&", and the type of
606 the field is "T", then the type will usually be "const
607 T". (There are no cv-qualified variants of reference
608 types.) */
609 if (TREE_CODE (expr_type) != REFERENCE_TYPE)
611 int quals = cvquals;
613 if (DECL_MUTABLE_P (field))
614 quals &= ~TYPE_QUAL_CONST;
615 quals |= cp_type_quals (expr_type);
616 expr_type = cp_build_qualified_type (expr_type, quals);
619 init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
620 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
621 /* 'move' breaks bit-fields, and has no effect for scalars. */
622 && !scalarish_type_p (expr_type))
623 init = move (init);
624 init = build_tree_list (NULL_TREE, init);
626 member_init_list = tree_cons (field, init, member_init_list);
628 finish_mem_initializers (member_init_list);
632 static void
633 do_build_copy_assign (tree fndecl)
635 tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl));
636 tree compound_stmt;
637 bool move_p = move_fn_p (fndecl);
638 bool trivial = trivial_fn_p (fndecl);
639 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
641 compound_stmt = begin_compound_stmt (0);
642 parm = convert_from_reference (parm);
644 if (trivial
645 && is_empty_class (current_class_type))
646 /* Don't copy the padding byte; it might not have been allocated
647 if *this is a base subobject. */;
648 else if (trivial)
650 tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
651 finish_expr_stmt (t);
653 else
655 tree fields;
656 int cvquals = cp_type_quals (TREE_TYPE (parm));
657 int i;
658 tree binfo, base_binfo;
660 /* Assign to each of the direct base classes. */
661 for (binfo = TYPE_BINFO (current_class_type), i = 0;
662 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
664 tree converted_parm;
665 vec<tree, va_gc> *parmvec;
667 /* We must convert PARM directly to the base class
668 explicitly since the base class may be ambiguous. */
669 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
670 tf_warning_or_error);
671 if (move_p)
672 converted_parm = move (converted_parm);
673 /* Call the base class assignment operator. */
674 parmvec = make_tree_vector_single (converted_parm);
675 finish_expr_stmt
676 (build_special_member_call (current_class_ref,
677 ansi_assopname (NOP_EXPR),
678 &parmvec,
679 base_binfo,
680 flags,
681 tf_warning_or_error));
682 release_tree_vector (parmvec);
685 /* Assign to each of the non-static data members. */
686 for (fields = TYPE_FIELDS (current_class_type);
687 fields;
688 fields = DECL_CHAIN (fields))
690 tree comp = current_class_ref;
691 tree init = parm;
692 tree field = fields;
693 tree expr_type;
694 int quals;
696 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
697 continue;
699 expr_type = TREE_TYPE (field);
701 if (CP_TYPE_CONST_P (expr_type))
703 error ("non-static const member %q#D, can%'t use default "
704 "assignment operator", field);
705 continue;
707 else if (TREE_CODE (expr_type) == REFERENCE_TYPE)
709 error ("non-static reference member %q#D, can%'t use "
710 "default assignment operator", field);
711 continue;
714 if (DECL_NAME (field))
716 if (VFIELD_NAME_P (DECL_NAME (field)))
717 continue;
719 else if (ANON_AGGR_TYPE_P (expr_type)
720 && TYPE_FIELDS (expr_type) != NULL_TREE)
721 /* Just use the field; anonymous types can't have
722 nontrivial copy ctors or assignment ops or this
723 function would be deleted. */;
724 else
725 continue;
727 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
729 /* Compute the type of init->field */
730 quals = cvquals;
731 if (DECL_MUTABLE_P (field))
732 quals &= ~TYPE_QUAL_CONST;
733 expr_type = cp_build_qualified_type (expr_type, quals);
735 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
736 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
737 /* 'move' breaks bit-fields, and has no effect for scalars. */
738 && !scalarish_type_p (expr_type))
739 init = move (init);
741 if (DECL_NAME (field))
742 init = cp_build_modify_expr (comp, NOP_EXPR, init,
743 tf_warning_or_error);
744 else
745 init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
746 finish_expr_stmt (init);
749 finish_return_stmt (current_class_ref);
750 finish_compound_stmt (compound_stmt);
753 /* Synthesize FNDECL, a non-static member function. */
755 void
756 synthesize_method (tree fndecl)
758 bool nested = (current_function_decl != NULL_TREE);
759 tree context = decl_function_context (fndecl);
760 bool need_body = true;
761 tree stmt;
762 location_t save_input_location = input_location;
763 int error_count = errorcount;
764 int warning_count = warningcount + werrorcount;
766 /* Reset the source location, we might have been previously
767 deferred, and thus have saved where we were first needed. */
768 DECL_SOURCE_LOCATION (fndecl)
769 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
771 /* If we've been asked to synthesize a clone, just synthesize the
772 cloned function instead. Doing so will automatically fill in the
773 body for the clone. */
774 if (DECL_CLONED_FUNCTION_P (fndecl))
775 fndecl = DECL_CLONED_FUNCTION (fndecl);
777 /* We may be in the middle of deferred access check. Disable
778 it now. */
779 push_deferring_access_checks (dk_no_deferred);
781 if (! context)
782 push_to_top_level ();
783 else if (nested)
784 push_function_context ();
786 input_location = DECL_SOURCE_LOCATION (fndecl);
788 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
789 stmt = begin_function_body ();
791 if (DECL_OVERLOADED_OPERATOR_P (fndecl) == NOP_EXPR)
793 do_build_copy_assign (fndecl);
794 need_body = false;
796 else if (DECL_CONSTRUCTOR_P (fndecl))
798 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
799 if (arg_chain != void_list_node)
800 do_build_copy_constructor (fndecl);
801 else
802 finish_mem_initializers (NULL_TREE);
805 /* If we haven't yet generated the body of the function, just
806 generate an empty compound statement. */
807 if (need_body)
809 tree compound_stmt;
810 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
811 finish_compound_stmt (compound_stmt);
814 finish_function_body (stmt);
815 expand_or_defer_fn (finish_function (0));
817 input_location = save_input_location;
819 if (! context)
820 pop_from_top_level ();
821 else if (nested)
822 pop_function_context ();
824 pop_deferring_access_checks ();
826 if (error_count != errorcount || warning_count != warningcount + werrorcount)
827 inform (input_location, "synthesized method %qD first required here ",
828 fndecl);
831 /* Build a reference to type TYPE with cv-quals QUALS, which is an
832 rvalue if RVALUE is true. */
834 static tree
835 build_stub_type (tree type, int quals, bool rvalue)
837 tree argtype = cp_build_qualified_type (type, quals);
838 return cp_build_reference_type (argtype, rvalue);
841 /* Build a dummy glvalue from dereferencing a dummy reference of type
842 REFTYPE. */
844 static tree
845 build_stub_object (tree reftype)
847 if (TREE_CODE (reftype) != REFERENCE_TYPE)
848 reftype = cp_build_reference_type (reftype, /*rval*/true);
849 tree stub = build1 (CONVERT_EXPR, reftype, integer_one_node);
850 return convert_from_reference (stub);
853 /* Determine which function will be called when looking up NAME in TYPE,
854 called with a single ARGTYPE argument, or no argument if ARGTYPE is
855 null. FLAGS and COMPLAIN are as for build_new_method_call.
857 Returns a FUNCTION_DECL if all is well.
858 Returns NULL_TREE if overload resolution failed.
859 Returns error_mark_node if the chosen function cannot be called. */
861 static tree
862 locate_fn_flags (tree type, tree name, tree argtype, int flags,
863 tsubst_flags_t complain)
865 tree ob, fn, fns, binfo, rval;
866 vec<tree, va_gc> *args;
868 if (TYPE_P (type))
869 binfo = TYPE_BINFO (type);
870 else
872 binfo = type;
873 type = BINFO_TYPE (binfo);
876 ob = build_stub_object (cp_build_reference_type (type, false));
877 args = make_tree_vector ();
878 if (argtype)
880 if (TREE_CODE (argtype) == TREE_LIST)
882 for (tree elt = argtype; elt != void_list_node;
883 elt = TREE_CHAIN (elt))
885 tree type = TREE_VALUE (elt);
886 tree arg = build_stub_object (type);
887 vec_safe_push (args, arg);
890 else
892 tree arg = build_stub_object (argtype);
893 args->quick_push (arg);
897 fns = lookup_fnfields (binfo, name, 0);
898 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
900 release_tree_vector (args);
901 if (fn && rval == error_mark_node)
902 return rval;
903 else
904 return fn;
907 /* Locate the dtor of TYPE. */
909 tree
910 get_dtor (tree type, tsubst_flags_t complain)
912 tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
913 LOOKUP_NORMAL, complain);
914 if (fn == error_mark_node)
915 return NULL_TREE;
916 return fn;
919 /* Locate the default ctor of TYPE. */
921 tree
922 locate_ctor (tree type)
924 tree fn;
926 push_deferring_access_checks (dk_no_check);
927 fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
928 LOOKUP_SPECULATIVE, tf_none);
929 pop_deferring_access_checks ();
930 if (fn == error_mark_node)
931 return NULL_TREE;
932 return fn;
935 /* Likewise, but give any appropriate errors. */
937 tree
938 get_default_ctor (tree type)
940 tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
941 LOOKUP_NORMAL, tf_warning_or_error);
942 if (fn == error_mark_node)
943 return NULL_TREE;
944 return fn;
947 /* Locate the copy ctor of TYPE. */
949 tree
950 get_copy_ctor (tree type, tsubst_flags_t complain)
952 int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
953 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
954 tree argtype = build_stub_type (type, quals, false);
955 tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
956 LOOKUP_NORMAL, complain);
957 if (fn == error_mark_node)
958 return NULL_TREE;
959 return fn;
962 /* Locate the copy assignment operator of TYPE. */
964 tree
965 get_copy_assign (tree type)
967 int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
968 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
969 tree argtype = build_stub_type (type, quals, false);
970 tree fn = locate_fn_flags (type, ansi_assopname (NOP_EXPR), argtype,
971 LOOKUP_NORMAL, tf_warning_or_error);
972 if (fn == error_mark_node)
973 return NULL_TREE;
974 return fn;
977 /* Locate the inherited constructor of constructor CTOR. */
979 tree
980 get_inherited_ctor (tree ctor)
982 gcc_assert (DECL_INHERITED_CTOR_BASE (ctor));
984 push_deferring_access_checks (dk_no_check);
985 tree fn = locate_fn_flags (DECL_INHERITED_CTOR_BASE (ctor),
986 complete_ctor_identifier,
987 FUNCTION_FIRST_USER_PARMTYPE (ctor),
988 LOOKUP_NORMAL|LOOKUP_SPECULATIVE,
989 tf_none);
990 pop_deferring_access_checks ();
991 if (fn == error_mark_node)
992 return NULL_TREE;
993 return fn;
996 /* walk_tree helper function for is_trivially_xible. If *TP is a call,
997 return it if it calls something other than a trivial special member
998 function. */
1000 static tree
1001 check_nontriv (tree *tp, int *, void *)
1003 tree fn;
1004 if (TREE_CODE (*tp) == CALL_EXPR)
1005 fn = CALL_EXPR_FN (*tp);
1006 else if (TREE_CODE (*tp) == AGGR_INIT_EXPR)
1007 fn = AGGR_INIT_EXPR_FN (*tp);
1008 else
1009 return NULL_TREE;
1011 if (TREE_CODE (fn) == ADDR_EXPR)
1012 fn = TREE_OPERAND (fn, 0);
1014 if (TREE_CODE (fn) != FUNCTION_DECL
1015 || !trivial_fn_p (fn))
1016 return fn;
1017 return NULL_TREE;
1020 /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */
1022 static tree
1023 assignable_expr (tree to, tree from)
1025 ++cp_unevaluated_operand;
1026 to = build_stub_object (to);
1027 from = build_stub_object (from);
1028 tree r = cp_build_modify_expr (to, NOP_EXPR, from, tf_none);
1029 --cp_unevaluated_operand;
1030 return r;
1033 /* The predicate condition for a template specialization
1034 is_constructible<T, Args...> shall be satisfied if and only if the
1035 following variable definition would be well-formed for some invented
1036 variable t: T t(create<Args>()...);
1038 Return something equivalent in well-formedness and triviality. */
1040 static tree
1041 constructible_expr (tree to, tree from)
1043 tree expr;
1044 if (CLASS_TYPE_P (to))
1046 tree ctype = to;
1047 vec<tree, va_gc> *args = NULL;
1048 if (TREE_CODE (to) != REFERENCE_TYPE)
1049 to = cp_build_reference_type (to, /*rval*/false);
1050 tree ob = build_stub_object (to);
1051 for (; from; from = TREE_CHAIN (from))
1052 vec_safe_push (args, build_stub_object (TREE_VALUE (from)));
1053 expr = build_special_member_call (ob, complete_ctor_identifier, &args,
1054 ctype, LOOKUP_NORMAL, tf_none);
1055 if (expr == error_mark_node)
1056 return error_mark_node;
1057 /* The current state of the standard vis-a-vis LWG 2116 is that
1058 is_*constructible involves destruction as well. */
1059 if (type_build_dtor_call (ctype))
1061 tree dtor = build_special_member_call (ob, complete_dtor_identifier,
1062 NULL, ctype, LOOKUP_NORMAL,
1063 tf_none);
1064 if (dtor == error_mark_node)
1065 return error_mark_node;
1066 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
1067 expr = build2 (COMPOUND_EXPR, void_type_node, expr, dtor);
1070 else
1072 if (from == NULL_TREE)
1073 return build_value_init (to, tf_none);
1074 else if (TREE_CHAIN (from))
1075 return error_mark_node; // too many initializers
1076 from = build_stub_object (TREE_VALUE (from));
1077 expr = perform_direct_initialization_if_possible (to, from,
1078 /*cast*/false,
1079 tf_none);
1081 return expr;
1084 /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or
1085 constructible (otherwise) from FROM, which is a single type for
1086 assignment or a list of types for construction. */
1088 bool
1089 is_trivially_xible (enum tree_code code, tree to, tree from)
1091 tree expr;
1092 if (code == MODIFY_EXPR)
1093 expr = assignable_expr (to, from);
1094 else if (from && TREE_CHAIN (from))
1095 return false; // only 0- and 1-argument ctors can be trivial
1096 else
1097 expr = constructible_expr (to, from);
1099 if (expr == error_mark_node)
1100 return false;
1101 tree nt = cp_walk_tree_without_duplicates (&expr, check_nontriv, NULL);
1102 return !nt;
1105 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
1106 DELETED_P or give an error message MSG with argument ARG. */
1108 static void
1109 process_subob_fn (tree fn, tree *spec_p, bool *trivial_p,
1110 bool *deleted_p, bool *constexpr_p,
1111 bool diag, tree arg)
1113 if (!fn || fn == error_mark_node)
1114 goto bad;
1116 if (spec_p)
1118 maybe_instantiate_noexcept (fn);
1119 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
1120 *spec_p = merge_exception_specifiers (*spec_p, raises);
1123 if (!trivial_fn_p (fn))
1125 if (trivial_p)
1126 *trivial_p = false;
1127 if (TREE_CODE (arg) == FIELD_DECL
1128 && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
1130 if (deleted_p)
1131 *deleted_p = true;
1132 if (diag)
1133 error ("union member %q+D with non-trivial %qD", arg, fn);
1137 if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
1139 *constexpr_p = false;
1140 if (diag)
1142 inform (0, "defaulted constructor calls non-constexpr "
1143 "%q+D", fn);
1144 explain_invalid_constexpr_fn (fn);
1148 return;
1150 bad:
1151 if (deleted_p)
1152 *deleted_p = true;
1155 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
1156 aggregates. */
1158 static void
1159 walk_field_subobs (tree fields, tree fnname, special_function_kind sfk,
1160 int quals, bool copy_arg_p, bool move_p,
1161 bool assign_p, tree *spec_p, bool *trivial_p,
1162 bool *deleted_p, bool *constexpr_p,
1163 bool diag, int flags, tsubst_flags_t complain)
1165 tree field;
1166 for (field = fields; field; field = DECL_CHAIN (field))
1168 tree mem_type, argtype, rval;
1170 if (TREE_CODE (field) != FIELD_DECL
1171 || DECL_ARTIFICIAL (field))
1172 continue;
1174 mem_type = strip_array_types (TREE_TYPE (field));
1175 if (assign_p)
1177 bool bad = true;
1178 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
1180 if (diag)
1181 error ("non-static const member %q#D, can%'t use default "
1182 "assignment operator", field);
1184 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1186 if (diag)
1187 error ("non-static reference member %q#D, can%'t use "
1188 "default assignment operator", field);
1190 else
1191 bad = false;
1193 if (bad && deleted_p)
1194 *deleted_p = true;
1196 else if (sfk == sfk_constructor)
1198 bool bad;
1200 if (DECL_INITIAL (field))
1202 if (diag && DECL_INITIAL (field) == error_mark_node)
1203 inform (0, "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 (0, "defaulted default constructor does not "
1255 "initialize %q+#D", field);
1258 else if (sfk == sfk_copy_constructor)
1260 /* 12.8p11b5 */
1261 if (TREE_CODE (mem_type) == REFERENCE_TYPE
1262 && TYPE_REF_IS_RVALUE (mem_type))
1264 if (diag)
1265 error ("copying non-static data member %q#D of rvalue "
1266 "reference type", field);
1267 if (deleted_p)
1268 *deleted_p = true;
1272 if (!CLASS_TYPE_P (mem_type))
1273 continue;
1275 if (ANON_AGGR_TYPE_P (mem_type))
1277 walk_field_subobs (TYPE_FIELDS (mem_type), fnname, sfk, quals,
1278 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1279 deleted_p, constexpr_p,
1280 diag, flags, complain);
1281 continue;
1284 if (copy_arg_p)
1286 int mem_quals = cp_type_quals (mem_type) | quals;
1287 if (DECL_MUTABLE_P (field))
1288 mem_quals &= ~TYPE_QUAL_CONST;
1289 argtype = build_stub_type (mem_type, mem_quals, move_p);
1291 else
1292 argtype = NULL_TREE;
1294 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
1296 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1297 constexpr_p, diag, field);
1301 /* The caller wants to generate an implicit declaration of SFK for CTYPE
1302 which is const if relevant and CONST_P is set. If spec_p, trivial_p and
1303 deleted_p are non-null, set their referent appropriately. If diag is
1304 true, we're either being called from maybe_explain_implicit_delete to
1305 give errors, or if constexpr_p is non-null, from
1306 explain_invalid_constexpr_fn. */
1308 static void
1309 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
1310 tree *spec_p, bool *trivial_p, bool *deleted_p,
1311 bool *constexpr_p, bool diag,
1312 tree inherited_base, tree inherited_parms)
1314 tree binfo, base_binfo, scope, fnname, rval, argtype;
1315 bool move_p, copy_arg_p, assign_p, expected_trivial, check_vdtor;
1316 vec<tree, va_gc> *vbases;
1317 int i, quals, flags;
1318 tsubst_flags_t complain;
1319 bool ctor_p;
1321 if (spec_p)
1322 *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec);
1324 if (deleted_p)
1326 /* "The closure type associated with a lambda-expression has a deleted
1327 default constructor and a deleted copy assignment operator."
1328 This is diagnosed in maybe_explain_implicit_delete. */
1329 if (LAMBDA_TYPE_P (ctype)
1330 && (sfk == sfk_constructor
1331 || sfk == sfk_copy_assignment))
1333 *deleted_p = true;
1334 return;
1337 *deleted_p = false;
1340 ctor_p = false;
1341 assign_p = false;
1342 check_vdtor = false;
1343 switch (sfk)
1345 case sfk_move_assignment:
1346 case sfk_copy_assignment:
1347 assign_p = true;
1348 fnname = ansi_assopname (NOP_EXPR);
1349 break;
1351 case sfk_destructor:
1352 check_vdtor = true;
1353 /* The synthesized method will call base dtors, but check complete
1354 here to avoid having to deal with VTT. */
1355 fnname = complete_dtor_identifier;
1356 break;
1358 case sfk_constructor:
1359 case sfk_move_constructor:
1360 case sfk_copy_constructor:
1361 case sfk_inheriting_constructor:
1362 ctor_p = true;
1363 fnname = complete_ctor_identifier;
1364 break;
1366 default:
1367 gcc_unreachable ();
1370 gcc_assert ((sfk == sfk_inheriting_constructor)
1371 == (inherited_base != NULL_TREE));
1373 /* If that user-written default constructor would satisfy the
1374 requirements of a constexpr constructor (7.1.5), the
1375 implicitly-defined default constructor is constexpr. */
1376 if (constexpr_p)
1377 *constexpr_p = ctor_p;
1379 move_p = false;
1380 switch (sfk)
1382 case sfk_constructor:
1383 case sfk_destructor:
1384 case sfk_inheriting_constructor:
1385 copy_arg_p = false;
1386 break;
1388 case sfk_move_constructor:
1389 case sfk_move_assignment:
1390 move_p = true;
1391 case sfk_copy_constructor:
1392 case sfk_copy_assignment:
1393 copy_arg_p = true;
1394 break;
1396 default:
1397 gcc_unreachable ();
1400 expected_trivial = type_has_trivial_fn (ctype, sfk);
1401 if (trivial_p)
1402 *trivial_p = expected_trivial;
1404 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
1405 class versions and other properties of the type. But a subobject
1406 class can be trivially copyable and yet have overload resolution
1407 choose a template constructor for initialization, depending on
1408 rvalueness and cv-quals. And furthermore, a member in a base might
1409 be trivial but deleted or otherwise not callable. So we can't exit
1410 early in C++0x. The same considerations apply in C++98/03, but
1411 there the definition of triviality does not consider overload
1412 resolution, so a constructor can be trivial even if it would otherwise
1413 call a non-trivial constructor. */
1414 if (expected_trivial
1415 && (!copy_arg_p || cxx_dialect < cxx11))
1417 if (constexpr_p && sfk == sfk_constructor)
1419 bool cx = trivial_default_constructor_is_constexpr (ctype);
1420 *constexpr_p = cx;
1421 if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
1422 /* A trivial constructor doesn't have any NSDMI. */
1423 inform (input_location, "defaulted default constructor does "
1424 "not initialize any non-static data member");
1426 if (!diag && cxx_dialect < cxx11)
1427 return;
1430 ++cp_unevaluated_operand;
1431 ++c_inhibit_evaluation_warnings;
1432 push_deferring_access_checks (dk_no_deferred);
1434 scope = push_scope (ctype);
1436 flags = LOOKUP_NORMAL|LOOKUP_SPECULATIVE;
1437 if (!inherited_base)
1438 flags |= LOOKUP_DEFAULTED;
1440 complain = diag ? tf_warning_or_error : tf_none;
1442 if (const_p)
1443 quals = TYPE_QUAL_CONST;
1444 else
1445 quals = TYPE_UNQUALIFIED;
1446 argtype = NULL_TREE;
1448 for (binfo = TYPE_BINFO (ctype), i = 0;
1449 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
1451 tree basetype = BINFO_TYPE (base_binfo);
1453 if (!assign_p && BINFO_VIRTUAL_P (base_binfo))
1454 /* We'll handle virtual bases below. */
1455 continue;
1457 if (copy_arg_p)
1458 argtype = build_stub_type (basetype, quals, move_p);
1459 else if (basetype == inherited_base)
1460 argtype = inherited_parms;
1461 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1462 if (inherited_base)
1463 argtype = NULL_TREE;
1465 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1466 constexpr_p, diag, basetype);
1467 if (ctor_p)
1469 /* In a constructor we also need to check the subobject
1470 destructors for cleanup of partially constructed objects. */
1471 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1472 NULL_TREE, flags, complain);
1473 /* Note that we don't pass down trivial_p; the subobject
1474 destructors don't affect triviality of the constructor. Nor
1475 do they affect constexpr-ness (a constant expression doesn't
1476 throw) or exception-specification (a throw from one of the
1477 dtors would be a double-fault). */
1478 process_subob_fn (rval, NULL, NULL,
1479 deleted_p, NULL, false,
1480 basetype);
1483 if (check_vdtor && type_has_virtual_destructor (basetype))
1485 rval = locate_fn_flags (ctype, ansi_opname (DELETE_EXPR),
1486 ptr_type_node, flags, complain);
1487 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
1488 to have a null rval (no class-specific op delete). */
1489 if (rval && rval == error_mark_node && deleted_p)
1490 *deleted_p = true;
1491 check_vdtor = false;
1494 if (diag && assign_p && move_p
1495 && BINFO_VIRTUAL_P (base_binfo)
1496 && rval && TREE_CODE (rval) == FUNCTION_DECL
1497 && move_fn_p (rval) && !trivial_fn_p (rval)
1498 && vbase_has_user_provided_move_assign (basetype))
1499 warning (OPT_Wvirtual_move_assign,
1500 "defaulted move assignment for %qT calls a non-trivial "
1501 "move assignment operator for virtual base %qT",
1502 ctype, basetype);
1505 vbases = CLASSTYPE_VBASECLASSES (ctype);
1506 if (vec_safe_is_empty (vbases))
1507 /* No virtual bases to worry about. */;
1508 else if (!assign_p)
1510 if (constexpr_p)
1511 *constexpr_p = false;
1512 FOR_EACH_VEC_ELT (*vbases, i, base_binfo)
1514 tree basetype = BINFO_TYPE (base_binfo);
1515 if (copy_arg_p)
1516 argtype = build_stub_type (basetype, quals, move_p);
1517 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1519 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1520 constexpr_p, diag, basetype);
1521 if (ctor_p && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype))
1523 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1524 NULL_TREE, flags, complain);
1525 process_subob_fn (rval, NULL, NULL,
1526 deleted_p, NULL, false,
1527 basetype);
1532 /* Now handle the non-static data members. */
1533 walk_field_subobs (TYPE_FIELDS (ctype), fnname, sfk, quals,
1534 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1535 deleted_p, constexpr_p,
1536 diag, flags, complain);
1537 if (ctor_p)
1538 walk_field_subobs (TYPE_FIELDS (ctype), complete_dtor_identifier,
1539 sfk_destructor, TYPE_UNQUALIFIED, false,
1540 false, false, NULL, NULL,
1541 deleted_p, NULL,
1542 false, flags, complain);
1544 pop_scope (scope);
1546 pop_deferring_access_checks ();
1547 --cp_unevaluated_operand;
1548 --c_inhibit_evaluation_warnings;
1551 /* DECL is a defaulted function whose exception specification is now
1552 needed. Return what it should be. */
1554 tree
1555 get_defaulted_eh_spec (tree decl)
1557 if (DECL_CLONED_FUNCTION_P (decl))
1558 decl = DECL_CLONED_FUNCTION (decl);
1559 special_function_kind sfk = special_function_p (decl);
1560 tree ctype = DECL_CONTEXT (decl);
1561 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1562 tree parm_type = TREE_VALUE (parms);
1563 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1564 tree spec = empty_except_spec;
1565 synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL,
1566 NULL, false, DECL_INHERITED_CTOR_BASE (decl),
1567 parms);
1568 return spec;
1571 /* DECL is a deleted function. If it's implicitly deleted, explain why and
1572 return true; else return false. */
1574 bool
1575 maybe_explain_implicit_delete (tree decl)
1577 /* If decl is a clone, get the primary variant. */
1578 decl = DECL_ORIGIN (decl);
1579 gcc_assert (DECL_DELETED_FN (decl));
1580 if (DECL_DEFAULTED_FN (decl))
1582 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
1583 static hash_set<tree> *explained;
1585 special_function_kind sfk;
1586 location_t loc;
1587 bool informed;
1588 tree ctype;
1590 if (!explained)
1591 explained = new hash_set<tree>;
1592 if (explained->add (decl))
1593 return true;
1595 sfk = special_function_p (decl);
1596 ctype = DECL_CONTEXT (decl);
1597 loc = input_location;
1598 input_location = DECL_SOURCE_LOCATION (decl);
1600 informed = false;
1601 if (LAMBDA_TYPE_P (ctype))
1603 informed = true;
1604 if (sfk == sfk_constructor)
1605 inform (DECL_SOURCE_LOCATION (decl),
1606 "a lambda closure type has a deleted default constructor");
1607 else if (sfk == sfk_copy_assignment)
1608 inform (DECL_SOURCE_LOCATION (decl),
1609 "a lambda closure type has a deleted copy assignment operator");
1610 else
1611 informed = false;
1613 else if (DECL_ARTIFICIAL (decl)
1614 && (sfk == sfk_copy_assignment
1615 || sfk == sfk_copy_constructor)
1616 && (type_has_user_declared_move_constructor (ctype)
1617 || type_has_user_declared_move_assign (ctype)))
1619 inform (0, "%q+#D is implicitly declared as deleted because %qT "
1620 "declares a move constructor or move assignment operator",
1621 decl, ctype);
1622 informed = true;
1624 if (!informed)
1626 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1627 tree parm_type = TREE_VALUE (parms);
1628 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1629 tree raises = NULL_TREE;
1630 bool deleted_p = false;
1631 tree scope = push_scope (ctype);
1633 synthesized_method_walk (ctype, sfk, const_p,
1634 &raises, NULL, &deleted_p, NULL, false,
1635 DECL_INHERITED_CTOR_BASE (decl), parms);
1636 if (deleted_p)
1638 inform (0, "%q+#D is implicitly deleted because the default "
1639 "definition would be ill-formed:", decl);
1640 synthesized_method_walk (ctype, sfk, const_p,
1641 NULL, NULL, NULL, NULL, true,
1642 DECL_INHERITED_CTOR_BASE (decl), parms);
1644 else if (!comp_except_specs
1645 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
1646 raises, ce_normal))
1647 inform (DECL_SOURCE_LOCATION (decl), "%q#F is implicitly "
1648 "deleted because its exception-specification does not "
1649 "match the implicit exception-specification %qX",
1650 decl, raises);
1651 #ifdef ENABLE_CHECKING
1652 else
1653 gcc_unreachable ();
1654 #endif
1656 pop_scope (scope);
1659 input_location = loc;
1660 return true;
1662 return false;
1665 /* DECL is a defaulted function which was declared constexpr. Explain why
1666 it can't be constexpr. */
1668 void
1669 explain_implicit_non_constexpr (tree decl)
1671 tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl));
1672 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1673 bool dummy;
1674 synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
1675 special_function_p (decl), const_p,
1676 NULL, NULL, NULL, &dummy, true,
1677 DECL_INHERITED_CTOR_BASE (decl),
1678 FUNCTION_FIRST_USER_PARMTYPE (decl));
1681 /* DECL is an instantiation of an inheriting constructor template. Deduce
1682 the correct exception-specification and deletedness for this particular
1683 specialization. */
1685 void
1686 deduce_inheriting_ctor (tree decl)
1688 gcc_assert (DECL_INHERITED_CTOR_BASE (decl));
1689 tree spec;
1690 bool trivial, constexpr_, deleted;
1691 synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor,
1692 false, &spec, &trivial, &deleted, &constexpr_,
1693 /*diag*/false,
1694 DECL_INHERITED_CTOR_BASE (decl),
1695 FUNCTION_FIRST_USER_PARMTYPE (decl));
1696 DECL_DELETED_FN (decl) = deleted;
1697 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
1700 /* Implicitly declare the special function indicated by KIND, as a
1701 member of TYPE. For copy constructors and assignment operators,
1702 CONST_P indicates whether these functions should take a const
1703 reference argument or a non-const reference. Returns the
1704 FUNCTION_DECL for the implicitly declared function. */
1706 tree
1707 implicitly_declare_fn (special_function_kind kind, tree type,
1708 bool const_p, tree inherited_ctor,
1709 tree inherited_parms)
1711 tree fn;
1712 tree parameter_types = void_list_node;
1713 tree return_type;
1714 tree fn_type;
1715 tree raises = empty_except_spec;
1716 tree rhs_parm_type = NULL_TREE;
1717 tree this_parm;
1718 tree name;
1719 HOST_WIDE_INT saved_processing_template_decl;
1720 bool deleted_p;
1721 bool constexpr_p;
1723 /* Because we create declarations for implicitly declared functions
1724 lazily, we may be creating the declaration for a member of TYPE
1725 while in some completely different context. However, TYPE will
1726 never be a dependent class (because we never want to do lookups
1727 for implicitly defined functions in a dependent class).
1728 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
1729 because we only create clones for constructors and destructors
1730 when not in a template. */
1731 gcc_assert (!dependent_type_p (type));
1732 saved_processing_template_decl = processing_template_decl;
1733 processing_template_decl = 0;
1735 type = TYPE_MAIN_VARIANT (type);
1737 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (type))
1739 if (kind == sfk_destructor)
1740 /* See comment in check_special_function_return_type. */
1741 return_type = build_pointer_type (void_type_node);
1742 else
1743 return_type = build_pointer_type (type);
1745 else
1746 return_type = void_type_node;
1748 switch (kind)
1750 case sfk_destructor:
1751 /* Destructor. */
1752 name = constructor_name (type);
1753 break;
1755 case sfk_constructor:
1756 /* Default constructor. */
1757 name = constructor_name (type);
1758 break;
1760 case sfk_copy_constructor:
1761 case sfk_copy_assignment:
1762 case sfk_move_constructor:
1763 case sfk_move_assignment:
1764 case sfk_inheriting_constructor:
1766 bool move_p;
1767 if (kind == sfk_copy_assignment
1768 || kind == sfk_move_assignment)
1770 return_type = build_reference_type (type);
1771 name = ansi_assopname (NOP_EXPR);
1773 else
1774 name = constructor_name (type);
1776 if (kind == sfk_inheriting_constructor)
1777 parameter_types = inherited_parms;
1778 else
1780 if (const_p)
1781 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
1782 else
1783 rhs_parm_type = type;
1784 move_p = (kind == sfk_move_assignment
1785 || kind == sfk_move_constructor);
1786 rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
1788 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
1790 break;
1792 default:
1793 gcc_unreachable ();
1796 tree inherited_base = (inherited_ctor
1797 ? DECL_CONTEXT (inherited_ctor)
1798 : NULL_TREE);
1799 bool trivial_p = false;
1801 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
1803 /* For an inheriting constructor template, just copy these flags from
1804 the inherited constructor template for now. */
1805 raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor));
1806 deleted_p = DECL_DELETED_FN (inherited_ctor);
1807 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
1809 else if (cxx_dialect >= cxx11)
1811 raises = unevaluated_noexcept_spec ();
1812 synthesized_method_walk (type, kind, const_p, NULL, &trivial_p,
1813 &deleted_p, &constexpr_p, false,
1814 inherited_base, inherited_parms);
1816 else
1817 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
1818 &deleted_p, &constexpr_p, false,
1819 inherited_base, inherited_parms);
1820 /* Don't bother marking a deleted constructor as constexpr. */
1821 if (deleted_p)
1822 constexpr_p = false;
1823 /* A trivial copy/move constructor is also a constexpr constructor,
1824 unless the class has virtual bases (7.1.5p4). */
1825 else if (trivial_p && cxx_dialect >= cxx11
1826 && (kind == sfk_copy_constructor
1827 || kind == sfk_move_constructor)
1828 && !CLASSTYPE_VBASECLASSES (type))
1829 gcc_assert (constexpr_p);
1831 if (!trivial_p && type_has_trivial_fn (type, kind))
1832 type_set_nontrivial_flag (type, kind);
1834 /* Create the function. */
1835 fn_type = build_method_type_directly (type, return_type, parameter_types);
1836 if (raises)
1837 fn_type = build_exception_variant (fn_type, raises);
1838 fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
1839 if (kind != sfk_inheriting_constructor)
1840 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
1841 if (kind == sfk_constructor || kind == sfk_copy_constructor
1842 || kind == sfk_move_constructor || kind == sfk_inheriting_constructor)
1843 DECL_CONSTRUCTOR_P (fn) = 1;
1844 else if (kind == sfk_destructor)
1845 DECL_DESTRUCTOR_P (fn) = 1;
1846 else
1848 DECL_ASSIGNMENT_OPERATOR_P (fn) = 1;
1849 SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR);
1852 /* If pointers to member functions use the least significant bit to
1853 indicate whether a function is virtual, ensure a pointer
1854 to this function will have that bit clear. */
1855 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
1856 && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
1857 DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
1859 /* Create the explicit arguments. */
1860 if (rhs_parm_type)
1862 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
1863 want its type to be included in the mangled function
1864 name. */
1865 tree decl = cp_build_parm_decl (NULL_TREE, rhs_parm_type);
1866 TREE_READONLY (decl) = 1;
1867 retrofit_lang_decl (decl);
1868 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
1869 DECL_ARGUMENTS (fn) = decl;
1871 else if (kind == sfk_inheriting_constructor)
1873 tree *p = &DECL_ARGUMENTS (fn);
1874 int index = 1;
1875 for (tree parm = inherited_parms; parm != void_list_node;
1876 parm = TREE_CHAIN (parm))
1878 *p = cp_build_parm_decl (NULL_TREE, TREE_VALUE (parm));
1879 retrofit_lang_decl (*p);
1880 DECL_PARM_LEVEL (*p) = 1;
1881 DECL_PARM_INDEX (*p) = index++;
1882 DECL_CONTEXT (*p) = fn;
1883 p = &DECL_CHAIN (*p);
1885 SET_DECL_INHERITED_CTOR_BASE (fn, inherited_base);
1886 DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor);
1887 /* A constructor so declared has the same access as the corresponding
1888 constructor in X. */
1889 TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor);
1890 TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor);
1891 /* Copy constexpr from the inherited constructor even if the
1892 inheriting constructor doesn't satisfy the requirements. */
1893 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
1895 /* Add the "this" parameter. */
1896 this_parm = build_this_parm (fn_type, TYPE_UNQUALIFIED);
1897 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
1898 DECL_ARGUMENTS (fn) = this_parm;
1900 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
1901 DECL_IN_AGGR_P (fn) = 1;
1902 DECL_ARTIFICIAL (fn) = 1;
1903 DECL_DEFAULTED_FN (fn) = 1;
1904 if (cxx_dialect >= cxx11)
1906 /* "The closure type associated with a lambda-expression has a deleted
1907 default constructor and a deleted copy assignment operator." */
1908 if ((kind == sfk_constructor
1909 || kind == sfk_copy_assignment)
1910 && LAMBDA_TYPE_P (type))
1911 deleted_p = true;
1912 DECL_DELETED_FN (fn) = deleted_p;
1913 DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
1915 DECL_EXTERNAL (fn) = true;
1916 DECL_NOT_REALLY_EXTERN (fn) = 1;
1917 DECL_DECLARED_INLINE_P (fn) = 1;
1918 set_linkage_according_to_type (type, fn);
1919 if (TREE_PUBLIC (fn))
1920 DECL_COMDAT (fn) = 1;
1921 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
1922 gcc_assert (!TREE_USED (fn));
1924 /* Restore PROCESSING_TEMPLATE_DECL. */
1925 processing_template_decl = saved_processing_template_decl;
1927 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
1928 fn = add_inherited_template_parms (fn, inherited_ctor);
1930 /* Warn about calling a non-trivial move assignment in a virtual base. */
1931 if (kind == sfk_move_assignment && !deleted_p && !trivial_p
1932 && CLASSTYPE_VBASECLASSES (type))
1934 location_t loc = input_location;
1935 input_location = DECL_SOURCE_LOCATION (fn);
1936 synthesized_method_walk (type, kind, const_p,
1937 NULL, NULL, NULL, NULL, true,
1938 NULL_TREE, NULL_TREE);
1939 input_location = loc;
1942 return fn;
1945 /* Gives any errors about defaulted functions which need to be deferred
1946 until the containing class is complete. */
1948 void
1949 defaulted_late_check (tree fn)
1951 /* Complain about invalid signature for defaulted fn. */
1952 tree ctx = DECL_CONTEXT (fn);
1953 special_function_kind kind = special_function_p (fn);
1954 bool fn_const_p = (copy_fn_p (fn) == 2);
1955 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
1956 NULL, NULL);
1957 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
1959 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
1960 TREE_TYPE (TREE_TYPE (implicit_fn)))
1961 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1962 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn))))
1964 error ("defaulted declaration %q+D", fn);
1965 error_at (DECL_SOURCE_LOCATION (fn),
1966 "does not match expected signature %qD", implicit_fn);
1969 /* 8.4.2/2: An explicitly-defaulted function (...) may have an explicit
1970 exception-specification only if it is compatible (15.4) with the
1971 exception-specification on the implicit declaration. If a function
1972 is explicitly defaulted on its first declaration, (...) it is
1973 implicitly considered to have the same exception-specification as if
1974 it had been implicitly declared. */
1975 maybe_instantiate_noexcept (fn);
1976 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
1977 if (!fn_spec)
1979 if (DECL_DEFAULTED_IN_CLASS_P (fn))
1980 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
1982 else if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
1983 /* Equivalent to the implicit spec. */;
1984 else if (DECL_DEFAULTED_IN_CLASS_P (fn)
1985 && !CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
1986 /* We can't compare an explicit exception-specification on a
1987 constructor defaulted in the class body to the implicit
1988 exception-specification until after we've parsed any NSDMI; see
1989 after_nsdmi_defaulted_late_checks. */;
1990 else
1992 tree eh_spec = get_defaulted_eh_spec (fn);
1993 if (!comp_except_specs (fn_spec, eh_spec, ce_normal))
1995 if (DECL_DEFAULTED_IN_CLASS_P (fn))
1996 DECL_DELETED_FN (fn) = true;
1997 else
1998 error ("function %q+D defaulted on its redeclaration "
1999 "with an exception-specification that differs from "
2000 "the implicit exception-specification %qX", fn, eh_spec);
2004 if (DECL_DEFAULTED_IN_CLASS_P (fn)
2005 && DECL_DECLARED_CONSTEXPR_P (implicit_fn))
2007 /* Hmm...should we do this for out-of-class too? Should it be OK to
2008 add constexpr later like inline, rather than requiring
2009 declarations to match? */
2010 DECL_DECLARED_CONSTEXPR_P (fn) = true;
2011 if (kind == sfk_constructor)
2012 TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
2015 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
2016 && DECL_DECLARED_CONSTEXPR_P (fn))
2018 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
2020 error ("explicitly defaulted function %q+D cannot be declared "
2021 "as constexpr because the implicit declaration is not "
2022 "constexpr:", fn);
2023 explain_implicit_non_constexpr (fn);
2025 DECL_DECLARED_CONSTEXPR_P (fn) = false;
2028 if (DECL_DELETED_FN (implicit_fn))
2029 DECL_DELETED_FN (fn) = 1;
2032 /* OK, we've parsed the NSDMI for class T, now we can check any explicit
2033 exception-specifications on functions defaulted in the class body. */
2035 void
2036 after_nsdmi_defaulted_late_checks (tree t)
2038 if (uses_template_parms (t))
2039 return;
2040 if (t == error_mark_node)
2041 return;
2042 for (tree fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
2043 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
2045 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2046 if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
2047 continue;
2049 tree eh_spec = get_defaulted_eh_spec (fn);
2050 if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)),
2051 eh_spec, ce_normal))
2052 DECL_DELETED_FN (fn) = true;
2056 /* Returns true iff FN can be explicitly defaulted, and gives any
2057 errors if defaulting FN is ill-formed. */
2059 bool
2060 defaultable_fn_check (tree fn)
2062 special_function_kind kind = sfk_none;
2064 if (template_parm_scope_p ())
2066 error ("a template cannot be defaulted");
2067 return false;
2070 if (DECL_CONSTRUCTOR_P (fn))
2072 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
2073 kind = sfk_constructor;
2074 else if (copy_fn_p (fn) > 0
2075 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
2076 == void_list_node))
2077 kind = sfk_copy_constructor;
2078 else if (move_fn_p (fn))
2079 kind = sfk_move_constructor;
2081 else if (DECL_DESTRUCTOR_P (fn))
2082 kind = sfk_destructor;
2083 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2084 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
2086 if (copy_fn_p (fn))
2087 kind = sfk_copy_assignment;
2088 else if (move_fn_p (fn))
2089 kind = sfk_move_assignment;
2092 if (kind == sfk_none)
2094 error ("%qD cannot be defaulted", fn);
2095 return false;
2097 else
2099 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
2100 t && t != void_list_node; t = TREE_CHAIN (t))
2101 if (TREE_PURPOSE (t))
2103 error ("defaulted function %q+D with default argument", fn);
2104 break;
2107 /* Avoid do_warn_unused_parameter warnings. */
2108 for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p))
2109 if (DECL_NAME (p))
2110 TREE_NO_WARNING (p) = 1;
2112 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn)))
2113 /* Defer checking. */;
2114 else if (!processing_template_decl)
2115 defaulted_late_check (fn);
2117 return true;
2121 /* Add an implicit declaration to TYPE for the kind of function
2122 indicated by SFK. Return the FUNCTION_DECL for the new implicit
2123 declaration. */
2125 tree
2126 lazily_declare_fn (special_function_kind sfk, tree type)
2128 tree fn;
2129 /* Whether or not the argument has a const reference type. */
2130 bool const_p = false;
2132 type = TYPE_MAIN_VARIANT (type);
2134 switch (sfk)
2136 case sfk_constructor:
2137 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
2138 break;
2139 case sfk_copy_constructor:
2140 const_p = TYPE_HAS_CONST_COPY_CTOR (type);
2141 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
2142 break;
2143 case sfk_move_constructor:
2144 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
2145 break;
2146 case sfk_copy_assignment:
2147 const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
2148 CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
2149 break;
2150 case sfk_move_assignment:
2151 CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
2152 break;
2153 case sfk_destructor:
2154 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
2155 break;
2156 default:
2157 gcc_unreachable ();
2160 /* Declare the function. */
2161 fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL);
2163 /* [class.copy]/8 If the class definition declares a move constructor or
2164 move assignment operator, the implicitly declared copy constructor is
2165 defined as deleted.... */
2166 if ((sfk == sfk_copy_assignment
2167 || sfk == sfk_copy_constructor)
2168 && (type_has_user_declared_move_constructor (type)
2169 || type_has_user_declared_move_assign (type)))
2170 DECL_DELETED_FN (fn) = true;
2172 /* A destructor may be virtual. */
2173 if (sfk == sfk_destructor
2174 || sfk == sfk_move_assignment
2175 || sfk == sfk_copy_assignment)
2176 check_for_override (fn, type);
2177 /* Add it to CLASSTYPE_METHOD_VEC. */
2178 add_method (type, fn, NULL_TREE);
2179 /* Add it to TYPE_METHODS. */
2180 if (sfk == sfk_destructor
2181 && DECL_VIRTUAL_P (fn))
2182 /* The ABI requires that a virtual destructor go at the end of the
2183 vtable. */
2184 TYPE_METHODS (type) = chainon (TYPE_METHODS (type), fn);
2185 else
2187 DECL_CHAIN (fn) = TYPE_METHODS (type);
2188 TYPE_METHODS (type) = fn;
2190 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
2191 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
2192 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
2193 /* Create appropriate clones. */
2194 clone_function_decl (fn, /*update_method_vec=*/true);
2196 return fn;
2199 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
2200 as there are artificial parms in FN. */
2202 tree
2203 skip_artificial_parms_for (const_tree fn, tree list)
2205 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2206 list = TREE_CHAIN (list);
2207 else
2208 return list;
2210 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2211 list = TREE_CHAIN (list);
2212 if (DECL_HAS_VTT_PARM_P (fn))
2213 list = TREE_CHAIN (list);
2214 return list;
2217 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
2218 artificial parms in FN. */
2221 num_artificial_parms_for (const_tree fn)
2223 int count = 0;
2225 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2226 count++;
2227 else
2228 return 0;
2230 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2231 count++;
2232 if (DECL_HAS_VTT_PARM_P (fn))
2233 count++;
2234 return count;
2238 #include "gt-cp-method.h"