Debian Codegraph @

gcc/cp/method.cc

raw
1 /* Handle the hair of processing (but not expanding) inline functions.
2 Also manage function and variable name overloading.
3 Copyright (C) 1987-2025 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann ([email protected])
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
22
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 "decl.h"
30 #include "stringpool.h"
31 #include "cgraph.h"
32 #include "varasm.h"
33 #include "toplev.h"
34 #include "intl.h"
35 #include "common/common-target.h"
36
37 static void do_build_copy_assign (tree);
38 static void do_build_copy_constructor (tree);
39 static tree make_alias_for_thunk (tree);
40
41 /* Called once to initialize method.cc. */
42
43 void
44 init_method (void)
45 {
46 init_mangle ();
47 }
48
49 /* Return a this or result adjusting thunk to FUNCTION. THIS_ADJUSTING
50 indicates whether it is a this or result adjusting thunk.
51 FIXED_OFFSET and VIRTUAL_OFFSET indicate how to do the adjustment
52 (see thunk_adjust). VIRTUAL_OFFSET can be NULL, but FIXED_OFFSET
53 never is. VIRTUAL_OFFSET is the /index/ into the vtable for this
54 adjusting thunks, we scale it to a byte offset. For covariant
55 thunks VIRTUAL_OFFSET is the virtual binfo. You must post process
56 the returned thunk with finish_thunk. */
57
58 tree
59 make_thunk (tree function, bool this_adjusting,
60 tree fixed_offset, tree virtual_offset)
61 {
62 HOST_WIDE_INT d;
63 tree thunk;
64
65 gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
66 /* We can have this thunks to covariant thunks, but not vice versa. */
67 gcc_assert (!DECL_THIS_THUNK_P (function));
68 gcc_assert (!DECL_RESULT_THUNK_P (function) || this_adjusting);
69
70 /* Scale the VIRTUAL_OFFSET to be in terms of bytes. */
71 if (this_adjusting && virtual_offset)
72 virtual_offset
73 = size_binop (MULT_EXPR,
74 virtual_offset,
75 convert (ssizetype,
76 TYPE_SIZE_UNIT (vtable_entry_type)));
77
78 d = tree_to_shwi (fixed_offset);
79
80 /* See if we already have the thunk in question. For this_adjusting
81 thunks VIRTUAL_OFFSET will be an INTEGER_CST, for covariant thunks it
82 will be a BINFO. */
83 for (thunk = DECL_THUNKS (function); thunk; thunk = DECL_CHAIN (thunk))
84 if (DECL_THIS_THUNK_P (thunk) == this_adjusting
85 && THUNK_FIXED_OFFSET (thunk) == d
86 && !virtual_offset == !THUNK_VIRTUAL_OFFSET (thunk)
87 && (!virtual_offset
88 || (this_adjusting
89 ? tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk),
90 virtual_offset)
91 : THUNK_VIRTUAL_OFFSET (thunk) == virtual_offset)))
92 return thunk;
93
94 /* All thunks must be created before FUNCTION is actually emitted;
95 the ABI requires that all thunks be emitted together with the
96 function to which they transfer control. */
97 gcc_assert (!TREE_ASM_WRITTEN (function));
98 /* Likewise, we can only be adding thunks to a function declared in
99 the class currently being laid out. */
100 gcc_assert (TYPE_SIZE (DECL_CONTEXT (function))
101 && TYPE_BEING_DEFINED (DECL_CONTEXT (function)));
102
103 thunk = build_decl (DECL_SOURCE_LOCATION (function),
104 FUNCTION_DECL, NULL_TREE, TREE_TYPE (function));
105 DECL_LANG_SPECIFIC (thunk) = DECL_LANG_SPECIFIC (function);
106 cxx_dup_lang_specific_decl (thunk);
107 DECL_VIRTUAL_P (thunk) = true;
108 SET_DECL_THUNKS (thunk, NULL_TREE);
109
110 DECL_CONTEXT (thunk) = DECL_CONTEXT (function);
111 TREE_READONLY (thunk) = TREE_READONLY (function);
112 TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (function);
113 TREE_PUBLIC (thunk) = TREE_PUBLIC (function);
114 SET_DECL_THUNK_P (thunk, this_adjusting);
115 THUNK_TARGET (thunk) = function;
116 THUNK_FIXED_OFFSET (thunk) = d;
117 THUNK_VIRTUAL_OFFSET (thunk) = virtual_offset;
118 THUNK_ALIAS (thunk) = NULL_TREE;
119
120 DECL_INTERFACE_KNOWN (thunk) = 1;
121 DECL_NOT_REALLY_EXTERN (thunk) = 1;
122 DECL_COMDAT (thunk) = DECL_COMDAT (function);
123 DECL_SAVED_AUTO_RETURN_TYPE (thunk) = NULL;
124 /* The thunk itself is not a constructor or destructor, even if
125 the thing it is thunking to is. */
126 DECL_CXX_DESTRUCTOR_P (thunk) = 0;
127 DECL_CXX_CONSTRUCTOR_P (thunk) = 0;
128 DECL_EXTERNAL (thunk) = 1;
129 DECL_ARTIFICIAL (thunk) = 1;
130 /* The THUNK is not a pending inline, even if the FUNCTION is. */
131 DECL_PENDING_INLINE_P (thunk) = 0;
132 DECL_DECLARED_INLINE_P (thunk) = 0;
133 /* Nor is it a template instantiation. */
134 DECL_USE_TEMPLATE (thunk) = 0;
135 DECL_TEMPLATE_INFO (thunk) = NULL;
136
137 /* Add it to the list of thunks associated with FUNCTION. */
138 DECL_CHAIN (thunk) = DECL_THUNKS (function);
139 SET_DECL_THUNKS (function, thunk);
140
141 return thunk;
142 }
143
144 /* Finish THUNK, a thunk decl. */
145
146 void
147 finish_thunk (tree thunk)
148 {
149 tree function, name;
150 tree fixed_offset = ssize_int (THUNK_FIXED_OFFSET (thunk));
151 tree virtual_offset = THUNK_VIRTUAL_OFFSET (thunk);
152
153 gcc_assert (!DECL_NAME (thunk) && DECL_THUNK_P (thunk));
154 if (virtual_offset && DECL_RESULT_THUNK_P (thunk))
155 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
156 function = THUNK_TARGET (thunk);
157 name = mangle_thunk (function, DECL_THIS_THUNK_P (thunk),
158 fixed_offset, virtual_offset, thunk);
159
160 /* We can end up with declarations of (logically) different
161 covariant thunks, that do identical adjustments. The two thunks
162 will be adjusting between within different hierarchies, which
163 happen to have the same layout. We must nullify one of them to
164 refer to the other. */
165 if (DECL_RESULT_THUNK_P (thunk))
166 {
167 tree cov_probe;
168
169 for (cov_probe = DECL_THUNKS (function);
170 cov_probe; cov_probe = DECL_CHAIN (cov_probe))
171 if (DECL_NAME (cov_probe) == name)
172 {
173 gcc_assert (!DECL_THUNKS (thunk));
174 THUNK_ALIAS (thunk) = (THUNK_ALIAS (cov_probe)
175 ? THUNK_ALIAS (cov_probe) : cov_probe);
176 break;
177 }
178 }
179
180 DECL_NAME (thunk) = name;
181 SET_DECL_ASSEMBLER_NAME (thunk, name);
182 }
183
184 static GTY (()) int thunk_labelno;
185
186 /* Create a static alias to target. */
187
188 tree
189 make_alias_for (tree target, tree newid)
190 {
191 tree alias = build_decl (DECL_SOURCE_LOCATION (target),
192 TREE_CODE (target), newid, TREE_TYPE (target));
193 DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (target);
194 cxx_dup_lang_specific_decl (alias);
195 DECL_CONTEXT (alias) = DECL_CONTEXT (target);
196 TREE_READONLY (alias) = TREE_READONLY (target);
197 TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (target);
198 TREE_PUBLIC (alias) = 0;
199 DECL_INTERFACE_KNOWN (alias) = 1;
200 if (DECL_LANG_SPECIFIC (alias))
201 {
202 DECL_NOT_REALLY_EXTERN (alias) = 1;
203 DECL_USE_TEMPLATE (alias) = 0;
204 DECL_TEMPLATE_INFO (alias) = NULL;
205 }
206 DECL_EXTERNAL (alias) = 0;
207 DECL_ARTIFICIAL (alias) = 1;
208 DECL_TEMPLATE_INSTANTIATED (alias) = 0;
209 if (TREE_CODE (alias) == FUNCTION_DECL)
210 {
211 DECL_SAVED_AUTO_RETURN_TYPE (alias) = NULL;
212 DECL_CXX_DESTRUCTOR_P (alias) = 0;
213 DECL_CXX_CONSTRUCTOR_P (alias) = 0;
214 DECL_PENDING_INLINE_P (alias) = 0;
215 DECL_DECLARED_INLINE_P (alias) = 0;
216 DECL_INITIAL (alias) = error_mark_node;
217 DECL_ARGUMENTS (alias) = copy_list (DECL_ARGUMENTS (target));
218 }
219 else
220 TREE_STATIC (alias) = 1;
221 TREE_ADDRESSABLE (alias) = 1;
222 TREE_USED (alias) = 1;
223 SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
224 return alias;
225 }
226
227 static tree
228 make_alias_for_thunk (tree function)
229 {
230 tree alias;
231 char buf[256];
232
233 targetm.asm_out.generate_internal_label (buf, "LTHUNK", thunk_labelno);
234 thunk_labelno++;
235
236 alias = make_alias_for (function, get_identifier (buf));
237
238 if (!flag_syntax_only)
239 {
240 struct cgraph_node *funcn, *aliasn;
241 funcn = cgraph_node::get (function);
242 gcc_checking_assert (funcn);
243 aliasn = cgraph_node::create_same_body_alias (alias, function);
244 DECL_ASSEMBLER_NAME (function);
245 gcc_assert (aliasn != NULL);
246 }
247
248 return alias;
249 }
250
251 /* Emit the definition of a C++ multiple inheritance or covariant
252 return vtable thunk. If EMIT_P is nonzero, the thunk is emitted
253 immediately. */
254
255 void
256 use_thunk (tree thunk_fndecl, bool emit_p)
257 {
258 tree a, t, function, alias;
259 tree virtual_offset;
260 HOST_WIDE_INT fixed_offset, virtual_value;
261 bool this_adjusting = DECL_THIS_THUNK_P (thunk_fndecl);
262 struct cgraph_node *funcn, *thunk_node;
263
264 /* We should have called finish_thunk to give it a name. */
265 gcc_assert (DECL_NAME (thunk_fndecl));
266
267 /* We should never be using an alias, always refer to the
268 aliased thunk. */
269 gcc_assert (!THUNK_ALIAS (thunk_fndecl));
270
271 if (TREE_ASM_WRITTEN (thunk_fndecl))
272 return;
273
274 function = THUNK_TARGET (thunk_fndecl);
275 if (DECL_RESULT (thunk_fndecl))
276 /* We already turned this thunk into an ordinary function.
277 There's no need to process this thunk again. */
278 return;
279
280 if (DECL_THUNK_P (function))
281 /* The target is itself a thunk, process it now. */
282 use_thunk (function, emit_p);
283
284 /* Thunks are always addressable; they only appear in vtables. */
285 TREE_ADDRESSABLE (thunk_fndecl) = 1;
286
287 /* Don't diagnose deprecated or unavailable functions just because they
288 have thunks emitted for them. */
289 auto du = make_temp_override (deprecated_state,
290 UNAVAILABLE_DEPRECATED_SUPPRESS);
291
292 /* Figure out what function is being thunked to. It's referenced in
293 this translation unit. */
294 TREE_ADDRESSABLE (function) = 1;
295 mark_used (function);
296 if (!emit_p)
297 return;
298
299 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function))
300 alias = make_alias_for_thunk (function);
301 else
302 alias = function;
303
304 fixed_offset = THUNK_FIXED_OFFSET (thunk_fndecl);
305 virtual_offset = THUNK_VIRTUAL_OFFSET (thunk_fndecl);
306
307 if (virtual_offset)
308 {
309 if (!this_adjusting)
310 virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
311 virtual_value = tree_to_shwi (virtual_offset);
312 gcc_assert (virtual_value);
313 }
314 else
315 virtual_value = 0;
316
317 /* And, if we need to emit the thunk, it's used. */
318 mark_used (thunk_fndecl);
319 /* This thunk is actually defined. */
320 DECL_EXTERNAL (thunk_fndecl) = 0;
321 /* The linkage of the function may have changed. FIXME in linkage
322 rewrite. */
323 gcc_assert (DECL_INTERFACE_KNOWN (function));
324 TREE_PUBLIC (thunk_fndecl) = TREE_PUBLIC (function);
325 DECL_VISIBILITY (thunk_fndecl) = DECL_VISIBILITY (function);
326 DECL_VISIBILITY_SPECIFIED (thunk_fndecl)
327 = DECL_VISIBILITY_SPECIFIED (function);
328 DECL_COMDAT (thunk_fndecl) = DECL_COMDAT (function);
329 DECL_WEAK (thunk_fndecl) = DECL_WEAK (function);
330
331 if (flag_syntax_only)
332 {
333 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
334 return;
335 }
336
337 push_to_top_level ();
338
339 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function)
340 && targetm_common.have_named_sections)
341 {
342 tree fn = function;
343 struct symtab_node *symbol;
344
345 if ((symbol = symtab_node::get (function))
346 && symbol->alias)
347 {
348 if (symbol->analyzed)
349 fn = symtab_node::get (function)->ultimate_alias_target ()->decl;
350 else
351 fn = symtab_node::get (function)->alias_target;
352 }
353 resolve_unique_section (fn, 0, flag_function_sections);
354
355 if (DECL_SECTION_NAME (fn) != NULL && DECL_ONE_ONLY (fn))
356 {
357 resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
358
359 /* Output the thunk into the same section as function. */
360 set_decl_section_name (thunk_fndecl, fn);
361 symtab_node::get (thunk_fndecl)->implicit_section
362 = symtab_node::get (fn)->implicit_section;
363 }
364 }
365
366 /* Set up cloned argument trees for the thunk. */
367 t = NULL_TREE;
368 for (a = DECL_ARGUMENTS (function); a; a = DECL_CHAIN (a))
369 {
370 tree x = copy_node (a);
371 DECL_CHAIN (x) = t;
372 DECL_CONTEXT (x) = thunk_fndecl;
373 SET_DECL_RTL (x, NULL);
374 DECL_HAS_VALUE_EXPR_P (x) = 0;
375 TREE_ADDRESSABLE (x) = 0;
376 t = x;
377 }
378 a = nreverse (t);
379 DECL_ARGUMENTS (thunk_fndecl) = a;
380 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
381 funcn = cgraph_node::get (function);
382 gcc_checking_assert (funcn);
383 thunk_node = funcn->create_thunk (thunk_fndecl, function,
384 this_adjusting, fixed_offset, virtual_value,
385 0, virtual_offset, alias);
386 if (DECL_ONE_ONLY (function))
387 thunk_node->add_to_same_comdat_group (funcn);
388
389 pop_from_top_level ();
390 }
391
392 /* Code for synthesizing methods which have default semantics defined. */
393
394 /* True iff CTYPE has a trivial SFK. */
395
396 static bool
397 type_has_trivial_fn (tree ctype, special_function_kind sfk)
398 {
399 switch (sfk)
400 {
401 case sfk_constructor:
402 return !TYPE_HAS_COMPLEX_DFLT (ctype);
403 case sfk_copy_constructor:
404 return !TYPE_HAS_COMPLEX_COPY_CTOR (ctype);
405 case sfk_move_constructor:
406 return !TYPE_HAS_COMPLEX_MOVE_CTOR (ctype);
407 case sfk_copy_assignment:
408 return !TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype);
409 case sfk_move_assignment:
410 return !TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype);
411 case sfk_destructor:
412 case sfk_virtual_destructor:
413 return !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype);
414 case sfk_inheriting_constructor:
415 case sfk_comparison:
416 return false;
417 default:
418 gcc_unreachable ();
419 }
420 }
421
422 /* Note that CTYPE has a non-trivial SFK even though we previously thought
423 it was trivial. */
424
425 static void
426 type_set_nontrivial_flag (tree ctype, special_function_kind sfk)
427 {
428 switch (sfk)
429 {
430 case sfk_constructor:
431 TYPE_HAS_COMPLEX_DFLT (ctype) = true;
432 return;
433 case sfk_copy_constructor:
434 TYPE_HAS_COMPLEX_COPY_CTOR (ctype) = true;
435 return;
436 case sfk_move_constructor:
437 TYPE_HAS_COMPLEX_MOVE_CTOR (ctype) = true;
438 return;
439 case sfk_copy_assignment:
440 TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype) = true;
441 return;
442 case sfk_move_assignment:
443 TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype) = true;
444 return;
445 case sfk_destructor:
446 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
447 return;
448 case sfk_inheriting_constructor:
449 default:
450 gcc_unreachable ();
451 }
452 }
453
454 /* True iff FN is a trivial defaulted member function ([cd]tor, op=). */
455
456 bool
457 trivial_fn_p (tree fn)
458 {
459 if (TREE_CODE (fn) == TEMPLATE_DECL)
460 return false;
461 if (!DECL_DEFAULTED_FN (fn))
462 return false;
463
464 /* If fn is a clone, get the primary variant. */
465 if (tree prim = DECL_CLONED_FUNCTION (fn))
466 fn = prim;
467 return type_has_trivial_fn (DECL_CONTEXT (fn), special_function_p (fn));
468 }
469
470 /* PARM is a PARM_DECL for a function which we want to forward to another
471 function without changing its value category, a la std::forward. */
472
473 tree
474 forward_parm (tree parm)
475 {
476 tree exp = convert_from_reference (parm);
477 tree type = TREE_TYPE (parm);
478 if (DECL_PACK_P (parm))
479 type = PACK_EXPANSION_PATTERN (type);
480 if (!TYPE_REF_P (type))
481 type = cp_build_reference_type (type, /*rval=*/true);
482 warning_sentinel w (warn_useless_cast);
483 exp = build_static_cast (input_location, type, exp,
484 tf_warning_or_error);
485 if (DECL_PACK_P (parm))
486 exp = make_pack_expansion (exp);
487 return exp;
488 }
489
490 /* Strip all inheriting constructors, if any, to return the original
491 constructor from a (possibly indirect) base class. */
492
493 tree
494 strip_inheriting_ctors (tree dfn)
495 {
496 if (!flag_new_inheriting_ctors)
497 return dfn;
498 tree fn = dfn;
499 while (tree inh = DECL_INHERITED_CTOR (fn))
500 fn = OVL_FIRST (inh);
501
502 if (TREE_CODE (fn) == TEMPLATE_DECL
503 && TREE_CODE (dfn) == FUNCTION_DECL)
504 fn = DECL_TEMPLATE_RESULT (fn);
505 return fn;
506 }
507
508 /* Find the binfo for the base subobject of BINFO being initialized by
509 inherited constructor FNDECL (a member of a direct base of BINFO). */
510
511 static tree inherited_ctor_binfo (tree, tree);
512 static tree
513 inherited_ctor_binfo_1 (tree binfo, tree fndecl)
514 {
515 tree base = DECL_CONTEXT (fndecl);
516 tree base_binfo;
517 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
518 if (BINFO_TYPE (base_binfo) == base)
519 return inherited_ctor_binfo (base_binfo, fndecl);
520
521 gcc_unreachable();
522 }
523
524 /* Find the binfo for the base subobject of BINFO being initialized by
525 inheriting constructor FNDECL (a member of BINFO), or BINFO if FNDECL is not
526 an inheriting constructor. */
527
528 static tree
529 inherited_ctor_binfo (tree binfo, tree fndecl)
530 {
531 tree inh = DECL_INHERITED_CTOR (fndecl);
532 if (!inh)
533 return binfo;
534
535 tree results = NULL_TREE;
536 for (ovl_iterator iter (inh); iter; ++iter)
537 {
538 tree one = inherited_ctor_binfo_1 (binfo, *iter);
539 if (!results)
540 results = one;
541 else if (one != results)
542 results = tree_cons (NULL_TREE, one, results);
543 }
544 return results;
545 }
546
547 /* Find the binfo for the base subobject being initialized by inheriting
548 constructor FNDECL, or NULL_TREE if FNDECL is not an inheriting
549 constructor. */
550
551 tree
552 inherited_ctor_binfo (tree fndecl)
553 {
554 if (!DECL_INHERITED_CTOR (fndecl))
555 return NULL_TREE;
556 tree binfo = TYPE_BINFO (DECL_CONTEXT (fndecl));
557 return inherited_ctor_binfo (binfo, fndecl);
558 }
559
560
561 /* True if we should omit all user-declared parameters from a base
562 construtor built from complete constructor FN.
563 That's when the ctor is inherited from a virtual base. */
564
565 bool
566 base_ctor_omit_inherited_parms (tree comp_ctor)
567 {
568 gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (comp_ctor));
569
570 if (!flag_new_inheriting_ctors)
571 /* We only optimize away the parameters in the new model. */
572 return false;
573
574 if (!CLASSTYPE_VBASECLASSES (DECL_CONTEXT (comp_ctor)))
575 return false;
576
577 if (FUNCTION_FIRST_USER_PARMTYPE (comp_ctor) == void_list_node)
578 /* No user-declared parameters to omit. */
579 return false;
580
581 for (tree binfo = inherited_ctor_binfo (comp_ctor);
582 binfo;
583 binfo = BINFO_INHERITANCE_CHAIN (binfo))
584 if (BINFO_VIRTUAL_P (binfo))
585 return true;
586
587 return false;
588 }
589
590
591 /* True if we should omit all user-declared parameters from constructor FN,
592 because it is a base clone of a ctor inherited from a virtual base. */
593
594 bool
595 ctor_omit_inherited_parms (tree fn)
596 {
597 gcc_checking_assert (TREE_CODE (fn) == FUNCTION_DECL);
598
599 if (!DECL_BASE_CONSTRUCTOR_P (fn))
600 return false;
601
602 return base_ctor_omit_inherited_parms (DECL_CLONED_FUNCTION (fn));
603 }
604
605 /* True iff constructor(s) INH inherited into BINFO initializes INIT_BINFO.
606 This can be true for multiple virtual bases as well as one direct
607 non-virtual base. */
608
609 static bool
610 binfo_inherited_from (tree binfo, tree init_binfo, tree inh)
611 {
612 /* inh is an OVERLOAD if we inherited the same constructor along
613 multiple paths, check all of them. */
614 for (ovl_iterator iter (inh); iter; ++iter)
615 {
616 tree fn = *iter;
617 tree base = DECL_CONTEXT (fn);
618 tree base_binfo = NULL_TREE;
619 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
620 if (BINFO_TYPE (base_binfo) == base)
621 break;
622 if (base_binfo == init_binfo
623 || (flag_new_inheriting_ctors
624 && binfo_inherited_from (base_binfo, init_binfo,
625 DECL_INHERITED_CTOR (fn))))
626 return true;
627 }
628 return false;
629 }
630
631 /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO
632 given the parameter or parameters PARM, possibly inherited constructor
633 base INH, or move flag MOVE_P. */
634
635 static tree
636 add_one_base_init (tree binfo, tree parm, bool move_p, tree inh,
637 tree member_init_list)
638 {
639 tree init;
640 if (inh)
641 {
642 /* An inheriting constructor only has a mem-initializer for
643 the base it inherits from. */
644 if (!binfo_inherited_from (TYPE_BINFO (current_class_type), binfo, inh))
645 return member_init_list;
646
647 tree *p = &init;
648 init = NULL_TREE;
649 for (; parm; parm = DECL_CHAIN (parm))
650 {
651 tree exp = forward_parm (parm);
652 *p = build_tree_list (NULL_TREE, exp);
653 p = &TREE_CHAIN (*p);
654 }
655 }
656 else
657 {
658 init = build_base_path (PLUS_EXPR, parm, binfo, 1,
659 tf_warning_or_error);
660 if (move_p)
661 init = move (init);
662 init = build_tree_list (NULL_TREE, init);
663 }
664 return tree_cons (binfo, init, member_init_list);
665 }
666
667 /* Generate code for default X(X&) or X(X&&) constructor or an inheriting
668 constructor. */
669
670 static void
671 do_build_copy_constructor (tree fndecl)
672 {
673 tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
674 bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
675 bool trivial = trivial_fn_p (fndecl);
676 tree inh = DECL_INHERITED_CTOR (fndecl);
677
678 if (!inh)
679 parm = convert_from_reference (parm);
680
681 if (trivial)
682 {
683 if (is_empty_class (current_class_type))
684 /* Don't copy the padding byte; it might not have been allocated
685 if *this is a base subobject. */;
686 else if (tree_int_cst_equal (TYPE_SIZE (current_class_type),
687 CLASSTYPE_SIZE (current_class_type)))
688 {
689 tree t = cp_build_init_expr (current_class_ref, parm);
690 finish_expr_stmt (t);
691 }
692 else
693 {
694 /* We must only copy the non-tail padding parts. */
695 tree base_size = CLASSTYPE_SIZE_UNIT (current_class_type);
696 base_size = size_binop (MINUS_EXPR, base_size, size_int (1));
697 tree array_type = build_array_type (unsigned_char_type_node,
698 build_index_type (base_size));
699 tree alias_set = build_int_cst (TREE_TYPE (current_class_ptr), 0);
700 tree lhs = build2 (MEM_REF, array_type,
701 current_class_ptr, alias_set);
702 tree rhs = build2 (MEM_REF, array_type,
703 TREE_OPERAND (parm, 0), alias_set);
704 tree t = cp_build_init_expr (lhs, rhs);
705 finish_expr_stmt (t);
706 }
707 }
708 else
709 {
710 tree member_init_list = NULL_TREE;
711 int i;
712 tree binfo, base_binfo;
713 vec<tree, va_gc> *vbases;
714
715 /* Initialize all the base-classes with the parameter converted
716 to their type so that we get their copy constructor and not
717 another constructor that takes current_class_type. We must
718 deal with the binfo's directly as a direct base might be
719 inaccessible due to ambiguity. */
720 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
721 vec_safe_iterate (vbases, i, &binfo); i++)
722 {
723 member_init_list = add_one_base_init (binfo, parm, move_p, inh,
724 member_init_list);
725 }
726
727 for (binfo = TYPE_BINFO (current_class_type), i = 0;
728 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
729 {
730 if (BINFO_VIRTUAL_P (base_binfo))
731 continue;
732 member_init_list = add_one_base_init (base_binfo, parm, move_p,
733 inh, member_init_list);
734 }
735
736 if (!inh)
737 {
738 int cvquals = cp_type_quals (TREE_TYPE (parm));
739
740 for (tree fields = TYPE_FIELDS (current_class_type);
741 fields; fields = DECL_CHAIN (fields))
742 {
743 tree field = fields;
744 tree expr_type;
745
746 if (TREE_CODE (field) != FIELD_DECL)
747 continue;
748
749 expr_type = TREE_TYPE (field);
750 if (DECL_NAME (field))
751 {
752 if (VFIELD_NAME_P (DECL_NAME (field)))
753 continue;
754 }
755 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
756 /* Just use the field; anonymous types can't have
757 nontrivial copy ctors or assignment ops or this
758 function would be deleted. */;
759 else
760 continue;
761
762 /* Compute the type of "init->field". If the copy-constructor
763 parameter is, for example, "const S&", and the type of
764 the field is "T", then the type will usually be "const
765 T". (There are no cv-qualified variants of reference
766 types.) */
767 if (!TYPE_REF_P (expr_type))
768 {
769 int quals = cvquals;
770
771 if (DECL_MUTABLE_P (field))
772 quals &= ~TYPE_QUAL_CONST;
773 quals |= cp_type_quals (expr_type);
774 expr_type = cp_build_qualified_type (expr_type, quals);
775 }
776
777 tree init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
778 if (move_p && !TYPE_REF_P (expr_type)
779 /* 'move' breaks bit-fields, and has no effect for scalars. */
780 && !scalarish_type_p (expr_type))
781 init = move (init);
782 init = build_tree_list (NULL_TREE, init);
783
784 member_init_list = tree_cons (field, init, member_init_list);
785 }
786 }
787
788 finish_mem_initializers (member_init_list);
789 }
790 }
791
792 static void
793 do_build_copy_assign (tree fndecl)
794 {
795 tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl));
796 tree compound_stmt;
797 bool move_p = move_fn_p (fndecl);
798 bool trivial = trivial_fn_p (fndecl);
799 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
800
801 compound_stmt = begin_compound_stmt (0);
802 parm = convert_from_reference (parm);
803
804 /* If we are building a defaulted xobj copy/move assignment operator then
805 current_class_ref will not have been set up.
806 Kind of an icky hack, but what can ya do? */
807 tree const class_ref = DECL_XOBJ_MEMBER_FUNCTION_P (fndecl)
808 ? cp_build_fold_indirect_ref (DECL_ARGUMENTS (fndecl)) : current_class_ref;
809
810 if (trivial
811 && is_empty_class (current_class_type))
812 /* Don't copy the padding byte; it might not have been allocated
813 if *this is a base subobject. */;
814 else if (trivial)
815 {
816 tree t = build2 (MODIFY_EXPR, void_type_node, class_ref, parm);
817 finish_expr_stmt (t);
818 }
819 else
820 {
821 tree fields;
822 int cvquals = cp_type_quals (TREE_TYPE (parm));
823 int i;
824 tree binfo, base_binfo;
825
826 /* Assign to each of the direct base classes. */
827 for (binfo = TYPE_BINFO (current_class_type), i = 0;
828 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
829 {
830 tree converted_parm;
831
832 /* We must convert PARM directly to the base class
833 explicitly since the base class may be ambiguous. */
834 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
835 tf_warning_or_error);
836 if (move_p)
837 converted_parm = move (converted_parm);
838 /* Call the base class assignment operator. */
839 releasing_vec parmvec (make_tree_vector_single (converted_parm));
840 finish_expr_stmt
841 (build_special_member_call (class_ref,
842 assign_op_identifier,
843 &parmvec,
844 base_binfo,
845 flags,
846 tf_warning_or_error));
847 }
848
849 /* Assign to each of the non-static data members. */
850 for (fields = TYPE_FIELDS (current_class_type);
851 fields;
852 fields = DECL_CHAIN (fields))
853 {
854 tree comp = class_ref;
855 tree init = parm;
856 tree field = fields;
857 tree expr_type;
858 int quals;
859
860 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
861 continue;
862
863 expr_type = TREE_TYPE (field);
864
865 if (CP_TYPE_CONST_P (expr_type))
866 {
867 error ("non-static const member %q#D, cannot use default "
868 "assignment operator", field);
869 continue;
870 }
871 else if (TYPE_REF_P (expr_type))
872 {
873 error ("non-static reference member %q#D, cannot use "
874 "default assignment operator", field);
875 continue;
876 }
877
878 if (DECL_NAME (field))
879 {
880 if (VFIELD_NAME_P (DECL_NAME (field)))
881 continue;
882 }
883 else if (ANON_AGGR_TYPE_P (expr_type)
884 && TYPE_FIELDS (expr_type) != NULL_TREE)
885 /* Just use the field; anonymous types can't have
886 nontrivial copy ctors or assignment ops or this
887 function would be deleted. */;
888 else
889 continue;
890
891 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
892
893 /* Compute the type of init->field */
894 quals = cvquals;
895 if (DECL_MUTABLE_P (field))
896 quals &= ~TYPE_QUAL_CONST;
897 expr_type = cp_build_qualified_type (expr_type, quals);
898
899 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
900 if (move_p && !TYPE_REF_P (expr_type)
901 /* 'move' breaks bit-fields, and has no effect for scalars. */
902 && !scalarish_type_p (expr_type))
903 init = move (init);
904
905 if (DECL_NAME (field))
906 init = cp_build_modify_expr (input_location, comp, NOP_EXPR, init,
907 tf_warning_or_error);
908 else
909 init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
910 finish_expr_stmt (init);
911 }
912 }
913 finish_return_stmt (class_ref);
914 finish_compound_stmt (compound_stmt);
915 }
916
917 /* C++20 <compare> comparison category types. */
918
919 enum comp_cat_tag
920 {
921 cc_partial_ordering,
922 cc_weak_ordering,
923 cc_strong_ordering,
924 cc_last
925 };
926
927 /* Names of the comparison categories and their value members, to be indexed by
928 comp_cat_tag enumerators. genericize_spaceship below relies on the ordering
929 of the members. */
930
931 struct comp_cat_info_t
932 {
933 const char *name;
934 const char *members[4];
935 };
936 static const comp_cat_info_t comp_cat_info[cc_last]
937 = {
938 { "partial_ordering", { "equivalent", "greater", "less", "unordered" } },
939 { "weak_ordering", { "equivalent", "greater", "less" } },
940 { "strong_ordering", { "equal", "greater", "less" } }
941 };
942
943 /* A cache of the category types to speed repeated lookups. */
944
945 static GTY((deletable)) tree comp_cat_cache[cc_last];
946
947 /* Look up one of the result variables in the comparison category type. */
948
949 static tree
950 lookup_comparison_result (tree type, const char *name_str,
951 tsubst_flags_t complain = tf_warning_or_error)
952 {
953 tree name = get_identifier (name_str);
954 tree decl = lookup_qualified_name (type, name);
955 if (TREE_CODE (decl) != VAR_DECL)
956 {
957 if (complain & tf_error)
958 {
959 auto_diagnostic_group d;
960 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
961 qualified_name_lookup_error (type, name, decl, input_location);
962 else
963 error ("%qD is not a static data member", decl);
964 inform (input_location, "determining value of %qs", "operator<=>");
965 }
966 return error_mark_node;
967 }
968 return decl;
969 }
970
971 /* Look up a <compare> comparison category type in std. */
972
973 static tree
974 lookup_comparison_category (comp_cat_tag tag,
975 tsubst_flags_t complain = tf_warning_or_error)
976 {
977 if (tree cached = comp_cat_cache[tag])
978 return cached;
979
980 tree name = get_identifier (comp_cat_info[tag].name);
981 tree decl = lookup_qualified_name (std_node, name);
982 if (TREE_CODE (decl) != TYPE_DECL)
983 {
984 if (complain & tf_error)
985 {
986 auto_diagnostic_group d;
987 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
988 qualified_name_lookup_error (std_node, name, decl, input_location);
989 else
990 error ("%qD is not a type", decl);
991 inform (input_location, "forming type of %qs", "operator<=>");
992 }
993 return error_mark_node;
994 }
995 /* Also make sure we can look up the value members now, since we won't
996 really use them until genericize time. */
997 tree type = TREE_TYPE (decl);
998 for (int i = 0; i < 4; ++i)
999 {
1000 const char *p = comp_cat_info[tag].members[i];
1001 if (!p) break;
1002 if (lookup_comparison_result (type, p, complain)
1003 == error_mark_node)
1004 return error_mark_node;
1005 }
1006 return comp_cat_cache[tag] = type;
1007 }
1008
1009 /* Wrapper that takes the tag rather than the type. */
1010
1011 static tree
1012 lookup_comparison_result (comp_cat_tag tag, const char *name_str,
1013 tsubst_flags_t complain = tf_warning_or_error)
1014 {
1015 tree type = lookup_comparison_category (tag, complain);
1016 return lookup_comparison_result (type, name_str, complain);
1017 }
1018
1019 /* Wrapper that takes the index into the members array instead of the name. */
1020
1021 static tree
1022 lookup_comparison_result (comp_cat_tag tag, tree type, int idx)
1023 {
1024 const char *name_str = comp_cat_info[tag].members[idx];
1025 if (!name_str)
1026 return NULL_TREE;
1027 return lookup_comparison_result (type, name_str);
1028 }
1029
1030 /* Does TYPE correspond to TAG? */
1031
1032 static bool
1033 is_cat (tree type, comp_cat_tag tag)
1034 {
1035 tree name = TYPE_LINKAGE_IDENTIFIER (type);
1036 return id_equal (name, comp_cat_info[tag].name);
1037 }
1038
1039 /* Return the comp_cat_tag for TYPE. */
1040
1041 static comp_cat_tag
1042 cat_tag_for (tree type)
1043 {
1044 if (!CLASS_TYPE_P (type) || !decl_in_std_namespace_p (TYPE_MAIN_DECL (type)))
1045 return cc_last;
1046 for (int i = 0; i < cc_last; ++i)
1047 {
1048 comp_cat_tag tag = (comp_cat_tag)i;
1049 if (is_cat (type, tag))
1050 return tag;
1051 }
1052 return cc_last;
1053 }
1054
1055 /* Return the comparison category tag of a <=> expression with non-class type
1056 OPTYPE. */
1057
1058 static comp_cat_tag
1059 spaceship_comp_cat (tree optype)
1060 {
1061 if (INTEGRAL_OR_ENUMERATION_TYPE_P (optype) || TYPE_PTROBV_P (optype))
1062 return cc_strong_ordering;
1063 else if (SCALAR_FLOAT_TYPE_P (optype))
1064 return cc_partial_ordering;
1065
1066 /* ??? should vector <=> produce a vector of one of the above? */
1067 gcc_unreachable ();
1068 }
1069
1070 /* Return the comparison category type of a <=> expression with non-class type
1071 OPTYPE. */
1072
1073 tree
1074 spaceship_type (tree optype, tsubst_flags_t complain)
1075 {
1076 comp_cat_tag tag = spaceship_comp_cat (optype);
1077 return lookup_comparison_category (tag, complain);
1078 }
1079
1080 /* Turn <=> with type TYPE and operands OP0 and OP1 into GENERIC.
1081 This is also used by build_comparison_op for fallback to op< and op==
1082 in a defaulted op<=>. */
1083
1084 tree
1085 genericize_spaceship (location_t loc, tree type, tree op0, tree op1)
1086 {
1087 /* ??? maybe optimize based on knowledge of representation? */
1088 comp_cat_tag tag = cat_tag_for (type);
1089
1090 if (tag == cc_last && is_auto (type))
1091 {
1092 /* build_comparison_op is checking to see if we want to suggest changing
1093 the op<=> return type from auto to a specific comparison category; any
1094 category will do for now. */
1095 tag = cc_strong_ordering;
1096 type = lookup_comparison_category (tag, tf_none);
1097 if (type == error_mark_node)
1098 return error_mark_node;
1099 }
1100 else if (tag == cc_last)
1101 return error_mark_node;
1102
1103 tree r;
1104 bool scalar = SCALAR_TYPE_P (TREE_TYPE (op0));
1105 if (scalar)
1106 {
1107 op0 = save_expr (op0);
1108 op1 = save_expr (op1);
1109 }
1110
1111 tree gt = lookup_comparison_result (tag, type, 1);
1112
1113 int flags = LOOKUP_NORMAL;
1114 tsubst_flags_t complain = tf_none;
1115 tree comp;
1116
1117 if (tag == cc_partial_ordering)
1118 {
1119 /* op0 == op1 ? equivalent : op0 < op1 ? less :
1120 op1 < op0 ? greater : unordered */
1121 tree uo = lookup_comparison_result (tag, type, 3);
1122 if (scalar)
1123 {
1124 /* For scalars use the low level operations; using build_new_op causes
1125 trouble with constexpr eval in the middle of genericize (100367). */
1126 comp = fold_build2 (LT_EXPR, boolean_type_node, op1, op0);
1127 r = fold_build3 (COND_EXPR, type, comp, gt, uo);
1128 }
1129 else
1130 {
1131 comp = build_new_op (loc, LT_EXPR, flags, op1, op0, complain);
1132 r = build_conditional_expr (loc, comp, gt, uo, complain);
1133 }
1134 }
1135 else
1136 /* op0 == op1 ? equal : op0 < op1 ? less : greater */
1137 r = gt;
1138
1139 tree lt = lookup_comparison_result (tag, type, 2);
1140 if (scalar)
1141 {
1142 comp = fold_build2 (LT_EXPR, boolean_type_node, op0, op1);
1143 r = fold_build3 (COND_EXPR, type, comp, lt, r);
1144 }
1145 else
1146 {
1147 comp = build_new_op (loc, LT_EXPR, flags, op0, op1, complain);
1148 r = build_conditional_expr (loc, comp, lt, r, complain);
1149 }
1150
1151 tree eq = lookup_comparison_result (tag, type, 0);
1152 if (scalar)
1153 {
1154 comp = fold_build2 (EQ_EXPR, boolean_type_node, op0, op1);
1155 r = fold_build3 (COND_EXPR, type, comp, eq, r);
1156 }
1157 else
1158 {
1159 comp = build_new_op (loc, EQ_EXPR, flags, op0, op1, complain);
1160 r = build_conditional_expr (loc, comp, eq, r, complain);
1161 }
1162
1163 return r;
1164 }
1165
1166 /* Check that the signature of a defaulted comparison operator is
1167 well-formed. */
1168
1169 static bool
1170 early_check_defaulted_comparison (tree fn)
1171 {
1172 location_t loc = DECL_SOURCE_LOCATION (fn);
1173 tree ctx;
1174 if (DECL_CLASS_SCOPE_P (fn))
1175 ctx = DECL_CONTEXT (fn);
1176 else
1177 ctx = DECL_FRIEND_CONTEXT (fn);
1178 bool ok = true;
1179
1180 if (cxx_dialect < cxx20)
1181 {
1182 error_at (loc, "defaulted %qD only available with %<-std=c++20%> or "
1183 "%<-std=gnu++20%>", fn);
1184 return false;
1185 }
1186
1187 if (!DECL_OVERLOADED_OPERATOR_IS (fn, SPACESHIP_EXPR)
1188 && !same_type_p (TREE_TYPE (TREE_TYPE (fn)), boolean_type_node))
1189 {
1190 diagnostic_t kind = DK_UNSPECIFIED;
1191 int opt = 0;
1192 if (is_auto (TREE_TYPE (fn)))
1193 kind = DK_PEDWARN;
1194 else
1195 kind = DK_ERROR;
1196 emit_diagnostic (kind, loc, opt,
1197 "defaulted %qD must return %<bool%>", fn);
1198 if (kind == DK_ERROR)
1199 ok = false;
1200 }
1201
1202 bool mem = DECL_IOBJ_MEMBER_FUNCTION_P (fn);
1203 if (mem && type_memfn_quals (TREE_TYPE (fn)) != TYPE_QUAL_CONST)
1204 {
1205 error_at (loc, "defaulted %qD must be %<const%>", fn);
1206 ok = false;
1207 }
1208 if (mem && type_memfn_rqual (TREE_TYPE (fn)) == REF_QUAL_RVALUE)
1209 {
1210 error_at (loc, "defaulted %qD must not have %<&&%> ref-qualifier", fn);
1211 ok = false;
1212 }
1213 tree parmnode = FUNCTION_FIRST_USER_PARMTYPE (fn);
1214 bool saw_byval = false;
1215 bool saw_byref = mem;
1216 bool saw_bad = false;
1217 for (; parmnode != void_list_node; parmnode = TREE_CHAIN (parmnode))
1218 {
1219 tree parmtype = TREE_VALUE (parmnode);
1220 if (CLASS_TYPE_P (parmtype))
1221 saw_byval = true;
1222 else if (TREE_CODE (parmtype) == REFERENCE_TYPE
1223 && !TYPE_REF_IS_RVALUE (parmtype)
1224 && TYPE_QUALS (TREE_TYPE (parmtype)) == TYPE_QUAL_CONST)
1225 {
1226 saw_byref = true;
1227 parmtype = TREE_TYPE (parmtype);
1228 }
1229 else
1230 saw_bad = true;
1231
1232 if (!saw_bad && !ctx)
1233 {
1234 /* Defaulted outside the class body. */
1235 ctx = TYPE_MAIN_VARIANT (parmtype);
1236 if (!is_friend (ctx, fn))
1237 {
1238 auto_diagnostic_group d;
1239 error_at (loc, "defaulted %qD is not a friend of %qT", fn, ctx);
1240 inform (location_of (ctx), "declared here");
1241 ok = false;
1242 }
1243 }
1244 else if (!same_type_ignoring_top_level_qualifiers_p (parmtype, ctx))
1245 saw_bad = true;
1246 }
1247
1248 if (saw_bad || (saw_byval && saw_byref))
1249 {
1250 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
1251 error_at (loc, "defaulted member %qD must have parameter type "
1252 "%<const %T&%>", fn, ctx);
1253 else if (saw_bad)
1254 error_at (loc, "defaulted %qD must have parameters of either type "
1255 "%<const %T&%> or %qT", fn, ctx, ctx);
1256 else
1257 error_at (loc, "defaulted %qD must have parameters of either type "
1258 "%<const %T&%> or %qT, not both", fn, ctx, ctx);
1259 ok = false;
1260 }
1261
1262 /* We still need to deduce deleted/constexpr/noexcept and maybe return. */
1263 DECL_MAYBE_DELETED (fn) = ok;
1264
1265 return ok;
1266 }
1267
1268 /* Subroutine of build_comparison_op. Given the vec of memberwise
1269 comparisons COMPS, calculate the overall comparison category for
1270 operator<=>. */
1271
1272 static tree
1273 common_comparison_type (vec<tree> &comps)
1274 {
1275 tree seen[cc_last] = {};
1276
1277 for (unsigned i = 0; i < comps.length(); ++i)
1278 {
1279 tree comp = comps[i];
1280 if (TREE_CODE (comp) == TREE_LIST)
1281 comp = TREE_VALUE (comp);
1282 tree ctype = TREE_TYPE (comp);
1283 comp_cat_tag tag = cat_tag_for (ctype);
1284 /* build_comparison_op already checked this. */
1285 gcc_checking_assert (tag < cc_last);
1286 seen[tag] = ctype;
1287 }
1288
1289 /* Otherwise, if at least one T i is std::partial_ordering, U is
1290 std::partial_ordering. */
1291 if (tree t = seen[cc_partial_ordering]) return t;
1292
1293 /* Otherwise, if at least one T i is std::weak_ordering, U is
1294 std::weak_ordering. */
1295 if (tree t = seen[cc_weak_ordering]) return t;
1296
1297 /* Otherwise, U is std::strong_ordering. */
1298 if (tree t = seen[cc_strong_ordering]) return t;
1299 return lookup_comparison_category (cc_strong_ordering);
1300 }
1301
1302 /* Data structure for build_comparison_op. */
1303
1304 struct comp_info
1305 {
1306 tree fndecl;
1307 location_t loc;
1308 tsubst_flags_t complain;
1309 tree_code code;
1310 comp_cat_tag retcat;
1311 bool first_time;
1312 bool constexp;
1313 bool was_constexp;
1314 bool noex;
1315
1316 comp_info (tree fndecl, tsubst_flags_t complain)
1317 : fndecl (fndecl), complain (complain)
1318 {
1319 loc = DECL_SOURCE_LOCATION (fndecl);
1320
1321 first_time = DECL_MAYBE_DELETED (fndecl);
1322 DECL_MAYBE_DELETED (fndecl) = false;
1323
1324 /* Do we want to try to set constexpr? */
1325 was_constexp = DECL_DECLARED_CONSTEXPR_P (fndecl);
1326 constexp = first_time;
1327 if (constexp)
1328 /* Set this for var_in_constexpr_fn. */
1329 DECL_DECLARED_CONSTEXPR_P (fndecl) = true;
1330
1331 /* Do we want to try to set noexcept? */
1332 noex = first_time;
1333 if (noex)
1334 {
1335 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fndecl));
1336 if (raises && !UNEVALUATED_NOEXCEPT_SPEC_P (raises))
1337 /* There was an explicit exception-specification. */
1338 noex = false;
1339 }
1340 }
1341
1342 /* EXPR is an expression built as part of the function body.
1343 Adjust the properties appropriately. */
1344 void check (tree expr)
1345 {
1346 if (expr == error_mark_node)
1347 DECL_DELETED_FN (fndecl) = true;
1348 if ((constexp || was_constexp)
1349 && !potential_rvalue_constant_expression (expr))
1350 {
1351 if (was_constexp)
1352 require_potential_rvalue_constant_expression_fncheck (expr);
1353 else
1354 constexp = false;
1355 }
1356 if (noex && !expr_noexcept_p (expr, tf_none))
1357 noex = false;
1358 }
1359
1360 ~comp_info ()
1361 {
1362 if (first_time)
1363 {
1364 DECL_DECLARED_CONSTEXPR_P (fndecl) = constexp || was_constexp;
1365 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fndecl));
1366 if (!raises || UNEVALUATED_NOEXCEPT_SPEC_P (raises))
1367 {
1368 raises = noex ? noexcept_true_spec : noexcept_false_spec;
1369 TREE_TYPE (fndecl) = build_exception_variant (TREE_TYPE (fndecl),
1370 raises);
1371 }
1372 }
1373 }
1374 };
1375
1376 /* Subroutine of build_comparison_op, to compare a single subobject. */
1377
1378 static tree
1379 do_one_comp (location_t loc, const comp_info &info, tree sub, tree lhs, tree rhs)
1380 {
1381 const tree_code code = info.code;
1382 const tree fndecl = info.fndecl;
1383 const comp_cat_tag retcat = info.retcat;
1384 const tsubst_flags_t complain = info.complain;
1385
1386 tree overload = NULL_TREE;
1387 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
1388 /* If we have an explicit comparison category return type we can fall back
1389 to </=, so don't give an error yet if <=> lookup fails. */
1390 bool tentative = retcat != cc_last;
1391 tree comp = build_new_op (loc, code, flags, lhs, rhs,
1392 NULL_TREE, NULL_TREE, &overload,
1393 tentative ? tf_none : complain);
1394
1395 if (code != SPACESHIP_EXPR)
1396 return comp;
1397
1398 tree rettype = TREE_TYPE (TREE_TYPE (fndecl));
1399
1400 if (comp == error_mark_node)
1401 {
1402 if (overload == NULL_TREE && (tentative || complain))
1403 {
1404 /* No viable <=>, try using op< and op==. */
1405 tree lteq = genericize_spaceship (loc, rettype, lhs, rhs);
1406 if (lteq != error_mark_node)
1407 {
1408 /* We found usable < and ==. */
1409 if (retcat != cc_last)
1410 /* Return type is a comparison category, use them. */
1411 comp = lteq;
1412 else if (complain & tf_error)
1413 /* Return type is auto, suggest changing it. */
1414 inform (info.loc, "changing the return type from %qs "
1415 "to a comparison category type will allow the "
1416 "comparison to use %qs and %qs", "auto",
1417 "operator<", "operator==");
1418 }
1419 else if (tentative && complain)
1420 /* No usable < and ==, give an error for op<=>. */
1421 build_new_op (loc, code, flags, lhs, rhs, complain);
1422 }
1423 if (comp == error_mark_node)
1424 return error_mark_node;
1425 }
1426
1427 if (FNDECL_USED_AUTO (fndecl)
1428 && cat_tag_for (TREE_TYPE (comp)) == cc_last)
1429 {
1430 /* The operator function is defined as deleted if ... Ri is not a
1431 comparison category type. */
1432 if (complain & tf_error)
1433 inform (loc,
1434 "three-way comparison of %qD has type %qT, not a "
1435 "comparison category type", sub, TREE_TYPE (comp));
1436 return error_mark_node;
1437 }
1438 else if (!FNDECL_USED_AUTO (fndecl)
1439 && !can_convert (rettype, TREE_TYPE (comp), complain))
1440 {
1441 if (complain & tf_error)
1442 error_at (loc,
1443 "three-way comparison of %qD has type %qT, which "
1444 "does not convert to %qT",
1445 sub, TREE_TYPE (comp), rettype);
1446 return error_mark_node;
1447 }
1448
1449 return comp;
1450 }
1451
1452 /* Build up the definition of a defaulted comparison operator. Unlike other
1453 defaulted functions that use synthesized_method_walk to determine whether
1454 the function is e.g. deleted, for comparisons we use the same code. We try
1455 to use synthesize_method at the earliest opportunity and bail out if the
1456 function ends up being deleted. */
1457
1458 void
1459 build_comparison_op (tree fndecl, bool defining, tsubst_flags_t complain)
1460 {
1461 comp_info info (fndecl, complain);
1462
1463 if (!defining && !(complain & tf_error) && !DECL_MAYBE_DELETED (fndecl))
1464 return;
1465
1466 int flags = LOOKUP_NORMAL;
1467 const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (DECL_NAME (fndecl));
1468 tree_code code = info.code = op->tree_code;
1469
1470 tree lhs = DECL_ARGUMENTS (fndecl);
1471 tree rhs = DECL_CHAIN (lhs);
1472 if (is_this_parameter (lhs))
1473 lhs = cp_build_fold_indirect_ref (lhs);
1474 else
1475 lhs = convert_from_reference (lhs);
1476 rhs = convert_from_reference (rhs);
1477 tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (lhs));
1478 gcc_assert (!defining || COMPLETE_TYPE_P (ctype));
1479
1480 iloc_sentinel ils (info.loc);
1481
1482 /* A defaulted comparison operator function for class C is defined as
1483 deleted if ... C has variant members. */
1484 if (TREE_CODE (ctype) == UNION_TYPE
1485 && next_aggregate_field (TYPE_FIELDS (ctype)))
1486 {
1487 if (complain & tf_error)
1488 inform (info.loc, "cannot default compare union %qT", ctype);
1489 DECL_DELETED_FN (fndecl) = true;
1490 return;
1491 }
1492
1493 tree compound_stmt = NULL_TREE;
1494 if (defining)
1495 compound_stmt = begin_compound_stmt (0);
1496 else
1497 ++cp_unevaluated_operand;
1498
1499 tree rettype = TREE_TYPE (TREE_TYPE (fndecl));
1500 if (code != SPACESHIP_EXPR && is_auto (rettype))
1501 {
1502 rettype = boolean_type_node;
1503 apply_deduced_return_type (fndecl, rettype);
1504 }
1505
1506 if (code == EQ_EXPR || code == SPACESHIP_EXPR)
1507 {
1508 comp_cat_tag &retcat = (info.retcat = cc_last);
1509 if (code == SPACESHIP_EXPR && !FNDECL_USED_AUTO (fndecl))
1510 retcat = cat_tag_for (rettype);
1511
1512 bool bad = false;
1513 auto_vec<tree> comps;
1514
1515 /* Compare the base subobjects. We handle them this way, rather than in
1516 the field loop below, because maybe_instantiate_noexcept might bring
1517 us here before we've built the base fields. */
1518 for (tree base_binfo : BINFO_BASE_BINFOS (TYPE_BINFO (ctype)))
1519 {
1520 tree lhs_base
1521 = build_base_path (PLUS_EXPR, lhs, base_binfo, 0, complain);
1522 tree rhs_base
1523 = build_base_path (PLUS_EXPR, rhs, base_binfo, 0, complain);
1524
1525 location_t loc = DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (ctype));
1526 tree comp = do_one_comp (loc, info, BINFO_TYPE (base_binfo),
1527 lhs_base, rhs_base);
1528 if (comp == error_mark_node)
1529 {
1530 bad = true;
1531 continue;
1532 }
1533
1534 comps.safe_push (comp);
1535 }
1536
1537 /* Now compare the field subobjects. */
1538 for (tree field = next_aggregate_field (TYPE_FIELDS (ctype));
1539 field;
1540 field = next_aggregate_field (DECL_CHAIN (field)))
1541 {
1542 if (DECL_VIRTUAL_P (field) || DECL_FIELD_IS_BASE (field))
1543 /* We ignore the vptr, and we already handled bases. */
1544 continue;
1545
1546 tree expr_type = TREE_TYPE (field);
1547
1548 location_t field_loc = DECL_SOURCE_LOCATION (field);
1549
1550 /* A defaulted comparison operator function for class C is defined as
1551 deleted if any non-static data member of C is of reference type or
1552 C has variant members. */
1553 if (TREE_CODE (expr_type) == REFERENCE_TYPE)
1554 {
1555 if (complain & tf_error)
1556 inform (field_loc, "cannot default compare "
1557 "reference member %qD", field);
1558 bad = true;
1559 continue;
1560 }
1561 else if (ANON_UNION_TYPE_P (expr_type)
1562 && next_aggregate_field (TYPE_FIELDS (expr_type)))
1563 {
1564 if (complain & tf_error)
1565 inform (field_loc, "cannot default compare "
1566 "anonymous union member");
1567 bad = true;
1568 continue;
1569 }
1570
1571 tree lhs_mem = build3_loc (field_loc, COMPONENT_REF, expr_type, lhs,
1572 field, NULL_TREE);
1573 tree rhs_mem = build3_loc (field_loc, COMPONENT_REF, expr_type, rhs,
1574 field, NULL_TREE);
1575 tree loop_indexes = NULL_TREE;
1576 while (TREE_CODE (expr_type) == ARRAY_TYPE)
1577 {
1578 /* Flexible array member. */
1579 if (TYPE_DOMAIN (expr_type) == NULL_TREE
1580 || TYPE_MAX_VALUE (TYPE_DOMAIN (expr_type)) == NULL_TREE)
1581 {
1582 if (complain & tf_error)
1583 inform (field_loc, "cannot default compare "
1584 "flexible array member");
1585 bad = true;
1586 break;
1587 }
1588 tree maxval = TYPE_MAX_VALUE (TYPE_DOMAIN (expr_type));
1589 /* [0] array. No subobjects to compare, just skip it. */
1590 if (integer_all_onesp (maxval))
1591 break;
1592 tree idx;
1593 /* [1] array, no loop needed, just add [0] ARRAY_REF.
1594 Similarly if !defining. */
1595 if (integer_zerop (maxval) || !defining)
1596 idx = size_zero_node;
1597 /* Some other array, will need runtime loop. */
1598 else
1599 {
1600 idx = get_internal_target_expr (maxval);
1601 loop_indexes = tree_cons (idx, NULL_TREE, loop_indexes);
1602 }
1603 expr_type = TREE_TYPE (expr_type);
1604 lhs_mem = build4_loc (field_loc, ARRAY_REF, expr_type, lhs_mem,
1605 idx, NULL_TREE, NULL_TREE);
1606 rhs_mem = build4_loc (field_loc, ARRAY_REF, expr_type, rhs_mem,
1607 idx, NULL_TREE, NULL_TREE);
1608 }
1609 if (TREE_CODE (expr_type) == ARRAY_TYPE)
1610 continue;
1611
1612 tree comp = do_one_comp (field_loc, info, field, lhs_mem, rhs_mem);
1613 if (comp == error_mark_node)
1614 {
1615 bad = true;
1616 continue;
1617 }
1618
1619 /* Most of the time, comp is the expression that should be evaluated
1620 to compare the two members. If the expression needs to be
1621 evaluated more than once in a loop, it will be a TREE_LIST
1622 instead, whose TREE_VALUE is the expression for one array element,
1623 TREE_PURPOSE is innermost iterator temporary and if the array
1624 is multidimensional, TREE_CHAIN will contain another TREE_LIST
1625 with second innermost iterator in its TREE_PURPOSE and so on. */
1626 if (loop_indexes)
1627 {
1628 TREE_VALUE (loop_indexes) = comp;
1629 comp = loop_indexes;
1630 }
1631 comps.safe_push (comp);
1632 }
1633 if (code == SPACESHIP_EXPR && is_auto (rettype))
1634 {
1635 rettype = common_comparison_type (comps);
1636 apply_deduced_return_type (fndecl, rettype);
1637 }
1638 tree retvaleq;
1639 if (code == EQ_EXPR)
1640 retvaleq = boolean_true_node;
1641 else
1642 {
1643 tree seql = lookup_comparison_result (cc_strong_ordering,
1644 "equal", complain);
1645 retvaleq = build_static_cast (input_location, rettype, seql,
1646 complain);
1647 if (retvaleq == error_mark_node)
1648 bad = true;
1649 }
1650 if (bad)
1651 {
1652 DECL_DELETED_FN (fndecl) = true;
1653 goto out;
1654 }
1655 for (unsigned i = 0; i < comps.length(); ++i)
1656 {
1657 tree comp = comps[i];
1658 tree eq, retval = NULL_TREE, if_ = NULL_TREE;
1659 tree loop_indexes = NULL_TREE;
1660 if (defining)
1661 {
1662 if (TREE_CODE (comp) == TREE_LIST)
1663 {
1664 loop_indexes = comp;
1665 comp = TREE_VALUE (comp);
1666 loop_indexes = nreverse (loop_indexes);
1667 for (tree loop_index = loop_indexes; loop_index;
1668 loop_index = TREE_CHAIN (loop_index))
1669 {
1670 tree for_stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
1671 tree idx = TREE_PURPOSE (loop_index);
1672 tree maxval = TARGET_EXPR_INITIAL (idx);
1673 TARGET_EXPR_INITIAL (idx) = size_zero_node;
1674 add_stmt (idx);
1675 finish_init_stmt (for_stmt);
1676 finish_for_cond (build2 (LE_EXPR, boolean_type_node, idx,
1677 maxval), for_stmt, false, 0,
1678 false);
1679 finish_for_expr (cp_build_unary_op (PREINCREMENT_EXPR,
1680 TARGET_EXPR_SLOT (idx),
1681 false, complain),
1682 for_stmt);
1683 /* Store in TREE_VALUE the for_stmt tree, so that we can
1684 later on call finish_for_stmt on it (in the reverse
1685 order). */
1686 TREE_VALUE (loop_index) = for_stmt;
1687 }
1688 loop_indexes = nreverse (loop_indexes);
1689 }
1690 if_ = begin_if_stmt ();
1691 }
1692 /* Spaceship is specified to use !=, but for the comparison category
1693 types, != is equivalent to !(==), so let's use == directly. */
1694 if (code == EQ_EXPR)
1695 {
1696 /* if (x==y); else return false; */
1697 eq = comp;
1698 retval = boolean_false_node;
1699 }
1700 else
1701 {
1702 /* if (auto v = x<=>y, v == 0); else return v; */
1703 if (TREE_CODE (comp) == SPACESHIP_EXPR)
1704 TREE_TYPE (comp) = rettype;
1705 else
1706 comp = build_static_cast (input_location, rettype, comp,
1707 complain);
1708 info.check (comp);
1709 if (defining)
1710 {
1711 tree var = create_temporary_var (rettype);
1712 DECL_NAME (var) = get_identifier ("retval");
1713 pushdecl (var);
1714 cp_finish_decl (var, comp, false, NULL_TREE, flags);
1715 comp = retval = var;
1716 }
1717 eq = build_new_op (info.loc, EQ_EXPR, flags, comp,
1718 integer_zero_node, NULL_TREE, NULL_TREE,
1719 NULL, complain);
1720 }
1721 tree ceq = contextual_conv_bool (eq, complain);
1722 info.check (ceq);
1723 if (defining)
1724 {
1725 finish_if_stmt_cond (ceq, if_);
1726 finish_then_clause (if_);
1727 begin_else_clause (if_);
1728 finish_return_stmt (retval);
1729 finish_else_clause (if_);
1730 finish_if_stmt (if_);
1731 for (tree loop_index = loop_indexes; loop_index;
1732 loop_index = TREE_CHAIN (loop_index))
1733 finish_for_stmt (TREE_VALUE (loop_index));
1734 }
1735 }
1736 if (defining)
1737 finish_return_stmt (retvaleq);
1738 }
1739 else if (code == NE_EXPR)
1740 {
1741 tree comp = build_new_op (info.loc, EQ_EXPR, flags, lhs, rhs,
1742 NULL_TREE, NULL_TREE, NULL, complain);
1743 comp = contextual_conv_bool (comp, complain);
1744 info.check (comp);
1745 if (defining)
1746 {
1747 tree neg = build1 (TRUTH_NOT_EXPR, boolean_type_node, comp);
1748 finish_return_stmt (neg);
1749 }
1750 }
1751 else
1752 {
1753 tree comp = build_new_op (info.loc, SPACESHIP_EXPR, flags, lhs, rhs,
1754 NULL_TREE, NULL_TREE, NULL, complain);
1755 tree comp2 = build_new_op (info.loc, code, flags, comp, integer_zero_node,
1756 NULL_TREE, NULL_TREE, NULL, complain);
1757 info.check (comp2);
1758 if (defining)
1759 finish_return_stmt (comp2);
1760 }
1761
1762 out:
1763 if (defining)
1764 finish_compound_stmt (compound_stmt);
1765 else
1766 --cp_unevaluated_operand;
1767 }
1768
1769 /* True iff DECL is an implicitly-declared special member function with no real
1770 source location, so we can use its DECL_SOURCE_LOCATION to remember where we
1771 triggered its synthesis. */
1772
1773 bool
1774 decl_remember_implicit_trigger_p (tree decl)
1775 {
1776 if (!DECL_ARTIFICIAL (decl))
1777 return false;
1778 special_function_kind sfk = special_function_p (decl);
1779 /* Inherited constructors have the location of their using-declaration, and
1780 operator== has the location of the corresponding operator<=>. */
1781 return (sfk != sfk_inheriting_constructor
1782 && sfk != sfk_comparison);
1783 }
1784
1785 /* Synthesize FNDECL, a non-static member function. */
1786
1787 void
1788 synthesize_method (tree fndecl)
1789 {
1790 bool need_body = true;
1791 tree stmt;
1792 location_t save_input_location = input_location;
1793 int error_count = errorcount;
1794 int warning_count = warningcount + werrorcount;
1795 special_function_kind sfk = special_function_p (fndecl);
1796 auto_diagnostic_group d;
1797
1798 /* Reset the source location, we might have been previously
1799 deferred, and thus have saved where we were first needed. */
1800 if (decl_remember_implicit_trigger_p (fndecl))
1801 DECL_SOURCE_LOCATION (fndecl)
1802 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
1803
1804 /* If we've been asked to synthesize a clone, just synthesize the
1805 cloned function instead. Doing so will automatically fill in the
1806 body for the clone. */
1807 if (DECL_CLONED_FUNCTION_P (fndecl))
1808 fndecl = DECL_CLONED_FUNCTION (fndecl);
1809
1810 /* We may be in the middle of deferred access check. Disable
1811 it now. */
1812 push_deferring_access_checks (dk_no_deferred);
1813
1814 bool push_to_top = maybe_push_to_top_level (fndecl);
1815
1816 input_location = DECL_SOURCE_LOCATION (fndecl);
1817
1818 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
1819 stmt = begin_function_body ();
1820
1821 if (DECL_ASSIGNMENT_OPERATOR_P (fndecl)
1822 && DECL_OVERLOADED_OPERATOR_IS (fndecl, NOP_EXPR))
1823 {
1824 do_build_copy_assign (fndecl);
1825 need_body = false;
1826 }
1827 else if (DECL_CONSTRUCTOR_P (fndecl))
1828 {
1829 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
1830 if (arg_chain != void_list_node)
1831 do_build_copy_constructor (fndecl);
1832 else
1833 finish_mem_initializers (NULL_TREE);
1834 }
1835 else if (sfk == sfk_comparison)
1836 {
1837 /* Pass tf_none so the function is just deleted if there's a problem. */
1838 build_comparison_op (fndecl, true, tf_none);
1839 need_body = false;
1840 }
1841
1842 /* If we haven't yet generated the body of the function, just
1843 generate an empty compound statement. */
1844 if (need_body)
1845 {
1846 tree compound_stmt;
1847 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
1848 finish_compound_stmt (compound_stmt);
1849 }
1850
1851 finish_function_body (stmt);
1852 finish_function (/*inline_p=*/false);
1853
1854 /* Remember that we were defined in this module. */
1855 set_instantiating_module (fndecl);
1856
1857 if (!DECL_DELETED_FN (fndecl))
1858 expand_or_defer_fn (fndecl);
1859
1860 input_location = save_input_location;
1861
1862 maybe_pop_from_top_level (push_to_top);
1863
1864 pop_deferring_access_checks ();
1865
1866 if (error_count != errorcount || warning_count != warningcount + werrorcount)
1867 if (DECL_ARTIFICIAL (fndecl))
1868 inform (input_location, "synthesized method %qD first required here",
1869 fndecl);
1870 }
1871
1872 /* Like synthesize_method, but don't actually synthesize defaulted comparison
1873 methods if their class is still incomplete. Just deduce the return
1874 type in that case. */
1875
1876 void
1877 maybe_synthesize_method (tree fndecl)
1878 {
1879 if (special_function_p (fndecl) == sfk_comparison)
1880 {
1881 tree lhs = DECL_ARGUMENTS (fndecl);
1882 if (is_this_parameter (lhs))
1883 lhs = cp_build_fold_indirect_ref (lhs);
1884 else
1885 lhs = convert_from_reference (lhs);
1886 tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (lhs));
1887 if (!COMPLETE_TYPE_P (ctype))
1888 {
1889 push_deferring_access_checks (dk_no_deferred);
1890 build_comparison_op (fndecl, false, tf_none);
1891 pop_deferring_access_checks ();
1892 return;
1893 }
1894 }
1895 return synthesize_method (fndecl);
1896 }
1897
1898 /* Build a reference to type TYPE with cv-quals QUALS, which is an
1899 rvalue if RVALUE is true. */
1900
1901 static tree
1902 build_stub_type (tree type, int quals, bool rvalue)
1903 {
1904 tree argtype = cp_build_qualified_type (type, quals);
1905 return cp_build_reference_type (argtype, rvalue);
1906 }
1907
1908 /* Build a dummy glvalue from dereferencing a dummy reference of type
1909 REFTYPE. */
1910
1911 tree
1912 build_stub_object (tree reftype)
1913 {
1914 if (!TYPE_REF_P (reftype))
1915 reftype = cp_build_reference_type (reftype, /*rval*/true);
1916 tree stub = build1 (CONVERT_EXPR, reftype, integer_one_node);
1917 return convert_from_reference (stub);
1918 }
1919
1920 /* True iff EXPR is the result of build_stub_object. */
1921
1922 bool
1923 is_stub_object (tree expr)
1924 {
1925 if (!REFERENCE_REF_P (expr))
1926 return false;
1927 expr = TREE_OPERAND (expr, 0);
1928 return (TREE_CODE (expr) == CONVERT_EXPR
1929 && TREE_OPERAND (expr, 0) == integer_one_node);
1930 }
1931
1932 /* Build a std::declval<TYPE>() expression and return it. */
1933
1934 tree
1935 build_trait_object (tree type)
1936 {
1937 /* TYPE can't be a function with cv-/ref-qualifiers: std::declval is
1938 defined as
1939
1940 template<class T>
1941 typename std::add_rvalue_reference<T>::type declval() noexcept;
1942
1943 and std::add_rvalue_reference yields T when T is a function with
1944 cv- or ref-qualifiers, making the definition ill-formed. */
1945 if (FUNC_OR_METHOD_TYPE_P (type)
1946 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
1947 || type_memfn_rqual (type) != REF_QUAL_NONE))
1948 return error_mark_node;
1949
1950 return build_stub_object (type);
1951 }
1952
1953 /* [func.require] Build an expression of INVOKE(FN_TYPE, ARG_TYPES...). If the
1954 given is not invocable, returns error_mark_node. */
1955
1956 tree
1957 build_invoke (tree fn_type, const_tree arg_types, tsubst_flags_t complain)
1958 {
1959 if (error_operand_p (fn_type) || error_operand_p (arg_types))
1960 return error_mark_node;
1961
1962 gcc_assert (TYPE_P (fn_type));
1963 gcc_assert (TREE_CODE (arg_types) == TREE_VEC);
1964
1965 /* Access check is required to determine if the given is invocable. */
1966 deferring_access_check_sentinel acs (dk_no_deferred);
1967
1968 /* INVOKE is an unevaluated context. */
1969 cp_unevaluated cp_uneval_guard;
1970
1971 bool is_ptrdatamem;
1972 bool is_ptrmemfunc;
1973 if (TREE_CODE (fn_type) == REFERENCE_TYPE)
1974 {
1975 tree non_ref_fn_type = TREE_TYPE (fn_type);
1976 is_ptrdatamem = TYPE_PTRDATAMEM_P (non_ref_fn_type);
1977 is_ptrmemfunc = TYPE_PTRMEMFUNC_P (non_ref_fn_type);
1978
1979 /* Dereference fn_type if it is a pointer to member. */
1980 if (is_ptrdatamem || is_ptrmemfunc)
1981 fn_type = non_ref_fn_type;
1982 }
1983 else
1984 {
1985 is_ptrdatamem = TYPE_PTRDATAMEM_P (fn_type);
1986 is_ptrmemfunc = TYPE_PTRMEMFUNC_P (fn_type);
1987 }
1988
1989 if (is_ptrdatamem && TREE_VEC_LENGTH (arg_types) != 1)
1990 {
1991 if (complain & tf_error)
1992 error ("pointer to data member type %qT can only be invoked with "
1993 "one argument", fn_type);
1994 return error_mark_node;
1995 }
1996 if (is_ptrmemfunc && TREE_VEC_LENGTH (arg_types) == 0)
1997 {
1998 if (complain & tf_error)
1999 error ("pointer to member function type %qT must be invoked with "
2000 "at least one argument", fn_type);
2001 return error_mark_node;
2002 }
2003
2004 /* Construct an expression of a pointer to member. */
2005 tree ptrmem_expr;
2006 if (is_ptrdatamem || is_ptrmemfunc)
2007 {
2008 tree datum_type = TREE_VEC_ELT (arg_types, 0);
2009 tree non_ref_datum_type = datum_type;
2010 if (TYPE_REF_P (datum_type))
2011 non_ref_datum_type = TREE_TYPE (datum_type);
2012
2013 /* datum must be a class type or a pointer to a class type. */
2014 if (!CLASS_TYPE_P (non_ref_datum_type)
2015 && !(POINTER_TYPE_P (non_ref_datum_type)
2016 && CLASS_TYPE_P (TREE_TYPE (non_ref_datum_type))))
2017 {
2018 if (complain & tf_error)
2019 error ("first argument type %qT of a pointer to member must be a "
2020 "class type or a pointer to a class type", datum_type);
2021 return error_mark_node;
2022 }
2023
2024 /* 1.1 & 1.4. */
2025 tree ptrmem_class_type = TYPE_PTRMEM_CLASS_TYPE (fn_type);
2026 const bool ptrmem_is_same_or_base_of_datum =
2027 (same_type_ignoring_top_level_qualifiers_p (ptrmem_class_type,
2028 non_ref_datum_type)
2029 || (NON_UNION_CLASS_TYPE_P (ptrmem_class_type)
2030 && NON_UNION_CLASS_TYPE_P (non_ref_datum_type)
2031 && DERIVED_FROM_P (ptrmem_class_type, non_ref_datum_type)));
2032
2033 bool datum_is_refwrap = false;
2034 if (!ptrmem_is_same_or_base_of_datum && CLASS_TYPE_P (non_ref_datum_type))
2035 {
2036 tree datum_decl = TYPE_NAME (TYPE_MAIN_VARIANT (non_ref_datum_type));
2037 if (decl_in_std_namespace_p (datum_decl))
2038 {
2039 const_tree name = DECL_NAME (datum_decl);
2040 if (name && (id_equal (name, "reference_wrapper")))
2041 {
2042 /* 1.2 & 1.5: Retrieve T& from std::reference_wrapper<T>,
2043 i.e., decltype(datum.get()). */
2044 datum_type =
2045 TREE_VEC_ELT (TYPE_TI_ARGS (non_ref_datum_type), 0);
2046 datum_type = cp_build_reference_type (datum_type, false);
2047 datum_is_refwrap = true;
2048 }
2049 }
2050 }
2051
2052 tree datum_expr = build_trait_object (datum_type);
2053 if (!ptrmem_is_same_or_base_of_datum && !datum_is_refwrap)
2054 /* 1.3 & 1.6: Try to dereference datum_expr. */
2055 datum_expr = build_x_indirect_ref (UNKNOWN_LOCATION, datum_expr,
2056 RO_UNARY_STAR, NULL_TREE, complain);
2057
2058 tree fn_expr = build_trait_object (fn_type);
2059 ptrmem_expr = build_m_component_ref (datum_expr, fn_expr, complain);
2060
2061 if (error_operand_p (ptrmem_expr))
2062 return error_mark_node;
2063
2064 if (is_ptrdatamem)
2065 return ptrmem_expr;
2066 }
2067
2068 /* Construct expressions for arguments to INVOKE. For a pointer to member
2069 function, the first argument, which is the object, is not arguments to
2070 the function. */
2071 releasing_vec args;
2072 for (int i = is_ptrmemfunc ? 1 : 0; i < TREE_VEC_LENGTH (arg_types); ++i)
2073 {
2074 tree arg_type = TREE_VEC_ELT (arg_types, i);
2075 tree arg = build_trait_object (arg_type);
2076 vec_safe_push (args, arg);
2077 }
2078
2079 tree invoke_expr;
2080 if (is_ptrmemfunc)
2081 invoke_expr = build_offset_ref_call_from_tree (ptrmem_expr, &args,
2082 complain);
2083 else /* 1.7. */
2084 invoke_expr = finish_call_expr (build_trait_object (fn_type), &args, false,
2085 false, complain);
2086 return invoke_expr;
2087 }
2088
2089 /* Determine which function will be called when looking up NAME in TYPE,
2090 called with a single ARGTYPE argument, or no argument if ARGTYPE is
2091 null. FLAGS and COMPLAIN are as for build_new_method_call.
2092
2093 Returns a FUNCTION_DECL if all is well.
2094 Returns NULL_TREE if overload resolution failed.
2095 Returns error_mark_node if the chosen function cannot be called. */
2096
2097 static tree
2098 locate_fn_flags (tree type, tree name, tree argtype, int flags,
2099 tsubst_flags_t complain)
2100 {
2101 tree ob, fn, fns, binfo, rval;
2102
2103 if (TYPE_P (type))
2104 binfo = TYPE_BINFO (type);
2105 else
2106 {
2107 binfo = type;
2108 type = BINFO_TYPE (binfo);
2109 }
2110
2111 ob = build_stub_object (cp_build_reference_type (type, false));
2112 releasing_vec args;
2113 if (argtype)
2114 {
2115 if (TREE_CODE (argtype) == TREE_LIST)
2116 {
2117 for (tree elt = argtype; elt && elt != void_list_node;
2118 elt = TREE_CHAIN (elt))
2119 {
2120 tree type = TREE_VALUE (elt);
2121 tree arg = build_stub_object (type);
2122 vec_safe_push (args, arg);
2123 }
2124 }
2125 else
2126 {
2127 tree arg = build_stub_object (argtype);
2128 args->quick_push (arg);
2129 }
2130 }
2131
2132 fns = lookup_fnfields (binfo, name, 0, complain);
2133 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
2134
2135 if (fn && rval == error_mark_node)
2136 return rval;
2137 else
2138 return fn;
2139 }
2140
2141 /* Locate the dtor of TYPE. */
2142
2143 tree
2144 get_dtor (tree type, tsubst_flags_t complain)
2145 {
2146 tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
2147 LOOKUP_NORMAL, complain);
2148 if (fn == error_mark_node)
2149 return NULL_TREE;
2150 return fn;
2151 }
2152
2153 /* Locate the default ctor of TYPE. */
2154
2155 tree
2156 locate_ctor (tree type)
2157 {
2158 tree fn;
2159
2160 push_deferring_access_checks (dk_no_check);
2161 fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
2162 LOOKUP_SPECULATIVE, tf_none);
2163 pop_deferring_access_checks ();
2164 if (fn == error_mark_node)
2165 return NULL_TREE;
2166 return fn;
2167 }
2168
2169 /* Likewise, but give any appropriate errors. */
2170
2171 tree
2172 get_default_ctor (tree type)
2173 {
2174 tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
2175 LOOKUP_NORMAL, tf_warning_or_error);
2176 if (fn == error_mark_node)
2177 return NULL_TREE;
2178 return fn;
2179 }
2180
2181 /* Locate the copy ctor of TYPE. */
2182
2183 tree
2184 get_copy_ctor (tree type, tsubst_flags_t complain)
2185 {
2186 int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
2187 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
2188 tree argtype = build_stub_type (type, quals, false);
2189 tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
2190 LOOKUP_NORMAL, complain);
2191 if (fn == error_mark_node)
2192 return NULL_TREE;
2193 return fn;
2194 }
2195
2196 /* Locate the copy assignment operator of TYPE. */
2197
2198 tree
2199 get_copy_assign (tree type)
2200 {
2201 int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
2202 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
2203 tree argtype = build_stub_type (type, quals, false);
2204 tree fn = locate_fn_flags (type, assign_op_identifier, argtype,
2205 LOOKUP_NORMAL, tf_warning_or_error);
2206 if (fn == error_mark_node)
2207 return NULL_TREE;
2208 return fn;
2209 }
2210
2211 /* walk_tree helper function for is_trivially_xible. If *TP is a call,
2212 return it if it calls something other than a trivial special member
2213 function. */
2214
2215 static tree
2216 check_nontriv (tree *tp, int *, void *)
2217 {
2218 tree fn = cp_get_callee (*tp);
2219 if (fn == NULL_TREE)
2220 return NULL_TREE;
2221
2222 if (TREE_CODE (fn) == ADDR_EXPR)
2223 fn = TREE_OPERAND (fn, 0);
2224
2225 if (TREE_CODE (fn) != FUNCTION_DECL
2226 || !trivial_fn_p (fn))
2227 return fn;
2228 return NULL_TREE;
2229 }
2230
2231 /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */
2232
2233 static tree
2234 assignable_expr (tree to, tree from)
2235 {
2236 cp_unevaluated cp_uneval_guard;
2237 to = build_trait_object (to);
2238 from = build_trait_object (from);
2239 tree r = cp_build_modify_expr (input_location, to, NOP_EXPR, from, tf_none);
2240 return r;
2241 }
2242
2243 /* The predicate condition for a template specialization
2244 is_constructible<T, Args...> shall be satisfied if and only if the
2245 following variable definition would be well-formed for some invented
2246 variable t: T t(create<Args>()...);
2247
2248 Return something equivalent in well-formedness and triviality. */
2249
2250 static tree
2251 constructible_expr (tree to, tree from)
2252 {
2253 tree expr;
2254 cp_unevaluated cp_uneval_guard;
2255 const int len = TREE_VEC_LENGTH (from);
2256 if (CLASS_TYPE_P (to))
2257 {
2258 tree ctype = to;
2259 vec<tree, va_gc> *args = NULL;
2260 if (!TYPE_REF_P (to))
2261 to = cp_build_reference_type (to, /*rval*/false);
2262 tree ob = build_stub_object (to);
2263 if (len == 0)
2264 expr = build_value_init (ctype, tf_none);
2265 else
2266 {
2267 vec_alloc (args, len);
2268 for (tree arg : tree_vec_range (from))
2269 args->quick_push (build_stub_object (arg));
2270 expr = build_special_member_call (ob, complete_ctor_identifier, &args,
2271 ctype, LOOKUP_NORMAL, tf_none);
2272 }
2273 if (expr == error_mark_node)
2274 return error_mark_node;
2275 /* The current state of the standard vis-a-vis LWG 2116 is that
2276 is_*constructible involves destruction as well. */
2277 if (type_build_dtor_call (ctype))
2278 {
2279 tree dtor = build_special_member_call (ob, complete_dtor_identifier,
2280 NULL, ctype, LOOKUP_NORMAL,
2281 tf_none);
2282 if (dtor == error_mark_node)
2283 return error_mark_node;
2284 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
2285 expr = build2 (COMPOUND_EXPR, void_type_node, expr, dtor);
2286 }
2287 }
2288 else
2289 {
2290 if (len == 0)
2291 return build_value_init (strip_array_types (to), tf_none);
2292 if (len > 1)
2293 {
2294 if (cxx_dialect < cxx20)
2295 /* Too many initializers. */
2296 return error_mark_node;
2297
2298 /* In C++20 this is well-formed:
2299 using T = int[2];
2300 T t(1, 2);
2301 which means that std::is_constructible_v<int[2], int, int>
2302 should be true. */
2303 vec<constructor_elt, va_gc> *v;
2304 vec_alloc (v, len);
2305 for (tree arg : tree_vec_range (from))
2306 {
2307 tree stub = build_stub_object (arg);
2308 constructor_elt elt = { NULL_TREE, stub };
2309 v->quick_push (elt);
2310 }
2311 from = build_constructor (init_list_type_node, v);
2312 CONSTRUCTOR_IS_DIRECT_INIT (from) = true;
2313 CONSTRUCTOR_IS_PAREN_INIT (from) = true;
2314 }
2315 else
2316 from = build_stub_object (TREE_VEC_ELT (from, 0));
2317 expr = perform_direct_initialization_if_possible (to, from,
2318 /*cast*/false,
2319 tf_none);
2320 /* If t(e) didn't work, maybe t{e} will. */
2321 if (expr == NULL_TREE
2322 && len == 1
2323 && cxx_dialect >= cxx20)
2324 {
2325 from = build_constructor_single (init_list_type_node, NULL_TREE,
2326 from);
2327 CONSTRUCTOR_IS_DIRECT_INIT (from) = true;
2328 CONSTRUCTOR_IS_PAREN_INIT (from) = true;
2329 expr = perform_direct_initialization_if_possible (to, from,
2330 /*cast*/false,
2331 tf_none);
2332 }
2333 }
2334 return expr;
2335 }
2336
2337 /* Returns a tree iff TO is assignable (if CODE is MODIFY_EXPR) or
2338 constructible (otherwise) from FROM, which is a single type for
2339 assignment or a list of types for construction. */
2340
2341 static tree
2342 is_xible_helper (enum tree_code code, tree to, tree from, bool trivial)
2343 {
2344 to = complete_type (to);
2345 deferring_access_check_sentinel acs (dk_no_deferred);
2346 if (VOID_TYPE_P (to) || ABSTRACT_CLASS_TYPE_P (to)
2347 || (from && FUNC_OR_METHOD_TYPE_P (from)
2348 && (TYPE_READONLY (from) || FUNCTION_REF_QUALIFIED (from))))
2349 return error_mark_node;
2350 tree expr;
2351 if (code == MODIFY_EXPR)
2352 expr = assignable_expr (to, from);
2353 else if (trivial && TREE_VEC_LENGTH (from) > 1
2354 && cxx_dialect < cxx20)
2355 return error_mark_node; // only 0- and 1-argument ctors can be trivial
2356 // before C++20 aggregate paren init
2357 else if (TREE_CODE (to) == ARRAY_TYPE && !TYPE_DOMAIN (to))
2358 return error_mark_node; // can't construct an array of unknown bound
2359 else
2360 expr = constructible_expr (to, from);
2361 return expr;
2362 }
2363
2364 /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or
2365 constructible (otherwise) from FROM, which is a single type for
2366 assignment or a list of types for construction. */
2367
2368 bool
2369 is_trivially_xible (enum tree_code code, tree to, tree from)
2370 {
2371 tree expr = is_xible_helper (code, to, from, /*trivial*/true);
2372 if (expr == NULL_TREE || expr == error_mark_node)
2373 return false;
2374 tree nt = cp_walk_tree_without_duplicates (&expr, check_nontriv, NULL);
2375 return !nt;
2376 }
2377
2378 /* Returns true iff TO is nothrow assignable (if CODE is MODIFY_EXPR) or
2379 constructible (otherwise) from FROM, which is a single type for
2380 assignment or a list of types for construction. */
2381
2382 bool
2383 is_nothrow_xible (enum tree_code code, tree to, tree from)
2384 {
2385 ++cp_noexcept_operand;
2386 tree expr = is_xible_helper (code, to, from, /*trivial*/false);
2387 --cp_noexcept_operand;
2388 if (expr == NULL_TREE || expr == error_mark_node)
2389 return false;
2390 return expr_noexcept_p (expr, tf_none);
2391 }
2392
2393 /* Returns true iff TO is assignable (if CODE is MODIFY_EXPR) or
2394 constructible (otherwise) from FROM, which is a single type for
2395 assignment or a list of types for construction. */
2396
2397 bool
2398 is_xible (enum tree_code code, tree to, tree from)
2399 {
2400 tree expr = is_xible_helper (code, to, from, /*trivial*/false);
2401 if (expr == error_mark_node)
2402 return false;
2403 return !!expr;
2404 }
2405
2406 /* Return true iff conjunction_v<is_reference<T>, is_constructible<T, U>> is
2407 true, and the initialization
2408 T t(VAL<U>); // DIRECT_INIT_P
2409 or
2410 T t = VAL<U>; // !DIRECT_INIT_P
2411 binds t to a temporary object whose lifetime is extended.
2412 VAL<T> is defined in [meta.unary.prop]:
2413 -- If T is a reference or function type, VAL<T> is an expression with the
2414 same type and value category as declval<T>().
2415 -- Otherwise, VAL<T> is a prvalue that initially has type T. */
2416
2417 bool
2418 ref_xes_from_temporary (tree to, tree from, bool direct_init_p)
2419 {
2420 /* Check is_reference<T>. */
2421 if (!TYPE_REF_P (to))
2422 return false;
2423 /* We don't check is_constructible<T, U>: if T isn't constructible
2424 from U, we won't be able to create a conversion. */
2425 tree val = build_trait_object (from);
2426 if (val == error_mark_node)
2427 return false;
2428 if (!TYPE_REF_P (from) && TREE_CODE (from) != FUNCTION_TYPE)
2429 val = CLASS_TYPE_P (from) ? force_rvalue (val, tf_none) : rvalue (val);
2430 return ref_conv_binds_to_temporary (to, val, direct_init_p).is_true ();
2431 }
2432
2433 /* Worker for is_{,nothrow_}convertible. Attempt to perform an implicit
2434 conversion from FROM to TO and return the result. */
2435
2436 static tree
2437 is_convertible_helper (tree from, tree to)
2438 {
2439 if (VOID_TYPE_P (from) && VOID_TYPE_P (to))
2440 return integer_one_node;
2441 cp_unevaluated u;
2442 tree expr = build_trait_object (from);
2443 /* std::is_{,nothrow_}convertible test whether the imaginary function
2444 definition
2445
2446 To test() { return std::declval<From>(); }
2447
2448 is well-formed. A function can't return a function. */
2449 if (FUNC_OR_METHOD_TYPE_P (to) || expr == error_mark_node)
2450 return error_mark_node;
2451 deferring_access_check_sentinel acs (dk_no_deferred);
2452 return perform_implicit_conversion (to, expr, tf_none);
2453 }
2454
2455 /* Return true if FROM can be converted to TO using implicit conversions,
2456 or both FROM and TO are possibly cv-qualified void. NB: This doesn't
2457 implement the "Access checks are performed as if from a context unrelated
2458 to either type" restriction. */
2459
2460 bool
2461 is_convertible (tree from, tree to)
2462 {
2463 tree expr = is_convertible_helper (from, to);
2464 if (expr == error_mark_node)
2465 return false;
2466 return !!expr;
2467 }
2468
2469 /* Like is_convertible, but the conversion is also noexcept. */
2470
2471 bool
2472 is_nothrow_convertible (tree from, tree to)
2473 {
2474 tree expr = is_convertible_helper (from, to);
2475 if (expr == NULL_TREE || expr == error_mark_node)
2476 return false;
2477 return expr_noexcept_p (expr, tf_none);
2478 }
2479
2480 /* Categorize various special_function_kinds. */
2481 #define SFK_CTOR_P(sfk) \
2482 ((sfk) >= sfk_constructor && (sfk) <= sfk_move_constructor)
2483 #define SFK_DTOR_P(sfk) \
2484 ((sfk) == sfk_destructor || (sfk) == sfk_virtual_destructor)
2485 #define SFK_ASSIGN_P(sfk) \
2486 ((sfk) == sfk_copy_assignment || (sfk) == sfk_move_assignment)
2487 #define SFK_COPY_P(sfk) \
2488 ((sfk) == sfk_copy_constructor || (sfk) == sfk_copy_assignment)
2489 #define SFK_MOVE_P(sfk) \
2490 ((sfk) == sfk_move_constructor || (sfk) == sfk_move_assignment)
2491
2492 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
2493 DELETED_P or give an error message MSG with argument ARG. */
2494
2495 static void
2496 process_subob_fn (tree fn, special_function_kind sfk, tree *spec_p,
2497 bool *trivial_p, bool *deleted_p, bool *constexpr_p,
2498 bool diag, tree arg, bool dtor_from_ctor = false)
2499 {
2500 if (!fn || fn == error_mark_node)
2501 {
2502 if (deleted_p)
2503 *deleted_p = true;
2504 return;
2505 }
2506
2507 if (spec_p)
2508 {
2509 if (!maybe_instantiate_noexcept (fn))
2510 *spec_p = error_mark_node;
2511 else
2512 {
2513 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2514 *spec_p = merge_exception_specifiers (*spec_p, raises);
2515 }
2516 }
2517
2518 if (!trivial_fn_p (fn) && !dtor_from_ctor)
2519 {
2520 if (trivial_p)
2521 *trivial_p = false;
2522 if (TREE_CODE (arg) == FIELD_DECL
2523 && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
2524 {
2525 if (deleted_p)
2526 *deleted_p = true;
2527 if (diag)
2528 error ("union member %q+D with non-trivial %qD", arg, fn);
2529 }
2530 }
2531
2532 if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
2533 {
2534 *constexpr_p = false;
2535 if (diag)
2536 {
2537 inform (DECL_SOURCE_LOCATION (fn),
2538 SFK_DTOR_P (sfk)
2539 ? G_("defaulted destructor calls non-%<constexpr%> %qD")
2540 : G_("defaulted constructor calls non-%<constexpr%> %qD"),
2541 fn);
2542 explain_invalid_constexpr_fn (fn);
2543 }
2544 }
2545 }
2546
2547 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
2548 aggregates. If DTOR_FROM_CTOR is true, we're walking subobject destructors
2549 called from a synthesized constructor, in which case we don't consider
2550 the triviality of the subobject destructor. */
2551
2552 static void
2553 walk_field_subobs (tree fields, special_function_kind sfk, tree fnname,
2554 int quals, tree *spec_p, bool *trivial_p,
2555 bool *deleted_p, bool *constexpr_p,
2556 bool diag, int flags, tsubst_flags_t complain,
2557 bool dtor_from_ctor)
2558 {
2559 if (!fields)
2560 return;
2561
2562 tree ctx = DECL_CONTEXT (fields);
2563
2564 /* CWG2084: A defaulted default ctor for a union with a DMI only initializes
2565 that member, so don't check other members. */
2566 enum { unknown, no, yes }
2567 only_dmi_mem = (sfk == sfk_constructor && TREE_CODE (ctx) == UNION_TYPE
2568 ? unknown : no);
2569
2570 again:
2571 for (tree field = fields; field; field = DECL_CHAIN (field))
2572 {
2573 tree mem_type, argtype, rval;
2574
2575 if (TREE_CODE (field) != FIELD_DECL
2576 || DECL_ARTIFICIAL (field)
2577 || DECL_UNNAMED_BIT_FIELD (field))
2578 continue;
2579
2580 /* Variant members only affect deletedness. In particular, they don't
2581 affect the exception-specification of a user-provided destructor,
2582 which we're figuring out via get_defaulted_eh_spec. So if we aren't
2583 asking if this is deleted, don't even look up the function; we don't
2584 want an error about a deleted function we aren't actually calling. */
2585 if (sfk == sfk_destructor && deleted_p == NULL
2586 && TREE_CODE (ctx) == UNION_TYPE)
2587 break;
2588
2589 if (only_dmi_mem != no)
2590 {
2591 if (DECL_INITIAL (field))
2592 only_dmi_mem = yes;
2593 else
2594 /* Don't check this until we know there's no DMI. */
2595 continue;
2596 }
2597
2598 mem_type = strip_array_types (TREE_TYPE (field));
2599 if (SFK_ASSIGN_P (sfk))
2600 {
2601 bool bad = true;
2602 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
2603 {
2604 if (diag)
2605 error ("non-static const member %q#D, cannot use default "
2606 "assignment operator", field);
2607 }
2608 else if (TYPE_REF_P (mem_type))
2609 {
2610 if (diag)
2611 error ("non-static reference member %q#D, cannot use "
2612 "default assignment operator", field);
2613 }
2614 else
2615 bad = false;
2616
2617 if (bad && deleted_p)
2618 *deleted_p = true;
2619 }
2620 else if (sfk == sfk_constructor || sfk == sfk_inheriting_constructor)
2621 {
2622 bool bad;
2623
2624 if (DECL_INITIAL (field))
2625 {
2626 if (diag && DECL_INITIAL (field) == error_mark_node)
2627 inform (DECL_SOURCE_LOCATION (field),
2628 "initializer for %q#D is invalid", field);
2629 if (trivial_p)
2630 *trivial_p = false;
2631 /* Core 1351: If the field has an NSDMI that could throw, the
2632 default constructor is noexcept(false). */
2633 if (spec_p)
2634 {
2635 tree nsdmi = get_nsdmi (field, /*ctor*/false, complain);
2636 if (nsdmi == error_mark_node)
2637 *spec_p = error_mark_node;
2638 else if (*spec_p != error_mark_node
2639 && !expr_noexcept_p (nsdmi, tf_none))
2640 *spec_p = noexcept_false_spec;
2641 }
2642 /* Don't do the normal processing. */
2643 continue;
2644 }
2645
2646 bad = false;
2647 if (CP_TYPE_CONST_P (mem_type)
2648 && default_init_uninitialized_part (mem_type))
2649 {
2650 if (diag)
2651 {
2652 error ("uninitialized const member in %q#T",
2653 current_class_type);
2654 inform (DECL_SOURCE_LOCATION (field),
2655 "%q#D should be initialized", field);
2656 }
2657 bad = true;
2658 }
2659 else if (TYPE_REF_P (mem_type))
2660 {
2661 if (diag)
2662 {
2663 error ("uninitialized reference member in %q#T",
2664 current_class_type);
2665 inform (DECL_SOURCE_LOCATION (field),
2666 "%q#D should be initialized", field);
2667 }
2668 bad = true;
2669 }
2670
2671 if (bad && deleted_p)
2672 *deleted_p = true;
2673
2674 /* Before C++20, for an implicitly-defined default constructor to
2675 be constexpr, every member must have a user-provided default
2676 constructor or an explicit initializer. */
2677 if (constexpr_p
2678 && cxx_dialect < cxx20
2679 && !CLASS_TYPE_P (mem_type)
2680 && TREE_CODE (ctx) != UNION_TYPE)
2681 {
2682 *constexpr_p = false;
2683 if (diag)
2684 inform (DECL_SOURCE_LOCATION (field),
2685 "defaulted default constructor does not "
2686 "initialize %q#D", field);
2687 }
2688 }
2689 else if (sfk == sfk_copy_constructor)
2690 {
2691 /* 12.8p11b5 */
2692 if (TYPE_REF_P (mem_type)
2693 && TYPE_REF_IS_RVALUE (mem_type))
2694 {
2695 if (diag)
2696 error ("copying non-static data member %q#D of rvalue "
2697 "reference type", field);
2698 if (deleted_p)
2699 *deleted_p = true;
2700 }
2701 }
2702
2703 if (!CLASS_TYPE_P (mem_type))
2704 continue;
2705
2706 if (ANON_AGGR_TYPE_P (mem_type))
2707 {
2708 walk_field_subobs (TYPE_FIELDS (mem_type), sfk, fnname, quals,
2709 spec_p, trivial_p, deleted_p, constexpr_p,
2710 diag, flags, complain, dtor_from_ctor);
2711 continue;
2712 }
2713
2714 if (SFK_COPY_P (sfk) || SFK_MOVE_P (sfk))
2715 {
2716 int mem_quals = cp_type_quals (mem_type) | quals;
2717 if (DECL_MUTABLE_P (field))
2718 mem_quals &= ~TYPE_QUAL_CONST;
2719 argtype = build_stub_type (mem_type, mem_quals, SFK_MOVE_P (sfk));
2720 }
2721 else
2722 argtype = NULL_TREE;
2723
2724 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
2725
2726 process_subob_fn (rval, sfk, spec_p, trivial_p, deleted_p,
2727 constexpr_p, diag, field, dtor_from_ctor);
2728 }
2729
2730 /* We didn't find a DMI in this union, now check all the members. */
2731 if (only_dmi_mem == unknown)
2732 {
2733 only_dmi_mem = no;
2734 goto again;
2735 }
2736 }
2737
2738 /* Base walker helper for synthesized_method_walk. Inspect a direct
2739 or virtual base. BINFO is the parent type's binfo. BASE_BINFO is
2740 the base binfo of interests. All other parms are as for
2741 synthesized_method_walk, or its local vars. */
2742
2743 static tree
2744 synthesized_method_base_walk (tree binfo, tree base_binfo,
2745 special_function_kind sfk, tree fnname, int quals,
2746 tree *inheriting_ctor, tree inherited_parms,
2747 int flags, bool diag,
2748 tree *spec_p, bool *trivial_p,
2749 bool *deleted_p, bool *constexpr_p)
2750 {
2751 bool inherited_binfo = false;
2752 tree argtype = NULL_TREE;
2753 deferring_kind defer = dk_no_deferred;
2754
2755 if (SFK_COPY_P (sfk) || SFK_MOVE_P (sfk))
2756 argtype = build_stub_type (BINFO_TYPE (base_binfo), quals, SFK_MOVE_P (sfk));
2757 else if (inheriting_ctor
2758 && (inherited_binfo
2759 = binfo_inherited_from (binfo, base_binfo, *inheriting_ctor)))
2760 {
2761 argtype = inherited_parms;
2762 /* Don't check access on the inherited constructor. */
2763 if (flag_new_inheriting_ctors)
2764 defer = dk_deferred;
2765 }
2766 else if (cxx_dialect >= cxx14 && sfk == sfk_virtual_destructor
2767 && BINFO_VIRTUAL_P (base_binfo)
2768 && ABSTRACT_CLASS_TYPE_P (BINFO_TYPE (binfo)))
2769 /* Don't check access when looking at vbases of abstract class's
2770 virtual destructor. */
2771 defer = dk_no_check;
2772
2773 if (defer != dk_no_deferred)
2774 push_deferring_access_checks (defer);
2775 tree rval = locate_fn_flags (base_binfo, fnname, argtype, flags,
2776 diag ? tf_warning_or_error : tf_none);
2777 if (defer != dk_no_deferred)
2778 pop_deferring_access_checks ();
2779
2780 /* Replace an inherited template with the appropriate specialization. */
2781 if (inherited_binfo && rval
2782 && DECL_P (*inheriting_ctor) && DECL_P (rval)
2783 && DECL_CONTEXT (*inheriting_ctor) == DECL_CONTEXT (rval))
2784 *inheriting_ctor = DECL_CLONED_FUNCTION (rval);
2785
2786 process_subob_fn (rval, sfk, spec_p, trivial_p, deleted_p,
2787 constexpr_p, diag, BINFO_TYPE (base_binfo));
2788 if (SFK_CTOR_P (sfk)
2789 && (!BINFO_VIRTUAL_P (base_binfo)
2790 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))))
2791 {
2792 /* In a constructor we also need to check the subobject
2793 destructors for cleanup of partially constructed objects. */
2794 tree dtor = locate_fn_flags (base_binfo, complete_dtor_identifier,
2795 NULL_TREE, flags,
2796 diag ? tf_warning_or_error : tf_none);
2797 /* Note that we don't pass down trivial_p; the subobject
2798 destructors don't affect triviality of the constructor. Nor
2799 do they affect constexpr-ness (a constant expression doesn't
2800 throw) or exception-specification (a throw from one of the
2801 dtors would be a double-fault). */
2802 process_subob_fn (dtor, sfk, NULL, NULL, deleted_p, NULL, false,
2803 BINFO_TYPE (base_binfo), /*dtor_from_ctor*/true);
2804 }
2805
2806 return rval;
2807 }
2808
2809 /* The caller wants to generate an implicit declaration of SFK for
2810 CTYPE which is const if relevant and CONST_P is set. If SPEC_P,
2811 TRIVIAL_P, DELETED_P or CONSTEXPR_P are non-null, set their
2812 referent appropriately. If DIAG is true, we're either being called
2813 from maybe_explain_implicit_delete to give errors, or if
2814 CONSTEXPR_P is non-null, from explain_invalid_constexpr_fn. */
2815
2816 static void
2817 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
2818 tree *spec_p, bool *trivial_p, bool *deleted_p,
2819 bool *constexpr_p, bool diag,
2820 tree *inheriting_ctor, tree inherited_parms)
2821 {
2822 tree binfo, base_binfo;
2823 int i;
2824
2825 /* SFK must be exactly one category. */
2826 gcc_checking_assert (SFK_DTOR_P(sfk) + SFK_CTOR_P(sfk)
2827 + SFK_ASSIGN_P(sfk) == 1);
2828
2829 if (spec_p)
2830 *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec);
2831
2832 if (deleted_p)
2833 {
2834 /* "The closure type associated with a lambda-expression has a deleted
2835 default constructor and a deleted copy assignment operator."
2836 This is diagnosed in maybe_explain_implicit_delete.
2837 In C++20, only lambda-expressions with lambda-captures have those
2838 deleted. */
2839 if (LAMBDA_TYPE_P (ctype)
2840 && (sfk == sfk_constructor || sfk == sfk_copy_assignment)
2841 && (cxx_dialect < cxx20
2842 || LAMBDA_EXPR_CAPTURE_LIST (CLASSTYPE_LAMBDA_EXPR (ctype))
2843 || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE
2844 (CLASSTYPE_LAMBDA_EXPR (ctype)) != CPLD_NONE))
2845 {
2846 *deleted_p = true;
2847 return;
2848 }
2849
2850 *deleted_p = false;
2851 }
2852
2853 bool check_vdtor = false;
2854 tree fnname;
2855
2856 if (SFK_DTOR_P (sfk))
2857 {
2858 check_vdtor = true;
2859 /* The synthesized method will call base dtors, but check complete
2860 here to avoid having to deal with VTT. */
2861 fnname = complete_dtor_identifier;
2862 }
2863 else if (SFK_ASSIGN_P (sfk))
2864 fnname = assign_op_identifier;
2865 else
2866 fnname = complete_ctor_identifier;
2867
2868 gcc_assert ((sfk == sfk_inheriting_constructor)
2869 == (inheriting_ctor && *inheriting_ctor != NULL_TREE));
2870
2871 /* If that user-written default constructor would satisfy the
2872 requirements of a constexpr constructor (7.1.5), the
2873 implicitly-defined default constructor is constexpr.
2874
2875 C++20:
2876 The implicitly-defined copy/move assignment operator is constexpr if
2877 - X is a literal type, and
2878 - the assignment operator selected to copy/move each direct base class
2879 subobject is a constexpr function, and
2880 - for each non-static data member of X that is of class type (or array
2881 thereof), the assignment operator selected to copy/move that
2882 member is a constexpr function.
2883
2884 C++23:
2885 The implicitly-defined copy/move assignment operator is constexpr. */
2886 if (constexpr_p)
2887 *constexpr_p = (SFK_CTOR_P (sfk)
2888 || (SFK_ASSIGN_P (sfk) && cxx_dialect >= cxx14)
2889 || (SFK_DTOR_P (sfk) && cxx_dialect >= cxx20));
2890
2891 bool expected_trivial = type_has_trivial_fn (ctype, sfk);
2892 if (trivial_p)
2893 *trivial_p = expected_trivial;
2894
2895 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
2896 class versions and other properties of the type. But a subobject
2897 class can be trivially copyable and yet have overload resolution
2898 choose a template constructor for initialization, depending on
2899 rvalueness and cv-quals. And furthermore, a member in a base might
2900 be trivial but deleted or otherwise not callable. So we can't exit
2901 early in C++0x. The same considerations apply in C++98/03, but
2902 there the definition of triviality does not consider overload
2903 resolution, so a constructor can be trivial even if it would otherwise
2904 call a non-trivial constructor. */
2905 if (expected_trivial
2906 && (!(SFK_COPY_P (sfk) || SFK_MOVE_P (sfk)) || cxx_dialect < cxx11))
2907 {
2908 if (constexpr_p && sfk == sfk_constructor)
2909 {
2910 bool cx = trivial_default_constructor_is_constexpr (ctype);
2911 *constexpr_p = cx;
2912 if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
2913 /* A trivial constructor doesn't have any NSDMI. */
2914 inform (input_location, "defaulted default constructor does "
2915 "not initialize any non-static data member");
2916 }
2917 if (!diag && cxx_dialect < cxx11)
2918 return;
2919 }
2920
2921 bool push_to_top = maybe_push_to_top_level (TYPE_NAME (ctype));
2922 ++cp_unevaluated_operand;
2923 ++c_inhibit_evaluation_warnings;
2924 push_deferring_access_checks (dk_no_deferred);
2925
2926 tree scope = push_scope (ctype);
2927
2928 int flags = LOOKUP_NORMAL | LOOKUP_SPECULATIVE;
2929 if (sfk != sfk_inheriting_constructor)
2930 flags |= LOOKUP_DEFAULTED;
2931
2932 tsubst_flags_t complain = diag ? tf_warning_or_error : tf_none;
2933 if (diag && spec_p)
2934 /* We're in get_defaulted_eh_spec; we don't actually want any walking
2935 diagnostics, we just want complain set. */
2936 diag = false;
2937 int quals = const_p ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED;
2938
2939 for (binfo = TYPE_BINFO (ctype), i = 0;
2940 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
2941 {
2942 if (!SFK_ASSIGN_P (sfk) && BINFO_VIRTUAL_P (base_binfo))
2943 /* We'll handle virtual bases below. */
2944 continue;
2945
2946 tree fn = synthesized_method_base_walk (binfo, base_binfo,
2947 sfk, fnname, quals,
2948 inheriting_ctor, inherited_parms,
2949 flags, diag, spec_p, trivial_p,
2950 deleted_p, constexpr_p);
2951
2952 if (diag && SFK_ASSIGN_P (sfk) && SFK_MOVE_P (sfk)
2953 && BINFO_VIRTUAL_P (base_binfo)
2954 && fn && TREE_CODE (fn) == FUNCTION_DECL
2955 && move_fn_p (fn) && !trivial_fn_p (fn)
2956 && vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
2957 warning (OPT_Wvirtual_move_assign,
2958 "defaulted move assignment for %qT calls a non-trivial "
2959 "move assignment operator for virtual base %qT",
2960 ctype, BINFO_TYPE (base_binfo));
2961
2962 if (check_vdtor && type_has_virtual_destructor (BINFO_TYPE (base_binfo)))
2963 {
2964 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
2965 to have a null fn (no class-specific op delete). */
2966 fn = locate_fn_flags (ctype, ovl_op_identifier (false, DELETE_EXPR),
2967 ptr_type_node, flags, tf_none);
2968 if (fn && fn == error_mark_node)
2969 {
2970 if (complain & tf_error)
2971 locate_fn_flags (ctype, ovl_op_identifier (false, DELETE_EXPR),
2972 ptr_type_node, flags, complain);
2973 if (deleted_p)
2974 *deleted_p = true;
2975 }
2976 check_vdtor = false;
2977 }
2978 }
2979
2980 vec<tree, va_gc> *vbases = CLASSTYPE_VBASECLASSES (ctype);
2981 if (SFK_ASSIGN_P (sfk))
2982 /* Already examined vbases above. */;
2983 else if (vec_safe_is_empty (vbases))
2984 /* No virtual bases to worry about. */;
2985 else if (ABSTRACT_CLASS_TYPE_P (ctype) && cxx_dialect >= cxx14
2986 /* DR 1658 specifies that vbases of abstract classes are
2987 ignored for both ctors and dtors. Except DR 2336
2988 overrides that skipping when determing the eh-spec of a
2989 virtual destructor. */
2990 && sfk != sfk_virtual_destructor)
2991 /* Vbase cdtors are not relevant. */;
2992 else
2993 {
2994 if (constexpr_p)
2995 *constexpr_p = false;
2996
2997 FOR_EACH_VEC_ELT (*vbases, i, base_binfo)
2998 synthesized_method_base_walk (binfo, base_binfo, sfk, fnname, quals,
2999 inheriting_ctor, inherited_parms,
3000 flags, diag,
3001 spec_p, trivial_p, deleted_p, constexpr_p);
3002 }
3003
3004 /* Now handle the non-static data members. */
3005 walk_field_subobs (TYPE_FIELDS (ctype), sfk, fnname, quals,
3006 spec_p, trivial_p, deleted_p, constexpr_p,
3007 diag, flags, complain, /*dtor_from_ctor*/false);
3008 if (SFK_CTOR_P (sfk))
3009 walk_field_subobs (TYPE_FIELDS (ctype), sfk_destructor,
3010 complete_dtor_identifier, TYPE_UNQUALIFIED,
3011 NULL, NULL, deleted_p, NULL,
3012 false, flags, complain, /*dtor_from_ctor*/true);
3013
3014 pop_scope (scope);
3015
3016 pop_deferring_access_checks ();
3017 --cp_unevaluated_operand;
3018 --c_inhibit_evaluation_warnings;
3019 maybe_pop_from_top_level (push_to_top);
3020 }
3021
3022 /* DECL is a defaulted function whose exception specification is now
3023 needed. Return what it should be. */
3024
3025 tree
3026 get_defaulted_eh_spec (tree decl, tsubst_flags_t complain)
3027 {
3028 /* For DECL_MAYBE_DELETED this should already have been handled by
3029 synthesize_method. */
3030 gcc_assert (!DECL_MAYBE_DELETED (decl));
3031
3032 if (DECL_CLONED_FUNCTION_P (decl))
3033 decl = DECL_CLONED_FUNCTION (decl);
3034 special_function_kind sfk = special_function_p (decl);
3035 tree ctype = DECL_CONTEXT (decl);
3036 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
3037 tree parm_type = TREE_VALUE (parms);
3038 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
3039 tree spec = empty_except_spec;
3040 bool diag = !DECL_DELETED_FN (decl) && (complain & tf_error);
3041 tree inh = DECL_INHERITED_CTOR (decl);
3042 if (SFK_DTOR_P (sfk) && DECL_VIRTUAL_P (decl))
3043 /* We have to examine virtual bases even if abstract. */
3044 sfk = sfk_virtual_destructor;
3045 bool pushed = false;
3046 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
3047 pushed = push_tinst_level (decl);
3048 synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL,
3049 NULL, diag, &inh, parms);
3050 if (pushed)
3051 pop_tinst_level ();
3052 return spec;
3053 }
3054
3055 /* DECL is a deleted function. If it's implicitly deleted, explain why and
3056 return true; else return false. */
3057
3058 bool
3059 maybe_explain_implicit_delete (tree decl)
3060 {
3061 /* If decl is a clone, get the primary variant. */
3062 decl = DECL_ORIGIN (decl);
3063 gcc_assert (DECL_DELETED_FN (decl));
3064 if (DECL_DEFAULTED_FN (decl))
3065 {
3066 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
3067 static hash_set<tree> *explained;
3068
3069 special_function_kind sfk;
3070 location_t loc;
3071 bool informed;
3072 tree ctype;
3073
3074 if (!explained)
3075 explained = new hash_set<tree>;
3076 if (explained->add (decl))
3077 return true;
3078
3079 sfk = special_function_p (decl);
3080 ctype = DECL_CONTEXT (decl);
3081 loc = input_location;
3082 input_location = DECL_SOURCE_LOCATION (decl);
3083
3084 informed = false;
3085 if (LAMBDA_TYPE_P (ctype))
3086 {
3087 informed = true;
3088 if (sfk == sfk_constructor)
3089 inform (DECL_SOURCE_LOCATION (decl),
3090 "a lambda closure type has a deleted default constructor");
3091 else if (sfk == sfk_copy_assignment)
3092 inform (DECL_SOURCE_LOCATION (decl),
3093 "a lambda closure type has a deleted copy assignment operator");
3094 else
3095 informed = false;
3096 }
3097 else if (DECL_ARTIFICIAL (decl)
3098 && (sfk == sfk_copy_assignment || sfk == sfk_copy_constructor)
3099 && classtype_has_move_assign_or_move_ctor_p (ctype, true))
3100 {
3101 inform (DECL_SOURCE_LOCATION (decl),
3102 "%q#D is implicitly declared as deleted because %qT "
3103 "declares a move constructor or move assignment operator",
3104 decl, ctype);
3105 informed = true;
3106 }
3107 else if (sfk == sfk_inheriting_constructor)
3108 {
3109 tree binfo = inherited_ctor_binfo (decl);
3110 if (TREE_CODE (binfo) != TREE_BINFO)
3111 {
3112 inform (DECL_SOURCE_LOCATION (decl),
3113 "%q#D inherits from multiple base subobjects",
3114 decl);
3115 informed = true;
3116 }
3117 }
3118 if (!informed && sfk == sfk_comparison)
3119 {
3120 inform (DECL_SOURCE_LOCATION (decl),
3121 "%q#D is implicitly deleted because the default "
3122 "definition would be ill-formed:", decl);
3123 build_comparison_op (decl, false, tf_warning_or_error);
3124 }
3125 else if (!informed)
3126 {
3127 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
3128 bool const_p = false;
3129 if (parms)
3130 {
3131 tree parm_type = TREE_VALUE (parms);
3132 const_p = CP_TYPE_CONST_P (non_reference (parm_type));
3133 }
3134 tree raises = NULL_TREE;
3135 bool deleted_p = false;
3136 tree scope = push_scope (ctype);
3137 tree inh = DECL_INHERITED_CTOR (decl);
3138
3139 synthesized_method_walk (ctype, sfk, const_p,
3140 &raises, NULL, &deleted_p, NULL, false,
3141 &inh, parms);
3142 if (deleted_p)
3143 {
3144 inform (DECL_SOURCE_LOCATION (decl),
3145 "%q#D is implicitly deleted because the default "
3146 "definition would be ill-formed:", decl);
3147 synthesized_method_walk (ctype, sfk, const_p,
3148 NULL, NULL, &deleted_p, NULL, true,
3149 &inh, parms);
3150 }
3151 else if (!comp_except_specs
3152 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
3153 raises, ce_normal))
3154 inform (DECL_SOURCE_LOCATION (decl), "%q#F is implicitly "
3155 "deleted because its exception-specification does not "
3156 "match the implicit exception-specification %qX",
3157 decl, raises);
3158 else if (flag_checking)
3159 gcc_unreachable ();
3160
3161 pop_scope (scope);
3162 }
3163
3164 input_location = loc;
3165 return true;
3166 }
3167 return false;
3168 }
3169
3170 /* DECL is a defaulted function which was declared constexpr. Explain why
3171 it can't be constexpr. */
3172
3173 void
3174 explain_implicit_non_constexpr (tree decl)
3175 {
3176 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
3177 bool const_p = CP_TYPE_CONST_P (non_reference (TREE_VALUE (parms)));
3178 tree inh = DECL_INHERITED_CTOR (decl);
3179 bool dummy;
3180 special_function_kind sfk = special_function_p (decl);
3181 if (sfk == sfk_comparison)
3182 {
3183 DECL_DECLARED_CONSTEXPR_P (decl) = true;
3184 build_comparison_op (decl, false, tf_warning_or_error);
3185 DECL_DECLARED_CONSTEXPR_P (decl) = false;
3186 }
3187 else
3188 synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
3189 sfk, const_p,
3190 NULL, NULL, NULL, &dummy, true,
3191 &inh, parms);
3192 }
3193
3194 /* DECL is an instantiation of an inheriting constructor template. Deduce
3195 the correct exception-specification and deletedness for this particular
3196 specialization. Return true if the deduction succeeds; false otherwise. */
3197
3198 bool
3199 deduce_inheriting_ctor (tree decl)
3200 {
3201 decl = DECL_ORIGIN (decl);
3202 gcc_assert (DECL_INHERITED_CTOR (decl));
3203 tree spec;
3204 bool trivial, constexpr_, deleted;
3205 tree inh = DECL_INHERITED_CTOR (decl);
3206 synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor,
3207 false, &spec, &trivial, &deleted, &constexpr_,
3208 /*diag*/false,
3209 &inh,
3210 FUNCTION_FIRST_USER_PARMTYPE (decl));
3211 if (spec == error_mark_node)
3212 return false;
3213 if (TREE_CODE (inherited_ctor_binfo (decl)) != TREE_BINFO)
3214 /* Inherited the same constructor from different base subobjects. */
3215 deleted = true;
3216 DECL_DELETED_FN (decl) = deleted;
3217 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
3218 SET_DECL_INHERITED_CTOR (decl, inh);
3219
3220 tree clone;
3221 FOR_EACH_CLONE (clone, decl)
3222 {
3223 DECL_DELETED_FN (clone) = deleted;
3224 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
3225 SET_DECL_INHERITED_CTOR (clone, inh);
3226 }
3227
3228 return true;
3229 }
3230
3231 /* Implicitly declare the special function indicated by KIND, as a
3232 member of TYPE. For copy constructors and assignment operators,
3233 CONST_P indicates whether these functions should take a const
3234 reference argument or a non-const reference.
3235 Returns the FUNCTION_DECL for the implicitly declared function. */
3236
3237 tree
3238 implicitly_declare_fn (special_function_kind kind, tree type,
3239 bool const_p, tree pattern_fn,
3240 tree inherited_parms)
3241 {
3242 tree fn;
3243 tree parameter_types = void_list_node;
3244 tree return_type;
3245 tree fn_type;
3246 tree raises = empty_except_spec;
3247 tree rhs_parm_type = NULL_TREE;
3248 tree this_parm;
3249 tree name;
3250 HOST_WIDE_INT saved_processing_template_decl;
3251 bool deleted_p = false;
3252 bool constexpr_p = false;
3253 tree inherited_ctor = (kind == sfk_inheriting_constructor
3254 ? pattern_fn : NULL_TREE);
3255
3256 /* Because we create declarations for implicitly declared functions
3257 lazily, we may be creating the declaration for a member of TYPE
3258 while in some completely different context. However, TYPE will
3259 never be a dependent class (because we never want to do lookups
3260 for implicitly defined functions in a dependent class). */
3261 gcc_assert (!dependent_type_p (type));
3262
3263 /* If the member-specification does not explicitly declare any member or
3264 friend named operator==, an == operator function is declared
3265 implicitly for each three-way comparison operator function defined as
3266 defaulted in the member-specification, with the same access and
3267 function-definition and in the same class scope as the respective
3268 three-way comparison operator function, except that the return type is
3269 replaced with bool and the declarator-id is replaced with
3270 operator==.
3271
3272 [Note: Such an implicitly-declared == operator for a class X is
3273 defined as defaulted in the definition of X and has the same
3274 parameter-declaration-clause and trailing requires-clause as the
3275 respective three-way comparison operator. It is declared with friend,
3276 virtual, constexpr, or consteval if the three-way comparison operator
3277 function is so declared. If the three-way comparison operator function
3278 has no noexcept-specifier, the implicitly-declared == operator
3279 function has an implicit exception specification (14.5) that may
3280 differ from the implicit exception specification of the three-way
3281 comparison operator function. --end note] */
3282 if (kind == sfk_comparison)
3283 {
3284 fn = copy_operator_fn (pattern_fn, EQ_EXPR);
3285 DECL_ARTIFICIAL (fn) = 1;
3286 apply_deduced_return_type (fn, boolean_type_node);
3287 return fn;
3288 }
3289
3290 /* Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
3291 because we only create clones for constructors and destructors
3292 when not in a template. */
3293 saved_processing_template_decl = processing_template_decl;
3294 processing_template_decl = 0;
3295
3296 type = TYPE_MAIN_VARIANT (type);
3297
3298 if (targetm.cxx.cdtor_returns_this ())
3299 {
3300 if (kind == sfk_destructor)
3301 /* See comment in check_special_function_return_type. */
3302 return_type = build_pointer_type (void_type_node);
3303 else
3304 return_type = build_pointer_type (type);
3305 }
3306 else
3307 return_type = void_type_node;
3308
3309 int this_quals = TYPE_UNQUALIFIED;
3310 switch (kind)
3311 {
3312 case sfk_destructor:
3313 /* Destructor. */
3314 name = dtor_identifier;
3315 break;
3316
3317 case sfk_constructor:
3318 /* Default constructor. */
3319 name = ctor_identifier;
3320 break;
3321
3322 case sfk_copy_constructor:
3323 case sfk_copy_assignment:
3324 case sfk_move_constructor:
3325 case sfk_move_assignment:
3326 case sfk_inheriting_constructor:
3327 {
3328 if (kind == sfk_copy_assignment
3329 || kind == sfk_move_assignment)
3330 {
3331 return_type = build_reference_type (type);
3332 name = assign_op_identifier;
3333 }
3334 else
3335 name = ctor_identifier;
3336
3337 if (kind == sfk_inheriting_constructor)
3338 parameter_types = inherited_parms;
3339 else
3340 {
3341 if (const_p)
3342 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
3343 else
3344 rhs_parm_type = type;
3345 bool move_p = (kind == sfk_move_assignment
3346 || kind == sfk_move_constructor);
3347 rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
3348
3349 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
3350 }
3351 break;
3352 }
3353
3354 default:
3355 gcc_unreachable ();
3356 }
3357
3358 bool trivial_p = false;
3359
3360 if (inherited_ctor)
3361 {
3362 /* For an inheriting constructor, just copy these flags from the
3363 inherited constructor until deduce_inheriting_ctor. */
3364 raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor));
3365 deleted_p = DECL_DELETED_FN (inherited_ctor);
3366 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
3367 }
3368 else if (cxx_dialect >= cxx11)
3369 {
3370 raises = noexcept_deferred_spec;
3371 synthesized_method_walk (type, kind, const_p, NULL, &trivial_p,
3372 &deleted_p, &constexpr_p, false,
3373 &inherited_ctor, inherited_parms);
3374 }
3375 else
3376 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
3377 &deleted_p, &constexpr_p, false,
3378 &inherited_ctor, inherited_parms);
3379 /* Don't bother marking a deleted constructor as constexpr. */
3380 if (deleted_p)
3381 constexpr_p = false;
3382 /* A trivial copy/move constructor is also a constexpr constructor,
3383 unless the class has virtual bases (7.1.5p4). */
3384 else if (trivial_p
3385 && cxx_dialect >= cxx11
3386 && (kind == sfk_copy_constructor
3387 || kind == sfk_move_constructor)
3388 && !CLASSTYPE_VBASECLASSES (type))
3389 gcc_assert (constexpr_p);
3390
3391 if (!trivial_p && type_has_trivial_fn (type, kind))
3392 type_set_nontrivial_flag (type, kind);
3393
3394 /* Create the function. */
3395 tree this_type = cp_build_qualified_type (type, this_quals);
3396 fn_type = build_method_type_directly (this_type, return_type,
3397 parameter_types);
3398
3399 if (raises)
3400 {
3401 if (raises != error_mark_node)
3402 fn_type = build_exception_variant (fn_type, raises);
3403 else
3404 {
3405 /* Can happen, e.g., in C++98 mode for an ill-formed non-static data
3406 member initializer (c++/89914). Also, in C++98, we might have
3407 failed to deduce RAISES, so try again but complain this time. */
3408 if (cxx_dialect < cxx11)
3409 synthesized_method_walk (type, kind, const_p, &raises, nullptr,
3410 nullptr, nullptr, /*diag=*/true,
3411 &inherited_ctor, inherited_parms);
3412 /* We should have seen an error at this point. */
3413 gcc_assert (seen_error ());
3414 }
3415 }
3416 fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
3417 if (kind != sfk_inheriting_constructor)
3418 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
3419
3420 if (IDENTIFIER_OVL_OP_P (name))
3421 {
3422 const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (name);
3423 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) = op->ovl_op_code;
3424 }
3425 else if (IDENTIFIER_CTOR_P (name))
3426 DECL_CXX_CONSTRUCTOR_P (fn) = true;
3427 else if (IDENTIFIER_DTOR_P (name))
3428 DECL_CXX_DESTRUCTOR_P (fn) = true;
3429 else
3430 gcc_unreachable ();
3431
3432 SET_DECL_ALIGN (fn, MINIMUM_METHOD_BOUNDARY);
3433
3434 /* Create the explicit arguments. */
3435 if (rhs_parm_type)
3436 {
3437 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
3438 want its type to be included in the mangled function
3439 name. */
3440 tree decl = cp_build_parm_decl (fn, NULL_TREE, rhs_parm_type);
3441 TREE_READONLY (decl) = 1;
3442 retrofit_lang_decl (decl);
3443 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
3444 DECL_ARGUMENTS (fn) = decl;
3445 }
3446 else if (kind == sfk_inheriting_constructor)
3447 {
3448 tree *p = &DECL_ARGUMENTS (fn);
3449 int index = 1;
3450 for (tree parm = inherited_parms; parm && parm != void_list_node;
3451 parm = TREE_CHAIN (parm))
3452 {
3453 *p = cp_build_parm_decl (fn, NULL_TREE, TREE_VALUE (parm));
3454 retrofit_lang_decl (*p);
3455 DECL_PARM_LEVEL (*p) = 1;
3456 DECL_PARM_INDEX (*p) = index++;
3457 p = &DECL_CHAIN (*p);
3458 }
3459 SET_DECL_INHERITED_CTOR (fn, inherited_ctor);
3460 DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor);
3461 /* A constructor so declared has the same access as the corresponding
3462 constructor in X. */
3463 TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor);
3464 TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor);
3465 /* Copy constexpr from the inherited constructor even if the
3466 inheriting constructor doesn't satisfy the requirements. */
3467 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
3468 tree inherited_ctor_fn = STRIP_TEMPLATE (inherited_ctor);
3469 /* Also copy any attributes. */
3470 DECL_ATTRIBUTES (fn) = clone_attrs (DECL_ATTRIBUTES (inherited_ctor_fn));
3471 DECL_DISREGARD_INLINE_LIMITS (fn)
3472 = DECL_DISREGARD_INLINE_LIMITS (inherited_ctor_fn);
3473 }
3474
3475 /* Add the "this" parameter. */
3476 this_parm = build_this_parm (fn, fn_type, this_quals);
3477 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
3478 DECL_ARGUMENTS (fn) = this_parm;
3479
3480 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
3481
3482 DECL_IN_AGGR_P (fn) = 1;
3483 DECL_ARTIFICIAL (fn) = 1;
3484 DECL_DEFAULTED_FN (fn) = 1;
3485 if (cxx_dialect >= cxx11)
3486 {
3487 DECL_DELETED_FN (fn) = deleted_p;
3488 DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
3489 }
3490 DECL_EXTERNAL (fn) = true;
3491 DECL_NOT_REALLY_EXTERN (fn) = 1;
3492 DECL_DECLARED_INLINE_P (fn) = 1;
3493 set_linkage_according_to_type (type, fn);
3494 if (TREE_PUBLIC (fn))
3495 DECL_COMDAT (fn) = 1;
3496 rest_of_decl_compilation (fn, namespace_bindings_p (), at_eof);
3497 gcc_assert (!TREE_USED (fn));
3498
3499 /* Propagate constraints from the inherited constructor. */
3500 if (flag_concepts && inherited_ctor)
3501 if (tree orig_ci = get_constraints (inherited_ctor))
3502 {
3503 tree new_ci = copy_node (orig_ci);
3504 set_constraints (fn, new_ci);
3505 }
3506
3507 /* Restore PROCESSING_TEMPLATE_DECL. */
3508 processing_template_decl = saved_processing_template_decl;
3509
3510 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
3511 fn = add_inherited_template_parms (fn, inherited_ctor);
3512
3513 /* Warn about calling a non-trivial move assignment in a virtual base. */
3514 if (kind == sfk_move_assignment && !deleted_p && !trivial_p
3515 && CLASSTYPE_VBASECLASSES (type))
3516 {
3517 location_t loc = input_location;
3518 input_location = DECL_SOURCE_LOCATION (fn);
3519 synthesized_method_walk (type, kind, const_p,
3520 NULL, NULL, NULL, NULL, true,
3521 NULL, NULL_TREE);
3522 input_location = loc;
3523 }
3524
3525 return fn;
3526 }
3527
3528 /* Mark an explicitly defaulted function FN as =deleted and warn.
3529 IMPLICIT_FN is the corresponding special member function that
3530 would have been implicitly declared. */
3531
3532 void
3533 maybe_delete_defaulted_fn (tree fn, tree implicit_fn)
3534 {
3535 if (DECL_ARTIFICIAL (fn) || !DECL_DEFAULTED_IN_CLASS_P (fn))
3536 return;
3537
3538 DECL_DELETED_FN (fn) = true;
3539
3540 auto_diagnostic_group d;
3541 const special_function_kind kind = special_function_p (fn);
3542 tree parmtype
3543 = TREE_VALUE (DECL_XOBJ_MEMBER_FUNCTION_P (fn)
3544 ? TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)))
3545 : FUNCTION_FIRST_USER_PARMTYPE (fn));
3546 const bool illformed_p
3547 /* [dcl.fct.def.default] "if F1 is an assignment operator"... */
3548 = (SFK_ASSIGN_P (kind)
3549 /* "and the return type of F1 differs from the return type of F2" */
3550 && (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
3551 TREE_TYPE (TREE_TYPE (implicit_fn)))
3552 /* "or F1's non-object parameter type is not a reference,
3553 the program is ill-formed" */
3554 || !TYPE_REF_P (parmtype)));
3555 /* Decide if we want to emit a pedwarn, error, or a warning. */
3556 diagnostic_t diag_kind;
3557 int opt;
3558 if (illformed_p)
3559 {
3560 diag_kind = DK_ERROR;
3561 opt = 0;
3562 }
3563 else
3564 {
3565 diag_kind = cxx_dialect >= cxx20 ? DK_WARNING : DK_PEDWARN;
3566 opt = OPT_Wdefaulted_function_deleted;
3567 }
3568
3569 /* Don't warn for template instantiations. */
3570 if (DECL_TEMPLATE_INSTANTIATION (fn) && diag_kind == DK_WARNING)
3571 return;
3572
3573 const char *wmsg;
3574 switch (kind)
3575 {
3576 case sfk_copy_constructor:
3577 wmsg = G_("explicitly defaulted copy constructor is implicitly deleted "
3578 "because its declared type does not match the type of an "
3579 "implicit copy constructor");
3580 break;
3581 case sfk_move_constructor:
3582 wmsg = G_("explicitly defaulted move constructor is implicitly deleted "
3583 "because its declared type does not match the type of an "
3584 "implicit move constructor");
3585 break;
3586 case sfk_copy_assignment:
3587 wmsg = G_("explicitly defaulted copy assignment operator is implicitly "
3588 "deleted because its declared type does not match the type "
3589 "of an implicit copy assignment operator");
3590 break;
3591 case sfk_move_assignment:
3592 wmsg = G_("explicitly defaulted move assignment operator is implicitly "
3593 "deleted because its declared type does not match the type "
3594 "of an implicit move assignment operator");
3595 break;
3596 default:
3597 gcc_unreachable ();
3598 }
3599 if (emit_diagnostic (diag_kind, DECL_SOURCE_LOCATION (fn), opt, wmsg))
3600 inform (DECL_SOURCE_LOCATION (fn),
3601 "expected signature: %qD", implicit_fn);
3602 }
3603
3604 /* Gives any errors about defaulted functions which need to be deferred
3605 until the containing class is complete. IMP_CONST is false or true
3606 if we are called from check_bases_and_members and signals whether
3607 the implicit function has a non-object parameter of type const C&. */
3608
3609 void
3610 defaulted_late_check (tree fn, tristate imp_const/*=tristate::unknown()*/)
3611 {
3612 /* Complain about invalid signature for defaulted fn. */
3613 tree ctx = DECL_CONTEXT (fn);
3614 special_function_kind kind = special_function_p (fn);
3615
3616 if (kind == sfk_comparison)
3617 {
3618 /* If the function was declared constexpr, check that the definition
3619 qualifies. Otherwise we can define the function lazily. */
3620 if (DECL_DECLARED_CONSTEXPR_P (fn) && !DECL_INITIAL (fn))
3621 {
3622 /* Prevent GC. */
3623 function_depth++;
3624 synthesize_method (fn);
3625 function_depth--;
3626 }
3627 return;
3628 }
3629
3630 bool fn_const_p = (copy_fn_p (fn) == 2);
3631 /* "if F2 has a non-object parameter of type const C&, the corresponding
3632 non-object parameter of F1 may be of type C&." But not the other way
3633 around. */
3634 if (fn_const_p && imp_const.is_false ())
3635 fn_const_p = false;
3636 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
3637 /*pattern_fn=*/NULL_TREE,
3638 /*inherited_parms=*/NULL_TREE);
3639 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
3640
3641 /* Includes special handling for a default xobj operator. */
3642 auto compare_fn_params = [](tree fn, tree implicit_fn){
3643 tree fn_parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
3644 tree implicit_fn_parms = TYPE_ARG_TYPES (TREE_TYPE (implicit_fn));
3645
3646 if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
3647 {
3648 tree fn_obj_ref_type = TREE_VALUE (fn_parms);
3649 /* We can't default xobj operators with an xobj parameter that is not
3650 an lvalue reference, even if it would correspond. */
3651 if (!TYPE_REF_P (fn_obj_ref_type)
3652 || TYPE_REF_IS_RVALUE (fn_obj_ref_type)
3653 || !object_parms_correspond (fn, implicit_fn,
3654 DECL_CONTEXT (implicit_fn)))
3655 return false;
3656 /* We just compared the object parameters, skip over them before
3657 passing to compparms. */
3658 fn_parms = TREE_CHAIN (fn_parms);
3659 implicit_fn_parms = TREE_CHAIN (implicit_fn_parms);
3660 }
3661 return compparms (fn_parms, implicit_fn_parms);
3662 };
3663
3664 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
3665 TREE_TYPE (TREE_TYPE (implicit_fn)))
3666 || !compare_fn_params (fn, implicit_fn))
3667 maybe_delete_defaulted_fn (fn, implicit_fn);
3668
3669 if (DECL_DELETED_FN (implicit_fn))
3670 {
3671 DECL_DELETED_FN (fn) = 1;
3672 return;
3673 }
3674
3675 /* If a function is explicitly defaulted on its first declaration without an
3676 exception-specification, it is implicitly considered to have the same
3677 exception-specification as if it had been implicitly declared. */
3678 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn))
3679 && DECL_DEFAULTED_IN_CLASS_P (fn))
3680 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
3681
3682 if (DECL_DEFAULTED_IN_CLASS_P (fn)
3683 && DECL_DECLARED_CONSTEXPR_P (implicit_fn))
3684 {
3685 /* Hmm...should we do this for out-of-class too? Should it be OK to
3686 add constexpr later like inline, rather than requiring
3687 declarations to match? */
3688 DECL_DECLARED_CONSTEXPR_P (fn) = true;
3689 if (kind == sfk_constructor)
3690 TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
3691 }
3692
3693 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
3694 && DECL_DECLARED_CONSTEXPR_P (fn))
3695 {
3696 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
3697 {
3698 auto_diagnostic_group d;
3699 error ("explicitly defaulted function %q+D cannot be declared "
3700 "%qs because the implicit declaration is not %qs:", fn,
3701 DECL_IMMEDIATE_FUNCTION_P (fn) ? "consteval" : "constexpr",
3702 "constexpr");
3703 explain_implicit_non_constexpr (fn);
3704 }
3705 DECL_DECLARED_CONSTEXPR_P (fn) = false;
3706 }
3707 }
3708
3709 /* Returns true iff FN can be explicitly defaulted, and gives any
3710 errors if defaulting FN is ill-formed. */
3711
3712 bool
3713 defaultable_fn_check (tree fn)
3714 {
3715 special_function_kind kind = sfk_none;
3716
3717 if (template_parm_scope_p ())
3718 {
3719 error ("a template cannot be defaulted");
3720 return false;
3721 }
3722
3723 if (DECL_CONSTRUCTOR_P (fn))
3724 {
3725 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
3726 kind = sfk_constructor;
3727 else if (copy_fn_p (fn) > 0
3728 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
3729 == void_list_node))
3730 kind = sfk_copy_constructor;
3731 else if (move_fn_p (fn))
3732 kind = sfk_move_constructor;
3733 }
3734 else if (DECL_DESTRUCTOR_P (fn))
3735 kind = sfk_destructor;
3736 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
3737 && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR))
3738 {
3739 if (copy_fn_p (fn))
3740 kind = sfk_copy_assignment;
3741 else if (move_fn_p (fn))
3742 kind = sfk_move_assignment;
3743 }
3744 else if (DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) >= OVL_OP_EQ_EXPR
3745 && DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) <= OVL_OP_SPACESHIP_EXPR)
3746 {
3747 kind = sfk_comparison;
3748 if (!early_check_defaulted_comparison (fn))
3749 return false;
3750 }
3751
3752 /* FIXME: We need to check for xobj member functions here to give better
3753 diagnostics for weird cases where unrelated xobj parameters are given.
3754 We just want to do better than 'cannot be defaulted'. */
3755
3756 if (kind == sfk_none)
3757 {
3758 error ("%qD cannot be defaulted", fn);
3759 return false;
3760 }
3761 else
3762 {
3763 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
3764 t && t != void_list_node; t = TREE_CHAIN (t))
3765 if (TREE_PURPOSE (t))
3766 {
3767 error ("defaulted function %q+D with default argument", fn);
3768 break;
3769 }
3770
3771 /* Avoid do_warn_unused_parameter warnings. */
3772 for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p))
3773 if (DECL_NAME (p))
3774 suppress_warning (p, OPT_Wunused_parameter);
3775
3776 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
3777 /* Defer checking. */;
3778 else if (!processing_template_decl)
3779 defaulted_late_check (fn);
3780
3781 return true;
3782 }
3783 }
3784
3785 /* Add an implicit declaration to TYPE for the kind of function
3786 indicated by SFK. Return the FUNCTION_DECL for the new implicit
3787 declaration. */
3788
3789 tree
3790 lazily_declare_fn (special_function_kind sfk, tree type)
3791 {
3792 tree fn;
3793 /* Whether or not the argument has a const reference type. */
3794 bool const_p = false;
3795
3796 type = TYPE_MAIN_VARIANT (type);
3797
3798 switch (sfk)
3799 {
3800 case sfk_constructor:
3801 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
3802 break;
3803 case sfk_copy_constructor:
3804 const_p = TYPE_HAS_CONST_COPY_CTOR (type);
3805 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
3806 break;
3807 case sfk_move_constructor:
3808 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
3809 break;
3810 case sfk_copy_assignment:
3811 const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
3812 CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
3813 break;
3814 case sfk_move_assignment:
3815 CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
3816 break;
3817 case sfk_destructor:
3818 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
3819 break;
3820 default:
3821 gcc_unreachable ();
3822 }
3823
3824 /* Declare the function. */
3825 fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL);
3826
3827 /* [class.copy]/8 If the class definition declares a move constructor or
3828 move assignment operator, the implicitly declared copy constructor is
3829 defined as deleted.... */
3830 if ((sfk == sfk_copy_assignment || sfk == sfk_copy_constructor)
3831 && cxx_dialect >= cxx11)
3832 {
3833 if (classtype_has_move_assign_or_move_ctor_p (type, true))
3834 DECL_DELETED_FN (fn) = true;
3835 else if (classtype_has_depr_implicit_copy (type))
3836 /* The implicit definition of a copy constructor as defaulted is
3837 deprecated if the class has a user-declared copy assignment operator
3838 or a user-declared destructor. The implicit definition of a copy
3839 assignment operator as defaulted is deprecated if the class has a
3840 user-declared copy constructor or a user-declared destructor (15.4,
3841 15.8). */
3842 TREE_DEPRECATED (fn) = true;
3843 }
3844
3845 /* Destructors and assignment operators may be virtual. */
3846 if (sfk == sfk_destructor
3847 || sfk == sfk_move_assignment
3848 || sfk == sfk_copy_assignment)
3849 check_for_override (fn, type);
3850
3851 /* Add it to the class */
3852 bool added = add_method (type, fn, false);
3853 gcc_assert (added || errorcount);
3854
3855 /* Add it to TYPE_FIELDS. */
3856 if (sfk == sfk_destructor
3857 && DECL_VIRTUAL_P (fn))
3858 /* The ABI requires that a virtual destructor go at the end of the
3859 vtable. */
3860 TYPE_FIELDS (type) = chainon (TYPE_FIELDS (type), fn);
3861 else
3862 {
3863 DECL_CHAIN (fn) = TYPE_FIELDS (type);
3864 TYPE_FIELDS (type) = fn;
3865 }
3866 /* Propagate TYPE_FIELDS. */
3867 fixup_type_variants (type);
3868
3869 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
3870 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (fn))
3871 /* Create appropriate clones. */
3872 clone_cdtor (fn, /*update_methods=*/true);
3873
3874 /* Classes, structs or unions TYPE marked with hotness attributes propagate
3875 the attribute to all methods. This is typically done in
3876 check_bases_and_members, but we must also inject them here for deferred
3877 lazily-declared functions. */
3878 maybe_propagate_warmth_attributes (fn, type);
3879
3880 return fn;
3881 }
3882
3883 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
3884 as there are artificial parms in FN. */
3885
3886 tree
3887 skip_artificial_parms_for (const_tree fn, tree list)
3888 {
3889 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
3890 list = TREE_CHAIN (list);
3891 else
3892 return list;
3893
3894 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
3895 list = TREE_CHAIN (list);
3896 if (DECL_HAS_VTT_PARM_P (fn))
3897 list = TREE_CHAIN (list);
3898 return list;
3899 }
3900
3901 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
3902 artificial parms in FN. */
3903
3904 int
3905 num_artificial_parms_for (const_tree fn)
3906 {
3907 int count = 0;
3908
3909 if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
3910 count++;
3911 else
3912 return 0;
3913
3914 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
3915 count++;
3916 if (DECL_HAS_VTT_PARM_P (fn))
3917 count++;
3918 return count;
3919 }
3920
3921
3922 #include "gt-cp-method.h"