c++: explicit object cleanups
[official-gcc.git] / gcc / cp / method.cc
blob6a9f03eb8f3cd0cb5855e46293d0b5d97c235594
1 /* Handle the hair of processing (but not expanding) inline functions.
2 Also manage function and variable name overloading.
3 Copyright (C) 1987-2024 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)
11 any later version.
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. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "target.h"
28 #include "cp-tree.h"
29 #include "stringpool.h"
30 #include "cgraph.h"
31 #include "varasm.h"
32 #include "toplev.h"
33 #include "intl.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.cc. */
42 void
43 init_method (void)
45 init_mangle ();
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. */
57 tree
58 make_thunk (tree function, bool this_adjusting,
59 tree fixed_offset, tree virtual_offset)
61 HOST_WIDE_INT d;
62 tree thunk;
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)
71 virtual_offset
72 = size_binop (MULT_EXPR,
73 virtual_offset,
74 convert (ssizetype,
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
81 will be a BINFO. */
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)
86 && (!virtual_offset
87 || (this_adjusting
88 ? tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk),
89 virtual_offset)
90 : THUNK_VIRTUAL_OFFSET (thunk) == virtual_offset)))
91 return thunk;
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);
140 return thunk;
143 /* Finish THUNK, a thunk decl. */
145 void
146 finish_thunk (tree thunk)
148 tree function, name;
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))
166 tree cov_probe;
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);
175 break;
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. */
187 tree
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));
218 else
219 TREE_STATIC (alias) = 1;
220 TREE_ADDRESSABLE (alias) = 1;
221 TREE_USED (alias) = 1;
222 SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
223 return alias;
226 static tree
227 make_alias_for_thunk (tree function)
229 tree alias;
230 char buf[256];
232 targetm.asm_out.generate_internal_label (buf, "LTHUNK", thunk_labelno);
233 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);
247 return alias;
250 /* Emit the definition of a C++ multiple inheritance or covariant
251 return vtable thunk. If EMIT_P is nonzero, the thunk is emitted
252 immediately. */
254 void
255 use_thunk (tree thunk_fndecl, bool emit_p)
257 tree a, t, function, alias;
258 tree virtual_offset;
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
267 aliased thunk. */
268 gcc_assert (!THUNK_ALIAS (thunk_fndecl));
270 if (TREE_ASM_WRITTEN (thunk_fndecl))
271 return;
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. */
277 return;
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);
290 if (!emit_p)
291 return;
293 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function))
294 alias = make_alias_for_thunk (function);
295 else
296 alias = function;
298 fixed_offset = THUNK_FIXED_OFFSET (thunk_fndecl);
299 virtual_offset = THUNK_VIRTUAL_OFFSET (thunk_fndecl);
301 if (virtual_offset)
303 if (!this_adjusting)
304 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
305 virtual_value = tree_to_shwi (virtual_offset);
306 gcc_assert (virtual_value);
308 else
309 virtual_value = 0;
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
316 rewrite. */
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;
328 return;
331 push_to_top_level ();
333 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function)
334 && targetm_common.have_named_sections)
336 tree fn = function;
337 struct symtab_node *symbol;
339 if ((symbol = symtab_node::get (function))
340 && symbol->alias)
342 if (symbol->analyzed)
343 fn = symtab_node::get (function)->ultimate_alias_target ()->decl;
344 else
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, 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. */
361 t = NULL_TREE;
362 for (a = DECL_ARGUMENTS (function); a; a = DECL_CHAIN (a))
364 tree x = copy_node (a);
365 DECL_CHAIN (x) = t;
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;
370 t = x;
372 a = nreverse (t);
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. */
390 static bool
391 type_has_trivial_fn (tree ctype, special_function_kind sfk)
393 switch (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);
405 case sfk_destructor:
406 case sfk_virtual_destructor:
407 return !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype);
408 case sfk_inheriting_constructor:
409 case sfk_comparison:
410 return false;
411 default:
412 gcc_unreachable ();
416 /* Note that CTYPE has a non-trivial SFK even though we previously thought
417 it was trivial. */
419 static void
420 type_set_nontrivial_flag (tree ctype, special_function_kind sfk)
422 switch (sfk)
424 case sfk_constructor:
425 TYPE_HAS_COMPLEX_DFLT (ctype) = true;
426 return;
427 case sfk_copy_constructor:
428 TYPE_HAS_COMPLEX_COPY_CTOR (ctype) = true;
429 return;
430 case sfk_move_constructor:
431 TYPE_HAS_COMPLEX_MOVE_CTOR (ctype) = true;
432 return;
433 case sfk_copy_assignment:
434 TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype) = true;
435 return;
436 case sfk_move_assignment:
437 TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype) = true;
438 return;
439 case sfk_destructor:
440 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
441 return;
442 case sfk_inheriting_constructor:
443 default:
444 gcc_unreachable ();
448 /* True iff FN is a trivial defaulted member function ([cd]tor, op=). */
450 bool
451 trivial_fn_p (tree fn)
453 if (TREE_CODE (fn) == TEMPLATE_DECL)
454 return false;
455 if (!DECL_DEFAULTED_FN (fn))
456 return false;
458 /* If fn is a clone, get the primary variant. */
459 if (tree prim = DECL_CLONED_FUNCTION (fn))
460 fn = prim;
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. */
467 tree
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);
481 return exp;
484 /* Strip all inheriting constructors, if any, to return the original
485 constructor from a (possibly indirect) base class. */
487 tree
488 strip_inheriting_ctors (tree dfn)
490 if (!flag_new_inheriting_ctors)
491 return dfn;
492 tree fn = dfn;
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);
499 return 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);
506 static tree
507 inherited_ctor_binfo_1 (tree binfo, tree fndecl)
509 tree base = DECL_CONTEXT (fndecl);
510 tree base_binfo;
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);
515 gcc_unreachable();
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. */
522 static tree
523 inherited_ctor_binfo (tree binfo, tree fndecl)
525 tree inh = DECL_INHERITED_CTOR (fndecl);
526 if (!inh)
527 return binfo;
529 tree results = NULL_TREE;
530 for (ovl_iterator iter (inh); iter; ++iter)
532 tree one = inherited_ctor_binfo_1 (binfo, *iter);
533 if (!results)
534 results = one;
535 else if (one != results)
536 results = tree_cons (NULL_TREE, one, results);
538 return 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
543 constructor. */
545 tree
546 inherited_ctor_binfo (tree fndecl)
548 if (!DECL_INHERITED_CTOR (fndecl))
549 return NULL_TREE;
550 tree binfo = TYPE_BINFO (DECL_CONTEXT (fndecl));
551 return inherited_ctor_binfo (binfo, fndecl);
555 /* True if we should omit all user-declared parameters from a base
556 construtor built from complete constructor FN.
557 That's when the ctor is inherited from a virtual base. */
559 bool
560 base_ctor_omit_inherited_parms (tree comp_ctor)
562 gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (comp_ctor));
564 if (!flag_new_inheriting_ctors)
565 /* We only optimize away the parameters in the new model. */
566 return false;
568 if (!CLASSTYPE_VBASECLASSES (DECL_CONTEXT (comp_ctor)))
569 return false;
571 if (FUNCTION_FIRST_USER_PARMTYPE (comp_ctor) == void_list_node)
572 /* No user-declared parameters to omit. */
573 return false;
575 for (tree binfo = inherited_ctor_binfo (comp_ctor);
576 binfo;
577 binfo = BINFO_INHERITANCE_CHAIN (binfo))
578 if (BINFO_VIRTUAL_P (binfo))
579 return true;
581 return false;
585 /* True if we should omit all user-declared parameters from constructor FN,
586 because it is a base clone of a ctor inherited from a virtual base. */
588 bool
589 ctor_omit_inherited_parms (tree fn)
591 gcc_checking_assert (TREE_CODE (fn) == FUNCTION_DECL);
593 if (!DECL_BASE_CONSTRUCTOR_P (fn))
594 return false;
596 return base_ctor_omit_inherited_parms (DECL_CLONED_FUNCTION (fn));
599 /* True iff constructor(s) INH inherited into BINFO initializes INIT_BINFO.
600 This can be true for multiple virtual bases as well as one direct
601 non-virtual base. */
603 static bool
604 binfo_inherited_from (tree binfo, tree init_binfo, tree inh)
606 /* inh is an OVERLOAD if we inherited the same constructor along
607 multiple paths, check all of them. */
608 for (ovl_iterator iter (inh); iter; ++iter)
610 tree fn = *iter;
611 tree base = DECL_CONTEXT (fn);
612 tree base_binfo = NULL_TREE;
613 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
614 if (BINFO_TYPE (base_binfo) == base)
615 break;
616 if (base_binfo == init_binfo
617 || (flag_new_inheriting_ctors
618 && binfo_inherited_from (base_binfo, init_binfo,
619 DECL_INHERITED_CTOR (fn))))
620 return true;
622 return false;
625 /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO
626 given the parameter or parameters PARM, possibly inherited constructor
627 base INH, or move flag MOVE_P. */
629 static tree
630 add_one_base_init (tree binfo, tree parm, bool move_p, tree inh,
631 tree member_init_list)
633 tree init;
634 if (inh)
636 /* An inheriting constructor only has a mem-initializer for
637 the base it inherits from. */
638 if (!binfo_inherited_from (TYPE_BINFO (current_class_type), binfo, inh))
639 return member_init_list;
641 tree *p = &init;
642 init = NULL_TREE;
643 for (; parm; parm = DECL_CHAIN (parm))
645 tree exp = forward_parm (parm);
646 *p = build_tree_list (NULL_TREE, exp);
647 p = &TREE_CHAIN (*p);
650 else
652 init = build_base_path (PLUS_EXPR, parm, binfo, 1,
653 tf_warning_or_error);
654 if (move_p)
655 init = move (init);
656 init = build_tree_list (NULL_TREE, init);
658 return tree_cons (binfo, init, member_init_list);
661 /* Generate code for default X(X&) or X(X&&) constructor or an inheriting
662 constructor. */
664 static void
665 do_build_copy_constructor (tree fndecl)
667 tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
668 bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
669 bool trivial = trivial_fn_p (fndecl);
670 tree inh = DECL_INHERITED_CTOR (fndecl);
672 if (!inh)
673 parm = convert_from_reference (parm);
675 if (trivial)
677 if (is_empty_class (current_class_type))
678 /* Don't copy the padding byte; it might not have been allocated
679 if *this is a base subobject. */;
680 else if (tree_int_cst_equal (TYPE_SIZE (current_class_type),
681 CLASSTYPE_SIZE (current_class_type)))
683 tree t = cp_build_init_expr (current_class_ref, parm);
684 finish_expr_stmt (t);
686 else
688 /* We must only copy the non-tail padding parts. */
689 tree base_size = CLASSTYPE_SIZE_UNIT (current_class_type);
690 base_size = size_binop (MINUS_EXPR, base_size, size_int (1));
691 tree array_type = build_array_type (unsigned_char_type_node,
692 build_index_type (base_size));
693 tree alias_set = build_int_cst (TREE_TYPE (current_class_ptr), 0);
694 tree lhs = build2 (MEM_REF, array_type,
695 current_class_ptr, alias_set);
696 tree rhs = build2 (MEM_REF, array_type,
697 TREE_OPERAND (parm, 0), alias_set);
698 tree t = cp_build_init_expr (lhs, rhs);
699 finish_expr_stmt (t);
702 else
704 tree member_init_list = NULL_TREE;
705 int i;
706 tree binfo, base_binfo;
707 vec<tree, va_gc> *vbases;
709 /* Initialize all the base-classes with the parameter converted
710 to their type so that we get their copy constructor and not
711 another constructor that takes current_class_type. We must
712 deal with the binfo's directly as a direct base might be
713 inaccessible due to ambiguity. */
714 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
715 vec_safe_iterate (vbases, i, &binfo); i++)
717 member_init_list = add_one_base_init (binfo, parm, move_p, inh,
718 member_init_list);
721 for (binfo = TYPE_BINFO (current_class_type), i = 0;
722 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
724 if (BINFO_VIRTUAL_P (base_binfo))
725 continue;
726 member_init_list = add_one_base_init (base_binfo, parm, move_p,
727 inh, member_init_list);
730 if (!inh)
732 int cvquals = cp_type_quals (TREE_TYPE (parm));
734 for (tree fields = TYPE_FIELDS (current_class_type);
735 fields; fields = DECL_CHAIN (fields))
737 tree field = fields;
738 tree expr_type;
740 if (TREE_CODE (field) != FIELD_DECL)
741 continue;
743 expr_type = TREE_TYPE (field);
744 if (DECL_NAME (field))
746 if (VFIELD_NAME_P (DECL_NAME (field)))
747 continue;
749 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
750 /* Just use the field; anonymous types can't have
751 nontrivial copy ctors or assignment ops or this
752 function would be deleted. */;
753 else
754 continue;
756 /* Compute the type of "init->field". If the copy-constructor
757 parameter is, for example, "const S&", and the type of
758 the field is "T", then the type will usually be "const
759 T". (There are no cv-qualified variants of reference
760 types.) */
761 if (!TYPE_REF_P (expr_type))
763 int quals = cvquals;
765 if (DECL_MUTABLE_P (field))
766 quals &= ~TYPE_QUAL_CONST;
767 quals |= cp_type_quals (expr_type);
768 expr_type = cp_build_qualified_type (expr_type, quals);
771 tree init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
772 if (move_p && !TYPE_REF_P (expr_type)
773 /* 'move' breaks bit-fields, and has no effect for scalars. */
774 && !scalarish_type_p (expr_type))
775 init = move (init);
776 init = build_tree_list (NULL_TREE, init);
778 member_init_list = tree_cons (field, init, member_init_list);
782 finish_mem_initializers (member_init_list);
786 static void
787 do_build_copy_assign (tree fndecl)
789 tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl));
790 tree compound_stmt;
791 bool move_p = move_fn_p (fndecl);
792 bool trivial = trivial_fn_p (fndecl);
793 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
795 compound_stmt = begin_compound_stmt (0);
796 parm = convert_from_reference (parm);
798 /* If we are building a defaulted xobj copy/move assignment operator then
799 current_class_ref will not have been set up.
800 Kind of an icky hack, but what can ya do? */
801 tree const class_ref = DECL_XOBJ_MEMBER_FUNCTION_P (fndecl)
802 ? cp_build_fold_indirect_ref (DECL_ARGUMENTS (fndecl)) : current_class_ref;
804 if (trivial
805 && is_empty_class (current_class_type))
806 /* Don't copy the padding byte; it might not have been allocated
807 if *this is a base subobject. */;
808 else if (trivial)
810 tree t = build2 (MODIFY_EXPR, void_type_node, class_ref, parm);
811 finish_expr_stmt (t);
813 else
815 tree fields;
816 int cvquals = cp_type_quals (TREE_TYPE (parm));
817 int i;
818 tree binfo, base_binfo;
820 /* Assign to each of the direct base classes. */
821 for (binfo = TYPE_BINFO (current_class_type), i = 0;
822 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
824 tree converted_parm;
826 /* We must convert PARM directly to the base class
827 explicitly since the base class may be ambiguous. */
828 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
829 tf_warning_or_error);
830 if (move_p)
831 converted_parm = move (converted_parm);
832 /* Call the base class assignment operator. */
833 releasing_vec parmvec (make_tree_vector_single (converted_parm));
834 finish_expr_stmt
835 (build_special_member_call (class_ref,
836 assign_op_identifier,
837 &parmvec,
838 base_binfo,
839 flags,
840 tf_warning_or_error));
843 /* Assign to each of the non-static data members. */
844 for (fields = TYPE_FIELDS (current_class_type);
845 fields;
846 fields = DECL_CHAIN (fields))
848 tree comp = class_ref;
849 tree init = parm;
850 tree field = fields;
851 tree expr_type;
852 int quals;
854 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
855 continue;
857 expr_type = TREE_TYPE (field);
859 if (CP_TYPE_CONST_P (expr_type))
861 error ("non-static const member %q#D, cannot use default "
862 "assignment operator", field);
863 continue;
865 else if (TYPE_REF_P (expr_type))
867 error ("non-static reference member %q#D, cannot use "
868 "default assignment operator", field);
869 continue;
872 if (DECL_NAME (field))
874 if (VFIELD_NAME_P (DECL_NAME (field)))
875 continue;
877 else if (ANON_AGGR_TYPE_P (expr_type)
878 && TYPE_FIELDS (expr_type) != NULL_TREE)
879 /* Just use the field; anonymous types can't have
880 nontrivial copy ctors or assignment ops or this
881 function would be deleted. */;
882 else
883 continue;
885 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
887 /* Compute the type of init->field */
888 quals = cvquals;
889 if (DECL_MUTABLE_P (field))
890 quals &= ~TYPE_QUAL_CONST;
891 expr_type = cp_build_qualified_type (expr_type, quals);
893 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
894 if (move_p && !TYPE_REF_P (expr_type)
895 /* 'move' breaks bit-fields, and has no effect for scalars. */
896 && !scalarish_type_p (expr_type))
897 init = move (init);
899 if (DECL_NAME (field))
900 init = cp_build_modify_expr (input_location, comp, NOP_EXPR, init,
901 tf_warning_or_error);
902 else
903 init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
904 finish_expr_stmt (init);
907 finish_return_stmt (class_ref);
908 finish_compound_stmt (compound_stmt);
911 /* C++20 <compare> comparison category types. */
913 enum comp_cat_tag
915 cc_partial_ordering,
916 cc_weak_ordering,
917 cc_strong_ordering,
918 cc_last
921 /* Names of the comparison categories and their value members, to be indexed by
922 comp_cat_tag enumerators. genericize_spaceship below relies on the ordering
923 of the members. */
925 struct comp_cat_info_t
927 const char *name;
928 const char *members[4];
930 static const comp_cat_info_t comp_cat_info[cc_last]
932 { "partial_ordering", { "equivalent", "greater", "less", "unordered" } },
933 { "weak_ordering", { "equivalent", "greater", "less" } },
934 { "strong_ordering", { "equal", "greater", "less" } }
937 /* A cache of the category types to speed repeated lookups. */
939 static GTY((deletable)) tree comp_cat_cache[cc_last];
941 /* Look up one of the result variables in the comparison category type. */
943 static tree
944 lookup_comparison_result (tree type, const char *name_str,
945 tsubst_flags_t complain = tf_warning_or_error)
947 tree name = get_identifier (name_str);
948 tree decl = lookup_qualified_name (type, name);
949 if (TREE_CODE (decl) != VAR_DECL)
951 if (complain & tf_error)
953 auto_diagnostic_group d;
954 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
955 qualified_name_lookup_error (type, name, decl, input_location);
956 else
957 error ("%qD is not a static data member", decl);
958 inform (input_location, "determining value of %qs", "operator<=>");
960 return error_mark_node;
962 return decl;
965 /* Look up a <compare> comparison category type in std. */
967 static tree
968 lookup_comparison_category (comp_cat_tag tag,
969 tsubst_flags_t complain = tf_warning_or_error)
971 if (tree cached = comp_cat_cache[tag])
972 return cached;
974 tree name = get_identifier (comp_cat_info[tag].name);
975 tree decl = lookup_qualified_name (std_node, name);
976 if (TREE_CODE (decl) != TYPE_DECL)
978 if (complain & tf_error)
980 auto_diagnostic_group d;
981 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
982 qualified_name_lookup_error (std_node, name, decl, input_location);
983 else
984 error ("%qD is not a type", decl);
985 inform (input_location, "forming type of %qs", "operator<=>");
987 return error_mark_node;
989 /* Also make sure we can look up the value members now, since we won't
990 really use them until genericize time. */
991 tree type = TREE_TYPE (decl);
992 for (int i = 0; i < 4; ++i)
994 const char *p = comp_cat_info[tag].members[i];
995 if (!p) break;
996 if (lookup_comparison_result (type, p, complain)
997 == error_mark_node)
998 return error_mark_node;
1000 return comp_cat_cache[tag] = type;
1003 /* Wrapper that takes the tag rather than the type. */
1005 static tree
1006 lookup_comparison_result (comp_cat_tag tag, const char *name_str,
1007 tsubst_flags_t complain = tf_warning_or_error)
1009 tree type = lookup_comparison_category (tag, complain);
1010 return lookup_comparison_result (type, name_str, complain);
1013 /* Wrapper that takes the index into the members array instead of the name. */
1015 static tree
1016 lookup_comparison_result (comp_cat_tag tag, tree type, int idx)
1018 const char *name_str = comp_cat_info[tag].members[idx];
1019 if (!name_str)
1020 return NULL_TREE;
1021 return lookup_comparison_result (type, name_str);
1024 /* Does TYPE correspond to TAG? */
1026 static bool
1027 is_cat (tree type, comp_cat_tag tag)
1029 tree name = TYPE_LINKAGE_IDENTIFIER (type);
1030 return id_equal (name, comp_cat_info[tag].name);
1033 /* Return the comp_cat_tag for TYPE. */
1035 static comp_cat_tag
1036 cat_tag_for (tree type)
1038 if (!CLASS_TYPE_P (type) || !decl_in_std_namespace_p (TYPE_MAIN_DECL (type)))
1039 return cc_last;
1040 for (int i = 0; i < cc_last; ++i)
1042 comp_cat_tag tag = (comp_cat_tag)i;
1043 if (is_cat (type, tag))
1044 return tag;
1046 return cc_last;
1049 /* Return the comparison category tag of a <=> expression with non-class type
1050 OPTYPE. */
1052 static comp_cat_tag
1053 spaceship_comp_cat (tree optype)
1055 if (INTEGRAL_OR_ENUMERATION_TYPE_P (optype) || TYPE_PTROBV_P (optype))
1056 return cc_strong_ordering;
1057 else if (SCALAR_FLOAT_TYPE_P (optype))
1058 return cc_partial_ordering;
1060 /* ??? should vector <=> produce a vector of one of the above? */
1061 gcc_unreachable ();
1064 /* Return the comparison category type of a <=> expression with non-class type
1065 OPTYPE. */
1067 tree
1068 spaceship_type (tree optype, tsubst_flags_t complain)
1070 comp_cat_tag tag = spaceship_comp_cat (optype);
1071 return lookup_comparison_category (tag, complain);
1074 /* Turn <=> with type TYPE and operands OP0 and OP1 into GENERIC.
1075 This is also used by build_comparison_op for fallback to op< and op==
1076 in a defaulted op<=>. */
1078 tree
1079 genericize_spaceship (location_t loc, tree type, tree op0, tree op1)
1081 /* ??? maybe optimize based on knowledge of representation? */
1082 comp_cat_tag tag = cat_tag_for (type);
1084 if (tag == cc_last && is_auto (type))
1086 /* build_comparison_op is checking to see if we want to suggest changing
1087 the op<=> return type from auto to a specific comparison category; any
1088 category will do for now. */
1089 tag = cc_strong_ordering;
1090 type = lookup_comparison_category (tag, tf_none);
1091 if (type == error_mark_node)
1092 return error_mark_node;
1095 gcc_checking_assert (tag < cc_last);
1097 tree r;
1098 bool scalar = SCALAR_TYPE_P (TREE_TYPE (op0));
1099 if (scalar)
1101 op0 = save_expr (op0);
1102 op1 = save_expr (op1);
1105 tree gt = lookup_comparison_result (tag, type, 1);
1107 int flags = LOOKUP_NORMAL;
1108 tsubst_flags_t complain = tf_none;
1109 tree comp;
1111 if (tag == cc_partial_ordering)
1113 /* op0 == op1 ? equivalent : op0 < op1 ? less :
1114 op1 < op0 ? greater : unordered */
1115 tree uo = lookup_comparison_result (tag, type, 3);
1116 if (scalar)
1118 /* For scalars use the low level operations; using build_new_op causes
1119 trouble with constexpr eval in the middle of genericize (100367). */
1120 comp = fold_build2 (LT_EXPR, boolean_type_node, op1, op0);
1121 r = fold_build3 (COND_EXPR, type, comp, gt, uo);
1123 else
1125 comp = build_new_op (loc, LT_EXPR, flags, op1, op0, complain);
1126 r = build_conditional_expr (loc, comp, gt, uo, complain);
1129 else
1130 /* op0 == op1 ? equal : op0 < op1 ? less : greater */
1131 r = gt;
1133 tree lt = lookup_comparison_result (tag, type, 2);
1134 if (scalar)
1136 comp = fold_build2 (LT_EXPR, boolean_type_node, op0, op1);
1137 r = fold_build3 (COND_EXPR, type, comp, lt, r);
1139 else
1141 comp = build_new_op (loc, LT_EXPR, flags, op0, op1, complain);
1142 r = build_conditional_expr (loc, comp, lt, r, complain);
1145 tree eq = lookup_comparison_result (tag, type, 0);
1146 if (scalar)
1148 comp = fold_build2 (EQ_EXPR, boolean_type_node, op0, op1);
1149 r = fold_build3 (COND_EXPR, type, comp, eq, r);
1151 else
1153 comp = build_new_op (loc, EQ_EXPR, flags, op0, op1, complain);
1154 r = build_conditional_expr (loc, comp, eq, r, complain);
1157 return r;
1160 /* Check that the signature of a defaulted comparison operator is
1161 well-formed. */
1163 static bool
1164 early_check_defaulted_comparison (tree fn)
1166 location_t loc = DECL_SOURCE_LOCATION (fn);
1167 tree ctx;
1168 if (DECL_CLASS_SCOPE_P (fn))
1169 ctx = DECL_CONTEXT (fn);
1170 else
1171 ctx = DECL_FRIEND_CONTEXT (fn);
1172 bool ok = true;
1174 if (cxx_dialect < cxx20)
1176 error_at (loc, "defaulted %qD only available with %<-std=c++20%> or "
1177 "%<-std=gnu++20%>", fn);
1178 return false;
1181 if (!DECL_OVERLOADED_OPERATOR_IS (fn, SPACESHIP_EXPR)
1182 && !same_type_p (TREE_TYPE (TREE_TYPE (fn)), boolean_type_node))
1184 diagnostic_t kind = DK_UNSPECIFIED;
1185 int opt = 0;
1186 if (is_auto (TREE_TYPE (fn)))
1187 kind = DK_PEDWARN;
1188 else
1189 kind = DK_ERROR;
1190 emit_diagnostic (kind, loc, opt,
1191 "defaulted %qD must return %<bool%>", fn);
1192 if (kind == DK_ERROR)
1193 ok = false;
1196 bool mem = DECL_IOBJ_MEMBER_FUNCTION_P (fn);
1197 if (mem && type_memfn_quals (TREE_TYPE (fn)) != TYPE_QUAL_CONST)
1199 error_at (loc, "defaulted %qD must be %<const%>", fn);
1200 ok = false;
1202 if (mem && type_memfn_rqual (TREE_TYPE (fn)) == REF_QUAL_RVALUE)
1204 error_at (loc, "defaulted %qD must not have %<&&%> ref-qualifier", fn);
1205 ok = false;
1207 tree parmnode = FUNCTION_FIRST_USER_PARMTYPE (fn);
1208 bool saw_byval = false;
1209 bool saw_byref = mem;
1210 bool saw_bad = false;
1211 for (; parmnode != void_list_node; parmnode = TREE_CHAIN (parmnode))
1213 tree parmtype = TREE_VALUE (parmnode);
1214 if (CLASS_TYPE_P (parmtype))
1215 saw_byval = true;
1216 else if (TREE_CODE (parmtype) == REFERENCE_TYPE
1217 && !TYPE_REF_IS_RVALUE (parmtype)
1218 && TYPE_QUALS (TREE_TYPE (parmtype)) == TYPE_QUAL_CONST)
1220 saw_byref = true;
1221 parmtype = TREE_TYPE (parmtype);
1223 else
1224 saw_bad = true;
1226 if (!saw_bad && !ctx)
1228 /* Defaulted outside the class body. */
1229 ctx = TYPE_MAIN_VARIANT (parmtype);
1230 if (!is_friend (ctx, fn))
1231 error_at (loc, "defaulted %qD is not a friend of %qT", fn, ctx);
1233 else if (!same_type_ignoring_top_level_qualifiers_p (parmtype, ctx))
1234 saw_bad = true;
1237 if (saw_bad || (saw_byval && saw_byref))
1239 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
1240 error_at (loc, "defaulted member %qD must have parameter type "
1241 "%<const %T&%>", fn, ctx);
1242 else if (saw_bad)
1243 error_at (loc, "defaulted %qD must have parameters of either type "
1244 "%<const %T&%> or %qT", fn, ctx, ctx);
1245 else
1246 error_at (loc, "defaulted %qD must have parameters of either type "
1247 "%<const %T&%> or %qT, not both", fn, ctx, ctx);
1248 ok = false;
1251 /* We still need to deduce deleted/constexpr/noexcept and maybe return. */
1252 DECL_MAYBE_DELETED (fn) = ok;
1254 return ok;
1257 /* Subroutine of build_comparison_op. Given the vec of memberwise
1258 comparisons COMPS, calculate the overall comparison category for
1259 operator<=>. */
1261 static tree
1262 common_comparison_type (vec<tree> &comps)
1264 tree seen[cc_last] = {};
1266 for (unsigned i = 0; i < comps.length(); ++i)
1268 tree comp = comps[i];
1269 if (TREE_CODE (comp) == TREE_LIST)
1270 comp = TREE_VALUE (comp);
1271 tree ctype = TREE_TYPE (comp);
1272 comp_cat_tag tag = cat_tag_for (ctype);
1273 /* build_comparison_op already checked this. */
1274 gcc_checking_assert (tag < cc_last);
1275 seen[tag] = ctype;
1278 /* Otherwise, if at least one T i is std::partial_ordering, U is
1279 std::partial_ordering. */
1280 if (tree t = seen[cc_partial_ordering]) return t;
1282 /* Otherwise, if at least one T i is std::weak_ordering, U is
1283 std::weak_ordering. */
1284 if (tree t = seen[cc_weak_ordering]) return t;
1286 /* Otherwise, U is std::strong_ordering. */
1287 if (tree t = seen[cc_strong_ordering]) return t;
1288 return lookup_comparison_category (cc_strong_ordering);
1291 /* Data structure for build_comparison_op. */
1293 struct comp_info
1295 tree fndecl;
1296 location_t loc;
1297 tsubst_flags_t complain;
1298 tree_code code;
1299 comp_cat_tag retcat;
1300 bool first_time;
1301 bool constexp;
1302 bool was_constexp;
1303 bool noex;
1305 comp_info (tree fndecl, tsubst_flags_t complain)
1306 : fndecl (fndecl), complain (complain)
1308 loc = DECL_SOURCE_LOCATION (fndecl);
1310 first_time = DECL_MAYBE_DELETED (fndecl);
1311 DECL_MAYBE_DELETED (fndecl) = false;
1313 /* Do we want to try to set constexpr? */
1314 was_constexp = DECL_DECLARED_CONSTEXPR_P (fndecl);
1315 constexp = first_time;
1316 if (constexp)
1317 /* Set this for var_in_constexpr_fn. */
1318 DECL_DECLARED_CONSTEXPR_P (fndecl) = true;
1320 /* Do we want to try to set noexcept? */
1321 noex = first_time;
1322 if (noex)
1324 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fndecl));
1325 if (raises && !UNEVALUATED_NOEXCEPT_SPEC_P (raises))
1326 /* There was an explicit exception-specification. */
1327 noex = false;
1331 /* EXPR is an expression built as part of the function body.
1332 Adjust the properties appropriately. */
1333 void check (tree expr)
1335 if (expr == error_mark_node)
1336 DECL_DELETED_FN (fndecl) = true;
1337 if ((constexp || was_constexp)
1338 && !potential_rvalue_constant_expression (expr))
1340 if (was_constexp)
1341 require_potential_rvalue_constant_expression_fncheck (expr);
1342 else
1343 constexp = false;
1345 if (noex && !expr_noexcept_p (expr, tf_none))
1346 noex = false;
1349 ~comp_info ()
1351 if (first_time)
1353 DECL_DECLARED_CONSTEXPR_P (fndecl) = constexp || was_constexp;
1354 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fndecl));
1355 if (!raises || UNEVALUATED_NOEXCEPT_SPEC_P (raises))
1357 raises = noex ? noexcept_true_spec : noexcept_false_spec;
1358 TREE_TYPE (fndecl) = build_exception_variant (TREE_TYPE (fndecl),
1359 raises);
1365 /* Subroutine of build_comparison_op, to compare a single subobject. */
1367 static tree
1368 do_one_comp (location_t loc, const comp_info &info, tree sub, tree lhs, tree rhs)
1370 const tree_code code = info.code;
1371 const tree fndecl = info.fndecl;
1372 const comp_cat_tag retcat = info.retcat;
1373 const tsubst_flags_t complain = info.complain;
1375 tree overload = NULL_TREE;
1376 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
1377 /* If we have an explicit comparison category return type we can fall back
1378 to </=, so don't give an error yet if <=> lookup fails. */
1379 bool tentative = retcat != cc_last;
1380 tree comp = build_new_op (loc, code, flags, lhs, rhs,
1381 NULL_TREE, NULL_TREE, &overload,
1382 tentative ? tf_none : complain);
1384 if (code != SPACESHIP_EXPR)
1385 return comp;
1387 tree rettype = TREE_TYPE (TREE_TYPE (fndecl));
1389 if (comp == error_mark_node)
1391 if (overload == NULL_TREE && (tentative || complain))
1393 /* No viable <=>, try using op< and op==. */
1394 tree lteq = genericize_spaceship (loc, rettype, lhs, rhs);
1395 if (lteq != error_mark_node)
1397 /* We found usable < and ==. */
1398 if (retcat != cc_last)
1399 /* Return type is a comparison category, use them. */
1400 comp = lteq;
1401 else if (complain & tf_error)
1402 /* Return type is auto, suggest changing it. */
1403 inform (info.loc, "changing the return type from %qs "
1404 "to a comparison category type will allow the "
1405 "comparison to use %qs and %qs", "auto",
1406 "operator<", "operator==");
1408 else if (tentative && complain)
1409 /* No usable < and ==, give an error for op<=>. */
1410 build_new_op (loc, code, flags, lhs, rhs, complain);
1412 if (comp == error_mark_node)
1413 return error_mark_node;
1416 if (FNDECL_USED_AUTO (fndecl)
1417 && cat_tag_for (TREE_TYPE (comp)) == cc_last)
1419 /* The operator function is defined as deleted if ... Ri is not a
1420 comparison category type. */
1421 if (complain & tf_error)
1422 inform (loc,
1423 "three-way comparison of %qD has type %qT, not a "
1424 "comparison category type", sub, TREE_TYPE (comp));
1425 return error_mark_node;
1427 else if (!FNDECL_USED_AUTO (fndecl)
1428 && !can_convert (rettype, TREE_TYPE (comp), complain))
1430 if (complain & tf_error)
1431 error_at (loc,
1432 "three-way comparison of %qD has type %qT, which "
1433 "does not convert to %qT",
1434 sub, TREE_TYPE (comp), rettype);
1435 return error_mark_node;
1438 return comp;
1441 /* Build up the definition of a defaulted comparison operator. Unlike other
1442 defaulted functions that use synthesized_method_walk to determine whether
1443 the function is e.g. deleted, for comparisons we use the same code. We try
1444 to use synthesize_method at the earliest opportunity and bail out if the
1445 function ends up being deleted. */
1447 void
1448 build_comparison_op (tree fndecl, bool defining, tsubst_flags_t complain)
1450 comp_info info (fndecl, complain);
1452 if (!defining && !(complain & tf_error) && !DECL_MAYBE_DELETED (fndecl))
1453 return;
1455 int flags = LOOKUP_NORMAL;
1456 const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (DECL_NAME (fndecl));
1457 tree_code code = info.code = op->tree_code;
1459 tree lhs = DECL_ARGUMENTS (fndecl);
1460 tree rhs = DECL_CHAIN (lhs);
1461 if (is_this_parameter (lhs))
1462 lhs = cp_build_fold_indirect_ref (lhs);
1463 else
1464 lhs = convert_from_reference (lhs);
1465 rhs = convert_from_reference (rhs);
1466 tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (lhs));
1467 gcc_assert (!defining || COMPLETE_TYPE_P (ctype));
1469 iloc_sentinel ils (info.loc);
1471 /* A defaulted comparison operator function for class C is defined as
1472 deleted if ... C has variant members. */
1473 if (TREE_CODE (ctype) == UNION_TYPE
1474 && next_aggregate_field (TYPE_FIELDS (ctype)))
1476 if (complain & tf_error)
1477 inform (info.loc, "cannot default compare union %qT", ctype);
1478 DECL_DELETED_FN (fndecl) = true;
1479 return;
1482 tree compound_stmt = NULL_TREE;
1483 if (defining)
1484 compound_stmt = begin_compound_stmt (0);
1485 else
1486 ++cp_unevaluated_operand;
1488 tree rettype = TREE_TYPE (TREE_TYPE (fndecl));
1489 if (code != SPACESHIP_EXPR && is_auto (rettype))
1491 rettype = boolean_type_node;
1492 apply_deduced_return_type (fndecl, rettype);
1495 if (code == EQ_EXPR || code == SPACESHIP_EXPR)
1497 comp_cat_tag &retcat = (info.retcat = cc_last);
1498 if (code == SPACESHIP_EXPR && !FNDECL_USED_AUTO (fndecl))
1499 retcat = cat_tag_for (rettype);
1501 bool bad = false;
1502 auto_vec<tree> comps;
1504 /* Compare the base subobjects. We handle them this way, rather than in
1505 the field loop below, because maybe_instantiate_noexcept might bring
1506 us here before we've built the base fields. */
1507 for (tree base_binfo : BINFO_BASE_BINFOS (TYPE_BINFO (ctype)))
1509 tree lhs_base
1510 = build_base_path (PLUS_EXPR, lhs, base_binfo, 0, complain);
1511 tree rhs_base
1512 = build_base_path (PLUS_EXPR, rhs, base_binfo, 0, complain);
1514 location_t loc = DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (ctype));
1515 tree comp = do_one_comp (loc, info, BINFO_TYPE (base_binfo),
1516 lhs_base, rhs_base);
1517 if (comp == error_mark_node)
1519 bad = true;
1520 continue;
1523 comps.safe_push (comp);
1526 /* Now compare the field subobjects. */
1527 for (tree field = next_aggregate_field (TYPE_FIELDS (ctype));
1528 field;
1529 field = next_aggregate_field (DECL_CHAIN (field)))
1531 if (DECL_VIRTUAL_P (field) || DECL_FIELD_IS_BASE (field))
1532 /* We ignore the vptr, and we already handled bases. */
1533 continue;
1535 tree expr_type = TREE_TYPE (field);
1537 location_t field_loc = DECL_SOURCE_LOCATION (field);
1539 /* A defaulted comparison operator function for class C is defined as
1540 deleted if any non-static data member of C is of reference type or
1541 C has variant members. */
1542 if (TREE_CODE (expr_type) == REFERENCE_TYPE)
1544 if (complain & tf_error)
1545 inform (field_loc, "cannot default compare "
1546 "reference member %qD", field);
1547 bad = true;
1548 continue;
1550 else if (ANON_UNION_TYPE_P (expr_type)
1551 && next_aggregate_field (TYPE_FIELDS (expr_type)))
1553 if (complain & tf_error)
1554 inform (field_loc, "cannot default compare "
1555 "anonymous union member");
1556 bad = true;
1557 continue;
1560 tree lhs_mem = build3_loc (field_loc, COMPONENT_REF, expr_type, lhs,
1561 field, NULL_TREE);
1562 tree rhs_mem = build3_loc (field_loc, COMPONENT_REF, expr_type, rhs,
1563 field, NULL_TREE);
1564 tree loop_indexes = NULL_TREE;
1565 while (TREE_CODE (expr_type) == ARRAY_TYPE)
1567 /* Flexible array member. */
1568 if (TYPE_DOMAIN (expr_type) == NULL_TREE
1569 || TYPE_MAX_VALUE (TYPE_DOMAIN (expr_type)) == NULL_TREE)
1571 if (complain & tf_error)
1572 inform (field_loc, "cannot default compare "
1573 "flexible array member");
1574 bad = true;
1575 break;
1577 tree maxval = TYPE_MAX_VALUE (TYPE_DOMAIN (expr_type));
1578 /* [0] array. No subobjects to compare, just skip it. */
1579 if (integer_all_onesp (maxval))
1580 break;
1581 tree idx;
1582 /* [1] array, no loop needed, just add [0] ARRAY_REF.
1583 Similarly if !defining. */
1584 if (integer_zerop (maxval) || !defining)
1585 idx = size_zero_node;
1586 /* Some other array, will need runtime loop. */
1587 else
1589 idx = force_target_expr (sizetype, maxval, complain);
1590 loop_indexes = tree_cons (idx, NULL_TREE, loop_indexes);
1592 expr_type = TREE_TYPE (expr_type);
1593 lhs_mem = build4_loc (field_loc, ARRAY_REF, expr_type, lhs_mem,
1594 idx, NULL_TREE, NULL_TREE);
1595 rhs_mem = build4_loc (field_loc, ARRAY_REF, expr_type, rhs_mem,
1596 idx, NULL_TREE, NULL_TREE);
1598 if (TREE_CODE (expr_type) == ARRAY_TYPE)
1599 continue;
1601 tree comp = do_one_comp (field_loc, info, field, lhs_mem, rhs_mem);
1602 if (comp == error_mark_node)
1604 bad = true;
1605 continue;
1608 /* Most of the time, comp is the expression that should be evaluated
1609 to compare the two members. If the expression needs to be
1610 evaluated more than once in a loop, it will be a TREE_LIST
1611 instead, whose TREE_VALUE is the expression for one array element,
1612 TREE_PURPOSE is innermost iterator temporary and if the array
1613 is multidimensional, TREE_CHAIN will contain another TREE_LIST
1614 with second innermost iterator in its TREE_PURPOSE and so on. */
1615 if (loop_indexes)
1617 TREE_VALUE (loop_indexes) = comp;
1618 comp = loop_indexes;
1620 comps.safe_push (comp);
1622 if (code == SPACESHIP_EXPR && is_auto (rettype))
1624 rettype = common_comparison_type (comps);
1625 apply_deduced_return_type (fndecl, rettype);
1627 if (bad)
1629 DECL_DELETED_FN (fndecl) = true;
1630 goto out;
1632 for (unsigned i = 0; i < comps.length(); ++i)
1634 tree comp = comps[i];
1635 tree eq, retval = NULL_TREE, if_ = NULL_TREE;
1636 tree loop_indexes = NULL_TREE;
1637 if (defining)
1639 if (TREE_CODE (comp) == TREE_LIST)
1641 loop_indexes = comp;
1642 comp = TREE_VALUE (comp);
1643 loop_indexes = nreverse (loop_indexes);
1644 for (tree loop_index = loop_indexes; loop_index;
1645 loop_index = TREE_CHAIN (loop_index))
1647 tree for_stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
1648 tree idx = TREE_PURPOSE (loop_index);
1649 tree maxval = TARGET_EXPR_INITIAL (idx);
1650 TARGET_EXPR_INITIAL (idx) = size_zero_node;
1651 add_stmt (idx);
1652 finish_init_stmt (for_stmt);
1653 finish_for_cond (build2 (LE_EXPR, boolean_type_node, idx,
1654 maxval), for_stmt, false, 0,
1655 false);
1656 finish_for_expr (cp_build_unary_op (PREINCREMENT_EXPR,
1657 TARGET_EXPR_SLOT (idx),
1658 false, complain),
1659 for_stmt);
1660 /* Store in TREE_VALUE the for_stmt tree, so that we can
1661 later on call finish_for_stmt on it (in the reverse
1662 order). */
1663 TREE_VALUE (loop_index) = for_stmt;
1665 loop_indexes = nreverse (loop_indexes);
1667 if_ = begin_if_stmt ();
1669 /* Spaceship is specified to use !=, but for the comparison category
1670 types, != is equivalent to !(==), so let's use == directly. */
1671 if (code == EQ_EXPR)
1673 /* if (x==y); else return false; */
1674 eq = comp;
1675 retval = boolean_false_node;
1677 else
1679 /* if (auto v = x<=>y, v == 0); else return v; */
1680 if (TREE_CODE (comp) == SPACESHIP_EXPR)
1681 TREE_TYPE (comp) = rettype;
1682 else
1683 comp = build_static_cast (input_location, rettype, comp,
1684 complain);
1685 info.check (comp);
1686 if (defining)
1688 tree var = create_temporary_var (rettype);
1689 DECL_NAME (var) = get_identifier ("retval");
1690 pushdecl (var);
1691 cp_finish_decl (var, comp, false, NULL_TREE, flags);
1692 comp = retval = var;
1694 eq = build_new_op (info.loc, EQ_EXPR, flags, comp,
1695 integer_zero_node, NULL_TREE, NULL_TREE,
1696 NULL, complain);
1698 tree ceq = contextual_conv_bool (eq, complain);
1699 info.check (ceq);
1700 if (defining)
1702 finish_if_stmt_cond (ceq, if_);
1703 finish_then_clause (if_);
1704 begin_else_clause (if_);
1705 finish_return_stmt (retval);
1706 finish_else_clause (if_);
1707 finish_if_stmt (if_);
1708 for (tree loop_index = loop_indexes; loop_index;
1709 loop_index = TREE_CHAIN (loop_index))
1710 finish_for_stmt (TREE_VALUE (loop_index));
1713 if (defining)
1715 tree val;
1716 if (code == EQ_EXPR)
1717 val = boolean_true_node;
1718 else
1720 tree seql = lookup_comparison_result (cc_strong_ordering,
1721 "equal", complain);
1722 val = build_static_cast (input_location, rettype, seql,
1723 complain);
1725 finish_return_stmt (val);
1728 else if (code == NE_EXPR)
1730 tree comp = build_new_op (info.loc, EQ_EXPR, flags, lhs, rhs,
1731 NULL_TREE, NULL_TREE, NULL, complain);
1732 comp = contextual_conv_bool (comp, complain);
1733 info.check (comp);
1734 if (defining)
1736 tree neg = build1 (TRUTH_NOT_EXPR, boolean_type_node, comp);
1737 finish_return_stmt (neg);
1740 else
1742 tree comp = build_new_op (info.loc, SPACESHIP_EXPR, flags, lhs, rhs,
1743 NULL_TREE, NULL_TREE, NULL, complain);
1744 tree comp2 = build_new_op (info.loc, code, flags, comp, integer_zero_node,
1745 NULL_TREE, NULL_TREE, NULL, complain);
1746 info.check (comp2);
1747 if (defining)
1748 finish_return_stmt (comp2);
1751 out:
1752 if (defining)
1753 finish_compound_stmt (compound_stmt);
1754 else
1755 --cp_unevaluated_operand;
1758 /* True iff DECL is an implicitly-declared special member function with no real
1759 source location, so we can use its DECL_SOURCE_LOCATION to remember where we
1760 triggered its synthesis. */
1762 bool
1763 decl_remember_implicit_trigger_p (tree decl)
1765 if (!DECL_ARTIFICIAL (decl))
1766 return false;
1767 special_function_kind sfk = special_function_p (decl);
1768 /* Inherited constructors have the location of their using-declaration, and
1769 operator== has the location of the corresponding operator<=>. */
1770 return (sfk != sfk_inheriting_constructor
1771 && sfk != sfk_comparison);
1774 /* Synthesize FNDECL, a non-static member function. */
1776 void
1777 synthesize_method (tree fndecl)
1779 bool need_body = true;
1780 tree stmt;
1781 location_t save_input_location = input_location;
1782 int error_count = errorcount;
1783 int warning_count = warningcount + werrorcount;
1784 special_function_kind sfk = special_function_p (fndecl);
1786 /* Reset the source location, we might have been previously
1787 deferred, and thus have saved where we were first needed. */
1788 if (decl_remember_implicit_trigger_p (fndecl))
1789 DECL_SOURCE_LOCATION (fndecl)
1790 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
1792 /* If we've been asked to synthesize a clone, just synthesize the
1793 cloned function instead. Doing so will automatically fill in the
1794 body for the clone. */
1795 if (DECL_CLONED_FUNCTION_P (fndecl))
1796 fndecl = DECL_CLONED_FUNCTION (fndecl);
1798 /* We may be in the middle of deferred access check. Disable
1799 it now. */
1800 push_deferring_access_checks (dk_no_deferred);
1802 bool push_to_top = maybe_push_to_top_level (fndecl);
1804 input_location = DECL_SOURCE_LOCATION (fndecl);
1806 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
1807 stmt = begin_function_body ();
1809 if (DECL_ASSIGNMENT_OPERATOR_P (fndecl)
1810 && DECL_OVERLOADED_OPERATOR_IS (fndecl, NOP_EXPR))
1812 do_build_copy_assign (fndecl);
1813 need_body = false;
1815 else if (DECL_CONSTRUCTOR_P (fndecl))
1817 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
1818 if (arg_chain != void_list_node)
1819 do_build_copy_constructor (fndecl);
1820 else
1821 finish_mem_initializers (NULL_TREE);
1823 else if (sfk == sfk_comparison)
1825 /* Pass tf_none so the function is just deleted if there's a problem. */
1826 build_comparison_op (fndecl, true, tf_none);
1827 need_body = false;
1830 /* If we haven't yet generated the body of the function, just
1831 generate an empty compound statement. */
1832 if (need_body)
1834 tree compound_stmt;
1835 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
1836 finish_compound_stmt (compound_stmt);
1839 finish_function_body (stmt);
1840 finish_function (/*inline_p=*/false);
1842 if (!DECL_DELETED_FN (fndecl))
1843 expand_or_defer_fn (fndecl);
1845 input_location = save_input_location;
1847 maybe_pop_from_top_level (push_to_top);
1849 pop_deferring_access_checks ();
1851 if (error_count != errorcount || warning_count != warningcount + werrorcount)
1852 if (DECL_ARTIFICIAL (fndecl))
1853 inform (input_location, "synthesized method %qD first required here",
1854 fndecl);
1857 /* Like synthesize_method, but don't actually synthesize defaulted comparison
1858 methods if their class is still incomplete. Just deduce the return
1859 type in that case. */
1861 void
1862 maybe_synthesize_method (tree fndecl)
1864 if (special_function_p (fndecl) == sfk_comparison)
1866 tree lhs = DECL_ARGUMENTS (fndecl);
1867 if (is_this_parameter (lhs))
1868 lhs = cp_build_fold_indirect_ref (lhs);
1869 else
1870 lhs = convert_from_reference (lhs);
1871 tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (lhs));
1872 if (!COMPLETE_TYPE_P (ctype))
1874 push_deferring_access_checks (dk_no_deferred);
1875 build_comparison_op (fndecl, false, tf_none);
1876 pop_deferring_access_checks ();
1877 return;
1880 return synthesize_method (fndecl);
1883 /* Build a reference to type TYPE with cv-quals QUALS, which is an
1884 rvalue if RVALUE is true. */
1886 static tree
1887 build_stub_type (tree type, int quals, bool rvalue)
1889 tree argtype = cp_build_qualified_type (type, quals);
1890 return cp_build_reference_type (argtype, rvalue);
1893 /* Build a dummy glvalue from dereferencing a dummy reference of type
1894 REFTYPE. */
1896 tree
1897 build_stub_object (tree reftype)
1899 if (!TYPE_REF_P (reftype))
1900 reftype = cp_build_reference_type (reftype, /*rval*/true);
1901 tree stub = build1 (CONVERT_EXPR, reftype, integer_one_node);
1902 return convert_from_reference (stub);
1905 /* Build a std::declval<TYPE>() expression and return it. */
1907 tree
1908 build_trait_object (tree type)
1910 /* TYPE can't be a function with cv-/ref-qualifiers: std::declval is
1911 defined as
1913 template<class T>
1914 typename std::add_rvalue_reference<T>::type declval() noexcept;
1916 and std::add_rvalue_reference yields T when T is a function with
1917 cv- or ref-qualifiers, making the definition ill-formed. */
1918 if (FUNC_OR_METHOD_TYPE_P (type)
1919 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
1920 || type_memfn_rqual (type) != REF_QUAL_NONE))
1921 return error_mark_node;
1923 return build_stub_object (type);
1926 /* Determine which function will be called when looking up NAME in TYPE,
1927 called with a single ARGTYPE argument, or no argument if ARGTYPE is
1928 null. FLAGS and COMPLAIN are as for build_new_method_call.
1930 Returns a FUNCTION_DECL if all is well.
1931 Returns NULL_TREE if overload resolution failed.
1932 Returns error_mark_node if the chosen function cannot be called. */
1934 static tree
1935 locate_fn_flags (tree type, tree name, tree argtype, int flags,
1936 tsubst_flags_t complain)
1938 tree ob, fn, fns, binfo, rval;
1940 if (TYPE_P (type))
1941 binfo = TYPE_BINFO (type);
1942 else
1944 binfo = type;
1945 type = BINFO_TYPE (binfo);
1948 ob = build_stub_object (cp_build_reference_type (type, false));
1949 releasing_vec args;
1950 if (argtype)
1952 if (TREE_CODE (argtype) == TREE_LIST)
1954 for (tree elt = argtype; elt && elt != void_list_node;
1955 elt = TREE_CHAIN (elt))
1957 tree type = TREE_VALUE (elt);
1958 tree arg = build_stub_object (type);
1959 vec_safe_push (args, arg);
1962 else
1964 tree arg = build_stub_object (argtype);
1965 args->quick_push (arg);
1969 fns = lookup_fnfields (binfo, name, 0, complain);
1970 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
1972 if (fn && rval == error_mark_node)
1973 return rval;
1974 else
1975 return fn;
1978 /* Locate the dtor of TYPE. */
1980 tree
1981 get_dtor (tree type, tsubst_flags_t complain)
1983 tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
1984 LOOKUP_NORMAL, complain);
1985 if (fn == error_mark_node)
1986 return NULL_TREE;
1987 return fn;
1990 /* Locate the default ctor of TYPE. */
1992 tree
1993 locate_ctor (tree type)
1995 tree fn;
1997 push_deferring_access_checks (dk_no_check);
1998 fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
1999 LOOKUP_SPECULATIVE, tf_none);
2000 pop_deferring_access_checks ();
2001 if (fn == error_mark_node)
2002 return NULL_TREE;
2003 return fn;
2006 /* Likewise, but give any appropriate errors. */
2008 tree
2009 get_default_ctor (tree type)
2011 tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
2012 LOOKUP_NORMAL, tf_warning_or_error);
2013 if (fn == error_mark_node)
2014 return NULL_TREE;
2015 return fn;
2018 /* Locate the copy ctor of TYPE. */
2020 tree
2021 get_copy_ctor (tree type, tsubst_flags_t complain)
2023 int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
2024 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
2025 tree argtype = build_stub_type (type, quals, false);
2026 tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
2027 LOOKUP_NORMAL, complain);
2028 if (fn == error_mark_node)
2029 return NULL_TREE;
2030 return fn;
2033 /* Locate the copy assignment operator of TYPE. */
2035 tree
2036 get_copy_assign (tree type)
2038 int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
2039 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
2040 tree argtype = build_stub_type (type, quals, false);
2041 tree fn = locate_fn_flags (type, assign_op_identifier, argtype,
2042 LOOKUP_NORMAL, tf_warning_or_error);
2043 if (fn == error_mark_node)
2044 return NULL_TREE;
2045 return fn;
2048 /* walk_tree helper function for is_trivially_xible. If *TP is a call,
2049 return it if it calls something other than a trivial special member
2050 function. */
2052 static tree
2053 check_nontriv (tree *tp, int *, void *)
2055 tree fn = cp_get_callee (*tp);
2056 if (fn == NULL_TREE)
2057 return NULL_TREE;
2059 if (TREE_CODE (fn) == ADDR_EXPR)
2060 fn = TREE_OPERAND (fn, 0);
2062 if (TREE_CODE (fn) != FUNCTION_DECL
2063 || !trivial_fn_p (fn))
2064 return fn;
2065 return NULL_TREE;
2068 /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */
2070 static tree
2071 assignable_expr (tree to, tree from)
2073 cp_unevaluated cp_uneval_guard;
2074 to = build_trait_object (to);
2075 from = build_trait_object (from);
2076 tree r = cp_build_modify_expr (input_location, to, NOP_EXPR, from, tf_none);
2077 return r;
2080 /* The predicate condition for a template specialization
2081 is_constructible<T, Args...> shall be satisfied if and only if the
2082 following variable definition would be well-formed for some invented
2083 variable t: T t(create<Args>()...);
2085 Return something equivalent in well-formedness and triviality. */
2087 static tree
2088 constructible_expr (tree to, tree from)
2090 tree expr;
2091 cp_unevaluated cp_uneval_guard;
2092 const int len = TREE_VEC_LENGTH (from);
2093 if (CLASS_TYPE_P (to))
2095 tree ctype = to;
2096 vec<tree, va_gc> *args = NULL;
2097 if (!TYPE_REF_P (to))
2098 to = cp_build_reference_type (to, /*rval*/false);
2099 tree ob = build_stub_object (to);
2100 if (len == 0)
2101 expr = build_value_init (ctype, tf_none);
2102 else
2104 vec_alloc (args, len);
2105 for (tree arg : tree_vec_range (from))
2106 args->quick_push (build_stub_object (arg));
2107 expr = build_special_member_call (ob, complete_ctor_identifier, &args,
2108 ctype, LOOKUP_NORMAL, tf_none);
2110 if (expr == error_mark_node)
2111 return error_mark_node;
2112 /* The current state of the standard vis-a-vis LWG 2116 is that
2113 is_*constructible involves destruction as well. */
2114 if (type_build_dtor_call (ctype))
2116 tree dtor = build_special_member_call (ob, complete_dtor_identifier,
2117 NULL, ctype, LOOKUP_NORMAL,
2118 tf_none);
2119 if (dtor == error_mark_node)
2120 return error_mark_node;
2121 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
2122 expr = build2 (COMPOUND_EXPR, void_type_node, expr, dtor);
2125 else
2127 if (len == 0)
2128 return build_value_init (strip_array_types (to), tf_none);
2129 if (len > 1)
2131 if (cxx_dialect < cxx20)
2132 /* Too many initializers. */
2133 return error_mark_node;
2135 /* In C++20 this is well-formed:
2136 using T = int[2];
2137 T t(1, 2);
2138 which means that std::is_constructible_v<int[2], int, int>
2139 should be true. */
2140 vec<constructor_elt, va_gc> *v;
2141 vec_alloc (v, len);
2142 for (tree arg : tree_vec_range (from))
2144 tree stub = build_stub_object (arg);
2145 constructor_elt elt = { NULL_TREE, stub };
2146 v->quick_push (elt);
2148 from = build_constructor (init_list_type_node, v);
2149 CONSTRUCTOR_IS_DIRECT_INIT (from) = true;
2150 CONSTRUCTOR_IS_PAREN_INIT (from) = true;
2152 else
2153 from = build_stub_object (TREE_VEC_ELT (from, 0));
2154 expr = perform_direct_initialization_if_possible (to, from,
2155 /*cast*/false,
2156 tf_none);
2157 /* If t(e) didn't work, maybe t{e} will. */
2158 if (expr == NULL_TREE
2159 && len == 1
2160 && cxx_dialect >= cxx20)
2162 from = build_constructor_single (init_list_type_node, NULL_TREE,
2163 from);
2164 CONSTRUCTOR_IS_DIRECT_INIT (from) = true;
2165 CONSTRUCTOR_IS_PAREN_INIT (from) = true;
2166 expr = perform_direct_initialization_if_possible (to, from,
2167 /*cast*/false,
2168 tf_none);
2171 return expr;
2174 /* Returns a tree iff TO is assignable (if CODE is MODIFY_EXPR) or
2175 constructible (otherwise) from FROM, which is a single type for
2176 assignment or a list of types for construction. */
2178 static tree
2179 is_xible_helper (enum tree_code code, tree to, tree from, bool trivial)
2181 to = complete_type (to);
2182 deferring_access_check_sentinel acs (dk_no_deferred);
2183 if (VOID_TYPE_P (to) || ABSTRACT_CLASS_TYPE_P (to)
2184 || (from && FUNC_OR_METHOD_TYPE_P (from)
2185 && (TYPE_READONLY (from) || FUNCTION_REF_QUALIFIED (from))))
2186 return error_mark_node;
2187 tree expr;
2188 if (code == MODIFY_EXPR)
2189 expr = assignable_expr (to, from);
2190 else if (trivial && TREE_VEC_LENGTH (from) > 1
2191 && cxx_dialect < cxx20)
2192 return error_mark_node; // only 0- and 1-argument ctors can be trivial
2193 // before C++20 aggregate paren init
2194 else if (TREE_CODE (to) == ARRAY_TYPE && !TYPE_DOMAIN (to))
2195 return error_mark_node; // can't construct an array of unknown bound
2196 else
2197 expr = constructible_expr (to, from);
2198 return expr;
2201 /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or
2202 constructible (otherwise) from FROM, which is a single type for
2203 assignment or a list of types for construction. */
2205 bool
2206 is_trivially_xible (enum tree_code code, tree to, tree from)
2208 tree expr = is_xible_helper (code, to, from, /*trivial*/true);
2209 if (expr == NULL_TREE || expr == error_mark_node)
2210 return false;
2211 tree nt = cp_walk_tree_without_duplicates (&expr, check_nontriv, NULL);
2212 return !nt;
2215 /* Returns true iff TO is nothrow assignable (if CODE is MODIFY_EXPR) or
2216 constructible (otherwise) from FROM, which is a single type for
2217 assignment or a list of types for construction. */
2219 bool
2220 is_nothrow_xible (enum tree_code code, tree to, tree from)
2222 ++cp_noexcept_operand;
2223 tree expr = is_xible_helper (code, to, from, /*trivial*/false);
2224 --cp_noexcept_operand;
2225 if (expr == NULL_TREE || expr == error_mark_node)
2226 return false;
2227 return expr_noexcept_p (expr, tf_none);
2230 /* Returns true iff TO is assignable (if CODE is MODIFY_EXPR) or
2231 constructible (otherwise) from FROM, which is a single type for
2232 assignment or a list of types for construction. */
2234 bool
2235 is_xible (enum tree_code code, tree to, tree from)
2237 tree expr = is_xible_helper (code, to, from, /*trivial*/false);
2238 if (expr == error_mark_node)
2239 return false;
2240 return !!expr;
2243 /* Return true iff conjunction_v<is_reference<T>, is_constructible<T, U>> is
2244 true, and the initialization
2245 T t(VAL<U>); // DIRECT_INIT_P
2247 T t = VAL<U>; // !DIRECT_INIT_P
2248 binds t to a temporary object whose lifetime is extended.
2249 VAL<T> is defined in [meta.unary.prop]:
2250 -- If T is a reference or function type, VAL<T> is an expression with the
2251 same type and value category as declval<T>().
2252 -- Otherwise, VAL<T> is a prvalue that initially has type T. */
2254 bool
2255 ref_xes_from_temporary (tree to, tree from, bool direct_init_p)
2257 /* Check is_reference<T>. */
2258 if (!TYPE_REF_P (to))
2259 return false;
2260 /* We don't check is_constructible<T, U>: if T isn't constructible
2261 from U, we won't be able to create a conversion. */
2262 tree val = build_trait_object (from);
2263 if (val == error_mark_node)
2264 return false;
2265 if (!TYPE_REF_P (from) && TREE_CODE (from) != FUNCTION_TYPE)
2266 val = CLASS_TYPE_P (from) ? force_rvalue (val, tf_none) : rvalue (val);
2267 return ref_conv_binds_to_temporary (to, val, direct_init_p).is_true ();
2270 /* Worker for is_{,nothrow_}convertible. Attempt to perform an implicit
2271 conversion from FROM to TO and return the result. */
2273 static tree
2274 is_convertible_helper (tree from, tree to)
2276 if (VOID_TYPE_P (from) && VOID_TYPE_P (to))
2277 return integer_one_node;
2278 cp_unevaluated u;
2279 tree expr = build_trait_object (from);
2280 /* std::is_{,nothrow_}convertible test whether the imaginary function
2281 definition
2283 To test() { return std::declval<From>(); }
2285 is well-formed. A function can't return a function. */
2286 if (FUNC_OR_METHOD_TYPE_P (to) || expr == error_mark_node)
2287 return error_mark_node;
2288 deferring_access_check_sentinel acs (dk_no_deferred);
2289 return perform_implicit_conversion (to, expr, tf_none);
2292 /* Return true if FROM can be converted to TO using implicit conversions,
2293 or both FROM and TO are possibly cv-qualified void. NB: This doesn't
2294 implement the "Access checks are performed as if from a context unrelated
2295 to either type" restriction. */
2297 bool
2298 is_convertible (tree from, tree to)
2300 tree expr = is_convertible_helper (from, to);
2301 if (expr == error_mark_node)
2302 return false;
2303 return !!expr;
2306 /* Like is_convertible, but the conversion is also noexcept. */
2308 bool
2309 is_nothrow_convertible (tree from, tree to)
2311 tree expr = is_convertible_helper (from, to);
2312 if (expr == NULL_TREE || expr == error_mark_node)
2313 return false;
2314 return expr_noexcept_p (expr, tf_none);
2317 /* Categorize various special_function_kinds. */
2318 #define SFK_CTOR_P(sfk) \
2319 ((sfk) >= sfk_constructor && (sfk) <= sfk_move_constructor)
2320 #define SFK_DTOR_P(sfk) \
2321 ((sfk) == sfk_destructor || (sfk) == sfk_virtual_destructor)
2322 #define SFK_ASSIGN_P(sfk) \
2323 ((sfk) == sfk_copy_assignment || (sfk) == sfk_move_assignment)
2324 #define SFK_COPY_P(sfk) \
2325 ((sfk) == sfk_copy_constructor || (sfk) == sfk_copy_assignment)
2326 #define SFK_MOVE_P(sfk) \
2327 ((sfk) == sfk_move_constructor || (sfk) == sfk_move_assignment)
2329 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
2330 DELETED_P or give an error message MSG with argument ARG. */
2332 static void
2333 process_subob_fn (tree fn, special_function_kind sfk, tree *spec_p,
2334 bool *trivial_p, bool *deleted_p, bool *constexpr_p,
2335 bool diag, tree arg, bool dtor_from_ctor = false)
2337 if (!fn || fn == error_mark_node)
2339 if (deleted_p)
2340 *deleted_p = true;
2341 return;
2344 if (spec_p)
2346 if (!maybe_instantiate_noexcept (fn))
2347 *spec_p = error_mark_node;
2348 else
2350 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2351 *spec_p = merge_exception_specifiers (*spec_p, raises);
2355 if (!trivial_fn_p (fn) && !dtor_from_ctor)
2357 if (trivial_p)
2358 *trivial_p = false;
2359 if (TREE_CODE (arg) == FIELD_DECL
2360 && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
2362 if (deleted_p)
2363 *deleted_p = true;
2364 if (diag)
2365 error ("union member %q+D with non-trivial %qD", arg, fn);
2369 if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
2371 *constexpr_p = false;
2372 if (diag)
2374 inform (DECL_SOURCE_LOCATION (fn),
2375 SFK_DTOR_P (sfk)
2376 ? G_("defaulted destructor calls non-%<constexpr%> %qD")
2377 : G_("defaulted constructor calls non-%<constexpr%> %qD"),
2378 fn);
2379 explain_invalid_constexpr_fn (fn);
2384 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
2385 aggregates. If DTOR_FROM_CTOR is true, we're walking subobject destructors
2386 called from a synthesized constructor, in which case we don't consider
2387 the triviality of the subobject destructor. */
2389 static void
2390 walk_field_subobs (tree fields, special_function_kind sfk, tree fnname,
2391 int quals, tree *spec_p, bool *trivial_p,
2392 bool *deleted_p, bool *constexpr_p,
2393 bool diag, int flags, tsubst_flags_t complain,
2394 bool dtor_from_ctor)
2396 if (!fields)
2397 return;
2399 tree ctx = DECL_CONTEXT (fields);
2401 /* CWG2084: A defaulted default ctor for a union with a DMI only initializes
2402 that member, so don't check other members. */
2403 enum { unknown, no, yes }
2404 only_dmi_mem = (sfk == sfk_constructor && TREE_CODE (ctx) == UNION_TYPE
2405 ? unknown : no);
2407 again:
2408 for (tree field = fields; field; field = DECL_CHAIN (field))
2410 tree mem_type, argtype, rval;
2412 if (TREE_CODE (field) != FIELD_DECL
2413 || DECL_ARTIFICIAL (field)
2414 || DECL_UNNAMED_BIT_FIELD (field))
2415 continue;
2417 /* Variant members only affect deletedness. In particular, they don't
2418 affect the exception-specification of a user-provided destructor,
2419 which we're figuring out via get_defaulted_eh_spec. So if we aren't
2420 asking if this is deleted, don't even look up the function; we don't
2421 want an error about a deleted function we aren't actually calling. */
2422 if (sfk == sfk_destructor && deleted_p == NULL
2423 && TREE_CODE (ctx) == UNION_TYPE)
2424 break;
2426 if (only_dmi_mem != no)
2428 if (DECL_INITIAL (field))
2429 only_dmi_mem = yes;
2430 else
2431 /* Don't check this until we know there's no DMI. */
2432 continue;
2435 mem_type = strip_array_types (TREE_TYPE (field));
2436 if (SFK_ASSIGN_P (sfk))
2438 bool bad = true;
2439 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
2441 if (diag)
2442 error ("non-static const member %q#D, cannot use default "
2443 "assignment operator", field);
2445 else if (TYPE_REF_P (mem_type))
2447 if (diag)
2448 error ("non-static reference member %q#D, cannot use "
2449 "default assignment operator", field);
2451 else
2452 bad = false;
2454 if (bad && deleted_p)
2455 *deleted_p = true;
2457 else if (sfk == sfk_constructor || sfk == sfk_inheriting_constructor)
2459 bool bad;
2461 if (DECL_INITIAL (field))
2463 if (diag && DECL_INITIAL (field) == error_mark_node)
2464 inform (DECL_SOURCE_LOCATION (field),
2465 "initializer for %q#D is invalid", field);
2466 if (trivial_p)
2467 *trivial_p = false;
2468 /* Core 1351: If the field has an NSDMI that could throw, the
2469 default constructor is noexcept(false). */
2470 if (spec_p)
2472 tree nsdmi = get_nsdmi (field, /*ctor*/false, complain);
2473 if (nsdmi == error_mark_node)
2474 *spec_p = error_mark_node;
2475 else if (*spec_p != error_mark_node
2476 && !expr_noexcept_p (nsdmi, tf_none))
2477 *spec_p = noexcept_false_spec;
2479 /* Don't do the normal processing. */
2480 continue;
2483 bad = false;
2484 if (CP_TYPE_CONST_P (mem_type)
2485 && default_init_uninitialized_part (mem_type))
2487 if (diag)
2489 error ("uninitialized const member in %q#T",
2490 current_class_type);
2491 inform (DECL_SOURCE_LOCATION (field),
2492 "%q#D should be initialized", field);
2494 bad = true;
2496 else if (TYPE_REF_P (mem_type))
2498 if (diag)
2500 error ("uninitialized reference member in %q#T",
2501 current_class_type);
2502 inform (DECL_SOURCE_LOCATION (field),
2503 "%q#D should be initialized", field);
2505 bad = true;
2508 if (bad && deleted_p)
2509 *deleted_p = true;
2511 /* Before C++20, for an implicitly-defined default constructor to
2512 be constexpr, every member must have a user-provided default
2513 constructor or an explicit initializer. */
2514 if (constexpr_p
2515 && cxx_dialect < cxx20
2516 && !CLASS_TYPE_P (mem_type)
2517 && TREE_CODE (ctx) != UNION_TYPE)
2519 *constexpr_p = false;
2520 if (diag)
2521 inform (DECL_SOURCE_LOCATION (field),
2522 "defaulted default constructor does not "
2523 "initialize %q#D", field);
2526 else if (sfk == sfk_copy_constructor)
2528 /* 12.8p11b5 */
2529 if (TYPE_REF_P (mem_type)
2530 && TYPE_REF_IS_RVALUE (mem_type))
2532 if (diag)
2533 error ("copying non-static data member %q#D of rvalue "
2534 "reference type", field);
2535 if (deleted_p)
2536 *deleted_p = true;
2540 if (!CLASS_TYPE_P (mem_type))
2541 continue;
2543 if (ANON_AGGR_TYPE_P (mem_type))
2545 walk_field_subobs (TYPE_FIELDS (mem_type), sfk, fnname, quals,
2546 spec_p, trivial_p, deleted_p, constexpr_p,
2547 diag, flags, complain, dtor_from_ctor);
2548 continue;
2551 if (SFK_COPY_P (sfk) || SFK_MOVE_P (sfk))
2553 int mem_quals = cp_type_quals (mem_type) | quals;
2554 if (DECL_MUTABLE_P (field))
2555 mem_quals &= ~TYPE_QUAL_CONST;
2556 argtype = build_stub_type (mem_type, mem_quals, SFK_MOVE_P (sfk));
2558 else
2559 argtype = NULL_TREE;
2561 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
2563 process_subob_fn (rval, sfk, spec_p, trivial_p, deleted_p,
2564 constexpr_p, diag, field, dtor_from_ctor);
2567 /* We didn't find a DMI in this union, now check all the members. */
2568 if (only_dmi_mem == unknown)
2570 only_dmi_mem = no;
2571 goto again;
2575 /* Base walker helper for synthesized_method_walk. Inspect a direct
2576 or virtual base. BINFO is the parent type's binfo. BASE_BINFO is
2577 the base binfo of interests. All other parms are as for
2578 synthesized_method_walk, or its local vars. */
2580 static tree
2581 synthesized_method_base_walk (tree binfo, tree base_binfo,
2582 special_function_kind sfk, tree fnname, int quals,
2583 tree *inheriting_ctor, tree inherited_parms,
2584 int flags, bool diag,
2585 tree *spec_p, bool *trivial_p,
2586 bool *deleted_p, bool *constexpr_p)
2588 bool inherited_binfo = false;
2589 tree argtype = NULL_TREE;
2590 deferring_kind defer = dk_no_deferred;
2592 if (SFK_COPY_P (sfk) || SFK_MOVE_P (sfk))
2593 argtype = build_stub_type (BINFO_TYPE (base_binfo), quals, SFK_MOVE_P (sfk));
2594 else if (inheriting_ctor
2595 && (inherited_binfo
2596 = binfo_inherited_from (binfo, base_binfo, *inheriting_ctor)))
2598 argtype = inherited_parms;
2599 /* Don't check access on the inherited constructor. */
2600 if (flag_new_inheriting_ctors)
2601 defer = dk_deferred;
2603 else if (cxx_dialect >= cxx14 && sfk == sfk_virtual_destructor
2604 && BINFO_VIRTUAL_P (base_binfo)
2605 && ABSTRACT_CLASS_TYPE_P (BINFO_TYPE (binfo)))
2606 /* Don't check access when looking at vbases of abstract class's
2607 virtual destructor. */
2608 defer = dk_no_check;
2610 if (defer != dk_no_deferred)
2611 push_deferring_access_checks (defer);
2612 tree rval = locate_fn_flags (base_binfo, fnname, argtype, flags,
2613 diag ? tf_warning_or_error : tf_none);
2614 if (defer != dk_no_deferred)
2615 pop_deferring_access_checks ();
2617 /* Replace an inherited template with the appropriate specialization. */
2618 if (inherited_binfo && rval
2619 && DECL_P (*inheriting_ctor) && DECL_P (rval)
2620 && DECL_CONTEXT (*inheriting_ctor) == DECL_CONTEXT (rval))
2621 *inheriting_ctor = DECL_CLONED_FUNCTION (rval);
2623 process_subob_fn (rval, sfk, spec_p, trivial_p, deleted_p,
2624 constexpr_p, diag, BINFO_TYPE (base_binfo));
2625 if (SFK_CTOR_P (sfk)
2626 && (!BINFO_VIRTUAL_P (base_binfo)
2627 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))))
2629 /* In a constructor we also need to check the subobject
2630 destructors for cleanup of partially constructed objects. */
2631 tree dtor = locate_fn_flags (base_binfo, complete_dtor_identifier,
2632 NULL_TREE, flags,
2633 diag ? tf_warning_or_error : tf_none);
2634 /* Note that we don't pass down trivial_p; the subobject
2635 destructors don't affect triviality of the constructor. Nor
2636 do they affect constexpr-ness (a constant expression doesn't
2637 throw) or exception-specification (a throw from one of the
2638 dtors would be a double-fault). */
2639 process_subob_fn (dtor, sfk, NULL, NULL, deleted_p, NULL, false,
2640 BINFO_TYPE (base_binfo), /*dtor_from_ctor*/true);
2643 return rval;
2646 /* The caller wants to generate an implicit declaration of SFK for
2647 CTYPE which is const if relevant and CONST_P is set. If SPEC_P,
2648 TRIVIAL_P, DELETED_P or CONSTEXPR_P are non-null, set their
2649 referent appropriately. If DIAG is true, we're either being called
2650 from maybe_explain_implicit_delete to give errors, or if
2651 CONSTEXPR_P is non-null, from explain_invalid_constexpr_fn. */
2653 static void
2654 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
2655 tree *spec_p, bool *trivial_p, bool *deleted_p,
2656 bool *constexpr_p, bool diag,
2657 tree *inheriting_ctor, tree inherited_parms)
2659 tree binfo, base_binfo;
2660 int i;
2662 /* SFK must be exactly one category. */
2663 gcc_checking_assert (SFK_DTOR_P(sfk) + SFK_CTOR_P(sfk)
2664 + SFK_ASSIGN_P(sfk) == 1);
2666 if (spec_p)
2667 *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec);
2669 if (deleted_p)
2671 /* "The closure type associated with a lambda-expression has a deleted
2672 default constructor and a deleted copy assignment operator."
2673 This is diagnosed in maybe_explain_implicit_delete.
2674 In C++20, only lambda-expressions with lambda-captures have those
2675 deleted. */
2676 if (LAMBDA_TYPE_P (ctype)
2677 && (sfk == sfk_constructor || sfk == sfk_copy_assignment)
2678 && (cxx_dialect < cxx20
2679 || LAMBDA_EXPR_CAPTURE_LIST (CLASSTYPE_LAMBDA_EXPR (ctype))
2680 || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE
2681 (CLASSTYPE_LAMBDA_EXPR (ctype)) != CPLD_NONE))
2683 *deleted_p = true;
2684 return;
2687 *deleted_p = false;
2690 bool check_vdtor = false;
2691 tree fnname;
2693 if (SFK_DTOR_P (sfk))
2695 check_vdtor = true;
2696 /* The synthesized method will call base dtors, but check complete
2697 here to avoid having to deal with VTT. */
2698 fnname = complete_dtor_identifier;
2700 else if (SFK_ASSIGN_P (sfk))
2701 fnname = assign_op_identifier;
2702 else
2703 fnname = complete_ctor_identifier;
2705 gcc_assert ((sfk == sfk_inheriting_constructor)
2706 == (inheriting_ctor && *inheriting_ctor != NULL_TREE));
2708 /* If that user-written default constructor would satisfy the
2709 requirements of a constexpr constructor (7.1.5), the
2710 implicitly-defined default constructor is constexpr.
2712 C++20:
2713 The implicitly-defined copy/move assignment operator is constexpr if
2714 - X is a literal type, and
2715 - the assignment operator selected to copy/move each direct base class
2716 subobject is a constexpr function, and
2717 - for each non-static data member of X that is of class type (or array
2718 thereof), the assignment operator selected to copy/move that
2719 member is a constexpr function.
2721 C++23:
2722 The implicitly-defined copy/move assignment operator is constexpr. */
2723 if (constexpr_p)
2724 *constexpr_p = (SFK_CTOR_P (sfk)
2725 || (SFK_ASSIGN_P (sfk) && cxx_dialect >= cxx14)
2726 || (SFK_DTOR_P (sfk) && cxx_dialect >= cxx20));
2728 bool expected_trivial = type_has_trivial_fn (ctype, sfk);
2729 if (trivial_p)
2730 *trivial_p = expected_trivial;
2732 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
2733 class versions and other properties of the type. But a subobject
2734 class can be trivially copyable and yet have overload resolution
2735 choose a template constructor for initialization, depending on
2736 rvalueness and cv-quals. And furthermore, a member in a base might
2737 be trivial but deleted or otherwise not callable. So we can't exit
2738 early in C++0x. The same considerations apply in C++98/03, but
2739 there the definition of triviality does not consider overload
2740 resolution, so a constructor can be trivial even if it would otherwise
2741 call a non-trivial constructor. */
2742 if (expected_trivial
2743 && (!(SFK_COPY_P (sfk) || SFK_MOVE_P (sfk)) || cxx_dialect < cxx11))
2745 if (constexpr_p && sfk == sfk_constructor)
2747 bool cx = trivial_default_constructor_is_constexpr (ctype);
2748 *constexpr_p = cx;
2749 if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
2750 /* A trivial constructor doesn't have any NSDMI. */
2751 inform (input_location, "defaulted default constructor does "
2752 "not initialize any non-static data member");
2754 if (!diag && cxx_dialect < cxx11)
2755 return;
2758 ++cp_unevaluated_operand;
2759 ++c_inhibit_evaluation_warnings;
2760 push_deferring_access_checks (dk_no_deferred);
2762 tree scope = push_scope (ctype);
2764 int flags = LOOKUP_NORMAL | LOOKUP_SPECULATIVE;
2765 if (sfk != sfk_inheriting_constructor)
2766 flags |= LOOKUP_DEFAULTED;
2768 tsubst_flags_t complain = diag ? tf_warning_or_error : tf_none;
2769 if (diag && spec_p)
2770 /* We're in get_defaulted_eh_spec; we don't actually want any walking
2771 diagnostics, we just want complain set. */
2772 diag = false;
2773 int quals = const_p ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED;
2775 for (binfo = TYPE_BINFO (ctype), i = 0;
2776 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
2778 if (!SFK_ASSIGN_P (sfk) && BINFO_VIRTUAL_P (base_binfo))
2779 /* We'll handle virtual bases below. */
2780 continue;
2782 tree fn = synthesized_method_base_walk (binfo, base_binfo,
2783 sfk, fnname, quals,
2784 inheriting_ctor, inherited_parms,
2785 flags, diag, spec_p, trivial_p,
2786 deleted_p, constexpr_p);
2788 if (diag && SFK_ASSIGN_P (sfk) && SFK_MOVE_P (sfk)
2789 && BINFO_VIRTUAL_P (base_binfo)
2790 && fn && TREE_CODE (fn) == FUNCTION_DECL
2791 && move_fn_p (fn) && !trivial_fn_p (fn)
2792 && vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
2793 warning (OPT_Wvirtual_move_assign,
2794 "defaulted move assignment for %qT calls a non-trivial "
2795 "move assignment operator for virtual base %qT",
2796 ctype, BINFO_TYPE (base_binfo));
2798 if (check_vdtor && type_has_virtual_destructor (BINFO_TYPE (base_binfo)))
2800 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
2801 to have a null fn (no class-specific op delete). */
2802 fn = locate_fn_flags (ctype, ovl_op_identifier (false, DELETE_EXPR),
2803 ptr_type_node, flags, tf_none);
2804 if (fn && fn == error_mark_node)
2806 if (complain & tf_error)
2807 locate_fn_flags (ctype, ovl_op_identifier (false, DELETE_EXPR),
2808 ptr_type_node, flags, complain);
2809 if (deleted_p)
2810 *deleted_p = true;
2812 check_vdtor = false;
2816 vec<tree, va_gc> *vbases = CLASSTYPE_VBASECLASSES (ctype);
2817 if (SFK_ASSIGN_P (sfk))
2818 /* Already examined vbases above. */;
2819 else if (vec_safe_is_empty (vbases))
2820 /* No virtual bases to worry about. */;
2821 else if (ABSTRACT_CLASS_TYPE_P (ctype) && cxx_dialect >= cxx14
2822 /* DR 1658 specifies that vbases of abstract classes are
2823 ignored for both ctors and dtors. Except DR 2336
2824 overrides that skipping when determing the eh-spec of a
2825 virtual destructor. */
2826 && sfk != sfk_virtual_destructor)
2827 /* Vbase cdtors are not relevant. */;
2828 else
2830 if (constexpr_p)
2831 *constexpr_p = false;
2833 FOR_EACH_VEC_ELT (*vbases, i, base_binfo)
2834 synthesized_method_base_walk (binfo, base_binfo, sfk, fnname, quals,
2835 inheriting_ctor, inherited_parms,
2836 flags, diag,
2837 spec_p, trivial_p, deleted_p, constexpr_p);
2840 /* Now handle the non-static data members. */
2841 walk_field_subobs (TYPE_FIELDS (ctype), sfk, fnname, quals,
2842 spec_p, trivial_p, deleted_p, constexpr_p,
2843 diag, flags, complain, /*dtor_from_ctor*/false);
2844 if (SFK_CTOR_P (sfk))
2845 walk_field_subobs (TYPE_FIELDS (ctype), sfk_destructor,
2846 complete_dtor_identifier, TYPE_UNQUALIFIED,
2847 NULL, NULL, deleted_p, NULL,
2848 false, flags, complain, /*dtor_from_ctor*/true);
2850 pop_scope (scope);
2852 pop_deferring_access_checks ();
2853 --cp_unevaluated_operand;
2854 --c_inhibit_evaluation_warnings;
2857 /* DECL is a defaulted function whose exception specification is now
2858 needed. Return what it should be. */
2860 tree
2861 get_defaulted_eh_spec (tree decl, tsubst_flags_t complain)
2863 /* For DECL_MAYBE_DELETED this should already have been handled by
2864 synthesize_method. */
2865 gcc_assert (!DECL_MAYBE_DELETED (decl));
2867 if (DECL_CLONED_FUNCTION_P (decl))
2868 decl = DECL_CLONED_FUNCTION (decl);
2869 special_function_kind sfk = special_function_p (decl);
2870 tree ctype = DECL_CONTEXT (decl);
2871 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
2872 tree parm_type = TREE_VALUE (parms);
2873 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
2874 tree spec = empty_except_spec;
2875 bool diag = !DECL_DELETED_FN (decl) && (complain & tf_error);
2876 tree inh = DECL_INHERITED_CTOR (decl);
2877 if (SFK_DTOR_P (sfk) && DECL_VIRTUAL_P (decl))
2878 /* We have to examine virtual bases even if abstract. */
2879 sfk = sfk_virtual_destructor;
2880 bool pushed = false;
2881 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2882 pushed = push_tinst_level (decl);
2883 synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL,
2884 NULL, diag, &inh, parms);
2885 if (pushed)
2886 pop_tinst_level ();
2887 return spec;
2890 /* DECL is a deleted function. If it's implicitly deleted, explain why and
2891 return true; else return false. */
2893 bool
2894 maybe_explain_implicit_delete (tree decl)
2896 /* If decl is a clone, get the primary variant. */
2897 decl = DECL_ORIGIN (decl);
2898 gcc_assert (DECL_DELETED_FN (decl));
2899 if (DECL_DEFAULTED_FN (decl))
2901 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
2902 static hash_set<tree> *explained;
2904 special_function_kind sfk;
2905 location_t loc;
2906 bool informed;
2907 tree ctype;
2909 if (!explained)
2910 explained = new hash_set<tree>;
2911 if (explained->add (decl))
2912 return true;
2914 sfk = special_function_p (decl);
2915 ctype = DECL_CONTEXT (decl);
2916 loc = input_location;
2917 input_location = DECL_SOURCE_LOCATION (decl);
2919 informed = false;
2920 if (LAMBDA_TYPE_P (ctype))
2922 informed = true;
2923 if (sfk == sfk_constructor)
2924 inform (DECL_SOURCE_LOCATION (decl),
2925 "a lambda closure type has a deleted default constructor");
2926 else if (sfk == sfk_copy_assignment)
2927 inform (DECL_SOURCE_LOCATION (decl),
2928 "a lambda closure type has a deleted copy assignment operator");
2929 else
2930 informed = false;
2932 else if (DECL_ARTIFICIAL (decl)
2933 && (sfk == sfk_copy_assignment || sfk == sfk_copy_constructor)
2934 && classtype_has_move_assign_or_move_ctor_p (ctype, true))
2936 inform (DECL_SOURCE_LOCATION (decl),
2937 "%q#D is implicitly declared as deleted because %qT "
2938 "declares a move constructor or move assignment operator",
2939 decl, ctype);
2940 informed = true;
2942 else if (sfk == sfk_inheriting_constructor)
2944 tree binfo = inherited_ctor_binfo (decl);
2945 if (TREE_CODE (binfo) != TREE_BINFO)
2947 inform (DECL_SOURCE_LOCATION (decl),
2948 "%q#D inherits from multiple base subobjects",
2949 decl);
2950 informed = true;
2953 if (!informed && sfk == sfk_comparison)
2955 inform (DECL_SOURCE_LOCATION (decl),
2956 "%q#D is implicitly deleted because the default "
2957 "definition would be ill-formed:", decl);
2958 build_comparison_op (decl, false, tf_warning_or_error);
2960 else if (!informed)
2962 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
2963 bool const_p = false;
2964 if (parms)
2966 tree parm_type = TREE_VALUE (parms);
2967 const_p = CP_TYPE_CONST_P (non_reference (parm_type));
2969 tree raises = NULL_TREE;
2970 bool deleted_p = false;
2971 tree scope = push_scope (ctype);
2972 tree inh = DECL_INHERITED_CTOR (decl);
2974 synthesized_method_walk (ctype, sfk, const_p,
2975 &raises, NULL, &deleted_p, NULL, false,
2976 &inh, parms);
2977 if (deleted_p)
2979 inform (DECL_SOURCE_LOCATION (decl),
2980 "%q#D is implicitly deleted because the default "
2981 "definition would be ill-formed:", decl);
2982 synthesized_method_walk (ctype, sfk, const_p,
2983 NULL, NULL, &deleted_p, NULL, true,
2984 &inh, parms);
2986 else if (!comp_except_specs
2987 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
2988 raises, ce_normal))
2989 inform (DECL_SOURCE_LOCATION (decl), "%q#F is implicitly "
2990 "deleted because its exception-specification does not "
2991 "match the implicit exception-specification %qX",
2992 decl, raises);
2993 else if (flag_checking)
2994 gcc_unreachable ();
2996 pop_scope (scope);
2999 input_location = loc;
3000 return true;
3002 return false;
3005 /* DECL is a defaulted function which was declared constexpr. Explain why
3006 it can't be constexpr. */
3008 void
3009 explain_implicit_non_constexpr (tree decl)
3011 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
3012 bool const_p = CP_TYPE_CONST_P (non_reference (TREE_VALUE (parms)));
3013 tree inh = DECL_INHERITED_CTOR (decl);
3014 bool dummy;
3015 special_function_kind sfk = special_function_p (decl);
3016 if (sfk == sfk_comparison)
3018 DECL_DECLARED_CONSTEXPR_P (decl) = true;
3019 build_comparison_op (decl, false, tf_warning_or_error);
3020 DECL_DECLARED_CONSTEXPR_P (decl) = false;
3022 else
3023 synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
3024 sfk, const_p,
3025 NULL, NULL, NULL, &dummy, true,
3026 &inh, parms);
3029 /* DECL is an instantiation of an inheriting constructor template. Deduce
3030 the correct exception-specification and deletedness for this particular
3031 specialization. Return true if the deduction succeeds; false otherwise. */
3033 bool
3034 deduce_inheriting_ctor (tree decl)
3036 decl = DECL_ORIGIN (decl);
3037 gcc_assert (DECL_INHERITED_CTOR (decl));
3038 tree spec;
3039 bool trivial, constexpr_, deleted;
3040 tree inh = DECL_INHERITED_CTOR (decl);
3041 synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor,
3042 false, &spec, &trivial, &deleted, &constexpr_,
3043 /*diag*/false,
3044 &inh,
3045 FUNCTION_FIRST_USER_PARMTYPE (decl));
3046 if (spec == error_mark_node)
3047 return false;
3048 if (TREE_CODE (inherited_ctor_binfo (decl)) != TREE_BINFO)
3049 /* Inherited the same constructor from different base subobjects. */
3050 deleted = true;
3051 DECL_DELETED_FN (decl) = deleted;
3052 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
3053 SET_DECL_INHERITED_CTOR (decl, inh);
3055 tree clone;
3056 FOR_EACH_CLONE (clone, decl)
3058 DECL_DELETED_FN (clone) = deleted;
3059 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
3060 SET_DECL_INHERITED_CTOR (clone, inh);
3063 return true;
3066 /* Implicitly declare the special function indicated by KIND, as a
3067 member of TYPE. For copy constructors and assignment operators,
3068 CONST_P indicates whether these functions should take a const
3069 reference argument or a non-const reference.
3070 Returns the FUNCTION_DECL for the implicitly declared function. */
3072 tree
3073 implicitly_declare_fn (special_function_kind kind, tree type,
3074 bool const_p, tree pattern_fn,
3075 tree inherited_parms)
3077 tree fn;
3078 tree parameter_types = void_list_node;
3079 tree return_type;
3080 tree fn_type;
3081 tree raises = empty_except_spec;
3082 tree rhs_parm_type = NULL_TREE;
3083 tree this_parm;
3084 tree name;
3085 HOST_WIDE_INT saved_processing_template_decl;
3086 bool deleted_p = false;
3087 bool constexpr_p = false;
3088 tree inherited_ctor = (kind == sfk_inheriting_constructor
3089 ? pattern_fn : NULL_TREE);
3091 /* Because we create declarations for implicitly declared functions
3092 lazily, we may be creating the declaration for a member of TYPE
3093 while in some completely different context. However, TYPE will
3094 never be a dependent class (because we never want to do lookups
3095 for implicitly defined functions in a dependent class). */
3096 gcc_assert (!dependent_type_p (type));
3098 /* If the member-specification does not explicitly declare any member or
3099 friend named operator==, an == operator function is declared
3100 implicitly for each three-way comparison operator function defined as
3101 defaulted in the member-specification, with the same access and
3102 function-definition and in the same class scope as the respective
3103 three-way comparison operator function, except that the return type is
3104 replaced with bool and the declarator-id is replaced with
3105 operator==.
3107 [Note: Such an implicitly-declared == operator for a class X is
3108 defined as defaulted in the definition of X and has the same
3109 parameter-declaration-clause and trailing requires-clause as the
3110 respective three-way comparison operator. It is declared with friend,
3111 virtual, constexpr, or consteval if the three-way comparison operator
3112 function is so declared. If the three-way comparison operator function
3113 has no noexcept-specifier, the implicitly-declared == operator
3114 function has an implicit exception specification (14.5) that may
3115 differ from the implicit exception specification of the three-way
3116 comparison operator function. --end note] */
3117 if (kind == sfk_comparison)
3119 fn = copy_operator_fn (pattern_fn, EQ_EXPR);
3120 DECL_ARTIFICIAL (fn) = 1;
3121 apply_deduced_return_type (fn, boolean_type_node);
3122 return fn;
3125 /* Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
3126 because we only create clones for constructors and destructors
3127 when not in a template. */
3128 saved_processing_template_decl = processing_template_decl;
3129 processing_template_decl = 0;
3131 type = TYPE_MAIN_VARIANT (type);
3133 if (targetm.cxx.cdtor_returns_this ())
3135 if (kind == sfk_destructor)
3136 /* See comment in check_special_function_return_type. */
3137 return_type = build_pointer_type (void_type_node);
3138 else
3139 return_type = build_pointer_type (type);
3141 else
3142 return_type = void_type_node;
3144 int this_quals = TYPE_UNQUALIFIED;
3145 switch (kind)
3147 case sfk_destructor:
3148 /* Destructor. */
3149 name = dtor_identifier;
3150 break;
3152 case sfk_constructor:
3153 /* Default constructor. */
3154 name = ctor_identifier;
3155 break;
3157 case sfk_copy_constructor:
3158 case sfk_copy_assignment:
3159 case sfk_move_constructor:
3160 case sfk_move_assignment:
3161 case sfk_inheriting_constructor:
3163 if (kind == sfk_copy_assignment
3164 || kind == sfk_move_assignment)
3166 return_type = build_reference_type (type);
3167 name = assign_op_identifier;
3169 else
3170 name = ctor_identifier;
3172 if (kind == sfk_inheriting_constructor)
3173 parameter_types = inherited_parms;
3174 else
3176 if (const_p)
3177 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
3178 else
3179 rhs_parm_type = type;
3180 bool move_p = (kind == sfk_move_assignment
3181 || kind == sfk_move_constructor);
3182 rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
3184 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
3186 break;
3189 default:
3190 gcc_unreachable ();
3193 bool trivial_p = false;
3195 if (inherited_ctor)
3197 /* For an inheriting constructor, just copy these flags from the
3198 inherited constructor until deduce_inheriting_ctor. */
3199 raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor));
3200 deleted_p = DECL_DELETED_FN (inherited_ctor);
3201 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
3203 else if (cxx_dialect >= cxx11)
3205 raises = noexcept_deferred_spec;
3206 synthesized_method_walk (type, kind, const_p, NULL, &trivial_p,
3207 &deleted_p, &constexpr_p, false,
3208 &inherited_ctor, inherited_parms);
3210 else
3211 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
3212 &deleted_p, &constexpr_p, false,
3213 &inherited_ctor, inherited_parms);
3214 /* Don't bother marking a deleted constructor as constexpr. */
3215 if (deleted_p)
3216 constexpr_p = false;
3217 /* A trivial copy/move constructor is also a constexpr constructor,
3218 unless the class has virtual bases (7.1.5p4). */
3219 else if (trivial_p
3220 && cxx_dialect >= cxx11
3221 && (kind == sfk_copy_constructor
3222 || kind == sfk_move_constructor)
3223 && !CLASSTYPE_VBASECLASSES (type))
3224 gcc_assert (constexpr_p);
3226 if (!trivial_p && type_has_trivial_fn (type, kind))
3227 type_set_nontrivial_flag (type, kind);
3229 /* Create the function. */
3230 tree this_type = cp_build_qualified_type (type, this_quals);
3231 fn_type = build_method_type_directly (this_type, return_type,
3232 parameter_types);
3234 if (raises)
3236 if (raises != error_mark_node)
3237 fn_type = build_exception_variant (fn_type, raises);
3238 else
3240 /* Can happen, e.g., in C++98 mode for an ill-formed non-static data
3241 member initializer (c++/89914). Also, in C++98, we might have
3242 failed to deduce RAISES, so try again but complain this time. */
3243 if (cxx_dialect < cxx11)
3244 synthesized_method_walk (type, kind, const_p, &raises, nullptr,
3245 nullptr, nullptr, /*diag=*/true,
3246 &inherited_ctor, inherited_parms);
3247 /* We should have seen an error at this point. */
3248 gcc_assert (seen_error ());
3251 fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
3252 if (kind != sfk_inheriting_constructor)
3253 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
3255 if (IDENTIFIER_OVL_OP_P (name))
3257 const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (name);
3258 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) = op->ovl_op_code;
3260 else if (IDENTIFIER_CTOR_P (name))
3261 DECL_CXX_CONSTRUCTOR_P (fn) = true;
3262 else if (IDENTIFIER_DTOR_P (name))
3263 DECL_CXX_DESTRUCTOR_P (fn) = true;
3264 else
3265 gcc_unreachable ();
3267 SET_DECL_ALIGN (fn, MINIMUM_METHOD_BOUNDARY);
3269 /* Create the explicit arguments. */
3270 if (rhs_parm_type)
3272 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
3273 want its type to be included in the mangled function
3274 name. */
3275 tree decl = cp_build_parm_decl (fn, NULL_TREE, rhs_parm_type);
3276 TREE_READONLY (decl) = 1;
3277 retrofit_lang_decl (decl);
3278 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
3279 DECL_ARGUMENTS (fn) = decl;
3281 else if (kind == sfk_inheriting_constructor)
3283 tree *p = &DECL_ARGUMENTS (fn);
3284 int index = 1;
3285 for (tree parm = inherited_parms; parm && parm != void_list_node;
3286 parm = TREE_CHAIN (parm))
3288 *p = cp_build_parm_decl (fn, NULL_TREE, TREE_VALUE (parm));
3289 retrofit_lang_decl (*p);
3290 DECL_PARM_LEVEL (*p) = 1;
3291 DECL_PARM_INDEX (*p) = index++;
3292 p = &DECL_CHAIN (*p);
3294 SET_DECL_INHERITED_CTOR (fn, inherited_ctor);
3295 DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor);
3296 /* A constructor so declared has the same access as the corresponding
3297 constructor in X. */
3298 TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor);
3299 TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor);
3300 /* Copy constexpr from the inherited constructor even if the
3301 inheriting constructor doesn't satisfy the requirements. */
3302 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
3303 /* Also copy any attributes. */
3304 DECL_ATTRIBUTES (fn) = clone_attrs (DECL_ATTRIBUTES (inherited_ctor));
3307 /* Add the "this" parameter. */
3308 this_parm = build_this_parm (fn, fn_type, this_quals);
3309 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
3310 DECL_ARGUMENTS (fn) = this_parm;
3312 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
3314 DECL_IN_AGGR_P (fn) = 1;
3315 DECL_ARTIFICIAL (fn) = 1;
3316 DECL_DEFAULTED_FN (fn) = 1;
3317 if (cxx_dialect >= cxx11)
3319 DECL_DELETED_FN (fn) = deleted_p;
3320 DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
3322 DECL_EXTERNAL (fn) = true;
3323 DECL_NOT_REALLY_EXTERN (fn) = 1;
3324 DECL_DECLARED_INLINE_P (fn) = 1;
3325 set_linkage_according_to_type (type, fn);
3326 if (TREE_PUBLIC (fn))
3327 DECL_COMDAT (fn) = 1;
3328 rest_of_decl_compilation (fn, namespace_bindings_p (), at_eof);
3329 gcc_assert (!TREE_USED (fn));
3331 /* Propagate constraints from the inherited constructor. */
3332 if (flag_concepts && inherited_ctor)
3333 if (tree orig_ci = get_constraints (inherited_ctor))
3335 tree new_ci = copy_node (orig_ci);
3336 set_constraints (fn, new_ci);
3339 /* Restore PROCESSING_TEMPLATE_DECL. */
3340 processing_template_decl = saved_processing_template_decl;
3342 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
3343 fn = add_inherited_template_parms (fn, inherited_ctor);
3345 /* Warn about calling a non-trivial move assignment in a virtual base. */
3346 if (kind == sfk_move_assignment && !deleted_p && !trivial_p
3347 && CLASSTYPE_VBASECLASSES (type))
3349 location_t loc = input_location;
3350 input_location = DECL_SOURCE_LOCATION (fn);
3351 synthesized_method_walk (type, kind, const_p,
3352 NULL, NULL, NULL, NULL, true,
3353 NULL, NULL_TREE);
3354 input_location = loc;
3357 return fn;
3360 /* Gives any errors about defaulted functions which need to be deferred
3361 until the containing class is complete. */
3363 void
3364 defaulted_late_check (tree fn)
3366 /* Complain about invalid signature for defaulted fn. */
3367 tree ctx = DECL_CONTEXT (fn);
3368 special_function_kind kind = special_function_p (fn);
3370 if (kind == sfk_comparison)
3372 /* If the function was declared constexpr, check that the definition
3373 qualifies. Otherwise we can define the function lazily. */
3374 if (DECL_DECLARED_CONSTEXPR_P (fn) && !DECL_INITIAL (fn))
3376 /* Prevent GC. */
3377 function_depth++;
3378 synthesize_method (fn);
3379 function_depth--;
3381 return;
3384 bool fn_const_p = (copy_fn_p (fn) == 2);
3385 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
3386 NULL, NULL);
3387 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
3389 /* Includes special handling for a default xobj operator. */
3390 auto compare_fn_params = [](tree fn, tree implicit_fn){
3391 tree fn_parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
3392 tree implicit_fn_parms = TYPE_ARG_TYPES (TREE_TYPE (implicit_fn));
3394 if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
3396 tree fn_obj_ref_type = TREE_VALUE (fn_parms);
3397 /* We can't default xobj operators with an xobj parameter that is not
3398 an lvalue reference. */
3399 if (!TYPE_REF_P (fn_obj_ref_type)
3400 || TYPE_REF_IS_RVALUE (fn_obj_ref_type))
3401 return false;
3402 /* If implicit_fn's object parameter is not a pointer, something is not
3403 right. */
3404 gcc_assert (TYPE_PTR_P (TREE_VALUE (implicit_fn_parms)));
3405 /* Strip the reference/pointer off each object parameter before
3406 comparing them. */
3407 if (!same_type_p (TREE_TYPE (fn_obj_ref_type),
3408 TREE_TYPE (TREE_VALUE (implicit_fn_parms))))
3409 return false;
3410 /* We just compared the object parameters, skip over them before
3411 passing to compparms. */
3412 fn_parms = TREE_CHAIN (fn_parms);
3413 implicit_fn_parms = TREE_CHAIN (implicit_fn_parms);
3415 return compparms(fn_parms, implicit_fn_parms);
3418 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
3419 TREE_TYPE (TREE_TYPE (implicit_fn)))
3420 || !compare_fn_params (fn, implicit_fn))
3422 error ("defaulted declaration %q+D does not match the "
3423 "expected signature", fn);
3424 inform (DECL_SOURCE_LOCATION (fn),
3425 "expected signature: %qD", implicit_fn);
3428 if (DECL_DELETED_FN (implicit_fn))
3430 DECL_DELETED_FN (fn) = 1;
3431 return;
3434 /* If a function is explicitly defaulted on its first declaration without an
3435 exception-specification, it is implicitly considered to have the same
3436 exception-specification as if it had been implicitly declared. */
3437 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn))
3438 && DECL_DEFAULTED_IN_CLASS_P (fn))
3439 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
3441 if (DECL_DEFAULTED_IN_CLASS_P (fn)
3442 && DECL_DECLARED_CONSTEXPR_P (implicit_fn))
3444 /* Hmm...should we do this for out-of-class too? Should it be OK to
3445 add constexpr later like inline, rather than requiring
3446 declarations to match? */
3447 DECL_DECLARED_CONSTEXPR_P (fn) = true;
3448 if (kind == sfk_constructor)
3449 TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
3452 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
3453 && DECL_DECLARED_CONSTEXPR_P (fn))
3455 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
3457 error ("explicitly defaulted function %q+D cannot be declared "
3458 "%qs because the implicit declaration is not %qs:", fn,
3459 DECL_IMMEDIATE_FUNCTION_P (fn) ? "consteval" : "constexpr",
3460 "constexpr");
3461 explain_implicit_non_constexpr (fn);
3463 DECL_DECLARED_CONSTEXPR_P (fn) = false;
3467 /* Returns true iff FN can be explicitly defaulted, and gives any
3468 errors if defaulting FN is ill-formed. */
3470 bool
3471 defaultable_fn_check (tree fn)
3473 special_function_kind kind = sfk_none;
3475 if (template_parm_scope_p ())
3477 error ("a template cannot be defaulted");
3478 return false;
3481 if (DECL_CONSTRUCTOR_P (fn))
3483 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
3484 kind = sfk_constructor;
3485 else if (copy_fn_p (fn) > 0
3486 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
3487 == void_list_node))
3488 kind = sfk_copy_constructor;
3489 else if (move_fn_p (fn))
3490 kind = sfk_move_constructor;
3492 else if (DECL_DESTRUCTOR_P (fn))
3493 kind = sfk_destructor;
3494 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
3495 && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR))
3497 if (copy_fn_p (fn))
3498 kind = sfk_copy_assignment;
3499 else if (move_fn_p (fn))
3500 kind = sfk_move_assignment;
3502 else if (DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) >= OVL_OP_EQ_EXPR
3503 && DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) <= OVL_OP_SPACESHIP_EXPR)
3505 kind = sfk_comparison;
3506 if (!early_check_defaulted_comparison (fn))
3507 return false;
3510 /* FIXME: We need to check for xobj member functions here to give better
3511 diagnostics for weird cases where unrelated xobj parameters are given.
3512 We just want to do better than 'cannot be defaulted'. */
3514 if (kind == sfk_none)
3516 error ("%qD cannot be defaulted", fn);
3517 return false;
3519 else
3521 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
3522 t && t != void_list_node; t = TREE_CHAIN (t))
3523 if (TREE_PURPOSE (t))
3525 error ("defaulted function %q+D with default argument", fn);
3526 break;
3529 /* Avoid do_warn_unused_parameter warnings. */
3530 for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p))
3531 if (DECL_NAME (p))
3532 suppress_warning (p, OPT_Wunused_parameter);
3534 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
3535 /* Defer checking. */;
3536 else if (!processing_template_decl)
3537 defaulted_late_check (fn);
3539 return true;
3543 /* Add an implicit declaration to TYPE for the kind of function
3544 indicated by SFK. Return the FUNCTION_DECL for the new implicit
3545 declaration. */
3547 tree
3548 lazily_declare_fn (special_function_kind sfk, tree type)
3550 tree fn;
3551 /* Whether or not the argument has a const reference type. */
3552 bool const_p = false;
3554 type = TYPE_MAIN_VARIANT (type);
3556 switch (sfk)
3558 case sfk_constructor:
3559 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
3560 break;
3561 case sfk_copy_constructor:
3562 const_p = TYPE_HAS_CONST_COPY_CTOR (type);
3563 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
3564 break;
3565 case sfk_move_constructor:
3566 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
3567 break;
3568 case sfk_copy_assignment:
3569 const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
3570 CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
3571 break;
3572 case sfk_move_assignment:
3573 CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
3574 break;
3575 case sfk_destructor:
3576 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
3577 break;
3578 default:
3579 gcc_unreachable ();
3582 /* Declare the function. */
3583 fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL);
3585 /* [class.copy]/8 If the class definition declares a move constructor or
3586 move assignment operator, the implicitly declared copy constructor is
3587 defined as deleted.... */
3588 if ((sfk == sfk_copy_assignment || sfk == sfk_copy_constructor)
3589 && cxx_dialect >= cxx11)
3591 if (classtype_has_move_assign_or_move_ctor_p (type, true))
3592 DECL_DELETED_FN (fn) = true;
3593 else if (classtype_has_depr_implicit_copy (type))
3594 /* The implicit definition of a copy constructor as defaulted is
3595 deprecated if the class has a user-declared copy assignment operator
3596 or a user-declared destructor. The implicit definition of a copy
3597 assignment operator as defaulted is deprecated if the class has a
3598 user-declared copy constructor or a user-declared destructor (15.4,
3599 15.8). */
3600 TREE_DEPRECATED (fn) = true;
3603 /* Destructors and assignment operators may be virtual. */
3604 if (sfk == sfk_destructor
3605 || sfk == sfk_move_assignment
3606 || sfk == sfk_copy_assignment)
3607 check_for_override (fn, type);
3609 /* Add it to the class */
3610 bool added = add_method (type, fn, false);
3611 gcc_assert (added || errorcount);
3613 /* Add it to TYPE_FIELDS. */
3614 if (sfk == sfk_destructor
3615 && DECL_VIRTUAL_P (fn))
3616 /* The ABI requires that a virtual destructor go at the end of the
3617 vtable. */
3618 TYPE_FIELDS (type) = chainon (TYPE_FIELDS (type), fn);
3619 else
3621 DECL_CHAIN (fn) = TYPE_FIELDS (type);
3622 TYPE_FIELDS (type) = fn;
3624 /* Propagate TYPE_FIELDS. */
3625 fixup_type_variants (type);
3627 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
3628 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (fn))
3629 /* Create appropriate clones. */
3630 clone_cdtor (fn, /*update_methods=*/true);
3632 /* Classes, structs or unions TYPE marked with hotness attributes propagate
3633 the attribute to all methods. This is typically done in
3634 check_bases_and_members, but we must also inject them here for deferred
3635 lazily-declared functions. */
3636 maybe_propagate_warmth_attributes (fn, type);
3638 return fn;
3641 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
3642 as there are artificial parms in FN. */
3644 tree
3645 skip_artificial_parms_for (const_tree fn, tree list)
3647 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
3648 list = TREE_CHAIN (list);
3649 else
3650 return list;
3652 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
3653 list = TREE_CHAIN (list);
3654 if (DECL_HAS_VTT_PARM_P (fn))
3655 list = TREE_CHAIN (list);
3656 return list;
3659 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
3660 artificial parms in FN. */
3663 num_artificial_parms_for (const_tree fn)
3665 int count = 0;
3667 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
3668 count++;
3669 else
3670 return 0;
3672 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
3673 count++;
3674 if (DECL_HAS_VTT_PARM_P (fn))
3675 count++;
3676 return count;
3680 #include "gt-cp-method.h"