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