PR c++/51494
[official-gcc.git] / gcc / cp / tree.c
blob178b80aa24fdabed8b47c79ebf0867ad5b6a5413
1 /* Language-dependent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "cp-tree.h"
27 #include "flags.h"
28 #include "tree-inline.h"
29 #include "debug.h"
30 #include "convert.h"
31 #include "cgraph.h"
32 #include "splay-tree.h"
33 #include "gimple.h" /* gimple_has_body_p */
34 #include "hash-table.h"
36 static tree bot_manip (tree *, int *, void *);
37 static tree bot_replace (tree *, int *, void *);
38 static int list_hash_eq (const void *, const void *);
39 static hashval_t list_hash_pieces (tree, tree, tree);
40 static hashval_t list_hash (const void *);
41 static tree build_target_expr (tree, tree, tsubst_flags_t);
42 static tree count_trees_r (tree *, int *, void *);
43 static tree verify_stmt_tree_r (tree *, int *, void *);
44 static tree build_local_temp (tree);
46 static tree handle_java_interface_attribute (tree *, tree, tree, int, bool *);
47 static tree handle_com_interface_attribute (tree *, tree, tree, int, bool *);
48 static tree handle_init_priority_attribute (tree *, tree, tree, int, bool *);
49 static tree handle_abi_tag_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 (REFERENCE_REF_P (ref))
65 return lvalue_kind (TREE_OPERAND (ref, 0));
67 if (TREE_TYPE (ref)
68 && TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
70 /* unnamed rvalue references are rvalues */
71 if (TYPE_REF_IS_RVALUE (TREE_TYPE (ref))
72 && TREE_CODE (ref) != PARM_DECL
73 && TREE_CODE (ref) != VAR_DECL
74 && TREE_CODE (ref) != COMPONENT_REF
75 /* Functions are always lvalues. */
76 && TREE_CODE (TREE_TYPE (TREE_TYPE (ref))) != FUNCTION_TYPE)
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 ARROW_EXPR:
143 case ARRAY_REF:
144 case PARM_DECL:
145 case RESULT_DECL:
146 return clk_ordinary;
148 /* A scope ref in a template, left as SCOPE_REF to support later
149 access checking. */
150 case SCOPE_REF:
151 gcc_assert (!type_dependent_expression_p (CONST_CAST_TREE (ref)));
153 tree op = TREE_OPERAND (ref, 1);
154 if (TREE_CODE (op) == FIELD_DECL)
155 return (DECL_C_BIT_FIELD (op) ? clk_bitfield : clk_ordinary);
156 else
157 return lvalue_kind (op);
160 case MAX_EXPR:
161 case MIN_EXPR:
162 /* Disallow <? and >? as lvalues if either argument side-effects. */
163 if (TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 0))
164 || TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 1)))
165 return clk_none;
166 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
167 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1));
168 break;
170 case COND_EXPR:
171 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1)
172 ? TREE_OPERAND (ref, 1)
173 : TREE_OPERAND (ref, 0));
174 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 2));
175 break;
177 case MODIFY_EXPR:
178 case TYPEID_EXPR:
179 return clk_ordinary;
181 case COMPOUND_EXPR:
182 return lvalue_kind (TREE_OPERAND (ref, 1));
184 case TARGET_EXPR:
185 return clk_class;
187 case VA_ARG_EXPR:
188 return (CLASS_TYPE_P (TREE_TYPE (ref)) ? clk_class : clk_none);
190 case CALL_EXPR:
191 /* We can see calls outside of TARGET_EXPR in templates. */
192 if (CLASS_TYPE_P (TREE_TYPE (ref)))
193 return clk_class;
194 return clk_none;
196 case FUNCTION_DECL:
197 /* All functions (except non-static-member functions) are
198 lvalues. */
199 return (DECL_NONSTATIC_MEMBER_FUNCTION_P (ref)
200 ? clk_none : clk_ordinary);
202 case BASELINK:
203 /* We now represent a reference to a single static member function
204 with a BASELINK. */
205 /* This CONST_CAST is okay because BASELINK_FUNCTIONS returns
206 its argument unmodified and we assign it to a const_tree. */
207 return lvalue_kind (BASELINK_FUNCTIONS (CONST_CAST_TREE (ref)));
209 case NON_DEPENDENT_EXPR:
210 /* We just return clk_ordinary for NON_DEPENDENT_EXPR in C++98, but
211 in C++11 lvalues don't bind to rvalue references, so we need to
212 work harder to avoid bogus errors (c++/44870). */
213 if (cxx_dialect < cxx0x)
214 return clk_ordinary;
215 else
216 return lvalue_kind (TREE_OPERAND (ref, 0));
218 default:
219 if (!TREE_TYPE (ref))
220 return clk_none;
221 if (CLASS_TYPE_P (TREE_TYPE (ref)))
222 return clk_class;
223 break;
226 /* If one operand is not an lvalue at all, then this expression is
227 not an lvalue. */
228 if (!op1_lvalue_kind || !op2_lvalue_kind)
229 return clk_none;
231 /* Otherwise, it's an lvalue, and it has all the odd properties
232 contributed by either operand. */
233 op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind;
234 /* It's not an ordinary lvalue if it involves any other kind. */
235 if ((op1_lvalue_kind & ~clk_ordinary) != clk_none)
236 op1_lvalue_kind &= ~clk_ordinary;
237 /* It can't be both a pseudo-lvalue and a non-addressable lvalue.
238 A COND_EXPR of those should be wrapped in a TARGET_EXPR. */
239 if ((op1_lvalue_kind & (clk_rvalueref|clk_class))
240 && (op1_lvalue_kind & (clk_bitfield|clk_packed)))
241 op1_lvalue_kind = clk_none;
242 return op1_lvalue_kind;
245 /* Returns the kind of lvalue that REF is, in the sense of
246 [basic.lval]. This function should really be named lvalue_p; it
247 computes the C++ definition of lvalue. */
249 cp_lvalue_kind
250 real_lvalue_p (const_tree ref)
252 cp_lvalue_kind kind = lvalue_kind (ref);
253 if (kind & (clk_rvalueref|clk_class))
254 return clk_none;
255 else
256 return kind;
259 /* This differs from real_lvalue_p in that class rvalues are considered
260 lvalues. */
262 bool
263 lvalue_p (const_tree ref)
265 return (lvalue_kind (ref) != clk_none);
268 /* This differs from real_lvalue_p in that rvalues formed by dereferencing
269 rvalue references are considered rvalues. */
271 bool
272 lvalue_or_rvalue_with_address_p (const_tree ref)
274 cp_lvalue_kind kind = lvalue_kind (ref);
275 if (kind & clk_class)
276 return false;
277 else
278 return (kind != clk_none);
281 /* Returns true if REF is an xvalue, false otherwise. */
283 bool
284 xvalue_p (const_tree ref)
286 return (lvalue_kind (ref) == clk_rvalueref);
289 /* Test whether DECL is a builtin that may appear in a
290 constant-expression. */
292 bool
293 builtin_valid_in_constant_expr_p (const_tree decl)
295 /* At present BUILT_IN_CONSTANT_P is the only builtin we're allowing
296 in constant-expressions. We may want to add other builtins later. */
297 return DECL_IS_BUILTIN_CONSTANT_P (decl);
300 /* Build a TARGET_EXPR, initializing the DECL with the VALUE. */
302 static tree
303 build_target_expr (tree decl, tree value, tsubst_flags_t complain)
305 tree t;
306 tree type = TREE_TYPE (decl);
308 #ifdef ENABLE_CHECKING
309 gcc_assert (VOID_TYPE_P (TREE_TYPE (value))
310 || TREE_TYPE (decl) == TREE_TYPE (value)
311 /* On ARM ctors return 'this'. */
312 || (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
313 && TREE_CODE (value) == CALL_EXPR)
314 || useless_type_conversion_p (TREE_TYPE (decl),
315 TREE_TYPE (value)));
316 #endif
318 t = cxx_maybe_build_cleanup (decl, complain);
319 if (t == error_mark_node)
320 return error_mark_node;
321 t = build4 (TARGET_EXPR, type, decl, value, t, NULL_TREE);
322 /* We always set TREE_SIDE_EFFECTS so that expand_expr does not
323 ignore the TARGET_EXPR. If there really turn out to be no
324 side-effects, then the optimizer should be able to get rid of
325 whatever code is generated anyhow. */
326 TREE_SIDE_EFFECTS (t) = 1;
328 return t;
331 /* Return an undeclared local temporary of type TYPE for use in building a
332 TARGET_EXPR. */
334 static tree
335 build_local_temp (tree type)
337 tree slot = build_decl (input_location,
338 VAR_DECL, NULL_TREE, type);
339 DECL_ARTIFICIAL (slot) = 1;
340 DECL_IGNORED_P (slot) = 1;
341 DECL_CONTEXT (slot) = current_function_decl;
342 layout_decl (slot, 0);
343 return slot;
346 /* Set various status flags when building an AGGR_INIT_EXPR object T. */
348 static void
349 process_aggr_init_operands (tree t)
351 bool side_effects;
353 side_effects = TREE_SIDE_EFFECTS (t);
354 if (!side_effects)
356 int i, n;
357 n = TREE_OPERAND_LENGTH (t);
358 for (i = 1; i < n; i++)
360 tree op = TREE_OPERAND (t, i);
361 if (op && TREE_SIDE_EFFECTS (op))
363 side_effects = 1;
364 break;
368 TREE_SIDE_EFFECTS (t) = side_effects;
371 /* Build an AGGR_INIT_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE,
372 FN, and SLOT. NARGS is the number of call arguments which are specified
373 as a tree array ARGS. */
375 static tree
376 build_aggr_init_array (tree return_type, tree fn, tree slot, int nargs,
377 tree *args)
379 tree t;
380 int i;
382 t = build_vl_exp (AGGR_INIT_EXPR, nargs + 3);
383 TREE_TYPE (t) = return_type;
384 AGGR_INIT_EXPR_FN (t) = fn;
385 AGGR_INIT_EXPR_SLOT (t) = slot;
386 for (i = 0; i < nargs; i++)
387 AGGR_INIT_EXPR_ARG (t, i) = args[i];
388 process_aggr_init_operands (t);
389 return t;
392 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
393 target. TYPE is the type to be initialized.
395 Build an AGGR_INIT_EXPR to represent the initialization. This function
396 differs from build_cplus_new in that an AGGR_INIT_EXPR can only be used
397 to initialize another object, whereas a TARGET_EXPR can either
398 initialize another object or create its own temporary object, and as a
399 result building up a TARGET_EXPR requires that the type's destructor be
400 callable. */
402 tree
403 build_aggr_init_expr (tree type, tree init)
405 tree fn;
406 tree slot;
407 tree rval;
408 int is_ctor;
410 /* Don't build AGGR_INIT_EXPR in a template. */
411 if (processing_template_decl)
412 return init;
414 if (TREE_CODE (init) == CALL_EXPR)
415 fn = CALL_EXPR_FN (init);
416 else if (TREE_CODE (init) == AGGR_INIT_EXPR)
417 fn = AGGR_INIT_EXPR_FN (init);
418 else
419 return convert (type, init);
421 is_ctor = (TREE_CODE (fn) == ADDR_EXPR
422 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
423 && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0)));
425 /* We split the CALL_EXPR into its function and its arguments here.
426 Then, in expand_expr, we put them back together. The reason for
427 this is that this expression might be a default argument
428 expression. In that case, we need a new temporary every time the
429 expression is used. That's what break_out_target_exprs does; it
430 replaces every AGGR_INIT_EXPR with a copy that uses a fresh
431 temporary slot. Then, expand_expr builds up a call-expression
432 using the new slot. */
434 /* If we don't need to use a constructor to create an object of this
435 type, don't mess with AGGR_INIT_EXPR. */
436 if (is_ctor || TREE_ADDRESSABLE (type))
438 slot = build_local_temp (type);
440 if (TREE_CODE(init) == CALL_EXPR)
441 rval = build_aggr_init_array (void_type_node, fn, slot,
442 call_expr_nargs (init),
443 CALL_EXPR_ARGP (init));
444 else
445 rval = build_aggr_init_array (void_type_node, fn, slot,
446 aggr_init_expr_nargs (init),
447 AGGR_INIT_EXPR_ARGP (init));
448 TREE_SIDE_EFFECTS (rval) = 1;
449 AGGR_INIT_VIA_CTOR_P (rval) = is_ctor;
450 TREE_NOTHROW (rval) = TREE_NOTHROW (init);
452 else
453 rval = init;
455 return rval;
458 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
459 target. TYPE is the type that this initialization should appear to
460 have.
462 Build an encapsulation of the initialization to perform
463 and return it so that it can be processed by language-independent
464 and language-specific expression expanders. */
466 tree
467 build_cplus_new (tree type, tree init, tsubst_flags_t complain)
469 tree rval = build_aggr_init_expr (type, init);
470 tree slot;
472 /* Make sure that we're not trying to create an instance of an
473 abstract class. */
474 if (abstract_virtuals_error_sfinae (NULL_TREE, type, complain))
475 return error_mark_node;
477 if (TREE_CODE (rval) == AGGR_INIT_EXPR)
478 slot = AGGR_INIT_EXPR_SLOT (rval);
479 else if (TREE_CODE (rval) == CALL_EXPR
480 || TREE_CODE (rval) == CONSTRUCTOR)
481 slot = build_local_temp (type);
482 else
483 return rval;
485 rval = build_target_expr (slot, rval, complain);
487 if (rval != error_mark_node)
488 TARGET_EXPR_IMPLICIT_P (rval) = 1;
490 return rval;
493 /* Subroutine of build_vec_init_expr: Build up a single element
494 intialization as a proxy for the full array initialization to get things
495 marked as used and any appropriate diagnostics.
497 Since we're deferring building the actual constructor calls until
498 gimplification time, we need to build one now and throw it away so
499 that the relevant constructor gets mark_used before cgraph decides
500 what functions are needed. Here we assume that init is either
501 NULL_TREE, void_type_node (indicating value-initialization), or
502 another array to copy. */
504 static tree
505 build_vec_init_elt (tree type, tree init, tsubst_flags_t complain)
507 tree inner_type = strip_array_types (type);
508 vec<tree, va_gc> *argvec;
510 if (integer_zerop (array_type_nelts_total (type))
511 || !CLASS_TYPE_P (inner_type))
512 /* No interesting initialization to do. */
513 return integer_zero_node;
514 else if (init == void_type_node)
515 return build_value_init (inner_type, complain);
517 gcc_assert (init == NULL_TREE
518 || (same_type_ignoring_top_level_qualifiers_p
519 (type, TREE_TYPE (init))));
521 argvec = make_tree_vector ();
522 if (init)
524 tree init_type = strip_array_types (TREE_TYPE (init));
525 tree dummy = build_dummy_object (init_type);
526 if (!real_lvalue_p (init))
527 dummy = move (dummy);
528 argvec->quick_push (dummy);
530 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
531 &argvec, inner_type, LOOKUP_NORMAL,
532 complain);
533 release_tree_vector (argvec);
535 /* For a trivial constructor, build_over_call creates a TARGET_EXPR. But
536 we don't want one here because we aren't creating a temporary. */
537 if (TREE_CODE (init) == TARGET_EXPR)
538 init = TARGET_EXPR_INITIAL (init);
540 return init;
543 /* Return a TARGET_EXPR which expresses the initialization of an array to
544 be named later, either default-initialization or copy-initialization
545 from another array of the same type. */
547 tree
548 build_vec_init_expr (tree type, tree init, tsubst_flags_t complain)
550 tree slot;
551 bool value_init = false;
552 tree elt_init = build_vec_init_elt (type, init, complain);
554 if (init == void_type_node)
556 value_init = true;
557 init = NULL_TREE;
560 slot = build_local_temp (type);
561 init = build2 (VEC_INIT_EXPR, type, slot, init);
562 TREE_SIDE_EFFECTS (init) = true;
563 SET_EXPR_LOCATION (init, input_location);
565 if (cxx_dialect >= cxx0x
566 && potential_constant_expression (elt_init))
567 VEC_INIT_EXPR_IS_CONSTEXPR (init) = true;
568 VEC_INIT_EXPR_VALUE_INIT (init) = value_init;
570 return init;
573 /* Give a helpful diagnostic for a non-constexpr VEC_INIT_EXPR in a context
574 that requires a constant expression. */
576 void
577 diagnose_non_constexpr_vec_init (tree expr)
579 tree type = TREE_TYPE (VEC_INIT_EXPR_SLOT (expr));
580 tree init, elt_init;
581 if (VEC_INIT_EXPR_VALUE_INIT (expr))
582 init = void_type_node;
583 else
584 init = VEC_INIT_EXPR_INIT (expr);
586 elt_init = build_vec_init_elt (type, init, tf_warning_or_error);
587 require_potential_constant_expression (elt_init);
590 tree
591 build_array_copy (tree init)
593 return build_vec_init_expr (TREE_TYPE (init), init, tf_warning_or_error);
596 /* Build a TARGET_EXPR using INIT to initialize a new temporary of the
597 indicated TYPE. */
599 tree
600 build_target_expr_with_type (tree init, tree type, tsubst_flags_t complain)
602 gcc_assert (!VOID_TYPE_P (type));
604 if (TREE_CODE (init) == TARGET_EXPR
605 || init == error_mark_node)
606 return init;
607 else if (CLASS_TYPE_P (type) && type_has_nontrivial_copy_init (type)
608 && !VOID_TYPE_P (TREE_TYPE (init))
609 && TREE_CODE (init) != COND_EXPR
610 && TREE_CODE (init) != CONSTRUCTOR
611 && TREE_CODE (init) != VA_ARG_EXPR)
612 /* We need to build up a copy constructor call. A void initializer
613 means we're being called from bot_manip. COND_EXPR is a special
614 case because we already have copies on the arms and we don't want
615 another one here. A CONSTRUCTOR is aggregate initialization, which
616 is handled separately. A VA_ARG_EXPR is magic creation of an
617 aggregate; there's no additional work to be done. */
618 return force_rvalue (init, complain);
620 return force_target_expr (type, init, complain);
623 /* Like the above function, but without the checking. This function should
624 only be used by code which is deliberately trying to subvert the type
625 system, such as call_builtin_trap. Or build_over_call, to avoid
626 infinite recursion. */
628 tree
629 force_target_expr (tree type, tree init, tsubst_flags_t complain)
631 tree slot;
633 gcc_assert (!VOID_TYPE_P (type));
635 slot = build_local_temp (type);
636 return build_target_expr (slot, init, complain);
639 /* Like build_target_expr_with_type, but use the type of INIT. */
641 tree
642 get_target_expr_sfinae (tree init, tsubst_flags_t complain)
644 if (TREE_CODE (init) == AGGR_INIT_EXPR)
645 return build_target_expr (AGGR_INIT_EXPR_SLOT (init), init, complain);
646 else if (TREE_CODE (init) == VEC_INIT_EXPR)
647 return build_target_expr (VEC_INIT_EXPR_SLOT (init), init, complain);
648 else
649 return build_target_expr_with_type (init, TREE_TYPE (init), complain);
652 tree
653 get_target_expr (tree init)
655 return get_target_expr_sfinae (init, tf_warning_or_error);
658 /* If EXPR is a bitfield reference, convert it to the declared type of
659 the bitfield, and return the resulting expression. Otherwise,
660 return EXPR itself. */
662 tree
663 convert_bitfield_to_declared_type (tree expr)
665 tree bitfield_type;
667 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
668 if (bitfield_type)
669 expr = convert_to_integer (TYPE_MAIN_VARIANT (bitfield_type),
670 expr);
671 return expr;
674 /* EXPR is being used in an rvalue context. Return a version of EXPR
675 that is marked as an rvalue. */
677 tree
678 rvalue (tree expr)
680 tree type;
682 if (error_operand_p (expr))
683 return expr;
685 expr = mark_rvalue_use (expr);
687 /* [basic.lval]
689 Non-class rvalues always have cv-unqualified types. */
690 type = TREE_TYPE (expr);
691 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
692 type = cv_unqualified (type);
694 /* We need to do this for rvalue refs as well to get the right answer
695 from decltype; see c++/36628. */
696 if (!processing_template_decl && lvalue_or_rvalue_with_address_p (expr))
697 expr = build1 (NON_LVALUE_EXPR, type, expr);
698 else if (type != TREE_TYPE (expr))
699 expr = build_nop (type, expr);
701 return expr;
705 /* Hash an ARRAY_TYPE. K is really of type `tree'. */
707 static hashval_t
708 cplus_array_hash (const void* k)
710 hashval_t hash;
711 const_tree const t = (const_tree) k;
713 hash = TYPE_UID (TREE_TYPE (t));
714 if (TYPE_DOMAIN (t))
715 hash ^= TYPE_UID (TYPE_DOMAIN (t));
716 return hash;
719 typedef struct cplus_array_info {
720 tree type;
721 tree domain;
722 } cplus_array_info;
724 /* Compare two ARRAY_TYPEs. K1 is really of type `tree', K2 is really
725 of type `cplus_array_info*'. */
727 static int
728 cplus_array_compare (const void * k1, const void * k2)
730 const_tree const t1 = (const_tree) k1;
731 const cplus_array_info *const t2 = (const cplus_array_info*) k2;
733 return (TREE_TYPE (t1) == t2->type && TYPE_DOMAIN (t1) == t2->domain);
736 /* Hash table containing dependent array types, which are unsuitable for
737 the language-independent type hash table. */
738 static GTY ((param_is (union tree_node))) htab_t cplus_array_htab;
740 /* Like build_array_type, but handle special C++ semantics. */
742 tree
743 build_cplus_array_type (tree elt_type, tree index_type)
745 tree t;
747 if (elt_type == error_mark_node || index_type == error_mark_node)
748 return error_mark_node;
750 if (processing_template_decl
751 && (dependent_type_p (elt_type)
752 || (index_type && !TREE_CONSTANT (TYPE_MAX_VALUE (index_type)))))
754 void **e;
755 cplus_array_info cai;
756 hashval_t hash;
758 if (cplus_array_htab == NULL)
759 cplus_array_htab = htab_create_ggc (61, &cplus_array_hash,
760 &cplus_array_compare, NULL);
762 hash = TYPE_UID (elt_type);
763 if (index_type)
764 hash ^= TYPE_UID (index_type);
765 cai.type = elt_type;
766 cai.domain = index_type;
768 e = htab_find_slot_with_hash (cplus_array_htab, &cai, hash, INSERT);
769 if (*e)
770 /* We have found the type: we're done. */
771 return (tree) *e;
772 else
774 /* Build a new array type. */
775 t = cxx_make_type (ARRAY_TYPE);
776 TREE_TYPE (t) = elt_type;
777 TYPE_DOMAIN (t) = index_type;
779 /* Store it in the hash table. */
780 *e = t;
782 /* Set the canonical type for this new node. */
783 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
784 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
785 SET_TYPE_STRUCTURAL_EQUALITY (t);
786 else if (TYPE_CANONICAL (elt_type) != elt_type
787 || (index_type
788 && TYPE_CANONICAL (index_type) != index_type))
789 TYPE_CANONICAL (t)
790 = build_cplus_array_type
791 (TYPE_CANONICAL (elt_type),
792 index_type ? TYPE_CANONICAL (index_type) : index_type);
793 else
794 TYPE_CANONICAL (t) = t;
797 else
799 if (!TYPE_STRUCTURAL_EQUALITY_P (elt_type)
800 && !(index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type))
801 && (TYPE_CANONICAL (elt_type) != elt_type
802 || (index_type && TYPE_CANONICAL (index_type) != index_type)))
803 /* Make sure that the canonical type is on the appropriate
804 variants list. */
805 build_cplus_array_type
806 (TYPE_CANONICAL (elt_type),
807 index_type ? TYPE_CANONICAL (index_type) : index_type);
808 t = build_array_type (elt_type, index_type);
811 /* Push these needs up so that initialization takes place
812 more easily. */
813 bool needs_ctor
814 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (elt_type));
815 TYPE_NEEDS_CONSTRUCTING (t) = needs_ctor;
816 bool needs_dtor
817 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (elt_type));
818 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = needs_dtor;
820 /* We want TYPE_MAIN_VARIANT of an array to strip cv-quals from the
821 element type as well, so fix it up if needed. */
822 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
824 tree m = build_cplus_array_type (TYPE_MAIN_VARIANT (elt_type),
825 index_type);
827 if (TYPE_MAIN_VARIANT (t) != m)
829 if (COMPLETE_TYPE_P (t) && !COMPLETE_TYPE_P (m))
831 /* m was built before the element type was complete, so we
832 also need to copy the layout info from t. */
833 tree size = TYPE_SIZE (t);
834 tree size_unit = TYPE_SIZE_UNIT (t);
835 unsigned int align = TYPE_ALIGN (t);
836 unsigned int user_align = TYPE_USER_ALIGN (t);
837 enum machine_mode mode = TYPE_MODE (t);
838 for (tree var = m; var; var = TYPE_NEXT_VARIANT (var))
840 TYPE_SIZE (var) = size;
841 TYPE_SIZE_UNIT (var) = size_unit;
842 TYPE_ALIGN (var) = align;
843 TYPE_USER_ALIGN (var) = user_align;
844 SET_TYPE_MODE (var, mode);
845 TYPE_NEEDS_CONSTRUCTING (var) = needs_ctor;
846 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (var) = needs_dtor;
850 TYPE_MAIN_VARIANT (t) = m;
851 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
852 TYPE_NEXT_VARIANT (m) = t;
856 /* Avoid spurious warnings with VLAs (c++/54583). */
857 if (TYPE_SIZE (t) && EXPR_P (TYPE_SIZE (t)))
858 TREE_NO_WARNING (TYPE_SIZE (t)) = 1;
860 return t;
863 /* Return an ARRAY_TYPE with element type ELT and length N. */
865 tree
866 build_array_of_n_type (tree elt, int n)
868 return build_cplus_array_type (elt, build_index_type (size_int (n - 1)));
871 /* Return a reference type node referring to TO_TYPE. If RVAL is
872 true, return an rvalue reference type, otherwise return an lvalue
873 reference type. If a type node exists, reuse it, otherwise create
874 a new one. */
875 tree
876 cp_build_reference_type (tree to_type, bool rval)
878 tree lvalue_ref, t;
879 lvalue_ref = build_reference_type (to_type);
880 if (!rval)
881 return lvalue_ref;
883 /* This code to create rvalue reference types is based on and tied
884 to the code creating lvalue reference types in the middle-end
885 functions build_reference_type_for_mode and build_reference_type.
887 It works by putting the rvalue reference type nodes after the
888 lvalue reference nodes in the TYPE_NEXT_REF_TO linked list, so
889 they will effectively be ignored by the middle end. */
891 for (t = lvalue_ref; (t = TYPE_NEXT_REF_TO (t)); )
892 if (TYPE_REF_IS_RVALUE (t))
893 return t;
895 t = build_distinct_type_copy (lvalue_ref);
897 TYPE_REF_IS_RVALUE (t) = true;
898 TYPE_NEXT_REF_TO (t) = TYPE_NEXT_REF_TO (lvalue_ref);
899 TYPE_NEXT_REF_TO (lvalue_ref) = t;
901 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
902 SET_TYPE_STRUCTURAL_EQUALITY (t);
903 else if (TYPE_CANONICAL (to_type) != to_type)
904 TYPE_CANONICAL (t)
905 = cp_build_reference_type (TYPE_CANONICAL (to_type), rval);
906 else
907 TYPE_CANONICAL (t) = t;
909 layout_type (t);
911 return t;
915 /* Returns EXPR cast to rvalue reference type, like std::move. */
917 tree
918 move (tree expr)
920 tree type = TREE_TYPE (expr);
921 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
922 type = cp_build_reference_type (type, /*rval*/true);
923 return build_static_cast (type, expr, tf_warning_or_error);
926 /* Used by the C++ front end to build qualified array types. However,
927 the C version of this function does not properly maintain canonical
928 types (which are not used in C). */
929 tree
930 c_build_qualified_type (tree type, int type_quals)
932 return cp_build_qualified_type (type, type_quals);
936 /* Make a variant of TYPE, qualified with the TYPE_QUALS. Handles
937 arrays correctly. In particular, if TYPE is an array of T's, and
938 TYPE_QUALS is non-empty, returns an array of qualified T's.
940 FLAGS determines how to deal with ill-formed qualifications. If
941 tf_ignore_bad_quals is set, then bad qualifications are dropped
942 (this is permitted if TYPE was introduced via a typedef or template
943 type parameter). If bad qualifications are dropped and tf_warning
944 is set, then a warning is issued for non-const qualifications. If
945 tf_ignore_bad_quals is not set and tf_error is not set, we
946 return error_mark_node. Otherwise, we issue an error, and ignore
947 the qualifications.
949 Qualification of a reference type is valid when the reference came
950 via a typedef or template type argument. [dcl.ref] No such
951 dispensation is provided for qualifying a function type. [dcl.fct]
952 DR 295 queries this and the proposed resolution brings it into line
953 with qualifying a reference. We implement the DR. We also behave
954 in a similar manner for restricting non-pointer types. */
956 tree
957 cp_build_qualified_type_real (tree type,
958 int type_quals,
959 tsubst_flags_t complain)
961 tree result;
962 int bad_quals = TYPE_UNQUALIFIED;
964 if (type == error_mark_node)
965 return type;
967 if (type_quals == cp_type_quals (type))
968 return type;
970 if (TREE_CODE (type) == ARRAY_TYPE)
972 /* In C++, the qualification really applies to the array element
973 type. Obtain the appropriately qualified element type. */
974 tree t;
975 tree element_type
976 = cp_build_qualified_type_real (TREE_TYPE (type),
977 type_quals,
978 complain);
980 if (element_type == error_mark_node)
981 return error_mark_node;
983 /* See if we already have an identically qualified type. Tests
984 should be equivalent to those in check_qualified_type. */
985 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
986 if (TREE_TYPE (t) == element_type
987 && TYPE_NAME (t) == TYPE_NAME (type)
988 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
989 && attribute_list_equal (TYPE_ATTRIBUTES (t),
990 TYPE_ATTRIBUTES (type)))
991 break;
993 if (!t)
995 t = build_cplus_array_type (element_type, TYPE_DOMAIN (type));
997 /* Keep the typedef name. */
998 if (TYPE_NAME (t) != TYPE_NAME (type))
1000 t = build_variant_type_copy (t);
1001 TYPE_NAME (t) = TYPE_NAME (type);
1005 /* Even if we already had this variant, we update
1006 TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case
1007 they changed since the variant was originally created.
1009 This seems hokey; if there is some way to use a previous
1010 variant *without* coming through here,
1011 TYPE_NEEDS_CONSTRUCTING will never be updated. */
1012 TYPE_NEEDS_CONSTRUCTING (t)
1013 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
1014 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1015 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
1016 return t;
1018 else if (TYPE_PTRMEMFUNC_P (type))
1020 /* For a pointer-to-member type, we can't just return a
1021 cv-qualified version of the RECORD_TYPE. If we do, we
1022 haven't changed the field that contains the actual pointer to
1023 a method, and so TYPE_PTRMEMFUNC_FN_TYPE will be wrong. */
1024 tree t;
1026 t = TYPE_PTRMEMFUNC_FN_TYPE (type);
1027 t = cp_build_qualified_type_real (t, type_quals, complain);
1028 return build_ptrmemfunc_type (t);
1030 else if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
1032 tree t = PACK_EXPANSION_PATTERN (type);
1034 t = cp_build_qualified_type_real (t, type_quals, complain);
1035 return make_pack_expansion (t);
1038 /* A reference or method type shall not be cv-qualified.
1039 [dcl.ref], [dcl.fct]. This used to be an error, but as of DR 295
1040 (in CD1) we always ignore extra cv-quals on functions. */
1041 if (type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)
1042 && (TREE_CODE (type) == REFERENCE_TYPE
1043 || TREE_CODE (type) == FUNCTION_TYPE
1044 || TREE_CODE (type) == METHOD_TYPE))
1046 if (TREE_CODE (type) == REFERENCE_TYPE)
1047 bad_quals |= type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1048 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1051 /* But preserve any function-cv-quals on a FUNCTION_TYPE. */
1052 if (TREE_CODE (type) == FUNCTION_TYPE)
1053 type_quals |= type_memfn_quals (type);
1055 /* A restrict-qualified type must be a pointer (or reference)
1056 to object or incomplete type. */
1057 if ((type_quals & TYPE_QUAL_RESTRICT)
1058 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1059 && TREE_CODE (type) != TYPENAME_TYPE
1060 && !POINTER_TYPE_P (type))
1062 bad_quals |= TYPE_QUAL_RESTRICT;
1063 type_quals &= ~TYPE_QUAL_RESTRICT;
1066 if (bad_quals == TYPE_UNQUALIFIED
1067 || (complain & tf_ignore_bad_quals))
1068 /*OK*/;
1069 else if (!(complain & tf_error))
1070 return error_mark_node;
1071 else
1073 tree bad_type = build_qualified_type (ptr_type_node, bad_quals);
1074 error ("%qV qualifiers cannot be applied to %qT",
1075 bad_type, type);
1078 /* Retrieve (or create) the appropriately qualified variant. */
1079 result = build_qualified_type (type, type_quals);
1081 /* If this was a pointer-to-method type, and we just made a copy,
1082 then we need to unshare the record that holds the cached
1083 pointer-to-member-function type, because these will be distinct
1084 between the unqualified and qualified types. */
1085 if (result != type
1086 && TREE_CODE (type) == POINTER_TYPE
1087 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE
1088 && TYPE_LANG_SPECIFIC (result) == TYPE_LANG_SPECIFIC (type))
1089 TYPE_LANG_SPECIFIC (result) = NULL;
1091 /* We may also have ended up building a new copy of the canonical
1092 type of a pointer-to-method type, which could have the same
1093 sharing problem described above. */
1094 if (TYPE_CANONICAL (result) != TYPE_CANONICAL (type)
1095 && TREE_CODE (type) == POINTER_TYPE
1096 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE
1097 && (TYPE_LANG_SPECIFIC (TYPE_CANONICAL (result))
1098 == TYPE_LANG_SPECIFIC (TYPE_CANONICAL (type))))
1099 TYPE_LANG_SPECIFIC (TYPE_CANONICAL (result)) = NULL;
1101 return result;
1104 /* Return TYPE with const and volatile removed. */
1106 tree
1107 cv_unqualified (tree type)
1109 int quals;
1111 if (type == error_mark_node)
1112 return type;
1114 quals = cp_type_quals (type);
1115 quals &= ~(TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE);
1116 return cp_build_qualified_type (type, quals);
1119 /* Builds a qualified variant of T that is not a typedef variant.
1120 E.g. consider the following declarations:
1121 typedef const int ConstInt;
1122 typedef ConstInt* PtrConstInt;
1123 If T is PtrConstInt, this function returns a type representing
1124 const int*.
1125 In other words, if T is a typedef, the function returns the underlying type.
1126 The cv-qualification and attributes of the type returned match the
1127 input type.
1128 They will always be compatible types.
1129 The returned type is built so that all of its subtypes
1130 recursively have their typedefs stripped as well.
1132 This is different from just returning TYPE_CANONICAL (T)
1133 Because of several reasons:
1134 * If T is a type that needs structural equality
1135 its TYPE_CANONICAL (T) will be NULL.
1136 * TYPE_CANONICAL (T) desn't carry type attributes
1137 and loses template parameter names. */
1139 tree
1140 strip_typedefs (tree t)
1142 tree result = NULL, type = NULL, t0 = NULL;
1144 if (!t || t == error_mark_node || t == TYPE_CANONICAL (t))
1145 return t;
1147 gcc_assert (TYPE_P (t));
1149 switch (TREE_CODE (t))
1151 case POINTER_TYPE:
1152 type = strip_typedefs (TREE_TYPE (t));
1153 result = build_pointer_type (type);
1154 break;
1155 case REFERENCE_TYPE:
1156 type = strip_typedefs (TREE_TYPE (t));
1157 result = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
1158 break;
1159 case OFFSET_TYPE:
1160 t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t));
1161 type = strip_typedefs (TREE_TYPE (t));
1162 result = build_offset_type (t0, type);
1163 break;
1164 case RECORD_TYPE:
1165 if (TYPE_PTRMEMFUNC_P (t))
1167 t0 = strip_typedefs (TYPE_PTRMEMFUNC_FN_TYPE (t));
1168 result = build_ptrmemfunc_type (t0);
1170 break;
1171 case ARRAY_TYPE:
1172 type = strip_typedefs (TREE_TYPE (t));
1173 t0 = strip_typedefs (TYPE_DOMAIN (t));;
1174 result = build_cplus_array_type (type, t0);
1175 break;
1176 case FUNCTION_TYPE:
1177 case METHOD_TYPE:
1179 tree arg_types = NULL, arg_node, arg_type;
1180 for (arg_node = TYPE_ARG_TYPES (t);
1181 arg_node;
1182 arg_node = TREE_CHAIN (arg_node))
1184 if (arg_node == void_list_node)
1185 break;
1186 arg_type = strip_typedefs (TREE_VALUE (arg_node));
1187 gcc_assert (arg_type);
1189 arg_types =
1190 tree_cons (TREE_PURPOSE (arg_node), arg_type, arg_types);
1193 if (arg_types)
1194 arg_types = nreverse (arg_types);
1196 /* A list of parameters not ending with an ellipsis
1197 must end with void_list_node. */
1198 if (arg_node)
1199 arg_types = chainon (arg_types, void_list_node);
1201 type = strip_typedefs (TREE_TYPE (t));
1202 if (TREE_CODE (t) == METHOD_TYPE)
1204 tree class_type = TREE_TYPE (TREE_VALUE (arg_types));
1205 gcc_assert (class_type);
1206 result =
1207 build_method_type_directly (class_type, type,
1208 TREE_CHAIN (arg_types));
1210 else
1212 result = build_function_type (type,
1213 arg_types);
1214 result = apply_memfn_quals (result, type_memfn_quals (t));
1217 if (TYPE_RAISES_EXCEPTIONS (t))
1218 result = build_exception_variant (result,
1219 TYPE_RAISES_EXCEPTIONS (t));
1221 break;
1222 case TYPENAME_TYPE:
1224 tree fullname = TYPENAME_TYPE_FULLNAME (t);
1225 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
1226 && TREE_OPERAND (fullname, 1))
1228 tree args = TREE_OPERAND (fullname, 1);
1229 tree new_args = copy_node (args);
1230 bool changed = false;
1231 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
1233 tree arg = TREE_VEC_ELT (args, i);
1234 tree strip_arg;
1235 if (TYPE_P (arg))
1236 strip_arg = strip_typedefs (arg);
1237 else
1238 strip_arg = strip_typedefs_expr (arg);
1239 TREE_VEC_ELT (new_args, i) = strip_arg;
1240 if (strip_arg != arg)
1241 changed = true;
1243 if (changed)
1244 fullname = lookup_template_function (TREE_OPERAND (fullname, 0),
1245 new_args);
1246 else
1247 ggc_free (new_args);
1249 result = make_typename_type (strip_typedefs (TYPE_CONTEXT (t)),
1250 fullname, typename_type, tf_none);
1252 break;
1253 case DECLTYPE_TYPE:
1254 result = strip_typedefs_expr (DECLTYPE_TYPE_EXPR (t));
1255 if (result == DECLTYPE_TYPE_EXPR (t))
1256 return t;
1257 else
1258 result = (finish_decltype_type
1259 (result,
1260 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t),
1261 tf_none));
1262 break;
1263 default:
1264 break;
1267 if (!result)
1268 result = TYPE_MAIN_VARIANT (t);
1269 if (TYPE_USER_ALIGN (t) != TYPE_USER_ALIGN (result)
1270 || TYPE_ALIGN (t) != TYPE_ALIGN (result))
1272 gcc_assert (TYPE_USER_ALIGN (t));
1273 if (TYPE_ALIGN (t) == TYPE_ALIGN (result))
1274 result = build_variant_type_copy (result);
1275 else
1276 result = build_aligned_type (result, TYPE_ALIGN (t));
1277 TYPE_USER_ALIGN (result) = true;
1279 if (TYPE_ATTRIBUTES (t))
1280 result = cp_build_type_attribute_variant (result, TYPE_ATTRIBUTES (t));
1281 return cp_build_qualified_type (result, cp_type_quals (t));
1284 /* Like strip_typedefs above, but works on expressions, so that in
1286 template<class T> struct A
1288 typedef T TT;
1289 B<sizeof(TT)> b;
1292 sizeof(TT) is replaced by sizeof(T). */
1294 tree
1295 strip_typedefs_expr (tree t)
1297 unsigned i,n;
1298 tree r, type, *ops;
1299 enum tree_code code;
1301 if (t == NULL_TREE || t == error_mark_node)
1302 return t;
1304 if (DECL_P (t) || CONSTANT_CLASS_P (t))
1305 return t;
1307 /* Some expressions have type operands, so let's handle types here rather
1308 than check TYPE_P in multiple places below. */
1309 if (TYPE_P (t))
1310 return strip_typedefs (t);
1312 code = TREE_CODE (t);
1313 switch (code)
1315 case IDENTIFIER_NODE:
1316 case TEMPLATE_PARM_INDEX:
1317 case OVERLOAD:
1318 case BASELINK:
1319 case ARGUMENT_PACK_SELECT:
1320 return t;
1322 case TRAIT_EXPR:
1324 tree type1 = strip_typedefs (TRAIT_EXPR_TYPE1 (t));
1325 tree type2 = strip_typedefs (TRAIT_EXPR_TYPE2 (t));
1326 if (type1 == TRAIT_EXPR_TYPE1 (t)
1327 && type2 == TRAIT_EXPR_TYPE2 (t))
1328 return t;
1329 r = copy_node (t);
1330 TRAIT_EXPR_TYPE1 (t) = type1;
1331 TRAIT_EXPR_TYPE2 (t) = type2;
1332 return r;
1335 case TREE_LIST:
1337 vec<tree, va_gc> *vec = make_tree_vector ();
1338 bool changed = false;
1339 tree it;
1340 for (it = t; it; it = TREE_CHAIN (it))
1342 tree val = strip_typedefs_expr (TREE_VALUE (t));
1343 vec_safe_push (vec, val);
1344 if (val != TREE_VALUE (t))
1345 changed = true;
1346 gcc_assert (TREE_PURPOSE (it) == NULL_TREE);
1348 if (changed)
1350 r = NULL_TREE;
1351 FOR_EACH_VEC_ELT_REVERSE (*vec, i, it)
1352 r = tree_cons (NULL_TREE, it, r);
1354 else
1355 r = t;
1356 release_tree_vector (vec);
1357 return r;
1360 case TREE_VEC:
1362 bool changed = false;
1363 vec<tree, va_gc> *vec = make_tree_vector ();
1364 n = TREE_VEC_LENGTH (t);
1365 vec_safe_reserve (vec, n);
1366 for (i = 0; i < n; ++i)
1368 tree op = strip_typedefs_expr (TREE_VEC_ELT (t, i));
1369 vec->quick_push (op);
1370 if (op != TREE_VEC_ELT (t, i))
1371 changed = true;
1373 if (changed)
1375 r = copy_node (t);
1376 for (i = 0; i < n; ++i)
1377 TREE_VEC_ELT (r, i) = (*vec)[i];
1378 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
1379 (r, GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t));
1381 else
1382 r = t;
1383 release_tree_vector (vec);
1384 return r;
1387 case CONSTRUCTOR:
1389 bool changed = false;
1390 vec<constructor_elt, va_gc> *vec
1391 = vec_safe_copy (CONSTRUCTOR_ELTS (t));
1392 n = CONSTRUCTOR_NELTS (t);
1393 type = strip_typedefs (TREE_TYPE (t));
1394 for (i = 0; i < n; ++i)
1396 constructor_elt *e = &(*vec)[i];
1397 tree op = strip_typedefs_expr (e->value);
1398 if (op != e->value)
1400 changed = true;
1401 e->value = op;
1403 gcc_checking_assert (e->index == strip_typedefs_expr (e->index));
1406 if (!changed && type == TREE_TYPE (t))
1408 vec_free (vec);
1409 return t;
1411 else
1413 r = copy_node (t);
1414 TREE_TYPE (r) = type;
1415 CONSTRUCTOR_ELTS (r) = vec;
1416 return r;
1420 case LAMBDA_EXPR:
1421 gcc_unreachable ();
1423 default:
1424 break;
1427 gcc_assert (EXPR_P (t));
1429 n = TREE_OPERAND_LENGTH (t);
1430 ops = XALLOCAVEC (tree, n);
1431 type = TREE_TYPE (t);
1433 switch (code)
1435 CASE_CONVERT:
1436 case IMPLICIT_CONV_EXPR:
1437 case DYNAMIC_CAST_EXPR:
1438 case STATIC_CAST_EXPR:
1439 case CONST_CAST_EXPR:
1440 case REINTERPRET_CAST_EXPR:
1441 case CAST_EXPR:
1442 case NEW_EXPR:
1443 type = strip_typedefs (type);
1444 /* fallthrough */
1446 default:
1447 for (i = 0; i < n; ++i)
1448 ops[i] = strip_typedefs_expr (TREE_OPERAND (t, i));
1449 break;
1452 /* If nothing changed, return t. */
1453 for (i = 0; i < n; ++i)
1454 if (ops[i] != TREE_OPERAND (t, i))
1455 break;
1456 if (i == n && type == TREE_TYPE (t))
1457 return t;
1459 r = copy_node (t);
1460 TREE_TYPE (r) = type;
1461 for (i = 0; i < n; ++i)
1462 TREE_OPERAND (r, i) = ops[i];
1463 return r;
1466 /* Makes a copy of BINFO and TYPE, which is to be inherited into a
1467 graph dominated by T. If BINFO is NULL, TYPE is a dependent base,
1468 and we do a shallow copy. If BINFO is non-NULL, we do a deep copy.
1469 VIRT indicates whether TYPE is inherited virtually or not.
1470 IGO_PREV points at the previous binfo of the inheritance graph
1471 order chain. The newly copied binfo's TREE_CHAIN forms this
1472 ordering.
1474 The CLASSTYPE_VBASECLASSES vector of T is constructed in the
1475 correct order. That is in the order the bases themselves should be
1476 constructed in.
1478 The BINFO_INHERITANCE of a virtual base class points to the binfo
1479 of the most derived type. ??? We could probably change this so that
1480 BINFO_INHERITANCE becomes synonymous with BINFO_PRIMARY, and hence
1481 remove a field. They currently can only differ for primary virtual
1482 virtual bases. */
1484 tree
1485 copy_binfo (tree binfo, tree type, tree t, tree *igo_prev, int virt)
1487 tree new_binfo;
1489 if (virt)
1491 /* See if we've already made this virtual base. */
1492 new_binfo = binfo_for_vbase (type, t);
1493 if (new_binfo)
1494 return new_binfo;
1497 new_binfo = make_tree_binfo (binfo ? BINFO_N_BASE_BINFOS (binfo) : 0);
1498 BINFO_TYPE (new_binfo) = type;
1500 /* Chain it into the inheritance graph. */
1501 TREE_CHAIN (*igo_prev) = new_binfo;
1502 *igo_prev = new_binfo;
1504 if (binfo && !BINFO_DEPENDENT_BASE_P (binfo))
1506 int ix;
1507 tree base_binfo;
1509 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), type));
1511 BINFO_OFFSET (new_binfo) = BINFO_OFFSET (binfo);
1512 BINFO_VIRTUALS (new_binfo) = BINFO_VIRTUALS (binfo);
1514 /* We do not need to copy the accesses, as they are read only. */
1515 BINFO_BASE_ACCESSES (new_binfo) = BINFO_BASE_ACCESSES (binfo);
1517 /* Recursively copy base binfos of BINFO. */
1518 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1520 tree new_base_binfo;
1521 new_base_binfo = copy_binfo (base_binfo, BINFO_TYPE (base_binfo),
1522 t, igo_prev,
1523 BINFO_VIRTUAL_P (base_binfo));
1525 if (!BINFO_INHERITANCE_CHAIN (new_base_binfo))
1526 BINFO_INHERITANCE_CHAIN (new_base_binfo) = new_binfo;
1527 BINFO_BASE_APPEND (new_binfo, new_base_binfo);
1530 else
1531 BINFO_DEPENDENT_BASE_P (new_binfo) = 1;
1533 if (virt)
1535 /* Push it onto the list after any virtual bases it contains
1536 will have been pushed. */
1537 CLASSTYPE_VBASECLASSES (t)->quick_push (new_binfo);
1538 BINFO_VIRTUAL_P (new_binfo) = 1;
1539 BINFO_INHERITANCE_CHAIN (new_binfo) = TYPE_BINFO (t);
1542 return new_binfo;
1545 /* Hashing of lists so that we don't make duplicates.
1546 The entry point is `list_hash_canon'. */
1548 /* Now here is the hash table. When recording a list, it is added
1549 to the slot whose index is the hash code mod the table size.
1550 Note that the hash table is used for several kinds of lists.
1551 While all these live in the same table, they are completely independent,
1552 and the hash code is computed differently for each of these. */
1554 static GTY ((param_is (union tree_node))) htab_t list_hash_table;
1556 struct list_proxy
1558 tree purpose;
1559 tree value;
1560 tree chain;
1563 /* Compare ENTRY (an entry in the hash table) with DATA (a list_proxy
1564 for a node we are thinking about adding). */
1566 static int
1567 list_hash_eq (const void* entry, const void* data)
1569 const_tree const t = (const_tree) entry;
1570 const struct list_proxy *const proxy = (const struct list_proxy *) data;
1572 return (TREE_VALUE (t) == proxy->value
1573 && TREE_PURPOSE (t) == proxy->purpose
1574 && TREE_CHAIN (t) == proxy->chain);
1577 /* Compute a hash code for a list (chain of TREE_LIST nodes
1578 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
1579 TREE_COMMON slots), by adding the hash codes of the individual entries. */
1581 static hashval_t
1582 list_hash_pieces (tree purpose, tree value, tree chain)
1584 hashval_t hashcode = 0;
1586 if (chain)
1587 hashcode += TREE_HASH (chain);
1589 if (value)
1590 hashcode += TREE_HASH (value);
1591 else
1592 hashcode += 1007;
1593 if (purpose)
1594 hashcode += TREE_HASH (purpose);
1595 else
1596 hashcode += 1009;
1597 return hashcode;
1600 /* Hash an already existing TREE_LIST. */
1602 static hashval_t
1603 list_hash (const void* p)
1605 const_tree const t = (const_tree) p;
1606 return list_hash_pieces (TREE_PURPOSE (t),
1607 TREE_VALUE (t),
1608 TREE_CHAIN (t));
1611 /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
1612 object for an identical list if one already exists. Otherwise, build a
1613 new one, and record it as the canonical object. */
1615 tree
1616 hash_tree_cons (tree purpose, tree value, tree chain)
1618 int hashcode = 0;
1619 void **slot;
1620 struct list_proxy proxy;
1622 /* Hash the list node. */
1623 hashcode = list_hash_pieces (purpose, value, chain);
1624 /* Create a proxy for the TREE_LIST we would like to create. We
1625 don't actually create it so as to avoid creating garbage. */
1626 proxy.purpose = purpose;
1627 proxy.value = value;
1628 proxy.chain = chain;
1629 /* See if it is already in the table. */
1630 slot = htab_find_slot_with_hash (list_hash_table, &proxy, hashcode,
1631 INSERT);
1632 /* If not, create a new node. */
1633 if (!*slot)
1634 *slot = tree_cons (purpose, value, chain);
1635 return (tree) *slot;
1638 /* Constructor for hashed lists. */
1640 tree
1641 hash_tree_chain (tree value, tree chain)
1643 return hash_tree_cons (NULL_TREE, value, chain);
1646 void
1647 debug_binfo (tree elem)
1649 HOST_WIDE_INT n;
1650 tree virtuals;
1652 fprintf (stderr, "type \"%s\", offset = " HOST_WIDE_INT_PRINT_DEC
1653 "\nvtable type:\n",
1654 TYPE_NAME_STRING (BINFO_TYPE (elem)),
1655 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
1656 debug_tree (BINFO_TYPE (elem));
1657 if (BINFO_VTABLE (elem))
1658 fprintf (stderr, "vtable decl \"%s\"\n",
1659 IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem))));
1660 else
1661 fprintf (stderr, "no vtable decl yet\n");
1662 fprintf (stderr, "virtuals:\n");
1663 virtuals = BINFO_VIRTUALS (elem);
1664 n = 0;
1666 while (virtuals)
1668 tree fndecl = TREE_VALUE (virtuals);
1669 fprintf (stderr, "%s [%ld =? %ld]\n",
1670 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
1671 (long) n, (long) TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
1672 ++n;
1673 virtuals = TREE_CHAIN (virtuals);
1677 /* Build a representation for the qualified name SCOPE::NAME. TYPE is
1678 the type of the result expression, if known, or NULL_TREE if the
1679 resulting expression is type-dependent. If TEMPLATE_P is true,
1680 NAME is known to be a template because the user explicitly used the
1681 "template" keyword after the "::".
1683 All SCOPE_REFs should be built by use of this function. */
1685 tree
1686 build_qualified_name (tree type, tree scope, tree name, bool template_p)
1688 tree t;
1689 if (type == error_mark_node
1690 || scope == error_mark_node
1691 || name == error_mark_node)
1692 return error_mark_node;
1693 t = build2 (SCOPE_REF, type, scope, name);
1694 QUALIFIED_NAME_IS_TEMPLATE (t) = template_p;
1695 PTRMEM_OK_P (t) = true;
1696 if (type)
1697 t = convert_from_reference (t);
1698 return t;
1701 /* Returns nonzero if X is an expression for a (possibly overloaded)
1702 function. If "f" is a function or function template, "f", "c->f",
1703 "c.f", "C::f", and "f<int>" will all be considered possibly
1704 overloaded functions. Returns 2 if the function is actually
1705 overloaded, i.e., if it is impossible to know the type of the
1706 function without performing overload resolution. */
1709 is_overloaded_fn (tree x)
1711 /* A baselink is also considered an overloaded function. */
1712 if (TREE_CODE (x) == OFFSET_REF
1713 || TREE_CODE (x) == COMPONENT_REF)
1714 x = TREE_OPERAND (x, 1);
1715 if (BASELINK_P (x))
1716 x = BASELINK_FUNCTIONS (x);
1717 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
1718 x = TREE_OPERAND (x, 0);
1719 if (DECL_FUNCTION_TEMPLATE_P (OVL_CURRENT (x))
1720 || (TREE_CODE (x) == OVERLOAD && OVL_CHAIN (x)))
1721 return 2;
1722 return (TREE_CODE (x) == FUNCTION_DECL
1723 || TREE_CODE (x) == OVERLOAD);
1726 /* X is the CALL_EXPR_FN of a CALL_EXPR. If X represents a dependent name
1727 (14.6.2), return the IDENTIFIER_NODE for that name. Otherwise, return
1728 NULL_TREE. */
1730 tree
1731 dependent_name (tree x)
1733 if (TREE_CODE (x) == IDENTIFIER_NODE)
1734 return x;
1735 if (TREE_CODE (x) != COMPONENT_REF
1736 && TREE_CODE (x) != OFFSET_REF
1737 && TREE_CODE (x) != BASELINK
1738 && is_overloaded_fn (x))
1739 return DECL_NAME (get_first_fn (x));
1740 return NULL_TREE;
1743 /* Returns true iff X is an expression for an overloaded function
1744 whose type cannot be known without performing overload
1745 resolution. */
1747 bool
1748 really_overloaded_fn (tree x)
1750 return is_overloaded_fn (x) == 2;
1753 tree
1754 get_fns (tree from)
1756 gcc_assert (is_overloaded_fn (from));
1757 /* A baselink is also considered an overloaded function. */
1758 if (TREE_CODE (from) == OFFSET_REF
1759 || TREE_CODE (from) == COMPONENT_REF)
1760 from = TREE_OPERAND (from, 1);
1761 if (BASELINK_P (from))
1762 from = BASELINK_FUNCTIONS (from);
1763 if (TREE_CODE (from) == TEMPLATE_ID_EXPR)
1764 from = TREE_OPERAND (from, 0);
1765 return from;
1768 tree
1769 get_first_fn (tree from)
1771 return OVL_CURRENT (get_fns (from));
1774 /* Return a new OVL node, concatenating it with the old one. */
1776 tree
1777 ovl_cons (tree decl, tree chain)
1779 tree result = make_node (OVERLOAD);
1780 TREE_TYPE (result) = unknown_type_node;
1781 OVL_FUNCTION (result) = decl;
1782 TREE_CHAIN (result) = chain;
1784 return result;
1787 /* Build a new overloaded function. If this is the first one,
1788 just return it; otherwise, ovl_cons the _DECLs */
1790 tree
1791 build_overload (tree decl, tree chain)
1793 if (! chain && TREE_CODE (decl) != TEMPLATE_DECL)
1794 return decl;
1795 return ovl_cons (decl, chain);
1798 /* Return the scope where the overloaded functions OVL were found. */
1800 tree
1801 ovl_scope (tree ovl)
1803 if (TREE_CODE (ovl) == OFFSET_REF
1804 || TREE_CODE (ovl) == COMPONENT_REF)
1805 ovl = TREE_OPERAND (ovl, 1);
1806 if (TREE_CODE (ovl) == BASELINK)
1807 return BINFO_TYPE (BASELINK_BINFO (ovl));
1808 if (TREE_CODE (ovl) == TEMPLATE_ID_EXPR)
1809 ovl = TREE_OPERAND (ovl, 0);
1810 /* Skip using-declarations. */
1811 while (TREE_CODE (ovl) == OVERLOAD && OVL_USED (ovl) && OVL_CHAIN (ovl))
1812 ovl = OVL_CHAIN (ovl);
1813 return CP_DECL_CONTEXT (OVL_CURRENT (ovl));
1816 /* Return TRUE if FN is a non-static member function, FALSE otherwise.
1817 This function looks into BASELINK and OVERLOAD nodes. */
1819 bool
1820 non_static_member_function_p (tree fn)
1822 if (fn == NULL_TREE)
1823 return false;
1825 if (is_overloaded_fn (fn))
1826 fn = get_first_fn (fn);
1828 return (DECL_P (fn)
1829 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn));
1833 #define PRINT_RING_SIZE 4
1835 static const char *
1836 cxx_printable_name_internal (tree decl, int v, bool translate)
1838 static unsigned int uid_ring[PRINT_RING_SIZE];
1839 static char *print_ring[PRINT_RING_SIZE];
1840 static bool trans_ring[PRINT_RING_SIZE];
1841 static int ring_counter;
1842 int i;
1844 /* Only cache functions. */
1845 if (v < 2
1846 || TREE_CODE (decl) != FUNCTION_DECL
1847 || DECL_LANG_SPECIFIC (decl) == 0)
1848 return lang_decl_name (decl, v, translate);
1850 /* See if this print name is lying around. */
1851 for (i = 0; i < PRINT_RING_SIZE; i++)
1852 if (uid_ring[i] == DECL_UID (decl) && translate == trans_ring[i])
1853 /* yes, so return it. */
1854 return print_ring[i];
1856 if (++ring_counter == PRINT_RING_SIZE)
1857 ring_counter = 0;
1859 if (current_function_decl != NULL_TREE)
1861 /* There may be both translated and untranslated versions of the
1862 name cached. */
1863 for (i = 0; i < 2; i++)
1865 if (uid_ring[ring_counter] == DECL_UID (current_function_decl))
1866 ring_counter += 1;
1867 if (ring_counter == PRINT_RING_SIZE)
1868 ring_counter = 0;
1870 gcc_assert (uid_ring[ring_counter] != DECL_UID (current_function_decl));
1873 free (print_ring[ring_counter]);
1875 print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v, translate));
1876 uid_ring[ring_counter] = DECL_UID (decl);
1877 trans_ring[ring_counter] = translate;
1878 return print_ring[ring_counter];
1881 const char *
1882 cxx_printable_name (tree decl, int v)
1884 return cxx_printable_name_internal (decl, v, false);
1887 const char *
1888 cxx_printable_name_translate (tree decl, int v)
1890 return cxx_printable_name_internal (decl, v, true);
1893 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
1894 listed in RAISES. */
1896 tree
1897 build_exception_variant (tree type, tree raises)
1899 tree v;
1900 int type_quals;
1902 if (comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (type), ce_exact))
1903 return type;
1905 type_quals = TYPE_QUALS (type);
1906 for (v = TYPE_MAIN_VARIANT (type); v; v = TYPE_NEXT_VARIANT (v))
1907 if (check_qualified_type (v, type, type_quals)
1908 && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (v), ce_exact))
1909 return v;
1911 /* Need to build a new variant. */
1912 v = build_variant_type_copy (type);
1913 TYPE_RAISES_EXCEPTIONS (v) = raises;
1914 return v;
1917 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new
1918 BOUND_TEMPLATE_TEMPLATE_PARM bound with NEWARGS as its template
1919 arguments. */
1921 tree
1922 bind_template_template_parm (tree t, tree newargs)
1924 tree decl = TYPE_NAME (t);
1925 tree t2;
1927 t2 = cxx_make_type (BOUND_TEMPLATE_TEMPLATE_PARM);
1928 decl = build_decl (input_location,
1929 TYPE_DECL, DECL_NAME (decl), NULL_TREE);
1931 /* These nodes have to be created to reflect new TYPE_DECL and template
1932 arguments. */
1933 TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t));
1934 TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl;
1935 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
1936 = build_template_info (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t), newargs);
1938 TREE_TYPE (decl) = t2;
1939 TYPE_NAME (t2) = decl;
1940 TYPE_STUB_DECL (t2) = decl;
1941 TYPE_SIZE (t2) = 0;
1942 SET_TYPE_STRUCTURAL_EQUALITY (t2);
1944 return t2;
1947 /* Called from count_trees via walk_tree. */
1949 static tree
1950 count_trees_r (tree *tp, int *walk_subtrees, void *data)
1952 ++*((int *) data);
1954 if (TYPE_P (*tp))
1955 *walk_subtrees = 0;
1957 return NULL_TREE;
1960 /* Debugging function for measuring the rough complexity of a tree
1961 representation. */
1964 count_trees (tree t)
1966 int n_trees = 0;
1967 cp_walk_tree_without_duplicates (&t, count_trees_r, &n_trees);
1968 return n_trees;
1971 /* Called from verify_stmt_tree via walk_tree. */
1973 static tree
1974 verify_stmt_tree_r (tree* tp, int * /*walk_subtrees*/, void* data)
1976 tree t = *tp;
1977 hash_table <pointer_hash <tree_node> > *statements
1978 = static_cast <hash_table <pointer_hash <tree_node> > *> (data);
1979 tree_node **slot;
1981 if (!STATEMENT_CODE_P (TREE_CODE (t)))
1982 return NULL_TREE;
1984 /* If this statement is already present in the hash table, then
1985 there is a circularity in the statement tree. */
1986 gcc_assert (!statements->find (t));
1988 slot = statements->find_slot (t, INSERT);
1989 *slot = t;
1991 return NULL_TREE;
1994 /* Debugging function to check that the statement T has not been
1995 corrupted. For now, this function simply checks that T contains no
1996 circularities. */
1998 void
1999 verify_stmt_tree (tree t)
2001 hash_table <pointer_hash <tree_node> > statements;
2002 statements.create (37);
2003 cp_walk_tree (&t, verify_stmt_tree_r, &statements, NULL);
2004 statements.dispose ();
2007 /* Check if the type T depends on a type with no linkage and if so, return
2008 it. If RELAXED_P then do not consider a class type declared within
2009 a vague-linkage function to have no linkage. */
2011 tree
2012 no_linkage_check (tree t, bool relaxed_p)
2014 tree r;
2016 /* There's no point in checking linkage on template functions; we
2017 can't know their complete types. */
2018 if (processing_template_decl)
2019 return NULL_TREE;
2021 switch (TREE_CODE (t))
2023 case RECORD_TYPE:
2024 if (TYPE_PTRMEMFUNC_P (t))
2025 goto ptrmem;
2026 /* Lambda types that don't have mangling scope have no linkage. We
2027 check CLASSTYPE_LAMBDA_EXPR here rather than LAMBDA_TYPE_P because
2028 when we get here from pushtag none of the lambda information is
2029 set up yet, so we want to assume that the lambda has linkage and
2030 fix it up later if not. */
2031 if (CLASSTYPE_LAMBDA_EXPR (t)
2032 && LAMBDA_TYPE_EXTRA_SCOPE (t) == NULL_TREE)
2033 return t;
2034 /* Fall through. */
2035 case UNION_TYPE:
2036 if (!CLASS_TYPE_P (t))
2037 return NULL_TREE;
2038 /* Fall through. */
2039 case ENUMERAL_TYPE:
2040 /* Only treat anonymous types as having no linkage if they're at
2041 namespace scope. This is core issue 966. */
2042 if (TYPE_ANONYMOUS_P (t) && TYPE_NAMESPACE_SCOPE_P (t))
2043 return t;
2045 for (r = CP_TYPE_CONTEXT (t); ; )
2047 /* If we're a nested type of a !TREE_PUBLIC class, we might not
2048 have linkage, or we might just be in an anonymous namespace.
2049 If we're in a TREE_PUBLIC class, we have linkage. */
2050 if (TYPE_P (r) && !TREE_PUBLIC (TYPE_NAME (r)))
2051 return no_linkage_check (TYPE_CONTEXT (t), relaxed_p);
2052 else if (TREE_CODE (r) == FUNCTION_DECL)
2054 if (!relaxed_p || !vague_linkage_p (r))
2055 return t;
2056 else
2057 r = CP_DECL_CONTEXT (r);
2059 else
2060 break;
2063 return NULL_TREE;
2065 case ARRAY_TYPE:
2066 case POINTER_TYPE:
2067 case REFERENCE_TYPE:
2068 return no_linkage_check (TREE_TYPE (t), relaxed_p);
2070 case OFFSET_TYPE:
2071 ptrmem:
2072 r = no_linkage_check (TYPE_PTRMEM_POINTED_TO_TYPE (t),
2073 relaxed_p);
2074 if (r)
2075 return r;
2076 return no_linkage_check (TYPE_PTRMEM_CLASS_TYPE (t), relaxed_p);
2078 case METHOD_TYPE:
2079 r = no_linkage_check (TYPE_METHOD_BASETYPE (t), relaxed_p);
2080 if (r)
2081 return r;
2082 /* Fall through. */
2083 case FUNCTION_TYPE:
2085 tree parm;
2086 for (parm = TYPE_ARG_TYPES (t);
2087 parm && parm != void_list_node;
2088 parm = TREE_CHAIN (parm))
2090 r = no_linkage_check (TREE_VALUE (parm), relaxed_p);
2091 if (r)
2092 return r;
2094 return no_linkage_check (TREE_TYPE (t), relaxed_p);
2097 default:
2098 return NULL_TREE;
2102 extern int depth_reached;
2104 void
2105 cxx_print_statistics (void)
2107 print_search_statistics ();
2108 print_class_statistics ();
2109 print_template_statistics ();
2110 if (GATHER_STATISTICS)
2111 fprintf (stderr, "maximum template instantiation depth reached: %d\n",
2112 depth_reached);
2115 /* Return, as an INTEGER_CST node, the number of elements for TYPE
2116 (which is an ARRAY_TYPE). This counts only elements of the top
2117 array. */
2119 tree
2120 array_type_nelts_top (tree type)
2122 return fold_build2_loc (input_location,
2123 PLUS_EXPR, sizetype,
2124 array_type_nelts (type),
2125 size_one_node);
2128 /* Return, as an INTEGER_CST node, the number of elements for TYPE
2129 (which is an ARRAY_TYPE). This one is a recursive count of all
2130 ARRAY_TYPEs that are clumped together. */
2132 tree
2133 array_type_nelts_total (tree type)
2135 tree sz = array_type_nelts_top (type);
2136 type = TREE_TYPE (type);
2137 while (TREE_CODE (type) == ARRAY_TYPE)
2139 tree n = array_type_nelts_top (type);
2140 sz = fold_build2_loc (input_location,
2141 MULT_EXPR, sizetype, sz, n);
2142 type = TREE_TYPE (type);
2144 return sz;
2147 /* Called from break_out_target_exprs via mapcar. */
2149 static tree
2150 bot_manip (tree* tp, int* walk_subtrees, void* data)
2152 splay_tree target_remap = ((splay_tree) data);
2153 tree t = *tp;
2155 if (!TYPE_P (t) && TREE_CONSTANT (t) && !TREE_SIDE_EFFECTS (t))
2157 /* There can't be any TARGET_EXPRs or their slot variables below this
2158 point. But we must make a copy, in case subsequent processing
2159 alters any part of it. For example, during gimplification a cast
2160 of the form (T) &X::f (where "f" is a member function) will lead
2161 to replacing the PTRMEM_CST for &X::f with a VAR_DECL. */
2162 *walk_subtrees = 0;
2163 *tp = unshare_expr (t);
2164 return NULL_TREE;
2166 if (TREE_CODE (t) == TARGET_EXPR)
2168 tree u;
2170 if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
2172 u = build_cplus_new (TREE_TYPE (t), TREE_OPERAND (t, 1),
2173 tf_warning_or_error);
2174 if (AGGR_INIT_ZERO_FIRST (TREE_OPERAND (t, 1)))
2175 AGGR_INIT_ZERO_FIRST (TREE_OPERAND (u, 1)) = true;
2177 else
2178 u = build_target_expr_with_type (TREE_OPERAND (t, 1), TREE_TYPE (t),
2179 tf_warning_or_error);
2181 TARGET_EXPR_IMPLICIT_P (u) = TARGET_EXPR_IMPLICIT_P (t);
2182 TARGET_EXPR_LIST_INIT_P (u) = TARGET_EXPR_LIST_INIT_P (t);
2183 TARGET_EXPR_DIRECT_INIT_P (u) = TARGET_EXPR_DIRECT_INIT_P (t);
2185 /* Map the old variable to the new one. */
2186 splay_tree_insert (target_remap,
2187 (splay_tree_key) TREE_OPERAND (t, 0),
2188 (splay_tree_value) TREE_OPERAND (u, 0));
2190 TREE_OPERAND (u, 1) = break_out_target_exprs (TREE_OPERAND (u, 1));
2192 /* Replace the old expression with the new version. */
2193 *tp = u;
2194 /* We don't have to go below this point; the recursive call to
2195 break_out_target_exprs will have handled anything below this
2196 point. */
2197 *walk_subtrees = 0;
2198 return NULL_TREE;
2201 /* Make a copy of this node. */
2202 t = copy_tree_r (tp, walk_subtrees, NULL);
2203 if (TREE_CODE (*tp) == CALL_EXPR)
2204 set_flags_from_callee (*tp);
2205 return t;
2208 /* Replace all remapped VAR_DECLs in T with their new equivalents.
2209 DATA is really a splay-tree mapping old variables to new
2210 variables. */
2212 static tree
2213 bot_replace (tree* t, int* /*walk_subtrees*/, void* data)
2215 splay_tree target_remap = ((splay_tree) data);
2217 if (TREE_CODE (*t) == VAR_DECL)
2219 splay_tree_node n = splay_tree_lookup (target_remap,
2220 (splay_tree_key) *t);
2221 if (n)
2222 *t = (tree) n->value;
2224 else if (TREE_CODE (*t) == PARM_DECL
2225 && DECL_NAME (*t) == this_identifier)
2227 /* In an NSDMI we need to replace the 'this' parameter we used for
2228 parsing with the real one for this function. */
2229 *t = current_class_ptr;
2231 else if (TREE_CODE (*t) == CONVERT_EXPR
2232 && CONVERT_EXPR_VBASE_PATH (*t))
2234 /* In an NSDMI build_base_path defers building conversions to virtual
2235 bases, and we handle it here. */
2236 tree basetype = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (*t)));
2237 vec<tree, va_gc> *vbases = CLASSTYPE_VBASECLASSES (current_class_type);
2238 int i; tree binfo;
2239 FOR_EACH_VEC_SAFE_ELT (vbases, i, binfo)
2240 if (BINFO_TYPE (binfo) == basetype)
2241 break;
2242 *t = build_base_path (PLUS_EXPR, TREE_OPERAND (*t, 0), binfo, true,
2243 tf_warning_or_error);
2246 return NULL_TREE;
2249 /* When we parse a default argument expression, we may create
2250 temporary variables via TARGET_EXPRs. When we actually use the
2251 default-argument expression, we make a copy of the expression
2252 and replace the temporaries with appropriate local versions. */
2254 tree
2255 break_out_target_exprs (tree t)
2257 static int target_remap_count;
2258 static splay_tree target_remap;
2260 if (!target_remap_count++)
2261 target_remap = splay_tree_new (splay_tree_compare_pointers,
2262 /*splay_tree_delete_key_fn=*/NULL,
2263 /*splay_tree_delete_value_fn=*/NULL);
2264 cp_walk_tree (&t, bot_manip, target_remap, NULL);
2265 cp_walk_tree (&t, bot_replace, target_remap, NULL);
2267 if (!--target_remap_count)
2269 splay_tree_delete (target_remap);
2270 target_remap = NULL;
2273 return t;
2276 /* Similar to `build_nt', but for template definitions of dependent
2277 expressions */
2279 tree
2280 build_min_nt_loc (location_t loc, enum tree_code code, ...)
2282 tree t;
2283 int length;
2284 int i;
2285 va_list p;
2287 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
2289 va_start (p, code);
2291 t = make_node (code);
2292 SET_EXPR_LOCATION (t, loc);
2293 length = TREE_CODE_LENGTH (code);
2295 for (i = 0; i < length; i++)
2297 tree x = va_arg (p, tree);
2298 TREE_OPERAND (t, i) = x;
2301 va_end (p);
2302 return t;
2306 /* Similar to `build', but for template definitions. */
2308 tree
2309 build_min (enum tree_code code, tree tt, ...)
2311 tree t;
2312 int length;
2313 int i;
2314 va_list p;
2316 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
2318 va_start (p, tt);
2320 t = make_node (code);
2321 length = TREE_CODE_LENGTH (code);
2322 TREE_TYPE (t) = tt;
2324 for (i = 0; i < length; i++)
2326 tree x = va_arg (p, tree);
2327 TREE_OPERAND (t, i) = x;
2328 if (x && !TYPE_P (x) && TREE_SIDE_EFFECTS (x))
2329 TREE_SIDE_EFFECTS (t) = 1;
2332 va_end (p);
2333 return t;
2336 /* Similar to `build', but for template definitions of non-dependent
2337 expressions. NON_DEP is the non-dependent expression that has been
2338 built. */
2340 tree
2341 build_min_non_dep (enum tree_code code, tree non_dep, ...)
2343 tree t;
2344 int length;
2345 int i;
2346 va_list p;
2348 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
2350 va_start (p, non_dep);
2352 if (REFERENCE_REF_P (non_dep))
2353 non_dep = TREE_OPERAND (non_dep, 0);
2355 t = make_node (code);
2356 length = TREE_CODE_LENGTH (code);
2357 TREE_TYPE (t) = TREE_TYPE (non_dep);
2358 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
2360 for (i = 0; i < length; i++)
2362 tree x = va_arg (p, tree);
2363 TREE_OPERAND (t, i) = x;
2366 if (code == COMPOUND_EXPR && TREE_CODE (non_dep) != COMPOUND_EXPR)
2367 /* This should not be considered a COMPOUND_EXPR, because it
2368 resolves to an overload. */
2369 COMPOUND_EXPR_OVERLOADED (t) = 1;
2371 va_end (p);
2372 return convert_from_reference (t);
2375 /* Similar to `build_nt_call_vec', but for template definitions of
2376 non-dependent expressions. NON_DEP is the non-dependent expression
2377 that has been built. */
2379 tree
2380 build_min_non_dep_call_vec (tree non_dep, tree fn, vec<tree, va_gc> *argvec)
2382 tree t = build_nt_call_vec (fn, argvec);
2383 if (REFERENCE_REF_P (non_dep))
2384 non_dep = TREE_OPERAND (non_dep, 0);
2385 TREE_TYPE (t) = TREE_TYPE (non_dep);
2386 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
2387 return convert_from_reference (t);
2390 tree
2391 get_type_decl (tree t)
2393 if (TREE_CODE (t) == TYPE_DECL)
2394 return t;
2395 if (TYPE_P (t))
2396 return TYPE_STUB_DECL (t);
2397 gcc_assert (t == error_mark_node);
2398 return t;
2401 /* Returns the namespace that contains DECL, whether directly or
2402 indirectly. */
2404 tree
2405 decl_namespace_context (tree decl)
2407 while (1)
2409 if (TREE_CODE (decl) == NAMESPACE_DECL)
2410 return decl;
2411 else if (TYPE_P (decl))
2412 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
2413 else
2414 decl = CP_DECL_CONTEXT (decl);
2418 /* Returns true if decl is within an anonymous namespace, however deeply
2419 nested, or false otherwise. */
2421 bool
2422 decl_anon_ns_mem_p (const_tree decl)
2424 while (1)
2426 if (decl == NULL_TREE || decl == error_mark_node)
2427 return false;
2428 if (TREE_CODE (decl) == NAMESPACE_DECL
2429 && DECL_NAME (decl) == NULL_TREE)
2430 return true;
2431 /* Classes and namespaces inside anonymous namespaces have
2432 TREE_PUBLIC == 0, so we can shortcut the search. */
2433 else if (TYPE_P (decl))
2434 return (TREE_PUBLIC (TYPE_MAIN_DECL (decl)) == 0);
2435 else if (TREE_CODE (decl) == NAMESPACE_DECL)
2436 return (TREE_PUBLIC (decl) == 0);
2437 else
2438 decl = DECL_CONTEXT (decl);
2442 /* Subroutine of cp_tree_equal: t1 and t2 are the CALL_EXPR_FNs of two
2443 CALL_EXPRS. Return whether they are equivalent. */
2445 static bool
2446 called_fns_equal (tree t1, tree t2)
2448 /* Core 1321: dependent names are equivalent even if the overload sets
2449 are different. But do compare explicit template arguments. */
2450 tree name1 = dependent_name (t1);
2451 tree name2 = dependent_name (t2);
2452 if (name1 || name2)
2454 tree targs1 = NULL_TREE, targs2 = NULL_TREE;
2456 if (name1 != name2)
2457 return false;
2459 if (TREE_CODE (t1) == TEMPLATE_ID_EXPR)
2460 targs1 = TREE_OPERAND (t1, 1);
2461 if (TREE_CODE (t2) == TEMPLATE_ID_EXPR)
2462 targs2 = TREE_OPERAND (t2, 1);
2463 return cp_tree_equal (targs1, targs2);
2465 else
2466 return cp_tree_equal (t1, t2);
2469 /* Return truthvalue of whether T1 is the same tree structure as T2.
2470 Return 1 if they are the same. Return 0 if they are different. */
2472 bool
2473 cp_tree_equal (tree t1, tree t2)
2475 enum tree_code code1, code2;
2477 if (t1 == t2)
2478 return true;
2479 if (!t1 || !t2)
2480 return false;
2482 for (code1 = TREE_CODE (t1);
2483 CONVERT_EXPR_CODE_P (code1)
2484 || code1 == NON_LVALUE_EXPR;
2485 code1 = TREE_CODE (t1))
2486 t1 = TREE_OPERAND (t1, 0);
2487 for (code2 = TREE_CODE (t2);
2488 CONVERT_EXPR_CODE_P (code2)
2489 || code1 == NON_LVALUE_EXPR;
2490 code2 = TREE_CODE (t2))
2491 t2 = TREE_OPERAND (t2, 0);
2493 /* They might have become equal now. */
2494 if (t1 == t2)
2495 return true;
2497 if (code1 != code2)
2498 return false;
2500 switch (code1)
2502 case INTEGER_CST:
2503 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
2504 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
2506 case REAL_CST:
2507 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2509 case STRING_CST:
2510 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2511 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2512 TREE_STRING_LENGTH (t1));
2514 case FIXED_CST:
2515 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
2516 TREE_FIXED_CST (t2));
2518 case COMPLEX_CST:
2519 return cp_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
2520 && cp_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
2522 case VECTOR_CST:
2523 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
2525 case CONSTRUCTOR:
2526 /* We need to do this when determining whether or not two
2527 non-type pointer to member function template arguments
2528 are the same. */
2529 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
2530 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
2531 return false;
2533 tree field, value;
2534 unsigned int i;
2535 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
2537 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
2538 if (!cp_tree_equal (field, elt2->index)
2539 || !cp_tree_equal (value, elt2->value))
2540 return false;
2543 return true;
2545 case TREE_LIST:
2546 if (!cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
2547 return false;
2548 if (!cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
2549 return false;
2550 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
2552 case SAVE_EXPR:
2553 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2555 case CALL_EXPR:
2557 tree arg1, arg2;
2558 call_expr_arg_iterator iter1, iter2;
2559 if (!called_fns_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
2560 return false;
2561 for (arg1 = first_call_expr_arg (t1, &iter1),
2562 arg2 = first_call_expr_arg (t2, &iter2);
2563 arg1 && arg2;
2564 arg1 = next_call_expr_arg (&iter1),
2565 arg2 = next_call_expr_arg (&iter2))
2566 if (!cp_tree_equal (arg1, arg2))
2567 return false;
2568 if (arg1 || arg2)
2569 return false;
2570 return true;
2573 case TARGET_EXPR:
2575 tree o1 = TREE_OPERAND (t1, 0);
2576 tree o2 = TREE_OPERAND (t2, 0);
2578 /* Special case: if either target is an unallocated VAR_DECL,
2579 it means that it's going to be unified with whatever the
2580 TARGET_EXPR is really supposed to initialize, so treat it
2581 as being equivalent to anything. */
2582 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
2583 && !DECL_RTL_SET_P (o1))
2584 /*Nop*/;
2585 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
2586 && !DECL_RTL_SET_P (o2))
2587 /*Nop*/;
2588 else if (!cp_tree_equal (o1, o2))
2589 return false;
2591 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
2594 case WITH_CLEANUP_EXPR:
2595 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
2596 return false;
2597 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
2599 case COMPONENT_REF:
2600 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
2601 return false;
2602 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2604 case PARM_DECL:
2605 /* For comparing uses of parameters in late-specified return types
2606 with an out-of-class definition of the function, but can also come
2607 up for expressions that involve 'this' in a member function
2608 template. */
2610 if (comparing_specializations)
2611 /* When comparing hash table entries, only an exact match is
2612 good enough; we don't want to replace 'this' with the
2613 version from another function. */
2614 return false;
2616 if (same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2618 if (DECL_ARTIFICIAL (t1) ^ DECL_ARTIFICIAL (t2))
2619 return false;
2620 if (DECL_ARTIFICIAL (t1)
2621 || (DECL_PARM_LEVEL (t1) == DECL_PARM_LEVEL (t2)
2622 && DECL_PARM_INDEX (t1) == DECL_PARM_INDEX (t2)))
2623 return true;
2625 return false;
2627 case VAR_DECL:
2628 case CONST_DECL:
2629 case FIELD_DECL:
2630 case FUNCTION_DECL:
2631 case TEMPLATE_DECL:
2632 case IDENTIFIER_NODE:
2633 case SSA_NAME:
2634 return false;
2636 case BASELINK:
2637 return (BASELINK_BINFO (t1) == BASELINK_BINFO (t2)
2638 && BASELINK_ACCESS_BINFO (t1) == BASELINK_ACCESS_BINFO (t2)
2639 && BASELINK_QUALIFIED_P (t1) == BASELINK_QUALIFIED_P (t2)
2640 && cp_tree_equal (BASELINK_FUNCTIONS (t1),
2641 BASELINK_FUNCTIONS (t2)));
2643 case TEMPLATE_PARM_INDEX:
2644 return (TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
2645 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2)
2646 && (TEMPLATE_PARM_PARAMETER_PACK (t1)
2647 == TEMPLATE_PARM_PARAMETER_PACK (t2))
2648 && same_type_p (TREE_TYPE (TEMPLATE_PARM_DECL (t1)),
2649 TREE_TYPE (TEMPLATE_PARM_DECL (t2))));
2651 case TEMPLATE_ID_EXPR:
2652 return (cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
2653 && cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
2655 case TREE_VEC:
2657 unsigned ix;
2658 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2659 return false;
2660 for (ix = TREE_VEC_LENGTH (t1); ix--;)
2661 if (!cp_tree_equal (TREE_VEC_ELT (t1, ix),
2662 TREE_VEC_ELT (t2, ix)))
2663 return false;
2664 return true;
2667 case SIZEOF_EXPR:
2668 case ALIGNOF_EXPR:
2670 tree o1 = TREE_OPERAND (t1, 0);
2671 tree o2 = TREE_OPERAND (t2, 0);
2673 if (code1 == SIZEOF_EXPR)
2675 if (SIZEOF_EXPR_TYPE_P (t1))
2676 o1 = TREE_TYPE (o1);
2677 if (SIZEOF_EXPR_TYPE_P (t2))
2678 o2 = TREE_TYPE (o2);
2680 if (TREE_CODE (o1) != TREE_CODE (o2))
2681 return false;
2682 if (TYPE_P (o1))
2683 return same_type_p (o1, o2);
2684 else
2685 return cp_tree_equal (o1, o2);
2688 case MODOP_EXPR:
2690 tree t1_op1, t2_op1;
2692 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
2693 return false;
2695 t1_op1 = TREE_OPERAND (t1, 1);
2696 t2_op1 = TREE_OPERAND (t2, 1);
2697 if (TREE_CODE (t1_op1) != TREE_CODE (t2_op1))
2698 return false;
2700 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t2, 2));
2703 case PTRMEM_CST:
2704 /* Two pointer-to-members are the same if they point to the same
2705 field or function in the same class. */
2706 if (PTRMEM_CST_MEMBER (t1) != PTRMEM_CST_MEMBER (t2))
2707 return false;
2709 return same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2));
2711 case OVERLOAD:
2712 if (OVL_FUNCTION (t1) != OVL_FUNCTION (t2))
2713 return false;
2714 return cp_tree_equal (OVL_CHAIN (t1), OVL_CHAIN (t2));
2716 case TRAIT_EXPR:
2717 if (TRAIT_EXPR_KIND (t1) != TRAIT_EXPR_KIND (t2))
2718 return false;
2719 return same_type_p (TRAIT_EXPR_TYPE1 (t1), TRAIT_EXPR_TYPE1 (t2))
2720 && same_type_p (TRAIT_EXPR_TYPE2 (t1), TRAIT_EXPR_TYPE2 (t2));
2722 case CAST_EXPR:
2723 case STATIC_CAST_EXPR:
2724 case REINTERPRET_CAST_EXPR:
2725 case CONST_CAST_EXPR:
2726 case DYNAMIC_CAST_EXPR:
2727 case IMPLICIT_CONV_EXPR:
2728 case NEW_EXPR:
2729 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2730 return false;
2731 /* Now compare operands as usual. */
2732 break;
2734 case DEFERRED_NOEXCEPT:
2735 return (cp_tree_equal (DEFERRED_NOEXCEPT_PATTERN (t1),
2736 DEFERRED_NOEXCEPT_PATTERN (t2))
2737 && comp_template_args (DEFERRED_NOEXCEPT_ARGS (t1),
2738 DEFERRED_NOEXCEPT_ARGS (t2)));
2739 break;
2741 default:
2742 break;
2745 switch (TREE_CODE_CLASS (code1))
2747 case tcc_unary:
2748 case tcc_binary:
2749 case tcc_comparison:
2750 case tcc_expression:
2751 case tcc_vl_exp:
2752 case tcc_reference:
2753 case tcc_statement:
2755 int i, n;
2757 n = cp_tree_operand_length (t1);
2758 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
2759 && n != TREE_OPERAND_LENGTH (t2))
2760 return false;
2762 for (i = 0; i < n; ++i)
2763 if (!cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
2764 return false;
2766 return true;
2769 case tcc_type:
2770 return same_type_p (t1, t2);
2771 default:
2772 gcc_unreachable ();
2774 /* We can get here with --disable-checking. */
2775 return false;
2778 /* The type of ARG when used as an lvalue. */
2780 tree
2781 lvalue_type (tree arg)
2783 tree type = TREE_TYPE (arg);
2784 return type;
2787 /* The type of ARG for printing error messages; denote lvalues with
2788 reference types. */
2790 tree
2791 error_type (tree arg)
2793 tree type = TREE_TYPE (arg);
2795 if (TREE_CODE (type) == ARRAY_TYPE)
2797 else if (TREE_CODE (type) == ERROR_MARK)
2799 else if (real_lvalue_p (arg))
2800 type = build_reference_type (lvalue_type (arg));
2801 else if (MAYBE_CLASS_TYPE_P (type))
2802 type = lvalue_type (arg);
2804 return type;
2807 /* Does FUNCTION use a variable-length argument list? */
2810 varargs_function_p (const_tree function)
2812 return stdarg_p (TREE_TYPE (function));
2815 /* Returns 1 if decl is a member of a class. */
2818 member_p (const_tree decl)
2820 const_tree const ctx = DECL_CONTEXT (decl);
2821 return (ctx && TYPE_P (ctx));
2824 /* Create a placeholder for member access where we don't actually have an
2825 object that the access is against. */
2827 tree
2828 build_dummy_object (tree type)
2830 tree decl = build1 (NOP_EXPR, build_pointer_type (type), void_zero_node);
2831 return cp_build_indirect_ref (decl, RO_NULL, tf_warning_or_error);
2834 /* We've gotten a reference to a member of TYPE. Return *this if appropriate,
2835 or a dummy object otherwise. If BINFOP is non-0, it is filled with the
2836 binfo path from current_class_type to TYPE, or 0. */
2838 tree
2839 maybe_dummy_object (tree type, tree* binfop)
2841 tree decl, context;
2842 tree binfo;
2843 tree current = current_nonlambda_class_type ();
2845 if (current
2846 && (binfo = lookup_base (current, type, ba_any, NULL,
2847 tf_warning_or_error)))
2848 context = current;
2849 else
2851 /* Reference from a nested class member function. */
2852 context = type;
2853 binfo = TYPE_BINFO (type);
2856 if (binfop)
2857 *binfop = binfo;
2859 if (current_class_ref
2860 /* current_class_ref might not correspond to current_class_type if
2861 we're in tsubst_default_argument or a lambda-declarator; in either
2862 case, we want to use current_class_ref if it matches CONTEXT. */
2863 && (same_type_ignoring_top_level_qualifiers_p
2864 (TREE_TYPE (current_class_ref), context)))
2865 decl = current_class_ref;
2866 else
2867 decl = build_dummy_object (context);
2869 return decl;
2872 /* Returns 1 if OB is a placeholder object, or a pointer to one. */
2875 is_dummy_object (const_tree ob)
2877 if (TREE_CODE (ob) == INDIRECT_REF)
2878 ob = TREE_OPERAND (ob, 0);
2879 return (TREE_CODE (ob) == NOP_EXPR
2880 && TREE_OPERAND (ob, 0) == void_zero_node);
2883 /* Returns 1 iff type T is something we want to treat as a scalar type for
2884 the purpose of deciding whether it is trivial/POD/standard-layout. */
2886 bool
2887 scalarish_type_p (const_tree t)
2889 if (t == error_mark_node)
2890 return 1;
2892 return (SCALAR_TYPE_P (t)
2893 || TREE_CODE (t) == VECTOR_TYPE);
2896 /* Returns true iff T requires non-trivial default initialization. */
2898 bool
2899 type_has_nontrivial_default_init (const_tree t)
2901 t = strip_array_types (CONST_CAST_TREE (t));
2903 if (CLASS_TYPE_P (t))
2904 return TYPE_HAS_COMPLEX_DFLT (t);
2905 else
2906 return 0;
2909 /* Returns true iff copying an object of type T (including via move
2910 constructor) is non-trivial. That is, T has no non-trivial copy
2911 constructors and no non-trivial move constructors. */
2913 bool
2914 type_has_nontrivial_copy_init (const_tree t)
2916 t = strip_array_types (CONST_CAST_TREE (t));
2918 if (CLASS_TYPE_P (t))
2920 gcc_assert (COMPLETE_TYPE_P (t));
2921 return ((TYPE_HAS_COPY_CTOR (t)
2922 && TYPE_HAS_COMPLEX_COPY_CTOR (t))
2923 || TYPE_HAS_COMPLEX_MOVE_CTOR (t));
2925 else
2926 return 0;
2929 /* Returns 1 iff type T is a trivially copyable type, as defined in
2930 [basic.types] and [class]. */
2932 bool
2933 trivially_copyable_p (const_tree t)
2935 t = strip_array_types (CONST_CAST_TREE (t));
2937 if (CLASS_TYPE_P (t))
2938 return ((!TYPE_HAS_COPY_CTOR (t)
2939 || !TYPE_HAS_COMPLEX_COPY_CTOR (t))
2940 && !TYPE_HAS_COMPLEX_MOVE_CTOR (t)
2941 && (!TYPE_HAS_COPY_ASSIGN (t)
2942 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (t))
2943 && !TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
2944 && TYPE_HAS_TRIVIAL_DESTRUCTOR (t));
2945 else
2946 return scalarish_type_p (t);
2949 /* Returns 1 iff type T is a trivial type, as defined in [basic.types] and
2950 [class]. */
2952 bool
2953 trivial_type_p (const_tree t)
2955 t = strip_array_types (CONST_CAST_TREE (t));
2957 if (CLASS_TYPE_P (t))
2958 return (TYPE_HAS_TRIVIAL_DFLT (t)
2959 && trivially_copyable_p (t));
2960 else
2961 return scalarish_type_p (t);
2964 /* Returns 1 iff type T is a POD type, as defined in [basic.types]. */
2966 bool
2967 pod_type_p (const_tree t)
2969 /* This CONST_CAST is okay because strip_array_types returns its
2970 argument unmodified and we assign it to a const_tree. */
2971 t = strip_array_types (CONST_CAST_TREE(t));
2973 if (!CLASS_TYPE_P (t))
2974 return scalarish_type_p (t);
2975 else if (cxx_dialect > cxx98)
2976 /* [class]/10: A POD struct is a class that is both a trivial class and a
2977 standard-layout class, and has no non-static data members of type
2978 non-POD struct, non-POD union (or array of such types).
2980 We don't need to check individual members because if a member is
2981 non-std-layout or non-trivial, the class will be too. */
2982 return (std_layout_type_p (t) && trivial_type_p (t));
2983 else
2984 /* The C++98 definition of POD is different. */
2985 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
2988 /* Returns true iff T is POD for the purpose of layout, as defined in the
2989 C++ ABI. */
2991 bool
2992 layout_pod_type_p (const_tree t)
2994 t = strip_array_types (CONST_CAST_TREE (t));
2996 if (CLASS_TYPE_P (t))
2997 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
2998 else
2999 return scalarish_type_p (t);
3002 /* Returns true iff T is a standard-layout type, as defined in
3003 [basic.types]. */
3005 bool
3006 std_layout_type_p (const_tree t)
3008 t = strip_array_types (CONST_CAST_TREE (t));
3010 if (CLASS_TYPE_P (t))
3011 return !CLASSTYPE_NON_STD_LAYOUT (t);
3012 else
3013 return scalarish_type_p (t);
3016 /* Nonzero iff type T is a class template implicit specialization. */
3018 bool
3019 class_tmpl_impl_spec_p (const_tree t)
3021 return CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INSTANTIATION (t);
3024 /* Returns 1 iff zero initialization of type T means actually storing
3025 zeros in it. */
3028 zero_init_p (const_tree t)
3030 /* This CONST_CAST is okay because strip_array_types returns its
3031 argument unmodified and we assign it to a const_tree. */
3032 t = strip_array_types (CONST_CAST_TREE(t));
3034 if (t == error_mark_node)
3035 return 1;
3037 /* NULL pointers to data members are initialized with -1. */
3038 if (TYPE_PTRDATAMEM_P (t))
3039 return 0;
3041 /* Classes that contain types that can't be zero-initialized, cannot
3042 be zero-initialized themselves. */
3043 if (CLASS_TYPE_P (t) && CLASSTYPE_NON_ZERO_INIT_P (t))
3044 return 0;
3046 return 1;
3049 /* Table of valid C++ attributes. */
3050 const struct attribute_spec cxx_attribute_table[] =
3052 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
3053 affects_type_identity } */
3054 { "java_interface", 0, 0, false, false, false,
3055 handle_java_interface_attribute, false },
3056 { "com_interface", 0, 0, false, false, false,
3057 handle_com_interface_attribute, false },
3058 { "init_priority", 1, 1, true, false, false,
3059 handle_init_priority_attribute, false },
3060 { "abi_tag", 1, -1, false, false, false,
3061 handle_abi_tag_attribute, true },
3062 { NULL, 0, 0, false, false, false, NULL, false }
3065 /* Handle a "java_interface" attribute; arguments as in
3066 struct attribute_spec.handler. */
3067 static tree
3068 handle_java_interface_attribute (tree* node,
3069 tree name,
3070 tree /*args*/,
3071 int flags,
3072 bool* no_add_attrs)
3074 if (DECL_P (*node)
3075 || !CLASS_TYPE_P (*node)
3076 || !TYPE_FOR_JAVA (*node))
3078 error ("%qE attribute can only be applied to Java class definitions",
3079 name);
3080 *no_add_attrs = true;
3081 return NULL_TREE;
3083 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
3084 *node = build_variant_type_copy (*node);
3085 TYPE_JAVA_INTERFACE (*node) = 1;
3087 return NULL_TREE;
3090 /* Handle a "com_interface" attribute; arguments as in
3091 struct attribute_spec.handler. */
3092 static tree
3093 handle_com_interface_attribute (tree* node,
3094 tree name,
3095 tree /*args*/,
3096 int /*flags*/,
3097 bool* no_add_attrs)
3099 static int warned;
3101 *no_add_attrs = true;
3103 if (DECL_P (*node)
3104 || !CLASS_TYPE_P (*node)
3105 || *node != TYPE_MAIN_VARIANT (*node))
3107 warning (OPT_Wattributes, "%qE attribute can only be applied "
3108 "to class definitions", name);
3109 return NULL_TREE;
3112 if (!warned++)
3113 warning (0, "%qE is obsolete; g++ vtables are now COM-compatible by default",
3114 name);
3116 return NULL_TREE;
3119 /* Handle an "init_priority" attribute; arguments as in
3120 struct attribute_spec.handler. */
3121 static tree
3122 handle_init_priority_attribute (tree* node,
3123 tree name,
3124 tree args,
3125 int /*flags*/,
3126 bool* no_add_attrs)
3128 tree initp_expr = TREE_VALUE (args);
3129 tree decl = *node;
3130 tree type = TREE_TYPE (decl);
3131 int pri;
3133 STRIP_NOPS (initp_expr);
3135 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
3137 error ("requested init_priority is not an integer constant");
3138 *no_add_attrs = true;
3139 return NULL_TREE;
3142 pri = TREE_INT_CST_LOW (initp_expr);
3144 type = strip_array_types (type);
3146 if (decl == NULL_TREE
3147 || TREE_CODE (decl) != VAR_DECL
3148 || !TREE_STATIC (decl)
3149 || DECL_EXTERNAL (decl)
3150 || (TREE_CODE (type) != RECORD_TYPE
3151 && TREE_CODE (type) != UNION_TYPE)
3152 /* Static objects in functions are initialized the
3153 first time control passes through that
3154 function. This is not precise enough to pin down an
3155 init_priority value, so don't allow it. */
3156 || current_function_decl)
3158 error ("can only use %qE attribute on file-scope definitions "
3159 "of objects of class type", name);
3160 *no_add_attrs = true;
3161 return NULL_TREE;
3164 if (pri > MAX_INIT_PRIORITY || pri <= 0)
3166 error ("requested init_priority is out of range");
3167 *no_add_attrs = true;
3168 return NULL_TREE;
3171 /* Check for init_priorities that are reserved for
3172 language and runtime support implementations.*/
3173 if (pri <= MAX_RESERVED_INIT_PRIORITY)
3175 warning
3176 (0, "requested init_priority is reserved for internal use");
3179 if (SUPPORTS_INIT_PRIORITY)
3181 SET_DECL_INIT_PRIORITY (decl, pri);
3182 DECL_HAS_INIT_PRIORITY_P (decl) = 1;
3183 return NULL_TREE;
3185 else
3187 error ("%qE attribute is not supported on this platform", name);
3188 *no_add_attrs = true;
3189 return NULL_TREE;
3193 /* DECL is being redeclared; the old declaration had the abi tags in OLD,
3194 and the new one has the tags in NEW_. Give an error if there are tags
3195 in NEW_ that weren't in OLD. */
3197 bool
3198 check_abi_tag_redeclaration (const_tree decl, const_tree old, const_tree new_)
3200 if (old && TREE_CODE (TREE_VALUE (old)) == TREE_LIST)
3201 old = TREE_VALUE (old);
3202 if (new_ && TREE_CODE (TREE_VALUE (new_)) == TREE_LIST)
3203 new_ = TREE_VALUE (new_);
3204 bool err = false;
3205 for (const_tree t = new_; t; t = TREE_CHAIN (t))
3207 tree str = TREE_VALUE (t);
3208 for (const_tree in = old; in; in = TREE_CHAIN (in))
3210 tree ostr = TREE_VALUE (in);
3211 if (cp_tree_equal (str, ostr))
3212 goto found;
3214 error ("redeclaration of %qD adds abi tag %E", decl, str);
3215 err = true;
3216 found:;
3218 if (err)
3220 inform (DECL_SOURCE_LOCATION (decl), "previous declaration here");
3221 return false;
3223 return true;
3226 /* Handle an "abi_tag" attribute; arguments as in
3227 struct attribute_spec.handler. */
3229 static tree
3230 handle_abi_tag_attribute (tree* node, tree name, tree args,
3231 int flags, bool* no_add_attrs)
3233 if (TYPE_P (*node))
3235 if (!TAGGED_TYPE_P (*node))
3237 error ("%qE attribute applied to non-class, non-enum type %qT",
3238 name, *node);
3239 goto fail;
3241 else if (!(flags & (int)ATTR_FLAG_TYPE_IN_PLACE))
3243 error ("%qE attribute applied to %qT after its definition",
3244 name, *node);
3245 goto fail;
3248 tree attributes = TYPE_ATTRIBUTES (*node);
3249 tree decl = TYPE_NAME (*node);
3251 /* Make sure all declarations have the same abi tags. */
3252 if (DECL_SOURCE_LOCATION (decl) != input_location)
3254 if (!check_abi_tag_redeclaration (decl,
3255 lookup_attribute ("abi_tag",
3256 attributes),
3257 args))
3258 goto fail;
3261 else
3263 if (TREE_CODE (*node) != FUNCTION_DECL)
3265 error ("%qE attribute applied to non-function %qD", name, *node);
3266 goto fail;
3268 else if (DECL_LANGUAGE (*node) == lang_c)
3270 error ("%qE attribute applied to extern \"C\" function %qD",
3271 name, *node);
3272 goto fail;
3276 return NULL_TREE;
3278 fail:
3279 *no_add_attrs = true;
3280 return NULL_TREE;
3283 /* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the
3284 thing pointed to by the constant. */
3286 tree
3287 make_ptrmem_cst (tree type, tree member)
3289 tree ptrmem_cst = make_node (PTRMEM_CST);
3290 TREE_TYPE (ptrmem_cst) = type;
3291 PTRMEM_CST_MEMBER (ptrmem_cst) = member;
3292 return ptrmem_cst;
3295 /* Build a variant of TYPE that has the indicated ATTRIBUTES. May
3296 return an existing type if an appropriate type already exists. */
3298 tree
3299 cp_build_type_attribute_variant (tree type, tree attributes)
3301 tree new_type;
3303 new_type = build_type_attribute_variant (type, attributes);
3304 if (TREE_CODE (new_type) == FUNCTION_TYPE
3305 || TREE_CODE (new_type) == METHOD_TYPE)
3306 new_type = build_exception_variant (new_type,
3307 TYPE_RAISES_EXCEPTIONS (type));
3309 /* Making a new main variant of a class type is broken. */
3310 gcc_assert (!CLASS_TYPE_P (type) || new_type == type);
3312 return new_type;
3315 /* Return TRUE if TYPE1 and TYPE2 are identical for type hashing purposes.
3316 Called only after doing all language independent checks. Only
3317 to check TYPE_RAISES_EXCEPTIONS for FUNCTION_TYPE, the rest is already
3318 compared in type_hash_eq. */
3320 bool
3321 cxx_type_hash_eq (const_tree typea, const_tree typeb)
3323 gcc_assert (TREE_CODE (typea) == FUNCTION_TYPE
3324 || TREE_CODE (typea) == METHOD_TYPE);
3326 return comp_except_specs (TYPE_RAISES_EXCEPTIONS (typea),
3327 TYPE_RAISES_EXCEPTIONS (typeb), ce_exact);
3330 /* Apply FUNC to all language-specific sub-trees of TP in a pre-order
3331 traversal. Called from walk_tree. */
3333 tree
3334 cp_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func,
3335 void *data, struct pointer_set_t *pset)
3337 enum tree_code code = TREE_CODE (*tp);
3338 tree result;
3340 #define WALK_SUBTREE(NODE) \
3341 do \
3343 result = cp_walk_tree (&(NODE), func, data, pset); \
3344 if (result) goto out; \
3346 while (0)
3348 /* Not one of the easy cases. We must explicitly go through the
3349 children. */
3350 result = NULL_TREE;
3351 switch (code)
3353 case DEFAULT_ARG:
3354 case TEMPLATE_TEMPLATE_PARM:
3355 case BOUND_TEMPLATE_TEMPLATE_PARM:
3356 case UNBOUND_CLASS_TEMPLATE:
3357 case TEMPLATE_PARM_INDEX:
3358 case TEMPLATE_TYPE_PARM:
3359 case TYPENAME_TYPE:
3360 case TYPEOF_TYPE:
3361 case UNDERLYING_TYPE:
3362 /* None of these have subtrees other than those already walked
3363 above. */
3364 *walk_subtrees_p = 0;
3365 break;
3367 case BASELINK:
3368 WALK_SUBTREE (BASELINK_FUNCTIONS (*tp));
3369 *walk_subtrees_p = 0;
3370 break;
3372 case PTRMEM_CST:
3373 WALK_SUBTREE (TREE_TYPE (*tp));
3374 *walk_subtrees_p = 0;
3375 break;
3377 case TREE_LIST:
3378 WALK_SUBTREE (TREE_PURPOSE (*tp));
3379 break;
3381 case OVERLOAD:
3382 WALK_SUBTREE (OVL_FUNCTION (*tp));
3383 WALK_SUBTREE (OVL_CHAIN (*tp));
3384 *walk_subtrees_p = 0;
3385 break;
3387 case USING_DECL:
3388 WALK_SUBTREE (DECL_NAME (*tp));
3389 WALK_SUBTREE (USING_DECL_SCOPE (*tp));
3390 WALK_SUBTREE (USING_DECL_DECLS (*tp));
3391 *walk_subtrees_p = 0;
3392 break;
3394 case RECORD_TYPE:
3395 if (TYPE_PTRMEMFUNC_P (*tp))
3396 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (*tp));
3397 break;
3399 case TYPE_ARGUMENT_PACK:
3400 case NONTYPE_ARGUMENT_PACK:
3402 tree args = ARGUMENT_PACK_ARGS (*tp);
3403 int i, len = TREE_VEC_LENGTH (args);
3404 for (i = 0; i < len; i++)
3405 WALK_SUBTREE (TREE_VEC_ELT (args, i));
3407 break;
3409 case TYPE_PACK_EXPANSION:
3410 WALK_SUBTREE (TREE_TYPE (*tp));
3411 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp));
3412 *walk_subtrees_p = 0;
3413 break;
3415 case EXPR_PACK_EXPANSION:
3416 WALK_SUBTREE (TREE_OPERAND (*tp, 0));
3417 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp));
3418 *walk_subtrees_p = 0;
3419 break;
3421 case CAST_EXPR:
3422 case REINTERPRET_CAST_EXPR:
3423 case STATIC_CAST_EXPR:
3424 case CONST_CAST_EXPR:
3425 case DYNAMIC_CAST_EXPR:
3426 case IMPLICIT_CONV_EXPR:
3427 if (TREE_TYPE (*tp))
3428 WALK_SUBTREE (TREE_TYPE (*tp));
3431 int i;
3432 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (*tp)); ++i)
3433 WALK_SUBTREE (TREE_OPERAND (*tp, i));
3435 *walk_subtrees_p = 0;
3436 break;
3438 case TRAIT_EXPR:
3439 WALK_SUBTREE (TRAIT_EXPR_TYPE1 (*tp));
3440 WALK_SUBTREE (TRAIT_EXPR_TYPE2 (*tp));
3441 *walk_subtrees_p = 0;
3442 break;
3444 case DECLTYPE_TYPE:
3445 WALK_SUBTREE (DECLTYPE_TYPE_EXPR (*tp));
3446 *walk_subtrees_p = 0;
3447 break;
3450 default:
3451 return NULL_TREE;
3454 /* We didn't find what we were looking for. */
3455 out:
3456 return result;
3458 #undef WALK_SUBTREE
3461 /* Like save_expr, but for C++. */
3463 tree
3464 cp_save_expr (tree expr)
3466 /* There is no reason to create a SAVE_EXPR within a template; if
3467 needed, we can create the SAVE_EXPR when instantiating the
3468 template. Furthermore, the middle-end cannot handle C++-specific
3469 tree codes. */
3470 if (processing_template_decl)
3471 return expr;
3472 return save_expr (expr);
3475 /* Initialize tree.c. */
3477 void
3478 init_tree (void)
3480 list_hash_table = htab_create_ggc (31, list_hash, list_hash_eq, NULL);
3483 /* Returns the kind of special function that DECL (a FUNCTION_DECL)
3484 is. Note that sfk_none is zero, so this function can be used as a
3485 predicate to test whether or not DECL is a special function. */
3487 special_function_kind
3488 special_function_p (const_tree decl)
3490 /* Rather than doing all this stuff with magic names, we should
3491 probably have a field of type `special_function_kind' in
3492 DECL_LANG_SPECIFIC. */
3493 if (DECL_INHERITED_CTOR_BASE (decl))
3494 return sfk_inheriting_constructor;
3495 if (DECL_COPY_CONSTRUCTOR_P (decl))
3496 return sfk_copy_constructor;
3497 if (DECL_MOVE_CONSTRUCTOR_P (decl))
3498 return sfk_move_constructor;
3499 if (DECL_CONSTRUCTOR_P (decl))
3500 return sfk_constructor;
3501 if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
3503 if (copy_fn_p (decl))
3504 return sfk_copy_assignment;
3505 if (move_fn_p (decl))
3506 return sfk_move_assignment;
3508 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
3509 return sfk_destructor;
3510 if (DECL_COMPLETE_DESTRUCTOR_P (decl))
3511 return sfk_complete_destructor;
3512 if (DECL_BASE_DESTRUCTOR_P (decl))
3513 return sfk_base_destructor;
3514 if (DECL_DELETING_DESTRUCTOR_P (decl))
3515 return sfk_deleting_destructor;
3516 if (DECL_CONV_FN_P (decl))
3517 return sfk_conversion;
3519 return sfk_none;
3522 /* Returns nonzero if TYPE is a character type, including wchar_t. */
3525 char_type_p (tree type)
3527 return (same_type_p (type, char_type_node)
3528 || same_type_p (type, unsigned_char_type_node)
3529 || same_type_p (type, signed_char_type_node)
3530 || same_type_p (type, char16_type_node)
3531 || same_type_p (type, char32_type_node)
3532 || same_type_p (type, wchar_type_node));
3535 /* Returns the kind of linkage associated with the indicated DECL. Th
3536 value returned is as specified by the language standard; it is
3537 independent of implementation details regarding template
3538 instantiation, etc. For example, it is possible that a declaration
3539 to which this function assigns external linkage would not show up
3540 as a global symbol when you run `nm' on the resulting object file. */
3542 linkage_kind
3543 decl_linkage (tree decl)
3545 /* This function doesn't attempt to calculate the linkage from first
3546 principles as given in [basic.link]. Instead, it makes use of
3547 the fact that we have already set TREE_PUBLIC appropriately, and
3548 then handles a few special cases. Ideally, we would calculate
3549 linkage first, and then transform that into a concrete
3550 implementation. */
3552 /* Things that don't have names have no linkage. */
3553 if (!DECL_NAME (decl))
3554 return lk_none;
3556 /* Fields have no linkage. */
3557 if (TREE_CODE (decl) == FIELD_DECL)
3558 return lk_none;
3560 /* Things that are TREE_PUBLIC have external linkage. */
3561 if (TREE_PUBLIC (decl))
3562 return lk_external;
3564 if (TREE_CODE (decl) == NAMESPACE_DECL)
3565 return lk_external;
3567 /* Linkage of a CONST_DECL depends on the linkage of the enumeration
3568 type. */
3569 if (TREE_CODE (decl) == CONST_DECL)
3570 return decl_linkage (TYPE_NAME (DECL_CONTEXT (decl)));
3572 /* Some things that are not TREE_PUBLIC have external linkage, too.
3573 For example, on targets that don't have weak symbols, we make all
3574 template instantiations have internal linkage (in the object
3575 file), but the symbols should still be treated as having external
3576 linkage from the point of view of the language. */
3577 if ((TREE_CODE (decl) == FUNCTION_DECL
3578 || TREE_CODE (decl) == VAR_DECL)
3579 && DECL_COMDAT (decl))
3580 return lk_external;
3582 /* Things in local scope do not have linkage, if they don't have
3583 TREE_PUBLIC set. */
3584 if (decl_function_context (decl))
3585 return lk_none;
3587 /* Members of the anonymous namespace also have TREE_PUBLIC unset, but
3588 are considered to have external linkage for language purposes. DECLs
3589 really meant to have internal linkage have DECL_THIS_STATIC set. */
3590 if (TREE_CODE (decl) == TYPE_DECL)
3591 return lk_external;
3592 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3594 if (!DECL_THIS_STATIC (decl))
3595 return lk_external;
3597 /* Static data members and static member functions from classes
3598 in anonymous namespace also don't have TREE_PUBLIC set. */
3599 if (DECL_CLASS_CONTEXT (decl))
3600 return lk_external;
3603 /* Everything else has internal linkage. */
3604 return lk_internal;
3607 /* Returns the storage duration of the object or reference associated with
3608 the indicated DECL, which should be a VAR_DECL or PARM_DECL. */
3610 duration_kind
3611 decl_storage_duration (tree decl)
3613 if (TREE_CODE (decl) == PARM_DECL)
3614 return dk_auto;
3615 if (TREE_CODE (decl) == FUNCTION_DECL)
3616 return dk_static;
3617 gcc_assert (TREE_CODE (decl) == VAR_DECL);
3618 if (!TREE_STATIC (decl)
3619 && !DECL_EXTERNAL (decl))
3620 return dk_auto;
3621 if (DECL_THREAD_LOCAL_P (decl))
3622 return dk_thread;
3623 return dk_static;
3626 /* EXP is an expression that we want to pre-evaluate. Returns (in
3627 *INITP) an expression that will perform the pre-evaluation. The
3628 value returned by this function is a side-effect free expression
3629 equivalent to the pre-evaluated expression. Callers must ensure
3630 that *INITP is evaluated before EXP. */
3632 tree
3633 stabilize_expr (tree exp, tree* initp)
3635 tree init_expr;
3637 if (!TREE_SIDE_EFFECTS (exp))
3638 init_expr = NULL_TREE;
3639 else if (VOID_TYPE_P (TREE_TYPE (exp)))
3641 init_expr = exp;
3642 exp = void_zero_node;
3644 /* There are no expressions with REFERENCE_TYPE, but there can be call
3645 arguments with such a type; just treat it as a pointer. */
3646 else if (TREE_CODE (TREE_TYPE (exp)) == REFERENCE_TYPE
3647 || SCALAR_TYPE_P (TREE_TYPE (exp))
3648 || !lvalue_or_rvalue_with_address_p (exp))
3650 init_expr = get_target_expr (exp);
3651 exp = TARGET_EXPR_SLOT (init_expr);
3653 else
3655 bool xval = !real_lvalue_p (exp);
3656 exp = cp_build_addr_expr (exp, tf_warning_or_error);
3657 init_expr = get_target_expr (exp);
3658 exp = TARGET_EXPR_SLOT (init_expr);
3659 exp = cp_build_indirect_ref (exp, RO_NULL, tf_warning_or_error);
3660 if (xval)
3661 exp = move (exp);
3663 *initp = init_expr;
3665 gcc_assert (!TREE_SIDE_EFFECTS (exp));
3666 return exp;
3669 /* Add NEW_EXPR, an expression whose value we don't care about, after the
3670 similar expression ORIG. */
3672 tree
3673 add_stmt_to_compound (tree orig, tree new_expr)
3675 if (!new_expr || !TREE_SIDE_EFFECTS (new_expr))
3676 return orig;
3677 if (!orig || !TREE_SIDE_EFFECTS (orig))
3678 return new_expr;
3679 return build2 (COMPOUND_EXPR, void_type_node, orig, new_expr);
3682 /* Like stabilize_expr, but for a call whose arguments we want to
3683 pre-evaluate. CALL is modified in place to use the pre-evaluated
3684 arguments, while, upon return, *INITP contains an expression to
3685 compute the arguments. */
3687 void
3688 stabilize_call (tree call, tree *initp)
3690 tree inits = NULL_TREE;
3691 int i;
3692 int nargs = call_expr_nargs (call);
3694 if (call == error_mark_node || processing_template_decl)
3696 *initp = NULL_TREE;
3697 return;
3700 gcc_assert (TREE_CODE (call) == CALL_EXPR);
3702 for (i = 0; i < nargs; i++)
3704 tree init;
3705 CALL_EXPR_ARG (call, i) =
3706 stabilize_expr (CALL_EXPR_ARG (call, i), &init);
3707 inits = add_stmt_to_compound (inits, init);
3710 *initp = inits;
3713 /* Like stabilize_expr, but for an AGGR_INIT_EXPR whose arguments we want
3714 to pre-evaluate. CALL is modified in place to use the pre-evaluated
3715 arguments, while, upon return, *INITP contains an expression to
3716 compute the arguments. */
3718 static void
3719 stabilize_aggr_init (tree call, tree *initp)
3721 tree inits = NULL_TREE;
3722 int i;
3723 int nargs = aggr_init_expr_nargs (call);
3725 if (call == error_mark_node)
3726 return;
3728 gcc_assert (TREE_CODE (call) == AGGR_INIT_EXPR);
3730 for (i = 0; i < nargs; i++)
3732 tree init;
3733 AGGR_INIT_EXPR_ARG (call, i) =
3734 stabilize_expr (AGGR_INIT_EXPR_ARG (call, i), &init);
3735 inits = add_stmt_to_compound (inits, init);
3738 *initp = inits;
3741 /* Like stabilize_expr, but for an initialization.
3743 If the initialization is for an object of class type, this function
3744 takes care not to introduce additional temporaries.
3746 Returns TRUE iff the expression was successfully pre-evaluated,
3747 i.e., if INIT is now side-effect free, except for, possibly, a
3748 single call to a constructor. */
3750 bool
3751 stabilize_init (tree init, tree *initp)
3753 tree t = init;
3755 *initp = NULL_TREE;
3757 if (t == error_mark_node || processing_template_decl)
3758 return true;
3760 if (TREE_CODE (t) == INIT_EXPR)
3761 t = TREE_OPERAND (t, 1);
3762 if (TREE_CODE (t) == TARGET_EXPR)
3763 t = TARGET_EXPR_INITIAL (t);
3765 /* If the RHS can be stabilized without breaking copy elision, stabilize
3766 it. We specifically don't stabilize class prvalues here because that
3767 would mean an extra copy, but they might be stabilized below. */
3768 if (TREE_CODE (init) == INIT_EXPR
3769 && TREE_CODE (t) != CONSTRUCTOR
3770 && TREE_CODE (t) != AGGR_INIT_EXPR
3771 && (SCALAR_TYPE_P (TREE_TYPE (t))
3772 || lvalue_or_rvalue_with_address_p (t)))
3774 TREE_OPERAND (init, 1) = stabilize_expr (t, initp);
3775 return true;
3778 if (TREE_CODE (t) == COMPOUND_EXPR
3779 && TREE_CODE (init) == INIT_EXPR)
3781 tree last = expr_last (t);
3782 /* Handle stabilizing the EMPTY_CLASS_EXPR pattern. */
3783 if (!TREE_SIDE_EFFECTS (last))
3785 *initp = t;
3786 TREE_OPERAND (init, 1) = last;
3787 return true;
3791 if (TREE_CODE (t) == CONSTRUCTOR)
3793 /* Aggregate initialization: stabilize each of the field
3794 initializers. */
3795 unsigned i;
3796 constructor_elt *ce;
3797 bool good = true;
3798 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
3799 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
3801 tree type = TREE_TYPE (ce->value);
3802 tree subinit;
3803 if (TREE_CODE (type) == REFERENCE_TYPE
3804 || SCALAR_TYPE_P (type))
3805 ce->value = stabilize_expr (ce->value, &subinit);
3806 else if (!stabilize_init (ce->value, &subinit))
3807 good = false;
3808 *initp = add_stmt_to_compound (*initp, subinit);
3810 return good;
3813 if (TREE_CODE (t) == CALL_EXPR)
3815 stabilize_call (t, initp);
3816 return true;
3819 if (TREE_CODE (t) == AGGR_INIT_EXPR)
3821 stabilize_aggr_init (t, initp);
3822 return true;
3825 /* The initialization is being performed via a bitwise copy -- and
3826 the item copied may have side effects. */
3827 return !TREE_SIDE_EFFECTS (init);
3830 /* Like "fold", but should be used whenever we might be processing the
3831 body of a template. */
3833 tree
3834 fold_if_not_in_template (tree expr)
3836 /* In the body of a template, there is never any need to call
3837 "fold". We will call fold later when actually instantiating the
3838 template. Integral constant expressions in templates will be
3839 evaluated via fold_non_dependent_expr, as necessary. */
3840 if (processing_template_decl)
3841 return expr;
3843 /* Fold C++ front-end specific tree codes. */
3844 if (TREE_CODE (expr) == UNARY_PLUS_EXPR)
3845 return fold_convert (TREE_TYPE (expr), TREE_OPERAND (expr, 0));
3847 return fold (expr);
3850 /* Returns true if a cast to TYPE may appear in an integral constant
3851 expression. */
3853 bool
3854 cast_valid_in_integral_constant_expression_p (tree type)
3856 return (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3857 || cxx_dialect >= cxx0x
3858 || dependent_type_p (type)
3859 || type == error_mark_node);
3862 /* Return true if we need to fix linkage information of DECL. */
3864 static bool
3865 cp_fix_function_decl_p (tree decl)
3867 /* Skip if DECL is not externally visible. */
3868 if (!TREE_PUBLIC (decl))
3869 return false;
3871 /* We need to fix DECL if it a appears to be exported but with no
3872 function body. Thunks do not have CFGs and we may need to
3873 handle them specially later. */
3874 if (!gimple_has_body_p (decl)
3875 && !DECL_THUNK_P (decl)
3876 && !DECL_EXTERNAL (decl))
3878 struct cgraph_node *node = cgraph_get_node (decl);
3880 /* Don't fix same_body aliases. Although they don't have their own
3881 CFG, they share it with what they alias to. */
3882 if (!node || !node->alias
3883 || !vec_safe_length (node->symbol.ref_list.references))
3884 return true;
3887 return false;
3890 /* Clean the C++ specific parts of the tree T. */
3892 void
3893 cp_free_lang_data (tree t)
3895 if (TREE_CODE (t) == METHOD_TYPE
3896 || TREE_CODE (t) == FUNCTION_TYPE)
3898 /* Default args are not interesting anymore. */
3899 tree argtypes = TYPE_ARG_TYPES (t);
3900 while (argtypes)
3902 TREE_PURPOSE (argtypes) = 0;
3903 argtypes = TREE_CHAIN (argtypes);
3906 else if (TREE_CODE (t) == FUNCTION_DECL
3907 && cp_fix_function_decl_p (t))
3909 /* If T is used in this translation unit at all, the definition
3910 must exist somewhere else since we have decided to not emit it
3911 in this TU. So make it an external reference. */
3912 DECL_EXTERNAL (t) = 1;
3913 TREE_STATIC (t) = 0;
3915 if (TREE_CODE (t) == NAMESPACE_DECL)
3917 /* The list of users of a namespace isn't useful for the middle-end
3918 or debug generators. */
3919 DECL_NAMESPACE_USERS (t) = NULL_TREE;
3920 /* Neither do we need the leftover chaining of namespaces
3921 from the binding level. */
3922 DECL_CHAIN (t) = NULL_TREE;
3926 /* Stub for c-common. Please keep in sync with c-decl.c.
3927 FIXME: If address space support is target specific, then this
3928 should be a C target hook. But currently this is not possible,
3929 because this function is called via REGISTER_TARGET_PRAGMAS. */
3930 void
3931 c_register_addr_space (const char * /*word*/, addr_space_t /*as*/)
3935 /* Return the number of operands in T that we care about for things like
3936 mangling. */
3939 cp_tree_operand_length (const_tree t)
3941 enum tree_code code = TREE_CODE (t);
3943 switch (code)
3945 case PREINCREMENT_EXPR:
3946 case PREDECREMENT_EXPR:
3947 case POSTINCREMENT_EXPR:
3948 case POSTDECREMENT_EXPR:
3949 return 1;
3951 case ARRAY_REF:
3952 return 2;
3954 case EXPR_PACK_EXPANSION:
3955 return 1;
3957 default:
3958 return TREE_OPERAND_LENGTH (t);
3962 /* Implement -Wzero_as_null_pointer_constant. Return true if the
3963 conditions for the warning hold, false otherwise. */
3964 bool
3965 maybe_warn_zero_as_null_pointer_constant (tree expr, location_t loc)
3967 if (c_inhibit_evaluation_warnings == 0
3968 && !NULLPTR_TYPE_P (TREE_TYPE (expr)))
3970 warning_at (loc, OPT_Wzero_as_null_pointer_constant,
3971 "zero as null pointer constant");
3972 return true;
3974 return false;
3977 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
3978 /* Complain that some language-specific thing hanging off a tree
3979 node has been accessed improperly. */
3981 void
3982 lang_check_failed (const char* file, int line, const char* function)
3984 internal_error ("lang_* check: failed in %s, at %s:%d",
3985 function, trim_filename (file), line);
3987 #endif /* ENABLE_TREE_CHECKING */
3989 #include "gt-cp-tree.h"