Merge trunk version 195164 into gupc branch.
[official-gcc.git] / gcc / cp / tree.c
blob88817d8c60c7cb57bb5a2ffbee6a3e0aa74160c5
1 /* Language-dependent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2013 Free Software Foundation, Inc.
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "cp-tree.h"
27 #include "flags.h"
28 #include "tree-inline.h"
29 #include "debug.h"
30 #include "convert.h"
31 #include "cgraph.h"
32 #include "splay-tree.h"
33 #include "gimple.h" /* gimple_has_body_p */
34 #include "hash-table.h"
36 static tree bot_manip (tree *, int *, void *);
37 static tree bot_replace (tree *, int *, void *);
38 static int list_hash_eq (const void *, const void *);
39 static hashval_t list_hash_pieces (tree, tree, tree);
40 static hashval_t list_hash (const void *);
41 static tree build_target_expr (tree, tree, tsubst_flags_t);
42 static tree count_trees_r (tree *, int *, void *);
43 static tree verify_stmt_tree_r (tree *, int *, void *);
44 static tree build_local_temp (tree);
46 static tree handle_java_interface_attribute (tree *, tree, tree, int, bool *);
47 static tree handle_com_interface_attribute (tree *, tree, tree, int, bool *);
48 static tree handle_init_priority_attribute (tree *, tree, tree, int, bool *);
49 static tree handle_abi_tag_attribute (tree *, tree, tree, int, bool *);
51 /* If REF is an lvalue, returns the kind of lvalue that REF is.
52 Otherwise, returns clk_none. */
54 cp_lvalue_kind
55 lvalue_kind (const_tree ref)
57 cp_lvalue_kind op1_lvalue_kind = clk_none;
58 cp_lvalue_kind op2_lvalue_kind = clk_none;
60 /* Expressions of reference type are sometimes wrapped in
61 INDIRECT_REFs. INDIRECT_REFs are just internal compiler
62 representation, not part of the language, so we have to look
63 through them. */
64 if (REFERENCE_REF_P (ref))
65 return lvalue_kind (TREE_OPERAND (ref, 0));
67 if (TREE_TYPE (ref)
68 && TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
70 /* unnamed rvalue references are rvalues */
71 if (TYPE_REF_IS_RVALUE (TREE_TYPE (ref))
72 && TREE_CODE (ref) != PARM_DECL
73 && TREE_CODE (ref) != VAR_DECL
74 && TREE_CODE (ref) != COMPONENT_REF
75 /* Functions are always lvalues. */
76 && TREE_CODE (TREE_TYPE (TREE_TYPE (ref))) != FUNCTION_TYPE)
77 return clk_rvalueref;
79 /* lvalue references and named rvalue references are lvalues. */
80 return clk_ordinary;
83 if (ref == current_class_ptr)
84 return clk_none;
86 switch (TREE_CODE (ref))
88 case SAVE_EXPR:
89 return clk_none;
90 /* preincrements and predecrements are valid lvals, provided
91 what they refer to are valid lvals. */
92 case PREINCREMENT_EXPR:
93 case PREDECREMENT_EXPR:
94 case TRY_CATCH_EXPR:
95 case WITH_CLEANUP_EXPR:
96 case REALPART_EXPR:
97 case IMAGPART_EXPR:
98 return lvalue_kind (TREE_OPERAND (ref, 0));
100 case COMPONENT_REF:
101 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
102 /* Look at the member designator. */
103 if (!op1_lvalue_kind)
105 else if (is_overloaded_fn (TREE_OPERAND (ref, 1)))
106 /* The "field" can be a FUNCTION_DECL or an OVERLOAD in some
107 situations. If we're seeing a COMPONENT_REF, it's a non-static
108 member, so it isn't an lvalue. */
109 op1_lvalue_kind = clk_none;
110 else if (TREE_CODE (TREE_OPERAND (ref, 1)) != FIELD_DECL)
111 /* This can be IDENTIFIER_NODE in a template. */;
112 else if (DECL_C_BIT_FIELD (TREE_OPERAND (ref, 1)))
114 /* Clear the ordinary bit. If this object was a class
115 rvalue we want to preserve that information. */
116 op1_lvalue_kind &= ~clk_ordinary;
117 /* The lvalue is for a bitfield. */
118 op1_lvalue_kind |= clk_bitfield;
120 else if (DECL_PACKED (TREE_OPERAND (ref, 1)))
121 op1_lvalue_kind |= clk_packed;
123 return op1_lvalue_kind;
125 case STRING_CST:
126 case COMPOUND_LITERAL_EXPR:
127 return clk_ordinary;
129 case CONST_DECL:
130 /* CONST_DECL without TREE_STATIC are enumeration values and
131 thus not lvalues. With TREE_STATIC they are used by ObjC++
132 in objc_build_string_object and need to be considered as
133 lvalues. */
134 if (! TREE_STATIC (ref))
135 return clk_none;
136 case VAR_DECL:
137 if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
138 && DECL_LANG_SPECIFIC (ref)
139 && DECL_IN_AGGR_P (ref))
140 return clk_none;
141 case INDIRECT_REF:
142 case ARROW_EXPR:
143 case ARRAY_REF:
144 case PARM_DECL:
145 case RESULT_DECL:
146 if (TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
147 return clk_ordinary;
148 break;
150 /* A scope ref in a template, left as SCOPE_REF to support later
151 access checking. */
152 case SCOPE_REF:
153 gcc_assert (!type_dependent_expression_p (CONST_CAST_TREE (ref)));
155 tree op = TREE_OPERAND (ref, 1);
156 if (TREE_CODE (op) == FIELD_DECL)
157 return (DECL_C_BIT_FIELD (op) ? clk_bitfield : clk_ordinary);
158 else
159 return lvalue_kind (op);
162 case MAX_EXPR:
163 case MIN_EXPR:
164 /* Disallow <? and >? as lvalues if either argument side-effects. */
165 if (TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 0))
166 || TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 1)))
167 return clk_none;
168 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
169 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1));
170 break;
172 case COND_EXPR:
173 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1)
174 ? TREE_OPERAND (ref, 1)
175 : TREE_OPERAND (ref, 0));
176 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 2));
177 break;
179 case MODIFY_EXPR:
180 case TYPEID_EXPR:
181 return clk_ordinary;
183 case COMPOUND_EXPR:
184 return lvalue_kind (TREE_OPERAND (ref, 1));
186 case TARGET_EXPR:
187 return clk_class;
189 case VA_ARG_EXPR:
190 return (CLASS_TYPE_P (TREE_TYPE (ref)) ? clk_class : clk_none);
192 case CALL_EXPR:
193 /* We can see calls outside of TARGET_EXPR in templates. */
194 if (CLASS_TYPE_P (TREE_TYPE (ref)))
195 return clk_class;
196 return clk_none;
198 case FUNCTION_DECL:
199 /* All functions (except non-static-member functions) are
200 lvalues. */
201 return (DECL_NONSTATIC_MEMBER_FUNCTION_P (ref)
202 ? clk_none : clk_ordinary);
204 case BASELINK:
205 /* We now represent a reference to a single static member function
206 with a BASELINK. */
207 /* This CONST_CAST is okay because BASELINK_FUNCTIONS returns
208 its argument unmodified and we assign it to a const_tree. */
209 return lvalue_kind (BASELINK_FUNCTIONS (CONST_CAST_TREE (ref)));
211 case NON_DEPENDENT_EXPR:
212 /* We just return clk_ordinary for NON_DEPENDENT_EXPR in C++98, but
213 in C++11 lvalues don't bind to rvalue references, so we need to
214 work harder to avoid bogus errors (c++/44870). */
215 if (cxx_dialect < cxx0x)
216 return clk_ordinary;
217 else
218 return lvalue_kind (TREE_OPERAND (ref, 0));
220 default:
221 if (!TREE_TYPE (ref))
222 return clk_none;
223 if (CLASS_TYPE_P (TREE_TYPE (ref)))
224 return clk_class;
225 break;
228 /* If one operand is not an lvalue at all, then this expression is
229 not an lvalue. */
230 if (!op1_lvalue_kind || !op2_lvalue_kind)
231 return clk_none;
233 /* Otherwise, it's an lvalue, and it has all the odd properties
234 contributed by either operand. */
235 op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind;
236 /* It's not an ordinary lvalue if it involves any other kind. */
237 if ((op1_lvalue_kind & ~clk_ordinary) != clk_none)
238 op1_lvalue_kind &= ~clk_ordinary;
239 /* It can't be both a pseudo-lvalue and a non-addressable lvalue.
240 A COND_EXPR of those should be wrapped in a TARGET_EXPR. */
241 if ((op1_lvalue_kind & (clk_rvalueref|clk_class))
242 && (op1_lvalue_kind & (clk_bitfield|clk_packed)))
243 op1_lvalue_kind = clk_none;
244 return op1_lvalue_kind;
247 /* Returns the kind of lvalue that REF is, in the sense of
248 [basic.lval]. This function should really be named lvalue_p; it
249 computes the C++ definition of lvalue. */
251 cp_lvalue_kind
252 real_lvalue_p (const_tree ref)
254 cp_lvalue_kind kind = lvalue_kind (ref);
255 if (kind & (clk_rvalueref|clk_class))
256 return clk_none;
257 else
258 return kind;
261 /* This differs from real_lvalue_p in that class rvalues are considered
262 lvalues. */
264 bool
265 lvalue_p (const_tree ref)
267 return (lvalue_kind (ref) != clk_none);
270 /* This differs from real_lvalue_p in that rvalues formed by dereferencing
271 rvalue references are considered rvalues. */
273 bool
274 lvalue_or_rvalue_with_address_p (const_tree ref)
276 cp_lvalue_kind kind = lvalue_kind (ref);
277 if (kind & clk_class)
278 return false;
279 else
280 return (kind != clk_none);
283 /* Returns true if REF is an xvalue, false otherwise. */
285 bool
286 xvalue_p (const_tree ref)
288 return (lvalue_kind (ref) == clk_rvalueref);
291 /* Test whether DECL is a builtin that may appear in a
292 constant-expression. */
294 bool
295 builtin_valid_in_constant_expr_p (const_tree decl)
297 /* At present BUILT_IN_CONSTANT_P is the only builtin we're allowing
298 in constant-expressions. We may want to add other builtins later. */
299 return DECL_IS_BUILTIN_CONSTANT_P (decl);
302 /* Build a TARGET_EXPR, initializing the DECL with the VALUE. */
304 static tree
305 build_target_expr (tree decl, tree value, tsubst_flags_t complain)
307 tree t;
308 tree type = TREE_TYPE (decl);
310 #ifdef ENABLE_CHECKING
311 gcc_assert (VOID_TYPE_P (TREE_TYPE (value))
312 || TREE_TYPE (decl) == TREE_TYPE (value)
313 /* On ARM ctors return 'this'. */
314 || (TREE_CODE (TREE_TYPE (value)) == POINTER_TYPE
315 && TREE_CODE (value) == CALL_EXPR)
316 || useless_type_conversion_p (TREE_TYPE (decl),
317 TREE_TYPE (value)));
318 #endif
320 t = cxx_maybe_build_cleanup (decl, complain);
321 if (t == error_mark_node)
322 return error_mark_node;
323 t = build4 (TARGET_EXPR, type, decl, value, t, NULL_TREE);
324 /* We always set TREE_SIDE_EFFECTS so that expand_expr does not
325 ignore the TARGET_EXPR. If there really turn out to be no
326 side-effects, then the optimizer should be able to get rid of
327 whatever code is generated anyhow. */
328 TREE_SIDE_EFFECTS (t) = 1;
330 return t;
333 /* Return an undeclared local temporary of type TYPE for use in building a
334 TARGET_EXPR. */
336 static tree
337 build_local_temp (tree type)
339 tree slot = build_decl (input_location,
340 VAR_DECL, NULL_TREE, type);
341 DECL_ARTIFICIAL (slot) = 1;
342 DECL_IGNORED_P (slot) = 1;
343 DECL_CONTEXT (slot) = current_function_decl;
344 layout_decl (slot, 0);
345 return slot;
348 /* Set various status flags when building an AGGR_INIT_EXPR object T. */
350 static void
351 process_aggr_init_operands (tree t)
353 bool side_effects;
355 side_effects = TREE_SIDE_EFFECTS (t);
356 if (!side_effects)
358 int i, n;
359 n = TREE_OPERAND_LENGTH (t);
360 for (i = 1; i < n; i++)
362 tree op = TREE_OPERAND (t, i);
363 if (op && TREE_SIDE_EFFECTS (op))
365 side_effects = 1;
366 break;
370 TREE_SIDE_EFFECTS (t) = side_effects;
373 /* Build an AGGR_INIT_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE,
374 FN, and SLOT. NARGS is the number of call arguments which are specified
375 as a tree array ARGS. */
377 static tree
378 build_aggr_init_array (tree return_type, tree fn, tree slot, int nargs,
379 tree *args)
381 tree t;
382 int i;
384 t = build_vl_exp (AGGR_INIT_EXPR, nargs + 3);
385 TREE_TYPE (t) = return_type;
386 AGGR_INIT_EXPR_FN (t) = fn;
387 AGGR_INIT_EXPR_SLOT (t) = slot;
388 for (i = 0; i < nargs; i++)
389 AGGR_INIT_EXPR_ARG (t, i) = args[i];
390 process_aggr_init_operands (t);
391 return t;
394 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
395 target. TYPE is the type to be initialized.
397 Build an AGGR_INIT_EXPR to represent the initialization. This function
398 differs from build_cplus_new in that an AGGR_INIT_EXPR can only be used
399 to initialize another object, whereas a TARGET_EXPR can either
400 initialize another object or create its own temporary object, and as a
401 result building up a TARGET_EXPR requires that the type's destructor be
402 callable. */
404 tree
405 build_aggr_init_expr (tree type, tree init)
407 tree fn;
408 tree slot;
409 tree rval;
410 int is_ctor;
412 /* Don't build AGGR_INIT_EXPR in a template. */
413 if (processing_template_decl)
414 return init;
416 if (TREE_CODE (init) == CALL_EXPR)
417 fn = CALL_EXPR_FN (init);
418 else if (TREE_CODE (init) == AGGR_INIT_EXPR)
419 fn = AGGR_INIT_EXPR_FN (init);
420 else
421 return convert (type, init);
423 is_ctor = (TREE_CODE (fn) == ADDR_EXPR
424 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
425 && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0)));
427 /* We split the CALL_EXPR into its function and its arguments here.
428 Then, in expand_expr, we put them back together. The reason for
429 this is that this expression might be a default argument
430 expression. In that case, we need a new temporary every time the
431 expression is used. That's what break_out_target_exprs does; it
432 replaces every AGGR_INIT_EXPR with a copy that uses a fresh
433 temporary slot. Then, expand_expr builds up a call-expression
434 using the new slot. */
436 /* If we don't need to use a constructor to create an object of this
437 type, don't mess with AGGR_INIT_EXPR. */
438 if (is_ctor || TREE_ADDRESSABLE (type))
440 slot = build_local_temp (type);
442 if (TREE_CODE(init) == CALL_EXPR)
443 rval = build_aggr_init_array (void_type_node, fn, slot,
444 call_expr_nargs (init),
445 CALL_EXPR_ARGP (init));
446 else
447 rval = build_aggr_init_array (void_type_node, fn, slot,
448 aggr_init_expr_nargs (init),
449 AGGR_INIT_EXPR_ARGP (init));
450 TREE_SIDE_EFFECTS (rval) = 1;
451 AGGR_INIT_VIA_CTOR_P (rval) = is_ctor;
452 TREE_NOTHROW (rval) = TREE_NOTHROW (init);
454 else
455 rval = init;
457 return rval;
460 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
461 target. TYPE is the type that this initialization should appear to
462 have.
464 Build an encapsulation of the initialization to perform
465 and return it so that it can be processed by language-independent
466 and language-specific expression expanders. */
468 tree
469 build_cplus_new (tree type, tree init, tsubst_flags_t complain)
471 tree rval = build_aggr_init_expr (type, init);
472 tree slot;
474 /* Make sure that we're not trying to create an instance of an
475 abstract class. */
476 if (abstract_virtuals_error_sfinae (NULL_TREE, type, complain))
477 return error_mark_node;
479 if (TREE_CODE (rval) == AGGR_INIT_EXPR)
480 slot = AGGR_INIT_EXPR_SLOT (rval);
481 else if (TREE_CODE (rval) == CALL_EXPR
482 || TREE_CODE (rval) == CONSTRUCTOR)
483 slot = build_local_temp (type);
484 else
485 return rval;
487 rval = build_target_expr (slot, rval, complain);
489 if (rval != error_mark_node)
490 TARGET_EXPR_IMPLICIT_P (rval) = 1;
492 return rval;
495 /* Subroutine of build_vec_init_expr: Build up a single element
496 intialization as a proxy for the full array initialization to get things
497 marked as used and any appropriate diagnostics.
499 Since we're deferring building the actual constructor calls until
500 gimplification time, we need to build one now and throw it away so
501 that the relevant constructor gets mark_used before cgraph decides
502 what functions are needed. Here we assume that init is either
503 NULL_TREE, void_type_node (indicating value-initialization), or
504 another array to copy. */
506 static tree
507 build_vec_init_elt (tree type, tree init, tsubst_flags_t complain)
509 tree inner_type = strip_array_types (type);
510 vec<tree, va_gc> *argvec;
512 if (integer_zerop (array_type_nelts_total (type))
513 || !CLASS_TYPE_P (inner_type))
514 /* No interesting initialization to do. */
515 return integer_zero_node;
516 else if (init == void_type_node)
517 return build_value_init (inner_type, complain);
519 gcc_assert (init == NULL_TREE
520 || (same_type_ignoring_top_level_qualifiers_p
521 (type, TREE_TYPE (init))));
523 argvec = make_tree_vector ();
524 if (init)
526 tree init_type = strip_array_types (TREE_TYPE (init));
527 tree dummy = build_dummy_object (init_type);
528 if (!real_lvalue_p (init))
529 dummy = move (dummy);
530 argvec->quick_push (dummy);
532 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
533 &argvec, inner_type, LOOKUP_NORMAL,
534 complain);
535 release_tree_vector (argvec);
537 /* For a trivial constructor, build_over_call creates a TARGET_EXPR. But
538 we don't want one here because we aren't creating a temporary. */
539 if (TREE_CODE (init) == TARGET_EXPR)
540 init = TARGET_EXPR_INITIAL (init);
542 return init;
545 /* Return a TARGET_EXPR which expresses the initialization of an array to
546 be named later, either default-initialization or copy-initialization
547 from another array of the same type. */
549 tree
550 build_vec_init_expr (tree type, tree init, tsubst_flags_t complain)
552 tree slot;
553 bool value_init = false;
554 tree elt_init = build_vec_init_elt (type, init, complain);
556 if (init == void_type_node)
558 value_init = true;
559 init = NULL_TREE;
562 slot = build_local_temp (type);
563 init = build2 (VEC_INIT_EXPR, type, slot, init);
564 TREE_SIDE_EFFECTS (init) = true;
565 SET_EXPR_LOCATION (init, input_location);
567 if (cxx_dialect >= cxx0x
568 && potential_constant_expression (elt_init))
569 VEC_INIT_EXPR_IS_CONSTEXPR (init) = true;
570 VEC_INIT_EXPR_VALUE_INIT (init) = value_init;
572 return init;
575 /* Give a helpful diagnostic for a non-constexpr VEC_INIT_EXPR in a context
576 that requires a constant expression. */
578 void
579 diagnose_non_constexpr_vec_init (tree expr)
581 tree type = TREE_TYPE (VEC_INIT_EXPR_SLOT (expr));
582 tree init, elt_init;
583 if (VEC_INIT_EXPR_VALUE_INIT (expr))
584 init = void_type_node;
585 else
586 init = VEC_INIT_EXPR_INIT (expr);
588 elt_init = build_vec_init_elt (type, init, tf_warning_or_error);
589 require_potential_constant_expression (elt_init);
592 tree
593 build_array_copy (tree init)
595 return build_vec_init_expr (TREE_TYPE (init), init, tf_warning_or_error);
598 /* Build a TARGET_EXPR using INIT to initialize a new temporary of the
599 indicated TYPE. */
601 tree
602 build_target_expr_with_type (tree init, tree type, tsubst_flags_t complain)
604 gcc_assert (!VOID_TYPE_P (type));
606 if (TREE_CODE (init) == TARGET_EXPR
607 || init == error_mark_node)
608 return init;
609 else if (CLASS_TYPE_P (type) && type_has_nontrivial_copy_init (type)
610 && !VOID_TYPE_P (TREE_TYPE (init))
611 && TREE_CODE (init) != COND_EXPR
612 && TREE_CODE (init) != CONSTRUCTOR
613 && TREE_CODE (init) != VA_ARG_EXPR)
614 /* We need to build up a copy constructor call. A void initializer
615 means we're being called from bot_manip. COND_EXPR is a special
616 case because we already have copies on the arms and we don't want
617 another one here. A CONSTRUCTOR is aggregate initialization, which
618 is handled separately. A VA_ARG_EXPR is magic creation of an
619 aggregate; there's no additional work to be done. */
620 return force_rvalue (init, complain);
622 return force_target_expr (type, init, complain);
625 /* Like the above function, but without the checking. This function should
626 only be used by code which is deliberately trying to subvert the type
627 system, such as call_builtin_trap. Or build_over_call, to avoid
628 infinite recursion. */
630 tree
631 force_target_expr (tree type, tree init, tsubst_flags_t complain)
633 tree slot;
635 gcc_assert (!VOID_TYPE_P (type));
637 slot = build_local_temp (type);
638 return build_target_expr (slot, init, complain);
641 /* Like build_target_expr_with_type, but use the type of INIT. */
643 tree
644 get_target_expr_sfinae (tree init, tsubst_flags_t complain)
646 if (TREE_CODE (init) == AGGR_INIT_EXPR)
647 return build_target_expr (AGGR_INIT_EXPR_SLOT (init), init, complain);
648 else if (TREE_CODE (init) == VEC_INIT_EXPR)
649 return build_target_expr (VEC_INIT_EXPR_SLOT (init), init, complain);
650 else
651 return build_target_expr_with_type (init, TREE_TYPE (init), complain);
654 tree
655 get_target_expr (tree init)
657 return get_target_expr_sfinae (init, tf_warning_or_error);
660 /* If EXPR is a bitfield reference, convert it to the declared type of
661 the bitfield, and return the resulting expression. Otherwise,
662 return EXPR itself. */
664 tree
665 convert_bitfield_to_declared_type (tree expr)
667 tree bitfield_type;
669 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
670 if (bitfield_type)
671 expr = convert_to_integer (TYPE_MAIN_VARIANT (bitfield_type),
672 expr);
673 return expr;
676 /* EXPR is being used in an rvalue context. Return a version of EXPR
677 that is marked as an rvalue. */
679 tree
680 rvalue (tree expr)
682 tree type;
684 if (error_operand_p (expr))
685 return expr;
687 expr = mark_rvalue_use (expr);
689 /* [basic.lval]
691 Non-class rvalues always have cv-unqualified types. */
692 type = TREE_TYPE (expr);
693 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
694 type = cv_unqualified (type);
696 /* We need to do this for rvalue refs as well to get the right answer
697 from decltype; see c++/36628. */
698 if (!processing_template_decl && lvalue_or_rvalue_with_address_p (expr))
699 expr = build1 (NON_LVALUE_EXPR, type, expr);
700 else if (type != TREE_TYPE (expr))
701 expr = build_nop (type, expr);
703 return expr;
707 /* Hash an ARRAY_TYPE. K is really of type `tree'. */
709 static hashval_t
710 cplus_array_hash (const void* k)
712 hashval_t hash;
713 const_tree const t = (const_tree) k;
715 hash = TYPE_UID (TREE_TYPE (t));
716 if (TYPE_DOMAIN (t))
717 hash ^= TYPE_UID (TYPE_DOMAIN (t));
718 return hash;
721 typedef struct cplus_array_info {
722 tree type;
723 tree domain;
724 } cplus_array_info;
726 /* Compare two ARRAY_TYPEs. K1 is really of type `tree', K2 is really
727 of type `cplus_array_info*'. */
729 static int
730 cplus_array_compare (const void * k1, const void * k2)
732 const_tree const t1 = (const_tree) k1;
733 const cplus_array_info *const t2 = (const cplus_array_info*) k2;
735 return (TREE_TYPE (t1) == t2->type && TYPE_DOMAIN (t1) == t2->domain);
738 /* Hash table containing dependent array types, which are unsuitable for
739 the language-independent type hash table. */
740 static GTY ((param_is (union tree_node))) htab_t cplus_array_htab;
742 /* Like build_array_type, but handle special C++ semantics. */
744 tree
745 build_cplus_array_type (tree elt_type, tree index_type)
747 tree t;
749 if (elt_type == error_mark_node || index_type == error_mark_node)
750 return error_mark_node;
752 if (processing_template_decl
753 && (dependent_type_p (elt_type)
754 || (index_type && !TREE_CONSTANT (TYPE_MAX_VALUE (index_type)))))
756 void **e;
757 cplus_array_info cai;
758 hashval_t hash;
760 if (cplus_array_htab == NULL)
761 cplus_array_htab = htab_create_ggc (61, &cplus_array_hash,
762 &cplus_array_compare, NULL);
764 hash = TYPE_UID (elt_type);
765 if (index_type)
766 hash ^= TYPE_UID (index_type);
767 cai.type = elt_type;
768 cai.domain = index_type;
770 e = htab_find_slot_with_hash (cplus_array_htab, &cai, hash, INSERT);
771 if (*e)
772 /* We have found the type: we're done. */
773 return (tree) *e;
774 else
776 /* Build a new array type. */
777 t = cxx_make_type (ARRAY_TYPE);
778 TREE_TYPE (t) = elt_type;
779 TYPE_DOMAIN (t) = index_type;
781 /* Store it in the hash table. */
782 *e = t;
784 /* Set the canonical type for this new node. */
785 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
786 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
787 SET_TYPE_STRUCTURAL_EQUALITY (t);
788 else if (TYPE_CANONICAL (elt_type) != elt_type
789 || (index_type
790 && TYPE_CANONICAL (index_type) != index_type))
791 TYPE_CANONICAL (t)
792 = build_cplus_array_type
793 (TYPE_CANONICAL (elt_type),
794 index_type ? TYPE_CANONICAL (index_type) : index_type);
795 else
796 TYPE_CANONICAL (t) = t;
799 else
801 if (!TYPE_STRUCTURAL_EQUALITY_P (elt_type)
802 && !(index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type))
803 && (TYPE_CANONICAL (elt_type) != elt_type
804 || (index_type && TYPE_CANONICAL (index_type) != index_type)))
805 /* Make sure that the canonical type is on the appropriate
806 variants list. */
807 build_cplus_array_type
808 (TYPE_CANONICAL (elt_type),
809 index_type ? TYPE_CANONICAL (index_type) : index_type);
810 t = build_array_type (elt_type, index_type);
813 /* Push these needs up so that initialization takes place
814 more easily. */
815 bool needs_ctor
816 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (elt_type));
817 TYPE_NEEDS_CONSTRUCTING (t) = needs_ctor;
818 bool needs_dtor
819 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (elt_type));
820 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = needs_dtor;
822 /* We want TYPE_MAIN_VARIANT of an array to strip cv-quals from the
823 element type as well, so fix it up if needed. */
824 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
826 tree m = build_cplus_array_type (TYPE_MAIN_VARIANT (elt_type),
827 index_type);
829 if (TYPE_MAIN_VARIANT (t) != m)
831 if (COMPLETE_TYPE_P (t) && !COMPLETE_TYPE_P (m))
833 /* m was built before the element type was complete, so we
834 also need to copy the layout info from t. */
835 tree size = TYPE_SIZE (t);
836 tree size_unit = TYPE_SIZE_UNIT (t);
837 unsigned int align = TYPE_ALIGN (t);
838 unsigned int user_align = TYPE_USER_ALIGN (t);
839 enum machine_mode mode = TYPE_MODE (t);
840 for (tree var = m; var; var = TYPE_NEXT_VARIANT (var))
842 TYPE_SIZE (var) = size;
843 TYPE_SIZE_UNIT (var) = size_unit;
844 TYPE_ALIGN (var) = align;
845 TYPE_USER_ALIGN (var) = user_align;
846 SET_TYPE_MODE (var, mode);
847 TYPE_NEEDS_CONSTRUCTING (var) = needs_ctor;
848 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (var) = needs_dtor;
852 TYPE_MAIN_VARIANT (t) = m;
853 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
854 TYPE_NEXT_VARIANT (m) = t;
858 /* Avoid spurious warnings with VLAs (c++/54583). */
859 if (TYPE_SIZE (t) && EXPR_P (TYPE_SIZE (t)))
860 TREE_NO_WARNING (TYPE_SIZE (t)) = 1;
862 return t;
865 /* Return an ARRAY_TYPE with element type ELT and length N. */
867 tree
868 build_array_of_n_type (tree elt, int n)
870 return build_cplus_array_type (elt, build_index_type (size_int (n - 1)));
873 /* Return a reference type node referring to TO_TYPE. If RVAL is
874 true, return an rvalue reference type, otherwise return an lvalue
875 reference type. If a type node exists, reuse it, otherwise create
876 a new one. */
877 tree
878 cp_build_reference_type (tree to_type, bool rval)
880 tree lvalue_ref, t;
881 lvalue_ref = build_reference_type (to_type);
882 if (!rval)
883 return lvalue_ref;
885 /* This code to create rvalue reference types is based on and tied
886 to the code creating lvalue reference types in the middle-end
887 functions build_reference_type_for_mode and build_reference_type.
889 It works by putting the rvalue reference type nodes after the
890 lvalue reference nodes in the TYPE_NEXT_REF_TO linked list, so
891 they will effectively be ignored by the middle end. */
893 for (t = lvalue_ref; (t = TYPE_NEXT_REF_TO (t)); )
894 if (TYPE_REF_IS_RVALUE (t))
895 return t;
897 t = build_distinct_type_copy (lvalue_ref);
899 TYPE_REF_IS_RVALUE (t) = true;
900 TYPE_NEXT_REF_TO (t) = TYPE_NEXT_REF_TO (lvalue_ref);
901 TYPE_NEXT_REF_TO (lvalue_ref) = t;
903 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
904 SET_TYPE_STRUCTURAL_EQUALITY (t);
905 else if (TYPE_CANONICAL (to_type) != to_type)
906 TYPE_CANONICAL (t)
907 = cp_build_reference_type (TYPE_CANONICAL (to_type), rval);
908 else
909 TYPE_CANONICAL (t) = t;
911 layout_type (t);
913 return t;
917 /* Returns EXPR cast to rvalue reference type, like std::move. */
919 tree
920 move (tree expr)
922 tree type = TREE_TYPE (expr);
923 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
924 type = cp_build_reference_type (type, /*rval*/true);
925 return build_static_cast (type, expr, tf_warning_or_error);
928 /* Used by the C++ front end to build qualified array types. However,
929 the C version of this function does not properly maintain canonical
930 types (which are not used in C). */
931 tree
932 c_build_qualified_type_1 (tree type, int type_quals, tree ARG_UNUSED(layout_qualiefier))
934 return cp_build_qualified_type (type, type_quals);
938 /* Make a variant of TYPE, qualified with the TYPE_QUALS. Handles
939 arrays correctly. In particular, if TYPE is an array of T's, and
940 TYPE_QUALS is non-empty, returns an array of qualified T's.
942 FLAGS determines how to deal with ill-formed qualifications. If
943 tf_ignore_bad_quals is set, then bad qualifications are dropped
944 (this is permitted if TYPE was introduced via a typedef or template
945 type parameter). If bad qualifications are dropped and tf_warning
946 is set, then a warning is issued for non-const qualifications. If
947 tf_ignore_bad_quals is not set and tf_error is not set, we
948 return error_mark_node. Otherwise, we issue an error, and ignore
949 the qualifications.
951 Qualification of a reference type is valid when the reference came
952 via a typedef or template type argument. [dcl.ref] No such
953 dispensation is provided for qualifying a function type. [dcl.fct]
954 DR 295 queries this and the proposed resolution brings it into line
955 with qualifying a reference. We implement the DR. We also behave
956 in a similar manner for restricting non-pointer types. */
958 tree
959 cp_build_qualified_type_real (tree type,
960 int type_quals,
961 tsubst_flags_t complain)
963 tree result;
964 int bad_quals = TYPE_UNQUALIFIED;
966 if (type == error_mark_node)
967 return type;
969 if (type_quals == cp_type_quals (type))
970 return type;
972 if (TREE_CODE (type) == ARRAY_TYPE)
974 /* In C++, the qualification really applies to the array element
975 type. Obtain the appropriately qualified element type. */
976 tree t;
977 tree element_type
978 = cp_build_qualified_type_real (TREE_TYPE (type),
979 type_quals,
980 complain);
982 if (element_type == error_mark_node)
983 return error_mark_node;
985 /* See if we already have an identically qualified type. Tests
986 should be equivalent to those in check_qualified_type. */
987 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
988 if (TREE_TYPE (t) == element_type
989 && TYPE_NAME (t) == TYPE_NAME (type)
990 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
991 && attribute_list_equal (TYPE_ATTRIBUTES (t),
992 TYPE_ATTRIBUTES (type)))
993 break;
995 if (!t)
997 t = build_cplus_array_type (element_type, TYPE_DOMAIN (type));
999 /* Keep the typedef name. */
1000 if (TYPE_NAME (t) != TYPE_NAME (type))
1002 t = build_variant_type_copy (t);
1003 TYPE_NAME (t) = TYPE_NAME (type);
1007 /* Even if we already had this variant, we update
1008 TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case
1009 they changed since the variant was originally created.
1011 This seems hokey; if there is some way to use a previous
1012 variant *without* coming through here,
1013 TYPE_NEEDS_CONSTRUCTING will never be updated. */
1014 TYPE_NEEDS_CONSTRUCTING (t)
1015 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
1016 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1017 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
1018 return t;
1020 else if (TYPE_PTRMEMFUNC_P (type))
1022 /* For a pointer-to-member type, we can't just return a
1023 cv-qualified version of the RECORD_TYPE. If we do, we
1024 haven't changed the field that contains the actual pointer to
1025 a method, and so TYPE_PTRMEMFUNC_FN_TYPE will be wrong. */
1026 tree t;
1028 t = TYPE_PTRMEMFUNC_FN_TYPE (type);
1029 t = cp_build_qualified_type_real (t, type_quals, complain);
1030 return build_ptrmemfunc_type (t);
1032 else if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
1034 tree t = PACK_EXPANSION_PATTERN (type);
1036 t = cp_build_qualified_type_real (t, type_quals, complain);
1037 return make_pack_expansion (t);
1040 /* A reference or method type shall not be cv-qualified.
1041 [dcl.ref], [dcl.fct]. This used to be an error, but as of DR 295
1042 (in CD1) we always ignore extra cv-quals on functions. */
1043 if (type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)
1044 && (TREE_CODE (type) == REFERENCE_TYPE
1045 || TREE_CODE (type) == FUNCTION_TYPE
1046 || TREE_CODE (type) == METHOD_TYPE))
1048 if (TREE_CODE (type) == REFERENCE_TYPE)
1049 bad_quals |= type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1050 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1053 /* But preserve any function-cv-quals on a FUNCTION_TYPE. */
1054 if (TREE_CODE (type) == FUNCTION_TYPE)
1055 type_quals |= type_memfn_quals (type);
1057 /* A restrict-qualified type must be a pointer (or reference)
1058 to object or incomplete type. */
1059 if ((type_quals & TYPE_QUAL_RESTRICT)
1060 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1061 && TREE_CODE (type) != TYPENAME_TYPE
1062 && !POINTER_TYPE_P (type))
1064 bad_quals |= TYPE_QUAL_RESTRICT;
1065 type_quals &= ~TYPE_QUAL_RESTRICT;
1068 if (bad_quals == TYPE_UNQUALIFIED
1069 || (complain & tf_ignore_bad_quals))
1070 /*OK*/;
1071 else if (!(complain & tf_error))
1072 return error_mark_node;
1073 else
1075 tree bad_type = build_qualified_type (ptr_type_node, bad_quals);
1076 error ("%qV qualifiers cannot be applied to %qT",
1077 bad_type, type);
1080 /* Retrieve (or create) the appropriately qualified variant. */
1081 result = build_qualified_type (type, type_quals);
1083 /* If this was a pointer-to-method type, and we just made a copy,
1084 then we need to unshare the record that holds the cached
1085 pointer-to-member-function type, because these will be distinct
1086 between the unqualified and qualified types. */
1087 if (result != type
1088 && TREE_CODE (type) == POINTER_TYPE
1089 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE
1090 && TYPE_LANG_SPECIFIC (result) == TYPE_LANG_SPECIFIC (type))
1091 TYPE_LANG_SPECIFIC (result) = NULL;
1093 /* We may also have ended up building a new copy of the canonical
1094 type of a pointer-to-method type, which could have the same
1095 sharing problem described above. */
1096 if (TYPE_CANONICAL (result) != TYPE_CANONICAL (type)
1097 && TREE_CODE (type) == POINTER_TYPE
1098 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE
1099 && (TYPE_LANG_SPECIFIC (TYPE_CANONICAL (result))
1100 == TYPE_LANG_SPECIFIC (TYPE_CANONICAL (type))))
1101 TYPE_LANG_SPECIFIC (TYPE_CANONICAL (result)) = NULL;
1103 return result;
1106 /* Return TYPE with const and volatile removed. */
1108 tree
1109 cv_unqualified (tree type)
1111 int quals;
1113 if (type == error_mark_node)
1114 return type;
1116 quals = cp_type_quals (type);
1117 quals &= ~(TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE);
1118 return cp_build_qualified_type (type, quals);
1121 /* Builds a qualified variant of T that is not a typedef variant.
1122 E.g. consider the following declarations:
1123 typedef const int ConstInt;
1124 typedef ConstInt* PtrConstInt;
1125 If T is PtrConstInt, this function returns a type representing
1126 const int*.
1127 In other words, if T is a typedef, the function returns the underlying type.
1128 The cv-qualification and attributes of the type returned match the
1129 input type.
1130 They will always be compatible types.
1131 The returned type is built so that all of its subtypes
1132 recursively have their typedefs stripped as well.
1134 This is different from just returning TYPE_CANONICAL (T)
1135 Because of several reasons:
1136 * If T is a type that needs structural equality
1137 its TYPE_CANONICAL (T) will be NULL.
1138 * TYPE_CANONICAL (T) desn't carry type attributes
1139 and loses template parameter names. */
1141 tree
1142 strip_typedefs (tree t)
1144 tree result = NULL, type = NULL, t0 = NULL;
1146 if (!t || t == error_mark_node || t == TYPE_CANONICAL (t))
1147 return t;
1149 gcc_assert (TYPE_P (t));
1151 switch (TREE_CODE (t))
1153 case POINTER_TYPE:
1154 type = strip_typedefs (TREE_TYPE (t));
1155 result = build_pointer_type (type);
1156 break;
1157 case REFERENCE_TYPE:
1158 type = strip_typedefs (TREE_TYPE (t));
1159 result = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
1160 break;
1161 case OFFSET_TYPE:
1162 t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t));
1163 type = strip_typedefs (TREE_TYPE (t));
1164 result = build_offset_type (t0, type);
1165 break;
1166 case RECORD_TYPE:
1167 if (TYPE_PTRMEMFUNC_P (t))
1169 t0 = strip_typedefs (TYPE_PTRMEMFUNC_FN_TYPE (t));
1170 result = build_ptrmemfunc_type (t0);
1172 break;
1173 case ARRAY_TYPE:
1174 type = strip_typedefs (TREE_TYPE (t));
1175 t0 = strip_typedefs (TYPE_DOMAIN (t));;
1176 result = build_cplus_array_type (type, t0);
1177 break;
1178 case FUNCTION_TYPE:
1179 case METHOD_TYPE:
1181 tree arg_types = NULL, arg_node, arg_type;
1182 for (arg_node = TYPE_ARG_TYPES (t);
1183 arg_node;
1184 arg_node = TREE_CHAIN (arg_node))
1186 if (arg_node == void_list_node)
1187 break;
1188 arg_type = strip_typedefs (TREE_VALUE (arg_node));
1189 gcc_assert (arg_type);
1191 arg_types =
1192 tree_cons (TREE_PURPOSE (arg_node), arg_type, arg_types);
1195 if (arg_types)
1196 arg_types = nreverse (arg_types);
1198 /* A list of parameters not ending with an ellipsis
1199 must end with void_list_node. */
1200 if (arg_node)
1201 arg_types = chainon (arg_types, void_list_node);
1203 type = strip_typedefs (TREE_TYPE (t));
1204 if (TREE_CODE (t) == METHOD_TYPE)
1206 tree class_type = TREE_TYPE (TREE_VALUE (arg_types));
1207 gcc_assert (class_type);
1208 result =
1209 build_method_type_directly (class_type, type,
1210 TREE_CHAIN (arg_types));
1212 else
1214 result = build_function_type (type,
1215 arg_types);
1216 result = apply_memfn_quals (result, type_memfn_quals (t));
1219 if (TYPE_RAISES_EXCEPTIONS (t))
1220 result = build_exception_variant (result,
1221 TYPE_RAISES_EXCEPTIONS (t));
1223 break;
1224 case TYPENAME_TYPE:
1225 result = make_typename_type (strip_typedefs (TYPE_CONTEXT (t)),
1226 TYPENAME_TYPE_FULLNAME (t),
1227 typename_type, tf_none);
1228 break;
1229 case DECLTYPE_TYPE:
1230 result = strip_typedefs_expr (DECLTYPE_TYPE_EXPR (t));
1231 if (result == DECLTYPE_TYPE_EXPR (t))
1232 return t;
1233 else
1234 result = (finish_decltype_type
1235 (result,
1236 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t),
1237 tf_none));
1238 break;
1239 default:
1240 break;
1243 if (!result)
1244 result = TYPE_MAIN_VARIANT (t);
1245 if (TYPE_USER_ALIGN (t) != TYPE_USER_ALIGN (result)
1246 || TYPE_ALIGN (t) != TYPE_ALIGN (result))
1248 gcc_assert (TYPE_USER_ALIGN (t));
1249 if (TYPE_ALIGN (t) == TYPE_ALIGN (result))
1250 result = build_variant_type_copy (result);
1251 else
1252 result = build_aligned_type (result, TYPE_ALIGN (t));
1253 TYPE_USER_ALIGN (result) = true;
1255 if (TYPE_ATTRIBUTES (t))
1256 result = cp_build_type_attribute_variant (result, TYPE_ATTRIBUTES (t));
1257 return cp_build_qualified_type (result, cp_type_quals (t));
1260 /* Like strip_typedefs above, but works on expressions, so that in
1262 template<class T> struct A
1264 typedef T TT;
1265 B<sizeof(TT)> b;
1268 sizeof(TT) is replaced by sizeof(T). */
1270 tree
1271 strip_typedefs_expr (tree t)
1273 unsigned i,n;
1274 tree r, type, *ops;
1275 enum tree_code code;
1277 if (t == NULL_TREE || t == error_mark_node)
1278 return t;
1280 if (DECL_P (t) || CONSTANT_CLASS_P (t))
1281 return t;
1283 /* Some expressions have type operands, so let's handle types here rather
1284 than check TYPE_P in multiple places below. */
1285 if (TYPE_P (t))
1286 return strip_typedefs (t);
1288 code = TREE_CODE (t);
1289 switch (code)
1291 case IDENTIFIER_NODE:
1292 case TEMPLATE_PARM_INDEX:
1293 case OVERLOAD:
1294 case BASELINK:
1295 case ARGUMENT_PACK_SELECT:
1296 return t;
1298 case TRAIT_EXPR:
1300 tree type1 = strip_typedefs (TRAIT_EXPR_TYPE1 (t));
1301 tree type2 = strip_typedefs (TRAIT_EXPR_TYPE2 (t));
1302 if (type1 == TRAIT_EXPR_TYPE1 (t)
1303 && type2 == TRAIT_EXPR_TYPE2 (t))
1304 return t;
1305 r = copy_node (t);
1306 TRAIT_EXPR_TYPE1 (t) = type1;
1307 TRAIT_EXPR_TYPE2 (t) = type2;
1308 return r;
1311 case TREE_LIST:
1313 vec<tree, va_gc> *vec = make_tree_vector ();
1314 bool changed = false;
1315 tree it;
1316 for (it = t; it; it = TREE_CHAIN (it))
1318 tree val = strip_typedefs_expr (TREE_VALUE (t));
1319 vec_safe_push (vec, val);
1320 if (val != TREE_VALUE (t))
1321 changed = true;
1322 gcc_assert (TREE_PURPOSE (it) == NULL_TREE);
1324 if (changed)
1326 r = NULL_TREE;
1327 FOR_EACH_VEC_ELT_REVERSE (*vec, i, it)
1328 r = tree_cons (NULL_TREE, it, r);
1330 else
1331 r = t;
1332 release_tree_vector (vec);
1333 return r;
1336 case TREE_VEC:
1338 bool changed = false;
1339 vec<tree, va_gc> *vec = make_tree_vector ();
1340 n = TREE_VEC_LENGTH (t);
1341 vec_safe_reserve (vec, n);
1342 for (i = 0; i < n; ++i)
1344 tree op = strip_typedefs_expr (TREE_VEC_ELT (t, i));
1345 vec->quick_push (op);
1346 if (op != TREE_VEC_ELT (t, i))
1347 changed = true;
1349 if (changed)
1351 r = copy_node (t);
1352 for (i = 0; i < n; ++i)
1353 TREE_VEC_ELT (r, i) = (*vec)[i];
1355 else
1356 r = t;
1357 release_tree_vector (vec);
1358 return r;
1361 case CONSTRUCTOR:
1363 bool changed = false;
1364 vec<constructor_elt, va_gc> *vec
1365 = vec_safe_copy (CONSTRUCTOR_ELTS (t));
1366 n = CONSTRUCTOR_NELTS (t);
1367 type = strip_typedefs (TREE_TYPE (t));
1368 for (i = 0; i < n; ++i)
1370 constructor_elt *e = &(*vec)[i];
1371 tree op = strip_typedefs_expr (e->value);
1372 if (op != e->value)
1374 changed = true;
1375 e->value = op;
1377 gcc_checking_assert (e->index == strip_typedefs_expr (e->index));
1380 if (!changed && type == TREE_TYPE (t))
1382 vec_free (vec);
1383 return t;
1385 else
1387 r = copy_node (t);
1388 TREE_TYPE (r) = type;
1389 CONSTRUCTOR_ELTS (r) = vec;
1390 return r;
1394 case LAMBDA_EXPR:
1395 gcc_unreachable ();
1397 default:
1398 break;
1401 gcc_assert (EXPR_P (t));
1403 n = TREE_OPERAND_LENGTH (t);
1404 ops = XALLOCAVEC (tree, n);
1405 type = TREE_TYPE (t);
1407 switch (code)
1409 CASE_CONVERT:
1410 case IMPLICIT_CONV_EXPR:
1411 case DYNAMIC_CAST_EXPR:
1412 case STATIC_CAST_EXPR:
1413 case CONST_CAST_EXPR:
1414 case REINTERPRET_CAST_EXPR:
1415 case CAST_EXPR:
1416 case NEW_EXPR:
1417 type = strip_typedefs (type);
1418 /* fallthrough */
1420 default:
1421 for (i = 0; i < n; ++i)
1422 ops[i] = strip_typedefs_expr (TREE_OPERAND (t, i));
1423 break;
1426 /* If nothing changed, return t. */
1427 for (i = 0; i < n; ++i)
1428 if (ops[i] != TREE_OPERAND (t, i))
1429 break;
1430 if (i == n && type == TREE_TYPE (t))
1431 return t;
1433 r = copy_node (t);
1434 TREE_TYPE (r) = type;
1435 for (i = 0; i < n; ++i)
1436 TREE_OPERAND (r, i) = ops[i];
1437 return r;
1440 /* Makes a copy of BINFO and TYPE, which is to be inherited into a
1441 graph dominated by T. If BINFO is NULL, TYPE is a dependent base,
1442 and we do a shallow copy. If BINFO is non-NULL, we do a deep copy.
1443 VIRT indicates whether TYPE is inherited virtually or not.
1444 IGO_PREV points at the previous binfo of the inheritance graph
1445 order chain. The newly copied binfo's TREE_CHAIN forms this
1446 ordering.
1448 The CLASSTYPE_VBASECLASSES vector of T is constructed in the
1449 correct order. That is in the order the bases themselves should be
1450 constructed in.
1452 The BINFO_INHERITANCE of a virtual base class points to the binfo
1453 of the most derived type. ??? We could probably change this so that
1454 BINFO_INHERITANCE becomes synonymous with BINFO_PRIMARY, and hence
1455 remove a field. They currently can only differ for primary virtual
1456 virtual bases. */
1458 tree
1459 copy_binfo (tree binfo, tree type, tree t, tree *igo_prev, int virt)
1461 tree new_binfo;
1463 if (virt)
1465 /* See if we've already made this virtual base. */
1466 new_binfo = binfo_for_vbase (type, t);
1467 if (new_binfo)
1468 return new_binfo;
1471 new_binfo = make_tree_binfo (binfo ? BINFO_N_BASE_BINFOS (binfo) : 0);
1472 BINFO_TYPE (new_binfo) = type;
1474 /* Chain it into the inheritance graph. */
1475 TREE_CHAIN (*igo_prev) = new_binfo;
1476 *igo_prev = new_binfo;
1478 if (binfo && !BINFO_DEPENDENT_BASE_P (binfo))
1480 int ix;
1481 tree base_binfo;
1483 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), type));
1485 BINFO_OFFSET (new_binfo) = BINFO_OFFSET (binfo);
1486 BINFO_VIRTUALS (new_binfo) = BINFO_VIRTUALS (binfo);
1488 /* We do not need to copy the accesses, as they are read only. */
1489 BINFO_BASE_ACCESSES (new_binfo) = BINFO_BASE_ACCESSES (binfo);
1491 /* Recursively copy base binfos of BINFO. */
1492 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1494 tree new_base_binfo;
1495 new_base_binfo = copy_binfo (base_binfo, BINFO_TYPE (base_binfo),
1496 t, igo_prev,
1497 BINFO_VIRTUAL_P (base_binfo));
1499 if (!BINFO_INHERITANCE_CHAIN (new_base_binfo))
1500 BINFO_INHERITANCE_CHAIN (new_base_binfo) = new_binfo;
1501 BINFO_BASE_APPEND (new_binfo, new_base_binfo);
1504 else
1505 BINFO_DEPENDENT_BASE_P (new_binfo) = 1;
1507 if (virt)
1509 /* Push it onto the list after any virtual bases it contains
1510 will have been pushed. */
1511 CLASSTYPE_VBASECLASSES (t)->quick_push (new_binfo);
1512 BINFO_VIRTUAL_P (new_binfo) = 1;
1513 BINFO_INHERITANCE_CHAIN (new_binfo) = TYPE_BINFO (t);
1516 return new_binfo;
1519 /* Hashing of lists so that we don't make duplicates.
1520 The entry point is `list_hash_canon'. */
1522 /* Now here is the hash table. When recording a list, it is added
1523 to the slot whose index is the hash code mod the table size.
1524 Note that the hash table is used for several kinds of lists.
1525 While all these live in the same table, they are completely independent,
1526 and the hash code is computed differently for each of these. */
1528 static GTY ((param_is (union tree_node))) htab_t list_hash_table;
1530 struct list_proxy
1532 tree purpose;
1533 tree value;
1534 tree chain;
1537 /* Compare ENTRY (an entry in the hash table) with DATA (a list_proxy
1538 for a node we are thinking about adding). */
1540 static int
1541 list_hash_eq (const void* entry, const void* data)
1543 const_tree const t = (const_tree) entry;
1544 const struct list_proxy *const proxy = (const struct list_proxy *) data;
1546 return (TREE_VALUE (t) == proxy->value
1547 && TREE_PURPOSE (t) == proxy->purpose
1548 && TREE_CHAIN (t) == proxy->chain);
1551 /* Compute a hash code for a list (chain of TREE_LIST nodes
1552 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
1553 TREE_COMMON slots), by adding the hash codes of the individual entries. */
1555 static hashval_t
1556 list_hash_pieces (tree purpose, tree value, tree chain)
1558 hashval_t hashcode = 0;
1560 if (chain)
1561 hashcode += TREE_HASH (chain);
1563 if (value)
1564 hashcode += TREE_HASH (value);
1565 else
1566 hashcode += 1007;
1567 if (purpose)
1568 hashcode += TREE_HASH (purpose);
1569 else
1570 hashcode += 1009;
1571 return hashcode;
1574 /* Hash an already existing TREE_LIST. */
1576 static hashval_t
1577 list_hash (const void* p)
1579 const_tree const t = (const_tree) p;
1580 return list_hash_pieces (TREE_PURPOSE (t),
1581 TREE_VALUE (t),
1582 TREE_CHAIN (t));
1585 /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
1586 object for an identical list if one already exists. Otherwise, build a
1587 new one, and record it as the canonical object. */
1589 tree
1590 hash_tree_cons (tree purpose, tree value, tree chain)
1592 int hashcode = 0;
1593 void **slot;
1594 struct list_proxy proxy;
1596 /* Hash the list node. */
1597 hashcode = list_hash_pieces (purpose, value, chain);
1598 /* Create a proxy for the TREE_LIST we would like to create. We
1599 don't actually create it so as to avoid creating garbage. */
1600 proxy.purpose = purpose;
1601 proxy.value = value;
1602 proxy.chain = chain;
1603 /* See if it is already in the table. */
1604 slot = htab_find_slot_with_hash (list_hash_table, &proxy, hashcode,
1605 INSERT);
1606 /* If not, create a new node. */
1607 if (!*slot)
1608 *slot = tree_cons (purpose, value, chain);
1609 return (tree) *slot;
1612 /* Constructor for hashed lists. */
1614 tree
1615 hash_tree_chain (tree value, tree chain)
1617 return hash_tree_cons (NULL_TREE, value, chain);
1620 void
1621 debug_binfo (tree elem)
1623 HOST_WIDE_INT n;
1624 tree virtuals;
1626 fprintf (stderr, "type \"%s\", offset = " HOST_WIDE_INT_PRINT_DEC
1627 "\nvtable type:\n",
1628 TYPE_NAME_STRING (BINFO_TYPE (elem)),
1629 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
1630 debug_tree (BINFO_TYPE (elem));
1631 if (BINFO_VTABLE (elem))
1632 fprintf (stderr, "vtable decl \"%s\"\n",
1633 IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem))));
1634 else
1635 fprintf (stderr, "no vtable decl yet\n");
1636 fprintf (stderr, "virtuals:\n");
1637 virtuals = BINFO_VIRTUALS (elem);
1638 n = 0;
1640 while (virtuals)
1642 tree fndecl = TREE_VALUE (virtuals);
1643 fprintf (stderr, "%s [%ld =? %ld]\n",
1644 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
1645 (long) n, (long) TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
1646 ++n;
1647 virtuals = TREE_CHAIN (virtuals);
1651 /* Build a representation for the qualified name SCOPE::NAME. TYPE is
1652 the type of the result expression, if known, or NULL_TREE if the
1653 resulting expression is type-dependent. If TEMPLATE_P is true,
1654 NAME is known to be a template because the user explicitly used the
1655 "template" keyword after the "::".
1657 All SCOPE_REFs should be built by use of this function. */
1659 tree
1660 build_qualified_name (tree type, tree scope, tree name, bool template_p)
1662 tree t;
1663 if (type == error_mark_node
1664 || scope == error_mark_node
1665 || name == error_mark_node)
1666 return error_mark_node;
1667 t = build2 (SCOPE_REF, type, scope, name);
1668 QUALIFIED_NAME_IS_TEMPLATE (t) = template_p;
1669 PTRMEM_OK_P (t) = true;
1670 if (type)
1671 t = convert_from_reference (t);
1672 return t;
1675 /* Returns nonzero if X is an expression for a (possibly overloaded)
1676 function. If "f" is a function or function template, "f", "c->f",
1677 "c.f", "C::f", and "f<int>" will all be considered possibly
1678 overloaded functions. Returns 2 if the function is actually
1679 overloaded, i.e., if it is impossible to know the type of the
1680 function without performing overload resolution. */
1683 is_overloaded_fn (tree x)
1685 /* A baselink is also considered an overloaded function. */
1686 if (TREE_CODE (x) == OFFSET_REF
1687 || TREE_CODE (x) == COMPONENT_REF)
1688 x = TREE_OPERAND (x, 1);
1689 if (BASELINK_P (x))
1690 x = BASELINK_FUNCTIONS (x);
1691 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
1692 x = TREE_OPERAND (x, 0);
1693 if (DECL_FUNCTION_TEMPLATE_P (OVL_CURRENT (x))
1694 || (TREE_CODE (x) == OVERLOAD && OVL_CHAIN (x)))
1695 return 2;
1696 return (TREE_CODE (x) == FUNCTION_DECL
1697 || TREE_CODE (x) == OVERLOAD);
1700 /* X is the CALL_EXPR_FN of a CALL_EXPR. If X represents a dependent name
1701 (14.6.2), return the IDENTIFIER_NODE for that name. Otherwise, return
1702 NULL_TREE. */
1704 tree
1705 dependent_name (tree x)
1707 if (TREE_CODE (x) == IDENTIFIER_NODE)
1708 return x;
1709 if (TREE_CODE (x) != COMPONENT_REF
1710 && TREE_CODE (x) != OFFSET_REF
1711 && TREE_CODE (x) != BASELINK
1712 && is_overloaded_fn (x))
1713 return DECL_NAME (get_first_fn (x));
1714 return NULL_TREE;
1717 /* Returns true iff X is an expression for an overloaded function
1718 whose type cannot be known without performing overload
1719 resolution. */
1721 bool
1722 really_overloaded_fn (tree x)
1724 return is_overloaded_fn (x) == 2;
1727 tree
1728 get_fns (tree from)
1730 gcc_assert (is_overloaded_fn (from));
1731 /* A baselink is also considered an overloaded function. */
1732 if (TREE_CODE (from) == OFFSET_REF
1733 || TREE_CODE (from) == COMPONENT_REF)
1734 from = TREE_OPERAND (from, 1);
1735 if (BASELINK_P (from))
1736 from = BASELINK_FUNCTIONS (from);
1737 if (TREE_CODE (from) == TEMPLATE_ID_EXPR)
1738 from = TREE_OPERAND (from, 0);
1739 return from;
1742 tree
1743 get_first_fn (tree from)
1745 return OVL_CURRENT (get_fns (from));
1748 /* Return a new OVL node, concatenating it with the old one. */
1750 tree
1751 ovl_cons (tree decl, tree chain)
1753 tree result = make_node (OVERLOAD);
1754 TREE_TYPE (result) = unknown_type_node;
1755 OVL_FUNCTION (result) = decl;
1756 TREE_CHAIN (result) = chain;
1758 return result;
1761 /* Build a new overloaded function. If this is the first one,
1762 just return it; otherwise, ovl_cons the _DECLs */
1764 tree
1765 build_overload (tree decl, tree chain)
1767 if (! chain && TREE_CODE (decl) != TEMPLATE_DECL)
1768 return decl;
1769 return ovl_cons (decl, chain);
1772 /* Return the scope where the overloaded functions OVL were found. */
1774 tree
1775 ovl_scope (tree ovl)
1777 if (TREE_CODE (ovl) == OFFSET_REF
1778 || TREE_CODE (ovl) == COMPONENT_REF)
1779 ovl = TREE_OPERAND (ovl, 1);
1780 if (TREE_CODE (ovl) == BASELINK)
1781 return BINFO_TYPE (BASELINK_BINFO (ovl));
1782 if (TREE_CODE (ovl) == TEMPLATE_ID_EXPR)
1783 ovl = TREE_OPERAND (ovl, 0);
1784 /* Skip using-declarations. */
1785 while (TREE_CODE (ovl) == OVERLOAD && OVL_USED (ovl) && OVL_CHAIN (ovl))
1786 ovl = OVL_CHAIN (ovl);
1787 return CP_DECL_CONTEXT (OVL_CURRENT (ovl));
1790 /* Return TRUE if FN is a non-static member function, FALSE otherwise.
1791 This function looks into BASELINK and OVERLOAD nodes. */
1793 bool
1794 non_static_member_function_p (tree fn)
1796 if (fn == NULL_TREE)
1797 return false;
1799 if (is_overloaded_fn (fn))
1800 fn = get_first_fn (fn);
1802 return (DECL_P (fn)
1803 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn));
1807 #define PRINT_RING_SIZE 4
1809 static const char *
1810 cxx_printable_name_internal (tree decl, int v, bool translate)
1812 static unsigned int uid_ring[PRINT_RING_SIZE];
1813 static char *print_ring[PRINT_RING_SIZE];
1814 static bool trans_ring[PRINT_RING_SIZE];
1815 static int ring_counter;
1816 int i;
1818 /* Only cache functions. */
1819 if (v < 2
1820 || TREE_CODE (decl) != FUNCTION_DECL
1821 || DECL_LANG_SPECIFIC (decl) == 0)
1822 return lang_decl_name (decl, v, translate);
1824 /* See if this print name is lying around. */
1825 for (i = 0; i < PRINT_RING_SIZE; i++)
1826 if (uid_ring[i] == DECL_UID (decl) && translate == trans_ring[i])
1827 /* yes, so return it. */
1828 return print_ring[i];
1830 if (++ring_counter == PRINT_RING_SIZE)
1831 ring_counter = 0;
1833 if (current_function_decl != NULL_TREE)
1835 /* There may be both translated and untranslated versions of the
1836 name cached. */
1837 for (i = 0; i < 2; i++)
1839 if (uid_ring[ring_counter] == DECL_UID (current_function_decl))
1840 ring_counter += 1;
1841 if (ring_counter == PRINT_RING_SIZE)
1842 ring_counter = 0;
1844 gcc_assert (uid_ring[ring_counter] != DECL_UID (current_function_decl));
1847 free (print_ring[ring_counter]);
1849 print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v, translate));
1850 uid_ring[ring_counter] = DECL_UID (decl);
1851 trans_ring[ring_counter] = translate;
1852 return print_ring[ring_counter];
1855 const char *
1856 cxx_printable_name (tree decl, int v)
1858 return cxx_printable_name_internal (decl, v, false);
1861 const char *
1862 cxx_printable_name_translate (tree decl, int v)
1864 return cxx_printable_name_internal (decl, v, true);
1867 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
1868 listed in RAISES. */
1870 tree
1871 build_exception_variant (tree type, tree raises)
1873 tree v;
1874 int type_quals;
1876 if (comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (type), ce_exact))
1877 return type;
1879 type_quals = TYPE_QUALS (type);
1880 for (v = TYPE_MAIN_VARIANT (type); v; v = TYPE_NEXT_VARIANT (v))
1881 if (check_qualified_type (v, type, type_quals, 0)
1882 && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (v), ce_exact))
1883 return v;
1885 /* Need to build a new variant. */
1886 v = build_variant_type_copy (type);
1887 TYPE_RAISES_EXCEPTIONS (v) = raises;
1888 return v;
1891 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new
1892 BOUND_TEMPLATE_TEMPLATE_PARM bound with NEWARGS as its template
1893 arguments. */
1895 tree
1896 bind_template_template_parm (tree t, tree newargs)
1898 tree decl = TYPE_NAME (t);
1899 tree t2;
1901 t2 = cxx_make_type (BOUND_TEMPLATE_TEMPLATE_PARM);
1902 decl = build_decl (input_location,
1903 TYPE_DECL, DECL_NAME (decl), NULL_TREE);
1905 /* These nodes have to be created to reflect new TYPE_DECL and template
1906 arguments. */
1907 TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t));
1908 TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl;
1909 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
1910 = build_template_info (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t), newargs);
1912 TREE_TYPE (decl) = t2;
1913 TYPE_NAME (t2) = decl;
1914 TYPE_STUB_DECL (t2) = decl;
1915 TYPE_SIZE (t2) = 0;
1916 SET_TYPE_STRUCTURAL_EQUALITY (t2);
1918 return t2;
1921 /* Called from count_trees via walk_tree. */
1923 static tree
1924 count_trees_r (tree *tp, int *walk_subtrees, void *data)
1926 ++*((int *) data);
1928 if (TYPE_P (*tp))
1929 *walk_subtrees = 0;
1931 return NULL_TREE;
1934 /* Debugging function for measuring the rough complexity of a tree
1935 representation. */
1938 count_trees (tree t)
1940 int n_trees = 0;
1941 cp_walk_tree_without_duplicates (&t, count_trees_r, &n_trees);
1942 return n_trees;
1945 /* Called from verify_stmt_tree via walk_tree. */
1947 static tree
1948 verify_stmt_tree_r (tree* tp, int * /*walk_subtrees*/, void* data)
1950 tree t = *tp;
1951 hash_table <pointer_hash <tree_node> > *statements
1952 = static_cast <hash_table <pointer_hash <tree_node> > *> (data);
1953 tree_node **slot;
1955 if (!STATEMENT_CODE_P (TREE_CODE (t)))
1956 return NULL_TREE;
1958 /* If this statement is already present in the hash table, then
1959 there is a circularity in the statement tree. */
1960 gcc_assert (!statements->find (t));
1962 slot = statements->find_slot (t, INSERT);
1963 *slot = t;
1965 return NULL_TREE;
1968 /* Debugging function to check that the statement T has not been
1969 corrupted. For now, this function simply checks that T contains no
1970 circularities. */
1972 void
1973 verify_stmt_tree (tree t)
1975 hash_table <pointer_hash <tree_node> > statements;
1976 statements.create (37);
1977 cp_walk_tree (&t, verify_stmt_tree_r, &statements, NULL);
1978 statements.dispose ();
1981 /* Check if the type T depends on a type with no linkage and if so, return
1982 it. If RELAXED_P then do not consider a class type declared within
1983 a vague-linkage function to have no linkage. */
1985 tree
1986 no_linkage_check (tree t, bool relaxed_p)
1988 tree r;
1990 /* There's no point in checking linkage on template functions; we
1991 can't know their complete types. */
1992 if (processing_template_decl)
1993 return NULL_TREE;
1995 switch (TREE_CODE (t))
1997 case RECORD_TYPE:
1998 if (TYPE_PTRMEMFUNC_P (t))
1999 goto ptrmem;
2000 /* Lambda types that don't have mangling scope have no linkage. We
2001 check CLASSTYPE_LAMBDA_EXPR here rather than LAMBDA_TYPE_P because
2002 when we get here from pushtag none of the lambda information is
2003 set up yet, so we want to assume that the lambda has linkage and
2004 fix it up later if not. */
2005 if (CLASSTYPE_LAMBDA_EXPR (t)
2006 && LAMBDA_TYPE_EXTRA_SCOPE (t) == NULL_TREE)
2007 return t;
2008 /* Fall through. */
2009 case UNION_TYPE:
2010 if (!CLASS_TYPE_P (t))
2011 return NULL_TREE;
2012 /* Fall through. */
2013 case ENUMERAL_TYPE:
2014 /* Only treat anonymous types as having no linkage if they're at
2015 namespace scope. This is core issue 966. */
2016 if (TYPE_ANONYMOUS_P (t) && TYPE_NAMESPACE_SCOPE_P (t))
2017 return t;
2019 for (r = CP_TYPE_CONTEXT (t); ; )
2021 /* If we're a nested type of a !TREE_PUBLIC class, we might not
2022 have linkage, or we might just be in an anonymous namespace.
2023 If we're in a TREE_PUBLIC class, we have linkage. */
2024 if (TYPE_P (r) && !TREE_PUBLIC (TYPE_NAME (r)))
2025 return no_linkage_check (TYPE_CONTEXT (t), relaxed_p);
2026 else if (TREE_CODE (r) == FUNCTION_DECL)
2028 if (!relaxed_p || !vague_linkage_p (r))
2029 return t;
2030 else
2031 r = CP_DECL_CONTEXT (r);
2033 else
2034 break;
2037 return NULL_TREE;
2039 case ARRAY_TYPE:
2040 case POINTER_TYPE:
2041 case REFERENCE_TYPE:
2042 return no_linkage_check (TREE_TYPE (t), relaxed_p);
2044 case OFFSET_TYPE:
2045 ptrmem:
2046 r = no_linkage_check (TYPE_PTRMEM_POINTED_TO_TYPE (t),
2047 relaxed_p);
2048 if (r)
2049 return r;
2050 return no_linkage_check (TYPE_PTRMEM_CLASS_TYPE (t), relaxed_p);
2052 case METHOD_TYPE:
2053 r = no_linkage_check (TYPE_METHOD_BASETYPE (t), relaxed_p);
2054 if (r)
2055 return r;
2056 /* Fall through. */
2057 case FUNCTION_TYPE:
2059 tree parm;
2060 for (parm = TYPE_ARG_TYPES (t);
2061 parm && parm != void_list_node;
2062 parm = TREE_CHAIN (parm))
2064 r = no_linkage_check (TREE_VALUE (parm), relaxed_p);
2065 if (r)
2066 return r;
2068 return no_linkage_check (TREE_TYPE (t), relaxed_p);
2071 default:
2072 return NULL_TREE;
2076 extern int depth_reached;
2078 void
2079 cxx_print_statistics (void)
2081 print_search_statistics ();
2082 print_class_statistics ();
2083 print_template_statistics ();
2084 if (GATHER_STATISTICS)
2085 fprintf (stderr, "maximum template instantiation depth reached: %d\n",
2086 depth_reached);
2089 /* Return, as an INTEGER_CST node, the number of elements for TYPE
2090 (which is an ARRAY_TYPE). This counts only elements of the top
2091 array. */
2093 tree
2094 array_type_nelts_top (tree type)
2096 return fold_build2_loc (input_location,
2097 PLUS_EXPR, sizetype,
2098 array_type_nelts (type),
2099 size_one_node);
2102 /* Return, as an INTEGER_CST node, the number of elements for TYPE
2103 (which is an ARRAY_TYPE). This one is a recursive count of all
2104 ARRAY_TYPEs that are clumped together. */
2106 tree
2107 array_type_nelts_total (tree type)
2109 tree sz = array_type_nelts_top (type);
2110 type = TREE_TYPE (type);
2111 while (TREE_CODE (type) == ARRAY_TYPE)
2113 tree n = array_type_nelts_top (type);
2114 sz = fold_build2_loc (input_location,
2115 MULT_EXPR, sizetype, sz, n);
2116 type = TREE_TYPE (type);
2118 return sz;
2121 /* Called from break_out_target_exprs via mapcar. */
2123 static tree
2124 bot_manip (tree* tp, int* walk_subtrees, void* data)
2126 splay_tree target_remap = ((splay_tree) data);
2127 tree t = *tp;
2129 if (!TYPE_P (t) && TREE_CONSTANT (t) && !TREE_SIDE_EFFECTS (t))
2131 /* There can't be any TARGET_EXPRs or their slot variables below this
2132 point. But we must make a copy, in case subsequent processing
2133 alters any part of it. For example, during gimplification a cast
2134 of the form (T) &X::f (where "f" is a member function) will lead
2135 to replacing the PTRMEM_CST for &X::f with a VAR_DECL. */
2136 *walk_subtrees = 0;
2137 *tp = unshare_expr (t);
2138 return NULL_TREE;
2140 if (TREE_CODE (t) == TARGET_EXPR)
2142 tree u;
2144 if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
2146 u = build_cplus_new (TREE_TYPE (t), TREE_OPERAND (t, 1),
2147 tf_warning_or_error);
2148 if (AGGR_INIT_ZERO_FIRST (TREE_OPERAND (t, 1)))
2149 AGGR_INIT_ZERO_FIRST (TREE_OPERAND (u, 1)) = true;
2151 else
2152 u = build_target_expr_with_type (TREE_OPERAND (t, 1), TREE_TYPE (t),
2153 tf_warning_or_error);
2155 TARGET_EXPR_IMPLICIT_P (u) = TARGET_EXPR_IMPLICIT_P (t);
2156 TARGET_EXPR_LIST_INIT_P (u) = TARGET_EXPR_LIST_INIT_P (t);
2157 TARGET_EXPR_DIRECT_INIT_P (u) = TARGET_EXPR_DIRECT_INIT_P (t);
2159 /* Map the old variable to the new one. */
2160 splay_tree_insert (target_remap,
2161 (splay_tree_key) TREE_OPERAND (t, 0),
2162 (splay_tree_value) TREE_OPERAND (u, 0));
2164 TREE_OPERAND (u, 1) = break_out_target_exprs (TREE_OPERAND (u, 1));
2166 /* Replace the old expression with the new version. */
2167 *tp = u;
2168 /* We don't have to go below this point; the recursive call to
2169 break_out_target_exprs will have handled anything below this
2170 point. */
2171 *walk_subtrees = 0;
2172 return NULL_TREE;
2175 /* Make a copy of this node. */
2176 t = copy_tree_r (tp, walk_subtrees, NULL);
2177 if (TREE_CODE (*tp) == CALL_EXPR)
2178 set_flags_from_callee (*tp);
2179 return t;
2182 /* Replace all remapped VAR_DECLs in T with their new equivalents.
2183 DATA is really a splay-tree mapping old variables to new
2184 variables. */
2186 static tree
2187 bot_replace (tree* t, int* /*walk_subtrees*/, void* data)
2189 splay_tree target_remap = ((splay_tree) data);
2191 if (TREE_CODE (*t) == VAR_DECL)
2193 splay_tree_node n = splay_tree_lookup (target_remap,
2194 (splay_tree_key) *t);
2195 if (n)
2196 *t = (tree) n->value;
2198 else if (TREE_CODE (*t) == PARM_DECL
2199 && DECL_NAME (*t) == this_identifier)
2201 /* In an NSDMI we need to replace the 'this' parameter we used for
2202 parsing with the real one for this function. */
2203 *t = current_class_ptr;
2205 else if (TREE_CODE (*t) == CONVERT_EXPR
2206 && CONVERT_EXPR_VBASE_PATH (*t))
2208 /* In an NSDMI build_base_path defers building conversions to virtual
2209 bases, and we handle it here. */
2210 tree basetype = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (*t)));
2211 vec<tree, va_gc> *vbases = CLASSTYPE_VBASECLASSES (current_class_type);
2212 int i; tree binfo;
2213 FOR_EACH_VEC_SAFE_ELT (vbases, i, binfo)
2214 if (BINFO_TYPE (binfo) == basetype)
2215 break;
2216 *t = build_base_path (PLUS_EXPR, TREE_OPERAND (*t, 0), binfo, true,
2217 tf_warning_or_error);
2220 return NULL_TREE;
2223 /* When we parse a default argument expression, we may create
2224 temporary variables via TARGET_EXPRs. When we actually use the
2225 default-argument expression, we make a copy of the expression
2226 and replace the temporaries with appropriate local versions. */
2228 tree
2229 break_out_target_exprs (tree t)
2231 static int target_remap_count;
2232 static splay_tree target_remap;
2234 if (!target_remap_count++)
2235 target_remap = splay_tree_new (splay_tree_compare_pointers,
2236 /*splay_tree_delete_key_fn=*/NULL,
2237 /*splay_tree_delete_value_fn=*/NULL);
2238 cp_walk_tree (&t, bot_manip, target_remap, NULL);
2239 cp_walk_tree (&t, bot_replace, target_remap, NULL);
2241 if (!--target_remap_count)
2243 splay_tree_delete (target_remap);
2244 target_remap = NULL;
2247 return t;
2250 /* Similar to `build_nt', but for template definitions of dependent
2251 expressions */
2253 tree
2254 build_min_nt_loc (location_t loc, enum tree_code code, ...)
2256 tree t;
2257 int length;
2258 int i;
2259 va_list p;
2261 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
2263 va_start (p, code);
2265 t = make_node (code);
2266 SET_EXPR_LOCATION (t, loc);
2267 length = TREE_CODE_LENGTH (code);
2269 for (i = 0; i < length; i++)
2271 tree x = va_arg (p, tree);
2272 TREE_OPERAND (t, i) = x;
2275 va_end (p);
2276 return t;
2280 /* Similar to `build', but for template definitions. */
2282 tree
2283 build_min (enum tree_code code, tree tt, ...)
2285 tree t;
2286 int length;
2287 int i;
2288 va_list p;
2290 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
2292 va_start (p, tt);
2294 t = make_node (code);
2295 length = TREE_CODE_LENGTH (code);
2296 TREE_TYPE (t) = tt;
2298 for (i = 0; i < length; i++)
2300 tree x = va_arg (p, tree);
2301 TREE_OPERAND (t, i) = x;
2302 if (x && !TYPE_P (x) && TREE_SIDE_EFFECTS (x))
2303 TREE_SIDE_EFFECTS (t) = 1;
2306 va_end (p);
2307 return t;
2310 /* Similar to `build', but for template definitions of non-dependent
2311 expressions. NON_DEP is the non-dependent expression that has been
2312 built. */
2314 tree
2315 build_min_non_dep (enum tree_code code, tree non_dep, ...)
2317 tree t;
2318 int length;
2319 int i;
2320 va_list p;
2322 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
2324 va_start (p, non_dep);
2326 if (REFERENCE_REF_P (non_dep))
2327 non_dep = TREE_OPERAND (non_dep, 0);
2329 t = make_node (code);
2330 length = TREE_CODE_LENGTH (code);
2331 TREE_TYPE (t) = TREE_TYPE (non_dep);
2332 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
2334 for (i = 0; i < length; i++)
2336 tree x = va_arg (p, tree);
2337 TREE_OPERAND (t, i) = x;
2340 if (code == COMPOUND_EXPR && TREE_CODE (non_dep) != COMPOUND_EXPR)
2341 /* This should not be considered a COMPOUND_EXPR, because it
2342 resolves to an overload. */
2343 COMPOUND_EXPR_OVERLOADED (t) = 1;
2345 va_end (p);
2346 return convert_from_reference (t);
2349 /* Similar to `build_nt_call_vec', but for template definitions of
2350 non-dependent expressions. NON_DEP is the non-dependent expression
2351 that has been built. */
2353 tree
2354 build_min_non_dep_call_vec (tree non_dep, tree fn, vec<tree, va_gc> *argvec)
2356 tree t = build_nt_call_vec (fn, argvec);
2357 if (REFERENCE_REF_P (non_dep))
2358 non_dep = TREE_OPERAND (non_dep, 0);
2359 TREE_TYPE (t) = TREE_TYPE (non_dep);
2360 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
2361 return convert_from_reference (t);
2364 tree
2365 get_type_decl (tree t)
2367 if (TREE_CODE (t) == TYPE_DECL)
2368 return t;
2369 if (TYPE_P (t))
2370 return TYPE_STUB_DECL (t);
2371 gcc_assert (t == error_mark_node);
2372 return t;
2375 /* Returns the namespace that contains DECL, whether directly or
2376 indirectly. */
2378 tree
2379 decl_namespace_context (tree decl)
2381 while (1)
2383 if (TREE_CODE (decl) == NAMESPACE_DECL)
2384 return decl;
2385 else if (TYPE_P (decl))
2386 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
2387 else
2388 decl = CP_DECL_CONTEXT (decl);
2392 /* Returns true if decl is within an anonymous namespace, however deeply
2393 nested, or false otherwise. */
2395 bool
2396 decl_anon_ns_mem_p (const_tree decl)
2398 while (1)
2400 if (decl == NULL_TREE || decl == error_mark_node)
2401 return false;
2402 if (TREE_CODE (decl) == NAMESPACE_DECL
2403 && DECL_NAME (decl) == NULL_TREE)
2404 return true;
2405 /* Classes and namespaces inside anonymous namespaces have
2406 TREE_PUBLIC == 0, so we can shortcut the search. */
2407 else if (TYPE_P (decl))
2408 return (TREE_PUBLIC (TYPE_MAIN_DECL (decl)) == 0);
2409 else if (TREE_CODE (decl) == NAMESPACE_DECL)
2410 return (TREE_PUBLIC (decl) == 0);
2411 else
2412 decl = DECL_CONTEXT (decl);
2416 /* Subroutine of cp_tree_equal: t1 and t2 are the CALL_EXPR_FNs of two
2417 CALL_EXPRS. Return whether they are equivalent. */
2419 static bool
2420 called_fns_equal (tree t1, tree t2)
2422 /* Core 1321: dependent names are equivalent even if the overload sets
2423 are different. But do compare explicit template arguments. */
2424 tree name1 = dependent_name (t1);
2425 tree name2 = dependent_name (t2);
2426 if (name1 || name2)
2428 tree targs1 = NULL_TREE, targs2 = NULL_TREE;
2430 if (name1 != name2)
2431 return false;
2433 if (TREE_CODE (t1) == TEMPLATE_ID_EXPR)
2434 targs1 = TREE_OPERAND (t1, 1);
2435 if (TREE_CODE (t2) == TEMPLATE_ID_EXPR)
2436 targs2 = TREE_OPERAND (t2, 1);
2437 return cp_tree_equal (targs1, targs2);
2439 else
2440 return cp_tree_equal (t1, t2);
2443 /* Return truthvalue of whether T1 is the same tree structure as T2.
2444 Return 1 if they are the same. Return 0 if they are different. */
2446 bool
2447 cp_tree_equal (tree t1, tree t2)
2449 enum tree_code code1, code2;
2451 if (t1 == t2)
2452 return true;
2453 if (!t1 || !t2)
2454 return false;
2456 for (code1 = TREE_CODE (t1);
2457 CONVERT_EXPR_CODE_P (code1)
2458 || code1 == NON_LVALUE_EXPR;
2459 code1 = TREE_CODE (t1))
2460 t1 = TREE_OPERAND (t1, 0);
2461 for (code2 = TREE_CODE (t2);
2462 CONVERT_EXPR_CODE_P (code2)
2463 || code1 == NON_LVALUE_EXPR;
2464 code2 = TREE_CODE (t2))
2465 t2 = TREE_OPERAND (t2, 0);
2467 /* They might have become equal now. */
2468 if (t1 == t2)
2469 return true;
2471 if (code1 != code2)
2472 return false;
2474 switch (code1)
2476 case INTEGER_CST:
2477 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
2478 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
2480 case REAL_CST:
2481 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2483 case STRING_CST:
2484 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2485 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2486 TREE_STRING_LENGTH (t1));
2488 case FIXED_CST:
2489 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
2490 TREE_FIXED_CST (t2));
2492 case COMPLEX_CST:
2493 return cp_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
2494 && cp_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
2496 case VECTOR_CST:
2497 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
2499 case CONSTRUCTOR:
2500 /* We need to do this when determining whether or not two
2501 non-type pointer to member function template arguments
2502 are the same. */
2503 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
2504 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
2505 return false;
2507 tree field, value;
2508 unsigned int i;
2509 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
2511 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
2512 if (!cp_tree_equal (field, elt2->index)
2513 || !cp_tree_equal (value, elt2->value))
2514 return false;
2517 return true;
2519 case TREE_LIST:
2520 if (!cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
2521 return false;
2522 if (!cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
2523 return false;
2524 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
2526 case SAVE_EXPR:
2527 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2529 case CALL_EXPR:
2531 tree arg1, arg2;
2532 call_expr_arg_iterator iter1, iter2;
2533 if (!called_fns_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
2534 return false;
2535 for (arg1 = first_call_expr_arg (t1, &iter1),
2536 arg2 = first_call_expr_arg (t2, &iter2);
2537 arg1 && arg2;
2538 arg1 = next_call_expr_arg (&iter1),
2539 arg2 = next_call_expr_arg (&iter2))
2540 if (!cp_tree_equal (arg1, arg2))
2541 return false;
2542 if (arg1 || arg2)
2543 return false;
2544 return true;
2547 case TARGET_EXPR:
2549 tree o1 = TREE_OPERAND (t1, 0);
2550 tree o2 = TREE_OPERAND (t2, 0);
2552 /* Special case: if either target is an unallocated VAR_DECL,
2553 it means that it's going to be unified with whatever the
2554 TARGET_EXPR is really supposed to initialize, so treat it
2555 as being equivalent to anything. */
2556 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
2557 && !DECL_RTL_SET_P (o1))
2558 /*Nop*/;
2559 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
2560 && !DECL_RTL_SET_P (o2))
2561 /*Nop*/;
2562 else if (!cp_tree_equal (o1, o2))
2563 return false;
2565 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
2568 case WITH_CLEANUP_EXPR:
2569 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
2570 return false;
2571 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
2573 case COMPONENT_REF:
2574 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
2575 return false;
2576 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2578 case PARM_DECL:
2579 /* For comparing uses of parameters in late-specified return types
2580 with an out-of-class definition of the function, but can also come
2581 up for expressions that involve 'this' in a member function
2582 template. */
2583 if (same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2585 if (DECL_ARTIFICIAL (t1) ^ DECL_ARTIFICIAL (t2))
2586 return false;
2587 if (DECL_ARTIFICIAL (t1)
2588 || (DECL_PARM_LEVEL (t1) == DECL_PARM_LEVEL (t2)
2589 && DECL_PARM_INDEX (t1) == DECL_PARM_INDEX (t2)))
2590 return true;
2592 return false;
2594 case VAR_DECL:
2595 case CONST_DECL:
2596 case FIELD_DECL:
2597 case FUNCTION_DECL:
2598 case TEMPLATE_DECL:
2599 case IDENTIFIER_NODE:
2600 case SSA_NAME:
2601 return false;
2603 case BASELINK:
2604 return (BASELINK_BINFO (t1) == BASELINK_BINFO (t2)
2605 && BASELINK_ACCESS_BINFO (t1) == BASELINK_ACCESS_BINFO (t2)
2606 && BASELINK_QUALIFIED_P (t1) == BASELINK_QUALIFIED_P (t2)
2607 && cp_tree_equal (BASELINK_FUNCTIONS (t1),
2608 BASELINK_FUNCTIONS (t2)));
2610 case TEMPLATE_PARM_INDEX:
2611 return (TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
2612 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2)
2613 && (TEMPLATE_PARM_PARAMETER_PACK (t1)
2614 == TEMPLATE_PARM_PARAMETER_PACK (t2))
2615 && same_type_p (TREE_TYPE (TEMPLATE_PARM_DECL (t1)),
2616 TREE_TYPE (TEMPLATE_PARM_DECL (t2))));
2618 case TEMPLATE_ID_EXPR:
2619 return (cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
2620 && cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
2622 case TREE_VEC:
2624 unsigned ix;
2625 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2626 return false;
2627 for (ix = TREE_VEC_LENGTH (t1); ix--;)
2628 if (!cp_tree_equal (TREE_VEC_ELT (t1, ix),
2629 TREE_VEC_ELT (t2, ix)))
2630 return false;
2631 return true;
2634 case SIZEOF_EXPR:
2635 case ALIGNOF_EXPR:
2637 tree o1 = TREE_OPERAND (t1, 0);
2638 tree o2 = TREE_OPERAND (t2, 0);
2640 if (code1 == SIZEOF_EXPR)
2642 if (SIZEOF_EXPR_TYPE_P (t1))
2643 o1 = TREE_TYPE (o1);
2644 if (SIZEOF_EXPR_TYPE_P (t2))
2645 o2 = TREE_TYPE (o2);
2647 if (TREE_CODE (o1) != TREE_CODE (o2))
2648 return false;
2649 if (TYPE_P (o1))
2650 return same_type_p (o1, o2);
2651 else
2652 return cp_tree_equal (o1, o2);
2655 case MODOP_EXPR:
2657 tree t1_op1, t2_op1;
2659 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
2660 return false;
2662 t1_op1 = TREE_OPERAND (t1, 1);
2663 t2_op1 = TREE_OPERAND (t2, 1);
2664 if (TREE_CODE (t1_op1) != TREE_CODE (t2_op1))
2665 return false;
2667 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t2, 2));
2670 case PTRMEM_CST:
2671 /* Two pointer-to-members are the same if they point to the same
2672 field or function in the same class. */
2673 if (PTRMEM_CST_MEMBER (t1) != PTRMEM_CST_MEMBER (t2))
2674 return false;
2676 return same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2));
2678 case OVERLOAD:
2679 if (OVL_FUNCTION (t1) != OVL_FUNCTION (t2))
2680 return false;
2681 return cp_tree_equal (OVL_CHAIN (t1), OVL_CHAIN (t2));
2683 case TRAIT_EXPR:
2684 if (TRAIT_EXPR_KIND (t1) != TRAIT_EXPR_KIND (t2))
2685 return false;
2686 return same_type_p (TRAIT_EXPR_TYPE1 (t1), TRAIT_EXPR_TYPE1 (t2))
2687 && same_type_p (TRAIT_EXPR_TYPE2 (t1), TRAIT_EXPR_TYPE2 (t2));
2689 case CAST_EXPR:
2690 case STATIC_CAST_EXPR:
2691 case REINTERPRET_CAST_EXPR:
2692 case CONST_CAST_EXPR:
2693 case DYNAMIC_CAST_EXPR:
2694 case IMPLICIT_CONV_EXPR:
2695 case NEW_EXPR:
2696 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2697 return false;
2698 /* Now compare operands as usual. */
2699 break;
2701 case DEFERRED_NOEXCEPT:
2702 return (cp_tree_equal (DEFERRED_NOEXCEPT_PATTERN (t1),
2703 DEFERRED_NOEXCEPT_PATTERN (t2))
2704 && comp_template_args (DEFERRED_NOEXCEPT_ARGS (t1),
2705 DEFERRED_NOEXCEPT_ARGS (t2)));
2706 break;
2708 default:
2709 break;
2712 switch (TREE_CODE_CLASS (code1))
2714 case tcc_unary:
2715 case tcc_binary:
2716 case tcc_comparison:
2717 case tcc_expression:
2718 case tcc_vl_exp:
2719 case tcc_reference:
2720 case tcc_statement:
2722 int i, n;
2724 n = cp_tree_operand_length (t1);
2725 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
2726 && n != TREE_OPERAND_LENGTH (t2))
2727 return false;
2729 for (i = 0; i < n; ++i)
2730 if (!cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
2731 return false;
2733 return true;
2736 case tcc_type:
2737 return same_type_p (t1, t2);
2738 default:
2739 gcc_unreachable ();
2741 /* We can get here with --disable-checking. */
2742 return false;
2745 /* The type of ARG when used as an lvalue. */
2747 tree
2748 lvalue_type (tree arg)
2750 tree type = TREE_TYPE (arg);
2751 return type;
2754 /* The type of ARG for printing error messages; denote lvalues with
2755 reference types. */
2757 tree
2758 error_type (tree arg)
2760 tree type = TREE_TYPE (arg);
2762 if (TREE_CODE (type) == ARRAY_TYPE)
2764 else if (TREE_CODE (type) == ERROR_MARK)
2766 else if (real_lvalue_p (arg))
2767 type = build_reference_type (lvalue_type (arg));
2768 else if (MAYBE_CLASS_TYPE_P (type))
2769 type = lvalue_type (arg);
2771 return type;
2774 /* Does FUNCTION use a variable-length argument list? */
2777 varargs_function_p (const_tree function)
2779 return stdarg_p (TREE_TYPE (function));
2782 /* Returns 1 if decl is a member of a class. */
2785 member_p (const_tree decl)
2787 const_tree const ctx = DECL_CONTEXT (decl);
2788 return (ctx && TYPE_P (ctx));
2791 /* Create a placeholder for member access where we don't actually have an
2792 object that the access is against. */
2794 tree
2795 build_dummy_object (tree type)
2797 tree decl = build1 (NOP_EXPR, build_pointer_type (type), void_zero_node);
2798 return cp_build_indirect_ref (decl, RO_NULL, tf_warning_or_error);
2801 /* We've gotten a reference to a member of TYPE. Return *this if appropriate,
2802 or a dummy object otherwise. If BINFOP is non-0, it is filled with the
2803 binfo path from current_class_type to TYPE, or 0. */
2805 tree
2806 maybe_dummy_object (tree type, tree* binfop)
2808 tree decl, context;
2809 tree binfo;
2810 tree current = current_nonlambda_class_type ();
2812 if (current
2813 && (binfo = lookup_base (current, type, ba_any, NULL,
2814 tf_warning_or_error)))
2815 context = current;
2816 else
2818 /* Reference from a nested class member function. */
2819 context = type;
2820 binfo = TYPE_BINFO (type);
2823 if (binfop)
2824 *binfop = binfo;
2826 if (current_class_ref
2827 /* current_class_ref might not correspond to current_class_type if
2828 we're in tsubst_default_argument or a lambda-declarator; in either
2829 case, we want to use current_class_ref if it matches CONTEXT. */
2830 && (same_type_ignoring_top_level_qualifiers_p
2831 (TREE_TYPE (current_class_ref), context)))
2832 decl = current_class_ref;
2833 else if (current != current_class_type
2834 && context == nonlambda_method_basetype ())
2835 /* In a lambda, need to go through 'this' capture. */
2836 decl = (build_x_indirect_ref
2837 (input_location, (lambda_expr_this_capture
2838 (CLASSTYPE_LAMBDA_EXPR (current_class_type))),
2839 RO_NULL, tf_warning_or_error));
2840 else
2841 decl = build_dummy_object (context);
2843 return decl;
2846 /* Returns 1 if OB is a placeholder object, or a pointer to one. */
2849 is_dummy_object (const_tree ob)
2851 if (TREE_CODE (ob) == INDIRECT_REF)
2852 ob = TREE_OPERAND (ob, 0);
2853 return (TREE_CODE (ob) == NOP_EXPR
2854 && TREE_OPERAND (ob, 0) == void_zero_node);
2857 /* Returns 1 iff type T is something we want to treat as a scalar type for
2858 the purpose of deciding whether it is trivial/POD/standard-layout. */
2860 bool
2861 scalarish_type_p (const_tree t)
2863 if (t == error_mark_node)
2864 return 1;
2866 return (SCALAR_TYPE_P (t)
2867 || TREE_CODE (t) == VECTOR_TYPE);
2870 /* Returns true iff T requires non-trivial default initialization. */
2872 bool
2873 type_has_nontrivial_default_init (const_tree t)
2875 t = strip_array_types (CONST_CAST_TREE (t));
2877 if (CLASS_TYPE_P (t))
2878 return TYPE_HAS_COMPLEX_DFLT (t);
2879 else
2880 return 0;
2883 /* Returns true iff copying an object of type T (including via move
2884 constructor) is non-trivial. That is, T has no non-trivial copy
2885 constructors and no non-trivial move constructors. */
2887 bool
2888 type_has_nontrivial_copy_init (const_tree t)
2890 t = strip_array_types (CONST_CAST_TREE (t));
2892 if (CLASS_TYPE_P (t))
2894 gcc_assert (COMPLETE_TYPE_P (t));
2895 return ((TYPE_HAS_COPY_CTOR (t)
2896 && TYPE_HAS_COMPLEX_COPY_CTOR (t))
2897 || TYPE_HAS_COMPLEX_MOVE_CTOR (t));
2899 else
2900 return 0;
2903 /* Returns 1 iff type T is a trivially copyable type, as defined in
2904 [basic.types] and [class]. */
2906 bool
2907 trivially_copyable_p (const_tree t)
2909 t = strip_array_types (CONST_CAST_TREE (t));
2911 if (CLASS_TYPE_P (t))
2912 return ((!TYPE_HAS_COPY_CTOR (t)
2913 || !TYPE_HAS_COMPLEX_COPY_CTOR (t))
2914 && !TYPE_HAS_COMPLEX_MOVE_CTOR (t)
2915 && (!TYPE_HAS_COPY_ASSIGN (t)
2916 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (t))
2917 && !TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
2918 && TYPE_HAS_TRIVIAL_DESTRUCTOR (t));
2919 else
2920 return scalarish_type_p (t);
2923 /* Returns 1 iff type T is a trivial type, as defined in [basic.types] and
2924 [class]. */
2926 bool
2927 trivial_type_p (const_tree t)
2929 t = strip_array_types (CONST_CAST_TREE (t));
2931 if (CLASS_TYPE_P (t))
2932 return (TYPE_HAS_TRIVIAL_DFLT (t)
2933 && trivially_copyable_p (t));
2934 else
2935 return scalarish_type_p (t);
2938 /* Returns 1 iff type T is a POD type, as defined in [basic.types]. */
2940 bool
2941 pod_type_p (const_tree t)
2943 /* This CONST_CAST is okay because strip_array_types returns its
2944 argument unmodified and we assign it to a const_tree. */
2945 t = strip_array_types (CONST_CAST_TREE(t));
2947 if (!CLASS_TYPE_P (t))
2948 return scalarish_type_p (t);
2949 else if (cxx_dialect > cxx98)
2950 /* [class]/10: A POD struct is a class that is both a trivial class and a
2951 standard-layout class, and has no non-static data members of type
2952 non-POD struct, non-POD union (or array of such types).
2954 We don't need to check individual members because if a member is
2955 non-std-layout or non-trivial, the class will be too. */
2956 return (std_layout_type_p (t) && trivial_type_p (t));
2957 else
2958 /* The C++98 definition of POD is different. */
2959 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
2962 /* Returns true iff T is POD for the purpose of layout, as defined in the
2963 C++ ABI. */
2965 bool
2966 layout_pod_type_p (const_tree t)
2968 t = strip_array_types (CONST_CAST_TREE (t));
2970 if (CLASS_TYPE_P (t))
2971 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
2972 else
2973 return scalarish_type_p (t);
2976 /* Returns true iff T is a standard-layout type, as defined in
2977 [basic.types]. */
2979 bool
2980 std_layout_type_p (const_tree t)
2982 t = strip_array_types (CONST_CAST_TREE (t));
2984 if (CLASS_TYPE_P (t))
2985 return !CLASSTYPE_NON_STD_LAYOUT (t);
2986 else
2987 return scalarish_type_p (t);
2990 /* Nonzero iff type T is a class template implicit specialization. */
2992 bool
2993 class_tmpl_impl_spec_p (const_tree t)
2995 return CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INSTANTIATION (t);
2998 /* Returns 1 iff zero initialization of type T means actually storing
2999 zeros in it. */
3002 zero_init_p (const_tree t)
3004 /* This CONST_CAST is okay because strip_array_types returns its
3005 argument unmodified and we assign it to a const_tree. */
3006 t = strip_array_types (CONST_CAST_TREE(t));
3008 if (t == error_mark_node)
3009 return 1;
3011 /* NULL pointers to data members are initialized with -1. */
3012 if (TYPE_PTRDATAMEM_P (t))
3013 return 0;
3015 /* Classes that contain types that can't be zero-initialized, cannot
3016 be zero-initialized themselves. */
3017 if (CLASS_TYPE_P (t) && CLASSTYPE_NON_ZERO_INIT_P (t))
3018 return 0;
3020 return 1;
3023 /* Table of valid C++ attributes. */
3024 const struct attribute_spec cxx_attribute_table[] =
3026 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
3027 affects_type_identity } */
3028 { "java_interface", 0, 0, false, false, false,
3029 handle_java_interface_attribute, false },
3030 { "com_interface", 0, 0, false, false, false,
3031 handle_com_interface_attribute, false },
3032 { "init_priority", 1, 1, true, false, false,
3033 handle_init_priority_attribute, false },
3034 { "abi_tag", 1, -1, false, false, false,
3035 handle_abi_tag_attribute, true },
3036 { NULL, 0, 0, false, false, false, NULL, false }
3039 /* Handle a "java_interface" attribute; arguments as in
3040 struct attribute_spec.handler. */
3041 static tree
3042 handle_java_interface_attribute (tree* node,
3043 tree name,
3044 tree /*args*/,
3045 int flags,
3046 bool* no_add_attrs)
3048 if (DECL_P (*node)
3049 || !CLASS_TYPE_P (*node)
3050 || !TYPE_FOR_JAVA (*node))
3052 error ("%qE attribute can only be applied to Java class definitions",
3053 name);
3054 *no_add_attrs = true;
3055 return NULL_TREE;
3057 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
3058 *node = build_variant_type_copy (*node);
3059 TYPE_JAVA_INTERFACE (*node) = 1;
3061 return NULL_TREE;
3064 /* Handle a "com_interface" attribute; arguments as in
3065 struct attribute_spec.handler. */
3066 static tree
3067 handle_com_interface_attribute (tree* node,
3068 tree name,
3069 tree /*args*/,
3070 int /*flags*/,
3071 bool* no_add_attrs)
3073 static int warned;
3075 *no_add_attrs = true;
3077 if (DECL_P (*node)
3078 || !CLASS_TYPE_P (*node)
3079 || *node != TYPE_MAIN_VARIANT (*node))
3081 warning (OPT_Wattributes, "%qE attribute can only be applied "
3082 "to class definitions", name);
3083 return NULL_TREE;
3086 if (!warned++)
3087 warning (0, "%qE is obsolete; g++ vtables are now COM-compatible by default",
3088 name);
3090 return NULL_TREE;
3093 /* Handle an "init_priority" attribute; arguments as in
3094 struct attribute_spec.handler. */
3095 static tree
3096 handle_init_priority_attribute (tree* node,
3097 tree name,
3098 tree args,
3099 int /*flags*/,
3100 bool* no_add_attrs)
3102 tree initp_expr = TREE_VALUE (args);
3103 tree decl = *node;
3104 tree type = TREE_TYPE (decl);
3105 int pri;
3107 STRIP_NOPS (initp_expr);
3109 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
3111 error ("requested init_priority is not an integer constant");
3112 *no_add_attrs = true;
3113 return NULL_TREE;
3116 pri = TREE_INT_CST_LOW (initp_expr);
3118 type = strip_array_types (type);
3120 if (decl == NULL_TREE
3121 || TREE_CODE (decl) != VAR_DECL
3122 || !TREE_STATIC (decl)
3123 || DECL_EXTERNAL (decl)
3124 || (TREE_CODE (type) != RECORD_TYPE
3125 && TREE_CODE (type) != UNION_TYPE)
3126 /* Static objects in functions are initialized the
3127 first time control passes through that
3128 function. This is not precise enough to pin down an
3129 init_priority value, so don't allow it. */
3130 || current_function_decl)
3132 error ("can only use %qE attribute on file-scope definitions "
3133 "of objects of class type", name);
3134 *no_add_attrs = true;
3135 return NULL_TREE;
3138 if (pri > MAX_INIT_PRIORITY || pri <= 0)
3140 error ("requested init_priority is out of range");
3141 *no_add_attrs = true;
3142 return NULL_TREE;
3145 /* Check for init_priorities that are reserved for
3146 language and runtime support implementations.*/
3147 if (pri <= MAX_RESERVED_INIT_PRIORITY)
3149 warning
3150 (0, "requested init_priority is reserved for internal use");
3153 if (SUPPORTS_INIT_PRIORITY)
3155 SET_DECL_INIT_PRIORITY (decl, pri);
3156 DECL_HAS_INIT_PRIORITY_P (decl) = 1;
3157 return NULL_TREE;
3159 else
3161 error ("%qE attribute is not supported on this platform", name);
3162 *no_add_attrs = true;
3163 return NULL_TREE;
3167 /* DECL is being redeclared; the old declaration had the abi tags in OLD,
3168 and the new one has the tags in NEW_. Give an error if there are tags
3169 in NEW_ that weren't in OLD. */
3171 bool
3172 check_abi_tag_redeclaration (const_tree decl, const_tree old, const_tree new_)
3174 if (old && TREE_CODE (TREE_VALUE (old)) == TREE_LIST)
3175 old = TREE_VALUE (old);
3176 if (new_ && TREE_CODE (TREE_VALUE (new_)) == TREE_LIST)
3177 new_ = TREE_VALUE (new_);
3178 bool err = false;
3179 for (const_tree t = new_; t; t = TREE_CHAIN (t))
3181 tree str = TREE_VALUE (t);
3182 for (const_tree in = old; in; in = TREE_CHAIN (in))
3184 tree ostr = TREE_VALUE (in);
3185 if (cp_tree_equal (str, ostr))
3186 goto found;
3188 error ("redeclaration of %qD adds abi tag %E", decl, str);
3189 err = true;
3190 found:;
3192 if (err)
3194 inform (DECL_SOURCE_LOCATION (decl), "previous declaration here");
3195 return false;
3197 return true;
3200 /* Handle an "abi_tag" attribute; arguments as in
3201 struct attribute_spec.handler. */
3203 static tree
3204 handle_abi_tag_attribute (tree* node, tree name, tree args,
3205 int flags, bool* no_add_attrs)
3207 if (TYPE_P (*node))
3209 if (!TAGGED_TYPE_P (*node))
3211 error ("%qE attribute applied to non-class, non-enum type %qT",
3212 name, *node);
3213 goto fail;
3215 else if (!(flags & (int)ATTR_FLAG_TYPE_IN_PLACE))
3217 error ("%qE attribute applied to %qT after its definition",
3218 name, *node);
3219 goto fail;
3222 tree attributes = TYPE_ATTRIBUTES (*node);
3223 tree decl = TYPE_NAME (*node);
3225 /* Make sure all declarations have the same abi tags. */
3226 if (DECL_SOURCE_LOCATION (decl) != input_location)
3228 if (!check_abi_tag_redeclaration (decl,
3229 lookup_attribute ("abi_tag",
3230 attributes),
3231 args))
3232 goto fail;
3235 else
3237 if (TREE_CODE (*node) != FUNCTION_DECL)
3239 error ("%qE attribute applied to non-function %qD", name, *node);
3240 goto fail;
3242 else if (DECL_LANGUAGE (*node) == lang_c)
3244 error ("%qE attribute applied to extern \"C\" function %qD",
3245 name, *node);
3246 goto fail;
3250 return NULL_TREE;
3252 fail:
3253 *no_add_attrs = true;
3254 return NULL_TREE;
3257 /* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the
3258 thing pointed to by the constant. */
3260 tree
3261 make_ptrmem_cst (tree type, tree member)
3263 tree ptrmem_cst = make_node (PTRMEM_CST);
3264 TREE_TYPE (ptrmem_cst) = type;
3265 PTRMEM_CST_MEMBER (ptrmem_cst) = member;
3266 return ptrmem_cst;
3269 /* Build a variant of TYPE that has the indicated ATTRIBUTES. May
3270 return an existing type if an appropriate type already exists. */
3272 tree
3273 cp_build_type_attribute_variant (tree type, tree attributes)
3275 tree new_type;
3277 new_type = build_type_attribute_variant (type, attributes);
3278 if (TREE_CODE (new_type) == FUNCTION_TYPE
3279 || TREE_CODE (new_type) == METHOD_TYPE)
3280 new_type = build_exception_variant (new_type,
3281 TYPE_RAISES_EXCEPTIONS (type));
3283 /* Making a new main variant of a class type is broken. */
3284 gcc_assert (!CLASS_TYPE_P (type) || new_type == type);
3286 return new_type;
3289 /* Return TRUE if TYPE1 and TYPE2 are identical for type hashing purposes.
3290 Called only after doing all language independent checks. Only
3291 to check TYPE_RAISES_EXCEPTIONS for FUNCTION_TYPE, the rest is already
3292 compared in type_hash_eq. */
3294 bool
3295 cxx_type_hash_eq (const_tree typea, const_tree typeb)
3297 gcc_assert (TREE_CODE (typea) == FUNCTION_TYPE
3298 || TREE_CODE (typea) == METHOD_TYPE);
3300 return comp_except_specs (TYPE_RAISES_EXCEPTIONS (typea),
3301 TYPE_RAISES_EXCEPTIONS (typeb), ce_exact);
3304 /* Apply FUNC to all language-specific sub-trees of TP in a pre-order
3305 traversal. Called from walk_tree. */
3307 tree
3308 cp_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func,
3309 void *data, struct pointer_set_t *pset)
3311 enum tree_code code = TREE_CODE (*tp);
3312 tree result;
3314 #define WALK_SUBTREE(NODE) \
3315 do \
3317 result = cp_walk_tree (&(NODE), func, data, pset); \
3318 if (result) goto out; \
3320 while (0)
3322 /* Not one of the easy cases. We must explicitly go through the
3323 children. */
3324 result = NULL_TREE;
3325 switch (code)
3327 case DEFAULT_ARG:
3328 case TEMPLATE_TEMPLATE_PARM:
3329 case BOUND_TEMPLATE_TEMPLATE_PARM:
3330 case UNBOUND_CLASS_TEMPLATE:
3331 case TEMPLATE_PARM_INDEX:
3332 case TEMPLATE_TYPE_PARM:
3333 case TYPENAME_TYPE:
3334 case TYPEOF_TYPE:
3335 case UNDERLYING_TYPE:
3336 /* None of these have subtrees other than those already walked
3337 above. */
3338 *walk_subtrees_p = 0;
3339 break;
3341 case BASELINK:
3342 WALK_SUBTREE (BASELINK_FUNCTIONS (*tp));
3343 *walk_subtrees_p = 0;
3344 break;
3346 case PTRMEM_CST:
3347 WALK_SUBTREE (TREE_TYPE (*tp));
3348 *walk_subtrees_p = 0;
3349 break;
3351 case TREE_LIST:
3352 WALK_SUBTREE (TREE_PURPOSE (*tp));
3353 break;
3355 case OVERLOAD:
3356 WALK_SUBTREE (OVL_FUNCTION (*tp));
3357 WALK_SUBTREE (OVL_CHAIN (*tp));
3358 *walk_subtrees_p = 0;
3359 break;
3361 case USING_DECL:
3362 WALK_SUBTREE (DECL_NAME (*tp));
3363 WALK_SUBTREE (USING_DECL_SCOPE (*tp));
3364 WALK_SUBTREE (USING_DECL_DECLS (*tp));
3365 *walk_subtrees_p = 0;
3366 break;
3368 case RECORD_TYPE:
3369 if (TYPE_PTRMEMFUNC_P (*tp))
3370 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (*tp));
3371 break;
3373 case TYPE_ARGUMENT_PACK:
3374 case NONTYPE_ARGUMENT_PACK:
3376 tree args = ARGUMENT_PACK_ARGS (*tp);
3377 int i, len = TREE_VEC_LENGTH (args);
3378 for (i = 0; i < len; i++)
3379 WALK_SUBTREE (TREE_VEC_ELT (args, i));
3381 break;
3383 case TYPE_PACK_EXPANSION:
3384 WALK_SUBTREE (TREE_TYPE (*tp));
3385 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp));
3386 *walk_subtrees_p = 0;
3387 break;
3389 case EXPR_PACK_EXPANSION:
3390 WALK_SUBTREE (TREE_OPERAND (*tp, 0));
3391 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp));
3392 *walk_subtrees_p = 0;
3393 break;
3395 case CAST_EXPR:
3396 case REINTERPRET_CAST_EXPR:
3397 case STATIC_CAST_EXPR:
3398 case CONST_CAST_EXPR:
3399 case DYNAMIC_CAST_EXPR:
3400 case IMPLICIT_CONV_EXPR:
3401 if (TREE_TYPE (*tp))
3402 WALK_SUBTREE (TREE_TYPE (*tp));
3405 int i;
3406 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (*tp)); ++i)
3407 WALK_SUBTREE (TREE_OPERAND (*tp, i));
3409 *walk_subtrees_p = 0;
3410 break;
3412 case TRAIT_EXPR:
3413 WALK_SUBTREE (TRAIT_EXPR_TYPE1 (*tp));
3414 WALK_SUBTREE (TRAIT_EXPR_TYPE2 (*tp));
3415 *walk_subtrees_p = 0;
3416 break;
3418 case DECLTYPE_TYPE:
3419 WALK_SUBTREE (DECLTYPE_TYPE_EXPR (*tp));
3420 *walk_subtrees_p = 0;
3421 break;
3424 default:
3425 return NULL_TREE;
3428 /* We didn't find what we were looking for. */
3429 out:
3430 return result;
3432 #undef WALK_SUBTREE
3435 /* Like save_expr, but for C++. */
3437 tree
3438 cp_save_expr (tree expr)
3440 /* There is no reason to create a SAVE_EXPR within a template; if
3441 needed, we can create the SAVE_EXPR when instantiating the
3442 template. Furthermore, the middle-end cannot handle C++-specific
3443 tree codes. */
3444 if (processing_template_decl)
3445 return expr;
3446 return save_expr (expr);
3449 /* Initialize tree.c. */
3451 void
3452 init_tree (void)
3454 list_hash_table = htab_create_ggc (31, list_hash, list_hash_eq, NULL);
3457 /* Returns the kind of special function that DECL (a FUNCTION_DECL)
3458 is. Note that sfk_none is zero, so this function can be used as a
3459 predicate to test whether or not DECL is a special function. */
3461 special_function_kind
3462 special_function_p (const_tree decl)
3464 /* Rather than doing all this stuff with magic names, we should
3465 probably have a field of type `special_function_kind' in
3466 DECL_LANG_SPECIFIC. */
3467 if (DECL_INHERITED_CTOR_BASE (decl))
3468 return sfk_inheriting_constructor;
3469 if (DECL_COPY_CONSTRUCTOR_P (decl))
3470 return sfk_copy_constructor;
3471 if (DECL_MOVE_CONSTRUCTOR_P (decl))
3472 return sfk_move_constructor;
3473 if (DECL_CONSTRUCTOR_P (decl))
3474 return sfk_constructor;
3475 if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
3477 if (copy_fn_p (decl))
3478 return sfk_copy_assignment;
3479 if (move_fn_p (decl))
3480 return sfk_move_assignment;
3482 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
3483 return sfk_destructor;
3484 if (DECL_COMPLETE_DESTRUCTOR_P (decl))
3485 return sfk_complete_destructor;
3486 if (DECL_BASE_DESTRUCTOR_P (decl))
3487 return sfk_base_destructor;
3488 if (DECL_DELETING_DESTRUCTOR_P (decl))
3489 return sfk_deleting_destructor;
3490 if (DECL_CONV_FN_P (decl))
3491 return sfk_conversion;
3493 return sfk_none;
3496 /* Returns nonzero if TYPE is a character type, including wchar_t. */
3499 char_type_p (tree type)
3501 return (same_type_p (type, char_type_node)
3502 || same_type_p (type, unsigned_char_type_node)
3503 || same_type_p (type, signed_char_type_node)
3504 || same_type_p (type, char16_type_node)
3505 || same_type_p (type, char32_type_node)
3506 || same_type_p (type, wchar_type_node));
3509 /* Returns the kind of linkage associated with the indicated DECL. Th
3510 value returned is as specified by the language standard; it is
3511 independent of implementation details regarding template
3512 instantiation, etc. For example, it is possible that a declaration
3513 to which this function assigns external linkage would not show up
3514 as a global symbol when you run `nm' on the resulting object file. */
3516 linkage_kind
3517 decl_linkage (tree decl)
3519 /* This function doesn't attempt to calculate the linkage from first
3520 principles as given in [basic.link]. Instead, it makes use of
3521 the fact that we have already set TREE_PUBLIC appropriately, and
3522 then handles a few special cases. Ideally, we would calculate
3523 linkage first, and then transform that into a concrete
3524 implementation. */
3526 /* Things that don't have names have no linkage. */
3527 if (!DECL_NAME (decl))
3528 return lk_none;
3530 /* Fields have no linkage. */
3531 if (TREE_CODE (decl) == FIELD_DECL)
3532 return lk_none;
3534 /* Things that are TREE_PUBLIC have external linkage. */
3535 if (TREE_PUBLIC (decl))
3536 return lk_external;
3538 if (TREE_CODE (decl) == NAMESPACE_DECL)
3539 return lk_external;
3541 /* Linkage of a CONST_DECL depends on the linkage of the enumeration
3542 type. */
3543 if (TREE_CODE (decl) == CONST_DECL)
3544 return decl_linkage (TYPE_NAME (DECL_CONTEXT (decl)));
3546 /* Some things that are not TREE_PUBLIC have external linkage, too.
3547 For example, on targets that don't have weak symbols, we make all
3548 template instantiations have internal linkage (in the object
3549 file), but the symbols should still be treated as having external
3550 linkage from the point of view of the language. */
3551 if ((TREE_CODE (decl) == FUNCTION_DECL
3552 || TREE_CODE (decl) == VAR_DECL)
3553 && DECL_COMDAT (decl))
3554 return lk_external;
3556 /* Things in local scope do not have linkage, if they don't have
3557 TREE_PUBLIC set. */
3558 if (decl_function_context (decl))
3559 return lk_none;
3561 /* Members of the anonymous namespace also have TREE_PUBLIC unset, but
3562 are considered to have external linkage for language purposes. DECLs
3563 really meant to have internal linkage have DECL_THIS_STATIC set. */
3564 if (TREE_CODE (decl) == TYPE_DECL)
3565 return lk_external;
3566 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3568 if (!DECL_THIS_STATIC (decl))
3569 return lk_external;
3571 /* Static data members and static member functions from classes
3572 in anonymous namespace also don't have TREE_PUBLIC set. */
3573 if (DECL_CLASS_CONTEXT (decl))
3574 return lk_external;
3577 /* Everything else has internal linkage. */
3578 return lk_internal;
3581 /* Returns the storage duration of the object or reference associated with
3582 the indicated DECL, which should be a VAR_DECL or PARM_DECL. */
3584 duration_kind
3585 decl_storage_duration (tree decl)
3587 if (TREE_CODE (decl) == PARM_DECL)
3588 return dk_auto;
3589 if (TREE_CODE (decl) == FUNCTION_DECL)
3590 return dk_static;
3591 gcc_assert (TREE_CODE (decl) == VAR_DECL);
3592 if (!TREE_STATIC (decl)
3593 && !DECL_EXTERNAL (decl))
3594 return dk_auto;
3595 if (DECL_THREAD_LOCAL_P (decl))
3596 return dk_thread;
3597 return dk_static;
3600 /* EXP is an expression that we want to pre-evaluate. Returns (in
3601 *INITP) an expression that will perform the pre-evaluation. The
3602 value returned by this function is a side-effect free expression
3603 equivalent to the pre-evaluated expression. Callers must ensure
3604 that *INITP is evaluated before EXP. */
3606 tree
3607 stabilize_expr (tree exp, tree* initp)
3609 tree init_expr;
3611 if (!TREE_SIDE_EFFECTS (exp))
3612 init_expr = NULL_TREE;
3613 else if (VOID_TYPE_P (TREE_TYPE (exp)))
3615 init_expr = exp;
3616 exp = void_zero_node;
3618 /* There are no expressions with REFERENCE_TYPE, but there can be call
3619 arguments with such a type; just treat it as a pointer. */
3620 else if (TREE_CODE (TREE_TYPE (exp)) == REFERENCE_TYPE
3621 || SCALAR_TYPE_P (TREE_TYPE (exp))
3622 || !lvalue_or_rvalue_with_address_p (exp))
3624 init_expr = get_target_expr (exp);
3625 exp = TARGET_EXPR_SLOT (init_expr);
3627 else
3629 bool xval = !real_lvalue_p (exp);
3630 exp = cp_build_addr_expr (exp, tf_warning_or_error);
3631 init_expr = get_target_expr (exp);
3632 exp = TARGET_EXPR_SLOT (init_expr);
3633 exp = cp_build_indirect_ref (exp, RO_NULL, tf_warning_or_error);
3634 if (xval)
3635 exp = move (exp);
3637 *initp = init_expr;
3639 gcc_assert (!TREE_SIDE_EFFECTS (exp));
3640 return exp;
3643 /* Add NEW_EXPR, an expression whose value we don't care about, after the
3644 similar expression ORIG. */
3646 tree
3647 add_stmt_to_compound (tree orig, tree new_expr)
3649 if (!new_expr || !TREE_SIDE_EFFECTS (new_expr))
3650 return orig;
3651 if (!orig || !TREE_SIDE_EFFECTS (orig))
3652 return new_expr;
3653 return build2 (COMPOUND_EXPR, void_type_node, orig, new_expr);
3656 /* Like stabilize_expr, but for a call whose arguments we want to
3657 pre-evaluate. CALL is modified in place to use the pre-evaluated
3658 arguments, while, upon return, *INITP contains an expression to
3659 compute the arguments. */
3661 void
3662 stabilize_call (tree call, tree *initp)
3664 tree inits = NULL_TREE;
3665 int i;
3666 int nargs = call_expr_nargs (call);
3668 if (call == error_mark_node || processing_template_decl)
3670 *initp = NULL_TREE;
3671 return;
3674 gcc_assert (TREE_CODE (call) == CALL_EXPR);
3676 for (i = 0; i < nargs; i++)
3678 tree init;
3679 CALL_EXPR_ARG (call, i) =
3680 stabilize_expr (CALL_EXPR_ARG (call, i), &init);
3681 inits = add_stmt_to_compound (inits, init);
3684 *initp = inits;
3687 /* Like stabilize_expr, but for an AGGR_INIT_EXPR whose arguments we want
3688 to pre-evaluate. CALL is modified in place to use the pre-evaluated
3689 arguments, while, upon return, *INITP contains an expression to
3690 compute the arguments. */
3692 static void
3693 stabilize_aggr_init (tree call, tree *initp)
3695 tree inits = NULL_TREE;
3696 int i;
3697 int nargs = aggr_init_expr_nargs (call);
3699 if (call == error_mark_node)
3700 return;
3702 gcc_assert (TREE_CODE (call) == AGGR_INIT_EXPR);
3704 for (i = 0; i < nargs; i++)
3706 tree init;
3707 AGGR_INIT_EXPR_ARG (call, i) =
3708 stabilize_expr (AGGR_INIT_EXPR_ARG (call, i), &init);
3709 inits = add_stmt_to_compound (inits, init);
3712 *initp = inits;
3715 /* Like stabilize_expr, but for an initialization.
3717 If the initialization is for an object of class type, this function
3718 takes care not to introduce additional temporaries.
3720 Returns TRUE iff the expression was successfully pre-evaluated,
3721 i.e., if INIT is now side-effect free, except for, possibly, a
3722 single call to a constructor. */
3724 bool
3725 stabilize_init (tree init, tree *initp)
3727 tree t = init;
3729 *initp = NULL_TREE;
3731 if (t == error_mark_node || processing_template_decl)
3732 return true;
3734 if (TREE_CODE (t) == INIT_EXPR)
3735 t = TREE_OPERAND (t, 1);
3736 if (TREE_CODE (t) == TARGET_EXPR)
3737 t = TARGET_EXPR_INITIAL (t);
3739 /* If the RHS can be stabilized without breaking copy elision, stabilize
3740 it. We specifically don't stabilize class prvalues here because that
3741 would mean an extra copy, but they might be stabilized below. */
3742 if (TREE_CODE (init) == INIT_EXPR
3743 && TREE_CODE (t) != CONSTRUCTOR
3744 && TREE_CODE (t) != AGGR_INIT_EXPR
3745 && (SCALAR_TYPE_P (TREE_TYPE (t))
3746 || lvalue_or_rvalue_with_address_p (t)))
3748 TREE_OPERAND (init, 1) = stabilize_expr (t, initp);
3749 return true;
3752 if (TREE_CODE (t) == COMPOUND_EXPR
3753 && TREE_CODE (init) == INIT_EXPR)
3755 tree last = expr_last (t);
3756 /* Handle stabilizing the EMPTY_CLASS_EXPR pattern. */
3757 if (!TREE_SIDE_EFFECTS (last))
3759 *initp = t;
3760 TREE_OPERAND (init, 1) = last;
3761 return true;
3765 if (TREE_CODE (t) == CONSTRUCTOR)
3767 /* Aggregate initialization: stabilize each of the field
3768 initializers. */
3769 unsigned i;
3770 constructor_elt *ce;
3771 bool good = true;
3772 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
3773 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
3775 tree type = TREE_TYPE (ce->value);
3776 tree subinit;
3777 if (TREE_CODE (type) == REFERENCE_TYPE
3778 || SCALAR_TYPE_P (type))
3779 ce->value = stabilize_expr (ce->value, &subinit);
3780 else if (!stabilize_init (ce->value, &subinit))
3781 good = false;
3782 *initp = add_stmt_to_compound (*initp, subinit);
3784 return good;
3787 if (TREE_CODE (t) == CALL_EXPR)
3789 stabilize_call (t, initp);
3790 return true;
3793 if (TREE_CODE (t) == AGGR_INIT_EXPR)
3795 stabilize_aggr_init (t, initp);
3796 return true;
3799 /* The initialization is being performed via a bitwise copy -- and
3800 the item copied may have side effects. */
3801 return !TREE_SIDE_EFFECTS (init);
3804 /* Like "fold", but should be used whenever we might be processing the
3805 body of a template. */
3807 tree
3808 fold_if_not_in_template (tree expr)
3810 /* In the body of a template, there is never any need to call
3811 "fold". We will call fold later when actually instantiating the
3812 template. Integral constant expressions in templates will be
3813 evaluated via fold_non_dependent_expr, as necessary. */
3814 if (processing_template_decl)
3815 return expr;
3817 /* Fold C++ front-end specific tree codes. */
3818 if (TREE_CODE (expr) == UNARY_PLUS_EXPR)
3819 return fold_convert (TREE_TYPE (expr), TREE_OPERAND (expr, 0));
3821 return fold (expr);
3824 /* Returns true if a cast to TYPE may appear in an integral constant
3825 expression. */
3827 bool
3828 cast_valid_in_integral_constant_expression_p (tree type)
3830 return (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3831 || cxx_dialect >= cxx0x
3832 || dependent_type_p (type)
3833 || type == error_mark_node);
3836 /* Return true if we need to fix linkage information of DECL. */
3838 static bool
3839 cp_fix_function_decl_p (tree decl)
3841 /* Skip if DECL is not externally visible. */
3842 if (!TREE_PUBLIC (decl))
3843 return false;
3845 /* We need to fix DECL if it a appears to be exported but with no
3846 function body. Thunks do not have CFGs and we may need to
3847 handle them specially later. */
3848 if (!gimple_has_body_p (decl)
3849 && !DECL_THUNK_P (decl)
3850 && !DECL_EXTERNAL (decl))
3852 struct cgraph_node *node = cgraph_get_node (decl);
3854 /* Don't fix same_body aliases. Although they don't have their own
3855 CFG, they share it with what they alias to. */
3856 if (!node || !node->alias
3857 || !vec_safe_length (node->symbol.ref_list.references))
3858 return true;
3861 return false;
3864 /* Clean the C++ specific parts of the tree T. */
3866 void
3867 cp_free_lang_data (tree t)
3869 if (TREE_CODE (t) == METHOD_TYPE
3870 || TREE_CODE (t) == FUNCTION_TYPE)
3872 /* Default args are not interesting anymore. */
3873 tree argtypes = TYPE_ARG_TYPES (t);
3874 while (argtypes)
3876 TREE_PURPOSE (argtypes) = 0;
3877 argtypes = TREE_CHAIN (argtypes);
3880 else if (TREE_CODE (t) == FUNCTION_DECL
3881 && cp_fix_function_decl_p (t))
3883 /* If T is used in this translation unit at all, the definition
3884 must exist somewhere else since we have decided to not emit it
3885 in this TU. So make it an external reference. */
3886 DECL_EXTERNAL (t) = 1;
3887 TREE_STATIC (t) = 0;
3889 if (TREE_CODE (t) == NAMESPACE_DECL)
3891 /* The list of users of a namespace isn't useful for the middle-end
3892 or debug generators. */
3893 DECL_NAMESPACE_USERS (t) = NULL_TREE;
3894 /* Neither do we need the leftover chaining of namespaces
3895 from the binding level. */
3896 DECL_CHAIN (t) = NULL_TREE;
3900 /* Stub for c-common. Please keep in sync with c-decl.c.
3901 FIXME: If address space support is target specific, then this
3902 should be a C target hook. But currently this is not possible,
3903 because this function is called via REGISTER_TARGET_PRAGMAS. */
3904 void
3905 c_register_addr_space (const char * /*word*/, addr_space_t /*as*/)
3909 /* Return the number of operands in T that we care about for things like
3910 mangling. */
3913 cp_tree_operand_length (const_tree t)
3915 enum tree_code code = TREE_CODE (t);
3917 switch (code)
3919 case PREINCREMENT_EXPR:
3920 case PREDECREMENT_EXPR:
3921 case POSTINCREMENT_EXPR:
3922 case POSTDECREMENT_EXPR:
3923 return 1;
3925 case ARRAY_REF:
3926 return 2;
3928 case EXPR_PACK_EXPANSION:
3929 return 1;
3931 default:
3932 return TREE_OPERAND_LENGTH (t);
3936 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
3937 /* Complain that some language-specific thing hanging off a tree
3938 node has been accessed improperly. */
3940 void
3941 lang_check_failed (const char* file, int line, const char* function)
3943 internal_error ("lang_* check: failed in %s, at %s:%d",
3944 function, trim_filename (file), line);
3946 #endif /* ENABLE_TREE_CHECKING */
3948 #include "gt-cp-tree.h"