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