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 : 95036 : init_method (void)
45 : : {
46 : 95036 : init_mangle ();
47 : 95036 : }
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 : 1053852 : make_thunk (tree function, bool this_adjusting,
60 : : tree fixed_offset, tree virtual_offset)
61 : : {
62 : 1053852 : HOST_WIDE_INT d;
63 : 1053852 : tree thunk;
64 : :
65 : 1053852 : gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
66 : : /* We can have this thunks to covariant thunks, but not vice versa. */
67 : 1053852 : gcc_assert (!DECL_THIS_THUNK_P (function));
68 : 1053852 : gcc_assert (!DECL_RESULT_THUNK_P (function) || this_adjusting);
69 : :
70 : : /* Scale the VIRTUAL_OFFSET to be in terms of bytes. */
71 : 1053852 : if (this_adjusting && virtual_offset)
72 : 851330 : virtual_offset
73 : 851330 : = size_binop (MULT_EXPR,
74 : : virtual_offset,
75 : : convert (ssizetype,
76 : : TYPE_SIZE_UNIT (vtable_entry_type)));
77 : :
78 : 1053852 : 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 : 2309272 : for (thunk = DECL_THUNKS (function); thunk; thunk = DECL_CHAIN (thunk))
84 : 764943 : if (DECL_THIS_THUNK_P (thunk) == this_adjusting
85 : 764940 : && THUNK_FIXED_OFFSET (thunk) == d
86 : 563831 : && !virtual_offset == !THUNK_VIRTUAL_OFFSET (thunk)
87 : 1328774 : && (!virtual_offset
88 : 484461 : || (this_adjusting
89 : 484461 : ? tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk),
90 : : virtual_offset)
91 : 184 : : THUNK_VIRTUAL_OFFSET (thunk) == virtual_offset)))
92 : 563375 : 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 : 490477 : 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 : 490477 : gcc_assert (TYPE_SIZE (DECL_CONTEXT (function))
101 : : && TYPE_BEING_DEFINED (DECL_CONTEXT (function)));
102 : :
103 : 490477 : thunk = build_decl (DECL_SOURCE_LOCATION (function),
104 : 490477 : FUNCTION_DECL, NULL_TREE, TREE_TYPE (function));
105 : 490477 : DECL_LANG_SPECIFIC (thunk) = DECL_LANG_SPECIFIC (function);
106 : 490477 : cxx_dup_lang_specific_decl (thunk);
107 : 490477 : DECL_VIRTUAL_P (thunk) = true;
108 : 490477 : SET_DECL_THUNKS (thunk, NULL_TREE);
109 : :
110 : 490477 : DECL_CONTEXT (thunk) = DECL_CONTEXT (function);
111 : 490477 : TREE_READONLY (thunk) = TREE_READONLY (function);
112 : 490477 : TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (function);
113 : 490477 : TREE_PUBLIC (thunk) = TREE_PUBLIC (function);
114 : 490477 : SET_DECL_THUNK_P (thunk, this_adjusting);
115 : 490477 : THUNK_TARGET (thunk) = function;
116 : 490477 : THUNK_FIXED_OFFSET (thunk) = d;
117 : 490477 : THUNK_VIRTUAL_OFFSET (thunk) = virtual_offset;
118 : 490477 : THUNK_ALIAS (thunk) = NULL_TREE;
119 : :
120 : 490477 : DECL_INTERFACE_KNOWN (thunk) = 1;
121 : 490477 : DECL_NOT_REALLY_EXTERN (thunk) = 1;
122 : 490477 : DECL_COMDAT (thunk) = DECL_COMDAT (function);
123 : 490477 : 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 : 490477 : DECL_CXX_DESTRUCTOR_P (thunk) = 0;
127 : 490477 : DECL_CXX_CONSTRUCTOR_P (thunk) = 0;
128 : 490477 : DECL_EXTERNAL (thunk) = 1;
129 : 490477 : DECL_ARTIFICIAL (thunk) = 1;
130 : : /* The THUNK is not a pending inline, even if the FUNCTION is. */
131 : 490477 : DECL_PENDING_INLINE_P (thunk) = 0;
132 : 490477 : DECL_DECLARED_INLINE_P (thunk) = 0;
133 : : /* Nor is it a template instantiation. */
134 : 490477 : DECL_USE_TEMPLATE (thunk) = 0;
135 : 490477 : DECL_TEMPLATE_INFO (thunk) = NULL;
136 : :
137 : : /* Add it to the list of thunks associated with FUNCTION. */
138 : 490477 : DECL_CHAIN (thunk) = DECL_THUNKS (function);
139 : 490477 : SET_DECL_THUNKS (function, thunk);
140 : :
141 : 490477 : return thunk;
142 : : }
143 : :
144 : : /* Finish THUNK, a thunk decl. */
145 : :
146 : : void
147 : 490477 : finish_thunk (tree thunk)
148 : : {
149 : 490477 : tree function, name;
150 : 490477 : tree fixed_offset = ssize_int (THUNK_FIXED_OFFSET (thunk));
151 : 490477 : tree virtual_offset = THUNK_VIRTUAL_OFFSET (thunk);
152 : :
153 : 490477 : gcc_assert (!DECL_NAME (thunk) && DECL_THUNK_P (thunk));
154 : 490477 : if (virtual_offset && DECL_RESULT_THUNK_P (thunk))
155 : 142 : virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
156 : 490477 : function = THUNK_TARGET (thunk);
157 : 490691 : 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 : 490477 : 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 : 490477 : DECL_NAME (thunk) = name;
181 : 490477 : SET_DECL_ASSEMBLER_NAME (thunk, name);
182 : 490477 : }
183 : :
184 : : static GTY (()) int thunk_labelno;
185 : :
186 : : /* Create a static alias to target. */
187 : :
188 : : tree
189 : 17224 : make_alias_for (tree target, tree newid)
190 : : {
191 : 17224 : tree alias = build_decl (DECL_SOURCE_LOCATION (target),
192 : 17224 : TREE_CODE (target), newid, TREE_TYPE (target));
193 : 17224 : DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (target);
194 : 17224 : cxx_dup_lang_specific_decl (alias);
195 : 17224 : DECL_CONTEXT (alias) = DECL_CONTEXT (target);
196 : 17224 : TREE_READONLY (alias) = TREE_READONLY (target);
197 : 17224 : TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (target);
198 : 17224 : TREE_PUBLIC (alias) = 0;
199 : 17224 : DECL_INTERFACE_KNOWN (alias) = 1;
200 : 17224 : if (DECL_LANG_SPECIFIC (alias))
201 : : {
202 : 17224 : DECL_NOT_REALLY_EXTERN (alias) = 1;
203 : 17224 : DECL_USE_TEMPLATE (alias) = 0;
204 : 17224 : DECL_TEMPLATE_INFO (alias) = NULL;
205 : : }
206 : 17224 : DECL_EXTERNAL (alias) = 0;
207 : 17224 : DECL_ARTIFICIAL (alias) = 1;
208 : 17224 : DECL_TEMPLATE_INSTANTIATED (alias) = 0;
209 : 17224 : if (TREE_CODE (alias) == FUNCTION_DECL)
210 : : {
211 : 17180 : DECL_SAVED_AUTO_RETURN_TYPE (alias) = NULL;
212 : 17180 : DECL_CXX_DESTRUCTOR_P (alias) = 0;
213 : 17180 : DECL_CXX_CONSTRUCTOR_P (alias) = 0;
214 : 17180 : DECL_PENDING_INLINE_P (alias) = 0;
215 : 17180 : DECL_DECLARED_INLINE_P (alias) = 0;
216 : 17180 : DECL_INITIAL (alias) = error_mark_node;
217 : 17180 : DECL_ARGUMENTS (alias) = copy_list (DECL_ARGUMENTS (target));
218 : : }
219 : : else
220 : 44 : TREE_STATIC (alias) = 1;
221 : 17224 : TREE_ADDRESSABLE (alias) = 1;
222 : 17224 : TREE_USED (alias) = 1;
223 : 17224 : SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
224 : 17224 : return alias;
225 : : }
226 : :
227 : : static tree
228 : 4368 : make_alias_for_thunk (tree function)
229 : : {
230 : 4368 : tree alias;
231 : 4368 : char buf[256];
232 : :
233 : 4368 : targetm.asm_out.generate_internal_label (buf, "LTHUNK", thunk_labelno);
234 : 4368 : thunk_labelno++;
235 : :
236 : 4368 : alias = make_alias_for (function, get_identifier (buf));
237 : :
238 : 4368 : if (!flag_syntax_only)
239 : : {
240 : 4368 : struct cgraph_node *funcn, *aliasn;
241 : 4368 : funcn = cgraph_node::get (function);
242 : 4368 : gcc_checking_assert (funcn);
243 : 4368 : aliasn = cgraph_node::create_same_body_alias (alias, function);
244 : 4368 : DECL_ASSEMBLER_NAME (function);
245 : 4368 : gcc_assert (aliasn != NULL);
246 : : }
247 : :
248 : 4368 : 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 : 8437 : use_thunk (tree thunk_fndecl, bool emit_p)
257 : : {
258 : 8437 : tree a, t, function, alias;
259 : 8437 : tree virtual_offset;
260 : 8437 : HOST_WIDE_INT fixed_offset, virtual_value;
261 : 8437 : bool this_adjusting = DECL_THIS_THUNK_P (thunk_fndecl);
262 : 8437 : struct cgraph_node *funcn, *thunk_node;
263 : :
264 : : /* We should have called finish_thunk to give it a name. */
265 : 8437 : gcc_assert (DECL_NAME (thunk_fndecl));
266 : :
267 : : /* We should never be using an alias, always refer to the
268 : : aliased thunk. */
269 : 8437 : gcc_assert (!THUNK_ALIAS (thunk_fndecl));
270 : :
271 : 8437 : if (TREE_ASM_WRITTEN (thunk_fndecl))
272 : : return;
273 : :
274 : 4473 : function = THUNK_TARGET (thunk_fndecl);
275 : 4473 : 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 : 4473 : 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 : 4473 : TREE_ADDRESSABLE (thunk_fndecl) = 1;
286 : :
287 : : /* Don't diagnose deprecated or unavailable functions just because they
288 : : have thunks emitted for them. */
289 : 4473 : auto du = make_temp_override (deprecated_state,
290 : 4473 : UNAVAILABLE_DEPRECATED_SUPPRESS);
291 : :
292 : : /* Figure out what function is being thunked to. It's referenced in
293 : : this translation unit. */
294 : 4473 : TREE_ADDRESSABLE (function) = 1;
295 : 4473 : mark_used (function);
296 : 4473 : if (!emit_p)
297 : : return;
298 : :
299 : 4368 : if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function))
300 : 4368 : alias = make_alias_for_thunk (function);
301 : : else
302 : : alias = function;
303 : :
304 : 4368 : fixed_offset = THUNK_FIXED_OFFSET (thunk_fndecl);
305 : 4368 : virtual_offset = THUNK_VIRTUAL_OFFSET (thunk_fndecl);
306 : :
307 : 4368 : if (virtual_offset)
308 : : {
309 : 2845 : if (!this_adjusting)
310 : 112 : virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
311 : 2845 : virtual_value = tree_to_shwi (virtual_offset);
312 : 2845 : 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 : 4368 : mark_used (thunk_fndecl);
319 : : /* This thunk is actually defined. */
320 : 4368 : DECL_EXTERNAL (thunk_fndecl) = 0;
321 : : /* The linkage of the function may have changed. FIXME in linkage
322 : : rewrite. */
323 : 4368 : gcc_assert (DECL_INTERFACE_KNOWN (function));
324 : 4368 : TREE_PUBLIC (thunk_fndecl) = TREE_PUBLIC (function);
325 : 4368 : DECL_VISIBILITY (thunk_fndecl) = DECL_VISIBILITY (function);
326 : 8736 : DECL_VISIBILITY_SPECIFIED (thunk_fndecl)
327 : 4368 : = DECL_VISIBILITY_SPECIFIED (function);
328 : 4368 : DECL_COMDAT (thunk_fndecl) = DECL_COMDAT (function);
329 : 4368 : DECL_WEAK (thunk_fndecl) = DECL_WEAK (function);
330 : :
331 : 4368 : if (flag_syntax_only)
332 : : {
333 : 0 : TREE_ASM_WRITTEN (thunk_fndecl) = 1;
334 : 0 : return;
335 : : }
336 : :
337 : 4368 : push_to_top_level ();
338 : :
339 : 4368 : if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function)
340 : 4368 : && targetm_common.have_named_sections)
341 : : {
342 : 4368 : tree fn = function;
343 : 4368 : struct symtab_node *symbol;
344 : :
345 : 4368 : if ((symbol = symtab_node::get (function))
346 : 4368 : && 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 : 4368 : resolve_unique_section (fn, 0, flag_function_sections);
354 : :
355 : 4368 : if (DECL_SECTION_NAME (fn) != NULL && DECL_ONE_ONLY (fn))
356 : : {
357 : 3634 : resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
358 : :
359 : : /* Output the thunk into the same section as function. */
360 : 3634 : set_decl_section_name (thunk_fndecl, fn);
361 : 7268 : symtab_node::get (thunk_fndecl)->implicit_section
362 : 3634 : = symtab_node::get (fn)->implicit_section;
363 : : }
364 : : }
365 : :
366 : : /* Set up cloned argument trees for the thunk. */
367 : 4368 : t = NULL_TREE;
368 : 9345 : for (a = DECL_ARGUMENTS (function); a; a = DECL_CHAIN (a))
369 : : {
370 : 4977 : tree x = copy_node (a);
371 : 4977 : DECL_CHAIN (x) = t;
372 : 4977 : DECL_CONTEXT (x) = thunk_fndecl;
373 : 4977 : SET_DECL_RTL (x, NULL);
374 : 4977 : DECL_HAS_VALUE_EXPR_P (x) = 0;
375 : 4977 : TREE_ADDRESSABLE (x) = 0;
376 : 4977 : t = x;
377 : : }
378 : 4368 : a = nreverse (t);
379 : 4368 : DECL_ARGUMENTS (thunk_fndecl) = a;
380 : 4368 : TREE_ASM_WRITTEN (thunk_fndecl) = 1;
381 : 4368 : funcn = cgraph_node::get (function);
382 : 4368 : gcc_checking_assert (funcn);
383 : 4368 : thunk_node = funcn->create_thunk (thunk_fndecl, function,
384 : : this_adjusting, fixed_offset, virtual_value,
385 : : 0, virtual_offset, alias);
386 : 4368 : if (DECL_ONE_ONLY (function))
387 : 3634 : thunk_node->add_to_same_comdat_group (funcn);
388 : :
389 : 4368 : pop_from_top_level ();
390 : 4473 : }
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 : 58148812 : type_has_trivial_fn (tree ctype, special_function_kind sfk)
398 : : {
399 : 58148812 : switch (sfk)
400 : : {
401 : 8672316 : case sfk_constructor:
402 : 8672316 : return !TYPE_HAS_COMPLEX_DFLT (ctype);
403 : 11227073 : case sfk_copy_constructor:
404 : 11227073 : return !TYPE_HAS_COMPLEX_COPY_CTOR (ctype);
405 : 8072145 : case sfk_move_constructor:
406 : 8072145 : return !TYPE_HAS_COMPLEX_MOVE_CTOR (ctype);
407 : 5372265 : case sfk_copy_assignment:
408 : 5372265 : return !TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype);
409 : 3524431 : case sfk_move_assignment:
410 : 3524431 : return !TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype);
411 : 20739526 : case sfk_destructor:
412 : 20739526 : case sfk_virtual_destructor:
413 : 20739526 : 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 : 234500 : type_set_nontrivial_flag (tree ctype, special_function_kind sfk)
427 : : {
428 : 234500 : switch (sfk)
429 : : {
430 : 141439 : case sfk_constructor:
431 : 141439 : TYPE_HAS_COMPLEX_DFLT (ctype) = true;
432 : 141439 : return;
433 : 3 : case sfk_copy_constructor:
434 : 3 : TYPE_HAS_COMPLEX_COPY_CTOR (ctype) = true;
435 : 3 : return;
436 : 92571 : case sfk_move_constructor:
437 : 92571 : TYPE_HAS_COMPLEX_MOVE_CTOR (ctype) = true;
438 : 92571 : return;
439 : 228 : case sfk_copy_assignment:
440 : 228 : TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype) = true;
441 : 228 : 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 : 229173847 : trivial_fn_p (tree fn)
458 : : {
459 : 229173847 : if (TREE_CODE (fn) == TEMPLATE_DECL)
460 : : return false;
461 : 229173847 : if (!DECL_DEFAULTED_FN (fn))
462 : : return false;
463 : :
464 : : /* If fn is a clone, get the primary variant. */
465 : 28182392 : if (tree prim = DECL_CLONED_FUNCTION (fn))
466 : 22287835 : fn = prim;
467 : 28182392 : 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 : 75505 : forward_parm (tree parm)
475 : : {
476 : 75505 : tree exp = convert_from_reference (parm);
477 : 75505 : tree type = TREE_TYPE (parm);
478 : 75505 : if (DECL_PACK_P (parm))
479 : 564 : type = PACK_EXPANSION_PATTERN (type);
480 : 75505 : if (!TYPE_REF_P (type))
481 : 63009 : type = cp_build_reference_type (type, /*rval=*/true);
482 : 75505 : warning_sentinel w (warn_useless_cast);
483 : 75505 : exp = build_static_cast (input_location, type, exp,
484 : : tf_warning_or_error);
485 : 75505 : if (DECL_PACK_P (parm))
486 : 564 : exp = make_pack_expansion (exp);
487 : 75505 : return exp;
488 : 75505 : }
489 : :
490 : : /* Strip all inheriting constructors, if any, to return the original
491 : : constructor from a (possibly indirect) base class. */
492 : :
493 : : tree
494 : 743772143 : strip_inheriting_ctors (tree dfn)
495 : : {
496 : 743772143 : if (!flag_new_inheriting_ctors)
497 : : return dfn;
498 : : tree fn = dfn;
499 : 1420648775 : while (tree inh = DECL_INHERITED_CTOR (fn))
500 : 744657134 : fn = OVL_FIRST (inh);
501 : :
502 : 743728492 : if (TREE_CODE (fn) == TEMPLATE_DECL
503 : 456058472 : && TREE_CODE (dfn) == FUNCTION_DECL)
504 : 5960 : 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 : 62534 : inherited_ctor_binfo_1 (tree binfo, tree fndecl)
514 : : {
515 : 62534 : tree base = DECL_CONTEXT (fndecl);
516 : 62534 : tree base_binfo;
517 : 63026 : for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
518 : 63026 : if (BINFO_TYPE (base_binfo) == base)
519 : 62534 : 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 : 117250 : inherited_ctor_binfo (tree binfo, tree fndecl)
530 : : {
531 : 234500 : tree inh = DECL_INHERITED_CTOR (fndecl);
532 : 117250 : if (!inh)
533 : : return binfo;
534 : :
535 : 62309 : tree results = NULL_TREE;
536 : 125068 : for (ovl_iterator iter (inh); iter; ++iter)
537 : : {
538 : 62534 : tree one = inherited_ctor_binfo_1 (binfo, *iter);
539 : 62534 : if (!results)
540 : : results = one;
541 : 225 : else if (one != results)
542 : 6 : results = tree_cons (NULL_TREE, one, results);
543 : : }
544 : 62309 : 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 : 4206732 : inherited_ctor_binfo (tree fndecl)
553 : : {
554 : 8413464 : if (!DECL_INHERITED_CTOR (fndecl))
555 : : return NULL_TREE;
556 : 54716 : tree binfo = TYPE_BINFO (DECL_CONTEXT (fndecl));
557 : 54716 : 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 : 160200789 : base_ctor_omit_inherited_parms (tree comp_ctor)
567 : : {
568 : 160200789 : gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (comp_ctor));
569 : :
570 : 160200789 : if (!flag_new_inheriting_ctors)
571 : : /* We only optimize away the parameters in the new model. */
572 : : return false;
573 : :
574 : 160153087 : if (!CLASSTYPE_VBASECLASSES (DECL_CONTEXT (comp_ctor)))
575 : : return false;
576 : :
577 : 5079458 : if (FUNCTION_FIRST_USER_PARMTYPE (comp_ctor) == void_list_node)
578 : : /* No user-declared parameters to omit. */
579 : : return false;
580 : :
581 : 4153738 : for (tree binfo = inherited_ctor_binfo (comp_ctor);
582 : 4155268 : 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 : 806700160 : ctor_omit_inherited_parms (tree fn)
596 : : {
597 : 806700160 : gcc_checking_assert (TREE_CODE (fn) == FUNCTION_DECL);
598 : :
599 : 806700160 : if (!DECL_BASE_CONSTRUCTOR_P (fn))
600 : : return false;
601 : :
602 : 117730639 : 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 : 4701216 : 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 : 4701584 : for (ovl_iterator iter (inh); iter; ++iter)
615 : : {
616 : 93069 : tree fn = *iter;
617 : 93069 : tree base = DECL_CONTEXT (fn);
618 : 93069 : tree base_binfo = NULL_TREE;
619 : 93507 : for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
620 : 93507 : if (BINFO_TYPE (base_binfo) == base)
621 : : break;
622 : 93069 : if (base_binfo == init_binfo
623 : 93069 : || (flag_new_inheriting_ctors
624 : 392 : && binfo_inherited_from (base_binfo, init_binfo,
625 : 784 : DECL_INHERITED_CTOR (fn))))
626 : 92710 : return true;
627 : : }
628 : 4608506 : 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 : 152149 : add_one_base_init (tree binfo, tree parm, bool move_p, tree inh,
637 : : tree member_init_list)
638 : : {
639 : 152149 : tree init;
640 : 152149 : if (inh)
641 : : {
642 : : /* An inheriting constructor only has a mem-initializer for
643 : : the base it inherits from. */
644 : 39717 : if (!binfo_inherited_from (TYPE_BINFO (current_class_type), binfo, inh))
645 : : return member_init_list;
646 : :
647 : 39648 : tree *p = &init;
648 : 39648 : init = NULL_TREE;
649 : 96043 : for (; parm; parm = DECL_CHAIN (parm))
650 : : {
651 : 56395 : tree exp = forward_parm (parm);
652 : 56395 : *p = build_tree_list (NULL_TREE, exp);
653 : 56395 : p = &TREE_CHAIN (*p);
654 : : }
655 : : }
656 : : else
657 : : {
658 : 112432 : init = build_base_path (PLUS_EXPR, parm, binfo, 1,
659 : : tf_warning_or_error);
660 : 112432 : if (move_p)
661 : 81379 : init = move (init);
662 : 112432 : init = build_tree_list (NULL_TREE, init);
663 : : }
664 : 152080 : 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 : 226805 : do_build_copy_constructor (tree fndecl)
672 : : {
673 : 226805 : tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
674 : 453610 : bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
675 : 226805 : bool trivial = trivial_fn_p (fndecl);
676 : 453610 : tree inh = DECL_INHERITED_CTOR (fndecl);
677 : :
678 : 226805 : if (!inh)
679 : 187172 : parm = convert_from_reference (parm);
680 : :
681 : 226805 : 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 : 226752 : tree member_init_list = NULL_TREE;
711 : 226752 : int i;
712 : 226752 : tree binfo, base_binfo;
713 : 226752 : 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 : 226752 : for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
721 : 226845 : 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 : 378882 : for (binfo = TYPE_BINFO (current_class_type), i = 0;
728 : 378882 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
729 : : {
730 : 152130 : if (BINFO_VIRTUAL_P (base_binfo))
731 : 74 : continue;
732 : 152056 : member_init_list = add_one_base_init (base_binfo, parm, move_p,
733 : : inh, member_init_list);
734 : : }
735 : :
736 : 226752 : if (!inh)
737 : : {
738 : 187119 : int cvquals = cp_type_quals (TREE_TYPE (parm));
739 : :
740 : 187119 : for (tree fields = TYPE_FIELDS (current_class_type);
741 : 11489568 : fields; fields = DECL_CHAIN (fields))
742 : : {
743 : 11302449 : tree field = fields;
744 : 11302449 : tree expr_type;
745 : :
746 : 11302449 : if (TREE_CODE (field) != FIELD_DECL)
747 : 11022308 : continue;
748 : :
749 : 280141 : expr_type = TREE_TYPE (field);
750 : 280141 : if (DECL_NAME (field))
751 : : {
752 : 167839 : if (VFIELD_NAME_P (DECL_NAME (field)))
753 : 437 : continue;
754 : : }
755 : 112302 : 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 : 112290 : 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 : 167414 : if (!TYPE_REF_P (expr_type))
768 : : {
769 : 166304 : int quals = cvquals;
770 : :
771 : 166304 : if (DECL_MUTABLE_P (field))
772 : 9 : quals &= ~TYPE_QUAL_CONST;
773 : 166304 : quals |= cp_type_quals (expr_type);
774 : 166304 : expr_type = cp_build_qualified_type (expr_type, quals);
775 : : }
776 : :
777 : 167414 : tree init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
778 : 92436 : if (move_p && !TYPE_REF_P (expr_type)
779 : : /* 'move' breaks bit-fields, and has no effect for scalars. */
780 : 259291 : && !scalarish_type_p (expr_type))
781 : 80745 : init = move (init);
782 : 167414 : init = build_tree_list (NULL_TREE, init);
783 : :
784 : 167414 : member_init_list = tree_cons (field, init, member_init_list);
785 : : }
786 : : }
787 : :
788 : 226752 : finish_mem_initializers (member_init_list);
789 : : }
790 : 226805 : }
791 : :
792 : : static void
793 : 42141 : do_build_copy_assign (tree fndecl)
794 : : {
795 : 42141 : tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl));
796 : 42141 : tree compound_stmt;
797 : 42141 : bool move_p = move_fn_p (fndecl);
798 : 42141 : bool trivial = trivial_fn_p (fndecl);
799 : 42141 : int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
800 : :
801 : 42141 : compound_stmt = begin_compound_stmt (0);
802 : 42141 : 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 : 84282 : tree const class_ref = DECL_XOBJ_MEMBER_FUNCTION_P (fndecl)
808 : 84282 : ? cp_build_fold_indirect_ref (DECL_ARGUMENTS (fndecl)) : current_class_ref;
809 : :
810 : 42141 : if (trivial
811 : 42141 : && 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 : 42138 : 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 : 42122 : tree fields;
822 : 42122 : int cvquals = cp_type_quals (TREE_TYPE (parm));
823 : 42122 : int i;
824 : 42122 : tree binfo, base_binfo;
825 : :
826 : : /* Assign to each of the direct base classes. */
827 : 42122 : for (binfo = TYPE_BINFO (current_class_type), i = 0;
828 : 62271 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
829 : : {
830 : 20149 : tree converted_parm;
831 : :
832 : : /* We must convert PARM directly to the base class
833 : : explicitly since the base class may be ambiguous. */
834 : 20149 : converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
835 : : tf_warning_or_error);
836 : 20149 : if (move_p)
837 : 19545 : converted_parm = move (converted_parm);
838 : : /* Call the base class assignment operator. */
839 : 20149 : releasing_vec parmvec (make_tree_vector_single (converted_parm));
840 : 20149 : finish_expr_stmt
841 : 20149 : (build_special_member_call (class_ref,
842 : : assign_op_identifier,
843 : : &parmvec,
844 : : base_binfo,
845 : : flags,
846 : : tf_warning_or_error));
847 : 20149 : }
848 : :
849 : : /* Assign to each of the non-static data members. */
850 : 42122 : for (fields = TYPE_FIELDS (current_class_type);
851 : 1517034 : fields;
852 : 1474912 : fields = DECL_CHAIN (fields))
853 : : {
854 : 1474912 : tree comp = class_ref;
855 : 1474912 : tree init = parm;
856 : 1474912 : tree field = fields;
857 : 1474912 : tree expr_type;
858 : 1474912 : int quals;
859 : :
860 : 1474912 : if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
861 : 1451103 : continue;
862 : :
863 : 23809 : expr_type = TREE_TYPE (field);
864 : :
865 : 23809 : 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 : 23807 : 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 : 23806 : if (DECL_NAME (field))
879 : : {
880 : 23803 : 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 : 23806 : comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
892 : :
893 : : /* Compute the type of init->field */
894 : 23806 : quals = cvquals;
895 : 23806 : if (DECL_MUTABLE_P (field))
896 : 3 : quals &= ~TYPE_QUAL_CONST;
897 : 23806 : expr_type = cp_build_qualified_type (expr_type, quals);
898 : :
899 : 23806 : init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
900 : 22010 : if (move_p && !TYPE_REF_P (expr_type)
901 : : /* 'move' breaks bit-fields, and has no effect for scalars. */
902 : 45816 : && !scalarish_type_p (expr_type))
903 : 21829 : init = move (init);
904 : :
905 : 23806 : if (DECL_NAME (field))
906 : 23803 : 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 : 23806 : finish_expr_stmt (init);
911 : : }
912 : : }
913 : 42141 : finish_return_stmt (class_ref);
914 : 42141 : finish_compound_stmt (compound_stmt);
915 : 42141 : }
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 : 309513 : lookup_comparison_result (tree type, const char *name_str,
951 : : tsubst_flags_t complain = tf_warning_or_error)
952 : : {
953 : 309513 : tree name = get_identifier (name_str);
954 : 309513 : tree decl = lookup_qualified_name (type, name);
955 : 309513 : 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 : 180100 : lookup_comparison_category (comp_cat_tag tag,
975 : : tsubst_flags_t complain = tf_warning_or_error)
976 : : {
977 : 180100 : if (tree cached = comp_cat_cache[tag])
978 : : return cached;
979 : :
980 : 21335 : tree name = get_identifier (comp_cat_info[tag].name);
981 : 21335 : tree decl = lookup_qualified_name (std_node, name);
982 : 21335 : 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 : 21317 : tree type = TREE_TYPE (decl);
998 : 85370 : for (int i = 0; i < 4; ++i)
999 : : {
1000 : 85259 : const char *p = comp_cat_info[tag].members[i];
1001 : 85259 : if (!p) break;
1002 : 64056 : if (lookup_comparison_result (type, p, complain)
1003 : 64056 : == error_mark_node)
1004 : : return error_mark_node;
1005 : : }
1006 : 21314 : return comp_cat_cache[tag] = type;
1007 : : }
1008 : :
1009 : : /* Wrapper that takes the tag rather than the type. */
1010 : :
1011 : : static tree
1012 : 288 : lookup_comparison_result (comp_cat_tag tag, const char *name_str,
1013 : : tsubst_flags_t complain = tf_warning_or_error)
1014 : : {
1015 : 288 : tree type = lookup_comparison_category (tag, complain);
1016 : 288 : 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 : 245169 : lookup_comparison_result (comp_cat_tag tag, tree type, int idx)
1023 : : {
1024 : 245169 : const char *name_str = comp_cat_info[tag].members[idx];
1025 : 245169 : if (!name_str)
1026 : : return NULL_TREE;
1027 : 245169 : return lookup_comparison_result (type, name_str);
1028 : : }
1029 : :
1030 : : /* Does TYPE correspond to TAG? */
1031 : :
1032 : : static bool
1033 : 243705 : is_cat (tree type, comp_cat_tag tag)
1034 : : {
1035 : 243705 : tree name = TYPE_LINKAGE_IDENTIFIER (type);
1036 : 243705 : 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 : 81844 : cat_tag_for (tree type)
1043 : : {
1044 : 81844 : if (!CLASS_TYPE_P (type) || !decl_in_std_namespace_p (TYPE_MAIN_DECL (type)))
1045 : 48 : return cc_last;
1046 : 243705 : for (int i = 0; i < cc_last; ++i)
1047 : : {
1048 : 243705 : comp_cat_tag tag = (comp_cat_tag)i;
1049 : 243705 : 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 : 179780 : spaceship_comp_cat (tree optype)
1060 : : {
1061 : 179780 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (optype) || TYPE_PTROBV_P (optype))
1062 : : return cc_strong_ordering;
1063 : 666 : 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 : 179780 : spaceship_type (tree optype, tsubst_flags_t complain)
1075 : : {
1076 : 179780 : comp_cat_tag tag = spaceship_comp_cat (optype);
1077 : 179780 : 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 : 81469 : genericize_spaceship (location_t loc, tree type, tree op0, tree op1)
1086 : : {
1087 : : /* ??? maybe optimize based on knowledge of representation? */
1088 : 81469 : comp_cat_tag tag = cat_tag_for (type);
1089 : :
1090 : 81469 : 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 : 81469 : else if (tag == cc_last)
1101 : 3 : return error_mark_node;
1102 : :
1103 : 81466 : tree r;
1104 : 81466 : bool scalar = SCALAR_TYPE_P (TREE_TYPE (op0));
1105 : 81409 : if (scalar)
1106 : : {
1107 : 81409 : op0 = save_expr (op0);
1108 : 81409 : op1 = save_expr (op1);
1109 : : }
1110 : :
1111 : 81466 : tree gt = lookup_comparison_result (tag, type, 1);
1112 : :
1113 : 81466 : int flags = LOOKUP_NORMAL;
1114 : 81466 : tsubst_flags_t complain = tf_none;
1115 : 81466 : tree comp;
1116 : :
1117 : 81466 : if (tag == cc_partial_ordering)
1118 : : {
1119 : : /* op0 == op1 ? equivalent : op0 < op1 ? less :
1120 : : op1 < op0 ? greater : unordered */
1121 : 771 : tree uo = lookup_comparison_result (tag, type, 3);
1122 : 771 : 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 : 759 : comp = fold_build2 (LT_EXPR, boolean_type_node, op1, op0);
1127 : 759 : 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 : 81466 : tree lt = lookup_comparison_result (tag, type, 2);
1140 : 81466 : if (scalar)
1141 : : {
1142 : 81409 : comp = fold_build2 (LT_EXPR, boolean_type_node, op0, op1);
1143 : 81409 : 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 : 81466 : tree eq = lookup_comparison_result (tag, type, 0);
1152 : 81466 : if (scalar)
1153 : : {
1154 : 81409 : comp = fold_build2 (EQ_EXPR, boolean_type_node, op0, op1);
1155 : 81409 : 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 : 56085 : early_check_defaulted_comparison (tree fn)
1171 : : {
1172 : 56085 : location_t loc = DECL_SOURCE_LOCATION (fn);
1173 : 56085 : tree ctx;
1174 : 56085 : if (DECL_CLASS_SCOPE_P (fn))
1175 : 7687 : ctx = DECL_CONTEXT (fn);
1176 : : else
1177 : 96796 : ctx = DECL_FRIEND_CONTEXT (fn);
1178 : 56085 : bool ok = true;
1179 : :
1180 : 56085 : 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 : 56081 : if (!DECL_OVERLOADED_OPERATOR_IS (fn, SPACESHIP_EXPR)
1188 : 56081 : && !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 : 56081 : ok = false;
1200 : : }
1201 : :
1202 : 56081 : bool mem = DECL_IOBJ_MEMBER_FUNCTION_P (fn);
1203 : 56081 : 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 : 56081 : 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 : 56081 : tree parmnode = FUNCTION_FIRST_USER_PARMTYPE (fn);
1214 : 56081 : bool saw_byval = false;
1215 : 56081 : bool saw_byref = mem;
1216 : 56081 : bool saw_bad = false;
1217 : 160564 : for (; parmnode != void_list_node; parmnode = TREE_CHAIN (parmnode))
1218 : : {
1219 : 104483 : tree parmtype = TREE_VALUE (parmnode);
1220 : 104483 : if (CLASS_TYPE_P (parmtype))
1221 : : saw_byval = true;
1222 : 82375 : else if (TREE_CODE (parmtype) == REFERENCE_TYPE
1223 : 82372 : && !TYPE_REF_IS_RVALUE (parmtype)
1224 : 164741 : && TYPE_QUALS (TREE_TYPE (parmtype)) == TYPE_QUAL_CONST)
1225 : : {
1226 : 82366 : saw_byref = true;
1227 : 82366 : parmtype = TREE_TYPE (parmtype);
1228 : : }
1229 : : else
1230 : : saw_bad = true;
1231 : :
1232 : 104483 : 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 : 104462 : else if (!same_type_ignoring_top_level_qualifiers_p (parmtype, ctx))
1245 : 9 : saw_bad = true;
1246 : : }
1247 : :
1248 : 56081 : 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 : 56081 : DECL_MAYBE_DELETED (fn) = ok;
1264 : :
1265 : 56081 : 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 : 124 : common_comparison_type (vec<tree> &comps)
1274 : : {
1275 : 124 : tree seen[cc_last] = {};
1276 : :
1277 : 231 : for (unsigned i = 0; i < comps.length(); ++i)
1278 : : {
1279 : 107 : tree comp = comps[i];
1280 : 107 : if (TREE_CODE (comp) == TREE_LIST)
1281 : 3 : comp = TREE_VALUE (comp);
1282 : 107 : tree ctype = TREE_TYPE (comp);
1283 : 107 : comp_cat_tag tag = cat_tag_for (ctype);
1284 : : /* build_comparison_op already checked this. */
1285 : 107 : gcc_checking_assert (tag < cc_last);
1286 : 107 : seen[tag] = ctype;
1287 : : }
1288 : :
1289 : : /* Otherwise, if at least one T i is std::partial_ordering, U is
1290 : : std::partial_ordering. */
1291 : 124 : 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 : 106 : if (tree t = seen[cc_weak_ordering]) return t;
1296 : :
1297 : : /* Otherwise, U is std::strong_ordering. */
1298 : 106 : if (tree t = seen[cc_strong_ordering]) return t;
1299 : 32 : 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 : 17788 : comp_info (tree fndecl, tsubst_flags_t complain)
1317 : 17788 : : fndecl (fndecl), complain (complain)
1318 : : {
1319 : 17788 : loc = DECL_SOURCE_LOCATION (fndecl);
1320 : :
1321 : 17788 : first_time = DECL_MAYBE_DELETED (fndecl);
1322 : 17788 : DECL_MAYBE_DELETED (fndecl) = false;
1323 : :
1324 : : /* Do we want to try to set constexpr? */
1325 : 17788 : was_constexp = DECL_DECLARED_CONSTEXPR_P (fndecl);
1326 : 17788 : constexp = first_time;
1327 : 17788 : if (constexp)
1328 : : /* Set this for var_in_constexpr_fn. */
1329 : 17689 : DECL_DECLARED_CONSTEXPR_P (fndecl) = true;
1330 : :
1331 : : /* Do we want to try to set noexcept? */
1332 : 17788 : noex = first_time;
1333 : 17788 : if (noex)
1334 : : {
1335 : 17689 : tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fndecl));
1336 : 23681 : if (raises && !UNEVALUATED_NOEXCEPT_SPEC_P (raises))
1337 : : /* There was an explicit exception-specification. */
1338 : 5992 : noex = false;
1339 : : }
1340 : 17788 : }
1341 : :
1342 : : /* EXPR is an expression built as part of the function body.
1343 : : Adjust the properties appropriately. */
1344 : 26544 : void check (tree expr)
1345 : : {
1346 : 26544 : if (expr == error_mark_node)
1347 : 0 : DECL_DELETED_FN (fndecl) = true;
1348 : 62 : if ((constexp || was_constexp)
1349 : 26571 : && !potential_rvalue_constant_expression (expr))
1350 : : {
1351 : 307 : if (was_constexp)
1352 : 12 : require_potential_rvalue_constant_expression_fncheck (expr);
1353 : : else
1354 : 295 : constexp = false;
1355 : : }
1356 : 26544 : if (noex && !expr_noexcept_p (expr, tf_none))
1357 : 54 : noex = false;
1358 : 26544 : }
1359 : :
1360 : 17788 : ~comp_info ()
1361 : : {
1362 : 17788 : if (first_time)
1363 : : {
1364 : 17689 : DECL_DECLARED_CONSTEXPR_P (fndecl) = constexp || was_constexp;
1365 : 17689 : tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fndecl));
1366 : 23681 : if (!raises || UNEVALUATED_NOEXCEPT_SPEC_P (raises))
1367 : : {
1368 : 11697 : raises = noex ? noexcept_true_spec : noexcept_false_spec;
1369 : 11697 : TREE_TYPE (fndecl) = build_exception_variant (TREE_TYPE (fndecl),
1370 : : raises);
1371 : : }
1372 : : }
1373 : 17788 : }
1374 : : };
1375 : :
1376 : : /* Subroutine of build_comparison_op, to compare a single subobject. */
1377 : :
1378 : : static tree
1379 : 26454 : do_one_comp (location_t loc, const comp_info &info, tree sub, tree lhs, tree rhs)
1380 : : {
1381 : 26454 : const tree_code code = info.code;
1382 : 26454 : const tree fndecl = info.fndecl;
1383 : 26454 : const comp_cat_tag retcat = info.retcat;
1384 : 26454 : const tsubst_flags_t complain = info.complain;
1385 : :
1386 : 26454 : tree overload = NULL_TREE;
1387 : 26454 : 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 : 26454 : bool tentative = retcat != cc_last;
1391 : 52775 : tree comp = build_new_op (loc, code, flags, lhs, rhs,
1392 : : NULL_TREE, NULL_TREE, &overload,
1393 : : tentative ? tf_none : complain);
1394 : :
1395 : 26454 : if (code != SPACESHIP_EXPR)
1396 : : return comp;
1397 : :
1398 : 324 : tree rettype = TREE_TYPE (TREE_TYPE (fndecl));
1399 : :
1400 : 324 : 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 : 246 : if (FNDECL_USED_AUTO (fndecl)
1428 : 246 : && 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 : 240 : else if (!FNDECL_USED_AUTO (fndecl)
1439 : 240 : && !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 : 17788 : build_comparison_op (tree fndecl, bool defining, tsubst_flags_t complain)
1460 : : {
1461 : 17788 : comp_info info (fndecl, complain);
1462 : :
1463 : 17788 : if (!defining && !(complain & tf_error) && !DECL_MAYBE_DELETED (fndecl))
1464 : : return;
1465 : :
1466 : 17779 : int flags = LOOKUP_NORMAL;
1467 : 17779 : const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (DECL_NAME (fndecl));
1468 : 17779 : tree_code code = info.code = op->tree_code;
1469 : :
1470 : 17779 : tree lhs = DECL_ARGUMENTS (fndecl);
1471 : 17779 : tree rhs = DECL_CHAIN (lhs);
1472 : 17779 : if (is_this_parameter (lhs))
1473 : 9174 : lhs = cp_build_fold_indirect_ref (lhs);
1474 : : else
1475 : 8605 : lhs = convert_from_reference (lhs);
1476 : 17779 : rhs = convert_from_reference (rhs);
1477 : 17779 : tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (lhs));
1478 : 17779 : gcc_assert (!defining || COMPLETE_TYPE_P (ctype));
1479 : :
1480 : 17779 : 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 : 17779 : if (TREE_CODE (ctype) == UNION_TYPE
1485 : 17779 : && 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 : 17773 : tree compound_stmt = NULL_TREE;
1494 : 17773 : if (defining)
1495 : 17692 : compound_stmt = begin_compound_stmt (0);
1496 : : else
1497 : 81 : ++cp_unevaluated_operand;
1498 : :
1499 : 17773 : tree rettype = TREE_TYPE (TREE_TYPE (fndecl));
1500 : 17773 : 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 : 17773 : if (code == EQ_EXPR || code == SPACESHIP_EXPR)
1507 : : {
1508 : 17698 : comp_cat_tag &retcat = (info.retcat = cc_last);
1509 : 17986 : if (code == SPACESHIP_EXPR && !FNDECL_USED_AUTO (fndecl))
1510 : 143 : retcat = cat_tag_for (rettype);
1511 : :
1512 : 17698 : bool bad = false;
1513 : 17698 : 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 : 17991 : for (tree base_binfo : BINFO_BASE_BINFOS (TYPE_BINFO (ctype)))
1519 : : {
1520 : 293 : tree lhs_base
1521 : 293 : = build_base_path (PLUS_EXPR, lhs, base_binfo, 0, complain);
1522 : 293 : tree rhs_base
1523 : 293 : = build_base_path (PLUS_EXPR, rhs, base_binfo, 0, complain);
1524 : :
1525 : 293 : location_t loc = DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (ctype));
1526 : 293 : tree comp = do_one_comp (loc, info, BINFO_TYPE (base_binfo),
1527 : 293 : lhs_base, rhs_base);
1528 : 293 : if (comp == error_mark_node)
1529 : : {
1530 : 12 : bad = true;
1531 : 12 : continue;
1532 : : }
1533 : :
1534 : 281 : comps.safe_push (comp);
1535 : : }
1536 : :
1537 : : /* Now compare the field subobjects. */
1538 : 17698 : for (tree field = next_aggregate_field (TYPE_FIELDS (ctype));
1539 : 44176 : field;
1540 : 26478 : field = next_aggregate_field (DECL_CHAIN (field)))
1541 : : {
1542 : 52956 : if (DECL_VIRTUAL_P (field) || DECL_FIELD_IS_BASE (field))
1543 : : /* We ignore the vptr, and we already handled bases. */
1544 : 428 : continue;
1545 : :
1546 : 26185 : tree expr_type = TREE_TYPE (field);
1547 : :
1548 : 26185 : 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 : 26185 : 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 : 26185 : && 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 : 26173 : tree lhs_mem = build3_loc (field_loc, COMPONENT_REF, expr_type, lhs,
1572 : : field, NULL_TREE);
1573 : 26173 : tree rhs_mem = build3_loc (field_loc, COMPONENT_REF, expr_type, rhs,
1574 : : field, NULL_TREE);
1575 : 26173 : tree loop_indexes = NULL_TREE;
1576 : 52352 : 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 : 26173 : if (TREE_CODE (expr_type) == ARRAY_TYPE)
1610 : 12 : continue;
1611 : :
1612 : 26161 : tree comp = do_one_comp (field_loc, info, field, lhs_mem, rhs_mem);
1613 : 26161 : 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 : 26050 : if (loop_indexes)
1627 : : {
1628 : 3 : TREE_VALUE (loop_indexes) = comp;
1629 : 3 : comp = loop_indexes;
1630 : : }
1631 : 26050 : comps.safe_push (comp);
1632 : : }
1633 : 17698 : if (code == SPACESHIP_EXPR && is_auto (rettype))
1634 : : {
1635 : 124 : rettype = common_comparison_type (comps);
1636 : 124 : apply_deduced_return_type (fndecl, rettype);
1637 : : }
1638 : 17698 : tree retvaleq;
1639 : 17698 : if (code == EQ_EXPR)
1640 : 17410 : retvaleq = boolean_true_node;
1641 : : else
1642 : : {
1643 : 288 : tree seql = lookup_comparison_result (cc_strong_ordering,
1644 : : "equal", complain);
1645 : 288 : retvaleq = build_static_cast (input_location, rettype, seql,
1646 : : complain);
1647 : 288 : if (retvaleq == error_mark_node)
1648 : : bad = true;
1649 : : }
1650 : 17659 : if (bad)
1651 : : {
1652 : 153 : DECL_DELETED_FN (fndecl) = true;
1653 : 153 : goto out;
1654 : : }
1655 : 43840 : for (unsigned i = 0; i < comps.length(); ++i)
1656 : : {
1657 : 26295 : tree comp = comps[i];
1658 : 26295 : tree eq, retval = NULL_TREE, if_ = NULL_TREE;
1659 : 26295 : tree loop_indexes = NULL_TREE;
1660 : 26295 : if (defining)
1661 : : {
1662 : 26289 : 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 : 26289 : 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 : 26295 : if (code == EQ_EXPR)
1695 : : {
1696 : : /* if (x==y); else return false; */
1697 : 26121 : eq = comp;
1698 : 26121 : retval = boolean_false_node;
1699 : : }
1700 : : else
1701 : : {
1702 : : /* if (auto v = x<=>y, v == 0); else return v; */
1703 : 174 : if (TREE_CODE (comp) == SPACESHIP_EXPR)
1704 : 0 : TREE_TYPE (comp) = rettype;
1705 : : else
1706 : 174 : comp = build_static_cast (input_location, rettype, comp,
1707 : : complain);
1708 : 174 : info.check (comp);
1709 : 174 : if (defining)
1710 : : {
1711 : 174 : tree var = create_temporary_var (rettype);
1712 : 174 : DECL_NAME (var) = get_identifier ("retval");
1713 : 174 : pushdecl (var);
1714 : 174 : cp_finish_decl (var, comp, false, NULL_TREE, flags);
1715 : 174 : comp = retval = var;
1716 : : }
1717 : 174 : eq = build_new_op (info.loc, EQ_EXPR, flags, comp,
1718 : : integer_zero_node, NULL_TREE, NULL_TREE,
1719 : : NULL, complain);
1720 : : }
1721 : 26295 : tree ceq = contextual_conv_bool (eq, complain);
1722 : 26295 : info.check (ceq);
1723 : 26295 : if (defining)
1724 : : {
1725 : 26289 : finish_if_stmt_cond (ceq, if_);
1726 : 26289 : finish_then_clause (if_);
1727 : 26289 : begin_else_clause (if_);
1728 : 26289 : finish_return_stmt (retval);
1729 : 26289 : finish_else_clause (if_);
1730 : 26289 : finish_if_stmt (if_);
1731 : 26292 : 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 : 17545 : if (defining)
1737 : 17539 : finish_return_stmt (retvaleq);
1738 : 17698 : }
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 : 17770 : out:
1763 : 17770 : if (defining)
1764 : 17692 : finish_compound_stmt (compound_stmt);
1765 : : else
1766 : 81 : --cp_unevaluated_operand;
1767 : 17788 : }
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 : 2272135 : decl_remember_implicit_trigger_p (tree decl)
1775 : : {
1776 : 2272135 : if (!DECL_ARTIFICIAL (decl))
1777 : : return false;
1778 : 1057289 : 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 : 1057289 : return (sfk != sfk_inheriting_constructor
1782 : 1057289 : && sfk != sfk_comparison);
1783 : : }
1784 : :
1785 : : /* Synthesize FNDECL, a non-static member function. */
1786 : :
1787 : : void
1788 : 1145215 : synthesize_method (tree fndecl)
1789 : : {
1790 : 1145215 : bool need_body = true;
1791 : 1145215 : tree stmt;
1792 : 1145215 : location_t save_input_location = input_location;
1793 : 1145215 : int error_count = errorcount;
1794 : 1145215 : int warning_count = warningcount + werrorcount;
1795 : 1145215 : special_function_kind sfk = special_function_p (fndecl);
1796 : 1145215 : 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 : 1145215 : if (decl_remember_implicit_trigger_p (fndecl))
1801 : 977980 : DECL_SOURCE_LOCATION (fndecl)
1802 : 488990 : = 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 : 1145215 : if (DECL_CLONED_FUNCTION_P (fndecl))
1808 : 1084924 : fndecl = DECL_CLONED_FUNCTION (fndecl);
1809 : :
1810 : : /* We may be in the middle of deferred access check. Disable
1811 : : it now. */
1812 : 1145215 : push_deferring_access_checks (dk_no_deferred);
1813 : :
1814 : 1145215 : bool push_to_top = maybe_push_to_top_level (fndecl);
1815 : :
1816 : 1145215 : input_location = DECL_SOURCE_LOCATION (fndecl);
1817 : :
1818 : 1145215 : start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
1819 : 1145215 : stmt = begin_function_body ();
1820 : :
1821 : 1145215 : if (DECL_ASSIGNMENT_OPERATOR_P (fndecl)
1822 : 1145215 : && DECL_OVERLOADED_OPERATOR_IS (fndecl, NOP_EXPR))
1823 : : {
1824 : 42141 : do_build_copy_assign (fndecl);
1825 : 42141 : need_body = false;
1826 : : }
1827 : 2206148 : else if (DECL_CONSTRUCTOR_P (fndecl))
1828 : : {
1829 : 651588 : tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
1830 : 651588 : if (arg_chain != void_list_node)
1831 : 226805 : do_build_copy_constructor (fndecl);
1832 : : else
1833 : 424783 : finish_mem_initializers (NULL_TREE);
1834 : : }
1835 : 451486 : else if (sfk == sfk_comparison)
1836 : : {
1837 : : /* Pass tf_none so the function is just deleted if there's a problem. */
1838 : 17695 : build_comparison_op (fndecl, true, tf_none);
1839 : 17695 : 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 : 711424 : if (need_body)
1845 : : {
1846 : 1085379 : tree compound_stmt;
1847 : 1085379 : compound_stmt = begin_compound_stmt (BCS_FN_BODY);
1848 : 1085379 : finish_compound_stmt (compound_stmt);
1849 : : }
1850 : :
1851 : 1145215 : finish_function_body (stmt);
1852 : 1145215 : finish_function (/*inline_p=*/false);
1853 : :
1854 : 1145215 : if (!DECL_DELETED_FN (fndecl))
1855 : 1145128 : expand_or_defer_fn (fndecl);
1856 : :
1857 : 1145215 : input_location = save_input_location;
1858 : :
1859 : 1145215 : maybe_pop_from_top_level (push_to_top);
1860 : :
1861 : 1145215 : pop_deferring_access_checks ();
1862 : :
1863 : 1145215 : 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 : 1145215 : }
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 : 8889 : maybe_synthesize_method (tree fndecl)
1875 : : {
1876 : 8889 : if (special_function_p (fndecl) == sfk_comparison)
1877 : : {
1878 : 8889 : tree lhs = DECL_ARGUMENTS (fndecl);
1879 : 8889 : if (is_this_parameter (lhs))
1880 : 293 : lhs = cp_build_fold_indirect_ref (lhs);
1881 : : else
1882 : 8596 : lhs = convert_from_reference (lhs);
1883 : 8889 : tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (lhs));
1884 : 8889 : 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 : 8880 : 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 : 6925503 : build_stub_type (tree type, int quals, bool rvalue)
1900 : : {
1901 : 6925503 : tree argtype = cp_build_qualified_type (type, quals);
1902 : 6925503 : 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 : 36259395 : build_stub_object (tree reftype)
1910 : : {
1911 : 36259395 : if (!TYPE_REF_P (reftype))
1912 : 2369785 : reftype = cp_build_reference_type (reftype, /*rval*/true);
1913 : 36259395 : tree stub = build1 (CONVERT_EXPR, reftype, integer_one_node);
1914 : 36259395 : return convert_from_reference (stub);
1915 : : }
1916 : :
1917 : : /* True iff EXPR is the result of build_stub_object. */
1918 : :
1919 : : bool
1920 : 659 : is_stub_object (tree expr)
1921 : : {
1922 : 659 : 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 : 3060826 : 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 : 3060826 : if (FUNC_OR_METHOD_TYPE_P (type)
1943 : 3060826 : && (type_memfn_quals (type) != TYPE_UNQUALIFIED
1944 : 216 : || type_memfn_rqual (type) != REF_QUAL_NONE))
1945 : 14 : return error_mark_node;
1946 : :
1947 : 3060812 : 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 : 88754 : build_invoke (tree fn_type, const_tree arg_types, tsubst_flags_t complain)
1955 : : {
1956 : 88754 : if (error_operand_p (fn_type) || error_operand_p (arg_types))
1957 : 0 : return error_mark_node;
1958 : :
1959 : 88754 : gcc_assert (TYPE_P (fn_type));
1960 : 88754 : gcc_assert (TREE_CODE (arg_types) == TREE_VEC);
1961 : :
1962 : : /* Access check is required to determine if the given is invocable. */
1963 : 88754 : deferring_access_check_sentinel acs (dk_no_deferred);
1964 : :
1965 : : /* INVOKE is an unevaluated context. */
1966 : 88754 : cp_unevaluated cp_uneval_guard;
1967 : :
1968 : 88754 : bool is_ptrdatamem;
1969 : 88754 : bool is_ptrmemfunc;
1970 : 88754 : if (TREE_CODE (fn_type) == REFERENCE_TYPE)
1971 : : {
1972 : 63772 : tree non_ref_fn_type = TREE_TYPE (fn_type);
1973 : 63772 : is_ptrdatamem = TYPE_PTRDATAMEM_P (non_ref_fn_type);
1974 : 63772 : is_ptrmemfunc = TYPE_PTRMEMFUNC_P (non_ref_fn_type);
1975 : :
1976 : : /* Dereference fn_type if it is a pointer to member. */
1977 : 63640 : if (is_ptrdatamem || is_ptrmemfunc)
1978 : : fn_type = non_ref_fn_type;
1979 : : }
1980 : : else
1981 : : {
1982 : 24982 : is_ptrdatamem = TYPE_PTRDATAMEM_P (fn_type);
1983 : 24982 : is_ptrmemfunc = TYPE_PTRMEMFUNC_P (fn_type);
1984 : : }
1985 : :
1986 : 26274 : if (is_ptrdatamem && TREE_VEC_LENGTH (arg_types) != 1)
1987 : : {
1988 : 43 : if (complain & tf_error)
1989 : 0 : error ("pointer to data member type %qT can only be invoked with "
1990 : : "one argument", fn_type);
1991 : 43 : return error_mark_node;
1992 : : }
1993 : 108347 : if (is_ptrmemfunc && TREE_VEC_LENGTH (arg_types) == 0)
1994 : : {
1995 : 29 : 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 : 29 : return error_mark_node;
1999 : : }
2000 : :
2001 : : /* Construct an expression of a pointer to member. */
2002 : 88682 : tree ptrmem_expr;
2003 : 88682 : if (is_ptrdatamem || is_ptrmemfunc)
2004 : : {
2005 : 20288 : tree datum_type = TREE_VEC_ELT (arg_types, 0);
2006 : 20288 : tree non_ref_datum_type = datum_type;
2007 : 20288 : if (TYPE_REF_P (datum_type))
2008 : 708 : non_ref_datum_type = TREE_TYPE (datum_type);
2009 : :
2010 : : /* datum must be a class type or a pointer to a class type. */
2011 : 808 : if (!CLASS_TYPE_P (non_ref_datum_type)
2012 : 20288 : && !(POINTER_TYPE_P (non_ref_datum_type)
2013 : 19380 : && CLASS_TYPE_P (TREE_TYPE (non_ref_datum_type))))
2014 : : {
2015 : 106 : 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 : 106 : return error_mark_node;
2019 : : }
2020 : :
2021 : : /* 1.1 & 1.4. */
2022 : 20182 : tree ptrmem_class_type = TYPE_PTRMEM_CLASS_TYPE (fn_type);
2023 : 20182 : const bool ptrmem_is_same_or_base_of_datum =
2024 : 20182 : (same_type_ignoring_top_level_qualifiers_p (ptrmem_class_type,
2025 : : non_ref_datum_type)
2026 : 20182 : || (NON_UNION_CLASS_TYPE_P (ptrmem_class_type)
2027 : 19480 : && NON_UNION_CLASS_TYPE_P (non_ref_datum_type)
2028 : 106 : && DERIVED_FROM_P (ptrmem_class_type, non_ref_datum_type)));
2029 : :
2030 : 19462 : bool datum_is_refwrap = false;
2031 : 19462 : if (!ptrmem_is_same_or_base_of_datum && CLASS_TYPE_P (non_ref_datum_type))
2032 : : {
2033 : 88 : tree datum_decl = TYPE_NAME (TYPE_MAIN_VARIANT (non_ref_datum_type));
2034 : 88 : if (decl_in_std_namespace_p (datum_decl))
2035 : : {
2036 : 38 : const_tree name = DECL_NAME (datum_decl);
2037 : 38 : 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 : 72 : datum_type =
2042 : 72 : TREE_VEC_ELT (TYPE_TI_ARGS (non_ref_datum_type), 0);
2043 : 36 : datum_is_refwrap = true;
2044 : : }
2045 : : }
2046 : : }
2047 : :
2048 : 20182 : tree datum_expr = build_trait_object (datum_type);
2049 : 20182 : if (!ptrmem_is_same_or_base_of_datum && !datum_is_refwrap)
2050 : : /* 1.3 & 1.6: Try to dereference datum_expr. */
2051 : 19426 : datum_expr = build_x_indirect_ref (UNKNOWN_LOCATION, datum_expr,
2052 : : RO_UNARY_STAR, NULL_TREE, complain);
2053 : :
2054 : 20182 : tree fn_expr = build_trait_object (fn_type);
2055 : 20182 : ptrmem_expr = build_m_component_ref (datum_expr, fn_expr, complain);
2056 : :
2057 : 20182 : if (error_operand_p (ptrmem_expr))
2058 : 53 : return error_mark_node;
2059 : :
2060 : 20129 : 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 : 87908 : releasing_vec args;
2068 : 271529 : for (int i = is_ptrmemfunc ? 1 : 0; i < TREE_VEC_LENGTH (arg_types); ++i)
2069 : : {
2070 : 95713 : tree arg_type = TREE_VEC_ELT (arg_types, i);
2071 : 95713 : tree arg = build_trait_object (arg_type);
2072 : 95713 : vec_safe_push (args, arg);
2073 : : }
2074 : :
2075 : 87908 : tree invoke_expr;
2076 : 87908 : if (is_ptrmemfunc)
2077 : 19514 : invoke_expr = build_offset_ref_call_from_tree (ptrmem_expr, &args,
2078 : : complain);
2079 : : else /* 1.7. */
2080 : 68394 : invoke_expr = finish_call_expr (build_trait_object (fn_type), &args, false,
2081 : : false, complain);
2082 : 87908 : return invoke_expr;
2083 : 88754 : }
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 : 22553287 : locate_fn_flags (tree type, tree name, tree argtype, int flags,
2095 : : tsubst_flags_t complain)
2096 : : {
2097 : 22553287 : tree ob, fn, fns, binfo, rval;
2098 : :
2099 : 22553287 : if (TYPE_P (type))
2100 : 10501097 : binfo = TYPE_BINFO (type);
2101 : : else
2102 : : {
2103 : 12052190 : binfo = type;
2104 : 12052190 : type = BINFO_TYPE (binfo);
2105 : : }
2106 : :
2107 : 22553287 : ob = build_stub_object (cp_build_reference_type (type, false));
2108 : 22553287 : releasing_vec args;
2109 : 22553287 : if (argtype)
2110 : : {
2111 : 8349523 : if (TREE_CODE (argtype) == TREE_LIST)
2112 : : {
2113 : 131984 : for (tree elt = argtype; elt && elt != void_list_node;
2114 : 78982 : elt = TREE_CHAIN (elt))
2115 : : {
2116 : 78982 : tree type = TREE_VALUE (elt);
2117 : 78982 : tree arg = build_stub_object (type);
2118 : 78982 : vec_safe_push (args, arg);
2119 : : }
2120 : : }
2121 : : else
2122 : : {
2123 : 8296521 : tree arg = build_stub_object (argtype);
2124 : 8296521 : args->quick_push (arg);
2125 : : }
2126 : : }
2127 : :
2128 : 22553287 : fns = lookup_fnfields (binfo, name, 0, complain);
2129 : 22553287 : rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
2130 : :
2131 : 22553287 : if (fn && rval == error_mark_node)
2132 : : return rval;
2133 : : else
2134 : 21835729 : return fn;
2135 : 22553287 : }
2136 : :
2137 : : /* Locate the dtor of TYPE. */
2138 : :
2139 : : tree
2140 : 2023 : get_dtor (tree type, tsubst_flags_t complain)
2141 : : {
2142 : 2023 : tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
2143 : : LOOKUP_NORMAL, complain);
2144 : 2023 : if (fn == error_mark_node)
2145 : 12 : return NULL_TREE;
2146 : : return fn;
2147 : : }
2148 : :
2149 : : /* Locate the default ctor of TYPE. */
2150 : :
2151 : : tree
2152 : 18104 : locate_ctor (tree type)
2153 : : {
2154 : 18104 : tree fn;
2155 : :
2156 : 18104 : push_deferring_access_checks (dk_no_check);
2157 : 18104 : fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
2158 : : LOOKUP_SPECULATIVE, tf_none);
2159 : 18104 : pop_deferring_access_checks ();
2160 : 18104 : if (fn == error_mark_node)
2161 : 176 : 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 : 543 : get_copy_ctor (tree type, tsubst_flags_t complain)
2181 : : {
2182 : 543 : int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
2183 : 543 : ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
2184 : 543 : tree argtype = build_stub_type (type, quals, false);
2185 : 543 : tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
2186 : : LOOKUP_NORMAL, complain);
2187 : 543 : 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 : 203939 : check_nontriv (tree *tp, int *, void *)
2213 : : {
2214 : 203939 : tree fn = cp_get_callee (*tp);
2215 : 203939 : if (fn == NULL_TREE)
2216 : : return NULL_TREE;
2217 : :
2218 : 4506 : if (TREE_CODE (fn) == ADDR_EXPR)
2219 : 4492 : fn = TREE_OPERAND (fn, 0);
2220 : :
2221 : 4506 : if (TREE_CODE (fn) != FUNCTION_DECL
2222 : 4506 : || !trivial_fn_p (fn))
2223 : 4506 : return fn;
2224 : : return NULL_TREE;
2225 : : }
2226 : :
2227 : : /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */
2228 : :
2229 : : static tree
2230 : 865956 : assignable_expr (tree to, tree from)
2231 : : {
2232 : 865956 : cp_unevaluated cp_uneval_guard;
2233 : 865956 : to = build_trait_object (to);
2234 : 865956 : from = build_trait_object (from);
2235 : 865956 : tree r = cp_build_modify_expr (input_location, to, NOP_EXPR, from, tf_none);
2236 : 1731912 : return r;
2237 : 865956 : }
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 : 1824534 : constructible_expr (tree to, tree from)
2248 : : {
2249 : 1824534 : tree expr;
2250 : 1824534 : cp_unevaluated cp_uneval_guard;
2251 : 1824534 : const int len = TREE_VEC_LENGTH (from);
2252 : 1824534 : if (CLASS_TYPE_P (to))
2253 : : {
2254 : 1020581 : if (ABSTRACT_CLASS_TYPE_P (to))
2255 : 4552 : return error_mark_node;
2256 : 1020542 : tree ctype = to;
2257 : 1020542 : vec<tree, va_gc> *args = NULL;
2258 : 1020542 : if (!TYPE_REF_P (to))
2259 : 1020542 : to = cp_build_reference_type (to, /*rval*/false);
2260 : 1020542 : tree ob = build_stub_object (to);
2261 : 1020542 : if (len == 0)
2262 : 401253 : expr = build_value_init (ctype, tf_none);
2263 : : else
2264 : : {
2265 : 619289 : vec_alloc (args, len);
2266 : 1242804 : for (tree arg : tree_vec_range (from))
2267 : 623515 : args->quick_push (build_stub_object (arg));
2268 : 619289 : expr = build_special_member_call (ob, complete_ctor_identifier, &args,
2269 : : ctype, LOOKUP_NORMAL, tf_none);
2270 : : }
2271 : 1020542 : if (expr == error_mark_node)
2272 : : return error_mark_node;
2273 : : /* The current state of the standard vis-a-vis LWG 2116 is that
2274 : : is_*constructible involves destruction as well. */
2275 : 1016039 : if (type_build_dtor_call (ctype))
2276 : : {
2277 : 159448 : tree dtor = build_special_member_call (ob, complete_dtor_identifier,
2278 : : NULL, ctype, LOOKUP_NORMAL,
2279 : : tf_none);
2280 : 159448 : if (dtor == error_mark_node)
2281 : : return error_mark_node;
2282 : 159438 : if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
2283 : 149329 : expr = build2 (COMPOUND_EXPR, void_type_node, expr, dtor);
2284 : : }
2285 : : }
2286 : : else
2287 : : {
2288 : 803953 : if (len == 0)
2289 : 178852 : return build_value_init (strip_array_types (to), tf_none);
2290 : 625101 : if (len > 1)
2291 : : {
2292 : 155 : if (cxx_dialect < cxx20)
2293 : : /* Too many initializers. */
2294 : 129 : return error_mark_node;
2295 : :
2296 : : /* In C++20 this is well-formed:
2297 : : using T = int[2];
2298 : : T t(1, 2);
2299 : : which means that std::is_constructible_v<int[2], int, int>
2300 : : should be true. */
2301 : 26 : vec<constructor_elt, va_gc> *v;
2302 : 26 : vec_alloc (v, len);
2303 : 78 : for (tree arg : tree_vec_range (from))
2304 : : {
2305 : 52 : tree stub = build_stub_object (arg);
2306 : 52 : constructor_elt elt = { NULL_TREE, stub };
2307 : 52 : v->quick_push (elt);
2308 : : }
2309 : 26 : from = build_constructor (init_list_type_node, v);
2310 : 26 : CONSTRUCTOR_IS_DIRECT_INIT (from) = true;
2311 : 26 : CONSTRUCTOR_IS_PAREN_INIT (from) = true;
2312 : : }
2313 : : else
2314 : 624946 : from = build_stub_object (TREE_VEC_ELT (from, 0));
2315 : 624972 : expr = perform_direct_initialization_if_possible (to, from,
2316 : : /*cast*/false,
2317 : : tf_none);
2318 : : /* If t(e) didn't work, maybe t{e} will. */
2319 : 624972 : if (expr == NULL_TREE
2320 : 624972 : && len == 1
2321 : 2222 : && cxx_dialect >= cxx20)
2322 : : {
2323 : 1219 : from = build_constructor_single (init_list_type_node, NULL_TREE,
2324 : : from);
2325 : 1219 : CONSTRUCTOR_IS_DIRECT_INIT (from) = true;
2326 : 1219 : CONSTRUCTOR_IS_PAREN_INIT (from) = true;
2327 : 1219 : expr = perform_direct_initialization_if_possible (to, from,
2328 : : /*cast*/false,
2329 : : tf_none);
2330 : : }
2331 : : }
2332 : : return expr;
2333 : 1824534 : }
2334 : :
2335 : : /* Valid if "Either T is a reference type, or T is a complete object type for
2336 : : which the expression declval<U&>().~U() is well-formed when treated as an
2337 : : unevaluated operand ([expr.context]), where U is remove_all_extents_t<T>."
2338 : :
2339 : : For a class U, return the destructor call; otherwise return void_node if
2340 : : valid or error_mark_node if not. */
2341 : :
2342 : : static tree
2343 : 48801 : destructible_expr (tree to)
2344 : : {
2345 : 48801 : cp_unevaluated cp_uneval_guard;
2346 : 48801 : int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
2347 : 48801 : if (TYPE_REF_P (to))
2348 : 57 : return void_node;
2349 : 48744 : if (!COMPLETE_TYPE_P (complete_type (to)))
2350 : 28 : return error_mark_node;
2351 : 48716 : to = strip_array_types (to);
2352 : 48716 : if (CLASS_TYPE_P (to))
2353 : : {
2354 : 26501 : to = build_trait_object (to);
2355 : 26501 : return build_delete (input_location, TREE_TYPE (to), to,
2356 : 26501 : sfk_complete_destructor, flags, 0, tf_none);
2357 : : }
2358 : : /* [expr.prim.id.dtor] If the id-expression names a pseudo-destructor, T
2359 : : shall be a scalar type.... */
2360 : 22215 : else if (scalarish_type_p (to))
2361 : 22193 : return void_node;
2362 : : else
2363 : 22 : return error_mark_node;
2364 : 48801 : }
2365 : :
2366 : : /* Returns a tree iff TO is assignable (if CODE is MODIFY_EXPR) or
2367 : : constructible (otherwise) from FROM, which is a single type for
2368 : : assignment or a list of types for construction. */
2369 : :
2370 : : static tree
2371 : 2739681 : is_xible_helper (enum tree_code code, tree to, tree from, bool trivial)
2372 : : {
2373 : 2739681 : to = complete_type (to);
2374 : 2739681 : deferring_access_check_sentinel acs (dk_no_deferred);
2375 : 2739681 : if (VOID_TYPE_P (to)
2376 : 2739681 : || (from && FUNC_OR_METHOD_TYPE_P (from)
2377 : 33 : && (TYPE_READONLY (from) || FUNCTION_REF_QUALIFIED (from))))
2378 : 160 : return error_mark_node;
2379 : 2739521 : tree expr;
2380 : 2739521 : if (code == MODIFY_EXPR)
2381 : 865956 : expr = assignable_expr (to, from);
2382 : 1873565 : else if (code == BIT_NOT_EXPR)
2383 : 48801 : expr = destructible_expr (to);
2384 : 55617 : else if (trivial && TREE_VEC_LENGTH (from) > 1
2385 : 1824778 : && cxx_dialect < cxx20)
2386 : 6 : return error_mark_node; // only 0- and 1-argument ctors can be trivial
2387 : : // before C++20 aggregate paren init
2388 : 1824758 : else if (TREE_CODE (to) == ARRAY_TYPE && !TYPE_DOMAIN (to))
2389 : 224 : return error_mark_node; // can't construct an array of unknown bound
2390 : : else
2391 : 1824534 : expr = constructible_expr (to, from);
2392 : : return expr;
2393 : : }
2394 : :
2395 : : /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or
2396 : : constructible (otherwise) from FROM, which is a single type for
2397 : : assignment or a list of types for construction. */
2398 : :
2399 : : bool
2400 : 91994 : is_trivially_xible (enum tree_code code, tree to, tree from)
2401 : : {
2402 : 91994 : tree expr = is_xible_helper (code, to, from, /*trivial*/true);
2403 : 91994 : if (expr == NULL_TREE || expr == error_mark_node)
2404 : : return false;
2405 : 90806 : tree nt = cp_walk_tree_without_duplicates (&expr, check_nontriv, NULL);
2406 : 90806 : return !nt;
2407 : : }
2408 : :
2409 : : /* Returns true iff TO is nothrow assignable (if CODE is MODIFY_EXPR) or
2410 : : constructible (otherwise) from FROM, which is a single type for
2411 : : assignment or a list of types for construction. */
2412 : :
2413 : : bool
2414 : 896660 : is_nothrow_xible (enum tree_code code, tree to, tree from)
2415 : : {
2416 : 896660 : ++cp_noexcept_operand;
2417 : 896660 : tree expr = is_xible_helper (code, to, from, /*trivial*/false);
2418 : 896660 : --cp_noexcept_operand;
2419 : 896660 : if (expr == NULL_TREE || expr == error_mark_node)
2420 : : return false;
2421 : 896319 : return expr_noexcept_p (expr, tf_none);
2422 : : }
2423 : :
2424 : : /* Returns true iff TO is assignable (if CODE is MODIFY_EXPR) or
2425 : : constructible (otherwise) from FROM, which is a single type for
2426 : : assignment or a list of types for construction. */
2427 : :
2428 : : bool
2429 : 1751027 : is_xible (enum tree_code code, tree to, tree from)
2430 : : {
2431 : 1751027 : tree expr = is_xible_helper (code, to, from, /*trivial*/false);
2432 : 1751027 : if (expr == error_mark_node)
2433 : : return false;
2434 : 1692764 : return !!expr;
2435 : : }
2436 : :
2437 : : /* Return true iff conjunction_v<is_reference<T>, is_constructible<T, U>> is
2438 : : true, and the initialization
2439 : : T t(VAL<U>); // DIRECT_INIT_P
2440 : : or
2441 : : T t = VAL<U>; // !DIRECT_INIT_P
2442 : : binds t to a temporary object whose lifetime is extended.
2443 : : VAL<T> is defined in [meta.unary.prop]:
2444 : : -- If T is a reference or function type, VAL<T> is an expression with the
2445 : : same type and value category as declval<T>().
2446 : : -- Otherwise, VAL<T> is a prvalue that initially has type T. */
2447 : :
2448 : : bool
2449 : 115714 : ref_xes_from_temporary (tree to, tree from, bool direct_init_p)
2450 : : {
2451 : : /* Check is_reference<T>. */
2452 : 115714 : if (!TYPE_REF_P (to))
2453 : : return false;
2454 : : /* We don't check is_constructible<T, U>: if T isn't constructible
2455 : : from U, we won't be able to create a conversion. */
2456 : 12339 : tree val = build_trait_object (from);
2457 : 12339 : if (val == error_mark_node)
2458 : : return false;
2459 : 12339 : if (!TYPE_REF_P (from) && TREE_CODE (from) != FUNCTION_TYPE)
2460 : 391 : val = CLASS_TYPE_P (from) ? force_rvalue (val, tf_none) : rvalue (val);
2461 : 12339 : return ref_conv_binds_to_temporary (to, val, direct_init_p).is_true ();
2462 : : }
2463 : :
2464 : : /* Worker for is_{,nothrow_}convertible. Attempt to perform an implicit
2465 : : conversion from FROM to TO and return the result. */
2466 : :
2467 : : static tree
2468 : 1085647 : is_convertible_helper (tree from, tree to)
2469 : : {
2470 : 1085647 : if (VOID_TYPE_P (from) && VOID_TYPE_P (to))
2471 : 44 : return integer_one_node;
2472 : 1085603 : cp_unevaluated u;
2473 : 1085603 : tree expr = build_trait_object (from);
2474 : : /* std::is_{,nothrow_}convertible test whether the imaginary function
2475 : : definition
2476 : :
2477 : : To test() { return std::declval<From>(); }
2478 : :
2479 : : is well-formed. A function can't return a function. */
2480 : 1085603 : if (FUNC_OR_METHOD_TYPE_P (to) || expr == error_mark_node)
2481 : 83 : return error_mark_node;
2482 : 1085520 : deferring_access_check_sentinel acs (dk_no_deferred);
2483 : 1085520 : return perform_implicit_conversion (to, expr, tf_none);
2484 : 1085603 : }
2485 : :
2486 : : /* Return true if FROM can be converted to TO using implicit conversions,
2487 : : or both FROM and TO are possibly cv-qualified void. NB: This doesn't
2488 : : implement the "Access checks are performed as if from a context unrelated
2489 : : to either type" restriction. */
2490 : :
2491 : : bool
2492 : 1084903 : is_convertible (tree from, tree to)
2493 : : {
2494 : 1084903 : tree expr = is_convertible_helper (from, to);
2495 : 1084903 : if (expr == error_mark_node)
2496 : : return false;
2497 : 894106 : return !!expr;
2498 : : }
2499 : :
2500 : : /* Like is_convertible, but the conversion is also noexcept. */
2501 : :
2502 : : bool
2503 : 744 : is_nothrow_convertible (tree from, tree to)
2504 : : {
2505 : 744 : tree expr = is_convertible_helper (from, to);
2506 : 744 : if (expr == NULL_TREE || expr == error_mark_node)
2507 : : return false;
2508 : 395 : return expr_noexcept_p (expr, tf_none);
2509 : : }
2510 : :
2511 : : /* Categorize various special_function_kinds. */
2512 : : #define SFK_CTOR_P(sfk) \
2513 : : ((sfk) >= sfk_constructor && (sfk) <= sfk_move_constructor)
2514 : : #define SFK_DTOR_P(sfk) \
2515 : : ((sfk) == sfk_destructor || (sfk) == sfk_virtual_destructor)
2516 : : #define SFK_ASSIGN_P(sfk) \
2517 : : ((sfk) == sfk_copy_assignment || (sfk) == sfk_move_assignment)
2518 : : #define SFK_COPY_P(sfk) \
2519 : : ((sfk) == sfk_copy_constructor || (sfk) == sfk_copy_assignment)
2520 : : #define SFK_MOVE_P(sfk) \
2521 : : ((sfk) == sfk_move_constructor || (sfk) == sfk_move_assignment)
2522 : :
2523 : : /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
2524 : : DELETED_P or give an error message MSG with argument ARG. */
2525 : :
2526 : : static void
2527 : 21159948 : process_subob_fn (tree fn, special_function_kind sfk, tree *spec_p,
2528 : : bool *trivial_p, bool *deleted_p, bool *constexpr_p,
2529 : : bool diag, tree arg, bool dtor_from_ctor = false)
2530 : : {
2531 : 21159948 : if (!fn || fn == error_mark_node)
2532 : : {
2533 : 723888 : if (deleted_p)
2534 : 723867 : *deleted_p = true;
2535 : 723888 : return;
2536 : : }
2537 : :
2538 : 20436060 : if (spec_p)
2539 : : {
2540 : 3509934 : if (!maybe_instantiate_noexcept (fn))
2541 : 3 : *spec_p = error_mark_node;
2542 : : else
2543 : : {
2544 : 3509931 : tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2545 : 3509931 : *spec_p = merge_exception_specifiers (*spec_p, raises);
2546 : : }
2547 : : }
2548 : :
2549 : 20436060 : if (!trivial_fn_p (fn) && !dtor_from_ctor)
2550 : : {
2551 : 6406621 : if (trivial_p)
2552 : 3816313 : *trivial_p = false;
2553 : 6406621 : if (TREE_CODE (arg) == FIELD_DECL
2554 : 6406621 : && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
2555 : : {
2556 : 1925 : if (deleted_p)
2557 : 1921 : *deleted_p = true;
2558 : 1925 : if (diag)
2559 : 25 : error ("union member %q+D with non-trivial %qD", arg, fn);
2560 : : }
2561 : : }
2562 : :
2563 : 20436060 : if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
2564 : : {
2565 : 4040882 : *constexpr_p = false;
2566 : 4040882 : if (diag)
2567 : : {
2568 : 10 : inform (DECL_SOURCE_LOCATION (fn),
2569 : 10 : SFK_DTOR_P (sfk)
2570 : : ? G_("defaulted destructor calls non-%<constexpr%> %qD")
2571 : : : G_("defaulted constructor calls non-%<constexpr%> %qD"),
2572 : : fn);
2573 : 10 : explain_invalid_constexpr_fn (fn);
2574 : : }
2575 : : }
2576 : : }
2577 : :
2578 : : /* Subroutine of synthesized_method_walk to allow recursion into anonymous
2579 : : aggregates. If DTOR_FROM_CTOR is true, we're walking subobject destructors
2580 : : called from a synthesized constructor, in which case we don't consider
2581 : : the triviality of the subobject destructor. */
2582 : :
2583 : : static void
2584 : 38997876 : walk_field_subobs (tree fields, special_function_kind sfk, tree fnname,
2585 : : int quals, tree *spec_p, bool *trivial_p,
2586 : : bool *deleted_p, bool *constexpr_p,
2587 : : bool diag, int flags, tsubst_flags_t complain,
2588 : : bool dtor_from_ctor)
2589 : : {
2590 : 38997876 : if (!fields)
2591 : : return;
2592 : :
2593 : 38997872 : tree ctx = DECL_CONTEXT (fields);
2594 : :
2595 : : /* CWG2084: A defaulted default ctor for a union with a DMI only initializes
2596 : : that member, so don't check other members. */
2597 : 38997872 : enum { unknown, no, yes }
2598 : 38997872 : only_dmi_mem = (sfk == sfk_constructor && TREE_CODE (ctx) == UNION_TYPE
2599 : 38997872 : ? unknown : no);
2600 : :
2601 : 39047136 : again:
2602 : 759313136 : for (tree field = fields; field; field = DECL_CHAIN (field))
2603 : : {
2604 : 720411816 : tree mem_type, argtype, rval;
2605 : :
2606 : 1407790939 : if (TREE_CODE (field) != FIELD_DECL
2607 : 39688241 : || DECL_ARTIFICIAL (field)
2608 : 753453967 : || DECL_UNNAMED_BIT_FIELD (field))
2609 : 687379123 : continue;
2610 : :
2611 : : /* Variant members only affect deletedness. In particular, they don't
2612 : : affect the exception-specification of a user-provided destructor,
2613 : : which we're figuring out via get_defaulted_eh_spec. So if we aren't
2614 : : asking if this is deleted, don't even look up the function; we don't
2615 : : want an error about a deleted function we aren't actually calling. */
2616 : 33032693 : if (sfk == sfk_destructor && deleted_p == NULL
2617 : 2575503 : && TREE_CODE (ctx) == UNION_TYPE)
2618 : : break;
2619 : :
2620 : 32886877 : if (only_dmi_mem != no)
2621 : : {
2622 : 129962 : if (DECL_INITIAL (field))
2623 : : only_dmi_mem = yes;
2624 : : else
2625 : : /* Don't check this until we know there's no DMI. */
2626 : 129787 : continue;
2627 : : }
2628 : :
2629 : 32757090 : mem_type = strip_array_types (TREE_TYPE (field));
2630 : 32757090 : if (SFK_ASSIGN_P (sfk))
2631 : : {
2632 : 3380491 : bool bad = true;
2633 : 3380491 : if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
2634 : : {
2635 : 113 : if (diag)
2636 : 7 : error ("non-static const member %q#D, cannot use default "
2637 : : "assignment operator", field);
2638 : : }
2639 : 3380378 : else if (TYPE_REF_P (mem_type))
2640 : : {
2641 : 1415 : if (diag)
2642 : 2 : error ("non-static reference member %q#D, cannot use "
2643 : : "default assignment operator", field);
2644 : : }
2645 : : else
2646 : : bad = false;
2647 : :
2648 : 3380491 : if (bad && deleted_p)
2649 : 1528 : *deleted_p = true;
2650 : : }
2651 : 29376599 : else if (sfk == sfk_constructor || sfk == sfk_inheriting_constructor)
2652 : : {
2653 : 2566242 : bool bad;
2654 : :
2655 : 2566242 : if (DECL_INITIAL (field))
2656 : : {
2657 : 680668 : if (diag && DECL_INITIAL (field) == error_mark_node)
2658 : 0 : inform (DECL_SOURCE_LOCATION (field),
2659 : : "initializer for %q#D is invalid", field);
2660 : 680668 : if (trivial_p)
2661 : 546168 : *trivial_p = false;
2662 : : /* Core 1351: If the field has an NSDMI that could throw, the
2663 : : default constructor is noexcept(false). */
2664 : 680668 : if (spec_p)
2665 : : {
2666 : 139436 : tree nsdmi = get_nsdmi (field, /*ctor*/false, complain);
2667 : 139436 : if (nsdmi == error_mark_node)
2668 : 121 : *spec_p = error_mark_node;
2669 : 139315 : else if (*spec_p != error_mark_node
2670 : 139315 : && !expr_noexcept_p (nsdmi, tf_none))
2671 : 862 : *spec_p = noexcept_false_spec;
2672 : : }
2673 : : /* Don't do the normal processing. */
2674 : 680668 : continue;
2675 : 680668 : }
2676 : :
2677 : 1885574 : bad = false;
2678 : 1885574 : if (CP_TYPE_CONST_P (mem_type)
2679 : 1885574 : && default_init_uninitialized_part (mem_type))
2680 : : {
2681 : 359 : if (diag)
2682 : : {
2683 : 53 : error ("uninitialized const member in %q#T",
2684 : : current_class_type);
2685 : 53 : inform (DECL_SOURCE_LOCATION (field),
2686 : : "%q#D should be initialized", field);
2687 : : }
2688 : : bad = true;
2689 : : }
2690 : 1885215 : else if (TYPE_REF_P (mem_type))
2691 : : {
2692 : 10038 : if (diag)
2693 : : {
2694 : 48 : error ("uninitialized reference member in %q#T",
2695 : : current_class_type);
2696 : 48 : inform (DECL_SOURCE_LOCATION (field),
2697 : : "%q#D should be initialized", field);
2698 : : }
2699 : : bad = true;
2700 : : }
2701 : :
2702 : 1885574 : if (bad && deleted_p)
2703 : 10397 : *deleted_p = true;
2704 : :
2705 : : /* Before C++20, for an implicitly-defined default constructor to
2706 : : be constexpr, every member must have a user-provided default
2707 : : constructor or an explicit initializer. */
2708 : 1885574 : if (constexpr_p
2709 : 1639088 : && cxx_dialect < cxx20
2710 : 977877 : && !CLASS_TYPE_P (mem_type)
2711 : 2567113 : && TREE_CODE (ctx) != UNION_TYPE)
2712 : : {
2713 : 593975 : *constexpr_p = false;
2714 : 593975 : if (diag)
2715 : 2 : inform (DECL_SOURCE_LOCATION (field),
2716 : : "defaulted default constructor does not "
2717 : : "initialize %q#D", field);
2718 : : }
2719 : : }
2720 : 26810357 : else if (sfk == sfk_copy_constructor)
2721 : : {
2722 : : /* 12.8p11b5 */
2723 : 4408008 : if (TYPE_REF_P (mem_type)
2724 : 4408008 : && TYPE_REF_IS_RVALUE (mem_type))
2725 : : {
2726 : 339 : if (diag)
2727 : 3 : error ("copying non-static data member %q#D of rvalue "
2728 : : "reference type", field);
2729 : 339 : if (deleted_p)
2730 : 339 : *deleted_p = true;
2731 : : }
2732 : : }
2733 : :
2734 : 32076422 : if (!CLASS_TYPE_P (mem_type))
2735 : 22780665 : continue;
2736 : :
2737 : 9295757 : if (ANON_AGGR_TYPE_P (mem_type))
2738 : : {
2739 : 187999 : walk_field_subobs (TYPE_FIELDS (mem_type), sfk, fnname, quals,
2740 : : spec_p, trivial_p, deleted_p, constexpr_p,
2741 : : diag, flags, complain, dtor_from_ctor);
2742 : 187999 : continue;
2743 : : }
2744 : :
2745 : 9107758 : if (SFK_COPY_P (sfk) || SFK_MOVE_P (sfk))
2746 : : {
2747 : 3286685 : int mem_quals = cp_type_quals (mem_type) | quals;
2748 : 3286685 : if (DECL_MUTABLE_P (field))
2749 : 252 : mem_quals &= ~TYPE_QUAL_CONST;
2750 : 3286685 : argtype = build_stub_type (mem_type, mem_quals, SFK_MOVE_P (sfk));
2751 : 3286685 : }
2752 : : else
2753 : : argtype = NULL_TREE;
2754 : :
2755 : 9107758 : rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
2756 : :
2757 : 9107758 : process_subob_fn (rval, sfk, spec_p, trivial_p, deleted_p,
2758 : : constexpr_p, diag, field, dtor_from_ctor);
2759 : : }
2760 : :
2761 : : /* We didn't find a DMI in this union, now check all the members. */
2762 : 39047136 : if (only_dmi_mem == unknown)
2763 : : {
2764 : 49264 : only_dmi_mem = no;
2765 : 49264 : goto again;
2766 : : }
2767 : : }
2768 : :
2769 : : /* Base walker helper for synthesized_method_walk. Inspect a direct
2770 : : or virtual base. BINFO is the parent type's binfo. BASE_BINFO is
2771 : : the base binfo of interests. All other parms are as for
2772 : : synthesized_method_walk, or its local vars. */
2773 : :
2774 : : static tree
2775 : 8298985 : synthesized_method_base_walk (tree binfo, tree base_binfo,
2776 : : special_function_kind sfk, tree fnname, int quals,
2777 : : tree *inheriting_ctor, tree inherited_parms,
2778 : : int flags, bool diag,
2779 : : tree *spec_p, bool *trivial_p,
2780 : : bool *deleted_p, bool *constexpr_p)
2781 : : {
2782 : 8298985 : bool inherited_binfo = false;
2783 : 8298985 : tree argtype = NULL_TREE;
2784 : 8298985 : deferring_kind defer = dk_no_deferred;
2785 : :
2786 : 8298985 : if (SFK_COPY_P (sfk) || SFK_MOVE_P (sfk))
2787 : 3637878 : argtype = build_stub_type (BINFO_TYPE (base_binfo), quals, SFK_MOVE_P (sfk));
2788 : 4661107 : else if (inheriting_ctor
2789 : 4661107 : && (inherited_binfo
2790 : 4661107 : = binfo_inherited_from (binfo, base_binfo, *inheriting_ctor)))
2791 : : {
2792 : 53029 : argtype = inherited_parms;
2793 : : /* Don't check access on the inherited constructor. */
2794 : 53029 : if (flag_new_inheriting_ctors)
2795 : : defer = dk_deferred;
2796 : : }
2797 : 4585211 : else if (cxx_dialect >= cxx14 && sfk == sfk_virtual_destructor
2798 : 1572539 : && BINFO_VIRTUAL_P (base_binfo)
2799 : 4789861 : && ABSTRACT_CLASS_TYPE_P (BINFO_TYPE (binfo)))
2800 : : /* Don't check access when looking at vbases of abstract class's
2801 : : virtual destructor. */
2802 : : defer = dk_no_check;
2803 : :
2804 : 3637878 : if (defer != dk_no_deferred)
2805 : 53158 : push_deferring_access_checks (defer);
2806 : 16597772 : tree rval = locate_fn_flags (base_binfo, fnname, argtype, flags,
2807 : : diag ? tf_warning_or_error : tf_none);
2808 : 8298985 : if (defer != dk_no_deferred)
2809 : 53158 : pop_deferring_access_checks ();
2810 : :
2811 : : /* Replace an inherited template with the appropriate specialization. */
2812 : 8298985 : if (inherited_binfo && rval
2813 : 53029 : && DECL_P (*inheriting_ctor) && DECL_P (rval)
2814 : 8351981 : && DECL_CONTEXT (*inheriting_ctor) == DECL_CONTEXT (rval))
2815 : 52984 : *inheriting_ctor = DECL_CLONED_FUNCTION (rval);
2816 : :
2817 : 16597970 : process_subob_fn (rval, sfk, spec_p, trivial_p, deleted_p,
2818 : 8298985 : constexpr_p, diag, BINFO_TYPE (base_binfo));
2819 : 8298985 : if (SFK_CTOR_P (sfk)
2820 : 12060804 : && (!BINFO_VIRTUAL_P (base_binfo)
2821 : 57329 : || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))))
2822 : : {
2823 : : /* In a constructor we also need to check the subobject
2824 : : destructors for cleanup of partially constructed objects. */
2825 : 3753205 : tree dtor = locate_fn_flags (base_binfo, complete_dtor_identifier,
2826 : : NULL_TREE, flags,
2827 : : diag ? tf_warning_or_error : tf_none);
2828 : : /* Note that we don't pass down trivial_p; the subobject
2829 : : destructors don't affect triviality of the constructor. Nor
2830 : : do they affect constexpr-ness (a constant expression doesn't
2831 : : throw) or exception-specification (a throw from one of the
2832 : : dtors would be a double-fault). */
2833 : 7506410 : process_subob_fn (dtor, sfk, NULL, NULL, deleted_p, NULL, false,
2834 : 3753205 : BINFO_TYPE (base_binfo), /*dtor_from_ctor*/true);
2835 : : }
2836 : :
2837 : 8298985 : return rval;
2838 : : }
2839 : :
2840 : : /* The caller wants to generate an implicit declaration of SFK for
2841 : : CTYPE which is const if relevant and CONST_P is set. If SPEC_P,
2842 : : TRIVIAL_P, DELETED_P or CONSTEXPR_P are non-null, set their
2843 : : referent appropriately. If DIAG is true, we're either being called
2844 : : from maybe_explain_implicit_delete to give errors, or if
2845 : : CONSTEXPR_P is non-null, from explain_invalid_constexpr_fn. */
2846 : :
2847 : : static void
2848 : 25561368 : synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
2849 : : tree *spec_p, bool *trivial_p, bool *deleted_p,
2850 : : bool *constexpr_p, bool diag,
2851 : : tree *inheriting_ctor, tree inherited_parms)
2852 : : {
2853 : 25561368 : tree binfo, base_binfo;
2854 : 25561368 : int i;
2855 : :
2856 : : /* SFK must be exactly one category. */
2857 : 25561368 : gcc_checking_assert (SFK_DTOR_P(sfk) + SFK_CTOR_P(sfk)
2858 : : + SFK_ASSIGN_P(sfk) == 1);
2859 : :
2860 : 25561368 : if (spec_p)
2861 : 3690786 : *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec);
2862 : :
2863 : 25561368 : if (deleted_p)
2864 : : {
2865 : : /* "The closure type associated with a lambda-expression has a deleted
2866 : : default constructor and a deleted copy assignment operator."
2867 : : This is diagnosed in maybe_explain_implicit_delete.
2868 : : In C++20, only lambda-expressions with lambda-captures have those
2869 : : deleted. */
2870 : 43255206 : if (LAMBDA_TYPE_P (ctype)
2871 : 758721 : && (sfk == sfk_constructor || sfk == sfk_copy_assignment)
2872 : 22102339 : && (cxx_dialect < cxx20
2873 : 137160 : || LAMBDA_EXPR_CAPTURE_LIST (CLASSTYPE_LAMBDA_EXPR (ctype))
2874 : 14807 : || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE
2875 : 14807 : (CLASSTYPE_LAMBDA_EXPR (ctype)) != CPLD_NONE))
2876 : : {
2877 : 141740 : *deleted_p = true;
2878 : 141740 : return;
2879 : : }
2880 : :
2881 : 21804277 : *deleted_p = false;
2882 : : }
2883 : :
2884 : 25419628 : bool check_vdtor = false;
2885 : 25419628 : tree fnname;
2886 : :
2887 : 25419628 : if (SFK_DTOR_P (sfk))
2888 : : {
2889 : 8379159 : check_vdtor = true;
2890 : : /* The synthesized method will call base dtors, but check complete
2891 : : here to avoid having to deal with VTT. */
2892 : 8379159 : fnname = complete_dtor_identifier;
2893 : : }
2894 : 17040469 : else if (SFK_ASSIGN_P (sfk))
2895 : 3623598 : fnname = assign_op_identifier;
2896 : : else
2897 : 13416871 : fnname = complete_ctor_identifier;
2898 : :
2899 : 50786248 : gcc_assert ((sfk == sfk_inheriting_constructor)
2900 : : == (inheriting_ctor && *inheriting_ctor != NULL_TREE));
2901 : :
2902 : : /* If that user-written default constructor would satisfy the
2903 : : requirements of a constexpr constructor (7.1.5), the
2904 : : implicitly-defined default constructor is constexpr.
2905 : :
2906 : : C++20:
2907 : : The implicitly-defined copy/move assignment operator is constexpr if
2908 : : - X is a literal type, and
2909 : : - the assignment operator selected to copy/move each direct base class
2910 : : subobject is a constexpr function, and
2911 : : - for each non-static data member of X that is of class type (or array
2912 : : thereof), the assignment operator selected to copy/move that
2913 : : member is a constexpr function.
2914 : :
2915 : : C++23:
2916 : : The implicitly-defined copy/move assignment operator is constexpr. */
2917 : 25419628 : if (constexpr_p)
2918 : 21803737 : *constexpr_p = (SFK_CTOR_P (sfk)
2919 : 9185044 : || (SFK_ASSIGN_P (sfk) && cxx_dialect >= cxx14)
2920 : 27569711 : || (SFK_DTOR_P (sfk) && cxx_dialect >= cxx20));
2921 : :
2922 : 25419628 : bool expected_trivial = type_has_trivial_fn (ctype, sfk);
2923 : 25419628 : if (trivial_p)
2924 : 21803719 : *trivial_p = expected_trivial;
2925 : :
2926 : : /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
2927 : : class versions and other properties of the type. But a subobject
2928 : : class can be trivially copyable and yet have overload resolution
2929 : : choose a template constructor for initialization, depending on
2930 : : rvalueness and cv-quals. And furthermore, a member in a base might
2931 : : be trivial but deleted or otherwise not callable. So we can't exit
2932 : : early in C++0x. The same considerations apply in C++98/03, but
2933 : : there the definition of triviality does not consider overload
2934 : : resolution, so a constructor can be trivial even if it would otherwise
2935 : : call a non-trivial constructor. */
2936 : 25419628 : if (expected_trivial
2937 : 18520474 : && (!(SFK_COPY_P (sfk) || SFK_MOVE_P (sfk)) || cxx_dialect < cxx11))
2938 : : {
2939 : 8149792 : if (constexpr_p && sfk == sfk_constructor)
2940 : : {
2941 : 2585222 : bool cx = trivial_default_constructor_is_constexpr (ctype);
2942 : 2585222 : *constexpr_p = cx;
2943 : 2585222 : if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
2944 : : /* A trivial constructor doesn't have any NSDMI. */
2945 : 2 : inform (input_location, "defaulted default constructor does "
2946 : : "not initialize any non-static data member");
2947 : : }
2948 : 8149792 : if (!diag && cxx_dialect < cxx11)
2949 : : return;
2950 : : }
2951 : :
2952 : 25404966 : bool push_to_top = maybe_push_to_top_level (TYPE_NAME (ctype));
2953 : 25404966 : ++cp_unevaluated_operand;
2954 : 25404966 : ++c_inhibit_evaluation_warnings;
2955 : 25404966 : push_deferring_access_checks (dk_no_deferred);
2956 : :
2957 : 25404966 : tree scope = push_scope (ctype);
2958 : :
2959 : 25404966 : int flags = LOOKUP_NORMAL | LOOKUP_SPECULATIVE;
2960 : 25404966 : if (sfk != sfk_inheriting_constructor)
2961 : 25351958 : flags |= LOOKUP_DEFAULTED;
2962 : :
2963 : 25404966 : tsubst_flags_t complain = diag ? tf_warning_or_error : tf_none;
2964 : 25404966 : if (diag && spec_p)
2965 : : /* We're in get_defaulted_eh_spec; we don't actually want any walking
2966 : : diagnostics, we just want complain set. */
2967 : 3262076 : diag = false;
2968 : 25404966 : int quals = const_p ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED;
2969 : :
2970 : 33494168 : for (binfo = TYPE_BINFO (ctype), i = 0;
2971 : 33494168 : BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
2972 : : {
2973 : 8089202 : if (!SFK_ASSIGN_P (sfk) && BINFO_VIRTUAL_P (base_binfo))
2974 : : /* We'll handle virtual bases below. */
2975 : 56666 : continue;
2976 : :
2977 : 8032536 : tree fn = synthesized_method_base_walk (binfo, base_binfo,
2978 : : sfk, fnname, quals,
2979 : : inheriting_ctor, inherited_parms,
2980 : : flags, diag, spec_p, trivial_p,
2981 : : deleted_p, constexpr_p);
2982 : :
2983 : 189 : if (diag && SFK_ASSIGN_P (sfk) && SFK_MOVE_P (sfk)
2984 : 76 : && BINFO_VIRTUAL_P (base_binfo)
2985 : 34 : && fn && TREE_CODE (fn) == FUNCTION_DECL
2986 : 34 : && move_fn_p (fn) && !trivial_fn_p (fn)
2987 : 21 : && vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo))
2988 : 8032554 : && warning_enabled_at (DECL_SOURCE_LOCATION (fn),
2989 : 18 : OPT_Wvirtual_move_assign))
2990 : 12 : warning (OPT_Wvirtual_move_assign,
2991 : : "defaulted move assignment for %qT calls a non-trivial "
2992 : : "move assignment operator for virtual base %qT",
2993 : 12 : ctype, BINFO_TYPE (base_binfo));
2994 : :
2995 : 8032536 : if (check_vdtor && type_has_virtual_destructor (BINFO_TYPE (base_binfo)))
2996 : : {
2997 : : /* Unlike for base ctor/op=/dtor, for operator delete it's fine
2998 : : to have a null fn (no class-specific op delete). */
2999 : 1371013 : fn = locate_fn_flags (ctype, ovl_op_identifier (false, DELETE_EXPR),
3000 : : ptr_type_node, flags, tf_none);
3001 : 1371013 : if (fn && fn == error_mark_node)
3002 : : {
3003 : 21 : if (complain & tf_error)
3004 : 5 : locate_fn_flags (ctype, ovl_op_identifier (false, DELETE_EXPR),
3005 : : ptr_type_node, flags, complain);
3006 : 21 : if (deleted_p)
3007 : 16 : *deleted_p = true;
3008 : : }
3009 : : check_vdtor = false;
3010 : : }
3011 : : }
3012 : :
3013 : 25404966 : vec<tree, va_gc> *vbases = CLASSTYPE_VBASECLASSES (ctype);
3014 : 25404966 : if (SFK_ASSIGN_P (sfk))
3015 : : /* Already examined vbases above. */;
3016 : 21782569 : else if (vec_safe_is_empty (vbases))
3017 : : /* No virtual bases to worry about. */;
3018 : 194747 : else if (ABSTRACT_CLASS_TYPE_P (ctype) && cxx_dialect >= cxx14
3019 : : /* DR 1658 specifies that vbases of abstract classes are
3020 : : ignored for both ctors and dtors. Except DR 2336
3021 : : overrides that skipping when determing the eh-spec of a
3022 : : virtual destructor. */
3023 : 195126 : && sfk != sfk_virtual_destructor)
3024 : : /* Vbase cdtors are not relevant. */;
3025 : : else
3026 : : {
3027 : 194406 : if (constexpr_p)
3028 : 10712 : *constexpr_p = false;
3029 : :
3030 : 460855 : FOR_EACH_VEC_ELT (*vbases, i, base_binfo)
3031 : 266449 : synthesized_method_base_walk (binfo, base_binfo, sfk, fnname, quals,
3032 : : inheriting_ctor, inherited_parms,
3033 : : flags, diag,
3034 : : spec_p, trivial_p, deleted_p, constexpr_p);
3035 : : }
3036 : :
3037 : : /* Now handle the non-static data members. */
3038 : 25404966 : walk_field_subobs (TYPE_FIELDS (ctype), sfk, fnname, quals,
3039 : : spec_p, trivial_p, deleted_p, constexpr_p,
3040 : : diag, flags, complain, /*dtor_from_ctor*/false);
3041 : 25404966 : if (SFK_CTOR_P (sfk))
3042 : 13404911 : walk_field_subobs (TYPE_FIELDS (ctype), sfk_destructor,
3043 : : complete_dtor_identifier, TYPE_UNQUALIFIED,
3044 : : NULL, NULL, deleted_p, NULL,
3045 : : false, flags, complain, /*dtor_from_ctor*/true);
3046 : :
3047 : 25404966 : pop_scope (scope);
3048 : :
3049 : 25404966 : pop_deferring_access_checks ();
3050 : 25404966 : --cp_unevaluated_operand;
3051 : 25404966 : --c_inhibit_evaluation_warnings;
3052 : 25404966 : maybe_pop_from_top_level (push_to_top);
3053 : : }
3054 : :
3055 : : /* DECL is a defaulted function whose exception specification is now
3056 : : needed. Return what it should be. */
3057 : :
3058 : : tree
3059 : 3615263 : get_defaulted_eh_spec (tree decl, tsubst_flags_t complain)
3060 : : {
3061 : : /* For DECL_MAYBE_DELETED this should already have been handled by
3062 : : synthesize_method. */
3063 : 3615263 : gcc_assert (!DECL_MAYBE_DELETED (decl));
3064 : :
3065 : 3615263 : if (DECL_CLONED_FUNCTION_P (decl))
3066 : 0 : decl = DECL_CLONED_FUNCTION (decl);
3067 : 3615263 : special_function_kind sfk = special_function_p (decl);
3068 : 3615263 : tree ctype = DECL_CONTEXT (decl);
3069 : 3615263 : tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
3070 : 3615263 : tree parm_type = TREE_VALUE (parms);
3071 : 3615263 : bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
3072 : 3615263 : tree spec = empty_except_spec;
3073 : 3615263 : bool diag = !DECL_DELETED_FN (decl) && (complain & tf_error);
3074 : 7230526 : tree inh = DECL_INHERITED_CTOR (decl);
3075 : 3615263 : if (SFK_DTOR_P (sfk) && DECL_VIRTUAL_P (decl))
3076 : : /* We have to examine virtual bases even if abstract. */
3077 : : sfk = sfk_virtual_destructor;
3078 : 3615263 : bool pushed = false;
3079 : 3615263 : if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
3080 : 2351144 : pushed = push_tinst_level (decl);
3081 : 3615263 : synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL,
3082 : : NULL, diag, &inh, parms);
3083 : 3615263 : if (pushed)
3084 : 2350861 : pop_tinst_level ();
3085 : 3615263 : return spec;
3086 : : }
3087 : :
3088 : : /* DECL is a deleted function. If it's implicitly deleted, explain why and
3089 : : return true; else return false. */
3090 : :
3091 : : bool
3092 : 2326 : maybe_explain_implicit_delete (tree decl)
3093 : : {
3094 : : /* If decl is a clone, get the primary variant. */
3095 : 2326 : decl = DECL_ORIGIN (decl);
3096 : 2326 : gcc_assert (DECL_DELETED_FN (decl));
3097 : 2326 : if (DECL_DEFAULTED_FN (decl))
3098 : : {
3099 : : /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
3100 : 1728 : static hash_set<tree> *explained;
3101 : :
3102 : 1728 : special_function_kind sfk;
3103 : 1728 : location_t loc;
3104 : 1728 : bool informed;
3105 : 1728 : tree ctype;
3106 : :
3107 : 1728 : if (!explained)
3108 : 230 : explained = new hash_set<tree>;
3109 : 1728 : if (explained->add (decl))
3110 : : return true;
3111 : :
3112 : 462 : sfk = special_function_p (decl);
3113 : 462 : ctype = DECL_CONTEXT (decl);
3114 : 462 : loc = input_location;
3115 : 462 : input_location = DECL_SOURCE_LOCATION (decl);
3116 : :
3117 : 462 : informed = false;
3118 : 893 : if (LAMBDA_TYPE_P (ctype))
3119 : : {
3120 : 36 : informed = true;
3121 : 36 : if (sfk == sfk_constructor)
3122 : 19 : inform (DECL_SOURCE_LOCATION (decl),
3123 : : "a lambda closure type has a deleted default constructor");
3124 : 17 : else if (sfk == sfk_copy_assignment)
3125 : 17 : inform (DECL_SOURCE_LOCATION (decl),
3126 : : "a lambda closure type has a deleted copy assignment operator");
3127 : : else
3128 : : informed = false;
3129 : : }
3130 : 426 : else if (DECL_ARTIFICIAL (decl)
3131 : 331 : && (sfk == sfk_copy_assignment || sfk == sfk_copy_constructor)
3132 : 536 : && classtype_has_move_assign_or_move_ctor_p (ctype, true))
3133 : : {
3134 : 69 : inform (DECL_SOURCE_LOCATION (decl),
3135 : : "%q#D is implicitly declared as deleted because %qT "
3136 : : "declares a move constructor or move assignment operator",
3137 : : decl, ctype);
3138 : 69 : informed = true;
3139 : : }
3140 : 357 : else if (sfk == sfk_inheriting_constructor)
3141 : : {
3142 : 18 : tree binfo = inherited_ctor_binfo (decl);
3143 : 18 : if (TREE_CODE (binfo) != TREE_BINFO)
3144 : : {
3145 : 3 : inform (DECL_SOURCE_LOCATION (decl),
3146 : : "%q#D inherits from multiple base subobjects",
3147 : : decl);
3148 : 3 : informed = true;
3149 : : }
3150 : : }
3151 : 462 : if (!informed && sfk == sfk_comparison)
3152 : : {
3153 : 75 : inform (DECL_SOURCE_LOCATION (decl),
3154 : : "%q#D is implicitly deleted because the default "
3155 : : "definition would be ill-formed:", decl);
3156 : 75 : build_comparison_op (decl, false, tf_warning_or_error);
3157 : : }
3158 : 387 : else if (!informed)
3159 : : {
3160 : 279 : tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
3161 : 279 : bool const_p = false;
3162 : 279 : if (parms)
3163 : : {
3164 : 276 : tree parm_type = TREE_VALUE (parms);
3165 : 276 : const_p = CP_TYPE_CONST_P (non_reference (parm_type));
3166 : : }
3167 : 279 : tree raises = NULL_TREE;
3168 : 279 : bool deleted_p = false;
3169 : 279 : tree scope = push_scope (ctype);
3170 : 558 : tree inh = DECL_INHERITED_CTOR (decl);
3171 : :
3172 : 279 : synthesized_method_walk (ctype, sfk, const_p,
3173 : : &raises, NULL, &deleted_p, NULL, false,
3174 : : &inh, parms);
3175 : 279 : if (deleted_p)
3176 : : {
3177 : 279 : inform (DECL_SOURCE_LOCATION (decl),
3178 : : "%q#D is implicitly deleted because the default "
3179 : : "definition would be ill-formed:", decl);
3180 : 279 : synthesized_method_walk (ctype, sfk, const_p,
3181 : : NULL, NULL, &deleted_p, NULL, true,
3182 : : &inh, parms);
3183 : : }
3184 : 0 : else if (!comp_except_specs
3185 : 0 : (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
3186 : : raises, ce_normal))
3187 : 0 : inform (DECL_SOURCE_LOCATION (decl), "%q#F is implicitly "
3188 : : "deleted because its exception-specification does not "
3189 : : "match the implicit exception-specification %qX",
3190 : : decl, raises);
3191 : 0 : else if (flag_checking)
3192 : 0 : gcc_unreachable ();
3193 : :
3194 : 279 : pop_scope (scope);
3195 : : }
3196 : :
3197 : 462 : input_location = loc;
3198 : 462 : return true;
3199 : : }
3200 : : return false;
3201 : : }
3202 : :
3203 : : /* DECL is a defaulted function which was declared constexpr. Explain why
3204 : : it can't be constexpr. */
3205 : :
3206 : : void
3207 : 27 : explain_implicit_non_constexpr (tree decl)
3208 : : {
3209 : 27 : tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
3210 : 27 : bool const_p = CP_TYPE_CONST_P (non_reference (TREE_VALUE (parms)));
3211 : 54 : tree inh = DECL_INHERITED_CTOR (decl);
3212 : 27 : bool dummy;
3213 : 27 : special_function_kind sfk = special_function_p (decl);
3214 : 27 : if (sfk == sfk_comparison)
3215 : : {
3216 : 9 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
3217 : 9 : build_comparison_op (decl, false, tf_warning_or_error);
3218 : 9 : DECL_DECLARED_CONSTEXPR_P (decl) = false;
3219 : : }
3220 : : else
3221 : 18 : synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
3222 : : sfk, const_p,
3223 : : NULL, NULL, NULL, &dummy, true,
3224 : : &inh, parms);
3225 : 27 : }
3226 : :
3227 : : /* DECL is an instantiation of an inheriting constructor template. Deduce
3228 : : the correct exception-specification and deletedness for this particular
3229 : : specialization. Return true if the deduction succeeds; false otherwise. */
3230 : :
3231 : : bool
3232 : 52978 : deduce_inheriting_ctor (tree decl)
3233 : : {
3234 : 52978 : decl = DECL_ORIGIN (decl);
3235 : 105956 : gcc_assert (DECL_INHERITED_CTOR (decl));
3236 : 52978 : tree spec;
3237 : 52978 : bool trivial, constexpr_, deleted;
3238 : 52978 : tree inh = DECL_INHERITED_CTOR (decl);
3239 : 52978 : synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor,
3240 : : false, &spec, &trivial, &deleted, &constexpr_,
3241 : : /*diag*/false,
3242 : : &inh,
3243 : 52978 : FUNCTION_FIRST_USER_PARMTYPE (decl));
3244 : 52978 : if (spec == error_mark_node)
3245 : : return false;
3246 : 52976 : if (TREE_CODE (inherited_ctor_binfo (decl)) != TREE_BINFO)
3247 : : /* Inherited the same constructor from different base subobjects. */
3248 : 3 : deleted = true;
3249 : 52976 : DECL_DELETED_FN (decl) = deleted;
3250 : 52976 : TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
3251 : 52976 : SET_DECL_INHERITED_CTOR (decl, inh);
3252 : :
3253 : 52976 : tree clone;
3254 : 158928 : FOR_EACH_CLONE (clone, decl)
3255 : : {
3256 : 105952 : DECL_DELETED_FN (clone) = deleted;
3257 : 105952 : TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
3258 : 105952 : SET_DECL_INHERITED_CTOR (clone, inh);
3259 : : }
3260 : :
3261 : : return true;
3262 : : }
3263 : :
3264 : : /* Implicitly declare the special function indicated by KIND, as a
3265 : : member of TYPE. For copy constructors and assignment operators,
3266 : : CONST_P indicates whether these functions should take a const
3267 : : reference argument or a non-const reference.
3268 : : Returns the FUNCTION_DECL for the implicitly declared function. */
3269 : :
3270 : : tree
3271 : 22157467 : implicitly_declare_fn (special_function_kind kind, tree type,
3272 : : bool const_p, tree pattern_fn,
3273 : : tree inherited_parms)
3274 : : {
3275 : 22157467 : tree fn;
3276 : 22157467 : tree parameter_types = void_list_node;
3277 : 22157467 : tree return_type;
3278 : 22157467 : tree fn_type;
3279 : 22157467 : tree raises = empty_except_spec;
3280 : 22157467 : tree rhs_parm_type = NULL_TREE;
3281 : 22157467 : tree this_parm;
3282 : 22157467 : tree name;
3283 : 22157467 : HOST_WIDE_INT saved_processing_template_decl;
3284 : 22157467 : bool deleted_p = false;
3285 : 22157467 : bool constexpr_p = false;
3286 : 44314934 : tree inherited_ctor = (kind == sfk_inheriting_constructor
3287 : 22157467 : ? pattern_fn : NULL_TREE);
3288 : :
3289 : : /* Because we create declarations for implicitly declared functions
3290 : : lazily, we may be creating the declaration for a member of TYPE
3291 : : while in some completely different context. However, TYPE will
3292 : : never be a dependent class (because we never want to do lookups
3293 : : for implicitly defined functions in a dependent class). */
3294 : 22157467 : gcc_assert (!dependent_type_p (type));
3295 : :
3296 : : /* If the member-specification does not explicitly declare any member or
3297 : : friend named operator==, an == operator function is declared
3298 : : implicitly for each three-way comparison operator function defined as
3299 : : defaulted in the member-specification, with the same access and
3300 : : function-definition and in the same class scope as the respective
3301 : : three-way comparison operator function, except that the return type is
3302 : : replaced with bool and the declarator-id is replaced with
3303 : : operator==.
3304 : :
3305 : : [Note: Such an implicitly-declared == operator for a class X is
3306 : : defined as defaulted in the definition of X and has the same
3307 : : parameter-declaration-clause and trailing requires-clause as the
3308 : : respective three-way comparison operator. It is declared with friend,
3309 : : virtual, constexpr, or consteval if the three-way comparison operator
3310 : : function is so declared. If the three-way comparison operator function
3311 : : has no noexcept-specifier, the implicitly-declared == operator
3312 : : function has an implicit exception specification (14.5) that may
3313 : : differ from the implicit exception specification of the three-way
3314 : : comparison operator function. --end note] */
3315 : 22157467 : if (kind == sfk_comparison)
3316 : : {
3317 : 226 : fn = copy_operator_fn (pattern_fn, EQ_EXPR);
3318 : 226 : DECL_ARTIFICIAL (fn) = 1;
3319 : 226 : apply_deduced_return_type (fn, boolean_type_node);
3320 : 226 : return fn;
3321 : : }
3322 : :
3323 : : /* Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
3324 : : because we only create clones for constructors and destructors
3325 : : when not in a template. */
3326 : 22157241 : saved_processing_template_decl = processing_template_decl;
3327 : 22157241 : processing_template_decl = 0;
3328 : :
3329 : 22157241 : type = TYPE_MAIN_VARIANT (type);
3330 : :
3331 : 22157241 : if (targetm.cxx.cdtor_returns_this ())
3332 : : {
3333 : 0 : if (kind == sfk_destructor)
3334 : : /* See comment in check_special_function_return_type. */
3335 : 0 : return_type = build_pointer_type (void_type_node);
3336 : : else
3337 : 0 : return_type = build_pointer_type (type);
3338 : : }
3339 : : else
3340 : 22157241 : return_type = void_type_node;
3341 : :
3342 : 22157241 : int this_quals = TYPE_UNQUALIFIED;
3343 : 22157241 : switch (kind)
3344 : : {
3345 : 5755073 : case sfk_destructor:
3346 : : /* Destructor. */
3347 : 5755073 : name = dtor_identifier;
3348 : 5755073 : break;
3349 : :
3350 : 3772307 : case sfk_constructor:
3351 : : /* Default constructor. */
3352 : 3772307 : name = ctor_identifier;
3353 : 3772307 : break;
3354 : :
3355 : 12629861 : case sfk_copy_constructor:
3356 : 12629861 : case sfk_copy_assignment:
3357 : 12629861 : case sfk_move_constructor:
3358 : 12629861 : case sfk_move_assignment:
3359 : 12629861 : case sfk_inheriting_constructor:
3360 : 12629861 : {
3361 : 12629861 : if (kind == sfk_copy_assignment
3362 : 12629861 : || kind == sfk_move_assignment)
3363 : : {
3364 : 3430196 : return_type = build_reference_type (type);
3365 : 3430196 : name = assign_op_identifier;
3366 : : }
3367 : : else
3368 : 9199665 : name = ctor_identifier;
3369 : :
3370 : 12629861 : if (kind == sfk_inheriting_constructor)
3371 : : parameter_types = inherited_parms;
3372 : : else
3373 : : {
3374 : 12365101 : if (const_p)
3375 : 7366170 : rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
3376 : : else
3377 : : rhs_parm_type = type;
3378 : 12365101 : bool move_p = (kind == sfk_move_assignment
3379 : 12365101 : || kind == sfk_move_constructor);
3380 : 12365101 : rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
3381 : :
3382 : 12365101 : parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
3383 : : }
3384 : : break;
3385 : : }
3386 : :
3387 : 0 : default:
3388 : 0 : gcc_unreachable ();
3389 : : }
3390 : :
3391 : 22157241 : bool trivial_p = false;
3392 : :
3393 : 22157241 : if (inherited_ctor)
3394 : : {
3395 : : /* For an inheriting constructor, just copy these flags from the
3396 : : inherited constructor until deduce_inheriting_ctor. */
3397 : 264760 : raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor));
3398 : 264760 : deleted_p = DECL_DELETED_FN (inherited_ctor);
3399 : 264760 : constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
3400 : : }
3401 : 21892481 : else if (cxx_dialect >= cxx11)
3402 : : {
3403 : 21870218 : raises = noexcept_deferred_spec;
3404 : 21870218 : synthesized_method_walk (type, kind, const_p, NULL, &trivial_p,
3405 : : &deleted_p, &constexpr_p, false,
3406 : : &inherited_ctor, inherited_parms);
3407 : : }
3408 : : else
3409 : 22263 : synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
3410 : : &deleted_p, &constexpr_p, false,
3411 : : &inherited_ctor, inherited_parms);
3412 : : /* Don't bother marking a deleted constructor as constexpr. */
3413 : 22157241 : if (deleted_p)
3414 : 913301 : constexpr_p = false;
3415 : : /* A trivial copy/move constructor is also a constexpr constructor,
3416 : : unless the class has virtual bases (7.1.5p4). */
3417 : 21243940 : else if (trivial_p
3418 : 17359647 : && cxx_dialect >= cxx11
3419 : 17344985 : && (kind == sfk_copy_constructor
3420 : 17344985 : || kind == sfk_move_constructor)
3421 : 28301509 : && !CLASSTYPE_VBASECLASSES (type))
3422 : 7057569 : gcc_assert (constexpr_p);
3423 : :
3424 : 22157241 : if (!trivial_p && type_has_trivial_fn (type, kind))
3425 : 234500 : type_set_nontrivial_flag (type, kind);
3426 : :
3427 : : /* Create the function. */
3428 : 22157241 : tree this_type = cp_build_qualified_type (type, this_quals);
3429 : 22157241 : fn_type = build_method_type_directly (this_type, return_type,
3430 : : parameter_types);
3431 : :
3432 : 22157241 : if (raises)
3433 : : {
3434 : 22052424 : if (raises != error_mark_node)
3435 : 22052421 : fn_type = build_exception_variant (fn_type, raises);
3436 : : else
3437 : : {
3438 : : /* Can happen, e.g., in C++98 mode for an ill-formed non-static data
3439 : : member initializer (c++/89914). Also, in C++98, we might have
3440 : : failed to deduce RAISES, so try again but complain this time. */
3441 : 3 : if (cxx_dialect < cxx11)
3442 : 3 : synthesized_method_walk (type, kind, const_p, &raises, nullptr,
3443 : : nullptr, nullptr, /*diag=*/true,
3444 : : &inherited_ctor, inherited_parms);
3445 : : /* We should have seen an error at this point. */
3446 : 3 : gcc_assert (seen_error ());
3447 : : }
3448 : : }
3449 : 22157241 : fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
3450 : 22157241 : if (kind != sfk_inheriting_constructor)
3451 : 21892481 : DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
3452 : :
3453 : 22157241 : if (IDENTIFIER_OVL_OP_P (name))
3454 : : {
3455 : 3430196 : const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (name);
3456 : 3430196 : DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) = op->ovl_op_code;
3457 : 3430196 : }
3458 : 18727045 : else if (IDENTIFIER_CTOR_P (name))
3459 : 12971972 : DECL_CXX_CONSTRUCTOR_P (fn) = true;
3460 : 5755073 : else if (IDENTIFIER_DTOR_P (name))
3461 : 5755073 : DECL_CXX_DESTRUCTOR_P (fn) = true;
3462 : : else
3463 : 0 : gcc_unreachable ();
3464 : :
3465 : 22157241 : SET_DECL_ALIGN (fn, MINIMUM_METHOD_BOUNDARY);
3466 : :
3467 : : /* Create the explicit arguments. */
3468 : 22157241 : if (rhs_parm_type)
3469 : : {
3470 : : /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
3471 : : want its type to be included in the mangled function
3472 : : name. */
3473 : 12365101 : tree decl = cp_build_parm_decl (fn, NULL_TREE, rhs_parm_type);
3474 : 12365101 : TREE_READONLY (decl) = 1;
3475 : 12365101 : retrofit_lang_decl (decl);
3476 : 12365101 : DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
3477 : 12365101 : DECL_ARGUMENTS (fn) = decl;
3478 : : }
3479 : 9792140 : else if (kind == sfk_inheriting_constructor)
3480 : : {
3481 : 264760 : tree *p = &DECL_ARGUMENTS (fn);
3482 : 264760 : int index = 1;
3483 : 540748 : for (tree parm = inherited_parms; parm && parm != void_list_node;
3484 : 275988 : parm = TREE_CHAIN (parm))
3485 : : {
3486 : 275988 : *p = cp_build_parm_decl (fn, NULL_TREE, TREE_VALUE (parm));
3487 : 275988 : retrofit_lang_decl (*p);
3488 : 275988 : DECL_PARM_LEVEL (*p) = 1;
3489 : 275988 : DECL_PARM_INDEX (*p) = index++;
3490 : 275988 : p = &DECL_CHAIN (*p);
3491 : : }
3492 : 264760 : SET_DECL_INHERITED_CTOR (fn, inherited_ctor);
3493 : 264760 : DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor);
3494 : : /* A constructor so declared has the same access as the corresponding
3495 : : constructor in X. */
3496 : 264760 : TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor);
3497 : 264760 : TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor);
3498 : : /* Copy constexpr from the inherited constructor even if the
3499 : : inheriting constructor doesn't satisfy the requirements. */
3500 : 264760 : constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
3501 : 264760 : tree inherited_ctor_fn = STRIP_TEMPLATE (inherited_ctor);
3502 : : /* Also copy any attributes. */
3503 : 264760 : DECL_ATTRIBUTES (fn) = clone_attrs (DECL_ATTRIBUTES (inherited_ctor_fn));
3504 : 264760 : DECL_DISREGARD_INLINE_LIMITS (fn)
3505 : 264760 : = DECL_DISREGARD_INLINE_LIMITS (inherited_ctor_fn);
3506 : : }
3507 : :
3508 : : /* Add the "this" parameter. */
3509 : 22157241 : this_parm = build_this_parm (fn, fn_type, this_quals);
3510 : 22157241 : DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
3511 : 22157241 : DECL_ARGUMENTS (fn) = this_parm;
3512 : :
3513 : 38559409 : grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
3514 : :
3515 : 22157241 : DECL_IN_AGGR_P (fn) = 1;
3516 : 22157241 : DECL_ARTIFICIAL (fn) = 1;
3517 : 22157241 : DECL_DEFAULTED_FN (fn) = 1;
3518 : 22157241 : if (cxx_dialect >= cxx11)
3519 : : {
3520 : 22134974 : DECL_DELETED_FN (fn) = deleted_p;
3521 : 22134974 : DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
3522 : : }
3523 : 22157241 : DECL_EXTERNAL (fn) = true;
3524 : 22157241 : DECL_NOT_REALLY_EXTERN (fn) = 1;
3525 : 22157241 : DECL_DECLARED_INLINE_P (fn) = 1;
3526 : 22157241 : set_linkage_according_to_type (type, fn);
3527 : 22157241 : if (TREE_PUBLIC (fn))
3528 : 22098427 : DECL_COMDAT (fn) = 1;
3529 : 22157241 : rest_of_decl_compilation (fn, namespace_bindings_p (), at_eof);
3530 : 22157241 : gcc_assert (!TREE_USED (fn));
3531 : :
3532 : : /* Propagate constraints from the inherited constructor. */
3533 : 22157241 : if (flag_concepts && inherited_ctor)
3534 : 100360 : if (tree orig_ci = get_constraints (inherited_ctor))
3535 : : {
3536 : 2376 : tree new_ci = copy_node (orig_ci);
3537 : 2376 : set_constraints (fn, new_ci);
3538 : : }
3539 : :
3540 : : /* Restore PROCESSING_TEMPLATE_DECL. */
3541 : 22157241 : processing_template_decl = saved_processing_template_decl;
3542 : :
3543 : 22157241 : if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
3544 : 49121 : fn = add_inherited_template_parms (fn, inherited_ctor);
3545 : :
3546 : : /* Warn about calling a non-trivial move assignment in a virtual base. */
3547 : 1127527 : if (kind == sfk_move_assignment && !deleted_p && !trivial_p
3548 : 22441679 : && CLASSTYPE_VBASECLASSES (type))
3549 : : {
3550 : 67 : location_t loc = input_location;
3551 : 67 : input_location = DECL_SOURCE_LOCATION (fn);
3552 : 67 : synthesized_method_walk (type, kind, const_p,
3553 : : NULL, NULL, NULL, NULL, true,
3554 : : NULL, NULL_TREE);
3555 : 67 : input_location = loc;
3556 : : }
3557 : :
3558 : : return fn;
3559 : : }
3560 : :
3561 : : /* Mark an explicitly defaulted function FN as =deleted and warn.
3562 : : IMPLICIT_FN is the corresponding special member function that
3563 : : would have been implicitly declared. */
3564 : :
3565 : : void
3566 : 117 : maybe_delete_defaulted_fn (tree fn, tree implicit_fn)
3567 : : {
3568 : 117 : if (DECL_ARTIFICIAL (fn) || !DECL_DEFAULTED_IN_CLASS_P (fn))
3569 : 5 : return;
3570 : :
3571 : 117 : DECL_DELETED_FN (fn) = true;
3572 : :
3573 : 117 : auto_diagnostic_group d;
3574 : 117 : const special_function_kind kind = special_function_p (fn);
3575 : 117 : tree parmtype
3576 : 117 : = TREE_VALUE (DECL_XOBJ_MEMBER_FUNCTION_P (fn)
3577 : : ? TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)))
3578 : : : FUNCTION_FIRST_USER_PARMTYPE (fn));
3579 : 117 : const bool illformed_p
3580 : : /* [dcl.fct.def.default] "if F1 is an assignment operator"... */
3581 : 117 : = (SFK_ASSIGN_P (kind)
3582 : : /* "and the return type of F1 differs from the return type of F2" */
3583 : 117 : && (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
3584 : : TREE_TYPE (TREE_TYPE (implicit_fn)))
3585 : : /* "or F1's non-object parameter type is not a reference,
3586 : : the program is ill-formed" */
3587 : 43 : || !TYPE_REF_P (parmtype)));
3588 : : /* Decide if we want to emit a pedwarn, error, or a warning. */
3589 : 95 : diagnostic_t diag_kind;
3590 : 95 : int opt;
3591 : 95 : if (illformed_p)
3592 : : {
3593 : : diag_kind = DK_ERROR;
3594 : : opt = 0;
3595 : : }
3596 : : else
3597 : : {
3598 : 95 : diag_kind = cxx_dialect >= cxx20 ? DK_WARNING : DK_PEDWARN;
3599 : : opt = OPT_Wdefaulted_function_deleted;
3600 : : }
3601 : :
3602 : : /* Don't warn for template instantiations. */
3603 : 117 : if (DECL_TEMPLATE_INSTANTIATION (fn) && diag_kind == DK_WARNING)
3604 : 5 : return;
3605 : :
3606 : 112 : const char *wmsg;
3607 : 112 : switch (kind)
3608 : : {
3609 : : case sfk_copy_constructor:
3610 : : wmsg = G_("explicitly defaulted copy constructor is implicitly deleted "
3611 : : "because its declared type does not match the type of an "
3612 : : "implicit copy constructor");
3613 : : break;
3614 : 23 : case sfk_move_constructor:
3615 : 23 : wmsg = G_("explicitly defaulted move constructor is implicitly deleted "
3616 : : "because its declared type does not match the type of an "
3617 : : "implicit move constructor");
3618 : 23 : break;
3619 : 32 : case sfk_copy_assignment:
3620 : 32 : wmsg = G_("explicitly defaulted copy assignment operator is implicitly "
3621 : : "deleted because its declared type does not match the type "
3622 : : "of an implicit copy assignment operator");
3623 : 32 : break;
3624 : 24 : case sfk_move_assignment:
3625 : 24 : wmsg = G_("explicitly defaulted move assignment operator is implicitly "
3626 : : "deleted because its declared type does not match the type "
3627 : : "of an implicit move assignment operator");
3628 : 24 : break;
3629 : 0 : default:
3630 : 0 : gcc_unreachable ();
3631 : : }
3632 : 112 : if (emit_diagnostic (diag_kind, DECL_SOURCE_LOCATION (fn), opt, wmsg))
3633 : 88 : inform (DECL_SOURCE_LOCATION (fn),
3634 : : "expected signature: %qD", implicit_fn);
3635 : 117 : }
3636 : :
3637 : : /* Gives any errors about defaulted functions which need to be deferred
3638 : : until the containing class is complete. IMP_CONST is false or true
3639 : : if we are called from check_bases_and_members and signals whether
3640 : : the implicit function has a non-object parameter of type const C&. */
3641 : :
3642 : : void
3643 : 5393432 : defaulted_late_check (tree fn, tristate imp_const/*=tristate::unknown()*/)
3644 : : {
3645 : : /* Complain about invalid signature for defaulted fn. */
3646 : 5393432 : tree ctx = DECL_CONTEXT (fn);
3647 : 5393432 : special_function_kind kind = special_function_p (fn);
3648 : :
3649 : 5393432 : if (kind == sfk_comparison)
3650 : : {
3651 : : /* If the function was declared constexpr, check that the definition
3652 : : qualifies. Otherwise we can define the function lazily. */
3653 : 11365 : if (DECL_DECLARED_CONSTEXPR_P (fn) && !DECL_INITIAL (fn))
3654 : : {
3655 : : /* Prevent GC. */
3656 : 8803 : function_depth++;
3657 : 8803 : synthesize_method (fn);
3658 : 8803 : function_depth--;
3659 : : }
3660 : 15836 : return;
3661 : : }
3662 : :
3663 : 5382067 : bool fn_const_p = (copy_fn_p (fn) == 2);
3664 : : /* "if F2 has a non-object parameter of type const C&, the corresponding
3665 : : non-object parameter of F1 may be of type C&." But not the other way
3666 : : around. */
3667 : 5382067 : if (fn_const_p && imp_const.is_false ())
3668 : : fn_const_p = false;
3669 : 5382067 : tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
3670 : : /*pattern_fn=*/NULL_TREE,
3671 : : /*inherited_parms=*/NULL_TREE);
3672 : 5382067 : tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
3673 : :
3674 : : /* Includes special handling for a default xobj operator. */
3675 : 10764120 : auto compare_fn_params = [](tree fn, tree implicit_fn){
3676 : 5382053 : tree fn_parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
3677 : 5382053 : tree implicit_fn_parms = TYPE_ARG_TYPES (TREE_TYPE (implicit_fn));
3678 : :
3679 : 5382053 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
3680 : : {
3681 : 6 : tree fn_obj_ref_type = TREE_VALUE (fn_parms);
3682 : : /* We can't default xobj operators with an xobj parameter that is not
3683 : : an lvalue reference, even if it would correspond. */
3684 : 6 : if (!TYPE_REF_P (fn_obj_ref_type)
3685 : 6 : || TYPE_REF_IS_RVALUE (fn_obj_ref_type)
3686 : 12 : || !object_parms_correspond (fn, implicit_fn,
3687 : 6 : DECL_CONTEXT (implicit_fn)))
3688 : 0 : return false;
3689 : : /* We just compared the object parameters, skip over them before
3690 : : passing to compparms. */
3691 : 6 : fn_parms = TREE_CHAIN (fn_parms);
3692 : 6 : implicit_fn_parms = TREE_CHAIN (implicit_fn_parms);
3693 : : }
3694 : 5382053 : return compparms (fn_parms, implicit_fn_parms);
3695 : : };
3696 : :
3697 : 5382067 : if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
3698 : : TREE_TYPE (TREE_TYPE (implicit_fn)))
3699 : 5382067 : || !compare_fn_params (fn, implicit_fn))
3700 : 117 : maybe_delete_defaulted_fn (fn, implicit_fn);
3701 : :
3702 : 5382067 : if (DECL_DELETED_FN (implicit_fn))
3703 : : {
3704 : 4471 : DECL_DELETED_FN (fn) = 1;
3705 : 4471 : return;
3706 : : }
3707 : :
3708 : : /* If a function is explicitly defaulted on its first declaration without an
3709 : : exception-specification, it is implicitly considered to have the same
3710 : : exception-specification as if it had been implicitly declared. */
3711 : 5377596 : if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn))
3712 : 5377596 : && DECL_DEFAULTED_IN_CLASS_P (fn))
3713 : 3619186 : TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
3714 : :
3715 : 10754664 : if (DECL_DEFAULTED_IN_CLASS_P (fn)
3716 : 10754664 : && DECL_DECLARED_CONSTEXPR_P (implicit_fn))
3717 : : {
3718 : : /* Hmm...should we do this for out-of-class too? Should it be OK to
3719 : : add constexpr later like inline, rather than requiring
3720 : : declarations to match? */
3721 : 2953469 : DECL_DECLARED_CONSTEXPR_P (fn) = true;
3722 : 2953469 : if (kind == sfk_constructor)
3723 : 911405 : TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
3724 : : }
3725 : :
3726 : 5377596 : if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
3727 : 5377596 : && DECL_DECLARED_CONSTEXPR_P (fn))
3728 : : {
3729 : 93464 : if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
3730 : : {
3731 : 17 : auto_diagnostic_group d;
3732 : 17 : error ("explicitly defaulted function %q+D cannot be declared "
3733 : : "%qs because the implicit declaration is not %qs:", fn,
3734 : 34 : DECL_IMMEDIATE_FUNCTION_P (fn) ? "consteval" : "constexpr",
3735 : : "constexpr");
3736 : 17 : explain_implicit_non_constexpr (fn);
3737 : 17 : }
3738 : 93464 : DECL_DECLARED_CONSTEXPR_P (fn) = false;
3739 : : }
3740 : : }
3741 : :
3742 : : /* Returns true iff FN can be explicitly defaulted, and gives any
3743 : : errors if defaulting FN is ill-formed. */
3744 : :
3745 : : bool
3746 : 5667540 : defaultable_fn_check (tree fn)
3747 : : {
3748 : 5667540 : special_function_kind kind = sfk_none;
3749 : :
3750 : 5667540 : if (template_parm_scope_p ())
3751 : : {
3752 : 3 : error ("a template cannot be defaulted");
3753 : 3 : return false;
3754 : : }
3755 : :
3756 : 11335074 : if (DECL_CONSTRUCTOR_P (fn))
3757 : : {
3758 : 3463658 : if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
3759 : : kind = sfk_constructor;
3760 : 1908587 : else if (copy_fn_p (fn) > 0
3761 : 1908587 : && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
3762 : 1044717 : == void_list_node))
3763 : : kind = sfk_copy_constructor;
3764 : 863873 : else if (move_fn_p (fn))
3765 : : kind = sfk_move_constructor;
3766 : : }
3767 : 2203879 : else if (DECL_DESTRUCTOR_P (fn))
3768 : : kind = sfk_destructor;
3769 : 1541990 : else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
3770 : 1541990 : && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR))
3771 : : {
3772 : 1485890 : if (copy_fn_p (fn))
3773 : : kind = sfk_copy_assignment;
3774 : 581310 : else if (move_fn_p (fn))
3775 : : kind = sfk_move_assignment;
3776 : : }
3777 : 56100 : else if (DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) >= OVL_OP_EQ_EXPR
3778 : 56100 : && DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) <= OVL_OP_SPACESHIP_EXPR)
3779 : : {
3780 : 56085 : kind = sfk_comparison;
3781 : 56085 : if (!early_check_defaulted_comparison (fn))
3782 : : return false;
3783 : : }
3784 : :
3785 : : /* FIXME: We need to check for xobj member functions here to give better
3786 : : diagnostics for weird cases where unrelated xobj parameters are given.
3787 : : We just want to do better than 'cannot be defaulted'. */
3788 : :
3789 : : if (kind == sfk_none)
3790 : : {
3791 : 21 : error ("%qD cannot be defaulted", fn);
3792 : 21 : return false;
3793 : : }
3794 : : else
3795 : : {
3796 : 5667437 : for (tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
3797 : 9166282 : t && t != void_list_node; t = TREE_CHAIN (t))
3798 : 3498848 : if (TREE_PURPOSE (t))
3799 : : {
3800 : 3 : error ("defaulted function %q+D with default argument", fn);
3801 : 3 : break;
3802 : : }
3803 : :
3804 : : /* Avoid do_warn_unused_parameter warnings. */
3805 : 9166285 : for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p))
3806 : 3498848 : if (DECL_NAME (p))
3807 : 197384 : suppress_warning (p, OPT_Wunused_parameter);
3808 : :
3809 : 5667437 : if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
3810 : : /* Defer checking. */;
3811 : 22456 : else if (!processing_template_decl)
3812 : 540 : defaulted_late_check (fn);
3813 : :
3814 : 5667437 : return true;
3815 : : }
3816 : : }
3817 : :
3818 : : /* Add an implicit declaration to TYPE for the kind of function
3819 : : indicated by SFK. Return the FUNCTION_DECL for the new implicit
3820 : : declaration. */
3821 : :
3822 : : tree
3823 : 16510414 : lazily_declare_fn (special_function_kind sfk, tree type)
3824 : : {
3825 : 16510414 : tree fn;
3826 : : /* Whether or not the argument has a const reference type. */
3827 : 16510414 : bool const_p = false;
3828 : :
3829 : 16510414 : type = TYPE_MAIN_VARIANT (type);
3830 : :
3831 : 16510414 : switch (sfk)
3832 : : {
3833 : 2285992 : case sfk_constructor:
3834 : 2285992 : CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
3835 : 2285992 : break;
3836 : 4037684 : case sfk_copy_constructor:
3837 : 4037684 : const_p = TYPE_HAS_CONST_COPY_CTOR (type);
3838 : 4037684 : CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
3839 : 4037684 : break;
3840 : 3128634 : case sfk_move_constructor:
3841 : 3128634 : CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
3842 : 3128634 : break;
3843 : 1237196 : case sfk_copy_assignment:
3844 : 1237196 : const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
3845 : 1237196 : CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
3846 : 1237196 : break;
3847 : 817484 : case sfk_move_assignment:
3848 : 817484 : CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
3849 : 817484 : break;
3850 : 5003424 : case sfk_destructor:
3851 : 5003424 : CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
3852 : 5003424 : break;
3853 : 0 : default:
3854 : 0 : gcc_unreachable ();
3855 : : }
3856 : :
3857 : : /* Declare the function. */
3858 : 16510414 : fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL);
3859 : :
3860 : : /* [class.copy]/8 If the class definition declares a move constructor or
3861 : : move assignment operator, the implicitly declared copy constructor is
3862 : : defined as deleted.... */
3863 : 16510414 : if ((sfk == sfk_copy_assignment || sfk == sfk_copy_constructor)
3864 : 5274880 : && cxx_dialect >= cxx11)
3865 : : {
3866 : 5261395 : if (classtype_has_move_assign_or_move_ctor_p (type, true))
3867 : 722734 : DECL_DELETED_FN (fn) = true;
3868 : 4538661 : else if (classtype_has_depr_implicit_copy (type))
3869 : : /* The implicit definition of a copy constructor as defaulted is
3870 : : deprecated if the class has a user-declared copy assignment operator
3871 : : or a user-declared destructor. The implicit definition of a copy
3872 : : assignment operator as defaulted is deprecated if the class has a
3873 : : user-declared copy constructor or a user-declared destructor (15.4,
3874 : : 15.8). */
3875 : 578877 : TREE_DEPRECATED (fn) = true;
3876 : : }
3877 : :
3878 : : /* Destructors and assignment operators may be virtual. */
3879 : 16510414 : if (sfk == sfk_destructor
3880 : 16510414 : || sfk == sfk_move_assignment
3881 : 10689506 : || sfk == sfk_copy_assignment)
3882 : 7058104 : check_for_override (fn, type);
3883 : :
3884 : : /* Add it to the class */
3885 : 16510414 : bool added = add_method (type, fn, false);
3886 : 16510414 : gcc_assert (added || errorcount);
3887 : :
3888 : : /* Add it to TYPE_FIELDS. */
3889 : 16510414 : if (sfk == sfk_destructor
3890 : 16510414 : && DECL_VIRTUAL_P (fn))
3891 : : /* The ABI requires that a virtual destructor go at the end of the
3892 : : vtable. */
3893 : 169658 : TYPE_FIELDS (type) = chainon (TYPE_FIELDS (type), fn);
3894 : : else
3895 : : {
3896 : 16340756 : DECL_CHAIN (fn) = TYPE_FIELDS (type);
3897 : 16340756 : TYPE_FIELDS (type) = fn;
3898 : : }
3899 : : /* Propagate TYPE_FIELDS. */
3900 : 16510414 : fixup_type_variants (type);
3901 : :
3902 : 16510414 : maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
3903 : 16510414 : if (DECL_MAYBE_IN_CHARGE_CDTOR_P (fn))
3904 : : /* Create appropriate clones. */
3905 : 14455734 : clone_cdtor (fn, /*update_methods=*/true);
3906 : :
3907 : : /* Classes, structs or unions TYPE marked with hotness attributes propagate
3908 : : the attribute to all methods. This is typically done in
3909 : : check_bases_and_members, but we must also inject them here for deferred
3910 : : lazily-declared functions. */
3911 : 16510414 : maybe_propagate_warmth_attributes (fn, type);
3912 : :
3913 : 16510414 : return fn;
3914 : : }
3915 : :
3916 : : /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
3917 : : as there are artificial parms in FN. */
3918 : :
3919 : : tree
3920 : 2548680230 : skip_artificial_parms_for (const_tree fn, tree list)
3921 : : {
3922 : 2548680230 : if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
3923 : 1453547727 : list = TREE_CHAIN (list);
3924 : : else
3925 : : return list;
3926 : :
3927 : 1453547727 : if (DECL_HAS_IN_CHARGE_PARM_P (fn))
3928 : 9935964 : list = TREE_CHAIN (list);
3929 : 1453547727 : if (DECL_HAS_VTT_PARM_P (fn))
3930 : 12766024 : list = TREE_CHAIN (list);
3931 : : return list;
3932 : : }
3933 : :
3934 : : /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
3935 : : artificial parms in FN. */
3936 : :
3937 : : int
3938 : 310182349 : num_artificial_parms_for (const_tree fn)
3939 : : {
3940 : 310182349 : int count = 0;
3941 : :
3942 : 310182349 : if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
3943 : 280225959 : count++;
3944 : : else
3945 : : return 0;
3946 : :
3947 : 280225959 : if (DECL_HAS_IN_CHARGE_PARM_P (fn))
3948 : 1771 : count++;
3949 : 280225959 : if (DECL_HAS_VTT_PARM_P (fn))
3950 : 109925 : count++;
3951 : : return count;
3952 : : }
3953 : :
3954 : :
3955 : : #include "gt-cp-method.h"
|