Fortran: Fix regression caused by r14-10477 [PR59104]
[official-gcc.git] / gcc / cp / method.cc
blob0b21656ed612ebca5fd32f140783645c7da69824
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))
1232 auto_diagnostic_group d;
1233 error_at (loc, "defaulted %qD is not a friend of %qT", fn, ctx);
1234 inform (location_of (ctx), "declared here");
1235 ok = false;
1238 else if (!same_type_ignoring_top_level_qualifiers_p (parmtype, ctx))
1239 saw_bad = true;
1242 if (saw_bad || (saw_byval && saw_byref))
1244 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
1245 error_at (loc, "defaulted member %qD must have parameter type "
1246 "%<const %T&%>", fn, ctx);
1247 else if (saw_bad)
1248 error_at (loc, "defaulted %qD must have parameters of either type "
1249 "%<const %T&%> or %qT", fn, ctx, ctx);
1250 else
1251 error_at (loc, "defaulted %qD must have parameters of either type "
1252 "%<const %T&%> or %qT, not both", fn, ctx, ctx);
1253 ok = false;
1256 /* We still need to deduce deleted/constexpr/noexcept and maybe return. */
1257 DECL_MAYBE_DELETED (fn) = ok;
1259 return ok;
1262 /* Subroutine of build_comparison_op. Given the vec of memberwise
1263 comparisons COMPS, calculate the overall comparison category for
1264 operator<=>. */
1266 static tree
1267 common_comparison_type (vec<tree> &comps)
1269 tree seen[cc_last] = {};
1271 for (unsigned i = 0; i < comps.length(); ++i)
1273 tree comp = comps[i];
1274 if (TREE_CODE (comp) == TREE_LIST)
1275 comp = TREE_VALUE (comp);
1276 tree ctype = TREE_TYPE (comp);
1277 comp_cat_tag tag = cat_tag_for (ctype);
1278 /* build_comparison_op already checked this. */
1279 gcc_checking_assert (tag < cc_last);
1280 seen[tag] = ctype;
1283 /* Otherwise, if at least one T i is std::partial_ordering, U is
1284 std::partial_ordering. */
1285 if (tree t = seen[cc_partial_ordering]) return t;
1287 /* Otherwise, if at least one T i is std::weak_ordering, U is
1288 std::weak_ordering. */
1289 if (tree t = seen[cc_weak_ordering]) return t;
1291 /* Otherwise, U is std::strong_ordering. */
1292 if (tree t = seen[cc_strong_ordering]) return t;
1293 return lookup_comparison_category (cc_strong_ordering);
1296 /* Data structure for build_comparison_op. */
1298 struct comp_info
1300 tree fndecl;
1301 location_t loc;
1302 tsubst_flags_t complain;
1303 tree_code code;
1304 comp_cat_tag retcat;
1305 bool first_time;
1306 bool constexp;
1307 bool was_constexp;
1308 bool noex;
1310 comp_info (tree fndecl, tsubst_flags_t complain)
1311 : fndecl (fndecl), complain (complain)
1313 loc = DECL_SOURCE_LOCATION (fndecl);
1315 first_time = DECL_MAYBE_DELETED (fndecl);
1316 DECL_MAYBE_DELETED (fndecl) = false;
1318 /* Do we want to try to set constexpr? */
1319 was_constexp = DECL_DECLARED_CONSTEXPR_P (fndecl);
1320 constexp = first_time;
1321 if (constexp)
1322 /* Set this for var_in_constexpr_fn. */
1323 DECL_DECLARED_CONSTEXPR_P (fndecl) = true;
1325 /* Do we want to try to set noexcept? */
1326 noex = first_time;
1327 if (noex)
1329 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fndecl));
1330 if (raises && !UNEVALUATED_NOEXCEPT_SPEC_P (raises))
1331 /* There was an explicit exception-specification. */
1332 noex = false;
1336 /* EXPR is an expression built as part of the function body.
1337 Adjust the properties appropriately. */
1338 void check (tree expr)
1340 if (expr == error_mark_node)
1341 DECL_DELETED_FN (fndecl) = true;
1342 if ((constexp || was_constexp)
1343 && !potential_rvalue_constant_expression (expr))
1345 if (was_constexp)
1346 require_potential_rvalue_constant_expression_fncheck (expr);
1347 else
1348 constexp = false;
1350 if (noex && !expr_noexcept_p (expr, tf_none))
1351 noex = false;
1354 ~comp_info ()
1356 if (first_time)
1358 DECL_DECLARED_CONSTEXPR_P (fndecl) = constexp || was_constexp;
1359 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fndecl));
1360 if (!raises || UNEVALUATED_NOEXCEPT_SPEC_P (raises))
1362 raises = noex ? noexcept_true_spec : noexcept_false_spec;
1363 TREE_TYPE (fndecl) = build_exception_variant (TREE_TYPE (fndecl),
1364 raises);
1370 /* Subroutine of build_comparison_op, to compare a single subobject. */
1372 static tree
1373 do_one_comp (location_t loc, const comp_info &info, tree sub, tree lhs, tree rhs)
1375 const tree_code code = info.code;
1376 const tree fndecl = info.fndecl;
1377 const comp_cat_tag retcat = info.retcat;
1378 const tsubst_flags_t complain = info.complain;
1380 tree overload = NULL_TREE;
1381 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
1382 /* If we have an explicit comparison category return type we can fall back
1383 to </=, so don't give an error yet if <=> lookup fails. */
1384 bool tentative = retcat != cc_last;
1385 tree comp = build_new_op (loc, code, flags, lhs, rhs,
1386 NULL_TREE, NULL_TREE, &overload,
1387 tentative ? tf_none : complain);
1389 if (code != SPACESHIP_EXPR)
1390 return comp;
1392 tree rettype = TREE_TYPE (TREE_TYPE (fndecl));
1394 if (comp == error_mark_node)
1396 if (overload == NULL_TREE && (tentative || complain))
1398 /* No viable <=>, try using op< and op==. */
1399 tree lteq = genericize_spaceship (loc, rettype, lhs, rhs);
1400 if (lteq != error_mark_node)
1402 /* We found usable < and ==. */
1403 if (retcat != cc_last)
1404 /* Return type is a comparison category, use them. */
1405 comp = lteq;
1406 else if (complain & tf_error)
1407 /* Return type is auto, suggest changing it. */
1408 inform (info.loc, "changing the return type from %qs "
1409 "to a comparison category type will allow the "
1410 "comparison to use %qs and %qs", "auto",
1411 "operator<", "operator==");
1413 else if (tentative && complain)
1414 /* No usable < and ==, give an error for op<=>. */
1415 build_new_op (loc, code, flags, lhs, rhs, complain);
1417 if (comp == error_mark_node)
1418 return error_mark_node;
1421 if (FNDECL_USED_AUTO (fndecl)
1422 && cat_tag_for (TREE_TYPE (comp)) == cc_last)
1424 /* The operator function is defined as deleted if ... Ri is not a
1425 comparison category type. */
1426 if (complain & tf_error)
1427 inform (loc,
1428 "three-way comparison of %qD has type %qT, not a "
1429 "comparison category type", sub, TREE_TYPE (comp));
1430 return error_mark_node;
1432 else if (!FNDECL_USED_AUTO (fndecl)
1433 && !can_convert (rettype, TREE_TYPE (comp), complain))
1435 if (complain & tf_error)
1436 error_at (loc,
1437 "three-way comparison of %qD has type %qT, which "
1438 "does not convert to %qT",
1439 sub, TREE_TYPE (comp), rettype);
1440 return error_mark_node;
1443 return comp;
1446 /* Build up the definition of a defaulted comparison operator. Unlike other
1447 defaulted functions that use synthesized_method_walk to determine whether
1448 the function is e.g. deleted, for comparisons we use the same code. We try
1449 to use synthesize_method at the earliest opportunity and bail out if the
1450 function ends up being deleted. */
1452 void
1453 build_comparison_op (tree fndecl, bool defining, tsubst_flags_t complain)
1455 comp_info info (fndecl, complain);
1457 if (!defining && !(complain & tf_error) && !DECL_MAYBE_DELETED (fndecl))
1458 return;
1460 int flags = LOOKUP_NORMAL;
1461 const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (DECL_NAME (fndecl));
1462 tree_code code = info.code = op->tree_code;
1464 tree lhs = DECL_ARGUMENTS (fndecl);
1465 tree rhs = DECL_CHAIN (lhs);
1466 if (is_this_parameter (lhs))
1467 lhs = cp_build_fold_indirect_ref (lhs);
1468 else
1469 lhs = convert_from_reference (lhs);
1470 rhs = convert_from_reference (rhs);
1471 tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (lhs));
1472 gcc_assert (!defining || COMPLETE_TYPE_P (ctype));
1474 iloc_sentinel ils (info.loc);
1476 /* A defaulted comparison operator function for class C is defined as
1477 deleted if ... C has variant members. */
1478 if (TREE_CODE (ctype) == UNION_TYPE
1479 && next_aggregate_field (TYPE_FIELDS (ctype)))
1481 if (complain & tf_error)
1482 inform (info.loc, "cannot default compare union %qT", ctype);
1483 DECL_DELETED_FN (fndecl) = true;
1484 return;
1487 tree compound_stmt = NULL_TREE;
1488 if (defining)
1489 compound_stmt = begin_compound_stmt (0);
1490 else
1491 ++cp_unevaluated_operand;
1493 tree rettype = TREE_TYPE (TREE_TYPE (fndecl));
1494 if (code != SPACESHIP_EXPR && is_auto (rettype))
1496 rettype = boolean_type_node;
1497 apply_deduced_return_type (fndecl, rettype);
1500 if (code == EQ_EXPR || code == SPACESHIP_EXPR)
1502 comp_cat_tag &retcat = (info.retcat = cc_last);
1503 if (code == SPACESHIP_EXPR && !FNDECL_USED_AUTO (fndecl))
1504 retcat = cat_tag_for (rettype);
1506 bool bad = false;
1507 auto_vec<tree> comps;
1509 /* Compare the base subobjects. We handle them this way, rather than in
1510 the field loop below, because maybe_instantiate_noexcept might bring
1511 us here before we've built the base fields. */
1512 for (tree base_binfo : BINFO_BASE_BINFOS (TYPE_BINFO (ctype)))
1514 tree lhs_base
1515 = build_base_path (PLUS_EXPR, lhs, base_binfo, 0, complain);
1516 tree rhs_base
1517 = build_base_path (PLUS_EXPR, rhs, base_binfo, 0, complain);
1519 location_t loc = DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (ctype));
1520 tree comp = do_one_comp (loc, info, BINFO_TYPE (base_binfo),
1521 lhs_base, rhs_base);
1522 if (comp == error_mark_node)
1524 bad = true;
1525 continue;
1528 comps.safe_push (comp);
1531 /* Now compare the field subobjects. */
1532 for (tree field = next_aggregate_field (TYPE_FIELDS (ctype));
1533 field;
1534 field = next_aggregate_field (DECL_CHAIN (field)))
1536 if (DECL_VIRTUAL_P (field) || DECL_FIELD_IS_BASE (field))
1537 /* We ignore the vptr, and we already handled bases. */
1538 continue;
1540 tree expr_type = TREE_TYPE (field);
1542 location_t field_loc = DECL_SOURCE_LOCATION (field);
1544 /* A defaulted comparison operator function for class C is defined as
1545 deleted if any non-static data member of C is of reference type or
1546 C has variant members. */
1547 if (TREE_CODE (expr_type) == REFERENCE_TYPE)
1549 if (complain & tf_error)
1550 inform (field_loc, "cannot default compare "
1551 "reference member %qD", field);
1552 bad = true;
1553 continue;
1555 else if (ANON_UNION_TYPE_P (expr_type)
1556 && next_aggregate_field (TYPE_FIELDS (expr_type)))
1558 if (complain & tf_error)
1559 inform (field_loc, "cannot default compare "
1560 "anonymous union member");
1561 bad = true;
1562 continue;
1565 tree lhs_mem = build3_loc (field_loc, COMPONENT_REF, expr_type, lhs,
1566 field, NULL_TREE);
1567 tree rhs_mem = build3_loc (field_loc, COMPONENT_REF, expr_type, rhs,
1568 field, NULL_TREE);
1569 tree loop_indexes = NULL_TREE;
1570 while (TREE_CODE (expr_type) == ARRAY_TYPE)
1572 /* Flexible array member. */
1573 if (TYPE_DOMAIN (expr_type) == NULL_TREE
1574 || TYPE_MAX_VALUE (TYPE_DOMAIN (expr_type)) == NULL_TREE)
1576 if (complain & tf_error)
1577 inform (field_loc, "cannot default compare "
1578 "flexible array member");
1579 bad = true;
1580 break;
1582 tree maxval = TYPE_MAX_VALUE (TYPE_DOMAIN (expr_type));
1583 /* [0] array. No subobjects to compare, just skip it. */
1584 if (integer_all_onesp (maxval))
1585 break;
1586 tree idx;
1587 /* [1] array, no loop needed, just add [0] ARRAY_REF.
1588 Similarly if !defining. */
1589 if (integer_zerop (maxval) || !defining)
1590 idx = size_zero_node;
1591 /* Some other array, will need runtime loop. */
1592 else
1594 idx = force_target_expr (sizetype, maxval, complain);
1595 loop_indexes = tree_cons (idx, NULL_TREE, loop_indexes);
1597 expr_type = TREE_TYPE (expr_type);
1598 lhs_mem = build4_loc (field_loc, ARRAY_REF, expr_type, lhs_mem,
1599 idx, NULL_TREE, NULL_TREE);
1600 rhs_mem = build4_loc (field_loc, ARRAY_REF, expr_type, rhs_mem,
1601 idx, NULL_TREE, NULL_TREE);
1603 if (TREE_CODE (expr_type) == ARRAY_TYPE)
1604 continue;
1606 tree comp = do_one_comp (field_loc, info, field, lhs_mem, rhs_mem);
1607 if (comp == error_mark_node)
1609 bad = true;
1610 continue;
1613 /* Most of the time, comp is the expression that should be evaluated
1614 to compare the two members. If the expression needs to be
1615 evaluated more than once in a loop, it will be a TREE_LIST
1616 instead, whose TREE_VALUE is the expression for one array element,
1617 TREE_PURPOSE is innermost iterator temporary and if the array
1618 is multidimensional, TREE_CHAIN will contain another TREE_LIST
1619 with second innermost iterator in its TREE_PURPOSE and so on. */
1620 if (loop_indexes)
1622 TREE_VALUE (loop_indexes) = comp;
1623 comp = loop_indexes;
1625 comps.safe_push (comp);
1627 if (code == SPACESHIP_EXPR && is_auto (rettype))
1629 rettype = common_comparison_type (comps);
1630 apply_deduced_return_type (fndecl, rettype);
1632 if (bad)
1634 DECL_DELETED_FN (fndecl) = true;
1635 goto out;
1637 for (unsigned i = 0; i < comps.length(); ++i)
1639 tree comp = comps[i];
1640 tree eq, retval = NULL_TREE, if_ = NULL_TREE;
1641 tree loop_indexes = NULL_TREE;
1642 if (defining)
1644 if (TREE_CODE (comp) == TREE_LIST)
1646 loop_indexes = comp;
1647 comp = TREE_VALUE (comp);
1648 loop_indexes = nreverse (loop_indexes);
1649 for (tree loop_index = loop_indexes; loop_index;
1650 loop_index = TREE_CHAIN (loop_index))
1652 tree for_stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
1653 tree idx = TREE_PURPOSE (loop_index);
1654 tree maxval = TARGET_EXPR_INITIAL (idx);
1655 TARGET_EXPR_INITIAL (idx) = size_zero_node;
1656 add_stmt (idx);
1657 finish_init_stmt (for_stmt);
1658 finish_for_cond (build2 (LE_EXPR, boolean_type_node, idx,
1659 maxval), for_stmt, false, 0,
1660 false);
1661 finish_for_expr (cp_build_unary_op (PREINCREMENT_EXPR,
1662 TARGET_EXPR_SLOT (idx),
1663 false, complain),
1664 for_stmt);
1665 /* Store in TREE_VALUE the for_stmt tree, so that we can
1666 later on call finish_for_stmt on it (in the reverse
1667 order). */
1668 TREE_VALUE (loop_index) = for_stmt;
1670 loop_indexes = nreverse (loop_indexes);
1672 if_ = begin_if_stmt ();
1674 /* Spaceship is specified to use !=, but for the comparison category
1675 types, != is equivalent to !(==), so let's use == directly. */
1676 if (code == EQ_EXPR)
1678 /* if (x==y); else return false; */
1679 eq = comp;
1680 retval = boolean_false_node;
1682 else
1684 /* if (auto v = x<=>y, v == 0); else return v; */
1685 if (TREE_CODE (comp) == SPACESHIP_EXPR)
1686 TREE_TYPE (comp) = rettype;
1687 else
1688 comp = build_static_cast (input_location, rettype, comp,
1689 complain);
1690 info.check (comp);
1691 if (defining)
1693 tree var = create_temporary_var (rettype);
1694 DECL_NAME (var) = get_identifier ("retval");
1695 pushdecl (var);
1696 cp_finish_decl (var, comp, false, NULL_TREE, flags);
1697 comp = retval = var;
1699 eq = build_new_op (info.loc, EQ_EXPR, flags, comp,
1700 integer_zero_node, NULL_TREE, NULL_TREE,
1701 NULL, complain);
1703 tree ceq = contextual_conv_bool (eq, complain);
1704 info.check (ceq);
1705 if (defining)
1707 finish_if_stmt_cond (ceq, if_);
1708 finish_then_clause (if_);
1709 begin_else_clause (if_);
1710 finish_return_stmt (retval);
1711 finish_else_clause (if_);
1712 finish_if_stmt (if_);
1713 for (tree loop_index = loop_indexes; loop_index;
1714 loop_index = TREE_CHAIN (loop_index))
1715 finish_for_stmt (TREE_VALUE (loop_index));
1718 if (defining)
1720 tree val;
1721 if (code == EQ_EXPR)
1722 val = boolean_true_node;
1723 else
1725 tree seql = lookup_comparison_result (cc_strong_ordering,
1726 "equal", complain);
1727 val = build_static_cast (input_location, rettype, seql,
1728 complain);
1730 finish_return_stmt (val);
1733 else if (code == NE_EXPR)
1735 tree comp = build_new_op (info.loc, EQ_EXPR, flags, lhs, rhs,
1736 NULL_TREE, NULL_TREE, NULL, complain);
1737 comp = contextual_conv_bool (comp, complain);
1738 info.check (comp);
1739 if (defining)
1741 tree neg = build1 (TRUTH_NOT_EXPR, boolean_type_node, comp);
1742 finish_return_stmt (neg);
1745 else
1747 tree comp = build_new_op (info.loc, SPACESHIP_EXPR, flags, lhs, rhs,
1748 NULL_TREE, NULL_TREE, NULL, complain);
1749 tree comp2 = build_new_op (info.loc, code, flags, comp, integer_zero_node,
1750 NULL_TREE, NULL_TREE, NULL, complain);
1751 info.check (comp2);
1752 if (defining)
1753 finish_return_stmt (comp2);
1756 out:
1757 if (defining)
1758 finish_compound_stmt (compound_stmt);
1759 else
1760 --cp_unevaluated_operand;
1763 /* True iff DECL is an implicitly-declared special member function with no real
1764 source location, so we can use its DECL_SOURCE_LOCATION to remember where we
1765 triggered its synthesis. */
1767 bool
1768 decl_remember_implicit_trigger_p (tree decl)
1770 if (!DECL_ARTIFICIAL (decl))
1771 return false;
1772 special_function_kind sfk = special_function_p (decl);
1773 /* Inherited constructors have the location of their using-declaration, and
1774 operator== has the location of the corresponding operator<=>. */
1775 return (sfk != sfk_inheriting_constructor
1776 && sfk != sfk_comparison);
1779 /* Synthesize FNDECL, a non-static member function. */
1781 void
1782 synthesize_method (tree fndecl)
1784 bool need_body = true;
1785 tree stmt;
1786 location_t save_input_location = input_location;
1787 int error_count = errorcount;
1788 int warning_count = warningcount + werrorcount;
1789 special_function_kind sfk = special_function_p (fndecl);
1791 /* Reset the source location, we might have been previously
1792 deferred, and thus have saved where we were first needed. */
1793 if (decl_remember_implicit_trigger_p (fndecl))
1794 DECL_SOURCE_LOCATION (fndecl)
1795 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
1797 /* If we've been asked to synthesize a clone, just synthesize the
1798 cloned function instead. Doing so will automatically fill in the
1799 body for the clone. */
1800 if (DECL_CLONED_FUNCTION_P (fndecl))
1801 fndecl = DECL_CLONED_FUNCTION (fndecl);
1803 /* We may be in the middle of deferred access check. Disable
1804 it now. */
1805 push_deferring_access_checks (dk_no_deferred);
1807 bool push_to_top = maybe_push_to_top_level (fndecl);
1809 input_location = DECL_SOURCE_LOCATION (fndecl);
1811 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
1812 stmt = begin_function_body ();
1814 if (DECL_ASSIGNMENT_OPERATOR_P (fndecl)
1815 && DECL_OVERLOADED_OPERATOR_IS (fndecl, NOP_EXPR))
1817 do_build_copy_assign (fndecl);
1818 need_body = false;
1820 else if (DECL_CONSTRUCTOR_P (fndecl))
1822 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
1823 if (arg_chain != void_list_node)
1824 do_build_copy_constructor (fndecl);
1825 else
1826 finish_mem_initializers (NULL_TREE);
1828 else if (sfk == sfk_comparison)
1830 /* Pass tf_none so the function is just deleted if there's a problem. */
1831 build_comparison_op (fndecl, true, tf_none);
1832 need_body = false;
1835 /* If we haven't yet generated the body of the function, just
1836 generate an empty compound statement. */
1837 if (need_body)
1839 tree compound_stmt;
1840 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
1841 finish_compound_stmt (compound_stmt);
1844 finish_function_body (stmt);
1845 finish_function (/*inline_p=*/false);
1847 if (!DECL_DELETED_FN (fndecl))
1848 expand_or_defer_fn (fndecl);
1850 input_location = save_input_location;
1852 maybe_pop_from_top_level (push_to_top);
1854 pop_deferring_access_checks ();
1856 if (error_count != errorcount || warning_count != warningcount + werrorcount)
1857 if (DECL_ARTIFICIAL (fndecl))
1858 inform (input_location, "synthesized method %qD first required here",
1859 fndecl);
1862 /* Like synthesize_method, but don't actually synthesize defaulted comparison
1863 methods if their class is still incomplete. Just deduce the return
1864 type in that case. */
1866 void
1867 maybe_synthesize_method (tree fndecl)
1869 if (special_function_p (fndecl) == sfk_comparison)
1871 tree lhs = DECL_ARGUMENTS (fndecl);
1872 if (is_this_parameter (lhs))
1873 lhs = cp_build_fold_indirect_ref (lhs);
1874 else
1875 lhs = convert_from_reference (lhs);
1876 tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (lhs));
1877 if (!COMPLETE_TYPE_P (ctype))
1879 push_deferring_access_checks (dk_no_deferred);
1880 build_comparison_op (fndecl, false, tf_none);
1881 pop_deferring_access_checks ();
1882 return;
1885 return synthesize_method (fndecl);
1888 /* Build a reference to type TYPE with cv-quals QUALS, which is an
1889 rvalue if RVALUE is true. */
1891 static tree
1892 build_stub_type (tree type, int quals, bool rvalue)
1894 tree argtype = cp_build_qualified_type (type, quals);
1895 return cp_build_reference_type (argtype, rvalue);
1898 /* Build a dummy glvalue from dereferencing a dummy reference of type
1899 REFTYPE. */
1901 tree
1902 build_stub_object (tree reftype)
1904 if (!TYPE_REF_P (reftype))
1905 reftype = cp_build_reference_type (reftype, /*rval*/true);
1906 tree stub = build1 (CONVERT_EXPR, reftype, integer_one_node);
1907 return convert_from_reference (stub);
1910 /* Build a std::declval<TYPE>() expression and return it. */
1912 tree
1913 build_trait_object (tree type)
1915 /* TYPE can't be a function with cv-/ref-qualifiers: std::declval is
1916 defined as
1918 template<class T>
1919 typename std::add_rvalue_reference<T>::type declval() noexcept;
1921 and std::add_rvalue_reference yields T when T is a function with
1922 cv- or ref-qualifiers, making the definition ill-formed. */
1923 if (FUNC_OR_METHOD_TYPE_P (type)
1924 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
1925 || type_memfn_rqual (type) != REF_QUAL_NONE))
1926 return error_mark_node;
1928 return build_stub_object (type);
1931 /* [func.require] Build an expression of INVOKE(FN_TYPE, ARG_TYPES...). If the
1932 given is not invocable, returns error_mark_node. */
1934 tree
1935 build_invoke (tree fn_type, const_tree arg_types, tsubst_flags_t complain)
1937 if (error_operand_p (fn_type) || error_operand_p (arg_types))
1938 return error_mark_node;
1940 gcc_assert (TYPE_P (fn_type));
1941 gcc_assert (TREE_CODE (arg_types) == TREE_VEC);
1943 /* Access check is required to determine if the given is invocable. */
1944 deferring_access_check_sentinel acs (dk_no_deferred);
1946 /* INVOKE is an unevaluated context. */
1947 cp_unevaluated cp_uneval_guard;
1949 bool is_ptrdatamem;
1950 bool is_ptrmemfunc;
1951 if (TREE_CODE (fn_type) == REFERENCE_TYPE)
1953 tree non_ref_fn_type = TREE_TYPE (fn_type);
1954 is_ptrdatamem = TYPE_PTRDATAMEM_P (non_ref_fn_type);
1955 is_ptrmemfunc = TYPE_PTRMEMFUNC_P (non_ref_fn_type);
1957 /* Dereference fn_type if it is a pointer to member. */
1958 if (is_ptrdatamem || is_ptrmemfunc)
1959 fn_type = non_ref_fn_type;
1961 else
1963 is_ptrdatamem = TYPE_PTRDATAMEM_P (fn_type);
1964 is_ptrmemfunc = TYPE_PTRMEMFUNC_P (fn_type);
1967 if (is_ptrdatamem && TREE_VEC_LENGTH (arg_types) != 1)
1969 if (complain & tf_error)
1970 error ("pointer to data member type %qT can only be invoked with "
1971 "one argument", fn_type);
1972 return error_mark_node;
1974 if (is_ptrmemfunc && TREE_VEC_LENGTH (arg_types) == 0)
1976 if (complain & tf_error)
1977 error ("pointer to member function type %qT must be invoked with "
1978 "at least one argument", fn_type);
1979 return error_mark_node;
1982 /* Construct an expression of a pointer to member. */
1983 tree ptrmem_expr;
1984 if (is_ptrdatamem || is_ptrmemfunc)
1986 tree datum_type = TREE_VEC_ELT (arg_types, 0);
1987 tree non_ref_datum_type = datum_type;
1988 if (TYPE_REF_P (datum_type))
1989 non_ref_datum_type = TREE_TYPE (datum_type);
1991 /* datum must be a class type or a pointer to a class type. */
1992 if (!CLASS_TYPE_P (non_ref_datum_type)
1993 && !(POINTER_TYPE_P (non_ref_datum_type)
1994 && CLASS_TYPE_P (TREE_TYPE (non_ref_datum_type))))
1996 if (complain & tf_error)
1997 error ("first argument type %qT of a pointer to member must be a "
1998 "class type or a pointer to a class type", datum_type);
1999 return error_mark_node;
2002 /* 1.1 & 1.4. */
2003 tree ptrmem_class_type = TYPE_PTRMEM_CLASS_TYPE (fn_type);
2004 const bool ptrmem_is_same_or_base_of_datum =
2005 (same_type_ignoring_top_level_qualifiers_p (ptrmem_class_type,
2006 non_ref_datum_type)
2007 || (NON_UNION_CLASS_TYPE_P (ptrmem_class_type)
2008 && NON_UNION_CLASS_TYPE_P (non_ref_datum_type)
2009 && DERIVED_FROM_P (ptrmem_class_type, non_ref_datum_type)));
2011 bool datum_is_refwrap = false;
2012 if (!ptrmem_is_same_or_base_of_datum && CLASS_TYPE_P (non_ref_datum_type))
2014 tree datum_decl = TYPE_NAME (TYPE_MAIN_VARIANT (non_ref_datum_type));
2015 if (decl_in_std_namespace_p (datum_decl))
2017 const_tree name = DECL_NAME (datum_decl);
2018 if (name && (id_equal (name, "reference_wrapper")))
2020 /* 1.2 & 1.5: Retrieve T from std::reference_wrapper<T>,
2021 i.e., decltype(datum.get()). */
2022 datum_type =
2023 TREE_VEC_ELT (TYPE_TI_ARGS (non_ref_datum_type), 0);
2024 datum_is_refwrap = true;
2029 tree datum_expr = build_trait_object (datum_type);
2030 if (!ptrmem_is_same_or_base_of_datum && !datum_is_refwrap)
2031 /* 1.3 & 1.6: Try to dereference datum_expr. */
2032 datum_expr = build_x_indirect_ref (UNKNOWN_LOCATION, datum_expr,
2033 RO_UNARY_STAR, NULL_TREE, complain);
2035 tree fn_expr = build_trait_object (fn_type);
2036 ptrmem_expr = build_m_component_ref (datum_expr, fn_expr, complain);
2038 if (error_operand_p (ptrmem_expr))
2039 return error_mark_node;
2041 if (is_ptrdatamem)
2042 return ptrmem_expr;
2045 /* Construct expressions for arguments to INVOKE. For a pointer to member
2046 function, the first argument, which is the object, is not arguments to
2047 the function. */
2048 releasing_vec args;
2049 for (int i = is_ptrmemfunc ? 1 : 0; i < TREE_VEC_LENGTH (arg_types); ++i)
2051 tree arg_type = TREE_VEC_ELT (arg_types, i);
2052 tree arg = build_trait_object (arg_type);
2053 vec_safe_push (args, arg);
2056 tree invoke_expr;
2057 if (is_ptrmemfunc)
2058 invoke_expr = build_offset_ref_call_from_tree (ptrmem_expr, &args,
2059 complain);
2060 else /* 1.7. */
2061 invoke_expr = finish_call_expr (build_trait_object (fn_type), &args, false,
2062 false, complain);
2063 return invoke_expr;
2066 /* Determine which function will be called when looking up NAME in TYPE,
2067 called with a single ARGTYPE argument, or no argument if ARGTYPE is
2068 null. FLAGS and COMPLAIN are as for build_new_method_call.
2070 Returns a FUNCTION_DECL if all is well.
2071 Returns NULL_TREE if overload resolution failed.
2072 Returns error_mark_node if the chosen function cannot be called. */
2074 static tree
2075 locate_fn_flags (tree type, tree name, tree argtype, int flags,
2076 tsubst_flags_t complain)
2078 tree ob, fn, fns, binfo, rval;
2080 if (TYPE_P (type))
2081 binfo = TYPE_BINFO (type);
2082 else
2084 binfo = type;
2085 type = BINFO_TYPE (binfo);
2088 ob = build_stub_object (cp_build_reference_type (type, false));
2089 releasing_vec args;
2090 if (argtype)
2092 if (TREE_CODE (argtype) == TREE_LIST)
2094 for (tree elt = argtype; elt && elt != void_list_node;
2095 elt = TREE_CHAIN (elt))
2097 tree type = TREE_VALUE (elt);
2098 tree arg = build_stub_object (type);
2099 vec_safe_push (args, arg);
2102 else
2104 tree arg = build_stub_object (argtype);
2105 args->quick_push (arg);
2109 fns = lookup_fnfields (binfo, name, 0, complain);
2110 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
2112 if (fn && rval == error_mark_node)
2113 return rval;
2114 else
2115 return fn;
2118 /* Locate the dtor of TYPE. */
2120 tree
2121 get_dtor (tree type, tsubst_flags_t complain)
2123 tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
2124 LOOKUP_NORMAL, complain);
2125 if (fn == error_mark_node)
2126 return NULL_TREE;
2127 return fn;
2130 /* Locate the default ctor of TYPE. */
2132 tree
2133 locate_ctor (tree type)
2135 tree fn;
2137 push_deferring_access_checks (dk_no_check);
2138 fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
2139 LOOKUP_SPECULATIVE, tf_none);
2140 pop_deferring_access_checks ();
2141 if (fn == error_mark_node)
2142 return NULL_TREE;
2143 return fn;
2146 /* Likewise, but give any appropriate errors. */
2148 tree
2149 get_default_ctor (tree type)
2151 tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
2152 LOOKUP_NORMAL, tf_warning_or_error);
2153 if (fn == error_mark_node)
2154 return NULL_TREE;
2155 return fn;
2158 /* Locate the copy ctor of TYPE. */
2160 tree
2161 get_copy_ctor (tree type, tsubst_flags_t complain)
2163 int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
2164 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
2165 tree argtype = build_stub_type (type, quals, false);
2166 tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
2167 LOOKUP_NORMAL, complain);
2168 if (fn == error_mark_node)
2169 return NULL_TREE;
2170 return fn;
2173 /* Locate the copy assignment operator of TYPE. */
2175 tree
2176 get_copy_assign (tree type)
2178 int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
2179 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
2180 tree argtype = build_stub_type (type, quals, false);
2181 tree fn = locate_fn_flags (type, assign_op_identifier, argtype,
2182 LOOKUP_NORMAL, tf_warning_or_error);
2183 if (fn == error_mark_node)
2184 return NULL_TREE;
2185 return fn;
2188 /* walk_tree helper function for is_trivially_xible. If *TP is a call,
2189 return it if it calls something other than a trivial special member
2190 function. */
2192 static tree
2193 check_nontriv (tree *tp, int *, void *)
2195 tree fn = cp_get_callee (*tp);
2196 if (fn == NULL_TREE)
2197 return NULL_TREE;
2199 if (TREE_CODE (fn) == ADDR_EXPR)
2200 fn = TREE_OPERAND (fn, 0);
2202 if (TREE_CODE (fn) != FUNCTION_DECL
2203 || !trivial_fn_p (fn))
2204 return fn;
2205 return NULL_TREE;
2208 /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */
2210 static tree
2211 assignable_expr (tree to, tree from)
2213 cp_unevaluated cp_uneval_guard;
2214 to = build_trait_object (to);
2215 from = build_trait_object (from);
2216 tree r = cp_build_modify_expr (input_location, to, NOP_EXPR, from, tf_none);
2217 return r;
2220 /* The predicate condition for a template specialization
2221 is_constructible<T, Args...> shall be satisfied if and only if the
2222 following variable definition would be well-formed for some invented
2223 variable t: T t(create<Args>()...);
2225 Return something equivalent in well-formedness and triviality. */
2227 static tree
2228 constructible_expr (tree to, tree from)
2230 tree expr;
2231 cp_unevaluated cp_uneval_guard;
2232 const int len = TREE_VEC_LENGTH (from);
2233 if (CLASS_TYPE_P (to))
2235 tree ctype = to;
2236 vec<tree, va_gc> *args = NULL;
2237 if (!TYPE_REF_P (to))
2238 to = cp_build_reference_type (to, /*rval*/false);
2239 tree ob = build_stub_object (to);
2240 if (len == 0)
2241 expr = build_value_init (ctype, tf_none);
2242 else
2244 vec_alloc (args, len);
2245 for (tree arg : tree_vec_range (from))
2246 args->quick_push (build_stub_object (arg));
2247 expr = build_special_member_call (ob, complete_ctor_identifier, &args,
2248 ctype, LOOKUP_NORMAL, tf_none);
2250 if (expr == error_mark_node)
2251 return error_mark_node;
2252 /* The current state of the standard vis-a-vis LWG 2116 is that
2253 is_*constructible involves destruction as well. */
2254 if (type_build_dtor_call (ctype))
2256 tree dtor = build_special_member_call (ob, complete_dtor_identifier,
2257 NULL, ctype, LOOKUP_NORMAL,
2258 tf_none);
2259 if (dtor == error_mark_node)
2260 return error_mark_node;
2261 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
2262 expr = build2 (COMPOUND_EXPR, void_type_node, expr, dtor);
2265 else
2267 if (len == 0)
2268 return build_value_init (strip_array_types (to), tf_none);
2269 if (len > 1)
2271 if (cxx_dialect < cxx20)
2272 /* Too many initializers. */
2273 return error_mark_node;
2275 /* In C++20 this is well-formed:
2276 using T = int[2];
2277 T t(1, 2);
2278 which means that std::is_constructible_v<int[2], int, int>
2279 should be true. */
2280 vec<constructor_elt, va_gc> *v;
2281 vec_alloc (v, len);
2282 for (tree arg : tree_vec_range (from))
2284 tree stub = build_stub_object (arg);
2285 constructor_elt elt = { NULL_TREE, stub };
2286 v->quick_push (elt);
2288 from = build_constructor (init_list_type_node, v);
2289 CONSTRUCTOR_IS_DIRECT_INIT (from) = true;
2290 CONSTRUCTOR_IS_PAREN_INIT (from) = true;
2292 else
2293 from = build_stub_object (TREE_VEC_ELT (from, 0));
2294 expr = perform_direct_initialization_if_possible (to, from,
2295 /*cast*/false,
2296 tf_none);
2297 /* If t(e) didn't work, maybe t{e} will. */
2298 if (expr == NULL_TREE
2299 && len == 1
2300 && cxx_dialect >= cxx20)
2302 from = build_constructor_single (init_list_type_node, NULL_TREE,
2303 from);
2304 CONSTRUCTOR_IS_DIRECT_INIT (from) = true;
2305 CONSTRUCTOR_IS_PAREN_INIT (from) = true;
2306 expr = perform_direct_initialization_if_possible (to, from,
2307 /*cast*/false,
2308 tf_none);
2311 return expr;
2314 /* Returns a tree iff TO is assignable (if CODE is MODIFY_EXPR) or
2315 constructible (otherwise) from FROM, which is a single type for
2316 assignment or a list of types for construction. */
2318 static tree
2319 is_xible_helper (enum tree_code code, tree to, tree from, bool trivial)
2321 to = complete_type (to);
2322 deferring_access_check_sentinel acs (dk_no_deferred);
2323 if (VOID_TYPE_P (to) || ABSTRACT_CLASS_TYPE_P (to)
2324 || (from && FUNC_OR_METHOD_TYPE_P (from)
2325 && (TYPE_READONLY (from) || FUNCTION_REF_QUALIFIED (from))))
2326 return error_mark_node;
2327 tree expr;
2328 if (code == MODIFY_EXPR)
2329 expr = assignable_expr (to, from);
2330 else if (trivial && TREE_VEC_LENGTH (from) > 1
2331 && cxx_dialect < cxx20)
2332 return error_mark_node; // only 0- and 1-argument ctors can be trivial
2333 // before C++20 aggregate paren init
2334 else if (TREE_CODE (to) == ARRAY_TYPE && !TYPE_DOMAIN (to))
2335 return error_mark_node; // can't construct an array of unknown bound
2336 else
2337 expr = constructible_expr (to, from);
2338 return expr;
2341 /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or
2342 constructible (otherwise) from FROM, which is a single type for
2343 assignment or a list of types for construction. */
2345 bool
2346 is_trivially_xible (enum tree_code code, tree to, tree from)
2348 tree expr = is_xible_helper (code, to, from, /*trivial*/true);
2349 if (expr == NULL_TREE || expr == error_mark_node)
2350 return false;
2351 tree nt = cp_walk_tree_without_duplicates (&expr, check_nontriv, NULL);
2352 return !nt;
2355 /* Returns true iff TO is nothrow assignable (if CODE is MODIFY_EXPR) or
2356 constructible (otherwise) from FROM, which is a single type for
2357 assignment or a list of types for construction. */
2359 bool
2360 is_nothrow_xible (enum tree_code code, tree to, tree from)
2362 ++cp_noexcept_operand;
2363 tree expr = is_xible_helper (code, to, from, /*trivial*/false);
2364 --cp_noexcept_operand;
2365 if (expr == NULL_TREE || expr == error_mark_node)
2366 return false;
2367 return expr_noexcept_p (expr, tf_none);
2370 /* Returns true iff TO is assignable (if CODE is MODIFY_EXPR) or
2371 constructible (otherwise) from FROM, which is a single type for
2372 assignment or a list of types for construction. */
2374 bool
2375 is_xible (enum tree_code code, tree to, tree from)
2377 tree expr = is_xible_helper (code, to, from, /*trivial*/false);
2378 if (expr == error_mark_node)
2379 return false;
2380 return !!expr;
2383 /* Return true iff conjunction_v<is_reference<T>, is_constructible<T, U>> is
2384 true, and the initialization
2385 T t(VAL<U>); // DIRECT_INIT_P
2387 T t = VAL<U>; // !DIRECT_INIT_P
2388 binds t to a temporary object whose lifetime is extended.
2389 VAL<T> is defined in [meta.unary.prop]:
2390 -- If T is a reference or function type, VAL<T> is an expression with the
2391 same type and value category as declval<T>().
2392 -- Otherwise, VAL<T> is a prvalue that initially has type T. */
2394 bool
2395 ref_xes_from_temporary (tree to, tree from, bool direct_init_p)
2397 /* Check is_reference<T>. */
2398 if (!TYPE_REF_P (to))
2399 return false;
2400 /* We don't check is_constructible<T, U>: if T isn't constructible
2401 from U, we won't be able to create a conversion. */
2402 tree val = build_trait_object (from);
2403 if (val == error_mark_node)
2404 return false;
2405 if (!TYPE_REF_P (from) && TREE_CODE (from) != FUNCTION_TYPE)
2406 val = CLASS_TYPE_P (from) ? force_rvalue (val, tf_none) : rvalue (val);
2407 return ref_conv_binds_to_temporary (to, val, direct_init_p).is_true ();
2410 /* Worker for is_{,nothrow_}convertible. Attempt to perform an implicit
2411 conversion from FROM to TO and return the result. */
2413 static tree
2414 is_convertible_helper (tree from, tree to)
2416 if (VOID_TYPE_P (from) && VOID_TYPE_P (to))
2417 return integer_one_node;
2418 cp_unevaluated u;
2419 tree expr = build_trait_object (from);
2420 /* std::is_{,nothrow_}convertible test whether the imaginary function
2421 definition
2423 To test() { return std::declval<From>(); }
2425 is well-formed. A function can't return a function. */
2426 if (FUNC_OR_METHOD_TYPE_P (to) || expr == error_mark_node)
2427 return error_mark_node;
2428 deferring_access_check_sentinel acs (dk_no_deferred);
2429 return perform_implicit_conversion (to, expr, tf_none);
2432 /* Return true if FROM can be converted to TO using implicit conversions,
2433 or both FROM and TO are possibly cv-qualified void. NB: This doesn't
2434 implement the "Access checks are performed as if from a context unrelated
2435 to either type" restriction. */
2437 bool
2438 is_convertible (tree from, tree to)
2440 tree expr = is_convertible_helper (from, to);
2441 if (expr == error_mark_node)
2442 return false;
2443 return !!expr;
2446 /* Like is_convertible, but the conversion is also noexcept. */
2448 bool
2449 is_nothrow_convertible (tree from, tree to)
2451 tree expr = is_convertible_helper (from, to);
2452 if (expr == NULL_TREE || expr == error_mark_node)
2453 return false;
2454 return expr_noexcept_p (expr, tf_none);
2457 /* Categorize various special_function_kinds. */
2458 #define SFK_CTOR_P(sfk) \
2459 ((sfk) >= sfk_constructor && (sfk) <= sfk_move_constructor)
2460 #define SFK_DTOR_P(sfk) \
2461 ((sfk) == sfk_destructor || (sfk) == sfk_virtual_destructor)
2462 #define SFK_ASSIGN_P(sfk) \
2463 ((sfk) == sfk_copy_assignment || (sfk) == sfk_move_assignment)
2464 #define SFK_COPY_P(sfk) \
2465 ((sfk) == sfk_copy_constructor || (sfk) == sfk_copy_assignment)
2466 #define SFK_MOVE_P(sfk) \
2467 ((sfk) == sfk_move_constructor || (sfk) == sfk_move_assignment)
2469 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
2470 DELETED_P or give an error message MSG with argument ARG. */
2472 static void
2473 process_subob_fn (tree fn, special_function_kind sfk, tree *spec_p,
2474 bool *trivial_p, bool *deleted_p, bool *constexpr_p,
2475 bool diag, tree arg, bool dtor_from_ctor = false)
2477 if (!fn || fn == error_mark_node)
2479 if (deleted_p)
2480 *deleted_p = true;
2481 return;
2484 if (spec_p)
2486 if (!maybe_instantiate_noexcept (fn))
2487 *spec_p = error_mark_node;
2488 else
2490 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2491 *spec_p = merge_exception_specifiers (*spec_p, raises);
2495 if (!trivial_fn_p (fn) && !dtor_from_ctor)
2497 if (trivial_p)
2498 *trivial_p = false;
2499 if (TREE_CODE (arg) == FIELD_DECL
2500 && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
2502 if (deleted_p)
2503 *deleted_p = true;
2504 if (diag)
2505 error ("union member %q+D with non-trivial %qD", arg, fn);
2509 if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
2511 *constexpr_p = false;
2512 if (diag)
2514 inform (DECL_SOURCE_LOCATION (fn),
2515 SFK_DTOR_P (sfk)
2516 ? G_("defaulted destructor calls non-%<constexpr%> %qD")
2517 : G_("defaulted constructor calls non-%<constexpr%> %qD"),
2518 fn);
2519 explain_invalid_constexpr_fn (fn);
2524 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
2525 aggregates. If DTOR_FROM_CTOR is true, we're walking subobject destructors
2526 called from a synthesized constructor, in which case we don't consider
2527 the triviality of the subobject destructor. */
2529 static void
2530 walk_field_subobs (tree fields, special_function_kind sfk, tree fnname,
2531 int quals, tree *spec_p, bool *trivial_p,
2532 bool *deleted_p, bool *constexpr_p,
2533 bool diag, int flags, tsubst_flags_t complain,
2534 bool dtor_from_ctor)
2536 if (!fields)
2537 return;
2539 tree ctx = DECL_CONTEXT (fields);
2541 /* CWG2084: A defaulted default ctor for a union with a DMI only initializes
2542 that member, so don't check other members. */
2543 enum { unknown, no, yes }
2544 only_dmi_mem = (sfk == sfk_constructor && TREE_CODE (ctx) == UNION_TYPE
2545 ? unknown : no);
2547 again:
2548 for (tree field = fields; field; field = DECL_CHAIN (field))
2550 tree mem_type, argtype, rval;
2552 if (TREE_CODE (field) != FIELD_DECL
2553 || DECL_ARTIFICIAL (field)
2554 || DECL_UNNAMED_BIT_FIELD (field))
2555 continue;
2557 /* Variant members only affect deletedness. In particular, they don't
2558 affect the exception-specification of a user-provided destructor,
2559 which we're figuring out via get_defaulted_eh_spec. So if we aren't
2560 asking if this is deleted, don't even look up the function; we don't
2561 want an error about a deleted function we aren't actually calling. */
2562 if (sfk == sfk_destructor && deleted_p == NULL
2563 && TREE_CODE (ctx) == UNION_TYPE)
2564 break;
2566 if (only_dmi_mem != no)
2568 if (DECL_INITIAL (field))
2569 only_dmi_mem = yes;
2570 else
2571 /* Don't check this until we know there's no DMI. */
2572 continue;
2575 mem_type = strip_array_types (TREE_TYPE (field));
2576 if (SFK_ASSIGN_P (sfk))
2578 bool bad = true;
2579 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
2581 if (diag)
2582 error ("non-static const member %q#D, cannot use default "
2583 "assignment operator", field);
2585 else if (TYPE_REF_P (mem_type))
2587 if (diag)
2588 error ("non-static reference member %q#D, cannot use "
2589 "default assignment operator", field);
2591 else
2592 bad = false;
2594 if (bad && deleted_p)
2595 *deleted_p = true;
2597 else if (sfk == sfk_constructor || sfk == sfk_inheriting_constructor)
2599 bool bad;
2601 if (DECL_INITIAL (field))
2603 if (diag && DECL_INITIAL (field) == error_mark_node)
2604 inform (DECL_SOURCE_LOCATION (field),
2605 "initializer for %q#D is invalid", field);
2606 if (trivial_p)
2607 *trivial_p = false;
2608 /* Core 1351: If the field has an NSDMI that could throw, the
2609 default constructor is noexcept(false). */
2610 if (spec_p)
2612 tree nsdmi = get_nsdmi (field, /*ctor*/false, complain);
2613 if (nsdmi == error_mark_node)
2614 *spec_p = error_mark_node;
2615 else if (*spec_p != error_mark_node
2616 && !expr_noexcept_p (nsdmi, tf_none))
2617 *spec_p = noexcept_false_spec;
2619 /* Don't do the normal processing. */
2620 continue;
2623 bad = false;
2624 if (CP_TYPE_CONST_P (mem_type)
2625 && default_init_uninitialized_part (mem_type))
2627 if (diag)
2629 error ("uninitialized const member in %q#T",
2630 current_class_type);
2631 inform (DECL_SOURCE_LOCATION (field),
2632 "%q#D should be initialized", field);
2634 bad = true;
2636 else if (TYPE_REF_P (mem_type))
2638 if (diag)
2640 error ("uninitialized reference member in %q#T",
2641 current_class_type);
2642 inform (DECL_SOURCE_LOCATION (field),
2643 "%q#D should be initialized", field);
2645 bad = true;
2648 if (bad && deleted_p)
2649 *deleted_p = true;
2651 /* Before C++20, for an implicitly-defined default constructor to
2652 be constexpr, every member must have a user-provided default
2653 constructor or an explicit initializer. */
2654 if (constexpr_p
2655 && cxx_dialect < cxx20
2656 && !CLASS_TYPE_P (mem_type)
2657 && TREE_CODE (ctx) != UNION_TYPE)
2659 *constexpr_p = false;
2660 if (diag)
2661 inform (DECL_SOURCE_LOCATION (field),
2662 "defaulted default constructor does not "
2663 "initialize %q#D", field);
2666 else if (sfk == sfk_copy_constructor)
2668 /* 12.8p11b5 */
2669 if (TYPE_REF_P (mem_type)
2670 && TYPE_REF_IS_RVALUE (mem_type))
2672 if (diag)
2673 error ("copying non-static data member %q#D of rvalue "
2674 "reference type", field);
2675 if (deleted_p)
2676 *deleted_p = true;
2680 if (!CLASS_TYPE_P (mem_type))
2681 continue;
2683 if (ANON_AGGR_TYPE_P (mem_type))
2685 walk_field_subobs (TYPE_FIELDS (mem_type), sfk, fnname, quals,
2686 spec_p, trivial_p, deleted_p, constexpr_p,
2687 diag, flags, complain, dtor_from_ctor);
2688 continue;
2691 if (SFK_COPY_P (sfk) || SFK_MOVE_P (sfk))
2693 int mem_quals = cp_type_quals (mem_type) | quals;
2694 if (DECL_MUTABLE_P (field))
2695 mem_quals &= ~TYPE_QUAL_CONST;
2696 argtype = build_stub_type (mem_type, mem_quals, SFK_MOVE_P (sfk));
2698 else
2699 argtype = NULL_TREE;
2701 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
2703 process_subob_fn (rval, sfk, spec_p, trivial_p, deleted_p,
2704 constexpr_p, diag, field, dtor_from_ctor);
2707 /* We didn't find a DMI in this union, now check all the members. */
2708 if (only_dmi_mem == unknown)
2710 only_dmi_mem = no;
2711 goto again;
2715 /* Base walker helper for synthesized_method_walk. Inspect a direct
2716 or virtual base. BINFO is the parent type's binfo. BASE_BINFO is
2717 the base binfo of interests. All other parms are as for
2718 synthesized_method_walk, or its local vars. */
2720 static tree
2721 synthesized_method_base_walk (tree binfo, tree base_binfo,
2722 special_function_kind sfk, tree fnname, int quals,
2723 tree *inheriting_ctor, tree inherited_parms,
2724 int flags, bool diag,
2725 tree *spec_p, bool *trivial_p,
2726 bool *deleted_p, bool *constexpr_p)
2728 bool inherited_binfo = false;
2729 tree argtype = NULL_TREE;
2730 deferring_kind defer = dk_no_deferred;
2732 if (SFK_COPY_P (sfk) || SFK_MOVE_P (sfk))
2733 argtype = build_stub_type (BINFO_TYPE (base_binfo), quals, SFK_MOVE_P (sfk));
2734 else if (inheriting_ctor
2735 && (inherited_binfo
2736 = binfo_inherited_from (binfo, base_binfo, *inheriting_ctor)))
2738 argtype = inherited_parms;
2739 /* Don't check access on the inherited constructor. */
2740 if (flag_new_inheriting_ctors)
2741 defer = dk_deferred;
2743 else if (cxx_dialect >= cxx14 && sfk == sfk_virtual_destructor
2744 && BINFO_VIRTUAL_P (base_binfo)
2745 && ABSTRACT_CLASS_TYPE_P (BINFO_TYPE (binfo)))
2746 /* Don't check access when looking at vbases of abstract class's
2747 virtual destructor. */
2748 defer = dk_no_check;
2750 if (defer != dk_no_deferred)
2751 push_deferring_access_checks (defer);
2752 tree rval = locate_fn_flags (base_binfo, fnname, argtype, flags,
2753 diag ? tf_warning_or_error : tf_none);
2754 if (defer != dk_no_deferred)
2755 pop_deferring_access_checks ();
2757 /* Replace an inherited template with the appropriate specialization. */
2758 if (inherited_binfo && rval
2759 && DECL_P (*inheriting_ctor) && DECL_P (rval)
2760 && DECL_CONTEXT (*inheriting_ctor) == DECL_CONTEXT (rval))
2761 *inheriting_ctor = DECL_CLONED_FUNCTION (rval);
2763 process_subob_fn (rval, sfk, spec_p, trivial_p, deleted_p,
2764 constexpr_p, diag, BINFO_TYPE (base_binfo));
2765 if (SFK_CTOR_P (sfk)
2766 && (!BINFO_VIRTUAL_P (base_binfo)
2767 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))))
2769 /* In a constructor we also need to check the subobject
2770 destructors for cleanup of partially constructed objects. */
2771 tree dtor = locate_fn_flags (base_binfo, complete_dtor_identifier,
2772 NULL_TREE, flags,
2773 diag ? tf_warning_or_error : tf_none);
2774 /* Note that we don't pass down trivial_p; the subobject
2775 destructors don't affect triviality of the constructor. Nor
2776 do they affect constexpr-ness (a constant expression doesn't
2777 throw) or exception-specification (a throw from one of the
2778 dtors would be a double-fault). */
2779 process_subob_fn (dtor, sfk, NULL, NULL, deleted_p, NULL, false,
2780 BINFO_TYPE (base_binfo), /*dtor_from_ctor*/true);
2783 return rval;
2786 /* The caller wants to generate an implicit declaration of SFK for
2787 CTYPE which is const if relevant and CONST_P is set. If SPEC_P,
2788 TRIVIAL_P, DELETED_P or CONSTEXPR_P are non-null, set their
2789 referent appropriately. If DIAG is true, we're either being called
2790 from maybe_explain_implicit_delete to give errors, or if
2791 CONSTEXPR_P is non-null, from explain_invalid_constexpr_fn. */
2793 static void
2794 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
2795 tree *spec_p, bool *trivial_p, bool *deleted_p,
2796 bool *constexpr_p, bool diag,
2797 tree *inheriting_ctor, tree inherited_parms)
2799 tree binfo, base_binfo;
2800 int i;
2802 /* SFK must be exactly one category. */
2803 gcc_checking_assert (SFK_DTOR_P(sfk) + SFK_CTOR_P(sfk)
2804 + SFK_ASSIGN_P(sfk) == 1);
2806 if (spec_p)
2807 *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec);
2809 if (deleted_p)
2811 /* "The closure type associated with a lambda-expression has a deleted
2812 default constructor and a deleted copy assignment operator."
2813 This is diagnosed in maybe_explain_implicit_delete.
2814 In C++20, only lambda-expressions with lambda-captures have those
2815 deleted. */
2816 if (LAMBDA_TYPE_P (ctype)
2817 && (sfk == sfk_constructor || sfk == sfk_copy_assignment)
2818 && (cxx_dialect < cxx20
2819 || LAMBDA_EXPR_CAPTURE_LIST (CLASSTYPE_LAMBDA_EXPR (ctype))
2820 || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE
2821 (CLASSTYPE_LAMBDA_EXPR (ctype)) != CPLD_NONE))
2823 *deleted_p = true;
2824 return;
2827 *deleted_p = false;
2830 bool check_vdtor = false;
2831 tree fnname;
2833 if (SFK_DTOR_P (sfk))
2835 check_vdtor = true;
2836 /* The synthesized method will call base dtors, but check complete
2837 here to avoid having to deal with VTT. */
2838 fnname = complete_dtor_identifier;
2840 else if (SFK_ASSIGN_P (sfk))
2841 fnname = assign_op_identifier;
2842 else
2843 fnname = complete_ctor_identifier;
2845 gcc_assert ((sfk == sfk_inheriting_constructor)
2846 == (inheriting_ctor && *inheriting_ctor != NULL_TREE));
2848 /* If that user-written default constructor would satisfy the
2849 requirements of a constexpr constructor (7.1.5), the
2850 implicitly-defined default constructor is constexpr.
2852 C++20:
2853 The implicitly-defined copy/move assignment operator is constexpr if
2854 - X is a literal type, and
2855 - the assignment operator selected to copy/move each direct base class
2856 subobject is a constexpr function, and
2857 - for each non-static data member of X that is of class type (or array
2858 thereof), the assignment operator selected to copy/move that
2859 member is a constexpr function.
2861 C++23:
2862 The implicitly-defined copy/move assignment operator is constexpr. */
2863 if (constexpr_p)
2864 *constexpr_p = (SFK_CTOR_P (sfk)
2865 || (SFK_ASSIGN_P (sfk) && cxx_dialect >= cxx14)
2866 || (SFK_DTOR_P (sfk) && cxx_dialect >= cxx20));
2868 bool expected_trivial = type_has_trivial_fn (ctype, sfk);
2869 if (trivial_p)
2870 *trivial_p = expected_trivial;
2872 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
2873 class versions and other properties of the type. But a subobject
2874 class can be trivially copyable and yet have overload resolution
2875 choose a template constructor for initialization, depending on
2876 rvalueness and cv-quals. And furthermore, a member in a base might
2877 be trivial but deleted or otherwise not callable. So we can't exit
2878 early in C++0x. The same considerations apply in C++98/03, but
2879 there the definition of triviality does not consider overload
2880 resolution, so a constructor can be trivial even if it would otherwise
2881 call a non-trivial constructor. */
2882 if (expected_trivial
2883 && (!(SFK_COPY_P (sfk) || SFK_MOVE_P (sfk)) || cxx_dialect < cxx11))
2885 if (constexpr_p && sfk == sfk_constructor)
2887 bool cx = trivial_default_constructor_is_constexpr (ctype);
2888 *constexpr_p = cx;
2889 if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
2890 /* A trivial constructor doesn't have any NSDMI. */
2891 inform (input_location, "defaulted default constructor does "
2892 "not initialize any non-static data member");
2894 if (!diag && cxx_dialect < cxx11)
2895 return;
2898 bool push_to_top = maybe_push_to_top_level (TYPE_NAME (ctype));
2899 ++cp_unevaluated_operand;
2900 ++c_inhibit_evaluation_warnings;
2901 push_deferring_access_checks (dk_no_deferred);
2903 tree scope = push_scope (ctype);
2905 int flags = LOOKUP_NORMAL | LOOKUP_SPECULATIVE;
2906 if (sfk != sfk_inheriting_constructor)
2907 flags |= LOOKUP_DEFAULTED;
2909 tsubst_flags_t complain = diag ? tf_warning_or_error : tf_none;
2910 if (diag && spec_p)
2911 /* We're in get_defaulted_eh_spec; we don't actually want any walking
2912 diagnostics, we just want complain set. */
2913 diag = false;
2914 int quals = const_p ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED;
2916 for (binfo = TYPE_BINFO (ctype), i = 0;
2917 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
2919 if (!SFK_ASSIGN_P (sfk) && BINFO_VIRTUAL_P (base_binfo))
2920 /* We'll handle virtual bases below. */
2921 continue;
2923 tree fn = synthesized_method_base_walk (binfo, base_binfo,
2924 sfk, fnname, quals,
2925 inheriting_ctor, inherited_parms,
2926 flags, diag, spec_p, trivial_p,
2927 deleted_p, constexpr_p);
2929 if (diag && SFK_ASSIGN_P (sfk) && SFK_MOVE_P (sfk)
2930 && BINFO_VIRTUAL_P (base_binfo)
2931 && fn && TREE_CODE (fn) == FUNCTION_DECL
2932 && move_fn_p (fn) && !trivial_fn_p (fn)
2933 && vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
2934 warning (OPT_Wvirtual_move_assign,
2935 "defaulted move assignment for %qT calls a non-trivial "
2936 "move assignment operator for virtual base %qT",
2937 ctype, BINFO_TYPE (base_binfo));
2939 if (check_vdtor && type_has_virtual_destructor (BINFO_TYPE (base_binfo)))
2941 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
2942 to have a null fn (no class-specific op delete). */
2943 fn = locate_fn_flags (ctype, ovl_op_identifier (false, DELETE_EXPR),
2944 ptr_type_node, flags, tf_none);
2945 if (fn && fn == error_mark_node)
2947 if (complain & tf_error)
2948 locate_fn_flags (ctype, ovl_op_identifier (false, DELETE_EXPR),
2949 ptr_type_node, flags, complain);
2950 if (deleted_p)
2951 *deleted_p = true;
2953 check_vdtor = false;
2957 vec<tree, va_gc> *vbases = CLASSTYPE_VBASECLASSES (ctype);
2958 if (SFK_ASSIGN_P (sfk))
2959 /* Already examined vbases above. */;
2960 else if (vec_safe_is_empty (vbases))
2961 /* No virtual bases to worry about. */;
2962 else if (ABSTRACT_CLASS_TYPE_P (ctype) && cxx_dialect >= cxx14
2963 /* DR 1658 specifies that vbases of abstract classes are
2964 ignored for both ctors and dtors. Except DR 2336
2965 overrides that skipping when determing the eh-spec of a
2966 virtual destructor. */
2967 && sfk != sfk_virtual_destructor)
2968 /* Vbase cdtors are not relevant. */;
2969 else
2971 if (constexpr_p)
2972 *constexpr_p = false;
2974 FOR_EACH_VEC_ELT (*vbases, i, base_binfo)
2975 synthesized_method_base_walk (binfo, base_binfo, sfk, fnname, quals,
2976 inheriting_ctor, inherited_parms,
2977 flags, diag,
2978 spec_p, trivial_p, deleted_p, constexpr_p);
2981 /* Now handle the non-static data members. */
2982 walk_field_subobs (TYPE_FIELDS (ctype), sfk, fnname, quals,
2983 spec_p, trivial_p, deleted_p, constexpr_p,
2984 diag, flags, complain, /*dtor_from_ctor*/false);
2985 if (SFK_CTOR_P (sfk))
2986 walk_field_subobs (TYPE_FIELDS (ctype), sfk_destructor,
2987 complete_dtor_identifier, TYPE_UNQUALIFIED,
2988 NULL, NULL, deleted_p, NULL,
2989 false, flags, complain, /*dtor_from_ctor*/true);
2991 pop_scope (scope);
2993 pop_deferring_access_checks ();
2994 --cp_unevaluated_operand;
2995 --c_inhibit_evaluation_warnings;
2996 maybe_pop_from_top_level (push_to_top);
2999 /* DECL is a defaulted function whose exception specification is now
3000 needed. Return what it should be. */
3002 tree
3003 get_defaulted_eh_spec (tree decl, tsubst_flags_t complain)
3005 /* For DECL_MAYBE_DELETED this should already have been handled by
3006 synthesize_method. */
3007 gcc_assert (!DECL_MAYBE_DELETED (decl));
3009 if (DECL_CLONED_FUNCTION_P (decl))
3010 decl = DECL_CLONED_FUNCTION (decl);
3011 special_function_kind sfk = special_function_p (decl);
3012 tree ctype = DECL_CONTEXT (decl);
3013 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
3014 tree parm_type = TREE_VALUE (parms);
3015 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
3016 tree spec = empty_except_spec;
3017 bool diag = !DECL_DELETED_FN (decl) && (complain & tf_error);
3018 tree inh = DECL_INHERITED_CTOR (decl);
3019 if (SFK_DTOR_P (sfk) && DECL_VIRTUAL_P (decl))
3020 /* We have to examine virtual bases even if abstract. */
3021 sfk = sfk_virtual_destructor;
3022 bool pushed = false;
3023 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
3024 pushed = push_tinst_level (decl);
3025 synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL,
3026 NULL, diag, &inh, parms);
3027 if (pushed)
3028 pop_tinst_level ();
3029 return spec;
3032 /* DECL is a deleted function. If it's implicitly deleted, explain why and
3033 return true; else return false. */
3035 bool
3036 maybe_explain_implicit_delete (tree decl)
3038 /* If decl is a clone, get the primary variant. */
3039 decl = DECL_ORIGIN (decl);
3040 gcc_assert (DECL_DELETED_FN (decl));
3041 if (DECL_DEFAULTED_FN (decl))
3043 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
3044 static hash_set<tree> *explained;
3046 special_function_kind sfk;
3047 location_t loc;
3048 bool informed;
3049 tree ctype;
3051 if (!explained)
3052 explained = new hash_set<tree>;
3053 if (explained->add (decl))
3054 return true;
3056 sfk = special_function_p (decl);
3057 ctype = DECL_CONTEXT (decl);
3058 loc = input_location;
3059 input_location = DECL_SOURCE_LOCATION (decl);
3061 informed = false;
3062 if (LAMBDA_TYPE_P (ctype))
3064 informed = true;
3065 if (sfk == sfk_constructor)
3066 inform (DECL_SOURCE_LOCATION (decl),
3067 "a lambda closure type has a deleted default constructor");
3068 else if (sfk == sfk_copy_assignment)
3069 inform (DECL_SOURCE_LOCATION (decl),
3070 "a lambda closure type has a deleted copy assignment operator");
3071 else
3072 informed = false;
3074 else if (DECL_ARTIFICIAL (decl)
3075 && (sfk == sfk_copy_assignment || sfk == sfk_copy_constructor)
3076 && classtype_has_move_assign_or_move_ctor_p (ctype, true))
3078 inform (DECL_SOURCE_LOCATION (decl),
3079 "%q#D is implicitly declared as deleted because %qT "
3080 "declares a move constructor or move assignment operator",
3081 decl, ctype);
3082 informed = true;
3084 else if (sfk == sfk_inheriting_constructor)
3086 tree binfo = inherited_ctor_binfo (decl);
3087 if (TREE_CODE (binfo) != TREE_BINFO)
3089 inform (DECL_SOURCE_LOCATION (decl),
3090 "%q#D inherits from multiple base subobjects",
3091 decl);
3092 informed = true;
3095 if (!informed && sfk == sfk_comparison)
3097 inform (DECL_SOURCE_LOCATION (decl),
3098 "%q#D is implicitly deleted because the default "
3099 "definition would be ill-formed:", decl);
3100 build_comparison_op (decl, false, tf_warning_or_error);
3102 else if (!informed)
3104 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
3105 bool const_p = false;
3106 if (parms)
3108 tree parm_type = TREE_VALUE (parms);
3109 const_p = CP_TYPE_CONST_P (non_reference (parm_type));
3111 tree raises = NULL_TREE;
3112 bool deleted_p = false;
3113 tree scope = push_scope (ctype);
3114 tree inh = DECL_INHERITED_CTOR (decl);
3116 synthesized_method_walk (ctype, sfk, const_p,
3117 &raises, NULL, &deleted_p, NULL, false,
3118 &inh, parms);
3119 if (deleted_p)
3121 inform (DECL_SOURCE_LOCATION (decl),
3122 "%q#D is implicitly deleted because the default "
3123 "definition would be ill-formed:", decl);
3124 synthesized_method_walk (ctype, sfk, const_p,
3125 NULL, NULL, &deleted_p, NULL, true,
3126 &inh, parms);
3128 else if (!comp_except_specs
3129 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
3130 raises, ce_normal))
3131 inform (DECL_SOURCE_LOCATION (decl), "%q#F is implicitly "
3132 "deleted because its exception-specification does not "
3133 "match the implicit exception-specification %qX",
3134 decl, raises);
3135 else if (flag_checking)
3136 gcc_unreachable ();
3138 pop_scope (scope);
3141 input_location = loc;
3142 return true;
3144 return false;
3147 /* DECL is a defaulted function which was declared constexpr. Explain why
3148 it can't be constexpr. */
3150 void
3151 explain_implicit_non_constexpr (tree decl)
3153 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
3154 bool const_p = CP_TYPE_CONST_P (non_reference (TREE_VALUE (parms)));
3155 tree inh = DECL_INHERITED_CTOR (decl);
3156 bool dummy;
3157 special_function_kind sfk = special_function_p (decl);
3158 if (sfk == sfk_comparison)
3160 DECL_DECLARED_CONSTEXPR_P (decl) = true;
3161 build_comparison_op (decl, false, tf_warning_or_error);
3162 DECL_DECLARED_CONSTEXPR_P (decl) = false;
3164 else
3165 synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
3166 sfk, const_p,
3167 NULL, NULL, NULL, &dummy, true,
3168 &inh, parms);
3171 /* DECL is an instantiation of an inheriting constructor template. Deduce
3172 the correct exception-specification and deletedness for this particular
3173 specialization. Return true if the deduction succeeds; false otherwise. */
3175 bool
3176 deduce_inheriting_ctor (tree decl)
3178 decl = DECL_ORIGIN (decl);
3179 gcc_assert (DECL_INHERITED_CTOR (decl));
3180 tree spec;
3181 bool trivial, constexpr_, deleted;
3182 tree inh = DECL_INHERITED_CTOR (decl);
3183 synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor,
3184 false, &spec, &trivial, &deleted, &constexpr_,
3185 /*diag*/false,
3186 &inh,
3187 FUNCTION_FIRST_USER_PARMTYPE (decl));
3188 if (spec == error_mark_node)
3189 return false;
3190 if (TREE_CODE (inherited_ctor_binfo (decl)) != TREE_BINFO)
3191 /* Inherited the same constructor from different base subobjects. */
3192 deleted = true;
3193 DECL_DELETED_FN (decl) = deleted;
3194 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
3195 SET_DECL_INHERITED_CTOR (decl, inh);
3197 tree clone;
3198 FOR_EACH_CLONE (clone, decl)
3200 DECL_DELETED_FN (clone) = deleted;
3201 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
3202 SET_DECL_INHERITED_CTOR (clone, inh);
3205 return true;
3208 /* Implicitly declare the special function indicated by KIND, as a
3209 member of TYPE. For copy constructors and assignment operators,
3210 CONST_P indicates whether these functions should take a const
3211 reference argument or a non-const reference.
3212 Returns the FUNCTION_DECL for the implicitly declared function. */
3214 tree
3215 implicitly_declare_fn (special_function_kind kind, tree type,
3216 bool const_p, tree pattern_fn,
3217 tree inherited_parms)
3219 tree fn;
3220 tree parameter_types = void_list_node;
3221 tree return_type;
3222 tree fn_type;
3223 tree raises = empty_except_spec;
3224 tree rhs_parm_type = NULL_TREE;
3225 tree this_parm;
3226 tree name;
3227 HOST_WIDE_INT saved_processing_template_decl;
3228 bool deleted_p = false;
3229 bool constexpr_p = false;
3230 tree inherited_ctor = (kind == sfk_inheriting_constructor
3231 ? pattern_fn : NULL_TREE);
3233 /* Because we create declarations for implicitly declared functions
3234 lazily, we may be creating the declaration for a member of TYPE
3235 while in some completely different context. However, TYPE will
3236 never be a dependent class (because we never want to do lookups
3237 for implicitly defined functions in a dependent class). */
3238 gcc_assert (!dependent_type_p (type));
3240 /* If the member-specification does not explicitly declare any member or
3241 friend named operator==, an == operator function is declared
3242 implicitly for each three-way comparison operator function defined as
3243 defaulted in the member-specification, with the same access and
3244 function-definition and in the same class scope as the respective
3245 three-way comparison operator function, except that the return type is
3246 replaced with bool and the declarator-id is replaced with
3247 operator==.
3249 [Note: Such an implicitly-declared == operator for a class X is
3250 defined as defaulted in the definition of X and has the same
3251 parameter-declaration-clause and trailing requires-clause as the
3252 respective three-way comparison operator. It is declared with friend,
3253 virtual, constexpr, or consteval if the three-way comparison operator
3254 function is so declared. If the three-way comparison operator function
3255 has no noexcept-specifier, the implicitly-declared == operator
3256 function has an implicit exception specification (14.5) that may
3257 differ from the implicit exception specification of the three-way
3258 comparison operator function. --end note] */
3259 if (kind == sfk_comparison)
3261 fn = copy_operator_fn (pattern_fn, EQ_EXPR);
3262 DECL_ARTIFICIAL (fn) = 1;
3263 apply_deduced_return_type (fn, boolean_type_node);
3264 return fn;
3267 /* Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
3268 because we only create clones for constructors and destructors
3269 when not in a template. */
3270 saved_processing_template_decl = processing_template_decl;
3271 processing_template_decl = 0;
3273 type = TYPE_MAIN_VARIANT (type);
3275 if (targetm.cxx.cdtor_returns_this ())
3277 if (kind == sfk_destructor)
3278 /* See comment in check_special_function_return_type. */
3279 return_type = build_pointer_type (void_type_node);
3280 else
3281 return_type = build_pointer_type (type);
3283 else
3284 return_type = void_type_node;
3286 int this_quals = TYPE_UNQUALIFIED;
3287 switch (kind)
3289 case sfk_destructor:
3290 /* Destructor. */
3291 name = dtor_identifier;
3292 break;
3294 case sfk_constructor:
3295 /* Default constructor. */
3296 name = ctor_identifier;
3297 break;
3299 case sfk_copy_constructor:
3300 case sfk_copy_assignment:
3301 case sfk_move_constructor:
3302 case sfk_move_assignment:
3303 case sfk_inheriting_constructor:
3305 if (kind == sfk_copy_assignment
3306 || kind == sfk_move_assignment)
3308 return_type = build_reference_type (type);
3309 name = assign_op_identifier;
3311 else
3312 name = ctor_identifier;
3314 if (kind == sfk_inheriting_constructor)
3315 parameter_types = inherited_parms;
3316 else
3318 if (const_p)
3319 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
3320 else
3321 rhs_parm_type = type;
3322 bool move_p = (kind == sfk_move_assignment
3323 || kind == sfk_move_constructor);
3324 rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
3326 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
3328 break;
3331 default:
3332 gcc_unreachable ();
3335 bool trivial_p = false;
3337 if (inherited_ctor)
3339 /* For an inheriting constructor, just copy these flags from the
3340 inherited constructor until deduce_inheriting_ctor. */
3341 raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor));
3342 deleted_p = DECL_DELETED_FN (inherited_ctor);
3343 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
3345 else if (cxx_dialect >= cxx11)
3347 raises = noexcept_deferred_spec;
3348 synthesized_method_walk (type, kind, const_p, NULL, &trivial_p,
3349 &deleted_p, &constexpr_p, false,
3350 &inherited_ctor, inherited_parms);
3352 else
3353 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
3354 &deleted_p, &constexpr_p, false,
3355 &inherited_ctor, inherited_parms);
3356 /* Don't bother marking a deleted constructor as constexpr. */
3357 if (deleted_p)
3358 constexpr_p = false;
3359 /* A trivial copy/move constructor is also a constexpr constructor,
3360 unless the class has virtual bases (7.1.5p4). */
3361 else if (trivial_p
3362 && cxx_dialect >= cxx11
3363 && (kind == sfk_copy_constructor
3364 || kind == sfk_move_constructor)
3365 && !CLASSTYPE_VBASECLASSES (type))
3366 gcc_assert (constexpr_p);
3368 if (!trivial_p && type_has_trivial_fn (type, kind))
3369 type_set_nontrivial_flag (type, kind);
3371 /* Create the function. */
3372 tree this_type = cp_build_qualified_type (type, this_quals);
3373 fn_type = build_method_type_directly (this_type, return_type,
3374 parameter_types);
3376 if (raises)
3378 if (raises != error_mark_node)
3379 fn_type = build_exception_variant (fn_type, raises);
3380 else
3382 /* Can happen, e.g., in C++98 mode for an ill-formed non-static data
3383 member initializer (c++/89914). Also, in C++98, we might have
3384 failed to deduce RAISES, so try again but complain this time. */
3385 if (cxx_dialect < cxx11)
3386 synthesized_method_walk (type, kind, const_p, &raises, nullptr,
3387 nullptr, nullptr, /*diag=*/true,
3388 &inherited_ctor, inherited_parms);
3389 /* We should have seen an error at this point. */
3390 gcc_assert (seen_error ());
3393 fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
3394 if (kind != sfk_inheriting_constructor)
3395 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
3397 if (IDENTIFIER_OVL_OP_P (name))
3399 const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (name);
3400 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) = op->ovl_op_code;
3402 else if (IDENTIFIER_CTOR_P (name))
3403 DECL_CXX_CONSTRUCTOR_P (fn) = true;
3404 else if (IDENTIFIER_DTOR_P (name))
3405 DECL_CXX_DESTRUCTOR_P (fn) = true;
3406 else
3407 gcc_unreachable ();
3409 SET_DECL_ALIGN (fn, MINIMUM_METHOD_BOUNDARY);
3411 /* Create the explicit arguments. */
3412 if (rhs_parm_type)
3414 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
3415 want its type to be included in the mangled function
3416 name. */
3417 tree decl = cp_build_parm_decl (fn, NULL_TREE, rhs_parm_type);
3418 TREE_READONLY (decl) = 1;
3419 retrofit_lang_decl (decl);
3420 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
3421 DECL_ARGUMENTS (fn) = decl;
3423 else if (kind == sfk_inheriting_constructor)
3425 tree *p = &DECL_ARGUMENTS (fn);
3426 int index = 1;
3427 for (tree parm = inherited_parms; parm && parm != void_list_node;
3428 parm = TREE_CHAIN (parm))
3430 *p = cp_build_parm_decl (fn, NULL_TREE, TREE_VALUE (parm));
3431 retrofit_lang_decl (*p);
3432 DECL_PARM_LEVEL (*p) = 1;
3433 DECL_PARM_INDEX (*p) = index++;
3434 p = &DECL_CHAIN (*p);
3436 SET_DECL_INHERITED_CTOR (fn, inherited_ctor);
3437 DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor);
3438 /* A constructor so declared has the same access as the corresponding
3439 constructor in X. */
3440 TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor);
3441 TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor);
3442 /* Copy constexpr from the inherited constructor even if the
3443 inheriting constructor doesn't satisfy the requirements. */
3444 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
3445 tree inherited_ctor_fn = STRIP_TEMPLATE (inherited_ctor);
3446 /* Also copy any attributes. */
3447 DECL_ATTRIBUTES (fn) = clone_attrs (DECL_ATTRIBUTES (inherited_ctor_fn));
3448 DECL_DISREGARD_INLINE_LIMITS (fn)
3449 = DECL_DISREGARD_INLINE_LIMITS (inherited_ctor_fn);
3452 /* Add the "this" parameter. */
3453 this_parm = build_this_parm (fn, fn_type, this_quals);
3454 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
3455 DECL_ARGUMENTS (fn) = this_parm;
3457 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
3459 DECL_IN_AGGR_P (fn) = 1;
3460 DECL_ARTIFICIAL (fn) = 1;
3461 DECL_DEFAULTED_FN (fn) = 1;
3462 if (cxx_dialect >= cxx11)
3464 DECL_DELETED_FN (fn) = deleted_p;
3465 DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
3467 DECL_EXTERNAL (fn) = true;
3468 DECL_NOT_REALLY_EXTERN (fn) = 1;
3469 DECL_DECLARED_INLINE_P (fn) = 1;
3470 set_linkage_according_to_type (type, fn);
3471 if (TREE_PUBLIC (fn))
3472 DECL_COMDAT (fn) = 1;
3473 rest_of_decl_compilation (fn, namespace_bindings_p (), at_eof);
3474 gcc_assert (!TREE_USED (fn));
3476 /* Propagate constraints from the inherited constructor. */
3477 if (flag_concepts && inherited_ctor)
3478 if (tree orig_ci = get_constraints (inherited_ctor))
3480 tree new_ci = copy_node (orig_ci);
3481 set_constraints (fn, new_ci);
3484 /* Restore PROCESSING_TEMPLATE_DECL. */
3485 processing_template_decl = saved_processing_template_decl;
3487 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
3488 fn = add_inherited_template_parms (fn, inherited_ctor);
3490 /* Warn about calling a non-trivial move assignment in a virtual base. */
3491 if (kind == sfk_move_assignment && !deleted_p && !trivial_p
3492 && CLASSTYPE_VBASECLASSES (type))
3494 location_t loc = input_location;
3495 input_location = DECL_SOURCE_LOCATION (fn);
3496 synthesized_method_walk (type, kind, const_p,
3497 NULL, NULL, NULL, NULL, true,
3498 NULL, NULL_TREE);
3499 input_location = loc;
3502 return fn;
3505 /* Gives any errors about defaulted functions which need to be deferred
3506 until the containing class is complete. */
3508 void
3509 defaulted_late_check (tree fn)
3511 /* Complain about invalid signature for defaulted fn. */
3512 tree ctx = DECL_CONTEXT (fn);
3513 special_function_kind kind = special_function_p (fn);
3515 if (kind == sfk_comparison)
3517 /* If the function was declared constexpr, check that the definition
3518 qualifies. Otherwise we can define the function lazily. */
3519 if (DECL_DECLARED_CONSTEXPR_P (fn) && !DECL_INITIAL (fn))
3521 /* Prevent GC. */
3522 function_depth++;
3523 synthesize_method (fn);
3524 function_depth--;
3526 return;
3529 bool fn_const_p = (copy_fn_p (fn) == 2);
3530 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
3531 NULL, NULL);
3532 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
3534 /* Includes special handling for a default xobj operator. */
3535 auto compare_fn_params = [](tree fn, tree implicit_fn){
3536 tree fn_parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
3537 tree implicit_fn_parms = TYPE_ARG_TYPES (TREE_TYPE (implicit_fn));
3539 if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
3541 tree fn_obj_ref_type = TREE_VALUE (fn_parms);
3542 /* We can't default xobj operators with an xobj parameter that is not
3543 an lvalue reference, even if it would correspond. */
3544 if (!TYPE_REF_P (fn_obj_ref_type)
3545 || TYPE_REF_IS_RVALUE (fn_obj_ref_type)
3546 || !object_parms_correspond (fn, implicit_fn,
3547 DECL_CONTEXT (implicit_fn)))
3548 return false;
3549 /* We just compared the object parameters, skip over them before
3550 passing to compparms. */
3551 fn_parms = TREE_CHAIN (fn_parms);
3552 implicit_fn_parms = TREE_CHAIN (implicit_fn_parms);
3554 return compparms (fn_parms, implicit_fn_parms);
3557 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
3558 TREE_TYPE (TREE_TYPE (implicit_fn)))
3559 || !compare_fn_params (fn, implicit_fn))
3561 error ("defaulted declaration %q+D does not match the "
3562 "expected signature", fn);
3563 inform (DECL_SOURCE_LOCATION (fn),
3564 "expected signature: %qD", implicit_fn);
3567 if (DECL_DELETED_FN (implicit_fn))
3569 DECL_DELETED_FN (fn) = 1;
3570 return;
3573 /* If a function is explicitly defaulted on its first declaration without an
3574 exception-specification, it is implicitly considered to have the same
3575 exception-specification as if it had been implicitly declared. */
3576 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn))
3577 && DECL_DEFAULTED_IN_CLASS_P (fn))
3578 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
3580 if (DECL_DEFAULTED_IN_CLASS_P (fn)
3581 && DECL_DECLARED_CONSTEXPR_P (implicit_fn))
3583 /* Hmm...should we do this for out-of-class too? Should it be OK to
3584 add constexpr later like inline, rather than requiring
3585 declarations to match? */
3586 DECL_DECLARED_CONSTEXPR_P (fn) = true;
3587 if (kind == sfk_constructor)
3588 TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
3591 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
3592 && DECL_DECLARED_CONSTEXPR_P (fn))
3594 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
3596 error ("explicitly defaulted function %q+D cannot be declared "
3597 "%qs because the implicit declaration is not %qs:", fn,
3598 DECL_IMMEDIATE_FUNCTION_P (fn) ? "consteval" : "constexpr",
3599 "constexpr");
3600 explain_implicit_non_constexpr (fn);
3602 DECL_DECLARED_CONSTEXPR_P (fn) = false;
3606 /* Returns true iff FN can be explicitly defaulted, and gives any
3607 errors if defaulting FN is ill-formed. */
3609 bool
3610 defaultable_fn_check (tree fn)
3612 special_function_kind kind = sfk_none;
3614 if (template_parm_scope_p ())
3616 error ("a template cannot be defaulted");
3617 return false;
3620 if (DECL_CONSTRUCTOR_P (fn))
3622 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
3623 kind = sfk_constructor;
3624 else if (copy_fn_p (fn) > 0
3625 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
3626 == void_list_node))
3627 kind = sfk_copy_constructor;
3628 else if (move_fn_p (fn))
3629 kind = sfk_move_constructor;
3631 else if (DECL_DESTRUCTOR_P (fn))
3632 kind = sfk_destructor;
3633 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
3634 && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR))
3636 if (copy_fn_p (fn))
3637 kind = sfk_copy_assignment;
3638 else if (move_fn_p (fn))
3639 kind = sfk_move_assignment;
3641 else if (DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) >= OVL_OP_EQ_EXPR
3642 && DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) <= OVL_OP_SPACESHIP_EXPR)
3644 kind = sfk_comparison;
3645 if (!early_check_defaulted_comparison (fn))
3646 return false;
3649 /* FIXME: We need to check for xobj member functions here to give better
3650 diagnostics for weird cases where unrelated xobj parameters are given.
3651 We just want to do better than 'cannot be defaulted'. */
3653 if (kind == sfk_none)
3655 error ("%qD cannot be defaulted", fn);
3656 return false;
3658 else
3660 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
3661 t && t != void_list_node; t = TREE_CHAIN (t))
3662 if (TREE_PURPOSE (t))
3664 error ("defaulted function %q+D with default argument", fn);
3665 break;
3668 /* Avoid do_warn_unused_parameter warnings. */
3669 for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p))
3670 if (DECL_NAME (p))
3671 suppress_warning (p, OPT_Wunused_parameter);
3673 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
3674 /* Defer checking. */;
3675 else if (!processing_template_decl)
3676 defaulted_late_check (fn);
3678 return true;
3682 /* Add an implicit declaration to TYPE for the kind of function
3683 indicated by SFK. Return the FUNCTION_DECL for the new implicit
3684 declaration. */
3686 tree
3687 lazily_declare_fn (special_function_kind sfk, tree type)
3689 tree fn;
3690 /* Whether or not the argument has a const reference type. */
3691 bool const_p = false;
3693 type = TYPE_MAIN_VARIANT (type);
3695 switch (sfk)
3697 case sfk_constructor:
3698 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
3699 break;
3700 case sfk_copy_constructor:
3701 const_p = TYPE_HAS_CONST_COPY_CTOR (type);
3702 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
3703 break;
3704 case sfk_move_constructor:
3705 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
3706 break;
3707 case sfk_copy_assignment:
3708 const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
3709 CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
3710 break;
3711 case sfk_move_assignment:
3712 CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
3713 break;
3714 case sfk_destructor:
3715 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
3716 break;
3717 default:
3718 gcc_unreachable ();
3721 /* Declare the function. */
3722 fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL);
3724 /* [class.copy]/8 If the class definition declares a move constructor or
3725 move assignment operator, the implicitly declared copy constructor is
3726 defined as deleted.... */
3727 if ((sfk == sfk_copy_assignment || sfk == sfk_copy_constructor)
3728 && cxx_dialect >= cxx11)
3730 if (classtype_has_move_assign_or_move_ctor_p (type, true))
3731 DECL_DELETED_FN (fn) = true;
3732 else if (classtype_has_depr_implicit_copy (type))
3733 /* The implicit definition of a copy constructor as defaulted is
3734 deprecated if the class has a user-declared copy assignment operator
3735 or a user-declared destructor. The implicit definition of a copy
3736 assignment operator as defaulted is deprecated if the class has a
3737 user-declared copy constructor or a user-declared destructor (15.4,
3738 15.8). */
3739 TREE_DEPRECATED (fn) = true;
3742 /* Destructors and assignment operators may be virtual. */
3743 if (sfk == sfk_destructor
3744 || sfk == sfk_move_assignment
3745 || sfk == sfk_copy_assignment)
3746 check_for_override (fn, type);
3748 /* Add it to the class */
3749 bool added = add_method (type, fn, false);
3750 gcc_assert (added || errorcount);
3752 /* Add it to TYPE_FIELDS. */
3753 if (sfk == sfk_destructor
3754 && DECL_VIRTUAL_P (fn))
3755 /* The ABI requires that a virtual destructor go at the end of the
3756 vtable. */
3757 TYPE_FIELDS (type) = chainon (TYPE_FIELDS (type), fn);
3758 else
3760 DECL_CHAIN (fn) = TYPE_FIELDS (type);
3761 TYPE_FIELDS (type) = fn;
3763 /* Propagate TYPE_FIELDS. */
3764 fixup_type_variants (type);
3766 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
3767 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (fn))
3768 /* Create appropriate clones. */
3769 clone_cdtor (fn, /*update_methods=*/true);
3771 /* Classes, structs or unions TYPE marked with hotness attributes propagate
3772 the attribute to all methods. This is typically done in
3773 check_bases_and_members, but we must also inject them here for deferred
3774 lazily-declared functions. */
3775 maybe_propagate_warmth_attributes (fn, type);
3777 return fn;
3780 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
3781 as there are artificial parms in FN. */
3783 tree
3784 skip_artificial_parms_for (const_tree fn, tree list)
3786 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
3787 list = TREE_CHAIN (list);
3788 else
3789 return list;
3791 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
3792 list = TREE_CHAIN (list);
3793 if (DECL_HAS_VTT_PARM_P (fn))
3794 list = TREE_CHAIN (list);
3795 return list;
3798 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
3799 artificial parms in FN. */
3802 num_artificial_parms_for (const_tree fn)
3804 int count = 0;
3806 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
3807 count++;
3808 else
3809 return 0;
3811 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
3812 count++;
3813 if (DECL_HAS_VTT_PARM_P (fn))
3814 count++;
3815 return count;
3819 #include "gt-cp-method.h"