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 102142 : init_method (void)
46 : {
47 102142 : init_mangle ();
48 102142 : }
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 1034342 : make_thunk (tree function, bool this_adjusting,
61 : tree fixed_offset, tree virtual_offset)
62 : {
63 1034342 : HOST_WIDE_INT d;
64 1034342 : tree thunk;
65 :
66 1034342 : gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
67 : /* We can have this thunks to covariant thunks, but not vice versa. */
68 1034342 : gcc_assert (!DECL_THIS_THUNK_P (function));
69 1034342 : gcc_assert (!DECL_RESULT_THUNK_P (function) || this_adjusting);
70 :
71 : /* Scale the VIRTUAL_OFFSET to be in terms of bytes. */
72 1034342 : if (this_adjusting && virtual_offset)
73 835616 : virtual_offset
74 835616 : = size_binop (MULT_EXPR,
75 : virtual_offset,
76 : convert (ssizetype,
77 : TYPE_SIZE_UNIT (vtable_entry_type)));
78 :
79 1034342 : 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 2266458 : for (thunk = DECL_THUNKS (function); thunk; thunk = DECL_CHAIN (thunk))
85 750221 : if (DECL_THIS_THUNK_P (thunk) == this_adjusting
86 750218 : && THUNK_FIXED_OFFSET (thunk) == d
87 552907 : && !virtual_offset == !THUNK_VIRTUAL_OFFSET (thunk)
88 1303128 : && (!virtual_offset
89 475193 : || (this_adjusting
90 475193 : ? tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk),
91 : virtual_offset)
92 184 : : THUNK_VIRTUAL_OFFSET (thunk) == virtual_offset)))
93 552447 : 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 481895 : 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 481895 : gcc_assert (TYPE_SIZE (DECL_CONTEXT (function))
102 : && TYPE_BEING_DEFINED (DECL_CONTEXT (function)));
103 :
104 481895 : thunk = build_decl (DECL_SOURCE_LOCATION (function),
105 481895 : FUNCTION_DECL, NULL_TREE, TREE_TYPE (function));
106 481895 : DECL_LANG_SPECIFIC (thunk) = DECL_LANG_SPECIFIC (function);
107 481895 : cxx_dup_lang_specific_decl (thunk);
108 481895 : DECL_VIRTUAL_P (thunk) = true;
109 481895 : SET_DECL_THUNKS (thunk, NULL_TREE);
110 :
111 481895 : DECL_CONTEXT (thunk) = DECL_CONTEXT (function);
112 481895 : TREE_READONLY (thunk) = TREE_READONLY (function);
113 481895 : TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (function);
114 481895 : TREE_PUBLIC (thunk) = TREE_PUBLIC (function);
115 481895 : SET_DECL_THUNK_P (thunk, this_adjusting);
116 481895 : THUNK_TARGET (thunk) = function;
117 481895 : THUNK_FIXED_OFFSET (thunk) = d;
118 481895 : THUNK_VIRTUAL_OFFSET (thunk) = virtual_offset;
119 481895 : THUNK_ALIAS (thunk) = NULL_TREE;
120 :
121 481895 : DECL_INTERFACE_KNOWN (thunk) = 1;
122 481895 : DECL_NOT_REALLY_EXTERN (thunk) = 1;
123 481895 : DECL_COMDAT (thunk) = DECL_COMDAT (function);
124 481895 : 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 481895 : DECL_CXX_DESTRUCTOR_P (thunk) = 0;
128 481895 : DECL_CXX_CONSTRUCTOR_P (thunk) = 0;
129 481895 : DECL_EXTERNAL (thunk) = 1;
130 481895 : DECL_ARTIFICIAL (thunk) = 1;
131 : /* The THUNK is not a pending inline, even if the FUNCTION is. */
132 481895 : DECL_PENDING_INLINE_P (thunk) = 0;
133 481895 : DECL_DECLARED_INLINE_P (thunk) = 0;
134 : /* Nor is it a template instantiation. */
135 481895 : DECL_USE_TEMPLATE (thunk) = 0;
136 481895 : DECL_TEMPLATE_INFO (thunk) = NULL;
137 :
138 : /* Add it to the list of thunks associated with FUNCTION. */
139 481895 : DECL_CHAIN (thunk) = DECL_THUNKS (function);
140 481895 : SET_DECL_THUNKS (function, thunk);
141 :
142 481895 : return thunk;
143 : }
144 :
145 : /* Finish THUNK, a thunk decl. */
146 :
147 : void
148 481895 : finish_thunk (tree thunk)
149 : {
150 481895 : tree function, name;
151 481895 : tree fixed_offset = ssize_int (THUNK_FIXED_OFFSET (thunk));
152 481895 : tree virtual_offset = THUNK_VIRTUAL_OFFSET (thunk);
153 :
154 481895 : gcc_assert (!DECL_NAME (thunk) && DECL_THUNK_P (thunk));
155 481895 : if (virtual_offset && DECL_RESULT_THUNK_P (thunk))
156 142 : virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
157 481895 : function = THUNK_TARGET (thunk);
158 482109 : 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 481895 : 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 481895 : DECL_NAME (thunk) = name;
182 481895 : SET_DECL_ASSEMBLER_NAME (thunk, name);
183 481895 : }
184 :
185 : static GTY (()) int thunk_labelno;
186 :
187 : /* Create a static alias to target. */
188 :
189 : tree
190 40999 : make_alias_for (tree target, tree newid)
191 : {
192 40999 : tree alias = build_decl (DECL_SOURCE_LOCATION (target),
193 40999 : TREE_CODE (target), newid, TREE_TYPE (target));
194 40999 : DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (target);
195 40999 : cxx_dup_lang_specific_decl (alias);
196 40999 : DECL_CONTEXT (alias) = DECL_CONTEXT (target);
197 40999 : TREE_READONLY (alias) = TREE_READONLY (target);
198 40999 : TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (target);
199 40999 : TREE_PUBLIC (alias) = 0;
200 40999 : DECL_INTERFACE_KNOWN (alias) = 1;
201 40999 : if (DECL_LANG_SPECIFIC (alias))
202 : {
203 40999 : DECL_NOT_REALLY_EXTERN (alias) = 1;
204 40999 : DECL_USE_TEMPLATE (alias) = 0;
205 40999 : DECL_TEMPLATE_INFO (alias) = NULL;
206 : }
207 40999 : DECL_EXTERNAL (alias) = 0;
208 40999 : DECL_ARTIFICIAL (alias) = 1;
209 40999 : DECL_TEMPLATE_INSTANTIATED (alias) = 0;
210 40999 : if (TREE_CODE (alias) == FUNCTION_DECL)
211 : {
212 40925 : DECL_SAVED_AUTO_RETURN_TYPE (alias) = NULL;
213 40925 : DECL_CXX_DESTRUCTOR_P (alias) = 0;
214 40925 : DECL_CXX_CONSTRUCTOR_P (alias) = 0;
215 40925 : DECL_PENDING_INLINE_P (alias) = 0;
216 40925 : DECL_DECLARED_INLINE_P (alias) = 0;
217 40925 : DECL_INITIAL (alias) = error_mark_node;
218 40925 : DECL_ARGUMENTS (alias) = copy_list (DECL_ARGUMENTS (target));
219 : }
220 : else
221 74 : TREE_STATIC (alias) = 1;
222 40999 : TREE_ADDRESSABLE (alias) = 1;
223 40999 : TREE_USED (alias) = 1;
224 40999 : SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
225 40999 : return alias;
226 : }
227 :
228 : static tree
229 4382 : make_alias_for_thunk (tree function)
230 : {
231 4382 : tree alias;
232 4382 : char buf[256];
233 :
234 4382 : targetm.asm_out.generate_internal_label (buf, "LTHUNK", thunk_labelno);
235 4382 : thunk_labelno++;
236 :
237 4382 : alias = make_alias_for (function, get_identifier (buf));
238 :
239 4382 : if (!flag_syntax_only)
240 : {
241 4382 : struct cgraph_node *funcn, *aliasn;
242 4382 : funcn = cgraph_node::get (function);
243 4382 : gcc_checking_assert (funcn);
244 4382 : aliasn = cgraph_node::create_same_body_alias (alias, function);
245 4382 : DECL_ASSEMBLER_NAME (function);
246 4382 : gcc_assert (aliasn != NULL);
247 : }
248 :
249 4382 : 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 8458 : use_thunk (tree thunk_fndecl, bool emit_p)
258 : {
259 8458 : tree a, t, function, alias;
260 8458 : tree virtual_offset;
261 8458 : HOST_WIDE_INT fixed_offset, virtual_value;
262 8458 : bool this_adjusting = DECL_THIS_THUNK_P (thunk_fndecl);
263 8458 : struct cgraph_node *funcn, *thunk_node;
264 :
265 : /* We should have called finish_thunk to give it a name. */
266 8458 : gcc_assert (DECL_NAME (thunk_fndecl));
267 :
268 : /* We should never be using an alias, always refer to the
269 : aliased thunk. */
270 8458 : gcc_assert (!THUNK_ALIAS (thunk_fndecl));
271 :
272 8458 : if (TREE_ASM_WRITTEN (thunk_fndecl))
273 : return;
274 :
275 4487 : function = THUNK_TARGET (thunk_fndecl);
276 4487 : 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 4487 : 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 4487 : TREE_ADDRESSABLE (thunk_fndecl) = 1;
287 :
288 : /* Don't diagnose deprecated or unavailable functions just because they
289 : have thunks emitted for them. */
290 4487 : auto du = make_temp_override (deprecated_state,
291 4487 : UNAVAILABLE_DEPRECATED_SUPPRESS);
292 :
293 : /* Figure out what function is being thunked to. It's referenced in
294 : this translation unit. */
295 4487 : TREE_ADDRESSABLE (function) = 1;
296 4487 : mark_used (function);
297 4487 : if (!emit_p)
298 : return;
299 :
300 4382 : if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function))
301 4382 : alias = make_alias_for_thunk (function);
302 : else
303 : alias = function;
304 :
305 4382 : fixed_offset = THUNK_FIXED_OFFSET (thunk_fndecl);
306 4382 : virtual_offset = THUNK_VIRTUAL_OFFSET (thunk_fndecl);
307 :
308 4382 : if (virtual_offset)
309 : {
310 2851 : if (!this_adjusting)
311 112 : virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
312 2851 : virtual_value = tree_to_shwi (virtual_offset);
313 2851 : 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 4382 : mark_used (thunk_fndecl);
320 : /* This thunk is actually defined. */
321 4382 : DECL_EXTERNAL (thunk_fndecl) = 0;
322 : /* The linkage of the function may have changed. FIXME in linkage
323 : rewrite. */
324 4382 : gcc_assert (DECL_INTERFACE_KNOWN (function));
325 4382 : TREE_PUBLIC (thunk_fndecl) = TREE_PUBLIC (function);
326 4382 : DECL_VISIBILITY (thunk_fndecl) = DECL_VISIBILITY (function);
327 8764 : DECL_VISIBILITY_SPECIFIED (thunk_fndecl)
328 4382 : = DECL_VISIBILITY_SPECIFIED (function);
329 4382 : DECL_COMDAT (thunk_fndecl) = DECL_COMDAT (function);
330 4382 : DECL_WEAK (thunk_fndecl) = DECL_WEAK (function);
331 :
332 4382 : if (flag_syntax_only)
333 : {
334 0 : TREE_ASM_WRITTEN (thunk_fndecl) = 1;
335 0 : return;
336 : }
337 :
338 4382 : push_to_top_level ();
339 :
340 4382 : if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function)
341 4382 : && targetm_common.have_named_sections)
342 : {
343 4382 : tree fn = function;
344 4382 : struct symtab_node *symbol;
345 :
346 4382 : if ((symbol = symtab_node::get (function))
347 4382 : && symbol->alias)
348 : {
349 256 : if (symbol->analyzed)
350 65 : fn = symtab_node::get (function)->ultimate_alias_target ()->decl;
351 : else
352 191 : fn = symtab_node::get (function)->alias_target;
353 : }
354 4382 : resolve_unique_section (fn, 0, flag_function_sections);
355 :
356 4382 : if (DECL_SECTION_NAME (fn) != NULL && DECL_ONE_ONLY (fn))
357 : {
358 3656 : resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
359 :
360 : /* Output the thunk into the same section as function. */
361 3656 : set_decl_section_name (thunk_fndecl, fn);
362 7312 : symtab_node::get (thunk_fndecl)->implicit_section
363 3656 : = symtab_node::get (fn)->implicit_section;
364 : }
365 : }
366 :
367 : /* Set up cloned argument trees for the thunk. */
368 4382 : t = NULL_TREE;
369 9372 : for (a = DECL_ARGUMENTS (function); a; a = DECL_CHAIN (a))
370 : {
371 4990 : tree x = copy_node (a);
372 4990 : DECL_CHAIN (x) = t;
373 4990 : DECL_CONTEXT (x) = thunk_fndecl;
374 4990 : SET_DECL_RTL (x, NULL);
375 4990 : DECL_HAS_VALUE_EXPR_P (x) = 0;
376 4990 : TREE_ADDRESSABLE (x) = 0;
377 4990 : t = x;
378 : }
379 4382 : a = nreverse (t);
380 4382 : DECL_ARGUMENTS (thunk_fndecl) = a;
381 4382 : TREE_ASM_WRITTEN (thunk_fndecl) = 1;
382 4382 : funcn = cgraph_node::get (function);
383 4382 : gcc_checking_assert (funcn);
384 4382 : thunk_node = funcn->create_thunk (thunk_fndecl, function,
385 : this_adjusting, fixed_offset, virtual_value,
386 : 0, virtual_offset, alias);
387 4382 : if (DECL_ONE_ONLY (function))
388 3656 : thunk_node->add_to_same_comdat_group (funcn);
389 :
390 4382 : pop_from_top_level ();
391 4487 : }
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 80425734 : type_has_trivial_fn (tree ctype, special_function_kind sfk)
399 : {
400 80425734 : switch (sfk)
401 : {
402 11295458 : case sfk_constructor:
403 11295458 : return !TYPE_HAS_COMPLEX_DFLT (ctype);
404 15928722 : case sfk_copy_constructor:
405 15928722 : return !TYPE_HAS_COMPLEX_COPY_CTOR (ctype);
406 12005028 : case sfk_move_constructor:
407 12005028 : return !TYPE_HAS_COMPLEX_MOVE_CTOR (ctype);
408 6752454 : case sfk_copy_assignment:
409 6752454 : return !TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype);
410 5430881 : case sfk_move_assignment:
411 5430881 : return !TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype);
412 28066130 : case sfk_destructor:
413 28066130 : case sfk_virtual_destructor:
414 28066130 : 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 269811 : type_set_nontrivial_flag (tree ctype, special_function_kind sfk)
428 : {
429 269811 : switch (sfk)
430 : {
431 163934 : case sfk_constructor:
432 163934 : TYPE_HAS_COMPLEX_DFLT (ctype) = true;
433 163934 : return;
434 3 : case sfk_copy_constructor:
435 3 : TYPE_HAS_COMPLEX_COPY_CTOR (ctype) = true;
436 3 : return;
437 105357 : case sfk_move_constructor:
438 105357 : TYPE_HAS_COMPLEX_MOVE_CTOR (ctype) = true;
439 105357 : return;
440 161 : case sfk_copy_assignment:
441 161 : TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype) = true;
442 161 : return;
443 302 : case sfk_move_assignment:
444 302 : TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype) = true;
445 302 : return;
446 54 : case sfk_destructor:
447 54 : TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true;
448 54 : 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 382754274 : trivial_fn_p (tree fn)
459 : {
460 382754274 : if (TREE_CODE (fn) == TEMPLATE_DECL)
461 : return false;
462 382754274 : if (!DECL_DEFAULTED_FN (fn))
463 : return false;
464 :
465 : /* If fn is a clone, get the primary variant. */
466 38806399 : if (tree prim = DECL_CLONED_FUNCTION (fn))
467 30242685 : fn = prim;
468 38806399 : 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 122573 : forward_parm (tree parm)
476 : {
477 122573 : tree exp = convert_from_reference (parm);
478 122573 : tree type = TREE_TYPE (parm);
479 122573 : if (DECL_PACK_P (parm))
480 666 : type = PACK_EXPANSION_PATTERN (type);
481 122573 : if (!TYPE_REF_P (type))
482 107167 : type = cp_build_reference_type (type, /*rval=*/true);
483 122573 : warning_sentinel w (warn_useless_cast);
484 122573 : exp = build_static_cast (input_location, type, exp,
485 : tf_warning_or_error);
486 122573 : if (DECL_PACK_P (parm))
487 666 : exp = make_pack_expansion (exp);
488 122573 : return exp;
489 122573 : }
490 :
491 : /* Strip all inheriting constructors, if any, to return the original
492 : constructor from a (possibly indirect) base class. */
493 :
494 : tree
495 1566964708 : strip_inheriting_ctors (tree dfn)
496 : {
497 1566964708 : if (!flag_new_inheriting_ctors)
498 : return dfn;
499 : tree fn = dfn;
500 2934265513 : while (tree inh = DECL_INHERITED_CTOR (fn))
501 1568733662 : fn = OVL_FIRST (inh);
502 :
503 1566877084 : if (TREE_CODE (fn) == TEMPLATE_DECL
504 778636298 : && TREE_CODE (dfn) == FUNCTION_DECL)
505 23213 : 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 80623 : inherited_ctor_binfo_1 (tree binfo, tree fndecl)
515 : {
516 80623 : tree base = DECL_CONTEXT (fndecl);
517 80623 : tree base_binfo;
518 81287 : for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
519 81287 : if (BINFO_TYPE (base_binfo) == base)
520 80623 : 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 152566 : inherited_ctor_binfo (tree binfo, tree fndecl)
531 : {
532 305132 : tree inh = DECL_INHERITED_CTOR (fndecl);
533 152566 : if (!inh)
534 : return binfo;
535 :
536 80398 : tree results = NULL_TREE;
537 161246 : for (ovl_iterator iter (inh); iter; ++iter)
538 : {
539 80623 : tree one = inherited_ctor_binfo_1 (binfo, *iter);
540 80623 : if (!results)
541 : results = one;
542 225 : else if (one != results)
543 6 : results = tree_cons (NULL_TREE, one, results);
544 : }
545 80398 : 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 5567489 : inherited_ctor_binfo (tree fndecl)
554 : {
555 11134978 : if (!DECL_INHERITED_CTOR (fndecl))
556 : return NULL_TREE;
557 71943 : tree binfo = TYPE_BINFO (DECL_CONTEXT (fndecl));
558 71943 : return inherited_ctor_binfo (binfo, fndecl);
559 : }
560 :
561 :
562 : /* True if we should omit all user-declared parameters from a base
563 : constructor built from complete constructor FN.
564 : That's when the ctor is inherited from a virtual base. */
565 :
566 : bool
567 210025711 : base_ctor_omit_inherited_parms (tree comp_ctor)
568 : {
569 210025711 : gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (comp_ctor));
570 :
571 210025711 : if (!flag_new_inheriting_ctors)
572 : /* We only optimize away the parameters in the new model. */
573 : return false;
574 :
575 209978009 : if (!CLASSTYPE_VBASECLASSES (DECL_CONTEXT (comp_ctor)))
576 : return false;
577 :
578 6495567 : if (FUNCTION_FIRST_USER_PARMTYPE (comp_ctor) == void_list_node)
579 : /* No user-declared parameters to omit. */
580 : return false;
581 :
582 5497268 : for (tree binfo = inherited_ctor_binfo (comp_ctor);
583 5498798 : 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 1046521643 : ctor_omit_inherited_parms (tree fn)
597 : {
598 1046521643 : gcc_checking_assert (TREE_CODE (fn) == FUNCTION_DECL);
599 :
600 1046521643 : if (!DECL_BASE_CONSTRUCTOR_P (fn))
601 : return false;
602 :
603 154925821 : 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 5491052 : 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 5491615 : for (ovl_iterator iter (inh); iter; ++iter)
616 : {
617 124661 : tree fn = *iter;
618 124661 : tree base = DECL_CONTEXT (fn);
619 124661 : tree base_binfo = NULL_TREE;
620 125485 : for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
621 125485 : if (BINFO_TYPE (base_binfo) == base)
622 : break;
623 124661 : if (base_binfo == init_binfo
624 124661 : || (flag_new_inheriting_ctors
625 587 : && binfo_inherited_from (base_binfo, init_binfo,
626 1174 : DECL_INHERITED_CTOR (fn))))
627 124107 : return true;
628 : }
629 5366945 : 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 164858 : add_one_base_init (tree binfo, tree parm, bool move_p, tree inh,
638 : tree member_init_list)
639 : {
640 164858 : tree init;
641 164858 : if (inh)
642 : {
643 : /* An inheriting constructor only has a mem-initializer for
644 : the base it inherits from. */
645 53908 : if (!binfo_inherited_from (TYPE_BINFO (current_class_type), binfo, inh))
646 : return member_init_list;
647 :
648 53818 : tree *p = &init;
649 53818 : init = NULL_TREE;
650 122330 : for (; parm; parm = DECL_CHAIN (parm))
651 : {
652 68512 : tree exp = forward_parm (parm);
653 68512 : *p = build_tree_list (NULL_TREE, exp);
654 68512 : p = &TREE_CHAIN (*p);
655 : }
656 : }
657 : else
658 : {
659 110950 : init = build_base_path (PLUS_EXPR, parm, binfo, 1,
660 : tf_warning_or_error);
661 110950 : if (move_p)
662 79787 : init = move (init);
663 110950 : init = build_tree_list (NULL_TREE, init);
664 : }
665 164768 : 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 252544 : do_build_copy_constructor (tree fndecl)
673 : {
674 252544 : tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
675 505088 : bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
676 252544 : bool trivial = trivial_fn_p (fndecl);
677 505088 : tree inh = DECL_INHERITED_CTOR (fndecl);
678 :
679 252544 : if (!inh)
680 198741 : parm = convert_from_reference (parm);
681 :
682 252544 : if (trivial)
683 : {
684 91 : 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 36 : else if (tree_int_cst_equal (TYPE_SIZE (current_class_type),
688 36 : CLASSTYPE_SIZE (current_class_type)))
689 : {
690 23 : tree t = cp_build_init_expr (current_class_ref, parm);
691 23 : 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 252453 : tree member_init_list = NULL_TREE;
712 252453 : int i;
713 252453 : tree binfo, base_binfo;
714 252453 : 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 252453 : for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
722 252554 : vec_safe_iterate (vbases, i, &binfo); i++)
723 : {
724 101 : member_init_list = add_one_base_init (binfo, parm, move_p, inh,
725 : member_init_list);
726 : }
727 :
728 417292 : for (binfo = TYPE_BINFO (current_class_type), i = 0;
729 417292 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
730 : {
731 164839 : if (BINFO_VIRTUAL_P (base_binfo))
732 82 : continue;
733 164757 : member_init_list = add_one_base_init (base_binfo, parm, move_p,
734 : inh, member_init_list);
735 : }
736 :
737 252453 : if (!inh)
738 : {
739 198650 : int cvquals = cp_type_quals (TREE_TYPE (parm));
740 :
741 198650 : for (tree fields = TYPE_FIELDS (current_class_type);
742 11469995 : fields; fields = DECL_CHAIN (fields))
743 : {
744 11271345 : tree field = fields;
745 11271345 : tree expr_type;
746 :
747 11271345 : if (TREE_CODE (field) != FIELD_DECL)
748 10973832 : continue;
749 :
750 297513 : expr_type = TREE_TYPE (field);
751 297513 : if (DECL_NAME (field))
752 : {
753 186705 : if (VFIELD_NAME_P (DECL_NAME (field)))
754 1162 : continue;
755 : }
756 110808 : 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 110796 : 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 185555 : if (!TYPE_REF_P (expr_type))
769 : {
770 184478 : int quals = cvquals;
771 :
772 184478 : if (DECL_MUTABLE_P (field))
773 9 : quals &= ~TYPE_QUAL_CONST;
774 184478 : quals |= cp_type_quals (expr_type);
775 184478 : expr_type = cp_build_qualified_type (expr_type, quals);
776 : }
777 :
778 185555 : tree init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
779 111630 : if (move_p && !TYPE_REF_P (expr_type)
780 : /* 'move' breaks bit-fields, and has no effect for scalars. */
781 296647 : && !scalarish_type_p (expr_type))
782 100178 : init = move (init);
783 185555 : init = build_tree_list (NULL_TREE, init);
784 :
785 185555 : member_init_list = tree_cons (field, init, member_init_list);
786 : }
787 : }
788 :
789 252453 : finish_mem_initializers (member_init_list);
790 : }
791 252544 : }
792 :
793 : static void
794 48242 : do_build_copy_assign (tree fndecl)
795 : {
796 48242 : tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl));
797 48242 : tree compound_stmt;
798 48242 : bool move_p = move_fn_p (fndecl);
799 48242 : bool trivial = trivial_fn_p (fndecl);
800 48242 : int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
801 :
802 48242 : compound_stmt = begin_compound_stmt (0);
803 48242 : 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 96484 : tree const class_ref = DECL_XOBJ_MEMBER_FUNCTION_P (fndecl)
809 96484 : ? cp_build_fold_indirect_ref (DECL_ARGUMENTS (fndecl)) : current_class_ref;
810 :
811 48242 : if (trivial
812 48242 : && 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 48239 : 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 48223 : tree fields;
823 48223 : int cvquals = cp_type_quals (TREE_TYPE (parm));
824 48223 : int i;
825 48223 : tree binfo, base_binfo;
826 :
827 : /* Assign to each of the direct base classes. */
828 48223 : for (binfo = TYPE_BINFO (current_class_type), i = 0;
829 67963 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
830 : {
831 19740 : tree converted_parm;
832 :
833 : /* We must convert PARM directly to the base class
834 : explicitly since the base class may be ambiguous. */
835 19740 : converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
836 : tf_warning_or_error);
837 19740 : if (move_p)
838 19090 : converted_parm = move (converted_parm);
839 : /* Call the base class assignment operator. */
840 19740 : releasing_vec parmvec (make_tree_vector_single (converted_parm));
841 19740 : finish_expr_stmt
842 19740 : (build_special_member_call (class_ref,
843 : assign_op_identifier,
844 : &parmvec,
845 : base_binfo,
846 : flags,
847 : tf_warning_or_error));
848 19740 : }
849 :
850 : /* Assign to each of the non-static data members. */
851 48223 : for (fields = TYPE_FIELDS (current_class_type);
852 1655319 : fields;
853 1607096 : fields = DECL_CHAIN (fields))
854 : {
855 1607096 : tree comp = class_ref;
856 1607096 : tree init = parm;
857 1607096 : tree field = fields;
858 1607096 : tree expr_type;
859 1607096 : int quals;
860 :
861 1607096 : if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
862 1576627 : continue;
863 :
864 30469 : expr_type = TREE_TYPE (field);
865 :
866 30469 : 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 30467 : 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 30466 : if (DECL_NAME (field))
880 : {
881 30463 : 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 30466 : comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
893 :
894 : /* Compute the type of init->field */
895 30466 : quals = cvquals;
896 30466 : if (DECL_MUTABLE_P (field))
897 3 : quals &= ~TYPE_QUAL_CONST;
898 30466 : expr_type = cp_build_qualified_type (expr_type, quals);
899 :
900 30466 : init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
901 28471 : if (move_p && !TYPE_REF_P (expr_type)
902 : /* 'move' breaks bit-fields, and has no effect for scalars. */
903 58937 : && !scalarish_type_p (expr_type))
904 28280 : init = move (init);
905 :
906 30466 : if (DECL_NAME (field))
907 30463 : 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 30466 : finish_expr_stmt (init);
912 : }
913 : }
914 48242 : finish_return_stmt (class_ref);
915 48242 : finish_compound_stmt (compound_stmt);
916 48242 : }
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 1029195 : lookup_comparison_result (tree type, const char *name_str,
952 : tsubst_flags_t complain = tf_warning_or_error)
953 : {
954 1029195 : tree name = get_identifier (name_str);
955 1029195 : tree decl = lookup_qualified_name (type, name);
956 1029195 : if (TREE_CODE (decl) != VAR_DECL)
957 : {
958 8 : if (complain & tf_error)
959 : {
960 5 : auto_diagnostic_group d;
961 5 : if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
962 2 : qualified_name_lookup_error (type, name, decl, input_location);
963 : else
964 3 : error ("%qD is not a static data member", decl);
965 5 : inform (input_location, "determining value of %qs", "operator<=>");
966 5 : }
967 8 : 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 621453 : lookup_comparison_category (comp_cat_tag tag,
976 : tsubst_flags_t complain = tf_warning_or_error)
977 : {
978 621453 : if (tree cached = comp_cat_cache[tag])
979 : return cached;
980 :
981 80460 : tree name = get_identifier (comp_cat_info[tag].name);
982 80460 : tree decl = lookup_qualified_name (std_node, name);
983 80460 : if (TREE_CODE (decl) != TYPE_DECL)
984 : {
985 20 : if (complain & tf_error)
986 : {
987 11 : auto_diagnostic_group d;
988 11 : if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
989 8 : qualified_name_lookup_error (std_node, name, decl, input_location);
990 : else
991 3 : error ("%qD is not a type", decl);
992 11 : inform (input_location, "forming type of %qs", "operator<=>");
993 11 : }
994 20 : 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 80440 : tree type = TREE_TYPE (decl);
999 321851 : for (int i = 0; i < 4; ++i)
1000 : {
1001 321745 : const char *p = comp_cat_info[tag].members[i];
1002 321745 : if (!p) break;
1003 241416 : if (lookup_comparison_result (type, p, complain)
1004 241416 : == error_mark_node)
1005 : return error_mark_node;
1006 : }
1007 80435 : return comp_cat_cache[tag] = type;
1008 : }
1009 :
1010 : /* Wrapper that takes the tag rather than the type. */
1011 :
1012 : static tree
1013 343 : lookup_comparison_result (comp_cat_tag tag, const char *name_str,
1014 : tsubst_flags_t complain = tf_warning_or_error)
1015 : {
1016 343 : tree type = lookup_comparison_category (tag, complain);
1017 343 : 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 787436 : lookup_comparison_result (comp_cat_tag tag, tree type, int idx)
1024 : {
1025 787436 : const char *name_str = comp_cat_info[tag].members[idx];
1026 787436 : if (!name_str)
1027 : return NULL_TREE;
1028 787436 : return lookup_comparison_result (type, name_str);
1029 : }
1030 :
1031 : /* Does TYPE correspond to TAG? */
1032 :
1033 : static bool
1034 781266 : is_cat (tree type, comp_cat_tag tag)
1035 : {
1036 781266 : tree name = TYPE_LINKAGE_IDENTIFIER (type);
1037 781266 : 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 261009 : cat_tag_for (tree type)
1044 : {
1045 261009 : if (!CLASS_TYPE_P (type) || !decl_in_std_namespace_p (TYPE_MAIN_DECL (type)))
1046 48 : return cc_last;
1047 781266 : for (int i = 0; i < cc_last; ++i)
1048 : {
1049 781266 : comp_cat_tag tag = (comp_cat_tag)i;
1050 781266 : 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 615987 : spaceship_comp_cat (tree optype)
1061 : {
1062 615987 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (optype) || TYPE_PTROBV_P (optype))
1063 : return cc_strong_ordering;
1064 618 : 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 615987 : spaceship_type (tree optype, tsubst_flags_t complain)
1076 : {
1077 615987 : comp_cat_tag tag = spaceship_comp_cat (optype);
1078 615987 : 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 260547 : genericize_spaceship (location_t loc, tree type, tree op0, tree op1)
1087 : {
1088 : /* ??? maybe optimize based on knowledge of representation? */
1089 260547 : comp_cat_tag tag = cat_tag_for (type);
1090 :
1091 260547 : 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 260547 : else if (tag == cc_last)
1102 3 : return error_mark_node;
1103 :
1104 260544 : tree r;
1105 260544 : bool scalar = SCALAR_TYPE_P (TREE_TYPE (op0));
1106 260487 : if (scalar)
1107 : {
1108 260487 : op0 = save_expr (op0);
1109 260487 : op1 = save_expr (op1);
1110 : }
1111 :
1112 260544 : tree gt = lookup_comparison_result (tag, type, 1);
1113 :
1114 260544 : int flags = LOOKUP_NORMAL;
1115 260544 : tsubst_flags_t complain = tf_none;
1116 260544 : tree comp;
1117 :
1118 260544 : if (tag == cc_partial_ordering)
1119 : {
1120 : /* op0 == op1 ? equivalent : op0 < op1 ? less :
1121 : op1 < op0 ? greater : unordered */
1122 732 : tree uo = lookup_comparison_result (tag, type, 3);
1123 732 : 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 720 : comp = fold_build2 (LT_EXPR, boolean_type_node, op1, op0);
1128 720 : 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 260544 : tree lt = lookup_comparison_result (tag, type, 2);
1141 260544 : if (scalar)
1142 : {
1143 260487 : comp = fold_build2 (LT_EXPR, boolean_type_node, op0, op1);
1144 260487 : 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 260544 : tree eq = lookup_comparison_result (tag, type, 0);
1153 260544 : if (scalar)
1154 : {
1155 260487 : comp = fold_build2 (EQ_EXPR, boolean_type_node, op0, op1);
1156 260487 : 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 222378 : early_check_defaulted_comparison (tree fn)
1172 : {
1173 222378 : location_t loc = DECL_SOURCE_LOCATION (fn);
1174 222378 : tree ctx;
1175 222378 : if (DECL_CLASS_SCOPE_P (fn))
1176 25354 : ctx = DECL_CONTEXT (fn);
1177 : else
1178 394048 : ctx = DECL_FRIEND_CONTEXT (fn);
1179 222378 : bool ok = true;
1180 :
1181 222378 : 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 222374 : if (!DECL_OVERLOADED_OPERATOR_IS (fn, SPACESHIP_EXPR)
1189 222374 : && !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 222374 : ok = false;
1201 : }
1202 :
1203 222374 : bool mem = DECL_IOBJ_MEMBER_FUNCTION_P (fn);
1204 222374 : 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 222374 : 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 222374 : tree parmnode = FUNCTION_FIRST_USER_PARMTYPE (fn);
1215 222374 : bool saw_byval = false;
1216 222374 : bool saw_byref = mem;
1217 222374 : bool saw_bad = false;
1218 641782 : for (; parmnode != void_list_node; parmnode = TREE_CHAIN (parmnode))
1219 : {
1220 419408 : tree parmtype = TREE_VALUE (parmnode);
1221 419408 : if (CLASS_TYPE_P (parmtype))
1222 : saw_byval = true;
1223 336574 : else if (TREE_CODE (parmtype) == REFERENCE_TYPE
1224 336571 : && !TYPE_REF_IS_RVALUE (parmtype)
1225 673139 : && TYPE_QUALS (TREE_TYPE (parmtype)) == TYPE_QUAL_CONST)
1226 : {
1227 336565 : saw_byref = true;
1228 336565 : parmtype = TREE_TYPE (parmtype);
1229 : }
1230 : else
1231 : saw_bad = true;
1232 :
1233 419408 : 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 419387 : else if (!same_type_ignoring_top_level_qualifiers_p (parmtype, ctx))
1246 9 : saw_bad = true;
1247 : }
1248 :
1249 222374 : 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 222374 : DECL_MAYBE_DELETED (fn) = ok;
1265 :
1266 222374 : 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 180 : common_comparison_type (vec<tree> &comps)
1275 : {
1276 180 : tree seen[cc_last] = {};
1277 :
1278 332 : for (unsigned i = 0; i < comps.length(); ++i)
1279 : {
1280 152 : tree comp = comps[i];
1281 152 : if (TREE_CODE (comp) == TREE_LIST)
1282 3 : comp = TREE_VALUE (comp);
1283 152 : tree ctype = TREE_TYPE (comp);
1284 152 : comp_cat_tag tag = cat_tag_for (ctype);
1285 : /* build_comparison_op already checked this. */
1286 152 : gcc_checking_assert (tag < cc_last);
1287 152 : seen[tag] = ctype;
1288 : }
1289 :
1290 : /* Otherwise, if at least one T i is std::partial_ordering, U is
1291 : std::partial_ordering. */
1292 180 : 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 159 : if (tree t = seen[cc_weak_ordering]) return t;
1297 :
1298 : /* Otherwise, U is std::strong_ordering. */
1299 159 : if (tree t = seen[cc_strong_ordering]) return t;
1300 47 : 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 64768 : comp_info (tree fndecl, tsubst_flags_t complain)
1318 64768 : : fndecl (fndecl), complain (complain)
1319 : {
1320 64768 : loc = DECL_SOURCE_LOCATION (fndecl);
1321 :
1322 64768 : first_time = DECL_MAYBE_DELETED (fndecl);
1323 64768 : DECL_MAYBE_DELETED (fndecl) = false;
1324 :
1325 : /* Do we want to try to set constexpr? */
1326 64768 : was_constexp = DECL_DECLARED_CONSTEXPR_P (fndecl);
1327 64768 : constexp = first_time;
1328 64768 : if (constexp)
1329 : /* Set this for var_in_constexpr_fn. */
1330 64667 : DECL_DECLARED_CONSTEXPR_P (fndecl) = true;
1331 :
1332 : /* Do we want to try to set noexcept? */
1333 64768 : noex = first_time;
1334 64768 : if (noex)
1335 : {
1336 64667 : tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fndecl));
1337 87774 : if (raises && !UNEVALUATED_NOEXCEPT_SPEC_P (raises))
1338 : /* There was an explicit exception-specification. */
1339 23107 : noex = false;
1340 : }
1341 64768 : }
1342 :
1343 : /* EXPR is an expression built as part of the function body.
1344 : Adjust the properties appropriately. */
1345 95343 : void check (tree expr)
1346 : {
1347 95343 : if (expr == error_mark_node)
1348 0 : DECL_DELETED_FN (fndecl) = true;
1349 69 : if ((constexp || was_constexp)
1350 95370 : && !potential_rvalue_constant_expression (expr))
1351 : {
1352 1224 : if (was_constexp)
1353 12 : require_potential_rvalue_constant_expression_fncheck (expr);
1354 : else
1355 1212 : constexp = false;
1356 : }
1357 95343 : if (noex && !expr_noexcept_p (expr, tf_none))
1358 78 : noex = false;
1359 95343 : }
1360 :
1361 64768 : ~comp_info ()
1362 : {
1363 64768 : if (first_time)
1364 : {
1365 64667 : DECL_DECLARED_CONSTEXPR_P (fndecl) = constexp || was_constexp;
1366 64667 : tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fndecl));
1367 87774 : if (!raises || UNEVALUATED_NOEXCEPT_SPEC_P (raises))
1368 : {
1369 41560 : raises = noex ? noexcept_true_spec : noexcept_false_spec;
1370 41560 : TREE_TYPE (fndecl) = build_exception_variant (TREE_TYPE (fndecl),
1371 : raises);
1372 : }
1373 : }
1374 64768 : }
1375 : };
1376 :
1377 : /* Subroutine of build_comparison_op, to compare a single subobject. */
1378 :
1379 : static tree
1380 95214 : do_one_comp (location_t loc, const comp_info &info, tree sub, tree lhs, tree rhs)
1381 : {
1382 95214 : const tree_code code = info.code;
1383 95214 : const tree fndecl = info.fndecl;
1384 95214 : const comp_cat_tag retcat = info.retcat;
1385 95214 : const tsubst_flags_t complain = info.complain;
1386 :
1387 95214 : tree overload = NULL_TREE;
1388 95214 : 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 95214 : bool tentative = retcat != cc_last;
1392 190301 : tree comp = build_new_op (loc, code, flags, lhs, rhs,
1393 : NULL_TREE, NULL_TREE, &overload,
1394 : tentative ? tf_none : complain);
1395 :
1396 95214 : if (code != SPACESHIP_EXPR)
1397 : return comp;
1398 :
1399 363 : tree rettype = TREE_TYPE (TREE_TYPE (fndecl));
1400 :
1401 363 : 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 285 : if (FNDECL_USED_AUTO (fndecl)
1429 285 : && 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 279 : else if (!FNDECL_USED_AUTO (fndecl)
1440 279 : && !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 64768 : build_comparison_op (tree fndecl, bool defining, tsubst_flags_t complain)
1461 : {
1462 64768 : comp_info info (fndecl, complain);
1463 :
1464 64768 : if (!defining && !(complain & tf_error) && !DECL_MAYBE_DELETED (fndecl))
1465 : return;
1466 :
1467 64759 : int flags = LOOKUP_NORMAL;
1468 64759 : const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (DECL_NAME (fndecl));
1469 64759 : tree_code code = info.code = op->tree_code;
1470 :
1471 64759 : tree lhs = DECL_ARGUMENTS (fndecl);
1472 64759 : tree rhs = DECL_CHAIN (lhs);
1473 64759 : if (is_this_parameter (lhs))
1474 29076 : lhs = cp_build_fold_indirect_ref (lhs);
1475 : else
1476 35683 : lhs = convert_from_reference (lhs);
1477 64759 : rhs = convert_from_reference (rhs);
1478 64759 : tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (lhs));
1479 64759 : gcc_assert (!defining || COMPLETE_TYPE_P (ctype));
1480 :
1481 64759 : 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 64759 : if (TREE_CODE (ctype) == UNION_TYPE
1486 64759 : && 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 64753 : tree compound_stmt = NULL_TREE;
1495 64753 : if (defining)
1496 64670 : compound_stmt = begin_compound_stmt (0);
1497 : else
1498 83 : ++cp_unevaluated_operand;
1499 :
1500 64753 : tree rettype = TREE_TYPE (TREE_TYPE (fndecl));
1501 64753 : 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 64753 : if (code == EQ_EXPR || code == SPACESHIP_EXPR)
1508 : {
1509 64678 : comp_cat_tag &retcat = (info.retcat = cc_last);
1510 65021 : if (code == SPACESHIP_EXPR && !FNDECL_USED_AUTO (fndecl))
1511 140 : retcat = cat_tag_for (rettype);
1512 :
1513 64678 : bool bad = false;
1514 64678 : 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 65877 : 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 64678 : for (tree field = next_aggregate_field (TYPE_FIELDS (ctype));
1540 159924 : field;
1541 95246 : field = next_aggregate_field (DECL_CHAIN (field)))
1542 : {
1543 190492 : if (DECL_VIRTUAL_P (field) || DECL_FIELD_IS_BASE (field))
1544 : /* We ignore the vptr, and we already handled bases. */
1545 1342 : continue;
1546 :
1547 94047 : tree expr_type = TREE_TYPE (field);
1548 :
1549 94047 : 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 94047 : if (TREE_CODE (expr_type) == REFERENCE_TYPE)
1555 : {
1556 14 : if (complain & tf_error)
1557 5 : inform (field_loc, "cannot default compare "
1558 : "reference member %qD", field);
1559 14 : bad = true;
1560 14 : continue;
1561 : }
1562 6 : else if (ANON_UNION_TYPE_P (expr_type)
1563 94039 : && 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 94027 : tree lhs_mem = build3_loc (field_loc, COMPONENT_REF, expr_type, lhs,
1573 : field, NULL_TREE);
1574 94027 : tree rhs_mem = build3_loc (field_loc, COMPONENT_REF, expr_type, rhs,
1575 : field, NULL_TREE);
1576 94027 : tree loop_indexes = NULL_TREE;
1577 188060 : 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 94027 : if (TREE_CODE (expr_type) == ARRAY_TYPE)
1611 12 : continue;
1612 :
1613 94015 : tree comp = do_one_comp (field_loc, info, field, lhs_mem, rhs_mem);
1614 94015 : 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 93904 : if (loop_indexes)
1628 : {
1629 3 : TREE_VALUE (loop_indexes) = comp;
1630 3 : comp = loop_indexes;
1631 : }
1632 93904 : comps.safe_push (comp);
1633 : }
1634 64678 : if (code == SPACESHIP_EXPR && is_auto (rettype))
1635 : {
1636 180 : rettype = common_comparison_type (comps);
1637 180 : apply_deduced_return_type (fndecl, rettype);
1638 : }
1639 64678 : tree retvaleq;
1640 64678 : if (code == EQ_EXPR)
1641 64335 : retvaleq = boolean_true_node;
1642 : else
1643 : {
1644 343 : tree seql = lookup_comparison_result (cc_strong_ordering,
1645 : "equal", complain);
1646 343 : retvaleq = build_static_cast (input_location, rettype, seql,
1647 : complain);
1648 343 : if (retvaleq == error_mark_node)
1649 : bad = true;
1650 : }
1651 64639 : if (bad)
1652 : {
1653 161 : DECL_DELETED_FN (fndecl) = true;
1654 161 : goto out;
1655 : }
1656 159572 : for (unsigned i = 0; i < comps.length(); ++i)
1657 : {
1658 95055 : tree comp = comps[i];
1659 95055 : tree eq, retval = NULL_TREE, if_ = NULL_TREE;
1660 95055 : tree loop_indexes = NULL_TREE;
1661 95055 : if (defining)
1662 : {
1663 95049 : 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 95049 : 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 95055 : if (code == EQ_EXPR)
1696 : {
1697 : /* if (x==y); else return false; */
1698 94842 : eq = comp;
1699 94842 : retval = boolean_false_node;
1700 : }
1701 : else
1702 : {
1703 : /* if (auto v = x<=>y, v == 0); else return v; */
1704 213 : if (TREE_CODE (comp) == SPACESHIP_EXPR)
1705 0 : TREE_TYPE (comp) = rettype;
1706 : else
1707 213 : comp = build_static_cast (input_location, rettype, comp,
1708 : complain);
1709 213 : info.check (comp);
1710 213 : if (defining)
1711 : {
1712 213 : tree var = create_temporary_var (rettype);
1713 213 : DECL_NAME (var) = get_identifier ("retval");
1714 213 : pushdecl (var);
1715 213 : cp_finish_decl (var, comp, false, NULL_TREE, flags);
1716 213 : comp = retval = var;
1717 : }
1718 213 : eq = build_new_op (info.loc, EQ_EXPR, flags, comp,
1719 : integer_zero_node, NULL_TREE, NULL_TREE,
1720 : NULL, complain);
1721 : }
1722 95055 : tree ceq = contextual_conv_bool (eq, complain);
1723 95055 : info.check (ceq);
1724 95055 : if (defining)
1725 : {
1726 95049 : finish_if_stmt_cond (ceq, if_);
1727 95049 : finish_then_clause (if_);
1728 95049 : begin_else_clause (if_);
1729 95049 : finish_return_stmt (retval);
1730 95049 : finish_else_clause (if_);
1731 95049 : finish_if_stmt (if_);
1732 95052 : 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 64517 : if (defining)
1738 64511 : finish_return_stmt (retvaleq);
1739 64678 : }
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 64750 : out:
1764 64750 : if (defining)
1765 64670 : finish_compound_stmt (compound_stmt);
1766 : else
1767 83 : --cp_unevaluated_operand;
1768 64768 : }
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 2487373 : decl_remember_implicit_trigger_p (tree decl)
1776 : {
1777 2487373 : if (!DECL_ARTIFICIAL (decl))
1778 : return false;
1779 1180089 : 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 1180089 : return (sfk != sfk_inheriting_constructor
1783 1180089 : && sfk != sfk_comparison);
1784 : }
1785 :
1786 : /* Synthesize FNDECL, a non-static member function. */
1787 :
1788 : void
1789 1276350 : synthesize_method (tree fndecl)
1790 : {
1791 1276350 : bool need_body = true;
1792 1276350 : tree stmt;
1793 1276350 : location_t save_input_location = input_location;
1794 1276350 : int error_count = errorcount;
1795 1276350 : int warning_count = warningcount + werrorcount;
1796 1276350 : special_function_kind sfk = special_function_p (fndecl);
1797 1276350 : 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 1276350 : if (decl_remember_implicit_trigger_p (fndecl))
1802 1072422 : DECL_SOURCE_LOCATION (fndecl)
1803 536211 : = 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 1276350 : if (DECL_CLONED_FUNCTION_P (fndecl))
1809 1162934 : fndecl = DECL_CLONED_FUNCTION (fndecl);
1810 :
1811 : /* We may be in the middle of deferred access check. Disable
1812 : it now. */
1813 1276350 : push_deferring_access_checks (dk_no_deferred);
1814 :
1815 1276350 : bool push_to_top = maybe_push_to_top_level (fndecl);
1816 :
1817 1276350 : input_location = DECL_SOURCE_LOCATION (fndecl);
1818 :
1819 1276350 : start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
1820 1276350 : stmt = begin_function_body ();
1821 :
1822 1276350 : if (DECL_ASSIGNMENT_OPERATOR_P (fndecl)
1823 1276350 : && DECL_OVERLOADED_OPERATOR_IS (fndecl, NOP_EXPR))
1824 : {
1825 48242 : do_build_copy_assign (fndecl);
1826 48242 : need_body = false;
1827 : }
1828 2456216 : else if (DECL_CONSTRUCTOR_P (fndecl))
1829 : {
1830 658967 : tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
1831 658967 : if (arg_chain != void_list_node)
1832 252544 : do_build_copy_constructor (fndecl);
1833 : else
1834 406423 : finish_mem_initializers (NULL_TREE);
1835 : }
1836 569141 : else if (sfk == sfk_comparison)
1837 : {
1838 : /* Pass tf_none so the function is just deleted if there's a problem. */
1839 64673 : build_comparison_op (fndecl, true, tf_none);
1840 64673 : 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 771882 : if (need_body)
1846 : {
1847 1163435 : tree compound_stmt;
1848 1163435 : compound_stmt = begin_compound_stmt (BCS_FN_BODY);
1849 1163435 : finish_compound_stmt (compound_stmt);
1850 : }
1851 :
1852 1276350 : finish_function_body (stmt);
1853 1276350 : finish_function (/*inline_p=*/false);
1854 :
1855 : /* Remember that we were defined in this module. */
1856 1276350 : set_instantiating_module (fndecl);
1857 :
1858 1276350 : if (!DECL_DELETED_FN (fndecl))
1859 1276257 : expand_or_defer_fn (fndecl);
1860 :
1861 1276350 : input_location = save_input_location;
1862 :
1863 1276350 : maybe_pop_from_top_level (push_to_top);
1864 :
1865 1276350 : pop_deferring_access_checks ();
1866 :
1867 1276350 : 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 1276350 : }
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 36014 : maybe_synthesize_method (tree fndecl)
1879 : {
1880 36014 : if (special_function_p (fndecl) == sfk_comparison)
1881 : {
1882 36014 : tree lhs = DECL_ARGUMENTS (fndecl);
1883 36014 : if (is_this_parameter (lhs))
1884 340 : lhs = cp_build_fold_indirect_ref (lhs);
1885 : else
1886 35674 : lhs = convert_from_reference (lhs);
1887 36014 : tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (lhs));
1888 36014 : 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 36005 : 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 9125041 : build_stub_type (tree type, int quals, bool rvalue)
1904 : {
1905 9125041 : tree argtype
1906 9125041 : = cp_build_qualified_type (type, quals,
1907 : tf_warning_or_error | tf_ignore_bad_quals);
1908 9125041 : return cp_build_reference_type (argtype, rvalue);
1909 : }
1910 :
1911 : /* Build a dummy glvalue from dereferencing a dummy reference of type
1912 : REFTYPE. */
1913 :
1914 : tree
1915 49266245 : build_stub_object (tree reftype)
1916 : {
1917 49266245 : if (!TYPE_REF_P (reftype))
1918 3931822 : reftype = cp_build_reference_type (reftype, /*rval*/true);
1919 49266245 : tree stub = build1 (CONVERT_EXPR, reftype, integer_one_node);
1920 49266245 : return convert_from_reference (stub);
1921 : }
1922 :
1923 : /* True iff EXPR is the result of build_stub_object. */
1924 :
1925 : bool
1926 1100385846 : is_stub_object (tree expr)
1927 : {
1928 1100385846 : if (!REFERENCE_REF_P (expr))
1929 : return false;
1930 108557654 : expr = TREE_OPERAND (expr, 0);
1931 108557654 : return (TREE_CODE (expr) == CONVERT_EXPR
1932 108557654 : && TREE_OPERAND (expr, 0) == integer_one_node);
1933 : }
1934 :
1935 : /* Build a std::declval<TYPE>() expression and return it. */
1936 :
1937 : static tree
1938 6405766 : build_trait_object (tree type, tsubst_flags_t complain)
1939 : {
1940 : /* TYPE can't be a function with cv-/ref-qualifiers: std::declval is
1941 : defined as
1942 :
1943 : template<class T>
1944 : typename std::add_rvalue_reference<T>::type declval() noexcept;
1945 :
1946 : and std::add_rvalue_reference yields T when T is a function with
1947 : cv- or ref-qualifiers, making the definition ill-formed. */
1948 6405766 : if (FUNC_OR_METHOD_TYPE_P (type)
1949 6405766 : && (type_memfn_quals (type) != TYPE_UNQUALIFIED
1950 244 : || type_memfn_rqual (type) != REF_QUAL_NONE))
1951 : {
1952 20 : if (complain & tf_error)
1953 3 : error ("object cannot have qualified function type %qT", type);
1954 20 : return error_mark_node;
1955 : }
1956 :
1957 6405746 : return build_stub_object (type);
1958 : }
1959 :
1960 : /* Build up an object for [meta.unary.prop]/5.2:
1961 : Otherwise [not a reference or function type], VAL<T> is a prvalue that
1962 : initially has type T. */
1963 :
1964 : static tree
1965 553 : build_prvalue_trait_object (tree t)
1966 : {
1967 553 : if (CLASS_TYPE_P (t))
1968 310 : return force_target_expr (t, void_node, tf_none);
1969 : else
1970 243 : return build1 (CONVERT_EXPR, t, integer_one_node);
1971 : }
1972 :
1973 : /* [func.require] Build an expression of INVOKE(FN_TYPE, ARG_TYPES...). If the
1974 : given is not invocable, returns error_mark_node, unless COMPLAIN includes
1975 : tf_error. */
1976 :
1977 : tree
1978 92683 : build_invoke (tree fn_type, const_tree arg_types, tsubst_flags_t complain)
1979 : {
1980 92683 : if (error_operand_p (fn_type) || error_operand_p (arg_types))
1981 0 : return error_mark_node;
1982 :
1983 92683 : gcc_assert (TYPE_P (fn_type));
1984 92683 : gcc_assert (TREE_CODE (arg_types) == TREE_VEC);
1985 :
1986 : /* Access check is required to determine if the given is invocable. */
1987 92683 : deferring_access_check_sentinel acs (dk_no_deferred);
1988 :
1989 : /* INVOKE is an unevaluated context. */
1990 92683 : cp_unevaluated cp_uneval_guard;
1991 :
1992 92683 : bool is_ptrdatamem;
1993 92683 : bool is_ptrmemfunc;
1994 92683 : if (TREE_CODE (fn_type) == REFERENCE_TYPE)
1995 : {
1996 66889 : tree non_ref_fn_type = TREE_TYPE (fn_type);
1997 66889 : is_ptrdatamem = TYPE_PTRDATAMEM_P (non_ref_fn_type);
1998 66889 : is_ptrmemfunc = TYPE_PTRMEMFUNC_P (non_ref_fn_type);
1999 :
2000 : /* Dereference fn_type if it is a pointer to member. */
2001 66699 : if (is_ptrdatamem || is_ptrmemfunc)
2002 : fn_type = non_ref_fn_type;
2003 : }
2004 : else
2005 : {
2006 25794 : is_ptrdatamem = TYPE_PTRDATAMEM_P (fn_type);
2007 25794 : is_ptrmemfunc = TYPE_PTRMEMFUNC_P (fn_type);
2008 : }
2009 :
2010 26869 : if (is_ptrdatamem && TREE_VEC_LENGTH (arg_types) != 1)
2011 : {
2012 42 : if (complain & tf_error)
2013 0 : error ("pointer to data member type %qT can only be invoked with "
2014 : "one argument", fn_type);
2015 42 : return error_mark_node;
2016 : }
2017 111892 : if (is_ptrmemfunc && TREE_VEC_LENGTH (arg_types) == 0)
2018 : {
2019 37 : if (complain & tf_error)
2020 0 : error ("pointer to member function type %qT must be invoked with "
2021 : "at least one argument", fn_type);
2022 37 : return error_mark_node;
2023 : }
2024 :
2025 : /* Construct an expression of a pointer to member. */
2026 92604 : tree ptrmem_expr;
2027 92604 : if (is_ptrdatamem || is_ptrmemfunc)
2028 : {
2029 19774 : tree datum_type = TREE_VEC_ELT (arg_types, 0);
2030 19774 : tree non_ref_datum_type = datum_type;
2031 19774 : if (TYPE_REF_P (datum_type))
2032 604 : non_ref_datum_type = TREE_TYPE (datum_type);
2033 :
2034 : /* datum must be a class type or a pointer to a class type. */
2035 760 : if (!CLASS_TYPE_P (non_ref_datum_type)
2036 19774 : && !(POINTER_TYPE_P (non_ref_datum_type)
2037 18888 : && CLASS_TYPE_P (TREE_TYPE (non_ref_datum_type))))
2038 : {
2039 132 : if (complain & tf_error)
2040 0 : error ("first argument type %qT of a pointer to member must be a "
2041 : "class type or a pointer to a class type", datum_type);
2042 132 : return error_mark_node;
2043 : }
2044 :
2045 : /* 1.1 & 1.4. */
2046 19642 : tree ptrmem_class_type = TYPE_PTRMEM_CLASS_TYPE (fn_type);
2047 19642 : const bool ptrmem_is_same_or_base_of_datum =
2048 19642 : (same_type_ignoring_top_level_qualifiers_p (ptrmem_class_type,
2049 : non_ref_datum_type)
2050 19642 : || (NON_UNION_CLASS_TYPE_P (ptrmem_class_type)
2051 19012 : && NON_UNION_CLASS_TYPE_P (non_ref_datum_type)
2052 130 : && DERIVED_FROM_P (ptrmem_class_type, non_ref_datum_type)));
2053 :
2054 18994 : bool datum_is_refwrap = false;
2055 18994 : if (!ptrmem_is_same_or_base_of_datum && CLASS_TYPE_P (non_ref_datum_type))
2056 : {
2057 112 : tree datum_decl = TYPE_NAME (TYPE_MAIN_VARIANT (non_ref_datum_type));
2058 112 : if (decl_in_std_namespace_p (datum_decl))
2059 : {
2060 62 : const_tree name = DECL_NAME (datum_decl);
2061 62 : if (name && (id_equal (name, "reference_wrapper")))
2062 : {
2063 : /* 1.2 & 1.5: Retrieve T& from std::reference_wrapper<T>,
2064 : i.e., decltype(datum.get()). */
2065 122 : datum_type =
2066 122 : TREE_VEC_ELT (TYPE_TI_ARGS (non_ref_datum_type), 0);
2067 61 : datum_type = cp_build_reference_type (datum_type, false);
2068 61 : datum_is_refwrap = true;
2069 : }
2070 : }
2071 : }
2072 :
2073 19642 : tree datum_expr = build_trait_object (datum_type, complain);
2074 19642 : if (!ptrmem_is_same_or_base_of_datum && !datum_is_refwrap)
2075 : /* 1.3 & 1.6: Try to dereference datum_expr. */
2076 18933 : datum_expr = build_x_indirect_ref (UNKNOWN_LOCATION, datum_expr,
2077 : RO_UNARY_STAR, NULL_TREE, complain);
2078 :
2079 19642 : if (error_operand_p (datum_expr))
2080 16 : return error_mark_node;
2081 :
2082 19626 : tree fn_expr = build_trait_object (fn_type, complain);
2083 19626 : ptrmem_expr = build_m_component_ref (datum_expr, fn_expr, complain);
2084 :
2085 19626 : if (error_operand_p (ptrmem_expr))
2086 42 : return error_mark_node;
2087 :
2088 19584 : if (is_ptrdatamem)
2089 : return ptrmem_expr;
2090 : }
2091 :
2092 : /* Construct expressions for arguments to INVOKE. For a pointer to member
2093 : function, the first argument, which is the object, is not arguments to
2094 : the function. */
2095 91930 : releasing_vec args;
2096 289691 : for (int i = is_ptrmemfunc ? 1 : 0; i < TREE_VEC_LENGTH (arg_types); ++i)
2097 : {
2098 105837 : tree arg_type = TREE_VEC_ELT (arg_types, i);
2099 105837 : tree arg = build_trait_object (arg_type, complain);
2100 105837 : if (error_operand_p (arg))
2101 6 : return error_mark_node;
2102 105831 : vec_safe_push (args, arg);
2103 : }
2104 :
2105 91924 : tree invoke_expr;
2106 91924 : if (is_ptrmemfunc)
2107 19100 : invoke_expr = build_offset_ref_call_from_tree (ptrmem_expr, &args,
2108 : complain);
2109 : else /* 1.7. */
2110 72824 : invoke_expr = finish_call_expr (build_trait_object (fn_type, complain),
2111 : &args, false, false, complain);
2112 : return invoke_expr;
2113 92683 : }
2114 :
2115 : /* Determine which function will be called when looking up NAME in TYPE,
2116 : called with a single ARGTYPE argument, or no argument if ARGTYPE is
2117 : null. FLAGS and COMPLAIN are as for build_new_method_call.
2118 :
2119 : Returns a FUNCTION_DECL if all is well.
2120 : Returns NULL_TREE if overload resolution failed.
2121 : Returns error_mark_node if the chosen function cannot be called. */
2122 :
2123 : static tree
2124 28075781 : locate_fn_flags (tree type, tree name, tree argtype, int flags,
2125 : tsubst_flags_t complain)
2126 : {
2127 28075781 : tree ob, fn, fns, binfo, rval;
2128 :
2129 28075781 : if (TYPE_P (type))
2130 13166232 : binfo = TYPE_BINFO (type);
2131 : else
2132 : {
2133 14909549 : binfo = type;
2134 14909549 : type = BINFO_TYPE (binfo);
2135 : }
2136 :
2137 28075781 : ob = build_stub_object (cp_build_reference_type (type, false));
2138 28075781 : releasing_vec args;
2139 28075781 : if (argtype)
2140 : {
2141 10634799 : if (TREE_CODE (argtype) == TREE_LIST)
2142 : {
2143 165388 : for (tree elt = argtype; elt && elt != void_list_node;
2144 95159 : elt = TREE_CHAIN (elt))
2145 : {
2146 95159 : tree type = TREE_VALUE (elt);
2147 95159 : tree arg = build_stub_object (type);
2148 95159 : vec_safe_push (args, arg);
2149 : }
2150 : }
2151 : else
2152 : {
2153 10564570 : tree arg = build_stub_object (argtype);
2154 10564570 : args->quick_push (arg);
2155 : }
2156 : }
2157 :
2158 28075781 : fns = lookup_fnfields (binfo, name, 0, complain);
2159 28075781 : rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
2160 :
2161 28075781 : if (fn && rval == error_mark_node)
2162 : return rval;
2163 : else
2164 27092254 : return fn;
2165 28075781 : }
2166 :
2167 : /* Locate the dtor of TYPE. */
2168 :
2169 : tree
2170 2017 : get_dtor (tree type, tsubst_flags_t complain)
2171 : {
2172 2017 : tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
2173 : LOOKUP_NORMAL, complain);
2174 2017 : if (fn == error_mark_node)
2175 12 : return NULL_TREE;
2176 : return fn;
2177 : }
2178 :
2179 : /* Locate the default ctor of TYPE. */
2180 :
2181 : tree
2182 17865 : locate_ctor (tree type)
2183 : {
2184 17865 : tree fn;
2185 :
2186 17865 : push_deferring_access_checks (dk_no_check);
2187 17865 : fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
2188 : LOOKUP_SPECULATIVE, tf_none);
2189 17865 : pop_deferring_access_checks ();
2190 17865 : if (fn == error_mark_node)
2191 176 : return NULL_TREE;
2192 : return fn;
2193 : }
2194 :
2195 : /* Likewise, but give any appropriate errors. */
2196 :
2197 : tree
2198 1255 : get_default_ctor (tree type)
2199 : {
2200 1255 : tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
2201 : LOOKUP_NORMAL, tf_warning_or_error);
2202 1255 : if (fn == error_mark_node)
2203 3 : return NULL_TREE;
2204 : return fn;
2205 : }
2206 :
2207 : /* Locate the copy ctor of TYPE. */
2208 :
2209 : tree
2210 538 : get_copy_ctor (tree type, tsubst_flags_t complain)
2211 : {
2212 538 : int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
2213 538 : ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
2214 538 : tree argtype = build_stub_type (type, quals, false);
2215 538 : tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
2216 : LOOKUP_NORMAL, complain);
2217 538 : if (fn == error_mark_node)
2218 6 : return NULL_TREE;
2219 : return fn;
2220 : }
2221 :
2222 : /* Locate the copy assignment operator of TYPE. */
2223 :
2224 : tree
2225 397 : get_copy_assign (tree type)
2226 : {
2227 397 : int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
2228 397 : ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
2229 397 : tree argtype = build_stub_type (type, quals, false);
2230 397 : tree fn = locate_fn_flags (type, assign_op_identifier, argtype,
2231 : LOOKUP_NORMAL, tf_warning_or_error);
2232 397 : if (fn == error_mark_node)
2233 3 : return NULL_TREE;
2234 : return fn;
2235 : }
2236 :
2237 : /* walk_tree helper function for is_trivially_xible. If *TP is a call,
2238 : return it if it calls something other than a trivial special member
2239 : function. */
2240 :
2241 : static tree
2242 265509 : check_nontriv (tree *tp, int *, void *)
2243 : {
2244 265509 : tree fn = cp_get_callee (*tp);
2245 265509 : if (fn == NULL_TREE)
2246 : return NULL_TREE;
2247 :
2248 10171 : if (TREE_CODE (fn) == ADDR_EXPR)
2249 10156 : fn = TREE_OPERAND (fn, 0);
2250 :
2251 10171 : if (TREE_CODE (fn) != FUNCTION_DECL
2252 10171 : || !trivial_fn_p (fn))
2253 10171 : return fn;
2254 : return NULL_TREE;
2255 : }
2256 :
2257 : /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */
2258 :
2259 : static tree
2260 1498317 : assignable_expr (tree to, tree from, bool explain)
2261 : {
2262 1498317 : cp_unevaluated cp_uneval_guard;
2263 1498317 : tsubst_flags_t complain = explain ? tf_error : tf_none;
2264 :
2265 1498317 : to = build_trait_object (to, complain);
2266 1498317 : if (to == error_mark_node)
2267 : return error_mark_node;
2268 :
2269 1498313 : from = build_trait_object (from, complain);
2270 1498313 : if (from == error_mark_node)
2271 : return error_mark_node;
2272 :
2273 1498313 : tree r = cp_build_modify_expr (input_location, to, NOP_EXPR, from, complain);
2274 1498313 : return r;
2275 1498317 : }
2276 :
2277 : /* The predicate condition for a template specialization
2278 : is_constructible<T, Args...> shall be satisfied if and only if the
2279 : following variable definition would be well-formed for some invented
2280 : variable t: T t(create<Args>()...);
2281 :
2282 : Return something equivalent in well-formedness and triviality. */
2283 :
2284 : static tree
2285 3380045 : constructible_expr (tree to, tree from, bool explain)
2286 : {
2287 3380045 : tree expr;
2288 3380045 : cp_unevaluated cp_uneval_guard;
2289 3380045 : tsubst_flags_t complain = explain ? tf_error : tf_none;
2290 3380045 : const int len = TREE_VEC_LENGTH (from);
2291 3380045 : if (CLASS_TYPE_P (to))
2292 : {
2293 1542705 : if (abstract_virtuals_error (NULL_TREE, to, complain))
2294 8631 : return error_mark_node;
2295 1542606 : tree ctype = to;
2296 1542606 : vec<tree, va_gc> *args = NULL;
2297 1542606 : if (!TYPE_REF_P (to))
2298 1542606 : to = cp_build_reference_type (to, /*rval*/false);
2299 1542606 : tree ob = build_stub_object (to);
2300 1542606 : if (len == 0)
2301 510336 : expr = build_value_init (ctype, complain);
2302 : else
2303 : {
2304 1032270 : vec_alloc (args, len);
2305 2070310 : for (tree arg : tree_vec_range (from))
2306 1038040 : args->quick_push (build_stub_object (arg));
2307 1032270 : expr = build_special_member_call (ob, complete_ctor_identifier, &args,
2308 : ctype, LOOKUP_NORMAL, complain);
2309 : }
2310 1542606 : if (expr == error_mark_node)
2311 : return error_mark_node;
2312 : /* The current state of the standard vis-a-vis LWG 2116 is that
2313 : is_*constructible involves destruction as well. */
2314 1534158 : if (type_build_dtor_call (ctype))
2315 : {
2316 292915 : tree dtor = build_special_member_call (ob, complete_dtor_identifier,
2317 : NULL, ctype, LOOKUP_NORMAL,
2318 : complain);
2319 292915 : if (dtor == error_mark_node)
2320 : return error_mark_node;
2321 292831 : if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
2322 282698 : expr = build2 (COMPOUND_EXPR, void_type_node, expr, dtor);
2323 : }
2324 : }
2325 : else
2326 : {
2327 1837340 : if (len == 0)
2328 294243 : return build_value_init (strip_array_types (to), complain);
2329 1543097 : if (len > 1)
2330 : {
2331 423 : if (cxx_dialect < cxx20)
2332 : {
2333 2 : if (explain)
2334 1 : error ("too many initializers for non-class type %qT", to);
2335 2 : return error_mark_node;
2336 : }
2337 :
2338 : /* In C++20 this is well-formed:
2339 : using T = int[2];
2340 : T t(1, 2);
2341 : which means that std::is_constructible_v<int[2], int, int>
2342 : should be true. */
2343 421 : vec<constructor_elt, va_gc> *v;
2344 421 : vec_alloc (v, len);
2345 1263 : for (tree arg : tree_vec_range (from))
2346 : {
2347 842 : tree stub = build_stub_object (arg);
2348 842 : constructor_elt elt = { NULL_TREE, stub };
2349 842 : v->quick_push (elt);
2350 : }
2351 421 : from = build_constructor (init_list_type_node, v);
2352 421 : CONSTRUCTOR_IS_DIRECT_INIT (from) = true;
2353 421 : CONSTRUCTOR_IS_PAREN_INIT (from) = true;
2354 : }
2355 : else
2356 1542674 : from = build_stub_object (TREE_VEC_ELT (from, 0));
2357 :
2358 1543095 : tree orig_from = from;
2359 1543095 : expr = perform_direct_initialization_if_possible (to, from,
2360 : /*cast*/false,
2361 : complain);
2362 : /* If t(e) didn't work, maybe t{e} will. */
2363 1543095 : if (expr == NULL_TREE
2364 1543095 : && len == 1
2365 3591 : && cxx_dialect >= cxx20)
2366 : {
2367 3566 : from = build_constructor_single (init_list_type_node, NULL_TREE,
2368 : from);
2369 3566 : CONSTRUCTOR_IS_DIRECT_INIT (from) = true;
2370 3566 : CONSTRUCTOR_IS_PAREN_INIT (from) = true;
2371 3566 : expr = perform_direct_initialization_if_possible (to, from,
2372 : /*cast*/false,
2373 : complain);
2374 : }
2375 :
2376 1543095 : if (expr == NULL_TREE && explain)
2377 : {
2378 14 : if (len > 1)
2379 2 : error ("too many initializers for non-class type %qT", to);
2380 : else
2381 : {
2382 : /* Redo the implicit conversion for diagnostics. */
2383 12 : int count = errorcount + warningcount;
2384 12 : perform_implicit_conversion_flags (to, orig_from, complain,
2385 : LOOKUP_NORMAL);
2386 12 : if (count == errorcount + warningcount)
2387 : /* The message may have been suppressed due to -w + -fpermissive,
2388 : emit a generic response instead. */
2389 0 : error ("the conversion is invalid");
2390 : }
2391 : }
2392 : }
2393 : return expr;
2394 3380045 : }
2395 :
2396 : /* Valid if "Either T is a reference type, or T is a complete object type for
2397 : which the expression declval<U&>().~U() is well-formed when treated as an
2398 : unevaluated operand ([expr.context]), where U is remove_all_extents_t<T>."
2399 :
2400 : For a class U, return the destructor call; otherwise return void_node if
2401 : valid or error_mark_node if not. */
2402 :
2403 : static tree
2404 54457 : destructible_expr (tree to, bool explain)
2405 : {
2406 54457 : cp_unevaluated cp_uneval_guard;
2407 54457 : tsubst_flags_t complain = explain ? tf_error : tf_none;
2408 54457 : int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
2409 54457 : if (TYPE_REF_P (to))
2410 278 : return void_node;
2411 54179 : if (!COMPLETE_TYPE_P (complete_type (to)))
2412 : {
2413 59 : if (explain)
2414 0 : error_at (location_of (to), "%qT is incomplete", to);
2415 59 : return error_mark_node;
2416 : }
2417 54120 : to = strip_array_types (to);
2418 54120 : if (CLASS_TYPE_P (to))
2419 : {
2420 29183 : to = build_trait_object (to, complain);
2421 29183 : return build_delete (input_location, TREE_TYPE (to), to,
2422 29183 : sfk_complete_destructor, flags, 0, complain);
2423 : }
2424 : /* [expr.prim.id.dtor] If the id-expression names a pseudo-destructor, T
2425 : shall be a scalar type.... */
2426 24937 : else if (scalarish_type_p (to))
2427 24898 : return void_node;
2428 : else
2429 : {
2430 39 : if (explain)
2431 3 : error_at (location_of (to), "%qT is not a class or scalar type", to);
2432 39 : return error_mark_node;
2433 : }
2434 54457 : }
2435 :
2436 : /* Returns a tree iff TO is assignable (if CODE is MODIFY_EXPR) or
2437 : constructible (otherwise) from FROM, which is a single type for
2438 : assignment or a list of types for construction. If EXPLAIN is
2439 : set, emit a diagnostic explaining why the operation failed. */
2440 :
2441 : static tree
2442 4933595 : is_xible_helper (enum tree_code code, tree to, tree from, bool explain)
2443 : {
2444 4933595 : to = complete_type (to);
2445 4933595 : deferring_access_check_sentinel acs (dk_no_deferred);
2446 :
2447 4933595 : if (VOID_TYPE_P (to))
2448 : {
2449 370 : if (explain)
2450 42 : error_at (location_of (to), "%qT is incomplete", to);
2451 370 : return error_mark_node;
2452 : }
2453 4933225 : if (from
2454 4878768 : && FUNC_OR_METHOD_TYPE_P (from)
2455 4933298 : && (TYPE_READONLY (from) || FUNCTION_REF_QUALIFIED (from)))
2456 : {
2457 33 : if (explain)
2458 0 : error ("%qT is a qualified function type", from);
2459 33 : return error_mark_node;
2460 : }
2461 :
2462 4933192 : tree expr;
2463 4933192 : if (code == MODIFY_EXPR)
2464 1498317 : expr = assignable_expr (to, from, explain);
2465 3434875 : else if (code == BIT_NOT_EXPR)
2466 54457 : expr = destructible_expr (to, explain);
2467 3380418 : else if (TREE_CODE (to) == ARRAY_TYPE && !TYPE_DOMAIN (to))
2468 : {
2469 373 : if (explain)
2470 0 : error ("cannot construct an array of unknown bound");
2471 373 : return error_mark_node;
2472 : }
2473 : else
2474 3380045 : expr = constructible_expr (to, from, explain);
2475 : return expr;
2476 : }
2477 :
2478 : /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or
2479 : constructible (otherwise) from FROM, which is a single type for
2480 : assignment or a list of types for construction. If EXPLAIN, diagnose
2481 : why we returned false. */
2482 :
2483 : bool
2484 119120 : is_trivially_xible (enum tree_code code, tree to, tree from,
2485 : bool explain/*=false*/)
2486 : {
2487 119120 : tree expr = is_xible_helper (code, to, from, explain);
2488 119120 : if (expr == NULL_TREE || expr == error_mark_node)
2489 : return false;
2490 :
2491 117089 : tree nt = cp_walk_tree_without_duplicates (&expr, check_nontriv, NULL);
2492 117089 : if (explain && nt)
2493 12 : inform (location_of (nt), "%qE is non-trivial", nt);
2494 117089 : return !nt;
2495 : }
2496 :
2497 : /* Returns true iff TO is nothrow assignable (if CODE is MODIFY_EXPR) or
2498 : constructible (otherwise) from FROM, which is a single type for
2499 : assignment or a list of types for construction. If EXPLAIN, diagnose
2500 : why we returned false. */
2501 :
2502 : bool
2503 1408731 : is_nothrow_xible (enum tree_code code, tree to, tree from,
2504 : bool explain/*=false*/)
2505 : {
2506 1408731 : ++cp_noexcept_operand;
2507 1408731 : tree expr = is_xible_helper (code, to, from, explain);
2508 1408731 : --cp_noexcept_operand;
2509 1408731 : if (expr == NULL_TREE || expr == error_mark_node)
2510 : return false;
2511 :
2512 1407793 : bool is_noexcept = expr_noexcept_p (expr, tf_none);
2513 1407793 : if (explain && !is_noexcept)
2514 10 : explain_not_noexcept (expr);
2515 : return is_noexcept;
2516 : }
2517 :
2518 : /* Returns true iff TO is assignable (if CODE is MODIFY_EXPR) or
2519 : constructible (otherwise) from FROM, which is a single type for
2520 : assignment or a list of types for construction. If EXPLAIN, diagnose
2521 : why we returned false. */
2522 :
2523 : bool
2524 3405744 : is_xible (enum tree_code code, tree to, tree from, bool explain/*=false*/)
2525 : {
2526 3405744 : tree expr = is_xible_helper (code, to, from, explain);
2527 3405744 : if (expr == error_mark_node)
2528 : return false;
2529 3349585 : return !!expr;
2530 : }
2531 :
2532 : /* Return true iff T is a reference type, and the initialization
2533 : T t(VAL<U>); // DIRECT_INIT_P
2534 : or
2535 : T t = VAL<U>; // !DIRECT_INIT_P
2536 : is well-formed and binds t to a temporary object whose lifetime is
2537 : extended.
2538 : The full-expression of the variable initialization is treated as an
2539 : unevaluated operand. Access checking is performed as if in a context
2540 : unrelated to T and U. Only the validity of the immediate context of
2541 : the variable initialization is considered.
2542 :
2543 : VAL<T> is defined in [meta.unary.prop]:
2544 : -- If T is a reference or function type, VAL<T> is an expression with the
2545 : same type and value category as declval<T>().
2546 : -- Otherwise, VAL<T> is a prvalue that initially has type T. */
2547 :
2548 : bool
2549 274154 : ref_xes_from_temporary (tree to, tree from, bool direct_init_p)
2550 : {
2551 : /* Check is_reference<T>. */
2552 274154 : if (!TYPE_REF_P (to))
2553 : return false;
2554 13638 : deferring_access_check_sentinel acs (dk_no_deferred);
2555 13638 : cp_unevaluated u;
2556 :
2557 13638 : tree val;
2558 13638 : if (TYPE_REF_P (from) || TREE_CODE (from) == FUNCTION_TYPE)
2559 13085 : val = build_trait_object (from, tf_none);
2560 : else
2561 553 : val = build_prvalue_trait_object (from);
2562 13638 : if (val == error_mark_node)
2563 : return false;
2564 13638 : return ref_conv_binds_to_temporary (to, val, direct_init_p).is_true ();
2565 13638 : }
2566 :
2567 : /* Worker for is_{,nothrow_}convertible. Attempt to perform an implicit
2568 : conversion from FROM to TO and return the result. If EXPLAIN, emit a
2569 : diagnostic about why the conversion failed. */
2570 :
2571 : static tree
2572 3149050 : is_convertible_helper (tree from, tree to, bool explain)
2573 : {
2574 3149050 : if (VOID_TYPE_P (from) && VOID_TYPE_P (to))
2575 36 : return integer_one_node;
2576 3149014 : cp_unevaluated u;
2577 3149014 : tsubst_flags_t complain = explain ? tf_error : tf_none;
2578 :
2579 : /* std::is_{,nothrow_}convertible test whether the imaginary function
2580 : definition
2581 :
2582 : To test() { return std::declval<From>(); }
2583 :
2584 : is well-formed. A function can't return a function. */
2585 3149014 : if (FUNC_OR_METHOD_TYPE_P (to))
2586 : {
2587 75 : if (explain)
2588 0 : error ("%qT is a function type", to);
2589 75 : return error_mark_node;
2590 : }
2591 :
2592 3148939 : tree expr = build_trait_object (from, complain);
2593 3148939 : if (expr == error_mark_node)
2594 : return error_mark_node;
2595 :
2596 3148933 : deferring_access_check_sentinel acs (dk_no_deferred);
2597 3148933 : return perform_implicit_conversion (to, expr, complain);
2598 3149014 : }
2599 :
2600 : /* Return true if FROM can be converted to TO using implicit conversions,
2601 : or both FROM and TO are possibly cv-qualified void. NB: This doesn't
2602 : implement the "Access checks are performed as if from a context unrelated
2603 : to either type" restriction. */
2604 :
2605 : bool
2606 3148361 : is_convertible (tree from, tree to, bool explain/*=false*/)
2607 : {
2608 3148361 : tree expr = is_convertible_helper (from, to, explain);
2609 3148361 : if (expr == error_mark_node)
2610 : return false;
2611 2845785 : return !!expr;
2612 : }
2613 :
2614 : /* Like is_convertible, but the conversion is also noexcept. */
2615 :
2616 : bool
2617 689 : is_nothrow_convertible (tree from, tree to, bool explain/*=false*/)
2618 : {
2619 689 : tree expr = is_convertible_helper (from, to, explain);
2620 689 : if (expr == NULL_TREE || expr == error_mark_node)
2621 : return false;
2622 :
2623 355 : bool is_noexcept = expr_noexcept_p (expr, tf_none);
2624 355 : if (explain && !is_noexcept)
2625 3 : explain_not_noexcept (expr);
2626 : return is_noexcept;
2627 : }
2628 :
2629 : /* Categorize various special_function_kinds. */
2630 : #define SFK_CTOR_P(sfk) \
2631 : ((sfk) >= sfk_constructor && (sfk) <= sfk_move_constructor)
2632 : #define SFK_DTOR_P(sfk) \
2633 : ((sfk) == sfk_destructor || (sfk) == sfk_virtual_destructor)
2634 : #define SFK_ASSIGN_P(sfk) \
2635 : ((sfk) == sfk_copy_assignment || (sfk) == sfk_move_assignment)
2636 : #define SFK_COPY_P(sfk) \
2637 : ((sfk) == sfk_copy_constructor || (sfk) == sfk_copy_assignment)
2638 : #define SFK_MOVE_P(sfk) \
2639 : ((sfk) == sfk_move_constructor || (sfk) == sfk_move_assignment)
2640 :
2641 : /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
2642 : DELETED_P or give an error message MSG with argument ARG. */
2643 :
2644 : static void
2645 26612793 : process_subob_fn (tree fn, special_function_kind sfk, tree *spec_p,
2646 : bool *trivial_p, bool *deleted_p, bool *constexpr_p,
2647 : bool diag, tree arg, bool dtor_from_ctor = false)
2648 : {
2649 26612793 : if (!fn || fn == error_mark_node)
2650 : {
2651 997017 : if (deleted_p)
2652 996996 : *deleted_p = true;
2653 997017 : return;
2654 : }
2655 :
2656 25615776 : if (spec_p)
2657 : {
2658 3921492 : if (!maybe_instantiate_noexcept (fn))
2659 3 : *spec_p = error_mark_node;
2660 : else
2661 : {
2662 3921489 : tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2663 3921489 : *spec_p = merge_exception_specifiers (*spec_p, raises);
2664 : }
2665 : }
2666 :
2667 25615776 : if (!trivial_fn_p (fn) && !dtor_from_ctor)
2668 : {
2669 7313862 : if (trivial_p)
2670 4594604 : *trivial_p = false;
2671 7313862 : if (TREE_CODE (arg) == FIELD_DECL
2672 7313862 : && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
2673 : {
2674 6912 : if (deleted_p)
2675 6908 : *deleted_p = true;
2676 6912 : if (diag)
2677 25 : error ("union member %q+D with non-trivial %qD", arg, fn);
2678 : }
2679 : }
2680 :
2681 25615776 : if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
2682 : {
2683 2412236 : *constexpr_p = false;
2684 2412236 : if (diag)
2685 : {
2686 10 : inform (DECL_SOURCE_LOCATION (fn),
2687 10 : SFK_DTOR_P (sfk)
2688 : ? G_("defaulted destructor calls non-%<constexpr%> %qD")
2689 : : G_("defaulted constructor calls non-%<constexpr%> %qD"),
2690 : fn);
2691 10 : explain_invalid_constexpr_fn (fn);
2692 : }
2693 : }
2694 : }
2695 :
2696 : /* Subroutine of synthesized_method_walk to allow recursion into anonymous
2697 : aggregates. If DTOR_FROM_CTOR is true, we're walking subobject destructors
2698 : called from a synthesized constructor, in which case we don't consider
2699 : the triviality of the subobject destructor. */
2700 :
2701 : static void
2702 55893137 : walk_field_subobs (tree fields, special_function_kind sfk, tree fnname,
2703 : int quals, tree *spec_p, bool *trivial_p,
2704 : bool *deleted_p, bool *constexpr_p,
2705 : bool diag, int flags, tsubst_flags_t complain,
2706 : bool dtor_from_ctor)
2707 : {
2708 55893137 : if (!fields)
2709 : return;
2710 :
2711 55893133 : tree ctx = DECL_CONTEXT (fields);
2712 :
2713 : /* CWG2084: A defaulted default ctor for a union with a DMI only initializes
2714 : that member, so don't check other members. */
2715 55893133 : enum { unknown, no, yes }
2716 61789634 : only_dmi_mem = (sfk == sfk_constructor && TREE_CODE (ctx) == UNION_TYPE
2717 55893133 : ? unknown : no);
2718 55893133 : int has_user_provided_ctor = -1;
2719 :
2720 55941053 : again:
2721 1013124804 : for (tree field = fields; field; field = DECL_CHAIN (field))
2722 : {
2723 957364600 : tree mem_type, argtype, rval;
2724 :
2725 1873036942 : if (TREE_CODE (field) != FIELD_DECL
2726 50552006 : || DECL_ARTIFICIAL (field)
2727 999067708 : || DECL_UNNAMED_BIT_FIELD (field))
2728 915672342 : continue;
2729 :
2730 : /* Variant members only affect deletedness. In particular, they don't
2731 : affect the exception-specification of a user-provided destructor,
2732 : which we're figuring out via get_defaulted_eh_spec. So if we aren't
2733 : asking if this is deleted, don't even look up the function; we don't
2734 : want an error about a deleted function we aren't actually calling. */
2735 41692258 : if (sfk == sfk_destructor && deleted_p == NULL
2736 3073722 : && TREE_CODE (ctx) == UNION_TYPE)
2737 : break;
2738 :
2739 41511409 : if (only_dmi_mem != no)
2740 : {
2741 126737 : if (DECL_INITIAL (field))
2742 : only_dmi_mem = yes;
2743 : else
2744 : /* Don't check this until we know there's no DMI. */
2745 126511 : continue;
2746 : }
2747 :
2748 41384898 : mem_type = strip_array_types (TREE_TYPE (field));
2749 41384898 : if (SFK_ASSIGN_P (sfk))
2750 : {
2751 4402539 : bool bad = true;
2752 4402539 : if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
2753 : {
2754 97 : if (diag)
2755 7 : error ("non-static const member %q#D, cannot use default "
2756 : "assignment operator", field);
2757 : }
2758 4402442 : else if (TYPE_REF_P (mem_type))
2759 : {
2760 1672 : if (diag)
2761 2 : error ("non-static reference member %q#D, cannot use "
2762 : "default assignment operator", field);
2763 : }
2764 : else
2765 : bad = false;
2766 :
2767 4402539 : if (bad && deleted_p)
2768 1769 : *deleted_p = true;
2769 : }
2770 36982359 : else if (sfk == sfk_constructor || sfk == sfk_inheriting_constructor)
2771 : {
2772 3332819 : bool bad;
2773 :
2774 3332819 : if (DECL_INITIAL (field))
2775 : {
2776 1057663 : if (diag && DECL_INITIAL (field) == error_mark_node)
2777 0 : inform (DECL_SOURCE_LOCATION (field),
2778 : "initializer for %q#D is invalid", field);
2779 1057663 : if (trivial_p)
2780 910053 : *trivial_p = false;
2781 : /* Core 1351: If the field has an NSDMI that could throw, the
2782 : default constructor is noexcept(false). */
2783 1057663 : if (spec_p)
2784 : {
2785 150145 : tree nsdmi = get_nsdmi (field, /*ctor*/false, complain);
2786 150145 : if (nsdmi == error_mark_node)
2787 111 : *spec_p = error_mark_node;
2788 150034 : else if (*spec_p != error_mark_node
2789 150034 : && !expr_noexcept_p (nsdmi, tf_none))
2790 1894 : *spec_p = noexcept_false_spec;
2791 : }
2792 : /* Don't do the normal processing. */
2793 1057663 : continue;
2794 1057663 : }
2795 :
2796 2275156 : bad = false;
2797 2275156 : if (CP_TYPE_CONST_P (mem_type)
2798 1861 : && TREE_CODE (ctx) != UNION_TYPE
2799 2276992 : && default_init_uninitialized_part (mem_type))
2800 : {
2801 1812 : if (diag)
2802 : {
2803 49 : auto_diagnostic_group d;
2804 49 : error ("uninitialized const member in %q#T",
2805 : current_class_type);
2806 49 : inform (DECL_SOURCE_LOCATION (field),
2807 : "%q#D should be initialized", field);
2808 49 : }
2809 : bad = true;
2810 : }
2811 2273344 : else if (TYPE_REF_P (mem_type))
2812 : {
2813 9797 : if (diag)
2814 : {
2815 48 : auto_diagnostic_group d;
2816 48 : error ("uninitialized reference member in %q#T",
2817 : current_class_type);
2818 48 : inform (DECL_SOURCE_LOCATION (field),
2819 : "%q#D should be initialized", field);
2820 48 : }
2821 : bad = true;
2822 : }
2823 :
2824 2275156 : if (bad && deleted_p)
2825 11609 : *deleted_p = true;
2826 :
2827 : /* Before C++20, for an implicitly-defined default constructor to
2828 : be constexpr, every member must have a user-provided default
2829 : constructor or an explicit initializer. */
2830 2275156 : if (constexpr_p
2831 2058412 : && cxx_dialect < cxx20
2832 84587 : && !CLASS_TYPE_P (mem_type)
2833 2349774 : && TREE_CODE (ctx) != UNION_TYPE)
2834 : {
2835 68932 : *constexpr_p = false;
2836 68932 : if (diag)
2837 2 : inform (DECL_SOURCE_LOCATION (field),
2838 : "defaulted default constructor does not "
2839 : "initialize %q#D", field);
2840 : }
2841 : }
2842 33649540 : else if (sfk == sfk_copy_constructor)
2843 : {
2844 : /* 12.8p11b5 */
2845 5661434 : if (TYPE_REF_P (mem_type)
2846 5661434 : && TYPE_REF_IS_RVALUE (mem_type))
2847 : {
2848 407 : if (diag)
2849 3 : error ("copying non-static data member %q#D of rvalue "
2850 : "reference type", field);
2851 407 : if (deleted_p)
2852 407 : *deleted_p = true;
2853 : }
2854 : }
2855 :
2856 40327235 : if (!CLASS_TYPE_P (mem_type))
2857 28313159 : continue;
2858 :
2859 12014076 : if (ANON_AGGR_TYPE_P (mem_type))
2860 : {
2861 280830 : walk_field_subobs (TYPE_FIELDS (mem_type), sfk, fnname, quals,
2862 : spec_p, trivial_p, deleted_p, constexpr_p,
2863 : diag, flags, complain, dtor_from_ctor);
2864 280830 : continue;
2865 : }
2866 :
2867 11733246 : if (SFK_COPY_P (sfk) || SFK_MOVE_P (sfk))
2868 : {
2869 4437365 : int mem_quals = cp_type_quals (mem_type) | quals;
2870 4437365 : if (DECL_MUTABLE_P (field))
2871 250 : mem_quals &= ~TYPE_QUAL_CONST;
2872 4437365 : argtype = build_stub_type (mem_type, mem_quals, SFK_MOVE_P (sfk));
2873 4437365 : }
2874 : else
2875 : argtype = NULL_TREE;
2876 :
2877 11733246 : if (cxx_dialect >= cxx26 && TREE_CODE (ctx) == UNION_TYPE)
2878 : {
2879 : /* C++26 [class.default.ctor]/2:
2880 : A defaulted default constructor for class X is defined as deleted
2881 : if
2882 : ...
2883 : - any non-variant potentially constructed subobject, except for
2884 : a non-static data member with a brace-or-equal-initializer, has
2885 : class type M (or possibly multidimensional array thereof) and
2886 : overload resolution as applied to find M's corresponding
2887 : constructor does not result in a usable candidate,
2888 : So, for C++26 this ignores default constructors of variant
2889 : members. */
2890 87459 : if (sfk == sfk_constructor || sfk == sfk_inheriting_constructor)
2891 1769 : continue;
2892 :
2893 : /* C++26 [class.default.ctor]/2:
2894 : ...
2895 : - any potentially constructed subobject S has class type M (or
2896 : possibly multidimensional array thereof), M has a destructor
2897 : that is deleted or inaccessible from the defaulted default
2898 : constructor, and either S is non-variant or S has a default
2899 : member initializer.
2900 : This is the dtor_from_ctor case, so ignore destructors of
2901 : variant members unless they have a DMI.
2902 : C++26 with CWG3189 [class.dtor]/4:
2903 : A defaulted destructor for a class X is defined as deleted if
2904 : ...
2905 : - X is has a non-union class and any non-variant potentially
2906 : constructed subobject has S of class type M (or possibly
2907 : multidimensional array thereof) where either
2908 : - S is not a variant member and M has a destructor that is
2909 : deleted or is inaccessible from the defaulted destructor, or
2910 : - S is a variant member, M has a destructor that is deleted,
2911 : inaccessible from the defaulted destructor, or non-trivial,
2912 : and either
2913 : - V S has a default member initializer or
2914 : - X has a user-provided constructor.
2915 : This is the !dtor_from_ctor case, so ignore destructors of
2916 : variant members unless they have a DMI or X has user-provided
2917 : constructor. */
2918 85690 : if (sfk == sfk_destructor)
2919 : {
2920 37629 : if (!dtor_from_ctor && has_user_provided_ctor == -1)
2921 8135 : has_user_provided_ctor
2922 8135 : = type_has_user_provided_constructor (current_class_type);
2923 37629 : if (DECL_INITIAL (field) == NULL_TREE
2924 37629 : && (dtor_from_ctor || !has_user_provided_ctor))
2925 28233 : continue;
2926 : }
2927 : }
2928 :
2929 11703244 : rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
2930 :
2931 11703244 : process_subob_fn (rval, sfk, spec_p, trivial_p, deleted_p,
2932 : constexpr_p, diag, field, dtor_from_ctor);
2933 : }
2934 :
2935 : /* We didn't find a DMI in this union, now check all the members. */
2936 55941053 : if (only_dmi_mem == unknown)
2937 : {
2938 47920 : only_dmi_mem = no;
2939 47920 : goto again;
2940 : }
2941 : }
2942 :
2943 : /* Base walker helper for synthesized_method_walk. Inspect a direct
2944 : or virtual base. BINFO is the parent type's binfo. BASE_BINFO is
2945 : the base binfo of interests. All other parms are as for
2946 : synthesized_method_walk, or its local vars. */
2947 :
2948 : static tree
2949 10121911 : synthesized_method_base_walk (tree binfo, tree base_binfo,
2950 : special_function_kind sfk, tree fnname, int quals,
2951 : tree *inheriting_ctor, tree inherited_parms,
2952 : int flags, bool diag,
2953 : tree *spec_p, bool *trivial_p,
2954 : bool *deleted_p, bool *constexpr_p)
2955 : {
2956 10121911 : bool inherited_binfo = false;
2957 10121911 : tree argtype = NULL_TREE;
2958 10121911 : deferring_kind defer = dk_no_deferred;
2959 :
2960 10121911 : if (SFK_COPY_P (sfk) || SFK_MOVE_P (sfk))
2961 4685354 : argtype = build_stub_type (BINFO_TYPE (base_binfo), quals, SFK_MOVE_P (sfk));
2962 5436557 : else if (inheriting_ctor
2963 5436557 : && (inherited_binfo
2964 5436557 : = binfo_inherited_from (binfo, base_binfo, *inheriting_ctor)))
2965 : {
2966 70256 : argtype = inherited_parms;
2967 : /* Don't check access on the inherited constructor. */
2968 70256 : if (flag_new_inheriting_ctors)
2969 : defer = dk_deferred;
2970 : }
2971 5343014 : else if (cxx_dialect >= cxx14 && sfk == sfk_virtual_destructor
2972 1634025 : && BINFO_VIRTUAL_P (base_binfo)
2973 5544616 : && ABSTRACT_CLASS_TYPE_P (BINFO_TYPE (binfo)))
2974 : /* Don't check access when looking at vbases of abstract class's
2975 : virtual destructor. */
2976 : defer = dk_no_check;
2977 :
2978 4685354 : if (defer != dk_no_deferred)
2979 70385 : push_deferring_access_checks (defer);
2980 20243613 : tree rval = locate_fn_flags (base_binfo, fnname, argtype, flags,
2981 : diag ? tf_warning_or_error : tf_none);
2982 10121911 : if (defer != dk_no_deferred)
2983 70385 : pop_deferring_access_checks ();
2984 :
2985 : /* Replace an inherited template with the appropriate specialization. */
2986 10121911 : if (inherited_binfo && rval
2987 70256 : && DECL_P (*inheriting_ctor) && DECL_P (rval)
2988 10190976 : && DECL_CONTEXT (*inheriting_ctor) == DECL_CONTEXT (rval))
2989 69053 : *inheriting_ctor = DECL_CLONED_FUNCTION (rval);
2990 :
2991 20243822 : process_subob_fn (rval, sfk, spec_p, trivial_p, deleted_p,
2992 10121911 : constexpr_p, diag, BINFO_TYPE (base_binfo));
2993 10121911 : if (SFK_CTOR_P (sfk)
2994 14919835 : && (!BINFO_VIRTUAL_P (base_binfo)
2995 59076 : || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))))
2996 : {
2997 : /* In a constructor we also need to check the subobject
2998 : destructors for cleanup of partially constructed objects. */
2999 4787638 : tree dtor = locate_fn_flags (base_binfo, complete_dtor_identifier,
3000 : NULL_TREE, flags,
3001 : diag ? tf_warning_or_error : tf_none);
3002 : /* Note that we don't pass down trivial_p; the subobject
3003 : destructors don't affect triviality of the constructor. Nor
3004 : do they affect constexpr-ness (a constant expression doesn't
3005 : throw) or exception-specification (a throw from one of the
3006 : dtors would be a double-fault). */
3007 9575276 : process_subob_fn (dtor, sfk, NULL, NULL, deleted_p, NULL, false,
3008 4787638 : BINFO_TYPE (base_binfo), /*dtor_from_ctor*/true);
3009 : }
3010 :
3011 10121911 : return rval;
3012 : }
3013 :
3014 : /* The caller wants to generate an implicit declaration of SFK for
3015 : CTYPE which is const if relevant and CONST_P is set. If SPEC_P,
3016 : TRIVIAL_P, DELETED_P or CONSTEXPR_P are non-null, set their
3017 : referent appropriately. If DIAG is true, we're either being called
3018 : from maybe_explain_implicit_delete to give errors, or if
3019 : CONSTEXPR_P is non-null, from explain_invalid_constexpr_fn. */
3020 :
3021 : static void
3022 35971072 : synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
3023 : tree *spec_p, bool *trivial_p, bool *deleted_p,
3024 : bool *constexpr_p, bool diag,
3025 : tree *inheriting_ctor, tree inherited_parms)
3026 : {
3027 35971072 : tree binfo, base_binfo;
3028 35971072 : int i;
3029 :
3030 : /* SFK must be exactly one category. */
3031 35971072 : gcc_checking_assert (SFK_DTOR_P(sfk) + SFK_CTOR_P(sfk)
3032 : + SFK_ASSIGN_P(sfk) == 1);
3033 :
3034 35971072 : if (spec_p)
3035 4203460 : *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec);
3036 :
3037 35971072 : if (deleted_p)
3038 : {
3039 : /* "The closure type associated with a lambda-expression has a deleted
3040 : default constructor and a deleted copy assignment operator."
3041 : This is diagnosed in maybe_explain_implicit_delete.
3042 : In C++20, only lambda-expressions with lambda-captures have those
3043 : deleted. */
3044 63082926 : if (LAMBDA_TYPE_P (ctype)
3045 1023278 : && (sfk == sfk_constructor || sfk == sfk_copy_assignment)
3046 32070030 : && (cxx_dialect < cxx20
3047 415968 : || LAMBDA_EXPR_CAPTURE_LIST (CLASSTYPE_LAMBDA_EXPR (ctype))
3048 47290 : || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE
3049 47290 : (CLASSTYPE_LAMBDA_EXPR (ctype)) != CPLD_NONE))
3050 : {
3051 164146 : *deleted_p = true;
3052 164146 : return;
3053 : }
3054 :
3055 31694901 : *deleted_p = false;
3056 : }
3057 :
3058 35806926 : bool check_vdtor = false;
3059 35806926 : tree fnname;
3060 :
3061 35806926 : if (SFK_DTOR_P (sfk))
3062 : {
3063 11229620 : check_vdtor = true;
3064 : /* The synthesized method will call base dtors, but check complete
3065 : here to avoid having to deal with VTT. */
3066 11229620 : fnname = complete_dtor_identifier;
3067 : }
3068 24577306 : else if (SFK_ASSIGN_P (sfk))
3069 4748629 : fnname = assign_op_identifier;
3070 : else
3071 19828677 : fnname = complete_ctor_identifier;
3072 :
3073 71543617 : gcc_assert ((sfk == sfk_inheriting_constructor)
3074 : == (inheriting_ctor && *inheriting_ctor != NULL_TREE));
3075 :
3076 : /* If that user-written default constructor would satisfy the
3077 : requirements of a constexpr constructor (7.1.5), the
3078 : implicitly-defined default constructor is constexpr.
3079 :
3080 : C++20:
3081 : The implicitly-defined copy/move assignment operator is constexpr if
3082 : - X is a literal type, and
3083 : - the assignment operator selected to copy/move each direct base class
3084 : subobject is a constexpr function, and
3085 : - for each non-static data member of X that is of class type (or array
3086 : thereof), the assignment operator selected to copy/move that
3087 : member is a constexpr function.
3088 :
3089 : C++23:
3090 : The implicitly-defined copy/move assignment operator is constexpr. */
3091 35806926 : if (constexpr_p)
3092 31694319 : *constexpr_p = (SFK_CTOR_P (sfk)
3093 12704755 : || (SFK_ASSIGN_P (sfk) && cxx_dialect >= cxx14)
3094 39935572 : || (SFK_DTOR_P (sfk) && cxx_dialect >= cxx20));
3095 :
3096 35806926 : bool expected_trivial = type_has_trivial_fn (ctype, sfk);
3097 35806926 : if (trivial_p)
3098 31694302 : *trivial_p = expected_trivial;
3099 :
3100 : /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
3101 : class versions and other properties of the type. But a subobject
3102 : class can be trivially copyable and yet have overload resolution
3103 : choose a template constructor for initialization, depending on
3104 : rvalueness and cv-quals. And furthermore, a member in a base might
3105 : be trivial but deleted or otherwise not callable. So we can't exit
3106 : early in C++0x. The same considerations apply in C++98/03, but
3107 : there the definition of triviality does not consider overload
3108 : resolution, so a constructor can be trivial even if it would otherwise
3109 : call a non-trivial constructor. */
3110 35806926 : if (expected_trivial
3111 27673217 : && (!(SFK_COPY_P (sfk) || SFK_MOVE_P (sfk)) || cxx_dialect < cxx11))
3112 : {
3113 12104224 : if (constexpr_p && sfk == sfk_constructor)
3114 : {
3115 4025795 : bool cx = trivial_default_constructor_is_constexpr (ctype);
3116 4025795 : *constexpr_p = cx;
3117 4025795 : if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
3118 : /* A trivial constructor doesn't have any NSDMI. */
3119 2 : inform (input_location, "defaulted default constructor does "
3120 : "not initialize any non-static data member");
3121 : }
3122 12104224 : if (!diag && cxx_dialect < cxx11)
3123 : return;
3124 : }
3125 :
3126 35793863 : bool push_to_top = maybe_push_to_top_level (TYPE_NAME (ctype));
3127 35793863 : ++cp_unevaluated_operand;
3128 35793863 : ++c_inhibit_evaluation_warnings;
3129 35793863 : push_deferring_access_checks (dk_no_deferred);
3130 :
3131 35793863 : tree scope = push_scope (ctype);
3132 :
3133 35793863 : int flags = LOOKUP_NORMAL | LOOKUP_SPECULATIVE;
3134 35793863 : if (sfk != sfk_inheriting_constructor)
3135 35723628 : flags |= LOOKUP_DEFAULTED;
3136 :
3137 35793863 : tsubst_flags_t complain = diag ? tf_warning_or_error : tf_none;
3138 35793863 : if (diag && spec_p)
3139 : /* We're in get_defaulted_eh_spec; we don't actually want any walking
3140 : diagnostics, we just want complain set. */
3141 3697688 : diag = false;
3142 35793863 : int quals = const_p ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED;
3143 :
3144 45708842 : for (binfo = TYPE_BINFO (ctype), i = 0;
3145 45708842 : BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
3146 : {
3147 9914979 : if (!SFK_ASSIGN_P (sfk) && BINFO_VIRTUAL_P (base_binfo))
3148 : /* We'll handle virtual bases below. */
3149 58595 : continue;
3150 :
3151 9856384 : tree fn = synthesized_method_base_walk (binfo, base_binfo,
3152 : sfk, fnname, quals,
3153 : inheriting_ctor, inherited_parms,
3154 : flags, diag, spec_p, trivial_p,
3155 : deleted_p, constexpr_p);
3156 :
3157 201 : if (diag && SFK_ASSIGN_P (sfk) && SFK_MOVE_P (sfk)
3158 93 : && BINFO_VIRTUAL_P (base_binfo)
3159 36 : && fn && TREE_CODE (fn) == FUNCTION_DECL
3160 36 : && move_fn_p (fn) && !trivial_fn_p (fn)
3161 21 : && vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo))
3162 9856402 : && warning_enabled_at (DECL_SOURCE_LOCATION (fn),
3163 18 : OPT_Wvirtual_move_assign))
3164 12 : warning (OPT_Wvirtual_move_assign,
3165 : "defaulted move assignment for %qT calls a non-trivial "
3166 : "move assignment operator for virtual base %qT",
3167 12 : ctype, BINFO_TYPE (base_binfo));
3168 :
3169 9856384 : if (check_vdtor && type_has_virtual_destructor (BINFO_TYPE (base_binfo)))
3170 : {
3171 : /* Unlike for base ctor/op=/dtor, for operator delete it's fine
3172 : to have a null fn (no class-specific op delete). */
3173 1440911 : fn = locate_fn_flags (ctype, ovl_op_identifier (false, DELETE_EXPR),
3174 : ptr_type_node, flags, tf_none);
3175 1440911 : if (fn && fn == error_mark_node)
3176 : {
3177 21 : if (complain & tf_error)
3178 5 : locate_fn_flags (ctype, ovl_op_identifier (false, DELETE_EXPR),
3179 : ptr_type_node, flags, complain);
3180 21 : if (deleted_p)
3181 16 : *deleted_p = true;
3182 : }
3183 : check_vdtor = false;
3184 : }
3185 : }
3186 :
3187 35793863 : vec<tree, va_gc> *vbases = CLASSTYPE_VBASECLASSES (ctype);
3188 35793863 : if (SFK_ASSIGN_P (sfk))
3189 : /* Already examined vbases above. */;
3190 31046491 : else if (vec_safe_is_empty (vbases))
3191 : /* No virtual bases to worry about. */;
3192 192647 : else if (ABSTRACT_CLASS_TYPE_P (ctype) && cxx_dialect >= cxx14
3193 : /* DR 1658 specifies that vbases of abstract classes are
3194 : ignored for both ctors and dtors. Except DR 2336
3195 : overrides that skipping when determining the eh-spec of a
3196 : virtual destructor. */
3197 193028 : && sfk != sfk_virtual_destructor)
3198 : /* Vbase cdtors are not relevant. */;
3199 : else
3200 : {
3201 192304 : if (constexpr_p && cxx_dialect < cxx26)
3202 8485 : *constexpr_p = false;
3203 :
3204 457831 : FOR_EACH_VEC_ELT (*vbases, i, base_binfo)
3205 265527 : synthesized_method_base_walk (binfo, base_binfo, sfk, fnname, quals,
3206 : inheriting_ctor, inherited_parms,
3207 : flags, diag,
3208 : spec_p, trivial_p, deleted_p, constexpr_p);
3209 : }
3210 :
3211 : /* Now handle the non-static data members. */
3212 35793863 : walk_field_subobs (TYPE_FIELDS (ctype), sfk, fnname, quals,
3213 : spec_p, trivial_p, deleted_p, constexpr_p,
3214 : diag, flags, complain, /*dtor_from_ctor*/false);
3215 35793863 : if (SFK_CTOR_P (sfk))
3216 19818444 : walk_field_subobs (TYPE_FIELDS (ctype), sfk_destructor,
3217 : complete_dtor_identifier, TYPE_UNQUALIFIED,
3218 : NULL, NULL, deleted_p, NULL,
3219 : false, flags, complain, /*dtor_from_ctor*/true);
3220 :
3221 35793863 : pop_scope (scope);
3222 :
3223 35793863 : pop_deferring_access_checks ();
3224 35793863 : --cp_unevaluated_operand;
3225 35793863 : --c_inhibit_evaluation_warnings;
3226 35793863 : maybe_pop_from_top_level (push_to_top);
3227 : }
3228 :
3229 : /* DECL is a defaulted function whose exception specification is now
3230 : needed. Return what it should be. */
3231 :
3232 : tree
3233 4111921 : get_defaulted_eh_spec (tree decl, tsubst_flags_t complain)
3234 : {
3235 : /* For DECL_MAYBE_DELETED this should already have been handled by
3236 : synthesize_method. */
3237 4111921 : gcc_assert (!DECL_MAYBE_DELETED (decl));
3238 :
3239 4111921 : if (DECL_CLONED_FUNCTION_P (decl))
3240 0 : decl = DECL_CLONED_FUNCTION (decl);
3241 4111921 : special_function_kind sfk = special_function_p (decl);
3242 4111921 : tree ctype = DECL_CONTEXT (decl);
3243 4111921 : tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
3244 4111921 : tree parm_type = TREE_VALUE (parms);
3245 4111921 : bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
3246 4111921 : tree spec = empty_except_spec;
3247 4111921 : bool diag = !DECL_DELETED_FN (decl) && (complain & tf_error);
3248 8223842 : tree inh = DECL_INHERITED_CTOR (decl);
3249 4111921 : if (SFK_DTOR_P (sfk) && DECL_VIRTUAL_P (decl))
3250 : /* We have to examine virtual bases even if abstract. */
3251 : sfk = sfk_virtual_destructor;
3252 4111921 : bool pushed = false;
3253 4111921 : if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
3254 2675934 : pushed = push_tinst_level (decl);
3255 4111921 : synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL,
3256 : NULL, diag, &inh, parms);
3257 4111921 : if (pushed)
3258 2675629 : pop_tinst_level ();
3259 4111921 : return spec;
3260 : }
3261 :
3262 : /* DECL is a deleted function. If it's implicitly deleted, explain why and
3263 : return true; else return false. */
3264 :
3265 : bool
3266 2451 : maybe_explain_implicit_delete (tree decl)
3267 : {
3268 : /* If decl is a clone, get the primary variant. */
3269 2451 : decl = DECL_ORIGIN (decl);
3270 2451 : gcc_assert (DECL_DELETED_FN (decl));
3271 2451 : if (DECL_DEFAULTED_FN (decl))
3272 : {
3273 : /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
3274 1772 : static hash_set<tree> *explained;
3275 :
3276 1772 : special_function_kind sfk;
3277 1772 : location_t loc;
3278 1772 : bool informed;
3279 1772 : tree ctype;
3280 :
3281 1772 : if (!explained)
3282 253 : explained = new hash_set<tree>;
3283 1772 : if (explained->add (decl))
3284 : return true;
3285 :
3286 499 : sfk = special_function_p (decl);
3287 499 : ctype = DECL_CONTEXT (decl);
3288 499 : loc = input_location;
3289 499 : input_location = DECL_SOURCE_LOCATION (decl);
3290 :
3291 499 : informed = false;
3292 971 : if (LAMBDA_TYPE_P (ctype))
3293 : {
3294 36 : informed = true;
3295 36 : if (sfk == sfk_constructor)
3296 19 : inform (DECL_SOURCE_LOCATION (decl),
3297 : "a lambda closure type has a deleted default constructor");
3298 17 : else if (sfk == sfk_copy_assignment)
3299 17 : inform (DECL_SOURCE_LOCATION (decl),
3300 : "a lambda closure type has a deleted copy assignment operator");
3301 : else
3302 : informed = false;
3303 : }
3304 463 : else if (DECL_ARTIFICIAL (decl)
3305 330 : && (sfk == sfk_copy_assignment || sfk == sfk_copy_constructor)
3306 578 : && classtype_has_move_assign_or_move_ctor_p (ctype, true))
3307 : {
3308 74 : inform (DECL_SOURCE_LOCATION (decl),
3309 : "%q#D is implicitly declared as deleted because %qT "
3310 : "declares a move constructor or move assignment operator",
3311 : decl, ctype);
3312 74 : informed = true;
3313 : }
3314 389 : else if (sfk == sfk_inheriting_constructor)
3315 : {
3316 18 : tree binfo = inherited_ctor_binfo (decl);
3317 18 : if (TREE_CODE (binfo) != TREE_BINFO)
3318 : {
3319 3 : inform (DECL_SOURCE_LOCATION (decl),
3320 : "%q#D inherits from multiple base subobjects",
3321 : decl);
3322 3 : informed = true;
3323 : }
3324 : }
3325 499 : if (!informed && sfk == sfk_comparison)
3326 : {
3327 77 : inform (DECL_SOURCE_LOCATION (decl),
3328 : "%q#D is implicitly deleted because the default "
3329 : "definition would be ill-formed:", decl);
3330 77 : build_comparison_op (decl, false, tf_warning_or_error);
3331 : }
3332 422 : else if (!informed)
3333 : {
3334 309 : tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
3335 309 : bool const_p = false;
3336 309 : if (parms)
3337 : {
3338 306 : tree parm_type = TREE_VALUE (parms);
3339 306 : const_p = CP_TYPE_CONST_P (non_reference (parm_type));
3340 : }
3341 309 : tree raises = NULL_TREE;
3342 309 : bool deleted_p = false;
3343 309 : tree scope = push_scope (ctype);
3344 618 : tree inh = DECL_INHERITED_CTOR (decl);
3345 :
3346 309 : synthesized_method_walk (ctype, sfk, const_p,
3347 : &raises, NULL, &deleted_p, NULL, false,
3348 : &inh, parms);
3349 309 : if (deleted_p)
3350 : {
3351 290 : inform (DECL_SOURCE_LOCATION (decl),
3352 : "%q#D is implicitly deleted because the default "
3353 : "definition would be ill-formed:", decl);
3354 290 : synthesized_method_walk (ctype, sfk, const_p,
3355 : NULL, NULL, &deleted_p, NULL, true,
3356 : &inh, parms);
3357 : }
3358 19 : else if (!comp_except_specs
3359 19 : (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
3360 : raises, ce_normal))
3361 19 : inform (DECL_SOURCE_LOCATION (decl), "%q#F is implicitly "
3362 : "deleted because its exception-specification does not "
3363 : "match the implicit exception-specification %qX",
3364 : decl, raises);
3365 0 : else if (flag_checking)
3366 0 : gcc_unreachable ();
3367 :
3368 309 : pop_scope (scope);
3369 : }
3370 :
3371 499 : input_location = loc;
3372 499 : return true;
3373 : }
3374 : return false;
3375 : }
3376 :
3377 : /* DECL is a defaulted function which was declared constexpr. Explain why
3378 : it can't be constexpr. */
3379 :
3380 : void
3381 26 : explain_implicit_non_constexpr (tree decl)
3382 : {
3383 26 : tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
3384 26 : bool const_p = CP_TYPE_CONST_P (non_reference (TREE_VALUE (parms)));
3385 52 : tree inh = DECL_INHERITED_CTOR (decl);
3386 26 : bool dummy;
3387 26 : special_function_kind sfk = special_function_p (decl);
3388 26 : if (sfk == sfk_comparison)
3389 : {
3390 9 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
3391 9 : build_comparison_op (decl, false, tf_warning_or_error);
3392 9 : DECL_DECLARED_CONSTEXPR_P (decl) = false;
3393 : }
3394 : else
3395 17 : synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
3396 : sfk, const_p,
3397 : NULL, NULL, NULL, &dummy, true,
3398 : &inh, parms);
3399 26 : }
3400 :
3401 : /* DECL is an instantiation of an inheriting constructor template. Deduce
3402 : the correct exception-specification and deletedness for this particular
3403 : specialization. Return true if the deduction succeeds; false otherwise. */
3404 :
3405 : bool
3406 70205 : deduce_inheriting_ctor (tree decl)
3407 : {
3408 70205 : decl = DECL_ORIGIN (decl);
3409 140410 : gcc_assert (DECL_INHERITED_CTOR (decl));
3410 70205 : tree spec;
3411 70205 : bool trivial, constexpr_, deleted;
3412 70205 : tree inh = DECL_INHERITED_CTOR (decl);
3413 70205 : synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor,
3414 : false, &spec, &trivial, &deleted, &constexpr_,
3415 : /*diag*/false,
3416 : &inh,
3417 70205 : FUNCTION_FIRST_USER_PARMTYPE (decl));
3418 70205 : if (spec == error_mark_node)
3419 : return false;
3420 70203 : if (TREE_CODE (inherited_ctor_binfo (decl)) != TREE_BINFO)
3421 : /* Inherited the same constructor from different base subobjects. */
3422 3 : deleted = true;
3423 70203 : DECL_DELETED_FN (decl) = deleted;
3424 70203 : TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
3425 70203 : SET_DECL_INHERITED_CTOR (decl, inh);
3426 :
3427 70203 : tree clone;
3428 210609 : FOR_EACH_CLONE (clone, decl)
3429 : {
3430 140406 : DECL_DELETED_FN (clone) = deleted;
3431 140406 : TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
3432 140406 : SET_DECL_INHERITED_CTOR (clone, inh);
3433 : }
3434 :
3435 : return true;
3436 : }
3437 :
3438 : /* Returns whether SFK is currently lazy within TYPE, i.e., it hasn't
3439 : yet been declared. */
3440 :
3441 : static bool
3442 32350303 : is_lazy_special_member (special_function_kind sfk, tree type)
3443 : {
3444 32350303 : switch (sfk)
3445 : {
3446 5693340 : case sfk_constructor:
3447 5693340 : return CLASSTYPE_LAZY_DEFAULT_CTOR (type);
3448 7443680 : case sfk_copy_constructor:
3449 7443680 : return CLASSTYPE_LAZY_COPY_CTOR (type);
3450 6004478 : case sfk_move_constructor:
3451 6004478 : return CLASSTYPE_LAZY_MOVE_CTOR (type);
3452 2978268 : case sfk_copy_assignment:
3453 2978268 : return CLASSTYPE_LAZY_COPY_ASSIGN (type);
3454 1504253 : case sfk_move_assignment:
3455 1504253 : return CLASSTYPE_LAZY_MOVE_ASSIGN (type);
3456 8256740 : case sfk_destructor:
3457 8256740 : return CLASSTYPE_LAZY_DESTRUCTOR (type);
3458 : default:
3459 : return false;
3460 : }
3461 : }
3462 :
3463 : /* Implicitly declare the special function indicated by KIND, as a
3464 : member of TYPE. For copy constructors and assignment operators,
3465 : CONST_P indicates whether these functions should take a const
3466 : reference argument or a non-const reference.
3467 : Returns the FUNCTION_DECL for the new implicitly declared function,
3468 : or NULL_TREE if we just discovered the function already exists.
3469 : Currently this can only happen for lazy implicit members. */
3470 :
3471 : tree
3472 32258926 : implicitly_declare_fn (special_function_kind kind, tree type,
3473 : bool const_p, tree pattern_fn,
3474 : tree inherited_parms)
3475 : {
3476 32258926 : tree fn;
3477 32258926 : tree parameter_types = void_list_node;
3478 32258926 : tree return_type;
3479 32258926 : tree fn_type;
3480 32258926 : tree raises = empty_except_spec;
3481 32258926 : tree rhs_parm_type = NULL_TREE;
3482 32258926 : tree this_parm;
3483 32258926 : tree name;
3484 32258926 : HOST_WIDE_INT saved_processing_template_decl;
3485 32258926 : bool deleted_p = false;
3486 32258926 : bool constexpr_p = false;
3487 64517852 : tree inherited_ctor = (kind == sfk_inheriting_constructor
3488 32258926 : ? pattern_fn : NULL_TREE);
3489 :
3490 : /* Because we create declarations for implicitly declared functions
3491 : lazily, we may be creating the declaration for a member of TYPE
3492 : while in some completely different context. However, TYPE will
3493 : never be a dependent class (because we never want to do lookups
3494 : for implicitly defined functions in a dependent class). */
3495 32258926 : gcc_assert (!dependent_type_p (type));
3496 :
3497 : /* If the member-specification does not explicitly declare any member or
3498 : friend named operator==, an == operator function is declared
3499 : implicitly for each three-way comparison operator function defined as
3500 : defaulted in the member-specification, with the same access and
3501 : function-definition and in the same class scope as the respective
3502 : three-way comparison operator function, except that the return type is
3503 : replaced with bool and the declarator-id is replaced with
3504 : operator==.
3505 :
3506 : [Note: Such an implicitly-declared == operator for a class X is
3507 : defined as defaulted in the definition of X and has the same
3508 : parameter-declaration-clause and trailing requires-clause as the
3509 : respective three-way comparison operator. It is declared with friend,
3510 : virtual, constexpr, or consteval if the three-way comparison operator
3511 : function is so declared. If the three-way comparison operator function
3512 : has no noexcept-specifier, the implicitly-declared == operator
3513 : function has an implicit exception specification (14.5) that may
3514 : differ from the implicit exception specification of the three-way
3515 : comparison operator function. --end note] */
3516 32258926 : if (kind == sfk_comparison)
3517 : {
3518 1139 : fn = copy_operator_fn (pattern_fn, EQ_EXPR);
3519 1139 : DECL_ARTIFICIAL (fn) = 1;
3520 1139 : apply_deduced_return_type (fn, boolean_type_node);
3521 1139 : return fn;
3522 : }
3523 :
3524 : /* Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
3525 : because we only create clones for constructors and destructors
3526 : when not in a template. */
3527 32257787 : saved_processing_template_decl = processing_template_decl;
3528 32257787 : processing_template_decl = 0;
3529 :
3530 32257787 : type = TYPE_MAIN_VARIANT (type);
3531 :
3532 32257787 : if (targetm.cxx.cdtor_returns_this ())
3533 : {
3534 0 : if (kind == sfk_destructor)
3535 : /* See comment in check_special_function_return_type. */
3536 0 : return_type = build_pointer_type (void_type_node);
3537 : else
3538 0 : return_type = build_pointer_type (type);
3539 : }
3540 : else
3541 32257787 : return_type = void_type_node;
3542 :
3543 32257787 : int this_quals = TYPE_UNQUALIFIED;
3544 32257787 : switch (kind)
3545 : {
3546 8229850 : case sfk_destructor:
3547 : /* Destructor. */
3548 8229850 : name = dtor_identifier;
3549 8229850 : break;
3550 :
3551 5678982 : case sfk_constructor:
3552 : /* Default constructor. */
3553 5678982 : name = ctor_identifier;
3554 5678982 : break;
3555 :
3556 18348955 : case sfk_copy_constructor:
3557 18348955 : case sfk_copy_assignment:
3558 18348955 : case sfk_move_constructor:
3559 18348955 : case sfk_move_assignment:
3560 18348955 : case sfk_inheriting_constructor:
3561 18348955 : {
3562 18348955 : if (kind == sfk_copy_assignment
3563 18348955 : || kind == sfk_move_assignment)
3564 : {
3565 4475060 : return_type = build_reference_type (type);
3566 4475060 : name = assign_op_identifier;
3567 : }
3568 : else
3569 13873895 : name = ctor_identifier;
3570 :
3571 18348955 : if (kind == sfk_inheriting_constructor)
3572 : parameter_types = inherited_parms;
3573 : else
3574 : {
3575 17879411 : if (const_p)
3576 10393364 : rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
3577 : else
3578 : rhs_parm_type = type;
3579 17879411 : bool move_p = (kind == sfk_move_assignment
3580 17879411 : || kind == sfk_move_constructor);
3581 17879411 : rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
3582 :
3583 17879411 : parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
3584 : }
3585 : break;
3586 : }
3587 :
3588 0 : default:
3589 0 : gcc_unreachable ();
3590 : }
3591 :
3592 32257787 : bool trivial_p = false;
3593 32257787 : bool was_lazy = is_lazy_special_member (kind, type);
3594 :
3595 32257787 : if (inherited_ctor)
3596 : {
3597 : /* For an inheriting constructor, just copy these flags from the
3598 : inherited constructor until deduce_inheriting_ctor. */
3599 469544 : raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor));
3600 469544 : deleted_p = DECL_DELETED_FN (inherited_ctor);
3601 469544 : constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
3602 : }
3603 31788243 : else if (cxx_dialect >= cxx11)
3604 : {
3605 31767221 : raises = noexcept_deferred_spec;
3606 31767221 : synthesized_method_walk (type, kind, const_p, NULL, &trivial_p,
3607 : &deleted_p, &constexpr_p, false,
3608 : &inherited_ctor, inherited_parms);
3609 : }
3610 : else
3611 21022 : synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
3612 : &deleted_p, &constexpr_p, false,
3613 : &inherited_ctor, inherited_parms);
3614 :
3615 : /* The above walk may have indirectly loaded a lazy decl we're
3616 : about to build from a module, let's not build it again. */
3617 32257787 : if (modules_p ()
3618 113033 : && was_lazy
3619 32350303 : && !is_lazy_special_member (kind, type))
3620 : return NULL_TREE;
3621 :
3622 : /* Don't bother marking a deleted constructor as constexpr. */
3623 32257784 : if (deleted_p)
3624 1230006 : constexpr_p = false;
3625 : /* A trivial copy/move constructor is also a constexpr constructor,
3626 : unless the class has virtual bases (7.1.5p4). */
3627 31027778 : else if (trivial_p
3628 26073372 : && cxx_dialect >= cxx11
3629 26060309 : && (kind == sfk_copy_constructor
3630 26060309 : || kind == sfk_move_constructor)
3631 42191665 : && !CLASSTYPE_VBASECLASSES (type))
3632 11163887 : gcc_assert (constexpr_p);
3633 :
3634 32257784 : if (!trivial_p && type_has_trivial_fn (type, kind))
3635 269811 : type_set_nontrivial_flag (type, kind);
3636 :
3637 : /* Create the function. */
3638 32257784 : tree this_type = cp_build_qualified_type (type, this_quals);
3639 32257784 : fn_type = build_method_type_directly (this_type, return_type,
3640 : parameter_types);
3641 :
3642 32257784 : if (raises)
3643 : {
3644 32098512 : if (raises != error_mark_node)
3645 32098509 : fn_type = build_exception_variant (fn_type, raises);
3646 : else
3647 : {
3648 : /* Can happen, e.g., in C++98 mode for an ill-formed non-static data
3649 : member initializer (c++/89914). Also, in C++98, we might have
3650 : failed to deduce RAISES, so try again but complain this time. */
3651 3 : if (cxx_dialect < cxx11)
3652 3 : synthesized_method_walk (type, kind, const_p, &raises, nullptr,
3653 : nullptr, nullptr, /*diag=*/true,
3654 : &inherited_ctor, inherited_parms);
3655 : /* We should have seen an error at this point. */
3656 3 : gcc_assert (seen_error ());
3657 : }
3658 : }
3659 32257784 : fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
3660 32257784 : if (kind != sfk_inheriting_constructor)
3661 31788240 : DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
3662 :
3663 32257784 : if (IDENTIFIER_OVL_OP_P (name))
3664 : {
3665 4475060 : const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (name);
3666 4475060 : DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) = op->ovl_op_code;
3667 4475060 : }
3668 27782724 : else if (IDENTIFIER_CTOR_P (name))
3669 19552877 : DECL_CXX_CONSTRUCTOR_P (fn) = true;
3670 8229847 : else if (IDENTIFIER_DTOR_P (name))
3671 8229847 : DECL_CXX_DESTRUCTOR_P (fn) = true;
3672 : else
3673 0 : gcc_unreachable ();
3674 :
3675 32257784 : SET_DECL_ALIGN (fn, MINIMUM_METHOD_BOUNDARY);
3676 :
3677 : /* Create the explicit arguments. */
3678 32257784 : if (rhs_parm_type)
3679 : {
3680 : /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
3681 : want its type to be included in the mangled function
3682 : name. */
3683 17879411 : tree decl = cp_build_parm_decl (fn, NULL_TREE, rhs_parm_type);
3684 17879411 : TREE_READONLY (decl) = 1;
3685 17879411 : retrofit_lang_decl (decl);
3686 17879411 : DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
3687 17879411 : DECL_ARGUMENTS (fn) = decl;
3688 : }
3689 14378373 : else if (kind == sfk_inheriting_constructor)
3690 : {
3691 469544 : tree *p = &DECL_ARGUMENTS (fn);
3692 469544 : int index = 1;
3693 944295 : for (tree parm = inherited_parms; parm && parm != void_list_node;
3694 474751 : parm = TREE_CHAIN (parm))
3695 : {
3696 474751 : *p = cp_build_parm_decl (fn, NULL_TREE, TREE_VALUE (parm));
3697 474751 : retrofit_lang_decl (*p);
3698 474751 : DECL_PARM_LEVEL (*p) = 1;
3699 474751 : DECL_PARM_INDEX (*p) = index++;
3700 474751 : p = &DECL_CHAIN (*p);
3701 : }
3702 469544 : SET_DECL_INHERITED_CTOR (fn, inherited_ctor);
3703 469544 : DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor);
3704 : /* A constructor so declared has the same access as the corresponding
3705 : constructor in X. */
3706 469544 : TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor);
3707 469544 : TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor);
3708 : /* Copy constexpr from the inherited constructor even if the
3709 : inheriting constructor doesn't satisfy the requirements. */
3710 469544 : constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
3711 469544 : tree inherited_ctor_fn = STRIP_TEMPLATE (inherited_ctor);
3712 : /* Also copy any attributes. */
3713 469544 : DECL_ATTRIBUTES (fn) = clone_attrs (DECL_ATTRIBUTES (inherited_ctor_fn));
3714 : /* But remove gnu::gnu_inline attribute. See PR123526. */
3715 469544 : DECL_ATTRIBUTES (fn)
3716 469544 : = remove_attribute ("gnu", "gnu_inline", DECL_ATTRIBUTES (fn));
3717 469544 : DECL_DISREGARD_INLINE_LIMITS (fn)
3718 469544 : = DECL_DISREGARD_INLINE_LIMITS (inherited_ctor_fn);
3719 : }
3720 :
3721 : /* Add the "this" parameter. */
3722 32257784 : this_parm = build_this_parm (fn, fn_type, this_quals);
3723 32257784 : DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
3724 32257784 : DECL_ARGUMENTS (fn) = this_parm;
3725 :
3726 56285721 : grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
3727 :
3728 32257784 : DECL_IN_AGGR_P (fn) = 1;
3729 32257784 : DECL_ARTIFICIAL (fn) = 1;
3730 32257784 : DECL_DEFAULTED_FN (fn) = 1;
3731 32257784 : if (cxx_dialect >= cxx11)
3732 : {
3733 32236758 : DECL_DELETED_FN (fn) = deleted_p;
3734 32236758 : DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
3735 : }
3736 32257784 : DECL_EXTERNAL (fn) = true;
3737 32257784 : DECL_NOT_REALLY_EXTERN (fn) = 1;
3738 32257784 : DECL_DECLARED_INLINE_P (fn) = 1;
3739 32257784 : set_linkage_according_to_type (type, fn);
3740 32257784 : if (TREE_PUBLIC (fn))
3741 32201885 : DECL_COMDAT (fn) = 1;
3742 32257784 : rest_of_decl_compilation (fn, namespace_bindings_p (), at_eof);
3743 32257784 : gcc_assert (!TREE_USED (fn));
3744 :
3745 : /* Propagate constraints from the inherited constructor. */
3746 32257784 : if (flag_concepts && inherited_ctor)
3747 467632 : if (tree orig_ci = get_constraints (inherited_ctor))
3748 : {
3749 7365 : tree new_ci = copy_node (orig_ci);
3750 7365 : set_constraints (fn, new_ci);
3751 : }
3752 :
3753 : /* Restore PROCESSING_TEMPLATE_DECL. */
3754 32257784 : processing_template_decl = saved_processing_template_decl;
3755 :
3756 32257784 : if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
3757 72985 : fn = add_inherited_template_parms (fn, inherited_ctor);
3758 :
3759 : /* Warn about calling a non-trivial move assignment in a virtual base. */
3760 1500938 : if (kind == sfk_move_assignment && !deleted_p && !trivial_p
3761 32616294 : && CLASSTYPE_VBASECLASSES (type))
3762 : {
3763 84 : location_t loc = input_location;
3764 84 : input_location = DECL_SOURCE_LOCATION (fn);
3765 84 : synthesized_method_walk (type, kind, const_p,
3766 : NULL, NULL, NULL, NULL, true,
3767 : NULL, NULL_TREE);
3768 84 : input_location = loc;
3769 : }
3770 :
3771 : return fn;
3772 : }
3773 :
3774 : /* Maybe mark an explicitly defaulted function FN as =deleted and warn,
3775 : or emit an error, as per [dcl.fct.def.default].
3776 : IMPLICIT_FN is the corresponding special member function that
3777 : would have been implicitly declared. */
3778 :
3779 : static void
3780 6528668 : maybe_delete_defaulted_fn (tree fn, tree implicit_fn)
3781 : {
3782 6528668 : if (DECL_ARTIFICIAL (fn))
3783 6528517 : return;
3784 :
3785 : /* Includes special handling for a default xobj operator.
3786 : Returns 2 for xobj parameter mismatch, 1 if parameters are
3787 : different and 0 if they are the same. */
3788 13057336 : auto compare_fn_params = [] (tree fn, tree implicit_fn)
3789 : {
3790 6528668 : tree fn_parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
3791 6528668 : tree implicit_fn_parms = TYPE_ARG_TYPES (TREE_TYPE (implicit_fn));
3792 :
3793 6528668 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
3794 : {
3795 24 : tree fn_obj_ref_type = TREE_VALUE (fn_parms);
3796 : /* We can't default xobj operators with an xobj parameter that is not
3797 : an lvalue reference, even if it would correspond. */
3798 24 : if (!TYPE_REF_P (fn_obj_ref_type)
3799 24 : || TYPE_REF_IS_RVALUE (fn_obj_ref_type)
3800 48 : || !object_parms_correspond (fn, implicit_fn,
3801 24 : DECL_CONTEXT (implicit_fn)))
3802 0 : return 2;
3803 : /* We just compared the object parameters, skip over them before
3804 : passing to compparms. */
3805 24 : fn_parms = TREE_CHAIN (fn_parms);
3806 24 : implicit_fn_parms = TREE_CHAIN (implicit_fn_parms);
3807 : }
3808 6528668 : return compparms (fn_parms, implicit_fn_parms) ? 0 : 1;
3809 : };
3810 :
3811 6528668 : bool same_ret_type = same_type_p (TREE_TYPE (TREE_TYPE (fn)),
3812 : TREE_TYPE (TREE_TYPE (implicit_fn)));
3813 6528668 : int cmp_params = compare_fn_params (fn, implicit_fn);
3814 6528668 : if (same_ret_type
3815 6528668 : && cmp_params == 0
3816 6528668 : && (cxx_dialect < cxx29 || !FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn))))
3817 : return;
3818 :
3819 232 : auto_diagnostic_group d;
3820 232 : const special_function_kind kind = special_function_p (fn);
3821 232 : tree parmtype
3822 232 : = TREE_VALUE (DECL_XOBJ_MEMBER_FUNCTION_P (fn)
3823 : ? TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)))
3824 : : FUNCTION_FIRST_USER_PARMTYPE (fn));
3825 232 : tree implicit_parmtype
3826 232 : = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (implicit_fn));
3827 :
3828 232 : if (/* [dcl.fct.def.default] "if F1 is an assignment operator"... */
3829 232 : (SFK_ASSIGN_P (kind)
3830 : /* "and the return type of F1 differs from the return type of F2" */
3831 147 : && (!same_ret_type
3832 : /* "or F1's non-object parameter type is not a reference,
3833 : the program is ill-formed" */
3834 108 : || !TYPE_REF_P (parmtype)))
3835 : /* If F1 is *not* explicitly defaulted on its first declaration, the
3836 : program is ill-formed. */
3837 184 : || !DECL_DEFAULTED_IN_CLASS_P (fn)
3838 413 : || (cxx_dialect >= cxx29
3839 : /* For C++29, the only case which is deleted rather than
3840 : ill-formed is when F1 has const C & argument and F2 C &
3841 : and no other non-allowed differences. */
3842 30 : && (FUNCTION_RVALUE_QUALIFIED (TREE_TYPE (fn))
3843 25 : || cmp_params == 2
3844 25 : || TYPE_REF_IS_RVALUE (parmtype)
3845 13 : || TYPE_QUALS (TREE_TYPE (parmtype)) != TYPE_QUAL_CONST
3846 11 : || TYPE_QUALS (TREE_TYPE (implicit_parmtype)))))
3847 : {
3848 74 : error ("defaulted declaration %q+D does not match the expected "
3849 : "signature", fn);
3850 74 : inform (DECL_SOURCE_LOCATION (fn), "expected signature: %qD",
3851 : implicit_fn);
3852 74 : return;
3853 : }
3854 :
3855 158 : DECL_DELETED_FN (fn) = true;
3856 :
3857 101 : const enum diagnostics::kind diag_kind = (cxx_dialect >= cxx20
3858 158 : ? diagnostics::kind::warning
3859 : : diagnostics::kind::pedwarn);
3860 :
3861 : /* Don't warn for template instantiations. */
3862 158 : if (DECL_TEMPLATE_INSTANTIATION (fn)
3863 158 : && diag_kind == diagnostics::kind::warning)
3864 : return;
3865 :
3866 151 : const char *wmsg;
3867 151 : switch (kind)
3868 : {
3869 : case sfk_copy_constructor:
3870 : wmsg = G_("explicitly defaulted copy constructor is implicitly deleted "
3871 : "because its declared type does not match the type of an "
3872 : "implicit copy constructor");
3873 : break;
3874 29 : case sfk_move_constructor:
3875 29 : wmsg = G_("explicitly defaulted move constructor is implicitly deleted "
3876 : "because its declared type does not match the type of an "
3877 : "implicit move constructor");
3878 29 : break;
3879 55 : case sfk_copy_assignment:
3880 55 : wmsg = G_("explicitly defaulted copy assignment operator is implicitly "
3881 : "deleted because its declared type does not match the type "
3882 : "of an implicit copy assignment operator");
3883 55 : break;
3884 27 : case sfk_move_assignment:
3885 27 : wmsg = G_("explicitly defaulted move assignment operator is implicitly "
3886 : "deleted because its declared type does not match the type "
3887 : "of an implicit move assignment operator");
3888 27 : break;
3889 0 : default:
3890 0 : gcc_unreachable ();
3891 : }
3892 151 : if (emit_diagnostic (diag_kind, DECL_SOURCE_LOCATION (fn),
3893 151 : OPT_Wdefaulted_function_deleted, wmsg))
3894 135 : inform (DECL_SOURCE_LOCATION (fn),
3895 : "expected signature: %qD", implicit_fn);
3896 232 : }
3897 :
3898 : /* Gives any errors about defaulted functions which need to be deferred
3899 : until the containing class is complete. IMP_CONST is false or true
3900 : if we are called from check_bases_and_members and signals whether
3901 : the implicit function has a non-object parameter of type const C&. */
3902 :
3903 : void
3904 6567809 : defaulted_late_check (tree fn, tristate imp_const/*=tristate::unknown()*/)
3905 : {
3906 : /* Complain about invalid signature for defaulted fn. */
3907 6567809 : tree ctx = DECL_CONTEXT (fn);
3908 6567809 : special_function_kind kind = special_function_p (fn);
3909 :
3910 6567809 : if (kind == sfk_comparison)
3911 : {
3912 : /* If the function was declared constexpr, check that the definition
3913 : qualifies. Otherwise we can define the function lazily. */
3914 39141 : if (DECL_DECLARED_CONSTEXPR_P (fn) && !DECL_INITIAL (fn))
3915 : {
3916 : /* Prevent GC. */
3917 28656 : function_depth++;
3918 28656 : synthesize_method (fn);
3919 28656 : function_depth--;
3920 : }
3921 39141 : return;
3922 : }
3923 :
3924 6528668 : bool fn_const_p = (copy_fn_p (fn) == 2);
3925 : /* "if F2 has a non-object parameter of type const C&, the corresponding
3926 : non-object parameter of F1 may be of type C&." But not the other way
3927 : around. */
3928 6528668 : if (fn_const_p && imp_const.is_false ())
3929 : fn_const_p = false;
3930 6528668 : tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
3931 : /*pattern_fn=*/NULL_TREE,
3932 : /*inherited_parms=*/NULL_TREE);
3933 6528668 : tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
3934 :
3935 6528668 : maybe_delete_defaulted_fn (fn, implicit_fn);
3936 :
3937 6528668 : if (DECL_DELETED_FN (implicit_fn))
3938 : {
3939 17185 : DECL_DELETED_FN (fn) = 1;
3940 17185 : return;
3941 : }
3942 :
3943 : /* If a function is explicitly defaulted on its first declaration without an
3944 : exception-specification, it is implicitly considered to have the same
3945 : exception-specification as if it had been implicitly declared. */
3946 6511483 : if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn))
3947 6511483 : && DECL_DEFAULTED_IN_CLASS_P (fn))
3948 4401991 : TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
3949 :
3950 13022425 : if (DECL_DEFAULTED_IN_CLASS_P (fn)
3951 13022425 : && DECL_DECLARED_CONSTEXPR_P (implicit_fn))
3952 : {
3953 : /* Hmm...should we do this for out-of-class too? Should it be OK to
3954 : add constexpr later like inline, rather than requiring
3955 : declarations to match? */
3956 4899220 : DECL_DECLARED_CONSTEXPR_P (fn) = true;
3957 4899220 : if (kind == sfk_constructor)
3958 1621349 : TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
3959 : }
3960 :
3961 6511483 : if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
3962 6511483 : && DECL_DECLARED_CONSTEXPR_P (fn))
3963 : {
3964 3086 : if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
3965 : {
3966 16 : auto_diagnostic_group d;
3967 16 : error ("explicitly defaulted function %q+D cannot be declared "
3968 : "%qs because the implicit declaration is not %qs:", fn,
3969 32 : DECL_IMMEDIATE_FUNCTION_P (fn) ? "consteval" : "constexpr",
3970 : "constexpr");
3971 16 : explain_implicit_non_constexpr (fn);
3972 16 : }
3973 3086 : DECL_DECLARED_CONSTEXPR_P (fn) = false;
3974 : }
3975 : }
3976 :
3977 : /* Returns true iff FN can be explicitly defaulted, and gives any
3978 : errors if defaulting FN is ill-formed. */
3979 :
3980 : bool
3981 6992066 : defaultable_fn_check (tree fn)
3982 : {
3983 6992066 : special_function_kind kind = sfk_none;
3984 :
3985 6992066 : if (template_parm_scope_p ())
3986 : {
3987 3 : error ("a template cannot be defaulted");
3988 3 : return false;
3989 : }
3990 :
3991 13984126 : if (DECL_CONSTRUCTOR_P (fn))
3992 : {
3993 4352108 : if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
3994 : kind = sfk_constructor;
3995 2141147 : else if (copy_fn_p (fn) > 0
3996 2141147 : && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
3997 1172316 : == void_list_node))
3998 : kind = sfk_copy_constructor;
3999 968834 : else if (move_fn_p (fn))
4000 : kind = sfk_move_constructor;
4001 : }
4002 2639955 : else if (DECL_DESTRUCTOR_P (fn))
4003 : kind = sfk_destructor;
4004 1911166 : else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
4005 1911166 : && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR))
4006 : {
4007 1688773 : if (copy_fn_p (fn))
4008 : kind = sfk_copy_assignment;
4009 669642 : else if (move_fn_p (fn))
4010 : kind = sfk_move_assignment;
4011 : }
4012 222393 : else if (DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) >= OVL_OP_EQ_EXPR
4013 222393 : && DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) <= OVL_OP_SPACESHIP_EXPR)
4014 : {
4015 222378 : kind = sfk_comparison;
4016 222378 : if (!early_check_defaulted_comparison (fn))
4017 : return false;
4018 : }
4019 :
4020 : /* FIXME: We need to check for xobj member functions here to give better
4021 : diagnostics for weird cases where unrelated xobj parameters are given.
4022 : We just want to do better than 'cannot be defaulted'. */
4023 :
4024 : if (kind == sfk_none)
4025 : {
4026 27 : error ("%qD cannot be defaulted", fn);
4027 27 : return false;
4028 : }
4029 : else
4030 : {
4031 6991957 : for (tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
4032 11241180 : t && t != void_list_node; t = TREE_CHAIN (t))
4033 4249226 : if (TREE_PURPOSE (t))
4034 : {
4035 3 : error ("defaulted function %q+D with default argument", fn);
4036 3 : break;
4037 : }
4038 :
4039 : /* Avoid do_warn_unused_parameter warnings. */
4040 11241183 : for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p))
4041 4249226 : if (DECL_NAME (p))
4042 242094 : suppress_warning (p, OPT_Wunused_parameter);
4043 :
4044 6991957 : if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
4045 : /* Defer checking. */;
4046 22690 : else if (!processing_template_decl)
4047 564 : defaulted_late_check (fn);
4048 :
4049 6991957 : return true;
4050 : }
4051 : }
4052 :
4053 : /* Add an implicit declaration to TYPE for the kind of function
4054 : indicated by SFK. */
4055 :
4056 : void
4057 25259575 : lazily_declare_fn (special_function_kind sfk, tree type)
4058 : {
4059 25259575 : type = TYPE_MAIN_VARIANT (type);
4060 :
4061 : /* Whether or not the argument has a const reference type. */
4062 25259575 : bool const_p = ((sfk == sfk_copy_constructor
4063 6146098 : && TYPE_HAS_CONST_COPY_CTOR (type))
4064 25259812 : || (sfk == sfk_copy_assignment
4065 1647204 : && TYPE_HAS_CONST_COPY_ASSIGN (type)));
4066 :
4067 : /* Declare the function. */
4068 25259575 : tree fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL);
4069 :
4070 : /* We may have indirectly acquired the function from a module,
4071 : if so there's nothing else to do. */
4072 25259575 : if (!fn)
4073 : return;
4074 :
4075 25259572 : switch (sfk)
4076 : {
4077 3899699 : case sfk_constructor:
4078 3899699 : CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
4079 3899699 : break;
4080 6146098 : case sfk_copy_constructor:
4081 6146098 : CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
4082 6146098 : break;
4083 5047105 : case sfk_move_constructor:
4084 5047105 : CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
4085 5047105 : break;
4086 1647204 : case sfk_copy_assignment:
4087 1647204 : CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
4088 1647204 : break;
4089 1112370 : case sfk_move_assignment:
4090 1112370 : CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
4091 1112370 : break;
4092 7407096 : case sfk_destructor:
4093 7407096 : CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
4094 7407096 : break;
4095 0 : default:
4096 0 : gcc_unreachable ();
4097 : }
4098 :
4099 : /* [class.copy]/8 If the class definition declares a move constructor or
4100 : move assignment operator, the implicitly declared copy constructor is
4101 : defined as deleted.... */
4102 25259572 : if ((sfk == sfk_copy_assignment || sfk == sfk_copy_constructor)
4103 7793302 : && cxx_dialect >= cxx11)
4104 : {
4105 7780627 : if (classtype_has_move_assign_or_move_ctor_p (type, true))
4106 925858 : DECL_DELETED_FN (fn) = true;
4107 6854769 : else if (classtype_has_depr_implicit_copy (type))
4108 : /* The implicit definition of a copy constructor as defaulted is
4109 : deprecated if the class has a user-declared copy assignment operator
4110 : or a user-declared destructor. The implicit definition of a copy
4111 : assignment operator as defaulted is deprecated if the class has a
4112 : user-declared copy constructor or a user-declared destructor (15.4,
4113 : 15.8). */
4114 671389 : TREE_DEPRECATED (fn) = true;
4115 : }
4116 :
4117 : /* Destructors and assignment operators may be virtual. */
4118 25259572 : if (sfk == sfk_destructor
4119 25259572 : || sfk == sfk_move_assignment
4120 16740106 : || sfk == sfk_copy_assignment)
4121 10166670 : check_for_override (fn, type);
4122 :
4123 : /* Add it to the class */
4124 25259572 : bool added = add_method (type, fn, false);
4125 25259572 : gcc_assert (added || errorcount);
4126 :
4127 : /* Add it to TYPE_FIELDS. */
4128 25259572 : if (sfk == sfk_destructor
4129 25259572 : && DECL_VIRTUAL_P (fn))
4130 : /* The ABI requires that a virtual destructor go at the end of the
4131 : vtable. */
4132 188011 : TYPE_FIELDS (type) = chainon (TYPE_FIELDS (type), fn);
4133 : else
4134 : {
4135 25071561 : DECL_CHAIN (fn) = TYPE_FIELDS (type);
4136 25071561 : TYPE_FIELDS (type) = fn;
4137 : }
4138 : /* Propagate TYPE_FIELDS. */
4139 25259572 : fixup_type_variants (type);
4140 :
4141 25259572 : maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
4142 25259572 : if (DECL_MAYBE_IN_CHARGE_CDTOR_P (fn))
4143 : /* Create appropriate clones. */
4144 22499998 : clone_cdtor (fn, /*update_methods=*/true);
4145 :
4146 : /* Classes, structs or unions TYPE marked with hotness attributes propagate
4147 : the attribute to all methods. This is typically done in
4148 : check_bases_and_members, but we must also inject them here for deferred
4149 : lazily-declared functions. */
4150 25259572 : maybe_propagate_warmth_attributes (fn, type);
4151 : }
4152 :
4153 : /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
4154 : as there are artificial parms in FN. */
4155 :
4156 : tree
4157 3622528725 : skip_artificial_parms_for (const_tree fn, tree list)
4158 : {
4159 3622528725 : if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
4160 1881520713 : list = TREE_CHAIN (list);
4161 : else
4162 : return list;
4163 :
4164 1881520713 : if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4165 11349199 : list = TREE_CHAIN (list);
4166 1881520713 : if (DECL_HAS_VTT_PARM_P (fn))
4167 14366587 : list = TREE_CHAIN (list);
4168 : return list;
4169 : }
4170 :
4171 : /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
4172 : artificial parms in FN. */
4173 :
4174 : int
4175 420826967 : num_artificial_parms_for (const_tree fn)
4176 : {
4177 420826967 : int count = 0;
4178 :
4179 420826967 : if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
4180 389019514 : count++;
4181 : else
4182 : return 0;
4183 :
4184 389019514 : if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4185 1612 : count++;
4186 389019514 : if (DECL_HAS_VTT_PARM_P (fn))
4187 107220 : count++;
4188 : return count;
4189 : }
4190 :
4191 : /* Return value of the __builtin_type_order trait. */
4192 :
4193 : tree
4194 5076 : type_order_value (tree type1, tree type2)
4195 : {
4196 5076 : tree rettype = lookup_comparison_category (cc_strong_ordering);
4197 5076 : if (rettype == error_mark_node)
4198 : return rettype;
4199 5072 : int ret;
4200 5072 : if (type1 == type2)
4201 : ret = 0;
4202 : else
4203 : {
4204 368 : const char *name1 = ASTRDUP (mangle_type_string (type1));
4205 368 : const char *name2 = mangle_type_string (type2);
4206 368 : ret = strcmp (name1, name2);
4207 : }
4208 5440 : return lookup_comparison_result (cc_strong_ordering, rettype,
4209 5440 : ret == 0 ? 0 : ret > 0 ? 1 : 2);
4210 : }
4211 :
4212 :
4213 : #include "gt-cp-method.h"
|