/
[official-gcc.git] / gcc / cp / tree.c
blob1a77dc18f328d1fa4e58e9802018f03aac477fc5
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, 2010
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 "tree-inline.h"
31 #include "debug.h"
32 #include "convert.h"
33 #include "cgraph.h"
34 #include "splay-tree.h"
35 #include "gimple.h" /* gimple_has_body_p */
37 static tree bot_manip (tree *, int *, void *);
38 static tree bot_replace (tree *, int *, void *);
39 static int list_hash_eq (const void *, const void *);
40 static hashval_t list_hash_pieces (tree, tree, tree);
41 static hashval_t list_hash (const void *);
42 static tree build_target_expr (tree, tree);
43 static tree count_trees_r (tree *, int *, void *);
44 static tree verify_stmt_tree_r (tree *, int *, void *);
45 static tree build_local_temp (tree);
47 static tree handle_java_interface_attribute (tree *, tree, tree, int, bool *);
48 static tree handle_com_interface_attribute (tree *, tree, tree, int, bool *);
49 static tree handle_init_priority_attribute (tree *, tree, tree, int, bool *);
51 /* If REF is an lvalue, returns the kind of lvalue that REF is.
52 Otherwise, returns clk_none. */
54 cp_lvalue_kind
55 lvalue_kind (const_tree ref)
57 cp_lvalue_kind op1_lvalue_kind = clk_none;
58 cp_lvalue_kind op2_lvalue_kind = clk_none;
60 /* Expressions of reference type are sometimes wrapped in
61 INDIRECT_REFs. INDIRECT_REFs are just internal compiler
62 representation, not part of the language, so we have to look
63 through them. */
64 if (TREE_CODE (ref) == INDIRECT_REF
65 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0)))
66 == REFERENCE_TYPE)
67 return lvalue_kind (TREE_OPERAND (ref, 0));
69 if (TREE_TYPE (ref)
70 && TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
72 /* unnamed rvalue references are rvalues */
73 if (TYPE_REF_IS_RVALUE (TREE_TYPE (ref))
74 && TREE_CODE (ref) != PARM_DECL
75 && TREE_CODE (ref) != VAR_DECL
76 && TREE_CODE (ref) != COMPONENT_REF)
77 return clk_rvalueref;
79 /* lvalue references and named rvalue references are lvalues. */
80 return clk_ordinary;
83 if (ref == current_class_ptr)
84 return clk_none;
86 switch (TREE_CODE (ref))
88 case SAVE_EXPR:
89 return clk_none;
90 /* preincrements and predecrements are valid lvals, provided
91 what they refer to are valid lvals. */
92 case PREINCREMENT_EXPR:
93 case PREDECREMENT_EXPR:
94 case TRY_CATCH_EXPR:
95 case WITH_CLEANUP_EXPR:
96 case REALPART_EXPR:
97 case IMAGPART_EXPR:
98 return lvalue_kind (TREE_OPERAND (ref, 0));
100 case COMPONENT_REF:
101 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
102 /* Look at the member designator. */
103 if (!op1_lvalue_kind)
105 else if (is_overloaded_fn (TREE_OPERAND (ref, 1)))
106 /* The "field" can be a FUNCTION_DECL or an OVERLOAD in some
107 situations. If we're seeing a COMPONENT_REF, it's a non-static
108 member, so it isn't an lvalue. */
109 op1_lvalue_kind = clk_none;
110 else if (TREE_CODE (TREE_OPERAND (ref, 1)) != FIELD_DECL)
111 /* This can be IDENTIFIER_NODE in a template. */;
112 else if (DECL_C_BIT_FIELD (TREE_OPERAND (ref, 1)))
114 /* Clear the ordinary bit. If this object was a class
115 rvalue we want to preserve that information. */
116 op1_lvalue_kind &= ~clk_ordinary;
117 /* The lvalue is for a bitfield. */
118 op1_lvalue_kind |= clk_bitfield;
120 else if (DECL_PACKED (TREE_OPERAND (ref, 1)))
121 op1_lvalue_kind |= clk_packed;
123 return op1_lvalue_kind;
125 case STRING_CST:
126 case COMPOUND_LITERAL_EXPR:
127 return clk_ordinary;
129 case CONST_DECL:
130 /* CONST_DECL without TREE_STATIC are enumeration values and
131 thus not lvalues. With TREE_STATIC they are used by ObjC++
132 in objc_build_string_object and need to be considered as
133 lvalues. */
134 if (! TREE_STATIC (ref))
135 return clk_none;
136 case VAR_DECL:
137 if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
138 && DECL_LANG_SPECIFIC (ref)
139 && DECL_IN_AGGR_P (ref))
140 return clk_none;
141 case INDIRECT_REF:
142 case ARRAY_REF:
143 case PARM_DECL:
144 case RESULT_DECL:
145 if (TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
146 return clk_ordinary;
147 break;
149 /* A scope ref in a template, left as SCOPE_REF to support later
150 access checking. */
151 case SCOPE_REF:
152 gcc_assert (!type_dependent_expression_p (CONST_CAST_TREE(ref)));
153 return lvalue_kind (TREE_OPERAND (ref, 1));
155 case MAX_EXPR:
156 case MIN_EXPR:
157 /* Disallow <? and >? as lvalues if either argument side-effects. */
158 if (TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 0))
159 || TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 1)))
160 return clk_none;
161 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
162 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1));
163 break;
165 case COND_EXPR:
166 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1)
167 ? TREE_OPERAND (ref, 1)
168 : TREE_OPERAND (ref, 0));
169 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 2));
170 break;
172 case MODIFY_EXPR:
173 return clk_ordinary;
175 case COMPOUND_EXPR:
176 return lvalue_kind (TREE_OPERAND (ref, 1));
178 case TARGET_EXPR:
179 return clk_class;
181 case VA_ARG_EXPR:
182 return (CLASS_TYPE_P (TREE_TYPE (ref)) ? clk_class : clk_none);
184 case CALL_EXPR:
185 /* Any class-valued call would be wrapped in a TARGET_EXPR. */
186 return clk_none;
188 case FUNCTION_DECL:
189 /* All functions (except non-static-member functions) are
190 lvalues. */
191 return (DECL_NONSTATIC_MEMBER_FUNCTION_P (ref)
192 ? clk_none : clk_ordinary);
194 case BASELINK:
195 /* We now represent a reference to a single static member function
196 with a BASELINK. */
197 /* This CONST_CAST is okay because BASELINK_FUNCTIONS returns
198 its argument unmodified and we assign it to a const_tree. */
199 return lvalue_kind (BASELINK_FUNCTIONS (CONST_CAST_TREE (ref)));
201 case NON_DEPENDENT_EXPR:
202 /* We must consider NON_DEPENDENT_EXPRs to be lvalues so that
203 things like "&E" where "E" is an expression with a
204 non-dependent type work. It is safe to be lenient because an
205 error will be issued when the template is instantiated if "E"
206 is not an lvalue. */
207 return clk_ordinary;
209 default:
210 break;
213 /* If one operand is not an lvalue at all, then this expression is
214 not an lvalue. */
215 if (!op1_lvalue_kind || !op2_lvalue_kind)
216 return clk_none;
218 /* Otherwise, it's an lvalue, and it has all the odd properties
219 contributed by either operand. */
220 op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind;
221 /* It's not an ordinary lvalue if it involves any other kind. */
222 if ((op1_lvalue_kind & ~clk_ordinary) != clk_none)
223 op1_lvalue_kind &= ~clk_ordinary;
224 /* It can't be both a pseudo-lvalue and a non-addressable lvalue.
225 A COND_EXPR of those should be wrapped in a TARGET_EXPR. */
226 if ((op1_lvalue_kind & (clk_rvalueref|clk_class))
227 && (op1_lvalue_kind & (clk_bitfield|clk_packed)))
228 op1_lvalue_kind = clk_none;
229 return op1_lvalue_kind;
232 /* Returns the kind of lvalue that REF is, in the sense of
233 [basic.lval]. This function should really be named lvalue_p; it
234 computes the C++ definition of lvalue. */
236 cp_lvalue_kind
237 real_lvalue_p (const_tree ref)
239 cp_lvalue_kind kind = lvalue_kind (ref);
240 if (kind & (clk_rvalueref|clk_class))
241 return clk_none;
242 else
243 return kind;
246 /* This differs from real_lvalue_p in that class rvalues are considered
247 lvalues. */
249 bool
250 lvalue_p (const_tree ref)
252 return (lvalue_kind (ref) != clk_none);
255 /* This differs from real_lvalue_p in that rvalues formed by dereferencing
256 rvalue references are considered rvalues. */
258 bool
259 lvalue_or_rvalue_with_address_p (const_tree ref)
261 cp_lvalue_kind kind = lvalue_kind (ref);
262 if (kind & clk_class)
263 return false;
264 else
265 return (kind != clk_none);
268 /* Test whether DECL is a builtin that may appear in a
269 constant-expression. */
271 bool
272 builtin_valid_in_constant_expr_p (const_tree decl)
274 /* At present BUILT_IN_CONSTANT_P is the only builtin we're allowing
275 in constant-expressions. We may want to add other builtins later. */
276 return DECL_IS_BUILTIN_CONSTANT_P (decl);
279 /* Build a TARGET_EXPR, initializing the DECL with the VALUE. */
281 static tree
282 build_target_expr (tree decl, tree value)
284 tree t;
286 #ifdef ENABLE_CHECKING
287 gcc_assert (VOID_TYPE_P (TREE_TYPE (value))
288 || TREE_TYPE (decl) == TREE_TYPE (value)
289 || useless_type_conversion_p (TREE_TYPE (decl),
290 TREE_TYPE (value)));
291 #endif
293 t = build4 (TARGET_EXPR, TREE_TYPE (decl), decl, value,
294 cxx_maybe_build_cleanup (decl), NULL_TREE);
295 /* We always set TREE_SIDE_EFFECTS so that expand_expr does not
296 ignore the TARGET_EXPR. If there really turn out to be no
297 side-effects, then the optimizer should be able to get rid of
298 whatever code is generated anyhow. */
299 TREE_SIDE_EFFECTS (t) = 1;
301 return t;
304 /* Return an undeclared local temporary of type TYPE for use in building a
305 TARGET_EXPR. */
307 static tree
308 build_local_temp (tree type)
310 tree slot = build_decl (input_location,
311 VAR_DECL, NULL_TREE, type);
312 DECL_ARTIFICIAL (slot) = 1;
313 DECL_IGNORED_P (slot) = 1;
314 DECL_CONTEXT (slot) = current_function_decl;
315 layout_decl (slot, 0);
316 return slot;
319 /* Set various status flags when building an AGGR_INIT_EXPR object T. */
321 static void
322 process_aggr_init_operands (tree t)
324 bool side_effects;
326 side_effects = TREE_SIDE_EFFECTS (t);
327 if (!side_effects)
329 int i, n;
330 n = TREE_OPERAND_LENGTH (t);
331 for (i = 1; i < n; i++)
333 tree op = TREE_OPERAND (t, i);
334 if (op && TREE_SIDE_EFFECTS (op))
336 side_effects = 1;
337 break;
341 TREE_SIDE_EFFECTS (t) = side_effects;
344 /* Build an AGGR_INIT_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE,
345 FN, and SLOT. NARGS is the number of call arguments which are specified
346 as a tree array ARGS. */
348 static tree
349 build_aggr_init_array (tree return_type, tree fn, tree slot, int nargs,
350 tree *args)
352 tree t;
353 int i;
355 t = build_vl_exp (AGGR_INIT_EXPR, nargs + 3);
356 TREE_TYPE (t) = return_type;
357 AGGR_INIT_EXPR_FN (t) = fn;
358 AGGR_INIT_EXPR_SLOT (t) = slot;
359 for (i = 0; i < nargs; i++)
360 AGGR_INIT_EXPR_ARG (t, i) = args[i];
361 process_aggr_init_operands (t);
362 return t;
365 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
366 target. TYPE is the type to be initialized.
368 Build an AGGR_INIT_EXPR to represent the initialization. This function
369 differs from build_cplus_new in that an AGGR_INIT_EXPR can only be used
370 to initialize another object, whereas a TARGET_EXPR can either
371 initialize another object or create its own temporary object, and as a
372 result building up a TARGET_EXPR requires that the type's destructor be
373 callable. */
375 tree
376 build_aggr_init_expr (tree type, tree init)
378 tree fn;
379 tree slot;
380 tree rval;
381 int is_ctor;
383 /* Make sure that we're not trying to create an instance of an
384 abstract class. */
385 abstract_virtuals_error (NULL_TREE, type);
387 if (TREE_CODE (init) == CALL_EXPR)
388 fn = CALL_EXPR_FN (init);
389 else if (TREE_CODE (init) == AGGR_INIT_EXPR)
390 fn = AGGR_INIT_EXPR_FN (init);
391 else
392 return convert (type, init);
394 is_ctor = (TREE_CODE (fn) == ADDR_EXPR
395 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
396 && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0)));
398 /* We split the CALL_EXPR into its function and its arguments here.
399 Then, in expand_expr, we put them back together. The reason for
400 this is that this expression might be a default argument
401 expression. In that case, we need a new temporary every time the
402 expression is used. That's what break_out_target_exprs does; it
403 replaces every AGGR_INIT_EXPR with a copy that uses a fresh
404 temporary slot. Then, expand_expr builds up a call-expression
405 using the new slot. */
407 /* If we don't need to use a constructor to create an object of this
408 type, don't mess with AGGR_INIT_EXPR. */
409 if (is_ctor || TREE_ADDRESSABLE (type))
411 slot = build_local_temp (type);
413 if (TREE_CODE(init) == CALL_EXPR)
414 rval = build_aggr_init_array (void_type_node, fn, slot,
415 call_expr_nargs (init),
416 CALL_EXPR_ARGP (init));
417 else
418 rval = build_aggr_init_array (void_type_node, fn, slot,
419 aggr_init_expr_nargs (init),
420 AGGR_INIT_EXPR_ARGP (init));
421 TREE_SIDE_EFFECTS (rval) = 1;
422 AGGR_INIT_VIA_CTOR_P (rval) = is_ctor;
423 TREE_NOTHROW (rval) = TREE_NOTHROW (init);
425 else
426 rval = init;
428 return rval;
431 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
432 target. TYPE is the type that this initialization should appear to
433 have.
435 Build an encapsulation of the initialization to perform
436 and return it so that it can be processed by language-independent
437 and language-specific expression expanders. */
439 tree
440 build_cplus_new (tree type, tree init)
442 tree rval = build_aggr_init_expr (type, init);
443 tree slot;
445 if (TREE_CODE (rval) == AGGR_INIT_EXPR)
446 slot = AGGR_INIT_EXPR_SLOT (rval);
447 else if (TREE_CODE (rval) == CALL_EXPR
448 || TREE_CODE (rval) == CONSTRUCTOR)
449 slot = build_local_temp (type);
450 else
451 return rval;
453 rval = build_target_expr (slot, rval);
454 TARGET_EXPR_IMPLICIT_P (rval) = 1;
456 return rval;
459 /* Return a TARGET_EXPR which expresses the initialization of an array to
460 be named later, either default-initialization or copy-initialization
461 from another array of the same type. */
463 tree
464 build_vec_init_expr (tree type, tree init)
466 tree slot;
467 tree inner_type = strip_array_types (type);
468 tree elt_init = integer_zero_node;
469 bool value_init = false;
471 /* Since we're deferring building the actual constructor calls until
472 gimplification time, we need to build one now and throw it away so
473 that the relevant constructor gets mark_used before cgraph decides
474 what functions are needed. Here we assume that init is either
475 NULL_TREE, void_type_node (indicating value-initialization), or
476 another array to copy. */
477 if (init == void_type_node)
479 elt_init = build_value_init (inner_type, tf_warning_or_error);
480 value_init = true;
481 init = NULL_TREE;
483 else
485 gcc_assert (init == NULL_TREE
486 || (same_type_ignoring_top_level_qualifiers_p
487 (type, TREE_TYPE (init))));
489 if (CLASS_TYPE_P (inner_type))
491 VEC(tree,gc) *argvec = make_tree_vector ();
492 if (init)
494 tree dummy = build_dummy_object (inner_type);
495 if (!real_lvalue_p (init))
496 dummy = move (dummy);
497 VEC_quick_push (tree, argvec, dummy);
499 elt_init
500 = build_special_member_call (NULL_TREE, complete_ctor_identifier,
501 &argvec, inner_type, LOOKUP_NORMAL,
502 tf_warning_or_error);
506 slot = build_local_temp (type);
507 init = build2 (VEC_INIT_EXPR, type, slot, init);
508 SET_EXPR_LOCATION (init, input_location);
510 if (current_function_decl
511 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
512 && potential_constant_expression (elt_init, tf_warning_or_error))
513 VEC_INIT_EXPR_IS_CONSTEXPR (init) = true;
514 VEC_INIT_EXPR_VALUE_INIT (init) = value_init;
516 init = build_target_expr (slot, init);
517 TARGET_EXPR_IMPLICIT_P (init) = 1;
519 return init;
522 tree
523 build_array_copy (tree init)
525 return build_vec_init_expr (TREE_TYPE (init), init);
528 /* Build a TARGET_EXPR using INIT to initialize a new temporary of the
529 indicated TYPE. */
531 tree
532 build_target_expr_with_type (tree init, tree type)
534 gcc_assert (!VOID_TYPE_P (type));
536 if (TREE_CODE (init) == TARGET_EXPR
537 || init == error_mark_node)
538 return init;
539 else if (CLASS_TYPE_P (type) && type_has_nontrivial_copy_init (type)
540 && !VOID_TYPE_P (TREE_TYPE (init))
541 && TREE_CODE (init) != COND_EXPR
542 && TREE_CODE (init) != CONSTRUCTOR
543 && TREE_CODE (init) != VA_ARG_EXPR)
544 /* We need to build up a copy constructor call. A void initializer
545 means we're being called from bot_manip. COND_EXPR is a special
546 case because we already have copies on the arms and we don't want
547 another one here. A CONSTRUCTOR is aggregate initialization, which
548 is handled separately. A VA_ARG_EXPR is magic creation of an
549 aggregate; there's no additional work to be done. */
550 return force_rvalue (init);
552 return force_target_expr (type, init);
555 /* Like the above function, but without the checking. This function should
556 only be used by code which is deliberately trying to subvert the type
557 system, such as call_builtin_trap. Or build_over_call, to avoid
558 infinite recursion. */
560 tree
561 force_target_expr (tree type, tree init)
563 tree slot;
565 gcc_assert (!VOID_TYPE_P (type));
567 slot = build_local_temp (type);
568 return build_target_expr (slot, init);
571 /* Like build_target_expr_with_type, but use the type of INIT. */
573 tree
574 get_target_expr (tree init)
576 if (TREE_CODE (init) == AGGR_INIT_EXPR)
577 return build_target_expr (AGGR_INIT_EXPR_SLOT (init), init);
578 else
579 return build_target_expr_with_type (init, TREE_TYPE (init));
582 /* If EXPR is a bitfield reference, convert it to the declared type of
583 the bitfield, and return the resulting expression. Otherwise,
584 return EXPR itself. */
586 tree
587 convert_bitfield_to_declared_type (tree expr)
589 tree bitfield_type;
591 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
592 if (bitfield_type)
593 expr = convert_to_integer (TYPE_MAIN_VARIANT (bitfield_type),
594 expr);
595 return expr;
598 /* EXPR is being used in an rvalue context. Return a version of EXPR
599 that is marked as an rvalue. */
601 tree
602 rvalue (tree expr)
604 tree type;
606 if (error_operand_p (expr))
607 return expr;
609 expr = mark_rvalue_use (expr);
611 /* [basic.lval]
613 Non-class rvalues always have cv-unqualified types. */
614 type = TREE_TYPE (expr);
615 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
616 type = cv_unqualified (type);
618 /* We need to do this for rvalue refs as well to get the right answer
619 from decltype; see c++/36628. */
620 if (!processing_template_decl && lvalue_or_rvalue_with_address_p (expr))
621 expr = build1 (NON_LVALUE_EXPR, type, expr);
622 else if (type != TREE_TYPE (expr))
623 expr = build_nop (type, expr);
625 return expr;
629 /* Hash an ARRAY_TYPE. K is really of type `tree'. */
631 static hashval_t
632 cplus_array_hash (const void* k)
634 hashval_t hash;
635 const_tree const t = (const_tree) k;
637 hash = TYPE_UID (TREE_TYPE (t));
638 if (TYPE_DOMAIN (t))
639 hash ^= TYPE_UID (TYPE_DOMAIN (t));
640 return hash;
643 typedef struct cplus_array_info {
644 tree type;
645 tree domain;
646 } cplus_array_info;
648 /* Compare two ARRAY_TYPEs. K1 is really of type `tree', K2 is really
649 of type `cplus_array_info*'. */
651 static int
652 cplus_array_compare (const void * k1, const void * k2)
654 const_tree const t1 = (const_tree) k1;
655 const cplus_array_info *const t2 = (const cplus_array_info*) k2;
657 return (TREE_TYPE (t1) == t2->type && TYPE_DOMAIN (t1) == t2->domain);
660 /* Hash table containing dependent array types, which are unsuitable for
661 the language-independent type hash table. */
662 static GTY ((param_is (union tree_node))) htab_t cplus_array_htab;
664 /* Like build_array_type, but handle special C++ semantics. */
666 tree
667 build_cplus_array_type (tree elt_type, tree index_type)
669 tree t;
671 if (elt_type == error_mark_node || index_type == error_mark_node)
672 return error_mark_node;
674 if (processing_template_decl
675 && (dependent_type_p (elt_type)
676 || (index_type && !TREE_CONSTANT (TYPE_MAX_VALUE (index_type)))))
678 void **e;
679 cplus_array_info cai;
680 hashval_t hash;
682 if (cplus_array_htab == NULL)
683 cplus_array_htab = htab_create_ggc (61, &cplus_array_hash,
684 &cplus_array_compare, NULL);
686 hash = TYPE_UID (elt_type);
687 if (index_type)
688 hash ^= TYPE_UID (index_type);
689 cai.type = elt_type;
690 cai.domain = index_type;
692 e = htab_find_slot_with_hash (cplus_array_htab, &cai, hash, INSERT);
693 if (*e)
694 /* We have found the type: we're done. */
695 return (tree) *e;
696 else
698 /* Build a new array type. */
699 t = cxx_make_type (ARRAY_TYPE);
700 TREE_TYPE (t) = elt_type;
701 TYPE_DOMAIN (t) = index_type;
703 /* Store it in the hash table. */
704 *e = t;
706 /* Set the canonical type for this new node. */
707 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
708 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
709 SET_TYPE_STRUCTURAL_EQUALITY (t);
710 else if (TYPE_CANONICAL (elt_type) != elt_type
711 || (index_type
712 && TYPE_CANONICAL (index_type) != index_type))
713 TYPE_CANONICAL (t)
714 = build_cplus_array_type
715 (TYPE_CANONICAL (elt_type),
716 index_type ? TYPE_CANONICAL (index_type) : index_type);
717 else
718 TYPE_CANONICAL (t) = t;
721 else
722 t = build_array_type (elt_type, index_type);
724 /* We want TYPE_MAIN_VARIANT of an array to strip cv-quals from the
725 element type as well, so fix it up if needed. */
726 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
728 tree m = build_cplus_array_type (TYPE_MAIN_VARIANT (elt_type),
729 index_type);
730 if (TYPE_MAIN_VARIANT (t) != m)
732 TYPE_MAIN_VARIANT (t) = m;
733 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
734 TYPE_NEXT_VARIANT (m) = t;
738 /* Push these needs up so that initialization takes place
739 more easily. */
740 TYPE_NEEDS_CONSTRUCTING (t)
741 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (elt_type));
742 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
743 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (elt_type));
744 return t;
747 /* Return an ARRAY_TYPE with element type ELT and length N. */
749 tree
750 build_array_of_n_type (tree elt, int n)
752 return build_cplus_array_type (elt, build_index_type (size_int (n - 1)));
755 /* Return a reference type node referring to TO_TYPE. If RVAL is
756 true, return an rvalue reference type, otherwise return an lvalue
757 reference type. If a type node exists, reuse it, otherwise create
758 a new one. */
759 tree
760 cp_build_reference_type (tree to_type, bool rval)
762 tree lvalue_ref, t;
763 lvalue_ref = build_reference_type (to_type);
764 if (!rval)
765 return lvalue_ref;
767 /* This code to create rvalue reference types is based on and tied
768 to the code creating lvalue reference types in the middle-end
769 functions build_reference_type_for_mode and build_reference_type.
771 It works by putting the rvalue reference type nodes after the
772 lvalue reference nodes in the TYPE_NEXT_REF_TO linked list, so
773 they will effectively be ignored by the middle end. */
775 for (t = lvalue_ref; (t = TYPE_NEXT_REF_TO (t)); )
776 if (TYPE_REF_IS_RVALUE (t))
777 return t;
779 t = build_distinct_type_copy (lvalue_ref);
781 TYPE_REF_IS_RVALUE (t) = true;
782 TYPE_NEXT_REF_TO (t) = TYPE_NEXT_REF_TO (lvalue_ref);
783 TYPE_NEXT_REF_TO (lvalue_ref) = t;
785 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
786 SET_TYPE_STRUCTURAL_EQUALITY (t);
787 else if (TYPE_CANONICAL (to_type) != to_type)
788 TYPE_CANONICAL (t)
789 = cp_build_reference_type (TYPE_CANONICAL (to_type), rval);
790 else
791 TYPE_CANONICAL (t) = t;
793 layout_type (t);
795 return t;
799 /* Returns EXPR cast to rvalue reference type, like std::move. */
801 tree
802 move (tree expr)
804 tree type = TREE_TYPE (expr);
805 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
806 type = cp_build_reference_type (type, /*rval*/true);
807 return build_static_cast (type, expr, tf_warning_or_error);
810 /* Used by the C++ front end to build qualified array types. However,
811 the C version of this function does not properly maintain canonical
812 types (which are not used in C). */
813 tree
814 c_build_qualified_type (tree type, int type_quals)
816 return cp_build_qualified_type (type, type_quals);
820 /* Make a variant of TYPE, qualified with the TYPE_QUALS. Handles
821 arrays correctly. In particular, if TYPE is an array of T's, and
822 TYPE_QUALS is non-empty, returns an array of qualified T's.
824 FLAGS determines how to deal with ill-formed qualifications. If
825 tf_ignore_bad_quals is set, then bad qualifications are dropped
826 (this is permitted if TYPE was introduced via a typedef or template
827 type parameter). If bad qualifications are dropped and tf_warning
828 is set, then a warning is issued for non-const qualifications. If
829 tf_ignore_bad_quals is not set and tf_error is not set, we
830 return error_mark_node. Otherwise, we issue an error, and ignore
831 the qualifications.
833 Qualification of a reference type is valid when the reference came
834 via a typedef or template type argument. [dcl.ref] No such
835 dispensation is provided for qualifying a function type. [dcl.fct]
836 DR 295 queries this and the proposed resolution brings it into line
837 with qualifying a reference. We implement the DR. We also behave
838 in a similar manner for restricting non-pointer types. */
840 tree
841 cp_build_qualified_type_real (tree type,
842 int type_quals,
843 tsubst_flags_t complain)
845 tree result;
846 int bad_quals = TYPE_UNQUALIFIED;
848 if (type == error_mark_node)
849 return type;
851 if (type_quals == cp_type_quals (type))
852 return type;
854 if (TREE_CODE (type) == ARRAY_TYPE)
856 /* In C++, the qualification really applies to the array element
857 type. Obtain the appropriately qualified element type. */
858 tree t;
859 tree element_type
860 = cp_build_qualified_type_real (TREE_TYPE (type),
861 type_quals,
862 complain);
864 if (element_type == error_mark_node)
865 return error_mark_node;
867 /* See if we already have an identically qualified type. Tests
868 should be equivalent to those in check_qualified_type. */
869 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
870 if (TREE_TYPE (t) == element_type
871 && TYPE_NAME (t) == TYPE_NAME (type)
872 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
873 && attribute_list_equal (TYPE_ATTRIBUTES (t),
874 TYPE_ATTRIBUTES (type)))
875 break;
877 if (!t)
879 t = build_cplus_array_type (element_type, TYPE_DOMAIN (type));
881 /* Keep the typedef name. */
882 if (TYPE_NAME (t) != TYPE_NAME (type))
884 t = build_variant_type_copy (t);
885 TYPE_NAME (t) = TYPE_NAME (type);
889 /* Even if we already had this variant, we update
890 TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case
891 they changed since the variant was originally created.
893 This seems hokey; if there is some way to use a previous
894 variant *without* coming through here,
895 TYPE_NEEDS_CONSTRUCTING will never be updated. */
896 TYPE_NEEDS_CONSTRUCTING (t)
897 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
898 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
899 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
900 return t;
902 else if (TYPE_PTRMEMFUNC_P (type))
904 /* For a pointer-to-member type, we can't just return a
905 cv-qualified version of the RECORD_TYPE. If we do, we
906 haven't changed the field that contains the actual pointer to
907 a method, and so TYPE_PTRMEMFUNC_FN_TYPE will be wrong. */
908 tree t;
910 t = TYPE_PTRMEMFUNC_FN_TYPE (type);
911 t = cp_build_qualified_type_real (t, type_quals, complain);
912 return build_ptrmemfunc_type (t);
914 else if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
916 tree t = PACK_EXPANSION_PATTERN (type);
918 t = cp_build_qualified_type_real (t, type_quals, complain);
919 return make_pack_expansion (t);
922 /* A reference or method type shall not be cv-qualified.
923 [dcl.ref], [dcl.fct]. This used to be an error, but as of DR 295
924 (in CD1) we always ignore extra cv-quals on functions. */
925 if (type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)
926 && (TREE_CODE (type) == REFERENCE_TYPE
927 || TREE_CODE (type) == FUNCTION_TYPE
928 || TREE_CODE (type) == METHOD_TYPE))
930 if (TREE_CODE (type) == REFERENCE_TYPE)
931 bad_quals |= type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
932 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
935 /* But preserve any function-cv-quals on a FUNCTION_TYPE. */
936 if (TREE_CODE (type) == FUNCTION_TYPE)
937 type_quals |= type_memfn_quals (type);
939 /* A restrict-qualified type must be a pointer (or reference)
940 to object or incomplete type. */
941 if ((type_quals & TYPE_QUAL_RESTRICT)
942 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
943 && TREE_CODE (type) != TYPENAME_TYPE
944 && !POINTER_TYPE_P (type))
946 bad_quals |= TYPE_QUAL_RESTRICT;
947 type_quals &= ~TYPE_QUAL_RESTRICT;
950 if (bad_quals == TYPE_UNQUALIFIED
951 || (complain & tf_ignore_bad_quals))
952 /*OK*/;
953 else if (!(complain & tf_error))
954 return error_mark_node;
955 else
957 tree bad_type = build_qualified_type (ptr_type_node, bad_quals);
958 error ("%qV qualifiers cannot be applied to %qT",
959 bad_type, type);
962 /* Retrieve (or create) the appropriately qualified variant. */
963 result = build_qualified_type (type, type_quals);
965 /* If this was a pointer-to-method type, and we just made a copy,
966 then we need to unshare the record that holds the cached
967 pointer-to-member-function type, because these will be distinct
968 between the unqualified and qualified types. */
969 if (result != type
970 && TREE_CODE (type) == POINTER_TYPE
971 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE
972 && TYPE_LANG_SPECIFIC (result) == TYPE_LANG_SPECIFIC (type))
973 TYPE_LANG_SPECIFIC (result) = NULL;
975 /* We may also have ended up building a new copy of the canonical
976 type of a pointer-to-method type, which could have the same
977 sharing problem described above. */
978 if (TYPE_CANONICAL (result) != TYPE_CANONICAL (type)
979 && TREE_CODE (type) == POINTER_TYPE
980 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE
981 && (TYPE_LANG_SPECIFIC (TYPE_CANONICAL (result))
982 == TYPE_LANG_SPECIFIC (TYPE_CANONICAL (type))))
983 TYPE_LANG_SPECIFIC (TYPE_CANONICAL (result)) = NULL;
985 return result;
988 /* Return TYPE with const and volatile removed. */
990 tree
991 cv_unqualified (tree type)
993 int quals;
995 if (type == error_mark_node)
996 return type;
998 quals = cp_type_quals (type);
999 quals &= ~(TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE);
1000 return cp_build_qualified_type (type, quals);
1003 /* Builds a qualified variant of T that is not a typedef variant.
1004 E.g. consider the following declarations:
1005 typedef const int ConstInt;
1006 typedef ConstInt* PtrConstInt;
1007 If T is PtrConstInt, this function returns a type representing
1008 const int*.
1009 In other words, if T is a typedef, the function returns the underlying type.
1010 The cv-qualification and attributes of the type returned match the
1011 input type.
1012 They will always be compatible types.
1013 The returned type is built so that all of its subtypes
1014 recursively have their typedefs stripped as well.
1016 This is different from just returning TYPE_CANONICAL (T)
1017 Because of several reasons:
1018 * If T is a type that needs structural equality
1019 its TYPE_CANONICAL (T) will be NULL.
1020 * TYPE_CANONICAL (T) desn't carry type attributes
1021 and looses template parameter names. */
1023 tree
1024 strip_typedefs (tree t)
1026 tree result = NULL, type = NULL, t0 = NULL;
1028 if (!t || t == error_mark_node || t == TYPE_CANONICAL (t))
1029 return t;
1031 gcc_assert (TYPE_P (t));
1033 switch (TREE_CODE (t))
1035 case POINTER_TYPE:
1036 type = strip_typedefs (TREE_TYPE (t));
1037 result = build_pointer_type (type);
1038 break;
1039 case REFERENCE_TYPE:
1040 type = strip_typedefs (TREE_TYPE (t));
1041 result = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
1042 break;
1043 case OFFSET_TYPE:
1044 t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t));
1045 type = strip_typedefs (TREE_TYPE (t));
1046 result = build_offset_type (t0, type);
1047 break;
1048 case RECORD_TYPE:
1049 if (TYPE_PTRMEMFUNC_P (t))
1051 t0 = strip_typedefs (TYPE_PTRMEMFUNC_FN_TYPE (t));
1052 result = build_ptrmemfunc_type (t0);
1054 break;
1055 case ARRAY_TYPE:
1056 type = strip_typedefs (TREE_TYPE (t));
1057 t0 = strip_typedefs (TYPE_DOMAIN (t));;
1058 result = build_cplus_array_type (type, t0);
1059 break;
1060 case FUNCTION_TYPE:
1061 case METHOD_TYPE:
1063 tree arg_types = NULL, arg_node, arg_type;
1064 for (arg_node = TYPE_ARG_TYPES (t);
1065 arg_node;
1066 arg_node = TREE_CHAIN (arg_node))
1068 if (arg_node == void_list_node)
1069 break;
1070 arg_type = strip_typedefs (TREE_VALUE (arg_node));
1071 gcc_assert (arg_type);
1073 arg_types =
1074 tree_cons (TREE_PURPOSE (arg_node), arg_type, arg_types);
1077 if (arg_types)
1078 arg_types = nreverse (arg_types);
1080 /* A list of parameters not ending with an ellipsis
1081 must end with void_list_node. */
1082 if (arg_node)
1083 arg_types = chainon (arg_types, void_list_node);
1085 type = strip_typedefs (TREE_TYPE (t));
1086 if (TREE_CODE (t) == METHOD_TYPE)
1088 tree class_type = TREE_TYPE (TREE_VALUE (arg_types));
1089 gcc_assert (class_type);
1090 result =
1091 build_method_type_directly (class_type, type,
1092 TREE_CHAIN (arg_types));
1094 else
1096 result = build_function_type (type,
1097 arg_types);
1098 result = apply_memfn_quals (result, type_memfn_quals (t));
1101 if (TYPE_RAISES_EXCEPTIONS (t))
1102 result = build_exception_variant (result,
1103 TYPE_RAISES_EXCEPTIONS (t));
1105 break;
1106 case TYPENAME_TYPE:
1107 result = make_typename_type (strip_typedefs (TYPE_CONTEXT (t)),
1108 TYPENAME_TYPE_FULLNAME (t),
1109 typename_type, tf_none);
1110 break;
1111 default:
1112 break;
1115 if (!result)
1116 result = TYPE_MAIN_VARIANT (t);
1117 if (TYPE_ATTRIBUTES (t))
1118 result = cp_build_type_attribute_variant (result, TYPE_ATTRIBUTES (t));
1119 return cp_build_qualified_type (result, cp_type_quals (t));
1122 /* Makes a copy of BINFO and TYPE, which is to be inherited into a
1123 graph dominated by T. If BINFO is NULL, TYPE is a dependent base,
1124 and we do a shallow copy. If BINFO is non-NULL, we do a deep copy.
1125 VIRT indicates whether TYPE is inherited virtually or not.
1126 IGO_PREV points at the previous binfo of the inheritance graph
1127 order chain. The newly copied binfo's TREE_CHAIN forms this
1128 ordering.
1130 The CLASSTYPE_VBASECLASSES vector of T is constructed in the
1131 correct order. That is in the order the bases themselves should be
1132 constructed in.
1134 The BINFO_INHERITANCE of a virtual base class points to the binfo
1135 of the most derived type. ??? We could probably change this so that
1136 BINFO_INHERITANCE becomes synonymous with BINFO_PRIMARY, and hence
1137 remove a field. They currently can only differ for primary virtual
1138 virtual bases. */
1140 tree
1141 copy_binfo (tree binfo, tree type, tree t, tree *igo_prev, int virt)
1143 tree new_binfo;
1145 if (virt)
1147 /* See if we've already made this virtual base. */
1148 new_binfo = binfo_for_vbase (type, t);
1149 if (new_binfo)
1150 return new_binfo;
1153 new_binfo = make_tree_binfo (binfo ? BINFO_N_BASE_BINFOS (binfo) : 0);
1154 BINFO_TYPE (new_binfo) = type;
1156 /* Chain it into the inheritance graph. */
1157 TREE_CHAIN (*igo_prev) = new_binfo;
1158 *igo_prev = new_binfo;
1160 if (binfo)
1162 int ix;
1163 tree base_binfo;
1165 gcc_assert (!BINFO_DEPENDENT_BASE_P (binfo));
1166 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), type));
1168 BINFO_OFFSET (new_binfo) = BINFO_OFFSET (binfo);
1169 BINFO_VIRTUALS (new_binfo) = BINFO_VIRTUALS (binfo);
1171 /* We do not need to copy the accesses, as they are read only. */
1172 BINFO_BASE_ACCESSES (new_binfo) = BINFO_BASE_ACCESSES (binfo);
1174 /* Recursively copy base binfos of BINFO. */
1175 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1177 tree new_base_binfo;
1179 gcc_assert (!BINFO_DEPENDENT_BASE_P (base_binfo));
1180 new_base_binfo = copy_binfo (base_binfo, BINFO_TYPE (base_binfo),
1181 t, igo_prev,
1182 BINFO_VIRTUAL_P (base_binfo));
1184 if (!BINFO_INHERITANCE_CHAIN (new_base_binfo))
1185 BINFO_INHERITANCE_CHAIN (new_base_binfo) = new_binfo;
1186 BINFO_BASE_APPEND (new_binfo, new_base_binfo);
1189 else
1190 BINFO_DEPENDENT_BASE_P (new_binfo) = 1;
1192 if (virt)
1194 /* Push it onto the list after any virtual bases it contains
1195 will have been pushed. */
1196 VEC_quick_push (tree, CLASSTYPE_VBASECLASSES (t), new_binfo);
1197 BINFO_VIRTUAL_P (new_binfo) = 1;
1198 BINFO_INHERITANCE_CHAIN (new_binfo) = TYPE_BINFO (t);
1201 return new_binfo;
1204 /* Hashing of lists so that we don't make duplicates.
1205 The entry point is `list_hash_canon'. */
1207 /* Now here is the hash table. When recording a list, it is added
1208 to the slot whose index is the hash code mod the table size.
1209 Note that the hash table is used for several kinds of lists.
1210 While all these live in the same table, they are completely independent,
1211 and the hash code is computed differently for each of these. */
1213 static GTY ((param_is (union tree_node))) htab_t list_hash_table;
1215 struct list_proxy
1217 tree purpose;
1218 tree value;
1219 tree chain;
1222 /* Compare ENTRY (an entry in the hash table) with DATA (a list_proxy
1223 for a node we are thinking about adding). */
1225 static int
1226 list_hash_eq (const void* entry, const void* data)
1228 const_tree const t = (const_tree) entry;
1229 const struct list_proxy *const proxy = (const struct list_proxy *) data;
1231 return (TREE_VALUE (t) == proxy->value
1232 && TREE_PURPOSE (t) == proxy->purpose
1233 && TREE_CHAIN (t) == proxy->chain);
1236 /* Compute a hash code for a list (chain of TREE_LIST nodes
1237 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
1238 TREE_COMMON slots), by adding the hash codes of the individual entries. */
1240 static hashval_t
1241 list_hash_pieces (tree purpose, tree value, tree chain)
1243 hashval_t hashcode = 0;
1245 if (chain)
1246 hashcode += TREE_HASH (chain);
1248 if (value)
1249 hashcode += TREE_HASH (value);
1250 else
1251 hashcode += 1007;
1252 if (purpose)
1253 hashcode += TREE_HASH (purpose);
1254 else
1255 hashcode += 1009;
1256 return hashcode;
1259 /* Hash an already existing TREE_LIST. */
1261 static hashval_t
1262 list_hash (const void* p)
1264 const_tree const t = (const_tree) p;
1265 return list_hash_pieces (TREE_PURPOSE (t),
1266 TREE_VALUE (t),
1267 TREE_CHAIN (t));
1270 /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
1271 object for an identical list if one already exists. Otherwise, build a
1272 new one, and record it as the canonical object. */
1274 tree
1275 hash_tree_cons (tree purpose, tree value, tree chain)
1277 int hashcode = 0;
1278 void **slot;
1279 struct list_proxy proxy;
1281 /* Hash the list node. */
1282 hashcode = list_hash_pieces (purpose, value, chain);
1283 /* Create a proxy for the TREE_LIST we would like to create. We
1284 don't actually create it so as to avoid creating garbage. */
1285 proxy.purpose = purpose;
1286 proxy.value = value;
1287 proxy.chain = chain;
1288 /* See if it is already in the table. */
1289 slot = htab_find_slot_with_hash (list_hash_table, &proxy, hashcode,
1290 INSERT);
1291 /* If not, create a new node. */
1292 if (!*slot)
1293 *slot = tree_cons (purpose, value, chain);
1294 return (tree) *slot;
1297 /* Constructor for hashed lists. */
1299 tree
1300 hash_tree_chain (tree value, tree chain)
1302 return hash_tree_cons (NULL_TREE, value, chain);
1305 void
1306 debug_binfo (tree elem)
1308 HOST_WIDE_INT n;
1309 tree virtuals;
1311 fprintf (stderr, "type \"%s\", offset = " HOST_WIDE_INT_PRINT_DEC
1312 "\nvtable type:\n",
1313 TYPE_NAME_STRING (BINFO_TYPE (elem)),
1314 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
1315 debug_tree (BINFO_TYPE (elem));
1316 if (BINFO_VTABLE (elem))
1317 fprintf (stderr, "vtable decl \"%s\"\n",
1318 IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem))));
1319 else
1320 fprintf (stderr, "no vtable decl yet\n");
1321 fprintf (stderr, "virtuals:\n");
1322 virtuals = BINFO_VIRTUALS (elem);
1323 n = 0;
1325 while (virtuals)
1327 tree fndecl = TREE_VALUE (virtuals);
1328 fprintf (stderr, "%s [%ld =? %ld]\n",
1329 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
1330 (long) n, (long) TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
1331 ++n;
1332 virtuals = TREE_CHAIN (virtuals);
1336 /* Build a representation for the qualified name SCOPE::NAME. TYPE is
1337 the type of the result expression, if known, or NULL_TREE if the
1338 resulting expression is type-dependent. If TEMPLATE_P is true,
1339 NAME is known to be a template because the user explicitly used the
1340 "template" keyword after the "::".
1342 All SCOPE_REFs should be built by use of this function. */
1344 tree
1345 build_qualified_name (tree type, tree scope, tree name, bool template_p)
1347 tree t;
1348 if (type == error_mark_node
1349 || scope == error_mark_node
1350 || name == error_mark_node)
1351 return error_mark_node;
1352 t = build2 (SCOPE_REF, type, scope, name);
1353 QUALIFIED_NAME_IS_TEMPLATE (t) = template_p;
1354 if (type)
1355 t = convert_from_reference (t);
1356 return t;
1359 /* Returns nonzero if X is an expression for a (possibly overloaded)
1360 function. If "f" is a function or function template, "f", "c->f",
1361 "c.f", "C::f", and "f<int>" will all be considered possibly
1362 overloaded functions. Returns 2 if the function is actually
1363 overloaded, i.e., if it is impossible to know the type of the
1364 function without performing overload resolution. */
1367 is_overloaded_fn (tree x)
1369 /* A baselink is also considered an overloaded function. */
1370 if (TREE_CODE (x) == OFFSET_REF
1371 || TREE_CODE (x) == COMPONENT_REF)
1372 x = TREE_OPERAND (x, 1);
1373 if (BASELINK_P (x))
1374 x = BASELINK_FUNCTIONS (x);
1375 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
1376 x = TREE_OPERAND (x, 0);
1377 if (DECL_FUNCTION_TEMPLATE_P (OVL_CURRENT (x))
1378 || (TREE_CODE (x) == OVERLOAD && OVL_CHAIN (x)))
1379 return 2;
1380 return (TREE_CODE (x) == FUNCTION_DECL
1381 || TREE_CODE (x) == OVERLOAD);
1384 /* Returns true iff X is an expression for an overloaded function
1385 whose type cannot be known without performing overload
1386 resolution. */
1388 bool
1389 really_overloaded_fn (tree x)
1391 return is_overloaded_fn (x) == 2;
1394 tree
1395 get_fns (tree from)
1397 gcc_assert (is_overloaded_fn (from));
1398 /* A baselink is also considered an overloaded function. */
1399 if (TREE_CODE (from) == OFFSET_REF
1400 || TREE_CODE (from) == COMPONENT_REF)
1401 from = TREE_OPERAND (from, 1);
1402 if (BASELINK_P (from))
1403 from = BASELINK_FUNCTIONS (from);
1404 if (TREE_CODE (from) == TEMPLATE_ID_EXPR)
1405 from = TREE_OPERAND (from, 0);
1406 return from;
1409 tree
1410 get_first_fn (tree from)
1412 return OVL_CURRENT (get_fns (from));
1415 /* Return a new OVL node, concatenating it with the old one. */
1417 tree
1418 ovl_cons (tree decl, tree chain)
1420 tree result = make_node (OVERLOAD);
1421 TREE_TYPE (result) = unknown_type_node;
1422 OVL_FUNCTION (result) = decl;
1423 TREE_CHAIN (result) = chain;
1425 return result;
1428 /* Build a new overloaded function. If this is the first one,
1429 just return it; otherwise, ovl_cons the _DECLs */
1431 tree
1432 build_overload (tree decl, tree chain)
1434 if (! chain && TREE_CODE (decl) != TEMPLATE_DECL)
1435 return decl;
1436 if (chain && TREE_CODE (chain) != OVERLOAD)
1437 chain = ovl_cons (chain, NULL_TREE);
1438 return ovl_cons (decl, chain);
1442 #define PRINT_RING_SIZE 4
1444 static const char *
1445 cxx_printable_name_internal (tree decl, int v, bool translate)
1447 static unsigned int uid_ring[PRINT_RING_SIZE];
1448 static char *print_ring[PRINT_RING_SIZE];
1449 static bool trans_ring[PRINT_RING_SIZE];
1450 static int ring_counter;
1451 int i;
1453 /* Only cache functions. */
1454 if (v < 2
1455 || TREE_CODE (decl) != FUNCTION_DECL
1456 || DECL_LANG_SPECIFIC (decl) == 0)
1457 return lang_decl_name (decl, v, translate);
1459 /* See if this print name is lying around. */
1460 for (i = 0; i < PRINT_RING_SIZE; i++)
1461 if (uid_ring[i] == DECL_UID (decl) && translate == trans_ring[i])
1462 /* yes, so return it. */
1463 return print_ring[i];
1465 if (++ring_counter == PRINT_RING_SIZE)
1466 ring_counter = 0;
1468 if (current_function_decl != NULL_TREE)
1470 /* There may be both translated and untranslated versions of the
1471 name cached. */
1472 for (i = 0; i < 2; i++)
1474 if (uid_ring[ring_counter] == DECL_UID (current_function_decl))
1475 ring_counter += 1;
1476 if (ring_counter == PRINT_RING_SIZE)
1477 ring_counter = 0;
1479 gcc_assert (uid_ring[ring_counter] != DECL_UID (current_function_decl));
1482 if (print_ring[ring_counter])
1483 free (print_ring[ring_counter]);
1485 print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v, translate));
1486 uid_ring[ring_counter] = DECL_UID (decl);
1487 trans_ring[ring_counter] = translate;
1488 return print_ring[ring_counter];
1491 const char *
1492 cxx_printable_name (tree decl, int v)
1494 return cxx_printable_name_internal (decl, v, false);
1497 const char *
1498 cxx_printable_name_translate (tree decl, int v)
1500 return cxx_printable_name_internal (decl, v, true);
1503 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
1504 listed in RAISES. */
1506 tree
1507 build_exception_variant (tree type, tree raises)
1509 tree v;
1510 int type_quals;
1512 if (comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (type), ce_exact))
1513 return type;
1515 type_quals = TYPE_QUALS (type);
1516 for (v = TYPE_MAIN_VARIANT (type); v; v = TYPE_NEXT_VARIANT (v))
1517 if (check_qualified_type (v, type, type_quals)
1518 && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (v), ce_exact))
1519 return v;
1521 /* Need to build a new variant. */
1522 v = build_variant_type_copy (type);
1523 TYPE_RAISES_EXCEPTIONS (v) = raises;
1524 return v;
1527 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new
1528 BOUND_TEMPLATE_TEMPLATE_PARM bound with NEWARGS as its template
1529 arguments. */
1531 tree
1532 bind_template_template_parm (tree t, tree newargs)
1534 tree decl = TYPE_NAME (t);
1535 tree t2;
1537 t2 = cxx_make_type (BOUND_TEMPLATE_TEMPLATE_PARM);
1538 decl = build_decl (input_location,
1539 TYPE_DECL, DECL_NAME (decl), NULL_TREE);
1541 /* These nodes have to be created to reflect new TYPE_DECL and template
1542 arguments. */
1543 TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t));
1544 TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl;
1545 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
1546 = build_template_info (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t), newargs);
1548 TREE_TYPE (decl) = t2;
1549 TYPE_NAME (t2) = decl;
1550 TYPE_STUB_DECL (t2) = decl;
1551 TYPE_SIZE (t2) = 0;
1552 SET_TYPE_STRUCTURAL_EQUALITY (t2);
1554 return t2;
1557 /* Called from count_trees via walk_tree. */
1559 static tree
1560 count_trees_r (tree *tp, int *walk_subtrees, void *data)
1562 ++*((int *) data);
1564 if (TYPE_P (*tp))
1565 *walk_subtrees = 0;
1567 return NULL_TREE;
1570 /* Debugging function for measuring the rough complexity of a tree
1571 representation. */
1574 count_trees (tree t)
1576 int n_trees = 0;
1577 cp_walk_tree_without_duplicates (&t, count_trees_r, &n_trees);
1578 return n_trees;
1581 /* Called from verify_stmt_tree via walk_tree. */
1583 static tree
1584 verify_stmt_tree_r (tree* tp,
1585 int* walk_subtrees ATTRIBUTE_UNUSED ,
1586 void* data)
1588 tree t = *tp;
1589 htab_t *statements = (htab_t *) data;
1590 void **slot;
1592 if (!STATEMENT_CODE_P (TREE_CODE (t)))
1593 return NULL_TREE;
1595 /* If this statement is already present in the hash table, then
1596 there is a circularity in the statement tree. */
1597 gcc_assert (!htab_find (*statements, t));
1599 slot = htab_find_slot (*statements, t, INSERT);
1600 *slot = t;
1602 return NULL_TREE;
1605 /* Debugging function to check that the statement T has not been
1606 corrupted. For now, this function simply checks that T contains no
1607 circularities. */
1609 void
1610 verify_stmt_tree (tree t)
1612 htab_t statements;
1613 statements = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
1614 cp_walk_tree (&t, verify_stmt_tree_r, &statements, NULL);
1615 htab_delete (statements);
1618 /* Check if the type T depends on a type with no linkage and if so, return
1619 it. If RELAXED_P then do not consider a class type declared within
1620 a vague-linkage function to have no linkage. */
1622 tree
1623 no_linkage_check (tree t, bool relaxed_p)
1625 tree r;
1627 /* There's no point in checking linkage on template functions; we
1628 can't know their complete types. */
1629 if (processing_template_decl)
1630 return NULL_TREE;
1632 switch (TREE_CODE (t))
1634 case RECORD_TYPE:
1635 if (TYPE_PTRMEMFUNC_P (t))
1636 goto ptrmem;
1637 /* Lambda types that don't have mangling scope have no linkage. We
1638 check CLASSTYPE_LAMBDA_EXPR here rather than LAMBDA_TYPE_P because
1639 when we get here from pushtag none of the lambda information is
1640 set up yet, so we want to assume that the lambda has linkage and
1641 fix it up later if not. */
1642 if (CLASSTYPE_LAMBDA_EXPR (t)
1643 && LAMBDA_TYPE_EXTRA_SCOPE (t) == NULL_TREE)
1644 return t;
1645 /* Fall through. */
1646 case UNION_TYPE:
1647 if (!CLASS_TYPE_P (t))
1648 return NULL_TREE;
1649 /* Fall through. */
1650 case ENUMERAL_TYPE:
1651 /* Only treat anonymous types as having no linkage if they're at
1652 namespace scope. This is core issue 966. */
1653 if (TYPE_ANONYMOUS_P (t) && TYPE_NAMESPACE_SCOPE_P (t))
1654 return t;
1656 for (r = CP_TYPE_CONTEXT (t); ; )
1658 /* If we're a nested type of a !TREE_PUBLIC class, we might not
1659 have linkage, or we might just be in an anonymous namespace.
1660 If we're in a TREE_PUBLIC class, we have linkage. */
1661 if (TYPE_P (r) && !TREE_PUBLIC (TYPE_NAME (r)))
1662 return no_linkage_check (TYPE_CONTEXT (t), relaxed_p);
1663 else if (TREE_CODE (r) == FUNCTION_DECL)
1665 if (!relaxed_p || !vague_linkage_p (r))
1666 return t;
1667 else
1668 r = CP_DECL_CONTEXT (r);
1670 else
1671 break;
1674 return NULL_TREE;
1676 case ARRAY_TYPE:
1677 case POINTER_TYPE:
1678 case REFERENCE_TYPE:
1679 return no_linkage_check (TREE_TYPE (t), relaxed_p);
1681 case OFFSET_TYPE:
1682 ptrmem:
1683 r = no_linkage_check (TYPE_PTRMEM_POINTED_TO_TYPE (t),
1684 relaxed_p);
1685 if (r)
1686 return r;
1687 return no_linkage_check (TYPE_PTRMEM_CLASS_TYPE (t), relaxed_p);
1689 case METHOD_TYPE:
1690 r = no_linkage_check (TYPE_METHOD_BASETYPE (t), relaxed_p);
1691 if (r)
1692 return r;
1693 /* Fall through. */
1694 case FUNCTION_TYPE:
1696 tree parm;
1697 for (parm = TYPE_ARG_TYPES (t);
1698 parm && parm != void_list_node;
1699 parm = TREE_CHAIN (parm))
1701 r = no_linkage_check (TREE_VALUE (parm), relaxed_p);
1702 if (r)
1703 return r;
1705 return no_linkage_check (TREE_TYPE (t), relaxed_p);
1708 default:
1709 return NULL_TREE;
1713 #ifdef GATHER_STATISTICS
1714 extern int depth_reached;
1715 #endif
1717 void
1718 cxx_print_statistics (void)
1720 print_search_statistics ();
1721 print_class_statistics ();
1722 print_template_statistics ();
1723 #ifdef GATHER_STATISTICS
1724 fprintf (stderr, "maximum template instantiation depth reached: %d\n",
1725 depth_reached);
1726 #endif
1729 /* Return, as an INTEGER_CST node, the number of elements for TYPE
1730 (which is an ARRAY_TYPE). This counts only elements of the top
1731 array. */
1733 tree
1734 array_type_nelts_top (tree type)
1736 return fold_build2_loc (input_location,
1737 PLUS_EXPR, sizetype,
1738 array_type_nelts (type),
1739 size_one_node);
1742 /* Return, as an INTEGER_CST node, the number of elements for TYPE
1743 (which is an ARRAY_TYPE). This one is a recursive count of all
1744 ARRAY_TYPEs that are clumped together. */
1746 tree
1747 array_type_nelts_total (tree type)
1749 tree sz = array_type_nelts_top (type);
1750 type = TREE_TYPE (type);
1751 while (TREE_CODE (type) == ARRAY_TYPE)
1753 tree n = array_type_nelts_top (type);
1754 sz = fold_build2_loc (input_location,
1755 MULT_EXPR, sizetype, sz, n);
1756 type = TREE_TYPE (type);
1758 return sz;
1761 /* Called from break_out_target_exprs via mapcar. */
1763 static tree
1764 bot_manip (tree* tp, int* walk_subtrees, void* data)
1766 splay_tree target_remap = ((splay_tree) data);
1767 tree t = *tp;
1769 if (!TYPE_P (t) && TREE_CONSTANT (t) && !TREE_SIDE_EFFECTS (t))
1771 /* There can't be any TARGET_EXPRs or their slot variables below
1772 this point. */
1773 *walk_subtrees = 0;
1774 return NULL_TREE;
1776 if (TREE_CODE (t) == TARGET_EXPR)
1778 tree u;
1780 if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
1781 u = build_cplus_new (TREE_TYPE (t), TREE_OPERAND (t, 1));
1782 else
1783 u = build_target_expr_with_type (TREE_OPERAND (t, 1), TREE_TYPE (t));
1785 /* Map the old variable to the new one. */
1786 splay_tree_insert (target_remap,
1787 (splay_tree_key) TREE_OPERAND (t, 0),
1788 (splay_tree_value) TREE_OPERAND (u, 0));
1790 TREE_OPERAND (u, 1) = break_out_target_exprs (TREE_OPERAND (u, 1));
1792 /* Replace the old expression with the new version. */
1793 *tp = u;
1794 /* We don't have to go below this point; the recursive call to
1795 break_out_target_exprs will have handled anything below this
1796 point. */
1797 *walk_subtrees = 0;
1798 return NULL_TREE;
1801 /* Make a copy of this node. */
1802 return copy_tree_r (tp, walk_subtrees, NULL);
1805 /* Replace all remapped VAR_DECLs in T with their new equivalents.
1806 DATA is really a splay-tree mapping old variables to new
1807 variables. */
1809 static tree
1810 bot_replace (tree* t,
1811 int* walk_subtrees ATTRIBUTE_UNUSED ,
1812 void* data)
1814 splay_tree target_remap = ((splay_tree) data);
1816 if (TREE_CODE (*t) == VAR_DECL)
1818 splay_tree_node n = splay_tree_lookup (target_remap,
1819 (splay_tree_key) *t);
1820 if (n)
1821 *t = (tree) n->value;
1824 return NULL_TREE;
1827 /* When we parse a default argument expression, we may create
1828 temporary variables via TARGET_EXPRs. When we actually use the
1829 default-argument expression, we make a copy of the expression, but
1830 we must replace the temporaries with appropriate local versions. */
1832 tree
1833 break_out_target_exprs (tree t)
1835 static int target_remap_count;
1836 static splay_tree target_remap;
1838 if (!target_remap_count++)
1839 target_remap = splay_tree_new (splay_tree_compare_pointers,
1840 /*splay_tree_delete_key_fn=*/NULL,
1841 /*splay_tree_delete_value_fn=*/NULL);
1842 cp_walk_tree (&t, bot_manip, target_remap, NULL);
1843 cp_walk_tree (&t, bot_replace, target_remap, NULL);
1845 if (!--target_remap_count)
1847 splay_tree_delete (target_remap);
1848 target_remap = NULL;
1851 return t;
1854 /* Similar to `build_nt', but for template definitions of dependent
1855 expressions */
1857 tree
1858 build_min_nt (enum tree_code code, ...)
1860 tree t;
1861 int length;
1862 int i;
1863 va_list p;
1865 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
1867 va_start (p, code);
1869 t = make_node (code);
1870 length = TREE_CODE_LENGTH (code);
1872 for (i = 0; i < length; i++)
1874 tree x = va_arg (p, tree);
1875 TREE_OPERAND (t, i) = x;
1878 va_end (p);
1879 return t;
1883 /* Similar to `build', but for template definitions. */
1885 tree
1886 build_min (enum tree_code code, tree tt, ...)
1888 tree t;
1889 int length;
1890 int i;
1891 va_list p;
1893 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
1895 va_start (p, tt);
1897 t = make_node (code);
1898 length = TREE_CODE_LENGTH (code);
1899 TREE_TYPE (t) = tt;
1901 for (i = 0; i < length; i++)
1903 tree x = va_arg (p, tree);
1904 TREE_OPERAND (t, i) = x;
1905 if (x && !TYPE_P (x) && TREE_SIDE_EFFECTS (x))
1906 TREE_SIDE_EFFECTS (t) = 1;
1909 va_end (p);
1910 return t;
1913 /* Similar to `build', but for template definitions of non-dependent
1914 expressions. NON_DEP is the non-dependent expression that has been
1915 built. */
1917 tree
1918 build_min_non_dep (enum tree_code code, tree non_dep, ...)
1920 tree t;
1921 int length;
1922 int i;
1923 va_list p;
1925 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
1927 va_start (p, non_dep);
1929 t = make_node (code);
1930 length = TREE_CODE_LENGTH (code);
1931 TREE_TYPE (t) = TREE_TYPE (non_dep);
1932 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
1934 for (i = 0; i < length; i++)
1936 tree x = va_arg (p, tree);
1937 TREE_OPERAND (t, i) = x;
1940 if (code == COMPOUND_EXPR && TREE_CODE (non_dep) != COMPOUND_EXPR)
1941 /* This should not be considered a COMPOUND_EXPR, because it
1942 resolves to an overload. */
1943 COMPOUND_EXPR_OVERLOADED (t) = 1;
1945 va_end (p);
1946 return t;
1949 /* Similar to `build_nt_call_vec', but for template definitions of
1950 non-dependent expressions. NON_DEP is the non-dependent expression
1951 that has been built. */
1953 tree
1954 build_min_non_dep_call_vec (tree non_dep, tree fn, VEC(tree,gc) *argvec)
1956 tree t = build_nt_call_vec (fn, argvec);
1957 TREE_TYPE (t) = TREE_TYPE (non_dep);
1958 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
1959 return t;
1962 tree
1963 get_type_decl (tree t)
1965 if (TREE_CODE (t) == TYPE_DECL)
1966 return t;
1967 if (TYPE_P (t))
1968 return TYPE_STUB_DECL (t);
1969 gcc_assert (t == error_mark_node);
1970 return t;
1973 /* Returns the namespace that contains DECL, whether directly or
1974 indirectly. */
1976 tree
1977 decl_namespace_context (tree decl)
1979 while (1)
1981 if (TREE_CODE (decl) == NAMESPACE_DECL)
1982 return decl;
1983 else if (TYPE_P (decl))
1984 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
1985 else
1986 decl = CP_DECL_CONTEXT (decl);
1990 /* Returns true if decl is within an anonymous namespace, however deeply
1991 nested, or false otherwise. */
1993 bool
1994 decl_anon_ns_mem_p (const_tree decl)
1996 while (1)
1998 if (decl == NULL_TREE || decl == error_mark_node)
1999 return false;
2000 if (TREE_CODE (decl) == NAMESPACE_DECL
2001 && DECL_NAME (decl) == NULL_TREE)
2002 return true;
2003 /* Classes and namespaces inside anonymous namespaces have
2004 TREE_PUBLIC == 0, so we can shortcut the search. */
2005 else if (TYPE_P (decl))
2006 return (TREE_PUBLIC (TYPE_NAME (decl)) == 0);
2007 else if (TREE_CODE (decl) == NAMESPACE_DECL)
2008 return (TREE_PUBLIC (decl) == 0);
2009 else
2010 decl = DECL_CONTEXT (decl);
2014 /* Return truthvalue of whether T1 is the same tree structure as T2.
2015 Return 1 if they are the same. Return 0 if they are different. */
2017 bool
2018 cp_tree_equal (tree t1, tree t2)
2020 enum tree_code code1, code2;
2022 if (t1 == t2)
2023 return true;
2024 if (!t1 || !t2)
2025 return false;
2027 for (code1 = TREE_CODE (t1);
2028 CONVERT_EXPR_CODE_P (code1)
2029 || code1 == NON_LVALUE_EXPR;
2030 code1 = TREE_CODE (t1))
2031 t1 = TREE_OPERAND (t1, 0);
2032 for (code2 = TREE_CODE (t2);
2033 CONVERT_EXPR_CODE_P (code2)
2034 || code1 == NON_LVALUE_EXPR;
2035 code2 = TREE_CODE (t2))
2036 t2 = TREE_OPERAND (t2, 0);
2038 /* They might have become equal now. */
2039 if (t1 == t2)
2040 return true;
2042 if (code1 != code2)
2043 return false;
2045 switch (code1)
2047 case INTEGER_CST:
2048 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
2049 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
2051 case REAL_CST:
2052 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2054 case STRING_CST:
2055 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2056 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2057 TREE_STRING_LENGTH (t1));
2059 case FIXED_CST:
2060 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
2061 TREE_FIXED_CST (t2));
2063 case COMPLEX_CST:
2064 return cp_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
2065 && cp_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
2067 case CONSTRUCTOR:
2068 /* We need to do this when determining whether or not two
2069 non-type pointer to member function template arguments
2070 are the same. */
2071 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
2072 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
2073 return false;
2075 tree field, value;
2076 unsigned int i;
2077 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
2079 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
2080 if (!cp_tree_equal (field, elt2->index)
2081 || !cp_tree_equal (value, elt2->value))
2082 return false;
2085 return true;
2087 case TREE_LIST:
2088 if (!cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
2089 return false;
2090 if (!cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
2091 return false;
2092 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
2094 case SAVE_EXPR:
2095 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2097 case CALL_EXPR:
2099 tree arg1, arg2;
2100 call_expr_arg_iterator iter1, iter2;
2101 if (!cp_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
2102 return false;
2103 for (arg1 = first_call_expr_arg (t1, &iter1),
2104 arg2 = first_call_expr_arg (t2, &iter2);
2105 arg1 && arg2;
2106 arg1 = next_call_expr_arg (&iter1),
2107 arg2 = next_call_expr_arg (&iter2))
2108 if (!cp_tree_equal (arg1, arg2))
2109 return false;
2110 if (arg1 || arg2)
2111 return false;
2112 return true;
2115 case TARGET_EXPR:
2117 tree o1 = TREE_OPERAND (t1, 0);
2118 tree o2 = TREE_OPERAND (t2, 0);
2120 /* Special case: if either target is an unallocated VAR_DECL,
2121 it means that it's going to be unified with whatever the
2122 TARGET_EXPR is really supposed to initialize, so treat it
2123 as being equivalent to anything. */
2124 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
2125 && !DECL_RTL_SET_P (o1))
2126 /*Nop*/;
2127 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
2128 && !DECL_RTL_SET_P (o2))
2129 /*Nop*/;
2130 else if (!cp_tree_equal (o1, o2))
2131 return false;
2133 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
2136 case WITH_CLEANUP_EXPR:
2137 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
2138 return false;
2139 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
2141 case COMPONENT_REF:
2142 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
2143 return false;
2144 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2146 case PARM_DECL:
2147 /* For comparing uses of parameters in late-specified return types
2148 with an out-of-class definition of the function. */
2149 if (same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
2150 && DECL_PARM_INDEX (t1) == DECL_PARM_INDEX (t2))
2151 return true;
2152 else
2153 return false;
2155 case VAR_DECL:
2156 case CONST_DECL:
2157 case FUNCTION_DECL:
2158 case TEMPLATE_DECL:
2159 case IDENTIFIER_NODE:
2160 case SSA_NAME:
2161 return false;
2163 case BASELINK:
2164 return (BASELINK_BINFO (t1) == BASELINK_BINFO (t2)
2165 && BASELINK_ACCESS_BINFO (t1) == BASELINK_ACCESS_BINFO (t2)
2166 && cp_tree_equal (BASELINK_FUNCTIONS (t1),
2167 BASELINK_FUNCTIONS (t2)));
2169 case TEMPLATE_PARM_INDEX:
2170 return (TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
2171 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2)
2172 && (TEMPLATE_PARM_PARAMETER_PACK (t1)
2173 == TEMPLATE_PARM_PARAMETER_PACK (t2))
2174 && same_type_p (TREE_TYPE (TEMPLATE_PARM_DECL (t1)),
2175 TREE_TYPE (TEMPLATE_PARM_DECL (t2))));
2177 case TEMPLATE_ID_EXPR:
2179 unsigned ix;
2180 tree vec1, vec2;
2182 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
2183 return false;
2184 vec1 = TREE_OPERAND (t1, 1);
2185 vec2 = TREE_OPERAND (t2, 1);
2187 if (!vec1 || !vec2)
2188 return !vec1 && !vec2;
2190 if (TREE_VEC_LENGTH (vec1) != TREE_VEC_LENGTH (vec2))
2191 return false;
2193 for (ix = TREE_VEC_LENGTH (vec1); ix--;)
2194 if (!cp_tree_equal (TREE_VEC_ELT (vec1, ix),
2195 TREE_VEC_ELT (vec2, ix)))
2196 return false;
2198 return true;
2201 case SIZEOF_EXPR:
2202 case ALIGNOF_EXPR:
2204 tree o1 = TREE_OPERAND (t1, 0);
2205 tree o2 = TREE_OPERAND (t2, 0);
2207 if (TREE_CODE (o1) != TREE_CODE (o2))
2208 return false;
2209 if (TYPE_P (o1))
2210 return same_type_p (o1, o2);
2211 else
2212 return cp_tree_equal (o1, o2);
2215 case MODOP_EXPR:
2217 tree t1_op1, t2_op1;
2219 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
2220 return false;
2222 t1_op1 = TREE_OPERAND (t1, 1);
2223 t2_op1 = TREE_OPERAND (t2, 1);
2224 if (TREE_CODE (t1_op1) != TREE_CODE (t2_op1))
2225 return false;
2227 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t2, 2));
2230 case PTRMEM_CST:
2231 /* Two pointer-to-members are the same if they point to the same
2232 field or function in the same class. */
2233 if (PTRMEM_CST_MEMBER (t1) != PTRMEM_CST_MEMBER (t2))
2234 return false;
2236 return same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2));
2238 case OVERLOAD:
2239 if (OVL_FUNCTION (t1) != OVL_FUNCTION (t2))
2240 return false;
2241 return cp_tree_equal (OVL_CHAIN (t1), OVL_CHAIN (t2));
2243 case TRAIT_EXPR:
2244 if (TRAIT_EXPR_KIND (t1) != TRAIT_EXPR_KIND (t2))
2245 return false;
2246 return same_type_p (TRAIT_EXPR_TYPE1 (t1), TRAIT_EXPR_TYPE1 (t2))
2247 && same_type_p (TRAIT_EXPR_TYPE2 (t1), TRAIT_EXPR_TYPE2 (t2));
2249 case CAST_EXPR:
2250 case STATIC_CAST_EXPR:
2251 case REINTERPRET_CAST_EXPR:
2252 case CONST_CAST_EXPR:
2253 case DYNAMIC_CAST_EXPR:
2254 case NEW_EXPR:
2255 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2256 return false;
2257 /* Now compare operands as usual. */
2258 break;
2260 default:
2261 break;
2264 switch (TREE_CODE_CLASS (code1))
2266 case tcc_unary:
2267 case tcc_binary:
2268 case tcc_comparison:
2269 case tcc_expression:
2270 case tcc_vl_exp:
2271 case tcc_reference:
2272 case tcc_statement:
2274 int i, n;
2276 n = TREE_OPERAND_LENGTH (t1);
2277 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
2278 && n != TREE_OPERAND_LENGTH (t2))
2279 return false;
2281 for (i = 0; i < n; ++i)
2282 if (!cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
2283 return false;
2285 return true;
2288 case tcc_type:
2289 return same_type_p (t1, t2);
2290 default:
2291 gcc_unreachable ();
2293 /* We can get here with --disable-checking. */
2294 return false;
2297 /* The type of ARG when used as an lvalue. */
2299 tree
2300 lvalue_type (tree arg)
2302 tree type = TREE_TYPE (arg);
2303 return type;
2306 /* The type of ARG for printing error messages; denote lvalues with
2307 reference types. */
2309 tree
2310 error_type (tree arg)
2312 tree type = TREE_TYPE (arg);
2314 if (TREE_CODE (type) == ARRAY_TYPE)
2316 else if (TREE_CODE (type) == ERROR_MARK)
2318 else if (real_lvalue_p (arg))
2319 type = build_reference_type (lvalue_type (arg));
2320 else if (MAYBE_CLASS_TYPE_P (type))
2321 type = lvalue_type (arg);
2323 return type;
2326 /* Does FUNCTION use a variable-length argument list? */
2329 varargs_function_p (const_tree function)
2331 return stdarg_p (TREE_TYPE (function));
2334 /* Returns 1 if decl is a member of a class. */
2337 member_p (const_tree decl)
2339 const_tree const ctx = DECL_CONTEXT (decl);
2340 return (ctx && TYPE_P (ctx));
2343 /* Create a placeholder for member access where we don't actually have an
2344 object that the access is against. */
2346 tree
2347 build_dummy_object (tree type)
2349 tree decl = build1 (NOP_EXPR, build_pointer_type (type), void_zero_node);
2350 return cp_build_indirect_ref (decl, RO_NULL, tf_warning_or_error);
2353 /* We've gotten a reference to a member of TYPE. Return *this if appropriate,
2354 or a dummy object otherwise. If BINFOP is non-0, it is filled with the
2355 binfo path from current_class_type to TYPE, or 0. */
2357 tree
2358 maybe_dummy_object (tree type, tree* binfop)
2360 tree decl, context;
2361 tree binfo;
2362 tree current = current_nonlambda_class_type ();
2364 if (current
2365 && (binfo = lookup_base (current, type, ba_any, NULL)))
2366 context = current;
2367 else
2369 /* Reference from a nested class member function. */
2370 context = type;
2371 binfo = TYPE_BINFO (type);
2374 if (binfop)
2375 *binfop = binfo;
2377 if (current_class_ref && context == current_class_type
2378 /* Kludge: Make sure that current_class_type is actually
2379 correct. It might not be if we're in the middle of
2380 tsubst_default_argument. */
2381 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (current_class_ref)),
2382 current_class_type))
2383 decl = current_class_ref;
2384 else if (current != current_class_type
2385 && context == nonlambda_method_basetype ())
2386 /* In a lambda, need to go through 'this' capture. */
2387 decl = (cp_build_indirect_ref
2388 ((lambda_expr_this_capture
2389 (CLASSTYPE_LAMBDA_EXPR (current_class_type))),
2390 RO_NULL, tf_warning_or_error));
2391 else
2392 decl = build_dummy_object (context);
2394 return decl;
2397 /* Returns 1 if OB is a placeholder object, or a pointer to one. */
2400 is_dummy_object (const_tree ob)
2402 if (TREE_CODE (ob) == INDIRECT_REF)
2403 ob = TREE_OPERAND (ob, 0);
2404 return (TREE_CODE (ob) == NOP_EXPR
2405 && TREE_OPERAND (ob, 0) == void_zero_node);
2408 /* Returns 1 iff type T is something we want to treat as a scalar type for
2409 the purpose of deciding whether it is trivial/POD/standard-layout. */
2411 static bool
2412 scalarish_type_p (const_tree t)
2414 if (t == error_mark_node)
2415 return 1;
2417 return (SCALAR_TYPE_P (t)
2418 || TREE_CODE (t) == VECTOR_TYPE);
2421 /* Returns true iff T requires non-trivial default initialization. */
2423 bool
2424 type_has_nontrivial_default_init (const_tree t)
2426 t = strip_array_types (CONST_CAST_TREE (t));
2428 if (CLASS_TYPE_P (t))
2429 return TYPE_HAS_COMPLEX_DFLT (t);
2430 else
2431 return 0;
2434 /* Returns true iff copying an object of type T (including via move
2435 constructor) is non-trivial. That is, T has no non-trivial copy
2436 constructors and no non-trivial move constructors. */
2438 bool
2439 type_has_nontrivial_copy_init (const_tree t)
2441 t = strip_array_types (CONST_CAST_TREE (t));
2443 if (CLASS_TYPE_P (t))
2445 gcc_assert (COMPLETE_TYPE_P (t));
2446 return ((TYPE_HAS_COPY_CTOR (t)
2447 && TYPE_HAS_COMPLEX_COPY_CTOR (t))
2448 || TYPE_HAS_COMPLEX_MOVE_CTOR (t));
2450 else
2451 return 0;
2454 /* Returns 1 iff type T is a trivially copyable type, as defined in
2455 [basic.types] and [class]. */
2457 bool
2458 trivially_copyable_p (const_tree t)
2460 t = strip_array_types (CONST_CAST_TREE (t));
2462 if (CLASS_TYPE_P (t))
2463 return ((!TYPE_HAS_COPY_CTOR (t)
2464 || !TYPE_HAS_COMPLEX_COPY_CTOR (t))
2465 && !TYPE_HAS_COMPLEX_MOVE_CTOR (t)
2466 && (!TYPE_HAS_COPY_ASSIGN (t)
2467 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (t))
2468 && !TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
2469 && TYPE_HAS_TRIVIAL_DESTRUCTOR (t));
2470 else
2471 return scalarish_type_p (t);
2474 /* Returns 1 iff type T is a trivial type, as defined in [basic.types] and
2475 [class]. */
2477 bool
2478 trivial_type_p (const_tree t)
2480 t = strip_array_types (CONST_CAST_TREE (t));
2482 if (CLASS_TYPE_P (t))
2483 return (TYPE_HAS_TRIVIAL_DFLT (t)
2484 && trivially_copyable_p (t));
2485 else
2486 return scalarish_type_p (t);
2489 /* Returns 1 iff type T is a POD type, as defined in [basic.types]. */
2491 bool
2492 pod_type_p (const_tree t)
2494 /* This CONST_CAST is okay because strip_array_types returns its
2495 argument unmodified and we assign it to a const_tree. */
2496 t = strip_array_types (CONST_CAST_TREE(t));
2498 if (!CLASS_TYPE_P (t))
2499 return scalarish_type_p (t);
2500 else if (cxx_dialect > cxx98)
2501 /* [class]/10: A POD struct is a class that is both a trivial class and a
2502 standard-layout class, and has no non-static data members of type
2503 non-POD struct, non-POD union (or array of such types).
2505 We don't need to check individual members because if a member is
2506 non-std-layout or non-trivial, the class will be too. */
2507 return (std_layout_type_p (t) && trivial_type_p (t));
2508 else
2509 /* The C++98 definition of POD is different. */
2510 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
2513 /* Returns true iff T is POD for the purpose of layout, as defined in the
2514 C++ ABI. */
2516 bool
2517 layout_pod_type_p (const_tree t)
2519 t = strip_array_types (CONST_CAST_TREE (t));
2521 if (CLASS_TYPE_P (t))
2522 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
2523 else
2524 return scalarish_type_p (t);
2527 /* Returns true iff T is a standard-layout type, as defined in
2528 [basic.types]. */
2530 bool
2531 std_layout_type_p (const_tree t)
2533 t = strip_array_types (CONST_CAST_TREE (t));
2535 if (CLASS_TYPE_P (t))
2536 return !CLASSTYPE_NON_STD_LAYOUT (t);
2537 else
2538 return scalarish_type_p (t);
2541 /* Nonzero iff type T is a class template implicit specialization. */
2543 bool
2544 class_tmpl_impl_spec_p (const_tree t)
2546 return CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INSTANTIATION (t);
2549 /* Returns 1 iff zero initialization of type T means actually storing
2550 zeros in it. */
2553 zero_init_p (const_tree t)
2555 /* This CONST_CAST is okay because strip_array_types returns its
2556 argument unmodified and we assign it to a const_tree. */
2557 t = strip_array_types (CONST_CAST_TREE(t));
2559 if (t == error_mark_node)
2560 return 1;
2562 /* NULL pointers to data members are initialized with -1. */
2563 if (TYPE_PTRMEM_P (t))
2564 return 0;
2566 /* Classes that contain types that can't be zero-initialized, cannot
2567 be zero-initialized themselves. */
2568 if (CLASS_TYPE_P (t) && CLASSTYPE_NON_ZERO_INIT_P (t))
2569 return 0;
2571 return 1;
2574 /* Table of valid C++ attributes. */
2575 const struct attribute_spec cxx_attribute_table[] =
2577 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2578 { "java_interface", 0, 0, false, false, false, handle_java_interface_attribute },
2579 { "com_interface", 0, 0, false, false, false, handle_com_interface_attribute },
2580 { "init_priority", 1, 1, true, false, false, handle_init_priority_attribute },
2581 { NULL, 0, 0, false, false, false, NULL }
2584 /* Handle a "java_interface" attribute; arguments as in
2585 struct attribute_spec.handler. */
2586 static tree
2587 handle_java_interface_attribute (tree* node,
2588 tree name,
2589 tree args ATTRIBUTE_UNUSED ,
2590 int flags,
2591 bool* no_add_attrs)
2593 if (DECL_P (*node)
2594 || !CLASS_TYPE_P (*node)
2595 || !TYPE_FOR_JAVA (*node))
2597 error ("%qE attribute can only be applied to Java class definitions",
2598 name);
2599 *no_add_attrs = true;
2600 return NULL_TREE;
2602 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
2603 *node = build_variant_type_copy (*node);
2604 TYPE_JAVA_INTERFACE (*node) = 1;
2606 return NULL_TREE;
2609 /* Handle a "com_interface" attribute; arguments as in
2610 struct attribute_spec.handler. */
2611 static tree
2612 handle_com_interface_attribute (tree* node,
2613 tree name,
2614 tree args ATTRIBUTE_UNUSED ,
2615 int flags ATTRIBUTE_UNUSED ,
2616 bool* no_add_attrs)
2618 static int warned;
2620 *no_add_attrs = true;
2622 if (DECL_P (*node)
2623 || !CLASS_TYPE_P (*node)
2624 || *node != TYPE_MAIN_VARIANT (*node))
2626 warning (OPT_Wattributes, "%qE attribute can only be applied "
2627 "to class definitions", name);
2628 return NULL_TREE;
2631 if (!warned++)
2632 warning (0, "%qE is obsolete; g++ vtables are now COM-compatible by default",
2633 name);
2635 return NULL_TREE;
2638 /* Handle an "init_priority" attribute; arguments as in
2639 struct attribute_spec.handler. */
2640 static tree
2641 handle_init_priority_attribute (tree* node,
2642 tree name,
2643 tree args,
2644 int flags ATTRIBUTE_UNUSED ,
2645 bool* no_add_attrs)
2647 tree initp_expr = TREE_VALUE (args);
2648 tree decl = *node;
2649 tree type = TREE_TYPE (decl);
2650 int pri;
2652 STRIP_NOPS (initp_expr);
2654 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
2656 error ("requested init_priority is not an integer constant");
2657 *no_add_attrs = true;
2658 return NULL_TREE;
2661 pri = TREE_INT_CST_LOW (initp_expr);
2663 type = strip_array_types (type);
2665 if (decl == NULL_TREE
2666 || TREE_CODE (decl) != VAR_DECL
2667 || !TREE_STATIC (decl)
2668 || DECL_EXTERNAL (decl)
2669 || (TREE_CODE (type) != RECORD_TYPE
2670 && TREE_CODE (type) != UNION_TYPE)
2671 /* Static objects in functions are initialized the
2672 first time control passes through that
2673 function. This is not precise enough to pin down an
2674 init_priority value, so don't allow it. */
2675 || current_function_decl)
2677 error ("can only use %qE attribute on file-scope definitions "
2678 "of objects of class type", name);
2679 *no_add_attrs = true;
2680 return NULL_TREE;
2683 if (pri > MAX_INIT_PRIORITY || pri <= 0)
2685 error ("requested init_priority is out of range");
2686 *no_add_attrs = true;
2687 return NULL_TREE;
2690 /* Check for init_priorities that are reserved for
2691 language and runtime support implementations.*/
2692 if (pri <= MAX_RESERVED_INIT_PRIORITY)
2694 warning
2695 (0, "requested init_priority is reserved for internal use");
2698 if (SUPPORTS_INIT_PRIORITY)
2700 SET_DECL_INIT_PRIORITY (decl, pri);
2701 DECL_HAS_INIT_PRIORITY_P (decl) = 1;
2702 return NULL_TREE;
2704 else
2706 error ("%qE attribute is not supported on this platform", name);
2707 *no_add_attrs = true;
2708 return NULL_TREE;
2712 /* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the
2713 thing pointed to by the constant. */
2715 tree
2716 make_ptrmem_cst (tree type, tree member)
2718 tree ptrmem_cst = make_node (PTRMEM_CST);
2719 TREE_TYPE (ptrmem_cst) = type;
2720 PTRMEM_CST_MEMBER (ptrmem_cst) = member;
2721 return ptrmem_cst;
2724 /* Build a variant of TYPE that has the indicated ATTRIBUTES. May
2725 return an existing type if an appropriate type already exists. */
2727 tree
2728 cp_build_type_attribute_variant (tree type, tree attributes)
2730 tree new_type;
2732 new_type = build_type_attribute_variant (type, attributes);
2733 if (TREE_CODE (new_type) == FUNCTION_TYPE
2734 || TREE_CODE (new_type) == METHOD_TYPE)
2735 new_type = build_exception_variant (new_type,
2736 TYPE_RAISES_EXCEPTIONS (type));
2738 /* Making a new main variant of a class type is broken. */
2739 gcc_assert (!CLASS_TYPE_P (type) || new_type == type);
2741 return new_type;
2744 /* Return TRUE if TYPE1 and TYPE2 are identical for type hashing purposes.
2745 Called only after doing all language independent checks. Only
2746 to check TYPE_RAISES_EXCEPTIONS for FUNCTION_TYPE, the rest is already
2747 compared in type_hash_eq. */
2749 bool
2750 cxx_type_hash_eq (const_tree typea, const_tree typeb)
2752 gcc_assert (TREE_CODE (typea) == FUNCTION_TYPE);
2754 return comp_except_specs (TYPE_RAISES_EXCEPTIONS (typea),
2755 TYPE_RAISES_EXCEPTIONS (typeb), ce_exact);
2758 /* Apply FUNC to all language-specific sub-trees of TP in a pre-order
2759 traversal. Called from walk_tree. */
2761 tree
2762 cp_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func,
2763 void *data, struct pointer_set_t *pset)
2765 enum tree_code code = TREE_CODE (*tp);
2766 tree result;
2768 #define WALK_SUBTREE(NODE) \
2769 do \
2771 result = cp_walk_tree (&(NODE), func, data, pset); \
2772 if (result) goto out; \
2774 while (0)
2776 /* Not one of the easy cases. We must explicitly go through the
2777 children. */
2778 result = NULL_TREE;
2779 switch (code)
2781 case DEFAULT_ARG:
2782 case TEMPLATE_TEMPLATE_PARM:
2783 case BOUND_TEMPLATE_TEMPLATE_PARM:
2784 case UNBOUND_CLASS_TEMPLATE:
2785 case TEMPLATE_PARM_INDEX:
2786 case TEMPLATE_TYPE_PARM:
2787 case TYPENAME_TYPE:
2788 case TYPEOF_TYPE:
2789 /* None of these have subtrees other than those already walked
2790 above. */
2791 *walk_subtrees_p = 0;
2792 break;
2794 case BASELINK:
2795 WALK_SUBTREE (BASELINK_FUNCTIONS (*tp));
2796 *walk_subtrees_p = 0;
2797 break;
2799 case PTRMEM_CST:
2800 WALK_SUBTREE (TREE_TYPE (*tp));
2801 *walk_subtrees_p = 0;
2802 break;
2804 case TREE_LIST:
2805 WALK_SUBTREE (TREE_PURPOSE (*tp));
2806 break;
2808 case OVERLOAD:
2809 WALK_SUBTREE (OVL_FUNCTION (*tp));
2810 WALK_SUBTREE (OVL_CHAIN (*tp));
2811 *walk_subtrees_p = 0;
2812 break;
2814 case USING_DECL:
2815 WALK_SUBTREE (DECL_NAME (*tp));
2816 WALK_SUBTREE (USING_DECL_SCOPE (*tp));
2817 WALK_SUBTREE (USING_DECL_DECLS (*tp));
2818 *walk_subtrees_p = 0;
2819 break;
2821 case RECORD_TYPE:
2822 if (TYPE_PTRMEMFUNC_P (*tp))
2823 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (*tp));
2824 break;
2826 case TYPE_ARGUMENT_PACK:
2827 case NONTYPE_ARGUMENT_PACK:
2829 tree args = ARGUMENT_PACK_ARGS (*tp);
2830 int i, len = TREE_VEC_LENGTH (args);
2831 for (i = 0; i < len; i++)
2832 WALK_SUBTREE (TREE_VEC_ELT (args, i));
2834 break;
2836 case TYPE_PACK_EXPANSION:
2837 WALK_SUBTREE (TREE_TYPE (*tp));
2838 *walk_subtrees_p = 0;
2839 break;
2841 case EXPR_PACK_EXPANSION:
2842 WALK_SUBTREE (TREE_OPERAND (*tp, 0));
2843 *walk_subtrees_p = 0;
2844 break;
2846 case CAST_EXPR:
2847 case REINTERPRET_CAST_EXPR:
2848 case STATIC_CAST_EXPR:
2849 case CONST_CAST_EXPR:
2850 case DYNAMIC_CAST_EXPR:
2851 if (TREE_TYPE (*tp))
2852 WALK_SUBTREE (TREE_TYPE (*tp));
2855 int i;
2856 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (*tp)); ++i)
2857 WALK_SUBTREE (TREE_OPERAND (*tp, i));
2859 *walk_subtrees_p = 0;
2860 break;
2862 case TRAIT_EXPR:
2863 WALK_SUBTREE (TRAIT_EXPR_TYPE1 (*tp));
2864 WALK_SUBTREE (TRAIT_EXPR_TYPE2 (*tp));
2865 *walk_subtrees_p = 0;
2866 break;
2868 case DECLTYPE_TYPE:
2869 WALK_SUBTREE (DECLTYPE_TYPE_EXPR (*tp));
2870 *walk_subtrees_p = 0;
2871 break;
2874 default:
2875 return NULL_TREE;
2878 /* We didn't find what we were looking for. */
2879 out:
2880 return result;
2882 #undef WALK_SUBTREE
2885 /* Like save_expr, but for C++. */
2887 tree
2888 cp_save_expr (tree expr)
2890 /* There is no reason to create a SAVE_EXPR within a template; if
2891 needed, we can create the SAVE_EXPR when instantiating the
2892 template. Furthermore, the middle-end cannot handle C++-specific
2893 tree codes. */
2894 if (processing_template_decl)
2895 return expr;
2896 return save_expr (expr);
2899 /* Initialize tree.c. */
2901 void
2902 init_tree (void)
2904 list_hash_table = htab_create_ggc (31, list_hash, list_hash_eq, NULL);
2907 /* Returns the kind of special function that DECL (a FUNCTION_DECL)
2908 is. Note that sfk_none is zero, so this function can be used as a
2909 predicate to test whether or not DECL is a special function. */
2911 special_function_kind
2912 special_function_p (const_tree decl)
2914 /* Rather than doing all this stuff with magic names, we should
2915 probably have a field of type `special_function_kind' in
2916 DECL_LANG_SPECIFIC. */
2917 if (DECL_COPY_CONSTRUCTOR_P (decl))
2918 return sfk_copy_constructor;
2919 if (DECL_MOVE_CONSTRUCTOR_P (decl))
2920 return sfk_move_constructor;
2921 if (DECL_CONSTRUCTOR_P (decl))
2922 return sfk_constructor;
2923 if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
2925 if (copy_fn_p (decl))
2926 return sfk_copy_assignment;
2927 if (move_fn_p (decl))
2928 return sfk_move_assignment;
2930 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
2931 return sfk_destructor;
2932 if (DECL_COMPLETE_DESTRUCTOR_P (decl))
2933 return sfk_complete_destructor;
2934 if (DECL_BASE_DESTRUCTOR_P (decl))
2935 return sfk_base_destructor;
2936 if (DECL_DELETING_DESTRUCTOR_P (decl))
2937 return sfk_deleting_destructor;
2938 if (DECL_CONV_FN_P (decl))
2939 return sfk_conversion;
2941 return sfk_none;
2944 /* Returns nonzero if TYPE is a character type, including wchar_t. */
2947 char_type_p (tree type)
2949 return (same_type_p (type, char_type_node)
2950 || same_type_p (type, unsigned_char_type_node)
2951 || same_type_p (type, signed_char_type_node)
2952 || same_type_p (type, char16_type_node)
2953 || same_type_p (type, char32_type_node)
2954 || same_type_p (type, wchar_type_node));
2957 /* Returns the kind of linkage associated with the indicated DECL. Th
2958 value returned is as specified by the language standard; it is
2959 independent of implementation details regarding template
2960 instantiation, etc. For example, it is possible that a declaration
2961 to which this function assigns external linkage would not show up
2962 as a global symbol when you run `nm' on the resulting object file. */
2964 linkage_kind
2965 decl_linkage (tree decl)
2967 /* This function doesn't attempt to calculate the linkage from first
2968 principles as given in [basic.link]. Instead, it makes use of
2969 the fact that we have already set TREE_PUBLIC appropriately, and
2970 then handles a few special cases. Ideally, we would calculate
2971 linkage first, and then transform that into a concrete
2972 implementation. */
2974 /* Things that don't have names have no linkage. */
2975 if (!DECL_NAME (decl))
2976 return lk_none;
2978 /* Fields have no linkage. */
2979 if (TREE_CODE (decl) == FIELD_DECL)
2980 return lk_none;
2982 /* Things that are TREE_PUBLIC have external linkage. */
2983 if (TREE_PUBLIC (decl))
2984 return lk_external;
2986 if (TREE_CODE (decl) == NAMESPACE_DECL)
2987 return lk_external;
2989 /* Linkage of a CONST_DECL depends on the linkage of the enumeration
2990 type. */
2991 if (TREE_CODE (decl) == CONST_DECL)
2992 return decl_linkage (TYPE_NAME (TREE_TYPE (decl)));
2994 /* Some things that are not TREE_PUBLIC have external linkage, too.
2995 For example, on targets that don't have weak symbols, we make all
2996 template instantiations have internal linkage (in the object
2997 file), but the symbols should still be treated as having external
2998 linkage from the point of view of the language. */
2999 if ((TREE_CODE (decl) == FUNCTION_DECL
3000 || TREE_CODE (decl) == VAR_DECL)
3001 && DECL_COMDAT (decl))
3002 return lk_external;
3004 /* Things in local scope do not have linkage, if they don't have
3005 TREE_PUBLIC set. */
3006 if (decl_function_context (decl))
3007 return lk_none;
3009 /* Members of the anonymous namespace also have TREE_PUBLIC unset, but
3010 are considered to have external linkage for language purposes. DECLs
3011 really meant to have internal linkage have DECL_THIS_STATIC set. */
3012 if (TREE_CODE (decl) == TYPE_DECL)
3013 return lk_external;
3014 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3016 if (!DECL_THIS_STATIC (decl))
3017 return lk_external;
3019 /* Static data members and static member functions from classes
3020 in anonymous namespace also don't have TREE_PUBLIC set. */
3021 if (DECL_CLASS_CONTEXT (decl))
3022 return lk_external;
3025 /* Everything else has internal linkage. */
3026 return lk_internal;
3029 /* Returns the storage duration of the object or reference associated with
3030 the indicated DECL, which should be a VAR_DECL or PARM_DECL. */
3032 duration_kind
3033 decl_storage_duration (tree decl)
3035 if (TREE_CODE (decl) == PARM_DECL)
3036 return dk_auto;
3037 if (TREE_CODE (decl) == FUNCTION_DECL)
3038 return dk_static;
3039 gcc_assert (TREE_CODE (decl) == VAR_DECL);
3040 if (!TREE_STATIC (decl)
3041 && !DECL_EXTERNAL (decl))
3042 return dk_auto;
3043 if (DECL_THREAD_LOCAL_P (decl))
3044 return dk_thread;
3045 return dk_static;
3048 /* EXP is an expression that we want to pre-evaluate. Returns (in
3049 *INITP) an expression that will perform the pre-evaluation. The
3050 value returned by this function is a side-effect free expression
3051 equivalent to the pre-evaluated expression. Callers must ensure
3052 that *INITP is evaluated before EXP. */
3054 tree
3055 stabilize_expr (tree exp, tree* initp)
3057 tree init_expr;
3059 if (!TREE_SIDE_EFFECTS (exp))
3060 init_expr = NULL_TREE;
3061 /* There are no expressions with REFERENCE_TYPE, but there can be call
3062 arguments with such a type; just treat it as a pointer. */
3063 else if (TREE_CODE (TREE_TYPE (exp)) == REFERENCE_TYPE
3064 || !lvalue_or_rvalue_with_address_p (exp))
3066 init_expr = get_target_expr (exp);
3067 exp = TARGET_EXPR_SLOT (init_expr);
3069 else
3071 bool xval = !real_lvalue_p (exp);
3072 exp = cp_build_addr_expr (exp, tf_warning_or_error);
3073 init_expr = get_target_expr (exp);
3074 exp = TARGET_EXPR_SLOT (init_expr);
3075 exp = cp_build_indirect_ref (exp, RO_NULL, tf_warning_or_error);
3076 if (xval)
3077 exp = move (exp);
3079 *initp = init_expr;
3081 gcc_assert (!TREE_SIDE_EFFECTS (exp));
3082 return exp;
3085 /* Add NEW_EXPR, an expression whose value we don't care about, after the
3086 similar expression ORIG. */
3088 tree
3089 add_stmt_to_compound (tree orig, tree new_expr)
3091 if (!new_expr || !TREE_SIDE_EFFECTS (new_expr))
3092 return orig;
3093 if (!orig || !TREE_SIDE_EFFECTS (orig))
3094 return new_expr;
3095 return build2 (COMPOUND_EXPR, void_type_node, orig, new_expr);
3098 /* Like stabilize_expr, but for a call whose arguments we want to
3099 pre-evaluate. CALL is modified in place to use the pre-evaluated
3100 arguments, while, upon return, *INITP contains an expression to
3101 compute the arguments. */
3103 void
3104 stabilize_call (tree call, tree *initp)
3106 tree inits = NULL_TREE;
3107 int i;
3108 int nargs = call_expr_nargs (call);
3110 if (call == error_mark_node || processing_template_decl)
3112 *initp = NULL_TREE;
3113 return;
3116 gcc_assert (TREE_CODE (call) == CALL_EXPR);
3118 for (i = 0; i < nargs; i++)
3120 tree init;
3121 CALL_EXPR_ARG (call, i) =
3122 stabilize_expr (CALL_EXPR_ARG (call, i), &init);
3123 inits = add_stmt_to_compound (inits, init);
3126 *initp = inits;
3129 /* Like stabilize_expr, but for an AGGR_INIT_EXPR whose arguments we want
3130 to pre-evaluate. CALL is modified in place to use the pre-evaluated
3131 arguments, while, upon return, *INITP contains an expression to
3132 compute the arguments. */
3134 void
3135 stabilize_aggr_init (tree call, tree *initp)
3137 tree inits = NULL_TREE;
3138 int i;
3139 int nargs = aggr_init_expr_nargs (call);
3141 if (call == error_mark_node)
3142 return;
3144 gcc_assert (TREE_CODE (call) == AGGR_INIT_EXPR);
3146 for (i = 0; i < nargs; i++)
3148 tree init;
3149 AGGR_INIT_EXPR_ARG (call, i) =
3150 stabilize_expr (AGGR_INIT_EXPR_ARG (call, i), &init);
3151 inits = add_stmt_to_compound (inits, init);
3154 *initp = inits;
3157 /* Like stabilize_expr, but for an initialization.
3159 If the initialization is for an object of class type, this function
3160 takes care not to introduce additional temporaries.
3162 Returns TRUE iff the expression was successfully pre-evaluated,
3163 i.e., if INIT is now side-effect free, except for, possible, a
3164 single call to a constructor. */
3166 bool
3167 stabilize_init (tree init, tree *initp)
3169 tree t = init;
3171 *initp = NULL_TREE;
3173 if (t == error_mark_node || processing_template_decl)
3174 return true;
3176 if (TREE_CODE (t) == INIT_EXPR
3177 && TREE_CODE (TREE_OPERAND (t, 1)) != TARGET_EXPR
3178 && TREE_CODE (TREE_OPERAND (t, 1)) != AGGR_INIT_EXPR)
3180 TREE_OPERAND (t, 1) = stabilize_expr (TREE_OPERAND (t, 1), initp);
3181 return true;
3184 if (TREE_CODE (t) == INIT_EXPR)
3185 t = TREE_OPERAND (t, 1);
3186 if (TREE_CODE (t) == TARGET_EXPR)
3187 t = TARGET_EXPR_INITIAL (t);
3188 if (TREE_CODE (t) == COMPOUND_EXPR)
3189 t = expr_last (t);
3190 if (TREE_CODE (t) == CONSTRUCTOR
3191 && EMPTY_CONSTRUCTOR_P (t))
3192 /* Default-initialization. */
3193 return true;
3195 /* If the initializer is a COND_EXPR, we can't preevaluate
3196 anything. */
3197 if (TREE_CODE (t) == COND_EXPR)
3198 return false;
3200 if (TREE_CODE (t) == CALL_EXPR)
3202 stabilize_call (t, initp);
3203 return true;
3206 if (TREE_CODE (t) == AGGR_INIT_EXPR)
3208 stabilize_aggr_init (t, initp);
3209 return true;
3212 /* The initialization is being performed via a bitwise copy -- and
3213 the item copied may have side effects. */
3214 return TREE_SIDE_EFFECTS (init);
3217 /* Like "fold", but should be used whenever we might be processing the
3218 body of a template. */
3220 tree
3221 fold_if_not_in_template (tree expr)
3223 /* In the body of a template, there is never any need to call
3224 "fold". We will call fold later when actually instantiating the
3225 template. Integral constant expressions in templates will be
3226 evaluated via fold_non_dependent_expr, as necessary. */
3227 if (processing_template_decl)
3228 return expr;
3230 /* Fold C++ front-end specific tree codes. */
3231 if (TREE_CODE (expr) == UNARY_PLUS_EXPR)
3232 return fold_convert (TREE_TYPE (expr), TREE_OPERAND (expr, 0));
3234 return fold (expr);
3237 /* Returns true if a cast to TYPE may appear in an integral constant
3238 expression. */
3240 bool
3241 cast_valid_in_integral_constant_expression_p (tree type)
3243 return (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3244 || cxx_dialect >= cxx0x
3245 || dependent_type_p (type)
3246 || type == error_mark_node);
3249 /* Return true if we need to fix linkage information of DECL. */
3251 static bool
3252 cp_fix_function_decl_p (tree decl)
3254 /* Skip if DECL is not externally visible. */
3255 if (!TREE_PUBLIC (decl))
3256 return false;
3258 /* We need to fix DECL if it a appears to be exported but with no
3259 function body. Thunks do not have CFGs and we may need to
3260 handle them specially later. */
3261 if (!gimple_has_body_p (decl)
3262 && !DECL_THUNK_P (decl)
3263 && !DECL_EXTERNAL (decl))
3265 struct cgraph_node *node = cgraph_get_node (decl);
3267 /* Don't fix same_body aliases. Although they don't have their own
3268 CFG, they share it with what they alias to. */
3269 if (!node
3270 || node->decl == decl
3271 || !node->same_body)
3272 return true;
3275 return false;
3278 /* Clean the C++ specific parts of the tree T. */
3280 void
3281 cp_free_lang_data (tree t)
3283 if (TREE_CODE (t) == METHOD_TYPE
3284 || TREE_CODE (t) == FUNCTION_TYPE)
3286 /* Default args are not interesting anymore. */
3287 tree argtypes = TYPE_ARG_TYPES (t);
3288 while (argtypes)
3290 TREE_PURPOSE (argtypes) = 0;
3291 argtypes = TREE_CHAIN (argtypes);
3294 else if (TREE_CODE (t) == FUNCTION_DECL
3295 && cp_fix_function_decl_p (t))
3297 /* If T is used in this translation unit at all, the definition
3298 must exist somewhere else since we have decided to not emit it
3299 in this TU. So make it an external reference. */
3300 DECL_EXTERNAL (t) = 1;
3301 TREE_STATIC (t) = 0;
3303 if (CP_AGGREGATE_TYPE_P (t)
3304 && TYPE_NAME (t))
3306 tree name = TYPE_NAME (t);
3307 if (TREE_CODE (name) == TYPE_DECL)
3308 name = DECL_NAME (name);
3309 /* Drop anonymous names. */
3310 if (name != NULL_TREE
3311 && ANON_AGGRNAME_P (name))
3312 TYPE_NAME (t) = NULL_TREE;
3314 if (TREE_CODE (t) == NAMESPACE_DECL)
3316 /* The list of users of a namespace isn't useful for the middle-end
3317 or debug generators. */
3318 DECL_NAMESPACE_USERS (t) = NULL_TREE;
3319 /* Neither do we need the leftover chaining of namespaces
3320 from the binding level. */
3321 DECL_CHAIN (t) = NULL_TREE;
3325 /* Stub for c-common. Please keep in sync with c-decl.c.
3326 FIXME: If address space support is target specific, then this
3327 should be a C target hook. But currently this is not possible,
3328 because this function is called via REGISTER_TARGET_PRAGMAS. */
3329 void
3330 c_register_addr_space (const char *word ATTRIBUTE_UNUSED,
3331 addr_space_t as ATTRIBUTE_UNUSED)
3336 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
3337 /* Complain that some language-specific thing hanging off a tree
3338 node has been accessed improperly. */
3340 void
3341 lang_check_failed (const char* file, int line, const char* function)
3343 internal_error ("lang_* check: failed in %s, at %s:%d",
3344 function, trim_filename (file), line);
3346 #endif /* ENABLE_TREE_CHECKING */
3348 #include "gt-cp-tree.h"