libgo: add misc/cgo files
[official-gcc.git] / gcc / cp / method.c
blob4cb52f23b3322384aae82ebcecc93115a544218d
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 warning_sentinel w (warn_useless_cast);
490 exp = build_static_cast (type, exp, tf_warning_or_error);
491 if (DECL_PACK_P (parm))
492 exp = make_pack_expansion (exp);
493 return exp;
496 /* Strip all inheriting constructors, if any, to return the original
497 constructor from a (possibly indirect) base class. */
499 tree
500 strip_inheriting_ctors (tree dfn)
502 if (!flag_new_inheriting_ctors)
503 return dfn;
504 tree fn = dfn;
505 while (tree inh = DECL_INHERITED_CTOR (fn))
506 fn = OVL_FIRST (inh);
508 if (TREE_CODE (fn) == TEMPLATE_DECL
509 && TREE_CODE (dfn) == FUNCTION_DECL)
510 fn = DECL_TEMPLATE_RESULT (fn);
511 return fn;
514 /* Find the binfo for the base subobject of BINFO being initialized by
515 inherited constructor FNDECL (a member of a direct base of BINFO). */
517 static tree inherited_ctor_binfo (tree, tree);
518 static tree
519 inherited_ctor_binfo_1 (tree binfo, tree fndecl)
521 tree base = DECL_CONTEXT (fndecl);
522 tree base_binfo;
523 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
524 if (BINFO_TYPE (base_binfo) == base)
525 return inherited_ctor_binfo (base_binfo, fndecl);
527 gcc_unreachable();
530 /* Find the binfo for the base subobject of BINFO being initialized by
531 inheriting constructor FNDECL (a member of BINFO), or BINFO if FNDECL is not
532 an inheriting constructor. */
534 static tree
535 inherited_ctor_binfo (tree binfo, tree fndecl)
537 tree inh = DECL_INHERITED_CTOR (fndecl);
538 if (!inh)
539 return binfo;
541 tree results = NULL_TREE;
542 for (ovl_iterator iter (inh); iter; ++iter)
544 tree one = inherited_ctor_binfo_1 (binfo, *iter);
545 if (!results)
546 results = one;
547 else if (one != results)
548 results = tree_cons (NULL_TREE, one, results);
550 return results;
553 /* Find the binfo for the base subobject being initialized by inheriting
554 constructor FNDECL, or NULL_TREE if FNDECL is not an inheriting
555 constructor. */
557 tree
558 inherited_ctor_binfo (tree fndecl)
560 if (!DECL_INHERITED_CTOR (fndecl))
561 return NULL_TREE;
562 tree binfo = TYPE_BINFO (DECL_CONTEXT (fndecl));
563 return inherited_ctor_binfo (binfo, fndecl);
566 /* True if we should omit all user-declared parameters from constructor FN,
567 because it is a base clone of a ctor inherited from a virtual base. */
569 bool
570 ctor_omit_inherited_parms (tree fn)
572 if (!flag_new_inheriting_ctors)
573 /* We only optimize away the parameters in the new model. */
574 return false;
575 if (!DECL_BASE_CONSTRUCTOR_P (fn)
576 || !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
577 return false;
578 if (FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn)) == void_list_node)
579 /* No user-declared parameters to omit. */
580 return false;
581 tree binfo = inherited_ctor_binfo (fn);
582 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
583 if (BINFO_VIRTUAL_P (binfo))
584 return true;
585 return false;
588 /* True iff constructor(s) INH inherited into BINFO initializes INIT_BINFO.
589 This can be true for multiple virtual bases as well as one direct
590 non-virtual base. */
592 static bool
593 binfo_inherited_from (tree binfo, tree init_binfo, tree inh)
595 /* inh is an OVERLOAD if we inherited the same constructor along
596 multiple paths, check all of them. */
597 for (ovl_iterator iter (inh); iter; ++iter)
599 tree fn = *iter;
600 tree base = DECL_CONTEXT (fn);
601 tree base_binfo = NULL_TREE;
602 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
603 if (BINFO_TYPE (base_binfo) == base)
604 break;
605 if (base_binfo == init_binfo
606 || (flag_new_inheriting_ctors
607 && binfo_inherited_from (base_binfo, init_binfo,
608 DECL_INHERITED_CTOR (fn))))
609 return true;
611 return false;
614 /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO
615 given the parameter or parameters PARM, possibly inherited constructor
616 base INH, or move flag MOVE_P. */
618 static tree
619 add_one_base_init (tree binfo, tree parm, bool move_p, tree inh,
620 tree member_init_list)
622 tree init;
623 if (inh)
625 /* An inheriting constructor only has a mem-initializer for
626 the base it inherits from. */
627 if (!binfo_inherited_from (TYPE_BINFO (current_class_type), binfo, inh))
628 return member_init_list;
630 tree *p = &init;
631 init = NULL_TREE;
632 for (; parm; parm = DECL_CHAIN (parm))
634 tree exp = forward_parm (parm);
635 *p = build_tree_list (NULL_TREE, exp);
636 p = &TREE_CHAIN (*p);
639 else
641 init = build_base_path (PLUS_EXPR, parm, binfo, 1,
642 tf_warning_or_error);
643 if (move_p)
644 init = move (init);
645 init = build_tree_list (NULL_TREE, init);
647 return tree_cons (binfo, init, member_init_list);
650 /* Generate code for default X(X&) or X(X&&) constructor or an inheriting
651 constructor. */
653 static void
654 do_build_copy_constructor (tree fndecl)
656 tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
657 bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
658 bool trivial = trivial_fn_p (fndecl);
659 tree inh = DECL_INHERITED_CTOR (fndecl);
661 if (!inh)
662 parm = convert_from_reference (parm);
664 if (trivial)
666 if (is_empty_class (current_class_type))
667 /* Don't copy the padding byte; it might not have been allocated
668 if *this is a base subobject. */;
669 else if (tree_int_cst_equal (TYPE_SIZE (current_class_type),
670 CLASSTYPE_SIZE (current_class_type)))
672 tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm);
673 finish_expr_stmt (t);
675 else
677 /* We must only copy the non-tail padding parts. */
678 tree base_size = CLASSTYPE_SIZE_UNIT (current_class_type);
679 base_size = size_binop (MINUS_EXPR, base_size, size_int (1));
680 tree array_type = build_array_type (unsigned_char_type_node,
681 build_index_type (base_size));
682 tree alias_set = build_int_cst (TREE_TYPE (current_class_ptr), 0);
683 tree lhs = build2 (MEM_REF, array_type,
684 current_class_ptr, alias_set);
685 tree rhs = build2 (MEM_REF, array_type,
686 TREE_OPERAND (parm, 0), alias_set);
687 tree t = build2 (INIT_EXPR, void_type_node, lhs, rhs);
688 finish_expr_stmt (t);
691 else
693 tree fields = TYPE_FIELDS (current_class_type);
694 tree member_init_list = NULL_TREE;
695 int cvquals = cp_type_quals (TREE_TYPE (parm));
696 int i;
697 tree binfo, base_binfo;
698 tree init;
699 vec<tree, va_gc> *vbases;
701 /* Initialize all the base-classes with the parameter converted
702 to their type so that we get their copy constructor and not
703 another constructor that takes current_class_type. We must
704 deal with the binfo's directly as a direct base might be
705 inaccessible due to ambiguity. */
706 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
707 vec_safe_iterate (vbases, i, &binfo); i++)
709 member_init_list = add_one_base_init (binfo, parm, move_p, inh,
710 member_init_list);
713 for (binfo = TYPE_BINFO (current_class_type), i = 0;
714 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
716 if (BINFO_VIRTUAL_P (base_binfo))
717 continue;
718 member_init_list = add_one_base_init (base_binfo, parm, move_p,
719 inh, member_init_list);
722 for (; fields; fields = DECL_CHAIN (fields))
724 tree field = fields;
725 tree expr_type;
727 if (TREE_CODE (field) != FIELD_DECL)
728 continue;
729 if (inh)
730 continue;
732 expr_type = TREE_TYPE (field);
733 if (DECL_NAME (field))
735 if (VFIELD_NAME_P (DECL_NAME (field)))
736 continue;
738 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
739 /* Just use the field; anonymous types can't have
740 nontrivial copy ctors or assignment ops or this
741 function would be deleted. */;
742 else
743 continue;
745 /* Compute the type of "init->field". If the copy-constructor
746 parameter is, for example, "const S&", and the type of
747 the field is "T", then the type will usually be "const
748 T". (There are no cv-qualified variants of reference
749 types.) */
750 if (TREE_CODE (expr_type) != REFERENCE_TYPE)
752 int quals = cvquals;
754 if (DECL_MUTABLE_P (field))
755 quals &= ~TYPE_QUAL_CONST;
756 quals |= cp_type_quals (expr_type);
757 expr_type = cp_build_qualified_type (expr_type, quals);
760 init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
761 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
762 /* 'move' breaks bit-fields, and has no effect for scalars. */
763 && !scalarish_type_p (expr_type))
764 init = move (init);
765 init = build_tree_list (NULL_TREE, init);
767 member_init_list = tree_cons (field, init, member_init_list);
769 finish_mem_initializers (member_init_list);
773 static void
774 do_build_copy_assign (tree fndecl)
776 tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl));
777 tree compound_stmt;
778 bool move_p = move_fn_p (fndecl);
779 bool trivial = trivial_fn_p (fndecl);
780 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
782 compound_stmt = begin_compound_stmt (0);
783 parm = convert_from_reference (parm);
785 if (trivial
786 && is_empty_class (current_class_type))
787 /* Don't copy the padding byte; it might not have been allocated
788 if *this is a base subobject. */;
789 else if (trivial)
791 tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
792 finish_expr_stmt (t);
794 else
796 tree fields;
797 int cvquals = cp_type_quals (TREE_TYPE (parm));
798 int i;
799 tree binfo, base_binfo;
801 /* Assign to each of the direct base classes. */
802 for (binfo = TYPE_BINFO (current_class_type), i = 0;
803 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
805 tree converted_parm;
806 vec<tree, va_gc> *parmvec;
808 /* We must convert PARM directly to the base class
809 explicitly since the base class may be ambiguous. */
810 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
811 tf_warning_or_error);
812 if (move_p)
813 converted_parm = move (converted_parm);
814 /* Call the base class assignment operator. */
815 parmvec = make_tree_vector_single (converted_parm);
816 finish_expr_stmt
817 (build_special_member_call (current_class_ref,
818 cp_assignment_operator_id (NOP_EXPR),
819 &parmvec,
820 base_binfo,
821 flags,
822 tf_warning_or_error));
823 release_tree_vector (parmvec);
826 /* Assign to each of the non-static data members. */
827 for (fields = TYPE_FIELDS (current_class_type);
828 fields;
829 fields = DECL_CHAIN (fields))
831 tree comp = current_class_ref;
832 tree init = parm;
833 tree field = fields;
834 tree expr_type;
835 int quals;
837 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
838 continue;
840 expr_type = TREE_TYPE (field);
842 if (CP_TYPE_CONST_P (expr_type))
844 error ("non-static const member %q#D, can%'t use default "
845 "assignment operator", field);
846 continue;
848 else if (TREE_CODE (expr_type) == REFERENCE_TYPE)
850 error ("non-static reference member %q#D, can%'t use "
851 "default assignment operator", field);
852 continue;
855 if (DECL_NAME (field))
857 if (VFIELD_NAME_P (DECL_NAME (field)))
858 continue;
860 else if (ANON_AGGR_TYPE_P (expr_type)
861 && TYPE_FIELDS (expr_type) != NULL_TREE)
862 /* Just use the field; anonymous types can't have
863 nontrivial copy ctors or assignment ops or this
864 function would be deleted. */;
865 else
866 continue;
868 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
870 /* Compute the type of init->field */
871 quals = cvquals;
872 if (DECL_MUTABLE_P (field))
873 quals &= ~TYPE_QUAL_CONST;
874 expr_type = cp_build_qualified_type (expr_type, quals);
876 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
877 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
878 /* 'move' breaks bit-fields, and has no effect for scalars. */
879 && !scalarish_type_p (expr_type))
880 init = move (init);
882 if (DECL_NAME (field))
883 init = cp_build_modify_expr (input_location, comp, NOP_EXPR, init,
884 tf_warning_or_error);
885 else
886 init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
887 finish_expr_stmt (init);
890 finish_return_stmt (current_class_ref);
891 finish_compound_stmt (compound_stmt);
894 /* Synthesize FNDECL, a non-static member function. */
896 void
897 synthesize_method (tree fndecl)
899 bool nested = (current_function_decl != NULL_TREE);
900 tree context = decl_function_context (fndecl);
901 bool need_body = true;
902 tree stmt;
903 location_t save_input_location = input_location;
904 int error_count = errorcount;
905 int warning_count = warningcount + werrorcount;
907 /* Reset the source location, we might have been previously
908 deferred, and thus have saved where we were first needed. */
909 DECL_SOURCE_LOCATION (fndecl)
910 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
912 /* If we've been asked to synthesize a clone, just synthesize the
913 cloned function instead. Doing so will automatically fill in the
914 body for the clone. */
915 if (DECL_CLONED_FUNCTION_P (fndecl))
916 fndecl = DECL_CLONED_FUNCTION (fndecl);
918 /* We may be in the middle of deferred access check. Disable
919 it now. */
920 push_deferring_access_checks (dk_no_deferred);
922 if (! context)
923 push_to_top_level ();
924 else if (nested)
925 push_function_context ();
927 input_location = DECL_SOURCE_LOCATION (fndecl);
929 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
930 stmt = begin_function_body ();
932 if (DECL_OVERLOADED_OPERATOR_P (fndecl) == NOP_EXPR)
934 do_build_copy_assign (fndecl);
935 need_body = false;
937 else if (DECL_CONSTRUCTOR_P (fndecl))
939 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
940 if (arg_chain != void_list_node)
941 do_build_copy_constructor (fndecl);
942 else
943 finish_mem_initializers (NULL_TREE);
946 /* If we haven't yet generated the body of the function, just
947 generate an empty compound statement. */
948 if (need_body)
950 tree compound_stmt;
951 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
952 finish_compound_stmt (compound_stmt);
955 finish_function_body (stmt);
956 expand_or_defer_fn (finish_function (0));
958 input_location = save_input_location;
960 if (! context)
961 pop_from_top_level ();
962 else if (nested)
963 pop_function_context ();
965 pop_deferring_access_checks ();
967 if (error_count != errorcount || warning_count != warningcount + werrorcount)
968 inform (input_location, "synthesized method %qD first required here ",
969 fndecl);
972 /* Build a reference to type TYPE with cv-quals QUALS, which is an
973 rvalue if RVALUE is true. */
975 static tree
976 build_stub_type (tree type, int quals, bool rvalue)
978 tree argtype = cp_build_qualified_type (type, quals);
979 return cp_build_reference_type (argtype, rvalue);
982 /* Build a dummy glvalue from dereferencing a dummy reference of type
983 REFTYPE. */
985 static tree
986 build_stub_object (tree reftype)
988 if (TREE_CODE (reftype) != REFERENCE_TYPE)
989 reftype = cp_build_reference_type (reftype, /*rval*/true);
990 tree stub = build1 (CONVERT_EXPR, reftype, integer_one_node);
991 return convert_from_reference (stub);
994 /* Determine which function will be called when looking up NAME in TYPE,
995 called with a single ARGTYPE argument, or no argument if ARGTYPE is
996 null. FLAGS and COMPLAIN are as for build_new_method_call.
998 Returns a FUNCTION_DECL if all is well.
999 Returns NULL_TREE if overload resolution failed.
1000 Returns error_mark_node if the chosen function cannot be called. */
1002 static tree
1003 locate_fn_flags (tree type, tree name, tree argtype, int flags,
1004 tsubst_flags_t complain)
1006 tree ob, fn, fns, binfo, rval;
1007 vec<tree, va_gc> *args;
1009 if (TYPE_P (type))
1010 binfo = TYPE_BINFO (type);
1011 else
1013 binfo = type;
1014 type = BINFO_TYPE (binfo);
1017 ob = build_stub_object (cp_build_reference_type (type, false));
1018 args = make_tree_vector ();
1019 if (argtype)
1021 if (TREE_CODE (argtype) == TREE_LIST)
1023 for (tree elt = argtype; elt && elt != void_list_node;
1024 elt = TREE_CHAIN (elt))
1026 tree type = TREE_VALUE (elt);
1027 tree arg = build_stub_object (type);
1028 vec_safe_push (args, arg);
1031 else
1033 tree arg = build_stub_object (argtype);
1034 args->quick_push (arg);
1038 fns = lookup_fnfields (binfo, name, 0);
1039 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
1041 release_tree_vector (args);
1042 if (fn && rval == error_mark_node)
1043 return rval;
1044 else
1045 return fn;
1048 /* Locate the dtor of TYPE. */
1050 tree
1051 get_dtor (tree type, tsubst_flags_t complain)
1053 tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
1054 LOOKUP_NORMAL, complain);
1055 if (fn == error_mark_node)
1056 return NULL_TREE;
1057 return fn;
1060 /* Locate the default ctor of TYPE. */
1062 tree
1063 locate_ctor (tree type)
1065 tree fn;
1067 push_deferring_access_checks (dk_no_check);
1068 fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
1069 LOOKUP_SPECULATIVE, tf_none);
1070 pop_deferring_access_checks ();
1071 if (fn == error_mark_node)
1072 return NULL_TREE;
1073 return fn;
1076 /* Likewise, but give any appropriate errors. */
1078 tree
1079 get_default_ctor (tree type)
1081 tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
1082 LOOKUP_NORMAL, tf_warning_or_error);
1083 if (fn == error_mark_node)
1084 return NULL_TREE;
1085 return fn;
1088 /* Locate the copy ctor of TYPE. */
1090 tree
1091 get_copy_ctor (tree type, tsubst_flags_t complain)
1093 int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
1094 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
1095 tree argtype = build_stub_type (type, quals, false);
1096 tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
1097 LOOKUP_NORMAL, complain);
1098 if (fn == error_mark_node)
1099 return NULL_TREE;
1100 return fn;
1103 /* Locate the copy assignment operator of TYPE. */
1105 tree
1106 get_copy_assign (tree type)
1108 int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
1109 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
1110 tree argtype = build_stub_type (type, quals, false);
1111 tree fn = locate_fn_flags (type, cp_assignment_operator_id (NOP_EXPR), argtype,
1112 LOOKUP_NORMAL, tf_warning_or_error);
1113 if (fn == error_mark_node)
1114 return NULL_TREE;
1115 return fn;
1118 /* walk_tree helper function for is_trivially_xible. If *TP is a call,
1119 return it if it calls something other than a trivial special member
1120 function. */
1122 static tree
1123 check_nontriv (tree *tp, int *, void *)
1125 tree fn = cp_get_callee (*tp);
1126 if (fn == NULL_TREE)
1127 return NULL_TREE;
1129 if (TREE_CODE (fn) == ADDR_EXPR)
1130 fn = TREE_OPERAND (fn, 0);
1132 if (TREE_CODE (fn) != FUNCTION_DECL
1133 || !trivial_fn_p (fn))
1134 return fn;
1135 return NULL_TREE;
1138 /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */
1140 static tree
1141 assignable_expr (tree to, tree from)
1143 ++cp_unevaluated_operand;
1144 to = build_stub_object (to);
1145 from = build_stub_object (from);
1146 tree r = cp_build_modify_expr (input_location, to, NOP_EXPR, from, tf_none);
1147 --cp_unevaluated_operand;
1148 return r;
1151 /* The predicate condition for a template specialization
1152 is_constructible<T, Args...> shall be satisfied if and only if the
1153 following variable definition would be well-formed for some invented
1154 variable t: T t(create<Args>()...);
1156 Return something equivalent in well-formedness and triviality. */
1158 static tree
1159 constructible_expr (tree to, tree from)
1161 tree expr;
1162 if (CLASS_TYPE_P (to))
1164 tree ctype = to;
1165 vec<tree, va_gc> *args = NULL;
1166 cp_unevaluated cp_uneval_guard;
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 (strip_array_types (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 a tree iff TO is 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 static tree
1208 is_xible_helper (enum tree_code code, tree to, tree from, bool trivial)
1210 if (VOID_TYPE_P (to) || ABSTRACT_CLASS_TYPE_P (to)
1211 || (from && FUNC_OR_METHOD_TYPE_P (from)
1212 && (TYPE_READONLY (from) || FUNCTION_REF_QUALIFIED (from))))
1213 return error_mark_node;
1214 tree expr;
1215 if (code == MODIFY_EXPR)
1216 expr = assignable_expr (to, from);
1217 else if (trivial && from && TREE_CHAIN (from))
1218 return error_mark_node; // only 0- and 1-argument ctors can be trivial
1219 else
1220 expr = constructible_expr (to, from);
1221 return expr;
1224 /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or
1225 constructible (otherwise) from FROM, which is a single type for
1226 assignment or a list of types for construction. */
1228 bool
1229 is_trivially_xible (enum tree_code code, tree to, tree from)
1231 tree expr;
1232 expr = is_xible_helper (code, to, from, /*trivial*/true);
1234 if (expr == error_mark_node)
1235 return false;
1236 tree nt = cp_walk_tree_without_duplicates (&expr, check_nontriv, NULL);
1237 return !nt;
1240 /* Returns true iff TO is assignable (if CODE is MODIFY_EXPR) or
1241 constructible (otherwise) from FROM, which is a single type for
1242 assignment or a list of types for construction. */
1244 bool
1245 is_xible (enum tree_code code, tree to, tree from)
1247 tree expr = is_xible_helper (code, to, from, /*trivial*/false);
1248 if (expr == error_mark_node)
1249 return false;
1250 return !!expr;
1253 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
1254 DELETED_P or give an error message MSG with argument ARG. */
1256 static void
1257 process_subob_fn (tree fn, tree *spec_p, bool *trivial_p,
1258 bool *deleted_p, bool *constexpr_p,
1259 bool diag, tree arg, bool dtor_from_ctor = false)
1261 if (!fn || fn == error_mark_node)
1263 if (deleted_p)
1264 *deleted_p = true;
1265 return;
1268 if (spec_p)
1270 maybe_instantiate_noexcept (fn);
1271 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
1272 *spec_p = merge_exception_specifiers (*spec_p, raises);
1275 if (!trivial_fn_p (fn) && !dtor_from_ctor)
1277 if (trivial_p)
1278 *trivial_p = false;
1279 if (TREE_CODE (arg) == FIELD_DECL
1280 && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
1282 if (deleted_p)
1283 *deleted_p = true;
1284 if (diag)
1285 error ("union member %q+D with non-trivial %qD", arg, fn);
1289 if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
1291 *constexpr_p = false;
1292 if (diag)
1294 inform (DECL_SOURCE_LOCATION (fn),
1295 "defaulted constructor calls non-constexpr %qD", fn);
1296 explain_invalid_constexpr_fn (fn);
1301 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
1302 aggregates. If DTOR_FROM_CTOR is true, we're walking subobject destructors
1303 called from a synthesized constructor, in which case we don't consider
1304 the triviality of the subobject destructor. */
1306 static void
1307 walk_field_subobs (tree fields, tree fnname, special_function_kind sfk,
1308 int quals, bool copy_arg_p, bool move_p,
1309 bool assign_p, tree *spec_p, bool *trivial_p,
1310 bool *deleted_p, bool *constexpr_p,
1311 bool diag, int flags, tsubst_flags_t complain,
1312 bool dtor_from_ctor)
1314 tree field;
1315 for (field = fields; field; field = DECL_CHAIN (field))
1317 tree mem_type, argtype, rval;
1319 if (TREE_CODE (field) != FIELD_DECL
1320 || DECL_ARTIFICIAL (field))
1321 continue;
1323 mem_type = strip_array_types (TREE_TYPE (field));
1324 if (assign_p)
1326 bool bad = true;
1327 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
1329 if (diag)
1330 error ("non-static const member %q#D, can%'t use default "
1331 "assignment operator", field);
1333 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1335 if (diag)
1336 error ("non-static reference member %q#D, can%'t use "
1337 "default assignment operator", field);
1339 else
1340 bad = false;
1342 if (bad && deleted_p)
1343 *deleted_p = true;
1345 else if (sfk == sfk_constructor)
1347 bool bad;
1349 if (DECL_INITIAL (field))
1351 if (diag && DECL_INITIAL (field) == error_mark_node)
1352 inform (DECL_SOURCE_LOCATION (field),
1353 "initializer for %q#D is invalid", field);
1354 if (trivial_p)
1355 *trivial_p = false;
1356 /* Core 1351: If the field has an NSDMI that could throw, the
1357 default constructor is noexcept(false). */
1358 if (spec_p)
1360 tree nsdmi = get_nsdmi (field, /*ctor*/false);
1361 if (!expr_noexcept_p (nsdmi, complain))
1362 *spec_p = noexcept_false_spec;
1364 /* Don't do the normal processing. */
1365 continue;
1368 bad = false;
1369 if (CP_TYPE_CONST_P (mem_type)
1370 && default_init_uninitialized_part (mem_type))
1372 if (diag)
1374 error ("uninitialized const member in %q#T",
1375 current_class_type);
1376 inform (DECL_SOURCE_LOCATION (field),
1377 "%q#D should be initialized", field);
1379 bad = true;
1381 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1383 if (diag)
1385 error ("uninitialized reference member in %q#T",
1386 current_class_type);
1387 inform (DECL_SOURCE_LOCATION (field),
1388 "%q#D should be initialized", field);
1390 bad = true;
1393 if (bad && deleted_p)
1394 *deleted_p = true;
1396 /* For an implicitly-defined default constructor to be constexpr,
1397 every member must have a user-provided default constructor or
1398 an explicit initializer. */
1399 if (constexpr_p && !CLASS_TYPE_P (mem_type)
1400 && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE)
1402 *constexpr_p = false;
1403 if (diag)
1404 inform (DECL_SOURCE_LOCATION (field),
1405 "defaulted default constructor does not "
1406 "initialize %q#D", field);
1409 else if (sfk == sfk_copy_constructor)
1411 /* 12.8p11b5 */
1412 if (TREE_CODE (mem_type) == REFERENCE_TYPE
1413 && TYPE_REF_IS_RVALUE (mem_type))
1415 if (diag)
1416 error ("copying non-static data member %q#D of rvalue "
1417 "reference type", field);
1418 if (deleted_p)
1419 *deleted_p = true;
1423 if (!CLASS_TYPE_P (mem_type))
1424 continue;
1426 if (ANON_AGGR_TYPE_P (mem_type))
1428 walk_field_subobs (TYPE_FIELDS (mem_type), fnname, sfk, quals,
1429 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1430 deleted_p, constexpr_p,
1431 diag, flags, complain, dtor_from_ctor);
1432 continue;
1435 if (copy_arg_p)
1437 int mem_quals = cp_type_quals (mem_type) | quals;
1438 if (DECL_MUTABLE_P (field))
1439 mem_quals &= ~TYPE_QUAL_CONST;
1440 argtype = build_stub_type (mem_type, mem_quals, move_p);
1442 else
1443 argtype = NULL_TREE;
1445 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
1447 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1448 constexpr_p, diag, field, dtor_from_ctor);
1452 /* Base walker helper for synthesized_method_walk. Inspect a direct
1453 or virtual base. BINFO is the parent type's binfo. BASE_BINFO is
1454 the base binfo of interests. All other parms are as for
1455 synthesized_method_walk, or its local vars. */
1457 static tree
1458 synthesized_method_base_walk (tree binfo, tree base_binfo,
1459 int quals, bool copy_arg_p,
1460 bool move_p, bool ctor_p,
1461 tree inheriting_ctor, tree inherited_parms,
1462 tree fnname, int flags, bool diag,
1463 tree *spec_p, bool *trivial_p,
1464 bool *deleted_p, bool *constexpr_p)
1466 bool inherited_binfo = false;
1467 tree argtype = NULL_TREE;
1468 deferring_kind defer = dk_no_deferred;
1470 if (copy_arg_p)
1471 argtype = build_stub_type (BINFO_TYPE (base_binfo), quals, move_p);
1472 else if ((inherited_binfo
1473 = binfo_inherited_from (binfo, base_binfo, inheriting_ctor)))
1475 argtype = inherited_parms;
1476 /* Don't check access on the inherited constructor. */
1477 if (flag_new_inheriting_ctors)
1478 defer = dk_deferred;
1480 /* To be conservative, ignore access to the base dtor that
1481 DR1658 instructs us to ignore. See the comment in
1482 synthesized_method_walk. */
1483 else if (cxx_dialect >= cxx14 && fnname == complete_dtor_identifier
1484 && BINFO_VIRTUAL_P (base_binfo)
1485 && ABSTRACT_CLASS_TYPE_P (BINFO_TYPE (binfo)))
1486 defer = dk_no_check;
1488 if (defer != dk_no_deferred)
1489 push_deferring_access_checks (defer);
1490 tree rval = locate_fn_flags (base_binfo, fnname, argtype, flags,
1491 diag ? tf_warning_or_error : tf_none);
1492 if (defer != dk_no_deferred)
1493 pop_deferring_access_checks ();
1495 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1496 constexpr_p, diag, BINFO_TYPE (base_binfo));
1497 if (ctor_p &&
1498 (!BINFO_VIRTUAL_P (base_binfo)
1499 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))))
1501 /* In a constructor we also need to check the subobject
1502 destructors for cleanup of partially constructed objects. */
1503 tree dtor = locate_fn_flags (base_binfo, complete_dtor_identifier,
1504 NULL_TREE, flags,
1505 diag ? tf_warning_or_error : tf_none);
1506 /* Note that we don't pass down trivial_p; the subobject
1507 destructors don't affect triviality of the constructor. Nor
1508 do they affect constexpr-ness (a constant expression doesn't
1509 throw) or exception-specification (a throw from one of the
1510 dtors would be a double-fault). */
1511 process_subob_fn (dtor, NULL, NULL, deleted_p, NULL, false,
1512 BINFO_TYPE (base_binfo), /*dtor_from_ctor*/true);
1515 return rval;
1518 /* The caller wants to generate an implicit declaration of SFK for
1519 CTYPE which is const if relevant and CONST_P is set. If SPEC_P,
1520 TRIVIAL_P, DELETED_P or CONSTEXPR_P are non-null, set their
1521 referent appropriately. If DIAG is true, we're either being called
1522 from maybe_explain_implicit_delete to give errors, or if
1523 CONSTEXPR_P is non-null, from explain_invalid_constexpr_fn. */
1525 static void
1526 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
1527 tree *spec_p, bool *trivial_p, bool *deleted_p,
1528 bool *constexpr_p, bool diag,
1529 tree inheriting_ctor, tree inherited_parms)
1531 tree binfo, base_binfo, fnname;
1532 int i;
1534 if (spec_p)
1535 *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec);
1537 if (deleted_p)
1539 /* "The closure type associated with a lambda-expression has a deleted
1540 default constructor and a deleted copy assignment operator."
1541 This is diagnosed in maybe_explain_implicit_delete. */
1542 if (LAMBDA_TYPE_P (ctype)
1543 && (sfk == sfk_constructor
1544 || sfk == sfk_copy_assignment))
1546 *deleted_p = true;
1547 return;
1550 *deleted_p = false;
1553 bool ctor_p = false;
1554 bool assign_p = false;
1555 bool check_vdtor = false;
1556 switch (sfk)
1558 case sfk_move_assignment:
1559 case sfk_copy_assignment:
1560 assign_p = true;
1561 fnname = cp_assignment_operator_id (NOP_EXPR);
1562 break;
1564 case sfk_destructor:
1565 check_vdtor = true;
1566 /* The synthesized method will call base dtors, but check complete
1567 here to avoid having to deal with VTT. */
1568 fnname = complete_dtor_identifier;
1569 break;
1571 case sfk_constructor:
1572 case sfk_move_constructor:
1573 case sfk_copy_constructor:
1574 case sfk_inheriting_constructor:
1575 ctor_p = true;
1576 fnname = complete_ctor_identifier;
1577 break;
1579 default:
1580 gcc_unreachable ();
1583 gcc_assert ((sfk == sfk_inheriting_constructor)
1584 == (inheriting_ctor != NULL_TREE));
1586 /* If that user-written default constructor would satisfy the
1587 requirements of a constexpr constructor (7.1.5), the
1588 implicitly-defined default constructor is constexpr.
1590 The implicitly-defined copy/move assignment operator is constexpr if
1591 - X is a literal type, and
1592 - the assignment operator selected to copy/move each direct base class
1593 subobject is a constexpr function, and
1594 - for each non-static data member of X that is of class type (or array
1595 thereof), the assignment operator selected to copy/move that
1596 member is a constexpr function. */
1597 if (constexpr_p)
1598 *constexpr_p = ctor_p || (assign_p && cxx_dialect >= cxx14);
1600 bool move_p = false;
1601 bool copy_arg_p = false;
1602 switch (sfk)
1604 case sfk_constructor:
1605 case sfk_destructor:
1606 case sfk_inheriting_constructor:
1607 break;
1609 case sfk_move_constructor:
1610 case sfk_move_assignment:
1611 move_p = true;
1612 /* FALLTHRU */
1613 case sfk_copy_constructor:
1614 case sfk_copy_assignment:
1615 copy_arg_p = true;
1616 break;
1618 default:
1619 gcc_unreachable ();
1622 bool expected_trivial = type_has_trivial_fn (ctype, sfk);
1623 if (trivial_p)
1624 *trivial_p = expected_trivial;
1626 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
1627 class versions and other properties of the type. But a subobject
1628 class can be trivially copyable and yet have overload resolution
1629 choose a template constructor for initialization, depending on
1630 rvalueness and cv-quals. And furthermore, a member in a base might
1631 be trivial but deleted or otherwise not callable. So we can't exit
1632 early in C++0x. The same considerations apply in C++98/03, but
1633 there the definition of triviality does not consider overload
1634 resolution, so a constructor can be trivial even if it would otherwise
1635 call a non-trivial constructor. */
1636 if (expected_trivial
1637 && (!copy_arg_p || cxx_dialect < cxx11))
1639 if (constexpr_p && sfk == sfk_constructor)
1641 bool cx = trivial_default_constructor_is_constexpr (ctype);
1642 *constexpr_p = cx;
1643 if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
1644 /* A trivial constructor doesn't have any NSDMI. */
1645 inform (input_location, "defaulted default constructor does "
1646 "not initialize any non-static data member");
1648 if (!diag && cxx_dialect < cxx11)
1649 return;
1652 ++cp_unevaluated_operand;
1653 ++c_inhibit_evaluation_warnings;
1654 push_deferring_access_checks (dk_no_deferred);
1656 tree scope = push_scope (ctype);
1658 int flags = LOOKUP_NORMAL | LOOKUP_SPECULATIVE;
1659 if (!inheriting_ctor)
1660 flags |= LOOKUP_DEFAULTED;
1662 tsubst_flags_t complain = diag ? tf_warning_or_error : tf_none;
1663 int quals = const_p ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED;
1665 for (binfo = TYPE_BINFO (ctype), i = 0;
1666 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
1668 if (!assign_p && BINFO_VIRTUAL_P (base_binfo))
1669 /* We'll handle virtual bases below. */
1670 continue;
1672 tree fn = synthesized_method_base_walk (binfo, base_binfo, quals,
1673 copy_arg_p, move_p, ctor_p,
1674 inheriting_ctor,
1675 inherited_parms,
1676 fnname, flags, diag,
1677 spec_p, trivial_p,
1678 deleted_p, constexpr_p);
1680 if (diag && assign_p && move_p
1681 && BINFO_VIRTUAL_P (base_binfo)
1682 && fn && TREE_CODE (fn) == FUNCTION_DECL
1683 && move_fn_p (fn) && !trivial_fn_p (fn)
1684 && vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
1685 warning (OPT_Wvirtual_move_assign,
1686 "defaulted move assignment for %qT calls a non-trivial "
1687 "move assignment operator for virtual base %qT",
1688 ctype, BINFO_TYPE (base_binfo));
1690 if (check_vdtor && type_has_virtual_destructor (BINFO_TYPE (base_binfo)))
1692 fn = locate_fn_flags (ctype, cp_operator_id (DELETE_EXPR),
1693 ptr_type_node, flags, complain);
1694 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
1695 to have a null fn (no class-specific op delete). */
1696 if (fn && fn == error_mark_node && deleted_p)
1697 *deleted_p = true;
1698 check_vdtor = false;
1702 vec<tree, va_gc> *vbases = CLASSTYPE_VBASECLASSES (ctype);
1703 if (assign_p)
1704 /* Already examined vbases above. */;
1705 else if (vec_safe_is_empty (vbases))
1706 /* No virtual bases to worry about. */;
1707 else if (ABSTRACT_CLASS_TYPE_P (ctype) && cxx_dialect >= cxx14
1708 /* DR 1658 specifies that vbases of abstract classes are
1709 ignored for both ctors and dtors. However, that breaks
1710 virtual dtor overriding when the ignored base has a
1711 throwing destructor. So, ignore that piece of 1658. A
1712 defect has been filed (no number yet). */
1713 && sfk != sfk_destructor)
1714 /* Vbase cdtors are not relevant. */;
1715 else
1717 if (constexpr_p)
1718 *constexpr_p = false;
1720 FOR_EACH_VEC_ELT (*vbases, i, base_binfo)
1721 synthesized_method_base_walk (binfo, base_binfo, quals,
1722 copy_arg_p, move_p, ctor_p,
1723 inheriting_ctor, inherited_parms,
1724 fnname, flags, diag,
1725 spec_p, trivial_p,
1726 deleted_p, constexpr_p);
1729 /* Now handle the non-static data members. */
1730 walk_field_subobs (TYPE_FIELDS (ctype), fnname, sfk, quals,
1731 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1732 deleted_p, constexpr_p,
1733 diag, flags, complain, /*dtor_from_ctor*/false);
1734 if (ctor_p)
1735 walk_field_subobs (TYPE_FIELDS (ctype), complete_dtor_identifier,
1736 sfk_destructor, TYPE_UNQUALIFIED, false,
1737 false, false, NULL, NULL,
1738 deleted_p, NULL,
1739 false, flags, complain, /*dtor_from_ctor*/true);
1741 pop_scope (scope);
1743 pop_deferring_access_checks ();
1744 --cp_unevaluated_operand;
1745 --c_inhibit_evaluation_warnings;
1748 /* DECL is a defaulted function whose exception specification is now
1749 needed. Return what it should be. */
1751 tree
1752 get_defaulted_eh_spec (tree decl)
1754 if (DECL_CLONED_FUNCTION_P (decl))
1755 decl = DECL_CLONED_FUNCTION (decl);
1756 special_function_kind sfk = special_function_p (decl);
1757 tree ctype = DECL_CONTEXT (decl);
1758 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1759 tree parm_type = TREE_VALUE (parms);
1760 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1761 tree spec = empty_except_spec;
1762 synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL,
1763 NULL, false, DECL_INHERITED_CTOR (decl),
1764 parms);
1765 return spec;
1768 /* DECL is a deleted function. If it's implicitly deleted, explain why and
1769 return true; else return false. */
1771 bool
1772 maybe_explain_implicit_delete (tree decl)
1774 /* If decl is a clone, get the primary variant. */
1775 decl = DECL_ORIGIN (decl);
1776 gcc_assert (DECL_DELETED_FN (decl));
1777 if (DECL_DEFAULTED_FN (decl))
1779 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
1780 static hash_set<tree> *explained;
1782 special_function_kind sfk;
1783 location_t loc;
1784 bool informed;
1785 tree ctype;
1787 if (!explained)
1788 explained = new hash_set<tree>;
1789 if (explained->add (decl))
1790 return true;
1792 sfk = special_function_p (decl);
1793 ctype = DECL_CONTEXT (decl);
1794 loc = input_location;
1795 input_location = DECL_SOURCE_LOCATION (decl);
1797 informed = false;
1798 if (LAMBDA_TYPE_P (ctype))
1800 informed = true;
1801 if (sfk == sfk_constructor)
1802 inform (DECL_SOURCE_LOCATION (decl),
1803 "a lambda closure type has a deleted default constructor");
1804 else if (sfk == sfk_copy_assignment)
1805 inform (DECL_SOURCE_LOCATION (decl),
1806 "a lambda closure type has a deleted copy assignment operator");
1807 else
1808 informed = false;
1810 else if (DECL_ARTIFICIAL (decl)
1811 && (sfk == sfk_copy_assignment
1812 || sfk == sfk_copy_constructor)
1813 && (type_has_user_declared_move_constructor (ctype)
1814 || type_has_user_declared_move_assign (ctype)))
1816 inform (DECL_SOURCE_LOCATION (decl),
1817 "%q#D is implicitly declared as deleted because %qT "
1818 "declares a move constructor or move assignment operator",
1819 decl, ctype);
1820 informed = true;
1822 else if (sfk == sfk_inheriting_constructor)
1824 tree binfo = inherited_ctor_binfo (decl);
1825 if (TREE_CODE (binfo) != TREE_BINFO)
1827 inform (DECL_SOURCE_LOCATION (decl),
1828 "%q#D inherits from multiple base subobjects",
1829 decl);
1830 informed = true;
1833 if (!informed)
1835 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1836 tree parm_type = TREE_VALUE (parms);
1837 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1838 tree raises = NULL_TREE;
1839 bool deleted_p = false;
1840 tree scope = push_scope (ctype);
1842 synthesized_method_walk (ctype, sfk, const_p,
1843 &raises, NULL, &deleted_p, NULL, false,
1844 DECL_INHERITED_CTOR (decl), parms);
1845 if (deleted_p)
1847 inform (DECL_SOURCE_LOCATION (decl),
1848 "%q#D is implicitly deleted because the default "
1849 "definition would be ill-formed:", decl);
1850 synthesized_method_walk (ctype, sfk, const_p,
1851 NULL, NULL, NULL, NULL, true,
1852 DECL_INHERITED_CTOR (decl), parms);
1854 else if (!comp_except_specs
1855 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
1856 raises, ce_normal))
1857 inform (DECL_SOURCE_LOCATION (decl), "%q#F is implicitly "
1858 "deleted because its exception-specification does not "
1859 "match the implicit exception-specification %qX",
1860 decl, raises);
1861 else if (flag_checking)
1862 gcc_unreachable ();
1864 pop_scope (scope);
1867 input_location = loc;
1868 return true;
1870 return false;
1873 /* DECL is a defaulted function which was declared constexpr. Explain why
1874 it can't be constexpr. */
1876 void
1877 explain_implicit_non_constexpr (tree decl)
1879 tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl));
1880 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1881 bool dummy;
1882 synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
1883 special_function_p (decl), const_p,
1884 NULL, NULL, NULL, &dummy, true,
1885 DECL_INHERITED_CTOR (decl),
1886 FUNCTION_FIRST_USER_PARMTYPE (decl));
1889 /* DECL is an instantiation of an inheriting constructor template. Deduce
1890 the correct exception-specification and deletedness for this particular
1891 specialization. */
1893 void
1894 deduce_inheriting_ctor (tree decl)
1896 decl = DECL_ORIGIN (decl);
1897 gcc_assert (DECL_INHERITED_CTOR (decl));
1898 tree spec;
1899 bool trivial, constexpr_, deleted;
1900 synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor,
1901 false, &spec, &trivial, &deleted, &constexpr_,
1902 /*diag*/false,
1903 DECL_INHERITED_CTOR (decl),
1904 FUNCTION_FIRST_USER_PARMTYPE (decl));
1905 if (TREE_CODE (inherited_ctor_binfo (decl)) != TREE_BINFO)
1906 /* Inherited the same constructor from different base subobjects. */
1907 deleted = true;
1908 DECL_DELETED_FN (decl) = deleted;
1909 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
1911 tree clone;
1912 FOR_EACH_CLONE (clone, decl)
1914 DECL_DELETED_FN (clone) = deleted;
1915 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
1919 /* Implicitly declare the special function indicated by KIND, as a
1920 member of TYPE. For copy constructors and assignment operators,
1921 CONST_P indicates whether these functions should take a const
1922 reference argument or a non-const reference. Returns the
1923 FUNCTION_DECL for the implicitly declared function. */
1925 tree
1926 implicitly_declare_fn (special_function_kind kind, tree type,
1927 bool const_p, tree inherited_ctor,
1928 tree inherited_parms)
1930 tree fn;
1931 tree parameter_types = void_list_node;
1932 tree return_type;
1933 tree fn_type;
1934 tree raises = empty_except_spec;
1935 tree rhs_parm_type = NULL_TREE;
1936 tree this_parm;
1937 tree name;
1938 HOST_WIDE_INT saved_processing_template_decl;
1939 bool deleted_p;
1940 bool constexpr_p;
1942 /* Because we create declarations for implicitly declared functions
1943 lazily, we may be creating the declaration for a member of TYPE
1944 while in some completely different context. However, TYPE will
1945 never be a dependent class (because we never want to do lookups
1946 for implicitly defined functions in a dependent class).
1947 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
1948 because we only create clones for constructors and destructors
1949 when not in a template. */
1950 gcc_assert (!dependent_type_p (type));
1951 saved_processing_template_decl = processing_template_decl;
1952 processing_template_decl = 0;
1954 type = TYPE_MAIN_VARIANT (type);
1956 if (targetm.cxx.cdtor_returns_this ())
1958 if (kind == sfk_destructor)
1959 /* See comment in check_special_function_return_type. */
1960 return_type = build_pointer_type (void_type_node);
1961 else
1962 return_type = build_pointer_type (type);
1964 else
1965 return_type = void_type_node;
1967 switch (kind)
1969 case sfk_destructor:
1970 /* Destructor. */
1971 name = constructor_name (type);
1972 break;
1974 case sfk_constructor:
1975 /* Default constructor. */
1976 name = constructor_name (type);
1977 break;
1979 case sfk_copy_constructor:
1980 case sfk_copy_assignment:
1981 case sfk_move_constructor:
1982 case sfk_move_assignment:
1983 case sfk_inheriting_constructor:
1985 bool move_p;
1986 if (kind == sfk_copy_assignment
1987 || kind == sfk_move_assignment)
1989 return_type = build_reference_type (type);
1990 name = cp_assignment_operator_id (NOP_EXPR);
1992 else
1993 name = constructor_name (type);
1995 if (kind == sfk_inheriting_constructor)
1996 parameter_types = inherited_parms;
1997 else
1999 if (const_p)
2000 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
2001 else
2002 rhs_parm_type = type;
2003 move_p = (kind == sfk_move_assignment
2004 || kind == sfk_move_constructor);
2005 rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
2007 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
2009 break;
2011 default:
2012 gcc_unreachable ();
2015 bool trivial_p = false;
2017 if (inherited_ctor)
2019 /* For an inheriting constructor, just copy these flags from the
2020 inherited constructor until deduce_inheriting_ctor. */
2021 raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor));
2022 deleted_p = DECL_DELETED_FN (inherited_ctor);
2023 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
2025 else if (cxx_dialect >= cxx11)
2027 raises = noexcept_deferred_spec;
2028 synthesized_method_walk (type, kind, const_p, NULL, &trivial_p,
2029 &deleted_p, &constexpr_p, false,
2030 inherited_ctor, inherited_parms);
2032 else
2033 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
2034 &deleted_p, &constexpr_p, false,
2035 inherited_ctor, inherited_parms);
2036 /* Don't bother marking a deleted constructor as constexpr. */
2037 if (deleted_p)
2038 constexpr_p = false;
2039 /* A trivial copy/move constructor is also a constexpr constructor,
2040 unless the class has virtual bases (7.1.5p4). */
2041 else if (trivial_p && cxx_dialect >= cxx11
2042 && (kind == sfk_copy_constructor
2043 || kind == sfk_move_constructor)
2044 && !CLASSTYPE_VBASECLASSES (type))
2045 gcc_assert (constexpr_p);
2047 if (!trivial_p && type_has_trivial_fn (type, kind))
2048 type_set_nontrivial_flag (type, kind);
2050 /* Create the function. */
2051 fn_type = build_method_type_directly (type, return_type, parameter_types);
2052 if (raises)
2053 fn_type = build_exception_variant (fn_type, raises);
2054 fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
2055 if (kind != sfk_inheriting_constructor)
2056 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
2057 if (kind == sfk_constructor || kind == sfk_copy_constructor
2058 || kind == sfk_move_constructor || kind == sfk_inheriting_constructor)
2059 DECL_CONSTRUCTOR_P (fn) = 1;
2060 else if (kind == sfk_destructor)
2061 DECL_DESTRUCTOR_P (fn) = 1;
2062 else
2063 SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR);
2065 SET_DECL_ALIGN (fn, MINIMUM_METHOD_BOUNDARY);
2067 /* Create the explicit arguments. */
2068 if (rhs_parm_type)
2070 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
2071 want its type to be included in the mangled function
2072 name. */
2073 tree decl = cp_build_parm_decl (fn, NULL_TREE, rhs_parm_type);
2074 TREE_READONLY (decl) = 1;
2075 retrofit_lang_decl (decl);
2076 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
2077 DECL_ARGUMENTS (fn) = decl;
2079 else if (kind == sfk_inheriting_constructor)
2081 tree *p = &DECL_ARGUMENTS (fn);
2082 int index = 1;
2083 for (tree parm = inherited_parms; parm && parm != void_list_node;
2084 parm = TREE_CHAIN (parm))
2086 *p = cp_build_parm_decl (fn, NULL_TREE, TREE_VALUE (parm));
2087 retrofit_lang_decl (*p);
2088 DECL_PARM_LEVEL (*p) = 1;
2089 DECL_PARM_INDEX (*p) = index++;
2090 p = &DECL_CHAIN (*p);
2092 SET_DECL_INHERITED_CTOR (fn, inherited_ctor);
2093 DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor);
2094 /* A constructor so declared has the same access as the corresponding
2095 constructor in X. */
2096 TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor);
2097 TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor);
2098 /* Copy constexpr from the inherited constructor even if the
2099 inheriting constructor doesn't satisfy the requirements. */
2100 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
2102 /* Add the "this" parameter. */
2103 this_parm = build_this_parm (fn, fn_type, TYPE_UNQUALIFIED);
2104 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
2105 DECL_ARGUMENTS (fn) = this_parm;
2107 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
2108 DECL_IN_AGGR_P (fn) = 1;
2109 DECL_ARTIFICIAL (fn) = 1;
2110 DECL_DEFAULTED_FN (fn) = 1;
2111 if (cxx_dialect >= cxx11)
2113 DECL_DELETED_FN (fn) = deleted_p;
2114 DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
2116 DECL_EXTERNAL (fn) = true;
2117 DECL_NOT_REALLY_EXTERN (fn) = 1;
2118 DECL_DECLARED_INLINE_P (fn) = 1;
2119 set_linkage_according_to_type (type, fn);
2120 if (TREE_PUBLIC (fn))
2121 DECL_COMDAT (fn) = 1;
2122 rest_of_decl_compilation (fn, namespace_bindings_p (), at_eof);
2123 gcc_assert (!TREE_USED (fn));
2125 /* Propagate constraints from the inherited constructor. */
2126 if (flag_concepts && inherited_ctor)
2127 if (tree orig_ci = get_constraints (inherited_ctor))
2129 tree new_ci = copy_node (orig_ci);
2130 set_constraints (fn, new_ci);
2133 /* Restore PROCESSING_TEMPLATE_DECL. */
2134 processing_template_decl = saved_processing_template_decl;
2136 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
2137 fn = add_inherited_template_parms (fn, inherited_ctor);
2139 /* Warn about calling a non-trivial move assignment in a virtual base. */
2140 if (kind == sfk_move_assignment && !deleted_p && !trivial_p
2141 && CLASSTYPE_VBASECLASSES (type))
2143 location_t loc = input_location;
2144 input_location = DECL_SOURCE_LOCATION (fn);
2145 synthesized_method_walk (type, kind, const_p,
2146 NULL, NULL, NULL, NULL, true,
2147 NULL_TREE, NULL_TREE);
2148 input_location = loc;
2151 return fn;
2154 /* Gives any errors about defaulted functions which need to be deferred
2155 until the containing class is complete. */
2157 void
2158 defaulted_late_check (tree fn)
2160 /* Complain about invalid signature for defaulted fn. */
2161 tree ctx = DECL_CONTEXT (fn);
2162 special_function_kind kind = special_function_p (fn);
2163 bool fn_const_p = (copy_fn_p (fn) == 2);
2164 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
2165 NULL, NULL);
2166 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
2168 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
2169 TREE_TYPE (TREE_TYPE (implicit_fn)))
2170 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2171 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn))))
2173 error ("defaulted declaration %q+D", fn);
2174 error_at (DECL_SOURCE_LOCATION (fn),
2175 "does not match expected signature %qD", implicit_fn);
2178 /* 8.4.2/2: An explicitly-defaulted function (...) may have an explicit
2179 exception-specification only if it is compatible (15.4) with the
2180 exception-specification on the implicit declaration. If a function
2181 is explicitly defaulted on its first declaration, (...) it is
2182 implicitly considered to have the same exception-specification as if
2183 it had been implicitly declared. */
2184 maybe_instantiate_noexcept (fn);
2185 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2186 if (!fn_spec)
2188 if (DECL_DEFAULTED_IN_CLASS_P (fn))
2189 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
2191 else if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
2192 /* Equivalent to the implicit spec. */;
2193 else if (DECL_DEFAULTED_IN_CLASS_P (fn)
2194 && !CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
2195 /* We can't compare an explicit exception-specification on a
2196 constructor defaulted in the class body to the implicit
2197 exception-specification until after we've parsed any NSDMI; see
2198 after_nsdmi_defaulted_late_checks. */;
2199 else
2201 tree eh_spec = get_defaulted_eh_spec (fn);
2202 if (!comp_except_specs (fn_spec, eh_spec, ce_normal))
2204 if (DECL_DEFAULTED_IN_CLASS_P (fn))
2205 DECL_DELETED_FN (fn) = true;
2206 else
2207 error ("function %q+D defaulted on its redeclaration "
2208 "with an exception-specification that differs from "
2209 "the implicit exception-specification %qX", fn, eh_spec);
2213 if (DECL_DEFAULTED_IN_CLASS_P (fn)
2214 && DECL_DECLARED_CONSTEXPR_P (implicit_fn))
2216 /* Hmm...should we do this for out-of-class too? Should it be OK to
2217 add constexpr later like inline, rather than requiring
2218 declarations to match? */
2219 DECL_DECLARED_CONSTEXPR_P (fn) = true;
2220 if (kind == sfk_constructor)
2221 TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
2224 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
2225 && DECL_DECLARED_CONSTEXPR_P (fn))
2227 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
2229 error ("explicitly defaulted function %q+D cannot be declared "
2230 "as constexpr because the implicit declaration is not "
2231 "constexpr:", fn);
2232 explain_implicit_non_constexpr (fn);
2234 DECL_DECLARED_CONSTEXPR_P (fn) = false;
2237 if (DECL_DELETED_FN (implicit_fn))
2238 DECL_DELETED_FN (fn) = 1;
2241 /* OK, we've parsed the NSDMI for class T, now we can check any explicit
2242 exception-specifications on functions defaulted in the class body. */
2244 void
2245 after_nsdmi_defaulted_late_checks (tree t)
2247 if (uses_template_parms (t))
2248 return;
2249 if (t == error_mark_node)
2250 return;
2251 for (tree fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
2252 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
2254 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2255 if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
2256 continue;
2258 tree eh_spec = get_defaulted_eh_spec (fn);
2259 if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)),
2260 eh_spec, ce_normal))
2261 DECL_DELETED_FN (fn) = true;
2265 /* Returns true iff FN can be explicitly defaulted, and gives any
2266 errors if defaulting FN is ill-formed. */
2268 bool
2269 defaultable_fn_check (tree fn)
2271 special_function_kind kind = sfk_none;
2273 if (template_parm_scope_p ())
2275 error ("a template cannot be defaulted");
2276 return false;
2279 if (DECL_CONSTRUCTOR_P (fn))
2281 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
2282 kind = sfk_constructor;
2283 else if (copy_fn_p (fn) > 0
2284 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
2285 == void_list_node))
2286 kind = sfk_copy_constructor;
2287 else if (move_fn_p (fn))
2288 kind = sfk_move_constructor;
2290 else if (DECL_DESTRUCTOR_P (fn))
2291 kind = sfk_destructor;
2292 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2293 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
2295 if (copy_fn_p (fn))
2296 kind = sfk_copy_assignment;
2297 else if (move_fn_p (fn))
2298 kind = sfk_move_assignment;
2301 if (kind == sfk_none)
2303 error ("%qD cannot be defaulted", fn);
2304 return false;
2306 else
2308 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
2309 t && t != void_list_node; t = TREE_CHAIN (t))
2310 if (TREE_PURPOSE (t))
2312 error ("defaulted function %q+D with default argument", fn);
2313 break;
2316 /* Avoid do_warn_unused_parameter warnings. */
2317 for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p))
2318 if (DECL_NAME (p))
2319 TREE_NO_WARNING (p) = 1;
2321 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn)))
2322 /* Defer checking. */;
2323 else if (!processing_template_decl)
2324 defaulted_late_check (fn);
2326 return true;
2330 /* Add an implicit declaration to TYPE for the kind of function
2331 indicated by SFK. Return the FUNCTION_DECL for the new implicit
2332 declaration. */
2334 tree
2335 lazily_declare_fn (special_function_kind sfk, tree type)
2337 tree fn;
2338 /* Whether or not the argument has a const reference type. */
2339 bool const_p = false;
2341 type = TYPE_MAIN_VARIANT (type);
2343 switch (sfk)
2345 case sfk_constructor:
2346 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
2347 break;
2348 case sfk_copy_constructor:
2349 const_p = TYPE_HAS_CONST_COPY_CTOR (type);
2350 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
2351 break;
2352 case sfk_move_constructor:
2353 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
2354 break;
2355 case sfk_copy_assignment:
2356 const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
2357 CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
2358 break;
2359 case sfk_move_assignment:
2360 CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
2361 break;
2362 case sfk_destructor:
2363 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
2364 break;
2365 default:
2366 gcc_unreachable ();
2369 /* Declare the function. */
2370 fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL);
2372 /* [class.copy]/8 If the class definition declares a move constructor or
2373 move assignment operator, the implicitly declared copy constructor is
2374 defined as deleted.... */
2375 if ((sfk == sfk_copy_assignment
2376 || sfk == sfk_copy_constructor)
2377 && (type_has_user_declared_move_constructor (type)
2378 || type_has_user_declared_move_assign (type)))
2379 DECL_DELETED_FN (fn) = true;
2381 /* A destructor may be virtual. */
2382 if (sfk == sfk_destructor
2383 || sfk == sfk_move_assignment
2384 || sfk == sfk_copy_assignment)
2385 check_for_override (fn, type);
2386 /* Add it to CLASSTYPE_METHOD_VEC. */
2387 bool added = add_method (type, fn, false);
2388 gcc_assert (added);
2389 /* Add it to TYPE_METHODS. */
2390 if (sfk == sfk_destructor
2391 && DECL_VIRTUAL_P (fn))
2392 /* The ABI requires that a virtual destructor go at the end of the
2393 vtable. */
2394 TYPE_METHODS (type) = chainon (TYPE_METHODS (type), fn);
2395 else
2397 DECL_CHAIN (fn) = TYPE_METHODS (type);
2398 TYPE_METHODS (type) = fn;
2400 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
2401 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
2402 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
2403 /* Create appropriate clones. */
2404 clone_function_decl (fn, /*update_methods=*/true);
2406 return fn;
2409 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
2410 as there are artificial parms in FN. */
2412 tree
2413 skip_artificial_parms_for (const_tree fn, tree list)
2415 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2416 list = TREE_CHAIN (list);
2417 else
2418 return list;
2420 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2421 list = TREE_CHAIN (list);
2422 if (DECL_HAS_VTT_PARM_P (fn))
2423 list = TREE_CHAIN (list);
2424 return list;
2427 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
2428 artificial parms in FN. */
2431 num_artificial_parms_for (const_tree fn)
2433 int count = 0;
2435 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2436 count++;
2437 else
2438 return 0;
2440 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2441 count++;
2442 if (DECL_HAS_VTT_PARM_P (fn))
2443 count++;
2444 return count;
2448 #include "gt-cp-method.h"