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