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