1 /* Handle the hair of processing (but not expanding) inline functions.
2 Also manage function and variable name overloading.
3 Copyright (C) 1987-2020 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"
34 #include "common/common-target.h"
36 static void do_build_copy_assign (tree
);
37 static void do_build_copy_constructor (tree
);
38 static tree
make_alias_for_thunk (tree
);
40 /* Called once to initialize method.c. */
48 /* Return a this or result adjusting thunk to FUNCTION. THIS_ADJUSTING
49 indicates whether it is a this or result adjusting thunk.
50 FIXED_OFFSET and VIRTUAL_OFFSET indicate how to do the adjustment
51 (see thunk_adjust). VIRTUAL_OFFSET can be NULL, but FIXED_OFFSET
52 never is. VIRTUAL_OFFSET is the /index/ into the vtable for this
53 adjusting thunks, we scale it to a byte offset. For covariant
54 thunks VIRTUAL_OFFSET is the virtual binfo. You must post process
55 the returned thunk with finish_thunk. */
58 make_thunk (tree function
, bool this_adjusting
,
59 tree fixed_offset
, tree virtual_offset
)
64 gcc_assert (TREE_CODE (function
) == FUNCTION_DECL
);
65 /* We can have this thunks to covariant thunks, but not vice versa. */
66 gcc_assert (!DECL_THIS_THUNK_P (function
));
67 gcc_assert (!DECL_RESULT_THUNK_P (function
) || this_adjusting
);
69 /* Scale the VIRTUAL_OFFSET to be in terms of bytes. */
70 if (this_adjusting
&& virtual_offset
)
72 = size_binop (MULT_EXPR
,
75 TYPE_SIZE_UNIT (vtable_entry_type
)));
77 d
= tree_to_shwi (fixed_offset
);
79 /* See if we already have the thunk in question. For this_adjusting
80 thunks VIRTUAL_OFFSET will be an INTEGER_CST, for covariant thunks it
82 for (thunk
= DECL_THUNKS (function
); thunk
; thunk
= DECL_CHAIN (thunk
))
83 if (DECL_THIS_THUNK_P (thunk
) == this_adjusting
84 && THUNK_FIXED_OFFSET (thunk
) == d
85 && !virtual_offset
== !THUNK_VIRTUAL_OFFSET (thunk
)
88 ? tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk
),
90 : THUNK_VIRTUAL_OFFSET (thunk
) == virtual_offset
)))
93 /* All thunks must be created before FUNCTION is actually emitted;
94 the ABI requires that all thunks be emitted together with the
95 function to which they transfer control. */
96 gcc_assert (!TREE_ASM_WRITTEN (function
));
97 /* Likewise, we can only be adding thunks to a function declared in
98 the class currently being laid out. */
99 gcc_assert (TYPE_SIZE (DECL_CONTEXT (function
))
100 && TYPE_BEING_DEFINED (DECL_CONTEXT (function
)));
102 thunk
= build_decl (DECL_SOURCE_LOCATION (function
),
103 FUNCTION_DECL
, NULL_TREE
, TREE_TYPE (function
));
104 DECL_LANG_SPECIFIC (thunk
) = DECL_LANG_SPECIFIC (function
);
105 cxx_dup_lang_specific_decl (thunk
);
106 DECL_VIRTUAL_P (thunk
) = true;
107 SET_DECL_THUNKS (thunk
, NULL_TREE
);
109 DECL_CONTEXT (thunk
) = DECL_CONTEXT (function
);
110 TREE_READONLY (thunk
) = TREE_READONLY (function
);
111 TREE_THIS_VOLATILE (thunk
) = TREE_THIS_VOLATILE (function
);
112 TREE_PUBLIC (thunk
) = TREE_PUBLIC (function
);
113 SET_DECL_THUNK_P (thunk
, this_adjusting
);
114 THUNK_TARGET (thunk
) = function
;
115 THUNK_FIXED_OFFSET (thunk
) = d
;
116 THUNK_VIRTUAL_OFFSET (thunk
) = virtual_offset
;
117 THUNK_ALIAS (thunk
) = NULL_TREE
;
119 DECL_INTERFACE_KNOWN (thunk
) = 1;
120 DECL_NOT_REALLY_EXTERN (thunk
) = 1;
121 DECL_COMDAT (thunk
) = DECL_COMDAT (function
);
122 DECL_SAVED_AUTO_RETURN_TYPE (thunk
) = NULL
;
123 /* The thunk itself is not a constructor or destructor, even if
124 the thing it is thunking to is. */
125 DECL_CXX_DESTRUCTOR_P (thunk
) = 0;
126 DECL_CXX_CONSTRUCTOR_P (thunk
) = 0;
127 DECL_EXTERNAL (thunk
) = 1;
128 DECL_ARTIFICIAL (thunk
) = 1;
129 /* The THUNK is not a pending inline, even if the FUNCTION is. */
130 DECL_PENDING_INLINE_P (thunk
) = 0;
131 DECL_DECLARED_INLINE_P (thunk
) = 0;
132 /* Nor is it a template instantiation. */
133 DECL_USE_TEMPLATE (thunk
) = 0;
134 DECL_TEMPLATE_INFO (thunk
) = NULL
;
136 /* Add it to the list of thunks associated with FUNCTION. */
137 DECL_CHAIN (thunk
) = DECL_THUNKS (function
);
138 SET_DECL_THUNKS (function
, thunk
);
143 /* Finish THUNK, a thunk decl. */
146 finish_thunk (tree thunk
)
149 tree fixed_offset
= ssize_int (THUNK_FIXED_OFFSET (thunk
));
150 tree virtual_offset
= THUNK_VIRTUAL_OFFSET (thunk
);
152 gcc_assert (!DECL_NAME (thunk
) && DECL_THUNK_P (thunk
));
153 if (virtual_offset
&& DECL_RESULT_THUNK_P (thunk
))
154 virtual_offset
= BINFO_VPTR_FIELD (virtual_offset
);
155 function
= THUNK_TARGET (thunk
);
156 name
= mangle_thunk (function
, DECL_THIS_THUNK_P (thunk
),
157 fixed_offset
, virtual_offset
, thunk
);
159 /* We can end up with declarations of (logically) different
160 covariant thunks, that do identical adjustments. The two thunks
161 will be adjusting between within different hierarchies, which
162 happen to have the same layout. We must nullify one of them to
163 refer to the other. */
164 if (DECL_RESULT_THUNK_P (thunk
))
168 for (cov_probe
= DECL_THUNKS (function
);
169 cov_probe
; cov_probe
= DECL_CHAIN (cov_probe
))
170 if (DECL_NAME (cov_probe
) == name
)
172 gcc_assert (!DECL_THUNKS (thunk
));
173 THUNK_ALIAS (thunk
) = (THUNK_ALIAS (cov_probe
)
174 ? THUNK_ALIAS (cov_probe
) : cov_probe
);
179 DECL_NAME (thunk
) = name
;
180 SET_DECL_ASSEMBLER_NAME (thunk
, name
);
183 static GTY (()) int thunk_labelno
;
185 /* Create a static alias to target. */
188 make_alias_for (tree target
, tree newid
)
190 tree alias
= build_decl (DECL_SOURCE_LOCATION (target
),
191 TREE_CODE (target
), newid
, TREE_TYPE (target
));
192 DECL_LANG_SPECIFIC (alias
) = DECL_LANG_SPECIFIC (target
);
193 cxx_dup_lang_specific_decl (alias
);
194 DECL_CONTEXT (alias
) = DECL_CONTEXT (target
);
195 TREE_READONLY (alias
) = TREE_READONLY (target
);
196 TREE_THIS_VOLATILE (alias
) = TREE_THIS_VOLATILE (target
);
197 TREE_PUBLIC (alias
) = 0;
198 DECL_INTERFACE_KNOWN (alias
) = 1;
199 if (DECL_LANG_SPECIFIC (alias
))
201 DECL_NOT_REALLY_EXTERN (alias
) = 1;
202 DECL_USE_TEMPLATE (alias
) = 0;
203 DECL_TEMPLATE_INFO (alias
) = NULL
;
205 DECL_EXTERNAL (alias
) = 0;
206 DECL_ARTIFICIAL (alias
) = 1;
207 DECL_TEMPLATE_INSTANTIATED (alias
) = 0;
208 if (TREE_CODE (alias
) == FUNCTION_DECL
)
210 DECL_SAVED_AUTO_RETURN_TYPE (alias
) = NULL
;
211 DECL_CXX_DESTRUCTOR_P (alias
) = 0;
212 DECL_CXX_CONSTRUCTOR_P (alias
) = 0;
213 DECL_PENDING_INLINE_P (alias
) = 0;
214 DECL_DECLARED_INLINE_P (alias
) = 0;
215 DECL_INITIAL (alias
) = error_mark_node
;
216 DECL_ARGUMENTS (alias
) = copy_list (DECL_ARGUMENTS (target
));
219 TREE_STATIC (alias
) = 1;
220 TREE_ADDRESSABLE (alias
) = 1;
221 TREE_USED (alias
) = 1;
222 SET_DECL_ASSEMBLER_NAME (alias
, DECL_NAME (alias
));
227 make_alias_for_thunk (tree function
)
232 targetm
.asm_out
.generate_internal_label (buf
, "LTHUNK", thunk_labelno
);
235 alias
= make_alias_for (function
, get_identifier (buf
));
237 if (!flag_syntax_only
)
239 struct cgraph_node
*funcn
, *aliasn
;
240 funcn
= cgraph_node::get (function
);
241 gcc_checking_assert (funcn
);
242 aliasn
= cgraph_node::create_same_body_alias (alias
, function
);
243 DECL_ASSEMBLER_NAME (function
);
244 gcc_assert (aliasn
!= NULL
);
250 /* Emit the definition of a C++ multiple inheritance or covariant
251 return vtable thunk. If EMIT_P is nonzero, the thunk is emitted
255 use_thunk (tree thunk_fndecl
, bool emit_p
)
257 tree a
, t
, function
, alias
;
259 HOST_WIDE_INT fixed_offset
, virtual_value
;
260 bool this_adjusting
= DECL_THIS_THUNK_P (thunk_fndecl
);
261 struct cgraph_node
*funcn
, *thunk_node
;
263 /* We should have called finish_thunk to give it a name. */
264 gcc_assert (DECL_NAME (thunk_fndecl
));
266 /* We should never be using an alias, always refer to the
268 gcc_assert (!THUNK_ALIAS (thunk_fndecl
));
270 if (TREE_ASM_WRITTEN (thunk_fndecl
))
273 function
= THUNK_TARGET (thunk_fndecl
);
274 if (DECL_RESULT (thunk_fndecl
))
275 /* We already turned this thunk into an ordinary function.
276 There's no need to process this thunk again. */
279 if (DECL_THUNK_P (function
))
280 /* The target is itself a thunk, process it now. */
281 use_thunk (function
, emit_p
);
283 /* Thunks are always addressable; they only appear in vtables. */
284 TREE_ADDRESSABLE (thunk_fndecl
) = 1;
286 /* Figure out what function is being thunked to. It's referenced in
287 this translation unit. */
288 TREE_ADDRESSABLE (function
) = 1;
289 mark_used (function
);
293 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function
))
294 alias
= make_alias_for_thunk (function
);
298 fixed_offset
= THUNK_FIXED_OFFSET (thunk_fndecl
);
299 virtual_offset
= THUNK_VIRTUAL_OFFSET (thunk_fndecl
);
304 virtual_offset
= BINFO_VPTR_FIELD (virtual_offset
);
305 virtual_value
= tree_to_shwi (virtual_offset
);
306 gcc_assert (virtual_value
);
311 /* And, if we need to emit the thunk, it's used. */
312 mark_used (thunk_fndecl
);
313 /* This thunk is actually defined. */
314 DECL_EXTERNAL (thunk_fndecl
) = 0;
315 /* The linkage of the function may have changed. FIXME in linkage
317 gcc_assert (DECL_INTERFACE_KNOWN (function
));
318 TREE_PUBLIC (thunk_fndecl
) = TREE_PUBLIC (function
);
319 DECL_VISIBILITY (thunk_fndecl
) = DECL_VISIBILITY (function
);
320 DECL_VISIBILITY_SPECIFIED (thunk_fndecl
)
321 = DECL_VISIBILITY_SPECIFIED (function
);
322 DECL_COMDAT (thunk_fndecl
) = DECL_COMDAT (function
);
323 DECL_WEAK (thunk_fndecl
) = DECL_WEAK (function
);
325 if (flag_syntax_only
)
327 TREE_ASM_WRITTEN (thunk_fndecl
) = 1;
331 push_to_top_level ();
333 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function
)
334 && targetm_common
.have_named_sections
)
337 struct symtab_node
*symbol
;
339 if ((symbol
= symtab_node::get (function
))
342 if (symbol
->analyzed
)
343 fn
= symtab_node::get (function
)->ultimate_alias_target ()->decl
;
345 fn
= symtab_node::get (function
)->alias_target
;
347 resolve_unique_section (fn
, 0, flag_function_sections
);
349 if (DECL_SECTION_NAME (fn
) != NULL
&& DECL_ONE_ONLY (fn
))
351 resolve_unique_section (thunk_fndecl
, 0, flag_function_sections
);
353 /* Output the thunk into the same section as function. */
354 set_decl_section_name (thunk_fndecl
, DECL_SECTION_NAME (fn
));
355 symtab_node::get (thunk_fndecl
)->implicit_section
356 = symtab_node::get (fn
)->implicit_section
;
360 /* Set up cloned argument trees for the thunk. */
362 for (a
= DECL_ARGUMENTS (function
); a
; a
= DECL_CHAIN (a
))
364 tree x
= copy_node (a
);
366 DECL_CONTEXT (x
) = thunk_fndecl
;
367 SET_DECL_RTL (x
, NULL
);
368 DECL_HAS_VALUE_EXPR_P (x
) = 0;
369 TREE_ADDRESSABLE (x
) = 0;
373 DECL_ARGUMENTS (thunk_fndecl
) = a
;
374 TREE_ASM_WRITTEN (thunk_fndecl
) = 1;
375 funcn
= cgraph_node::get (function
);
376 gcc_checking_assert (funcn
);
377 thunk_node
= funcn
->create_thunk (thunk_fndecl
, function
,
378 this_adjusting
, fixed_offset
, virtual_value
,
379 0, virtual_offset
, alias
);
380 if (DECL_ONE_ONLY (function
))
381 thunk_node
->add_to_same_comdat_group (funcn
);
383 pop_from_top_level ();
386 /* Code for synthesizing methods which have default semantics defined. */
388 /* True iff CTYPE has a trivial SFK. */
391 type_has_trivial_fn (tree ctype
, special_function_kind sfk
)
395 case sfk_constructor
:
396 return !TYPE_HAS_COMPLEX_DFLT (ctype
);
397 case sfk_copy_constructor
:
398 return !TYPE_HAS_COMPLEX_COPY_CTOR (ctype
);
399 case sfk_move_constructor
:
400 return !TYPE_HAS_COMPLEX_MOVE_CTOR (ctype
);
401 case sfk_copy_assignment
:
402 return !TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype
);
403 case sfk_move_assignment
:
404 return !TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype
);
406 case sfk_virtual_destructor
:
407 return !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype
);
408 case sfk_inheriting_constructor
:
416 /* Note that CTYPE has a non-trivial SFK even though we previously thought
420 type_set_nontrivial_flag (tree ctype
, special_function_kind sfk
)
424 case sfk_constructor
:
425 TYPE_HAS_COMPLEX_DFLT (ctype
) = true;
427 case sfk_copy_constructor
:
428 TYPE_HAS_COMPLEX_COPY_CTOR (ctype
) = true;
430 case sfk_move_constructor
:
431 TYPE_HAS_COMPLEX_MOVE_CTOR (ctype
) = true;
433 case sfk_copy_assignment
:
434 TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype
) = true;
436 case sfk_move_assignment
:
437 TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype
) = true;
440 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype
) = true;
442 case sfk_inheriting_constructor
:
448 /* True iff FN is a trivial defaulted member function ([cd]tor, op=). */
451 trivial_fn_p (tree fn
)
453 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
455 if (!DECL_DEFAULTED_FN (fn
))
458 /* If fn is a clone, get the primary variant. */
459 if (tree prim
= DECL_CLONED_FUNCTION (fn
))
461 return type_has_trivial_fn (DECL_CONTEXT (fn
), special_function_p (fn
));
464 /* PARM is a PARM_DECL for a function which we want to forward to another
465 function without changing its value category, a la std::forward. */
468 forward_parm (tree parm
)
470 tree exp
= convert_from_reference (parm
);
471 tree type
= TREE_TYPE (parm
);
472 if (DECL_PACK_P (parm
))
473 type
= PACK_EXPANSION_PATTERN (type
);
474 if (!TYPE_REF_P (type
))
475 type
= cp_build_reference_type (type
, /*rval=*/true);
476 warning_sentinel
w (warn_useless_cast
);
477 exp
= build_static_cast (input_location
, type
, exp
,
478 tf_warning_or_error
);
479 if (DECL_PACK_P (parm
))
480 exp
= make_pack_expansion (exp
);
484 /* Strip all inheriting constructors, if any, to return the original
485 constructor from a (possibly indirect) base class. */
488 strip_inheriting_ctors (tree dfn
)
490 if (!flag_new_inheriting_ctors
)
493 while (tree inh
= DECL_INHERITED_CTOR (fn
))
494 fn
= OVL_FIRST (inh
);
496 if (TREE_CODE (fn
) == TEMPLATE_DECL
497 && TREE_CODE (dfn
) == FUNCTION_DECL
)
498 fn
= DECL_TEMPLATE_RESULT (fn
);
502 /* Find the binfo for the base subobject of BINFO being initialized by
503 inherited constructor FNDECL (a member of a direct base of BINFO). */
505 static tree
inherited_ctor_binfo (tree
, tree
);
507 inherited_ctor_binfo_1 (tree binfo
, tree fndecl
)
509 tree base
= DECL_CONTEXT (fndecl
);
511 for (int i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
512 if (BINFO_TYPE (base_binfo
) == base
)
513 return inherited_ctor_binfo (base_binfo
, fndecl
);
518 /* Find the binfo for the base subobject of BINFO being initialized by
519 inheriting constructor FNDECL (a member of BINFO), or BINFO if FNDECL is not
520 an inheriting constructor. */
523 inherited_ctor_binfo (tree binfo
, tree fndecl
)
525 tree inh
= DECL_INHERITED_CTOR (fndecl
);
529 tree results
= NULL_TREE
;
530 for (ovl_iterator
iter (inh
); iter
; ++iter
)
532 tree one
= inherited_ctor_binfo_1 (binfo
, *iter
);
535 else if (one
!= results
)
536 results
= tree_cons (NULL_TREE
, one
, results
);
541 /* Find the binfo for the base subobject being initialized by inheriting
542 constructor FNDECL, or NULL_TREE if FNDECL is not an inheriting
546 inherited_ctor_binfo (tree fndecl
)
548 if (!DECL_INHERITED_CTOR (fndecl
))
550 tree binfo
= TYPE_BINFO (DECL_CONTEXT (fndecl
));
551 return inherited_ctor_binfo (binfo
, fndecl
);
554 /* True if we should omit all user-declared parameters from constructor FN,
555 because it is a base clone of a ctor inherited from a virtual base. */
558 ctor_omit_inherited_parms (tree fn
)
560 if (!flag_new_inheriting_ctors
)
561 /* We only optimize away the parameters in the new model. */
563 if (!DECL_BASE_CONSTRUCTOR_P (fn
)
564 || !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn
)))
567 if (FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn
)) == void_list_node
)
568 /* No user-declared parameters to omit. */
571 tree binfo
= inherited_ctor_binfo (fn
);
572 for (; binfo
; binfo
= BINFO_INHERITANCE_CHAIN (binfo
))
573 if (BINFO_VIRTUAL_P (binfo
))
579 /* True iff constructor(s) INH inherited into BINFO initializes INIT_BINFO.
580 This can be true for multiple virtual bases as well as one direct
584 binfo_inherited_from (tree binfo
, tree init_binfo
, tree inh
)
586 /* inh is an OVERLOAD if we inherited the same constructor along
587 multiple paths, check all of them. */
588 for (ovl_iterator
iter (inh
); iter
; ++iter
)
591 tree base
= DECL_CONTEXT (fn
);
592 tree base_binfo
= NULL_TREE
;
593 for (int i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
594 if (BINFO_TYPE (base_binfo
) == base
)
596 if (base_binfo
== init_binfo
597 || (flag_new_inheriting_ctors
598 && binfo_inherited_from (base_binfo
, init_binfo
,
599 DECL_INHERITED_CTOR (fn
))))
605 /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO
606 given the parameter or parameters PARM, possibly inherited constructor
607 base INH, or move flag MOVE_P. */
610 add_one_base_init (tree binfo
, tree parm
, bool move_p
, tree inh
,
611 tree member_init_list
)
616 /* An inheriting constructor only has a mem-initializer for
617 the base it inherits from. */
618 if (!binfo_inherited_from (TYPE_BINFO (current_class_type
), binfo
, inh
))
619 return member_init_list
;
623 for (; parm
; parm
= DECL_CHAIN (parm
))
625 tree exp
= forward_parm (parm
);
626 *p
= build_tree_list (NULL_TREE
, exp
);
627 p
= &TREE_CHAIN (*p
);
632 init
= build_base_path (PLUS_EXPR
, parm
, binfo
, 1,
633 tf_warning_or_error
);
636 init
= build_tree_list (NULL_TREE
, init
);
638 return tree_cons (binfo
, init
, member_init_list
);
641 /* Generate code for default X(X&) or X(X&&) constructor or an inheriting
645 do_build_copy_constructor (tree fndecl
)
647 tree parm
= FUNCTION_FIRST_USER_PARM (fndecl
);
648 bool move_p
= DECL_MOVE_CONSTRUCTOR_P (fndecl
);
649 bool trivial
= trivial_fn_p (fndecl
);
650 tree inh
= DECL_INHERITED_CTOR (fndecl
);
653 parm
= convert_from_reference (parm
);
657 if (is_empty_class (current_class_type
))
658 /* Don't copy the padding byte; it might not have been allocated
659 if *this is a base subobject. */;
660 else if (tree_int_cst_equal (TYPE_SIZE (current_class_type
),
661 CLASSTYPE_SIZE (current_class_type
)))
663 tree t
= build2 (INIT_EXPR
, void_type_node
, current_class_ref
, parm
);
664 finish_expr_stmt (t
);
668 /* We must only copy the non-tail padding parts. */
669 tree base_size
= CLASSTYPE_SIZE_UNIT (current_class_type
);
670 base_size
= size_binop (MINUS_EXPR
, base_size
, size_int (1));
671 tree array_type
= build_array_type (unsigned_char_type_node
,
672 build_index_type (base_size
));
673 tree alias_set
= build_int_cst (TREE_TYPE (current_class_ptr
), 0);
674 tree lhs
= build2 (MEM_REF
, array_type
,
675 current_class_ptr
, alias_set
);
676 tree rhs
= build2 (MEM_REF
, array_type
,
677 TREE_OPERAND (parm
, 0), alias_set
);
678 tree t
= build2 (INIT_EXPR
, void_type_node
, lhs
, rhs
);
679 finish_expr_stmt (t
);
684 tree member_init_list
= NULL_TREE
;
686 tree binfo
, base_binfo
;
687 vec
<tree
, va_gc
> *vbases
;
689 /* Initialize all the base-classes with the parameter converted
690 to their type so that we get their copy constructor and not
691 another constructor that takes current_class_type. We must
692 deal with the binfo's directly as a direct base might be
693 inaccessible due to ambiguity. */
694 for (vbases
= CLASSTYPE_VBASECLASSES (current_class_type
), i
= 0;
695 vec_safe_iterate (vbases
, i
, &binfo
); i
++)
697 member_init_list
= add_one_base_init (binfo
, parm
, move_p
, inh
,
701 for (binfo
= TYPE_BINFO (current_class_type
), i
= 0;
702 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
704 if (BINFO_VIRTUAL_P (base_binfo
))
706 member_init_list
= add_one_base_init (base_binfo
, parm
, move_p
,
707 inh
, member_init_list
);
712 int cvquals
= cp_type_quals (TREE_TYPE (parm
));
714 for (tree fields
= TYPE_FIELDS (current_class_type
);
715 fields
; fields
= DECL_CHAIN (fields
))
720 if (TREE_CODE (field
) != FIELD_DECL
)
723 expr_type
= TREE_TYPE (field
);
724 if (DECL_NAME (field
))
726 if (VFIELD_NAME_P (DECL_NAME (field
)))
729 else if (ANON_AGGR_TYPE_P (expr_type
) && TYPE_FIELDS (expr_type
))
730 /* Just use the field; anonymous types can't have
731 nontrivial copy ctors or assignment ops or this
732 function would be deleted. */;
736 /* Compute the type of "init->field". If the copy-constructor
737 parameter is, for example, "const S&", and the type of
738 the field is "T", then the type will usually be "const
739 T". (There are no cv-qualified variants of reference
741 if (!TYPE_REF_P (expr_type
))
745 if (DECL_MUTABLE_P (field
))
746 quals
&= ~TYPE_QUAL_CONST
;
747 quals
|= cp_type_quals (expr_type
);
748 expr_type
= cp_build_qualified_type (expr_type
, quals
);
751 tree init
= build3 (COMPONENT_REF
, expr_type
, parm
, field
, NULL_TREE
);
752 if (move_p
&& !TYPE_REF_P (expr_type
)
753 /* 'move' breaks bit-fields, and has no effect for scalars. */
754 && !scalarish_type_p (expr_type
))
756 init
= build_tree_list (NULL_TREE
, init
);
758 member_init_list
= tree_cons (field
, init
, member_init_list
);
762 finish_mem_initializers (member_init_list
);
767 do_build_copy_assign (tree fndecl
)
769 tree parm
= DECL_CHAIN (DECL_ARGUMENTS (fndecl
));
771 bool move_p
= move_fn_p (fndecl
);
772 bool trivial
= trivial_fn_p (fndecl
);
773 int flags
= LOOKUP_NORMAL
| LOOKUP_NONVIRTUAL
| LOOKUP_DEFAULTED
;
775 compound_stmt
= begin_compound_stmt (0);
776 parm
= convert_from_reference (parm
);
779 && is_empty_class (current_class_type
))
780 /* Don't copy the padding byte; it might not have been allocated
781 if *this is a base subobject. */;
784 tree t
= build2 (MODIFY_EXPR
, void_type_node
, current_class_ref
, parm
);
785 finish_expr_stmt (t
);
790 int cvquals
= cp_type_quals (TREE_TYPE (parm
));
792 tree binfo
, base_binfo
;
794 /* Assign to each of the direct base classes. */
795 for (binfo
= TYPE_BINFO (current_class_type
), i
= 0;
796 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
800 /* We must convert PARM directly to the base class
801 explicitly since the base class may be ambiguous. */
802 converted_parm
= build_base_path (PLUS_EXPR
, parm
, base_binfo
, 1,
803 tf_warning_or_error
);
805 converted_parm
= move (converted_parm
);
806 /* Call the base class assignment operator. */
807 releasing_vec
parmvec (make_tree_vector_single (converted_parm
));
809 (build_special_member_call (current_class_ref
,
810 assign_op_identifier
,
814 tf_warning_or_error
));
817 /* Assign to each of the non-static data members. */
818 for (fields
= TYPE_FIELDS (current_class_type
);
820 fields
= DECL_CHAIN (fields
))
822 tree comp
= current_class_ref
;
828 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
831 expr_type
= TREE_TYPE (field
);
833 if (CP_TYPE_CONST_P (expr_type
))
835 error ("non-static const member %q#D, cannot use default "
836 "assignment operator", field
);
839 else if (TYPE_REF_P (expr_type
))
841 error ("non-static reference member %q#D, cannot use "
842 "default assignment operator", field
);
846 if (DECL_NAME (field
))
848 if (VFIELD_NAME_P (DECL_NAME (field
)))
851 else if (ANON_AGGR_TYPE_P (expr_type
)
852 && TYPE_FIELDS (expr_type
) != NULL_TREE
)
853 /* Just use the field; anonymous types can't have
854 nontrivial copy ctors or assignment ops or this
855 function would be deleted. */;
859 comp
= build3 (COMPONENT_REF
, expr_type
, comp
, field
, NULL_TREE
);
861 /* Compute the type of init->field */
863 if (DECL_MUTABLE_P (field
))
864 quals
&= ~TYPE_QUAL_CONST
;
865 expr_type
= cp_build_qualified_type (expr_type
, quals
);
867 init
= build3 (COMPONENT_REF
, expr_type
, init
, field
, NULL_TREE
);
868 if (move_p
&& !TYPE_REF_P (expr_type
)
869 /* 'move' breaks bit-fields, and has no effect for scalars. */
870 && !scalarish_type_p (expr_type
))
873 if (DECL_NAME (field
))
874 init
= cp_build_modify_expr (input_location
, comp
, NOP_EXPR
, init
,
875 tf_warning_or_error
);
877 init
= build2 (MODIFY_EXPR
, TREE_TYPE (comp
), comp
, init
);
878 finish_expr_stmt (init
);
881 finish_return_stmt (current_class_ref
);
882 finish_compound_stmt (compound_stmt
);
885 /* C++20 <compare> comparison category types. */
895 /* Names of the comparison categories and their value members, to be indexed by
896 comp_cat_tag enumerators. genericize_spaceship below relies on the ordering
899 struct comp_cat_info_t
902 const char *members
[4];
904 static const comp_cat_info_t comp_cat_info
[cc_last
]
906 { "partial_ordering", { "equivalent", "greater", "less", "unordered" } },
907 { "weak_ordering", { "equivalent", "greater", "less" } },
908 { "strong_ordering", { "equal", "greater", "less" } }
911 /* A cache of the category types to speed repeated lookups. */
913 static GTY((deletable
)) tree comp_cat_cache
[cc_last
];
915 /* Look up one of the result variables in the comparison category type. */
918 lookup_comparison_result (tree type
, const char *name_str
,
919 tsubst_flags_t complain
= tf_warning_or_error
)
921 tree name
= get_identifier (name_str
);
922 tree decl
= lookup_qualified_name (type
, name
);
923 if (TREE_CODE (decl
) != VAR_DECL
)
925 if (complain
& tf_error
)
927 auto_diagnostic_group d
;
928 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
929 qualified_name_lookup_error (type
, name
, decl
, input_location
);
931 error ("%qD is not a static data member", decl
);
932 inform (input_location
, "determining value of %qs", "operator<=>");
934 return error_mark_node
;
939 /* Look up a <compare> comparison category type in std. */
942 lookup_comparison_category (comp_cat_tag tag
,
943 tsubst_flags_t complain
= tf_warning_or_error
)
945 if (tree cached
= comp_cat_cache
[tag
])
948 tree name
= get_identifier (comp_cat_info
[tag
].name
);
949 tree decl
= lookup_qualified_name (std_node
, name
);
950 if (TREE_CODE (decl
) != TYPE_DECL
)
952 if (complain
& tf_error
)
954 auto_diagnostic_group d
;
955 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
956 qualified_name_lookup_error (std_node
, name
, decl
, input_location
);
958 error ("%qD is not a type", decl
);
959 inform (input_location
, "forming type of %qs", "operator<=>");
961 return error_mark_node
;
963 /* Also make sure we can look up the value members now, since we won't
964 really use them until genericize time. */
965 tree type
= TREE_TYPE (decl
);
966 for (int i
= 0; i
< 4; ++i
)
968 const char *p
= comp_cat_info
[tag
].members
[i
];
970 if (lookup_comparison_result (type
, p
, complain
)
972 return error_mark_node
;
974 return comp_cat_cache
[tag
] = type
;
977 /* Wrapper that takes the tag rather than the type. */
980 lookup_comparison_result (comp_cat_tag tag
, const char *name_str
,
981 tsubst_flags_t complain
= tf_warning_or_error
)
983 tree type
= lookup_comparison_category (tag
, complain
);
984 return lookup_comparison_result (type
, name_str
, complain
);
987 /* Wrapper that takes the index into the members array instead of the name. */
990 lookup_comparison_result (comp_cat_tag tag
, tree type
, int idx
)
992 const char *name_str
= comp_cat_info
[tag
].members
[idx
];
995 return lookup_comparison_result (type
, name_str
);
998 /* Does TYPE correspond to TAG? */
1001 is_cat (tree type
, comp_cat_tag tag
)
1003 tree name
= TYPE_LINKAGE_IDENTIFIER (type
);
1004 return id_equal (name
, comp_cat_info
[tag
].name
);
1007 /* Return the comp_cat_tag for TYPE. */
1010 cat_tag_for (tree type
)
1012 for (int i
= 0; i
< cc_last
; ++i
)
1014 comp_cat_tag tag
= (comp_cat_tag
)i
;
1015 if (is_cat (type
, tag
))
1021 /* Return the comparison category tag of a <=> expression with non-class type
1025 spaceship_comp_cat (tree optype
)
1027 if (INTEGRAL_OR_ENUMERATION_TYPE_P (optype
) || TYPE_PTROBV_P (optype
))
1028 return cc_strong_ordering
;
1029 else if (TREE_CODE (optype
) == REAL_TYPE
)
1030 return cc_partial_ordering
;
1032 /* ??? should vector <=> produce a vector of one of the above? */
1036 /* Return the comparison category type of a <=> expression with non-class type
1040 spaceship_type (tree optype
, tsubst_flags_t complain
)
1042 comp_cat_tag tag
= spaceship_comp_cat (optype
);
1043 return lookup_comparison_category (tag
, complain
);
1046 /* Turn <=> with type TYPE and operands OP0 and OP1 into GENERIC. */
1049 genericize_spaceship (tree type
, tree op0
, tree op1
)
1051 /* ??? maybe optimize based on knowledge of representation? */
1052 comp_cat_tag tag
= cat_tag_for (type
);
1053 gcc_checking_assert (tag
< cc_last
);
1056 op0
= save_expr (op0
);
1057 op1
= save_expr (op1
);
1059 tree gt
= lookup_comparison_result (tag
, type
, 1);
1061 if (tag
== cc_partial_ordering
)
1063 /* op0 == op1 ? equivalent : op0 < op1 ? less :
1064 op0 > op1 ? greater : unordered */
1065 tree uo
= lookup_comparison_result (tag
, type
, 3);
1066 tree comp
= fold_build2 (GT_EXPR
, boolean_type_node
, op0
, op1
);
1067 r
= fold_build3 (COND_EXPR
, type
, comp
, gt
, uo
);
1070 /* op0 == op1 ? equal : op0 < op1 ? less : greater */
1073 tree lt
= lookup_comparison_result (tag
, type
, 2);
1074 tree comp
= fold_build2 (LT_EXPR
, boolean_type_node
, op0
, op1
);
1075 r
= fold_build3 (COND_EXPR
, type
, comp
, lt
, r
);
1077 tree eq
= lookup_comparison_result (tag
, type
, 0);
1078 comp
= fold_build2 (EQ_EXPR
, boolean_type_node
, op0
, op1
);
1079 r
= fold_build3 (COND_EXPR
, type
, comp
, eq
, r
);
1081 /* Wrap the whole thing in a TARGET_EXPR like build_conditional_expr_1. */
1082 r
= get_target_expr (r
);
1087 /* Check that the signature of a defaulted comparison operator is
1091 early_check_defaulted_comparison (tree fn
)
1093 location_t loc
= DECL_SOURCE_LOCATION (fn
);
1095 if (DECL_CLASS_SCOPE_P (fn
))
1096 ctx
= DECL_CONTEXT (fn
);
1098 ctx
= DECL_FRIEND_CONTEXT (fn
);
1101 if (cxx_dialect
< cxx20
)
1103 error_at (loc
, "defaulted %qD only available with %<-std=c++20%> or "
1104 "%<-std=gnu++20%>", fn
);
1108 if (!DECL_OVERLOADED_OPERATOR_IS (fn
, SPACESHIP_EXPR
)
1109 && !same_type_p (TREE_TYPE (TREE_TYPE (fn
)), boolean_type_node
))
1111 diagnostic_t kind
= DK_UNSPECIFIED
;
1113 if (is_auto (TREE_TYPE (fn
)))
1117 emit_diagnostic (kind
, loc
, opt
,
1118 "defaulted %qD must return %<bool%>", fn
);
1119 if (kind
== DK_ERROR
)
1123 bool mem
= DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
);
1124 if (mem
&& type_memfn_quals (TREE_TYPE (fn
)) != TYPE_QUAL_CONST
)
1126 error_at (loc
, "defaulted %qD must be %<const%>", fn
);
1129 if (mem
&& type_memfn_rqual (TREE_TYPE (fn
)) == REF_QUAL_RVALUE
)
1131 error_at (loc
, "defaulted %qD must not have %<&&%> ref-qualifier", fn
);
1134 tree parmnode
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
1135 bool saw_byval
= false;
1136 bool saw_byref
= mem
;
1137 bool saw_bad
= false;
1138 for (; parmnode
!= void_list_node
; parmnode
= TREE_CHAIN (parmnode
))
1140 tree parmtype
= TREE_VALUE (parmnode
);
1141 if (CLASS_TYPE_P (parmtype
))
1143 else if (TREE_CODE (parmtype
) == REFERENCE_TYPE
1144 && !TYPE_REF_IS_RVALUE (parmtype
)
1145 && TYPE_QUALS (TREE_TYPE (parmtype
)) == TYPE_QUAL_CONST
)
1148 parmtype
= TREE_TYPE (parmtype
);
1153 if (!saw_bad
&& !ctx
)
1155 /* Defaulted outside the class body. */
1156 ctx
= TYPE_MAIN_VARIANT (parmtype
);
1157 if (!is_friend (ctx
, fn
))
1158 error_at (loc
, "defaulted %qD is not a friend of %qT", fn
, ctx
);
1160 else if (!same_type_ignoring_top_level_qualifiers_p (parmtype
, ctx
))
1164 if (saw_bad
|| (saw_byval
&& saw_byref
))
1166 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
1167 error_at (loc
, "defaulted member %qD must have parameter type "
1168 "%<const %T&%>", fn
, ctx
);
1170 error_at (loc
, "defaulted %qD must have parameters of either type "
1171 "%<const %T&%> or %qT", fn
, ctx
, ctx
);
1173 error_at (loc
, "defaulted %qD must have parameters of either type "
1174 "%<const %T&%> or %qT, not both", fn
, ctx
, ctx
);
1178 /* We still need to deduce deleted/constexpr/noexcept and maybe return. */
1179 DECL_MAYBE_DELETED (fn
) = ok
;
1184 /* Subroutine of build_comparison_op. Given the vec of memberwise
1185 comparisons COMPS, calculate the overall comparison category for
1189 common_comparison_type (vec
<tree
> &comps
)
1191 tree seen
[cc_last
] = {};
1193 for (unsigned i
= 0; i
< comps
.length(); ++i
)
1195 tree comp
= comps
[i
];
1196 tree ctype
= TREE_TYPE (comp
);
1197 comp_cat_tag tag
= cat_tag_for (ctype
);
1198 /* build_comparison_op already checked this. */
1199 gcc_checking_assert (tag
< cc_last
);
1203 /* Otherwise, if at least one T i is std::partial_ordering, U is
1204 std::partial_ordering. */
1205 if (tree t
= seen
[cc_partial_ordering
]) return t
;
1207 /* Otherwise, if at least one T i is std::weak_ordering, U is
1208 std::weak_ordering. */
1209 if (tree t
= seen
[cc_weak_ordering
]) return t
;
1211 /* Otherwise, U is std::strong_ordering. */
1212 if (tree t
= seen
[cc_strong_ordering
]) return t
;
1213 return lookup_comparison_category (cc_strong_ordering
);
1216 /* Data structure for build_comparison_op. */
1228 comp_info (tree fndecl
, tsubst_flags_t
&complain
)
1231 loc
= DECL_SOURCE_LOCATION (fndecl
);
1233 /* We only have tf_error set when we're called from
1234 explain_invalid_constexpr_fn or maybe_explain_implicit_delete. */
1235 defining
= !(complain
& tf_error
);
1237 first_time
= DECL_MAYBE_DELETED (fndecl
);
1238 DECL_MAYBE_DELETED (fndecl
) = false;
1240 /* Do we want to try to set constexpr? */
1241 was_constexp
= DECL_DECLARED_CONSTEXPR_P (fndecl
);
1242 constexp
= first_time
;
1244 /* Set this for var_in_constexpr_fn. */
1245 DECL_DECLARED_CONSTEXPR_P (fndecl
) = true;
1247 /* Do we want to try to set noexcept? */
1251 tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fndecl
));
1252 if (raises
&& !UNEVALUATED_NOEXCEPT_SPEC_P (raises
))
1253 /* There was an explicit exception-specification. */
1258 /* EXPR is an expression built as part of the function body.
1259 Adjust the properties appropriately. */
1260 void check (tree expr
)
1262 if (expr
== error_mark_node
)
1263 DECL_DELETED_FN (fndecl
) = true;
1264 if ((constexp
|| was_constexp
)
1265 && !potential_rvalue_constant_expression (expr
))
1268 require_potential_rvalue_constant_expression (expr
);
1272 if (noex
&& !expr_noexcept_p (expr
, tf_none
))
1280 DECL_DECLARED_CONSTEXPR_P (fndecl
) = constexp
|| was_constexp
;
1281 tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fndecl
));
1282 if (!raises
|| UNEVALUATED_NOEXCEPT_SPEC_P (raises
))
1284 raises
= noex
? noexcept_true_spec
: noexcept_false_spec
;
1285 TREE_TYPE (fndecl
) = build_exception_variant (TREE_TYPE (fndecl
),
1292 /* Build up the definition of a defaulted comparison operator. Unlike other
1293 defaulted functions that use synthesized_method_walk to determine whether
1294 the function is e.g. deleted, for comparisons we use the same code. We try
1295 to use synthesize_method at the earliest opportunity and bail out if the
1296 function ends up being deleted. */
1299 build_comparison_op (tree fndecl
, tsubst_flags_t complain
)
1301 comp_info
info (fndecl
, complain
);
1303 if (!info
.defining
&& !(complain
& tf_error
) && !DECL_MAYBE_DELETED (fndecl
))
1306 int flags
= LOOKUP_NORMAL
| LOOKUP_NONVIRTUAL
| LOOKUP_DEFAULTED
;
1307 const ovl_op_info_t
*op
= IDENTIFIER_OVL_OP_INFO (DECL_NAME (fndecl
));
1308 tree_code code
= op
->tree_code
;
1310 tree lhs
= DECL_ARGUMENTS (fndecl
);
1311 tree rhs
= DECL_CHAIN (lhs
);
1312 if (is_this_parameter (lhs
))
1313 lhs
= cp_build_fold_indirect_ref (lhs
);
1315 lhs
= convert_from_reference (lhs
);
1316 rhs
= convert_from_reference (rhs
);
1317 tree ctype
= TYPE_MAIN_VARIANT (TREE_TYPE (lhs
));
1319 iloc_sentinel
ils (info
.loc
);
1321 /* A defaulted comparison operator function for class C is defined as
1322 deleted if ... C has variant members. */
1323 if (TREE_CODE (ctype
) == UNION_TYPE
1324 && next_initializable_field (TYPE_FIELDS (ctype
)))
1326 if (complain
& tf_error
)
1327 inform (info
.loc
, "cannot default compare union %qT", ctype
);
1328 DECL_DELETED_FN (fndecl
) = true;
1332 tree compound_stmt
= NULL_TREE
;
1334 compound_stmt
= begin_compound_stmt (0);
1336 ++cp_unevaluated_operand
;
1338 tree rettype
= TREE_TYPE (TREE_TYPE (fndecl
));
1339 if (code
!= SPACESHIP_EXPR
&& is_auto (rettype
))
1341 rettype
= boolean_type_node
;
1342 apply_deduced_return_type (fndecl
, rettype
);
1345 if (code
== EQ_EXPR
|| code
== SPACESHIP_EXPR
)
1348 auto_vec
<tree
> comps
;
1350 /* Compare each of the subobjects. Note that we get bases from
1351 next_initializable_field because we're past C++17. */
1352 for (tree field
= next_initializable_field (TYPE_FIELDS (ctype
));
1354 field
= next_initializable_field (DECL_CHAIN (field
)))
1356 tree expr_type
= TREE_TYPE (field
);
1358 /* A defaulted comparison operator function for class C is defined as
1359 deleted if any non-static data member of C is of reference type or
1360 C has variant members. */
1361 if (TREE_CODE (expr_type
) == REFERENCE_TYPE
)
1363 if (complain
& tf_error
)
1364 inform (DECL_SOURCE_LOCATION (field
), "cannot default compare "
1365 "reference member %qD", field
);
1369 else if (ANON_UNION_TYPE_P (expr_type
)
1370 && next_initializable_field (TYPE_FIELDS (expr_type
)))
1372 if (complain
& tf_error
)
1373 inform (DECL_SOURCE_LOCATION (field
), "cannot default compare "
1374 "anonymous union member");
1379 tree lhs_mem
= build3 (COMPONENT_REF
, expr_type
, lhs
, field
,
1381 tree rhs_mem
= build3 (COMPONENT_REF
, expr_type
, rhs
, field
,
1383 tree comp
= build_new_op (info
.loc
, code
, flags
, lhs_mem
, rhs_mem
,
1384 NULL_TREE
, NULL
, complain
);
1385 if (comp
== error_mark_node
)
1390 if (code
== SPACESHIP_EXPR
1391 && cat_tag_for (TREE_TYPE (comp
)) == cc_last
)
1393 /* The operator function is defined as deleted if ... Ri is not a
1394 comparison category type. */
1395 if (complain
& tf_error
)
1396 inform (DECL_SOURCE_LOCATION (field
),
1397 "three-way comparison of %qD has type %qT, not a "
1398 "comparison category type", field
, TREE_TYPE (comp
));
1402 comps
.safe_push (comp
);
1404 if (code
== SPACESHIP_EXPR
&& is_auto (rettype
))
1406 rettype
= common_comparison_type (comps
);
1407 apply_deduced_return_type (fndecl
, rettype
);
1411 DECL_DELETED_FN (fndecl
) = true;
1414 for (unsigned i
= 0; i
< comps
.length(); ++i
)
1416 tree comp
= comps
[i
];
1417 tree eq
, retval
= NULL_TREE
, if_
= NULL_TREE
;
1419 if_
= begin_if_stmt ();
1420 /* Spaceship is specified to use !=, but for the comparison category
1421 types, != is equivalent to !(==), so let's use == directly. */
1422 if (code
== EQ_EXPR
)
1424 /* if (x==y); else return false; */
1426 retval
= boolean_false_node
;
1430 /* if (auto v = x<=>y, v == 0); else return v; */
1431 if (TREE_CODE (comp
) == SPACESHIP_EXPR
)
1432 TREE_TYPE (comp
) = rettype
;
1434 comp
= build_static_cast (input_location
, rettype
, comp
,
1439 tree var
= create_temporary_var (rettype
);
1441 cp_finish_decl (var
, comp
, false, NULL_TREE
, flags
);
1442 comp
= retval
= var
;
1444 eq
= build_new_op (info
.loc
, EQ_EXPR
, flags
, comp
,
1445 integer_zero_node
, NULL_TREE
, NULL
,
1448 tree ceq
= contextual_conv_bool (eq
, complain
);
1452 finish_if_stmt_cond (ceq
, if_
);
1453 finish_then_clause (if_
);
1454 begin_else_clause (if_
);
1455 finish_return_stmt (retval
);
1456 finish_else_clause (if_
);
1457 finish_if_stmt (if_
);
1463 if (code
== EQ_EXPR
)
1464 val
= boolean_true_node
;
1467 tree seql
= lookup_comparison_result (cc_strong_ordering
,
1469 val
= build_static_cast (input_location
, rettype
, seql
,
1472 finish_return_stmt (val
);
1475 else if (code
== NE_EXPR
)
1477 tree comp
= build_new_op (info
.loc
, EQ_EXPR
, flags
, lhs
, rhs
,
1478 NULL_TREE
, NULL
, complain
);
1479 comp
= contextual_conv_bool (comp
, complain
);
1483 tree neg
= build1 (TRUTH_NOT_EXPR
, boolean_type_node
, comp
);
1484 finish_return_stmt (neg
);
1489 tree comp
= build_new_op (info
.loc
, SPACESHIP_EXPR
, flags
, lhs
, rhs
,
1490 NULL_TREE
, NULL
, complain
);
1491 tree comp2
= build_new_op (info
.loc
, code
, flags
, comp
, integer_zero_node
,
1492 NULL_TREE
, NULL
, complain
);
1495 finish_return_stmt (comp2
);
1500 finish_compound_stmt (compound_stmt
);
1502 --cp_unevaluated_operand
;
1505 /* True iff DECL is an implicitly-declared special member function with no real
1506 source location, so we can use its DECL_SOURCE_LOCATION to remember where we
1507 triggered its synthesis. */
1510 decl_remember_implicit_trigger_p (tree decl
)
1512 if (!DECL_ARTIFICIAL (decl
))
1514 special_function_kind sfk
= special_function_p (decl
);
1515 /* Inherited constructors have the location of their using-declaration, and
1516 operator== has the location of the corresponding operator<=>. */
1517 return (sfk
!= sfk_inheriting_constructor
1518 && sfk
!= sfk_comparison
);
1521 /* Synthesize FNDECL, a non-static member function. */
1524 synthesize_method (tree fndecl
)
1526 bool nested
= (current_function_decl
!= NULL_TREE
);
1527 tree context
= decl_function_context (fndecl
);
1528 bool need_body
= true;
1530 location_t save_input_location
= input_location
;
1531 int error_count
= errorcount
;
1532 int warning_count
= warningcount
+ werrorcount
;
1533 special_function_kind sfk
= special_function_p (fndecl
);
1535 /* Reset the source location, we might have been previously
1536 deferred, and thus have saved where we were first needed. */
1537 if (decl_remember_implicit_trigger_p (fndecl
))
1538 DECL_SOURCE_LOCATION (fndecl
)
1539 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl
)));
1541 /* If we've been asked to synthesize a clone, just synthesize the
1542 cloned function instead. Doing so will automatically fill in the
1543 body for the clone. */
1544 if (DECL_CLONED_FUNCTION_P (fndecl
))
1545 fndecl
= DECL_CLONED_FUNCTION (fndecl
);
1547 /* We may be in the middle of deferred access check. Disable
1549 push_deferring_access_checks (dk_no_deferred
);
1552 push_to_top_level ();
1554 push_function_context ();
1556 input_location
= DECL_SOURCE_LOCATION (fndecl
);
1558 start_preparsed_function (fndecl
, NULL_TREE
, SF_DEFAULT
| SF_PRE_PARSED
);
1559 stmt
= begin_function_body ();
1561 if (DECL_ASSIGNMENT_OPERATOR_P (fndecl
)
1562 && DECL_OVERLOADED_OPERATOR_IS (fndecl
, NOP_EXPR
))
1564 do_build_copy_assign (fndecl
);
1567 else if (DECL_CONSTRUCTOR_P (fndecl
))
1569 tree arg_chain
= FUNCTION_FIRST_USER_PARMTYPE (fndecl
);
1570 if (arg_chain
!= void_list_node
)
1571 do_build_copy_constructor (fndecl
);
1573 finish_mem_initializers (NULL_TREE
);
1575 else if (sfk
== sfk_comparison
)
1577 /* Pass tf_none so the function is just deleted if there's a problem. */
1578 build_comparison_op (fndecl
, tf_none
);
1582 /* If we haven't yet generated the body of the function, just
1583 generate an empty compound statement. */
1587 compound_stmt
= begin_compound_stmt (BCS_FN_BODY
);
1588 finish_compound_stmt (compound_stmt
);
1591 finish_function_body (stmt
);
1592 finish_function (/*inline_p=*/false);
1594 if (!DECL_DELETED_FN (fndecl
))
1595 expand_or_defer_fn (fndecl
);
1597 input_location
= save_input_location
;
1600 pop_from_top_level ();
1602 pop_function_context ();
1604 pop_deferring_access_checks ();
1606 if (error_count
!= errorcount
|| warning_count
!= warningcount
+ werrorcount
)
1607 if (DECL_ARTIFICIAL (fndecl
))
1608 inform (input_location
, "synthesized method %qD first required here",
1612 /* Build a reference to type TYPE with cv-quals QUALS, which is an
1613 rvalue if RVALUE is true. */
1616 build_stub_type (tree type
, int quals
, bool rvalue
)
1618 tree argtype
= cp_build_qualified_type (type
, quals
);
1619 return cp_build_reference_type (argtype
, rvalue
);
1622 /* Build a dummy glvalue from dereferencing a dummy reference of type
1626 build_stub_object (tree reftype
)
1628 if (!TYPE_REF_P (reftype
))
1629 reftype
= cp_build_reference_type (reftype
, /*rval*/true);
1630 tree stub
= build1 (CONVERT_EXPR
, reftype
, integer_one_node
);
1631 return convert_from_reference (stub
);
1634 /* Determine which function will be called when looking up NAME in TYPE,
1635 called with a single ARGTYPE argument, or no argument if ARGTYPE is
1636 null. FLAGS and COMPLAIN are as for build_new_method_call.
1638 Returns a FUNCTION_DECL if all is well.
1639 Returns NULL_TREE if overload resolution failed.
1640 Returns error_mark_node if the chosen function cannot be called. */
1643 locate_fn_flags (tree type
, tree name
, tree argtype
, int flags
,
1644 tsubst_flags_t complain
)
1646 tree ob
, fn
, fns
, binfo
, rval
;
1649 binfo
= TYPE_BINFO (type
);
1653 type
= BINFO_TYPE (binfo
);
1656 ob
= build_stub_object (cp_build_reference_type (type
, false));
1660 if (TREE_CODE (argtype
) == TREE_LIST
)
1662 for (tree elt
= argtype
; elt
&& elt
!= void_list_node
;
1663 elt
= TREE_CHAIN (elt
))
1665 tree type
= TREE_VALUE (elt
);
1666 tree arg
= build_stub_object (type
);
1667 vec_safe_push (args
, arg
);
1672 tree arg
= build_stub_object (argtype
);
1673 args
->quick_push (arg
);
1677 fns
= lookup_fnfields (binfo
, name
, 0, complain
);
1678 rval
= build_new_method_call (ob
, fns
, &args
, binfo
, flags
, &fn
, complain
);
1680 if (fn
&& rval
== error_mark_node
)
1686 /* Locate the dtor of TYPE. */
1689 get_dtor (tree type
, tsubst_flags_t complain
)
1691 tree fn
= locate_fn_flags (type
, complete_dtor_identifier
, NULL_TREE
,
1692 LOOKUP_NORMAL
, complain
);
1693 if (fn
== error_mark_node
)
1698 /* Locate the default ctor of TYPE. */
1701 locate_ctor (tree type
)
1705 push_deferring_access_checks (dk_no_check
);
1706 fn
= locate_fn_flags (type
, complete_ctor_identifier
, NULL_TREE
,
1707 LOOKUP_SPECULATIVE
, tf_none
);
1708 pop_deferring_access_checks ();
1709 if (fn
== error_mark_node
)
1714 /* Likewise, but give any appropriate errors. */
1717 get_default_ctor (tree type
)
1719 tree fn
= locate_fn_flags (type
, complete_ctor_identifier
, NULL_TREE
,
1720 LOOKUP_NORMAL
, tf_warning_or_error
);
1721 if (fn
== error_mark_node
)
1726 /* Locate the copy ctor of TYPE. */
1729 get_copy_ctor (tree type
, tsubst_flags_t complain
)
1731 int quals
= (TYPE_HAS_CONST_COPY_CTOR (type
)
1732 ? TYPE_QUAL_CONST
: TYPE_UNQUALIFIED
);
1733 tree argtype
= build_stub_type (type
, quals
, false);
1734 tree fn
= locate_fn_flags (type
, complete_ctor_identifier
, argtype
,
1735 LOOKUP_NORMAL
, complain
);
1736 if (fn
== error_mark_node
)
1741 /* Locate the copy assignment operator of TYPE. */
1744 get_copy_assign (tree type
)
1746 int quals
= (TYPE_HAS_CONST_COPY_ASSIGN (type
)
1747 ? TYPE_QUAL_CONST
: TYPE_UNQUALIFIED
);
1748 tree argtype
= build_stub_type (type
, quals
, false);
1749 tree fn
= locate_fn_flags (type
, assign_op_identifier
, argtype
,
1750 LOOKUP_NORMAL
, tf_warning_or_error
);
1751 if (fn
== error_mark_node
)
1756 /* walk_tree helper function for is_trivially_xible. If *TP is a call,
1757 return it if it calls something other than a trivial special member
1761 check_nontriv (tree
*tp
, int *, void *)
1763 tree fn
= cp_get_callee (*tp
);
1764 if (fn
== NULL_TREE
)
1767 if (TREE_CODE (fn
) == ADDR_EXPR
)
1768 fn
= TREE_OPERAND (fn
, 0);
1770 if (TREE_CODE (fn
) != FUNCTION_DECL
1771 || !trivial_fn_p (fn
))
1776 /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */
1779 assignable_expr (tree to
, tree from
)
1781 cp_unevaluated cp_uneval_guard
;
1782 to
= build_stub_object (to
);
1783 from
= build_stub_object (from
);
1784 tree r
= cp_build_modify_expr (input_location
, to
, NOP_EXPR
, from
, tf_none
);
1788 /* The predicate condition for a template specialization
1789 is_constructible<T, Args...> shall be satisfied if and only if the
1790 following variable definition would be well-formed for some invented
1791 variable t: T t(create<Args>()...);
1793 Return something equivalent in well-formedness and triviality. */
1796 constructible_expr (tree to
, tree from
)
1799 cp_unevaluated cp_uneval_guard
;
1800 if (CLASS_TYPE_P (to
))
1803 vec
<tree
, va_gc
> *args
= NULL
;
1804 if (!TYPE_REF_P (to
))
1805 to
= cp_build_reference_type (to
, /*rval*/false);
1806 tree ob
= build_stub_object (to
);
1807 for (; from
; from
= TREE_CHAIN (from
))
1808 vec_safe_push (args
, build_stub_object (TREE_VALUE (from
)));
1809 expr
= build_special_member_call (ob
, complete_ctor_identifier
, &args
,
1810 ctype
, LOOKUP_NORMAL
, tf_none
);
1811 if (expr
== error_mark_node
)
1812 return error_mark_node
;
1813 /* The current state of the standard vis-a-vis LWG 2116 is that
1814 is_*constructible involves destruction as well. */
1815 if (type_build_dtor_call (ctype
))
1817 tree dtor
= build_special_member_call (ob
, complete_dtor_identifier
,
1818 NULL
, ctype
, LOOKUP_NORMAL
,
1820 if (dtor
== error_mark_node
)
1821 return error_mark_node
;
1822 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype
))
1823 expr
= build2 (COMPOUND_EXPR
, void_type_node
, expr
, dtor
);
1828 if (from
== NULL_TREE
)
1829 return build_value_init (strip_array_types (to
), tf_none
);
1830 const int len
= list_length (from
);
1833 if (cxx_dialect
< cxx20
)
1834 /* Too many initializers. */
1835 return error_mark_node
;
1837 /* In C++20 this is well-formed:
1840 which means that std::is_constructible_v<int[2], int, int>
1842 vec
<constructor_elt
, va_gc
> *v
;
1844 for (tree t
= from
; t
; t
= TREE_CHAIN (t
))
1846 tree stub
= build_stub_object (TREE_VALUE (t
));
1847 constructor_elt elt
= { NULL_TREE
, stub
};
1848 v
->quick_push (elt
);
1850 from
= build_constructor (init_list_type_node
, v
);
1851 CONSTRUCTOR_IS_DIRECT_INIT (from
) = true;
1852 CONSTRUCTOR_IS_PAREN_INIT (from
) = true;
1855 from
= build_stub_object (TREE_VALUE (from
));
1856 expr
= perform_direct_initialization_if_possible (to
, from
,
1859 /* If t(e) didn't work, maybe t{e} will. */
1860 if (expr
== NULL_TREE
1862 && cxx_dialect
>= cxx20
)
1864 from
= build_constructor_single (init_list_type_node
, NULL_TREE
,
1866 CONSTRUCTOR_IS_DIRECT_INIT (from
) = true;
1867 CONSTRUCTOR_IS_PAREN_INIT (from
) = true;
1868 expr
= perform_direct_initialization_if_possible (to
, from
,
1876 /* Returns a tree iff TO is assignable (if CODE is MODIFY_EXPR) or
1877 constructible (otherwise) from FROM, which is a single type for
1878 assignment or a list of types for construction. */
1881 is_xible_helper (enum tree_code code
, tree to
, tree from
, bool trivial
)
1883 deferring_access_check_sentinel
acs (dk_no_deferred
);
1884 if (VOID_TYPE_P (to
) || ABSTRACT_CLASS_TYPE_P (to
)
1885 || (from
&& FUNC_OR_METHOD_TYPE_P (from
)
1886 && (TYPE_READONLY (from
) || FUNCTION_REF_QUALIFIED (from
))))
1887 return error_mark_node
;
1889 if (code
== MODIFY_EXPR
)
1890 expr
= assignable_expr (to
, from
);
1891 else if (trivial
&& from
&& TREE_CHAIN (from
))
1892 return error_mark_node
; // only 0- and 1-argument ctors can be trivial
1893 else if (TREE_CODE (to
) == ARRAY_TYPE
&& !TYPE_DOMAIN (to
))
1894 return error_mark_node
; // can't construct an array of unknown bound
1896 expr
= constructible_expr (to
, from
);
1900 /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or
1901 constructible (otherwise) from FROM, which is a single type for
1902 assignment or a list of types for construction. */
1905 is_trivially_xible (enum tree_code code
, tree to
, tree from
)
1908 expr
= is_xible_helper (code
, to
, from
, /*trivial*/true);
1910 if (expr
== NULL_TREE
|| expr
== error_mark_node
)
1912 tree nt
= cp_walk_tree_without_duplicates (&expr
, check_nontriv
, NULL
);
1916 /* Returns true iff TO is assignable (if CODE is MODIFY_EXPR) or
1917 constructible (otherwise) from FROM, which is a single type for
1918 assignment or a list of types for construction. */
1921 is_xible (enum tree_code code
, tree to
, tree from
)
1923 tree expr
= is_xible_helper (code
, to
, from
, /*trivial*/false);
1924 if (expr
== error_mark_node
)
1929 /* Categorize various special_function_kinds. */
1930 #define SFK_CTOR_P(sfk) \
1931 ((sfk) >= sfk_constructor && (sfk) <= sfk_move_constructor)
1932 #define SFK_DTOR_P(sfk) \
1933 ((sfk) == sfk_destructor || (sfk) == sfk_virtual_destructor)
1934 #define SFK_ASSIGN_P(sfk) \
1935 ((sfk) == sfk_copy_assignment || (sfk) == sfk_move_assignment)
1936 #define SFK_COPY_P(sfk) \
1937 ((sfk) == sfk_copy_constructor || (sfk) == sfk_copy_assignment)
1938 #define SFK_MOVE_P(sfk) \
1939 ((sfk) == sfk_move_constructor || (sfk) == sfk_move_assignment)
1941 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
1942 DELETED_P or give an error message MSG with argument ARG. */
1945 process_subob_fn (tree fn
, special_function_kind sfk
, tree
*spec_p
,
1946 bool *trivial_p
, bool *deleted_p
, bool *constexpr_p
,
1947 bool diag
, tree arg
, bool dtor_from_ctor
= false)
1949 if (!fn
|| fn
== error_mark_node
)
1958 if (!maybe_instantiate_noexcept (fn
))
1959 *spec_p
= error_mark_node
;
1962 tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
1963 *spec_p
= merge_exception_specifiers (*spec_p
, raises
);
1967 if (!trivial_fn_p (fn
) && !dtor_from_ctor
)
1971 if (TREE_CODE (arg
) == FIELD_DECL
1972 && TREE_CODE (DECL_CONTEXT (arg
)) == UNION_TYPE
)
1977 error ("union member %q+D with non-trivial %qD", arg
, fn
);
1981 if (constexpr_p
&& !DECL_DECLARED_CONSTEXPR_P (fn
))
1983 *constexpr_p
= false;
1986 inform (DECL_SOURCE_LOCATION (fn
),
1988 ? G_("defaulted destructor calls non-%<constexpr%> %qD")
1989 : G_("defaulted constructor calls non-%<constexpr%> %qD"),
1991 explain_invalid_constexpr_fn (fn
);
1996 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
1997 aggregates. If DTOR_FROM_CTOR is true, we're walking subobject destructors
1998 called from a synthesized constructor, in which case we don't consider
1999 the triviality of the subobject destructor. */
2002 walk_field_subobs (tree fields
, special_function_kind sfk
, tree fnname
,
2003 int quals
, tree
*spec_p
, bool *trivial_p
,
2004 bool *deleted_p
, bool *constexpr_p
,
2005 bool diag
, int flags
, tsubst_flags_t complain
,
2006 bool dtor_from_ctor
)
2009 for (field
= fields
; field
; field
= DECL_CHAIN (field
))
2011 tree mem_type
, argtype
, rval
;
2013 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
2016 /* Variant members only affect deletedness. In particular, they don't
2017 affect the exception-specification of a user-provided destructor,
2018 which we're figuring out via get_defaulted_eh_spec. So if we aren't
2019 asking if this is deleted, don't even look up the function; we don't
2020 want an error about a deleted function we aren't actually calling. */
2021 if (sfk
== sfk_destructor
&& deleted_p
== NULL
2022 && TREE_CODE (DECL_CONTEXT (field
)) == UNION_TYPE
)
2025 mem_type
= strip_array_types (TREE_TYPE (field
));
2026 if (SFK_ASSIGN_P (sfk
))
2029 if (CP_TYPE_CONST_P (mem_type
) && !CLASS_TYPE_P (mem_type
))
2032 error ("non-static const member %q#D, cannot use default "
2033 "assignment operator", field
);
2035 else if (TYPE_REF_P (mem_type
))
2038 error ("non-static reference member %q#D, cannot use "
2039 "default assignment operator", field
);
2044 if (bad
&& deleted_p
)
2047 else if (sfk
== sfk_constructor
|| sfk
== sfk_inheriting_constructor
)
2051 if (DECL_INITIAL (field
))
2053 if (diag
&& DECL_INITIAL (field
) == error_mark_node
)
2054 inform (DECL_SOURCE_LOCATION (field
),
2055 "initializer for %q#D is invalid", field
);
2058 /* Core 1351: If the field has an NSDMI that could throw, the
2059 default constructor is noexcept(false). */
2062 tree nsdmi
= get_nsdmi (field
, /*ctor*/false, complain
);
2063 if (nsdmi
== error_mark_node
)
2064 *spec_p
= error_mark_node
;
2065 else if (*spec_p
!= error_mark_node
2066 && !expr_noexcept_p (nsdmi
, tf_none
))
2067 *spec_p
= noexcept_false_spec
;
2069 /* Don't do the normal processing. */
2074 if (CP_TYPE_CONST_P (mem_type
)
2075 && default_init_uninitialized_part (mem_type
))
2079 error ("uninitialized const member in %q#T",
2080 current_class_type
);
2081 inform (DECL_SOURCE_LOCATION (field
),
2082 "%q#D should be initialized", field
);
2086 else if (TYPE_REF_P (mem_type
))
2090 error ("uninitialized reference member in %q#T",
2091 current_class_type
);
2092 inform (DECL_SOURCE_LOCATION (field
),
2093 "%q#D should be initialized", field
);
2098 if (bad
&& deleted_p
)
2101 /* Before C++20, for an implicitly-defined default constructor to
2102 be constexpr, every member must have a user-provided default
2103 constructor or an explicit initializer. */
2105 && cxx_dialect
< cxx20
2106 && !CLASS_TYPE_P (mem_type
)
2107 && TREE_CODE (DECL_CONTEXT (field
)) != UNION_TYPE
)
2109 *constexpr_p
= false;
2111 inform (DECL_SOURCE_LOCATION (field
),
2112 "defaulted default constructor does not "
2113 "initialize %q#D", field
);
2116 else if (sfk
== sfk_copy_constructor
)
2119 if (TYPE_REF_P (mem_type
)
2120 && TYPE_REF_IS_RVALUE (mem_type
))
2123 error ("copying non-static data member %q#D of rvalue "
2124 "reference type", field
);
2130 if (!CLASS_TYPE_P (mem_type
))
2133 if (ANON_AGGR_TYPE_P (mem_type
))
2135 walk_field_subobs (TYPE_FIELDS (mem_type
), sfk
, fnname
, quals
,
2136 spec_p
, trivial_p
, deleted_p
, constexpr_p
,
2137 diag
, flags
, complain
, dtor_from_ctor
);
2141 if (SFK_COPY_P (sfk
) || SFK_MOVE_P (sfk
))
2143 int mem_quals
= cp_type_quals (mem_type
) | quals
;
2144 if (DECL_MUTABLE_P (field
))
2145 mem_quals
&= ~TYPE_QUAL_CONST
;
2146 argtype
= build_stub_type (mem_type
, mem_quals
, SFK_MOVE_P (sfk
));
2149 argtype
= NULL_TREE
;
2151 rval
= locate_fn_flags (mem_type
, fnname
, argtype
, flags
, complain
);
2153 process_subob_fn (rval
, sfk
, spec_p
, trivial_p
, deleted_p
,
2154 constexpr_p
, diag
, field
, dtor_from_ctor
);
2158 /* Base walker helper for synthesized_method_walk. Inspect a direct
2159 or virtual base. BINFO is the parent type's binfo. BASE_BINFO is
2160 the base binfo of interests. All other parms are as for
2161 synthesized_method_walk, or its local vars. */
2164 synthesized_method_base_walk (tree binfo
, tree base_binfo
,
2165 special_function_kind sfk
, tree fnname
, int quals
,
2166 tree
*inheriting_ctor
, tree inherited_parms
,
2167 int flags
, bool diag
,
2168 tree
*spec_p
, bool *trivial_p
,
2169 bool *deleted_p
, bool *constexpr_p
)
2171 bool inherited_binfo
= false;
2172 tree argtype
= NULL_TREE
;
2173 deferring_kind defer
= dk_no_deferred
;
2175 if (SFK_COPY_P (sfk
) || SFK_MOVE_P (sfk
))
2176 argtype
= build_stub_type (BINFO_TYPE (base_binfo
), quals
, SFK_MOVE_P (sfk
));
2177 else if (inheriting_ctor
2179 = binfo_inherited_from (binfo
, base_binfo
, *inheriting_ctor
)))
2181 argtype
= inherited_parms
;
2182 /* Don't check access on the inherited constructor. */
2183 if (flag_new_inheriting_ctors
)
2184 defer
= dk_deferred
;
2186 else if (cxx_dialect
>= cxx14
&& sfk
== sfk_virtual_destructor
2187 && BINFO_VIRTUAL_P (base_binfo
)
2188 && ABSTRACT_CLASS_TYPE_P (BINFO_TYPE (binfo
)))
2189 /* Don't check access when looking at vbases of abstract class's
2190 virtual destructor. */
2191 defer
= dk_no_check
;
2193 if (defer
!= dk_no_deferred
)
2194 push_deferring_access_checks (defer
);
2195 tree rval
= locate_fn_flags (base_binfo
, fnname
, argtype
, flags
,
2196 diag
? tf_warning_or_error
: tf_none
);
2197 if (defer
!= dk_no_deferred
)
2198 pop_deferring_access_checks ();
2200 /* Replace an inherited template with the appropriate specialization. */
2201 if (inherited_binfo
&& rval
2202 && DECL_P (*inheriting_ctor
) && DECL_P (rval
)
2203 && DECL_CONTEXT (*inheriting_ctor
) == DECL_CONTEXT (rval
))
2204 *inheriting_ctor
= DECL_CLONED_FUNCTION (rval
);
2206 process_subob_fn (rval
, sfk
, spec_p
, trivial_p
, deleted_p
,
2207 constexpr_p
, diag
, BINFO_TYPE (base_binfo
));
2208 if (SFK_CTOR_P (sfk
)
2209 && (!BINFO_VIRTUAL_P (base_binfo
)
2210 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo
))))
2212 /* In a constructor we also need to check the subobject
2213 destructors for cleanup of partially constructed objects. */
2214 tree dtor
= locate_fn_flags (base_binfo
, complete_dtor_identifier
,
2216 diag
? tf_warning_or_error
: tf_none
);
2217 /* Note that we don't pass down trivial_p; the subobject
2218 destructors don't affect triviality of the constructor. Nor
2219 do they affect constexpr-ness (a constant expression doesn't
2220 throw) or exception-specification (a throw from one of the
2221 dtors would be a double-fault). */
2222 process_subob_fn (dtor
, sfk
, NULL
, NULL
, deleted_p
, NULL
, false,
2223 BINFO_TYPE (base_binfo
), /*dtor_from_ctor*/true);
2229 /* The caller wants to generate an implicit declaration of SFK for
2230 CTYPE which is const if relevant and CONST_P is set. If SPEC_P,
2231 TRIVIAL_P, DELETED_P or CONSTEXPR_P are non-null, set their
2232 referent appropriately. If DIAG is true, we're either being called
2233 from maybe_explain_implicit_delete to give errors, or if
2234 CONSTEXPR_P is non-null, from explain_invalid_constexpr_fn. */
2237 synthesized_method_walk (tree ctype
, special_function_kind sfk
, bool const_p
,
2238 tree
*spec_p
, bool *trivial_p
, bool *deleted_p
,
2239 bool *constexpr_p
, bool diag
,
2240 tree
*inheriting_ctor
, tree inherited_parms
)
2242 tree binfo
, base_binfo
;
2245 /* SFK must be exactly one category. */
2246 gcc_checking_assert (SFK_DTOR_P(sfk
) + SFK_CTOR_P(sfk
)
2247 + SFK_ASSIGN_P(sfk
) == 1);
2250 *spec_p
= (cxx_dialect
>= cxx11
? noexcept_true_spec
: empty_except_spec
);
2254 /* "The closure type associated with a lambda-expression has a deleted
2255 default constructor and a deleted copy assignment operator."
2256 This is diagnosed in maybe_explain_implicit_delete.
2257 In C++20, only lambda-expressions with lambda-captures have those
2259 if (LAMBDA_TYPE_P (ctype
)
2260 && (sfk
== sfk_constructor
|| sfk
== sfk_copy_assignment
)
2261 && (cxx_dialect
< cxx20
2262 || LAMBDA_EXPR_CAPTURE_LIST (CLASSTYPE_LAMBDA_EXPR (ctype
))
2263 || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE
2264 (CLASSTYPE_LAMBDA_EXPR (ctype
)) != CPLD_NONE
))
2273 bool check_vdtor
= false;
2276 if (SFK_DTOR_P (sfk
))
2279 /* The synthesized method will call base dtors, but check complete
2280 here to avoid having to deal with VTT. */
2281 fnname
= complete_dtor_identifier
;
2283 else if (SFK_ASSIGN_P (sfk
))
2284 fnname
= assign_op_identifier
;
2286 fnname
= complete_ctor_identifier
;
2288 gcc_assert ((sfk
== sfk_inheriting_constructor
)
2289 == (inheriting_ctor
&& *inheriting_ctor
!= NULL_TREE
));
2291 /* If that user-written default constructor would satisfy the
2292 requirements of a constexpr constructor (7.1.5), the
2293 implicitly-defined default constructor is constexpr.
2295 The implicitly-defined copy/move assignment operator is constexpr if
2296 - X is a literal type, and
2297 - the assignment operator selected to copy/move each direct base class
2298 subobject is a constexpr function, and
2299 - for each non-static data member of X that is of class type (or array
2300 thereof), the assignment operator selected to copy/move that
2301 member is a constexpr function. */
2303 *constexpr_p
= (SFK_CTOR_P (sfk
)
2304 || (SFK_ASSIGN_P (sfk
) && cxx_dialect
>= cxx14
)
2305 || (SFK_DTOR_P (sfk
) && cxx_dialect
>= cxx20
));
2307 bool expected_trivial
= type_has_trivial_fn (ctype
, sfk
);
2309 *trivial_p
= expected_trivial
;
2311 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
2312 class versions and other properties of the type. But a subobject
2313 class can be trivially copyable and yet have overload resolution
2314 choose a template constructor for initialization, depending on
2315 rvalueness and cv-quals. And furthermore, a member in a base might
2316 be trivial but deleted or otherwise not callable. So we can't exit
2317 early in C++0x. The same considerations apply in C++98/03, but
2318 there the definition of triviality does not consider overload
2319 resolution, so a constructor can be trivial even if it would otherwise
2320 call a non-trivial constructor. */
2321 if (expected_trivial
2322 && (!(SFK_COPY_P (sfk
) || SFK_MOVE_P (sfk
)) || cxx_dialect
< cxx11
))
2324 if (constexpr_p
&& sfk
== sfk_constructor
)
2326 bool cx
= trivial_default_constructor_is_constexpr (ctype
);
2328 if (diag
&& !cx
&& TREE_CODE (ctype
) == UNION_TYPE
)
2329 /* A trivial constructor doesn't have any NSDMI. */
2330 inform (input_location
, "defaulted default constructor does "
2331 "not initialize any non-static data member");
2333 if (!diag
&& cxx_dialect
< cxx11
)
2337 ++cp_unevaluated_operand
;
2338 ++c_inhibit_evaluation_warnings
;
2339 push_deferring_access_checks (dk_no_deferred
);
2341 tree scope
= push_scope (ctype
);
2343 int flags
= LOOKUP_NORMAL
| LOOKUP_SPECULATIVE
;
2344 if (sfk
!= sfk_inheriting_constructor
)
2345 flags
|= LOOKUP_DEFAULTED
;
2347 tsubst_flags_t complain
= diag
? tf_warning_or_error
: tf_none
;
2349 /* We're in get_defaulted_eh_spec; we don't actually want any walking
2350 diagnostics, we just want complain set. */
2352 int quals
= const_p
? TYPE_QUAL_CONST
: TYPE_UNQUALIFIED
;
2354 for (binfo
= TYPE_BINFO (ctype
), i
= 0;
2355 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
2357 if (!SFK_ASSIGN_P (sfk
) && BINFO_VIRTUAL_P (base_binfo
))
2358 /* We'll handle virtual bases below. */
2361 tree fn
= synthesized_method_base_walk (binfo
, base_binfo
,
2363 inheriting_ctor
, inherited_parms
,
2364 flags
, diag
, spec_p
, trivial_p
,
2365 deleted_p
, constexpr_p
);
2367 if (diag
&& SFK_ASSIGN_P (sfk
) && SFK_MOVE_P (sfk
)
2368 && BINFO_VIRTUAL_P (base_binfo
)
2369 && fn
&& TREE_CODE (fn
) == FUNCTION_DECL
2370 && move_fn_p (fn
) && !trivial_fn_p (fn
)
2371 && vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo
)))
2372 warning (OPT_Wvirtual_move_assign
,
2373 "defaulted move assignment for %qT calls a non-trivial "
2374 "move assignment operator for virtual base %qT",
2375 ctype
, BINFO_TYPE (base_binfo
));
2377 if (check_vdtor
&& type_has_virtual_destructor (BINFO_TYPE (base_binfo
)))
2379 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
2380 to have a null fn (no class-specific op delete). */
2381 fn
= locate_fn_flags (ctype
, ovl_op_identifier (false, DELETE_EXPR
),
2382 ptr_type_node
, flags
, tf_none
);
2383 if (fn
&& fn
== error_mark_node
)
2385 if (complain
& tf_error
)
2386 locate_fn_flags (ctype
, ovl_op_identifier (false, DELETE_EXPR
),
2387 ptr_type_node
, flags
, complain
);
2391 check_vdtor
= false;
2395 vec
<tree
, va_gc
> *vbases
= CLASSTYPE_VBASECLASSES (ctype
);
2396 if (SFK_ASSIGN_P (sfk
))
2397 /* Already examined vbases above. */;
2398 else if (vec_safe_is_empty (vbases
))
2399 /* No virtual bases to worry about. */;
2400 else if (ABSTRACT_CLASS_TYPE_P (ctype
) && cxx_dialect
>= cxx14
2401 /* DR 1658 specifies that vbases of abstract classes are
2402 ignored for both ctors and dtors. Except DR 2336
2403 overrides that skipping when determing the eh-spec of a
2404 virtual destructor. */
2405 && sfk
!= sfk_virtual_destructor
)
2406 /* Vbase cdtors are not relevant. */;
2410 *constexpr_p
= false;
2412 FOR_EACH_VEC_ELT (*vbases
, i
, base_binfo
)
2413 synthesized_method_base_walk (binfo
, base_binfo
, sfk
, fnname
, quals
,
2414 inheriting_ctor
, inherited_parms
,
2416 spec_p
, trivial_p
, deleted_p
, constexpr_p
);
2419 /* Now handle the non-static data members. */
2420 walk_field_subobs (TYPE_FIELDS (ctype
), sfk
, fnname
, quals
,
2421 spec_p
, trivial_p
, deleted_p
, constexpr_p
,
2422 diag
, flags
, complain
, /*dtor_from_ctor*/false);
2423 if (SFK_CTOR_P (sfk
))
2424 walk_field_subobs (TYPE_FIELDS (ctype
), sfk_destructor
,
2425 complete_dtor_identifier
, TYPE_UNQUALIFIED
,
2426 NULL
, NULL
, deleted_p
, NULL
,
2427 false, flags
, complain
, /*dtor_from_ctor*/true);
2431 pop_deferring_access_checks ();
2432 --cp_unevaluated_operand
;
2433 --c_inhibit_evaluation_warnings
;
2436 /* DECL is a defaulted function whose exception specification is now
2437 needed. Return what it should be. */
2440 get_defaulted_eh_spec (tree decl
, tsubst_flags_t complain
)
2442 /* For DECL_MAYBE_DELETED this should already have been handled by
2443 synthesize_method. */
2444 gcc_assert (!DECL_MAYBE_DELETED (decl
));
2446 if (DECL_CLONED_FUNCTION_P (decl
))
2447 decl
= DECL_CLONED_FUNCTION (decl
);
2448 special_function_kind sfk
= special_function_p (decl
);
2449 tree ctype
= DECL_CONTEXT (decl
);
2450 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
2451 tree parm_type
= TREE_VALUE (parms
);
2452 bool const_p
= CP_TYPE_CONST_P (non_reference (parm_type
));
2453 tree spec
= empty_except_spec
;
2454 bool diag
= !DECL_DELETED_FN (decl
) && (complain
& tf_error
);
2455 tree inh
= DECL_INHERITED_CTOR (decl
);
2456 if (SFK_DTOR_P (sfk
) && DECL_VIRTUAL_P (decl
))
2457 /* We have to examine virtual bases even if abstract. */
2458 sfk
= sfk_virtual_destructor
;
2459 bool pushed
= false;
2460 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2461 pushed
= push_tinst_level (decl
);
2462 synthesized_method_walk (ctype
, sfk
, const_p
, &spec
, NULL
, NULL
,
2463 NULL
, diag
, &inh
, parms
);
2469 /* DECL is a deleted function. If it's implicitly deleted, explain why and
2470 return true; else return false. */
2473 maybe_explain_implicit_delete (tree decl
)
2475 /* If decl is a clone, get the primary variant. */
2476 decl
= DECL_ORIGIN (decl
);
2477 gcc_assert (DECL_DELETED_FN (decl
));
2478 if (DECL_DEFAULTED_FN (decl
))
2480 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
2481 static hash_set
<tree
> *explained
;
2483 special_function_kind sfk
;
2489 explained
= new hash_set
<tree
>;
2490 if (explained
->add (decl
))
2493 sfk
= special_function_p (decl
);
2494 ctype
= DECL_CONTEXT (decl
);
2495 loc
= input_location
;
2496 input_location
= DECL_SOURCE_LOCATION (decl
);
2499 if (LAMBDA_TYPE_P (ctype
))
2502 if (sfk
== sfk_constructor
)
2503 inform (DECL_SOURCE_LOCATION (decl
),
2504 "a lambda closure type has a deleted default constructor");
2505 else if (sfk
== sfk_copy_assignment
)
2506 inform (DECL_SOURCE_LOCATION (decl
),
2507 "a lambda closure type has a deleted copy assignment operator");
2511 else if (DECL_ARTIFICIAL (decl
)
2512 && (sfk
== sfk_copy_assignment
|| sfk
== sfk_copy_constructor
)
2513 && classtype_has_move_assign_or_move_ctor_p (ctype
, true))
2515 inform (DECL_SOURCE_LOCATION (decl
),
2516 "%q#D is implicitly declared as deleted because %qT "
2517 "declares a move constructor or move assignment operator",
2521 else if (sfk
== sfk_inheriting_constructor
)
2523 tree binfo
= inherited_ctor_binfo (decl
);
2524 if (TREE_CODE (binfo
) != TREE_BINFO
)
2526 inform (DECL_SOURCE_LOCATION (decl
),
2527 "%q#D inherits from multiple base subobjects",
2532 if (!informed
&& sfk
== sfk_comparison
)
2534 inform (DECL_SOURCE_LOCATION (decl
),
2535 "%q#D is implicitly deleted because the default "
2536 "definition would be ill-formed:", decl
);
2537 build_comparison_op (decl
, tf_warning_or_error
);
2541 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
2542 bool const_p
= false;
2545 tree parm_type
= TREE_VALUE (parms
);
2546 const_p
= CP_TYPE_CONST_P (non_reference (parm_type
));
2548 tree raises
= NULL_TREE
;
2549 bool deleted_p
= false;
2550 tree scope
= push_scope (ctype
);
2551 tree inh
= DECL_INHERITED_CTOR (decl
);
2553 synthesized_method_walk (ctype
, sfk
, const_p
,
2554 &raises
, NULL
, &deleted_p
, NULL
, false,
2558 inform (DECL_SOURCE_LOCATION (decl
),
2559 "%q#D is implicitly deleted because the default "
2560 "definition would be ill-formed:", decl
);
2561 synthesized_method_walk (ctype
, sfk
, const_p
,
2562 NULL
, NULL
, &deleted_p
, NULL
, true,
2565 else if (!comp_except_specs
2566 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl
)),
2568 inform (DECL_SOURCE_LOCATION (decl
), "%q#F is implicitly "
2569 "deleted because its exception-specification does not "
2570 "match the implicit exception-specification %qX",
2572 else if (flag_checking
)
2578 input_location
= loc
;
2584 /* DECL is a defaulted function which was declared constexpr. Explain why
2585 it can't be constexpr. */
2588 explain_implicit_non_constexpr (tree decl
)
2590 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
2591 bool const_p
= CP_TYPE_CONST_P (non_reference (TREE_VALUE (parms
)));
2592 tree inh
= DECL_INHERITED_CTOR (decl
);
2594 special_function_kind sfk
= special_function_p (decl
);
2595 if (sfk
== sfk_comparison
)
2597 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
2598 build_comparison_op (decl
, tf_warning_or_error
);
2599 DECL_DECLARED_CONSTEXPR_P (decl
) = false;
2602 synthesized_method_walk (DECL_CLASS_CONTEXT (decl
),
2604 NULL
, NULL
, NULL
, &dummy
, true,
2608 /* DECL is an instantiation of an inheriting constructor template. Deduce
2609 the correct exception-specification and deletedness for this particular
2613 deduce_inheriting_ctor (tree decl
)
2615 decl
= DECL_ORIGIN (decl
);
2616 gcc_assert (DECL_INHERITED_CTOR (decl
));
2618 bool trivial
, constexpr_
, deleted
;
2619 tree inh
= DECL_INHERITED_CTOR (decl
);
2620 synthesized_method_walk (DECL_CONTEXT (decl
), sfk_inheriting_constructor
,
2621 false, &spec
, &trivial
, &deleted
, &constexpr_
,
2624 FUNCTION_FIRST_USER_PARMTYPE (decl
));
2625 if (TREE_CODE (inherited_ctor_binfo (decl
)) != TREE_BINFO
)
2626 /* Inherited the same constructor from different base subobjects. */
2628 DECL_DELETED_FN (decl
) = deleted
;
2629 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
), spec
);
2630 SET_DECL_INHERITED_CTOR (decl
, inh
);
2633 FOR_EACH_CLONE (clone
, decl
)
2635 DECL_DELETED_FN (clone
) = deleted
;
2636 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
2637 SET_DECL_INHERITED_CTOR (clone
, inh
);
2641 /* Implicitly declare the special function indicated by KIND, as a
2642 member of TYPE. For copy constructors and assignment operators,
2643 CONST_P indicates whether these functions should take a const
2644 reference argument or a non-const reference.
2645 Returns the FUNCTION_DECL for the implicitly declared function. */
2648 implicitly_declare_fn (special_function_kind kind
, tree type
,
2649 bool const_p
, tree pattern_fn
,
2650 tree inherited_parms
)
2653 tree parameter_types
= void_list_node
;
2656 tree raises
= empty_except_spec
;
2657 tree rhs_parm_type
= NULL_TREE
;
2660 HOST_WIDE_INT saved_processing_template_decl
;
2661 bool deleted_p
= false;
2662 bool constexpr_p
= false;
2663 tree inherited_ctor
= (kind
== sfk_inheriting_constructor
2664 ? pattern_fn
: NULL_TREE
);
2666 /* Because we create declarations for implicitly declared functions
2667 lazily, we may be creating the declaration for a member of TYPE
2668 while in some completely different context. However, TYPE will
2669 never be a dependent class (because we never want to do lookups
2670 for implicitly defined functions in a dependent class). */
2671 gcc_assert (!dependent_type_p (type
));
2673 /* If the member-specification does not explicitly declare any member or
2674 friend named operator==, an == operator function is declared
2675 implicitly for each three-way comparison operator function defined as
2676 defaulted in the member-specification, with the same access and
2677 function-definition and in the same class scope as the respective
2678 three-way comparison operator function, except that the return type is
2679 replaced with bool and the declarator-id is replaced with
2682 [Note: Such an implicitly-declared == operator for a class X is
2683 defined as defaulted in the definition of X and has the same
2684 parameter-declaration-clause and trailing requires-clause as the
2685 respective three-way comparison operator. It is declared with friend,
2686 virtual, constexpr, or consteval if the three-way comparison operator
2687 function is so declared. If the three-way comparison operator function
2688 has no noexcept-specifier, the implicitly-declared == operator
2689 function has an implicit exception specification (14.5) that may
2690 differ from the implicit exception specification of the three-way
2691 comparison operator function. --end note] */
2692 if (kind
== sfk_comparison
)
2694 fn
= copy_operator_fn (pattern_fn
, EQ_EXPR
);
2695 DECL_ARTIFICIAL (fn
) = 1;
2696 TREE_TYPE (fn
) = change_return_type (boolean_type_node
, TREE_TYPE (fn
));
2700 /* Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
2701 because we only create clones for constructors and destructors
2702 when not in a template. */
2703 saved_processing_template_decl
= processing_template_decl
;
2704 processing_template_decl
= 0;
2706 type
= TYPE_MAIN_VARIANT (type
);
2708 if (targetm
.cxx
.cdtor_returns_this ())
2710 if (kind
== sfk_destructor
)
2711 /* See comment in check_special_function_return_type. */
2712 return_type
= build_pointer_type (void_type_node
);
2714 return_type
= build_pointer_type (type
);
2717 return_type
= void_type_node
;
2719 int this_quals
= TYPE_UNQUALIFIED
;
2722 case sfk_destructor
:
2724 name
= dtor_identifier
;
2727 case sfk_constructor
:
2728 /* Default constructor. */
2729 name
= ctor_identifier
;
2732 case sfk_copy_constructor
:
2733 case sfk_copy_assignment
:
2734 case sfk_move_constructor
:
2735 case sfk_move_assignment
:
2736 case sfk_inheriting_constructor
:
2738 if (kind
== sfk_copy_assignment
2739 || kind
== sfk_move_assignment
)
2741 return_type
= build_reference_type (type
);
2742 name
= assign_op_identifier
;
2745 name
= ctor_identifier
;
2747 if (kind
== sfk_inheriting_constructor
)
2748 parameter_types
= inherited_parms
;
2752 rhs_parm_type
= cp_build_qualified_type (type
, TYPE_QUAL_CONST
);
2754 rhs_parm_type
= type
;
2755 bool move_p
= (kind
== sfk_move_assignment
2756 || kind
== sfk_move_constructor
);
2757 rhs_parm_type
= cp_build_reference_type (rhs_parm_type
, move_p
);
2759 parameter_types
= tree_cons (NULL_TREE
, rhs_parm_type
, parameter_types
);
2768 bool trivial_p
= false;
2772 /* For an inheriting constructor, just copy these flags from the
2773 inherited constructor until deduce_inheriting_ctor. */
2774 raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor
));
2775 deleted_p
= DECL_DELETED_FN (inherited_ctor
);
2776 constexpr_p
= DECL_DECLARED_CONSTEXPR_P (inherited_ctor
);
2778 else if (cxx_dialect
>= cxx11
)
2780 raises
= noexcept_deferred_spec
;
2781 synthesized_method_walk (type
, kind
, const_p
, NULL
, &trivial_p
,
2782 &deleted_p
, &constexpr_p
, false,
2783 &inherited_ctor
, inherited_parms
);
2786 synthesized_method_walk (type
, kind
, const_p
, &raises
, &trivial_p
,
2787 &deleted_p
, &constexpr_p
, false,
2788 &inherited_ctor
, inherited_parms
);
2789 /* Don't bother marking a deleted constructor as constexpr. */
2791 constexpr_p
= false;
2792 /* A trivial copy/move constructor is also a constexpr constructor,
2793 unless the class has virtual bases (7.1.5p4). */
2795 && cxx_dialect
>= cxx11
2796 && (kind
== sfk_copy_constructor
2797 || kind
== sfk_move_constructor
)
2798 && !CLASSTYPE_VBASECLASSES (type
))
2799 gcc_assert (constexpr_p
);
2801 if (!trivial_p
&& type_has_trivial_fn (type
, kind
))
2802 type_set_nontrivial_flag (type
, kind
);
2804 /* Create the function. */
2805 tree this_type
= cp_build_qualified_type (type
, this_quals
);
2806 fn_type
= build_method_type_directly (this_type
, return_type
,
2811 if (raises
!= error_mark_node
)
2812 fn_type
= build_exception_variant (fn_type
, raises
);
2814 /* Can happen, eg, in C++98 mode for an ill-formed non-static data
2815 member initializer (c++/89914). */
2816 gcc_assert (seen_error ());
2818 fn
= build_lang_decl (FUNCTION_DECL
, name
, fn_type
);
2819 if (kind
!= sfk_inheriting_constructor
)
2820 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (TYPE_NAME (type
));
2822 if (IDENTIFIER_OVL_OP_P (name
))
2824 const ovl_op_info_t
*op
= IDENTIFIER_OVL_OP_INFO (name
);
2825 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn
) = op
->ovl_op_code
;
2827 else if (IDENTIFIER_CTOR_P (name
))
2828 DECL_CXX_CONSTRUCTOR_P (fn
) = true;
2829 else if (IDENTIFIER_DTOR_P (name
))
2830 DECL_CXX_DESTRUCTOR_P (fn
) = true;
2834 SET_DECL_ALIGN (fn
, MINIMUM_METHOD_BOUNDARY
);
2836 /* Create the explicit arguments. */
2839 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
2840 want its type to be included in the mangled function
2842 tree decl
= cp_build_parm_decl (fn
, NULL_TREE
, rhs_parm_type
);
2843 TREE_READONLY (decl
) = 1;
2844 retrofit_lang_decl (decl
);
2845 DECL_PARM_INDEX (decl
) = DECL_PARM_LEVEL (decl
) = 1;
2846 DECL_ARGUMENTS (fn
) = decl
;
2848 else if (kind
== sfk_inheriting_constructor
)
2850 tree
*p
= &DECL_ARGUMENTS (fn
);
2852 for (tree parm
= inherited_parms
; parm
&& parm
!= void_list_node
;
2853 parm
= TREE_CHAIN (parm
))
2855 *p
= cp_build_parm_decl (fn
, NULL_TREE
, TREE_VALUE (parm
));
2856 retrofit_lang_decl (*p
);
2857 DECL_PARM_LEVEL (*p
) = 1;
2858 DECL_PARM_INDEX (*p
) = index
++;
2859 p
= &DECL_CHAIN (*p
);
2861 SET_DECL_INHERITED_CTOR (fn
, inherited_ctor
);
2862 DECL_NONCONVERTING_P (fn
) = DECL_NONCONVERTING_P (inherited_ctor
);
2863 /* A constructor so declared has the same access as the corresponding
2864 constructor in X. */
2865 TREE_PRIVATE (fn
) = TREE_PRIVATE (inherited_ctor
);
2866 TREE_PROTECTED (fn
) = TREE_PROTECTED (inherited_ctor
);
2867 /* Copy constexpr from the inherited constructor even if the
2868 inheriting constructor doesn't satisfy the requirements. */
2869 constexpr_p
= DECL_DECLARED_CONSTEXPR_P (inherited_ctor
);
2872 /* Add the "this" parameter. */
2873 this_parm
= build_this_parm (fn
, fn_type
, this_quals
);
2874 DECL_CHAIN (this_parm
) = DECL_ARGUMENTS (fn
);
2875 DECL_ARGUMENTS (fn
) = this_parm
;
2877 grokclassfn (type
, fn
, kind
== sfk_destructor
? DTOR_FLAG
: NO_SPECIAL
);
2879 DECL_IN_AGGR_P (fn
) = 1;
2880 DECL_ARTIFICIAL (fn
) = 1;
2881 DECL_DEFAULTED_FN (fn
) = 1;
2882 if (cxx_dialect
>= cxx11
)
2884 DECL_DELETED_FN (fn
) = deleted_p
;
2885 DECL_DECLARED_CONSTEXPR_P (fn
) = constexpr_p
;
2887 DECL_EXTERNAL (fn
) = true;
2888 DECL_NOT_REALLY_EXTERN (fn
) = 1;
2889 DECL_DECLARED_INLINE_P (fn
) = 1;
2890 set_linkage_according_to_type (type
, fn
);
2891 if (TREE_PUBLIC (fn
))
2892 DECL_COMDAT (fn
) = 1;
2893 rest_of_decl_compilation (fn
, namespace_bindings_p (), at_eof
);
2894 gcc_assert (!TREE_USED (fn
));
2896 /* Propagate constraints from the inherited constructor. */
2897 if (flag_concepts
&& inherited_ctor
)
2898 if (tree orig_ci
= get_constraints (inherited_ctor
))
2900 tree new_ci
= copy_node (orig_ci
);
2901 set_constraints (fn
, new_ci
);
2904 /* Restore PROCESSING_TEMPLATE_DECL. */
2905 processing_template_decl
= saved_processing_template_decl
;
2907 if (inherited_ctor
&& TREE_CODE (inherited_ctor
) == TEMPLATE_DECL
)
2908 fn
= add_inherited_template_parms (fn
, inherited_ctor
);
2910 /* Warn about calling a non-trivial move assignment in a virtual base. */
2911 if (kind
== sfk_move_assignment
&& !deleted_p
&& !trivial_p
2912 && CLASSTYPE_VBASECLASSES (type
))
2914 location_t loc
= input_location
;
2915 input_location
= DECL_SOURCE_LOCATION (fn
);
2916 synthesized_method_walk (type
, kind
, const_p
,
2917 NULL
, NULL
, NULL
, NULL
, true,
2919 input_location
= loc
;
2925 /* Gives any errors about defaulted functions which need to be deferred
2926 until the containing class is complete. */
2929 defaulted_late_check (tree fn
)
2931 /* Complain about invalid signature for defaulted fn. */
2932 tree ctx
= DECL_CONTEXT (fn
);
2933 special_function_kind kind
= special_function_p (fn
);
2935 if (kind
== sfk_comparison
)
2937 /* If the function was declared constexpr, check that the definition
2938 qualifies. Otherwise we can define the function lazily. */
2939 if (DECL_DECLARED_CONSTEXPR_P (fn
) && !DECL_INITIAL (fn
))
2940 synthesize_method (fn
);
2944 bool fn_const_p
= (copy_fn_p (fn
) == 2);
2945 tree implicit_fn
= implicitly_declare_fn (kind
, ctx
, fn_const_p
,
2947 tree eh_spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn
));
2949 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn
)),
2950 TREE_TYPE (TREE_TYPE (implicit_fn
)))
2951 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2952 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn
))))
2954 error ("defaulted declaration %q+D does not match the "
2955 "expected signature", fn
);
2956 inform (DECL_SOURCE_LOCATION (fn
),
2957 "expected signature: %qD", implicit_fn
);
2960 if (DECL_DELETED_FN (implicit_fn
))
2962 DECL_DELETED_FN (fn
) = 1;
2966 /* If a function is explicitly defaulted on its first declaration without an
2967 exception-specification, it is implicitly considered to have the same
2968 exception-specification as if it had been implicitly declared. */
2969 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
))
2970 && DECL_DEFAULTED_IN_CLASS_P (fn
))
2971 TREE_TYPE (fn
) = build_exception_variant (TREE_TYPE (fn
), eh_spec
);
2973 if (DECL_DEFAULTED_IN_CLASS_P (fn
)
2974 && DECL_DECLARED_CONSTEXPR_P (implicit_fn
))
2976 /* Hmm...should we do this for out-of-class too? Should it be OK to
2977 add constexpr later like inline, rather than requiring
2978 declarations to match? */
2979 DECL_DECLARED_CONSTEXPR_P (fn
) = true;
2980 if (kind
== sfk_constructor
)
2981 TYPE_HAS_CONSTEXPR_CTOR (ctx
) = true;
2984 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn
)
2985 && DECL_DECLARED_CONSTEXPR_P (fn
))
2987 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx
))
2989 error ("explicitly defaulted function %q+D cannot be declared "
2990 "%qs because the implicit declaration is not %qs:", fn
,
2991 DECL_IMMEDIATE_FUNCTION_P (fn
) ? "consteval" : "constexpr",
2993 explain_implicit_non_constexpr (fn
);
2995 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
2999 /* Returns true iff FN can be explicitly defaulted, and gives any
3000 errors if defaulting FN is ill-formed. */
3003 defaultable_fn_check (tree fn
)
3005 special_function_kind kind
= sfk_none
;
3007 if (template_parm_scope_p ())
3009 error ("a template cannot be defaulted");
3013 if (DECL_CONSTRUCTOR_P (fn
))
3015 if (FUNCTION_FIRST_USER_PARMTYPE (fn
) == void_list_node
)
3016 kind
= sfk_constructor
;
3017 else if (copy_fn_p (fn
) > 0
3018 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn
))
3020 kind
= sfk_copy_constructor
;
3021 else if (move_fn_p (fn
))
3022 kind
= sfk_move_constructor
;
3024 else if (DECL_DESTRUCTOR_P (fn
))
3025 kind
= sfk_destructor
;
3026 else if (DECL_ASSIGNMENT_OPERATOR_P (fn
)
3027 && DECL_OVERLOADED_OPERATOR_IS (fn
, NOP_EXPR
))
3030 kind
= sfk_copy_assignment
;
3031 else if (move_fn_p (fn
))
3032 kind
= sfk_move_assignment
;
3034 else if (DECL_OVERLOADED_OPERATOR_CODE_RAW (fn
) >= OVL_OP_EQ_EXPR
3035 && DECL_OVERLOADED_OPERATOR_CODE_RAW (fn
) <= OVL_OP_SPACESHIP_EXPR
)
3037 kind
= sfk_comparison
;
3038 if (!early_check_defaulted_comparison (fn
))
3042 if (kind
== sfk_none
)
3044 error ("%qD cannot be defaulted", fn
);
3049 for (tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
3050 t
&& t
!= void_list_node
; t
= TREE_CHAIN (t
))
3051 if (TREE_PURPOSE (t
))
3053 error ("defaulted function %q+D with default argument", fn
);
3057 /* Avoid do_warn_unused_parameter warnings. */
3058 for (tree p
= FUNCTION_FIRST_USER_PARM (fn
); p
; p
= DECL_CHAIN (p
))
3060 TREE_NO_WARNING (p
) = 1;
3062 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
3063 /* Defer checking. */;
3064 else if (!processing_template_decl
)
3065 defaulted_late_check (fn
);
3071 /* Add an implicit declaration to TYPE for the kind of function
3072 indicated by SFK. Return the FUNCTION_DECL for the new implicit
3076 lazily_declare_fn (special_function_kind sfk
, tree type
)
3079 /* Whether or not the argument has a const reference type. */
3080 bool const_p
= false;
3082 type
= TYPE_MAIN_VARIANT (type
);
3086 case sfk_constructor
:
3087 CLASSTYPE_LAZY_DEFAULT_CTOR (type
) = 0;
3089 case sfk_copy_constructor
:
3090 const_p
= TYPE_HAS_CONST_COPY_CTOR (type
);
3091 CLASSTYPE_LAZY_COPY_CTOR (type
) = 0;
3093 case sfk_move_constructor
:
3094 CLASSTYPE_LAZY_MOVE_CTOR (type
) = 0;
3096 case sfk_copy_assignment
:
3097 const_p
= TYPE_HAS_CONST_COPY_ASSIGN (type
);
3098 CLASSTYPE_LAZY_COPY_ASSIGN (type
) = 0;
3100 case sfk_move_assignment
:
3101 CLASSTYPE_LAZY_MOVE_ASSIGN (type
) = 0;
3103 case sfk_destructor
:
3104 CLASSTYPE_LAZY_DESTRUCTOR (type
) = 0;
3110 /* Declare the function. */
3111 fn
= implicitly_declare_fn (sfk
, type
, const_p
, NULL
, NULL
);
3113 /* [class.copy]/8 If the class definition declares a move constructor or
3114 move assignment operator, the implicitly declared copy constructor is
3115 defined as deleted.... */
3116 if ((sfk
== sfk_copy_assignment
|| sfk
== sfk_copy_constructor
)
3117 && cxx_dialect
>= cxx11
)
3119 if (classtype_has_move_assign_or_move_ctor_p (type
, true))
3120 DECL_DELETED_FN (fn
) = true;
3121 else if (classtype_has_depr_implicit_copy (type
))
3122 /* The implicit definition of a copy constructor as defaulted is
3123 deprecated if the class has a user-declared copy assignment operator
3124 or a user-declared destructor. The implicit definition of a copy
3125 assignment operator as defaulted is deprecated if the class has a
3126 user-declared copy constructor or a user-declared destructor (15.4,
3128 TREE_DEPRECATED (fn
) = true;
3131 /* Destructors and assignment operators may be virtual. */
3132 if (sfk
== sfk_destructor
3133 || sfk
== sfk_move_assignment
3134 || sfk
== sfk_copy_assignment
)
3135 check_for_override (fn
, type
);
3137 /* Add it to the class */
3138 bool added
= add_method (type
, fn
, false);
3139 gcc_assert (added
|| errorcount
);
3141 /* Add it to TYPE_FIELDS. */
3142 if (sfk
== sfk_destructor
3143 && DECL_VIRTUAL_P (fn
))
3144 /* The ABI requires that a virtual destructor go at the end of the
3146 TYPE_FIELDS (type
) = chainon (TYPE_FIELDS (type
), fn
);
3149 DECL_CHAIN (fn
) = TYPE_FIELDS (type
);
3150 TYPE_FIELDS (type
) = fn
;
3152 /* Propagate TYPE_FIELDS. */
3153 fixup_type_variants (type
);
3155 maybe_add_class_template_decl_list (type
, fn
, /*friend_p=*/0);
3156 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (fn
))
3157 /* Create appropriate clones. */
3158 clone_cdtor (fn
, /*update_methods=*/true);
3163 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
3164 as there are artificial parms in FN. */
3167 skip_artificial_parms_for (const_tree fn
, tree list
)
3169 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
3170 list
= TREE_CHAIN (list
);
3174 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
3175 list
= TREE_CHAIN (list
);
3176 if (DECL_HAS_VTT_PARM_P (fn
))
3177 list
= TREE_CHAIN (list
);
3181 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
3182 artificial parms in FN. */
3185 num_artificial_parms_for (const_tree fn
)
3189 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
3194 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
3196 if (DECL_HAS_VTT_PARM_P (fn
))
3202 #include "gt-cp-method.h"