c++: Allow exporting a typedef redeclaration [PR102341]
[official-gcc.git] / gcc / cp / method.cc
bloba70dd5d6adc18227d203ca1fab3e9b123cceb520
1 /* Handle the hair of processing (but not expanding) inline functions.
2 Also manage function and variable name overloading.
3 Copyright (C) 1987-2023 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 (trivial
799 && is_empty_class (current_class_type))
800 /* Don't copy the padding byte; it might not have been allocated
801 if *this is a base subobject. */;
802 else if (trivial)
804 tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
805 finish_expr_stmt (t);
807 else
809 tree fields;
810 int cvquals = cp_type_quals (TREE_TYPE (parm));
811 int i;
812 tree binfo, base_binfo;
814 /* Assign to each of the direct base classes. */
815 for (binfo = TYPE_BINFO (current_class_type), i = 0;
816 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
818 tree converted_parm;
820 /* We must convert PARM directly to the base class
821 explicitly since the base class may be ambiguous. */
822 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
823 tf_warning_or_error);
824 if (move_p)
825 converted_parm = move (converted_parm);
826 /* Call the base class assignment operator. */
827 releasing_vec parmvec (make_tree_vector_single (converted_parm));
828 finish_expr_stmt
829 (build_special_member_call (current_class_ref,
830 assign_op_identifier,
831 &parmvec,
832 base_binfo,
833 flags,
834 tf_warning_or_error));
837 /* Assign to each of the non-static data members. */
838 for (fields = TYPE_FIELDS (current_class_type);
839 fields;
840 fields = DECL_CHAIN (fields))
842 tree comp = current_class_ref;
843 tree init = parm;
844 tree field = fields;
845 tree expr_type;
846 int quals;
848 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
849 continue;
851 expr_type = TREE_TYPE (field);
853 if (CP_TYPE_CONST_P (expr_type))
855 error ("non-static const member %q#D, cannot use default "
856 "assignment operator", field);
857 continue;
859 else if (TYPE_REF_P (expr_type))
861 error ("non-static reference member %q#D, cannot use "
862 "default assignment operator", field);
863 continue;
866 if (DECL_NAME (field))
868 if (VFIELD_NAME_P (DECL_NAME (field)))
869 continue;
871 else if (ANON_AGGR_TYPE_P (expr_type)
872 && TYPE_FIELDS (expr_type) != NULL_TREE)
873 /* Just use the field; anonymous types can't have
874 nontrivial copy ctors or assignment ops or this
875 function would be deleted. */;
876 else
877 continue;
879 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
881 /* Compute the type of init->field */
882 quals = cvquals;
883 if (DECL_MUTABLE_P (field))
884 quals &= ~TYPE_QUAL_CONST;
885 expr_type = cp_build_qualified_type (expr_type, quals);
887 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
888 if (move_p && !TYPE_REF_P (expr_type)
889 /* 'move' breaks bit-fields, and has no effect for scalars. */
890 && !scalarish_type_p (expr_type))
891 init = move (init);
893 if (DECL_NAME (field))
894 init = cp_build_modify_expr (input_location, comp, NOP_EXPR, init,
895 tf_warning_or_error);
896 else
897 init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
898 finish_expr_stmt (init);
901 finish_return_stmt (current_class_ref);
902 finish_compound_stmt (compound_stmt);
905 /* C++20 <compare> comparison category types. */
907 enum comp_cat_tag
909 cc_partial_ordering,
910 cc_weak_ordering,
911 cc_strong_ordering,
912 cc_last
915 /* Names of the comparison categories and their value members, to be indexed by
916 comp_cat_tag enumerators. genericize_spaceship below relies on the ordering
917 of the members. */
919 struct comp_cat_info_t
921 const char *name;
922 const char *members[4];
924 static const comp_cat_info_t comp_cat_info[cc_last]
926 { "partial_ordering", { "equivalent", "greater", "less", "unordered" } },
927 { "weak_ordering", { "equivalent", "greater", "less" } },
928 { "strong_ordering", { "equal", "greater", "less" } }
931 /* A cache of the category types to speed repeated lookups. */
933 static GTY((deletable)) tree comp_cat_cache[cc_last];
935 /* Look up one of the result variables in the comparison category type. */
937 static tree
938 lookup_comparison_result (tree type, const char *name_str,
939 tsubst_flags_t complain = tf_warning_or_error)
941 tree name = get_identifier (name_str);
942 tree decl = lookup_qualified_name (type, name);
943 if (TREE_CODE (decl) != VAR_DECL)
945 if (complain & tf_error)
947 auto_diagnostic_group d;
948 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
949 qualified_name_lookup_error (type, name, decl, input_location);
950 else
951 error ("%qD is not a static data member", decl);
952 inform (input_location, "determining value of %qs", "operator<=>");
954 return error_mark_node;
956 return decl;
959 /* Look up a <compare> comparison category type in std. */
961 static tree
962 lookup_comparison_category (comp_cat_tag tag,
963 tsubst_flags_t complain = tf_warning_or_error)
965 if (tree cached = comp_cat_cache[tag])
966 return cached;
968 tree name = get_identifier (comp_cat_info[tag].name);
969 tree decl = lookup_qualified_name (std_node, name);
970 if (TREE_CODE (decl) != TYPE_DECL)
972 if (complain & tf_error)
974 auto_diagnostic_group d;
975 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
976 qualified_name_lookup_error (std_node, name, decl, input_location);
977 else
978 error ("%qD is not a type", decl);
979 inform (input_location, "forming type of %qs", "operator<=>");
981 return error_mark_node;
983 /* Also make sure we can look up the value members now, since we won't
984 really use them until genericize time. */
985 tree type = TREE_TYPE (decl);
986 for (int i = 0; i < 4; ++i)
988 const char *p = comp_cat_info[tag].members[i];
989 if (!p) break;
990 if (lookup_comparison_result (type, p, complain)
991 == error_mark_node)
992 return error_mark_node;
994 return comp_cat_cache[tag] = type;
997 /* Wrapper that takes the tag rather than the type. */
999 static tree
1000 lookup_comparison_result (comp_cat_tag tag, const char *name_str,
1001 tsubst_flags_t complain = tf_warning_or_error)
1003 tree type = lookup_comparison_category (tag, complain);
1004 return lookup_comparison_result (type, name_str, complain);
1007 /* Wrapper that takes the index into the members array instead of the name. */
1009 static tree
1010 lookup_comparison_result (comp_cat_tag tag, tree type, int idx)
1012 const char *name_str = comp_cat_info[tag].members[idx];
1013 if (!name_str)
1014 return NULL_TREE;
1015 return lookup_comparison_result (type, name_str);
1018 /* Does TYPE correspond to TAG? */
1020 static bool
1021 is_cat (tree type, comp_cat_tag tag)
1023 tree name = TYPE_LINKAGE_IDENTIFIER (type);
1024 return id_equal (name, comp_cat_info[tag].name);
1027 /* Return the comp_cat_tag for TYPE. */
1029 static comp_cat_tag
1030 cat_tag_for (tree type)
1032 if (!CLASS_TYPE_P (type) || !decl_in_std_namespace_p (TYPE_MAIN_DECL (type)))
1033 return cc_last;
1034 for (int i = 0; i < cc_last; ++i)
1036 comp_cat_tag tag = (comp_cat_tag)i;
1037 if (is_cat (type, tag))
1038 return tag;
1040 return cc_last;
1043 /* Return the comparison category tag of a <=> expression with non-class type
1044 OPTYPE. */
1046 static comp_cat_tag
1047 spaceship_comp_cat (tree optype)
1049 if (INTEGRAL_OR_ENUMERATION_TYPE_P (optype) || TYPE_PTROBV_P (optype))
1050 return cc_strong_ordering;
1051 else if (SCALAR_FLOAT_TYPE_P (optype))
1052 return cc_partial_ordering;
1054 /* ??? should vector <=> produce a vector of one of the above? */
1055 gcc_unreachable ();
1058 /* Return the comparison category type of a <=> expression with non-class type
1059 OPTYPE. */
1061 tree
1062 spaceship_type (tree optype, tsubst_flags_t complain)
1064 comp_cat_tag tag = spaceship_comp_cat (optype);
1065 return lookup_comparison_category (tag, complain);
1068 /* Turn <=> with type TYPE and operands OP0 and OP1 into GENERIC.
1069 This is also used by build_comparison_op for fallback to op< and op==
1070 in a defaulted op<=>. */
1072 tree
1073 genericize_spaceship (location_t loc, tree type, tree op0, tree op1)
1075 /* ??? maybe optimize based on knowledge of representation? */
1076 comp_cat_tag tag = cat_tag_for (type);
1078 if (tag == cc_last && is_auto (type))
1080 /* build_comparison_op is checking to see if we want to suggest changing
1081 the op<=> return type from auto to a specific comparison category; any
1082 category will do for now. */
1083 tag = cc_strong_ordering;
1084 type = lookup_comparison_category (tag, tf_none);
1085 if (type == error_mark_node)
1086 return error_mark_node;
1089 gcc_checking_assert (tag < cc_last);
1091 tree r;
1092 bool scalar = SCALAR_TYPE_P (TREE_TYPE (op0));
1093 if (scalar)
1095 op0 = save_expr (op0);
1096 op1 = save_expr (op1);
1099 tree gt = lookup_comparison_result (tag, type, 1);
1101 int flags = LOOKUP_NORMAL;
1102 tsubst_flags_t complain = tf_none;
1103 tree comp;
1105 if (tag == cc_partial_ordering)
1107 /* op0 == op1 ? equivalent : op0 < op1 ? less :
1108 op1 < op0 ? greater : unordered */
1109 tree uo = lookup_comparison_result (tag, type, 3);
1110 if (scalar)
1112 /* For scalars use the low level operations; using build_new_op causes
1113 trouble with constexpr eval in the middle of genericize (100367). */
1114 comp = fold_build2 (LT_EXPR, boolean_type_node, op1, op0);
1115 r = fold_build3 (COND_EXPR, type, comp, gt, uo);
1117 else
1119 comp = build_new_op (loc, LT_EXPR, flags, op1, op0, complain);
1120 r = build_conditional_expr (loc, comp, gt, uo, complain);
1123 else
1124 /* op0 == op1 ? equal : op0 < op1 ? less : greater */
1125 r = gt;
1127 tree lt = lookup_comparison_result (tag, type, 2);
1128 if (scalar)
1130 comp = fold_build2 (LT_EXPR, boolean_type_node, op0, op1);
1131 r = fold_build3 (COND_EXPR, type, comp, lt, r);
1133 else
1135 comp = build_new_op (loc, LT_EXPR, flags, op0, op1, complain);
1136 r = build_conditional_expr (loc, comp, lt, r, complain);
1139 tree eq = lookup_comparison_result (tag, type, 0);
1140 if (scalar)
1142 comp = fold_build2 (EQ_EXPR, boolean_type_node, op0, op1);
1143 r = fold_build3 (COND_EXPR, type, comp, eq, r);
1145 else
1147 comp = build_new_op (loc, EQ_EXPR, flags, op0, op1, complain);
1148 r = build_conditional_expr (loc, comp, eq, r, complain);
1151 return r;
1154 /* Check that the signature of a defaulted comparison operator is
1155 well-formed. */
1157 static bool
1158 early_check_defaulted_comparison (tree fn)
1160 location_t loc = DECL_SOURCE_LOCATION (fn);
1161 tree ctx;
1162 if (DECL_CLASS_SCOPE_P (fn))
1163 ctx = DECL_CONTEXT (fn);
1164 else
1165 ctx = DECL_FRIEND_CONTEXT (fn);
1166 bool ok = true;
1168 if (cxx_dialect < cxx20)
1170 error_at (loc, "defaulted %qD only available with %<-std=c++20%> or "
1171 "%<-std=gnu++20%>", fn);
1172 return false;
1175 if (!DECL_OVERLOADED_OPERATOR_IS (fn, SPACESHIP_EXPR)
1176 && !same_type_p (TREE_TYPE (TREE_TYPE (fn)), boolean_type_node))
1178 diagnostic_t kind = DK_UNSPECIFIED;
1179 int opt = 0;
1180 if (is_auto (TREE_TYPE (fn)))
1181 kind = DK_PEDWARN;
1182 else
1183 kind = DK_ERROR;
1184 emit_diagnostic (kind, loc, opt,
1185 "defaulted %qD must return %<bool%>", fn);
1186 if (kind == DK_ERROR)
1187 ok = false;
1190 bool mem = DECL_NONSTATIC_MEMBER_FUNCTION_P (fn);
1191 if (mem && type_memfn_quals (TREE_TYPE (fn)) != TYPE_QUAL_CONST)
1193 error_at (loc, "defaulted %qD must be %<const%>", fn);
1194 ok = false;
1196 if (mem && type_memfn_rqual (TREE_TYPE (fn)) == REF_QUAL_RVALUE)
1198 error_at (loc, "defaulted %qD must not have %<&&%> ref-qualifier", fn);
1199 ok = false;
1201 tree parmnode = FUNCTION_FIRST_USER_PARMTYPE (fn);
1202 bool saw_byval = false;
1203 bool saw_byref = mem;
1204 bool saw_bad = false;
1205 for (; parmnode != void_list_node; parmnode = TREE_CHAIN (parmnode))
1207 tree parmtype = TREE_VALUE (parmnode);
1208 if (CLASS_TYPE_P (parmtype))
1209 saw_byval = true;
1210 else if (TREE_CODE (parmtype) == REFERENCE_TYPE
1211 && !TYPE_REF_IS_RVALUE (parmtype)
1212 && TYPE_QUALS (TREE_TYPE (parmtype)) == TYPE_QUAL_CONST)
1214 saw_byref = true;
1215 parmtype = TREE_TYPE (parmtype);
1217 else
1218 saw_bad = true;
1220 if (!saw_bad && !ctx)
1222 /* Defaulted outside the class body. */
1223 ctx = TYPE_MAIN_VARIANT (parmtype);
1224 if (!is_friend (ctx, fn))
1225 error_at (loc, "defaulted %qD is not a friend of %qT", fn, ctx);
1227 else if (!same_type_ignoring_top_level_qualifiers_p (parmtype, ctx))
1228 saw_bad = true;
1231 if (saw_bad || (saw_byval && saw_byref))
1233 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1234 error_at (loc, "defaulted member %qD must have parameter type "
1235 "%<const %T&%>", fn, ctx);
1236 else if (saw_bad)
1237 error_at (loc, "defaulted %qD must have parameters of either type "
1238 "%<const %T&%> or %qT", fn, ctx, ctx);
1239 else
1240 error_at (loc, "defaulted %qD must have parameters of either type "
1241 "%<const %T&%> or %qT, not both", fn, ctx, ctx);
1242 ok = false;
1245 /* We still need to deduce deleted/constexpr/noexcept and maybe return. */
1246 DECL_MAYBE_DELETED (fn) = ok;
1248 return ok;
1251 /* Subroutine of build_comparison_op. Given the vec of memberwise
1252 comparisons COMPS, calculate the overall comparison category for
1253 operator<=>. */
1255 static tree
1256 common_comparison_type (vec<tree> &comps)
1258 tree seen[cc_last] = {};
1260 for (unsigned i = 0; i < comps.length(); ++i)
1262 tree comp = comps[i];
1263 if (TREE_CODE (comp) == TREE_LIST)
1264 comp = TREE_VALUE (comp);
1265 tree ctype = TREE_TYPE (comp);
1266 comp_cat_tag tag = cat_tag_for (ctype);
1267 /* build_comparison_op already checked this. */
1268 gcc_checking_assert (tag < cc_last);
1269 seen[tag] = ctype;
1272 /* Otherwise, if at least one T i is std::partial_ordering, U is
1273 std::partial_ordering. */
1274 if (tree t = seen[cc_partial_ordering]) return t;
1276 /* Otherwise, if at least one T i is std::weak_ordering, U is
1277 std::weak_ordering. */
1278 if (tree t = seen[cc_weak_ordering]) return t;
1280 /* Otherwise, U is std::strong_ordering. */
1281 if (tree t = seen[cc_strong_ordering]) return t;
1282 return lookup_comparison_category (cc_strong_ordering);
1285 /* Data structure for build_comparison_op. */
1287 struct comp_info
1289 tree fndecl;
1290 location_t loc;
1291 tsubst_flags_t complain;
1292 tree_code code;
1293 comp_cat_tag retcat;
1294 bool first_time;
1295 bool constexp;
1296 bool was_constexp;
1297 bool noex;
1299 comp_info (tree fndecl, tsubst_flags_t complain)
1300 : fndecl (fndecl), complain (complain)
1302 loc = DECL_SOURCE_LOCATION (fndecl);
1304 first_time = DECL_MAYBE_DELETED (fndecl);
1305 DECL_MAYBE_DELETED (fndecl) = false;
1307 /* Do we want to try to set constexpr? */
1308 was_constexp = DECL_DECLARED_CONSTEXPR_P (fndecl);
1309 constexp = first_time;
1310 if (constexp)
1311 /* Set this for var_in_constexpr_fn. */
1312 DECL_DECLARED_CONSTEXPR_P (fndecl) = true;
1314 /* Do we want to try to set noexcept? */
1315 noex = first_time;
1316 if (noex)
1318 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fndecl));
1319 if (raises && !UNEVALUATED_NOEXCEPT_SPEC_P (raises))
1320 /* There was an explicit exception-specification. */
1321 noex = false;
1325 /* EXPR is an expression built as part of the function body.
1326 Adjust the properties appropriately. */
1327 void check (tree expr)
1329 if (expr == error_mark_node)
1330 DECL_DELETED_FN (fndecl) = true;
1331 if ((constexp || was_constexp)
1332 && !potential_rvalue_constant_expression (expr))
1334 if (was_constexp)
1335 require_potential_rvalue_constant_expression_fncheck (expr);
1336 else
1337 constexp = false;
1339 if (noex && !expr_noexcept_p (expr, tf_none))
1340 noex = false;
1343 ~comp_info ()
1345 if (first_time)
1347 DECL_DECLARED_CONSTEXPR_P (fndecl) = constexp || was_constexp;
1348 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fndecl));
1349 if (!raises || UNEVALUATED_NOEXCEPT_SPEC_P (raises))
1351 raises = noex ? noexcept_true_spec : noexcept_false_spec;
1352 TREE_TYPE (fndecl) = build_exception_variant (TREE_TYPE (fndecl),
1353 raises);
1359 /* Subroutine of build_comparison_op, to compare a single subobject. */
1361 static tree
1362 do_one_comp (location_t loc, const comp_info &info, tree sub, tree lhs, tree rhs)
1364 const tree_code code = info.code;
1365 const tree fndecl = info.fndecl;
1366 const comp_cat_tag retcat = info.retcat;
1367 const tsubst_flags_t complain = info.complain;
1369 tree overload = NULL_TREE;
1370 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
1371 /* If we have an explicit comparison category return type we can fall back
1372 to </=, so don't give an error yet if <=> lookup fails. */
1373 bool tentative = retcat != cc_last;
1374 tree comp = build_new_op (loc, code, flags, lhs, rhs,
1375 NULL_TREE, NULL_TREE, &overload,
1376 tentative ? tf_none : complain);
1378 if (code != SPACESHIP_EXPR)
1379 return comp;
1381 tree rettype = TREE_TYPE (TREE_TYPE (fndecl));
1383 if (comp == error_mark_node)
1385 if (overload == NULL_TREE && (tentative || complain))
1387 /* No viable <=>, try using op< and op==. */
1388 tree lteq = genericize_spaceship (loc, rettype, lhs, rhs);
1389 if (lteq != error_mark_node)
1391 /* We found usable < and ==. */
1392 if (retcat != cc_last)
1393 /* Return type is a comparison category, use them. */
1394 comp = lteq;
1395 else if (complain & tf_error)
1396 /* Return type is auto, suggest changing it. */
1397 inform (info.loc, "changing the return type from %qs "
1398 "to a comparison category type will allow the "
1399 "comparison to use %qs and %qs", "auto",
1400 "operator<", "operator==");
1402 else if (tentative && complain)
1403 /* No usable < and ==, give an error for op<=>. */
1404 build_new_op (loc, code, flags, lhs, rhs, complain);
1406 if (comp == error_mark_node)
1407 return error_mark_node;
1410 if (FNDECL_USED_AUTO (fndecl)
1411 && cat_tag_for (TREE_TYPE (comp)) == cc_last)
1413 /* The operator function is defined as deleted if ... Ri is not a
1414 comparison category type. */
1415 if (complain & tf_error)
1416 inform (loc,
1417 "three-way comparison of %qD has type %qT, not a "
1418 "comparison category type", sub, TREE_TYPE (comp));
1419 return error_mark_node;
1421 else if (!FNDECL_USED_AUTO (fndecl)
1422 && !can_convert (rettype, TREE_TYPE (comp), complain))
1424 if (complain & tf_error)
1425 error_at (loc,
1426 "three-way comparison of %qD has type %qT, which "
1427 "does not convert to %qT",
1428 sub, TREE_TYPE (comp), rettype);
1429 return error_mark_node;
1432 return comp;
1435 /* Build up the definition of a defaulted comparison operator. Unlike other
1436 defaulted functions that use synthesized_method_walk to determine whether
1437 the function is e.g. deleted, for comparisons we use the same code. We try
1438 to use synthesize_method at the earliest opportunity and bail out if the
1439 function ends up being deleted. */
1441 void
1442 build_comparison_op (tree fndecl, bool defining, tsubst_flags_t complain)
1444 comp_info info (fndecl, complain);
1446 if (!defining && !(complain & tf_error) && !DECL_MAYBE_DELETED (fndecl))
1447 return;
1449 int flags = LOOKUP_NORMAL;
1450 const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (DECL_NAME (fndecl));
1451 tree_code code = info.code = op->tree_code;
1453 tree lhs = DECL_ARGUMENTS (fndecl);
1454 tree rhs = DECL_CHAIN (lhs);
1455 if (is_this_parameter (lhs))
1456 lhs = cp_build_fold_indirect_ref (lhs);
1457 else
1458 lhs = convert_from_reference (lhs);
1459 rhs = convert_from_reference (rhs);
1460 tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (lhs));
1461 gcc_assert (!defining || COMPLETE_TYPE_P (ctype));
1463 iloc_sentinel ils (info.loc);
1465 /* A defaulted comparison operator function for class C is defined as
1466 deleted if ... C has variant members. */
1467 if (TREE_CODE (ctype) == UNION_TYPE
1468 && next_aggregate_field (TYPE_FIELDS (ctype)))
1470 if (complain & tf_error)
1471 inform (info.loc, "cannot default compare union %qT", ctype);
1472 DECL_DELETED_FN (fndecl) = true;
1473 return;
1476 tree compound_stmt = NULL_TREE;
1477 if (defining)
1478 compound_stmt = begin_compound_stmt (0);
1479 else
1480 ++cp_unevaluated_operand;
1482 tree rettype = TREE_TYPE (TREE_TYPE (fndecl));
1483 if (code != SPACESHIP_EXPR && is_auto (rettype))
1485 rettype = boolean_type_node;
1486 apply_deduced_return_type (fndecl, rettype);
1489 if (code == EQ_EXPR || code == SPACESHIP_EXPR)
1491 comp_cat_tag &retcat = (info.retcat = cc_last);
1492 if (code == SPACESHIP_EXPR && !FNDECL_USED_AUTO (fndecl))
1493 retcat = cat_tag_for (rettype);
1495 bool bad = false;
1496 auto_vec<tree> comps;
1498 /* Compare the base subobjects. We handle them this way, rather than in
1499 the field loop below, because maybe_instantiate_noexcept might bring
1500 us here before we've built the base fields. */
1501 for (tree base_binfo : BINFO_BASE_BINFOS (TYPE_BINFO (ctype)))
1503 tree lhs_base
1504 = build_base_path (PLUS_EXPR, lhs, base_binfo, 0, complain);
1505 tree rhs_base
1506 = build_base_path (PLUS_EXPR, rhs, base_binfo, 0, complain);
1508 location_t loc = DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (ctype));
1509 tree comp = do_one_comp (loc, info, BINFO_TYPE (base_binfo),
1510 lhs_base, rhs_base);
1511 if (comp == error_mark_node)
1513 bad = true;
1514 continue;
1517 comps.safe_push (comp);
1520 /* Now compare the field subobjects. */
1521 for (tree field = next_aggregate_field (TYPE_FIELDS (ctype));
1522 field;
1523 field = next_aggregate_field (DECL_CHAIN (field)))
1525 if (DECL_VIRTUAL_P (field) || DECL_FIELD_IS_BASE (field))
1526 /* We ignore the vptr, and we already handled bases. */
1527 continue;
1529 tree expr_type = TREE_TYPE (field);
1531 location_t field_loc = DECL_SOURCE_LOCATION (field);
1533 /* A defaulted comparison operator function for class C is defined as
1534 deleted if any non-static data member of C is of reference type or
1535 C has variant members. */
1536 if (TREE_CODE (expr_type) == REFERENCE_TYPE)
1538 if (complain & tf_error)
1539 inform (field_loc, "cannot default compare "
1540 "reference member %qD", field);
1541 bad = true;
1542 continue;
1544 else if (ANON_UNION_TYPE_P (expr_type)
1545 && next_aggregate_field (TYPE_FIELDS (expr_type)))
1547 if (complain & tf_error)
1548 inform (field_loc, "cannot default compare "
1549 "anonymous union member");
1550 bad = true;
1551 continue;
1554 tree lhs_mem = build3_loc (field_loc, COMPONENT_REF, expr_type, lhs,
1555 field, NULL_TREE);
1556 tree rhs_mem = build3_loc (field_loc, COMPONENT_REF, expr_type, rhs,
1557 field, NULL_TREE);
1558 tree loop_indexes = NULL_TREE;
1559 while (TREE_CODE (expr_type) == ARRAY_TYPE)
1561 /* Flexible array member. */
1562 if (TYPE_DOMAIN (expr_type) == NULL_TREE
1563 || TYPE_MAX_VALUE (TYPE_DOMAIN (expr_type)) == NULL_TREE)
1565 if (complain & tf_error)
1566 inform (field_loc, "cannot default compare "
1567 "flexible array member");
1568 bad = true;
1569 break;
1571 tree maxval = TYPE_MAX_VALUE (TYPE_DOMAIN (expr_type));
1572 /* [0] array. No subobjects to compare, just skip it. */
1573 if (integer_all_onesp (maxval))
1574 break;
1575 tree idx;
1576 /* [1] array, no loop needed, just add [0] ARRAY_REF.
1577 Similarly if !defining. */
1578 if (integer_zerop (maxval) || !defining)
1579 idx = size_zero_node;
1580 /* Some other array, will need runtime loop. */
1581 else
1583 idx = force_target_expr (sizetype, maxval, complain);
1584 loop_indexes = tree_cons (idx, NULL_TREE, loop_indexes);
1586 expr_type = TREE_TYPE (expr_type);
1587 lhs_mem = build4_loc (field_loc, ARRAY_REF, expr_type, lhs_mem,
1588 idx, NULL_TREE, NULL_TREE);
1589 rhs_mem = build4_loc (field_loc, ARRAY_REF, expr_type, rhs_mem,
1590 idx, NULL_TREE, NULL_TREE);
1592 if (TREE_CODE (expr_type) == ARRAY_TYPE)
1593 continue;
1595 tree comp = do_one_comp (field_loc, info, field, lhs_mem, rhs_mem);
1596 if (comp == error_mark_node)
1598 bad = true;
1599 continue;
1602 /* Most of the time, comp is the expression that should be evaluated
1603 to compare the two members. If the expression needs to be
1604 evaluated more than once in a loop, it will be a TREE_LIST
1605 instead, whose TREE_VALUE is the expression for one array element,
1606 TREE_PURPOSE is innermost iterator temporary and if the array
1607 is multidimensional, TREE_CHAIN will contain another TREE_LIST
1608 with second innermost iterator in its TREE_PURPOSE and so on. */
1609 if (loop_indexes)
1611 TREE_VALUE (loop_indexes) = comp;
1612 comp = loop_indexes;
1614 comps.safe_push (comp);
1616 if (code == SPACESHIP_EXPR && is_auto (rettype))
1618 rettype = common_comparison_type (comps);
1619 apply_deduced_return_type (fndecl, rettype);
1621 if (bad)
1623 DECL_DELETED_FN (fndecl) = true;
1624 goto out;
1626 for (unsigned i = 0; i < comps.length(); ++i)
1628 tree comp = comps[i];
1629 tree eq, retval = NULL_TREE, if_ = NULL_TREE;
1630 tree loop_indexes = NULL_TREE;
1631 if (defining)
1633 if (TREE_CODE (comp) == TREE_LIST)
1635 loop_indexes = comp;
1636 comp = TREE_VALUE (comp);
1637 loop_indexes = nreverse (loop_indexes);
1638 for (tree loop_index = loop_indexes; loop_index;
1639 loop_index = TREE_CHAIN (loop_index))
1641 tree for_stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
1642 tree idx = TREE_PURPOSE (loop_index);
1643 tree maxval = TARGET_EXPR_INITIAL (idx);
1644 TARGET_EXPR_INITIAL (idx) = size_zero_node;
1645 add_stmt (idx);
1646 finish_init_stmt (for_stmt);
1647 finish_for_cond (build2 (LE_EXPR, boolean_type_node, idx,
1648 maxval), for_stmt, false, 0,
1649 false);
1650 finish_for_expr (cp_build_unary_op (PREINCREMENT_EXPR,
1651 TARGET_EXPR_SLOT (idx),
1652 false, complain),
1653 for_stmt);
1654 /* Store in TREE_VALUE the for_stmt tree, so that we can
1655 later on call finish_for_stmt on it (in the reverse
1656 order). */
1657 TREE_VALUE (loop_index) = for_stmt;
1659 loop_indexes = nreverse (loop_indexes);
1661 if_ = begin_if_stmt ();
1663 /* Spaceship is specified to use !=, but for the comparison category
1664 types, != is equivalent to !(==), so let's use == directly. */
1665 if (code == EQ_EXPR)
1667 /* if (x==y); else return false; */
1668 eq = comp;
1669 retval = boolean_false_node;
1671 else
1673 /* if (auto v = x<=>y, v == 0); else return v; */
1674 if (TREE_CODE (comp) == SPACESHIP_EXPR)
1675 TREE_TYPE (comp) = rettype;
1676 else
1677 comp = build_static_cast (input_location, rettype, comp,
1678 complain);
1679 info.check (comp);
1680 if (defining)
1682 tree var = create_temporary_var (rettype);
1683 DECL_NAME (var) = get_identifier ("retval");
1684 pushdecl (var);
1685 cp_finish_decl (var, comp, false, NULL_TREE, flags);
1686 comp = retval = var;
1688 eq = build_new_op (info.loc, EQ_EXPR, flags, comp,
1689 integer_zero_node, NULL_TREE, NULL_TREE,
1690 NULL, complain);
1692 tree ceq = contextual_conv_bool (eq, complain);
1693 info.check (ceq);
1694 if (defining)
1696 finish_if_stmt_cond (ceq, if_);
1697 finish_then_clause (if_);
1698 begin_else_clause (if_);
1699 finish_return_stmt (retval);
1700 finish_else_clause (if_);
1701 finish_if_stmt (if_);
1702 for (tree loop_index = loop_indexes; loop_index;
1703 loop_index = TREE_CHAIN (loop_index))
1704 finish_for_stmt (TREE_VALUE (loop_index));
1707 if (defining)
1709 tree val;
1710 if (code == EQ_EXPR)
1711 val = boolean_true_node;
1712 else
1714 tree seql = lookup_comparison_result (cc_strong_ordering,
1715 "equal", complain);
1716 val = build_static_cast (input_location, rettype, seql,
1717 complain);
1719 finish_return_stmt (val);
1722 else if (code == NE_EXPR)
1724 tree comp = build_new_op (info.loc, EQ_EXPR, flags, lhs, rhs,
1725 NULL_TREE, NULL_TREE, NULL, complain);
1726 comp = contextual_conv_bool (comp, complain);
1727 info.check (comp);
1728 if (defining)
1730 tree neg = build1 (TRUTH_NOT_EXPR, boolean_type_node, comp);
1731 finish_return_stmt (neg);
1734 else
1736 tree comp = build_new_op (info.loc, SPACESHIP_EXPR, flags, lhs, rhs,
1737 NULL_TREE, NULL_TREE, NULL, complain);
1738 tree comp2 = build_new_op (info.loc, code, flags, comp, integer_zero_node,
1739 NULL_TREE, NULL_TREE, NULL, complain);
1740 info.check (comp2);
1741 if (defining)
1742 finish_return_stmt (comp2);
1745 out:
1746 if (defining)
1747 finish_compound_stmt (compound_stmt);
1748 else
1749 --cp_unevaluated_operand;
1752 /* True iff DECL is an implicitly-declared special member function with no real
1753 source location, so we can use its DECL_SOURCE_LOCATION to remember where we
1754 triggered its synthesis. */
1756 bool
1757 decl_remember_implicit_trigger_p (tree decl)
1759 if (!DECL_ARTIFICIAL (decl))
1760 return false;
1761 special_function_kind sfk = special_function_p (decl);
1762 /* Inherited constructors have the location of their using-declaration, and
1763 operator== has the location of the corresponding operator<=>. */
1764 return (sfk != sfk_inheriting_constructor
1765 && sfk != sfk_comparison);
1768 /* Synthesize FNDECL, a non-static member function. */
1770 void
1771 synthesize_method (tree fndecl)
1773 bool nested = (current_function_decl != NULL_TREE);
1774 tree context = decl_function_context (fndecl);
1775 bool need_body = true;
1776 tree stmt;
1777 location_t save_input_location = input_location;
1778 int error_count = errorcount;
1779 int warning_count = warningcount + werrorcount;
1780 special_function_kind sfk = special_function_p (fndecl);
1782 /* Reset the source location, we might have been previously
1783 deferred, and thus have saved where we were first needed. */
1784 if (decl_remember_implicit_trigger_p (fndecl))
1785 DECL_SOURCE_LOCATION (fndecl)
1786 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
1788 /* If we've been asked to synthesize a clone, just synthesize the
1789 cloned function instead. Doing so will automatically fill in the
1790 body for the clone. */
1791 if (DECL_CLONED_FUNCTION_P (fndecl))
1792 fndecl = DECL_CLONED_FUNCTION (fndecl);
1794 /* We may be in the middle of deferred access check. Disable
1795 it now. */
1796 push_deferring_access_checks (dk_no_deferred);
1798 if (! context)
1799 push_to_top_level ();
1800 else if (nested)
1801 push_function_context ();
1803 input_location = DECL_SOURCE_LOCATION (fndecl);
1805 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
1806 stmt = begin_function_body ();
1808 if (DECL_ASSIGNMENT_OPERATOR_P (fndecl)
1809 && DECL_OVERLOADED_OPERATOR_IS (fndecl, NOP_EXPR))
1811 do_build_copy_assign (fndecl);
1812 need_body = false;
1814 else if (DECL_CONSTRUCTOR_P (fndecl))
1816 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
1817 if (arg_chain != void_list_node)
1818 do_build_copy_constructor (fndecl);
1819 else
1820 finish_mem_initializers (NULL_TREE);
1822 else if (sfk == sfk_comparison)
1824 /* Pass tf_none so the function is just deleted if there's a problem. */
1825 build_comparison_op (fndecl, true, tf_none);
1826 need_body = false;
1829 /* If we haven't yet generated the body of the function, just
1830 generate an empty compound statement. */
1831 if (need_body)
1833 tree compound_stmt;
1834 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
1835 finish_compound_stmt (compound_stmt);
1838 finish_function_body (stmt);
1839 finish_function (/*inline_p=*/false);
1841 if (!DECL_DELETED_FN (fndecl))
1842 expand_or_defer_fn (fndecl);
1844 input_location = save_input_location;
1846 if (! context)
1847 pop_from_top_level ();
1848 else if (nested)
1849 pop_function_context ();
1851 pop_deferring_access_checks ();
1853 if (error_count != errorcount || warning_count != warningcount + werrorcount)
1854 if (DECL_ARTIFICIAL (fndecl))
1855 inform (input_location, "synthesized method %qD first required here",
1856 fndecl);
1859 /* Like synthesize_method, but don't actually synthesize defaulted comparison
1860 methods if their class is still incomplete. Just deduce the return
1861 type in that case. */
1863 void
1864 maybe_synthesize_method (tree fndecl)
1866 if (special_function_p (fndecl) == sfk_comparison)
1868 tree lhs = DECL_ARGUMENTS (fndecl);
1869 if (is_this_parameter (lhs))
1870 lhs = cp_build_fold_indirect_ref (lhs);
1871 else
1872 lhs = convert_from_reference (lhs);
1873 tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (lhs));
1874 if (!COMPLETE_TYPE_P (ctype))
1876 push_deferring_access_checks (dk_no_deferred);
1877 build_comparison_op (fndecl, false, tf_none);
1878 pop_deferring_access_checks ();
1879 return;
1882 return synthesize_method (fndecl);
1885 /* Build a reference to type TYPE with cv-quals QUALS, which is an
1886 rvalue if RVALUE is true. */
1888 static tree
1889 build_stub_type (tree type, int quals, bool rvalue)
1891 tree argtype = cp_build_qualified_type (type, quals);
1892 return cp_build_reference_type (argtype, rvalue);
1895 /* Build a dummy glvalue from dereferencing a dummy reference of type
1896 REFTYPE. */
1898 tree
1899 build_stub_object (tree reftype)
1901 if (!TYPE_REF_P (reftype))
1902 reftype = cp_build_reference_type (reftype, /*rval*/true);
1903 tree stub = build1 (CONVERT_EXPR, reftype, integer_one_node);
1904 return convert_from_reference (stub);
1907 /* Build a std::declval<TYPE>() expression and return it. */
1909 tree
1910 build_trait_object (tree type)
1912 /* TYPE can't be a function with cv-/ref-qualifiers: std::declval is
1913 defined as
1915 template<class T>
1916 typename std::add_rvalue_reference<T>::type declval() noexcept;
1918 and std::add_rvalue_reference yields T when T is a function with
1919 cv- or ref-qualifiers, making the definition ill-formed. */
1920 if (FUNC_OR_METHOD_TYPE_P (type)
1921 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
1922 || type_memfn_rqual (type) != REF_QUAL_NONE))
1923 return error_mark_node;
1925 return build_stub_object (type);
1928 /* Determine which function will be called when looking up NAME in TYPE,
1929 called with a single ARGTYPE argument, or no argument if ARGTYPE is
1930 null. FLAGS and COMPLAIN are as for build_new_method_call.
1932 Returns a FUNCTION_DECL if all is well.
1933 Returns NULL_TREE if overload resolution failed.
1934 Returns error_mark_node if the chosen function cannot be called. */
1936 static tree
1937 locate_fn_flags (tree type, tree name, tree argtype, int flags,
1938 tsubst_flags_t complain)
1940 tree ob, fn, fns, binfo, rval;
1942 if (TYPE_P (type))
1943 binfo = TYPE_BINFO (type);
1944 else
1946 binfo = type;
1947 type = BINFO_TYPE (binfo);
1950 ob = build_stub_object (cp_build_reference_type (type, false));
1951 releasing_vec args;
1952 if (argtype)
1954 if (TREE_CODE (argtype) == TREE_LIST)
1956 for (tree elt = argtype; elt && elt != void_list_node;
1957 elt = TREE_CHAIN (elt))
1959 tree type = TREE_VALUE (elt);
1960 tree arg = build_stub_object (type);
1961 vec_safe_push (args, arg);
1964 else
1966 tree arg = build_stub_object (argtype);
1967 args->quick_push (arg);
1971 fns = lookup_fnfields (binfo, name, 0, complain);
1972 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
1974 if (fn && rval == error_mark_node)
1975 return rval;
1976 else
1977 return fn;
1980 /* Locate the dtor of TYPE. */
1982 tree
1983 get_dtor (tree type, tsubst_flags_t complain)
1985 tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
1986 LOOKUP_NORMAL, complain);
1987 if (fn == error_mark_node)
1988 return NULL_TREE;
1989 return fn;
1992 /* Locate the default ctor of TYPE. */
1994 tree
1995 locate_ctor (tree type)
1997 tree fn;
1999 push_deferring_access_checks (dk_no_check);
2000 fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
2001 LOOKUP_SPECULATIVE, tf_none);
2002 pop_deferring_access_checks ();
2003 if (fn == error_mark_node)
2004 return NULL_TREE;
2005 return fn;
2008 /* Likewise, but give any appropriate errors. */
2010 tree
2011 get_default_ctor (tree type)
2013 tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
2014 LOOKUP_NORMAL, tf_warning_or_error);
2015 if (fn == error_mark_node)
2016 return NULL_TREE;
2017 return fn;
2020 /* Locate the copy ctor of TYPE. */
2022 tree
2023 get_copy_ctor (tree type, tsubst_flags_t complain)
2025 int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
2026 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
2027 tree argtype = build_stub_type (type, quals, false);
2028 tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
2029 LOOKUP_NORMAL, complain);
2030 if (fn == error_mark_node)
2031 return NULL_TREE;
2032 return fn;
2035 /* Locate the copy assignment operator of TYPE. */
2037 tree
2038 get_copy_assign (tree type)
2040 int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
2041 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
2042 tree argtype = build_stub_type (type, quals, false);
2043 tree fn = locate_fn_flags (type, assign_op_identifier, argtype,
2044 LOOKUP_NORMAL, tf_warning_or_error);
2045 if (fn == error_mark_node)
2046 return NULL_TREE;
2047 return fn;
2050 /* walk_tree helper function for is_trivially_xible. If *TP is a call,
2051 return it if it calls something other than a trivial special member
2052 function. */
2054 static tree
2055 check_nontriv (tree *tp, int *, void *)
2057 tree fn = cp_get_callee (*tp);
2058 if (fn == NULL_TREE)
2059 return NULL_TREE;
2061 if (TREE_CODE (fn) == ADDR_EXPR)
2062 fn = TREE_OPERAND (fn, 0);
2064 if (TREE_CODE (fn) != FUNCTION_DECL
2065 || !trivial_fn_p (fn))
2066 return fn;
2067 return NULL_TREE;
2070 /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */
2072 static tree
2073 assignable_expr (tree to, tree from)
2075 cp_unevaluated cp_uneval_guard;
2076 to = build_trait_object (to);
2077 from = build_trait_object (from);
2078 tree r = cp_build_modify_expr (input_location, to, NOP_EXPR, from, tf_none);
2079 return r;
2082 /* The predicate condition for a template specialization
2083 is_constructible<T, Args...> shall be satisfied if and only if the
2084 following variable definition would be well-formed for some invented
2085 variable t: T t(create<Args>()...);
2087 Return something equivalent in well-formedness and triviality. */
2089 static tree
2090 constructible_expr (tree to, tree from)
2092 tree expr;
2093 cp_unevaluated cp_uneval_guard;
2094 if (CLASS_TYPE_P (to))
2096 tree ctype = to;
2097 vec<tree, va_gc> *args = NULL;
2098 if (!TYPE_REF_P (to))
2099 to = cp_build_reference_type (to, /*rval*/false);
2100 tree ob = build_stub_object (to);
2101 vec_alloc (args, TREE_VEC_LENGTH (from));
2102 for (tree arg : tree_vec_range (from))
2103 args->quick_push (build_stub_object (arg));
2104 expr = build_special_member_call (ob, complete_ctor_identifier, &args,
2105 ctype, LOOKUP_NORMAL, tf_none);
2106 if (expr == error_mark_node)
2107 return error_mark_node;
2108 /* The current state of the standard vis-a-vis LWG 2116 is that
2109 is_*constructible involves destruction as well. */
2110 if (type_build_dtor_call (ctype))
2112 tree dtor = build_special_member_call (ob, complete_dtor_identifier,
2113 NULL, ctype, LOOKUP_NORMAL,
2114 tf_none);
2115 if (dtor == error_mark_node)
2116 return error_mark_node;
2117 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
2118 expr = build2 (COMPOUND_EXPR, void_type_node, expr, dtor);
2121 else
2123 const int len = TREE_VEC_LENGTH (from);
2124 if (len == 0)
2125 return build_value_init (strip_array_types (to), tf_none);
2126 if (len > 1)
2128 if (cxx_dialect < cxx20)
2129 /* Too many initializers. */
2130 return error_mark_node;
2132 /* In C++20 this is well-formed:
2133 using T = int[2];
2134 T t(1, 2);
2135 which means that std::is_constructible_v<int[2], int, int>
2136 should be true. */
2137 vec<constructor_elt, va_gc> *v;
2138 vec_alloc (v, len);
2139 for (tree arg : tree_vec_range (from))
2141 tree stub = build_stub_object (arg);
2142 constructor_elt elt = { NULL_TREE, stub };
2143 v->quick_push (elt);
2145 from = build_constructor (init_list_type_node, v);
2146 CONSTRUCTOR_IS_DIRECT_INIT (from) = true;
2147 CONSTRUCTOR_IS_PAREN_INIT (from) = true;
2149 else
2150 from = build_stub_object (TREE_VEC_ELT (from, 0));
2151 expr = perform_direct_initialization_if_possible (to, from,
2152 /*cast*/false,
2153 tf_none);
2154 /* If t(e) didn't work, maybe t{e} will. */
2155 if (expr == NULL_TREE
2156 && len == 1
2157 && cxx_dialect >= cxx20)
2159 from = build_constructor_single (init_list_type_node, NULL_TREE,
2160 from);
2161 CONSTRUCTOR_IS_DIRECT_INIT (from) = true;
2162 CONSTRUCTOR_IS_PAREN_INIT (from) = true;
2163 expr = perform_direct_initialization_if_possible (to, from,
2164 /*cast*/false,
2165 tf_none);
2168 return expr;
2171 /* Returns a tree iff TO is assignable (if CODE is MODIFY_EXPR) or
2172 constructible (otherwise) from FROM, which is a single type for
2173 assignment or a list of types for construction. */
2175 static tree
2176 is_xible_helper (enum tree_code code, tree to, tree from, bool trivial)
2178 to = complete_type (to);
2179 deferring_access_check_sentinel acs (dk_no_deferred);
2180 if (VOID_TYPE_P (to) || ABSTRACT_CLASS_TYPE_P (to)
2181 || (from && FUNC_OR_METHOD_TYPE_P (from)
2182 && (TYPE_READONLY (from) || FUNCTION_REF_QUALIFIED (from))))
2183 return error_mark_node;
2184 tree expr;
2185 if (code == MODIFY_EXPR)
2186 expr = assignable_expr (to, from);
2187 else if (trivial && TREE_VEC_LENGTH (from) > 1
2188 && cxx_dialect < cxx20)
2189 return error_mark_node; // only 0- and 1-argument ctors can be trivial
2190 // before C++20 aggregate paren init
2191 else if (TREE_CODE (to) == ARRAY_TYPE && !TYPE_DOMAIN (to))
2192 return error_mark_node; // can't construct an array of unknown bound
2193 else
2194 expr = constructible_expr (to, from);
2195 return expr;
2198 /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or
2199 constructible (otherwise) from FROM, which is a single type for
2200 assignment or a list of types for construction. */
2202 bool
2203 is_trivially_xible (enum tree_code code, tree to, tree from)
2205 tree expr = is_xible_helper (code, to, from, /*trivial*/true);
2206 if (expr == NULL_TREE || expr == error_mark_node)
2207 return false;
2208 tree nt = cp_walk_tree_without_duplicates (&expr, check_nontriv, NULL);
2209 return !nt;
2212 /* Returns true iff TO is nothrow assignable (if CODE is MODIFY_EXPR) or
2213 constructible (otherwise) from FROM, which is a single type for
2214 assignment or a list of types for construction. */
2216 bool
2217 is_nothrow_xible (enum tree_code code, tree to, tree from)
2219 tree expr = is_xible_helper (code, to, from, /*trivial*/false);
2220 if (expr == NULL_TREE || expr == error_mark_node)
2221 return false;
2222 return expr_noexcept_p (expr, tf_none);
2225 /* Returns true iff TO is assignable (if CODE is MODIFY_EXPR) or
2226 constructible (otherwise) from FROM, which is a single type for
2227 assignment or a list of types for construction. */
2229 bool
2230 is_xible (enum tree_code code, tree to, tree from)
2232 tree expr = is_xible_helper (code, to, from, /*trivial*/false);
2233 if (expr == error_mark_node)
2234 return false;
2235 return !!expr;
2238 /* Return true iff conjunction_v<is_reference<T>, is_constructible<T, U>> is
2239 true, and the initialization
2240 T t(VAL<U>); // DIRECT_INIT_P
2242 T t = VAL<U>; // !DIRECT_INIT_P
2243 binds t to a temporary object whose lifetime is extended.
2244 VAL<T> is defined in [meta.unary.prop]:
2245 -- If T is a reference or function type, VAL<T> is an expression with the
2246 same type and value category as declval<T>().
2247 -- Otherwise, VAL<T> is a prvalue that initially has type T. */
2249 bool
2250 ref_xes_from_temporary (tree to, tree from, bool direct_init_p)
2252 /* Check is_reference<T>. */
2253 if (!TYPE_REF_P (to))
2254 return false;
2255 /* We don't check is_constructible<T, U>: if T isn't constructible
2256 from U, we won't be able to create a conversion. */
2257 tree val = build_trait_object (from);
2258 if (val == error_mark_node)
2259 return false;
2260 if (!TYPE_REF_P (from) && TREE_CODE (from) != FUNCTION_TYPE)
2261 val = CLASS_TYPE_P (from) ? force_rvalue (val, tf_none) : rvalue (val);
2262 return ref_conv_binds_to_temporary (to, val, direct_init_p).is_true ();
2265 /* Worker for is_{,nothrow_}convertible. Attempt to perform an implicit
2266 conversion from FROM to TO and return the result. */
2268 static tree
2269 is_convertible_helper (tree from, tree to)
2271 if (VOID_TYPE_P (from) && VOID_TYPE_P (to))
2272 return integer_one_node;
2273 cp_unevaluated u;
2274 tree expr = build_trait_object (from);
2275 /* std::is_{,nothrow_}convertible test whether the imaginary function
2276 definition
2278 To test() { return std::declval<From>(); }
2280 is well-formed. A function can't return a function. */
2281 if (FUNC_OR_METHOD_TYPE_P (to) || expr == error_mark_node)
2282 return error_mark_node;
2283 deferring_access_check_sentinel acs (dk_no_deferred);
2284 return perform_implicit_conversion (to, expr, tf_none);
2287 /* Return true if FROM can be converted to TO using implicit conversions,
2288 or both FROM and TO are possibly cv-qualified void. NB: This doesn't
2289 implement the "Access checks are performed as if from a context unrelated
2290 to either type" restriction. */
2292 bool
2293 is_convertible (tree from, tree to)
2295 tree expr = is_convertible_helper (from, to);
2296 if (expr == error_mark_node)
2297 return false;
2298 return !!expr;
2301 /* Like is_convertible, but the conversion is also noexcept. */
2303 bool
2304 is_nothrow_convertible (tree from, tree to)
2306 tree expr = is_convertible_helper (from, to);
2307 if (expr == NULL_TREE || expr == error_mark_node)
2308 return false;
2309 return expr_noexcept_p (expr, tf_none);
2312 /* Categorize various special_function_kinds. */
2313 #define SFK_CTOR_P(sfk) \
2314 ((sfk) >= sfk_constructor && (sfk) <= sfk_move_constructor)
2315 #define SFK_DTOR_P(sfk) \
2316 ((sfk) == sfk_destructor || (sfk) == sfk_virtual_destructor)
2317 #define SFK_ASSIGN_P(sfk) \
2318 ((sfk) == sfk_copy_assignment || (sfk) == sfk_move_assignment)
2319 #define SFK_COPY_P(sfk) \
2320 ((sfk) == sfk_copy_constructor || (sfk) == sfk_copy_assignment)
2321 #define SFK_MOVE_P(sfk) \
2322 ((sfk) == sfk_move_constructor || (sfk) == sfk_move_assignment)
2324 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
2325 DELETED_P or give an error message MSG with argument ARG. */
2327 static void
2328 process_subob_fn (tree fn, special_function_kind sfk, tree *spec_p,
2329 bool *trivial_p, bool *deleted_p, bool *constexpr_p,
2330 bool diag, tree arg, bool dtor_from_ctor = false)
2332 if (!fn || fn == error_mark_node)
2334 if (deleted_p)
2335 *deleted_p = true;
2336 return;
2339 if (spec_p)
2341 if (!maybe_instantiate_noexcept (fn))
2342 *spec_p = error_mark_node;
2343 else
2345 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2346 *spec_p = merge_exception_specifiers (*spec_p, raises);
2350 if (!trivial_fn_p (fn) && !dtor_from_ctor)
2352 if (trivial_p)
2353 *trivial_p = false;
2354 if (TREE_CODE (arg) == FIELD_DECL
2355 && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
2357 if (deleted_p)
2358 *deleted_p = true;
2359 if (diag)
2360 error ("union member %q+D with non-trivial %qD", arg, fn);
2364 if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
2366 *constexpr_p = false;
2367 if (diag)
2369 inform (DECL_SOURCE_LOCATION (fn),
2370 SFK_DTOR_P (sfk)
2371 ? G_("defaulted destructor calls non-%<constexpr%> %qD")
2372 : G_("defaulted constructor calls non-%<constexpr%> %qD"),
2373 fn);
2374 explain_invalid_constexpr_fn (fn);
2379 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
2380 aggregates. If DTOR_FROM_CTOR is true, we're walking subobject destructors
2381 called from a synthesized constructor, in which case we don't consider
2382 the triviality of the subobject destructor. */
2384 static void
2385 walk_field_subobs (tree fields, special_function_kind sfk, tree fnname,
2386 int quals, tree *spec_p, bool *trivial_p,
2387 bool *deleted_p, bool *constexpr_p,
2388 bool diag, int flags, tsubst_flags_t complain,
2389 bool dtor_from_ctor)
2391 if (!fields)
2392 return;
2394 tree ctx = DECL_CONTEXT (fields);
2396 /* CWG2084: A defaulted default ctor for a union with a DMI only initializes
2397 that member, so don't check other members. */
2398 enum { unknown, no, yes }
2399 only_dmi_mem = (sfk == sfk_constructor && TREE_CODE (ctx) == UNION_TYPE
2400 ? unknown : no);
2402 again:
2403 for (tree field = fields; field; field = DECL_CHAIN (field))
2405 tree mem_type, argtype, rval;
2407 if (TREE_CODE (field) != FIELD_DECL
2408 || DECL_ARTIFICIAL (field)
2409 || DECL_UNNAMED_BIT_FIELD (field))
2410 continue;
2412 /* Variant members only affect deletedness. In particular, they don't
2413 affect the exception-specification of a user-provided destructor,
2414 which we're figuring out via get_defaulted_eh_spec. So if we aren't
2415 asking if this is deleted, don't even look up the function; we don't
2416 want an error about a deleted function we aren't actually calling. */
2417 if (sfk == sfk_destructor && deleted_p == NULL
2418 && TREE_CODE (ctx) == UNION_TYPE)
2419 break;
2421 if (only_dmi_mem != no)
2423 if (DECL_INITIAL (field))
2424 only_dmi_mem = yes;
2425 else
2426 /* Don't check this until we know there's no DMI. */
2427 continue;
2430 mem_type = strip_array_types (TREE_TYPE (field));
2431 if (SFK_ASSIGN_P (sfk))
2433 bool bad = true;
2434 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
2436 if (diag)
2437 error ("non-static const member %q#D, cannot use default "
2438 "assignment operator", field);
2440 else if (TYPE_REF_P (mem_type))
2442 if (diag)
2443 error ("non-static reference member %q#D, cannot use "
2444 "default assignment operator", field);
2446 else
2447 bad = false;
2449 if (bad && deleted_p)
2450 *deleted_p = true;
2452 else if (sfk == sfk_constructor || sfk == sfk_inheriting_constructor)
2454 bool bad;
2456 if (DECL_INITIAL (field))
2458 if (diag && DECL_INITIAL (field) == error_mark_node)
2459 inform (DECL_SOURCE_LOCATION (field),
2460 "initializer for %q#D is invalid", field);
2461 if (trivial_p)
2462 *trivial_p = false;
2463 /* Core 1351: If the field has an NSDMI that could throw, the
2464 default constructor is noexcept(false). */
2465 if (spec_p)
2467 tree nsdmi = get_nsdmi (field, /*ctor*/false, complain);
2468 if (nsdmi == error_mark_node)
2469 *spec_p = error_mark_node;
2470 else if (*spec_p != error_mark_node
2471 && !expr_noexcept_p (nsdmi, tf_none))
2472 *spec_p = noexcept_false_spec;
2474 /* Don't do the normal processing. */
2475 continue;
2478 bad = false;
2479 if (CP_TYPE_CONST_P (mem_type)
2480 && default_init_uninitialized_part (mem_type))
2482 if (diag)
2484 error ("uninitialized const member in %q#T",
2485 current_class_type);
2486 inform (DECL_SOURCE_LOCATION (field),
2487 "%q#D should be initialized", field);
2489 bad = true;
2491 else if (TYPE_REF_P (mem_type))
2493 if (diag)
2495 error ("uninitialized reference member in %q#T",
2496 current_class_type);
2497 inform (DECL_SOURCE_LOCATION (field),
2498 "%q#D should be initialized", field);
2500 bad = true;
2503 if (bad && deleted_p)
2504 *deleted_p = true;
2506 /* Before C++20, for an implicitly-defined default constructor to
2507 be constexpr, every member must have a user-provided default
2508 constructor or an explicit initializer. */
2509 if (constexpr_p
2510 && cxx_dialect < cxx20
2511 && !CLASS_TYPE_P (mem_type)
2512 && TREE_CODE (ctx) != UNION_TYPE)
2514 *constexpr_p = false;
2515 if (diag)
2516 inform (DECL_SOURCE_LOCATION (field),
2517 "defaulted default constructor does not "
2518 "initialize %q#D", field);
2521 else if (sfk == sfk_copy_constructor)
2523 /* 12.8p11b5 */
2524 if (TYPE_REF_P (mem_type)
2525 && TYPE_REF_IS_RVALUE (mem_type))
2527 if (diag)
2528 error ("copying non-static data member %q#D of rvalue "
2529 "reference type", field);
2530 if (deleted_p)
2531 *deleted_p = true;
2535 if (!CLASS_TYPE_P (mem_type))
2536 continue;
2538 if (ANON_AGGR_TYPE_P (mem_type))
2540 walk_field_subobs (TYPE_FIELDS (mem_type), sfk, fnname, quals,
2541 spec_p, trivial_p, deleted_p, constexpr_p,
2542 diag, flags, complain, dtor_from_ctor);
2543 continue;
2546 if (SFK_COPY_P (sfk) || SFK_MOVE_P (sfk))
2548 int mem_quals = cp_type_quals (mem_type) | quals;
2549 if (DECL_MUTABLE_P (field))
2550 mem_quals &= ~TYPE_QUAL_CONST;
2551 argtype = build_stub_type (mem_type, mem_quals, SFK_MOVE_P (sfk));
2553 else
2554 argtype = NULL_TREE;
2556 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
2558 process_subob_fn (rval, sfk, spec_p, trivial_p, deleted_p,
2559 constexpr_p, diag, field, dtor_from_ctor);
2562 /* We didn't find a DMI in this union, now check all the members. */
2563 if (only_dmi_mem == unknown)
2565 only_dmi_mem = no;
2566 goto again;
2570 /* Base walker helper for synthesized_method_walk. Inspect a direct
2571 or virtual base. BINFO is the parent type's binfo. BASE_BINFO is
2572 the base binfo of interests. All other parms are as for
2573 synthesized_method_walk, or its local vars. */
2575 static tree
2576 synthesized_method_base_walk (tree binfo, tree base_binfo,
2577 special_function_kind sfk, tree fnname, int quals,
2578 tree *inheriting_ctor, tree inherited_parms,
2579 int flags, bool diag,
2580 tree *spec_p, bool *trivial_p,
2581 bool *deleted_p, bool *constexpr_p)
2583 bool inherited_binfo = false;
2584 tree argtype = NULL_TREE;
2585 deferring_kind defer = dk_no_deferred;
2587 if (SFK_COPY_P (sfk) || SFK_MOVE_P (sfk))
2588 argtype = build_stub_type (BINFO_TYPE (base_binfo), quals, SFK_MOVE_P (sfk));
2589 else if (inheriting_ctor
2590 && (inherited_binfo
2591 = binfo_inherited_from (binfo, base_binfo, *inheriting_ctor)))
2593 argtype = inherited_parms;
2594 /* Don't check access on the inherited constructor. */
2595 if (flag_new_inheriting_ctors)
2596 defer = dk_deferred;
2598 else if (cxx_dialect >= cxx14 && sfk == sfk_virtual_destructor
2599 && BINFO_VIRTUAL_P (base_binfo)
2600 && ABSTRACT_CLASS_TYPE_P (BINFO_TYPE (binfo)))
2601 /* Don't check access when looking at vbases of abstract class's
2602 virtual destructor. */
2603 defer = dk_no_check;
2605 if (defer != dk_no_deferred)
2606 push_deferring_access_checks (defer);
2607 tree rval = locate_fn_flags (base_binfo, fnname, argtype, flags,
2608 diag ? tf_warning_or_error : tf_none);
2609 if (defer != dk_no_deferred)
2610 pop_deferring_access_checks ();
2612 /* Replace an inherited template with the appropriate specialization. */
2613 if (inherited_binfo && rval
2614 && DECL_P (*inheriting_ctor) && DECL_P (rval)
2615 && DECL_CONTEXT (*inheriting_ctor) == DECL_CONTEXT (rval))
2616 *inheriting_ctor = DECL_CLONED_FUNCTION (rval);
2618 process_subob_fn (rval, sfk, spec_p, trivial_p, deleted_p,
2619 constexpr_p, diag, BINFO_TYPE (base_binfo));
2620 if (SFK_CTOR_P (sfk)
2621 && (!BINFO_VIRTUAL_P (base_binfo)
2622 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))))
2624 /* In a constructor we also need to check the subobject
2625 destructors for cleanup of partially constructed objects. */
2626 tree dtor = locate_fn_flags (base_binfo, complete_dtor_identifier,
2627 NULL_TREE, flags,
2628 diag ? tf_warning_or_error : tf_none);
2629 /* Note that we don't pass down trivial_p; the subobject
2630 destructors don't affect triviality of the constructor. Nor
2631 do they affect constexpr-ness (a constant expression doesn't
2632 throw) or exception-specification (a throw from one of the
2633 dtors would be a double-fault). */
2634 process_subob_fn (dtor, sfk, NULL, NULL, deleted_p, NULL, false,
2635 BINFO_TYPE (base_binfo), /*dtor_from_ctor*/true);
2638 return rval;
2641 /* The caller wants to generate an implicit declaration of SFK for
2642 CTYPE which is const if relevant and CONST_P is set. If SPEC_P,
2643 TRIVIAL_P, DELETED_P or CONSTEXPR_P are non-null, set their
2644 referent appropriately. If DIAG is true, we're either being called
2645 from maybe_explain_implicit_delete to give errors, or if
2646 CONSTEXPR_P is non-null, from explain_invalid_constexpr_fn. */
2648 static void
2649 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
2650 tree *spec_p, bool *trivial_p, bool *deleted_p,
2651 bool *constexpr_p, bool diag,
2652 tree *inheriting_ctor, tree inherited_parms)
2654 tree binfo, base_binfo;
2655 int i;
2657 /* SFK must be exactly one category. */
2658 gcc_checking_assert (SFK_DTOR_P(sfk) + SFK_CTOR_P(sfk)
2659 + SFK_ASSIGN_P(sfk) == 1);
2661 if (spec_p)
2662 *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec);
2664 if (deleted_p)
2666 /* "The closure type associated with a lambda-expression has a deleted
2667 default constructor and a deleted copy assignment operator."
2668 This is diagnosed in maybe_explain_implicit_delete.
2669 In C++20, only lambda-expressions with lambda-captures have those
2670 deleted. */
2671 if (LAMBDA_TYPE_P (ctype)
2672 && (sfk == sfk_constructor || sfk == sfk_copy_assignment)
2673 && (cxx_dialect < cxx20
2674 || LAMBDA_EXPR_CAPTURE_LIST (CLASSTYPE_LAMBDA_EXPR (ctype))
2675 || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE
2676 (CLASSTYPE_LAMBDA_EXPR (ctype)) != CPLD_NONE))
2678 *deleted_p = true;
2679 return;
2682 *deleted_p = false;
2685 bool check_vdtor = false;
2686 tree fnname;
2688 if (SFK_DTOR_P (sfk))
2690 check_vdtor = true;
2691 /* The synthesized method will call base dtors, but check complete
2692 here to avoid having to deal with VTT. */
2693 fnname = complete_dtor_identifier;
2695 else if (SFK_ASSIGN_P (sfk))
2696 fnname = assign_op_identifier;
2697 else
2698 fnname = complete_ctor_identifier;
2700 gcc_assert ((sfk == sfk_inheriting_constructor)
2701 == (inheriting_ctor && *inheriting_ctor != NULL_TREE));
2703 /* If that user-written default constructor would satisfy the
2704 requirements of a constexpr constructor (7.1.5), the
2705 implicitly-defined default constructor is constexpr.
2707 C++20:
2708 The implicitly-defined copy/move assignment operator is constexpr if
2709 - X is a literal type, and
2710 - the assignment operator selected to copy/move each direct base class
2711 subobject is a constexpr function, and
2712 - for each non-static data member of X that is of class type (or array
2713 thereof), the assignment operator selected to copy/move that
2714 member is a constexpr function.
2716 C++23:
2717 The implicitly-defined copy/move assignment operator is constexpr. */
2718 if (constexpr_p)
2719 *constexpr_p = (SFK_CTOR_P (sfk)
2720 || (SFK_ASSIGN_P (sfk) && cxx_dialect >= cxx14)
2721 || (SFK_DTOR_P (sfk) && cxx_dialect >= cxx20));
2723 bool expected_trivial = type_has_trivial_fn (ctype, sfk);
2724 if (trivial_p)
2725 *trivial_p = expected_trivial;
2727 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
2728 class versions and other properties of the type. But a subobject
2729 class can be trivially copyable and yet have overload resolution
2730 choose a template constructor for initialization, depending on
2731 rvalueness and cv-quals. And furthermore, a member in a base might
2732 be trivial but deleted or otherwise not callable. So we can't exit
2733 early in C++0x. The same considerations apply in C++98/03, but
2734 there the definition of triviality does not consider overload
2735 resolution, so a constructor can be trivial even if it would otherwise
2736 call a non-trivial constructor. */
2737 if (expected_trivial
2738 && (!(SFK_COPY_P (sfk) || SFK_MOVE_P (sfk)) || cxx_dialect < cxx11))
2740 if (constexpr_p && sfk == sfk_constructor)
2742 bool cx = trivial_default_constructor_is_constexpr (ctype);
2743 *constexpr_p = cx;
2744 if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
2745 /* A trivial constructor doesn't have any NSDMI. */
2746 inform (input_location, "defaulted default constructor does "
2747 "not initialize any non-static data member");
2749 if (!diag && cxx_dialect < cxx11)
2750 return;
2753 ++cp_unevaluated_operand;
2754 ++c_inhibit_evaluation_warnings;
2755 push_deferring_access_checks (dk_no_deferred);
2757 tree scope = push_scope (ctype);
2759 int flags = LOOKUP_NORMAL | LOOKUP_SPECULATIVE;
2760 if (sfk != sfk_inheriting_constructor)
2761 flags |= LOOKUP_DEFAULTED;
2763 tsubst_flags_t complain = diag ? tf_warning_or_error : tf_none;
2764 if (diag && spec_p)
2765 /* We're in get_defaulted_eh_spec; we don't actually want any walking
2766 diagnostics, we just want complain set. */
2767 diag = false;
2768 int quals = const_p ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED;
2770 for (binfo = TYPE_BINFO (ctype), i = 0;
2771 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
2773 if (!SFK_ASSIGN_P (sfk) && BINFO_VIRTUAL_P (base_binfo))
2774 /* We'll handle virtual bases below. */
2775 continue;
2777 tree fn = synthesized_method_base_walk (binfo, base_binfo,
2778 sfk, fnname, quals,
2779 inheriting_ctor, inherited_parms,
2780 flags, diag, spec_p, trivial_p,
2781 deleted_p, constexpr_p);
2783 if (diag && SFK_ASSIGN_P (sfk) && SFK_MOVE_P (sfk)
2784 && BINFO_VIRTUAL_P (base_binfo)
2785 && fn && TREE_CODE (fn) == FUNCTION_DECL
2786 && move_fn_p (fn) && !trivial_fn_p (fn)
2787 && vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
2788 warning (OPT_Wvirtual_move_assign,
2789 "defaulted move assignment for %qT calls a non-trivial "
2790 "move assignment operator for virtual base %qT",
2791 ctype, BINFO_TYPE (base_binfo));
2793 if (check_vdtor && type_has_virtual_destructor (BINFO_TYPE (base_binfo)))
2795 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
2796 to have a null fn (no class-specific op delete). */
2797 fn = locate_fn_flags (ctype, ovl_op_identifier (false, DELETE_EXPR),
2798 ptr_type_node, flags, tf_none);
2799 if (fn && fn == error_mark_node)
2801 if (complain & tf_error)
2802 locate_fn_flags (ctype, ovl_op_identifier (false, DELETE_EXPR),
2803 ptr_type_node, flags, complain);
2804 if (deleted_p)
2805 *deleted_p = true;
2807 check_vdtor = false;
2811 vec<tree, va_gc> *vbases = CLASSTYPE_VBASECLASSES (ctype);
2812 if (SFK_ASSIGN_P (sfk))
2813 /* Already examined vbases above. */;
2814 else if (vec_safe_is_empty (vbases))
2815 /* No virtual bases to worry about. */;
2816 else if (ABSTRACT_CLASS_TYPE_P (ctype) && cxx_dialect >= cxx14
2817 /* DR 1658 specifies that vbases of abstract classes are
2818 ignored for both ctors and dtors. Except DR 2336
2819 overrides that skipping when determing the eh-spec of a
2820 virtual destructor. */
2821 && sfk != sfk_virtual_destructor)
2822 /* Vbase cdtors are not relevant. */;
2823 else
2825 if (constexpr_p)
2826 *constexpr_p = false;
2828 FOR_EACH_VEC_ELT (*vbases, i, base_binfo)
2829 synthesized_method_base_walk (binfo, base_binfo, sfk, fnname, quals,
2830 inheriting_ctor, inherited_parms,
2831 flags, diag,
2832 spec_p, trivial_p, deleted_p, constexpr_p);
2835 /* Now handle the non-static data members. */
2836 walk_field_subobs (TYPE_FIELDS (ctype), sfk, fnname, quals,
2837 spec_p, trivial_p, deleted_p, constexpr_p,
2838 diag, flags, complain, /*dtor_from_ctor*/false);
2839 if (SFK_CTOR_P (sfk))
2840 walk_field_subobs (TYPE_FIELDS (ctype), sfk_destructor,
2841 complete_dtor_identifier, TYPE_UNQUALIFIED,
2842 NULL, NULL, deleted_p, NULL,
2843 false, flags, complain, /*dtor_from_ctor*/true);
2845 pop_scope (scope);
2847 pop_deferring_access_checks ();
2848 --cp_unevaluated_operand;
2849 --c_inhibit_evaluation_warnings;
2852 /* DECL is a defaulted function whose exception specification is now
2853 needed. Return what it should be. */
2855 tree
2856 get_defaulted_eh_spec (tree decl, tsubst_flags_t complain)
2858 /* For DECL_MAYBE_DELETED this should already have been handled by
2859 synthesize_method. */
2860 gcc_assert (!DECL_MAYBE_DELETED (decl));
2862 if (DECL_CLONED_FUNCTION_P (decl))
2863 decl = DECL_CLONED_FUNCTION (decl);
2864 special_function_kind sfk = special_function_p (decl);
2865 tree ctype = DECL_CONTEXT (decl);
2866 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
2867 tree parm_type = TREE_VALUE (parms);
2868 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
2869 tree spec = empty_except_spec;
2870 bool diag = !DECL_DELETED_FN (decl) && (complain & tf_error);
2871 tree inh = DECL_INHERITED_CTOR (decl);
2872 if (SFK_DTOR_P (sfk) && DECL_VIRTUAL_P (decl))
2873 /* We have to examine virtual bases even if abstract. */
2874 sfk = sfk_virtual_destructor;
2875 bool pushed = false;
2876 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2877 pushed = push_tinst_level (decl);
2878 synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL,
2879 NULL, diag, &inh, parms);
2880 if (pushed)
2881 pop_tinst_level ();
2882 return spec;
2885 /* DECL is a deleted function. If it's implicitly deleted, explain why and
2886 return true; else return false. */
2888 bool
2889 maybe_explain_implicit_delete (tree decl)
2891 /* If decl is a clone, get the primary variant. */
2892 decl = DECL_ORIGIN (decl);
2893 gcc_assert (DECL_DELETED_FN (decl));
2894 if (DECL_DEFAULTED_FN (decl))
2896 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
2897 static hash_set<tree> *explained;
2899 special_function_kind sfk;
2900 location_t loc;
2901 bool informed;
2902 tree ctype;
2904 if (!explained)
2905 explained = new hash_set<tree>;
2906 if (explained->add (decl))
2907 return true;
2909 sfk = special_function_p (decl);
2910 ctype = DECL_CONTEXT (decl);
2911 loc = input_location;
2912 input_location = DECL_SOURCE_LOCATION (decl);
2914 informed = false;
2915 if (LAMBDA_TYPE_P (ctype))
2917 informed = true;
2918 if (sfk == sfk_constructor)
2919 inform (DECL_SOURCE_LOCATION (decl),
2920 "a lambda closure type has a deleted default constructor");
2921 else if (sfk == sfk_copy_assignment)
2922 inform (DECL_SOURCE_LOCATION (decl),
2923 "a lambda closure type has a deleted copy assignment operator");
2924 else
2925 informed = false;
2927 else if (DECL_ARTIFICIAL (decl)
2928 && (sfk == sfk_copy_assignment || sfk == sfk_copy_constructor)
2929 && classtype_has_move_assign_or_move_ctor_p (ctype, true))
2931 inform (DECL_SOURCE_LOCATION (decl),
2932 "%q#D is implicitly declared as deleted because %qT "
2933 "declares a move constructor or move assignment operator",
2934 decl, ctype);
2935 informed = true;
2937 else if (sfk == sfk_inheriting_constructor)
2939 tree binfo = inherited_ctor_binfo (decl);
2940 if (TREE_CODE (binfo) != TREE_BINFO)
2942 inform (DECL_SOURCE_LOCATION (decl),
2943 "%q#D inherits from multiple base subobjects",
2944 decl);
2945 informed = true;
2948 if (!informed && sfk == sfk_comparison)
2950 inform (DECL_SOURCE_LOCATION (decl),
2951 "%q#D is implicitly deleted because the default "
2952 "definition would be ill-formed:", decl);
2953 build_comparison_op (decl, false, tf_warning_or_error);
2955 else if (!informed)
2957 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
2958 bool const_p = false;
2959 if (parms)
2961 tree parm_type = TREE_VALUE (parms);
2962 const_p = CP_TYPE_CONST_P (non_reference (parm_type));
2964 tree raises = NULL_TREE;
2965 bool deleted_p = false;
2966 tree scope = push_scope (ctype);
2967 tree inh = DECL_INHERITED_CTOR (decl);
2969 synthesized_method_walk (ctype, sfk, const_p,
2970 &raises, NULL, &deleted_p, NULL, false,
2971 &inh, parms);
2972 if (deleted_p)
2974 inform (DECL_SOURCE_LOCATION (decl),
2975 "%q#D is implicitly deleted because the default "
2976 "definition would be ill-formed:", decl);
2977 synthesized_method_walk (ctype, sfk, const_p,
2978 NULL, NULL, &deleted_p, NULL, true,
2979 &inh, parms);
2981 else if (!comp_except_specs
2982 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
2983 raises, ce_normal))
2984 inform (DECL_SOURCE_LOCATION (decl), "%q#F is implicitly "
2985 "deleted because its exception-specification does not "
2986 "match the implicit exception-specification %qX",
2987 decl, raises);
2988 else if (flag_checking)
2989 gcc_unreachable ();
2991 pop_scope (scope);
2994 input_location = loc;
2995 return true;
2997 return false;
3000 /* DECL is a defaulted function which was declared constexpr. Explain why
3001 it can't be constexpr. */
3003 void
3004 explain_implicit_non_constexpr (tree decl)
3006 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
3007 bool const_p = CP_TYPE_CONST_P (non_reference (TREE_VALUE (parms)));
3008 tree inh = DECL_INHERITED_CTOR (decl);
3009 bool dummy;
3010 special_function_kind sfk = special_function_p (decl);
3011 if (sfk == sfk_comparison)
3013 DECL_DECLARED_CONSTEXPR_P (decl) = true;
3014 build_comparison_op (decl, false, tf_warning_or_error);
3015 DECL_DECLARED_CONSTEXPR_P (decl) = false;
3017 else
3018 synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
3019 sfk, const_p,
3020 NULL, NULL, NULL, &dummy, true,
3021 &inh, parms);
3024 /* DECL is an instantiation of an inheriting constructor template. Deduce
3025 the correct exception-specification and deletedness for this particular
3026 specialization. Return true if the deduction succeeds; false otherwise. */
3028 bool
3029 deduce_inheriting_ctor (tree decl)
3031 decl = DECL_ORIGIN (decl);
3032 gcc_assert (DECL_INHERITED_CTOR (decl));
3033 tree spec;
3034 bool trivial, constexpr_, deleted;
3035 tree inh = DECL_INHERITED_CTOR (decl);
3036 synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor,
3037 false, &spec, &trivial, &deleted, &constexpr_,
3038 /*diag*/false,
3039 &inh,
3040 FUNCTION_FIRST_USER_PARMTYPE (decl));
3041 if (spec == error_mark_node)
3042 return false;
3043 if (TREE_CODE (inherited_ctor_binfo (decl)) != TREE_BINFO)
3044 /* Inherited the same constructor from different base subobjects. */
3045 deleted = true;
3046 DECL_DELETED_FN (decl) = deleted;
3047 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
3048 SET_DECL_INHERITED_CTOR (decl, inh);
3050 tree clone;
3051 FOR_EACH_CLONE (clone, decl)
3053 DECL_DELETED_FN (clone) = deleted;
3054 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
3055 SET_DECL_INHERITED_CTOR (clone, inh);
3058 return true;
3061 /* Implicitly declare the special function indicated by KIND, as a
3062 member of TYPE. For copy constructors and assignment operators,
3063 CONST_P indicates whether these functions should take a const
3064 reference argument or a non-const reference.
3065 Returns the FUNCTION_DECL for the implicitly declared function. */
3067 tree
3068 implicitly_declare_fn (special_function_kind kind, tree type,
3069 bool const_p, tree pattern_fn,
3070 tree inherited_parms)
3072 tree fn;
3073 tree parameter_types = void_list_node;
3074 tree return_type;
3075 tree fn_type;
3076 tree raises = empty_except_spec;
3077 tree rhs_parm_type = NULL_TREE;
3078 tree this_parm;
3079 tree name;
3080 HOST_WIDE_INT saved_processing_template_decl;
3081 bool deleted_p = false;
3082 bool constexpr_p = false;
3083 tree inherited_ctor = (kind == sfk_inheriting_constructor
3084 ? pattern_fn : NULL_TREE);
3086 /* Because we create declarations for implicitly declared functions
3087 lazily, we may be creating the declaration for a member of TYPE
3088 while in some completely different context. However, TYPE will
3089 never be a dependent class (because we never want to do lookups
3090 for implicitly defined functions in a dependent class). */
3091 gcc_assert (!dependent_type_p (type));
3093 /* If the member-specification does not explicitly declare any member or
3094 friend named operator==, an == operator function is declared
3095 implicitly for each three-way comparison operator function defined as
3096 defaulted in the member-specification, with the same access and
3097 function-definition and in the same class scope as the respective
3098 three-way comparison operator function, except that the return type is
3099 replaced with bool and the declarator-id is replaced with
3100 operator==.
3102 [Note: Such an implicitly-declared == operator for a class X is
3103 defined as defaulted in the definition of X and has the same
3104 parameter-declaration-clause and trailing requires-clause as the
3105 respective three-way comparison operator. It is declared with friend,
3106 virtual, constexpr, or consteval if the three-way comparison operator
3107 function is so declared. If the three-way comparison operator function
3108 has no noexcept-specifier, the implicitly-declared == operator
3109 function has an implicit exception specification (14.5) that may
3110 differ from the implicit exception specification of the three-way
3111 comparison operator function. --end note] */
3112 if (kind == sfk_comparison)
3114 fn = copy_operator_fn (pattern_fn, EQ_EXPR);
3115 DECL_ARTIFICIAL (fn) = 1;
3116 apply_deduced_return_type (fn, boolean_type_node);
3117 return fn;
3120 /* Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
3121 because we only create clones for constructors and destructors
3122 when not in a template. */
3123 saved_processing_template_decl = processing_template_decl;
3124 processing_template_decl = 0;
3126 type = TYPE_MAIN_VARIANT (type);
3128 if (targetm.cxx.cdtor_returns_this ())
3130 if (kind == sfk_destructor)
3131 /* See comment in check_special_function_return_type. */
3132 return_type = build_pointer_type (void_type_node);
3133 else
3134 return_type = build_pointer_type (type);
3136 else
3137 return_type = void_type_node;
3139 int this_quals = TYPE_UNQUALIFIED;
3140 switch (kind)
3142 case sfk_destructor:
3143 /* Destructor. */
3144 name = dtor_identifier;
3145 break;
3147 case sfk_constructor:
3148 /* Default constructor. */
3149 name = ctor_identifier;
3150 break;
3152 case sfk_copy_constructor:
3153 case sfk_copy_assignment:
3154 case sfk_move_constructor:
3155 case sfk_move_assignment:
3156 case sfk_inheriting_constructor:
3158 if (kind == sfk_copy_assignment
3159 || kind == sfk_move_assignment)
3161 return_type = build_reference_type (type);
3162 name = assign_op_identifier;
3164 else
3165 name = ctor_identifier;
3167 if (kind == sfk_inheriting_constructor)
3168 parameter_types = inherited_parms;
3169 else
3171 if (const_p)
3172 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
3173 else
3174 rhs_parm_type = type;
3175 bool move_p = (kind == sfk_move_assignment
3176 || kind == sfk_move_constructor);
3177 rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
3179 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
3181 break;
3184 default:
3185 gcc_unreachable ();
3188 bool trivial_p = false;
3190 if (inherited_ctor)
3192 /* For an inheriting constructor, just copy these flags from the
3193 inherited constructor until deduce_inheriting_ctor. */
3194 raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor));
3195 deleted_p = DECL_DELETED_FN (inherited_ctor);
3196 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
3198 else if (cxx_dialect >= cxx11)
3200 raises = noexcept_deferred_spec;
3201 synthesized_method_walk (type, kind, const_p, NULL, &trivial_p,
3202 &deleted_p, &constexpr_p, false,
3203 &inherited_ctor, inherited_parms);
3205 else
3206 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
3207 &deleted_p, &constexpr_p, false,
3208 &inherited_ctor, inherited_parms);
3209 /* Don't bother marking a deleted constructor as constexpr. */
3210 if (deleted_p)
3211 constexpr_p = false;
3212 /* A trivial copy/move constructor is also a constexpr constructor,
3213 unless the class has virtual bases (7.1.5p4). */
3214 else if (trivial_p
3215 && cxx_dialect >= cxx11
3216 && (kind == sfk_copy_constructor
3217 || kind == sfk_move_constructor)
3218 && !CLASSTYPE_VBASECLASSES (type))
3219 gcc_assert (constexpr_p);
3221 if (!trivial_p && type_has_trivial_fn (type, kind))
3222 type_set_nontrivial_flag (type, kind);
3224 /* Create the function. */
3225 tree this_type = cp_build_qualified_type (type, this_quals);
3226 fn_type = build_method_type_directly (this_type, return_type,
3227 parameter_types);
3229 if (raises)
3231 if (raises != error_mark_node)
3232 fn_type = build_exception_variant (fn_type, raises);
3233 else
3235 /* Can happen, e.g., in C++98 mode for an ill-formed non-static data
3236 member initializer (c++/89914). Also, in C++98, we might have
3237 failed to deduce RAISES, so try again but complain this time. */
3238 if (cxx_dialect < cxx11)
3239 synthesized_method_walk (type, kind, const_p, &raises, nullptr,
3240 nullptr, nullptr, /*diag=*/true,
3241 &inherited_ctor, inherited_parms);
3242 /* We should have seen an error at this point. */
3243 gcc_assert (seen_error ());
3246 fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
3247 if (kind != sfk_inheriting_constructor)
3248 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
3250 if (IDENTIFIER_OVL_OP_P (name))
3252 const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (name);
3253 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) = op->ovl_op_code;
3255 else if (IDENTIFIER_CTOR_P (name))
3256 DECL_CXX_CONSTRUCTOR_P (fn) = true;
3257 else if (IDENTIFIER_DTOR_P (name))
3258 DECL_CXX_DESTRUCTOR_P (fn) = true;
3259 else
3260 gcc_unreachable ();
3262 SET_DECL_ALIGN (fn, MINIMUM_METHOD_BOUNDARY);
3264 /* Create the explicit arguments. */
3265 if (rhs_parm_type)
3267 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
3268 want its type to be included in the mangled function
3269 name. */
3270 tree decl = cp_build_parm_decl (fn, NULL_TREE, rhs_parm_type);
3271 TREE_READONLY (decl) = 1;
3272 retrofit_lang_decl (decl);
3273 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
3274 DECL_ARGUMENTS (fn) = decl;
3276 else if (kind == sfk_inheriting_constructor)
3278 tree *p = &DECL_ARGUMENTS (fn);
3279 int index = 1;
3280 for (tree parm = inherited_parms; parm && parm != void_list_node;
3281 parm = TREE_CHAIN (parm))
3283 *p = cp_build_parm_decl (fn, NULL_TREE, TREE_VALUE (parm));
3284 retrofit_lang_decl (*p);
3285 DECL_PARM_LEVEL (*p) = 1;
3286 DECL_PARM_INDEX (*p) = index++;
3287 p = &DECL_CHAIN (*p);
3289 SET_DECL_INHERITED_CTOR (fn, inherited_ctor);
3290 DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor);
3291 /* A constructor so declared has the same access as the corresponding
3292 constructor in X. */
3293 TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor);
3294 TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor);
3295 /* Copy constexpr from the inherited constructor even if the
3296 inheriting constructor doesn't satisfy the requirements. */
3297 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
3298 /* Also copy any attributes. */
3299 DECL_ATTRIBUTES (fn) = clone_attrs (DECL_ATTRIBUTES (inherited_ctor));
3302 /* Add the "this" parameter. */
3303 this_parm = build_this_parm (fn, fn_type, this_quals);
3304 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
3305 DECL_ARGUMENTS (fn) = this_parm;
3307 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
3309 DECL_IN_AGGR_P (fn) = 1;
3310 DECL_ARTIFICIAL (fn) = 1;
3311 DECL_DEFAULTED_FN (fn) = 1;
3312 if (cxx_dialect >= cxx11)
3314 DECL_DELETED_FN (fn) = deleted_p;
3315 DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
3317 DECL_EXTERNAL (fn) = true;
3318 DECL_NOT_REALLY_EXTERN (fn) = 1;
3319 DECL_DECLARED_INLINE_P (fn) = 1;
3320 set_linkage_according_to_type (type, fn);
3321 if (TREE_PUBLIC (fn))
3322 DECL_COMDAT (fn) = 1;
3323 rest_of_decl_compilation (fn, namespace_bindings_p (), at_eof);
3324 gcc_assert (!TREE_USED (fn));
3326 /* Propagate constraints from the inherited constructor. */
3327 if (flag_concepts && inherited_ctor)
3328 if (tree orig_ci = get_constraints (inherited_ctor))
3330 tree new_ci = copy_node (orig_ci);
3331 set_constraints (fn, new_ci);
3334 /* Restore PROCESSING_TEMPLATE_DECL. */
3335 processing_template_decl = saved_processing_template_decl;
3337 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
3338 fn = add_inherited_template_parms (fn, inherited_ctor);
3340 /* Warn about calling a non-trivial move assignment in a virtual base. */
3341 if (kind == sfk_move_assignment && !deleted_p && !trivial_p
3342 && CLASSTYPE_VBASECLASSES (type))
3344 location_t loc = input_location;
3345 input_location = DECL_SOURCE_LOCATION (fn);
3346 synthesized_method_walk (type, kind, const_p,
3347 NULL, NULL, NULL, NULL, true,
3348 NULL, NULL_TREE);
3349 input_location = loc;
3352 return fn;
3355 /* Gives any errors about defaulted functions which need to be deferred
3356 until the containing class is complete. */
3358 void
3359 defaulted_late_check (tree fn)
3361 /* Complain about invalid signature for defaulted fn. */
3362 tree ctx = DECL_CONTEXT (fn);
3363 special_function_kind kind = special_function_p (fn);
3365 if (kind == sfk_comparison)
3367 /* If the function was declared constexpr, check that the definition
3368 qualifies. Otherwise we can define the function lazily. */
3369 if (DECL_DECLARED_CONSTEXPR_P (fn) && !DECL_INITIAL (fn))
3371 /* Prevent GC. */
3372 function_depth++;
3373 synthesize_method (fn);
3374 function_depth--;
3376 return;
3379 bool fn_const_p = (copy_fn_p (fn) == 2);
3380 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
3381 NULL, NULL);
3382 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
3384 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
3385 TREE_TYPE (TREE_TYPE (implicit_fn)))
3386 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
3387 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn))))
3389 error ("defaulted declaration %q+D does not match the "
3390 "expected signature", fn);
3391 inform (DECL_SOURCE_LOCATION (fn),
3392 "expected signature: %qD", implicit_fn);
3395 if (DECL_DELETED_FN (implicit_fn))
3397 DECL_DELETED_FN (fn) = 1;
3398 return;
3401 /* If a function is explicitly defaulted on its first declaration without an
3402 exception-specification, it is implicitly considered to have the same
3403 exception-specification as if it had been implicitly declared. */
3404 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn))
3405 && DECL_DEFAULTED_IN_CLASS_P (fn))
3406 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
3408 if (DECL_DEFAULTED_IN_CLASS_P (fn)
3409 && DECL_DECLARED_CONSTEXPR_P (implicit_fn))
3411 /* Hmm...should we do this for out-of-class too? Should it be OK to
3412 add constexpr later like inline, rather than requiring
3413 declarations to match? */
3414 DECL_DECLARED_CONSTEXPR_P (fn) = true;
3415 if (kind == sfk_constructor)
3416 TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
3419 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
3420 && DECL_DECLARED_CONSTEXPR_P (fn))
3422 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
3424 error ("explicitly defaulted function %q+D cannot be declared "
3425 "%qs because the implicit declaration is not %qs:", fn,
3426 DECL_IMMEDIATE_FUNCTION_P (fn) ? "consteval" : "constexpr",
3427 "constexpr");
3428 explain_implicit_non_constexpr (fn);
3430 DECL_DECLARED_CONSTEXPR_P (fn) = false;
3434 /* Returns true iff FN can be explicitly defaulted, and gives any
3435 errors if defaulting FN is ill-formed. */
3437 bool
3438 defaultable_fn_check (tree fn)
3440 special_function_kind kind = sfk_none;
3442 if (template_parm_scope_p ())
3444 error ("a template cannot be defaulted");
3445 return false;
3448 if (DECL_CONSTRUCTOR_P (fn))
3450 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
3451 kind = sfk_constructor;
3452 else if (copy_fn_p (fn) > 0
3453 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
3454 == void_list_node))
3455 kind = sfk_copy_constructor;
3456 else if (move_fn_p (fn))
3457 kind = sfk_move_constructor;
3459 else if (DECL_DESTRUCTOR_P (fn))
3460 kind = sfk_destructor;
3461 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
3462 && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR))
3464 if (copy_fn_p (fn))
3465 kind = sfk_copy_assignment;
3466 else if (move_fn_p (fn))
3467 kind = sfk_move_assignment;
3469 else if (DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) >= OVL_OP_EQ_EXPR
3470 && DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) <= OVL_OP_SPACESHIP_EXPR)
3472 kind = sfk_comparison;
3473 if (!early_check_defaulted_comparison (fn))
3474 return false;
3477 if (kind == sfk_none)
3479 error ("%qD cannot be defaulted", fn);
3480 return false;
3482 else
3484 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
3485 t && t != void_list_node; t = TREE_CHAIN (t))
3486 if (TREE_PURPOSE (t))
3488 error ("defaulted function %q+D with default argument", fn);
3489 break;
3492 /* Avoid do_warn_unused_parameter warnings. */
3493 for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p))
3494 if (DECL_NAME (p))
3495 suppress_warning (p, OPT_Wunused_parameter);
3497 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
3498 /* Defer checking. */;
3499 else if (!processing_template_decl)
3500 defaulted_late_check (fn);
3502 return true;
3506 /* Add an implicit declaration to TYPE for the kind of function
3507 indicated by SFK. Return the FUNCTION_DECL for the new implicit
3508 declaration. */
3510 tree
3511 lazily_declare_fn (special_function_kind sfk, tree type)
3513 tree fn;
3514 /* Whether or not the argument has a const reference type. */
3515 bool const_p = false;
3517 type = TYPE_MAIN_VARIANT (type);
3519 switch (sfk)
3521 case sfk_constructor:
3522 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
3523 break;
3524 case sfk_copy_constructor:
3525 const_p = TYPE_HAS_CONST_COPY_CTOR (type);
3526 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
3527 break;
3528 case sfk_move_constructor:
3529 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
3530 break;
3531 case sfk_copy_assignment:
3532 const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
3533 CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
3534 break;
3535 case sfk_move_assignment:
3536 CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
3537 break;
3538 case sfk_destructor:
3539 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
3540 break;
3541 default:
3542 gcc_unreachable ();
3545 /* Declare the function. */
3546 fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL);
3548 /* [class.copy]/8 If the class definition declares a move constructor or
3549 move assignment operator, the implicitly declared copy constructor is
3550 defined as deleted.... */
3551 if ((sfk == sfk_copy_assignment || sfk == sfk_copy_constructor)
3552 && cxx_dialect >= cxx11)
3554 if (classtype_has_move_assign_or_move_ctor_p (type, true))
3555 DECL_DELETED_FN (fn) = true;
3556 else if (classtype_has_depr_implicit_copy (type))
3557 /* The implicit definition of a copy constructor as defaulted is
3558 deprecated if the class has a user-declared copy assignment operator
3559 or a user-declared destructor. The implicit definition of a copy
3560 assignment operator as defaulted is deprecated if the class has a
3561 user-declared copy constructor or a user-declared destructor (15.4,
3562 15.8). */
3563 TREE_DEPRECATED (fn) = true;
3566 /* Destructors and assignment operators may be virtual. */
3567 if (sfk == sfk_destructor
3568 || sfk == sfk_move_assignment
3569 || sfk == sfk_copy_assignment)
3570 check_for_override (fn, type);
3572 /* Add it to the class */
3573 bool added = add_method (type, fn, false);
3574 gcc_assert (added || errorcount);
3576 /* Add it to TYPE_FIELDS. */
3577 if (sfk == sfk_destructor
3578 && DECL_VIRTUAL_P (fn))
3579 /* The ABI requires that a virtual destructor go at the end of the
3580 vtable. */
3581 TYPE_FIELDS (type) = chainon (TYPE_FIELDS (type), fn);
3582 else
3584 DECL_CHAIN (fn) = TYPE_FIELDS (type);
3585 TYPE_FIELDS (type) = fn;
3587 /* Propagate TYPE_FIELDS. */
3588 fixup_type_variants (type);
3590 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
3591 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (fn))
3592 /* Create appropriate clones. */
3593 clone_cdtor (fn, /*update_methods=*/true);
3595 /* Classes, structs or unions TYPE marked with hotness attributes propagate
3596 the attribute to all methods. This is typically done in
3597 check_bases_and_members, but we must also inject them here for deferred
3598 lazily-declared functions. */
3599 maybe_propagate_warmth_attributes (fn, type);
3601 return fn;
3604 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
3605 as there are artificial parms in FN. */
3607 tree
3608 skip_artificial_parms_for (const_tree fn, tree list)
3610 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
3611 list = TREE_CHAIN (list);
3612 else
3613 return list;
3615 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
3616 list = TREE_CHAIN (list);
3617 if (DECL_HAS_VTT_PARM_P (fn))
3618 list = TREE_CHAIN (list);
3619 return list;
3622 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
3623 artificial parms in FN. */
3626 num_artificial_parms_for (const_tree fn)
3628 int count = 0;
3630 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
3631 count++;
3632 else
3633 return 0;
3635 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
3636 count++;
3637 if (DECL_HAS_VTT_PARM_P (fn))
3638 count++;
3639 return count;
3643 #include "gt-cp-method.h"