Daily bump.
[official-gcc.git] / gcc / cp / method.c
blob33e2f3cb52fb34e2b17720f3db4bcc80e478a6cd
1 /* Handle the hair of processing (but not expanding) inline functions.
2 Also manage function and variable name overloading.
3 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* Handle method declarations. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "hash-set.h"
29 #include "machmode.h"
30 #include "vec.h"
31 #include "double-int.h"
32 #include "input.h"
33 #include "alias.h"
34 #include "symtab.h"
35 #include "wide-int.h"
36 #include "inchash.h"
37 #include "tree.h"
38 #include "stringpool.h"
39 #include "varasm.h"
40 #include "cp-tree.h"
41 #include "flags.h"
42 #include "toplev.h"
43 #include "tm_p.h"
44 #include "target.h"
45 #include "common/common-target.h"
46 #include "diagnostic.h"
47 #include "hash-map.h"
48 #include "is-a.h"
49 #include "plugin-api.h"
50 #include "hard-reg-set.h"
51 #include "input.h"
52 #include "function.h"
53 #include "ipa-ref.h"
54 #include "cgraph.h"
56 /* Various flags to control the mangling process. */
58 enum mangling_flags
60 /* No flags. */
61 mf_none = 0,
62 /* The thing we are presently mangling is part of a template type,
63 rather than a fully instantiated type. Therefore, we may see
64 complex expressions where we would normally expect to see a
65 simple integer constant. */
66 mf_maybe_uninstantiated = 1,
67 /* When mangling a numeric value, use the form `_XX_' (instead of
68 just `XX') if the value has more than one digit. */
69 mf_use_underscores_around_value = 2
72 typedef enum mangling_flags mangling_flags;
74 static void do_build_copy_assign (tree);
75 static void do_build_copy_constructor (tree);
76 static tree make_alias_for_thunk (tree);
78 /* Called once to initialize method.c. */
80 void
81 init_method (void)
83 init_mangle ();
86 /* Return a this or result adjusting thunk to FUNCTION. THIS_ADJUSTING
87 indicates whether it is a this or result adjusting thunk.
88 FIXED_OFFSET and VIRTUAL_OFFSET indicate how to do the adjustment
89 (see thunk_adjust). VIRTUAL_OFFSET can be NULL, but FIXED_OFFSET
90 never is. VIRTUAL_OFFSET is the /index/ into the vtable for this
91 adjusting thunks, we scale it to a byte offset. For covariant
92 thunks VIRTUAL_OFFSET is the virtual binfo. You must post process
93 the returned thunk with finish_thunk. */
95 tree
96 make_thunk (tree function, bool this_adjusting,
97 tree fixed_offset, tree virtual_offset)
99 HOST_WIDE_INT d;
100 tree thunk;
102 gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
103 /* We can have this thunks to covariant thunks, but not vice versa. */
104 gcc_assert (!DECL_THIS_THUNK_P (function));
105 gcc_assert (!DECL_RESULT_THUNK_P (function) || this_adjusting);
107 /* Scale the VIRTUAL_OFFSET to be in terms of bytes. */
108 if (this_adjusting && virtual_offset)
109 virtual_offset
110 = size_binop (MULT_EXPR,
111 virtual_offset,
112 convert (ssizetype,
113 TYPE_SIZE_UNIT (vtable_entry_type)));
115 d = tree_to_shwi (fixed_offset);
117 /* See if we already have the thunk in question. For this_adjusting
118 thunks VIRTUAL_OFFSET will be an INTEGER_CST, for covariant thunks it
119 will be a BINFO. */
120 for (thunk = DECL_THUNKS (function); thunk; thunk = DECL_CHAIN (thunk))
121 if (DECL_THIS_THUNK_P (thunk) == this_adjusting
122 && THUNK_FIXED_OFFSET (thunk) == d
123 && !virtual_offset == !THUNK_VIRTUAL_OFFSET (thunk)
124 && (!virtual_offset
125 || (this_adjusting
126 ? tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk),
127 virtual_offset)
128 : THUNK_VIRTUAL_OFFSET (thunk) == virtual_offset)))
129 return thunk;
131 /* All thunks must be created before FUNCTION is actually emitted;
132 the ABI requires that all thunks be emitted together with the
133 function to which they transfer control. */
134 gcc_assert (!TREE_ASM_WRITTEN (function));
135 /* Likewise, we can only be adding thunks to a function declared in
136 the class currently being laid out. */
137 gcc_assert (TYPE_SIZE (DECL_CONTEXT (function))
138 && TYPE_BEING_DEFINED (DECL_CONTEXT (function)));
140 thunk = build_decl (DECL_SOURCE_LOCATION (function),
141 FUNCTION_DECL, NULL_TREE, TREE_TYPE (function));
142 DECL_LANG_SPECIFIC (thunk) = DECL_LANG_SPECIFIC (function);
143 cxx_dup_lang_specific_decl (thunk);
144 DECL_VIRTUAL_P (thunk) = true;
145 SET_DECL_THUNKS (thunk, NULL_TREE);
147 DECL_CONTEXT (thunk) = DECL_CONTEXT (function);
148 TREE_READONLY (thunk) = TREE_READONLY (function);
149 TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (function);
150 TREE_PUBLIC (thunk) = TREE_PUBLIC (function);
151 SET_DECL_THUNK_P (thunk, this_adjusting);
152 THUNK_TARGET (thunk) = function;
153 THUNK_FIXED_OFFSET (thunk) = d;
154 THUNK_VIRTUAL_OFFSET (thunk) = virtual_offset;
155 THUNK_ALIAS (thunk) = NULL_TREE;
157 DECL_INTERFACE_KNOWN (thunk) = 1;
158 DECL_NOT_REALLY_EXTERN (thunk) = 1;
159 DECL_COMDAT (thunk) = DECL_COMDAT (function);
160 DECL_SAVED_FUNCTION_DATA (thunk) = NULL;
161 /* The thunk itself is not a constructor or destructor, even if
162 the thing it is thunking to is. */
163 DECL_DESTRUCTOR_P (thunk) = 0;
164 DECL_CONSTRUCTOR_P (thunk) = 0;
165 DECL_EXTERNAL (thunk) = 1;
166 DECL_ARTIFICIAL (thunk) = 1;
167 /* The THUNK is not a pending inline, even if the FUNCTION is. */
168 DECL_PENDING_INLINE_P (thunk) = 0;
169 DECL_DECLARED_INLINE_P (thunk) = 0;
170 /* Nor is it a template instantiation. */
171 DECL_USE_TEMPLATE (thunk) = 0;
172 DECL_TEMPLATE_INFO (thunk) = NULL;
174 /* Add it to the list of thunks associated with FUNCTION. */
175 DECL_CHAIN (thunk) = DECL_THUNKS (function);
176 SET_DECL_THUNKS (function, thunk);
178 return thunk;
181 /* Finish THUNK, a thunk decl. */
183 void
184 finish_thunk (tree thunk)
186 tree function, name;
187 tree fixed_offset = ssize_int (THUNK_FIXED_OFFSET (thunk));
188 tree virtual_offset = THUNK_VIRTUAL_OFFSET (thunk);
190 gcc_assert (!DECL_NAME (thunk) && DECL_THUNK_P (thunk));
191 if (virtual_offset && DECL_RESULT_THUNK_P (thunk))
192 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
193 function = THUNK_TARGET (thunk);
194 name = mangle_thunk (function, DECL_THIS_THUNK_P (thunk),
195 fixed_offset, virtual_offset);
197 /* We can end up with declarations of (logically) different
198 covariant thunks, that do identical adjustments. The two thunks
199 will be adjusting between within different hierarchies, which
200 happen to have the same layout. We must nullify one of them to
201 refer to the other. */
202 if (DECL_RESULT_THUNK_P (thunk))
204 tree cov_probe;
206 for (cov_probe = DECL_THUNKS (function);
207 cov_probe; cov_probe = DECL_CHAIN (cov_probe))
208 if (DECL_NAME (cov_probe) == name)
210 gcc_assert (!DECL_THUNKS (thunk));
211 THUNK_ALIAS (thunk) = (THUNK_ALIAS (cov_probe)
212 ? THUNK_ALIAS (cov_probe) : cov_probe);
213 break;
217 DECL_NAME (thunk) = name;
218 SET_DECL_ASSEMBLER_NAME (thunk, name);
221 static GTY (()) int thunk_labelno;
223 /* Create a static alias to target. */
225 tree
226 make_alias_for (tree target, tree newid)
228 tree alias = build_decl (DECL_SOURCE_LOCATION (target),
229 TREE_CODE (target), newid, TREE_TYPE (target));
230 DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (target);
231 cxx_dup_lang_specific_decl (alias);
232 DECL_CONTEXT (alias) = NULL;
233 TREE_READONLY (alias) = TREE_READONLY (target);
234 TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (target);
235 TREE_PUBLIC (alias) = 0;
236 DECL_INTERFACE_KNOWN (alias) = 1;
237 if (DECL_LANG_SPECIFIC (alias))
239 DECL_NOT_REALLY_EXTERN (alias) = 1;
240 DECL_USE_TEMPLATE (alias) = 0;
241 DECL_TEMPLATE_INFO (alias) = NULL;
243 DECL_EXTERNAL (alias) = 0;
244 DECL_ARTIFICIAL (alias) = 1;
245 DECL_TEMPLATE_INSTANTIATED (alias) = 0;
246 if (TREE_CODE (alias) == FUNCTION_DECL)
248 DECL_SAVED_FUNCTION_DATA (alias) = NULL;
249 DECL_DESTRUCTOR_P (alias) = 0;
250 DECL_CONSTRUCTOR_P (alias) = 0;
251 DECL_PENDING_INLINE_P (alias) = 0;
252 DECL_DECLARED_INLINE_P (alias) = 0;
253 DECL_INITIAL (alias) = error_mark_node;
254 DECL_ARGUMENTS (alias) = copy_list (DECL_ARGUMENTS (target));
256 else
257 TREE_STATIC (alias) = 1;
258 TREE_ADDRESSABLE (alias) = 1;
259 TREE_USED (alias) = 1;
260 SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
261 return alias;
264 static tree
265 make_alias_for_thunk (tree function)
267 tree alias;
268 char buf[256];
270 targetm.asm_out.generate_internal_label (buf, "LTHUNK", thunk_labelno);
271 thunk_labelno++;
273 alias = make_alias_for (function, get_identifier (buf));
275 if (!flag_syntax_only)
277 struct cgraph_node *funcn, *aliasn;
278 funcn = cgraph_node::get (function);
279 gcc_checking_assert (funcn);
280 aliasn = cgraph_node::create_same_body_alias (alias, function);
281 DECL_ASSEMBLER_NAME (function);
282 gcc_assert (aliasn != NULL);
285 return alias;
288 /* Emit the definition of a C++ multiple inheritance or covariant
289 return vtable thunk. If EMIT_P is nonzero, the thunk is emitted
290 immediately. */
292 void
293 use_thunk (tree thunk_fndecl, bool emit_p)
295 tree a, t, function, alias;
296 tree virtual_offset;
297 HOST_WIDE_INT fixed_offset, virtual_value;
298 bool this_adjusting = DECL_THIS_THUNK_P (thunk_fndecl);
299 struct cgraph_node *funcn, *thunk_node;
301 /* We should have called finish_thunk to give it a name. */
302 gcc_assert (DECL_NAME (thunk_fndecl));
304 /* We should never be using an alias, always refer to the
305 aliased thunk. */
306 gcc_assert (!THUNK_ALIAS (thunk_fndecl));
308 if (TREE_ASM_WRITTEN (thunk_fndecl))
309 return;
311 function = THUNK_TARGET (thunk_fndecl);
312 if (DECL_RESULT (thunk_fndecl))
313 /* We already turned this thunk into an ordinary function.
314 There's no need to process this thunk again. */
315 return;
317 if (DECL_THUNK_P (function))
318 /* The target is itself a thunk, process it now. */
319 use_thunk (function, emit_p);
321 /* Thunks are always addressable; they only appear in vtables. */
322 TREE_ADDRESSABLE (thunk_fndecl) = 1;
324 /* Figure out what function is being thunked to. It's referenced in
325 this translation unit. */
326 TREE_ADDRESSABLE (function) = 1;
327 mark_used (function);
328 if (!emit_p)
329 return;
331 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function))
332 alias = make_alias_for_thunk (function);
333 else
334 alias = function;
336 fixed_offset = THUNK_FIXED_OFFSET (thunk_fndecl);
337 virtual_offset = THUNK_VIRTUAL_OFFSET (thunk_fndecl);
339 if (virtual_offset)
341 if (!this_adjusting)
342 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
343 virtual_value = tree_to_shwi (virtual_offset);
344 gcc_assert (virtual_value);
346 else
347 virtual_value = 0;
349 /* And, if we need to emit the thunk, it's used. */
350 mark_used (thunk_fndecl);
351 /* This thunk is actually defined. */
352 DECL_EXTERNAL (thunk_fndecl) = 0;
353 /* The linkage of the function may have changed. FIXME in linkage
354 rewrite. */
355 gcc_assert (DECL_INTERFACE_KNOWN (function));
356 TREE_PUBLIC (thunk_fndecl) = TREE_PUBLIC (function);
357 DECL_VISIBILITY (thunk_fndecl) = DECL_VISIBILITY (function);
358 DECL_VISIBILITY_SPECIFIED (thunk_fndecl)
359 = DECL_VISIBILITY_SPECIFIED (function);
360 DECL_COMDAT (thunk_fndecl) = DECL_COMDAT (function);
361 DECL_WEAK (thunk_fndecl) = DECL_WEAK (function);
363 if (flag_syntax_only)
365 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
366 return;
369 push_to_top_level ();
371 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function)
372 && targetm_common.have_named_sections)
374 tree fn = function;
375 struct symtab_node *symbol;
377 if ((symbol = symtab_node::get (function))
378 && symbol->alias)
380 if (symbol->analyzed)
381 fn = symtab_node::get (function)->ultimate_alias_target ()->decl;
382 else
383 fn = symtab_node::get (function)->alias_target;
385 resolve_unique_section (fn, 0, flag_function_sections);
387 if (DECL_SECTION_NAME (fn) != NULL && DECL_ONE_ONLY (fn))
389 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
391 /* Output the thunk into the same section as function. */
392 set_decl_section_name (thunk_fndecl, DECL_SECTION_NAME (fn));
393 symtab_node::get (thunk_fndecl)->implicit_section
394 = symtab_node::get (fn)->implicit_section;
398 /* Set up cloned argument trees for the thunk. */
399 t = NULL_TREE;
400 for (a = DECL_ARGUMENTS (function); a; a = DECL_CHAIN (a))
402 tree x = copy_node (a);
403 DECL_CHAIN (x) = t;
404 DECL_CONTEXT (x) = thunk_fndecl;
405 SET_DECL_RTL (x, NULL);
406 DECL_HAS_VALUE_EXPR_P (x) = 0;
407 TREE_ADDRESSABLE (x) = 0;
408 t = x;
410 a = nreverse (t);
411 DECL_ARGUMENTS (thunk_fndecl) = a;
412 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
413 funcn = cgraph_node::get (function);
414 gcc_checking_assert (funcn);
415 thunk_node = funcn->create_thunk (thunk_fndecl, function,
416 this_adjusting, fixed_offset, virtual_value,
417 virtual_offset, alias);
418 if (DECL_ONE_ONLY (function))
419 thunk_node->add_to_same_comdat_group (funcn);
421 pop_from_top_level ();
424 /* Code for synthesizing methods which have default semantics defined. */
426 /* True iff CTYPE has a trivial SFK. */
428 static bool
429 type_has_trivial_fn (tree ctype, special_function_kind sfk)
431 switch (sfk)
433 case sfk_constructor:
434 return !TYPE_HAS_COMPLEX_DFLT (ctype);
435 case sfk_copy_constructor:
436 return !TYPE_HAS_COMPLEX_COPY_CTOR (ctype);
437 case sfk_move_constructor:
438 return !TYPE_HAS_COMPLEX_MOVE_CTOR (ctype);
439 case sfk_copy_assignment:
440 return !TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype);
441 case sfk_move_assignment:
442 return !TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype);
443 case sfk_destructor:
444 return !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype);
445 case sfk_inheriting_constructor:
446 return false;
447 default:
448 gcc_unreachable ();
452 /* Note that CTYPE has a non-trivial SFK even though we previously thought
453 it was trivial. */
455 static void
456 type_set_nontrivial_flag (tree ctype, special_function_kind sfk)
458 switch (sfk)
460 case sfk_constructor:
461 TYPE_HAS_COMPLEX_DFLT (ctype) = true;
462 return;
463 case sfk_copy_constructor:
464 TYPE_HAS_COMPLEX_COPY_CTOR (ctype) = true;
465 return;
466 case sfk_move_constructor:
467 TYPE_HAS_COMPLEX_MOVE_CTOR (ctype) = true;
468 return;
469 case sfk_copy_assignment:
470 TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype) = true;
471 return;
472 case sfk_move_assignment:
473 TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype) = true;
474 return;
475 case sfk_destructor:
476 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
477 return;
478 case sfk_inheriting_constructor:
479 default:
480 gcc_unreachable ();
484 /* True iff FN is a trivial defaulted member function ([cd]tor, op=). */
486 bool
487 trivial_fn_p (tree fn)
489 if (!DECL_DEFAULTED_FN (fn))
490 return false;
492 /* If fn is a clone, get the primary variant. */
493 if (tree prim = DECL_CLONED_FUNCTION (fn))
494 fn = prim;
495 return type_has_trivial_fn (DECL_CONTEXT (fn), special_function_p (fn));
498 /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO
499 given the parameter or parameters PARM, possibly inherited constructor
500 base INH, or move flag MOVE_P. */
502 static tree
503 add_one_base_init (tree binfo, tree parm, bool move_p, tree inh,
504 tree member_init_list)
506 tree init;
507 if (inh)
509 /* An inheriting constructor only has a mem-initializer for
510 the base it inherits from. */
511 if (BINFO_TYPE (binfo) != inh)
512 return member_init_list;
514 tree *p = &init;
515 init = NULL_TREE;
516 for (; parm; parm = DECL_CHAIN (parm))
518 tree exp = convert_from_reference (parm);
519 if (TREE_CODE (TREE_TYPE (parm)) != REFERENCE_TYPE
520 || TYPE_REF_IS_RVALUE (TREE_TYPE (parm)))
521 exp = move (exp);
522 *p = build_tree_list (NULL_TREE, exp);
523 p = &TREE_CHAIN (*p);
526 else
528 init = build_base_path (PLUS_EXPR, parm, binfo, 1,
529 tf_warning_or_error);
530 if (move_p)
531 init = move (init);
532 init = build_tree_list (NULL_TREE, init);
534 return tree_cons (binfo, init, member_init_list);
537 /* Generate code for default X(X&) or X(X&&) constructor or an inheriting
538 constructor. */
540 static void
541 do_build_copy_constructor (tree fndecl)
543 tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
544 bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
545 bool trivial = trivial_fn_p (fndecl);
546 tree inh = DECL_INHERITED_CTOR_BASE (fndecl);
548 if (!inh)
549 parm = convert_from_reference (parm);
551 if (trivial
552 && is_empty_class (current_class_type))
553 /* Don't copy the padding byte; it might not have been allocated
554 if *this is a base subobject. */;
555 else if (trivial)
557 tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm);
558 finish_expr_stmt (t);
560 else
562 tree fields = TYPE_FIELDS (current_class_type);
563 tree member_init_list = NULL_TREE;
564 int cvquals = cp_type_quals (TREE_TYPE (parm));
565 int i;
566 tree binfo, base_binfo;
567 tree init;
568 vec<tree, va_gc> *vbases;
570 /* Initialize all the base-classes with the parameter converted
571 to their type so that we get their copy constructor and not
572 another constructor that takes current_class_type. We must
573 deal with the binfo's directly as a direct base might be
574 inaccessible due to ambiguity. */
575 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
576 vec_safe_iterate (vbases, i, &binfo); i++)
578 member_init_list = add_one_base_init (binfo, parm, move_p, inh,
579 member_init_list);
582 for (binfo = TYPE_BINFO (current_class_type), i = 0;
583 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
585 if (BINFO_VIRTUAL_P (base_binfo))
586 continue;
587 member_init_list = add_one_base_init (base_binfo, parm, move_p,
588 inh, member_init_list);
591 for (; fields; fields = DECL_CHAIN (fields))
593 tree field = fields;
594 tree expr_type;
596 if (TREE_CODE (field) != FIELD_DECL)
597 continue;
598 if (inh)
599 continue;
601 expr_type = TREE_TYPE (field);
602 if (DECL_NAME (field))
604 if (VFIELD_NAME_P (DECL_NAME (field)))
605 continue;
607 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
608 /* Just use the field; anonymous types can't have
609 nontrivial copy ctors or assignment ops or this
610 function would be deleted. */;
611 else
612 continue;
614 /* Compute the type of "init->field". If the copy-constructor
615 parameter is, for example, "const S&", and the type of
616 the field is "T", then the type will usually be "const
617 T". (There are no cv-qualified variants of reference
618 types.) */
619 if (TREE_CODE (expr_type) != REFERENCE_TYPE)
621 int quals = cvquals;
623 if (DECL_MUTABLE_P (field))
624 quals &= ~TYPE_QUAL_CONST;
625 quals |= cp_type_quals (expr_type);
626 expr_type = cp_build_qualified_type (expr_type, quals);
629 init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
630 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
631 /* 'move' breaks bit-fields, and has no effect for scalars. */
632 && !scalarish_type_p (expr_type))
633 init = move (init);
634 init = build_tree_list (NULL_TREE, init);
636 member_init_list = tree_cons (field, init, member_init_list);
638 finish_mem_initializers (member_init_list);
642 static void
643 do_build_copy_assign (tree fndecl)
645 tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl));
646 tree compound_stmt;
647 bool move_p = move_fn_p (fndecl);
648 bool trivial = trivial_fn_p (fndecl);
649 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
651 compound_stmt = begin_compound_stmt (0);
652 parm = convert_from_reference (parm);
654 if (trivial
655 && is_empty_class (current_class_type))
656 /* Don't copy the padding byte; it might not have been allocated
657 if *this is a base subobject. */;
658 else if (trivial)
660 tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
661 finish_expr_stmt (t);
663 else
665 tree fields;
666 int cvquals = cp_type_quals (TREE_TYPE (parm));
667 int i;
668 tree binfo, base_binfo;
670 /* Assign to each of the direct base classes. */
671 for (binfo = TYPE_BINFO (current_class_type), i = 0;
672 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
674 tree converted_parm;
675 vec<tree, va_gc> *parmvec;
677 /* We must convert PARM directly to the base class
678 explicitly since the base class may be ambiguous. */
679 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
680 tf_warning_or_error);
681 if (move_p)
682 converted_parm = move (converted_parm);
683 /* Call the base class assignment operator. */
684 parmvec = make_tree_vector_single (converted_parm);
685 finish_expr_stmt
686 (build_special_member_call (current_class_ref,
687 ansi_assopname (NOP_EXPR),
688 &parmvec,
689 base_binfo,
690 flags,
691 tf_warning_or_error));
692 release_tree_vector (parmvec);
695 /* Assign to each of the non-static data members. */
696 for (fields = TYPE_FIELDS (current_class_type);
697 fields;
698 fields = DECL_CHAIN (fields))
700 tree comp = current_class_ref;
701 tree init = parm;
702 tree field = fields;
703 tree expr_type;
704 int quals;
706 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
707 continue;
709 expr_type = TREE_TYPE (field);
711 if (CP_TYPE_CONST_P (expr_type))
713 error ("non-static const member %q#D, can%'t use default "
714 "assignment operator", field);
715 continue;
717 else if (TREE_CODE (expr_type) == REFERENCE_TYPE)
719 error ("non-static reference member %q#D, can%'t use "
720 "default assignment operator", field);
721 continue;
724 if (DECL_NAME (field))
726 if (VFIELD_NAME_P (DECL_NAME (field)))
727 continue;
729 else if (ANON_AGGR_TYPE_P (expr_type)
730 && TYPE_FIELDS (expr_type) != NULL_TREE)
731 /* Just use the field; anonymous types can't have
732 nontrivial copy ctors or assignment ops or this
733 function would be deleted. */;
734 else
735 continue;
737 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
739 /* Compute the type of init->field */
740 quals = cvquals;
741 if (DECL_MUTABLE_P (field))
742 quals &= ~TYPE_QUAL_CONST;
743 expr_type = cp_build_qualified_type (expr_type, quals);
745 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
746 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
747 /* 'move' breaks bit-fields, and has no effect for scalars. */
748 && !scalarish_type_p (expr_type))
749 init = move (init);
751 if (DECL_NAME (field))
752 init = cp_build_modify_expr (comp, NOP_EXPR, init,
753 tf_warning_or_error);
754 else
755 init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
756 finish_expr_stmt (init);
759 finish_return_stmt (current_class_ref);
760 finish_compound_stmt (compound_stmt);
763 /* Synthesize FNDECL, a non-static member function. */
765 void
766 synthesize_method (tree fndecl)
768 bool nested = (current_function_decl != NULL_TREE);
769 tree context = decl_function_context (fndecl);
770 bool need_body = true;
771 tree stmt;
772 location_t save_input_location = input_location;
773 int error_count = errorcount;
774 int warning_count = warningcount + werrorcount;
776 /* Reset the source location, we might have been previously
777 deferred, and thus have saved where we were first needed. */
778 DECL_SOURCE_LOCATION (fndecl)
779 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
781 /* If we've been asked to synthesize a clone, just synthesize the
782 cloned function instead. Doing so will automatically fill in the
783 body for the clone. */
784 if (DECL_CLONED_FUNCTION_P (fndecl))
785 fndecl = DECL_CLONED_FUNCTION (fndecl);
787 /* We may be in the middle of deferred access check. Disable
788 it now. */
789 push_deferring_access_checks (dk_no_deferred);
791 if (! context)
792 push_to_top_level ();
793 else if (nested)
794 push_function_context ();
796 input_location = DECL_SOURCE_LOCATION (fndecl);
798 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
799 stmt = begin_function_body ();
801 if (DECL_OVERLOADED_OPERATOR_P (fndecl) == NOP_EXPR)
803 do_build_copy_assign (fndecl);
804 need_body = false;
806 else if (DECL_CONSTRUCTOR_P (fndecl))
808 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
809 if (arg_chain != void_list_node)
810 do_build_copy_constructor (fndecl);
811 else
812 finish_mem_initializers (NULL_TREE);
815 /* If we haven't yet generated the body of the function, just
816 generate an empty compound statement. */
817 if (need_body)
819 tree compound_stmt;
820 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
821 finish_compound_stmt (compound_stmt);
824 finish_function_body (stmt);
825 expand_or_defer_fn (finish_function (0));
827 input_location = save_input_location;
829 if (! context)
830 pop_from_top_level ();
831 else if (nested)
832 pop_function_context ();
834 pop_deferring_access_checks ();
836 if (error_count != errorcount || warning_count != warningcount + werrorcount)
837 inform (input_location, "synthesized method %qD first required here ",
838 fndecl);
841 /* Build a reference to type TYPE with cv-quals QUALS, which is an
842 rvalue if RVALUE is true. */
844 static tree
845 build_stub_type (tree type, int quals, bool rvalue)
847 tree argtype = cp_build_qualified_type (type, quals);
848 return cp_build_reference_type (argtype, rvalue);
851 /* Build a dummy glvalue from dereferencing a dummy reference of type
852 REFTYPE. */
854 static tree
855 build_stub_object (tree reftype)
857 if (TREE_CODE (reftype) != REFERENCE_TYPE)
858 reftype = cp_build_reference_type (reftype, /*rval*/true);
859 tree stub = build1 (CONVERT_EXPR, reftype, integer_one_node);
860 return convert_from_reference (stub);
863 /* Determine which function will be called when looking up NAME in TYPE,
864 called with a single ARGTYPE argument, or no argument if ARGTYPE is
865 null. FLAGS and COMPLAIN are as for build_new_method_call.
867 Returns a FUNCTION_DECL if all is well.
868 Returns NULL_TREE if overload resolution failed.
869 Returns error_mark_node if the chosen function cannot be called. */
871 static tree
872 locate_fn_flags (tree type, tree name, tree argtype, int flags,
873 tsubst_flags_t complain)
875 tree ob, fn, fns, binfo, rval;
876 vec<tree, va_gc> *args;
878 if (TYPE_P (type))
879 binfo = TYPE_BINFO (type);
880 else
882 binfo = type;
883 type = BINFO_TYPE (binfo);
886 ob = build_stub_object (cp_build_reference_type (type, false));
887 args = make_tree_vector ();
888 if (argtype)
890 if (TREE_CODE (argtype) == TREE_LIST)
892 for (tree elt = argtype; elt != void_list_node;
893 elt = TREE_CHAIN (elt))
895 tree type = TREE_VALUE (elt);
896 tree arg = build_stub_object (type);
897 vec_safe_push (args, arg);
900 else
902 tree arg = build_stub_object (argtype);
903 args->quick_push (arg);
907 fns = lookup_fnfields (binfo, name, 0);
908 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
910 release_tree_vector (args);
911 if (fn && rval == error_mark_node)
912 return rval;
913 else
914 return fn;
917 /* Locate the dtor of TYPE. */
919 tree
920 get_dtor (tree type, tsubst_flags_t complain)
922 tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
923 LOOKUP_NORMAL, complain);
924 if (fn == error_mark_node)
925 return NULL_TREE;
926 return fn;
929 /* Locate the default ctor of TYPE. */
931 tree
932 locate_ctor (tree type)
934 tree fn;
936 push_deferring_access_checks (dk_no_check);
937 fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
938 LOOKUP_SPECULATIVE, tf_none);
939 pop_deferring_access_checks ();
940 if (fn == error_mark_node)
941 return NULL_TREE;
942 return fn;
945 /* Likewise, but give any appropriate errors. */
947 tree
948 get_default_ctor (tree type)
950 tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
951 LOOKUP_NORMAL, tf_warning_or_error);
952 if (fn == error_mark_node)
953 return NULL_TREE;
954 return fn;
957 /* Locate the copy ctor of TYPE. */
959 tree
960 get_copy_ctor (tree type, tsubst_flags_t complain)
962 int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
963 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
964 tree argtype = build_stub_type (type, quals, false);
965 tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
966 LOOKUP_NORMAL, complain);
967 if (fn == error_mark_node)
968 return NULL_TREE;
969 return fn;
972 /* Locate the copy assignment operator of TYPE. */
974 tree
975 get_copy_assign (tree type)
977 int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
978 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
979 tree argtype = build_stub_type (type, quals, false);
980 tree fn = locate_fn_flags (type, ansi_assopname (NOP_EXPR), argtype,
981 LOOKUP_NORMAL, tf_warning_or_error);
982 if (fn == error_mark_node)
983 return NULL_TREE;
984 return fn;
987 /* Locate the inherited constructor of constructor CTOR. */
989 tree
990 get_inherited_ctor (tree ctor)
992 gcc_assert (DECL_INHERITED_CTOR_BASE (ctor));
994 push_deferring_access_checks (dk_no_check);
995 tree fn = locate_fn_flags (DECL_INHERITED_CTOR_BASE (ctor),
996 complete_ctor_identifier,
997 FUNCTION_FIRST_USER_PARMTYPE (ctor),
998 LOOKUP_NORMAL|LOOKUP_SPECULATIVE,
999 tf_none);
1000 pop_deferring_access_checks ();
1001 if (fn == error_mark_node)
1002 return NULL_TREE;
1003 return fn;
1006 /* walk_tree helper function for is_trivially_xible. If *TP is a call,
1007 return it if it calls something other than a trivial special member
1008 function. */
1010 static tree
1011 check_nontriv (tree *tp, int *, void *)
1013 tree fn;
1014 if (TREE_CODE (*tp) == CALL_EXPR)
1015 fn = CALL_EXPR_FN (*tp);
1016 else if (TREE_CODE (*tp) == AGGR_INIT_EXPR)
1017 fn = AGGR_INIT_EXPR_FN (*tp);
1018 else
1019 return NULL_TREE;
1021 if (TREE_CODE (fn) == ADDR_EXPR)
1022 fn = TREE_OPERAND (fn, 0);
1024 if (TREE_CODE (fn) != FUNCTION_DECL
1025 || !trivial_fn_p (fn))
1026 return fn;
1027 return NULL_TREE;
1030 /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */
1032 static tree
1033 assignable_expr (tree to, tree from)
1035 ++cp_unevaluated_operand;
1036 to = build_stub_object (to);
1037 from = build_stub_object (from);
1038 tree r = cp_build_modify_expr (to, NOP_EXPR, from, tf_none);
1039 --cp_unevaluated_operand;
1040 return r;
1043 /* The predicate condition for a template specialization
1044 is_constructible<T, Args...> shall be satisfied if and only if the
1045 following variable definition would be well-formed for some invented
1046 variable t: T t(create<Args>()...);
1048 Return something equivalent in well-formedness and triviality. */
1050 static tree
1051 constructible_expr (tree to, tree from)
1053 tree expr;
1054 if (CLASS_TYPE_P (to))
1056 tree ctype = to;
1057 vec<tree, va_gc> *args = NULL;
1058 if (TREE_CODE (to) != REFERENCE_TYPE)
1059 to = cp_build_reference_type (to, /*rval*/false);
1060 tree ob = build_stub_object (to);
1061 for (; from; from = TREE_CHAIN (from))
1062 vec_safe_push (args, build_stub_object (TREE_VALUE (from)));
1063 expr = build_special_member_call (ob, complete_ctor_identifier, &args,
1064 ctype, LOOKUP_NORMAL, tf_none);
1065 if (expr == error_mark_node)
1066 return error_mark_node;
1067 /* The current state of the standard vis-a-vis LWG 2116 is that
1068 is_*constructible involves destruction as well. */
1069 if (type_build_dtor_call (ctype))
1071 tree dtor = build_special_member_call (ob, complete_dtor_identifier,
1072 NULL, ctype, LOOKUP_NORMAL,
1073 tf_none);
1074 if (dtor == error_mark_node)
1075 return error_mark_node;
1076 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
1077 expr = build2 (COMPOUND_EXPR, void_type_node, expr, dtor);
1080 else
1082 if (from == NULL_TREE)
1083 return build_value_init (to, tf_none);
1084 else if (TREE_CHAIN (from))
1085 return error_mark_node; // too many initializers
1086 from = build_stub_object (TREE_VALUE (from));
1087 expr = perform_direct_initialization_if_possible (to, from,
1088 /*cast*/false,
1089 tf_none);
1091 return expr;
1094 /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or
1095 constructible (otherwise) from FROM, which is a single type for
1096 assignment or a list of types for construction. */
1098 bool
1099 is_trivially_xible (enum tree_code code, tree to, tree from)
1101 tree expr;
1102 if (code == MODIFY_EXPR)
1103 expr = assignable_expr (to, from);
1104 else if (from && TREE_CHAIN (from))
1105 return false; // only 0- and 1-argument ctors can be trivial
1106 else
1107 expr = constructible_expr (to, from);
1109 if (expr == error_mark_node)
1110 return false;
1111 tree nt = cp_walk_tree_without_duplicates (&expr, check_nontriv, NULL);
1112 return !nt;
1115 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
1116 DELETED_P or give an error message MSG with argument ARG. */
1118 static void
1119 process_subob_fn (tree fn, tree *spec_p, bool *trivial_p,
1120 bool *deleted_p, bool *constexpr_p,
1121 bool diag, tree arg)
1123 if (!fn || fn == error_mark_node)
1124 goto bad;
1126 if (spec_p)
1128 maybe_instantiate_noexcept (fn);
1129 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
1130 *spec_p = merge_exception_specifiers (*spec_p, raises);
1133 if (!trivial_fn_p (fn))
1135 if (trivial_p)
1136 *trivial_p = false;
1137 if (TREE_CODE (arg) == FIELD_DECL
1138 && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
1140 if (deleted_p)
1141 *deleted_p = true;
1142 if (diag)
1143 error ("union member %q+D with non-trivial %qD", arg, fn);
1147 if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
1149 *constexpr_p = false;
1150 if (diag)
1152 inform (0, "defaulted constructor calls non-constexpr "
1153 "%q+D", fn);
1154 explain_invalid_constexpr_fn (fn);
1158 return;
1160 bad:
1161 if (deleted_p)
1162 *deleted_p = true;
1165 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
1166 aggregates. */
1168 static void
1169 walk_field_subobs (tree fields, tree fnname, special_function_kind sfk,
1170 int quals, bool copy_arg_p, bool move_p,
1171 bool assign_p, tree *spec_p, bool *trivial_p,
1172 bool *deleted_p, bool *constexpr_p,
1173 bool diag, int flags, tsubst_flags_t complain)
1175 tree field;
1176 for (field = fields; field; field = DECL_CHAIN (field))
1178 tree mem_type, argtype, rval;
1180 if (TREE_CODE (field) != FIELD_DECL
1181 || DECL_ARTIFICIAL (field))
1182 continue;
1184 mem_type = strip_array_types (TREE_TYPE (field));
1185 if (assign_p)
1187 bool bad = true;
1188 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
1190 if (diag)
1191 error ("non-static const member %q#D, can%'t use default "
1192 "assignment operator", field);
1194 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1196 if (diag)
1197 error ("non-static reference member %q#D, can%'t use "
1198 "default assignment operator", field);
1200 else
1201 bad = false;
1203 if (bad && deleted_p)
1204 *deleted_p = true;
1206 else if (sfk == sfk_constructor)
1208 bool bad;
1210 if (DECL_INITIAL (field))
1212 if (diag && DECL_INITIAL (field) == error_mark_node)
1213 inform (0, "initializer for %q+#D is invalid", field);
1214 if (trivial_p)
1215 *trivial_p = false;
1216 /* Core 1351: If the field has an NSDMI that could throw, the
1217 default constructor is noexcept(false). */
1218 if (spec_p)
1220 tree nsdmi = get_nsdmi (field, /*ctor*/false);
1221 if (!expr_noexcept_p (nsdmi, complain))
1222 *spec_p = noexcept_false_spec;
1224 /* Don't do the normal processing. */
1225 continue;
1228 bad = false;
1229 if (CP_TYPE_CONST_P (mem_type)
1230 && default_init_uninitialized_part (mem_type))
1232 if (diag)
1234 error ("uninitialized const member in %q#T",
1235 current_class_type);
1236 inform (DECL_SOURCE_LOCATION (field),
1237 "%q#D should be initialized", field);
1239 bad = true;
1241 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1243 if (diag)
1245 error ("uninitialized reference member in %q#T",
1246 current_class_type);
1247 inform (DECL_SOURCE_LOCATION (field),
1248 "%q#D should be initialized", field);
1250 bad = true;
1253 if (bad && deleted_p)
1254 *deleted_p = true;
1256 /* For an implicitly-defined default constructor to be constexpr,
1257 every member must have a user-provided default constructor or
1258 an explicit initializer. */
1259 if (constexpr_p && !CLASS_TYPE_P (mem_type)
1260 && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE)
1262 *constexpr_p = false;
1263 if (diag)
1264 inform (0, "defaulted default constructor does not "
1265 "initialize %q+#D", field);
1268 else if (sfk == sfk_copy_constructor)
1270 /* 12.8p11b5 */
1271 if (TREE_CODE (mem_type) == REFERENCE_TYPE
1272 && TYPE_REF_IS_RVALUE (mem_type))
1274 if (diag)
1275 error ("copying non-static data member %q#D of rvalue "
1276 "reference type", field);
1277 if (deleted_p)
1278 *deleted_p = true;
1282 if (!CLASS_TYPE_P (mem_type))
1283 continue;
1285 if (ANON_AGGR_TYPE_P (mem_type))
1287 walk_field_subobs (TYPE_FIELDS (mem_type), fnname, sfk, quals,
1288 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1289 deleted_p, constexpr_p,
1290 diag, flags, complain);
1291 continue;
1294 if (copy_arg_p)
1296 int mem_quals = cp_type_quals (mem_type) | quals;
1297 if (DECL_MUTABLE_P (field))
1298 mem_quals &= ~TYPE_QUAL_CONST;
1299 argtype = build_stub_type (mem_type, mem_quals, move_p);
1301 else
1302 argtype = NULL_TREE;
1304 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
1306 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1307 constexpr_p, diag, field);
1311 /* The caller wants to generate an implicit declaration of SFK for CTYPE
1312 which is const if relevant and CONST_P is set. If spec_p, trivial_p and
1313 deleted_p are non-null, set their referent appropriately. If diag is
1314 true, we're either being called from maybe_explain_implicit_delete to
1315 give errors, or if constexpr_p is non-null, from
1316 explain_invalid_constexpr_fn. */
1318 static void
1319 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
1320 tree *spec_p, bool *trivial_p, bool *deleted_p,
1321 bool *constexpr_p, bool diag,
1322 tree inherited_base, tree inherited_parms)
1324 tree binfo, base_binfo, scope, fnname, rval, argtype;
1325 bool move_p, copy_arg_p, assign_p, expected_trivial, check_vdtor;
1326 vec<tree, va_gc> *vbases;
1327 int i, quals, flags;
1328 tsubst_flags_t complain;
1329 bool ctor_p;
1331 if (spec_p)
1332 *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec);
1334 if (deleted_p)
1336 /* "The closure type associated with a lambda-expression has a deleted
1337 default constructor and a deleted copy assignment operator."
1338 This is diagnosed in maybe_explain_implicit_delete. */
1339 if (LAMBDA_TYPE_P (ctype)
1340 && (sfk == sfk_constructor
1341 || sfk == sfk_copy_assignment))
1343 *deleted_p = true;
1344 return;
1347 *deleted_p = false;
1350 ctor_p = false;
1351 assign_p = false;
1352 check_vdtor = false;
1353 switch (sfk)
1355 case sfk_move_assignment:
1356 case sfk_copy_assignment:
1357 assign_p = true;
1358 fnname = ansi_assopname (NOP_EXPR);
1359 break;
1361 case sfk_destructor:
1362 check_vdtor = true;
1363 /* The synthesized method will call base dtors, but check complete
1364 here to avoid having to deal with VTT. */
1365 fnname = complete_dtor_identifier;
1366 break;
1368 case sfk_constructor:
1369 case sfk_move_constructor:
1370 case sfk_copy_constructor:
1371 case sfk_inheriting_constructor:
1372 ctor_p = true;
1373 fnname = complete_ctor_identifier;
1374 break;
1376 default:
1377 gcc_unreachable ();
1380 gcc_assert ((sfk == sfk_inheriting_constructor)
1381 == (inherited_base != NULL_TREE));
1383 /* If that user-written default constructor would satisfy the
1384 requirements of a constexpr constructor (7.1.5), the
1385 implicitly-defined default constructor is constexpr. */
1386 if (constexpr_p)
1387 *constexpr_p = ctor_p;
1389 move_p = false;
1390 switch (sfk)
1392 case sfk_constructor:
1393 case sfk_destructor:
1394 case sfk_inheriting_constructor:
1395 copy_arg_p = false;
1396 break;
1398 case sfk_move_constructor:
1399 case sfk_move_assignment:
1400 move_p = true;
1401 case sfk_copy_constructor:
1402 case sfk_copy_assignment:
1403 copy_arg_p = true;
1404 break;
1406 default:
1407 gcc_unreachable ();
1410 expected_trivial = type_has_trivial_fn (ctype, sfk);
1411 if (trivial_p)
1412 *trivial_p = expected_trivial;
1414 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
1415 class versions and other properties of the type. But a subobject
1416 class can be trivially copyable and yet have overload resolution
1417 choose a template constructor for initialization, depending on
1418 rvalueness and cv-quals. And furthermore, a member in a base might
1419 be trivial but deleted or otherwise not callable. So we can't exit
1420 early in C++0x. The same considerations apply in C++98/03, but
1421 there the definition of triviality does not consider overload
1422 resolution, so a constructor can be trivial even if it would otherwise
1423 call a non-trivial constructor. */
1424 if (expected_trivial
1425 && (!copy_arg_p || cxx_dialect < cxx11))
1427 if (constexpr_p && sfk == sfk_constructor)
1429 bool cx = trivial_default_constructor_is_constexpr (ctype);
1430 *constexpr_p = cx;
1431 if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
1432 /* A trivial constructor doesn't have any NSDMI. */
1433 inform (input_location, "defaulted default constructor does "
1434 "not initialize any non-static data member");
1436 if (!diag && cxx_dialect < cxx11)
1437 return;
1440 ++cp_unevaluated_operand;
1441 ++c_inhibit_evaluation_warnings;
1442 push_deferring_access_checks (dk_no_deferred);
1444 scope = push_scope (ctype);
1446 flags = LOOKUP_NORMAL|LOOKUP_SPECULATIVE;
1447 if (!inherited_base)
1448 flags |= LOOKUP_DEFAULTED;
1450 complain = diag ? tf_warning_or_error : tf_none;
1452 if (const_p)
1453 quals = TYPE_QUAL_CONST;
1454 else
1455 quals = TYPE_UNQUALIFIED;
1456 argtype = NULL_TREE;
1458 for (binfo = TYPE_BINFO (ctype), i = 0;
1459 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
1461 tree basetype = BINFO_TYPE (base_binfo);
1463 if (!assign_p && BINFO_VIRTUAL_P (base_binfo))
1464 /* We'll handle virtual bases below. */
1465 continue;
1467 if (copy_arg_p)
1468 argtype = build_stub_type (basetype, quals, move_p);
1469 else if (basetype == inherited_base)
1470 argtype = inherited_parms;
1471 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1472 if (inherited_base)
1473 argtype = NULL_TREE;
1475 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1476 constexpr_p, diag, basetype);
1477 if (ctor_p)
1479 /* In a constructor we also need to check the subobject
1480 destructors for cleanup of partially constructed objects. */
1481 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1482 NULL_TREE, flags, complain);
1483 /* Note that we don't pass down trivial_p; the subobject
1484 destructors don't affect triviality of the constructor. Nor
1485 do they affect constexpr-ness (a constant expression doesn't
1486 throw) or exception-specification (a throw from one of the
1487 dtors would be a double-fault). */
1488 process_subob_fn (rval, NULL, NULL,
1489 deleted_p, NULL, false,
1490 basetype);
1493 if (check_vdtor && type_has_virtual_destructor (basetype))
1495 rval = locate_fn_flags (ctype, ansi_opname (DELETE_EXPR),
1496 ptr_type_node, flags, complain);
1497 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
1498 to have a null rval (no class-specific op delete). */
1499 if (rval && rval == error_mark_node && deleted_p)
1500 *deleted_p = true;
1501 check_vdtor = false;
1504 if (diag && assign_p && move_p
1505 && BINFO_VIRTUAL_P (base_binfo)
1506 && rval && TREE_CODE (rval) == FUNCTION_DECL
1507 && move_fn_p (rval) && !trivial_fn_p (rval)
1508 && vbase_has_user_provided_move_assign (basetype))
1509 warning (OPT_Wvirtual_move_assign,
1510 "defaulted move assignment for %qT calls a non-trivial "
1511 "move assignment operator for virtual base %qT",
1512 ctype, basetype);
1515 vbases = CLASSTYPE_VBASECLASSES (ctype);
1516 if (vec_safe_is_empty (vbases))
1517 /* No virtual bases to worry about. */;
1518 else if (!assign_p)
1520 if (constexpr_p)
1521 *constexpr_p = false;
1522 FOR_EACH_VEC_ELT (*vbases, i, base_binfo)
1524 tree basetype = BINFO_TYPE (base_binfo);
1525 if (copy_arg_p)
1526 argtype = build_stub_type (basetype, quals, move_p);
1527 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1529 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1530 constexpr_p, diag, basetype);
1531 if (ctor_p && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype))
1533 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1534 NULL_TREE, flags, complain);
1535 process_subob_fn (rval, NULL, NULL,
1536 deleted_p, NULL, false,
1537 basetype);
1542 /* Now handle the non-static data members. */
1543 walk_field_subobs (TYPE_FIELDS (ctype), fnname, sfk, quals,
1544 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1545 deleted_p, constexpr_p,
1546 diag, flags, complain);
1547 if (ctor_p)
1548 walk_field_subobs (TYPE_FIELDS (ctype), complete_dtor_identifier,
1549 sfk_destructor, TYPE_UNQUALIFIED, false,
1550 false, false, NULL, NULL,
1551 deleted_p, NULL,
1552 false, flags, complain);
1554 pop_scope (scope);
1556 pop_deferring_access_checks ();
1557 --cp_unevaluated_operand;
1558 --c_inhibit_evaluation_warnings;
1561 /* DECL is a defaulted function whose exception specification is now
1562 needed. Return what it should be. */
1564 tree
1565 get_defaulted_eh_spec (tree decl)
1567 if (DECL_CLONED_FUNCTION_P (decl))
1568 decl = DECL_CLONED_FUNCTION (decl);
1569 special_function_kind sfk = special_function_p (decl);
1570 tree ctype = DECL_CONTEXT (decl);
1571 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1572 tree parm_type = TREE_VALUE (parms);
1573 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1574 tree spec = empty_except_spec;
1575 synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL,
1576 NULL, false, DECL_INHERITED_CTOR_BASE (decl),
1577 parms);
1578 return spec;
1581 /* DECL is a deleted function. If it's implicitly deleted, explain why and
1582 return true; else return false. */
1584 bool
1585 maybe_explain_implicit_delete (tree decl)
1587 /* If decl is a clone, get the primary variant. */
1588 decl = DECL_ORIGIN (decl);
1589 gcc_assert (DECL_DELETED_FN (decl));
1590 if (DECL_DEFAULTED_FN (decl))
1592 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
1593 static hash_set<tree> *explained;
1595 special_function_kind sfk;
1596 location_t loc;
1597 bool informed;
1598 tree ctype;
1600 if (!explained)
1601 explained = new hash_set<tree>;
1602 if (explained->add (decl))
1603 return true;
1605 sfk = special_function_p (decl);
1606 ctype = DECL_CONTEXT (decl);
1607 loc = input_location;
1608 input_location = DECL_SOURCE_LOCATION (decl);
1610 informed = false;
1611 if (LAMBDA_TYPE_P (ctype))
1613 informed = true;
1614 if (sfk == sfk_constructor)
1615 inform (DECL_SOURCE_LOCATION (decl),
1616 "a lambda closure type has a deleted default constructor");
1617 else if (sfk == sfk_copy_assignment)
1618 inform (DECL_SOURCE_LOCATION (decl),
1619 "a lambda closure type has a deleted copy assignment operator");
1620 else
1621 informed = false;
1623 else if (DECL_ARTIFICIAL (decl)
1624 && (sfk == sfk_copy_assignment
1625 || sfk == sfk_copy_constructor)
1626 && (type_has_user_declared_move_constructor (ctype)
1627 || type_has_user_declared_move_assign (ctype)))
1629 inform (0, "%q+#D is implicitly declared as deleted because %qT "
1630 "declares a move constructor or move assignment operator",
1631 decl, ctype);
1632 informed = true;
1634 if (!informed)
1636 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1637 tree parm_type = TREE_VALUE (parms);
1638 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1639 tree raises = NULL_TREE;
1640 bool deleted_p = false;
1641 tree scope = push_scope (ctype);
1643 synthesized_method_walk (ctype, sfk, const_p,
1644 &raises, NULL, &deleted_p, NULL, false,
1645 DECL_INHERITED_CTOR_BASE (decl), parms);
1646 if (deleted_p)
1648 inform (0, "%q+#D is implicitly deleted because the default "
1649 "definition would be ill-formed:", decl);
1650 synthesized_method_walk (ctype, sfk, const_p,
1651 NULL, NULL, NULL, NULL, true,
1652 DECL_INHERITED_CTOR_BASE (decl), parms);
1654 else if (!comp_except_specs
1655 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
1656 raises, ce_normal))
1657 inform (DECL_SOURCE_LOCATION (decl), "%q#F is implicitly "
1658 "deleted because its exception-specification does not "
1659 "match the implicit exception-specification %qX",
1660 decl, raises);
1661 #ifdef ENABLE_CHECKING
1662 else
1663 gcc_unreachable ();
1664 #endif
1666 pop_scope (scope);
1669 input_location = loc;
1670 return true;
1672 return false;
1675 /* DECL is a defaulted function which was declared constexpr. Explain why
1676 it can't be constexpr. */
1678 void
1679 explain_implicit_non_constexpr (tree decl)
1681 tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl));
1682 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1683 bool dummy;
1684 synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
1685 special_function_p (decl), const_p,
1686 NULL, NULL, NULL, &dummy, true,
1687 DECL_INHERITED_CTOR_BASE (decl),
1688 FUNCTION_FIRST_USER_PARMTYPE (decl));
1691 /* DECL is an instantiation of an inheriting constructor template. Deduce
1692 the correct exception-specification and deletedness for this particular
1693 specialization. */
1695 void
1696 deduce_inheriting_ctor (tree decl)
1698 gcc_assert (DECL_INHERITED_CTOR_BASE (decl));
1699 tree spec;
1700 bool trivial, constexpr_, deleted;
1701 synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor,
1702 false, &spec, &trivial, &deleted, &constexpr_,
1703 /*diag*/false,
1704 DECL_INHERITED_CTOR_BASE (decl),
1705 FUNCTION_FIRST_USER_PARMTYPE (decl));
1706 DECL_DELETED_FN (decl) = deleted;
1707 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
1710 /* Implicitly declare the special function indicated by KIND, as a
1711 member of TYPE. For copy constructors and assignment operators,
1712 CONST_P indicates whether these functions should take a const
1713 reference argument or a non-const reference. Returns the
1714 FUNCTION_DECL for the implicitly declared function. */
1716 tree
1717 implicitly_declare_fn (special_function_kind kind, tree type,
1718 bool const_p, tree inherited_ctor,
1719 tree inherited_parms)
1721 tree fn;
1722 tree parameter_types = void_list_node;
1723 tree return_type;
1724 tree fn_type;
1725 tree raises = empty_except_spec;
1726 tree rhs_parm_type = NULL_TREE;
1727 tree this_parm;
1728 tree name;
1729 HOST_WIDE_INT saved_processing_template_decl;
1730 bool deleted_p;
1731 bool constexpr_p;
1733 /* Because we create declarations for implicitly declared functions
1734 lazily, we may be creating the declaration for a member of TYPE
1735 while in some completely different context. However, TYPE will
1736 never be a dependent class (because we never want to do lookups
1737 for implicitly defined functions in a dependent class).
1738 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
1739 because we only create clones for constructors and destructors
1740 when not in a template. */
1741 gcc_assert (!dependent_type_p (type));
1742 saved_processing_template_decl = processing_template_decl;
1743 processing_template_decl = 0;
1745 type = TYPE_MAIN_VARIANT (type);
1747 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (type))
1749 if (kind == sfk_destructor)
1750 /* See comment in check_special_function_return_type. */
1751 return_type = build_pointer_type (void_type_node);
1752 else
1753 return_type = build_pointer_type (type);
1755 else
1756 return_type = void_type_node;
1758 switch (kind)
1760 case sfk_destructor:
1761 /* Destructor. */
1762 name = constructor_name (type);
1763 break;
1765 case sfk_constructor:
1766 /* Default constructor. */
1767 name = constructor_name (type);
1768 break;
1770 case sfk_copy_constructor:
1771 case sfk_copy_assignment:
1772 case sfk_move_constructor:
1773 case sfk_move_assignment:
1774 case sfk_inheriting_constructor:
1776 bool move_p;
1777 if (kind == sfk_copy_assignment
1778 || kind == sfk_move_assignment)
1780 return_type = build_reference_type (type);
1781 name = ansi_assopname (NOP_EXPR);
1783 else
1784 name = constructor_name (type);
1786 if (kind == sfk_inheriting_constructor)
1787 parameter_types = inherited_parms;
1788 else
1790 if (const_p)
1791 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
1792 else
1793 rhs_parm_type = type;
1794 move_p = (kind == sfk_move_assignment
1795 || kind == sfk_move_constructor);
1796 rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
1798 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
1800 break;
1802 default:
1803 gcc_unreachable ();
1806 tree inherited_base = (inherited_ctor
1807 ? DECL_CONTEXT (inherited_ctor)
1808 : NULL_TREE);
1809 bool trivial_p = false;
1811 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
1813 /* For an inheriting constructor template, just copy these flags from
1814 the inherited constructor template for now. */
1815 raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor));
1816 deleted_p = DECL_DELETED_FN (inherited_ctor);
1817 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
1819 else if (cxx_dialect >= cxx11)
1821 raises = unevaluated_noexcept_spec ();
1822 synthesized_method_walk (type, kind, const_p, NULL, &trivial_p,
1823 &deleted_p, &constexpr_p, false,
1824 inherited_base, inherited_parms);
1826 else
1827 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
1828 &deleted_p, &constexpr_p, false,
1829 inherited_base, inherited_parms);
1830 /* Don't bother marking a deleted constructor as constexpr. */
1831 if (deleted_p)
1832 constexpr_p = false;
1833 /* A trivial copy/move constructor is also a constexpr constructor,
1834 unless the class has virtual bases (7.1.5p4). */
1835 else if (trivial_p && cxx_dialect >= cxx11
1836 && (kind == sfk_copy_constructor
1837 || kind == sfk_move_constructor)
1838 && !CLASSTYPE_VBASECLASSES (type))
1839 gcc_assert (constexpr_p);
1841 if (!trivial_p && type_has_trivial_fn (type, kind))
1842 type_set_nontrivial_flag (type, kind);
1844 /* Create the function. */
1845 fn_type = build_method_type_directly (type, return_type, parameter_types);
1846 if (raises)
1847 fn_type = build_exception_variant (fn_type, raises);
1848 fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
1849 if (kind != sfk_inheriting_constructor)
1850 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
1851 if (kind == sfk_constructor || kind == sfk_copy_constructor
1852 || kind == sfk_move_constructor || kind == sfk_inheriting_constructor)
1853 DECL_CONSTRUCTOR_P (fn) = 1;
1854 else if (kind == sfk_destructor)
1855 DECL_DESTRUCTOR_P (fn) = 1;
1856 else
1858 DECL_ASSIGNMENT_OPERATOR_P (fn) = 1;
1859 SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR);
1862 /* If pointers to member functions use the least significant bit to
1863 indicate whether a function is virtual, ensure a pointer
1864 to this function will have that bit clear. */
1865 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
1866 && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
1867 DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
1869 /* Create the explicit arguments. */
1870 if (rhs_parm_type)
1872 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
1873 want its type to be included in the mangled function
1874 name. */
1875 tree decl = cp_build_parm_decl (NULL_TREE, rhs_parm_type);
1876 TREE_READONLY (decl) = 1;
1877 retrofit_lang_decl (decl);
1878 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
1879 DECL_ARGUMENTS (fn) = decl;
1881 else if (kind == sfk_inheriting_constructor)
1883 tree *p = &DECL_ARGUMENTS (fn);
1884 int index = 1;
1885 for (tree parm = inherited_parms; parm != void_list_node;
1886 parm = TREE_CHAIN (parm))
1888 *p = cp_build_parm_decl (NULL_TREE, TREE_VALUE (parm));
1889 retrofit_lang_decl (*p);
1890 DECL_PARM_LEVEL (*p) = 1;
1891 DECL_PARM_INDEX (*p) = index++;
1892 DECL_CONTEXT (*p) = fn;
1893 p = &DECL_CHAIN (*p);
1895 SET_DECL_INHERITED_CTOR_BASE (fn, inherited_base);
1896 DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor);
1897 /* A constructor so declared has the same access as the corresponding
1898 constructor in X. */
1899 TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor);
1900 TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor);
1901 /* Copy constexpr from the inherited constructor even if the
1902 inheriting constructor doesn't satisfy the requirements. */
1903 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
1905 /* Add the "this" parameter. */
1906 this_parm = build_this_parm (fn_type, TYPE_UNQUALIFIED);
1907 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
1908 DECL_ARGUMENTS (fn) = this_parm;
1910 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
1911 DECL_IN_AGGR_P (fn) = 1;
1912 DECL_ARTIFICIAL (fn) = 1;
1913 DECL_DEFAULTED_FN (fn) = 1;
1914 if (cxx_dialect >= cxx11)
1916 /* "The closure type associated with a lambda-expression has a deleted
1917 default constructor and a deleted copy assignment operator." */
1918 if ((kind == sfk_constructor
1919 || kind == sfk_copy_assignment)
1920 && LAMBDA_TYPE_P (type))
1921 deleted_p = true;
1922 DECL_DELETED_FN (fn) = deleted_p;
1923 DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
1925 DECL_EXTERNAL (fn) = true;
1926 DECL_NOT_REALLY_EXTERN (fn) = 1;
1927 DECL_DECLARED_INLINE_P (fn) = 1;
1928 DECL_COMDAT (fn) = 1;
1929 set_linkage_according_to_type (type, fn);
1930 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
1931 gcc_assert (!TREE_USED (fn));
1933 /* Restore PROCESSING_TEMPLATE_DECL. */
1934 processing_template_decl = saved_processing_template_decl;
1936 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
1937 fn = add_inherited_template_parms (fn, inherited_ctor);
1939 /* Warn about calling a non-trivial move assignment in a virtual base. */
1940 if (kind == sfk_move_assignment && !deleted_p && !trivial_p
1941 && CLASSTYPE_VBASECLASSES (type))
1943 location_t loc = input_location;
1944 input_location = DECL_SOURCE_LOCATION (fn);
1945 synthesized_method_walk (type, kind, const_p,
1946 NULL, NULL, NULL, NULL, true,
1947 NULL_TREE, NULL_TREE);
1948 input_location = loc;
1951 return fn;
1954 /* Gives any errors about defaulted functions which need to be deferred
1955 until the containing class is complete. */
1957 void
1958 defaulted_late_check (tree fn)
1960 /* Complain about invalid signature for defaulted fn. */
1961 tree ctx = DECL_CONTEXT (fn);
1962 special_function_kind kind = special_function_p (fn);
1963 bool fn_const_p = (copy_fn_p (fn) == 2);
1964 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
1965 NULL, NULL);
1966 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
1968 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
1969 TREE_TYPE (TREE_TYPE (implicit_fn)))
1970 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1971 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn))))
1973 error ("defaulted declaration %q+D", fn);
1974 error_at (DECL_SOURCE_LOCATION (fn),
1975 "does not match expected signature %qD", implicit_fn);
1978 /* 8.4.2/2: An explicitly-defaulted function (...) may have an explicit
1979 exception-specification only if it is compatible (15.4) with the
1980 exception-specification on the implicit declaration. If a function
1981 is explicitly defaulted on its first declaration, (...) it is
1982 implicitly considered to have the same exception-specification as if
1983 it had been implicitly declared. */
1984 maybe_instantiate_noexcept (fn);
1985 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
1986 if (!fn_spec)
1988 if (DECL_DEFAULTED_IN_CLASS_P (fn))
1989 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
1991 else if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
1992 /* Equivalent to the implicit spec. */;
1993 else if (DECL_DEFAULTED_IN_CLASS_P (fn)
1994 && !CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
1995 /* We can't compare an explicit exception-specification on a
1996 constructor defaulted in the class body to the implicit
1997 exception-specification until after we've parsed any NSDMI; see
1998 after_nsdmi_defaulted_late_checks. */;
1999 else
2001 tree eh_spec = get_defaulted_eh_spec (fn);
2002 if (!comp_except_specs (fn_spec, eh_spec, ce_normal))
2004 if (DECL_DEFAULTED_IN_CLASS_P (fn))
2005 DECL_DELETED_FN (fn) = true;
2006 else
2007 error ("function %q+D defaulted on its redeclaration "
2008 "with an exception-specification that differs from "
2009 "the implicit exception-specification %qX", fn, eh_spec);
2013 if (DECL_DEFAULTED_IN_CLASS_P (fn)
2014 && DECL_DECLARED_CONSTEXPR_P (implicit_fn))
2016 /* Hmm...should we do this for out-of-class too? Should it be OK to
2017 add constexpr later like inline, rather than requiring
2018 declarations to match? */
2019 DECL_DECLARED_CONSTEXPR_P (fn) = true;
2020 if (kind == sfk_constructor)
2021 TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
2024 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
2025 && DECL_DECLARED_CONSTEXPR_P (fn))
2027 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
2029 error ("explicitly defaulted function %q+D cannot be declared "
2030 "as constexpr because the implicit declaration is not "
2031 "constexpr:", fn);
2032 explain_implicit_non_constexpr (fn);
2034 DECL_DECLARED_CONSTEXPR_P (fn) = false;
2037 if (DECL_DELETED_FN (implicit_fn))
2038 DECL_DELETED_FN (fn) = 1;
2041 /* OK, we've parsed the NSDMI for class T, now we can check any explicit
2042 exception-specifications on functions defaulted in the class body. */
2044 void
2045 after_nsdmi_defaulted_late_checks (tree t)
2047 if (uses_template_parms (t))
2048 return;
2049 if (t == error_mark_node)
2050 return;
2051 for (tree fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
2052 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
2054 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2055 if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
2056 continue;
2058 tree eh_spec = get_defaulted_eh_spec (fn);
2059 if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)),
2060 eh_spec, ce_normal))
2061 DECL_DELETED_FN (fn) = true;
2065 /* Returns true iff FN can be explicitly defaulted, and gives any
2066 errors if defaulting FN is ill-formed. */
2068 bool
2069 defaultable_fn_check (tree fn)
2071 special_function_kind kind = sfk_none;
2073 if (template_parm_scope_p ())
2075 error ("a template cannot be defaulted");
2076 return false;
2079 if (DECL_CONSTRUCTOR_P (fn))
2081 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
2082 kind = sfk_constructor;
2083 else if (copy_fn_p (fn) > 0
2084 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
2085 == void_list_node))
2086 kind = sfk_copy_constructor;
2087 else if (move_fn_p (fn))
2088 kind = sfk_move_constructor;
2090 else if (DECL_DESTRUCTOR_P (fn))
2091 kind = sfk_destructor;
2092 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2093 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
2095 if (copy_fn_p (fn))
2096 kind = sfk_copy_assignment;
2097 else if (move_fn_p (fn))
2098 kind = sfk_move_assignment;
2101 if (kind == sfk_none)
2103 error ("%qD cannot be defaulted", fn);
2104 return false;
2106 else
2108 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
2109 t && t != void_list_node; t = TREE_CHAIN (t))
2110 if (TREE_PURPOSE (t))
2112 error ("defaulted function %q+D with default argument", fn);
2113 break;
2116 /* Avoid do_warn_unused_parameter warnings. */
2117 for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p))
2118 if (DECL_NAME (p))
2119 TREE_NO_WARNING (p) = 1;
2121 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn)))
2122 /* Defer checking. */;
2123 else if (!processing_template_decl)
2124 defaulted_late_check (fn);
2126 return true;
2130 /* Add an implicit declaration to TYPE for the kind of function
2131 indicated by SFK. Return the FUNCTION_DECL for the new implicit
2132 declaration. */
2134 tree
2135 lazily_declare_fn (special_function_kind sfk, tree type)
2137 tree fn;
2138 /* Whether or not the argument has a const reference type. */
2139 bool const_p = false;
2141 switch (sfk)
2143 case sfk_constructor:
2144 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
2145 break;
2146 case sfk_copy_constructor:
2147 const_p = TYPE_HAS_CONST_COPY_CTOR (type);
2148 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
2149 break;
2150 case sfk_move_constructor:
2151 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
2152 break;
2153 case sfk_copy_assignment:
2154 const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
2155 CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
2156 break;
2157 case sfk_move_assignment:
2158 CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
2159 break;
2160 case sfk_destructor:
2161 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
2162 break;
2163 default:
2164 gcc_unreachable ();
2167 /* Declare the function. */
2168 fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL);
2170 /* [class.copy]/8 If the class definition declares a move constructor or
2171 move assignment operator, the implicitly declared copy constructor is
2172 defined as deleted.... */
2173 if ((sfk == sfk_copy_assignment
2174 || sfk == sfk_copy_constructor)
2175 && (type_has_user_declared_move_constructor (type)
2176 || type_has_user_declared_move_assign (type)))
2177 DECL_DELETED_FN (fn) = true;
2179 /* A destructor may be virtual. */
2180 if (sfk == sfk_destructor
2181 || sfk == sfk_move_assignment
2182 || sfk == sfk_copy_assignment)
2183 check_for_override (fn, type);
2184 /* Add it to CLASSTYPE_METHOD_VEC. */
2185 add_method (type, fn, NULL_TREE);
2186 /* Add it to TYPE_METHODS. */
2187 if (sfk == sfk_destructor
2188 && DECL_VIRTUAL_P (fn))
2189 /* The ABI requires that a virtual destructor go at the end of the
2190 vtable. */
2191 TYPE_METHODS (type) = chainon (TYPE_METHODS (type), fn);
2192 else
2194 DECL_CHAIN (fn) = TYPE_METHODS (type);
2195 TYPE_METHODS (type) = fn;
2197 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
2198 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
2199 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
2200 /* Create appropriate clones. */
2201 clone_function_decl (fn, /*update_method_vec=*/true);
2203 return fn;
2206 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
2207 as there are artificial parms in FN. */
2209 tree
2210 skip_artificial_parms_for (const_tree fn, tree list)
2212 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2213 list = TREE_CHAIN (list);
2214 else
2215 return list;
2217 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2218 list = TREE_CHAIN (list);
2219 if (DECL_HAS_VTT_PARM_P (fn))
2220 list = TREE_CHAIN (list);
2221 return list;
2224 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
2225 artificial parms in FN. */
2228 num_artificial_parms_for (const_tree fn)
2230 int count = 0;
2232 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2233 count++;
2234 else
2235 return 0;
2237 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2238 count++;
2239 if (DECL_HAS_VTT_PARM_P (fn))
2240 count++;
2241 return count;
2245 #include "gt-cp-method.h"