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
)))
566 if (FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn
)) == void_list_node
)
567 /* No user-declared parameters to omit. */
569 tree binfo
= inherited_ctor_binfo (fn
);
570 for (; binfo
; binfo
= BINFO_INHERITANCE_CHAIN (binfo
))
571 if (BINFO_VIRTUAL_P (binfo
))
576 /* True iff constructor(s) INH inherited into BINFO initializes INIT_BINFO.
577 This can be true for multiple virtual bases as well as one direct
581 binfo_inherited_from (tree binfo
, tree init_binfo
, tree inh
)
583 /* inh is an OVERLOAD if we inherited the same constructor along
584 multiple paths, check all of them. */
585 for (ovl_iterator
iter (inh
); iter
; ++iter
)
588 tree base
= DECL_CONTEXT (fn
);
589 tree base_binfo
= NULL_TREE
;
590 for (int i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
591 if (BINFO_TYPE (base_binfo
) == base
)
593 if (base_binfo
== init_binfo
594 || (flag_new_inheriting_ctors
595 && binfo_inherited_from (base_binfo
, init_binfo
,
596 DECL_INHERITED_CTOR (fn
))))
602 /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO
603 given the parameter or parameters PARM, possibly inherited constructor
604 base INH, or move flag MOVE_P. */
607 add_one_base_init (tree binfo
, tree parm
, bool move_p
, tree inh
,
608 tree member_init_list
)
613 /* An inheriting constructor only has a mem-initializer for
614 the base it inherits from. */
615 if (!binfo_inherited_from (TYPE_BINFO (current_class_type
), binfo
, inh
))
616 return member_init_list
;
620 for (; parm
; parm
= DECL_CHAIN (parm
))
622 tree exp
= forward_parm (parm
);
623 *p
= build_tree_list (NULL_TREE
, exp
);
624 p
= &TREE_CHAIN (*p
);
629 init
= build_base_path (PLUS_EXPR
, parm
, binfo
, 1,
630 tf_warning_or_error
);
633 init
= build_tree_list (NULL_TREE
, init
);
635 return tree_cons (binfo
, init
, member_init_list
);
638 /* Generate code for default X(X&) or X(X&&) constructor or an inheriting
642 do_build_copy_constructor (tree fndecl
)
644 tree parm
= FUNCTION_FIRST_USER_PARM (fndecl
);
645 bool move_p
= DECL_MOVE_CONSTRUCTOR_P (fndecl
);
646 bool trivial
= trivial_fn_p (fndecl
);
647 tree inh
= DECL_INHERITED_CTOR (fndecl
);
650 parm
= convert_from_reference (parm
);
654 if (is_empty_class (current_class_type
))
655 /* Don't copy the padding byte; it might not have been allocated
656 if *this is a base subobject. */;
657 else if (tree_int_cst_equal (TYPE_SIZE (current_class_type
),
658 CLASSTYPE_SIZE (current_class_type
)))
660 tree t
= build2 (INIT_EXPR
, void_type_node
, current_class_ref
, parm
);
661 finish_expr_stmt (t
);
665 /* We must only copy the non-tail padding parts. */
666 tree base_size
= CLASSTYPE_SIZE_UNIT (current_class_type
);
667 base_size
= size_binop (MINUS_EXPR
, base_size
, size_int (1));
668 tree array_type
= build_array_type (unsigned_char_type_node
,
669 build_index_type (base_size
));
670 tree alias_set
= build_int_cst (TREE_TYPE (current_class_ptr
), 0);
671 tree lhs
= build2 (MEM_REF
, array_type
,
672 current_class_ptr
, alias_set
);
673 tree rhs
= build2 (MEM_REF
, array_type
,
674 TREE_OPERAND (parm
, 0), alias_set
);
675 tree t
= build2 (INIT_EXPR
, void_type_node
, lhs
, rhs
);
676 finish_expr_stmt (t
);
681 tree member_init_list
= NULL_TREE
;
683 tree binfo
, base_binfo
;
684 vec
<tree
, va_gc
> *vbases
;
686 /* Initialize all the base-classes with the parameter converted
687 to their type so that we get their copy constructor and not
688 another constructor that takes current_class_type. We must
689 deal with the binfo's directly as a direct base might be
690 inaccessible due to ambiguity. */
691 for (vbases
= CLASSTYPE_VBASECLASSES (current_class_type
), i
= 0;
692 vec_safe_iterate (vbases
, i
, &binfo
); i
++)
694 member_init_list
= add_one_base_init (binfo
, parm
, move_p
, inh
,
698 for (binfo
= TYPE_BINFO (current_class_type
), i
= 0;
699 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
701 if (BINFO_VIRTUAL_P (base_binfo
))
703 member_init_list
= add_one_base_init (base_binfo
, parm
, move_p
,
704 inh
, member_init_list
);
709 int cvquals
= cp_type_quals (TREE_TYPE (parm
));
711 for (tree fields
= TYPE_FIELDS (current_class_type
);
712 fields
; fields
= DECL_CHAIN (fields
))
717 if (TREE_CODE (field
) != FIELD_DECL
)
720 expr_type
= TREE_TYPE (field
);
721 if (DECL_NAME (field
))
723 if (VFIELD_NAME_P (DECL_NAME (field
)))
726 else if (ANON_AGGR_TYPE_P (expr_type
) && TYPE_FIELDS (expr_type
))
727 /* Just use the field; anonymous types can't have
728 nontrivial copy ctors or assignment ops or this
729 function would be deleted. */;
733 /* Compute the type of "init->field". If the copy-constructor
734 parameter is, for example, "const S&", and the type of
735 the field is "T", then the type will usually be "const
736 T". (There are no cv-qualified variants of reference
738 if (!TYPE_REF_P (expr_type
))
742 if (DECL_MUTABLE_P (field
))
743 quals
&= ~TYPE_QUAL_CONST
;
744 quals
|= cp_type_quals (expr_type
);
745 expr_type
= cp_build_qualified_type (expr_type
, quals
);
748 tree init
= build3 (COMPONENT_REF
, expr_type
, parm
, field
, NULL_TREE
);
749 if (move_p
&& !TYPE_REF_P (expr_type
)
750 /* 'move' breaks bit-fields, and has no effect for scalars. */
751 && !scalarish_type_p (expr_type
))
753 init
= build_tree_list (NULL_TREE
, init
);
755 member_init_list
= tree_cons (field
, init
, member_init_list
);
759 finish_mem_initializers (member_init_list
);
764 do_build_copy_assign (tree fndecl
)
766 tree parm
= DECL_CHAIN (DECL_ARGUMENTS (fndecl
));
768 bool move_p
= move_fn_p (fndecl
);
769 bool trivial
= trivial_fn_p (fndecl
);
770 int flags
= LOOKUP_NORMAL
| LOOKUP_NONVIRTUAL
| LOOKUP_DEFAULTED
;
772 compound_stmt
= begin_compound_stmt (0);
773 parm
= convert_from_reference (parm
);
776 && is_empty_class (current_class_type
))
777 /* Don't copy the padding byte; it might not have been allocated
778 if *this is a base subobject. */;
781 tree t
= build2 (MODIFY_EXPR
, void_type_node
, current_class_ref
, parm
);
782 finish_expr_stmt (t
);
787 int cvquals
= cp_type_quals (TREE_TYPE (parm
));
789 tree binfo
, base_binfo
;
791 /* Assign to each of the direct base classes. */
792 for (binfo
= TYPE_BINFO (current_class_type
), i
= 0;
793 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
797 /* We must convert PARM directly to the base class
798 explicitly since the base class may be ambiguous. */
799 converted_parm
= build_base_path (PLUS_EXPR
, parm
, base_binfo
, 1,
800 tf_warning_or_error
);
802 converted_parm
= move (converted_parm
);
803 /* Call the base class assignment operator. */
804 releasing_vec
parmvec (make_tree_vector_single (converted_parm
));
806 (build_special_member_call (current_class_ref
,
807 assign_op_identifier
,
811 tf_warning_or_error
));
814 /* Assign to each of the non-static data members. */
815 for (fields
= TYPE_FIELDS (current_class_type
);
817 fields
= DECL_CHAIN (fields
))
819 tree comp
= current_class_ref
;
825 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
828 expr_type
= TREE_TYPE (field
);
830 if (CP_TYPE_CONST_P (expr_type
))
832 error ("non-static const member %q#D, cannot use default "
833 "assignment operator", field
);
836 else if (TYPE_REF_P (expr_type
))
838 error ("non-static reference member %q#D, cannot use "
839 "default assignment operator", field
);
843 if (DECL_NAME (field
))
845 if (VFIELD_NAME_P (DECL_NAME (field
)))
848 else if (ANON_AGGR_TYPE_P (expr_type
)
849 && TYPE_FIELDS (expr_type
) != NULL_TREE
)
850 /* Just use the field; anonymous types can't have
851 nontrivial copy ctors or assignment ops or this
852 function would be deleted. */;
856 comp
= build3 (COMPONENT_REF
, expr_type
, comp
, field
, NULL_TREE
);
858 /* Compute the type of init->field */
860 if (DECL_MUTABLE_P (field
))
861 quals
&= ~TYPE_QUAL_CONST
;
862 expr_type
= cp_build_qualified_type (expr_type
, quals
);
864 init
= build3 (COMPONENT_REF
, expr_type
, init
, field
, NULL_TREE
);
865 if (move_p
&& !TYPE_REF_P (expr_type
)
866 /* 'move' breaks bit-fields, and has no effect for scalars. */
867 && !scalarish_type_p (expr_type
))
870 if (DECL_NAME (field
))
871 init
= cp_build_modify_expr (input_location
, comp
, NOP_EXPR
, init
,
872 tf_warning_or_error
);
874 init
= build2 (MODIFY_EXPR
, TREE_TYPE (comp
), comp
, init
);
875 finish_expr_stmt (init
);
878 finish_return_stmt (current_class_ref
);
879 finish_compound_stmt (compound_stmt
);
882 /* C++20 <compare> comparison category types. */
892 /* Names of the comparison categories and their value members, to be indexed by
893 comp_cat_tag enumerators. genericize_spaceship below relies on the ordering
896 struct comp_cat_info_t
899 const char *members
[4];
901 static const comp_cat_info_t comp_cat_info
[cc_last
]
903 { "partial_ordering", { "equivalent", "greater", "less", "unordered" } },
904 { "weak_ordering", { "equivalent", "greater", "less" } },
905 { "strong_ordering", { "equal", "greater", "less" } }
908 /* A cache of the category types to speed repeated lookups. */
910 static GTY((deletable
)) tree comp_cat_cache
[cc_last
];
912 /* Look up one of the result variables in the comparison category type. */
915 lookup_comparison_result (tree type
, const char *name_str
,
916 tsubst_flags_t complain
= tf_warning_or_error
)
918 tree name
= get_identifier (name_str
);
919 tree decl
= lookup_qualified_name (type
, name
);
920 if (TREE_CODE (decl
) != VAR_DECL
)
922 if (complain
& tf_error
)
924 auto_diagnostic_group d
;
925 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
926 qualified_name_lookup_error (type
, name
, decl
, input_location
);
928 error ("%qD is not a static data member", decl
);
929 inform (input_location
, "determining value of %qs", "operator<=>");
931 return error_mark_node
;
936 /* Look up a <compare> comparison category type in std. */
939 lookup_comparison_category (comp_cat_tag tag
,
940 tsubst_flags_t complain
= tf_warning_or_error
)
942 if (tree cached
= comp_cat_cache
[tag
])
945 tree name
= get_identifier (comp_cat_info
[tag
].name
);
946 tree decl
= lookup_qualified_name (std_node
, name
);
947 if (TREE_CODE (decl
) != TYPE_DECL
)
949 if (complain
& tf_error
)
951 auto_diagnostic_group d
;
952 if (decl
== error_mark_node
|| TREE_CODE (decl
) == TREE_LIST
)
953 qualified_name_lookup_error (std_node
, name
, decl
, input_location
);
955 error ("%qD is not a type", decl
);
956 inform (input_location
, "forming type of %qs", "operator<=>");
958 return error_mark_node
;
960 /* Also make sure we can look up the value members now, since we won't
961 really use them until genericize time. */
962 tree type
= TREE_TYPE (decl
);
963 for (int i
= 0; i
< 4; ++i
)
965 const char *p
= comp_cat_info
[tag
].members
[i
];
967 if (lookup_comparison_result (type
, p
, complain
)
969 return error_mark_node
;
971 return comp_cat_cache
[tag
] = type
;
974 /* Wrapper that takes the tag rather than the type. */
977 lookup_comparison_result (comp_cat_tag tag
, const char *name_str
,
978 tsubst_flags_t complain
= tf_warning_or_error
)
980 tree type
= lookup_comparison_category (tag
, complain
);
981 return lookup_comparison_result (type
, name_str
, complain
);
984 /* Wrapper that takes the index into the members array instead of the name. */
987 lookup_comparison_result (comp_cat_tag tag
, tree type
, int idx
)
989 const char *name_str
= comp_cat_info
[tag
].members
[idx
];
992 return lookup_comparison_result (type
, name_str
);
995 /* Does TYPE correspond to TAG? */
998 is_cat (tree type
, comp_cat_tag tag
)
1000 tree name
= TYPE_LINKAGE_IDENTIFIER (type
);
1001 return id_equal (name
, comp_cat_info
[tag
].name
);
1004 /* Return the comp_cat_tag for TYPE. */
1007 cat_tag_for (tree type
)
1009 for (int i
= 0; i
< cc_last
; ++i
)
1011 comp_cat_tag tag
= (comp_cat_tag
)i
;
1012 if (is_cat (type
, tag
))
1018 /* Return the comparison category tag of a <=> expression with non-class type
1022 spaceship_comp_cat (tree optype
)
1024 if (INTEGRAL_OR_ENUMERATION_TYPE_P (optype
) || TYPE_PTROBV_P (optype
))
1025 return cc_strong_ordering
;
1026 else if (TREE_CODE (optype
) == REAL_TYPE
)
1027 return cc_partial_ordering
;
1029 /* ??? should vector <=> produce a vector of one of the above? */
1033 /* Return the comparison category type of a <=> expression with non-class type
1037 spaceship_type (tree optype
, tsubst_flags_t complain
)
1039 comp_cat_tag tag
= spaceship_comp_cat (optype
);
1040 return lookup_comparison_category (tag
, complain
);
1043 /* Turn <=> with type TYPE and operands OP0 and OP1 into GENERIC. */
1046 genericize_spaceship (tree type
, tree op0
, tree op1
)
1048 /* ??? maybe optimize based on knowledge of representation? */
1049 comp_cat_tag tag
= cat_tag_for (type
);
1050 gcc_checking_assert (tag
< cc_last
);
1053 op0
= save_expr (op0
);
1054 op1
= save_expr (op1
);
1056 tree gt
= lookup_comparison_result (tag
, type
, 1);
1058 if (tag
== cc_partial_ordering
)
1060 /* op0 == op1 ? equivalent : op0 < op1 ? less :
1061 op0 > op1 ? greater : unordered */
1062 tree uo
= lookup_comparison_result (tag
, type
, 3);
1063 tree comp
= fold_build2 (GT_EXPR
, boolean_type_node
, op0
, op1
);
1064 r
= fold_build3 (COND_EXPR
, type
, comp
, gt
, uo
);
1067 /* op0 == op1 ? equal : op0 < op1 ? less : greater */
1070 tree lt
= lookup_comparison_result (tag
, type
, 2);
1071 tree comp
= fold_build2 (LT_EXPR
, boolean_type_node
, op0
, op1
);
1072 r
= fold_build3 (COND_EXPR
, type
, comp
, lt
, r
);
1074 tree eq
= lookup_comparison_result (tag
, type
, 0);
1075 comp
= fold_build2 (EQ_EXPR
, boolean_type_node
, op0
, op1
);
1076 r
= fold_build3 (COND_EXPR
, type
, comp
, eq
, r
);
1078 /* Wrap the whole thing in a TARGET_EXPR like build_conditional_expr_1. */
1079 r
= get_target_expr (r
);
1084 /* Check that the signature of a defaulted comparison operator is
1088 early_check_defaulted_comparison (tree fn
)
1090 location_t loc
= DECL_SOURCE_LOCATION (fn
);
1092 if (DECL_CLASS_SCOPE_P (fn
))
1093 ctx
= DECL_CONTEXT (fn
);
1095 ctx
= DECL_FRIEND_CONTEXT (fn
);
1098 if (cxx_dialect
< cxx2a
)
1100 error_at (loc
, "defaulted %qD only available with %<-std=c++2a%> or "
1101 "%<-std=gnu++2a%>", fn
);
1105 if (!DECL_OVERLOADED_OPERATOR_IS (fn
, SPACESHIP_EXPR
)
1106 && !same_type_p (TREE_TYPE (TREE_TYPE (fn
)), boolean_type_node
))
1108 diagnostic_t kind
= DK_UNSPECIFIED
;
1110 if (is_auto (TREE_TYPE (fn
)))
1114 emit_diagnostic (kind
, loc
, opt
,
1115 "defaulted %qD must return %<bool%>", fn
);
1116 if (kind
== DK_ERROR
)
1120 bool mem
= DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
);
1121 if (mem
&& type_memfn_quals (TREE_TYPE (fn
)) != TYPE_QUAL_CONST
)
1123 error_at (loc
, "defaulted %qD must be %<const%>", fn
);
1126 tree parmnode
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
1127 bool saw_byval
= false;
1128 bool saw_byref
= mem
;
1129 bool saw_bad
= false;
1130 for (; parmnode
!= void_list_node
; parmnode
= TREE_CHAIN (parmnode
))
1132 tree parmtype
= TREE_VALUE (parmnode
);
1133 if (same_type_p (parmtype
, ctx
))
1135 else if (TREE_CODE (parmtype
) != REFERENCE_TYPE
1136 || TYPE_QUALS (TREE_TYPE (parmtype
)) != TYPE_QUAL_CONST
1137 || !(same_type_ignoring_top_level_qualifiers_p
1138 (TREE_TYPE (parmtype
), ctx
)))
1144 if (saw_bad
|| (saw_byval
&& saw_byref
))
1146 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
1147 error_at (loc
, "defaulted member %qD must have parameter type "
1148 "%<const %T&%>", fn
, ctx
);
1150 error_at (loc
, "defaulted %qD must have parameters of either type "
1151 "%<const %T&%> or %qT", fn
, ctx
, ctx
);
1153 error_at (loc
, "defaulted %qD must have parameters of either type "
1154 "%<const %T&%> or %qT, not both", fn
, ctx
, ctx
);
1158 /* We still need to deduce deleted/constexpr/noexcept and maybe return. */
1159 DECL_MAYBE_DELETED (fn
) = ok
;
1164 /* Subroutine of build_comparison_op. Given the vec of memberwise
1165 comparisons COMPS, calculate the overall comparison category for
1169 common_comparison_type (vec
<tree
> &comps
)
1171 tree seen
[cc_last
] = {};
1173 for (unsigned i
= 0; i
< comps
.length(); ++i
)
1175 tree comp
= comps
[i
];
1176 tree ctype
= TREE_TYPE (comp
);
1177 comp_cat_tag tag
= cat_tag_for (ctype
);
1181 /* If any Ti is not a comparison category type, U is void. */
1182 return void_type_node
;
1185 /* Otherwise, if at least one T i is std::partial_ordering, U is
1186 std::partial_ordering. */
1187 if (tree t
= seen
[cc_partial_ordering
]) return t
;
1189 /* Otherwise, if at least one T i is std::weak_ordering, U is
1190 std::weak_ordering. */
1191 if (tree t
= seen
[cc_weak_ordering
]) return t
;
1193 /* Otherwise, U is std::strong_ordering. */
1194 if (tree t
= seen
[cc_strong_ordering
]) return t
;
1195 return lookup_comparison_category (cc_strong_ordering
);
1198 /* Data structure for build_comparison_op. */
1210 comp_info (tree fndecl
, tsubst_flags_t
&complain
)
1213 loc
= DECL_SOURCE_LOCATION (fndecl
);
1215 /* We only have tf_error set when we're called from
1216 explain_invalid_constexpr_fn or maybe_explain_implicit_delete. */
1217 defining
= !(complain
& tf_error
);
1219 first_time
= DECL_MAYBE_DELETED (fndecl
);
1220 DECL_MAYBE_DELETED (fndecl
) = false;
1222 /* Do we want to try to set constexpr? */
1223 was_constexp
= DECL_DECLARED_CONSTEXPR_P (fndecl
);
1224 constexp
= first_time
;
1226 /* Set this for var_in_constexpr_fn. */
1227 DECL_DECLARED_CONSTEXPR_P (fndecl
) = true;
1229 /* Do we want to try to set noexcept? */
1233 tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fndecl
));
1234 if (raises
&& !UNEVALUATED_NOEXCEPT_SPEC_P (raises
))
1235 /* There was an explicit exception-specification. */
1240 /* EXPR is an expression built as part of the function body.
1241 Adjust the properties appropriately. */
1242 void check (tree expr
)
1244 if (expr
== error_mark_node
)
1245 DECL_DELETED_FN (fndecl
) = true;
1246 if ((constexp
|| was_constexp
)
1247 && !potential_rvalue_constant_expression (expr
))
1250 require_potential_rvalue_constant_expression (expr
);
1254 if (noex
&& !expr_noexcept_p (expr
, tf_none
))
1262 DECL_DECLARED_CONSTEXPR_P (fndecl
) = constexp
|| was_constexp
;
1263 tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fndecl
));
1264 if (!raises
|| UNEVALUATED_NOEXCEPT_SPEC_P (raises
))
1266 raises
= noex
? noexcept_true_spec
: noexcept_false_spec
;
1267 TREE_TYPE (fndecl
) = build_exception_variant (TREE_TYPE (fndecl
),
1274 /* Build up the definition of a defaulted comparison operator. Unlike other
1275 defaulted functions that use synthesized_method_walk to determine whether
1276 the function is e.g. deleted, for comparisons we use the same code. We try
1277 to use synthesize_method at the earliest opportunity and bail out if the
1278 function ends up being deleted. */
1281 build_comparison_op (tree fndecl
, tsubst_flags_t complain
)
1283 comp_info
info (fndecl
, complain
);
1285 if (!info
.defining
&& !(complain
& tf_error
) && !DECL_MAYBE_DELETED (fndecl
))
1288 int flags
= LOOKUP_NORMAL
| LOOKUP_NONVIRTUAL
| LOOKUP_DEFAULTED
;
1289 const ovl_op_info_t
*op
= IDENTIFIER_OVL_OP_INFO (DECL_NAME (fndecl
));
1290 tree_code code
= op
->tree_code
;
1292 tree lhs
= DECL_ARGUMENTS (fndecl
);
1293 tree rhs
= DECL_CHAIN (lhs
);
1294 if (is_this_parameter (lhs
))
1295 lhs
= cp_build_fold_indirect_ref (lhs
);
1297 lhs
= convert_from_reference (lhs
);
1298 rhs
= convert_from_reference (rhs
);
1299 tree ctype
= TYPE_MAIN_VARIANT (TREE_TYPE (lhs
));
1301 iloc_sentinel
ils (info
.loc
);
1303 /* A defaulted comparison operator function for class C is defined as
1304 deleted if ... C is a union-like class. */
1305 if (TREE_CODE (ctype
) == UNION_TYPE
)
1307 if (complain
& tf_error
)
1308 inform (info
.loc
, "cannot default compare union %qT", ctype
);
1309 DECL_DELETED_FN (fndecl
) = true;
1313 tree compound_stmt
= NULL_TREE
;
1315 compound_stmt
= begin_compound_stmt (0);
1317 ++cp_unevaluated_operand
;
1319 tree rettype
= TREE_TYPE (TREE_TYPE (fndecl
));
1320 if (code
!= SPACESHIP_EXPR
&& is_auto (rettype
))
1322 rettype
= boolean_type_node
;
1323 apply_deduced_return_type (fndecl
, rettype
);
1326 if (code
== EQ_EXPR
|| code
== SPACESHIP_EXPR
)
1328 auto_vec
<tree
> comps
;
1330 /* Compare each of the subobjects. Note that we get bases from
1331 next_initializable_field because we're past C++17. */
1332 for (tree field
= next_initializable_field (TYPE_FIELDS (ctype
));
1334 field
= next_initializable_field (DECL_CHAIN (field
)))
1336 tree expr_type
= TREE_TYPE (field
);
1338 /* A defaulted comparison operator function for class C is defined as
1339 deleted if any non-static data member of C is of reference type or
1340 C is a union-like class. */
1341 if (TREE_CODE (expr_type
) == REFERENCE_TYPE
)
1343 if (complain
& tf_error
)
1344 inform (DECL_SOURCE_LOCATION (field
), "cannot default compare "
1345 "reference member %qD", field
);
1346 DECL_DELETED_FN (fndecl
) = true;
1349 else if (ANON_UNION_TYPE_P (expr_type
))
1351 if (complain
& tf_error
)
1352 inform (DECL_SOURCE_LOCATION (field
), "cannot default compare "
1353 "anonymous union member");
1354 DECL_DELETED_FN (fndecl
) = true;
1358 tree lhs_mem
= build3 (COMPONENT_REF
, expr_type
, lhs
, field
,
1360 tree rhs_mem
= build3 (COMPONENT_REF
, expr_type
, rhs
, field
,
1362 tree comp
= build_new_op (info
.loc
, code
, flags
, lhs_mem
, rhs_mem
,
1363 NULL_TREE
, NULL
, complain
);
1364 if (comp
== error_mark_node
)
1366 DECL_DELETED_FN (fndecl
) = true;
1369 comps
.safe_push (comp
);
1371 if (code
== SPACESHIP_EXPR
&& is_auto (rettype
))
1373 rettype
= common_comparison_type (comps
);
1374 apply_deduced_return_type (fndecl
, rettype
);
1376 for (unsigned i
= 0; i
< comps
.length(); ++i
)
1378 tree comp
= comps
[i
];
1379 tree eq
, retval
= NULL_TREE
, if_
= NULL_TREE
;
1381 if_
= begin_if_stmt ();
1382 /* Spaceship is specified to use !=, but for the comparison category
1383 types, != is equivalent to !(==), so let's use == directly. */
1384 if (code
== EQ_EXPR
)
1386 /* if (x==y); else return false; */
1388 retval
= boolean_false_node
;
1392 /* if (auto v = x<=>y, v == 0); else return v; */
1393 if (TREE_CODE (comp
) == SPACESHIP_EXPR
)
1394 TREE_TYPE (comp
) = rettype
;
1396 comp
= build_static_cast (input_location
, rettype
, comp
,
1401 tree var
= create_temporary_var (rettype
);
1403 cp_finish_decl (var
, comp
, false, NULL_TREE
, flags
);
1404 comp
= retval
= var
;
1406 eq
= build_new_op (info
.loc
, EQ_EXPR
, flags
, comp
,
1407 integer_zero_node
, NULL_TREE
, NULL
,
1410 tree ceq
= contextual_conv_bool (eq
, complain
);
1414 finish_if_stmt_cond (ceq
, if_
);
1415 finish_then_clause (if_
);
1416 begin_else_clause (if_
);
1417 finish_return_stmt (retval
);
1418 finish_else_clause (if_
);
1419 finish_if_stmt (if_
);
1425 if (code
== EQ_EXPR
)
1426 val
= boolean_true_node
;
1429 tree seql
= lookup_comparison_result (cc_strong_ordering
,
1431 val
= build_static_cast (input_location
, rettype
, seql
,
1434 finish_return_stmt (val
);
1437 else if (code
== NE_EXPR
)
1439 tree comp
= build_new_op (info
.loc
, EQ_EXPR
, flags
, lhs
, rhs
,
1440 NULL_TREE
, NULL
, complain
);
1441 comp
= contextual_conv_bool (comp
, complain
);
1445 tree neg
= build1 (TRUTH_NOT_EXPR
, boolean_type_node
, comp
);
1446 finish_return_stmt (neg
);
1451 tree comp
= build_new_op (info
.loc
, SPACESHIP_EXPR
, flags
, lhs
, rhs
,
1452 NULL_TREE
, NULL
, complain
);
1453 tree comp2
= build_new_op (info
.loc
, code
, flags
, comp
, integer_zero_node
,
1454 NULL_TREE
, NULL
, complain
);
1457 finish_return_stmt (comp2
);
1461 finish_compound_stmt (compound_stmt
);
1463 --cp_unevaluated_operand
;
1466 /* True iff DECL is an implicitly-declared special member function with no real
1467 source location, so we can use its DECL_SOURCE_LOCATION to remember where we
1468 triggered its synthesis. */
1471 decl_remember_implicit_trigger_p (tree decl
)
1473 if (!DECL_ARTIFICIAL (decl
))
1475 special_function_kind sfk
= special_function_p (decl
);
1476 /* Inherited constructors have the location of their using-declaration, and
1477 operator== has the location of the corresponding operator<=>. */
1478 return (sfk
!= sfk_inheriting_constructor
1479 && sfk
!= sfk_comparison
);
1482 /* Synthesize FNDECL, a non-static member function. */
1485 synthesize_method (tree fndecl
)
1487 bool nested
= (current_function_decl
!= NULL_TREE
);
1488 tree context
= decl_function_context (fndecl
);
1489 bool need_body
= true;
1491 location_t save_input_location
= input_location
;
1492 int error_count
= errorcount
;
1493 int warning_count
= warningcount
+ werrorcount
;
1494 special_function_kind sfk
= special_function_p (fndecl
);
1496 /* Reset the source location, we might have been previously
1497 deferred, and thus have saved where we were first needed. */
1498 if (decl_remember_implicit_trigger_p (fndecl
))
1499 DECL_SOURCE_LOCATION (fndecl
)
1500 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl
)));
1502 /* If we've been asked to synthesize a clone, just synthesize the
1503 cloned function instead. Doing so will automatically fill in the
1504 body for the clone. */
1505 if (DECL_CLONED_FUNCTION_P (fndecl
))
1506 fndecl
= DECL_CLONED_FUNCTION (fndecl
);
1508 /* We may be in the middle of deferred access check. Disable
1510 push_deferring_access_checks (dk_no_deferred
);
1513 push_to_top_level ();
1515 push_function_context ();
1517 input_location
= DECL_SOURCE_LOCATION (fndecl
);
1519 start_preparsed_function (fndecl
, NULL_TREE
, SF_DEFAULT
| SF_PRE_PARSED
);
1520 stmt
= begin_function_body ();
1522 if (DECL_ASSIGNMENT_OPERATOR_P (fndecl
)
1523 && DECL_OVERLOADED_OPERATOR_IS (fndecl
, NOP_EXPR
))
1525 do_build_copy_assign (fndecl
);
1528 else if (DECL_CONSTRUCTOR_P (fndecl
))
1530 tree arg_chain
= FUNCTION_FIRST_USER_PARMTYPE (fndecl
);
1531 if (arg_chain
!= void_list_node
)
1532 do_build_copy_constructor (fndecl
);
1534 finish_mem_initializers (NULL_TREE
);
1536 else if (sfk
== sfk_comparison
)
1538 /* Pass tf_none so the function is just deleted if there's a problem. */
1539 build_comparison_op (fndecl
, tf_none
);
1543 /* If we haven't yet generated the body of the function, just
1544 generate an empty compound statement. */
1548 compound_stmt
= begin_compound_stmt (BCS_FN_BODY
);
1549 finish_compound_stmt (compound_stmt
);
1552 finish_function_body (stmt
);
1553 finish_function (/*inline_p=*/false);
1555 if (!DECL_DELETED_FN (fndecl
))
1556 expand_or_defer_fn (fndecl
);
1558 input_location
= save_input_location
;
1561 pop_from_top_level ();
1563 pop_function_context ();
1565 pop_deferring_access_checks ();
1567 if (error_count
!= errorcount
|| warning_count
!= warningcount
+ werrorcount
)
1568 if (DECL_ARTIFICIAL (fndecl
))
1569 inform (input_location
, "synthesized method %qD first required here",
1573 /* Build a reference to type TYPE with cv-quals QUALS, which is an
1574 rvalue if RVALUE is true. */
1577 build_stub_type (tree type
, int quals
, bool rvalue
)
1579 tree argtype
= cp_build_qualified_type (type
, quals
);
1580 return cp_build_reference_type (argtype
, rvalue
);
1583 /* Build a dummy glvalue from dereferencing a dummy reference of type
1587 build_stub_object (tree reftype
)
1589 if (!TYPE_REF_P (reftype
))
1590 reftype
= cp_build_reference_type (reftype
, /*rval*/true);
1591 tree stub
= build1 (CONVERT_EXPR
, reftype
, integer_one_node
);
1592 return convert_from_reference (stub
);
1595 /* Determine which function will be called when looking up NAME in TYPE,
1596 called with a single ARGTYPE argument, or no argument if ARGTYPE is
1597 null. FLAGS and COMPLAIN are as for build_new_method_call.
1599 Returns a FUNCTION_DECL if all is well.
1600 Returns NULL_TREE if overload resolution failed.
1601 Returns error_mark_node if the chosen function cannot be called. */
1604 locate_fn_flags (tree type
, tree name
, tree argtype
, int flags
,
1605 tsubst_flags_t complain
)
1607 tree ob
, fn
, fns
, binfo
, rval
;
1610 binfo
= TYPE_BINFO (type
);
1614 type
= BINFO_TYPE (binfo
);
1617 ob
= build_stub_object (cp_build_reference_type (type
, false));
1621 if (TREE_CODE (argtype
) == TREE_LIST
)
1623 for (tree elt
= argtype
; elt
&& elt
!= void_list_node
;
1624 elt
= TREE_CHAIN (elt
))
1626 tree type
= TREE_VALUE (elt
);
1627 tree arg
= build_stub_object (type
);
1628 vec_safe_push (args
, arg
);
1633 tree arg
= build_stub_object (argtype
);
1634 args
->quick_push (arg
);
1638 fns
= lookup_fnfields (binfo
, name
, 0);
1639 rval
= build_new_method_call (ob
, fns
, &args
, binfo
, flags
, &fn
, complain
);
1641 if (fn
&& rval
== error_mark_node
)
1647 /* Locate the dtor of TYPE. */
1650 get_dtor (tree type
, tsubst_flags_t complain
)
1652 tree fn
= locate_fn_flags (type
, complete_dtor_identifier
, NULL_TREE
,
1653 LOOKUP_NORMAL
, complain
);
1654 if (fn
== error_mark_node
)
1659 /* Locate the default ctor of TYPE. */
1662 locate_ctor (tree type
)
1666 push_deferring_access_checks (dk_no_check
);
1667 fn
= locate_fn_flags (type
, complete_ctor_identifier
, NULL_TREE
,
1668 LOOKUP_SPECULATIVE
, tf_none
);
1669 pop_deferring_access_checks ();
1670 if (fn
== error_mark_node
)
1675 /* Likewise, but give any appropriate errors. */
1678 get_default_ctor (tree type
)
1680 tree fn
= locate_fn_flags (type
, complete_ctor_identifier
, NULL_TREE
,
1681 LOOKUP_NORMAL
, tf_warning_or_error
);
1682 if (fn
== error_mark_node
)
1687 /* Locate the copy ctor of TYPE. */
1690 get_copy_ctor (tree type
, tsubst_flags_t complain
)
1692 int quals
= (TYPE_HAS_CONST_COPY_CTOR (type
)
1693 ? TYPE_QUAL_CONST
: TYPE_UNQUALIFIED
);
1694 tree argtype
= build_stub_type (type
, quals
, false);
1695 tree fn
= locate_fn_flags (type
, complete_ctor_identifier
, argtype
,
1696 LOOKUP_NORMAL
, complain
);
1697 if (fn
== error_mark_node
)
1702 /* Locate the copy assignment operator of TYPE. */
1705 get_copy_assign (tree type
)
1707 int quals
= (TYPE_HAS_CONST_COPY_ASSIGN (type
)
1708 ? TYPE_QUAL_CONST
: TYPE_UNQUALIFIED
);
1709 tree argtype
= build_stub_type (type
, quals
, false);
1710 tree fn
= locate_fn_flags (type
, assign_op_identifier
, argtype
,
1711 LOOKUP_NORMAL
, tf_warning_or_error
);
1712 if (fn
== error_mark_node
)
1717 /* walk_tree helper function for is_trivially_xible. If *TP is a call,
1718 return it if it calls something other than a trivial special member
1722 check_nontriv (tree
*tp
, int *, void *)
1724 tree fn
= cp_get_callee (*tp
);
1725 if (fn
== NULL_TREE
)
1728 if (TREE_CODE (fn
) == ADDR_EXPR
)
1729 fn
= TREE_OPERAND (fn
, 0);
1731 if (TREE_CODE (fn
) != FUNCTION_DECL
1732 || !trivial_fn_p (fn
))
1737 /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */
1740 assignable_expr (tree to
, tree from
)
1742 ++cp_unevaluated_operand
;
1743 to
= build_stub_object (to
);
1744 from
= build_stub_object (from
);
1745 tree r
= cp_build_modify_expr (input_location
, to
, NOP_EXPR
, from
, tf_none
);
1746 --cp_unevaluated_operand
;
1750 /* The predicate condition for a template specialization
1751 is_constructible<T, Args...> shall be satisfied if and only if the
1752 following variable definition would be well-formed for some invented
1753 variable t: T t(create<Args>()...);
1755 Return something equivalent in well-formedness and triviality. */
1758 constructible_expr (tree to
, tree from
)
1761 cp_unevaluated cp_uneval_guard
;
1762 if (CLASS_TYPE_P (to
))
1765 vec
<tree
, va_gc
> *args
= NULL
;
1766 if (!TYPE_REF_P (to
))
1767 to
= cp_build_reference_type (to
, /*rval*/false);
1768 tree ob
= build_stub_object (to
);
1769 for (; from
; from
= TREE_CHAIN (from
))
1770 vec_safe_push (args
, build_stub_object (TREE_VALUE (from
)));
1771 expr
= build_special_member_call (ob
, complete_ctor_identifier
, &args
,
1772 ctype
, LOOKUP_NORMAL
, tf_none
);
1773 if (expr
== error_mark_node
)
1774 return error_mark_node
;
1775 /* The current state of the standard vis-a-vis LWG 2116 is that
1776 is_*constructible involves destruction as well. */
1777 if (type_build_dtor_call (ctype
))
1779 tree dtor
= build_special_member_call (ob
, complete_dtor_identifier
,
1780 NULL
, ctype
, LOOKUP_NORMAL
,
1782 if (dtor
== error_mark_node
)
1783 return error_mark_node
;
1784 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype
))
1785 expr
= build2 (COMPOUND_EXPR
, void_type_node
, expr
, dtor
);
1790 if (from
== NULL_TREE
)
1791 return build_value_init (strip_array_types (to
), tf_none
);
1792 else if (TREE_CHAIN (from
))
1793 return error_mark_node
; // too many initializers
1794 from
= build_stub_object (TREE_VALUE (from
));
1795 expr
= perform_direct_initialization_if_possible (to
, from
,
1802 /* Returns a tree iff TO is assignable (if CODE is MODIFY_EXPR) or
1803 constructible (otherwise) from FROM, which is a single type for
1804 assignment or a list of types for construction. */
1807 is_xible_helper (enum tree_code code
, tree to
, tree from
, bool trivial
)
1809 if (VOID_TYPE_P (to
) || ABSTRACT_CLASS_TYPE_P (to
)
1810 || (from
&& FUNC_OR_METHOD_TYPE_P (from
)
1811 && (TYPE_READONLY (from
) || FUNCTION_REF_QUALIFIED (from
))))
1812 return error_mark_node
;
1814 if (code
== MODIFY_EXPR
)
1815 expr
= assignable_expr (to
, from
);
1816 else if (trivial
&& from
&& TREE_CHAIN (from
))
1817 return error_mark_node
; // only 0- and 1-argument ctors can be trivial
1818 else if (TREE_CODE (to
) == ARRAY_TYPE
&& !TYPE_DOMAIN (to
))
1819 return error_mark_node
; // can't construct an array of unknown bound
1821 expr
= constructible_expr (to
, from
);
1825 /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or
1826 constructible (otherwise) from FROM, which is a single type for
1827 assignment or a list of types for construction. */
1830 is_trivially_xible (enum tree_code code
, tree to
, tree from
)
1833 expr
= is_xible_helper (code
, to
, from
, /*trivial*/true);
1835 if (expr
== NULL_TREE
|| expr
== error_mark_node
)
1837 tree nt
= cp_walk_tree_without_duplicates (&expr
, check_nontriv
, NULL
);
1841 /* Returns true iff TO is assignable (if CODE is MODIFY_EXPR) or
1842 constructible (otherwise) from FROM, which is a single type for
1843 assignment or a list of types for construction. */
1846 is_xible (enum tree_code code
, tree to
, tree from
)
1848 tree expr
= is_xible_helper (code
, to
, from
, /*trivial*/false);
1849 if (expr
== error_mark_node
)
1854 /* Categorize various special_function_kinds. */
1855 #define SFK_CTOR_P(sfk) \
1856 ((sfk) >= sfk_constructor && (sfk) <= sfk_move_constructor)
1857 #define SFK_DTOR_P(sfk) \
1858 ((sfk) == sfk_destructor || (sfk) == sfk_virtual_destructor)
1859 #define SFK_ASSIGN_P(sfk) \
1860 ((sfk) == sfk_copy_assignment || (sfk) == sfk_move_assignment)
1861 #define SFK_COPY_P(sfk) \
1862 ((sfk) == sfk_copy_constructor || (sfk) == sfk_copy_assignment)
1863 #define SFK_MOVE_P(sfk) \
1864 ((sfk) == sfk_move_constructor || (sfk) == sfk_move_assignment)
1866 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
1867 DELETED_P or give an error message MSG with argument ARG. */
1870 process_subob_fn (tree fn
, special_function_kind sfk
, tree
*spec_p
,
1871 bool *trivial_p
, bool *deleted_p
, bool *constexpr_p
,
1872 bool diag
, tree arg
, bool dtor_from_ctor
= false)
1874 if (!fn
|| fn
== error_mark_node
)
1883 if (!maybe_instantiate_noexcept (fn
))
1884 *spec_p
= error_mark_node
;
1887 tree raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
));
1888 *spec_p
= merge_exception_specifiers (*spec_p
, raises
);
1892 if (!trivial_fn_p (fn
) && !dtor_from_ctor
)
1896 if (TREE_CODE (arg
) == FIELD_DECL
1897 && TREE_CODE (DECL_CONTEXT (arg
)) == UNION_TYPE
)
1902 error ("union member %q+D with non-trivial %qD", arg
, fn
);
1906 if (constexpr_p
&& !DECL_DECLARED_CONSTEXPR_P (fn
))
1908 *constexpr_p
= false;
1911 inform (DECL_SOURCE_LOCATION (fn
),
1913 ? G_("defaulted destructor calls non-%<constexpr%> %qD")
1914 : G_("defaulted constructor calls non-%<constexpr%> %qD"),
1916 explain_invalid_constexpr_fn (fn
);
1921 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
1922 aggregates. If DTOR_FROM_CTOR is true, we're walking subobject destructors
1923 called from a synthesized constructor, in which case we don't consider
1924 the triviality of the subobject destructor. */
1927 walk_field_subobs (tree fields
, special_function_kind sfk
, tree fnname
,
1928 int quals
, tree
*spec_p
, bool *trivial_p
,
1929 bool *deleted_p
, bool *constexpr_p
,
1930 bool diag
, int flags
, tsubst_flags_t complain
,
1931 bool dtor_from_ctor
)
1934 for (field
= fields
; field
; field
= DECL_CHAIN (field
))
1936 tree mem_type
, argtype
, rval
;
1938 if (TREE_CODE (field
) != FIELD_DECL
|| DECL_ARTIFICIAL (field
))
1941 /* Variant members only affect deletedness. In particular, they don't
1942 affect the exception-specification of a user-provided destructor,
1943 which we're figuring out via get_defaulted_eh_spec. So if we aren't
1944 asking if this is deleted, don't even look up the function; we don't
1945 want an error about a deleted function we aren't actually calling. */
1946 if (sfk
== sfk_destructor
&& deleted_p
== NULL
1947 && TREE_CODE (DECL_CONTEXT (field
)) == UNION_TYPE
)
1950 mem_type
= strip_array_types (TREE_TYPE (field
));
1951 if (SFK_ASSIGN_P (sfk
))
1954 if (CP_TYPE_CONST_P (mem_type
) && !CLASS_TYPE_P (mem_type
))
1957 error ("non-static const member %q#D, cannot use default "
1958 "assignment operator", field
);
1960 else if (TYPE_REF_P (mem_type
))
1963 error ("non-static reference member %q#D, cannot use "
1964 "default assignment operator", field
);
1969 if (bad
&& deleted_p
)
1972 else if (sfk
== sfk_constructor
|| sfk
== sfk_inheriting_constructor
)
1976 if (DECL_INITIAL (field
))
1978 if (diag
&& DECL_INITIAL (field
) == error_mark_node
)
1979 inform (DECL_SOURCE_LOCATION (field
),
1980 "initializer for %q#D is invalid", field
);
1983 /* Core 1351: If the field has an NSDMI that could throw, the
1984 default constructor is noexcept(false). */
1987 tree nsdmi
= get_nsdmi (field
, /*ctor*/false, complain
);
1988 if (nsdmi
== error_mark_node
)
1989 *spec_p
= error_mark_node
;
1990 else if (*spec_p
!= error_mark_node
1991 && !expr_noexcept_p (nsdmi
, complain
))
1992 *spec_p
= noexcept_false_spec
;
1994 /* Don't do the normal processing. */
1999 if (CP_TYPE_CONST_P (mem_type
)
2000 && default_init_uninitialized_part (mem_type
))
2004 error ("uninitialized const member in %q#T",
2005 current_class_type
);
2006 inform (DECL_SOURCE_LOCATION (field
),
2007 "%q#D should be initialized", field
);
2011 else if (TYPE_REF_P (mem_type
))
2015 error ("uninitialized reference member in %q#T",
2016 current_class_type
);
2017 inform (DECL_SOURCE_LOCATION (field
),
2018 "%q#D should be initialized", field
);
2023 if (bad
&& deleted_p
)
2026 /* Before C++20, for an implicitly-defined default constructor to
2027 be constexpr, every member must have a user-provided default
2028 constructor or an explicit initializer. */
2030 && cxx_dialect
< cxx2a
2031 && !CLASS_TYPE_P (mem_type
)
2032 && TREE_CODE (DECL_CONTEXT (field
)) != UNION_TYPE
)
2034 *constexpr_p
= false;
2036 inform (DECL_SOURCE_LOCATION (field
),
2037 "defaulted default constructor does not "
2038 "initialize %q#D", field
);
2041 else if (sfk
== sfk_copy_constructor
)
2044 if (TYPE_REF_P (mem_type
)
2045 && TYPE_REF_IS_RVALUE (mem_type
))
2048 error ("copying non-static data member %q#D of rvalue "
2049 "reference type", field
);
2055 if (!CLASS_TYPE_P (mem_type
))
2058 if (ANON_AGGR_TYPE_P (mem_type
))
2060 walk_field_subobs (TYPE_FIELDS (mem_type
), sfk
, fnname
, quals
,
2061 spec_p
, trivial_p
, deleted_p
, constexpr_p
,
2062 diag
, flags
, complain
, dtor_from_ctor
);
2066 if (SFK_COPY_P (sfk
) || SFK_MOVE_P (sfk
))
2068 int mem_quals
= cp_type_quals (mem_type
) | quals
;
2069 if (DECL_MUTABLE_P (field
))
2070 mem_quals
&= ~TYPE_QUAL_CONST
;
2071 argtype
= build_stub_type (mem_type
, mem_quals
, SFK_MOVE_P (sfk
));
2074 argtype
= NULL_TREE
;
2076 rval
= locate_fn_flags (mem_type
, fnname
, argtype
, flags
, complain
);
2078 process_subob_fn (rval
, sfk
, spec_p
, trivial_p
, deleted_p
,
2079 constexpr_p
, diag
, field
, dtor_from_ctor
);
2083 /* Base walker helper for synthesized_method_walk. Inspect a direct
2084 or virtual base. BINFO is the parent type's binfo. BASE_BINFO is
2085 the base binfo of interests. All other parms are as for
2086 synthesized_method_walk, or its local vars. */
2089 synthesized_method_base_walk (tree binfo
, tree base_binfo
,
2090 special_function_kind sfk
, tree fnname
, int quals
,
2091 tree
*inheriting_ctor
, tree inherited_parms
,
2092 int flags
, bool diag
,
2093 tree
*spec_p
, bool *trivial_p
,
2094 bool *deleted_p
, bool *constexpr_p
)
2096 bool inherited_binfo
= false;
2097 tree argtype
= NULL_TREE
;
2098 deferring_kind defer
= dk_no_deferred
;
2100 if (SFK_COPY_P (sfk
) || SFK_MOVE_P (sfk
))
2101 argtype
= build_stub_type (BINFO_TYPE (base_binfo
), quals
, SFK_MOVE_P (sfk
));
2102 else if (inheriting_ctor
2104 = binfo_inherited_from (binfo
, base_binfo
, *inheriting_ctor
)))
2106 argtype
= inherited_parms
;
2107 /* Don't check access on the inherited constructor. */
2108 if (flag_new_inheriting_ctors
)
2109 defer
= dk_deferred
;
2111 else if (cxx_dialect
>= cxx14
&& sfk
== sfk_virtual_destructor
2112 && BINFO_VIRTUAL_P (base_binfo
)
2113 && ABSTRACT_CLASS_TYPE_P (BINFO_TYPE (binfo
)))
2114 /* Don't check access when looking at vbases of abstract class's
2115 virtual destructor. */
2116 defer
= dk_no_check
;
2118 if (defer
!= dk_no_deferred
)
2119 push_deferring_access_checks (defer
);
2120 tree rval
= locate_fn_flags (base_binfo
, fnname
, argtype
, flags
,
2121 diag
? tf_warning_or_error
: tf_none
);
2122 if (defer
!= dk_no_deferred
)
2123 pop_deferring_access_checks ();
2125 /* Replace an inherited template with the appropriate specialization. */
2126 if (inherited_binfo
&& rval
2127 && DECL_P (*inheriting_ctor
) && DECL_P (rval
)
2128 && DECL_CONTEXT (*inheriting_ctor
) == DECL_CONTEXT (rval
))
2129 *inheriting_ctor
= DECL_CLONED_FUNCTION (rval
);
2131 process_subob_fn (rval
, sfk
, spec_p
, trivial_p
, deleted_p
,
2132 constexpr_p
, diag
, BINFO_TYPE (base_binfo
));
2133 if (SFK_CTOR_P (sfk
)
2134 && (!BINFO_VIRTUAL_P (base_binfo
)
2135 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo
))))
2137 /* In a constructor we also need to check the subobject
2138 destructors for cleanup of partially constructed objects. */
2139 tree dtor
= locate_fn_flags (base_binfo
, complete_dtor_identifier
,
2141 diag
? tf_warning_or_error
: tf_none
);
2142 /* Note that we don't pass down trivial_p; the subobject
2143 destructors don't affect triviality of the constructor. Nor
2144 do they affect constexpr-ness (a constant expression doesn't
2145 throw) or exception-specification (a throw from one of the
2146 dtors would be a double-fault). */
2147 process_subob_fn (dtor
, sfk
, NULL
, NULL
, deleted_p
, NULL
, false,
2148 BINFO_TYPE (base_binfo
), /*dtor_from_ctor*/true);
2154 /* The caller wants to generate an implicit declaration of SFK for
2155 CTYPE which is const if relevant and CONST_P is set. If SPEC_P,
2156 TRIVIAL_P, DELETED_P or CONSTEXPR_P are non-null, set their
2157 referent appropriately. If DIAG is true, we're either being called
2158 from maybe_explain_implicit_delete to give errors, or if
2159 CONSTEXPR_P is non-null, from explain_invalid_constexpr_fn. */
2162 synthesized_method_walk (tree ctype
, special_function_kind sfk
, bool const_p
,
2163 tree
*spec_p
, bool *trivial_p
, bool *deleted_p
,
2164 bool *constexpr_p
, bool diag
,
2165 tree
*inheriting_ctor
, tree inherited_parms
)
2167 tree binfo
, base_binfo
;
2170 /* SFK must be exactly one category. */
2171 gcc_checking_assert (SFK_DTOR_P(sfk
) + SFK_CTOR_P(sfk
)
2172 + SFK_ASSIGN_P(sfk
) == 1);
2175 *spec_p
= (cxx_dialect
>= cxx11
? noexcept_true_spec
: empty_except_spec
);
2179 /* "The closure type associated with a lambda-expression has a deleted
2180 default constructor and a deleted copy assignment operator."
2181 This is diagnosed in maybe_explain_implicit_delete.
2182 In C++2a, only lambda-expressions with lambda-captures have those
2184 if (LAMBDA_TYPE_P (ctype
)
2185 && (sfk
== sfk_constructor
|| sfk
== sfk_copy_assignment
)
2186 && (cxx_dialect
< cxx2a
2187 || LAMBDA_EXPR_CAPTURE_LIST (CLASSTYPE_LAMBDA_EXPR (ctype
))
2188 || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE
2189 (CLASSTYPE_LAMBDA_EXPR (ctype
)) != CPLD_NONE
))
2198 bool check_vdtor
= false;
2201 if (SFK_DTOR_P (sfk
))
2204 /* The synthesized method will call base dtors, but check complete
2205 here to avoid having to deal with VTT. */
2206 fnname
= complete_dtor_identifier
;
2208 else if (SFK_ASSIGN_P (sfk
))
2209 fnname
= assign_op_identifier
;
2211 fnname
= complete_ctor_identifier
;
2213 gcc_assert ((sfk
== sfk_inheriting_constructor
)
2214 == (inheriting_ctor
&& *inheriting_ctor
!= NULL_TREE
));
2216 /* If that user-written default constructor would satisfy the
2217 requirements of a constexpr constructor (7.1.5), the
2218 implicitly-defined default constructor is constexpr.
2220 The implicitly-defined copy/move assignment operator is constexpr if
2221 - X is a literal type, and
2222 - the assignment operator selected to copy/move each direct base class
2223 subobject is a constexpr function, and
2224 - for each non-static data member of X that is of class type (or array
2225 thereof), the assignment operator selected to copy/move that
2226 member is a constexpr function. */
2228 *constexpr_p
= (SFK_CTOR_P (sfk
)
2229 || (SFK_ASSIGN_P (sfk
) && cxx_dialect
>= cxx14
)
2230 || (SFK_DTOR_P (sfk
) && cxx_dialect
>= cxx2a
));
2232 bool expected_trivial
= type_has_trivial_fn (ctype
, sfk
);
2234 *trivial_p
= expected_trivial
;
2236 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
2237 class versions and other properties of the type. But a subobject
2238 class can be trivially copyable and yet have overload resolution
2239 choose a template constructor for initialization, depending on
2240 rvalueness and cv-quals. And furthermore, a member in a base might
2241 be trivial but deleted or otherwise not callable. So we can't exit
2242 early in C++0x. The same considerations apply in C++98/03, but
2243 there the definition of triviality does not consider overload
2244 resolution, so a constructor can be trivial even if it would otherwise
2245 call a non-trivial constructor. */
2246 if (expected_trivial
2247 && (!(SFK_COPY_P (sfk
) || SFK_MOVE_P (sfk
)) || cxx_dialect
< cxx11
))
2249 if (constexpr_p
&& sfk
== sfk_constructor
)
2251 bool cx
= trivial_default_constructor_is_constexpr (ctype
);
2253 if (diag
&& !cx
&& TREE_CODE (ctype
) == UNION_TYPE
)
2254 /* A trivial constructor doesn't have any NSDMI. */
2255 inform (input_location
, "defaulted default constructor does "
2256 "not initialize any non-static data member");
2258 if (!diag
&& cxx_dialect
< cxx11
)
2262 ++cp_unevaluated_operand
;
2263 ++c_inhibit_evaluation_warnings
;
2264 push_deferring_access_checks (dk_no_deferred
);
2266 tree scope
= push_scope (ctype
);
2268 int flags
= LOOKUP_NORMAL
| LOOKUP_SPECULATIVE
;
2269 if (sfk
!= sfk_inheriting_constructor
)
2270 flags
|= LOOKUP_DEFAULTED
;
2272 tsubst_flags_t complain
= diag
? tf_warning_or_error
: tf_none
;
2274 /* We're in get_defaulted_eh_spec; we don't actually want any walking
2275 diagnostics, we just want complain set. */
2277 int quals
= const_p
? TYPE_QUAL_CONST
: TYPE_UNQUALIFIED
;
2279 for (binfo
= TYPE_BINFO (ctype
), i
= 0;
2280 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
2282 if (!SFK_ASSIGN_P (sfk
) && BINFO_VIRTUAL_P (base_binfo
))
2283 /* We'll handle virtual bases below. */
2286 tree fn
= synthesized_method_base_walk (binfo
, base_binfo
,
2288 inheriting_ctor
, inherited_parms
,
2289 flags
, diag
, spec_p
, trivial_p
,
2290 deleted_p
, constexpr_p
);
2292 if (diag
&& SFK_ASSIGN_P (sfk
) && SFK_MOVE_P (sfk
)
2293 && BINFO_VIRTUAL_P (base_binfo
)
2294 && fn
&& TREE_CODE (fn
) == FUNCTION_DECL
2295 && move_fn_p (fn
) && !trivial_fn_p (fn
)
2296 && vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo
)))
2297 warning (OPT_Wvirtual_move_assign
,
2298 "defaulted move assignment for %qT calls a non-trivial "
2299 "move assignment operator for virtual base %qT",
2300 ctype
, BINFO_TYPE (base_binfo
));
2302 if (check_vdtor
&& type_has_virtual_destructor (BINFO_TYPE (base_binfo
)))
2304 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
2305 to have a null fn (no class-specific op delete). */
2306 fn
= locate_fn_flags (ctype
, ovl_op_identifier (false, DELETE_EXPR
),
2307 ptr_type_node
, flags
, tf_none
);
2308 if (fn
&& fn
== error_mark_node
)
2310 if (complain
& tf_error
)
2311 locate_fn_flags (ctype
, ovl_op_identifier (false, DELETE_EXPR
),
2312 ptr_type_node
, flags
, complain
);
2316 check_vdtor
= false;
2320 vec
<tree
, va_gc
> *vbases
= CLASSTYPE_VBASECLASSES (ctype
);
2321 if (SFK_ASSIGN_P (sfk
))
2322 /* Already examined vbases above. */;
2323 else if (vec_safe_is_empty (vbases
))
2324 /* No virtual bases to worry about. */;
2325 else if (ABSTRACT_CLASS_TYPE_P (ctype
) && cxx_dialect
>= cxx14
2326 /* DR 1658 specifies that vbases of abstract classes are
2327 ignored for both ctors and dtors. Except DR 2336
2328 overrides that skipping when determing the eh-spec of a
2329 virtual destructor. */
2330 && sfk
!= sfk_virtual_destructor
)
2331 /* Vbase cdtors are not relevant. */;
2335 *constexpr_p
= false;
2337 FOR_EACH_VEC_ELT (*vbases
, i
, base_binfo
)
2338 synthesized_method_base_walk (binfo
, base_binfo
, sfk
, fnname
, quals
,
2339 inheriting_ctor
, inherited_parms
,
2341 spec_p
, trivial_p
, deleted_p
, constexpr_p
);
2344 /* Now handle the non-static data members. */
2345 walk_field_subobs (TYPE_FIELDS (ctype
), sfk
, fnname
, quals
,
2346 spec_p
, trivial_p
, deleted_p
, constexpr_p
,
2347 diag
, flags
, complain
, /*dtor_from_ctor*/false);
2348 if (SFK_CTOR_P (sfk
))
2349 walk_field_subobs (TYPE_FIELDS (ctype
), sfk_destructor
,
2350 complete_dtor_identifier
, TYPE_UNQUALIFIED
,
2351 NULL
, NULL
, deleted_p
, NULL
,
2352 false, flags
, complain
, /*dtor_from_ctor*/true);
2356 pop_deferring_access_checks ();
2357 --cp_unevaluated_operand
;
2358 --c_inhibit_evaluation_warnings
;
2361 /* DECL is a defaulted function whose exception specification is now
2362 needed. Return what it should be. */
2365 get_defaulted_eh_spec (tree decl
, tsubst_flags_t complain
)
2367 if (DECL_CLONED_FUNCTION_P (decl
))
2368 decl
= DECL_CLONED_FUNCTION (decl
);
2369 special_function_kind sfk
= special_function_p (decl
);
2370 if (sfk
== sfk_comparison
)
2372 /* We're in synthesize_method. Start with NULL_TREE, build_comparison_op
2373 will adjust as needed. */
2374 gcc_assert (decl
== current_function_decl
);
2377 tree ctype
= DECL_CONTEXT (decl
);
2378 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
2379 tree parm_type
= TREE_VALUE (parms
);
2380 bool const_p
= CP_TYPE_CONST_P (non_reference (parm_type
));
2381 tree spec
= empty_except_spec
;
2382 bool diag
= !DECL_DELETED_FN (decl
) && (complain
& tf_error
);
2383 tree inh
= DECL_INHERITED_CTOR (decl
);
2384 if (SFK_DTOR_P (sfk
) && DECL_VIRTUAL_P (decl
))
2385 /* We have to examine virtual bases even if abstract. */
2386 sfk
= sfk_virtual_destructor
;
2387 bool pushed
= false;
2388 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
2389 pushed
= push_tinst_level (decl
);
2390 synthesized_method_walk (ctype
, sfk
, const_p
, &spec
, NULL
, NULL
,
2391 NULL
, diag
, &inh
, parms
);
2397 /* DECL is a deleted function. If it's implicitly deleted, explain why and
2398 return true; else return false. */
2401 maybe_explain_implicit_delete (tree decl
)
2403 /* If decl is a clone, get the primary variant. */
2404 decl
= DECL_ORIGIN (decl
);
2405 gcc_assert (DECL_DELETED_FN (decl
));
2406 if (DECL_DEFAULTED_FN (decl
))
2408 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
2409 static hash_set
<tree
> *explained
;
2411 special_function_kind sfk
;
2417 explained
= new hash_set
<tree
>;
2418 if (explained
->add (decl
))
2421 sfk
= special_function_p (decl
);
2422 ctype
= DECL_CONTEXT (decl
);
2423 loc
= input_location
;
2424 input_location
= DECL_SOURCE_LOCATION (decl
);
2427 if (LAMBDA_TYPE_P (ctype
))
2430 if (sfk
== sfk_constructor
)
2431 inform (DECL_SOURCE_LOCATION (decl
),
2432 "a lambda closure type has a deleted default constructor");
2433 else if (sfk
== sfk_copy_assignment
)
2434 inform (DECL_SOURCE_LOCATION (decl
),
2435 "a lambda closure type has a deleted copy assignment operator");
2439 else if (DECL_ARTIFICIAL (decl
)
2440 && (sfk
== sfk_copy_assignment
|| sfk
== sfk_copy_constructor
)
2441 && classtype_has_move_assign_or_move_ctor_p (ctype
, true))
2443 inform (DECL_SOURCE_LOCATION (decl
),
2444 "%q#D is implicitly declared as deleted because %qT "
2445 "declares a move constructor or move assignment operator",
2449 else if (sfk
== sfk_inheriting_constructor
)
2451 tree binfo
= inherited_ctor_binfo (decl
);
2452 if (TREE_CODE (binfo
) != TREE_BINFO
)
2454 inform (DECL_SOURCE_LOCATION (decl
),
2455 "%q#D inherits from multiple base subobjects",
2460 if (!informed
&& sfk
== sfk_comparison
)
2462 inform (DECL_SOURCE_LOCATION (decl
),
2463 "%q#D is implicitly deleted because the default "
2464 "definition would be ill-formed:", decl
);
2465 build_comparison_op (decl
, tf_warning_or_error
);
2469 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
2470 bool const_p
= false;
2473 tree parm_type
= TREE_VALUE (parms
);
2474 const_p
= CP_TYPE_CONST_P (non_reference (parm_type
));
2476 tree raises
= NULL_TREE
;
2477 bool deleted_p
= false;
2478 tree scope
= push_scope (ctype
);
2479 tree inh
= DECL_INHERITED_CTOR (decl
);
2481 synthesized_method_walk (ctype
, sfk
, const_p
,
2482 &raises
, NULL
, &deleted_p
, NULL
, false,
2486 inform (DECL_SOURCE_LOCATION (decl
),
2487 "%q#D is implicitly deleted because the default "
2488 "definition would be ill-formed:", decl
);
2489 synthesized_method_walk (ctype
, sfk
, const_p
,
2490 NULL
, NULL
, &deleted_p
, NULL
, true,
2493 else if (!comp_except_specs
2494 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl
)),
2496 inform (DECL_SOURCE_LOCATION (decl
), "%q#F is implicitly "
2497 "deleted because its exception-specification does not "
2498 "match the implicit exception-specification %qX",
2500 else if (flag_checking
)
2506 input_location
= loc
;
2512 /* DECL is a defaulted function which was declared constexpr. Explain why
2513 it can't be constexpr. */
2516 explain_implicit_non_constexpr (tree decl
)
2518 tree parms
= FUNCTION_FIRST_USER_PARMTYPE (decl
);
2519 bool const_p
= CP_TYPE_CONST_P (non_reference (TREE_VALUE (parms
)));
2520 tree inh
= DECL_INHERITED_CTOR (decl
);
2522 special_function_kind sfk
= special_function_p (decl
);
2523 if (sfk
== sfk_comparison
)
2525 DECL_DECLARED_CONSTEXPR_P (decl
) = true;
2526 build_comparison_op (decl
, tf_warning_or_error
);
2527 DECL_DECLARED_CONSTEXPR_P (decl
) = false;
2530 synthesized_method_walk (DECL_CLASS_CONTEXT (decl
),
2532 NULL
, NULL
, NULL
, &dummy
, true,
2536 /* DECL is an instantiation of an inheriting constructor template. Deduce
2537 the correct exception-specification and deletedness for this particular
2541 deduce_inheriting_ctor (tree decl
)
2543 decl
= DECL_ORIGIN (decl
);
2544 gcc_assert (DECL_INHERITED_CTOR (decl
));
2546 bool trivial
, constexpr_
, deleted
;
2547 tree inh
= DECL_INHERITED_CTOR (decl
);
2548 synthesized_method_walk (DECL_CONTEXT (decl
), sfk_inheriting_constructor
,
2549 false, &spec
, &trivial
, &deleted
, &constexpr_
,
2552 FUNCTION_FIRST_USER_PARMTYPE (decl
));
2553 if (TREE_CODE (inherited_ctor_binfo (decl
)) != TREE_BINFO
)
2554 /* Inherited the same constructor from different base subobjects. */
2556 DECL_DELETED_FN (decl
) = deleted
;
2557 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
), spec
);
2558 SET_DECL_INHERITED_CTOR (decl
, inh
);
2561 FOR_EACH_CLONE (clone
, decl
)
2563 DECL_DELETED_FN (clone
) = deleted
;
2564 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
), spec
);
2565 SET_DECL_INHERITED_CTOR (clone
, inh
);
2569 /* Implicitly declare the special function indicated by KIND, as a
2570 member of TYPE. For copy constructors and assignment operators,
2571 CONST_P indicates whether these functions should take a const
2572 reference argument or a non-const reference.
2573 Returns the FUNCTION_DECL for the implicitly declared function. */
2576 implicitly_declare_fn (special_function_kind kind
, tree type
,
2577 bool const_p
, tree pattern_fn
,
2578 tree inherited_parms
)
2581 tree parameter_types
= void_list_node
;
2584 tree raises
= empty_except_spec
;
2585 tree rhs_parm_type
= NULL_TREE
;
2588 HOST_WIDE_INT saved_processing_template_decl
;
2589 bool deleted_p
= false;
2590 bool constexpr_p
= false;
2591 bool friend_p
= (kind
== sfk_comparison
&& DECL_FRIEND_P (pattern_fn
));
2592 tree inherited_ctor
= (kind
== sfk_inheriting_constructor
2593 ? pattern_fn
: NULL_TREE
);
2595 /* Because we create declarations for implicitly declared functions
2596 lazily, we may be creating the declaration for a member of TYPE
2597 while in some completely different context. However, TYPE will
2598 never be a dependent class (because we never want to do lookups
2599 for implicitly defined functions in a dependent class).
2600 Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
2601 because we only create clones for constructors and destructors
2602 when not in a template. */
2603 gcc_assert (!dependent_type_p (type
));
2604 saved_processing_template_decl
= processing_template_decl
;
2605 processing_template_decl
= 0;
2607 type
= TYPE_MAIN_VARIANT (type
);
2609 if (targetm
.cxx
.cdtor_returns_this ())
2611 if (kind
== sfk_destructor
)
2612 /* See comment in check_special_function_return_type. */
2613 return_type
= build_pointer_type (void_type_node
);
2615 return_type
= build_pointer_type (type
);
2618 return_type
= void_type_node
;
2620 int this_quals
= TYPE_UNQUALIFIED
;
2623 case sfk_destructor
:
2625 name
= dtor_identifier
;
2628 case sfk_constructor
:
2629 /* Default constructor. */
2630 name
= ctor_identifier
;
2633 case sfk_copy_constructor
:
2634 case sfk_copy_assignment
:
2635 case sfk_move_constructor
:
2636 case sfk_move_assignment
:
2637 case sfk_inheriting_constructor
:
2639 if (kind
== sfk_copy_assignment
2640 || kind
== sfk_move_assignment
)
2642 return_type
= build_reference_type (type
);
2643 name
= assign_op_identifier
;
2646 name
= ctor_identifier
;
2648 if (kind
== sfk_inheriting_constructor
)
2649 parameter_types
= inherited_parms
;
2653 rhs_parm_type
= cp_build_qualified_type (type
, TYPE_QUAL_CONST
);
2655 rhs_parm_type
= type
;
2656 bool move_p
= (kind
== sfk_move_assignment
2657 || kind
== sfk_move_constructor
);
2658 rhs_parm_type
= cp_build_reference_type (rhs_parm_type
, move_p
);
2660 parameter_types
= tree_cons (NULL_TREE
, rhs_parm_type
, parameter_types
);
2665 case sfk_comparison
:
2666 /* If the class definition does not explicitly declare an == operator
2667 function, but declares a defaulted three-way comparison operator
2668 function, an == operator function is declared implicitly with the same
2669 access as the three-way comparison operator function.
2671 The implicitly-declared == operator for a class X is an inline member
2672 and is defined as defaulted in the definition of X.
2674 If the three-way comparison operator function is declared as a
2675 non-static const member, the implicitly-declared == operator function
2676 is a member of the form
2678 bool X::operator==(const X&) const;
2680 Otherwise, the implicitly-declared == operator function is of the form
2682 friend bool operator==(const X&, const X&); */
2683 /* No other comparison operator is implicitly declared. */
2684 name
= ovl_op_identifier (false, EQ_EXPR
);
2685 return_type
= boolean_type_node
;
2686 rhs_parm_type
= cp_build_qualified_type (type
, TYPE_QUAL_CONST
);
2687 rhs_parm_type
= cp_build_reference_type (rhs_parm_type
, false);
2688 parameter_types
= tree_cons (NULL_TREE
, rhs_parm_type
, parameter_types
);
2690 parameter_types
= tree_cons (NULL_TREE
, rhs_parm_type
, parameter_types
);
2691 this_quals
= TYPE_QUAL_CONST
;
2698 bool trivial_p
= false;
2702 /* For an inheriting constructor, just copy these flags from the
2703 inherited constructor until deduce_inheriting_ctor. */
2704 raises
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor
));
2705 deleted_p
= DECL_DELETED_FN (inherited_ctor
);
2706 constexpr_p
= DECL_DECLARED_CONSTEXPR_P (inherited_ctor
);
2708 else if (cxx_dialect
>= cxx11
)
2710 raises
= noexcept_deferred_spec
;
2711 if (kind
!= sfk_comparison
)
2712 synthesized_method_walk (type
, kind
, const_p
, NULL
, &trivial_p
,
2713 &deleted_p
, &constexpr_p
, false,
2714 &inherited_ctor
, inherited_parms
);
2717 synthesized_method_walk (type
, kind
, const_p
, &raises
, &trivial_p
,
2718 &deleted_p
, &constexpr_p
, false,
2719 &inherited_ctor
, inherited_parms
);
2720 /* Don't bother marking a deleted constructor as constexpr. */
2722 constexpr_p
= false;
2723 /* A trivial copy/move constructor is also a constexpr constructor,
2724 unless the class has virtual bases (7.1.5p4). */
2726 && cxx_dialect
>= cxx11
2727 && (kind
== sfk_copy_constructor
2728 || kind
== sfk_move_constructor
)
2729 && !CLASSTYPE_VBASECLASSES (type
))
2730 gcc_assert (constexpr_p
);
2732 if (!trivial_p
&& type_has_trivial_fn (type
, kind
))
2733 type_set_nontrivial_flag (type
, kind
);
2735 /* Create the function. */
2737 fn_type
= build_function_type (return_type
, parameter_types
);
2740 tree this_type
= cp_build_qualified_type (type
, this_quals
);
2741 fn_type
= build_method_type_directly (this_type
, return_type
,
2747 if (raises
!= error_mark_node
)
2748 fn_type
= build_exception_variant (fn_type
, raises
);
2750 /* Can happen, eg, in C++98 mode for an ill-formed non-static data
2751 member initializer (c++/89914). */
2752 gcc_assert (seen_error ());
2754 fn
= build_lang_decl (FUNCTION_DECL
, name
, fn_type
);
2755 if (kind
== sfk_comparison
)
2757 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (pattern_fn
);
2758 DECL_MAYBE_DELETED (fn
) = true;
2760 else if (kind
!= sfk_inheriting_constructor
)
2761 DECL_SOURCE_LOCATION (fn
) = DECL_SOURCE_LOCATION (TYPE_NAME (type
));
2763 if (IDENTIFIER_OVL_OP_P (name
))
2765 const ovl_op_info_t
*op
= IDENTIFIER_OVL_OP_INFO (name
);
2766 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn
) = op
->ovl_op_code
;
2768 else if (IDENTIFIER_CTOR_P (name
))
2769 DECL_CXX_CONSTRUCTOR_P (fn
) = true;
2770 else if (IDENTIFIER_DTOR_P (name
))
2771 DECL_CXX_DESTRUCTOR_P (fn
) = true;
2775 SET_DECL_ALIGN (fn
, MINIMUM_METHOD_BOUNDARY
);
2777 /* Create the explicit arguments. */
2780 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
2781 want its type to be included in the mangled function
2783 tree decl
= cp_build_parm_decl (fn
, NULL_TREE
, rhs_parm_type
);
2784 TREE_READONLY (decl
) = 1;
2785 retrofit_lang_decl (decl
);
2786 DECL_PARM_INDEX (decl
) = DECL_PARM_LEVEL (decl
) = 1;
2787 DECL_ARGUMENTS (fn
) = decl
;
2790 /* The second parm of friend op==. */
2791 tree decl2
= copy_decl (decl
);
2792 DECL_CHAIN (decl
) = decl2
;
2793 DECL_PARM_INDEX (decl2
) = 2;
2796 else if (kind
== sfk_inheriting_constructor
)
2798 tree
*p
= &DECL_ARGUMENTS (fn
);
2800 for (tree parm
= inherited_parms
; parm
&& parm
!= void_list_node
;
2801 parm
= TREE_CHAIN (parm
))
2803 *p
= cp_build_parm_decl (fn
, NULL_TREE
, TREE_VALUE (parm
));
2804 retrofit_lang_decl (*p
);
2805 DECL_PARM_LEVEL (*p
) = 1;
2806 DECL_PARM_INDEX (*p
) = index
++;
2807 p
= &DECL_CHAIN (*p
);
2809 SET_DECL_INHERITED_CTOR (fn
, inherited_ctor
);
2810 DECL_NONCONVERTING_P (fn
) = DECL_NONCONVERTING_P (inherited_ctor
);
2811 /* A constructor so declared has the same access as the corresponding
2812 constructor in X. */
2813 TREE_PRIVATE (fn
) = TREE_PRIVATE (inherited_ctor
);
2814 TREE_PROTECTED (fn
) = TREE_PROTECTED (inherited_ctor
);
2815 /* Copy constexpr from the inherited constructor even if the
2816 inheriting constructor doesn't satisfy the requirements. */
2817 constexpr_p
= DECL_DECLARED_CONSTEXPR_P (inherited_ctor
);
2821 DECL_CONTEXT (fn
) = DECL_CONTEXT (pattern_fn
);
2824 /* Add the "this" parameter. */
2825 this_parm
= build_this_parm (fn
, fn_type
, this_quals
);
2826 DECL_CHAIN (this_parm
) = DECL_ARGUMENTS (fn
);
2827 DECL_ARGUMENTS (fn
) = this_parm
;
2829 grokclassfn (type
, fn
, kind
== sfk_destructor
? DTOR_FLAG
: NO_SPECIAL
);
2832 DECL_IN_AGGR_P (fn
) = 1;
2833 DECL_ARTIFICIAL (fn
) = 1;
2834 DECL_DEFAULTED_FN (fn
) = 1;
2835 if (cxx_dialect
>= cxx11
)
2837 DECL_DELETED_FN (fn
) = deleted_p
;
2838 DECL_DECLARED_CONSTEXPR_P (fn
) = constexpr_p
;
2840 DECL_EXTERNAL (fn
) = true;
2841 DECL_NOT_REALLY_EXTERN (fn
) = 1;
2842 DECL_DECLARED_INLINE_P (fn
) = 1;
2843 set_linkage_according_to_type (type
, fn
);
2844 if (TREE_PUBLIC (fn
))
2845 DECL_COMDAT (fn
) = 1;
2846 if (kind
== sfk_comparison
&& !friend_p
)
2848 /* The implicit op== has the same access as the op<=>. */
2849 TREE_PRIVATE (fn
) = TREE_PRIVATE (pattern_fn
);
2850 TREE_PROTECTED (fn
) = TREE_PROTECTED (pattern_fn
);
2852 rest_of_decl_compilation (fn
, namespace_bindings_p (), at_eof
);
2853 gcc_assert (!TREE_USED (fn
));
2855 /* Propagate constraints from the inherited constructor. */
2856 if (flag_concepts
&& inherited_ctor
)
2857 if (tree orig_ci
= get_constraints (inherited_ctor
))
2859 tree new_ci
= copy_node (orig_ci
);
2860 set_constraints (fn
, new_ci
);
2863 /* Restore PROCESSING_TEMPLATE_DECL. */
2864 processing_template_decl
= saved_processing_template_decl
;
2866 if (inherited_ctor
&& TREE_CODE (inherited_ctor
) == TEMPLATE_DECL
)
2867 fn
= add_inherited_template_parms (fn
, inherited_ctor
);
2869 /* Warn about calling a non-trivial move assignment in a virtual base. */
2870 if (kind
== sfk_move_assignment
&& !deleted_p
&& !trivial_p
2871 && CLASSTYPE_VBASECLASSES (type
))
2873 location_t loc
= input_location
;
2874 input_location
= DECL_SOURCE_LOCATION (fn
);
2875 synthesized_method_walk (type
, kind
, const_p
,
2876 NULL
, NULL
, NULL
, NULL
, true,
2878 input_location
= loc
;
2884 /* Gives any errors about defaulted functions which need to be deferred
2885 until the containing class is complete. */
2888 defaulted_late_check (tree fn
)
2890 /* Complain about invalid signature for defaulted fn. */
2891 tree ctx
= DECL_CONTEXT (fn
);
2892 special_function_kind kind
= special_function_p (fn
);
2894 if (kind
== sfk_comparison
)
2896 /* If the function was declared constexpr, check that the definition
2897 qualifies. Otherwise we can define the function lazily. */
2898 if (DECL_DECLARED_CONSTEXPR_P (fn
))
2899 synthesize_method (fn
);
2903 bool fn_const_p
= (copy_fn_p (fn
) == 2);
2904 tree implicit_fn
= implicitly_declare_fn (kind
, ctx
, fn_const_p
,
2906 tree eh_spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn
));
2908 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn
)),
2909 TREE_TYPE (TREE_TYPE (implicit_fn
)))
2910 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2911 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn
))))
2913 error ("defaulted declaration %q+D does not match the "
2914 "expected signature", fn
);
2915 inform (DECL_SOURCE_LOCATION (fn
),
2916 "expected signature: %qD", implicit_fn
);
2919 if (DECL_DELETED_FN (implicit_fn
))
2921 DECL_DELETED_FN (fn
) = 1;
2925 /* If a function is explicitly defaulted on its first declaration without an
2926 exception-specification, it is implicitly considered to have the same
2927 exception-specification as if it had been implicitly declared. */
2928 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn
))
2929 && DECL_DEFAULTED_IN_CLASS_P (fn
))
2930 TREE_TYPE (fn
) = build_exception_variant (TREE_TYPE (fn
), eh_spec
);
2932 if (DECL_DEFAULTED_IN_CLASS_P (fn
)
2933 && DECL_DECLARED_CONSTEXPR_P (implicit_fn
))
2935 /* Hmm...should we do this for out-of-class too? Should it be OK to
2936 add constexpr later like inline, rather than requiring
2937 declarations to match? */
2938 DECL_DECLARED_CONSTEXPR_P (fn
) = true;
2939 if (kind
== sfk_constructor
)
2940 TYPE_HAS_CONSTEXPR_CTOR (ctx
) = true;
2943 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn
)
2944 && DECL_DECLARED_CONSTEXPR_P (fn
))
2946 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx
))
2948 error ("explicitly defaulted function %q+D cannot be declared "
2949 "%qs because the implicit declaration is not %qs:", fn
,
2950 DECL_IMMEDIATE_FUNCTION_P (fn
) ? "consteval" : "constexpr",
2952 explain_implicit_non_constexpr (fn
);
2954 DECL_DECLARED_CONSTEXPR_P (fn
) = false;
2958 /* Returns true iff FN can be explicitly defaulted, and gives any
2959 errors if defaulting FN is ill-formed. */
2962 defaultable_fn_check (tree fn
)
2964 special_function_kind kind
= sfk_none
;
2966 if (template_parm_scope_p ())
2968 error ("a template cannot be defaulted");
2972 if (DECL_CONSTRUCTOR_P (fn
))
2974 if (FUNCTION_FIRST_USER_PARMTYPE (fn
) == void_list_node
)
2975 kind
= sfk_constructor
;
2976 else if (copy_fn_p (fn
) > 0
2977 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn
))
2979 kind
= sfk_copy_constructor
;
2980 else if (move_fn_p (fn
))
2981 kind
= sfk_move_constructor
;
2983 else if (DECL_DESTRUCTOR_P (fn
))
2984 kind
= sfk_destructor
;
2985 else if (DECL_ASSIGNMENT_OPERATOR_P (fn
)
2986 && DECL_OVERLOADED_OPERATOR_IS (fn
, NOP_EXPR
))
2989 kind
= sfk_copy_assignment
;
2990 else if (move_fn_p (fn
))
2991 kind
= sfk_move_assignment
;
2993 else if (DECL_OVERLOADED_OPERATOR_CODE_RAW (fn
) >= OVL_OP_EQ_EXPR
2994 && DECL_OVERLOADED_OPERATOR_CODE_RAW (fn
) <= OVL_OP_SPACESHIP_EXPR
)
2996 kind
= sfk_comparison
;
2997 if (!early_check_defaulted_comparison (fn
))
3001 if (kind
== sfk_none
)
3003 error ("%qD cannot be defaulted", fn
);
3008 for (tree t
= FUNCTION_FIRST_USER_PARMTYPE (fn
);
3009 t
&& t
!= void_list_node
; t
= TREE_CHAIN (t
))
3010 if (TREE_PURPOSE (t
))
3012 error ("defaulted function %q+D with default argument", fn
);
3016 /* Avoid do_warn_unused_parameter warnings. */
3017 for (tree p
= FUNCTION_FIRST_USER_PARM (fn
); p
; p
= DECL_CHAIN (p
))
3019 TREE_NO_WARNING (p
) = 1;
3021 if (current_class_type
&& TYPE_BEING_DEFINED (current_class_type
))
3022 /* Defer checking. */;
3023 else if (!processing_template_decl
)
3024 defaulted_late_check (fn
);
3030 /* Add an implicit declaration to TYPE for the kind of function
3031 indicated by SFK. Return the FUNCTION_DECL for the new implicit
3035 lazily_declare_fn (special_function_kind sfk
, tree type
)
3038 /* Whether or not the argument has a const reference type. */
3039 bool const_p
= false;
3041 type
= TYPE_MAIN_VARIANT (type
);
3045 case sfk_constructor
:
3046 CLASSTYPE_LAZY_DEFAULT_CTOR (type
) = 0;
3048 case sfk_copy_constructor
:
3049 const_p
= TYPE_HAS_CONST_COPY_CTOR (type
);
3050 CLASSTYPE_LAZY_COPY_CTOR (type
) = 0;
3052 case sfk_move_constructor
:
3053 CLASSTYPE_LAZY_MOVE_CTOR (type
) = 0;
3055 case sfk_copy_assignment
:
3056 const_p
= TYPE_HAS_CONST_COPY_ASSIGN (type
);
3057 CLASSTYPE_LAZY_COPY_ASSIGN (type
) = 0;
3059 case sfk_move_assignment
:
3060 CLASSTYPE_LAZY_MOVE_ASSIGN (type
) = 0;
3062 case sfk_destructor
:
3063 CLASSTYPE_LAZY_DESTRUCTOR (type
) = 0;
3069 /* Declare the function. */
3070 fn
= implicitly_declare_fn (sfk
, type
, const_p
, NULL
, NULL
);
3072 /* [class.copy]/8 If the class definition declares a move constructor or
3073 move assignment operator, the implicitly declared copy constructor is
3074 defined as deleted.... */
3075 if ((sfk
== sfk_copy_assignment
|| sfk
== sfk_copy_constructor
)
3076 && cxx_dialect
>= cxx11
)
3078 if (classtype_has_move_assign_or_move_ctor_p (type
, true))
3079 DECL_DELETED_FN (fn
) = true;
3080 else if (classtype_has_depr_implicit_copy (type
))
3081 /* The implicit definition of a copy constructor as defaulted is
3082 deprecated if the class has a user-declared copy assignment operator
3083 or a user-declared destructor. The implicit definition of a copy
3084 assignment operator as defaulted is deprecated if the class has a
3085 user-declared copy constructor or a user-declared destructor (15.4,
3087 TREE_DEPRECATED (fn
) = true;
3090 /* Destructors and assignment operators may be virtual. */
3091 if (sfk
== sfk_destructor
3092 || sfk
== sfk_move_assignment
3093 || sfk
== sfk_copy_assignment
)
3094 check_for_override (fn
, type
);
3096 /* Add it to the class */
3097 bool added
= add_method (type
, fn
, false);
3098 gcc_assert (added
|| errorcount
);
3100 /* Add it to TYPE_FIELDS. */
3101 if (sfk
== sfk_destructor
3102 && DECL_VIRTUAL_P (fn
))
3103 /* The ABI requires that a virtual destructor go at the end of the
3105 TYPE_FIELDS (type
) = chainon (TYPE_FIELDS (type
), fn
);
3108 DECL_CHAIN (fn
) = TYPE_FIELDS (type
);
3109 TYPE_FIELDS (type
) = fn
;
3111 /* Propagate TYPE_FIELDS. */
3112 fixup_type_variants (type
);
3114 maybe_add_class_template_decl_list (type
, fn
, /*friend_p=*/0);
3115 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (fn
))
3116 /* Create appropriate clones. */
3117 clone_function_decl (fn
, /*update_methods=*/true);
3122 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
3123 as there are artificial parms in FN. */
3126 skip_artificial_parms_for (const_tree fn
, tree list
)
3128 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
3129 list
= TREE_CHAIN (list
);
3133 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
3134 list
= TREE_CHAIN (list
);
3135 if (DECL_HAS_VTT_PARM_P (fn
))
3136 list
= TREE_CHAIN (list
);
3140 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
3141 artificial parms in FN. */
3144 num_artificial_parms_for (const_tree fn
)
3148 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
))
3153 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
3155 if (DECL_HAS_VTT_PARM_P (fn
))
3161 #include "gt-cp-method.h"