Merge from mainline (165734:167278).
[official-gcc/graphite-test-results.git] / gcc / cp / tree.c
blobba8e978429325b4644b72c02f900894a7da3d2f5
1 /* Language-dependent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
4 Free Software Foundation, Inc.
5 Hacked by Michael Tiemann (tiemann@cygnus.com)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "flags.h"
30 #include "toplev.h"
31 #include "tree-inline.h"
32 #include "debug.h"
33 #include "convert.h"
34 #include "cgraph.h"
35 #include "splay-tree.h"
36 #include "gimple.h" /* gimple_has_body_p */
38 static tree bot_manip (tree *, int *, void *);
39 static tree bot_replace (tree *, int *, void *);
40 static int list_hash_eq (const void *, const void *);
41 static hashval_t list_hash_pieces (tree, tree, tree);
42 static hashval_t list_hash (const void *);
43 static tree build_target_expr (tree, tree);
44 static tree count_trees_r (tree *, int *, void *);
45 static tree verify_stmt_tree_r (tree *, int *, void *);
46 static tree build_local_temp (tree);
48 static tree handle_java_interface_attribute (tree *, tree, tree, int, bool *);
49 static tree handle_com_interface_attribute (tree *, tree, tree, int, bool *);
50 static tree handle_init_priority_attribute (tree *, tree, tree, int, bool *);
52 /* If REF is an lvalue, returns the kind of lvalue that REF is.
53 Otherwise, returns clk_none. */
55 cp_lvalue_kind
56 lvalue_kind (const_tree ref)
58 cp_lvalue_kind op1_lvalue_kind = clk_none;
59 cp_lvalue_kind op2_lvalue_kind = clk_none;
61 /* Expressions of reference type are sometimes wrapped in
62 INDIRECT_REFs. INDIRECT_REFs are just internal compiler
63 representation, not part of the language, so we have to look
64 through them. */
65 if (TREE_CODE (ref) == INDIRECT_REF
66 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0)))
67 == REFERENCE_TYPE)
68 return lvalue_kind (TREE_OPERAND (ref, 0));
70 if (TREE_TYPE (ref)
71 && TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
73 /* unnamed rvalue references are rvalues */
74 if (TYPE_REF_IS_RVALUE (TREE_TYPE (ref))
75 && TREE_CODE (ref) != PARM_DECL
76 && TREE_CODE (ref) != VAR_DECL
77 && TREE_CODE (ref) != COMPONENT_REF)
78 return clk_rvalueref;
80 /* lvalue references and named rvalue references are lvalues. */
81 return clk_ordinary;
84 if (ref == current_class_ptr)
85 return clk_none;
87 switch (TREE_CODE (ref))
89 case SAVE_EXPR:
90 return clk_none;
91 /* preincrements and predecrements are valid lvals, provided
92 what they refer to are valid lvals. */
93 case PREINCREMENT_EXPR:
94 case PREDECREMENT_EXPR:
95 case TRY_CATCH_EXPR:
96 case WITH_CLEANUP_EXPR:
97 case REALPART_EXPR:
98 case IMAGPART_EXPR:
99 return lvalue_kind (TREE_OPERAND (ref, 0));
101 case COMPONENT_REF:
102 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
103 /* Look at the member designator. */
104 if (!op1_lvalue_kind)
106 else if (is_overloaded_fn (TREE_OPERAND (ref, 1)))
107 /* The "field" can be a FUNCTION_DECL or an OVERLOAD in some
108 situations. If we're seeing a COMPONENT_REF, it's a non-static
109 member, so it isn't an lvalue. */
110 op1_lvalue_kind = clk_none;
111 else if (TREE_CODE (TREE_OPERAND (ref, 1)) != FIELD_DECL)
112 /* This can be IDENTIFIER_NODE in a template. */;
113 else if (DECL_C_BIT_FIELD (TREE_OPERAND (ref, 1)))
115 /* Clear the ordinary bit. If this object was a class
116 rvalue we want to preserve that information. */
117 op1_lvalue_kind &= ~clk_ordinary;
118 /* The lvalue is for a bitfield. */
119 op1_lvalue_kind |= clk_bitfield;
121 else if (DECL_PACKED (TREE_OPERAND (ref, 1)))
122 op1_lvalue_kind |= clk_packed;
124 return op1_lvalue_kind;
126 case STRING_CST:
127 case COMPOUND_LITERAL_EXPR:
128 return clk_ordinary;
130 case CONST_DECL:
131 /* CONST_DECL without TREE_STATIC are enumeration values and
132 thus not lvalues. With TREE_STATIC they are used by ObjC++
133 in objc_build_string_object and need to be considered as
134 lvalues. */
135 if (! TREE_STATIC (ref))
136 return clk_none;
137 case VAR_DECL:
138 if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
139 && DECL_LANG_SPECIFIC (ref)
140 && DECL_IN_AGGR_P (ref))
141 return clk_none;
142 case INDIRECT_REF:
143 case ARRAY_REF:
144 case PARM_DECL:
145 case RESULT_DECL:
146 if (TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
147 return clk_ordinary;
148 break;
150 /* A currently unresolved scope ref. */
151 case SCOPE_REF:
152 gcc_unreachable ();
153 case MAX_EXPR:
154 case MIN_EXPR:
155 /* Disallow <? and >? as lvalues if either argument side-effects. */
156 if (TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 0))
157 || TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 1)))
158 return clk_none;
159 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
160 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1));
161 break;
163 case COND_EXPR:
164 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1)
165 ? TREE_OPERAND (ref, 1)
166 : TREE_OPERAND (ref, 0));
167 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 2));
168 break;
170 case MODIFY_EXPR:
171 return clk_ordinary;
173 case COMPOUND_EXPR:
174 return lvalue_kind (TREE_OPERAND (ref, 1));
176 case TARGET_EXPR:
177 return clk_class;
179 case VA_ARG_EXPR:
180 return (CLASS_TYPE_P (TREE_TYPE (ref)) ? clk_class : clk_none);
182 case CALL_EXPR:
183 /* Any class-valued call would be wrapped in a TARGET_EXPR. */
184 return clk_none;
186 case FUNCTION_DECL:
187 /* All functions (except non-static-member functions) are
188 lvalues. */
189 return (DECL_NONSTATIC_MEMBER_FUNCTION_P (ref)
190 ? clk_none : clk_ordinary);
192 case BASELINK:
193 /* We now represent a reference to a single static member function
194 with a BASELINK. */
195 /* This CONST_CAST is okay because BASELINK_FUNCTIONS returns
196 its argument unmodified and we assign it to a const_tree. */
197 return lvalue_kind (BASELINK_FUNCTIONS (CONST_CAST_TREE (ref)));
199 case NON_DEPENDENT_EXPR:
200 /* We must consider NON_DEPENDENT_EXPRs to be lvalues so that
201 things like "&E" where "E" is an expression with a
202 non-dependent type work. It is safe to be lenient because an
203 error will be issued when the template is instantiated if "E"
204 is not an lvalue. */
205 return clk_ordinary;
207 default:
208 break;
211 /* If one operand is not an lvalue at all, then this expression is
212 not an lvalue. */
213 if (!op1_lvalue_kind || !op2_lvalue_kind)
214 return clk_none;
216 /* Otherwise, it's an lvalue, and it has all the odd properties
217 contributed by either operand. */
218 op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind;
219 /* It's not an ordinary lvalue if it involves any other kind. */
220 if ((op1_lvalue_kind & ~clk_ordinary) != clk_none)
221 op1_lvalue_kind &= ~clk_ordinary;
222 /* It can't be both a pseudo-lvalue and a non-addressable lvalue.
223 A COND_EXPR of those should be wrapped in a TARGET_EXPR. */
224 if ((op1_lvalue_kind & (clk_rvalueref|clk_class))
225 && (op1_lvalue_kind & (clk_bitfield|clk_packed)))
226 op1_lvalue_kind = clk_none;
227 return op1_lvalue_kind;
230 /* Returns the kind of lvalue that REF is, in the sense of
231 [basic.lval]. This function should really be named lvalue_p; it
232 computes the C++ definition of lvalue. */
234 cp_lvalue_kind
235 real_lvalue_p (const_tree ref)
237 cp_lvalue_kind kind = lvalue_kind (ref);
238 if (kind & (clk_rvalueref|clk_class))
239 return clk_none;
240 else
241 return kind;
244 /* This differs from real_lvalue_p in that class rvalues are considered
245 lvalues. */
247 bool
248 lvalue_p (const_tree ref)
250 return (lvalue_kind (ref) != clk_none);
253 /* This differs from real_lvalue_p in that rvalues formed by dereferencing
254 rvalue references are considered rvalues. */
256 bool
257 lvalue_or_rvalue_with_address_p (const_tree ref)
259 cp_lvalue_kind kind = lvalue_kind (ref);
260 if (kind & clk_class)
261 return false;
262 else
263 return (kind != clk_none);
266 /* Test whether DECL is a builtin that may appear in a
267 constant-expression. */
269 bool
270 builtin_valid_in_constant_expr_p (const_tree decl)
272 /* At present BUILT_IN_CONSTANT_P is the only builtin we're allowing
273 in constant-expressions. We may want to add other builtins later. */
274 return DECL_IS_BUILTIN_CONSTANT_P (decl);
277 /* Build a TARGET_EXPR, initializing the DECL with the VALUE. */
279 static tree
280 build_target_expr (tree decl, tree value)
282 tree t;
284 #ifdef ENABLE_CHECKING
285 gcc_assert (VOID_TYPE_P (TREE_TYPE (value))
286 || TREE_TYPE (decl) == TREE_TYPE (value)
287 || useless_type_conversion_p (TREE_TYPE (decl),
288 TREE_TYPE (value)));
289 #endif
291 t = build4 (TARGET_EXPR, TREE_TYPE (decl), decl, value,
292 cxx_maybe_build_cleanup (decl), NULL_TREE);
293 /* We always set TREE_SIDE_EFFECTS so that expand_expr does not
294 ignore the TARGET_EXPR. If there really turn out to be no
295 side-effects, then the optimizer should be able to get rid of
296 whatever code is generated anyhow. */
297 TREE_SIDE_EFFECTS (t) = 1;
299 return t;
302 /* Return an undeclared local temporary of type TYPE for use in building a
303 TARGET_EXPR. */
305 static tree
306 build_local_temp (tree type)
308 tree slot = build_decl (input_location,
309 VAR_DECL, NULL_TREE, type);
310 DECL_ARTIFICIAL (slot) = 1;
311 DECL_IGNORED_P (slot) = 1;
312 DECL_CONTEXT (slot) = current_function_decl;
313 layout_decl (slot, 0);
314 return slot;
317 /* Set various status flags when building an AGGR_INIT_EXPR object T. */
319 static void
320 process_aggr_init_operands (tree t)
322 bool side_effects;
324 side_effects = TREE_SIDE_EFFECTS (t);
325 if (!side_effects)
327 int i, n;
328 n = TREE_OPERAND_LENGTH (t);
329 for (i = 1; i < n; i++)
331 tree op = TREE_OPERAND (t, i);
332 if (op && TREE_SIDE_EFFECTS (op))
334 side_effects = 1;
335 break;
339 TREE_SIDE_EFFECTS (t) = side_effects;
342 /* Build an AGGR_INIT_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE,
343 FN, and SLOT. NARGS is the number of call arguments which are specified
344 as a tree array ARGS. */
346 static tree
347 build_aggr_init_array (tree return_type, tree fn, tree slot, int nargs,
348 tree *args)
350 tree t;
351 int i;
353 t = build_vl_exp (AGGR_INIT_EXPR, nargs + 3);
354 TREE_TYPE (t) = return_type;
355 AGGR_INIT_EXPR_FN (t) = fn;
356 AGGR_INIT_EXPR_SLOT (t) = slot;
357 for (i = 0; i < nargs; i++)
358 AGGR_INIT_EXPR_ARG (t, i) = args[i];
359 process_aggr_init_operands (t);
360 return t;
363 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
364 target. TYPE is the type to be initialized.
366 Build an AGGR_INIT_EXPR to represent the initialization. This function
367 differs from build_cplus_new in that an AGGR_INIT_EXPR can only be used
368 to initialize another object, whereas a TARGET_EXPR can either
369 initialize another object or create its own temporary object, and as a
370 result building up a TARGET_EXPR requires that the type's destructor be
371 callable. */
373 tree
374 build_aggr_init_expr (tree type, tree init)
376 tree fn;
377 tree slot;
378 tree rval;
379 int is_ctor;
381 /* Make sure that we're not trying to create an instance of an
382 abstract class. */
383 abstract_virtuals_error (NULL_TREE, type);
385 if (TREE_CODE (init) == CALL_EXPR)
386 fn = CALL_EXPR_FN (init);
387 else if (TREE_CODE (init) == AGGR_INIT_EXPR)
388 fn = AGGR_INIT_EXPR_FN (init);
389 else
390 return convert (type, init);
392 is_ctor = (TREE_CODE (fn) == ADDR_EXPR
393 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
394 && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0)));
396 /* We split the CALL_EXPR into its function and its arguments here.
397 Then, in expand_expr, we put them back together. The reason for
398 this is that this expression might be a default argument
399 expression. In that case, we need a new temporary every time the
400 expression is used. That's what break_out_target_exprs does; it
401 replaces every AGGR_INIT_EXPR with a copy that uses a fresh
402 temporary slot. Then, expand_expr builds up a call-expression
403 using the new slot. */
405 /* If we don't need to use a constructor to create an object of this
406 type, don't mess with AGGR_INIT_EXPR. */
407 if (is_ctor || TREE_ADDRESSABLE (type))
409 slot = build_local_temp (type);
411 if (TREE_CODE(init) == CALL_EXPR)
412 rval = build_aggr_init_array (void_type_node, fn, slot,
413 call_expr_nargs (init),
414 CALL_EXPR_ARGP (init));
415 else
416 rval = build_aggr_init_array (void_type_node, fn, slot,
417 aggr_init_expr_nargs (init),
418 AGGR_INIT_EXPR_ARGP (init));
419 TREE_SIDE_EFFECTS (rval) = 1;
420 AGGR_INIT_VIA_CTOR_P (rval) = is_ctor;
421 TREE_NOTHROW (rval) = TREE_NOTHROW (init);
423 else
424 rval = init;
426 return rval;
429 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
430 target. TYPE is the type that this initialization should appear to
431 have.
433 Build an encapsulation of the initialization to perform
434 and return it so that it can be processed by language-independent
435 and language-specific expression expanders. */
437 tree
438 build_cplus_new (tree type, tree init)
440 tree rval = build_aggr_init_expr (type, init);
441 tree slot;
443 if (TREE_CODE (rval) == AGGR_INIT_EXPR)
444 slot = AGGR_INIT_EXPR_SLOT (rval);
445 else if (TREE_CODE (rval) == CALL_EXPR
446 || TREE_CODE (rval) == CONSTRUCTOR)
447 slot = build_local_temp (type);
448 else
449 return rval;
451 rval = build_target_expr (slot, rval);
452 TARGET_EXPR_IMPLICIT_P (rval) = 1;
454 return rval;
457 /* Return a TARGET_EXPR which expresses the initialization of an array to
458 be named later, either default-initialization or copy-initialization
459 from another array of the same type. */
461 tree
462 build_vec_init_expr (tree type, tree init)
464 tree slot;
465 tree inner_type = strip_array_types (type);
466 tree elt_init = integer_zero_node;
467 bool value_init = false;
469 /* Since we're deferring building the actual constructor calls until
470 gimplification time, we need to build one now and throw it away so
471 that the relevant constructor gets mark_used before cgraph decides
472 what functions are needed. Here we assume that init is either
473 NULL_TREE, void_type_node (indicating value-initialization), or
474 another array to copy. */
475 if (init == void_type_node)
477 elt_init = build_value_init (inner_type, tf_warning_or_error);
478 value_init = true;
479 init = NULL_TREE;
481 else
483 gcc_assert (init == NULL_TREE
484 || (same_type_ignoring_top_level_qualifiers_p
485 (type, TREE_TYPE (init))));
487 if (CLASS_TYPE_P (inner_type))
489 VEC(tree,gc) *argvec = make_tree_vector ();
490 if (init)
492 tree dummy = build_dummy_object (inner_type);
493 if (!real_lvalue_p (init))
494 dummy = move (dummy);
495 VEC_quick_push (tree, argvec, dummy);
497 elt_init
498 = build_special_member_call (NULL_TREE, complete_ctor_identifier,
499 &argvec, inner_type, LOOKUP_NORMAL,
500 tf_warning_or_error);
504 slot = build_local_temp (type);
505 init = build2 (VEC_INIT_EXPR, type, slot, init);
506 SET_EXPR_LOCATION (init, input_location);
508 if (current_function_decl
509 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
510 && potential_constant_expression (elt_init, tf_warning_or_error))
511 VEC_INIT_EXPR_IS_CONSTEXPR (init) = true;
512 VEC_INIT_EXPR_VALUE_INIT (init) = value_init;
514 init = build_target_expr (slot, init);
515 TARGET_EXPR_IMPLICIT_P (init) = 1;
517 return init;
520 tree
521 build_array_copy (tree init)
523 return build_vec_init_expr (TREE_TYPE (init), init);
526 /* Build a TARGET_EXPR using INIT to initialize a new temporary of the
527 indicated TYPE. */
529 tree
530 build_target_expr_with_type (tree init, tree type)
532 gcc_assert (!VOID_TYPE_P (type));
534 if (TREE_CODE (init) == TARGET_EXPR
535 || init == error_mark_node)
536 return init;
537 else if (CLASS_TYPE_P (type) && type_has_nontrivial_copy_init (type)
538 && !VOID_TYPE_P (TREE_TYPE (init))
539 && TREE_CODE (init) != COND_EXPR
540 && TREE_CODE (init) != CONSTRUCTOR
541 && TREE_CODE (init) != VA_ARG_EXPR)
542 /* We need to build up a copy constructor call. A void initializer
543 means we're being called from bot_manip. COND_EXPR is a special
544 case because we already have copies on the arms and we don't want
545 another one here. A CONSTRUCTOR is aggregate initialization, which
546 is handled separately. A VA_ARG_EXPR is magic creation of an
547 aggregate; there's no additional work to be done. */
548 return force_rvalue (init);
550 return force_target_expr (type, init);
553 /* Like the above function, but without the checking. This function should
554 only be used by code which is deliberately trying to subvert the type
555 system, such as call_builtin_trap. Or build_over_call, to avoid
556 infinite recursion. */
558 tree
559 force_target_expr (tree type, tree init)
561 tree slot;
563 gcc_assert (!VOID_TYPE_P (type));
565 slot = build_local_temp (type);
566 return build_target_expr (slot, init);
569 /* Like build_target_expr_with_type, but use the type of INIT. */
571 tree
572 get_target_expr (tree init)
574 if (TREE_CODE (init) == AGGR_INIT_EXPR)
575 return build_target_expr (AGGR_INIT_EXPR_SLOT (init), init);
576 else
577 return build_target_expr_with_type (init, TREE_TYPE (init));
580 /* If EXPR is a bitfield reference, convert it to the declared type of
581 the bitfield, and return the resulting expression. Otherwise,
582 return EXPR itself. */
584 tree
585 convert_bitfield_to_declared_type (tree expr)
587 tree bitfield_type;
589 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
590 if (bitfield_type)
591 expr = convert_to_integer (TYPE_MAIN_VARIANT (bitfield_type),
592 expr);
593 return expr;
596 /* EXPR is being used in an rvalue context. Return a version of EXPR
597 that is marked as an rvalue. */
599 tree
600 rvalue (tree expr)
602 tree type;
604 if (error_operand_p (expr))
605 return expr;
607 expr = mark_rvalue_use (expr);
609 /* [basic.lval]
611 Non-class rvalues always have cv-unqualified types. */
612 type = TREE_TYPE (expr);
613 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
614 type = cv_unqualified (type);
616 /* We need to do this for rvalue refs as well to get the right answer
617 from decltype; see c++/36628. */
618 if (!processing_template_decl && lvalue_or_rvalue_with_address_p (expr))
619 expr = build1 (NON_LVALUE_EXPR, type, expr);
620 else if (type != TREE_TYPE (expr))
621 expr = build_nop (type, expr);
623 return expr;
627 /* Hash an ARRAY_TYPE. K is really of type `tree'. */
629 static hashval_t
630 cplus_array_hash (const void* k)
632 hashval_t hash;
633 const_tree const t = (const_tree) k;
635 hash = TYPE_UID (TREE_TYPE (t));
636 if (TYPE_DOMAIN (t))
637 hash ^= TYPE_UID (TYPE_DOMAIN (t));
638 return hash;
641 typedef struct cplus_array_info {
642 tree type;
643 tree domain;
644 } cplus_array_info;
646 /* Compare two ARRAY_TYPEs. K1 is really of type `tree', K2 is really
647 of type `cplus_array_info*'. */
649 static int
650 cplus_array_compare (const void * k1, const void * k2)
652 const_tree const t1 = (const_tree) k1;
653 const cplus_array_info *const t2 = (const cplus_array_info*) k2;
655 return (TREE_TYPE (t1) == t2->type && TYPE_DOMAIN (t1) == t2->domain);
658 /* Hash table containing dependent array types, which are unsuitable for
659 the language-independent type hash table. */
660 static GTY ((param_is (union tree_node))) htab_t cplus_array_htab;
662 /* Like build_array_type, but handle special C++ semantics. */
664 tree
665 build_cplus_array_type (tree elt_type, tree index_type)
667 tree t;
669 if (elt_type == error_mark_node || index_type == error_mark_node)
670 return error_mark_node;
672 if (processing_template_decl
673 && (dependent_type_p (elt_type)
674 || (index_type && !TREE_CONSTANT (TYPE_MAX_VALUE (index_type)))))
676 void **e;
677 cplus_array_info cai;
678 hashval_t hash;
680 if (cplus_array_htab == NULL)
681 cplus_array_htab = htab_create_ggc (61, &cplus_array_hash,
682 &cplus_array_compare, NULL);
684 hash = TYPE_UID (elt_type);
685 if (index_type)
686 hash ^= TYPE_UID (index_type);
687 cai.type = elt_type;
688 cai.domain = index_type;
690 e = htab_find_slot_with_hash (cplus_array_htab, &cai, hash, INSERT);
691 if (*e)
692 /* We have found the type: we're done. */
693 return (tree) *e;
694 else
696 /* Build a new array type. */
697 t = cxx_make_type (ARRAY_TYPE);
698 TREE_TYPE (t) = elt_type;
699 TYPE_DOMAIN (t) = index_type;
701 /* Store it in the hash table. */
702 *e = t;
704 /* Set the canonical type for this new node. */
705 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
706 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
707 SET_TYPE_STRUCTURAL_EQUALITY (t);
708 else if (TYPE_CANONICAL (elt_type) != elt_type
709 || (index_type
710 && TYPE_CANONICAL (index_type) != index_type))
711 TYPE_CANONICAL (t)
712 = build_cplus_array_type
713 (TYPE_CANONICAL (elt_type),
714 index_type ? TYPE_CANONICAL (index_type) : index_type);
715 else
716 TYPE_CANONICAL (t) = t;
719 else
720 t = build_array_type (elt_type, index_type);
722 /* We want TYPE_MAIN_VARIANT of an array to strip cv-quals from the
723 element type as well, so fix it up if needed. */
724 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
726 tree m = build_cplus_array_type (TYPE_MAIN_VARIANT (elt_type),
727 index_type);
728 if (TYPE_MAIN_VARIANT (t) != m)
730 TYPE_MAIN_VARIANT (t) = m;
731 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
732 TYPE_NEXT_VARIANT (m) = t;
736 /* Push these needs up so that initialization takes place
737 more easily. */
738 TYPE_NEEDS_CONSTRUCTING (t)
739 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (elt_type));
740 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
741 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (elt_type));
742 return t;
745 /* Return an ARRAY_TYPE with element type ELT and length N. */
747 tree
748 build_array_of_n_type (tree elt, int n)
750 return build_cplus_array_type (elt, build_index_type (size_int (n - 1)));
753 /* Return a reference type node referring to TO_TYPE. If RVAL is
754 true, return an rvalue reference type, otherwise return an lvalue
755 reference type. If a type node exists, reuse it, otherwise create
756 a new one. */
757 tree
758 cp_build_reference_type (tree to_type, bool rval)
760 tree lvalue_ref, t;
761 lvalue_ref = build_reference_type (to_type);
762 if (!rval)
763 return lvalue_ref;
765 /* This code to create rvalue reference types is based on and tied
766 to the code creating lvalue reference types in the middle-end
767 functions build_reference_type_for_mode and build_reference_type.
769 It works by putting the rvalue reference type nodes after the
770 lvalue reference nodes in the TYPE_NEXT_REF_TO linked list, so
771 they will effectively be ignored by the middle end. */
773 for (t = lvalue_ref; (t = TYPE_NEXT_REF_TO (t)); )
774 if (TYPE_REF_IS_RVALUE (t))
775 return t;
777 t = build_distinct_type_copy (lvalue_ref);
779 TYPE_REF_IS_RVALUE (t) = true;
780 TYPE_NEXT_REF_TO (t) = TYPE_NEXT_REF_TO (lvalue_ref);
781 TYPE_NEXT_REF_TO (lvalue_ref) = t;
783 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
784 SET_TYPE_STRUCTURAL_EQUALITY (t);
785 else if (TYPE_CANONICAL (to_type) != to_type)
786 TYPE_CANONICAL (t)
787 = cp_build_reference_type (TYPE_CANONICAL (to_type), rval);
788 else
789 TYPE_CANONICAL (t) = t;
791 layout_type (t);
793 return t;
797 /* Returns EXPR cast to rvalue reference type, like std::move. */
799 tree
800 move (tree expr)
802 tree type = TREE_TYPE (expr);
803 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
804 type = cp_build_reference_type (type, /*rval*/true);
805 return build_static_cast (type, expr, tf_warning_or_error);
808 /* Used by the C++ front end to build qualified array types. However,
809 the C version of this function does not properly maintain canonical
810 types (which are not used in C). */
811 tree
812 c_build_qualified_type (tree type, int type_quals)
814 return cp_build_qualified_type (type, type_quals);
818 /* Make a variant of TYPE, qualified with the TYPE_QUALS. Handles
819 arrays correctly. In particular, if TYPE is an array of T's, and
820 TYPE_QUALS is non-empty, returns an array of qualified T's.
822 FLAGS determines how to deal with ill-formed qualifications. If
823 tf_ignore_bad_quals is set, then bad qualifications are dropped
824 (this is permitted if TYPE was introduced via a typedef or template
825 type parameter). If bad qualifications are dropped and tf_warning
826 is set, then a warning is issued for non-const qualifications. If
827 tf_ignore_bad_quals is not set and tf_error is not set, we
828 return error_mark_node. Otherwise, we issue an error, and ignore
829 the qualifications.
831 Qualification of a reference type is valid when the reference came
832 via a typedef or template type argument. [dcl.ref] No such
833 dispensation is provided for qualifying a function type. [dcl.fct]
834 DR 295 queries this and the proposed resolution brings it into line
835 with qualifying a reference. We implement the DR. We also behave
836 in a similar manner for restricting non-pointer types. */
838 tree
839 cp_build_qualified_type_real (tree type,
840 int type_quals,
841 tsubst_flags_t complain)
843 tree result;
844 int bad_quals = TYPE_UNQUALIFIED;
846 if (type == error_mark_node)
847 return type;
849 if (type_quals == cp_type_quals (type))
850 return type;
852 if (TREE_CODE (type) == ARRAY_TYPE)
854 /* In C++, the qualification really applies to the array element
855 type. Obtain the appropriately qualified element type. */
856 tree t;
857 tree element_type
858 = cp_build_qualified_type_real (TREE_TYPE (type),
859 type_quals,
860 complain);
862 if (element_type == error_mark_node)
863 return error_mark_node;
865 /* See if we already have an identically qualified type. Tests
866 should be equivalent to those in check_qualified_type. */
867 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
868 if (TREE_TYPE (t) == element_type
869 && TYPE_NAME (t) == TYPE_NAME (type)
870 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
871 && attribute_list_equal (TYPE_ATTRIBUTES (t),
872 TYPE_ATTRIBUTES (type)))
873 break;
875 if (!t)
877 t = build_cplus_array_type (element_type, TYPE_DOMAIN (type));
879 /* Keep the typedef name. */
880 if (TYPE_NAME (t) != TYPE_NAME (type))
882 t = build_variant_type_copy (t);
883 TYPE_NAME (t) = TYPE_NAME (type);
887 /* Even if we already had this variant, we update
888 TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case
889 they changed since the variant was originally created.
891 This seems hokey; if there is some way to use a previous
892 variant *without* coming through here,
893 TYPE_NEEDS_CONSTRUCTING will never be updated. */
894 TYPE_NEEDS_CONSTRUCTING (t)
895 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
896 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
897 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
898 return t;
900 else if (TYPE_PTRMEMFUNC_P (type))
902 /* For a pointer-to-member type, we can't just return a
903 cv-qualified version of the RECORD_TYPE. If we do, we
904 haven't changed the field that contains the actual pointer to
905 a method, and so TYPE_PTRMEMFUNC_FN_TYPE will be wrong. */
906 tree t;
908 t = TYPE_PTRMEMFUNC_FN_TYPE (type);
909 t = cp_build_qualified_type_real (t, type_quals, complain);
910 return build_ptrmemfunc_type (t);
912 else if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
914 tree t = PACK_EXPANSION_PATTERN (type);
916 t = cp_build_qualified_type_real (t, type_quals, complain);
917 return make_pack_expansion (t);
920 /* A reference or method type shall not be cv-qualified.
921 [dcl.ref], [dcl.fct]. This used to be an error, but as of DR 295
922 (in CD1) we always ignore extra cv-quals on functions. */
923 if (type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)
924 && (TREE_CODE (type) == REFERENCE_TYPE
925 || TREE_CODE (type) == FUNCTION_TYPE
926 || TREE_CODE (type) == METHOD_TYPE))
928 if (TREE_CODE (type) == REFERENCE_TYPE)
929 bad_quals |= type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
930 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
933 /* But preserve any function-cv-quals on a FUNCTION_TYPE. */
934 if (TREE_CODE (type) == FUNCTION_TYPE)
935 type_quals |= type_memfn_quals (type);
937 /* A restrict-qualified type must be a pointer (or reference)
938 to object or incomplete type. */
939 if ((type_quals & TYPE_QUAL_RESTRICT)
940 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
941 && TREE_CODE (type) != TYPENAME_TYPE
942 && !POINTER_TYPE_P (type))
944 bad_quals |= TYPE_QUAL_RESTRICT;
945 type_quals &= ~TYPE_QUAL_RESTRICT;
948 if (bad_quals == TYPE_UNQUALIFIED
949 || (complain & tf_ignore_bad_quals))
950 /*OK*/;
951 else if (!(complain & tf_error))
952 return error_mark_node;
953 else
955 tree bad_type = build_qualified_type (ptr_type_node, bad_quals);
956 error ("%qV qualifiers cannot be applied to %qT",
957 bad_type, type);
960 /* Retrieve (or create) the appropriately qualified variant. */
961 result = build_qualified_type (type, type_quals);
963 /* If this was a pointer-to-method type, and we just made a copy,
964 then we need to unshare the record that holds the cached
965 pointer-to-member-function type, because these will be distinct
966 between the unqualified and qualified types. */
967 if (result != type
968 && TREE_CODE (type) == POINTER_TYPE
969 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE
970 && TYPE_LANG_SPECIFIC (result) == TYPE_LANG_SPECIFIC (type))
971 TYPE_LANG_SPECIFIC (result) = NULL;
973 /* We may also have ended up building a new copy of the canonical
974 type of a pointer-to-method type, which could have the same
975 sharing problem described above. */
976 if (TYPE_CANONICAL (result) != TYPE_CANONICAL (type)
977 && TREE_CODE (type) == POINTER_TYPE
978 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE
979 && (TYPE_LANG_SPECIFIC (TYPE_CANONICAL (result))
980 == TYPE_LANG_SPECIFIC (TYPE_CANONICAL (type))))
981 TYPE_LANG_SPECIFIC (TYPE_CANONICAL (result)) = NULL;
983 return result;
986 /* Return TYPE with const and volatile removed. */
988 tree
989 cv_unqualified (tree type)
991 int quals;
993 if (type == error_mark_node)
994 return type;
996 quals = cp_type_quals (type);
997 quals &= ~(TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE);
998 return cp_build_qualified_type (type, quals);
1001 /* Builds a qualified variant of T that is not a typedef variant.
1002 E.g. consider the following declarations:
1003 typedef const int ConstInt;
1004 typedef ConstInt* PtrConstInt;
1005 If T is PtrConstInt, this function returns a type representing
1006 const int*.
1007 In other words, if T is a typedef, the function returns the underlying type.
1008 The cv-qualification and attributes of the type returned match the
1009 input type.
1010 They will always be compatible types.
1011 The returned type is built so that all of its subtypes
1012 recursively have their typedefs stripped as well.
1014 This is different from just returning TYPE_CANONICAL (T)
1015 Because of several reasons:
1016 * If T is a type that needs structural equality
1017 its TYPE_CANONICAL (T) will be NULL.
1018 * TYPE_CANONICAL (T) desn't carry type attributes
1019 and looses template parameter names. */
1021 tree
1022 strip_typedefs (tree t)
1024 tree result = NULL, type = NULL, t0 = NULL;
1026 if (!t || t == error_mark_node || t == TYPE_CANONICAL (t))
1027 return t;
1029 gcc_assert (TYPE_P (t));
1031 switch (TREE_CODE (t))
1033 case POINTER_TYPE:
1034 type = strip_typedefs (TREE_TYPE (t));
1035 result = build_pointer_type (type);
1036 break;
1037 case REFERENCE_TYPE:
1038 type = strip_typedefs (TREE_TYPE (t));
1039 result = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
1040 break;
1041 case OFFSET_TYPE:
1042 t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t));
1043 type = strip_typedefs (TREE_TYPE (t));
1044 result = build_offset_type (t0, type);
1045 break;
1046 case RECORD_TYPE:
1047 if (TYPE_PTRMEMFUNC_P (t))
1049 t0 = strip_typedefs (TYPE_PTRMEMFUNC_FN_TYPE (t));
1050 result = build_ptrmemfunc_type (t0);
1052 break;
1053 case ARRAY_TYPE:
1054 type = strip_typedefs (TREE_TYPE (t));
1055 t0 = strip_typedefs (TYPE_DOMAIN (t));;
1056 result = build_cplus_array_type (type, t0);
1057 break;
1058 case FUNCTION_TYPE:
1059 case METHOD_TYPE:
1061 tree arg_types = NULL, arg_node, arg_type;
1062 for (arg_node = TYPE_ARG_TYPES (t);
1063 arg_node;
1064 arg_node = TREE_CHAIN (arg_node))
1066 if (arg_node == void_list_node)
1067 break;
1068 arg_type = strip_typedefs (TREE_VALUE (arg_node));
1069 gcc_assert (arg_type);
1071 arg_types =
1072 tree_cons (TREE_PURPOSE (arg_node), arg_type, arg_types);
1075 if (arg_types)
1076 arg_types = nreverse (arg_types);
1078 /* A list of parameters not ending with an ellipsis
1079 must end with void_list_node. */
1080 if (arg_node)
1081 arg_types = chainon (arg_types, void_list_node);
1083 type = strip_typedefs (TREE_TYPE (t));
1084 if (TREE_CODE (t) == METHOD_TYPE)
1086 tree class_type = TREE_TYPE (TREE_VALUE (arg_types));
1087 gcc_assert (class_type);
1088 result =
1089 build_method_type_directly (class_type, type,
1090 TREE_CHAIN (arg_types));
1092 else
1094 result = build_function_type (type,
1095 arg_types);
1096 result = apply_memfn_quals (result, type_memfn_quals (t));
1099 if (TYPE_RAISES_EXCEPTIONS (t))
1100 result = build_exception_variant (result,
1101 TYPE_RAISES_EXCEPTIONS (t));
1103 break;
1104 case TYPENAME_TYPE:
1105 result = make_typename_type (strip_typedefs (TYPE_CONTEXT (t)),
1106 TYPENAME_TYPE_FULLNAME (t),
1107 typename_type, tf_none);
1108 break;
1109 default:
1110 break;
1113 if (!result)
1114 result = TYPE_MAIN_VARIANT (t);
1115 if (TYPE_ATTRIBUTES (t))
1116 result = cp_build_type_attribute_variant (result, TYPE_ATTRIBUTES (t));
1117 return cp_build_qualified_type (result, cp_type_quals (t));
1120 /* Makes a copy of BINFO and TYPE, which is to be inherited into a
1121 graph dominated by T. If BINFO is NULL, TYPE is a dependent base,
1122 and we do a shallow copy. If BINFO is non-NULL, we do a deep copy.
1123 VIRT indicates whether TYPE is inherited virtually or not.
1124 IGO_PREV points at the previous binfo of the inheritance graph
1125 order chain. The newly copied binfo's TREE_CHAIN forms this
1126 ordering.
1128 The CLASSTYPE_VBASECLASSES vector of T is constructed in the
1129 correct order. That is in the order the bases themselves should be
1130 constructed in.
1132 The BINFO_INHERITANCE of a virtual base class points to the binfo
1133 of the most derived type. ??? We could probably change this so that
1134 BINFO_INHERITANCE becomes synonymous with BINFO_PRIMARY, and hence
1135 remove a field. They currently can only differ for primary virtual
1136 virtual bases. */
1138 tree
1139 copy_binfo (tree binfo, tree type, tree t, tree *igo_prev, int virt)
1141 tree new_binfo;
1143 if (virt)
1145 /* See if we've already made this virtual base. */
1146 new_binfo = binfo_for_vbase (type, t);
1147 if (new_binfo)
1148 return new_binfo;
1151 new_binfo = make_tree_binfo (binfo ? BINFO_N_BASE_BINFOS (binfo) : 0);
1152 BINFO_TYPE (new_binfo) = type;
1154 /* Chain it into the inheritance graph. */
1155 TREE_CHAIN (*igo_prev) = new_binfo;
1156 *igo_prev = new_binfo;
1158 if (binfo)
1160 int ix;
1161 tree base_binfo;
1163 gcc_assert (!BINFO_DEPENDENT_BASE_P (binfo));
1164 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), type));
1166 BINFO_OFFSET (new_binfo) = BINFO_OFFSET (binfo);
1167 BINFO_VIRTUALS (new_binfo) = BINFO_VIRTUALS (binfo);
1169 /* We do not need to copy the accesses, as they are read only. */
1170 BINFO_BASE_ACCESSES (new_binfo) = BINFO_BASE_ACCESSES (binfo);
1172 /* Recursively copy base binfos of BINFO. */
1173 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1175 tree new_base_binfo;
1177 gcc_assert (!BINFO_DEPENDENT_BASE_P (base_binfo));
1178 new_base_binfo = copy_binfo (base_binfo, BINFO_TYPE (base_binfo),
1179 t, igo_prev,
1180 BINFO_VIRTUAL_P (base_binfo));
1182 if (!BINFO_INHERITANCE_CHAIN (new_base_binfo))
1183 BINFO_INHERITANCE_CHAIN (new_base_binfo) = new_binfo;
1184 BINFO_BASE_APPEND (new_binfo, new_base_binfo);
1187 else
1188 BINFO_DEPENDENT_BASE_P (new_binfo) = 1;
1190 if (virt)
1192 /* Push it onto the list after any virtual bases it contains
1193 will have been pushed. */
1194 VEC_quick_push (tree, CLASSTYPE_VBASECLASSES (t), new_binfo);
1195 BINFO_VIRTUAL_P (new_binfo) = 1;
1196 BINFO_INHERITANCE_CHAIN (new_binfo) = TYPE_BINFO (t);
1199 return new_binfo;
1202 /* Hashing of lists so that we don't make duplicates.
1203 The entry point is `list_hash_canon'. */
1205 /* Now here is the hash table. When recording a list, it is added
1206 to the slot whose index is the hash code mod the table size.
1207 Note that the hash table is used for several kinds of lists.
1208 While all these live in the same table, they are completely independent,
1209 and the hash code is computed differently for each of these. */
1211 static GTY ((param_is (union tree_node))) htab_t list_hash_table;
1213 struct list_proxy
1215 tree purpose;
1216 tree value;
1217 tree chain;
1220 /* Compare ENTRY (an entry in the hash table) with DATA (a list_proxy
1221 for a node we are thinking about adding). */
1223 static int
1224 list_hash_eq (const void* entry, const void* data)
1226 const_tree const t = (const_tree) entry;
1227 const struct list_proxy *const proxy = (const struct list_proxy *) data;
1229 return (TREE_VALUE (t) == proxy->value
1230 && TREE_PURPOSE (t) == proxy->purpose
1231 && TREE_CHAIN (t) == proxy->chain);
1234 /* Compute a hash code for a list (chain of TREE_LIST nodes
1235 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
1236 TREE_COMMON slots), by adding the hash codes of the individual entries. */
1238 static hashval_t
1239 list_hash_pieces (tree purpose, tree value, tree chain)
1241 hashval_t hashcode = 0;
1243 if (chain)
1244 hashcode += TREE_HASH (chain);
1246 if (value)
1247 hashcode += TREE_HASH (value);
1248 else
1249 hashcode += 1007;
1250 if (purpose)
1251 hashcode += TREE_HASH (purpose);
1252 else
1253 hashcode += 1009;
1254 return hashcode;
1257 /* Hash an already existing TREE_LIST. */
1259 static hashval_t
1260 list_hash (const void* p)
1262 const_tree const t = (const_tree) p;
1263 return list_hash_pieces (TREE_PURPOSE (t),
1264 TREE_VALUE (t),
1265 TREE_CHAIN (t));
1268 /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
1269 object for an identical list if one already exists. Otherwise, build a
1270 new one, and record it as the canonical object. */
1272 tree
1273 hash_tree_cons (tree purpose, tree value, tree chain)
1275 int hashcode = 0;
1276 void **slot;
1277 struct list_proxy proxy;
1279 /* Hash the list node. */
1280 hashcode = list_hash_pieces (purpose, value, chain);
1281 /* Create a proxy for the TREE_LIST we would like to create. We
1282 don't actually create it so as to avoid creating garbage. */
1283 proxy.purpose = purpose;
1284 proxy.value = value;
1285 proxy.chain = chain;
1286 /* See if it is already in the table. */
1287 slot = htab_find_slot_with_hash (list_hash_table, &proxy, hashcode,
1288 INSERT);
1289 /* If not, create a new node. */
1290 if (!*slot)
1291 *slot = tree_cons (purpose, value, chain);
1292 return (tree) *slot;
1295 /* Constructor for hashed lists. */
1297 tree
1298 hash_tree_chain (tree value, tree chain)
1300 return hash_tree_cons (NULL_TREE, value, chain);
1303 void
1304 debug_binfo (tree elem)
1306 HOST_WIDE_INT n;
1307 tree virtuals;
1309 fprintf (stderr, "type \"%s\", offset = " HOST_WIDE_INT_PRINT_DEC
1310 "\nvtable type:\n",
1311 TYPE_NAME_STRING (BINFO_TYPE (elem)),
1312 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
1313 debug_tree (BINFO_TYPE (elem));
1314 if (BINFO_VTABLE (elem))
1315 fprintf (stderr, "vtable decl \"%s\"\n",
1316 IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem))));
1317 else
1318 fprintf (stderr, "no vtable decl yet\n");
1319 fprintf (stderr, "virtuals:\n");
1320 virtuals = BINFO_VIRTUALS (elem);
1321 n = 0;
1323 while (virtuals)
1325 tree fndecl = TREE_VALUE (virtuals);
1326 fprintf (stderr, "%s [%ld =? %ld]\n",
1327 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
1328 (long) n, (long) TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
1329 ++n;
1330 virtuals = TREE_CHAIN (virtuals);
1334 /* Build a representation for the qualified name SCOPE::NAME. TYPE is
1335 the type of the result expression, if known, or NULL_TREE if the
1336 resulting expression is type-dependent. If TEMPLATE_P is true,
1337 NAME is known to be a template because the user explicitly used the
1338 "template" keyword after the "::".
1340 All SCOPE_REFs should be built by use of this function. */
1342 tree
1343 build_qualified_name (tree type, tree scope, tree name, bool template_p)
1345 tree t;
1346 if (type == error_mark_node
1347 || scope == error_mark_node
1348 || name == error_mark_node)
1349 return error_mark_node;
1350 t = build2 (SCOPE_REF, type, scope, name);
1351 QUALIFIED_NAME_IS_TEMPLATE (t) = template_p;
1352 if (type)
1353 t = convert_from_reference (t);
1354 return t;
1357 /* Returns nonzero if X is an expression for a (possibly overloaded)
1358 function. If "f" is a function or function template, "f", "c->f",
1359 "c.f", "C::f", and "f<int>" will all be considered possibly
1360 overloaded functions. Returns 2 if the function is actually
1361 overloaded, i.e., if it is impossible to know the type of the
1362 function without performing overload resolution. */
1365 is_overloaded_fn (tree x)
1367 /* A baselink is also considered an overloaded function. */
1368 if (TREE_CODE (x) == OFFSET_REF
1369 || TREE_CODE (x) == COMPONENT_REF)
1370 x = TREE_OPERAND (x, 1);
1371 if (BASELINK_P (x))
1372 x = BASELINK_FUNCTIONS (x);
1373 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
1374 x = TREE_OPERAND (x, 0);
1375 if (DECL_FUNCTION_TEMPLATE_P (OVL_CURRENT (x))
1376 || (TREE_CODE (x) == OVERLOAD && OVL_CHAIN (x)))
1377 return 2;
1378 return (TREE_CODE (x) == FUNCTION_DECL
1379 || TREE_CODE (x) == OVERLOAD);
1382 /* Returns true iff X is an expression for an overloaded function
1383 whose type cannot be known without performing overload
1384 resolution. */
1386 bool
1387 really_overloaded_fn (tree x)
1389 return is_overloaded_fn (x) == 2;
1392 tree
1393 get_fns (tree from)
1395 gcc_assert (is_overloaded_fn (from));
1396 /* A baselink is also considered an overloaded function. */
1397 if (TREE_CODE (from) == OFFSET_REF
1398 || TREE_CODE (from) == COMPONENT_REF)
1399 from = TREE_OPERAND (from, 1);
1400 if (BASELINK_P (from))
1401 from = BASELINK_FUNCTIONS (from);
1402 if (TREE_CODE (from) == TEMPLATE_ID_EXPR)
1403 from = TREE_OPERAND (from, 0);
1404 return from;
1407 tree
1408 get_first_fn (tree from)
1410 return OVL_CURRENT (get_fns (from));
1413 /* Return a new OVL node, concatenating it with the old one. */
1415 tree
1416 ovl_cons (tree decl, tree chain)
1418 tree result = make_node (OVERLOAD);
1419 TREE_TYPE (result) = unknown_type_node;
1420 OVL_FUNCTION (result) = decl;
1421 TREE_CHAIN (result) = chain;
1423 return result;
1426 /* Build a new overloaded function. If this is the first one,
1427 just return it; otherwise, ovl_cons the _DECLs */
1429 tree
1430 build_overload (tree decl, tree chain)
1432 if (! chain && TREE_CODE (decl) != TEMPLATE_DECL)
1433 return decl;
1434 if (chain && TREE_CODE (chain) != OVERLOAD)
1435 chain = ovl_cons (chain, NULL_TREE);
1436 return ovl_cons (decl, chain);
1440 #define PRINT_RING_SIZE 4
1442 static const char *
1443 cxx_printable_name_internal (tree decl, int v, bool translate)
1445 static unsigned int uid_ring[PRINT_RING_SIZE];
1446 static char *print_ring[PRINT_RING_SIZE];
1447 static bool trans_ring[PRINT_RING_SIZE];
1448 static int ring_counter;
1449 int i;
1451 /* Only cache functions. */
1452 if (v < 2
1453 || TREE_CODE (decl) != FUNCTION_DECL
1454 || DECL_LANG_SPECIFIC (decl) == 0)
1455 return lang_decl_name (decl, v, translate);
1457 /* See if this print name is lying around. */
1458 for (i = 0; i < PRINT_RING_SIZE; i++)
1459 if (uid_ring[i] == DECL_UID (decl) && translate == trans_ring[i])
1460 /* yes, so return it. */
1461 return print_ring[i];
1463 if (++ring_counter == PRINT_RING_SIZE)
1464 ring_counter = 0;
1466 if (current_function_decl != NULL_TREE)
1468 /* There may be both translated and untranslated versions of the
1469 name cached. */
1470 for (i = 0; i < 2; i++)
1472 if (uid_ring[ring_counter] == DECL_UID (current_function_decl))
1473 ring_counter += 1;
1474 if (ring_counter == PRINT_RING_SIZE)
1475 ring_counter = 0;
1477 gcc_assert (uid_ring[ring_counter] != DECL_UID (current_function_decl));
1480 if (print_ring[ring_counter])
1481 free (print_ring[ring_counter]);
1483 print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v, translate));
1484 uid_ring[ring_counter] = DECL_UID (decl);
1485 trans_ring[ring_counter] = translate;
1486 return print_ring[ring_counter];
1489 const char *
1490 cxx_printable_name (tree decl, int v)
1492 return cxx_printable_name_internal (decl, v, false);
1495 const char *
1496 cxx_printable_name_translate (tree decl, int v)
1498 return cxx_printable_name_internal (decl, v, true);
1501 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
1502 listed in RAISES. */
1504 tree
1505 build_exception_variant (tree type, tree raises)
1507 tree v;
1508 int type_quals;
1510 if (comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (type), ce_exact))
1511 return type;
1513 type_quals = TYPE_QUALS (type);
1514 for (v = TYPE_MAIN_VARIANT (type); v; v = TYPE_NEXT_VARIANT (v))
1515 if (check_qualified_type (v, type, type_quals)
1516 && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (v), ce_exact))
1517 return v;
1519 /* Need to build a new variant. */
1520 v = build_variant_type_copy (type);
1521 TYPE_RAISES_EXCEPTIONS (v) = raises;
1522 return v;
1525 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new
1526 BOUND_TEMPLATE_TEMPLATE_PARM bound with NEWARGS as its template
1527 arguments. */
1529 tree
1530 bind_template_template_parm (tree t, tree newargs)
1532 tree decl = TYPE_NAME (t);
1533 tree t2;
1535 t2 = cxx_make_type (BOUND_TEMPLATE_TEMPLATE_PARM);
1536 decl = build_decl (input_location,
1537 TYPE_DECL, DECL_NAME (decl), NULL_TREE);
1539 /* These nodes have to be created to reflect new TYPE_DECL and template
1540 arguments. */
1541 TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t));
1542 TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl;
1543 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
1544 = build_template_info (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t), newargs);
1546 TREE_TYPE (decl) = t2;
1547 TYPE_NAME (t2) = decl;
1548 TYPE_STUB_DECL (t2) = decl;
1549 TYPE_SIZE (t2) = 0;
1550 SET_TYPE_STRUCTURAL_EQUALITY (t2);
1552 return t2;
1555 /* Called from count_trees via walk_tree. */
1557 static tree
1558 count_trees_r (tree *tp, int *walk_subtrees, void *data)
1560 ++*((int *) data);
1562 if (TYPE_P (*tp))
1563 *walk_subtrees = 0;
1565 return NULL_TREE;
1568 /* Debugging function for measuring the rough complexity of a tree
1569 representation. */
1572 count_trees (tree t)
1574 int n_trees = 0;
1575 cp_walk_tree_without_duplicates (&t, count_trees_r, &n_trees);
1576 return n_trees;
1579 /* Called from verify_stmt_tree via walk_tree. */
1581 static tree
1582 verify_stmt_tree_r (tree* tp,
1583 int* walk_subtrees ATTRIBUTE_UNUSED ,
1584 void* data)
1586 tree t = *tp;
1587 htab_t *statements = (htab_t *) data;
1588 void **slot;
1590 if (!STATEMENT_CODE_P (TREE_CODE (t)))
1591 return NULL_TREE;
1593 /* If this statement is already present in the hash table, then
1594 there is a circularity in the statement tree. */
1595 gcc_assert (!htab_find (*statements, t));
1597 slot = htab_find_slot (*statements, t, INSERT);
1598 *slot = t;
1600 return NULL_TREE;
1603 /* Debugging function to check that the statement T has not been
1604 corrupted. For now, this function simply checks that T contains no
1605 circularities. */
1607 void
1608 verify_stmt_tree (tree t)
1610 htab_t statements;
1611 statements = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
1612 cp_walk_tree (&t, verify_stmt_tree_r, &statements, NULL);
1613 htab_delete (statements);
1616 /* Check if the type T depends on a type with no linkage and if so, return
1617 it. If RELAXED_P then do not consider a class type declared within
1618 a vague-linkage function to have no linkage. */
1620 tree
1621 no_linkage_check (tree t, bool relaxed_p)
1623 tree r;
1625 /* There's no point in checking linkage on template functions; we
1626 can't know their complete types. */
1627 if (processing_template_decl)
1628 return NULL_TREE;
1630 switch (TREE_CODE (t))
1632 case RECORD_TYPE:
1633 if (TYPE_PTRMEMFUNC_P (t))
1634 goto ptrmem;
1635 /* Lambda types that don't have mangling scope have no linkage. We
1636 check CLASSTYPE_LAMBDA_EXPR here rather than LAMBDA_TYPE_P because
1637 when we get here from pushtag none of the lambda information is
1638 set up yet, so we want to assume that the lambda has linkage and
1639 fix it up later if not. */
1640 if (CLASSTYPE_LAMBDA_EXPR (t)
1641 && LAMBDA_TYPE_EXTRA_SCOPE (t) == NULL_TREE)
1642 return t;
1643 /* Fall through. */
1644 case UNION_TYPE:
1645 if (!CLASS_TYPE_P (t))
1646 return NULL_TREE;
1647 /* Fall through. */
1648 case ENUMERAL_TYPE:
1649 /* Only treat anonymous types as having no linkage if they're at
1650 namespace scope. This is core issue 966. */
1651 if (TYPE_ANONYMOUS_P (t) && TYPE_NAMESPACE_SCOPE_P (t))
1652 return t;
1654 for (r = CP_TYPE_CONTEXT (t); ; )
1656 /* If we're a nested type of a !TREE_PUBLIC class, we might not
1657 have linkage, or we might just be in an anonymous namespace.
1658 If we're in a TREE_PUBLIC class, we have linkage. */
1659 if (TYPE_P (r) && !TREE_PUBLIC (TYPE_NAME (r)))
1660 return no_linkage_check (TYPE_CONTEXT (t), relaxed_p);
1661 else if (TREE_CODE (r) == FUNCTION_DECL)
1663 if (!relaxed_p || !vague_linkage_p (r))
1664 return t;
1665 else
1666 r = CP_DECL_CONTEXT (r);
1668 else
1669 break;
1672 return NULL_TREE;
1674 case ARRAY_TYPE:
1675 case POINTER_TYPE:
1676 case REFERENCE_TYPE:
1677 return no_linkage_check (TREE_TYPE (t), relaxed_p);
1679 case OFFSET_TYPE:
1680 ptrmem:
1681 r = no_linkage_check (TYPE_PTRMEM_POINTED_TO_TYPE (t),
1682 relaxed_p);
1683 if (r)
1684 return r;
1685 return no_linkage_check (TYPE_PTRMEM_CLASS_TYPE (t), relaxed_p);
1687 case METHOD_TYPE:
1688 r = no_linkage_check (TYPE_METHOD_BASETYPE (t), relaxed_p);
1689 if (r)
1690 return r;
1691 /* Fall through. */
1692 case FUNCTION_TYPE:
1694 tree parm;
1695 for (parm = TYPE_ARG_TYPES (t);
1696 parm && parm != void_list_node;
1697 parm = TREE_CHAIN (parm))
1699 r = no_linkage_check (TREE_VALUE (parm), relaxed_p);
1700 if (r)
1701 return r;
1703 return no_linkage_check (TREE_TYPE (t), relaxed_p);
1706 default:
1707 return NULL_TREE;
1711 #ifdef GATHER_STATISTICS
1712 extern int depth_reached;
1713 #endif
1715 void
1716 cxx_print_statistics (void)
1718 print_search_statistics ();
1719 print_class_statistics ();
1720 print_template_statistics ();
1721 #ifdef GATHER_STATISTICS
1722 fprintf (stderr, "maximum template instantiation depth reached: %d\n",
1723 depth_reached);
1724 #endif
1727 /* Return, as an INTEGER_CST node, the number of elements for TYPE
1728 (which is an ARRAY_TYPE). This counts only elements of the top
1729 array. */
1731 tree
1732 array_type_nelts_top (tree type)
1734 return fold_build2_loc (input_location,
1735 PLUS_EXPR, sizetype,
1736 array_type_nelts (type),
1737 size_one_node);
1740 /* Return, as an INTEGER_CST node, the number of elements for TYPE
1741 (which is an ARRAY_TYPE). This one is a recursive count of all
1742 ARRAY_TYPEs that are clumped together. */
1744 tree
1745 array_type_nelts_total (tree type)
1747 tree sz = array_type_nelts_top (type);
1748 type = TREE_TYPE (type);
1749 while (TREE_CODE (type) == ARRAY_TYPE)
1751 tree n = array_type_nelts_top (type);
1752 sz = fold_build2_loc (input_location,
1753 MULT_EXPR, sizetype, sz, n);
1754 type = TREE_TYPE (type);
1756 return sz;
1759 /* Called from break_out_target_exprs via mapcar. */
1761 static tree
1762 bot_manip (tree* tp, int* walk_subtrees, void* data)
1764 splay_tree target_remap = ((splay_tree) data);
1765 tree t = *tp;
1767 if (!TYPE_P (t) && TREE_CONSTANT (t) && !TREE_SIDE_EFFECTS (t))
1769 /* There can't be any TARGET_EXPRs or their slot variables below
1770 this point. */
1771 *walk_subtrees = 0;
1772 return NULL_TREE;
1774 if (TREE_CODE (t) == TARGET_EXPR)
1776 tree u;
1778 if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
1779 u = build_cplus_new (TREE_TYPE (t), TREE_OPERAND (t, 1));
1780 else
1781 u = build_target_expr_with_type (TREE_OPERAND (t, 1), TREE_TYPE (t));
1783 /* Map the old variable to the new one. */
1784 splay_tree_insert (target_remap,
1785 (splay_tree_key) TREE_OPERAND (t, 0),
1786 (splay_tree_value) TREE_OPERAND (u, 0));
1788 TREE_OPERAND (u, 1) = break_out_target_exprs (TREE_OPERAND (u, 1));
1790 /* Replace the old expression with the new version. */
1791 *tp = u;
1792 /* We don't have to go below this point; the recursive call to
1793 break_out_target_exprs will have handled anything below this
1794 point. */
1795 *walk_subtrees = 0;
1796 return NULL_TREE;
1799 /* Make a copy of this node. */
1800 return copy_tree_r (tp, walk_subtrees, NULL);
1803 /* Replace all remapped VAR_DECLs in T with their new equivalents.
1804 DATA is really a splay-tree mapping old variables to new
1805 variables. */
1807 static tree
1808 bot_replace (tree* t,
1809 int* walk_subtrees ATTRIBUTE_UNUSED ,
1810 void* data)
1812 splay_tree target_remap = ((splay_tree) data);
1814 if (TREE_CODE (*t) == VAR_DECL)
1816 splay_tree_node n = splay_tree_lookup (target_remap,
1817 (splay_tree_key) *t);
1818 if (n)
1819 *t = (tree) n->value;
1822 return NULL_TREE;
1825 /* When we parse a default argument expression, we may create
1826 temporary variables via TARGET_EXPRs. When we actually use the
1827 default-argument expression, we make a copy of the expression, but
1828 we must replace the temporaries with appropriate local versions. */
1830 tree
1831 break_out_target_exprs (tree t)
1833 static int target_remap_count;
1834 static splay_tree target_remap;
1836 if (!target_remap_count++)
1837 target_remap = splay_tree_new (splay_tree_compare_pointers,
1838 /*splay_tree_delete_key_fn=*/NULL,
1839 /*splay_tree_delete_value_fn=*/NULL);
1840 cp_walk_tree (&t, bot_manip, target_remap, NULL);
1841 cp_walk_tree (&t, bot_replace, target_remap, NULL);
1843 if (!--target_remap_count)
1845 splay_tree_delete (target_remap);
1846 target_remap = NULL;
1849 return t;
1852 /* Similar to `build_nt', but for template definitions of dependent
1853 expressions */
1855 tree
1856 build_min_nt (enum tree_code code, ...)
1858 tree t;
1859 int length;
1860 int i;
1861 va_list p;
1863 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
1865 va_start (p, code);
1867 t = make_node (code);
1868 length = TREE_CODE_LENGTH (code);
1870 for (i = 0; i < length; i++)
1872 tree x = va_arg (p, tree);
1873 TREE_OPERAND (t, i) = x;
1876 va_end (p);
1877 return t;
1881 /* Similar to `build', but for template definitions. */
1883 tree
1884 build_min (enum tree_code code, tree tt, ...)
1886 tree t;
1887 int length;
1888 int i;
1889 va_list p;
1891 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
1893 va_start (p, tt);
1895 t = make_node (code);
1896 length = TREE_CODE_LENGTH (code);
1897 TREE_TYPE (t) = tt;
1899 for (i = 0; i < length; i++)
1901 tree x = va_arg (p, tree);
1902 TREE_OPERAND (t, i) = x;
1903 if (x && !TYPE_P (x) && TREE_SIDE_EFFECTS (x))
1904 TREE_SIDE_EFFECTS (t) = 1;
1907 va_end (p);
1908 return t;
1911 /* Similar to `build', but for template definitions of non-dependent
1912 expressions. NON_DEP is the non-dependent expression that has been
1913 built. */
1915 tree
1916 build_min_non_dep (enum tree_code code, tree non_dep, ...)
1918 tree t;
1919 int length;
1920 int i;
1921 va_list p;
1923 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
1925 va_start (p, non_dep);
1927 t = make_node (code);
1928 length = TREE_CODE_LENGTH (code);
1929 TREE_TYPE (t) = TREE_TYPE (non_dep);
1930 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
1932 for (i = 0; i < length; i++)
1934 tree x = va_arg (p, tree);
1935 TREE_OPERAND (t, i) = x;
1938 if (code == COMPOUND_EXPR && TREE_CODE (non_dep) != COMPOUND_EXPR)
1939 /* This should not be considered a COMPOUND_EXPR, because it
1940 resolves to an overload. */
1941 COMPOUND_EXPR_OVERLOADED (t) = 1;
1943 va_end (p);
1944 return t;
1947 /* Similar to `build_nt_call_vec', but for template definitions of
1948 non-dependent expressions. NON_DEP is the non-dependent expression
1949 that has been built. */
1951 tree
1952 build_min_non_dep_call_vec (tree non_dep, tree fn, VEC(tree,gc) *argvec)
1954 tree t = build_nt_call_vec (fn, argvec);
1955 TREE_TYPE (t) = TREE_TYPE (non_dep);
1956 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
1957 return t;
1960 tree
1961 get_type_decl (tree t)
1963 if (TREE_CODE (t) == TYPE_DECL)
1964 return t;
1965 if (TYPE_P (t))
1966 return TYPE_STUB_DECL (t);
1967 gcc_assert (t == error_mark_node);
1968 return t;
1971 /* Returns the namespace that contains DECL, whether directly or
1972 indirectly. */
1974 tree
1975 decl_namespace_context (tree decl)
1977 while (1)
1979 if (TREE_CODE (decl) == NAMESPACE_DECL)
1980 return decl;
1981 else if (TYPE_P (decl))
1982 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
1983 else
1984 decl = CP_DECL_CONTEXT (decl);
1988 /* Returns true if decl is within an anonymous namespace, however deeply
1989 nested, or false otherwise. */
1991 bool
1992 decl_anon_ns_mem_p (const_tree decl)
1994 while (1)
1996 if (decl == NULL_TREE || decl == error_mark_node)
1997 return false;
1998 if (TREE_CODE (decl) == NAMESPACE_DECL
1999 && DECL_NAME (decl) == NULL_TREE)
2000 return true;
2001 /* Classes and namespaces inside anonymous namespaces have
2002 TREE_PUBLIC == 0, so we can shortcut the search. */
2003 else if (TYPE_P (decl))
2004 return (TREE_PUBLIC (TYPE_NAME (decl)) == 0);
2005 else if (TREE_CODE (decl) == NAMESPACE_DECL)
2006 return (TREE_PUBLIC (decl) == 0);
2007 else
2008 decl = DECL_CONTEXT (decl);
2012 /* Return truthvalue of whether T1 is the same tree structure as T2.
2013 Return 1 if they are the same. Return 0 if they are different. */
2015 bool
2016 cp_tree_equal (tree t1, tree t2)
2018 enum tree_code code1, code2;
2020 if (t1 == t2)
2021 return true;
2022 if (!t1 || !t2)
2023 return false;
2025 for (code1 = TREE_CODE (t1);
2026 CONVERT_EXPR_CODE_P (code1)
2027 || code1 == NON_LVALUE_EXPR;
2028 code1 = TREE_CODE (t1))
2029 t1 = TREE_OPERAND (t1, 0);
2030 for (code2 = TREE_CODE (t2);
2031 CONVERT_EXPR_CODE_P (code2)
2032 || code1 == NON_LVALUE_EXPR;
2033 code2 = TREE_CODE (t2))
2034 t2 = TREE_OPERAND (t2, 0);
2036 /* They might have become equal now. */
2037 if (t1 == t2)
2038 return true;
2040 if (code1 != code2)
2041 return false;
2043 switch (code1)
2045 case INTEGER_CST:
2046 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
2047 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
2049 case REAL_CST:
2050 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2052 case STRING_CST:
2053 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2054 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2055 TREE_STRING_LENGTH (t1));
2057 case FIXED_CST:
2058 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
2059 TREE_FIXED_CST (t2));
2061 case COMPLEX_CST:
2062 return cp_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
2063 && cp_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
2065 case CONSTRUCTOR:
2066 /* We need to do this when determining whether or not two
2067 non-type pointer to member function template arguments
2068 are the same. */
2069 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
2070 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
2071 return false;
2073 tree field, value;
2074 unsigned int i;
2075 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
2077 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
2078 if (!cp_tree_equal (field, elt2->index)
2079 || !cp_tree_equal (value, elt2->value))
2080 return false;
2083 return true;
2085 case TREE_LIST:
2086 if (!cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
2087 return false;
2088 if (!cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
2089 return false;
2090 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
2092 case SAVE_EXPR:
2093 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2095 case CALL_EXPR:
2097 tree arg1, arg2;
2098 call_expr_arg_iterator iter1, iter2;
2099 if (!cp_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
2100 return false;
2101 for (arg1 = first_call_expr_arg (t1, &iter1),
2102 arg2 = first_call_expr_arg (t2, &iter2);
2103 arg1 && arg2;
2104 arg1 = next_call_expr_arg (&iter1),
2105 arg2 = next_call_expr_arg (&iter2))
2106 if (!cp_tree_equal (arg1, arg2))
2107 return false;
2108 if (arg1 || arg2)
2109 return false;
2110 return true;
2113 case TARGET_EXPR:
2115 tree o1 = TREE_OPERAND (t1, 0);
2116 tree o2 = TREE_OPERAND (t2, 0);
2118 /* Special case: if either target is an unallocated VAR_DECL,
2119 it means that it's going to be unified with whatever the
2120 TARGET_EXPR is really supposed to initialize, so treat it
2121 as being equivalent to anything. */
2122 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
2123 && !DECL_RTL_SET_P (o1))
2124 /*Nop*/;
2125 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
2126 && !DECL_RTL_SET_P (o2))
2127 /*Nop*/;
2128 else if (!cp_tree_equal (o1, o2))
2129 return false;
2131 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
2134 case WITH_CLEANUP_EXPR:
2135 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
2136 return false;
2137 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
2139 case COMPONENT_REF:
2140 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
2141 return false;
2142 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2144 case PARM_DECL:
2145 /* For comparing uses of parameters in late-specified return types
2146 with an out-of-class definition of the function. */
2147 if (same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
2148 && DECL_PARM_INDEX (t1) == DECL_PARM_INDEX (t2))
2149 return true;
2150 else
2151 return false;
2153 case VAR_DECL:
2154 case CONST_DECL:
2155 case FUNCTION_DECL:
2156 case TEMPLATE_DECL:
2157 case IDENTIFIER_NODE:
2158 case SSA_NAME:
2159 return false;
2161 case BASELINK:
2162 return (BASELINK_BINFO (t1) == BASELINK_BINFO (t2)
2163 && BASELINK_ACCESS_BINFO (t1) == BASELINK_ACCESS_BINFO (t2)
2164 && cp_tree_equal (BASELINK_FUNCTIONS (t1),
2165 BASELINK_FUNCTIONS (t2)));
2167 case TEMPLATE_PARM_INDEX:
2168 return (TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
2169 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2)
2170 && (TEMPLATE_PARM_PARAMETER_PACK (t1)
2171 == TEMPLATE_PARM_PARAMETER_PACK (t2))
2172 && same_type_p (TREE_TYPE (TEMPLATE_PARM_DECL (t1)),
2173 TREE_TYPE (TEMPLATE_PARM_DECL (t2))));
2175 case TEMPLATE_ID_EXPR:
2177 unsigned ix;
2178 tree vec1, vec2;
2180 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
2181 return false;
2182 vec1 = TREE_OPERAND (t1, 1);
2183 vec2 = TREE_OPERAND (t2, 1);
2185 if (!vec1 || !vec2)
2186 return !vec1 && !vec2;
2188 if (TREE_VEC_LENGTH (vec1) != TREE_VEC_LENGTH (vec2))
2189 return false;
2191 for (ix = TREE_VEC_LENGTH (vec1); ix--;)
2192 if (!cp_tree_equal (TREE_VEC_ELT (vec1, ix),
2193 TREE_VEC_ELT (vec2, ix)))
2194 return false;
2196 return true;
2199 case SIZEOF_EXPR:
2200 case ALIGNOF_EXPR:
2202 tree o1 = TREE_OPERAND (t1, 0);
2203 tree o2 = TREE_OPERAND (t2, 0);
2205 if (TREE_CODE (o1) != TREE_CODE (o2))
2206 return false;
2207 if (TYPE_P (o1))
2208 return same_type_p (o1, o2);
2209 else
2210 return cp_tree_equal (o1, o2);
2213 case MODOP_EXPR:
2215 tree t1_op1, t2_op1;
2217 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
2218 return false;
2220 t1_op1 = TREE_OPERAND (t1, 1);
2221 t2_op1 = TREE_OPERAND (t2, 1);
2222 if (TREE_CODE (t1_op1) != TREE_CODE (t2_op1))
2223 return false;
2225 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t2, 2));
2228 case PTRMEM_CST:
2229 /* Two pointer-to-members are the same if they point to the same
2230 field or function in the same class. */
2231 if (PTRMEM_CST_MEMBER (t1) != PTRMEM_CST_MEMBER (t2))
2232 return false;
2234 return same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2));
2236 case OVERLOAD:
2237 if (OVL_FUNCTION (t1) != OVL_FUNCTION (t2))
2238 return false;
2239 return cp_tree_equal (OVL_CHAIN (t1), OVL_CHAIN (t2));
2241 case TRAIT_EXPR:
2242 if (TRAIT_EXPR_KIND (t1) != TRAIT_EXPR_KIND (t2))
2243 return false;
2244 return same_type_p (TRAIT_EXPR_TYPE1 (t1), TRAIT_EXPR_TYPE1 (t2))
2245 && same_type_p (TRAIT_EXPR_TYPE2 (t1), TRAIT_EXPR_TYPE2 (t2));
2247 case CAST_EXPR:
2248 case STATIC_CAST_EXPR:
2249 case REINTERPRET_CAST_EXPR:
2250 case CONST_CAST_EXPR:
2251 case DYNAMIC_CAST_EXPR:
2252 case NEW_EXPR:
2253 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2254 return false;
2255 /* Now compare operands as usual. */
2256 break;
2258 default:
2259 break;
2262 switch (TREE_CODE_CLASS (code1))
2264 case tcc_unary:
2265 case tcc_binary:
2266 case tcc_comparison:
2267 case tcc_expression:
2268 case tcc_vl_exp:
2269 case tcc_reference:
2270 case tcc_statement:
2272 int i, n;
2274 n = TREE_OPERAND_LENGTH (t1);
2275 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
2276 && n != TREE_OPERAND_LENGTH (t2))
2277 return false;
2279 for (i = 0; i < n; ++i)
2280 if (!cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
2281 return false;
2283 return true;
2286 case tcc_type:
2287 return same_type_p (t1, t2);
2288 default:
2289 gcc_unreachable ();
2291 /* We can get here with --disable-checking. */
2292 return false;
2295 /* The type of ARG when used as an lvalue. */
2297 tree
2298 lvalue_type (tree arg)
2300 tree type = TREE_TYPE (arg);
2301 return type;
2304 /* The type of ARG for printing error messages; denote lvalues with
2305 reference types. */
2307 tree
2308 error_type (tree arg)
2310 tree type = TREE_TYPE (arg);
2312 if (TREE_CODE (type) == ARRAY_TYPE)
2314 else if (TREE_CODE (type) == ERROR_MARK)
2316 else if (real_lvalue_p (arg))
2317 type = build_reference_type (lvalue_type (arg));
2318 else if (MAYBE_CLASS_TYPE_P (type))
2319 type = lvalue_type (arg);
2321 return type;
2324 /* Does FUNCTION use a variable-length argument list? */
2327 varargs_function_p (const_tree function)
2329 return stdarg_p (TREE_TYPE (function));
2332 /* Returns 1 if decl is a member of a class. */
2335 member_p (const_tree decl)
2337 const_tree const ctx = DECL_CONTEXT (decl);
2338 return (ctx && TYPE_P (ctx));
2341 /* Create a placeholder for member access where we don't actually have an
2342 object that the access is against. */
2344 tree
2345 build_dummy_object (tree type)
2347 tree decl = build1 (NOP_EXPR, build_pointer_type (type), void_zero_node);
2348 return cp_build_indirect_ref (decl, RO_NULL, tf_warning_or_error);
2351 /* We've gotten a reference to a member of TYPE. Return *this if appropriate,
2352 or a dummy object otherwise. If BINFOP is non-0, it is filled with the
2353 binfo path from current_class_type to TYPE, or 0. */
2355 tree
2356 maybe_dummy_object (tree type, tree* binfop)
2358 tree decl, context;
2359 tree binfo;
2360 tree current = current_nonlambda_class_type ();
2362 if (current
2363 && (binfo = lookup_base (current, type, ba_any, NULL)))
2364 context = current;
2365 else
2367 /* Reference from a nested class member function. */
2368 context = type;
2369 binfo = TYPE_BINFO (type);
2372 if (binfop)
2373 *binfop = binfo;
2375 if (current_class_ref && context == current_class_type
2376 /* Kludge: Make sure that current_class_type is actually
2377 correct. It might not be if we're in the middle of
2378 tsubst_default_argument. */
2379 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (current_class_ref)),
2380 current_class_type))
2381 decl = current_class_ref;
2382 else if (current != current_class_type
2383 && context == nonlambda_method_basetype ())
2384 /* In a lambda, need to go through 'this' capture. */
2385 decl = (cp_build_indirect_ref
2386 ((lambda_expr_this_capture
2387 (CLASSTYPE_LAMBDA_EXPR (current_class_type))),
2388 RO_NULL, tf_warning_or_error));
2389 else
2390 decl = build_dummy_object (context);
2392 return decl;
2395 /* Returns 1 if OB is a placeholder object, or a pointer to one. */
2398 is_dummy_object (const_tree ob)
2400 if (TREE_CODE (ob) == INDIRECT_REF)
2401 ob = TREE_OPERAND (ob, 0);
2402 return (TREE_CODE (ob) == NOP_EXPR
2403 && TREE_OPERAND (ob, 0) == void_zero_node);
2406 /* Returns 1 iff type T is something we want to treat as a scalar type for
2407 the purpose of deciding whether it is trivial/POD/standard-layout. */
2409 static bool
2410 scalarish_type_p (const_tree t)
2412 if (t == error_mark_node)
2413 return 1;
2415 return (SCALAR_TYPE_P (t)
2416 || TREE_CODE (t) == VECTOR_TYPE);
2419 /* Returns true iff T requires non-trivial default initialization. */
2421 bool
2422 type_has_nontrivial_default_init (const_tree t)
2424 t = strip_array_types (CONST_CAST_TREE (t));
2426 if (CLASS_TYPE_P (t))
2427 return TYPE_HAS_COMPLEX_DFLT (t);
2428 else
2429 return 0;
2432 /* Returns true iff copying an object of type T (including via move
2433 constructor) is non-trivial. That is, T has no non-trivial copy
2434 constructors and no non-trivial move constructors. */
2436 bool
2437 type_has_nontrivial_copy_init (const_tree t)
2439 t = strip_array_types (CONST_CAST_TREE (t));
2441 if (CLASS_TYPE_P (t))
2443 gcc_assert (COMPLETE_TYPE_P (t));
2444 return ((TYPE_HAS_COPY_CTOR (t)
2445 && TYPE_HAS_COMPLEX_COPY_CTOR (t))
2446 || TYPE_HAS_COMPLEX_MOVE_CTOR (t));
2448 else
2449 return 0;
2452 /* Returns 1 iff type T is a trivially copyable type, as defined in
2453 [basic.types] and [class]. */
2455 bool
2456 trivially_copyable_p (const_tree t)
2458 t = strip_array_types (CONST_CAST_TREE (t));
2460 if (CLASS_TYPE_P (t))
2461 return ((!TYPE_HAS_COPY_CTOR (t)
2462 || !TYPE_HAS_COMPLEX_COPY_CTOR (t))
2463 && !TYPE_HAS_COMPLEX_MOVE_CTOR (t)
2464 && (!TYPE_HAS_COPY_ASSIGN (t)
2465 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (t))
2466 && !TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
2467 && TYPE_HAS_TRIVIAL_DESTRUCTOR (t));
2468 else
2469 return scalarish_type_p (t);
2472 /* Returns 1 iff type T is a trivial type, as defined in [basic.types] and
2473 [class]. */
2475 bool
2476 trivial_type_p (const_tree t)
2478 t = strip_array_types (CONST_CAST_TREE (t));
2480 if (CLASS_TYPE_P (t))
2481 return (TYPE_HAS_TRIVIAL_DFLT (t)
2482 && trivially_copyable_p (t));
2483 else
2484 return scalarish_type_p (t);
2487 /* Returns 1 iff type T is a POD type, as defined in [basic.types]. */
2489 bool
2490 pod_type_p (const_tree t)
2492 /* This CONST_CAST is okay because strip_array_types returns its
2493 argument unmodified and we assign it to a const_tree. */
2494 t = strip_array_types (CONST_CAST_TREE(t));
2496 if (!CLASS_TYPE_P (t))
2497 return scalarish_type_p (t);
2498 else if (cxx_dialect > cxx98)
2499 /* [class]/10: A POD struct is a class that is both a trivial class and a
2500 standard-layout class, and has no non-static data members of type
2501 non-POD struct, non-POD union (or array of such types).
2503 We don't need to check individual members because if a member is
2504 non-std-layout or non-trivial, the class will be too. */
2505 return (std_layout_type_p (t) && trivial_type_p (t));
2506 else
2507 /* The C++98 definition of POD is different. */
2508 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
2511 /* Returns true iff T is POD for the purpose of layout, as defined in the
2512 C++ ABI. */
2514 bool
2515 layout_pod_type_p (const_tree t)
2517 t = strip_array_types (CONST_CAST_TREE (t));
2519 if (CLASS_TYPE_P (t))
2520 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
2521 else
2522 return scalarish_type_p (t);
2525 /* Returns true iff T is a standard-layout type, as defined in
2526 [basic.types]. */
2528 bool
2529 std_layout_type_p (const_tree t)
2531 t = strip_array_types (CONST_CAST_TREE (t));
2533 if (CLASS_TYPE_P (t))
2534 return !CLASSTYPE_NON_STD_LAYOUT (t);
2535 else
2536 return scalarish_type_p (t);
2539 /* Nonzero iff type T is a class template implicit specialization. */
2541 bool
2542 class_tmpl_impl_spec_p (const_tree t)
2544 return CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INSTANTIATION (t);
2547 /* Returns 1 iff zero initialization of type T means actually storing
2548 zeros in it. */
2551 zero_init_p (const_tree t)
2553 /* This CONST_CAST is okay because strip_array_types returns its
2554 argument unmodified and we assign it to a const_tree. */
2555 t = strip_array_types (CONST_CAST_TREE(t));
2557 if (t == error_mark_node)
2558 return 1;
2560 /* NULL pointers to data members are initialized with -1. */
2561 if (TYPE_PTRMEM_P (t))
2562 return 0;
2564 /* Classes that contain types that can't be zero-initialized, cannot
2565 be zero-initialized themselves. */
2566 if (CLASS_TYPE_P (t) && CLASSTYPE_NON_ZERO_INIT_P (t))
2567 return 0;
2569 return 1;
2572 /* Table of valid C++ attributes. */
2573 const struct attribute_spec cxx_attribute_table[] =
2575 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2576 { "java_interface", 0, 0, false, false, false, handle_java_interface_attribute },
2577 { "com_interface", 0, 0, false, false, false, handle_com_interface_attribute },
2578 { "init_priority", 1, 1, true, false, false, handle_init_priority_attribute },
2579 { NULL, 0, 0, false, false, false, NULL }
2582 /* Handle a "java_interface" attribute; arguments as in
2583 struct attribute_spec.handler. */
2584 static tree
2585 handle_java_interface_attribute (tree* node,
2586 tree name,
2587 tree args ATTRIBUTE_UNUSED ,
2588 int flags,
2589 bool* no_add_attrs)
2591 if (DECL_P (*node)
2592 || !CLASS_TYPE_P (*node)
2593 || !TYPE_FOR_JAVA (*node))
2595 error ("%qE attribute can only be applied to Java class definitions",
2596 name);
2597 *no_add_attrs = true;
2598 return NULL_TREE;
2600 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
2601 *node = build_variant_type_copy (*node);
2602 TYPE_JAVA_INTERFACE (*node) = 1;
2604 return NULL_TREE;
2607 /* Handle a "com_interface" attribute; arguments as in
2608 struct attribute_spec.handler. */
2609 static tree
2610 handle_com_interface_attribute (tree* node,
2611 tree name,
2612 tree args ATTRIBUTE_UNUSED ,
2613 int flags ATTRIBUTE_UNUSED ,
2614 bool* no_add_attrs)
2616 static int warned;
2618 *no_add_attrs = true;
2620 if (DECL_P (*node)
2621 || !CLASS_TYPE_P (*node)
2622 || *node != TYPE_MAIN_VARIANT (*node))
2624 warning (OPT_Wattributes, "%qE attribute can only be applied "
2625 "to class definitions", name);
2626 return NULL_TREE;
2629 if (!warned++)
2630 warning (0, "%qE is obsolete; g++ vtables are now COM-compatible by default",
2631 name);
2633 return NULL_TREE;
2636 /* Handle an "init_priority" attribute; arguments as in
2637 struct attribute_spec.handler. */
2638 static tree
2639 handle_init_priority_attribute (tree* node,
2640 tree name,
2641 tree args,
2642 int flags ATTRIBUTE_UNUSED ,
2643 bool* no_add_attrs)
2645 tree initp_expr = TREE_VALUE (args);
2646 tree decl = *node;
2647 tree type = TREE_TYPE (decl);
2648 int pri;
2650 STRIP_NOPS (initp_expr);
2652 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
2654 error ("requested init_priority is not an integer constant");
2655 *no_add_attrs = true;
2656 return NULL_TREE;
2659 pri = TREE_INT_CST_LOW (initp_expr);
2661 type = strip_array_types (type);
2663 if (decl == NULL_TREE
2664 || TREE_CODE (decl) != VAR_DECL
2665 || !TREE_STATIC (decl)
2666 || DECL_EXTERNAL (decl)
2667 || (TREE_CODE (type) != RECORD_TYPE
2668 && TREE_CODE (type) != UNION_TYPE)
2669 /* Static objects in functions are initialized the
2670 first time control passes through that
2671 function. This is not precise enough to pin down an
2672 init_priority value, so don't allow it. */
2673 || current_function_decl)
2675 error ("can only use %qE attribute on file-scope definitions "
2676 "of objects of class type", name);
2677 *no_add_attrs = true;
2678 return NULL_TREE;
2681 if (pri > MAX_INIT_PRIORITY || pri <= 0)
2683 error ("requested init_priority is out of range");
2684 *no_add_attrs = true;
2685 return NULL_TREE;
2688 /* Check for init_priorities that are reserved for
2689 language and runtime support implementations.*/
2690 if (pri <= MAX_RESERVED_INIT_PRIORITY)
2692 warning
2693 (0, "requested init_priority is reserved for internal use");
2696 if (SUPPORTS_INIT_PRIORITY)
2698 SET_DECL_INIT_PRIORITY (decl, pri);
2699 DECL_HAS_INIT_PRIORITY_P (decl) = 1;
2700 return NULL_TREE;
2702 else
2704 error ("%qE attribute is not supported on this platform", name);
2705 *no_add_attrs = true;
2706 return NULL_TREE;
2710 /* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the
2711 thing pointed to by the constant. */
2713 tree
2714 make_ptrmem_cst (tree type, tree member)
2716 tree ptrmem_cst = make_node (PTRMEM_CST);
2717 TREE_TYPE (ptrmem_cst) = type;
2718 PTRMEM_CST_MEMBER (ptrmem_cst) = member;
2719 return ptrmem_cst;
2722 /* Build a variant of TYPE that has the indicated ATTRIBUTES. May
2723 return an existing type if an appropriate type already exists. */
2725 tree
2726 cp_build_type_attribute_variant (tree type, tree attributes)
2728 tree new_type;
2730 new_type = build_type_attribute_variant (type, attributes);
2731 if (TREE_CODE (new_type) == FUNCTION_TYPE
2732 || TREE_CODE (new_type) == METHOD_TYPE)
2733 new_type = build_exception_variant (new_type,
2734 TYPE_RAISES_EXCEPTIONS (type));
2736 /* Making a new main variant of a class type is broken. */
2737 gcc_assert (!CLASS_TYPE_P (type) || new_type == type);
2739 return new_type;
2742 /* Return TRUE if TYPE1 and TYPE2 are identical for type hashing purposes.
2743 Called only after doing all language independent checks. Only
2744 to check TYPE_RAISES_EXCEPTIONS for FUNCTION_TYPE, the rest is already
2745 compared in type_hash_eq. */
2747 bool
2748 cxx_type_hash_eq (const_tree typea, const_tree typeb)
2750 gcc_assert (TREE_CODE (typea) == FUNCTION_TYPE);
2752 return comp_except_specs (TYPE_RAISES_EXCEPTIONS (typea),
2753 TYPE_RAISES_EXCEPTIONS (typeb), ce_exact);
2756 /* Apply FUNC to all language-specific sub-trees of TP in a pre-order
2757 traversal. Called from walk_tree. */
2759 tree
2760 cp_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func,
2761 void *data, struct pointer_set_t *pset)
2763 enum tree_code code = TREE_CODE (*tp);
2764 tree result;
2766 #define WALK_SUBTREE(NODE) \
2767 do \
2769 result = cp_walk_tree (&(NODE), func, data, pset); \
2770 if (result) goto out; \
2772 while (0)
2774 /* Not one of the easy cases. We must explicitly go through the
2775 children. */
2776 result = NULL_TREE;
2777 switch (code)
2779 case DEFAULT_ARG:
2780 case TEMPLATE_TEMPLATE_PARM:
2781 case BOUND_TEMPLATE_TEMPLATE_PARM:
2782 case UNBOUND_CLASS_TEMPLATE:
2783 case TEMPLATE_PARM_INDEX:
2784 case TEMPLATE_TYPE_PARM:
2785 case TYPENAME_TYPE:
2786 case TYPEOF_TYPE:
2787 /* None of these have subtrees other than those already walked
2788 above. */
2789 *walk_subtrees_p = 0;
2790 break;
2792 case BASELINK:
2793 WALK_SUBTREE (BASELINK_FUNCTIONS (*tp));
2794 *walk_subtrees_p = 0;
2795 break;
2797 case PTRMEM_CST:
2798 WALK_SUBTREE (TREE_TYPE (*tp));
2799 *walk_subtrees_p = 0;
2800 break;
2802 case TREE_LIST:
2803 WALK_SUBTREE (TREE_PURPOSE (*tp));
2804 break;
2806 case OVERLOAD:
2807 WALK_SUBTREE (OVL_FUNCTION (*tp));
2808 WALK_SUBTREE (OVL_CHAIN (*tp));
2809 *walk_subtrees_p = 0;
2810 break;
2812 case USING_DECL:
2813 WALK_SUBTREE (DECL_NAME (*tp));
2814 WALK_SUBTREE (USING_DECL_SCOPE (*tp));
2815 WALK_SUBTREE (USING_DECL_DECLS (*tp));
2816 *walk_subtrees_p = 0;
2817 break;
2819 case RECORD_TYPE:
2820 if (TYPE_PTRMEMFUNC_P (*tp))
2821 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (*tp));
2822 break;
2824 case TYPE_ARGUMENT_PACK:
2825 case NONTYPE_ARGUMENT_PACK:
2827 tree args = ARGUMENT_PACK_ARGS (*tp);
2828 int i, len = TREE_VEC_LENGTH (args);
2829 for (i = 0; i < len; i++)
2830 WALK_SUBTREE (TREE_VEC_ELT (args, i));
2832 break;
2834 case TYPE_PACK_EXPANSION:
2835 WALK_SUBTREE (TREE_TYPE (*tp));
2836 *walk_subtrees_p = 0;
2837 break;
2839 case EXPR_PACK_EXPANSION:
2840 WALK_SUBTREE (TREE_OPERAND (*tp, 0));
2841 *walk_subtrees_p = 0;
2842 break;
2844 case CAST_EXPR:
2845 case REINTERPRET_CAST_EXPR:
2846 case STATIC_CAST_EXPR:
2847 case CONST_CAST_EXPR:
2848 case DYNAMIC_CAST_EXPR:
2849 if (TREE_TYPE (*tp))
2850 WALK_SUBTREE (TREE_TYPE (*tp));
2853 int i;
2854 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (*tp)); ++i)
2855 WALK_SUBTREE (TREE_OPERAND (*tp, i));
2857 *walk_subtrees_p = 0;
2858 break;
2860 case TRAIT_EXPR:
2861 WALK_SUBTREE (TRAIT_EXPR_TYPE1 (*tp));
2862 WALK_SUBTREE (TRAIT_EXPR_TYPE2 (*tp));
2863 *walk_subtrees_p = 0;
2864 break;
2866 case DECLTYPE_TYPE:
2867 WALK_SUBTREE (DECLTYPE_TYPE_EXPR (*tp));
2868 *walk_subtrees_p = 0;
2869 break;
2872 default:
2873 return NULL_TREE;
2876 /* We didn't find what we were looking for. */
2877 out:
2878 return result;
2880 #undef WALK_SUBTREE
2883 /* Like save_expr, but for C++. */
2885 tree
2886 cp_save_expr (tree expr)
2888 /* There is no reason to create a SAVE_EXPR within a template; if
2889 needed, we can create the SAVE_EXPR when instantiating the
2890 template. Furthermore, the middle-end cannot handle C++-specific
2891 tree codes. */
2892 if (processing_template_decl)
2893 return expr;
2894 return save_expr (expr);
2897 /* Initialize tree.c. */
2899 void
2900 init_tree (void)
2902 list_hash_table = htab_create_ggc (31, list_hash, list_hash_eq, NULL);
2905 /* Returns the kind of special function that DECL (a FUNCTION_DECL)
2906 is. Note that sfk_none is zero, so this function can be used as a
2907 predicate to test whether or not DECL is a special function. */
2909 special_function_kind
2910 special_function_p (const_tree decl)
2912 /* Rather than doing all this stuff with magic names, we should
2913 probably have a field of type `special_function_kind' in
2914 DECL_LANG_SPECIFIC. */
2915 if (DECL_COPY_CONSTRUCTOR_P (decl))
2916 return sfk_copy_constructor;
2917 if (DECL_MOVE_CONSTRUCTOR_P (decl))
2918 return sfk_move_constructor;
2919 if (DECL_CONSTRUCTOR_P (decl))
2920 return sfk_constructor;
2921 if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
2923 if (copy_fn_p (decl))
2924 return sfk_copy_assignment;
2925 if (move_fn_p (decl))
2926 return sfk_move_assignment;
2928 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
2929 return sfk_destructor;
2930 if (DECL_COMPLETE_DESTRUCTOR_P (decl))
2931 return sfk_complete_destructor;
2932 if (DECL_BASE_DESTRUCTOR_P (decl))
2933 return sfk_base_destructor;
2934 if (DECL_DELETING_DESTRUCTOR_P (decl))
2935 return sfk_deleting_destructor;
2936 if (DECL_CONV_FN_P (decl))
2937 return sfk_conversion;
2939 return sfk_none;
2942 /* Returns nonzero if TYPE is a character type, including wchar_t. */
2945 char_type_p (tree type)
2947 return (same_type_p (type, char_type_node)
2948 || same_type_p (type, unsigned_char_type_node)
2949 || same_type_p (type, signed_char_type_node)
2950 || same_type_p (type, char16_type_node)
2951 || same_type_p (type, char32_type_node)
2952 || same_type_p (type, wchar_type_node));
2955 /* Returns the kind of linkage associated with the indicated DECL. Th
2956 value returned is as specified by the language standard; it is
2957 independent of implementation details regarding template
2958 instantiation, etc. For example, it is possible that a declaration
2959 to which this function assigns external linkage would not show up
2960 as a global symbol when you run `nm' on the resulting object file. */
2962 linkage_kind
2963 decl_linkage (tree decl)
2965 /* This function doesn't attempt to calculate the linkage from first
2966 principles as given in [basic.link]. Instead, it makes use of
2967 the fact that we have already set TREE_PUBLIC appropriately, and
2968 then handles a few special cases. Ideally, we would calculate
2969 linkage first, and then transform that into a concrete
2970 implementation. */
2972 /* Things that don't have names have no linkage. */
2973 if (!DECL_NAME (decl))
2974 return lk_none;
2976 /* Fields have no linkage. */
2977 if (TREE_CODE (decl) == FIELD_DECL)
2978 return lk_none;
2980 /* Things that are TREE_PUBLIC have external linkage. */
2981 if (TREE_PUBLIC (decl))
2982 return lk_external;
2984 if (TREE_CODE (decl) == NAMESPACE_DECL)
2985 return lk_external;
2987 /* Linkage of a CONST_DECL depends on the linkage of the enumeration
2988 type. */
2989 if (TREE_CODE (decl) == CONST_DECL)
2990 return decl_linkage (TYPE_NAME (TREE_TYPE (decl)));
2992 /* Some things that are not TREE_PUBLIC have external linkage, too.
2993 For example, on targets that don't have weak symbols, we make all
2994 template instantiations have internal linkage (in the object
2995 file), but the symbols should still be treated as having external
2996 linkage from the point of view of the language. */
2997 if ((TREE_CODE (decl) == FUNCTION_DECL
2998 || TREE_CODE (decl) == VAR_DECL)
2999 && DECL_COMDAT (decl))
3000 return lk_external;
3002 /* Things in local scope do not have linkage, if they don't have
3003 TREE_PUBLIC set. */
3004 if (decl_function_context (decl))
3005 return lk_none;
3007 /* Members of the anonymous namespace also have TREE_PUBLIC unset, but
3008 are considered to have external linkage for language purposes. DECLs
3009 really meant to have internal linkage have DECL_THIS_STATIC set. */
3010 if (TREE_CODE (decl) == TYPE_DECL)
3011 return lk_external;
3012 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3014 if (!DECL_THIS_STATIC (decl))
3015 return lk_external;
3017 /* Static data members and static member functions from classes
3018 in anonymous namespace also don't have TREE_PUBLIC set. */
3019 if (DECL_CLASS_CONTEXT (decl))
3020 return lk_external;
3023 /* Everything else has internal linkage. */
3024 return lk_internal;
3027 /* Returns the storage duration of the object or reference associated with
3028 the indicated DECL, which should be a VAR_DECL or PARM_DECL. */
3030 duration_kind
3031 decl_storage_duration (tree decl)
3033 if (TREE_CODE (decl) == PARM_DECL)
3034 return dk_auto;
3035 if (TREE_CODE (decl) == FUNCTION_DECL)
3036 return dk_static;
3037 gcc_assert (TREE_CODE (decl) == VAR_DECL);
3038 if (!TREE_STATIC (decl)
3039 && !DECL_EXTERNAL (decl))
3040 return dk_auto;
3041 if (DECL_THREAD_LOCAL_P (decl))
3042 return dk_thread;
3043 return dk_static;
3046 /* EXP is an expression that we want to pre-evaluate. Returns (in
3047 *INITP) an expression that will perform the pre-evaluation. The
3048 value returned by this function is a side-effect free expression
3049 equivalent to the pre-evaluated expression. Callers must ensure
3050 that *INITP is evaluated before EXP. */
3052 tree
3053 stabilize_expr (tree exp, tree* initp)
3055 tree init_expr;
3057 if (!TREE_SIDE_EFFECTS (exp))
3058 init_expr = NULL_TREE;
3059 /* There are no expressions with REFERENCE_TYPE, but there can be call
3060 arguments with such a type; just treat it as a pointer. */
3061 else if (TREE_CODE (TREE_TYPE (exp)) == REFERENCE_TYPE
3062 || !lvalue_or_rvalue_with_address_p (exp))
3064 init_expr = get_target_expr (exp);
3065 exp = TARGET_EXPR_SLOT (init_expr);
3067 else
3069 bool xval = !real_lvalue_p (exp);
3070 exp = cp_build_addr_expr (exp, tf_warning_or_error);
3071 init_expr = get_target_expr (exp);
3072 exp = TARGET_EXPR_SLOT (init_expr);
3073 exp = cp_build_indirect_ref (exp, RO_NULL, tf_warning_or_error);
3074 if (xval)
3075 exp = move (exp);
3077 *initp = init_expr;
3079 gcc_assert (!TREE_SIDE_EFFECTS (exp));
3080 return exp;
3083 /* Add NEW_EXPR, an expression whose value we don't care about, after the
3084 similar expression ORIG. */
3086 tree
3087 add_stmt_to_compound (tree orig, tree new_expr)
3089 if (!new_expr || !TREE_SIDE_EFFECTS (new_expr))
3090 return orig;
3091 if (!orig || !TREE_SIDE_EFFECTS (orig))
3092 return new_expr;
3093 return build2 (COMPOUND_EXPR, void_type_node, orig, new_expr);
3096 /* Like stabilize_expr, but for a call whose arguments we want to
3097 pre-evaluate. CALL is modified in place to use the pre-evaluated
3098 arguments, while, upon return, *INITP contains an expression to
3099 compute the arguments. */
3101 void
3102 stabilize_call (tree call, tree *initp)
3104 tree inits = NULL_TREE;
3105 int i;
3106 int nargs = call_expr_nargs (call);
3108 if (call == error_mark_node || processing_template_decl)
3110 *initp = NULL_TREE;
3111 return;
3114 gcc_assert (TREE_CODE (call) == CALL_EXPR);
3116 for (i = 0; i < nargs; i++)
3118 tree init;
3119 CALL_EXPR_ARG (call, i) =
3120 stabilize_expr (CALL_EXPR_ARG (call, i), &init);
3121 inits = add_stmt_to_compound (inits, init);
3124 *initp = inits;
3127 /* Like stabilize_expr, but for an AGGR_INIT_EXPR whose arguments we want
3128 to pre-evaluate. CALL is modified in place to use the pre-evaluated
3129 arguments, while, upon return, *INITP contains an expression to
3130 compute the arguments. */
3132 void
3133 stabilize_aggr_init (tree call, tree *initp)
3135 tree inits = NULL_TREE;
3136 int i;
3137 int nargs = aggr_init_expr_nargs (call);
3139 if (call == error_mark_node)
3140 return;
3142 gcc_assert (TREE_CODE (call) == AGGR_INIT_EXPR);
3144 for (i = 0; i < nargs; i++)
3146 tree init;
3147 AGGR_INIT_EXPR_ARG (call, i) =
3148 stabilize_expr (AGGR_INIT_EXPR_ARG (call, i), &init);
3149 inits = add_stmt_to_compound (inits, init);
3152 *initp = inits;
3155 /* Like stabilize_expr, but for an initialization.
3157 If the initialization is for an object of class type, this function
3158 takes care not to introduce additional temporaries.
3160 Returns TRUE iff the expression was successfully pre-evaluated,
3161 i.e., if INIT is now side-effect free, except for, possible, a
3162 single call to a constructor. */
3164 bool
3165 stabilize_init (tree init, tree *initp)
3167 tree t = init;
3169 *initp = NULL_TREE;
3171 if (t == error_mark_node || processing_template_decl)
3172 return true;
3174 if (TREE_CODE (t) == INIT_EXPR
3175 && TREE_CODE (TREE_OPERAND (t, 1)) != TARGET_EXPR
3176 && TREE_CODE (TREE_OPERAND (t, 1)) != AGGR_INIT_EXPR)
3178 TREE_OPERAND (t, 1) = stabilize_expr (TREE_OPERAND (t, 1), initp);
3179 return true;
3182 if (TREE_CODE (t) == INIT_EXPR)
3183 t = TREE_OPERAND (t, 1);
3184 if (TREE_CODE (t) == TARGET_EXPR)
3185 t = TARGET_EXPR_INITIAL (t);
3186 if (TREE_CODE (t) == COMPOUND_EXPR)
3187 t = expr_last (t);
3188 if (TREE_CODE (t) == CONSTRUCTOR
3189 && EMPTY_CONSTRUCTOR_P (t))
3190 /* Default-initialization. */
3191 return true;
3193 /* If the initializer is a COND_EXPR, we can't preevaluate
3194 anything. */
3195 if (TREE_CODE (t) == COND_EXPR)
3196 return false;
3198 if (TREE_CODE (t) == CALL_EXPR)
3200 stabilize_call (t, initp);
3201 return true;
3204 if (TREE_CODE (t) == AGGR_INIT_EXPR)
3206 stabilize_aggr_init (t, initp);
3207 return true;
3210 /* The initialization is being performed via a bitwise copy -- and
3211 the item copied may have side effects. */
3212 return TREE_SIDE_EFFECTS (init);
3215 /* Like "fold", but should be used whenever we might be processing the
3216 body of a template. */
3218 tree
3219 fold_if_not_in_template (tree expr)
3221 /* In the body of a template, there is never any need to call
3222 "fold". We will call fold later when actually instantiating the
3223 template. Integral constant expressions in templates will be
3224 evaluated via fold_non_dependent_expr, as necessary. */
3225 if (processing_template_decl)
3226 return expr;
3228 /* Fold C++ front-end specific tree codes. */
3229 if (TREE_CODE (expr) == UNARY_PLUS_EXPR)
3230 return fold_convert (TREE_TYPE (expr), TREE_OPERAND (expr, 0));
3232 return fold (expr);
3235 /* Returns true if a cast to TYPE may appear in an integral constant
3236 expression. */
3238 bool
3239 cast_valid_in_integral_constant_expression_p (tree type)
3241 return (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3242 || cxx_dialect >= cxx0x
3243 || dependent_type_p (type)
3244 || type == error_mark_node);
3247 /* Return true if we need to fix linkage information of DECL. */
3249 static bool
3250 cp_fix_function_decl_p (tree decl)
3252 /* Skip if DECL is not externally visible. */
3253 if (!TREE_PUBLIC (decl))
3254 return false;
3256 /* We need to fix DECL if it a appears to be exported but with no
3257 function body. Thunks do not have CFGs and we may need to
3258 handle them specially later. */
3259 if (!gimple_has_body_p (decl)
3260 && !DECL_THUNK_P (decl)
3261 && !DECL_EXTERNAL (decl))
3263 struct cgraph_node *node = cgraph_get_node (decl);
3265 /* Don't fix same_body aliases. Although they don't have their own
3266 CFG, they share it with what they alias to. */
3267 if (!node
3268 || node->decl == decl
3269 || !node->same_body)
3270 return true;
3273 return false;
3276 /* Clean the C++ specific parts of the tree T. */
3278 void
3279 cp_free_lang_data (tree t)
3281 if (TREE_CODE (t) == METHOD_TYPE
3282 || TREE_CODE (t) == FUNCTION_TYPE)
3284 /* Default args are not interesting anymore. */
3285 tree argtypes = TYPE_ARG_TYPES (t);
3286 while (argtypes)
3288 TREE_PURPOSE (argtypes) = 0;
3289 argtypes = TREE_CHAIN (argtypes);
3292 else if (TREE_CODE (t) == FUNCTION_DECL
3293 && cp_fix_function_decl_p (t))
3295 /* If T is used in this translation unit at all, the definition
3296 must exist somewhere else since we have decided to not emit it
3297 in this TU. So make it an external reference. */
3298 DECL_EXTERNAL (t) = 1;
3299 TREE_STATIC (t) = 0;
3301 if (CP_AGGREGATE_TYPE_P (t)
3302 && TYPE_NAME (t))
3304 tree name = TYPE_NAME (t);
3305 if (TREE_CODE (name) == TYPE_DECL)
3306 name = DECL_NAME (name);
3307 /* Drop anonymous names. */
3308 if (name != NULL_TREE
3309 && ANON_AGGRNAME_P (name))
3310 TYPE_NAME (t) = NULL_TREE;
3312 if (TREE_CODE (t) == NAMESPACE_DECL)
3314 /* The list of users of a namespace isn't useful for the middle-end
3315 or debug generators. */
3316 DECL_NAMESPACE_USERS (t) = NULL_TREE;
3317 /* Neither do we need the leftover chaining of namespaces
3318 from the binding level. */
3319 DECL_CHAIN (t) = NULL_TREE;
3323 /* Stub for c-common. Please keep in sync with c-decl.c.
3324 FIXME: If address space support is target specific, then this
3325 should be a C target hook. But currently this is not possible,
3326 because this function is called via REGISTER_TARGET_PRAGMAS. */
3327 void
3328 c_register_addr_space (const char *word ATTRIBUTE_UNUSED,
3329 addr_space_t as ATTRIBUTE_UNUSED)
3334 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
3335 /* Complain that some language-specific thing hanging off a tree
3336 node has been accessed improperly. */
3338 void
3339 lang_check_failed (const char* file, int line, const char* function)
3341 internal_error ("lang_* check: failed in %s, at %s:%d",
3342 function, trim_filename (file), line);
3344 #endif /* ENABLE_TREE_CHECKING */
3346 #include "gt-cp-tree.h"