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