1/* Language-dependent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2024 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
24#include "tree.h"
25#include "cp-tree.h"
26#include "gimple-expr.h"
27#include "cgraph.h"
28#include "stor-layout.h"
29#include "print-tree.h"
30#include "tree-iterator.h"
31#include "tree-inline.h"
32#include "debug.h"
33#include "convert.h"
34#include "gimplify.h"
35#include "stringpool.h"
36#include "attribs.h"
37#include "flags.h"
38#include "selftest.h"
39
40static tree bot_manip (tree *, int *, void *);
41static tree bot_replace (tree *, int *, void *);
42static hashval_t list_hash_pieces (tree, tree, tree);
43static tree build_target_expr (tree, tree, tsubst_flags_t);
44static tree count_trees_r (tree *, int *, void *);
45static tree verify_stmt_tree_r (tree *, int *, void *);
46
47static tree handle_init_priority_attribute (tree *, tree, tree, int, bool *);
48static tree handle_abi_tag_attribute (tree *, tree, tree, int, bool *);
49static tree handle_contract_attribute (tree *, tree, tree, int, bool *);
50static tree handle_no_dangling_attribute (tree *, tree, tree, int, bool *);
51
52/* If REF is an lvalue, returns the kind of lvalue that REF is.
53 Otherwise, returns clk_none. */
54
55cp_lvalue_kind
56lvalue_kind (const_tree ref)
57{
58 cp_lvalue_kind op1_lvalue_kind = clk_none;
59 cp_lvalue_kind op2_lvalue_kind = clk_none;
60
61 /* Expressions of reference type are sometimes wrapped in
62 INDIRECT_REFs. INDIRECT_REFs are just internal compiler
63 representation, not part of the language, so we have to look
64 through them. */
65 if (REFERENCE_REF_P (ref))
66 return lvalue_kind (TREE_OPERAND (ref, 0));
67
68 if (TREE_TYPE (ref)
69 && TYPE_REF_P (TREE_TYPE (ref)))
70 {
71 /* unnamed rvalue references are rvalues */
72 if (TYPE_REF_IS_RVALUE (TREE_TYPE (ref))
73 && TREE_CODE (ref) != PARM_DECL
74 && !VAR_P (ref)
75 && TREE_CODE (ref) != COMPONENT_REF
76 /* Functions are always lvalues. */
77 && TREE_CODE (TREE_TYPE (TREE_TYPE (ref))) != FUNCTION_TYPE)
78 {
79 op1_lvalue_kind = clk_rvalueref;
80 if (implicit_rvalue_p (t: ref))
81 op1_lvalue_kind |= clk_implicit_rval;
82 return op1_lvalue_kind;
83 }
84
85 /* lvalue references and named rvalue references are lvalues. */
86 return clk_ordinary;
87 }
88
89 if (ref == current_class_ptr)
90 return clk_none;
91
92 /* Expressions with cv void type are prvalues. */
93 if (TREE_TYPE (ref) && VOID_TYPE_P (TREE_TYPE (ref)))
94 return clk_none;
95
96 switch (TREE_CODE (ref))
97 {
98 case SAVE_EXPR:
99 return clk_none;
100
101 /* preincrements and predecrements are valid lvals, provided
102 what they refer to are valid lvals. */
103 case PREINCREMENT_EXPR:
104 case PREDECREMENT_EXPR:
105 case TRY_CATCH_EXPR:
106 case REALPART_EXPR:
107 case IMAGPART_EXPR:
108 case VIEW_CONVERT_EXPR:
109 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
110 /* As for ARRAY_REF and COMPONENT_REF, these codes turn a class prvalue
111 into an xvalue: we need to materialize the temporary before we mess
112 with it. Except VIEW_CONVERT_EXPR that doesn't actually change the
113 type, as in location wrapper and REF_PARENTHESIZED_P. */
114 if (op1_lvalue_kind == clk_class
115 && !(TREE_CODE (ref) == VIEW_CONVERT_EXPR
116 && (same_type_ignoring_top_level_qualifiers_p
117 (TREE_TYPE (ref), TREE_TYPE (TREE_OPERAND (ref, 0))))))
118 return clk_rvalueref;
119 return op1_lvalue_kind;
120
121 case ARRAY_REF:
122 {
123 tree op1 = TREE_OPERAND (ref, 0);
124 if (TREE_CODE (TREE_TYPE (op1)) == ARRAY_TYPE)
125 {
126 op1_lvalue_kind = lvalue_kind (ref: op1);
127 if (op1_lvalue_kind == clk_class)
128 /* in the case of an array operand, the result is an lvalue if
129 that operand is an lvalue and an xvalue otherwise */
130 op1_lvalue_kind = clk_rvalueref;
131 return op1_lvalue_kind;
132 }
133 else
134 return clk_ordinary;
135 }
136
137 case MEMBER_REF:
138 case DOTSTAR_EXPR:
139 if (TREE_CODE (ref) == MEMBER_REF)
140 op1_lvalue_kind = clk_ordinary;
141 else
142 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
143 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
144 op1_lvalue_kind = clk_none;
145 else if (op1_lvalue_kind == clk_class)
146 /* The result of a .* expression whose second operand is a pointer to a
147 data member is an lvalue if the first operand is an lvalue and an
148 xvalue otherwise. */
149 op1_lvalue_kind = clk_rvalueref;
150 return op1_lvalue_kind;
151
152 case COMPONENT_REF:
153 if (BASELINK_P (TREE_OPERAND (ref, 1)))
154 {
155 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (ref, 1));
156
157 /* For static member function recurse on the BASELINK, we can get
158 here e.g. from reference_binding. If BASELINK_FUNCTIONS is
159 OVERLOAD, the overload is resolved first if possible through
160 resolve_address_of_overloaded_function. */
161 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_STATIC_FUNCTION_P (fn))
162 return lvalue_kind (TREE_OPERAND (ref, 1));
163 }
164 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
165 if (op1_lvalue_kind == clk_class)
166 /* If E1 is an lvalue, then E1.E2 is an lvalue;
167 otherwise E1.E2 is an xvalue. */
168 op1_lvalue_kind = clk_rvalueref;
169
170 /* Look at the member designator. */
171 if (!op1_lvalue_kind)
172 ;
173 else if (is_overloaded_fn (TREE_OPERAND (ref, 1)))
174 /* The "field" can be a FUNCTION_DECL or an OVERLOAD in some
175 situations. If we're seeing a COMPONENT_REF, it's a non-static
176 member, so it isn't an lvalue. */
177 op1_lvalue_kind = clk_none;
178 else if (TREE_CODE (TREE_OPERAND (ref, 1)) != FIELD_DECL)
179 /* This can be IDENTIFIER_NODE in a template. */;
180 else if (DECL_C_BIT_FIELD (TREE_OPERAND (ref, 1)))
181 {
182 /* Clear the ordinary bit. If this object was a class
183 rvalue we want to preserve that information. */
184 op1_lvalue_kind &= ~clk_ordinary;
185 /* The lvalue is for a bitfield. */
186 op1_lvalue_kind |= clk_bitfield;
187 }
188 else if (DECL_PACKED (TREE_OPERAND (ref, 1)))
189 op1_lvalue_kind |= clk_packed;
190
191 return op1_lvalue_kind;
192
193 case STRING_CST:
194 case COMPOUND_LITERAL_EXPR:
195 return clk_ordinary;
196
197 case CONST_DECL:
198 /* CONST_DECL without TREE_STATIC are enumeration values and
199 thus not lvalues. With TREE_STATIC they are used by ObjC++
200 in objc_build_string_object and need to be considered as
201 lvalues. */
202 if (! TREE_STATIC (ref))
203 return clk_none;
204 /* FALLTHRU */
205 case VAR_DECL:
206 if (VAR_P (ref) && DECL_HAS_VALUE_EXPR_P (ref))
207 return lvalue_kind (DECL_VALUE_EXPR (CONST_CAST_TREE (ref)));
208
209 if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
210 && DECL_LANG_SPECIFIC (ref)
211 && DECL_IN_AGGR_P (ref))
212 return clk_none;
213 /* FALLTHRU */
214 case INDIRECT_REF:
215 case ARROW_EXPR:
216 case PARM_DECL:
217 case RESULT_DECL:
218 case PLACEHOLDER_EXPR:
219 return clk_ordinary;
220
221 /* A scope ref in a template, left as SCOPE_REF to support later
222 access checking. */
223 case SCOPE_REF:
224 gcc_assert (!type_dependent_expression_p (CONST_CAST_TREE (ref)));
225 {
226 tree op = TREE_OPERAND (ref, 1);
227 if (TREE_CODE (op) == FIELD_DECL)
228 return (DECL_C_BIT_FIELD (op) ? clk_bitfield : clk_ordinary);
229 else
230 return lvalue_kind (ref: op);
231 }
232
233 case MAX_EXPR:
234 case MIN_EXPR:
235 /* Disallow <? and >? as lvalues if either argument side-effects. */
236 if (TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 0))
237 || TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 1)))
238 return clk_none;
239 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
240 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1));
241 break;
242
243 case COND_EXPR:
244 if (processing_template_decl)
245 {
246 /* Within templates, a REFERENCE_TYPE will indicate whether
247 the COND_EXPR result is an ordinary lvalue or rvalueref.
248 Since REFERENCE_TYPEs are handled above, if we reach this
249 point, we know we got a plain rvalue. Unless we have a
250 type-dependent expr, that is, but we shouldn't be testing
251 lvalueness if we can't even tell the types yet! */
252 gcc_assert (!type_dependent_expression_p (CONST_CAST_TREE (ref)));
253 goto default_;
254 }
255 {
256 tree op1 = TREE_OPERAND (ref, 1);
257 if (!op1) op1 = TREE_OPERAND (ref, 0);
258 tree op2 = TREE_OPERAND (ref, 2);
259 op1_lvalue_kind = lvalue_kind (ref: op1);
260 op2_lvalue_kind = lvalue_kind (ref: op2);
261 if (!op1_lvalue_kind != !op2_lvalue_kind)
262 {
263 /* The second or the third operand (but not both) is a
264 throw-expression; the result is of the type
265 and value category of the other. */
266 if (op1_lvalue_kind && TREE_CODE (op2) == THROW_EXPR)
267 op2_lvalue_kind = op1_lvalue_kind;
268 else if (op2_lvalue_kind && TREE_CODE (op1) == THROW_EXPR)
269 op1_lvalue_kind = op2_lvalue_kind;
270 }
271 }
272 break;
273
274 case MODOP_EXPR:
275 /* We expect to see unlowered MODOP_EXPRs only during
276 template processing. */
277 gcc_assert (processing_template_decl);
278 return clk_ordinary;
279
280 case MODIFY_EXPR:
281 case TYPEID_EXPR:
282 return clk_ordinary;
283
284 case COMPOUND_EXPR:
285 return lvalue_kind (TREE_OPERAND (ref, 1));
286
287 case TARGET_EXPR:
288 return clk_class;
289
290 case VA_ARG_EXPR:
291 return (CLASS_TYPE_P (TREE_TYPE (ref)) ? clk_class : clk_none);
292
293 case CALL_EXPR:
294 /* We can see calls outside of TARGET_EXPR in templates. */
295 if (CLASS_TYPE_P (TREE_TYPE (ref)))
296 return clk_class;
297 return clk_none;
298
299 case FUNCTION_DECL:
300 /* All functions (except non-static-member functions) are
301 lvalues. */
302 return (DECL_IOBJ_MEMBER_FUNCTION_P (ref)
303 ? clk_none : clk_ordinary);
304
305 case BASELINK:
306 /* We now represent a reference to a single static member function
307 with a BASELINK. */
308 /* This CONST_CAST is okay because BASELINK_FUNCTIONS returns
309 its argument unmodified and we assign it to a const_tree. */
310 return lvalue_kind (BASELINK_FUNCTIONS (CONST_CAST_TREE (ref)));
311
312 case PAREN_EXPR:
313 return lvalue_kind (TREE_OPERAND (ref, 0));
314
315 case TEMPLATE_PARM_INDEX:
316 if (CLASS_TYPE_P (TREE_TYPE (ref)))
317 /* A template parameter object is an lvalue. */
318 return clk_ordinary;
319 return clk_none;
320
321 default:
322 default_:
323 if (!TREE_TYPE (ref))
324 return clk_none;
325 if (CLASS_TYPE_P (TREE_TYPE (ref))
326 || TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE)
327 return clk_class;
328 return clk_none;
329 }
330
331 /* If one operand is not an lvalue at all, then this expression is
332 not an lvalue. */
333 if (!op1_lvalue_kind || !op2_lvalue_kind)
334 return clk_none;
335
336 /* Otherwise, it's an lvalue, and it has all the odd properties
337 contributed by either operand. */
338 op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind;
339 /* It's not an ordinary lvalue if it involves any other kind. */
340 if ((op1_lvalue_kind & ~clk_ordinary) != clk_none)
341 op1_lvalue_kind &= ~clk_ordinary;
342 /* It can't be both a pseudo-lvalue and a non-addressable lvalue.
343 A COND_EXPR of those should be wrapped in a TARGET_EXPR. */
344 if ((op1_lvalue_kind & (clk_rvalueref|clk_class))
345 && (op1_lvalue_kind & (clk_bitfield|clk_packed)))
346 op1_lvalue_kind = clk_none;
347 return op1_lvalue_kind;
348}
349
350/* Returns the kind of lvalue that REF is, in the sense of [basic.lval]. */
351
352cp_lvalue_kind
353real_lvalue_p (const_tree ref)
354{
355 cp_lvalue_kind kind = lvalue_kind (ref);
356 if (kind & (clk_rvalueref|clk_class))
357 return clk_none;
358 else
359 return kind;
360}
361
362/* c-common wants us to return bool. */
363
364bool
365lvalue_p (const_tree t)
366{
367 return real_lvalue_p (ref: t);
368}
369
370/* This differs from lvalue_p in that xvalues are included. */
371
372bool
373glvalue_p (const_tree ref)
374{
375 cp_lvalue_kind kind = lvalue_kind (ref);
376 if (kind & clk_class)
377 return false;
378 else
379 return (kind != clk_none);
380}
381
382/* This differs from glvalue_p in that class prvalues are included. */
383
384bool
385obvalue_p (const_tree ref)
386{
387 return (lvalue_kind (ref) != clk_none);
388}
389
390/* Returns true if REF is an xvalue (the result of dereferencing an rvalue
391 reference), false otherwise. */
392
393bool
394xvalue_p (const_tree ref)
395{
396 return (lvalue_kind (ref) & clk_rvalueref);
397}
398
399/* True if REF is a bit-field. */
400
401bool
402bitfield_p (const_tree ref)
403{
404 return (lvalue_kind (ref) & clk_bitfield);
405}
406
407/* C++-specific version of stabilize_reference. */
408
409tree
410cp_stabilize_reference (tree ref)
411{
412 if (processing_template_decl)
413 /* As in cp_save_expr. */
414 return ref;
415
416 STRIP_ANY_LOCATION_WRAPPER (ref);
417 switch (TREE_CODE (ref))
418 {
419 /* We need to treat specially anything stabilize_reference doesn't
420 handle specifically. */
421 case VAR_DECL:
422 case PARM_DECL:
423 case RESULT_DECL:
424 CASE_CONVERT:
425 case FLOAT_EXPR:
426 case FIX_TRUNC_EXPR:
427 case INDIRECT_REF:
428 case COMPONENT_REF:
429 case BIT_FIELD_REF:
430 case ARRAY_REF:
431 case ARRAY_RANGE_REF:
432 case ERROR_MARK:
433 break;
434 default:
435 cp_lvalue_kind kind = lvalue_kind (ref);
436 if ((kind & ~clk_class) != clk_none)
437 {
438 tree type = unlowered_expr_type (ref);
439 bool rval = !!(kind & clk_rvalueref);
440 type = cp_build_reference_type (type, rval);
441 /* This inhibits warnings in, eg, cxx_mark_addressable
442 (c++/60955). */
443 warning_sentinel s (extra_warnings);
444 ref = build_static_cast (input_location, type, ref,
445 tf_error);
446 }
447 }
448
449 return stabilize_reference (ref);
450}
451
452/* Test whether DECL is a builtin that may appear in a
453 constant-expression. */
454
455bool
456builtin_valid_in_constant_expr_p (const_tree decl)
457{
458 STRIP_ANY_LOCATION_WRAPPER (decl);
459 if (TREE_CODE (decl) != FUNCTION_DECL)
460 /* Not a function. */
461 return false;
462 if (DECL_BUILT_IN_CLASS (decl) != BUILT_IN_NORMAL)
463 {
464 if (fndecl_built_in_p (node: decl, klass: BUILT_IN_FRONTEND))
465 switch (DECL_FE_FUNCTION_CODE (decl))
466 {
467 case CP_BUILT_IN_IS_CONSTANT_EVALUATED:
468 case CP_BUILT_IN_SOURCE_LOCATION:
469 case CP_BUILT_IN_IS_CORRESPONDING_MEMBER:
470 case CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS:
471 return true;
472 default:
473 break;
474 }
475 /* Not a built-in. */
476 return false;
477 }
478 switch (DECL_FUNCTION_CODE (decl))
479 {
480 /* These always have constant results like the corresponding
481 macros/symbol. */
482 case BUILT_IN_FILE:
483 case BUILT_IN_FUNCTION:
484 case BUILT_IN_LINE:
485
486 /* The following built-ins are valid in constant expressions
487 when their arguments are. */
488 case BUILT_IN_ADD_OVERFLOW_P:
489 case BUILT_IN_SUB_OVERFLOW_P:
490 case BUILT_IN_MUL_OVERFLOW_P:
491
492 /* These have constant results even if their operands are
493 non-constant. */
494 case BUILT_IN_CONSTANT_P:
495 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
496 return true;
497 default:
498 return false;
499 }
500}
501
502/* Build a TARGET_EXPR, initializing the DECL with the VALUE. */
503
504static tree
505build_target_expr (tree decl, tree value, tsubst_flags_t complain)
506{
507 tree t;
508 tree type = TREE_TYPE (decl);
509
510 value = mark_rvalue_use (value);
511
512 gcc_checking_assert (VOID_TYPE_P (TREE_TYPE (value))
513 || TREE_TYPE (decl) == TREE_TYPE (value)
514 /* On ARM ctors return 'this'. */
515 || (TYPE_PTR_P (TREE_TYPE (value))
516 && TREE_CODE (value) == CALL_EXPR)
517 || useless_type_conversion_p (TREE_TYPE (decl),
518 TREE_TYPE (value)));
519
520 /* Set TREE_READONLY for optimization, such as gimplify_init_constructor
521 moving a constant aggregate into .rodata. */
522 if (CP_TYPE_CONST_NON_VOLATILE_P (type)
523 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
524 && !VOID_TYPE_P (TREE_TYPE (value))
525 && !TYPE_HAS_MUTABLE_P (type)
526 && reduced_constant_expression_p (value))
527 TREE_READONLY (decl) = true;
528
529 if (complain & tf_no_cleanup)
530 /* The caller is building a new-expr and does not need a cleanup. */
531 t = NULL_TREE;
532 else
533 {
534 t = cxx_maybe_build_cleanup (decl, complain);
535 if (t == error_mark_node)
536 return error_mark_node;
537 }
538
539 set_target_expr_eliding (value);
540
541 t = build4 (TARGET_EXPR, type, decl, value, t, NULL_TREE);
542 if (location_t eloc = cp_expr_location (t_: value))
543 SET_EXPR_LOCATION (t, eloc);
544 /* We always set TREE_SIDE_EFFECTS so that expand_expr does not
545 ignore the TARGET_EXPR. If there really turn out to be no
546 side-effects, then the optimizer should be able to get rid of
547 whatever code is generated anyhow. */
548 TREE_SIDE_EFFECTS (t) = 1;
549
550 return t;
551}
552
553/* Return an undeclared local temporary of type TYPE for use in building a
554 TARGET_EXPR. */
555
556tree
557build_local_temp (tree type)
558{
559 tree slot = build_decl (input_location,
560 VAR_DECL, NULL_TREE, type);
561 DECL_ARTIFICIAL (slot) = 1;
562 DECL_IGNORED_P (slot) = 1;
563 DECL_CONTEXT (slot) = current_function_decl;
564 layout_decl (slot, 0);
565 return slot;
566}
567
568/* Return whether DECL is such a local temporary (or one from
569 create_tmp_var_raw). */
570
571bool
572is_local_temp (tree decl)
573{
574 return (VAR_P (decl) && DECL_ARTIFICIAL (decl)
575 && !TREE_STATIC (decl));
576}
577
578/* Set various status flags when building an AGGR_INIT_EXPR object T. */
579
580static void
581process_aggr_init_operands (tree t)
582{
583 bool side_effects;
584
585 side_effects = TREE_SIDE_EFFECTS (t);
586 if (!side_effects)
587 {
588 int i, n;
589 n = TREE_OPERAND_LENGTH (t);
590 for (i = 1; i < n; i++)
591 {
592 tree op = TREE_OPERAND (t, i);
593 if (op && TREE_SIDE_EFFECTS (op))
594 {
595 side_effects = 1;
596 break;
597 }
598 }
599 }
600 TREE_SIDE_EFFECTS (t) = side_effects;
601}
602
603/* Build an AGGR_INIT_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE,
604 FN, and SLOT. NARGS is the number of call arguments which are specified
605 as a tree array ARGS. */
606
607static tree
608build_aggr_init_array (tree return_type, tree fn, tree slot, int nargs,
609 tree *args)
610{
611 tree t;
612 int i;
613
614 t = build_vl_exp (AGGR_INIT_EXPR, nargs + 3);
615 TREE_TYPE (t) = return_type;
616 AGGR_INIT_EXPR_FN (t) = fn;
617 AGGR_INIT_EXPR_SLOT (t) = slot;
618 for (i = 0; i < nargs; i++)
619 AGGR_INIT_EXPR_ARG (t, i) = args[i];
620 process_aggr_init_operands (t);
621 return t;
622}
623
624/* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
625 target. TYPE is the type to be initialized.
626
627 Build an AGGR_INIT_EXPR to represent the initialization. This function
628 differs from build_cplus_new in that an AGGR_INIT_EXPR can only be used
629 to initialize another object, whereas a TARGET_EXPR can either
630 initialize another object or create its own temporary object, and as a
631 result building up a TARGET_EXPR requires that the type's destructor be
632 callable. */
633
634tree
635build_aggr_init_expr (tree type, tree init)
636{
637 tree fn;
638 tree slot;
639 tree rval;
640 int is_ctor;
641
642 gcc_assert (!VOID_TYPE_P (type));
643
644 /* Don't build AGGR_INIT_EXPR in a template. */
645 if (processing_template_decl)
646 return init;
647
648 fn = cp_get_callee (init);
649 if (fn == NULL_TREE)
650 return convert (type, init);
651
652 is_ctor = (TREE_CODE (fn) == ADDR_EXPR
653 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
654 && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0)));
655
656 /* We split the CALL_EXPR into its function and its arguments here.
657 Then, in expand_expr, we put them back together. The reason for
658 this is that this expression might be a default argument
659 expression. In that case, we need a new temporary every time the
660 expression is used. That's what break_out_target_exprs does; it
661 replaces every AGGR_INIT_EXPR with a copy that uses a fresh
662 temporary slot. Then, expand_expr builds up a call-expression
663 using the new slot. */
664
665 /* If we don't need to use a constructor to create an object of this
666 type, don't mess with AGGR_INIT_EXPR. */
667 if (is_ctor || TREE_ADDRESSABLE (type))
668 {
669 slot = build_local_temp (type);
670
671 if (TREE_CODE (init) == CALL_EXPR)
672 {
673 rval = build_aggr_init_array (void_type_node, fn, slot,
674 call_expr_nargs (init),
675 CALL_EXPR_ARGP (init));
676 AGGR_INIT_FROM_THUNK_P (rval)
677 = CALL_FROM_THUNK_P (init);
678 }
679 else
680 {
681 rval = build_aggr_init_array (void_type_node, fn, slot,
682 aggr_init_expr_nargs (init),
683 AGGR_INIT_EXPR_ARGP (init));
684 AGGR_INIT_FROM_THUNK_P (rval)
685 = AGGR_INIT_FROM_THUNK_P (init);
686 }
687 TREE_SIDE_EFFECTS (rval) = 1;
688 AGGR_INIT_VIA_CTOR_P (rval) = is_ctor;
689 TREE_NOTHROW (rval) = TREE_NOTHROW (init);
690 CALL_EXPR_OPERATOR_SYNTAX (rval) = CALL_EXPR_OPERATOR_SYNTAX (init);
691 CALL_EXPR_ORDERED_ARGS (rval) = CALL_EXPR_ORDERED_ARGS (init);
692 CALL_EXPR_REVERSE_ARGS (rval) = CALL_EXPR_REVERSE_ARGS (init);
693 SET_EXPR_LOCATION (rval, EXPR_LOCATION (init));
694 }
695 else
696 rval = init;
697
698 return rval;
699}
700
701/* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
702 target. TYPE is the type that this initialization should appear to
703 have.
704
705 Build an encapsulation of the initialization to perform
706 and return it so that it can be processed by language-independent
707 and language-specific expression expanders. */
708
709tree
710build_cplus_new (tree type, tree init, tsubst_flags_t complain)
711{
712 /* This function should cope with what build_special_member_call
713 can produce. When performing parenthesized aggregate initialization,
714 it can produce a { }. */
715 if (BRACE_ENCLOSED_INITIALIZER_P (init))
716 {
717 gcc_assert (cxx_dialect >= cxx20);
718 return finish_compound_literal (type, init, complain);
719 }
720
721 tree rval = build_aggr_init_expr (type, init);
722 tree slot;
723
724 if (init == error_mark_node)
725 return error_mark_node;
726
727 if (!complete_type_or_maybe_complain (type, init, complain))
728 return error_mark_node;
729
730 /* Make sure that we're not trying to create an instance of an
731 abstract class. */
732 if (abstract_virtuals_error (NULL_TREE, type, complain))
733 return error_mark_node;
734
735 if (TREE_CODE (rval) == AGGR_INIT_EXPR)
736 slot = AGGR_INIT_EXPR_SLOT (rval);
737 else if (TREE_CODE (rval) == CALL_EXPR
738 || TREE_CODE (rval) == CONSTRUCTOR)
739 slot = build_local_temp (type);
740 else
741 return rval;
742
743 rval = build_target_expr (decl: slot, value: rval, complain);
744
745 if (rval != error_mark_node)
746 TARGET_EXPR_IMPLICIT_P (rval) = 1;
747
748 return rval;
749}
750
751/* Subroutine of build_vec_init_expr: Build up a single element
752 intialization as a proxy for the full array initialization to get things
753 marked as used and any appropriate diagnostics.
754
755 This used to be necessary because we were deferring building the actual
756 constructor calls until gimplification time; now we only do it to set
757 VEC_INIT_EXPR_IS_CONSTEXPR.
758
759 We assume that init is either NULL_TREE, {}, void_type_node (indicating
760 value-initialization), or another array to copy. */
761
762static tree
763build_vec_init_elt (tree type, tree init, tsubst_flags_t complain)
764{
765 tree inner_type = strip_array_types (type);
766
767 if (integer_zerop (array_type_nelts_total (type))
768 || !CLASS_TYPE_P (inner_type))
769 /* No interesting initialization to do. */
770 return integer_zero_node;
771 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
772 {
773 /* Even if init has initializers for some array elements,
774 we're interested in the {}-init of trailing elements. */
775 if (CP_AGGREGATE_TYPE_P (inner_type))
776 {
777 tree empty = build_constructor (init_list_type_node, nullptr);
778 return digest_init (inner_type, empty, complain);
779 }
780 else
781 /* It's equivalent to value-init. */
782 init = void_type_node;
783 }
784 if (init == void_type_node)
785 return build_value_init (inner_type, complain);
786
787 releasing_vec argvec;
788 if (init && !BRACE_ENCLOSED_INITIALIZER_P (init))
789 {
790 tree init_type = strip_array_types (TREE_TYPE (init));
791 tree dummy = build_dummy_object (init_type);
792 if (!lvalue_p (t: init))
793 dummy = move (dummy);
794 argvec->quick_push (obj: dummy);
795 }
796 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
797 &argvec, inner_type, LOOKUP_NORMAL,
798 complain);
799
800 /* For a trivial constructor, build_over_call creates a TARGET_EXPR. But
801 we don't want one here because we aren't creating a temporary. */
802 if (TREE_CODE (init) == TARGET_EXPR)
803 init = TARGET_EXPR_INITIAL (init);
804
805 return init;
806}
807
808/* Return a TARGET_EXPR which expresses the initialization of an array to
809 be named later, either default-initialization or copy-initialization
810 from another array of the same type. */
811
812tree
813build_vec_init_expr (tree type, tree init, tsubst_flags_t complain)
814{
815 if (tree vi = get_vec_init_expr (t: init))
816 return vi;
817
818 tree elt_init;
819 if (init && TREE_CODE (init) == CONSTRUCTOR
820 && !BRACE_ENCLOSED_INITIALIZER_P (init))
821 /* We built any needed constructor calls in digest_init. */
822 elt_init = init;
823 else
824 elt_init = build_vec_init_elt (type, init, complain);
825
826 bool value_init = false;
827 if (init == void_type_node)
828 {
829 value_init = true;
830 init = NULL_TREE;
831 }
832
833 tree slot = build_local_temp (type);
834 init = build2 (VEC_INIT_EXPR, type, slot, init);
835 TREE_SIDE_EFFECTS (init) = true;
836 SET_EXPR_LOCATION (init, input_location);
837
838 if (cxx_dialect >= cxx11)
839 {
840 bool cx = potential_constant_expression (elt_init);
841 if (BRACE_ENCLOSED_INITIALIZER_P (init))
842 cx &= potential_constant_expression (init);
843 VEC_INIT_EXPR_IS_CONSTEXPR (init) = cx;
844 }
845 VEC_INIT_EXPR_VALUE_INIT (init) = value_init;
846
847 return init;
848}
849
850/* Call build_vec_init to expand VEC_INIT into TARGET (for which NULL_TREE
851 means VEC_INIT_EXPR_SLOT). */
852
853tree
854expand_vec_init_expr (tree target, tree vec_init, tsubst_flags_t complain,
855 vec<tree,va_gc> **flags)
856{
857 iloc_sentinel ils = EXPR_LOCATION (vec_init);
858
859 if (!target)
860 target = VEC_INIT_EXPR_SLOT (vec_init);
861 tree init = VEC_INIT_EXPR_INIT (vec_init);
862 int from_array = (init && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE);
863 return build_vec_init (target, NULL_TREE, init,
864 VEC_INIT_EXPR_VALUE_INIT (vec_init),
865 from_array, complain, flags);
866}
867
868/* Give a helpful diagnostic for a non-constexpr VEC_INIT_EXPR in a context
869 that requires a constant expression. */
870
871void
872diagnose_non_constexpr_vec_init (tree expr)
873{
874 tree type = TREE_TYPE (VEC_INIT_EXPR_SLOT (expr));
875 tree init, elt_init;
876 if (VEC_INIT_EXPR_VALUE_INIT (expr))
877 init = void_type_node;
878 else
879 init = VEC_INIT_EXPR_INIT (expr);
880
881 elt_init = build_vec_init_elt (type, init, complain: tf_warning_or_error);
882 require_potential_constant_expression (elt_init);
883}
884
885tree
886build_array_copy (tree init)
887{
888 return get_target_expr (build_vec_init_expr
889 (TREE_TYPE (init), init, complain: tf_warning_or_error));
890}
891
892/* Build a TARGET_EXPR using INIT to initialize a new temporary of the
893 indicated TYPE. */
894
895tree
896build_target_expr_with_type (tree init, tree type, tsubst_flags_t complain)
897{
898 gcc_assert (!VOID_TYPE_P (type));
899 gcc_assert (!VOID_TYPE_P (TREE_TYPE (init)));
900
901 if (TREE_CODE (init) == TARGET_EXPR
902 || init == error_mark_node)
903 return init;
904 else if (CLASS_TYPE_P (type) && type_has_nontrivial_copy_init (type)
905 && TREE_CODE (init) != COND_EXPR
906 && TREE_CODE (init) != CONSTRUCTOR
907 && TREE_CODE (init) != VA_ARG_EXPR
908 && TREE_CODE (init) != CALL_EXPR)
909 /* We need to build up a copy constructor call. COND_EXPR is a special
910 case because we already have copies on the arms and we don't want
911 another one here. A CONSTRUCTOR is aggregate initialization, which
912 is handled separately. A VA_ARG_EXPR is magic creation of an
913 aggregate; there's no additional work to be done. A CALL_EXPR
914 already creates a prvalue. */
915 return force_rvalue (init, complain);
916
917 return force_target_expr (type, init, complain);
918}
919
920/* Like the above function, but without the checking. This function should
921 only be used by code which is deliberately trying to subvert the type
922 system, such as call_builtin_trap. Or build_over_call, to avoid
923 infinite recursion. */
924
925tree
926force_target_expr (tree type, tree init, tsubst_flags_t complain)
927{
928 tree slot;
929
930 gcc_assert (!VOID_TYPE_P (type));
931
932 slot = build_local_temp (type);
933 return build_target_expr (decl: slot, value: init, complain);
934}
935
936/* Like build_target_expr_with_type, but use the type of INIT. */
937
938tree
939get_target_expr (tree init, tsubst_flags_t complain /* = tf_warning_or_error */)
940{
941 if (TREE_CODE (init) == AGGR_INIT_EXPR)
942 return build_target_expr (AGGR_INIT_EXPR_SLOT (init), value: init, complain);
943 else if (TREE_CODE (init) == VEC_INIT_EXPR)
944 return build_target_expr (VEC_INIT_EXPR_SLOT (init), value: init, complain);
945 else
946 {
947 init = convert_bitfield_to_declared_type (init);
948 return build_target_expr_with_type (init, TREE_TYPE (init), complain);
949 }
950}
951
952/* If EXPR is a bitfield reference, convert it to the declared type of
953 the bitfield, and return the resulting expression. Otherwise,
954 return EXPR itself. */
955
956tree
957convert_bitfield_to_declared_type (tree expr)
958{
959 tree bitfield_type;
960
961 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
962 if (bitfield_type)
963 expr = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type),
964 x: expr);
965 return expr;
966}
967
968/* EXPR is being used in an rvalue context. Return a version of EXPR
969 that is marked as an rvalue. */
970
971tree
972rvalue (tree expr)
973{
974 tree type;
975
976 if (error_operand_p (t: expr))
977 return expr;
978
979 expr = mark_rvalue_use (expr);
980
981 /* [expr.type]: "If a prvalue initially has the type "cv T", where T is a
982 cv-unqualified non-class, non-array type, the type of the expression is
983 adjusted to T prior to any further analysis. */
984 type = TREE_TYPE (expr);
985 if (!CLASS_TYPE_P (type) && TREE_CODE (type) != ARRAY_TYPE
986 && cv_qualified_p (type))
987 type = cv_unqualified (type);
988
989 /* We need to do this for rvalue refs as well to get the right answer
990 from decltype; see c++/36628. */
991 if (!processing_template_decl && glvalue_p (ref: expr))
992 {
993 /* But don't use this function for class lvalues; use move (to treat an
994 lvalue as an xvalue) or force_rvalue (to make a prvalue copy). */
995 gcc_checking_assert (!CLASS_TYPE_P (type));
996 expr = build1 (NON_LVALUE_EXPR, type, expr);
997 }
998 else if (type != TREE_TYPE (expr))
999 expr = build_nop (type, expr);
1000
1001 return expr;
1002}
1003
1004
1005struct cplus_array_info
1006{
1007 tree type;
1008 tree domain;
1009};
1010
1011struct cplus_array_hasher : ggc_ptr_hash<tree_node>
1012{
1013 typedef cplus_array_info *compare_type;
1014
1015 static hashval_t hash (tree t);
1016 static bool equal (tree, cplus_array_info *);
1017};
1018
1019/* Hash an ARRAY_TYPE. K is really of type `tree'. */
1020
1021hashval_t
1022cplus_array_hasher::hash (tree t)
1023{
1024 hashval_t hash;
1025
1026 hash = TYPE_UID (TREE_TYPE (t));
1027 if (TYPE_DOMAIN (t))
1028 hash ^= TYPE_UID (TYPE_DOMAIN (t));
1029 return hash;
1030}
1031
1032/* Compare two ARRAY_TYPEs. K1 is really of type `tree', K2 is really
1033 of type `cplus_array_info*'. */
1034
1035bool
1036cplus_array_hasher::equal (tree t1, cplus_array_info *t2)
1037{
1038 return (TREE_TYPE (t1) == t2->type && TYPE_DOMAIN (t1) == t2->domain);
1039}
1040
1041/* Hash table containing dependent array types, which are unsuitable for
1042 the language-independent type hash table. */
1043static GTY (()) hash_table<cplus_array_hasher> *cplus_array_htab;
1044
1045/* Build an ARRAY_TYPE without laying it out. */
1046
1047static tree
1048build_min_array_type (tree elt_type, tree index_type)
1049{
1050 tree t = cxx_make_type (ARRAY_TYPE);
1051 TREE_TYPE (t) = elt_type;
1052 TYPE_DOMAIN (t) = index_type;
1053 return t;
1054}
1055
1056/* Set TYPE_CANONICAL like build_array_type_1, but using
1057 build_cplus_array_type. */
1058
1059static void
1060set_array_type_canon (tree t, tree elt_type, tree index_type, bool dep)
1061{
1062 /* Set the canonical type for this new node. */
1063 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
1064 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
1065 SET_TYPE_STRUCTURAL_EQUALITY (t);
1066 else if (TYPE_CANONICAL (elt_type) != elt_type
1067 || (index_type && TYPE_CANONICAL (index_type) != index_type))
1068 TYPE_CANONICAL (t)
1069 = build_cplus_array_type (TYPE_CANONICAL (elt_type),
1070 index_type
1071 ? TYPE_CANONICAL (index_type) : index_type,
1072 is_dep: dep);
1073 else
1074 TYPE_CANONICAL (t) = t;
1075}
1076
1077/* Like build_array_type, but handle special C++ semantics: an array of a
1078 variant element type is a variant of the array of the main variant of
1079 the element type. IS_DEPENDENT is -ve if we should determine the
1080 dependency. Otherwise its bool value indicates dependency. */
1081
1082tree
1083build_cplus_array_type (tree elt_type, tree index_type, int dependent)
1084{
1085 tree t;
1086
1087 if (elt_type == error_mark_node || index_type == error_mark_node)
1088 return error_mark_node;
1089
1090 if (dependent < 0)
1091 dependent = (uses_template_parms (elt_type)
1092 || (index_type && uses_template_parms (index_type)));
1093
1094 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
1095 /* Start with an array of the TYPE_MAIN_VARIANT. */
1096 t = build_cplus_array_type (TYPE_MAIN_VARIANT (elt_type),
1097 index_type, dependent);
1098 else if (dependent)
1099 {
1100 /* Since type_hash_canon calls layout_type, we need to use our own
1101 hash table. */
1102 cplus_array_info cai;
1103 hashval_t hash;
1104
1105 if (cplus_array_htab == NULL)
1106 cplus_array_htab = hash_table<cplus_array_hasher>::create_ggc (n: 61);
1107
1108 hash = TYPE_UID (elt_type);
1109 if (index_type)
1110 hash ^= TYPE_UID (index_type);
1111 cai.type = elt_type;
1112 cai.domain = index_type;
1113
1114 tree *e = cplus_array_htab->find_slot_with_hash (comparable: &cai, hash, insert: INSERT);
1115 if (*e)
1116 /* We have found the type: we're done. */
1117 return (tree) *e;
1118 else
1119 {
1120 /* Build a new array type. */
1121 t = build_min_array_type (elt_type, index_type);
1122
1123 /* Store it in the hash table. */
1124 *e = t;
1125
1126 /* Set the canonical type for this new node. */
1127 set_array_type_canon (t, elt_type, index_type, dep: dependent);
1128
1129 /* Mark it as dependent now, this saves time later. */
1130 TYPE_DEPENDENT_P_VALID (t) = true;
1131 TYPE_DEPENDENT_P (t) = true;
1132 }
1133 }
1134 else
1135 {
1136 bool typeless_storage = is_byte_access_type (elt_type);
1137 t = build_array_type (elt_type, index_type, typeless_storage);
1138
1139 /* Mark as non-dependenty now, this will save time later. */
1140 TYPE_DEPENDENT_P_VALID (t) = true;
1141 }
1142
1143 /* Now check whether we already have this array variant. */
1144 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
1145 {
1146 tree m = t;
1147 for (t = m; t; t = TYPE_NEXT_VARIANT (t))
1148 if (TREE_TYPE (t) == elt_type
1149 && TYPE_NAME (t) == NULL_TREE
1150 && TYPE_ATTRIBUTES (t) == NULL_TREE)
1151 break;
1152 if (!t)
1153 {
1154 t = build_min_array_type (elt_type, index_type);
1155 /* Mark dependency now, this saves time later. */
1156 TYPE_DEPENDENT_P_VALID (t) = true;
1157 TYPE_DEPENDENT_P (t) = dependent;
1158 set_array_type_canon (t, elt_type, index_type, dep: dependent);
1159 if (!dependent)
1160 {
1161 layout_type (t);
1162 /* Make sure sizes are shared with the main variant.
1163 layout_type can't be called after setting TYPE_NEXT_VARIANT,
1164 as it will overwrite alignment etc. of all variants. */
1165 TYPE_SIZE (t) = TYPE_SIZE (m);
1166 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (m);
1167 TYPE_TYPELESS_STORAGE (t) = TYPE_TYPELESS_STORAGE (m);
1168 }
1169
1170 TYPE_MAIN_VARIANT (t) = m;
1171 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
1172 TYPE_NEXT_VARIANT (m) = t;
1173 }
1174 }
1175
1176 /* Avoid spurious warnings with VLAs (c++/54583). */
1177 if (TYPE_SIZE (t) && EXPR_P (TYPE_SIZE (t)))
1178 suppress_warning (TYPE_SIZE (t), OPT_Wunused);
1179
1180 /* Push these needs up to the ARRAY_TYPE so that initialization takes
1181 place more easily. */
1182 bool needs_ctor = (TYPE_NEEDS_CONSTRUCTING (t)
1183 = TYPE_NEEDS_CONSTRUCTING (elt_type));
1184 bool needs_dtor = (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1185 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type));
1186
1187 if (!dependent && t == TYPE_MAIN_VARIANT (t)
1188 && !COMPLETE_TYPE_P (t) && COMPLETE_TYPE_P (elt_type))
1189 {
1190 /* The element type has been completed since the last time we saw
1191 this array type; update the layout and 'tor flags for any variants
1192 that need it. */
1193 layout_type (t);
1194 for (tree v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1195 {
1196 TYPE_NEEDS_CONSTRUCTING (v) = needs_ctor;
1197 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (v) = needs_dtor;
1198 }
1199 }
1200
1201 return t;
1202}
1203
1204/* Return an ARRAY_TYPE with element type ELT and length N. */
1205
1206tree
1207build_array_of_n_type (tree elt, int n)
1208{
1209 return build_cplus_array_type (elt_type: elt, index_type: build_index_type (size_int (n - 1)));
1210}
1211
1212/* True iff T is an array of unknown bound. */
1213
1214bool
1215array_of_unknown_bound_p (const_tree t)
1216{
1217 return (TREE_CODE (t) == ARRAY_TYPE
1218 && !TYPE_DOMAIN (t));
1219}
1220
1221/* True iff T is an N3639 array of runtime bound (VLA). These were approved
1222 for C++14 but then removed. This should only be used for N3639
1223 specifically; code wondering more generally if something is a VLA should use
1224 vla_type_p. */
1225
1226bool
1227array_of_runtime_bound_p (tree t)
1228{
1229 if (!t || TREE_CODE (t) != ARRAY_TYPE)
1230 return false;
1231 if (variably_modified_type_p (TREE_TYPE (t), NULL_TREE))
1232 return false;
1233 tree dom = TYPE_DOMAIN (t);
1234 if (!dom)
1235 return false;
1236 tree max = TYPE_MAX_VALUE (dom);
1237 return (!potential_rvalue_constant_expression (max)
1238 || (!value_dependent_expression_p (max) && !TREE_CONSTANT (max)));
1239}
1240
1241/* True iff T is a variable length array. */
1242
1243bool
1244vla_type_p (tree t)
1245{
1246 for (; t && TREE_CODE (t) == ARRAY_TYPE;
1247 t = TREE_TYPE (t))
1248 if (tree dom = TYPE_DOMAIN (t))
1249 {
1250 tree max = TYPE_MAX_VALUE (dom);
1251 if (!potential_rvalue_constant_expression (max)
1252 || (!value_dependent_expression_p (max) && !TREE_CONSTANT (max)))
1253 return true;
1254 }
1255 return false;
1256}
1257
1258
1259/* Return a reference type node of MODE referring to TO_TYPE. If MODE
1260 is VOIDmode the standard pointer mode will be picked. If RVAL is
1261 true, return an rvalue reference type, otherwise return an lvalue
1262 reference type. If a type node exists, reuse it, otherwise create
1263 a new one. */
1264tree
1265cp_build_reference_type_for_mode (tree to_type, machine_mode mode, bool rval)
1266{
1267 tree lvalue_ref, t;
1268
1269 if (to_type == error_mark_node)
1270 return error_mark_node;
1271
1272 if (TYPE_REF_P (to_type))
1273 {
1274 rval = rval && TYPE_REF_IS_RVALUE (to_type);
1275 to_type = TREE_TYPE (to_type);
1276 }
1277
1278 lvalue_ref = build_reference_type_for_mode (to_type, mode, false);
1279
1280 if (!rval)
1281 return lvalue_ref;
1282
1283 /* This code to create rvalue reference types is based on and tied
1284 to the code creating lvalue reference types in the middle-end
1285 functions build_reference_type_for_mode and build_reference_type.
1286
1287 It works by putting the rvalue reference type nodes after the
1288 lvalue reference nodes in the TYPE_NEXT_REF_TO linked list, so
1289 they will effectively be ignored by the middle end. */
1290
1291 for (t = lvalue_ref; (t = TYPE_NEXT_REF_TO (t)); )
1292 if (TYPE_REF_IS_RVALUE (t))
1293 return t;
1294
1295 t = build_distinct_type_copy (lvalue_ref);
1296
1297 TYPE_REF_IS_RVALUE (t) = true;
1298 TYPE_NEXT_REF_TO (t) = TYPE_NEXT_REF_TO (lvalue_ref);
1299 TYPE_NEXT_REF_TO (lvalue_ref) = t;
1300
1301 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
1302 SET_TYPE_STRUCTURAL_EQUALITY (t);
1303 else if (TYPE_CANONICAL (to_type) != to_type)
1304 TYPE_CANONICAL (t)
1305 = cp_build_reference_type_for_mode (TYPE_CANONICAL (to_type), mode, rval);
1306 else
1307 TYPE_CANONICAL (t) = t;
1308
1309 layout_type (t);
1310
1311 return t;
1312
1313}
1314
1315/* Return a reference type node referring to TO_TYPE. If RVAL is
1316 true, return an rvalue reference type, otherwise return an lvalue
1317 reference type. If a type node exists, reuse it, otherwise create
1318 a new one. */
1319tree
1320cp_build_reference_type (tree to_type, bool rval)
1321{
1322 return cp_build_reference_type_for_mode (to_type, VOIDmode, rval);
1323}
1324
1325/* Returns EXPR cast to rvalue reference type, like std::move. */
1326
1327tree
1328move (tree expr)
1329{
1330 tree type = TREE_TYPE (expr);
1331 gcc_assert (!TYPE_REF_P (type));
1332 if (xvalue_p (ref: expr))
1333 return expr;
1334 type = cp_build_reference_type (to_type: type, /*rval*/true);
1335 return build_static_cast (input_location, type, expr,
1336 tf_warning_or_error);
1337}
1338
1339/* Used by the C++ front end to build qualified array types. However,
1340 the C version of this function does not properly maintain canonical
1341 types (which are not used in C). */
1342tree
1343c_build_qualified_type (tree type, int type_quals, tree /* orig_qual_type */,
1344 size_t /* orig_qual_indirect */)
1345{
1346 return cp_build_qualified_type (type, type_quals);
1347}
1348
1349
1350/* Make a variant of TYPE, qualified with the TYPE_QUALS. Handles
1351 arrays correctly. In particular, if TYPE is an array of T's, and
1352 TYPE_QUALS is non-empty, returns an array of qualified T's.
1353
1354 FLAGS determines how to deal with ill-formed qualifications. If
1355 tf_ignore_bad_quals is set, then bad qualifications are dropped
1356 (this is permitted if TYPE was introduced via a typedef or template
1357 type parameter). If bad qualifications are dropped and tf_warning
1358 is set, then a warning is issued for non-const qualifications. If
1359 tf_ignore_bad_quals is not set and tf_error is not set, we
1360 return error_mark_node. Otherwise, we issue an error, and ignore
1361 the qualifications.
1362
1363 Qualification of a reference type is valid when the reference came
1364 via a typedef or template type argument. [dcl.ref] No such
1365 dispensation is provided for qualifying a function type. [dcl.fct]
1366 DR 295 queries this and the proposed resolution brings it into line
1367 with qualifying a reference. We implement the DR. We also behave
1368 in a similar manner for restricting non-pointer types. */
1369
1370tree
1371cp_build_qualified_type (tree type, int type_quals,
1372 tsubst_flags_t complain /* = tf_warning_or_error */)
1373{
1374 tree result;
1375 int bad_quals = TYPE_UNQUALIFIED;
1376
1377 if (type == error_mark_node)
1378 return type;
1379
1380 if (type_quals == cp_type_quals (type))
1381 return type;
1382
1383 if (TREE_CODE (type) == ARRAY_TYPE)
1384 {
1385 /* In C++, the qualification really applies to the array element
1386 type. Obtain the appropriately qualified element type. */
1387 tree t;
1388 tree element_type
1389 = cp_build_qualified_type (TREE_TYPE (type), type_quals, complain);
1390
1391 if (element_type == error_mark_node)
1392 return error_mark_node;
1393
1394 /* See if we already have an identically qualified type. Tests
1395 should be equivalent to those in check_qualified_type. */
1396 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1397 if (TREE_TYPE (t) == element_type
1398 && TYPE_NAME (t) == TYPE_NAME (type)
1399 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
1400 && attribute_list_equal (TYPE_ATTRIBUTES (t),
1401 TYPE_ATTRIBUTES (type)))
1402 break;
1403
1404 if (!t)
1405 {
1406 /* If we already know the dependentness, tell the array type
1407 constructor. This is important for module streaming, as we cannot
1408 dynamically determine that on read in. */
1409 t = build_cplus_array_type (elt_type: element_type, TYPE_DOMAIN (type),
1410 TYPE_DEPENDENT_P_VALID (type)
1411 ? int (TYPE_DEPENDENT_P (type)) : -1);
1412
1413 /* Keep the typedef name. */
1414 if (TYPE_NAME (t) != TYPE_NAME (type))
1415 {
1416 t = build_variant_type_copy (t);
1417 TYPE_NAME (t) = TYPE_NAME (type);
1418 SET_TYPE_ALIGN (t, TYPE_ALIGN (type));
1419 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (type);
1420 }
1421 }
1422
1423 /* Even if we already had this variant, we update
1424 TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case
1425 they changed since the variant was originally created.
1426
1427 This seems hokey; if there is some way to use a previous
1428 variant *without* coming through here,
1429 TYPE_NEEDS_CONSTRUCTING will never be updated. */
1430 TYPE_NEEDS_CONSTRUCTING (t)
1431 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
1432 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1433 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
1434 return t;
1435 }
1436 else if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
1437 {
1438 tree t = PACK_EXPANSION_PATTERN (type);
1439
1440 t = cp_build_qualified_type (type: t, type_quals, complain);
1441 return make_pack_expansion (t, complain);
1442 }
1443
1444 /* A reference or method type shall not be cv-qualified.
1445 [dcl.ref], [dcl.fct]. This used to be an error, but as of DR 295
1446 (in CD1) we always ignore extra cv-quals on functions. */
1447
1448 /* [dcl.ref/1] Cv-qualified references are ill-formed except when
1449 the cv-qualifiers are introduced through the use of a typedef-name
1450 ([dcl.typedef], [temp.param]) or decltype-specifier
1451 ([dcl.type.decltype]),in which case the cv-qualifiers are
1452 ignored. */
1453 if (type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)
1454 && (TYPE_REF_P (type)
1455 || FUNC_OR_METHOD_TYPE_P (type)))
1456 {
1457 if (TYPE_REF_P (type)
1458 && (!typedef_variant_p (type) || FUNC_OR_METHOD_TYPE_P (type)))
1459 bad_quals |= type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1460 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1461 }
1462
1463 /* But preserve any function-cv-quals on a FUNCTION_TYPE. */
1464 if (TREE_CODE (type) == FUNCTION_TYPE)
1465 type_quals |= type_memfn_quals (type);
1466
1467 /* A restrict-qualified type must be a pointer (or reference)
1468 to object or incomplete type. */
1469 if ((type_quals & TYPE_QUAL_RESTRICT)
1470 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1471 && TREE_CODE (type) != TYPENAME_TYPE
1472 && !INDIRECT_TYPE_P (type))
1473 {
1474 bad_quals |= TYPE_QUAL_RESTRICT;
1475 type_quals &= ~TYPE_QUAL_RESTRICT;
1476 }
1477
1478 if (bad_quals == TYPE_UNQUALIFIED
1479 || (complain & tf_ignore_bad_quals))
1480 /*OK*/;
1481 else if (!(complain & tf_error))
1482 return error_mark_node;
1483 else
1484 {
1485 tree bad_type = build_qualified_type (ptr_type_node, bad_quals);
1486 error ("%qV qualifiers cannot be applied to %qT",
1487 bad_type, type);
1488 }
1489
1490 /* Retrieve (or create) the appropriately qualified variant. */
1491 result = build_qualified_type (type, type_quals);
1492
1493 return result;
1494}
1495
1496/* Return TYPE with const and volatile removed. */
1497
1498tree
1499cv_unqualified (tree type)
1500{
1501 int quals;
1502
1503 if (type == error_mark_node)
1504 return type;
1505
1506 quals = cp_type_quals (type);
1507 quals &= ~(TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE);
1508 return cp_build_qualified_type (type, type_quals: quals);
1509}
1510
1511/* Subroutine of strip_typedefs. We want to apply to RESULT the attributes
1512 from ATTRIBS that affect type identity, and no others. If any are not
1513 applied, set *remove_attributes to true. */
1514
1515static tree
1516apply_identity_attributes (tree result, tree attribs, bool *remove_attributes)
1517{
1518 tree first_ident = NULL_TREE;
1519 tree new_attribs = NULL_TREE;
1520 tree *p = &new_attribs;
1521
1522 if (OVERLOAD_TYPE_P (result))
1523 {
1524 /* On classes and enums all attributes are ingrained. */
1525 gcc_assert (attribs == TYPE_ATTRIBUTES (result));
1526 return result;
1527 }
1528
1529 for (tree a = attribs; a; a = TREE_CHAIN (a))
1530 {
1531 const attribute_spec *as
1532 = lookup_attribute_spec (get_attribute_name (a));
1533 if (as && as->affects_type_identity)
1534 {
1535 if (!first_ident)
1536 first_ident = a;
1537 else if (first_ident == error_mark_node)
1538 {
1539 *p = tree_cons (TREE_PURPOSE (a), TREE_VALUE (a), NULL_TREE);
1540 p = &TREE_CHAIN (*p);
1541 }
1542 }
1543 else if (first_ident && first_ident != error_mark_node)
1544 {
1545 for (tree a2 = first_ident; a2 != a; a2 = TREE_CHAIN (a2))
1546 {
1547 *p = tree_cons (TREE_PURPOSE (a2), TREE_VALUE (a2), NULL_TREE);
1548 p = &TREE_CHAIN (*p);
1549 }
1550 first_ident = error_mark_node;
1551 }
1552 }
1553 if (first_ident != error_mark_node)
1554 new_attribs = first_ident;
1555
1556 if (first_ident == attribs)
1557 /* All attributes affected type identity. */;
1558 else
1559 *remove_attributes = true;
1560
1561 return cp_build_type_attribute_variant (result, new_attribs);
1562}
1563
1564/* Builds a qualified variant of T that is either not a typedef variant
1565 (the default behavior) or not a typedef variant of a user-facing type
1566 (if FLAGS contains STF_USER_FACING). If T is not a type, then this
1567 just dispatches to strip_typedefs_expr.
1568
1569 E.g. consider the following declarations:
1570 typedef const int ConstInt;
1571 typedef ConstInt* PtrConstInt;
1572 If T is PtrConstInt, this function returns a type representing
1573 const int*.
1574 In other words, if T is a typedef, the function returns the underlying type.
1575 The cv-qualification and attributes of the type returned match the
1576 input type.
1577 They will always be compatible types.
1578 The returned type is built so that all of its subtypes
1579 recursively have their typedefs stripped as well.
1580
1581 This is different from just returning TYPE_CANONICAL (T)
1582 Because of several reasons:
1583 * If T is a type that needs structural equality
1584 its TYPE_CANONICAL (T) will be NULL.
1585 * TYPE_CANONICAL (T) desn't carry type attributes
1586 and loses template parameter names.
1587
1588 If REMOVE_ATTRIBUTES is non-null, also strip attributes that don't
1589 affect type identity, and set the referent to true if any were
1590 stripped. */
1591
1592tree
1593strip_typedefs (tree t, bool *remove_attributes /* = NULL */,
1594 unsigned int flags /* = 0 */)
1595{
1596 tree result = NULL, type = NULL, t0 = NULL;
1597
1598 if (!t || t == error_mark_node)
1599 return t;
1600
1601 if (!TYPE_P (t))
1602 return strip_typedefs_expr (t, remove_attributes, flags);
1603
1604 if (t == TYPE_CANONICAL (t))
1605 return t;
1606
1607 if (!(flags & STF_STRIP_DEPENDENT)
1608 && dependent_alias_template_spec_p (t, nt_opaque))
1609 /* DR 1558: However, if the template-id is dependent, subsequent
1610 template argument substitution still applies to the template-id. */
1611 return t;
1612
1613 switch (TREE_CODE (t))
1614 {
1615 case POINTER_TYPE:
1616 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1617 result = build_pointer_type_for_mode (type, TYPE_MODE (t), false);
1618 break;
1619 case REFERENCE_TYPE:
1620 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1621 result = cp_build_reference_type_for_mode (to_type: type, TYPE_MODE (t), TYPE_REF_IS_RVALUE (t));
1622 break;
1623 case OFFSET_TYPE:
1624 t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t), remove_attributes, flags);
1625 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1626 result = build_offset_type (t0, type);
1627 break;
1628 case RECORD_TYPE:
1629 if (TYPE_PTRMEMFUNC_P (t))
1630 {
1631 t0 = strip_typedefs (TYPE_PTRMEMFUNC_FN_TYPE (t),
1632 remove_attributes, flags);
1633 result = build_ptrmemfunc_type (t0);
1634 }
1635 break;
1636 case ARRAY_TYPE:
1637 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1638 t0 = strip_typedefs (TYPE_DOMAIN (t), remove_attributes, flags);
1639 gcc_checking_assert (TYPE_DEPENDENT_P_VALID (t)
1640 || !dependent_type_p (t));
1641 result = build_cplus_array_type (elt_type: type, index_type: t0, TYPE_DEPENDENT_P (t));
1642 break;
1643 case FUNCTION_TYPE:
1644 case METHOD_TYPE:
1645 {
1646 tree arg_types = NULL, arg_node, arg_node2, arg_type;
1647 bool changed;
1648
1649 /* Because we stomp on TREE_PURPOSE of TYPE_ARG_TYPES in many places
1650 around the compiler (e.g. cp_parser_late_parsing_default_args), we
1651 can't expect that re-hashing a function type will find a previous
1652 equivalent type, so try to reuse the input type if nothing has
1653 changed. If the type is itself a variant, that will change. */
1654 bool is_variant = typedef_variant_p (type: t);
1655 if (remove_attributes
1656 && (TYPE_ATTRIBUTES (t) || TYPE_USER_ALIGN (t)))
1657 is_variant = true;
1658
1659 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1660 tree canon_spec = (flag_noexcept_type
1661 ? canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (t))
1662 : NULL_TREE);
1663 changed = (type != TREE_TYPE (t) || is_variant
1664 || TYPE_RAISES_EXCEPTIONS (t) != canon_spec);
1665
1666 for (arg_node = TYPE_ARG_TYPES (t);
1667 arg_node;
1668 arg_node = TREE_CHAIN (arg_node))
1669 {
1670 if (arg_node == void_list_node)
1671 break;
1672 arg_type = strip_typedefs (TREE_VALUE (arg_node),
1673 remove_attributes, flags);
1674 gcc_assert (arg_type);
1675 if (arg_type == TREE_VALUE (arg_node) && !changed)
1676 continue;
1677
1678 if (!changed)
1679 {
1680 changed = true;
1681 for (arg_node2 = TYPE_ARG_TYPES (t);
1682 arg_node2 != arg_node;
1683 arg_node2 = TREE_CHAIN (arg_node2))
1684 arg_types
1685 = tree_cons (TREE_PURPOSE (arg_node2),
1686 TREE_VALUE (arg_node2), arg_types);
1687 }
1688
1689 arg_types
1690 = tree_cons (TREE_PURPOSE (arg_node), arg_type, arg_types);
1691 }
1692
1693 if (!changed)
1694 return t;
1695
1696 if (arg_types)
1697 arg_types = nreverse (arg_types);
1698
1699 /* A list of parameters not ending with an ellipsis
1700 must end with void_list_node. */
1701 if (arg_node)
1702 arg_types = chainon (arg_types, void_list_node);
1703
1704 if (TREE_CODE (t) == METHOD_TYPE)
1705 {
1706 tree class_type = TREE_TYPE (TREE_VALUE (arg_types));
1707 gcc_assert (class_type);
1708 result =
1709 build_method_type_directly (class_type, type,
1710 TREE_CHAIN (arg_types));
1711 }
1712 else
1713 {
1714 result = build_function_type (type, arg_types);
1715 result = apply_memfn_quals (result, type_memfn_quals (t));
1716 }
1717
1718 result = build_cp_fntype_variant (result,
1719 type_memfn_rqual (t), canon_spec,
1720 TYPE_HAS_LATE_RETURN_TYPE (t));
1721 }
1722 break;
1723 case TYPENAME_TYPE:
1724 {
1725 bool changed = false;
1726 tree fullname = TYPENAME_TYPE_FULLNAME (t);
1727 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
1728 && TREE_OPERAND (fullname, 1))
1729 {
1730 tree args = TREE_OPERAND (fullname, 1);
1731 tree new_args = copy_node (args);
1732 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
1733 {
1734 tree arg = TREE_VEC_ELT (args, i);
1735 tree strip_arg = strip_typedefs (t: arg, remove_attributes, flags);
1736 TREE_VEC_ELT (new_args, i) = strip_arg;
1737 if (strip_arg != arg)
1738 changed = true;
1739 }
1740 if (changed)
1741 {
1742 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_args)
1743 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
1744 fullname
1745 = lookup_template_function (TREE_OPERAND (fullname, 0),
1746 new_args);
1747 }
1748 else
1749 ggc_free (new_args);
1750 }
1751 tree ctx = strip_typedefs (TYPE_CONTEXT (t), remove_attributes, flags);
1752 if (!changed && ctx == TYPE_CONTEXT (t) && !typedef_variant_p (type: t))
1753 return t;
1754 tree name = fullname;
1755 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
1756 name = TREE_OPERAND (fullname, 0);
1757 /* Use build_typename_type rather than make_typename_type because we
1758 don't want to resolve it here, just strip typedefs. */
1759 result = build_typename_type (ctx, name, fullname, typename_type);
1760 }
1761 break;
1762 case DECLTYPE_TYPE:
1763 result = strip_typedefs_expr (DECLTYPE_TYPE_EXPR (t),
1764 remove_attributes, flags);
1765 if (result == DECLTYPE_TYPE_EXPR (t))
1766 result = NULL_TREE;
1767 else
1768 result = (finish_decltype_type
1769 (result,
1770 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t),
1771 tf_none));
1772 break;
1773 case TRAIT_TYPE:
1774 {
1775 tree type1 = strip_typedefs (TRAIT_TYPE_TYPE1 (t),
1776 remove_attributes, flags);
1777 tree type2 = strip_typedefs (TRAIT_TYPE_TYPE2 (t),
1778 remove_attributes, flags);
1779 if (type1 == TRAIT_TYPE_TYPE1 (t) && type2 == TRAIT_TYPE_TYPE2 (t))
1780 result = NULL_TREE;
1781 else
1782 result = finish_trait_type (TRAIT_TYPE_KIND (t), type1, type2,
1783 tf_warning_or_error);
1784 }
1785 break;
1786 case TYPE_PACK_EXPANSION:
1787 {
1788 tree pat = PACK_EXPANSION_PATTERN (t);
1789 if (TYPE_P (pat))
1790 {
1791 type = strip_typedefs (t: pat, remove_attributes, flags);
1792 if (type != pat)
1793 {
1794 result = build_distinct_type_copy (t);
1795 PACK_EXPANSION_PATTERN (result) = type;
1796 }
1797 }
1798 }
1799 break;
1800 default:
1801 break;
1802 }
1803
1804 if (!result)
1805 {
1806 if (typedef_variant_p (type: t))
1807 {
1808 if ((flags & STF_USER_VISIBLE)
1809 && !user_facing_original_type_p (t))
1810 return t;
1811 /* If T is a non-template alias or typedef, we can assume that
1812 instantiating its definition will hit any substitution failure,
1813 so we don't need to retain it here as well. */
1814 if (!alias_template_specialization_p (t, nt_opaque))
1815 flags |= STF_STRIP_DEPENDENT;
1816 result = strip_typedefs (DECL_ORIGINAL_TYPE (TYPE_NAME (t)),
1817 remove_attributes, flags);
1818 }
1819 else
1820 result = TYPE_MAIN_VARIANT (t);
1821 }
1822 /*gcc_assert (!typedef_variant_p (result)
1823 || dependent_alias_template_spec_p (result, nt_opaque)
1824 || ((flags & STF_USER_VISIBLE)
1825 && !user_facing_original_type_p (result)));*/
1826
1827 if (COMPLETE_TYPE_P (result) && !COMPLETE_TYPE_P (t))
1828 /* If RESULT is complete and T isn't, it's likely the case that T
1829 is a variant of RESULT which hasn't been updated yet. Skip the
1830 attribute handling. */;
1831 else
1832 {
1833 if (TYPE_USER_ALIGN (t) != TYPE_USER_ALIGN (result)
1834 || TYPE_ALIGN (t) != TYPE_ALIGN (result))
1835 {
1836 gcc_assert (TYPE_USER_ALIGN (t));
1837 if (remove_attributes)
1838 *remove_attributes = true;
1839 else
1840 {
1841 if (TYPE_ALIGN (t) == TYPE_ALIGN (result))
1842 result = build_variant_type_copy (result);
1843 else
1844 result = build_aligned_type (result, TYPE_ALIGN (t));
1845 TYPE_USER_ALIGN (result) = true;
1846 }
1847 }
1848
1849 if (TYPE_ATTRIBUTES (t))
1850 {
1851 if (remove_attributes)
1852 result = apply_identity_attributes (result, TYPE_ATTRIBUTES (t),
1853 remove_attributes);
1854 else
1855 result = cp_build_type_attribute_variant (result,
1856 TYPE_ATTRIBUTES (t));
1857 }
1858 }
1859
1860 return cp_build_qualified_type (type: result, type_quals: cp_type_quals (t));
1861}
1862
1863/* Like strip_typedefs above, but works on expressions (and other
1864 non-types such as TREE_VEC), so that in
1865
1866 template<class T> struct A
1867 {
1868 typedef T TT;
1869 B<sizeof(TT)> b;
1870 };
1871
1872 sizeof(TT) is replaced by sizeof(T). */
1873
1874tree
1875strip_typedefs_expr (tree t, bool *remove_attributes, unsigned int flags)
1876{
1877 unsigned i,n;
1878 tree r, type, *ops;
1879 enum tree_code code;
1880
1881 if (t == NULL_TREE || t == error_mark_node)
1882 return t;
1883
1884 STRIP_ANY_LOCATION_WRAPPER (t);
1885
1886 if (DECL_P (t) || CONSTANT_CLASS_P (t))
1887 return t;
1888
1889 code = TREE_CODE (t);
1890 switch (code)
1891 {
1892 case IDENTIFIER_NODE:
1893 case TEMPLATE_PARM_INDEX:
1894 case OVERLOAD:
1895 case BASELINK:
1896 case ARGUMENT_PACK_SELECT:
1897 return t;
1898
1899 case TRAIT_EXPR:
1900 {
1901 tree type1 = strip_typedefs (TRAIT_EXPR_TYPE1 (t),
1902 remove_attributes, flags);
1903 tree type2 = strip_typedefs (TRAIT_EXPR_TYPE2 (t),
1904 remove_attributes, flags);
1905 if (type1 == TRAIT_EXPR_TYPE1 (t)
1906 && type2 == TRAIT_EXPR_TYPE2 (t))
1907 return t;
1908 r = copy_node (t);
1909 TRAIT_EXPR_TYPE1 (r) = type1;
1910 TRAIT_EXPR_TYPE2 (r) = type2;
1911 return r;
1912 }
1913
1914 case TREE_LIST:
1915 {
1916 bool changed = false;
1917 auto_vec<tree_pair, 4> vec;
1918 r = t;
1919 for (; t; t = TREE_CHAIN (t))
1920 {
1921 tree purpose = strip_typedefs (TREE_PURPOSE (t),
1922 remove_attributes, flags);
1923 tree value = strip_typedefs (TREE_VALUE (t),
1924 remove_attributes, flags);
1925 if (purpose != TREE_PURPOSE (t) || value != TREE_VALUE (t))
1926 changed = true;
1927 vec.safe_push (obj: {purpose, value});
1928 }
1929 if (changed)
1930 {
1931 r = NULL_TREE;
1932 for (int i = vec.length () - 1; i >= 0; i--)
1933 r = tree_cons (vec[i].first, vec[i].second, r);
1934 }
1935 return r;
1936 }
1937
1938 case TREE_VEC:
1939 {
1940 bool changed = false;
1941 releasing_vec vec;
1942 n = TREE_VEC_LENGTH (t);
1943 vec_safe_reserve (r&: vec, n);
1944 for (i = 0; i < n; ++i)
1945 {
1946 tree op = strip_typedefs (TREE_VEC_ELT (t, i),
1947 remove_attributes, flags);
1948 vec->quick_push (obj: op);
1949 if (op != TREE_VEC_ELT (t, i))
1950 changed = true;
1951 }
1952 if (changed)
1953 {
1954 r = copy_node (t);
1955 for (i = 0; i < n; ++i)
1956 TREE_VEC_ELT (r, i) = (*vec)[i];
1957 NON_DEFAULT_TEMPLATE_ARGS_COUNT (r)
1958 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
1959 }
1960 else
1961 r = t;
1962 return r;
1963 }
1964
1965 case CONSTRUCTOR:
1966 {
1967 bool changed = false;
1968 vec<constructor_elt, va_gc> *vec
1969 = vec_safe_copy (CONSTRUCTOR_ELTS (t));
1970 n = CONSTRUCTOR_NELTS (t);
1971 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1972 for (i = 0; i < n; ++i)
1973 {
1974 constructor_elt *e = &(*vec)[i];
1975 tree op = strip_typedefs (t: e->value, remove_attributes, flags);
1976 if (op != e->value)
1977 {
1978 changed = true;
1979 e->value = op;
1980 }
1981 gcc_checking_assert
1982 (e->index == strip_typedefs (e->index, remove_attributes,
1983 flags));
1984 }
1985
1986 if (!changed && type == TREE_TYPE (t))
1987 {
1988 vec_free (v&: vec);
1989 return t;
1990 }
1991 else
1992 {
1993 r = copy_node (t);
1994 TREE_TYPE (r) = type;
1995 CONSTRUCTOR_ELTS (r) = vec;
1996 return r;
1997 }
1998 }
1999
2000 case LAMBDA_EXPR:
2001 return t;
2002
2003 case STATEMENT_LIST:
2004 error ("statement-expression in a constant expression");
2005 return error_mark_node;
2006
2007 default:
2008 break;
2009 }
2010
2011 gcc_assert (EXPR_P (t));
2012
2013 n = cp_tree_operand_length (t);
2014 ops = XALLOCAVEC (tree, n);
2015 type = TREE_TYPE (t);
2016
2017 switch (code)
2018 {
2019 CASE_CONVERT:
2020 case IMPLICIT_CONV_EXPR:
2021 case DYNAMIC_CAST_EXPR:
2022 case STATIC_CAST_EXPR:
2023 case CONST_CAST_EXPR:
2024 case REINTERPRET_CAST_EXPR:
2025 case CAST_EXPR:
2026 case NEW_EXPR:
2027 type = strip_typedefs (t: type, remove_attributes, flags);
2028 /* fallthrough */
2029
2030 default:
2031 for (i = 0; i < n; ++i)
2032 ops[i] = strip_typedefs (TREE_OPERAND (t, i),
2033 remove_attributes, flags);
2034 break;
2035 }
2036
2037 /* If nothing changed, return t. */
2038 for (i = 0; i < n; ++i)
2039 if (ops[i] != TREE_OPERAND (t, i))
2040 break;
2041 if (i == n && type == TREE_TYPE (t))
2042 return t;
2043
2044 r = copy_node (t);
2045 TREE_TYPE (r) = type;
2046 for (i = 0; i < n; ++i)
2047 TREE_OPERAND (r, i) = ops[i];
2048 return r;
2049}
2050
2051/* Makes a copy of BINFO and TYPE, which is to be inherited into a
2052 graph dominated by T. If BINFO is NULL, TYPE is a dependent base,
2053 and we do a shallow copy. If BINFO is non-NULL, we do a deep copy.
2054 VIRT indicates whether TYPE is inherited virtually or not.
2055 IGO_PREV points at the previous binfo of the inheritance graph
2056 order chain. The newly copied binfo's TREE_CHAIN forms this
2057 ordering.
2058
2059 The CLASSTYPE_VBASECLASSES vector of T is constructed in the
2060 correct order. That is in the order the bases themselves should be
2061 constructed in.
2062
2063 The BINFO_INHERITANCE of a virtual base class points to the binfo
2064 of the most derived type. ??? We could probably change this so that
2065 BINFO_INHERITANCE becomes synonymous with BINFO_PRIMARY, and hence
2066 remove a field. They currently can only differ for primary virtual
2067 virtual bases. */
2068
2069tree
2070copy_binfo (tree binfo, tree type, tree t, tree *igo_prev, int virt)
2071{
2072 tree new_binfo;
2073
2074 if (virt)
2075 {
2076 /* See if we've already made this virtual base. */
2077 new_binfo = binfo_for_vbase (type, t);
2078 if (new_binfo)
2079 return new_binfo;
2080 }
2081
2082 new_binfo = make_tree_binfo (binfo ? BINFO_N_BASE_BINFOS (binfo) : 0);
2083 BINFO_TYPE (new_binfo) = type;
2084
2085 /* Chain it into the inheritance graph. */
2086 TREE_CHAIN (*igo_prev) = new_binfo;
2087 *igo_prev = new_binfo;
2088
2089 if (binfo && !BINFO_DEPENDENT_BASE_P (binfo))
2090 {
2091 int ix;
2092 tree base_binfo;
2093
2094 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), type));
2095
2096 BINFO_OFFSET (new_binfo) = BINFO_OFFSET (binfo);
2097 BINFO_VIRTUALS (new_binfo) = BINFO_VIRTUALS (binfo);
2098
2099 /* We do not need to copy the accesses, as they are read only. */
2100 BINFO_BASE_ACCESSES (new_binfo) = BINFO_BASE_ACCESSES (binfo);
2101
2102 /* Recursively copy base binfos of BINFO. */
2103 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2104 {
2105 tree new_base_binfo;
2106 new_base_binfo = copy_binfo (binfo: base_binfo, BINFO_TYPE (base_binfo),
2107 t, igo_prev,
2108 BINFO_VIRTUAL_P (base_binfo));
2109
2110 if (!BINFO_INHERITANCE_CHAIN (new_base_binfo))
2111 BINFO_INHERITANCE_CHAIN (new_base_binfo) = new_binfo;
2112 BINFO_BASE_APPEND (new_binfo, new_base_binfo);
2113 }
2114 }
2115 else
2116 BINFO_DEPENDENT_BASE_P (new_binfo) = 1;
2117
2118 if (virt)
2119 {
2120 /* Push it onto the list after any virtual bases it contains
2121 will have been pushed. */
2122 CLASSTYPE_VBASECLASSES (t)->quick_push (obj: new_binfo);
2123 BINFO_VIRTUAL_P (new_binfo) = 1;
2124 BINFO_INHERITANCE_CHAIN (new_binfo) = TYPE_BINFO (t);
2125 }
2126
2127 return new_binfo;
2128}
2129
2130/* Hashing of lists so that we don't make duplicates.
2131 The entry point is `list_hash_canon'. */
2132
2133struct list_proxy
2134{
2135 tree purpose;
2136 tree value;
2137 tree chain;
2138};
2139
2140struct list_hasher : ggc_ptr_hash<tree_node>
2141{
2142 typedef list_proxy *compare_type;
2143
2144 static hashval_t hash (tree);
2145 static bool equal (tree, list_proxy *);
2146};
2147
2148/* Now here is the hash table. When recording a list, it is added
2149 to the slot whose index is the hash code mod the table size.
2150 Note that the hash table is used for several kinds of lists.
2151 While all these live in the same table, they are completely independent,
2152 and the hash code is computed differently for each of these. */
2153
2154static GTY (()) hash_table<list_hasher> *list_hash_table;
2155
2156/* Compare ENTRY (an entry in the hash table) with DATA (a list_proxy
2157 for a node we are thinking about adding). */
2158
2159bool
2160list_hasher::equal (tree t, list_proxy *proxy)
2161{
2162 return (TREE_VALUE (t) == proxy->value
2163 && TREE_PURPOSE (t) == proxy->purpose
2164 && TREE_CHAIN (t) == proxy->chain);
2165}
2166
2167/* Compute a hash code for a list (chain of TREE_LIST nodes
2168 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
2169 TREE_COMMON slots), by adding the hash codes of the individual entries. */
2170
2171static hashval_t
2172list_hash_pieces (tree purpose, tree value, tree chain)
2173{
2174 hashval_t hashcode = 0;
2175
2176 if (chain)
2177 hashcode += TREE_HASH (chain);
2178
2179 if (value)
2180 hashcode += TREE_HASH (value);
2181 else
2182 hashcode += 1007;
2183 if (purpose)
2184 hashcode += TREE_HASH (purpose);
2185 else
2186 hashcode += 1009;
2187 return hashcode;
2188}
2189
2190/* Hash an already existing TREE_LIST. */
2191
2192hashval_t
2193list_hasher::hash (tree t)
2194{
2195 return list_hash_pieces (TREE_PURPOSE (t),
2196 TREE_VALUE (t),
2197 TREE_CHAIN (t));
2198}
2199
2200/* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
2201 object for an identical list if one already exists. Otherwise, build a
2202 new one, and record it as the canonical object. */
2203
2204tree
2205hash_tree_cons (tree purpose, tree value, tree chain)
2206{
2207 int hashcode = 0;
2208 tree *slot;
2209 struct list_proxy proxy;
2210
2211 /* Hash the list node. */
2212 hashcode = list_hash_pieces (purpose, value, chain);
2213 /* Create a proxy for the TREE_LIST we would like to create. We
2214 don't actually create it so as to avoid creating garbage. */
2215 proxy.purpose = purpose;
2216 proxy.value = value;
2217 proxy.chain = chain;
2218 /* See if it is already in the table. */
2219 slot = list_hash_table->find_slot_with_hash (comparable: &proxy, hash: hashcode, insert: INSERT);
2220 /* If not, create a new node. */
2221 if (!*slot)
2222 *slot = tree_cons (purpose, value, chain);
2223 return (tree) *slot;
2224}
2225
2226/* Constructor for hashed lists. */
2227
2228tree
2229hash_tree_chain (tree value, tree chain)
2230{
2231 return hash_tree_cons (NULL_TREE, value, chain);
2232}
2233
2234void
2235debug_binfo (tree elem)
2236{
2237 HOST_WIDE_INT n;
2238 tree virtuals;
2239
2240 fprintf (stderr, format: "type \"%s\", offset = " HOST_WIDE_INT_PRINT_DEC
2241 "\nvtable type:\n",
2242 TYPE_NAME_STRING (BINFO_TYPE (elem)),
2243 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
2244 debug_tree (BINFO_TYPE (elem));
2245 if (BINFO_VTABLE (elem))
2246 fprintf (stderr, format: "vtable decl \"%s\"\n",
2247 IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem))));
2248 else
2249 fprintf (stderr, format: "no vtable decl yet\n");
2250 fprintf (stderr, format: "virtuals:\n");
2251 virtuals = BINFO_VIRTUALS (elem);
2252 n = 0;
2253
2254 while (virtuals)
2255 {
2256 tree fndecl = TREE_VALUE (virtuals);
2257 fprintf (stderr, format: "%s [" HOST_WIDE_INT_PRINT_DEC " =? "
2258 HOST_WIDE_INT_PRINT_DEC "]\n",
2259 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
2260 n, TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
2261 ++n;
2262 virtuals = TREE_CHAIN (virtuals);
2263 }
2264}
2265
2266/* Build a representation for the qualified name SCOPE::NAME. TYPE is
2267 the type of the result expression, if known, or NULL_TREE if the
2268 resulting expression is type-dependent. If TEMPLATE_P is true,
2269 NAME is known to be a template because the user explicitly used the
2270 "template" keyword after the "::".
2271
2272 All SCOPE_REFs should be built by use of this function. */
2273
2274tree
2275build_qualified_name (tree type, tree scope, tree name, bool template_p)
2276{
2277 tree t;
2278 if (type == error_mark_node
2279 || scope == error_mark_node
2280 || name == error_mark_node)
2281 return error_mark_node;
2282 gcc_assert (TREE_CODE (name) != SCOPE_REF);
2283 t = build2 (SCOPE_REF, type, scope, name);
2284 QUALIFIED_NAME_IS_TEMPLATE (t) = template_p;
2285 PTRMEM_OK_P (t) = true;
2286 if (type)
2287 t = convert_from_reference (t);
2288 return t;
2289}
2290
2291/* Like check_qualified_type, but also check ref-qualifier, exception
2292 specification, and whether the return type was specified after the
2293 parameters. */
2294
2295static bool
2296cp_check_qualified_type (const_tree cand, const_tree base, int type_quals,
2297 cp_ref_qualifier rqual, tree raises, bool late)
2298{
2299 return (TYPE_QUALS (cand) == type_quals
2300 && check_base_type (cand, base)
2301 && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (cand),
2302 ce_exact)
2303 && TYPE_HAS_LATE_RETURN_TYPE (cand) == late
2304 && type_memfn_rqual (cand) == rqual);
2305}
2306
2307/* Build the FUNCTION_TYPE or METHOD_TYPE with the ref-qualifier RQUAL. */
2308
2309tree
2310build_ref_qualified_type (tree type, cp_ref_qualifier rqual)
2311{
2312 tree raises = TYPE_RAISES_EXCEPTIONS (type);
2313 bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
2314 return build_cp_fntype_variant (type, rqual, raises, late);
2315}
2316
2317tree
2318make_binding_vec (tree name, unsigned clusters MEM_STAT_DECL)
2319{
2320 /* Stored in an unsigned short, but we're limited to the number of
2321 modules anyway. */
2322 gcc_checking_assert (clusters <= (unsigned short)(~0));
2323 size_t length = (offsetof (tree_binding_vec, vec)
2324 + clusters * sizeof (binding_cluster));
2325 tree vec = ggc_alloc_cleared_tree_node_stat (s: length PASS_MEM_STAT);
2326 TREE_SET_CODE (vec, BINDING_VECTOR);
2327 BINDING_VECTOR_NAME (vec) = name;
2328 BINDING_VECTOR_ALLOC_CLUSTERS (vec) = clusters;
2329 BINDING_VECTOR_NUM_CLUSTERS (vec) = 0;
2330
2331 return vec;
2332}
2333
2334/* Make a raw overload node containing FN. */
2335
2336tree
2337ovl_make (tree fn, tree next)
2338{
2339 tree result = make_node (OVERLOAD);
2340
2341 if (TREE_CODE (fn) == OVERLOAD)
2342 OVL_NESTED_P (result) = true;
2343
2344 TREE_TYPE (result) = (next || TREE_CODE (fn) == TEMPLATE_DECL
2345 ? unknown_type_node : TREE_TYPE (fn));
2346 if (next && TREE_CODE (next) == OVERLOAD && OVL_DEDUP_P (next))
2347 OVL_DEDUP_P (result) = true;
2348 OVL_FUNCTION (result) = fn;
2349 OVL_CHAIN (result) = next;
2350 return result;
2351}
2352
2353/* Add FN to the (potentially NULL) overload set OVL. USING_OR_HIDDEN is >
2354 zero if this is a using-decl. It is > 1 if we're exporting the
2355 using decl. USING_OR_HIDDEN is < 0, if FN is hidden. (A decl
2356 cannot be both using and hidden.) We keep the hidden decls first,
2357 but remaining ones are unordered. */
2358
2359tree
2360ovl_insert (tree fn, tree maybe_ovl, int using_or_hidden)
2361{
2362 tree result = maybe_ovl;
2363 tree insert_after = NULL_TREE;
2364
2365 /* Skip hidden. */
2366 for (; maybe_ovl && TREE_CODE (maybe_ovl) == OVERLOAD
2367 && OVL_HIDDEN_P (maybe_ovl);
2368 maybe_ovl = OVL_CHAIN (maybe_ovl))
2369 {
2370 gcc_checking_assert (!OVL_LOOKUP_P (maybe_ovl));
2371 insert_after = maybe_ovl;
2372 }
2373
2374 if (maybe_ovl || using_or_hidden || TREE_CODE (fn) == TEMPLATE_DECL)
2375 {
2376 maybe_ovl = ovl_make (fn, next: maybe_ovl);
2377
2378 if (using_or_hidden < 0)
2379 OVL_HIDDEN_P (maybe_ovl) = true;
2380 if (using_or_hidden > 0)
2381 {
2382 OVL_DEDUP_P (maybe_ovl) = OVL_USING_P (maybe_ovl) = true;
2383 if (using_or_hidden > 1)
2384 OVL_EXPORT_P (maybe_ovl) = true;
2385 }
2386 }
2387 else
2388 maybe_ovl = fn;
2389
2390 if (insert_after)
2391 {
2392 OVL_CHAIN (insert_after) = maybe_ovl;
2393 TREE_TYPE (insert_after) = unknown_type_node;
2394 }
2395 else
2396 result = maybe_ovl;
2397
2398 return result;
2399}
2400
2401/* Skip any hidden names at the beginning of OVL. */
2402
2403tree
2404ovl_skip_hidden (tree ovl)
2405{
2406 while (ovl && TREE_CODE (ovl) == OVERLOAD && OVL_HIDDEN_P (ovl))
2407 ovl = OVL_CHAIN (ovl);
2408
2409 return ovl;
2410}
2411
2412/* NODE is an OVL_HIDDEN_P node that is now revealed. */
2413
2414tree
2415ovl_iterator::reveal_node (tree overload, tree node)
2416{
2417 /* We cannot have returned NODE as part of a lookup overload, so we
2418 don't have to worry about preserving that. */
2419
2420 OVL_HIDDEN_P (node) = false;
2421 if (tree chain = OVL_CHAIN (node))
2422 if (TREE_CODE (chain) == OVERLOAD)
2423 {
2424 if (OVL_HIDDEN_P (chain))
2425 {
2426 /* The node needs moving, and the simplest way is to remove it
2427 and reinsert. */
2428 overload = remove_node (head: overload, node);
2429 overload = ovl_insert (OVL_FUNCTION (node), maybe_ovl: overload);
2430 }
2431 else if (OVL_DEDUP_P (chain))
2432 OVL_DEDUP_P (node) = true;
2433 }
2434 return overload;
2435}
2436
2437/* NODE is on the overloads of OVL. Remove it.
2438 The removed node is unaltered and may continue to be iterated
2439 from (i.e. it is safe to remove a node from an overload one is
2440 currently iterating over). */
2441
2442tree
2443ovl_iterator::remove_node (tree overload, tree node)
2444{
2445 tree *slot = &overload;
2446 while (*slot != node)
2447 {
2448 tree probe = *slot;
2449 gcc_checking_assert (!OVL_LOOKUP_P (probe));
2450
2451 slot = &OVL_CHAIN (probe);
2452 }
2453
2454 /* Stitch out NODE. We don't have to worry about now making a
2455 singleton overload (and consequently maybe setting its type),
2456 because all uses of this function will be followed by inserting a
2457 new node that must follow the place we've cut this out from. */
2458 if (TREE_CODE (node) != OVERLOAD)
2459 /* Cloned inherited ctors don't mark themselves as via_using. */
2460 *slot = NULL_TREE;
2461 else
2462 *slot = OVL_CHAIN (node);
2463
2464 return overload;
2465}
2466
2467/* Mark or unmark a lookup set. */
2468
2469void
2470lookup_mark (tree ovl, bool val)
2471{
2472 for (lkp_iterator iter (ovl); iter; ++iter)
2473 {
2474 gcc_checking_assert (LOOKUP_SEEN_P (*iter) != val);
2475 LOOKUP_SEEN_P (*iter) = val;
2476 }
2477}
2478
2479/* Add a set of new FNS into a lookup. */
2480
2481tree
2482lookup_add (tree fns, tree lookup)
2483{
2484 if (fns == error_mark_node || lookup == error_mark_node)
2485 return error_mark_node;
2486
2487 if (lookup || TREE_CODE (fns) == TEMPLATE_DECL)
2488 {
2489 lookup = ovl_make (fn: fns, next: lookup);
2490 OVL_LOOKUP_P (lookup) = true;
2491 }
2492 else
2493 lookup = fns;
2494
2495 return lookup;
2496}
2497
2498/* FNS is a new overload set, add them to LOOKUP, if they are not
2499 already present there. */
2500
2501tree
2502lookup_maybe_add (tree fns, tree lookup, bool deduping)
2503{
2504 if (deduping)
2505 for (tree next, probe = fns; probe; probe = next)
2506 {
2507 tree fn = probe;
2508 next = NULL_TREE;
2509
2510 if (TREE_CODE (probe) == OVERLOAD)
2511 {
2512 fn = OVL_FUNCTION (probe);
2513 next = OVL_CHAIN (probe);
2514 }
2515
2516 if (!LOOKUP_SEEN_P (fn))
2517 LOOKUP_SEEN_P (fn) = true;
2518 else
2519 {
2520 /* This function was already seen. Insert all the
2521 predecessors onto the lookup. */
2522 for (; fns != probe; fns = OVL_CHAIN (fns))
2523 {
2524 lookup = lookup_add (OVL_FUNCTION (fns), lookup);
2525 /* Propagate OVL_USING, but OVL_HIDDEN &
2526 OVL_DEDUP_P don't matter. */
2527 if (OVL_USING_P (fns))
2528 OVL_USING_P (lookup) = true;
2529 }
2530
2531 /* And now skip this function. */
2532 fns = next;
2533 }
2534 }
2535
2536 if (fns)
2537 /* We ended in a set of new functions. Add them all in one go. */
2538 lookup = lookup_add (fns, lookup);
2539
2540 return lookup;
2541}
2542
2543/* Returns nonzero if X is an expression for a (possibly overloaded)
2544 function. If "f" is a function or function template, "f", "c->f",
2545 "c.f", "C::f", and "f<int>" will all be considered possibly
2546 overloaded functions. Returns 2 if the function is actually
2547 overloaded, i.e., if it is impossible to know the type of the
2548 function without performing overload resolution. */
2549
2550int
2551is_overloaded_fn (tree x)
2552{
2553 STRIP_ANY_LOCATION_WRAPPER (x);
2554
2555 /* A baselink is also considered an overloaded function. */
2556 if (TREE_CODE (x) == OFFSET_REF
2557 || TREE_CODE (x) == COMPONENT_REF)
2558 x = TREE_OPERAND (x, 1);
2559 x = MAYBE_BASELINK_FUNCTIONS (x);
2560 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
2561 x = TREE_OPERAND (x, 0);
2562
2563 if (DECL_FUNCTION_TEMPLATE_P (OVL_FIRST (x))
2564 || (TREE_CODE (x) == OVERLOAD && !OVL_SINGLE_P (x)))
2565 return 2;
2566
2567 return OVL_P (x);
2568}
2569
2570/* X is the CALL_EXPR_FN of a CALL_EXPR. If X represents a dependent name
2571 (14.6.2), return the IDENTIFIER_NODE for that name. Otherwise, return
2572 NULL_TREE. */
2573
2574tree
2575dependent_name (tree x)
2576{
2577 /* FIXME a dependent name must be unqualified, but this function doesn't
2578 distinguish between qualified and unqualified identifiers. */
2579 if (identifier_p (t: x))
2580 return x;
2581 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
2582 x = TREE_OPERAND (x, 0);
2583 if (OVL_P (x))
2584 return OVL_NAME (x);
2585 return NULL_TREE;
2586}
2587
2588/* Like dependent_name, but instead takes a CALL_EXPR and also checks
2589 its dependence. */
2590
2591tree
2592call_expr_dependent_name (tree x)
2593{
2594 if (TREE_TYPE (x) != NULL_TREE)
2595 /* X isn't dependent, so its callee isn't a dependent name. */
2596 return NULL_TREE;
2597 return dependent_name (CALL_EXPR_FN (x));
2598}
2599
2600/* Returns true iff X is an expression for an overloaded function
2601 whose type cannot be known without performing overload
2602 resolution. */
2603
2604bool
2605really_overloaded_fn (tree x)
2606{
2607 return is_overloaded_fn (x) == 2;
2608}
2609
2610/* Get the overload set FROM refers to. Returns NULL if it's not an
2611 overload set. */
2612
2613tree
2614maybe_get_fns (tree from)
2615{
2616 STRIP_ANY_LOCATION_WRAPPER (from);
2617
2618 /* A baselink is also considered an overloaded function. */
2619 if (TREE_CODE (from) == OFFSET_REF
2620 || TREE_CODE (from) == COMPONENT_REF)
2621 from = TREE_OPERAND (from, 1);
2622 if (BASELINK_P (from))
2623 from = BASELINK_FUNCTIONS (from);
2624 if (TREE_CODE (from) == TEMPLATE_ID_EXPR)
2625 from = TREE_OPERAND (from, 0);
2626
2627 if (OVL_P (from))
2628 return from;
2629
2630 return NULL;
2631}
2632
2633/* FROM refers to an overload set. Return that set (or die). */
2634
2635tree
2636get_fns (tree from)
2637{
2638 tree res = maybe_get_fns (from);
2639
2640 gcc_assert (res);
2641 return res;
2642}
2643
2644/* Return the first function of the overload set FROM refers to. */
2645
2646tree
2647get_first_fn (tree from)
2648{
2649 return OVL_FIRST (get_fns (from));
2650}
2651
2652/* Return the scope where the overloaded functions OVL were found. */
2653
2654tree
2655ovl_scope (tree ovl)
2656{
2657 if (TREE_CODE (ovl) == OFFSET_REF
2658 || TREE_CODE (ovl) == COMPONENT_REF)
2659 ovl = TREE_OPERAND (ovl, 1);
2660 if (TREE_CODE (ovl) == BASELINK)
2661 return BINFO_TYPE (BASELINK_BINFO (ovl));
2662 if (TREE_CODE (ovl) == TEMPLATE_ID_EXPR)
2663 ovl = TREE_OPERAND (ovl, 0);
2664 /* Skip using-declarations. */
2665 lkp_iterator iter (ovl);
2666 do
2667 ovl = *iter;
2668 while (iter.using_p () && ++iter);
2669
2670 return CP_DECL_CONTEXT (ovl);
2671}
2672
2673#define PRINT_RING_SIZE 4
2674
2675static const char *
2676cxx_printable_name_internal (tree decl, int v, bool translate)
2677{
2678 static unsigned int uid_ring[PRINT_RING_SIZE];
2679 static char *print_ring[PRINT_RING_SIZE];
2680 static bool trans_ring[PRINT_RING_SIZE];
2681 static int ring_counter;
2682 int i;
2683
2684 /* Only cache functions. */
2685 if (v < 2
2686 || TREE_CODE (decl) != FUNCTION_DECL
2687 || DECL_LANG_SPECIFIC (decl) == 0)
2688 return lang_decl_name (decl, v, translate);
2689
2690 /* See if this print name is lying around. */
2691 for (i = 0; i < PRINT_RING_SIZE; i++)
2692 if (uid_ring[i] == DECL_UID (decl) && translate == trans_ring[i])
2693 /* yes, so return it. */
2694 return print_ring[i];
2695
2696 if (++ring_counter == PRINT_RING_SIZE)
2697 ring_counter = 0;
2698
2699 if (current_function_decl != NULL_TREE)
2700 {
2701 /* There may be both translated and untranslated versions of the
2702 name cached. */
2703 for (i = 0; i < 2; i++)
2704 {
2705 if (uid_ring[ring_counter] == DECL_UID (current_function_decl))
2706 ring_counter += 1;
2707 if (ring_counter == PRINT_RING_SIZE)
2708 ring_counter = 0;
2709 }
2710 gcc_assert (uid_ring[ring_counter] != DECL_UID (current_function_decl));
2711 }
2712
2713 free (ptr: print_ring[ring_counter]);
2714
2715 print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v, translate));
2716 uid_ring[ring_counter] = DECL_UID (decl);
2717 trans_ring[ring_counter] = translate;
2718 return print_ring[ring_counter];
2719}
2720
2721const char *
2722cxx_printable_name (tree decl, int v)
2723{
2724 return cxx_printable_name_internal (decl, v, translate: false);
2725}
2726
2727const char *
2728cxx_printable_name_translate (tree decl, int v)
2729{
2730 return cxx_printable_name_internal (decl, v, translate: true);
2731}
2732
2733/* Return the canonical version of exception-specification RAISES for a C++17
2734 function type, for use in type comparison and building TYPE_CANONICAL. */
2735
2736tree
2737canonical_eh_spec (tree raises)
2738{
2739 if (raises == NULL_TREE)
2740 return raises;
2741 else if (DEFERRED_NOEXCEPT_SPEC_P (raises)
2742 || UNPARSED_NOEXCEPT_SPEC_P (raises)
2743 || uses_template_parms (raises)
2744 || uses_template_parms (TREE_PURPOSE (raises)))
2745 /* Keep a dependent or deferred exception specification. */
2746 return raises;
2747 else if (nothrow_spec_p (raises))
2748 /* throw() -> noexcept. */
2749 return noexcept_true_spec;
2750 else
2751 /* For C++17 type matching, anything else -> nothing. */
2752 return NULL_TREE;
2753}
2754
2755tree
2756build_cp_fntype_variant (tree type, cp_ref_qualifier rqual,
2757 tree raises, bool late)
2758{
2759 cp_cv_quals type_quals = TYPE_QUALS (type);
2760
2761 if (cp_check_qualified_type (cand: type, base: type, type_quals, rqual, raises, late))
2762 return type;
2763
2764 tree v = TYPE_MAIN_VARIANT (type);
2765 for (; v; v = TYPE_NEXT_VARIANT (v))
2766 if (cp_check_qualified_type (cand: v, base: type, type_quals, rqual, raises, late))
2767 return v;
2768
2769 /* Need to build a new variant. */
2770 v = build_variant_type_copy (type);
2771 if (!TYPE_DEPENDENT_P (v))
2772 /* We no longer know that it's not type-dependent. */
2773 TYPE_DEPENDENT_P_VALID (v) = false;
2774 TYPE_RAISES_EXCEPTIONS (v) = raises;
2775 TYPE_HAS_LATE_RETURN_TYPE (v) = late;
2776 switch (rqual)
2777 {
2778 case REF_QUAL_RVALUE:
2779 FUNCTION_RVALUE_QUALIFIED (v) = 1;
2780 FUNCTION_REF_QUALIFIED (v) = 1;
2781 break;
2782 case REF_QUAL_LVALUE:
2783 FUNCTION_RVALUE_QUALIFIED (v) = 0;
2784 FUNCTION_REF_QUALIFIED (v) = 1;
2785 break;
2786 default:
2787 FUNCTION_REF_QUALIFIED (v) = 0;
2788 break;
2789 }
2790
2791 /* Canonicalize the exception specification. */
2792 tree cr = flag_noexcept_type ? canonical_eh_spec (raises) : NULL_TREE;
2793
2794 if (TYPE_STRUCTURAL_EQUALITY_P (type))
2795 /* Propagate structural equality. */
2796 SET_TYPE_STRUCTURAL_EQUALITY (v);
2797 else if (TYPE_CANONICAL (type) != type || cr != raises || late)
2798 /* Build the underlying canonical type, since it is different
2799 from TYPE. */
2800 TYPE_CANONICAL (v) = build_cp_fntype_variant (TYPE_CANONICAL (type),
2801 rqual, raises: cr, late: false);
2802 else
2803 /* T is its own canonical type. */
2804 TYPE_CANONICAL (v) = v;
2805
2806 return v;
2807}
2808
2809/* TYPE is a function or method type with a deferred exception
2810 specification that has been parsed to RAISES. Fixup all the type
2811 variants that are affected in place. Via decltype &| noexcept
2812 tricks, the unparsed spec could have escaped into the type system.
2813 The general case is hard to fixup canonical types for. */
2814
2815void
2816fixup_deferred_exception_variants (tree type, tree raises)
2817{
2818 tree original = TYPE_RAISES_EXCEPTIONS (type);
2819 tree cr = flag_noexcept_type ? canonical_eh_spec (raises) : NULL_TREE;
2820
2821 gcc_checking_assert (UNPARSED_NOEXCEPT_SPEC_P (original));
2822
2823 /* Though sucky, this walk will process the canonical variants
2824 first. */
2825 tree prev = NULL_TREE;
2826 for (tree variant = TYPE_MAIN_VARIANT (type);
2827 variant; prev = variant, variant = TYPE_NEXT_VARIANT (variant))
2828 if (TYPE_RAISES_EXCEPTIONS (variant) == original)
2829 {
2830 gcc_checking_assert (variant != TYPE_MAIN_VARIANT (type));
2831
2832 if (!TYPE_STRUCTURAL_EQUALITY_P (variant))
2833 {
2834 cp_cv_quals var_quals = TYPE_QUALS (variant);
2835 cp_ref_qualifier rqual = type_memfn_rqual (variant);
2836
2837 /* If VARIANT would become a dup (cp_check_qualified_type-wise)
2838 of an existing variant in the variant list of TYPE after its
2839 exception specification has been parsed, elide it. Otherwise,
2840 build_cp_fntype_variant could use it, leading to "canonical
2841 types differ for identical types." */
2842 tree v = TYPE_MAIN_VARIANT (type);
2843 for (; v; v = TYPE_NEXT_VARIANT (v))
2844 if (cp_check_qualified_type (cand: v, base: variant, type_quals: var_quals,
2845 rqual, raises: cr, late: false))
2846 {
2847 /* The main variant will not match V, so PREV will never
2848 be null. */
2849 TYPE_NEXT_VARIANT (prev) = TYPE_NEXT_VARIANT (variant);
2850 break;
2851 }
2852 TYPE_RAISES_EXCEPTIONS (variant) = raises;
2853
2854 if (!v)
2855 v = build_cp_fntype_variant (TYPE_CANONICAL (variant),
2856 rqual, raises: cr, late: false);
2857 TYPE_CANONICAL (variant) = TYPE_CANONICAL (v);
2858 }
2859 else
2860 TYPE_RAISES_EXCEPTIONS (variant) = raises;
2861
2862 if (!TYPE_DEPENDENT_P (variant))
2863 /* We no longer know that it's not type-dependent. */
2864 TYPE_DEPENDENT_P_VALID (variant) = false;
2865 }
2866}
2867
2868/* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
2869 listed in RAISES. */
2870
2871tree
2872build_exception_variant (tree type, tree raises)
2873{
2874 cp_ref_qualifier rqual = type_memfn_rqual (type);
2875 bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
2876 return build_cp_fntype_variant (type, rqual, raises, late);
2877}
2878
2879/* Given a TEMPLATE_TEMPLATE_PARM node T, create a new
2880 BOUND_TEMPLATE_TEMPLATE_PARM bound with NEWARGS as its template
2881 arguments. */
2882
2883tree
2884bind_template_template_parm (tree t, tree newargs)
2885{
2886 tree decl = TYPE_NAME (t);
2887 tree t2;
2888
2889 t2 = cxx_make_type (BOUND_TEMPLATE_TEMPLATE_PARM);
2890 decl = build_decl (input_location,
2891 TYPE_DECL, DECL_NAME (decl), NULL_TREE);
2892 SET_DECL_TEMPLATE_PARM_P (decl);
2893
2894 /* These nodes have to be created to reflect new TYPE_DECL and template
2895 arguments. */
2896 TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t));
2897 TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl;
2898 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
2899 = build_template_info (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t), newargs);
2900
2901 TREE_TYPE (decl) = t2;
2902 TYPE_NAME (t2) = decl;
2903 TYPE_STUB_DECL (t2) = decl;
2904 TYPE_SIZE (t2) = 0;
2905
2906 if (any_template_arguments_need_structural_equality_p (newargs))
2907 SET_TYPE_STRUCTURAL_EQUALITY (t2);
2908 else
2909 TYPE_CANONICAL (t2) = canonical_type_parameter (t2);
2910
2911 return t2;
2912}
2913
2914/* Called from count_trees via walk_tree. */
2915
2916static tree
2917count_trees_r (tree *tp, int *walk_subtrees, void *data)
2918{
2919 ++*((int *) data);
2920
2921 if (TYPE_P (*tp))
2922 *walk_subtrees = 0;
2923
2924 return NULL_TREE;
2925}
2926
2927/* Debugging function for measuring the rough complexity of a tree
2928 representation. */
2929
2930int
2931count_trees (tree t)
2932{
2933 int n_trees = 0;
2934 cp_walk_tree_without_duplicates (&t, count_trees_r, &n_trees);
2935 return n_trees;
2936}
2937
2938/* Called from verify_stmt_tree via walk_tree. */
2939
2940static tree
2941verify_stmt_tree_r (tree* tp, int * /*walk_subtrees*/, void* data)
2942{
2943 tree t = *tp;
2944 hash_table<nofree_ptr_hash <tree_node> > *statements
2945 = static_cast <hash_table<nofree_ptr_hash <tree_node> > *> (data);
2946 tree_node **slot;
2947
2948 if (!STATEMENT_CODE_P (TREE_CODE (t)))
2949 return NULL_TREE;
2950
2951 /* If this statement is already present in the hash table, then
2952 there is a circularity in the statement tree. */
2953 gcc_assert (!statements->find (t));
2954
2955 slot = statements->find_slot (value: t, insert: INSERT);
2956 *slot = t;
2957
2958 return NULL_TREE;
2959}
2960
2961/* Debugging function to check that the statement T has not been
2962 corrupted. For now, this function simply checks that T contains no
2963 circularities. */
2964
2965void
2966verify_stmt_tree (tree t)
2967{
2968 hash_table<nofree_ptr_hash <tree_node> > statements (37);
2969 cp_walk_tree (&t, verify_stmt_tree_r, &statements, NULL);
2970}
2971
2972/* Check if the type T depends on a type with no linkage and if so,
2973 return it. If RELAXED_P then do not consider a class type declared
2974 within a vague-linkage function or in a module CMI to have no linkage,
2975 since it can still be accessed within a different TU. Remember:
2976 no-linkage is not the same as internal-linkage. */
2977
2978tree
2979no_linkage_check (tree t, bool relaxed_p)
2980{
2981 tree r;
2982
2983 /* Lambda types that don't have mangling scope have no linkage. We
2984 check CLASSTYPE_LAMBDA_EXPR for error_mark_node because
2985 when we get here from pushtag none of the lambda information is
2986 set up yet, so we want to assume that the lambda has linkage and
2987 fix it up later if not. We need to check this even in templates so
2988 that we properly handle a lambda-expression in the signature. */
2989 if (LAMBDA_TYPE_P (t)
2990 && CLASSTYPE_LAMBDA_EXPR (t) != error_mark_node)
2991 {
2992 tree extra = LAMBDA_TYPE_EXTRA_SCOPE (t);
2993 if (!extra)
2994 return t;
2995 }
2996
2997 /* Otherwise there's no point in checking linkage on template functions; we
2998 can't know their complete types. */
2999 if (processing_template_decl)
3000 return NULL_TREE;
3001
3002 switch (TREE_CODE (t))
3003 {
3004 case RECORD_TYPE:
3005 if (TYPE_PTRMEMFUNC_P (t))
3006 goto ptrmem;
3007 /* Fall through. */
3008 case UNION_TYPE:
3009 if (!CLASS_TYPE_P (t))
3010 return NULL_TREE;
3011 /* Fall through. */
3012 case ENUMERAL_TYPE:
3013 /* Only treat unnamed types as having no linkage if they're at
3014 namespace scope. This is core issue 966. */
3015 if (TYPE_UNNAMED_P (t) && TYPE_NAMESPACE_SCOPE_P (t))
3016 {
3017 if (relaxed_p
3018 && TREE_PUBLIC (CP_TYPE_CONTEXT (t))
3019 && module_maybe_has_cmi_p ())
3020 /* This type could possibly be accessed outside this TU. */
3021 return NULL_TREE;
3022 else
3023 return t;
3024 }
3025
3026 for (r = CP_TYPE_CONTEXT (t); ; )
3027 {
3028 /* If we're a nested type of a !TREE_PUBLIC class, we might not
3029 have linkage, or we might just be in an anonymous namespace.
3030 If we're in a TREE_PUBLIC class, we have linkage. */
3031 if (TYPE_P (r) && !TREE_PUBLIC (TYPE_NAME (r)))
3032 return no_linkage_check (TYPE_CONTEXT (t), relaxed_p);
3033 else if (TREE_CODE (r) == FUNCTION_DECL)
3034 {
3035 if (relaxed_p
3036 && (vague_linkage_p (r)
3037 || (TREE_PUBLIC (r) && module_maybe_has_cmi_p ())))
3038 r = CP_DECL_CONTEXT (r);
3039 else
3040 return t;
3041 }
3042 else
3043 break;
3044 }
3045
3046 return NULL_TREE;
3047
3048 case ARRAY_TYPE:
3049 case POINTER_TYPE:
3050 case REFERENCE_TYPE:
3051 case VECTOR_TYPE:
3052 return no_linkage_check (TREE_TYPE (t), relaxed_p);
3053
3054 case OFFSET_TYPE:
3055 ptrmem:
3056 r = no_linkage_check (TYPE_PTRMEM_POINTED_TO_TYPE (t),
3057 relaxed_p);
3058 if (r)
3059 return r;
3060 return no_linkage_check (TYPE_PTRMEM_CLASS_TYPE (t), relaxed_p);
3061
3062 case METHOD_TYPE:
3063 case FUNCTION_TYPE:
3064 {
3065 tree parm = TYPE_ARG_TYPES (t);
3066 if (TREE_CODE (t) == METHOD_TYPE)
3067 /* The 'this' pointer isn't interesting; a method has the same
3068 linkage (or lack thereof) as its enclosing class. */
3069 parm = TREE_CHAIN (parm);
3070 for (;
3071 parm && parm != void_list_node;
3072 parm = TREE_CHAIN (parm))
3073 {
3074 r = no_linkage_check (TREE_VALUE (parm), relaxed_p);
3075 if (r)
3076 return r;
3077 }
3078 return no_linkage_check (TREE_TYPE (t), relaxed_p);
3079 }
3080
3081 default:
3082 return NULL_TREE;
3083 }
3084}
3085
3086extern int depth_reached;
3087
3088void
3089cxx_print_statistics (void)
3090{
3091 print_template_statistics ();
3092 if (GATHER_STATISTICS)
3093 fprintf (stderr, format: "maximum template instantiation depth reached: %d\n",
3094 depth_reached);
3095}
3096
3097/* Return, as an INTEGER_CST node, the number of elements for TYPE
3098 (which is an ARRAY_TYPE). This counts only elements of the top
3099 array. */
3100
3101tree
3102array_type_nelts_top (tree type)
3103{
3104 return fold_build2_loc (input_location,
3105 PLUS_EXPR, sizetype,
3106 array_type_nelts (type),
3107 size_one_node);
3108}
3109
3110/* Return, as an INTEGER_CST node, the number of elements for TYPE
3111 (which is an ARRAY_TYPE). This one is a recursive count of all
3112 ARRAY_TYPEs that are clumped together. */
3113
3114tree
3115array_type_nelts_total (tree type)
3116{
3117 tree sz = array_type_nelts_top (type);
3118 type = TREE_TYPE (type);
3119 while (TREE_CODE (type) == ARRAY_TYPE)
3120 {
3121 tree n = array_type_nelts_top (type);
3122 sz = fold_build2_loc (input_location,
3123 MULT_EXPR, sizetype, sz, n);
3124 type = TREE_TYPE (type);
3125 }
3126 return sz;
3127}
3128
3129struct bot_data
3130{
3131 splay_tree target_remap;
3132 bool clear_location;
3133};
3134
3135/* Called from break_out_target_exprs via mapcar. */
3136
3137static tree
3138bot_manip (tree* tp, int* walk_subtrees, void* data_)
3139{
3140 bot_data &data = *(bot_data*)data_;
3141 splay_tree target_remap = data.target_remap;
3142 tree t = *tp;
3143
3144 if (!TYPE_P (t) && TREE_CONSTANT (t) && !TREE_SIDE_EFFECTS (t))
3145 {
3146 /* There can't be any TARGET_EXPRs or their slot variables below this
3147 point. But we must make a copy, in case subsequent processing
3148 alters any part of it. For example, during gimplification a cast
3149 of the form (T) &X::f (where "f" is a member function) will lead
3150 to replacing the PTRMEM_CST for &X::f with a VAR_DECL. */
3151 *walk_subtrees = 0;
3152 *tp = unshare_expr (t);
3153 return NULL_TREE;
3154 }
3155 if (TREE_CODE (t) == TARGET_EXPR)
3156 {
3157 tree u;
3158
3159 if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
3160 {
3161 u = build_cplus_new (TREE_TYPE (t), TREE_OPERAND (t, 1),
3162 complain: tf_warning_or_error);
3163 if (u == error_mark_node)
3164 return u;
3165 if (AGGR_INIT_ZERO_FIRST (TREE_OPERAND (t, 1)))
3166 AGGR_INIT_ZERO_FIRST (TREE_OPERAND (u, 1)) = true;
3167 }
3168 else
3169 u = force_target_expr (TREE_TYPE (t), TREE_OPERAND (t, 1),
3170 complain: tf_warning_or_error);
3171
3172 TARGET_EXPR_IMPLICIT_P (u) = TARGET_EXPR_IMPLICIT_P (t);
3173 TARGET_EXPR_LIST_INIT_P (u) = TARGET_EXPR_LIST_INIT_P (t);
3174 TARGET_EXPR_DIRECT_INIT_P (u) = TARGET_EXPR_DIRECT_INIT_P (t);
3175 TARGET_EXPR_ELIDING_P (u) = TARGET_EXPR_ELIDING_P (t);
3176
3177 /* Map the old variable to the new one. */
3178 splay_tree_insert (target_remap,
3179 (splay_tree_key) TREE_OPERAND (t, 0),
3180 (splay_tree_value) TREE_OPERAND (u, 0));
3181
3182 TREE_OPERAND (u, 1) = break_out_target_exprs (TREE_OPERAND (u, 1),
3183 data.clear_location);
3184 if (TREE_OPERAND (u, 1) == error_mark_node)
3185 return error_mark_node;
3186
3187 if (data.clear_location)
3188 SET_EXPR_LOCATION (u, input_location);
3189
3190 /* Replace the old expression with the new version. */
3191 *tp = u;
3192 /* We don't have to go below this point; the recursive call to
3193 break_out_target_exprs will have handled anything below this
3194 point. */
3195 *walk_subtrees = 0;
3196 return NULL_TREE;
3197 }
3198 if (TREE_CODE (*tp) == SAVE_EXPR)
3199 {
3200 t = *tp;
3201 splay_tree_node n = splay_tree_lookup (target_remap,
3202 (splay_tree_key) t);
3203 if (n)
3204 {
3205 *tp = (tree)n->value;
3206 *walk_subtrees = 0;
3207 }
3208 else
3209 {
3210 copy_tree_r (tp, walk_subtrees, NULL);
3211 splay_tree_insert (target_remap,
3212 (splay_tree_key)t,
3213 (splay_tree_value)*tp);
3214 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
3215 splay_tree_insert (target_remap,
3216 (splay_tree_key)*tp,
3217 (splay_tree_value)*tp);
3218 }
3219 return NULL_TREE;
3220 }
3221 if (TREE_CODE (*tp) == DECL_EXPR
3222 && VAR_P (DECL_EXPR_DECL (*tp))
3223 && DECL_ARTIFICIAL (DECL_EXPR_DECL (*tp))
3224 && !TREE_STATIC (DECL_EXPR_DECL (*tp)))
3225 {
3226 tree t;
3227 splay_tree_node n
3228 = splay_tree_lookup (target_remap,
3229 (splay_tree_key) DECL_EXPR_DECL (*tp));
3230 if (n)
3231 t = (tree) n->value;
3232 else
3233 {
3234 t = create_temporary_var (TREE_TYPE (DECL_EXPR_DECL (*tp)));
3235 DECL_INITIAL (t) = DECL_INITIAL (DECL_EXPR_DECL (*tp));
3236 splay_tree_insert (target_remap,
3237 (splay_tree_key) DECL_EXPR_DECL (*tp),
3238 (splay_tree_value) t);
3239 }
3240 copy_tree_r (tp, walk_subtrees, NULL);
3241 DECL_EXPR_DECL (*tp) = t;
3242 if (data.clear_location && EXPR_HAS_LOCATION (*tp))
3243 SET_EXPR_LOCATION (*tp, input_location);
3244 return NULL_TREE;
3245 }
3246 if (TREE_CODE (*tp) == BIND_EXPR && BIND_EXPR_VARS (*tp))
3247 {
3248 copy_tree_r (tp, walk_subtrees, NULL);
3249 for (tree *p = &BIND_EXPR_VARS (*tp); *p; p = &DECL_CHAIN (*p))
3250 {
3251 gcc_assert (VAR_P (*p) && DECL_ARTIFICIAL (*p) && !TREE_STATIC (*p));
3252 tree t = create_temporary_var (TREE_TYPE (*p));
3253 DECL_INITIAL (t) = DECL_INITIAL (*p);
3254 DECL_CHAIN (t) = DECL_CHAIN (*p);
3255 splay_tree_insert (target_remap, (splay_tree_key) *p,
3256 (splay_tree_value) t);
3257 *p = t;
3258 }
3259 if (data.clear_location && EXPR_HAS_LOCATION (*tp))
3260 SET_EXPR_LOCATION (*tp, input_location);
3261 return NULL_TREE;
3262 }
3263
3264 /* Make a copy of this node. */
3265 t = copy_tree_r (tp, walk_subtrees, NULL);
3266 if (TREE_CODE (*tp) == CALL_EXPR || TREE_CODE (*tp) == AGGR_INIT_EXPR)
3267 if (!processing_template_decl)
3268 set_flags_from_callee (*tp);
3269 if (data.clear_location && EXPR_HAS_LOCATION (*tp))
3270 SET_EXPR_LOCATION (*tp, input_location);
3271 return t;
3272}
3273
3274/* Replace all remapped VAR_DECLs in T with their new equivalents.
3275 DATA is really a splay-tree mapping old variables to new
3276 variables. */
3277
3278static tree
3279bot_replace (tree* t, int */*walk_subtrees*/, void* data_)
3280{
3281 bot_data &data = *(bot_data*)data_;
3282 splay_tree target_remap = data.target_remap;
3283
3284 if (VAR_P (*t))
3285 {
3286 splay_tree_node n = splay_tree_lookup (target_remap,
3287 (splay_tree_key) *t);
3288 if (n)
3289 *t = (tree) n->value;
3290 }
3291 else if (TREE_CODE (*t) == PARM_DECL
3292 && DECL_NAME (*t) == this_identifier
3293 && !DECL_CONTEXT (*t))
3294 {
3295 /* In an NSDMI we need to replace the 'this' parameter we used for
3296 parsing with the real one for this function. */
3297 *t = current_class_ptr;
3298 }
3299 else if (TREE_CODE (*t) == CONVERT_EXPR
3300 && CONVERT_EXPR_VBASE_PATH (*t))
3301 {
3302 /* In an NSDMI build_base_path defers building conversions to morally
3303 virtual bases, and we handle it here. */
3304 tree basetype = TREE_TYPE (*t);
3305 *t = convert_to_base (TREE_OPERAND (*t, 0), basetype,
3306 /*check_access=*/false, /*nonnull=*/true,
3307 tf_warning_or_error);
3308 }
3309
3310 return NULL_TREE;
3311}
3312
3313/* When we parse a default argument expression, we may create
3314 temporary variables via TARGET_EXPRs. When we actually use the
3315 default-argument expression, we make a copy of the expression
3316 and replace the temporaries with appropriate local versions.
3317
3318 If CLEAR_LOCATION is true, override any EXPR_LOCATION with
3319 input_location. */
3320
3321tree
3322break_out_target_exprs (tree t, bool clear_location /* = false */)
3323{
3324 static int target_remap_count;
3325 static splay_tree target_remap;
3326
3327 /* We shouldn't be called on templated trees, nor do we want to
3328 produce them. */
3329 gcc_checking_assert (!processing_template_decl);
3330
3331 if (!target_remap_count++)
3332 target_remap = splay_tree_new (splay_tree_compare_pointers,
3333 /*splay_tree_delete_key_fn=*/NULL,
3334 /*splay_tree_delete_value_fn=*/NULL);
3335 bot_data data = { .target_remap: target_remap, .clear_location: clear_location };
3336 if (cp_walk_tree (&t, bot_manip, &data, NULL) == error_mark_node)
3337 t = error_mark_node;
3338 if (cp_walk_tree (&t, bot_replace, &data, NULL) == error_mark_node)
3339 t = error_mark_node;
3340
3341 if (!--target_remap_count)
3342 {
3343 splay_tree_delete (target_remap);
3344 target_remap = NULL;
3345 }
3346
3347 return t;
3348}
3349
3350/* Build an expression for the subobject of OBJ at CONSTRUCTOR index INDEX,
3351 which we expect to have type TYPE. */
3352
3353tree
3354build_ctor_subob_ref (tree index, tree type, tree obj)
3355{
3356 if (index == NULL_TREE)
3357 /* Can't refer to a particular member of a vector. */
3358 obj = NULL_TREE;
3359 else if (TREE_CODE (index) == INTEGER_CST)
3360 obj = cp_build_array_ref (input_location, obj, index, tf_none);
3361 else
3362 obj = build_class_member_access_expr (obj, index, NULL_TREE,
3363 /*reference*/false, tf_none);
3364 if (obj)
3365 {
3366 tree objtype = TREE_TYPE (obj);
3367 if (TREE_CODE (objtype) == ARRAY_TYPE && !TYPE_DOMAIN (objtype))
3368 {
3369 /* When the destination object refers to a flexible array member
3370 verify that it matches the type of the source object except
3371 for its domain and qualifiers. */
3372 gcc_assert (comptypes (TYPE_MAIN_VARIANT (type),
3373 TYPE_MAIN_VARIANT (objtype),
3374 COMPARE_REDECLARATION));
3375 }
3376 else
3377 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, objtype));
3378 }
3379
3380 return obj;
3381}
3382
3383struct replace_placeholders_t
3384{
3385 tree obj; /* The object to be substituted for a PLACEHOLDER_EXPR. */
3386 tree exp; /* The outermost exp. */
3387 bool seen; /* Whether we've encountered a PLACEHOLDER_EXPR. */
3388 hash_set<tree> *pset; /* To avoid walking same trees multiple times. */
3389};
3390
3391/* Like substitute_placeholder_in_expr, but handle C++ tree codes and
3392 build up subexpressions as we go deeper. */
3393
3394static tree
3395replace_placeholders_r (tree* t, int* walk_subtrees, void* data_)
3396{
3397 replace_placeholders_t *d = static_cast<replace_placeholders_t*>(data_);
3398 tree obj = d->obj;
3399
3400 if (TYPE_P (*t) || TREE_CONSTANT (*t))
3401 {
3402 *walk_subtrees = false;
3403 return NULL_TREE;
3404 }
3405
3406 switch (TREE_CODE (*t))
3407 {
3408 case PLACEHOLDER_EXPR:
3409 {
3410 tree x = obj;
3411 for (; !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (*t),
3412 TREE_TYPE (x));
3413 x = TREE_OPERAND (x, 0))
3414 gcc_assert (handled_component_p (x));
3415 *t = unshare_expr (x);
3416 *walk_subtrees = false;
3417 d->seen = true;
3418 }
3419 break;
3420
3421 case CONSTRUCTOR:
3422 {
3423 constructor_elt *ce;
3424 vec<constructor_elt,va_gc> *v = CONSTRUCTOR_ELTS (*t);
3425 /* Don't walk into CONSTRUCTOR_PLACEHOLDER_BOUNDARY ctors
3426 other than the d->exp one, those have PLACEHOLDER_EXPRs
3427 related to another object. */
3428 if ((CONSTRUCTOR_PLACEHOLDER_BOUNDARY (*t)
3429 && *t != d->exp)
3430 || d->pset->add (k: *t))
3431 {
3432 *walk_subtrees = false;
3433 return NULL_TREE;
3434 }
3435 for (unsigned i = 0; vec_safe_iterate (v, ix: i, ptr: &ce); ++i)
3436 {
3437 tree *valp = &ce->value;
3438 tree type = TREE_TYPE (*valp);
3439 tree subob = obj;
3440
3441 /* Elements with RANGE_EXPR index shouldn't have any
3442 placeholders in them. */
3443 if (ce->index && TREE_CODE (ce->index) == RANGE_EXPR)
3444 continue;
3445
3446 if (TREE_CODE (*valp) == CONSTRUCTOR
3447 && AGGREGATE_TYPE_P (type))
3448 {
3449 /* If we're looking at the initializer for OBJ, then build
3450 a sub-object reference. If we're looking at an
3451 initializer for another object, just pass OBJ down. */
3452 if (same_type_ignoring_top_level_qualifiers_p
3453 (TREE_TYPE (*t), TREE_TYPE (obj)))
3454 subob = build_ctor_subob_ref (index: ce->index, type, obj);
3455 if (TREE_CODE (*valp) == TARGET_EXPR)
3456 valp = &TARGET_EXPR_INITIAL (*valp);
3457 }
3458 d->obj = subob;
3459 cp_walk_tree (valp, replace_placeholders_r, data_, NULL);
3460 d->obj = obj;
3461 }
3462 *walk_subtrees = false;
3463 break;
3464 }
3465
3466 default:
3467 if (d->pset->add (k: *t))
3468 *walk_subtrees = false;
3469 break;
3470 }
3471
3472 return NULL_TREE;
3473}
3474
3475/* Replace PLACEHOLDER_EXPRs in EXP with object OBJ. SEEN_P is set if
3476 a PLACEHOLDER_EXPR has been encountered. */
3477
3478tree
3479replace_placeholders (tree exp, tree obj, bool *seen_p /*= NULL*/)
3480{
3481 /* This is only relevant for C++14. */
3482 if (cxx_dialect < cxx14)
3483 return exp;
3484
3485 /* If the object isn't a (member of a) class, do nothing. */
3486 tree op0 = obj;
3487 while (handled_component_p (t: op0))
3488 op0 = TREE_OPERAND (op0, 0);
3489 if (!CLASS_TYPE_P (strip_array_types (TREE_TYPE (op0))))
3490 return exp;
3491
3492 tree *tp = &exp;
3493 if (TREE_CODE (exp) == TARGET_EXPR)
3494 tp = &TARGET_EXPR_INITIAL (exp);
3495 hash_set<tree> pset;
3496 replace_placeholders_t data = { .obj: obj, .exp: *tp, .seen: false, .pset: &pset };
3497 cp_walk_tree (tp, replace_placeholders_r, &data, NULL);
3498 if (seen_p)
3499 *seen_p = data.seen;
3500 return exp;
3501}
3502
3503/* Callback function for find_placeholders. */
3504
3505static tree
3506find_placeholders_r (tree *t, int *walk_subtrees, void *)
3507{
3508 if (TYPE_P (*t) || TREE_CONSTANT (*t))
3509 {
3510 *walk_subtrees = false;
3511 return NULL_TREE;
3512 }
3513
3514 switch (TREE_CODE (*t))
3515 {
3516 case PLACEHOLDER_EXPR:
3517 return *t;
3518
3519 case CONSTRUCTOR:
3520 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (*t))
3521 *walk_subtrees = false;
3522 break;
3523
3524 default:
3525 break;
3526 }
3527
3528 return NULL_TREE;
3529}
3530
3531/* Return true if EXP contains a PLACEHOLDER_EXPR. Don't walk into
3532 ctors with CONSTRUCTOR_PLACEHOLDER_BOUNDARY flag set. */
3533
3534bool
3535find_placeholders (tree exp)
3536{
3537 /* This is only relevant for C++14. */
3538 if (cxx_dialect < cxx14)
3539 return false;
3540
3541 return cp_walk_tree_without_duplicates (&exp, find_placeholders_r, NULL);
3542}
3543
3544/* Similar to `build_nt', but for template definitions of dependent
3545 expressions */
3546
3547tree
3548build_min_nt_loc (location_t loc, enum tree_code code, ...)
3549{
3550 tree t;
3551 int length;
3552 int i;
3553 va_list p;
3554
3555 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3556
3557 va_start (p, code);
3558
3559 t = make_node (code);
3560 SET_EXPR_LOCATION (t, loc);
3561 length = TREE_CODE_LENGTH (code);
3562
3563 for (i = 0; i < length; i++)
3564 TREE_OPERAND (t, i) = va_arg (p, tree);
3565
3566 va_end (p);
3567 return t;
3568}
3569
3570/* Similar to `build', but for template definitions. */
3571
3572tree
3573build_min (enum tree_code code, tree tt, ...)
3574{
3575 tree t;
3576 int length;
3577 int i;
3578 va_list p;
3579
3580 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3581
3582 va_start (p, tt);
3583
3584 t = make_node (code);
3585 length = TREE_CODE_LENGTH (code);
3586 TREE_TYPE (t) = tt;
3587
3588 for (i = 0; i < length; i++)
3589 {
3590 tree x = va_arg (p, tree);
3591 TREE_OPERAND (t, i) = x;
3592 if (x && !TYPE_P (x) && TREE_SIDE_EFFECTS (x))
3593 TREE_SIDE_EFFECTS (t) = 1;
3594 }
3595
3596 va_end (p);
3597
3598 return t;
3599}
3600
3601/* Similar to `build', but for template definitions of non-dependent
3602 expressions. NON_DEP is the non-dependent expression that has been
3603 built. */
3604
3605tree
3606build_min_non_dep (enum tree_code code, tree non_dep, ...)
3607{
3608 tree t;
3609 int length;
3610 int i;
3611 va_list p;
3612
3613 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3614
3615 va_start (p, non_dep);
3616
3617 if (REFERENCE_REF_P (non_dep))
3618 non_dep = TREE_OPERAND (non_dep, 0);
3619
3620 t = make_node (code);
3621 SET_EXPR_LOCATION (t, cp_expr_loc_or_input_loc (non_dep));
3622 length = TREE_CODE_LENGTH (code);
3623 TREE_TYPE (t) = unlowered_expr_type (non_dep);
3624 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
3625
3626 for (i = 0; i < length; i++)
3627 {
3628 tree x = va_arg (p, tree);
3629 TREE_OPERAND (t, i) = x;
3630 if (x && !TYPE_P (x))
3631 TREE_SIDE_EFFECTS (t) |= TREE_SIDE_EFFECTS (x);
3632 }
3633
3634 va_end (p);
3635 return convert_from_reference (t);
3636}
3637
3638/* Similar to build_min_nt, but call expressions */
3639
3640tree
3641build_min_nt_call_vec (tree fn, vec<tree, va_gc> *args)
3642{
3643 tree ret, t;
3644 unsigned int ix;
3645
3646 ret = build_vl_exp (CALL_EXPR, vec_safe_length (v: args) + 3);
3647 CALL_EXPR_FN (ret) = fn;
3648 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
3649 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
3650 CALL_EXPR_ARG (ret, ix) = t;
3651
3652 return ret;
3653}
3654
3655/* Similar to `build_min_nt_call_vec', but for template definitions of
3656 non-dependent expressions. NON_DEP is the non-dependent expression
3657 that has been built. */
3658
3659tree
3660build_min_non_dep_call_vec (tree non_dep, tree fn, vec<tree, va_gc> *argvec)
3661{
3662 tree t = build_min_nt_call_vec (fn, args: argvec);
3663 if (REFERENCE_REF_P (non_dep))
3664 non_dep = TREE_OPERAND (non_dep, 0);
3665 TREE_TYPE (t) = TREE_TYPE (non_dep);
3666 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
3667 if (argvec)
3668 for (tree x : *argvec)
3669 if (x && !TYPE_P (x))
3670 TREE_SIDE_EFFECTS (t) |= TREE_SIDE_EFFECTS (x);
3671 return convert_from_reference (t);
3672}
3673
3674/* Similar to build_min_non_dep, but for expressions that have been resolved to
3675 a call to an operator overload. OP is the operator that has been
3676 overloaded. NON_DEP is the non-dependent expression that's been built,
3677 which should be a CALL_EXPR or an INDIRECT_REF to a CALL_EXPR. OVERLOAD is
3678 the overload that NON_DEP is calling. */
3679
3680tree
3681build_min_non_dep_op_overload (enum tree_code op,
3682 tree non_dep,
3683 tree overload, ...)
3684{
3685 va_list p;
3686 int nargs, expected_nargs;
3687 tree fn, call, obj = NULL_TREE;
3688
3689 non_dep = extract_call_expr (non_dep);
3690
3691 nargs = call_expr_nargs (non_dep);
3692
3693 expected_nargs = cp_tree_code_length (op);
3694 if (DECL_OBJECT_MEMBER_FUNCTION_P (overload)
3695 /* For ARRAY_REF, operator[] is either a non-static member or newly
3696 static member, never out of class and for the static member case
3697 if user uses single index the operator[] needs to have a single
3698 argument as well, but the function is called with 2 - the object
3699 it is invoked on and the index. */
3700 || op == ARRAY_REF)
3701 expected_nargs -= 1;
3702 if ((op == POSTINCREMENT_EXPR
3703 || op == POSTDECREMENT_EXPR)
3704 /* With -fpermissive non_dep could be operator++(). */
3705 && (!flag_permissive || nargs != expected_nargs))
3706 expected_nargs += 1;
3707 gcc_assert (nargs == expected_nargs);
3708
3709 releasing_vec args;
3710 va_start (p, overload);
3711
3712 if (!DECL_OBJECT_MEMBER_FUNCTION_P (overload))
3713 {
3714 fn = overload;
3715 if (op == ARRAY_REF)
3716 obj = va_arg (p, tree);
3717 for (int i = 0; i < nargs; i++)
3718 {
3719 tree arg = va_arg (p, tree);
3720 vec_safe_push (r&: args, t: arg);
3721 }
3722 }
3723 else
3724 {
3725 tree object = va_arg (p, tree);
3726 tree binfo = TYPE_BINFO (TREE_TYPE (object));
3727 tree method = build_baselink (binfo, binfo, overload, NULL_TREE);
3728 fn = build_min (code: COMPONENT_REF, TREE_TYPE (overload),
3729 object, method, NULL_TREE);
3730 for (int i = 0; i < nargs; i++)
3731 {
3732 tree arg = va_arg (p, tree);
3733 vec_safe_push (r&: args, t: arg);
3734 }
3735 }
3736
3737 va_end (p);
3738 call = build_min_non_dep_call_vec (non_dep, fn, argvec: args);
3739
3740 tree call_expr = extract_call_expr (call);
3741 KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep);
3742 CALL_EXPR_OPERATOR_SYNTAX (call_expr) = true;
3743 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep);
3744 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep);
3745
3746 if (obj)
3747 return keep_unused_object_arg (call, obj, overload);
3748 return call;
3749}
3750
3751/* Similar to above build_min_non_dep_op_overload, but arguments
3752 are taken from ARGS vector. */
3753
3754tree
3755build_min_non_dep_op_overload (tree non_dep, tree overload, tree object,
3756 vec<tree, va_gc> *args)
3757{
3758 non_dep = extract_call_expr (non_dep);
3759
3760 unsigned int nargs = call_expr_nargs (non_dep);
3761 tree fn = overload;
3762 if (DECL_OBJECT_MEMBER_FUNCTION_P (overload))
3763 {
3764 tree binfo = TYPE_BINFO (TREE_TYPE (object));
3765 tree method = build_baselink (binfo, binfo, overload, NULL_TREE);
3766 fn = build_min (code: COMPONENT_REF, TREE_TYPE (overload),
3767 object, method, NULL_TREE);
3768 object = NULL_TREE;
3769 }
3770 gcc_assert (vec_safe_length (args) == nargs);
3771
3772 tree call = build_min_non_dep_call_vec (non_dep, fn, argvec: args);
3773
3774 tree call_expr = extract_call_expr (call);
3775 KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep);
3776 CALL_EXPR_OPERATOR_SYNTAX (call_expr) = true;
3777 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep);
3778 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep);
3779
3780 if (object)
3781 return keep_unused_object_arg (call, object, overload);
3782 return call;
3783}
3784
3785/* Return a new tree vec copied from VEC, with ELT inserted at index IDX. */
3786
3787vec<tree, va_gc> *
3788vec_copy_and_insert (vec<tree, va_gc> *old_vec, tree elt, unsigned idx)
3789{
3790 unsigned len = vec_safe_length (v: old_vec);
3791 gcc_assert (idx <= len);
3792
3793 vec<tree, va_gc> *new_vec = NULL;
3794 vec_alloc (v&: new_vec, nelems: len + 1);
3795
3796 unsigned i;
3797 for (i = 0; i < len; ++i)
3798 {
3799 if (i == idx)
3800 new_vec->quick_push (obj: elt);
3801 new_vec->quick_push (obj: (*old_vec)[i]);
3802 }
3803 if (i == idx)
3804 new_vec->quick_push (obj: elt);
3805
3806 return new_vec;
3807}
3808
3809tree
3810get_type_decl (tree t)
3811{
3812 if (TREE_CODE (t) == TYPE_DECL)
3813 return t;
3814 if (TYPE_P (t))
3815 return TYPE_STUB_DECL (t);
3816 gcc_assert (t == error_mark_node);
3817 return t;
3818}
3819
3820/* Returns the namespace that contains DECL, whether directly or
3821 indirectly. */
3822
3823tree
3824decl_namespace_context (tree decl)
3825{
3826 while (1)
3827 {
3828 if (TREE_CODE (decl) == NAMESPACE_DECL)
3829 return decl;
3830 else if (TYPE_P (decl))
3831 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
3832 else
3833 decl = CP_DECL_CONTEXT (decl);
3834 }
3835}
3836
3837/* Returns true if decl is within an anonymous namespace, however deeply
3838 nested, or false otherwise. */
3839
3840bool
3841decl_anon_ns_mem_p (tree decl)
3842{
3843 return !TREE_PUBLIC (decl_namespace_context (decl));
3844}
3845
3846/* Returns true if the enclosing scope of DECL has internal or no linkage. */
3847
3848bool
3849decl_internal_context_p (const_tree decl)
3850{
3851 while (TREE_CODE (decl) != NAMESPACE_DECL)
3852 {
3853 /* Classes inside anonymous namespaces have TREE_PUBLIC == 0. */
3854 if (TYPE_P (decl))
3855 return !TREE_PUBLIC (TYPE_MAIN_DECL (decl));
3856
3857 decl = CP_DECL_CONTEXT (decl);
3858 }
3859 return !TREE_PUBLIC (decl);
3860}
3861
3862/* Subroutine of cp_tree_equal: t1 and t2 are two CALL_EXPRs.
3863 Return whether their CALL_EXPR_FNs are equivalent. */
3864
3865static bool
3866called_fns_equal (tree t1, tree t2)
3867{
3868 /* Core 1321: dependent names are equivalent even if the overload sets
3869 are different. But do compare explicit template arguments. */
3870 tree name1 = call_expr_dependent_name (x: t1);
3871 tree name2 = call_expr_dependent_name (x: t2);
3872 t1 = CALL_EXPR_FN (t1);
3873 t2 = CALL_EXPR_FN (t2);
3874 if (name1 || name2)
3875 {
3876 tree targs1 = NULL_TREE, targs2 = NULL_TREE;
3877
3878 if (name1 != name2)
3879 return false;
3880
3881 /* FIXME dependent_name currently returns an unqualified name regardless
3882 of whether the function was named with a qualified- or unqualified-id.
3883 Until that's fixed, check that we aren't looking at overload sets from
3884 different scopes. */
3885 if (is_overloaded_fn (x: t1) && is_overloaded_fn (x: t2)
3886 && (DECL_CONTEXT (get_first_fn (t1))
3887 != DECL_CONTEXT (get_first_fn (t2))))
3888 return false;
3889
3890 if (TREE_CODE (t1) == TEMPLATE_ID_EXPR)
3891 targs1 = TREE_OPERAND (t1, 1);
3892 if (TREE_CODE (t2) == TEMPLATE_ID_EXPR)
3893 targs2 = TREE_OPERAND (t2, 1);
3894 return cp_tree_equal (targs1, targs2);
3895 }
3896 else
3897 return cp_tree_equal (t1, t2);
3898}
3899
3900bool comparing_override_contracts;
3901
3902/* In a component reference, return the innermost object of
3903 the postfix-expression. */
3904
3905static tree
3906get_innermost_component (tree t)
3907{
3908 gcc_assert (TREE_CODE (t) == COMPONENT_REF);
3909 while (TREE_CODE (t) == COMPONENT_REF)
3910 t = TREE_OPERAND (t, 0);
3911 return t;
3912}
3913
3914/* Returns true if T is a possibly converted 'this' or '*this' expression. */
3915
3916static bool
3917is_this_expression (tree t)
3918{
3919 t = get_innermost_component (t);
3920 /* See through deferences and no-op conversions. */
3921 if (INDIRECT_REF_P (t))
3922 t = TREE_OPERAND (t, 0);
3923 if (TREE_CODE (t) == NOP_EXPR)
3924 t = TREE_OPERAND (t, 0);
3925 return is_this_parameter (t);
3926}
3927
3928static bool
3929comparing_this_references (tree t1, tree t2)
3930{
3931 return is_this_expression (t: t1) && is_this_expression (t: t2);
3932}
3933
3934static bool
3935equivalent_member_references (tree t1, tree t2)
3936{
3937 if (!comparing_this_references (t1, t2))
3938 return false;
3939 t1 = TREE_OPERAND (t1, 1);
3940 t2 = TREE_OPERAND (t2, 1);
3941 return t1 == t2;
3942}
3943
3944/* Return truthvalue of whether T1 is the same tree structure as T2.
3945 Return 1 if they are the same. Return 0 if they are different. */
3946
3947bool
3948cp_tree_equal (tree t1, tree t2)
3949{
3950 enum tree_code code1, code2;
3951
3952 if (t1 == t2)
3953 return true;
3954 if (!t1 || !t2)
3955 return false;
3956
3957 code1 = TREE_CODE (t1);
3958 code2 = TREE_CODE (t2);
3959
3960 if (code1 != code2)
3961 return false;
3962
3963 if (CONSTANT_CLASS_P (t1)
3964 && !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3965 return false;
3966
3967 switch (code1)
3968 {
3969 case VOID_CST:
3970 /* There's only a single VOID_CST node, so we should never reach
3971 here. */
3972 gcc_unreachable ();
3973
3974 case INTEGER_CST:
3975 return tree_int_cst_equal (t1, t2);
3976
3977 case REAL_CST:
3978 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
3979
3980 case STRING_CST:
3981 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3982 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3983 TREE_STRING_LENGTH (t1));
3984
3985 case FIXED_CST:
3986 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
3987 TREE_FIXED_CST (t2));
3988
3989 case COMPLEX_CST:
3990 return cp_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
3991 && cp_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
3992
3993 case VECTOR_CST:
3994 return operand_equal_p (t1, t2, flags: OEP_ONLY_CONST);
3995
3996 case CONSTRUCTOR:
3997 /* We need to do this when determining whether or not two
3998 non-type pointer to member function template arguments
3999 are the same. */
4000 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
4001 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
4002 return false;
4003 {
4004 tree field, value;
4005 unsigned int i;
4006 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
4007 {
4008 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
4009 if (!cp_tree_equal (t1: field, t2: elt2->index)
4010 || !cp_tree_equal (t1: value, t2: elt2->value))
4011 return false;
4012 }
4013 }
4014 return true;
4015
4016 case TREE_LIST:
4017 if (!cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
4018 return false;
4019 if (!cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
4020 return false;
4021 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
4022
4023 case SAVE_EXPR:
4024 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
4025
4026 case CALL_EXPR:
4027 {
4028 if (KOENIG_LOOKUP_P (t1) != KOENIG_LOOKUP_P (t2))
4029 return false;
4030
4031 if (!called_fns_equal (t1, t2))
4032 return false;
4033
4034 call_expr_arg_iterator iter1, iter2;
4035 init_call_expr_arg_iterator (exp: t1, iter: &iter1);
4036 init_call_expr_arg_iterator (exp: t2, iter: &iter2);
4037 if (iter1.n != iter2.n)
4038 return false;
4039
4040 while (more_call_expr_args_p (iter: &iter1))
4041 {
4042 tree arg1 = next_call_expr_arg (iter: &iter1);
4043 tree arg2 = next_call_expr_arg (iter: &iter2);
4044
4045 gcc_checking_assert (arg1 && arg2);
4046 if (!cp_tree_equal (t1: arg1, t2: arg2))
4047 return false;
4048 }
4049
4050 return true;
4051 }
4052
4053 case TARGET_EXPR:
4054 {
4055 tree o1 = TREE_OPERAND (t1, 0);
4056 tree o2 = TREE_OPERAND (t2, 0);
4057
4058 /* Special case: if either target is an unallocated VAR_DECL,
4059 it means that it's going to be unified with whatever the
4060 TARGET_EXPR is really supposed to initialize, so treat it
4061 as being equivalent to anything. */
4062 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
4063 && !DECL_RTL_SET_P (o1))
4064 /*Nop*/;
4065 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
4066 && !DECL_RTL_SET_P (o2))
4067 /*Nop*/;
4068 else if (!cp_tree_equal (t1: o1, t2: o2))
4069 return false;
4070
4071 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
4072 }
4073
4074 case PARM_DECL:
4075 /* For comparing uses of parameters in late-specified return types
4076 with an out-of-class definition of the function, but can also come
4077 up for expressions that involve 'this' in a member function
4078 template. */
4079
4080 if (comparing_specializations
4081 && DECL_CONTEXT (t1) != DECL_CONTEXT (t2))
4082 /* When comparing hash table entries, only an exact match is
4083 good enough; we don't want to replace 'this' with the
4084 version from another function. But be more flexible
4085 with parameters with identical contexts. */
4086 return false;
4087
4088 if (same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
4089 {
4090 if (DECL_ARTIFICIAL (t1) ^ DECL_ARTIFICIAL (t2))
4091 return false;
4092 if (CONSTRAINT_VAR_P (t1) ^ CONSTRAINT_VAR_P (t2))
4093 return false;
4094 if (DECL_ARTIFICIAL (t1)
4095 || (DECL_PARM_LEVEL (t1) == DECL_PARM_LEVEL (t2)
4096 && DECL_PARM_INDEX (t1) == DECL_PARM_INDEX (t2)))
4097 return true;
4098 }
4099 return false;
4100
4101 case TEMPLATE_DECL:
4102 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t1)
4103 && DECL_TEMPLATE_TEMPLATE_PARM_P (t2))
4104 return cp_tree_equal (TREE_TYPE (t1), TREE_TYPE (t2));
4105 /* Fall through. */
4106 case VAR_DECL:
4107 case CONST_DECL:
4108 case FIELD_DECL:
4109 case FUNCTION_DECL:
4110 case IDENTIFIER_NODE:
4111 case SSA_NAME:
4112 case USING_DECL:
4113 case DEFERRED_PARSE:
4114 return false;
4115
4116 case BASELINK:
4117 return (BASELINK_BINFO (t1) == BASELINK_BINFO (t2)
4118 && BASELINK_ACCESS_BINFO (t1) == BASELINK_ACCESS_BINFO (t2)
4119 && BASELINK_QUALIFIED_P (t1) == BASELINK_QUALIFIED_P (t2)
4120 && cp_tree_equal (BASELINK_FUNCTIONS (t1),
4121 BASELINK_FUNCTIONS (t2)));
4122
4123 case TEMPLATE_PARM_INDEX:
4124 return (TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
4125 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2)
4126 && (TEMPLATE_PARM_PARAMETER_PACK (t1)
4127 == TEMPLATE_PARM_PARAMETER_PACK (t2))
4128 && same_type_p (TREE_TYPE (TEMPLATE_PARM_DECL (t1)),
4129 TREE_TYPE (TEMPLATE_PARM_DECL (t2))));
4130
4131 case TEMPLATE_ID_EXPR:
4132 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
4133 return false;
4134 if (!comp_template_args (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)))
4135 return false;
4136 return true;
4137
4138 case CONSTRAINT_INFO:
4139 return cp_tree_equal (CI_ASSOCIATED_CONSTRAINTS (t1),
4140 CI_ASSOCIATED_CONSTRAINTS (t2));
4141
4142 case CHECK_CONSTR:
4143 return (CHECK_CONSTR_CONCEPT (t1) == CHECK_CONSTR_CONCEPT (t2)
4144 && comp_template_args (CHECK_CONSTR_ARGS (t1),
4145 CHECK_CONSTR_ARGS (t2)));
4146
4147 case TREE_VEC:
4148 /* These are template args. Really we should be getting the
4149 caller to do this as it knows it to be true. */
4150 if (!comp_template_args (t1, t2))
4151 return false;
4152 return true;
4153
4154 case SIZEOF_EXPR:
4155 case ALIGNOF_EXPR:
4156 {
4157 tree o1 = TREE_OPERAND (t1, 0);
4158 tree o2 = TREE_OPERAND (t2, 0);
4159
4160 if (code1 == SIZEOF_EXPR)
4161 {
4162 if (SIZEOF_EXPR_TYPE_P (t1))
4163 o1 = TREE_TYPE (o1);
4164 if (SIZEOF_EXPR_TYPE_P (t2))
4165 o2 = TREE_TYPE (o2);
4166 }
4167 else if (ALIGNOF_EXPR_STD_P (t1) != ALIGNOF_EXPR_STD_P (t2))
4168 return false;
4169
4170 if (TREE_CODE (o1) != TREE_CODE (o2))
4171 return false;
4172
4173 if (ARGUMENT_PACK_P (o1))
4174 return template_args_equal (o1, o2);
4175 else if (TYPE_P (o1))
4176 return same_type_p (o1, o2);
4177 else
4178 return cp_tree_equal (t1: o1, t2: o2);
4179 }
4180
4181 case MODOP_EXPR:
4182 {
4183 tree t1_op1, t2_op1;
4184
4185 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
4186 return false;
4187
4188 t1_op1 = TREE_OPERAND (t1, 1);
4189 t2_op1 = TREE_OPERAND (t2, 1);
4190 if (TREE_CODE (t1_op1) != TREE_CODE (t2_op1))
4191 return false;
4192
4193 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t2, 2));
4194 }
4195
4196 case PTRMEM_CST:
4197 /* Two pointer-to-members are the same if they point to the same
4198 field or function in the same class. */
4199 if (PTRMEM_CST_MEMBER (t1) != PTRMEM_CST_MEMBER (t2))
4200 return false;
4201
4202 return same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2));
4203
4204 case OVERLOAD:
4205 {
4206 /* Two overloads. Must be exactly the same set of decls. */
4207 lkp_iterator first (t1);
4208 lkp_iterator second (t2);
4209
4210 for (; first && second; ++first, ++second)
4211 if (*first != *second)
4212 return false;
4213 return !(first || second);
4214 }
4215
4216 case TRAIT_EXPR:
4217 if (TRAIT_EXPR_KIND (t1) != TRAIT_EXPR_KIND (t2))
4218 return false;
4219 return cp_tree_equal (TRAIT_EXPR_TYPE1 (t1), TRAIT_EXPR_TYPE1 (t2))
4220 && cp_tree_equal (TRAIT_EXPR_TYPE2 (t1), TRAIT_EXPR_TYPE2 (t2));
4221
4222 case NON_LVALUE_EXPR:
4223 case VIEW_CONVERT_EXPR:
4224 /* Used for location wrappers with possibly NULL types. */
4225 if (!TREE_TYPE (t1) || !TREE_TYPE (t2))
4226 {
4227 if (TREE_TYPE (t1) || TREE_TYPE (t2))
4228 return false;
4229 break;
4230 }
4231 /* FALLTHROUGH */
4232
4233 case CAST_EXPR:
4234 case STATIC_CAST_EXPR:
4235 case REINTERPRET_CAST_EXPR:
4236 case CONST_CAST_EXPR:
4237 case DYNAMIC_CAST_EXPR:
4238 case IMPLICIT_CONV_EXPR:
4239 case NEW_EXPR:
4240 case BIT_CAST_EXPR:
4241 CASE_CONVERT:
4242 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
4243 return false;
4244 /* Now compare operands as usual. */
4245 break;
4246
4247 case DEFERRED_NOEXCEPT:
4248 return (cp_tree_equal (DEFERRED_NOEXCEPT_PATTERN (t1),
4249 DEFERRED_NOEXCEPT_PATTERN (t2))
4250 && comp_template_args (DEFERRED_NOEXCEPT_ARGS (t1),
4251 DEFERRED_NOEXCEPT_ARGS (t2)));
4252
4253 case LAMBDA_EXPR:
4254 /* Two lambda-expressions are never considered equivalent. */
4255 return false;
4256
4257 case TYPE_ARGUMENT_PACK:
4258 case NONTYPE_ARGUMENT_PACK:
4259 {
4260 tree p1 = ARGUMENT_PACK_ARGS (t1);
4261 tree p2 = ARGUMENT_PACK_ARGS (t2);
4262 int len = TREE_VEC_LENGTH (p1);
4263 if (TREE_VEC_LENGTH (p2) != len)
4264 return false;
4265
4266 for (int ix = 0; ix != len; ix++)
4267 if (!template_args_equal (TREE_VEC_ELT (p1, ix),
4268 TREE_VEC_ELT (p2, ix)))
4269 return false;
4270 return true;
4271 }
4272
4273 case EXPR_PACK_EXPANSION:
4274 if (!cp_tree_equal (PACK_EXPANSION_PATTERN (t1),
4275 PACK_EXPANSION_PATTERN (t2)))
4276 return false;
4277 if (!comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
4278 PACK_EXPANSION_EXTRA_ARGS (t2)))
4279 return false;
4280 return true;
4281
4282 case COMPONENT_REF:
4283 /* If we're comparing contract conditions of overrides, member references
4284 compare equal if they designate the same member. */
4285 if (comparing_override_contracts)
4286 return equivalent_member_references (t1, t2);
4287 break;
4288
4289 default:
4290 break;
4291 }
4292
4293 switch (TREE_CODE_CLASS (code1))
4294 {
4295 case tcc_unary:
4296 case tcc_binary:
4297 case tcc_comparison:
4298 case tcc_expression:
4299 case tcc_vl_exp:
4300 case tcc_reference:
4301 case tcc_statement:
4302 {
4303 int n = cp_tree_operand_length (t1);
4304 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
4305 && n != TREE_OPERAND_LENGTH (t2))
4306 return false;
4307
4308 for (int i = 0; i < n; ++i)
4309 if (!cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
4310 return false;
4311
4312 return true;
4313 }
4314
4315 case tcc_type:
4316 return same_type_p (t1, t2);
4317
4318 default:
4319 gcc_unreachable ();
4320 }
4321
4322 /* We can get here with --disable-checking. */
4323 return false;
4324}
4325
4326/* The type of ARG when used as an lvalue. */
4327
4328tree
4329lvalue_type (tree arg)
4330{
4331 tree type = TREE_TYPE (arg);
4332 return type;
4333}
4334
4335/* The type of ARG for printing error messages; denote lvalues with
4336 reference types. */
4337
4338tree
4339error_type (tree arg)
4340{
4341 tree type = TREE_TYPE (arg);
4342
4343 if (TREE_CODE (type) == ARRAY_TYPE)
4344 ;
4345 else if (TREE_CODE (type) == ERROR_MARK)
4346 ;
4347 else if (lvalue_p (t: arg))
4348 type = build_reference_type (lvalue_type (arg));
4349 else if (MAYBE_CLASS_TYPE_P (type))
4350 type = lvalue_type (arg);
4351
4352 return type;
4353}
4354
4355/* Does FUNCTION use a variable-length argument list? */
4356
4357int
4358varargs_function_p (const_tree function)
4359{
4360 return stdarg_p (TREE_TYPE (function));
4361}
4362
4363/* Returns 1 if decl is a member of a class. */
4364
4365int
4366member_p (const_tree decl)
4367{
4368 const_tree const ctx = DECL_CONTEXT (decl);
4369 return (ctx && TYPE_P (ctx));
4370}
4371
4372/* Create a placeholder for member access where we don't actually have an
4373 object that the access is against. For a general declval<T> equivalent,
4374 use build_stub_object instead. */
4375
4376tree
4377build_dummy_object (tree type)
4378{
4379 tree decl = build1 (CONVERT_EXPR, build_pointer_type (type), void_node);
4380 return cp_build_fold_indirect_ref (decl);
4381}
4382
4383/* We've gotten a reference to a member of TYPE. Return *this if appropriate,
4384 or a dummy object otherwise. If BINFOP is non-0, it is filled with the
4385 binfo path from current_class_type to TYPE, or 0. */
4386
4387tree
4388maybe_dummy_object (tree type, tree* binfop)
4389{
4390 tree decl, context;
4391 tree binfo;
4392 tree current = current_nonlambda_class_type ();
4393
4394 if (current
4395 && (binfo = lookup_base (current, type, ba_any, NULL,
4396 tf_warning_or_error)))
4397 context = current;
4398 else
4399 {
4400 /* Reference from a nested class member function. */
4401 context = type;
4402 binfo = TYPE_BINFO (type);
4403 }
4404
4405 if (binfop)
4406 *binfop = binfo;
4407
4408 /* current_class_ref might not correspond to current_class_type if
4409 we're in tsubst_default_argument or a lambda-declarator; in either
4410 case, we want to use current_class_ref if it matches CONTEXT. */
4411 tree ctype = current_class_ref ? TREE_TYPE (current_class_ref) : NULL_TREE;
4412 if (ctype
4413 && same_type_ignoring_top_level_qualifiers_p (ctype, context))
4414 decl = current_class_ref;
4415 else
4416 {
4417 /* Return a dummy object whose cv-quals are consistent with (the
4418 non-lambda) 'this' if available. */
4419 if (ctype)
4420 {
4421 int quals = TYPE_UNQUALIFIED;
4422 if (tree lambda = CLASSTYPE_LAMBDA_EXPR (ctype))
4423 {
4424 if (tree cap = lambda_expr_this_capture (lambda, false))
4425 quals = cp_type_quals (TREE_TYPE (TREE_TYPE (cap)));
4426 }
4427 else
4428 quals = cp_type_quals (ctype);
4429 context = cp_build_qualified_type (type: context, type_quals: quals);
4430 }
4431 decl = build_dummy_object (type: context);
4432 }
4433
4434 return decl;
4435}
4436
4437/* Returns 1 if OB is a placeholder object, or a pointer to one. */
4438
4439bool
4440is_dummy_object (const_tree ob)
4441{
4442 if (INDIRECT_REF_P (ob))
4443 ob = TREE_OPERAND (ob, 0);
4444 return (TREE_CODE (ob) == CONVERT_EXPR
4445 && TREE_OPERAND (ob, 0) == void_node);
4446}
4447
4448/* Returns true if TYPE is char, unsigned char, or std::byte. */
4449
4450bool
4451is_byte_access_type (tree type)
4452{
4453 type = TYPE_MAIN_VARIANT (type);
4454 if (type == char_type_node
4455 || type == unsigned_char_type_node)
4456 return true;
4457
4458 return (TREE_CODE (type) == ENUMERAL_TYPE
4459 && TYPE_CONTEXT (type) == std_node
4460 && !strcmp (s1: "byte", TYPE_NAME_STRING (type)));
4461}
4462
4463/* Returns true if TYPE is unsigned char or std::byte. */
4464
4465bool
4466is_byte_access_type_not_plain_char (tree type)
4467{
4468 type = TYPE_MAIN_VARIANT (type);
4469 if (type == char_type_node)
4470 return false;
4471
4472 return is_byte_access_type (type);
4473}
4474
4475/* Returns 1 iff type T is something we want to treat as a scalar type for
4476 the purpose of deciding whether it is trivial/POD/standard-layout. */
4477
4478bool
4479scalarish_type_p (const_tree t)
4480{
4481 if (t == error_mark_node)
4482 return 1;
4483
4484 return (SCALAR_TYPE_P (t) || VECTOR_TYPE_P (t));
4485}
4486
4487/* Returns true iff T requires non-trivial default initialization. */
4488
4489bool
4490type_has_nontrivial_default_init (const_tree t)
4491{
4492 t = strip_array_types (CONST_CAST_TREE (t));
4493
4494 if (CLASS_TYPE_P (t))
4495 return TYPE_HAS_COMPLEX_DFLT (t);
4496 else
4497 return 0;
4498}
4499
4500/* Track classes with only deleted copy/move constructors so that we can warn
4501 if they are used in call/return by value. */
4502
4503static GTY(()) hash_set<tree>* deleted_copy_types;
4504static void
4505remember_deleted_copy (const_tree t)
4506{
4507 if (!deleted_copy_types)
4508 deleted_copy_types = hash_set<tree>::create_ggc(n: 37);
4509 deleted_copy_types->add (CONST_CAST_TREE (t));
4510}
4511void
4512maybe_warn_parm_abi (tree t, location_t loc)
4513{
4514 if (!deleted_copy_types
4515 || !deleted_copy_types->contains (k: t))
4516 return;
4517
4518 if ((flag_abi_version == 12 || warn_abi_version == 12)
4519 && classtype_has_non_deleted_move_ctor (t))
4520 {
4521 bool w;
4522 auto_diagnostic_group d;
4523 if (flag_abi_version > 12)
4524 w = warning_at (loc, OPT_Wabi, "%<-fabi-version=13%> (GCC 8.2) fixes "
4525 "the calling convention for %qT, which was "
4526 "accidentally changed in 8.1", t);
4527 else
4528 w = warning_at (loc, OPT_Wabi, "%<-fabi-version=12%> (GCC 8.1) "
4529 "accidentally changes the calling convention for %qT",
4530 t);
4531 if (w)
4532 inform (location_of (t), " declared here");
4533 return;
4534 }
4535
4536 auto_diagnostic_group d;
4537 if (warning_at (loc, OPT_Wabi, "the calling convention for %qT changes in "
4538 "%<-fabi-version=13%> (GCC 8.2)", t))
4539 inform (location_of (t), " because all of its copy and move "
4540 "constructors are deleted");
4541}
4542
4543/* Returns true iff copying an object of type T (including via move
4544 constructor) is non-trivial. That is, T has no non-trivial copy
4545 constructors and no non-trivial move constructors, and not all copy/move
4546 constructors are deleted. This function implements the ABI notion of
4547 non-trivial copy, which has diverged from the one in the standard. */
4548
4549bool
4550type_has_nontrivial_copy_init (const_tree type)
4551{
4552 tree t = strip_array_types (CONST_CAST_TREE (type));
4553
4554 if (CLASS_TYPE_P (t))
4555 {
4556 gcc_assert (COMPLETE_TYPE_P (t));
4557
4558 if (TYPE_HAS_COMPLEX_COPY_CTOR (t)
4559 || TYPE_HAS_COMPLEX_MOVE_CTOR (t))
4560 /* Nontrivial. */
4561 return true;
4562
4563 if (cxx_dialect < cxx11)
4564 /* No deleted functions before C++11. */
4565 return false;
4566
4567 /* Before ABI v12 we did a bitwise copy of types with only deleted
4568 copy/move constructors. */
4569 if (!abi_version_at_least (12)
4570 && !(warn_abi && abi_version_crosses (12)))
4571 return false;
4572
4573 bool saw_copy = false;
4574 bool saw_non_deleted = false;
4575 bool saw_non_deleted_move = false;
4576
4577 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
4578 saw_copy = saw_non_deleted = true;
4579 else if (CLASSTYPE_LAZY_COPY_CTOR (t))
4580 {
4581 saw_copy = true;
4582 if (classtype_has_move_assign_or_move_ctor_p (t, user_declared: true))
4583 /* [class.copy]/8 If the class definition declares a move
4584 constructor or move assignment operator, the implicitly declared
4585 copy constructor is defined as deleted.... */;
4586 else
4587 /* Any other reason the implicitly-declared function would be
4588 deleted would also cause TYPE_HAS_COMPLEX_COPY_CTOR to be
4589 set. */
4590 saw_non_deleted = true;
4591 }
4592
4593 if (!saw_non_deleted)
4594 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4595 {
4596 tree fn = *iter;
4597 if (copy_fn_p (fn))
4598 {
4599 saw_copy = true;
4600 if (!DECL_DELETED_FN (fn))
4601 {
4602 /* Not deleted, therefore trivial. */
4603 saw_non_deleted = true;
4604 break;
4605 }
4606 }
4607 else if (move_fn_p (fn))
4608 if (!DECL_DELETED_FN (fn))
4609 saw_non_deleted_move = true;
4610 }
4611
4612 gcc_assert (saw_copy);
4613
4614 /* ABI v12 buggily ignored move constructors. */
4615 bool v11nontriv = false;
4616 bool v12nontriv = !saw_non_deleted;
4617 bool v13nontriv = !saw_non_deleted && !saw_non_deleted_move;
4618 bool nontriv = (abi_version_at_least (13) ? v13nontriv
4619 : flag_abi_version == 12 ? v12nontriv
4620 : v11nontriv);
4621 bool warn_nontriv = (warn_abi_version >= 13 ? v13nontriv
4622 : warn_abi_version == 12 ? v12nontriv
4623 : v11nontriv);
4624 if (nontriv != warn_nontriv)
4625 remember_deleted_copy (t);
4626
4627 return nontriv;
4628 }
4629 else
4630 return 0;
4631}
4632
4633/* Returns 1 iff type T is a trivially copyable type, as defined in
4634 [basic.types] and [class]. */
4635
4636bool
4637trivially_copyable_p (const_tree t)
4638{
4639 t = strip_array_types (CONST_CAST_TREE (t));
4640
4641 if (CLASS_TYPE_P (t))
4642 return ((!TYPE_HAS_COPY_CTOR (t)
4643 || !TYPE_HAS_COMPLEX_COPY_CTOR (t))
4644 && !TYPE_HAS_COMPLEX_MOVE_CTOR (t)
4645 && (!TYPE_HAS_COPY_ASSIGN (t)
4646 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (t))
4647 && !TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
4648 && TYPE_HAS_TRIVIAL_DESTRUCTOR (t));
4649 else
4650 /* CWG 2094 makes volatile-qualified scalars trivially copyable again. */
4651 return scalarish_type_p (t);
4652}
4653
4654/* Returns 1 iff type T is a trivial type, as defined in [basic.types] and
4655 [class]. */
4656
4657bool
4658trivial_type_p (const_tree t)
4659{
4660 t = strip_array_types (CONST_CAST_TREE (t));
4661
4662 if (CLASS_TYPE_P (t))
4663 return (TYPE_HAS_TRIVIAL_DFLT (t)
4664 && trivially_copyable_p (t));
4665 else
4666 return scalarish_type_p (t);
4667}
4668
4669/* Returns 1 iff type T is a POD type, as defined in [basic.types]. */
4670
4671bool
4672pod_type_p (const_tree t)
4673{
4674 /* This CONST_CAST is okay because strip_array_types returns its
4675 argument unmodified and we assign it to a const_tree. */
4676 t = strip_array_types (CONST_CAST_TREE(t));
4677
4678 if (!CLASS_TYPE_P (t))
4679 return scalarish_type_p (t);
4680 else if (cxx_dialect > cxx98)
4681 /* [class]/10: A POD struct is a class that is both a trivial class and a
4682 standard-layout class, and has no non-static data members of type
4683 non-POD struct, non-POD union (or array of such types).
4684
4685 We don't need to check individual members because if a member is
4686 non-std-layout or non-trivial, the class will be too. */
4687 return (std_layout_type_p (t) && trivial_type_p (t));
4688 else
4689 /* The C++98 definition of POD is different. */
4690 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
4691}
4692
4693/* Returns true iff T is POD for the purpose of layout, as defined in the
4694 C++ ABI. */
4695
4696bool
4697layout_pod_type_p (const_tree t)
4698{
4699 t = strip_array_types (CONST_CAST_TREE (t));
4700
4701 if (CLASS_TYPE_P (t))
4702 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
4703 else
4704 return scalarish_type_p (t);
4705}
4706
4707/* Returns true iff T is a standard-layout type, as defined in
4708 [basic.types]. */
4709
4710bool
4711std_layout_type_p (const_tree t)
4712{
4713 t = strip_array_types (CONST_CAST_TREE (t));
4714
4715 if (CLASS_TYPE_P (t))
4716 return !CLASSTYPE_NON_STD_LAYOUT (t);
4717 else
4718 return scalarish_type_p (t);
4719}
4720
4721static bool record_has_unique_obj_representations (const_tree, const_tree);
4722
4723/* Returns true iff T satisfies std::has_unique_object_representations<T>,
4724 as defined in [meta.unary.prop]. */
4725
4726bool
4727type_has_unique_obj_representations (const_tree t)
4728{
4729 bool ret;
4730
4731 t = strip_array_types (CONST_CAST_TREE (t));
4732
4733 if (!trivially_copyable_p (t))
4734 return false;
4735
4736 if (CLASS_TYPE_P (t) && CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t))
4737 return CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t);
4738
4739 switch (TREE_CODE (t))
4740 {
4741 case INTEGER_TYPE:
4742 case POINTER_TYPE:
4743 case REFERENCE_TYPE:
4744 /* If some backend has any paddings in these types, we should add
4745 a target hook for this and handle it there. */
4746 return true;
4747
4748 case BOOLEAN_TYPE:
4749 /* For bool values other than 0 and 1 should only appear with
4750 undefined behavior. */
4751 return true;
4752
4753 case ENUMERAL_TYPE:
4754 return type_has_unique_obj_representations (ENUM_UNDERLYING_TYPE (t));
4755
4756 case REAL_TYPE:
4757 /* XFmode certainly contains padding on x86, which the CPU doesn't store
4758 when storing long double values, so for that we have to return false.
4759 Other kinds of floating point values are questionable due to +.0/-.0
4760 and NaNs, let's play safe for now. */
4761 return false;
4762
4763 case FIXED_POINT_TYPE:
4764 return false;
4765
4766 case OFFSET_TYPE:
4767 return true;
4768
4769 case COMPLEX_TYPE:
4770 case VECTOR_TYPE:
4771 return type_has_unique_obj_representations (TREE_TYPE (t));
4772
4773 case RECORD_TYPE:
4774 ret = record_has_unique_obj_representations (t, TYPE_SIZE (t));
4775 if (CLASS_TYPE_P (t))
4776 {
4777 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
4778 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
4779 }
4780 return ret;
4781
4782 case UNION_TYPE:
4783 ret = true;
4784 bool any_fields;
4785 any_fields = false;
4786 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4787 if (TREE_CODE (field) == FIELD_DECL)
4788 {
4789 any_fields = true;
4790 if (!type_has_unique_obj_representations (TREE_TYPE (field))
4791 || simple_cst_equal (DECL_SIZE (field), TYPE_SIZE (t)) != 1)
4792 {
4793 ret = false;
4794 break;
4795 }
4796 }
4797 if (!any_fields && !integer_zerop (TYPE_SIZE (t)))
4798 ret = false;
4799 if (CLASS_TYPE_P (t))
4800 {
4801 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
4802 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
4803 }
4804 return ret;
4805
4806 case NULLPTR_TYPE:
4807 return false;
4808
4809 case ERROR_MARK:
4810 return false;
4811
4812 default:
4813 gcc_unreachable ();
4814 }
4815}
4816
4817/* Helper function for type_has_unique_obj_representations. */
4818
4819static bool
4820record_has_unique_obj_representations (const_tree t, const_tree sz)
4821{
4822 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4823 if (TREE_CODE (field) != FIELD_DECL)
4824 ;
4825 /* For bases, can't use type_has_unique_obj_representations here, as in
4826 struct S { int i : 24; S (); };
4827 struct T : public S { int j : 8; T (); };
4828 S doesn't have unique obj representations, but T does. */
4829 else if (DECL_FIELD_IS_BASE (field))
4830 {
4831 if (!record_has_unique_obj_representations (TREE_TYPE (field),
4832 DECL_SIZE (field)))
4833 return false;
4834 }
4835 else if (DECL_C_BIT_FIELD (field) && !DECL_UNNAMED_BIT_FIELD (field))
4836 {
4837 tree btype = DECL_BIT_FIELD_TYPE (field);
4838 if (!type_has_unique_obj_representations (t: btype))
4839 return false;
4840 }
4841 else if (!type_has_unique_obj_representations (TREE_TYPE (field)))
4842 return false;
4843
4844 offset_int cur = 0;
4845 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4846 if (TREE_CODE (field) == FIELD_DECL && !DECL_UNNAMED_BIT_FIELD (field))
4847 {
4848 offset_int fld = wi::to_offset (DECL_FIELD_OFFSET (field));
4849 offset_int bitpos = wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
4850 fld = fld * BITS_PER_UNIT + bitpos;
4851 if (cur != fld)
4852 return false;
4853 if (DECL_SIZE (field))
4854 {
4855 offset_int size = wi::to_offset (DECL_SIZE (field));
4856 cur += size;
4857 }
4858 }
4859 if (cur != wi::to_offset (t: sz))
4860 return false;
4861
4862 return true;
4863}
4864
4865/* Nonzero iff type T is a class template implicit specialization. */
4866
4867bool
4868class_tmpl_impl_spec_p (const_tree t)
4869{
4870 return CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INSTANTIATION (t);
4871}
4872
4873/* Returns 1 iff zero initialization of type T means actually storing
4874 zeros in it. */
4875
4876int
4877zero_init_p (const_tree t)
4878{
4879 /* This CONST_CAST is okay because strip_array_types returns its
4880 argument unmodified and we assign it to a const_tree. */
4881 t = strip_array_types (CONST_CAST_TREE(t));
4882
4883 if (t == error_mark_node)
4884 return 1;
4885
4886 /* NULL pointers to data members are initialized with -1. */
4887 if (TYPE_PTRDATAMEM_P (t))
4888 return 0;
4889
4890 /* Classes that contain types that can't be zero-initialized, cannot
4891 be zero-initialized themselves. */
4892 if (CLASS_TYPE_P (t) && CLASSTYPE_NON_ZERO_INIT_P (t))
4893 return 0;
4894
4895 return 1;
4896}
4897
4898/* Returns true if the expression or initializer T is the result of
4899 zero-initialization for its type, taking pointers to members
4900 into consideration. */
4901
4902bool
4903zero_init_expr_p (tree t)
4904{
4905 tree type = TREE_TYPE (t);
4906 if (!type || uses_template_parms (type))
4907 return false;
4908 if (TYPE_PTRMEM_P (type))
4909 return null_member_pointer_value_p (t);
4910 if (TREE_CODE (t) == CONSTRUCTOR)
4911 {
4912 if (COMPOUND_LITERAL_P (t)
4913 || BRACE_ENCLOSED_INITIALIZER_P (t))
4914 /* Undigested, conversions might change the zeroness. */
4915 return false;
4916 for (constructor_elt &elt : CONSTRUCTOR_ELTS (t))
4917 {
4918 if (TREE_CODE (type) == UNION_TYPE
4919 && elt.index != first_field (type))
4920 return false;
4921 if (!zero_init_expr_p (t: elt.value))
4922 return false;
4923 }
4924 return true;
4925 }
4926 if (zero_init_p (t: type))
4927 return initializer_zerop (t);
4928 return false;
4929}
4930
4931/* True IFF T is a C++20 structural type (P1907R1) that can be used as a
4932 non-type template parameter. If EXPLAIN, explain why not. */
4933
4934bool
4935structural_type_p (tree t, bool explain)
4936{
4937 /* A structural type is one of the following: */
4938
4939 /* a scalar type, or */
4940 if (SCALAR_TYPE_P (t))
4941 return true;
4942 /* an lvalue reference type, or */
4943 if (TYPE_REF_P (t) && !TYPE_REF_IS_RVALUE (t))
4944 return true;
4945 /* a literal class type with the following properties:
4946 - all base classes and non-static data members are public and non-mutable
4947 and
4948 - the types of all bases classes and non-static data members are
4949 structural types or (possibly multi-dimensional) array thereof. */
4950 if (!CLASS_TYPE_P (t))
4951 return false;
4952 if (!literal_type_p (t))
4953 {
4954 if (explain)
4955 explain_non_literal_class (t);
4956 return false;
4957 }
4958 for (tree m = next_aggregate_field (TYPE_FIELDS (t)); m;
4959 m = next_aggregate_field (DECL_CHAIN (m)))
4960 {
4961 if (TREE_PRIVATE (m) || TREE_PROTECTED (m))
4962 {
4963 if (explain)
4964 {
4965 if (DECL_FIELD_IS_BASE (m))
4966 inform (location_of (m), "base class %qT is not public",
4967 TREE_TYPE (m));
4968 else
4969 inform (location_of (m), "%qD is not public", m);
4970 }
4971 return false;
4972 }
4973 if (DECL_MUTABLE_P (m))
4974 {
4975 if (explain)
4976 inform (location_of (m), "%qD is mutable", m);
4977 return false;
4978 }
4979 tree mtype = strip_array_types (TREE_TYPE (m));
4980 if (!structural_type_p (t: mtype))
4981 {
4982 if (explain)
4983 {
4984 inform (location_of (m), "%qD has a non-structural type", m);
4985 structural_type_p (t: mtype, explain: true);
4986 }
4987 return false;
4988 }
4989 }
4990 return true;
4991}
4992
4993/* Partially handle the C++11 [[carries_dependency]] attribute.
4994 Just emit a different diagnostics when it is used on something the
4995 spec doesn't allow vs. where it allows and we just choose to ignore
4996 it. */
4997
4998static tree
4999handle_carries_dependency_attribute (tree *node, tree name,
5000 tree ARG_UNUSED (args),
5001 int ARG_UNUSED (flags),
5002 bool *no_add_attrs)
5003{
5004 if (TREE_CODE (*node) != FUNCTION_DECL
5005 && TREE_CODE (*node) != PARM_DECL)
5006 {
5007 warning (OPT_Wattributes, "%qE attribute can only be applied to "
5008 "functions or parameters", name);
5009 *no_add_attrs = true;
5010 }
5011 else
5012 {
5013 warning (OPT_Wattributes, "%qE attribute ignored", name);
5014 *no_add_attrs = true;
5015 }
5016 return NULL_TREE;
5017}
5018
5019/* Handle the C++17 [[nodiscard]] attribute, which is similar to the GNU
5020 warn_unused_result attribute. */
5021
5022static tree
5023handle_nodiscard_attribute (tree *node, tree name, tree args,
5024 int /*flags*/, bool *no_add_attrs)
5025{
5026 if (args && TREE_CODE (TREE_VALUE (args)) != STRING_CST)
5027 {
5028 error ("%qE attribute argument must be a string constant", name);
5029 *no_add_attrs = true;
5030 }
5031 if (TREE_CODE (*node) == FUNCTION_DECL)
5032 {
5033 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node)))
5034 && !DECL_CONSTRUCTOR_P (*node))
5035 warning_at (DECL_SOURCE_LOCATION (*node),
5036 OPT_Wattributes, "%qE attribute applied to %qD with void "
5037 "return type", name, *node);
5038 }
5039 else if (OVERLOAD_TYPE_P (*node))
5040 /* OK */;
5041 else
5042 {
5043 warning (OPT_Wattributes, "%qE attribute can only be applied to "
5044 "functions or to class or enumeration types", name);
5045 *no_add_attrs = true;
5046 }
5047 return NULL_TREE;
5048}
5049
5050/* Handle a C++20 "no_unique_address" attribute; arguments as in
5051 struct attribute_spec.handler. */
5052static tree
5053handle_no_unique_addr_attribute (tree* node,
5054 tree name,
5055 tree /*args*/,
5056 int /*flags*/,
5057 bool* no_add_attrs)
5058{
5059 if (TREE_CODE (*node) == VAR_DECL)
5060 {
5061 DECL_MERGEABLE (*node) = true;
5062 if (pedantic)
5063 warning (OPT_Wattributes, "%qE attribute can only be applied to "
5064 "non-static data members", name);
5065 }
5066 else if (TREE_CODE (*node) != FIELD_DECL)
5067 {
5068 warning (OPT_Wattributes, "%qE attribute can only be applied to "
5069 "non-static data members", name);
5070 *no_add_attrs = true;
5071 }
5072 else if (DECL_C_BIT_FIELD (*node))
5073 {
5074 warning (OPT_Wattributes, "%qE attribute cannot be applied to "
5075 "a bit-field", name);
5076 *no_add_attrs = true;
5077 }
5078
5079 return NULL_TREE;
5080}
5081
5082/* The C++20 [[likely]] and [[unlikely]] attributes on labels map to the GNU
5083 hot/cold attributes. */
5084
5085static tree
5086handle_likeliness_attribute (tree *node, tree name, tree args,
5087 int flags, bool *no_add_attrs)
5088{
5089 *no_add_attrs = true;
5090 if (TREE_CODE (*node) == LABEL_DECL
5091 || TREE_CODE (*node) == FUNCTION_DECL)
5092 {
5093 if (args)
5094 warning (OPT_Wattributes, "%qE attribute takes no arguments", name);
5095 tree bname = (is_attribute_p (attr_name: "likely", ident: name)
5096 ? get_identifier ("hot") : get_identifier ("cold"));
5097 if (TREE_CODE (*node) == FUNCTION_DECL)
5098 warning (OPT_Wattributes, "ISO C++ %qE attribute does not apply to "
5099 "functions; treating as %<[[gnu::%E]]%>", name, bname);
5100 tree battr = build_tree_list (bname, NULL_TREE);
5101 decl_attributes (node, battr, flags);
5102 return NULL_TREE;
5103 }
5104 else
5105 return error_mark_node;
5106}
5107
5108/* Table of valid C++ attributes. */
5109static const attribute_spec cxx_gnu_attributes[] =
5110{
5111 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
5112 affects_type_identity, handler, exclude } */
5113 { .name: "init_priority", .min_length: 1, .max_length: 1, .decl_required: true, .type_required: false, .function_type_required: false, .affects_type_identity: false,
5114 .handler: handle_init_priority_attribute, NULL },
5115 { .name: "abi_tag", .min_length: 1, .max_length: -1, .decl_required: false, .type_required: false, .function_type_required: false, .affects_type_identity: true,
5116 .handler: handle_abi_tag_attribute, NULL },
5117 { .name: "no_dangling", .min_length: 0, .max_length: 1, .decl_required: false, .type_required: true, .function_type_required: false, .affects_type_identity: false,
5118 .handler: handle_no_dangling_attribute, NULL },
5119};
5120
5121const scoped_attribute_specs cxx_gnu_attribute_table =
5122{
5123 .ns: "gnu", .attributes: { cxx_gnu_attributes }
5124};
5125
5126/* Table of C++ standard attributes. */
5127static const attribute_spec std_attributes[] =
5128{
5129 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
5130 affects_type_identity, handler, exclude } */
5131 { .name: "maybe_unused", .min_length: 0, .max_length: 0, .decl_required: false, .type_required: false, .function_type_required: false, .affects_type_identity: false,
5132 .handler: handle_unused_attribute, NULL },
5133 { .name: "nodiscard", .min_length: 0, .max_length: 1, .decl_required: false, .type_required: false, .function_type_required: false, .affects_type_identity: false,
5134 .handler: handle_nodiscard_attribute, NULL },
5135 { .name: "no_unique_address", .min_length: 0, .max_length: 0, .decl_required: true, .type_required: false, .function_type_required: false, .affects_type_identity: false,
5136 .handler: handle_no_unique_addr_attribute, NULL },
5137 { .name: "likely", .min_length: 0, .max_length: 0, .decl_required: false, .type_required: false, .function_type_required: false, .affects_type_identity: false,
5138 .handler: handle_likeliness_attribute, .exclude: attr_cold_hot_exclusions },
5139 { .name: "unlikely", .min_length: 0, .max_length: 0, .decl_required: false, .type_required: false, .function_type_required: false, .affects_type_identity: false,
5140 .handler: handle_likeliness_attribute, .exclude: attr_cold_hot_exclusions },
5141 { .name: "noreturn", .min_length: 0, .max_length: 0, .decl_required: true, .type_required: false, .function_type_required: false, .affects_type_identity: false,
5142 .handler: handle_noreturn_attribute, .exclude: attr_noreturn_exclusions },
5143 { .name: "carries_dependency", .min_length: 0, .max_length: 0, .decl_required: true, .type_required: false, .function_type_required: false, .affects_type_identity: false,
5144 .handler: handle_carries_dependency_attribute, NULL },
5145 { .name: "pre", .min_length: 0, .max_length: -1, .decl_required: false, .type_required: false, .function_type_required: false, .affects_type_identity: false,
5146 .handler: handle_contract_attribute, NULL },
5147 { .name: "post", .min_length: 0, .max_length: -1, .decl_required: false, .type_required: false, .function_type_required: false, .affects_type_identity: false,
5148 .handler: handle_contract_attribute, NULL }
5149};
5150
5151const scoped_attribute_specs std_attribute_table =
5152{
5153 .ns: nullptr, .attributes: { std_attributes }
5154};
5155
5156/* Handle an "init_priority" attribute; arguments as in
5157 struct attribute_spec.handler. */
5158static tree
5159handle_init_priority_attribute (tree* node,
5160 tree name,
5161 tree args,
5162 int /*flags*/,
5163 bool* no_add_attrs)
5164{
5165 if (!SUPPORTS_INIT_PRIORITY)
5166 /* Treat init_priority as an unrecognized attribute (mirroring
5167 __has_attribute) if the target doesn't support init priorities. */
5168 return error_mark_node;
5169
5170 tree initp_expr = TREE_VALUE (args);
5171 tree decl = *node;
5172 tree type = TREE_TYPE (decl);
5173 int pri;
5174
5175 STRIP_NOPS (initp_expr);
5176 initp_expr = default_conversion (initp_expr);
5177 if (initp_expr)
5178 initp_expr = maybe_constant_value (initp_expr);
5179
5180 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
5181 {
5182 error ("requested %<init_priority%> is not an integer constant");
5183 cxx_constant_value (initp_expr);
5184 *no_add_attrs = true;
5185 return NULL_TREE;
5186 }
5187
5188 pri = TREE_INT_CST_LOW (initp_expr);
5189
5190 type = strip_array_types (type);
5191
5192 if (decl == NULL_TREE
5193 || !VAR_P (decl)
5194 || !TREE_STATIC (decl)
5195 || DECL_EXTERNAL (decl)
5196 || (TREE_CODE (type) != RECORD_TYPE
5197 && TREE_CODE (type) != UNION_TYPE)
5198 /* Static objects in functions are initialized the
5199 first time control passes through that
5200 function. This is not precise enough to pin down an
5201 init_priority value, so don't allow it. */
5202 || current_function_decl)
5203 {
5204 error ("can only use %qE attribute on file-scope definitions "
5205 "of objects of class type", name);
5206 *no_add_attrs = true;
5207 return NULL_TREE;
5208 }
5209
5210 if (pri > MAX_INIT_PRIORITY || pri <= 0)
5211 {
5212 error ("requested %<init_priority%> %i is out of range [0, %i]",
5213 pri, MAX_INIT_PRIORITY);
5214 *no_add_attrs = true;
5215 return NULL_TREE;
5216 }
5217
5218 /* Check for init_priorities that are reserved for
5219 language and runtime support implementations.*/
5220 if (pri <= MAX_RESERVED_INIT_PRIORITY)
5221 {
5222 warning
5223 (0, "requested %<init_priority%> %i is reserved for internal use",
5224 pri);
5225 }
5226
5227 SET_DECL_INIT_PRIORITY (decl, pri);
5228 DECL_HAS_INIT_PRIORITY_P (decl) = 1;
5229 return NULL_TREE;
5230}
5231
5232/* DECL is being redeclared; the old declaration had the abi tags in OLD,
5233 and the new one has the tags in NEW_. Give an error if there are tags
5234 in NEW_ that weren't in OLD. */
5235
5236bool
5237check_abi_tag_redeclaration (const_tree decl, const_tree old, const_tree new_)
5238{
5239 if (old && TREE_CODE (TREE_VALUE (old)) == TREE_LIST)
5240 old = TREE_VALUE (old);
5241 if (new_ && TREE_CODE (TREE_VALUE (new_)) == TREE_LIST)
5242 new_ = TREE_VALUE (new_);
5243 bool err = false;
5244 for (const_tree t = new_; t; t = TREE_CHAIN (t))
5245 {
5246 tree str = TREE_VALUE (t);
5247 for (const_tree in = old; in; in = TREE_CHAIN (in))
5248 {
5249 tree ostr = TREE_VALUE (in);
5250 if (cp_tree_equal (t1: str, t2: ostr))
5251 goto found;
5252 }
5253 error ("redeclaration of %qD adds abi tag %qE", decl, str);
5254 err = true;
5255 found:;
5256 }
5257 if (err)
5258 {
5259 inform (DECL_SOURCE_LOCATION (decl), "previous declaration here");
5260 return false;
5261 }
5262 return true;
5263}
5264
5265/* The abi_tag attribute with the name NAME was given ARGS. If they are
5266 ill-formed, give an error and return false; otherwise, return true. */
5267
5268bool
5269check_abi_tag_args (tree args, tree name)
5270{
5271 if (!args)
5272 {
5273 error ("the %qE attribute requires arguments", name);
5274 return false;
5275 }
5276 for (tree arg = args; arg; arg = TREE_CHAIN (arg))
5277 {
5278 tree elt = TREE_VALUE (arg);
5279 if (TREE_CODE (elt) != STRING_CST
5280 || (!same_type_ignoring_top_level_qualifiers_p
5281 (strip_array_types (TREE_TYPE (elt)),
5282 char_type_node)))
5283 {
5284 error ("arguments to the %qE attribute must be narrow string "
5285 "literals", name);
5286 return false;
5287 }
5288 const char *begin = TREE_STRING_POINTER (elt);
5289 const char *end = begin + TREE_STRING_LENGTH (elt);
5290 for (const char *p = begin; p != end; ++p)
5291 {
5292 char c = *p;
5293 if (p == begin)
5294 {
5295 if (!ISALPHA (c) && c != '_')
5296 {
5297 error ("arguments to the %qE attribute must contain valid "
5298 "identifiers", name);
5299 inform (input_location, "%<%c%> is not a valid first "
5300 "character for an identifier", c);
5301 return false;
5302 }
5303 }
5304 else if (p == end - 1)
5305 gcc_assert (c == 0);
5306 else
5307 {
5308 if (!ISALNUM (c) && c != '_')
5309 {
5310 error ("arguments to the %qE attribute must contain valid "
5311 "identifiers", name);
5312 inform (input_location, "%<%c%> is not a valid character "
5313 "in an identifier", c);
5314 return false;
5315 }
5316 }
5317 }
5318 }
5319 return true;
5320}
5321
5322/* Handle an "abi_tag" attribute; arguments as in
5323 struct attribute_spec.handler. */
5324
5325static tree
5326handle_abi_tag_attribute (tree* node, tree name, tree args,
5327 int flags, bool* no_add_attrs)
5328{
5329 if (!check_abi_tag_args (args, name))
5330 goto fail;
5331
5332 if (TYPE_P (*node))
5333 {
5334 if (!OVERLOAD_TYPE_P (*node))
5335 {
5336 error ("%qE attribute applied to non-class, non-enum type %qT",
5337 name, *node);
5338 goto fail;
5339 }
5340 else if (!(flags & (int)ATTR_FLAG_TYPE_IN_PLACE))
5341 {
5342 error ("%qE attribute applied to %qT after its definition",
5343 name, *node);
5344 goto fail;
5345 }
5346 else if (CLASS_TYPE_P (*node)
5347 && CLASSTYPE_TEMPLATE_INSTANTIATION (*node))
5348 {
5349 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
5350 "template instantiation %qT", name, *node);
5351 goto fail;
5352 }
5353 else if (CLASS_TYPE_P (*node)
5354 && CLASSTYPE_TEMPLATE_SPECIALIZATION (*node))
5355 {
5356 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
5357 "template specialization %qT", name, *node);
5358 goto fail;
5359 }
5360
5361 tree attributes = TYPE_ATTRIBUTES (*node);
5362 tree decl = TYPE_NAME (*node);
5363
5364 /* Make sure all declarations have the same abi tags. */
5365 if (DECL_SOURCE_LOCATION (decl) != input_location)
5366 {
5367 if (!check_abi_tag_redeclaration (decl,
5368 old: lookup_attribute (attr_name: "abi_tag",
5369 list: attributes),
5370 new_: args))
5371 goto fail;
5372 }
5373 }
5374 else
5375 {
5376 if (!VAR_OR_FUNCTION_DECL_P (*node))
5377 {
5378 error ("%qE attribute applied to non-function, non-variable %qD",
5379 name, *node);
5380 goto fail;
5381 }
5382 else if (DECL_LANGUAGE (*node) == lang_c)
5383 {
5384 error ("%qE attribute applied to extern \"C\" declaration %qD",
5385 name, *node);
5386 goto fail;
5387 }
5388 }
5389
5390 return NULL_TREE;
5391
5392 fail:
5393 *no_add_attrs = true;
5394 return NULL_TREE;
5395}
5396
5397/* Perform checking for contract attributes. */
5398
5399tree
5400handle_contract_attribute (tree *ARG_UNUSED (node), tree ARG_UNUSED (name),
5401 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
5402 bool *ARG_UNUSED (no_add_attrs))
5403{
5404 /* TODO: Is there any checking we could do here? */
5405 return NULL_TREE;
5406}
5407
5408/* Handle a "no_dangling" attribute; arguments as in
5409 struct attribute_spec.handler. */
5410
5411tree
5412handle_no_dangling_attribute (tree *node, tree name, tree args, int,
5413 bool *no_add_attrs)
5414{
5415 if (args && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
5416 {
5417 error ("%qE attribute argument must be an expression that evaluates "
5418 "to true or false", name);
5419 *no_add_attrs = true;
5420 }
5421 else if (!FUNC_OR_METHOD_TYPE_P (*node)
5422 && !RECORD_OR_UNION_TYPE_P (*node))
5423 {
5424 warning (OPT_Wattributes, "%qE attribute ignored", name);
5425 *no_add_attrs = true;
5426 }
5427
5428 return NULL_TREE;
5429}
5430
5431/* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the
5432 thing pointed to by the constant. */
5433
5434tree
5435make_ptrmem_cst (tree type, tree member)
5436{
5437 tree ptrmem_cst = make_node (PTRMEM_CST);
5438 TREE_TYPE (ptrmem_cst) = type;
5439 PTRMEM_CST_MEMBER (ptrmem_cst) = member;
5440 PTRMEM_CST_LOCATION (ptrmem_cst) = input_location;
5441 return ptrmem_cst;
5442}
5443
5444/* Build a variant of TYPE that has the indicated ATTRIBUTES. May
5445 return an existing type if an appropriate type already exists. */
5446
5447tree
5448cp_build_type_attribute_variant (tree type, tree attributes)
5449{
5450 tree new_type;
5451
5452 new_type = build_type_attribute_variant (type, attributes);
5453 if (FUNC_OR_METHOD_TYPE_P (new_type))
5454 gcc_checking_assert (cxx_type_hash_eq (type, new_type));
5455
5456 /* Making a new main variant of a class type is broken. */
5457 gcc_assert (!CLASS_TYPE_P (type) || new_type == type);
5458
5459 return new_type;
5460}
5461
5462/* Return TRUE if TYPE1 and TYPE2 are identical for type hashing purposes.
5463 Called only after doing all language independent checks. */
5464
5465bool
5466cxx_type_hash_eq (const_tree typea, const_tree typeb)
5467{
5468 gcc_assert (FUNC_OR_METHOD_TYPE_P (typea));
5469
5470 if (type_memfn_rqual (typea) != type_memfn_rqual (typeb))
5471 return false;
5472 if (TYPE_HAS_LATE_RETURN_TYPE (typea) != TYPE_HAS_LATE_RETURN_TYPE (typeb))
5473 return false;
5474 return comp_except_specs (TYPE_RAISES_EXCEPTIONS (typea),
5475 TYPE_RAISES_EXCEPTIONS (typeb), ce_exact);
5476}
5477
5478/* Copy the language-specific type variant modifiers from TYPEB to TYPEA. For
5479 C++, these are the exception-specifier and ref-qualifier. */
5480
5481tree
5482cxx_copy_lang_qualifiers (const_tree typea, const_tree typeb)
5483{
5484 tree type = CONST_CAST_TREE (typea);
5485 if (FUNC_OR_METHOD_TYPE_P (type))
5486 type = build_cp_fntype_variant (type, rqual: type_memfn_rqual (typeb),
5487 TYPE_RAISES_EXCEPTIONS (typeb),
5488 TYPE_HAS_LATE_RETURN_TYPE (typeb));
5489 return type;
5490}
5491
5492/* Apply FUNC to all language-specific sub-trees of TP in a pre-order
5493 traversal. Called from walk_tree. */
5494
5495tree
5496cp_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func,
5497 void *data, hash_set<tree> *pset)
5498{
5499 tree t = *tp;
5500 enum tree_code code = TREE_CODE (t);
5501 tree result;
5502
5503#define WALK_SUBTREE(NODE) \
5504 do \
5505 { \
5506 result = cp_walk_tree (&(NODE), func, data, pset); \
5507 if (result) goto out; \
5508 } \
5509 while (0)
5510
5511 if (TYPE_P (t))
5512 {
5513 /* If *WALK_SUBTREES_P is 1, we're interested in the syntactic form of
5514 the argument, so don't look through typedefs, but do walk into
5515 template arguments for alias templates (and non-typedefed classes).
5516
5517 If *WALK_SUBTREES_P > 1, we're interested in type identity or
5518 equivalence, so look through typedefs, ignoring template arguments for
5519 alias templates, and walk into template args of classes.
5520
5521 See find_abi_tags_r for an example of setting *WALK_SUBTREES_P to 2
5522 when that's the behavior the walk_tree_fn wants. */
5523 if (*walk_subtrees_p == 1 && typedef_variant_p (type: t))
5524 {
5525 if (tree ti = TYPE_ALIAS_TEMPLATE_INFO (t))
5526 WALK_SUBTREE (TI_ARGS (ti));
5527 *walk_subtrees_p = 0;
5528 return NULL_TREE;
5529 }
5530
5531 if (tree ti = TYPE_TEMPLATE_INFO (t))
5532 WALK_SUBTREE (TI_ARGS (ti));
5533 }
5534
5535 /* Not one of the easy cases. We must explicitly go through the
5536 children. */
5537 result = NULL_TREE;
5538 switch (code)
5539 {
5540 case TEMPLATE_TYPE_PARM:
5541 if (template_placeholder_p (t))
5542 WALK_SUBTREE (CLASS_PLACEHOLDER_TEMPLATE (t));
5543 /* Fall through. */
5544 case DEFERRED_PARSE:
5545 case TEMPLATE_TEMPLATE_PARM:
5546 case BOUND_TEMPLATE_TEMPLATE_PARM:
5547 case UNBOUND_CLASS_TEMPLATE:
5548 case TEMPLATE_PARM_INDEX:
5549 case TYPEOF_TYPE:
5550 /* None of these have subtrees other than those already walked
5551 above. */
5552 *walk_subtrees_p = 0;
5553 break;
5554
5555 case TYPENAME_TYPE:
5556 WALK_SUBTREE (TYPE_CONTEXT (t));
5557 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
5558 *walk_subtrees_p = 0;
5559 break;
5560
5561 case BASELINK:
5562 if (BASELINK_QUALIFIED_P (t))
5563 WALK_SUBTREE (BINFO_TYPE (BASELINK_ACCESS_BINFO (t)));
5564 WALK_SUBTREE (BASELINK_FUNCTIONS (t));
5565 *walk_subtrees_p = 0;
5566 break;
5567
5568 case PTRMEM_CST:
5569 WALK_SUBTREE (TREE_TYPE (t));
5570 *walk_subtrees_p = 0;
5571 break;
5572
5573 case TREE_LIST:
5574 WALK_SUBTREE (TREE_PURPOSE (t));
5575 break;
5576
5577 case OVERLOAD:
5578 WALK_SUBTREE (OVL_FUNCTION (t));
5579 WALK_SUBTREE (OVL_CHAIN (t));
5580 *walk_subtrees_p = 0;
5581 break;
5582
5583 case USING_DECL:
5584 WALK_SUBTREE (DECL_NAME (t));
5585 WALK_SUBTREE (USING_DECL_SCOPE (t));
5586 WALK_SUBTREE (USING_DECL_DECLS (t));
5587 *walk_subtrees_p = 0;
5588 break;
5589
5590 case RECORD_TYPE:
5591 if (TYPE_PTRMEMFUNC_P (t))
5592 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (t));
5593 break;
5594
5595 case TYPE_ARGUMENT_PACK:
5596 case NONTYPE_ARGUMENT_PACK:
5597 {
5598 tree args = ARGUMENT_PACK_ARGS (t);
5599 for (tree arg : tree_vec_range (args))
5600 WALK_SUBTREE (arg);
5601 }
5602 break;
5603
5604 case TYPE_PACK_EXPANSION:
5605 WALK_SUBTREE (TREE_TYPE (t));
5606 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (t));
5607 *walk_subtrees_p = 0;
5608 break;
5609
5610 case EXPR_PACK_EXPANSION:
5611 WALK_SUBTREE (TREE_OPERAND (t, 0));
5612 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (t));
5613 *walk_subtrees_p = 0;
5614 break;
5615
5616 case CAST_EXPR:
5617 case REINTERPRET_CAST_EXPR:
5618 case STATIC_CAST_EXPR:
5619 case CONST_CAST_EXPR:
5620 case DYNAMIC_CAST_EXPR:
5621 case IMPLICIT_CONV_EXPR:
5622 case BIT_CAST_EXPR:
5623 if (TREE_TYPE (t))
5624 WALK_SUBTREE (TREE_TYPE (t));
5625 break;
5626
5627 case CONSTRUCTOR:
5628 if (COMPOUND_LITERAL_P (t))
5629 WALK_SUBTREE (TREE_TYPE (t));
5630 break;
5631
5632 case TRAIT_EXPR:
5633 WALK_SUBTREE (TRAIT_EXPR_TYPE1 (t));
5634 WALK_SUBTREE (TRAIT_EXPR_TYPE2 (t));
5635 *walk_subtrees_p = 0;
5636 break;
5637
5638 case TRAIT_TYPE:
5639 WALK_SUBTREE (TRAIT_TYPE_TYPE1 (t));
5640 WALK_SUBTREE (TRAIT_TYPE_TYPE2 (t));
5641 *walk_subtrees_p = 0;
5642 break;
5643
5644 case DECLTYPE_TYPE:
5645 {
5646 cp_unevaluated u;
5647 WALK_SUBTREE (DECLTYPE_TYPE_EXPR (t));
5648 *walk_subtrees_p = 0;
5649 break;
5650 }
5651
5652 case ALIGNOF_EXPR:
5653 case SIZEOF_EXPR:
5654 case NOEXCEPT_EXPR:
5655 {
5656 cp_unevaluated u;
5657 WALK_SUBTREE (TREE_OPERAND (t, 0));
5658 *walk_subtrees_p = 0;
5659 break;
5660 }
5661
5662 case REQUIRES_EXPR:
5663 {
5664 cp_unevaluated u;
5665 for (tree parm = REQUIRES_EXPR_PARMS (t); parm; parm = DECL_CHAIN (parm))
5666 /* Walk the types of each parameter, but not the parameter itself,
5667 since doing so would cause false positives in the unexpanded pack
5668 checker if the requires-expr introduces a function parameter pack,
5669 e.g. requires (Ts... ts) { }. */
5670 WALK_SUBTREE (TREE_TYPE (parm));
5671 WALK_SUBTREE (REQUIRES_EXPR_REQS (t));
5672 *walk_subtrees_p = 0;
5673 break;
5674 }
5675
5676 case DECL_EXPR:
5677 /* User variables should be mentioned in BIND_EXPR_VARS
5678 and their initializers and sizes walked when walking
5679 the containing BIND_EXPR. Compiler temporaries are
5680 handled here. And also normal variables in templates,
5681 since do_poplevel doesn't build a BIND_EXPR then. */
5682 if (VAR_P (TREE_OPERAND (t, 0))
5683 && (processing_template_decl
5684 || (DECL_ARTIFICIAL (TREE_OPERAND (t, 0))
5685 && !TREE_STATIC (TREE_OPERAND (t, 0)))))
5686 {
5687 tree decl = TREE_OPERAND (t, 0);
5688 WALK_SUBTREE (DECL_INITIAL (decl));
5689 WALK_SUBTREE (DECL_SIZE (decl));
5690 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
5691 }
5692 break;
5693
5694 case LAMBDA_EXPR:
5695 /* Don't walk into the body of the lambda, but the capture initializers
5696 are part of the enclosing context. */
5697 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
5698 cap = TREE_CHAIN (cap))
5699 WALK_SUBTREE (TREE_VALUE (cap));
5700 break;
5701
5702 case CO_YIELD_EXPR:
5703 if (TREE_OPERAND (t, 1))
5704 /* Operand 1 is the tree for the relevant co_await which has any
5705 interesting sub-trees. */
5706 WALK_SUBTREE (TREE_OPERAND (t, 1));
5707 break;
5708
5709 case CO_AWAIT_EXPR:
5710 if (TREE_OPERAND (t, 1))
5711 /* Operand 1 is frame variable. */
5712 WALK_SUBTREE (TREE_OPERAND (t, 1));
5713 if (TREE_OPERAND (t, 2))
5714 /* Operand 2 has the initialiser, and we need to walk any subtrees
5715 there. */
5716 WALK_SUBTREE (TREE_OPERAND (t, 2));
5717 break;
5718
5719 case CO_RETURN_EXPR:
5720 if (TREE_OPERAND (t, 0))
5721 {
5722 if (VOID_TYPE_P (TREE_OPERAND (t, 0)))
5723 /* For void expressions, operand 1 is a trivial call, and any
5724 interesting subtrees will be part of operand 0. */
5725 WALK_SUBTREE (TREE_OPERAND (t, 0));
5726 else if (TREE_OPERAND (t, 1))
5727 /* Interesting sub-trees will be in the return_value () call
5728 arguments. */
5729 WALK_SUBTREE (TREE_OPERAND (t, 1));
5730 }
5731 break;
5732
5733 case STATIC_ASSERT:
5734 WALK_SUBTREE (STATIC_ASSERT_CONDITION (t));
5735 WALK_SUBTREE (STATIC_ASSERT_MESSAGE (t));
5736 break;
5737
5738 default:
5739 return NULL_TREE;
5740 }
5741
5742 /* We didn't find what we were looking for. */
5743 out:
5744 return result;
5745
5746#undef WALK_SUBTREE
5747}
5748
5749/* Like save_expr, but for C++. */
5750
5751tree
5752cp_save_expr (tree expr)
5753{
5754 /* There is no reason to create a SAVE_EXPR within a template; if
5755 needed, we can create the SAVE_EXPR when instantiating the
5756 template. Furthermore, the middle-end cannot handle C++-specific
5757 tree codes. */
5758 if (processing_template_decl)
5759 return expr;
5760
5761 /* TARGET_EXPRs are only expanded once. */
5762 if (TREE_CODE (expr) == TARGET_EXPR)
5763 return expr;
5764
5765 return save_expr (expr);
5766}
5767
5768/* Initialize tree.cc. */
5769
5770void
5771init_tree (void)
5772{
5773 list_hash_table = hash_table<list_hasher>::create_ggc (n: 61);
5774}
5775
5776/* Returns the kind of special function that DECL (a FUNCTION_DECL)
5777 is. Note that sfk_none is zero, so this function can be used as a
5778 predicate to test whether or not DECL is a special function. */
5779
5780special_function_kind
5781special_function_p (const_tree decl)
5782{
5783 /* Rather than doing all this stuff with magic names, we should
5784 probably have a field of type `special_function_kind' in
5785 DECL_LANG_SPECIFIC. */
5786 if (DECL_INHERITED_CTOR (decl))
5787 return sfk_inheriting_constructor;
5788 if (DECL_COPY_CONSTRUCTOR_P (decl))
5789 return sfk_copy_constructor;
5790 if (DECL_MOVE_CONSTRUCTOR_P (decl))
5791 return sfk_move_constructor;
5792 if (DECL_CONSTRUCTOR_P (decl))
5793 return sfk_constructor;
5794 if (DECL_ASSIGNMENT_OPERATOR_P (decl)
5795 && DECL_OVERLOADED_OPERATOR_IS (decl, NOP_EXPR))
5796 {
5797 if (copy_fn_p (decl))
5798 return sfk_copy_assignment;
5799 if (move_fn_p (decl))
5800 return sfk_move_assignment;
5801 }
5802 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
5803 return sfk_destructor;
5804 if (DECL_COMPLETE_DESTRUCTOR_P (decl))
5805 return sfk_complete_destructor;
5806 if (DECL_BASE_DESTRUCTOR_P (decl))
5807 return sfk_base_destructor;
5808 if (DECL_DELETING_DESTRUCTOR_P (decl))
5809 return sfk_deleting_destructor;
5810 if (DECL_CONV_FN_P (decl))
5811 return sfk_conversion;
5812 if (deduction_guide_p (decl))
5813 return sfk_deduction_guide;
5814 if (DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) >= OVL_OP_EQ_EXPR
5815 && DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) <= OVL_OP_SPACESHIP_EXPR)
5816 return sfk_comparison;
5817
5818 return sfk_none;
5819}
5820
5821/* As above, but only if DECL is a special member function as per 11.3.3
5822 [special]: default/copy/move ctor, copy/move assignment, or destructor. */
5823
5824special_function_kind
5825special_memfn_p (const_tree decl)
5826{
5827 switch (special_function_kind sfk = special_function_p (decl))
5828 {
5829 case sfk_constructor:
5830 if (!default_ctor_p (decl))
5831 break;
5832 gcc_fallthrough();
5833 case sfk_copy_constructor:
5834 case sfk_copy_assignment:
5835 case sfk_move_assignment:
5836 case sfk_move_constructor:
5837 case sfk_destructor:
5838 return sfk;
5839
5840 default:
5841 break;
5842 }
5843 return sfk_none;
5844}
5845
5846/* Returns nonzero if TYPE is a character type, including wchar_t. */
5847
5848int
5849char_type_p (tree type)
5850{
5851 return (same_type_p (type, char_type_node)
5852 || same_type_p (type, unsigned_char_type_node)
5853 || same_type_p (type, signed_char_type_node)
5854 || same_type_p (type, char8_type_node)
5855 || same_type_p (type, char16_type_node)
5856 || same_type_p (type, char32_type_node)
5857 || same_type_p (type, wchar_type_node));
5858}
5859
5860/* Returns the kind of linkage associated with the indicated DECL. Th
5861 value returned is as specified by the language standard; it is
5862 independent of implementation details regarding template
5863 instantiation, etc. For example, it is possible that a declaration
5864 to which this function assigns external linkage would not show up
5865 as a global symbol when you run `nm' on the resulting object file. */
5866
5867linkage_kind
5868decl_linkage (tree decl)
5869{
5870 /* This function doesn't attempt to calculate the linkage from first
5871 principles as given in [basic.link]. Instead, it makes use of
5872 the fact that we have already set TREE_PUBLIC appropriately, and
5873 then handles a few special cases. Ideally, we would calculate
5874 linkage first, and then transform that into a concrete
5875 implementation. */
5876
5877 /* Things that don't have names have no linkage. */
5878 if (!DECL_NAME (decl))
5879 return lk_none;
5880
5881 /* Fields have no linkage. */
5882 if (TREE_CODE (decl) == FIELD_DECL)
5883 return lk_none;
5884
5885 /* Things in local scope do not have linkage. */
5886 if (decl_function_context (decl))
5887 return lk_none;
5888
5889 /* Things that are TREE_PUBLIC have external linkage. */
5890 if (TREE_PUBLIC (decl))
5891 return lk_external;
5892
5893 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor variants,
5894 check one of the "clones" for the real linkage. */
5895 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl)
5896 && DECL_CHAIN (decl)
5897 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)))
5898 return decl_linkage (DECL_CHAIN (decl));
5899
5900 if (TREE_CODE (decl) == NAMESPACE_DECL)
5901 return lk_external;
5902
5903 /* Linkage of a CONST_DECL depends on the linkage of the enumeration
5904 type. */
5905 if (TREE_CODE (decl) == CONST_DECL)
5906 return decl_linkage (TYPE_NAME (DECL_CONTEXT (decl)));
5907
5908 /* Members of the anonymous namespace also have TREE_PUBLIC unset, but
5909 are considered to have external linkage for language purposes, as do
5910 template instantiations on targets without weak symbols. DECLs really
5911 meant to have internal linkage have DECL_THIS_STATIC set. */
5912 if (TREE_CODE (decl) == TYPE_DECL)
5913 return lk_external;
5914 if (VAR_OR_FUNCTION_DECL_P (decl))
5915 {
5916 if (!DECL_THIS_STATIC (decl))
5917 return lk_external;
5918
5919 /* Static data members and static member functions from classes
5920 in anonymous namespace also don't have TREE_PUBLIC set. */
5921 if (DECL_CLASS_CONTEXT (decl))
5922 return lk_external;
5923 }
5924
5925 /* Everything else has internal linkage. */
5926 return lk_internal;
5927}
5928
5929/* Returns the storage duration of the object or reference associated with
5930 the indicated DECL, which should be a VAR_DECL or PARM_DECL. */
5931
5932duration_kind
5933decl_storage_duration (tree decl)
5934{
5935 if (TREE_CODE (decl) == PARM_DECL)
5936 return dk_auto;
5937 if (TREE_CODE (decl) == FUNCTION_DECL)
5938 return dk_static;
5939 gcc_assert (VAR_P (decl));
5940 if (!TREE_STATIC (decl)
5941 && !DECL_EXTERNAL (decl))
5942 return dk_auto;
5943 if (CP_DECL_THREAD_LOCAL_P (decl))
5944 return dk_thread;
5945 return dk_static;
5946}
5947
5948/* EXP is an expression that we want to pre-evaluate. Returns (in
5949 *INITP) an expression that will perform the pre-evaluation. The
5950 value returned by this function is a side-effect free expression
5951 equivalent to the pre-evaluated expression. Callers must ensure
5952 that *INITP is evaluated before EXP. */
5953
5954tree
5955stabilize_expr (tree exp, tree* initp)
5956{
5957 tree init_expr;
5958
5959 if (!TREE_SIDE_EFFECTS (exp))
5960 init_expr = NULL_TREE;
5961 else if (VOID_TYPE_P (TREE_TYPE (exp)))
5962 {
5963 init_expr = exp;
5964 exp = void_node;
5965 }
5966 /* There are no expressions with REFERENCE_TYPE, but there can be call
5967 arguments with such a type; just treat it as a pointer. */
5968 else if (TYPE_REF_P (TREE_TYPE (exp))
5969 || SCALAR_TYPE_P (TREE_TYPE (exp))
5970 || !glvalue_p (ref: exp))
5971 {
5972 init_expr = get_target_expr (init: exp);
5973 exp = TARGET_EXPR_SLOT (init_expr);
5974 if (CLASS_TYPE_P (TREE_TYPE (exp)))
5975 exp = move (expr: exp);
5976 else
5977 exp = rvalue (expr: exp);
5978 }
5979 else
5980 {
5981 bool xval = !lvalue_p (t: exp);
5982 exp = cp_build_addr_expr (exp, tf_warning_or_error);
5983 init_expr = get_target_expr (init: exp);
5984 exp = TARGET_EXPR_SLOT (init_expr);
5985 exp = cp_build_fold_indirect_ref (exp);
5986 if (xval)
5987 exp = move (expr: exp);
5988 }
5989 *initp = init_expr;
5990
5991 gcc_assert (!TREE_SIDE_EFFECTS (exp));
5992 return exp;
5993}
5994
5995/* Add NEW_EXPR, an expression whose value we don't care about, after the
5996 similar expression ORIG. */
5997
5998tree
5999add_stmt_to_compound (tree orig, tree new_expr)
6000{
6001 if (!new_expr || !TREE_SIDE_EFFECTS (new_expr))
6002 return orig;
6003 if (!orig || !TREE_SIDE_EFFECTS (orig))
6004 return new_expr;
6005 return build2 (COMPOUND_EXPR, void_type_node, orig, new_expr);
6006}
6007
6008/* Like stabilize_expr, but for a call whose arguments we want to
6009 pre-evaluate. CALL is modified in place to use the pre-evaluated
6010 arguments, while, upon return, *INITP contains an expression to
6011 compute the arguments. */
6012
6013void
6014stabilize_call (tree call, tree *initp)
6015{
6016 tree inits = NULL_TREE;
6017 int i;
6018 int nargs = call_expr_nargs (call);
6019
6020 if (call == error_mark_node || processing_template_decl)
6021 {
6022 *initp = NULL_TREE;
6023 return;
6024 }
6025
6026 gcc_assert (TREE_CODE (call) == CALL_EXPR);
6027
6028 for (i = 0; i < nargs; i++)
6029 {
6030 tree init;
6031 CALL_EXPR_ARG (call, i) =
6032 stabilize_expr (CALL_EXPR_ARG (call, i), initp: &init);
6033 inits = add_stmt_to_compound (orig: inits, new_expr: init);
6034 }
6035
6036 *initp = inits;
6037}
6038
6039/* Like stabilize_expr, but for an AGGR_INIT_EXPR whose arguments we want
6040 to pre-evaluate. CALL is modified in place to use the pre-evaluated
6041 arguments, while, upon return, *INITP contains an expression to
6042 compute the arguments. */
6043
6044static void
6045stabilize_aggr_init (tree call, tree *initp)
6046{
6047 tree inits = NULL_TREE;
6048 int i;
6049 int nargs = aggr_init_expr_nargs (call);
6050
6051 if (call == error_mark_node)
6052 return;
6053
6054 gcc_assert (TREE_CODE (call) == AGGR_INIT_EXPR);
6055
6056 for (i = 0; i < nargs; i++)
6057 {
6058 tree init;
6059 AGGR_INIT_EXPR_ARG (call, i) =
6060 stabilize_expr (AGGR_INIT_EXPR_ARG (call, i), initp: &init);
6061 inits = add_stmt_to_compound (orig: inits, new_expr: init);
6062 }
6063
6064 *initp = inits;
6065}
6066
6067/* Like stabilize_expr, but for an initialization.
6068
6069 If the initialization is for an object of class type, this function
6070 takes care not to introduce additional temporaries.
6071
6072 Returns TRUE iff the expression was successfully pre-evaluated,
6073 i.e., if INIT is now side-effect free, except for, possibly, a
6074 single call to a constructor. */
6075
6076bool
6077stabilize_init (tree init, tree *initp)
6078{
6079 tree t = init;
6080
6081 *initp = NULL_TREE;
6082
6083 if (t == error_mark_node || processing_template_decl)
6084 return true;
6085
6086 if (TREE_CODE (t) == INIT_EXPR)
6087 t = TREE_OPERAND (t, 1);
6088 if (TREE_CODE (t) == TARGET_EXPR)
6089 t = TARGET_EXPR_INITIAL (t);
6090
6091 /* If the RHS can be stabilized without breaking copy elision, stabilize
6092 it. We specifically don't stabilize class prvalues here because that
6093 would mean an extra copy, but they might be stabilized below. */
6094 if (TREE_CODE (init) == INIT_EXPR
6095 && TREE_CODE (t) != CONSTRUCTOR
6096 && TREE_CODE (t) != AGGR_INIT_EXPR
6097 && (SCALAR_TYPE_P (TREE_TYPE (t))
6098 || glvalue_p (ref: t)))
6099 {
6100 TREE_OPERAND (init, 1) = stabilize_expr (exp: t, initp);
6101 return true;
6102 }
6103
6104 if (TREE_CODE (t) == COMPOUND_EXPR
6105 && TREE_CODE (init) == INIT_EXPR)
6106 {
6107 tree last = expr_last (t);
6108 /* Handle stabilizing the EMPTY_CLASS_EXPR pattern. */
6109 if (!TREE_SIDE_EFFECTS (last))
6110 {
6111 *initp = t;
6112 TREE_OPERAND (init, 1) = last;
6113 return true;
6114 }
6115 }
6116
6117 if (TREE_CODE (t) == CONSTRUCTOR)
6118 {
6119 /* Aggregate initialization: stabilize each of the field
6120 initializers. */
6121 unsigned i;
6122 constructor_elt *ce;
6123 bool good = true;
6124 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
6125 for (i = 0; vec_safe_iterate (v, ix: i, ptr: &ce); ++i)
6126 {
6127 tree type = TREE_TYPE (ce->value);
6128 tree subinit;
6129 if (TYPE_REF_P (type)
6130 || SCALAR_TYPE_P (type))
6131 ce->value = stabilize_expr (exp: ce->value, initp: &subinit);
6132 else if (!stabilize_init (init: ce->value, initp: &subinit))
6133 good = false;
6134 *initp = add_stmt_to_compound (orig: *initp, new_expr: subinit);
6135 }
6136 return good;
6137 }
6138
6139 if (TREE_CODE (t) == CALL_EXPR)
6140 {
6141 stabilize_call (call: t, initp);
6142 return true;
6143 }
6144
6145 if (TREE_CODE (t) == AGGR_INIT_EXPR)
6146 {
6147 stabilize_aggr_init (call: t, initp);
6148 return true;
6149 }
6150
6151 /* The initialization is being performed via a bitwise copy -- and
6152 the item copied may have side effects. */
6153 return !TREE_SIDE_EFFECTS (init);
6154}
6155
6156/* Returns true if a cast to TYPE may appear in an integral constant
6157 expression. */
6158
6159bool
6160cast_valid_in_integral_constant_expression_p (tree type)
6161{
6162 return (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6163 || cxx_dialect >= cxx11
6164 || dependent_type_p (type)
6165 || type == error_mark_node);
6166}
6167
6168/* Return true if we need to fix linkage information of DECL. */
6169
6170static bool
6171cp_fix_function_decl_p (tree decl)
6172{
6173 /* Skip if DECL is not externally visible. */
6174 if (!TREE_PUBLIC (decl))
6175 return false;
6176
6177 /* We need to fix DECL if it a appears to be exported but with no
6178 function body. Thunks do not have CFGs and we may need to
6179 handle them specially later. */
6180 if (!gimple_has_body_p (decl)
6181 && !DECL_THUNK_P (decl)
6182 && !DECL_EXTERNAL (decl))
6183 {
6184 struct cgraph_node *node = cgraph_node::get (decl);
6185
6186 /* Don't fix same_body aliases. Although they don't have their own
6187 CFG, they share it with what they alias to. */
6188 if (!node || !node->alias || !node->num_references ())
6189 return true;
6190 }
6191
6192 return false;
6193}
6194
6195/* Clean the C++ specific parts of the tree T. */
6196
6197void
6198cp_free_lang_data (tree t)
6199{
6200 if (FUNC_OR_METHOD_TYPE_P (t))
6201 {
6202 /* Default args are not interesting anymore. */
6203 tree argtypes = TYPE_ARG_TYPES (t);
6204 while (argtypes)
6205 {
6206 TREE_PURPOSE (argtypes) = 0;
6207 argtypes = TREE_CHAIN (argtypes);
6208 }
6209 }
6210 else if (TREE_CODE (t) == FUNCTION_DECL
6211 && cp_fix_function_decl_p (decl: t))
6212 {
6213 /* If T is used in this translation unit at all, the definition
6214 must exist somewhere else since we have decided to not emit it
6215 in this TU. So make it an external reference. */
6216 DECL_EXTERNAL (t) = 1;
6217 TREE_STATIC (t) = 0;
6218 }
6219 if (TREE_CODE (t) == NAMESPACE_DECL)
6220 /* We do not need the leftover chaining of namespaces from the
6221 binding level. */
6222 DECL_CHAIN (t) = NULL_TREE;
6223}
6224
6225/* Stub for c-common. Please keep in sync with c-decl.cc.
6226 FIXME: If address space support is target specific, then this
6227 should be a C target hook. But currently this is not possible,
6228 because this function is called via REGISTER_TARGET_PRAGMAS. */
6229void
6230c_register_addr_space (const char * /*word*/, addr_space_t /*as*/)
6231{
6232}
6233
6234/* Return the number of operands in T that we care about for things like
6235 mangling. */
6236
6237int
6238cp_tree_operand_length (const_tree t)
6239{
6240 enum tree_code code = TREE_CODE (t);
6241
6242 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
6243 return VL_EXP_OPERAND_LENGTH (t);
6244
6245 return cp_tree_code_length (code);
6246}
6247
6248/* Like cp_tree_operand_length, but takes a tree_code CODE. */
6249
6250int
6251cp_tree_code_length (enum tree_code code)
6252{
6253 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
6254
6255 switch (code)
6256 {
6257 case PREINCREMENT_EXPR:
6258 case PREDECREMENT_EXPR:
6259 case POSTINCREMENT_EXPR:
6260 case POSTDECREMENT_EXPR:
6261 return 1;
6262
6263 case ARRAY_REF:
6264 return 2;
6265
6266 case EXPR_PACK_EXPANSION:
6267 return 1;
6268
6269 default:
6270 return TREE_CODE_LENGTH (code);
6271 }
6272}
6273
6274/* Implement -Wzero_as_null_pointer_constant. Return true if the
6275 conditions for the warning hold, false otherwise. */
6276bool
6277maybe_warn_zero_as_null_pointer_constant (tree expr, location_t loc)
6278{
6279 if (c_inhibit_evaluation_warnings == 0
6280 && !null_node_p (expr) && !NULLPTR_TYPE_P (TREE_TYPE (expr)))
6281 {
6282 warning_at (loc, OPT_Wzero_as_null_pointer_constant,
6283 "zero as null pointer constant");
6284 return true;
6285 }
6286 return false;
6287}
6288
6289/* FNDECL is a function declaration whose type may have been altered by
6290 adding extra parameters such as this, in-charge, or VTT. When this
6291 takes place, the positional arguments supplied by the user (as in the
6292 'format' attribute arguments) may refer to the wrong argument. This
6293 function returns an integer indicating how many arguments should be
6294 skipped. */
6295
6296int
6297maybe_adjust_arg_pos_for_attribute (const_tree fndecl)
6298{
6299 if (!fndecl)
6300 return 0;
6301 int n = num_artificial_parms_for (fndecl);
6302 /* The manual states that it's the user's responsibility to account
6303 for the implicit this parameter. */
6304 return n > 0 ? n - 1 : 0;
6305}
6306
6307
6308/* Release memory we no longer need after parsing. */
6309void
6310cp_tree_c_finish_parsing ()
6311{
6312 if (previous_class_level)
6313 invalidate_class_lookup_cache ();
6314 deleted_copy_types = NULL;
6315}
6316
6317#if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
6318/* Complain that some language-specific thing hanging off a tree
6319 node has been accessed improperly. */
6320
6321void
6322lang_check_failed (const char* file, int line, const char* function)
6323{
6324 internal_error ("%<lang_*%> check: failed in %s, at %s:%d",
6325 function, trim_filename (file), line);
6326}
6327#endif /* ENABLE_TREE_CHECKING */
6328
6329#if CHECKING_P
6330
6331namespace selftest {
6332
6333/* Verify that lvalue_kind () works, for various expressions,
6334 and that location wrappers don't affect the results. */
6335
6336static void
6337test_lvalue_kind ()
6338{
6339 location_t loc = BUILTINS_LOCATION;
6340
6341 /* Verify constants and parameters, without and with
6342 location wrappers. */
6343 tree int_cst = build_int_cst (integer_type_node, 42);
6344 ASSERT_EQ (clk_none, lvalue_kind (int_cst));
6345
6346 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
6347 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
6348 ASSERT_EQ (clk_none, lvalue_kind (wrapped_int_cst));
6349
6350 tree string_lit = build_string (4, "foo");
6351 TREE_TYPE (string_lit) = char_array_type_node;
6352 string_lit = fix_string_type (string_lit);
6353 ASSERT_EQ (clk_ordinary, lvalue_kind (string_lit));
6354
6355 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
6356 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
6357 ASSERT_EQ (clk_ordinary, lvalue_kind (wrapped_string_lit));
6358
6359 tree parm = build_decl (UNKNOWN_LOCATION, PARM_DECL,
6360 get_identifier ("some_parm"),
6361 integer_type_node);
6362 ASSERT_EQ (clk_ordinary, lvalue_kind (parm));
6363
6364 tree wrapped_parm = maybe_wrap_with_location (parm, loc);
6365 ASSERT_TRUE (location_wrapper_p (wrapped_parm));
6366 ASSERT_EQ (clk_ordinary, lvalue_kind (wrapped_parm));
6367
6368 /* Verify that lvalue_kind of std::move on a parm isn't
6369 affected by location wrappers. */
6370 tree rvalue_ref_of_parm = move (expr: parm);
6371 ASSERT_EQ (clk_rvalueref, lvalue_kind (rvalue_ref_of_parm));
6372 tree rvalue_ref_of_wrapped_parm = move (expr: wrapped_parm);
6373 ASSERT_EQ (clk_rvalueref, lvalue_kind (rvalue_ref_of_wrapped_parm));
6374
6375 /* Verify lvalue_p. */
6376 ASSERT_FALSE (lvalue_p (int_cst));
6377 ASSERT_FALSE (lvalue_p (wrapped_int_cst));
6378 ASSERT_TRUE (lvalue_p (parm));
6379 ASSERT_TRUE (lvalue_p (wrapped_parm));
6380 ASSERT_FALSE (lvalue_p (rvalue_ref_of_parm));
6381 ASSERT_FALSE (lvalue_p (rvalue_ref_of_wrapped_parm));
6382}
6383
6384/* Run all of the selftests within this file. */
6385
6386void
6387cp_tree_cc_tests ()
6388{
6389 test_lvalue_kind ();
6390}
6391
6392} // namespace selftest
6393
6394#endif /* #if CHECKING_P */
6395
6396
6397#include "gt-cp-tree.h"
6398

source code of gcc/cp/tree.cc