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