gcc/cp/
[official-gcc.git] / gcc / cp / method.c
bloba0ca36dfd0246a122bc2808b88814e821269c26a
1 /* Handle the hair of processing (but not expanding) inline functions.
2 Also manage function and variable name overloading.
3 Copyright (C) 1987-2017 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* Handle method declarations. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "target.h"
28 #include "cp-tree.h"
29 #include "stringpool.h"
30 #include "cgraph.h"
31 #include "varasm.h"
32 #include "toplev.h"
33 #include "common/common-target.h"
35 /* Various flags to control the mangling process. */
37 enum mangling_flags
39 /* No flags. */
40 mf_none = 0,
41 /* The thing we are presently mangling is part of a template type,
42 rather than a fully instantiated type. Therefore, we may see
43 complex expressions where we would normally expect to see a
44 simple integer constant. */
45 mf_maybe_uninstantiated = 1,
46 /* When mangling a numeric value, use the form `_XX_' (instead of
47 just `XX') if the value has more than one digit. */
48 mf_use_underscores_around_value = 2
51 static void do_build_copy_assign (tree);
52 static void do_build_copy_constructor (tree);
53 static tree make_alias_for_thunk (tree);
55 /* Called once to initialize method.c. */
57 void
58 init_method (void)
60 init_mangle ();
63 /* Return a this or result adjusting thunk to FUNCTION. THIS_ADJUSTING
64 indicates whether it is a this or result adjusting thunk.
65 FIXED_OFFSET and VIRTUAL_OFFSET indicate how to do the adjustment
66 (see thunk_adjust). VIRTUAL_OFFSET can be NULL, but FIXED_OFFSET
67 never is. VIRTUAL_OFFSET is the /index/ into the vtable for this
68 adjusting thunks, we scale it to a byte offset. For covariant
69 thunks VIRTUAL_OFFSET is the virtual binfo. You must post process
70 the returned thunk with finish_thunk. */
72 tree
73 make_thunk (tree function, bool this_adjusting,
74 tree fixed_offset, tree virtual_offset)
76 HOST_WIDE_INT d;
77 tree thunk;
79 gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
80 /* We can have this thunks to covariant thunks, but not vice versa. */
81 gcc_assert (!DECL_THIS_THUNK_P (function));
82 gcc_assert (!DECL_RESULT_THUNK_P (function) || this_adjusting);
84 /* Scale the VIRTUAL_OFFSET to be in terms of bytes. */
85 if (this_adjusting && virtual_offset)
86 virtual_offset
87 = size_binop (MULT_EXPR,
88 virtual_offset,
89 convert (ssizetype,
90 TYPE_SIZE_UNIT (vtable_entry_type)));
92 d = tree_to_shwi (fixed_offset);
94 /* See if we already have the thunk in question. For this_adjusting
95 thunks VIRTUAL_OFFSET will be an INTEGER_CST, for covariant thunks it
96 will be a BINFO. */
97 for (thunk = DECL_THUNKS (function); thunk; thunk = DECL_CHAIN (thunk))
98 if (DECL_THIS_THUNK_P (thunk) == this_adjusting
99 && THUNK_FIXED_OFFSET (thunk) == d
100 && !virtual_offset == !THUNK_VIRTUAL_OFFSET (thunk)
101 && (!virtual_offset
102 || (this_adjusting
103 ? tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk),
104 virtual_offset)
105 : THUNK_VIRTUAL_OFFSET (thunk) == virtual_offset)))
106 return thunk;
108 /* All thunks must be created before FUNCTION is actually emitted;
109 the ABI requires that all thunks be emitted together with the
110 function to which they transfer control. */
111 gcc_assert (!TREE_ASM_WRITTEN (function));
112 /* Likewise, we can only be adding thunks to a function declared in
113 the class currently being laid out. */
114 gcc_assert (TYPE_SIZE (DECL_CONTEXT (function))
115 && TYPE_BEING_DEFINED (DECL_CONTEXT (function)));
117 thunk = build_decl (DECL_SOURCE_LOCATION (function),
118 FUNCTION_DECL, NULL_TREE, TREE_TYPE (function));
119 DECL_LANG_SPECIFIC (thunk) = DECL_LANG_SPECIFIC (function);
120 cxx_dup_lang_specific_decl (thunk);
121 DECL_VIRTUAL_P (thunk) = true;
122 SET_DECL_THUNKS (thunk, NULL_TREE);
124 DECL_CONTEXT (thunk) = DECL_CONTEXT (function);
125 TREE_READONLY (thunk) = TREE_READONLY (function);
126 TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (function);
127 TREE_PUBLIC (thunk) = TREE_PUBLIC (function);
128 SET_DECL_THUNK_P (thunk, this_adjusting);
129 THUNK_TARGET (thunk) = function;
130 THUNK_FIXED_OFFSET (thunk) = d;
131 THUNK_VIRTUAL_OFFSET (thunk) = virtual_offset;
132 THUNK_ALIAS (thunk) = NULL_TREE;
134 DECL_INTERFACE_KNOWN (thunk) = 1;
135 DECL_NOT_REALLY_EXTERN (thunk) = 1;
136 DECL_COMDAT (thunk) = DECL_COMDAT (function);
137 DECL_SAVED_FUNCTION_DATA (thunk) = NULL;
138 /* The thunk itself is not a constructor or destructor, even if
139 the thing it is thunking to is. */
140 DECL_DESTRUCTOR_P (thunk) = 0;
141 DECL_CONSTRUCTOR_P (thunk) = 0;
142 DECL_EXTERNAL (thunk) = 1;
143 DECL_ARTIFICIAL (thunk) = 1;
144 /* The THUNK is not a pending inline, even if the FUNCTION is. */
145 DECL_PENDING_INLINE_P (thunk) = 0;
146 DECL_DECLARED_INLINE_P (thunk) = 0;
147 /* Nor is it a template instantiation. */
148 DECL_USE_TEMPLATE (thunk) = 0;
149 DECL_TEMPLATE_INFO (thunk) = NULL;
151 /* Add it to the list of thunks associated with FUNCTION. */
152 DECL_CHAIN (thunk) = DECL_THUNKS (function);
153 SET_DECL_THUNKS (function, thunk);
155 return thunk;
158 /* Finish THUNK, a thunk decl. */
160 void
161 finish_thunk (tree thunk)
163 tree function, name;
164 tree fixed_offset = ssize_int (THUNK_FIXED_OFFSET (thunk));
165 tree virtual_offset = THUNK_VIRTUAL_OFFSET (thunk);
167 gcc_assert (!DECL_NAME (thunk) && DECL_THUNK_P (thunk));
168 if (virtual_offset && DECL_RESULT_THUNK_P (thunk))
169 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
170 function = THUNK_TARGET (thunk);
171 name = mangle_thunk (function, DECL_THIS_THUNK_P (thunk),
172 fixed_offset, virtual_offset, thunk);
174 /* We can end up with declarations of (logically) different
175 covariant thunks, that do identical adjustments. The two thunks
176 will be adjusting between within different hierarchies, which
177 happen to have the same layout. We must nullify one of them to
178 refer to the other. */
179 if (DECL_RESULT_THUNK_P (thunk))
181 tree cov_probe;
183 for (cov_probe = DECL_THUNKS (function);
184 cov_probe; cov_probe = DECL_CHAIN (cov_probe))
185 if (DECL_NAME (cov_probe) == name)
187 gcc_assert (!DECL_THUNKS (thunk));
188 THUNK_ALIAS (thunk) = (THUNK_ALIAS (cov_probe)
189 ? THUNK_ALIAS (cov_probe) : cov_probe);
190 break;
194 DECL_NAME (thunk) = name;
195 SET_DECL_ASSEMBLER_NAME (thunk, name);
198 static GTY (()) int thunk_labelno;
200 /* Create a static alias to target. */
202 tree
203 make_alias_for (tree target, tree newid)
205 tree alias = build_decl (DECL_SOURCE_LOCATION (target),
206 TREE_CODE (target), newid, TREE_TYPE (target));
207 DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (target);
208 cxx_dup_lang_specific_decl (alias);
209 DECL_CONTEXT (alias) = NULL;
210 TREE_READONLY (alias) = TREE_READONLY (target);
211 TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (target);
212 TREE_PUBLIC (alias) = 0;
213 DECL_INTERFACE_KNOWN (alias) = 1;
214 if (DECL_LANG_SPECIFIC (alias))
216 DECL_NOT_REALLY_EXTERN (alias) = 1;
217 DECL_USE_TEMPLATE (alias) = 0;
218 DECL_TEMPLATE_INFO (alias) = NULL;
220 DECL_EXTERNAL (alias) = 0;
221 DECL_ARTIFICIAL (alias) = 1;
222 DECL_TEMPLATE_INSTANTIATED (alias) = 0;
223 if (TREE_CODE (alias) == FUNCTION_DECL)
225 DECL_SAVED_FUNCTION_DATA (alias) = NULL;
226 DECL_DESTRUCTOR_P (alias) = 0;
227 DECL_CONSTRUCTOR_P (alias) = 0;
228 DECL_PENDING_INLINE_P (alias) = 0;
229 DECL_DECLARED_INLINE_P (alias) = 0;
230 DECL_INITIAL (alias) = error_mark_node;
231 DECL_ARGUMENTS (alias) = copy_list (DECL_ARGUMENTS (target));
233 else
234 TREE_STATIC (alias) = 1;
235 TREE_ADDRESSABLE (alias) = 1;
236 TREE_USED (alias) = 1;
237 SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
238 return alias;
241 static tree
242 make_alias_for_thunk (tree function)
244 tree alias;
245 char buf[256];
247 targetm.asm_out.generate_internal_label (buf, "LTHUNK", thunk_labelno);
248 thunk_labelno++;
250 alias = make_alias_for (function, get_identifier (buf));
252 if (!flag_syntax_only)
254 struct cgraph_node *funcn, *aliasn;
255 funcn = cgraph_node::get (function);
256 gcc_checking_assert (funcn);
257 aliasn = cgraph_node::create_same_body_alias (alias, function);
258 DECL_ASSEMBLER_NAME (function);
259 gcc_assert (aliasn != NULL);
262 return alias;
265 /* Emit the definition of a C++ multiple inheritance or covariant
266 return vtable thunk. If EMIT_P is nonzero, the thunk is emitted
267 immediately. */
269 void
270 use_thunk (tree thunk_fndecl, bool emit_p)
272 tree a, t, function, alias;
273 tree virtual_offset;
274 HOST_WIDE_INT fixed_offset, virtual_value;
275 bool this_adjusting = DECL_THIS_THUNK_P (thunk_fndecl);
276 struct cgraph_node *funcn, *thunk_node;
278 /* We should have called finish_thunk to give it a name. */
279 gcc_assert (DECL_NAME (thunk_fndecl));
281 /* We should never be using an alias, always refer to the
282 aliased thunk. */
283 gcc_assert (!THUNK_ALIAS (thunk_fndecl));
285 if (TREE_ASM_WRITTEN (thunk_fndecl))
286 return;
288 function = THUNK_TARGET (thunk_fndecl);
289 if (DECL_RESULT (thunk_fndecl))
290 /* We already turned this thunk into an ordinary function.
291 There's no need to process this thunk again. */
292 return;
294 if (DECL_THUNK_P (function))
295 /* The target is itself a thunk, process it now. */
296 use_thunk (function, emit_p);
298 /* Thunks are always addressable; they only appear in vtables. */
299 TREE_ADDRESSABLE (thunk_fndecl) = 1;
301 /* Figure out what function is being thunked to. It's referenced in
302 this translation unit. */
303 TREE_ADDRESSABLE (function) = 1;
304 mark_used (function);
305 if (!emit_p)
306 return;
308 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function))
309 alias = make_alias_for_thunk (function);
310 else
311 alias = function;
313 fixed_offset = THUNK_FIXED_OFFSET (thunk_fndecl);
314 virtual_offset = THUNK_VIRTUAL_OFFSET (thunk_fndecl);
316 if (virtual_offset)
318 if (!this_adjusting)
319 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
320 virtual_value = tree_to_shwi (virtual_offset);
321 gcc_assert (virtual_value);
323 else
324 virtual_value = 0;
326 /* And, if we need to emit the thunk, it's used. */
327 mark_used (thunk_fndecl);
328 /* This thunk is actually defined. */
329 DECL_EXTERNAL (thunk_fndecl) = 0;
330 /* The linkage of the function may have changed. FIXME in linkage
331 rewrite. */
332 gcc_assert (DECL_INTERFACE_KNOWN (function));
333 TREE_PUBLIC (thunk_fndecl) = TREE_PUBLIC (function);
334 DECL_VISIBILITY (thunk_fndecl) = DECL_VISIBILITY (function);
335 DECL_VISIBILITY_SPECIFIED (thunk_fndecl)
336 = DECL_VISIBILITY_SPECIFIED (function);
337 DECL_COMDAT (thunk_fndecl) = DECL_COMDAT (function);
338 DECL_WEAK (thunk_fndecl) = DECL_WEAK (function);
340 if (flag_syntax_only)
342 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
343 return;
346 push_to_top_level ();
348 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function)
349 && targetm_common.have_named_sections)
351 tree fn = function;
352 struct symtab_node *symbol;
354 if ((symbol = symtab_node::get (function))
355 && symbol->alias)
357 if (symbol->analyzed)
358 fn = symtab_node::get (function)->ultimate_alias_target ()->decl;
359 else
360 fn = symtab_node::get (function)->alias_target;
362 resolve_unique_section (fn, 0, flag_function_sections);
364 if (DECL_SECTION_NAME (fn) != NULL && DECL_ONE_ONLY (fn))
366 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
368 /* Output the thunk into the same section as function. */
369 set_decl_section_name (thunk_fndecl, DECL_SECTION_NAME (fn));
370 symtab_node::get (thunk_fndecl)->implicit_section
371 = symtab_node::get (fn)->implicit_section;
375 /* Set up cloned argument trees for the thunk. */
376 t = NULL_TREE;
377 for (a = DECL_ARGUMENTS (function); a; a = DECL_CHAIN (a))
379 tree x = copy_node (a);
380 DECL_CHAIN (x) = t;
381 DECL_CONTEXT (x) = thunk_fndecl;
382 SET_DECL_RTL (x, NULL);
383 DECL_HAS_VALUE_EXPR_P (x) = 0;
384 TREE_ADDRESSABLE (x) = 0;
385 t = x;
387 a = nreverse (t);
388 DECL_ARGUMENTS (thunk_fndecl) = a;
389 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
390 funcn = cgraph_node::get (function);
391 gcc_checking_assert (funcn);
392 thunk_node = funcn->create_thunk (thunk_fndecl, function,
393 this_adjusting, fixed_offset, virtual_value,
394 virtual_offset, alias);
395 if (DECL_ONE_ONLY (function))
396 thunk_node->add_to_same_comdat_group (funcn);
398 pop_from_top_level ();
401 /* Code for synthesizing methods which have default semantics defined. */
403 /* True iff CTYPE has a trivial SFK. */
405 static bool
406 type_has_trivial_fn (tree ctype, special_function_kind sfk)
408 switch (sfk)
410 case sfk_constructor:
411 return !TYPE_HAS_COMPLEX_DFLT (ctype);
412 case sfk_copy_constructor:
413 return !TYPE_HAS_COMPLEX_COPY_CTOR (ctype);
414 case sfk_move_constructor:
415 return !TYPE_HAS_COMPLEX_MOVE_CTOR (ctype);
416 case sfk_copy_assignment:
417 return !TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype);
418 case sfk_move_assignment:
419 return !TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype);
420 case sfk_destructor:
421 return !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype);
422 case sfk_inheriting_constructor:
423 return false;
424 default:
425 gcc_unreachable ();
429 /* Note that CTYPE has a non-trivial SFK even though we previously thought
430 it was trivial. */
432 static void
433 type_set_nontrivial_flag (tree ctype, special_function_kind sfk)
435 switch (sfk)
437 case sfk_constructor:
438 TYPE_HAS_COMPLEX_DFLT (ctype) = true;
439 return;
440 case sfk_copy_constructor:
441 TYPE_HAS_COMPLEX_COPY_CTOR (ctype) = true;
442 return;
443 case sfk_move_constructor:
444 TYPE_HAS_COMPLEX_MOVE_CTOR (ctype) = true;
445 return;
446 case sfk_copy_assignment:
447 TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype) = true;
448 return;
449 case sfk_move_assignment:
450 TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype) = true;
451 return;
452 case sfk_destructor:
453 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
454 return;
455 case sfk_inheriting_constructor:
456 default:
457 gcc_unreachable ();
461 /* True iff FN is a trivial defaulted member function ([cd]tor, op=). */
463 bool
464 trivial_fn_p (tree fn)
466 if (TREE_CODE (fn) == TEMPLATE_DECL)
467 return false;
468 if (!DECL_DEFAULTED_FN (fn))
469 return false;
471 /* If fn is a clone, get the primary variant. */
472 if (tree prim = DECL_CLONED_FUNCTION (fn))
473 fn = prim;
474 return type_has_trivial_fn (DECL_CONTEXT (fn), special_function_p (fn));
477 /* PARM is a PARM_DECL for a function which we want to forward to another
478 function without changing its value category, a la std::forward. */
480 tree
481 forward_parm (tree parm)
483 tree exp = convert_from_reference (parm);
484 tree type = TREE_TYPE (parm);
485 if (DECL_PACK_P (parm))
486 type = PACK_EXPANSION_PATTERN (type);
487 if (TREE_CODE (type) != REFERENCE_TYPE)
488 type = cp_build_reference_type (type, /*rval=*/true);
489 exp = build_static_cast (type, exp, tf_warning_or_error);
490 if (DECL_PACK_P (parm))
491 exp = make_pack_expansion (exp);
492 return exp;
495 /* Strip all inheriting constructors, if any, to return the original
496 constructor from a (possibly indirect) base class. */
498 tree
499 strip_inheriting_ctors (tree dfn)
501 if (!flag_new_inheriting_ctors)
502 return dfn;
503 tree fn = dfn;
504 while (tree inh = DECL_INHERITED_CTOR (fn))
506 inh = OVL_CURRENT (inh);
507 fn = inh;
509 if (TREE_CODE (fn) == TEMPLATE_DECL
510 && TREE_CODE (dfn) == FUNCTION_DECL)
511 fn = DECL_TEMPLATE_RESULT (fn);
512 return fn;
515 /* Find the binfo for the base subobject of BINFO being initialized by
516 inherited constructor FNDECL (a member of a direct base of BINFO). */
518 static tree inherited_ctor_binfo (tree, tree);
519 static tree
520 inherited_ctor_binfo_1 (tree binfo, tree fndecl)
522 tree base = DECL_CONTEXT (fndecl);
523 tree base_binfo;
524 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
525 if (BINFO_TYPE (base_binfo) == base)
526 return inherited_ctor_binfo (base_binfo, fndecl);
528 gcc_unreachable();
531 /* Find the binfo for the base subobject of BINFO being initialized by
532 inheriting constructor FNDECL (a member of BINFO), or BINFO if FNDECL is not
533 an inheriting constructor. */
535 static tree
536 inherited_ctor_binfo (tree binfo, tree fndecl)
538 tree inh = DECL_INHERITED_CTOR (fndecl);
539 if (!inh)
540 return binfo;
542 tree results = NULL_TREE;
543 for (; inh; inh = OVL_NEXT (inh))
545 tree one = inherited_ctor_binfo_1 (binfo, OVL_CURRENT (inh));
546 if (!results)
547 results = one;
548 else if (one != results)
549 results = tree_cons (NULL_TREE, one, results);
551 return results;
554 /* Find the binfo for the base subobject being initialized by inheriting
555 constructor FNDECL, or NULL_TREE if FNDECL is not an inheriting
556 constructor. */
558 tree
559 inherited_ctor_binfo (tree fndecl)
561 if (!DECL_INHERITED_CTOR (fndecl))
562 return NULL_TREE;
563 tree binfo = TYPE_BINFO (DECL_CONTEXT (fndecl));
564 return inherited_ctor_binfo (binfo, fndecl);
567 /* True if we should omit all user-declared parameters from constructor FN,
568 because it is a base clone of a ctor inherited from a virtual base. */
570 bool
571 ctor_omit_inherited_parms (tree fn)
573 if (!flag_new_inheriting_ctors)
574 /* We only optimize away the parameters in the new model. */
575 return false;
576 if (!DECL_BASE_CONSTRUCTOR_P (fn)
577 || !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
578 return false;
579 if (FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn)) == void_list_node)
580 /* No user-declared parameters to omit. */
581 return false;
582 tree binfo = inherited_ctor_binfo (fn);
583 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
584 if (BINFO_VIRTUAL_P (binfo))
585 return true;
586 return false;
589 /* True iff constructor(s) INH inherited into BINFO initializes INIT_BINFO.
590 This can be true for multiple virtual bases as well as one direct
591 non-virtual base. */
593 static bool
594 binfo_inherited_from (tree binfo, tree init_binfo, tree inh)
596 /* inh is an OVERLOAD if we inherited the same constructor along
597 multiple paths, check all of them. */
598 for (; inh; inh = OVL_NEXT (inh))
600 tree fn = OVL_CURRENT (inh);
601 tree base = DECL_CONTEXT (fn);
602 tree base_binfo = NULL_TREE;
603 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
604 if (BINFO_TYPE (base_binfo) == base)
605 break;
606 if (base_binfo == init_binfo
607 || (flag_new_inheriting_ctors
608 && binfo_inherited_from (base_binfo, init_binfo,
609 DECL_INHERITED_CTOR (fn))))
610 return true;
612 return false;
615 /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO
616 given the parameter or parameters PARM, possibly inherited constructor
617 base INH, or move flag MOVE_P. */
619 static tree
620 add_one_base_init (tree binfo, tree parm, bool move_p, tree inh,
621 tree member_init_list)
623 tree init;
624 if (inh)
626 /* An inheriting constructor only has a mem-initializer for
627 the base it inherits from. */
628 if (!binfo_inherited_from (TYPE_BINFO (current_class_type), binfo, inh))
629 return member_init_list;
631 tree *p = &init;
632 init = NULL_TREE;
633 for (; parm; parm = DECL_CHAIN (parm))
635 tree exp = forward_parm (parm);
636 *p = build_tree_list (NULL_TREE, exp);
637 p = &TREE_CHAIN (*p);
640 else
642 init = build_base_path (PLUS_EXPR, parm, binfo, 1,
643 tf_warning_or_error);
644 if (move_p)
645 init = move (init);
646 init = build_tree_list (NULL_TREE, init);
648 return tree_cons (binfo, init, member_init_list);
651 /* Generate code for default X(X&) or X(X&&) constructor or an inheriting
652 constructor. */
654 static void
655 do_build_copy_constructor (tree fndecl)
657 tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
658 bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
659 bool trivial = trivial_fn_p (fndecl);
660 tree inh = DECL_INHERITED_CTOR (fndecl);
662 if (!inh)
663 parm = convert_from_reference (parm);
665 if (trivial)
667 if (is_empty_class (current_class_type))
668 /* Don't copy the padding byte; it might not have been allocated
669 if *this is a base subobject. */;
670 else if (tree_int_cst_equal (TYPE_SIZE (current_class_type),
671 CLASSTYPE_SIZE (current_class_type)))
673 tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm);
674 finish_expr_stmt (t);
676 else
678 /* We must only copy the non-tail padding parts. */
679 tree base_size = CLASSTYPE_SIZE_UNIT (current_class_type);
680 base_size = size_binop (MINUS_EXPR, base_size, size_int (1));
681 tree array_type = build_array_type (unsigned_char_type_node,
682 build_index_type (base_size));
683 tree alias_set = build_int_cst (TREE_TYPE (current_class_ptr), 0);
684 tree lhs = build2 (MEM_REF, array_type,
685 current_class_ptr, alias_set);
686 tree rhs = build2 (MEM_REF, array_type,
687 TREE_OPERAND (parm, 0), alias_set);
688 tree t = build2 (INIT_EXPR, void_type_node, lhs, rhs);
689 finish_expr_stmt (t);
692 else
694 tree fields = TYPE_FIELDS (current_class_type);
695 tree member_init_list = NULL_TREE;
696 int cvquals = cp_type_quals (TREE_TYPE (parm));
697 int i;
698 tree binfo, base_binfo;
699 tree init;
700 vec<tree, va_gc> *vbases;
702 /* Initialize all the base-classes with the parameter converted
703 to their type so that we get their copy constructor and not
704 another constructor that takes current_class_type. We must
705 deal with the binfo's directly as a direct base might be
706 inaccessible due to ambiguity. */
707 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
708 vec_safe_iterate (vbases, i, &binfo); i++)
710 member_init_list = add_one_base_init (binfo, parm, move_p, inh,
711 member_init_list);
714 for (binfo = TYPE_BINFO (current_class_type), i = 0;
715 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
717 if (BINFO_VIRTUAL_P (base_binfo))
718 continue;
719 member_init_list = add_one_base_init (base_binfo, parm, move_p,
720 inh, member_init_list);
723 for (; fields; fields = DECL_CHAIN (fields))
725 tree field = fields;
726 tree expr_type;
728 if (TREE_CODE (field) != FIELD_DECL)
729 continue;
730 if (inh)
731 continue;
733 expr_type = TREE_TYPE (field);
734 if (DECL_NAME (field))
736 if (VFIELD_NAME_P (DECL_NAME (field)))
737 continue;
739 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
740 /* Just use the field; anonymous types can't have
741 nontrivial copy ctors or assignment ops or this
742 function would be deleted. */;
743 else
744 continue;
746 /* Compute the type of "init->field". If the copy-constructor
747 parameter is, for example, "const S&", and the type of
748 the field is "T", then the type will usually be "const
749 T". (There are no cv-qualified variants of reference
750 types.) */
751 if (TREE_CODE (expr_type) != REFERENCE_TYPE)
753 int quals = cvquals;
755 if (DECL_MUTABLE_P (field))
756 quals &= ~TYPE_QUAL_CONST;
757 quals |= cp_type_quals (expr_type);
758 expr_type = cp_build_qualified_type (expr_type, quals);
761 init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
762 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
763 /* 'move' breaks bit-fields, and has no effect for scalars. */
764 && !scalarish_type_p (expr_type))
765 init = move (init);
766 init = build_tree_list (NULL_TREE, init);
768 member_init_list = tree_cons (field, init, member_init_list);
770 finish_mem_initializers (member_init_list);
774 static void
775 do_build_copy_assign (tree fndecl)
777 tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl));
778 tree compound_stmt;
779 bool move_p = move_fn_p (fndecl);
780 bool trivial = trivial_fn_p (fndecl);
781 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
783 compound_stmt = begin_compound_stmt (0);
784 parm = convert_from_reference (parm);
786 if (trivial
787 && is_empty_class (current_class_type))
788 /* Don't copy the padding byte; it might not have been allocated
789 if *this is a base subobject. */;
790 else if (trivial)
792 tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
793 finish_expr_stmt (t);
795 else
797 tree fields;
798 int cvquals = cp_type_quals (TREE_TYPE (parm));
799 int i;
800 tree binfo, base_binfo;
802 /* Assign to each of the direct base classes. */
803 for (binfo = TYPE_BINFO (current_class_type), i = 0;
804 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
806 tree converted_parm;
807 vec<tree, va_gc> *parmvec;
809 /* We must convert PARM directly to the base class
810 explicitly since the base class may be ambiguous. */
811 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
812 tf_warning_or_error);
813 if (move_p)
814 converted_parm = move (converted_parm);
815 /* Call the base class assignment operator. */
816 parmvec = make_tree_vector_single (converted_parm);
817 finish_expr_stmt
818 (build_special_member_call (current_class_ref,
819 cp_assignment_operator_id (NOP_EXPR),
820 &parmvec,
821 base_binfo,
822 flags,
823 tf_warning_or_error));
824 release_tree_vector (parmvec);
827 /* Assign to each of the non-static data members. */
828 for (fields = TYPE_FIELDS (current_class_type);
829 fields;
830 fields = DECL_CHAIN (fields))
832 tree comp = current_class_ref;
833 tree init = parm;
834 tree field = fields;
835 tree expr_type;
836 int quals;
838 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
839 continue;
841 expr_type = TREE_TYPE (field);
843 if (CP_TYPE_CONST_P (expr_type))
845 error ("non-static const member %q#D, can%'t use default "
846 "assignment operator", field);
847 continue;
849 else if (TREE_CODE (expr_type) == REFERENCE_TYPE)
851 error ("non-static reference member %q#D, can%'t use "
852 "default assignment operator", field);
853 continue;
856 if (DECL_NAME (field))
858 if (VFIELD_NAME_P (DECL_NAME (field)))
859 continue;
861 else if (ANON_AGGR_TYPE_P (expr_type)
862 && TYPE_FIELDS (expr_type) != NULL_TREE)
863 /* Just use the field; anonymous types can't have
864 nontrivial copy ctors or assignment ops or this
865 function would be deleted. */;
866 else
867 continue;
869 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
871 /* Compute the type of init->field */
872 quals = cvquals;
873 if (DECL_MUTABLE_P (field))
874 quals &= ~TYPE_QUAL_CONST;
875 expr_type = cp_build_qualified_type (expr_type, quals);
877 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
878 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
879 /* 'move' breaks bit-fields, and has no effect for scalars. */
880 && !scalarish_type_p (expr_type))
881 init = move (init);
883 if (DECL_NAME (field))
884 init = cp_build_modify_expr (input_location, comp, NOP_EXPR, init,
885 tf_warning_or_error);
886 else
887 init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
888 finish_expr_stmt (init);
891 finish_return_stmt (current_class_ref);
892 finish_compound_stmt (compound_stmt);
895 /* Synthesize FNDECL, a non-static member function. */
897 void
898 synthesize_method (tree fndecl)
900 bool nested = (current_function_decl != NULL_TREE);
901 tree context = decl_function_context (fndecl);
902 bool need_body = true;
903 tree stmt;
904 location_t save_input_location = input_location;
905 int error_count = errorcount;
906 int warning_count = warningcount + werrorcount;
908 /* Reset the source location, we might have been previously
909 deferred, and thus have saved where we were first needed. */
910 DECL_SOURCE_LOCATION (fndecl)
911 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
913 /* If we've been asked to synthesize a clone, just synthesize the
914 cloned function instead. Doing so will automatically fill in the
915 body for the clone. */
916 if (DECL_CLONED_FUNCTION_P (fndecl))
917 fndecl = DECL_CLONED_FUNCTION (fndecl);
919 /* We may be in the middle of deferred access check. Disable
920 it now. */
921 push_deferring_access_checks (dk_no_deferred);
923 if (! context)
924 push_to_top_level ();
925 else if (nested)
926 push_function_context ();
928 input_location = DECL_SOURCE_LOCATION (fndecl);
930 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
931 stmt = begin_function_body ();
933 if (DECL_OVERLOADED_OPERATOR_P (fndecl) == NOP_EXPR)
935 do_build_copy_assign (fndecl);
936 need_body = false;
938 else if (DECL_CONSTRUCTOR_P (fndecl))
940 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
941 if (arg_chain != void_list_node)
942 do_build_copy_constructor (fndecl);
943 else
944 finish_mem_initializers (NULL_TREE);
947 /* If we haven't yet generated the body of the function, just
948 generate an empty compound statement. */
949 if (need_body)
951 tree compound_stmt;
952 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
953 finish_compound_stmt (compound_stmt);
956 finish_function_body (stmt);
957 expand_or_defer_fn (finish_function (0));
959 input_location = save_input_location;
961 if (! context)
962 pop_from_top_level ();
963 else if (nested)
964 pop_function_context ();
966 pop_deferring_access_checks ();
968 if (error_count != errorcount || warning_count != warningcount + werrorcount)
969 inform (input_location, "synthesized method %qD first required here ",
970 fndecl);
973 /* Build a reference to type TYPE with cv-quals QUALS, which is an
974 rvalue if RVALUE is true. */
976 static tree
977 build_stub_type (tree type, int quals, bool rvalue)
979 tree argtype = cp_build_qualified_type (type, quals);
980 return cp_build_reference_type (argtype, rvalue);
983 /* Build a dummy glvalue from dereferencing a dummy reference of type
984 REFTYPE. */
986 static tree
987 build_stub_object (tree reftype)
989 if (TREE_CODE (reftype) != REFERENCE_TYPE)
990 reftype = cp_build_reference_type (reftype, /*rval*/true);
991 tree stub = build1 (CONVERT_EXPR, reftype, integer_one_node);
992 return convert_from_reference (stub);
995 /* Determine which function will be called when looking up NAME in TYPE,
996 called with a single ARGTYPE argument, or no argument if ARGTYPE is
997 null. FLAGS and COMPLAIN are as for build_new_method_call.
999 Returns a FUNCTION_DECL if all is well.
1000 Returns NULL_TREE if overload resolution failed.
1001 Returns error_mark_node if the chosen function cannot be called. */
1003 static tree
1004 locate_fn_flags (tree type, tree name, tree argtype, int flags,
1005 tsubst_flags_t complain)
1007 tree ob, fn, fns, binfo, rval;
1008 vec<tree, va_gc> *args;
1010 if (TYPE_P (type))
1011 binfo = TYPE_BINFO (type);
1012 else
1014 binfo = type;
1015 type = BINFO_TYPE (binfo);
1018 ob = build_stub_object (cp_build_reference_type (type, false));
1019 args = make_tree_vector ();
1020 if (argtype)
1022 if (TREE_CODE (argtype) == TREE_LIST)
1024 for (tree elt = argtype; elt && elt != void_list_node;
1025 elt = TREE_CHAIN (elt))
1027 tree type = TREE_VALUE (elt);
1028 tree arg = build_stub_object (type);
1029 vec_safe_push (args, arg);
1032 else
1034 tree arg = build_stub_object (argtype);
1035 args->quick_push (arg);
1039 fns = lookup_fnfields (binfo, name, 0);
1040 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
1042 release_tree_vector (args);
1043 if (fn && rval == error_mark_node)
1044 return rval;
1045 else
1046 return fn;
1049 /* Locate the dtor of TYPE. */
1051 tree
1052 get_dtor (tree type, tsubst_flags_t complain)
1054 tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
1055 LOOKUP_NORMAL, complain);
1056 if (fn == error_mark_node)
1057 return NULL_TREE;
1058 return fn;
1061 /* Locate the default ctor of TYPE. */
1063 tree
1064 locate_ctor (tree type)
1066 tree fn;
1068 push_deferring_access_checks (dk_no_check);
1069 fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
1070 LOOKUP_SPECULATIVE, tf_none);
1071 pop_deferring_access_checks ();
1072 if (fn == error_mark_node)
1073 return NULL_TREE;
1074 return fn;
1077 /* Likewise, but give any appropriate errors. */
1079 tree
1080 get_default_ctor (tree type)
1082 tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
1083 LOOKUP_NORMAL, tf_warning_or_error);
1084 if (fn == error_mark_node)
1085 return NULL_TREE;
1086 return fn;
1089 /* Locate the copy ctor of TYPE. */
1091 tree
1092 get_copy_ctor (tree type, tsubst_flags_t complain)
1094 int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
1095 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
1096 tree argtype = build_stub_type (type, quals, false);
1097 tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
1098 LOOKUP_NORMAL, complain);
1099 if (fn == error_mark_node)
1100 return NULL_TREE;
1101 return fn;
1104 /* Locate the copy assignment operator of TYPE. */
1106 tree
1107 get_copy_assign (tree type)
1109 int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
1110 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
1111 tree argtype = build_stub_type (type, quals, false);
1112 tree fn = locate_fn_flags (type, cp_assignment_operator_id (NOP_EXPR), argtype,
1113 LOOKUP_NORMAL, tf_warning_or_error);
1114 if (fn == error_mark_node)
1115 return NULL_TREE;
1116 return fn;
1119 /* walk_tree helper function for is_trivially_xible. If *TP is a call,
1120 return it if it calls something other than a trivial special member
1121 function. */
1123 static tree
1124 check_nontriv (tree *tp, int *, void *)
1126 tree fn = cp_get_callee (*tp);
1127 if (fn == NULL_TREE)
1128 return NULL_TREE;
1130 if (TREE_CODE (fn) == ADDR_EXPR)
1131 fn = TREE_OPERAND (fn, 0);
1133 if (TREE_CODE (fn) != FUNCTION_DECL
1134 || !trivial_fn_p (fn))
1135 return fn;
1136 return NULL_TREE;
1139 /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */
1141 static tree
1142 assignable_expr (tree to, tree from)
1144 ++cp_unevaluated_operand;
1145 to = build_stub_object (to);
1146 from = build_stub_object (from);
1147 tree r = cp_build_modify_expr (input_location, to, NOP_EXPR, from, tf_none);
1148 --cp_unevaluated_operand;
1149 return r;
1152 /* The predicate condition for a template specialization
1153 is_constructible<T, Args...> shall be satisfied if and only if the
1154 following variable definition would be well-formed for some invented
1155 variable t: T t(create<Args>()...);
1157 Return something equivalent in well-formedness and triviality. */
1159 static tree
1160 constructible_expr (tree to, tree from)
1162 tree expr;
1163 if (CLASS_TYPE_P (to))
1165 tree ctype = to;
1166 vec<tree, va_gc> *args = NULL;
1167 if (TREE_CODE (to) != REFERENCE_TYPE)
1168 to = cp_build_reference_type (to, /*rval*/false);
1169 tree ob = build_stub_object (to);
1170 for (; from; from = TREE_CHAIN (from))
1171 vec_safe_push (args, build_stub_object (TREE_VALUE (from)));
1172 expr = build_special_member_call (ob, complete_ctor_identifier, &args,
1173 ctype, LOOKUP_NORMAL, tf_none);
1174 if (expr == error_mark_node)
1175 return error_mark_node;
1176 /* The current state of the standard vis-a-vis LWG 2116 is that
1177 is_*constructible involves destruction as well. */
1178 if (type_build_dtor_call (ctype))
1180 tree dtor = build_special_member_call (ob, complete_dtor_identifier,
1181 NULL, ctype, LOOKUP_NORMAL,
1182 tf_none);
1183 if (dtor == error_mark_node)
1184 return error_mark_node;
1185 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
1186 expr = build2 (COMPOUND_EXPR, void_type_node, expr, dtor);
1189 else
1191 if (from == NULL_TREE)
1192 return build_value_init (to, tf_none);
1193 else if (TREE_CHAIN (from))
1194 return error_mark_node; // too many initializers
1195 from = build_stub_object (TREE_VALUE (from));
1196 expr = perform_direct_initialization_if_possible (to, from,
1197 /*cast*/false,
1198 tf_none);
1200 return expr;
1203 /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or
1204 constructible (otherwise) from FROM, which is a single type for
1205 assignment or a list of types for construction. */
1207 bool
1208 is_trivially_xible (enum tree_code code, tree to, tree from)
1210 tree expr;
1211 if (code == MODIFY_EXPR)
1212 expr = assignable_expr (to, from);
1213 else if (from && TREE_CHAIN (from))
1214 return false; // only 0- and 1-argument ctors can be trivial
1215 else
1216 expr = constructible_expr (to, from);
1218 if (expr == error_mark_node)
1219 return false;
1220 tree nt = cp_walk_tree_without_duplicates (&expr, check_nontriv, NULL);
1221 return !nt;
1224 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
1225 DELETED_P or give an error message MSG with argument ARG. */
1227 static void
1228 process_subob_fn (tree fn, tree *spec_p, bool *trivial_p,
1229 bool *deleted_p, bool *constexpr_p,
1230 bool diag, tree arg, bool dtor_from_ctor = false)
1232 if (!fn || fn == error_mark_node)
1234 if (deleted_p)
1235 *deleted_p = true;
1236 return;
1239 if (spec_p)
1241 maybe_instantiate_noexcept (fn);
1242 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
1243 *spec_p = merge_exception_specifiers (*spec_p, raises);
1246 if (!trivial_fn_p (fn) && !dtor_from_ctor)
1248 if (trivial_p)
1249 *trivial_p = false;
1250 if (TREE_CODE (arg) == FIELD_DECL
1251 && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
1253 if (deleted_p)
1254 *deleted_p = true;
1255 if (diag)
1256 error ("union member %q+D with non-trivial %qD", arg, fn);
1260 if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
1262 *constexpr_p = false;
1263 if (diag)
1265 inform (DECL_SOURCE_LOCATION (fn),
1266 "defaulted constructor calls non-constexpr %qD", fn);
1267 explain_invalid_constexpr_fn (fn);
1272 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
1273 aggregates. If DTOR_FROM_CTOR is true, we're walking subobject destructors
1274 called from a synthesized constructor, in which case we don't consider
1275 the triviality of the subobject destructor. */
1277 static void
1278 walk_field_subobs (tree fields, tree fnname, special_function_kind sfk,
1279 int quals, bool copy_arg_p, bool move_p,
1280 bool assign_p, tree *spec_p, bool *trivial_p,
1281 bool *deleted_p, bool *constexpr_p,
1282 bool diag, int flags, tsubst_flags_t complain,
1283 bool dtor_from_ctor)
1285 tree field;
1286 for (field = fields; field; field = DECL_CHAIN (field))
1288 tree mem_type, argtype, rval;
1290 if (TREE_CODE (field) != FIELD_DECL
1291 || DECL_ARTIFICIAL (field))
1292 continue;
1294 mem_type = strip_array_types (TREE_TYPE (field));
1295 if (assign_p)
1297 bool bad = true;
1298 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
1300 if (diag)
1301 error ("non-static const member %q#D, can%'t use default "
1302 "assignment operator", field);
1304 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1306 if (diag)
1307 error ("non-static reference member %q#D, can%'t use "
1308 "default assignment operator", field);
1310 else
1311 bad = false;
1313 if (bad && deleted_p)
1314 *deleted_p = true;
1316 else if (sfk == sfk_constructor)
1318 bool bad;
1320 if (DECL_INITIAL (field))
1322 if (diag && DECL_INITIAL (field) == error_mark_node)
1323 inform (DECL_SOURCE_LOCATION (field),
1324 "initializer for %q#D is invalid", field);
1325 if (trivial_p)
1326 *trivial_p = false;
1327 /* Core 1351: If the field has an NSDMI that could throw, the
1328 default constructor is noexcept(false). */
1329 if (spec_p)
1331 tree nsdmi = get_nsdmi (field, /*ctor*/false);
1332 if (!expr_noexcept_p (nsdmi, complain))
1333 *spec_p = noexcept_false_spec;
1335 /* Don't do the normal processing. */
1336 continue;
1339 bad = false;
1340 if (CP_TYPE_CONST_P (mem_type)
1341 && default_init_uninitialized_part (mem_type))
1343 if (diag)
1345 error ("uninitialized const member in %q#T",
1346 current_class_type);
1347 inform (DECL_SOURCE_LOCATION (field),
1348 "%q#D should be initialized", field);
1350 bad = true;
1352 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1354 if (diag)
1356 error ("uninitialized reference member in %q#T",
1357 current_class_type);
1358 inform (DECL_SOURCE_LOCATION (field),
1359 "%q#D should be initialized", field);
1361 bad = true;
1364 if (bad && deleted_p)
1365 *deleted_p = true;
1367 /* For an implicitly-defined default constructor to be constexpr,
1368 every member must have a user-provided default constructor or
1369 an explicit initializer. */
1370 if (constexpr_p && !CLASS_TYPE_P (mem_type)
1371 && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE)
1373 *constexpr_p = false;
1374 if (diag)
1375 inform (DECL_SOURCE_LOCATION (field),
1376 "defaulted default constructor does not "
1377 "initialize %q#D", field);
1380 else if (sfk == sfk_copy_constructor)
1382 /* 12.8p11b5 */
1383 if (TREE_CODE (mem_type) == REFERENCE_TYPE
1384 && TYPE_REF_IS_RVALUE (mem_type))
1386 if (diag)
1387 error ("copying non-static data member %q#D of rvalue "
1388 "reference type", field);
1389 if (deleted_p)
1390 *deleted_p = true;
1394 if (!CLASS_TYPE_P (mem_type))
1395 continue;
1397 if (ANON_AGGR_TYPE_P (mem_type))
1399 walk_field_subobs (TYPE_FIELDS (mem_type), fnname, sfk, quals,
1400 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1401 deleted_p, constexpr_p,
1402 diag, flags, complain, dtor_from_ctor);
1403 continue;
1406 if (copy_arg_p)
1408 int mem_quals = cp_type_quals (mem_type) | quals;
1409 if (DECL_MUTABLE_P (field))
1410 mem_quals &= ~TYPE_QUAL_CONST;
1411 argtype = build_stub_type (mem_type, mem_quals, move_p);
1413 else
1414 argtype = NULL_TREE;
1416 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
1418 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1419 constexpr_p, diag, field, dtor_from_ctor);
1423 /* Base walker helper for synthesized_method_walk. Inspect a direct
1424 or virtual base. BINFO is the parent type's binfo. BASE_BINFO is
1425 the base binfo of interests. All other parms are as for
1426 synthesized_method_walk, or its local vars. */
1428 static tree
1429 synthesized_method_base_walk (tree binfo, tree base_binfo,
1430 int quals, bool copy_arg_p,
1431 bool move_p, bool ctor_p,
1432 tree inheriting_ctor, tree inherited_parms,
1433 tree fnname, int flags, bool diag,
1434 tree *spec_p, bool *trivial_p,
1435 bool *deleted_p, bool *constexpr_p)
1437 bool inherited_binfo = false;
1438 tree argtype = NULL_TREE;
1439 deferring_kind defer = dk_no_deferred;
1441 if (copy_arg_p)
1442 argtype = build_stub_type (BINFO_TYPE (base_binfo), quals, move_p);
1443 else if ((inherited_binfo
1444 = binfo_inherited_from (binfo, base_binfo, inheriting_ctor)))
1446 argtype = inherited_parms;
1447 /* Don't check access on the inherited constructor. */
1448 if (flag_new_inheriting_ctors)
1449 defer = dk_deferred;
1451 /* To be conservative, ignore access to the base dtor that
1452 DR1658 instructs us to ignore. See the comment in
1453 synthesized_method_walk. */
1454 else if (cxx_dialect >= cxx14 && fnname == complete_dtor_identifier
1455 && BINFO_VIRTUAL_P (base_binfo)
1456 && ABSTRACT_CLASS_TYPE_P (BINFO_TYPE (binfo)))
1457 defer = dk_no_check;
1459 if (defer != dk_no_deferred)
1460 push_deferring_access_checks (defer);
1461 tree rval = locate_fn_flags (base_binfo, fnname, argtype, flags,
1462 diag ? tf_warning_or_error : tf_none);
1463 if (defer != dk_no_deferred)
1464 pop_deferring_access_checks ();
1466 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1467 constexpr_p, diag, BINFO_TYPE (base_binfo));
1468 if (ctor_p &&
1469 (!BINFO_VIRTUAL_P (base_binfo)
1470 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))))
1472 /* In a constructor we also need to check the subobject
1473 destructors for cleanup of partially constructed objects. */
1474 tree dtor = locate_fn_flags (base_binfo, complete_dtor_identifier,
1475 NULL_TREE, flags,
1476 diag ? tf_warning_or_error : tf_none);
1477 /* Note that we don't pass down trivial_p; the subobject
1478 destructors don't affect triviality of the constructor. Nor
1479 do they affect constexpr-ness (a constant expression doesn't
1480 throw) or exception-specification (a throw from one of the
1481 dtors would be a double-fault). */
1482 process_subob_fn (dtor, NULL, NULL, deleted_p, NULL, false,
1483 BINFO_TYPE (base_binfo), /*dtor_from_ctor*/true);
1486 return rval;
1489 /* The caller wants to generate an implicit declaration of SFK for
1490 CTYPE which is const if relevant and CONST_P is set. If SPEC_P,
1491 TRIVIAL_P, DELETED_P or CONSTEXPR_P are non-null, set their
1492 referent appropriately. If DIAG is true, we're either being called
1493 from maybe_explain_implicit_delete to give errors, or if
1494 CONSTEXPR_P is non-null, from explain_invalid_constexpr_fn. */
1496 static void
1497 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
1498 tree *spec_p, bool *trivial_p, bool *deleted_p,
1499 bool *constexpr_p, bool diag,
1500 tree inheriting_ctor, tree inherited_parms)
1502 tree binfo, base_binfo, fnname;
1503 int i;
1505 if (spec_p)
1506 *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec);
1508 if (deleted_p)
1510 /* "The closure type associated with a lambda-expression has a deleted
1511 default constructor and a deleted copy assignment operator."
1512 This is diagnosed in maybe_explain_implicit_delete. */
1513 if (LAMBDA_TYPE_P (ctype)
1514 && (sfk == sfk_constructor
1515 || sfk == sfk_copy_assignment))
1517 *deleted_p = true;
1518 return;
1521 *deleted_p = false;
1524 bool ctor_p = false;
1525 bool assign_p = false;
1526 bool check_vdtor = false;
1527 switch (sfk)
1529 case sfk_move_assignment:
1530 case sfk_copy_assignment:
1531 assign_p = true;
1532 fnname = cp_assignment_operator_id (NOP_EXPR);
1533 break;
1535 case sfk_destructor:
1536 check_vdtor = true;
1537 /* The synthesized method will call base dtors, but check complete
1538 here to avoid having to deal with VTT. */
1539 fnname = complete_dtor_identifier;
1540 break;
1542 case sfk_constructor:
1543 case sfk_move_constructor:
1544 case sfk_copy_constructor:
1545 case sfk_inheriting_constructor:
1546 ctor_p = true;
1547 fnname = complete_ctor_identifier;
1548 break;
1550 default:
1551 gcc_unreachable ();
1554 gcc_assert ((sfk == sfk_inheriting_constructor)
1555 == (inheriting_ctor != NULL_TREE));
1557 /* If that user-written default constructor would satisfy the
1558 requirements of a constexpr constructor (7.1.5), the
1559 implicitly-defined default constructor is constexpr.
1561 The implicitly-defined copy/move assignment operator is constexpr if
1562 - X is a literal type, and
1563 - the assignment operator selected to copy/move each direct base class
1564 subobject is a constexpr function, and
1565 - for each non-static data member of X that is of class type (or array
1566 thereof), the assignment operator selected to copy/move that
1567 member is a constexpr function. */
1568 if (constexpr_p)
1569 *constexpr_p = ctor_p || (assign_p && cxx_dialect >= cxx14);
1571 bool move_p = false;
1572 bool copy_arg_p = false;
1573 switch (sfk)
1575 case sfk_constructor:
1576 case sfk_destructor:
1577 case sfk_inheriting_constructor:
1578 break;
1580 case sfk_move_constructor:
1581 case sfk_move_assignment:
1582 move_p = true;
1583 /* FALLTHRU */
1584 case sfk_copy_constructor:
1585 case sfk_copy_assignment:
1586 copy_arg_p = true;
1587 break;
1589 default:
1590 gcc_unreachable ();
1593 bool expected_trivial = type_has_trivial_fn (ctype, sfk);
1594 if (trivial_p)
1595 *trivial_p = expected_trivial;
1597 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
1598 class versions and other properties of the type. But a subobject
1599 class can be trivially copyable and yet have overload resolution
1600 choose a template constructor for initialization, depending on
1601 rvalueness and cv-quals. And furthermore, a member in a base might
1602 be trivial but deleted or otherwise not callable. So we can't exit
1603 early in C++0x. The same considerations apply in C++98/03, but
1604 there the definition of triviality does not consider overload
1605 resolution, so a constructor can be trivial even if it would otherwise
1606 call a non-trivial constructor. */
1607 if (expected_trivial
1608 && (!copy_arg_p || cxx_dialect < cxx11))
1610 if (constexpr_p && sfk == sfk_constructor)
1612 bool cx = trivial_default_constructor_is_constexpr (ctype);
1613 *constexpr_p = cx;
1614 if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
1615 /* A trivial constructor doesn't have any NSDMI. */
1616 inform (input_location, "defaulted default constructor does "
1617 "not initialize any non-static data member");
1619 if (!diag && cxx_dialect < cxx11)
1620 return;
1623 ++cp_unevaluated_operand;
1624 ++c_inhibit_evaluation_warnings;
1625 push_deferring_access_checks (dk_no_deferred);
1627 tree scope = push_scope (ctype);
1629 int flags = LOOKUP_NORMAL | LOOKUP_SPECULATIVE;
1630 if (!inheriting_ctor)
1631 flags |= LOOKUP_DEFAULTED;
1633 tsubst_flags_t complain = diag ? tf_warning_or_error : tf_none;
1634 int quals = const_p ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED;
1636 for (binfo = TYPE_BINFO (ctype), i = 0;
1637 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
1639 if (!assign_p && BINFO_VIRTUAL_P (base_binfo))
1640 /* We'll handle virtual bases below. */
1641 continue;
1643 tree fn = synthesized_method_base_walk (binfo, base_binfo, quals,
1644 copy_arg_p, move_p, ctor_p,
1645 inheriting_ctor,
1646 inherited_parms,
1647 fnname, flags, diag,
1648 spec_p, trivial_p,
1649 deleted_p, constexpr_p);
1651 if (diag && assign_p && move_p
1652 && BINFO_VIRTUAL_P (base_binfo)
1653 && fn && TREE_CODE (fn) == FUNCTION_DECL
1654 && move_fn_p (fn) && !trivial_fn_p (fn)
1655 && vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
1656 warning (OPT_Wvirtual_move_assign,
1657 "defaulted move assignment for %qT calls a non-trivial "
1658 "move assignment operator for virtual base %qT",
1659 ctype, BINFO_TYPE (base_binfo));
1661 if (check_vdtor && type_has_virtual_destructor (BINFO_TYPE (base_binfo)))
1663 fn = locate_fn_flags (ctype, cp_operator_id (DELETE_EXPR),
1664 ptr_type_node, flags, complain);
1665 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
1666 to have a null fn (no class-specific op delete). */
1667 if (fn && fn == error_mark_node && deleted_p)
1668 *deleted_p = true;
1669 check_vdtor = false;
1673 vec<tree, va_gc> *vbases = CLASSTYPE_VBASECLASSES (ctype);
1674 if (assign_p)
1675 /* Already examined vbases above. */;
1676 else if (vec_safe_is_empty (vbases))
1677 /* No virtual bases to worry about. */;
1678 else if (ABSTRACT_CLASS_TYPE_P (ctype) && cxx_dialect >= cxx14
1679 /* DR 1658 specifies that vbases of abstract classes are
1680 ignored for both ctors and dtors. However, that breaks
1681 virtual dtor overriding when the ignored base has a
1682 throwing destructor. So, ignore that piece of 1658. A
1683 defect has been filed (no number yet). */
1684 && sfk != sfk_destructor)
1685 /* Vbase cdtors are not relevant. */;
1686 else
1688 if (constexpr_p)
1689 *constexpr_p = false;
1691 FOR_EACH_VEC_ELT (*vbases, i, base_binfo)
1692 synthesized_method_base_walk (binfo, base_binfo, quals,
1693 copy_arg_p, move_p, ctor_p,
1694 inheriting_ctor, inherited_parms,
1695 fnname, flags, diag,
1696 spec_p, trivial_p,
1697 deleted_p, constexpr_p);
1700 /* Now handle the non-static data members. */
1701 walk_field_subobs (TYPE_FIELDS (ctype), fnname, sfk, quals,
1702 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1703 deleted_p, constexpr_p,
1704 diag, flags, complain, /*dtor_from_ctor*/false);
1705 if (ctor_p)
1706 walk_field_subobs (TYPE_FIELDS (ctype), complete_dtor_identifier,
1707 sfk_destructor, TYPE_UNQUALIFIED, false,
1708 false, false, NULL, NULL,
1709 deleted_p, NULL,
1710 false, flags, complain, /*dtor_from_ctor*/true);
1712 pop_scope (scope);
1714 pop_deferring_access_checks ();
1715 --cp_unevaluated_operand;
1716 --c_inhibit_evaluation_warnings;
1719 /* DECL is a defaulted function whose exception specification is now
1720 needed. Return what it should be. */
1722 tree
1723 get_defaulted_eh_spec (tree decl)
1725 if (DECL_CLONED_FUNCTION_P (decl))
1726 decl = DECL_CLONED_FUNCTION (decl);
1727 special_function_kind sfk = special_function_p (decl);
1728 tree ctype = DECL_CONTEXT (decl);
1729 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1730 tree parm_type = TREE_VALUE (parms);
1731 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1732 tree spec = empty_except_spec;
1733 synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL,
1734 NULL, false, DECL_INHERITED_CTOR (decl),
1735 parms);
1736 return spec;
1739 /* DECL is a deleted function. If it's implicitly deleted, explain why and
1740 return true; else return false. */
1742 bool
1743 maybe_explain_implicit_delete (tree decl)
1745 /* If decl is a clone, get the primary variant. */
1746 decl = DECL_ORIGIN (decl);
1747 gcc_assert (DECL_DELETED_FN (decl));
1748 if (DECL_DEFAULTED_FN (decl))
1750 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
1751 static hash_set<tree> *explained;
1753 special_function_kind sfk;
1754 location_t loc;
1755 bool informed;
1756 tree ctype;
1758 if (!explained)
1759 explained = new hash_set<tree>;
1760 if (explained->add (decl))
1761 return true;
1763 sfk = special_function_p (decl);
1764 ctype = DECL_CONTEXT (decl);
1765 loc = input_location;
1766 input_location = DECL_SOURCE_LOCATION (decl);
1768 informed = false;
1769 if (LAMBDA_TYPE_P (ctype))
1771 informed = true;
1772 if (sfk == sfk_constructor)
1773 inform (DECL_SOURCE_LOCATION (decl),
1774 "a lambda closure type has a deleted default constructor");
1775 else if (sfk == sfk_copy_assignment)
1776 inform (DECL_SOURCE_LOCATION (decl),
1777 "a lambda closure type has a deleted copy assignment operator");
1778 else
1779 informed = false;
1781 else if (DECL_ARTIFICIAL (decl)
1782 && (sfk == sfk_copy_assignment
1783 || sfk == sfk_copy_constructor)
1784 && (type_has_user_declared_move_constructor (ctype)
1785 || type_has_user_declared_move_assign (ctype)))
1787 inform (DECL_SOURCE_LOCATION (decl),
1788 "%q#D is implicitly declared as deleted because %qT "
1789 "declares a move constructor or move assignment operator",
1790 decl, ctype);
1791 informed = true;
1793 else if (sfk == sfk_inheriting_constructor)
1795 tree binfo = inherited_ctor_binfo (decl);
1796 if (TREE_CODE (binfo) != TREE_BINFO)
1798 inform (DECL_SOURCE_LOCATION (decl),
1799 "%q#D inherits from multiple base subobjects",
1800 decl);
1801 informed = true;
1804 if (!informed)
1806 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1807 tree parm_type = TREE_VALUE (parms);
1808 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1809 tree raises = NULL_TREE;
1810 bool deleted_p = false;
1811 tree scope = push_scope (ctype);
1813 synthesized_method_walk (ctype, sfk, const_p,
1814 &raises, NULL, &deleted_p, NULL, false,
1815 DECL_INHERITED_CTOR (decl), parms);
1816 if (deleted_p)
1818 inform (DECL_SOURCE_LOCATION (decl),
1819 "%q#D is implicitly deleted because the default "
1820 "definition would be ill-formed:", decl);
1821 synthesized_method_walk (ctype, sfk, const_p,
1822 NULL, NULL, NULL, NULL, true,
1823 DECL_INHERITED_CTOR (decl), parms);
1825 else if (!comp_except_specs
1826 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
1827 raises, ce_normal))
1828 inform (DECL_SOURCE_LOCATION (decl), "%q#F is implicitly "
1829 "deleted because its exception-specification does not "
1830 "match the implicit exception-specification %qX",
1831 decl, raises);
1832 else if (flag_checking)
1833 gcc_unreachable ();
1835 pop_scope (scope);
1838 input_location = loc;
1839 return true;
1841 return false;
1844 /* DECL is a defaulted function which was declared constexpr. Explain why
1845 it can't be constexpr. */
1847 void
1848 explain_implicit_non_constexpr (tree decl)
1850 tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl));
1851 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1852 bool dummy;
1853 synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
1854 special_function_p (decl), const_p,
1855 NULL, NULL, NULL, &dummy, true,
1856 DECL_INHERITED_CTOR (decl),
1857 FUNCTION_FIRST_USER_PARMTYPE (decl));
1860 /* DECL is an instantiation of an inheriting constructor template. Deduce
1861 the correct exception-specification and deletedness for this particular
1862 specialization. */
1864 void
1865 deduce_inheriting_ctor (tree decl)
1867 decl = DECL_ORIGIN (decl);
1868 gcc_assert (DECL_INHERITED_CTOR (decl));
1869 tree spec;
1870 bool trivial, constexpr_, deleted;
1871 synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor,
1872 false, &spec, &trivial, &deleted, &constexpr_,
1873 /*diag*/false,
1874 DECL_INHERITED_CTOR (decl),
1875 FUNCTION_FIRST_USER_PARMTYPE (decl));
1876 if (TREE_CODE (inherited_ctor_binfo (decl)) != TREE_BINFO)
1877 /* Inherited the same constructor from different base subobjects. */
1878 deleted = true;
1879 DECL_DELETED_FN (decl) = deleted;
1880 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
1882 tree clone;
1883 FOR_EACH_CLONE (clone, decl)
1885 DECL_DELETED_FN (clone) = deleted;
1886 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
1890 /* Implicitly declare the special function indicated by KIND, as a
1891 member of TYPE. For copy constructors and assignment operators,
1892 CONST_P indicates whether these functions should take a const
1893 reference argument or a non-const reference. Returns the
1894 FUNCTION_DECL for the implicitly declared function. */
1896 tree
1897 implicitly_declare_fn (special_function_kind kind, tree type,
1898 bool const_p, tree inherited_ctor,
1899 tree inherited_parms)
1901 tree fn;
1902 tree parameter_types = void_list_node;
1903 tree return_type;
1904 tree fn_type;
1905 tree raises = empty_except_spec;
1906 tree rhs_parm_type = NULL_TREE;
1907 tree this_parm;
1908 tree name;
1909 HOST_WIDE_INT saved_processing_template_decl;
1910 bool deleted_p;
1911 bool constexpr_p;
1913 /* Because we create declarations for implicitly declared functions
1914 lazily, we may be creating the declaration for a member of TYPE
1915 while in some completely different context. However, TYPE will
1916 never be a dependent class (because we never want to do lookups
1917 for implicitly defined functions in a dependent class).
1918 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
1919 because we only create clones for constructors and destructors
1920 when not in a template. */
1921 gcc_assert (!dependent_type_p (type));
1922 saved_processing_template_decl = processing_template_decl;
1923 processing_template_decl = 0;
1925 type = TYPE_MAIN_VARIANT (type);
1927 if (targetm.cxx.cdtor_returns_this ())
1929 if (kind == sfk_destructor)
1930 /* See comment in check_special_function_return_type. */
1931 return_type = build_pointer_type (void_type_node);
1932 else
1933 return_type = build_pointer_type (type);
1935 else
1936 return_type = void_type_node;
1938 switch (kind)
1940 case sfk_destructor:
1941 /* Destructor. */
1942 name = constructor_name (type);
1943 break;
1945 case sfk_constructor:
1946 /* Default constructor. */
1947 name = constructor_name (type);
1948 break;
1950 case sfk_copy_constructor:
1951 case sfk_copy_assignment:
1952 case sfk_move_constructor:
1953 case sfk_move_assignment:
1954 case sfk_inheriting_constructor:
1956 bool move_p;
1957 if (kind == sfk_copy_assignment
1958 || kind == sfk_move_assignment)
1960 return_type = build_reference_type (type);
1961 name = cp_assignment_operator_id (NOP_EXPR);
1963 else
1964 name = constructor_name (type);
1966 if (kind == sfk_inheriting_constructor)
1967 parameter_types = inherited_parms;
1968 else
1970 if (const_p)
1971 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
1972 else
1973 rhs_parm_type = type;
1974 move_p = (kind == sfk_move_assignment
1975 || kind == sfk_move_constructor);
1976 rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
1978 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
1980 break;
1982 default:
1983 gcc_unreachable ();
1986 bool trivial_p = false;
1988 if (inherited_ctor)
1990 /* For an inheriting constructor, just copy these flags from the
1991 inherited constructor until deduce_inheriting_ctor. */
1992 raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor));
1993 deleted_p = DECL_DELETED_FN (inherited_ctor);
1994 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
1996 else if (cxx_dialect >= cxx11)
1998 raises = unevaluated_noexcept_spec ();
1999 synthesized_method_walk (type, kind, const_p, NULL, &trivial_p,
2000 &deleted_p, &constexpr_p, false,
2001 inherited_ctor, inherited_parms);
2003 else
2004 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
2005 &deleted_p, &constexpr_p, false,
2006 inherited_ctor, inherited_parms);
2007 /* Don't bother marking a deleted constructor as constexpr. */
2008 if (deleted_p)
2009 constexpr_p = false;
2010 /* A trivial copy/move constructor is also a constexpr constructor,
2011 unless the class has virtual bases (7.1.5p4). */
2012 else if (trivial_p && cxx_dialect >= cxx11
2013 && (kind == sfk_copy_constructor
2014 || kind == sfk_move_constructor)
2015 && !CLASSTYPE_VBASECLASSES (type))
2016 gcc_assert (constexpr_p);
2018 if (!trivial_p && type_has_trivial_fn (type, kind))
2019 type_set_nontrivial_flag (type, kind);
2021 /* Create the function. */
2022 fn_type = build_method_type_directly (type, return_type, parameter_types);
2023 if (raises)
2024 fn_type = build_exception_variant (fn_type, raises);
2025 fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
2026 if (kind != sfk_inheriting_constructor)
2027 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
2028 if (kind == sfk_constructor || kind == sfk_copy_constructor
2029 || kind == sfk_move_constructor || kind == sfk_inheriting_constructor)
2030 DECL_CONSTRUCTOR_P (fn) = 1;
2031 else if (kind == sfk_destructor)
2032 DECL_DESTRUCTOR_P (fn) = 1;
2033 else
2035 DECL_ASSIGNMENT_OPERATOR_P (fn) = 1;
2036 SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR);
2039 SET_DECL_ALIGN (fn, MINIMUM_METHOD_BOUNDARY);
2041 /* Create the explicit arguments. */
2042 if (rhs_parm_type)
2044 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
2045 want its type to be included in the mangled function
2046 name. */
2047 tree decl = cp_build_parm_decl (NULL_TREE, rhs_parm_type);
2048 TREE_READONLY (decl) = 1;
2049 retrofit_lang_decl (decl);
2050 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
2051 DECL_ARGUMENTS (fn) = decl;
2053 else if (kind == sfk_inheriting_constructor)
2055 tree *p = &DECL_ARGUMENTS (fn);
2056 int index = 1;
2057 for (tree parm = inherited_parms; parm && parm != void_list_node;
2058 parm = TREE_CHAIN (parm))
2060 *p = cp_build_parm_decl (NULL_TREE, TREE_VALUE (parm));
2061 retrofit_lang_decl (*p);
2062 DECL_PARM_LEVEL (*p) = 1;
2063 DECL_PARM_INDEX (*p) = index++;
2064 DECL_CONTEXT (*p) = fn;
2065 p = &DECL_CHAIN (*p);
2067 SET_DECL_INHERITED_CTOR (fn, inherited_ctor);
2068 DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor);
2069 /* A constructor so declared has the same access as the corresponding
2070 constructor in X. */
2071 TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor);
2072 TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor);
2073 /* Copy constexpr from the inherited constructor even if the
2074 inheriting constructor doesn't satisfy the requirements. */
2075 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
2077 /* Add the "this" parameter. */
2078 this_parm = build_this_parm (fn_type, TYPE_UNQUALIFIED);
2079 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
2080 DECL_ARGUMENTS (fn) = this_parm;
2082 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
2083 DECL_IN_AGGR_P (fn) = 1;
2084 DECL_ARTIFICIAL (fn) = 1;
2085 DECL_DEFAULTED_FN (fn) = 1;
2086 if (cxx_dialect >= cxx11)
2088 DECL_DELETED_FN (fn) = deleted_p;
2089 DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
2091 DECL_EXTERNAL (fn) = true;
2092 DECL_NOT_REALLY_EXTERN (fn) = 1;
2093 DECL_DECLARED_INLINE_P (fn) = 1;
2094 set_linkage_according_to_type (type, fn);
2095 if (TREE_PUBLIC (fn))
2096 DECL_COMDAT (fn) = 1;
2097 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
2098 gcc_assert (!TREE_USED (fn));
2100 /* Propagate constraints from the inherited constructor. */
2101 if (flag_concepts && inherited_ctor)
2102 if (tree orig_ci = get_constraints (inherited_ctor))
2104 tree new_ci = copy_node (orig_ci);
2105 set_constraints (fn, new_ci);
2108 /* Restore PROCESSING_TEMPLATE_DECL. */
2109 processing_template_decl = saved_processing_template_decl;
2111 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
2112 fn = add_inherited_template_parms (fn, inherited_ctor);
2114 /* Warn about calling a non-trivial move assignment in a virtual base. */
2115 if (kind == sfk_move_assignment && !deleted_p && !trivial_p
2116 && CLASSTYPE_VBASECLASSES (type))
2118 location_t loc = input_location;
2119 input_location = DECL_SOURCE_LOCATION (fn);
2120 synthesized_method_walk (type, kind, const_p,
2121 NULL, NULL, NULL, NULL, true,
2122 NULL_TREE, NULL_TREE);
2123 input_location = loc;
2126 return fn;
2129 /* Gives any errors about defaulted functions which need to be deferred
2130 until the containing class is complete. */
2132 void
2133 defaulted_late_check (tree fn)
2135 /* Complain about invalid signature for defaulted fn. */
2136 tree ctx = DECL_CONTEXT (fn);
2137 special_function_kind kind = special_function_p (fn);
2138 bool fn_const_p = (copy_fn_p (fn) == 2);
2139 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
2140 NULL, NULL);
2141 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
2143 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
2144 TREE_TYPE (TREE_TYPE (implicit_fn)))
2145 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2146 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn))))
2148 error ("defaulted declaration %q+D", fn);
2149 error_at (DECL_SOURCE_LOCATION (fn),
2150 "does not match expected signature %qD", implicit_fn);
2153 /* 8.4.2/2: An explicitly-defaulted function (...) may have an explicit
2154 exception-specification only if it is compatible (15.4) with the
2155 exception-specification on the implicit declaration. If a function
2156 is explicitly defaulted on its first declaration, (...) it is
2157 implicitly considered to have the same exception-specification as if
2158 it had been implicitly declared. */
2159 maybe_instantiate_noexcept (fn);
2160 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2161 if (!fn_spec)
2163 if (DECL_DEFAULTED_IN_CLASS_P (fn))
2164 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
2166 else if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
2167 /* Equivalent to the implicit spec. */;
2168 else if (DECL_DEFAULTED_IN_CLASS_P (fn)
2169 && !CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
2170 /* We can't compare an explicit exception-specification on a
2171 constructor defaulted in the class body to the implicit
2172 exception-specification until after we've parsed any NSDMI; see
2173 after_nsdmi_defaulted_late_checks. */;
2174 else
2176 tree eh_spec = get_defaulted_eh_spec (fn);
2177 if (!comp_except_specs (fn_spec, eh_spec, ce_normal))
2179 if (DECL_DEFAULTED_IN_CLASS_P (fn))
2180 DECL_DELETED_FN (fn) = true;
2181 else
2182 error ("function %q+D defaulted on its redeclaration "
2183 "with an exception-specification that differs from "
2184 "the implicit exception-specification %qX", fn, eh_spec);
2188 if (DECL_DEFAULTED_IN_CLASS_P (fn)
2189 && DECL_DECLARED_CONSTEXPR_P (implicit_fn))
2191 /* Hmm...should we do this for out-of-class too? Should it be OK to
2192 add constexpr later like inline, rather than requiring
2193 declarations to match? */
2194 DECL_DECLARED_CONSTEXPR_P (fn) = true;
2195 if (kind == sfk_constructor)
2196 TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
2199 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
2200 && DECL_DECLARED_CONSTEXPR_P (fn))
2202 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
2204 error ("explicitly defaulted function %q+D cannot be declared "
2205 "as constexpr because the implicit declaration is not "
2206 "constexpr:", fn);
2207 explain_implicit_non_constexpr (fn);
2209 DECL_DECLARED_CONSTEXPR_P (fn) = false;
2212 if (DECL_DELETED_FN (implicit_fn))
2213 DECL_DELETED_FN (fn) = 1;
2216 /* OK, we've parsed the NSDMI for class T, now we can check any explicit
2217 exception-specifications on functions defaulted in the class body. */
2219 void
2220 after_nsdmi_defaulted_late_checks (tree t)
2222 if (uses_template_parms (t))
2223 return;
2224 if (t == error_mark_node)
2225 return;
2226 for (tree fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
2227 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
2229 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2230 if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
2231 continue;
2233 tree eh_spec = get_defaulted_eh_spec (fn);
2234 if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)),
2235 eh_spec, ce_normal))
2236 DECL_DELETED_FN (fn) = true;
2240 /* Returns true iff FN can be explicitly defaulted, and gives any
2241 errors if defaulting FN is ill-formed. */
2243 bool
2244 defaultable_fn_check (tree fn)
2246 special_function_kind kind = sfk_none;
2248 if (template_parm_scope_p ())
2250 error ("a template cannot be defaulted");
2251 return false;
2254 if (DECL_CONSTRUCTOR_P (fn))
2256 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
2257 kind = sfk_constructor;
2258 else if (copy_fn_p (fn) > 0
2259 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
2260 == void_list_node))
2261 kind = sfk_copy_constructor;
2262 else if (move_fn_p (fn))
2263 kind = sfk_move_constructor;
2265 else if (DECL_DESTRUCTOR_P (fn))
2266 kind = sfk_destructor;
2267 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2268 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
2270 if (copy_fn_p (fn))
2271 kind = sfk_copy_assignment;
2272 else if (move_fn_p (fn))
2273 kind = sfk_move_assignment;
2276 if (kind == sfk_none)
2278 error ("%qD cannot be defaulted", fn);
2279 return false;
2281 else
2283 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
2284 t && t != void_list_node; t = TREE_CHAIN (t))
2285 if (TREE_PURPOSE (t))
2287 error ("defaulted function %q+D with default argument", fn);
2288 break;
2291 /* Avoid do_warn_unused_parameter warnings. */
2292 for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p))
2293 if (DECL_NAME (p))
2294 TREE_NO_WARNING (p) = 1;
2296 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn)))
2297 /* Defer checking. */;
2298 else if (!processing_template_decl)
2299 defaulted_late_check (fn);
2301 return true;
2305 /* Add an implicit declaration to TYPE for the kind of function
2306 indicated by SFK. Return the FUNCTION_DECL for the new implicit
2307 declaration. */
2309 tree
2310 lazily_declare_fn (special_function_kind sfk, tree type)
2312 tree fn;
2313 /* Whether or not the argument has a const reference type. */
2314 bool const_p = false;
2316 type = TYPE_MAIN_VARIANT (type);
2318 switch (sfk)
2320 case sfk_constructor:
2321 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
2322 break;
2323 case sfk_copy_constructor:
2324 const_p = TYPE_HAS_CONST_COPY_CTOR (type);
2325 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
2326 break;
2327 case sfk_move_constructor:
2328 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
2329 break;
2330 case sfk_copy_assignment:
2331 const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
2332 CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
2333 break;
2334 case sfk_move_assignment:
2335 CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
2336 break;
2337 case sfk_destructor:
2338 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
2339 break;
2340 default:
2341 gcc_unreachable ();
2344 /* Declare the function. */
2345 fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL);
2347 /* [class.copy]/8 If the class definition declares a move constructor or
2348 move assignment operator, the implicitly declared copy constructor is
2349 defined as deleted.... */
2350 if ((sfk == sfk_copy_assignment
2351 || sfk == sfk_copy_constructor)
2352 && (type_has_user_declared_move_constructor (type)
2353 || type_has_user_declared_move_assign (type)))
2354 DECL_DELETED_FN (fn) = true;
2356 /* A destructor may be virtual. */
2357 if (sfk == sfk_destructor
2358 || sfk == sfk_move_assignment
2359 || sfk == sfk_copy_assignment)
2360 check_for_override (fn, type);
2361 /* Add it to CLASSTYPE_METHOD_VEC. */
2362 add_method (type, fn, false);
2363 /* Add it to TYPE_METHODS. */
2364 if (sfk == sfk_destructor
2365 && DECL_VIRTUAL_P (fn))
2366 /* The ABI requires that a virtual destructor go at the end of the
2367 vtable. */
2368 TYPE_METHODS (type) = chainon (TYPE_METHODS (type), fn);
2369 else
2371 DECL_CHAIN (fn) = TYPE_METHODS (type);
2372 TYPE_METHODS (type) = fn;
2374 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
2375 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
2376 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
2377 /* Create appropriate clones. */
2378 clone_function_decl (fn, /*update_methods=*/true);
2380 return fn;
2383 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
2384 as there are artificial parms in FN. */
2386 tree
2387 skip_artificial_parms_for (const_tree fn, tree list)
2389 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2390 list = TREE_CHAIN (list);
2391 else
2392 return list;
2394 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2395 list = TREE_CHAIN (list);
2396 if (DECL_HAS_VTT_PARM_P (fn))
2397 list = TREE_CHAIN (list);
2398 return list;
2401 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
2402 artificial parms in FN. */
2405 num_artificial_parms_for (const_tree fn)
2407 int count = 0;
2409 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2410 count++;
2411 else
2412 return 0;
2414 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2415 count++;
2416 if (DECL_HAS_VTT_PARM_P (fn))
2417 count++;
2418 return count;
2422 #include "gt-cp-method.h"