1 /* Handle the hair of processing (but not expanding) inline functions.
2 Also manage function and variable name overloading.
3 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
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"
31 #include "double-int.h"
38 #include "stringpool.h"
45 #include "common/common-target.h"
46 #include "diagnostic.h"
49 #include "plugin-api.h"
50 #include "hard-reg-set.h"
56 /* Various flags to control the mangling process. */
62 /* The thing we are presently mangling is part of a template type,
63 rather than a fully instantiated type. Therefore, we may see
64 complex expressions where we would normally expect to see a
65 simple integer constant. */
66 mf_maybe_uninstantiated = 1,
67 /* When mangling a numeric value, use the form `_XX_' (instead of
68 just `XX') if the value has more than one digit. */
69 mf_use_underscores_around_value = 2
72 typedef enum mangling_flags mangling_flags;
74 static void do_build_copy_assign (tree);
75 static void do_build_copy_constructor (tree);
76 static tree make_alias_for_thunk (tree);
78 /* Called once to initialize method.c. */
86 /* Return a this or result adjusting thunk to FUNCTION. THIS_ADJUSTING
87 indicates whether it is a this or result adjusting thunk.
88 FIXED_OFFSET and VIRTUAL_OFFSET indicate how to do the adjustment
89 (see thunk_adjust). VIRTUAL_OFFSET can be NULL, but FIXED_OFFSET
90 never is. VIRTUAL_OFFSET is the /index/ into the vtable for this
91 adjusting thunks, we scale it to a byte offset. For covariant
92 thunks VIRTUAL_OFFSET is the virtual binfo. You must post process
93 the returned thunk with finish_thunk. */
96 make_thunk (tree function, bool this_adjusting,
97 tree fixed_offset, tree virtual_offset)
102 gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
103 /* We can have this thunks to covariant thunks, but not vice versa. */
104 gcc_assert (!DECL_THIS_THUNK_P (function));
105 gcc_assert (!DECL_RESULT_THUNK_P (function) || this_adjusting);
107 /* Scale the VIRTUAL_OFFSET to be in terms of bytes. */
108 if (this_adjusting && virtual_offset)
110 = size_binop (MULT_EXPR,
113 TYPE_SIZE_UNIT (vtable_entry_type)));
115 d = tree_to_shwi (fixed_offset);
117 /* See if we already have the thunk in question. For this_adjusting
118 thunks VIRTUAL_OFFSET will be an INTEGER_CST, for covariant thunks it
120 for (thunk = DECL_THUNKS (function); thunk; thunk = DECL_CHAIN (thunk))
121 if (DECL_THIS_THUNK_P (thunk) == this_adjusting
122 && THUNK_FIXED_OFFSET (thunk) == d
123 && !virtual_offset == !THUNK_VIRTUAL_OFFSET (thunk)
126 ? tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk),
128 : THUNK_VIRTUAL_OFFSET (thunk) == virtual_offset)))
131 /* All thunks must be created before FUNCTION is actually emitted;
132 the ABI requires that all thunks be emitted together with the
133 function to which they transfer control. */
134 gcc_assert (!TREE_ASM_WRITTEN (function));
135 /* Likewise, we can only be adding thunks to a function declared in
136 the class currently being laid out. */
137 gcc_assert (TYPE_SIZE (DECL_CONTEXT (function))
138 && TYPE_BEING_DEFINED (DECL_CONTEXT (function)));
140 thunk = build_decl (DECL_SOURCE_LOCATION (function),
141 FUNCTION_DECL, NULL_TREE, TREE_TYPE (function));
142 DECL_LANG_SPECIFIC (thunk) = DECL_LANG_SPECIFIC (function);
143 cxx_dup_lang_specific_decl (thunk);
144 DECL_VIRTUAL_P (thunk) = true;
145 SET_DECL_THUNKS (thunk, NULL_TREE);
147 DECL_CONTEXT (thunk) = DECL_CONTEXT (function);
148 TREE_READONLY (thunk) = TREE_READONLY (function);
149 TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (function);
150 TREE_PUBLIC (thunk) = TREE_PUBLIC (function);
151 SET_DECL_THUNK_P (thunk, this_adjusting);
152 THUNK_TARGET (thunk) = function;
153 THUNK_FIXED_OFFSET (thunk) = d;
154 THUNK_VIRTUAL_OFFSET (thunk) = virtual_offset;
155 THUNK_ALIAS (thunk) = NULL_TREE;
157 DECL_INTERFACE_KNOWN (thunk) = 1;
158 DECL_NOT_REALLY_EXTERN (thunk) = 1;
159 DECL_COMDAT (thunk) = DECL_COMDAT (function);
160 DECL_SAVED_FUNCTION_DATA (thunk) = NULL;
161 /* The thunk itself is not a constructor or destructor, even if
162 the thing it is thunking to is. */
163 DECL_DESTRUCTOR_P (thunk) = 0;
164 DECL_CONSTRUCTOR_P (thunk) = 0;
165 DECL_EXTERNAL (thunk) = 1;
166 DECL_ARTIFICIAL (thunk) = 1;
167 /* The THUNK is not a pending inline, even if the FUNCTION is. */
168 DECL_PENDING_INLINE_P (thunk) = 0;
169 DECL_DECLARED_INLINE_P (thunk) = 0;
170 /* Nor is it a template instantiation. */
171 DECL_USE_TEMPLATE (thunk) = 0;
172 DECL_TEMPLATE_INFO (thunk) = NULL;
174 /* Add it to the list of thunks associated with FUNCTION. */
175 DECL_CHAIN (thunk) = DECL_THUNKS (function);
176 SET_DECL_THUNKS (function, thunk);
181 /* Finish THUNK, a thunk decl. */
184 finish_thunk (tree thunk)
187 tree fixed_offset = ssize_int (THUNK_FIXED_OFFSET (thunk));
188 tree virtual_offset = THUNK_VIRTUAL_OFFSET (thunk);
190 gcc_assert (!DECL_NAME (thunk) && DECL_THUNK_P (thunk));
191 if (virtual_offset && DECL_RESULT_THUNK_P (thunk))
192 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
193 function = THUNK_TARGET (thunk);
194 name = mangle_thunk (function, DECL_THIS_THUNK_P (thunk),
195 fixed_offset, virtual_offset);
197 /* We can end up with declarations of (logically) different
198 covariant thunks, that do identical adjustments. The two thunks
199 will be adjusting between within different hierarchies, which
200 happen to have the same layout. We must nullify one of them to
201 refer to the other. */
202 if (DECL_RESULT_THUNK_P (thunk))
206 for (cov_probe = DECL_THUNKS (function);
207 cov_probe; cov_probe = DECL_CHAIN (cov_probe))
208 if (DECL_NAME (cov_probe) == name)
210 gcc_assert (!DECL_THUNKS (thunk));
211 THUNK_ALIAS (thunk) = (THUNK_ALIAS (cov_probe)
212 ? THUNK_ALIAS (cov_probe) : cov_probe);
217 DECL_NAME (thunk) = name;
218 SET_DECL_ASSEMBLER_NAME (thunk, name);
221 static GTY (()) int thunk_labelno;
223 /* Create a static alias to target. */
226 make_alias_for (tree target, tree newid)
228 tree alias = build_decl (DECL_SOURCE_LOCATION (target),
229 TREE_CODE (target), newid, TREE_TYPE (target));
230 DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (target);
231 cxx_dup_lang_specific_decl (alias);
232 DECL_CONTEXT (alias) = NULL;
233 TREE_READONLY (alias) = TREE_READONLY (target);
234 TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (target);
235 TREE_PUBLIC (alias) = 0;
236 DECL_INTERFACE_KNOWN (alias) = 1;
237 if (DECL_LANG_SPECIFIC (alias))
239 DECL_NOT_REALLY_EXTERN (alias) = 1;
240 DECL_USE_TEMPLATE (alias) = 0;
241 DECL_TEMPLATE_INFO (alias) = NULL;
243 DECL_EXTERNAL (alias) = 0;
244 DECL_ARTIFICIAL (alias) = 1;
245 DECL_TEMPLATE_INSTANTIATED (alias) = 0;
246 if (TREE_CODE (alias) == FUNCTION_DECL)
248 DECL_SAVED_FUNCTION_DATA (alias) = NULL;
249 DECL_DESTRUCTOR_P (alias) = 0;
250 DECL_CONSTRUCTOR_P (alias) = 0;
251 DECL_PENDING_INLINE_P (alias) = 0;
252 DECL_DECLARED_INLINE_P (alias) = 0;
253 DECL_INITIAL (alias) = error_mark_node;
254 DECL_ARGUMENTS (alias) = copy_list (DECL_ARGUMENTS (target));
257 TREE_STATIC (alias) = 1;
258 TREE_ADDRESSABLE (alias) = 1;
259 TREE_USED (alias) = 1;
260 SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
265 make_alias_for_thunk (tree function)
270 targetm.asm_out.generate_internal_label (buf, "LTHUNK", thunk_labelno);
273 alias = make_alias_for (function, get_identifier (buf));
275 if (!flag_syntax_only)
277 struct cgraph_node *funcn, *aliasn;
278 funcn = cgraph_node::get (function);
279 gcc_checking_assert (funcn);
280 aliasn = cgraph_node::create_same_body_alias (alias, function);
281 DECL_ASSEMBLER_NAME (function);
282 gcc_assert (aliasn != NULL);
288 /* Emit the definition of a C++ multiple inheritance or covariant
289 return vtable thunk. If EMIT_P is nonzero, the thunk is emitted
293 use_thunk (tree thunk_fndecl, bool emit_p)
295 tree a, t, function, alias;
297 HOST_WIDE_INT fixed_offset, virtual_value;
298 bool this_adjusting = DECL_THIS_THUNK_P (thunk_fndecl);
299 struct cgraph_node *funcn, *thunk_node;
301 /* We should have called finish_thunk to give it a name. */
302 gcc_assert (DECL_NAME (thunk_fndecl));
304 /* We should never be using an alias, always refer to the
306 gcc_assert (!THUNK_ALIAS (thunk_fndecl));
308 if (TREE_ASM_WRITTEN (thunk_fndecl))
311 function = THUNK_TARGET (thunk_fndecl);
312 if (DECL_RESULT (thunk_fndecl))
313 /* We already turned this thunk into an ordinary function.
314 There's no need to process this thunk again. */
317 if (DECL_THUNK_P (function))
318 /* The target is itself a thunk, process it now. */
319 use_thunk (function, emit_p);
321 /* Thunks are always addressable; they only appear in vtables. */
322 TREE_ADDRESSABLE (thunk_fndecl) = 1;
324 /* Figure out what function is being thunked to. It's referenced in
325 this translation unit. */
326 TREE_ADDRESSABLE (function) = 1;
327 mark_used (function);
331 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function))
332 alias = make_alias_for_thunk (function);
336 fixed_offset = THUNK_FIXED_OFFSET (thunk_fndecl);
337 virtual_offset = THUNK_VIRTUAL_OFFSET (thunk_fndecl);
342 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
343 virtual_value = tree_to_shwi (virtual_offset);
344 gcc_assert (virtual_value);
349 /* And, if we need to emit the thunk, it's used. */
350 mark_used (thunk_fndecl);
351 /* This thunk is actually defined. */
352 DECL_EXTERNAL (thunk_fndecl) = 0;
353 /* The linkage of the function may have changed. FIXME in linkage
355 gcc_assert (DECL_INTERFACE_KNOWN (function));
356 TREE_PUBLIC (thunk_fndecl) = TREE_PUBLIC (function);
357 DECL_VISIBILITY (thunk_fndecl) = DECL_VISIBILITY (function);
358 DECL_VISIBILITY_SPECIFIED (thunk_fndecl)
359 = DECL_VISIBILITY_SPECIFIED (function);
360 DECL_COMDAT (thunk_fndecl) = DECL_COMDAT (function);
361 DECL_WEAK (thunk_fndecl) = DECL_WEAK (function);
363 if (flag_syntax_only)
365 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
369 push_to_top_level ();
371 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function)
372 && targetm_common.have_named_sections)
375 struct symtab_node *symbol;
377 if ((symbol = symtab_node::get (function))
380 if (symbol->analyzed)
381 fn = symtab_node::get (function)->ultimate_alias_target ()->decl;
383 fn = symtab_node::get (function)->alias_target;
385 resolve_unique_section (fn, 0, flag_function_sections);
387 if (DECL_SECTION_NAME (fn) != NULL && DECL_ONE_ONLY (fn))
389 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
391 /* Output the thunk into the same section as function. */
392 set_decl_section_name (thunk_fndecl, DECL_SECTION_NAME (fn));
393 symtab_node::get (thunk_fndecl)->implicit_section
394 = symtab_node::get (fn)->implicit_section;
398 /* Set up cloned argument trees for the thunk. */
400 for (a = DECL_ARGUMENTS (function); a; a = DECL_CHAIN (a))
402 tree x = copy_node (a);
404 DECL_CONTEXT (x) = thunk_fndecl;
405 SET_DECL_RTL (x, NULL);
406 DECL_HAS_VALUE_EXPR_P (x) = 0;
407 TREE_ADDRESSABLE (x) = 0;
411 DECL_ARGUMENTS (thunk_fndecl) = a;
412 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
413 funcn = cgraph_node::get (function);
414 gcc_checking_assert (funcn);
415 thunk_node = funcn->create_thunk (thunk_fndecl, function,
416 this_adjusting, fixed_offset, virtual_value,
417 virtual_offset, alias);
418 if (DECL_ONE_ONLY (function))
419 thunk_node->add_to_same_comdat_group (funcn);
422 || !targetm.asm_out.can_output_mi_thunk (thunk_fndecl, fixed_offset,
423 virtual_value, alias))
425 /* If this is a covariant thunk, or we don't have the necessary
426 code for efficient thunks, generate a thunk function that
427 just makes a call to the real function. Unfortunately, this
428 doesn't work for varargs. */
430 if (varargs_function_p (function))
431 error ("generic thunk code fails for method %q#D which uses %<...%>",
435 pop_from_top_level ();
438 /* Code for synthesizing methods which have default semantics defined. */
440 /* True iff CTYPE has a trivial SFK. */
443 type_has_trivial_fn (tree ctype, special_function_kind sfk)
447 case sfk_constructor:
448 return !TYPE_HAS_COMPLEX_DFLT (ctype);
449 case sfk_copy_constructor:
450 return !TYPE_HAS_COMPLEX_COPY_CTOR (ctype);
451 case sfk_move_constructor:
452 return !TYPE_HAS_COMPLEX_MOVE_CTOR (ctype);
453 case sfk_copy_assignment:
454 return !TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype);
455 case sfk_move_assignment:
456 return !TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype);
458 return !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype);
459 case sfk_inheriting_constructor:
466 /* Note that CTYPE has a non-trivial SFK even though we previously thought
470 type_set_nontrivial_flag (tree ctype, special_function_kind sfk)
474 case sfk_constructor:
475 TYPE_HAS_COMPLEX_DFLT (ctype) = true;
477 case sfk_copy_constructor:
478 TYPE_HAS_COMPLEX_COPY_CTOR (ctype) = true;
480 case sfk_move_constructor:
481 TYPE_HAS_COMPLEX_MOVE_CTOR (ctype) = true;
483 case sfk_copy_assignment:
484 TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype) = true;
486 case sfk_move_assignment:
487 TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype) = true;
490 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
492 case sfk_inheriting_constructor:
498 /* True iff FN is a trivial defaulted member function ([cd]tor, op=). */
501 trivial_fn_p (tree fn)
503 if (!DECL_DEFAULTED_FN (fn))
506 /* If fn is a clone, get the primary variant. */
507 if (tree prim = DECL_CLONED_FUNCTION (fn))
509 return type_has_trivial_fn (DECL_CONTEXT (fn), special_function_p (fn));
512 /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO
513 given the parameter or parameters PARM, possibly inherited constructor
514 base INH, or move flag MOVE_P. */
517 add_one_base_init (tree binfo, tree parm, bool move_p, tree inh,
518 tree member_init_list)
523 /* An inheriting constructor only has a mem-initializer for
524 the base it inherits from. */
525 if (BINFO_TYPE (binfo) != inh)
526 return member_init_list;
530 for (; parm; parm = DECL_CHAIN (parm))
532 tree exp = convert_from_reference (parm);
533 if (TREE_CODE (TREE_TYPE (parm)) != REFERENCE_TYPE
534 || TYPE_REF_IS_RVALUE (TREE_TYPE (parm)))
536 *p = build_tree_list (NULL_TREE, exp);
537 p = &TREE_CHAIN (*p);
542 init = build_base_path (PLUS_EXPR, parm, binfo, 1,
543 tf_warning_or_error);
546 init = build_tree_list (NULL_TREE, init);
548 return tree_cons (binfo, init, member_init_list);
551 /* Generate code for default X(X&) or X(X&&) constructor or an inheriting
555 do_build_copy_constructor (tree fndecl)
557 tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
558 bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
559 bool trivial = trivial_fn_p (fndecl);
560 tree inh = DECL_INHERITED_CTOR_BASE (fndecl);
563 parm = convert_from_reference (parm);
566 && is_empty_class (current_class_type))
567 /* Don't copy the padding byte; it might not have been allocated
568 if *this is a base subobject. */;
571 tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm);
572 finish_expr_stmt (t);
576 tree fields = TYPE_FIELDS (current_class_type);
577 tree member_init_list = NULL_TREE;
578 int cvquals = cp_type_quals (TREE_TYPE (parm));
580 tree binfo, base_binfo;
582 vec<tree, va_gc> *vbases;
584 /* Initialize all the base-classes with the parameter converted
585 to their type so that we get their copy constructor and not
586 another constructor that takes current_class_type. We must
587 deal with the binfo's directly as a direct base might be
588 inaccessible due to ambiguity. */
589 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
590 vec_safe_iterate (vbases, i, &binfo); i++)
592 member_init_list = add_one_base_init (binfo, parm, move_p, inh,
596 for (binfo = TYPE_BINFO (current_class_type), i = 0;
597 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
599 if (BINFO_VIRTUAL_P (base_binfo))
601 member_init_list = add_one_base_init (base_binfo, parm, move_p,
602 inh, member_init_list);
605 for (; fields; fields = DECL_CHAIN (fields))
610 if (TREE_CODE (field) != FIELD_DECL)
615 expr_type = TREE_TYPE (field);
616 if (DECL_NAME (field))
618 if (VFIELD_NAME_P (DECL_NAME (field)))
621 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
622 /* Just use the field; anonymous types can't have
623 nontrivial copy ctors or assignment ops or this
624 function would be deleted. */;
628 /* Compute the type of "init->field". If the copy-constructor
629 parameter is, for example, "const S&", and the type of
630 the field is "T", then the type will usually be "const
631 T". (There are no cv-qualified variants of reference
633 if (TREE_CODE (expr_type) != REFERENCE_TYPE)
637 if (DECL_MUTABLE_P (field))
638 quals &= ~TYPE_QUAL_CONST;
639 quals |= cp_type_quals (expr_type);
640 expr_type = cp_build_qualified_type (expr_type, quals);
643 init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
644 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
645 /* 'move' breaks bit-fields, and has no effect for scalars. */
646 && !scalarish_type_p (expr_type))
648 init = build_tree_list (NULL_TREE, init);
650 member_init_list = tree_cons (field, init, member_init_list);
652 finish_mem_initializers (member_init_list);
657 do_build_copy_assign (tree fndecl)
659 tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl));
661 bool move_p = move_fn_p (fndecl);
662 bool trivial = trivial_fn_p (fndecl);
663 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
665 compound_stmt = begin_compound_stmt (0);
666 parm = convert_from_reference (parm);
669 && is_empty_class (current_class_type))
670 /* Don't copy the padding byte; it might not have been allocated
671 if *this is a base subobject. */;
674 tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
675 finish_expr_stmt (t);
680 int cvquals = cp_type_quals (TREE_TYPE (parm));
682 tree binfo, base_binfo;
684 /* Assign to each of the direct base classes. */
685 for (binfo = TYPE_BINFO (current_class_type), i = 0;
686 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
689 vec<tree, va_gc> *parmvec;
691 /* We must convert PARM directly to the base class
692 explicitly since the base class may be ambiguous. */
693 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
694 tf_warning_or_error);
696 converted_parm = move (converted_parm);
697 /* Call the base class assignment operator. */
698 parmvec = make_tree_vector_single (converted_parm);
700 (build_special_member_call (current_class_ref,
701 ansi_assopname (NOP_EXPR),
705 tf_warning_or_error));
706 release_tree_vector (parmvec);
709 /* Assign to each of the non-static data members. */
710 for (fields = TYPE_FIELDS (current_class_type);
712 fields = DECL_CHAIN (fields))
714 tree comp = current_class_ref;
720 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
723 expr_type = TREE_TYPE (field);
725 if (CP_TYPE_CONST_P (expr_type))
727 error ("non-static const member %q#D, can%'t use default "
728 "assignment operator", field);
731 else if (TREE_CODE (expr_type) == REFERENCE_TYPE)
733 error ("non-static reference member %q#D, can%'t use "
734 "default assignment operator", field);
738 if (DECL_NAME (field))
740 if (VFIELD_NAME_P (DECL_NAME (field)))
743 else if (ANON_AGGR_TYPE_P (expr_type)
744 && TYPE_FIELDS (expr_type) != NULL_TREE)
745 /* Just use the field; anonymous types can't have
746 nontrivial copy ctors or assignment ops or this
747 function would be deleted. */;
751 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
753 /* Compute the type of init->field */
755 if (DECL_MUTABLE_P (field))
756 quals &= ~TYPE_QUAL_CONST;
757 expr_type = cp_build_qualified_type (expr_type, quals);
759 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
760 if (move_p && TREE_CODE (expr_type) != REFERENCE_TYPE
761 /* 'move' breaks bit-fields, and has no effect for scalars. */
762 && !scalarish_type_p (expr_type))
765 if (DECL_NAME (field))
766 init = cp_build_modify_expr (comp, NOP_EXPR, init,
767 tf_warning_or_error);
769 init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
770 finish_expr_stmt (init);
773 finish_return_stmt (current_class_ref);
774 finish_compound_stmt (compound_stmt);
777 /* Synthesize FNDECL, a non-static member function. */
780 synthesize_method (tree fndecl)
782 bool nested = (current_function_decl != NULL_TREE);
783 tree context = decl_function_context (fndecl);
784 bool need_body = true;
786 location_t save_input_location = input_location;
787 int error_count = errorcount;
788 int warning_count = warningcount + werrorcount;
790 /* Reset the source location, we might have been previously
791 deferred, and thus have saved where we were first needed. */
792 DECL_SOURCE_LOCATION (fndecl)
793 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
795 /* If we've been asked to synthesize a clone, just synthesize the
796 cloned function instead. Doing so will automatically fill in the
797 body for the clone. */
798 if (DECL_CLONED_FUNCTION_P (fndecl))
799 fndecl = DECL_CLONED_FUNCTION (fndecl);
801 /* We may be in the middle of deferred access check. Disable
803 push_deferring_access_checks (dk_no_deferred);
806 push_to_top_level ();
808 push_function_context ();
810 input_location = DECL_SOURCE_LOCATION (fndecl);
812 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
813 stmt = begin_function_body ();
815 if (DECL_OVERLOADED_OPERATOR_P (fndecl) == NOP_EXPR)
817 do_build_copy_assign (fndecl);
820 else if (DECL_CONSTRUCTOR_P (fndecl))
822 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
823 if (arg_chain != void_list_node)
824 do_build_copy_constructor (fndecl);
826 finish_mem_initializers (NULL_TREE);
829 /* If we haven't yet generated the body of the function, just
830 generate an empty compound statement. */
834 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
835 finish_compound_stmt (compound_stmt);
838 finish_function_body (stmt);
839 expand_or_defer_fn (finish_function (0));
841 input_location = save_input_location;
844 pop_from_top_level ();
846 pop_function_context ();
848 pop_deferring_access_checks ();
850 if (error_count != errorcount || warning_count != warningcount + werrorcount)
851 inform (input_location, "synthesized method %qD first required here ",
855 /* Build a reference to type TYPE with cv-quals QUALS, which is an
856 rvalue if RVALUE is true. */
859 build_stub_type (tree type, int quals, bool rvalue)
861 tree argtype = cp_build_qualified_type (type, quals);
862 return cp_build_reference_type (argtype, rvalue);
865 /* Build a dummy glvalue from dereferencing a dummy reference of type
869 build_stub_object (tree reftype)
871 if (TREE_CODE (reftype) != REFERENCE_TYPE)
872 reftype = cp_build_reference_type (reftype, /*rval*/true);
873 tree stub = build1 (CONVERT_EXPR, reftype, integer_one_node);
874 return convert_from_reference (stub);
877 /* Determine which function will be called when looking up NAME in TYPE,
878 called with a single ARGTYPE argument, or no argument if ARGTYPE is
879 null. FLAGS and COMPLAIN are as for build_new_method_call.
881 Returns a FUNCTION_DECL if all is well.
882 Returns NULL_TREE if overload resolution failed.
883 Returns error_mark_node if the chosen function cannot be called. */
886 locate_fn_flags (tree type, tree name, tree argtype, int flags,
887 tsubst_flags_t complain)
889 tree ob, fn, fns, binfo, rval;
890 vec<tree, va_gc> *args;
893 binfo = TYPE_BINFO (type);
897 type = BINFO_TYPE (binfo);
900 ob = build_stub_object (cp_build_reference_type (type, false));
901 args = make_tree_vector ();
904 if (TREE_CODE (argtype) == TREE_LIST)
906 for (tree elt = argtype; elt != void_list_node;
907 elt = TREE_CHAIN (elt))
909 tree type = TREE_VALUE (elt);
910 tree arg = build_stub_object (type);
911 vec_safe_push (args, arg);
916 tree arg = build_stub_object (argtype);
917 args->quick_push (arg);
921 fns = lookup_fnfields (binfo, name, 0);
922 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
924 release_tree_vector (args);
925 if (fn && rval == error_mark_node)
931 /* Locate the dtor of TYPE. */
934 get_dtor (tree type, tsubst_flags_t complain)
936 tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
937 LOOKUP_NORMAL, complain);
938 if (fn == error_mark_node)
943 /* Locate the default ctor of TYPE. */
946 locate_ctor (tree type)
950 push_deferring_access_checks (dk_no_check);
951 fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
952 LOOKUP_SPECULATIVE, tf_none);
953 pop_deferring_access_checks ();
954 if (fn == error_mark_node)
959 /* Likewise, but give any appropriate errors. */
962 get_default_ctor (tree type)
964 tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
965 LOOKUP_NORMAL, tf_warning_or_error);
966 if (fn == error_mark_node)
971 /* Locate the copy ctor of TYPE. */
974 get_copy_ctor (tree type, tsubst_flags_t complain)
976 int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
977 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
978 tree argtype = build_stub_type (type, quals, false);
979 tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
980 LOOKUP_NORMAL, complain);
981 if (fn == error_mark_node)
986 /* Locate the copy assignment operator of TYPE. */
989 get_copy_assign (tree type)
991 int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
992 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
993 tree argtype = build_stub_type (type, quals, false);
994 tree fn = locate_fn_flags (type, ansi_assopname (NOP_EXPR), argtype,
995 LOOKUP_NORMAL, tf_warning_or_error);
996 if (fn == error_mark_node)
1001 /* Locate the inherited constructor of constructor CTOR. */
1004 get_inherited_ctor (tree ctor)
1006 gcc_assert (DECL_INHERITED_CTOR_BASE (ctor));
1008 push_deferring_access_checks (dk_no_check);
1009 tree fn = locate_fn_flags (DECL_INHERITED_CTOR_BASE (ctor),
1010 complete_ctor_identifier,
1011 FUNCTION_FIRST_USER_PARMTYPE (ctor),
1012 LOOKUP_NORMAL|LOOKUP_SPECULATIVE,
1014 pop_deferring_access_checks ();
1015 if (fn == error_mark_node)
1020 /* walk_tree helper function for is_trivially_xible. If *TP is a call,
1021 return it if it calls something other than a trivial special member
1025 check_nontriv (tree *tp, int *, void *)
1028 if (TREE_CODE (*tp) == CALL_EXPR)
1029 fn = CALL_EXPR_FN (*tp);
1030 else if (TREE_CODE (*tp) == AGGR_INIT_EXPR)
1031 fn = AGGR_INIT_EXPR_FN (*tp);
1035 if (TREE_CODE (fn) == ADDR_EXPR)
1036 fn = TREE_OPERAND (fn, 0);
1038 if (TREE_CODE (fn) != FUNCTION_DECL
1039 || !trivial_fn_p (fn))
1044 /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */
1047 assignable_expr (tree to, tree from)
1049 ++cp_unevaluated_operand;
1050 to = build_stub_object (to);
1051 from = build_stub_object (from);
1052 tree r = cp_build_modify_expr (to, NOP_EXPR, from, tf_none);
1053 --cp_unevaluated_operand;
1057 /* The predicate condition for a template specialization
1058 is_constructible<T, Args...> shall be satisfied if and only if the
1059 following variable definition would be well-formed for some invented
1060 variable t: T t(create<Args>()...);
1062 Return something equivalent in well-formedness and triviality. */
1065 constructible_expr (tree to, tree from)
1068 if (CLASS_TYPE_P (to))
1071 vec<tree, va_gc> *args = NULL;
1072 if (TREE_CODE (to) != REFERENCE_TYPE)
1073 to = cp_build_reference_type (to, /*rval*/false);
1074 tree ob = build_stub_object (to);
1075 for (; from; from = TREE_CHAIN (from))
1076 vec_safe_push (args, build_stub_object (TREE_VALUE (from)));
1077 expr = build_special_member_call (ob, complete_ctor_identifier, &args,
1078 ctype, LOOKUP_NORMAL, tf_none);
1079 if (expr == error_mark_node)
1080 return error_mark_node;
1081 /* The current state of the standard vis-a-vis LWG 2116 is that
1082 is_*constructible involves destruction as well. */
1083 if (type_build_dtor_call (ctype))
1085 tree dtor = build_special_member_call (ob, complete_dtor_identifier,
1086 NULL, ctype, LOOKUP_NORMAL,
1088 if (dtor == error_mark_node)
1089 return error_mark_node;
1090 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
1091 expr = build2 (COMPOUND_EXPR, void_type_node, expr, dtor);
1096 if (from == NULL_TREE)
1097 return build_value_init (to, tf_none);
1098 else if (TREE_CHAIN (from))
1099 return error_mark_node; // too many initializers
1100 from = build_stub_object (TREE_VALUE (from));
1101 expr = perform_direct_initialization_if_possible (to, from,
1108 /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or
1109 constructible (otherwise) from FROM, which is a single type for
1110 assignment or a list of types for construction. */
1113 is_trivially_xible (enum tree_code code, tree to, tree from)
1116 if (code == MODIFY_EXPR)
1117 expr = assignable_expr (to, from);
1118 else if (from && TREE_CHAIN (from))
1119 return false; // only 0- and 1-argument ctors can be trivial
1121 expr = constructible_expr (to, from);
1123 if (expr == error_mark_node)
1125 tree nt = cp_walk_tree_without_duplicates (&expr, check_nontriv, NULL);
1129 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
1130 DELETED_P or give an error message MSG with argument ARG. */
1133 process_subob_fn (tree fn, tree *spec_p, bool *trivial_p,
1134 bool *deleted_p, bool *constexpr_p,
1135 bool diag, tree arg)
1137 if (!fn || fn == error_mark_node)
1142 maybe_instantiate_noexcept (fn);
1143 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
1144 *spec_p = merge_exception_specifiers (*spec_p, raises);
1147 if (!trivial_fn_p (fn))
1151 if (TREE_CODE (arg) == FIELD_DECL
1152 && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
1157 error ("union member %q+D with non-trivial %qD", arg, fn);
1161 if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
1163 *constexpr_p = false;
1166 inform (0, "defaulted constructor calls non-constexpr "
1168 explain_invalid_constexpr_fn (fn);
1179 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
1183 walk_field_subobs (tree fields, tree fnname, special_function_kind sfk,
1184 int quals, bool copy_arg_p, bool move_p,
1185 bool assign_p, tree *spec_p, bool *trivial_p,
1186 bool *deleted_p, bool *constexpr_p,
1187 bool diag, int flags, tsubst_flags_t complain)
1190 for (field = fields; field; field = DECL_CHAIN (field))
1192 tree mem_type, argtype, rval;
1194 if (TREE_CODE (field) != FIELD_DECL
1195 || DECL_ARTIFICIAL (field))
1198 mem_type = strip_array_types (TREE_TYPE (field));
1202 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
1205 error ("non-static const member %q#D, can%'t use default "
1206 "assignment operator", field);
1208 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1211 error ("non-static reference member %q#D, can%'t use "
1212 "default assignment operator", field);
1217 if (bad && deleted_p)
1220 else if (sfk == sfk_constructor)
1224 if (DECL_INITIAL (field))
1226 if (diag && DECL_INITIAL (field) == error_mark_node)
1227 inform (0, "initializer for %q+#D is invalid", field);
1230 /* Core 1351: If the field has an NSDMI that could throw, the
1231 default constructor is noexcept(false). */
1234 tree nsdmi = get_nsdmi (field, /*ctor*/false);
1235 if (!expr_noexcept_p (nsdmi, complain))
1236 *spec_p = noexcept_false_spec;
1238 /* Don't do the normal processing. */
1243 if (CP_TYPE_CONST_P (mem_type)
1244 && default_init_uninitialized_part (mem_type))
1248 error ("uninitialized const member in %q#T",
1249 current_class_type);
1250 inform (DECL_SOURCE_LOCATION (field),
1251 "%q#D should be initialized", field);
1255 else if (TREE_CODE (mem_type) == REFERENCE_TYPE)
1259 error ("uninitialized reference member in %q#T",
1260 current_class_type);
1261 inform (DECL_SOURCE_LOCATION (field),
1262 "%q#D should be initialized", field);
1267 if (bad && deleted_p)
1270 /* For an implicitly-defined default constructor to be constexpr,
1271 every member must have a user-provided default constructor or
1272 an explicit initializer. */
1273 if (constexpr_p && !CLASS_TYPE_P (mem_type)
1274 && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE)
1276 *constexpr_p = false;
1278 inform (0, "defaulted default constructor does not "
1279 "initialize %q+#D", field);
1282 else if (sfk == sfk_copy_constructor)
1285 if (TREE_CODE (mem_type) == REFERENCE_TYPE
1286 && TYPE_REF_IS_RVALUE (mem_type))
1289 error ("copying non-static data member %q#D of rvalue "
1290 "reference type", field);
1296 if (!CLASS_TYPE_P (mem_type))
1299 if (ANON_AGGR_TYPE_P (mem_type))
1301 walk_field_subobs (TYPE_FIELDS (mem_type), fnname, sfk, quals,
1302 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1303 deleted_p, constexpr_p,
1304 diag, flags, complain);
1310 int mem_quals = cp_type_quals (mem_type) | quals;
1311 if (DECL_MUTABLE_P (field))
1312 mem_quals &= ~TYPE_QUAL_CONST;
1313 argtype = build_stub_type (mem_type, mem_quals, move_p);
1316 argtype = NULL_TREE;
1318 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
1320 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1321 constexpr_p, diag, field);
1325 /* The caller wants to generate an implicit declaration of SFK for CTYPE
1326 which is const if relevant and CONST_P is set. If spec_p, trivial_p and
1327 deleted_p are non-null, set their referent appropriately. If diag is
1328 true, we're either being called from maybe_explain_implicit_delete to
1329 give errors, or if constexpr_p is non-null, from
1330 explain_invalid_constexpr_fn. */
1333 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
1334 tree *spec_p, bool *trivial_p, bool *deleted_p,
1335 bool *constexpr_p, bool diag,
1336 tree inherited_base, tree inherited_parms)
1338 tree binfo, base_binfo, scope, fnname, rval, argtype;
1339 bool move_p, copy_arg_p, assign_p, expected_trivial, check_vdtor;
1340 vec<tree, va_gc> *vbases;
1341 int i, quals, flags;
1342 tsubst_flags_t complain;
1346 *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec);
1350 /* "The closure type associated with a lambda-expression has a deleted
1351 default constructor and a deleted copy assignment operator."
1352 This is diagnosed in maybe_explain_implicit_delete. */
1353 if (LAMBDA_TYPE_P (ctype)
1354 && (sfk == sfk_constructor
1355 || sfk == sfk_copy_assignment))
1366 check_vdtor = false;
1369 case sfk_move_assignment:
1370 case sfk_copy_assignment:
1372 fnname = ansi_assopname (NOP_EXPR);
1375 case sfk_destructor:
1377 /* The synthesized method will call base dtors, but check complete
1378 here to avoid having to deal with VTT. */
1379 fnname = complete_dtor_identifier;
1382 case sfk_constructor:
1383 case sfk_move_constructor:
1384 case sfk_copy_constructor:
1385 case sfk_inheriting_constructor:
1387 fnname = complete_ctor_identifier;
1394 gcc_assert ((sfk == sfk_inheriting_constructor)
1395 == (inherited_base != NULL_TREE));
1397 /* If that user-written default constructor would satisfy the
1398 requirements of a constexpr constructor (7.1.5), the
1399 implicitly-defined default constructor is constexpr. */
1401 *constexpr_p = ctor_p;
1406 case sfk_constructor:
1407 case sfk_destructor:
1408 case sfk_inheriting_constructor:
1412 case sfk_move_constructor:
1413 case sfk_move_assignment:
1415 case sfk_copy_constructor:
1416 case sfk_copy_assignment:
1424 expected_trivial = type_has_trivial_fn (ctype, sfk);
1426 *trivial_p = expected_trivial;
1428 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
1429 class versions and other properties of the type. But a subobject
1430 class can be trivially copyable and yet have overload resolution
1431 choose a template constructor for initialization, depending on
1432 rvalueness and cv-quals. And furthermore, a member in a base might
1433 be trivial but deleted or otherwise not callable. So we can't exit
1434 early in C++0x. The same considerations apply in C++98/03, but
1435 there the definition of triviality does not consider overload
1436 resolution, so a constructor can be trivial even if it would otherwise
1437 call a non-trivial constructor. */
1438 if (expected_trivial
1439 && (!copy_arg_p || cxx_dialect < cxx11))
1441 if (constexpr_p && sfk == sfk_constructor)
1443 bool cx = trivial_default_constructor_is_constexpr (ctype);
1445 if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
1446 /* A trivial constructor doesn't have any NSDMI. */
1447 inform (input_location, "defaulted default constructor does "
1448 "not initialize any non-static data member");
1450 if (!diag && cxx_dialect < cxx11)
1454 ++cp_unevaluated_operand;
1455 ++c_inhibit_evaluation_warnings;
1456 push_deferring_access_checks (dk_no_deferred);
1458 scope = push_scope (ctype);
1460 flags = LOOKUP_NORMAL|LOOKUP_SPECULATIVE;
1461 if (!inherited_base)
1462 flags |= LOOKUP_DEFAULTED;
1464 complain = diag ? tf_warning_or_error : tf_none;
1467 quals = TYPE_QUAL_CONST;
1469 quals = TYPE_UNQUALIFIED;
1470 argtype = NULL_TREE;
1472 for (binfo = TYPE_BINFO (ctype), i = 0;
1473 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
1475 tree basetype = BINFO_TYPE (base_binfo);
1477 if (!assign_p && BINFO_VIRTUAL_P (base_binfo))
1478 /* We'll handle virtual bases below. */
1482 argtype = build_stub_type (basetype, quals, move_p);
1483 else if (basetype == inherited_base)
1484 argtype = inherited_parms;
1485 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1487 argtype = NULL_TREE;
1489 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1490 constexpr_p, diag, basetype);
1493 /* In a constructor we also need to check the subobject
1494 destructors for cleanup of partially constructed objects. */
1495 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1496 NULL_TREE, flags, complain);
1497 /* Note that we don't pass down trivial_p; the subobject
1498 destructors don't affect triviality of the constructor. Nor
1499 do they affect constexpr-ness (a constant expression doesn't
1500 throw) or exception-specification (a throw from one of the
1501 dtors would be a double-fault). */
1502 process_subob_fn (rval, NULL, NULL,
1503 deleted_p, NULL, false,
1507 if (check_vdtor && type_has_virtual_destructor (basetype))
1509 rval = locate_fn_flags (ctype, ansi_opname (DELETE_EXPR),
1510 ptr_type_node, flags, complain);
1511 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
1512 to have a null rval (no class-specific op delete). */
1513 if (rval && rval == error_mark_node && deleted_p)
1515 check_vdtor = false;
1518 if (diag && assign_p && move_p
1519 && BINFO_VIRTUAL_P (base_binfo)
1520 && rval && TREE_CODE (rval) == FUNCTION_DECL
1521 && move_fn_p (rval) && !trivial_fn_p (rval)
1522 && vbase_has_user_provided_move_assign (basetype))
1523 warning (OPT_Wvirtual_move_assign,
1524 "defaulted move assignment for %qT calls a non-trivial "
1525 "move assignment operator for virtual base %qT",
1529 vbases = CLASSTYPE_VBASECLASSES (ctype);
1530 if (vec_safe_is_empty (vbases))
1531 /* No virtual bases to worry about. */;
1535 *constexpr_p = false;
1536 FOR_EACH_VEC_ELT (*vbases, i, base_binfo)
1538 tree basetype = BINFO_TYPE (base_binfo);
1540 argtype = build_stub_type (basetype, quals, move_p);
1541 rval = locate_fn_flags (base_binfo, fnname, argtype, flags, complain);
1543 process_subob_fn (rval, spec_p, trivial_p, deleted_p,
1544 constexpr_p, diag, basetype);
1545 if (ctor_p && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype))
1547 rval = locate_fn_flags (base_binfo, complete_dtor_identifier,
1548 NULL_TREE, flags, complain);
1549 process_subob_fn (rval, NULL, NULL,
1550 deleted_p, NULL, false,
1556 /* Now handle the non-static data members. */
1557 walk_field_subobs (TYPE_FIELDS (ctype), fnname, sfk, quals,
1558 copy_arg_p, move_p, assign_p, spec_p, trivial_p,
1559 deleted_p, constexpr_p,
1560 diag, flags, complain);
1562 walk_field_subobs (TYPE_FIELDS (ctype), complete_dtor_identifier,
1563 sfk_destructor, TYPE_UNQUALIFIED, false,
1564 false, false, NULL, NULL,
1566 false, flags, complain);
1570 pop_deferring_access_checks ();
1571 --cp_unevaluated_operand;
1572 --c_inhibit_evaluation_warnings;
1575 /* DECL is a defaulted function whose exception specification is now
1576 needed. Return what it should be. */
1579 get_defaulted_eh_spec (tree decl)
1581 if (DECL_CLONED_FUNCTION_P (decl))
1582 decl = DECL_CLONED_FUNCTION (decl);
1583 special_function_kind sfk = special_function_p (decl);
1584 tree ctype = DECL_CONTEXT (decl);
1585 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1586 tree parm_type = TREE_VALUE (parms);
1587 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1588 tree spec = empty_except_spec;
1589 synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL,
1590 NULL, false, DECL_INHERITED_CTOR_BASE (decl),
1595 /* DECL is a deleted function. If it's implicitly deleted, explain why and
1596 return true; else return false. */
1599 maybe_explain_implicit_delete (tree decl)
1601 /* If decl is a clone, get the primary variant. */
1602 decl = DECL_ORIGIN (decl);
1603 gcc_assert (DECL_DELETED_FN (decl));
1604 if (DECL_DEFAULTED_FN (decl))
1606 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
1607 static hash_set<tree> *explained;
1609 special_function_kind sfk;
1615 explained = new hash_set<tree>;
1616 if (explained->add (decl))
1619 sfk = special_function_p (decl);
1620 ctype = DECL_CONTEXT (decl);
1621 loc = input_location;
1622 input_location = DECL_SOURCE_LOCATION (decl);
1625 if (LAMBDA_TYPE_P (ctype))
1628 if (sfk == sfk_constructor)
1629 inform (DECL_SOURCE_LOCATION (decl),
1630 "a lambda closure type has a deleted default constructor");
1631 else if (sfk == sfk_copy_assignment)
1632 inform (DECL_SOURCE_LOCATION (decl),
1633 "a lambda closure type has a deleted copy assignment operator");
1637 else if (DECL_ARTIFICIAL (decl)
1638 && (sfk == sfk_copy_assignment
1639 || sfk == sfk_copy_constructor)
1640 && (type_has_user_declared_move_constructor (ctype)
1641 || type_has_user_declared_move_assign (ctype)))
1643 inform (0, "%q+#D is implicitly declared as deleted because %qT "
1644 "declares a move constructor or move assignment operator",
1650 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
1651 tree parm_type = TREE_VALUE (parms);
1652 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1653 tree raises = NULL_TREE;
1654 bool deleted_p = false;
1655 tree scope = push_scope (ctype);
1657 synthesized_method_walk (ctype, sfk, const_p,
1658 &raises, NULL, &deleted_p, NULL, false,
1659 DECL_INHERITED_CTOR_BASE (decl), parms);
1662 inform (0, "%q+#D is implicitly deleted because the default "
1663 "definition would be ill-formed:", decl);
1664 synthesized_method_walk (ctype, sfk, const_p,
1665 NULL, NULL, NULL, NULL, true,
1666 DECL_INHERITED_CTOR_BASE (decl), parms);
1668 else if (!comp_except_specs
1669 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
1671 inform (DECL_SOURCE_LOCATION (decl), "%q#F is implicitly "
1672 "deleted because its exception-specification does not "
1673 "match the implicit exception-specification %qX",
1675 #ifdef ENABLE_CHECKING
1683 input_location = loc;
1689 /* DECL is a defaulted function which was declared constexpr. Explain why
1690 it can't be constexpr. */
1693 explain_implicit_non_constexpr (tree decl)
1695 tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl));
1696 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
1698 synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
1699 special_function_p (decl), const_p,
1700 NULL, NULL, NULL, &dummy, true,
1701 DECL_INHERITED_CTOR_BASE (decl),
1702 FUNCTION_FIRST_USER_PARMTYPE (decl));
1705 /* DECL is an instantiation of an inheriting constructor template. Deduce
1706 the correct exception-specification and deletedness for this particular
1710 deduce_inheriting_ctor (tree decl)
1712 gcc_assert (DECL_INHERITED_CTOR_BASE (decl));
1714 bool trivial, constexpr_, deleted;
1715 synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor,
1716 false, &spec, &trivial, &deleted, &constexpr_,
1718 DECL_INHERITED_CTOR_BASE (decl),
1719 FUNCTION_FIRST_USER_PARMTYPE (decl));
1720 DECL_DELETED_FN (decl) = deleted;
1721 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
1724 /* Implicitly declare the special function indicated by KIND, as a
1725 member of TYPE. For copy constructors and assignment operators,
1726 CONST_P indicates whether these functions should take a const
1727 reference argument or a non-const reference. Returns the
1728 FUNCTION_DECL for the implicitly declared function. */
1731 implicitly_declare_fn (special_function_kind kind, tree type,
1732 bool const_p, tree inherited_ctor,
1733 tree inherited_parms)
1736 tree parameter_types = void_list_node;
1739 tree raises = empty_except_spec;
1740 tree rhs_parm_type = NULL_TREE;
1743 HOST_WIDE_INT saved_processing_template_decl;
1747 /* Because we create declarations for implicitly declared functions
1748 lazily, we may be creating the declaration for a member of TYPE
1749 while in some completely different context. However, TYPE will
1750 never be a dependent class (because we never want to do lookups
1751 for implicitly defined functions in a dependent class).
1752 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
1753 because we only create clones for constructors and destructors
1754 when not in a template. */
1755 gcc_assert (!dependent_type_p (type));
1756 saved_processing_template_decl = processing_template_decl;
1757 processing_template_decl = 0;
1759 type = TYPE_MAIN_VARIANT (type);
1761 if (targetm.cxx.cdtor_returns_this () && !TYPE_FOR_JAVA (type))
1763 if (kind == sfk_destructor)
1764 /* See comment in check_special_function_return_type. */
1765 return_type = build_pointer_type (void_type_node);
1767 return_type = build_pointer_type (type);
1770 return_type = void_type_node;
1774 case sfk_destructor:
1776 name = constructor_name (type);
1779 case sfk_constructor:
1780 /* Default constructor. */
1781 name = constructor_name (type);
1784 case sfk_copy_constructor:
1785 case sfk_copy_assignment:
1786 case sfk_move_constructor:
1787 case sfk_move_assignment:
1788 case sfk_inheriting_constructor:
1791 if (kind == sfk_copy_assignment
1792 || kind == sfk_move_assignment)
1794 return_type = build_reference_type (type);
1795 name = ansi_assopname (NOP_EXPR);
1798 name = constructor_name (type);
1800 if (kind == sfk_inheriting_constructor)
1801 parameter_types = inherited_parms;
1805 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
1807 rhs_parm_type = type;
1808 move_p = (kind == sfk_move_assignment
1809 || kind == sfk_move_constructor);
1810 rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
1812 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
1820 tree inherited_base = (inherited_ctor
1821 ? DECL_CONTEXT (inherited_ctor)
1823 bool trivial_p = false;
1825 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
1827 /* For an inheriting constructor template, just copy these flags from
1828 the inherited constructor template for now. */
1829 raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor));
1830 deleted_p = DECL_DELETED_FN (inherited_ctor);
1831 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
1833 else if (cxx_dialect >= cxx11)
1835 raises = unevaluated_noexcept_spec ();
1836 synthesized_method_walk (type, kind, const_p, NULL, &trivial_p,
1837 &deleted_p, &constexpr_p, false,
1838 inherited_base, inherited_parms);
1841 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
1842 &deleted_p, &constexpr_p, false,
1843 inherited_base, inherited_parms);
1844 /* Don't bother marking a deleted constructor as constexpr. */
1846 constexpr_p = false;
1847 /* A trivial copy/move constructor is also a constexpr constructor,
1848 unless the class has virtual bases (7.1.5p4). */
1849 else if (trivial_p && cxx_dialect >= cxx11
1850 && (kind == sfk_copy_constructor
1851 || kind == sfk_move_constructor)
1852 && !CLASSTYPE_VBASECLASSES (type))
1853 gcc_assert (constexpr_p);
1855 if (!trivial_p && type_has_trivial_fn (type, kind))
1856 type_set_nontrivial_flag (type, kind);
1858 /* Create the function. */
1859 fn_type = build_method_type_directly (type, return_type, parameter_types);
1861 fn_type = build_exception_variant (fn_type, raises);
1862 fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
1863 if (kind != sfk_inheriting_constructor)
1864 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
1865 if (kind == sfk_constructor || kind == sfk_copy_constructor
1866 || kind == sfk_move_constructor || kind == sfk_inheriting_constructor)
1867 DECL_CONSTRUCTOR_P (fn) = 1;
1868 else if (kind == sfk_destructor)
1869 DECL_DESTRUCTOR_P (fn) = 1;
1872 DECL_ASSIGNMENT_OPERATOR_P (fn) = 1;
1873 SET_OVERLOADED_OPERATOR_CODE (fn, NOP_EXPR);
1876 /* If pointers to member functions use the least significant bit to
1877 indicate whether a function is virtual, ensure a pointer
1878 to this function will have that bit clear. */
1879 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
1880 && DECL_ALIGN (fn) < 2 * BITS_PER_UNIT)
1881 DECL_ALIGN (fn) = 2 * BITS_PER_UNIT;
1883 /* Create the explicit arguments. */
1886 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
1887 want its type to be included in the mangled function
1889 tree decl = cp_build_parm_decl (NULL_TREE, rhs_parm_type);
1890 TREE_READONLY (decl) = 1;
1891 retrofit_lang_decl (decl);
1892 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
1893 DECL_ARGUMENTS (fn) = decl;
1895 else if (kind == sfk_inheriting_constructor)
1897 tree *p = &DECL_ARGUMENTS (fn);
1899 for (tree parm = inherited_parms; parm != void_list_node;
1900 parm = TREE_CHAIN (parm))
1902 *p = cp_build_parm_decl (NULL_TREE, TREE_VALUE (parm));
1903 retrofit_lang_decl (*p);
1904 DECL_PARM_LEVEL (*p) = 1;
1905 DECL_PARM_INDEX (*p) = index++;
1906 DECL_CONTEXT (*p) = fn;
1907 p = &DECL_CHAIN (*p);
1909 SET_DECL_INHERITED_CTOR_BASE (fn, inherited_base);
1910 DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor);
1911 /* A constructor so declared has the same access as the corresponding
1912 constructor in X. */
1913 TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor);
1914 TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor);
1915 /* Copy constexpr from the inherited constructor even if the
1916 inheriting constructor doesn't satisfy the requirements. */
1917 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
1919 /* Add the "this" parameter. */
1920 this_parm = build_this_parm (fn_type, TYPE_UNQUALIFIED);
1921 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
1922 DECL_ARGUMENTS (fn) = this_parm;
1924 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
1925 DECL_IN_AGGR_P (fn) = 1;
1926 DECL_ARTIFICIAL (fn) = 1;
1927 DECL_DEFAULTED_FN (fn) = 1;
1928 if (cxx_dialect >= cxx11)
1930 /* "The closure type associated with a lambda-expression has a deleted
1931 default constructor and a deleted copy assignment operator." */
1932 if ((kind == sfk_constructor
1933 || kind == sfk_copy_assignment)
1934 && LAMBDA_TYPE_P (type))
1936 DECL_DELETED_FN (fn) = deleted_p;
1937 DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
1939 DECL_EXTERNAL (fn) = true;
1940 DECL_NOT_REALLY_EXTERN (fn) = 1;
1941 DECL_DECLARED_INLINE_P (fn) = 1;
1942 DECL_COMDAT (fn) = 1;
1943 set_linkage_according_to_type (type, fn);
1944 rest_of_decl_compilation (fn, toplevel_bindings_p (), at_eof);
1945 gcc_assert (!TREE_USED (fn));
1947 /* Restore PROCESSING_TEMPLATE_DECL. */
1948 processing_template_decl = saved_processing_template_decl;
1950 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
1951 fn = add_inherited_template_parms (fn, inherited_ctor);
1953 /* Warn about calling a non-trivial move assignment in a virtual base. */
1954 if (kind == sfk_move_assignment && !deleted_p && !trivial_p
1955 && CLASSTYPE_VBASECLASSES (type))
1957 location_t loc = input_location;
1958 input_location = DECL_SOURCE_LOCATION (fn);
1959 synthesized_method_walk (type, kind, const_p,
1960 NULL, NULL, NULL, NULL, true,
1961 NULL_TREE, NULL_TREE);
1962 input_location = loc;
1968 /* Gives any errors about defaulted functions which need to be deferred
1969 until the containing class is complete. */
1972 defaulted_late_check (tree fn)
1974 /* Complain about invalid signature for defaulted fn. */
1975 tree ctx = DECL_CONTEXT (fn);
1976 special_function_kind kind = special_function_p (fn);
1977 bool fn_const_p = (copy_fn_p (fn) == 2);
1978 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
1980 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
1982 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
1983 TREE_TYPE (TREE_TYPE (implicit_fn)))
1984 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1985 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn))))
1987 error ("defaulted declaration %q+D", fn);
1988 error_at (DECL_SOURCE_LOCATION (fn),
1989 "does not match expected signature %qD", implicit_fn);
1992 /* 8.4.2/2: An explicitly-defaulted function (...) may have an explicit
1993 exception-specification only if it is compatible (15.4) with the
1994 exception-specification on the implicit declaration. If a function
1995 is explicitly defaulted on its first declaration, (...) it is
1996 implicitly considered to have the same exception-specification as if
1997 it had been implicitly declared. */
1998 maybe_instantiate_noexcept (fn);
1999 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2002 if (DECL_DEFAULTED_IN_CLASS_P (fn))
2003 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
2005 else if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
2006 /* Equivalent to the implicit spec. */;
2007 else if (DECL_DEFAULTED_IN_CLASS_P (fn)
2008 && !CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
2009 /* We can't compare an explicit exception-specification on a
2010 constructor defaulted in the class body to the implicit
2011 exception-specification until after we've parsed any NSDMI; see
2012 after_nsdmi_defaulted_late_checks. */;
2015 tree eh_spec = get_defaulted_eh_spec (fn);
2016 if (!comp_except_specs (fn_spec, eh_spec, ce_normal))
2018 if (DECL_DEFAULTED_IN_CLASS_P (fn))
2019 DECL_DELETED_FN (fn) = true;
2021 error ("function %q+D defaulted on its redeclaration "
2022 "with an exception-specification that differs from "
2023 "the implicit exception-specification %qX", fn, eh_spec);
2027 if (DECL_DEFAULTED_IN_CLASS_P (fn)
2028 && DECL_DECLARED_CONSTEXPR_P (implicit_fn))
2030 /* Hmm...should we do this for out-of-class too? Should it be OK to
2031 add constexpr later like inline, rather than requiring
2032 declarations to match? */
2033 DECL_DECLARED_CONSTEXPR_P (fn) = true;
2034 if (kind == sfk_constructor)
2035 TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
2038 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
2039 && DECL_DECLARED_CONSTEXPR_P (fn))
2041 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
2043 error ("explicitly defaulted function %q+D cannot be declared "
2044 "as constexpr because the implicit declaration is not "
2046 explain_implicit_non_constexpr (fn);
2048 DECL_DECLARED_CONSTEXPR_P (fn) = false;
2051 if (DECL_DELETED_FN (implicit_fn))
2052 DECL_DELETED_FN (fn) = 1;
2055 /* OK, we've parsed the NSDMI for class T, now we can check any explicit
2056 exception-specifications on functions defaulted in the class body. */
2059 after_nsdmi_defaulted_late_checks (tree t)
2061 if (uses_template_parms (t))
2063 if (t == error_mark_node)
2065 for (tree fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn))
2066 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn))
2068 tree fn_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2069 if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec))
2072 tree eh_spec = get_defaulted_eh_spec (fn);
2073 if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn)),
2074 eh_spec, ce_normal))
2075 DECL_DELETED_FN (fn) = true;
2079 /* Returns true iff FN can be explicitly defaulted, and gives any
2080 errors if defaulting FN is ill-formed. */
2083 defaultable_fn_check (tree fn)
2085 special_function_kind kind = sfk_none;
2087 if (template_parm_scope_p ())
2089 error ("a template cannot be defaulted");
2093 if (DECL_CONSTRUCTOR_P (fn))
2095 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
2096 kind = sfk_constructor;
2097 else if (copy_fn_p (fn) > 0
2098 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
2100 kind = sfk_copy_constructor;
2101 else if (move_fn_p (fn))
2102 kind = sfk_move_constructor;
2104 else if (DECL_DESTRUCTOR_P (fn))
2105 kind = sfk_destructor;
2106 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
2107 && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
2110 kind = sfk_copy_assignment;
2111 else if (move_fn_p (fn))
2112 kind = sfk_move_assignment;
2115 if (kind == sfk_none)
2117 error ("%qD cannot be defaulted", fn);
2122 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
2123 t && t != void_list_node; t = TREE_CHAIN (t))
2124 if (TREE_PURPOSE (t))
2126 error ("defaulted function %q+D with default argument", fn);
2130 /* Avoid do_warn_unused_parameter warnings. */
2131 for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p))
2133 TREE_NO_WARNING (p) = 1;
2135 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn)))
2136 /* Defer checking. */;
2137 else if (!processing_template_decl)
2138 defaulted_late_check (fn);
2144 /* Add an implicit declaration to TYPE for the kind of function
2145 indicated by SFK. Return the FUNCTION_DECL for the new implicit
2149 lazily_declare_fn (special_function_kind sfk, tree type)
2152 /* Whether or not the argument has a const reference type. */
2153 bool const_p = false;
2157 case sfk_constructor:
2158 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
2160 case sfk_copy_constructor:
2161 const_p = TYPE_HAS_CONST_COPY_CTOR (type);
2162 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
2164 case sfk_move_constructor:
2165 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
2167 case sfk_copy_assignment:
2168 const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
2169 CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
2171 case sfk_move_assignment:
2172 CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
2174 case sfk_destructor:
2175 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
2181 /* Declare the function. */
2182 fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL);
2184 /* [class.copy]/8 If the class definition declares a move constructor or
2185 move assignment operator, the implicitly declared copy constructor is
2186 defined as deleted.... */
2187 if ((sfk == sfk_copy_assignment
2188 || sfk == sfk_copy_constructor)
2189 && (type_has_user_declared_move_constructor (type)
2190 || type_has_user_declared_move_assign (type)))
2191 DECL_DELETED_FN (fn) = true;
2193 /* A destructor may be virtual. */
2194 if (sfk == sfk_destructor
2195 || sfk == sfk_move_assignment
2196 || sfk == sfk_copy_assignment)
2197 check_for_override (fn, type);
2198 /* Add it to CLASSTYPE_METHOD_VEC. */
2199 add_method (type, fn, NULL_TREE);
2200 /* Add it to TYPE_METHODS. */
2201 if (sfk == sfk_destructor
2202 && DECL_VIRTUAL_P (fn))
2203 /* The ABI requires that a virtual destructor go at the end of the
2205 TYPE_METHODS (type) = chainon (TYPE_METHODS (type), fn);
2208 DECL_CHAIN (fn) = TYPE_METHODS (type);
2209 TYPE_METHODS (type) = fn;
2211 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
2212 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)
2213 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
2214 /* Create appropriate clones. */
2215 clone_function_decl (fn, /*update_method_vec=*/true);
2220 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
2221 as there are artificial parms in FN. */
2224 skip_artificial_parms_for (const_tree fn, tree list)
2226 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2227 list = TREE_CHAIN (list);
2231 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2232 list = TREE_CHAIN (list);
2233 if (DECL_HAS_VTT_PARM_P (fn))
2234 list = TREE_CHAIN (list);
2238 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
2239 artificial parms in FN. */
2242 num_artificial_parms_for (const_tree fn)
2246 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2251 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
2253 if (DECL_HAS_VTT_PARM_P (fn))
2259 #include "gt-cp-method.h"