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 : 96471 : init_method (void)
45 : : {
46 : 96471 : init_mangle ();
47 : 96471 : }
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 : 1069280 : make_thunk (tree function, bool this_adjusting,
60 : : tree fixed_offset, tree virtual_offset)
61 : : {
62 : 1069280 : HOST_WIDE_INT d;
63 : 1069280 : tree thunk;
64 : :
65 : 1069280 : gcc_assert (TREE_CODE (function) == FUNCTION_DECL);
66 : : /* We can have this thunks to covariant thunks, but not vice versa. */
67 : 1069280 : gcc_assert (!DECL_THIS_THUNK_P (function));
68 : 1069280 : gcc_assert (!DECL_RESULT_THUNK_P (function) || this_adjusting);
69 : :
70 : : /* Scale the VIRTUAL_OFFSET to be in terms of bytes. */
71 : 1069280 : if (this_adjusting && virtual_offset)
72 : 863790 : virtual_offset
73 : 863790 : = size_binop (MULT_EXPR,
74 : : virtual_offset,
75 : : convert (ssizetype,
76 : : TYPE_SIZE_UNIT (vtable_entry_type)));
77 : :
78 : 1069280 : 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 : 2343092 : for (thunk = DECL_THUNKS (function); thunk; thunk = DECL_CHAIN (thunk))
84 : 776159 : if (DECL_THIS_THUNK_P (thunk) == this_adjusting
85 : 776156 : && THUNK_FIXED_OFFSET (thunk) == d
86 : 572085 : && !virtual_offset == !THUNK_VIRTUAL_OFFSET (thunk)
87 : 1348244 : && (!virtual_offset
88 : 491545 : || (this_adjusting
89 : 491545 : ? tree_int_cst_equal (THUNK_VIRTUAL_OFFSET (thunk),
90 : : virtual_offset)
91 : 184 : : THUNK_VIRTUAL_OFFSET (thunk) == virtual_offset)))
92 : 571627 : 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 : 497653 : 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 : 497653 : gcc_assert (TYPE_SIZE (DECL_CONTEXT (function))
101 : : && TYPE_BEING_DEFINED (DECL_CONTEXT (function)));
102 : :
103 : 497653 : thunk = build_decl (DECL_SOURCE_LOCATION (function),
104 : 497653 : FUNCTION_DECL, NULL_TREE, TREE_TYPE (function));
105 : 497653 : DECL_LANG_SPECIFIC (thunk) = DECL_LANG_SPECIFIC (function);
106 : 497653 : cxx_dup_lang_specific_decl (thunk);
107 : 497653 : DECL_VIRTUAL_P (thunk) = true;
108 : 497653 : SET_DECL_THUNKS (thunk, NULL_TREE);
109 : :
110 : 497653 : DECL_CONTEXT (thunk) = DECL_CONTEXT (function);
111 : 497653 : TREE_READONLY (thunk) = TREE_READONLY (function);
112 : 497653 : TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (function);
113 : 497653 : TREE_PUBLIC (thunk) = TREE_PUBLIC (function);
114 : 497653 : SET_DECL_THUNK_P (thunk, this_adjusting);
115 : 497653 : THUNK_TARGET (thunk) = function;
116 : 497653 : THUNK_FIXED_OFFSET (thunk) = d;
117 : 497653 : THUNK_VIRTUAL_OFFSET (thunk) = virtual_offset;
118 : 497653 : THUNK_ALIAS (thunk) = NULL_TREE;
119 : :
120 : 497653 : DECL_INTERFACE_KNOWN (thunk) = 1;
121 : 497653 : DECL_NOT_REALLY_EXTERN (thunk) = 1;
122 : 497653 : DECL_COMDAT (thunk) = DECL_COMDAT (function);
123 : 497653 : 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 : 497653 : DECL_CXX_DESTRUCTOR_P (thunk) = 0;
127 : 497653 : DECL_CXX_CONSTRUCTOR_P (thunk) = 0;
128 : 497653 : DECL_EXTERNAL (thunk) = 1;
129 : 497653 : DECL_ARTIFICIAL (thunk) = 1;
130 : : /* The THUNK is not a pending inline, even if the FUNCTION is. */
131 : 497653 : DECL_PENDING_INLINE_P (thunk) = 0;
132 : 497653 : DECL_DECLARED_INLINE_P (thunk) = 0;
133 : : /* Nor is it a template instantiation. */
134 : 497653 : DECL_USE_TEMPLATE (thunk) = 0;
135 : 497653 : DECL_TEMPLATE_INFO (thunk) = NULL;
136 : :
137 : : /* Add it to the list of thunks associated with FUNCTION. */
138 : 497653 : DECL_CHAIN (thunk) = DECL_THUNKS (function);
139 : 497653 : SET_DECL_THUNKS (function, thunk);
140 : :
141 : 497653 : return thunk;
142 : : }
143 : :
144 : : /* Finish THUNK, a thunk decl. */
145 : :
146 : : void
147 : 497653 : finish_thunk (tree thunk)
148 : : {
149 : 497653 : tree function, name;
150 : 497653 : tree fixed_offset = ssize_int (THUNK_FIXED_OFFSET (thunk));
151 : 497653 : tree virtual_offset = THUNK_VIRTUAL_OFFSET (thunk);
152 : :
153 : 497653 : gcc_assert (!DECL_NAME (thunk) && DECL_THUNK_P (thunk));
154 : 497653 : if (virtual_offset && DECL_RESULT_THUNK_P (thunk))
155 : 142 : virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
156 : 497653 : function = THUNK_TARGET (thunk);
157 : 497867 : 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 : 497653 : 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 : 497653 : DECL_NAME (thunk) = name;
181 : 497653 : SET_DECL_ASSEMBLER_NAME (thunk, name);
182 : 497653 : }
183 : :
184 : : static GTY (()) int thunk_labelno;
185 : :
186 : : /* Create a static alias to target. */
187 : :
188 : : tree
189 : 17536 : make_alias_for (tree target, tree newid)
190 : : {
191 : 17536 : tree alias = build_decl (DECL_SOURCE_LOCATION (target),
192 : 17536 : TREE_CODE (target), newid, TREE_TYPE (target));
193 : 17536 : DECL_LANG_SPECIFIC (alias) = DECL_LANG_SPECIFIC (target);
194 : 17536 : cxx_dup_lang_specific_decl (alias);
195 : 17536 : DECL_CONTEXT (alias) = DECL_CONTEXT (target);
196 : 17536 : TREE_READONLY (alias) = TREE_READONLY (target);
197 : 17536 : TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (target);
198 : 17536 : TREE_PUBLIC (alias) = 0;
199 : 17536 : DECL_INTERFACE_KNOWN (alias) = 1;
200 : 17536 : if (DECL_LANG_SPECIFIC (alias))
201 : : {
202 : 17536 : DECL_NOT_REALLY_EXTERN (alias) = 1;
203 : 17536 : DECL_USE_TEMPLATE (alias) = 0;
204 : 17536 : DECL_TEMPLATE_INFO (alias) = NULL;
205 : : }
206 : 17536 : DECL_EXTERNAL (alias) = 0;
207 : 17536 : DECL_ARTIFICIAL (alias) = 1;
208 : 17536 : DECL_TEMPLATE_INSTANTIATED (alias) = 0;
209 : 17536 : if (TREE_CODE (alias) == FUNCTION_DECL)
210 : : {
211 : 17493 : DECL_SAVED_AUTO_RETURN_TYPE (alias) = NULL;
212 : 17493 : DECL_CXX_DESTRUCTOR_P (alias) = 0;
213 : 17493 : DECL_CXX_CONSTRUCTOR_P (alias) = 0;
214 : 17493 : DECL_PENDING_INLINE_P (alias) = 0;
215 : 17493 : DECL_DECLARED_INLINE_P (alias) = 0;
216 : 17493 : DECL_INITIAL (alias) = error_mark_node;
217 : 17493 : DECL_ARGUMENTS (alias) = copy_list (DECL_ARGUMENTS (target));
218 : : }
219 : : else
220 : 43 : TREE_STATIC (alias) = 1;
221 : 17536 : TREE_ADDRESSABLE (alias) = 1;
222 : 17536 : TREE_USED (alias) = 1;
223 : 17536 : SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
224 : 17536 : return alias;
225 : : }
226 : :
227 : : static tree
228 : 4402 : make_alias_for_thunk (tree function)
229 : : {
230 : 4402 : tree alias;
231 : 4402 : char buf[256];
232 : :
233 : 4402 : targetm.asm_out.generate_internal_label (buf, "LTHUNK", thunk_labelno);
234 : 4402 : thunk_labelno++;
235 : :
236 : 4402 : alias = make_alias_for (function, get_identifier (buf));
237 : :
238 : 4402 : if (!flag_syntax_only)
239 : : {
240 : 4402 : struct cgraph_node *funcn, *aliasn;
241 : 4402 : funcn = cgraph_node::get (function);
242 : 4402 : gcc_checking_assert (funcn);
243 : 4402 : aliasn = cgraph_node::create_same_body_alias (alias, function);
244 : 4402 : DECL_ASSEMBLER_NAME (function);
245 : 4402 : gcc_assert (aliasn != NULL);
246 : : }
247 : :
248 : 4402 : 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 : 8497 : use_thunk (tree thunk_fndecl, bool emit_p)
257 : : {
258 : 8497 : tree a, t, function, alias;
259 : 8497 : tree virtual_offset;
260 : 8497 : HOST_WIDE_INT fixed_offset, virtual_value;
261 : 8497 : bool this_adjusting = DECL_THIS_THUNK_P (thunk_fndecl);
262 : 8497 : struct cgraph_node *funcn, *thunk_node;
263 : :
264 : : /* We should have called finish_thunk to give it a name. */
265 : 8497 : gcc_assert (DECL_NAME (thunk_fndecl));
266 : :
267 : : /* We should never be using an alias, always refer to the
268 : : aliased thunk. */
269 : 8497 : gcc_assert (!THUNK_ALIAS (thunk_fndecl));
270 : :
271 : 8497 : if (TREE_ASM_WRITTEN (thunk_fndecl))
272 : : return;
273 : :
274 : 4507 : function = THUNK_TARGET (thunk_fndecl);
275 : 4507 : 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 : 4507 : 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 : 4507 : TREE_ADDRESSABLE (thunk_fndecl) = 1;
286 : :
287 : : /* Don't diagnose deprecated or unavailable functions just because they
288 : : have thunks emitted for them. */
289 : 4507 : auto du = make_temp_override (deprecated_state,
290 : 4507 : UNAVAILABLE_DEPRECATED_SUPPRESS);
291 : :
292 : : /* Figure out what function is being thunked to. It's referenced in
293 : : this translation unit. */
294 : 4507 : TREE_ADDRESSABLE (function) = 1;
295 : 4507 : mark_used (function);
296 : 4507 : if (!emit_p)
297 : : return;
298 : :
299 : 4402 : if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function))
300 : 4402 : alias = make_alias_for_thunk (function);
301 : : else
302 : : alias = function;
303 : :
304 : 4402 : fixed_offset = THUNK_FIXED_OFFSET (thunk_fndecl);
305 : 4402 : virtual_offset = THUNK_VIRTUAL_OFFSET (thunk_fndecl);
306 : :
307 : 4402 : if (virtual_offset)
308 : : {
309 : 2865 : if (!this_adjusting)
310 : 112 : virtual_offset = BINFO_VPTR_FIELD (virtual_offset);
311 : 2865 : virtual_value = tree_to_shwi (virtual_offset);
312 : 2865 : 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 : 4402 : mark_used (thunk_fndecl);
319 : : /* This thunk is actually defined. */
320 : 4402 : DECL_EXTERNAL (thunk_fndecl) = 0;
321 : : /* The linkage of the function may have changed. FIXME in linkage
322 : : rewrite. */
323 : 4402 : gcc_assert (DECL_INTERFACE_KNOWN (function));
324 : 4402 : TREE_PUBLIC (thunk_fndecl) = TREE_PUBLIC (function);
325 : 4402 : DECL_VISIBILITY (thunk_fndecl) = DECL_VISIBILITY (function);
326 : 8804 : DECL_VISIBILITY_SPECIFIED (thunk_fndecl)
327 : 4402 : = DECL_VISIBILITY_SPECIFIED (function);
328 : 4402 : DECL_COMDAT (thunk_fndecl) = DECL_COMDAT (function);
329 : 4402 : DECL_WEAK (thunk_fndecl) = DECL_WEAK (function);
330 : :
331 : 4402 : if (flag_syntax_only)
332 : : {
333 : 0 : TREE_ASM_WRITTEN (thunk_fndecl) = 1;
334 : 0 : return;
335 : : }
336 : :
337 : 4402 : push_to_top_level ();
338 : :
339 : 4402 : if (TARGET_USE_LOCAL_THUNK_ALIAS_P (function)
340 : 4402 : && targetm_common.have_named_sections)
341 : : {
342 : 4402 : tree fn = function;
343 : 4402 : struct symtab_node *symbol;
344 : :
345 : 4402 : if ((symbol = symtab_node::get (function))
346 : 4402 : && symbol->alias)
347 : : {
348 : 260 : if (symbol->analyzed)
349 : 65 : fn = symtab_node::get (function)->ultimate_alias_target ()->decl;
350 : : else
351 : 195 : fn = symtab_node::get (function)->alias_target;
352 : : }
353 : 4402 : resolve_unique_section (fn, 0, flag_function_sections);
354 : :
355 : 4402 : if (DECL_SECTION_NAME (fn) != NULL && DECL_ONE_ONLY (fn))
356 : : {
357 : 3668 : resolve_unique_section (thunk_fndecl, 0, flag_function_sections);
358 : :
359 : : /* Output the thunk into the same section as function. */
360 : 3668 : set_decl_section_name (thunk_fndecl, fn);
361 : 7336 : symtab_node::get (thunk_fndecl)->implicit_section
362 : 3668 : = symtab_node::get (fn)->implicit_section;
363 : : }
364 : : }
365 : :
366 : : /* Set up cloned argument trees for the thunk. */
367 : 4402 : t = NULL_TREE;
368 : 9413 : for (a = DECL_ARGUMENTS (function); a; a = DECL_CHAIN (a))
369 : : {
370 : 5011 : tree x = copy_node (a);
371 : 5011 : DECL_CHAIN (x) = t;
372 : 5011 : DECL_CONTEXT (x) = thunk_fndecl;
373 : 5011 : SET_DECL_RTL (x, NULL);
374 : 5011 : DECL_HAS_VALUE_EXPR_P (x) = 0;
375 : 5011 : TREE_ADDRESSABLE (x) = 0;
376 : 5011 : t = x;
377 : : }
378 : 4402 : a = nreverse (t);
379 : 4402 : DECL_ARGUMENTS (thunk_fndecl) = a;
380 : 4402 : TREE_ASM_WRITTEN (thunk_fndecl) = 1;
381 : 4402 : funcn = cgraph_node::get (function);
382 : 4402 : gcc_checking_assert (funcn);
383 : 4402 : thunk_node = funcn->create_thunk (thunk_fndecl, function,
384 : : this_adjusting, fixed_offset, virtual_value,
385 : : 0, virtual_offset, alias);
386 : 4402 : if (DECL_ONE_ONLY (function))
387 : 3668 : thunk_node->add_to_same_comdat_group (funcn);
388 : :
389 : 4402 : pop_from_top_level ();
390 : 4507 : }
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 : 60008362 : type_has_trivial_fn (tree ctype, special_function_kind sfk)
398 : : {
399 : 60008362 : switch (sfk)
400 : : {
401 : 8873359 : case sfk_constructor:
402 : 8873359 : return !TYPE_HAS_COMPLEX_DFLT (ctype);
403 : 11587629 : case sfk_copy_constructor:
404 : 11587629 : return !TYPE_HAS_COMPLEX_COPY_CTOR (ctype);
405 : 8298112 : case sfk_move_constructor:
406 : 8298112 : return !TYPE_HAS_COMPLEX_MOVE_CTOR (ctype);
407 : 5622632 : case sfk_copy_assignment:
408 : 5622632 : return !TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype);
409 : 3727059 : case sfk_move_assignment:
410 : 3727059 : return !TYPE_HAS_COMPLEX_MOVE_ASSIGN (ctype);
411 : 21281068 : case sfk_destructor:
412 : 21281068 : case sfk_virtual_destructor:
413 : 21281068 : 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 : 242655 : type_set_nontrivial_flag (tree ctype, special_function_kind sfk)
427 : : {
428 : 242655 : switch (sfk)
429 : : {
430 : 148156 : case sfk_constructor:
431 : 148156 : TYPE_HAS_COMPLEX_DFLT (ctype) = true;
432 : 148156 : return;
433 : 3 : case sfk_copy_constructor:
434 : 3 : TYPE_HAS_COMPLEX_COPY_CTOR (ctype) = true;
435 : 3 : return;
436 : 93972 : case sfk_move_constructor:
437 : 93972 : TYPE_HAS_COMPLEX_MOVE_CTOR (ctype) = true;
438 : 93972 : return;
439 : 265 : case sfk_copy_assignment:
440 : 265 : TYPE_HAS_COMPLEX_COPY_ASSIGN (ctype) = true;
441 : 265 : 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 : 242257734 : trivial_fn_p (tree fn)
458 : : {
459 : 242257734 : if (TREE_CODE (fn) == TEMPLATE_DECL)
460 : : return false;
461 : 242257734 : if (!DECL_DEFAULTED_FN (fn))
462 : : return false;
463 : :
464 : : /* If fn is a clone, get the primary variant. */
465 : 29396058 : if (tree prim = DECL_CLONED_FUNCTION (fn))
466 : 23051736 : fn = prim;
467 : 29396058 : 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 : 80017 : forward_parm (tree parm)
475 : : {
476 : 80017 : tree exp = convert_from_reference (parm);
477 : 80017 : tree type = TREE_TYPE (parm);
478 : 80017 : if (DECL_PACK_P (parm))
479 : 581 : type = PACK_EXPANSION_PATTERN (type);
480 : 80017 : if (!TYPE_REF_P (type))
481 : 64831 : type = cp_build_reference_type (type, /*rval=*/true);
482 : 80017 : warning_sentinel w (warn_useless_cast);
483 : 80017 : exp = build_static_cast (input_location, type, exp,
484 : : tf_warning_or_error);
485 : 80017 : if (DECL_PACK_P (parm))
486 : 581 : exp = make_pack_expansion (exp);
487 : 80017 : return exp;
488 : 80017 : }
489 : :
490 : : /* Strip all inheriting constructors, if any, to return the original
491 : : constructor from a (possibly indirect) base class. */
492 : :
493 : : tree
494 : 784651516 : strip_inheriting_ctors (tree dfn)
495 : : {
496 : 784651516 : if (!flag_new_inheriting_ctors)
497 : : return dfn;
498 : : tree fn = dfn;
499 : 1493051729 : while (tree inh = DECL_INHERITED_CTOR (fn))
500 : 785613988 : fn = OVL_FIRST (inh);
501 : :
502 : 784607871 : if (TREE_CODE (fn) == TEMPLATE_DECL
503 : 474264748 : && TREE_CODE (dfn) == FUNCTION_DECL)
504 : 6083 : 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 : 66914 : inherited_ctor_binfo_1 (tree binfo, tree fndecl)
514 : : {
515 : 66914 : tree base = DECL_CONTEXT (fndecl);
516 : 66914 : tree base_binfo;
517 : 67404 : for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
518 : 67404 : if (BINFO_TYPE (base_binfo) == base)
519 : 66914 : 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 : 125939 : inherited_ctor_binfo (tree binfo, tree fndecl)
530 : : {
531 : 251878 : tree inh = DECL_INHERITED_CTOR (fndecl);
532 : 125939 : if (!inh)
533 : : return binfo;
534 : :
535 : 66689 : tree results = NULL_TREE;
536 : 133828 : for (ovl_iterator iter (inh); iter; ++iter)
537 : : {
538 : 66914 : tree one = inherited_ctor_binfo_1 (binfo, *iter);
539 : 66914 : if (!results)
540 : : results = one;
541 : 225 : else if (one != results)
542 : 6 : results = tree_cons (NULL_TREE, one, results);
543 : : }
544 : 66689 : 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 : 4301263 : inherited_ctor_binfo (tree fndecl)
553 : : {
554 : 8602526 : if (!DECL_INHERITED_CTOR (fndecl))
555 : : return NULL_TREE;
556 : 59025 : tree binfo = TYPE_BINFO (DECL_CONTEXT (fndecl));
557 : 59025 : 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 : 163319227 : base_ctor_omit_inherited_parms (tree comp_ctor)
567 : : {
568 : 163319227 : gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (comp_ctor));
569 : :
570 : 163319227 : if (!flag_new_inheriting_ctors)
571 : : /* We only optimize away the parameters in the new model. */
572 : : return false;
573 : :
574 : 163271525 : if (!CLASSTYPE_VBASECLASSES (DECL_CONTEXT (comp_ctor)))
575 : : return false;
576 : :
577 : 5184809 : if (FUNCTION_FIRST_USER_PARMTYPE (comp_ctor) == void_list_node)
578 : : /* No user-declared parameters to omit. */
579 : : return false;
580 : :
581 : 4243960 : for (tree binfo = inherited_ctor_binfo (comp_ctor);
582 : 4245490 : 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 : 823412868 : ctor_omit_inherited_parms (tree fn)
596 : : {
597 : 823412868 : gcc_checking_assert (TREE_CODE (fn) == FUNCTION_DECL);
598 : :
599 : 823412868 : if (!DECL_BASE_CONSTRUCTOR_P (fn))
600 : : return false;
601 : :
602 : 120020637 : 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 : 4813222 : 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 : 4813588 : for (ovl_iterator iter (inh); iter; ++iter)
615 : : {
616 : 100797 : tree fn = *iter;
617 : 100797 : tree base = DECL_CONTEXT (fn);
618 : 100797 : tree base_binfo = NULL_TREE;
619 : 101231 : for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
620 : 101231 : if (BINFO_TYPE (base_binfo) == base)
621 : : break;
622 : 100797 : if (base_binfo == init_binfo
623 : 100797 : || (flag_new_inheriting_ctors
624 : 390 : && binfo_inherited_from (base_binfo, init_binfo,
625 : 780 : DECL_INHERITED_CTOR (fn))))
626 : 100440 : return true;
627 : : }
628 : 4712782 : 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 : 157143 : add_one_base_init (tree binfo, tree parm, bool move_p, tree inh,
637 : : tree member_init_list)
638 : : {
639 : 157143 : tree init;
640 : 157143 : if (inh)
641 : : {
642 : : /* An inheriting constructor only has a mem-initializer for
643 : : the base it inherits from. */
644 : 43138 : if (!binfo_inherited_from (TYPE_BINFO (current_class_type), binfo, inh))
645 : : return member_init_list;
646 : :
647 : 43069 : tree *p = &init;
648 : 43069 : init = NULL_TREE;
649 : 103288 : for (; parm; parm = DECL_CHAIN (parm))
650 : : {
651 : 60219 : tree exp = forward_parm (parm);
652 : 60219 : *p = build_tree_list (NULL_TREE, exp);
653 : 60219 : p = &TREE_CHAIN (*p);
654 : : }
655 : : }
656 : : else
657 : : {
658 : 114005 : init = build_base_path (PLUS_EXPR, parm, binfo, 1,
659 : : tf_warning_or_error);
660 : 114005 : if (move_p)
661 : 82507 : init = move (init);
662 : 114005 : init = build_tree_list (NULL_TREE, init);
663 : : }
664 : 157074 : 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 : 233010 : do_build_copy_constructor (tree fndecl)
672 : : {
673 : 233010 : tree parm = FUNCTION_FIRST_USER_PARM (fndecl);
674 : 466020 : bool move_p = DECL_MOVE_CONSTRUCTOR_P (fndecl);
675 : 233010 : bool trivial = trivial_fn_p (fndecl);
676 : 466020 : tree inh = DECL_INHERITED_CTOR (fndecl);
677 : :
678 : 233010 : if (!inh)
679 : 189956 : parm = convert_from_reference (parm);
680 : :
681 : 233010 : 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 : 232957 : tree member_init_list = NULL_TREE;
711 : 232957 : int i;
712 : 232957 : tree binfo, base_binfo;
713 : 232957 : 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 : 232957 : for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
721 : 233051 : vec_safe_iterate (vbases, i, &binfo); i++)
722 : : {
723 : 94 : member_init_list = add_one_base_init (binfo, parm, move_p, inh,
724 : : member_init_list);
725 : : }
726 : :
727 : 390081 : for (binfo = TYPE_BINFO (current_class_type), i = 0;
728 : 390081 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
729 : : {
730 : 157124 : if (BINFO_VIRTUAL_P (base_binfo))
731 : 75 : continue;
732 : 157049 : member_init_list = add_one_base_init (base_binfo, parm, move_p,
733 : : inh, member_init_list);
734 : : }
735 : :
736 : 232957 : if (!inh)
737 : : {
738 : 189903 : int cvquals = cp_type_quals (TREE_TYPE (parm));
739 : :
740 : 189903 : for (tree fields = TYPE_FIELDS (current_class_type);
741 : 11673338 : fields; fields = DECL_CHAIN (fields))
742 : : {
743 : 11483435 : tree field = fields;
744 : 11483435 : tree expr_type;
745 : :
746 : 11483435 : if (TREE_CODE (field) != FIELD_DECL)
747 : 11199229 : continue;
748 : :
749 : 284206 : expr_type = TREE_TYPE (field);
750 : 284206 : if (DECL_NAME (field))
751 : : {
752 : 170331 : if (VFIELD_NAME_P (DECL_NAME (field)))
753 : 442 : continue;
754 : : }
755 : 113875 : 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 : 113863 : 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 : 169901 : if (!TYPE_REF_P (expr_type))
768 : : {
769 : 168791 : int quals = cvquals;
770 : :
771 : 168791 : if (DECL_MUTABLE_P (field))
772 : 9 : quals &= ~TYPE_QUAL_CONST;
773 : 168791 : quals |= cp_type_quals (expr_type);
774 : 168791 : expr_type = cp_build_qualified_type (expr_type, quals);
775 : : }
776 : :
777 : 169901 : tree init = build3 (COMPONENT_REF, expr_type, parm, field, NULL_TREE);
778 : 93925 : if (move_p && !TYPE_REF_P (expr_type)
779 : : /* 'move' breaks bit-fields, and has no effect for scalars. */
780 : 263267 : && !scalarish_type_p (expr_type))
781 : 82086 : init = move (init);
782 : 169901 : init = build_tree_list (NULL_TREE, init);
783 : :
784 : 169901 : member_init_list = tree_cons (field, init, member_init_list);
785 : : }
786 : : }
787 : :
788 : 232957 : finish_mem_initializers (member_init_list);
789 : : }
790 : 233010 : }
791 : :
792 : : static void
793 : 42814 : do_build_copy_assign (tree fndecl)
794 : : {
795 : 42814 : tree parm = DECL_CHAIN (DECL_ARGUMENTS (fndecl));
796 : 42814 : tree compound_stmt;
797 : 42814 : bool move_p = move_fn_p (fndecl);
798 : 42814 : bool trivial = trivial_fn_p (fndecl);
799 : 42814 : int flags = LOOKUP_NORMAL | LOOKUP_NONVIRTUAL | LOOKUP_DEFAULTED;
800 : :
801 : 42814 : compound_stmt = begin_compound_stmt (0);
802 : 42814 : 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 : 85628 : tree const class_ref = DECL_XOBJ_MEMBER_FUNCTION_P (fndecl)
808 : 85628 : ? cp_build_fold_indirect_ref (DECL_ARGUMENTS (fndecl)) : current_class_ref;
809 : :
810 : 42814 : if (trivial
811 : 42814 : && 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 : 42811 : 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 : 42795 : tree fields;
822 : 42795 : int cvquals = cp_type_quals (TREE_TYPE (parm));
823 : 42795 : int i;
824 : 42795 : tree binfo, base_binfo;
825 : :
826 : : /* Assign to each of the direct base classes. */
827 : 42795 : for (binfo = TYPE_BINFO (current_class_type), i = 0;
828 : 63225 : BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
829 : : {
830 : 20430 : tree converted_parm;
831 : :
832 : : /* We must convert PARM directly to the base class
833 : : explicitly since the base class may be ambiguous. */
834 : 20430 : converted_parm = build_base_path (PLUS_EXPR, parm, base_binfo, 1,
835 : : tf_warning_or_error);
836 : 20430 : if (move_p)
837 : 19828 : converted_parm = move (converted_parm);
838 : : /* Call the base class assignment operator. */
839 : 20430 : releasing_vec parmvec (make_tree_vector_single (converted_parm));
840 : 20430 : finish_expr_stmt
841 : 20430 : (build_special_member_call (class_ref,
842 : : assign_op_identifier,
843 : : &parmvec,
844 : : base_binfo,
845 : : flags,
846 : : tf_warning_or_error));
847 : 20430 : }
848 : :
849 : : /* Assign to each of the non-static data members. */
850 : 42795 : for (fields = TYPE_FIELDS (current_class_type);
851 : 1539944 : fields;
852 : 1497149 : fields = DECL_CHAIN (fields))
853 : : {
854 : 1497149 : tree comp = class_ref;
855 : 1497149 : tree init = parm;
856 : 1497149 : tree field = fields;
857 : 1497149 : tree expr_type;
858 : 1497149 : int quals;
859 : :
860 : 1497149 : if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
861 : 1472934 : continue;
862 : :
863 : 24215 : expr_type = TREE_TYPE (field);
864 : :
865 : 24215 : 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 : 24213 : 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 : 24212 : if (DECL_NAME (field))
879 : : {
880 : 24209 : 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 : 24212 : comp = build3 (COMPONENT_REF, expr_type, comp, field, NULL_TREE);
892 : :
893 : : /* Compute the type of init->field */
894 : 24212 : quals = cvquals;
895 : 24212 : if (DECL_MUTABLE_P (field))
896 : 3 : quals &= ~TYPE_QUAL_CONST;
897 : 24212 : expr_type = cp_build_qualified_type (expr_type, quals);
898 : :
899 : 24212 : init = build3 (COMPONENT_REF, expr_type, init, field, NULL_TREE);
900 : 22402 : if (move_p && !TYPE_REF_P (expr_type)
901 : : /* 'move' breaks bit-fields, and has no effect for scalars. */
902 : 46614 : && !scalarish_type_p (expr_type))
903 : 22221 : init = move (init);
904 : :
905 : 24212 : if (DECL_NAME (field))
906 : 24209 : 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 : 24212 : finish_expr_stmt (init);
911 : : }
912 : : }
913 : 42814 : finish_return_stmt (class_ref);
914 : 42814 : finish_compound_stmt (compound_stmt);
915 : 42814 : }
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 : 360092 : lookup_comparison_result (tree type, const char *name_str,
951 : : tsubst_flags_t complain = tf_warning_or_error)
952 : : {
953 : 360092 : tree name = get_identifier (name_str);
954 : 360092 : tree decl = lookup_qualified_name (type, name);
955 : 360092 : if (TREE_CODE (decl) != VAR_DECL)
956 : : {
957 : 7 : if (complain & tf_error)
958 : : {
959 : 4 : auto_diagnostic_group d;
960 : 4 : if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
961 : 1 : qualified_name_lookup_error (type, name, decl, input_location);
962 : : else
963 : 3 : error ("%qD is not a static data member", decl);
964 : 4 : inform (input_location, "determining value of %qs", "operator<=>");
965 : 4 : }
966 : 7 : 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 : 212940 : lookup_comparison_category (comp_cat_tag tag,
975 : : tsubst_flags_t complain = tf_warning_or_error)
976 : : {
977 : 212940 : if (tree cached = comp_cat_cache[tag])
978 : : return cached;
979 : :
980 : 20840 : tree name = get_identifier (comp_cat_info[tag].name);
981 : 20840 : tree decl = lookup_qualified_name (std_node, name);
982 : 20840 : if (TREE_CODE (decl) != TYPE_DECL)
983 : : {
984 : 19 : if (complain & tf_error)
985 : : {
986 : 10 : auto_diagnostic_group d;
987 : 10 : if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
988 : 7 : qualified_name_lookup_error (std_node, name, decl, input_location);
989 : : else
990 : 3 : error ("%qD is not a type", decl);
991 : 10 : inform (input_location, "forming type of %qs", "operator<=>");
992 : 10 : }
993 : 19 : 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 : 20821 : tree type = TREE_TYPE (decl);
998 : 83380 : for (int i = 0; i < 4; ++i)
999 : : {
1000 : 83272 : const char *p = comp_cat_info[tag].members[i];
1001 : 83272 : if (!p) break;
1002 : 62563 : if (lookup_comparison_result (type, p, complain)
1003 : 62563 : == error_mark_node)
1004 : : return error_mark_node;
1005 : : }
1006 : 20817 : return comp_cat_cache[tag] = type;
1007 : : }
1008 : :
1009 : : /* Wrapper that takes the tag rather than the type. */
1010 : :
1011 : : static tree
1012 : 295 : lookup_comparison_result (comp_cat_tag tag, const char *name_str,
1013 : : tsubst_flags_t complain = tf_warning_or_error)
1014 : : {
1015 : 295 : tree type = lookup_comparison_category (tag, complain);
1016 : 295 : 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 : 297234 : lookup_comparison_result (comp_cat_tag tag, tree type, int idx)
1023 : : {
1024 : 297234 : const char *name_str = comp_cat_info[tag].members[idx];
1025 : 297234 : if (!name_str)
1026 : : return NULL_TREE;
1027 : 297234 : return lookup_comparison_result (type, name_str);
1028 : : }
1029 : :
1030 : : /* Does TYPE correspond to TAG? */
1031 : :
1032 : : static bool
1033 : 291608 : is_cat (tree type, comp_cat_tag tag)
1034 : : {
1035 : 291608 : tree name = TYPE_LINKAGE_IDENTIFIER (type);
1036 : 291608 : 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 : 97841 : cat_tag_for (tree type)
1043 : : {
1044 : 97841 : if (!CLASS_TYPE_P (type) || !decl_in_std_namespace_p (TYPE_MAIN_DECL (type)))
1045 : 48 : return cc_last;
1046 : 291608 : for (int i = 0; i < cc_last; ++i)
1047 : : {
1048 : 291608 : comp_cat_tag tag = (comp_cat_tag)i;
1049 : 291608 : 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 : 208549 : spaceship_comp_cat (tree optype)
1060 : : {
1061 : 208549 : if (INTEGRAL_OR_ENUMERATION_TYPE_P (optype) || TYPE_PTROBV_P (optype))
1062 : : return cc_strong_ordering;
1063 : 676 : 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 : 208549 : spaceship_type (tree optype, tsubst_flags_t complain)
1075 : : {
1076 : 208549 : comp_cat_tag tag = spaceship_comp_cat (optype);
1077 : 208549 : 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 : 97456 : genericize_spaceship (location_t loc, tree type, tree op0, tree op1)
1086 : : {
1087 : : /* ??? maybe optimize based on knowledge of representation? */
1088 : 97456 : comp_cat_tag tag = cat_tag_for (type);
1089 : :
1090 : 97456 : 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 : 97456 : else if (tag == cc_last)
1101 : 3 : return error_mark_node;
1102 : :
1103 : 97453 : tree r;
1104 : 97453 : bool scalar = SCALAR_TYPE_P (TREE_TYPE (op0));
1105 : 97396 : if (scalar)
1106 : : {
1107 : 97396 : op0 = save_expr (op0);
1108 : 97396 : op1 = save_expr (op1);
1109 : : }
1110 : :
1111 : 97453 : tree gt = lookup_comparison_result (tag, type, 1);
1112 : :
1113 : 97453 : int flags = LOOKUP_NORMAL;
1114 : 97453 : tsubst_flags_t complain = tf_none;
1115 : 97453 : tree comp;
1116 : :
1117 : 97453 : if (tag == cc_partial_ordering)
1118 : : {
1119 : : /* op0 == op1 ? equivalent : op0 < op1 ? less :
1120 : : op1 < op0 ? greater : unordered */
1121 : 815 : tree uo = lookup_comparison_result (tag, type, 3);
1122 : 815 : 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 : 803 : comp = fold_build2 (LT_EXPR, boolean_type_node, op1, op0);
1127 : 803 : 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 : 97453 : tree lt = lookup_comparison_result (tag, type, 2);
1140 : 97453 : if (scalar)
1141 : : {
1142 : 97396 : comp = fold_build2 (LT_EXPR, boolean_type_node, op0, op1);
1143 : 97396 : 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 : 97453 : tree eq = lookup_comparison_result (tag, type, 0);
1152 : 97453 : if (scalar)
1153 : : {
1154 : 97396 : comp = fold_build2 (EQ_EXPR, boolean_type_node, op0, op1);
1155 : 97396 : 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 : 59865 : early_check_defaulted_comparison (tree fn)
1171 : : {
1172 : 59865 : location_t loc = DECL_SOURCE_LOCATION (fn);
1173 : 59865 : tree ctx;
1174 : 59865 : if (DECL_CLASS_SCOPE_P (fn))
1175 : 8016 : ctx = DECL_CONTEXT (fn);
1176 : : else
1177 : 103698 : ctx = DECL_FRIEND_CONTEXT (fn);
1178 : 59865 : bool ok = true;
1179 : :
1180 : 59865 : 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 : 59861 : if (!DECL_OVERLOADED_OPERATOR_IS (fn, SPACESHIP_EXPR)
1188 : 59861 : && !same_type_p (TREE_TYPE (TREE_TYPE (fn)), boolean_type_node))
1189 : : {
1190 : 6 : enum diagnostics::kind kind = diagnostics::kind::unspecified;
1191 : 6 : int opt = 0;
1192 : 6 : if (is_auto (TREE_TYPE (fn)))
1193 : : kind = diagnostics::kind::pedwarn;
1194 : : else
1195 : 6 : kind = diagnostics::kind::error;
1196 : 6 : emit_diagnostic (kind, loc, opt,
1197 : : "defaulted %qD must return %<bool%>", fn);
1198 : 6 : if (kind == diagnostics::kind::error)
1199 : 59861 : ok = false;
1200 : : }
1201 : :
1202 : 59861 : bool mem = DECL_IOBJ_MEMBER_FUNCTION_P (fn);
1203 : 59861 : 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 : 59861 : 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 : 59861 : tree parmnode = FUNCTION_FIRST_USER_PARMTYPE (fn);
1214 : 59861 : bool saw_byval = false;
1215 : 59861 : bool saw_byref = mem;
1216 : 59861 : bool saw_bad = false;
1217 : 171575 : for (; parmnode != void_list_node; parmnode = TREE_CHAIN (parmnode))
1218 : : {
1219 : 111714 : tree parmtype = TREE_VALUE (parmnode);
1220 : 111714 : if (CLASS_TYPE_P (parmtype))
1221 : : saw_byval = true;
1222 : 88832 : else if (TREE_CODE (parmtype) == REFERENCE_TYPE
1223 : 88829 : && !TYPE_REF_IS_RVALUE (parmtype)
1224 : 177655 : && TYPE_QUALS (TREE_TYPE (parmtype)) == TYPE_QUAL_CONST)
1225 : : {
1226 : 88823 : saw_byref = true;
1227 : 88823 : parmtype = TREE_TYPE (parmtype);
1228 : : }
1229 : : else
1230 : : saw_bad = true;
1231 : :
1232 : 111714 : 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 : 111693 : else if (!same_type_ignoring_top_level_qualifiers_p (parmtype, ctx))
1245 : 9 : saw_bad = true;
1246 : : }
1247 : :
1248 : 59861 : 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 : 59861 : DECL_MAYBE_DELETED (fn) = ok;
1264 : :
1265 : 59861 : 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 : 131 : common_comparison_type (vec<tree> &comps)
1274 : : {
1275 : 131 : tree seen[cc_last] = {};
1276 : :
1277 : 243 : for (unsigned i = 0; i < comps.length(); ++i)
1278 : : {
1279 : 112 : tree comp = comps[i];
1280 : 112 : if (TREE_CODE (comp) == TREE_LIST)
1281 : 3 : comp = TREE_VALUE (comp);
1282 : 112 : tree ctype = TREE_TYPE (comp);
1283 : 112 : comp_cat_tag tag = cat_tag_for (ctype);
1284 : : /* build_comparison_op already checked this. */
1285 : 112 : gcc_checking_assert (tag < cc_last);
1286 : 112 : seen[tag] = ctype;
1287 : : }
1288 : :
1289 : : /* Otherwise, if at least one T i is std::partial_ordering, U is
1290 : : std::partial_ordering. */
1291 : 131 : 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 : 113 : if (tree t = seen[cc_weak_ordering]) return t;
1296 : :
1297 : : /* Otherwise, U is std::strong_ordering. */
1298 : 113 : if (tree t = seen[cc_strong_ordering]) return t;
1299 : 34 : 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 : 18505 : comp_info (tree fndecl, tsubst_flags_t complain)
1317 : 18505 : : fndecl (fndecl), complain (complain)
1318 : : {
1319 : 18505 : loc = DECL_SOURCE_LOCATION (fndecl);
1320 : :
1321 : 18505 : first_time = DECL_MAYBE_DELETED (fndecl);
1322 : 18505 : DECL_MAYBE_DELETED (fndecl) = false;
1323 : :
1324 : : /* Do we want to try to set constexpr? */
1325 : 18505 : was_constexp = DECL_DECLARED_CONSTEXPR_P (fndecl);
1326 : 18505 : constexp = first_time;
1327 : 18505 : if (constexp)
1328 : : /* Set this for var_in_constexpr_fn. */
1329 : 18406 : DECL_DECLARED_CONSTEXPR_P (fndecl) = true;
1330 : :
1331 : : /* Do we want to try to set noexcept? */
1332 : 18505 : noex = first_time;
1333 : 18505 : if (noex)
1334 : : {
1335 : 18406 : tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fndecl));
1336 : 24635 : if (raises && !UNEVALUATED_NOEXCEPT_SPEC_P (raises))
1337 : : /* There was an explicit exception-specification. */
1338 : 6229 : noex = false;
1339 : : }
1340 : 18505 : }
1341 : :
1342 : : /* EXPR is an expression built as part of the function body.
1343 : : Adjust the properties appropriately. */
1344 : 27618 : void check (tree expr)
1345 : : {
1346 : 27618 : if (expr == error_mark_node)
1347 : 0 : DECL_DELETED_FN (fndecl) = true;
1348 : 62 : if ((constexp || was_constexp)
1349 : 27645 : && !potential_rvalue_constant_expression (expr))
1350 : : {
1351 : 311 : if (was_constexp)
1352 : 12 : require_potential_rvalue_constant_expression_fncheck (expr);
1353 : : else
1354 : 299 : constexp = false;
1355 : : }
1356 : 27618 : if (noex && !expr_noexcept_p (expr, tf_none))
1357 : 59 : noex = false;
1358 : 27618 : }
1359 : :
1360 : 18505 : ~comp_info ()
1361 : : {
1362 : 18505 : if (first_time)
1363 : : {
1364 : 18406 : DECL_DECLARED_CONSTEXPR_P (fndecl) = constexp || was_constexp;
1365 : 18406 : tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fndecl));
1366 : 24635 : if (!raises || UNEVALUATED_NOEXCEPT_SPEC_P (raises))
1367 : : {
1368 : 12177 : raises = noex ? noexcept_true_spec : noexcept_false_spec;
1369 : 12177 : TREE_TYPE (fndecl) = build_exception_variant (TREE_TYPE (fndecl),
1370 : : raises);
1371 : : }
1372 : : }
1373 : 18505 : }
1374 : : };
1375 : :
1376 : : /* Subroutine of build_comparison_op, to compare a single subobject. */
1377 : :
1378 : : static tree
1379 : 27523 : do_one_comp (location_t loc, const comp_info &info, tree sub, tree lhs, tree rhs)
1380 : : {
1381 : 27523 : const tree_code code = info.code;
1382 : 27523 : const tree fndecl = info.fndecl;
1383 : 27523 : const comp_cat_tag retcat = info.retcat;
1384 : 27523 : const tsubst_flags_t complain = info.complain;
1385 : :
1386 : 27523 : tree overload = NULL_TREE;
1387 : 27523 : 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 : 27523 : bool tentative = retcat != cc_last;
1391 : 54913 : tree comp = build_new_op (loc, code, flags, lhs, rhs,
1392 : : NULL_TREE, NULL_TREE, &overload,
1393 : : tentative ? tf_none : complain);
1394 : :
1395 : 27523 : if (code != SPACESHIP_EXPR)
1396 : : return comp;
1397 : :
1398 : 329 : tree rettype = TREE_TYPE (TREE_TYPE (fndecl));
1399 : :
1400 : 329 : 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 : 251 : if (FNDECL_USED_AUTO (fndecl)
1428 : 251 : && 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 : 245 : else if (!FNDECL_USED_AUTO (fndecl)
1439 : 245 : && !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 : 18505 : build_comparison_op (tree fndecl, bool defining, tsubst_flags_t complain)
1460 : : {
1461 : 18505 : comp_info info (fndecl, complain);
1462 : :
1463 : 18505 : if (!defining && !(complain & tf_error) && !DECL_MAYBE_DELETED (fndecl))
1464 : : return;
1465 : :
1466 : 18496 : int flags = LOOKUP_NORMAL;
1467 : 18496 : const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (DECL_NAME (fndecl));
1468 : 18496 : tree_code code = info.code = op->tree_code;
1469 : :
1470 : 18496 : tree lhs = DECL_ARGUMENTS (fndecl);
1471 : 18496 : tree rhs = DECL_CHAIN (lhs);
1472 : 18496 : if (is_this_parameter (lhs))
1473 : 9521 : lhs = cp_build_fold_indirect_ref (lhs);
1474 : : else
1475 : 8975 : lhs = convert_from_reference (lhs);
1476 : 18496 : rhs = convert_from_reference (rhs);
1477 : 18496 : tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (lhs));
1478 : 18496 : gcc_assert (!defining || COMPLETE_TYPE_P (ctype));
1479 : :
1480 : 18496 : 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 : 18496 : if (TREE_CODE (ctype) == UNION_TYPE
1485 : 18496 : && 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 : 18490 : tree compound_stmt = NULL_TREE;
1494 : 18490 : if (defining)
1495 : 18409 : compound_stmt = begin_compound_stmt (0);
1496 : : else
1497 : 81 : ++cp_unevaluated_operand;
1498 : :
1499 : 18490 : tree rettype = TREE_TYPE (TREE_TYPE (fndecl));
1500 : 18490 : 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 : 18490 : if (code == EQ_EXPR || code == SPACESHIP_EXPR)
1507 : : {
1508 : 18415 : comp_cat_tag &retcat = (info.retcat = cc_last);
1509 : 18710 : if (code == SPACESHIP_EXPR && !FNDECL_USED_AUTO (fndecl))
1510 : 143 : retcat = cat_tag_for (rettype);
1511 : :
1512 : 18415 : bool bad = false;
1513 : 18415 : 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 : 18712 : for (tree base_binfo : BINFO_BASE_BINFOS (TYPE_BINFO (ctype)))
1519 : : {
1520 : 297 : tree lhs_base
1521 : 297 : = build_base_path (PLUS_EXPR, lhs, base_binfo, 0, complain);
1522 : 297 : tree rhs_base
1523 : 297 : = build_base_path (PLUS_EXPR, rhs, base_binfo, 0, complain);
1524 : :
1525 : 297 : location_t loc = DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (ctype));
1526 : 297 : tree comp = do_one_comp (loc, info, BINFO_TYPE (base_binfo),
1527 : 297 : lhs_base, rhs_base);
1528 : 297 : if (comp == error_mark_node)
1529 : : {
1530 : 12 : bad = true;
1531 : 12 : continue;
1532 : : }
1533 : :
1534 : 285 : comps.safe_push (comp);
1535 : : }
1536 : :
1537 : : /* Now compare the field subobjects. */
1538 : 18415 : for (tree field = next_aggregate_field (TYPE_FIELDS (ctype));
1539 : 45962 : field;
1540 : 27547 : field = next_aggregate_field (DECL_CHAIN (field)))
1541 : : {
1542 : 55094 : if (DECL_VIRTUAL_P (field) || DECL_FIELD_IS_BASE (field))
1543 : : /* We ignore the vptr, and we already handled bases. */
1544 : 432 : continue;
1545 : :
1546 : 27250 : tree expr_type = TREE_TYPE (field);
1547 : :
1548 : 27250 : 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 : 27250 : 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 : 27250 : && 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 : 27238 : tree lhs_mem = build3_loc (field_loc, COMPONENT_REF, expr_type, lhs,
1572 : : field, NULL_TREE);
1573 : 27238 : tree rhs_mem = build3_loc (field_loc, COMPONENT_REF, expr_type, rhs,
1574 : : field, NULL_TREE);
1575 : 27238 : tree loop_indexes = NULL_TREE;
1576 : 54482 : 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 : 27238 : if (TREE_CODE (expr_type) == ARRAY_TYPE)
1610 : 12 : continue;
1611 : :
1612 : 27226 : tree comp = do_one_comp (field_loc, info, field, lhs_mem, rhs_mem);
1613 : 27226 : 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 : 27115 : if (loop_indexes)
1627 : : {
1628 : 3 : TREE_VALUE (loop_indexes) = comp;
1629 : 3 : comp = loop_indexes;
1630 : : }
1631 : 27115 : comps.safe_push (comp);
1632 : : }
1633 : 18415 : if (code == SPACESHIP_EXPR && is_auto (rettype))
1634 : : {
1635 : 131 : rettype = common_comparison_type (comps);
1636 : 131 : apply_deduced_return_type (fndecl, rettype);
1637 : : }
1638 : 18415 : tree retvaleq;
1639 : 18415 : if (code == EQ_EXPR)
1640 : 18120 : retvaleq = boolean_true_node;
1641 : : else
1642 : : {
1643 : 295 : tree seql = lookup_comparison_result (cc_strong_ordering,
1644 : : "equal", complain);
1645 : 295 : retvaleq = build_static_cast (input_location, rettype, seql,
1646 : : complain);
1647 : 295 : if (retvaleq == error_mark_node)
1648 : : bad = true;
1649 : : }
1650 : 18376 : if (bad)
1651 : : {
1652 : 153 : DECL_DELETED_FN (fndecl) = true;
1653 : 153 : goto out;
1654 : : }
1655 : 45626 : for (unsigned i = 0; i < comps.length(); ++i)
1656 : : {
1657 : 27364 : tree comp = comps[i];
1658 : 27364 : tree eq, retval = NULL_TREE, if_ = NULL_TREE;
1659 : 27364 : tree loop_indexes = NULL_TREE;
1660 : 27364 : if (defining)
1661 : : {
1662 : 27358 : 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 : 27358 : 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 : 27364 : if (code == EQ_EXPR)
1695 : : {
1696 : : /* if (x==y); else return false; */
1697 : 27185 : eq = comp;
1698 : 27185 : retval = boolean_false_node;
1699 : : }
1700 : : else
1701 : : {
1702 : : /* if (auto v = x<=>y, v == 0); else return v; */
1703 : 179 : if (TREE_CODE (comp) == SPACESHIP_EXPR)
1704 : 0 : TREE_TYPE (comp) = rettype;
1705 : : else
1706 : 179 : comp = build_static_cast (input_location, rettype, comp,
1707 : : complain);
1708 : 179 : info.check (comp);
1709 : 179 : if (defining)
1710 : : {
1711 : 179 : tree var = create_temporary_var (rettype);
1712 : 179 : DECL_NAME (var) = get_identifier ("retval");
1713 : 179 : pushdecl (var);
1714 : 179 : cp_finish_decl (var, comp, false, NULL_TREE, flags);
1715 : 179 : comp = retval = var;
1716 : : }
1717 : 179 : eq = build_new_op (info.loc, EQ_EXPR, flags, comp,
1718 : : integer_zero_node, NULL_TREE, NULL_TREE,
1719 : : NULL, complain);
1720 : : }
1721 : 27364 : tree ceq = contextual_conv_bool (eq, complain);
1722 : 27364 : info.check (ceq);
1723 : 27364 : if (defining)
1724 : : {
1725 : 27358 : finish_if_stmt_cond (ceq, if_);
1726 : 27358 : finish_then_clause (if_);
1727 : 27358 : begin_else_clause (if_);
1728 : 27358 : finish_return_stmt (retval);
1729 : 27358 : finish_else_clause (if_);
1730 : 27358 : finish_if_stmt (if_);
1731 : 27361 : 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 : 18262 : if (defining)
1737 : 18256 : finish_return_stmt (retvaleq);
1738 : 18415 : }
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 : 18487 : out:
1763 : 18487 : if (defining)
1764 : 18409 : finish_compound_stmt (compound_stmt);
1765 : : else
1766 : 81 : --cp_unevaluated_operand;
1767 : 18505 : }
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 : 2323963 : decl_remember_implicit_trigger_p (tree decl)
1775 : : {
1776 : 2323963 : if (!DECL_ARTIFICIAL (decl))
1777 : : return false;
1778 : 1081654 : 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 : 1081654 : return (sfk != sfk_inheriting_constructor
1782 : 1081654 : && sfk != sfk_comparison);
1783 : : }
1784 : :
1785 : : /* Synthesize FNDECL, a non-static member function. */
1786 : :
1787 : : void
1788 : 1171492 : synthesize_method (tree fndecl)
1789 : : {
1790 : 1171492 : bool need_body = true;
1791 : 1171492 : tree stmt;
1792 : 1171492 : location_t save_input_location = input_location;
1793 : 1171492 : int error_count = errorcount;
1794 : 1171492 : int warning_count = warningcount + werrorcount;
1795 : 1171492 : special_function_kind sfk = special_function_p (fndecl);
1796 : 1171492 : 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 : 1171492 : if (decl_remember_implicit_trigger_p (fndecl))
1801 : 995496 : DECL_SOURCE_LOCATION (fndecl)
1802 : 497748 : = 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 : 1171492 : if (DECL_CLONED_FUNCTION_P (fndecl))
1808 : 1109805 : fndecl = DECL_CLONED_FUNCTION (fndecl);
1809 : :
1810 : : /* We may be in the middle of deferred access check. Disable
1811 : : it now. */
1812 : 1171492 : push_deferring_access_checks (dk_no_deferred);
1813 : :
1814 : 1171492 : bool push_to_top = maybe_push_to_top_level (fndecl);
1815 : :
1816 : 1171492 : input_location = DECL_SOURCE_LOCATION (fndecl);
1817 : :
1818 : 1171492 : start_preparsed_function (fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
1819 : 1171492 : stmt = begin_function_body ();
1820 : :
1821 : 1171492 : if (DECL_ASSIGNMENT_OPERATOR_P (fndecl)
1822 : 1171492 : && DECL_OVERLOADED_OPERATOR_IS (fndecl, NOP_EXPR))
1823 : : {
1824 : 42814 : do_build_copy_assign (fndecl);
1825 : 42814 : need_body = false;
1826 : : }
1827 : 2257356 : else if (DECL_CONSTRUCTOR_P (fndecl))
1828 : : {
1829 : 668480 : tree arg_chain = FUNCTION_FIRST_USER_PARMTYPE (fndecl);
1830 : 668480 : if (arg_chain != void_list_node)
1831 : 233010 : do_build_copy_constructor (fndecl);
1832 : : else
1833 : 435470 : finish_mem_initializers (NULL_TREE);
1834 : : }
1835 : 460198 : else if (sfk == sfk_comparison)
1836 : : {
1837 : : /* Pass tf_none so the function is just deleted if there's a problem. */
1838 : 18412 : build_comparison_op (fndecl, true, tf_none);
1839 : 18412 : 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 : 729706 : if (need_body)
1845 : : {
1846 : 1110266 : tree compound_stmt;
1847 : 1110266 : compound_stmt = begin_compound_stmt (BCS_FN_BODY);
1848 : 1110266 : finish_compound_stmt (compound_stmt);
1849 : : }
1850 : :
1851 : 1171492 : finish_function_body (stmt);
1852 : 1171492 : finish_function (/*inline_p=*/false);
1853 : :
1854 : : /* Remember that we were defined in this module. */
1855 : 1171492 : set_instantiating_module (fndecl);
1856 : :
1857 : 1171492 : if (!DECL_DELETED_FN (fndecl))
1858 : 1171405 : expand_or_defer_fn (fndecl);
1859 : :
1860 : 1171492 : input_location = save_input_location;
1861 : :
1862 : 1171492 : maybe_pop_from_top_level (push_to_top);
1863 : :
1864 : 1171492 : pop_deferring_access_checks ();
1865 : :
1866 : 1171492 : if (error_count != errorcount || warning_count != warningcount + werrorcount)
1867 : 35 : if (DECL_ARTIFICIAL (fndecl))
1868 : 28 : inform (input_location, "synthesized method %qD first required here",
1869 : : fndecl);
1870 : 1171492 : }
1871 : :
1872 : : /* Like synthesize_method, but don't actually synthesize defaulted comparison
1873 : : methods if their class is still incomplete. Just deduce the return
1874 : : type in that case. */
1875 : :
1876 : : void
1877 : 9267 : maybe_synthesize_method (tree fndecl)
1878 : : {
1879 : 9267 : if (special_function_p (fndecl) == sfk_comparison)
1880 : : {
1881 : 9267 : tree lhs = DECL_ARGUMENTS (fndecl);
1882 : 9267 : if (is_this_parameter (lhs))
1883 : 301 : lhs = cp_build_fold_indirect_ref (lhs);
1884 : : else
1885 : 8966 : lhs = convert_from_reference (lhs);
1886 : 9267 : tree ctype = TYPE_MAIN_VARIANT (TREE_TYPE (lhs));
1887 : 9267 : if (!COMPLETE_TYPE_P (ctype))
1888 : : {
1889 : 9 : push_deferring_access_checks (dk_no_deferred);
1890 : 9 : build_comparison_op (fndecl, false, tf_none);
1891 : 9 : pop_deferring_access_checks ();
1892 : 9 : return;
1893 : : }
1894 : : }
1895 : 9258 : return synthesize_method (fndecl);
1896 : : }
1897 : :
1898 : : /* Build a reference to type TYPE with cv-quals QUALS, which is an
1899 : : rvalue if RVALUE is true. */
1900 : :
1901 : : static tree
1902 : 7122316 : build_stub_type (tree type, int quals, bool rvalue)
1903 : : {
1904 : 7122316 : tree argtype = cp_build_qualified_type (type, quals);
1905 : 7122316 : return cp_build_reference_type (argtype, rvalue);
1906 : : }
1907 : :
1908 : : /* Build a dummy glvalue from dereferencing a dummy reference of type
1909 : : REFTYPE. */
1910 : :
1911 : : tree
1912 : 37943405 : build_stub_object (tree reftype)
1913 : : {
1914 : 37943405 : if (!TYPE_REF_P (reftype))
1915 : 2515964 : reftype = cp_build_reference_type (reftype, /*rval*/true);
1916 : 37943405 : tree stub = build1 (CONVERT_EXPR, reftype, integer_one_node);
1917 : 37943405 : return convert_from_reference (stub);
1918 : : }
1919 : :
1920 : : /* True iff EXPR is the result of build_stub_object. */
1921 : :
1922 : : bool
1923 : 831707907 : is_stub_object (tree expr)
1924 : : {
1925 : 831707907 : if (!REFERENCE_REF_P (expr))
1926 : : return false;
1927 : 73203372 : expr = TREE_OPERAND (expr, 0);
1928 : 73203372 : return (TREE_CODE (expr) == CONVERT_EXPR
1929 : 73203372 : && TREE_OPERAND (expr, 0) == integer_one_node);
1930 : : }
1931 : :
1932 : : /* Build a std::declval<TYPE>() expression and return it. */
1933 : :
1934 : : static tree
1935 : 3573172 : build_trait_object (tree type, tsubst_flags_t complain)
1936 : : {
1937 : : /* TYPE can't be a function with cv-/ref-qualifiers: std::declval is
1938 : : defined as
1939 : :
1940 : : template<class T>
1941 : : typename std::add_rvalue_reference<T>::type declval() noexcept;
1942 : :
1943 : : and std::add_rvalue_reference yields T when T is a function with
1944 : : cv- or ref-qualifiers, making the definition ill-formed. */
1945 : 3573172 : if (FUNC_OR_METHOD_TYPE_P (type)
1946 : 3573172 : && (type_memfn_quals (type) != TYPE_UNQUALIFIED
1947 : 207 : || type_memfn_rqual (type) != REF_QUAL_NONE))
1948 : : {
1949 : 20 : if (complain & tf_error)
1950 : 3 : error ("object cannot have qualified function type %qT", type);
1951 : 20 : return error_mark_node;
1952 : : }
1953 : :
1954 : 3573152 : return build_stub_object (type);
1955 : : }
1956 : :
1957 : : /* [func.require] Build an expression of INVOKE(FN_TYPE, ARG_TYPES...). If the
1958 : : given is not invocable, returns error_mark_node, unless COMPLAIN includes
1959 : : tf_error. */
1960 : :
1961 : : tree
1962 : 93075 : build_invoke (tree fn_type, const_tree arg_types, tsubst_flags_t complain)
1963 : : {
1964 : 93075 : if (error_operand_p (fn_type) || error_operand_p (arg_types))
1965 : 0 : return error_mark_node;
1966 : :
1967 : 93075 : gcc_assert (TYPE_P (fn_type));
1968 : 93075 : gcc_assert (TREE_CODE (arg_types) == TREE_VEC);
1969 : :
1970 : : /* Access check is required to determine if the given is invocable. */
1971 : 93075 : deferring_access_check_sentinel acs (dk_no_deferred);
1972 : :
1973 : : /* INVOKE is an unevaluated context. */
1974 : 93075 : cp_unevaluated cp_uneval_guard;
1975 : :
1976 : 93075 : bool is_ptrdatamem;
1977 : 93075 : bool is_ptrmemfunc;
1978 : 93075 : if (TREE_CODE (fn_type) == REFERENCE_TYPE)
1979 : : {
1980 : 67682 : tree non_ref_fn_type = TREE_TYPE (fn_type);
1981 : 67682 : is_ptrdatamem = TYPE_PTRDATAMEM_P (non_ref_fn_type);
1982 : 67682 : is_ptrmemfunc = TYPE_PTRMEMFUNC_P (non_ref_fn_type);
1983 : :
1984 : : /* Dereference fn_type if it is a pointer to member. */
1985 : 67546 : if (is_ptrdatamem || is_ptrmemfunc)
1986 : : fn_type = non_ref_fn_type;
1987 : : }
1988 : : else
1989 : : {
1990 : 25393 : is_ptrdatamem = TYPE_PTRDATAMEM_P (fn_type);
1991 : 25393 : is_ptrmemfunc = TYPE_PTRMEMFUNC_P (fn_type);
1992 : : }
1993 : :
1994 : 26694 : if (is_ptrdatamem && TREE_VEC_LENGTH (arg_types) != 1)
1995 : : {
1996 : 43 : if (complain & tf_error)
1997 : 0 : error ("pointer to data member type %qT can only be invoked with "
1998 : : "one argument", fn_type);
1999 : 43 : return error_mark_node;
2000 : : }
2001 : 112980 : if (is_ptrmemfunc && TREE_VEC_LENGTH (arg_types) == 0)
2002 : : {
2003 : 29 : if (complain & tf_error)
2004 : 0 : error ("pointer to member function type %qT must be invoked with "
2005 : : "at least one argument", fn_type);
2006 : 29 : return error_mark_node;
2007 : : }
2008 : :
2009 : : /* Construct an expression of a pointer to member. */
2010 : 93003 : tree ptrmem_expr;
2011 : 93003 : if (is_ptrdatamem || is_ptrmemfunc)
2012 : : {
2013 : 20605 : tree datum_type = TREE_VEC_ELT (arg_types, 0);
2014 : 20605 : tree non_ref_datum_type = datum_type;
2015 : 20605 : if (TYPE_REF_P (datum_type))
2016 : 717 : non_ref_datum_type = TREE_TYPE (datum_type);
2017 : :
2018 : : /* datum must be a class type or a pointer to a class type. */
2019 : 832 : if (!CLASS_TYPE_P (non_ref_datum_type)
2020 : 20605 : && !(POINTER_TYPE_P (non_ref_datum_type)
2021 : 19672 : && CLASS_TYPE_P (TREE_TYPE (non_ref_datum_type))))
2022 : : {
2023 : 107 : if (complain & tf_error)
2024 : 0 : error ("first argument type %qT of a pointer to member must be a "
2025 : : "class type or a pointer to a class type", datum_type);
2026 : 107 : return error_mark_node;
2027 : : }
2028 : :
2029 : : /* 1.1 & 1.4. */
2030 : 20498 : tree ptrmem_class_type = TYPE_PTRMEM_CLASS_TYPE (fn_type);
2031 : 20498 : const bool ptrmem_is_same_or_base_of_datum =
2032 : 20498 : (same_type_ignoring_top_level_qualifiers_p (ptrmem_class_type,
2033 : : non_ref_datum_type)
2034 : 20498 : || (NON_UNION_CLASS_TYPE_P (ptrmem_class_type)
2035 : 19784 : && NON_UNION_CLASS_TYPE_P (non_ref_datum_type)
2036 : 118 : && DERIVED_FROM_P (ptrmem_class_type, non_ref_datum_type)));
2037 : :
2038 : 19766 : bool datum_is_refwrap = false;
2039 : 19766 : if (!ptrmem_is_same_or_base_of_datum && CLASS_TYPE_P (non_ref_datum_type))
2040 : : {
2041 : 100 : tree datum_decl = TYPE_NAME (TYPE_MAIN_VARIANT (non_ref_datum_type));
2042 : 100 : if (decl_in_std_namespace_p (datum_decl))
2043 : : {
2044 : 50 : const_tree name = DECL_NAME (datum_decl);
2045 : 50 : if (name && (id_equal (name, "reference_wrapper")))
2046 : : {
2047 : : /* 1.2 & 1.5: Retrieve T& from std::reference_wrapper<T>,
2048 : : i.e., decltype(datum.get()). */
2049 : 96 : datum_type =
2050 : 96 : TREE_VEC_ELT (TYPE_TI_ARGS (non_ref_datum_type), 0);
2051 : 48 : datum_type = cp_build_reference_type (datum_type, false);
2052 : 48 : datum_is_refwrap = true;
2053 : : }
2054 : : }
2055 : : }
2056 : :
2057 : 20498 : tree datum_expr = build_trait_object (datum_type, complain);
2058 : 20498 : if (!ptrmem_is_same_or_base_of_datum && !datum_is_refwrap)
2059 : : /* 1.3 & 1.6: Try to dereference datum_expr. */
2060 : 19718 : datum_expr = build_x_indirect_ref (UNKNOWN_LOCATION, datum_expr,
2061 : : RO_UNARY_STAR, NULL_TREE, complain);
2062 : :
2063 : 20498 : if (error_operand_p (datum_expr))
2064 : 17 : return error_mark_node;
2065 : :
2066 : 20481 : tree fn_expr = build_trait_object (fn_type, complain);
2067 : 20481 : ptrmem_expr = build_m_component_ref (datum_expr, fn_expr, complain);
2068 : :
2069 : 20481 : if (error_operand_p (ptrmem_expr))
2070 : 42 : return error_mark_node;
2071 : :
2072 : 20439 : if (is_ptrdatamem)
2073 : : return ptrmem_expr;
2074 : : }
2075 : :
2076 : : /* Construct expressions for arguments to INVOKE. For a pointer to member
2077 : : function, the first argument, which is the object, is not arguments to
2078 : : the function. */
2079 : 92218 : releasing_vec args;
2080 : 287133 : for (int i = is_ptrmemfunc ? 1 : 0; i < TREE_VEC_LENGTH (arg_types); ++i)
2081 : : {
2082 : 102703 : tree arg_type = TREE_VEC_ELT (arg_types, i);
2083 : 102703 : tree arg = build_trait_object (arg_type, complain);
2084 : 102703 : if (error_operand_p (arg))
2085 : 6 : return error_mark_node;
2086 : 102697 : vec_safe_push (args, arg);
2087 : : }
2088 : :
2089 : 92212 : tree invoke_expr;
2090 : 92212 : if (is_ptrmemfunc)
2091 : 19820 : invoke_expr = build_offset_ref_call_from_tree (ptrmem_expr, &args,
2092 : : complain);
2093 : : else /* 1.7. */
2094 : 72392 : invoke_expr = finish_call_expr (build_trait_object (fn_type, complain),
2095 : : &args, false, false, complain);
2096 : : return invoke_expr;
2097 : 93075 : }
2098 : :
2099 : : /* Determine which function will be called when looking up NAME in TYPE,
2100 : : called with a single ARGTYPE argument, or no argument if ARGTYPE is
2101 : : null. FLAGS and COMPLAIN are as for build_new_method_call.
2102 : :
2103 : : Returns a FUNCTION_DECL if all is well.
2104 : : Returns NULL_TREE if overload resolution failed.
2105 : : Returns error_mark_node if the chosen function cannot be called. */
2106 : :
2107 : : static tree
2108 : 23154012 : locate_fn_flags (tree type, tree name, tree argtype, int flags,
2109 : : tsubst_flags_t complain)
2110 : : {
2111 : 23154012 : tree ob, fn, fns, binfo, rval;
2112 : :
2113 : 23154012 : if (TYPE_P (type))
2114 : 10797862 : binfo = TYPE_BINFO (type);
2115 : : else
2116 : : {
2117 : 12356150 : binfo = type;
2118 : 12356150 : type = BINFO_TYPE (binfo);
2119 : : }
2120 : :
2121 : 23154012 : ob = build_stub_object (cp_build_reference_type (type, false));
2122 : 23154012 : releasing_vec args;
2123 : 23154012 : if (argtype)
2124 : : {
2125 : 8572265 : if (TREE_CODE (argtype) == TREE_LIST)
2126 : : {
2127 : 141270 : for (tree elt = argtype; elt && elt != void_list_node;
2128 : 83959 : elt = TREE_CHAIN (elt))
2129 : : {
2130 : 83959 : tree type = TREE_VALUE (elt);
2131 : 83959 : tree arg = build_stub_object (type);
2132 : 83959 : vec_safe_push (args, arg);
2133 : : }
2134 : : }
2135 : : else
2136 : : {
2137 : 8514954 : tree arg = build_stub_object (argtype);
2138 : 8514954 : args->quick_push (arg);
2139 : : }
2140 : : }
2141 : :
2142 : 23154012 : fns = lookup_fnfields (binfo, name, 0, complain);
2143 : 23154012 : rval = build_new_method_call (ob, fns, &args, binfo, flags, &fn, complain);
2144 : :
2145 : 23154012 : if (fn && rval == error_mark_node)
2146 : : return rval;
2147 : : else
2148 : 22403680 : return fn;
2149 : 23154012 : }
2150 : :
2151 : : /* Locate the dtor of TYPE. */
2152 : :
2153 : : tree
2154 : 2023 : get_dtor (tree type, tsubst_flags_t complain)
2155 : : {
2156 : 2023 : tree fn = locate_fn_flags (type, complete_dtor_identifier, NULL_TREE,
2157 : : LOOKUP_NORMAL, complain);
2158 : 2023 : if (fn == error_mark_node)
2159 : 12 : return NULL_TREE;
2160 : : return fn;
2161 : : }
2162 : :
2163 : : /* Locate the default ctor of TYPE. */
2164 : :
2165 : : tree
2166 : 18298 : locate_ctor (tree type)
2167 : : {
2168 : 18298 : tree fn;
2169 : :
2170 : 18298 : push_deferring_access_checks (dk_no_check);
2171 : 18298 : fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
2172 : : LOOKUP_SPECULATIVE, tf_none);
2173 : 18298 : pop_deferring_access_checks ();
2174 : 18298 : if (fn == error_mark_node)
2175 : 176 : return NULL_TREE;
2176 : : return fn;
2177 : : }
2178 : :
2179 : : /* Likewise, but give any appropriate errors. */
2180 : :
2181 : : tree
2182 : 1254 : get_default_ctor (tree type)
2183 : : {
2184 : 1254 : tree fn = locate_fn_flags (type, complete_ctor_identifier, NULL_TREE,
2185 : : LOOKUP_NORMAL, tf_warning_or_error);
2186 : 1254 : if (fn == error_mark_node)
2187 : 3 : return NULL_TREE;
2188 : : return fn;
2189 : : }
2190 : :
2191 : : /* Locate the copy ctor of TYPE. */
2192 : :
2193 : : tree
2194 : 543 : get_copy_ctor (tree type, tsubst_flags_t complain)
2195 : : {
2196 : 543 : int quals = (TYPE_HAS_CONST_COPY_CTOR (type)
2197 : 543 : ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
2198 : 543 : tree argtype = build_stub_type (type, quals, false);
2199 : 543 : tree fn = locate_fn_flags (type, complete_ctor_identifier, argtype,
2200 : : LOOKUP_NORMAL, complain);
2201 : 543 : if (fn == error_mark_node)
2202 : 6 : return NULL_TREE;
2203 : : return fn;
2204 : : }
2205 : :
2206 : : /* Locate the copy assignment operator of TYPE. */
2207 : :
2208 : : tree
2209 : 397 : get_copy_assign (tree type)
2210 : : {
2211 : 397 : int quals = (TYPE_HAS_CONST_COPY_ASSIGN (type)
2212 : 397 : ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED);
2213 : 397 : tree argtype = build_stub_type (type, quals, false);
2214 : 397 : tree fn = locate_fn_flags (type, assign_op_identifier, argtype,
2215 : : LOOKUP_NORMAL, tf_warning_or_error);
2216 : 397 : if (fn == error_mark_node)
2217 : 3 : return NULL_TREE;
2218 : : return fn;
2219 : : }
2220 : :
2221 : : /* walk_tree helper function for is_trivially_xible. If *TP is a call,
2222 : : return it if it calls something other than a trivial special member
2223 : : function. */
2224 : :
2225 : : static tree
2226 : 204531 : check_nontriv (tree *tp, int *, void *)
2227 : : {
2228 : 204531 : tree fn = cp_get_callee (*tp);
2229 : 204531 : if (fn == NULL_TREE)
2230 : : return NULL_TREE;
2231 : :
2232 : 4630 : if (TREE_CODE (fn) == ADDR_EXPR)
2233 : 4616 : fn = TREE_OPERAND (fn, 0);
2234 : :
2235 : 4630 : if (TREE_CODE (fn) != FUNCTION_DECL
2236 : 4630 : || !trivial_fn_p (fn))
2237 : 4630 : return fn;
2238 : : return NULL_TREE;
2239 : : }
2240 : :
2241 : : /* Return declval<T>() = declval<U>() treated as an unevaluated operand. */
2242 : :
2243 : : static tree
2244 : 1004665 : assignable_expr (tree to, tree from, bool explain)
2245 : : {
2246 : 1004665 : cp_unevaluated cp_uneval_guard;
2247 : 1004665 : tsubst_flags_t complain = explain ? tf_error : tf_none;
2248 : :
2249 : 1004665 : to = build_trait_object (to, complain);
2250 : 1004665 : if (to == error_mark_node)
2251 : : return error_mark_node;
2252 : :
2253 : 1004665 : from = build_trait_object (from, complain);
2254 : 1004665 : if (from == error_mark_node)
2255 : : return error_mark_node;
2256 : :
2257 : 1004665 : tree r = cp_build_modify_expr (input_location, to, NOP_EXPR, from, complain);
2258 : 1004665 : return r;
2259 : 1004665 : }
2260 : :
2261 : : /* The predicate condition for a template specialization
2262 : : is_constructible<T, Args...> shall be satisfied if and only if the
2263 : : following variable definition would be well-formed for some invented
2264 : : variable t: T t(create<Args>()...);
2265 : :
2266 : : Return something equivalent in well-formedness and triviality. */
2267 : :
2268 : : static tree
2269 : 2088965 : constructible_expr (tree to, tree from, bool explain)
2270 : : {
2271 : 2088965 : tree expr;
2272 : 2088965 : cp_unevaluated cp_uneval_guard;
2273 : 2088965 : tsubst_flags_t complain = explain ? tf_error : tf_none;
2274 : 2088965 : const int len = TREE_VEC_LENGTH (from);
2275 : 2088965 : if (CLASS_TYPE_P (to))
2276 : : {
2277 : 1136785 : if (ABSTRACT_CLASS_TYPE_P (to))
2278 : 5136 : return error_mark_node;
2279 : 1136746 : tree ctype = to;
2280 : 1136746 : vec<tree, va_gc> *args = NULL;
2281 : 1136746 : if (!TYPE_REF_P (to))
2282 : 1136746 : to = cp_build_reference_type (to, /*rval*/false);
2283 : 1136746 : tree ob = build_stub_object (to);
2284 : 1136746 : if (len == 0)
2285 : 416138 : expr = build_value_init (ctype, complain);
2286 : : else
2287 : : {
2288 : 720608 : vec_alloc (args, len);
2289 : 1445826 : for (tree arg : tree_vec_range (from))
2290 : 725218 : args->quick_push (build_stub_object (arg));
2291 : 720608 : expr = build_special_member_call (ob, complete_ctor_identifier, &args,
2292 : : ctype, LOOKUP_NORMAL, complain);
2293 : : }
2294 : 1136746 : if (expr == error_mark_node)
2295 : : return error_mark_node;
2296 : : /* The current state of the standard vis-a-vis LWG 2116 is that
2297 : : is_*constructible involves destruction as well. */
2298 : 1131665 : if (type_build_dtor_call (ctype))
2299 : : {
2300 : 168525 : tree dtor = build_special_member_call (ob, complete_dtor_identifier,
2301 : : NULL, ctype, LOOKUP_NORMAL,
2302 : : complain);
2303 : 168525 : if (dtor == error_mark_node)
2304 : : return error_mark_node;
2305 : 168509 : if (!TYPE_HAS_TRIVIAL_DESTRUCTOR (ctype))
2306 : 158148 : expr = build2 (COMPOUND_EXPR, void_type_node, expr, dtor);
2307 : : }
2308 : : }
2309 : : else
2310 : : {
2311 : 952180 : if (len == 0)
2312 : 197533 : return build_value_init (strip_array_types (to), complain);
2313 : 754647 : if (len > 1)
2314 : : {
2315 : 161 : if (cxx_dialect < cxx20)
2316 : : {
2317 : 133 : if (explain)
2318 : 2 : error ("too many initializers for non-class type %qT", to);
2319 : 133 : return error_mark_node;
2320 : : }
2321 : :
2322 : : /* In C++20 this is well-formed:
2323 : : using T = int[2];
2324 : : T t(1, 2);
2325 : : which means that std::is_constructible_v<int[2], int, int>
2326 : : should be true. */
2327 : 28 : vec<constructor_elt, va_gc> *v;
2328 : 28 : vec_alloc (v, len);
2329 : 84 : for (tree arg : tree_vec_range (from))
2330 : : {
2331 : 56 : tree stub = build_stub_object (arg);
2332 : 56 : constructor_elt elt = { NULL_TREE, stub };
2333 : 56 : v->quick_push (elt);
2334 : : }
2335 : 28 : from = build_constructor (init_list_type_node, v);
2336 : 28 : CONSTRUCTOR_IS_DIRECT_INIT (from) = true;
2337 : 28 : CONSTRUCTOR_IS_PAREN_INIT (from) = true;
2338 : : }
2339 : : else
2340 : 754486 : from = build_stub_object (TREE_VEC_ELT (from, 0));
2341 : :
2342 : 754514 : tree orig_from = from;
2343 : 754514 : expr = perform_direct_initialization_if_possible (to, from,
2344 : : /*cast*/false,
2345 : : complain);
2346 : : /* If t(e) didn't work, maybe t{e} will. */
2347 : 754514 : if (expr == NULL_TREE
2348 : 754514 : && len == 1
2349 : 2306 : && cxx_dialect >= cxx20)
2350 : : {
2351 : 1295 : from = build_constructor_single (init_list_type_node, NULL_TREE,
2352 : : from);
2353 : 1295 : CONSTRUCTOR_IS_DIRECT_INIT (from) = true;
2354 : 1295 : CONSTRUCTOR_IS_PAREN_INIT (from) = true;
2355 : 1295 : expr = perform_direct_initialization_if_possible (to, from,
2356 : : /*cast*/false,
2357 : : complain);
2358 : : }
2359 : :
2360 : 754514 : if (expr == NULL_TREE && explain)
2361 : : {
2362 : 15 : if (len > 1)
2363 : 1 : error ("too many initializers for non-class type %qT", to);
2364 : : else
2365 : : {
2366 : : /* Redo the implicit conversion for diagnostics. */
2367 : 14 : int count = errorcount + warningcount;
2368 : 14 : perform_implicit_conversion_flags (to, orig_from, complain,
2369 : : LOOKUP_NORMAL);
2370 : 14 : if (count == errorcount + warningcount)
2371 : : /* The message may have been suppressed due to -w + -fpermissive,
2372 : : emit a generic response instead. */
2373 : 0 : error ("the conversion is invalid");
2374 : : }
2375 : : }
2376 : : }
2377 : : return expr;
2378 : 2088965 : }
2379 : :
2380 : : /* Valid if "Either T is a reference type, or T is a complete object type for
2381 : : which the expression declval<U&>().~U() is well-formed when treated as an
2382 : : unevaluated operand ([expr.context]), where U is remove_all_extents_t<T>."
2383 : :
2384 : : For a class U, return the destructor call; otherwise return void_node if
2385 : : valid or error_mark_node if not. */
2386 : :
2387 : : static tree
2388 : 49559 : destructible_expr (tree to, bool explain)
2389 : : {
2390 : 49559 : cp_unevaluated cp_uneval_guard;
2391 : 49559 : tsubst_flags_t complain = explain ? tf_error : tf_none;
2392 : 49559 : int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
2393 : 49559 : if (TYPE_REF_P (to))
2394 : 57 : return void_node;
2395 : 49502 : if (!COMPLETE_TYPE_P (complete_type (to)))
2396 : : {
2397 : 28 : if (explain)
2398 : 0 : error_at (location_of (to), "%qT is incomplete", to);
2399 : 28 : return error_mark_node;
2400 : : }
2401 : 49474 : to = strip_array_types (to);
2402 : 49474 : if (CLASS_TYPE_P (to))
2403 : : {
2404 : 26948 : to = build_trait_object (to, complain);
2405 : 26948 : return build_delete (input_location, TREE_TYPE (to), to,
2406 : 26948 : sfk_complete_destructor, flags, 0, complain);
2407 : : }
2408 : : /* [expr.prim.id.dtor] If the id-expression names a pseudo-destructor, T
2409 : : shall be a scalar type.... */
2410 : 22526 : else if (scalarish_type_p (to))
2411 : 22504 : return void_node;
2412 : : else
2413 : 22 : return error_mark_node;
2414 : 49559 : }
2415 : :
2416 : : /* Returns a tree iff TO is assignable (if CODE is MODIFY_EXPR) or
2417 : : constructible (otherwise) from FROM, which is a single type for
2418 : : assignment or a list of types for construction. If EXPLAIN is
2419 : : set, emit a diagnostic explaining why the operation failed. */
2420 : :
2421 : : static tree
2422 : 3143660 : is_xible_helper (enum tree_code code, tree to, tree from, bool explain)
2423 : : {
2424 : 3143660 : to = complete_type (to);
2425 : 3143660 : deferring_access_check_sentinel acs (dk_no_deferred);
2426 : :
2427 : 3143660 : if (VOID_TYPE_P (to))
2428 : : {
2429 : 230 : if (explain)
2430 : 42 : error_at (location_of (to), "%qT is incomplete", to);
2431 : 230 : return error_mark_node;
2432 : : }
2433 : 3143430 : if (from
2434 : 3093871 : && FUNC_OR_METHOD_TYPE_P (from)
2435 : 3143463 : && (TYPE_READONLY (from) || FUNCTION_REF_QUALIFIED (from)))
2436 : : {
2437 : 17 : if (explain)
2438 : 0 : error ("%qT is a qualified function type", from);
2439 : 17 : return error_mark_node;
2440 : : }
2441 : :
2442 : 3143413 : tree expr;
2443 : 3143413 : if (code == MODIFY_EXPR)
2444 : 1004665 : expr = assignable_expr (to, from, explain);
2445 : 2138748 : else if (code == BIT_NOT_EXPR)
2446 : 49559 : expr = destructible_expr (to, explain);
2447 : 2089189 : else if (TREE_CODE (to) == ARRAY_TYPE && !TYPE_DOMAIN (to))
2448 : : {
2449 : 224 : if (explain)
2450 : 0 : error ("cannot construct an array of unknown bound");
2451 : 224 : return error_mark_node;
2452 : : }
2453 : : else
2454 : 2088965 : expr = constructible_expr (to, from, explain);
2455 : : return expr;
2456 : : }
2457 : :
2458 : : /* Returns true iff TO is trivially assignable (if CODE is MODIFY_EXPR) or
2459 : : constructible (otherwise) from FROM, which is a single type for
2460 : : assignment or a list of types for construction. If EXPLAIN, diagnose
2461 : : why we returned false. */
2462 : :
2463 : : bool
2464 : 93242 : is_trivially_xible (enum tree_code code, tree to, tree from,
2465 : : bool explain/*=false*/)
2466 : : {
2467 : 93242 : tree expr = is_xible_helper (code, to, from, explain);
2468 : 93242 : if (expr == NULL_TREE || expr == error_mark_node)
2469 : : return false;
2470 : :
2471 : 92008 : tree nt = cp_walk_tree_without_duplicates (&expr, check_nontriv, NULL);
2472 : 92008 : if (explain && nt)
2473 : 12 : inform (location_of (nt), "%qE is non-trivial", nt);
2474 : 92008 : return !nt;
2475 : : }
2476 : :
2477 : : /* Returns true iff TO is nothrow assignable (if CODE is MODIFY_EXPR) or
2478 : : constructible (otherwise) from FROM, which is a single type for
2479 : : assignment or a list of types for construction. If EXPLAIN, diagnose
2480 : : why we returned false. */
2481 : :
2482 : : bool
2483 : 956533 : is_nothrow_xible (enum tree_code code, tree to, tree from,
2484 : : bool explain/*=false*/)
2485 : : {
2486 : 956533 : ++cp_noexcept_operand;
2487 : 956533 : tree expr = is_xible_helper (code, to, from, explain);
2488 : 956533 : --cp_noexcept_operand;
2489 : 956533 : if (expr == NULL_TREE || expr == error_mark_node)
2490 : : return false;
2491 : :
2492 : 956140 : bool is_noexcept = expr_noexcept_p (expr, tf_none);
2493 : 956140 : if (explain && !is_noexcept)
2494 : 11 : explain_not_noexcept (expr);
2495 : : return is_noexcept;
2496 : : }
2497 : :
2498 : : /* Returns true iff TO is assignable (if CODE is MODIFY_EXPR) or
2499 : : constructible (otherwise) from FROM, which is a single type for
2500 : : assignment or a list of types for construction. If EXPLAIN, diagnose
2501 : : why we returned false. */
2502 : :
2503 : : bool
2504 : 2093885 : is_xible (enum tree_code code, tree to, tree from, bool explain/*=false*/)
2505 : : {
2506 : 2093885 : tree expr = is_xible_helper (code, to, from, explain);
2507 : 2093885 : if (expr == error_mark_node)
2508 : : return false;
2509 : 2032547 : return !!expr;
2510 : : }
2511 : :
2512 : : /* Return true iff conjunction_v<is_reference<T>, is_constructible<T, U>> is
2513 : : true, and the initialization
2514 : : T t(VAL<U>); // DIRECT_INIT_P
2515 : : or
2516 : : T t = VAL<U>; // !DIRECT_INIT_P
2517 : : binds t to a temporary object whose lifetime is extended.
2518 : : VAL<T> is defined in [meta.unary.prop]:
2519 : : -- If T is a reference or function type, VAL<T> is an expression with the
2520 : : same type and value category as declval<T>().
2521 : : -- Otherwise, VAL<T> is a prvalue that initially has type T. */
2522 : :
2523 : : bool
2524 : 119014 : ref_xes_from_temporary (tree to, tree from, bool direct_init_p)
2525 : : {
2526 : : /* Check is_reference<T>. */
2527 : 119014 : if (!TYPE_REF_P (to))
2528 : : return false;
2529 : : /* We don't check is_constructible<T, U>: if T isn't constructible
2530 : : from U, we won't be able to create a conversion. */
2531 : 12442 : tree val = build_trait_object (from, tf_none);
2532 : 12442 : if (val == error_mark_node)
2533 : : return false;
2534 : 12442 : if (!TYPE_REF_P (from) && TREE_CODE (from) != FUNCTION_TYPE)
2535 : 391 : val = CLASS_TYPE_P (from) ? force_rvalue (val, tf_none) : rvalue (val);
2536 : 12442 : return ref_conv_binds_to_temporary (to, val, direct_init_p).is_true ();
2537 : : }
2538 : :
2539 : : /* Worker for is_{,nothrow_}convertible. Attempt to perform an implicit
2540 : : conversion from FROM to TO and return the result. If EXPLAIN, emit a
2541 : : diagnostic about why the conversion failed. */
2542 : :
2543 : : static tree
2544 : 1308500 : is_convertible_helper (tree from, tree to, bool explain)
2545 : : {
2546 : 1308500 : if (VOID_TYPE_P (from) && VOID_TYPE_P (to))
2547 : 44 : return integer_one_node;
2548 : 1308456 : cp_unevaluated u;
2549 : 1308456 : tsubst_flags_t complain = explain ? tf_error : tf_none;
2550 : :
2551 : : /* std::is_{,nothrow_}convertible test whether the imaginary function
2552 : : definition
2553 : :
2554 : : To test() { return std::declval<From>(); }
2555 : :
2556 : : is well-formed. A function can't return a function. */
2557 : 1308456 : if (FUNC_OR_METHOD_TYPE_P (to))
2558 : : {
2559 : 78 : if (explain)
2560 : 0 : error ("%qT is a function type", to);
2561 : 78 : return error_mark_node;
2562 : : }
2563 : :
2564 : 1308378 : tree expr = build_trait_object (from, complain);
2565 : 1308378 : if (expr == error_mark_node)
2566 : : return error_mark_node;
2567 : :
2568 : 1308372 : deferring_access_check_sentinel acs (dk_no_deferred);
2569 : 1308372 : return perform_implicit_conversion (to, expr, complain);
2570 : 1308456 : }
2571 : :
2572 : : /* Return true if FROM can be converted to TO using implicit conversions,
2573 : : or both FROM and TO are possibly cv-qualified void. NB: This doesn't
2574 : : implement the "Access checks are performed as if from a context unrelated
2575 : : to either type" restriction. */
2576 : :
2577 : : bool
2578 : 1307744 : is_convertible (tree from, tree to, bool explain/*=false*/)
2579 : : {
2580 : 1307744 : tree expr = is_convertible_helper (from, to, explain);
2581 : 1307744 : if (expr == error_mark_node)
2582 : : return false;
2583 : 1098304 : return !!expr;
2584 : : }
2585 : :
2586 : : /* Like is_convertible, but the conversion is also noexcept. */
2587 : :
2588 : : bool
2589 : 756 : is_nothrow_convertible (tree from, tree to, bool explain/*=false*/)
2590 : : {
2591 : 756 : tree expr = is_convertible_helper (from, to, explain);
2592 : 756 : if (expr == NULL_TREE || expr == error_mark_node)
2593 : : return false;
2594 : :
2595 : 404 : bool is_noexcept = expr_noexcept_p (expr, tf_none);
2596 : 404 : if (explain && !is_noexcept)
2597 : 3 : explain_not_noexcept (expr);
2598 : : return is_noexcept;
2599 : : }
2600 : :
2601 : : /* Categorize various special_function_kinds. */
2602 : : #define SFK_CTOR_P(sfk) \
2603 : : ((sfk) >= sfk_constructor && (sfk) <= sfk_move_constructor)
2604 : : #define SFK_DTOR_P(sfk) \
2605 : : ((sfk) == sfk_destructor || (sfk) == sfk_virtual_destructor)
2606 : : #define SFK_ASSIGN_P(sfk) \
2607 : : ((sfk) == sfk_copy_assignment || (sfk) == sfk_move_assignment)
2608 : : #define SFK_COPY_P(sfk) \
2609 : : ((sfk) == sfk_copy_constructor || (sfk) == sfk_copy_assignment)
2610 : : #define SFK_MOVE_P(sfk) \
2611 : : ((sfk) == sfk_move_constructor || (sfk) == sfk_move_assignment)
2612 : :
2613 : : /* Subroutine of synthesized_method_walk. Update SPEC_P, TRIVIAL_P and
2614 : : DELETED_P or give an error message MSG with argument ARG. */
2615 : :
2616 : : static void
2617 : 21738859 : process_subob_fn (tree fn, special_function_kind sfk, tree *spec_p,
2618 : : bool *trivial_p, bool *deleted_p, bool *constexpr_p,
2619 : : bool diag, tree arg, bool dtor_from_ctor = false)
2620 : : {
2621 : 21738859 : if (!fn || fn == error_mark_node)
2622 : : {
2623 : 759467 : if (deleted_p)
2624 : 759446 : *deleted_p = true;
2625 : 759467 : return;
2626 : : }
2627 : :
2628 : 20979392 : if (spec_p)
2629 : : {
2630 : 3573397 : if (!maybe_instantiate_noexcept (fn))
2631 : 3 : *spec_p = error_mark_node;
2632 : : else
2633 : : {
2634 : 3573394 : tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn));
2635 : 3573394 : *spec_p = merge_exception_specifiers (*spec_p, raises);
2636 : : }
2637 : : }
2638 : :
2639 : 20979392 : if (!trivial_fn_p (fn) && !dtor_from_ctor)
2640 : : {
2641 : 6522678 : if (trivial_p)
2642 : 3888935 : *trivial_p = false;
2643 : 6522678 : if (TREE_CODE (arg) == FIELD_DECL
2644 : 6522678 : && TREE_CODE (DECL_CONTEXT (arg)) == UNION_TYPE)
2645 : : {
2646 : 2065 : if (deleted_p)
2647 : 2061 : *deleted_p = true;
2648 : 2065 : if (diag)
2649 : 25 : error ("union member %q+D with non-trivial %qD", arg, fn);
2650 : : }
2651 : : }
2652 : :
2653 : 20979392 : if (constexpr_p && !DECL_DECLARED_CONSTEXPR_P (fn))
2654 : : {
2655 : 4048026 : *constexpr_p = false;
2656 : 4048026 : if (diag)
2657 : : {
2658 : 10 : inform (DECL_SOURCE_LOCATION (fn),
2659 : 10 : SFK_DTOR_P (sfk)
2660 : : ? G_("defaulted destructor calls non-%<constexpr%> %qD")
2661 : : : G_("defaulted constructor calls non-%<constexpr%> %qD"),
2662 : : fn);
2663 : 10 : explain_invalid_constexpr_fn (fn);
2664 : : }
2665 : : }
2666 : : }
2667 : :
2668 : : /* Subroutine of synthesized_method_walk to allow recursion into anonymous
2669 : : aggregates. If DTOR_FROM_CTOR is true, we're walking subobject destructors
2670 : : called from a synthesized constructor, in which case we don't consider
2671 : : the triviality of the subobject destructor. */
2672 : :
2673 : : static void
2674 : 39768997 : walk_field_subobs (tree fields, special_function_kind sfk, tree fnname,
2675 : : int quals, tree *spec_p, bool *trivial_p,
2676 : : bool *deleted_p, bool *constexpr_p,
2677 : : bool diag, int flags, tsubst_flags_t complain,
2678 : : bool dtor_from_ctor)
2679 : : {
2680 : 39768997 : if (!fields)
2681 : : return;
2682 : :
2683 : 39768993 : tree ctx = DECL_CONTEXT (fields);
2684 : :
2685 : : /* CWG2084: A defaulted default ctor for a union with a DMI only initializes
2686 : : that member, so don't check other members. */
2687 : 39768993 : enum { unknown, no, yes }
2688 : 39768993 : only_dmi_mem = (sfk == sfk_constructor && TREE_CODE (ctx) == UNION_TYPE
2689 : 39768993 : ? unknown : no);
2690 : :
2691 : 39819070 : again:
2692 : 777904951 : for (tree field = fields; field; field = DECL_CHAIN (field))
2693 : : {
2694 : 738234273 : tree mem_type, argtype, rval;
2695 : :
2696 : 1442354601 : if (TREE_CODE (field) != FIELD_DECL
2697 : 40987639 : || DECL_ARTIFICIAL (field)
2698 : 772357676 : || DECL_UNNAMED_BIT_FIELD (field))
2699 : 704120328 : continue;
2700 : :
2701 : : /* Variant members only affect deletedness. In particular, they don't
2702 : : affect the exception-specification of a user-provided destructor,
2703 : : which we're figuring out via get_defaulted_eh_spec. So if we aren't
2704 : : asking if this is deleted, don't even look up the function; we don't
2705 : : want an error about a deleted function we aren't actually calling. */
2706 : 34113945 : if (sfk == sfk_destructor && deleted_p == NULL
2707 : 2645635 : && TREE_CODE (ctx) == UNION_TYPE)
2708 : : break;
2709 : :
2710 : 33965553 : if (only_dmi_mem != no)
2711 : : {
2712 : 132020 : if (DECL_INITIAL (field))
2713 : : only_dmi_mem = yes;
2714 : : else
2715 : : /* Don't check this until we know there's no DMI. */
2716 : 131839 : continue;
2717 : : }
2718 : :
2719 : 33833714 : mem_type = strip_array_types (TREE_TYPE (field));
2720 : 33833714 : if (SFK_ASSIGN_P (sfk))
2721 : : {
2722 : 3481062 : bool bad = true;
2723 : 3481062 : if (CP_TYPE_CONST_P (mem_type) && !CLASS_TYPE_P (mem_type))
2724 : : {
2725 : 139 : if (diag)
2726 : 7 : error ("non-static const member %q#D, cannot use default "
2727 : : "assignment operator", field);
2728 : : }
2729 : 3480923 : else if (TYPE_REF_P (mem_type))
2730 : : {
2731 : 1458 : if (diag)
2732 : 2 : error ("non-static reference member %q#D, cannot use "
2733 : : "default assignment operator", field);
2734 : : }
2735 : : else
2736 : : bad = false;
2737 : :
2738 : 3481062 : if (bad && deleted_p)
2739 : 1597 : *deleted_p = true;
2740 : : }
2741 : 30352652 : else if (sfk == sfk_constructor || sfk == sfk_inheriting_constructor)
2742 : : {
2743 : 2672122 : bool bad;
2744 : :
2745 : 2672122 : if (DECL_INITIAL (field))
2746 : : {
2747 : 712905 : if (diag && DECL_INITIAL (field) == error_mark_node)
2748 : 0 : inform (DECL_SOURCE_LOCATION (field),
2749 : : "initializer for %q#D is invalid", field);
2750 : 712905 : if (trivial_p)
2751 : 573249 : *trivial_p = false;
2752 : : /* Core 1351: If the field has an NSDMI that could throw, the
2753 : : default constructor is noexcept(false). */
2754 : 712905 : if (spec_p)
2755 : : {
2756 : 144822 : tree nsdmi = get_nsdmi (field, /*ctor*/false, complain);
2757 : 144822 : if (nsdmi == error_mark_node)
2758 : 121 : *spec_p = error_mark_node;
2759 : 144701 : else if (*spec_p != error_mark_node
2760 : 144701 : && !expr_noexcept_p (nsdmi, tf_none))
2761 : 910 : *spec_p = noexcept_false_spec;
2762 : : }
2763 : : /* Don't do the normal processing. */
2764 : 712905 : continue;
2765 : 712905 : }
2766 : :
2767 : 1959217 : bad = false;
2768 : 1959217 : if (CP_TYPE_CONST_P (mem_type)
2769 : 1959217 : && default_init_uninitialized_part (mem_type))
2770 : : {
2771 : 395 : if (diag)
2772 : : {
2773 : 53 : error ("uninitialized const member in %q#T",
2774 : : current_class_type);
2775 : 53 : inform (DECL_SOURCE_LOCATION (field),
2776 : : "%q#D should be initialized", field);
2777 : : }
2778 : : bad = true;
2779 : : }
2780 : 1958822 : else if (TYPE_REF_P (mem_type))
2781 : : {
2782 : 10182 : if (diag)
2783 : : {
2784 : 48 : error ("uninitialized reference member in %q#T",
2785 : : current_class_type);
2786 : 48 : inform (DECL_SOURCE_LOCATION (field),
2787 : : "%q#D should be initialized", field);
2788 : : }
2789 : : bad = true;
2790 : : }
2791 : :
2792 : 1959217 : if (bad && deleted_p)
2793 : 10577 : *deleted_p = true;
2794 : :
2795 : : /* Before C++20, for an implicitly-defined default constructor to
2796 : : be constexpr, every member must have a user-provided default
2797 : : constructor or an explicit initializer. */
2798 : 1959217 : if (constexpr_p
2799 : 1700344 : && cxx_dialect < cxx20
2800 : 997281 : && !CLASS_TYPE_P (mem_type)
2801 : 2658965 : && TREE_CODE (ctx) != UNION_TYPE)
2802 : : {
2803 : 611847 : *constexpr_p = false;
2804 : 611847 : if (diag)
2805 : 2 : inform (DECL_SOURCE_LOCATION (field),
2806 : : "defaulted default constructor does not "
2807 : : "initialize %q#D", field);
2808 : : }
2809 : : }
2810 : 27680530 : else if (sfk == sfk_copy_constructor)
2811 : : {
2812 : : /* 12.8p11b5 */
2813 : 4553028 : if (TYPE_REF_P (mem_type)
2814 : 4553028 : && TYPE_REF_IS_RVALUE (mem_type))
2815 : : {
2816 : 343 : if (diag)
2817 : 3 : error ("copying non-static data member %q#D of rvalue "
2818 : : "reference type", field);
2819 : 343 : if (deleted_p)
2820 : 343 : *deleted_p = true;
2821 : : }
2822 : : }
2823 : :
2824 : 33120809 : if (!CLASS_TYPE_P (mem_type))
2825 : 23543396 : continue;
2826 : :
2827 : 9577413 : if (ANON_AGGR_TYPE_P (mem_type))
2828 : : {
2829 : 194704 : walk_field_subobs (TYPE_FIELDS (mem_type), sfk, fnname, quals,
2830 : : spec_p, trivial_p, deleted_p, constexpr_p,
2831 : : diag, flags, complain, dtor_from_ctor);
2832 : 194704 : continue;
2833 : : }
2834 : :
2835 : 9382709 : if (SFK_COPY_P (sfk) || SFK_MOVE_P (sfk))
2836 : : {
2837 : 3388617 : int mem_quals = cp_type_quals (mem_type) | quals;
2838 : 3388617 : if (DECL_MUTABLE_P (field))
2839 : 252 : mem_quals &= ~TYPE_QUAL_CONST;
2840 : 3388617 : argtype = build_stub_type (mem_type, mem_quals, SFK_MOVE_P (sfk));
2841 : 3388617 : }
2842 : : else
2843 : : argtype = NULL_TREE;
2844 : :
2845 : 9382709 : rval = locate_fn_flags (mem_type, fnname, argtype, flags, complain);
2846 : :
2847 : 9382709 : process_subob_fn (rval, sfk, spec_p, trivial_p, deleted_p,
2848 : : constexpr_p, diag, field, dtor_from_ctor);
2849 : : }
2850 : :
2851 : : /* We didn't find a DMI in this union, now check all the members. */
2852 : 39819070 : if (only_dmi_mem == unknown)
2853 : : {
2854 : 50077 : only_dmi_mem = no;
2855 : 50077 : goto again;
2856 : : }
2857 : : }
2858 : :
2859 : : /* Base walker helper for synthesized_method_walk. Inspect a direct
2860 : : or virtual base. BINFO is the parent type's binfo. BASE_BINFO is
2861 : : the base binfo of interests. All other parms are as for
2862 : : synthesized_method_walk, or its local vars. */
2863 : :
2864 : : static tree
2865 : 8502453 : synthesized_method_base_walk (tree binfo, tree base_binfo,
2866 : : special_function_kind sfk, tree fnname, int quals,
2867 : : tree *inheriting_ctor, tree inherited_parms,
2868 : : int flags, bool diag,
2869 : : tree *spec_p, bool *trivial_p,
2870 : : bool *deleted_p, bool *constexpr_p)
2871 : : {
2872 : 8502453 : bool inherited_binfo = false;
2873 : 8502453 : tree argtype = NULL_TREE;
2874 : 8502453 : deferring_kind defer = dk_no_deferred;
2875 : :
2876 : 8502453 : if (SFK_COPY_P (sfk) || SFK_MOVE_P (sfk))
2877 : 3732759 : argtype = build_stub_type (BINFO_TYPE (base_binfo), quals, SFK_MOVE_P (sfk));
2878 : 4769694 : else if (inheriting_ctor
2879 : 4769694 : && (inherited_binfo
2880 : 4769694 : = binfo_inherited_from (binfo, base_binfo, *inheriting_ctor)))
2881 : : {
2882 : 57338 : argtype = inherited_parms;
2883 : : /* Don't check access on the inherited constructor. */
2884 : 57338 : if (flag_new_inheriting_ctors)
2885 : : defer = dk_deferred;
2886 : : }
2887 : 4689383 : else if (cxx_dialect >= cxx14 && sfk == sfk_virtual_destructor
2888 : 1597114 : && BINFO_VIRTUAL_P (base_binfo)
2889 : 4896806 : && ABSTRACT_CLASS_TYPE_P (BINFO_TYPE (binfo)))
2890 : : /* Don't check access when looking at vbases of abstract class's
2891 : : virtual destructor. */
2892 : : defer = dk_no_check;
2893 : :
2894 : 3732759 : if (defer != dk_no_deferred)
2895 : 57467 : push_deferring_access_checks (defer);
2896 : 17004680 : tree rval = locate_fn_flags (base_binfo, fnname, argtype, flags,
2897 : : diag ? tf_warning_or_error : tf_none);
2898 : 8502453 : if (defer != dk_no_deferred)
2899 : 57467 : pop_deferring_access_checks ();
2900 : :
2901 : : /* Replace an inherited template with the appropriate specialization. */
2902 : 8502453 : if (inherited_binfo && rval
2903 : 57338 : && DECL_P (*inheriting_ctor) && DECL_P (rval)
2904 : 8559758 : && DECL_CONTEXT (*inheriting_ctor) == DECL_CONTEXT (rval))
2905 : 57293 : *inheriting_ctor = DECL_CLONED_FUNCTION (rval);
2906 : :
2907 : 17004906 : process_subob_fn (rval, sfk, spec_p, trivial_p, deleted_p,
2908 : 8502453 : constexpr_p, diag, BINFO_TYPE (base_binfo));
2909 : 8502453 : if (SFK_CTOR_P (sfk)
2910 : 12365581 : && (!BINFO_VIRTUAL_P (base_binfo)
2911 : 58176 : || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))))
2912 : : {
2913 : : /* In a constructor we also need to check the subobject
2914 : : destructors for cleanup of partially constructed objects. */
2915 : 3853697 : tree dtor = locate_fn_flags (base_binfo, complete_dtor_identifier,
2916 : : NULL_TREE, flags,
2917 : : diag ? tf_warning_or_error : tf_none);
2918 : : /* Note that we don't pass down trivial_p; the subobject
2919 : : destructors don't affect triviality of the constructor. Nor
2920 : : do they affect constexpr-ness (a constant expression doesn't
2921 : : throw) or exception-specification (a throw from one of the
2922 : : dtors would be a double-fault). */
2923 : 7707394 : process_subob_fn (dtor, sfk, NULL, NULL, deleted_p, NULL, false,
2924 : 3853697 : BINFO_TYPE (base_binfo), /*dtor_from_ctor*/true);
2925 : : }
2926 : :
2927 : 8502453 : return rval;
2928 : : }
2929 : :
2930 : : /* The caller wants to generate an implicit declaration of SFK for
2931 : : CTYPE which is const if relevant and CONST_P is set. If SPEC_P,
2932 : : TRIVIAL_P, DELETED_P or CONSTEXPR_P are non-null, set their
2933 : : referent appropriately. If DIAG is true, we're either being called
2934 : : from maybe_explain_implicit_delete to give errors, or if
2935 : : CONSTEXPR_P is non-null, from explain_invalid_constexpr_fn. */
2936 : :
2937 : : static void
2938 : 26085120 : synthesized_method_walk (tree ctype, special_function_kind sfk, bool const_p,
2939 : : tree *spec_p, bool *trivial_p, bool *deleted_p,
2940 : : bool *constexpr_p, bool diag,
2941 : : tree *inheriting_ctor, tree inherited_parms)
2942 : : {
2943 : 26085120 : tree binfo, base_binfo;
2944 : 26085120 : int i;
2945 : :
2946 : : /* SFK must be exactly one category. */
2947 : 26085120 : gcc_checking_assert (SFK_DTOR_P(sfk) + SFK_CTOR_P(sfk)
2948 : : + SFK_ASSIGN_P(sfk) == 1);
2949 : :
2950 : 26085120 : if (spec_p)
2951 : 3772710 : *spec_p = (cxx_dialect >= cxx11 ? noexcept_true_spec : empty_except_spec);
2952 : :
2953 : 26085120 : if (deleted_p)
2954 : : {
2955 : : /* "The closure type associated with a lambda-expression has a deleted
2956 : : default constructor and a deleted copy assignment operator."
2957 : : This is diagnosed in maybe_explain_implicit_delete.
2958 : : In C++20, only lambda-expressions with lambda-captures have those
2959 : : deleted. */
2960 : 44133276 : if (LAMBDA_TYPE_P (ctype)
2961 : 793875 : && (sfk == sfk_constructor || sfk == sfk_copy_assignment)
2962 : 22553606 : && (cxx_dialect < cxx20
2963 : 288900 : || LAMBDA_EXPR_CAPTURE_LIST (CLASSTYPE_LAMBDA_EXPR (ctype))
2964 : 15774 : || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE
2965 : 15774 : (CLASSTYPE_LAMBDA_EXPR (ctype)) != CPLD_NONE))
2966 : : {
2967 : 148498 : *deleted_p = true;
2968 : 148498 : return;
2969 : : }
2970 : :
2971 : 22241078 : *deleted_p = false;
2972 : : }
2973 : :
2974 : 25936622 : bool check_vdtor = false;
2975 : 25936622 : tree fnname;
2976 : :
2977 : 25936622 : if (SFK_DTOR_P (sfk))
2978 : : {
2979 : 8549972 : check_vdtor = true;
2980 : : /* The synthesized method will call base dtors, but check complete
2981 : : here to avoid having to deal with VTT. */
2982 : 8549972 : fnname = complete_dtor_identifier;
2983 : : }
2984 : 17386650 : else if (SFK_ASSIGN_P (sfk))
2985 : 3727571 : fnname = assign_op_identifier;
2986 : : else
2987 : 13659079 : fnname = complete_ctor_identifier;
2988 : :
2989 : 51815927 : gcc_assert ((sfk == sfk_inheriting_constructor)
2990 : : == (inheriting_ctor && *inheriting_ctor != NULL_TREE));
2991 : :
2992 : : /* If that user-written default constructor would satisfy the
2993 : : requirements of a constexpr constructor (7.1.5), the
2994 : : implicitly-defined default constructor is constexpr.
2995 : :
2996 : : C++20:
2997 : : The implicitly-defined copy/move assignment operator is constexpr if
2998 : : - X is a literal type, and
2999 : : - the assignment operator selected to copy/move each direct base class
3000 : : subobject is a constexpr function, and
3001 : : - for each non-static data member of X that is of class type (or array
3002 : : thereof), the assignment operator selected to copy/move that
3003 : : member is a constexpr function.
3004 : :
3005 : : C++23:
3006 : : The implicitly-defined copy/move assignment operator is constexpr. */
3007 : 25936622 : if (constexpr_p)
3008 : 22240521 : *constexpr_p = (SFK_CTOR_P (sfk)
3009 : 9402664 : || (SFK_ASSIGN_P (sfk) && cxx_dialect >= cxx14)
3010 : 28128923 : || (SFK_DTOR_P (sfk) && cxx_dialect >= cxx20));
3011 : :
3012 : 25936622 : bool expected_trivial = type_has_trivial_fn (ctype, sfk);
3013 : 25936622 : if (trivial_p)
3014 : 22240504 : *trivial_p = expected_trivial;
3015 : :
3016 : : /* The TYPE_HAS_COMPLEX_* flags tell us about constraints from base
3017 : : class versions and other properties of the type. But a subobject
3018 : : class can be trivially copyable and yet have overload resolution
3019 : : choose a template constructor for initialization, depending on
3020 : : rvalueness and cv-quals. And furthermore, a member in a base might
3021 : : be trivial but deleted or otherwise not callable. So we can't exit
3022 : : early in C++0x. The same considerations apply in C++98/03, but
3023 : : there the definition of triviality does not consider overload
3024 : : resolution, so a constructor can be trivial even if it would otherwise
3025 : : call a non-trivial constructor. */
3026 : 25936622 : if (expected_trivial
3027 : 18893289 : && (!(SFK_COPY_P (sfk) || SFK_MOVE_P (sfk)) || cxx_dialect < cxx11))
3028 : : {
3029 : 8293451 : if (constexpr_p && sfk == sfk_constructor)
3030 : : {
3031 : 2609913 : bool cx = trivial_default_constructor_is_constexpr (ctype);
3032 : 2609913 : *constexpr_p = cx;
3033 : 2609913 : if (diag && !cx && TREE_CODE (ctype) == UNION_TYPE)
3034 : : /* A trivial constructor doesn't have any NSDMI. */
3035 : 2 : inform (input_location, "defaulted default constructor does "
3036 : : "not initialize any non-static data member");
3037 : : }
3038 : 8293451 : if (!diag && cxx_dialect < cxx11)
3039 : : return;
3040 : : }
3041 : :
3042 : 25924560 : bool push_to_top = maybe_push_to_top_level (TYPE_NAME (ctype));
3043 : 25924560 : ++cp_unevaluated_operand;
3044 : 25924560 : ++c_inhibit_evaluation_warnings;
3045 : 25924560 : push_deferring_access_checks (dk_no_deferred);
3046 : :
3047 : 25924560 : tree scope = push_scope (ctype);
3048 : :
3049 : 25924560 : int flags = LOOKUP_NORMAL | LOOKUP_SPECULATIVE;
3050 : 25924560 : if (sfk != sfk_inheriting_constructor)
3051 : 25867243 : flags |= LOOKUP_DEFAULTED;
3052 : :
3053 : 25924560 : tsubst_flags_t complain = diag ? tf_warning_or_error : tf_none;
3054 : 25924560 : if (diag && spec_p)
3055 : : /* We're in get_defaulted_eh_spec; we don't actually want any walking
3056 : : diagnostics, we just want complain set. */
3057 : 3323037 : diag = false;
3058 : 25924560 : int quals = const_p ? TYPE_QUAL_CONST : TYPE_UNQUALIFIED;
3059 : :
3060 : 34215167 : for (binfo = TYPE_BINFO (ctype), i = 0;
3061 : 34215167 : BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
3062 : : {
3063 : 8290607 : if (!SFK_ASSIGN_P (sfk) && BINFO_VIRTUAL_P (base_binfo))
3064 : : /* We'll handle virtual bases below. */
3065 : 58548 : continue;
3066 : :
3067 : 8232059 : tree fn = synthesized_method_base_walk (binfo, base_binfo,
3068 : : sfk, fnname, quals,
3069 : : inheriting_ctor, inherited_parms,
3070 : : flags, diag, spec_p, trivial_p,
3071 : : deleted_p, constexpr_p);
3072 : :
3073 : 218 : if (diag && SFK_ASSIGN_P (sfk) && SFK_MOVE_P (sfk)
3074 : 100 : && BINFO_VIRTUAL_P (base_binfo)
3075 : 55 : && fn && TREE_CODE (fn) == FUNCTION_DECL
3076 : 55 : && move_fn_p (fn) && !trivial_fn_p (fn)
3077 : 21 : && vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo))
3078 : 8232077 : && warning_enabled_at (DECL_SOURCE_LOCATION (fn),
3079 : 18 : OPT_Wvirtual_move_assign))
3080 : 12 : warning (OPT_Wvirtual_move_assign,
3081 : : "defaulted move assignment for %qT calls a non-trivial "
3082 : : "move assignment operator for virtual base %qT",
3083 : 12 : ctype, BINFO_TYPE (base_binfo));
3084 : :
3085 : 8232059 : if (check_vdtor && type_has_virtual_destructor (BINFO_TYPE (base_binfo)))
3086 : : {
3087 : : /* Unlike for base ctor/op=/dtor, for operator delete it's fine
3088 : : to have a null fn (no class-specific op delete). */
3089 : 1392633 : fn = locate_fn_flags (ctype, ovl_op_identifier (false, DELETE_EXPR),
3090 : : ptr_type_node, flags, tf_none);
3091 : 1392633 : if (fn && fn == error_mark_node)
3092 : : {
3093 : 21 : if (complain & tf_error)
3094 : 5 : locate_fn_flags (ctype, ovl_op_identifier (false, DELETE_EXPR),
3095 : : ptr_type_node, flags, complain);
3096 : 21 : if (deleted_p)
3097 : 16 : *deleted_p = true;
3098 : : }
3099 : : check_vdtor = false;
3100 : : }
3101 : : }
3102 : :
3103 : 25924560 : vec<tree, va_gc> *vbases = CLASSTYPE_VBASECLASSES (ctype);
3104 : 25924560 : if (SFK_ASSIGN_P (sfk))
3105 : : /* Already examined vbases above. */;
3106 : 22198198 : else if (vec_safe_is_empty (vbases))
3107 : : /* No virtual bases to worry about. */;
3108 : 198102 : else if (ABSTRACT_CLASS_TYPE_P (ctype) && cxx_dialect >= cxx14
3109 : : /* DR 1658 specifies that vbases of abstract classes are
3110 : : ignored for both ctors and dtors. Except DR 2336
3111 : : overrides that skipping when determing the eh-spec of a
3112 : : virtual destructor. */
3113 : 198481 : && sfk != sfk_virtual_destructor)
3114 : : /* Vbase cdtors are not relevant. */;
3115 : : else
3116 : : {
3117 : 197761 : if (constexpr_p && cxx_dialect < cxx26)
3118 : 8456 : *constexpr_p = false;
3119 : :
3120 : 468155 : FOR_EACH_VEC_ELT (*vbases, i, base_binfo)
3121 : 270394 : synthesized_method_base_walk (binfo, base_binfo, sfk, fnname, quals,
3122 : : inheriting_ctor, inherited_parms,
3123 : : flags, diag,
3124 : : spec_p, trivial_p, deleted_p, constexpr_p);
3125 : : }
3126 : :
3127 : : /* Now handle the non-static data members. */
3128 : 25924560 : walk_field_subobs (TYPE_FIELDS (ctype), sfk, fnname, quals,
3129 : : spec_p, trivial_p, deleted_p, constexpr_p,
3130 : : diag, flags, complain, /*dtor_from_ctor*/false);
3131 : 25924560 : if (SFK_CTOR_P (sfk))
3132 : 13649733 : walk_field_subobs (TYPE_FIELDS (ctype), sfk_destructor,
3133 : : complete_dtor_identifier, TYPE_UNQUALIFIED,
3134 : : NULL, NULL, deleted_p, NULL,
3135 : : false, flags, complain, /*dtor_from_ctor*/true);
3136 : :
3137 : 25924560 : pop_scope (scope);
3138 : :
3139 : 25924560 : pop_deferring_access_checks ();
3140 : 25924560 : --cp_unevaluated_operand;
3141 : 25924560 : --c_inhibit_evaluation_warnings;
3142 : 25924560 : maybe_pop_from_top_level (push_to_top);
3143 : : }
3144 : :
3145 : : /* DECL is a defaulted function whose exception specification is now
3146 : : needed. Return what it should be. */
3147 : :
3148 : : tree
3149 : 3695436 : get_defaulted_eh_spec (tree decl, tsubst_flags_t complain)
3150 : : {
3151 : : /* For DECL_MAYBE_DELETED this should already have been handled by
3152 : : synthesize_method. */
3153 : 3695436 : gcc_assert (!DECL_MAYBE_DELETED (decl));
3154 : :
3155 : 3695436 : if (DECL_CLONED_FUNCTION_P (decl))
3156 : 0 : decl = DECL_CLONED_FUNCTION (decl);
3157 : 3695436 : special_function_kind sfk = special_function_p (decl);
3158 : 3695436 : tree ctype = DECL_CONTEXT (decl);
3159 : 3695436 : tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
3160 : 3695436 : tree parm_type = TREE_VALUE (parms);
3161 : 3695436 : bool const_p = CP_TYPE_CONST_P (non_reference (parm_type));
3162 : 3695436 : tree spec = empty_except_spec;
3163 : 3695436 : bool diag = !DECL_DELETED_FN (decl) && (complain & tf_error);
3164 : 7390872 : tree inh = DECL_INHERITED_CTOR (decl);
3165 : 3695436 : if (SFK_DTOR_P (sfk) && DECL_VIRTUAL_P (decl))
3166 : : /* We have to examine virtual bases even if abstract. */
3167 : : sfk = sfk_virtual_destructor;
3168 : 3695436 : bool pushed = false;
3169 : 3695436 : if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
3170 : 2406319 : pushed = push_tinst_level (decl);
3171 : 3695436 : synthesized_method_walk (ctype, sfk, const_p, &spec, NULL, NULL,
3172 : : NULL, diag, &inh, parms);
3173 : 3695436 : if (pushed)
3174 : 2406032 : pop_tinst_level ();
3175 : 3695436 : return spec;
3176 : : }
3177 : :
3178 : : /* DECL is a deleted function. If it's implicitly deleted, explain why and
3179 : : return true; else return false. */
3180 : :
3181 : : bool
3182 : 2418 : maybe_explain_implicit_delete (tree decl)
3183 : : {
3184 : : /* If decl is a clone, get the primary variant. */
3185 : 2418 : decl = DECL_ORIGIN (decl);
3186 : 2418 : gcc_assert (DECL_DELETED_FN (decl));
3187 : 2418 : if (DECL_DEFAULTED_FN (decl))
3188 : : {
3189 : : /* Not marked GTY; it doesn't need to be GC'd or written to PCH. */
3190 : 1756 : static hash_set<tree> *explained;
3191 : :
3192 : 1756 : special_function_kind sfk;
3193 : 1756 : location_t loc;
3194 : 1756 : bool informed;
3195 : 1756 : tree ctype;
3196 : :
3197 : 1756 : if (!explained)
3198 : 239 : explained = new hash_set<tree>;
3199 : 1756 : if (explained->add (decl))
3200 : : return true;
3201 : :
3202 : 478 : sfk = special_function_p (decl);
3203 : 478 : ctype = DECL_CONTEXT (decl);
3204 : 478 : loc = input_location;
3205 : 478 : input_location = DECL_SOURCE_LOCATION (decl);
3206 : :
3207 : 478 : informed = false;
3208 : 925 : if (LAMBDA_TYPE_P (ctype))
3209 : : {
3210 : 36 : informed = true;
3211 : 36 : if (sfk == sfk_constructor)
3212 : 19 : inform (DECL_SOURCE_LOCATION (decl),
3213 : : "a lambda closure type has a deleted default constructor");
3214 : 17 : else if (sfk == sfk_copy_assignment)
3215 : 17 : inform (DECL_SOURCE_LOCATION (decl),
3216 : : "a lambda closure type has a deleted copy assignment operator");
3217 : : else
3218 : : informed = false;
3219 : : }
3220 : 442 : else if (DECL_ARTIFICIAL (decl)
3221 : 347 : && (sfk == sfk_copy_assignment || sfk == sfk_copy_constructor)
3222 : 560 : && classtype_has_move_assign_or_move_ctor_p (ctype, true))
3223 : : {
3224 : 77 : inform (DECL_SOURCE_LOCATION (decl),
3225 : : "%q#D is implicitly declared as deleted because %qT "
3226 : : "declares a move constructor or move assignment operator",
3227 : : decl, ctype);
3228 : 77 : informed = true;
3229 : : }
3230 : 365 : else if (sfk == sfk_inheriting_constructor)
3231 : : {
3232 : 18 : tree binfo = inherited_ctor_binfo (decl);
3233 : 18 : if (TREE_CODE (binfo) != TREE_BINFO)
3234 : : {
3235 : 3 : inform (DECL_SOURCE_LOCATION (decl),
3236 : : "%q#D inherits from multiple base subobjects",
3237 : : decl);
3238 : 3 : informed = true;
3239 : : }
3240 : : }
3241 : 478 : if (!informed && sfk == sfk_comparison)
3242 : : {
3243 : 75 : inform (DECL_SOURCE_LOCATION (decl),
3244 : : "%q#D is implicitly deleted because the default "
3245 : : "definition would be ill-formed:", decl);
3246 : 75 : build_comparison_op (decl, false, tf_warning_or_error);
3247 : : }
3248 : 403 : else if (!informed)
3249 : : {
3250 : 287 : tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
3251 : 287 : bool const_p = false;
3252 : 287 : if (parms)
3253 : : {
3254 : 284 : tree parm_type = TREE_VALUE (parms);
3255 : 284 : const_p = CP_TYPE_CONST_P (non_reference (parm_type));
3256 : : }
3257 : 287 : tree raises = NULL_TREE;
3258 : 287 : bool deleted_p = false;
3259 : 287 : tree scope = push_scope (ctype);
3260 : 574 : tree inh = DECL_INHERITED_CTOR (decl);
3261 : :
3262 : 287 : synthesized_method_walk (ctype, sfk, const_p,
3263 : : &raises, NULL, &deleted_p, NULL, false,
3264 : : &inh, parms);
3265 : 287 : if (deleted_p)
3266 : : {
3267 : 287 : inform (DECL_SOURCE_LOCATION (decl),
3268 : : "%q#D is implicitly deleted because the default "
3269 : : "definition would be ill-formed:", decl);
3270 : 287 : synthesized_method_walk (ctype, sfk, const_p,
3271 : : NULL, NULL, &deleted_p, NULL, true,
3272 : : &inh, parms);
3273 : : }
3274 : 0 : else if (!comp_except_specs
3275 : 0 : (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)),
3276 : : raises, ce_normal))
3277 : 0 : inform (DECL_SOURCE_LOCATION (decl), "%q#F is implicitly "
3278 : : "deleted because its exception-specification does not "
3279 : : "match the implicit exception-specification %qX",
3280 : : decl, raises);
3281 : 0 : else if (flag_checking)
3282 : 0 : gcc_unreachable ();
3283 : :
3284 : 287 : pop_scope (scope);
3285 : : }
3286 : :
3287 : 478 : input_location = loc;
3288 : 478 : return true;
3289 : : }
3290 : : return false;
3291 : : }
3292 : :
3293 : : /* DECL is a defaulted function which was declared constexpr. Explain why
3294 : : it can't be constexpr. */
3295 : :
3296 : : void
3297 : 26 : explain_implicit_non_constexpr (tree decl)
3298 : : {
3299 : 26 : tree parms = FUNCTION_FIRST_USER_PARMTYPE (decl);
3300 : 26 : bool const_p = CP_TYPE_CONST_P (non_reference (TREE_VALUE (parms)));
3301 : 52 : tree inh = DECL_INHERITED_CTOR (decl);
3302 : 26 : bool dummy;
3303 : 26 : special_function_kind sfk = special_function_p (decl);
3304 : 26 : if (sfk == sfk_comparison)
3305 : : {
3306 : 9 : DECL_DECLARED_CONSTEXPR_P (decl) = true;
3307 : 9 : build_comparison_op (decl, false, tf_warning_or_error);
3308 : 9 : DECL_DECLARED_CONSTEXPR_P (decl) = false;
3309 : : }
3310 : : else
3311 : 17 : synthesized_method_walk (DECL_CLASS_CONTEXT (decl),
3312 : : sfk, const_p,
3313 : : NULL, NULL, NULL, &dummy, true,
3314 : : &inh, parms);
3315 : 26 : }
3316 : :
3317 : : /* DECL is an instantiation of an inheriting constructor template. Deduce
3318 : : the correct exception-specification and deletedness for this particular
3319 : : specialization. Return true if the deduction succeeds; false otherwise. */
3320 : :
3321 : : bool
3322 : 57287 : deduce_inheriting_ctor (tree decl)
3323 : : {
3324 : 57287 : decl = DECL_ORIGIN (decl);
3325 : 114574 : gcc_assert (DECL_INHERITED_CTOR (decl));
3326 : 57287 : tree spec;
3327 : 57287 : bool trivial, constexpr_, deleted;
3328 : 57287 : tree inh = DECL_INHERITED_CTOR (decl);
3329 : 57287 : synthesized_method_walk (DECL_CONTEXT (decl), sfk_inheriting_constructor,
3330 : : false, &spec, &trivial, &deleted, &constexpr_,
3331 : : /*diag*/false,
3332 : : &inh,
3333 : 57287 : FUNCTION_FIRST_USER_PARMTYPE (decl));
3334 : 57287 : if (spec == error_mark_node)
3335 : : return false;
3336 : 57285 : if (TREE_CODE (inherited_ctor_binfo (decl)) != TREE_BINFO)
3337 : : /* Inherited the same constructor from different base subobjects. */
3338 : 3 : deleted = true;
3339 : 57285 : DECL_DELETED_FN (decl) = deleted;
3340 : 57285 : TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl), spec);
3341 : 57285 : SET_DECL_INHERITED_CTOR (decl, inh);
3342 : :
3343 : 57285 : tree clone;
3344 : 171855 : FOR_EACH_CLONE (clone, decl)
3345 : : {
3346 : 114570 : DECL_DELETED_FN (clone) = deleted;
3347 : 114570 : TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
3348 : 114570 : SET_DECL_INHERITED_CTOR (clone, inh);
3349 : : }
3350 : :
3351 : : return true;
3352 : : }
3353 : :
3354 : : /* Implicitly declare the special function indicated by KIND, as a
3355 : : member of TYPE. For copy constructors and assignment operators,
3356 : : CONST_P indicates whether these functions should take a const
3357 : : reference argument or a non-const reference.
3358 : : Returns the FUNCTION_DECL for the implicitly declared function. */
3359 : :
3360 : : tree
3361 : 22631079 : implicitly_declare_fn (special_function_kind kind, tree type,
3362 : : bool const_p, tree pattern_fn,
3363 : : tree inherited_parms)
3364 : : {
3365 : 22631079 : tree fn;
3366 : 22631079 : tree parameter_types = void_list_node;
3367 : 22631079 : tree return_type;
3368 : 22631079 : tree fn_type;
3369 : 22631079 : tree raises = empty_except_spec;
3370 : 22631079 : tree rhs_parm_type = NULL_TREE;
3371 : 22631079 : tree this_parm;
3372 : 22631079 : tree name;
3373 : 22631079 : HOST_WIDE_INT saved_processing_template_decl;
3374 : 22631079 : bool deleted_p = false;
3375 : 22631079 : bool constexpr_p = false;
3376 : 45262158 : tree inherited_ctor = (kind == sfk_inheriting_constructor
3377 : 22631079 : ? pattern_fn : NULL_TREE);
3378 : :
3379 : : /* Because we create declarations for implicitly declared functions
3380 : : lazily, we may be creating the declaration for a member of TYPE
3381 : : while in some completely different context. However, TYPE will
3382 : : never be a dependent class (because we never want to do lookups
3383 : : for implicitly defined functions in a dependent class). */
3384 : 22631079 : gcc_assert (!dependent_type_p (type));
3385 : :
3386 : : /* If the member-specification does not explicitly declare any member or
3387 : : friend named operator==, an == operator function is declared
3388 : : implicitly for each three-way comparison operator function defined as
3389 : : defaulted in the member-specification, with the same access and
3390 : : function-definition and in the same class scope as the respective
3391 : : three-way comparison operator function, except that the return type is
3392 : : replaced with bool and the declarator-id is replaced with
3393 : : operator==.
3394 : :
3395 : : [Note: Such an implicitly-declared == operator for a class X is
3396 : : defined as defaulted in the definition of X and has the same
3397 : : parameter-declaration-clause and trailing requires-clause as the
3398 : : respective three-way comparison operator. It is declared with friend,
3399 : : virtual, constexpr, or consteval if the three-way comparison operator
3400 : : function is so declared. If the three-way comparison operator function
3401 : : has no noexcept-specifier, the implicitly-declared == operator
3402 : : function has an implicit exception specification (14.5) that may
3403 : : differ from the implicit exception specification of the three-way
3404 : : comparison operator function. --end note] */
3405 : 22631079 : if (kind == sfk_comparison)
3406 : : {
3407 : 246 : fn = copy_operator_fn (pattern_fn, EQ_EXPR);
3408 : 246 : DECL_ARTIFICIAL (fn) = 1;
3409 : 246 : apply_deduced_return_type (fn, boolean_type_node);
3410 : 246 : return fn;
3411 : : }
3412 : :
3413 : : /* Furthermore, we must set PROCESSING_TEMPLATE_DECL to zero here
3414 : : because we only create clones for constructors and destructors
3415 : : when not in a template. */
3416 : 22630833 : saved_processing_template_decl = processing_template_decl;
3417 : 22630833 : processing_template_decl = 0;
3418 : :
3419 : 22630833 : type = TYPE_MAIN_VARIANT (type);
3420 : :
3421 : 22630833 : if (targetm.cxx.cdtor_returns_this ())
3422 : : {
3423 : 0 : if (kind == sfk_destructor)
3424 : : /* See comment in check_special_function_return_type. */
3425 : 0 : return_type = build_pointer_type (void_type_node);
3426 : : else
3427 : 0 : return_type = build_pointer_type (type);
3428 : : }
3429 : : else
3430 : 22630833 : return_type = void_type_node;
3431 : :
3432 : 22630833 : int this_quals = TYPE_UNQUALIFIED;
3433 : 22630833 : switch (kind)
3434 : : {
3435 : 5877270 : case sfk_destructor:
3436 : : /* Destructor. */
3437 : 5877270 : name = dtor_identifier;
3438 : 5877270 : break;
3439 : :
3440 : 3835251 : case sfk_constructor:
3441 : : /* Default constructor. */
3442 : 3835251 : name = ctor_identifier;
3443 : 3835251 : break;
3444 : :
3445 : 12918312 : case sfk_copy_constructor:
3446 : 12918312 : case sfk_copy_assignment:
3447 : 12918312 : case sfk_move_constructor:
3448 : 12918312 : case sfk_move_assignment:
3449 : 12918312 : case sfk_inheriting_constructor:
3450 : 12918312 : {
3451 : 12918312 : if (kind == sfk_copy_assignment
3452 : 12918312 : || kind == sfk_move_assignment)
3453 : : {
3454 : 3525657 : return_type = build_reference_type (type);
3455 : 3525657 : name = assign_op_identifier;
3456 : : }
3457 : : else
3458 : 9392655 : name = ctor_identifier;
3459 : :
3460 : 12918312 : if (kind == sfk_inheriting_constructor)
3461 : : parameter_types = inherited_parms;
3462 : : else
3463 : : {
3464 : 12619194 : if (const_p)
3465 : 7522046 : rhs_parm_type = cp_build_qualified_type (type, TYPE_QUAL_CONST);
3466 : : else
3467 : : rhs_parm_type = type;
3468 : 12619194 : bool move_p = (kind == sfk_move_assignment
3469 : 12619194 : || kind == sfk_move_constructor);
3470 : 12619194 : rhs_parm_type = cp_build_reference_type (rhs_parm_type, move_p);
3471 : :
3472 : 12619194 : parameter_types = tree_cons (NULL_TREE, rhs_parm_type, parameter_types);
3473 : : }
3474 : : break;
3475 : : }
3476 : :
3477 : 0 : default:
3478 : 0 : gcc_unreachable ();
3479 : : }
3480 : :
3481 : 22630833 : bool trivial_p = false;
3482 : :
3483 : 22630833 : if (inherited_ctor)
3484 : : {
3485 : : /* For an inheriting constructor, just copy these flags from the
3486 : : inherited constructor until deduce_inheriting_ctor. */
3487 : 299118 : raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (inherited_ctor));
3488 : 299118 : deleted_p = DECL_DELETED_FN (inherited_ctor);
3489 : 299118 : constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
3490 : : }
3491 : 22331715 : else if (cxx_dialect >= cxx11)
3492 : : {
3493 : 22312018 : raises = noexcept_deferred_spec;
3494 : 22312018 : synthesized_method_walk (type, kind, const_p, NULL, &trivial_p,
3495 : : &deleted_p, &constexpr_p, false,
3496 : : &inherited_ctor, inherited_parms);
3497 : : }
3498 : : else
3499 : 19697 : synthesized_method_walk (type, kind, const_p, &raises, &trivial_p,
3500 : : &deleted_p, &constexpr_p, false,
3501 : : &inherited_ctor, inherited_parms);
3502 : : /* Don't bother marking a deleted constructor as constexpr. */
3503 : 22630833 : if (deleted_p)
3504 : 957711 : constexpr_p = false;
3505 : : /* A trivial copy/move constructor is also a constexpr constructor,
3506 : : unless the class has virtual bases (7.1.5p4). */
3507 : 21673122 : else if (trivial_p
3508 : 17684320 : && cxx_dialect >= cxx11
3509 : 17672258 : && (kind == sfk_copy_constructor
3510 : 17672258 : || kind == sfk_move_constructor)
3511 : 28856078 : && !CLASSTYPE_VBASECLASSES (type))
3512 : 7182956 : gcc_assert (constexpr_p);
3513 : :
3514 : 22630833 : if (!trivial_p && type_has_trivial_fn (type, kind))
3515 : 242655 : type_set_nontrivial_flag (type, kind);
3516 : :
3517 : : /* Create the function. */
3518 : 22630833 : tree this_type = cp_build_qualified_type (type, this_quals);
3519 : 22630833 : fn_type = build_method_type_directly (this_type, return_type,
3520 : : parameter_types);
3521 : :
3522 : 22630833 : if (raises)
3523 : : {
3524 : 22511935 : if (raises != error_mark_node)
3525 : 22511932 : fn_type = build_exception_variant (fn_type, raises);
3526 : : else
3527 : : {
3528 : : /* Can happen, e.g., in C++98 mode for an ill-formed non-static data
3529 : : member initializer (c++/89914). Also, in C++98, we might have
3530 : : failed to deduce RAISES, so try again but complain this time. */
3531 : 3 : if (cxx_dialect < cxx11)
3532 : 3 : synthesized_method_walk (type, kind, const_p, &raises, nullptr,
3533 : : nullptr, nullptr, /*diag=*/true,
3534 : : &inherited_ctor, inherited_parms);
3535 : : /* We should have seen an error at this point. */
3536 : 3 : gcc_assert (seen_error ());
3537 : : }
3538 : : }
3539 : 22630833 : fn = build_lang_decl (FUNCTION_DECL, name, fn_type);
3540 : 22630833 : if (kind != sfk_inheriting_constructor)
3541 : 22331715 : DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (TYPE_NAME (type));
3542 : :
3543 : 22630833 : if (IDENTIFIER_OVL_OP_P (name))
3544 : : {
3545 : 3525657 : const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (name);
3546 : 3525657 : DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) = op->ovl_op_code;
3547 : 3525657 : }
3548 : 19105176 : else if (IDENTIFIER_CTOR_P (name))
3549 : 13227906 : DECL_CXX_CONSTRUCTOR_P (fn) = true;
3550 : 5877270 : else if (IDENTIFIER_DTOR_P (name))
3551 : 5877270 : DECL_CXX_DESTRUCTOR_P (fn) = true;
3552 : : else
3553 : 0 : gcc_unreachable ();
3554 : :
3555 : 22630833 : SET_DECL_ALIGN (fn, MINIMUM_METHOD_BOUNDARY);
3556 : :
3557 : : /* Create the explicit arguments. */
3558 : 22630833 : if (rhs_parm_type)
3559 : : {
3560 : : /* Note that this parameter is *not* marked DECL_ARTIFICIAL; we
3561 : : want its type to be included in the mangled function
3562 : : name. */
3563 : 12619194 : tree decl = cp_build_parm_decl (fn, NULL_TREE, rhs_parm_type);
3564 : 12619194 : TREE_READONLY (decl) = 1;
3565 : 12619194 : retrofit_lang_decl (decl);
3566 : 12619194 : DECL_PARM_INDEX (decl) = DECL_PARM_LEVEL (decl) = 1;
3567 : 12619194 : DECL_ARGUMENTS (fn) = decl;
3568 : : }
3569 : 10011639 : else if (kind == sfk_inheriting_constructor)
3570 : : {
3571 : 299118 : tree *p = &DECL_ARGUMENTS (fn);
3572 : 299118 : int index = 1;
3573 : 607189 : for (tree parm = inherited_parms; parm && parm != void_list_node;
3574 : 308071 : parm = TREE_CHAIN (parm))
3575 : : {
3576 : 308071 : *p = cp_build_parm_decl (fn, NULL_TREE, TREE_VALUE (parm));
3577 : 308071 : retrofit_lang_decl (*p);
3578 : 308071 : DECL_PARM_LEVEL (*p) = 1;
3579 : 308071 : DECL_PARM_INDEX (*p) = index++;
3580 : 308071 : p = &DECL_CHAIN (*p);
3581 : : }
3582 : 299118 : SET_DECL_INHERITED_CTOR (fn, inherited_ctor);
3583 : 299118 : DECL_NONCONVERTING_P (fn) = DECL_NONCONVERTING_P (inherited_ctor);
3584 : : /* A constructor so declared has the same access as the corresponding
3585 : : constructor in X. */
3586 : 299118 : TREE_PRIVATE (fn) = TREE_PRIVATE (inherited_ctor);
3587 : 299118 : TREE_PROTECTED (fn) = TREE_PROTECTED (inherited_ctor);
3588 : : /* Copy constexpr from the inherited constructor even if the
3589 : : inheriting constructor doesn't satisfy the requirements. */
3590 : 299118 : constexpr_p = DECL_DECLARED_CONSTEXPR_P (inherited_ctor);
3591 : 299118 : tree inherited_ctor_fn = STRIP_TEMPLATE (inherited_ctor);
3592 : : /* Also copy any attributes. */
3593 : 299118 : DECL_ATTRIBUTES (fn) = clone_attrs (DECL_ATTRIBUTES (inherited_ctor_fn));
3594 : 299118 : DECL_DISREGARD_INLINE_LIMITS (fn)
3595 : 299118 : = DECL_DISREGARD_INLINE_LIMITS (inherited_ctor_fn);
3596 : : }
3597 : :
3598 : : /* Add the "this" parameter. */
3599 : 22630833 : this_parm = build_this_parm (fn, fn_type, this_quals);
3600 : 22630833 : DECL_CHAIN (this_parm) = DECL_ARGUMENTS (fn);
3601 : 22630833 : DECL_ARGUMENTS (fn) = this_parm;
3602 : :
3603 : 39384396 : grokclassfn (type, fn, kind == sfk_destructor ? DTOR_FLAG : NO_SPECIAL);
3604 : :
3605 : 22630833 : DECL_IN_AGGR_P (fn) = 1;
3606 : 22630833 : DECL_ARTIFICIAL (fn) = 1;
3607 : 22630833 : DECL_DEFAULTED_FN (fn) = 1;
3608 : 22630833 : if (cxx_dialect >= cxx11)
3609 : : {
3610 : 22611132 : DECL_DELETED_FN (fn) = deleted_p;
3611 : 22611132 : DECL_DECLARED_CONSTEXPR_P (fn) = constexpr_p;
3612 : : }
3613 : 22630833 : DECL_EXTERNAL (fn) = true;
3614 : 22630833 : DECL_NOT_REALLY_EXTERN (fn) = 1;
3615 : 22630833 : DECL_DECLARED_INLINE_P (fn) = 1;
3616 : 22630833 : set_linkage_according_to_type (type, fn);
3617 : 22630833 : if (TREE_PUBLIC (fn))
3618 : 22571319 : DECL_COMDAT (fn) = 1;
3619 : 22630833 : rest_of_decl_compilation (fn, namespace_bindings_p (), at_eof);
3620 : 22630833 : gcc_assert (!TREE_USED (fn));
3621 : :
3622 : : /* Propagate constraints from the inherited constructor. */
3623 : 22630833 : if (flag_concepts && inherited_ctor)
3624 : 134059 : if (tree orig_ci = get_constraints (inherited_ctor))
3625 : : {
3626 : 2416 : tree new_ci = copy_node (orig_ci);
3627 : 2416 : set_constraints (fn, new_ci);
3628 : : }
3629 : :
3630 : : /* Restore PROCESSING_TEMPLATE_DECL. */
3631 : 22630833 : processing_template_decl = saved_processing_template_decl;
3632 : :
3633 : 22630833 : if (inherited_ctor && TREE_CODE (inherited_ctor) == TEMPLATE_DECL)
3634 : 49889 : fn = add_inherited_template_parms (fn, inherited_ctor);
3635 : :
3636 : : /* Warn about calling a non-trivial move assignment in a virtual base. */
3637 : 1162456 : if (kind == sfk_move_assignment && !deleted_p && !trivial_p
3638 : 22920375 : && CLASSTYPE_VBASECLASSES (type))
3639 : : {
3640 : 88 : location_t loc = input_location;
3641 : 88 : input_location = DECL_SOURCE_LOCATION (fn);
3642 : 88 : synthesized_method_walk (type, kind, const_p,
3643 : : NULL, NULL, NULL, NULL, true,
3644 : : NULL, NULL_TREE);
3645 : 88 : input_location = loc;
3646 : : }
3647 : :
3648 : : return fn;
3649 : : }
3650 : :
3651 : : /* Mark an explicitly defaulted function FN as =deleted and warn.
3652 : : IMPLICIT_FN is the corresponding special member function that
3653 : : would have been implicitly declared. */
3654 : :
3655 : : void
3656 : 117 : maybe_delete_defaulted_fn (tree fn, tree implicit_fn)
3657 : : {
3658 : 117 : if (DECL_ARTIFICIAL (fn) || !DECL_DEFAULTED_IN_CLASS_P (fn))
3659 : 5 : return;
3660 : :
3661 : 117 : DECL_DELETED_FN (fn) = true;
3662 : :
3663 : 117 : auto_diagnostic_group d;
3664 : 117 : const special_function_kind kind = special_function_p (fn);
3665 : 117 : tree parmtype
3666 : 117 : = TREE_VALUE (DECL_XOBJ_MEMBER_FUNCTION_P (fn)
3667 : : ? TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)))
3668 : : : FUNCTION_FIRST_USER_PARMTYPE (fn));
3669 : 117 : const bool illformed_p
3670 : : /* [dcl.fct.def.default] "if F1 is an assignment operator"... */
3671 : 117 : = (SFK_ASSIGN_P (kind)
3672 : : /* "and the return type of F1 differs from the return type of F2" */
3673 : 117 : && (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
3674 : : TREE_TYPE (TREE_TYPE (implicit_fn)))
3675 : : /* "or F1's non-object parameter type is not a reference,
3676 : : the program is ill-formed" */
3677 : 43 : || !TYPE_REF_P (parmtype)));
3678 : : /* Decide if we want to emit a pedwarn, error, or a warning. */
3679 : 95 : enum diagnostics::kind diag_kind;
3680 : 95 : int opt;
3681 : 95 : if (illformed_p)
3682 : : {
3683 : : diag_kind = diagnostics::kind::error;
3684 : : opt = 0;
3685 : : }
3686 : : else
3687 : : {
3688 : 64 : diag_kind = (cxx_dialect >= cxx20
3689 : 95 : ? diagnostics::kind::warning
3690 : : : diagnostics::kind::pedwarn);
3691 : : opt = OPT_Wdefaulted_function_deleted;
3692 : : }
3693 : :
3694 : : /* Don't warn for template instantiations. */
3695 : 117 : if (DECL_TEMPLATE_INSTANTIATION (fn)
3696 : 117 : && diag_kind == diagnostics::kind::warning)
3697 : 5 : return;
3698 : :
3699 : 112 : const char *wmsg;
3700 : 112 : switch (kind)
3701 : : {
3702 : : case sfk_copy_constructor:
3703 : : wmsg = G_("explicitly defaulted copy constructor is implicitly deleted "
3704 : : "because its declared type does not match the type of an "
3705 : : "implicit copy constructor");
3706 : : break;
3707 : 23 : case sfk_move_constructor:
3708 : 23 : wmsg = G_("explicitly defaulted move constructor is implicitly deleted "
3709 : : "because its declared type does not match the type of an "
3710 : : "implicit move constructor");
3711 : 23 : break;
3712 : 32 : case sfk_copy_assignment:
3713 : 32 : wmsg = G_("explicitly defaulted copy assignment operator is implicitly "
3714 : : "deleted because its declared type does not match the type "
3715 : : "of an implicit copy assignment operator");
3716 : 32 : break;
3717 : 24 : case sfk_move_assignment:
3718 : 24 : wmsg = G_("explicitly defaulted move assignment operator is implicitly "
3719 : : "deleted because its declared type does not match the type "
3720 : : "of an implicit move assignment operator");
3721 : 24 : break;
3722 : 0 : default:
3723 : 0 : gcc_unreachable ();
3724 : : }
3725 : 112 : if (emit_diagnostic (diag_kind, DECL_SOURCE_LOCATION (fn), opt, wmsg))
3726 : 88 : inform (DECL_SOURCE_LOCATION (fn),
3727 : : "expected signature: %qD", implicit_fn);
3728 : 117 : }
3729 : :
3730 : : /* Gives any errors about defaulted functions which need to be deferred
3731 : : until the containing class is complete. IMP_CONST is false or true
3732 : : if we are called from check_bases_and_members and signals whether
3733 : : the implicit function has a non-object parameter of type const C&. */
3734 : :
3735 : : void
3736 : 5492522 : defaulted_late_check (tree fn, tristate imp_const/*=tristate::unknown()*/)
3737 : : {
3738 : : /* Complain about invalid signature for defaulted fn. */
3739 : 5492522 : tree ctx = DECL_CONTEXT (fn);
3740 : 5492522 : special_function_kind kind = special_function_p (fn);
3741 : :
3742 : 5492522 : if (kind == sfk_comparison)
3743 : : {
3744 : : /* If the function was declared constexpr, check that the definition
3745 : : qualifies. Otherwise we can define the function lazily. */
3746 : 11821 : if (DECL_DECLARED_CONSTEXPR_P (fn) && !DECL_INITIAL (fn))
3747 : : {
3748 : : /* Prevent GC. */
3749 : 9142 : function_depth++;
3750 : 9142 : synthesize_method (fn);
3751 : 9142 : function_depth--;
3752 : : }
3753 : 16534 : return;
3754 : : }
3755 : :
3756 : 5480701 : bool fn_const_p = (copy_fn_p (fn) == 2);
3757 : : /* "if F2 has a non-object parameter of type const C&, the corresponding
3758 : : non-object parameter of F1 may be of type C&." But not the other way
3759 : : around. */
3760 : 5480701 : if (fn_const_p && imp_const.is_false ())
3761 : : fn_const_p = false;
3762 : 5480701 : tree implicit_fn = implicitly_declare_fn (kind, ctx, fn_const_p,
3763 : : /*pattern_fn=*/NULL_TREE,
3764 : : /*inherited_parms=*/NULL_TREE);
3765 : 5480701 : tree eh_spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (implicit_fn));
3766 : :
3767 : : /* Includes special handling for a default xobj operator. */
3768 : 10961388 : auto compare_fn_params = [](tree fn, tree implicit_fn){
3769 : 5480687 : tree fn_parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
3770 : 5480687 : tree implicit_fn_parms = TYPE_ARG_TYPES (TREE_TYPE (implicit_fn));
3771 : :
3772 : 5480687 : if (DECL_XOBJ_MEMBER_FUNCTION_P (fn))
3773 : : {
3774 : 6 : tree fn_obj_ref_type = TREE_VALUE (fn_parms);
3775 : : /* We can't default xobj operators with an xobj parameter that is not
3776 : : an lvalue reference, even if it would correspond. */
3777 : 6 : if (!TYPE_REF_P (fn_obj_ref_type)
3778 : 6 : || TYPE_REF_IS_RVALUE (fn_obj_ref_type)
3779 : 12 : || !object_parms_correspond (fn, implicit_fn,
3780 : 6 : DECL_CONTEXT (implicit_fn)))
3781 : 0 : return false;
3782 : : /* We just compared the object parameters, skip over them before
3783 : : passing to compparms. */
3784 : 6 : fn_parms = TREE_CHAIN (fn_parms);
3785 : 6 : implicit_fn_parms = TREE_CHAIN (implicit_fn_parms);
3786 : : }
3787 : 5480687 : return compparms (fn_parms, implicit_fn_parms);
3788 : : };
3789 : :
3790 : 5480701 : if (!same_type_p (TREE_TYPE (TREE_TYPE (fn)),
3791 : : TREE_TYPE (TREE_TYPE (implicit_fn)))
3792 : 5480701 : || !compare_fn_params (fn, implicit_fn))
3793 : 117 : maybe_delete_defaulted_fn (fn, implicit_fn);
3794 : :
3795 : 5480701 : if (DECL_DELETED_FN (implicit_fn))
3796 : : {
3797 : 4713 : DECL_DELETED_FN (fn) = 1;
3798 : 4713 : return;
3799 : : }
3800 : :
3801 : : /* If a function is explicitly defaulted on its first declaration without an
3802 : : exception-specification, it is implicitly considered to have the same
3803 : : exception-specification as if it had been implicitly declared. */
3804 : 5475988 : if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (fn))
3805 : 5475988 : && DECL_DEFAULTED_IN_CLASS_P (fn))
3806 : 3677550 : TREE_TYPE (fn) = build_exception_variant (TREE_TYPE (fn), eh_spec);
3807 : :
3808 : 10951442 : if (DECL_DEFAULTED_IN_CLASS_P (fn)
3809 : 10951442 : && DECL_DECLARED_CONSTEXPR_P (implicit_fn))
3810 : : {
3811 : : /* Hmm...should we do this for out-of-class too? Should it be OK to
3812 : : add constexpr later like inline, rather than requiring
3813 : : declarations to match? */
3814 : 3033868 : DECL_DECLARED_CONSTEXPR_P (fn) = true;
3815 : 3033868 : if (kind == sfk_constructor)
3816 : 925203 : TYPE_HAS_CONSTEXPR_CTOR (ctx) = true;
3817 : : }
3818 : :
3819 : 5475988 : if (!DECL_DECLARED_CONSTEXPR_P (implicit_fn)
3820 : 5475988 : && DECL_DECLARED_CONSTEXPR_P (fn))
3821 : : {
3822 : 93874 : if (!CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
3823 : : {
3824 : 16 : auto_diagnostic_group d;
3825 : 16 : error ("explicitly defaulted function %q+D cannot be declared "
3826 : : "%qs because the implicit declaration is not %qs:", fn,
3827 : 32 : DECL_IMMEDIATE_FUNCTION_P (fn) ? "consteval" : "constexpr",
3828 : : "constexpr");
3829 : 16 : explain_implicit_non_constexpr (fn);
3830 : 16 : }
3831 : 93874 : DECL_DECLARED_CONSTEXPR_P (fn) = false;
3832 : : }
3833 : : }
3834 : :
3835 : : /* Returns true iff FN can be explicitly defaulted, and gives any
3836 : : errors if defaulting FN is ill-formed. */
3837 : :
3838 : : bool
3839 : 5775528 : defaultable_fn_check (tree fn)
3840 : : {
3841 : 5775528 : special_function_kind kind = sfk_none;
3842 : :
3843 : 5775528 : if (template_parm_scope_p ())
3844 : : {
3845 : 3 : error ("a template cannot be defaulted");
3846 : 3 : return false;
3847 : : }
3848 : :
3849 : 11551050 : if (DECL_CONSTRUCTOR_P (fn))
3850 : : {
3851 : 3517393 : if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
3852 : : kind = sfk_constructor;
3853 : 1944087 : else if (copy_fn_p (fn) > 0
3854 : 1944087 : && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
3855 : 1061836 : == void_list_node))
3856 : : kind = sfk_copy_constructor;
3857 : 882254 : else if (move_fn_p (fn))
3858 : : kind = sfk_move_constructor;
3859 : : }
3860 : 2258132 : else if (DECL_DESTRUCTOR_P (fn))
3861 : : kind = sfk_destructor;
3862 : 1581689 : else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
3863 : 1581689 : && DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR))
3864 : : {
3865 : 1521809 : if (copy_fn_p (fn))
3866 : : kind = sfk_copy_assignment;
3867 : 595445 : else if (move_fn_p (fn))
3868 : : kind = sfk_move_assignment;
3869 : : }
3870 : 59880 : else if (DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) >= OVL_OP_EQ_EXPR
3871 : 59880 : && DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) <= OVL_OP_SPACESHIP_EXPR)
3872 : : {
3873 : 59865 : kind = sfk_comparison;
3874 : 59865 : if (!early_check_defaulted_comparison (fn))
3875 : : return false;
3876 : : }
3877 : :
3878 : : /* FIXME: We need to check for xobj member functions here to give better
3879 : : diagnostics for weird cases where unrelated xobj parameters are given.
3880 : : We just want to do better than 'cannot be defaulted'. */
3881 : :
3882 : : if (kind == sfk_none)
3883 : : {
3884 : 21 : error ("%qD cannot be defaulted", fn);
3885 : 21 : return false;
3886 : : }
3887 : : else
3888 : : {
3889 : 5775425 : for (tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
3890 : 9352920 : t && t != void_list_node; t = TREE_CHAIN (t))
3891 : 3577498 : if (TREE_PURPOSE (t))
3892 : : {
3893 : 3 : error ("defaulted function %q+D with default argument", fn);
3894 : 3 : break;
3895 : : }
3896 : :
3897 : : /* Avoid do_warn_unused_parameter warnings. */
3898 : 9352923 : for (tree p = FUNCTION_FIRST_USER_PARM (fn); p; p = DECL_CHAIN (p))
3899 : 3577498 : if (DECL_NAME (p))
3900 : 206901 : suppress_warning (p, OPT_Wunused_parameter);
3901 : :
3902 : 5775425 : if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
3903 : : /* Defer checking. */;
3904 : 22786 : else if (!processing_template_decl)
3905 : 546 : defaulted_late_check (fn);
3906 : :
3907 : 5775425 : return true;
3908 : : }
3909 : : }
3910 : :
3911 : : /* Add an implicit declaration to TYPE for the kind of function
3912 : : indicated by SFK. Return the FUNCTION_DECL for the new implicit
3913 : : declaration. */
3914 : :
3915 : : tree
3916 : 16851014 : lazily_declare_fn (special_function_kind sfk, tree type)
3917 : : {
3918 : 16851014 : tree fn;
3919 : : /* Whether or not the argument has a const reference type. */
3920 : 16851014 : bool const_p = false;
3921 : :
3922 : 16851014 : type = TYPE_MAIN_VARIANT (type);
3923 : :
3924 : 16851014 : switch (sfk)
3925 : : {
3926 : 2333010 : case sfk_constructor:
3927 : 2333010 : CLASSTYPE_LAZY_DEFAULT_CTOR (type) = 0;
3928 : 2333010 : break;
3929 : 4107410 : case sfk_copy_constructor:
3930 : 4107410 : const_p = TYPE_HAS_CONST_COPY_CTOR (type);
3931 : 4107410 : CLASSTYPE_LAZY_COPY_CTOR (type) = 0;
3932 : 4107410 : break;
3933 : 3178912 : case sfk_move_constructor:
3934 : 3178912 : CLASSTYPE_LAZY_MOVE_CTOR (type) = 0;
3935 : 3178912 : break;
3936 : 1273886 : case sfk_copy_assignment:
3937 : 1273886 : const_p = TYPE_HAS_CONST_COPY_ASSIGN (type);
3938 : 1273886 : CLASSTYPE_LAZY_COPY_ASSIGN (type) = 0;
3939 : 1273886 : break;
3940 : 846811 : case sfk_move_assignment:
3941 : 846811 : CLASSTYPE_LAZY_MOVE_ASSIGN (type) = 0;
3942 : 846811 : break;
3943 : 5110985 : case sfk_destructor:
3944 : 5110985 : CLASSTYPE_LAZY_DESTRUCTOR (type) = 0;
3945 : 5110985 : break;
3946 : 0 : default:
3947 : 0 : gcc_unreachable ();
3948 : : }
3949 : :
3950 : : /* Declare the function. */
3951 : 16851014 : fn = implicitly_declare_fn (sfk, type, const_p, NULL, NULL);
3952 : :
3953 : : /* [class.copy]/8 If the class definition declares a move constructor or
3954 : : move assignment operator, the implicitly declared copy constructor is
3955 : : defined as deleted.... */
3956 : 16851014 : if ((sfk == sfk_copy_assignment || sfk == sfk_copy_constructor)
3957 : 5381296 : && cxx_dialect >= cxx11)
3958 : : {
3959 : 5369415 : if (classtype_has_move_assign_or_move_ctor_p (type, true))
3960 : 737310 : DECL_DELETED_FN (fn) = true;
3961 : 4632105 : else if (classtype_has_depr_implicit_copy (type))
3962 : : /* The implicit definition of a copy constructor as defaulted is
3963 : : deprecated if the class has a user-declared copy assignment operator
3964 : : or a user-declared destructor. The implicit definition of a copy
3965 : : assignment operator as defaulted is deprecated if the class has a
3966 : : user-declared copy constructor or a user-declared destructor (15.4,
3967 : : 15.8). */
3968 : 592652 : TREE_DEPRECATED (fn) = true;
3969 : : }
3970 : :
3971 : : /* Destructors and assignment operators may be virtual. */
3972 : 16851014 : if (sfk == sfk_destructor
3973 : 16851014 : || sfk == sfk_move_assignment
3974 : 10893218 : || sfk == sfk_copy_assignment)
3975 : 7231682 : check_for_override (fn, type);
3976 : :
3977 : : /* Add it to the class */
3978 : 16851014 : bool added = add_method (type, fn, false);
3979 : 16851014 : gcc_assert (added || errorcount);
3980 : :
3981 : : /* Add it to TYPE_FIELDS. */
3982 : 16851014 : if (sfk == sfk_destructor
3983 : 16851014 : && DECL_VIRTUAL_P (fn))
3984 : : /* The ABI requires that a virtual destructor go at the end of the
3985 : : vtable. */
3986 : 172346 : TYPE_FIELDS (type) = chainon (TYPE_FIELDS (type), fn);
3987 : : else
3988 : : {
3989 : 16678668 : DECL_CHAIN (fn) = TYPE_FIELDS (type);
3990 : 16678668 : TYPE_FIELDS (type) = fn;
3991 : : }
3992 : : /* Propagate TYPE_FIELDS. */
3993 : 16851014 : fixup_type_variants (type);
3994 : :
3995 : 16851014 : maybe_add_class_template_decl_list (type, fn, /*friend_p=*/0);
3996 : 16851014 : if (DECL_MAYBE_IN_CHARGE_CDTOR_P (fn))
3997 : : /* Create appropriate clones. */
3998 : 14730317 : clone_cdtor (fn, /*update_methods=*/true);
3999 : :
4000 : : /* Classes, structs or unions TYPE marked with hotness attributes propagate
4001 : : the attribute to all methods. This is typically done in
4002 : : check_bases_and_members, but we must also inject them here for deferred
4003 : : lazily-declared functions. */
4004 : 16851014 : maybe_propagate_warmth_attributes (fn, type);
4005 : :
4006 : 16851014 : return fn;
4007 : : }
4008 : :
4009 : : /* Given a FUNCTION_DECL FN and a chain LIST, skip as many elements of LIST
4010 : : as there are artificial parms in FN. */
4011 : :
4012 : : tree
4013 : 2570397387 : skip_artificial_parms_for (const_tree fn, tree list)
4014 : : {
4015 : 2570397387 : if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
4016 : 1488133464 : list = TREE_CHAIN (list);
4017 : : else
4018 : : return list;
4019 : :
4020 : 1488133464 : if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4021 : 10107196 : list = TREE_CHAIN (list);
4022 : 1488133464 : if (DECL_HAS_VTT_PARM_P (fn))
4023 : 12989222 : list = TREE_CHAIN (list);
4024 : : return list;
4025 : : }
4026 : :
4027 : : /* Given a FUNCTION_DECL FN and a chain LIST, return the number of
4028 : : artificial parms in FN. */
4029 : :
4030 : : int
4031 : 318591879 : num_artificial_parms_for (const_tree fn)
4032 : : {
4033 : 318591879 : int count = 0;
4034 : :
4035 : 318591879 : if (DECL_IOBJ_MEMBER_FUNCTION_P (fn))
4036 : 287938430 : count++;
4037 : : else
4038 : : return 0;
4039 : :
4040 : 287938430 : if (DECL_HAS_IN_CHARGE_PARM_P (fn))
4041 : 1777 : count++;
4042 : 287938430 : if (DECL_HAS_VTT_PARM_P (fn))
4043 : 112194 : count++;
4044 : : return count;
4045 : : }
4046 : :
4047 : : /* Return value of the __builtin_type_order trait. */
4048 : :
4049 : : tree
4050 : 4062 : type_order_value (tree type1, tree type2)
4051 : : {
4052 : 4062 : tree rettype = lookup_comparison_category (cc_strong_ordering);
4053 : 4062 : if (rettype == error_mark_node)
4054 : : return rettype;
4055 : 4060 : int ret;
4056 : 4060 : if (type1 == type2)
4057 : : ret = 0;
4058 : : else
4059 : : {
4060 : 76 : const char *name1 = ASTRDUP (mangle_type_string (type1));
4061 : 76 : const char *name2 = mangle_type_string (type2);
4062 : 76 : ret = strcmp (name1, name2);
4063 : : }
4064 : 4136 : return lookup_comparison_result (cc_strong_ordering, rettype,
4065 : 4136 : ret == 0 ? 0 : ret > 0 ? 1 : 2);
4066 : : }
4067 : :
4068 : :
4069 : : #include "gt-cp-method.h"
|