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