]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/tree.c
re PR c++/29470 (Using declaration access semantics change with templates)
[thirdparty/gcc.git] / gcc / cp / tree.c
CommitLineData
8d08fdba 1/* Language-dependent node constructors for parse phase of GNU compiler.
06ceef4e 2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
a7cbc517 3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
e77f031d 4 Free Software Foundation, Inc.
8d08fdba
MS
5 Hacked by Michael Tiemann (tiemann@cygnus.com)
6
f5adbb8d 7This file is part of GCC.
8d08fdba 8
f5adbb8d 9GCC is free software; you can redistribute it and/or modify
8d08fdba 10it under the terms of the GNU General Public License as published by
e77f031d 11the Free Software Foundation; either version 3, or (at your option)
8d08fdba
MS
12any later version.
13
f5adbb8d 14GCC is distributed in the hope that it will be useful,
8d08fdba
MS
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
e77f031d
NC
20along with GCC; see the file COPYING3. If not see
21<http://www.gnu.org/licenses/>. */
8d08fdba
MS
22
23#include "config.h"
8d052bc7 24#include "system.h"
4977bab6
ZW
25#include "coretypes.h"
26#include "tm.h"
8d08fdba
MS
27#include "tree.h"
28#include "cp-tree.h"
29#include "flags.h"
11ad4784 30#include "real.h"
28cbf42c 31#include "rtl.h"
12027a89 32#include "toplev.h"
46e8c075
MM
33#include "insn-config.h"
34#include "integrate.h"
25af8512 35#include "tree-inline.h"
e58a9aa1 36#include "debug.h"
8a3c9180 37#include "target.h"
41990f96 38#include "convert.h"
04941f76 39#include "tree-flow.h"
12027a89 40
b57b79f7
NN
41static tree bot_manip (tree *, int *, void *);
42static tree bot_replace (tree *, int *, void *);
43static tree build_cplus_array_type_1 (tree, tree);
44static int list_hash_eq (const void *, const void *);
45static hashval_t list_hash_pieces (tree, tree, tree);
46static hashval_t list_hash (const void *);
4af9e878 47static cp_lvalue_kind lvalue_p_1 (tree, int);
b57b79f7
NN
48static tree build_target_expr (tree, tree);
49static tree count_trees_r (tree *, int *, void *);
50static tree verify_stmt_tree_r (tree *, int *, void *);
a6f86b51 51static tree build_local_temp (tree);
b57b79f7
NN
52
53static tree handle_java_interface_attribute (tree *, tree, tree, int, bool *);
54static tree handle_com_interface_attribute (tree *, tree, tree, int, bool *);
55static tree handle_init_priority_attribute (tree *, tree, tree, int, bool *);
91d231cb 56
27b8d0cd
MM
57/* If REF is an lvalue, returns the kind of lvalue that REF is.
58 Otherwise, returns clk_none. If TREAT_CLASS_RVALUES_AS_LVALUES is
838dfd8a 59 nonzero, rvalues of class type are considered lvalues. */
8d08fdba 60
27b8d0cd 61static cp_lvalue_kind
4af9e878 62lvalue_p_1 (tree ref,
0cbd7506 63 int treat_class_rvalues_as_lvalues)
8ccc31eb 64{
27b8d0cd
MM
65 cp_lvalue_kind op1_lvalue_kind = clk_none;
66 cp_lvalue_kind op2_lvalue_kind = clk_none;
67
8af2fec4
RY
68 /* Expressions of reference type are sometimes wrapped in
69 INDIRECT_REFs. INDIRECT_REFs are just internal compiler
70 representation, not part of the language, so we have to look
71 through them. */
72 if (TREE_CODE (ref) == INDIRECT_REF
73 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0)))
74 == REFERENCE_TYPE)
75 return lvalue_p_1 (TREE_OPERAND (ref, 0),
76 treat_class_rvalues_as_lvalues);
77
8ccc31eb 78 if (TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
8af2fec4
RY
79 {
80 /* unnamed rvalue references are rvalues */
81 if (TYPE_REF_IS_RVALUE (TREE_TYPE (ref))
82 && TREE_CODE (ref) != PARM_DECL
83 && TREE_CODE (ref) != VAR_DECL
84 && TREE_CODE (ref) != COMPONENT_REF)
85 return clk_none;
86
d732e98f 87 /* lvalue references and named rvalue references are lvalues. */
8af2fec4
RY
88 return clk_ordinary;
89 }
8ccc31eb 90
394fd776 91 if (ref == current_class_ptr)
27b8d0cd 92 return clk_none;
8ccc31eb
MS
93
94 switch (TREE_CODE (ref))
95 {
8f4361eb
AP
96 case SAVE_EXPR:
97 return clk_none;
8ccc31eb 98 /* preincrements and predecrements are valid lvals, provided
e92cc029 99 what they refer to are valid lvals. */
8ccc31eb
MS
100 case PREINCREMENT_EXPR:
101 case PREDECREMENT_EXPR:
c7ae64f2
JM
102 case TRY_CATCH_EXPR:
103 case WITH_CLEANUP_EXPR:
69851283
MM
104 case REALPART_EXPR:
105 case IMAGPART_EXPR:
106 return lvalue_p_1 (TREE_OPERAND (ref, 0),
d18a8251 107 treat_class_rvalues_as_lvalues);
8ccc31eb 108
27b8d0cd
MM
109 case COMPONENT_REF:
110 op1_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 0),
d18a8251 111 treat_class_rvalues_as_lvalues);
c8b2e872 112 /* Look at the member designator. */
4af9e878 113 if (!op1_lvalue_kind)
0cbd7506 114 ;
4af9e878
JM
115 else if (is_overloaded_fn (TREE_OPERAND (ref, 1)))
116 /* The "field" can be a FUNCTION_DECL or an OVERLOAD in some
117 situations. */
118 op1_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 1),
119 treat_class_rvalues_as_lvalues);
e0d1297c 120 else if (DECL_C_BIT_FIELD (TREE_OPERAND (ref, 1)))
27b8d0cd
MM
121 {
122 /* Clear the ordinary bit. If this object was a class
123 rvalue we want to preserve that information. */
124 op1_lvalue_kind &= ~clk_ordinary;
cd0be382 125 /* The lvalue is for a bitfield. */
27b8d0cd
MM
126 op1_lvalue_kind |= clk_bitfield;
127 }
e0d1297c
NS
128 else if (DECL_PACKED (TREE_OPERAND (ref, 1)))
129 op1_lvalue_kind |= clk_packed;
9f63daea 130
27b8d0cd
MM
131 return op1_lvalue_kind;
132
8ccc31eb 133 case STRING_CST:
266b4890 134 case COMPOUND_LITERAL_EXPR:
27b8d0cd 135 return clk_ordinary;
8ccc31eb 136
e58a9aa1 137 case CONST_DECL:
8ccc31eb
MS
138 case VAR_DECL:
139 if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
140 && DECL_LANG_SPECIFIC (ref)
141 && DECL_IN_AGGR_P (ref))
27b8d0cd 142 return clk_none;
8ccc31eb
MS
143 case INDIRECT_REF:
144 case ARRAY_REF:
145 case PARM_DECL:
146 case RESULT_DECL:
59e76fc6 147 if (TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
27b8d0cd 148 return clk_ordinary;
8ccc31eb
MS
149 break;
150
8ccc31eb
MS
151 /* A currently unresolved scope ref. */
152 case SCOPE_REF:
315fb5db 153 gcc_unreachable ();
27b8d0cd
MM
154 case MAX_EXPR:
155 case MIN_EXPR:
d211a298
RS
156 /* Disallow <? and >? as lvalues if either argument side-effects. */
157 if (TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 0))
158 || TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 1)))
159 return clk_none;
27b8d0cd 160 op1_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 0),
d18a8251 161 treat_class_rvalues_as_lvalues);
27b8d0cd 162 op2_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 1),
d18a8251 163 treat_class_rvalues_as_lvalues);
8ccc31eb
MS
164 break;
165
166 case COND_EXPR:
42924ed7
JJ
167 op1_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 1)
168 ? TREE_OPERAND (ref, 1)
169 : TREE_OPERAND (ref, 0),
d18a8251 170 treat_class_rvalues_as_lvalues);
27b8d0cd 171 op2_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 2),
d18a8251 172 treat_class_rvalues_as_lvalues);
27b8d0cd 173 break;
8ccc31eb
MS
174
175 case MODIFY_EXPR:
27b8d0cd 176 return clk_ordinary;
8ccc31eb
MS
177
178 case COMPOUND_EXPR:
69851283 179 return lvalue_p_1 (TREE_OPERAND (ref, 1),
d18a8251 180 treat_class_rvalues_as_lvalues);
69851283
MM
181
182 case TARGET_EXPR:
27b8d0cd 183 return treat_class_rvalues_as_lvalues ? clk_class : clk_none;
69851283 184
356955cf 185 case VA_ARG_EXPR:
c0ad5a31
MM
186 return (treat_class_rvalues_as_lvalues
187 && CLASS_TYPE_P (TREE_TYPE (ref))
188 ? clk_class : clk_none);
189
190 case CALL_EXPR:
4e8dca1c
JM
191 /* Any class-valued call would be wrapped in a TARGET_EXPR. */
192 return clk_none;
69851283
MM
193
194 case FUNCTION_DECL:
195 /* All functions (except non-static-member functions) are
196 lvalues. */
9f63daea 197 return (DECL_NONSTATIC_MEMBER_FUNCTION_P (ref)
27b8d0cd 198 ? clk_none : clk_ordinary);
7f85441b 199
4af9e878
JM
200 case BASELINK:
201 /* We now represent a reference to a single static member function
202 with a BASELINK. */
203 return lvalue_p_1 (BASELINK_FUNCTIONS (ref),
204 treat_class_rvalues_as_lvalues);
205
d17811fd
MM
206 case NON_DEPENDENT_EXPR:
207 /* We must consider NON_DEPENDENT_EXPRs to be lvalues so that
208 things like "&E" where "E" is an expression with a
209 non-dependent type work. It is safe to be lenient because an
210 error will be issued when the template is instantiated if "E"
211 is not an lvalue. */
212 return clk_ordinary;
213
7f85441b
KG
214 default:
215 break;
8ccc31eb
MS
216 }
217
27b8d0cd
MM
218 /* If one operand is not an lvalue at all, then this expression is
219 not an lvalue. */
220 if (!op1_lvalue_kind || !op2_lvalue_kind)
221 return clk_none;
222
223 /* Otherwise, it's an lvalue, and it has all the odd properties
224 contributed by either operand. */
225 op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind;
226 /* It's not an ordinary lvalue if it involves either a bit-field or
227 a class rvalue. */
228 if ((op1_lvalue_kind & ~clk_ordinary) != clk_none)
229 op1_lvalue_kind &= ~clk_ordinary;
230 return op1_lvalue_kind;
8ccc31eb
MS
231}
232
aa6e8ed3
MM
233/* Returns the kind of lvalue that REF is, in the sense of
234 [basic.lval]. This function should really be named lvalue_p; it
235 computes the C++ definition of lvalue. */
236
237cp_lvalue_kind
4af9e878 238real_lvalue_p (tree ref)
aa6e8ed3 239{
9f63daea 240 return lvalue_p_1 (ref,
d18a8251 241 /*treat_class_rvalues_as_lvalues=*/0);
aa6e8ed3
MM
242}
243
27b8d0cd
MM
244/* This differs from real_lvalue_p in that class rvalues are
245 considered lvalues. */
69851283 246
8d08fdba 247int
4af9e878 248lvalue_p (tree ref)
8d08fdba 249{
9f63daea 250 return
d18a8251 251 (lvalue_p_1 (ref, /*class rvalue ok*/ 1) != clk_none);
6c6e776d
MA
252}
253
100d337a
MA
254/* Test whether DECL is a builtin that may appear in a
255 constant-expression. */
256
257bool
58f9752a 258builtin_valid_in_constant_expr_p (const_tree decl)
100d337a
MA
259{
260 /* At present BUILT_IN_CONSTANT_P is the only builtin we're allowing
261 in constant-expressions. We may want to add other builtins later. */
88a7beb7 262 return DECL_IS_BUILTIN_CONSTANT_P (decl);
100d337a
MA
263}
264
c506ca22
MM
265/* Build a TARGET_EXPR, initializing the DECL with the VALUE. */
266
267static tree
b57b79f7 268build_target_expr (tree decl, tree value)
c506ca22
MM
269{
270 tree t;
04941f76
AO
271
272#ifdef ENABLE_CHECKING
273 gcc_assert (VOID_TYPE_P (TREE_TYPE (value))
274 || TREE_TYPE (decl) == TREE_TYPE (value)
275 || useless_type_conversion_p (TREE_TYPE (decl),
276 TREE_TYPE (value)));
277#endif
c506ca22 278
f293ce4b
RS
279 t = build4 (TARGET_EXPR, TREE_TYPE (decl), decl, value,
280 cxx_maybe_build_cleanup (decl), NULL_TREE);
c506ca22
MM
281 /* We always set TREE_SIDE_EFFECTS so that expand_expr does not
282 ignore the TARGET_EXPR. If there really turn out to be no
283 side-effects, then the optimizer should be able to get rid of
284 whatever code is generated anyhow. */
285 TREE_SIDE_EFFECTS (t) = 1;
286
287 return t;
288}
289
a6f86b51
JM
290/* Return an undeclared local temporary of type TYPE for use in building a
291 TARGET_EXPR. */
292
293static tree
294build_local_temp (tree type)
295{
296 tree slot = build_decl (VAR_DECL, NULL_TREE, type);
297 DECL_ARTIFICIAL (slot) = 1;
78e0d62b 298 DECL_IGNORED_P (slot) = 1;
a6f86b51
JM
299 DECL_CONTEXT (slot) = current_function_decl;
300 layout_decl (slot, 0);
301 return slot;
302}
303
5039610b
SL
304/* Set various status flags when building an AGGR_INIT_EXPR object T. */
305
306static void
307process_aggr_init_operands (tree t)
308{
309 bool side_effects;
310
311 side_effects = TREE_SIDE_EFFECTS (t);
312 if (!side_effects)
313 {
314 int i, n;
315 n = TREE_OPERAND_LENGTH (t);
316 for (i = 1; i < n; i++)
317 {
318 tree op = TREE_OPERAND (t, i);
319 if (op && TREE_SIDE_EFFECTS (op))
320 {
321 side_effects = 1;
322 break;
323 }
324 }
325 }
326 TREE_SIDE_EFFECTS (t) = side_effects;
327}
328
329/* Build an AGGR_INIT_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE,
330 FN, and SLOT. NARGS is the number of call arguments which are specified
331 as a tree array ARGS. */
332
333static tree
334build_aggr_init_array (tree return_type, tree fn, tree slot, int nargs,
335 tree *args)
336{
337 tree t;
338 int i;
339
340 t = build_vl_exp (AGGR_INIT_EXPR, nargs + 3);
341 TREE_TYPE (t) = return_type;
342 AGGR_INIT_EXPR_FN (t) = fn;
343 AGGR_INIT_EXPR_SLOT (t) = slot;
344 for (i = 0; i < nargs; i++)
345 AGGR_INIT_EXPR_ARG (t, i) = args[i];
346 process_aggr_init_operands (t);
347 return t;
348}
349
350/* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
844ae01d 351 target. TYPE is the type to be initialized.
8d08fdba 352
844ae01d
JM
353 Build an AGGR_INIT_EXPR to represent the initialization. This function
354 differs from build_cplus_new in that an AGGR_INIT_EXPR can only be used
355 to initialize another object, whereas a TARGET_EXPR can either
356 initialize another object or create its own temporary object, and as a
357 result building up a TARGET_EXPR requires that the type's destructor be
358 callable. */
e92cc029 359
8d08fdba 360tree
844ae01d 361build_aggr_init_expr (tree type, tree init)
8d08fdba 362{
e1376b00 363 tree fn;
e8abc66f
MS
364 tree slot;
365 tree rval;
4977bab6 366 int is_ctor;
e8abc66f 367
27b8d0cd
MM
368 /* Make sure that we're not trying to create an instance of an
369 abstract class. */
5bb2f1e7 370 abstract_virtuals_error (NULL_TREE, type);
27b8d0cd 371
5039610b
SL
372 if (TREE_CODE (init) == CALL_EXPR)
373 fn = CALL_EXPR_FN (init);
374 else if (TREE_CODE (init) == AGGR_INIT_EXPR)
375 fn = AGGR_INIT_EXPR_FN (init);
376 else
06126ca2 377 return convert (type, init);
c11b6f21 378
4977bab6
ZW
379 is_ctor = (TREE_CODE (fn) == ADDR_EXPR
380 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
381 && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0)));
382
e1376b00
MM
383 /* We split the CALL_EXPR into its function and its arguments here.
384 Then, in expand_expr, we put them back together. The reason for
385 this is that this expression might be a default argument
386 expression. In that case, we need a new temporary every time the
387 expression is used. That's what break_out_target_exprs does; it
388 replaces every AGGR_INIT_EXPR with a copy that uses a fresh
389 temporary slot. Then, expand_expr builds up a call-expression
390 using the new slot. */
4977bab6
ZW
391
392 /* If we don't need to use a constructor to create an object of this
393 type, don't mess with AGGR_INIT_EXPR. */
394 if (is_ctor || TREE_ADDRESSABLE (type))
395 {
844ae01d
JM
396 slot = build_local_temp (type);
397
5039610b
SL
398 if (TREE_CODE(init) == CALL_EXPR)
399 rval = build_aggr_init_array (void_type_node, fn, slot,
400 call_expr_nargs (init),
401 CALL_EXPR_ARGP (init));
402 else
403 rval = build_aggr_init_array (void_type_node, fn, slot,
404 aggr_init_expr_nargs (init),
405 AGGR_INIT_EXPR_ARGP (init));
4977bab6
ZW
406 TREE_SIDE_EFFECTS (rval) = 1;
407 AGGR_INIT_VIA_CTOR_P (rval) = is_ctor;
408 }
409 else
410 rval = init;
411
844ae01d
JM
412 return rval;
413}
414
415/* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
416 target. TYPE is the type that this initialization should appear to
417 have.
418
419 Build an encapsulation of the initialization to perform
420 and return it so that it can be processed by language-independent
421 and language-specific expression expanders. */
422
423tree
424build_cplus_new (tree type, tree init)
425{
426 tree rval = build_aggr_init_expr (type, init);
427 tree slot;
428
429 if (TREE_CODE (rval) == AGGR_INIT_EXPR)
430 slot = AGGR_INIT_EXPR_SLOT (rval);
431 else if (TREE_CODE (rval) == CALL_EXPR)
432 slot = build_local_temp (type);
433 else
434 return rval;
435
9d85d30c 436 rval = build_target_expr (slot, rval);
c08cd4c1 437 TARGET_EXPR_IMPLICIT_P (rval) = 1;
8d08fdba 438
8d08fdba
MS
439 return rval;
440}
441
ab93b543 442/* Build a TARGET_EXPR using INIT to initialize a new temporary of the
c506ca22 443 indicated TYPE. */
aa36c081
JM
444
445tree
b57b79f7 446build_target_expr_with_type (tree init, tree type)
aa36c081 447{
50bc768d 448 gcc_assert (!VOID_TYPE_P (type));
59445d74 449
5062dbd5
JM
450 if (TREE_CODE (init) == TARGET_EXPR)
451 return init;
182609b5 452 else if (CLASS_TYPE_P (type) && !TYPE_HAS_TRIVIAL_INIT_REF (type)
7efc22ea 453 && !VOID_TYPE_P (TREE_TYPE (init))
4b5aa881 454 && TREE_CODE (init) != COND_EXPR
662eceda
MM
455 && TREE_CODE (init) != CONSTRUCTOR
456 && TREE_CODE (init) != VA_ARG_EXPR)
7efc22ea
JM
457 /* We need to build up a copy constructor call. A void initializer
458 means we're being called from bot_manip. COND_EXPR is a special
182609b5 459 case because we already have copies on the arms and we don't want
4b5aa881 460 another one here. A CONSTRUCTOR is aggregate initialization, which
662eceda
MM
461 is handled separately. A VA_ARG_EXPR is magic creation of an
462 aggregate; there's no additional work to be done. */
182609b5 463 return force_rvalue (init);
5062dbd5 464
357d956e 465 return force_target_expr (type, init);
a6f86b51 466}
aa36c081 467
a6f86b51
JM
468/* Like the above function, but without the checking. This function should
469 only be used by code which is deliberately trying to subvert the type
470 system, such as call_builtin_trap. */
471
472tree
473force_target_expr (tree type, tree init)
474{
59445d74
RH
475 tree slot;
476
50bc768d 477 gcc_assert (!VOID_TYPE_P (type));
59445d74
RH
478
479 slot = build_local_temp (type);
a6f86b51 480 return build_target_expr (slot, init);
aa36c081
JM
481}
482
c506ca22
MM
483/* Like build_target_expr_with_type, but use the type of INIT. */
484
485tree
b57b79f7 486get_target_expr (tree init)
c506ca22
MM
487{
488 return build_target_expr_with_type (init, TREE_TYPE (init));
489}
490
e1039697
MM
491/* If EXPR is a bitfield reference, convert it to the declared type of
492 the bitfield, and return the resulting expression. Otherwise,
493 return EXPR itself. */
494
495tree
496convert_bitfield_to_declared_type (tree expr)
497{
498 tree bitfield_type;
499
500 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
501 if (bitfield_type)
41990f96
MM
502 expr = convert_to_integer (TYPE_MAIN_VARIANT (bitfield_type),
503 expr);
e1039697
MM
504 return expr;
505}
506
5cc53d4e
MM
507/* EXPR is being used in an rvalue context. Return a version of EXPR
508 that is marked as an rvalue. */
509
510tree
511rvalue (tree expr)
512{
41990f96
MM
513 tree type;
514
515 if (error_operand_p (expr))
516 return expr;
517
518 /* [basic.lval]
519
520 Non-class rvalues always have cv-unqualified types. */
521 type = TREE_TYPE (expr);
522 if (!CLASS_TYPE_P (type) && cp_type_quals (type))
523 type = TYPE_MAIN_VARIANT (type);
524
525 if (!processing_template_decl && real_lvalue_p (expr))
526 expr = build1 (NON_LVALUE_EXPR, type, expr);
527 else if (type != TREE_TYPE (expr))
528 expr = build_nop (type, expr);
529
5cc53d4e
MM
530 return expr;
531}
532
8d08fdba 533\f
06d40de8
DG
534/* Hash an ARRAY_TYPE. K is really of type `tree'. */
535
536static hashval_t
537cplus_array_hash (const void* k)
538{
539 hashval_t hash;
741ac903 540 const_tree const t = (const_tree) k;
06d40de8 541
eb9c434c
JJ
542 hash = TYPE_UID (TREE_TYPE (t));
543 if (TYPE_DOMAIN (t))
544 hash ^= TYPE_UID (TYPE_DOMAIN (t));
06d40de8
DG
545 return hash;
546}
547
548typedef struct cplus_array_info {
549 tree type;
550 tree domain;
551} cplus_array_info;
552
553/* Compare two ARRAY_TYPEs. K1 is really of type `tree', K2 is really
554 of type `cplus_array_info*'. */
555
556static int
557cplus_array_compare (const void * k1, const void * k2)
558{
741ac903
KG
559 const_tree const t1 = (const_tree) k1;
560 const cplus_array_info *const t2 = (const cplus_array_info*) k2;
06d40de8 561
714f2304 562 return (TREE_TYPE (t1) == t2->type && TYPE_DOMAIN (t1) == t2->domain);
06d40de8
DG
563}
564
714f2304
DG
565/* Hash table containing all of the C++ array types, including
566 dependent array types and array types whose element type is
567 cv-qualified. */
06d40de8
DG
568static GTY ((param_is (union tree_node))) htab_t cplus_array_htab;
569
570
bd6dd845 571static tree
b57b79f7 572build_cplus_array_type_1 (tree elt_type, tree index_type)
8d08fdba 573{
8d08fdba
MS
574 tree t;
575
adecb3f4
MM
576 if (elt_type == error_mark_node || index_type == error_mark_node)
577 return error_mark_node;
578
6da06848
JJ
579 if (processing_template_decl
580 && (dependent_type_p (elt_type)
581 || (index_type && !TREE_CONSTANT (TYPE_MAX_VALUE (index_type)))))
5566b478 582 {
06d40de8
DG
583 void **e;
584 cplus_array_info cai;
585 hashval_t hash;
714f2304 586
06d40de8
DG
587 if (cplus_array_htab == NULL)
588 cplus_array_htab = htab_create_ggc (61, &cplus_array_hash,
589 &cplus_array_compare, NULL);
590
eb9c434c
JJ
591 hash = TYPE_UID (elt_type);
592 if (index_type)
593 hash ^= TYPE_UID (index_type);
06d40de8
DG
594 cai.type = elt_type;
595 cai.domain = index_type;
596
597 e = htab_find_slot_with_hash (cplus_array_htab, &cai, hash, INSERT);
598 if (*e)
714f2304 599 /* We have found the type: we're done. */
06d40de8
DG
600 return (tree) *e;
601 else
602 {
714f2304 603 /* Build a new array type. */
06d40de8
DG
604 t = make_node (ARRAY_TYPE);
605 TREE_TYPE (t) = elt_type;
606 TYPE_DOMAIN (t) = index_type;
607
714f2304
DG
608 /* Store it in the hash table. */
609 *e = t;
610
611 /* Set the canonical type for this new node. */
06d40de8
DG
612 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
613 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
614 SET_TYPE_STRUCTURAL_EQUALITY (t);
615 else if (TYPE_CANONICAL (elt_type) != elt_type
616 || (index_type
617 && TYPE_CANONICAL (index_type) != index_type))
714f2304
DG
618 TYPE_CANONICAL (t)
619 = build_cplus_array_type
620 (TYPE_CANONICAL (elt_type),
6da06848 621 index_type ? TYPE_CANONICAL (index_type) : index_type);
714f2304
DG
622 else
623 TYPE_CANONICAL (t) = t;
06d40de8 624 }
5566b478
MS
625 }
626 else
80661759 627 t = build_array_type (elt_type, index_type);
8d08fdba
MS
628
629 /* Push these needs up so that initialization takes place
630 more easily. */
9f63daea 631 TYPE_NEEDS_CONSTRUCTING (t)
db3626d1 632 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (elt_type));
9f63daea 633 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
834c6dff 634 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (elt_type));
8d08fdba
MS
635 return t;
636}
e349ee73
MS
637
638tree
b57b79f7 639build_cplus_array_type (tree elt_type, tree index_type)
e349ee73
MS
640{
641 tree t;
89d684bb 642 int type_quals = cp_type_quals (elt_type);
91063b51 643
4b011bbf
JM
644 if (type_quals != TYPE_UNQUALIFIED)
645 elt_type = cp_build_qualified_type (elt_type, TYPE_UNQUALIFIED);
e349ee73
MS
646
647 t = build_cplus_array_type_1 (elt_type, index_type);
648
4b011bbf
JM
649 if (type_quals != TYPE_UNQUALIFIED)
650 t = cp_build_qualified_type (t, type_quals);
e349ee73
MS
651
652 return t;
653}
8af2fec4 654
09357846
JM
655/* Return an ARRAY_TYPE with element type ELT and length N. */
656
657tree
658build_array_of_n_type (tree elt, int n)
659{
660 return build_cplus_array_type (elt, build_index_type (size_int (n - 1)));
661}
662
8af2fec4
RY
663/* Return a reference type node referring to TO_TYPE. If RVAL is
664 true, return an rvalue reference type, otherwise return an lvalue
665 reference type. If a type node exists, reuse it, otherwise create
666 a new one. */
667tree
668cp_build_reference_type (tree to_type, bool rval)
669{
670 tree lvalue_ref, t;
671 lvalue_ref = build_reference_type (to_type);
672 if (!rval)
673 return lvalue_ref;
674
675 /* This code to create rvalue reference types is based on and tied
676 to the code creating lvalue reference types in the middle-end
677 functions build_reference_type_for_mode and build_reference_type.
678
679 It works by putting the rvalue reference type nodes after the
680 lvalue reference nodes in the TYPE_NEXT_REF_TO linked list, so
681 they will effectively be ignored by the middle end. */
682
683 for (t = lvalue_ref; (t = TYPE_NEXT_REF_TO (t)); )
684 if (TYPE_REF_IS_RVALUE (t))
685 return t;
686
687 t = copy_node (lvalue_ref);
688
689 TYPE_REF_IS_RVALUE (t) = true;
690 TYPE_NEXT_REF_TO (t) = TYPE_NEXT_REF_TO (lvalue_ref);
691 TYPE_NEXT_REF_TO (lvalue_ref) = t;
692 TYPE_MAIN_VARIANT (t) = t;
693
694 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
695 SET_TYPE_STRUCTURAL_EQUALITY (t);
696 else if (TYPE_CANONICAL (to_type) != to_type)
697 TYPE_CANONICAL (t)
698 = cp_build_reference_type (TYPE_CANONICAL (to_type), rval);
699 else
700 TYPE_CANONICAL (t) = t;
701
702 layout_type (t);
703
704 return t;
705
706}
707
9ae165a0
DG
708/* Used by the C++ front end to build qualified array types. However,
709 the C version of this function does not properly maintain canonical
710 types (which are not used in C). */
711tree
712c_build_qualified_type (tree type, int type_quals)
713{
714 return cp_build_qualified_type (type, type_quals);
715}
8af2fec4 716
8d08fdba 717\f
adecb3f4
MM
718/* Make a variant of TYPE, qualified with the TYPE_QUALS. Handles
719 arrays correctly. In particular, if TYPE is an array of T's, and
c2ea3a40 720 TYPE_QUALS is non-empty, returns an array of qualified T's.
9f63daea 721
39a13be5 722 FLAGS determines how to deal with ill-formed qualifications. If
4f2b0fb2
NS
723 tf_ignore_bad_quals is set, then bad qualifications are dropped
724 (this is permitted if TYPE was introduced via a typedef or template
725 type parameter). If bad qualifications are dropped and tf_warning
726 is set, then a warning is issued for non-const qualifications. If
727 tf_ignore_bad_quals is not set and tf_error is not set, we
728 return error_mark_node. Otherwise, we issue an error, and ignore
729 the qualifications.
730
731 Qualification of a reference type is valid when the reference came
732 via a typedef or template type argument. [dcl.ref] No such
733 dispensation is provided for qualifying a function type. [dcl.fct]
734 DR 295 queries this and the proposed resolution brings it into line
34cd5ae7 735 with qualifying a reference. We implement the DR. We also behave
4f2b0fb2 736 in a similar manner for restricting non-pointer types. */
9f63daea 737
f376e137 738tree
9f63daea 739cp_build_qualified_type_real (tree type,
0cbd7506
MS
740 int type_quals,
741 tsubst_flags_t complain)
f376e137 742{
2adeacc9 743 tree result;
4f2b0fb2 744 int bad_quals = TYPE_UNQUALIFIED;
2adeacc9 745
e76a2646
MS
746 if (type == error_mark_node)
747 return type;
e271912d 748
89d684bb 749 if (type_quals == cp_type_quals (type))
e271912d
JM
750 return type;
751
4f2b0fb2 752 if (TREE_CODE (type) == ARRAY_TYPE)
f376e137 753 {
db3626d1
MM
754 /* In C++, the qualification really applies to the array element
755 type. Obtain the appropriately qualified element type. */
756 tree t;
9f63daea
EC
757 tree element_type
758 = cp_build_qualified_type_real (TREE_TYPE (type),
db3626d1
MM
759 type_quals,
760 complain);
761
762 if (element_type == error_mark_node)
adecb3f4 763 return error_mark_node;
f376e137 764
29fae15c
MM
765 /* See if we already have an identically qualified type. */
766 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
9f63daea 767 if (cp_type_quals (t) == type_quals
29fae15c
MM
768 && TYPE_NAME (t) == TYPE_NAME (type)
769 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type))
770 break;
9f63daea 771
29fae15c 772 if (!t)
37d24376
DG
773 {
774 t = build_cplus_array_type_1 (element_type, TYPE_DOMAIN (type));
775
776 if (TYPE_MAIN_VARIANT (t) != TYPE_MAIN_VARIANT (type))
777 {
778 /* Set the main variant of the newly-created ARRAY_TYPE
779 (with cv-qualified element type) to the main variant of
780 the unqualified ARRAY_TYPE we started with. */
781 tree last_variant = t;
782 tree m = TYPE_MAIN_VARIANT (type);
783
784 /* Find the last variant on the new ARRAY_TYPEs list of
785 variants, setting the main variant of each of the other
786 types to the main variant of our unqualified
787 ARRAY_TYPE. */
788 while (TYPE_NEXT_VARIANT (last_variant))
789 {
790 TYPE_MAIN_VARIANT (last_variant) = m;
791 last_variant = TYPE_NEXT_VARIANT (last_variant);
792 }
793
794 /* Splice in the newly-created variants. */
795 TYPE_NEXT_VARIANT (last_variant) = TYPE_NEXT_VARIANT (m);
796 TYPE_NEXT_VARIANT (m) = t;
797 TYPE_MAIN_VARIANT (last_variant) = m;
798 }
799 }
f376e137 800
db3626d1 801 /* Even if we already had this variant, we update
834c6dff 802 TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case
9f63daea
EC
803 they changed since the variant was originally created.
804
db3626d1
MM
805 This seems hokey; if there is some way to use a previous
806 variant *without* coming through here,
807 TYPE_NEEDS_CONSTRUCTING will never be updated. */
9f63daea 808 TYPE_NEEDS_CONSTRUCTING (t)
db3626d1 809 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
9f63daea 810 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
834c6dff 811 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
db3626d1 812 return t;
f376e137 813 }
2adeacc9
MM
814 else if (TYPE_PTRMEMFUNC_P (type))
815 {
816 /* For a pointer-to-member type, we can't just return a
817 cv-qualified version of the RECORD_TYPE. If we do, we
4f2b0fb2 818 haven't changed the field that contains the actual pointer to
2adeacc9
MM
819 a method, and so TYPE_PTRMEMFUNC_FN_TYPE will be wrong. */
820 tree t;
821
822 t = TYPE_PTRMEMFUNC_FN_TYPE (type);
823 t = cp_build_qualified_type_real (t, type_quals, complain);
46cbda4a 824 return build_ptrmemfunc_type (t);
2adeacc9 825 }
9a3c2683
JJ
826 else if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
827 {
828 tree t = PACK_EXPANSION_PATTERN (type);
829
830 t = cp_build_qualified_type_real (t, type_quals, complain);
831 return make_pack_expansion (t);
832 }
9f63daea 833
39a13be5
RW
834 /* A reference or method type shall not be cv-qualified.
835 [dcl.ref], [dcl.fct] */
4b011bbf
JM
836 if (type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)
837 && (TREE_CODE (type) == REFERENCE_TYPE
4b011bbf
JM
838 || TREE_CODE (type) == METHOD_TYPE))
839 {
840 bad_quals |= type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
4b011bbf
JM
841 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
842 }
9f63daea 843
4b011bbf 844 /* A restrict-qualified type must be a pointer (or reference)
9804209d 845 to object or incomplete type, or a function type. */
4b011bbf
JM
846 if ((type_quals & TYPE_QUAL_RESTRICT)
847 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
848 && TREE_CODE (type) != TYPENAME_TYPE
9804209d 849 && TREE_CODE (type) != FUNCTION_TYPE
4b011bbf
JM
850 && !POINTER_TYPE_P (type))
851 {
852 bad_quals |= TYPE_QUAL_RESTRICT;
853 type_quals &= ~TYPE_QUAL_RESTRICT;
854 }
855
856 if (bad_quals == TYPE_UNQUALIFIED)
857 /*OK*/;
858 else if (!(complain & (tf_error | tf_ignore_bad_quals)))
859 return error_mark_node;
4b011bbf
JM
860 else
861 {
862 if (complain & tf_ignore_bad_quals)
0cbd7506
MS
863 /* We're not going to warn about constifying things that can't
864 be constified. */
865 bad_quals &= ~TYPE_QUAL_CONST;
4b011bbf 866 if (bad_quals)
0cbd7506
MS
867 {
868 tree bad_type = build_qualified_type (ptr_type_node, bad_quals);
9f63daea 869
0cbd7506
MS
870 if (!(complain & tf_ignore_bad_quals))
871 error ("%qV qualifiers cannot be applied to %qT",
4b011bbf 872 bad_type, type);
0cbd7506 873 }
4b011bbf 874 }
9f63daea 875
2adeacc9
MM
876 /* Retrieve (or create) the appropriately qualified variant. */
877 result = build_qualified_type (type, type_quals);
878
879 /* If this was a pointer-to-method type, and we just made a copy,
3cfab7ec
GK
880 then we need to unshare the record that holds the cached
881 pointer-to-member-function type, because these will be distinct
882 between the unqualified and qualified types. */
9f63daea 883 if (result != type
2adeacc9 884 && TREE_CODE (type) == POINTER_TYPE
0f67bdf1
JM
885 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE
886 && TYPE_LANG_SPECIFIC (result) == TYPE_LANG_SPECIFIC (type))
3cfab7ec 887 TYPE_LANG_SPECIFIC (result) = NULL;
2adeacc9 888
7aa4a1df
DG
889 /* We may also have ended up building a new copy of the canonical
890 type of a pointer-to-method type, which could have the same
891 sharing problem described above. */
892 if (TYPE_CANONICAL (result) != TYPE_CANONICAL (type)
893 && TREE_CODE (type) == POINTER_TYPE
894 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE
895 && (TYPE_LANG_SPECIFIC (TYPE_CANONICAL (result))
896 == TYPE_LANG_SPECIFIC (TYPE_CANONICAL (type))))
897 TYPE_LANG_SPECIFIC (TYPE_CANONICAL (result)) = NULL;
898
899
2adeacc9 900 return result;
f376e137 901}
53929c47
JM
902
903/* Returns the canonical version of TYPE. In other words, if TYPE is
904 a typedef, returns the underlying type. The cv-qualification of
905 the type returned matches the type input; they will always be
906 compatible types. */
907
908tree
b57b79f7 909canonical_type_variant (tree t)
53929c47 910{
1ad8aeeb
DG
911 if (t == error_mark_node)
912 return error_mark_node;
913
89d684bb 914 return cp_build_qualified_type (TYPE_MAIN_VARIANT (t), cp_type_quals (t));
53929c47 915}
f376e137 916\f
48b45647
NS
917/* Makes a copy of BINFO and TYPE, which is to be inherited into a
918 graph dominated by T. If BINFO is NULL, TYPE is a dependent base,
919 and we do a shallow copy. If BINFO is non-NULL, we do a deep copy.
920 VIRT indicates whether TYPE is inherited virtually or not.
921 IGO_PREV points at the previous binfo of the inheritance graph
922 order chain. The newly copied binfo's TREE_CHAIN forms this
923 ordering.
924
925 The CLASSTYPE_VBASECLASSES vector of T is constructed in the
926 correct order. That is in the order the bases themselves should be
927 constructed in.
dbbf88d1
NS
928
929 The BINFO_INHERITANCE of a virtual base class points to the binfo
48b45647
NS
930 of the most derived type. ??? We could probably change this so that
931 BINFO_INHERITANCE becomes synonymous with BINFO_PRIMARY, and hence
932 remove a field. They currently can only differ for primary virtual
933 virtual bases. */
dbbf88d1
NS
934
935tree
48b45647 936copy_binfo (tree binfo, tree type, tree t, tree *igo_prev, int virt)
9a71c18b 937{
48b45647 938 tree new_binfo;
9a71c18b 939
48b45647
NS
940 if (virt)
941 {
942 /* See if we've already made this virtual base. */
943 new_binfo = binfo_for_vbase (type, t);
944 if (new_binfo)
945 return new_binfo;
946 }
9f63daea 947
fa743e8c 948 new_binfo = make_tree_binfo (binfo ? BINFO_N_BASE_BINFOS (binfo) : 0);
48b45647 949 BINFO_TYPE (new_binfo) = type;
9a71c18b 950
48b45647
NS
951 /* Chain it into the inheritance graph. */
952 TREE_CHAIN (*igo_prev) = new_binfo;
953 *igo_prev = new_binfo;
9f63daea 954
48b45647 955 if (binfo)
dfbcd65a 956 {
fa743e8c
NS
957 int ix;
958 tree base_binfo;
9f63daea 959
50bc768d 960 gcc_assert (!BINFO_DEPENDENT_BASE_P (binfo));
539ed333 961 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), type));
9f63daea 962
48b45647
NS
963 BINFO_OFFSET (new_binfo) = BINFO_OFFSET (binfo);
964 BINFO_VIRTUALS (new_binfo) = BINFO_VIRTUALS (binfo);
9f63daea 965
fa743e8c
NS
966 /* We do not need to copy the accesses, as they are read only. */
967 BINFO_BASE_ACCESSES (new_binfo) = BINFO_BASE_ACCESSES (binfo);
9f63daea 968
48b45647 969 /* Recursively copy base binfos of BINFO. */
fa743e8c 970 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
dbbf88d1 971 {
48b45647 972 tree new_base_binfo;
9f63daea 973
50bc768d 974 gcc_assert (!BINFO_DEPENDENT_BASE_P (base_binfo));
48b45647
NS
975 new_base_binfo = copy_binfo (base_binfo, BINFO_TYPE (base_binfo),
976 t, igo_prev,
977 BINFO_VIRTUAL_P (base_binfo));
9f63daea 978
48b45647
NS
979 if (!BINFO_INHERITANCE_CHAIN (new_base_binfo))
980 BINFO_INHERITANCE_CHAIN (new_base_binfo) = new_binfo;
fa743e8c 981 BINFO_BASE_APPEND (new_binfo, new_base_binfo);
dbbf88d1 982 }
9a71c18b 983 }
48b45647
NS
984 else
985 BINFO_DEPENDENT_BASE_P (new_binfo) = 1;
9f63daea 986
48b45647
NS
987 if (virt)
988 {
989 /* Push it onto the list after any virtual bases it contains
990 will have been pushed. */
991 VEC_quick_push (tree, CLASSTYPE_VBASECLASSES (t), new_binfo);
992 BINFO_VIRTUAL_P (new_binfo) = 1;
993 BINFO_INHERITANCE_CHAIN (new_binfo) = TYPE_BINFO (t);
994 }
9f63daea 995
48b45647 996 return new_binfo;
9a71c18b 997}
8d08fdba
MS
998\f
999/* Hashing of lists so that we don't make duplicates.
1000 The entry point is `list_hash_canon'. */
1001
8d08fdba
MS
1002/* Now here is the hash table. When recording a list, it is added
1003 to the slot whose index is the hash code mod the table size.
1004 Note that the hash table is used for several kinds of lists.
1005 While all these live in the same table, they are completely independent,
1006 and the hash code is computed differently for each of these. */
1007
e2500fed 1008static GTY ((param_is (union tree_node))) htab_t list_hash_table;
9ccb25d5 1009
9f63daea 1010struct list_proxy
9ccb25d5
MM
1011{
1012 tree purpose;
1013 tree value;
1014 tree chain;
1015};
1016
1017/* Compare ENTRY (an entry in the hash table) with DATA (a list_proxy
1018 for a node we are thinking about adding). */
1019
1020static int
b57b79f7 1021list_hash_eq (const void* entry, const void* data)
9ccb25d5 1022{
741ac903
KG
1023 const_tree const t = (const_tree) entry;
1024 const struct list_proxy *const proxy = (const struct list_proxy *) data;
9ccb25d5
MM
1025
1026 return (TREE_VALUE (t) == proxy->value
1027 && TREE_PURPOSE (t) == proxy->purpose
1028 && TREE_CHAIN (t) == proxy->chain);
1029}
8d08fdba
MS
1030
1031/* Compute a hash code for a list (chain of TREE_LIST nodes
1032 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
1033 TREE_COMMON slots), by adding the hash codes of the individual entries. */
1034
9ccb25d5 1035static hashval_t
b57b79f7 1036list_hash_pieces (tree purpose, tree value, tree chain)
8d08fdba 1037{
9ccb25d5 1038 hashval_t hashcode = 0;
9f63daea 1039
37c46b43 1040 if (chain)
fd917e0d 1041 hashcode += TREE_HASH (chain);
9f63daea 1042
37c46b43 1043 if (value)
fd917e0d 1044 hashcode += TREE_HASH (value);
8d08fdba
MS
1045 else
1046 hashcode += 1007;
37c46b43 1047 if (purpose)
fd917e0d 1048 hashcode += TREE_HASH (purpose);
8d08fdba
MS
1049 else
1050 hashcode += 1009;
1051 return hashcode;
1052}
1053
9ccb25d5 1054/* Hash an already existing TREE_LIST. */
8d08fdba 1055
9ccb25d5 1056static hashval_t
b57b79f7 1057list_hash (const void* p)
8d08fdba 1058{
741ac903 1059 const_tree const t = (const_tree) p;
9f63daea
EC
1060 return list_hash_pieces (TREE_PURPOSE (t),
1061 TREE_VALUE (t),
9ccb25d5 1062 TREE_CHAIN (t));
8d08fdba
MS
1063}
1064
51632249
JM
1065/* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
1066 object for an identical list if one already exists. Otherwise, build a
1067 new one, and record it as the canonical object. */
8d08fdba 1068
8d08fdba 1069tree
b57b79f7 1070hash_tree_cons (tree purpose, tree value, tree chain)
8d08fdba 1071{
a703fb38 1072 int hashcode = 0;
fad205ff 1073 void **slot;
9ccb25d5
MM
1074 struct list_proxy proxy;
1075
1076 /* Hash the list node. */
1077 hashcode = list_hash_pieces (purpose, value, chain);
1078 /* Create a proxy for the TREE_LIST we would like to create. We
1079 don't actually create it so as to avoid creating garbage. */
1080 proxy.purpose = purpose;
1081 proxy.value = value;
1082 proxy.chain = chain;
1083 /* See if it is already in the table. */
1084 slot = htab_find_slot_with_hash (list_hash_table, &proxy, hashcode,
1085 INSERT);
1086 /* If not, create a new node. */
1087 if (!*slot)
fad205ff 1088 *slot = tree_cons (purpose, value, chain);
67f5655f 1089 return (tree) *slot;
8d08fdba
MS
1090}
1091
1092/* Constructor for hashed lists. */
e92cc029 1093
8d08fdba 1094tree
b57b79f7 1095hash_tree_chain (tree value, tree chain)
8d08fdba 1096{
51632249 1097 return hash_tree_cons (NULL_TREE, value, chain);
8d08fdba 1098}
8d08fdba 1099\f
8d08fdba 1100void
b57b79f7 1101debug_binfo (tree elem)
8d08fdba 1102{
fed3cef0 1103 HOST_WIDE_INT n;
8d08fdba
MS
1104 tree virtuals;
1105
90ff44cf
KG
1106 fprintf (stderr, "type \"%s\", offset = " HOST_WIDE_INT_PRINT_DEC
1107 "\nvtable type:\n",
1108 TYPE_NAME_STRING (BINFO_TYPE (elem)),
fed3cef0 1109 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
8d08fdba
MS
1110 debug_tree (BINFO_TYPE (elem));
1111 if (BINFO_VTABLE (elem))
fed3cef0 1112 fprintf (stderr, "vtable decl \"%s\"\n",
c35cce41 1113 IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem))));
8d08fdba
MS
1114 else
1115 fprintf (stderr, "no vtable decl yet\n");
1116 fprintf (stderr, "virtuals:\n");
da3d4dfa 1117 virtuals = BINFO_VIRTUALS (elem);
1f84ec23 1118 n = 0;
f30432d7 1119
8d08fdba
MS
1120 while (virtuals)
1121 {
83f2ccf4 1122 tree fndecl = TREE_VALUE (virtuals);
71e89f27 1123 fprintf (stderr, "%s [%ld =? %ld]\n",
8d08fdba 1124 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
71e89f27 1125 (long) n, (long) TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
f30432d7 1126 ++n;
8d08fdba 1127 virtuals = TREE_CHAIN (virtuals);
8d08fdba
MS
1128 }
1129}
1130
02ed62dd
MM
1131/* Build a representation for the qualified name SCOPE::NAME. TYPE is
1132 the type of the result expression, if known, or NULL_TREE if the
1133 resulting expression is type-dependent. If TEMPLATE_P is true,
1134 NAME is known to be a template because the user explicitly used the
3db45ab5 1135 "template" keyword after the "::".
02ed62dd
MM
1136
1137 All SCOPE_REFs should be built by use of this function. */
1138
1139tree
1140build_qualified_name (tree type, tree scope, tree name, bool template_p)
1141{
1142 tree t;
36569397
MM
1143 if (type == error_mark_node
1144 || scope == error_mark_node
1145 || name == error_mark_node)
1146 return error_mark_node;
02ed62dd
MM
1147 t = build2 (SCOPE_REF, type, scope, name);
1148 QUALIFIED_NAME_IS_TEMPLATE (t) = template_p;
1149 return t;
1150}
1151
3b426391 1152/* Returns nonzero if X is an expression for a (possibly overloaded)
eff3a276
MM
1153 function. If "f" is a function or function template, "f", "c->f",
1154 "c.f", "C::f", and "f<int>" will all be considered possibly
1155 overloaded functions. Returns 2 if the function is actually
b9704fc5 1156 overloaded, i.e., if it is impossible to know the type of the
eff3a276
MM
1157 function without performing overload resolution. */
1158
8d08fdba 1159int
b57b79f7 1160is_overloaded_fn (tree x)
8d08fdba 1161{
4bb0968f 1162 /* A baselink is also considered an overloaded function. */
ccbe00a4
JM
1163 if (TREE_CODE (x) == OFFSET_REF
1164 || TREE_CODE (x) == COMPONENT_REF)
05e0b2f4 1165 x = TREE_OPERAND (x, 1);
4bb0968f 1166 if (BASELINK_P (x))
da15dae6 1167 x = BASELINK_FUNCTIONS (x);
eff3a276
MM
1168 if (TREE_CODE (x) == TEMPLATE_ID_EXPR
1169 || DECL_FUNCTION_TEMPLATE_P (OVL_CURRENT (x))
1170 || (TREE_CODE (x) == OVERLOAD && OVL_CHAIN (x)))
1171 return 2;
1172 return (TREE_CODE (x) == FUNCTION_DECL
1173 || TREE_CODE (x) == OVERLOAD);
8d08fdba
MS
1174}
1175
eff3a276
MM
1176/* Returns true iff X is an expression for an overloaded function
1177 whose type cannot be known without performing overload
1178 resolution. */
1179
1180bool
b57b79f7 1181really_overloaded_fn (tree x)
9f63daea 1182{
eff3a276 1183 return is_overloaded_fn (x) == 2;
8926095f
MS
1184}
1185
8d08fdba 1186tree
b57b79f7 1187get_first_fn (tree from)
8d08fdba 1188{
50bc768d 1189 gcc_assert (is_overloaded_fn (from));
c6002625 1190 /* A baselink is also considered an overloaded function. */
ccbe00a4
JM
1191 if (TREE_CODE (from) == COMPONENT_REF)
1192 from = TREE_OPERAND (from, 1);
4bb0968f 1193 if (BASELINK_P (from))
da15dae6 1194 from = BASELINK_FUNCTIONS (from);
2c73f9f5
ML
1195 return OVL_CURRENT (from);
1196}
8d08fdba 1197
c6002625 1198/* Return a new OVL node, concatenating it with the old one. */
2c73f9f5
ML
1199
1200tree
b57b79f7 1201ovl_cons (tree decl, tree chain)
2c73f9f5
ML
1202{
1203 tree result = make_node (OVERLOAD);
1204 TREE_TYPE (result) = unknown_type_node;
1205 OVL_FUNCTION (result) = decl;
1206 TREE_CHAIN (result) = chain;
9f63daea 1207
2c73f9f5
ML
1208 return result;
1209}
1210
2c73f9f5
ML
1211/* Build a new overloaded function. If this is the first one,
1212 just return it; otherwise, ovl_cons the _DECLs */
1213
1214tree
b57b79f7 1215build_overload (tree decl, tree chain)
2c73f9f5 1216{
161c12b0 1217 if (! chain && TREE_CODE (decl) != TEMPLATE_DECL)
2c73f9f5 1218 return decl;
161c12b0 1219 if (chain && TREE_CODE (chain) != OVERLOAD)
2c73f9f5
ML
1220 chain = ovl_cons (chain, NULL_TREE);
1221 return ovl_cons (decl, chain);
1222}
1223
8d08fdba
MS
1224\f
1225#define PRINT_RING_SIZE 4
1226
e1def31b 1227const char *
b57b79f7 1228cxx_printable_name (tree decl, int v)
8d08fdba 1229{
1bde0042 1230 static unsigned int uid_ring[PRINT_RING_SIZE];
8d08fdba
MS
1231 static char *print_ring[PRINT_RING_SIZE];
1232 static int ring_counter;
1233 int i;
1234
1235 /* Only cache functions. */
2ba25f50
MS
1236 if (v < 2
1237 || TREE_CODE (decl) != FUNCTION_DECL
8d08fdba 1238 || DECL_LANG_SPECIFIC (decl) == 0)
2ba25f50 1239 return lang_decl_name (decl, v);
8d08fdba
MS
1240
1241 /* See if this print name is lying around. */
1242 for (i = 0; i < PRINT_RING_SIZE; i++)
1bde0042 1243 if (uid_ring[i] == DECL_UID (decl))
8d08fdba
MS
1244 /* yes, so return it. */
1245 return print_ring[i];
1246
1247 if (++ring_counter == PRINT_RING_SIZE)
1248 ring_counter = 0;
1249
1250 if (current_function_decl != NULL_TREE)
1251 {
1bde0042 1252 if (uid_ring[ring_counter] == DECL_UID (current_function_decl))
8d08fdba
MS
1253 ring_counter += 1;
1254 if (ring_counter == PRINT_RING_SIZE)
1255 ring_counter = 0;
1bde0042 1256 gcc_assert (uid_ring[ring_counter] != DECL_UID (current_function_decl));
8d08fdba
MS
1257 }
1258
1259 if (print_ring[ring_counter])
1260 free (print_ring[ring_counter]);
1261
2ba25f50 1262 print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v));
1bde0042 1263 uid_ring[ring_counter] = DECL_UID (decl);
8d08fdba
MS
1264 return print_ring[ring_counter];
1265}
1266\f
f30432d7 1267/* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
8d08fdba 1268 listed in RAISES. */
e92cc029 1269
8d08fdba 1270tree
b57b79f7 1271build_exception_variant (tree type, tree raises)
8d08fdba 1272{
8d08fdba 1273 tree v = TYPE_MAIN_VARIANT (type);
91063b51 1274 int type_quals = TYPE_QUALS (type);
8d08fdba 1275
45537677 1276 for (; v; v = TYPE_NEXT_VARIANT (v))
896c3aa3 1277 if (check_qualified_type (v, type, type_quals)
0cbd7506 1278 && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (v), 1))
4cc1d462 1279 return v;
8d08fdba
MS
1280
1281 /* Need to build a new variant. */
8dd16ecc 1282 v = build_variant_type_copy (type);
8d08fdba
MS
1283 TYPE_RAISES_EXCEPTIONS (v) = raises;
1284 return v;
1285}
1286
dac65501
KL
1287/* Given a TEMPLATE_TEMPLATE_PARM node T, create a new
1288 BOUND_TEMPLATE_TEMPLATE_PARM bound with NEWARGS as its template
1899c3a4 1289 arguments. */
73b0fce8
KL
1290
1291tree
b57b79f7 1292bind_template_template_parm (tree t, tree newargs)
73b0fce8 1293{
1899c3a4 1294 tree decl = TYPE_NAME (t);
6b9b6b15
JM
1295 tree t2;
1296
9e1e64ec 1297 t2 = cxx_make_type (BOUND_TEMPLATE_TEMPLATE_PARM);
dac65501 1298 decl = build_decl (TYPE_DECL, DECL_NAME (decl), NULL_TREE);
1899c3a4 1299
dac65501
KL
1300 /* These nodes have to be created to reflect new TYPE_DECL and template
1301 arguments. */
1302 TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t));
1303 TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl;
1304 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
9f63daea 1305 = tree_cons (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t),
dac65501 1306 newargs, NULL_TREE);
6b9b6b15 1307
1899c3a4
KL
1308 TREE_TYPE (decl) = t2;
1309 TYPE_NAME (t2) = decl;
1310 TYPE_STUB_DECL (t2) = decl;
dac65501 1311 TYPE_SIZE (t2) = 0;
06d40de8 1312 SET_TYPE_STRUCTURAL_EQUALITY (t2);
73b0fce8 1313
73b0fce8
KL
1314 return t2;
1315}
1316
bf3428d0 1317/* Called from count_trees via walk_tree. */
297a5329
JM
1318
1319static tree
44de5aeb 1320count_trees_r (tree *tp, int *walk_subtrees, void *data)
297a5329 1321{
44de5aeb
RK
1322 ++*((int *) data);
1323
1324 if (TYPE_P (*tp))
1325 *walk_subtrees = 0;
1326
297a5329
JM
1327 return NULL_TREE;
1328}
1329
1330/* Debugging function for measuring the rough complexity of a tree
1331 representation. */
1332
1333int
b57b79f7 1334count_trees (tree t)
297a5329 1335{
bf3428d0 1336 int n_trees = 0;
14588106 1337 cp_walk_tree_without_duplicates (&t, count_trees_r, &n_trees);
297a5329 1338 return n_trees;
9f63daea 1339}
297a5329 1340
b2244c65
MM
1341/* Called from verify_stmt_tree via walk_tree. */
1342
1343static tree
9f63daea 1344verify_stmt_tree_r (tree* tp,
0cbd7506
MS
1345 int* walk_subtrees ATTRIBUTE_UNUSED ,
1346 void* data)
b2244c65
MM
1347{
1348 tree t = *tp;
1349 htab_t *statements = (htab_t *) data;
1350 void **slot;
1351
009ed910 1352 if (!STATEMENT_CODE_P (TREE_CODE (t)))
b2244c65
MM
1353 return NULL_TREE;
1354
1355 /* If this statement is already present in the hash table, then
1356 there is a circularity in the statement tree. */
315fb5db 1357 gcc_assert (!htab_find (*statements, t));
9f63daea 1358
b2244c65
MM
1359 slot = htab_find_slot (*statements, t, INSERT);
1360 *slot = t;
1361
1362 return NULL_TREE;
1363}
1364
1365/* Debugging function to check that the statement T has not been
1366 corrupted. For now, this function simply checks that T contains no
1367 circularities. */
1368
1369void
b57b79f7 1370verify_stmt_tree (tree t)
b2244c65
MM
1371{
1372 htab_t statements;
1373 statements = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
14588106 1374 cp_walk_tree (&t, verify_stmt_tree_r, &statements, NULL);
b2244c65
MM
1375 htab_delete (statements);
1376}
1377
50a6dbd7 1378/* Check if the type T depends on a type with no linkage and if so, return
4684cd27
MM
1379 it. If RELAXED_P then do not consider a class type declared within
1380 a TREE_PUBLIC function to have no linkage. */
50a6dbd7
JM
1381
1382tree
4684cd27 1383no_linkage_check (tree t, bool relaxed_p)
50a6dbd7 1384{
caf43ca4
MM
1385 tree r;
1386
2adeacc9
MM
1387 /* There's no point in checking linkage on template functions; we
1388 can't know their complete types. */
1389 if (processing_template_decl)
1390 return NULL_TREE;
1391
caf43ca4
MM
1392 switch (TREE_CODE (t))
1393 {
4684cd27
MM
1394 tree fn;
1395
caf43ca4
MM
1396 case RECORD_TYPE:
1397 if (TYPE_PTRMEMFUNC_P (t))
1398 goto ptrmem;
1399 /* Fall through. */
1400 case UNION_TYPE:
1401 if (!CLASS_TYPE_P (t))
1402 return NULL_TREE;
1403 /* Fall through. */
1404 case ENUMERAL_TYPE:
4684cd27
MM
1405 if (TYPE_ANONYMOUS_P (t))
1406 return t;
1407 fn = decl_function_context (TYPE_MAIN_DECL (t));
1408 if (fn && (!relaxed_p || !TREE_PUBLIC (fn)))
caf43ca4
MM
1409 return t;
1410 return NULL_TREE;
1411
1412 case ARRAY_TYPE:
1413 case POINTER_TYPE:
1414 case REFERENCE_TYPE:
4684cd27 1415 return no_linkage_check (TREE_TYPE (t), relaxed_p);
caf43ca4
MM
1416
1417 case OFFSET_TYPE:
1418 ptrmem:
4684cd27
MM
1419 r = no_linkage_check (TYPE_PTRMEM_POINTED_TO_TYPE (t),
1420 relaxed_p);
caf43ca4
MM
1421 if (r)
1422 return r;
4684cd27 1423 return no_linkage_check (TYPE_PTRMEM_CLASS_TYPE (t), relaxed_p);
caf43ca4
MM
1424
1425 case METHOD_TYPE:
4684cd27 1426 r = no_linkage_check (TYPE_METHOD_BASETYPE (t), relaxed_p);
caf43ca4
MM
1427 if (r)
1428 return r;
1429 /* Fall through. */
1430 case FUNCTION_TYPE:
1431 {
1432 tree parm;
9f63daea
EC
1433 for (parm = TYPE_ARG_TYPES (t);
1434 parm && parm != void_list_node;
caf43ca4
MM
1435 parm = TREE_CHAIN (parm))
1436 {
4684cd27 1437 r = no_linkage_check (TREE_VALUE (parm), relaxed_p);
caf43ca4
MM
1438 if (r)
1439 return r;
1440 }
4684cd27 1441 return no_linkage_check (TREE_TYPE (t), relaxed_p);
caf43ca4
MM
1442 }
1443
1444 default:
1445 return NULL_TREE;
1446 }
50a6dbd7
JM
1447}
1448
5566b478
MS
1449#ifdef GATHER_STATISTICS
1450extern int depth_reached;
1451#endif
1452
8d08fdba 1453void
b57b79f7 1454cxx_print_statistics (void)
8d08fdba 1455{
8d08fdba
MS
1456 print_search_statistics ();
1457 print_class_statistics ();
5566b478
MS
1458#ifdef GATHER_STATISTICS
1459 fprintf (stderr, "maximum template instantiation depth reached: %d\n",
1460 depth_reached);
1461#endif
8d08fdba
MS
1462}
1463
e92cc029
MS
1464/* Return, as an INTEGER_CST node, the number of elements for TYPE
1465 (which is an ARRAY_TYPE). This counts only elements of the top
1466 array. */
8d08fdba
MS
1467
1468tree
b57b79f7 1469array_type_nelts_top (tree type)
8d08fdba 1470{
7866705a
SB
1471 return fold_build2 (PLUS_EXPR, sizetype,
1472 array_type_nelts (type),
701e903a 1473 size_one_node);
8d08fdba
MS
1474}
1475
e92cc029
MS
1476/* Return, as an INTEGER_CST node, the number of elements for TYPE
1477 (which is an ARRAY_TYPE). This one is a recursive count of all
1478 ARRAY_TYPEs that are clumped together. */
8d08fdba
MS
1479
1480tree
b57b79f7 1481array_type_nelts_total (tree type)
8d08fdba
MS
1482{
1483 tree sz = array_type_nelts_top (type);
1484 type = TREE_TYPE (type);
1485 while (TREE_CODE (type) == ARRAY_TYPE)
1486 {
1487 tree n = array_type_nelts_top (type);
7866705a 1488 sz = fold_build2 (MULT_EXPR, sizetype, sz, n);
8d08fdba
MS
1489 type = TREE_TYPE (type);
1490 }
1491 return sz;
1492}
878cd289 1493
b3ab27f3
MM
1494/* Called from break_out_target_exprs via mapcar. */
1495
1496static tree
b57b79f7 1497bot_manip (tree* tp, int* walk_subtrees, void* data)
878cd289 1498{
8dfaeb63
MM
1499 splay_tree target_remap = ((splay_tree) data);
1500 tree t = *tp;
1501
4f976745 1502 if (!TYPE_P (t) && TREE_CONSTANT (t))
8dfaeb63 1503 {
495d26d6 1504 /* There can't be any TARGET_EXPRs or their slot variables below
0cbd7506
MS
1505 this point. We used to check !TREE_SIDE_EFFECTS, but then we
1506 failed to copy an ADDR_EXPR of the slot VAR_DECL. */
8dfaeb63
MM
1507 *walk_subtrees = 0;
1508 return NULL_TREE;
1509 }
495d26d6 1510 if (TREE_CODE (t) == TARGET_EXPR)
73aad9b9 1511 {
b3ab27f3
MM
1512 tree u;
1513
02531345 1514 if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
7efc22ea 1515 u = build_cplus_new (TREE_TYPE (t), TREE_OPERAND (t, 1));
9f63daea 1516 else
7efc22ea 1517 u = build_target_expr_with_type (TREE_OPERAND (t, 1), TREE_TYPE (t));
b3ab27f3
MM
1518
1519 /* Map the old variable to the new one. */
9f63daea
EC
1520 splay_tree_insert (target_remap,
1521 (splay_tree_key) TREE_OPERAND (t, 0),
b3ab27f3 1522 (splay_tree_value) TREE_OPERAND (u, 0));
8dfaeb63 1523
7efc22ea
JM
1524 TREE_OPERAND (u, 1) = break_out_target_exprs (TREE_OPERAND (u, 1));
1525
8dfaeb63
MM
1526 /* Replace the old expression with the new version. */
1527 *tp = u;
1528 /* We don't have to go below this point; the recursive call to
1529 break_out_target_exprs will have handled anything below this
1530 point. */
1531 *walk_subtrees = 0;
1532 return NULL_TREE;
73aad9b9 1533 }
73aad9b9 1534
8dfaeb63
MM
1535 /* Make a copy of this node. */
1536 return copy_tree_r (tp, walk_subtrees, NULL);
878cd289 1537}
9f63daea 1538
8dfaeb63
MM
1539/* Replace all remapped VAR_DECLs in T with their new equivalents.
1540 DATA is really a splay-tree mapping old variables to new
1541 variables. */
b3ab27f3
MM
1542
1543static tree
9f63daea 1544bot_replace (tree* t,
0cbd7506
MS
1545 int* walk_subtrees ATTRIBUTE_UNUSED ,
1546 void* data)
b3ab27f3 1547{
8dfaeb63
MM
1548 splay_tree target_remap = ((splay_tree) data);
1549
b3ab27f3
MM
1550 if (TREE_CODE (*t) == VAR_DECL)
1551 {
1552 splay_tree_node n = splay_tree_lookup (target_remap,
1553 (splay_tree_key) *t);
1554 if (n)
1555 *t = (tree) n->value;
1556 }
1557
1558 return NULL_TREE;
1559}
9f63daea 1560
8dfaeb63
MM
1561/* When we parse a default argument expression, we may create
1562 temporary variables via TARGET_EXPRs. When we actually use the
1563 default-argument expression, we make a copy of the expression, but
1564 we must replace the temporaries with appropriate local versions. */
e92cc029 1565
878cd289 1566tree
b57b79f7 1567break_out_target_exprs (tree t)
878cd289 1568{
8dfaeb63
MM
1569 static int target_remap_count;
1570 static splay_tree target_remap;
1571
b3ab27f3 1572 if (!target_remap_count++)
9f63daea
EC
1573 target_remap = splay_tree_new (splay_tree_compare_pointers,
1574 /*splay_tree_delete_key_fn=*/NULL,
b3ab27f3 1575 /*splay_tree_delete_value_fn=*/NULL);
14588106
RG
1576 cp_walk_tree (&t, bot_manip, target_remap, NULL);
1577 cp_walk_tree (&t, bot_replace, target_remap, NULL);
b3ab27f3
MM
1578
1579 if (!--target_remap_count)
1580 {
1581 splay_tree_delete (target_remap);
1582 target_remap = NULL;
1583 }
1584
1585 return t;
878cd289 1586}
f30432d7 1587
8e1daa34
NS
1588/* Similar to `build_nt', but for template definitions of dependent
1589 expressions */
5566b478
MS
1590
1591tree
e34d07f2 1592build_min_nt (enum tree_code code, ...)
5566b478 1593{
926ce8bd
KH
1594 tree t;
1595 int length;
1596 int i;
e34d07f2 1597 va_list p;
5566b478 1598
5039610b
SL
1599 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
1600
e34d07f2 1601 va_start (p, code);
5566b478 1602
5566b478 1603 t = make_node (code);
8d5e6e25 1604 length = TREE_CODE_LENGTH (code);
5566b478
MS
1605
1606 for (i = 0; i < length; i++)
1607 {
1608 tree x = va_arg (p, tree);
2a1e9fdd 1609 TREE_OPERAND (t, i) = x;
5566b478
MS
1610 }
1611
e34d07f2 1612 va_end (p);
5566b478
MS
1613 return t;
1614}
1615
5039610b 1616
8e1daa34 1617/* Similar to `build', but for template definitions. */
5566b478
MS
1618
1619tree
e34d07f2 1620build_min (enum tree_code code, tree tt, ...)
5566b478 1621{
926ce8bd
KH
1622 tree t;
1623 int length;
1624 int i;
e34d07f2 1625 va_list p;
5566b478 1626
5039610b
SL
1627 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
1628
e34d07f2 1629 va_start (p, tt);
5566b478 1630
5566b478 1631 t = make_node (code);
8d5e6e25 1632 length = TREE_CODE_LENGTH (code);
2a1e9fdd 1633 TREE_TYPE (t) = tt;
5566b478
MS
1634
1635 for (i = 0; i < length; i++)
1636 {
1637 tree x = va_arg (p, tree);
2a1e9fdd 1638 TREE_OPERAND (t, i) = x;
4f976745 1639 if (x && !TYPE_P (x) && TREE_SIDE_EFFECTS (x))
8e1daa34 1640 TREE_SIDE_EFFECTS (t) = 1;
5566b478
MS
1641 }
1642
e34d07f2 1643 va_end (p);
5566b478
MS
1644 return t;
1645}
1646
8e1daa34
NS
1647/* Similar to `build', but for template definitions of non-dependent
1648 expressions. NON_DEP is the non-dependent expression that has been
1649 built. */
1650
1651tree
1652build_min_non_dep (enum tree_code code, tree non_dep, ...)
1653{
926ce8bd
KH
1654 tree t;
1655 int length;
1656 int i;
8e1daa34
NS
1657 va_list p;
1658
5039610b
SL
1659 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
1660
8e1daa34
NS
1661 va_start (p, non_dep);
1662
1663 t = make_node (code);
1664 length = TREE_CODE_LENGTH (code);
1665 TREE_TYPE (t) = TREE_TYPE (non_dep);
8e1daa34
NS
1666 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
1667
1668 for (i = 0; i < length; i++)
1669 {
1670 tree x = va_arg (p, tree);
1671 TREE_OPERAND (t, i) = x;
1672 }
1673
1674 if (code == COMPOUND_EXPR && TREE_CODE (non_dep) != COMPOUND_EXPR)
1675 /* This should not be considered a COMPOUND_EXPR, because it
04c06002 1676 resolves to an overload. */
8e1daa34 1677 COMPOUND_EXPR_OVERLOADED (t) = 1;
9f63daea 1678
8e1daa34
NS
1679 va_end (p);
1680 return t;
1681}
1682
5039610b
SL
1683/* Similar to `build_call_list', but for template definitions of non-dependent
1684 expressions. NON_DEP is the non-dependent expression that has been
1685 built. */
1686
1687tree
1688build_min_non_dep_call_list (tree non_dep, tree fn, tree arglist)
1689{
1690 tree t = build_nt_call_list (fn, arglist);
1691 TREE_TYPE (t) = TREE_TYPE (non_dep);
1692 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
1693 return t;
1694}
1695
5566b478 1696tree
b57b79f7 1697get_type_decl (tree t)
5566b478 1698{
5566b478
MS
1699 if (TREE_CODE (t) == TYPE_DECL)
1700 return t;
2f939d94 1701 if (TYPE_P (t))
5566b478 1702 return TYPE_STUB_DECL (t);
315fb5db
NS
1703 gcc_assert (t == error_mark_node);
1704 return t;
5566b478
MS
1705}
1706
700466c2
JM
1707/* Returns the namespace that contains DECL, whether directly or
1708 indirectly. */
1709
1710tree
b57b79f7 1711decl_namespace_context (tree decl)
700466c2
JM
1712{
1713 while (1)
1714 {
1715 if (TREE_CODE (decl) == NAMESPACE_DECL)
1716 return decl;
1717 else if (TYPE_P (decl))
1718 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
1719 else
1720 decl = CP_DECL_CONTEXT (decl);
1721 }
1722}
1723
b9e75696
JM
1724/* Returns true if decl is within an anonymous namespace, however deeply
1725 nested, or false otherwise. */
1726
1727bool
58f9752a 1728decl_anon_ns_mem_p (const_tree decl)
b9e75696
JM
1729{
1730 while (1)
1731 {
653109bd 1732 if (decl == NULL_TREE || decl == error_mark_node)
b9e75696
JM
1733 return false;
1734 if (TREE_CODE (decl) == NAMESPACE_DECL
1735 && DECL_NAME (decl) == NULL_TREE)
1736 return true;
1737 /* Classes and namespaces inside anonymous namespaces have
1738 TREE_PUBLIC == 0, so we can shortcut the search. */
1739 else if (TYPE_P (decl))
1740 return (TREE_PUBLIC (TYPE_NAME (decl)) == 0);
1741 else if (TREE_CODE (decl) == NAMESPACE_DECL)
1742 return (TREE_PUBLIC (decl) == 0);
1743 else
1744 decl = DECL_CONTEXT (decl);
1745 }
1746}
1747
67d743fe 1748/* Return truthvalue of whether T1 is the same tree structure as T2.
c8a209ca 1749 Return 1 if they are the same. Return 0 if they are different. */
67d743fe 1750
c8a209ca 1751bool
b57b79f7 1752cp_tree_equal (tree t1, tree t2)
67d743fe 1753{
926ce8bd 1754 enum tree_code code1, code2;
67d743fe
MS
1755
1756 if (t1 == t2)
c8a209ca
NS
1757 return true;
1758 if (!t1 || !t2)
1759 return false;
1760
1761 for (code1 = TREE_CODE (t1);
1a87cf0c 1762 CONVERT_EXPR_CODE_P (code1)
c8a209ca
NS
1763 || code1 == NON_LVALUE_EXPR;
1764 code1 = TREE_CODE (t1))
1765 t1 = TREE_OPERAND (t1, 0);
1766 for (code2 = TREE_CODE (t2);
1a87cf0c 1767 CONVERT_EXPR_CODE_P (code2)
c8a209ca
NS
1768 || code1 == NON_LVALUE_EXPR;
1769 code2 = TREE_CODE (t2))
1770 t2 = TREE_OPERAND (t2, 0);
1771
1772 /* They might have become equal now. */
1773 if (t1 == t2)
1774 return true;
9f63daea 1775
67d743fe 1776 if (code1 != code2)
c8a209ca 1777 return false;
67d743fe
MS
1778
1779 switch (code1)
1780 {
1781 case INTEGER_CST:
1782 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
1783 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
1784
1785 case REAL_CST:
1786 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
1787
1788 case STRING_CST:
1789 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
da61dec9 1790 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
c8a209ca 1791 TREE_STRING_LENGTH (t1));
67d743fe 1792
d05739f8
JM
1793 case FIXED_CST:
1794 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
1795 TREE_FIXED_CST (t2));
1796
2a2193e0
SM
1797 case COMPLEX_CST:
1798 return cp_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
1799 && cp_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
1800
67d743fe 1801 case CONSTRUCTOR:
7dd4bdf5
MM
1802 /* We need to do this when determining whether or not two
1803 non-type pointer to member function template arguments
1804 are the same. */
3bfdc719 1805 if (!(same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
7dd4bdf5
MM
1806 /* The first operand is RTL. */
1807 && TREE_OPERAND (t1, 0) == TREE_OPERAND (t2, 0)))
c8a209ca 1808 return false;
7dd4bdf5
MM
1809 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
1810
1811 case TREE_LIST:
c8a209ca
NS
1812 if (!cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
1813 return false;
1814 if (!cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
1815 return false;
7dd4bdf5 1816 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
67d743fe
MS
1817
1818 case SAVE_EXPR:
1819 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1820
1821 case CALL_EXPR:
5039610b
SL
1822 {
1823 tree arg1, arg2;
1824 call_expr_arg_iterator iter1, iter2;
1825 if (!cp_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
1826 return false;
1827 for (arg1 = first_call_expr_arg (t1, &iter1),
1828 arg2 = first_call_expr_arg (t2, &iter2);
1829 arg1 && arg2;
1830 arg1 = next_call_expr_arg (&iter1),
1831 arg2 = next_call_expr_arg (&iter2))
1832 if (!cp_tree_equal (arg1, arg2))
1833 return false;
1834 return (arg1 || arg2);
1835 }
67d743fe 1836
c8a209ca
NS
1837 case TARGET_EXPR:
1838 {
1839 tree o1 = TREE_OPERAND (t1, 0);
1840 tree o2 = TREE_OPERAND (t2, 0);
9f63daea 1841
c8a209ca
NS
1842 /* Special case: if either target is an unallocated VAR_DECL,
1843 it means that it's going to be unified with whatever the
1844 TARGET_EXPR is really supposed to initialize, so treat it
1845 as being equivalent to anything. */
1846 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
1847 && !DECL_RTL_SET_P (o1))
1848 /*Nop*/;
1849 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
1850 && !DECL_RTL_SET_P (o2))
1851 /*Nop*/;
1852 else if (!cp_tree_equal (o1, o2))
1853 return false;
9f63daea 1854
c8a209ca
NS
1855 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
1856 }
9f63daea 1857
67d743fe 1858 case WITH_CLEANUP_EXPR:
c8a209ca
NS
1859 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
1860 return false;
6ad7895a 1861 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
67d743fe
MS
1862
1863 case COMPONENT_REF:
c8a209ca
NS
1864 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
1865 return false;
1866 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
67d743fe 1867
67d743fe 1868 case PARM_DECL:
a77f94e2
JM
1869 /* For comparing uses of parameters in late-specified return types
1870 with an out-of-class definition of the function. */
1871 if ((!DECL_CONTEXT (t1) || !DECL_CONTEXT (t2))
1872 && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
1873 && DECL_NAME (t1) == DECL_NAME (t2))
1874 return true;
1875 else
1876 return false;
1877
1878 case VAR_DECL:
67d743fe
MS
1879 case CONST_DECL:
1880 case FUNCTION_DECL:
c8a209ca
NS
1881 case TEMPLATE_DECL:
1882 case IDENTIFIER_NODE:
47c0c7d7 1883 case SSA_NAME:
c8a209ca 1884 return false;
67d743fe 1885
17a27b4f
MM
1886 case BASELINK:
1887 return (BASELINK_BINFO (t1) == BASELINK_BINFO (t2)
1888 && BASELINK_ACCESS_BINFO (t1) == BASELINK_ACCESS_BINFO (t2)
1889 && cp_tree_equal (BASELINK_FUNCTIONS (t1),
1890 BASELINK_FUNCTIONS (t2)));
1891
f84b4be9 1892 case TEMPLATE_PARM_INDEX:
31758337
NS
1893 return (TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
1894 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2)
1895 && same_type_p (TREE_TYPE (TEMPLATE_PARM_DECL (t1)),
1896 TREE_TYPE (TEMPLATE_PARM_DECL (t2))));
67d743fe 1897
bf12d54d
NS
1898 case TEMPLATE_ID_EXPR:
1899 {
1900 unsigned ix;
1901 tree vec1, vec2;
9f63daea 1902
bf12d54d
NS
1903 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
1904 return false;
1905 vec1 = TREE_OPERAND (t1, 1);
1906 vec2 = TREE_OPERAND (t2, 1);
1907
1908 if (!vec1 || !vec2)
1909 return !vec1 && !vec2;
9f63daea 1910
bf12d54d
NS
1911 if (TREE_VEC_LENGTH (vec1) != TREE_VEC_LENGTH (vec2))
1912 return false;
1913
1914 for (ix = TREE_VEC_LENGTH (vec1); ix--;)
1915 if (!cp_tree_equal (TREE_VEC_ELT (vec1, ix),
1916 TREE_VEC_ELT (vec2, ix)))
1917 return false;
9f63daea 1918
bf12d54d
NS
1919 return true;
1920 }
9f63daea 1921
67d743fe 1922 case SIZEOF_EXPR:
abff8e06 1923 case ALIGNOF_EXPR:
c8a209ca
NS
1924 {
1925 tree o1 = TREE_OPERAND (t1, 0);
1926 tree o2 = TREE_OPERAND (t2, 0);
9f63daea 1927
c8a209ca
NS
1928 if (TREE_CODE (o1) != TREE_CODE (o2))
1929 return false;
1930 if (TYPE_P (o1))
1931 return same_type_p (o1, o2);
1932 else
1933 return cp_tree_equal (o1, o2);
1934 }
9f63daea 1935
6f9f76e3
SM
1936 case MODOP_EXPR:
1937 {
1938 tree t1_op1, t2_op1;
1939
1940 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
1941 return false;
1942
1943 t1_op1 = TREE_OPERAND (t1, 1);
1944 t2_op1 = TREE_OPERAND (t2, 1);
1945 if (TREE_CODE (t1_op1) != TREE_CODE (t2_op1))
1946 return false;
1947
1948 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t2, 2));
1949 }
1950
61a127b3
MM
1951 case PTRMEM_CST:
1952 /* Two pointer-to-members are the same if they point to the same
1953 field or function in the same class. */
c8a209ca
NS
1954 if (PTRMEM_CST_MEMBER (t1) != PTRMEM_CST_MEMBER (t2))
1955 return false;
1956
1957 return same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2));
61a127b3 1958
943e3ede
MM
1959 case OVERLOAD:
1960 if (OVL_FUNCTION (t1) != OVL_FUNCTION (t2))
1961 return false;
1962 return cp_tree_equal (OVL_CHAIN (t1), OVL_CHAIN (t2));
1963
ea798d0f
PC
1964 case TRAIT_EXPR:
1965 if (TRAIT_EXPR_KIND (t1) != TRAIT_EXPR_KIND (t2))
1966 return false;
1967 return same_type_p (TRAIT_EXPR_TYPE1 (t1), TRAIT_EXPR_TYPE1 (t2))
1968 && same_type_p (TRAIT_EXPR_TYPE2 (t1), TRAIT_EXPR_TYPE2 (t2));
1969
7f85441b
KG
1970 default:
1971 break;
67d743fe
MS
1972 }
1973
1974 switch (TREE_CODE_CLASS (code1))
1975 {
6615c446
JO
1976 case tcc_unary:
1977 case tcc_binary:
1978 case tcc_comparison:
1979 case tcc_expression:
5039610b 1980 case tcc_vl_exp:
6615c446
JO
1981 case tcc_reference:
1982 case tcc_statement:
aa1826e2 1983 {
5039610b
SL
1984 int i, n;
1985
1986 n = TREE_OPERAND_LENGTH (t1);
1987 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
1988 && n != TREE_OPERAND_LENGTH (t2))
1989 return false;
9f63daea 1990
5039610b 1991 for (i = 0; i < n; ++i)
c8a209ca
NS
1992 if (!cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
1993 return false;
9f63daea 1994
c8a209ca 1995 return true;
aa1826e2 1996 }
9f63daea 1997
6615c446 1998 case tcc_type:
c8a209ca 1999 return same_type_p (t1, t2);
6615c446
JO
2000 default:
2001 gcc_unreachable ();
67d743fe 2002 }
6615c446 2003 /* We can get here with --disable-checking. */
c8a209ca 2004 return false;
67d743fe 2005}
73aad9b9 2006
d11ad92e
MS
2007/* The type of ARG when used as an lvalue. */
2008
2009tree
b57b79f7 2010lvalue_type (tree arg)
d11ad92e 2011{
2c73f9f5 2012 tree type = TREE_TYPE (arg);
8cd4c175 2013 return type;
d11ad92e
MS
2014}
2015
2016/* The type of ARG for printing error messages; denote lvalues with
2017 reference types. */
2018
2019tree
b57b79f7 2020error_type (tree arg)
d11ad92e
MS
2021{
2022 tree type = TREE_TYPE (arg);
9f63daea 2023
d11ad92e
MS
2024 if (TREE_CODE (type) == ARRAY_TYPE)
2025 ;
08476342
NS
2026 else if (TREE_CODE (type) == ERROR_MARK)
2027 ;
d11ad92e
MS
2028 else if (real_lvalue_p (arg))
2029 type = build_reference_type (lvalue_type (arg));
9e1e64ec 2030 else if (MAYBE_CLASS_TYPE_P (type))
d11ad92e
MS
2031 type = lvalue_type (arg);
2032
2033 return type;
2034}
eb66be0e
MS
2035
2036/* Does FUNCTION use a variable-length argument list? */
2037
2038int
58f9752a 2039varargs_function_p (const_tree function)
eb66be0e 2040{
58f9752a 2041 const_tree parm = TYPE_ARG_TYPES (TREE_TYPE (function));
eb66be0e
MS
2042 for (; parm; parm = TREE_CHAIN (parm))
2043 if (TREE_VALUE (parm) == void_type_node)
2044 return 0;
2045 return 1;
2046}
f94ae2f5
JM
2047
2048/* Returns 1 if decl is a member of a class. */
2049
2050int
58f9752a 2051member_p (const_tree decl)
f94ae2f5 2052{
58f9752a 2053 const_tree const ctx = DECL_CONTEXT (decl);
2f939d94 2054 return (ctx && TYPE_P (ctx));
f94ae2f5 2055}
51924768
JM
2056
2057/* Create a placeholder for member access where we don't actually have an
2058 object that the access is against. */
2059
2060tree
b57b79f7 2061build_dummy_object (tree type)
51924768 2062{
44689c12 2063 tree decl = build1 (NOP_EXPR, build_pointer_type (type), void_zero_node);
5ade1ed2 2064 return cp_build_indirect_ref (decl, NULL, tf_warning_or_error);
51924768
JM
2065}
2066
2067/* We've gotten a reference to a member of TYPE. Return *this if appropriate,
2068 or a dummy object otherwise. If BINFOP is non-0, it is filled with the
2069 binfo path from current_class_type to TYPE, or 0. */
2070
2071tree
b57b79f7 2072maybe_dummy_object (tree type, tree* binfop)
51924768
JM
2073{
2074 tree decl, context;
2db1ab2d 2075 tree binfo;
9f63daea 2076
51924768 2077 if (current_class_type
2db1ab2d 2078 && (binfo = lookup_base (current_class_type, type,
18e4be85 2079 ba_unique | ba_quiet, NULL)))
51924768
JM
2080 context = current_class_type;
2081 else
2082 {
2083 /* Reference from a nested class member function. */
2084 context = type;
2db1ab2d 2085 binfo = TYPE_BINFO (type);
51924768
JM
2086 }
2087
2db1ab2d
NS
2088 if (binfop)
2089 *binfop = binfo;
9f63daea 2090
a29e1034 2091 if (current_class_ref && context == current_class_type
3ebf5204 2092 /* Kludge: Make sure that current_class_type is actually
0cbd7506
MS
2093 correct. It might not be if we're in the middle of
2094 tsubst_default_argument. */
a29e1034
JM
2095 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (current_class_ref)),
2096 current_class_type))
51924768
JM
2097 decl = current_class_ref;
2098 else
2099 decl = build_dummy_object (context);
2100
2101 return decl;
2102}
2103
2104/* Returns 1 if OB is a placeholder object, or a pointer to one. */
2105
2106int
58f9752a 2107is_dummy_object (const_tree ob)
51924768
JM
2108{
2109 if (TREE_CODE (ob) == INDIRECT_REF)
2110 ob = TREE_OPERAND (ob, 0);
2111 return (TREE_CODE (ob) == NOP_EXPR
44689c12 2112 && TREE_OPERAND (ob, 0) == void_zero_node);
51924768 2113}
5524676d
JM
2114
2115/* Returns 1 iff type T is a POD type, as defined in [basic.types]. */
2116
2117int
58f9752a 2118pod_type_p (const_tree t)
5524676d 2119{
4e9b57fa 2120 /* This CONST_CAST is okay because strip_array_types returns its
75547801 2121 argument unmodified and we assign it to a const_tree. */
b1d5455a 2122 t = strip_array_types (CONST_CAST_TREE(t));
5524676d 2123
17bbb839
MM
2124 if (t == error_mark_node)
2125 return 1;
52fb2769
NS
2126 if (INTEGRAL_TYPE_P (t))
2127 return 1; /* integral, character or enumeral type */
2128 if (FLOAT_TYPE_P (t))
5524676d 2129 return 1;
52fb2769
NS
2130 if (TYPE_PTR_P (t))
2131 return 1; /* pointer to non-member */
a5ac359a
MM
2132 if (TYPE_PTR_TO_MEMBER_P (t))
2133 return 1; /* pointer to member */
b1009a5d
ZL
2134
2135 if (TREE_CODE (t) == VECTOR_TYPE)
44370687 2136 return 1; /* vectors are (small) arrays of scalars */
b1009a5d 2137
72114ca1 2138 if (! RECORD_OR_UNION_CODE_P (TREE_CODE (t)))
52fb2769 2139 return 0; /* other non-class type (reference or function) */
72114ca1
JM
2140 if (! CLASS_TYPE_P (t))
2141 return 1; /* struct created by the back end */
52fb2769 2142 if (CLASSTYPE_NON_POD_P (t))
5524676d 2143 return 0;
5524676d
JM
2144 return 1;
2145}
e5dc5fb2 2146
39ef6592
LC
2147/* Nonzero iff type T is a class template implicit specialization. */
2148
2149bool
ac7d7749 2150class_tmpl_impl_spec_p (const_tree t)
39ef6592
LC
2151{
2152 return CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INSTANTIATION (t);
2153}
2154
94e6e4c4
AO
2155/* Returns 1 iff zero initialization of type T means actually storing
2156 zeros in it. */
2157
2158int
58f9752a 2159zero_init_p (const_tree t)
94e6e4c4 2160{
4e9b57fa 2161 /* This CONST_CAST is okay because strip_array_types returns its
75547801 2162 argument unmodified and we assign it to a const_tree. */
b1d5455a 2163 t = strip_array_types (CONST_CAST_TREE(t));
94e6e4c4 2164
17bbb839
MM
2165 if (t == error_mark_node)
2166 return 1;
2167
94e6e4c4
AO
2168 /* NULL pointers to data members are initialized with -1. */
2169 if (TYPE_PTRMEM_P (t))
2170 return 0;
2171
2172 /* Classes that contain types that can't be zero-initialized, cannot
2173 be zero-initialized themselves. */
2174 if (CLASS_TYPE_P (t) && CLASSTYPE_NON_ZERO_INIT_P (t))
2175 return 0;
2176
2177 return 1;
2178}
2179
91d231cb 2180/* Table of valid C++ attributes. */
349ae713 2181const struct attribute_spec cxx_attribute_table[] =
e5dc5fb2 2182{
91d231cb
JM
2183 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2184 { "java_interface", 0, 0, false, false, false, handle_java_interface_attribute },
2185 { "com_interface", 0, 0, false, false, false, handle_com_interface_attribute },
2186 { "init_priority", 1, 1, true, false, false, handle_init_priority_attribute },
0cbd7506 2187 { NULL, 0, 0, false, false, false, NULL }
91d231cb
JM
2188};
2189
2190/* Handle a "java_interface" attribute; arguments as in
2191 struct attribute_spec.handler. */
2192static tree
9f63daea 2193handle_java_interface_attribute (tree* node,
0cbd7506
MS
2194 tree name,
2195 tree args ATTRIBUTE_UNUSED ,
2196 int flags,
2197 bool* no_add_attrs)
91d231cb
JM
2198{
2199 if (DECL_P (*node)
2200 || !CLASS_TYPE_P (*node)
2201 || !TYPE_FOR_JAVA (*node))
60c87482 2202 {
a82e1a7d 2203 error ("%qE attribute can only be applied to Java class definitions",
4460cef2 2204 name);
91d231cb
JM
2205 *no_add_attrs = true;
2206 return NULL_TREE;
60c87482 2207 }
91d231cb 2208 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8dd16ecc 2209 *node = build_variant_type_copy (*node);
91d231cb 2210 TYPE_JAVA_INTERFACE (*node) = 1;
e5dc5fb2 2211
91d231cb
JM
2212 return NULL_TREE;
2213}
2214
2215/* Handle a "com_interface" attribute; arguments as in
2216 struct attribute_spec.handler. */
2217static tree
9f63daea 2218handle_com_interface_attribute (tree* node,
0cbd7506
MS
2219 tree name,
2220 tree args ATTRIBUTE_UNUSED ,
2221 int flags ATTRIBUTE_UNUSED ,
2222 bool* no_add_attrs)
91d231cb
JM
2223{
2224 static int warned;
2225
2226 *no_add_attrs = true;
2227
2228 if (DECL_P (*node)
2229 || !CLASS_TYPE_P (*node)
2230 || *node != TYPE_MAIN_VARIANT (*node))
e5dc5fb2 2231 {
5c498b10
DD
2232 warning (OPT_Wattributes, "%qE attribute can only be applied "
2233 "to class definitions", name);
91d231cb
JM
2234 return NULL_TREE;
2235 }
e5dc5fb2 2236
91d231cb 2237 if (!warned++)
d4ee4d25 2238 warning (0, "%qE is obsolete; g++ vtables are now COM-compatible by default",
4460cef2 2239 name);
91d231cb
JM
2240
2241 return NULL_TREE;
2242}
2243
2244/* Handle an "init_priority" attribute; arguments as in
2245 struct attribute_spec.handler. */
2246static tree
9f63daea 2247handle_init_priority_attribute (tree* node,
0cbd7506
MS
2248 tree name,
2249 tree args,
2250 int flags ATTRIBUTE_UNUSED ,
2251 bool* no_add_attrs)
91d231cb
JM
2252{
2253 tree initp_expr = TREE_VALUE (args);
2254 tree decl = *node;
2255 tree type = TREE_TYPE (decl);
2256 int pri;
2257
2258 STRIP_NOPS (initp_expr);
9f63daea 2259
91d231cb
JM
2260 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
2261 {
2262 error ("requested init_priority is not an integer constant");
2263 *no_add_attrs = true;
2264 return NULL_TREE;
2265 }
e5dc5fb2 2266
91d231cb 2267 pri = TREE_INT_CST_LOW (initp_expr);
9f63daea 2268
91d231cb
JM
2269 type = strip_array_types (type);
2270
2271 if (decl == NULL_TREE
2272 || TREE_CODE (decl) != VAR_DECL
2273 || !TREE_STATIC (decl)
2274 || DECL_EXTERNAL (decl)
2275 || (TREE_CODE (type) != RECORD_TYPE
2276 && TREE_CODE (type) != UNION_TYPE)
2277 /* Static objects in functions are initialized the
2278 first time control passes through that
2279 function. This is not precise enough to pin down an
c6002625 2280 init_priority value, so don't allow it. */
9f63daea 2281 || current_function_decl)
91d231cb 2282 {
a82e1a7d 2283 error ("can only use %qE attribute on file-scope definitions "
0cbd7506 2284 "of objects of class type", name);
91d231cb
JM
2285 *no_add_attrs = true;
2286 return NULL_TREE;
2287 }
e5dc5fb2 2288
91d231cb
JM
2289 if (pri > MAX_INIT_PRIORITY || pri <= 0)
2290 {
2291 error ("requested init_priority is out of range");
2292 *no_add_attrs = true;
2293 return NULL_TREE;
2294 }
e5dc5fb2 2295
91d231cb
JM
2296 /* Check for init_priorities that are reserved for
2297 language and runtime support implementations.*/
2298 if (pri <= MAX_RESERVED_INIT_PRIORITY)
2299 {
9f63daea 2300 warning
d4ee4d25 2301 (0, "requested init_priority is reserved for internal use");
e5dc5fb2
JM
2302 }
2303
91d231cb
JM
2304 if (SUPPORTS_INIT_PRIORITY)
2305 {
820cc88f
DB
2306 SET_DECL_INIT_PRIORITY (decl, pri);
2307 DECL_HAS_INIT_PRIORITY_P (decl) = 1;
91d231cb
JM
2308 return NULL_TREE;
2309 }
2310 else
2311 {
a82e1a7d 2312 error ("%qE attribute is not supported on this platform", name);
91d231cb
JM
2313 *no_add_attrs = true;
2314 return NULL_TREE;
2315 }
e5dc5fb2 2316}
87533b37
MM
2317
2318/* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the
2319 thing pointed to by the constant. */
2320
2321tree
b57b79f7 2322make_ptrmem_cst (tree type, tree member)
87533b37
MM
2323{
2324 tree ptrmem_cst = make_node (PTRMEM_CST);
87533b37
MM
2325 TREE_TYPE (ptrmem_cst) = type;
2326 PTRMEM_CST_MEMBER (ptrmem_cst) = member;
2327 return ptrmem_cst;
2328}
2329
e9525111 2330/* Build a variant of TYPE that has the indicated ATTRIBUTES. May
51035976 2331 return an existing type if an appropriate type already exists. */
e9525111
MM
2332
2333tree
2334cp_build_type_attribute_variant (tree type, tree attributes)
2335{
2336 tree new_type;
2337
2338 new_type = build_type_attribute_variant (type, attributes);
2339 if (TREE_CODE (new_type) == FUNCTION_TYPE
9f63daea 2340 && (TYPE_RAISES_EXCEPTIONS (new_type)
e9525111
MM
2341 != TYPE_RAISES_EXCEPTIONS (type)))
2342 new_type = build_exception_variant (new_type,
2343 TYPE_RAISES_EXCEPTIONS (type));
8e30dcf3
JM
2344
2345 /* Making a new main variant of a class type is broken. */
2346 gcc_assert (!CLASS_TYPE_P (type) || new_type == type);
2347
e9525111
MM
2348 return new_type;
2349}
2350
2dff8956
JJ
2351/* Return TRUE if TYPE1 and TYPE2 are identical for type hashing purposes.
2352 Called only after doing all language independent checks. Only
2353 to check TYPE_RAISES_EXCEPTIONS for FUNCTION_TYPE, the rest is already
2354 compared in type_hash_eq. */
2355
2356bool
2357cxx_type_hash_eq (const_tree typea, const_tree typeb)
2358{
2359 gcc_assert (TREE_CODE (typea) == FUNCTION_TYPE);
2360
2361 return comp_except_specs (TYPE_RAISES_EXCEPTIONS (typea),
2362 TYPE_RAISES_EXCEPTIONS (typeb), 1);
2363}
2364
25af8512 2365/* Apply FUNC to all language-specific sub-trees of TP in a pre-order
350fae66 2366 traversal. Called from walk_tree. */
25af8512 2367
9f63daea 2368tree
350fae66 2369cp_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func,
0c58f841 2370 void *data, struct pointer_set_t *pset)
25af8512
AO
2371{
2372 enum tree_code code = TREE_CODE (*tp);
2373 tree result;
9f63daea 2374
25af8512
AO
2375#define WALK_SUBTREE(NODE) \
2376 do \
2377 { \
14588106 2378 result = cp_walk_tree (&(NODE), func, data, pset); \
6de9cd9a 2379 if (result) goto out; \
25af8512
AO
2380 } \
2381 while (0)
2382
2383 /* Not one of the easy cases. We must explicitly go through the
2384 children. */
6de9cd9a 2385 result = NULL_TREE;
25af8512
AO
2386 switch (code)
2387 {
2388 case DEFAULT_ARG:
2389 case TEMPLATE_TEMPLATE_PARM:
2390 case BOUND_TEMPLATE_TEMPLATE_PARM:
b8c6534b 2391 case UNBOUND_CLASS_TEMPLATE:
25af8512
AO
2392 case TEMPLATE_PARM_INDEX:
2393 case TEMPLATE_TYPE_PARM:
2394 case TYPENAME_TYPE:
2395 case TYPEOF_TYPE:
da1d7781 2396 /* None of these have subtrees other than those already walked
0cbd7506 2397 above. */
25af8512
AO
2398 *walk_subtrees_p = 0;
2399 break;
2400
5d80a306
DG
2401 case BASELINK:
2402 WALK_SUBTREE (BASELINK_FUNCTIONS (*tp));
2403 *walk_subtrees_p = 0;
2404 break;
2405
25af8512
AO
2406 case PTRMEM_CST:
2407 WALK_SUBTREE (TREE_TYPE (*tp));
2408 *walk_subtrees_p = 0;
2409 break;
2410
2411 case TREE_LIST:
5dae1114 2412 WALK_SUBTREE (TREE_PURPOSE (*tp));
25af8512
AO
2413 break;
2414
2415 case OVERLOAD:
2416 WALK_SUBTREE (OVL_FUNCTION (*tp));
2417 WALK_SUBTREE (OVL_CHAIN (*tp));
2418 *walk_subtrees_p = 0;
4439d02f
DG
2419 break;
2420
2421 case USING_DECL:
2422 WALK_SUBTREE (DECL_NAME (*tp));
2423 WALK_SUBTREE (USING_DECL_SCOPE (*tp));
2424 WALK_SUBTREE (USING_DECL_DECLS (*tp));
2425 *walk_subtrees_p = 0;
25af8512
AO
2426 break;
2427
2428 case RECORD_TYPE:
2429 if (TYPE_PTRMEMFUNC_P (*tp))
2430 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (*tp));
2431 break;
2432
5d80a306
DG
2433 case TYPE_ARGUMENT_PACK:
2434 case NONTYPE_ARGUMENT_PACK:
2435 {
2436 tree args = ARGUMENT_PACK_ARGS (*tp);
2437 int i, len = TREE_VEC_LENGTH (args);
2438 for (i = 0; i < len; i++)
2439 WALK_SUBTREE (TREE_VEC_ELT (args, i));
2440 }
2441 break;
2442
2443 case TYPE_PACK_EXPANSION:
2444 WALK_SUBTREE (TREE_TYPE (*tp));
2445 *walk_subtrees_p = 0;
2446 break;
2447
2448 case EXPR_PACK_EXPANSION:
2449 WALK_SUBTREE (TREE_OPERAND (*tp, 0));
2450 *walk_subtrees_p = 0;
2451 break;
2452
2453 case CAST_EXPR:
a7cbc517
JJ
2454 case REINTERPRET_CAST_EXPR:
2455 case STATIC_CAST_EXPR:
2456 case CONST_CAST_EXPR:
2457 case DYNAMIC_CAST_EXPR:
5d80a306
DG
2458 if (TREE_TYPE (*tp))
2459 WALK_SUBTREE (TREE_TYPE (*tp));
2460
2461 {
2462 int i;
2463 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (*tp)); ++i)
2464 WALK_SUBTREE (TREE_OPERAND (*tp, i));
2465 }
2466 *walk_subtrees_p = 0;
2467 break;
2468
cb68ec50
PC
2469 case TRAIT_EXPR:
2470 WALK_SUBTREE (TRAIT_EXPR_TYPE1 (*tp));
2471 WALK_SUBTREE (TRAIT_EXPR_TYPE2 (*tp));
2472 *walk_subtrees_p = 0;
2473 break;
2474
3ad6a8e1
DG
2475 case DECLTYPE_TYPE:
2476 WALK_SUBTREE (DECLTYPE_TYPE_EXPR (*tp));
2477 *walk_subtrees_p = 0;
2478 break;
2479
2480
25af8512 2481 default:
350fae66 2482 return NULL_TREE;
25af8512
AO
2483 }
2484
2485 /* We didn't find what we were looking for. */
6de9cd9a 2486 out:
6de9cd9a 2487 return result;
25af8512
AO
2488
2489#undef WALK_SUBTREE
2490}
2491
b655f214
MM
2492/* Like save_expr, but for C++. */
2493
2494tree
2495cp_save_expr (tree expr)
2496{
2497 /* There is no reason to create a SAVE_EXPR within a template; if
2498 needed, we can create the SAVE_EXPR when instantiating the
2499 template. Furthermore, the middle-end cannot handle C++-specific
2500 tree codes. */
2501 if (processing_template_decl)
2502 return expr;
2503 return save_expr (expr);
2504}
2505
87e3dbc9
MM
2506/* Initialize tree.c. */
2507
0a818f84 2508void
b57b79f7 2509init_tree (void)
0a818f84 2510{
e2500fed 2511 list_hash_table = htab_create_ggc (31, list_hash, list_hash_eq, NULL);
0a818f84
GRK
2512}
2513
872f37f9 2514/* Returns the kind of special function that DECL (a FUNCTION_DECL)
50ad9642
MM
2515 is. Note that sfk_none is zero, so this function can be used as a
2516 predicate to test whether or not DECL is a special function. */
872f37f9
MM
2517
2518special_function_kind
58f9752a 2519special_function_p (const_tree decl)
872f37f9
MM
2520{
2521 /* Rather than doing all this stuff with magic names, we should
2522 probably have a field of type `special_function_kind' in
2523 DECL_LANG_SPECIFIC. */
2524 if (DECL_COPY_CONSTRUCTOR_P (decl))
2525 return sfk_copy_constructor;
2526 if (DECL_CONSTRUCTOR_P (decl))
2527 return sfk_constructor;
596ea4e5 2528 if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
872f37f9
MM
2529 return sfk_assignment_operator;
2530 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
2531 return sfk_destructor;
2532 if (DECL_COMPLETE_DESTRUCTOR_P (decl))
2533 return sfk_complete_destructor;
2534 if (DECL_BASE_DESTRUCTOR_P (decl))
2535 return sfk_base_destructor;
2536 if (DECL_DELETING_DESTRUCTOR_P (decl))
2537 return sfk_deleting_destructor;
2538 if (DECL_CONV_FN_P (decl))
2539 return sfk_conversion;
2540
2541 return sfk_none;
2542}
7b019c19 2543
838dfd8a 2544/* Returns nonzero if TYPE is a character type, including wchar_t. */
7b019c19
MM
2545
2546int
b57b79f7 2547char_type_p (tree type)
7b019c19
MM
2548{
2549 return (same_type_p (type, char_type_node)
2550 || same_type_p (type, unsigned_char_type_node)
2551 || same_type_p (type, signed_char_type_node)
b6baa67d
KVH
2552 || same_type_p (type, char16_type_node)
2553 || same_type_p (type, char32_type_node)
7b019c19
MM
2554 || same_type_p (type, wchar_type_node));
2555}
ad50e811
MM
2556
2557/* Returns the kind of linkage associated with the indicated DECL. Th
2558 value returned is as specified by the language standard; it is
2559 independent of implementation details regarding template
2560 instantiation, etc. For example, it is possible that a declaration
2561 to which this function assigns external linkage would not show up
2562 as a global symbol when you run `nm' on the resulting object file. */
2563
2564linkage_kind
b57b79f7 2565decl_linkage (tree decl)
ad50e811
MM
2566{
2567 /* This function doesn't attempt to calculate the linkage from first
2568 principles as given in [basic.link]. Instead, it makes use of
2569 the fact that we have already set TREE_PUBLIC appropriately, and
2570 then handles a few special cases. Ideally, we would calculate
2571 linkage first, and then transform that into a concrete
2572 implementation. */
2573
2574 /* Things that don't have names have no linkage. */
2575 if (!DECL_NAME (decl))
2576 return lk_none;
2577
c02cdc25
TT
2578 /* Fields have no linkage. */
2579 if (TREE_CODE (decl) == FIELD_DECL)
2580 return lk_none;
2581
ad50e811
MM
2582 /* Things that are TREE_PUBLIC have external linkage. */
2583 if (TREE_PUBLIC (decl))
2584 return lk_external;
3db45ab5 2585
b70f0f48
JM
2586 if (TREE_CODE (decl) == NAMESPACE_DECL)
2587 return lk_external;
2588
3db45ab5 2589 /* Linkage of a CONST_DECL depends on the linkage of the enumeration
3f774254
DB
2590 type. */
2591 if (TREE_CODE (decl) == CONST_DECL)
2592 return decl_linkage (TYPE_NAME (TREE_TYPE (decl)));
ad50e811
MM
2593
2594 /* Some things that are not TREE_PUBLIC have external linkage, too.
2595 For example, on targets that don't have weak symbols, we make all
2596 template instantiations have internal linkage (in the object
2597 file), but the symbols should still be treated as having external
2598 linkage from the point of view of the language. */
b9e75696
JM
2599 if (TREE_CODE (decl) != TYPE_DECL && DECL_LANG_SPECIFIC (decl)
2600 && DECL_COMDAT (decl))
ad50e811
MM
2601 return lk_external;
2602
2603 /* Things in local scope do not have linkage, if they don't have
2604 TREE_PUBLIC set. */
2605 if (decl_function_context (decl))
2606 return lk_none;
2607
b70f0f48
JM
2608 /* Members of the anonymous namespace also have TREE_PUBLIC unset, but
2609 are considered to have external linkage for language purposes. DECLs
2610 really meant to have internal linkage have DECL_THIS_STATIC set. */
ce41114b 2611 if (TREE_CODE (decl) == TYPE_DECL)
b70f0f48 2612 return lk_external;
ce41114b
JJ
2613 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2614 {
2615 if (!DECL_THIS_STATIC (decl))
2616 return lk_external;
2617
2618 /* Static data members and static member functions from classes
2619 in anonymous namespace also don't have TREE_PUBLIC set. */
2620 if (DECL_CLASS_CONTEXT (decl))
2621 return lk_external;
2622 }
b70f0f48 2623
ad50e811
MM
2624 /* Everything else has internal linkage. */
2625 return lk_internal;
2626}
6f30f1f1 2627\f
9beafc83
MM
2628/* EXP is an expression that we want to pre-evaluate. Returns (in
2629 *INITP) an expression that will perform the pre-evaluation. The
2630 value returned by this function is a side-effect free expression
2631 equivalent to the pre-evaluated expression. Callers must ensure
2632 that *INITP is evaluated before EXP. */
6f30f1f1
JM
2633
2634tree
b57b79f7 2635stabilize_expr (tree exp, tree* initp)
6f30f1f1
JM
2636{
2637 tree init_expr;
2638
2639 if (!TREE_SIDE_EFFECTS (exp))
9beafc83 2640 init_expr = NULL_TREE;
6f30f1f1
JM
2641 else if (!real_lvalue_p (exp)
2642 || !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (exp)))
2643 {
2644 init_expr = get_target_expr (exp);
2645 exp = TARGET_EXPR_SLOT (init_expr);
2646 }
2647 else
2648 {
5ade1ed2 2649 exp = cp_build_unary_op (ADDR_EXPR, exp, 1, tf_warning_or_error);
6f30f1f1
JM
2650 init_expr = get_target_expr (exp);
2651 exp = TARGET_EXPR_SLOT (init_expr);
5ade1ed2 2652 exp = cp_build_indirect_ref (exp, 0, tf_warning_or_error);
6f30f1f1 2653 }
6f30f1f1 2654 *initp = init_expr;
9beafc83
MM
2655
2656 gcc_assert (!TREE_SIDE_EFFECTS (exp));
6f30f1f1
JM
2657 return exp;
2658}
6de9cd9a 2659
be93747e 2660/* Add NEW_EXPR, an expression whose value we don't care about, after the
40aac948
JM
2661 similar expression ORIG. */
2662
2663tree
be93747e 2664add_stmt_to_compound (tree orig, tree new_expr)
40aac948 2665{
be93747e 2666 if (!new_expr || !TREE_SIDE_EFFECTS (new_expr))
40aac948
JM
2667 return orig;
2668 if (!orig || !TREE_SIDE_EFFECTS (orig))
be93747e
KG
2669 return new_expr;
2670 return build2 (COMPOUND_EXPR, void_type_node, orig, new_expr);
40aac948
JM
2671}
2672
9beafc83
MM
2673/* Like stabilize_expr, but for a call whose arguments we want to
2674 pre-evaluate. CALL is modified in place to use the pre-evaluated
2675 arguments, while, upon return, *INITP contains an expression to
2676 compute the arguments. */
6de9cd9a
DN
2677
2678void
2679stabilize_call (tree call, tree *initp)
2680{
2681 tree inits = NULL_TREE;
5039610b
SL
2682 int i;
2683 int nargs = call_expr_nargs (call);
6de9cd9a 2684
28267cfc
JJ
2685 if (call == error_mark_node || processing_template_decl)
2686 {
2687 *initp = NULL_TREE;
2688 return;
2689 }
6de9cd9a 2690
5039610b 2691 gcc_assert (TREE_CODE (call) == CALL_EXPR);
6de9cd9a 2692
5039610b
SL
2693 for (i = 0; i < nargs; i++)
2694 {
2695 tree init;
2696 CALL_EXPR_ARG (call, i) =
2697 stabilize_expr (CALL_EXPR_ARG (call, i), &init);
2698 inits = add_stmt_to_compound (inits, init);
2699 }
2700
2701 *initp = inits;
2702}
2703
2704/* Like stabilize_expr, but for an AGGR_INIT_EXPR whose arguments we want
2705 to pre-evaluate. CALL is modified in place to use the pre-evaluated
2706 arguments, while, upon return, *INITP contains an expression to
2707 compute the arguments. */
2708
2709void
2710stabilize_aggr_init (tree call, tree *initp)
2711{
2712 tree inits = NULL_TREE;
2713 int i;
2714 int nargs = aggr_init_expr_nargs (call);
2715
2716 if (call == error_mark_node)
2717 return;
2718
2719 gcc_assert (TREE_CODE (call) == AGGR_INIT_EXPR);
2720
2721 for (i = 0; i < nargs; i++)
2722 {
2723 tree init;
2724 AGGR_INIT_EXPR_ARG (call, i) =
2725 stabilize_expr (AGGR_INIT_EXPR_ARG (call, i), &init);
2726 inits = add_stmt_to_compound (inits, init);
2727 }
6de9cd9a
DN
2728
2729 *initp = inits;
2730}
2731
9beafc83
MM
2732/* Like stabilize_expr, but for an initialization.
2733
2734 If the initialization is for an object of class type, this function
2735 takes care not to introduce additional temporaries.
2736
2737 Returns TRUE iff the expression was successfully pre-evaluated,
2738 i.e., if INIT is now side-effect free, except for, possible, a
2739 single call to a constructor. */
6de9cd9a
DN
2740
2741bool
2742stabilize_init (tree init, tree *initp)
2743{
2744 tree t = init;
2745
9beafc83
MM
2746 *initp = NULL_TREE;
2747
28267cfc 2748 if (t == error_mark_node || processing_template_decl)
6de9cd9a
DN
2749 return true;
2750
2751 if (TREE_CODE (t) == INIT_EXPR
844ae01d
JM
2752 && TREE_CODE (TREE_OPERAND (t, 1)) != TARGET_EXPR
2753 && TREE_CODE (TREE_OPERAND (t, 1)) != AGGR_INIT_EXPR)
6de9cd9a 2754 {
9beafc83
MM
2755 TREE_OPERAND (t, 1) = stabilize_expr (TREE_OPERAND (t, 1), initp);
2756 return true;
2757 }
6de9cd9a 2758
9beafc83
MM
2759 if (TREE_CODE (t) == INIT_EXPR)
2760 t = TREE_OPERAND (t, 1);
2761 if (TREE_CODE (t) == TARGET_EXPR)
2762 t = TARGET_EXPR_INITIAL (t);
2763 if (TREE_CODE (t) == COMPOUND_EXPR)
2764 t = expr_last (t);
2765 if (TREE_CODE (t) == CONSTRUCTOR
2766 && EMPTY_CONSTRUCTOR_P (t))
2767 /* Default-initialization. */
2768 return true;
2769
2770 /* If the initializer is a COND_EXPR, we can't preevaluate
2771 anything. */
2772 if (TREE_CODE (t) == COND_EXPR)
2773 return false;
6de9cd9a 2774
5039610b 2775 if (TREE_CODE (t) == CALL_EXPR)
9beafc83
MM
2776 {
2777 stabilize_call (t, initp);
2778 return true;
6de9cd9a
DN
2779 }
2780
5039610b
SL
2781 if (TREE_CODE (t) == AGGR_INIT_EXPR)
2782 {
2783 stabilize_aggr_init (t, initp);
2784 return true;
2785 }
2786
9beafc83
MM
2787 /* The initialization is being performed via a bitwise copy -- and
2788 the item copied may have side effects. */
2789 return TREE_SIDE_EFFECTS (init);
6de9cd9a
DN
2790}
2791
455f19cb
MM
2792/* Like "fold", but should be used whenever we might be processing the
2793 body of a template. */
2794
2795tree
2796fold_if_not_in_template (tree expr)
2797{
2798 /* In the body of a template, there is never any need to call
2799 "fold". We will call fold later when actually instantiating the
2800 template. Integral constant expressions in templates will be
f9f1c24e 2801 evaluated via fold_non_dependent_expr, as necessary. */
392e3d51
RS
2802 if (processing_template_decl)
2803 return expr;
2804
2805 /* Fold C++ front-end specific tree codes. */
2806 if (TREE_CODE (expr) == UNARY_PLUS_EXPR)
2807 return fold_convert (TREE_TYPE (expr), TREE_OPERAND (expr, 0));
2808
2809 return fold (expr);
455f19cb
MM
2810}
2811
015c2c66
MM
2812/* Returns true if a cast to TYPE may appear in an integral constant
2813 expression. */
2814
2815bool
2816cast_valid_in_integral_constant_expression_p (tree type)
2817{
2818 return (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
2819 || dependent_type_p (type)
2820 || type == error_mark_node);
2821}
2822
e2500fed
GK
2823\f
2824#if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
2825/* Complain that some language-specific thing hanging off a tree
2826 node has been accessed improperly. */
2827
2828void
b57b79f7 2829lang_check_failed (const char* file, int line, const char* function)
e2500fed
GK
2830{
2831 internal_error ("lang_* check: failed in %s, at %s:%d",
2832 function, trim_filename (file), line);
2833}
2834#endif /* ENABLE_TREE_CHECKING */
2835
2836#include "gt-cp-tree.h"