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"
34 #include "common/common-target.h"
35 #include "diagnostic.h"
39 /* Various flags to control the mangling process. */
45 /* The thing we are presently mangling is part of a template type,
46 rather than a fully instantiated type. Therefore, we may see
47 complex expressions where we would normally expect to see a
48 simple integer constant. */
49 mf_maybe_uninstantiated = 1,
50 /* When mangling a numeric value, use the form `_XX_' (instead of
51 just `XX') if the value has more than one digit. */
52 mf_use_underscores_around_value = 2
55 typedef enum mangling_flags mangling_flags;
57 static void do_build_copy_assign (tree);
58 static void do_build_copy_constructor (tree);
59 static tree make_alias_for_thunk (tree);
61 /* Called once to initialize method.c. */
69 /* Return a this or result adjusting thunk to FUNCTION. THIS_ADJUSTING
70 indicates whether it is a this or result adjusting thunk.
71 FIXED_OFFSET and VIRTUAL_OFFSET indicate how to do the adjustment
72 (see thunk_adjust). VIRTUAL_OFFSET can be NULL, but FIXED_OFFSET
73 never is. VIRTUAL_OFFSET is the /index/ into the vtable for this
74 adjusting thunks, we scale it to a byte offset. For covariant
75 thunks VIRTUAL_OFFSET is the virtual binfo. You must post process
76 the returned thunk with finish_thunk. */
79 make_thunk (tree function, bool this_adjusting,
80 tree fixed_offset, tree virtual_offset)
85 gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
86 /* We can have this thunks to covariant thunks, but not vice versa. */
87 gcc_assert (!DECL_THIS_THUNK_P (function));
88 gcc_assert (!DECL_RESULT_THUNK_P (function) || this_adjusting);
90 /* Scale the VIRTUAL_OFFSET to be in terms of bytes. */
91 if (this_adjusting && virtual_offset)
93 = size_binop (MULT_EXPR,
96 TYPE_SIZE_UNIT (vtable_entry_type)));
98 d = tree_low_cst (fixed_offset, 0);
100 /* See if we already have the thunk in question. For this_adjusting
101 thunks VIRTUAL_OFFSET will be an INTEGER_CST, for covariant thunks it
103 for (thunk = DECL_THUNKS (function); thunk; thunk = DECL_CHAIN (thunk))
104 if (DECL_THIS_THUNK_P (thunk) == this_adjusting
105 && THUNK_FIXED_OFFSET (thunk) == d
106 && !virtual_offset == !THUNK_VIRTUAL_OFFSET (thunk)
109 ? tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk),
111 : THUNK_VIRTUAL_OFFSET (thunk) == virtual_offset)))
114 /* All thunks must be created before FUNCTION is actually emitted;
115 the ABI requires that all thunks be emitted together with the
116 function to which they transfer control. */
117 gcc_assert (!TREE_ASM_WRITTEN (function));
118 /* Likewise, we can only be adding thunks to a function declared in
119 the class currently being laid out. */
120 gcc_assert (TYPE_SIZE (DECL_CONTEXT (function))
121 && TYPE_BEING_DEFINED (DECL_CONTEXT (function)));
123 thunk = build_decl (DECL_SOURCE_LOCATION (function),
124 FUNCTION_DECL, NULL_TREE, TREE_TYPE (function));
125 DECL_LANG_SPECIFIC (thunk) = DECL_LANG_SPECIFIC (function);
126 cxx_dup_lang_specific_decl (thunk);
127 DECL_VIRTUAL_P (thunk) = true;
128 SET_DECL_THUNKS (thunk, NULL_TREE);
130 DECL_CONTEXT (thunk) = DECL_CONTEXT (function);
131 TREE_READONLY (thunk) = TREE_READONLY (function);
132 TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (function);
133 TREE_PUBLIC (thunk) = TREE_PUBLIC (function);
134 SET_DECL_THUNK_P (thunk, this_adjusting);
135 THUNK_TARGET (thunk) = function;
136 THUNK_FIXED_OFFSET (thunk) = d;
137 THUNK_VIRTUAL_OFFSET (thunk) = virtual_offset;
138 THUNK_ALIAS (thunk) = NULL_TREE;
140 DECL_INTERFACE_KNOWN (thunk) = 1;
141 DECL_NOT_REALLY_EXTERN (thunk) = 1;
142 DECL_COMDAT (thunk) = DECL_COMDAT (function);
143 DECL_SAVED_FUNCTION_DATA (thunk) = NULL;
144 /* The thunk itself is not a constructor or destructor, even if
145 the thing it is thunking to is. */
146 DECL_DESTRUCTOR_P (thunk) = 0;
147 DECL_CONSTRUCTOR_P (thunk) = 0;
148 DECL_EXTERNAL (thunk) = 1;
149 DECL_ARTIFICIAL (thunk) = 1;
150 /* The THUNK is not a pending inline, even if the FUNCTION is. */
151 DECL_PENDING_INLINE_P (thunk) = 0;
152 DECL_DECLARED_INLINE_P (thunk) = 0;
153 /* Nor is it a template instantiation. */
154 DECL_USE_TEMPLATE (thunk) = 0;
155 DECL_TEMPLATE_INFO (thunk) = NULL;
157 /* Add it to the list of thunks associated with FUNCTION. */
158 DECL_CHAIN (thunk) = DECL_THUNKS (function);
159 SET_DECL_THUNKS (function, thunk);
164 /* Finish THUNK, a thunk decl. */
167 finish_thunk (tree thunk)
170 tree fixed_offset = ssize_int (THUNK_FIXED_OFFSET (thunk));
171 tree virtual_offset = THUNK_VIRTUAL_OFFSET (thunk);
173 gcc_assert (!DECL_NAME (thunk) && DECL_THUNK_P (thunk));
174 if (virtual_offset && DECL_RESULT_THUNK_P (thunk))
175 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
176 function = THUNK_TARGET (thunk);
177 name = mangle_thunk (function, DECL_THIS_THUNK_P (thunk),
178 fixed_offset, virtual_offset);
180 /* We can end up with declarations of (logically) different
181 covariant thunks, that do identical adjustments. The two thunks
182 will be adjusting between within different hierarchies, which
183 happen to have the same layout. We must nullify one of them to
184 refer to the other. */
185 if (DECL_RESULT_THUNK_P (thunk))
189 for (cov_probe = DECL_THUNKS (function);
190 cov_probe; cov_probe = DECL_CHAIN (cov_probe))
191 if (DECL_NAME (cov_probe) == name)
193 gcc_assert (!DECL_THUNKS (thunk));
194 THUNK_ALIAS (thunk) = (THUNK_ALIAS (cov_probe)
195 ? THUNK_ALIAS (cov_probe) : cov_probe);
200 DECL_NAME (thunk) = name;
201 SET_DECL_ASSEMBLER_NAME (thunk, name);
204 static GTY (()) int thunk_labelno;
206 /* Create a static alias to target. */
209 make_alias_for (tree target, tree newid)
211 tree alias = build_decl (DECL_SOURCE_LOCATION (target),
212 TREE_CODE (target), newid, TREE_TYPE (target));
213 DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (target);
214 cxx_dup_lang_specific_decl (alias);
215 DECL_CONTEXT (alias) = NULL;
216 TREE_READONLY (alias) = TREE_READONLY (target);
217 TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (target);
218 TREE_PUBLIC (alias) = 0;
219 DECL_INTERFACE_KNOWN (alias) = 1;
220 if (DECL_LANG_SPECIFIC (alias))
222 DECL_NOT_REALLY_EXTERN (alias) = 1;
223 DECL_USE_TEMPLATE (alias) = 0;
224 DECL_TEMPLATE_INFO (alias) = NULL;
226 DECL_EXTERNAL (alias) = 0;
227 DECL_ARTIFICIAL (alias) = 1;
228 DECL_TEMPLATE_INSTANTIATED (alias) = 0;
229 if (TREE_CODE (alias) == FUNCTION_DECL)
231 DECL_SAVED_FUNCTION_DATA (alias) = NULL;
232 DECL_DESTRUCTOR_P (alias) = 0;
233 DECL_CONSTRUCTOR_P (alias) = 0;
234 DECL_PENDING_INLINE_P (alias) = 0;
235 DECL_DECLARED_INLINE_P (alias) = 0;
236 DECL_INITIAL (alias) = error_mark_node;
237 DECL_ARGUMENTS (alias) = copy_list (DECL_ARGUMENTS (target));
240 TREE_STATIC (alias) = 1;
241 TREE_ADDRESSABLE (alias) = 1;
242 TREE_USED (alias) = 1;
243 SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
248 make_alias_for_thunk (tree function)
253 targetm.asm_out.generate_internal_label (buf, "LTHUNK", thunk_labelno);
256 alias = make_alias_for (function, get_identifier (buf));
258 if (!flag_syntax_only)
260 struct cgraph_node *funcn, *aliasn;
261 funcn = cgraph_get_node (function);
262 gcc_checking_assert (funcn);
263 aliasn = cgraph_same_body_alias (funcn, alias, function);
264 DECL_ASSEMBLER_NAME (function);
265 gcc_assert (aliasn != NULL);
271 /* Emit the definition of a C++ multiple inheritance or covariant
272 return vtable thunk. If EMIT_P is nonzero, the thunk is emitted
276 use_thunk (tree thunk_fndecl, bool emit_p)
278 tree a, t, function, alias;
280 HOST_WIDE_INT fixed_offset, virtual_value;
281 bool this_adjusting = DECL_THIS_THUNK_P (thunk_fndecl);
282 struct cgraph_node *funcn, *thunk_node;
284 /* We should have called finish_thunk to give it a name. */
285 gcc_assert (DECL_NAME (thunk_fndecl));
287 /* We should never be using an alias, always refer to the
289 gcc_assert (!THUNK_ALIAS (thunk_fndecl));
291 if (TREE_ASM_WRITTEN (thunk_fndecl))
294 function = THUNK_TARGET (thunk_fndecl);
295 if (DECL_RESULT (thunk_fndecl))
296 /* We already turned this thunk into an ordinary function.
297 There's no need to process this thunk again. */
300 if (DECL_THUNK_P (function))
301 /* The target is itself a thunk, process it now. */
302 use_thunk (function, emit_p);
304 /* Thunks are always addressable; they only appear in vtables. */
305 TREE_ADDRESSABLE (thunk_fndecl) = 1;
307 /* Figure out what function is being thunked to. It's referenced in
308 this translation unit. */
309 TREE_ADDRESSABLE (function) = 1;
310 mark_used (function);
314 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function))
315 alias = make_alias_for_thunk (function);
319 fixed_offset = THUNK_FIXED_OFFSET (thunk_fndecl);
320 virtual_offset = THUNK_VIRTUAL_OFFSET (thunk_fndecl);
325 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
326 virtual_value = tree_low_cst (virtual_offset, /*pos=*/0);
327 gcc_assert (virtual_value);
332 /* And, if we need to emit the thunk, it's used. */
333 mark_used (thunk_fndecl);
334 /* This thunk is actually defined. */
335 DECL_EXTERNAL (thunk_fndecl) = 0;
336 /* The linkage of the function may have changed. FIXME in linkage
338 gcc_assert (DECL_INTERFACE_KNOWN (function));
339 TREE_PUBLIC (thunk_fndecl) = TREE_PUBLIC (function);
340 DECL_VISIBILITY (thunk_fndecl) = DECL_VISIBILITY (function);
341 DECL_VISIBILITY_SPECIFIED (thunk_fndecl)
342 = DECL_VISIBILITY_SPECIFIED (function);
343 DECL_COMDAT (thunk_fndecl) = DECL_COMDAT (function);
344 DECL_WEAK (thunk_fndecl) = DECL_WEAK (function);
346 if (flag_syntax_only)
348 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
352 push_to_top_level ();
354 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function)
355 && targetm_common.have_named_sections)
357 resolve_unique_section (function, 0, flag_function_sections);
359 if (DECL_SECTION_NAME (function) != NULL && DECL_ONE_ONLY (function))
361 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
363 /* Output the thunk into the same section as function. */
364 DECL_SECTION_NAME (thunk_fndecl) = DECL_SECTION_NAME (function);
368 /* Set up cloned argument trees for the thunk. */
370 for (a = DECL_ARGUMENTS (function); a; a = DECL_CHAIN (a))
372 tree x = copy_node (a);
374 DECL_CONTEXT (x) = thunk_fndecl;
375 SET_DECL_RTL (x, NULL);
376 DECL_HAS_VALUE_EXPR_P (x) = 0;
377 TREE_ADDRESSABLE (x) = 0;
381 DECL_ARGUMENTS (thunk_fndecl) = a;
382 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
383 funcn = cgraph_get_node (function);
384 gcc_checking_assert (funcn);
385 thunk_node = cgraph_add_thunk (funcn, thunk_fndecl, function,
386 this_adjusting, fixed_offset, virtual_value,
387 virtual_offset, alias);
388 if (DECL_ONE_ONLY (function))
389 symtab_add_to_same_comdat_group ((symtab_node) thunk_node,
390 (symtab_node) funcn);
393 || !targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
394 virtual_value, alias))
396 /* If this is a covariant thunk, or we don't have the necessary
397 code for efficient thunks, generate a thunk function that
398 just makes a call to the real function. Unfortunately, this
399 doesn't work for varargs. */
401 if (varargs_function_p (function))
402 error ("generic thunk code fails for method %q#D which uses %<...%>",
406 pop_from_top_level ();
409 /* Code for synthesizing methods which have default semantics defined. */
411 /* True iff CTYPE has a trivial SFK. */
414 type_has_trivial_fn (tree ctype, special_function_kind sfk)
418 case sfk_constructor:
419 return !TYPE_HAS_COMPLEX_DFLT (ctype);
420 case sfk_copy_constructor:
421 return !TYPE_HAS_COMPLEX_COPY_CTOR (ctype);
422 case sfk_move_constructor:
423 return !TYPE_HAS_COMPLEX_MOVE_CTOR (ctype);
424 case sfk_copy_assignment:
425 return !TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype);
426 case sfk_move_assignment:
427 return !TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype);
429 return !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype);
430 case sfk_inheriting_constructor:
437 /* Note that CTYPE has a non-trivial SFK even though we previously thought
441 type_set_nontrivial_flag (tree ctype, special_function_kind sfk)
445 case sfk_constructor:
446 TYPE_HAS_COMPLEX_DFLT (ctype) = true;
448 case sfk_copy_constructor:
449 TYPE_HAS_COMPLEX_COPY_CTOR (ctype) = true;
451 case sfk_move_constructor:
452 TYPE_HAS_COMPLEX_MOVE_CTOR (ctype) = true;
454 case sfk_copy_assignment:
455 TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype) = true;
457 case sfk_move_assignment:
458 TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype) = true;
461 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
463 case sfk_inheriting_constructor:
469 /* True iff FN is a trivial defaulted member function ([cd]tor, op=). */
472 trivial_fn_p (tree fn)
474 if (!DECL_DEFAULTED_FN (fn))
477 /* If fn is a clone, get the primary variant. */
478 fn = DECL_ORIGIN (fn);
479 return type_has_trivial_fn (DECL_CONTEXT (fn), special_function_p (fn));
482 /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO
483 given the parameter or parameters PARM, possibly inherited constructor
484 base INH, or move flag MOVE_P. */
487 add_one_base_init (tree binfo, tree parm, bool move_p, tree inh,
488 tree member_init_list)
493 /* An inheriting constructor only has a mem-initializer for
494 the base it inherits from. */
495 if (BINFO_TYPE (binfo) != inh)
496 return member_init_list;
500 for (; parm; parm = DECL_CHAIN (parm))
502 tree exp = convert_from_reference (parm);
503 if (TREE_CODE (TREE_TYPE (parm)) != REFERENCE_TYPE
504 || TYPE_REF_IS_RVALUE (TREE_TYPE (parm)))
506 *p = build_tree_list (NULL_TREE, exp);
507 p = &TREE_CHAIN (*p);
512 init = build_base_path (PLUS_EXPR, parm, binfo, 1,
513 tf_warning_or_error);
516 init = build_tree_list (NULL_TREE, init);
518 return tree_cons (binfo, init, member_init_list);
521 /* Generate code for default X(X&) or X(X&&) constructor or an inheriting
525 do_build_copy_constructor (tree fndecl)
527 tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
528 bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
529 bool trivial = trivial_fn_p (fndecl);
530 tree inh = DECL_INHERITED_CTOR_BASE (fndecl);
533 parm = convert_from_reference (parm);
536 && is_empty_class (current_class_type))
537 /* Don't copy the padding byte; it might not have been allocated
538 if *this is a base subobject. */;
541 tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm);
542 finish_expr_stmt (t);
546 tree fields = TYPE_FIELDS (current_class_type);
547 tree member_init_list = NULL_TREE;
548 int cvquals = cp_type_quals (TREE_TYPE (parm));
550 tree binfo, base_binfo;
552 vec<tree, va_gc> *vbases;
554 /* Initialize all the base-classes with the parameter converted
555 to their type so that we get their copy constructor and not
556 another constructor that takes current_class_type. We must
557 deal with the binfo's directly as a direct base might be
558 inaccessible due to ambiguity. */
559 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
560 vec_safe_iterate (vbases, i, &binfo); i++)
562 member_init_list = add_one_base_init (binfo, parm, move_p, inh,
566 for (binfo = TYPE_BINFO (current_class_type), i = 0;
567 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
569 if (BINFO_VIRTUAL_P (base_binfo))
571 member_init_list = add_one_base_init (base_binfo, parm, move_p,
572 inh, member_init_list);
575 for (; fields; fields = DECL_CHAIN (fields))
580 if (TREE_CODE (field) != FIELD_DECL)
585 expr_type = TREE_TYPE (field);
586 if (DECL_NAME (field))
588 if (VFIELD_NAME_P (DECL_NAME (field)))
591 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
592 /* Just use the field; anonymous types can't have
593 nontrivial copy ctors or assignment ops or this
594 function would be deleted. */;
598 /* Compute the type of "init->field". If the copy-constructor
599 parameter is, for example, "const S&", and the type of
600 the field is "T", then the type will usually be "const
601 T". (There are no cv-qualified variants of reference
603 if (TREE_CODE (expr_type) != REFERENCE_TYPE)
607 if (DECL_MUTABLE_P (field))
608 quals &= ~TYPE_QUAL_CONST;
609 quals |= cp_type_quals (expr_type);
610 expr_type = cp_build_qualified_type (expr_type, quals);
613 init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
614 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
615 /* 'move' breaks bit-fields, and has no effect for scalars. */
616 && !scalarish_type_p (expr_type))
618 init = build_tree_list (NULL_TREE, init);
620 member_init_list = tree_cons (field, init, member_init_list);
622 finish_mem_initializers (member_init_list);
627 do_build_copy_assign (tree fndecl)
629 tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl));
631 bool move_p = move_fn_p (fndecl);
632 bool trivial = trivial_fn_p (fndecl);
633 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
635 compound_stmt = begin_compound_stmt (0);
636 parm = convert_from_reference (parm);
639 && is_empty_class (current_class_type))
640 /* Don't copy the padding byte; it might not have been allocated
641 if *this is a base subobject. */;
644 tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
645 finish_expr_stmt (t);
650 int cvquals = cp_type_quals (TREE_TYPE (parm));
652 tree binfo, base_binfo;
654 /* Assign to each of the direct base classes. */
655 for (binfo = TYPE_BINFO (current_class_type), i = 0;
656 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
659 vec<tree, va_gc> *parmvec;
661 /* We must convert PARM directly to the base class
662 explicitly since the base class may be ambiguous. */
663 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
664 tf_warning_or_error);
666 converted_parm = move (converted_parm);
667 /* Call the base class assignment operator. */
668 parmvec = make_tree_vector_single (converted_parm);
670 (build_special_member_call (current_class_ref,
671 ansi_assopname (NOP_EXPR),
675 tf_warning_or_error));
676 release_tree_vector (parmvec);
679 /* Assign to each of the non-static data members. */
680 for (fields = TYPE_FIELDS (current_class_type);
682 fields = DECL_CHAIN (fields))
684 tree comp = current_class_ref;
690 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
693 expr_type = TREE_TYPE (field);
695 if (CP_TYPE_CONST_P (expr_type))
697 error ("non-static const member %q#D, can%'t use default "
698 "assignment operator", field);
701 else if (TREE_CODE (expr_type) == REFERENCE_TYPE)
703 error ("non-static reference member %q#D, can%'t use "
704 "default assignment operator", field);
708 if (DECL_NAME (field))
710 if (VFIELD_NAME_P (DECL_NAME (field)))
713 else if (ANON_AGGR_TYPE_P (expr_type)
714 && TYPE_FIELDS (expr_type) != NULL_TREE)
715 /* Just use the field; anonymous types can't have
716 nontrivial copy ctors or assignment ops or this
717 function would be deleted. */;
721 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
723 /* Compute the type of init->field */
725 if (DECL_MUTABLE_P (field))
726 quals &= ~TYPE_QUAL_CONST;
727 expr_type = cp_build_qualified_type (expr_type, quals);
729 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
730 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
731 /* 'move' breaks bit-fields, and has no effect for scalars. */
732 && !scalarish_type_p (expr_type))
735 if (DECL_NAME (field))
736 init = cp_build_modify_expr (comp, NOP_EXPR, init,
737 tf_warning_or_error);
739 init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
740 finish_expr_stmt (init);
743 finish_return_stmt (current_class_ref);
744 finish_compound_stmt (compound_stmt);
747 /* Synthesize FNDECL, a non-static member function. */
750 synthesize_method (tree fndecl)
752 bool nested = (current_function_decl != NULL_TREE);
753 tree context = decl_function_context (fndecl);
754 bool need_body = true;
756 location_t save_input_location = input_location;
757 int error_count = errorcount;
758 int warning_count = warningcount;
760 /* Reset the source location, we might have been previously
761 deferred, and thus have saved where we were first needed. */
762 DECL_SOURCE_LOCATION (fndecl)
763 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
765 /* If we've been asked to synthesize a clone, just synthesize the
766 cloned function instead. Doing so will automatically fill in the
767 body for the clone. */
768 if (DECL_CLONED_FUNCTION_P (fndecl))
769 fndecl = DECL_CLONED_FUNCTION (fndecl);
771 /* We may be in the middle of deferred access check. Disable
773 push_deferring_access_checks (dk_no_deferred);
776 push_to_top_level ();
778 push_function_context ();
780 input_location = DECL_SOURCE_LOCATION (fndecl);
782 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
783 stmt = begin_function_body ();
785 if (DECL_OVERLOADED_OPERATOR_P (fndecl) == NOP_EXPR)
787 do_build_copy_assign (fndecl);
790 else if (DECL_CONSTRUCTOR_P (fndecl))
792 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
793 if (arg_chain != void_list_node)
794 do_build_copy_constructor (fndecl);
796 finish_mem_initializers (NULL_TREE);
799 /* If we haven't yet generated the body of the function, just
800 generate an empty compound statement. */
804 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
805 finish_compound_stmt (compound_stmt);
808 finish_function_body (stmt);
809 expand_or_defer_fn (finish_function (0));
811 input_location = save_input_location;
814 pop_from_top_level ();
816 pop_function_context ();
818 pop_deferring_access_checks ();
820 if (error_count != errorcount || warning_count != warningcount)
821 inform (input_location, "synthesized method %qD first required here ",
825 /* Build a reference to type TYPE with cv-quals QUALS, which is an
826 rvalue if RVALUE is true. */
829 build_stub_type (tree type, int quals, bool rvalue)
831 tree argtype = cp_build_qualified_type (type, quals);
832 return cp_build_reference_type (argtype, rvalue);
835 /* Build a dummy glvalue from dereferencing a dummy reference of type
839 build_stub_object (tree reftype)
841 tree stub = build1 (NOP_EXPR, reftype, integer_one_node);
842 return convert_from_reference (stub);
845 /* Determine which function will be called when looking up NAME in TYPE,
846 called with a single ARGTYPE argument, or no argument if ARGTYPE is
847 null. FLAGS and COMPLAIN are as for build_new_method_call.
849 Returns a FUNCTION_DECL if all is well.
850 Returns NULL_TREE if overload resolution failed.
851 Returns error_mark_node if the chosen function cannot be called. */
854 locate_fn_flags (tree type, tree name, tree argtype, int flags,
855 tsubst_flags_t complain)
857 tree ob, fn, fns, binfo, rval;
858 vec<tree, va_gc> *args;
861 binfo = TYPE_BINFO (type);
865 type = BINFO_TYPE (binfo);
868 ob = build_stub_object (cp_build_reference_type (type, false));
869 args = make_tree_vector ();
872 if (TREE_CODE (argtype) == TREE_LIST)
874 for (tree elt = argtype; elt != void_list_node;
875 elt = TREE_CHAIN (elt))
877 tree type = TREE_VALUE (elt);
878 if (TREE_CODE (type) != REFERENCE_TYPE)
879 type = cp_build_reference_type (type, /*rval*/true);
880 tree arg = build_stub_object (type);
881 vec_safe_push (args, arg);
886 tree arg = build_stub_object (argtype);
887 args->quick_push (arg);
891 fns = lookup_fnfields (binfo, name, 0);
892 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
894 release_tree_vector (args);
895 if (fn && rval == error_mark_node)
901 /* Locate the dtor of TYPE. */
904 get_dtor (tree type, tsubst_flags_t complain)
906 tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
907 LOOKUP_NORMAL, complain);
908 if (fn == error_mark_node)
913 /* Locate the default ctor of TYPE. */
916 locate_ctor (tree type)
920 push_deferring_access_checks (dk_no_check);
921 fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
922 LOOKUP_SPECULATIVE, tf_none);
923 pop_deferring_access_checks ();
924 if (fn == error_mark_node)
929 /* Likewise, but give any appropriate errors. */
932 get_default_ctor (tree type)
934 tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
935 LOOKUP_NORMAL, tf_warning_or_error);
936 if (fn == error_mark_node)
941 /* Locate the copy ctor of TYPE. */
944 get_copy_ctor (tree type, tsubst_flags_t complain)
946 int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
947 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
948 tree argtype = build_stub_type (type, quals, false);
949 tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
950 LOOKUP_NORMAL, complain);
951 if (fn == error_mark_node)
956 /* Locate the copy assignment operator of TYPE. */
959 get_copy_assign (tree type)
961 int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
962 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
963 tree argtype = build_stub_type (type, quals, false);
964 tree fn = locate_fn_flags (type, ansi_assopname (NOP_EXPR), argtype,
965 LOOKUP_NORMAL, tf_warning_or_error);
966 if (fn == error_mark_node)
971 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
972 DELETED_P or give an error message MSG with argument ARG. */
975 process_subob_fn (tree fn, tree *spec_p, bool *trivial_p,
976 bool *deleted_p, bool *constexpr_p,
979 if (!fn || fn == error_mark_node)
984 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
985 *spec_p = merge_exception_specifiers (*spec_p, raises, fn);
988 if (!trivial_fn_p (fn))
992 if (TREE_CODE (arg) == FIELD_DECL
993 && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
998 error ("union member %q+D with non-trivial %qD", arg, fn);
1002 if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
1004 *constexpr_p = false;
1007 inform (0, "defaulted constructor calls non-constexpr "
1009 explain_invalid_constexpr_fn (fn);
1020 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
1024 walk_field_subobs (tree fields, tree fnname, special_function_kind sfk,
1025 int quals, bool copy_arg_p, bool move_p,
1026 bool assign_p, tree *spec_p, bool *trivial_p,
1027 bool *deleted_p, bool *constexpr_p,
1028 bool diag, int flags, tsubst_flags_t complain)
1031 for (field = fields; field; field = DECL_CHAIN (field))
1033 tree mem_type, argtype, rval;
1035 if (TREE_CODE (field) != FIELD_DECL
1036 || DECL_ARTIFICIAL (field))
1039 mem_type = strip_array_types (TREE_TYPE (field));
1043 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
1046 error ("non-static const member %q#D, can%'t use default "
1047 "assignment operator", field);
1049 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1052 error ("non-static reference member %q#D, can%'t use "
1053 "default assignment operator", field);
1058 if (bad && deleted_p)
1061 else if (sfk == sfk_constructor)
1065 if (DECL_INITIAL (field))
1067 if (diag && DECL_INITIAL (field) == error_mark_node)
1068 inform (0, "initializer for %q+#D is invalid", field);
1072 /* Core 1351: If the field has an NSDMI that could throw, the
1073 default constructor is noexcept(false). FIXME this is
1074 broken by deferred parsing and 1360 saying we can't lazily
1075 declare a non-trivial default constructor. Also this
1076 needs to do deferred instantiation. Disable until the
1077 conflict between 1351 and 1360 is resolved. */
1078 if (spec_p && !expr_noexcept_p (DECL_INITIAL (field), complain))
1079 *spec_p = noexcept_false_spec;
1082 /* Don't do the normal processing. */
1087 if (CP_TYPE_CONST_P (mem_type)
1088 && default_init_uninitialized_part (mem_type))
1091 error ("uninitialized non-static const member %q#D",
1095 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1098 error ("uninitialized non-static reference member %q#D",
1103 if (bad && deleted_p)
1106 /* For an implicitly-defined default constructor to be constexpr,
1107 every member must have a user-provided default constructor or
1108 an explicit initializer. */
1109 if (constexpr_p && !CLASS_TYPE_P (mem_type)
1110 && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE)
1112 *constexpr_p = false;
1114 inform (0, "defaulted default constructor does not "
1115 "initialize %q+#D", field);
1119 if (!CLASS_TYPE_P (mem_type))
1122 if (ANON_AGGR_TYPE_P (mem_type))
1124 walk_field_subobs (TYPE_FIELDS (mem_type), fnname, sfk, quals,
1125 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1126 deleted_p, constexpr_p,
1127 diag, flags, complain);
1133 int mem_quals = cp_type_quals (mem_type) | quals;
1134 if (DECL_MUTABLE_P (field))
1135 mem_quals &= ~TYPE_QUAL_CONST;
1136 argtype = build_stub_type (mem_type, mem_quals, move_p);
1139 argtype = NULL_TREE;
1141 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
1143 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1144 constexpr_p, diag, field);
1148 /* The caller wants to generate an implicit declaration of SFK for CTYPE
1149 which is const if relevant and CONST_P is set. If spec_p, trivial_p and
1150 deleted_p are non-null, set their referent appropriately. If diag is
1151 true, we're either being called from maybe_explain_implicit_delete to
1152 give errors, or if constexpr_p is non-null, from
1153 explain_invalid_constexpr_fn. */
1156 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
1157 tree *spec_p, bool *trivial_p, bool *deleted_p,
1158 bool *constexpr_p, bool diag,
1159 tree inherited_base, tree inherited_parms)
1161 tree binfo, base_binfo, scope, fnname, rval, argtype;
1162 bool move_p, copy_arg_p, assign_p, expected_trivial, check_vdtor;
1163 vec<tree, va_gc> *vbases;
1164 int i, quals, flags;
1165 tsubst_flags_t complain;
1169 *spec_p = (cxx_dialect >= cxx0x ? noexcept_true_spec : empty_except_spec);
1173 /* "The closure type associated with a lambda-expression has a deleted
1174 default constructor and a deleted copy assignment operator."
1175 This is diagnosed in maybe_explain_implicit_delete. */
1176 if (LAMBDA_TYPE_P (ctype)
1177 && (sfk == sfk_constructor
1178 || sfk == sfk_copy_assignment))
1189 check_vdtor = false;
1192 case sfk_move_assignment:
1193 case sfk_copy_assignment:
1195 fnname = ansi_assopname (NOP_EXPR);
1198 case sfk_destructor:
1200 /* The synthesized method will call base dtors, but check complete
1201 here to avoid having to deal with VTT. */
1202 fnname = complete_dtor_identifier;
1205 case sfk_constructor:
1206 case sfk_move_constructor:
1207 case sfk_copy_constructor:
1208 case sfk_inheriting_constructor:
1210 fnname = complete_ctor_identifier;
1217 gcc_assert ((sfk == sfk_inheriting_constructor)
1218 == (inherited_base != NULL_TREE));
1220 /* If that user-written default constructor would satisfy the
1221 requirements of a constexpr constructor (7.1.5), the
1222 implicitly-defined default constructor is constexpr. */
1224 *constexpr_p = ctor_p;
1229 case sfk_constructor:
1230 case sfk_destructor:
1231 case sfk_inheriting_constructor:
1235 case sfk_move_constructor:
1236 case sfk_move_assignment:
1238 case sfk_copy_constructor:
1239 case sfk_copy_assignment:
1247 expected_trivial = type_has_trivial_fn (ctype, sfk);
1249 *trivial_p = expected_trivial;
1251 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
1252 class versions and other properties of the type. But a subobject
1253 class can be trivially copyable and yet have overload resolution
1254 choose a template constructor for initialization, depending on
1255 rvalueness and cv-quals. So we can't exit early for copy/move
1256 methods in C++0x. The same considerations apply in C++98/03, but
1257 there the definition of triviality does not consider overload
1258 resolution, so a constructor can be trivial even if it would otherwise
1259 call a non-trivial constructor. */
1260 if (expected_trivial
1261 && (!copy_arg_p || cxx_dialect < cxx0x))
1263 if (constexpr_p && sfk == sfk_constructor)
1265 bool cx = trivial_default_constructor_is_constexpr (ctype);
1267 if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
1268 /* A trivial constructor doesn't have any NSDMI. */
1269 inform (input_location, "defaulted default constructor does "
1270 "not initialize any non-static data member");
1276 ++cp_unevaluated_operand;
1277 ++c_inhibit_evaluation_warnings;
1278 push_deferring_access_checks (dk_no_deferred);
1280 scope = push_scope (ctype);
1282 flags = LOOKUP_NORMAL|LOOKUP_SPECULATIVE;
1283 if (!inherited_base)
1284 flags |= LOOKUP_DEFAULTED;
1286 complain = diag ? tf_warning_or_error : tf_none;
1289 quals = TYPE_QUAL_CONST;
1291 quals = TYPE_UNQUALIFIED;
1292 argtype = NULL_TREE;
1294 for (binfo = TYPE_BINFO (ctype), i = 0;
1295 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
1297 tree basetype = BINFO_TYPE (base_binfo);
1299 if (!assign_p && BINFO_VIRTUAL_P (base_binfo))
1300 /* We'll handle virtual bases below. */
1304 argtype = build_stub_type (basetype, quals, move_p);
1305 else if (basetype == inherited_base)
1306 argtype = inherited_parms;
1307 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1309 argtype = NULL_TREE;
1311 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1312 constexpr_p, diag, basetype);
1313 if (ctor_p && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype))
1315 /* In a constructor we also need to check the subobject
1316 destructors for cleanup of partially constructed objects. */
1317 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1318 NULL_TREE, flags, complain);
1319 /* Note that we don't pass down trivial_p; the subobject
1320 destructors don't affect triviality of the constructor. Nor
1321 do they affect constexpr-ness (a constant expression doesn't
1322 throw) or exception-specification (a throw from one of the
1323 dtors would be a double-fault). */
1324 process_subob_fn (rval, NULL, NULL,
1325 deleted_p, NULL, false,
1329 if (check_vdtor && type_has_virtual_destructor (basetype))
1331 rval = locate_fn_flags (ctype, ansi_opname (DELETE_EXPR),
1332 ptr_type_node, flags, complain);
1333 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
1334 to have a null rval (no class-specific op delete). */
1335 if (rval && rval == error_mark_node && deleted_p)
1337 check_vdtor = false;
1340 if (diag && assign_p && move_p
1341 && BINFO_VIRTUAL_P (base_binfo)
1342 && rval && TREE_CODE (rval) == FUNCTION_DECL
1343 && move_fn_p (rval) && !trivial_fn_p (rval)
1344 && vbase_has_user_provided_move_assign (basetype))
1345 warning (OPT_Wvirtual_move_assign,
1346 "defaulted move assignment for %qT calls a non-trivial "
1347 "move assignment operator for virtual base %qT",
1351 vbases = CLASSTYPE_VBASECLASSES (ctype);
1353 /* No virtual bases to worry about. */;
1357 *constexpr_p = false;
1358 FOR_EACH_VEC_ELT (*vbases, i, base_binfo)
1360 tree basetype = BINFO_TYPE (base_binfo);
1362 argtype = build_stub_type (basetype, quals, move_p);
1363 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1365 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1366 constexpr_p, diag, basetype);
1367 if (ctor_p && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype))
1369 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1370 NULL_TREE, flags, complain);
1371 process_subob_fn (rval, NULL, NULL,
1372 deleted_p, NULL, false,
1378 /* Now handle the non-static data members. */
1379 walk_field_subobs (TYPE_FIELDS (ctype), fnname, sfk, quals,
1380 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1381 deleted_p, constexpr_p,
1382 diag, flags, complain);
1384 walk_field_subobs (TYPE_FIELDS (ctype), complete_dtor_identifier,
1385 sfk_destructor, TYPE_UNQUALIFIED, false,
1386 false, false, NULL, NULL,
1388 false, flags, complain);
1392 pop_deferring_access_checks ();
1393 --cp_unevaluated_operand;
1394 --c_inhibit_evaluation_warnings;
1397 /* DECL is a deleted function. If it's implicitly deleted, explain why and
1398 return true; else return false. */
1401 maybe_explain_implicit_delete (tree decl)
1403 /* If decl is a clone, get the primary variant. */
1404 decl = DECL_ORIGIN (decl);
1405 gcc_assert (DECL_DELETED_FN (decl));
1406 if (DECL_DEFAULTED_FN (decl))
1408 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
1409 static struct pointer_set_t *explained;
1411 special_function_kind sfk;
1417 explained = pointer_set_create ();
1418 if (pointer_set_insert (explained, decl))
1421 sfk = special_function_p (decl);
1422 ctype = DECL_CONTEXT (decl);
1423 loc = input_location;
1424 input_location = DECL_SOURCE_LOCATION (decl);
1427 if (LAMBDA_TYPE_P (ctype))
1430 if (sfk == sfk_constructor)
1431 inform (DECL_SOURCE_LOCATION (decl),
1432 "a lambda closure type has a deleted default constructor");
1433 else if (sfk == sfk_copy_assignment)
1434 inform (DECL_SOURCE_LOCATION (decl),
1435 "a lambda closure type has a deleted copy assignment operator");
1439 else if (DECL_ARTIFICIAL (decl)
1440 && (sfk == sfk_copy_assignment
1441 || sfk == sfk_copy_constructor)
1442 && (type_has_user_declared_move_constructor (ctype)
1443 || type_has_user_declared_move_assign (ctype)))
1445 inform (0, "%q+#D is implicitly declared as deleted because %qT "
1446 "declares a move constructor or move assignment operator",
1452 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1453 tree parm_type = TREE_VALUE (parms);
1454 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1455 tree scope = push_scope (ctype);
1456 inform (0, "%q+#D is implicitly deleted because the default "
1457 "definition would be ill-formed:", decl);
1459 synthesized_method_walk (ctype, sfk, const_p,
1460 NULL, NULL, NULL, NULL, true,
1461 DECL_INHERITED_CTOR_BASE (decl), parms);
1464 input_location = loc;
1470 /* DECL is a defaulted function which was declared constexpr. Explain why
1471 it can't be constexpr. */
1474 explain_implicit_non_constexpr (tree decl)
1476 tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl));
1477 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1479 synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
1480 special_function_p (decl), const_p,
1481 NULL, NULL, NULL, &dummy, true,
1482 NULL_TREE, NULL_TREE);
1485 /* DECL is an instantiation of an inheriting constructor template. Deduce
1486 the correct exception-specification and deletedness for this particular
1490 deduce_inheriting_ctor (tree decl)
1492 gcc_assert (DECL_INHERITED_CTOR_BASE (decl));
1494 bool trivial, constexpr_, deleted;
1495 synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor,
1496 false, &spec, &trivial, &deleted, &constexpr_,
1498 DECL_INHERITED_CTOR_BASE (decl),
1499 FUNCTION_FIRST_USER_PARMTYPE (decl));
1500 DECL_DELETED_FN (decl) = deleted;
1501 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
1504 /* Implicitly declare the special function indicated by KIND, as a
1505 member of TYPE. For copy constructors and assignment operators,
1506 CONST_P indicates whether these functions should take a const
1507 reference argument or a non-const reference. Returns the
1508 FUNCTION_DECL for the implicitly declared function. */
1511 implicitly_declare_fn (special_function_kind kind, tree type,
1512 bool const_p, tree inherited_ctor,
1513 tree inherited_parms)
1516 tree parameter_types = void_list_node;
1519 tree raises = empty_except_spec;
1520 tree rhs_parm_type = NULL_TREE;
1523 HOST_WIDE_INT saved_processing_template_decl;
1527 /* Because we create declarations for implicitly declared functions
1528 lazily, we may be creating the declaration for a member of TYPE
1529 while in some completely different context. However, TYPE will
1530 never be a dependent class (because we never want to do lookups
1531 for implicitly defined functions in a dependent class).
1532 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
1533 because we only create clones for constructors and destructors
1534 when not in a template. */
1535 gcc_assert (!dependent_type_p (type));
1536 saved_processing_template_decl = processing_template_decl;
1537 processing_template_decl = 0;
1539 type = TYPE_MAIN_VARIANT (type);
1541 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (type))
1543 if (kind == sfk_destructor)
1544 /* See comment in check_special_function_return_type. */
1545 return_type = build_pointer_type (void_type_node);
1547 return_type = build_pointer_type (type);
1550 return_type = void_type_node;
1554 case sfk_destructor:
1556 name = constructor_name (type);
1559 case sfk_constructor:
1560 /* Default constructor. */
1561 name = constructor_name (type);
1564 case sfk_copy_constructor:
1565 case sfk_copy_assignment:
1566 case sfk_move_constructor:
1567 case sfk_move_assignment:
1568 case sfk_inheriting_constructor:
1571 if (kind == sfk_copy_assignment
1572 || kind == sfk_move_assignment)
1574 return_type = build_reference_type (type);
1575 name = ansi_assopname (NOP_EXPR);
1578 name = constructor_name (type);
1580 if (kind == sfk_inheriting_constructor)
1581 parameter_types = inherited_parms;
1585 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
1587 rhs_parm_type = type;
1588 move_p = (kind == sfk_move_assignment
1589 || kind == sfk_move_constructor);
1590 rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
1592 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
1600 tree inherited_base = (inherited_ctor
1601 ? DECL_CONTEXT (inherited_ctor)
1603 bool trivial_p = false;
1605 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
1607 /* For an inheriting constructor template, just copy these flags from
1608 the inherited constructor template for now. */
1609 raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor));
1610 deleted_p = DECL_DELETED_FN (DECL_TEMPLATE_RESULT (inherited_ctor));
1612 = DECL_DECLARED_CONSTEXPR_P (DECL_TEMPLATE_RESULT (inherited_ctor));
1615 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
1616 &deleted_p, &constexpr_p, false,
1617 inherited_base, inherited_parms);
1618 /* Don't bother marking a deleted constructor as constexpr. */
1620 constexpr_p = false;
1621 /* A trivial copy/move constructor is also a constexpr constructor. */
1622 else if (trivial_p && cxx_dialect >= cxx0x
1623 && (kind == sfk_copy_constructor
1624 || kind == sfk_move_constructor))
1625 gcc_assert (constexpr_p);
1627 if (!trivial_p && type_has_trivial_fn (type, kind))
1628 type_set_nontrivial_flag (type, kind);
1630 /* Create the function. */
1631 fn_type = build_method_type_directly (type, return_type, parameter_types);
1633 fn_type = build_exception_variant (fn_type, raises);
1634 fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
1635 if (kind != sfk_inheriting_constructor)
1636 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
1637 if (kind == sfk_constructor || kind == sfk_copy_constructor
1638 || kind == sfk_move_constructor || kind == sfk_inheriting_constructor)
1639 DECL_CONSTRUCTOR_P (fn) = 1;
1640 else if (kind == sfk_destructor)
1641 DECL_DESTRUCTOR_P (fn) = 1;
1644 DECL_ASSIGNMENT_OPERATOR_P (fn) = 1;
1645 SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR);
1648 /* If pointers to member functions use the least significant bit to
1649 indicate whether a function is virtual, ensure a pointer
1650 to this function will have that bit clear. */
1651 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
1652 && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
1653 DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
1655 /* Create the explicit arguments. */
1658 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
1659 want its type to be included in the mangled function
1661 tree decl = cp_build_parm_decl (NULL_TREE, rhs_parm_type);
1662 TREE_READONLY (decl) = 1;
1663 retrofit_lang_decl (decl);
1664 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
1665 DECL_ARGUMENTS (fn) = decl;
1667 else if (kind == sfk_inheriting_constructor)
1669 tree *p = &DECL_ARGUMENTS (fn);
1671 for (tree parm = inherited_parms; parm != void_list_node;
1672 parm = TREE_CHAIN (parm))
1674 *p = cp_build_parm_decl (NULL_TREE, TREE_VALUE (parm));
1675 retrofit_lang_decl (*p);
1676 DECL_PARM_LEVEL (*p) = 1;
1677 DECL_PARM_INDEX (*p) = index++;
1678 DECL_CONTEXT (*p) = fn;
1679 p = &DECL_CHAIN (*p);
1681 SET_DECL_INHERITED_CTOR_BASE (fn, inherited_base);
1682 DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor);
1683 /* A constructor so declared has the same access as the corresponding
1684 constructor in X. */
1685 TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor);
1686 TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor);
1687 /* Copy constexpr from the inherited constructor even if the
1688 inheriting constructor doesn't satisfy the requirements. */
1690 = DECL_DECLARED_CONSTEXPR_P (STRIP_TEMPLATE (inherited_ctor));
1692 /* Add the "this" parameter. */
1693 this_parm = build_this_parm (fn_type, TYPE_UNQUALIFIED);
1694 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
1695 DECL_ARGUMENTS (fn) = this_parm;
1697 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
1698 set_linkage_according_to_type (type, fn);
1699 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
1700 DECL_IN_AGGR_P (fn) = 1;
1701 DECL_ARTIFICIAL (fn) = 1;
1702 DECL_DEFAULTED_FN (fn) = 1;
1703 if (cxx_dialect >= cxx0x)
1705 DECL_DELETED_FN (fn) = deleted_p;
1706 DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
1708 DECL_EXTERNAL (fn) = true;
1709 DECL_NOT_REALLY_EXTERN (fn) = 1;
1710 DECL_DECLARED_INLINE_P (fn) = 1;
1711 gcc_assert (!TREE_USED (fn));
1713 /* Restore PROCESSING_TEMPLATE_DECL. */
1714 processing_template_decl = saved_processing_template_decl;
1716 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
1717 fn = add_inherited_template_parms (fn, inherited_ctor);
1719 /* Warn about calling a non-trivial move assignment in a virtual base. */
1720 if (kind == sfk_move_assignment && !deleted_p && !trivial_p
1721 && CLASSTYPE_VBASECLASSES (type))
1723 location_t loc = input_location;
1724 input_location = DECL_SOURCE_LOCATION (fn);
1725 synthesized_method_walk (type, kind, const_p,
1726 NULL, NULL, NULL, NULL, true,
1727 NULL_TREE, NULL_TREE);
1728 input_location = loc;
1734 /* Gives any errors about defaulted functions which need to be deferred
1735 until the containing class is complete. */
1738 defaulted_late_check (tree fn)
1740 /* Complain about invalid signature for defaulted fn. */
1741 tree ctx = DECL_CONTEXT (fn);
1742 special_function_kind kind = special_function_p (fn);
1743 bool fn_const_p = (copy_fn_p (fn) == 2);
1744 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
1747 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
1748 TREE_TYPE (TREE_TYPE (implicit_fn)))
1749 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1750 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn))))
1752 error ("defaulted declaration %q+D", fn);
1753 error_at (DECL_SOURCE_LOCATION (fn),
1754 "does not match expected signature %qD", implicit_fn);
1757 /* 8.4.2/2: If it is explicitly defaulted on its first declaration, it is
1758 implicitly considered to have the same exception-specification as if
1759 it had been implicitly declared. */
1760 if (DECL_DEFAULTED_IN_CLASS_P (fn))
1762 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
1763 if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)))
1765 maybe_instantiate_noexcept (fn);
1766 if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)),
1767 eh_spec, ce_normal))
1768 error ("function %q+D defaulted on its first declaration "
1769 "with an exception-specification that differs from "
1770 "the implicit declaration %q#D", fn, implicit_fn);
1772 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
1773 if (DECL_DECLARED_CONSTEXPR_P (implicit_fn))
1775 /* Hmm...should we do this for out-of-class too? Should it be OK to
1776 add constexpr later like inline, rather than requiring
1777 declarations to match? */
1778 DECL_DECLARED_CONSTEXPR_P (fn) = true;
1779 if (kind == sfk_constructor)
1780 TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
1784 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
1785 && DECL_DECLARED_CONSTEXPR_P (fn))
1787 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
1789 error ("explicitly defaulted function %q+D cannot be declared "
1790 "as constexpr because the implicit declaration is not "
1792 explain_implicit_non_constexpr (fn);
1794 DECL_DECLARED_CONSTEXPR_P (fn) = false;
1797 if (DECL_DELETED_FN (implicit_fn))
1798 DECL_DELETED_FN (fn) = 1;
1801 /* Returns true iff FN can be explicitly defaulted, and gives any
1802 errors if defaulting FN is ill-formed. */
1805 defaultable_fn_check (tree fn)
1807 special_function_kind kind = sfk_none;
1809 if (template_parm_scope_p ())
1811 error ("a template cannot be defaulted");
1815 if (DECL_CONSTRUCTOR_P (fn))
1817 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
1818 kind = sfk_constructor;
1819 else if (copy_fn_p (fn) > 0
1820 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
1822 kind = sfk_copy_constructor;
1823 else if (move_fn_p (fn))
1824 kind = sfk_move_constructor;
1826 else if (DECL_DESTRUCTOR_P (fn))
1827 kind = sfk_destructor;
1828 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
1829 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
1832 kind = sfk_copy_assignment;
1833 else if (move_fn_p (fn))
1834 kind = sfk_move_assignment;
1837 if (kind == sfk_none)
1839 error ("%qD cannot be defaulted", fn);
1844 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
1845 for (; t && t != void_list_node; t = TREE_CHAIN (t))
1846 if (TREE_PURPOSE (t))
1848 error ("defaulted function %q+D with default argument", fn);
1851 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn)))
1852 /* Defer checking. */;
1853 else if (!processing_template_decl)
1854 defaulted_late_check (fn);
1860 /* Add an implicit declaration to TYPE for the kind of function
1861 indicated by SFK. Return the FUNCTION_DECL for the new implicit
1865 lazily_declare_fn (special_function_kind sfk, tree type)
1868 /* Whether or not the argument has a const reference type. */
1869 bool const_p = false;
1873 case sfk_constructor:
1874 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
1876 case sfk_copy_constructor:
1877 const_p = TYPE_HAS_CONST_COPY_CTOR (type);
1878 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
1880 case sfk_move_constructor:
1881 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
1883 case sfk_copy_assignment:
1884 const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
1885 CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
1887 case sfk_move_assignment:
1888 CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
1890 case sfk_destructor:
1891 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
1897 /* Declare the function. */
1898 fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL);
1900 /* [class.copy]/8 If the class definition declares a move constructor or
1901 move assignment operator, the implicitly declared copy constructor is
1902 defined as deleted.... */
1903 if ((sfk == sfk_copy_assignment
1904 || sfk == sfk_copy_constructor)
1905 && (type_has_user_declared_move_constructor (type)
1906 || type_has_user_declared_move_assign (type)))
1907 DECL_DELETED_FN (fn) = true;
1909 /* A destructor may be virtual. */
1910 if (sfk == sfk_destructor
1911 || sfk == sfk_move_assignment
1912 || sfk == sfk_copy_assignment)
1913 check_for_override (fn, type);
1914 /* Add it to CLASSTYPE_METHOD_VEC. */
1915 add_method (type, fn, NULL_TREE);
1916 /* Add it to TYPE_METHODS. */
1917 if (sfk == sfk_destructor
1918 && DECL_VIRTUAL_P (fn)
1919 && abi_version_at_least (2))
1920 /* The ABI requires that a virtual destructor go at the end of the
1922 TYPE_METHODS (type) = chainon (TYPE_METHODS (type), fn);
1925 /* G++ 3.2 put the implicit destructor at the *beginning* of the
1926 TYPE_METHODS list, which cause the destructor to be emitted
1927 in an incorrect location in the vtable. */
1928 if (warn_abi && sfk == sfk_destructor && DECL_VIRTUAL_P (fn))
1929 warning (OPT_Wabi, "vtable layout for class %qT may not be ABI-compliant"
1930 "and may change in a future version of GCC due to "
1931 "implicit virtual destructor",
1933 DECL_CHAIN (fn) = TYPE_METHODS (type);
1934 TYPE_METHODS (type) = fn;
1936 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
1937 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
1938 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
1939 /* Create appropriate clones. */
1940 clone_function_decl (fn, /*update_method_vec=*/true);
1945 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
1946 as there are artificial parms in FN. */
1949 skip_artificial_parms_for (const_tree fn, tree list)
1951 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1952 list = TREE_CHAIN (list);
1956 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
1957 list = TREE_CHAIN (list);
1958 if (DECL_HAS_VTT_PARM_P (fn))
1959 list = TREE_CHAIN (list);
1963 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
1964 artificial parms in FN. */
1967 num_artificial_parms_for (const_tree fn)
1971 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1976 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
1978 if (DECL_HAS_VTT_PARM_P (fn))
1984 #include "gt-cp-method.h"