2015-10-18 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / cp / method.c
blobb3e247c5b1f719c964ee57aa68a098399829fe18
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 static void do_build_copy_assign (tree);
60 static void do_build_copy_constructor (tree);
61 static tree make_alias_for_thunk (tree);
63 /* Called once to initialize method.c. */
65 void
66 init_method (void)
68 init_mangle ();
71 /* Return a this or result adjusting thunk to FUNCTION. THIS_ADJUSTING
72 indicates whether it is a this or result adjusting thunk.
73 FIXED_OFFSET and VIRTUAL_OFFSET indicate how to do the adjustment
74 (see thunk_adjust). VIRTUAL_OFFSET can be NULL, but FIXED_OFFSET
75 never is. VIRTUAL_OFFSET is the /index/ into the vtable for this
76 adjusting thunks, we scale it to a byte offset. For covariant
77 thunks VIRTUAL_OFFSET is the virtual binfo. You must post process
78 the returned thunk with finish_thunk. */
80 tree
81 make_thunk (tree function, bool this_adjusting,
82 tree fixed_offset, tree virtual_offset)
84 HOST_WIDE_INT d;
85 tree thunk;
87 gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
88 /* We can have this thunks to covariant thunks, but not vice versa. */
89 gcc_assert (!DECL_THIS_THUNK_P (function));
90 gcc_assert (!DECL_RESULT_THUNK_P (function) || this_adjusting);
92 /* Scale the VIRTUAL_OFFSET to be in terms of bytes. */
93 if (this_adjusting && virtual_offset)
94 virtual_offset
95 = size_binop (MULT_EXPR,
96 virtual_offset,
97 convert (ssizetype,
98 TYPE_SIZE_UNIT (vtable_entry_type)));
100 d = tree_to_shwi (fixed_offset);
102 /* See if we already have the thunk in question. For this_adjusting
103 thunks VIRTUAL_OFFSET will be an INTEGER_CST, for covariant thunks it
104 will be a BINFO. */
105 for (thunk = DECL_THUNKS (function); thunk; thunk = DECL_CHAIN (thunk))
106 if (DECL_THIS_THUNK_P (thunk) == this_adjusting
107 && THUNK_FIXED_OFFSET (thunk) == d
108 && !virtual_offset == !THUNK_VIRTUAL_OFFSET (thunk)
109 && (!virtual_offset
110 || (this_adjusting
111 ? tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk),
112 virtual_offset)
113 : THUNK_VIRTUAL_OFFSET (thunk) == virtual_offset)))
114 return thunk;
116 /* All thunks must be created before FUNCTION is actually emitted;
117 the ABI requires that all thunks be emitted together with the
118 function to which they transfer control. */
119 gcc_assert (!TREE_ASM_WRITTEN (function));
120 /* Likewise, we can only be adding thunks to a function declared in
121 the class currently being laid out. */
122 gcc_assert (TYPE_SIZE (DECL_CONTEXT (function))
123 && TYPE_BEING_DEFINED (DECL_CONTEXT (function)));
125 thunk = build_decl (DECL_SOURCE_LOCATION (function),
126 FUNCTION_DECL, NULL_TREE, TREE_TYPE (function));
127 DECL_LANG_SPECIFIC (thunk) = DECL_LANG_SPECIFIC (function);
128 cxx_dup_lang_specific_decl (thunk);
129 DECL_VIRTUAL_P (thunk) = true;
130 SET_DECL_THUNKS (thunk, NULL_TREE);
132 DECL_CONTEXT (thunk) = DECL_CONTEXT (function);
133 TREE_READONLY (thunk) = TREE_READONLY (function);
134 TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (function);
135 TREE_PUBLIC (thunk) = TREE_PUBLIC (function);
136 SET_DECL_THUNK_P (thunk, this_adjusting);
137 THUNK_TARGET (thunk) = function;
138 THUNK_FIXED_OFFSET (thunk) = d;
139 THUNK_VIRTUAL_OFFSET (thunk) = virtual_offset;
140 THUNK_ALIAS (thunk) = NULL_TREE;
142 DECL_INTERFACE_KNOWN (thunk) = 1;
143 DECL_NOT_REALLY_EXTERN (thunk) = 1;
144 DECL_COMDAT (thunk) = DECL_COMDAT (function);
145 DECL_SAVED_FUNCTION_DATA (thunk) = NULL;
146 /* The thunk itself is not a constructor or destructor, even if
147 the thing it is thunking to is. */
148 DECL_DESTRUCTOR_P (thunk) = 0;
149 DECL_CONSTRUCTOR_P (thunk) = 0;
150 DECL_EXTERNAL (thunk) = 1;
151 DECL_ARTIFICIAL (thunk) = 1;
152 /* The THUNK is not a pending inline, even if the FUNCTION is. */
153 DECL_PENDING_INLINE_P (thunk) = 0;
154 DECL_DECLARED_INLINE_P (thunk) = 0;
155 /* Nor is it a template instantiation. */
156 DECL_USE_TEMPLATE (thunk) = 0;
157 DECL_TEMPLATE_INFO (thunk) = NULL;
159 /* Add it to the list of thunks associated with FUNCTION. */
160 DECL_CHAIN (thunk) = DECL_THUNKS (function);
161 SET_DECL_THUNKS (function, thunk);
163 return thunk;
166 /* Finish THUNK, a thunk decl. */
168 void
169 finish_thunk (tree thunk)
171 tree function, name;
172 tree fixed_offset = ssize_int (THUNK_FIXED_OFFSET (thunk));
173 tree virtual_offset = THUNK_VIRTUAL_OFFSET (thunk);
175 gcc_assert (!DECL_NAME (thunk) && DECL_THUNK_P (thunk));
176 if (virtual_offset && DECL_RESULT_THUNK_P (thunk))
177 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
178 function = THUNK_TARGET (thunk);
179 name = mangle_thunk (function, DECL_THIS_THUNK_P (thunk),
180 fixed_offset, virtual_offset);
182 /* We can end up with declarations of (logically) different
183 covariant thunks, that do identical adjustments. The two thunks
184 will be adjusting between within different hierarchies, which
185 happen to have the same layout. We must nullify one of them to
186 refer to the other. */
187 if (DECL_RESULT_THUNK_P (thunk))
189 tree cov_probe;
191 for (cov_probe = DECL_THUNKS (function);
192 cov_probe; cov_probe = DECL_CHAIN (cov_probe))
193 if (DECL_NAME (cov_probe) == name)
195 gcc_assert (!DECL_THUNKS (thunk));
196 THUNK_ALIAS (thunk) = (THUNK_ALIAS (cov_probe)
197 ? THUNK_ALIAS (cov_probe) : cov_probe);
198 break;
202 DECL_NAME (thunk) = name;
203 SET_DECL_ASSEMBLER_NAME (thunk, name);
206 static GTY (()) int thunk_labelno;
208 /* Create a static alias to target. */
210 tree
211 make_alias_for (tree target, tree newid)
213 tree alias = build_decl (DECL_SOURCE_LOCATION (target),
214 TREE_CODE (target), newid, TREE_TYPE (target));
215 DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (target);
216 cxx_dup_lang_specific_decl (alias);
217 DECL_CONTEXT (alias) = NULL;
218 TREE_READONLY (alias) = TREE_READONLY (target);
219 TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (target);
220 TREE_PUBLIC (alias) = 0;
221 DECL_INTERFACE_KNOWN (alias) = 1;
222 if (DECL_LANG_SPECIFIC (alias))
224 DECL_NOT_REALLY_EXTERN (alias) = 1;
225 DECL_USE_TEMPLATE (alias) = 0;
226 DECL_TEMPLATE_INFO (alias) = NULL;
228 DECL_EXTERNAL (alias) = 0;
229 DECL_ARTIFICIAL (alias) = 1;
230 DECL_TEMPLATE_INSTANTIATED (alias) = 0;
231 if (TREE_CODE (alias) == FUNCTION_DECL)
233 DECL_SAVED_FUNCTION_DATA (alias) = NULL;
234 DECL_DESTRUCTOR_P (alias) = 0;
235 DECL_CONSTRUCTOR_P (alias) = 0;
236 DECL_PENDING_INLINE_P (alias) = 0;
237 DECL_DECLARED_INLINE_P (alias) = 0;
238 DECL_INITIAL (alias) = error_mark_node;
239 DECL_ARGUMENTS (alias) = copy_list (DECL_ARGUMENTS (target));
241 else
242 TREE_STATIC (alias) = 1;
243 TREE_ADDRESSABLE (alias) = 1;
244 TREE_USED (alias) = 1;
245 SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
246 return alias;
249 static tree
250 make_alias_for_thunk (tree function)
252 tree alias;
253 char buf[256];
255 targetm.asm_out.generate_internal_label (buf, "LTHUNK", thunk_labelno);
256 thunk_labelno++;
258 alias = make_alias_for (function, get_identifier (buf));
260 if (!flag_syntax_only)
262 struct cgraph_node *funcn, *aliasn;
263 funcn = cgraph_node::get (function);
264 gcc_checking_assert (funcn);
265 aliasn = cgraph_node::create_same_body_alias (alias, function);
266 DECL_ASSEMBLER_NAME (function);
267 gcc_assert (aliasn != NULL);
270 return alias;
273 /* Emit the definition of a C++ multiple inheritance or covariant
274 return vtable thunk. If EMIT_P is nonzero, the thunk is emitted
275 immediately. */
277 void
278 use_thunk (tree thunk_fndecl, bool emit_p)
280 tree a, t, function, alias;
281 tree virtual_offset;
282 HOST_WIDE_INT fixed_offset, virtual_value;
283 bool this_adjusting = DECL_THIS_THUNK_P (thunk_fndecl);
284 struct cgraph_node *funcn, *thunk_node;
286 /* We should have called finish_thunk to give it a name. */
287 gcc_assert (DECL_NAME (thunk_fndecl));
289 /* We should never be using an alias, always refer to the
290 aliased thunk. */
291 gcc_assert (!THUNK_ALIAS (thunk_fndecl));
293 if (TREE_ASM_WRITTEN (thunk_fndecl))
294 return;
296 function = THUNK_TARGET (thunk_fndecl);
297 if (DECL_RESULT (thunk_fndecl))
298 /* We already turned this thunk into an ordinary function.
299 There's no need to process this thunk again. */
300 return;
302 if (DECL_THUNK_P (function))
303 /* The target is itself a thunk, process it now. */
304 use_thunk (function, emit_p);
306 /* Thunks are always addressable; they only appear in vtables. */
307 TREE_ADDRESSABLE (thunk_fndecl) = 1;
309 /* Figure out what function is being thunked to. It's referenced in
310 this translation unit. */
311 TREE_ADDRESSABLE (function) = 1;
312 mark_used (function);
313 if (!emit_p)
314 return;
316 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function))
317 alias = make_alias_for_thunk (function);
318 else
319 alias = function;
321 fixed_offset = THUNK_FIXED_OFFSET (thunk_fndecl);
322 virtual_offset = THUNK_VIRTUAL_OFFSET (thunk_fndecl);
324 if (virtual_offset)
326 if (!this_adjusting)
327 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
328 virtual_value = tree_to_shwi (virtual_offset);
329 gcc_assert (virtual_value);
331 else
332 virtual_value = 0;
334 /* And, if we need to emit the thunk, it's used. */
335 mark_used (thunk_fndecl);
336 /* This thunk is actually defined. */
337 DECL_EXTERNAL (thunk_fndecl) = 0;
338 /* The linkage of the function may have changed. FIXME in linkage
339 rewrite. */
340 gcc_assert (DECL_INTERFACE_KNOWN (function));
341 TREE_PUBLIC (thunk_fndecl) = TREE_PUBLIC (function);
342 DECL_VISIBILITY (thunk_fndecl) = DECL_VISIBILITY (function);
343 DECL_VISIBILITY_SPECIFIED (thunk_fndecl)
344 = DECL_VISIBILITY_SPECIFIED (function);
345 DECL_COMDAT (thunk_fndecl) = DECL_COMDAT (function);
346 DECL_WEAK (thunk_fndecl) = DECL_WEAK (function);
348 if (flag_syntax_only)
350 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
351 return;
354 push_to_top_level ();
356 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function)
357 && targetm_common.have_named_sections)
359 tree fn = function;
360 struct symtab_node *symbol;
362 if ((symbol = symtab_node::get (function))
363 && symbol->alias)
365 if (symbol->analyzed)
366 fn = symtab_node::get (function)->ultimate_alias_target ()->decl;
367 else
368 fn = symtab_node::get (function)->alias_target;
370 resolve_unique_section (fn, 0, flag_function_sections);
372 if (DECL_SECTION_NAME (fn) != NULL && DECL_ONE_ONLY (fn))
374 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
376 /* Output the thunk into the same section as function. */
377 set_decl_section_name (thunk_fndecl, DECL_SECTION_NAME (fn));
378 symtab_node::get (thunk_fndecl)->implicit_section
379 = symtab_node::get (fn)->implicit_section;
383 /* Set up cloned argument trees for the thunk. */
384 t = NULL_TREE;
385 for (a = DECL_ARGUMENTS (function); a; a = DECL_CHAIN (a))
387 tree x = copy_node (a);
388 DECL_CHAIN (x) = t;
389 DECL_CONTEXT (x) = thunk_fndecl;
390 SET_DECL_RTL (x, NULL);
391 DECL_HAS_VALUE_EXPR_P (x) = 0;
392 TREE_ADDRESSABLE (x) = 0;
393 t = x;
395 a = nreverse (t);
396 DECL_ARGUMENTS (thunk_fndecl) = a;
397 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
398 funcn = cgraph_node::get (function);
399 gcc_checking_assert (funcn);
400 thunk_node = funcn->create_thunk (thunk_fndecl, function,
401 this_adjusting, fixed_offset, virtual_value,
402 virtual_offset, alias);
403 if (DECL_ONE_ONLY (function))
404 thunk_node->add_to_same_comdat_group (funcn);
406 pop_from_top_level ();
409 /* Code for synthesizing methods which have default semantics defined. */
411 /* True iff CTYPE has a trivial SFK. */
413 static bool
414 type_has_trivial_fn (tree ctype, special_function_kind sfk)
416 switch (sfk)
418 case sfk_constructor:
419 return !TYPE_HAS_COMPLEX_DFLT (ctype);
420 case sfk_copy_constructor:
421 return !TYPE_HAS_COMPLEX_COPY_CTOR (ctype);
422 case sfk_move_constructor:
423 return !TYPE_HAS_COMPLEX_MOVE_CTOR (ctype);
424 case sfk_copy_assignment:
425 return !TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype);
426 case sfk_move_assignment:
427 return !TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype);
428 case sfk_destructor:
429 return !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype);
430 case sfk_inheriting_constructor:
431 return false;
432 default:
433 gcc_unreachable ();
437 /* Note that CTYPE has a non-trivial SFK even though we previously thought
438 it was trivial. */
440 static void
441 type_set_nontrivial_flag (tree ctype, special_function_kind sfk)
443 switch (sfk)
445 case sfk_constructor:
446 TYPE_HAS_COMPLEX_DFLT (ctype) = true;
447 return;
448 case sfk_copy_constructor:
449 TYPE_HAS_COMPLEX_COPY_CTOR (ctype) = true;
450 return;
451 case sfk_move_constructor:
452 TYPE_HAS_COMPLEX_MOVE_CTOR (ctype) = true;
453 return;
454 case sfk_copy_assignment:
455 TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype) = true;
456 return;
457 case sfk_move_assignment:
458 TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype) = true;
459 return;
460 case sfk_destructor:
461 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
462 return;
463 case sfk_inheriting_constructor:
464 default:
465 gcc_unreachable ();
469 /* True iff FN is a trivial defaulted member function ([cd]tor, op=). */
471 bool
472 trivial_fn_p (tree fn)
474 if (TREE_CODE (fn) == TEMPLATE_DECL)
475 return false;
476 if (!DECL_DEFAULTED_FN (fn))
477 return false;
479 /* If fn is a clone, get the primary variant. */
480 if (tree prim = DECL_CLONED_FUNCTION (fn))
481 fn = prim;
482 return type_has_trivial_fn (DECL_CONTEXT (fn), special_function_p (fn));
485 /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO
486 given the parameter or parameters PARM, possibly inherited constructor
487 base INH, or move flag MOVE_P. */
489 static tree
490 add_one_base_init (tree binfo, tree parm, bool move_p, tree inh,
491 tree member_init_list)
493 tree init;
494 if (inh)
496 /* An inheriting constructor only has a mem-initializer for
497 the base it inherits from. */
498 if (BINFO_TYPE (binfo) != inh)
499 return member_init_list;
501 tree *p = &init;
502 init = NULL_TREE;
503 for (; parm; parm = DECL_CHAIN (parm))
505 tree exp = convert_from_reference (parm);
506 if (TREE_CODE (TREE_TYPE (parm)) != REFERENCE_TYPE
507 || TYPE_REF_IS_RVALUE (TREE_TYPE (parm)))
508 exp = move (exp);
509 *p = build_tree_list (NULL_TREE, exp);
510 p = &TREE_CHAIN (*p);
513 else
515 init = build_base_path (PLUS_EXPR, parm, binfo, 1,
516 tf_warning_or_error);
517 if (move_p)
518 init = move (init);
519 init = build_tree_list (NULL_TREE, init);
521 return tree_cons (binfo, init, member_init_list);
524 /* Generate code for default X(X&) or X(X&&) constructor or an inheriting
525 constructor. */
527 static void
528 do_build_copy_constructor (tree fndecl)
530 tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
531 bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
532 bool trivial = trivial_fn_p (fndecl);
533 tree inh = DECL_INHERITED_CTOR_BASE (fndecl);
535 if (!inh)
536 parm = convert_from_reference (parm);
538 if (trivial
539 && is_empty_class (current_class_type))
540 /* Don't copy the padding byte; it might not have been allocated
541 if *this is a base subobject. */;
542 else if (trivial)
544 tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm);
545 finish_expr_stmt (t);
547 else
549 tree fields = TYPE_FIELDS (current_class_type);
550 tree member_init_list = NULL_TREE;
551 int cvquals = cp_type_quals (TREE_TYPE (parm));
552 int i;
553 tree binfo, base_binfo;
554 tree init;
555 vec<tree, va_gc> *vbases;
557 /* Initialize all the base-classes with the parameter converted
558 to their type so that we get their copy constructor and not
559 another constructor that takes current_class_type. We must
560 deal with the binfo's directly as a direct base might be
561 inaccessible due to ambiguity. */
562 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
563 vec_safe_iterate (vbases, i, &binfo); i++)
565 member_init_list = add_one_base_init (binfo, parm, move_p, inh,
566 member_init_list);
569 for (binfo = TYPE_BINFO (current_class_type), i = 0;
570 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
572 if (BINFO_VIRTUAL_P (base_binfo))
573 continue;
574 member_init_list = add_one_base_init (base_binfo, parm, move_p,
575 inh, member_init_list);
578 for (; fields; fields = DECL_CHAIN (fields))
580 tree field = fields;
581 tree expr_type;
583 if (TREE_CODE (field) != FIELD_DECL)
584 continue;
585 if (inh)
586 continue;
588 expr_type = TREE_TYPE (field);
589 if (DECL_NAME (field))
591 if (VFIELD_NAME_P (DECL_NAME (field)))
592 continue;
594 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
595 /* Just use the field; anonymous types can't have
596 nontrivial copy ctors or assignment ops or this
597 function would be deleted. */;
598 else
599 continue;
601 /* Compute the type of "init->field". If the copy-constructor
602 parameter is, for example, "const S&", and the type of
603 the field is "T", then the type will usually be "const
604 T". (There are no cv-qualified variants of reference
605 types.) */
606 if (TREE_CODE (expr_type) != REFERENCE_TYPE)
608 int quals = cvquals;
610 if (DECL_MUTABLE_P (field))
611 quals &= ~TYPE_QUAL_CONST;
612 quals |= cp_type_quals (expr_type);
613 expr_type = cp_build_qualified_type (expr_type, quals);
616 init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
617 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
618 /* 'move' breaks bit-fields, and has no effect for scalars. */
619 && !scalarish_type_p (expr_type))
620 init = move (init);
621 init = build_tree_list (NULL_TREE, init);
623 member_init_list = tree_cons (field, init, member_init_list);
625 finish_mem_initializers (member_init_list);
629 static void
630 do_build_copy_assign (tree fndecl)
632 tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl));
633 tree compound_stmt;
634 bool move_p = move_fn_p (fndecl);
635 bool trivial = trivial_fn_p (fndecl);
636 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
638 compound_stmt = begin_compound_stmt (0);
639 parm = convert_from_reference (parm);
641 if (trivial
642 && is_empty_class (current_class_type))
643 /* Don't copy the padding byte; it might not have been allocated
644 if *this is a base subobject. */;
645 else if (trivial)
647 tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
648 finish_expr_stmt (t);
650 else
652 tree fields;
653 int cvquals = cp_type_quals (TREE_TYPE (parm));
654 int i;
655 tree binfo, base_binfo;
657 /* Assign to each of the direct base classes. */
658 for (binfo = TYPE_BINFO (current_class_type), i = 0;
659 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
661 tree converted_parm;
662 vec<tree, va_gc> *parmvec;
664 /* We must convert PARM directly to the base class
665 explicitly since the base class may be ambiguous. */
666 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
667 tf_warning_or_error);
668 if (move_p)
669 converted_parm = move (converted_parm);
670 /* Call the base class assignment operator. */
671 parmvec = make_tree_vector_single (converted_parm);
672 finish_expr_stmt
673 (build_special_member_call (current_class_ref,
674 ansi_assopname (NOP_EXPR),
675 &parmvec,
676 base_binfo,
677 flags,
678 tf_warning_or_error));
679 release_tree_vector (parmvec);
682 /* Assign to each of the non-static data members. */
683 for (fields = TYPE_FIELDS (current_class_type);
684 fields;
685 fields = DECL_CHAIN (fields))
687 tree comp = current_class_ref;
688 tree init = parm;
689 tree field = fields;
690 tree expr_type;
691 int quals;
693 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
694 continue;
696 expr_type = TREE_TYPE (field);
698 if (CP_TYPE_CONST_P (expr_type))
700 error ("non-static const member %q#D, can%'t use default "
701 "assignment operator", field);
702 continue;
704 else if (TREE_CODE (expr_type) == REFERENCE_TYPE)
706 error ("non-static reference member %q#D, can%'t use "
707 "default assignment operator", field);
708 continue;
711 if (DECL_NAME (field))
713 if (VFIELD_NAME_P (DECL_NAME (field)))
714 continue;
716 else if (ANON_AGGR_TYPE_P (expr_type)
717 && TYPE_FIELDS (expr_type) != NULL_TREE)
718 /* Just use the field; anonymous types can't have
719 nontrivial copy ctors or assignment ops or this
720 function would be deleted. */;
721 else
722 continue;
724 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
726 /* Compute the type of init->field */
727 quals = cvquals;
728 if (DECL_MUTABLE_P (field))
729 quals &= ~TYPE_QUAL_CONST;
730 expr_type = cp_build_qualified_type (expr_type, quals);
732 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
733 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
734 /* 'move' breaks bit-fields, and has no effect for scalars. */
735 && !scalarish_type_p (expr_type))
736 init = move (init);
738 if (DECL_NAME (field))
739 init = cp_build_modify_expr (comp, NOP_EXPR, init,
740 tf_warning_or_error);
741 else
742 init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
743 finish_expr_stmt (init);
746 finish_return_stmt (current_class_ref);
747 finish_compound_stmt (compound_stmt);
750 /* Synthesize FNDECL, a non-static member function. */
752 void
753 synthesize_method (tree fndecl)
755 bool nested = (current_function_decl != NULL_TREE);
756 tree context = decl_function_context (fndecl);
757 bool need_body = true;
758 tree stmt;
759 location_t save_input_location = input_location;
760 int error_count = errorcount;
761 int warning_count = warningcount + werrorcount;
763 /* Reset the source location, we might have been previously
764 deferred, and thus have saved where we were first needed. */
765 DECL_SOURCE_LOCATION (fndecl)
766 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
768 /* If we've been asked to synthesize a clone, just synthesize the
769 cloned function instead. Doing so will automatically fill in the
770 body for the clone. */
771 if (DECL_CLONED_FUNCTION_P (fndecl))
772 fndecl = DECL_CLONED_FUNCTION (fndecl);
774 /* We may be in the middle of deferred access check. Disable
775 it now. */
776 push_deferring_access_checks (dk_no_deferred);
778 if (! context)
779 push_to_top_level ();
780 else if (nested)
781 push_function_context ();
783 input_location = DECL_SOURCE_LOCATION (fndecl);
785 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
786 stmt = begin_function_body ();
788 if (DECL_OVERLOADED_OPERATOR_P (fndecl) == NOP_EXPR)
790 do_build_copy_assign (fndecl);
791 need_body = false;
793 else if (DECL_CONSTRUCTOR_P (fndecl))
795 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
796 if (arg_chain != void_list_node)
797 do_build_copy_constructor (fndecl);
798 else
799 finish_mem_initializers (NULL_TREE);
802 /* If we haven't yet generated the body of the function, just
803 generate an empty compound statement. */
804 if (need_body)
806 tree compound_stmt;
807 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
808 finish_compound_stmt (compound_stmt);
811 finish_function_body (stmt);
812 expand_or_defer_fn (finish_function (0));
814 input_location = save_input_location;
816 if (! context)
817 pop_from_top_level ();
818 else if (nested)
819 pop_function_context ();
821 pop_deferring_access_checks ();
823 if (error_count != errorcount || warning_count != warningcount + werrorcount)
824 inform (input_location, "synthesized method %qD first required here ",
825 fndecl);
828 /* Build a reference to type TYPE with cv-quals QUALS, which is an
829 rvalue if RVALUE is true. */
831 static tree
832 build_stub_type (tree type, int quals, bool rvalue)
834 tree argtype = cp_build_qualified_type (type, quals);
835 return cp_build_reference_type (argtype, rvalue);
838 /* Build a dummy glvalue from dereferencing a dummy reference of type
839 REFTYPE. */
841 static tree
842 build_stub_object (tree reftype)
844 if (TREE_CODE (reftype) != REFERENCE_TYPE)
845 reftype = cp_build_reference_type (reftype, /*rval*/true);
846 tree stub = build1 (CONVERT_EXPR, reftype, integer_one_node);
847 return convert_from_reference (stub);
850 /* Determine which function will be called when looking up NAME in TYPE,
851 called with a single ARGTYPE argument, or no argument if ARGTYPE is
852 null. FLAGS and COMPLAIN are as for build_new_method_call.
854 Returns a FUNCTION_DECL if all is well.
855 Returns NULL_TREE if overload resolution failed.
856 Returns error_mark_node if the chosen function cannot be called. */
858 static tree
859 locate_fn_flags (tree type, tree name, tree argtype, int flags,
860 tsubst_flags_t complain)
862 tree ob, fn, fns, binfo, rval;
863 vec<tree, va_gc> *args;
865 if (TYPE_P (type))
866 binfo = TYPE_BINFO (type);
867 else
869 binfo = type;
870 type = BINFO_TYPE (binfo);
873 ob = build_stub_object (cp_build_reference_type (type, false));
874 args = make_tree_vector ();
875 if (argtype)
877 if (TREE_CODE (argtype) == TREE_LIST)
879 for (tree elt = argtype; elt != void_list_node;
880 elt = TREE_CHAIN (elt))
882 tree type = TREE_VALUE (elt);
883 tree arg = build_stub_object (type);
884 vec_safe_push (args, arg);
887 else
889 tree arg = build_stub_object (argtype);
890 args->quick_push (arg);
894 fns = lookup_fnfields (binfo, name, 0);
895 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
897 release_tree_vector (args);
898 if (fn && rval == error_mark_node)
899 return rval;
900 else
901 return fn;
904 /* Locate the dtor of TYPE. */
906 tree
907 get_dtor (tree type, tsubst_flags_t complain)
909 tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
910 LOOKUP_NORMAL, complain);
911 if (fn == error_mark_node)
912 return NULL_TREE;
913 return fn;
916 /* Locate the default ctor of TYPE. */
918 tree
919 locate_ctor (tree type)
921 tree fn;
923 push_deferring_access_checks (dk_no_check);
924 fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
925 LOOKUP_SPECULATIVE, tf_none);
926 pop_deferring_access_checks ();
927 if (fn == error_mark_node)
928 return NULL_TREE;
929 return fn;
932 /* Likewise, but give any appropriate errors. */
934 tree
935 get_default_ctor (tree type)
937 tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
938 LOOKUP_NORMAL, tf_warning_or_error);
939 if (fn == error_mark_node)
940 return NULL_TREE;
941 return fn;
944 /* Locate the copy ctor of TYPE. */
946 tree
947 get_copy_ctor (tree type, tsubst_flags_t complain)
949 int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
950 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
951 tree argtype = build_stub_type (type, quals, false);
952 tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
953 LOOKUP_NORMAL, complain);
954 if (fn == error_mark_node)
955 return NULL_TREE;
956 return fn;
959 /* Locate the copy assignment operator of TYPE. */
961 tree
962 get_copy_assign (tree type)
964 int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
965 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
966 tree argtype = build_stub_type (type, quals, false);
967 tree fn = locate_fn_flags (type, ansi_assopname (NOP_EXPR), argtype,
968 LOOKUP_NORMAL, tf_warning_or_error);
969 if (fn == error_mark_node)
970 return NULL_TREE;
971 return fn;
974 /* Locate the inherited constructor of constructor CTOR. */
976 tree
977 get_inherited_ctor (tree ctor)
979 gcc_assert (DECL_INHERITED_CTOR_BASE (ctor));
981 push_deferring_access_checks (dk_no_check);
982 tree fn = locate_fn_flags (DECL_INHERITED_CTOR_BASE (ctor),
983 complete_ctor_identifier,
984 FUNCTION_FIRST_USER_PARMTYPE (ctor),
985 LOOKUP_NORMAL|LOOKUP_SPECULATIVE,
986 tf_none);
987 pop_deferring_access_checks ();
988 if (fn == error_mark_node)
989 return NULL_TREE;
990 return fn;
993 /* walk_tree helper function for is_trivially_xible. If *TP is a call,
994 return it if it calls something other than a trivial special member
995 function. */
997 static tree
998 check_nontriv (tree *tp, int *, void *)
1000 tree fn;
1001 if (TREE_CODE (*tp) == CALL_EXPR)
1002 fn = CALL_EXPR_FN (*tp);
1003 else if (TREE_CODE (*tp) == AGGR_INIT_EXPR)
1004 fn = AGGR_INIT_EXPR_FN (*tp);
1005 else
1006 return NULL_TREE;
1008 if (TREE_CODE (fn) == ADDR_EXPR)
1009 fn = TREE_OPERAND (fn, 0);
1011 if (TREE_CODE (fn) != FUNCTION_DECL
1012 || !trivial_fn_p (fn))
1013 return fn;
1014 return NULL_TREE;
1017 /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */
1019 static tree
1020 assignable_expr (tree to, tree from)
1022 ++cp_unevaluated_operand;
1023 to = build_stub_object (to);
1024 from = build_stub_object (from);
1025 tree r = cp_build_modify_expr (to, NOP_EXPR, from, tf_none);
1026 --cp_unevaluated_operand;
1027 return r;
1030 /* The predicate condition for a template specialization
1031 is_constructible<T, Args...> shall be satisfied if and only if the
1032 following variable definition would be well-formed for some invented
1033 variable t: T t(create<Args>()...);
1035 Return something equivalent in well-formedness and triviality. */
1037 static tree
1038 constructible_expr (tree to, tree from)
1040 tree expr;
1041 if (CLASS_TYPE_P (to))
1043 tree ctype = to;
1044 vec<tree, va_gc> *args = NULL;
1045 if (TREE_CODE (to) != REFERENCE_TYPE)
1046 to = cp_build_reference_type (to, /*rval*/false);
1047 tree ob = build_stub_object (to);
1048 for (; from; from = TREE_CHAIN (from))
1049 vec_safe_push (args, build_stub_object (TREE_VALUE (from)));
1050 expr = build_special_member_call (ob, complete_ctor_identifier, &args,
1051 ctype, LOOKUP_NORMAL, tf_none);
1052 if (expr == error_mark_node)
1053 return error_mark_node;
1054 /* The current state of the standard vis-a-vis LWG 2116 is that
1055 is_*constructible involves destruction as well. */
1056 if (type_build_dtor_call (ctype))
1058 tree dtor = build_special_member_call (ob, complete_dtor_identifier,
1059 NULL, ctype, LOOKUP_NORMAL,
1060 tf_none);
1061 if (dtor == error_mark_node)
1062 return error_mark_node;
1063 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
1064 expr = build2 (COMPOUND_EXPR, void_type_node, expr, dtor);
1067 else
1069 if (from == NULL_TREE)
1070 return build_value_init (to, tf_none);
1071 else if (TREE_CHAIN (from))
1072 return error_mark_node; // too many initializers
1073 from = build_stub_object (TREE_VALUE (from));
1074 expr = perform_direct_initialization_if_possible (to, from,
1075 /*cast*/false,
1076 tf_none);
1078 return expr;
1081 /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or
1082 constructible (otherwise) from FROM, which is a single type for
1083 assignment or a list of types for construction. */
1085 bool
1086 is_trivially_xible (enum tree_code code, tree to, tree from)
1088 tree expr;
1089 if (code == MODIFY_EXPR)
1090 expr = assignable_expr (to, from);
1091 else if (from && TREE_CHAIN (from))
1092 return false; // only 0- and 1-argument ctors can be trivial
1093 else
1094 expr = constructible_expr (to, from);
1096 if (expr == error_mark_node)
1097 return false;
1098 tree nt = cp_walk_tree_without_duplicates (&expr, check_nontriv, NULL);
1099 return !nt;
1102 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
1103 DELETED_P or give an error message MSG with argument ARG. */
1105 static void
1106 process_subob_fn (tree fn, tree *spec_p, bool *trivial_p,
1107 bool *deleted_p, bool *constexpr_p,
1108 bool diag, tree arg)
1110 if (!fn || fn == error_mark_node)
1111 goto bad;
1113 if (spec_p)
1115 maybe_instantiate_noexcept (fn);
1116 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
1117 *spec_p = merge_exception_specifiers (*spec_p, raises);
1120 if (!trivial_fn_p (fn))
1122 if (trivial_p)
1123 *trivial_p = false;
1124 if (TREE_CODE (arg) == FIELD_DECL
1125 && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
1127 if (deleted_p)
1128 *deleted_p = true;
1129 if (diag)
1130 error ("union member %q+D with non-trivial %qD", arg, fn);
1134 if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
1136 *constexpr_p = false;
1137 if (diag)
1139 inform (DECL_SOURCE_LOCATION (fn),
1140 "defaulted constructor calls non-constexpr %qD", fn);
1141 explain_invalid_constexpr_fn (fn);
1145 return;
1147 bad:
1148 if (deleted_p)
1149 *deleted_p = true;
1152 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
1153 aggregates. */
1155 static void
1156 walk_field_subobs (tree fields, tree fnname, special_function_kind sfk,
1157 int quals, bool copy_arg_p, bool move_p,
1158 bool assign_p, tree *spec_p, bool *trivial_p,
1159 bool *deleted_p, bool *constexpr_p,
1160 bool diag, int flags, tsubst_flags_t complain)
1162 tree field;
1163 for (field = fields; field; field = DECL_CHAIN (field))
1165 tree mem_type, argtype, rval;
1167 if (TREE_CODE (field) != FIELD_DECL
1168 || DECL_ARTIFICIAL (field))
1169 continue;
1171 mem_type = strip_array_types (TREE_TYPE (field));
1172 if (assign_p)
1174 bool bad = true;
1175 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
1177 if (diag)
1178 error ("non-static const member %q#D, can%'t use default "
1179 "assignment operator", field);
1181 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1183 if (diag)
1184 error ("non-static reference member %q#D, can%'t use "
1185 "default assignment operator", field);
1187 else
1188 bad = false;
1190 if (bad && deleted_p)
1191 *deleted_p = true;
1193 else if (sfk == sfk_constructor)
1195 bool bad;
1197 if (DECL_INITIAL (field))
1199 if (diag && DECL_INITIAL (field) == error_mark_node)
1200 inform (DECL_SOURCE_LOCATION (field),
1201 "initializer for %q#D is invalid", field);
1202 if (trivial_p)
1203 *trivial_p = false;
1204 /* Core 1351: If the field has an NSDMI that could throw, the
1205 default constructor is noexcept(false). */
1206 if (spec_p)
1208 tree nsdmi = get_nsdmi (field, /*ctor*/false);
1209 if (!expr_noexcept_p (nsdmi, complain))
1210 *spec_p = noexcept_false_spec;
1212 /* Don't do the normal processing. */
1213 continue;
1216 bad = false;
1217 if (CP_TYPE_CONST_P (mem_type)
1218 && default_init_uninitialized_part (mem_type))
1220 if (diag)
1222 error ("uninitialized const member in %q#T",
1223 current_class_type);
1224 inform (DECL_SOURCE_LOCATION (field),
1225 "%q#D should be initialized", field);
1227 bad = true;
1229 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1231 if (diag)
1233 error ("uninitialized reference member in %q#T",
1234 current_class_type);
1235 inform (DECL_SOURCE_LOCATION (field),
1236 "%q#D should be initialized", field);
1238 bad = true;
1241 if (bad && deleted_p)
1242 *deleted_p = true;
1244 /* For an implicitly-defined default constructor to be constexpr,
1245 every member must have a user-provided default constructor or
1246 an explicit initializer. */
1247 if (constexpr_p && !CLASS_TYPE_P (mem_type)
1248 && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE)
1250 *constexpr_p = false;
1251 if (diag)
1252 inform (DECL_SOURCE_LOCATION (field),
1253 "defaulted default constructor does not "
1254 "initialize %q#D", field);
1257 else if (sfk == sfk_copy_constructor)
1259 /* 12.8p11b5 */
1260 if (TREE_CODE (mem_type) == REFERENCE_TYPE
1261 && TYPE_REF_IS_RVALUE (mem_type))
1263 if (diag)
1264 error ("copying non-static data member %q#D of rvalue "
1265 "reference type", field);
1266 if (deleted_p)
1267 *deleted_p = true;
1271 if (!CLASS_TYPE_P (mem_type))
1272 continue;
1274 if (ANON_AGGR_TYPE_P (mem_type))
1276 walk_field_subobs (TYPE_FIELDS (mem_type), fnname, sfk, quals,
1277 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1278 deleted_p, constexpr_p,
1279 diag, flags, complain);
1280 continue;
1283 if (copy_arg_p)
1285 int mem_quals = cp_type_quals (mem_type) | quals;
1286 if (DECL_MUTABLE_P (field))
1287 mem_quals &= ~TYPE_QUAL_CONST;
1288 argtype = build_stub_type (mem_type, mem_quals, move_p);
1290 else
1291 argtype = NULL_TREE;
1293 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
1295 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1296 constexpr_p, diag, field);
1300 /* The caller wants to generate an implicit declaration of SFK for CTYPE
1301 which is const if relevant and CONST_P is set. If spec_p, trivial_p and
1302 deleted_p are non-null, set their referent appropriately. If diag is
1303 true, we're either being called from maybe_explain_implicit_delete to
1304 give errors, or if constexpr_p is non-null, from
1305 explain_invalid_constexpr_fn. */
1307 static void
1308 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
1309 tree *spec_p, bool *trivial_p, bool *deleted_p,
1310 bool *constexpr_p, bool diag,
1311 tree inherited_base, tree inherited_parms)
1313 tree binfo, base_binfo, scope, fnname, rval, argtype;
1314 bool move_p, copy_arg_p, assign_p, expected_trivial, check_vdtor;
1315 vec<tree, va_gc> *vbases;
1316 int i, quals, flags;
1317 tsubst_flags_t complain;
1318 bool ctor_p;
1320 if (spec_p)
1321 *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec);
1323 if (deleted_p)
1325 /* "The closure type associated with a lambda-expression has a deleted
1326 default constructor and a deleted copy assignment operator."
1327 This is diagnosed in maybe_explain_implicit_delete. */
1328 if (LAMBDA_TYPE_P (ctype)
1329 && (sfk == sfk_constructor
1330 || sfk == sfk_copy_assignment))
1332 *deleted_p = true;
1333 return;
1336 *deleted_p = false;
1339 ctor_p = false;
1340 assign_p = false;
1341 check_vdtor = false;
1342 switch (sfk)
1344 case sfk_move_assignment:
1345 case sfk_copy_assignment:
1346 assign_p = true;
1347 fnname = ansi_assopname (NOP_EXPR);
1348 break;
1350 case sfk_destructor:
1351 check_vdtor = true;
1352 /* The synthesized method will call base dtors, but check complete
1353 here to avoid having to deal with VTT. */
1354 fnname = complete_dtor_identifier;
1355 break;
1357 case sfk_constructor:
1358 case sfk_move_constructor:
1359 case sfk_copy_constructor:
1360 case sfk_inheriting_constructor:
1361 ctor_p = true;
1362 fnname = complete_ctor_identifier;
1363 break;
1365 default:
1366 gcc_unreachable ();
1369 gcc_assert ((sfk == sfk_inheriting_constructor)
1370 == (inherited_base != NULL_TREE));
1372 /* If that user-written default constructor would satisfy the
1373 requirements of a constexpr constructor (7.1.5), the
1374 implicitly-defined default constructor is constexpr. */
1375 if (constexpr_p)
1376 *constexpr_p = ctor_p;
1378 move_p = false;
1379 switch (sfk)
1381 case sfk_constructor:
1382 case sfk_destructor:
1383 case sfk_inheriting_constructor:
1384 copy_arg_p = false;
1385 break;
1387 case sfk_move_constructor:
1388 case sfk_move_assignment:
1389 move_p = true;
1390 case sfk_copy_constructor:
1391 case sfk_copy_assignment:
1392 copy_arg_p = true;
1393 break;
1395 default:
1396 gcc_unreachable ();
1399 expected_trivial = type_has_trivial_fn (ctype, sfk);
1400 if (trivial_p)
1401 *trivial_p = expected_trivial;
1403 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
1404 class versions and other properties of the type. But a subobject
1405 class can be trivially copyable and yet have overload resolution
1406 choose a template constructor for initialization, depending on
1407 rvalueness and cv-quals. And furthermore, a member in a base might
1408 be trivial but deleted or otherwise not callable. So we can't exit
1409 early in C++0x. The same considerations apply in C++98/03, but
1410 there the definition of triviality does not consider overload
1411 resolution, so a constructor can be trivial even if it would otherwise
1412 call a non-trivial constructor. */
1413 if (expected_trivial
1414 && (!copy_arg_p || cxx_dialect < cxx11))
1416 if (constexpr_p && sfk == sfk_constructor)
1418 bool cx = trivial_default_constructor_is_constexpr (ctype);
1419 *constexpr_p = cx;
1420 if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
1421 /* A trivial constructor doesn't have any NSDMI. */
1422 inform (input_location, "defaulted default constructor does "
1423 "not initialize any non-static data member");
1425 if (!diag && cxx_dialect < cxx11)
1426 return;
1429 ++cp_unevaluated_operand;
1430 ++c_inhibit_evaluation_warnings;
1431 push_deferring_access_checks (dk_no_deferred);
1433 scope = push_scope (ctype);
1435 flags = LOOKUP_NORMAL|LOOKUP_SPECULATIVE;
1436 if (!inherited_base)
1437 flags |= LOOKUP_DEFAULTED;
1439 complain = diag ? tf_warning_or_error : tf_none;
1441 if (const_p)
1442 quals = TYPE_QUAL_CONST;
1443 else
1444 quals = TYPE_UNQUALIFIED;
1445 argtype = NULL_TREE;
1447 for (binfo = TYPE_BINFO (ctype), i = 0;
1448 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
1450 tree basetype = BINFO_TYPE (base_binfo);
1452 if (!assign_p && BINFO_VIRTUAL_P (base_binfo))
1453 /* We'll handle virtual bases below. */
1454 continue;
1456 if (copy_arg_p)
1457 argtype = build_stub_type (basetype, quals, move_p);
1458 else if (basetype == inherited_base)
1459 argtype = inherited_parms;
1460 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1461 if (inherited_base)
1462 argtype = NULL_TREE;
1464 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1465 constexpr_p, diag, basetype);
1466 if (ctor_p)
1468 /* In a constructor we also need to check the subobject
1469 destructors for cleanup of partially constructed objects. */
1470 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1471 NULL_TREE, flags, complain);
1472 /* Note that we don't pass down trivial_p; the subobject
1473 destructors don't affect triviality of the constructor. Nor
1474 do they affect constexpr-ness (a constant expression doesn't
1475 throw) or exception-specification (a throw from one of the
1476 dtors would be a double-fault). */
1477 process_subob_fn (rval, NULL, NULL,
1478 deleted_p, NULL, false,
1479 basetype);
1482 if (check_vdtor && type_has_virtual_destructor (basetype))
1484 rval = locate_fn_flags (ctype, ansi_opname (DELETE_EXPR),
1485 ptr_type_node, flags, complain);
1486 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
1487 to have a null rval (no class-specific op delete). */
1488 if (rval && rval == error_mark_node && deleted_p)
1489 *deleted_p = true;
1490 check_vdtor = false;
1493 if (diag && assign_p && move_p
1494 && BINFO_VIRTUAL_P (base_binfo)
1495 && rval && TREE_CODE (rval) == FUNCTION_DECL
1496 && move_fn_p (rval) && !trivial_fn_p (rval)
1497 && vbase_has_user_provided_move_assign (basetype))
1498 warning (OPT_Wvirtual_move_assign,
1499 "defaulted move assignment for %qT calls a non-trivial "
1500 "move assignment operator for virtual base %qT",
1501 ctype, basetype);
1504 vbases = CLASSTYPE_VBASECLASSES (ctype);
1505 if (vec_safe_is_empty (vbases))
1506 /* No virtual bases to worry about. */;
1507 else if (!assign_p)
1509 if (constexpr_p)
1510 *constexpr_p = false;
1511 FOR_EACH_VEC_ELT (*vbases, i, base_binfo)
1513 tree basetype = BINFO_TYPE (base_binfo);
1514 if (copy_arg_p)
1515 argtype = build_stub_type (basetype, quals, move_p);
1516 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1518 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1519 constexpr_p, diag, basetype);
1520 if (ctor_p && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype))
1522 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1523 NULL_TREE, flags, complain);
1524 process_subob_fn (rval, NULL, NULL,
1525 deleted_p, NULL, false,
1526 basetype);
1531 /* Now handle the non-static data members. */
1532 walk_field_subobs (TYPE_FIELDS (ctype), fnname, sfk, quals,
1533 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1534 deleted_p, constexpr_p,
1535 diag, flags, complain);
1536 if (ctor_p)
1537 walk_field_subobs (TYPE_FIELDS (ctype), complete_dtor_identifier,
1538 sfk_destructor, TYPE_UNQUALIFIED, false,
1539 false, false, NULL, NULL,
1540 deleted_p, NULL,
1541 false, flags, complain);
1543 pop_scope (scope);
1545 pop_deferring_access_checks ();
1546 --cp_unevaluated_operand;
1547 --c_inhibit_evaluation_warnings;
1550 /* DECL is a defaulted function whose exception specification is now
1551 needed. Return what it should be. */
1553 tree
1554 get_defaulted_eh_spec (tree decl)
1556 if (DECL_CLONED_FUNCTION_P (decl))
1557 decl = DECL_CLONED_FUNCTION (decl);
1558 special_function_kind sfk = special_function_p (decl);
1559 tree ctype = DECL_CONTEXT (decl);
1560 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1561 tree parm_type = TREE_VALUE (parms);
1562 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1563 tree spec = empty_except_spec;
1564 synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL,
1565 NULL, false, DECL_INHERITED_CTOR_BASE (decl),
1566 parms);
1567 return spec;
1570 /* DECL is a deleted function. If it's implicitly deleted, explain why and
1571 return true; else return false. */
1573 bool
1574 maybe_explain_implicit_delete (tree decl)
1576 /* If decl is a clone, get the primary variant. */
1577 decl = DECL_ORIGIN (decl);
1578 gcc_assert (DECL_DELETED_FN (decl));
1579 if (DECL_DEFAULTED_FN (decl))
1581 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
1582 static hash_set<tree> *explained;
1584 special_function_kind sfk;
1585 location_t loc;
1586 bool informed;
1587 tree ctype;
1589 if (!explained)
1590 explained = new hash_set<tree>;
1591 if (explained->add (decl))
1592 return true;
1594 sfk = special_function_p (decl);
1595 ctype = DECL_CONTEXT (decl);
1596 loc = input_location;
1597 input_location = DECL_SOURCE_LOCATION (decl);
1599 informed = false;
1600 if (LAMBDA_TYPE_P (ctype))
1602 informed = true;
1603 if (sfk == sfk_constructor)
1604 inform (DECL_SOURCE_LOCATION (decl),
1605 "a lambda closure type has a deleted default constructor");
1606 else if (sfk == sfk_copy_assignment)
1607 inform (DECL_SOURCE_LOCATION (decl),
1608 "a lambda closure type has a deleted copy assignment operator");
1609 else
1610 informed = false;
1612 else if (DECL_ARTIFICIAL (decl)
1613 && (sfk == sfk_copy_assignment
1614 || sfk == sfk_copy_constructor)
1615 && (type_has_user_declared_move_constructor (ctype)
1616 || type_has_user_declared_move_assign (ctype)))
1618 inform (DECL_SOURCE_LOCATION (decl),
1619 "%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 (DECL_SOURCE_LOCATION (decl),
1639 "%q#D is implicitly deleted because the default "
1640 "definition would be ill-formed:", decl);
1641 synthesized_method_walk (ctype, sfk, const_p,
1642 NULL, NULL, NULL, NULL, true,
1643 DECL_INHERITED_CTOR_BASE (decl), parms);
1645 else if (!comp_except_specs
1646 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
1647 raises, ce_normal))
1648 inform (DECL_SOURCE_LOCATION (decl), "%q#F is implicitly "
1649 "deleted because its exception-specification does not "
1650 "match the implicit exception-specification %qX",
1651 decl, raises);
1652 #ifdef ENABLE_CHECKING
1653 else
1654 gcc_unreachable ();
1655 #endif
1657 pop_scope (scope);
1660 input_location = loc;
1661 return true;
1663 return false;
1666 /* DECL is a defaulted function which was declared constexpr. Explain why
1667 it can't be constexpr. */
1669 void
1670 explain_implicit_non_constexpr (tree decl)
1672 tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl));
1673 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1674 bool dummy;
1675 synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
1676 special_function_p (decl), const_p,
1677 NULL, NULL, NULL, &dummy, true,
1678 DECL_INHERITED_CTOR_BASE (decl),
1679 FUNCTION_FIRST_USER_PARMTYPE (decl));
1682 /* DECL is an instantiation of an inheriting constructor template. Deduce
1683 the correct exception-specification and deletedness for this particular
1684 specialization. */
1686 void
1687 deduce_inheriting_ctor (tree decl)
1689 gcc_assert (DECL_INHERITED_CTOR_BASE (decl));
1690 tree spec;
1691 bool trivial, constexpr_, deleted;
1692 synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor,
1693 false, &spec, &trivial, &deleted, &constexpr_,
1694 /*diag*/false,
1695 DECL_INHERITED_CTOR_BASE (decl),
1696 FUNCTION_FIRST_USER_PARMTYPE (decl));
1697 DECL_DELETED_FN (decl) = deleted;
1698 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
1701 /* Implicitly declare the special function indicated by KIND, as a
1702 member of TYPE. For copy constructors and assignment operators,
1703 CONST_P indicates whether these functions should take a const
1704 reference argument or a non-const reference. Returns the
1705 FUNCTION_DECL for the implicitly declared function. */
1707 tree
1708 implicitly_declare_fn (special_function_kind kind, tree type,
1709 bool const_p, tree inherited_ctor,
1710 tree inherited_parms)
1712 tree fn;
1713 tree parameter_types = void_list_node;
1714 tree return_type;
1715 tree fn_type;
1716 tree raises = empty_except_spec;
1717 tree rhs_parm_type = NULL_TREE;
1718 tree this_parm;
1719 tree name;
1720 HOST_WIDE_INT saved_processing_template_decl;
1721 bool deleted_p;
1722 bool constexpr_p;
1724 /* Because we create declarations for implicitly declared functions
1725 lazily, we may be creating the declaration for a member of TYPE
1726 while in some completely different context. However, TYPE will
1727 never be a dependent class (because we never want to do lookups
1728 for implicitly defined functions in a dependent class).
1729 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
1730 because we only create clones for constructors and destructors
1731 when not in a template. */
1732 gcc_assert (!dependent_type_p (type));
1733 saved_processing_template_decl = processing_template_decl;
1734 processing_template_decl = 0;
1736 type = TYPE_MAIN_VARIANT (type);
1738 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (type))
1740 if (kind == sfk_destructor)
1741 /* See comment in check_special_function_return_type. */
1742 return_type = build_pointer_type (void_type_node);
1743 else
1744 return_type = build_pointer_type (type);
1746 else
1747 return_type = void_type_node;
1749 switch (kind)
1751 case sfk_destructor:
1752 /* Destructor. */
1753 name = constructor_name (type);
1754 break;
1756 case sfk_constructor:
1757 /* Default constructor. */
1758 name = constructor_name (type);
1759 break;
1761 case sfk_copy_constructor:
1762 case sfk_copy_assignment:
1763 case sfk_move_constructor:
1764 case sfk_move_assignment:
1765 case sfk_inheriting_constructor:
1767 bool move_p;
1768 if (kind == sfk_copy_assignment
1769 || kind == sfk_move_assignment)
1771 return_type = build_reference_type (type);
1772 name = ansi_assopname (NOP_EXPR);
1774 else
1775 name = constructor_name (type);
1777 if (kind == sfk_inheriting_constructor)
1778 parameter_types = inherited_parms;
1779 else
1781 if (const_p)
1782 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
1783 else
1784 rhs_parm_type = type;
1785 move_p = (kind == sfk_move_assignment
1786 || kind == sfk_move_constructor);
1787 rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
1789 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
1791 break;
1793 default:
1794 gcc_unreachable ();
1797 tree inherited_base = (inherited_ctor
1798 ? DECL_CONTEXT (inherited_ctor)
1799 : NULL_TREE);
1800 bool trivial_p = false;
1802 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
1804 /* For an inheriting constructor template, just copy these flags from
1805 the inherited constructor template for now. */
1806 raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor));
1807 deleted_p = DECL_DELETED_FN (inherited_ctor);
1808 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
1810 else if (cxx_dialect >= cxx11)
1812 raises = unevaluated_noexcept_spec ();
1813 synthesized_method_walk (type, kind, const_p, NULL, &trivial_p,
1814 &deleted_p, &constexpr_p, false,
1815 inherited_base, inherited_parms);
1817 else
1818 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
1819 &deleted_p, &constexpr_p, false,
1820 inherited_base, inherited_parms);
1821 /* Don't bother marking a deleted constructor as constexpr. */
1822 if (deleted_p)
1823 constexpr_p = false;
1824 /* A trivial copy/move constructor is also a constexpr constructor,
1825 unless the class has virtual bases (7.1.5p4). */
1826 else if (trivial_p && cxx_dialect >= cxx11
1827 && (kind == sfk_copy_constructor
1828 || kind == sfk_move_constructor)
1829 && !CLASSTYPE_VBASECLASSES (type))
1830 gcc_assert (constexpr_p);
1832 if (!trivial_p && type_has_trivial_fn (type, kind))
1833 type_set_nontrivial_flag (type, kind);
1835 /* Create the function. */
1836 fn_type = build_method_type_directly (type, return_type, parameter_types);
1837 if (raises)
1838 fn_type = build_exception_variant (fn_type, raises);
1839 fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
1840 if (kind != sfk_inheriting_constructor)
1841 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
1842 if (kind == sfk_constructor || kind == sfk_copy_constructor
1843 || kind == sfk_move_constructor || kind == sfk_inheriting_constructor)
1844 DECL_CONSTRUCTOR_P (fn) = 1;
1845 else if (kind == sfk_destructor)
1846 DECL_DESTRUCTOR_P (fn) = 1;
1847 else
1849 DECL_ASSIGNMENT_OPERATOR_P (fn) = 1;
1850 SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR);
1853 /* If pointers to member functions use the least significant bit to
1854 indicate whether a function is virtual, ensure a pointer
1855 to this function will have that bit clear. */
1856 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
1857 && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
1858 DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
1860 /* Create the explicit arguments. */
1861 if (rhs_parm_type)
1863 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
1864 want its type to be included in the mangled function
1865 name. */
1866 tree decl = cp_build_parm_decl (NULL_TREE, rhs_parm_type);
1867 TREE_READONLY (decl) = 1;
1868 retrofit_lang_decl (decl);
1869 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
1870 DECL_ARGUMENTS (fn) = decl;
1872 else if (kind == sfk_inheriting_constructor)
1874 tree *p = &DECL_ARGUMENTS (fn);
1875 int index = 1;
1876 for (tree parm = inherited_parms; parm != void_list_node;
1877 parm = TREE_CHAIN (parm))
1879 *p = cp_build_parm_decl (NULL_TREE, TREE_VALUE (parm));
1880 retrofit_lang_decl (*p);
1881 DECL_PARM_LEVEL (*p) = 1;
1882 DECL_PARM_INDEX (*p) = index++;
1883 DECL_CONTEXT (*p) = fn;
1884 p = &DECL_CHAIN (*p);
1886 SET_DECL_INHERITED_CTOR_BASE (fn, inherited_base);
1887 DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor);
1888 /* A constructor so declared has the same access as the corresponding
1889 constructor in X. */
1890 TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor);
1891 TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor);
1892 /* Copy constexpr from the inherited constructor even if the
1893 inheriting constructor doesn't satisfy the requirements. */
1894 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
1896 /* Add the "this" parameter. */
1897 this_parm = build_this_parm (fn_type, TYPE_UNQUALIFIED);
1898 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
1899 DECL_ARGUMENTS (fn) = this_parm;
1901 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
1902 DECL_IN_AGGR_P (fn) = 1;
1903 DECL_ARTIFICIAL (fn) = 1;
1904 DECL_DEFAULTED_FN (fn) = 1;
1905 if (cxx_dialect >= cxx11)
1907 /* "The closure type associated with a lambda-expression has a deleted
1908 default constructor and a deleted copy assignment operator." */
1909 if ((kind == sfk_constructor
1910 || kind == sfk_copy_assignment)
1911 && LAMBDA_TYPE_P (type))
1912 deleted_p = true;
1913 DECL_DELETED_FN (fn) = deleted_p;
1914 DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
1916 DECL_EXTERNAL (fn) = true;
1917 DECL_NOT_REALLY_EXTERN (fn) = 1;
1918 DECL_DECLARED_INLINE_P (fn) = 1;
1919 set_linkage_according_to_type (type, fn);
1920 if (TREE_PUBLIC (fn))
1921 DECL_COMDAT (fn) = 1;
1922 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
1923 gcc_assert (!TREE_USED (fn));
1925 /* Propagate constraints from the inherited constructor. */
1926 if (flag_concepts && inherited_ctor)
1927 if (tree orig_ci = get_constraints (inherited_ctor))
1929 tree new_ci = copy_node (orig_ci);
1930 set_constraints (fn, new_ci);
1933 /* Restore PROCESSING_TEMPLATE_DECL. */
1934 processing_template_decl = saved_processing_template_decl;
1936 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
1937 fn = add_inherited_template_parms (fn, inherited_ctor);
1939 /* Warn about calling a non-trivial move assignment in a virtual base. */
1940 if (kind == sfk_move_assignment && !deleted_p && !trivial_p
1941 && CLASSTYPE_VBASECLASSES (type))
1943 location_t loc = input_location;
1944 input_location = DECL_SOURCE_LOCATION (fn);
1945 synthesized_method_walk (type, kind, const_p,
1946 NULL, NULL, NULL, NULL, true,
1947 NULL_TREE, NULL_TREE);
1948 input_location = loc;
1951 return fn;
1954 /* Gives any errors about defaulted functions which need to be deferred
1955 until the containing class is complete. */
1957 void
1958 defaulted_late_check (tree fn)
1960 /* Complain about invalid signature for defaulted fn. */
1961 tree ctx = DECL_CONTEXT (fn);
1962 special_function_kind kind = special_function_p (fn);
1963 bool fn_const_p = (copy_fn_p (fn) == 2);
1964 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
1965 NULL, NULL);
1966 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
1968 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
1969 TREE_TYPE (TREE_TYPE (implicit_fn)))
1970 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1971 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn))))
1973 error ("defaulted declaration %q+D", fn);
1974 error_at (DECL_SOURCE_LOCATION (fn),
1975 "does not match expected signature %qD", implicit_fn);
1978 /* 8.4.2/2: An explicitly-defaulted function (...) may have an explicit
1979 exception-specification only if it is compatible (15.4) with the
1980 exception-specification on the implicit declaration. If a function
1981 is explicitly defaulted on its first declaration, (...) it is
1982 implicitly considered to have the same exception-specification as if
1983 it had been implicitly declared. */
1984 maybe_instantiate_noexcept (fn);
1985 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
1986 if (!fn_spec)
1988 if (DECL_DEFAULTED_IN_CLASS_P (fn))
1989 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
1991 else if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
1992 /* Equivalent to the implicit spec. */;
1993 else if (DECL_DEFAULTED_IN_CLASS_P (fn)
1994 && !CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
1995 /* We can't compare an explicit exception-specification on a
1996 constructor defaulted in the class body to the implicit
1997 exception-specification until after we've parsed any NSDMI; see
1998 after_nsdmi_defaulted_late_checks. */;
1999 else
2001 tree eh_spec = get_defaulted_eh_spec (fn);
2002 if (!comp_except_specs (fn_spec, eh_spec, ce_normal))
2004 if (DECL_DEFAULTED_IN_CLASS_P (fn))
2005 DECL_DELETED_FN (fn) = true;
2006 else
2007 error ("function %q+D defaulted on its redeclaration "
2008 "with an exception-specification that differs from "
2009 "the implicit exception-specification %qX", fn, eh_spec);
2013 if (DECL_DEFAULTED_IN_CLASS_P (fn)
2014 && DECL_DECLARED_CONSTEXPR_P (implicit_fn))
2016 /* Hmm...should we do this for out-of-class too? Should it be OK to
2017 add constexpr later like inline, rather than requiring
2018 declarations to match? */
2019 DECL_DECLARED_CONSTEXPR_P (fn) = true;
2020 if (kind == sfk_constructor)
2021 TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
2024 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
2025 && DECL_DECLARED_CONSTEXPR_P (fn))
2027 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
2029 error ("explicitly defaulted function %q+D cannot be declared "
2030 "as constexpr because the implicit declaration is not "
2031 "constexpr:", fn);
2032 explain_implicit_non_constexpr (fn);
2034 DECL_DECLARED_CONSTEXPR_P (fn) = false;
2037 if (DECL_DELETED_FN (implicit_fn))
2038 DECL_DELETED_FN (fn) = 1;
2041 /* OK, we've parsed the NSDMI for class T, now we can check any explicit
2042 exception-specifications on functions defaulted in the class body. */
2044 void
2045 after_nsdmi_defaulted_late_checks (tree t)
2047 if (uses_template_parms (t))
2048 return;
2049 if (t == error_mark_node)
2050 return;
2051 for (tree fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
2052 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
2054 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2055 if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
2056 continue;
2058 tree eh_spec = get_defaulted_eh_spec (fn);
2059 if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)),
2060 eh_spec, ce_normal))
2061 DECL_DELETED_FN (fn) = true;
2065 /* Returns true iff FN can be explicitly defaulted, and gives any
2066 errors if defaulting FN is ill-formed. */
2068 bool
2069 defaultable_fn_check (tree fn)
2071 special_function_kind kind = sfk_none;
2073 if (template_parm_scope_p ())
2075 error ("a template cannot be defaulted");
2076 return false;
2079 if (DECL_CONSTRUCTOR_P (fn))
2081 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
2082 kind = sfk_constructor;
2083 else if (copy_fn_p (fn) > 0
2084 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
2085 == void_list_node))
2086 kind = sfk_copy_constructor;
2087 else if (move_fn_p (fn))
2088 kind = sfk_move_constructor;
2090 else if (DECL_DESTRUCTOR_P (fn))
2091 kind = sfk_destructor;
2092 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2093 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
2095 if (copy_fn_p (fn))
2096 kind = sfk_copy_assignment;
2097 else if (move_fn_p (fn))
2098 kind = sfk_move_assignment;
2101 if (kind == sfk_none)
2103 error ("%qD cannot be defaulted", fn);
2104 return false;
2106 else
2108 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
2109 t && t != void_list_node; t = TREE_CHAIN (t))
2110 if (TREE_PURPOSE (t))
2112 error ("defaulted function %q+D with default argument", fn);
2113 break;
2116 /* Avoid do_warn_unused_parameter warnings. */
2117 for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p))
2118 if (DECL_NAME (p))
2119 TREE_NO_WARNING (p) = 1;
2121 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn)))
2122 /* Defer checking. */;
2123 else if (!processing_template_decl)
2124 defaulted_late_check (fn);
2126 return true;
2130 /* Add an implicit declaration to TYPE for the kind of function
2131 indicated by SFK. Return the FUNCTION_DECL for the new implicit
2132 declaration. */
2134 tree
2135 lazily_declare_fn (special_function_kind sfk, tree type)
2137 tree fn;
2138 /* Whether or not the argument has a const reference type. */
2139 bool const_p = false;
2141 type = TYPE_MAIN_VARIANT (type);
2143 switch (sfk)
2145 case sfk_constructor:
2146 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
2147 break;
2148 case sfk_copy_constructor:
2149 const_p = TYPE_HAS_CONST_COPY_CTOR (type);
2150 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
2151 break;
2152 case sfk_move_constructor:
2153 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
2154 break;
2155 case sfk_copy_assignment:
2156 const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
2157 CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
2158 break;
2159 case sfk_move_assignment:
2160 CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
2161 break;
2162 case sfk_destructor:
2163 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
2164 break;
2165 default:
2166 gcc_unreachable ();
2169 /* Declare the function. */
2170 fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL);
2172 /* [class.copy]/8 If the class definition declares a move constructor or
2173 move assignment operator, the implicitly declared copy constructor is
2174 defined as deleted.... */
2175 if ((sfk == sfk_copy_assignment
2176 || sfk == sfk_copy_constructor)
2177 && (type_has_user_declared_move_constructor (type)
2178 || type_has_user_declared_move_assign (type)))
2179 DECL_DELETED_FN (fn) = true;
2181 /* A destructor may be virtual. */
2182 if (sfk == sfk_destructor
2183 || sfk == sfk_move_assignment
2184 || sfk == sfk_copy_assignment)
2185 check_for_override (fn, type);
2186 /* Add it to CLASSTYPE_METHOD_VEC. */
2187 add_method (type, fn, NULL_TREE);
2188 /* Add it to TYPE_METHODS. */
2189 if (sfk == sfk_destructor
2190 && DECL_VIRTUAL_P (fn))
2191 /* The ABI requires that a virtual destructor go at the end of the
2192 vtable. */
2193 TYPE_METHODS (type) = chainon (TYPE_METHODS (type), fn);
2194 else
2196 DECL_CHAIN (fn) = TYPE_METHODS (type);
2197 TYPE_METHODS (type) = fn;
2199 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
2200 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
2201 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
2202 /* Create appropriate clones. */
2203 clone_function_decl (fn, /*update_method_vec=*/true);
2205 return fn;
2208 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
2209 as there are artificial parms in FN. */
2211 tree
2212 skip_artificial_parms_for (const_tree fn, tree list)
2214 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2215 list = TREE_CHAIN (list);
2216 else
2217 return list;
2219 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2220 list = TREE_CHAIN (list);
2221 if (DECL_HAS_VTT_PARM_P (fn))
2222 list = TREE_CHAIN (list);
2223 return list;
2226 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
2227 artificial parms in FN. */
2230 num_artificial_parms_for (const_tree fn)
2232 int count = 0;
2234 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2235 count++;
2236 else
2237 return 0;
2239 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2240 count++;
2241 if (DECL_HAS_VTT_PARM_P (fn))
2242 count++;
2243 return count;
2247 #include "gt-cp-method.h"