]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/tree.c
re PR target/84986 (Performance regression: loop no longer vectorized (x86-64))
[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. */
570f86f9 3099 tree exp; /* The outermost exp. */
817a77e4 3100 bool seen; /* Whether we've encountered a PLACEHOLDER_EXPR. */
2dc589be 3101 hash_set<tree> *pset; /* To avoid walking same trees multiple times. */
817a77e4
JM
3102};
3103
3e605b20
JM
3104/* Like substitute_placeholder_in_expr, but handle C++ tree codes and
3105 build up subexpressions as we go deeper. */
3106
3e605b20
JM
3107static tree
3108replace_placeholders_r (tree* t, int* walk_subtrees, void* data_)
3109{
817a77e4
JM
3110 replace_placeholders_t *d = static_cast<replace_placeholders_t*>(data_);
3111 tree obj = d->obj;
3e605b20 3112
b671df81 3113 if (TYPE_P (*t) || TREE_CONSTANT (*t))
3e605b20
JM
3114 {
3115 *walk_subtrees = false;
3116 return NULL_TREE;
3117 }
3118
3119 switch (TREE_CODE (*t))
3120 {
3121 case PLACEHOLDER_EXPR:
6a9263f7
JM
3122 {
3123 tree x = obj;
2dc589be
JJ
3124 for (; !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (*t),
3125 TREE_TYPE (x));
6a9263f7
JM
3126 x = TREE_OPERAND (x, 0))
3127 gcc_assert (TREE_CODE (x) == COMPONENT_REF);
570f86f9 3128 *t = unshare_expr (x);
6a9263f7 3129 *walk_subtrees = false;
817a77e4 3130 d->seen = true;
6a9263f7 3131 }
3e605b20
JM
3132 break;
3133
3134 case CONSTRUCTOR:
3135 {
3136 constructor_elt *ce;
3137 vec<constructor_elt,va_gc> *v = CONSTRUCTOR_ELTS (*t);
570f86f9
JJ
3138 /* Don't walk into CONSTRUCTOR_PLACEHOLDER_BOUNDARY ctors
3139 other than the d->exp one, those have PLACEHOLDER_EXPRs
3140 related to another object. */
3141 if ((CONSTRUCTOR_PLACEHOLDER_BOUNDARY (*t)
3142 && *t != d->exp)
3143 || d->pset->add (*t))
0a552ae2
JJ
3144 {
3145 *walk_subtrees = false;
3146 return NULL_TREE;
3147 }
3e605b20
JM
3148 for (unsigned i = 0; vec_safe_iterate (v, i, &ce); ++i)
3149 {
3150 tree *valp = &ce->value;
3151 tree type = TREE_TYPE (*valp);
3152 tree subob = obj;
3153
3154 if (TREE_CODE (*valp) == CONSTRUCTOR
3155 && AGGREGATE_TYPE_P (type))
3156 {
7599760d
JM
3157 /* If we're looking at the initializer for OBJ, then build
3158 a sub-object reference. If we're looking at an
3159 initializer for another object, just pass OBJ down. */
3160 if (same_type_ignoring_top_level_qualifiers_p
3161 (TREE_TYPE (*t), TREE_TYPE (obj)))
3162 subob = build_ctor_subob_ref (ce->index, type, obj);
3e605b20
JM
3163 if (TREE_CODE (*valp) == TARGET_EXPR)
3164 valp = &TARGET_EXPR_INITIAL (*valp);
3165 }
817a77e4 3166 d->obj = subob;
0a552ae2 3167 cp_walk_tree (valp, replace_placeholders_r, data_, NULL);
817a77e4 3168 d->obj = obj;
3e605b20
JM
3169 }
3170 *walk_subtrees = false;
3171 break;
3172 }
3173
3174 default:
0a552ae2
JJ
3175 if (d->pset->add (*t))
3176 *walk_subtrees = false;
3e605b20
JM
3177 break;
3178 }
3179
3180 return NULL_TREE;
3181}
3182
2166aeb3
MP
3183/* Replace PLACEHOLDER_EXPRs in EXP with object OBJ. SEEN_P is set if
3184 a PLACEHOLDER_EXPR has been encountered. */
3185
3e605b20 3186tree
817a77e4 3187replace_placeholders (tree exp, tree obj, bool *seen_p)
3e605b20 3188{
2166aeb3
MP
3189 /* This is only relevant for C++14. */
3190 if (cxx_dialect < cxx14)
3191 return exp;
3192
3193 /* If the object isn't a (member of a) class, do nothing. */
3194 tree op0 = obj;
3195 while (TREE_CODE (op0) == COMPONENT_REF)
3196 op0 = TREE_OPERAND (op0, 0);
3197 if (!CLASS_TYPE_P (strip_array_types (TREE_TYPE (op0))))
3198 return exp;
3199
3e605b20
JM
3200 tree *tp = &exp;
3201 if (TREE_CODE (exp) == TARGET_EXPR)
3202 tp = &TARGET_EXPR_INITIAL (exp);
570f86f9
JJ
3203 hash_set<tree> pset;
3204 replace_placeholders_t data = { obj, *tp, false, &pset };
0a552ae2 3205 cp_walk_tree (tp, replace_placeholders_r, &data, NULL);
817a77e4
JM
3206 if (seen_p)
3207 *seen_p = data.seen;
3e605b20
JM
3208 return exp;
3209}
3210
570f86f9
JJ
3211/* Callback function for find_placeholders. */
3212
3213static tree
3214find_placeholders_r (tree *t, int *walk_subtrees, void *)
3215{
3216 if (TYPE_P (*t) || TREE_CONSTANT (*t))
3217 {
3218 *walk_subtrees = false;
3219 return NULL_TREE;
3220 }
3221
3222 switch (TREE_CODE (*t))
3223 {
3224 case PLACEHOLDER_EXPR:
3225 return *t;
3226
3227 case CONSTRUCTOR:
3228 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (*t))
3229 *walk_subtrees = false;
3230 break;
3231
3232 default:
3233 break;
3234 }
3235
3236 return NULL_TREE;
3237}
3238
3239/* Return true if EXP contains a PLACEHOLDER_EXPR. Don't walk into
3240 ctors with CONSTRUCTOR_PLACEHOLDER_BOUNDARY flag set. */
3241
3242bool
3243find_placeholders (tree exp)
3244{
3245 /* This is only relevant for C++14. */
3246 if (cxx_dialect < cxx14)
3247 return false;
3248
3249 return cp_walk_tree_without_duplicates (&exp, find_placeholders_r, NULL);
3250}
3251
8e1daa34
NS
3252/* Similar to `build_nt', but for template definitions of dependent
3253 expressions */
5566b478
MS
3254
3255tree
f330f599 3256build_min_nt_loc (location_t loc, enum tree_code code, ...)
5566b478 3257{
926ce8bd
KH
3258 tree t;
3259 int length;
3260 int i;
e34d07f2 3261 va_list p;
5566b478 3262
5039610b
SL
3263 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3264
e34d07f2 3265 va_start (p, code);
5566b478 3266
5566b478 3267 t = make_node (code);
f330f599 3268 SET_EXPR_LOCATION (t, loc);
8d5e6e25 3269 length = TREE_CODE_LENGTH (code);
5566b478
MS
3270
3271 for (i = 0; i < length; i++)
3272 {
3273 tree x = va_arg (p, tree);
2a1e9fdd 3274 TREE_OPERAND (t, i) = x;
58dec010
NS
3275 if (x && TREE_CODE (x) == OVERLOAD)
3276 lookup_keep (x, true);
5566b478
MS
3277 }
3278
e34d07f2 3279 va_end (p);
5566b478
MS
3280 return t;
3281}
3282
8e1daa34 3283/* Similar to `build', but for template definitions. */
5566b478
MS
3284
3285tree
e34d07f2 3286build_min (enum tree_code code, tree tt, ...)
5566b478 3287{
926ce8bd
KH
3288 tree t;
3289 int length;
3290 int i;
e34d07f2 3291 va_list p;
5566b478 3292
5039610b
SL
3293 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3294
e34d07f2 3295 va_start (p, tt);
5566b478 3296
5566b478 3297 t = make_node (code);
8d5e6e25 3298 length = TREE_CODE_LENGTH (code);
2a1e9fdd 3299 TREE_TYPE (t) = tt;
5566b478
MS
3300
3301 for (i = 0; i < length; i++)
3302 {
3303 tree x = va_arg (p, tree);
2a1e9fdd 3304 TREE_OPERAND (t, i) = x;
58dec010
NS
3305 if (x)
3306 {
3307 if (!TYPE_P (x) && TREE_SIDE_EFFECTS (x))
3308 TREE_SIDE_EFFECTS (t) = 1;
3309 if (TREE_CODE (x) == OVERLOAD)
3310 lookup_keep (x, true);
3311 }
5566b478
MS
3312 }
3313
e34d07f2 3314 va_end (p);
ea9e71de
NS
3315
3316 if (code == CAST_EXPR)
3317 /* The single operand is a TREE_LIST, which we have to check. */
3318 for (tree v = TREE_OPERAND (t, 0); v; v = TREE_CHAIN (v))
3319 if (TREE_CODE (TREE_VALUE (v)) == OVERLOAD)
3320 lookup_keep (TREE_VALUE (v), true);
3321
5566b478
MS
3322 return t;
3323}
3324
8e1daa34
NS
3325/* Similar to `build', but for template definitions of non-dependent
3326 expressions. NON_DEP is the non-dependent expression that has been
3327 built. */
3328
3329tree
3330build_min_non_dep (enum tree_code code, tree non_dep, ...)
3331{
926ce8bd
KH
3332 tree t;
3333 int length;
3334 int i;
8e1daa34
NS
3335 va_list p;
3336
5039610b
SL
3337 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3338
8e1daa34
NS
3339 va_start (p, non_dep);
3340
e87b4dde
JM
3341 if (REFERENCE_REF_P (non_dep))
3342 non_dep = TREE_OPERAND (non_dep, 0);
3343
8e1daa34
NS
3344 t = make_node (code);
3345 length = TREE_CODE_LENGTH (code);
6a2cc46b 3346 TREE_TYPE (t) = unlowered_expr_type (non_dep);
8e1daa34
NS
3347 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
3348
3349 for (i = 0; i < length; i++)
3350 {
3351 tree x = va_arg (p, tree);
3352 TREE_OPERAND (t, i) = x;
58dec010
NS
3353 if (x && TREE_CODE (x) == OVERLOAD)
3354 lookup_keep (x, true);
8e1daa34
NS
3355 }
3356
3357 if (code == COMPOUND_EXPR && TREE_CODE (non_dep) != COMPOUND_EXPR)
3358 /* This should not be considered a COMPOUND_EXPR, because it
04c06002 3359 resolves to an overload. */
8e1daa34 3360 COMPOUND_EXPR_OVERLOADED (t) = 1;
9f63daea 3361
8e1daa34 3362 va_end (p);
e87b4dde 3363 return convert_from_reference (t);
8e1daa34
NS
3364}
3365
58dec010
NS
3366/* Similar to build_min_nt, but call expressions */
3367
3368tree
3369build_min_nt_call_vec (tree fn, vec<tree, va_gc> *args)
3370{
3371 tree ret, t;
3372 unsigned int ix;
3373
3374 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
3375 CALL_EXPR_FN (ret) = fn;
3376 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
3377 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
3378 {
3379 CALL_EXPR_ARG (ret, ix) = t;
3380 if (TREE_CODE (t) == OVERLOAD)
3381 lookup_keep (t, true);
3382 }
3383 return ret;
3384}
3385
3386/* Similar to `build_min_nt_call_vec', but for template definitions of
3fcb9d1b
NF
3387 non-dependent expressions. NON_DEP is the non-dependent expression
3388 that has been built. */
5039610b
SL
3389
3390tree
9771b263 3391build_min_non_dep_call_vec (tree non_dep, tree fn, vec<tree, va_gc> *argvec)
5039610b 3392{
58dec010 3393 tree t = build_min_nt_call_vec (fn, argvec);
e87b4dde
JM
3394 if (REFERENCE_REF_P (non_dep))
3395 non_dep = TREE_OPERAND (non_dep, 0);
5039610b
SL
3396 TREE_TYPE (t) = TREE_TYPE (non_dep);
3397 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
e87b4dde 3398 return convert_from_reference (t);
5039610b
SL
3399}
3400
fcb9363e
PP
3401/* Similar to build_min_non_dep, but for expressions that have been resolved to
3402 a call to an operator overload. OP is the operator that has been
3403 overloaded. NON_DEP is the non-dependent expression that's been built,
3404 which should be a CALL_EXPR or an INDIRECT_REF to a CALL_EXPR. OVERLOAD is
3405 the overload that NON_DEP is calling. */
3406
3407tree
3408build_min_non_dep_op_overload (enum tree_code op,
3409 tree non_dep,
3410 tree overload, ...)
3411{
3412 va_list p;
3413 int nargs, expected_nargs;
3414 tree fn, call;
3415 vec<tree, va_gc> *args;
3416
4eb24e01 3417 non_dep = extract_call_expr (non_dep);
fcb9363e
PP
3418
3419 nargs = call_expr_nargs (non_dep);
3420
3421 expected_nargs = cp_tree_code_length (op);
e8b0383c
JJ
3422 if ((op == POSTINCREMENT_EXPR
3423 || op == POSTDECREMENT_EXPR)
3424 /* With -fpermissive non_dep could be operator++(). */
3425 && (!flag_permissive || nargs != expected_nargs))
fcb9363e
PP
3426 expected_nargs += 1;
3427 gcc_assert (nargs == expected_nargs);
3428
3429 args = make_tree_vector ();
3430 va_start (p, overload);
3431
3432 if (TREE_CODE (TREE_TYPE (overload)) == FUNCTION_TYPE)
3433 {
3434 fn = overload;
3435 for (int i = 0; i < nargs; i++)
3436 {
3437 tree arg = va_arg (p, tree);
3438 vec_safe_push (args, arg);
3439 }
3440 }
3441 else if (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE)
3442 {
3443 tree object = va_arg (p, tree);
3444 tree binfo = TYPE_BINFO (TREE_TYPE (object));
3445 tree method = build_baselink (binfo, binfo, overload, NULL_TREE);
3446 fn = build_min (COMPONENT_REF, TREE_TYPE (overload),
3447 object, method, NULL_TREE);
3448 for (int i = 1; i < nargs; i++)
3449 {
3450 tree arg = va_arg (p, tree);
3451 vec_safe_push (args, arg);
3452 }
3453 }
3454 else
3455 gcc_unreachable ();
3456
3457 va_end (p);
3458 call = build_min_non_dep_call_vec (non_dep, fn, args);
3459 release_tree_vector (args);
3460
4eb24e01 3461 tree call_expr = extract_call_expr (call);
aa2500e9 3462 KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep);
4eb24e01
JM
3463 CALL_EXPR_OPERATOR_SYNTAX (call_expr) = true;
3464 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep);
3465 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep);
aa2500e9 3466
fcb9363e
PP
3467 return call;
3468}
3469
af63ba4b
JM
3470/* Return a new tree vec copied from VEC, with ELT inserted at index IDX. */
3471
3472vec<tree, va_gc> *
3473vec_copy_and_insert (vec<tree, va_gc> *old_vec, tree elt, unsigned idx)
3474{
3475 unsigned len = vec_safe_length (old_vec);
3476 gcc_assert (idx <= len);
3477
3478 vec<tree, va_gc> *new_vec = NULL;
3479 vec_alloc (new_vec, len + 1);
3480
3481 unsigned i;
3482 for (i = 0; i < len; ++i)
3483 {
3484 if (i == idx)
3485 new_vec->quick_push (elt);
3486 new_vec->quick_push ((*old_vec)[i]);
3487 }
3488 if (i == idx)
3489 new_vec->quick_push (elt);
3490
3491 return new_vec;
3492}
3493
5566b478 3494tree
b57b79f7 3495get_type_decl (tree t)
5566b478 3496{
5566b478
MS
3497 if (TREE_CODE (t) == TYPE_DECL)
3498 return t;
2f939d94 3499 if (TYPE_P (t))
5566b478 3500 return TYPE_STUB_DECL (t);
315fb5db
NS
3501 gcc_assert (t == error_mark_node);
3502 return t;
5566b478
MS
3503}
3504
700466c2
JM
3505/* Returns the namespace that contains DECL, whether directly or
3506 indirectly. */
3507
3508tree
b57b79f7 3509decl_namespace_context (tree decl)
700466c2
JM
3510{
3511 while (1)
3512 {
3513 if (TREE_CODE (decl) == NAMESPACE_DECL)
3514 return decl;
3515 else if (TYPE_P (decl))
3516 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
3517 else
3518 decl = CP_DECL_CONTEXT (decl);
3519 }
3520}
3521
b9e75696
JM
3522/* Returns true if decl is within an anonymous namespace, however deeply
3523 nested, or false otherwise. */
3524
3525bool
58f9752a 3526decl_anon_ns_mem_p (const_tree decl)
b9e75696 3527{
d48b9bbe 3528 while (TREE_CODE (decl) != NAMESPACE_DECL)
b9e75696 3529 {
d48b9bbe
NS
3530 /* Classes inside anonymous namespaces have TREE_PUBLIC == 0. */
3531 if (TYPE_P (decl))
3532 return !TREE_PUBLIC (TYPE_MAIN_DECL (decl));
3533
3534 decl = CP_DECL_CONTEXT (decl);
b9e75696 3535 }
d48b9bbe 3536 return !TREE_PUBLIC (decl);
b9e75696
JM
3537}
3538
c873934c
JM
3539/* Subroutine of cp_tree_equal: t1 and t2 are the CALL_EXPR_FNs of two
3540 CALL_EXPRS. Return whether they are equivalent. */
3541
3542static bool
3543called_fns_equal (tree t1, tree t2)
3544{
3545 /* Core 1321: dependent names are equivalent even if the overload sets
3546 are different. But do compare explicit template arguments. */
3547 tree name1 = dependent_name (t1);
3548 tree name2 = dependent_name (t2);
3549 if (name1 || name2)
3550 {
3551 tree targs1 = NULL_TREE, targs2 = NULL_TREE;
3552
3553 if (name1 != name2)
3554 return false;
3555
3556 if (TREE_CODE (t1) == TEMPLATE_ID_EXPR)
3557 targs1 = TREE_OPERAND (t1, 1);
3558 if (TREE_CODE (t2) == TEMPLATE_ID_EXPR)
3559 targs2 = TREE_OPERAND (t2, 1);
3560 return cp_tree_equal (targs1, targs2);
3561 }
3562 else
3563 return cp_tree_equal (t1, t2);
3564}
3565
67d743fe 3566/* Return truthvalue of whether T1 is the same tree structure as T2.
c8a209ca 3567 Return 1 if they are the same. Return 0 if they are different. */
67d743fe 3568
c8a209ca 3569bool
b57b79f7 3570cp_tree_equal (tree t1, tree t2)
67d743fe 3571{
926ce8bd 3572 enum tree_code code1, code2;
67d743fe
MS
3573
3574 if (t1 == t2)
c8a209ca
NS
3575 return true;
3576 if (!t1 || !t2)
3577 return false;
3578
ef796bef
JM
3579 code1 = TREE_CODE (t1);
3580 code2 = TREE_CODE (t2);
9f63daea 3581
67d743fe 3582 if (code1 != code2)
c8a209ca 3583 return false;
67d743fe 3584
6296cf8e
JM
3585 if (CONSTANT_CLASS_P (t1)
3586 && !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3587 return false;
3588
67d743fe
MS
3589 switch (code1)
3590 {
632f2871
RS
3591 case VOID_CST:
3592 /* There's only a single VOID_CST node, so we should never reach
3593 here. */
3594 gcc_unreachable ();
3595
67d743fe 3596 case INTEGER_CST:
807e902e 3597 return tree_int_cst_equal (t1, t2);
67d743fe
MS
3598
3599 case REAL_CST:
624d31fe 3600 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
67d743fe
MS
3601
3602 case STRING_CST:
3603 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
da61dec9 3604 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
c8a209ca 3605 TREE_STRING_LENGTH (t1));
67d743fe 3606
d05739f8
JM
3607 case FIXED_CST:
3608 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
3609 TREE_FIXED_CST (t2));
3610
2a2193e0
SM
3611 case COMPLEX_CST:
3612 return cp_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
3613 && cp_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
3614
4eab75dd
MG
3615 case VECTOR_CST:
3616 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
3617
67d743fe 3618 case CONSTRUCTOR:
7dd4bdf5
MM
3619 /* We need to do this when determining whether or not two
3620 non-type pointer to member function template arguments
3621 are the same. */
31d06664
JM
3622 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
3623 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
c8a209ca 3624 return false;
31d06664
JM
3625 {
3626 tree field, value;
3627 unsigned int i;
3628 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
3629 {
3630 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
3631 if (!cp_tree_equal (field, elt2->index)
3632 || !cp_tree_equal (value, elt2->value))
3633 return false;
3634 }
3635 }
3636 return true;
7dd4bdf5
MM
3637
3638 case TREE_LIST:
c8a209ca
NS
3639 if (!cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
3640 return false;
3641 if (!cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
3642 return false;
7dd4bdf5 3643 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
67d743fe
MS
3644
3645 case SAVE_EXPR:
3646 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3647
3648 case CALL_EXPR:
5039610b
SL
3649 {
3650 tree arg1, arg2;
3651 call_expr_arg_iterator iter1, iter2;
c873934c 3652 if (!called_fns_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
5039610b
SL
3653 return false;
3654 for (arg1 = first_call_expr_arg (t1, &iter1),
3655 arg2 = first_call_expr_arg (t2, &iter2);
3656 arg1 && arg2;
3657 arg1 = next_call_expr_arg (&iter1),
3658 arg2 = next_call_expr_arg (&iter2))
3659 if (!cp_tree_equal (arg1, arg2))
3660 return false;
96b4a0b5
JM
3661 if (arg1 || arg2)
3662 return false;
3663 return true;
5039610b 3664 }
67d743fe 3665
c8a209ca
NS
3666 case TARGET_EXPR:
3667 {
3668 tree o1 = TREE_OPERAND (t1, 0);
3669 tree o2 = TREE_OPERAND (t2, 0);
9f63daea 3670
c8a209ca
NS
3671 /* Special case: if either target is an unallocated VAR_DECL,
3672 it means that it's going to be unified with whatever the
3673 TARGET_EXPR is really supposed to initialize, so treat it
3674 as being equivalent to anything. */
5a6ccc94 3675 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
c8a209ca
NS
3676 && !DECL_RTL_SET_P (o1))
3677 /*Nop*/;
5a6ccc94 3678 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
c8a209ca
NS
3679 && !DECL_RTL_SET_P (o2))
3680 /*Nop*/;
3681 else if (!cp_tree_equal (o1, o2))
3682 return false;
9f63daea 3683
c8a209ca
NS
3684 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3685 }
9f63daea 3686
67d743fe 3687 case PARM_DECL:
a77f94e2 3688 /* For comparing uses of parameters in late-specified return types
e7dc5734
JM
3689 with an out-of-class definition of the function, but can also come
3690 up for expressions that involve 'this' in a member function
3691 template. */
25976b7f 3692
971e17ff 3693 if (comparing_specializations && !CONSTRAINT_VAR_P (t1))
25976b7f
JM
3694 /* When comparing hash table entries, only an exact match is
3695 good enough; we don't want to replace 'this' with the
971e17ff
AS
3696 version from another function. But be more flexible
3697 with local parameters in a requires-expression. */
25976b7f
JM
3698 return false;
3699
e7dc5734
JM
3700 if (same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3701 {
3702 if (DECL_ARTIFICIAL (t1) ^ DECL_ARTIFICIAL (t2))
3703 return false;
971e17ff
AS
3704 if (CONSTRAINT_VAR_P (t1) ^ CONSTRAINT_VAR_P (t2))
3705 return false;
e7dc5734
JM
3706 if (DECL_ARTIFICIAL (t1)
3707 || (DECL_PARM_LEVEL (t1) == DECL_PARM_LEVEL (t2)
3708 && DECL_PARM_INDEX (t1) == DECL_PARM_INDEX (t2)))
3709 return true;
3710 }
3711 return false;
a77f94e2
JM
3712
3713 case VAR_DECL:
67d743fe 3714 case CONST_DECL:
9b2770f2 3715 case FIELD_DECL:
67d743fe 3716 case FUNCTION_DECL:
c8a209ca
NS
3717 case TEMPLATE_DECL:
3718 case IDENTIFIER_NODE:
47c0c7d7 3719 case SSA_NAME:
c8a209ca 3720 return false;
67d743fe 3721
17a27b4f
MM
3722 case BASELINK:
3723 return (BASELINK_BINFO (t1) == BASELINK_BINFO (t2)
3724 && BASELINK_ACCESS_BINFO (t1) == BASELINK_ACCESS_BINFO (t2)
4643a68e 3725 && BASELINK_QUALIFIED_P (t1) == BASELINK_QUALIFIED_P (t2)
17a27b4f
MM
3726 && cp_tree_equal (BASELINK_FUNCTIONS (t1),
3727 BASELINK_FUNCTIONS (t2)));
3728
f84b4be9 3729 case TEMPLATE_PARM_INDEX:
31758337
NS
3730 return (TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
3731 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2)
9524f710
LE
3732 && (TEMPLATE_PARM_PARAMETER_PACK (t1)
3733 == TEMPLATE_PARM_PARAMETER_PACK (t2))
31758337
NS
3734 && same_type_p (TREE_TYPE (TEMPLATE_PARM_DECL (t1)),
3735 TREE_TYPE (TEMPLATE_PARM_DECL (t2))));
67d743fe 3736
bf12d54d 3737 case TEMPLATE_ID_EXPR:
c873934c
JM
3738 return (cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3739 && cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3740
971e17ff
AS
3741 case CONSTRAINT_INFO:
3742 return cp_tree_equal (CI_ASSOCIATED_CONSTRAINTS (t1),
3743 CI_ASSOCIATED_CONSTRAINTS (t2));
3744
f078dc7d
AS
3745 case CHECK_CONSTR:
3746 return (CHECK_CONSTR_CONCEPT (t1) == CHECK_CONSTR_CONCEPT (t2)
3747 && comp_template_args (CHECK_CONSTR_ARGS (t1),
3748 CHECK_CONSTR_ARGS (t2)));
3749
c873934c 3750 case TREE_VEC:
bf12d54d
NS
3751 {
3752 unsigned ix;
c873934c 3753 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
bf12d54d 3754 return false;
c873934c
JM
3755 for (ix = TREE_VEC_LENGTH (t1); ix--;)
3756 if (!cp_tree_equal (TREE_VEC_ELT (t1, ix),
3757 TREE_VEC_ELT (t2, ix)))
bf12d54d 3758 return false;
bf12d54d
NS
3759 return true;
3760 }
9f63daea 3761
67d743fe 3762 case SIZEOF_EXPR:
abff8e06 3763 case ALIGNOF_EXPR:
c8a209ca
NS
3764 {
3765 tree o1 = TREE_OPERAND (t1, 0);
3766 tree o2 = TREE_OPERAND (t2, 0);
9f63daea 3767
ba29e5c2
JJ
3768 if (code1 == SIZEOF_EXPR)
3769 {
3770 if (SIZEOF_EXPR_TYPE_P (t1))
3771 o1 = TREE_TYPE (o1);
3772 if (SIZEOF_EXPR_TYPE_P (t2))
3773 o2 = TREE_TYPE (o2);
3774 }
c8a209ca
NS
3775 if (TREE_CODE (o1) != TREE_CODE (o2))
3776 return false;
3777 if (TYPE_P (o1))
3778 return same_type_p (o1, o2);
3779 else
3780 return cp_tree_equal (o1, o2);
3781 }
9f63daea 3782
6f9f76e3
SM
3783 case MODOP_EXPR:
3784 {
3785 tree t1_op1, t2_op1;
3786
3787 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
3788 return false;
3789
3790 t1_op1 = TREE_OPERAND (t1, 1);
3791 t2_op1 = TREE_OPERAND (t2, 1);
3792 if (TREE_CODE (t1_op1) != TREE_CODE (t2_op1))
3793 return false;
3794
3795 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t2, 2));
3796 }
3797
61a127b3
MM
3798 case PTRMEM_CST:
3799 /* Two pointer-to-members are the same if they point to the same
3800 field or function in the same class. */
c8a209ca
NS
3801 if (PTRMEM_CST_MEMBER (t1) != PTRMEM_CST_MEMBER (t2))
3802 return false;
3803
3804 return same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2));
61a127b3 3805
943e3ede 3806 case OVERLOAD:
a736411a
NS
3807 {
3808 /* Two overloads. Must be exactly the same set of decls. */
3809 lkp_iterator first (t1);
3810 lkp_iterator second (t2);
3811
3812 for (; first && second; ++first, ++second)
3813 if (*first != *second)
3814 return false;
3815 return !(first || second);
3816 }
943e3ede 3817
ea798d0f
PC
3818 case TRAIT_EXPR:
3819 if (TRAIT_EXPR_KIND (t1) != TRAIT_EXPR_KIND (t2))
3820 return false;
3821 return same_type_p (TRAIT_EXPR_TYPE1 (t1), TRAIT_EXPR_TYPE1 (t2))
cf5986df 3822 && cp_tree_equal (TRAIT_EXPR_TYPE2 (t1), TRAIT_EXPR_TYPE2 (t2));
ea798d0f 3823
ab73eba8
JM
3824 case CAST_EXPR:
3825 case STATIC_CAST_EXPR:
3826 case REINTERPRET_CAST_EXPR:
3827 case CONST_CAST_EXPR:
3828 case DYNAMIC_CAST_EXPR:
a4474a38 3829 case IMPLICIT_CONV_EXPR:
ab73eba8 3830 case NEW_EXPR:
ef796bef
JM
3831 CASE_CONVERT:
3832 case NON_LVALUE_EXPR:
3833 case VIEW_CONVERT_EXPR:
ab73eba8
JM
3834 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3835 return false;
3836 /* Now compare operands as usual. */
3837 break;
3838
10261728
JM
3839 case DEFERRED_NOEXCEPT:
3840 return (cp_tree_equal (DEFERRED_NOEXCEPT_PATTERN (t1),
3841 DEFERRED_NOEXCEPT_PATTERN (t2))
3842 && comp_template_args (DEFERRED_NOEXCEPT_ARGS (t1),
3843 DEFERRED_NOEXCEPT_ARGS (t2)));
3844 break;
3845
7f85441b
KG
3846 default:
3847 break;
67d743fe
MS
3848 }
3849
3850 switch (TREE_CODE_CLASS (code1))
3851 {
6615c446
JO
3852 case tcc_unary:
3853 case tcc_binary:
3854 case tcc_comparison:
3855 case tcc_expression:
5039610b 3856 case tcc_vl_exp:
6615c446
JO
3857 case tcc_reference:
3858 case tcc_statement:
aa1826e2 3859 {
5039610b
SL
3860 int i, n;
3861
d26e5986 3862 n = cp_tree_operand_length (t1);
5039610b
SL
3863 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
3864 && n != TREE_OPERAND_LENGTH (t2))
3865 return false;
9f63daea 3866
5039610b 3867 for (i = 0; i < n; ++i)
c8a209ca
NS
3868 if (!cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
3869 return false;
9f63daea 3870
c8a209ca 3871 return true;
aa1826e2 3872 }
9f63daea 3873
6615c446 3874 case tcc_type:
c8a209ca 3875 return same_type_p (t1, t2);
6615c446
JO
3876 default:
3877 gcc_unreachable ();
67d743fe 3878 }
6615c446 3879 /* We can get here with --disable-checking. */
c8a209ca 3880 return false;
67d743fe 3881}
73aad9b9 3882
d11ad92e
MS
3883/* The type of ARG when used as an lvalue. */
3884
3885tree
b57b79f7 3886lvalue_type (tree arg)
d11ad92e 3887{
2c73f9f5 3888 tree type = TREE_TYPE (arg);
8cd4c175 3889 return type;
d11ad92e
MS
3890}
3891
3892/* The type of ARG for printing error messages; denote lvalues with
3893 reference types. */
3894
3895tree
b57b79f7 3896error_type (tree arg)
d11ad92e
MS
3897{
3898 tree type = TREE_TYPE (arg);
9f63daea 3899
d11ad92e
MS
3900 if (TREE_CODE (type) == ARRAY_TYPE)
3901 ;
08476342
NS
3902 else if (TREE_CODE (type) == ERROR_MARK)
3903 ;
72b3e203 3904 else if (lvalue_p (arg))
d11ad92e 3905 type = build_reference_type (lvalue_type (arg));
9e1e64ec 3906 else if (MAYBE_CLASS_TYPE_P (type))
d11ad92e
MS
3907 type = lvalue_type (arg);
3908
3909 return type;
3910}
eb66be0e
MS
3911
3912/* Does FUNCTION use a variable-length argument list? */
3913
3914int
58f9752a 3915varargs_function_p (const_tree function)
eb66be0e 3916{
f38958e8 3917 return stdarg_p (TREE_TYPE (function));
eb66be0e 3918}
f94ae2f5
JM
3919
3920/* Returns 1 if decl is a member of a class. */
3921
3922int
58f9752a 3923member_p (const_tree decl)
f94ae2f5 3924{
58f9752a 3925 const_tree const ctx = DECL_CONTEXT (decl);
2f939d94 3926 return (ctx && TYPE_P (ctx));
f94ae2f5 3927}
51924768
JM
3928
3929/* Create a placeholder for member access where we don't actually have an
3930 object that the access is against. */
3931
3932tree
b57b79f7 3933build_dummy_object (tree type)
51924768 3934{
32f3d032 3935 tree decl = build1 (CONVERT_EXPR, build_pointer_type (type), void_node);
04757a2a 3936 return cp_build_fold_indirect_ref (decl);
51924768
JM
3937}
3938
3939/* We've gotten a reference to a member of TYPE. Return *this if appropriate,
3940 or a dummy object otherwise. If BINFOP is non-0, it is filled with the
3941 binfo path from current_class_type to TYPE, or 0. */
3942
3943tree
b57b79f7 3944maybe_dummy_object (tree type, tree* binfop)
51924768
JM
3945{
3946 tree decl, context;
2db1ab2d 3947 tree binfo;
a6846853 3948 tree current = current_nonlambda_class_type ();
9f63daea 3949
a6846853 3950 if (current
22854930
PC
3951 && (binfo = lookup_base (current, type, ba_any, NULL,
3952 tf_warning_or_error)))
a6846853 3953 context = current;
51924768
JM
3954 else
3955 {
3956 /* Reference from a nested class member function. */
3957 context = type;
2db1ab2d 3958 binfo = TYPE_BINFO (type);
51924768
JM
3959 }
3960
2db1ab2d
NS
3961 if (binfop)
3962 *binfop = binfo;
9f63daea 3963
41d04a8d
JM
3964 if (current_class_ref
3965 /* current_class_ref might not correspond to current_class_type if
3966 we're in tsubst_default_argument or a lambda-declarator; in either
3967 case, we want to use current_class_ref if it matches CONTEXT. */
3968 && (same_type_ignoring_top_level_qualifiers_p
3969 (TREE_TYPE (current_class_ref), context)))
51924768
JM
3970 decl = current_class_ref;
3971 else
3972 decl = build_dummy_object (context);
3973
3974 return decl;
3975}
3976
3977/* Returns 1 if OB is a placeholder object, or a pointer to one. */
3978
3979int
58f9752a 3980is_dummy_object (const_tree ob)
51924768 3981{
591cb3cf 3982 if (INDIRECT_REF_P (ob))
51924768 3983 ob = TREE_OPERAND (ob, 0);
32f3d032 3984 return (TREE_CODE (ob) == CONVERT_EXPR
632f2871 3985 && TREE_OPERAND (ob, 0) == void_node);
51924768 3986}
5524676d 3987
c32097d8
JM
3988/* Returns 1 iff type T is something we want to treat as a scalar type for
3989 the purpose of deciding whether it is trivial/POD/standard-layout. */
3990
11f35925 3991bool
c32097d8
JM
3992scalarish_type_p (const_tree t)
3993{
3994 if (t == error_mark_node)
3995 return 1;
3996
b55b02ea 3997 return (SCALAR_TYPE_P (t) || VECTOR_TYPE_P (t));
c32097d8
JM
3998}
3999
4000/* Returns true iff T requires non-trivial default initialization. */
4001
4002bool
4003type_has_nontrivial_default_init (const_tree t)
4004{
4005 t = strip_array_types (CONST_CAST_TREE (t));
4006
4007 if (CLASS_TYPE_P (t))
4008 return TYPE_HAS_COMPLEX_DFLT (t);
4009 else
4010 return 0;
4011}
4012
f3ec182d
JM
4013/* Track classes with only deleted copy/move constructors so that we can warn
4014 if they are used in call/return by value. */
4015
4016static GTY(()) hash_set<tree>* deleted_copy_types;
4017static void
4018remember_deleted_copy (const_tree t)
4019{
4020 if (!deleted_copy_types)
4021 deleted_copy_types = hash_set<tree>::create_ggc(37);
4022 deleted_copy_types->add (CONST_CAST_TREE (t));
4023}
4024void
4025maybe_warn_parm_abi (tree t, location_t loc)
4026{
4027 if (!deleted_copy_types
4028 || !deleted_copy_types->contains (t))
4029 return;
4030
4031 warning_at (loc, OPT_Wabi, "the calling convention for %qT changes in "
4032 "-fabi-version=12 (GCC 8)", t);
4033 static bool explained = false;
4034 if (!explained)
4035 {
4036 inform (loc, " because all of its copy and move constructors "
4037 "are deleted");
4038 explained = true;
4039 }
4040}
4041
d758e847
JM
4042/* Returns true iff copying an object of type T (including via move
4043 constructor) is non-trivial. That is, T has no non-trivial copy
f3ec182d
JM
4044 constructors and no non-trivial move constructors, and not all copy/move
4045 constructors are deleted. This function implements the ABI notion of
4046 non-trivial copy, which has diverged from the one in the standard. */
c32097d8
JM
4047
4048bool
f3ec182d 4049type_has_nontrivial_copy_init (const_tree type)
c32097d8 4050{
f3ec182d 4051 tree t = strip_array_types (CONST_CAST_TREE (type));
c32097d8
JM
4052
4053 if (CLASS_TYPE_P (t))
d758e847
JM
4054 {
4055 gcc_assert (COMPLETE_TYPE_P (t));
f3ec182d
JM
4056
4057 if (TYPE_HAS_COMPLEX_COPY_CTOR (t)
4058 || TYPE_HAS_COMPLEX_MOVE_CTOR (t))
4059 /* Nontrivial. */
4060 return true;
4061
4062 if (cxx_dialect < cxx11)
4063 /* No deleted functions before C++11. */
4064 return false;
4065
4066 /* Before ABI v12 we did a bitwise copy of types with only deleted
4067 copy/move constructors. */
4068 if (!abi_version_at_least (12)
4069 && !(warn_abi && abi_version_crosses (12)))
4070 return false;
4071
4072 bool saw_copy = false;
4073 bool saw_non_deleted = false;
4074
4075 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
4076 saw_copy = saw_non_deleted = true;
4077 else if (CLASSTYPE_LAZY_COPY_CTOR (t))
4078 {
4079 saw_copy = true;
4a18c066 4080 if (classtype_has_move_assign_or_move_ctor_p (t, true))
f3ec182d
JM
4081 /* [class.copy]/8 If the class definition declares a move
4082 constructor or move assignment operator, the implicitly declared
4083 copy constructor is defined as deleted.... */;
4084 else
4085 /* Any other reason the implicitly-declared function would be
4086 deleted would also cause TYPE_HAS_COMPLEX_COPY_CTOR to be
4087 set. */
4088 saw_non_deleted = true;
4089 }
4090
527b7b19 4091 if (!saw_non_deleted)
a736411a 4092 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
f3ec182d 4093 {
a736411a 4094 tree fn = *iter;
f3ec182d
JM
4095 if (copy_fn_p (fn))
4096 {
4097 saw_copy = true;
4098 if (!DECL_DELETED_FN (fn))
4099 {
4100 /* Not deleted, therefore trivial. */
4101 saw_non_deleted = true;
4102 break;
4103 }
4104 }
4105 }
4106
4107 gcc_assert (saw_copy);
4108
4109 if (saw_copy && !saw_non_deleted)
4110 {
4111 if (warn_abi && abi_version_crosses (12))
4112 remember_deleted_copy (t);
4113 if (abi_version_at_least (12))
4114 return true;
4115 }
4116
4117 return false;
d758e847 4118 }
c32097d8
JM
4119 else
4120 return 0;
4121}
4122
46408846
JM
4123/* Returns 1 iff type T is a trivially copyable type, as defined in
4124 [basic.types] and [class]. */
c32097d8
JM
4125
4126bool
46408846 4127trivially_copyable_p (const_tree t)
c32097d8
JM
4128{
4129 t = strip_array_types (CONST_CAST_TREE (t));
4130
4131 if (CLASS_TYPE_P (t))
d758e847
JM
4132 return ((!TYPE_HAS_COPY_CTOR (t)
4133 || !TYPE_HAS_COMPLEX_COPY_CTOR (t))
4134 && !TYPE_HAS_COMPLEX_MOVE_CTOR (t)
4135 && (!TYPE_HAS_COPY_ASSIGN (t)
4136 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (t))
4137 && !TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
334738b4 4138 && TYPE_HAS_TRIVIAL_DESTRUCTOR (t));
c32097d8 4139 else
75bda2e8 4140 return !CP_TYPE_VOLATILE_P (t) && scalarish_type_p (t);
c32097d8
JM
4141}
4142
46408846
JM
4143/* Returns 1 iff type T is a trivial type, as defined in [basic.types] and
4144 [class]. */
4145
4146bool
4147trivial_type_p (const_tree t)
4148{
4149 t = strip_array_types (CONST_CAST_TREE (t));
4150
4151 if (CLASS_TYPE_P (t))
4152 return (TYPE_HAS_TRIVIAL_DFLT (t)
4153 && trivially_copyable_p (t));
4154 else
4155 return scalarish_type_p (t);
4156}
4157
5524676d
JM
4158/* Returns 1 iff type T is a POD type, as defined in [basic.types]. */
4159
c32097d8 4160bool
58f9752a 4161pod_type_p (const_tree t)
5524676d 4162{
4e9b57fa 4163 /* This CONST_CAST is okay because strip_array_types returns its
75547801 4164 argument unmodified and we assign it to a const_tree. */
b1d5455a 4165 t = strip_array_types (CONST_CAST_TREE(t));
5524676d 4166
cc72bbaa
JM
4167 if (!CLASS_TYPE_P (t))
4168 return scalarish_type_p (t);
4169 else if (cxx_dialect > cxx98)
c32097d8
JM
4170 /* [class]/10: A POD struct is a class that is both a trivial class and a
4171 standard-layout class, and has no non-static data members of type
4172 non-POD struct, non-POD union (or array of such types).
4173
4174 We don't need to check individual members because if a member is
4175 non-std-layout or non-trivial, the class will be too. */
4176 return (std_layout_type_p (t) && trivial_type_p (t));
4177 else
cc72bbaa
JM
4178 /* The C++98 definition of POD is different. */
4179 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
c32097d8
JM
4180}
4181
4182/* Returns true iff T is POD for the purpose of layout, as defined in the
4183 C++ ABI. */
4184
4185bool
4186layout_pod_type_p (const_tree t)
4187{
4188 t = strip_array_types (CONST_CAST_TREE (t));
4189
4190 if (CLASS_TYPE_P (t))
4191 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
4192 else
4193 return scalarish_type_p (t);
4194}
4195
4196/* Returns true iff T is a standard-layout type, as defined in
4197 [basic.types]. */
4198
4199bool
4200std_layout_type_p (const_tree t)
4201{
4202 t = strip_array_types (CONST_CAST_TREE (t));
4203
4204 if (CLASS_TYPE_P (t))
4205 return !CLASSTYPE_NON_STD_LAYOUT (t);
4206 else
4207 return scalarish_type_p (t);
5524676d 4208}
e5dc5fb2 4209
342cfb3e
JJ
4210static bool record_has_unique_obj_representations (const_tree, const_tree);
4211
4212/* Returns true iff T satisfies std::has_unique_object_representations<T>,
4213 as defined in [meta.unary.prop]. */
4214
4215bool
4216type_has_unique_obj_representations (const_tree t)
4217{
4218 bool ret;
4219
4220 t = strip_array_types (CONST_CAST_TREE (t));
4221
4222 if (!trivially_copyable_p (t))
4223 return false;
4224
4225 if (CLASS_TYPE_P (t) && CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t))
4226 return CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t);
4227
4228 switch (TREE_CODE (t))
4229 {
4230 case INTEGER_TYPE:
4231 case POINTER_TYPE:
4232 case REFERENCE_TYPE:
4233 /* If some backend has any paddings in these types, we should add
4234 a target hook for this and handle it there. */
4235 return true;
4236
4237 case BOOLEAN_TYPE:
4238 /* For bool values other than 0 and 1 should only appear with
4239 undefined behavior. */
4240 return true;
4241
4242 case ENUMERAL_TYPE:
4243 return type_has_unique_obj_representations (ENUM_UNDERLYING_TYPE (t));
4244
4245 case REAL_TYPE:
4246 /* XFmode certainly contains padding on x86, which the CPU doesn't store
4247 when storing long double values, so for that we have to return false.
4248 Other kinds of floating point values are questionable due to +.0/-.0
4249 and NaNs, let's play safe for now. */
4250 return false;
4251
4252 case FIXED_POINT_TYPE:
4253 return false;
4254
4255 case OFFSET_TYPE:
4256 return true;
4257
4258 case COMPLEX_TYPE:
4259 case VECTOR_TYPE:
4260 return type_has_unique_obj_representations (TREE_TYPE (t));
4261
4262 case RECORD_TYPE:
4263 ret = record_has_unique_obj_representations (t, TYPE_SIZE (t));
4264 if (CLASS_TYPE_P (t))
4265 {
4266 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
4267 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
4268 }
4269 return ret;
4270
4271 case UNION_TYPE:
4272 ret = true;
4273 bool any_fields;
4274 any_fields = false;
4275 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4276 if (TREE_CODE (field) == FIELD_DECL)
4277 {
4278 any_fields = true;
4279 if (!type_has_unique_obj_representations (TREE_TYPE (field))
4280 || simple_cst_equal (DECL_SIZE (field), TYPE_SIZE (t)) != 1)
4281 {
4282 ret = false;
4283 break;
4284 }
4285 }
4286 if (!any_fields && !integer_zerop (TYPE_SIZE (t)))
4287 ret = false;
4288 if (CLASS_TYPE_P (t))
4289 {
4290 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
4291 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
4292 }
4293 return ret;
4294
4295 case NULLPTR_TYPE:
4296 return false;
4297
4298 case ERROR_MARK:
4299 return false;
4300
4301 default:
4302 gcc_unreachable ();
4303 }
4304}
4305
4306/* Helper function for type_has_unique_obj_representations. */
4307
4308static bool
4309record_has_unique_obj_representations (const_tree t, const_tree sz)
4310{
4311 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4312 if (TREE_CODE (field) != FIELD_DECL)
4313 ;
4314 /* For bases, can't use type_has_unique_obj_representations here, as in
4315 struct S { int i : 24; S (); };
4316 struct T : public S { int j : 8; T (); };
4317 S doesn't have unique obj representations, but T does. */
4318 else if (DECL_FIELD_IS_BASE (field))
4319 {
4320 if (!record_has_unique_obj_representations (TREE_TYPE (field),
4321 DECL_SIZE (field)))
4322 return false;
4323 }
4324 else if (DECL_C_BIT_FIELD (field))
4325 {
4326 tree btype = DECL_BIT_FIELD_TYPE (field);
4327 if (!type_has_unique_obj_representations (btype))
4328 return false;
4329 }
4330 else if (!type_has_unique_obj_representations (TREE_TYPE (field)))
4331 return false;
4332
4333 offset_int cur = 0;
4334 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4335 if (TREE_CODE (field) == FIELD_DECL)
4336 {
4337 offset_int fld = wi::to_offset (DECL_FIELD_OFFSET (field));
4338 offset_int bitpos = wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
4339 fld = fld * BITS_PER_UNIT + bitpos;
4340 if (cur != fld)
4341 return false;
4342 if (DECL_SIZE (field))
4343 {
4344 offset_int size = wi::to_offset (DECL_SIZE (field));
4345 cur += size;
4346 }
4347 }
4348 if (cur != wi::to_offset (sz))
4349 return false;
4350
4351 return true;
4352}
4353
39ef6592
LC
4354/* Nonzero iff type T is a class template implicit specialization. */
4355
4356bool
ac7d7749 4357class_tmpl_impl_spec_p (const_tree t)
39ef6592
LC
4358{
4359 return CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INSTANTIATION (t);
4360}
4361
94e6e4c4
AO
4362/* Returns 1 iff zero initialization of type T means actually storing
4363 zeros in it. */
4364
4365int
58f9752a 4366zero_init_p (const_tree t)
94e6e4c4 4367{
4e9b57fa 4368 /* This CONST_CAST is okay because strip_array_types returns its
75547801 4369 argument unmodified and we assign it to a const_tree. */
b1d5455a 4370 t = strip_array_types (CONST_CAST_TREE(t));
94e6e4c4 4371
17bbb839
MM
4372 if (t == error_mark_node)
4373 return 1;
4374
94e6e4c4 4375 /* NULL pointers to data members are initialized with -1. */
66b1156a 4376 if (TYPE_PTRDATAMEM_P (t))
94e6e4c4
AO
4377 return 0;
4378
4379 /* Classes that contain types that can't be zero-initialized, cannot
4380 be zero-initialized themselves. */
4381 if (CLASS_TYPE_P (t) && CLASSTYPE_NON_ZERO_INIT_P (t))
4382 return 0;
4383
4384 return 1;
4385}
4386
b632761d
JM
4387/* Handle the C++17 [[nodiscard]] attribute, which is similar to the GNU
4388 warn_unused_result attribute. */
4389
4390static tree
4391handle_nodiscard_attribute (tree *node, tree name, tree /*args*/,
4392 int /*flags*/, bool *no_add_attrs)
4393{
4394 if (TREE_CODE (*node) == FUNCTION_DECL)
4395 {
4396 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
4397 warning (OPT_Wattributes, "%qE attribute applied to %qD with void "
4398 "return type", name, *node);
4399 }
4400 else if (OVERLOAD_TYPE_P (*node))
4401 /* OK */;
4402 else
4403 {
4404 warning (OPT_Wattributes, "%qE attribute can only be applied to "
4405 "functions or to class or enumeration types", name);
4406 *no_add_attrs = true;
4407 }
4408 return NULL_TREE;
4409}
4410
91d231cb 4411/* Table of valid C++ attributes. */
349ae713 4412const struct attribute_spec cxx_attribute_table[] =
e5dc5fb2 4413{
4849deb1
JJ
4414 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4415 affects_type_identity, handler, exclude } */
4416 { "init_priority", 1, 1, true, false, false, false,
4417 handle_init_priority_attribute, NULL },
4418 { "abi_tag", 1, -1, false, false, false, true,
4419 handle_abi_tag_attribute, NULL },
4420 { NULL, 0, 0, false, false, false, false, NULL, NULL }
91d231cb
JM
4421};
4422
d067e05f
JM
4423/* Table of C++ standard attributes. */
4424const struct attribute_spec std_attribute_table[] =
4425{
4849deb1
JJ
4426 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4427 affects_type_identity, handler, exclude } */
4428 { "maybe_unused", 0, 0, false, false, false, false,
4429 handle_unused_attribute, NULL },
4430 { "nodiscard", 0, 0, false, false, false, false,
4431 handle_nodiscard_attribute, NULL },
4432 { NULL, 0, 0, false, false, false, false, NULL, NULL }
d067e05f
JM
4433};
4434
91d231cb
JM
4435/* Handle an "init_priority" attribute; arguments as in
4436 struct attribute_spec.handler. */
4437static tree
9f63daea 4438handle_init_priority_attribute (tree* node,
0cbd7506
MS
4439 tree name,
4440 tree args,
12308bc6 4441 int /*flags*/,
0cbd7506 4442 bool* no_add_attrs)
91d231cb
JM
4443{
4444 tree initp_expr = TREE_VALUE (args);
4445 tree decl = *node;
4446 tree type = TREE_TYPE (decl);
4447 int pri;
4448
4449 STRIP_NOPS (initp_expr);
5d77fb19 4450 initp_expr = default_conversion (initp_expr);
cda0a029
JM
4451 if (initp_expr)
4452 initp_expr = maybe_constant_value (initp_expr);
9f63daea 4453
91d231cb
JM
4454 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
4455 {
4456 error ("requested init_priority is not an integer constant");
cda0a029 4457 cxx_constant_value (initp_expr);
91d231cb
JM
4458 *no_add_attrs = true;
4459 return NULL_TREE;
4460 }
e5dc5fb2 4461
91d231cb 4462 pri = TREE_INT_CST_LOW (initp_expr);
9f63daea 4463
91d231cb
JM
4464 type = strip_array_types (type);
4465
4466 if (decl == NULL_TREE
5a6ccc94 4467 || !VAR_P (decl)
91d231cb
JM
4468 || !TREE_STATIC (decl)
4469 || DECL_EXTERNAL (decl)
4470 || (TREE_CODE (type) != RECORD_TYPE
4471 && TREE_CODE (type) != UNION_TYPE)
4472 /* Static objects in functions are initialized the
4473 first time control passes through that
4474 function. This is not precise enough to pin down an
c6002625 4475 init_priority value, so don't allow it. */
9f63daea 4476 || current_function_decl)
91d231cb 4477 {
a82e1a7d 4478 error ("can only use %qE attribute on file-scope definitions "
0cbd7506 4479 "of objects of class type", name);
91d231cb
JM
4480 *no_add_attrs = true;
4481 return NULL_TREE;
4482 }
e5dc5fb2 4483
91d231cb
JM
4484 if (pri > MAX_INIT_PRIORITY || pri <= 0)
4485 {
4486 error ("requested init_priority is out of range");
4487 *no_add_attrs = true;
4488 return NULL_TREE;
4489 }
e5dc5fb2 4490
91d231cb
JM
4491 /* Check for init_priorities that are reserved for
4492 language and runtime support implementations.*/
4493 if (pri <= MAX_RESERVED_INIT_PRIORITY)
4494 {
9f63daea 4495 warning
d4ee4d25 4496 (0, "requested init_priority is reserved for internal use");
e5dc5fb2
JM
4497 }
4498
91d231cb
JM
4499 if (SUPPORTS_INIT_PRIORITY)
4500 {
820cc88f
DB
4501 SET_DECL_INIT_PRIORITY (decl, pri);
4502 DECL_HAS_INIT_PRIORITY_P (decl) = 1;
91d231cb
JM
4503 return NULL_TREE;
4504 }
4505 else
4506 {
a82e1a7d 4507 error ("%qE attribute is not supported on this platform", name);
91d231cb
JM
4508 *no_add_attrs = true;
4509 return NULL_TREE;
4510 }
e5dc5fb2 4511}
87533b37 4512
7dbb85a7
JM
4513/* DECL is being redeclared; the old declaration had the abi tags in OLD,
4514 and the new one has the tags in NEW_. Give an error if there are tags
4515 in NEW_ that weren't in OLD. */
4516
4517bool
4518check_abi_tag_redeclaration (const_tree decl, const_tree old, const_tree new_)
4519{
4520 if (old && TREE_CODE (TREE_VALUE (old)) == TREE_LIST)
4521 old = TREE_VALUE (old);
4522 if (new_ && TREE_CODE (TREE_VALUE (new_)) == TREE_LIST)
4523 new_ = TREE_VALUE (new_);
4524 bool err = false;
4525 for (const_tree t = new_; t; t = TREE_CHAIN (t))
4526 {
4527 tree str = TREE_VALUE (t);
4528 for (const_tree in = old; in; in = TREE_CHAIN (in))
4529 {
4530 tree ostr = TREE_VALUE (in);
4531 if (cp_tree_equal (str, ostr))
4532 goto found;
4533 }
0f2c4a8f 4534 error ("redeclaration of %qD adds abi tag %qE", decl, str);
7dbb85a7
JM
4535 err = true;
4536 found:;
4537 }
4538 if (err)
4539 {
4540 inform (DECL_SOURCE_LOCATION (decl), "previous declaration here");
4541 return false;
4542 }
4543 return true;
4544}
4545
7cb73573
JM
4546/* The abi_tag attribute with the name NAME was given ARGS. If they are
4547 ill-formed, give an error and return false; otherwise, return true. */
7dbb85a7 4548
7cb73573
JM
4549bool
4550check_abi_tag_args (tree args, tree name)
7dbb85a7 4551{
7cb73573
JM
4552 if (!args)
4553 {
4554 error ("the %qE attribute requires arguments", name);
4555 return false;
4556 }
4c5cf0b2
JM
4557 for (tree arg = args; arg; arg = TREE_CHAIN (arg))
4558 {
4559 tree elt = TREE_VALUE (arg);
4560 if (TREE_CODE (elt) != STRING_CST
4561 || (!same_type_ignoring_top_level_qualifiers_p
4562 (strip_array_types (TREE_TYPE (elt)),
4563 char_type_node)))
4564 {
4565 error ("arguments to the %qE attribute must be narrow string "
4566 "literals", name);
7cb73573 4567 return false;
4c5cf0b2
JM
4568 }
4569 const char *begin = TREE_STRING_POINTER (elt);
4570 const char *end = begin + TREE_STRING_LENGTH (elt);
4571 for (const char *p = begin; p != end; ++p)
4572 {
4573 char c = *p;
4574 if (p == begin)
4575 {
4576 if (!ISALPHA (c) && c != '_')
4577 {
4578 error ("arguments to the %qE attribute must contain valid "
4579 "identifiers", name);
4580 inform (input_location, "%<%c%> is not a valid first "
4581 "character for an identifier", c);
7cb73573 4582 return false;
4c5cf0b2
JM
4583 }
4584 }
4585 else if (p == end - 1)
4586 gcc_assert (c == 0);
4587 else
4588 {
4589 if (!ISALNUM (c) && c != '_')
4590 {
4591 error ("arguments to the %qE attribute must contain valid "
4592 "identifiers", name);
4593 inform (input_location, "%<%c%> is not a valid character "
4594 "in an identifier", c);
7cb73573 4595 return false;
4c5cf0b2
JM
4596 }
4597 }
4598 }
4599 }
7cb73573
JM
4600 return true;
4601}
4602
4603/* Handle an "abi_tag" attribute; arguments as in
4604 struct attribute_spec.handler. */
4605
4606static tree
4607handle_abi_tag_attribute (tree* node, tree name, tree args,
4608 int flags, bool* no_add_attrs)
4609{
4610 if (!check_abi_tag_args (args, name))
4611 goto fail;
4c5cf0b2 4612
7dbb85a7
JM
4613 if (TYPE_P (*node))
4614 {
73243d63 4615 if (!OVERLOAD_TYPE_P (*node))
7dbb85a7
JM
4616 {
4617 error ("%qE attribute applied to non-class, non-enum type %qT",
4618 name, *node);
4619 goto fail;
4620 }
4621 else if (!(flags & (int)ATTR_FLAG_TYPE_IN_PLACE))
4622 {
4623 error ("%qE attribute applied to %qT after its definition",
4624 name, *node);
4625 goto fail;
4626 }
7b3bc1f3
MP
4627 else if (CLASS_TYPE_P (*node)
4628 && CLASSTYPE_TEMPLATE_INSTANTIATION (*node))
2982147e
JM
4629 {
4630 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
4631 "template instantiation %qT", name, *node);
4632 goto fail;
4633 }
7b3bc1f3
MP
4634 else if (CLASS_TYPE_P (*node)
4635 && CLASSTYPE_TEMPLATE_SPECIALIZATION (*node))
2982147e
JM
4636 {
4637 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
4638 "template specialization %qT", name, *node);
4639 goto fail;
4640 }
7dbb85a7
JM
4641
4642 tree attributes = TYPE_ATTRIBUTES (*node);
4643 tree decl = TYPE_NAME (*node);
4644
4645 /* Make sure all declarations have the same abi tags. */
4646 if (DECL_SOURCE_LOCATION (decl) != input_location)
4647 {
4648 if (!check_abi_tag_redeclaration (decl,
4649 lookup_attribute ("abi_tag",
4650 attributes),
4651 args))
4652 goto fail;
4653 }
4654 }
4655 else
4656 {
56a6f1d3 4657 if (!VAR_OR_FUNCTION_DECL_P (*node))
7dbb85a7 4658 {
7cb73573
JM
4659 error ("%qE attribute applied to non-function, non-variable %qD",
4660 name, *node);
7dbb85a7
JM
4661 goto fail;
4662 }
4663 else if (DECL_LANGUAGE (*node) == lang_c)
4664 {
7cb73573 4665 error ("%qE attribute applied to extern \"C\" declaration %qD",
7dbb85a7
JM
4666 name, *node);
4667 goto fail;
4668 }
4669 }
4670
4671 return NULL_TREE;
4672
4673 fail:
4674 *no_add_attrs = true;
4675 return NULL_TREE;
4676}
4677
87533b37
MM
4678/* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the
4679 thing pointed to by the constant. */
4680
4681tree
b57b79f7 4682make_ptrmem_cst (tree type, tree member)
87533b37
MM
4683{
4684 tree ptrmem_cst = make_node (PTRMEM_CST);
87533b37
MM
4685 TREE_TYPE (ptrmem_cst) = type;
4686 PTRMEM_CST_MEMBER (ptrmem_cst) = member;
4687 return ptrmem_cst;
4688}
4689
e9525111 4690/* Build a variant of TYPE that has the indicated ATTRIBUTES. May
51035976 4691 return an existing type if an appropriate type already exists. */
e9525111
MM
4692
4693tree
4694cp_build_type_attribute_variant (tree type, tree attributes)
4695{
4696 tree new_type;
4697
4698 new_type = build_type_attribute_variant (type, attributes);
3a55fb4c
JM
4699 if (TREE_CODE (new_type) == FUNCTION_TYPE
4700 || TREE_CODE (new_type) == METHOD_TYPE)
2eed8e37
BK
4701 {
4702 new_type = build_exception_variant (new_type,
4703 TYPE_RAISES_EXCEPTIONS (type));
4704 new_type = build_ref_qualified_type (new_type,
4705 type_memfn_rqual (type));
4706 }
8e30dcf3
JM
4707
4708 /* Making a new main variant of a class type is broken. */
4709 gcc_assert (!CLASS_TYPE_P (type) || new_type == type);
4710
e9525111
MM
4711 return new_type;
4712}
4713
2dff8956 4714/* Return TRUE if TYPE1 and TYPE2 are identical for type hashing purposes.
1906d6b4 4715 Called only after doing all language independent checks. */
2dff8956
JJ
4716
4717bool
4718cxx_type_hash_eq (const_tree typea, const_tree typeb)
4719{
220e83ca
KT
4720 gcc_assert (TREE_CODE (typea) == FUNCTION_TYPE
4721 || TREE_CODE (typea) == METHOD_TYPE);
2dff8956 4722
1906d6b4
JM
4723 if (type_memfn_rqual (typea) != type_memfn_rqual (typeb))
4724 return false;
2dff8956 4725 return comp_except_specs (TYPE_RAISES_EXCEPTIONS (typea),
3a55fb4c 4726 TYPE_RAISES_EXCEPTIONS (typeb), ce_exact);
2dff8956
JJ
4727}
4728
27c825c5
JM
4729/* Copy the language-specific type variant modifiers from TYPEB to TYPEA. For
4730 C++, these are the exception-specifier and ref-qualifier. */
4731
4732tree
4733cxx_copy_lang_qualifiers (const_tree typea, const_tree typeb)
4734{
4735 tree type = CONST_CAST_TREE (typea);
4736 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
4737 {
4738 type = build_exception_variant (type, TYPE_RAISES_EXCEPTIONS (typeb));
4739 type = build_ref_qualified_type (type, type_memfn_rqual (typeb));
4740 }
4741 return type;
4742}
4743
25af8512 4744/* Apply FUNC to all language-specific sub-trees of TP in a pre-order
350fae66 4745 traversal. Called from walk_tree. */
25af8512 4746
9f63daea 4747tree
350fae66 4748cp_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func,
6e2830c3 4749 void *data, hash_set<tree> *pset)
25af8512
AO
4750{
4751 enum tree_code code = TREE_CODE (*tp);
4752 tree result;
9f63daea 4753
25af8512
AO
4754#define WALK_SUBTREE(NODE) \
4755 do \
4756 { \
14588106 4757 result = cp_walk_tree (&(NODE), func, data, pset); \
6de9cd9a 4758 if (result) goto out; \
25af8512
AO
4759 } \
4760 while (0)
4761
4762 /* Not one of the easy cases. We must explicitly go through the
4763 children. */
6de9cd9a 4764 result = NULL_TREE;
25af8512
AO
4765 switch (code)
4766 {
4767 case DEFAULT_ARG:
4768 case TEMPLATE_TEMPLATE_PARM:
4769 case BOUND_TEMPLATE_TEMPLATE_PARM:
b8c6534b 4770 case UNBOUND_CLASS_TEMPLATE:
25af8512
AO
4771 case TEMPLATE_PARM_INDEX:
4772 case TEMPLATE_TYPE_PARM:
4773 case TYPENAME_TYPE:
4774 case TYPEOF_TYPE:
a0d260fc 4775 case UNDERLYING_TYPE:
da1d7781 4776 /* None of these have subtrees other than those already walked
0cbd7506 4777 above. */
25af8512
AO
4778 *walk_subtrees_p = 0;
4779 break;
4780
5d80a306 4781 case BASELINK:
88b811bd
JM
4782 if (BASELINK_QUALIFIED_P (*tp))
4783 WALK_SUBTREE (BINFO_TYPE (BASELINK_ACCESS_BINFO (*tp)));
5d80a306
DG
4784 WALK_SUBTREE (BASELINK_FUNCTIONS (*tp));
4785 *walk_subtrees_p = 0;
4786 break;
4787
25af8512
AO
4788 case PTRMEM_CST:
4789 WALK_SUBTREE (TREE_TYPE (*tp));
4790 *walk_subtrees_p = 0;
4791 break;
4792
4793 case TREE_LIST:
5dae1114 4794 WALK_SUBTREE (TREE_PURPOSE (*tp));
25af8512
AO
4795 break;
4796
4797 case OVERLOAD:
4798 WALK_SUBTREE (OVL_FUNCTION (*tp));
4799 WALK_SUBTREE (OVL_CHAIN (*tp));
4800 *walk_subtrees_p = 0;
4439d02f
DG
4801 break;
4802
4803 case USING_DECL:
4804 WALK_SUBTREE (DECL_NAME (*tp));
4805 WALK_SUBTREE (USING_DECL_SCOPE (*tp));
4806 WALK_SUBTREE (USING_DECL_DECLS (*tp));
4807 *walk_subtrees_p = 0;
25af8512
AO
4808 break;
4809
4810 case RECORD_TYPE:
4811 if (TYPE_PTRMEMFUNC_P (*tp))
35abb8ed 4812 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (*tp));
25af8512
AO
4813 break;
4814
5d80a306
DG
4815 case TYPE_ARGUMENT_PACK:
4816 case NONTYPE_ARGUMENT_PACK:
4817 {
4818 tree args = ARGUMENT_PACK_ARGS (*tp);
4819 int i, len = TREE_VEC_LENGTH (args);
4820 for (i = 0; i < len; i++)
4821 WALK_SUBTREE (TREE_VEC_ELT (args, i));
4822 }
4823 break;
4824
4825 case TYPE_PACK_EXPANSION:
4826 WALK_SUBTREE (TREE_TYPE (*tp));
c67dd256 4827 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp));
5d80a306
DG
4828 *walk_subtrees_p = 0;
4829 break;
4830
4831 case EXPR_PACK_EXPANSION:
4832 WALK_SUBTREE (TREE_OPERAND (*tp, 0));
c67dd256 4833 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp));
5d80a306
DG
4834 *walk_subtrees_p = 0;
4835 break;
4836
4837 case CAST_EXPR:
a7cbc517
JJ
4838 case REINTERPRET_CAST_EXPR:
4839 case STATIC_CAST_EXPR:
4840 case CONST_CAST_EXPR:
4841 case DYNAMIC_CAST_EXPR:
a4474a38 4842 case IMPLICIT_CONV_EXPR:
5d80a306
DG
4843 if (TREE_TYPE (*tp))
4844 WALK_SUBTREE (TREE_TYPE (*tp));
4845
4846 {
4847 int i;
4848 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (*tp)); ++i)
4849 WALK_SUBTREE (TREE_OPERAND (*tp, i));
4850 }
4851 *walk_subtrees_p = 0;
4852 break;
4853
cb68ec50
PC
4854 case TRAIT_EXPR:
4855 WALK_SUBTREE (TRAIT_EXPR_TYPE1 (*tp));
4856 WALK_SUBTREE (TRAIT_EXPR_TYPE2 (*tp));
4857 *walk_subtrees_p = 0;
4858 break;
4859
3ad6a8e1
DG
4860 case DECLTYPE_TYPE:
4861 WALK_SUBTREE (DECLTYPE_TYPE_EXPR (*tp));
4862 *walk_subtrees_p = 0;
4863 break;
4864
971e17ff
AS
4865 case REQUIRES_EXPR:
4866 // Only recurse through the nested expression. Do not
4867 // walk the parameter list. Doing so causes false
4868 // positives in the pack expansion checker since the
4869 // requires parameters are introduced as pack expansions.
4870 WALK_SUBTREE (TREE_OPERAND (*tp, 1));
4871 *walk_subtrees_p = 0;
4872 break;
3ad6a8e1 4873
e43ebb12
JJ
4874 case DECL_EXPR:
4875 /* User variables should be mentioned in BIND_EXPR_VARS
4876 and their initializers and sizes walked when walking
4877 the containing BIND_EXPR. Compiler temporaries are
4878 handled here. */
4879 if (VAR_P (TREE_OPERAND (*tp, 0))
4880 && DECL_ARTIFICIAL (TREE_OPERAND (*tp, 0))
4881 && !TREE_STATIC (TREE_OPERAND (*tp, 0)))
4882 {
4883 tree decl = TREE_OPERAND (*tp, 0);
4884 WALK_SUBTREE (DECL_INITIAL (decl));
4885 WALK_SUBTREE (DECL_SIZE (decl));
4886 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
4887 }
4888 break;
4889
25af8512 4890 default:
350fae66 4891 return NULL_TREE;
25af8512
AO
4892 }
4893
4894 /* We didn't find what we were looking for. */
6de9cd9a 4895 out:
6de9cd9a 4896 return result;
25af8512
AO
4897
4898#undef WALK_SUBTREE
4899}
4900
b655f214
MM
4901/* Like save_expr, but for C++. */
4902
4903tree
4904cp_save_expr (tree expr)
4905{
4906 /* There is no reason to create a SAVE_EXPR within a template; if
4907 needed, we can create the SAVE_EXPR when instantiating the
4908 template. Furthermore, the middle-end cannot handle C++-specific
4909 tree codes. */
4910 if (processing_template_decl)
4911 return expr;
4912 return save_expr (expr);
4913}
4914
87e3dbc9
MM
4915/* Initialize tree.c. */
4916
0a818f84 4917void
b57b79f7 4918init_tree (void)
0a818f84 4919{
2a22f99c 4920 list_hash_table = hash_table<list_hasher>::create_ggc (61);
d067e05f 4921 register_scoped_attributes (std_attribute_table, NULL);
0a818f84
GRK
4922}
4923
872f37f9 4924/* Returns the kind of special function that DECL (a FUNCTION_DECL)
50ad9642
MM
4925 is. Note that sfk_none is zero, so this function can be used as a
4926 predicate to test whether or not DECL is a special function. */
872f37f9
MM
4927
4928special_function_kind
58f9752a 4929special_function_p (const_tree decl)
872f37f9
MM
4930{
4931 /* Rather than doing all this stuff with magic names, we should
4932 probably have a field of type `special_function_kind' in
4933 DECL_LANG_SPECIFIC. */
31f7f784 4934 if (DECL_INHERITED_CTOR (decl))
85b5d65a 4935 return sfk_inheriting_constructor;
872f37f9
MM
4936 if (DECL_COPY_CONSTRUCTOR_P (decl))
4937 return sfk_copy_constructor;
d5f4eddd
JM
4938 if (DECL_MOVE_CONSTRUCTOR_P (decl))
4939 return sfk_move_constructor;
872f37f9
MM
4940 if (DECL_CONSTRUCTOR_P (decl))
4941 return sfk_constructor;
137073d3
NS
4942 if (DECL_ASSIGNMENT_OPERATOR_P (decl)
4943 && DECL_OVERLOADED_OPERATOR_IS (decl, NOP_EXPR))
ac177431
JM
4944 {
4945 if (copy_fn_p (decl))
4946 return sfk_copy_assignment;
4947 if (move_fn_p (decl))
4948 return sfk_move_assignment;
4949 }
872f37f9
MM
4950 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
4951 return sfk_destructor;
4952 if (DECL_COMPLETE_DESTRUCTOR_P (decl))
4953 return sfk_complete_destructor;
4954 if (DECL_BASE_DESTRUCTOR_P (decl))
4955 return sfk_base_destructor;
4956 if (DECL_DELETING_DESTRUCTOR_P (decl))
4957 return sfk_deleting_destructor;
4958 if (DECL_CONV_FN_P (decl))
4959 return sfk_conversion;
a56c0ac0
JM
4960 if (deduction_guide_p (decl))
4961 return sfk_deduction_guide;
872f37f9
MM
4962
4963 return sfk_none;
4964}
7b019c19 4965
838dfd8a 4966/* Returns nonzero if TYPE is a character type, including wchar_t. */
7b019c19
MM
4967
4968int
b57b79f7 4969char_type_p (tree type)
7b019c19
MM
4970{
4971 return (same_type_p (type, char_type_node)
4972 || same_type_p (type, unsigned_char_type_node)
4973 || same_type_p (type, signed_char_type_node)
b6baa67d
KVH
4974 || same_type_p (type, char16_type_node)
4975 || same_type_p (type, char32_type_node)
7b019c19
MM
4976 || same_type_p (type, wchar_type_node));
4977}
ad50e811
MM
4978
4979/* Returns the kind of linkage associated with the indicated DECL. Th
4980 value returned is as specified by the language standard; it is
4981 independent of implementation details regarding template
4982 instantiation, etc. For example, it is possible that a declaration
4983 to which this function assigns external linkage would not show up
4984 as a global symbol when you run `nm' on the resulting object file. */
4985
4986linkage_kind
b57b79f7 4987decl_linkage (tree decl)
ad50e811
MM
4988{
4989 /* This function doesn't attempt to calculate the linkage from first
4990 principles as given in [basic.link]. Instead, it makes use of
4991 the fact that we have already set TREE_PUBLIC appropriately, and
4992 then handles a few special cases. Ideally, we would calculate
4993 linkage first, and then transform that into a concrete
4994 implementation. */
4995
4996 /* Things that don't have names have no linkage. */
4997 if (!DECL_NAME (decl))
4998 return lk_none;
4999
c02cdc25
TT
5000 /* Fields have no linkage. */
5001 if (TREE_CODE (decl) == FIELD_DECL)
5002 return lk_none;
5003
ad50e811
MM
5004 /* Things that are TREE_PUBLIC have external linkage. */
5005 if (TREE_PUBLIC (decl))
5006 return lk_external;
3db45ab5 5007
effdaefe
JM
5008 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor variants,
5009 check one of the "clones" for the real linkage. */
5010 if ((DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
5011 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl))
5012 && DECL_CHAIN (decl)
5013 && DECL_CLONED_FUNCTION (DECL_CHAIN (decl)))
5014 return decl_linkage (DECL_CHAIN (decl));
5015
b70f0f48
JM
5016 if (TREE_CODE (decl) == NAMESPACE_DECL)
5017 return lk_external;
5018
3db45ab5 5019 /* Linkage of a CONST_DECL depends on the linkage of the enumeration
3f774254
DB
5020 type. */
5021 if (TREE_CODE (decl) == CONST_DECL)
8d0d1915 5022 return decl_linkage (TYPE_NAME (DECL_CONTEXT (decl)));
ad50e811 5023
ad50e811
MM
5024 /* Things in local scope do not have linkage, if they don't have
5025 TREE_PUBLIC set. */
5026 if (decl_function_context (decl))
5027 return lk_none;
5028
b70f0f48 5029 /* Members of the anonymous namespace also have TREE_PUBLIC unset, but
d5d0ed2d
JM
5030 are considered to have external linkage for language purposes, as do
5031 template instantiations on targets without weak symbols. DECLs really
5032 meant to have internal linkage have DECL_THIS_STATIC set. */
ce41114b 5033 if (TREE_CODE (decl) == TYPE_DECL)
b70f0f48 5034 return lk_external;
cb6da767 5035 if (VAR_OR_FUNCTION_DECL_P (decl))
ce41114b
JJ
5036 {
5037 if (!DECL_THIS_STATIC (decl))
5038 return lk_external;
5039
5040 /* Static data members and static member functions from classes
5041 in anonymous namespace also don't have TREE_PUBLIC set. */
5042 if (DECL_CLASS_CONTEXT (decl))
5043 return lk_external;
5044 }
b70f0f48 5045
ad50e811
MM
5046 /* Everything else has internal linkage. */
5047 return lk_internal;
5048}
b95ca513
JM
5049
5050/* Returns the storage duration of the object or reference associated with
5051 the indicated DECL, which should be a VAR_DECL or PARM_DECL. */
5052
5053duration_kind
5054decl_storage_duration (tree decl)
5055{
5056 if (TREE_CODE (decl) == PARM_DECL)
5057 return dk_auto;
5058 if (TREE_CODE (decl) == FUNCTION_DECL)
5059 return dk_static;
5a6ccc94 5060 gcc_assert (VAR_P (decl));
b95ca513
JM
5061 if (!TREE_STATIC (decl)
5062 && !DECL_EXTERNAL (decl))
5063 return dk_auto;
3048c0c7 5064 if (CP_DECL_THREAD_LOCAL_P (decl))
b95ca513
JM
5065 return dk_thread;
5066 return dk_static;
5067}
6f30f1f1 5068\f
9beafc83
MM
5069/* EXP is an expression that we want to pre-evaluate. Returns (in
5070 *INITP) an expression that will perform the pre-evaluation. The
5071 value returned by this function is a side-effect free expression
5072 equivalent to the pre-evaluated expression. Callers must ensure
5073 that *INITP is evaluated before EXP. */
6f30f1f1
JM
5074
5075tree
b57b79f7 5076stabilize_expr (tree exp, tree* initp)
6f30f1f1
JM
5077{
5078 tree init_expr;
5079
5080 if (!TREE_SIDE_EFFECTS (exp))
9beafc83 5081 init_expr = NULL_TREE;
982058cb
PC
5082 else if (VOID_TYPE_P (TREE_TYPE (exp)))
5083 {
989e6706 5084 init_expr = exp;
632f2871 5085 exp = void_node;
982058cb 5086 }
e3edeff4
JM
5087 /* There are no expressions with REFERENCE_TYPE, but there can be call
5088 arguments with such a type; just treat it as a pointer. */
5089 else if (TREE_CODE (TREE_TYPE (exp)) == REFERENCE_TYPE
fa07d25b 5090 || SCALAR_TYPE_P (TREE_TYPE (exp))
c3edc633 5091 || !glvalue_p (exp))
6f30f1f1
JM
5092 {
5093 init_expr = get_target_expr (exp);
5094 exp = TARGET_EXPR_SLOT (init_expr);
fc2bfea1
JM
5095 if (CLASS_TYPE_P (TREE_TYPE (exp)))
5096 exp = move (exp);
5097 else
5098 exp = rvalue (exp);
6f30f1f1
JM
5099 }
5100 else
5101 {
72b3e203 5102 bool xval = !lvalue_p (exp);
93c0e0bb 5103 exp = cp_build_addr_expr (exp, tf_warning_or_error);
6f30f1f1
JM
5104 init_expr = get_target_expr (exp);
5105 exp = TARGET_EXPR_SLOT (init_expr);
04757a2a 5106 exp = cp_build_fold_indirect_ref (exp);
883fff6c
JM
5107 if (xval)
5108 exp = move (exp);
6f30f1f1 5109 }
6f30f1f1 5110 *initp = init_expr;
9beafc83
MM
5111
5112 gcc_assert (!TREE_SIDE_EFFECTS (exp));
6f30f1f1
JM
5113 return exp;
5114}
6de9cd9a 5115
be93747e 5116/* Add NEW_EXPR, an expression whose value we don't care about, after the
40aac948
JM
5117 similar expression ORIG. */
5118
5119tree
be93747e 5120add_stmt_to_compound (tree orig, tree new_expr)
40aac948 5121{
be93747e 5122 if (!new_expr || !TREE_SIDE_EFFECTS (new_expr))
40aac948
JM
5123 return orig;
5124 if (!orig || !TREE_SIDE_EFFECTS (orig))
be93747e
KG
5125 return new_expr;
5126 return build2 (COMPOUND_EXPR, void_type_node, orig, new_expr);
40aac948
JM
5127}
5128
9beafc83
MM
5129/* Like stabilize_expr, but for a call whose arguments we want to
5130 pre-evaluate. CALL is modified in place to use the pre-evaluated
5131 arguments, while, upon return, *INITP contains an expression to
5132 compute the arguments. */
6de9cd9a
DN
5133
5134void
5135stabilize_call (tree call, tree *initp)
5136{
5137 tree inits = NULL_TREE;
5039610b
SL
5138 int i;
5139 int nargs = call_expr_nargs (call);
6de9cd9a 5140
28267cfc
JJ
5141 if (call == error_mark_node || processing_template_decl)
5142 {
5143 *initp = NULL_TREE;
5144 return;
5145 }
6de9cd9a 5146
5039610b 5147 gcc_assert (TREE_CODE (call) == CALL_EXPR);
6de9cd9a 5148
5039610b
SL
5149 for (i = 0; i < nargs; i++)
5150 {
5151 tree init;
5152 CALL_EXPR_ARG (call, i) =
5153 stabilize_expr (CALL_EXPR_ARG (call, i), &init);
5154 inits = add_stmt_to_compound (inits, init);
5155 }
5156
5157 *initp = inits;
5158}
5159
5160/* Like stabilize_expr, but for an AGGR_INIT_EXPR whose arguments we want
5161 to pre-evaluate. CALL is modified in place to use the pre-evaluated
5162 arguments, while, upon return, *INITP contains an expression to
5163 compute the arguments. */
5164
81bd268c 5165static void
5039610b
SL
5166stabilize_aggr_init (tree call, tree *initp)
5167{
5168 tree inits = NULL_TREE;
5169 int i;
5170 int nargs = aggr_init_expr_nargs (call);
5171
5172 if (call == error_mark_node)
5173 return;
5174
5175 gcc_assert (TREE_CODE (call) == AGGR_INIT_EXPR);
5176
5177 for (i = 0; i < nargs; i++)
5178 {
5179 tree init;
5180 AGGR_INIT_EXPR_ARG (call, i) =
5181 stabilize_expr (AGGR_INIT_EXPR_ARG (call, i), &init);
5182 inits = add_stmt_to_compound (inits, init);
5183 }
6de9cd9a
DN
5184
5185 *initp = inits;
5186}
5187
9beafc83
MM
5188/* Like stabilize_expr, but for an initialization.
5189
5190 If the initialization is for an object of class type, this function
5191 takes care not to introduce additional temporaries.
5192
5193 Returns TRUE iff the expression was successfully pre-evaluated,
66edf32a 5194 i.e., if INIT is now side-effect free, except for, possibly, a
9beafc83 5195 single call to a constructor. */
6de9cd9a
DN
5196
5197bool
5198stabilize_init (tree init, tree *initp)
5199{
5200 tree t = init;
5201
9beafc83
MM
5202 *initp = NULL_TREE;
5203
28267cfc 5204 if (t == error_mark_node || processing_template_decl)
6de9cd9a
DN
5205 return true;
5206
9beafc83
MM
5207 if (TREE_CODE (t) == INIT_EXPR)
5208 t = TREE_OPERAND (t, 1);
5209 if (TREE_CODE (t) == TARGET_EXPR)
5210 t = TARGET_EXPR_INITIAL (t);
66edf32a
JM
5211
5212 /* If the RHS can be stabilized without breaking copy elision, stabilize
5213 it. We specifically don't stabilize class prvalues here because that
5214 would mean an extra copy, but they might be stabilized below. */
5215 if (TREE_CODE (init) == INIT_EXPR
5216 && TREE_CODE (t) != CONSTRUCTOR
5217 && TREE_CODE (t) != AGGR_INIT_EXPR
5218 && (SCALAR_TYPE_P (TREE_TYPE (t))
c3edc633 5219 || glvalue_p (t)))
66edf32a
JM
5220 {
5221 TREE_OPERAND (init, 1) = stabilize_expr (t, initp);
5222 return true;
5223 }
5224
5225 if (TREE_CODE (t) == COMPOUND_EXPR
5226 && TREE_CODE (init) == INIT_EXPR)
5227 {
5228 tree last = expr_last (t);
5229 /* Handle stabilizing the EMPTY_CLASS_EXPR pattern. */
5230 if (!TREE_SIDE_EFFECTS (last))
5231 {
5232 *initp = t;
5233 TREE_OPERAND (init, 1) = last;
5234 return true;
5235 }
5236 }
5237
b9d6b015
JM
5238 if (TREE_CODE (t) == CONSTRUCTOR)
5239 {
5240 /* Aggregate initialization: stabilize each of the field
5241 initializers. */
5242 unsigned i;
0c59fd2f 5243 constructor_elt *ce;
b9d6b015 5244 bool good = true;
9771b263
DN
5245 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
5246 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
0c59fd2f
JM
5247 {
5248 tree type = TREE_TYPE (ce->value);
5249 tree subinit;
5250 if (TREE_CODE (type) == REFERENCE_TYPE
5251 || SCALAR_TYPE_P (type))
5252 ce->value = stabilize_expr (ce->value, &subinit);
5253 else if (!stabilize_init (ce->value, &subinit))
5254 good = false;
5255 *initp = add_stmt_to_compound (*initp, subinit);
5256 }
b9d6b015
JM
5257 return good;
5258 }
9beafc83 5259
5039610b 5260 if (TREE_CODE (t) == CALL_EXPR)
9beafc83
MM
5261 {
5262 stabilize_call (t, initp);
5263 return true;
6de9cd9a
DN
5264 }
5265
5039610b
SL
5266 if (TREE_CODE (t) == AGGR_INIT_EXPR)
5267 {
5268 stabilize_aggr_init (t, initp);
5269 return true;
5270 }
5271
9beafc83
MM
5272 /* The initialization is being performed via a bitwise copy -- and
5273 the item copied may have side effects. */
4bbbcbf6 5274 return !TREE_SIDE_EFFECTS (init);
6de9cd9a
DN
5275}
5276
015c2c66
MM
5277/* Returns true if a cast to TYPE may appear in an integral constant
5278 expression. */
5279
5280bool
5281cast_valid_in_integral_constant_expression_p (tree type)
5282{
5283 return (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
604b2bfc 5284 || cxx_dialect >= cxx11
015c2c66
MM
5285 || dependent_type_p (type)
5286 || type == error_mark_node);
5287}
5288
4537ec0c
DN
5289/* Return true if we need to fix linkage information of DECL. */
5290
5291static bool
5292cp_fix_function_decl_p (tree decl)
5293{
5294 /* Skip if DECL is not externally visible. */
5295 if (!TREE_PUBLIC (decl))
5296 return false;
5297
5298 /* We need to fix DECL if it a appears to be exported but with no
5299 function body. Thunks do not have CFGs and we may need to
5300 handle them specially later. */
5301 if (!gimple_has_body_p (decl)
5302 && !DECL_THUNK_P (decl)
5303 && !DECL_EXTERNAL (decl))
87501227 5304 {
d52f5295 5305 struct cgraph_node *node = cgraph_node::get (decl);
87501227
JJ
5306
5307 /* Don't fix same_body aliases. Although they don't have their own
5308 CFG, they share it with what they alias to. */
67348ccc
DM
5309 if (!node || !node->alias
5310 || !vec_safe_length (node->ref_list.references))
87501227
JJ
5311 return true;
5312 }
4537ec0c
DN
5313
5314 return false;
5315}
5316
5317/* Clean the C++ specific parts of the tree T. */
5318
5319void
5320cp_free_lang_data (tree t)
5321{
5322 if (TREE_CODE (t) == METHOD_TYPE
5323 || TREE_CODE (t) == FUNCTION_TYPE)
5324 {
5325 /* Default args are not interesting anymore. */
5326 tree argtypes = TYPE_ARG_TYPES (t);
5327 while (argtypes)
5328 {
5329 TREE_PURPOSE (argtypes) = 0;
5330 argtypes = TREE_CHAIN (argtypes);
5331 }
5332 }
5333 else if (TREE_CODE (t) == FUNCTION_DECL
5334 && cp_fix_function_decl_p (t))
5335 {
5336 /* If T is used in this translation unit at all, the definition
5337 must exist somewhere else since we have decided to not emit it
5338 in this TU. So make it an external reference. */
5339 DECL_EXTERNAL (t) = 1;
5340 TREE_STATIC (t) = 0;
5341 }
b4ca4f9e 5342 if (TREE_CODE (t) == NAMESPACE_DECL)
44e00a7a
NS
5343 /* We do not need the leftover chaining of namespaces from the
5344 binding level. */
5345 DECL_CHAIN (t) = NULL_TREE;
4537ec0c
DN
5346}
5347
bffad7f1
SB
5348/* Stub for c-common. Please keep in sync with c-decl.c.
5349 FIXME: If address space support is target specific, then this
5350 should be a C target hook. But currently this is not possible,
5351 because this function is called via REGISTER_TARGET_PRAGMAS. */
5352void
12308bc6 5353c_register_addr_space (const char * /*word*/, addr_space_t /*as*/)
bffad7f1
SB
5354{
5355}
5356
d26e5986
NF
5357/* Return the number of operands in T that we care about for things like
5358 mangling. */
5359
5360int
5361cp_tree_operand_length (const_tree t)
5362{
5363 enum tree_code code = TREE_CODE (t);
5364
5fdfa03e
PP
5365 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
5366 return VL_EXP_OPERAND_LENGTH (t);
d26e5986 5367
5fdfa03e 5368 return cp_tree_code_length (code);
d26e5986 5369}
30b07d03 5370
fcb9363e
PP
5371/* Like cp_tree_operand_length, but takes a tree_code CODE. */
5372
5373int
5374cp_tree_code_length (enum tree_code code)
5375{
5376 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
5377
5378 switch (code)
5379 {
5380 case PREINCREMENT_EXPR:
5381 case PREDECREMENT_EXPR:
5382 case POSTINCREMENT_EXPR:
5383 case POSTDECREMENT_EXPR:
5384 return 1;
5385
5386 case ARRAY_REF:
5387 return 2;
5388
5389 case EXPR_PACK_EXPANSION:
5390 return 1;
5391
5392 default:
5393 return TREE_CODE_LENGTH (code);
5394 }
5395}
5396
4a5a49b0
JJ
5397/* Wrapper around warn_deprecated_use that doesn't warn for
5398 current_class_type. */
5399
5400void
5401cp_warn_deprecated_use (tree node)
5402{
5403 if (TYPE_P (node)
5404 && current_class_type
5405 && TYPE_MAIN_VARIANT (node) == current_class_type)
5406 return;
5407 warn_deprecated_use (node, NULL_TREE);
5408}
5409
30b07d03
PC
5410/* Implement -Wzero_as_null_pointer_constant. Return true if the
5411 conditions for the warning hold, false otherwise. */
5412bool
5413maybe_warn_zero_as_null_pointer_constant (tree expr, location_t loc)
5414{
5415 if (c_inhibit_evaluation_warnings == 0
5416 && !NULLPTR_TYPE_P (TREE_TYPE (expr)))
5417 {
5418 warning_at (loc, OPT_Wzero_as_null_pointer_constant,
5419 "zero as null pointer constant");
5420 return true;
5421 }
5422 return false;
5423}
e2500fed
GK
5424\f
5425#if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
5426/* Complain that some language-specific thing hanging off a tree
5427 node has been accessed improperly. */
5428
5429void
b57b79f7 5430lang_check_failed (const char* file, int line, const char* function)
e2500fed
GK
5431{
5432 internal_error ("lang_* check: failed in %s, at %s:%d",
5433 function, trim_filename (file), line);
5434}
5435#endif /* ENABLE_TREE_CHECKING */
5436
9a004410
DM
5437#if CHECKING_P
5438
5439namespace selftest {
5440
5441/* Verify that lvalue_kind () works, for various expressions,
5442 and that location wrappers don't affect the results. */
5443
5444static void
5445test_lvalue_kind ()
5446{
5447 location_t loc = BUILTINS_LOCATION;
5448
5449 /* Verify constants and parameters, without and with
5450 location wrappers. */
5451 tree int_cst = build_int_cst (integer_type_node, 42);
5452 ASSERT_EQ (clk_none, lvalue_kind (int_cst));
5453
5454 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
5455 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
5456 ASSERT_EQ (clk_none, lvalue_kind (wrapped_int_cst));
5457
5458 tree string_lit = build_string (4, "foo");
5459 TREE_TYPE (string_lit) = char_array_type_node;
5460 string_lit = fix_string_type (string_lit);
5461 ASSERT_EQ (clk_ordinary, lvalue_kind (string_lit));
5462
5463 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
5464 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
5465 ASSERT_EQ (clk_ordinary, lvalue_kind (wrapped_string_lit));
5466
5467 tree parm = build_decl (UNKNOWN_LOCATION, PARM_DECL,
5468 get_identifier ("some_parm"),
5469 integer_type_node);
5470 ASSERT_EQ (clk_ordinary, lvalue_kind (parm));
5471
5472 tree wrapped_parm = maybe_wrap_with_location (parm, loc);
5473 ASSERT_TRUE (location_wrapper_p (wrapped_parm));
5474 ASSERT_EQ (clk_ordinary, lvalue_kind (wrapped_parm));
5475
5476 /* Verify that lvalue_kind of std::move on a parm isn't
5477 affected by location wrappers. */
5478 tree rvalue_ref_of_parm = move (parm);
5479 ASSERT_EQ (clk_rvalueref, lvalue_kind (rvalue_ref_of_parm));
5480 tree rvalue_ref_of_wrapped_parm = move (wrapped_parm);
5481 ASSERT_EQ (clk_rvalueref, lvalue_kind (rvalue_ref_of_wrapped_parm));
5482}
5483
5484/* Run all of the selftests within this file. */
5485
5486void
5487cp_tree_c_tests ()
5488{
5489 test_lvalue_kind ();
5490}
5491
5492} // namespace selftest
5493
5494#endif /* #if CHECKING_P */
5495
5496
e2500fed 5497#include "gt-cp-tree.h"