Merge from trunk:
[official-gcc.git] / main / gcc / cp / method.c
blob1fa4be8d55229d7eddf4cb00cd31851f3e78a339
1 /* Handle the hair of processing (but not expanding) inline functions.
2 Also manage function and variable name overloading.
3 Copyright (C) 1987-2014 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 "tree.h"
29 #include "stringpool.h"
30 #include "varasm.h"
31 #include "cp-tree.h"
32 #include "flags.h"
33 #include "toplev.h"
34 #include "tm_p.h"
35 #include "target.h"
36 #include "common/common-target.h"
37 #include "diagnostic.h"
38 #include "cgraph.h"
39 #include "pointer-set.h"
41 /* Various flags to control the mangling process. */
43 enum mangling_flags
45 /* No flags. */
46 mf_none = 0,
47 /* The thing we are presently mangling is part of a template type,
48 rather than a fully instantiated type. Therefore, we may see
49 complex expressions where we would normally expect to see a
50 simple integer constant. */
51 mf_maybe_uninstantiated = 1,
52 /* When mangling a numeric value, use the form `_XX_' (instead of
53 just `XX') if the value has more than one digit. */
54 mf_use_underscores_around_value = 2
57 typedef enum mangling_flags mangling_flags;
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_get_node (function);
264 gcc_checking_assert (funcn);
265 aliasn = cgraph_same_body_alias (funcn, 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_get_node (function))
363 && symbol->alias)
365 if (symbol->analyzed)
366 fn = symtab_alias_ultimate_target (symtab_get_node (function))->decl;
367 else
368 fn = symtab_get_node (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_get_node (thunk_fndecl)->implicit_section
379 = symtab_get_node (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_get_node (function);
399 gcc_checking_assert (funcn);
400 thunk_node = cgraph_add_thunk (funcn, thunk_fndecl, function,
401 this_adjusting, fixed_offset, virtual_value,
402 virtual_offset, alias);
403 if (DECL_ONE_ONLY (function))
404 symtab_add_to_same_comdat_group (thunk_node,
405 funcn);
407 if (!this_adjusting
408 || !targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
409 virtual_value, alias))
411 /* If this is a covariant thunk, or we don't have the necessary
412 code for efficient thunks, generate a thunk function that
413 just makes a call to the real function. Unfortunately, this
414 doesn't work for varargs. */
416 if (varargs_function_p (function))
417 error ("generic thunk code fails for method %q#D which uses %<...%>",
418 function);
421 pop_from_top_level ();
424 /* Code for synthesizing methods which have default semantics defined. */
426 /* True iff CTYPE has a trivial SFK. */
428 static bool
429 type_has_trivial_fn (tree ctype, special_function_kind sfk)
431 switch (sfk)
433 case sfk_constructor:
434 return !TYPE_HAS_COMPLEX_DFLT (ctype);
435 case sfk_copy_constructor:
436 return !TYPE_HAS_COMPLEX_COPY_CTOR (ctype);
437 case sfk_move_constructor:
438 return !TYPE_HAS_COMPLEX_MOVE_CTOR (ctype);
439 case sfk_copy_assignment:
440 return !TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype);
441 case sfk_move_assignment:
442 return !TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype);
443 case sfk_destructor:
444 return !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype);
445 case sfk_inheriting_constructor:
446 return false;
447 default:
448 gcc_unreachable ();
452 /* Note that CTYPE has a non-trivial SFK even though we previously thought
453 it was trivial. */
455 static void
456 type_set_nontrivial_flag (tree ctype, special_function_kind sfk)
458 switch (sfk)
460 case sfk_constructor:
461 TYPE_HAS_COMPLEX_DFLT (ctype) = true;
462 return;
463 case sfk_copy_constructor:
464 TYPE_HAS_COMPLEX_COPY_CTOR (ctype) = true;
465 return;
466 case sfk_move_constructor:
467 TYPE_HAS_COMPLEX_MOVE_CTOR (ctype) = true;
468 return;
469 case sfk_copy_assignment:
470 TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype) = true;
471 return;
472 case sfk_move_assignment:
473 TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype) = true;
474 return;
475 case sfk_destructor:
476 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
477 return;
478 case sfk_inheriting_constructor:
479 default:
480 gcc_unreachable ();
484 /* True iff FN is a trivial defaulted member function ([cd]tor, op=). */
486 bool
487 trivial_fn_p (tree fn)
489 if (!DECL_DEFAULTED_FN (fn))
490 return false;
492 /* If fn is a clone, get the primary variant. */
493 if (tree prim = DECL_CLONED_FUNCTION (fn))
494 fn = prim;
495 return type_has_trivial_fn (DECL_CONTEXT (fn), special_function_p (fn));
498 /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO
499 given the parameter or parameters PARM, possibly inherited constructor
500 base INH, or move flag MOVE_P. */
502 static tree
503 add_one_base_init (tree binfo, tree parm, bool move_p, tree inh,
504 tree member_init_list)
506 tree init;
507 if (inh)
509 /* An inheriting constructor only has a mem-initializer for
510 the base it inherits from. */
511 if (BINFO_TYPE (binfo) != inh)
512 return member_init_list;
514 tree *p = &init;
515 init = NULL_TREE;
516 for (; parm; parm = DECL_CHAIN (parm))
518 tree exp = convert_from_reference (parm);
519 if (TREE_CODE (TREE_TYPE (parm)) != REFERENCE_TYPE
520 || TYPE_REF_IS_RVALUE (TREE_TYPE (parm)))
521 exp = move (exp);
522 *p = build_tree_list (NULL_TREE, exp);
523 p = &TREE_CHAIN (*p);
526 else
528 init = build_base_path (PLUS_EXPR, parm, binfo, 1,
529 tf_warning_or_error);
530 if (move_p)
531 init = move (init);
532 init = build_tree_list (NULL_TREE, init);
534 return tree_cons (binfo, init, member_init_list);
537 /* Generate code for default X(X&) or X(X&&) constructor or an inheriting
538 constructor. */
540 static void
541 do_build_copy_constructor (tree fndecl)
543 tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
544 bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
545 bool trivial = trivial_fn_p (fndecl);
546 tree inh = DECL_INHERITED_CTOR_BASE (fndecl);
548 if (!inh)
549 parm = convert_from_reference (parm);
551 if (trivial
552 && is_empty_class (current_class_type))
553 /* Don't copy the padding byte; it might not have been allocated
554 if *this is a base subobject. */;
555 else if (trivial)
557 tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm);
558 finish_expr_stmt (t);
560 else
562 tree fields = TYPE_FIELDS (current_class_type);
563 tree member_init_list = NULL_TREE;
564 int cvquals = cp_type_quals (TREE_TYPE (parm));
565 int i;
566 tree binfo, base_binfo;
567 tree init;
568 vec<tree, va_gc> *vbases;
570 /* Initialize all the base-classes with the parameter converted
571 to their type so that we get their copy constructor and not
572 another constructor that takes current_class_type. We must
573 deal with the binfo's directly as a direct base might be
574 inaccessible due to ambiguity. */
575 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
576 vec_safe_iterate (vbases, i, &binfo); i++)
578 member_init_list = add_one_base_init (binfo, parm, move_p, inh,
579 member_init_list);
582 for (binfo = TYPE_BINFO (current_class_type), i = 0;
583 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
585 if (BINFO_VIRTUAL_P (base_binfo))
586 continue;
587 member_init_list = add_one_base_init (base_binfo, parm, move_p,
588 inh, member_init_list);
591 for (; fields; fields = DECL_CHAIN (fields))
593 tree field = fields;
594 tree expr_type;
596 if (TREE_CODE (field) != FIELD_DECL)
597 continue;
598 if (inh)
599 continue;
601 expr_type = TREE_TYPE (field);
602 if (DECL_NAME (field))
604 if (VFIELD_NAME_P (DECL_NAME (field)))
605 continue;
607 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
608 /* Just use the field; anonymous types can't have
609 nontrivial copy ctors or assignment ops or this
610 function would be deleted. */;
611 else
612 continue;
614 /* Compute the type of "init->field". If the copy-constructor
615 parameter is, for example, "const S&", and the type of
616 the field is "T", then the type will usually be "const
617 T". (There are no cv-qualified variants of reference
618 types.) */
619 if (TREE_CODE (expr_type) != REFERENCE_TYPE)
621 int quals = cvquals;
623 if (DECL_MUTABLE_P (field))
624 quals &= ~TYPE_QUAL_CONST;
625 quals |= cp_type_quals (expr_type);
626 expr_type = cp_build_qualified_type (expr_type, quals);
629 init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
630 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
631 /* 'move' breaks bit-fields, and has no effect for scalars. */
632 && !scalarish_type_p (expr_type))
633 init = move (init);
634 init = build_tree_list (NULL_TREE, init);
636 member_init_list = tree_cons (field, init, member_init_list);
638 finish_mem_initializers (member_init_list);
642 static void
643 do_build_copy_assign (tree fndecl)
645 tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl));
646 tree compound_stmt;
647 bool move_p = move_fn_p (fndecl);
648 bool trivial = trivial_fn_p (fndecl);
649 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
651 compound_stmt = begin_compound_stmt (0);
652 parm = convert_from_reference (parm);
654 if (trivial
655 && is_empty_class (current_class_type))
656 /* Don't copy the padding byte; it might not have been allocated
657 if *this is a base subobject. */;
658 else if (trivial)
660 tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
661 finish_expr_stmt (t);
663 else
665 tree fields;
666 int cvquals = cp_type_quals (TREE_TYPE (parm));
667 int i;
668 tree binfo, base_binfo;
670 /* Assign to each of the direct base classes. */
671 for (binfo = TYPE_BINFO (current_class_type), i = 0;
672 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
674 tree converted_parm;
675 vec<tree, va_gc> *parmvec;
677 /* We must convert PARM directly to the base class
678 explicitly since the base class may be ambiguous. */
679 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
680 tf_warning_or_error);
681 if (move_p)
682 converted_parm = move (converted_parm);
683 /* Call the base class assignment operator. */
684 parmvec = make_tree_vector_single (converted_parm);
685 finish_expr_stmt
686 (build_special_member_call (current_class_ref,
687 ansi_assopname (NOP_EXPR),
688 &parmvec,
689 base_binfo,
690 flags,
691 tf_warning_or_error));
692 release_tree_vector (parmvec);
695 /* Assign to each of the non-static data members. */
696 for (fields = TYPE_FIELDS (current_class_type);
697 fields;
698 fields = DECL_CHAIN (fields))
700 tree comp = current_class_ref;
701 tree init = parm;
702 tree field = fields;
703 tree expr_type;
704 int quals;
706 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
707 continue;
709 expr_type = TREE_TYPE (field);
711 if (CP_TYPE_CONST_P (expr_type))
713 error ("non-static const member %q#D, can%'t use default "
714 "assignment operator", field);
715 continue;
717 else if (TREE_CODE (expr_type) == REFERENCE_TYPE)
719 error ("non-static reference member %q#D, can%'t use "
720 "default assignment operator", field);
721 continue;
724 if (DECL_NAME (field))
726 if (VFIELD_NAME_P (DECL_NAME (field)))
727 continue;
729 else if (ANON_AGGR_TYPE_P (expr_type)
730 && TYPE_FIELDS (expr_type) != NULL_TREE)
731 /* Just use the field; anonymous types can't have
732 nontrivial copy ctors or assignment ops or this
733 function would be deleted. */;
734 else
735 continue;
737 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
739 /* Compute the type of init->field */
740 quals = cvquals;
741 if (DECL_MUTABLE_P (field))
742 quals &= ~TYPE_QUAL_CONST;
743 expr_type = cp_build_qualified_type (expr_type, quals);
745 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
746 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
747 /* 'move' breaks bit-fields, and has no effect for scalars. */
748 && !scalarish_type_p (expr_type))
749 init = move (init);
751 if (DECL_NAME (field))
752 init = cp_build_modify_expr (comp, NOP_EXPR, init,
753 tf_warning_or_error);
754 else
755 init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
756 finish_expr_stmt (init);
759 finish_return_stmt (current_class_ref);
760 finish_compound_stmt (compound_stmt);
763 /* Synthesize FNDECL, a non-static member function. */
765 void
766 synthesize_method (tree fndecl)
768 bool nested = (current_function_decl != NULL_TREE);
769 tree context = decl_function_context (fndecl);
770 bool need_body = true;
771 tree stmt;
772 location_t save_input_location = input_location;
773 int error_count = errorcount;
774 int warning_count = warningcount + werrorcount;
776 /* Reset the source location, we might have been previously
777 deferred, and thus have saved where we were first needed. */
778 DECL_SOURCE_LOCATION (fndecl)
779 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
781 /* If we've been asked to synthesize a clone, just synthesize the
782 cloned function instead. Doing so will automatically fill in the
783 body for the clone. */
784 if (DECL_CLONED_FUNCTION_P (fndecl))
785 fndecl = DECL_CLONED_FUNCTION (fndecl);
787 /* We may be in the middle of deferred access check. Disable
788 it now. */
789 push_deferring_access_checks (dk_no_deferred);
791 if (! context)
792 push_to_top_level ();
793 else if (nested)
794 push_function_context ();
796 input_location = DECL_SOURCE_LOCATION (fndecl);
798 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
799 stmt = begin_function_body ();
801 if (DECL_OVERLOADED_OPERATOR_P (fndecl) == NOP_EXPR)
803 do_build_copy_assign (fndecl);
804 need_body = false;
806 else if (DECL_CONSTRUCTOR_P (fndecl))
808 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
809 if (arg_chain != void_list_node)
810 do_build_copy_constructor (fndecl);
811 else
812 finish_mem_initializers (NULL_TREE);
815 /* If we haven't yet generated the body of the function, just
816 generate an empty compound statement. */
817 if (need_body)
819 tree compound_stmt;
820 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
821 finish_compound_stmt (compound_stmt);
824 finish_function_body (stmt);
825 expand_or_defer_fn (finish_function (0));
827 input_location = save_input_location;
829 if (! context)
830 pop_from_top_level ();
831 else if (nested)
832 pop_function_context ();
834 pop_deferring_access_checks ();
836 if (error_count != errorcount || warning_count != warningcount + werrorcount)
837 inform (input_location, "synthesized method %qD first required here ",
838 fndecl);
841 /* Build a reference to type TYPE with cv-quals QUALS, which is an
842 rvalue if RVALUE is true. */
844 static tree
845 build_stub_type (tree type, int quals, bool rvalue)
847 tree argtype = cp_build_qualified_type (type, quals);
848 return cp_build_reference_type (argtype, rvalue);
851 /* Build a dummy glvalue from dereferencing a dummy reference of type
852 REFTYPE. */
854 static tree
855 build_stub_object (tree reftype)
857 tree stub = build1 (NOP_EXPR, reftype, integer_one_node);
858 return convert_from_reference (stub);
861 /* Determine which function will be called when looking up NAME in TYPE,
862 called with a single ARGTYPE argument, or no argument if ARGTYPE is
863 null. FLAGS and COMPLAIN are as for build_new_method_call.
865 Returns a FUNCTION_DECL if all is well.
866 Returns NULL_TREE if overload resolution failed.
867 Returns error_mark_node if the chosen function cannot be called. */
869 static tree
870 locate_fn_flags (tree type, tree name, tree argtype, int flags,
871 tsubst_flags_t complain)
873 tree ob, fn, fns, binfo, rval;
874 vec<tree, va_gc> *args;
876 if (TYPE_P (type))
877 binfo = TYPE_BINFO (type);
878 else
880 binfo = type;
881 type = BINFO_TYPE (binfo);
884 ob = build_stub_object (cp_build_reference_type (type, false));
885 args = make_tree_vector ();
886 if (argtype)
888 if (TREE_CODE (argtype) == TREE_LIST)
890 for (tree elt = argtype; elt != void_list_node;
891 elt = TREE_CHAIN (elt))
893 tree type = TREE_VALUE (elt);
894 if (TREE_CODE (type) != REFERENCE_TYPE)
895 type = cp_build_reference_type (type, /*rval*/true);
896 tree arg = build_stub_object (type);
897 vec_safe_push (args, arg);
900 else
902 tree arg = build_stub_object (argtype);
903 args->quick_push (arg);
907 fns = lookup_fnfields (binfo, name, 0);
908 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
910 release_tree_vector (args);
911 if (fn && rval == error_mark_node)
912 return rval;
913 else
914 return fn;
917 /* Locate the dtor of TYPE. */
919 tree
920 get_dtor (tree type, tsubst_flags_t complain)
922 tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
923 LOOKUP_NORMAL, complain);
924 if (fn == error_mark_node)
925 return NULL_TREE;
926 return fn;
929 /* Locate the default ctor of TYPE. */
931 tree
932 locate_ctor (tree type)
934 tree fn;
936 push_deferring_access_checks (dk_no_check);
937 fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
938 LOOKUP_SPECULATIVE, tf_none);
939 pop_deferring_access_checks ();
940 if (fn == error_mark_node)
941 return NULL_TREE;
942 return fn;
945 /* Likewise, but give any appropriate errors. */
947 tree
948 get_default_ctor (tree type)
950 tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
951 LOOKUP_NORMAL, tf_warning_or_error);
952 if (fn == error_mark_node)
953 return NULL_TREE;
954 return fn;
957 /* Locate the copy ctor of TYPE. */
959 tree
960 get_copy_ctor (tree type, tsubst_flags_t complain)
962 int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
963 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
964 tree argtype = build_stub_type (type, quals, false);
965 tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
966 LOOKUP_NORMAL, complain);
967 if (fn == error_mark_node)
968 return NULL_TREE;
969 return fn;
972 /* Locate the copy assignment operator of TYPE. */
974 tree
975 get_copy_assign (tree type)
977 int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
978 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
979 tree argtype = build_stub_type (type, quals, false);
980 tree fn = locate_fn_flags (type, ansi_assopname (NOP_EXPR), argtype,
981 LOOKUP_NORMAL, tf_warning_or_error);
982 if (fn == error_mark_node)
983 return NULL_TREE;
984 return fn;
987 /* Locate the inherited constructor of constructor CTOR. */
989 tree
990 get_inherited_ctor (tree ctor)
992 gcc_assert (DECL_INHERITED_CTOR_BASE (ctor));
994 push_deferring_access_checks (dk_no_check);
995 tree fn = locate_fn_flags (DECL_INHERITED_CTOR_BASE (ctor),
996 complete_ctor_identifier,
997 FUNCTION_FIRST_USER_PARMTYPE (ctor),
998 LOOKUP_NORMAL|LOOKUP_SPECULATIVE,
999 tf_none);
1000 pop_deferring_access_checks ();
1001 if (fn == error_mark_node)
1002 return NULL_TREE;
1003 return fn;
1006 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
1007 DELETED_P or give an error message MSG with argument ARG. */
1009 static void
1010 process_subob_fn (tree fn, tree *spec_p, bool *trivial_p,
1011 bool *deleted_p, bool *constexpr_p,
1012 bool diag, tree arg)
1014 if (!fn || fn == error_mark_node)
1015 goto bad;
1017 if (spec_p)
1019 maybe_instantiate_noexcept (fn);
1020 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
1021 *spec_p = merge_exception_specifiers (*spec_p, raises);
1024 if (!trivial_fn_p (fn))
1026 if (trivial_p)
1027 *trivial_p = false;
1028 if (TREE_CODE (arg) == FIELD_DECL
1029 && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
1031 if (deleted_p)
1032 *deleted_p = true;
1033 if (diag)
1034 error ("union member %q+D with non-trivial %qD", arg, fn);
1038 if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
1040 *constexpr_p = false;
1041 if (diag)
1043 inform (0, "defaulted constructor calls non-constexpr "
1044 "%q+D", fn);
1045 explain_invalid_constexpr_fn (fn);
1049 return;
1051 bad:
1052 if (deleted_p)
1053 *deleted_p = true;
1056 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
1057 aggregates. */
1059 static void
1060 walk_field_subobs (tree fields, tree fnname, special_function_kind sfk,
1061 int quals, bool copy_arg_p, bool move_p,
1062 bool assign_p, tree *spec_p, bool *trivial_p,
1063 bool *deleted_p, bool *constexpr_p,
1064 bool diag, int flags, tsubst_flags_t complain)
1066 tree field;
1067 for (field = fields; field; field = DECL_CHAIN (field))
1069 tree mem_type, argtype, rval;
1071 if (TREE_CODE (field) != FIELD_DECL
1072 || DECL_ARTIFICIAL (field))
1073 continue;
1075 mem_type = strip_array_types (TREE_TYPE (field));
1076 if (assign_p)
1078 bool bad = true;
1079 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
1081 if (diag)
1082 error ("non-static const member %q#D, can%'t use default "
1083 "assignment operator", field);
1085 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1087 if (diag)
1088 error ("non-static reference member %q#D, can%'t use "
1089 "default assignment operator", field);
1091 else
1092 bad = false;
1094 if (bad && deleted_p)
1095 *deleted_p = true;
1097 else if (sfk == sfk_constructor)
1099 bool bad;
1101 if (DECL_INITIAL (field))
1103 if (diag && DECL_INITIAL (field) == error_mark_node)
1104 inform (0, "initializer for %q+#D is invalid", field);
1105 if (trivial_p)
1106 *trivial_p = false;
1107 /* Core 1351: If the field has an NSDMI that could throw, the
1108 default constructor is noexcept(false). */
1109 if (spec_p)
1111 tree nsdmi = get_nsdmi (field, /*ctor*/false);
1112 if (!expr_noexcept_p (nsdmi, complain))
1113 *spec_p = noexcept_false_spec;
1115 /* Don't do the normal processing. */
1116 continue;
1119 bad = false;
1120 if (CP_TYPE_CONST_P (mem_type)
1121 && default_init_uninitialized_part (mem_type))
1123 if (diag)
1125 error ("uninitialized const member in %q#T",
1126 current_class_type);
1127 inform (DECL_SOURCE_LOCATION (field),
1128 "%q#D should be initialized", field);
1130 bad = true;
1132 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1134 if (diag)
1136 error ("uninitialized reference member in %q#T",
1137 current_class_type);
1138 inform (DECL_SOURCE_LOCATION (field),
1139 "%q#D should be initialized", field);
1141 bad = true;
1144 if (bad && deleted_p)
1145 *deleted_p = true;
1147 /* For an implicitly-defined default constructor to be constexpr,
1148 every member must have a user-provided default constructor or
1149 an explicit initializer. */
1150 if (constexpr_p && !CLASS_TYPE_P (mem_type)
1151 && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE)
1153 *constexpr_p = false;
1154 if (diag)
1155 inform (0, "defaulted default constructor does not "
1156 "initialize %q+#D", field);
1159 else if (sfk == sfk_copy_constructor)
1161 /* 12.8p11b5 */
1162 if (TREE_CODE (mem_type) == REFERENCE_TYPE
1163 && TYPE_REF_IS_RVALUE (mem_type))
1165 if (diag)
1166 error ("copying non-static data member %q#D of rvalue "
1167 "reference type", field);
1168 if (deleted_p)
1169 *deleted_p = true;
1173 if (!CLASS_TYPE_P (mem_type))
1174 continue;
1176 if (ANON_AGGR_TYPE_P (mem_type))
1178 walk_field_subobs (TYPE_FIELDS (mem_type), fnname, sfk, quals,
1179 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1180 deleted_p, constexpr_p,
1181 diag, flags, complain);
1182 continue;
1185 if (copy_arg_p)
1187 int mem_quals = cp_type_quals (mem_type) | quals;
1188 if (DECL_MUTABLE_P (field))
1189 mem_quals &= ~TYPE_QUAL_CONST;
1190 argtype = build_stub_type (mem_type, mem_quals, move_p);
1192 else
1193 argtype = NULL_TREE;
1195 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
1197 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1198 constexpr_p, diag, field);
1202 /* The caller wants to generate an implicit declaration of SFK for CTYPE
1203 which is const if relevant and CONST_P is set. If spec_p, trivial_p and
1204 deleted_p are non-null, set their referent appropriately. If diag is
1205 true, we're either being called from maybe_explain_implicit_delete to
1206 give errors, or if constexpr_p is non-null, from
1207 explain_invalid_constexpr_fn. */
1209 static void
1210 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
1211 tree *spec_p, bool *trivial_p, bool *deleted_p,
1212 bool *constexpr_p, bool diag,
1213 tree inherited_base, tree inherited_parms)
1215 tree binfo, base_binfo, scope, fnname, rval, argtype;
1216 bool move_p, copy_arg_p, assign_p, expected_trivial, check_vdtor;
1217 vec<tree, va_gc> *vbases;
1218 int i, quals, flags;
1219 tsubst_flags_t complain;
1220 bool ctor_p;
1222 if (spec_p)
1223 *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec);
1225 if (deleted_p)
1227 /* "The closure type associated with a lambda-expression has a deleted
1228 default constructor and a deleted copy assignment operator."
1229 This is diagnosed in maybe_explain_implicit_delete. */
1230 if (LAMBDA_TYPE_P (ctype)
1231 && (sfk == sfk_constructor
1232 || sfk == sfk_copy_assignment))
1234 *deleted_p = true;
1235 return;
1238 *deleted_p = false;
1241 ctor_p = false;
1242 assign_p = false;
1243 check_vdtor = false;
1244 switch (sfk)
1246 case sfk_move_assignment:
1247 case sfk_copy_assignment:
1248 assign_p = true;
1249 fnname = ansi_assopname (NOP_EXPR);
1250 break;
1252 case sfk_destructor:
1253 check_vdtor = true;
1254 /* The synthesized method will call base dtors, but check complete
1255 here to avoid having to deal with VTT. */
1256 fnname = complete_dtor_identifier;
1257 break;
1259 case sfk_constructor:
1260 case sfk_move_constructor:
1261 case sfk_copy_constructor:
1262 case sfk_inheriting_constructor:
1263 ctor_p = true;
1264 fnname = complete_ctor_identifier;
1265 break;
1267 default:
1268 gcc_unreachable ();
1271 gcc_assert ((sfk == sfk_inheriting_constructor)
1272 == (inherited_base != NULL_TREE));
1274 /* If that user-written default constructor would satisfy the
1275 requirements of a constexpr constructor (7.1.5), the
1276 implicitly-defined default constructor is constexpr. */
1277 if (constexpr_p)
1278 *constexpr_p = ctor_p;
1280 move_p = false;
1281 switch (sfk)
1283 case sfk_constructor:
1284 case sfk_destructor:
1285 case sfk_inheriting_constructor:
1286 copy_arg_p = false;
1287 break;
1289 case sfk_move_constructor:
1290 case sfk_move_assignment:
1291 move_p = true;
1292 case sfk_copy_constructor:
1293 case sfk_copy_assignment:
1294 copy_arg_p = true;
1295 break;
1297 default:
1298 gcc_unreachable ();
1301 expected_trivial = type_has_trivial_fn (ctype, sfk);
1302 if (trivial_p)
1303 *trivial_p = expected_trivial;
1305 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
1306 class versions and other properties of the type. But a subobject
1307 class can be trivially copyable and yet have overload resolution
1308 choose a template constructor for initialization, depending on
1309 rvalueness and cv-quals. And furthermore, a member in a base might
1310 be trivial but deleted or otherwise not callable. So we can't exit
1311 early in C++0x. The same considerations apply in C++98/03, but
1312 there the definition of triviality does not consider overload
1313 resolution, so a constructor can be trivial even if it would otherwise
1314 call a non-trivial constructor. */
1315 if (expected_trivial
1316 && (!copy_arg_p || cxx_dialect < cxx11))
1318 if (constexpr_p && sfk == sfk_constructor)
1320 bool cx = trivial_default_constructor_is_constexpr (ctype);
1321 *constexpr_p = cx;
1322 if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
1323 /* A trivial constructor doesn't have any NSDMI. */
1324 inform (input_location, "defaulted default constructor does "
1325 "not initialize any non-static data member");
1327 if (!diag && cxx_dialect < cxx11)
1328 return;
1331 ++cp_unevaluated_operand;
1332 ++c_inhibit_evaluation_warnings;
1333 push_deferring_access_checks (dk_no_deferred);
1335 scope = push_scope (ctype);
1337 flags = LOOKUP_NORMAL|LOOKUP_SPECULATIVE;
1338 if (!inherited_base)
1339 flags |= LOOKUP_DEFAULTED;
1341 complain = diag ? tf_warning_or_error : tf_none;
1343 if (const_p)
1344 quals = TYPE_QUAL_CONST;
1345 else
1346 quals = TYPE_UNQUALIFIED;
1347 argtype = NULL_TREE;
1349 for (binfo = TYPE_BINFO (ctype), i = 0;
1350 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
1352 tree basetype = BINFO_TYPE (base_binfo);
1354 if (!assign_p && BINFO_VIRTUAL_P (base_binfo))
1355 /* We'll handle virtual bases below. */
1356 continue;
1358 if (copy_arg_p)
1359 argtype = build_stub_type (basetype, quals, move_p);
1360 else if (basetype == inherited_base)
1361 argtype = inherited_parms;
1362 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1363 if (inherited_base)
1364 argtype = NULL_TREE;
1366 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1367 constexpr_p, diag, basetype);
1368 if (ctor_p)
1370 /* In a constructor we also need to check the subobject
1371 destructors for cleanup of partially constructed objects. */
1372 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1373 NULL_TREE, flags, complain);
1374 /* Note that we don't pass down trivial_p; the subobject
1375 destructors don't affect triviality of the constructor. Nor
1376 do they affect constexpr-ness (a constant expression doesn't
1377 throw) or exception-specification (a throw from one of the
1378 dtors would be a double-fault). */
1379 process_subob_fn (rval, NULL, NULL,
1380 deleted_p, NULL, false,
1381 basetype);
1384 if (check_vdtor && type_has_virtual_destructor (basetype))
1386 rval = locate_fn_flags (ctype, ansi_opname (DELETE_EXPR),
1387 ptr_type_node, flags, complain);
1388 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
1389 to have a null rval (no class-specific op delete). */
1390 if (rval && rval == error_mark_node && deleted_p)
1391 *deleted_p = true;
1392 check_vdtor = false;
1395 if (diag && assign_p && move_p
1396 && BINFO_VIRTUAL_P (base_binfo)
1397 && rval && TREE_CODE (rval) == FUNCTION_DECL
1398 && move_fn_p (rval) && !trivial_fn_p (rval)
1399 && vbase_has_user_provided_move_assign (basetype))
1400 warning (OPT_Wvirtual_move_assign,
1401 "defaulted move assignment for %qT calls a non-trivial "
1402 "move assignment operator for virtual base %qT",
1403 ctype, basetype);
1406 vbases = CLASSTYPE_VBASECLASSES (ctype);
1407 if (vec_safe_is_empty (vbases))
1408 /* No virtual bases to worry about. */;
1409 else if (!assign_p)
1411 if (constexpr_p)
1412 *constexpr_p = false;
1413 FOR_EACH_VEC_ELT (*vbases, i, base_binfo)
1415 tree basetype = BINFO_TYPE (base_binfo);
1416 if (copy_arg_p)
1417 argtype = build_stub_type (basetype, quals, move_p);
1418 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1420 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1421 constexpr_p, diag, basetype);
1422 if (ctor_p && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype))
1424 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1425 NULL_TREE, flags, complain);
1426 process_subob_fn (rval, NULL, NULL,
1427 deleted_p, NULL, false,
1428 basetype);
1433 /* Now handle the non-static data members. */
1434 walk_field_subobs (TYPE_FIELDS (ctype), fnname, sfk, quals,
1435 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1436 deleted_p, constexpr_p,
1437 diag, flags, complain);
1438 if (ctor_p)
1439 walk_field_subobs (TYPE_FIELDS (ctype), complete_dtor_identifier,
1440 sfk_destructor, TYPE_UNQUALIFIED, false,
1441 false, false, NULL, NULL,
1442 deleted_p, NULL,
1443 false, flags, complain);
1445 pop_scope (scope);
1447 pop_deferring_access_checks ();
1448 --cp_unevaluated_operand;
1449 --c_inhibit_evaluation_warnings;
1452 /* DECL is a defaulted function whose exception specification is now
1453 needed. Return what it should be. */
1455 tree
1456 get_defaulted_eh_spec (tree decl)
1458 if (DECL_CLONED_FUNCTION_P (decl))
1459 decl = DECL_CLONED_FUNCTION (decl);
1460 special_function_kind sfk = special_function_p (decl);
1461 tree ctype = DECL_CONTEXT (decl);
1462 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1463 tree parm_type = TREE_VALUE (parms);
1464 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1465 tree spec = empty_except_spec;
1466 synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL,
1467 NULL, false, DECL_INHERITED_CTOR_BASE (decl),
1468 parms);
1469 return spec;
1472 /* DECL is a deleted function. If it's implicitly deleted, explain why and
1473 return true; else return false. */
1475 bool
1476 maybe_explain_implicit_delete (tree decl)
1478 /* If decl is a clone, get the primary variant. */
1479 decl = DECL_ORIGIN (decl);
1480 gcc_assert (DECL_DELETED_FN (decl));
1481 if (DECL_DEFAULTED_FN (decl))
1483 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
1484 static struct pointer_set_t *explained;
1486 special_function_kind sfk;
1487 location_t loc;
1488 bool informed;
1489 tree ctype;
1491 if (!explained)
1492 explained = pointer_set_create ();
1493 if (pointer_set_insert (explained, decl))
1494 return true;
1496 sfk = special_function_p (decl);
1497 ctype = DECL_CONTEXT (decl);
1498 loc = input_location;
1499 input_location = DECL_SOURCE_LOCATION (decl);
1501 informed = false;
1502 if (LAMBDA_TYPE_P (ctype))
1504 informed = true;
1505 if (sfk == sfk_constructor)
1506 inform (DECL_SOURCE_LOCATION (decl),
1507 "a lambda closure type has a deleted default constructor");
1508 else if (sfk == sfk_copy_assignment)
1509 inform (DECL_SOURCE_LOCATION (decl),
1510 "a lambda closure type has a deleted copy assignment operator");
1511 else
1512 informed = false;
1514 else if (DECL_ARTIFICIAL (decl)
1515 && (sfk == sfk_copy_assignment
1516 || sfk == sfk_copy_constructor)
1517 && (type_has_user_declared_move_constructor (ctype)
1518 || type_has_user_declared_move_assign (ctype)))
1520 inform (0, "%q+#D is implicitly declared as deleted because %qT "
1521 "declares a move constructor or move assignment operator",
1522 decl, ctype);
1523 informed = true;
1525 if (!informed)
1527 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1528 tree parm_type = TREE_VALUE (parms);
1529 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1530 tree raises = NULL_TREE;
1531 bool deleted_p = false;
1532 tree scope = push_scope (ctype);
1534 synthesized_method_walk (ctype, sfk, const_p,
1535 &raises, NULL, &deleted_p, NULL, false,
1536 DECL_INHERITED_CTOR_BASE (decl), parms);
1537 if (deleted_p)
1539 inform (0, "%q+#D is implicitly deleted because the default "
1540 "definition would be ill-formed:", decl);
1541 synthesized_method_walk (ctype, sfk, const_p,
1542 NULL, NULL, NULL, NULL, true,
1543 DECL_INHERITED_CTOR_BASE (decl), parms);
1545 else if (!comp_except_specs
1546 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
1547 raises, ce_normal))
1548 inform (DECL_SOURCE_LOCATION (decl), "%q#F is implicitly "
1549 "deleted because its exception-specification does not "
1550 "match the implicit exception-specification %qX",
1551 decl, raises);
1552 #ifdef ENABLE_CHECKING
1553 else
1554 gcc_unreachable ();
1555 #endif
1557 pop_scope (scope);
1560 input_location = loc;
1561 return true;
1563 return false;
1566 /* DECL is a defaulted function which was declared constexpr. Explain why
1567 it can't be constexpr. */
1569 void
1570 explain_implicit_non_constexpr (tree decl)
1572 tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl));
1573 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1574 bool dummy;
1575 synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
1576 special_function_p (decl), const_p,
1577 NULL, NULL, NULL, &dummy, true,
1578 DECL_INHERITED_CTOR_BASE (decl),
1579 FUNCTION_FIRST_USER_PARMTYPE (decl));
1582 /* DECL is an instantiation of an inheriting constructor template. Deduce
1583 the correct exception-specification and deletedness for this particular
1584 specialization. */
1586 void
1587 deduce_inheriting_ctor (tree decl)
1589 gcc_assert (DECL_INHERITED_CTOR_BASE (decl));
1590 tree spec;
1591 bool trivial, constexpr_, deleted;
1592 synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor,
1593 false, &spec, &trivial, &deleted, &constexpr_,
1594 /*diag*/false,
1595 DECL_INHERITED_CTOR_BASE (decl),
1596 FUNCTION_FIRST_USER_PARMTYPE (decl));
1597 DECL_DELETED_FN (decl) = deleted;
1598 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
1601 /* Implicitly declare the special function indicated by KIND, as a
1602 member of TYPE. For copy constructors and assignment operators,
1603 CONST_P indicates whether these functions should take a const
1604 reference argument or a non-const reference. Returns the
1605 FUNCTION_DECL for the implicitly declared function. */
1607 tree
1608 implicitly_declare_fn (special_function_kind kind, tree type,
1609 bool const_p, tree inherited_ctor,
1610 tree inherited_parms)
1612 tree fn;
1613 tree parameter_types = void_list_node;
1614 tree return_type;
1615 tree fn_type;
1616 tree raises = empty_except_spec;
1617 tree rhs_parm_type = NULL_TREE;
1618 tree this_parm;
1619 tree name;
1620 HOST_WIDE_INT saved_processing_template_decl;
1621 bool deleted_p;
1622 bool constexpr_p;
1624 /* Because we create declarations for implicitly declared functions
1625 lazily, we may be creating the declaration for a member of TYPE
1626 while in some completely different context. However, TYPE will
1627 never be a dependent class (because we never want to do lookups
1628 for implicitly defined functions in a dependent class).
1629 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
1630 because we only create clones for constructors and destructors
1631 when not in a template. */
1632 gcc_assert (!dependent_type_p (type));
1633 saved_processing_template_decl = processing_template_decl;
1634 processing_template_decl = 0;
1636 type = TYPE_MAIN_VARIANT (type);
1638 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (type))
1640 if (kind == sfk_destructor)
1641 /* See comment in check_special_function_return_type. */
1642 return_type = build_pointer_type (void_type_node);
1643 else
1644 return_type = build_pointer_type (type);
1646 else
1647 return_type = void_type_node;
1649 switch (kind)
1651 case sfk_destructor:
1652 /* Destructor. */
1653 name = constructor_name (type);
1654 break;
1656 case sfk_constructor:
1657 /* Default constructor. */
1658 name = constructor_name (type);
1659 break;
1661 case sfk_copy_constructor:
1662 case sfk_copy_assignment:
1663 case sfk_move_constructor:
1664 case sfk_move_assignment:
1665 case sfk_inheriting_constructor:
1667 bool move_p;
1668 if (kind == sfk_copy_assignment
1669 || kind == sfk_move_assignment)
1671 return_type = build_reference_type (type);
1672 name = ansi_assopname (NOP_EXPR);
1674 else
1675 name = constructor_name (type);
1677 if (kind == sfk_inheriting_constructor)
1678 parameter_types = inherited_parms;
1679 else
1681 if (const_p)
1682 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
1683 else
1684 rhs_parm_type = type;
1685 move_p = (kind == sfk_move_assignment
1686 || kind == sfk_move_constructor);
1687 rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
1689 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
1691 break;
1693 default:
1694 gcc_unreachable ();
1697 tree inherited_base = (inherited_ctor
1698 ? DECL_CONTEXT (inherited_ctor)
1699 : NULL_TREE);
1700 bool trivial_p = false;
1702 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
1704 /* For an inheriting constructor template, just copy these flags from
1705 the inherited constructor template for now. */
1706 raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor));
1707 deleted_p = DECL_DELETED_FN (inherited_ctor);
1708 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
1710 else if (cxx_dialect >= cxx11)
1712 raises = unevaluated_noexcept_spec ();
1713 synthesized_method_walk (type, kind, const_p, NULL, &trivial_p,
1714 &deleted_p, &constexpr_p, false,
1715 inherited_base, inherited_parms);
1717 else
1718 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
1719 &deleted_p, &constexpr_p, false,
1720 inherited_base, inherited_parms);
1721 /* Don't bother marking a deleted constructor as constexpr. */
1722 if (deleted_p)
1723 constexpr_p = false;
1724 /* A trivial copy/move constructor is also a constexpr constructor,
1725 unless the class has virtual bases (7.1.5p4). */
1726 else if (trivial_p && cxx_dialect >= cxx11
1727 && (kind == sfk_copy_constructor
1728 || kind == sfk_move_constructor)
1729 && !CLASSTYPE_VBASECLASSES (type))
1730 gcc_assert (constexpr_p);
1732 if (!trivial_p && type_has_trivial_fn (type, kind))
1733 type_set_nontrivial_flag (type, kind);
1735 /* Create the function. */
1736 fn_type = build_method_type_directly (type, return_type, parameter_types);
1737 if (raises)
1738 fn_type = build_exception_variant (fn_type, raises);
1739 fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
1740 if (kind != sfk_inheriting_constructor)
1741 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
1742 if (kind == sfk_constructor || kind == sfk_copy_constructor
1743 || kind == sfk_move_constructor || kind == sfk_inheriting_constructor)
1744 DECL_CONSTRUCTOR_P (fn) = 1;
1745 else if (kind == sfk_destructor)
1746 DECL_DESTRUCTOR_P (fn) = 1;
1747 else
1749 DECL_ASSIGNMENT_OPERATOR_P (fn) = 1;
1750 SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR);
1753 /* If pointers to member functions use the least significant bit to
1754 indicate whether a function is virtual, ensure a pointer
1755 to this function will have that bit clear. */
1756 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
1757 && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
1758 DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
1760 /* Create the explicit arguments. */
1761 if (rhs_parm_type)
1763 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
1764 want its type to be included in the mangled function
1765 name. */
1766 tree decl = cp_build_parm_decl (NULL_TREE, rhs_parm_type);
1767 TREE_READONLY (decl) = 1;
1768 retrofit_lang_decl (decl);
1769 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
1770 DECL_ARGUMENTS (fn) = decl;
1772 else if (kind == sfk_inheriting_constructor)
1774 tree *p = &DECL_ARGUMENTS (fn);
1775 int index = 1;
1776 for (tree parm = inherited_parms; parm != void_list_node;
1777 parm = TREE_CHAIN (parm))
1779 *p = cp_build_parm_decl (NULL_TREE, TREE_VALUE (parm));
1780 retrofit_lang_decl (*p);
1781 DECL_PARM_LEVEL (*p) = 1;
1782 DECL_PARM_INDEX (*p) = index++;
1783 DECL_CONTEXT (*p) = fn;
1784 p = &DECL_CHAIN (*p);
1786 SET_DECL_INHERITED_CTOR_BASE (fn, inherited_base);
1787 DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor);
1788 /* A constructor so declared has the same access as the corresponding
1789 constructor in X. */
1790 TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor);
1791 TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor);
1792 /* Copy constexpr from the inherited constructor even if the
1793 inheriting constructor doesn't satisfy the requirements. */
1794 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
1796 /* Add the "this" parameter. */
1797 this_parm = build_this_parm (fn_type, TYPE_UNQUALIFIED);
1798 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
1799 DECL_ARGUMENTS (fn) = this_parm;
1801 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
1802 set_linkage_according_to_type (type, fn);
1803 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
1804 DECL_IN_AGGR_P (fn) = 1;
1805 DECL_ARTIFICIAL (fn) = 1;
1806 DECL_DEFAULTED_FN (fn) = 1;
1807 if (cxx_dialect >= cxx11)
1809 DECL_DELETED_FN (fn) = deleted_p;
1810 DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
1812 DECL_EXTERNAL (fn) = true;
1813 DECL_NOT_REALLY_EXTERN (fn) = 1;
1814 DECL_DECLARED_INLINE_P (fn) = 1;
1815 gcc_assert (!TREE_USED (fn));
1817 /* Restore PROCESSING_TEMPLATE_DECL. */
1818 processing_template_decl = saved_processing_template_decl;
1820 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
1821 fn = add_inherited_template_parms (fn, inherited_ctor);
1823 /* Warn about calling a non-trivial move assignment in a virtual base. */
1824 if (kind == sfk_move_assignment && !deleted_p && !trivial_p
1825 && CLASSTYPE_VBASECLASSES (type))
1827 location_t loc = input_location;
1828 input_location = DECL_SOURCE_LOCATION (fn);
1829 synthesized_method_walk (type, kind, const_p,
1830 NULL, NULL, NULL, NULL, true,
1831 NULL_TREE, NULL_TREE);
1832 input_location = loc;
1835 return fn;
1838 /* Gives any errors about defaulted functions which need to be deferred
1839 until the containing class is complete. */
1841 void
1842 defaulted_late_check (tree fn)
1844 /* Complain about invalid signature for defaulted fn. */
1845 tree ctx = DECL_CONTEXT (fn);
1846 special_function_kind kind = special_function_p (fn);
1847 bool fn_const_p = (copy_fn_p (fn) == 2);
1848 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
1849 NULL, NULL);
1850 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
1852 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
1853 TREE_TYPE (TREE_TYPE (implicit_fn)))
1854 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1855 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn))))
1857 error ("defaulted declaration %q+D", fn);
1858 error_at (DECL_SOURCE_LOCATION (fn),
1859 "does not match expected signature %qD", implicit_fn);
1862 /* 8.4.2/2: An explicitly-defaulted function (...) may have an explicit
1863 exception-specification only if it is compatible (15.4) with the
1864 exception-specification on the implicit declaration. If a function
1865 is explicitly defaulted on its first declaration, (...) it is
1866 implicitly considered to have the same exception-specification as if
1867 it had been implicitly declared. */
1868 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
1869 if (!fn_spec)
1871 if (DECL_DEFAULTED_IN_CLASS_P (fn))
1872 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
1874 else if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
1875 /* Equivalent to the implicit spec. */;
1876 else if (DECL_DEFAULTED_IN_CLASS_P (fn)
1877 && !CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
1878 /* We can't compare an explicit exception-specification on a
1879 constructor defaulted in the class body to the implicit
1880 exception-specification until after we've parsed any NSDMI; see
1881 after_nsdmi_defaulted_late_checks. */;
1882 else
1884 tree eh_spec = get_defaulted_eh_spec (fn);
1885 if (!comp_except_specs (fn_spec, eh_spec, ce_normal))
1887 if (DECL_DEFAULTED_IN_CLASS_P (fn))
1888 DECL_DELETED_FN (fn) = true;
1889 else
1890 error ("function %q+D defaulted on its redeclaration "
1891 "with an exception-specification that differs from "
1892 "the implicit exception-specification %qX", fn, eh_spec);
1896 if (DECL_DEFAULTED_IN_CLASS_P (fn)
1897 && DECL_DECLARED_CONSTEXPR_P (implicit_fn))
1899 /* Hmm...should we do this for out-of-class too? Should it be OK to
1900 add constexpr later like inline, rather than requiring
1901 declarations to match? */
1902 DECL_DECLARED_CONSTEXPR_P (fn) = true;
1903 if (kind == sfk_constructor)
1904 TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
1907 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
1908 && DECL_DECLARED_CONSTEXPR_P (fn))
1910 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
1912 error ("explicitly defaulted function %q+D cannot be declared "
1913 "as constexpr because the implicit declaration is not "
1914 "constexpr:", fn);
1915 explain_implicit_non_constexpr (fn);
1917 DECL_DECLARED_CONSTEXPR_P (fn) = false;
1920 if (DECL_DELETED_FN (implicit_fn))
1921 DECL_DELETED_FN (fn) = 1;
1924 /* OK, we've parsed the NSDMI for class T, now we can check any explicit
1925 exception-specifications on functions defaulted in the class body. */
1927 void
1928 after_nsdmi_defaulted_late_checks (tree t)
1930 if (uses_template_parms (t))
1931 return;
1932 if (t == error_mark_node)
1933 return;
1934 for (tree fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
1935 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
1937 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
1938 if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
1939 continue;
1941 tree eh_spec = get_defaulted_eh_spec (fn);
1942 if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)),
1943 eh_spec, ce_normal))
1944 DECL_DELETED_FN (fn) = true;
1948 /* Returns true iff FN can be explicitly defaulted, and gives any
1949 errors if defaulting FN is ill-formed. */
1951 bool
1952 defaultable_fn_check (tree fn)
1954 special_function_kind kind = sfk_none;
1956 if (template_parm_scope_p ())
1958 error ("a template cannot be defaulted");
1959 return false;
1962 if (DECL_CONSTRUCTOR_P (fn))
1964 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
1965 kind = sfk_constructor;
1966 else if (copy_fn_p (fn) > 0
1967 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
1968 == void_list_node))
1969 kind = sfk_copy_constructor;
1970 else if (move_fn_p (fn))
1971 kind = sfk_move_constructor;
1973 else if (DECL_DESTRUCTOR_P (fn))
1974 kind = sfk_destructor;
1975 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
1976 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
1978 if (copy_fn_p (fn))
1979 kind = sfk_copy_assignment;
1980 else if (move_fn_p (fn))
1981 kind = sfk_move_assignment;
1984 if (kind == sfk_none)
1986 error ("%qD cannot be defaulted", fn);
1987 return false;
1989 else
1991 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
1992 t && t != void_list_node; t = TREE_CHAIN (t))
1993 if (TREE_PURPOSE (t))
1995 error ("defaulted function %q+D with default argument", fn);
1996 break;
1999 /* Avoid do_warn_unused_parameter warnings. */
2000 for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p))
2001 if (DECL_NAME (p))
2002 TREE_NO_WARNING (p) = 1;
2004 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn)))
2005 /* Defer checking. */;
2006 else if (!processing_template_decl)
2007 defaulted_late_check (fn);
2009 return true;
2013 /* Add an implicit declaration to TYPE for the kind of function
2014 indicated by SFK. Return the FUNCTION_DECL for the new implicit
2015 declaration. */
2017 tree
2018 lazily_declare_fn (special_function_kind sfk, tree type)
2020 tree fn;
2021 /* Whether or not the argument has a const reference type. */
2022 bool const_p = false;
2024 switch (sfk)
2026 case sfk_constructor:
2027 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
2028 break;
2029 case sfk_copy_constructor:
2030 const_p = TYPE_HAS_CONST_COPY_CTOR (type);
2031 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
2032 break;
2033 case sfk_move_constructor:
2034 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
2035 break;
2036 case sfk_copy_assignment:
2037 const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
2038 CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
2039 break;
2040 case sfk_move_assignment:
2041 CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
2042 break;
2043 case sfk_destructor:
2044 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
2045 break;
2046 default:
2047 gcc_unreachable ();
2050 /* Declare the function. */
2051 fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL);
2053 /* [class.copy]/8 If the class definition declares a move constructor or
2054 move assignment operator, the implicitly declared copy constructor is
2055 defined as deleted.... */
2056 if ((sfk == sfk_copy_assignment
2057 || sfk == sfk_copy_constructor)
2058 && (type_has_user_declared_move_constructor (type)
2059 || type_has_user_declared_move_assign (type)))
2060 DECL_DELETED_FN (fn) = true;
2062 /* A destructor may be virtual. */
2063 if (sfk == sfk_destructor
2064 || sfk == sfk_move_assignment
2065 || sfk == sfk_copy_assignment)
2066 check_for_override (fn, type);
2067 /* Add it to CLASSTYPE_METHOD_VEC. */
2068 add_method (type, fn, NULL_TREE);
2069 /* Add it to TYPE_METHODS. */
2070 if (sfk == sfk_destructor
2071 && DECL_VIRTUAL_P (fn))
2072 /* The ABI requires that a virtual destructor go at the end of the
2073 vtable. */
2074 TYPE_METHODS (type) = chainon (TYPE_METHODS (type), fn);
2075 else
2077 DECL_CHAIN (fn) = TYPE_METHODS (type);
2078 TYPE_METHODS (type) = fn;
2080 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
2081 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
2082 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
2083 /* Create appropriate clones. */
2084 clone_function_decl (fn, /*update_method_vec=*/true);
2086 return fn;
2089 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
2090 as there are artificial parms in FN. */
2092 tree
2093 skip_artificial_parms_for (const_tree fn, tree list)
2095 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2096 list = TREE_CHAIN (list);
2097 else
2098 return list;
2100 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2101 list = TREE_CHAIN (list);
2102 if (DECL_HAS_VTT_PARM_P (fn))
2103 list = TREE_CHAIN (list);
2104 return list;
2107 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
2108 artificial parms in FN. */
2111 num_artificial_parms_for (const_tree fn)
2113 int count = 0;
2115 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2116 count++;
2117 else
2118 return 0;
2120 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2121 count++;
2122 if (DECL_HAS_VTT_PARM_P (fn))
2123 count++;
2124 return count;
2128 #include "gt-cp-method.h"