Merge trunk version 194351 into gupc branch.
[official-gcc.git] / gcc / cp / tree.c
blob4bac537af2566622938c321377e42199cc545d3b
1 /* Language-dependent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011,
4 2012
5 Free Software Foundation, Inc.
6 Hacked by Michael Tiemann (tiemann@cygnus.com)
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "flags.h"
31 #include "tree-inline.h"
32 #include "debug.h"
33 #include "convert.h"
34 #include "cgraph.h"
35 #include "splay-tree.h"
36 #include "gimple.h" /* gimple_has_body_p */
37 #include "hash-table.h"
39 static tree bot_manip (tree *, int *, void *);
40 static tree bot_replace (tree *, int *, void *);
41 static int list_hash_eq (const void *, const void *);
42 static hashval_t list_hash_pieces (tree, tree, tree);
43 static hashval_t list_hash (const void *);
44 static tree build_target_expr (tree, tree, tsubst_flags_t);
45 static tree count_trees_r (tree *, int *, void *);
46 static tree verify_stmt_tree_r (tree *, int *, void *);
47 static tree build_local_temp (tree);
49 static tree handle_java_interface_attribute (tree *, tree, tree, int, bool *);
50 static tree handle_com_interface_attribute (tree *, tree, tree, int, bool *);
51 static tree handle_init_priority_attribute (tree *, tree, tree, int, bool *);
52 static tree handle_abi_tag_attribute (tree *, tree, tree, int, bool *);
54 /* If REF is an lvalue, returns the kind of lvalue that REF is.
55 Otherwise, returns clk_none. */
57 cp_lvalue_kind
58 lvalue_kind (const_tree ref)
60 cp_lvalue_kind op1_lvalue_kind = clk_none;
61 cp_lvalue_kind op2_lvalue_kind = clk_none;
63 /* Expressions of reference type are sometimes wrapped in
64 INDIRECT_REFs. INDIRECT_REFs are just internal compiler
65 representation, not part of the language, so we have to look
66 through them. */
67 if (REFERENCE_REF_P (ref))
68 return lvalue_kind (TREE_OPERAND (ref, 0));
70 if (TREE_TYPE (ref)
71 && TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
73 /* unnamed rvalue references are rvalues */
74 if (TYPE_REF_IS_RVALUE (TREE_TYPE (ref))
75 && TREE_CODE (ref) != PARM_DECL
76 && TREE_CODE (ref) != VAR_DECL
77 && TREE_CODE (ref) != COMPONENT_REF
78 /* Functions are always lvalues. */
79 && TREE_CODE (TREE_TYPE (TREE_TYPE (ref))) != FUNCTION_TYPE)
80 return clk_rvalueref;
82 /* lvalue references and named rvalue references are lvalues. */
83 return clk_ordinary;
86 if (ref == current_class_ptr)
87 return clk_none;
89 switch (TREE_CODE (ref))
91 case SAVE_EXPR:
92 return clk_none;
93 /* preincrements and predecrements are valid lvals, provided
94 what they refer to are valid lvals. */
95 case PREINCREMENT_EXPR:
96 case PREDECREMENT_EXPR:
97 case TRY_CATCH_EXPR:
98 case WITH_CLEANUP_EXPR:
99 case REALPART_EXPR:
100 case IMAGPART_EXPR:
101 return lvalue_kind (TREE_OPERAND (ref, 0));
103 case COMPONENT_REF:
104 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
105 /* Look at the member designator. */
106 if (!op1_lvalue_kind)
108 else if (is_overloaded_fn (TREE_OPERAND (ref, 1)))
109 /* The "field" can be a FUNCTION_DECL or an OVERLOAD in some
110 situations. If we're seeing a COMPONENT_REF, it's a non-static
111 member, so it isn't an lvalue. */
112 op1_lvalue_kind = clk_none;
113 else if (TREE_CODE (TREE_OPERAND (ref, 1)) != FIELD_DECL)
114 /* This can be IDENTIFIER_NODE in a template. */;
115 else if (DECL_C_BIT_FIELD (TREE_OPERAND (ref, 1)))
117 /* Clear the ordinary bit. If this object was a class
118 rvalue we want to preserve that information. */
119 op1_lvalue_kind &= ~clk_ordinary;
120 /* The lvalue is for a bitfield. */
121 op1_lvalue_kind |= clk_bitfield;
123 else if (DECL_PACKED (TREE_OPERAND (ref, 1)))
124 op1_lvalue_kind |= clk_packed;
126 return op1_lvalue_kind;
128 case STRING_CST:
129 case COMPOUND_LITERAL_EXPR:
130 return clk_ordinary;
132 case CONST_DECL:
133 /* CONST_DECL without TREE_STATIC are enumeration values and
134 thus not lvalues. With TREE_STATIC they are used by ObjC++
135 in objc_build_string_object and need to be considered as
136 lvalues. */
137 if (! TREE_STATIC (ref))
138 return clk_none;
139 case VAR_DECL:
140 if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
141 && DECL_LANG_SPECIFIC (ref)
142 && DECL_IN_AGGR_P (ref))
143 return clk_none;
144 case INDIRECT_REF:
145 case ARROW_EXPR:
146 case ARRAY_REF:
147 case PARM_DECL:
148 case RESULT_DECL:
149 if (TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
150 return clk_ordinary;
151 break;
153 /* A scope ref in a template, left as SCOPE_REF to support later
154 access checking. */
155 case SCOPE_REF:
156 gcc_assert (!type_dependent_expression_p (CONST_CAST_TREE (ref)));
158 tree op = TREE_OPERAND (ref, 1);
159 if (TREE_CODE (op) == FIELD_DECL)
160 return (DECL_C_BIT_FIELD (op) ? clk_bitfield : clk_ordinary);
161 else
162 return lvalue_kind (op);
165 case MAX_EXPR:
166 case MIN_EXPR:
167 /* Disallow <? and >? as lvalues if either argument side-effects. */
168 if (TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 0))
169 || TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 1)))
170 return clk_none;
171 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
172 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1));
173 break;
175 case COND_EXPR:
176 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1)
177 ? TREE_OPERAND (ref, 1)
178 : TREE_OPERAND (ref, 0));
179 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 2));
180 break;
182 case MODIFY_EXPR:
183 case TYPEID_EXPR:
184 return clk_ordinary;
186 case COMPOUND_EXPR:
187 return lvalue_kind (TREE_OPERAND (ref, 1));
189 case TARGET_EXPR:
190 return clk_class;
192 case VA_ARG_EXPR:
193 return (CLASS_TYPE_P (TREE_TYPE (ref)) ? clk_class : clk_none);
195 case CALL_EXPR:
196 /* We can see calls outside of TARGET_EXPR in templates. */
197 if (CLASS_TYPE_P (TREE_TYPE (ref)))
198 return clk_class;
199 return clk_none;
201 case FUNCTION_DECL:
202 /* All functions (except non-static-member functions) are
203 lvalues. */
204 return (DECL_NONSTATIC_MEMBER_FUNCTION_P (ref)
205 ? clk_none : clk_ordinary);
207 case BASELINK:
208 /* We now represent a reference to a single static member function
209 with a BASELINK. */
210 /* This CONST_CAST is okay because BASELINK_FUNCTIONS returns
211 its argument unmodified and we assign it to a const_tree. */
212 return lvalue_kind (BASELINK_FUNCTIONS (CONST_CAST_TREE (ref)));
214 case NON_DEPENDENT_EXPR:
215 /* We just return clk_ordinary for NON_DEPENDENT_EXPR in C++98, but
216 in C++11 lvalues don't bind to rvalue references, so we need to
217 work harder to avoid bogus errors (c++/44870). */
218 if (cxx_dialect < cxx0x)
219 return clk_ordinary;
220 else
221 return lvalue_kind (TREE_OPERAND (ref, 0));
223 default:
224 if (!TREE_TYPE (ref))
225 return clk_none;
226 if (CLASS_TYPE_P (TREE_TYPE (ref)))
227 return clk_class;
228 break;
231 /* If one operand is not an lvalue at all, then this expression is
232 not an lvalue. */
233 if (!op1_lvalue_kind || !op2_lvalue_kind)
234 return clk_none;
236 /* Otherwise, it's an lvalue, and it has all the odd properties
237 contributed by either operand. */
238 op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind;
239 /* It's not an ordinary lvalue if it involves any other kind. */
240 if ((op1_lvalue_kind & ~clk_ordinary) != clk_none)
241 op1_lvalue_kind &= ~clk_ordinary;
242 /* It can't be both a pseudo-lvalue and a non-addressable lvalue.
243 A COND_EXPR of those should be wrapped in a TARGET_EXPR. */
244 if ((op1_lvalue_kind & (clk_rvalueref|clk_class))
245 && (op1_lvalue_kind & (clk_bitfield|clk_packed)))
246 op1_lvalue_kind = clk_none;
247 return op1_lvalue_kind;
250 /* Returns the kind of lvalue that REF is, in the sense of
251 [basic.lval]. This function should really be named lvalue_p; it
252 computes the C++ definition of lvalue. */
254 cp_lvalue_kind
255 real_lvalue_p (const_tree ref)
257 cp_lvalue_kind kind = lvalue_kind (ref);
258 if (kind & (clk_rvalueref|clk_class))
259 return clk_none;
260 else
261 return kind;
264 /* This differs from real_lvalue_p in that class rvalues are considered
265 lvalues. */
267 bool
268 lvalue_p (const_tree ref)
270 return (lvalue_kind (ref) != clk_none);
273 /* This differs from real_lvalue_p in that rvalues formed by dereferencing
274 rvalue references are considered rvalues. */
276 bool
277 lvalue_or_rvalue_with_address_p (const_tree ref)
279 cp_lvalue_kind kind = lvalue_kind (ref);
280 if (kind & clk_class)
281 return false;
282 else
283 return (kind != clk_none);
286 /* Returns true if REF is an xvalue, false otherwise. */
288 bool
289 xvalue_p (const_tree ref)
291 return (lvalue_kind (ref) == clk_rvalueref);
294 /* Test whether DECL is a builtin that may appear in a
295 constant-expression. */
297 bool
298 builtin_valid_in_constant_expr_p (const_tree decl)
300 /* At present BUILT_IN_CONSTANT_P is the only builtin we're allowing
301 in constant-expressions. We may want to add other builtins later. */
302 return DECL_IS_BUILTIN_CONSTANT_P (decl);
305 /* Build a TARGET_EXPR, initializing the DECL with the VALUE. */
307 static tree
308 build_target_expr (tree decl, tree value, tsubst_flags_t complain)
310 tree t;
311 tree type = TREE_TYPE (decl);
313 #ifdef ENABLE_CHECKING
314 gcc_assert (VOID_TYPE_P (TREE_TYPE (value))
315 || TREE_TYPE (decl) == TREE_TYPE (value)
316 /* On ARM ctors return 'this'. */
317 || (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
318 && TREE_CODE (value) == CALL_EXPR)
319 || useless_type_conversion_p (TREE_TYPE (decl),
320 TREE_TYPE (value)));
321 #endif
323 t = cxx_maybe_build_cleanup (decl, complain);
324 if (t == error_mark_node)
325 return error_mark_node;
326 t = build4 (TARGET_EXPR, type, decl, value, t, NULL_TREE);
327 /* We always set TREE_SIDE_EFFECTS so that expand_expr does not
328 ignore the TARGET_EXPR. If there really turn out to be no
329 side-effects, then the optimizer should be able to get rid of
330 whatever code is generated anyhow. */
331 TREE_SIDE_EFFECTS (t) = 1;
333 return t;
336 /* Return an undeclared local temporary of type TYPE for use in building a
337 TARGET_EXPR. */
339 static tree
340 build_local_temp (tree type)
342 tree slot = build_decl (input_location,
343 VAR_DECL, NULL_TREE, type);
344 DECL_ARTIFICIAL (slot) = 1;
345 DECL_IGNORED_P (slot) = 1;
346 DECL_CONTEXT (slot) = current_function_decl;
347 layout_decl (slot, 0);
348 return slot;
351 /* Set various status flags when building an AGGR_INIT_EXPR object T. */
353 static void
354 process_aggr_init_operands (tree t)
356 bool side_effects;
358 side_effects = TREE_SIDE_EFFECTS (t);
359 if (!side_effects)
361 int i, n;
362 n = TREE_OPERAND_LENGTH (t);
363 for (i = 1; i < n; i++)
365 tree op = TREE_OPERAND (t, i);
366 if (op && TREE_SIDE_EFFECTS (op))
368 side_effects = 1;
369 break;
373 TREE_SIDE_EFFECTS (t) = side_effects;
376 /* Build an AGGR_INIT_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE,
377 FN, and SLOT. NARGS is the number of call arguments which are specified
378 as a tree array ARGS. */
380 static tree
381 build_aggr_init_array (tree return_type, tree fn, tree slot, int nargs,
382 tree *args)
384 tree t;
385 int i;
387 t = build_vl_exp (AGGR_INIT_EXPR, nargs + 3);
388 TREE_TYPE (t) = return_type;
389 AGGR_INIT_EXPR_FN (t) = fn;
390 AGGR_INIT_EXPR_SLOT (t) = slot;
391 for (i = 0; i < nargs; i++)
392 AGGR_INIT_EXPR_ARG (t, i) = args[i];
393 process_aggr_init_operands (t);
394 return t;
397 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
398 target. TYPE is the type to be initialized.
400 Build an AGGR_INIT_EXPR to represent the initialization. This function
401 differs from build_cplus_new in that an AGGR_INIT_EXPR can only be used
402 to initialize another object, whereas a TARGET_EXPR can either
403 initialize another object or create its own temporary object, and as a
404 result building up a TARGET_EXPR requires that the type's destructor be
405 callable. */
407 tree
408 build_aggr_init_expr (tree type, tree init)
410 tree fn;
411 tree slot;
412 tree rval;
413 int is_ctor;
415 if (TREE_CODE (init) == CALL_EXPR)
416 fn = CALL_EXPR_FN (init);
417 else if (TREE_CODE (init) == AGGR_INIT_EXPR)
418 fn = AGGR_INIT_EXPR_FN (init);
419 else
420 return convert (type, init);
422 is_ctor = (TREE_CODE (fn) == ADDR_EXPR
423 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
424 && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0)));
426 /* We split the CALL_EXPR into its function and its arguments here.
427 Then, in expand_expr, we put them back together. The reason for
428 this is that this expression might be a default argument
429 expression. In that case, we need a new temporary every time the
430 expression is used. That's what break_out_target_exprs does; it
431 replaces every AGGR_INIT_EXPR with a copy that uses a fresh
432 temporary slot. Then, expand_expr builds up a call-expression
433 using the new slot. */
435 /* If we don't need to use a constructor to create an object of this
436 type, don't mess with AGGR_INIT_EXPR. */
437 if (is_ctor || TREE_ADDRESSABLE (type))
439 slot = build_local_temp (type);
441 if (TREE_CODE(init) == CALL_EXPR)
442 rval = build_aggr_init_array (void_type_node, fn, slot,
443 call_expr_nargs (init),
444 CALL_EXPR_ARGP (init));
445 else
446 rval = build_aggr_init_array (void_type_node, fn, slot,
447 aggr_init_expr_nargs (init),
448 AGGR_INIT_EXPR_ARGP (init));
449 TREE_SIDE_EFFECTS (rval) = 1;
450 AGGR_INIT_VIA_CTOR_P (rval) = is_ctor;
451 TREE_NOTHROW (rval) = TREE_NOTHROW (init);
453 else
454 rval = init;
456 return rval;
459 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
460 target. TYPE is the type that this initialization should appear to
461 have.
463 Build an encapsulation of the initialization to perform
464 and return it so that it can be processed by language-independent
465 and language-specific expression expanders. */
467 tree
468 build_cplus_new (tree type, tree init, tsubst_flags_t complain)
470 tree rval = build_aggr_init_expr (type, init);
471 tree slot;
473 /* Make sure that we're not trying to create an instance of an
474 abstract class. */
475 if (abstract_virtuals_error_sfinae (NULL_TREE, type, complain))
476 return error_mark_node;
478 if (TREE_CODE (rval) == AGGR_INIT_EXPR)
479 slot = AGGR_INIT_EXPR_SLOT (rval);
480 else if (TREE_CODE (rval) == CALL_EXPR
481 || TREE_CODE (rval) == CONSTRUCTOR)
482 slot = build_local_temp (type);
483 else
484 return rval;
486 rval = build_target_expr (slot, rval, complain);
488 if (rval != error_mark_node)
489 TARGET_EXPR_IMPLICIT_P (rval) = 1;
491 return rval;
494 /* Subroutine of build_vec_init_expr: Build up a single element
495 intialization as a proxy for the full array initialization to get things
496 marked as used and any appropriate diagnostics.
498 Since we're deferring building the actual constructor calls until
499 gimplification time, we need to build one now and throw it away so
500 that the relevant constructor gets mark_used before cgraph decides
501 what functions are needed. Here we assume that init is either
502 NULL_TREE, void_type_node (indicating value-initialization), or
503 another array to copy. */
505 static tree
506 build_vec_init_elt (tree type, tree init, tsubst_flags_t complain)
508 tree inner_type = strip_array_types (type);
509 vec<tree, va_gc> *argvec;
511 if (integer_zerop (array_type_nelts_total (type))
512 || !CLASS_TYPE_P (inner_type))
513 /* No interesting initialization to do. */
514 return integer_zero_node;
515 else if (init == void_type_node)
516 return build_value_init (inner_type, complain);
518 gcc_assert (init == NULL_TREE
519 || (same_type_ignoring_top_level_qualifiers_p
520 (type, TREE_TYPE (init))));
522 argvec = make_tree_vector ();
523 if (init)
525 tree init_type = strip_array_types (TREE_TYPE (init));
526 tree dummy = build_dummy_object (init_type);
527 if (!real_lvalue_p (init))
528 dummy = move (dummy);
529 argvec->quick_push (dummy);
531 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
532 &argvec, inner_type, LOOKUP_NORMAL,
533 complain);
534 release_tree_vector (argvec);
536 /* For a trivial constructor, build_over_call creates a TARGET_EXPR. But
537 we don't want one here because we aren't creating a temporary. */
538 if (TREE_CODE (init) == TARGET_EXPR)
539 init = TARGET_EXPR_INITIAL (init);
541 return init;
544 /* Return a TARGET_EXPR which expresses the initialization of an array to
545 be named later, either default-initialization or copy-initialization
546 from another array of the same type. */
548 tree
549 build_vec_init_expr (tree type, tree init, tsubst_flags_t complain)
551 tree slot;
552 bool value_init = false;
553 tree elt_init = build_vec_init_elt (type, init, complain);
555 if (init == void_type_node)
557 value_init = true;
558 init = NULL_TREE;
561 slot = build_local_temp (type);
562 init = build2 (VEC_INIT_EXPR, type, slot, init);
563 TREE_SIDE_EFFECTS (init) = true;
564 SET_EXPR_LOCATION (init, input_location);
566 if (cxx_dialect >= cxx0x
567 && potential_constant_expression (elt_init))
568 VEC_INIT_EXPR_IS_CONSTEXPR (init) = true;
569 VEC_INIT_EXPR_VALUE_INIT (init) = value_init;
571 return init;
574 /* Give a helpful diagnostic for a non-constexpr VEC_INIT_EXPR in a context
575 that requires a constant expression. */
577 void
578 diagnose_non_constexpr_vec_init (tree expr)
580 tree type = TREE_TYPE (VEC_INIT_EXPR_SLOT (expr));
581 tree init, elt_init;
582 if (VEC_INIT_EXPR_VALUE_INIT (expr))
583 init = void_type_node;
584 else
585 init = VEC_INIT_EXPR_INIT (expr);
587 elt_init = build_vec_init_elt (type, init, tf_warning_or_error);
588 require_potential_constant_expression (elt_init);
591 tree
592 build_array_copy (tree init)
594 return build_vec_init_expr (TREE_TYPE (init), init, tf_warning_or_error);
597 /* Build a TARGET_EXPR using INIT to initialize a new temporary of the
598 indicated TYPE. */
600 tree
601 build_target_expr_with_type (tree init, tree type, tsubst_flags_t complain)
603 gcc_assert (!VOID_TYPE_P (type));
605 if (TREE_CODE (init) == TARGET_EXPR
606 || init == error_mark_node)
607 return init;
608 else if (CLASS_TYPE_P (type) && type_has_nontrivial_copy_init (type)
609 && !VOID_TYPE_P (TREE_TYPE (init))
610 && TREE_CODE (init) != COND_EXPR
611 && TREE_CODE (init) != CONSTRUCTOR
612 && TREE_CODE (init) != VA_ARG_EXPR)
613 /* We need to build up a copy constructor call. A void initializer
614 means we're being called from bot_manip. COND_EXPR is a special
615 case because we already have copies on the arms and we don't want
616 another one here. A CONSTRUCTOR is aggregate initialization, which
617 is handled separately. A VA_ARG_EXPR is magic creation of an
618 aggregate; there's no additional work to be done. */
619 return force_rvalue (init, complain);
621 return force_target_expr (type, init, complain);
624 /* Like the above function, but without the checking. This function should
625 only be used by code which is deliberately trying to subvert the type
626 system, such as call_builtin_trap. Or build_over_call, to avoid
627 infinite recursion. */
629 tree
630 force_target_expr (tree type, tree init, tsubst_flags_t complain)
632 tree slot;
634 gcc_assert (!VOID_TYPE_P (type));
636 slot = build_local_temp (type);
637 return build_target_expr (slot, init, complain);
640 /* Like build_target_expr_with_type, but use the type of INIT. */
642 tree
643 get_target_expr_sfinae (tree init, tsubst_flags_t complain)
645 if (TREE_CODE (init) == AGGR_INIT_EXPR)
646 return build_target_expr (AGGR_INIT_EXPR_SLOT (init), init, complain);
647 else if (TREE_CODE (init) == VEC_INIT_EXPR)
648 return build_target_expr (VEC_INIT_EXPR_SLOT (init), init, complain);
649 else
650 return build_target_expr_with_type (init, TREE_TYPE (init), complain);
653 tree
654 get_target_expr (tree init)
656 return get_target_expr_sfinae (init, tf_warning_or_error);
659 /* If EXPR is a bitfield reference, convert it to the declared type of
660 the bitfield, and return the resulting expression. Otherwise,
661 return EXPR itself. */
663 tree
664 convert_bitfield_to_declared_type (tree expr)
666 tree bitfield_type;
668 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
669 if (bitfield_type)
670 expr = convert_to_integer (TYPE_MAIN_VARIANT (bitfield_type),
671 expr);
672 return expr;
675 /* EXPR is being used in an rvalue context. Return a version of EXPR
676 that is marked as an rvalue. */
678 tree
679 rvalue (tree expr)
681 tree type;
683 if (error_operand_p (expr))
684 return expr;
686 expr = mark_rvalue_use (expr);
688 /* [basic.lval]
690 Non-class rvalues always have cv-unqualified types. */
691 type = TREE_TYPE (expr);
692 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
693 type = cv_unqualified (type);
695 /* We need to do this for rvalue refs as well to get the right answer
696 from decltype; see c++/36628. */
697 if (!processing_template_decl && lvalue_or_rvalue_with_address_p (expr))
698 expr = build1 (NON_LVALUE_EXPR, type, expr);
699 else if (type != TREE_TYPE (expr))
700 expr = build_nop (type, expr);
702 return expr;
706 /* Hash an ARRAY_TYPE. K is really of type `tree'. */
708 static hashval_t
709 cplus_array_hash (const void* k)
711 hashval_t hash;
712 const_tree const t = (const_tree) k;
714 hash = TYPE_UID (TREE_TYPE (t));
715 if (TYPE_DOMAIN (t))
716 hash ^= TYPE_UID (TYPE_DOMAIN (t));
717 return hash;
720 typedef struct cplus_array_info {
721 tree type;
722 tree domain;
723 } cplus_array_info;
725 /* Compare two ARRAY_TYPEs. K1 is really of type `tree', K2 is really
726 of type `cplus_array_info*'. */
728 static int
729 cplus_array_compare (const void * k1, const void * k2)
731 const_tree const t1 = (const_tree) k1;
732 const cplus_array_info *const t2 = (const cplus_array_info*) k2;
734 return (TREE_TYPE (t1) == t2->type && TYPE_DOMAIN (t1) == t2->domain);
737 /* Hash table containing dependent array types, which are unsuitable for
738 the language-independent type hash table. */
739 static GTY ((param_is (union tree_node))) htab_t cplus_array_htab;
741 /* Like build_array_type, but handle special C++ semantics. */
743 tree
744 build_cplus_array_type (tree elt_type, tree index_type)
746 tree t;
748 if (elt_type == error_mark_node || index_type == error_mark_node)
749 return error_mark_node;
751 if (processing_template_decl
752 && (dependent_type_p (elt_type)
753 || (index_type && !TREE_CONSTANT (TYPE_MAX_VALUE (index_type)))))
755 void **e;
756 cplus_array_info cai;
757 hashval_t hash;
759 if (cplus_array_htab == NULL)
760 cplus_array_htab = htab_create_ggc (61, &cplus_array_hash,
761 &cplus_array_compare, NULL);
763 hash = TYPE_UID (elt_type);
764 if (index_type)
765 hash ^= TYPE_UID (index_type);
766 cai.type = elt_type;
767 cai.domain = index_type;
769 e = htab_find_slot_with_hash (cplus_array_htab, &cai, hash, INSERT);
770 if (*e)
771 /* We have found the type: we're done. */
772 return (tree) *e;
773 else
775 /* Build a new array type. */
776 t = cxx_make_type (ARRAY_TYPE);
777 TREE_TYPE (t) = elt_type;
778 TYPE_DOMAIN (t) = index_type;
780 /* Store it in the hash table. */
781 *e = t;
783 /* Set the canonical type for this new node. */
784 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
785 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
786 SET_TYPE_STRUCTURAL_EQUALITY (t);
787 else if (TYPE_CANONICAL (elt_type) != elt_type
788 || (index_type
789 && TYPE_CANONICAL (index_type) != index_type))
790 TYPE_CANONICAL (t)
791 = build_cplus_array_type
792 (TYPE_CANONICAL (elt_type),
793 index_type ? TYPE_CANONICAL (index_type) : index_type);
794 else
795 TYPE_CANONICAL (t) = t;
798 else
800 if (!TYPE_STRUCTURAL_EQUALITY_P (elt_type)
801 && !(index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type))
802 && (TYPE_CANONICAL (elt_type) != elt_type
803 || (index_type && TYPE_CANONICAL (index_type) != index_type)))
804 /* Make sure that the canonical type is on the appropriate
805 variants list. */
806 build_cplus_array_type
807 (TYPE_CANONICAL (elt_type),
808 index_type ? TYPE_CANONICAL (index_type) : index_type);
809 t = build_array_type (elt_type, index_type);
812 /* We want TYPE_MAIN_VARIANT of an array to strip cv-quals from the
813 element type as well, so fix it up if needed. */
814 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
816 tree m = build_cplus_array_type (TYPE_MAIN_VARIANT (elt_type),
817 index_type);
819 if (TYPE_MAIN_VARIANT (t) != m)
821 TYPE_MAIN_VARIANT (t) = m;
822 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
823 TYPE_NEXT_VARIANT (m) = t;
827 /* Avoid spurious warnings with VLAs (c++/54583). */
828 if (TYPE_SIZE (t) && EXPR_P (TYPE_SIZE (t)))
829 TREE_NO_WARNING (TYPE_SIZE (t)) = 1;
831 /* Push these needs up so that initialization takes place
832 more easily. */
833 TYPE_NEEDS_CONSTRUCTING (t)
834 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (elt_type));
835 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
836 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (elt_type));
837 return t;
840 /* Return an ARRAY_TYPE with element type ELT and length N. */
842 tree
843 build_array_of_n_type (tree elt, int n)
845 return build_cplus_array_type (elt, build_index_type (size_int (n - 1)));
848 /* Return a reference type node referring to TO_TYPE. If RVAL is
849 true, return an rvalue reference type, otherwise return an lvalue
850 reference type. If a type node exists, reuse it, otherwise create
851 a new one. */
852 tree
853 cp_build_reference_type (tree to_type, bool rval)
855 tree lvalue_ref, t;
856 lvalue_ref = build_reference_type (to_type);
857 if (!rval)
858 return lvalue_ref;
860 /* This code to create rvalue reference types is based on and tied
861 to the code creating lvalue reference types in the middle-end
862 functions build_reference_type_for_mode and build_reference_type.
864 It works by putting the rvalue reference type nodes after the
865 lvalue reference nodes in the TYPE_NEXT_REF_TO linked list, so
866 they will effectively be ignored by the middle end. */
868 for (t = lvalue_ref; (t = TYPE_NEXT_REF_TO (t)); )
869 if (TYPE_REF_IS_RVALUE (t))
870 return t;
872 t = build_distinct_type_copy (lvalue_ref);
874 TYPE_REF_IS_RVALUE (t) = true;
875 TYPE_NEXT_REF_TO (t) = TYPE_NEXT_REF_TO (lvalue_ref);
876 TYPE_NEXT_REF_TO (lvalue_ref) = t;
878 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
879 SET_TYPE_STRUCTURAL_EQUALITY (t);
880 else if (TYPE_CANONICAL (to_type) != to_type)
881 TYPE_CANONICAL (t)
882 = cp_build_reference_type (TYPE_CANONICAL (to_type), rval);
883 else
884 TYPE_CANONICAL (t) = t;
886 layout_type (t);
888 return t;
892 /* Returns EXPR cast to rvalue reference type, like std::move. */
894 tree
895 move (tree expr)
897 tree type = TREE_TYPE (expr);
898 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
899 type = cp_build_reference_type (type, /*rval*/true);
900 return build_static_cast (type, expr, tf_warning_or_error);
903 /* Used by the C++ front end to build qualified array types. However,
904 the C version of this function does not properly maintain canonical
905 types (which are not used in C). */
906 tree
907 c_build_qualified_type_1 (tree type, int type_quals, tree ARG_UNUSED(layout_qualiefier))
909 return cp_build_qualified_type (type, type_quals);
913 /* Make a variant of TYPE, qualified with the TYPE_QUALS. Handles
914 arrays correctly. In particular, if TYPE is an array of T's, and
915 TYPE_QUALS is non-empty, returns an array of qualified T's.
917 FLAGS determines how to deal with ill-formed qualifications. If
918 tf_ignore_bad_quals is set, then bad qualifications are dropped
919 (this is permitted if TYPE was introduced via a typedef or template
920 type parameter). If bad qualifications are dropped and tf_warning
921 is set, then a warning is issued for non-const qualifications. If
922 tf_ignore_bad_quals is not set and tf_error is not set, we
923 return error_mark_node. Otherwise, we issue an error, and ignore
924 the qualifications.
926 Qualification of a reference type is valid when the reference came
927 via a typedef or template type argument. [dcl.ref] No such
928 dispensation is provided for qualifying a function type. [dcl.fct]
929 DR 295 queries this and the proposed resolution brings it into line
930 with qualifying a reference. We implement the DR. We also behave
931 in a similar manner for restricting non-pointer types. */
933 tree
934 cp_build_qualified_type_real (tree type,
935 int type_quals,
936 tsubst_flags_t complain)
938 tree result;
939 int bad_quals = TYPE_UNQUALIFIED;
941 if (type == error_mark_node)
942 return type;
944 if (type_quals == cp_type_quals (type))
945 return type;
947 if (TREE_CODE (type) == ARRAY_TYPE)
949 /* In C++, the qualification really applies to the array element
950 type. Obtain the appropriately qualified element type. */
951 tree t;
952 tree element_type
953 = cp_build_qualified_type_real (TREE_TYPE (type),
954 type_quals,
955 complain);
957 if (element_type == error_mark_node)
958 return error_mark_node;
960 /* See if we already have an identically qualified type. Tests
961 should be equivalent to those in check_qualified_type. */
962 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
963 if (TREE_TYPE (t) == element_type
964 && TYPE_NAME (t) == TYPE_NAME (type)
965 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
966 && attribute_list_equal (TYPE_ATTRIBUTES (t),
967 TYPE_ATTRIBUTES (type)))
968 break;
970 if (!t)
972 t = build_cplus_array_type (element_type, TYPE_DOMAIN (type));
974 /* Keep the typedef name. */
975 if (TYPE_NAME (t) != TYPE_NAME (type))
977 t = build_variant_type_copy (t);
978 TYPE_NAME (t) = TYPE_NAME (type);
982 /* Even if we already had this variant, we update
983 TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case
984 they changed since the variant was originally created.
986 This seems hokey; if there is some way to use a previous
987 variant *without* coming through here,
988 TYPE_NEEDS_CONSTRUCTING will never be updated. */
989 TYPE_NEEDS_CONSTRUCTING (t)
990 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
991 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
992 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
993 return t;
995 else if (TYPE_PTRMEMFUNC_P (type))
997 /* For a pointer-to-member type, we can't just return a
998 cv-qualified version of the RECORD_TYPE. If we do, we
999 haven't changed the field that contains the actual pointer to
1000 a method, and so TYPE_PTRMEMFUNC_FN_TYPE will be wrong. */
1001 tree t;
1003 t = TYPE_PTRMEMFUNC_FN_TYPE (type);
1004 t = cp_build_qualified_type_real (t, type_quals, complain);
1005 return build_ptrmemfunc_type (t);
1007 else if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
1009 tree t = PACK_EXPANSION_PATTERN (type);
1011 t = cp_build_qualified_type_real (t, type_quals, complain);
1012 return make_pack_expansion (t);
1015 /* A reference or method type shall not be cv-qualified.
1016 [dcl.ref], [dcl.fct]. This used to be an error, but as of DR 295
1017 (in CD1) we always ignore extra cv-quals on functions. */
1018 if (type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)
1019 && (TREE_CODE (type) == REFERENCE_TYPE
1020 || TREE_CODE (type) == FUNCTION_TYPE
1021 || TREE_CODE (type) == METHOD_TYPE))
1023 if (TREE_CODE (type) == REFERENCE_TYPE)
1024 bad_quals |= type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1025 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1028 /* But preserve any function-cv-quals on a FUNCTION_TYPE. */
1029 if (TREE_CODE (type) == FUNCTION_TYPE)
1030 type_quals |= type_memfn_quals (type);
1032 /* A restrict-qualified type must be a pointer (or reference)
1033 to object or incomplete type. */
1034 if ((type_quals & TYPE_QUAL_RESTRICT)
1035 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1036 && TREE_CODE (type) != TYPENAME_TYPE
1037 && !POINTER_TYPE_P (type))
1039 bad_quals |= TYPE_QUAL_RESTRICT;
1040 type_quals &= ~TYPE_QUAL_RESTRICT;
1043 if (bad_quals == TYPE_UNQUALIFIED
1044 || (complain & tf_ignore_bad_quals))
1045 /*OK*/;
1046 else if (!(complain & tf_error))
1047 return error_mark_node;
1048 else
1050 tree bad_type = build_qualified_type (ptr_type_node, bad_quals);
1051 error ("%qV qualifiers cannot be applied to %qT",
1052 bad_type, type);
1055 /* Retrieve (or create) the appropriately qualified variant. */
1056 result = build_qualified_type (type, type_quals);
1058 /* If this was a pointer-to-method type, and we just made a copy,
1059 then we need to unshare the record that holds the cached
1060 pointer-to-member-function type, because these will be distinct
1061 between the unqualified and qualified types. */
1062 if (result != type
1063 && TREE_CODE (type) == POINTER_TYPE
1064 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE
1065 && TYPE_LANG_SPECIFIC (result) == TYPE_LANG_SPECIFIC (type))
1066 TYPE_LANG_SPECIFIC (result) = NULL;
1068 /* We may also have ended up building a new copy of the canonical
1069 type of a pointer-to-method type, which could have the same
1070 sharing problem described above. */
1071 if (TYPE_CANONICAL (result) != TYPE_CANONICAL (type)
1072 && TREE_CODE (type) == POINTER_TYPE
1073 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE
1074 && (TYPE_LANG_SPECIFIC (TYPE_CANONICAL (result))
1075 == TYPE_LANG_SPECIFIC (TYPE_CANONICAL (type))))
1076 TYPE_LANG_SPECIFIC (TYPE_CANONICAL (result)) = NULL;
1078 return result;
1081 /* Return TYPE with const and volatile removed. */
1083 tree
1084 cv_unqualified (tree type)
1086 int quals;
1088 if (type == error_mark_node)
1089 return type;
1091 quals = cp_type_quals (type);
1092 quals &= ~(TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE);
1093 return cp_build_qualified_type (type, quals);
1096 /* Builds a qualified variant of T that is not a typedef variant.
1097 E.g. consider the following declarations:
1098 typedef const int ConstInt;
1099 typedef ConstInt* PtrConstInt;
1100 If T is PtrConstInt, this function returns a type representing
1101 const int*.
1102 In other words, if T is a typedef, the function returns the underlying type.
1103 The cv-qualification and attributes of the type returned match the
1104 input type.
1105 They will always be compatible types.
1106 The returned type is built so that all of its subtypes
1107 recursively have their typedefs stripped as well.
1109 This is different from just returning TYPE_CANONICAL (T)
1110 Because of several reasons:
1111 * If T is a type that needs structural equality
1112 its TYPE_CANONICAL (T) will be NULL.
1113 * TYPE_CANONICAL (T) desn't carry type attributes
1114 and loses template parameter names. */
1116 tree
1117 strip_typedefs (tree t)
1119 tree result = NULL, type = NULL, t0 = NULL;
1121 if (!t || t == error_mark_node || t == TYPE_CANONICAL (t))
1122 return t;
1124 gcc_assert (TYPE_P (t));
1126 switch (TREE_CODE (t))
1128 case POINTER_TYPE:
1129 type = strip_typedefs (TREE_TYPE (t));
1130 result = build_pointer_type (type);
1131 break;
1132 case REFERENCE_TYPE:
1133 type = strip_typedefs (TREE_TYPE (t));
1134 result = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
1135 break;
1136 case OFFSET_TYPE:
1137 t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t));
1138 type = strip_typedefs (TREE_TYPE (t));
1139 result = build_offset_type (t0, type);
1140 break;
1141 case RECORD_TYPE:
1142 if (TYPE_PTRMEMFUNC_P (t))
1144 t0 = strip_typedefs (TYPE_PTRMEMFUNC_FN_TYPE (t));
1145 result = build_ptrmemfunc_type (t0);
1147 break;
1148 case ARRAY_TYPE:
1149 type = strip_typedefs (TREE_TYPE (t));
1150 t0 = strip_typedefs (TYPE_DOMAIN (t));;
1151 result = build_cplus_array_type (type, t0);
1152 break;
1153 case FUNCTION_TYPE:
1154 case METHOD_TYPE:
1156 tree arg_types = NULL, arg_node, arg_type;
1157 for (arg_node = TYPE_ARG_TYPES (t);
1158 arg_node;
1159 arg_node = TREE_CHAIN (arg_node))
1161 if (arg_node == void_list_node)
1162 break;
1163 arg_type = strip_typedefs (TREE_VALUE (arg_node));
1164 gcc_assert (arg_type);
1166 arg_types =
1167 tree_cons (TREE_PURPOSE (arg_node), arg_type, arg_types);
1170 if (arg_types)
1171 arg_types = nreverse (arg_types);
1173 /* A list of parameters not ending with an ellipsis
1174 must end with void_list_node. */
1175 if (arg_node)
1176 arg_types = chainon (arg_types, void_list_node);
1178 type = strip_typedefs (TREE_TYPE (t));
1179 if (TREE_CODE (t) == METHOD_TYPE)
1181 tree class_type = TREE_TYPE (TREE_VALUE (arg_types));
1182 gcc_assert (class_type);
1183 result =
1184 build_method_type_directly (class_type, type,
1185 TREE_CHAIN (arg_types));
1187 else
1189 result = build_function_type (type,
1190 arg_types);
1191 result = apply_memfn_quals (result, type_memfn_quals (t));
1194 if (TYPE_RAISES_EXCEPTIONS (t))
1195 result = build_exception_variant (result,
1196 TYPE_RAISES_EXCEPTIONS (t));
1198 break;
1199 case TYPENAME_TYPE:
1200 result = make_typename_type (strip_typedefs (TYPE_CONTEXT (t)),
1201 TYPENAME_TYPE_FULLNAME (t),
1202 typename_type, tf_none);
1203 break;
1204 case DECLTYPE_TYPE:
1205 result = strip_typedefs_expr (DECLTYPE_TYPE_EXPR (t));
1206 if (result == DECLTYPE_TYPE_EXPR (t))
1207 return t;
1208 else
1209 result = (finish_decltype_type
1210 (result,
1211 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t),
1212 tf_none));
1213 break;
1214 default:
1215 break;
1218 if (!result)
1219 result = TYPE_MAIN_VARIANT (t);
1220 if (TYPE_USER_ALIGN (t) != TYPE_USER_ALIGN (result)
1221 || TYPE_ALIGN (t) != TYPE_ALIGN (result))
1223 gcc_assert (TYPE_USER_ALIGN (t));
1224 if (TYPE_ALIGN (t) == TYPE_ALIGN (result))
1225 result = build_variant_type_copy (result);
1226 else
1227 result = build_aligned_type (result, TYPE_ALIGN (t));
1228 TYPE_USER_ALIGN (result) = true;
1230 if (TYPE_ATTRIBUTES (t))
1231 result = cp_build_type_attribute_variant (result, TYPE_ATTRIBUTES (t));
1232 return cp_build_qualified_type (result, cp_type_quals (t));
1235 /* Like strip_typedefs above, but works on expressions, so that in
1237 template<class T> struct A
1239 typedef T TT;
1240 B<sizeof(TT)> b;
1243 sizeof(TT) is replaced by sizeof(T). */
1245 tree
1246 strip_typedefs_expr (tree t)
1248 unsigned i,n;
1249 tree r, type, *ops;
1250 enum tree_code code;
1252 if (t == NULL_TREE || t == error_mark_node)
1253 return t;
1255 if (DECL_P (t) || CONSTANT_CLASS_P (t))
1256 return t;
1258 /* Some expressions have type operands, so let's handle types here rather
1259 than check TYPE_P in multiple places below. */
1260 if (TYPE_P (t))
1261 return strip_typedefs (t);
1263 code = TREE_CODE (t);
1264 switch (code)
1266 case IDENTIFIER_NODE:
1267 case TEMPLATE_PARM_INDEX:
1268 case OVERLOAD:
1269 case BASELINK:
1270 case ARGUMENT_PACK_SELECT:
1271 return t;
1273 case TRAIT_EXPR:
1275 tree type1 = strip_typedefs (TRAIT_EXPR_TYPE1 (t));
1276 tree type2 = strip_typedefs (TRAIT_EXPR_TYPE2 (t));
1277 if (type1 == TRAIT_EXPR_TYPE1 (t)
1278 && type2 == TRAIT_EXPR_TYPE2 (t))
1279 return t;
1280 r = copy_node (t);
1281 TRAIT_EXPR_TYPE1 (t) = type1;
1282 TRAIT_EXPR_TYPE2 (t) = type2;
1283 return r;
1286 case TREE_LIST:
1288 vec<tree, va_gc> *vec = make_tree_vector ();
1289 bool changed = false;
1290 tree it;
1291 for (it = t; it; it = TREE_CHAIN (it))
1293 tree val = strip_typedefs_expr (TREE_VALUE (t));
1294 vec_safe_push (vec, val);
1295 if (val != TREE_VALUE (t))
1296 changed = true;
1297 gcc_assert (TREE_PURPOSE (it) == NULL_TREE);
1299 if (changed)
1301 r = NULL_TREE;
1302 FOR_EACH_VEC_ELT_REVERSE (*vec, i, it)
1303 r = tree_cons (NULL_TREE, it, r);
1305 else
1306 r = t;
1307 release_tree_vector (vec);
1308 return r;
1311 case TREE_VEC:
1313 bool changed = false;
1314 vec<tree, va_gc> *vec = make_tree_vector ();
1315 n = TREE_VEC_LENGTH (t);
1316 vec_safe_reserve (vec, n);
1317 for (i = 0; i < n; ++i)
1319 tree op = strip_typedefs_expr (TREE_VEC_ELT (t, i));
1320 vec->quick_push (op);
1321 if (op != TREE_VEC_ELT (t, i))
1322 changed = true;
1324 if (changed)
1326 r = copy_node (t);
1327 for (i = 0; i < n; ++i)
1328 TREE_VEC_ELT (r, i) = (*vec)[i];
1330 else
1331 r = t;
1332 release_tree_vector (vec);
1333 return r;
1336 case CONSTRUCTOR:
1338 bool changed = false;
1339 vec<constructor_elt, va_gc> *vec
1340 = vec_safe_copy (CONSTRUCTOR_ELTS (t));
1341 n = CONSTRUCTOR_NELTS (t);
1342 type = strip_typedefs (TREE_TYPE (t));
1343 for (i = 0; i < n; ++i)
1345 constructor_elt *e = &(*vec)[i];
1346 tree op = strip_typedefs_expr (e->value);
1347 if (op != e->value)
1349 changed = true;
1350 e->value = op;
1352 gcc_checking_assert (e->index == strip_typedefs_expr (e->index));
1355 if (!changed && type == TREE_TYPE (t))
1357 vec_free (vec);
1358 return t;
1360 else
1362 r = copy_node (t);
1363 TREE_TYPE (r) = type;
1364 CONSTRUCTOR_ELTS (r) = vec;
1365 return r;
1369 case LAMBDA_EXPR:
1370 gcc_unreachable ();
1372 default:
1373 break;
1376 gcc_assert (EXPR_P (t));
1378 n = TREE_OPERAND_LENGTH (t);
1379 ops = XALLOCAVEC (tree, n);
1380 type = TREE_TYPE (t);
1382 switch (code)
1384 CASE_CONVERT:
1385 case IMPLICIT_CONV_EXPR:
1386 case DYNAMIC_CAST_EXPR:
1387 case STATIC_CAST_EXPR:
1388 case CONST_CAST_EXPR:
1389 case REINTERPRET_CAST_EXPR:
1390 case CAST_EXPR:
1391 case NEW_EXPR:
1392 type = strip_typedefs (type);
1393 /* fallthrough */
1395 default:
1396 for (i = 0; i < n; ++i)
1397 ops[i] = strip_typedefs_expr (TREE_OPERAND (t, i));
1398 break;
1401 /* If nothing changed, return t. */
1402 for (i = 0; i < n; ++i)
1403 if (ops[i] != TREE_OPERAND (t, i))
1404 break;
1405 if (i == n && type == TREE_TYPE (t))
1406 return t;
1408 r = copy_node (t);
1409 TREE_TYPE (r) = type;
1410 for (i = 0; i < n; ++i)
1411 TREE_OPERAND (r, i) = ops[i];
1412 return r;
1415 /* Makes a copy of BINFO and TYPE, which is to be inherited into a
1416 graph dominated by T. If BINFO is NULL, TYPE is a dependent base,
1417 and we do a shallow copy. If BINFO is non-NULL, we do a deep copy.
1418 VIRT indicates whether TYPE is inherited virtually or not.
1419 IGO_PREV points at the previous binfo of the inheritance graph
1420 order chain. The newly copied binfo's TREE_CHAIN forms this
1421 ordering.
1423 The CLASSTYPE_VBASECLASSES vector of T is constructed in the
1424 correct order. That is in the order the bases themselves should be
1425 constructed in.
1427 The BINFO_INHERITANCE of a virtual base class points to the binfo
1428 of the most derived type. ??? We could probably change this so that
1429 BINFO_INHERITANCE becomes synonymous with BINFO_PRIMARY, and hence
1430 remove a field. They currently can only differ for primary virtual
1431 virtual bases. */
1433 tree
1434 copy_binfo (tree binfo, tree type, tree t, tree *igo_prev, int virt)
1436 tree new_binfo;
1438 if (virt)
1440 /* See if we've already made this virtual base. */
1441 new_binfo = binfo_for_vbase (type, t);
1442 if (new_binfo)
1443 return new_binfo;
1446 new_binfo = make_tree_binfo (binfo ? BINFO_N_BASE_BINFOS (binfo) : 0);
1447 BINFO_TYPE (new_binfo) = type;
1449 /* Chain it into the inheritance graph. */
1450 TREE_CHAIN (*igo_prev) = new_binfo;
1451 *igo_prev = new_binfo;
1453 if (binfo && !BINFO_DEPENDENT_BASE_P (binfo))
1455 int ix;
1456 tree base_binfo;
1458 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), type));
1460 BINFO_OFFSET (new_binfo) = BINFO_OFFSET (binfo);
1461 BINFO_VIRTUALS (new_binfo) = BINFO_VIRTUALS (binfo);
1463 /* We do not need to copy the accesses, as they are read only. */
1464 BINFO_BASE_ACCESSES (new_binfo) = BINFO_BASE_ACCESSES (binfo);
1466 /* Recursively copy base binfos of BINFO. */
1467 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1469 tree new_base_binfo;
1470 new_base_binfo = copy_binfo (base_binfo, BINFO_TYPE (base_binfo),
1471 t, igo_prev,
1472 BINFO_VIRTUAL_P (base_binfo));
1474 if (!BINFO_INHERITANCE_CHAIN (new_base_binfo))
1475 BINFO_INHERITANCE_CHAIN (new_base_binfo) = new_binfo;
1476 BINFO_BASE_APPEND (new_binfo, new_base_binfo);
1479 else
1480 BINFO_DEPENDENT_BASE_P (new_binfo) = 1;
1482 if (virt)
1484 /* Push it onto the list after any virtual bases it contains
1485 will have been pushed. */
1486 CLASSTYPE_VBASECLASSES (t)->quick_push (new_binfo);
1487 BINFO_VIRTUAL_P (new_binfo) = 1;
1488 BINFO_INHERITANCE_CHAIN (new_binfo) = TYPE_BINFO (t);
1491 return new_binfo;
1494 /* Hashing of lists so that we don't make duplicates.
1495 The entry point is `list_hash_canon'. */
1497 /* Now here is the hash table. When recording a list, it is added
1498 to the slot whose index is the hash code mod the table size.
1499 Note that the hash table is used for several kinds of lists.
1500 While all these live in the same table, they are completely independent,
1501 and the hash code is computed differently for each of these. */
1503 static GTY ((param_is (union tree_node))) htab_t list_hash_table;
1505 struct list_proxy
1507 tree purpose;
1508 tree value;
1509 tree chain;
1512 /* Compare ENTRY (an entry in the hash table) with DATA (a list_proxy
1513 for a node we are thinking about adding). */
1515 static int
1516 list_hash_eq (const void* entry, const void* data)
1518 const_tree const t = (const_tree) entry;
1519 const struct list_proxy *const proxy = (const struct list_proxy *) data;
1521 return (TREE_VALUE (t) == proxy->value
1522 && TREE_PURPOSE (t) == proxy->purpose
1523 && TREE_CHAIN (t) == proxy->chain);
1526 /* Compute a hash code for a list (chain of TREE_LIST nodes
1527 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
1528 TREE_COMMON slots), by adding the hash codes of the individual entries. */
1530 static hashval_t
1531 list_hash_pieces (tree purpose, tree value, tree chain)
1533 hashval_t hashcode = 0;
1535 if (chain)
1536 hashcode += TREE_HASH (chain);
1538 if (value)
1539 hashcode += TREE_HASH (value);
1540 else
1541 hashcode += 1007;
1542 if (purpose)
1543 hashcode += TREE_HASH (purpose);
1544 else
1545 hashcode += 1009;
1546 return hashcode;
1549 /* Hash an already existing TREE_LIST. */
1551 static hashval_t
1552 list_hash (const void* p)
1554 const_tree const t = (const_tree) p;
1555 return list_hash_pieces (TREE_PURPOSE (t),
1556 TREE_VALUE (t),
1557 TREE_CHAIN (t));
1560 /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
1561 object for an identical list if one already exists. Otherwise, build a
1562 new one, and record it as the canonical object. */
1564 tree
1565 hash_tree_cons (tree purpose, tree value, tree chain)
1567 int hashcode = 0;
1568 void **slot;
1569 struct list_proxy proxy;
1571 /* Hash the list node. */
1572 hashcode = list_hash_pieces (purpose, value, chain);
1573 /* Create a proxy for the TREE_LIST we would like to create. We
1574 don't actually create it so as to avoid creating garbage. */
1575 proxy.purpose = purpose;
1576 proxy.value = value;
1577 proxy.chain = chain;
1578 /* See if it is already in the table. */
1579 slot = htab_find_slot_with_hash (list_hash_table, &proxy, hashcode,
1580 INSERT);
1581 /* If not, create a new node. */
1582 if (!*slot)
1583 *slot = tree_cons (purpose, value, chain);
1584 return (tree) *slot;
1587 /* Constructor for hashed lists. */
1589 tree
1590 hash_tree_chain (tree value, tree chain)
1592 return hash_tree_cons (NULL_TREE, value, chain);
1595 void
1596 debug_binfo (tree elem)
1598 HOST_WIDE_INT n;
1599 tree virtuals;
1601 fprintf (stderr, "type \"%s\", offset = " HOST_WIDE_INT_PRINT_DEC
1602 "\nvtable type:\n",
1603 TYPE_NAME_STRING (BINFO_TYPE (elem)),
1604 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
1605 debug_tree (BINFO_TYPE (elem));
1606 if (BINFO_VTABLE (elem))
1607 fprintf (stderr, "vtable decl \"%s\"\n",
1608 IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem))));
1609 else
1610 fprintf (stderr, "no vtable decl yet\n");
1611 fprintf (stderr, "virtuals:\n");
1612 virtuals = BINFO_VIRTUALS (elem);
1613 n = 0;
1615 while (virtuals)
1617 tree fndecl = TREE_VALUE (virtuals);
1618 fprintf (stderr, "%s [%ld =? %ld]\n",
1619 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
1620 (long) n, (long) TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
1621 ++n;
1622 virtuals = TREE_CHAIN (virtuals);
1626 /* Build a representation for the qualified name SCOPE::NAME. TYPE is
1627 the type of the result expression, if known, or NULL_TREE if the
1628 resulting expression is type-dependent. If TEMPLATE_P is true,
1629 NAME is known to be a template because the user explicitly used the
1630 "template" keyword after the "::".
1632 All SCOPE_REFs should be built by use of this function. */
1634 tree
1635 build_qualified_name (tree type, tree scope, tree name, bool template_p)
1637 tree t;
1638 if (type == error_mark_node
1639 || scope == error_mark_node
1640 || name == error_mark_node)
1641 return error_mark_node;
1642 t = build2 (SCOPE_REF, type, scope, name);
1643 QUALIFIED_NAME_IS_TEMPLATE (t) = template_p;
1644 PTRMEM_OK_P (t) = true;
1645 if (type)
1646 t = convert_from_reference (t);
1647 return t;
1650 /* Returns nonzero if X is an expression for a (possibly overloaded)
1651 function. If "f" is a function or function template, "f", "c->f",
1652 "c.f", "C::f", and "f<int>" will all be considered possibly
1653 overloaded functions. Returns 2 if the function is actually
1654 overloaded, i.e., if it is impossible to know the type of the
1655 function without performing overload resolution. */
1658 is_overloaded_fn (tree x)
1660 /* A baselink is also considered an overloaded function. */
1661 if (TREE_CODE (x) == OFFSET_REF
1662 || TREE_CODE (x) == COMPONENT_REF)
1663 x = TREE_OPERAND (x, 1);
1664 if (BASELINK_P (x))
1665 x = BASELINK_FUNCTIONS (x);
1666 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
1667 x = TREE_OPERAND (x, 0);
1668 if (DECL_FUNCTION_TEMPLATE_P (OVL_CURRENT (x))
1669 || (TREE_CODE (x) == OVERLOAD && OVL_CHAIN (x)))
1670 return 2;
1671 return (TREE_CODE (x) == FUNCTION_DECL
1672 || TREE_CODE (x) == OVERLOAD);
1675 /* X is the CALL_EXPR_FN of a CALL_EXPR. If X represents a dependent name
1676 (14.6.2), return the IDENTIFIER_NODE for that name. Otherwise, return
1677 NULL_TREE. */
1679 tree
1680 dependent_name (tree x)
1682 if (TREE_CODE (x) == IDENTIFIER_NODE)
1683 return x;
1684 if (TREE_CODE (x) != COMPONENT_REF
1685 && TREE_CODE (x) != OFFSET_REF
1686 && TREE_CODE (x) != BASELINK
1687 && is_overloaded_fn (x))
1688 return DECL_NAME (get_first_fn (x));
1689 return NULL_TREE;
1692 /* Returns true iff X is an expression for an overloaded function
1693 whose type cannot be known without performing overload
1694 resolution. */
1696 bool
1697 really_overloaded_fn (tree x)
1699 return is_overloaded_fn (x) == 2;
1702 tree
1703 get_fns (tree from)
1705 gcc_assert (is_overloaded_fn (from));
1706 /* A baselink is also considered an overloaded function. */
1707 if (TREE_CODE (from) == OFFSET_REF
1708 || TREE_CODE (from) == COMPONENT_REF)
1709 from = TREE_OPERAND (from, 1);
1710 if (BASELINK_P (from))
1711 from = BASELINK_FUNCTIONS (from);
1712 if (TREE_CODE (from) == TEMPLATE_ID_EXPR)
1713 from = TREE_OPERAND (from, 0);
1714 return from;
1717 tree
1718 get_first_fn (tree from)
1720 return OVL_CURRENT (get_fns (from));
1723 /* Return a new OVL node, concatenating it with the old one. */
1725 tree
1726 ovl_cons (tree decl, tree chain)
1728 tree result = make_node (OVERLOAD);
1729 TREE_TYPE (result) = unknown_type_node;
1730 OVL_FUNCTION (result) = decl;
1731 TREE_CHAIN (result) = chain;
1733 return result;
1736 /* Build a new overloaded function. If this is the first one,
1737 just return it; otherwise, ovl_cons the _DECLs */
1739 tree
1740 build_overload (tree decl, tree chain)
1742 if (! chain && TREE_CODE (decl) != TEMPLATE_DECL)
1743 return decl;
1744 return ovl_cons (decl, chain);
1747 /* Return the scope where the overloaded functions OVL were found. */
1749 tree
1750 ovl_scope (tree ovl)
1752 if (TREE_CODE (ovl) == OFFSET_REF
1753 || TREE_CODE (ovl) == COMPONENT_REF)
1754 ovl = TREE_OPERAND (ovl, 1);
1755 if (TREE_CODE (ovl) == BASELINK)
1756 return BINFO_TYPE (BASELINK_BINFO (ovl));
1757 if (TREE_CODE (ovl) == TEMPLATE_ID_EXPR)
1758 ovl = TREE_OPERAND (ovl, 0);
1759 /* Skip using-declarations. */
1760 while (TREE_CODE (ovl) == OVERLOAD && OVL_USED (ovl) && OVL_CHAIN (ovl))
1761 ovl = OVL_CHAIN (ovl);
1762 return CP_DECL_CONTEXT (OVL_CURRENT (ovl));
1765 /* Return TRUE if FN is a non-static member function, FALSE otherwise.
1766 This function looks into BASELINK and OVERLOAD nodes. */
1768 bool
1769 non_static_member_function_p (tree fn)
1771 if (fn == NULL_TREE)
1772 return false;
1774 if (is_overloaded_fn (fn))
1775 fn = get_first_fn (fn);
1777 return (DECL_P (fn)
1778 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn));
1782 #define PRINT_RING_SIZE 4
1784 static const char *
1785 cxx_printable_name_internal (tree decl, int v, bool translate)
1787 static unsigned int uid_ring[PRINT_RING_SIZE];
1788 static char *print_ring[PRINT_RING_SIZE];
1789 static bool trans_ring[PRINT_RING_SIZE];
1790 static int ring_counter;
1791 int i;
1793 /* Only cache functions. */
1794 if (v < 2
1795 || TREE_CODE (decl) != FUNCTION_DECL
1796 || DECL_LANG_SPECIFIC (decl) == 0)
1797 return lang_decl_name (decl, v, translate);
1799 /* See if this print name is lying around. */
1800 for (i = 0; i < PRINT_RING_SIZE; i++)
1801 if (uid_ring[i] == DECL_UID (decl) && translate == trans_ring[i])
1802 /* yes, so return it. */
1803 return print_ring[i];
1805 if (++ring_counter == PRINT_RING_SIZE)
1806 ring_counter = 0;
1808 if (current_function_decl != NULL_TREE)
1810 /* There may be both translated and untranslated versions of the
1811 name cached. */
1812 for (i = 0; i < 2; i++)
1814 if (uid_ring[ring_counter] == DECL_UID (current_function_decl))
1815 ring_counter += 1;
1816 if (ring_counter == PRINT_RING_SIZE)
1817 ring_counter = 0;
1819 gcc_assert (uid_ring[ring_counter] != DECL_UID (current_function_decl));
1822 free (print_ring[ring_counter]);
1824 print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v, translate));
1825 uid_ring[ring_counter] = DECL_UID (decl);
1826 trans_ring[ring_counter] = translate;
1827 return print_ring[ring_counter];
1830 const char *
1831 cxx_printable_name (tree decl, int v)
1833 return cxx_printable_name_internal (decl, v, false);
1836 const char *
1837 cxx_printable_name_translate (tree decl, int v)
1839 return cxx_printable_name_internal (decl, v, true);
1842 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
1843 listed in RAISES. */
1845 tree
1846 build_exception_variant (tree type, tree raises)
1848 tree v;
1849 int type_quals;
1851 if (comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (type), ce_exact))
1852 return type;
1854 type_quals = TYPE_QUALS (type);
1855 for (v = TYPE_MAIN_VARIANT (type); v; v = TYPE_NEXT_VARIANT (v))
1856 if (check_qualified_type (v, type, type_quals, 0)
1857 && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (v), ce_exact))
1858 return v;
1860 /* Need to build a new variant. */
1861 v = build_variant_type_copy (type);
1862 TYPE_RAISES_EXCEPTIONS (v) = raises;
1863 return v;
1866 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new
1867 BOUND_TEMPLATE_TEMPLATE_PARM bound with NEWARGS as its template
1868 arguments. */
1870 tree
1871 bind_template_template_parm (tree t, tree newargs)
1873 tree decl = TYPE_NAME (t);
1874 tree t2;
1876 t2 = cxx_make_type (BOUND_TEMPLATE_TEMPLATE_PARM);
1877 decl = build_decl (input_location,
1878 TYPE_DECL, DECL_NAME (decl), NULL_TREE);
1880 /* These nodes have to be created to reflect new TYPE_DECL and template
1881 arguments. */
1882 TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t));
1883 TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl;
1884 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
1885 = build_template_info (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t), newargs);
1887 TREE_TYPE (decl) = t2;
1888 TYPE_NAME (t2) = decl;
1889 TYPE_STUB_DECL (t2) = decl;
1890 TYPE_SIZE (t2) = 0;
1891 SET_TYPE_STRUCTURAL_EQUALITY (t2);
1893 return t2;
1896 /* Called from count_trees via walk_tree. */
1898 static tree
1899 count_trees_r (tree *tp, int *walk_subtrees, void *data)
1901 ++*((int *) data);
1903 if (TYPE_P (*tp))
1904 *walk_subtrees = 0;
1906 return NULL_TREE;
1909 /* Debugging function for measuring the rough complexity of a tree
1910 representation. */
1913 count_trees (tree t)
1915 int n_trees = 0;
1916 cp_walk_tree_without_duplicates (&t, count_trees_r, &n_trees);
1917 return n_trees;
1920 /* Called from verify_stmt_tree via walk_tree. */
1922 static tree
1923 verify_stmt_tree_r (tree* tp, int * /*walk_subtrees*/, void* data)
1925 tree t = *tp;
1926 hash_table <pointer_hash <tree_node> > *statements
1927 = static_cast <hash_table <pointer_hash <tree_node> > *> (data);
1928 tree_node **slot;
1930 if (!STATEMENT_CODE_P (TREE_CODE (t)))
1931 return NULL_TREE;
1933 /* If this statement is already present in the hash table, then
1934 there is a circularity in the statement tree. */
1935 gcc_assert (!statements->find (t));
1937 slot = statements->find_slot (t, INSERT);
1938 *slot = t;
1940 return NULL_TREE;
1943 /* Debugging function to check that the statement T has not been
1944 corrupted. For now, this function simply checks that T contains no
1945 circularities. */
1947 void
1948 verify_stmt_tree (tree t)
1950 hash_table <pointer_hash <tree_node> > statements;
1951 statements.create (37);
1952 cp_walk_tree (&t, verify_stmt_tree_r, &statements, NULL);
1953 statements.dispose ();
1956 /* Check if the type T depends on a type with no linkage and if so, return
1957 it. If RELAXED_P then do not consider a class type declared within
1958 a vague-linkage function to have no linkage. */
1960 tree
1961 no_linkage_check (tree t, bool relaxed_p)
1963 tree r;
1965 /* There's no point in checking linkage on template functions; we
1966 can't know their complete types. */
1967 if (processing_template_decl)
1968 return NULL_TREE;
1970 switch (TREE_CODE (t))
1972 case RECORD_TYPE:
1973 if (TYPE_PTRMEMFUNC_P (t))
1974 goto ptrmem;
1975 /* Lambda types that don't have mangling scope have no linkage. We
1976 check CLASSTYPE_LAMBDA_EXPR here rather than LAMBDA_TYPE_P because
1977 when we get here from pushtag none of the lambda information is
1978 set up yet, so we want to assume that the lambda has linkage and
1979 fix it up later if not. */
1980 if (CLASSTYPE_LAMBDA_EXPR (t)
1981 && LAMBDA_TYPE_EXTRA_SCOPE (t) == NULL_TREE)
1982 return t;
1983 /* Fall through. */
1984 case UNION_TYPE:
1985 if (!CLASS_TYPE_P (t))
1986 return NULL_TREE;
1987 /* Fall through. */
1988 case ENUMERAL_TYPE:
1989 /* Only treat anonymous types as having no linkage if they're at
1990 namespace scope. This is core issue 966. */
1991 if (TYPE_ANONYMOUS_P (t) && TYPE_NAMESPACE_SCOPE_P (t))
1992 return t;
1994 for (r = CP_TYPE_CONTEXT (t); ; )
1996 /* If we're a nested type of a !TREE_PUBLIC class, we might not
1997 have linkage, or we might just be in an anonymous namespace.
1998 If we're in a TREE_PUBLIC class, we have linkage. */
1999 if (TYPE_P (r) && !TREE_PUBLIC (TYPE_NAME (r)))
2000 return no_linkage_check (TYPE_CONTEXT (t), relaxed_p);
2001 else if (TREE_CODE (r) == FUNCTION_DECL)
2003 if (!relaxed_p || !vague_linkage_p (r))
2004 return t;
2005 else
2006 r = CP_DECL_CONTEXT (r);
2008 else
2009 break;
2012 return NULL_TREE;
2014 case ARRAY_TYPE:
2015 case POINTER_TYPE:
2016 case REFERENCE_TYPE:
2017 return no_linkage_check (TREE_TYPE (t), relaxed_p);
2019 case OFFSET_TYPE:
2020 ptrmem:
2021 r = no_linkage_check (TYPE_PTRMEM_POINTED_TO_TYPE (t),
2022 relaxed_p);
2023 if (r)
2024 return r;
2025 return no_linkage_check (TYPE_PTRMEM_CLASS_TYPE (t), relaxed_p);
2027 case METHOD_TYPE:
2028 r = no_linkage_check (TYPE_METHOD_BASETYPE (t), relaxed_p);
2029 if (r)
2030 return r;
2031 /* Fall through. */
2032 case FUNCTION_TYPE:
2034 tree parm;
2035 for (parm = TYPE_ARG_TYPES (t);
2036 parm && parm != void_list_node;
2037 parm = TREE_CHAIN (parm))
2039 r = no_linkage_check (TREE_VALUE (parm), relaxed_p);
2040 if (r)
2041 return r;
2043 return no_linkage_check (TREE_TYPE (t), relaxed_p);
2046 default:
2047 return NULL_TREE;
2051 extern int depth_reached;
2053 void
2054 cxx_print_statistics (void)
2056 print_search_statistics ();
2057 print_class_statistics ();
2058 print_template_statistics ();
2059 if (GATHER_STATISTICS)
2060 fprintf (stderr, "maximum template instantiation depth reached: %d\n",
2061 depth_reached);
2064 /* Return, as an INTEGER_CST node, the number of elements for TYPE
2065 (which is an ARRAY_TYPE). This counts only elements of the top
2066 array. */
2068 tree
2069 array_type_nelts_top (tree type)
2071 return fold_build2_loc (input_location,
2072 PLUS_EXPR, sizetype,
2073 array_type_nelts (type),
2074 size_one_node);
2077 /* Return, as an INTEGER_CST node, the number of elements for TYPE
2078 (which is an ARRAY_TYPE). This one is a recursive count of all
2079 ARRAY_TYPEs that are clumped together. */
2081 tree
2082 array_type_nelts_total (tree type)
2084 tree sz = array_type_nelts_top (type);
2085 type = TREE_TYPE (type);
2086 while (TREE_CODE (type) == ARRAY_TYPE)
2088 tree n = array_type_nelts_top (type);
2089 sz = fold_build2_loc (input_location,
2090 MULT_EXPR, sizetype, sz, n);
2091 type = TREE_TYPE (type);
2093 return sz;
2096 /* Called from break_out_target_exprs via mapcar. */
2098 static tree
2099 bot_manip (tree* tp, int* walk_subtrees, void* data)
2101 splay_tree target_remap = ((splay_tree) data);
2102 tree t = *tp;
2104 if (!TYPE_P (t) && TREE_CONSTANT (t) && !TREE_SIDE_EFFECTS (t))
2106 /* There can't be any TARGET_EXPRs or their slot variables below this
2107 point. But we must make a copy, in case subsequent processing
2108 alters any part of it. For example, during gimplification a cast
2109 of the form (T) &X::f (where "f" is a member function) will lead
2110 to replacing the PTRMEM_CST for &X::f with a VAR_DECL. */
2111 *walk_subtrees = 0;
2112 *tp = unshare_expr (t);
2113 return NULL_TREE;
2115 if (TREE_CODE (t) == TARGET_EXPR)
2117 tree u;
2119 if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
2121 u = build_cplus_new (TREE_TYPE (t), TREE_OPERAND (t, 1),
2122 tf_warning_or_error);
2123 if (AGGR_INIT_ZERO_FIRST (TREE_OPERAND (t, 1)))
2124 AGGR_INIT_ZERO_FIRST (TREE_OPERAND (u, 1)) = true;
2126 else
2127 u = build_target_expr_with_type (TREE_OPERAND (t, 1), TREE_TYPE (t),
2128 tf_warning_or_error);
2130 TARGET_EXPR_IMPLICIT_P (u) = TARGET_EXPR_IMPLICIT_P (t);
2131 TARGET_EXPR_LIST_INIT_P (u) = TARGET_EXPR_LIST_INIT_P (t);
2132 TARGET_EXPR_DIRECT_INIT_P (u) = TARGET_EXPR_DIRECT_INIT_P (t);
2134 /* Map the old variable to the new one. */
2135 splay_tree_insert (target_remap,
2136 (splay_tree_key) TREE_OPERAND (t, 0),
2137 (splay_tree_value) TREE_OPERAND (u, 0));
2139 TREE_OPERAND (u, 1) = break_out_target_exprs (TREE_OPERAND (u, 1));
2141 /* Replace the old expression with the new version. */
2142 *tp = u;
2143 /* We don't have to go below this point; the recursive call to
2144 break_out_target_exprs will have handled anything below this
2145 point. */
2146 *walk_subtrees = 0;
2147 return NULL_TREE;
2150 /* Make a copy of this node. */
2151 t = copy_tree_r (tp, walk_subtrees, NULL);
2152 if (TREE_CODE (*tp) == CALL_EXPR)
2153 set_flags_from_callee (*tp);
2154 return t;
2157 /* Replace all remapped VAR_DECLs in T with their new equivalents.
2158 DATA is really a splay-tree mapping old variables to new
2159 variables. */
2161 static tree
2162 bot_replace (tree* t, int* /*walk_subtrees*/, void* data)
2164 splay_tree target_remap = ((splay_tree) data);
2166 if (TREE_CODE (*t) == VAR_DECL)
2168 splay_tree_node n = splay_tree_lookup (target_remap,
2169 (splay_tree_key) *t);
2170 if (n)
2171 *t = (tree) n->value;
2173 else if (TREE_CODE (*t) == PARM_DECL
2174 && DECL_NAME (*t) == this_identifier)
2176 /* In an NSDMI we need to replace the 'this' parameter we used for
2177 parsing with the real one for this function. */
2178 *t = current_class_ptr;
2180 else if (TREE_CODE (*t) == CONVERT_EXPR
2181 && CONVERT_EXPR_VBASE_PATH (*t))
2183 /* In an NSDMI build_base_path defers building conversions to virtual
2184 bases, and we handle it here. */
2185 tree basetype = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (*t)));
2186 vec<tree, va_gc> *vbases = CLASSTYPE_VBASECLASSES (current_class_type);
2187 int i; tree binfo;
2188 FOR_EACH_VEC_SAFE_ELT (vbases, i, binfo)
2189 if (BINFO_TYPE (binfo) == basetype)
2190 break;
2191 *t = build_base_path (PLUS_EXPR, TREE_OPERAND (*t, 0), binfo, true,
2192 tf_warning_or_error);
2195 return NULL_TREE;
2198 /* When we parse a default argument expression, we may create
2199 temporary variables via TARGET_EXPRs. When we actually use the
2200 default-argument expression, we make a copy of the expression
2201 and replace the temporaries with appropriate local versions. */
2203 tree
2204 break_out_target_exprs (tree t)
2206 static int target_remap_count;
2207 static splay_tree target_remap;
2209 if (!target_remap_count++)
2210 target_remap = splay_tree_new (splay_tree_compare_pointers,
2211 /*splay_tree_delete_key_fn=*/NULL,
2212 /*splay_tree_delete_value_fn=*/NULL);
2213 cp_walk_tree (&t, bot_manip, target_remap, NULL);
2214 cp_walk_tree (&t, bot_replace, target_remap, NULL);
2216 if (!--target_remap_count)
2218 splay_tree_delete (target_remap);
2219 target_remap = NULL;
2222 return t;
2225 /* Similar to `build_nt', but for template definitions of dependent
2226 expressions */
2228 tree
2229 build_min_nt_loc (location_t loc, enum tree_code code, ...)
2231 tree t;
2232 int length;
2233 int i;
2234 va_list p;
2236 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
2238 va_start (p, code);
2240 t = make_node (code);
2241 SET_EXPR_LOCATION (t, loc);
2242 length = TREE_CODE_LENGTH (code);
2244 for (i = 0; i < length; i++)
2246 tree x = va_arg (p, tree);
2247 TREE_OPERAND (t, i) = x;
2250 va_end (p);
2251 return t;
2255 /* Similar to `build', but for template definitions. */
2257 tree
2258 build_min (enum tree_code code, tree tt, ...)
2260 tree t;
2261 int length;
2262 int i;
2263 va_list p;
2265 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
2267 va_start (p, tt);
2269 t = make_node (code);
2270 length = TREE_CODE_LENGTH (code);
2271 TREE_TYPE (t) = tt;
2273 for (i = 0; i < length; i++)
2275 tree x = va_arg (p, tree);
2276 TREE_OPERAND (t, i) = x;
2277 if (x && !TYPE_P (x) && TREE_SIDE_EFFECTS (x))
2278 TREE_SIDE_EFFECTS (t) = 1;
2281 va_end (p);
2282 return t;
2285 /* Similar to `build', but for template definitions of non-dependent
2286 expressions. NON_DEP is the non-dependent expression that has been
2287 built. */
2289 tree
2290 build_min_non_dep (enum tree_code code, tree non_dep, ...)
2292 tree t;
2293 int length;
2294 int i;
2295 va_list p;
2297 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
2299 va_start (p, non_dep);
2301 if (REFERENCE_REF_P (non_dep))
2302 non_dep = TREE_OPERAND (non_dep, 0);
2304 t = make_node (code);
2305 length = TREE_CODE_LENGTH (code);
2306 TREE_TYPE (t) = TREE_TYPE (non_dep);
2307 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
2309 for (i = 0; i < length; i++)
2311 tree x = va_arg (p, tree);
2312 TREE_OPERAND (t, i) = x;
2315 if (code == COMPOUND_EXPR && TREE_CODE (non_dep) != COMPOUND_EXPR)
2316 /* This should not be considered a COMPOUND_EXPR, because it
2317 resolves to an overload. */
2318 COMPOUND_EXPR_OVERLOADED (t) = 1;
2320 va_end (p);
2321 return convert_from_reference (t);
2324 /* Similar to `build_nt_call_vec', but for template definitions of
2325 non-dependent expressions. NON_DEP is the non-dependent expression
2326 that has been built. */
2328 tree
2329 build_min_non_dep_call_vec (tree non_dep, tree fn, vec<tree, va_gc> *argvec)
2331 tree t = build_nt_call_vec (fn, argvec);
2332 if (REFERENCE_REF_P (non_dep))
2333 non_dep = TREE_OPERAND (non_dep, 0);
2334 TREE_TYPE (t) = TREE_TYPE (non_dep);
2335 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
2336 return convert_from_reference (t);
2339 tree
2340 get_type_decl (tree t)
2342 if (TREE_CODE (t) == TYPE_DECL)
2343 return t;
2344 if (TYPE_P (t))
2345 return TYPE_STUB_DECL (t);
2346 gcc_assert (t == error_mark_node);
2347 return t;
2350 /* Returns the namespace that contains DECL, whether directly or
2351 indirectly. */
2353 tree
2354 decl_namespace_context (tree decl)
2356 while (1)
2358 if (TREE_CODE (decl) == NAMESPACE_DECL)
2359 return decl;
2360 else if (TYPE_P (decl))
2361 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
2362 else
2363 decl = CP_DECL_CONTEXT (decl);
2367 /* Returns true if decl is within an anonymous namespace, however deeply
2368 nested, or false otherwise. */
2370 bool
2371 decl_anon_ns_mem_p (const_tree decl)
2373 while (1)
2375 if (decl == NULL_TREE || decl == error_mark_node)
2376 return false;
2377 if (TREE_CODE (decl) == NAMESPACE_DECL
2378 && DECL_NAME (decl) == NULL_TREE)
2379 return true;
2380 /* Classes and namespaces inside anonymous namespaces have
2381 TREE_PUBLIC == 0, so we can shortcut the search. */
2382 else if (TYPE_P (decl))
2383 return (TREE_PUBLIC (TYPE_NAME (decl)) == 0);
2384 else if (TREE_CODE (decl) == NAMESPACE_DECL)
2385 return (TREE_PUBLIC (decl) == 0);
2386 else
2387 decl = DECL_CONTEXT (decl);
2391 /* Subroutine of cp_tree_equal: t1 and t2 are the CALL_EXPR_FNs of two
2392 CALL_EXPRS. Return whether they are equivalent. */
2394 static bool
2395 called_fns_equal (tree t1, tree t2)
2397 /* Core 1321: dependent names are equivalent even if the overload sets
2398 are different. But do compare explicit template arguments. */
2399 tree name1 = dependent_name (t1);
2400 tree name2 = dependent_name (t2);
2401 if (name1 || name2)
2403 tree targs1 = NULL_TREE, targs2 = NULL_TREE;
2405 if (name1 != name2)
2406 return false;
2408 if (TREE_CODE (t1) == TEMPLATE_ID_EXPR)
2409 targs1 = TREE_OPERAND (t1, 1);
2410 if (TREE_CODE (t2) == TEMPLATE_ID_EXPR)
2411 targs2 = TREE_OPERAND (t2, 1);
2412 return cp_tree_equal (targs1, targs2);
2414 else
2415 return cp_tree_equal (t1, t2);
2418 /* Return truthvalue of whether T1 is the same tree structure as T2.
2419 Return 1 if they are the same. Return 0 if they are different. */
2421 bool
2422 cp_tree_equal (tree t1, tree t2)
2424 enum tree_code code1, code2;
2426 if (t1 == t2)
2427 return true;
2428 if (!t1 || !t2)
2429 return false;
2431 for (code1 = TREE_CODE (t1);
2432 CONVERT_EXPR_CODE_P (code1)
2433 || code1 == NON_LVALUE_EXPR;
2434 code1 = TREE_CODE (t1))
2435 t1 = TREE_OPERAND (t1, 0);
2436 for (code2 = TREE_CODE (t2);
2437 CONVERT_EXPR_CODE_P (code2)
2438 || code1 == NON_LVALUE_EXPR;
2439 code2 = TREE_CODE (t2))
2440 t2 = TREE_OPERAND (t2, 0);
2442 /* They might have become equal now. */
2443 if (t1 == t2)
2444 return true;
2446 if (code1 != code2)
2447 return false;
2449 switch (code1)
2451 case INTEGER_CST:
2452 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
2453 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
2455 case REAL_CST:
2456 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2458 case STRING_CST:
2459 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2460 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2461 TREE_STRING_LENGTH (t1));
2463 case FIXED_CST:
2464 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
2465 TREE_FIXED_CST (t2));
2467 case COMPLEX_CST:
2468 return cp_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
2469 && cp_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
2471 case CONSTRUCTOR:
2472 /* We need to do this when determining whether or not two
2473 non-type pointer to member function template arguments
2474 are the same. */
2475 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
2476 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
2477 return false;
2479 tree field, value;
2480 unsigned int i;
2481 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
2483 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
2484 if (!cp_tree_equal (field, elt2->index)
2485 || !cp_tree_equal (value, elt2->value))
2486 return false;
2489 return true;
2491 case TREE_LIST:
2492 if (!cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
2493 return false;
2494 if (!cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
2495 return false;
2496 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
2498 case SAVE_EXPR:
2499 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2501 case CALL_EXPR:
2503 tree arg1, arg2;
2504 call_expr_arg_iterator iter1, iter2;
2505 if (!called_fns_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
2506 return false;
2507 for (arg1 = first_call_expr_arg (t1, &iter1),
2508 arg2 = first_call_expr_arg (t2, &iter2);
2509 arg1 && arg2;
2510 arg1 = next_call_expr_arg (&iter1),
2511 arg2 = next_call_expr_arg (&iter2))
2512 if (!cp_tree_equal (arg1, arg2))
2513 return false;
2514 if (arg1 || arg2)
2515 return false;
2516 return true;
2519 case TARGET_EXPR:
2521 tree o1 = TREE_OPERAND (t1, 0);
2522 tree o2 = TREE_OPERAND (t2, 0);
2524 /* Special case: if either target is an unallocated VAR_DECL,
2525 it means that it's going to be unified with whatever the
2526 TARGET_EXPR is really supposed to initialize, so treat it
2527 as being equivalent to anything. */
2528 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
2529 && !DECL_RTL_SET_P (o1))
2530 /*Nop*/;
2531 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
2532 && !DECL_RTL_SET_P (o2))
2533 /*Nop*/;
2534 else if (!cp_tree_equal (o1, o2))
2535 return false;
2537 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
2540 case WITH_CLEANUP_EXPR:
2541 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
2542 return false;
2543 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
2545 case COMPONENT_REF:
2546 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
2547 return false;
2548 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2550 case PARM_DECL:
2551 /* For comparing uses of parameters in late-specified return types
2552 with an out-of-class definition of the function, but can also come
2553 up for expressions that involve 'this' in a member function
2554 template. */
2555 if (same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2557 if (DECL_ARTIFICIAL (t1) ^ DECL_ARTIFICIAL (t2))
2558 return false;
2559 if (DECL_ARTIFICIAL (t1)
2560 || (DECL_PARM_LEVEL (t1) == DECL_PARM_LEVEL (t2)
2561 && DECL_PARM_INDEX (t1) == DECL_PARM_INDEX (t2)))
2562 return true;
2564 return false;
2566 case VAR_DECL:
2567 case CONST_DECL:
2568 case FIELD_DECL:
2569 case FUNCTION_DECL:
2570 case TEMPLATE_DECL:
2571 case IDENTIFIER_NODE:
2572 case SSA_NAME:
2573 return false;
2575 case BASELINK:
2576 return (BASELINK_BINFO (t1) == BASELINK_BINFO (t2)
2577 && BASELINK_ACCESS_BINFO (t1) == BASELINK_ACCESS_BINFO (t2)
2578 && BASELINK_QUALIFIED_P (t1) == BASELINK_QUALIFIED_P (t2)
2579 && cp_tree_equal (BASELINK_FUNCTIONS (t1),
2580 BASELINK_FUNCTIONS (t2)));
2582 case TEMPLATE_PARM_INDEX:
2583 return (TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
2584 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2)
2585 && (TEMPLATE_PARM_PARAMETER_PACK (t1)
2586 == TEMPLATE_PARM_PARAMETER_PACK (t2))
2587 && same_type_p (TREE_TYPE (TEMPLATE_PARM_DECL (t1)),
2588 TREE_TYPE (TEMPLATE_PARM_DECL (t2))));
2590 case TEMPLATE_ID_EXPR:
2591 return (cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
2592 && cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
2594 case TREE_VEC:
2596 unsigned ix;
2597 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2598 return false;
2599 for (ix = TREE_VEC_LENGTH (t1); ix--;)
2600 if (!cp_tree_equal (TREE_VEC_ELT (t1, ix),
2601 TREE_VEC_ELT (t2, ix)))
2602 return false;
2603 return true;
2606 case SIZEOF_EXPR:
2607 case ALIGNOF_EXPR:
2609 tree o1 = TREE_OPERAND (t1, 0);
2610 tree o2 = TREE_OPERAND (t2, 0);
2612 if (code1 == SIZEOF_EXPR)
2614 if (SIZEOF_EXPR_TYPE_P (t1))
2615 o1 = TREE_TYPE (o1);
2616 if (SIZEOF_EXPR_TYPE_P (t2))
2617 o2 = TREE_TYPE (o2);
2619 if (TREE_CODE (o1) != TREE_CODE (o2))
2620 return false;
2621 if (TYPE_P (o1))
2622 return same_type_p (o1, o2);
2623 else
2624 return cp_tree_equal (o1, o2);
2627 case MODOP_EXPR:
2629 tree t1_op1, t2_op1;
2631 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
2632 return false;
2634 t1_op1 = TREE_OPERAND (t1, 1);
2635 t2_op1 = TREE_OPERAND (t2, 1);
2636 if (TREE_CODE (t1_op1) != TREE_CODE (t2_op1))
2637 return false;
2639 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t2, 2));
2642 case PTRMEM_CST:
2643 /* Two pointer-to-members are the same if they point to the same
2644 field or function in the same class. */
2645 if (PTRMEM_CST_MEMBER (t1) != PTRMEM_CST_MEMBER (t2))
2646 return false;
2648 return same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2));
2650 case OVERLOAD:
2651 if (OVL_FUNCTION (t1) != OVL_FUNCTION (t2))
2652 return false;
2653 return cp_tree_equal (OVL_CHAIN (t1), OVL_CHAIN (t2));
2655 case TRAIT_EXPR:
2656 if (TRAIT_EXPR_KIND (t1) != TRAIT_EXPR_KIND (t2))
2657 return false;
2658 return same_type_p (TRAIT_EXPR_TYPE1 (t1), TRAIT_EXPR_TYPE1 (t2))
2659 && same_type_p (TRAIT_EXPR_TYPE2 (t1), TRAIT_EXPR_TYPE2 (t2));
2661 case CAST_EXPR:
2662 case STATIC_CAST_EXPR:
2663 case REINTERPRET_CAST_EXPR:
2664 case CONST_CAST_EXPR:
2665 case DYNAMIC_CAST_EXPR:
2666 case IMPLICIT_CONV_EXPR:
2667 case NEW_EXPR:
2668 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2669 return false;
2670 /* Now compare operands as usual. */
2671 break;
2673 case DEFERRED_NOEXCEPT:
2674 return (cp_tree_equal (DEFERRED_NOEXCEPT_PATTERN (t1),
2675 DEFERRED_NOEXCEPT_PATTERN (t2))
2676 && comp_template_args (DEFERRED_NOEXCEPT_ARGS (t1),
2677 DEFERRED_NOEXCEPT_ARGS (t2)));
2678 break;
2680 default:
2681 break;
2684 switch (TREE_CODE_CLASS (code1))
2686 case tcc_unary:
2687 case tcc_binary:
2688 case tcc_comparison:
2689 case tcc_expression:
2690 case tcc_vl_exp:
2691 case tcc_reference:
2692 case tcc_statement:
2694 int i, n;
2696 n = cp_tree_operand_length (t1);
2697 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
2698 && n != TREE_OPERAND_LENGTH (t2))
2699 return false;
2701 for (i = 0; i < n; ++i)
2702 if (!cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
2703 return false;
2705 return true;
2708 case tcc_type:
2709 return same_type_p (t1, t2);
2710 default:
2711 gcc_unreachable ();
2713 /* We can get here with --disable-checking. */
2714 return false;
2717 /* The type of ARG when used as an lvalue. */
2719 tree
2720 lvalue_type (tree arg)
2722 tree type = TREE_TYPE (arg);
2723 return type;
2726 /* The type of ARG for printing error messages; denote lvalues with
2727 reference types. */
2729 tree
2730 error_type (tree arg)
2732 tree type = TREE_TYPE (arg);
2734 if (TREE_CODE (type) == ARRAY_TYPE)
2736 else if (TREE_CODE (type) == ERROR_MARK)
2738 else if (real_lvalue_p (arg))
2739 type = build_reference_type (lvalue_type (arg));
2740 else if (MAYBE_CLASS_TYPE_P (type))
2741 type = lvalue_type (arg);
2743 return type;
2746 /* Does FUNCTION use a variable-length argument list? */
2749 varargs_function_p (const_tree function)
2751 return stdarg_p (TREE_TYPE (function));
2754 /* Returns 1 if decl is a member of a class. */
2757 member_p (const_tree decl)
2759 const_tree const ctx = DECL_CONTEXT (decl);
2760 return (ctx && TYPE_P (ctx));
2763 /* Create a placeholder for member access where we don't actually have an
2764 object that the access is against. */
2766 tree
2767 build_dummy_object (tree type)
2769 tree decl = build1 (NOP_EXPR, build_pointer_type (type), void_zero_node);
2770 return cp_build_indirect_ref (decl, RO_NULL, tf_warning_or_error);
2773 /* We've gotten a reference to a member of TYPE. Return *this if appropriate,
2774 or a dummy object otherwise. If BINFOP is non-0, it is filled with the
2775 binfo path from current_class_type to TYPE, or 0. */
2777 tree
2778 maybe_dummy_object (tree type, tree* binfop)
2780 tree decl, context;
2781 tree binfo;
2782 tree current = current_nonlambda_class_type ();
2784 if (current
2785 && (binfo = lookup_base (current, type, ba_any, NULL,
2786 tf_warning_or_error)))
2787 context = current;
2788 else
2790 /* Reference from a nested class member function. */
2791 context = type;
2792 binfo = TYPE_BINFO (type);
2795 if (binfop)
2796 *binfop = binfo;
2798 if (current_class_ref
2799 /* current_class_ref might not correspond to current_class_type if
2800 we're in tsubst_default_argument or a lambda-declarator; in either
2801 case, we want to use current_class_ref if it matches CONTEXT. */
2802 && (same_type_ignoring_top_level_qualifiers_p
2803 (TREE_TYPE (current_class_ref), context)))
2804 decl = current_class_ref;
2805 else if (current != current_class_type
2806 && context == nonlambda_method_basetype ())
2807 /* In a lambda, need to go through 'this' capture. */
2808 decl = (build_x_indirect_ref
2809 (input_location, (lambda_expr_this_capture
2810 (CLASSTYPE_LAMBDA_EXPR (current_class_type))),
2811 RO_NULL, tf_warning_or_error));
2812 else
2813 decl = build_dummy_object (context);
2815 return decl;
2818 /* Returns 1 if OB is a placeholder object, or a pointer to one. */
2821 is_dummy_object (const_tree ob)
2823 if (TREE_CODE (ob) == INDIRECT_REF)
2824 ob = TREE_OPERAND (ob, 0);
2825 return (TREE_CODE (ob) == NOP_EXPR
2826 && TREE_OPERAND (ob, 0) == void_zero_node);
2829 /* Returns 1 iff type T is something we want to treat as a scalar type for
2830 the purpose of deciding whether it is trivial/POD/standard-layout. */
2832 bool
2833 scalarish_type_p (const_tree t)
2835 if (t == error_mark_node)
2836 return 1;
2838 return (SCALAR_TYPE_P (t)
2839 || TREE_CODE (t) == VECTOR_TYPE);
2842 /* Returns true iff T requires non-trivial default initialization. */
2844 bool
2845 type_has_nontrivial_default_init (const_tree t)
2847 t = strip_array_types (CONST_CAST_TREE (t));
2849 if (CLASS_TYPE_P (t))
2850 return TYPE_HAS_COMPLEX_DFLT (t);
2851 else
2852 return 0;
2855 /* Returns true iff copying an object of type T (including via move
2856 constructor) is non-trivial. That is, T has no non-trivial copy
2857 constructors and no non-trivial move constructors. */
2859 bool
2860 type_has_nontrivial_copy_init (const_tree t)
2862 t = strip_array_types (CONST_CAST_TREE (t));
2864 if (CLASS_TYPE_P (t))
2866 gcc_assert (COMPLETE_TYPE_P (t));
2867 return ((TYPE_HAS_COPY_CTOR (t)
2868 && TYPE_HAS_COMPLEX_COPY_CTOR (t))
2869 || TYPE_HAS_COMPLEX_MOVE_CTOR (t));
2871 else
2872 return 0;
2875 /* Returns 1 iff type T is a trivially copyable type, as defined in
2876 [basic.types] and [class]. */
2878 bool
2879 trivially_copyable_p (const_tree t)
2881 t = strip_array_types (CONST_CAST_TREE (t));
2883 if (CLASS_TYPE_P (t))
2884 return ((!TYPE_HAS_COPY_CTOR (t)
2885 || !TYPE_HAS_COMPLEX_COPY_CTOR (t))
2886 && !TYPE_HAS_COMPLEX_MOVE_CTOR (t)
2887 && (!TYPE_HAS_COPY_ASSIGN (t)
2888 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (t))
2889 && !TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
2890 && TYPE_HAS_TRIVIAL_DESTRUCTOR (t));
2891 else
2892 return scalarish_type_p (t);
2895 /* Returns 1 iff type T is a trivial type, as defined in [basic.types] and
2896 [class]. */
2898 bool
2899 trivial_type_p (const_tree t)
2901 t = strip_array_types (CONST_CAST_TREE (t));
2903 if (CLASS_TYPE_P (t))
2904 return (TYPE_HAS_TRIVIAL_DFLT (t)
2905 && trivially_copyable_p (t));
2906 else
2907 return scalarish_type_p (t);
2910 /* Returns 1 iff type T is a POD type, as defined in [basic.types]. */
2912 bool
2913 pod_type_p (const_tree t)
2915 /* This CONST_CAST is okay because strip_array_types returns its
2916 argument unmodified and we assign it to a const_tree. */
2917 t = strip_array_types (CONST_CAST_TREE(t));
2919 if (!CLASS_TYPE_P (t))
2920 return scalarish_type_p (t);
2921 else if (cxx_dialect > cxx98)
2922 /* [class]/10: A POD struct is a class that is both a trivial class and a
2923 standard-layout class, and has no non-static data members of type
2924 non-POD struct, non-POD union (or array of such types).
2926 We don't need to check individual members because if a member is
2927 non-std-layout or non-trivial, the class will be too. */
2928 return (std_layout_type_p (t) && trivial_type_p (t));
2929 else
2930 /* The C++98 definition of POD is different. */
2931 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
2934 /* Returns true iff T is POD for the purpose of layout, as defined in the
2935 C++ ABI. */
2937 bool
2938 layout_pod_type_p (const_tree t)
2940 t = strip_array_types (CONST_CAST_TREE (t));
2942 if (CLASS_TYPE_P (t))
2943 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
2944 else
2945 return scalarish_type_p (t);
2948 /* Returns true iff T is a standard-layout type, as defined in
2949 [basic.types]. */
2951 bool
2952 std_layout_type_p (const_tree t)
2954 t = strip_array_types (CONST_CAST_TREE (t));
2956 if (CLASS_TYPE_P (t))
2957 return !CLASSTYPE_NON_STD_LAYOUT (t);
2958 else
2959 return scalarish_type_p (t);
2962 /* Nonzero iff type T is a class template implicit specialization. */
2964 bool
2965 class_tmpl_impl_spec_p (const_tree t)
2967 return CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INSTANTIATION (t);
2970 /* Returns 1 iff zero initialization of type T means actually storing
2971 zeros in it. */
2974 zero_init_p (const_tree t)
2976 /* This CONST_CAST is okay because strip_array_types returns its
2977 argument unmodified and we assign it to a const_tree. */
2978 t = strip_array_types (CONST_CAST_TREE(t));
2980 if (t == error_mark_node)
2981 return 1;
2983 /* NULL pointers to data members are initialized with -1. */
2984 if (TYPE_PTRDATAMEM_P (t))
2985 return 0;
2987 /* Classes that contain types that can't be zero-initialized, cannot
2988 be zero-initialized themselves. */
2989 if (CLASS_TYPE_P (t) && CLASSTYPE_NON_ZERO_INIT_P (t))
2990 return 0;
2992 return 1;
2995 /* Table of valid C++ attributes. */
2996 const struct attribute_spec cxx_attribute_table[] =
2998 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
2999 affects_type_identity } */
3000 { "java_interface", 0, 0, false, false, false,
3001 handle_java_interface_attribute, false },
3002 { "com_interface", 0, 0, false, false, false,
3003 handle_com_interface_attribute, false },
3004 { "init_priority", 1, 1, true, false, false,
3005 handle_init_priority_attribute, false },
3006 { "abi_tag", 1, -1, false, false, false,
3007 handle_abi_tag_attribute, true },
3008 { NULL, 0, 0, false, false, false, NULL, false }
3011 /* Handle a "java_interface" attribute; arguments as in
3012 struct attribute_spec.handler. */
3013 static tree
3014 handle_java_interface_attribute (tree* node,
3015 tree name,
3016 tree /*args*/,
3017 int flags,
3018 bool* no_add_attrs)
3020 if (DECL_P (*node)
3021 || !CLASS_TYPE_P (*node)
3022 || !TYPE_FOR_JAVA (*node))
3024 error ("%qE attribute can only be applied to Java class definitions",
3025 name);
3026 *no_add_attrs = true;
3027 return NULL_TREE;
3029 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
3030 *node = build_variant_type_copy (*node);
3031 TYPE_JAVA_INTERFACE (*node) = 1;
3033 return NULL_TREE;
3036 /* Handle a "com_interface" attribute; arguments as in
3037 struct attribute_spec.handler. */
3038 static tree
3039 handle_com_interface_attribute (tree* node,
3040 tree name,
3041 tree /*args*/,
3042 int /*flags*/,
3043 bool* no_add_attrs)
3045 static int warned;
3047 *no_add_attrs = true;
3049 if (DECL_P (*node)
3050 || !CLASS_TYPE_P (*node)
3051 || *node != TYPE_MAIN_VARIANT (*node))
3053 warning (OPT_Wattributes, "%qE attribute can only be applied "
3054 "to class definitions", name);
3055 return NULL_TREE;
3058 if (!warned++)
3059 warning (0, "%qE is obsolete; g++ vtables are now COM-compatible by default",
3060 name);
3062 return NULL_TREE;
3065 /* Handle an "init_priority" attribute; arguments as in
3066 struct attribute_spec.handler. */
3067 static tree
3068 handle_init_priority_attribute (tree* node,
3069 tree name,
3070 tree args,
3071 int /*flags*/,
3072 bool* no_add_attrs)
3074 tree initp_expr = TREE_VALUE (args);
3075 tree decl = *node;
3076 tree type = TREE_TYPE (decl);
3077 int pri;
3079 STRIP_NOPS (initp_expr);
3081 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
3083 error ("requested init_priority is not an integer constant");
3084 *no_add_attrs = true;
3085 return NULL_TREE;
3088 pri = TREE_INT_CST_LOW (initp_expr);
3090 type = strip_array_types (type);
3092 if (decl == NULL_TREE
3093 || TREE_CODE (decl) != VAR_DECL
3094 || !TREE_STATIC (decl)
3095 || DECL_EXTERNAL (decl)
3096 || (TREE_CODE (type) != RECORD_TYPE
3097 && TREE_CODE (type) != UNION_TYPE)
3098 /* Static objects in functions are initialized the
3099 first time control passes through that
3100 function. This is not precise enough to pin down an
3101 init_priority value, so don't allow it. */
3102 || current_function_decl)
3104 error ("can only use %qE attribute on file-scope definitions "
3105 "of objects of class type", name);
3106 *no_add_attrs = true;
3107 return NULL_TREE;
3110 if (pri > MAX_INIT_PRIORITY || pri <= 0)
3112 error ("requested init_priority is out of range");
3113 *no_add_attrs = true;
3114 return NULL_TREE;
3117 /* Check for init_priorities that are reserved for
3118 language and runtime support implementations.*/
3119 if (pri <= MAX_RESERVED_INIT_PRIORITY)
3121 warning
3122 (0, "requested init_priority is reserved for internal use");
3125 if (SUPPORTS_INIT_PRIORITY)
3127 SET_DECL_INIT_PRIORITY (decl, pri);
3128 DECL_HAS_INIT_PRIORITY_P (decl) = 1;
3129 return NULL_TREE;
3131 else
3133 error ("%qE attribute is not supported on this platform", name);
3134 *no_add_attrs = true;
3135 return NULL_TREE;
3139 /* DECL is being redeclared; the old declaration had the abi tags in OLD,
3140 and the new one has the tags in NEW_. Give an error if there are tags
3141 in NEW_ that weren't in OLD. */
3143 bool
3144 check_abi_tag_redeclaration (const_tree decl, const_tree old, const_tree new_)
3146 if (old && TREE_CODE (TREE_VALUE (old)) == TREE_LIST)
3147 old = TREE_VALUE (old);
3148 if (new_ && TREE_CODE (TREE_VALUE (new_)) == TREE_LIST)
3149 new_ = TREE_VALUE (new_);
3150 bool err = false;
3151 for (const_tree t = new_; t; t = TREE_CHAIN (t))
3153 tree str = TREE_VALUE (t);
3154 for (const_tree in = old; in; in = TREE_CHAIN (in))
3156 tree ostr = TREE_VALUE (in);
3157 if (cp_tree_equal (str, ostr))
3158 goto found;
3160 error ("redeclaration of %qD adds abi tag %E", decl, str);
3161 err = true;
3162 found:;
3164 if (err)
3166 inform (DECL_SOURCE_LOCATION (decl), "previous declaration here");
3167 return false;
3169 return true;
3172 /* Handle an "abi_tag" attribute; arguments as in
3173 struct attribute_spec.handler. */
3175 static tree
3176 handle_abi_tag_attribute (tree* node, tree name, tree args,
3177 int flags, bool* no_add_attrs)
3179 if (TYPE_P (*node))
3181 if (!TAGGED_TYPE_P (*node))
3183 error ("%qE attribute applied to non-class, non-enum type %qT",
3184 name, *node);
3185 goto fail;
3187 else if (!(flags & (int)ATTR_FLAG_TYPE_IN_PLACE))
3189 error ("%qE attribute applied to %qT after its definition",
3190 name, *node);
3191 goto fail;
3194 tree attributes = TYPE_ATTRIBUTES (*node);
3195 tree decl = TYPE_NAME (*node);
3197 /* Make sure all declarations have the same abi tags. */
3198 if (DECL_SOURCE_LOCATION (decl) != input_location)
3200 if (!check_abi_tag_redeclaration (decl,
3201 lookup_attribute ("abi_tag",
3202 attributes),
3203 args))
3204 goto fail;
3207 else
3209 if (TREE_CODE (*node) != FUNCTION_DECL)
3211 error ("%qE attribute applied to non-function %qD", name, *node);
3212 goto fail;
3214 else if (DECL_LANGUAGE (*node) == lang_c)
3216 error ("%qE attribute applied to extern \"C\" function %qD",
3217 name, *node);
3218 goto fail;
3222 return NULL_TREE;
3224 fail:
3225 *no_add_attrs = true;
3226 return NULL_TREE;
3229 /* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the
3230 thing pointed to by the constant. */
3232 tree
3233 make_ptrmem_cst (tree type, tree member)
3235 tree ptrmem_cst = make_node (PTRMEM_CST);
3236 TREE_TYPE (ptrmem_cst) = type;
3237 PTRMEM_CST_MEMBER (ptrmem_cst) = member;
3238 return ptrmem_cst;
3241 /* Build a variant of TYPE that has the indicated ATTRIBUTES. May
3242 return an existing type if an appropriate type already exists. */
3244 tree
3245 cp_build_type_attribute_variant (tree type, tree attributes)
3247 tree new_type;
3249 new_type = build_type_attribute_variant (type, attributes);
3250 if (TREE_CODE (new_type) == FUNCTION_TYPE
3251 || TREE_CODE (new_type) == METHOD_TYPE)
3252 new_type = build_exception_variant (new_type,
3253 TYPE_RAISES_EXCEPTIONS (type));
3255 /* Making a new main variant of a class type is broken. */
3256 gcc_assert (!CLASS_TYPE_P (type) || new_type == type);
3258 return new_type;
3261 /* Return TRUE if TYPE1 and TYPE2 are identical for type hashing purposes.
3262 Called only after doing all language independent checks. Only
3263 to check TYPE_RAISES_EXCEPTIONS for FUNCTION_TYPE, the rest is already
3264 compared in type_hash_eq. */
3266 bool
3267 cxx_type_hash_eq (const_tree typea, const_tree typeb)
3269 gcc_assert (TREE_CODE (typea) == FUNCTION_TYPE
3270 || TREE_CODE (typea) == METHOD_TYPE);
3272 return comp_except_specs (TYPE_RAISES_EXCEPTIONS (typea),
3273 TYPE_RAISES_EXCEPTIONS (typeb), ce_exact);
3276 /* Apply FUNC to all language-specific sub-trees of TP in a pre-order
3277 traversal. Called from walk_tree. */
3279 tree
3280 cp_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func,
3281 void *data, struct pointer_set_t *pset)
3283 enum tree_code code = TREE_CODE (*tp);
3284 tree result;
3286 #define WALK_SUBTREE(NODE) \
3287 do \
3289 result = cp_walk_tree (&(NODE), func, data, pset); \
3290 if (result) goto out; \
3292 while (0)
3294 /* Not one of the easy cases. We must explicitly go through the
3295 children. */
3296 result = NULL_TREE;
3297 switch (code)
3299 case DEFAULT_ARG:
3300 case TEMPLATE_TEMPLATE_PARM:
3301 case BOUND_TEMPLATE_TEMPLATE_PARM:
3302 case UNBOUND_CLASS_TEMPLATE:
3303 case TEMPLATE_PARM_INDEX:
3304 case TEMPLATE_TYPE_PARM:
3305 case TYPENAME_TYPE:
3306 case TYPEOF_TYPE:
3307 case UNDERLYING_TYPE:
3308 /* None of these have subtrees other than those already walked
3309 above. */
3310 *walk_subtrees_p = 0;
3311 break;
3313 case BASELINK:
3314 WALK_SUBTREE (BASELINK_FUNCTIONS (*tp));
3315 *walk_subtrees_p = 0;
3316 break;
3318 case PTRMEM_CST:
3319 WALK_SUBTREE (TREE_TYPE (*tp));
3320 *walk_subtrees_p = 0;
3321 break;
3323 case TREE_LIST:
3324 WALK_SUBTREE (TREE_PURPOSE (*tp));
3325 break;
3327 case OVERLOAD:
3328 WALK_SUBTREE (OVL_FUNCTION (*tp));
3329 WALK_SUBTREE (OVL_CHAIN (*tp));
3330 *walk_subtrees_p = 0;
3331 break;
3333 case USING_DECL:
3334 WALK_SUBTREE (DECL_NAME (*tp));
3335 WALK_SUBTREE (USING_DECL_SCOPE (*tp));
3336 WALK_SUBTREE (USING_DECL_DECLS (*tp));
3337 *walk_subtrees_p = 0;
3338 break;
3340 case RECORD_TYPE:
3341 if (TYPE_PTRMEMFUNC_P (*tp))
3342 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (*tp));
3343 break;
3345 case TYPE_ARGUMENT_PACK:
3346 case NONTYPE_ARGUMENT_PACK:
3348 tree args = ARGUMENT_PACK_ARGS (*tp);
3349 int i, len = TREE_VEC_LENGTH (args);
3350 for (i = 0; i < len; i++)
3351 WALK_SUBTREE (TREE_VEC_ELT (args, i));
3353 break;
3355 case TYPE_PACK_EXPANSION:
3356 WALK_SUBTREE (TREE_TYPE (*tp));
3357 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp));
3358 *walk_subtrees_p = 0;
3359 break;
3361 case EXPR_PACK_EXPANSION:
3362 WALK_SUBTREE (TREE_OPERAND (*tp, 0));
3363 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp));
3364 *walk_subtrees_p = 0;
3365 break;
3367 case CAST_EXPR:
3368 case REINTERPRET_CAST_EXPR:
3369 case STATIC_CAST_EXPR:
3370 case CONST_CAST_EXPR:
3371 case DYNAMIC_CAST_EXPR:
3372 case IMPLICIT_CONV_EXPR:
3373 if (TREE_TYPE (*tp))
3374 WALK_SUBTREE (TREE_TYPE (*tp));
3377 int i;
3378 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (*tp)); ++i)
3379 WALK_SUBTREE (TREE_OPERAND (*tp, i));
3381 *walk_subtrees_p = 0;
3382 break;
3384 case TRAIT_EXPR:
3385 WALK_SUBTREE (TRAIT_EXPR_TYPE1 (*tp));
3386 WALK_SUBTREE (TRAIT_EXPR_TYPE2 (*tp));
3387 *walk_subtrees_p = 0;
3388 break;
3390 case DECLTYPE_TYPE:
3391 WALK_SUBTREE (DECLTYPE_TYPE_EXPR (*tp));
3392 *walk_subtrees_p = 0;
3393 break;
3396 default:
3397 return NULL_TREE;
3400 /* We didn't find what we were looking for. */
3401 out:
3402 return result;
3404 #undef WALK_SUBTREE
3407 /* Like save_expr, but for C++. */
3409 tree
3410 cp_save_expr (tree expr)
3412 /* There is no reason to create a SAVE_EXPR within a template; if
3413 needed, we can create the SAVE_EXPR when instantiating the
3414 template. Furthermore, the middle-end cannot handle C++-specific
3415 tree codes. */
3416 if (processing_template_decl)
3417 return expr;
3418 return save_expr (expr);
3421 /* Initialize tree.c. */
3423 void
3424 init_tree (void)
3426 list_hash_table = htab_create_ggc (31, list_hash, list_hash_eq, NULL);
3429 /* Returns the kind of special function that DECL (a FUNCTION_DECL)
3430 is. Note that sfk_none is zero, so this function can be used as a
3431 predicate to test whether or not DECL is a special function. */
3433 special_function_kind
3434 special_function_p (const_tree decl)
3436 /* Rather than doing all this stuff with magic names, we should
3437 probably have a field of type `special_function_kind' in
3438 DECL_LANG_SPECIFIC. */
3439 if (DECL_INHERITED_CTOR_BASE (decl))
3440 return sfk_inheriting_constructor;
3441 if (DECL_COPY_CONSTRUCTOR_P (decl))
3442 return sfk_copy_constructor;
3443 if (DECL_MOVE_CONSTRUCTOR_P (decl))
3444 return sfk_move_constructor;
3445 if (DECL_CONSTRUCTOR_P (decl))
3446 return sfk_constructor;
3447 if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
3449 if (copy_fn_p (decl))
3450 return sfk_copy_assignment;
3451 if (move_fn_p (decl))
3452 return sfk_move_assignment;
3454 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
3455 return sfk_destructor;
3456 if (DECL_COMPLETE_DESTRUCTOR_P (decl))
3457 return sfk_complete_destructor;
3458 if (DECL_BASE_DESTRUCTOR_P (decl))
3459 return sfk_base_destructor;
3460 if (DECL_DELETING_DESTRUCTOR_P (decl))
3461 return sfk_deleting_destructor;
3462 if (DECL_CONV_FN_P (decl))
3463 return sfk_conversion;
3465 return sfk_none;
3468 /* Returns nonzero if TYPE is a character type, including wchar_t. */
3471 char_type_p (tree type)
3473 return (same_type_p (type, char_type_node)
3474 || same_type_p (type, unsigned_char_type_node)
3475 || same_type_p (type, signed_char_type_node)
3476 || same_type_p (type, char16_type_node)
3477 || same_type_p (type, char32_type_node)
3478 || same_type_p (type, wchar_type_node));
3481 /* Returns the kind of linkage associated with the indicated DECL. Th
3482 value returned is as specified by the language standard; it is
3483 independent of implementation details regarding template
3484 instantiation, etc. For example, it is possible that a declaration
3485 to which this function assigns external linkage would not show up
3486 as a global symbol when you run `nm' on the resulting object file. */
3488 linkage_kind
3489 decl_linkage (tree decl)
3491 /* This function doesn't attempt to calculate the linkage from first
3492 principles as given in [basic.link]. Instead, it makes use of
3493 the fact that we have already set TREE_PUBLIC appropriately, and
3494 then handles a few special cases. Ideally, we would calculate
3495 linkage first, and then transform that into a concrete
3496 implementation. */
3498 /* Things that don't have names have no linkage. */
3499 if (!DECL_NAME (decl))
3500 return lk_none;
3502 /* Fields have no linkage. */
3503 if (TREE_CODE (decl) == FIELD_DECL)
3504 return lk_none;
3506 /* Things that are TREE_PUBLIC have external linkage. */
3507 if (TREE_PUBLIC (decl))
3508 return lk_external;
3510 if (TREE_CODE (decl) == NAMESPACE_DECL)
3511 return lk_external;
3513 /* Linkage of a CONST_DECL depends on the linkage of the enumeration
3514 type. */
3515 if (TREE_CODE (decl) == CONST_DECL)
3516 return decl_linkage (TYPE_NAME (DECL_CONTEXT (decl)));
3518 /* Some things that are not TREE_PUBLIC have external linkage, too.
3519 For example, on targets that don't have weak symbols, we make all
3520 template instantiations have internal linkage (in the object
3521 file), but the symbols should still be treated as having external
3522 linkage from the point of view of the language. */
3523 if ((TREE_CODE (decl) == FUNCTION_DECL
3524 || TREE_CODE (decl) == VAR_DECL)
3525 && DECL_COMDAT (decl))
3526 return lk_external;
3528 /* Things in local scope do not have linkage, if they don't have
3529 TREE_PUBLIC set. */
3530 if (decl_function_context (decl))
3531 return lk_none;
3533 /* Members of the anonymous namespace also have TREE_PUBLIC unset, but
3534 are considered to have external linkage for language purposes. DECLs
3535 really meant to have internal linkage have DECL_THIS_STATIC set. */
3536 if (TREE_CODE (decl) == TYPE_DECL)
3537 return lk_external;
3538 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3540 if (!DECL_THIS_STATIC (decl))
3541 return lk_external;
3543 /* Static data members and static member functions from classes
3544 in anonymous namespace also don't have TREE_PUBLIC set. */
3545 if (DECL_CLASS_CONTEXT (decl))
3546 return lk_external;
3549 /* Everything else has internal linkage. */
3550 return lk_internal;
3553 /* Returns the storage duration of the object or reference associated with
3554 the indicated DECL, which should be a VAR_DECL or PARM_DECL. */
3556 duration_kind
3557 decl_storage_duration (tree decl)
3559 if (TREE_CODE (decl) == PARM_DECL)
3560 return dk_auto;
3561 if (TREE_CODE (decl) == FUNCTION_DECL)
3562 return dk_static;
3563 gcc_assert (TREE_CODE (decl) == VAR_DECL);
3564 if (!TREE_STATIC (decl)
3565 && !DECL_EXTERNAL (decl))
3566 return dk_auto;
3567 if (DECL_THREAD_LOCAL_P (decl))
3568 return dk_thread;
3569 return dk_static;
3572 /* EXP is an expression that we want to pre-evaluate. Returns (in
3573 *INITP) an expression that will perform the pre-evaluation. The
3574 value returned by this function is a side-effect free expression
3575 equivalent to the pre-evaluated expression. Callers must ensure
3576 that *INITP is evaluated before EXP. */
3578 tree
3579 stabilize_expr (tree exp, tree* initp)
3581 tree init_expr;
3583 if (!TREE_SIDE_EFFECTS (exp))
3584 init_expr = NULL_TREE;
3585 else if (VOID_TYPE_P (TREE_TYPE (exp)))
3587 init_expr = exp;
3588 exp = void_zero_node;
3590 /* There are no expressions with REFERENCE_TYPE, but there can be call
3591 arguments with such a type; just treat it as a pointer. */
3592 else if (TREE_CODE (TREE_TYPE (exp)) == REFERENCE_TYPE
3593 || SCALAR_TYPE_P (TREE_TYPE (exp))
3594 || !lvalue_or_rvalue_with_address_p (exp))
3596 init_expr = get_target_expr (exp);
3597 exp = TARGET_EXPR_SLOT (init_expr);
3599 else
3601 bool xval = !real_lvalue_p (exp);
3602 exp = cp_build_addr_expr (exp, tf_warning_or_error);
3603 init_expr = get_target_expr (exp);
3604 exp = TARGET_EXPR_SLOT (init_expr);
3605 exp = cp_build_indirect_ref (exp, RO_NULL, tf_warning_or_error);
3606 if (xval)
3607 exp = move (exp);
3609 *initp = init_expr;
3611 gcc_assert (!TREE_SIDE_EFFECTS (exp));
3612 return exp;
3615 /* Add NEW_EXPR, an expression whose value we don't care about, after the
3616 similar expression ORIG. */
3618 tree
3619 add_stmt_to_compound (tree orig, tree new_expr)
3621 if (!new_expr || !TREE_SIDE_EFFECTS (new_expr))
3622 return orig;
3623 if (!orig || !TREE_SIDE_EFFECTS (orig))
3624 return new_expr;
3625 return build2 (COMPOUND_EXPR, void_type_node, orig, new_expr);
3628 /* Like stabilize_expr, but for a call whose arguments we want to
3629 pre-evaluate. CALL is modified in place to use the pre-evaluated
3630 arguments, while, upon return, *INITP contains an expression to
3631 compute the arguments. */
3633 void
3634 stabilize_call (tree call, tree *initp)
3636 tree inits = NULL_TREE;
3637 int i;
3638 int nargs = call_expr_nargs (call);
3640 if (call == error_mark_node || processing_template_decl)
3642 *initp = NULL_TREE;
3643 return;
3646 gcc_assert (TREE_CODE (call) == CALL_EXPR);
3648 for (i = 0; i < nargs; i++)
3650 tree init;
3651 CALL_EXPR_ARG (call, i) =
3652 stabilize_expr (CALL_EXPR_ARG (call, i), &init);
3653 inits = add_stmt_to_compound (inits, init);
3656 *initp = inits;
3659 /* Like stabilize_expr, but for an AGGR_INIT_EXPR whose arguments we want
3660 to pre-evaluate. CALL is modified in place to use the pre-evaluated
3661 arguments, while, upon return, *INITP contains an expression to
3662 compute the arguments. */
3664 static void
3665 stabilize_aggr_init (tree call, tree *initp)
3667 tree inits = NULL_TREE;
3668 int i;
3669 int nargs = aggr_init_expr_nargs (call);
3671 if (call == error_mark_node)
3672 return;
3674 gcc_assert (TREE_CODE (call) == AGGR_INIT_EXPR);
3676 for (i = 0; i < nargs; i++)
3678 tree init;
3679 AGGR_INIT_EXPR_ARG (call, i) =
3680 stabilize_expr (AGGR_INIT_EXPR_ARG (call, i), &init);
3681 inits = add_stmt_to_compound (inits, init);
3684 *initp = inits;
3687 /* Like stabilize_expr, but for an initialization.
3689 If the initialization is for an object of class type, this function
3690 takes care not to introduce additional temporaries.
3692 Returns TRUE iff the expression was successfully pre-evaluated,
3693 i.e., if INIT is now side-effect free, except for, possibly, a
3694 single call to a constructor. */
3696 bool
3697 stabilize_init (tree init, tree *initp)
3699 tree t = init;
3701 *initp = NULL_TREE;
3703 if (t == error_mark_node || processing_template_decl)
3704 return true;
3706 if (TREE_CODE (t) == INIT_EXPR)
3707 t = TREE_OPERAND (t, 1);
3708 if (TREE_CODE (t) == TARGET_EXPR)
3709 t = TARGET_EXPR_INITIAL (t);
3711 /* If the RHS can be stabilized without breaking copy elision, stabilize
3712 it. We specifically don't stabilize class prvalues here because that
3713 would mean an extra copy, but they might be stabilized below. */
3714 if (TREE_CODE (init) == INIT_EXPR
3715 && TREE_CODE (t) != CONSTRUCTOR
3716 && TREE_CODE (t) != AGGR_INIT_EXPR
3717 && (SCALAR_TYPE_P (TREE_TYPE (t))
3718 || lvalue_or_rvalue_with_address_p (t)))
3720 TREE_OPERAND (init, 1) = stabilize_expr (t, initp);
3721 return true;
3724 if (TREE_CODE (t) == COMPOUND_EXPR
3725 && TREE_CODE (init) == INIT_EXPR)
3727 tree last = expr_last (t);
3728 /* Handle stabilizing the EMPTY_CLASS_EXPR pattern. */
3729 if (!TREE_SIDE_EFFECTS (last))
3731 *initp = t;
3732 TREE_OPERAND (init, 1) = last;
3733 return true;
3737 if (TREE_CODE (t) == CONSTRUCTOR)
3739 /* Aggregate initialization: stabilize each of the field
3740 initializers. */
3741 unsigned i;
3742 constructor_elt *ce;
3743 bool good = true;
3744 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
3745 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
3747 tree type = TREE_TYPE (ce->value);
3748 tree subinit;
3749 if (TREE_CODE (type) == REFERENCE_TYPE
3750 || SCALAR_TYPE_P (type))
3751 ce->value = stabilize_expr (ce->value, &subinit);
3752 else if (!stabilize_init (ce->value, &subinit))
3753 good = false;
3754 *initp = add_stmt_to_compound (*initp, subinit);
3756 return good;
3759 if (TREE_CODE (t) == CALL_EXPR)
3761 stabilize_call (t, initp);
3762 return true;
3765 if (TREE_CODE (t) == AGGR_INIT_EXPR)
3767 stabilize_aggr_init (t, initp);
3768 return true;
3771 /* The initialization is being performed via a bitwise copy -- and
3772 the item copied may have side effects. */
3773 return !TREE_SIDE_EFFECTS (init);
3776 /* Like "fold", but should be used whenever we might be processing the
3777 body of a template. */
3779 tree
3780 fold_if_not_in_template (tree expr)
3782 /* In the body of a template, there is never any need to call
3783 "fold". We will call fold later when actually instantiating the
3784 template. Integral constant expressions in templates will be
3785 evaluated via fold_non_dependent_expr, as necessary. */
3786 if (processing_template_decl)
3787 return expr;
3789 /* Fold C++ front-end specific tree codes. */
3790 if (TREE_CODE (expr) == UNARY_PLUS_EXPR)
3791 return fold_convert (TREE_TYPE (expr), TREE_OPERAND (expr, 0));
3793 return fold (expr);
3796 /* Returns true if a cast to TYPE may appear in an integral constant
3797 expression. */
3799 bool
3800 cast_valid_in_integral_constant_expression_p (tree type)
3802 return (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3803 || cxx_dialect >= cxx0x
3804 || dependent_type_p (type)
3805 || type == error_mark_node);
3808 /* Return true if we need to fix linkage information of DECL. */
3810 static bool
3811 cp_fix_function_decl_p (tree decl)
3813 /* Skip if DECL is not externally visible. */
3814 if (!TREE_PUBLIC (decl))
3815 return false;
3817 /* We need to fix DECL if it a appears to be exported but with no
3818 function body. Thunks do not have CFGs and we may need to
3819 handle them specially later. */
3820 if (!gimple_has_body_p (decl)
3821 && !DECL_THUNK_P (decl)
3822 && !DECL_EXTERNAL (decl))
3824 struct cgraph_node *node = cgraph_get_node (decl);
3826 /* Don't fix same_body aliases. Although they don't have their own
3827 CFG, they share it with what they alias to. */
3828 if (!node || !node->alias
3829 || !vec_safe_length (node->symbol.ref_list.references))
3830 return true;
3833 return false;
3836 /* Clean the C++ specific parts of the tree T. */
3838 void
3839 cp_free_lang_data (tree t)
3841 if (TREE_CODE (t) == METHOD_TYPE
3842 || TREE_CODE (t) == FUNCTION_TYPE)
3844 /* Default args are not interesting anymore. */
3845 tree argtypes = TYPE_ARG_TYPES (t);
3846 while (argtypes)
3848 TREE_PURPOSE (argtypes) = 0;
3849 argtypes = TREE_CHAIN (argtypes);
3852 else if (TREE_CODE (t) == FUNCTION_DECL
3853 && cp_fix_function_decl_p (t))
3855 /* If T is used in this translation unit at all, the definition
3856 must exist somewhere else since we have decided to not emit it
3857 in this TU. So make it an external reference. */
3858 DECL_EXTERNAL (t) = 1;
3859 TREE_STATIC (t) = 0;
3861 if (TREE_CODE (t) == NAMESPACE_DECL)
3863 /* The list of users of a namespace isn't useful for the middle-end
3864 or debug generators. */
3865 DECL_NAMESPACE_USERS (t) = NULL_TREE;
3866 /* Neither do we need the leftover chaining of namespaces
3867 from the binding level. */
3868 DECL_CHAIN (t) = NULL_TREE;
3872 /* Stub for c-common. Please keep in sync with c-decl.c.
3873 FIXME: If address space support is target specific, then this
3874 should be a C target hook. But currently this is not possible,
3875 because this function is called via REGISTER_TARGET_PRAGMAS. */
3876 void
3877 c_register_addr_space (const char * /*word*/, addr_space_t /*as*/)
3881 /* Return the number of operands in T that we care about for things like
3882 mangling. */
3885 cp_tree_operand_length (const_tree t)
3887 enum tree_code code = TREE_CODE (t);
3889 switch (code)
3891 case PREINCREMENT_EXPR:
3892 case PREDECREMENT_EXPR:
3893 case POSTINCREMENT_EXPR:
3894 case POSTDECREMENT_EXPR:
3895 return 1;
3897 case ARRAY_REF:
3898 return 2;
3900 case EXPR_PACK_EXPANSION:
3901 return 1;
3903 default:
3904 return TREE_OPERAND_LENGTH (t);
3908 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
3909 /* Complain that some language-specific thing hanging off a tree
3910 node has been accessed improperly. */
3912 void
3913 lang_check_failed (const char* file, int line, const char* function)
3915 internal_error ("lang_* check: failed in %s, at %s:%d",
3916 function, trim_filename (file), line);
3918 #endif /* ENABLE_TREE_CHECKING */
3920 #include "gt-cp-tree.h"