Concretize gimple_cond_set_code
[official-gcc.git] / gcc / cp / method.c
blob418ed8874d09d0f76f3659b828f5b2dd86630a16
1 /* Handle the hair of processing (but not expanding) inline functions.
2 Also manage function and variable name overloading.
3 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* Handle method declarations. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "tree.h"
29 #include "stringpool.h"
30 #include "varasm.h"
31 #include "cp-tree.h"
32 #include "flags.h"
33 #include "toplev.h"
34 #include "tm_p.h"
35 #include "target.h"
36 #include "common/common-target.h"
37 #include "diagnostic.h"
38 #include "cgraph.h"
40 /* Various flags to control the mangling process. */
42 enum mangling_flags
44 /* No flags. */
45 mf_none = 0,
46 /* The thing we are presently mangling is part of a template type,
47 rather than a fully instantiated type. Therefore, we may see
48 complex expressions where we would normally expect to see a
49 simple integer constant. */
50 mf_maybe_uninstantiated = 1,
51 /* When mangling a numeric value, use the form `_XX_' (instead of
52 just `XX') if the value has more than one digit. */
53 mf_use_underscores_around_value = 2
56 typedef enum mangling_flags mangling_flags;
58 static void do_build_copy_assign (tree);
59 static void do_build_copy_constructor (tree);
60 static tree make_alias_for_thunk (tree);
62 /* Called once to initialize method.c. */
64 void
65 init_method (void)
67 init_mangle ();
70 /* Return a this or result adjusting thunk to FUNCTION. THIS_ADJUSTING
71 indicates whether it is a this or result adjusting thunk.
72 FIXED_OFFSET and VIRTUAL_OFFSET indicate how to do the adjustment
73 (see thunk_adjust). VIRTUAL_OFFSET can be NULL, but FIXED_OFFSET
74 never is. VIRTUAL_OFFSET is the /index/ into the vtable for this
75 adjusting thunks, we scale it to a byte offset. For covariant
76 thunks VIRTUAL_OFFSET is the virtual binfo. You must post process
77 the returned thunk with finish_thunk. */
79 tree
80 make_thunk (tree function, bool this_adjusting,
81 tree fixed_offset, tree virtual_offset)
83 HOST_WIDE_INT d;
84 tree thunk;
86 gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
87 /* We can have this thunks to covariant thunks, but not vice versa. */
88 gcc_assert (!DECL_THIS_THUNK_P (function));
89 gcc_assert (!DECL_RESULT_THUNK_P (function) || this_adjusting);
91 /* Scale the VIRTUAL_OFFSET to be in terms of bytes. */
92 if (this_adjusting && virtual_offset)
93 virtual_offset
94 = size_binop (MULT_EXPR,
95 virtual_offset,
96 convert (ssizetype,
97 TYPE_SIZE_UNIT (vtable_entry_type)));
99 d = tree_to_shwi (fixed_offset);
101 /* See if we already have the thunk in question. For this_adjusting
102 thunks VIRTUAL_OFFSET will be an INTEGER_CST, for covariant thunks it
103 will be a BINFO. */
104 for (thunk = DECL_THUNKS (function); thunk; thunk = DECL_CHAIN (thunk))
105 if (DECL_THIS_THUNK_P (thunk) == this_adjusting
106 && THUNK_FIXED_OFFSET (thunk) == d
107 && !virtual_offset == !THUNK_VIRTUAL_OFFSET (thunk)
108 && (!virtual_offset
109 || (this_adjusting
110 ? tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk),
111 virtual_offset)
112 : THUNK_VIRTUAL_OFFSET (thunk) == virtual_offset)))
113 return thunk;
115 /* All thunks must be created before FUNCTION is actually emitted;
116 the ABI requires that all thunks be emitted together with the
117 function to which they transfer control. */
118 gcc_assert (!TREE_ASM_WRITTEN (function));
119 /* Likewise, we can only be adding thunks to a function declared in
120 the class currently being laid out. */
121 gcc_assert (TYPE_SIZE (DECL_CONTEXT (function))
122 && TYPE_BEING_DEFINED (DECL_CONTEXT (function)));
124 thunk = build_decl (DECL_SOURCE_LOCATION (function),
125 FUNCTION_DECL, NULL_TREE, TREE_TYPE (function));
126 DECL_LANG_SPECIFIC (thunk) = DECL_LANG_SPECIFIC (function);
127 cxx_dup_lang_specific_decl (thunk);
128 DECL_VIRTUAL_P (thunk) = true;
129 SET_DECL_THUNKS (thunk, NULL_TREE);
131 DECL_CONTEXT (thunk) = DECL_CONTEXT (function);
132 TREE_READONLY (thunk) = TREE_READONLY (function);
133 TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (function);
134 TREE_PUBLIC (thunk) = TREE_PUBLIC (function);
135 SET_DECL_THUNK_P (thunk, this_adjusting);
136 THUNK_TARGET (thunk) = function;
137 THUNK_FIXED_OFFSET (thunk) = d;
138 THUNK_VIRTUAL_OFFSET (thunk) = virtual_offset;
139 THUNK_ALIAS (thunk) = NULL_TREE;
141 DECL_INTERFACE_KNOWN (thunk) = 1;
142 DECL_NOT_REALLY_EXTERN (thunk) = 1;
143 DECL_COMDAT (thunk) = DECL_COMDAT (function);
144 DECL_SAVED_FUNCTION_DATA (thunk) = NULL;
145 /* The thunk itself is not a constructor or destructor, even if
146 the thing it is thunking to is. */
147 DECL_DESTRUCTOR_P (thunk) = 0;
148 DECL_CONSTRUCTOR_P (thunk) = 0;
149 DECL_EXTERNAL (thunk) = 1;
150 DECL_ARTIFICIAL (thunk) = 1;
151 /* The THUNK is not a pending inline, even if the FUNCTION is. */
152 DECL_PENDING_INLINE_P (thunk) = 0;
153 DECL_DECLARED_INLINE_P (thunk) = 0;
154 /* Nor is it a template instantiation. */
155 DECL_USE_TEMPLATE (thunk) = 0;
156 DECL_TEMPLATE_INFO (thunk) = NULL;
158 /* Add it to the list of thunks associated with FUNCTION. */
159 DECL_CHAIN (thunk) = DECL_THUNKS (function);
160 SET_DECL_THUNKS (function, thunk);
162 return thunk;
165 /* Finish THUNK, a thunk decl. */
167 void
168 finish_thunk (tree thunk)
170 tree function, name;
171 tree fixed_offset = ssize_int (THUNK_FIXED_OFFSET (thunk));
172 tree virtual_offset = THUNK_VIRTUAL_OFFSET (thunk);
174 gcc_assert (!DECL_NAME (thunk) && DECL_THUNK_P (thunk));
175 if (virtual_offset && DECL_RESULT_THUNK_P (thunk))
176 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
177 function = THUNK_TARGET (thunk);
178 name = mangle_thunk (function, DECL_THIS_THUNK_P (thunk),
179 fixed_offset, virtual_offset);
181 /* We can end up with declarations of (logically) different
182 covariant thunks, that do identical adjustments. The two thunks
183 will be adjusting between within different hierarchies, which
184 happen to have the same layout. We must nullify one of them to
185 refer to the other. */
186 if (DECL_RESULT_THUNK_P (thunk))
188 tree cov_probe;
190 for (cov_probe = DECL_THUNKS (function);
191 cov_probe; cov_probe = DECL_CHAIN (cov_probe))
192 if (DECL_NAME (cov_probe) == name)
194 gcc_assert (!DECL_THUNKS (thunk));
195 THUNK_ALIAS (thunk) = (THUNK_ALIAS (cov_probe)
196 ? THUNK_ALIAS (cov_probe) : cov_probe);
197 break;
201 DECL_NAME (thunk) = name;
202 SET_DECL_ASSEMBLER_NAME (thunk, name);
205 static GTY (()) int thunk_labelno;
207 /* Create a static alias to target. */
209 tree
210 make_alias_for (tree target, tree newid)
212 tree alias = build_decl (DECL_SOURCE_LOCATION (target),
213 TREE_CODE (target), newid, TREE_TYPE (target));
214 DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (target);
215 cxx_dup_lang_specific_decl (alias);
216 DECL_CONTEXT (alias) = NULL;
217 TREE_READONLY (alias) = TREE_READONLY (target);
218 TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (target);
219 TREE_PUBLIC (alias) = 0;
220 DECL_INTERFACE_KNOWN (alias) = 1;
221 if (DECL_LANG_SPECIFIC (alias))
223 DECL_NOT_REALLY_EXTERN (alias) = 1;
224 DECL_USE_TEMPLATE (alias) = 0;
225 DECL_TEMPLATE_INFO (alias) = NULL;
227 DECL_EXTERNAL (alias) = 0;
228 DECL_ARTIFICIAL (alias) = 1;
229 DECL_TEMPLATE_INSTANTIATED (alias) = 0;
230 if (TREE_CODE (alias) == FUNCTION_DECL)
232 DECL_SAVED_FUNCTION_DATA (alias) = NULL;
233 DECL_DESTRUCTOR_P (alias) = 0;
234 DECL_CONSTRUCTOR_P (alias) = 0;
235 DECL_PENDING_INLINE_P (alias) = 0;
236 DECL_DECLARED_INLINE_P (alias) = 0;
237 DECL_INITIAL (alias) = error_mark_node;
238 DECL_ARGUMENTS (alias) = copy_list (DECL_ARGUMENTS (target));
240 else
241 TREE_STATIC (alias) = 1;
242 TREE_ADDRESSABLE (alias) = 1;
243 TREE_USED (alias) = 1;
244 SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
245 return alias;
248 static tree
249 make_alias_for_thunk (tree function)
251 tree alias;
252 char buf[256];
254 targetm.asm_out.generate_internal_label (buf, "LTHUNK", thunk_labelno);
255 thunk_labelno++;
257 alias = make_alias_for (function, get_identifier (buf));
259 if (!flag_syntax_only)
261 struct cgraph_node *funcn, *aliasn;
262 funcn = cgraph_node::get (function);
263 gcc_checking_assert (funcn);
264 aliasn = cgraph_node::create_same_body_alias (alias, function);
265 DECL_ASSEMBLER_NAME (function);
266 gcc_assert (aliasn != NULL);
269 return alias;
272 /* Emit the definition of a C++ multiple inheritance or covariant
273 return vtable thunk. If EMIT_P is nonzero, the thunk is emitted
274 immediately. */
276 void
277 use_thunk (tree thunk_fndecl, bool emit_p)
279 tree a, t, function, alias;
280 tree virtual_offset;
281 HOST_WIDE_INT fixed_offset, virtual_value;
282 bool this_adjusting = DECL_THIS_THUNK_P (thunk_fndecl);
283 struct cgraph_node *funcn, *thunk_node;
285 /* We should have called finish_thunk to give it a name. */
286 gcc_assert (DECL_NAME (thunk_fndecl));
288 /* We should never be using an alias, always refer to the
289 aliased thunk. */
290 gcc_assert (!THUNK_ALIAS (thunk_fndecl));
292 if (TREE_ASM_WRITTEN (thunk_fndecl))
293 return;
295 function = THUNK_TARGET (thunk_fndecl);
296 if (DECL_RESULT (thunk_fndecl))
297 /* We already turned this thunk into an ordinary function.
298 There's no need to process this thunk again. */
299 return;
301 if (DECL_THUNK_P (function))
302 /* The target is itself a thunk, process it now. */
303 use_thunk (function, emit_p);
305 /* Thunks are always addressable; they only appear in vtables. */
306 TREE_ADDRESSABLE (thunk_fndecl) = 1;
308 /* Figure out what function is being thunked to. It's referenced in
309 this translation unit. */
310 TREE_ADDRESSABLE (function) = 1;
311 mark_used (function);
312 if (!emit_p)
313 return;
315 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function))
316 alias = make_alias_for_thunk (function);
317 else
318 alias = function;
320 fixed_offset = THUNK_FIXED_OFFSET (thunk_fndecl);
321 virtual_offset = THUNK_VIRTUAL_OFFSET (thunk_fndecl);
323 if (virtual_offset)
325 if (!this_adjusting)
326 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
327 virtual_value = tree_to_shwi (virtual_offset);
328 gcc_assert (virtual_value);
330 else
331 virtual_value = 0;
333 /* And, if we need to emit the thunk, it's used. */
334 mark_used (thunk_fndecl);
335 /* This thunk is actually defined. */
336 DECL_EXTERNAL (thunk_fndecl) = 0;
337 /* The linkage of the function may have changed. FIXME in linkage
338 rewrite. */
339 gcc_assert (DECL_INTERFACE_KNOWN (function));
340 TREE_PUBLIC (thunk_fndecl) = TREE_PUBLIC (function);
341 DECL_VISIBILITY (thunk_fndecl) = DECL_VISIBILITY (function);
342 DECL_VISIBILITY_SPECIFIED (thunk_fndecl)
343 = DECL_VISIBILITY_SPECIFIED (function);
344 DECL_COMDAT (thunk_fndecl) = DECL_COMDAT (function);
345 DECL_WEAK (thunk_fndecl) = DECL_WEAK (function);
347 if (flag_syntax_only)
349 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
350 return;
353 push_to_top_level ();
355 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function)
356 && targetm_common.have_named_sections)
358 tree fn = function;
359 struct symtab_node *symbol;
361 if ((symbol = symtab_node::get (function))
362 && symbol->alias)
364 if (symbol->analyzed)
365 fn = symtab_node::get (function)->ultimate_alias_target ()->decl;
366 else
367 fn = symtab_node::get (function)->alias_target;
369 resolve_unique_section (fn, 0, flag_function_sections);
371 if (DECL_SECTION_NAME (fn) != NULL && DECL_ONE_ONLY (fn))
373 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
375 /* Output the thunk into the same section as function. */
376 set_decl_section_name (thunk_fndecl, DECL_SECTION_NAME (fn));
377 symtab_node::get (thunk_fndecl)->implicit_section
378 = symtab_node::get (fn)->implicit_section;
382 /* Set up cloned argument trees for the thunk. */
383 t = NULL_TREE;
384 for (a = DECL_ARGUMENTS (function); a; a = DECL_CHAIN (a))
386 tree x = copy_node (a);
387 DECL_CHAIN (x) = t;
388 DECL_CONTEXT (x) = thunk_fndecl;
389 SET_DECL_RTL (x, NULL);
390 DECL_HAS_VALUE_EXPR_P (x) = 0;
391 TREE_ADDRESSABLE (x) = 0;
392 t = x;
394 a = nreverse (t);
395 DECL_ARGUMENTS (thunk_fndecl) = a;
396 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
397 funcn = cgraph_node::get (function);
398 gcc_checking_assert (funcn);
399 thunk_node = funcn->create_thunk (thunk_fndecl, function,
400 this_adjusting, fixed_offset, virtual_value,
401 virtual_offset, alias);
402 if (DECL_ONE_ONLY (function))
403 thunk_node->add_to_same_comdat_group (funcn);
405 if (!this_adjusting
406 || !targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
407 virtual_value, alias))
409 /* If this is a covariant thunk, or we don't have the necessary
410 code for efficient thunks, generate a thunk function that
411 just makes a call to the real function. Unfortunately, this
412 doesn't work for varargs. */
414 if (varargs_function_p (function))
415 error ("generic thunk code fails for method %q#D which uses %<...%>",
416 function);
419 pop_from_top_level ();
422 /* Code for synthesizing methods which have default semantics defined. */
424 /* True iff CTYPE has a trivial SFK. */
426 static bool
427 type_has_trivial_fn (tree ctype, special_function_kind sfk)
429 switch (sfk)
431 case sfk_constructor:
432 return !TYPE_HAS_COMPLEX_DFLT (ctype);
433 case sfk_copy_constructor:
434 return !TYPE_HAS_COMPLEX_COPY_CTOR (ctype);
435 case sfk_move_constructor:
436 return !TYPE_HAS_COMPLEX_MOVE_CTOR (ctype);
437 case sfk_copy_assignment:
438 return !TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype);
439 case sfk_move_assignment:
440 return !TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype);
441 case sfk_destructor:
442 return !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype);
443 case sfk_inheriting_constructor:
444 return false;
445 default:
446 gcc_unreachable ();
450 /* Note that CTYPE has a non-trivial SFK even though we previously thought
451 it was trivial. */
453 static void
454 type_set_nontrivial_flag (tree ctype, special_function_kind sfk)
456 switch (sfk)
458 case sfk_constructor:
459 TYPE_HAS_COMPLEX_DFLT (ctype) = true;
460 return;
461 case sfk_copy_constructor:
462 TYPE_HAS_COMPLEX_COPY_CTOR (ctype) = true;
463 return;
464 case sfk_move_constructor:
465 TYPE_HAS_COMPLEX_MOVE_CTOR (ctype) = true;
466 return;
467 case sfk_copy_assignment:
468 TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype) = true;
469 return;
470 case sfk_move_assignment:
471 TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype) = true;
472 return;
473 case sfk_destructor:
474 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
475 return;
476 case sfk_inheriting_constructor:
477 default:
478 gcc_unreachable ();
482 /* True iff FN is a trivial defaulted member function ([cd]tor, op=). */
484 bool
485 trivial_fn_p (tree fn)
487 if (!DECL_DEFAULTED_FN (fn))
488 return false;
490 /* If fn is a clone, get the primary variant. */
491 if (tree prim = DECL_CLONED_FUNCTION (fn))
492 fn = prim;
493 return type_has_trivial_fn (DECL_CONTEXT (fn), special_function_p (fn));
496 /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO
497 given the parameter or parameters PARM, possibly inherited constructor
498 base INH, or move flag MOVE_P. */
500 static tree
501 add_one_base_init (tree binfo, tree parm, bool move_p, tree inh,
502 tree member_init_list)
504 tree init;
505 if (inh)
507 /* An inheriting constructor only has a mem-initializer for
508 the base it inherits from. */
509 if (BINFO_TYPE (binfo) != inh)
510 return member_init_list;
512 tree *p = &init;
513 init = NULL_TREE;
514 for (; parm; parm = DECL_CHAIN (parm))
516 tree exp = convert_from_reference (parm);
517 if (TREE_CODE (TREE_TYPE (parm)) != REFERENCE_TYPE
518 || TYPE_REF_IS_RVALUE (TREE_TYPE (parm)))
519 exp = move (exp);
520 *p = build_tree_list (NULL_TREE, exp);
521 p = &TREE_CHAIN (*p);
524 else
526 init = build_base_path (PLUS_EXPR, parm, binfo, 1,
527 tf_warning_or_error);
528 if (move_p)
529 init = move (init);
530 init = build_tree_list (NULL_TREE, init);
532 return tree_cons (binfo, init, member_init_list);
535 /* Generate code for default X(X&) or X(X&&) constructor or an inheriting
536 constructor. */
538 static void
539 do_build_copy_constructor (tree fndecl)
541 tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
542 bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
543 bool trivial = trivial_fn_p (fndecl);
544 tree inh = DECL_INHERITED_CTOR_BASE (fndecl);
546 if (!inh)
547 parm = convert_from_reference (parm);
549 if (trivial
550 && is_empty_class (current_class_type))
551 /* Don't copy the padding byte; it might not have been allocated
552 if *this is a base subobject. */;
553 else if (trivial)
555 tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm);
556 finish_expr_stmt (t);
558 else
560 tree fields = TYPE_FIELDS (current_class_type);
561 tree member_init_list = NULL_TREE;
562 int cvquals = cp_type_quals (TREE_TYPE (parm));
563 int i;
564 tree binfo, base_binfo;
565 tree init;
566 vec<tree, va_gc> *vbases;
568 /* Initialize all the base-classes with the parameter converted
569 to their type so that we get their copy constructor and not
570 another constructor that takes current_class_type. We must
571 deal with the binfo's directly as a direct base might be
572 inaccessible due to ambiguity. */
573 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
574 vec_safe_iterate (vbases, i, &binfo); i++)
576 member_init_list = add_one_base_init (binfo, parm, move_p, inh,
577 member_init_list);
580 for (binfo = TYPE_BINFO (current_class_type), i = 0;
581 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
583 if (BINFO_VIRTUAL_P (base_binfo))
584 continue;
585 member_init_list = add_one_base_init (base_binfo, parm, move_p,
586 inh, member_init_list);
589 for (; fields; fields = DECL_CHAIN (fields))
591 tree field = fields;
592 tree expr_type;
594 if (TREE_CODE (field) != FIELD_DECL)
595 continue;
596 if (inh)
597 continue;
599 expr_type = TREE_TYPE (field);
600 if (DECL_NAME (field))
602 if (VFIELD_NAME_P (DECL_NAME (field)))
603 continue;
605 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
606 /* Just use the field; anonymous types can't have
607 nontrivial copy ctors or assignment ops or this
608 function would be deleted. */;
609 else
610 continue;
612 /* Compute the type of "init->field". If the copy-constructor
613 parameter is, for example, "const S&", and the type of
614 the field is "T", then the type will usually be "const
615 T". (There are no cv-qualified variants of reference
616 types.) */
617 if (TREE_CODE (expr_type) != REFERENCE_TYPE)
619 int quals = cvquals;
621 if (DECL_MUTABLE_P (field))
622 quals &= ~TYPE_QUAL_CONST;
623 quals |= cp_type_quals (expr_type);
624 expr_type = cp_build_qualified_type (expr_type, quals);
627 init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
628 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
629 /* 'move' breaks bit-fields, and has no effect for scalars. */
630 && !scalarish_type_p (expr_type))
631 init = move (init);
632 init = build_tree_list (NULL_TREE, init);
634 member_init_list = tree_cons (field, init, member_init_list);
636 finish_mem_initializers (member_init_list);
640 static void
641 do_build_copy_assign (tree fndecl)
643 tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl));
644 tree compound_stmt;
645 bool move_p = move_fn_p (fndecl);
646 bool trivial = trivial_fn_p (fndecl);
647 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
649 compound_stmt = begin_compound_stmt (0);
650 parm = convert_from_reference (parm);
652 if (trivial
653 && is_empty_class (current_class_type))
654 /* Don't copy the padding byte; it might not have been allocated
655 if *this is a base subobject. */;
656 else if (trivial)
658 tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
659 finish_expr_stmt (t);
661 else
663 tree fields;
664 int cvquals = cp_type_quals (TREE_TYPE (parm));
665 int i;
666 tree binfo, base_binfo;
668 /* Assign to each of the direct base classes. */
669 for (binfo = TYPE_BINFO (current_class_type), i = 0;
670 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
672 tree converted_parm;
673 vec<tree, va_gc> *parmvec;
675 /* We must convert PARM directly to the base class
676 explicitly since the base class may be ambiguous. */
677 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
678 tf_warning_or_error);
679 if (move_p)
680 converted_parm = move (converted_parm);
681 /* Call the base class assignment operator. */
682 parmvec = make_tree_vector_single (converted_parm);
683 finish_expr_stmt
684 (build_special_member_call (current_class_ref,
685 ansi_assopname (NOP_EXPR),
686 &parmvec,
687 base_binfo,
688 flags,
689 tf_warning_or_error));
690 release_tree_vector (parmvec);
693 /* Assign to each of the non-static data members. */
694 for (fields = TYPE_FIELDS (current_class_type);
695 fields;
696 fields = DECL_CHAIN (fields))
698 tree comp = current_class_ref;
699 tree init = parm;
700 tree field = fields;
701 tree expr_type;
702 int quals;
704 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
705 continue;
707 expr_type = TREE_TYPE (field);
709 if (CP_TYPE_CONST_P (expr_type))
711 error ("non-static const member %q#D, can%'t use default "
712 "assignment operator", field);
713 continue;
715 else if (TREE_CODE (expr_type) == REFERENCE_TYPE)
717 error ("non-static reference member %q#D, can%'t use "
718 "default assignment operator", field);
719 continue;
722 if (DECL_NAME (field))
724 if (VFIELD_NAME_P (DECL_NAME (field)))
725 continue;
727 else if (ANON_AGGR_TYPE_P (expr_type)
728 && TYPE_FIELDS (expr_type) != NULL_TREE)
729 /* Just use the field; anonymous types can't have
730 nontrivial copy ctors or assignment ops or this
731 function would be deleted. */;
732 else
733 continue;
735 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
737 /* Compute the type of init->field */
738 quals = cvquals;
739 if (DECL_MUTABLE_P (field))
740 quals &= ~TYPE_QUAL_CONST;
741 expr_type = cp_build_qualified_type (expr_type, quals);
743 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
744 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
745 /* 'move' breaks bit-fields, and has no effect for scalars. */
746 && !scalarish_type_p (expr_type))
747 init = move (init);
749 if (DECL_NAME (field))
750 init = cp_build_modify_expr (comp, NOP_EXPR, init,
751 tf_warning_or_error);
752 else
753 init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
754 finish_expr_stmt (init);
757 finish_return_stmt (current_class_ref);
758 finish_compound_stmt (compound_stmt);
761 /* Synthesize FNDECL, a non-static member function. */
763 void
764 synthesize_method (tree fndecl)
766 bool nested = (current_function_decl != NULL_TREE);
767 tree context = decl_function_context (fndecl);
768 bool need_body = true;
769 tree stmt;
770 location_t save_input_location = input_location;
771 int error_count = errorcount;
772 int warning_count = warningcount + werrorcount;
774 /* Reset the source location, we might have been previously
775 deferred, and thus have saved where we were first needed. */
776 DECL_SOURCE_LOCATION (fndecl)
777 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
779 /* If we've been asked to synthesize a clone, just synthesize the
780 cloned function instead. Doing so will automatically fill in the
781 body for the clone. */
782 if (DECL_CLONED_FUNCTION_P (fndecl))
783 fndecl = DECL_CLONED_FUNCTION (fndecl);
785 /* We may be in the middle of deferred access check. Disable
786 it now. */
787 push_deferring_access_checks (dk_no_deferred);
789 if (! context)
790 push_to_top_level ();
791 else if (nested)
792 push_function_context ();
794 input_location = DECL_SOURCE_LOCATION (fndecl);
796 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
797 stmt = begin_function_body ();
799 if (DECL_OVERLOADED_OPERATOR_P (fndecl) == NOP_EXPR)
801 do_build_copy_assign (fndecl);
802 need_body = false;
804 else if (DECL_CONSTRUCTOR_P (fndecl))
806 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
807 if (arg_chain != void_list_node)
808 do_build_copy_constructor (fndecl);
809 else
810 finish_mem_initializers (NULL_TREE);
813 /* If we haven't yet generated the body of the function, just
814 generate an empty compound statement. */
815 if (need_body)
817 tree compound_stmt;
818 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
819 finish_compound_stmt (compound_stmt);
822 finish_function_body (stmt);
823 expand_or_defer_fn (finish_function (0));
825 input_location = save_input_location;
827 if (! context)
828 pop_from_top_level ();
829 else if (nested)
830 pop_function_context ();
832 pop_deferring_access_checks ();
834 if (error_count != errorcount || warning_count != warningcount + werrorcount)
835 inform (input_location, "synthesized method %qD first required here ",
836 fndecl);
839 /* Build a reference to type TYPE with cv-quals QUALS, which is an
840 rvalue if RVALUE is true. */
842 static tree
843 build_stub_type (tree type, int quals, bool rvalue)
845 tree argtype = cp_build_qualified_type (type, quals);
846 return cp_build_reference_type (argtype, rvalue);
849 /* Build a dummy glvalue from dereferencing a dummy reference of type
850 REFTYPE. */
852 static tree
853 build_stub_object (tree reftype)
855 if (TREE_CODE (reftype) != REFERENCE_TYPE)
856 reftype = cp_build_reference_type (reftype, /*rval*/true);
857 tree stub = build1 (CONVERT_EXPR, reftype, integer_one_node);
858 return convert_from_reference (stub);
861 /* Determine which function will be called when looking up NAME in TYPE,
862 called with a single ARGTYPE argument, or no argument if ARGTYPE is
863 null. FLAGS and COMPLAIN are as for build_new_method_call.
865 Returns a FUNCTION_DECL if all is well.
866 Returns NULL_TREE if overload resolution failed.
867 Returns error_mark_node if the chosen function cannot be called. */
869 static tree
870 locate_fn_flags (tree type, tree name, tree argtype, int flags,
871 tsubst_flags_t complain)
873 tree ob, fn, fns, binfo, rval;
874 vec<tree, va_gc> *args;
876 if (TYPE_P (type))
877 binfo = TYPE_BINFO (type);
878 else
880 binfo = type;
881 type = BINFO_TYPE (binfo);
884 ob = build_stub_object (cp_build_reference_type (type, false));
885 args = make_tree_vector ();
886 if (argtype)
888 if (TREE_CODE (argtype) == TREE_LIST)
890 for (tree elt = argtype; elt != void_list_node;
891 elt = TREE_CHAIN (elt))
893 tree type = TREE_VALUE (elt);
894 tree arg = build_stub_object (type);
895 vec_safe_push (args, arg);
898 else
900 tree arg = build_stub_object (argtype);
901 args->quick_push (arg);
905 fns = lookup_fnfields (binfo, name, 0);
906 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
908 release_tree_vector (args);
909 if (fn && rval == error_mark_node)
910 return rval;
911 else
912 return fn;
915 /* Locate the dtor of TYPE. */
917 tree
918 get_dtor (tree type, tsubst_flags_t complain)
920 tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
921 LOOKUP_NORMAL, complain);
922 if (fn == error_mark_node)
923 return NULL_TREE;
924 return fn;
927 /* Locate the default ctor of TYPE. */
929 tree
930 locate_ctor (tree type)
932 tree fn;
934 push_deferring_access_checks (dk_no_check);
935 fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
936 LOOKUP_SPECULATIVE, tf_none);
937 pop_deferring_access_checks ();
938 if (fn == error_mark_node)
939 return NULL_TREE;
940 return fn;
943 /* Likewise, but give any appropriate errors. */
945 tree
946 get_default_ctor (tree type)
948 tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
949 LOOKUP_NORMAL, tf_warning_or_error);
950 if (fn == error_mark_node)
951 return NULL_TREE;
952 return fn;
955 /* Locate the copy ctor of TYPE. */
957 tree
958 get_copy_ctor (tree type, tsubst_flags_t complain)
960 int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
961 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
962 tree argtype = build_stub_type (type, quals, false);
963 tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
964 LOOKUP_NORMAL, complain);
965 if (fn == error_mark_node)
966 return NULL_TREE;
967 return fn;
970 /* Locate the copy assignment operator of TYPE. */
972 tree
973 get_copy_assign (tree type)
975 int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
976 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
977 tree argtype = build_stub_type (type, quals, false);
978 tree fn = locate_fn_flags (type, ansi_assopname (NOP_EXPR), argtype,
979 LOOKUP_NORMAL, tf_warning_or_error);
980 if (fn == error_mark_node)
981 return NULL_TREE;
982 return fn;
985 /* Locate the inherited constructor of constructor CTOR. */
987 tree
988 get_inherited_ctor (tree ctor)
990 gcc_assert (DECL_INHERITED_CTOR_BASE (ctor));
992 push_deferring_access_checks (dk_no_check);
993 tree fn = locate_fn_flags (DECL_INHERITED_CTOR_BASE (ctor),
994 complete_ctor_identifier,
995 FUNCTION_FIRST_USER_PARMTYPE (ctor),
996 LOOKUP_NORMAL|LOOKUP_SPECULATIVE,
997 tf_none);
998 pop_deferring_access_checks ();
999 if (fn == error_mark_node)
1000 return NULL_TREE;
1001 return fn;
1004 /* walk_tree helper function for is_trivially_xible. If *TP is a call,
1005 return it if it calls something other than a trivial special member
1006 function. */
1008 static tree
1009 check_nontriv (tree *tp, int *, void *)
1011 tree fn;
1012 if (TREE_CODE (*tp) == CALL_EXPR)
1013 fn = CALL_EXPR_FN (*tp);
1014 else if (TREE_CODE (*tp) == AGGR_INIT_EXPR)
1015 fn = AGGR_INIT_EXPR_FN (*tp);
1016 else
1017 return NULL_TREE;
1019 if (TREE_CODE (fn) == ADDR_EXPR)
1020 fn = TREE_OPERAND (fn, 0);
1022 if (TREE_CODE (fn) != FUNCTION_DECL
1023 || !trivial_fn_p (fn))
1024 return fn;
1025 return NULL_TREE;
1028 /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */
1030 static tree
1031 assignable_expr (tree to, tree from)
1033 ++cp_unevaluated_operand;
1034 to = build_stub_object (to);
1035 from = build_stub_object (from);
1036 tree r = cp_build_modify_expr (to, NOP_EXPR, from, tf_none);
1037 --cp_unevaluated_operand;
1038 return r;
1041 /* The predicate condition for a template specialization
1042 is_constructible<T, Args...> shall be satisfied if and only if the
1043 following variable definition would be well-formed for some invented
1044 variable t: T t(create<Args>()...);
1046 Return something equivalent in well-formedness and triviality. */
1048 static tree
1049 constructible_expr (tree to, tree from)
1051 tree expr;
1052 if (CLASS_TYPE_P (to))
1054 tree ctype = to;
1055 vec<tree, va_gc> *args = NULL;
1056 if (TREE_CODE (to) != REFERENCE_TYPE)
1057 to = cp_build_reference_type (to, /*rval*/false);
1058 tree ob = build_stub_object (to);
1059 for (; from; from = TREE_CHAIN (from))
1060 vec_safe_push (args, build_stub_object (TREE_VALUE (from)));
1061 expr = build_special_member_call (ob, complete_ctor_identifier, &args,
1062 ctype, LOOKUP_NORMAL, tf_none);
1063 if (expr == error_mark_node)
1064 return error_mark_node;
1065 /* The current state of the standard vis-a-vis LWG 2116 is that
1066 is_*constructible involves destruction as well. */
1067 if (type_build_dtor_call (ctype))
1069 tree dtor = build_special_member_call (ob, complete_dtor_identifier,
1070 NULL, ctype, LOOKUP_NORMAL,
1071 tf_none);
1072 if (dtor == error_mark_node)
1073 return error_mark_node;
1074 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
1075 expr = build2 (COMPOUND_EXPR, void_type_node, expr, dtor);
1078 else
1080 if (from == NULL_TREE)
1081 return build_value_init (to, tf_none);
1082 else if (TREE_CHAIN (from))
1083 return error_mark_node; // too many initializers
1084 from = build_stub_object (TREE_VALUE (from));
1085 expr = perform_direct_initialization_if_possible (to, from,
1086 /*cast*/false,
1087 tf_none);
1089 return expr;
1092 /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or
1093 constructible (otherwise) from FROM, which is a single type for
1094 assignment or a list of types for construction. */
1096 bool
1097 is_trivially_xible (enum tree_code code, tree to, tree from)
1099 tree expr;
1100 if (code == MODIFY_EXPR)
1101 expr = assignable_expr (to, from);
1102 else if (from && TREE_CHAIN (from))
1103 return false; // only 0- and 1-argument ctors can be trivial
1104 else
1105 expr = constructible_expr (to, from);
1107 if (expr == error_mark_node)
1108 return false;
1109 tree nt = cp_walk_tree_without_duplicates (&expr, check_nontriv, NULL);
1110 return !nt;
1113 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
1114 DELETED_P or give an error message MSG with argument ARG. */
1116 static void
1117 process_subob_fn (tree fn, tree *spec_p, bool *trivial_p,
1118 bool *deleted_p, bool *constexpr_p,
1119 bool diag, tree arg)
1121 if (!fn || fn == error_mark_node)
1122 goto bad;
1124 if (spec_p)
1126 maybe_instantiate_noexcept (fn);
1127 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
1128 *spec_p = merge_exception_specifiers (*spec_p, raises);
1131 if (!trivial_fn_p (fn))
1133 if (trivial_p)
1134 *trivial_p = false;
1135 if (TREE_CODE (arg) == FIELD_DECL
1136 && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
1138 if (deleted_p)
1139 *deleted_p = true;
1140 if (diag)
1141 error ("union member %q+D with non-trivial %qD", arg, fn);
1145 if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
1147 *constexpr_p = false;
1148 if (diag)
1150 inform (0, "defaulted constructor calls non-constexpr "
1151 "%q+D", fn);
1152 explain_invalid_constexpr_fn (fn);
1156 return;
1158 bad:
1159 if (deleted_p)
1160 *deleted_p = true;
1163 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
1164 aggregates. */
1166 static void
1167 walk_field_subobs (tree fields, tree fnname, special_function_kind sfk,
1168 int quals, bool copy_arg_p, bool move_p,
1169 bool assign_p, tree *spec_p, bool *trivial_p,
1170 bool *deleted_p, bool *constexpr_p,
1171 bool diag, int flags, tsubst_flags_t complain)
1173 tree field;
1174 for (field = fields; field; field = DECL_CHAIN (field))
1176 tree mem_type, argtype, rval;
1178 if (TREE_CODE (field) != FIELD_DECL
1179 || DECL_ARTIFICIAL (field))
1180 continue;
1182 mem_type = strip_array_types (TREE_TYPE (field));
1183 if (assign_p)
1185 bool bad = true;
1186 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
1188 if (diag)
1189 error ("non-static const member %q#D, can%'t use default "
1190 "assignment operator", field);
1192 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1194 if (diag)
1195 error ("non-static reference member %q#D, can%'t use "
1196 "default assignment operator", field);
1198 else
1199 bad = false;
1201 if (bad && deleted_p)
1202 *deleted_p = true;
1204 else if (sfk == sfk_constructor)
1206 bool bad;
1208 if (DECL_INITIAL (field))
1210 if (diag && DECL_INITIAL (field) == error_mark_node)
1211 inform (0, "initializer for %q+#D is invalid", field);
1212 if (trivial_p)
1213 *trivial_p = false;
1214 /* Core 1351: If the field has an NSDMI that could throw, the
1215 default constructor is noexcept(false). */
1216 if (spec_p)
1218 tree nsdmi = get_nsdmi (field, /*ctor*/false);
1219 if (!expr_noexcept_p (nsdmi, complain))
1220 *spec_p = noexcept_false_spec;
1222 /* Don't do the normal processing. */
1223 continue;
1226 bad = false;
1227 if (CP_TYPE_CONST_P (mem_type)
1228 && default_init_uninitialized_part (mem_type))
1230 if (diag)
1232 error ("uninitialized const member in %q#T",
1233 current_class_type);
1234 inform (DECL_SOURCE_LOCATION (field),
1235 "%q#D should be initialized", field);
1237 bad = true;
1239 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1241 if (diag)
1243 error ("uninitialized reference member in %q#T",
1244 current_class_type);
1245 inform (DECL_SOURCE_LOCATION (field),
1246 "%q#D should be initialized", field);
1248 bad = true;
1251 if (bad && deleted_p)
1252 *deleted_p = true;
1254 /* For an implicitly-defined default constructor to be constexpr,
1255 every member must have a user-provided default constructor or
1256 an explicit initializer. */
1257 if (constexpr_p && !CLASS_TYPE_P (mem_type)
1258 && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE)
1260 *constexpr_p = false;
1261 if (diag)
1262 inform (0, "defaulted default constructor does not "
1263 "initialize %q+#D", field);
1266 else if (sfk == sfk_copy_constructor)
1268 /* 12.8p11b5 */
1269 if (TREE_CODE (mem_type) == REFERENCE_TYPE
1270 && TYPE_REF_IS_RVALUE (mem_type))
1272 if (diag)
1273 error ("copying non-static data member %q#D of rvalue "
1274 "reference type", field);
1275 if (deleted_p)
1276 *deleted_p = true;
1280 if (!CLASS_TYPE_P (mem_type))
1281 continue;
1283 if (ANON_AGGR_TYPE_P (mem_type))
1285 walk_field_subobs (TYPE_FIELDS (mem_type), fnname, sfk, quals,
1286 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1287 deleted_p, constexpr_p,
1288 diag, flags, complain);
1289 continue;
1292 if (copy_arg_p)
1294 int mem_quals = cp_type_quals (mem_type) | quals;
1295 if (DECL_MUTABLE_P (field))
1296 mem_quals &= ~TYPE_QUAL_CONST;
1297 argtype = build_stub_type (mem_type, mem_quals, move_p);
1299 else
1300 argtype = NULL_TREE;
1302 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
1304 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1305 constexpr_p, diag, field);
1309 /* The caller wants to generate an implicit declaration of SFK for CTYPE
1310 which is const if relevant and CONST_P is set. If spec_p, trivial_p and
1311 deleted_p are non-null, set their referent appropriately. If diag is
1312 true, we're either being called from maybe_explain_implicit_delete to
1313 give errors, or if constexpr_p is non-null, from
1314 explain_invalid_constexpr_fn. */
1316 static void
1317 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
1318 tree *spec_p, bool *trivial_p, bool *deleted_p,
1319 bool *constexpr_p, bool diag,
1320 tree inherited_base, tree inherited_parms)
1322 tree binfo, base_binfo, scope, fnname, rval, argtype;
1323 bool move_p, copy_arg_p, assign_p, expected_trivial, check_vdtor;
1324 vec<tree, va_gc> *vbases;
1325 int i, quals, flags;
1326 tsubst_flags_t complain;
1327 bool ctor_p;
1329 if (spec_p)
1330 *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec);
1332 if (deleted_p)
1334 /* "The closure type associated with a lambda-expression has a deleted
1335 default constructor and a deleted copy assignment operator."
1336 This is diagnosed in maybe_explain_implicit_delete. */
1337 if (LAMBDA_TYPE_P (ctype)
1338 && (sfk == sfk_constructor
1339 || sfk == sfk_copy_assignment))
1341 *deleted_p = true;
1342 return;
1345 *deleted_p = false;
1348 ctor_p = false;
1349 assign_p = false;
1350 check_vdtor = false;
1351 switch (sfk)
1353 case sfk_move_assignment:
1354 case sfk_copy_assignment:
1355 assign_p = true;
1356 fnname = ansi_assopname (NOP_EXPR);
1357 break;
1359 case sfk_destructor:
1360 check_vdtor = true;
1361 /* The synthesized method will call base dtors, but check complete
1362 here to avoid having to deal with VTT. */
1363 fnname = complete_dtor_identifier;
1364 break;
1366 case sfk_constructor:
1367 case sfk_move_constructor:
1368 case sfk_copy_constructor:
1369 case sfk_inheriting_constructor:
1370 ctor_p = true;
1371 fnname = complete_ctor_identifier;
1372 break;
1374 default:
1375 gcc_unreachable ();
1378 gcc_assert ((sfk == sfk_inheriting_constructor)
1379 == (inherited_base != NULL_TREE));
1381 /* If that user-written default constructor would satisfy the
1382 requirements of a constexpr constructor (7.1.5), the
1383 implicitly-defined default constructor is constexpr. */
1384 if (constexpr_p)
1385 *constexpr_p = ctor_p;
1387 move_p = false;
1388 switch (sfk)
1390 case sfk_constructor:
1391 case sfk_destructor:
1392 case sfk_inheriting_constructor:
1393 copy_arg_p = false;
1394 break;
1396 case sfk_move_constructor:
1397 case sfk_move_assignment:
1398 move_p = true;
1399 case sfk_copy_constructor:
1400 case sfk_copy_assignment:
1401 copy_arg_p = true;
1402 break;
1404 default:
1405 gcc_unreachable ();
1408 expected_trivial = type_has_trivial_fn (ctype, sfk);
1409 if (trivial_p)
1410 *trivial_p = expected_trivial;
1412 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
1413 class versions and other properties of the type. But a subobject
1414 class can be trivially copyable and yet have overload resolution
1415 choose a template constructor for initialization, depending on
1416 rvalueness and cv-quals. And furthermore, a member in a base might
1417 be trivial but deleted or otherwise not callable. So we can't exit
1418 early in C++0x. The same considerations apply in C++98/03, but
1419 there the definition of triviality does not consider overload
1420 resolution, so a constructor can be trivial even if it would otherwise
1421 call a non-trivial constructor. */
1422 if (expected_trivial
1423 && (!copy_arg_p || cxx_dialect < cxx11))
1425 if (constexpr_p && sfk == sfk_constructor)
1427 bool cx = trivial_default_constructor_is_constexpr (ctype);
1428 *constexpr_p = cx;
1429 if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
1430 /* A trivial constructor doesn't have any NSDMI. */
1431 inform (input_location, "defaulted default constructor does "
1432 "not initialize any non-static data member");
1434 if (!diag && cxx_dialect < cxx11)
1435 return;
1438 ++cp_unevaluated_operand;
1439 ++c_inhibit_evaluation_warnings;
1440 push_deferring_access_checks (dk_no_deferred);
1442 scope = push_scope (ctype);
1444 flags = LOOKUP_NORMAL|LOOKUP_SPECULATIVE;
1445 if (!inherited_base)
1446 flags |= LOOKUP_DEFAULTED;
1448 complain = diag ? tf_warning_or_error : tf_none;
1450 if (const_p)
1451 quals = TYPE_QUAL_CONST;
1452 else
1453 quals = TYPE_UNQUALIFIED;
1454 argtype = NULL_TREE;
1456 for (binfo = TYPE_BINFO (ctype), i = 0;
1457 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
1459 tree basetype = BINFO_TYPE (base_binfo);
1461 if (!assign_p && BINFO_VIRTUAL_P (base_binfo))
1462 /* We'll handle virtual bases below. */
1463 continue;
1465 if (copy_arg_p)
1466 argtype = build_stub_type (basetype, quals, move_p);
1467 else if (basetype == inherited_base)
1468 argtype = inherited_parms;
1469 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1470 if (inherited_base)
1471 argtype = NULL_TREE;
1473 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1474 constexpr_p, diag, basetype);
1475 if (ctor_p)
1477 /* In a constructor we also need to check the subobject
1478 destructors for cleanup of partially constructed objects. */
1479 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1480 NULL_TREE, flags, complain);
1481 /* Note that we don't pass down trivial_p; the subobject
1482 destructors don't affect triviality of the constructor. Nor
1483 do they affect constexpr-ness (a constant expression doesn't
1484 throw) or exception-specification (a throw from one of the
1485 dtors would be a double-fault). */
1486 process_subob_fn (rval, NULL, NULL,
1487 deleted_p, NULL, false,
1488 basetype);
1491 if (check_vdtor && type_has_virtual_destructor (basetype))
1493 rval = locate_fn_flags (ctype, ansi_opname (DELETE_EXPR),
1494 ptr_type_node, flags, complain);
1495 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
1496 to have a null rval (no class-specific op delete). */
1497 if (rval && rval == error_mark_node && deleted_p)
1498 *deleted_p = true;
1499 check_vdtor = false;
1502 if (diag && assign_p && move_p
1503 && BINFO_VIRTUAL_P (base_binfo)
1504 && rval && TREE_CODE (rval) == FUNCTION_DECL
1505 && move_fn_p (rval) && !trivial_fn_p (rval)
1506 && vbase_has_user_provided_move_assign (basetype))
1507 warning (OPT_Wvirtual_move_assign,
1508 "defaulted move assignment for %qT calls a non-trivial "
1509 "move assignment operator for virtual base %qT",
1510 ctype, basetype);
1513 vbases = CLASSTYPE_VBASECLASSES (ctype);
1514 if (vec_safe_is_empty (vbases))
1515 /* No virtual bases to worry about. */;
1516 else if (!assign_p)
1518 if (constexpr_p)
1519 *constexpr_p = false;
1520 FOR_EACH_VEC_ELT (*vbases, i, base_binfo)
1522 tree basetype = BINFO_TYPE (base_binfo);
1523 if (copy_arg_p)
1524 argtype = build_stub_type (basetype, quals, move_p);
1525 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1527 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1528 constexpr_p, diag, basetype);
1529 if (ctor_p && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype))
1531 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1532 NULL_TREE, flags, complain);
1533 process_subob_fn (rval, NULL, NULL,
1534 deleted_p, NULL, false,
1535 basetype);
1540 /* Now handle the non-static data members. */
1541 walk_field_subobs (TYPE_FIELDS (ctype), fnname, sfk, quals,
1542 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1543 deleted_p, constexpr_p,
1544 diag, flags, complain);
1545 if (ctor_p)
1546 walk_field_subobs (TYPE_FIELDS (ctype), complete_dtor_identifier,
1547 sfk_destructor, TYPE_UNQUALIFIED, false,
1548 false, false, NULL, NULL,
1549 deleted_p, NULL,
1550 false, flags, complain);
1552 pop_scope (scope);
1554 pop_deferring_access_checks ();
1555 --cp_unevaluated_operand;
1556 --c_inhibit_evaluation_warnings;
1559 /* DECL is a defaulted function whose exception specification is now
1560 needed. Return what it should be. */
1562 tree
1563 get_defaulted_eh_spec (tree decl)
1565 if (DECL_CLONED_FUNCTION_P (decl))
1566 decl = DECL_CLONED_FUNCTION (decl);
1567 special_function_kind sfk = special_function_p (decl);
1568 tree ctype = DECL_CONTEXT (decl);
1569 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1570 tree parm_type = TREE_VALUE (parms);
1571 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1572 tree spec = empty_except_spec;
1573 synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL,
1574 NULL, false, DECL_INHERITED_CTOR_BASE (decl),
1575 parms);
1576 return spec;
1579 /* DECL is a deleted function. If it's implicitly deleted, explain why and
1580 return true; else return false. */
1582 bool
1583 maybe_explain_implicit_delete (tree decl)
1585 /* If decl is a clone, get the primary variant. */
1586 decl = DECL_ORIGIN (decl);
1587 gcc_assert (DECL_DELETED_FN (decl));
1588 if (DECL_DEFAULTED_FN (decl))
1590 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
1591 static hash_set<tree> *explained;
1593 special_function_kind sfk;
1594 location_t loc;
1595 bool informed;
1596 tree ctype;
1598 if (!explained)
1599 explained = new hash_set<tree>;
1600 if (explained->add (decl))
1601 return true;
1603 sfk = special_function_p (decl);
1604 ctype = DECL_CONTEXT (decl);
1605 loc = input_location;
1606 input_location = DECL_SOURCE_LOCATION (decl);
1608 informed = false;
1609 if (LAMBDA_TYPE_P (ctype))
1611 informed = true;
1612 if (sfk == sfk_constructor)
1613 inform (DECL_SOURCE_LOCATION (decl),
1614 "a lambda closure type has a deleted default constructor");
1615 else if (sfk == sfk_copy_assignment)
1616 inform (DECL_SOURCE_LOCATION (decl),
1617 "a lambda closure type has a deleted copy assignment operator");
1618 else
1619 informed = false;
1621 else if (DECL_ARTIFICIAL (decl)
1622 && (sfk == sfk_copy_assignment
1623 || sfk == sfk_copy_constructor)
1624 && (type_has_user_declared_move_constructor (ctype)
1625 || type_has_user_declared_move_assign (ctype)))
1627 inform (0, "%q+#D is implicitly declared as deleted because %qT "
1628 "declares a move constructor or move assignment operator",
1629 decl, ctype);
1630 informed = true;
1632 if (!informed)
1634 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1635 tree parm_type = TREE_VALUE (parms);
1636 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1637 tree raises = NULL_TREE;
1638 bool deleted_p = false;
1639 tree scope = push_scope (ctype);
1641 synthesized_method_walk (ctype, sfk, const_p,
1642 &raises, NULL, &deleted_p, NULL, false,
1643 DECL_INHERITED_CTOR_BASE (decl), parms);
1644 if (deleted_p)
1646 inform (0, "%q+#D is implicitly deleted because the default "
1647 "definition would be ill-formed:", decl);
1648 synthesized_method_walk (ctype, sfk, const_p,
1649 NULL, NULL, NULL, NULL, true,
1650 DECL_INHERITED_CTOR_BASE (decl), parms);
1652 else if (!comp_except_specs
1653 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
1654 raises, ce_normal))
1655 inform (DECL_SOURCE_LOCATION (decl), "%q#F is implicitly "
1656 "deleted because its exception-specification does not "
1657 "match the implicit exception-specification %qX",
1658 decl, raises);
1659 #ifdef ENABLE_CHECKING
1660 else
1661 gcc_unreachable ();
1662 #endif
1664 pop_scope (scope);
1667 input_location = loc;
1668 return true;
1670 return false;
1673 /* DECL is a defaulted function which was declared constexpr. Explain why
1674 it can't be constexpr. */
1676 void
1677 explain_implicit_non_constexpr (tree decl)
1679 tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl));
1680 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1681 bool dummy;
1682 synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
1683 special_function_p (decl), const_p,
1684 NULL, NULL, NULL, &dummy, true,
1685 DECL_INHERITED_CTOR_BASE (decl),
1686 FUNCTION_FIRST_USER_PARMTYPE (decl));
1689 /* DECL is an instantiation of an inheriting constructor template. Deduce
1690 the correct exception-specification and deletedness for this particular
1691 specialization. */
1693 void
1694 deduce_inheriting_ctor (tree decl)
1696 gcc_assert (DECL_INHERITED_CTOR_BASE (decl));
1697 tree spec;
1698 bool trivial, constexpr_, deleted;
1699 synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor,
1700 false, &spec, &trivial, &deleted, &constexpr_,
1701 /*diag*/false,
1702 DECL_INHERITED_CTOR_BASE (decl),
1703 FUNCTION_FIRST_USER_PARMTYPE (decl));
1704 DECL_DELETED_FN (decl) = deleted;
1705 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
1708 /* Implicitly declare the special function indicated by KIND, as a
1709 member of TYPE. For copy constructors and assignment operators,
1710 CONST_P indicates whether these functions should take a const
1711 reference argument or a non-const reference. Returns the
1712 FUNCTION_DECL for the implicitly declared function. */
1714 tree
1715 implicitly_declare_fn (special_function_kind kind, tree type,
1716 bool const_p, tree inherited_ctor,
1717 tree inherited_parms)
1719 tree fn;
1720 tree parameter_types = void_list_node;
1721 tree return_type;
1722 tree fn_type;
1723 tree raises = empty_except_spec;
1724 tree rhs_parm_type = NULL_TREE;
1725 tree this_parm;
1726 tree name;
1727 HOST_WIDE_INT saved_processing_template_decl;
1728 bool deleted_p;
1729 bool constexpr_p;
1731 /* Because we create declarations for implicitly declared functions
1732 lazily, we may be creating the declaration for a member of TYPE
1733 while in some completely different context. However, TYPE will
1734 never be a dependent class (because we never want to do lookups
1735 for implicitly defined functions in a dependent class).
1736 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
1737 because we only create clones for constructors and destructors
1738 when not in a template. */
1739 gcc_assert (!dependent_type_p (type));
1740 saved_processing_template_decl = processing_template_decl;
1741 processing_template_decl = 0;
1743 type = TYPE_MAIN_VARIANT (type);
1745 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (type))
1747 if (kind == sfk_destructor)
1748 /* See comment in check_special_function_return_type. */
1749 return_type = build_pointer_type (void_type_node);
1750 else
1751 return_type = build_pointer_type (type);
1753 else
1754 return_type = void_type_node;
1756 switch (kind)
1758 case sfk_destructor:
1759 /* Destructor. */
1760 name = constructor_name (type);
1761 break;
1763 case sfk_constructor:
1764 /* Default constructor. */
1765 name = constructor_name (type);
1766 break;
1768 case sfk_copy_constructor:
1769 case sfk_copy_assignment:
1770 case sfk_move_constructor:
1771 case sfk_move_assignment:
1772 case sfk_inheriting_constructor:
1774 bool move_p;
1775 if (kind == sfk_copy_assignment
1776 || kind == sfk_move_assignment)
1778 return_type = build_reference_type (type);
1779 name = ansi_assopname (NOP_EXPR);
1781 else
1782 name = constructor_name (type);
1784 if (kind == sfk_inheriting_constructor)
1785 parameter_types = inherited_parms;
1786 else
1788 if (const_p)
1789 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
1790 else
1791 rhs_parm_type = type;
1792 move_p = (kind == sfk_move_assignment
1793 || kind == sfk_move_constructor);
1794 rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
1796 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
1798 break;
1800 default:
1801 gcc_unreachable ();
1804 tree inherited_base = (inherited_ctor
1805 ? DECL_CONTEXT (inherited_ctor)
1806 : NULL_TREE);
1807 bool trivial_p = false;
1809 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
1811 /* For an inheriting constructor template, just copy these flags from
1812 the inherited constructor template for now. */
1813 raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor));
1814 deleted_p = DECL_DELETED_FN (inherited_ctor);
1815 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
1817 else if (cxx_dialect >= cxx11)
1819 raises = unevaluated_noexcept_spec ();
1820 synthesized_method_walk (type, kind, const_p, NULL, &trivial_p,
1821 &deleted_p, &constexpr_p, false,
1822 inherited_base, inherited_parms);
1824 else
1825 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
1826 &deleted_p, &constexpr_p, false,
1827 inherited_base, inherited_parms);
1828 /* Don't bother marking a deleted constructor as constexpr. */
1829 if (deleted_p)
1830 constexpr_p = false;
1831 /* A trivial copy/move constructor is also a constexpr constructor,
1832 unless the class has virtual bases (7.1.5p4). */
1833 else if (trivial_p && cxx_dialect >= cxx11
1834 && (kind == sfk_copy_constructor
1835 || kind == sfk_move_constructor)
1836 && !CLASSTYPE_VBASECLASSES (type))
1837 gcc_assert (constexpr_p);
1839 if (!trivial_p && type_has_trivial_fn (type, kind))
1840 type_set_nontrivial_flag (type, kind);
1842 /* Create the function. */
1843 fn_type = build_method_type_directly (type, return_type, parameter_types);
1844 if (raises)
1845 fn_type = build_exception_variant (fn_type, raises);
1846 fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
1847 if (kind != sfk_inheriting_constructor)
1848 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
1849 if (kind == sfk_constructor || kind == sfk_copy_constructor
1850 || kind == sfk_move_constructor || kind == sfk_inheriting_constructor)
1851 DECL_CONSTRUCTOR_P (fn) = 1;
1852 else if (kind == sfk_destructor)
1853 DECL_DESTRUCTOR_P (fn) = 1;
1854 else
1856 DECL_ASSIGNMENT_OPERATOR_P (fn) = 1;
1857 SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR);
1860 /* If pointers to member functions use the least significant bit to
1861 indicate whether a function is virtual, ensure a pointer
1862 to this function will have that bit clear. */
1863 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
1864 && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
1865 DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
1867 /* Create the explicit arguments. */
1868 if (rhs_parm_type)
1870 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
1871 want its type to be included in the mangled function
1872 name. */
1873 tree decl = cp_build_parm_decl (NULL_TREE, rhs_parm_type);
1874 TREE_READONLY (decl) = 1;
1875 retrofit_lang_decl (decl);
1876 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
1877 DECL_ARGUMENTS (fn) = decl;
1879 else if (kind == sfk_inheriting_constructor)
1881 tree *p = &DECL_ARGUMENTS (fn);
1882 int index = 1;
1883 for (tree parm = inherited_parms; parm != void_list_node;
1884 parm = TREE_CHAIN (parm))
1886 *p = cp_build_parm_decl (NULL_TREE, TREE_VALUE (parm));
1887 retrofit_lang_decl (*p);
1888 DECL_PARM_LEVEL (*p) = 1;
1889 DECL_PARM_INDEX (*p) = index++;
1890 DECL_CONTEXT (*p) = fn;
1891 p = &DECL_CHAIN (*p);
1893 SET_DECL_INHERITED_CTOR_BASE (fn, inherited_base);
1894 DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor);
1895 /* A constructor so declared has the same access as the corresponding
1896 constructor in X. */
1897 TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor);
1898 TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor);
1899 /* Copy constexpr from the inherited constructor even if the
1900 inheriting constructor doesn't satisfy the requirements. */
1901 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
1903 /* Add the "this" parameter. */
1904 this_parm = build_this_parm (fn_type, TYPE_UNQUALIFIED);
1905 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
1906 DECL_ARGUMENTS (fn) = this_parm;
1908 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
1909 DECL_IN_AGGR_P (fn) = 1;
1910 DECL_ARTIFICIAL (fn) = 1;
1911 DECL_DEFAULTED_FN (fn) = 1;
1912 if (cxx_dialect >= cxx11)
1914 /* "The closure type associated with a lambda-expression has a deleted
1915 default constructor and a deleted copy assignment operator." */
1916 if ((kind == sfk_constructor
1917 || kind == sfk_copy_assignment)
1918 && LAMBDA_TYPE_P (type))
1919 deleted_p = true;
1920 DECL_DELETED_FN (fn) = deleted_p;
1921 DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
1923 DECL_EXTERNAL (fn) = true;
1924 DECL_NOT_REALLY_EXTERN (fn) = 1;
1925 DECL_DECLARED_INLINE_P (fn) = 1;
1926 DECL_COMDAT (fn) = 1;
1927 set_linkage_according_to_type (type, fn);
1928 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
1929 gcc_assert (!TREE_USED (fn));
1931 /* Restore PROCESSING_TEMPLATE_DECL. */
1932 processing_template_decl = saved_processing_template_decl;
1934 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
1935 fn = add_inherited_template_parms (fn, inherited_ctor);
1937 /* Warn about calling a non-trivial move assignment in a virtual base. */
1938 if (kind == sfk_move_assignment && !deleted_p && !trivial_p
1939 && CLASSTYPE_VBASECLASSES (type))
1941 location_t loc = input_location;
1942 input_location = DECL_SOURCE_LOCATION (fn);
1943 synthesized_method_walk (type, kind, const_p,
1944 NULL, NULL, NULL, NULL, true,
1945 NULL_TREE, NULL_TREE);
1946 input_location = loc;
1949 return fn;
1952 /* Gives any errors about defaulted functions which need to be deferred
1953 until the containing class is complete. */
1955 void
1956 defaulted_late_check (tree fn)
1958 /* Complain about invalid signature for defaulted fn. */
1959 tree ctx = DECL_CONTEXT (fn);
1960 special_function_kind kind = special_function_p (fn);
1961 bool fn_const_p = (copy_fn_p (fn) == 2);
1962 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
1963 NULL, NULL);
1964 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
1966 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
1967 TREE_TYPE (TREE_TYPE (implicit_fn)))
1968 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1969 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn))))
1971 error ("defaulted declaration %q+D", fn);
1972 error_at (DECL_SOURCE_LOCATION (fn),
1973 "does not match expected signature %qD", implicit_fn);
1976 /* 8.4.2/2: An explicitly-defaulted function (...) may have an explicit
1977 exception-specification only if it is compatible (15.4) with the
1978 exception-specification on the implicit declaration. If a function
1979 is explicitly defaulted on its first declaration, (...) it is
1980 implicitly considered to have the same exception-specification as if
1981 it had been implicitly declared. */
1982 maybe_instantiate_noexcept (fn);
1983 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
1984 if (!fn_spec)
1986 if (DECL_DEFAULTED_IN_CLASS_P (fn))
1987 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
1989 else if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
1990 /* Equivalent to the implicit spec. */;
1991 else if (DECL_DEFAULTED_IN_CLASS_P (fn)
1992 && !CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
1993 /* We can't compare an explicit exception-specification on a
1994 constructor defaulted in the class body to the implicit
1995 exception-specification until after we've parsed any NSDMI; see
1996 after_nsdmi_defaulted_late_checks. */;
1997 else
1999 tree eh_spec = get_defaulted_eh_spec (fn);
2000 if (!comp_except_specs (fn_spec, eh_spec, ce_normal))
2002 if (DECL_DEFAULTED_IN_CLASS_P (fn))
2003 DECL_DELETED_FN (fn) = true;
2004 else
2005 error ("function %q+D defaulted on its redeclaration "
2006 "with an exception-specification that differs from "
2007 "the implicit exception-specification %qX", fn, eh_spec);
2011 if (DECL_DEFAULTED_IN_CLASS_P (fn)
2012 && DECL_DECLARED_CONSTEXPR_P (implicit_fn))
2014 /* Hmm...should we do this for out-of-class too? Should it be OK to
2015 add constexpr later like inline, rather than requiring
2016 declarations to match? */
2017 DECL_DECLARED_CONSTEXPR_P (fn) = true;
2018 if (kind == sfk_constructor)
2019 TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
2022 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
2023 && DECL_DECLARED_CONSTEXPR_P (fn))
2025 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
2027 error ("explicitly defaulted function %q+D cannot be declared "
2028 "as constexpr because the implicit declaration is not "
2029 "constexpr:", fn);
2030 explain_implicit_non_constexpr (fn);
2032 DECL_DECLARED_CONSTEXPR_P (fn) = false;
2035 if (DECL_DELETED_FN (implicit_fn))
2036 DECL_DELETED_FN (fn) = 1;
2039 /* OK, we've parsed the NSDMI for class T, now we can check any explicit
2040 exception-specifications on functions defaulted in the class body. */
2042 void
2043 after_nsdmi_defaulted_late_checks (tree t)
2045 if (uses_template_parms (t))
2046 return;
2047 if (t == error_mark_node)
2048 return;
2049 for (tree fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
2050 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
2052 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2053 if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
2054 continue;
2056 tree eh_spec = get_defaulted_eh_spec (fn);
2057 if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)),
2058 eh_spec, ce_normal))
2059 DECL_DELETED_FN (fn) = true;
2063 /* Returns true iff FN can be explicitly defaulted, and gives any
2064 errors if defaulting FN is ill-formed. */
2066 bool
2067 defaultable_fn_check (tree fn)
2069 special_function_kind kind = sfk_none;
2071 if (template_parm_scope_p ())
2073 error ("a template cannot be defaulted");
2074 return false;
2077 if (DECL_CONSTRUCTOR_P (fn))
2079 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
2080 kind = sfk_constructor;
2081 else if (copy_fn_p (fn) > 0
2082 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
2083 == void_list_node))
2084 kind = sfk_copy_constructor;
2085 else if (move_fn_p (fn))
2086 kind = sfk_move_constructor;
2088 else if (DECL_DESTRUCTOR_P (fn))
2089 kind = sfk_destructor;
2090 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2091 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
2093 if (copy_fn_p (fn))
2094 kind = sfk_copy_assignment;
2095 else if (move_fn_p (fn))
2096 kind = sfk_move_assignment;
2099 if (kind == sfk_none)
2101 error ("%qD cannot be defaulted", fn);
2102 return false;
2104 else
2106 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
2107 t && t != void_list_node; t = TREE_CHAIN (t))
2108 if (TREE_PURPOSE (t))
2110 error ("defaulted function %q+D with default argument", fn);
2111 break;
2114 /* Avoid do_warn_unused_parameter warnings. */
2115 for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p))
2116 if (DECL_NAME (p))
2117 TREE_NO_WARNING (p) = 1;
2119 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn)))
2120 /* Defer checking. */;
2121 else if (!processing_template_decl)
2122 defaulted_late_check (fn);
2124 return true;
2128 /* Add an implicit declaration to TYPE for the kind of function
2129 indicated by SFK. Return the FUNCTION_DECL for the new implicit
2130 declaration. */
2132 tree
2133 lazily_declare_fn (special_function_kind sfk, tree type)
2135 tree fn;
2136 /* Whether or not the argument has a const reference type. */
2137 bool const_p = false;
2139 switch (sfk)
2141 case sfk_constructor:
2142 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
2143 break;
2144 case sfk_copy_constructor:
2145 const_p = TYPE_HAS_CONST_COPY_CTOR (type);
2146 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
2147 break;
2148 case sfk_move_constructor:
2149 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
2150 break;
2151 case sfk_copy_assignment:
2152 const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
2153 CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
2154 break;
2155 case sfk_move_assignment:
2156 CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
2157 break;
2158 case sfk_destructor:
2159 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
2160 break;
2161 default:
2162 gcc_unreachable ();
2165 /* Declare the function. */
2166 fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL);
2168 /* [class.copy]/8 If the class definition declares a move constructor or
2169 move assignment operator, the implicitly declared copy constructor is
2170 defined as deleted.... */
2171 if ((sfk == sfk_copy_assignment
2172 || sfk == sfk_copy_constructor)
2173 && (type_has_user_declared_move_constructor (type)
2174 || type_has_user_declared_move_assign (type)))
2175 DECL_DELETED_FN (fn) = true;
2177 /* A destructor may be virtual. */
2178 if (sfk == sfk_destructor
2179 || sfk == sfk_move_assignment
2180 || sfk == sfk_copy_assignment)
2181 check_for_override (fn, type);
2182 /* Add it to CLASSTYPE_METHOD_VEC. */
2183 add_method (type, fn, NULL_TREE);
2184 /* Add it to TYPE_METHODS. */
2185 if (sfk == sfk_destructor
2186 && DECL_VIRTUAL_P (fn))
2187 /* The ABI requires that a virtual destructor go at the end of the
2188 vtable. */
2189 TYPE_METHODS (type) = chainon (TYPE_METHODS (type), fn);
2190 else
2192 DECL_CHAIN (fn) = TYPE_METHODS (type);
2193 TYPE_METHODS (type) = fn;
2195 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
2196 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
2197 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
2198 /* Create appropriate clones. */
2199 clone_function_decl (fn, /*update_method_vec=*/true);
2201 return fn;
2204 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
2205 as there are artificial parms in FN. */
2207 tree
2208 skip_artificial_parms_for (const_tree fn, tree list)
2210 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2211 list = TREE_CHAIN (list);
2212 else
2213 return list;
2215 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2216 list = TREE_CHAIN (list);
2217 if (DECL_HAS_VTT_PARM_P (fn))
2218 list = TREE_CHAIN (list);
2219 return list;
2222 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
2223 artificial parms in FN. */
2226 num_artificial_parms_for (const_tree fn)
2228 int count = 0;
2230 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2231 count++;
2232 else
2233 return 0;
2235 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2236 count++;
2237 if (DECL_HAS_VTT_PARM_P (fn))
2238 count++;
2239 return count;
2243 #include "gt-cp-method.h"