1 /* Handle the hair of processing (but not expanding) inline functions.
2 Also manage function and variable name overloading.
3 Copyright (C) 1987-2017 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* Handle method declarations. */
26 #include "coretypes.h"
29 #include "stringpool.h"
33 #include "common/common-target.h"
35 /* Various flags to control the mangling process. */
41 /* The thing we are presently mangling is part of a template type,
42 rather than a fully instantiated type. Therefore, we may see
43 complex expressions where we would normally expect to see a
44 simple integer constant. */
45 mf_maybe_uninstantiated
= 1,
46 /* When mangling a numeric value, use the form `_XX_' (instead of
47 just `XX') if the value has more than one digit. */
48 mf_use_underscores_around_value
= 2
51 static void do_build_copy_assign (tree
);
52 static void do_build_copy_constructor (tree
);
53 static tree
make_alias_for_thunk (tree
);
55 /* Called once to initialize method.c. */
63 /* Return a this or result adjusting thunk to FUNCTION. THIS_ADJUSTING
64 indicates whether it is a this or result adjusting thunk.
65 FIXED_OFFSET and VIRTUAL_OFFSET indicate how to do the adjustment
66 (see thunk_adjust). VIRTUAL_OFFSET can be NULL, but FIXED_OFFSET
67 never is. VIRTUAL_OFFSET is the /index/ into the vtable for this
68 adjusting thunks, we scale it to a byte offset. For covariant
69 thunks VIRTUAL_OFFSET is the virtual binfo. You must post process
70 the returned thunk with finish_thunk. */
73 make_thunk (tree function
, bool this_adjusting
,
74 tree fixed_offset
, tree virtual_offset
)
79 gcc_assert (TREE_CODE (function
) == FUNCTION_DECL
);
80 /* We can have this thunks to covariant thunks, but not vice versa. */
81 gcc_assert (!DECL_THIS_THUNK_P (function
));
82 gcc_assert (!DECL_RESULT_THUNK_P (function
) || this_adjusting
);
84 /* Scale the VIRTUAL_OFFSET to be in terms of bytes. */
85 if (this_adjusting
&& virtual_offset
)
87 = size_binop (MULT_EXPR
,
90 TYPE_SIZE_UNIT (vtable_entry_type
)));
92 d
= tree_to_shwi (fixed_offset
);
94 /* See if we already have the thunk in question. For this_adjusting
95 thunks VIRTUAL_OFFSET will be an INTEGER_CST, for covariant thunks it
97 for (thunk
= DECL_THUNKS (function
); thunk
; thunk
= DECL_CHAIN (thunk
))
98 if (DECL_THIS_THUNK_P (thunk
) == this_adjusting
99 && THUNK_FIXED_OFFSET (thunk
) == d
100 && !virtual_offset
== !THUNK_VIRTUAL_OFFSET (thunk
)
103 ? tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk
),
105 : THUNK_VIRTUAL_OFFSET (thunk
) == virtual_offset
)))
108 /* All thunks must be created before FUNCTION is actually emitted;
109 the ABI requires that all thunks be emitted together with the
110 function to which they transfer control. */
111 gcc_assert (!TREE_ASM_WRITTEN (function
));
112 /* Likewise, we can only be adding thunks to a function declared in
113 the class currently being laid out. */
114 gcc_assert (TYPE_SIZE (DECL_CONTEXT (function
))
115 && TYPE_BEING_DEFINED (DECL_CONTEXT (function
)));
117 thunk
= build_decl (DECL_SOURCE_LOCATION (function
),
118 FUNCTION_DECL
, NULL_TREE
, TREE_TYPE (function
));
119 DECL_LANG_SPECIFIC (thunk
) = DECL_LANG_SPECIFIC (function
);
120 cxx_dup_lang_specific_decl (thunk
);
121 DECL_VIRTUAL_P (thunk
) = true;
122 SET_DECL_THUNKS (thunk
, NULL_TREE
);
124 DECL_CONTEXT (thunk
) = DECL_CONTEXT (function
);
125 TREE_READONLY (thunk
) = TREE_READONLY (function
);
126 TREE_THIS_VOLATILE (thunk
) = TREE_THIS_VOLATILE (function
);
127 TREE_PUBLIC (thunk
) = TREE_PUBLIC (function
);
128 SET_DECL_THUNK_P (thunk
, this_adjusting
);
129 THUNK_TARGET (thunk
) = function
;
130 THUNK_FIXED_OFFSET (thunk
) = d
;
131 THUNK_VIRTUAL_OFFSET (thunk
) = virtual_offset
;
132 THUNK_ALIAS (thunk
) = NULL_TREE
;
134 DECL_INTERFACE_KNOWN (thunk
) = 1;
135 DECL_NOT_REALLY_EXTERN (thunk
) = 1;
136 DECL_COMDAT (thunk
) = DECL_COMDAT (function
);
137 DECL_SAVED_FUNCTION_DATA (thunk
) = NULL
;
138 /* The thunk itself is not a constructor or destructor, even if
139 the thing it is thunking to is. */
140 DECL_DESTRUCTOR_P (thunk
) = 0;
141 DECL_CONSTRUCTOR_P (thunk
) = 0;
142 DECL_EXTERNAL (thunk
) = 1;
143 DECL_ARTIFICIAL (thunk
) = 1;
144 /* The THUNK is not a pending inline, even if the FUNCTION is. */
145 DECL_PENDING_INLINE_P (thunk
) = 0;
146 DECL_DECLARED_INLINE_P (thunk
) = 0;
147 /* Nor is it a template instantiation. */
148 DECL_USE_TEMPLATE (thunk
) = 0;
149 DECL_TEMPLATE_INFO (thunk
) = NULL
;
151 /* Add it to the list of thunks associated with FUNCTION. */
152 DECL_CHAIN (thunk
) = DECL_THUNKS (function
);
153 SET_DECL_THUNKS (function
, thunk
);
158 /* Finish THUNK, a thunk decl. */
161 finish_thunk (tree thunk
)
164 tree fixed_offset
= ssize_int (THUNK_FIXED_OFFSET (thunk
));
165 tree virtual_offset
= THUNK_VIRTUAL_OFFSET (thunk
);
167 gcc_assert (!DECL_NAME (thunk
) && DECL_THUNK_P (thunk
));
168 if (virtual_offset
&& DECL_RESULT_THUNK_P (thunk
))
169 virtual_offset
= BINFO_VPTR_FIELD (virtual_offset
);
170 function
= THUNK_TARGET (thunk
);
171 name
= mangle_thunk (function
, DECL_THIS_THUNK_P (thunk
),
172 fixed_offset
, virtual_offset
, thunk
);
174 /* We can end up with declarations of (logically) different
175 covariant thunks, that do identical adjustments. The two thunks
176 will be adjusting between within different hierarchies, which
177 happen to have the same layout. We must nullify one of them to
178 refer to the other. */
179 if (DECL_RESULT_THUNK_P (thunk
))
183 for (cov_probe
= DECL_THUNKS (function
);
184 cov_probe
; cov_probe
= DECL_CHAIN (cov_probe
))
185 if (DECL_NAME (cov_probe
) == name
)
187 gcc_assert (!DECL_THUNKS (thunk
));
188 THUNK_ALIAS (thunk
) = (THUNK_ALIAS (cov_probe
)
189 ? THUNK_ALIAS (cov_probe
) : cov_probe
);
194 DECL_NAME (thunk
) = name
;
195 SET_DECL_ASSEMBLER_NAME (thunk
, name
);
198 static GTY (()) int thunk_labelno
;
200 /* Create a static alias to target. */
203 make_alias_for (tree target
, tree newid
)
205 tree alias
= build_decl (DECL_SOURCE_LOCATION (target
),
206 TREE_CODE (target
), newid
, TREE_TYPE (target
));
207 DECL_LANG_SPECIFIC (alias
) = DECL_LANG_SPECIFIC (target
);
208 cxx_dup_lang_specific_decl (alias
);
209 DECL_CONTEXT (alias
) = NULL
;
210 TREE_READONLY (alias
) = TREE_READONLY (target
);
211 TREE_THIS_VOLATILE (alias
) = TREE_THIS_VOLATILE (target
);
212 TREE_PUBLIC (alias
) = 0;
213 DECL_INTERFACE_KNOWN (alias
) = 1;
214 if (DECL_LANG_SPECIFIC (alias
))
216 DECL_NOT_REALLY_EXTERN (alias
) = 1;
217 DECL_USE_TEMPLATE (alias
) = 0;
218 DECL_TEMPLATE_INFO (alias
) = NULL
;
220 DECL_EXTERNAL (alias
) = 0;
221 DECL_ARTIFICIAL (alias
) = 1;
222 DECL_TEMPLATE_INSTANTIATED (alias
) = 0;
223 if (TREE_CODE (alias
) == FUNCTION_DECL
)
225 DECL_SAVED_FUNCTION_DATA (alias
) = NULL
;
226 DECL_DESTRUCTOR_P (alias
) = 0;
227 DECL_CONSTRUCTOR_P (alias
) = 0;
228 DECL_PENDING_INLINE_P (alias
) = 0;
229 DECL_DECLARED_INLINE_P (alias
) = 0;
230 DECL_INITIAL (alias
) = error_mark_node
;
231 DECL_ARGUMENTS (alias
) = copy_list (DECL_ARGUMENTS (target
));
234 TREE_STATIC (alias
) = 1;
235 TREE_ADDRESSABLE (alias
) = 1;
236 TREE_USED (alias
) = 1;
237 SET_DECL_ASSEMBLER_NAME (alias
, DECL_NAME (alias
));
242 make_alias_for_thunk (tree function
)
247 targetm
.asm_out
.generate_internal_label (buf
, "LTHUNK", thunk_labelno
);
250 alias
= make_alias_for (function
, get_identifier (buf
));
252 if (!flag_syntax_only
)
254 struct cgraph_node
*funcn
, *aliasn
;
255 funcn
= cgraph_node::get (function
);
256 gcc_checking_assert (funcn
);
257 aliasn
= cgraph_node::create_same_body_alias (alias
, function
);
258 DECL_ASSEMBLER_NAME (function
);
259 gcc_assert (aliasn
!= NULL
);
265 /* Emit the definition of a C++ multiple inheritance or covariant
266 return vtable thunk. If EMIT_P is nonzero, the thunk is emitted
270 use_thunk (tree thunk_fndecl
, bool emit_p
)
272 tree a
, t
, function
, alias
;
274 HOST_WIDE_INT fixed_offset
, virtual_value
;
275 bool this_adjusting
= DECL_THIS_THUNK_P (thunk_fndecl
);
276 struct cgraph_node
*funcn
, *thunk_node
;
278 /* We should have called finish_thunk to give it a name. */
279 gcc_assert (DECL_NAME (thunk_fndecl
));
281 /* We should never be using an alias, always refer to the
283 gcc_assert (!THUNK_ALIAS (thunk_fndecl
));
285 if (TREE_ASM_WRITTEN (thunk_fndecl
))
288 function
= THUNK_TARGET (thunk_fndecl
);
289 if (DECL_RESULT (thunk_fndecl
))
290 /* We already turned this thunk into an ordinary function.
291 There's no need to process this thunk again. */
294 if (DECL_THUNK_P (function
))
295 /* The target is itself a thunk, process it now. */
296 use_thunk (function
, emit_p
);
298 /* Thunks are always addressable; they only appear in vtables. */
299 TREE_ADDRESSABLE (thunk_fndecl
) = 1;
301 /* Figure out what function is being thunked to. It's referenced in
302 this translation unit. */
303 TREE_ADDRESSABLE (function
) = 1;
304 mark_used (function
);
308 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function
))
309 alias
= make_alias_for_thunk (function
);
313 fixed_offset
= THUNK_FIXED_OFFSET (thunk_fndecl
);
314 virtual_offset
= THUNK_VIRTUAL_OFFSET (thunk_fndecl
);
319 virtual_offset
= BINFO_VPTR_FIELD (virtual_offset
);
320 virtual_value
= tree_to_shwi (virtual_offset
);
321 gcc_assert (virtual_value
);
326 /* And, if we need to emit the thunk, it's used. */
327 mark_used (thunk_fndecl
);
328 /* This thunk is actually defined. */
329 DECL_EXTERNAL (thunk_fndecl
) = 0;
330 /* The linkage of the function may have changed. FIXME in linkage
332 gcc_assert (DECL_INTERFACE_KNOWN (function
));
333 TREE_PUBLIC (thunk_fndecl
) = TREE_PUBLIC (function
);
334 DECL_VISIBILITY (thunk_fndecl
) = DECL_VISIBILITY (function
);
335 DECL_VISIBILITY_SPECIFIED (thunk_fndecl
)
336 = DECL_VISIBILITY_SPECIFIED (function
);
337 DECL_COMDAT (thunk_fndecl
) = DECL_COMDAT (function
);
338 DECL_WEAK (thunk_fndecl
) = DECL_WEAK (function
);
340 if (flag_syntax_only
)
342 TREE_ASM_WRITTEN (thunk_fndecl
) = 1;
346 push_to_top_level ();
348 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function
)
349 && targetm_common
.have_named_sections
)
352 struct symtab_node
*symbol
;
354 if ((symbol
= symtab_node::get (function
))
357 if (symbol
->analyzed
)
358 fn
= symtab_node::get (function
)->ultimate_alias_target ()->decl
;
360 fn
= symtab_node::get (function
)->alias_target
;
362 resolve_unique_section (fn
, 0, flag_function_sections
);
364 if (DECL_SECTION_NAME (fn
) != NULL
&& DECL_ONE_ONLY (fn
))
366 resolve_unique_section (thunk_fndecl
, 0, flag_function_sections
);
368 /* Output the thunk into the same section as function. */
369 set_decl_section_name (thunk_fndecl
, DECL_SECTION_NAME (fn
));
370 symtab_node::get (thunk_fndecl
)->implicit_section
371 = symtab_node::get (fn
)->implicit_section
;
375 /* Set up cloned argument trees for the thunk. */
377 for (a
= DECL_ARGUMENTS (function
); a
; a
= DECL_CHAIN (a
))
379 tree x
= copy_node (a
);
381 DECL_CONTEXT (x
) = thunk_fndecl
;
382 SET_DECL_RTL (x
, NULL
);
383 DECL_HAS_VALUE_EXPR_P (x
) = 0;
384 TREE_ADDRESSABLE (x
) = 0;
388 DECL_ARGUMENTS (thunk_fndecl
) = a
;
389 TREE_ASM_WRITTEN (thunk_fndecl
) = 1;
390 funcn
= cgraph_node::get (function
);
391 gcc_checking_assert (funcn
);
392 thunk_node
= funcn
->create_thunk (thunk_fndecl
, function
,
393 this_adjusting
, fixed_offset
, virtual_value
,
394 virtual_offset
, alias
);
395 if (DECL_ONE_ONLY (function
))
396 thunk_node
->add_to_same_comdat_group (funcn
);
398 pop_from_top_level ();
401 /* Code for synthesizing methods which have default semantics defined. */
403 /* True iff CTYPE has a trivial SFK. */
406 type_has_trivial_fn (tree ctype
, special_function_kind sfk
)
410 case sfk_constructor
:
411 return !TYPE_HAS_COMPLEX_DFLT (ctype
);
412 case sfk_copy_constructor
:
413 return !TYPE_HAS_COMPLEX_COPY_CTOR (ctype
);
414 case sfk_move_constructor
:
415 return !TYPE_HAS_COMPLEX_MOVE_CTOR (ctype
);
416 case sfk_copy_assignment
:
417 return !TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype
);
418 case sfk_move_assignment
:
419 return !TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype
);
421 return !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype
);
422 case sfk_inheriting_constructor
:
429 /* Note that CTYPE has a non-trivial SFK even though we previously thought
433 type_set_nontrivial_flag (tree ctype
, special_function_kind sfk
)
437 case sfk_constructor
:
438 TYPE_HAS_COMPLEX_DFLT (ctype
) = true;
440 case sfk_copy_constructor
:
441 TYPE_HAS_COMPLEX_COPY_CTOR (ctype
) = true;
443 case sfk_move_constructor
:
444 TYPE_HAS_COMPLEX_MOVE_CTOR (ctype
) = true;
446 case sfk_copy_assignment
:
447 TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype
) = true;
449 case sfk_move_assignment
:
450 TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype
) = true;
453 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype
) = true;
455 case sfk_inheriting_constructor
:
461 /* True iff FN is a trivial defaulted member function ([cd]tor, op=). */
464 trivial_fn_p (tree fn
)
466 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
468 if (!DECL_DEFAULTED_FN (fn
))
471 /* If fn is a clone, get the primary variant. */
472 if (tree prim
= DECL_CLONED_FUNCTION (fn
))
474 return type_has_trivial_fn (DECL_CONTEXT (fn
), special_function_p (fn
));
477 /* PARM is a PARM_DECL for a function which we want to forward to another
478 function without changing its value category, a la std::forward. */
481 forward_parm (tree parm
)
483 tree exp
= convert_from_reference (parm
);
484 tree type
= TREE_TYPE (parm
);
485 if (DECL_PACK_P (parm
))
486 type
= PACK_EXPANSION_PATTERN (type
);
487 if (TREE_CODE (type
) != REFERENCE_TYPE
)
488 type
= cp_build_reference_type (type
, /*rval=*/true);
489 exp
= build_static_cast (type
, exp
, tf_warning_or_error
);
490 if (DECL_PACK_P (parm
))
491 exp
= make_pack_expansion (exp
);
495 /* Strip all inheriting constructors, if any, to return the original
496 constructor from a (possibly indirect) base class. */
499 strip_inheriting_ctors (tree dfn
)
501 gcc_assert (flag_new_inheriting_ctors
);
503 while (tree inh
= DECL_INHERITED_CTOR (fn
))
505 inh
= OVL_CURRENT (inh
);
508 if (TREE_CODE (fn
) == TEMPLATE_DECL
509 && TREE_CODE (dfn
) == FUNCTION_DECL
)
510 fn
= DECL_TEMPLATE_RESULT (fn
);
514 /* Find the binfo for the base subobject of BINFO being initialized by
515 inherited constructor FNDECL (a member of a direct base of BINFO). */
517 static tree
inherited_ctor_binfo (tree
, tree
);
519 inherited_ctor_binfo_1 (tree binfo
, tree fndecl
)
521 tree base
= DECL_CONTEXT (fndecl
);
523 for (int i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
524 if (BINFO_TYPE (base_binfo
) == base
)
525 return inherited_ctor_binfo (base_binfo
, fndecl
);
530 /* Find the binfo for the base subobject of BINFO being initialized by
531 inheriting constructor FNDECL (a member of BINFO), or BINFO if FNDECL is not
532 an inheriting constructor. */
535 inherited_ctor_binfo (tree binfo
, tree fndecl
)
537 tree inh
= DECL_INHERITED_CTOR (fndecl
);
541 tree results
= NULL_TREE
;
542 for (; inh
; inh
= OVL_NEXT (inh
))
544 tree one
= inherited_ctor_binfo_1 (binfo
, OVL_CURRENT (inh
));
547 else if (one
!= results
)
548 results
= tree_cons (NULL_TREE
, one
, results
);
553 /* Find the binfo for the base subobject being initialized by inheriting
554 constructor FNDECL, or NULL_TREE if FNDECL is not an inheriting
558 inherited_ctor_binfo (tree fndecl
)
560 if (!DECL_INHERITED_CTOR (fndecl
))
562 tree binfo
= TYPE_BINFO (DECL_CONTEXT (fndecl
));
563 return inherited_ctor_binfo (binfo
, fndecl
);
566 /* True if we should omit all user-declared parameters from constructor FN,
567 because it is a base clone of a ctor inherited from a virtual base. */
570 ctor_omit_inherited_parms (tree fn
)
572 if (!flag_new_inheriting_ctors
)
573 /* We only optimize away the parameters in the new model. */
575 if (!DECL_BASE_CONSTRUCTOR_P (fn
)
576 || !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn
)))
578 tree binfo
= inherited_ctor_binfo (fn
);
579 for (; binfo
; binfo
= BINFO_INHERITANCE_CHAIN (binfo
))
580 if (BINFO_VIRTUAL_P (binfo
))
585 /* True iff constructor(s) INH inherited into BINFO initializes INIT_BINFO.
586 This can be true for multiple virtual bases as well as one direct
590 binfo_inherited_from (tree binfo
, tree init_binfo
, tree inh
)
592 /* inh is an OVERLOAD if we inherited the same constructor along
593 multiple paths, check all of them. */
594 for (; inh
; inh
= OVL_NEXT (inh
))
596 tree fn
= OVL_CURRENT (inh
);
597 tree base
= DECL_CONTEXT (fn
);
598 tree base_binfo
= NULL_TREE
;
599 for (int i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
600 if (BINFO_TYPE (base_binfo
) == base
)
602 if (base_binfo
== init_binfo
603 || (flag_new_inheriting_ctors
604 && binfo_inherited_from (base_binfo
, init_binfo
,
605 DECL_INHERITED_CTOR (fn
))))
611 /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO
612 given the parameter or parameters PARM, possibly inherited constructor
613 base INH, or move flag MOVE_P. */
616 add_one_base_init (tree binfo
, tree parm
, bool move_p
, tree inh
,
617 tree member_init_list
)
622 /* An inheriting constructor only has a mem-initializer for
623 the base it inherits from. */
624 if (!binfo_inherited_from (TYPE_BINFO (current_class_type
), binfo
, inh
))
625 return member_init_list
;
629 for (; parm
; parm
= DECL_CHAIN (parm
))
631 tree exp
= forward_parm (parm
);
632 *p
= build_tree_list (NULL_TREE
, exp
);
633 p
= &TREE_CHAIN (*p
);
638 init
= build_base_path (PLUS_EXPR
, parm
, binfo
, 1,
639 tf_warning_or_error
);
642 init
= build_tree_list (NULL_TREE
, init
);
644 return tree_cons (binfo
, init
, member_init_list
);
647 /* Generate code for default X(X&) or X(X&&) constructor or an inheriting
651 do_build_copy_constructor (tree fndecl
)
653 tree parm
= FUNCTION_FIRST_USER_PARM (fndecl
);
654 bool move_p
= DECL_MOVE_CONSTRUCTOR_P (fndecl
);
655 bool trivial
= trivial_fn_p (fndecl
);
656 tree inh
= DECL_INHERITED_CTOR (fndecl
);
659 parm
= convert_from_reference (parm
);
663 if (is_empty_class (current_class_type
))
664 /* Don't copy the padding byte; it might not have been allocated
665 if *this is a base subobject. */;
666 else if (tree_int_cst_equal (TYPE_SIZE (current_class_type
),
667 CLASSTYPE_SIZE (current_class_type
)))
669 tree t
= build2 (INIT_EXPR
, void_type_node
, current_class_ref
, parm
);
670 finish_expr_stmt (t
);
674 /* We must only copy the non-tail padding parts. */
675 tree base_size
= CLASSTYPE_SIZE_UNIT (current_class_type
);
676 base_size
= size_binop (MINUS_EXPR
, base_size
, size_int (1));
677 tree array_type
= build_array_type (unsigned_char_type_node
,
678 build_index_type (base_size
));
679 tree alias_set
= build_int_cst (TREE_TYPE (current_class_ptr
), 0);
680 tree lhs
= build2 (MEM_REF
, array_type
,
681 current_class_ptr
, alias_set
);
682 tree rhs
= build2 (MEM_REF
, array_type
,
683 TREE_OPERAND (parm
, 0), alias_set
);
684 tree t
= build2 (INIT_EXPR
, void_type_node
, lhs
, rhs
);
685 finish_expr_stmt (t
);
690 tree fields
= TYPE_FIELDS (current_class_type
);
691 tree member_init_list
= NULL_TREE
;
692 int cvquals
= cp_type_quals (TREE_TYPE (parm
));
694 tree binfo
, base_binfo
;
696 vec
<tree
, va_gc
> *vbases
;
698 /* Initialize all the base-classes with the parameter converted
699 to their type so that we get their copy constructor and not
700 another constructor that takes current_class_type. We must
701 deal with the binfo's directly as a direct base might be
702 inaccessible due to ambiguity. */
703 for (vbases
= CLASSTYPE_VBASECLASSES (current_class_type
), i
= 0;
704 vec_safe_iterate (vbases
, i
, &binfo
); i
++)
706 member_init_list
= add_one_base_init (binfo
, parm
, move_p
, inh
,
710 for (binfo
= TYPE_BINFO (current_class_type
), i
= 0;
711 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
713 if (BINFO_VIRTUAL_P (base_binfo
))
715 member_init_list
= add_one_base_init (base_binfo
, parm
, move_p
,
716 inh
, member_init_list
);
719 for (; fields
; fields
= DECL_CHAIN (fields
))
724 if (TREE_CODE (field
) != FIELD_DECL
)
729 expr_type
= TREE_TYPE (field
);
730 if (DECL_NAME (field
))
732 if (VFIELD_NAME_P (DECL_NAME (field
)))
735 else if (ANON_AGGR_TYPE_P (expr_type
) && TYPE_FIELDS (expr_type
))
736 /* Just use the field; anonymous types can't have
737 nontrivial copy ctors or assignment ops or this
738 function would be deleted. */;
742 /* Compute the type of "init->field". If the copy-constructor
743 parameter is, for example, "const S&", and the type of
744 the field is "T", then the type will usually be "const
745 T". (There are no cv-qualified variants of reference
747 if (TREE_CODE (expr_type
) != REFERENCE_TYPE
)
751 if (DECL_MUTABLE_P (field
))
752 quals
&= ~TYPE_QUAL_CONST
;
753 quals
|= cp_type_quals (expr_type
);
754 expr_type
= cp_build_qualified_type (expr_type
, quals
);
757 init
= build3 (COMPONENT_REF
, expr_type
, parm
, field
, NULL_TREE
);
758 if (move_p
&& TREE_CODE (expr_type
) != REFERENCE_TYPE
759 /* 'move' breaks bit-fields, and has no effect for scalars. */
760 && !scalarish_type_p (expr_type
))
762 init
= build_tree_list (NULL_TREE
, init
);
764 member_init_list
= tree_cons (field
, init
, member_init_list
);
766 finish_mem_initializers (member_init_list
);
771 do_build_copy_assign (tree fndecl
)
773 tree parm
= DECL_CHAIN (DECL_ARGUMENTS (fndecl
));
775 bool move_p
= move_fn_p (fndecl
);
776 bool trivial
= trivial_fn_p (fndecl
);
777 int flags
= LOOKUP_NORMAL
| LOOKUP_NONVIRTUAL
| LOOKUP_DEFAULTED
;
779 compound_stmt
= begin_compound_stmt (0);
780 parm
= convert_from_reference (parm
);
783 && is_empty_class (current_class_type
))
784 /* Don't copy the padding byte; it might not have been allocated
785 if *this is a base subobject. */;
788 tree t
= build2 (MODIFY_EXPR
, void_type_node
, current_class_ref
, parm
);
789 finish_expr_stmt (t
);
794 int cvquals
= cp_type_quals (TREE_TYPE (parm
));
796 tree binfo
, base_binfo
;
798 /* Assign to each of the direct base classes. */
799 for (binfo
= TYPE_BINFO (current_class_type
), i
= 0;
800 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
803 vec
<tree
, va_gc
> *parmvec
;
805 /* We must convert PARM directly to the base class
806 explicitly since the base class may be ambiguous. */
807 converted_parm
= build_base_path (PLUS_EXPR
, parm
, base_binfo
, 1,
808 tf_warning_or_error
);
810 converted_parm
= move (converted_parm
);
811 /* Call the base class assignment operator. */
812 parmvec
= make_tree_vector_single (converted_parm
);
814 (build_special_member_call (current_class_ref
,
815 ansi_assopname (NOP_EXPR
),
819 tf_warning_or_error
));
820 release_tree_vector (parmvec
);
823 /* Assign to each of the non-static data members. */
824 for (fields
= TYPE_FIELDS (current_class_type
);
826 fields
= DECL_CHAIN (fields
))
828 tree comp
= current_class_ref
;
834 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
837 expr_type
= TREE_TYPE (field
);
839 if (CP_TYPE_CONST_P (expr_type
))
841 error ("non-static const member %q#D, can%'t use default "
842 "assignment operator", field
);
845 else if (TREE_CODE (expr_type
) == REFERENCE_TYPE
)
847 error ("non-static reference member %q#D, can%'t use "
848 "default assignment operator", field
);
852 if (DECL_NAME (field
))
854 if (VFIELD_NAME_P (DECL_NAME (field
)))
857 else if (ANON_AGGR_TYPE_P (expr_type
)
858 && TYPE_FIELDS (expr_type
) != NULL_TREE
)
859 /* Just use the field; anonymous types can't have
860 nontrivial copy ctors or assignment ops or this
861 function would be deleted. */;
865 comp
= build3 (COMPONENT_REF
, expr_type
, comp
, field
, NULL_TREE
);
867 /* Compute the type of init->field */
869 if (DECL_MUTABLE_P (field
))
870 quals
&= ~TYPE_QUAL_CONST
;
871 expr_type
= cp_build_qualified_type (expr_type
, quals
);
873 init
= build3 (COMPONENT_REF
, expr_type
, init
, field
, NULL_TREE
);
874 if (move_p
&& TREE_CODE (expr_type
) != REFERENCE_TYPE
875 /* 'move' breaks bit-fields, and has no effect for scalars. */
876 && !scalarish_type_p (expr_type
))
879 if (DECL_NAME (field
))
880 init
= cp_build_modify_expr (input_location
, comp
, NOP_EXPR
, init
,
881 tf_warning_or_error
);
883 init
= build2 (MODIFY_EXPR
, TREE_TYPE (comp
), comp
, init
);
884 finish_expr_stmt (init
);
887 finish_return_stmt (current_class_ref
);
888 finish_compound_stmt (compound_stmt
);
891 /* Synthesize FNDECL, a non-static member function. */
894 synthesize_method (tree fndecl
)
896 bool nested
= (current_function_decl
!= NULL_TREE
);
897 tree context
= decl_function_context (fndecl
);
898 bool need_body
= true;
900 location_t save_input_location
= input_location
;
901 int error_count
= errorcount
;
902 int warning_count
= warningcount
+ werrorcount
;
904 /* Reset the source location, we might have been previously
905 deferred, and thus have saved where we were first needed. */
906 DECL_SOURCE_LOCATION (fndecl
)
907 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl
)));
909 /* If we've been asked to synthesize a clone, just synthesize the
910 cloned function instead. Doing so will automatically fill in the
911 body for the clone. */
912 if (DECL_CLONED_FUNCTION_P (fndecl
))
913 fndecl
= DECL_CLONED_FUNCTION (fndecl
);
915 /* We may be in the middle of deferred access check. Disable
917 push_deferring_access_checks (dk_no_deferred
);
920 push_to_top_level ();
922 push_function_context ();
924 input_location
= DECL_SOURCE_LOCATION (fndecl
);
926 start_preparsed_function (fndecl
, NULL_TREE
, SF_DEFAULT
| SF_PRE_PARSED
);
927 stmt
= begin_function_body ();
929 if (DECL_OVERLOADED_OPERATOR_P (fndecl
) == NOP_EXPR
)
931 do_build_copy_assign (fndecl
);
934 else if (DECL_CONSTRUCTOR_P (fndecl
))
936 tree arg_chain
= FUNCTION_FIRST_USER_PARMTYPE (fndecl
);
937 if (arg_chain
!= void_list_node
)
938 do_build_copy_constructor (fndecl
);
940 finish_mem_initializers (NULL_TREE
);
943 /* If we haven't yet generated the body of the function, just
944 generate an empty compound statement. */
948 compound_stmt
= begin_compound_stmt (BCS_FN_BODY
);
949 finish_compound_stmt (compound_stmt
);
952 finish_function_body (stmt
);
953 expand_or_defer_fn (finish_function (0));
955 input_location
= save_input_location
;
958 pop_from_top_level ();
960 pop_function_context ();
962 pop_deferring_access_checks ();
964 if (error_count
!= errorcount
|| warning_count
!= warningcount
+ werrorcount
)
965 inform (input_location
, "synthesized method %qD first required here ",
969 /* Build a reference to type TYPE with cv-quals QUALS, which is an
970 rvalue if RVALUE is true. */
973 build_stub_type (tree type
, int quals
, bool rvalue
)
975 tree argtype
= cp_build_qualified_type (type
, quals
);
976 return cp_build_reference_type (argtype
, rvalue
);
979 /* Build a dummy glvalue from dereferencing a dummy reference of type
983 build_stub_object (tree reftype
)
985 if (TREE_CODE (reftype
) != REFERENCE_TYPE
)
986 reftype
= cp_build_reference_type (reftype
, /*rval*/true);
987 tree stub
= build1 (CONVERT_EXPR
, reftype
, integer_one_node
);
988 return convert_from_reference (stub
);
991 /* Determine which function will be called when looking up NAME in TYPE,
992 called with a single ARGTYPE argument, or no argument if ARGTYPE is
993 null. FLAGS and COMPLAIN are as for build_new_method_call.
995 Returns a FUNCTION_DECL if all is well.
996 Returns NULL_TREE if overload resolution failed.
997 Returns error_mark_node if the chosen function cannot be called. */
1000 locate_fn_flags (tree type
, tree name
, tree argtype
, int flags
,
1001 tsubst_flags_t complain
)
1003 tree ob
, fn
, fns
, binfo
, rval
;
1004 vec
<tree
, va_gc
> *args
;
1007 binfo
= TYPE_BINFO (type
);
1011 type
= BINFO_TYPE (binfo
);
1014 ob
= build_stub_object (cp_build_reference_type (type
, false));
1015 args
= make_tree_vector ();
1018 if (TREE_CODE (argtype
) == TREE_LIST
)
1020 for (tree elt
= argtype
; elt
&& elt
!= void_list_node
;
1021 elt
= TREE_CHAIN (elt
))
1023 tree type
= TREE_VALUE (elt
);
1024 tree arg
= build_stub_object (type
);
1025 vec_safe_push (args
, arg
);
1030 tree arg
= build_stub_object (argtype
);
1031 args
->quick_push (arg
);
1035 fns
= lookup_fnfields (binfo
, name
, 0);
1036 rval
= build_new_method_call (ob
, fns
, &args
, binfo
, flags
, &fn
, complain
);
1038 release_tree_vector (args
);
1039 if (fn
&& rval
== error_mark_node
)
1045 /* Locate the dtor of TYPE. */
1048 get_dtor (tree type
, tsubst_flags_t complain
)
1050 tree fn
= locate_fn_flags (type
, complete_dtor_identifier
, NULL_TREE
,
1051 LOOKUP_NORMAL
, complain
);
1052 if (fn
== error_mark_node
)
1057 /* Locate the default ctor of TYPE. */
1060 locate_ctor (tree type
)
1064 push_deferring_access_checks (dk_no_check
);
1065 fn
= locate_fn_flags (type
, complete_ctor_identifier
, NULL_TREE
,
1066 LOOKUP_SPECULATIVE
, tf_none
);
1067 pop_deferring_access_checks ();
1068 if (fn
== error_mark_node
)
1073 /* Likewise, but give any appropriate errors. */
1076 get_default_ctor (tree type
)
1078 tree fn
= locate_fn_flags (type
, complete_ctor_identifier
, NULL_TREE
,
1079 LOOKUP_NORMAL
, tf_warning_or_error
);
1080 if (fn
== error_mark_node
)
1085 /* Locate the copy ctor of TYPE. */
1088 get_copy_ctor (tree type
, tsubst_flags_t complain
)
1090 int quals
= (TYPE_HAS_CONST_COPY_CTOR (type
)
1091 ? TYPE_QUAL_CONST
: TYPE_UNQUALIFIED
);
1092 tree argtype
= build_stub_type (type
, quals
, false);
1093 tree fn
= locate_fn_flags (type
, complete_ctor_identifier
, argtype
,
1094 LOOKUP_NORMAL
, complain
);
1095 if (fn
== error_mark_node
)
1100 /* Locate the copy assignment operator of TYPE. */
1103 get_copy_assign (tree type
)
1105 int quals
= (TYPE_HAS_CONST_COPY_ASSIGN (type
)
1106 ? TYPE_QUAL_CONST
: TYPE_UNQUALIFIED
);
1107 tree argtype
= build_stub_type (type
, quals
, false);
1108 tree fn
= locate_fn_flags (type
, ansi_assopname (NOP_EXPR
), argtype
,
1109 LOOKUP_NORMAL
, tf_warning_or_error
);
1110 if (fn
== error_mark_node
)
1115 /* walk_tree helper function for is_trivially_xible. If *TP is a call,
1116 return it if it calls something other than a trivial special member
1120 check_nontriv (tree
*tp
, int *, void *)
1122 tree fn
= cp_get_callee (*tp
);
1123 if (fn
== NULL_TREE
)
1126 if (TREE_CODE (fn
) == ADDR_EXPR
)
1127 fn
= TREE_OPERAND (fn
, 0);
1129 if (TREE_CODE (fn
) != FUNCTION_DECL
1130 || !trivial_fn_p (fn
))
1135 /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */
1138 assignable_expr (tree to
, tree from
)
1140 ++cp_unevaluated_operand
;
1141 to
= build_stub_object (to
);
1142 from
= build_stub_object (from
);
1143 tree r
= cp_build_modify_expr (input_location
, to
, NOP_EXPR
, from
, tf_none
);
1144 --cp_unevaluated_operand
;
1148 /* The predicate condition for a template specialization
1149 is_constructible<T, Args...> shall be satisfied if and only if the
1150 following variable definition would be well-formed for some invented
1151 variable t: T t(create<Args>()...);
1153 Return something equivalent in well-formedness and triviality. */
1156 constructible_expr (tree to
, tree from
)
1159 if (CLASS_TYPE_P (to
))
1162 vec
<tree
, va_gc
> *args
= NULL
;
1163 if (TREE_CODE (to
) != REFERENCE_TYPE
)
1164 to
= cp_build_reference_type (to
, /*rval*/false);
1165 tree ob
= build_stub_object (to
);
1166 for (; from
; from
= TREE_CHAIN (from
))
1167 vec_safe_push (args
, build_stub_object (TREE_VALUE (from
)));
1168 expr
= build_special_member_call (ob
, complete_ctor_identifier
, &args
,
1169 ctype
, LOOKUP_NORMAL
, tf_none
);
1170 if (expr
== error_mark_node
)
1171 return error_mark_node
;
1172 /* The current state of the standard vis-a-vis LWG 2116 is that
1173 is_*constructible involves destruction as well. */
1174 if (type_build_dtor_call (ctype
))
1176 tree dtor
= build_special_member_call (ob
, complete_dtor_identifier
,
1177 NULL
, ctype
, LOOKUP_NORMAL
,
1179 if (dtor
== error_mark_node
)
1180 return error_mark_node
;
1181 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype
))
1182 expr
= build2 (COMPOUND_EXPR
, void_type_node
, expr
, dtor
);
1187 if (from
== NULL_TREE
)
1188 return build_value_init (to
, tf_none
);
1189 else if (TREE_CHAIN (from
))
1190 return error_mark_node
; // too many initializers
1191 from
= build_stub_object (TREE_VALUE (from
));
1192 expr
= perform_direct_initialization_if_possible (to
, from
,
1199 /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or
1200 constructible (otherwise) from FROM, which is a single type for
1201 assignment or a list of types for construction. */
1204 is_trivially_xible (enum tree_code code
, tree to
, tree from
)
1207 if (code
== MODIFY_EXPR
)
1208 expr
= assignable_expr (to
, from
);
1209 else if (from
&& TREE_CHAIN (from
))
1210 return false; // only 0- and 1-argument ctors can be trivial
1212 expr
= constructible_expr (to
, from
);
1214 if (expr
== error_mark_node
)
1216 tree nt
= cp_walk_tree_without_duplicates (&expr
, check_nontriv
, NULL
);
1220 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
1221 DELETED_P or give an error message MSG with argument ARG. */
1224 process_subob_fn (tree fn
, tree
*spec_p
, bool *trivial_p
,
1225 bool *deleted_p
, bool *constexpr_p
,
1226 bool diag
, tree arg
, bool dtor_from_ctor
= false)
1228 if (!fn
|| fn
== error_mark_node
)
1237 maybe_instantiate_noexcept (fn
);
1238 tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
1239 *spec_p
= merge_exception_specifiers (*spec_p
, raises
);
1242 if (!trivial_fn_p (fn
) && !dtor_from_ctor
)
1246 if (TREE_CODE (arg
) == FIELD_DECL
1247 && TREE_CODE (DECL_CONTEXT (arg
)) == UNION_TYPE
)
1252 error ("union member %q+D with non-trivial %qD", arg
, fn
);
1256 if (constexpr_p
&& !DECL_DECLARED_CONSTEXPR_P (fn
))
1258 *constexpr_p
= false;
1261 inform (DECL_SOURCE_LOCATION (fn
),
1262 "defaulted constructor calls non-constexpr %qD", fn
);
1263 explain_invalid_constexpr_fn (fn
);
1268 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
1269 aggregates. If DTOR_FROM_CTOR is true, we're walking subobject destructors
1270 called from a synthesized constructor, in which case we don't consider
1271 the triviality of the subobject destructor. */
1274 walk_field_subobs (tree fields
, tree fnname
, special_function_kind sfk
,
1275 int quals
, bool copy_arg_p
, bool move_p
,
1276 bool assign_p
, tree
*spec_p
, bool *trivial_p
,
1277 bool *deleted_p
, bool *constexpr_p
,
1278 bool diag
, int flags
, tsubst_flags_t complain
,
1279 bool dtor_from_ctor
)
1282 for (field
= fields
; field
; field
= DECL_CHAIN (field
))
1284 tree mem_type
, argtype
, rval
;
1286 if (TREE_CODE (field
) != FIELD_DECL
1287 || DECL_ARTIFICIAL (field
))
1290 mem_type
= strip_array_types (TREE_TYPE (field
));
1294 if (CP_TYPE_CONST_P (mem_type
) && !CLASS_TYPE_P (mem_type
))
1297 error ("non-static const member %q#D, can%'t use default "
1298 "assignment operator", field
);
1300 else if (TREE_CODE (mem_type
) == REFERENCE_TYPE
)
1303 error ("non-static reference member %q#D, can%'t use "
1304 "default assignment operator", field
);
1309 if (bad
&& deleted_p
)
1312 else if (sfk
== sfk_constructor
)
1316 if (DECL_INITIAL (field
))
1318 if (diag
&& DECL_INITIAL (field
) == error_mark_node
)
1319 inform (DECL_SOURCE_LOCATION (field
),
1320 "initializer for %q#D is invalid", field
);
1323 /* Core 1351: If the field has an NSDMI that could throw, the
1324 default constructor is noexcept(false). */
1327 tree nsdmi
= get_nsdmi (field
, /*ctor*/false);
1328 if (!expr_noexcept_p (nsdmi
, complain
))
1329 *spec_p
= noexcept_false_spec
;
1331 /* Don't do the normal processing. */
1336 if (CP_TYPE_CONST_P (mem_type
)
1337 && default_init_uninitialized_part (mem_type
))
1341 error ("uninitialized const member in %q#T",
1342 current_class_type
);
1343 inform (DECL_SOURCE_LOCATION (field
),
1344 "%q#D should be initialized", field
);
1348 else if (TREE_CODE (mem_type
) == REFERENCE_TYPE
)
1352 error ("uninitialized reference member in %q#T",
1353 current_class_type
);
1354 inform (DECL_SOURCE_LOCATION (field
),
1355 "%q#D should be initialized", field
);
1360 if (bad
&& deleted_p
)
1363 /* For an implicitly-defined default constructor to be constexpr,
1364 every member must have a user-provided default constructor or
1365 an explicit initializer. */
1366 if (constexpr_p
&& !CLASS_TYPE_P (mem_type
)
1367 && TREE_CODE (DECL_CONTEXT (field
)) != UNION_TYPE
)
1369 *constexpr_p
= false;
1371 inform (DECL_SOURCE_LOCATION (field
),
1372 "defaulted default constructor does not "
1373 "initialize %q#D", field
);
1376 else if (sfk
== sfk_copy_constructor
)
1379 if (TREE_CODE (mem_type
) == REFERENCE_TYPE
1380 && TYPE_REF_IS_RVALUE (mem_type
))
1383 error ("copying non-static data member %q#D of rvalue "
1384 "reference type", field
);
1390 if (!CLASS_TYPE_P (mem_type
))
1393 if (ANON_AGGR_TYPE_P (mem_type
))
1395 walk_field_subobs (TYPE_FIELDS (mem_type
), fnname
, sfk
, quals
,
1396 copy_arg_p
, move_p
, assign_p
, spec_p
, trivial_p
,
1397 deleted_p
, constexpr_p
,
1398 diag
, flags
, complain
, dtor_from_ctor
);
1404 int mem_quals
= cp_type_quals (mem_type
) | quals
;
1405 if (DECL_MUTABLE_P (field
))
1406 mem_quals
&= ~TYPE_QUAL_CONST
;
1407 argtype
= build_stub_type (mem_type
, mem_quals
, move_p
);
1410 argtype
= NULL_TREE
;
1412 rval
= locate_fn_flags (mem_type
, fnname
, argtype
, flags
, complain
);
1414 process_subob_fn (rval
, spec_p
, trivial_p
, deleted_p
,
1415 constexpr_p
, diag
, field
, dtor_from_ctor
);
1419 /* The caller wants to generate an implicit declaration of SFK for
1420 CTYPE which is const if relevant and CONST_P is set. If SPEC_P,
1421 TRIVIAL_P, DELETED_P or CONSTEXPR_P are non-null, set their
1422 referent appropriately. If DIAG is true, we're either being called
1423 from maybe_explain_implicit_delete to give errors, or if
1424 CONSTEXPR_P is non-null, from explain_invalid_constexpr_fn. */
1427 synthesized_method_walk (tree ctype
, special_function_kind sfk
, bool const_p
,
1428 tree
*spec_p
, bool *trivial_p
, bool *deleted_p
,
1429 bool *constexpr_p
, bool diag
,
1430 tree inheriting_ctor
, tree inherited_parms
)
1432 tree binfo
, base_binfo
, scope
, fnname
, rval
, argtype
;
1433 bool move_p
, copy_arg_p
, assign_p
, expected_trivial
, check_vdtor
;
1434 vec
<tree
, va_gc
> *vbases
;
1435 int i
, quals
, flags
;
1436 tsubst_flags_t complain
;
1440 *spec_p
= (cxx_dialect
>= cxx11
? noexcept_true_spec
: empty_except_spec
);
1444 /* "The closure type associated with a lambda-expression has a deleted
1445 default constructor and a deleted copy assignment operator."
1446 This is diagnosed in maybe_explain_implicit_delete. */
1447 if (LAMBDA_TYPE_P (ctype
)
1448 && (sfk
== sfk_constructor
1449 || sfk
== sfk_copy_assignment
))
1460 check_vdtor
= false;
1463 case sfk_move_assignment
:
1464 case sfk_copy_assignment
:
1466 fnname
= ansi_assopname (NOP_EXPR
);
1469 case sfk_destructor
:
1471 /* The synthesized method will call base dtors, but check complete
1472 here to avoid having to deal with VTT. */
1473 fnname
= complete_dtor_identifier
;
1476 case sfk_constructor
:
1477 case sfk_move_constructor
:
1478 case sfk_copy_constructor
:
1479 case sfk_inheriting_constructor
:
1481 fnname
= complete_ctor_identifier
;
1488 gcc_assert ((sfk
== sfk_inheriting_constructor
)
1489 == (inheriting_ctor
!= NULL_TREE
));
1491 /* If that user-written default constructor would satisfy the
1492 requirements of a constexpr constructor (7.1.5), the
1493 implicitly-defined default constructor is constexpr.
1495 The implicitly-defined copy/move assignment operator is constexpr if
1496 - X is a literal type, and
1497 - the assignment operator selected to copy/move each direct base class
1498 subobject is a constexpr function, and
1499 - for each non-static data member of X that is of class type (or array
1500 thereof), the assignment operator selected to copy/move that member is a
1501 constexpr function. */
1503 *constexpr_p
= ctor_p
1504 || (assign_p
&& cxx_dialect
>= cxx14
);
1509 case sfk_constructor
:
1510 case sfk_destructor
:
1511 case sfk_inheriting_constructor
:
1515 case sfk_move_constructor
:
1516 case sfk_move_assignment
:
1519 case sfk_copy_constructor
:
1520 case sfk_copy_assignment
:
1528 expected_trivial
= type_has_trivial_fn (ctype
, sfk
);
1530 *trivial_p
= expected_trivial
;
1532 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
1533 class versions and other properties of the type. But a subobject
1534 class can be trivially copyable and yet have overload resolution
1535 choose a template constructor for initialization, depending on
1536 rvalueness and cv-quals. And furthermore, a member in a base might
1537 be trivial but deleted or otherwise not callable. So we can't exit
1538 early in C++0x. The same considerations apply in C++98/03, but
1539 there the definition of triviality does not consider overload
1540 resolution, so a constructor can be trivial even if it would otherwise
1541 call a non-trivial constructor. */
1542 if (expected_trivial
1543 && (!copy_arg_p
|| cxx_dialect
< cxx11
))
1545 if (constexpr_p
&& sfk
== sfk_constructor
)
1547 bool cx
= trivial_default_constructor_is_constexpr (ctype
);
1549 if (diag
&& !cx
&& TREE_CODE (ctype
) == UNION_TYPE
)
1550 /* A trivial constructor doesn't have any NSDMI. */
1551 inform (input_location
, "defaulted default constructor does "
1552 "not initialize any non-static data member");
1554 if (!diag
&& cxx_dialect
< cxx11
)
1558 ++cp_unevaluated_operand
;
1559 ++c_inhibit_evaluation_warnings
;
1560 push_deferring_access_checks (dk_no_deferred
);
1562 scope
= push_scope (ctype
);
1564 flags
= LOOKUP_NORMAL
|LOOKUP_SPECULATIVE
;
1565 if (!inheriting_ctor
)
1566 flags
|= LOOKUP_DEFAULTED
;
1568 complain
= diag
? tf_warning_or_error
: tf_none
;
1571 quals
= TYPE_QUAL_CONST
;
1573 quals
= TYPE_UNQUALIFIED
;
1574 argtype
= NULL_TREE
;
1576 for (binfo
= TYPE_BINFO (ctype
), i
= 0;
1577 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
1579 tree basetype
= BINFO_TYPE (base_binfo
);
1581 if (!assign_p
&& BINFO_VIRTUAL_P (base_binfo
))
1582 /* We'll handle virtual bases below. */
1585 bool inherited_binfo
= false;
1588 argtype
= build_stub_type (basetype
, quals
, move_p
);
1589 else if ((inherited_binfo
1590 = binfo_inherited_from (binfo
, base_binfo
, inheriting_ctor
)))
1592 /* Don't check access on the inherited constructor. */
1593 argtype
= inherited_parms
;
1594 if (flag_new_inheriting_ctors
)
1595 push_deferring_access_checks (dk_deferred
);
1597 rval
= locate_fn_flags (base_binfo
, fnname
, argtype
, flags
, complain
);
1598 if (inherited_binfo
)
1600 if (flag_new_inheriting_ctors
)
1601 pop_deferring_access_checks ();
1602 argtype
= NULL_TREE
;
1605 process_subob_fn (rval
, spec_p
, trivial_p
, deleted_p
,
1606 constexpr_p
, diag
, basetype
);
1609 /* In a constructor we also need to check the subobject
1610 destructors for cleanup of partially constructed objects. */
1611 rval
= locate_fn_flags (base_binfo
, complete_dtor_identifier
,
1612 NULL_TREE
, flags
, complain
);
1613 /* Note that we don't pass down trivial_p; the subobject
1614 destructors don't affect triviality of the constructor. Nor
1615 do they affect constexpr-ness (a constant expression doesn't
1616 throw) or exception-specification (a throw from one of the
1617 dtors would be a double-fault). */
1618 process_subob_fn (rval
, NULL
, NULL
,
1619 deleted_p
, NULL
, false,
1620 basetype
, /*dtor_from_ctor*/true);
1623 if (check_vdtor
&& type_has_virtual_destructor (basetype
))
1625 rval
= locate_fn_flags (ctype
, ansi_opname (DELETE_EXPR
),
1626 ptr_type_node
, flags
, complain
);
1627 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
1628 to have a null rval (no class-specific op delete). */
1629 if (rval
&& rval
== error_mark_node
&& deleted_p
)
1631 check_vdtor
= false;
1634 if (diag
&& assign_p
&& move_p
1635 && BINFO_VIRTUAL_P (base_binfo
)
1636 && rval
&& TREE_CODE (rval
) == FUNCTION_DECL
1637 && move_fn_p (rval
) && !trivial_fn_p (rval
)
1638 && vbase_has_user_provided_move_assign (basetype
))
1639 warning (OPT_Wvirtual_move_assign
,
1640 "defaulted move assignment for %qT calls a non-trivial "
1641 "move assignment operator for virtual base %qT",
1645 vbases
= CLASSTYPE_VBASECLASSES (ctype
);
1647 /* No need to examine vbases here. */;
1648 else if (vec_safe_is_empty (vbases
))
1649 /* No virtual bases to worry about. */;
1650 else if (ABSTRACT_CLASS_TYPE_P (ctype
) && cxx_dialect
>= cxx14
)
1651 /* Vbase cdtors are not relevant. */;
1655 *constexpr_p
= false;
1656 FOR_EACH_VEC_ELT (*vbases
, i
, base_binfo
)
1658 tree basetype
= BINFO_TYPE (base_binfo
);
1659 bool inherited_binfo
= false;
1662 argtype
= build_stub_type (basetype
, quals
, move_p
);
1663 else if ((inherited_binfo
1664 = binfo_inherited_from (binfo
, base_binfo
, inheriting_ctor
)))
1666 argtype
= inherited_parms
;
1667 if (flag_new_inheriting_ctors
)
1668 push_deferring_access_checks (dk_deferred
);
1670 rval
= locate_fn_flags (base_binfo
, fnname
, argtype
, flags
, complain
);
1671 if (inherited_binfo
)
1673 if (flag_new_inheriting_ctors
)
1674 pop_deferring_access_checks ();
1675 argtype
= NULL_TREE
;
1678 process_subob_fn (rval
, spec_p
, trivial_p
, deleted_p
,
1679 constexpr_p
, diag
, basetype
);
1680 if (ctor_p
&& TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype
))
1682 rval
= locate_fn_flags (base_binfo
, complete_dtor_identifier
,
1683 NULL_TREE
, flags
, complain
);
1684 process_subob_fn (rval
, NULL
, NULL
,
1685 deleted_p
, NULL
, false,
1686 basetype
, /*dtor_from_ctor*/true);
1691 /* Now handle the non-static data members. */
1692 walk_field_subobs (TYPE_FIELDS (ctype
), fnname
, sfk
, quals
,
1693 copy_arg_p
, move_p
, assign_p
, spec_p
, trivial_p
,
1694 deleted_p
, constexpr_p
,
1695 diag
, flags
, complain
, /*dtor_from_ctor*/false);
1697 walk_field_subobs (TYPE_FIELDS (ctype
), complete_dtor_identifier
,
1698 sfk_destructor
, TYPE_UNQUALIFIED
, false,
1699 false, false, NULL
, NULL
,
1701 false, flags
, complain
, /*dtor_from_ctor*/true);
1705 pop_deferring_access_checks ();
1706 --cp_unevaluated_operand
;
1707 --c_inhibit_evaluation_warnings
;
1710 /* DECL is a defaulted function whose exception specification is now
1711 needed. Return what it should be. */
1714 get_defaulted_eh_spec (tree decl
)
1716 if (DECL_CLONED_FUNCTION_P (decl
))
1717 decl
= DECL_CLONED_FUNCTION (decl
);
1718 special_function_kind sfk
= special_function_p (decl
);
1719 tree ctype
= DECL_CONTEXT (decl
);
1720 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
1721 tree parm_type
= TREE_VALUE (parms
);
1722 bool const_p
= CP_TYPE_CONST_P (non_reference (parm_type
));
1723 tree spec
= empty_except_spec
;
1724 synthesized_method_walk (ctype
, sfk
, const_p
, &spec
, NULL
, NULL
,
1725 NULL
, false, DECL_INHERITED_CTOR (decl
),
1730 /* DECL is a deleted function. If it's implicitly deleted, explain why and
1731 return true; else return false. */
1734 maybe_explain_implicit_delete (tree decl
)
1736 /* If decl is a clone, get the primary variant. */
1737 decl
= DECL_ORIGIN (decl
);
1738 gcc_assert (DECL_DELETED_FN (decl
));
1739 if (DECL_DEFAULTED_FN (decl
))
1741 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
1742 static hash_set
<tree
> *explained
;
1744 special_function_kind sfk
;
1750 explained
= new hash_set
<tree
>;
1751 if (explained
->add (decl
))
1754 sfk
= special_function_p (decl
);
1755 ctype
= DECL_CONTEXT (decl
);
1756 loc
= input_location
;
1757 input_location
= DECL_SOURCE_LOCATION (decl
);
1760 if (LAMBDA_TYPE_P (ctype
))
1763 if (sfk
== sfk_constructor
)
1764 inform (DECL_SOURCE_LOCATION (decl
),
1765 "a lambda closure type has a deleted default constructor");
1766 else if (sfk
== sfk_copy_assignment
)
1767 inform (DECL_SOURCE_LOCATION (decl
),
1768 "a lambda closure type has a deleted copy assignment operator");
1772 else if (DECL_ARTIFICIAL (decl
)
1773 && (sfk
== sfk_copy_assignment
1774 || sfk
== sfk_copy_constructor
)
1775 && (type_has_user_declared_move_constructor (ctype
)
1776 || type_has_user_declared_move_assign (ctype
)))
1778 inform (DECL_SOURCE_LOCATION (decl
),
1779 "%q#D is implicitly declared as deleted because %qT "
1780 "declares a move constructor or move assignment operator",
1784 else if (sfk
== sfk_inheriting_constructor
)
1786 tree binfo
= inherited_ctor_binfo (decl
);
1787 if (TREE_CODE (binfo
) != TREE_BINFO
)
1789 inform (DECL_SOURCE_LOCATION (decl
),
1790 "%q#D inherits from multiple base subobjects",
1797 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
1798 tree parm_type
= TREE_VALUE (parms
);
1799 bool const_p
= CP_TYPE_CONST_P (non_reference (parm_type
));
1800 tree raises
= NULL_TREE
;
1801 bool deleted_p
= false;
1802 tree scope
= push_scope (ctype
);
1804 synthesized_method_walk (ctype
, sfk
, const_p
,
1805 &raises
, NULL
, &deleted_p
, NULL
, false,
1806 DECL_INHERITED_CTOR (decl
), parms
);
1809 inform (DECL_SOURCE_LOCATION (decl
),
1810 "%q#D is implicitly deleted because the default "
1811 "definition would be ill-formed:", decl
);
1812 synthesized_method_walk (ctype
, sfk
, const_p
,
1813 NULL
, NULL
, NULL
, NULL
, true,
1814 DECL_INHERITED_CTOR (decl
), parms
);
1816 else if (!comp_except_specs
1817 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl
)),
1819 inform (DECL_SOURCE_LOCATION (decl
), "%q#F is implicitly "
1820 "deleted because its exception-specification does not "
1821 "match the implicit exception-specification %qX",
1823 else if (flag_checking
)
1829 input_location
= loc
;
1835 /* DECL is a defaulted function which was declared constexpr. Explain why
1836 it can't be constexpr. */
1839 explain_implicit_non_constexpr (tree decl
)
1841 tree parm_type
= TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl
));
1842 bool const_p
= CP_TYPE_CONST_P (non_reference (parm_type
));
1844 synthesized_method_walk (DECL_CLASS_CONTEXT (decl
),
1845 special_function_p (decl
), const_p
,
1846 NULL
, NULL
, NULL
, &dummy
, true,
1847 DECL_INHERITED_CTOR (decl
),
1848 FUNCTION_FIRST_USER_PARMTYPE (decl
));
1851 /* DECL is an instantiation of an inheriting constructor template. Deduce
1852 the correct exception-specification and deletedness for this particular
1856 deduce_inheriting_ctor (tree decl
)
1858 gcc_assert (DECL_INHERITED_CTOR (decl
));
1860 bool trivial
, constexpr_
, deleted
;
1861 synthesized_method_walk (DECL_CONTEXT (decl
), sfk_inheriting_constructor
,
1862 false, &spec
, &trivial
, &deleted
, &constexpr_
,
1864 DECL_INHERITED_CTOR (decl
),
1865 FUNCTION_FIRST_USER_PARMTYPE (decl
));
1866 if (TREE_CODE (inherited_ctor_binfo (decl
)) != TREE_BINFO
)
1867 /* Inherited the same constructor from different base subobjects. */
1869 DECL_DELETED_FN (decl
) = deleted
;
1870 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
), spec
);
1873 /* Implicitly declare the special function indicated by KIND, as a
1874 member of TYPE. For copy constructors and assignment operators,
1875 CONST_P indicates whether these functions should take a const
1876 reference argument or a non-const reference. Returns the
1877 FUNCTION_DECL for the implicitly declared function. */
1880 implicitly_declare_fn (special_function_kind kind
, tree type
,
1881 bool const_p
, tree inherited_ctor
,
1882 tree inherited_parms
)
1885 tree parameter_types
= void_list_node
;
1888 tree raises
= empty_except_spec
;
1889 tree rhs_parm_type
= NULL_TREE
;
1892 HOST_WIDE_INT saved_processing_template_decl
;
1896 /* Because we create declarations for implicitly declared functions
1897 lazily, we may be creating the declaration for a member of TYPE
1898 while in some completely different context. However, TYPE will
1899 never be a dependent class (because we never want to do lookups
1900 for implicitly defined functions in a dependent class).
1901 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
1902 because we only create clones for constructors and destructors
1903 when not in a template. */
1904 gcc_assert (!dependent_type_p (type
));
1905 saved_processing_template_decl
= processing_template_decl
;
1906 processing_template_decl
= 0;
1908 type
= TYPE_MAIN_VARIANT (type
);
1910 if (targetm
.cxx
.cdtor_returns_this ())
1912 if (kind
== sfk_destructor
)
1913 /* See comment in check_special_function_return_type. */
1914 return_type
= build_pointer_type (void_type_node
);
1916 return_type
= build_pointer_type (type
);
1919 return_type
= void_type_node
;
1923 case sfk_destructor
:
1925 name
= constructor_name (type
);
1928 case sfk_constructor
:
1929 /* Default constructor. */
1930 name
= constructor_name (type
);
1933 case sfk_copy_constructor
:
1934 case sfk_copy_assignment
:
1935 case sfk_move_constructor
:
1936 case sfk_move_assignment
:
1937 case sfk_inheriting_constructor
:
1940 if (kind
== sfk_copy_assignment
1941 || kind
== sfk_move_assignment
)
1943 return_type
= build_reference_type (type
);
1944 name
= ansi_assopname (NOP_EXPR
);
1947 name
= constructor_name (type
);
1949 if (kind
== sfk_inheriting_constructor
)
1950 parameter_types
= inherited_parms
;
1954 rhs_parm_type
= cp_build_qualified_type (type
, TYPE_QUAL_CONST
);
1956 rhs_parm_type
= type
;
1957 move_p
= (kind
== sfk_move_assignment
1958 || kind
== sfk_move_constructor
);
1959 rhs_parm_type
= cp_build_reference_type (rhs_parm_type
, move_p
);
1961 parameter_types
= tree_cons (NULL_TREE
, rhs_parm_type
, parameter_types
);
1969 bool trivial_p
= false;
1971 if (inherited_ctor
&& TREE_CODE (inherited_ctor
) == TEMPLATE_DECL
)
1973 /* For an inheriting constructor template, just copy these flags from
1974 the inherited constructor template for now. */
1975 raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor
));
1976 deleted_p
= DECL_DELETED_FN (inherited_ctor
);
1977 constexpr_p
= DECL_DECLARED_CONSTEXPR_P (inherited_ctor
);
1979 else if (cxx_dialect
>= cxx11
)
1981 raises
= unevaluated_noexcept_spec ();
1982 synthesized_method_walk (type
, kind
, const_p
, NULL
, &trivial_p
,
1983 &deleted_p
, &constexpr_p
, false,
1984 inherited_ctor
, inherited_parms
);
1987 synthesized_method_walk (type
, kind
, const_p
, &raises
, &trivial_p
,
1988 &deleted_p
, &constexpr_p
, false,
1989 inherited_ctor
, inherited_parms
);
1990 /* Don't bother marking a deleted constructor as constexpr. */
1992 constexpr_p
= false;
1993 /* A trivial copy/move constructor is also a constexpr constructor,
1994 unless the class has virtual bases (7.1.5p4). */
1995 else if (trivial_p
&& cxx_dialect
>= cxx11
1996 && (kind
== sfk_copy_constructor
1997 || kind
== sfk_move_constructor
)
1998 && !CLASSTYPE_VBASECLASSES (type
))
1999 gcc_assert (constexpr_p
);
2001 if (!trivial_p
&& type_has_trivial_fn (type
, kind
))
2002 type_set_nontrivial_flag (type
, kind
);
2004 /* Create the function. */
2005 fn_type
= build_method_type_directly (type
, return_type
, parameter_types
);
2007 fn_type
= build_exception_variant (fn_type
, raises
);
2008 fn
= build_lang_decl (FUNCTION_DECL
, name
, fn_type
);
2009 if (kind
!= sfk_inheriting_constructor
)
2010 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (TYPE_NAME (type
));
2011 if (kind
== sfk_constructor
|| kind
== sfk_copy_constructor
2012 || kind
== sfk_move_constructor
|| kind
== sfk_inheriting_constructor
)
2013 DECL_CONSTRUCTOR_P (fn
) = 1;
2014 else if (kind
== sfk_destructor
)
2015 DECL_DESTRUCTOR_P (fn
) = 1;
2018 DECL_ASSIGNMENT_OPERATOR_P (fn
) = 1;
2019 SET_OVERLOADED_OPERATOR_CODE (fn
, NOP_EXPR
);
2022 SET_DECL_ALIGN (fn
, MINIMUM_METHOD_BOUNDARY
);
2024 /* Create the explicit arguments. */
2027 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
2028 want its type to be included in the mangled function
2030 tree decl
= cp_build_parm_decl (NULL_TREE
, rhs_parm_type
);
2031 TREE_READONLY (decl
) = 1;
2032 retrofit_lang_decl (decl
);
2033 DECL_PARM_INDEX (decl
) = DECL_PARM_LEVEL (decl
) = 1;
2034 DECL_ARGUMENTS (fn
) = decl
;
2036 else if (kind
== sfk_inheriting_constructor
)
2038 tree
*p
= &DECL_ARGUMENTS (fn
);
2040 for (tree parm
= inherited_parms
; parm
&& parm
!= void_list_node
;
2041 parm
= TREE_CHAIN (parm
))
2043 *p
= cp_build_parm_decl (NULL_TREE
, TREE_VALUE (parm
));
2044 retrofit_lang_decl (*p
);
2045 DECL_PARM_LEVEL (*p
) = 1;
2046 DECL_PARM_INDEX (*p
) = index
++;
2047 DECL_CONTEXT (*p
) = fn
;
2048 p
= &DECL_CHAIN (*p
);
2050 SET_DECL_INHERITED_CTOR (fn
, inherited_ctor
);
2051 DECL_NONCONVERTING_P (fn
) = DECL_NONCONVERTING_P (inherited_ctor
);
2052 /* A constructor so declared has the same access as the corresponding
2053 constructor in X. */
2054 TREE_PRIVATE (fn
) = TREE_PRIVATE (inherited_ctor
);
2055 TREE_PROTECTED (fn
) = TREE_PROTECTED (inherited_ctor
);
2056 /* Copy constexpr from the inherited constructor even if the
2057 inheriting constructor doesn't satisfy the requirements. */
2058 constexpr_p
= DECL_DECLARED_CONSTEXPR_P (inherited_ctor
);
2060 /* Add the "this" parameter. */
2061 this_parm
= build_this_parm (fn_type
, TYPE_UNQUALIFIED
);
2062 DECL_CHAIN (this_parm
) = DECL_ARGUMENTS (fn
);
2063 DECL_ARGUMENTS (fn
) = this_parm
;
2065 grokclassfn (type
, fn
, kind
== sfk_destructor
? DTOR_FLAG
: NO_SPECIAL
);
2066 DECL_IN_AGGR_P (fn
) = 1;
2067 DECL_ARTIFICIAL (fn
) = 1;
2068 DECL_DEFAULTED_FN (fn
) = 1;
2069 if (cxx_dialect
>= cxx11
)
2071 DECL_DELETED_FN (fn
) = deleted_p
;
2072 DECL_DECLARED_CONSTEXPR_P (fn
) = constexpr_p
;
2074 DECL_EXTERNAL (fn
) = true;
2075 DECL_NOT_REALLY_EXTERN (fn
) = 1;
2076 DECL_DECLARED_INLINE_P (fn
) = 1;
2077 set_linkage_according_to_type (type
, fn
);
2078 if (TREE_PUBLIC (fn
))
2079 DECL_COMDAT (fn
) = 1;
2080 rest_of_decl_compilation (fn
, toplevel_bindings_p (), at_eof
);
2081 gcc_assert (!TREE_USED (fn
));
2083 /* Propagate constraints from the inherited constructor. */
2084 if (flag_concepts
&& inherited_ctor
)
2085 if (tree orig_ci
= get_constraints (inherited_ctor
))
2087 tree new_ci
= copy_node (orig_ci
);
2088 set_constraints (fn
, new_ci
);
2091 /* Restore PROCESSING_TEMPLATE_DECL. */
2092 processing_template_decl
= saved_processing_template_decl
;
2094 if (inherited_ctor
&& TREE_CODE (inherited_ctor
) == TEMPLATE_DECL
)
2095 fn
= add_inherited_template_parms (fn
, inherited_ctor
);
2097 /* Warn about calling a non-trivial move assignment in a virtual base. */
2098 if (kind
== sfk_move_assignment
&& !deleted_p
&& !trivial_p
2099 && CLASSTYPE_VBASECLASSES (type
))
2101 location_t loc
= input_location
;
2102 input_location
= DECL_SOURCE_LOCATION (fn
);
2103 synthesized_method_walk (type
, kind
, const_p
,
2104 NULL
, NULL
, NULL
, NULL
, true,
2105 NULL_TREE
, NULL_TREE
);
2106 input_location
= loc
;
2112 /* Gives any errors about defaulted functions which need to be deferred
2113 until the containing class is complete. */
2116 defaulted_late_check (tree fn
)
2118 /* Complain about invalid signature for defaulted fn. */
2119 tree ctx
= DECL_CONTEXT (fn
);
2120 special_function_kind kind
= special_function_p (fn
);
2121 bool fn_const_p
= (copy_fn_p (fn
) == 2);
2122 tree implicit_fn
= implicitly_declare_fn (kind
, ctx
, fn_const_p
,
2124 tree eh_spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn
));
2126 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn
)),
2127 TREE_TYPE (TREE_TYPE (implicit_fn
)))
2128 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2129 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn
))))
2131 error ("defaulted declaration %q+D", fn
);
2132 error_at (DECL_SOURCE_LOCATION (fn
),
2133 "does not match expected signature %qD", implicit_fn
);
2136 /* 8.4.2/2: An explicitly-defaulted function (...) may have an explicit
2137 exception-specification only if it is compatible (15.4) with the
2138 exception-specification on the implicit declaration. If a function
2139 is explicitly defaulted on its first declaration, (...) it is
2140 implicitly considered to have the same exception-specification as if
2141 it had been implicitly declared. */
2142 maybe_instantiate_noexcept (fn
);
2143 tree fn_spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
2146 if (DECL_DEFAULTED_IN_CLASS_P (fn
))
2147 TREE_TYPE (fn
) = build_exception_variant (TREE_TYPE (fn
), eh_spec
);
2149 else if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec
))
2150 /* Equivalent to the implicit spec. */;
2151 else if (DECL_DEFAULTED_IN_CLASS_P (fn
)
2152 && !CLASSTYPE_TEMPLATE_INSTANTIATION (ctx
))
2153 /* We can't compare an explicit exception-specification on a
2154 constructor defaulted in the class body to the implicit
2155 exception-specification until after we've parsed any NSDMI; see
2156 after_nsdmi_defaulted_late_checks. */;
2159 tree eh_spec
= get_defaulted_eh_spec (fn
);
2160 if (!comp_except_specs (fn_spec
, eh_spec
, ce_normal
))
2162 if (DECL_DEFAULTED_IN_CLASS_P (fn
))
2163 DECL_DELETED_FN (fn
) = true;
2165 error ("function %q+D defaulted on its redeclaration "
2166 "with an exception-specification that differs from "
2167 "the implicit exception-specification %qX", fn
, eh_spec
);
2171 if (DECL_DEFAULTED_IN_CLASS_P (fn
)
2172 && DECL_DECLARED_CONSTEXPR_P (implicit_fn
))
2174 /* Hmm...should we do this for out-of-class too? Should it be OK to
2175 add constexpr later like inline, rather than requiring
2176 declarations to match? */
2177 DECL_DECLARED_CONSTEXPR_P (fn
) = true;
2178 if (kind
== sfk_constructor
)
2179 TYPE_HAS_CONSTEXPR_CTOR (ctx
) = true;
2182 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn
)
2183 && DECL_DECLARED_CONSTEXPR_P (fn
))
2185 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx
))
2187 error ("explicitly defaulted function %q+D cannot be declared "
2188 "as constexpr because the implicit declaration is not "
2190 explain_implicit_non_constexpr (fn
);
2192 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
2195 if (DECL_DELETED_FN (implicit_fn
))
2196 DECL_DELETED_FN (fn
) = 1;
2199 /* OK, we've parsed the NSDMI for class T, now we can check any explicit
2200 exception-specifications on functions defaulted in the class body. */
2203 after_nsdmi_defaulted_late_checks (tree t
)
2205 if (uses_template_parms (t
))
2207 if (t
== error_mark_node
)
2209 for (tree fn
= TYPE_METHODS (t
); fn
; fn
= DECL_CHAIN (fn
))
2210 if (!DECL_ARTIFICIAL (fn
) && DECL_DEFAULTED_IN_CLASS_P (fn
))
2212 tree fn_spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
2213 if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec
))
2216 tree eh_spec
= get_defaulted_eh_spec (fn
);
2217 if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
)),
2218 eh_spec
, ce_normal
))
2219 DECL_DELETED_FN (fn
) = true;
2223 /* Returns true iff FN can be explicitly defaulted, and gives any
2224 errors if defaulting FN is ill-formed. */
2227 defaultable_fn_check (tree fn
)
2229 special_function_kind kind
= sfk_none
;
2231 if (template_parm_scope_p ())
2233 error ("a template cannot be defaulted");
2237 if (DECL_CONSTRUCTOR_P (fn
))
2239 if (FUNCTION_FIRST_USER_PARMTYPE (fn
) == void_list_node
)
2240 kind
= sfk_constructor
;
2241 else if (copy_fn_p (fn
) > 0
2242 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn
))
2244 kind
= sfk_copy_constructor
;
2245 else if (move_fn_p (fn
))
2246 kind
= sfk_move_constructor
;
2248 else if (DECL_DESTRUCTOR_P (fn
))
2249 kind
= sfk_destructor
;
2250 else if (DECL_ASSIGNMENT_OPERATOR_P (fn
)
2251 && DECL_OVERLOADED_OPERATOR_P (fn
) == NOP_EXPR
)
2254 kind
= sfk_copy_assignment
;
2255 else if (move_fn_p (fn
))
2256 kind
= sfk_move_assignment
;
2259 if (kind
== sfk_none
)
2261 error ("%qD cannot be defaulted", fn
);
2266 for (tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
2267 t
&& t
!= void_list_node
; t
= TREE_CHAIN (t
))
2268 if (TREE_PURPOSE (t
))
2270 error ("defaulted function %q+D with default argument", fn
);
2274 /* Avoid do_warn_unused_parameter warnings. */
2275 for (tree p
= FUNCTION_FIRST_USER_PARM (fn
); p
; p
= DECL_CHAIN (p
))
2277 TREE_NO_WARNING (p
) = 1;
2279 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn
)))
2280 /* Defer checking. */;
2281 else if (!processing_template_decl
)
2282 defaulted_late_check (fn
);
2288 /* Add an implicit declaration to TYPE for the kind of function
2289 indicated by SFK. Return the FUNCTION_DECL for the new implicit
2293 lazily_declare_fn (special_function_kind sfk
, tree type
)
2296 /* Whether or not the argument has a const reference type. */
2297 bool const_p
= false;
2299 type
= TYPE_MAIN_VARIANT (type
);
2303 case sfk_constructor
:
2304 CLASSTYPE_LAZY_DEFAULT_CTOR (type
) = 0;
2306 case sfk_copy_constructor
:
2307 const_p
= TYPE_HAS_CONST_COPY_CTOR (type
);
2308 CLASSTYPE_LAZY_COPY_CTOR (type
) = 0;
2310 case sfk_move_constructor
:
2311 CLASSTYPE_LAZY_MOVE_CTOR (type
) = 0;
2313 case sfk_copy_assignment
:
2314 const_p
= TYPE_HAS_CONST_COPY_ASSIGN (type
);
2315 CLASSTYPE_LAZY_COPY_ASSIGN (type
) = 0;
2317 case sfk_move_assignment
:
2318 CLASSTYPE_LAZY_MOVE_ASSIGN (type
) = 0;
2320 case sfk_destructor
:
2321 CLASSTYPE_LAZY_DESTRUCTOR (type
) = 0;
2327 /* Declare the function. */
2328 fn
= implicitly_declare_fn (sfk
, type
, const_p
, NULL
, NULL
);
2330 /* [class.copy]/8 If the class definition declares a move constructor or
2331 move assignment operator, the implicitly declared copy constructor is
2332 defined as deleted.... */
2333 if ((sfk
== sfk_copy_assignment
2334 || sfk
== sfk_copy_constructor
)
2335 && (type_has_user_declared_move_constructor (type
)
2336 || type_has_user_declared_move_assign (type
)))
2337 DECL_DELETED_FN (fn
) = true;
2339 /* A destructor may be virtual. */
2340 if (sfk
== sfk_destructor
2341 || sfk
== sfk_move_assignment
2342 || sfk
== sfk_copy_assignment
)
2343 check_for_override (fn
, type
);
2344 /* Add it to CLASSTYPE_METHOD_VEC. */
2345 add_method (type
, fn
, NULL_TREE
);
2346 /* Add it to TYPE_METHODS. */
2347 if (sfk
== sfk_destructor
2348 && DECL_VIRTUAL_P (fn
))
2349 /* The ABI requires that a virtual destructor go at the end of the
2351 TYPE_METHODS (type
) = chainon (TYPE_METHODS (type
), fn
);
2354 DECL_CHAIN (fn
) = TYPE_METHODS (type
);
2355 TYPE_METHODS (type
) = fn
;
2357 maybe_add_class_template_decl_list (type
, fn
, /*friend_p=*/0);
2358 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn
)
2359 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn
))
2360 /* Create appropriate clones. */
2361 clone_function_decl (fn
, /*update_method_vec=*/true);
2366 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
2367 as there are artificial parms in FN. */
2370 skip_artificial_parms_for (const_tree fn
, tree list
)
2372 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
2373 list
= TREE_CHAIN (list
);
2377 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
2378 list
= TREE_CHAIN (list
);
2379 if (DECL_HAS_VTT_PARM_P (fn
))
2380 list
= TREE_CHAIN (list
);
2384 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
2385 artificial parms in FN. */
2388 num_artificial_parms_for (const_tree fn
)
2392 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
2397 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
2399 if (DECL_HAS_VTT_PARM_P (fn
))
2405 #include "gt-cp-method.h"