1 /* Handle the hair of processing (but not expanding) inline functions.
2 Also manage function and variable name overloading.
3 Copyright (C) 1987-2013 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)
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. */
26 #include "coretypes.h"
29 #include "stringpool.h"
36 #include "common/common-target.h"
37 #include "diagnostic.h"
39 #include "pointer-set.h"
41 /* Various flags to control the mangling process. */
47 /* The thing we are presently mangling is part of a template type,
48 rather than a fully instantiated type. Therefore, we may see
49 complex expressions where we would normally expect to see a
50 simple integer constant. */
51 mf_maybe_uninstantiated = 1,
52 /* When mangling a numeric value, use the form `_XX_' (instead of
53 just `XX') if the value has more than one digit. */
54 mf_use_underscores_around_value = 2
57 typedef enum mangling_flags mangling_flags;
59 static void do_build_copy_assign (tree);
60 static void do_build_copy_constructor (tree);
61 static tree make_alias_for_thunk (tree);
63 /* Called once to initialize method.c. */
71 /* Return a this or result adjusting thunk to FUNCTION. THIS_ADJUSTING
72 indicates whether it is a this or result adjusting thunk.
73 FIXED_OFFSET and VIRTUAL_OFFSET indicate how to do the adjustment
74 (see thunk_adjust). VIRTUAL_OFFSET can be NULL, but FIXED_OFFSET
75 never is. VIRTUAL_OFFSET is the /index/ into the vtable for this
76 adjusting thunks, we scale it to a byte offset. For covariant
77 thunks VIRTUAL_OFFSET is the virtual binfo. You must post process
78 the returned thunk with finish_thunk. */
81 make_thunk (tree function, bool this_adjusting,
82 tree fixed_offset, tree virtual_offset)
87 gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
88 /* We can have this thunks to covariant thunks, but not vice versa. */
89 gcc_assert (!DECL_THIS_THUNK_P (function));
90 gcc_assert (!DECL_RESULT_THUNK_P (function) || this_adjusting);
92 /* Scale the VIRTUAL_OFFSET to be in terms of bytes. */
93 if (this_adjusting && virtual_offset)
95 = size_binop (MULT_EXPR,
98 TYPE_SIZE_UNIT (vtable_entry_type)));
100 d = tree_to_shwi (fixed_offset);
102 /* See if we already have the thunk in question. For this_adjusting
103 thunks VIRTUAL_OFFSET will be an INTEGER_CST, for covariant thunks it
105 for (thunk = DECL_THUNKS (function); thunk; thunk = DECL_CHAIN (thunk))
106 if (DECL_THIS_THUNK_P (thunk) == this_adjusting
107 && THUNK_FIXED_OFFSET (thunk) == d
108 && !virtual_offset == !THUNK_VIRTUAL_OFFSET (thunk)
111 ? tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk),
113 : THUNK_VIRTUAL_OFFSET (thunk) == virtual_offset)))
116 /* All thunks must be created before FUNCTION is actually emitted;
117 the ABI requires that all thunks be emitted together with the
118 function to which they transfer control. */
119 gcc_assert (!TREE_ASM_WRITTEN (function));
120 /* Likewise, we can only be adding thunks to a function declared in
121 the class currently being laid out. */
122 gcc_assert (TYPE_SIZE (DECL_CONTEXT (function))
123 && TYPE_BEING_DEFINED (DECL_CONTEXT (function)));
125 thunk = build_decl (DECL_SOURCE_LOCATION (function),
126 FUNCTION_DECL, NULL_TREE, TREE_TYPE (function));
127 DECL_LANG_SPECIFIC (thunk) = DECL_LANG_SPECIFIC (function);
128 cxx_dup_lang_specific_decl (thunk);
129 DECL_VIRTUAL_P (thunk) = true;
130 SET_DECL_THUNKS (thunk, NULL_TREE);
132 DECL_CONTEXT (thunk) = DECL_CONTEXT (function);
133 TREE_READONLY (thunk) = TREE_READONLY (function);
134 TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (function);
135 TREE_PUBLIC (thunk) = TREE_PUBLIC (function);
136 SET_DECL_THUNK_P (thunk, this_adjusting);
137 THUNK_TARGET (thunk) = function;
138 THUNK_FIXED_OFFSET (thunk) = d;
139 THUNK_VIRTUAL_OFFSET (thunk) = virtual_offset;
140 THUNK_ALIAS (thunk) = NULL_TREE;
142 DECL_INTERFACE_KNOWN (thunk) = 1;
143 DECL_NOT_REALLY_EXTERN (thunk) = 1;
144 DECL_COMDAT (thunk) = DECL_COMDAT (function);
145 DECL_SAVED_FUNCTION_DATA (thunk) = NULL;
146 /* The thunk itself is not a constructor or destructor, even if
147 the thing it is thunking to is. */
148 DECL_DESTRUCTOR_P (thunk) = 0;
149 DECL_CONSTRUCTOR_P (thunk) = 0;
150 DECL_EXTERNAL (thunk) = 1;
151 DECL_ARTIFICIAL (thunk) = 1;
152 /* The THUNK is not a pending inline, even if the FUNCTION is. */
153 DECL_PENDING_INLINE_P (thunk) = 0;
154 DECL_DECLARED_INLINE_P (thunk) = 0;
155 /* Nor is it a template instantiation. */
156 DECL_USE_TEMPLATE (thunk) = 0;
157 DECL_TEMPLATE_INFO (thunk) = NULL;
159 /* Add it to the list of thunks associated with FUNCTION. */
160 DECL_CHAIN (thunk) = DECL_THUNKS (function);
161 SET_DECL_THUNKS (function, thunk);
166 /* Finish THUNK, a thunk decl. */
169 finish_thunk (tree thunk)
172 tree fixed_offset = ssize_int (THUNK_FIXED_OFFSET (thunk));
173 tree virtual_offset = THUNK_VIRTUAL_OFFSET (thunk);
175 gcc_assert (!DECL_NAME (thunk) && DECL_THUNK_P (thunk));
176 if (virtual_offset && DECL_RESULT_THUNK_P (thunk))
177 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
178 function = THUNK_TARGET (thunk);
179 name = mangle_thunk (function, DECL_THIS_THUNK_P (thunk),
180 fixed_offset, virtual_offset);
182 /* We can end up with declarations of (logically) different
183 covariant thunks, that do identical adjustments. The two thunks
184 will be adjusting between within different hierarchies, which
185 happen to have the same layout. We must nullify one of them to
186 refer to the other. */
187 if (DECL_RESULT_THUNK_P (thunk))
191 for (cov_probe = DECL_THUNKS (function);
192 cov_probe; cov_probe = DECL_CHAIN (cov_probe))
193 if (DECL_NAME (cov_probe) == name)
195 gcc_assert (!DECL_THUNKS (thunk));
196 THUNK_ALIAS (thunk) = (THUNK_ALIAS (cov_probe)
197 ? THUNK_ALIAS (cov_probe) : cov_probe);
202 DECL_NAME (thunk) = name;
203 SET_DECL_ASSEMBLER_NAME (thunk, name);
206 static GTY (()) int thunk_labelno;
208 /* Create a static alias to target. */
211 make_alias_for (tree target, tree newid)
213 tree alias = build_decl (DECL_SOURCE_LOCATION (target),
214 TREE_CODE (target), newid, TREE_TYPE (target));
215 DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (target);
216 cxx_dup_lang_specific_decl (alias);
217 DECL_CONTEXT (alias) = NULL;
218 TREE_READONLY (alias) = TREE_READONLY (target);
219 TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (target);
220 TREE_PUBLIC (alias) = 0;
221 DECL_INTERFACE_KNOWN (alias) = 1;
222 if (DECL_LANG_SPECIFIC (alias))
224 DECL_NOT_REALLY_EXTERN (alias) = 1;
225 DECL_USE_TEMPLATE (alias) = 0;
226 DECL_TEMPLATE_INFO (alias) = NULL;
228 DECL_EXTERNAL (alias) = 0;
229 DECL_ARTIFICIAL (alias) = 1;
230 DECL_TEMPLATE_INSTANTIATED (alias) = 0;
231 if (TREE_CODE (alias) == FUNCTION_DECL)
233 DECL_SAVED_FUNCTION_DATA (alias) = NULL;
234 DECL_DESTRUCTOR_P (alias) = 0;
235 DECL_CONSTRUCTOR_P (alias) = 0;
236 DECL_PENDING_INLINE_P (alias) = 0;
237 DECL_DECLARED_INLINE_P (alias) = 0;
238 DECL_INITIAL (alias) = error_mark_node;
239 DECL_ARGUMENTS (alias) = copy_list (DECL_ARGUMENTS (target));
242 TREE_STATIC (alias) = 1;
243 TREE_ADDRESSABLE (alias) = 1;
244 TREE_USED (alias) = 1;
245 SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
250 make_alias_for_thunk (tree function)
255 targetm.asm_out.generate_internal_label (buf, "LTHUNK", thunk_labelno);
258 alias = make_alias_for (function, get_identifier (buf));
260 if (!flag_syntax_only)
262 struct cgraph_node *funcn, *aliasn;
263 funcn = cgraph_get_node (function);
264 gcc_checking_assert (funcn);
265 aliasn = cgraph_same_body_alias (funcn, alias, function);
266 DECL_ASSEMBLER_NAME (function);
267 gcc_assert (aliasn != NULL);
273 /* Emit the definition of a C++ multiple inheritance or covariant
274 return vtable thunk. If EMIT_P is nonzero, the thunk is emitted
278 use_thunk (tree thunk_fndecl, bool emit_p)
280 tree a, t, function, alias;
282 HOST_WIDE_INT fixed_offset, virtual_value;
283 bool this_adjusting = DECL_THIS_THUNK_P (thunk_fndecl);
284 struct cgraph_node *funcn, *thunk_node;
286 /* We should have called finish_thunk to give it a name. */
287 gcc_assert (DECL_NAME (thunk_fndecl));
289 /* We should never be using an alias, always refer to the
291 gcc_assert (!THUNK_ALIAS (thunk_fndecl));
293 if (TREE_ASM_WRITTEN (thunk_fndecl))
296 function = THUNK_TARGET (thunk_fndecl);
297 if (DECL_RESULT (thunk_fndecl))
298 /* We already turned this thunk into an ordinary function.
299 There's no need to process this thunk again. */
302 if (DECL_THUNK_P (function))
303 /* The target is itself a thunk, process it now. */
304 use_thunk (function, emit_p);
306 /* Thunks are always addressable; they only appear in vtables. */
307 TREE_ADDRESSABLE (thunk_fndecl) = 1;
309 /* Figure out what function is being thunked to. It's referenced in
310 this translation unit. */
311 TREE_ADDRESSABLE (function) = 1;
312 mark_used (function);
316 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function))
317 alias = make_alias_for_thunk (function);
321 fixed_offset = THUNK_FIXED_OFFSET (thunk_fndecl);
322 virtual_offset = THUNK_VIRTUAL_OFFSET (thunk_fndecl);
327 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
328 virtual_value = tree_to_shwi (virtual_offset);
329 gcc_assert (virtual_value);
334 /* And, if we need to emit the thunk, it's used. */
335 mark_used (thunk_fndecl);
336 /* This thunk is actually defined. */
337 DECL_EXTERNAL (thunk_fndecl) = 0;
338 /* The linkage of the function may have changed. FIXME in linkage
340 gcc_assert (DECL_INTERFACE_KNOWN (function));
341 TREE_PUBLIC (thunk_fndecl) = TREE_PUBLIC (function);
342 DECL_VISIBILITY (thunk_fndecl) = DECL_VISIBILITY (function);
343 DECL_VISIBILITY_SPECIFIED (thunk_fndecl)
344 = DECL_VISIBILITY_SPECIFIED (function);
345 DECL_COMDAT (thunk_fndecl) = DECL_COMDAT (function);
346 DECL_WEAK (thunk_fndecl) = DECL_WEAK (function);
348 if (flag_syntax_only)
350 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
354 push_to_top_level ();
356 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function)
357 && targetm_common.have_named_sections)
359 resolve_unique_section (function, 0, flag_function_sections);
361 if (DECL_SECTION_NAME (function) != NULL && DECL_ONE_ONLY (function))
363 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
365 /* Output the thunk into the same section as function. */
366 DECL_SECTION_NAME (thunk_fndecl) = DECL_SECTION_NAME (function);
370 /* Set up cloned argument trees for the thunk. */
372 for (a = DECL_ARGUMENTS (function); a; a = DECL_CHAIN (a))
374 tree x = copy_node (a);
376 DECL_CONTEXT (x) = thunk_fndecl;
377 SET_DECL_RTL (x, NULL);
378 DECL_HAS_VALUE_EXPR_P (x) = 0;
379 TREE_ADDRESSABLE (x) = 0;
383 DECL_ARGUMENTS (thunk_fndecl) = a;
384 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
385 funcn = cgraph_get_node (function);
386 gcc_checking_assert (funcn);
387 thunk_node = cgraph_add_thunk (funcn, thunk_fndecl, function,
388 this_adjusting, fixed_offset, virtual_value,
389 virtual_offset, alias);
390 if (DECL_ONE_ONLY (function))
391 symtab_add_to_same_comdat_group (thunk_node,
395 || !targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
396 virtual_value, alias))
398 /* If this is a covariant thunk, or we don't have the necessary
399 code for efficient thunks, generate a thunk function that
400 just makes a call to the real function. Unfortunately, this
401 doesn't work for varargs. */
403 if (varargs_function_p (function))
404 error ("generic thunk code fails for method %q#D which uses %<...%>",
408 pop_from_top_level ();
411 /* Code for synthesizing methods which have default semantics defined. */
413 /* True iff CTYPE has a trivial SFK. */
416 type_has_trivial_fn (tree ctype, special_function_kind sfk)
420 case sfk_constructor:
421 return !TYPE_HAS_COMPLEX_DFLT (ctype);
422 case sfk_copy_constructor:
423 return !TYPE_HAS_COMPLEX_COPY_CTOR (ctype);
424 case sfk_move_constructor:
425 return !TYPE_HAS_COMPLEX_MOVE_CTOR (ctype);
426 case sfk_copy_assignment:
427 return !TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype);
428 case sfk_move_assignment:
429 return !TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype);
431 return !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype);
432 case sfk_inheriting_constructor:
439 /* Note that CTYPE has a non-trivial SFK even though we previously thought
443 type_set_nontrivial_flag (tree ctype, special_function_kind sfk)
447 case sfk_constructor:
448 TYPE_HAS_COMPLEX_DFLT (ctype) = true;
450 case sfk_copy_constructor:
451 TYPE_HAS_COMPLEX_COPY_CTOR (ctype) = true;
453 case sfk_move_constructor:
454 TYPE_HAS_COMPLEX_MOVE_CTOR (ctype) = true;
456 case sfk_copy_assignment:
457 TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype) = true;
459 case sfk_move_assignment:
460 TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype) = true;
463 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
465 case sfk_inheriting_constructor:
471 /* True iff FN is a trivial defaulted member function ([cd]tor, op=). */
474 trivial_fn_p (tree fn)
476 if (!DECL_DEFAULTED_FN (fn))
479 /* If fn is a clone, get the primary variant. */
480 fn = DECL_ORIGIN (fn);
481 return type_has_trivial_fn (DECL_CONTEXT (fn), special_function_p (fn));
484 /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO
485 given the parameter or parameters PARM, possibly inherited constructor
486 base INH, or move flag MOVE_P. */
489 add_one_base_init (tree binfo, tree parm, bool move_p, tree inh,
490 tree member_init_list)
495 /* An inheriting constructor only has a mem-initializer for
496 the base it inherits from. */
497 if (BINFO_TYPE (binfo) != inh)
498 return member_init_list;
502 for (; parm; parm = DECL_CHAIN (parm))
504 tree exp = convert_from_reference (parm);
505 if (TREE_CODE (TREE_TYPE (parm)) != REFERENCE_TYPE
506 || TYPE_REF_IS_RVALUE (TREE_TYPE (parm)))
508 *p = build_tree_list (NULL_TREE, exp);
509 p = &TREE_CHAIN (*p);
514 init = build_base_path (PLUS_EXPR, parm, binfo, 1,
515 tf_warning_or_error);
518 init = build_tree_list (NULL_TREE, init);
520 return tree_cons (binfo, init, member_init_list);
523 /* Generate code for default X(X&) or X(X&&) constructor or an inheriting
527 do_build_copy_constructor (tree fndecl)
529 tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
530 bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
531 bool trivial = trivial_fn_p (fndecl);
532 tree inh = DECL_INHERITED_CTOR_BASE (fndecl);
535 parm = convert_from_reference (parm);
538 && is_empty_class (current_class_type))
539 /* Don't copy the padding byte; it might not have been allocated
540 if *this is a base subobject. */;
543 tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm);
544 finish_expr_stmt (t);
548 tree fields = TYPE_FIELDS (current_class_type);
549 tree member_init_list = NULL_TREE;
550 int cvquals = cp_type_quals (TREE_TYPE (parm));
552 tree binfo, base_binfo;
554 vec<tree, va_gc> *vbases;
556 /* Initialize all the base-classes with the parameter converted
557 to their type so that we get their copy constructor and not
558 another constructor that takes current_class_type. We must
559 deal with the binfo's directly as a direct base might be
560 inaccessible due to ambiguity. */
561 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
562 vec_safe_iterate (vbases, i, &binfo); i++)
564 member_init_list = add_one_base_init (binfo, parm, move_p, inh,
568 for (binfo = TYPE_BINFO (current_class_type), i = 0;
569 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
571 if (BINFO_VIRTUAL_P (base_binfo))
573 member_init_list = add_one_base_init (base_binfo, parm, move_p,
574 inh, member_init_list);
577 for (; fields; fields = DECL_CHAIN (fields))
582 if (TREE_CODE (field) != FIELD_DECL)
587 expr_type = TREE_TYPE (field);
588 if (DECL_NAME (field))
590 if (VFIELD_NAME_P (DECL_NAME (field)))
593 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
594 /* Just use the field; anonymous types can't have
595 nontrivial copy ctors or assignment ops or this
596 function would be deleted. */;
600 /* Compute the type of "init->field". If the copy-constructor
601 parameter is, for example, "const S&", and the type of
602 the field is "T", then the type will usually be "const
603 T". (There are no cv-qualified variants of reference
605 if (TREE_CODE (expr_type) != REFERENCE_TYPE)
609 if (DECL_MUTABLE_P (field))
610 quals &= ~TYPE_QUAL_CONST;
611 quals |= cp_type_quals (expr_type);
612 expr_type = cp_build_qualified_type (expr_type, quals);
615 init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
616 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
617 /* 'move' breaks bit-fields, and has no effect for scalars. */
618 && !scalarish_type_p (expr_type))
620 init = build_tree_list (NULL_TREE, init);
622 member_init_list = tree_cons (field, init, member_init_list);
624 finish_mem_initializers (member_init_list);
629 do_build_copy_assign (tree fndecl)
631 tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl));
633 bool move_p = move_fn_p (fndecl);
634 bool trivial = trivial_fn_p (fndecl);
635 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
637 compound_stmt = begin_compound_stmt (0);
638 parm = convert_from_reference (parm);
641 && is_empty_class (current_class_type))
642 /* Don't copy the padding byte; it might not have been allocated
643 if *this is a base subobject. */;
646 tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
647 finish_expr_stmt (t);
652 int cvquals = cp_type_quals (TREE_TYPE (parm));
654 tree binfo, base_binfo;
656 /* Assign to each of the direct base classes. */
657 for (binfo = TYPE_BINFO (current_class_type), i = 0;
658 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
661 vec<tree, va_gc> *parmvec;
663 /* We must convert PARM directly to the base class
664 explicitly since the base class may be ambiguous. */
665 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
666 tf_warning_or_error);
668 converted_parm = move (converted_parm);
669 /* Call the base class assignment operator. */
670 parmvec = make_tree_vector_single (converted_parm);
672 (build_special_member_call (current_class_ref,
673 ansi_assopname (NOP_EXPR),
677 tf_warning_or_error));
678 release_tree_vector (parmvec);
681 /* Assign to each of the non-static data members. */
682 for (fields = TYPE_FIELDS (current_class_type);
684 fields = DECL_CHAIN (fields))
686 tree comp = current_class_ref;
692 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
695 expr_type = TREE_TYPE (field);
697 if (CP_TYPE_CONST_P (expr_type))
699 error ("non-static const member %q#D, can%'t use default "
700 "assignment operator", field);
703 else if (TREE_CODE (expr_type) == REFERENCE_TYPE)
705 error ("non-static reference member %q#D, can%'t use "
706 "default assignment operator", field);
710 if (DECL_NAME (field))
712 if (VFIELD_NAME_P (DECL_NAME (field)))
715 else if (ANON_AGGR_TYPE_P (expr_type)
716 && TYPE_FIELDS (expr_type) != NULL_TREE)
717 /* Just use the field; anonymous types can't have
718 nontrivial copy ctors or assignment ops or this
719 function would be deleted. */;
723 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
725 /* Compute the type of init->field */
727 if (DECL_MUTABLE_P (field))
728 quals &= ~TYPE_QUAL_CONST;
729 expr_type = cp_build_qualified_type (expr_type, quals);
731 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
732 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
733 /* 'move' breaks bit-fields, and has no effect for scalars. */
734 && !scalarish_type_p (expr_type))
737 if (DECL_NAME (field))
738 init = cp_build_modify_expr (comp, NOP_EXPR, init,
739 tf_warning_or_error);
741 init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
742 finish_expr_stmt (init);
745 finish_return_stmt (current_class_ref);
746 finish_compound_stmt (compound_stmt);
749 /* Synthesize FNDECL, a non-static member function. */
752 synthesize_method (tree fndecl)
754 bool nested = (current_function_decl != NULL_TREE);
755 tree context = decl_function_context (fndecl);
756 bool need_body = true;
758 location_t save_input_location = input_location;
759 int error_count = errorcount;
760 int warning_count = warningcount + werrorcount;
762 /* Reset the source location, we might have been previously
763 deferred, and thus have saved where we were first needed. */
764 DECL_SOURCE_LOCATION (fndecl)
765 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
767 /* If we've been asked to synthesize a clone, just synthesize the
768 cloned function instead. Doing so will automatically fill in the
769 body for the clone. */
770 if (DECL_CLONED_FUNCTION_P (fndecl))
771 fndecl = DECL_CLONED_FUNCTION (fndecl);
773 /* We may be in the middle of deferred access check. Disable
775 push_deferring_access_checks (dk_no_deferred);
778 push_to_top_level ();
780 push_function_context ();
782 input_location = DECL_SOURCE_LOCATION (fndecl);
784 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
785 stmt = begin_function_body ();
787 if (DECL_OVERLOADED_OPERATOR_P (fndecl) == NOP_EXPR)
789 do_build_copy_assign (fndecl);
792 else if (DECL_CONSTRUCTOR_P (fndecl))
794 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
795 if (arg_chain != void_list_node)
796 do_build_copy_constructor (fndecl);
798 finish_mem_initializers (NULL_TREE);
801 /* If we haven't yet generated the body of the function, just
802 generate an empty compound statement. */
806 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
807 finish_compound_stmt (compound_stmt);
810 finish_function_body (stmt);
811 expand_or_defer_fn (finish_function (0));
813 input_location = save_input_location;
816 pop_from_top_level ();
818 pop_function_context ();
820 pop_deferring_access_checks ();
822 if (error_count != errorcount || warning_count != warningcount + werrorcount)
823 inform (input_location, "synthesized method %qD first required here ",
827 /* Build a reference to type TYPE with cv-quals QUALS, which is an
828 rvalue if RVALUE is true. */
831 build_stub_type (tree type, int quals, bool rvalue)
833 tree argtype = cp_build_qualified_type (type, quals);
834 return cp_build_reference_type (argtype, rvalue);
837 /* Build a dummy glvalue from dereferencing a dummy reference of type
841 build_stub_object (tree reftype)
843 tree stub = build1 (NOP_EXPR, reftype, integer_one_node);
844 return convert_from_reference (stub);
847 /* Determine which function will be called when looking up NAME in TYPE,
848 called with a single ARGTYPE argument, or no argument if ARGTYPE is
849 null. FLAGS and COMPLAIN are as for build_new_method_call.
851 Returns a FUNCTION_DECL if all is well.
852 Returns NULL_TREE if overload resolution failed.
853 Returns error_mark_node if the chosen function cannot be called. */
856 locate_fn_flags (tree type, tree name, tree argtype, int flags,
857 tsubst_flags_t complain)
859 tree ob, fn, fns, binfo, rval;
860 vec<tree, va_gc> *args;
863 binfo = TYPE_BINFO (type);
867 type = BINFO_TYPE (binfo);
870 ob = build_stub_object (cp_build_reference_type (type, false));
871 args = make_tree_vector ();
874 if (TREE_CODE (argtype) == TREE_LIST)
876 for (tree elt = argtype; elt != void_list_node;
877 elt = TREE_CHAIN (elt))
879 tree type = TREE_VALUE (elt);
880 if (TREE_CODE (type) != REFERENCE_TYPE)
881 type = cp_build_reference_type (type, /*rval*/true);
882 tree arg = build_stub_object (type);
883 vec_safe_push (args, arg);
888 tree arg = build_stub_object (argtype);
889 args->quick_push (arg);
893 fns = lookup_fnfields (binfo, name, 0);
894 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
896 release_tree_vector (args);
897 if (fn && rval == error_mark_node)
903 /* Locate the dtor of TYPE. */
906 get_dtor (tree type, tsubst_flags_t complain)
908 tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
909 LOOKUP_NORMAL, complain);
910 if (fn == error_mark_node)
915 /* Locate the default ctor of TYPE. */
918 locate_ctor (tree type)
922 push_deferring_access_checks (dk_no_check);
923 fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
924 LOOKUP_SPECULATIVE, tf_none);
925 pop_deferring_access_checks ();
926 if (fn == error_mark_node)
931 /* Likewise, but give any appropriate errors. */
934 get_default_ctor (tree type)
936 tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
937 LOOKUP_NORMAL, tf_warning_or_error);
938 if (fn == error_mark_node)
943 /* Locate the copy ctor of TYPE. */
946 get_copy_ctor (tree type, tsubst_flags_t complain)
948 int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
949 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
950 tree argtype = build_stub_type (type, quals, false);
951 tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
952 LOOKUP_NORMAL, complain);
953 if (fn == error_mark_node)
958 /* Locate the copy assignment operator of TYPE. */
961 get_copy_assign (tree type)
963 int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
964 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
965 tree argtype = build_stub_type (type, quals, false);
966 tree fn = locate_fn_flags (type, ansi_assopname (NOP_EXPR), argtype,
967 LOOKUP_NORMAL, tf_warning_or_error);
968 if (fn == error_mark_node)
973 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
974 DELETED_P or give an error message MSG with argument ARG. */
977 process_subob_fn (tree fn, tree *spec_p, bool *trivial_p,
978 bool *deleted_p, bool *constexpr_p,
981 if (!fn || fn == error_mark_node)
986 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
987 *spec_p = merge_exception_specifiers (*spec_p, raises, fn);
990 if (!trivial_fn_p (fn))
994 if (TREE_CODE (arg) == FIELD_DECL
995 && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
1000 error ("union member %q+D with non-trivial %qD", arg, fn);
1004 if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
1006 *constexpr_p = false;
1009 inform (0, "defaulted constructor calls non-constexpr "
1011 explain_invalid_constexpr_fn (fn);
1022 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
1026 walk_field_subobs (tree fields, tree fnname, special_function_kind sfk,
1027 int quals, bool copy_arg_p, bool move_p,
1028 bool assign_p, tree *spec_p, bool *trivial_p,
1029 bool *deleted_p, bool *constexpr_p,
1030 bool diag, int flags, tsubst_flags_t complain)
1033 for (field = fields; field; field = DECL_CHAIN (field))
1035 tree mem_type, argtype, rval;
1037 if (TREE_CODE (field) != FIELD_DECL
1038 || DECL_ARTIFICIAL (field))
1041 mem_type = strip_array_types (TREE_TYPE (field));
1045 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
1048 error ("non-static const member %q#D, can%'t use default "
1049 "assignment operator", field);
1051 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1054 error ("non-static reference member %q#D, can%'t use "
1055 "default assignment operator", field);
1060 if (bad && deleted_p)
1063 else if (sfk == sfk_constructor)
1067 if (DECL_INITIAL (field))
1069 if (diag && DECL_INITIAL (field) == error_mark_node)
1070 inform (0, "initializer for %q+#D is invalid", field);
1074 /* Core 1351: If the field has an NSDMI that could throw, the
1075 default constructor is noexcept(false). FIXME this is
1076 broken by deferred parsing and 1360 saying we can't lazily
1077 declare a non-trivial default constructor. Also this
1078 needs to do deferred instantiation. Disable until the
1079 conflict between 1351 and 1360 is resolved. */
1080 if (spec_p && !expr_noexcept_p (DECL_INITIAL (field), complain))
1081 *spec_p = noexcept_false_spec;
1084 /* Don't do the normal processing. */
1089 if (CP_TYPE_CONST_P (mem_type)
1090 && default_init_uninitialized_part (mem_type))
1093 error ("uninitialized non-static const member %q#D",
1097 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1100 error ("uninitialized non-static reference member %q#D",
1105 if (bad && deleted_p)
1108 /* For an implicitly-defined default constructor to be constexpr,
1109 every member must have a user-provided default constructor or
1110 an explicit initializer. */
1111 if (constexpr_p && !CLASS_TYPE_P (mem_type)
1112 && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE)
1114 *constexpr_p = false;
1116 inform (0, "defaulted default constructor does not "
1117 "initialize %q+#D", field);
1120 else if (sfk == sfk_copy_constructor)
1123 if (TREE_CODE (mem_type) == REFERENCE_TYPE
1124 && TYPE_REF_IS_RVALUE (mem_type))
1127 error ("copying non-static data member %q#D of rvalue "
1128 "reference type", field);
1134 if (!CLASS_TYPE_P (mem_type))
1137 if (ANON_AGGR_TYPE_P (mem_type))
1139 walk_field_subobs (TYPE_FIELDS (mem_type), fnname, sfk, quals,
1140 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1141 deleted_p, constexpr_p,
1142 diag, flags, complain);
1148 int mem_quals = cp_type_quals (mem_type) | quals;
1149 if (DECL_MUTABLE_P (field))
1150 mem_quals &= ~TYPE_QUAL_CONST;
1151 argtype = build_stub_type (mem_type, mem_quals, move_p);
1154 argtype = NULL_TREE;
1156 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
1158 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1159 constexpr_p, diag, field);
1163 /* The caller wants to generate an implicit declaration of SFK for CTYPE
1164 which is const if relevant and CONST_P is set. If spec_p, trivial_p and
1165 deleted_p are non-null, set their referent appropriately. If diag is
1166 true, we're either being called from maybe_explain_implicit_delete to
1167 give errors, or if constexpr_p is non-null, from
1168 explain_invalid_constexpr_fn. */
1171 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
1172 tree *spec_p, bool *trivial_p, bool *deleted_p,
1173 bool *constexpr_p, bool diag,
1174 tree inherited_base, tree inherited_parms)
1176 tree binfo, base_binfo, scope, fnname, rval, argtype;
1177 bool move_p, copy_arg_p, assign_p, expected_trivial, check_vdtor;
1178 vec<tree, va_gc> *vbases;
1179 int i, quals, flags;
1180 tsubst_flags_t complain;
1184 *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec);
1188 /* "The closure type associated with a lambda-expression has a deleted
1189 default constructor and a deleted copy assignment operator."
1190 This is diagnosed in maybe_explain_implicit_delete. */
1191 if (LAMBDA_TYPE_P (ctype)
1192 && (sfk == sfk_constructor
1193 || sfk == sfk_copy_assignment))
1204 check_vdtor = false;
1207 case sfk_move_assignment:
1208 case sfk_copy_assignment:
1210 fnname = ansi_assopname (NOP_EXPR);
1213 case sfk_destructor:
1215 /* The synthesized method will call base dtors, but check complete
1216 here to avoid having to deal with VTT. */
1217 fnname = complete_dtor_identifier;
1220 case sfk_constructor:
1221 case sfk_move_constructor:
1222 case sfk_copy_constructor:
1223 case sfk_inheriting_constructor:
1225 fnname = complete_ctor_identifier;
1232 gcc_assert ((sfk == sfk_inheriting_constructor)
1233 == (inherited_base != NULL_TREE));
1235 /* If that user-written default constructor would satisfy the
1236 requirements of a constexpr constructor (7.1.5), the
1237 implicitly-defined default constructor is constexpr. */
1239 *constexpr_p = ctor_p;
1244 case sfk_constructor:
1245 case sfk_destructor:
1246 case sfk_inheriting_constructor:
1250 case sfk_move_constructor:
1251 case sfk_move_assignment:
1253 case sfk_copy_constructor:
1254 case sfk_copy_assignment:
1262 expected_trivial = type_has_trivial_fn (ctype, sfk);
1264 *trivial_p = expected_trivial;
1266 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
1267 class versions and other properties of the type. But a subobject
1268 class can be trivially copyable and yet have overload resolution
1269 choose a template constructor for initialization, depending on
1270 rvalueness and cv-quals. And furthermore, a member in a base might
1271 be trivial but deleted or otherwise not callable. So we can't exit
1272 early in C++0x. The same considerations apply in C++98/03, but
1273 there the definition of triviality does not consider overload
1274 resolution, so a constructor can be trivial even if it would otherwise
1275 call a non-trivial constructor. */
1276 if (expected_trivial
1277 && (!copy_arg_p || cxx_dialect < cxx11))
1279 if (constexpr_p && sfk == sfk_constructor)
1281 bool cx = trivial_default_constructor_is_constexpr (ctype);
1283 if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
1284 /* A trivial constructor doesn't have any NSDMI. */
1285 inform (input_location, "defaulted default constructor does "
1286 "not initialize any non-static data member");
1288 if (!diag && cxx_dialect < cxx11)
1292 ++cp_unevaluated_operand;
1293 ++c_inhibit_evaluation_warnings;
1294 push_deferring_access_checks (dk_no_deferred);
1296 scope = push_scope (ctype);
1298 flags = LOOKUP_NORMAL|LOOKUP_SPECULATIVE;
1299 if (!inherited_base)
1300 flags |= LOOKUP_DEFAULTED;
1302 complain = diag ? tf_warning_or_error : tf_none;
1305 quals = TYPE_QUAL_CONST;
1307 quals = TYPE_UNQUALIFIED;
1308 argtype = NULL_TREE;
1310 for (binfo = TYPE_BINFO (ctype), i = 0;
1311 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
1313 tree basetype = BINFO_TYPE (base_binfo);
1315 if (!assign_p && BINFO_VIRTUAL_P (base_binfo))
1316 /* We'll handle virtual bases below. */
1320 argtype = build_stub_type (basetype, quals, move_p);
1321 else if (basetype == inherited_base)
1322 argtype = inherited_parms;
1323 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1325 argtype = NULL_TREE;
1327 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1328 constexpr_p, diag, basetype);
1331 /* In a constructor we also need to check the subobject
1332 destructors for cleanup of partially constructed objects. */
1333 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1334 NULL_TREE, flags, complain);
1335 /* Note that we don't pass down trivial_p; the subobject
1336 destructors don't affect triviality of the constructor. Nor
1337 do they affect constexpr-ness (a constant expression doesn't
1338 throw) or exception-specification (a throw from one of the
1339 dtors would be a double-fault). */
1340 process_subob_fn (rval, NULL, NULL,
1341 deleted_p, NULL, false,
1345 if (check_vdtor && type_has_virtual_destructor (basetype))
1347 rval = locate_fn_flags (ctype, ansi_opname (DELETE_EXPR),
1348 ptr_type_node, flags, complain);
1349 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
1350 to have a null rval (no class-specific op delete). */
1351 if (rval && rval == error_mark_node && deleted_p)
1353 check_vdtor = false;
1356 if (diag && assign_p && move_p
1357 && BINFO_VIRTUAL_P (base_binfo)
1358 && rval && TREE_CODE (rval) == FUNCTION_DECL
1359 && move_fn_p (rval) && !trivial_fn_p (rval)
1360 && vbase_has_user_provided_move_assign (basetype))
1361 warning (OPT_Wvirtual_move_assign,
1362 "defaulted move assignment for %qT calls a non-trivial "
1363 "move assignment operator for virtual base %qT",
1367 vbases = CLASSTYPE_VBASECLASSES (ctype);
1369 /* No virtual bases to worry about. */;
1373 *constexpr_p = false;
1374 FOR_EACH_VEC_ELT (*vbases, i, base_binfo)
1376 tree basetype = BINFO_TYPE (base_binfo);
1378 argtype = build_stub_type (basetype, quals, move_p);
1379 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1381 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1382 constexpr_p, diag, basetype);
1383 if (ctor_p && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype))
1385 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1386 NULL_TREE, flags, complain);
1387 process_subob_fn (rval, NULL, NULL,
1388 deleted_p, NULL, false,
1394 /* Now handle the non-static data members. */
1395 walk_field_subobs (TYPE_FIELDS (ctype), fnname, sfk, quals,
1396 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1397 deleted_p, constexpr_p,
1398 diag, flags, complain);
1400 walk_field_subobs (TYPE_FIELDS (ctype), complete_dtor_identifier,
1401 sfk_destructor, TYPE_UNQUALIFIED, false,
1402 false, false, NULL, NULL,
1404 false, flags, complain);
1408 pop_deferring_access_checks ();
1409 --cp_unevaluated_operand;
1410 --c_inhibit_evaluation_warnings;
1413 /* DECL is a deleted function. If it's implicitly deleted, explain why and
1414 return true; else return false. */
1417 maybe_explain_implicit_delete (tree decl)
1419 /* If decl is a clone, get the primary variant. */
1420 decl = DECL_ORIGIN (decl);
1421 gcc_assert (DECL_DELETED_FN (decl));
1422 if (DECL_DEFAULTED_FN (decl))
1424 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
1425 static struct pointer_set_t *explained;
1427 special_function_kind sfk;
1433 explained = pointer_set_create ();
1434 if (pointer_set_insert (explained, decl))
1437 sfk = special_function_p (decl);
1438 ctype = DECL_CONTEXT (decl);
1439 loc = input_location;
1440 input_location = DECL_SOURCE_LOCATION (decl);
1443 if (LAMBDA_TYPE_P (ctype))
1446 if (sfk == sfk_constructor)
1447 inform (DECL_SOURCE_LOCATION (decl),
1448 "a lambda closure type has a deleted default constructor");
1449 else if (sfk == sfk_copy_assignment)
1450 inform (DECL_SOURCE_LOCATION (decl),
1451 "a lambda closure type has a deleted copy assignment operator");
1455 else if (DECL_ARTIFICIAL (decl)
1456 && (sfk == sfk_copy_assignment
1457 || sfk == sfk_copy_constructor)
1458 && (type_has_user_declared_move_constructor (ctype)
1459 || type_has_user_declared_move_assign (ctype)))
1461 inform (0, "%q+#D is implicitly declared as deleted because %qT "
1462 "declares a move constructor or move assignment operator",
1468 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1469 tree parm_type = TREE_VALUE (parms);
1470 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1471 tree raises = NULL_TREE;
1472 bool deleted_p = false;
1473 tree scope = push_scope (ctype);
1475 synthesized_method_walk (ctype, sfk, const_p,
1476 &raises, NULL, &deleted_p, NULL, false,
1477 DECL_INHERITED_CTOR_BASE (decl), parms);
1480 inform (0, "%q+#D is implicitly deleted because the default "
1481 "definition would be ill-formed:", decl);
1482 synthesized_method_walk (ctype, sfk, const_p,
1483 NULL, NULL, NULL, NULL, true,
1484 DECL_INHERITED_CTOR_BASE (decl), parms);
1486 else if (!comp_except_specs
1487 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
1489 inform (DECL_SOURCE_LOCATION (decl), "%q#F is implicitly "
1490 "deleted because its exception-specification does not "
1491 "match the implicit exception-specification %qX",
1493 #ifdef ENABLE_CHECKING
1501 input_location = loc;
1507 /* DECL is a defaulted function which was declared constexpr. Explain why
1508 it can't be constexpr. */
1511 explain_implicit_non_constexpr (tree decl)
1513 tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl));
1514 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1516 synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
1517 special_function_p (decl), const_p,
1518 NULL, NULL, NULL, &dummy, true,
1519 NULL_TREE, NULL_TREE);
1522 /* DECL is an instantiation of an inheriting constructor template. Deduce
1523 the correct exception-specification and deletedness for this particular
1527 deduce_inheriting_ctor (tree decl)
1529 gcc_assert (DECL_INHERITED_CTOR_BASE (decl));
1531 bool trivial, constexpr_, deleted;
1532 synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor,
1533 false, &spec, &trivial, &deleted, &constexpr_,
1535 DECL_INHERITED_CTOR_BASE (decl),
1536 FUNCTION_FIRST_USER_PARMTYPE (decl));
1537 DECL_DELETED_FN (decl) = deleted;
1538 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
1541 /* Implicitly declare the special function indicated by KIND, as a
1542 member of TYPE. For copy constructors and assignment operators,
1543 CONST_P indicates whether these functions should take a const
1544 reference argument or a non-const reference. Returns the
1545 FUNCTION_DECL for the implicitly declared function. */
1548 implicitly_declare_fn (special_function_kind kind, tree type,
1549 bool const_p, tree inherited_ctor,
1550 tree inherited_parms)
1553 tree parameter_types = void_list_node;
1556 tree raises = empty_except_spec;
1557 tree rhs_parm_type = NULL_TREE;
1560 HOST_WIDE_INT saved_processing_template_decl;
1564 /* Because we create declarations for implicitly declared functions
1565 lazily, we may be creating the declaration for a member of TYPE
1566 while in some completely different context. However, TYPE will
1567 never be a dependent class (because we never want to do lookups
1568 for implicitly defined functions in a dependent class).
1569 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
1570 because we only create clones for constructors and destructors
1571 when not in a template. */
1572 gcc_assert (!dependent_type_p (type));
1573 saved_processing_template_decl = processing_template_decl;
1574 processing_template_decl = 0;
1576 type = TYPE_MAIN_VARIANT (type);
1578 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (type))
1580 if (kind == sfk_destructor)
1581 /* See comment in check_special_function_return_type. */
1582 return_type = build_pointer_type (void_type_node);
1584 return_type = build_pointer_type (type);
1587 return_type = void_type_node;
1591 case sfk_destructor:
1593 name = constructor_name (type);
1596 case sfk_constructor:
1597 /* Default constructor. */
1598 name = constructor_name (type);
1601 case sfk_copy_constructor:
1602 case sfk_copy_assignment:
1603 case sfk_move_constructor:
1604 case sfk_move_assignment:
1605 case sfk_inheriting_constructor:
1608 if (kind == sfk_copy_assignment
1609 || kind == sfk_move_assignment)
1611 return_type = build_reference_type (type);
1612 name = ansi_assopname (NOP_EXPR);
1615 name = constructor_name (type);
1617 if (kind == sfk_inheriting_constructor)
1618 parameter_types = inherited_parms;
1622 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
1624 rhs_parm_type = type;
1625 move_p = (kind == sfk_move_assignment
1626 || kind == sfk_move_constructor);
1627 rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
1629 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
1637 tree inherited_base = (inherited_ctor
1638 ? DECL_CONTEXT (inherited_ctor)
1640 bool trivial_p = false;
1642 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
1644 /* For an inheriting constructor template, just copy these flags from
1645 the inherited constructor template for now. */
1646 raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor));
1647 deleted_p = DECL_DELETED_FN (DECL_TEMPLATE_RESULT (inherited_ctor));
1649 = DECL_DECLARED_CONSTEXPR_P (DECL_TEMPLATE_RESULT (inherited_ctor));
1652 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
1653 &deleted_p, &constexpr_p, false,
1654 inherited_base, inherited_parms);
1655 /* Don't bother marking a deleted constructor as constexpr. */
1657 constexpr_p = false;
1658 /* A trivial copy/move constructor is also a constexpr constructor. */
1659 else if (trivial_p && cxx_dialect >= cxx11
1660 && (kind == sfk_copy_constructor
1661 || kind == sfk_move_constructor))
1662 gcc_assert (constexpr_p);
1664 if (!trivial_p && type_has_trivial_fn (type, kind))
1665 type_set_nontrivial_flag (type, kind);
1667 /* Create the function. */
1668 fn_type = build_method_type_directly (type, return_type, parameter_types);
1670 fn_type = build_exception_variant (fn_type, raises);
1671 fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
1672 if (kind != sfk_inheriting_constructor)
1673 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
1674 if (kind == sfk_constructor || kind == sfk_copy_constructor
1675 || kind == sfk_move_constructor || kind == sfk_inheriting_constructor)
1676 DECL_CONSTRUCTOR_P (fn) = 1;
1677 else if (kind == sfk_destructor)
1678 DECL_DESTRUCTOR_P (fn) = 1;
1681 DECL_ASSIGNMENT_OPERATOR_P (fn) = 1;
1682 SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR);
1685 /* If pointers to member functions use the least significant bit to
1686 indicate whether a function is virtual, ensure a pointer
1687 to this function will have that bit clear. */
1688 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
1689 && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
1690 DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
1692 /* Create the explicit arguments. */
1695 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
1696 want its type to be included in the mangled function
1698 tree decl = cp_build_parm_decl (NULL_TREE, rhs_parm_type);
1699 TREE_READONLY (decl) = 1;
1700 retrofit_lang_decl (decl);
1701 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
1702 DECL_ARGUMENTS (fn) = decl;
1704 else if (kind == sfk_inheriting_constructor)
1706 tree *p = &DECL_ARGUMENTS (fn);
1708 for (tree parm = inherited_parms; parm != void_list_node;
1709 parm = TREE_CHAIN (parm))
1711 *p = cp_build_parm_decl (NULL_TREE, TREE_VALUE (parm));
1712 retrofit_lang_decl (*p);
1713 DECL_PARM_LEVEL (*p) = 1;
1714 DECL_PARM_INDEX (*p) = index++;
1715 DECL_CONTEXT (*p) = fn;
1716 p = &DECL_CHAIN (*p);
1718 SET_DECL_INHERITED_CTOR_BASE (fn, inherited_base);
1719 DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor);
1720 /* A constructor so declared has the same access as the corresponding
1721 constructor in X. */
1722 TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor);
1723 TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor);
1724 /* Copy constexpr from the inherited constructor even if the
1725 inheriting constructor doesn't satisfy the requirements. */
1727 = DECL_DECLARED_CONSTEXPR_P (STRIP_TEMPLATE (inherited_ctor));
1729 /* Add the "this" parameter. */
1730 this_parm = build_this_parm (fn_type, TYPE_UNQUALIFIED);
1731 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
1732 DECL_ARGUMENTS (fn) = this_parm;
1734 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
1735 set_linkage_according_to_type (type, fn);
1736 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
1737 DECL_IN_AGGR_P (fn) = 1;
1738 DECL_ARTIFICIAL (fn) = 1;
1739 DECL_DEFAULTED_FN (fn) = 1;
1740 if (cxx_dialect >= cxx11)
1742 DECL_DELETED_FN (fn) = deleted_p;
1743 DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
1745 DECL_EXTERNAL (fn) = true;
1746 DECL_NOT_REALLY_EXTERN (fn) = 1;
1747 DECL_DECLARED_INLINE_P (fn) = 1;
1748 gcc_assert (!TREE_USED (fn));
1750 /* Restore PROCESSING_TEMPLATE_DECL. */
1751 processing_template_decl = saved_processing_template_decl;
1753 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
1754 fn = add_inherited_template_parms (fn, inherited_ctor);
1756 /* Warn about calling a non-trivial move assignment in a virtual base. */
1757 if (kind == sfk_move_assignment && !deleted_p && !trivial_p
1758 && CLASSTYPE_VBASECLASSES (type))
1760 location_t loc = input_location;
1761 input_location = DECL_SOURCE_LOCATION (fn);
1762 synthesized_method_walk (type, kind, const_p,
1763 NULL, NULL, NULL, NULL, true,
1764 NULL_TREE, NULL_TREE);
1765 input_location = loc;
1771 /* Gives any errors about defaulted functions which need to be deferred
1772 until the containing class is complete. */
1775 defaulted_late_check (tree fn)
1777 /* Complain about invalid signature for defaulted fn. */
1778 tree ctx = DECL_CONTEXT (fn);
1779 special_function_kind kind = special_function_p (fn);
1780 bool fn_const_p = (copy_fn_p (fn) == 2);
1781 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
1783 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
1785 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
1786 TREE_TYPE (TREE_TYPE (implicit_fn)))
1787 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1788 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn))))
1790 error ("defaulted declaration %q+D", fn);
1791 error_at (DECL_SOURCE_LOCATION (fn),
1792 "does not match expected signature %qD", implicit_fn);
1795 /* 8.4.2/2: An explicitly-defaulted function (...) may have an explicit
1796 exception-specification only if it is compatible (15.4) with the
1797 exception-specification on the implicit declaration. If a function
1798 is explicitly defaulted on its first declaration, (...) it is
1799 implicitly considered to have the same exception-specification as if
1800 it had been implicitly declared. */
1801 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
1803 maybe_instantiate_noexcept (fn);
1804 if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)),
1805 eh_spec, ce_normal))
1807 if (DECL_DEFAULTED_IN_CLASS_P (fn))
1809 DECL_DELETED_FN (fn) = true;
1810 eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
1813 error ("function %q+D defaulted on its redeclaration "
1814 "with an exception-specification that differs from "
1815 "the implicit declaration %q#D", fn, implicit_fn);
1818 if (DECL_DEFAULTED_IN_CLASS_P (fn))
1819 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
1821 if (DECL_DEFAULTED_IN_CLASS_P (fn)
1822 && DECL_DECLARED_CONSTEXPR_P (implicit_fn))
1824 /* Hmm...should we do this for out-of-class too? Should it be OK to
1825 add constexpr later like inline, rather than requiring
1826 declarations to match? */
1827 DECL_DECLARED_CONSTEXPR_P (fn) = true;
1828 if (kind == sfk_constructor)
1829 TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
1832 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
1833 && DECL_DECLARED_CONSTEXPR_P (fn))
1835 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
1837 error ("explicitly defaulted function %q+D cannot be declared "
1838 "as constexpr because the implicit declaration is not "
1840 explain_implicit_non_constexpr (fn);
1842 DECL_DECLARED_CONSTEXPR_P (fn) = false;
1845 if (DECL_DELETED_FN (implicit_fn))
1846 DECL_DELETED_FN (fn) = 1;
1849 /* Returns true iff FN can be explicitly defaulted, and gives any
1850 errors if defaulting FN is ill-formed. */
1853 defaultable_fn_check (tree fn)
1855 special_function_kind kind = sfk_none;
1857 if (template_parm_scope_p ())
1859 error ("a template cannot be defaulted");
1863 if (DECL_CONSTRUCTOR_P (fn))
1865 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
1866 kind = sfk_constructor;
1867 else if (copy_fn_p (fn) > 0
1868 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
1870 kind = sfk_copy_constructor;
1871 else if (move_fn_p (fn))
1872 kind = sfk_move_constructor;
1874 else if (DECL_DESTRUCTOR_P (fn))
1875 kind = sfk_destructor;
1876 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
1877 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
1880 kind = sfk_copy_assignment;
1881 else if (move_fn_p (fn))
1882 kind = sfk_move_assignment;
1885 if (kind == sfk_none)
1887 error ("%qD cannot be defaulted", fn);
1892 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
1893 t && t != void_list_node; t = TREE_CHAIN (t))
1894 if (TREE_PURPOSE (t))
1896 error ("defaulted function %q+D with default argument", fn);
1900 /* Avoid do_warn_unused_parameter warnings. */
1901 for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p))
1903 TREE_NO_WARNING (p) = 1;
1905 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn)))
1906 /* Defer checking. */;
1907 else if (!processing_template_decl)
1908 defaulted_late_check (fn);
1914 /* Add an implicit declaration to TYPE for the kind of function
1915 indicated by SFK. Return the FUNCTION_DECL for the new implicit
1919 lazily_declare_fn (special_function_kind sfk, tree type)
1922 /* Whether or not the argument has a const reference type. */
1923 bool const_p = false;
1927 case sfk_constructor:
1928 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
1930 case sfk_copy_constructor:
1931 const_p = TYPE_HAS_CONST_COPY_CTOR (type);
1932 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
1934 case sfk_move_constructor:
1935 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
1937 case sfk_copy_assignment:
1938 const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
1939 CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
1941 case sfk_move_assignment:
1942 CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
1944 case sfk_destructor:
1945 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
1951 /* Declare the function. */
1952 fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL);
1954 /* [class.copy]/8 If the class definition declares a move constructor or
1955 move assignment operator, the implicitly declared copy constructor is
1956 defined as deleted.... */
1957 if ((sfk == sfk_copy_assignment
1958 || sfk == sfk_copy_constructor)
1959 && (type_has_user_declared_move_constructor (type)
1960 || type_has_user_declared_move_assign (type)))
1961 DECL_DELETED_FN (fn) = true;
1963 /* A destructor may be virtual. */
1964 if (sfk == sfk_destructor
1965 || sfk == sfk_move_assignment
1966 || sfk == sfk_copy_assignment)
1967 check_for_override (fn, type);
1968 /* Add it to CLASSTYPE_METHOD_VEC. */
1969 add_method (type, fn, NULL_TREE);
1970 /* Add it to TYPE_METHODS. */
1971 if (sfk == sfk_destructor
1972 && DECL_VIRTUAL_P (fn)
1973 && abi_version_at_least (2))
1974 /* The ABI requires that a virtual destructor go at the end of the
1976 TYPE_METHODS (type) = chainon (TYPE_METHODS (type), fn);
1979 /* G++ 3.2 put the implicit destructor at the *beginning* of the
1980 TYPE_METHODS list, which cause the destructor to be emitted
1981 in an incorrect location in the vtable. */
1982 if (warn_abi && sfk == sfk_destructor && DECL_VIRTUAL_P (fn))
1983 warning (OPT_Wabi, "vtable layout for class %qT may not be ABI-compliant"
1984 "and may change in a future version of GCC due to "
1985 "implicit virtual destructor",
1987 DECL_CHAIN (fn) = TYPE_METHODS (type);
1988 TYPE_METHODS (type) = fn;
1990 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
1991 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
1992 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
1993 /* Create appropriate clones. */
1994 clone_function_decl (fn, /*update_method_vec=*/true);
1999 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
2000 as there are artificial parms in FN. */
2003 skip_artificial_parms_for (const_tree fn, tree list)
2005 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2006 list = TREE_CHAIN (list);
2010 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2011 list = TREE_CHAIN (list);
2012 if (DECL_HAS_VTT_PARM_P (fn))
2013 list = TREE_CHAIN (list);
2017 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
2018 artificial parms in FN. */
2021 num_artificial_parms_for (const_tree fn)
2025 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2030 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2032 if (DECL_HAS_VTT_PARM_P (fn))
2038 #include "gt-cp-method.h"