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