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_CXX_DESTRUCTOR_P (thunk
) = 0;
141 DECL_CXX_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_CXX_DESTRUCTOR_P (alias
) = 0;
227 DECL_CXX_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 warning_sentinel
w (warn_useless_cast
);
490 exp
= build_static_cast (type
, exp
, tf_warning_or_error
);
491 if (DECL_PACK_P (parm
))
492 exp
= make_pack_expansion (exp
);
496 /* Strip all inheriting constructors, if any, to return the original
497 constructor from a (possibly indirect) base class. */
500 strip_inheriting_ctors (tree dfn
)
502 if (!flag_new_inheriting_ctors
)
505 while (tree inh
= DECL_INHERITED_CTOR (fn
))
506 fn
= OVL_FIRST (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 (ovl_iterator
iter (inh
); iter
; ++iter
)
544 tree one
= inherited_ctor_binfo_1 (binfo
, *iter
);
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 if (FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn
)) == void_list_node
)
579 /* No user-declared parameters to omit. */
581 tree binfo
= inherited_ctor_binfo (fn
);
582 for (; binfo
; binfo
= BINFO_INHERITANCE_CHAIN (binfo
))
583 if (BINFO_VIRTUAL_P (binfo
))
588 /* True iff constructor(s) INH inherited into BINFO initializes INIT_BINFO.
589 This can be true for multiple virtual bases as well as one direct
593 binfo_inherited_from (tree binfo
, tree init_binfo
, tree inh
)
595 /* inh is an OVERLOAD if we inherited the same constructor along
596 multiple paths, check all of them. */
597 for (ovl_iterator
iter (inh
); iter
; ++iter
)
600 tree base
= DECL_CONTEXT (fn
);
601 tree base_binfo
= NULL_TREE
;
602 for (int i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
603 if (BINFO_TYPE (base_binfo
) == base
)
605 if (base_binfo
== init_binfo
606 || (flag_new_inheriting_ctors
607 && binfo_inherited_from (base_binfo
, init_binfo
,
608 DECL_INHERITED_CTOR (fn
))))
614 /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO
615 given the parameter or parameters PARM, possibly inherited constructor
616 base INH, or move flag MOVE_P. */
619 add_one_base_init (tree binfo
, tree parm
, bool move_p
, tree inh
,
620 tree member_init_list
)
625 /* An inheriting constructor only has a mem-initializer for
626 the base it inherits from. */
627 if (!binfo_inherited_from (TYPE_BINFO (current_class_type
), binfo
, inh
))
628 return member_init_list
;
632 for (; parm
; parm
= DECL_CHAIN (parm
))
634 tree exp
= forward_parm (parm
);
635 *p
= build_tree_list (NULL_TREE
, exp
);
636 p
= &TREE_CHAIN (*p
);
641 init
= build_base_path (PLUS_EXPR
, parm
, binfo
, 1,
642 tf_warning_or_error
);
645 init
= build_tree_list (NULL_TREE
, init
);
647 return tree_cons (binfo
, init
, member_init_list
);
650 /* Generate code for default X(X&) or X(X&&) constructor or an inheriting
654 do_build_copy_constructor (tree fndecl
)
656 tree parm
= FUNCTION_FIRST_USER_PARM (fndecl
);
657 bool move_p
= DECL_MOVE_CONSTRUCTOR_P (fndecl
);
658 bool trivial
= trivial_fn_p (fndecl
);
659 tree inh
= DECL_INHERITED_CTOR (fndecl
);
662 parm
= convert_from_reference (parm
);
666 if (is_empty_class (current_class_type
))
667 /* Don't copy the padding byte; it might not have been allocated
668 if *this is a base subobject. */;
669 else if (tree_int_cst_equal (TYPE_SIZE (current_class_type
),
670 CLASSTYPE_SIZE (current_class_type
)))
672 tree t
= build2 (INIT_EXPR
, void_type_node
, current_class_ref
, parm
);
673 finish_expr_stmt (t
);
677 /* We must only copy the non-tail padding parts. */
678 tree base_size
= CLASSTYPE_SIZE_UNIT (current_class_type
);
679 base_size
= size_binop (MINUS_EXPR
, base_size
, size_int (1));
680 tree array_type
= build_array_type (unsigned_char_type_node
,
681 build_index_type (base_size
));
682 tree alias_set
= build_int_cst (TREE_TYPE (current_class_ptr
), 0);
683 tree lhs
= build2 (MEM_REF
, array_type
,
684 current_class_ptr
, alias_set
);
685 tree rhs
= build2 (MEM_REF
, array_type
,
686 TREE_OPERAND (parm
, 0), alias_set
);
687 tree t
= build2 (INIT_EXPR
, void_type_node
, lhs
, rhs
);
688 finish_expr_stmt (t
);
693 tree fields
= TYPE_FIELDS (current_class_type
);
694 tree member_init_list
= NULL_TREE
;
695 int cvquals
= cp_type_quals (TREE_TYPE (parm
));
697 tree binfo
, base_binfo
;
699 vec
<tree
, va_gc
> *vbases
;
701 /* Initialize all the base-classes with the parameter converted
702 to their type so that we get their copy constructor and not
703 another constructor that takes current_class_type. We must
704 deal with the binfo's directly as a direct base might be
705 inaccessible due to ambiguity. */
706 for (vbases
= CLASSTYPE_VBASECLASSES (current_class_type
), i
= 0;
707 vec_safe_iterate (vbases
, i
, &binfo
); i
++)
709 member_init_list
= add_one_base_init (binfo
, parm
, move_p
, inh
,
713 for (binfo
= TYPE_BINFO (current_class_type
), i
= 0;
714 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
716 if (BINFO_VIRTUAL_P (base_binfo
))
718 member_init_list
= add_one_base_init (base_binfo
, parm
, move_p
,
719 inh
, member_init_list
);
722 for (; fields
; fields
= DECL_CHAIN (fields
))
727 if (TREE_CODE (field
) != FIELD_DECL
)
732 expr_type
= TREE_TYPE (field
);
733 if (DECL_NAME (field
))
735 if (VFIELD_NAME_P (DECL_NAME (field
)))
738 else if (ANON_AGGR_TYPE_P (expr_type
) && TYPE_FIELDS (expr_type
))
739 /* Just use the field; anonymous types can't have
740 nontrivial copy ctors or assignment ops or this
741 function would be deleted. */;
745 /* Compute the type of "init->field". If the copy-constructor
746 parameter is, for example, "const S&", and the type of
747 the field is "T", then the type will usually be "const
748 T". (There are no cv-qualified variants of reference
750 if (TREE_CODE (expr_type
) != REFERENCE_TYPE
)
754 if (DECL_MUTABLE_P (field
))
755 quals
&= ~TYPE_QUAL_CONST
;
756 quals
|= cp_type_quals (expr_type
);
757 expr_type
= cp_build_qualified_type (expr_type
, quals
);
760 init
= build3 (COMPONENT_REF
, expr_type
, parm
, field
, NULL_TREE
);
761 if (move_p
&& TREE_CODE (expr_type
) != REFERENCE_TYPE
762 /* 'move' breaks bit-fields, and has no effect for scalars. */
763 && !scalarish_type_p (expr_type
))
765 init
= build_tree_list (NULL_TREE
, init
);
767 member_init_list
= tree_cons (field
, init
, member_init_list
);
769 finish_mem_initializers (member_init_list
);
774 do_build_copy_assign (tree fndecl
)
776 tree parm
= DECL_CHAIN (DECL_ARGUMENTS (fndecl
));
778 bool move_p
= move_fn_p (fndecl
);
779 bool trivial
= trivial_fn_p (fndecl
);
780 int flags
= LOOKUP_NORMAL
| LOOKUP_NONVIRTUAL
| LOOKUP_DEFAULTED
;
782 compound_stmt
= begin_compound_stmt (0);
783 parm
= convert_from_reference (parm
);
786 && is_empty_class (current_class_type
))
787 /* Don't copy the padding byte; it might not have been allocated
788 if *this is a base subobject. */;
791 tree t
= build2 (MODIFY_EXPR
, void_type_node
, current_class_ref
, parm
);
792 finish_expr_stmt (t
);
797 int cvquals
= cp_type_quals (TREE_TYPE (parm
));
799 tree binfo
, base_binfo
;
801 /* Assign to each of the direct base classes. */
802 for (binfo
= TYPE_BINFO (current_class_type
), i
= 0;
803 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
806 vec
<tree
, va_gc
> *parmvec
;
808 /* We must convert PARM directly to the base class
809 explicitly since the base class may be ambiguous. */
810 converted_parm
= build_base_path (PLUS_EXPR
, parm
, base_binfo
, 1,
811 tf_warning_or_error
);
813 converted_parm
= move (converted_parm
);
814 /* Call the base class assignment operator. */
815 parmvec
= make_tree_vector_single (converted_parm
);
817 (build_special_member_call (current_class_ref
,
818 cp_assignment_operator_id (NOP_EXPR
),
822 tf_warning_or_error
));
823 release_tree_vector (parmvec
);
826 /* Assign to each of the non-static data members. */
827 for (fields
= TYPE_FIELDS (current_class_type
);
829 fields
= DECL_CHAIN (fields
))
831 tree comp
= current_class_ref
;
837 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
840 expr_type
= TREE_TYPE (field
);
842 if (CP_TYPE_CONST_P (expr_type
))
844 error ("non-static const member %q#D, can%'t use default "
845 "assignment operator", field
);
848 else if (TREE_CODE (expr_type
) == REFERENCE_TYPE
)
850 error ("non-static reference member %q#D, can%'t use "
851 "default assignment operator", field
);
855 if (DECL_NAME (field
))
857 if (VFIELD_NAME_P (DECL_NAME (field
)))
860 else if (ANON_AGGR_TYPE_P (expr_type
)
861 && TYPE_FIELDS (expr_type
) != NULL_TREE
)
862 /* Just use the field; anonymous types can't have
863 nontrivial copy ctors or assignment ops or this
864 function would be deleted. */;
868 comp
= build3 (COMPONENT_REF
, expr_type
, comp
, field
, NULL_TREE
);
870 /* Compute the type of init->field */
872 if (DECL_MUTABLE_P (field
))
873 quals
&= ~TYPE_QUAL_CONST
;
874 expr_type
= cp_build_qualified_type (expr_type
, quals
);
876 init
= build3 (COMPONENT_REF
, expr_type
, init
, field
, NULL_TREE
);
877 if (move_p
&& TREE_CODE (expr_type
) != REFERENCE_TYPE
878 /* 'move' breaks bit-fields, and has no effect for scalars. */
879 && !scalarish_type_p (expr_type
))
882 if (DECL_NAME (field
))
883 init
= cp_build_modify_expr (input_location
, comp
, NOP_EXPR
, init
,
884 tf_warning_or_error
);
886 init
= build2 (MODIFY_EXPR
, TREE_TYPE (comp
), comp
, init
);
887 finish_expr_stmt (init
);
890 finish_return_stmt (current_class_ref
);
891 finish_compound_stmt (compound_stmt
);
894 /* Synthesize FNDECL, a non-static member function. */
897 synthesize_method (tree fndecl
)
899 bool nested
= (current_function_decl
!= NULL_TREE
);
900 tree context
= decl_function_context (fndecl
);
901 bool need_body
= true;
903 location_t save_input_location
= input_location
;
904 int error_count
= errorcount
;
905 int warning_count
= warningcount
+ werrorcount
;
907 /* Reset the source location, we might have been previously
908 deferred, and thus have saved where we were first needed. */
909 DECL_SOURCE_LOCATION (fndecl
)
910 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl
)));
912 /* If we've been asked to synthesize a clone, just synthesize the
913 cloned function instead. Doing so will automatically fill in the
914 body for the clone. */
915 if (DECL_CLONED_FUNCTION_P (fndecl
))
916 fndecl
= DECL_CLONED_FUNCTION (fndecl
);
918 /* We may be in the middle of deferred access check. Disable
920 push_deferring_access_checks (dk_no_deferred
);
923 push_to_top_level ();
925 push_function_context ();
927 input_location
= DECL_SOURCE_LOCATION (fndecl
);
929 start_preparsed_function (fndecl
, NULL_TREE
, SF_DEFAULT
| SF_PRE_PARSED
);
930 stmt
= begin_function_body ();
932 if (DECL_OVERLOADED_OPERATOR_P (fndecl
) == NOP_EXPR
)
934 do_build_copy_assign (fndecl
);
937 else if (DECL_CONSTRUCTOR_P (fndecl
))
939 tree arg_chain
= FUNCTION_FIRST_USER_PARMTYPE (fndecl
);
940 if (arg_chain
!= void_list_node
)
941 do_build_copy_constructor (fndecl
);
943 finish_mem_initializers (NULL_TREE
);
946 /* If we haven't yet generated the body of the function, just
947 generate an empty compound statement. */
951 compound_stmt
= begin_compound_stmt (BCS_FN_BODY
);
952 finish_compound_stmt (compound_stmt
);
955 finish_function_body (stmt
);
956 expand_or_defer_fn (finish_function (0));
958 input_location
= save_input_location
;
961 pop_from_top_level ();
963 pop_function_context ();
965 pop_deferring_access_checks ();
967 if (error_count
!= errorcount
|| warning_count
!= warningcount
+ werrorcount
)
968 inform (input_location
, "synthesized method %qD first required here ",
972 /* Build a reference to type TYPE with cv-quals QUALS, which is an
973 rvalue if RVALUE is true. */
976 build_stub_type (tree type
, int quals
, bool rvalue
)
978 tree argtype
= cp_build_qualified_type (type
, quals
);
979 return cp_build_reference_type (argtype
, rvalue
);
982 /* Build a dummy glvalue from dereferencing a dummy reference of type
986 build_stub_object (tree reftype
)
988 if (TREE_CODE (reftype
) != REFERENCE_TYPE
)
989 reftype
= cp_build_reference_type (reftype
, /*rval*/true);
990 tree stub
= build1 (CONVERT_EXPR
, reftype
, integer_one_node
);
991 return convert_from_reference (stub
);
994 /* Determine which function will be called when looking up NAME in TYPE,
995 called with a single ARGTYPE argument, or no argument if ARGTYPE is
996 null. FLAGS and COMPLAIN are as for build_new_method_call.
998 Returns a FUNCTION_DECL if all is well.
999 Returns NULL_TREE if overload resolution failed.
1000 Returns error_mark_node if the chosen function cannot be called. */
1003 locate_fn_flags (tree type
, tree name
, tree argtype
, int flags
,
1004 tsubst_flags_t complain
)
1006 tree ob
, fn
, fns
, binfo
, rval
;
1007 vec
<tree
, va_gc
> *args
;
1010 binfo
= TYPE_BINFO (type
);
1014 type
= BINFO_TYPE (binfo
);
1017 ob
= build_stub_object (cp_build_reference_type (type
, false));
1018 args
= make_tree_vector ();
1021 if (TREE_CODE (argtype
) == TREE_LIST
)
1023 for (tree elt
= argtype
; elt
&& elt
!= void_list_node
;
1024 elt
= TREE_CHAIN (elt
))
1026 tree type
= TREE_VALUE (elt
);
1027 tree arg
= build_stub_object (type
);
1028 vec_safe_push (args
, arg
);
1033 tree arg
= build_stub_object (argtype
);
1034 args
->quick_push (arg
);
1038 fns
= lookup_fnfields (binfo
, name
, 0);
1039 rval
= build_new_method_call (ob
, fns
, &args
, binfo
, flags
, &fn
, complain
);
1041 release_tree_vector (args
);
1042 if (fn
&& rval
== error_mark_node
)
1048 /* Locate the dtor of TYPE. */
1051 get_dtor (tree type
, tsubst_flags_t complain
)
1053 tree fn
= locate_fn_flags (type
, complete_dtor_identifier
, NULL_TREE
,
1054 LOOKUP_NORMAL
, complain
);
1055 if (fn
== error_mark_node
)
1060 /* Locate the default ctor of TYPE. */
1063 locate_ctor (tree type
)
1067 push_deferring_access_checks (dk_no_check
);
1068 fn
= locate_fn_flags (type
, complete_ctor_identifier
, NULL_TREE
,
1069 LOOKUP_SPECULATIVE
, tf_none
);
1070 pop_deferring_access_checks ();
1071 if (fn
== error_mark_node
)
1076 /* Likewise, but give any appropriate errors. */
1079 get_default_ctor (tree type
)
1081 tree fn
= locate_fn_flags (type
, complete_ctor_identifier
, NULL_TREE
,
1082 LOOKUP_NORMAL
, tf_warning_or_error
);
1083 if (fn
== error_mark_node
)
1088 /* Locate the copy ctor of TYPE. */
1091 get_copy_ctor (tree type
, tsubst_flags_t complain
)
1093 int quals
= (TYPE_HAS_CONST_COPY_CTOR (type
)
1094 ? TYPE_QUAL_CONST
: TYPE_UNQUALIFIED
);
1095 tree argtype
= build_stub_type (type
, quals
, false);
1096 tree fn
= locate_fn_flags (type
, complete_ctor_identifier
, argtype
,
1097 LOOKUP_NORMAL
, complain
);
1098 if (fn
== error_mark_node
)
1103 /* Locate the copy assignment operator of TYPE. */
1106 get_copy_assign (tree type
)
1108 int quals
= (TYPE_HAS_CONST_COPY_ASSIGN (type
)
1109 ? TYPE_QUAL_CONST
: TYPE_UNQUALIFIED
);
1110 tree argtype
= build_stub_type (type
, quals
, false);
1111 tree fn
= locate_fn_flags (type
, cp_assignment_operator_id (NOP_EXPR
), argtype
,
1112 LOOKUP_NORMAL
, tf_warning_or_error
);
1113 if (fn
== error_mark_node
)
1118 /* walk_tree helper function for is_trivially_xible. If *TP is a call,
1119 return it if it calls something other than a trivial special member
1123 check_nontriv (tree
*tp
, int *, void *)
1125 tree fn
= cp_get_callee (*tp
);
1126 if (fn
== NULL_TREE
)
1129 if (TREE_CODE (fn
) == ADDR_EXPR
)
1130 fn
= TREE_OPERAND (fn
, 0);
1132 if (TREE_CODE (fn
) != FUNCTION_DECL
1133 || !trivial_fn_p (fn
))
1138 /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */
1141 assignable_expr (tree to
, tree from
)
1143 ++cp_unevaluated_operand
;
1144 to
= build_stub_object (to
);
1145 from
= build_stub_object (from
);
1146 tree r
= cp_build_modify_expr (input_location
, to
, NOP_EXPR
, from
, tf_none
);
1147 --cp_unevaluated_operand
;
1151 /* The predicate condition for a template specialization
1152 is_constructible<T, Args...> shall be satisfied if and only if the
1153 following variable definition would be well-formed for some invented
1154 variable t: T t(create<Args>()...);
1156 Return something equivalent in well-formedness and triviality. */
1159 constructible_expr (tree to
, tree from
)
1162 if (CLASS_TYPE_P (to
))
1165 vec
<tree
, va_gc
> *args
= NULL
;
1166 cp_unevaluated cp_uneval_guard
;
1167 if (TREE_CODE (to
) != REFERENCE_TYPE
)
1168 to
= cp_build_reference_type (to
, /*rval*/false);
1169 tree ob
= build_stub_object (to
);
1170 for (; from
; from
= TREE_CHAIN (from
))
1171 vec_safe_push (args
, build_stub_object (TREE_VALUE (from
)));
1172 expr
= build_special_member_call (ob
, complete_ctor_identifier
, &args
,
1173 ctype
, LOOKUP_NORMAL
, tf_none
);
1174 if (expr
== error_mark_node
)
1175 return error_mark_node
;
1176 /* The current state of the standard vis-a-vis LWG 2116 is that
1177 is_*constructible involves destruction as well. */
1178 if (type_build_dtor_call (ctype
))
1180 tree dtor
= build_special_member_call (ob
, complete_dtor_identifier
,
1181 NULL
, ctype
, LOOKUP_NORMAL
,
1183 if (dtor
== error_mark_node
)
1184 return error_mark_node
;
1185 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype
))
1186 expr
= build2 (COMPOUND_EXPR
, void_type_node
, expr
, dtor
);
1191 if (from
== NULL_TREE
)
1192 return build_value_init (strip_array_types (to
), tf_none
);
1193 else if (TREE_CHAIN (from
))
1194 return error_mark_node
; // too many initializers
1195 from
= build_stub_object (TREE_VALUE (from
));
1196 expr
= perform_direct_initialization_if_possible (to
, from
,
1203 /* Returns a tree iff TO is assignable (if CODE is MODIFY_EXPR) or
1204 constructible (otherwise) from FROM, which is a single type for
1205 assignment or a list of types for construction. */
1208 is_xible_helper (enum tree_code code
, tree to
, tree from
, bool trivial
)
1210 if (VOID_TYPE_P (to
) || ABSTRACT_CLASS_TYPE_P (to
)
1211 || (from
&& FUNC_OR_METHOD_TYPE_P (from
)
1212 && (TYPE_READONLY (from
) || FUNCTION_REF_QUALIFIED (from
))))
1213 return error_mark_node
;
1215 if (code
== MODIFY_EXPR
)
1216 expr
= assignable_expr (to
, from
);
1217 else if (trivial
&& from
&& TREE_CHAIN (from
))
1218 return error_mark_node
; // only 0- and 1-argument ctors can be trivial
1220 expr
= constructible_expr (to
, from
);
1224 /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or
1225 constructible (otherwise) from FROM, which is a single type for
1226 assignment or a list of types for construction. */
1229 is_trivially_xible (enum tree_code code
, tree to
, tree from
)
1232 expr
= is_xible_helper (code
, to
, from
, /*trivial*/true);
1234 if (expr
== error_mark_node
)
1236 tree nt
= cp_walk_tree_without_duplicates (&expr
, check_nontriv
, NULL
);
1240 /* Returns true iff TO is assignable (if CODE is MODIFY_EXPR) or
1241 constructible (otherwise) from FROM, which is a single type for
1242 assignment or a list of types for construction. */
1245 is_xible (enum tree_code code
, tree to
, tree from
)
1247 tree expr
= is_xible_helper (code
, to
, from
, /*trivial*/false);
1248 if (expr
== error_mark_node
)
1253 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
1254 DELETED_P or give an error message MSG with argument ARG. */
1257 process_subob_fn (tree fn
, tree
*spec_p
, bool *trivial_p
,
1258 bool *deleted_p
, bool *constexpr_p
,
1259 bool diag
, tree arg
, bool dtor_from_ctor
= false)
1261 if (!fn
|| fn
== error_mark_node
)
1270 maybe_instantiate_noexcept (fn
);
1271 tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
1272 *spec_p
= merge_exception_specifiers (*spec_p
, raises
);
1275 if (!trivial_fn_p (fn
) && !dtor_from_ctor
)
1279 if (TREE_CODE (arg
) == FIELD_DECL
1280 && TREE_CODE (DECL_CONTEXT (arg
)) == UNION_TYPE
)
1285 error ("union member %q+D with non-trivial %qD", arg
, fn
);
1289 if (constexpr_p
&& !DECL_DECLARED_CONSTEXPR_P (fn
))
1291 *constexpr_p
= false;
1294 inform (DECL_SOURCE_LOCATION (fn
),
1295 "defaulted constructor calls non-constexpr %qD", fn
);
1296 explain_invalid_constexpr_fn (fn
);
1301 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
1302 aggregates. If DTOR_FROM_CTOR is true, we're walking subobject destructors
1303 called from a synthesized constructor, in which case we don't consider
1304 the triviality of the subobject destructor. */
1307 walk_field_subobs (tree fields
, tree fnname
, special_function_kind sfk
,
1308 int quals
, bool copy_arg_p
, bool move_p
,
1309 bool assign_p
, tree
*spec_p
, bool *trivial_p
,
1310 bool *deleted_p
, bool *constexpr_p
,
1311 bool diag
, int flags
, tsubst_flags_t complain
,
1312 bool dtor_from_ctor
)
1315 for (field
= fields
; field
; field
= DECL_CHAIN (field
))
1317 tree mem_type
, argtype
, rval
;
1319 if (TREE_CODE (field
) != FIELD_DECL
1320 || DECL_ARTIFICIAL (field
))
1323 mem_type
= strip_array_types (TREE_TYPE (field
));
1327 if (CP_TYPE_CONST_P (mem_type
) && !CLASS_TYPE_P (mem_type
))
1330 error ("non-static const member %q#D, can%'t use default "
1331 "assignment operator", field
);
1333 else if (TREE_CODE (mem_type
) == REFERENCE_TYPE
)
1336 error ("non-static reference member %q#D, can%'t use "
1337 "default assignment operator", field
);
1342 if (bad
&& deleted_p
)
1345 else if (sfk
== sfk_constructor
|| sfk
== sfk_inheriting_constructor
)
1349 if (DECL_INITIAL (field
))
1351 if (diag
&& DECL_INITIAL (field
) == error_mark_node
)
1352 inform (DECL_SOURCE_LOCATION (field
),
1353 "initializer for %q#D is invalid", field
);
1356 /* Core 1351: If the field has an NSDMI that could throw, the
1357 default constructor is noexcept(false). */
1360 tree nsdmi
= get_nsdmi (field
, /*ctor*/false, complain
);
1361 if (!expr_noexcept_p (nsdmi
, complain
))
1362 *spec_p
= noexcept_false_spec
;
1364 /* Don't do the normal processing. */
1369 if (CP_TYPE_CONST_P (mem_type
)
1370 && default_init_uninitialized_part (mem_type
))
1374 error ("uninitialized const member in %q#T",
1375 current_class_type
);
1376 inform (DECL_SOURCE_LOCATION (field
),
1377 "%q#D should be initialized", field
);
1381 else if (TREE_CODE (mem_type
) == REFERENCE_TYPE
)
1385 error ("uninitialized reference member in %q#T",
1386 current_class_type
);
1387 inform (DECL_SOURCE_LOCATION (field
),
1388 "%q#D should be initialized", field
);
1393 if (bad
&& deleted_p
)
1396 /* For an implicitly-defined default constructor to be constexpr,
1397 every member must have a user-provided default constructor or
1398 an explicit initializer. */
1399 if (constexpr_p
&& !CLASS_TYPE_P (mem_type
)
1400 && TREE_CODE (DECL_CONTEXT (field
)) != UNION_TYPE
)
1402 *constexpr_p
= false;
1404 inform (DECL_SOURCE_LOCATION (field
),
1405 "defaulted default constructor does not "
1406 "initialize %q#D", field
);
1409 else if (sfk
== sfk_copy_constructor
)
1412 if (TREE_CODE (mem_type
) == REFERENCE_TYPE
1413 && TYPE_REF_IS_RVALUE (mem_type
))
1416 error ("copying non-static data member %q#D of rvalue "
1417 "reference type", field
);
1423 if (!CLASS_TYPE_P (mem_type
))
1426 if (ANON_AGGR_TYPE_P (mem_type
))
1428 walk_field_subobs (TYPE_FIELDS (mem_type
), fnname
, sfk
, quals
,
1429 copy_arg_p
, move_p
, assign_p
, spec_p
, trivial_p
,
1430 deleted_p
, constexpr_p
,
1431 diag
, flags
, complain
, dtor_from_ctor
);
1437 int mem_quals
= cp_type_quals (mem_type
) | quals
;
1438 if (DECL_MUTABLE_P (field
))
1439 mem_quals
&= ~TYPE_QUAL_CONST
;
1440 argtype
= build_stub_type (mem_type
, mem_quals
, move_p
);
1443 argtype
= NULL_TREE
;
1445 rval
= locate_fn_flags (mem_type
, fnname
, argtype
, flags
, complain
);
1447 process_subob_fn (rval
, spec_p
, trivial_p
, deleted_p
,
1448 constexpr_p
, diag
, field
, dtor_from_ctor
);
1452 /* Base walker helper for synthesized_method_walk. Inspect a direct
1453 or virtual base. BINFO is the parent type's binfo. BASE_BINFO is
1454 the base binfo of interests. All other parms are as for
1455 synthesized_method_walk, or its local vars. */
1458 synthesized_method_base_walk (tree binfo
, tree base_binfo
,
1459 int quals
, bool copy_arg_p
,
1460 bool move_p
, bool ctor_p
,
1461 tree
*inheriting_ctor
, tree inherited_parms
,
1462 tree fnname
, int flags
, bool diag
,
1463 tree
*spec_p
, bool *trivial_p
,
1464 bool *deleted_p
, bool *constexpr_p
)
1466 bool inherited_binfo
= false;
1467 tree argtype
= NULL_TREE
;
1468 deferring_kind defer
= dk_no_deferred
;
1471 argtype
= build_stub_type (BINFO_TYPE (base_binfo
), quals
, move_p
);
1472 else if (inheriting_ctor
1474 = binfo_inherited_from (binfo
, base_binfo
, *inheriting_ctor
)))
1476 argtype
= inherited_parms
;
1477 /* Don't check access on the inherited constructor. */
1478 if (flag_new_inheriting_ctors
)
1479 defer
= dk_deferred
;
1481 /* To be conservative, ignore access to the base dtor that
1482 DR1658 instructs us to ignore. See the comment in
1483 synthesized_method_walk. */
1484 else if (cxx_dialect
>= cxx14
&& fnname
== complete_dtor_identifier
1485 && BINFO_VIRTUAL_P (base_binfo
)
1486 && ABSTRACT_CLASS_TYPE_P (BINFO_TYPE (binfo
)))
1487 defer
= dk_no_check
;
1489 if (defer
!= dk_no_deferred
)
1490 push_deferring_access_checks (defer
);
1491 tree rval
= locate_fn_flags (base_binfo
, fnname
, argtype
, flags
,
1492 diag
? tf_warning_or_error
: tf_none
);
1493 if (defer
!= dk_no_deferred
)
1494 pop_deferring_access_checks ();
1496 /* Replace an inherited template with the appropriate specialization. */
1497 if (inherited_binfo
&& rval
1498 && DECL_P (*inheriting_ctor
) && DECL_P (rval
)
1499 && DECL_CONTEXT (*inheriting_ctor
) == DECL_CONTEXT (rval
))
1500 *inheriting_ctor
= DECL_CLONED_FUNCTION (rval
);
1502 process_subob_fn (rval
, spec_p
, trivial_p
, deleted_p
,
1503 constexpr_p
, diag
, BINFO_TYPE (base_binfo
));
1505 (!BINFO_VIRTUAL_P (base_binfo
)
1506 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo
))))
1508 /* In a constructor we also need to check the subobject
1509 destructors for cleanup of partially constructed objects. */
1510 tree dtor
= locate_fn_flags (base_binfo
, complete_dtor_identifier
,
1512 diag
? tf_warning_or_error
: tf_none
);
1513 /* Note that we don't pass down trivial_p; the subobject
1514 destructors don't affect triviality of the constructor. Nor
1515 do they affect constexpr-ness (a constant expression doesn't
1516 throw) or exception-specification (a throw from one of the
1517 dtors would be a double-fault). */
1518 process_subob_fn (dtor
, NULL
, NULL
, deleted_p
, NULL
, false,
1519 BINFO_TYPE (base_binfo
), /*dtor_from_ctor*/true);
1525 /* The caller wants to generate an implicit declaration of SFK for
1526 CTYPE which is const if relevant and CONST_P is set. If SPEC_P,
1527 TRIVIAL_P, DELETED_P or CONSTEXPR_P are non-null, set their
1528 referent appropriately. If DIAG is true, we're either being called
1529 from maybe_explain_implicit_delete to give errors, or if
1530 CONSTEXPR_P is non-null, from explain_invalid_constexpr_fn. */
1533 synthesized_method_walk (tree ctype
, special_function_kind sfk
, bool const_p
,
1534 tree
*spec_p
, bool *trivial_p
, bool *deleted_p
,
1535 bool *constexpr_p
, bool diag
,
1536 tree
*inheriting_ctor
, tree inherited_parms
)
1538 tree binfo
, base_binfo
, fnname
;
1542 *spec_p
= (cxx_dialect
>= cxx11
? noexcept_true_spec
: empty_except_spec
);
1546 /* "The closure type associated with a lambda-expression has a deleted
1547 default constructor and a deleted copy assignment operator."
1548 This is diagnosed in maybe_explain_implicit_delete. */
1549 if (LAMBDA_TYPE_P (ctype
)
1550 && (sfk
== sfk_constructor
1551 || sfk
== sfk_copy_assignment
))
1560 bool ctor_p
= false;
1561 bool assign_p
= false;
1562 bool check_vdtor
= false;
1565 case sfk_move_assignment
:
1566 case sfk_copy_assignment
:
1568 fnname
= cp_assignment_operator_id (NOP_EXPR
);
1571 case sfk_destructor
:
1573 /* The synthesized method will call base dtors, but check complete
1574 here to avoid having to deal with VTT. */
1575 fnname
= complete_dtor_identifier
;
1578 case sfk_constructor
:
1579 case sfk_move_constructor
:
1580 case sfk_copy_constructor
:
1581 case sfk_inheriting_constructor
:
1583 fnname
= complete_ctor_identifier
;
1590 gcc_assert ((sfk
== sfk_inheriting_constructor
)
1591 == (inheriting_ctor
&& *inheriting_ctor
!= NULL_TREE
));
1593 /* If that user-written default constructor would satisfy the
1594 requirements of a constexpr constructor (7.1.5), the
1595 implicitly-defined default constructor is constexpr.
1597 The implicitly-defined copy/move assignment operator is constexpr if
1598 - X is a literal type, and
1599 - the assignment operator selected to copy/move each direct base class
1600 subobject is a constexpr function, and
1601 - for each non-static data member of X that is of class type (or array
1602 thereof), the assignment operator selected to copy/move that
1603 member is a constexpr function. */
1605 *constexpr_p
= ctor_p
|| (assign_p
&& cxx_dialect
>= cxx14
);
1607 bool move_p
= false;
1608 bool copy_arg_p
= false;
1611 case sfk_constructor
:
1612 case sfk_destructor
:
1613 case sfk_inheriting_constructor
:
1616 case sfk_move_constructor
:
1617 case sfk_move_assignment
:
1620 case sfk_copy_constructor
:
1621 case sfk_copy_assignment
:
1629 bool expected_trivial
= type_has_trivial_fn (ctype
, sfk
);
1631 *trivial_p
= expected_trivial
;
1633 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
1634 class versions and other properties of the type. But a subobject
1635 class can be trivially copyable and yet have overload resolution
1636 choose a template constructor for initialization, depending on
1637 rvalueness and cv-quals. And furthermore, a member in a base might
1638 be trivial but deleted or otherwise not callable. So we can't exit
1639 early in C++0x. The same considerations apply in C++98/03, but
1640 there the definition of triviality does not consider overload
1641 resolution, so a constructor can be trivial even if it would otherwise
1642 call a non-trivial constructor. */
1643 if (expected_trivial
1644 && (!copy_arg_p
|| cxx_dialect
< cxx11
))
1646 if (constexpr_p
&& sfk
== sfk_constructor
)
1648 bool cx
= trivial_default_constructor_is_constexpr (ctype
);
1650 if (diag
&& !cx
&& TREE_CODE (ctype
) == UNION_TYPE
)
1651 /* A trivial constructor doesn't have any NSDMI. */
1652 inform (input_location
, "defaulted default constructor does "
1653 "not initialize any non-static data member");
1655 if (!diag
&& cxx_dialect
< cxx11
)
1659 ++cp_unevaluated_operand
;
1660 ++c_inhibit_evaluation_warnings
;
1661 push_deferring_access_checks (dk_no_deferred
);
1663 tree scope
= push_scope (ctype
);
1665 int flags
= LOOKUP_NORMAL
| LOOKUP_SPECULATIVE
;
1666 if (sfk
!= sfk_inheriting_constructor
)
1667 flags
|= LOOKUP_DEFAULTED
;
1669 tsubst_flags_t complain
= diag
? tf_warning_or_error
: tf_none
;
1671 /* We're in get_defaulted_eh_spec; we don't actually want any walking
1672 diagnostics, we just want complain set. */
1674 int quals
= const_p
? TYPE_QUAL_CONST
: TYPE_UNQUALIFIED
;
1676 for (binfo
= TYPE_BINFO (ctype
), i
= 0;
1677 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
1679 if (!assign_p
&& BINFO_VIRTUAL_P (base_binfo
))
1680 /* We'll handle virtual bases below. */
1683 tree fn
= synthesized_method_base_walk (binfo
, base_binfo
, quals
,
1684 copy_arg_p
, move_p
, ctor_p
,
1687 fnname
, flags
, diag
,
1689 deleted_p
, constexpr_p
);
1691 if (diag
&& assign_p
&& move_p
1692 && BINFO_VIRTUAL_P (base_binfo
)
1693 && fn
&& TREE_CODE (fn
) == FUNCTION_DECL
1694 && move_fn_p (fn
) && !trivial_fn_p (fn
)
1695 && vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo
)))
1696 warning (OPT_Wvirtual_move_assign
,
1697 "defaulted move assignment for %qT calls a non-trivial "
1698 "move assignment operator for virtual base %qT",
1699 ctype
, BINFO_TYPE (base_binfo
));
1701 if (check_vdtor
&& type_has_virtual_destructor (BINFO_TYPE (base_binfo
)))
1703 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
1704 to have a null fn (no class-specific op delete). */
1705 fn
= locate_fn_flags (ctype
, cp_operator_id (DELETE_EXPR
),
1706 ptr_type_node
, flags
, tf_none
);
1707 if (fn
&& fn
== error_mark_node
)
1709 if (complain
& tf_error
)
1710 locate_fn_flags (ctype
, cp_operator_id (DELETE_EXPR
),
1711 ptr_type_node
, flags
, complain
);
1715 check_vdtor
= false;
1719 vec
<tree
, va_gc
> *vbases
= CLASSTYPE_VBASECLASSES (ctype
);
1721 /* Already examined vbases above. */;
1722 else if (vec_safe_is_empty (vbases
))
1723 /* No virtual bases to worry about. */;
1724 else if (ABSTRACT_CLASS_TYPE_P (ctype
) && cxx_dialect
>= cxx14
1725 /* DR 1658 specifies that vbases of abstract classes are
1726 ignored for both ctors and dtors. However, that breaks
1727 virtual dtor overriding when the ignored base has a
1728 throwing destructor. So, ignore that piece of 1658. A
1729 defect has been filed (no number yet). */
1730 && sfk
!= sfk_destructor
)
1731 /* Vbase cdtors are not relevant. */;
1735 *constexpr_p
= false;
1737 FOR_EACH_VEC_ELT (*vbases
, i
, base_binfo
)
1738 synthesized_method_base_walk (binfo
, base_binfo
, quals
,
1739 copy_arg_p
, move_p
, ctor_p
,
1740 inheriting_ctor
, inherited_parms
,
1741 fnname
, flags
, diag
,
1743 deleted_p
, constexpr_p
);
1746 /* Now handle the non-static data members. */
1747 walk_field_subobs (TYPE_FIELDS (ctype
), fnname
, sfk
, quals
,
1748 copy_arg_p
, move_p
, assign_p
, spec_p
, trivial_p
,
1749 deleted_p
, constexpr_p
,
1750 diag
, flags
, complain
, /*dtor_from_ctor*/false);
1752 walk_field_subobs (TYPE_FIELDS (ctype
), complete_dtor_identifier
,
1753 sfk_destructor
, TYPE_UNQUALIFIED
, false,
1754 false, false, NULL
, NULL
,
1756 false, flags
, complain
, /*dtor_from_ctor*/true);
1760 pop_deferring_access_checks ();
1761 --cp_unevaluated_operand
;
1762 --c_inhibit_evaluation_warnings
;
1765 /* DECL is a defaulted function whose exception specification is now
1766 needed. Return what it should be. */
1769 get_defaulted_eh_spec (tree decl
, tsubst_flags_t complain
)
1771 if (DECL_CLONED_FUNCTION_P (decl
))
1772 decl
= DECL_CLONED_FUNCTION (decl
);
1773 special_function_kind sfk
= special_function_p (decl
);
1774 tree ctype
= DECL_CONTEXT (decl
);
1775 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
1776 tree parm_type
= TREE_VALUE (parms
);
1777 bool const_p
= CP_TYPE_CONST_P (non_reference (parm_type
));
1778 tree spec
= empty_except_spec
;
1779 bool diag
= !DECL_DELETED_FN (decl
) && (complain
& tf_error
);
1780 tree inh
= DECL_INHERITED_CTOR (decl
);
1781 synthesized_method_walk (ctype
, sfk
, const_p
, &spec
, NULL
, NULL
,
1782 NULL
, diag
, &inh
, parms
);
1786 /* DECL is a deleted function. If it's implicitly deleted, explain why and
1787 return true; else return false. */
1790 maybe_explain_implicit_delete (tree decl
)
1792 /* If decl is a clone, get the primary variant. */
1793 decl
= DECL_ORIGIN (decl
);
1794 gcc_assert (DECL_DELETED_FN (decl
));
1795 if (DECL_DEFAULTED_FN (decl
))
1797 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
1798 static hash_set
<tree
> *explained
;
1800 special_function_kind sfk
;
1806 explained
= new hash_set
<tree
>;
1807 if (explained
->add (decl
))
1810 sfk
= special_function_p (decl
);
1811 ctype
= DECL_CONTEXT (decl
);
1812 loc
= input_location
;
1813 input_location
= DECL_SOURCE_LOCATION (decl
);
1816 if (LAMBDA_TYPE_P (ctype
))
1819 if (sfk
== sfk_constructor
)
1820 inform (DECL_SOURCE_LOCATION (decl
),
1821 "a lambda closure type has a deleted default constructor");
1822 else if (sfk
== sfk_copy_assignment
)
1823 inform (DECL_SOURCE_LOCATION (decl
),
1824 "a lambda closure type has a deleted copy assignment operator");
1828 else if (DECL_ARTIFICIAL (decl
)
1829 && (sfk
== sfk_copy_assignment
|| sfk
== sfk_copy_constructor
)
1830 && classtype_has_move_assign_or_move_ctor_p (ctype
, true))
1832 inform (DECL_SOURCE_LOCATION (decl
),
1833 "%q#D is implicitly declared as deleted because %qT "
1834 "declares a move constructor or move assignment operator",
1838 else if (sfk
== sfk_inheriting_constructor
)
1840 tree binfo
= inherited_ctor_binfo (decl
);
1841 if (TREE_CODE (binfo
) != TREE_BINFO
)
1843 inform (DECL_SOURCE_LOCATION (decl
),
1844 "%q#D inherits from multiple base subobjects",
1851 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
1852 tree parm_type
= TREE_VALUE (parms
);
1853 bool const_p
= CP_TYPE_CONST_P (non_reference (parm_type
));
1854 tree raises
= NULL_TREE
;
1855 bool deleted_p
= false;
1856 tree scope
= push_scope (ctype
);
1857 tree inh
= DECL_INHERITED_CTOR (decl
);
1859 synthesized_method_walk (ctype
, sfk
, const_p
,
1860 &raises
, NULL
, &deleted_p
, NULL
, false,
1864 inform (DECL_SOURCE_LOCATION (decl
),
1865 "%q#D is implicitly deleted because the default "
1866 "definition would be ill-formed:", decl
);
1867 synthesized_method_walk (ctype
, sfk
, const_p
,
1868 NULL
, NULL
, NULL
, NULL
, true,
1871 else if (!comp_except_specs
1872 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl
)),
1874 inform (DECL_SOURCE_LOCATION (decl
), "%q#F is implicitly "
1875 "deleted because its exception-specification does not "
1876 "match the implicit exception-specification %qX",
1878 else if (flag_checking
)
1884 input_location
= loc
;
1890 /* DECL is a defaulted function which was declared constexpr. Explain why
1891 it can't be constexpr. */
1894 explain_implicit_non_constexpr (tree decl
)
1896 tree parm_type
= TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (decl
));
1897 bool const_p
= CP_TYPE_CONST_P (non_reference (parm_type
));
1898 tree inh
= DECL_INHERITED_CTOR (decl
);
1900 synthesized_method_walk (DECL_CLASS_CONTEXT (decl
),
1901 special_function_p (decl
), const_p
,
1902 NULL
, NULL
, NULL
, &dummy
, true,
1904 FUNCTION_FIRST_USER_PARMTYPE (decl
));
1907 /* DECL is an instantiation of an inheriting constructor template. Deduce
1908 the correct exception-specification and deletedness for this particular
1912 deduce_inheriting_ctor (tree decl
)
1914 decl
= DECL_ORIGIN (decl
);
1915 gcc_assert (DECL_INHERITED_CTOR (decl
));
1917 bool trivial
, constexpr_
, deleted
;
1918 tree inh
= DECL_INHERITED_CTOR (decl
);
1919 synthesized_method_walk (DECL_CONTEXT (decl
), sfk_inheriting_constructor
,
1920 false, &spec
, &trivial
, &deleted
, &constexpr_
,
1923 FUNCTION_FIRST_USER_PARMTYPE (decl
));
1924 if (TREE_CODE (inherited_ctor_binfo (decl
)) != TREE_BINFO
)
1925 /* Inherited the same constructor from different base subobjects. */
1927 DECL_DELETED_FN (decl
) = deleted
;
1928 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
), spec
);
1929 SET_DECL_INHERITED_CTOR (decl
, inh
);
1932 FOR_EACH_CLONE (clone
, decl
)
1934 DECL_DELETED_FN (clone
) = deleted
;
1935 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
1936 SET_DECL_INHERITED_CTOR (clone
, inh
);
1940 /* Implicitly declare the special function indicated by KIND, as a
1941 member of TYPE. For copy constructors and assignment operators,
1942 CONST_P indicates whether these functions should take a const
1943 reference argument or a non-const reference. Returns the
1944 FUNCTION_DECL for the implicitly declared function. */
1947 implicitly_declare_fn (special_function_kind kind
, tree type
,
1948 bool const_p
, tree inherited_ctor
,
1949 tree inherited_parms
)
1952 tree parameter_types
= void_list_node
;
1955 tree raises
= empty_except_spec
;
1956 tree rhs_parm_type
= NULL_TREE
;
1959 HOST_WIDE_INT saved_processing_template_decl
;
1963 /* Because we create declarations for implicitly declared functions
1964 lazily, we may be creating the declaration for a member of TYPE
1965 while in some completely different context. However, TYPE will
1966 never be a dependent class (because we never want to do lookups
1967 for implicitly defined functions in a dependent class).
1968 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
1969 because we only create clones for constructors and destructors
1970 when not in a template. */
1971 gcc_assert (!dependent_type_p (type
));
1972 saved_processing_template_decl
= processing_template_decl
;
1973 processing_template_decl
= 0;
1975 type
= TYPE_MAIN_VARIANT (type
);
1977 if (targetm
.cxx
.cdtor_returns_this ())
1979 if (kind
== sfk_destructor
)
1980 /* See comment in check_special_function_return_type. */
1981 return_type
= build_pointer_type (void_type_node
);
1983 return_type
= build_pointer_type (type
);
1986 return_type
= void_type_node
;
1990 case sfk_destructor
:
1992 name
= dtor_identifier
;
1995 case sfk_constructor
:
1996 /* Default constructor. */
1997 name
= ctor_identifier
;
2000 case sfk_copy_constructor
:
2001 case sfk_copy_assignment
:
2002 case sfk_move_constructor
:
2003 case sfk_move_assignment
:
2004 case sfk_inheriting_constructor
:
2006 if (kind
== sfk_copy_assignment
2007 || kind
== sfk_move_assignment
)
2009 return_type
= build_reference_type (type
);
2010 name
= cp_assignment_operator_id (NOP_EXPR
);
2013 name
= ctor_identifier
;
2015 if (kind
== sfk_inheriting_constructor
)
2016 parameter_types
= inherited_parms
;
2020 rhs_parm_type
= cp_build_qualified_type (type
, TYPE_QUAL_CONST
);
2022 rhs_parm_type
= type
;
2023 bool move_p
= (kind
== sfk_move_assignment
2024 || kind
== sfk_move_constructor
);
2025 rhs_parm_type
= cp_build_reference_type (rhs_parm_type
, move_p
);
2027 parameter_types
= tree_cons (NULL_TREE
, rhs_parm_type
, parameter_types
);
2035 bool trivial_p
= false;
2039 /* For an inheriting constructor, just copy these flags from the
2040 inherited constructor until deduce_inheriting_ctor. */
2041 raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor
));
2042 deleted_p
= DECL_DELETED_FN (inherited_ctor
);
2043 constexpr_p
= DECL_DECLARED_CONSTEXPR_P (inherited_ctor
);
2045 else if (cxx_dialect
>= cxx11
)
2047 raises
= noexcept_deferred_spec
;
2048 synthesized_method_walk (type
, kind
, const_p
, NULL
, &trivial_p
,
2049 &deleted_p
, &constexpr_p
, false,
2050 &inherited_ctor
, inherited_parms
);
2053 synthesized_method_walk (type
, kind
, const_p
, &raises
, &trivial_p
,
2054 &deleted_p
, &constexpr_p
, false,
2055 &inherited_ctor
, inherited_parms
);
2056 /* Don't bother marking a deleted constructor as constexpr. */
2058 constexpr_p
= false;
2059 /* A trivial copy/move constructor is also a constexpr constructor,
2060 unless the class has virtual bases (7.1.5p4). */
2061 else if (trivial_p
&& cxx_dialect
>= cxx11
2062 && (kind
== sfk_copy_constructor
2063 || kind
== sfk_move_constructor
)
2064 && !CLASSTYPE_VBASECLASSES (type
))
2065 gcc_assert (constexpr_p
);
2067 if (!trivial_p
&& type_has_trivial_fn (type
, kind
))
2068 type_set_nontrivial_flag (type
, kind
);
2070 /* Create the function. */
2071 fn_type
= build_method_type_directly (type
, return_type
, parameter_types
);
2073 fn_type
= build_exception_variant (fn_type
, raises
);
2074 fn
= build_lang_decl (FUNCTION_DECL
, name
, fn_type
);
2075 if (kind
!= sfk_inheriting_constructor
)
2076 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (TYPE_NAME (type
));
2078 if (!IDENTIFIER_CDTOR_P (name
))
2079 /* Assignment operator. */
2080 SET_OVERLOADED_OPERATOR_CODE (fn
, NOP_EXPR
);
2081 else if (IDENTIFIER_CTOR_P (name
))
2082 DECL_CXX_CONSTRUCTOR_P (fn
) = true;
2084 DECL_CXX_DESTRUCTOR_P (fn
) = true;
2086 SET_DECL_ALIGN (fn
, MINIMUM_METHOD_BOUNDARY
);
2088 /* Create the explicit arguments. */
2091 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
2092 want its type to be included in the mangled function
2094 tree decl
= cp_build_parm_decl (fn
, NULL_TREE
, rhs_parm_type
);
2095 TREE_READONLY (decl
) = 1;
2096 retrofit_lang_decl (decl
);
2097 DECL_PARM_INDEX (decl
) = DECL_PARM_LEVEL (decl
) = 1;
2098 DECL_ARGUMENTS (fn
) = decl
;
2100 else if (kind
== sfk_inheriting_constructor
)
2102 tree
*p
= &DECL_ARGUMENTS (fn
);
2104 for (tree parm
= inherited_parms
; parm
&& parm
!= void_list_node
;
2105 parm
= TREE_CHAIN (parm
))
2107 *p
= cp_build_parm_decl (fn
, NULL_TREE
, TREE_VALUE (parm
));
2108 retrofit_lang_decl (*p
);
2109 DECL_PARM_LEVEL (*p
) = 1;
2110 DECL_PARM_INDEX (*p
) = index
++;
2111 p
= &DECL_CHAIN (*p
);
2113 SET_DECL_INHERITED_CTOR (fn
, inherited_ctor
);
2114 DECL_NONCONVERTING_P (fn
) = DECL_NONCONVERTING_P (inherited_ctor
);
2115 /* A constructor so declared has the same access as the corresponding
2116 constructor in X. */
2117 TREE_PRIVATE (fn
) = TREE_PRIVATE (inherited_ctor
);
2118 TREE_PROTECTED (fn
) = TREE_PROTECTED (inherited_ctor
);
2119 /* Copy constexpr from the inherited constructor even if the
2120 inheriting constructor doesn't satisfy the requirements. */
2121 constexpr_p
= DECL_DECLARED_CONSTEXPR_P (inherited_ctor
);
2123 /* Add the "this" parameter. */
2124 this_parm
= build_this_parm (fn
, fn_type
, TYPE_UNQUALIFIED
);
2125 DECL_CHAIN (this_parm
) = DECL_ARGUMENTS (fn
);
2126 DECL_ARGUMENTS (fn
) = this_parm
;
2128 grokclassfn (type
, fn
, kind
== sfk_destructor
? DTOR_FLAG
: NO_SPECIAL
);
2129 DECL_IN_AGGR_P (fn
) = 1;
2130 DECL_ARTIFICIAL (fn
) = 1;
2131 DECL_DEFAULTED_FN (fn
) = 1;
2132 if (cxx_dialect
>= cxx11
)
2134 DECL_DELETED_FN (fn
) = deleted_p
;
2135 DECL_DECLARED_CONSTEXPR_P (fn
) = constexpr_p
;
2137 DECL_EXTERNAL (fn
) = true;
2138 DECL_NOT_REALLY_EXTERN (fn
) = 1;
2139 DECL_DECLARED_INLINE_P (fn
) = 1;
2140 set_linkage_according_to_type (type
, fn
);
2141 if (TREE_PUBLIC (fn
))
2142 DECL_COMDAT (fn
) = 1;
2143 rest_of_decl_compilation (fn
, namespace_bindings_p (), at_eof
);
2144 gcc_assert (!TREE_USED (fn
));
2146 /* Propagate constraints from the inherited constructor. */
2147 if (flag_concepts
&& inherited_ctor
)
2148 if (tree orig_ci
= get_constraints (inherited_ctor
))
2150 tree new_ci
= copy_node (orig_ci
);
2151 set_constraints (fn
, new_ci
);
2154 /* Restore PROCESSING_TEMPLATE_DECL. */
2155 processing_template_decl
= saved_processing_template_decl
;
2157 if (inherited_ctor
&& TREE_CODE (inherited_ctor
) == TEMPLATE_DECL
)
2158 fn
= add_inherited_template_parms (fn
, inherited_ctor
);
2160 /* Warn about calling a non-trivial move assignment in a virtual base. */
2161 if (kind
== sfk_move_assignment
&& !deleted_p
&& !trivial_p
2162 && CLASSTYPE_VBASECLASSES (type
))
2164 location_t loc
= input_location
;
2165 input_location
= DECL_SOURCE_LOCATION (fn
);
2166 synthesized_method_walk (type
, kind
, const_p
,
2167 NULL
, NULL
, NULL
, NULL
, true,
2169 input_location
= loc
;
2175 /* Gives any errors about defaulted functions which need to be deferred
2176 until the containing class is complete. */
2179 defaulted_late_check (tree fn
)
2181 /* Complain about invalid signature for defaulted fn. */
2182 tree ctx
= DECL_CONTEXT (fn
);
2183 special_function_kind kind
= special_function_p (fn
);
2184 bool fn_const_p
= (copy_fn_p (fn
) == 2);
2185 tree implicit_fn
= implicitly_declare_fn (kind
, ctx
, fn_const_p
,
2187 tree eh_spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn
));
2189 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn
)),
2190 TREE_TYPE (TREE_TYPE (implicit_fn
)))
2191 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2192 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn
))))
2194 error ("defaulted declaration %q+D does not match the "
2195 "expected signature", fn
);
2196 inform (DECL_SOURCE_LOCATION (fn
),
2197 "expected signature: %qD", implicit_fn
);
2201 if (DECL_DELETED_FN (implicit_fn
))
2203 DECL_DELETED_FN (fn
) = 1;
2207 /* 8.4.2/2: An explicitly-defaulted function (...) may have an explicit
2208 exception-specification only if it is compatible (15.4) with the
2209 exception-specification on the implicit declaration. If a function
2210 is explicitly defaulted on its first declaration, (...) it is
2211 implicitly considered to have the same exception-specification as if
2212 it had been implicitly declared. */
2213 maybe_instantiate_noexcept (fn
);
2214 tree fn_spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
2217 if (DECL_DEFAULTED_IN_CLASS_P (fn
))
2218 TREE_TYPE (fn
) = build_exception_variant (TREE_TYPE (fn
), eh_spec
);
2220 else if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec
))
2221 /* Equivalent to the implicit spec. */;
2222 else if (DECL_DEFAULTED_IN_CLASS_P (fn
)
2223 && !CLASSTYPE_TEMPLATE_INSTANTIATION (ctx
))
2224 /* We can't compare an explicit exception-specification on a
2225 constructor defaulted in the class body to the implicit
2226 exception-specification until after we've parsed any NSDMI; see
2227 after_nsdmi_defaulted_late_checks. */;
2230 tree eh_spec
= get_defaulted_eh_spec (fn
);
2231 if (!comp_except_specs (fn_spec
, eh_spec
, ce_normal
))
2233 if (DECL_DEFAULTED_IN_CLASS_P (fn
))
2234 DECL_DELETED_FN (fn
) = true;
2236 error ("function %q+D defaulted on its redeclaration "
2237 "with an exception-specification that differs from "
2238 "the implicit exception-specification %qX", fn
, eh_spec
);
2242 if (DECL_DEFAULTED_IN_CLASS_P (fn
)
2243 && DECL_DECLARED_CONSTEXPR_P (implicit_fn
))
2245 /* Hmm...should we do this for out-of-class too? Should it be OK to
2246 add constexpr later like inline, rather than requiring
2247 declarations to match? */
2248 DECL_DECLARED_CONSTEXPR_P (fn
) = true;
2249 if (kind
== sfk_constructor
)
2250 TYPE_HAS_CONSTEXPR_CTOR (ctx
) = true;
2253 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn
)
2254 && DECL_DECLARED_CONSTEXPR_P (fn
))
2256 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx
))
2258 error ("explicitly defaulted function %q+D cannot be declared "
2259 "as constexpr because the implicit declaration is not "
2261 explain_implicit_non_constexpr (fn
);
2263 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
2267 /* OK, we've parsed the NSDMI for class T, now we can check any explicit
2268 exception-specifications on functions defaulted in the class body. */
2271 after_nsdmi_defaulted_late_checks (tree t
)
2273 if (uses_template_parms (t
))
2275 if (t
== error_mark_node
)
2277 for (tree fn
= TYPE_FIELDS (t
); fn
; fn
= DECL_CHAIN (fn
))
2278 if (!DECL_ARTIFICIAL (fn
)
2279 && DECL_DECLARES_FUNCTION_P (fn
)
2280 && DECL_DEFAULTED_IN_CLASS_P (fn
))
2282 tree fn_spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
2283 if (UNEVALUATED_NOEXCEPT_SPEC_P (fn_spec
))
2286 tree eh_spec
= get_defaulted_eh_spec (fn
);
2287 if (!comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
)),
2288 eh_spec
, ce_normal
))
2289 DECL_DELETED_FN (fn
) = true;
2293 /* Returns true iff FN can be explicitly defaulted, and gives any
2294 errors if defaulting FN is ill-formed. */
2297 defaultable_fn_check (tree fn
)
2299 special_function_kind kind
= sfk_none
;
2301 if (template_parm_scope_p ())
2303 error ("a template cannot be defaulted");
2307 if (DECL_CONSTRUCTOR_P (fn
))
2309 if (FUNCTION_FIRST_USER_PARMTYPE (fn
) == void_list_node
)
2310 kind
= sfk_constructor
;
2311 else if (copy_fn_p (fn
) > 0
2312 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn
))
2314 kind
= sfk_copy_constructor
;
2315 else if (move_fn_p (fn
))
2316 kind
= sfk_move_constructor
;
2318 else if (DECL_DESTRUCTOR_P (fn
))
2319 kind
= sfk_destructor
;
2320 else if (DECL_ASSIGNMENT_OPERATOR_P (fn
)
2321 && DECL_OVERLOADED_OPERATOR_P (fn
) == NOP_EXPR
)
2324 kind
= sfk_copy_assignment
;
2325 else if (move_fn_p (fn
))
2326 kind
= sfk_move_assignment
;
2329 if (kind
== sfk_none
)
2331 error ("%qD cannot be defaulted", fn
);
2336 for (tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
2337 t
&& t
!= void_list_node
; t
= TREE_CHAIN (t
))
2338 if (TREE_PURPOSE (t
))
2340 error ("defaulted function %q+D with default argument", fn
);
2344 /* Avoid do_warn_unused_parameter warnings. */
2345 for (tree p
= FUNCTION_FIRST_USER_PARM (fn
); p
; p
= DECL_CHAIN (p
))
2347 TREE_NO_WARNING (p
) = 1;
2349 if (TYPE_BEING_DEFINED (DECL_CONTEXT (fn
)))
2350 /* Defer checking. */;
2351 else if (!processing_template_decl
)
2352 defaulted_late_check (fn
);
2358 /* Add an implicit declaration to TYPE for the kind of function
2359 indicated by SFK. Return the FUNCTION_DECL for the new implicit
2363 lazily_declare_fn (special_function_kind sfk
, tree type
)
2366 /* Whether or not the argument has a const reference type. */
2367 bool const_p
= false;
2369 type
= TYPE_MAIN_VARIANT (type
);
2373 case sfk_constructor
:
2374 CLASSTYPE_LAZY_DEFAULT_CTOR (type
) = 0;
2376 case sfk_copy_constructor
:
2377 const_p
= TYPE_HAS_CONST_COPY_CTOR (type
);
2378 CLASSTYPE_LAZY_COPY_CTOR (type
) = 0;
2380 case sfk_move_constructor
:
2381 CLASSTYPE_LAZY_MOVE_CTOR (type
) = 0;
2383 case sfk_copy_assignment
:
2384 const_p
= TYPE_HAS_CONST_COPY_ASSIGN (type
);
2385 CLASSTYPE_LAZY_COPY_ASSIGN (type
) = 0;
2387 case sfk_move_assignment
:
2388 CLASSTYPE_LAZY_MOVE_ASSIGN (type
) = 0;
2390 case sfk_destructor
:
2391 CLASSTYPE_LAZY_DESTRUCTOR (type
) = 0;
2397 /* Declare the function. */
2398 fn
= implicitly_declare_fn (sfk
, type
, const_p
, NULL
, NULL
);
2400 /* [class.copy]/8 If the class definition declares a move constructor or
2401 move assignment operator, the implicitly declared copy constructor is
2402 defined as deleted.... */
2403 if ((sfk
== sfk_copy_assignment
|| sfk
== sfk_copy_constructor
)
2404 && classtype_has_move_assign_or_move_ctor_p (type
, true))
2405 DECL_DELETED_FN (fn
) = true;
2407 /* Destructors and assignment operators may be virtual. */
2408 if (sfk
== sfk_destructor
2409 || sfk
== sfk_move_assignment
2410 || sfk
== sfk_copy_assignment
)
2411 check_for_override (fn
, type
);
2413 /* Add it to the class */
2414 bool added
= add_method (type
, fn
, false);
2417 /* Add it to TYPE_FIELDS. */
2418 if (sfk
== sfk_destructor
2419 && DECL_VIRTUAL_P (fn
))
2420 /* The ABI requires that a virtual destructor go at the end of the
2422 TYPE_FIELDS (type
) = chainon (TYPE_FIELDS (type
), fn
);
2425 DECL_CHAIN (fn
) = TYPE_FIELDS (type
);
2426 TYPE_FIELDS (type
) = fn
;
2428 /* Propagate TYPE_FIELDS. */
2429 fixup_type_variants (type
);
2431 maybe_add_class_template_decl_list (type
, fn
, /*friend_p=*/0);
2432 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn
)
2433 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn
))
2434 /* Create appropriate clones. */
2435 clone_function_decl (fn
, /*update_methods=*/true);
2440 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
2441 as there are artificial parms in FN. */
2444 skip_artificial_parms_for (const_tree fn
, tree list
)
2446 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
2447 list
= TREE_CHAIN (list
);
2451 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
2452 list
= TREE_CHAIN (list
);
2453 if (DECL_HAS_VTT_PARM_P (fn
))
2454 list
= TREE_CHAIN (list
);
2458 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
2459 artificial parms in FN. */
2462 num_artificial_parms_for (const_tree fn
)
2466 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
2471 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
2473 if (DECL_HAS_VTT_PARM_P (fn
))
2479 #include "gt-cp-method.h"