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