c++: local-scope OMP UDR reductions have no template head
[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-2020 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, DECL_SECTION_NAME (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 /* True if we should omit all user-declared parameters from constructor FN,
555 because it is a base clone of a ctor inherited from a virtual base. */
556
557 bool
558 ctor_omit_inherited_parms (tree fn)
559 {
560 if (!flag_new_inheriting_ctors)
561 /* We only optimize away the parameters in the new model. */
562 return false;
563 if (!DECL_BASE_CONSTRUCTOR_P (fn)
564 || !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
565 return false;
566
567 if (FUNCTION_FIRST_USER_PARMTYPE (DECL_ORIGIN (fn)) == void_list_node)
568 /* No user-declared parameters to omit. */
569 return false;
570
571 tree binfo = inherited_ctor_binfo (fn);
572 for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
573 if (BINFO_VIRTUAL_P (binfo))
574 return true;
575
576 return false;
577 }
578
579 /* True iff constructor(s) INH inherited into BINFO initializes INIT_BINFO.
580 This can be true for multiple virtual bases as well as one direct
581 non-virtual base. */
582
583 static bool
584 binfo_inherited_from (tree binfo, tree init_binfo, tree inh)
585 {
586 /* inh is an OVERLOAD if we inherited the same constructor along
587 multiple paths, check all of them. */
588 for (ovl_iterator iter (inh); iter; ++iter)
589 {
590 tree fn = *iter;
591 tree base = DECL_CONTEXT (fn);
592 tree base_binfo = NULL_TREE;
593 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
594 if (BINFO_TYPE (base_binfo) == base)
595 break;
596 if (base_binfo == init_binfo
597 || (flag_new_inheriting_ctors
598 && binfo_inherited_from (base_binfo, init_binfo,
599 DECL_INHERITED_CTOR (fn))))
600 return true;
601 }
602 return false;
603 }
604
605 /* Subroutine of do_build_copy_constructor: Add a mem-initializer for BINFO
606 given the parameter or parameters PARM, possibly inherited constructor
607 base INH, or move flag MOVE_P. */
608
609 static tree
610 add_one_base_init (tree binfo, tree parm, bool move_p, tree inh,
611 tree member_init_list)
612 {
613 tree init;
614 if (inh)
615 {
616 /* An inheriting constructor only has a mem-initializer for
617 the base it inherits from. */
618 if (!binfo_inherited_from (TYPE_BINFO (current_class_type), binfo, inh))
619 return member_init_list;
620
621 tree *p = &init;
622 init = NULL_TREE;
623 for (; parm; parm = DECL_CHAIN (parm))
624 {
625 tree exp = forward_parm (parm);
626 *p = build_tree_list (NULL_TREE, exp);
627 p = &TREE_CHAIN (*p);
628 }
629 }
630 else
631 {
632 init = build_base_path (PLUS_EXPR, parm, binfo, 1,
633 tf_warning_or_error);
634 if (move_p)
635 init = move (init);
636 init = build_tree_list (NULL_TREE, init);
637 }
638 return tree_cons (binfo, init, member_init_list);
639 }
640
641 /* Generate code for default X(X&) or X(X&&) constructor or an inheriting
642 constructor. */
643
644 static void
645 do_build_copy_constructor (tree fndecl)
646 {
647 tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
648 bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
649 bool trivial = trivial_fn_p (fndecl);
650 tree inh = DECL_INHERITED_CTOR (fndecl);
651
652 if (!inh)
653 parm = convert_from_reference (parm);
654
655 if (trivial)
656 {
657 if (is_empty_class (current_class_type))
658 /* Don't copy the padding byte; it might not have been allocated
659 if *this is a base subobject. */;
660 else if (tree_int_cst_equal (TYPE_SIZE (current_class_type),
661 CLASSTYPE_SIZE (current_class_type)))
662 {
663 tree t = build2 (INIT_EXPR, void_type_node, current_class_ref, parm);
664 finish_expr_stmt (t);
665 }
666 else
667 {
668 /* We must only copy the non-tail padding parts. */
669 tree base_size = CLASSTYPE_SIZE_UNIT (current_class_type);
670 base_size = size_binop (MINUS_EXPR, base_size, size_int (1));
671 tree array_type = build_array_type (unsigned_char_type_node,
672 build_index_type (base_size));
673 tree alias_set = build_int_cst (TREE_TYPE (current_class_ptr), 0);
674 tree lhs = build2 (MEM_REF, array_type,
675 current_class_ptr, alias_set);
676 tree rhs = build2 (MEM_REF, array_type,
677 TREE_OPERAND (parm, 0), alias_set);
678 tree t = build2 (INIT_EXPR, void_type_node, lhs, rhs);
679 finish_expr_stmt (t);
680 }
681 }
682 else
683 {
684 tree member_init_list = NULL_TREE;
685 int i;
686 tree binfo, base_binfo;
687 vec<tree, va_gc> *vbases;
688
689 /* Initialize all the base-classes with the parameter converted
690 to their type so that we get their copy constructor and not
691 another constructor that takes current_class_type. We must
692 deal with the binfo's directly as a direct base might be
693 inaccessible due to ambiguity. */
694 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
695 vec_safe_iterate (vbases, i, &binfo); i++)
696 {
697 member_init_list = add_one_base_init (binfo, parm, move_p, inh,
698 member_init_list);
699 }
700
701 for (binfo = TYPE_BINFO (current_class_type), i = 0;
702 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
703 {
704 if (BINFO_VIRTUAL_P (base_binfo))
705 continue;
706 member_init_list = add_one_base_init (base_binfo, parm, move_p,
707 inh, member_init_list);
708 }
709
710 if (!inh)
711 {
712 int cvquals = cp_type_quals (TREE_TYPE (parm));
713
714 for (tree fields = TYPE_FIELDS (current_class_type);
715 fields; fields = DECL_CHAIN (fields))
716 {
717 tree field = fields;
718 tree expr_type;
719
720 if (TREE_CODE (field) != FIELD_DECL)
721 continue;
722
723 expr_type = TREE_TYPE (field);
724 if (DECL_NAME (field))
725 {
726 if (VFIELD_NAME_P (DECL_NAME (field)))
727 continue;
728 }
729 else if (ANON_AGGR_TYPE_P (expr_type) && TYPE_FIELDS (expr_type))
730 /* Just use the field; anonymous types can't have
731 nontrivial copy ctors or assignment ops or this
732 function would be deleted. */;
733 else
734 continue;
735
736 /* Compute the type of "init->field". If the copy-constructor
737 parameter is, for example, "const S&", and the type of
738 the field is "T", then the type will usually be "const
739 T". (There are no cv-qualified variants of reference
740 types.) */
741 if (!TYPE_REF_P (expr_type))
742 {
743 int quals = cvquals;
744
745 if (DECL_MUTABLE_P (field))
746 quals &= ~TYPE_QUAL_CONST;
747 quals |= cp_type_quals (expr_type);
748 expr_type = cp_build_qualified_type (expr_type, quals);
749 }
750
751 tree init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
752 if (move_p && !TYPE_REF_P (expr_type)
753 /* 'move' breaks bit-fields, and has no effect for scalars. */
754 && !scalarish_type_p (expr_type))
755 init = move (init);
756 init = build_tree_list (NULL_TREE, init);
757
758 member_init_list = tree_cons (field, init, member_init_list);
759 }
760 }
761
762 finish_mem_initializers (member_init_list);
763 }
764 }
765
766 static void
767 do_build_copy_assign (tree fndecl)
768 {
769 tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl));
770 tree compound_stmt;
771 bool move_p = move_fn_p (fndecl);
772 bool trivial = trivial_fn_p (fndecl);
773 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
774
775 compound_stmt = begin_compound_stmt (0);
776 parm = convert_from_reference (parm);
777
778 if (trivial
779 && is_empty_class (current_class_type))
780 /* Don't copy the padding byte; it might not have been allocated
781 if *this is a base subobject. */;
782 else if (trivial)
783 {
784 tree t = build2 (MODIFY_EXPR, void_type_node, current_class_ref, parm);
785 finish_expr_stmt (t);
786 }
787 else
788 {
789 tree fields;
790 int cvquals = cp_type_quals (TREE_TYPE (parm));
791 int i;
792 tree binfo, base_binfo;
793
794 /* Assign to each of the direct base classes. */
795 for (binfo = TYPE_BINFO (current_class_type), i = 0;
796 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
797 {
798 tree converted_parm;
799
800 /* We must convert PARM directly to the base class
801 explicitly since the base class may be ambiguous. */
802 converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
803 tf_warning_or_error);
804 if (move_p)
805 converted_parm = move (converted_parm);
806 /* Call the base class assignment operator. */
807 releasing_vec parmvec (make_tree_vector_single (converted_parm));
808 finish_expr_stmt
809 (build_special_member_call (current_class_ref,
810 assign_op_identifier,
811 &parmvec,
812 base_binfo,
813 flags,
814 tf_warning_or_error));
815 }
816
817 /* Assign to each of the non-static data members. */
818 for (fields = TYPE_FIELDS (current_class_type);
819 fields;
820 fields = DECL_CHAIN (fields))
821 {
822 tree comp = current_class_ref;
823 tree init = parm;
824 tree field = fields;
825 tree expr_type;
826 int quals;
827
828 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
829 continue;
830
831 expr_type = TREE_TYPE (field);
832
833 if (CP_TYPE_CONST_P (expr_type))
834 {
835 error ("non-static const member %q#D, cannot use default "
836 "assignment operator", field);
837 continue;
838 }
839 else if (TYPE_REF_P (expr_type))
840 {
841 error ("non-static reference member %q#D, cannot use "
842 "default assignment operator", field);
843 continue;
844 }
845
846 if (DECL_NAME (field))
847 {
848 if (VFIELD_NAME_P (DECL_NAME (field)))
849 continue;
850 }
851 else if (ANON_AGGR_TYPE_P (expr_type)
852 && TYPE_FIELDS (expr_type) != NULL_TREE)
853 /* Just use the field; anonymous types can't have
854 nontrivial copy ctors or assignment ops or this
855 function would be deleted. */;
856 else
857 continue;
858
859 comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
860
861 /* Compute the type of init->field */
862 quals = cvquals;
863 if (DECL_MUTABLE_P (field))
864 quals &= ~TYPE_QUAL_CONST;
865 expr_type = cp_build_qualified_type (expr_type, quals);
866
867 init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
868 if (move_p && !TYPE_REF_P (expr_type)
869 /* 'move' breaks bit-fields, and has no effect for scalars. */
870 && !scalarish_type_p (expr_type))
871 init = move (init);
872
873 if (DECL_NAME (field))
874 init = cp_build_modify_expr (input_location, comp, NOP_EXPR, init,
875 tf_warning_or_error);
876 else
877 init = build2 (MODIFY_EXPR, TREE_TYPE (comp), comp, init);
878 finish_expr_stmt (init);
879 }
880 }
881 finish_return_stmt (current_class_ref);
882 finish_compound_stmt (compound_stmt);
883 }
884
885 /* C++20 <compare> comparison category types. */
886
887 enum comp_cat_tag
888 {
889 cc_partial_ordering,
890 cc_weak_ordering,
891 cc_strong_ordering,
892 cc_last
893 };
894
895 /* Names of the comparison categories and their value members, to be indexed by
896 comp_cat_tag enumerators. genericize_spaceship below relies on the ordering
897 of the members. */
898
899 struct comp_cat_info_t
900 {
901 const char *name;
902 const char *members[4];
903 };
904 static const comp_cat_info_t comp_cat_info[cc_last]
905 = {
906 { "partial_ordering", { "equivalent", "greater", "less", "unordered" } },
907 { "weak_ordering", { "equivalent", "greater", "less" } },
908 { "strong_ordering", { "equal", "greater", "less" } }
909 };
910
911 /* A cache of the category types to speed repeated lookups. */
912
913 static GTY((deletable)) tree comp_cat_cache[cc_last];
914
915 /* Look up one of the result variables in the comparison category type. */
916
917 static tree
918 lookup_comparison_result (tree type, const char *name_str,
919 tsubst_flags_t complain = tf_warning_or_error)
920 {
921 tree name = get_identifier (name_str);
922 tree decl = lookup_qualified_name (type, name);
923 if (TREE_CODE (decl) != VAR_DECL)
924 {
925 if (complain & tf_error)
926 {
927 auto_diagnostic_group d;
928 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
929 qualified_name_lookup_error (type, name, decl, input_location);
930 else
931 error ("%qD is not a static data member", decl);
932 inform (input_location, "determining value of %qs", "operator<=>");
933 }
934 return error_mark_node;
935 }
936 return decl;
937 }
938
939 /* Look up a <compare> comparison category type in std. */
940
941 static tree
942 lookup_comparison_category (comp_cat_tag tag,
943 tsubst_flags_t complain = tf_warning_or_error)
944 {
945 if (tree cached = comp_cat_cache[tag])
946 return cached;
947
948 tree name = get_identifier (comp_cat_info[tag].name);
949 tree decl = lookup_qualified_name (std_node, name);
950 if (TREE_CODE (decl) != TYPE_DECL)
951 {
952 if (complain & tf_error)
953 {
954 auto_diagnostic_group d;
955 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
956 qualified_name_lookup_error (std_node, name, decl, input_location);
957 else
958 error ("%qD is not a type", decl);
959 inform (input_location, "forming type of %qs", "operator<=>");
960 }
961 return error_mark_node;
962 }
963 /* Also make sure we can look up the value members now, since we won't
964 really use them until genericize time. */
965 tree type = TREE_TYPE (decl);
966 for (int i = 0; i < 4; ++i)
967 {
968 const char *p = comp_cat_info[tag].members[i];
969 if (!p) break;
970 if (lookup_comparison_result (type, p, complain)
971 == error_mark_node)
972 return error_mark_node;
973 }
974 return comp_cat_cache[tag] = type;
975 }
976
977 /* Wrapper that takes the tag rather than the type. */
978
979 static tree
980 lookup_comparison_result (comp_cat_tag tag, const char *name_str,
981 tsubst_flags_t complain = tf_warning_or_error)
982 {
983 tree type = lookup_comparison_category (tag, complain);
984 return lookup_comparison_result (type, name_str, complain);
985 }
986
987 /* Wrapper that takes the index into the members array instead of the name. */
988
989 static tree
990 lookup_comparison_result (comp_cat_tag tag, tree type, int idx)
991 {
992 const char *name_str = comp_cat_info[tag].members[idx];
993 if (!name_str)
994 return NULL_TREE;
995 return lookup_comparison_result (type, name_str);
996 }
997
998 /* Does TYPE correspond to TAG? */
999
1000 static bool
1001 is_cat (tree type, comp_cat_tag tag)
1002 {
1003 tree name = TYPE_LINKAGE_IDENTIFIER (type);
1004 return id_equal (name, comp_cat_info[tag].name);
1005 }
1006
1007 /* Return the comp_cat_tag for TYPE. */
1008
1009 static comp_cat_tag
1010 cat_tag_for (tree type)
1011 {
1012 for (int i = 0; i < cc_last; ++i)
1013 {
1014 comp_cat_tag tag = (comp_cat_tag)i;
1015 if (is_cat (type, tag))
1016 return tag;
1017 }
1018 return cc_last;
1019 }
1020
1021 /* Return the comparison category tag of a <=> expression with non-class type
1022 OPTYPE. */
1023
1024 static comp_cat_tag
1025 spaceship_comp_cat (tree optype)
1026 {
1027 if (INTEGRAL_OR_ENUMERATION_TYPE_P (optype) || TYPE_PTROBV_P (optype))
1028 return cc_strong_ordering;
1029 else if (TREE_CODE (optype) == REAL_TYPE)
1030 return cc_partial_ordering;
1031
1032 /* ??? should vector <=> produce a vector of one of the above? */
1033 gcc_unreachable ();
1034 }
1035
1036 /* Return the comparison category type of a <=> expression with non-class type
1037 OPTYPE. */
1038
1039 tree
1040 spaceship_type (tree optype, tsubst_flags_t complain)
1041 {
1042 comp_cat_tag tag = spaceship_comp_cat (optype);
1043 return lookup_comparison_category (tag, complain);
1044 }
1045
1046 /* Turn <=> with type TYPE and operands OP0 and OP1 into GENERIC. */
1047
1048 tree
1049 genericize_spaceship (tree type, tree op0, tree op1)
1050 {
1051 /* ??? maybe optimize based on knowledge of representation? */
1052 comp_cat_tag tag = cat_tag_for (type);
1053 gcc_checking_assert (tag < cc_last);
1054
1055 tree r;
1056 op0 = save_expr (op0);
1057 op1 = save_expr (op1);
1058
1059 tree gt = lookup_comparison_result (tag, type, 1);
1060
1061 if (tag == cc_partial_ordering)
1062 {
1063 /* op0 == op1 ? equivalent : op0 < op1 ? less :
1064 op0 > op1 ? greater : unordered */
1065 tree uo = lookup_comparison_result (tag, type, 3);
1066 tree comp = fold_build2 (GT_EXPR, boolean_type_node, op0, op1);
1067 r = fold_build3 (COND_EXPR, type, comp, gt, uo);
1068 }
1069 else
1070 /* op0 == op1 ? equal : op0 < op1 ? less : greater */
1071 r = gt;
1072
1073 tree lt = lookup_comparison_result (tag, type, 2);
1074 tree comp = fold_build2 (LT_EXPR, boolean_type_node, op0, op1);
1075 r = fold_build3 (COND_EXPR, type, comp, lt, r);
1076
1077 tree eq = lookup_comparison_result (tag, type, 0);
1078 comp = fold_build2 (EQ_EXPR, boolean_type_node, op0, op1);
1079 r = fold_build3 (COND_EXPR, type, comp, eq, r);
1080
1081 /* Wrap the whole thing in a TARGET_EXPR like build_conditional_expr_1. */
1082 r = get_target_expr (r);
1083
1084 return r;
1085 }
1086
1087 /* Check that the signature of a defaulted comparison operator is
1088 well-formed. */
1089
1090 static bool
1091 early_check_defaulted_comparison (tree fn)
1092 {
1093 location_t loc = DECL_SOURCE_LOCATION (fn);
1094 tree ctx;
1095 if (DECL_CLASS_SCOPE_P (fn))
1096 ctx = DECL_CONTEXT (fn);
1097 else
1098 ctx = DECL_FRIEND_CONTEXT (fn);
1099 bool ok = true;
1100
1101 if (cxx_dialect < cxx20)
1102 {
1103 error_at (loc, "defaulted %qD only available with %<-std=c++20%> or "
1104 "%<-std=gnu++20%>", fn);
1105 return false;
1106 }
1107
1108 if (!DECL_OVERLOADED_OPERATOR_IS (fn, SPACESHIP_EXPR)
1109 && !same_type_p (TREE_TYPE (TREE_TYPE (fn)), boolean_type_node))
1110 {
1111 diagnostic_t kind = DK_UNSPECIFIED;
1112 int opt = 0;
1113 if (is_auto (TREE_TYPE (fn)))
1114 kind = DK_PEDWARN;
1115 else
1116 kind = DK_ERROR;
1117 emit_diagnostic (kind, loc, opt,
1118 "defaulted %qD must return %<bool%>", fn);
1119 if (kind == DK_ERROR)
1120 ok = false;
1121 }
1122
1123 bool mem = DECL_NONSTATIC_MEMBER_FUNCTION_P (fn);
1124 if (mem && type_memfn_quals (TREE_TYPE (fn)) != TYPE_QUAL_CONST)
1125 {
1126 error_at (loc, "defaulted %qD must be %<const%>", fn);
1127 ok = false;
1128 }
1129 if (mem && type_memfn_rqual (TREE_TYPE (fn)) == REF_QUAL_RVALUE)
1130 {
1131 error_at (loc, "defaulted %qD must not have %<&&%> ref-qualifier", fn);
1132 ok = false;
1133 }
1134 tree parmnode = FUNCTION_FIRST_USER_PARMTYPE (fn);
1135 bool saw_byval = false;
1136 bool saw_byref = mem;
1137 bool saw_bad = false;
1138 for (; parmnode != void_list_node; parmnode = TREE_CHAIN (parmnode))
1139 {
1140 tree parmtype = TREE_VALUE (parmnode);
1141 if (CLASS_TYPE_P (parmtype))
1142 saw_byval = true;
1143 else if (TREE_CODE (parmtype) == REFERENCE_TYPE
1144 && !TYPE_REF_IS_RVALUE (parmtype)
1145 && TYPE_QUALS (TREE_TYPE (parmtype)) == TYPE_QUAL_CONST)
1146 {
1147 saw_byref = true;
1148 parmtype = TREE_TYPE (parmtype);
1149 }
1150 else
1151 saw_bad = true;
1152
1153 if (!saw_bad && !ctx)
1154 {
1155 /* Defaulted outside the class body. */
1156 ctx = TYPE_MAIN_VARIANT (parmtype);
1157 if (!is_friend (ctx, fn))
1158 error_at (loc, "defaulted %qD is not a friend of %qT", fn, ctx);
1159 }
1160 else if (!same_type_ignoring_top_level_qualifiers_p (parmtype, ctx))
1161 saw_bad = true;
1162 }
1163
1164 if (saw_bad || (saw_byval && saw_byref))
1165 {
1166 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
1167 error_at (loc, "defaulted member %qD must have parameter type "
1168 "%<const %T&%>", fn, ctx);
1169 else if (saw_bad)
1170 error_at (loc, "defaulted %qD must have parameters of either type "
1171 "%<const %T&%> or %qT", fn, ctx, ctx);
1172 else
1173 error_at (loc, "defaulted %qD must have parameters of either type "
1174 "%<const %T&%> or %qT, not both", fn, ctx, ctx);
1175 ok = false;
1176 }
1177
1178 /* We still need to deduce deleted/constexpr/noexcept and maybe return. */
1179 DECL_MAYBE_DELETED (fn) = ok;
1180
1181 return ok;
1182 }
1183
1184 /* Subroutine of build_comparison_op. Given the vec of memberwise
1185 comparisons COMPS, calculate the overall comparison category for
1186 operator<=>. */
1187
1188 static tree
1189 common_comparison_type (vec<tree> &comps)
1190 {
1191 tree seen[cc_last] = {};
1192
1193 for (unsigned i = 0; i < comps.length(); ++i)
1194 {
1195 tree comp = comps[i];
1196 tree ctype = TREE_TYPE (comp);
1197 comp_cat_tag tag = cat_tag_for (ctype);
1198 /* build_comparison_op already checked this. */
1199 gcc_checking_assert (tag < cc_last);
1200 seen[tag] = ctype;
1201 }
1202
1203 /* Otherwise, if at least one T i is std::partial_ordering, U is
1204 std::partial_ordering. */
1205 if (tree t = seen[cc_partial_ordering]) return t;
1206
1207 /* Otherwise, if at least one T i is std::weak_ordering, U is
1208 std::weak_ordering. */
1209 if (tree t = seen[cc_weak_ordering]) return t;
1210
1211 /* Otherwise, U is std::strong_ordering. */
1212 if (tree t = seen[cc_strong_ordering]) return t;
1213 return lookup_comparison_category (cc_strong_ordering);
1214 }
1215
1216 /* Data structure for build_comparison_op. */
1217
1218 struct comp_info
1219 {
1220 tree fndecl;
1221 location_t loc;
1222 bool defining;
1223 bool first_time;
1224 bool constexp;
1225 bool was_constexp;
1226 bool noex;
1227
1228 comp_info (tree fndecl, tsubst_flags_t &complain)
1229 : fndecl (fndecl)
1230 {
1231 loc = DECL_SOURCE_LOCATION (fndecl);
1232
1233 /* We only have tf_error set when we're called from
1234 explain_invalid_constexpr_fn or maybe_explain_implicit_delete. */
1235 defining = !(complain & tf_error);
1236
1237 first_time = DECL_MAYBE_DELETED (fndecl);
1238 DECL_MAYBE_DELETED (fndecl) = false;
1239
1240 /* Do we want to try to set constexpr? */
1241 was_constexp = DECL_DECLARED_CONSTEXPR_P (fndecl);
1242 constexp = first_time;
1243 if (constexp)
1244 /* Set this for var_in_constexpr_fn. */
1245 DECL_DECLARED_CONSTEXPR_P (fndecl) = true;
1246
1247 /* Do we want to try to set noexcept? */
1248 noex = first_time;
1249 if (noex)
1250 {
1251 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fndecl));
1252 if (raises && !UNEVALUATED_NOEXCEPT_SPEC_P (raises))
1253 /* There was an explicit exception-specification. */
1254 noex = false;
1255 }
1256 }
1257
1258 /* EXPR is an expression built as part of the function body.
1259 Adjust the properties appropriately. */
1260 void check (tree expr)
1261 {
1262 if (expr == error_mark_node)
1263 DECL_DELETED_FN (fndecl) = true;
1264 if ((constexp || was_constexp)
1265 && !potential_rvalue_constant_expression (expr))
1266 {
1267 if (was_constexp)
1268 require_potential_rvalue_constant_expression (expr);
1269 else
1270 constexp = false;
1271 }
1272 if (noex && !expr_noexcept_p (expr, tf_none))
1273 noex = false;
1274 }
1275
1276 ~comp_info ()
1277 {
1278 if (first_time)
1279 {
1280 DECL_DECLARED_CONSTEXPR_P (fndecl) = constexp || was_constexp;
1281 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fndecl));
1282 if (!raises || UNEVALUATED_NOEXCEPT_SPEC_P (raises))
1283 {
1284 raises = noex ? noexcept_true_spec : noexcept_false_spec;
1285 TREE_TYPE (fndecl) = build_exception_variant (TREE_TYPE (fndecl),
1286 raises);
1287 }
1288 }
1289 }
1290 };
1291
1292 /* Build up the definition of a defaulted comparison operator. Unlike other
1293 defaulted functions that use synthesized_method_walk to determine whether
1294 the function is e.g. deleted, for comparisons we use the same code. We try
1295 to use synthesize_method at the earliest opportunity and bail out if the
1296 function ends up being deleted. */
1297
1298 static void
1299 build_comparison_op (tree fndecl, tsubst_flags_t complain)
1300 {
1301 comp_info info (fndecl, complain);
1302
1303 if (!info.defining && !(complain & tf_error) && !DECL_MAYBE_DELETED (fndecl))
1304 return;
1305
1306 int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
1307 const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (DECL_NAME (fndecl));
1308 tree_code code = op->tree_code;
1309
1310 tree lhs = DECL_ARGUMENTS (fndecl);
1311 tree rhs = DECL_CHAIN (lhs);
1312 if (is_this_parameter (lhs))
1313 lhs = cp_build_fold_indirect_ref (lhs);
1314 else
1315 lhs = convert_from_reference (lhs);
1316 rhs = convert_from_reference (rhs);
1317 tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (lhs));
1318
1319 iloc_sentinel ils (info.loc);
1320
1321 /* A defaulted comparison operator function for class C is defined as
1322 deleted if ... C has variant members. */
1323 if (TREE_CODE (ctype) == UNION_TYPE
1324 && next_initializable_field (TYPE_FIELDS (ctype)))
1325 {
1326 if (complain & tf_error)
1327 inform (info.loc, "cannot default compare union %qT", ctype);
1328 DECL_DELETED_FN (fndecl) = true;
1329 return;
1330 }
1331
1332 tree compound_stmt = NULL_TREE;
1333 if (info.defining)
1334 compound_stmt = begin_compound_stmt (0);
1335 else
1336 ++cp_unevaluated_operand;
1337
1338 tree rettype = TREE_TYPE (TREE_TYPE (fndecl));
1339 if (code != SPACESHIP_EXPR && is_auto (rettype))
1340 {
1341 rettype = boolean_type_node;
1342 apply_deduced_return_type (fndecl, rettype);
1343 }
1344
1345 if (code == EQ_EXPR || code == SPACESHIP_EXPR)
1346 {
1347 bool bad = false;
1348 auto_vec<tree> comps;
1349
1350 /* Compare each of the subobjects. Note that we get bases from
1351 next_initializable_field because we're past C++17. */
1352 for (tree field = next_initializable_field (TYPE_FIELDS (ctype));
1353 field;
1354 field = next_initializable_field (DECL_CHAIN (field)))
1355 {
1356 tree expr_type = TREE_TYPE (field);
1357
1358 /* A defaulted comparison operator function for class C is defined as
1359 deleted if any non-static data member of C is of reference type or
1360 C has variant members. */
1361 if (TREE_CODE (expr_type) == REFERENCE_TYPE)
1362 {
1363 if (complain & tf_error)
1364 inform (DECL_SOURCE_LOCATION (field), "cannot default compare "
1365 "reference member %qD", field);
1366 bad = true;
1367 continue;
1368 }
1369 else if (ANON_UNION_TYPE_P (expr_type)
1370 && next_initializable_field (TYPE_FIELDS (expr_type)))
1371 {
1372 if (complain & tf_error)
1373 inform (DECL_SOURCE_LOCATION (field), "cannot default compare "
1374 "anonymous union member");
1375 bad = true;
1376 continue;
1377 }
1378
1379 tree lhs_mem = build3 (COMPONENT_REF, expr_type, lhs, field,
1380 NULL_TREE);
1381 tree rhs_mem = build3 (COMPONENT_REF, expr_type, rhs, field,
1382 NULL_TREE);
1383 tree comp = build_new_op (info.loc, code, flags, lhs_mem, rhs_mem,
1384 NULL_TREE, NULL, complain);
1385 if (comp == error_mark_node)
1386 {
1387 bad = true;
1388 continue;
1389 }
1390 if (code == SPACESHIP_EXPR
1391 && cat_tag_for (TREE_TYPE (comp)) == cc_last)
1392 {
1393 /* The operator function is defined as deleted if ... Ri is not a
1394 comparison category type. */
1395 if (complain & tf_error)
1396 inform (DECL_SOURCE_LOCATION (field),
1397 "three-way comparison of %qD has type %qT, not a "
1398 "comparison category type", field, TREE_TYPE (comp));
1399 bad = true;
1400 continue;
1401 }
1402 comps.safe_push (comp);
1403 }
1404 if (code == SPACESHIP_EXPR && is_auto (rettype))
1405 {
1406 rettype = common_comparison_type (comps);
1407 apply_deduced_return_type (fndecl, rettype);
1408 }
1409 if (bad)
1410 {
1411 DECL_DELETED_FN (fndecl) = true;
1412 goto out;
1413 }
1414 for (unsigned i = 0; i < comps.length(); ++i)
1415 {
1416 tree comp = comps[i];
1417 tree eq, retval = NULL_TREE, if_ = NULL_TREE;
1418 if (info.defining)
1419 if_ = begin_if_stmt ();
1420 /* Spaceship is specified to use !=, but for the comparison category
1421 types, != is equivalent to !(==), so let's use == directly. */
1422 if (code == EQ_EXPR)
1423 {
1424 /* if (x==y); else return false; */
1425 eq = comp;
1426 retval = boolean_false_node;
1427 }
1428 else
1429 {
1430 /* if (auto v = x<=>y, v == 0); else return v; */
1431 if (TREE_CODE (comp) == SPACESHIP_EXPR)
1432 TREE_TYPE (comp) = rettype;
1433 else
1434 comp = build_static_cast (input_location, rettype, comp,
1435 complain);
1436 info.check (comp);
1437 if (info.defining)
1438 {
1439 tree var = create_temporary_var (rettype);
1440 pushdecl (var);
1441 cp_finish_decl (var, comp, false, NULL_TREE, flags);
1442 comp = retval = var;
1443 }
1444 eq = build_new_op (info.loc, EQ_EXPR, flags, comp,
1445 integer_zero_node, NULL_TREE, NULL,
1446 complain);
1447 }
1448 tree ceq = contextual_conv_bool (eq, complain);
1449 info.check (ceq);
1450 if (info.defining)
1451 {
1452 finish_if_stmt_cond (ceq, if_);
1453 finish_then_clause (if_);
1454 begin_else_clause (if_);
1455 finish_return_stmt (retval);
1456 finish_else_clause (if_);
1457 finish_if_stmt (if_);
1458 }
1459 }
1460 if (info.defining)
1461 {
1462 tree val;
1463 if (code == EQ_EXPR)
1464 val = boolean_true_node;
1465 else
1466 {
1467 tree seql = lookup_comparison_result (cc_strong_ordering,
1468 "equal", complain);
1469 val = build_static_cast (input_location, rettype, seql,
1470 complain);
1471 }
1472 finish_return_stmt (val);
1473 }
1474 }
1475 else if (code == NE_EXPR)
1476 {
1477 tree comp = build_new_op (info.loc, EQ_EXPR, flags, lhs, rhs,
1478 NULL_TREE, NULL, complain);
1479 comp = contextual_conv_bool (comp, complain);
1480 info.check (comp);
1481 if (info.defining)
1482 {
1483 tree neg = build1 (TRUTH_NOT_EXPR, boolean_type_node, comp);
1484 finish_return_stmt (neg);
1485 }
1486 }
1487 else
1488 {
1489 tree comp = build_new_op (info.loc, SPACESHIP_EXPR, flags, lhs, rhs,
1490 NULL_TREE, NULL, complain);
1491 tree comp2 = build_new_op (info.loc, code, flags, comp, integer_zero_node,
1492 NULL_TREE, NULL, complain);
1493 info.check (comp2);
1494 if (info.defining)
1495 finish_return_stmt (comp2);
1496 }
1497
1498 out:
1499 if (info.defining)
1500 finish_compound_stmt (compound_stmt);
1501 else
1502 --cp_unevaluated_operand;
1503 }
1504
1505 /* True iff DECL is an implicitly-declared special member function with no real
1506 source location, so we can use its DECL_SOURCE_LOCATION to remember where we
1507 triggered its synthesis. */
1508
1509 bool
1510 decl_remember_implicit_trigger_p (tree decl)
1511 {
1512 if (!DECL_ARTIFICIAL (decl))
1513 return false;
1514 special_function_kind sfk = special_function_p (decl);
1515 /* Inherited constructors have the location of their using-declaration, and
1516 operator== has the location of the corresponding operator<=>. */
1517 return (sfk != sfk_inheriting_constructor
1518 && sfk != sfk_comparison);
1519 }
1520
1521 /* Synthesize FNDECL, a non-static member function. */
1522
1523 void
1524 synthesize_method (tree fndecl)
1525 {
1526 bool nested = (current_function_decl != NULL_TREE);
1527 tree context = decl_function_context (fndecl);
1528 bool need_body = true;
1529 tree stmt;
1530 location_t save_input_location = input_location;
1531 int error_count = errorcount;
1532 int warning_count = warningcount + werrorcount;
1533 special_function_kind sfk = special_function_p (fndecl);
1534
1535 /* Reset the source location, we might have been previously
1536 deferred, and thus have saved where we were first needed. */
1537 if (decl_remember_implicit_trigger_p (fndecl))
1538 DECL_SOURCE_LOCATION (fndecl)
1539 = DECL_SOURCE_LOCATION (TYPE_NAME (DECL_CONTEXT (fndecl)));
1540
1541 /* If we've been asked to synthesize a clone, just synthesize the
1542 cloned function instead. Doing so will automatically fill in the
1543 body for the clone. */
1544 if (DECL_CLONED_FUNCTION_P (fndecl))
1545 fndecl = DECL_CLONED_FUNCTION (fndecl);
1546
1547 /* We may be in the middle of deferred access check. Disable
1548 it now. */
1549 push_deferring_access_checks (dk_no_deferred);
1550
1551 if (! context)
1552 push_to_top_level ();
1553 else if (nested)
1554 push_function_context ();
1555
1556 input_location = DECL_SOURCE_LOCATION (fndecl);
1557
1558 start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
1559 stmt = begin_function_body ();
1560
1561 if (DECL_ASSIGNMENT_OPERATOR_P (fndecl)
1562 && DECL_OVERLOADED_OPERATOR_IS (fndecl, NOP_EXPR))
1563 {
1564 do_build_copy_assign (fndecl);
1565 need_body = false;
1566 }
1567 else if (DECL_CONSTRUCTOR_P (fndecl))
1568 {
1569 tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
1570 if (arg_chain != void_list_node)
1571 do_build_copy_constructor (fndecl);
1572 else
1573 finish_mem_initializers (NULL_TREE);
1574 }
1575 else if (sfk == sfk_comparison)
1576 {
1577 /* Pass tf_none so the function is just deleted if there's a problem. */
1578 build_comparison_op (fndecl, tf_none);
1579 need_body = false;
1580 }
1581
1582 /* If we haven't yet generated the body of the function, just
1583 generate an empty compound statement. */
1584 if (need_body)
1585 {
1586 tree compound_stmt;
1587 compound_stmt = begin_compound_stmt (BCS_FN_BODY);
1588 finish_compound_stmt (compound_stmt);
1589 }
1590
1591 finish_function_body (stmt);
1592 finish_function (/*inline_p=*/false);
1593
1594 if (!DECL_DELETED_FN (fndecl))
1595 expand_or_defer_fn (fndecl);
1596
1597 input_location = save_input_location;
1598
1599 if (! context)
1600 pop_from_top_level ();
1601 else if (nested)
1602 pop_function_context ();
1603
1604 pop_deferring_access_checks ();
1605
1606 if (error_count != errorcount || warning_count != warningcount + werrorcount)
1607 if (DECL_ARTIFICIAL (fndecl))
1608 inform (input_location, "synthesized method %qD first required here",
1609 fndecl);
1610 }
1611
1612 /* Build a reference to type TYPE with cv-quals QUALS, which is an
1613 rvalue if RVALUE is true. */
1614
1615 static tree
1616 build_stub_type (tree type, int quals, bool rvalue)
1617 {
1618 tree argtype = cp_build_qualified_type (type, quals);
1619 return cp_build_reference_type (argtype, rvalue);
1620 }
1621
1622 /* Build a dummy glvalue from dereferencing a dummy reference of type
1623 REFTYPE. */
1624
1625 static tree
1626 build_stub_object (tree reftype)
1627 {
1628 if (!TYPE_REF_P (reftype))
1629 reftype = cp_build_reference_type (reftype, /*rval*/true);
1630 tree stub = build1 (CONVERT_EXPR, reftype, integer_one_node);
1631 return convert_from_reference (stub);
1632 }
1633
1634 /* Determine which function will be called when looking up NAME in TYPE,
1635 called with a single ARGTYPE argument, or no argument if ARGTYPE is
1636 null. FLAGS and COMPLAIN are as for build_new_method_call.
1637
1638 Returns a FUNCTION_DECL if all is well.
1639 Returns NULL_TREE if overload resolution failed.
1640 Returns error_mark_node if the chosen function cannot be called. */
1641
1642 static tree
1643 locate_fn_flags (tree type, tree name, tree argtype, int flags,
1644 tsubst_flags_t complain)
1645 {
1646 tree ob, fn, fns, binfo, rval;
1647
1648 if (TYPE_P (type))
1649 binfo = TYPE_BINFO (type);
1650 else
1651 {
1652 binfo = type;
1653 type = BINFO_TYPE (binfo);
1654 }
1655
1656 ob = build_stub_object (cp_build_reference_type (type, false));
1657 releasing_vec args;
1658 if (argtype)
1659 {
1660 if (TREE_CODE (argtype) == TREE_LIST)
1661 {
1662 for (tree elt = argtype; elt && elt != void_list_node;
1663 elt = TREE_CHAIN (elt))
1664 {
1665 tree type = TREE_VALUE (elt);
1666 tree arg = build_stub_object (type);
1667 vec_safe_push (args, arg);
1668 }
1669 }
1670 else
1671 {
1672 tree arg = build_stub_object (argtype);
1673 args->quick_push (arg);
1674 }
1675 }
1676
1677 fns = lookup_fnfields (binfo, name, 0, complain);
1678 rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
1679
1680 if (fn && rval == error_mark_node)
1681 return rval;
1682 else
1683 return fn;
1684 }
1685
1686 /* Locate the dtor of TYPE. */
1687
1688 tree
1689 get_dtor (tree type, tsubst_flags_t complain)
1690 {
1691 tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
1692 LOOKUP_NORMAL, complain);
1693 if (fn == error_mark_node)
1694 return NULL_TREE;
1695 return fn;
1696 }
1697
1698 /* Locate the default ctor of TYPE. */
1699
1700 tree
1701 locate_ctor (tree type)
1702 {
1703 tree fn;
1704
1705 push_deferring_access_checks (dk_no_check);
1706 fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
1707 LOOKUP_SPECULATIVE, tf_none);
1708 pop_deferring_access_checks ();
1709 if (fn == error_mark_node)
1710 return NULL_TREE;
1711 return fn;
1712 }
1713
1714 /* Likewise, but give any appropriate errors. */
1715
1716 tree
1717 get_default_ctor (tree type)
1718 {
1719 tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
1720 LOOKUP_NORMAL, tf_warning_or_error);
1721 if (fn == error_mark_node)
1722 return NULL_TREE;
1723 return fn;
1724 }
1725
1726 /* Locate the copy ctor of TYPE. */
1727
1728 tree
1729 get_copy_ctor (tree type, tsubst_flags_t complain)
1730 {
1731 int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
1732 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
1733 tree argtype = build_stub_type (type, quals, false);
1734 tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
1735 LOOKUP_NORMAL, complain);
1736 if (fn == error_mark_node)
1737 return NULL_TREE;
1738 return fn;
1739 }
1740
1741 /* Locate the copy assignment operator of TYPE. */
1742
1743 tree
1744 get_copy_assign (tree type)
1745 {
1746 int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
1747 ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
1748 tree argtype = build_stub_type (type, quals, false);
1749 tree fn = locate_fn_flags (type, assign_op_identifier, argtype,
1750 LOOKUP_NORMAL, tf_warning_or_error);
1751 if (fn == error_mark_node)
1752 return NULL_TREE;
1753 return fn;
1754 }
1755
1756 /* walk_tree helper function for is_trivially_xible. If *TP is a call,
1757 return it if it calls something other than a trivial special member
1758 function. */
1759
1760 static tree
1761 check_nontriv (tree *tp, int *, void *)
1762 {
1763 tree fn = cp_get_callee (*tp);
1764 if (fn == NULL_TREE)
1765 return NULL_TREE;
1766
1767 if (TREE_CODE (fn) == ADDR_EXPR)
1768 fn = TREE_OPERAND (fn, 0);
1769
1770 if (TREE_CODE (fn) != FUNCTION_DECL
1771 || !trivial_fn_p (fn))
1772 return fn;
1773 return NULL_TREE;
1774 }
1775
1776 /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */
1777
1778 static tree
1779 assignable_expr (tree to, tree from)
1780 {
1781 cp_unevaluated cp_uneval_guard;
1782 to = build_stub_object (to);
1783 from = build_stub_object (from);
1784 tree r = cp_build_modify_expr (input_location, to, NOP_EXPR, from, tf_none);
1785 return r;
1786 }
1787
1788 /* The predicate condition for a template specialization
1789 is_constructible<T, Args...> shall be satisfied if and only if the
1790 following variable definition would be well-formed for some invented
1791 variable t: T t(create<Args>()...);
1792
1793 Return something equivalent in well-formedness and triviality. */
1794
1795 static tree
1796 constructible_expr (tree to, tree from)
1797 {
1798 tree expr;
1799 cp_unevaluated cp_uneval_guard;
1800 if (CLASS_TYPE_P (to))
1801 {
1802 tree ctype = to;
1803 vec<tree, va_gc> *args = NULL;
1804 if (!TYPE_REF_P (to))
1805 to = cp_build_reference_type (to, /*rval*/false);
1806 tree ob = build_stub_object (to);
1807 for (; from; from = TREE_CHAIN (from))
1808 vec_safe_push (args, build_stub_object (TREE_VALUE (from)));
1809 expr = build_special_member_call (ob, complete_ctor_identifier, &args,
1810 ctype, LOOKUP_NORMAL, tf_none);
1811 if (expr == error_mark_node)
1812 return error_mark_node;
1813 /* The current state of the standard vis-a-vis LWG 2116 is that
1814 is_*constructible involves destruction as well. */
1815 if (type_build_dtor_call (ctype))
1816 {
1817 tree dtor = build_special_member_call (ob, complete_dtor_identifier,
1818 NULL, ctype, LOOKUP_NORMAL,
1819 tf_none);
1820 if (dtor == error_mark_node)
1821 return error_mark_node;
1822 if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
1823 expr = build2 (COMPOUND_EXPR, void_type_node, expr, dtor);
1824 }
1825 }
1826 else
1827 {
1828 if (from == NULL_TREE)
1829 return build_value_init (strip_array_types (to), tf_none);
1830 const int len = list_length (from);
1831 if (len > 1)
1832 {
1833 if (cxx_dialect < cxx20)
1834 /* Too many initializers. */
1835 return error_mark_node;
1836
1837 /* In C++20 this is well-formed:
1838 using T = int[2];
1839 T t(1, 2);
1840 which means that std::is_constructible_v<int[2], int, int>
1841 should be true. */
1842 vec<constructor_elt, va_gc> *v;
1843 vec_alloc (v, len);
1844 for (tree t = from; t; t = TREE_CHAIN (t))
1845 {
1846 tree stub = build_stub_object (TREE_VALUE (t));
1847 constructor_elt elt = { NULL_TREE, stub };
1848 v->quick_push (elt);
1849 }
1850 from = build_constructor (init_list_type_node, v);
1851 CONSTRUCTOR_IS_DIRECT_INIT (from) = true;
1852 CONSTRUCTOR_IS_PAREN_INIT (from) = true;
1853 }
1854 else
1855 from = build_stub_object (TREE_VALUE (from));
1856 expr = perform_direct_initialization_if_possible (to, from,
1857 /*cast*/false,
1858 tf_none);
1859 /* If t(e) didn't work, maybe t{e} will. */
1860 if (expr == NULL_TREE
1861 && len == 1
1862 && cxx_dialect >= cxx20)
1863 {
1864 from = build_constructor_single (init_list_type_node, NULL_TREE,
1865 from);
1866 CONSTRUCTOR_IS_DIRECT_INIT (from) = true;
1867 CONSTRUCTOR_IS_PAREN_INIT (from) = true;
1868 expr = perform_direct_initialization_if_possible (to, from,
1869 /*cast*/false,
1870 tf_none);
1871 }
1872 }
1873 return expr;
1874 }
1875
1876 /* Returns a tree iff TO is assignable (if CODE is MODIFY_EXPR) or
1877 constructible (otherwise) from FROM, which is a single type for
1878 assignment or a list of types for construction. */
1879
1880 static tree
1881 is_xible_helper (enum tree_code code, tree to, tree from, bool trivial)
1882 {
1883 deferring_access_check_sentinel acs (dk_no_deferred);
1884 if (VOID_TYPE_P (to) || ABSTRACT_CLASS_TYPE_P (to)
1885 || (from && FUNC_OR_METHOD_TYPE_P (from)
1886 && (TYPE_READONLY (from) || FUNCTION_REF_QUALIFIED (from))))
1887 return error_mark_node;
1888 tree expr;
1889 if (code == MODIFY_EXPR)
1890 expr = assignable_expr (to, from);
1891 else if (trivial && from && TREE_CHAIN (from))
1892 return error_mark_node; // only 0- and 1-argument ctors can be trivial
1893 else if (TREE_CODE (to) == ARRAY_TYPE && !TYPE_DOMAIN (to))
1894 return error_mark_node; // can't construct an array of unknown bound
1895 else
1896 expr = constructible_expr (to, from);
1897 return expr;
1898 }
1899
1900 /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or
1901 constructible (otherwise) from FROM, which is a single type for
1902 assignment or a list of types for construction. */
1903
1904 bool
1905 is_trivially_xible (enum tree_code code, tree to, tree from)
1906 {
1907 tree expr;
1908 expr = is_xible_helper (code, to, from, /*trivial*/true);
1909
1910 if (expr == NULL_TREE || expr == error_mark_node)
1911 return false;
1912 tree nt = cp_walk_tree_without_duplicates (&expr, check_nontriv, NULL);
1913 return !nt;
1914 }
1915
1916 /* Returns true iff TO is assignable (if CODE is MODIFY_EXPR) or
1917 constructible (otherwise) from FROM, which is a single type for
1918 assignment or a list of types for construction. */
1919
1920 bool
1921 is_xible (enum tree_code code, tree to, tree from)
1922 {
1923 tree expr = is_xible_helper (code, to, from, /*trivial*/false);
1924 if (expr == error_mark_node)
1925 return false;
1926 return !!expr;
1927 }
1928
1929 /* Categorize various special_function_kinds. */
1930 #define SFK_CTOR_P(sfk) \
1931 ((sfk) >= sfk_constructor && (sfk) <= sfk_move_constructor)
1932 #define SFK_DTOR_P(sfk) \
1933 ((sfk) == sfk_destructor || (sfk) == sfk_virtual_destructor)
1934 #define SFK_ASSIGN_P(sfk) \
1935 ((sfk) == sfk_copy_assignment || (sfk) == sfk_move_assignment)
1936 #define SFK_COPY_P(sfk) \
1937 ((sfk) == sfk_copy_constructor || (sfk) == sfk_copy_assignment)
1938 #define SFK_MOVE_P(sfk) \
1939 ((sfk) == sfk_move_constructor || (sfk) == sfk_move_assignment)
1940
1941 /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
1942 DELETED_P or give an error message MSG with argument ARG. */
1943
1944 static void
1945 process_subob_fn (tree fn, special_function_kind sfk, tree *spec_p,
1946 bool *trivial_p, bool *deleted_p, bool *constexpr_p,
1947 bool diag, tree arg, bool dtor_from_ctor = false)
1948 {
1949 if (!fn || fn == error_mark_node)
1950 {
1951 if (deleted_p)
1952 *deleted_p = true;
1953 return;
1954 }
1955
1956 if (spec_p)
1957 {
1958 if (!maybe_instantiate_noexcept (fn))
1959 *spec_p = error_mark_node;
1960 else
1961 {
1962 tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
1963 *spec_p = merge_exception_specifiers (*spec_p, raises);
1964 }
1965 }
1966
1967 if (!trivial_fn_p (fn) && !dtor_from_ctor)
1968 {
1969 if (trivial_p)
1970 *trivial_p = false;
1971 if (TREE_CODE (arg) == FIELD_DECL
1972 && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
1973 {
1974 if (deleted_p)
1975 *deleted_p = true;
1976 if (diag)
1977 error ("union member %q+D with non-trivial %qD", arg, fn);
1978 }
1979 }
1980
1981 if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
1982 {
1983 *constexpr_p = false;
1984 if (diag)
1985 {
1986 inform (DECL_SOURCE_LOCATION (fn),
1987 SFK_DTOR_P (sfk)
1988 ? G_("defaulted destructor calls non-%<constexpr%> %qD")
1989 : G_("defaulted constructor calls non-%<constexpr%> %qD"),
1990 fn);
1991 explain_invalid_constexpr_fn (fn);
1992 }
1993 }
1994 }
1995
1996 /* Subroutine of synthesized_method_walk to allow recursion into anonymous
1997 aggregates. If DTOR_FROM_CTOR is true, we're walking subobject destructors
1998 called from a synthesized constructor, in which case we don't consider
1999 the triviality of the subobject destructor. */
2000
2001 static void
2002 walk_field_subobs (tree fields, special_function_kind sfk, tree fnname,
2003 int quals, tree *spec_p, bool *trivial_p,
2004 bool *deleted_p, bool *constexpr_p,
2005 bool diag, int flags, tsubst_flags_t complain,
2006 bool dtor_from_ctor)
2007 {
2008 tree field;
2009 for (field = fields; field; field = DECL_CHAIN (field))
2010 {
2011 tree mem_type, argtype, rval;
2012
2013 if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
2014 continue;
2015
2016 /* Variant members only affect deletedness. In particular, they don't
2017 affect the exception-specification of a user-provided destructor,
2018 which we're figuring out via get_defaulted_eh_spec. So if we aren't
2019 asking if this is deleted, don't even look up the function; we don't
2020 want an error about a deleted function we aren't actually calling. */
2021 if (sfk == sfk_destructor && deleted_p == NULL
2022 && TREE_CODE (DECL_CONTEXT (field)) == UNION_TYPE)
2023 break;
2024
2025 mem_type = strip_array_types (TREE_TYPE (field));
2026 if (SFK_ASSIGN_P (sfk))
2027 {
2028 bool bad = true;
2029 if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
2030 {
2031 if (diag)
2032 error ("non-static const member %q#D, cannot use default "
2033 "assignment operator", field);
2034 }
2035 else if (TYPE_REF_P (mem_type))
2036 {
2037 if (diag)
2038 error ("non-static reference member %q#D, cannot use "
2039 "default assignment operator", field);
2040 }
2041 else
2042 bad = false;
2043
2044 if (bad && deleted_p)
2045 *deleted_p = true;
2046 }
2047 else if (sfk == sfk_constructor || sfk == sfk_inheriting_constructor)
2048 {
2049 bool bad;
2050
2051 if (DECL_INITIAL (field))
2052 {
2053 if (diag && DECL_INITIAL (field) == error_mark_node)
2054 inform (DECL_SOURCE_LOCATION (field),
2055 "initializer for %q#D is invalid", field);
2056 if (trivial_p)
2057 *trivial_p = false;
2058 /* Core 1351: If the field has an NSDMI that could throw, the
2059 default constructor is noexcept(false). */
2060 if (spec_p)
2061 {
2062 tree nsdmi = get_nsdmi (field, /*ctor*/false, complain);
2063 if (nsdmi == error_mark_node)
2064 *spec_p = error_mark_node;
2065 else if (*spec_p != error_mark_node
2066 && !expr_noexcept_p (nsdmi, tf_none))
2067 *spec_p = noexcept_false_spec;
2068 }
2069 /* Don't do the normal processing. */
2070 continue;
2071 }
2072
2073 bad = false;
2074 if (CP_TYPE_CONST_P (mem_type)
2075 && default_init_uninitialized_part (mem_type))
2076 {
2077 if (diag)
2078 {
2079 error ("uninitialized const member in %q#T",
2080 current_class_type);
2081 inform (DECL_SOURCE_LOCATION (field),
2082 "%q#D should be initialized", field);
2083 }
2084 bad = true;
2085 }
2086 else if (TYPE_REF_P (mem_type))
2087 {
2088 if (diag)
2089 {
2090 error ("uninitialized reference member in %q#T",
2091 current_class_type);
2092 inform (DECL_SOURCE_LOCATION (field),
2093 "%q#D should be initialized", field);
2094 }
2095 bad = true;
2096 }
2097
2098 if (bad && deleted_p)
2099 *deleted_p = true;
2100
2101 /* Before C++20, for an implicitly-defined default constructor to
2102 be constexpr, every member must have a user-provided default
2103 constructor or an explicit initializer. */
2104 if (constexpr_p
2105 && cxx_dialect < cxx20
2106 && !CLASS_TYPE_P (mem_type)
2107 && TREE_CODE (DECL_CONTEXT (field)) != UNION_TYPE)
2108 {
2109 *constexpr_p = false;
2110 if (diag)
2111 inform (DECL_SOURCE_LOCATION (field),
2112 "defaulted default constructor does not "
2113 "initialize %q#D", field);
2114 }
2115 }
2116 else if (sfk == sfk_copy_constructor)
2117 {
2118 /* 12.8p11b5 */
2119 if (TYPE_REF_P (mem_type)
2120 && TYPE_REF_IS_RVALUE (mem_type))
2121 {
2122 if (diag)
2123 error ("copying non-static data member %q#D of rvalue "
2124 "reference type", field);
2125 if (deleted_p)
2126 *deleted_p = true;
2127 }
2128 }
2129
2130 if (!CLASS_TYPE_P (mem_type))
2131 continue;
2132
2133 if (ANON_AGGR_TYPE_P (mem_type))
2134 {
2135 walk_field_subobs (TYPE_FIELDS (mem_type), sfk, fnname, quals,
2136 spec_p, trivial_p, deleted_p, constexpr_p,
2137 diag, flags, complain, dtor_from_ctor);
2138 continue;
2139 }
2140
2141 if (SFK_COPY_P (sfk) || SFK_MOVE_P (sfk))
2142 {
2143 int mem_quals = cp_type_quals (mem_type) | quals;
2144 if (DECL_MUTABLE_P (field))
2145 mem_quals &= ~TYPE_QUAL_CONST;
2146 argtype = build_stub_type (mem_type, mem_quals, SFK_MOVE_P (sfk));
2147 }
2148 else
2149 argtype = NULL_TREE;
2150
2151 rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
2152
2153 process_subob_fn (rval, sfk, spec_p, trivial_p, deleted_p,
2154 constexpr_p, diag, field, dtor_from_ctor);
2155 }
2156 }
2157
2158 /* Base walker helper for synthesized_method_walk. Inspect a direct
2159 or virtual base. BINFO is the parent type's binfo. BASE_BINFO is
2160 the base binfo of interests. All other parms are as for
2161 synthesized_method_walk, or its local vars. */
2162
2163 static tree
2164 synthesized_method_base_walk (tree binfo, tree base_binfo,
2165 special_function_kind sfk, tree fnname, int quals,
2166 tree *inheriting_ctor, tree inherited_parms,
2167 int flags, bool diag,
2168 tree *spec_p, bool *trivial_p,
2169 bool *deleted_p, bool *constexpr_p)
2170 {
2171 bool inherited_binfo = false;
2172 tree argtype = NULL_TREE;
2173 deferring_kind defer = dk_no_deferred;
2174
2175 if (SFK_COPY_P (sfk) || SFK_MOVE_P (sfk))
2176 argtype = build_stub_type (BINFO_TYPE (base_binfo), quals, SFK_MOVE_P (sfk));
2177 else if (inheriting_ctor
2178 && (inherited_binfo
2179 = binfo_inherited_from (binfo, base_binfo, *inheriting_ctor)))
2180 {
2181 argtype = inherited_parms;
2182 /* Don't check access on the inherited constructor. */
2183 if (flag_new_inheriting_ctors)
2184 defer = dk_deferred;
2185 }
2186 else if (cxx_dialect >= cxx14 && sfk == sfk_virtual_destructor
2187 && BINFO_VIRTUAL_P (base_binfo)
2188 && ABSTRACT_CLASS_TYPE_P (BINFO_TYPE (binfo)))
2189 /* Don't check access when looking at vbases of abstract class's
2190 virtual destructor. */
2191 defer = dk_no_check;
2192
2193 if (defer != dk_no_deferred)
2194 push_deferring_access_checks (defer);
2195 tree rval = locate_fn_flags (base_binfo, fnname, argtype, flags,
2196 diag ? tf_warning_or_error : tf_none);
2197 if (defer != dk_no_deferred)
2198 pop_deferring_access_checks ();
2199
2200 /* Replace an inherited template with the appropriate specialization. */
2201 if (inherited_binfo && rval
2202 && DECL_P (*inheriting_ctor) && DECL_P (rval)
2203 && DECL_CONTEXT (*inheriting_ctor) == DECL_CONTEXT (rval))
2204 *inheriting_ctor = DECL_CLONED_FUNCTION (rval);
2205
2206 process_subob_fn (rval, sfk, spec_p, trivial_p, deleted_p,
2207 constexpr_p, diag, BINFO_TYPE (base_binfo));
2208 if (SFK_CTOR_P (sfk)
2209 && (!BINFO_VIRTUAL_P (base_binfo)
2210 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))))
2211 {
2212 /* In a constructor we also need to check the subobject
2213 destructors for cleanup of partially constructed objects. */
2214 tree dtor = locate_fn_flags (base_binfo, complete_dtor_identifier,
2215 NULL_TREE, flags,
2216 diag ? tf_warning_or_error : tf_none);
2217 /* Note that we don't pass down trivial_p; the subobject
2218 destructors don't affect triviality of the constructor. Nor
2219 do they affect constexpr-ness (a constant expression doesn't
2220 throw) or exception-specification (a throw from one of the
2221 dtors would be a double-fault). */
2222 process_subob_fn (dtor, sfk, NULL, NULL, deleted_p, NULL, false,
2223 BINFO_TYPE (base_binfo), /*dtor_from_ctor*/true);
2224 }
2225
2226 return rval;
2227 }
2228
2229 /* The caller wants to generate an implicit declaration of SFK for
2230 CTYPE which is const if relevant and CONST_P is set. If SPEC_P,
2231 TRIVIAL_P, DELETED_P or CONSTEXPR_P are non-null, set their
2232 referent appropriately. If DIAG is true, we're either being called
2233 from maybe_explain_implicit_delete to give errors, or if
2234 CONSTEXPR_P is non-null, from explain_invalid_constexpr_fn. */
2235
2236 static void
2237 synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
2238 tree *spec_p, bool *trivial_p, bool *deleted_p,
2239 bool *constexpr_p, bool diag,
2240 tree *inheriting_ctor, tree inherited_parms)
2241 {
2242 tree binfo, base_binfo;
2243 int i;
2244
2245 /* SFK must be exactly one category. */
2246 gcc_checking_assert (SFK_DTOR_P(sfk) + SFK_CTOR_P(sfk)
2247 + SFK_ASSIGN_P(sfk) == 1);
2248
2249 if (spec_p)
2250 *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec);
2251
2252 if (deleted_p)
2253 {
2254 /* "The closure type associated with a lambda-expression has a deleted
2255 default constructor and a deleted copy assignment operator."
2256 This is diagnosed in maybe_explain_implicit_delete.
2257 In C++20, only lambda-expressions with lambda-captures have those
2258 deleted. */
2259 if (LAMBDA_TYPE_P (ctype)
2260 && (sfk == sfk_constructor || sfk == sfk_copy_assignment)
2261 && (cxx_dialect < cxx20
2262 || LAMBDA_EXPR_CAPTURE_LIST (CLASSTYPE_LAMBDA_EXPR (ctype))
2263 || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE
2264 (CLASSTYPE_LAMBDA_EXPR (ctype)) != CPLD_NONE))
2265 {
2266 *deleted_p = true;
2267 return;
2268 }
2269
2270 *deleted_p = false;
2271 }
2272
2273 bool check_vdtor = false;
2274 tree fnname;
2275
2276 if (SFK_DTOR_P (sfk))
2277 {
2278 check_vdtor = true;
2279 /* The synthesized method will call base dtors, but check complete
2280 here to avoid having to deal with VTT. */
2281 fnname = complete_dtor_identifier;
2282 }
2283 else if (SFK_ASSIGN_P (sfk))
2284 fnname = assign_op_identifier;
2285 else
2286 fnname = complete_ctor_identifier;
2287
2288 gcc_assert ((sfk == sfk_inheriting_constructor)
2289 == (inheriting_ctor && *inheriting_ctor != NULL_TREE));
2290
2291 /* If that user-written default constructor would satisfy the
2292 requirements of a constexpr constructor (7.1.5), the
2293 implicitly-defined default constructor is constexpr.
2294
2295 The implicitly-defined copy/move assignment operator is constexpr if
2296 - X is a literal type, and
2297 - the assignment operator selected to copy/move each direct base class
2298 subobject is a constexpr function, and
2299 - for each non-static data member of X that is of class type (or array
2300 thereof), the assignment operator selected to copy/move that
2301 member is a constexpr function. */
2302 if (constexpr_p)
2303 *constexpr_p = (SFK_CTOR_P (sfk)
2304 || (SFK_ASSIGN_P (sfk) && cxx_dialect >= cxx14)
2305 || (SFK_DTOR_P (sfk) && cxx_dialect >= cxx20));
2306
2307 bool expected_trivial = type_has_trivial_fn (ctype, sfk);
2308 if (trivial_p)
2309 *trivial_p = expected_trivial;
2310
2311 /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
2312 class versions and other properties of the type. But a subobject
2313 class can be trivially copyable and yet have overload resolution
2314 choose a template constructor for initialization, depending on
2315 rvalueness and cv-quals. And furthermore, a member in a base might
2316 be trivial but deleted or otherwise not callable. So we can't exit
2317 early in C++0x. The same considerations apply in C++98/03, but
2318 there the definition of triviality does not consider overload
2319 resolution, so a constructor can be trivial even if it would otherwise
2320 call a non-trivial constructor. */
2321 if (expected_trivial
2322 && (!(SFK_COPY_P (sfk) || SFK_MOVE_P (sfk)) || cxx_dialect < cxx11))
2323 {
2324 if (constexpr_p && sfk == sfk_constructor)
2325 {
2326 bool cx = trivial_default_constructor_is_constexpr (ctype);
2327 *constexpr_p = cx;
2328 if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
2329 /* A trivial constructor doesn't have any NSDMI. */
2330 inform (input_location, "defaulted default constructor does "
2331 "not initialize any non-static data member");
2332 }
2333 if (!diag && cxx_dialect < cxx11)
2334 return;
2335 }
2336
2337 ++cp_unevaluated_operand;
2338 ++c_inhibit_evaluation_warnings;
2339 push_deferring_access_checks (dk_no_deferred);
2340
2341 tree scope = push_scope (ctype);
2342
2343 int flags = LOOKUP_NORMAL | LOOKUP_SPECULATIVE;
2344 if (sfk != sfk_inheriting_constructor)
2345 flags |= LOOKUP_DEFAULTED;
2346
2347 tsubst_flags_t complain = diag ? tf_warning_or_error : tf_none;
2348 if (diag && spec_p)
2349 /* We're in get_defaulted_eh_spec; we don't actually want any walking
2350 diagnostics, we just want complain set. */
2351 diag = false;
2352 int quals = const_p ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED;
2353
2354 for (binfo = TYPE_BINFO (ctype), i = 0;
2355 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
2356 {
2357 if (!SFK_ASSIGN_P (sfk) && BINFO_VIRTUAL_P (base_binfo))
2358 /* We'll handle virtual bases below. */
2359 continue;
2360
2361 tree fn = synthesized_method_base_walk (binfo, base_binfo,
2362 sfk, fnname, quals,
2363 inheriting_ctor, inherited_parms,
2364 flags, diag, spec_p, trivial_p,
2365 deleted_p, constexpr_p);
2366
2367 if (diag && SFK_ASSIGN_P (sfk) && SFK_MOVE_P (sfk)
2368 && BINFO_VIRTUAL_P (base_binfo)
2369 && fn && TREE_CODE (fn) == FUNCTION_DECL
2370 && move_fn_p (fn) && !trivial_fn_p (fn)
2371 && vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo)))
2372 warning (OPT_Wvirtual_move_assign,
2373 "defaulted move assignment for %qT calls a non-trivial "
2374 "move assignment operator for virtual base %qT",
2375 ctype, BINFO_TYPE (base_binfo));
2376
2377 if (check_vdtor && type_has_virtual_destructor (BINFO_TYPE (base_binfo)))
2378 {
2379 /* Unlike for base ctor/op=/dtor, for operator delete it's fine
2380 to have a null fn (no class-specific op delete). */
2381 fn = locate_fn_flags (ctype, ovl_op_identifier (false, DELETE_EXPR),
2382 ptr_type_node, flags, tf_none);
2383 if (fn && fn == error_mark_node)
2384 {
2385 if (complain & tf_error)
2386 locate_fn_flags (ctype, ovl_op_identifier (false, DELETE_EXPR),
2387 ptr_type_node, flags, complain);
2388 if (deleted_p)
2389 *deleted_p = true;
2390 }
2391 check_vdtor = false;
2392 }
2393 }
2394
2395 vec<tree, va_gc> *vbases = CLASSTYPE_VBASECLASSES (ctype);
2396 if (SFK_ASSIGN_P (sfk))
2397 /* Already examined vbases above. */;
2398 else if (vec_safe_is_empty (vbases))
2399 /* No virtual bases to worry about. */;
2400 else if (ABSTRACT_CLASS_TYPE_P (ctype) && cxx_dialect >= cxx14
2401 /* DR 1658 specifies that vbases of abstract classes are
2402 ignored for both ctors and dtors. Except DR 2336
2403 overrides that skipping when determing the eh-spec of a
2404 virtual destructor. */
2405 && sfk != sfk_virtual_destructor)
2406 /* Vbase cdtors are not relevant. */;
2407 else
2408 {
2409 if (constexpr_p)
2410 *constexpr_p = false;
2411
2412 FOR_EACH_VEC_ELT (*vbases, i, base_binfo)
2413 synthesized_method_base_walk (binfo, base_binfo, sfk, fnname, quals,
2414 inheriting_ctor, inherited_parms,
2415 flags, diag,
2416 spec_p, trivial_p, deleted_p, constexpr_p);
2417 }
2418
2419 /* Now handle the non-static data members. */
2420 walk_field_subobs (TYPE_FIELDS (ctype), sfk, fnname, quals,
2421 spec_p, trivial_p, deleted_p, constexpr_p,
2422 diag, flags, complain, /*dtor_from_ctor*/false);
2423 if (SFK_CTOR_P (sfk))
2424 walk_field_subobs (TYPE_FIELDS (ctype), sfk_destructor,
2425 complete_dtor_identifier, TYPE_UNQUALIFIED,
2426 NULL, NULL, deleted_p, NULL,
2427 false, flags, complain, /*dtor_from_ctor*/true);
2428
2429 pop_scope (scope);
2430
2431 pop_deferring_access_checks ();
2432 --cp_unevaluated_operand;
2433 --c_inhibit_evaluation_warnings;
2434 }
2435
2436 /* DECL is a defaulted function whose exception specification is now
2437 needed. Return what it should be. */
2438
2439 tree
2440 get_defaulted_eh_spec (tree decl, tsubst_flags_t complain)
2441 {
2442 /* For DECL_MAYBE_DELETED this should already have been handled by
2443 synthesize_method. */
2444 gcc_assert (!DECL_MAYBE_DELETED (decl));
2445
2446 if (DECL_CLONED_FUNCTION_P (decl))
2447 decl = DECL_CLONED_FUNCTION (decl);
2448 special_function_kind sfk = special_function_p (decl);
2449 tree ctype = DECL_CONTEXT (decl);
2450 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
2451 tree parm_type = TREE_VALUE (parms);
2452 bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
2453 tree spec = empty_except_spec;
2454 bool diag = !DECL_DELETED_FN (decl) && (complain & tf_error);
2455 tree inh = DECL_INHERITED_CTOR (decl);
2456 if (SFK_DTOR_P (sfk) && DECL_VIRTUAL_P (decl))
2457 /* We have to examine virtual bases even if abstract. */
2458 sfk = sfk_virtual_destructor;
2459 bool pushed = false;
2460 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2461 pushed = push_tinst_level (decl);
2462 synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL,
2463 NULL, diag, &inh, parms);
2464 if (pushed)
2465 pop_tinst_level ();
2466 return spec;
2467 }
2468
2469 /* DECL is a deleted function. If it's implicitly deleted, explain why and
2470 return true; else return false. */
2471
2472 bool
2473 maybe_explain_implicit_delete (tree decl)
2474 {
2475 /* If decl is a clone, get the primary variant. */
2476 decl = DECL_ORIGIN (decl);
2477 gcc_assert (DECL_DELETED_FN (decl));
2478 if (DECL_DEFAULTED_FN (decl))
2479 {
2480 /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
2481 static hash_set<tree> *explained;
2482
2483 special_function_kind sfk;
2484 location_t loc;
2485 bool informed;
2486 tree ctype;
2487
2488 if (!explained)
2489 explained = new hash_set<tree>;
2490 if (explained->add (decl))
2491 return true;
2492
2493 sfk = special_function_p (decl);
2494 ctype = DECL_CONTEXT (decl);
2495 loc = input_location;
2496 input_location = DECL_SOURCE_LOCATION (decl);
2497
2498 informed = false;
2499 if (LAMBDA_TYPE_P (ctype))
2500 {
2501 informed = true;
2502 if (sfk == sfk_constructor)
2503 inform (DECL_SOURCE_LOCATION (decl),
2504 "a lambda closure type has a deleted default constructor");
2505 else if (sfk == sfk_copy_assignment)
2506 inform (DECL_SOURCE_LOCATION (decl),
2507 "a lambda closure type has a deleted copy assignment operator");
2508 else
2509 informed = false;
2510 }
2511 else if (DECL_ARTIFICIAL (decl)
2512 && (sfk == sfk_copy_assignment || sfk == sfk_copy_constructor)
2513 && classtype_has_move_assign_or_move_ctor_p (ctype, true))
2514 {
2515 inform (DECL_SOURCE_LOCATION (decl),
2516 "%q#D is implicitly declared as deleted because %qT "
2517 "declares a move constructor or move assignment operator",
2518 decl, ctype);
2519 informed = true;
2520 }
2521 else if (sfk == sfk_inheriting_constructor)
2522 {
2523 tree binfo = inherited_ctor_binfo (decl);
2524 if (TREE_CODE (binfo) != TREE_BINFO)
2525 {
2526 inform (DECL_SOURCE_LOCATION (decl),
2527 "%q#D inherits from multiple base subobjects",
2528 decl);
2529 informed = true;
2530 }
2531 }
2532 if (!informed && sfk == sfk_comparison)
2533 {
2534 inform (DECL_SOURCE_LOCATION (decl),
2535 "%q#D is implicitly deleted because the default "
2536 "definition would be ill-formed:", decl);
2537 build_comparison_op (decl, tf_warning_or_error);
2538 }
2539 else if (!informed)
2540 {
2541 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
2542 bool const_p = false;
2543 if (parms)
2544 {
2545 tree parm_type = TREE_VALUE (parms);
2546 const_p = CP_TYPE_CONST_P (non_reference (parm_type));
2547 }
2548 tree raises = NULL_TREE;
2549 bool deleted_p = false;
2550 tree scope = push_scope (ctype);
2551 tree inh = DECL_INHERITED_CTOR (decl);
2552
2553 synthesized_method_walk (ctype, sfk, const_p,
2554 &raises, NULL, &deleted_p, NULL, false,
2555 &inh, parms);
2556 if (deleted_p)
2557 {
2558 inform (DECL_SOURCE_LOCATION (decl),
2559 "%q#D is implicitly deleted because the default "
2560 "definition would be ill-formed:", decl);
2561 synthesized_method_walk (ctype, sfk, const_p,
2562 NULL, NULL, &deleted_p, NULL, true,
2563 &inh, parms);
2564 }
2565 else if (!comp_except_specs
2566 (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
2567 raises, ce_normal))
2568 inform (DECL_SOURCE_LOCATION (decl), "%q#F is implicitly "
2569 "deleted because its exception-specification does not "
2570 "match the implicit exception-specification %qX",
2571 decl, raises);
2572 else if (flag_checking)
2573 gcc_unreachable ();
2574
2575 pop_scope (scope);
2576 }
2577
2578 input_location = loc;
2579 return true;
2580 }
2581 return false;
2582 }
2583
2584 /* DECL is a defaulted function which was declared constexpr. Explain why
2585 it can't be constexpr. */
2586
2587 void
2588 explain_implicit_non_constexpr (tree decl)
2589 {
2590 tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
2591 bool const_p = CP_TYPE_CONST_P (non_reference (TREE_VALUE (parms)));
2592 tree inh = DECL_INHERITED_CTOR (decl);
2593 bool dummy;
2594 special_function_kind sfk = special_function_p (decl);
2595 if (sfk == sfk_comparison)
2596 {
2597 DECL_DECLARED_CONSTEXPR_P (decl) = true;
2598 build_comparison_op (decl, tf_warning_or_error);
2599 DECL_DECLARED_CONSTEXPR_P (decl) = false;
2600 }
2601 else
2602 synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
2603 sfk, const_p,
2604 NULL, NULL, NULL, &dummy, true,
2605 &inh, parms);
2606 }
2607
2608 /* DECL is an instantiation of an inheriting constructor template. Deduce
2609 the correct exception-specification and deletedness for this particular
2610 specialization. */
2611
2612 void
2613 deduce_inheriting_ctor (tree decl)
2614 {
2615 decl = DECL_ORIGIN (decl);
2616 gcc_assert (DECL_INHERITED_CTOR (decl));
2617 tree spec;
2618 bool trivial, constexpr_, deleted;
2619 tree inh = DECL_INHERITED_CTOR (decl);
2620 synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor,
2621 false, &spec, &trivial, &deleted, &constexpr_,
2622 /*diag*/false,
2623 &inh,
2624 FUNCTION_FIRST_USER_PARMTYPE (decl));
2625 if (TREE_CODE (inherited_ctor_binfo (decl)) != TREE_BINFO)
2626 /* Inherited the same constructor from different base subobjects. */
2627 deleted = true;
2628 DECL_DELETED_FN (decl) = deleted;
2629 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
2630 SET_DECL_INHERITED_CTOR (decl, inh);
2631
2632 tree clone;
2633 FOR_EACH_CLONE (clone, decl)
2634 {
2635 DECL_DELETED_FN (clone) = deleted;
2636 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
2637 SET_DECL_INHERITED_CTOR (clone, inh);
2638 }
2639 }
2640
2641 /* Implicitly declare the special function indicated by KIND, as a
2642 member of TYPE. For copy constructors and assignment operators,
2643 CONST_P indicates whether these functions should take a const
2644 reference argument or a non-const reference.
2645 Returns the FUNCTION_DECL for the implicitly declared function. */
2646
2647 tree
2648 implicitly_declare_fn (special_function_kind kind, tree type,
2649 bool const_p, tree pattern_fn,
2650 tree inherited_parms)
2651 {
2652 tree fn;
2653 tree parameter_types = void_list_node;
2654 tree return_type;
2655 tree fn_type;
2656 tree raises = empty_except_spec;
2657 tree rhs_parm_type = NULL_TREE;
2658 tree this_parm;
2659 tree name;
2660 HOST_WIDE_INT saved_processing_template_decl;
2661 bool deleted_p = false;
2662 bool constexpr_p = false;
2663 tree inherited_ctor = (kind == sfk_inheriting_constructor
2664 ? pattern_fn : NULL_TREE);
2665
2666 /* Because we create declarations for implicitly declared functions
2667 lazily, we may be creating the declaration for a member of TYPE
2668 while in some completely different context. However, TYPE will
2669 never be a dependent class (because we never want to do lookups
2670 for implicitly defined functions in a dependent class). */
2671 gcc_assert (!dependent_type_p (type));
2672
2673 /* If the member-specification does not explicitly declare any member or
2674 friend named operator==, an == operator function is declared
2675 implicitly for each three-way comparison operator function defined as
2676 defaulted in the member-specification, with the same access and
2677 function-definition and in the same class scope as the respective
2678 three-way comparison operator function, except that the return type is
2679 replaced with bool and the declarator-id is replaced with
2680 operator==.
2681
2682 [Note: Such an implicitly-declared == operator for a class X is
2683 defined as defaulted in the definition of X and has the same
2684 parameter-declaration-clause and trailing requires-clause as the
2685 respective three-way comparison operator. It is declared with friend,
2686 virtual, constexpr, or consteval if the three-way comparison operator
2687 function is so declared. If the three-way comparison operator function
2688 has no noexcept-specifier, the implicitly-declared == operator
2689 function has an implicit exception specification (14.5) that may
2690 differ from the implicit exception specification of the three-way
2691 comparison operator function. --end note] */
2692 if (kind == sfk_comparison)
2693 {
2694 fn = copy_operator_fn (pattern_fn, EQ_EXPR);
2695 DECL_ARTIFICIAL (fn) = 1;
2696 TREE_TYPE (fn) = change_return_type (boolean_type_node, TREE_TYPE (fn));
2697 return fn;
2698 }
2699
2700 /* Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
2701 because we only create clones for constructors and destructors
2702 when not in a template. */
2703 saved_processing_template_decl = processing_template_decl;
2704 processing_template_decl = 0;
2705
2706 type = TYPE_MAIN_VARIANT (type);
2707
2708 if (targetm.cxx.cdtor_returns_this ())
2709 {
2710 if (kind == sfk_destructor)
2711 /* See comment in check_special_function_return_type. */
2712 return_type = build_pointer_type (void_type_node);
2713 else
2714 return_type = build_pointer_type (type);
2715 }
2716 else
2717 return_type = void_type_node;
2718
2719 int this_quals = TYPE_UNQUALIFIED;
2720 switch (kind)
2721 {
2722 case sfk_destructor:
2723 /* Destructor. */
2724 name = dtor_identifier;
2725 break;
2726
2727 case sfk_constructor:
2728 /* Default constructor. */
2729 name = ctor_identifier;
2730 break;
2731
2732 case sfk_copy_constructor:
2733 case sfk_copy_assignment:
2734 case sfk_move_constructor:
2735 case sfk_move_assignment:
2736 case sfk_inheriting_constructor:
2737 {
2738 if (kind == sfk_copy_assignment
2739 || kind == sfk_move_assignment)
2740 {
2741 return_type = build_reference_type (type);
2742 name = assign_op_identifier;
2743 }
2744 else
2745 name = ctor_identifier;
2746
2747 if (kind == sfk_inheriting_constructor)
2748 parameter_types = inherited_parms;
2749 else
2750 {
2751 if (const_p)
2752 rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
2753 else
2754 rhs_parm_type = type;
2755 bool move_p = (kind == sfk_move_assignment
2756 || kind == sfk_move_constructor);
2757 rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
2758
2759 parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
2760 }
2761 break;
2762 }
2763
2764 default:
2765 gcc_unreachable ();
2766 }
2767
2768 bool trivial_p = false;
2769
2770 if (inherited_ctor)
2771 {
2772 /* For an inheriting constructor, just copy these flags from the
2773 inherited constructor until deduce_inheriting_ctor. */
2774 raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor));
2775 deleted_p = DECL_DELETED_FN (inherited_ctor);
2776 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
2777 }
2778 else if (cxx_dialect >= cxx11)
2779 {
2780 raises = noexcept_deferred_spec;
2781 synthesized_method_walk (type, kind, const_p, NULL, &trivial_p,
2782 &deleted_p, &constexpr_p, false,
2783 &inherited_ctor, inherited_parms);
2784 }
2785 else
2786 synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
2787 &deleted_p, &constexpr_p, false,
2788 &inherited_ctor, inherited_parms);
2789 /* Don't bother marking a deleted constructor as constexpr. */
2790 if (deleted_p)
2791 constexpr_p = false;
2792 /* A trivial copy/move constructor is also a constexpr constructor,
2793 unless the class has virtual bases (7.1.5p4). */
2794 else if (trivial_p
2795 && cxx_dialect >= cxx11
2796 && (kind == sfk_copy_constructor
2797 || kind == sfk_move_constructor)
2798 && !CLASSTYPE_VBASECLASSES (type))
2799 gcc_assert (constexpr_p);
2800
2801 if (!trivial_p && type_has_trivial_fn (type, kind))
2802 type_set_nontrivial_flag (type, kind);
2803
2804 /* Create the function. */
2805 tree this_type = cp_build_qualified_type (type, this_quals);
2806 fn_type = build_method_type_directly (this_type, return_type,
2807 parameter_types);
2808
2809 if (raises)
2810 {
2811 if (raises != error_mark_node)
2812 fn_type = build_exception_variant (fn_type, raises);
2813 else
2814 /* Can happen, eg, in C++98 mode for an ill-formed non-static data
2815 member initializer (c++/89914). */
2816 gcc_assert (seen_error ());
2817 }
2818 fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
2819 if (kind != sfk_inheriting_constructor)
2820 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
2821
2822 if (IDENTIFIER_OVL_OP_P (name))
2823 {
2824 const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (name);
2825 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) = op->ovl_op_code;
2826 }
2827 else if (IDENTIFIER_CTOR_P (name))
2828 DECL_CXX_CONSTRUCTOR_P (fn) = true;
2829 else if (IDENTIFIER_DTOR_P (name))
2830 DECL_CXX_DESTRUCTOR_P (fn) = true;
2831 else
2832 gcc_unreachable ();
2833
2834 SET_DECL_ALIGN (fn, MINIMUM_METHOD_BOUNDARY);
2835
2836 /* Create the explicit arguments. */
2837 if (rhs_parm_type)
2838 {
2839 /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
2840 want its type to be included in the mangled function
2841 name. */
2842 tree decl = cp_build_parm_decl (fn, NULL_TREE, rhs_parm_type);
2843 TREE_READONLY (decl) = 1;
2844 retrofit_lang_decl (decl);
2845 DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
2846 DECL_ARGUMENTS (fn) = decl;
2847 }
2848 else if (kind == sfk_inheriting_constructor)
2849 {
2850 tree *p = &DECL_ARGUMENTS (fn);
2851 int index = 1;
2852 for (tree parm = inherited_parms; parm && parm != void_list_node;
2853 parm = TREE_CHAIN (parm))
2854 {
2855 *p = cp_build_parm_decl (fn, NULL_TREE, TREE_VALUE (parm));
2856 retrofit_lang_decl (*p);
2857 DECL_PARM_LEVEL (*p) = 1;
2858 DECL_PARM_INDEX (*p) = index++;
2859 p = &DECL_CHAIN (*p);
2860 }
2861 SET_DECL_INHERITED_CTOR (fn, inherited_ctor);
2862 DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor);
2863 /* A constructor so declared has the same access as the corresponding
2864 constructor in X. */
2865 TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor);
2866 TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor);
2867 /* Copy constexpr from the inherited constructor even if the
2868 inheriting constructor doesn't satisfy the requirements. */
2869 constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
2870 }
2871
2872 /* Add the "this" parameter. */
2873 this_parm = build_this_parm (fn, fn_type, this_quals);
2874 DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
2875 DECL_ARGUMENTS (fn) = this_parm;
2876
2877 grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
2878
2879 DECL_IN_AGGR_P (fn) = 1;
2880 DECL_ARTIFICIAL (fn) = 1;
2881 DECL_DEFAULTED_FN (fn) = 1;
2882 if (cxx_dialect >= cxx11)
2883 {
2884 DECL_DELETED_FN (fn) = deleted_p;
2885 DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
2886 }
2887 DECL_EXTERNAL (fn) = true;
2888 DECL_NOT_REALLY_EXTERN (fn) = 1;
2889 DECL_DECLARED_INLINE_P (fn) = 1;
2890 set_linkage_according_to_type (type, fn);
2891 if (TREE_PUBLIC (fn))
2892 DECL_COMDAT (fn) = 1;
2893 rest_of_decl_compilation (fn, namespace_bindings_p (), at_eof);
2894 gcc_assert (!TREE_USED (fn));
2895
2896 /* Propagate constraints from the inherited constructor. */
2897 if (flag_concepts && inherited_ctor)
2898 if (tree orig_ci = get_constraints (inherited_ctor))
2899 {
2900 tree new_ci = copy_node (orig_ci);
2901 set_constraints (fn, new_ci);
2902 }
2903
2904 /* Restore PROCESSING_TEMPLATE_DECL. */
2905 processing_template_decl = saved_processing_template_decl;
2906
2907 if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
2908 fn = add_inherited_template_parms (fn, inherited_ctor);
2909
2910 /* Warn about calling a non-trivial move assignment in a virtual base. */
2911 if (kind == sfk_move_assignment && !deleted_p && !trivial_p
2912 && CLASSTYPE_VBASECLASSES (type))
2913 {
2914 location_t loc = input_location;
2915 input_location = DECL_SOURCE_LOCATION (fn);
2916 synthesized_method_walk (type, kind, const_p,
2917 NULL, NULL, NULL, NULL, true,
2918 NULL, NULL_TREE);
2919 input_location = loc;
2920 }
2921
2922 return fn;
2923 }
2924
2925 /* Gives any errors about defaulted functions which need to be deferred
2926 until the containing class is complete. */
2927
2928 void
2929 defaulted_late_check (tree fn)
2930 {
2931 /* Complain about invalid signature for defaulted fn. */
2932 tree ctx = DECL_CONTEXT (fn);
2933 special_function_kind kind = special_function_p (fn);
2934
2935 if (kind == sfk_comparison)
2936 {
2937 /* If the function was declared constexpr, check that the definition
2938 qualifies. Otherwise we can define the function lazily. */
2939 if (DECL_DECLARED_CONSTEXPR_P (fn) && !DECL_INITIAL (fn))
2940 synthesize_method (fn);
2941 return;
2942 }
2943
2944 bool fn_const_p = (copy_fn_p (fn) == 2);
2945 tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
2946 NULL, NULL);
2947 tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
2948
2949 if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
2950 TREE_TYPE (TREE_TYPE (implicit_fn)))
2951 || !compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2952 TYPE_ARG_TYPES (TREE_TYPE (implicit_fn))))
2953 {
2954 error ("defaulted declaration %q+D does not match the "
2955 "expected signature", fn);
2956 inform (DECL_SOURCE_LOCATION (fn),
2957 "expected signature: %qD", implicit_fn);
2958 }
2959
2960 if (DECL_DELETED_FN (implicit_fn))
2961 {
2962 DECL_DELETED_FN (fn) = 1;
2963 return;
2964 }
2965
2966 /* If a function is explicitly defaulted on its first declaration without an
2967 exception-specification, it is implicitly considered to have the same
2968 exception-specification as if it had been implicitly declared. */
2969 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn))
2970 && DECL_DEFAULTED_IN_CLASS_P (fn))
2971 TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
2972
2973 if (DECL_DEFAULTED_IN_CLASS_P (fn)
2974 && DECL_DECLARED_CONSTEXPR_P (implicit_fn))
2975 {
2976 /* Hmm...should we do this for out-of-class too? Should it be OK to
2977 add constexpr later like inline, rather than requiring
2978 declarations to match? */
2979 DECL_DECLARED_CONSTEXPR_P (fn) = true;
2980 if (kind == sfk_constructor)
2981 TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
2982 }
2983
2984 if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
2985 && DECL_DECLARED_CONSTEXPR_P (fn))
2986 {
2987 if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
2988 {
2989 error ("explicitly defaulted function %q+D cannot be declared "
2990 "%qs because the implicit declaration is not %qs:", fn,
2991 DECL_IMMEDIATE_FUNCTION_P (fn) ? "consteval" : "constexpr",
2992 "constexpr");
2993 explain_implicit_non_constexpr (fn);
2994 }
2995 DECL_DECLARED_CONSTEXPR_P (fn) = false;
2996 }
2997 }
2998
2999 /* Returns true iff FN can be explicitly defaulted, and gives any
3000 errors if defaulting FN is ill-formed. */
3001
3002 bool
3003 defaultable_fn_check (tree fn)
3004 {
3005 special_function_kind kind = sfk_none;
3006
3007 if (template_parm_scope_p ())
3008 {
3009 error ("a template cannot be defaulted");
3010 return false;
3011 }
3012
3013 if (DECL_CONSTRUCTOR_P (fn))
3014 {
3015 if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
3016 kind = sfk_constructor;
3017 else if (copy_fn_p (fn) > 0
3018 && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
3019 == void_list_node))
3020 kind = sfk_copy_constructor;
3021 else if (move_fn_p (fn))
3022 kind = sfk_move_constructor;
3023 }
3024 else if (DECL_DESTRUCTOR_P (fn))
3025 kind = sfk_destructor;
3026 else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
3027 && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR))
3028 {
3029 if (copy_fn_p (fn))
3030 kind = sfk_copy_assignment;
3031 else if (move_fn_p (fn))
3032 kind = sfk_move_assignment;
3033 }
3034 else if (DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) >= OVL_OP_EQ_EXPR
3035 && DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) <= OVL_OP_SPACESHIP_EXPR)
3036 {
3037 kind = sfk_comparison;
3038 if (!early_check_defaulted_comparison (fn))
3039 return false;
3040 }
3041
3042 if (kind == sfk_none)
3043 {
3044 error ("%qD cannot be defaulted", fn);
3045 return false;
3046 }
3047 else
3048 {
3049 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
3050 t && t != void_list_node; t = TREE_CHAIN (t))
3051 if (TREE_PURPOSE (t))
3052 {
3053 error ("defaulted function %q+D with default argument", fn);
3054 break;
3055 }
3056
3057 /* Avoid do_warn_unused_parameter warnings. */
3058 for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p))
3059 if (DECL_NAME (p))
3060 TREE_NO_WARNING (p) = 1;
3061
3062 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
3063 /* Defer checking. */;
3064 else if (!processing_template_decl)
3065 defaulted_late_check (fn);
3066
3067 return true;
3068 }
3069 }
3070
3071 /* Add an implicit declaration to TYPE for the kind of function
3072 indicated by SFK. Return the FUNCTION_DECL for the new implicit
3073 declaration. */
3074
3075 tree
3076 lazily_declare_fn (special_function_kind sfk, tree type)
3077 {
3078 tree fn;
3079 /* Whether or not the argument has a const reference type. */
3080 bool const_p = false;
3081
3082 type = TYPE_MAIN_VARIANT (type);
3083
3084 switch (sfk)
3085 {
3086 case sfk_constructor:
3087 CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
3088 break;
3089 case sfk_copy_constructor:
3090 const_p = TYPE_HAS_CONST_COPY_CTOR (type);
3091 CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
3092 break;
3093 case sfk_move_constructor:
3094 CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
3095 break;
3096 case sfk_copy_assignment:
3097 const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
3098 CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
3099 break;
3100 case sfk_move_assignment:
3101 CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
3102 break;
3103 case sfk_destructor:
3104 CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
3105 break;
3106 default:
3107 gcc_unreachable ();
3108 }
3109
3110 /* Declare the function. */
3111 fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL);
3112
3113 /* [class.copy]/8 If the class definition declares a move constructor or
3114 move assignment operator, the implicitly declared copy constructor is
3115 defined as deleted.... */
3116 if ((sfk == sfk_copy_assignment || sfk == sfk_copy_constructor)
3117 && cxx_dialect >= cxx11)
3118 {
3119 if (classtype_has_move_assign_or_move_ctor_p (type, true))
3120 DECL_DELETED_FN (fn) = true;
3121 else if (classtype_has_depr_implicit_copy (type))
3122 /* The implicit definition of a copy constructor as defaulted is
3123 deprecated if the class has a user-declared copy assignment operator
3124 or a user-declared destructor. The implicit definition of a copy
3125 assignment operator as defaulted is deprecated if the class has a
3126 user-declared copy constructor or a user-declared destructor (15.4,
3127 15.8). */
3128 TREE_DEPRECATED (fn) = true;
3129 }
3130
3131 /* Destructors and assignment operators may be virtual. */
3132 if (sfk == sfk_destructor
3133 || sfk == sfk_move_assignment
3134 || sfk == sfk_copy_assignment)
3135 check_for_override (fn, type);
3136
3137 /* Add it to the class */
3138 bool added = add_method (type, fn, false);
3139 gcc_assert (added || errorcount);
3140
3141 /* Add it to TYPE_FIELDS. */
3142 if (sfk == sfk_destructor
3143 && DECL_VIRTUAL_P (fn))
3144 /* The ABI requires that a virtual destructor go at the end of the
3145 vtable. */
3146 TYPE_FIELDS (type) = chainon (TYPE_FIELDS (type), fn);
3147 else
3148 {
3149 DECL_CHAIN (fn) = TYPE_FIELDS (type);
3150 TYPE_FIELDS (type) = fn;
3151 }
3152 /* Propagate TYPE_FIELDS. */
3153 fixup_type_variants (type);
3154
3155 maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
3156 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (fn))
3157 /* Create appropriate clones. */
3158 clone_cdtor (fn, /*update_methods=*/true);
3159
3160 return fn;
3161 }
3162
3163 /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
3164 as there are artificial parms in FN. */
3165
3166 tree
3167 skip_artificial_parms_for (const_tree fn, tree list)
3168 {
3169 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
3170 list = TREE_CHAIN (list);
3171 else
3172 return list;
3173
3174 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
3175 list = TREE_CHAIN (list);
3176 if (DECL_HAS_VTT_PARM_P (fn))
3177 list = TREE_CHAIN (list);
3178 return list;
3179 }
3180
3181 /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
3182 artificial parms in FN. */
3183
3184 int
3185 num_artificial_parms_for (const_tree fn)
3186 {
3187 int count = 0;
3188
3189 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
3190 count++;
3191 else
3192 return 0;
3193
3194 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
3195 count++;
3196 if (DECL_HAS_VTT_PARM_P (fn))
3197 count++;
3198 return count;
3199 }
3200
3201
3202 #include "gt-cp-method.h"