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