* cgraph.h: Flatten. Remove all include files.
[official-gcc.git] / gcc / cp / method.c
blob10751ed6ba6564c6f357550eb3e7de2fd07d8af0
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 "hash-map.h"
39 #include "is-a.h"
40 #include "plugin-api.h"
41 #include "vec.h"
42 #include "hashtab.h"
43 #include "hash-set.h"
44 #include "machmode.h"
45 #include "hard-reg-set.h"
46 #include "input.h"
47 #include "function.h"
48 #include "ipa-ref.h"
49 #include "cgraph.h"
51 /* Various flags to control the mangling process. */
53 enum mangling_flags
55 /* No flags. */
56 mf_none = 0,
57 /* The thing we are presently mangling is part of a template type,
58 rather than a fully instantiated type. Therefore, we may see
59 complex expressions where we would normally expect to see a
60 simple integer constant. */
61 mf_maybe_uninstantiated = 1,
62 /* When mangling a numeric value, use the form `_XX_' (instead of
63 just `XX') if the value has more than one digit. */
64 mf_use_underscores_around_value = 2
67 typedef enum mangling_flags mangling_flags;
69 static void do_build_copy_assign (tree);
70 static void do_build_copy_constructor (tree);
71 static tree make_alias_for_thunk (tree);
73 /* Called once to initialize method.c. */
75 void
76 init_method (void)
78 init_mangle ();
81 /* Return a this or result adjusting thunk to FUNCTION. THIS_ADJUSTING
82 indicates whether it is a this or result adjusting thunk.
83 FIXED_OFFSET and VIRTUAL_OFFSET indicate how to do the adjustment
84 (see thunk_adjust). VIRTUAL_OFFSET can be NULL, but FIXED_OFFSET
85 never is. VIRTUAL_OFFSET is the /index/ into the vtable for this
86 adjusting thunks, we scale it to a byte offset. For covariant
87 thunks VIRTUAL_OFFSET is the virtual binfo. You must post process
88 the returned thunk with finish_thunk. */
90 tree
91 make_thunk (tree function, bool this_adjusting,
92 tree fixed_offset, tree virtual_offset)
94 HOST_WIDE_INT d;
95 tree thunk;
97 gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
98 /* We can have this thunks to covariant thunks, but not vice versa. */
99 gcc_assert (!DECL_THIS_THUNK_P (function));
100 gcc_assert (!DECL_RESULT_THUNK_P (function) || this_adjusting);
102 /* Scale the VIRTUAL_OFFSET to be in terms of bytes. */
103 if (this_adjusting && virtual_offset)
104 virtual_offset
105 = size_binop (MULT_EXPR,
106 virtual_offset,
107 convert (ssizetype,
108 TYPE_SIZE_UNIT (vtable_entry_type)));
110 d = tree_to_shwi (fixed_offset);
112 /* See if we already have the thunk in question. For this_adjusting
113 thunks VIRTUAL_OFFSET will be an INTEGER_CST, for covariant thunks it
114 will be a BINFO. */
115 for (thunk = DECL_THUNKS (function); thunk; thunk = DECL_CHAIN (thunk))
116 if (DECL_THIS_THUNK_P (thunk) == this_adjusting
117 && THUNK_FIXED_OFFSET (thunk) == d
118 && !virtual_offset == !THUNK_VIRTUAL_OFFSET (thunk)
119 && (!virtual_offset
120 || (this_adjusting
121 ? tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk),
122 virtual_offset)
123 : THUNK_VIRTUAL_OFFSET (thunk) == virtual_offset)))
124 return thunk;
126 /* All thunks must be created before FUNCTION is actually emitted;
127 the ABI requires that all thunks be emitted together with the
128 function to which they transfer control. */
129 gcc_assert (!TREE_ASM_WRITTEN (function));
130 /* Likewise, we can only be adding thunks to a function declared in
131 the class currently being laid out. */
132 gcc_assert (TYPE_SIZE (DECL_CONTEXT (function))
133 && TYPE_BEING_DEFINED (DECL_CONTEXT (function)));
135 thunk = build_decl (DECL_SOURCE_LOCATION (function),
136 FUNCTION_DECL, NULL_TREE, TREE_TYPE (function));
137 DECL_LANG_SPECIFIC (thunk) = DECL_LANG_SPECIFIC (function);
138 cxx_dup_lang_specific_decl (thunk);
139 DECL_VIRTUAL_P (thunk) = true;
140 SET_DECL_THUNKS (thunk, NULL_TREE);
142 DECL_CONTEXT (thunk) = DECL_CONTEXT (function);
143 TREE_READONLY (thunk) = TREE_READONLY (function);
144 TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (function);
145 TREE_PUBLIC (thunk) = TREE_PUBLIC (function);
146 SET_DECL_THUNK_P (thunk, this_adjusting);
147 THUNK_TARGET (thunk) = function;
148 THUNK_FIXED_OFFSET (thunk) = d;
149 THUNK_VIRTUAL_OFFSET (thunk) = virtual_offset;
150 THUNK_ALIAS (thunk) = NULL_TREE;
152 DECL_INTERFACE_KNOWN (thunk) = 1;
153 DECL_NOT_REALLY_EXTERN (thunk) = 1;
154 DECL_COMDAT (thunk) = DECL_COMDAT (function);
155 DECL_SAVED_FUNCTION_DATA (thunk) = NULL;
156 /* The thunk itself is not a constructor or destructor, even if
157 the thing it is thunking to is. */
158 DECL_DESTRUCTOR_P (thunk) = 0;
159 DECL_CONSTRUCTOR_P (thunk) = 0;
160 DECL_EXTERNAL (thunk) = 1;
161 DECL_ARTIFICIAL (thunk) = 1;
162 /* The THUNK is not a pending inline, even if the FUNCTION is. */
163 DECL_PENDING_INLINE_P (thunk) = 0;
164 DECL_DECLARED_INLINE_P (thunk) = 0;
165 /* Nor is it a template instantiation. */
166 DECL_USE_TEMPLATE (thunk) = 0;
167 DECL_TEMPLATE_INFO (thunk) = NULL;
169 /* Add it to the list of thunks associated with FUNCTION. */
170 DECL_CHAIN (thunk) = DECL_THUNKS (function);
171 SET_DECL_THUNKS (function, thunk);
173 return thunk;
176 /* Finish THUNK, a thunk decl. */
178 void
179 finish_thunk (tree thunk)
181 tree function, name;
182 tree fixed_offset = ssize_int (THUNK_FIXED_OFFSET (thunk));
183 tree virtual_offset = THUNK_VIRTUAL_OFFSET (thunk);
185 gcc_assert (!DECL_NAME (thunk) && DECL_THUNK_P (thunk));
186 if (virtual_offset && DECL_RESULT_THUNK_P (thunk))
187 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
188 function = THUNK_TARGET (thunk);
189 name = mangle_thunk (function, DECL_THIS_THUNK_P (thunk),
190 fixed_offset, virtual_offset);
192 /* We can end up with declarations of (logically) different
193 covariant thunks, that do identical adjustments. The two thunks
194 will be adjusting between within different hierarchies, which
195 happen to have the same layout. We must nullify one of them to
196 refer to the other. */
197 if (DECL_RESULT_THUNK_P (thunk))
199 tree cov_probe;
201 for (cov_probe = DECL_THUNKS (function);
202 cov_probe; cov_probe = DECL_CHAIN (cov_probe))
203 if (DECL_NAME (cov_probe) == name)
205 gcc_assert (!DECL_THUNKS (thunk));
206 THUNK_ALIAS (thunk) = (THUNK_ALIAS (cov_probe)
207 ? THUNK_ALIAS (cov_probe) : cov_probe);
208 break;
212 DECL_NAME (thunk) = name;
213 SET_DECL_ASSEMBLER_NAME (thunk, name);
216 static GTY (()) int thunk_labelno;
218 /* Create a static alias to target. */
220 tree
221 make_alias_for (tree target, tree newid)
223 tree alias = build_decl (DECL_SOURCE_LOCATION (target),
224 TREE_CODE (target), newid, TREE_TYPE (target));
225 DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (target);
226 cxx_dup_lang_specific_decl (alias);
227 DECL_CONTEXT (alias) = NULL;
228 TREE_READONLY (alias) = TREE_READONLY (target);
229 TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (target);
230 TREE_PUBLIC (alias) = 0;
231 DECL_INTERFACE_KNOWN (alias) = 1;
232 if (DECL_LANG_SPECIFIC (alias))
234 DECL_NOT_REALLY_EXTERN (alias) = 1;
235 DECL_USE_TEMPLATE (alias) = 0;
236 DECL_TEMPLATE_INFO (alias) = NULL;
238 DECL_EXTERNAL (alias) = 0;
239 DECL_ARTIFICIAL (alias) = 1;
240 DECL_TEMPLATE_INSTANTIATED (alias) = 0;
241 if (TREE_CODE (alias) == FUNCTION_DECL)
243 DECL_SAVED_FUNCTION_DATA (alias) = NULL;
244 DECL_DESTRUCTOR_P (alias) = 0;
245 DECL_CONSTRUCTOR_P (alias) = 0;
246 DECL_PENDING_INLINE_P (alias) = 0;
247 DECL_DECLARED_INLINE_P (alias) = 0;
248 DECL_INITIAL (alias) = error_mark_node;
249 DECL_ARGUMENTS (alias) = copy_list (DECL_ARGUMENTS (target));
251 else
252 TREE_STATIC (alias) = 1;
253 TREE_ADDRESSABLE (alias) = 1;
254 TREE_USED (alias) = 1;
255 SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
256 return alias;
259 static tree
260 make_alias_for_thunk (tree function)
262 tree alias;
263 char buf[256];
265 targetm.asm_out.generate_internal_label (buf, "LTHUNK", thunk_labelno);
266 thunk_labelno++;
268 alias = make_alias_for (function, get_identifier (buf));
270 if (!flag_syntax_only)
272 struct cgraph_node *funcn, *aliasn;
273 funcn = cgraph_node::get (function);
274 gcc_checking_assert (funcn);
275 aliasn = cgraph_node::create_same_body_alias (alias, function);
276 DECL_ASSEMBLER_NAME (function);
277 gcc_assert (aliasn != NULL);
280 return alias;
283 /* Emit the definition of a C++ multiple inheritance or covariant
284 return vtable thunk. If EMIT_P is nonzero, the thunk is emitted
285 immediately. */
287 void
288 use_thunk (tree thunk_fndecl, bool emit_p)
290 tree a, t, function, alias;
291 tree virtual_offset;
292 HOST_WIDE_INT fixed_offset, virtual_value;
293 bool this_adjusting = DECL_THIS_THUNK_P (thunk_fndecl);
294 struct cgraph_node *funcn, *thunk_node;
296 /* We should have called finish_thunk to give it a name. */
297 gcc_assert (DECL_NAME (thunk_fndecl));
299 /* We should never be using an alias, always refer to the
300 aliased thunk. */
301 gcc_assert (!THUNK_ALIAS (thunk_fndecl));
303 if (TREE_ASM_WRITTEN (thunk_fndecl))
304 return;
306 function = THUNK_TARGET (thunk_fndecl);
307 if (DECL_RESULT (thunk_fndecl))
308 /* We already turned this thunk into an ordinary function.
309 There's no need to process this thunk again. */
310 return;
312 if (DECL_THUNK_P (function))
313 /* The target is itself a thunk, process it now. */
314 use_thunk (function, emit_p);
316 /* Thunks are always addressable; they only appear in vtables. */
317 TREE_ADDRESSABLE (thunk_fndecl) = 1;
319 /* Figure out what function is being thunked to. It's referenced in
320 this translation unit. */
321 TREE_ADDRESSABLE (function) = 1;
322 mark_used (function);
323 if (!emit_p)
324 return;
326 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function))
327 alias = make_alias_for_thunk (function);
328 else
329 alias = function;
331 fixed_offset = THUNK_FIXED_OFFSET (thunk_fndecl);
332 virtual_offset = THUNK_VIRTUAL_OFFSET (thunk_fndecl);
334 if (virtual_offset)
336 if (!this_adjusting)
337 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
338 virtual_value = tree_to_shwi (virtual_offset);
339 gcc_assert (virtual_value);
341 else
342 virtual_value = 0;
344 /* And, if we need to emit the thunk, it's used. */
345 mark_used (thunk_fndecl);
346 /* This thunk is actually defined. */
347 DECL_EXTERNAL (thunk_fndecl) = 0;
348 /* The linkage of the function may have changed. FIXME in linkage
349 rewrite. */
350 gcc_assert (DECL_INTERFACE_KNOWN (function));
351 TREE_PUBLIC (thunk_fndecl) = TREE_PUBLIC (function);
352 DECL_VISIBILITY (thunk_fndecl) = DECL_VISIBILITY (function);
353 DECL_VISIBILITY_SPECIFIED (thunk_fndecl)
354 = DECL_VISIBILITY_SPECIFIED (function);
355 DECL_COMDAT (thunk_fndecl) = DECL_COMDAT (function);
356 DECL_WEAK (thunk_fndecl) = DECL_WEAK (function);
358 if (flag_syntax_only)
360 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
361 return;
364 push_to_top_level ();
366 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function)
367 && targetm_common.have_named_sections)
369 tree fn = function;
370 struct symtab_node *symbol;
372 if ((symbol = symtab_node::get (function))
373 && symbol->alias)
375 if (symbol->analyzed)
376 fn = symtab_node::get (function)->ultimate_alias_target ()->decl;
377 else
378 fn = symtab_node::get (function)->alias_target;
380 resolve_unique_section (fn, 0, flag_function_sections);
382 if (DECL_SECTION_NAME (fn) != NULL && DECL_ONE_ONLY (fn))
384 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
386 /* Output the thunk into the same section as function. */
387 set_decl_section_name (thunk_fndecl, DECL_SECTION_NAME (fn));
388 symtab_node::get (thunk_fndecl)->implicit_section
389 = symtab_node::get (fn)->implicit_section;
393 /* Set up cloned argument trees for the thunk. */
394 t = NULL_TREE;
395 for (a = DECL_ARGUMENTS (function); a; a = DECL_CHAIN (a))
397 tree x = copy_node (a);
398 DECL_CHAIN (x) = t;
399 DECL_CONTEXT (x) = thunk_fndecl;
400 SET_DECL_RTL (x, NULL);
401 DECL_HAS_VALUE_EXPR_P (x) = 0;
402 TREE_ADDRESSABLE (x) = 0;
403 t = x;
405 a = nreverse (t);
406 DECL_ARGUMENTS (thunk_fndecl) = a;
407 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
408 funcn = cgraph_node::get (function);
409 gcc_checking_assert (funcn);
410 thunk_node = funcn->create_thunk (thunk_fndecl, function,
411 this_adjusting, fixed_offset, virtual_value,
412 virtual_offset, alias);
413 if (DECL_ONE_ONLY (function))
414 thunk_node->add_to_same_comdat_group (funcn);
416 if (!this_adjusting
417 || !targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
418 virtual_value, alias))
420 /* If this is a covariant thunk, or we don't have the necessary
421 code for efficient thunks, generate a thunk function that
422 just makes a call to the real function. Unfortunately, this
423 doesn't work for varargs. */
425 if (varargs_function_p (function))
426 error ("generic thunk code fails for method %q#D which uses %<...%>",
427 function);
430 pop_from_top_level ();
433 /* Code for synthesizing methods which have default semantics defined. */
435 /* True iff CTYPE has a trivial SFK. */
437 static bool
438 type_has_trivial_fn (tree ctype, special_function_kind sfk)
440 switch (sfk)
442 case sfk_constructor:
443 return !TYPE_HAS_COMPLEX_DFLT (ctype);
444 case sfk_copy_constructor:
445 return !TYPE_HAS_COMPLEX_COPY_CTOR (ctype);
446 case sfk_move_constructor:
447 return !TYPE_HAS_COMPLEX_MOVE_CTOR (ctype);
448 case sfk_copy_assignment:
449 return !TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype);
450 case sfk_move_assignment:
451 return !TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype);
452 case sfk_destructor:
453 return !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype);
454 case sfk_inheriting_constructor:
455 return false;
456 default:
457 gcc_unreachable ();
461 /* Note that CTYPE has a non-trivial SFK even though we previously thought
462 it was trivial. */
464 static void
465 type_set_nontrivial_flag (tree ctype, special_function_kind sfk)
467 switch (sfk)
469 case sfk_constructor:
470 TYPE_HAS_COMPLEX_DFLT (ctype) = true;
471 return;
472 case sfk_copy_constructor:
473 TYPE_HAS_COMPLEX_COPY_CTOR (ctype) = true;
474 return;
475 case sfk_move_constructor:
476 TYPE_HAS_COMPLEX_MOVE_CTOR (ctype) = true;
477 return;
478 case sfk_copy_assignment:
479 TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype) = true;
480 return;
481 case sfk_move_assignment:
482 TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype) = true;
483 return;
484 case sfk_destructor:
485 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
486 return;
487 case sfk_inheriting_constructor:
488 default:
489 gcc_unreachable ();
493 /* True iff FN is a trivial defaulted member function ([cd]tor, op=). */
495 bool
496 trivial_fn_p (tree fn)
498 if (!DECL_DEFAULTED_FN (fn))
499 return false;
501 /* If fn is a clone, get the primary variant. */
502 if (tree prim = DECL_CLONED_FUNCTION (fn))
503 fn = prim;
504 return type_has_trivial_fn (DECL_CONTEXT (fn), special_function_p (fn));
507 /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO
508 given the parameter or parameters PARM, possibly inherited constructor
509 base INH, or move flag MOVE_P. */
511 static tree
512 add_one_base_init (tree binfo, tree parm, bool move_p, tree inh,
513 tree member_init_list)
515 tree init;
516 if (inh)
518 /* An inheriting constructor only has a mem-initializer for
519 the base it inherits from. */
520 if (BINFO_TYPE (binfo) != inh)
521 return member_init_list;
523 tree *p = &init;
524 init = NULL_TREE;
525 for (; parm; parm = DECL_CHAIN (parm))
527 tree exp = convert_from_reference (parm);
528 if (TREE_CODE (TREE_TYPE (parm)) != REFERENCE_TYPE
529 || TYPE_REF_IS_RVALUE (TREE_TYPE (parm)))
530 exp = move (exp);
531 *p = build_tree_list (NULL_TREE, exp);
532 p = &TREE_CHAIN (*p);
535 else
537 init = build_base_path (PLUS_EXPR, parm, binfo, 1,
538 tf_warning_or_error);
539 if (move_p)
540 init = move (init);
541 init = build_tree_list (NULL_TREE, init);
543 return tree_cons (binfo, init, member_init_list);
546 /* Generate code for default X(X&) or X(X&&) constructor or an inheriting
547 constructor. */
549 static void
550 do_build_copy_constructor (tree fndecl)
552 tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
553 bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
554 bool trivial = trivial_fn_p (fndecl);
555 tree inh = DECL_INHERITED_CTOR_BASE (fndecl);
557 if (!inh)
558 parm = convert_from_reference (parm);
560 if (trivial
561 && is_empty_class (current_class_type))
562 /* Don't copy the padding byte; it might not have been allocated
563 if *this is a base subobject. */;
564 else if (trivial)
566 tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm);
567 finish_expr_stmt (t);
569 else
571 tree fields = TYPE_FIELDS (current_class_type);
572 tree member_init_list = NULL_TREE;
573 int cvquals = cp_type_quals (TREE_TYPE (parm));
574 int i;
575 tree binfo, base_binfo;
576 tree init;
577 vec<tree, va_gc> *vbases;
579 /* Initialize all the base-classes with the parameter converted
580 to their type so that we get their copy constructor and not
581 another constructor that takes current_class_type. We must
582 deal with the binfo's directly as a direct base might be
583 inaccessible due to ambiguity. */
584 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
585 vec_safe_iterate (vbases, i, &binfo); i++)
587 member_init_list = add_one_base_init (binfo, parm, move_p, inh,
588 member_init_list);
591 for (binfo = TYPE_BINFO (current_class_type), i = 0;
592 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
594 if (BINFO_VIRTUAL_P (base_binfo))
595 continue;
596 member_init_list = add_one_base_init (base_binfo, parm, move_p,
597 inh, member_init_list);
600 for (; fields; fields = DECL_CHAIN (fields))
602 tree field = fields;
603 tree expr_type;
605 if (TREE_CODE (field) != FIELD_DECL)
606 continue;
607 if (inh)
608 continue;
610 expr_type = TREE_TYPE (field);
611 if (DECL_NAME (field))
613 if (VFIELD_NAME_P (DECL_NAME (field)))
614 continue;
616 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
617 /* Just use the field; anonymous types can't have
618 nontrivial copy ctors or assignment ops or this
619 function would be deleted. */;
620 else
621 continue;
623 /* Compute the type of "init->field". If the copy-constructor
624 parameter is, for example, "const S&", and the type of
625 the field is "T", then the type will usually be "const
626 T". (There are no cv-qualified variants of reference
627 types.) */
628 if (TREE_CODE (expr_type) != REFERENCE_TYPE)
630 int quals = cvquals;
632 if (DECL_MUTABLE_P (field))
633 quals &= ~TYPE_QUAL_CONST;
634 quals |= cp_type_quals (expr_type);
635 expr_type = cp_build_qualified_type (expr_type, quals);
638 init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
639 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
640 /* 'move' breaks bit-fields, and has no effect for scalars. */
641 && !scalarish_type_p (expr_type))
642 init = move (init);
643 init = build_tree_list (NULL_TREE, init);
645 member_init_list = tree_cons (field, init, member_init_list);
647 finish_mem_initializers (member_init_list);
651 static void
652 do_build_copy_assign (tree fndecl)
654 tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl));
655 tree compound_stmt;
656 bool move_p = move_fn_p (fndecl);
657 bool trivial = trivial_fn_p (fndecl);
658 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
660 compound_stmt = begin_compound_stmt (0);
661 parm = convert_from_reference (parm);
663 if (trivial
664 && is_empty_class (current_class_type))
665 /* Don't copy the padding byte; it might not have been allocated
666 if *this is a base subobject. */;
667 else if (trivial)
669 tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
670 finish_expr_stmt (t);
672 else
674 tree fields;
675 int cvquals = cp_type_quals (TREE_TYPE (parm));
676 int i;
677 tree binfo, base_binfo;
679 /* Assign to each of the direct base classes. */
680 for (binfo = TYPE_BINFO (current_class_type), i = 0;
681 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
683 tree converted_parm;
684 vec<tree, va_gc> *parmvec;
686 /* We must convert PARM directly to the base class
687 explicitly since the base class may be ambiguous. */
688 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
689 tf_warning_or_error);
690 if (move_p)
691 converted_parm = move (converted_parm);
692 /* Call the base class assignment operator. */
693 parmvec = make_tree_vector_single (converted_parm);
694 finish_expr_stmt
695 (build_special_member_call (current_class_ref,
696 ansi_assopname (NOP_EXPR),
697 &parmvec,
698 base_binfo,
699 flags,
700 tf_warning_or_error));
701 release_tree_vector (parmvec);
704 /* Assign to each of the non-static data members. */
705 for (fields = TYPE_FIELDS (current_class_type);
706 fields;
707 fields = DECL_CHAIN (fields))
709 tree comp = current_class_ref;
710 tree init = parm;
711 tree field = fields;
712 tree expr_type;
713 int quals;
715 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
716 continue;
718 expr_type = TREE_TYPE (field);
720 if (CP_TYPE_CONST_P (expr_type))
722 error ("non-static const member %q#D, can%'t use default "
723 "assignment operator", field);
724 continue;
726 else if (TREE_CODE (expr_type) == REFERENCE_TYPE)
728 error ("non-static reference member %q#D, can%'t use "
729 "default assignment operator", field);
730 continue;
733 if (DECL_NAME (field))
735 if (VFIELD_NAME_P (DECL_NAME (field)))
736 continue;
738 else if (ANON_AGGR_TYPE_P (expr_type)
739 && TYPE_FIELDS (expr_type) != NULL_TREE)
740 /* Just use the field; anonymous types can't have
741 nontrivial copy ctors or assignment ops or this
742 function would be deleted. */;
743 else
744 continue;
746 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
748 /* Compute the type of init->field */
749 quals = cvquals;
750 if (DECL_MUTABLE_P (field))
751 quals &= ~TYPE_QUAL_CONST;
752 expr_type = cp_build_qualified_type (expr_type, quals);
754 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
755 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
756 /* 'move' breaks bit-fields, and has no effect for scalars. */
757 && !scalarish_type_p (expr_type))
758 init = move (init);
760 if (DECL_NAME (field))
761 init = cp_build_modify_expr (comp, NOP_EXPR, init,
762 tf_warning_or_error);
763 else
764 init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
765 finish_expr_stmt (init);
768 finish_return_stmt (current_class_ref);
769 finish_compound_stmt (compound_stmt);
772 /* Synthesize FNDECL, a non-static member function. */
774 void
775 synthesize_method (tree fndecl)
777 bool nested = (current_function_decl != NULL_TREE);
778 tree context = decl_function_context (fndecl);
779 bool need_body = true;
780 tree stmt;
781 location_t save_input_location = input_location;
782 int error_count = errorcount;
783 int warning_count = warningcount + werrorcount;
785 /* Reset the source location, we might have been previously
786 deferred, and thus have saved where we were first needed. */
787 DECL_SOURCE_LOCATION (fndecl)
788 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
790 /* If we've been asked to synthesize a clone, just synthesize the
791 cloned function instead. Doing so will automatically fill in the
792 body for the clone. */
793 if (DECL_CLONED_FUNCTION_P (fndecl))
794 fndecl = DECL_CLONED_FUNCTION (fndecl);
796 /* We may be in the middle of deferred access check. Disable
797 it now. */
798 push_deferring_access_checks (dk_no_deferred);
800 if (! context)
801 push_to_top_level ();
802 else if (nested)
803 push_function_context ();
805 input_location = DECL_SOURCE_LOCATION (fndecl);
807 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
808 stmt = begin_function_body ();
810 if (DECL_OVERLOADED_OPERATOR_P (fndecl) == NOP_EXPR)
812 do_build_copy_assign (fndecl);
813 need_body = false;
815 else if (DECL_CONSTRUCTOR_P (fndecl))
817 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
818 if (arg_chain != void_list_node)
819 do_build_copy_constructor (fndecl);
820 else
821 finish_mem_initializers (NULL_TREE);
824 /* If we haven't yet generated the body of the function, just
825 generate an empty compound statement. */
826 if (need_body)
828 tree compound_stmt;
829 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
830 finish_compound_stmt (compound_stmt);
833 finish_function_body (stmt);
834 expand_or_defer_fn (finish_function (0));
836 input_location = save_input_location;
838 if (! context)
839 pop_from_top_level ();
840 else if (nested)
841 pop_function_context ();
843 pop_deferring_access_checks ();
845 if (error_count != errorcount || warning_count != warningcount + werrorcount)
846 inform (input_location, "synthesized method %qD first required here ",
847 fndecl);
850 /* Build a reference to type TYPE with cv-quals QUALS, which is an
851 rvalue if RVALUE is true. */
853 static tree
854 build_stub_type (tree type, int quals, bool rvalue)
856 tree argtype = cp_build_qualified_type (type, quals);
857 return cp_build_reference_type (argtype, rvalue);
860 /* Build a dummy glvalue from dereferencing a dummy reference of type
861 REFTYPE. */
863 static tree
864 build_stub_object (tree reftype)
866 if (TREE_CODE (reftype) != REFERENCE_TYPE)
867 reftype = cp_build_reference_type (reftype, /*rval*/true);
868 tree stub = build1 (CONVERT_EXPR, reftype, integer_one_node);
869 return convert_from_reference (stub);
872 /* Determine which function will be called when looking up NAME in TYPE,
873 called with a single ARGTYPE argument, or no argument if ARGTYPE is
874 null. FLAGS and COMPLAIN are as for build_new_method_call.
876 Returns a FUNCTION_DECL if all is well.
877 Returns NULL_TREE if overload resolution failed.
878 Returns error_mark_node if the chosen function cannot be called. */
880 static tree
881 locate_fn_flags (tree type, tree name, tree argtype, int flags,
882 tsubst_flags_t complain)
884 tree ob, fn, fns, binfo, rval;
885 vec<tree, va_gc> *args;
887 if (TYPE_P (type))
888 binfo = TYPE_BINFO (type);
889 else
891 binfo = type;
892 type = BINFO_TYPE (binfo);
895 ob = build_stub_object (cp_build_reference_type (type, false));
896 args = make_tree_vector ();
897 if (argtype)
899 if (TREE_CODE (argtype) == TREE_LIST)
901 for (tree elt = argtype; elt != void_list_node;
902 elt = TREE_CHAIN (elt))
904 tree type = TREE_VALUE (elt);
905 tree arg = build_stub_object (type);
906 vec_safe_push (args, arg);
909 else
911 tree arg = build_stub_object (argtype);
912 args->quick_push (arg);
916 fns = lookup_fnfields (binfo, name, 0);
917 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
919 release_tree_vector (args);
920 if (fn && rval == error_mark_node)
921 return rval;
922 else
923 return fn;
926 /* Locate the dtor of TYPE. */
928 tree
929 get_dtor (tree type, tsubst_flags_t complain)
931 tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
932 LOOKUP_NORMAL, complain);
933 if (fn == error_mark_node)
934 return NULL_TREE;
935 return fn;
938 /* Locate the default ctor of TYPE. */
940 tree
941 locate_ctor (tree type)
943 tree fn;
945 push_deferring_access_checks (dk_no_check);
946 fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
947 LOOKUP_SPECULATIVE, tf_none);
948 pop_deferring_access_checks ();
949 if (fn == error_mark_node)
950 return NULL_TREE;
951 return fn;
954 /* Likewise, but give any appropriate errors. */
956 tree
957 get_default_ctor (tree type)
959 tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
960 LOOKUP_NORMAL, tf_warning_or_error);
961 if (fn == error_mark_node)
962 return NULL_TREE;
963 return fn;
966 /* Locate the copy ctor of TYPE. */
968 tree
969 get_copy_ctor (tree type, tsubst_flags_t complain)
971 int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
972 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
973 tree argtype = build_stub_type (type, quals, false);
974 tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
975 LOOKUP_NORMAL, complain);
976 if (fn == error_mark_node)
977 return NULL_TREE;
978 return fn;
981 /* Locate the copy assignment operator of TYPE. */
983 tree
984 get_copy_assign (tree type)
986 int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
987 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
988 tree argtype = build_stub_type (type, quals, false);
989 tree fn = locate_fn_flags (type, ansi_assopname (NOP_EXPR), argtype,
990 LOOKUP_NORMAL, tf_warning_or_error);
991 if (fn == error_mark_node)
992 return NULL_TREE;
993 return fn;
996 /* Locate the inherited constructor of constructor CTOR. */
998 tree
999 get_inherited_ctor (tree ctor)
1001 gcc_assert (DECL_INHERITED_CTOR_BASE (ctor));
1003 push_deferring_access_checks (dk_no_check);
1004 tree fn = locate_fn_flags (DECL_INHERITED_CTOR_BASE (ctor),
1005 complete_ctor_identifier,
1006 FUNCTION_FIRST_USER_PARMTYPE (ctor),
1007 LOOKUP_NORMAL|LOOKUP_SPECULATIVE,
1008 tf_none);
1009 pop_deferring_access_checks ();
1010 if (fn == error_mark_node)
1011 return NULL_TREE;
1012 return fn;
1015 /* walk_tree helper function for is_trivially_xible. If *TP is a call,
1016 return it if it calls something other than a trivial special member
1017 function. */
1019 static tree
1020 check_nontriv (tree *tp, int *, void *)
1022 tree fn;
1023 if (TREE_CODE (*tp) == CALL_EXPR)
1024 fn = CALL_EXPR_FN (*tp);
1025 else if (TREE_CODE (*tp) == AGGR_INIT_EXPR)
1026 fn = AGGR_INIT_EXPR_FN (*tp);
1027 else
1028 return NULL_TREE;
1030 if (TREE_CODE (fn) == ADDR_EXPR)
1031 fn = TREE_OPERAND (fn, 0);
1033 if (TREE_CODE (fn) != FUNCTION_DECL
1034 || !trivial_fn_p (fn))
1035 return fn;
1036 return NULL_TREE;
1039 /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */
1041 static tree
1042 assignable_expr (tree to, tree from)
1044 ++cp_unevaluated_operand;
1045 to = build_stub_object (to);
1046 from = build_stub_object (from);
1047 tree r = cp_build_modify_expr (to, NOP_EXPR, from, tf_none);
1048 --cp_unevaluated_operand;
1049 return r;
1052 /* The predicate condition for a template specialization
1053 is_constructible<T, Args...> shall be satisfied if and only if the
1054 following variable definition would be well-formed for some invented
1055 variable t: T t(create<Args>()...);
1057 Return something equivalent in well-formedness and triviality. */
1059 static tree
1060 constructible_expr (tree to, tree from)
1062 tree expr;
1063 if (CLASS_TYPE_P (to))
1065 tree ctype = to;
1066 vec<tree, va_gc> *args = NULL;
1067 if (TREE_CODE (to) != REFERENCE_TYPE)
1068 to = cp_build_reference_type (to, /*rval*/false);
1069 tree ob = build_stub_object (to);
1070 for (; from; from = TREE_CHAIN (from))
1071 vec_safe_push (args, build_stub_object (TREE_VALUE (from)));
1072 expr = build_special_member_call (ob, complete_ctor_identifier, &args,
1073 ctype, LOOKUP_NORMAL, tf_none);
1074 if (expr == error_mark_node)
1075 return error_mark_node;
1076 /* The current state of the standard vis-a-vis LWG 2116 is that
1077 is_*constructible involves destruction as well. */
1078 if (type_build_dtor_call (ctype))
1080 tree dtor = build_special_member_call (ob, complete_dtor_identifier,
1081 NULL, ctype, LOOKUP_NORMAL,
1082 tf_none);
1083 if (dtor == error_mark_node)
1084 return error_mark_node;
1085 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
1086 expr = build2 (COMPOUND_EXPR, void_type_node, expr, dtor);
1089 else
1091 if (from == NULL_TREE)
1092 return build_value_init (to, tf_none);
1093 else if (TREE_CHAIN (from))
1094 return error_mark_node; // too many initializers
1095 from = build_stub_object (TREE_VALUE (from));
1096 expr = perform_direct_initialization_if_possible (to, from,
1097 /*cast*/false,
1098 tf_none);
1100 return expr;
1103 /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or
1104 constructible (otherwise) from FROM, which is a single type for
1105 assignment or a list of types for construction. */
1107 bool
1108 is_trivially_xible (enum tree_code code, tree to, tree from)
1110 tree expr;
1111 if (code == MODIFY_EXPR)
1112 expr = assignable_expr (to, from);
1113 else if (from && TREE_CHAIN (from))
1114 return false; // only 0- and 1-argument ctors can be trivial
1115 else
1116 expr = constructible_expr (to, from);
1118 if (expr == error_mark_node)
1119 return false;
1120 tree nt = cp_walk_tree_without_duplicates (&expr, check_nontriv, NULL);
1121 return !nt;
1124 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
1125 DELETED_P or give an error message MSG with argument ARG. */
1127 static void
1128 process_subob_fn (tree fn, tree *spec_p, bool *trivial_p,
1129 bool *deleted_p, bool *constexpr_p,
1130 bool diag, tree arg)
1132 if (!fn || fn == error_mark_node)
1133 goto bad;
1135 if (spec_p)
1137 maybe_instantiate_noexcept (fn);
1138 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
1139 *spec_p = merge_exception_specifiers (*spec_p, raises);
1142 if (!trivial_fn_p (fn))
1144 if (trivial_p)
1145 *trivial_p = false;
1146 if (TREE_CODE (arg) == FIELD_DECL
1147 && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
1149 if (deleted_p)
1150 *deleted_p = true;
1151 if (diag)
1152 error ("union member %q+D with non-trivial %qD", arg, fn);
1156 if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
1158 *constexpr_p = false;
1159 if (diag)
1161 inform (0, "defaulted constructor calls non-constexpr "
1162 "%q+D", fn);
1163 explain_invalid_constexpr_fn (fn);
1167 return;
1169 bad:
1170 if (deleted_p)
1171 *deleted_p = true;
1174 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
1175 aggregates. */
1177 static void
1178 walk_field_subobs (tree fields, tree fnname, special_function_kind sfk,
1179 int quals, bool copy_arg_p, bool move_p,
1180 bool assign_p, tree *spec_p, bool *trivial_p,
1181 bool *deleted_p, bool *constexpr_p,
1182 bool diag, int flags, tsubst_flags_t complain)
1184 tree field;
1185 for (field = fields; field; field = DECL_CHAIN (field))
1187 tree mem_type, argtype, rval;
1189 if (TREE_CODE (field) != FIELD_DECL
1190 || DECL_ARTIFICIAL (field))
1191 continue;
1193 mem_type = strip_array_types (TREE_TYPE (field));
1194 if (assign_p)
1196 bool bad = true;
1197 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
1199 if (diag)
1200 error ("non-static const member %q#D, can%'t use default "
1201 "assignment operator", field);
1203 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1205 if (diag)
1206 error ("non-static reference member %q#D, can%'t use "
1207 "default assignment operator", field);
1209 else
1210 bad = false;
1212 if (bad && deleted_p)
1213 *deleted_p = true;
1215 else if (sfk == sfk_constructor)
1217 bool bad;
1219 if (DECL_INITIAL (field))
1221 if (diag && DECL_INITIAL (field) == error_mark_node)
1222 inform (0, "initializer for %q+#D is invalid", field);
1223 if (trivial_p)
1224 *trivial_p = false;
1225 /* Core 1351: If the field has an NSDMI that could throw, the
1226 default constructor is noexcept(false). */
1227 if (spec_p)
1229 tree nsdmi = get_nsdmi (field, /*ctor*/false);
1230 if (!expr_noexcept_p (nsdmi, complain))
1231 *spec_p = noexcept_false_spec;
1233 /* Don't do the normal processing. */
1234 continue;
1237 bad = false;
1238 if (CP_TYPE_CONST_P (mem_type)
1239 && default_init_uninitialized_part (mem_type))
1241 if (diag)
1243 error ("uninitialized const member in %q#T",
1244 current_class_type);
1245 inform (DECL_SOURCE_LOCATION (field),
1246 "%q#D should be initialized", field);
1248 bad = true;
1250 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1252 if (diag)
1254 error ("uninitialized reference member in %q#T",
1255 current_class_type);
1256 inform (DECL_SOURCE_LOCATION (field),
1257 "%q#D should be initialized", field);
1259 bad = true;
1262 if (bad && deleted_p)
1263 *deleted_p = true;
1265 /* For an implicitly-defined default constructor to be constexpr,
1266 every member must have a user-provided default constructor or
1267 an explicit initializer. */
1268 if (constexpr_p && !CLASS_TYPE_P (mem_type)
1269 && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE)
1271 *constexpr_p = false;
1272 if (diag)
1273 inform (0, "defaulted default constructor does not "
1274 "initialize %q+#D", field);
1277 else if (sfk == sfk_copy_constructor)
1279 /* 12.8p11b5 */
1280 if (TREE_CODE (mem_type) == REFERENCE_TYPE
1281 && TYPE_REF_IS_RVALUE (mem_type))
1283 if (diag)
1284 error ("copying non-static data member %q#D of rvalue "
1285 "reference type", field);
1286 if (deleted_p)
1287 *deleted_p = true;
1291 if (!CLASS_TYPE_P (mem_type))
1292 continue;
1294 if (ANON_AGGR_TYPE_P (mem_type))
1296 walk_field_subobs (TYPE_FIELDS (mem_type), fnname, sfk, quals,
1297 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1298 deleted_p, constexpr_p,
1299 diag, flags, complain);
1300 continue;
1303 if (copy_arg_p)
1305 int mem_quals = cp_type_quals (mem_type) | quals;
1306 if (DECL_MUTABLE_P (field))
1307 mem_quals &= ~TYPE_QUAL_CONST;
1308 argtype = build_stub_type (mem_type, mem_quals, move_p);
1310 else
1311 argtype = NULL_TREE;
1313 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
1315 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1316 constexpr_p, diag, field);
1320 /* The caller wants to generate an implicit declaration of SFK for CTYPE
1321 which is const if relevant and CONST_P is set. If spec_p, trivial_p and
1322 deleted_p are non-null, set their referent appropriately. If diag is
1323 true, we're either being called from maybe_explain_implicit_delete to
1324 give errors, or if constexpr_p is non-null, from
1325 explain_invalid_constexpr_fn. */
1327 static void
1328 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
1329 tree *spec_p, bool *trivial_p, bool *deleted_p,
1330 bool *constexpr_p, bool diag,
1331 tree inherited_base, tree inherited_parms)
1333 tree binfo, base_binfo, scope, fnname, rval, argtype;
1334 bool move_p, copy_arg_p, assign_p, expected_trivial, check_vdtor;
1335 vec<tree, va_gc> *vbases;
1336 int i, quals, flags;
1337 tsubst_flags_t complain;
1338 bool ctor_p;
1340 if (spec_p)
1341 *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec);
1343 if (deleted_p)
1345 /* "The closure type associated with a lambda-expression has a deleted
1346 default constructor and a deleted copy assignment operator."
1347 This is diagnosed in maybe_explain_implicit_delete. */
1348 if (LAMBDA_TYPE_P (ctype)
1349 && (sfk == sfk_constructor
1350 || sfk == sfk_copy_assignment))
1352 *deleted_p = true;
1353 return;
1356 *deleted_p = false;
1359 ctor_p = false;
1360 assign_p = false;
1361 check_vdtor = false;
1362 switch (sfk)
1364 case sfk_move_assignment:
1365 case sfk_copy_assignment:
1366 assign_p = true;
1367 fnname = ansi_assopname (NOP_EXPR);
1368 break;
1370 case sfk_destructor:
1371 check_vdtor = true;
1372 /* The synthesized method will call base dtors, but check complete
1373 here to avoid having to deal with VTT. */
1374 fnname = complete_dtor_identifier;
1375 break;
1377 case sfk_constructor:
1378 case sfk_move_constructor:
1379 case sfk_copy_constructor:
1380 case sfk_inheriting_constructor:
1381 ctor_p = true;
1382 fnname = complete_ctor_identifier;
1383 break;
1385 default:
1386 gcc_unreachable ();
1389 gcc_assert ((sfk == sfk_inheriting_constructor)
1390 == (inherited_base != NULL_TREE));
1392 /* If that user-written default constructor would satisfy the
1393 requirements of a constexpr constructor (7.1.5), the
1394 implicitly-defined default constructor is constexpr. */
1395 if (constexpr_p)
1396 *constexpr_p = ctor_p;
1398 move_p = false;
1399 switch (sfk)
1401 case sfk_constructor:
1402 case sfk_destructor:
1403 case sfk_inheriting_constructor:
1404 copy_arg_p = false;
1405 break;
1407 case sfk_move_constructor:
1408 case sfk_move_assignment:
1409 move_p = true;
1410 case sfk_copy_constructor:
1411 case sfk_copy_assignment:
1412 copy_arg_p = true;
1413 break;
1415 default:
1416 gcc_unreachable ();
1419 expected_trivial = type_has_trivial_fn (ctype, sfk);
1420 if (trivial_p)
1421 *trivial_p = expected_trivial;
1423 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
1424 class versions and other properties of the type. But a subobject
1425 class can be trivially copyable and yet have overload resolution
1426 choose a template constructor for initialization, depending on
1427 rvalueness and cv-quals. And furthermore, a member in a base might
1428 be trivial but deleted or otherwise not callable. So we can't exit
1429 early in C++0x. The same considerations apply in C++98/03, but
1430 there the definition of triviality does not consider overload
1431 resolution, so a constructor can be trivial even if it would otherwise
1432 call a non-trivial constructor. */
1433 if (expected_trivial
1434 && (!copy_arg_p || cxx_dialect < cxx11))
1436 if (constexpr_p && sfk == sfk_constructor)
1438 bool cx = trivial_default_constructor_is_constexpr (ctype);
1439 *constexpr_p = cx;
1440 if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
1441 /* A trivial constructor doesn't have any NSDMI. */
1442 inform (input_location, "defaulted default constructor does "
1443 "not initialize any non-static data member");
1445 if (!diag && cxx_dialect < cxx11)
1446 return;
1449 ++cp_unevaluated_operand;
1450 ++c_inhibit_evaluation_warnings;
1451 push_deferring_access_checks (dk_no_deferred);
1453 scope = push_scope (ctype);
1455 flags = LOOKUP_NORMAL|LOOKUP_SPECULATIVE;
1456 if (!inherited_base)
1457 flags |= LOOKUP_DEFAULTED;
1459 complain = diag ? tf_warning_or_error : tf_none;
1461 if (const_p)
1462 quals = TYPE_QUAL_CONST;
1463 else
1464 quals = TYPE_UNQUALIFIED;
1465 argtype = NULL_TREE;
1467 for (binfo = TYPE_BINFO (ctype), i = 0;
1468 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
1470 tree basetype = BINFO_TYPE (base_binfo);
1472 if (!assign_p && BINFO_VIRTUAL_P (base_binfo))
1473 /* We'll handle virtual bases below. */
1474 continue;
1476 if (copy_arg_p)
1477 argtype = build_stub_type (basetype, quals, move_p);
1478 else if (basetype == inherited_base)
1479 argtype = inherited_parms;
1480 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1481 if (inherited_base)
1482 argtype = NULL_TREE;
1484 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1485 constexpr_p, diag, basetype);
1486 if (ctor_p)
1488 /* In a constructor we also need to check the subobject
1489 destructors for cleanup of partially constructed objects. */
1490 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1491 NULL_TREE, flags, complain);
1492 /* Note that we don't pass down trivial_p; the subobject
1493 destructors don't affect triviality of the constructor. Nor
1494 do they affect constexpr-ness (a constant expression doesn't
1495 throw) or exception-specification (a throw from one of the
1496 dtors would be a double-fault). */
1497 process_subob_fn (rval, NULL, NULL,
1498 deleted_p, NULL, false,
1499 basetype);
1502 if (check_vdtor && type_has_virtual_destructor (basetype))
1504 rval = locate_fn_flags (ctype, ansi_opname (DELETE_EXPR),
1505 ptr_type_node, flags, complain);
1506 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
1507 to have a null rval (no class-specific op delete). */
1508 if (rval && rval == error_mark_node && deleted_p)
1509 *deleted_p = true;
1510 check_vdtor = false;
1513 if (diag && assign_p && move_p
1514 && BINFO_VIRTUAL_P (base_binfo)
1515 && rval && TREE_CODE (rval) == FUNCTION_DECL
1516 && move_fn_p (rval) && !trivial_fn_p (rval)
1517 && vbase_has_user_provided_move_assign (basetype))
1518 warning (OPT_Wvirtual_move_assign,
1519 "defaulted move assignment for %qT calls a non-trivial "
1520 "move assignment operator for virtual base %qT",
1521 ctype, basetype);
1524 vbases = CLASSTYPE_VBASECLASSES (ctype);
1525 if (vec_safe_is_empty (vbases))
1526 /* No virtual bases to worry about. */;
1527 else if (!assign_p)
1529 if (constexpr_p)
1530 *constexpr_p = false;
1531 FOR_EACH_VEC_ELT (*vbases, i, base_binfo)
1533 tree basetype = BINFO_TYPE (base_binfo);
1534 if (copy_arg_p)
1535 argtype = build_stub_type (basetype, quals, move_p);
1536 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1538 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1539 constexpr_p, diag, basetype);
1540 if (ctor_p && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype))
1542 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1543 NULL_TREE, flags, complain);
1544 process_subob_fn (rval, NULL, NULL,
1545 deleted_p, NULL, false,
1546 basetype);
1551 /* Now handle the non-static data members. */
1552 walk_field_subobs (TYPE_FIELDS (ctype), fnname, sfk, quals,
1553 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1554 deleted_p, constexpr_p,
1555 diag, flags, complain);
1556 if (ctor_p)
1557 walk_field_subobs (TYPE_FIELDS (ctype), complete_dtor_identifier,
1558 sfk_destructor, TYPE_UNQUALIFIED, false,
1559 false, false, NULL, NULL,
1560 deleted_p, NULL,
1561 false, flags, complain);
1563 pop_scope (scope);
1565 pop_deferring_access_checks ();
1566 --cp_unevaluated_operand;
1567 --c_inhibit_evaluation_warnings;
1570 /* DECL is a defaulted function whose exception specification is now
1571 needed. Return what it should be. */
1573 tree
1574 get_defaulted_eh_spec (tree decl)
1576 if (DECL_CLONED_FUNCTION_P (decl))
1577 decl = DECL_CLONED_FUNCTION (decl);
1578 special_function_kind sfk = special_function_p (decl);
1579 tree ctype = DECL_CONTEXT (decl);
1580 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1581 tree parm_type = TREE_VALUE (parms);
1582 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1583 tree spec = empty_except_spec;
1584 synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL,
1585 NULL, false, DECL_INHERITED_CTOR_BASE (decl),
1586 parms);
1587 return spec;
1590 /* DECL is a deleted function. If it's implicitly deleted, explain why and
1591 return true; else return false. */
1593 bool
1594 maybe_explain_implicit_delete (tree decl)
1596 /* If decl is a clone, get the primary variant. */
1597 decl = DECL_ORIGIN (decl);
1598 gcc_assert (DECL_DELETED_FN (decl));
1599 if (DECL_DEFAULTED_FN (decl))
1601 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
1602 static hash_set<tree> *explained;
1604 special_function_kind sfk;
1605 location_t loc;
1606 bool informed;
1607 tree ctype;
1609 if (!explained)
1610 explained = new hash_set<tree>;
1611 if (explained->add (decl))
1612 return true;
1614 sfk = special_function_p (decl);
1615 ctype = DECL_CONTEXT (decl);
1616 loc = input_location;
1617 input_location = DECL_SOURCE_LOCATION (decl);
1619 informed = false;
1620 if (LAMBDA_TYPE_P (ctype))
1622 informed = true;
1623 if (sfk == sfk_constructor)
1624 inform (DECL_SOURCE_LOCATION (decl),
1625 "a lambda closure type has a deleted default constructor");
1626 else if (sfk == sfk_copy_assignment)
1627 inform (DECL_SOURCE_LOCATION (decl),
1628 "a lambda closure type has a deleted copy assignment operator");
1629 else
1630 informed = false;
1632 else if (DECL_ARTIFICIAL (decl)
1633 && (sfk == sfk_copy_assignment
1634 || sfk == sfk_copy_constructor)
1635 && (type_has_user_declared_move_constructor (ctype)
1636 || type_has_user_declared_move_assign (ctype)))
1638 inform (0, "%q+#D is implicitly declared as deleted because %qT "
1639 "declares a move constructor or move assignment operator",
1640 decl, ctype);
1641 informed = true;
1643 if (!informed)
1645 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1646 tree parm_type = TREE_VALUE (parms);
1647 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1648 tree raises = NULL_TREE;
1649 bool deleted_p = false;
1650 tree scope = push_scope (ctype);
1652 synthesized_method_walk (ctype, sfk, const_p,
1653 &raises, NULL, &deleted_p, NULL, false,
1654 DECL_INHERITED_CTOR_BASE (decl), parms);
1655 if (deleted_p)
1657 inform (0, "%q+#D is implicitly deleted because the default "
1658 "definition would be ill-formed:", decl);
1659 synthesized_method_walk (ctype, sfk, const_p,
1660 NULL, NULL, NULL, NULL, true,
1661 DECL_INHERITED_CTOR_BASE (decl), parms);
1663 else if (!comp_except_specs
1664 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
1665 raises, ce_normal))
1666 inform (DECL_SOURCE_LOCATION (decl), "%q#F is implicitly "
1667 "deleted because its exception-specification does not "
1668 "match the implicit exception-specification %qX",
1669 decl, raises);
1670 #ifdef ENABLE_CHECKING
1671 else
1672 gcc_unreachable ();
1673 #endif
1675 pop_scope (scope);
1678 input_location = loc;
1679 return true;
1681 return false;
1684 /* DECL is a defaulted function which was declared constexpr. Explain why
1685 it can't be constexpr. */
1687 void
1688 explain_implicit_non_constexpr (tree decl)
1690 tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl));
1691 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1692 bool dummy;
1693 synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
1694 special_function_p (decl), const_p,
1695 NULL, NULL, NULL, &dummy, true,
1696 DECL_INHERITED_CTOR_BASE (decl),
1697 FUNCTION_FIRST_USER_PARMTYPE (decl));
1700 /* DECL is an instantiation of an inheriting constructor template. Deduce
1701 the correct exception-specification and deletedness for this particular
1702 specialization. */
1704 void
1705 deduce_inheriting_ctor (tree decl)
1707 gcc_assert (DECL_INHERITED_CTOR_BASE (decl));
1708 tree spec;
1709 bool trivial, constexpr_, deleted;
1710 synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor,
1711 false, &spec, &trivial, &deleted, &constexpr_,
1712 /*diag*/false,
1713 DECL_INHERITED_CTOR_BASE (decl),
1714 FUNCTION_FIRST_USER_PARMTYPE (decl));
1715 DECL_DELETED_FN (decl) = deleted;
1716 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
1719 /* Implicitly declare the special function indicated by KIND, as a
1720 member of TYPE. For copy constructors and assignment operators,
1721 CONST_P indicates whether these functions should take a const
1722 reference argument or a non-const reference. Returns the
1723 FUNCTION_DECL for the implicitly declared function. */
1725 tree
1726 implicitly_declare_fn (special_function_kind kind, tree type,
1727 bool const_p, tree inherited_ctor,
1728 tree inherited_parms)
1730 tree fn;
1731 tree parameter_types = void_list_node;
1732 tree return_type;
1733 tree fn_type;
1734 tree raises = empty_except_spec;
1735 tree rhs_parm_type = NULL_TREE;
1736 tree this_parm;
1737 tree name;
1738 HOST_WIDE_INT saved_processing_template_decl;
1739 bool deleted_p;
1740 bool constexpr_p;
1742 /* Because we create declarations for implicitly declared functions
1743 lazily, we may be creating the declaration for a member of TYPE
1744 while in some completely different context. However, TYPE will
1745 never be a dependent class (because we never want to do lookups
1746 for implicitly defined functions in a dependent class).
1747 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
1748 because we only create clones for constructors and destructors
1749 when not in a template. */
1750 gcc_assert (!dependent_type_p (type));
1751 saved_processing_template_decl = processing_template_decl;
1752 processing_template_decl = 0;
1754 type = TYPE_MAIN_VARIANT (type);
1756 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (type))
1758 if (kind == sfk_destructor)
1759 /* See comment in check_special_function_return_type. */
1760 return_type = build_pointer_type (void_type_node);
1761 else
1762 return_type = build_pointer_type (type);
1764 else
1765 return_type = void_type_node;
1767 switch (kind)
1769 case sfk_destructor:
1770 /* Destructor. */
1771 name = constructor_name (type);
1772 break;
1774 case sfk_constructor:
1775 /* Default constructor. */
1776 name = constructor_name (type);
1777 break;
1779 case sfk_copy_constructor:
1780 case sfk_copy_assignment:
1781 case sfk_move_constructor:
1782 case sfk_move_assignment:
1783 case sfk_inheriting_constructor:
1785 bool move_p;
1786 if (kind == sfk_copy_assignment
1787 || kind == sfk_move_assignment)
1789 return_type = build_reference_type (type);
1790 name = ansi_assopname (NOP_EXPR);
1792 else
1793 name = constructor_name (type);
1795 if (kind == sfk_inheriting_constructor)
1796 parameter_types = inherited_parms;
1797 else
1799 if (const_p)
1800 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
1801 else
1802 rhs_parm_type = type;
1803 move_p = (kind == sfk_move_assignment
1804 || kind == sfk_move_constructor);
1805 rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
1807 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
1809 break;
1811 default:
1812 gcc_unreachable ();
1815 tree inherited_base = (inherited_ctor
1816 ? DECL_CONTEXT (inherited_ctor)
1817 : NULL_TREE);
1818 bool trivial_p = false;
1820 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
1822 /* For an inheriting constructor template, just copy these flags from
1823 the inherited constructor template for now. */
1824 raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor));
1825 deleted_p = DECL_DELETED_FN (inherited_ctor);
1826 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
1828 else if (cxx_dialect >= cxx11)
1830 raises = unevaluated_noexcept_spec ();
1831 synthesized_method_walk (type, kind, const_p, NULL, &trivial_p,
1832 &deleted_p, &constexpr_p, false,
1833 inherited_base, inherited_parms);
1835 else
1836 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
1837 &deleted_p, &constexpr_p, false,
1838 inherited_base, inherited_parms);
1839 /* Don't bother marking a deleted constructor as constexpr. */
1840 if (deleted_p)
1841 constexpr_p = false;
1842 /* A trivial copy/move constructor is also a constexpr constructor,
1843 unless the class has virtual bases (7.1.5p4). */
1844 else if (trivial_p && cxx_dialect >= cxx11
1845 && (kind == sfk_copy_constructor
1846 || kind == sfk_move_constructor)
1847 && !CLASSTYPE_VBASECLASSES (type))
1848 gcc_assert (constexpr_p);
1850 if (!trivial_p && type_has_trivial_fn (type, kind))
1851 type_set_nontrivial_flag (type, kind);
1853 /* Create the function. */
1854 fn_type = build_method_type_directly (type, return_type, parameter_types);
1855 if (raises)
1856 fn_type = build_exception_variant (fn_type, raises);
1857 fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
1858 if (kind != sfk_inheriting_constructor)
1859 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
1860 if (kind == sfk_constructor || kind == sfk_copy_constructor
1861 || kind == sfk_move_constructor || kind == sfk_inheriting_constructor)
1862 DECL_CONSTRUCTOR_P (fn) = 1;
1863 else if (kind == sfk_destructor)
1864 DECL_DESTRUCTOR_P (fn) = 1;
1865 else
1867 DECL_ASSIGNMENT_OPERATOR_P (fn) = 1;
1868 SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR);
1871 /* If pointers to member functions use the least significant bit to
1872 indicate whether a function is virtual, ensure a pointer
1873 to this function will have that bit clear. */
1874 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
1875 && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
1876 DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
1878 /* Create the explicit arguments. */
1879 if (rhs_parm_type)
1881 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
1882 want its type to be included in the mangled function
1883 name. */
1884 tree decl = cp_build_parm_decl (NULL_TREE, rhs_parm_type);
1885 TREE_READONLY (decl) = 1;
1886 retrofit_lang_decl (decl);
1887 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
1888 DECL_ARGUMENTS (fn) = decl;
1890 else if (kind == sfk_inheriting_constructor)
1892 tree *p = &DECL_ARGUMENTS (fn);
1893 int index = 1;
1894 for (tree parm = inherited_parms; parm != void_list_node;
1895 parm = TREE_CHAIN (parm))
1897 *p = cp_build_parm_decl (NULL_TREE, TREE_VALUE (parm));
1898 retrofit_lang_decl (*p);
1899 DECL_PARM_LEVEL (*p) = 1;
1900 DECL_PARM_INDEX (*p) = index++;
1901 DECL_CONTEXT (*p) = fn;
1902 p = &DECL_CHAIN (*p);
1904 SET_DECL_INHERITED_CTOR_BASE (fn, inherited_base);
1905 DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor);
1906 /* A constructor so declared has the same access as the corresponding
1907 constructor in X. */
1908 TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor);
1909 TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor);
1910 /* Copy constexpr from the inherited constructor even if the
1911 inheriting constructor doesn't satisfy the requirements. */
1912 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
1914 /* Add the "this" parameter. */
1915 this_parm = build_this_parm (fn_type, TYPE_UNQUALIFIED);
1916 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
1917 DECL_ARGUMENTS (fn) = this_parm;
1919 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
1920 DECL_IN_AGGR_P (fn) = 1;
1921 DECL_ARTIFICIAL (fn) = 1;
1922 DECL_DEFAULTED_FN (fn) = 1;
1923 if (cxx_dialect >= cxx11)
1925 /* "The closure type associated with a lambda-expression has a deleted
1926 default constructor and a deleted copy assignment operator." */
1927 if ((kind == sfk_constructor
1928 || kind == sfk_copy_assignment)
1929 && LAMBDA_TYPE_P (type))
1930 deleted_p = true;
1931 DECL_DELETED_FN (fn) = deleted_p;
1932 DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
1934 DECL_EXTERNAL (fn) = true;
1935 DECL_NOT_REALLY_EXTERN (fn) = 1;
1936 DECL_DECLARED_INLINE_P (fn) = 1;
1937 DECL_COMDAT (fn) = 1;
1938 set_linkage_according_to_type (type, fn);
1939 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
1940 gcc_assert (!TREE_USED (fn));
1942 /* Restore PROCESSING_TEMPLATE_DECL. */
1943 processing_template_decl = saved_processing_template_decl;
1945 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
1946 fn = add_inherited_template_parms (fn, inherited_ctor);
1948 /* Warn about calling a non-trivial move assignment in a virtual base. */
1949 if (kind == sfk_move_assignment && !deleted_p && !trivial_p
1950 && CLASSTYPE_VBASECLASSES (type))
1952 location_t loc = input_location;
1953 input_location = DECL_SOURCE_LOCATION (fn);
1954 synthesized_method_walk (type, kind, const_p,
1955 NULL, NULL, NULL, NULL, true,
1956 NULL_TREE, NULL_TREE);
1957 input_location = loc;
1960 return fn;
1963 /* Gives any errors about defaulted functions which need to be deferred
1964 until the containing class is complete. */
1966 void
1967 defaulted_late_check (tree fn)
1969 /* Complain about invalid signature for defaulted fn. */
1970 tree ctx = DECL_CONTEXT (fn);
1971 special_function_kind kind = special_function_p (fn);
1972 bool fn_const_p = (copy_fn_p (fn) == 2);
1973 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
1974 NULL, NULL);
1975 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
1977 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
1978 TREE_TYPE (TREE_TYPE (implicit_fn)))
1979 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1980 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn))))
1982 error ("defaulted declaration %q+D", fn);
1983 error_at (DECL_SOURCE_LOCATION (fn),
1984 "does not match expected signature %qD", implicit_fn);
1987 /* 8.4.2/2: An explicitly-defaulted function (...) may have an explicit
1988 exception-specification only if it is compatible (15.4) with the
1989 exception-specification on the implicit declaration. If a function
1990 is explicitly defaulted on its first declaration, (...) it is
1991 implicitly considered to have the same exception-specification as if
1992 it had been implicitly declared. */
1993 maybe_instantiate_noexcept (fn);
1994 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
1995 if (!fn_spec)
1997 if (DECL_DEFAULTED_IN_CLASS_P (fn))
1998 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
2000 else if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
2001 /* Equivalent to the implicit spec. */;
2002 else if (DECL_DEFAULTED_IN_CLASS_P (fn)
2003 && !CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
2004 /* We can't compare an explicit exception-specification on a
2005 constructor defaulted in the class body to the implicit
2006 exception-specification until after we've parsed any NSDMI; see
2007 after_nsdmi_defaulted_late_checks. */;
2008 else
2010 tree eh_spec = get_defaulted_eh_spec (fn);
2011 if (!comp_except_specs (fn_spec, eh_spec, ce_normal))
2013 if (DECL_DEFAULTED_IN_CLASS_P (fn))
2014 DECL_DELETED_FN (fn) = true;
2015 else
2016 error ("function %q+D defaulted on its redeclaration "
2017 "with an exception-specification that differs from "
2018 "the implicit exception-specification %qX", fn, eh_spec);
2022 if (DECL_DEFAULTED_IN_CLASS_P (fn)
2023 && DECL_DECLARED_CONSTEXPR_P (implicit_fn))
2025 /* Hmm...should we do this for out-of-class too? Should it be OK to
2026 add constexpr later like inline, rather than requiring
2027 declarations to match? */
2028 DECL_DECLARED_CONSTEXPR_P (fn) = true;
2029 if (kind == sfk_constructor)
2030 TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
2033 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
2034 && DECL_DECLARED_CONSTEXPR_P (fn))
2036 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
2038 error ("explicitly defaulted function %q+D cannot be declared "
2039 "as constexpr because the implicit declaration is not "
2040 "constexpr:", fn);
2041 explain_implicit_non_constexpr (fn);
2043 DECL_DECLARED_CONSTEXPR_P (fn) = false;
2046 if (DECL_DELETED_FN (implicit_fn))
2047 DECL_DELETED_FN (fn) = 1;
2050 /* OK, we've parsed the NSDMI for class T, now we can check any explicit
2051 exception-specifications on functions defaulted in the class body. */
2053 void
2054 after_nsdmi_defaulted_late_checks (tree t)
2056 if (uses_template_parms (t))
2057 return;
2058 if (t == error_mark_node)
2059 return;
2060 for (tree fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
2061 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
2063 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2064 if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
2065 continue;
2067 tree eh_spec = get_defaulted_eh_spec (fn);
2068 if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)),
2069 eh_spec, ce_normal))
2070 DECL_DELETED_FN (fn) = true;
2074 /* Returns true iff FN can be explicitly defaulted, and gives any
2075 errors if defaulting FN is ill-formed. */
2077 bool
2078 defaultable_fn_check (tree fn)
2080 special_function_kind kind = sfk_none;
2082 if (template_parm_scope_p ())
2084 error ("a template cannot be defaulted");
2085 return false;
2088 if (DECL_CONSTRUCTOR_P (fn))
2090 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
2091 kind = sfk_constructor;
2092 else if (copy_fn_p (fn) > 0
2093 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
2094 == void_list_node))
2095 kind = sfk_copy_constructor;
2096 else if (move_fn_p (fn))
2097 kind = sfk_move_constructor;
2099 else if (DECL_DESTRUCTOR_P (fn))
2100 kind = sfk_destructor;
2101 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2102 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
2104 if (copy_fn_p (fn))
2105 kind = sfk_copy_assignment;
2106 else if (move_fn_p (fn))
2107 kind = sfk_move_assignment;
2110 if (kind == sfk_none)
2112 error ("%qD cannot be defaulted", fn);
2113 return false;
2115 else
2117 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
2118 t && t != void_list_node; t = TREE_CHAIN (t))
2119 if (TREE_PURPOSE (t))
2121 error ("defaulted function %q+D with default argument", fn);
2122 break;
2125 /* Avoid do_warn_unused_parameter warnings. */
2126 for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p))
2127 if (DECL_NAME (p))
2128 TREE_NO_WARNING (p) = 1;
2130 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn)))
2131 /* Defer checking. */;
2132 else if (!processing_template_decl)
2133 defaulted_late_check (fn);
2135 return true;
2139 /* Add an implicit declaration to TYPE for the kind of function
2140 indicated by SFK. Return the FUNCTION_DECL for the new implicit
2141 declaration. */
2143 tree
2144 lazily_declare_fn (special_function_kind sfk, tree type)
2146 tree fn;
2147 /* Whether or not the argument has a const reference type. */
2148 bool const_p = false;
2150 switch (sfk)
2152 case sfk_constructor:
2153 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
2154 break;
2155 case sfk_copy_constructor:
2156 const_p = TYPE_HAS_CONST_COPY_CTOR (type);
2157 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
2158 break;
2159 case sfk_move_constructor:
2160 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
2161 break;
2162 case sfk_copy_assignment:
2163 const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
2164 CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
2165 break;
2166 case sfk_move_assignment:
2167 CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
2168 break;
2169 case sfk_destructor:
2170 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
2171 break;
2172 default:
2173 gcc_unreachable ();
2176 /* Declare the function. */
2177 fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL);
2179 /* [class.copy]/8 If the class definition declares a move constructor or
2180 move assignment operator, the implicitly declared copy constructor is
2181 defined as deleted.... */
2182 if ((sfk == sfk_copy_assignment
2183 || sfk == sfk_copy_constructor)
2184 && (type_has_user_declared_move_constructor (type)
2185 || type_has_user_declared_move_assign (type)))
2186 DECL_DELETED_FN (fn) = true;
2188 /* A destructor may be virtual. */
2189 if (sfk == sfk_destructor
2190 || sfk == sfk_move_assignment
2191 || sfk == sfk_copy_assignment)
2192 check_for_override (fn, type);
2193 /* Add it to CLASSTYPE_METHOD_VEC. */
2194 add_method (type, fn, NULL_TREE);
2195 /* Add it to TYPE_METHODS. */
2196 if (sfk == sfk_destructor
2197 && DECL_VIRTUAL_P (fn))
2198 /* The ABI requires that a virtual destructor go at the end of the
2199 vtable. */
2200 TYPE_METHODS (type) = chainon (TYPE_METHODS (type), fn);
2201 else
2203 DECL_CHAIN (fn) = TYPE_METHODS (type);
2204 TYPE_METHODS (type) = fn;
2206 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
2207 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
2208 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
2209 /* Create appropriate clones. */
2210 clone_function_decl (fn, /*update_method_vec=*/true);
2212 return fn;
2215 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
2216 as there are artificial parms in FN. */
2218 tree
2219 skip_artificial_parms_for (const_tree fn, tree list)
2221 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2222 list = TREE_CHAIN (list);
2223 else
2224 return list;
2226 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2227 list = TREE_CHAIN (list);
2228 if (DECL_HAS_VTT_PARM_P (fn))
2229 list = TREE_CHAIN (list);
2230 return list;
2233 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
2234 artificial parms in FN. */
2237 num_artificial_parms_for (const_tree fn)
2239 int count = 0;
2241 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2242 count++;
2243 else
2244 return 0;
2246 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2247 count++;
2248 if (DECL_HAS_VTT_PARM_P (fn))
2249 count++;
2250 return count;
2254 #include "gt-cp-method.h"