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