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