graph.c (init_graph_slim_pretty_print): Remove.
[official-gcc.git] / gcc / cp / tree.c
blobf8b4bbce822144cb3399078346d3096178171332
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 && !VAR_P (ref)
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 ARRAY_NOTATION_REF:
145 case PARM_DECL:
146 case RESULT_DECL:
147 return clk_ordinary;
149 /* A scope ref in a template, left as SCOPE_REF to support later
150 access checking. */
151 case SCOPE_REF:
152 gcc_assert (!type_dependent_expression_p (CONST_CAST_TREE (ref)));
154 tree op = TREE_OPERAND (ref, 1);
155 if (TREE_CODE (op) == FIELD_DECL)
156 return (DECL_C_BIT_FIELD (op) ? clk_bitfield : clk_ordinary);
157 else
158 return lvalue_kind (op);
161 case MAX_EXPR:
162 case MIN_EXPR:
163 /* Disallow <? and >? as lvalues if either argument side-effects. */
164 if (TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 0))
165 || TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 1)))
166 return clk_none;
167 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
168 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1));
169 break;
171 case COND_EXPR:
172 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1)
173 ? TREE_OPERAND (ref, 1)
174 : TREE_OPERAND (ref, 0));
175 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 2));
176 break;
178 case MODIFY_EXPR:
179 case TYPEID_EXPR:
180 return clk_ordinary;
182 case COMPOUND_EXPR:
183 return lvalue_kind (TREE_OPERAND (ref, 1));
185 case TARGET_EXPR:
186 return clk_class;
188 case VA_ARG_EXPR:
189 return (CLASS_TYPE_P (TREE_TYPE (ref)) ? clk_class : clk_none);
191 case CALL_EXPR:
192 /* We can see calls outside of TARGET_EXPR in templates. */
193 if (CLASS_TYPE_P (TREE_TYPE (ref)))
194 return clk_class;
195 return clk_none;
197 case FUNCTION_DECL:
198 /* All functions (except non-static-member functions) are
199 lvalues. */
200 return (DECL_NONSTATIC_MEMBER_FUNCTION_P (ref)
201 ? clk_none : clk_ordinary);
203 case BASELINK:
204 /* We now represent a reference to a single static member function
205 with a BASELINK. */
206 /* This CONST_CAST is okay because BASELINK_FUNCTIONS returns
207 its argument unmodified and we assign it to a const_tree. */
208 return lvalue_kind (BASELINK_FUNCTIONS (CONST_CAST_TREE (ref)));
210 case NON_DEPENDENT_EXPR:
211 /* We just return clk_ordinary for NON_DEPENDENT_EXPR in C++98, but
212 in C++11 lvalues don't bind to rvalue references, so we need to
213 work harder to avoid bogus errors (c++/44870). */
214 if (cxx_dialect < cxx11)
215 return clk_ordinary;
216 else
217 return lvalue_kind (TREE_OPERAND (ref, 0));
219 default:
220 if (!TREE_TYPE (ref))
221 return clk_none;
222 if (CLASS_TYPE_P (TREE_TYPE (ref)))
223 return clk_class;
224 break;
227 /* If one operand is not an lvalue at all, then this expression is
228 not an lvalue. */
229 if (!op1_lvalue_kind || !op2_lvalue_kind)
230 return clk_none;
232 /* Otherwise, it's an lvalue, and it has all the odd properties
233 contributed by either operand. */
234 op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind;
235 /* It's not an ordinary lvalue if it involves any other kind. */
236 if ((op1_lvalue_kind & ~clk_ordinary) != clk_none)
237 op1_lvalue_kind &= ~clk_ordinary;
238 /* It can't be both a pseudo-lvalue and a non-addressable lvalue.
239 A COND_EXPR of those should be wrapped in a TARGET_EXPR. */
240 if ((op1_lvalue_kind & (clk_rvalueref|clk_class))
241 && (op1_lvalue_kind & (clk_bitfield|clk_packed)))
242 op1_lvalue_kind = clk_none;
243 return op1_lvalue_kind;
246 /* Returns the kind of lvalue that REF is, in the sense of
247 [basic.lval]. This function should really be named lvalue_p; it
248 computes the C++ definition of lvalue. */
250 cp_lvalue_kind
251 real_lvalue_p (const_tree ref)
253 cp_lvalue_kind kind = lvalue_kind (ref);
254 if (kind & (clk_rvalueref|clk_class))
255 return clk_none;
256 else
257 return kind;
260 /* This differs from real_lvalue_p in that class rvalues are considered
261 lvalues. */
263 bool
264 lvalue_p (const_tree ref)
266 return (lvalue_kind (ref) != clk_none);
269 /* This differs from real_lvalue_p in that rvalues formed by dereferencing
270 rvalue references are considered rvalues. */
272 bool
273 lvalue_or_rvalue_with_address_p (const_tree ref)
275 cp_lvalue_kind kind = lvalue_kind (ref);
276 if (kind & clk_class)
277 return false;
278 else
279 return (kind != clk_none);
282 /* Returns true if REF is an xvalue, false otherwise. */
284 bool
285 xvalue_p (const_tree ref)
287 return (lvalue_kind (ref) == clk_rvalueref);
290 /* Test whether DECL is a builtin that may appear in a
291 constant-expression. */
293 bool
294 builtin_valid_in_constant_expr_p (const_tree decl)
296 /* At present BUILT_IN_CONSTANT_P is the only builtin we're allowing
297 in constant-expressions. We may want to add other builtins later. */
298 return DECL_IS_BUILTIN_CONSTANT_P (decl);
301 /* Build a TARGET_EXPR, initializing the DECL with the VALUE. */
303 static tree
304 build_target_expr (tree decl, tree value, tsubst_flags_t complain)
306 tree t;
307 tree type = TREE_TYPE (decl);
309 #ifdef ENABLE_CHECKING
310 gcc_assert (VOID_TYPE_P (TREE_TYPE (value))
311 || TREE_TYPE (decl) == TREE_TYPE (value)
312 /* On ARM ctors return 'this'. */
313 || (TYPE_PTR_P (TREE_TYPE (value))
314 && TREE_CODE (value) == CALL_EXPR)
315 || useless_type_conversion_p (TREE_TYPE (decl),
316 TREE_TYPE (value)));
317 #endif
319 t = cxx_maybe_build_cleanup (decl, complain);
320 if (t == error_mark_node)
321 return error_mark_node;
322 t = build4 (TARGET_EXPR, type, decl, value, t, NULL_TREE);
323 /* We always set TREE_SIDE_EFFECTS so that expand_expr does not
324 ignore the TARGET_EXPR. If there really turn out to be no
325 side-effects, then the optimizer should be able to get rid of
326 whatever code is generated anyhow. */
327 TREE_SIDE_EFFECTS (t) = 1;
329 return t;
332 /* Return an undeclared local temporary of type TYPE for use in building a
333 TARGET_EXPR. */
335 static tree
336 build_local_temp (tree type)
338 tree slot = build_decl (input_location,
339 VAR_DECL, NULL_TREE, type);
340 DECL_ARTIFICIAL (slot) = 1;
341 DECL_IGNORED_P (slot) = 1;
342 DECL_CONTEXT (slot) = current_function_decl;
343 layout_decl (slot, 0);
344 return slot;
347 /* Set various status flags when building an AGGR_INIT_EXPR object T. */
349 static void
350 process_aggr_init_operands (tree t)
352 bool side_effects;
354 side_effects = TREE_SIDE_EFFECTS (t);
355 if (!side_effects)
357 int i, n;
358 n = TREE_OPERAND_LENGTH (t);
359 for (i = 1; i < n; i++)
361 tree op = TREE_OPERAND (t, i);
362 if (op && TREE_SIDE_EFFECTS (op))
364 side_effects = 1;
365 break;
369 TREE_SIDE_EFFECTS (t) = side_effects;
372 /* Build an AGGR_INIT_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE,
373 FN, and SLOT. NARGS is the number of call arguments which are specified
374 as a tree array ARGS. */
376 static tree
377 build_aggr_init_array (tree return_type, tree fn, tree slot, int nargs,
378 tree *args)
380 tree t;
381 int i;
383 t = build_vl_exp (AGGR_INIT_EXPR, nargs + 3);
384 TREE_TYPE (t) = return_type;
385 AGGR_INIT_EXPR_FN (t) = fn;
386 AGGR_INIT_EXPR_SLOT (t) = slot;
387 for (i = 0; i < nargs; i++)
388 AGGR_INIT_EXPR_ARG (t, i) = args[i];
389 process_aggr_init_operands (t);
390 return t;
393 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
394 target. TYPE is the type to be initialized.
396 Build an AGGR_INIT_EXPR to represent the initialization. This function
397 differs from build_cplus_new in that an AGGR_INIT_EXPR can only be used
398 to initialize another object, whereas a TARGET_EXPR can either
399 initialize another object or create its own temporary object, and as a
400 result building up a TARGET_EXPR requires that the type's destructor be
401 callable. */
403 tree
404 build_aggr_init_expr (tree type, tree init)
406 tree fn;
407 tree slot;
408 tree rval;
409 int is_ctor;
411 /* Don't build AGGR_INIT_EXPR in a template. */
412 if (processing_template_decl)
413 return init;
415 if (TREE_CODE (init) == CALL_EXPR)
416 fn = CALL_EXPR_FN (init);
417 else if (TREE_CODE (init) == AGGR_INIT_EXPR)
418 fn = AGGR_INIT_EXPR_FN (init);
419 else
420 return convert (type, init);
422 is_ctor = (TREE_CODE (fn) == ADDR_EXPR
423 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
424 && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0)));
426 /* We split the CALL_EXPR into its function and its arguments here.
427 Then, in expand_expr, we put them back together. The reason for
428 this is that this expression might be a default argument
429 expression. In that case, we need a new temporary every time the
430 expression is used. That's what break_out_target_exprs does; it
431 replaces every AGGR_INIT_EXPR with a copy that uses a fresh
432 temporary slot. Then, expand_expr builds up a call-expression
433 using the new slot. */
435 /* If we don't need to use a constructor to create an object of this
436 type, don't mess with AGGR_INIT_EXPR. */
437 if (is_ctor || TREE_ADDRESSABLE (type))
439 slot = build_local_temp (type);
441 if (TREE_CODE(init) == CALL_EXPR)
442 rval = build_aggr_init_array (void_type_node, fn, slot,
443 call_expr_nargs (init),
444 CALL_EXPR_ARGP (init));
445 else
446 rval = build_aggr_init_array (void_type_node, fn, slot,
447 aggr_init_expr_nargs (init),
448 AGGR_INIT_EXPR_ARGP (init));
449 TREE_SIDE_EFFECTS (rval) = 1;
450 AGGR_INIT_VIA_CTOR_P (rval) = is_ctor;
451 TREE_NOTHROW (rval) = TREE_NOTHROW (init);
453 else
454 rval = init;
456 return rval;
459 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
460 target. TYPE is the type that this initialization should appear to
461 have.
463 Build an encapsulation of the initialization to perform
464 and return it so that it can be processed by language-independent
465 and language-specific expression expanders. */
467 tree
468 build_cplus_new (tree type, tree init, tsubst_flags_t complain)
470 tree rval = build_aggr_init_expr (type, init);
471 tree slot;
473 if (!complete_type_or_maybe_complain (type, init, complain))
474 return error_mark_node;
476 /* Make sure that we're not trying to create an instance of an
477 abstract class. */
478 if (abstract_virtuals_error_sfinae (NULL_TREE, type, complain))
479 return error_mark_node;
481 if (TREE_CODE (rval) == AGGR_INIT_EXPR)
482 slot = AGGR_INIT_EXPR_SLOT (rval);
483 else if (TREE_CODE (rval) == CALL_EXPR
484 || TREE_CODE (rval) == CONSTRUCTOR)
485 slot = build_local_temp (type);
486 else
487 return rval;
489 rval = build_target_expr (slot, rval, complain);
491 if (rval != error_mark_node)
492 TARGET_EXPR_IMPLICIT_P (rval) = 1;
494 return rval;
497 /* Subroutine of build_vec_init_expr: Build up a single element
498 intialization as a proxy for the full array initialization to get things
499 marked as used and any appropriate diagnostics.
501 Since we're deferring building the actual constructor calls until
502 gimplification time, we need to build one now and throw it away so
503 that the relevant constructor gets mark_used before cgraph decides
504 what functions are needed. Here we assume that init is either
505 NULL_TREE, void_type_node (indicating value-initialization), or
506 another array to copy. */
508 static tree
509 build_vec_init_elt (tree type, tree init, tsubst_flags_t complain)
511 tree inner_type = strip_array_types (type);
512 vec<tree, va_gc> *argvec;
514 if (integer_zerop (array_type_nelts_total (type))
515 || !CLASS_TYPE_P (inner_type))
516 /* No interesting initialization to do. */
517 return integer_zero_node;
518 else if (init == void_type_node)
519 return build_value_init (inner_type, complain);
521 gcc_assert (init == NULL_TREE
522 || (same_type_ignoring_top_level_qualifiers_p
523 (type, TREE_TYPE (init))));
525 argvec = make_tree_vector ();
526 if (init)
528 tree init_type = strip_array_types (TREE_TYPE (init));
529 tree dummy = build_dummy_object (init_type);
530 if (!real_lvalue_p (init))
531 dummy = move (dummy);
532 argvec->quick_push (dummy);
534 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
535 &argvec, inner_type, LOOKUP_NORMAL,
536 complain);
537 release_tree_vector (argvec);
539 /* For a trivial constructor, build_over_call creates a TARGET_EXPR. But
540 we don't want one here because we aren't creating a temporary. */
541 if (TREE_CODE (init) == TARGET_EXPR)
542 init = TARGET_EXPR_INITIAL (init);
544 return init;
547 /* Return a TARGET_EXPR which expresses the initialization of an array to
548 be named later, either default-initialization or copy-initialization
549 from another array of the same type. */
551 tree
552 build_vec_init_expr (tree type, tree init, tsubst_flags_t complain)
554 tree slot;
555 bool value_init = false;
556 tree elt_init = build_vec_init_elt (type, init, complain);
558 if (init == void_type_node)
560 value_init = true;
561 init = NULL_TREE;
564 slot = build_local_temp (type);
565 init = build2 (VEC_INIT_EXPR, type, slot, init);
566 TREE_SIDE_EFFECTS (init) = true;
567 SET_EXPR_LOCATION (init, input_location);
569 if (cxx_dialect >= cxx11
570 && potential_constant_expression (elt_init))
571 VEC_INIT_EXPR_IS_CONSTEXPR (init) = true;
572 VEC_INIT_EXPR_VALUE_INIT (init) = value_init;
574 return init;
577 /* Give a helpful diagnostic for a non-constexpr VEC_INIT_EXPR in a context
578 that requires a constant expression. */
580 void
581 diagnose_non_constexpr_vec_init (tree expr)
583 tree type = TREE_TYPE (VEC_INIT_EXPR_SLOT (expr));
584 tree init, elt_init;
585 if (VEC_INIT_EXPR_VALUE_INIT (expr))
586 init = void_type_node;
587 else
588 init = VEC_INIT_EXPR_INIT (expr);
590 elt_init = build_vec_init_elt (type, init, tf_warning_or_error);
591 require_potential_constant_expression (elt_init);
594 tree
595 build_array_copy (tree init)
597 return build_vec_init_expr (TREE_TYPE (init), init, tf_warning_or_error);
600 /* Build a TARGET_EXPR using INIT to initialize a new temporary of the
601 indicated TYPE. */
603 tree
604 build_target_expr_with_type (tree init, tree type, tsubst_flags_t complain)
606 gcc_assert (!VOID_TYPE_P (type));
608 if (TREE_CODE (init) == TARGET_EXPR
609 || init == error_mark_node)
610 return init;
611 else if (CLASS_TYPE_P (type) && type_has_nontrivial_copy_init (type)
612 && !VOID_TYPE_P (TREE_TYPE (init))
613 && TREE_CODE (init) != COND_EXPR
614 && TREE_CODE (init) != CONSTRUCTOR
615 && TREE_CODE (init) != VA_ARG_EXPR)
616 /* We need to build up a copy constructor call. A void initializer
617 means we're being called from bot_manip. COND_EXPR is a special
618 case because we already have copies on the arms and we don't want
619 another one here. A CONSTRUCTOR is aggregate initialization, which
620 is handled separately. A VA_ARG_EXPR is magic creation of an
621 aggregate; there's no additional work to be done. */
622 return force_rvalue (init, complain);
624 return force_target_expr (type, init, complain);
627 /* Like the above function, but without the checking. This function should
628 only be used by code which is deliberately trying to subvert the type
629 system, such as call_builtin_trap. Or build_over_call, to avoid
630 infinite recursion. */
632 tree
633 force_target_expr (tree type, tree init, tsubst_flags_t complain)
635 tree slot;
637 gcc_assert (!VOID_TYPE_P (type));
639 slot = build_local_temp (type);
640 return build_target_expr (slot, init, complain);
643 /* Like build_target_expr_with_type, but use the type of INIT. */
645 tree
646 get_target_expr_sfinae (tree init, tsubst_flags_t complain)
648 if (TREE_CODE (init) == AGGR_INIT_EXPR)
649 return build_target_expr (AGGR_INIT_EXPR_SLOT (init), init, complain);
650 else if (TREE_CODE (init) == VEC_INIT_EXPR)
651 return build_target_expr (VEC_INIT_EXPR_SLOT (init), init, complain);
652 else
653 return build_target_expr_with_type (init, TREE_TYPE (init), complain);
656 tree
657 get_target_expr (tree init)
659 return get_target_expr_sfinae (init, tf_warning_or_error);
662 /* If EXPR is a bitfield reference, convert it to the declared type of
663 the bitfield, and return the resulting expression. Otherwise,
664 return EXPR itself. */
666 tree
667 convert_bitfield_to_declared_type (tree expr)
669 tree bitfield_type;
671 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
672 if (bitfield_type)
673 expr = convert_to_integer (TYPE_MAIN_VARIANT (bitfield_type),
674 expr);
675 return expr;
678 /* EXPR is being used in an rvalue context. Return a version of EXPR
679 that is marked as an rvalue. */
681 tree
682 rvalue (tree expr)
684 tree type;
686 if (error_operand_p (expr))
687 return expr;
689 expr = mark_rvalue_use (expr);
691 /* [basic.lval]
693 Non-class rvalues always have cv-unqualified types. */
694 type = TREE_TYPE (expr);
695 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
696 type = cv_unqualified (type);
698 /* We need to do this for rvalue refs as well to get the right answer
699 from decltype; see c++/36628. */
700 if (!processing_template_decl && lvalue_or_rvalue_with_address_p (expr))
701 expr = build1 (NON_LVALUE_EXPR, type, expr);
702 else if (type != TREE_TYPE (expr))
703 expr = build_nop (type, expr);
705 return expr;
709 /* Hash an ARRAY_TYPE. K is really of type `tree'. */
711 static hashval_t
712 cplus_array_hash (const void* k)
714 hashval_t hash;
715 const_tree const t = (const_tree) k;
717 hash = TYPE_UID (TREE_TYPE (t));
718 if (TYPE_DOMAIN (t))
719 hash ^= TYPE_UID (TYPE_DOMAIN (t));
720 return hash;
723 typedef struct cplus_array_info {
724 tree type;
725 tree domain;
726 } cplus_array_info;
728 /* Compare two ARRAY_TYPEs. K1 is really of type `tree', K2 is really
729 of type `cplus_array_info*'. */
731 static int
732 cplus_array_compare (const void * k1, const void * k2)
734 const_tree const t1 = (const_tree) k1;
735 const cplus_array_info *const t2 = (const cplus_array_info*) k2;
737 return (TREE_TYPE (t1) == t2->type && TYPE_DOMAIN (t1) == t2->domain);
740 /* Hash table containing dependent array types, which are unsuitable for
741 the language-independent type hash table. */
742 static GTY ((param_is (union tree_node))) htab_t cplus_array_htab;
744 /* Like build_array_type, but handle special C++ semantics. */
746 tree
747 build_cplus_array_type (tree elt_type, tree index_type)
749 tree t;
751 if (elt_type == error_mark_node || index_type == error_mark_node)
752 return error_mark_node;
754 if (processing_template_decl
755 && (dependent_type_p (elt_type)
756 || (index_type && !TREE_CONSTANT (TYPE_MAX_VALUE (index_type)))))
758 void **e;
759 cplus_array_info cai;
760 hashval_t hash;
762 if (cplus_array_htab == NULL)
763 cplus_array_htab = htab_create_ggc (61, &cplus_array_hash,
764 &cplus_array_compare, NULL);
766 hash = TYPE_UID (elt_type);
767 if (index_type)
768 hash ^= TYPE_UID (index_type);
769 cai.type = elt_type;
770 cai.domain = index_type;
772 e = htab_find_slot_with_hash (cplus_array_htab, &cai, hash, INSERT);
773 if (*e)
774 /* We have found the type: we're done. */
775 return (tree) *e;
776 else
778 /* Build a new array type. */
779 t = cxx_make_type (ARRAY_TYPE);
780 TREE_TYPE (t) = elt_type;
781 TYPE_DOMAIN (t) = index_type;
783 /* Store it in the hash table. */
784 *e = t;
786 /* Set the canonical type for this new node. */
787 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
788 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
789 SET_TYPE_STRUCTURAL_EQUALITY (t);
790 else if (TYPE_CANONICAL (elt_type) != elt_type
791 || (index_type
792 && TYPE_CANONICAL (index_type) != index_type))
793 TYPE_CANONICAL (t)
794 = build_cplus_array_type
795 (TYPE_CANONICAL (elt_type),
796 index_type ? TYPE_CANONICAL (index_type) : index_type);
797 else
798 TYPE_CANONICAL (t) = t;
801 else
803 if (!TYPE_STRUCTURAL_EQUALITY_P (elt_type)
804 && !(index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type))
805 && (TYPE_CANONICAL (elt_type) != elt_type
806 || (index_type && TYPE_CANONICAL (index_type) != index_type)))
807 /* Make sure that the canonical type is on the appropriate
808 variants list. */
809 build_cplus_array_type
810 (TYPE_CANONICAL (elt_type),
811 index_type ? TYPE_CANONICAL (index_type) : index_type);
812 t = build_array_type (elt_type, index_type);
815 /* Push these needs up so that initialization takes place
816 more easily. */
817 bool needs_ctor
818 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (elt_type));
819 TYPE_NEEDS_CONSTRUCTING (t) = needs_ctor;
820 bool needs_dtor
821 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (elt_type));
822 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = needs_dtor;
824 /* We want TYPE_MAIN_VARIANT of an array to strip cv-quals from the
825 element type as well, so fix it up if needed. */
826 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
828 tree m = build_cplus_array_type (TYPE_MAIN_VARIANT (elt_type),
829 index_type);
831 if (TYPE_MAIN_VARIANT (t) != m)
833 if (COMPLETE_TYPE_P (TREE_TYPE (t)) && !COMPLETE_TYPE_P (m))
835 /* m was built before the element type was complete, so we
836 also need to copy the layout info from t. We might
837 end up doing this multiple times if t is an array of
838 unknown bound. */
839 tree size = TYPE_SIZE (t);
840 tree size_unit = TYPE_SIZE_UNIT (t);
841 unsigned int align = TYPE_ALIGN (t);
842 unsigned int user_align = TYPE_USER_ALIGN (t);
843 enum machine_mode mode = TYPE_MODE (t);
844 for (tree var = m; var; var = TYPE_NEXT_VARIANT (var))
846 TYPE_SIZE (var) = size;
847 TYPE_SIZE_UNIT (var) = size_unit;
848 TYPE_ALIGN (var) = align;
849 TYPE_USER_ALIGN (var) = user_align;
850 SET_TYPE_MODE (var, mode);
851 TYPE_NEEDS_CONSTRUCTING (var) = needs_ctor;
852 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (var) = needs_dtor;
856 TYPE_MAIN_VARIANT (t) = m;
857 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
858 TYPE_NEXT_VARIANT (m) = t;
862 /* Avoid spurious warnings with VLAs (c++/54583). */
863 if (TYPE_SIZE (t) && EXPR_P (TYPE_SIZE (t)))
864 TREE_NO_WARNING (TYPE_SIZE (t)) = 1;
866 return t;
869 /* Return an ARRAY_TYPE with element type ELT and length N. */
871 tree
872 build_array_of_n_type (tree elt, int n)
874 return build_cplus_array_type (elt, build_index_type (size_int (n - 1)));
877 /* True iff T is a C++1y array of runtime bound (VLA). */
879 bool
880 array_of_runtime_bound_p (tree t)
882 if (!t || TREE_CODE (t) != ARRAY_TYPE)
883 return false;
884 tree dom = TYPE_DOMAIN (t);
885 if (!dom)
886 return false;
887 tree max = TYPE_MAX_VALUE (dom);
888 return (!potential_rvalue_constant_expression (max)
889 || (!value_dependent_expression_p (max) && !TREE_CONSTANT (max)));
892 /* Return a reference type node referring to TO_TYPE. If RVAL is
893 true, return an rvalue reference type, otherwise return an lvalue
894 reference type. If a type node exists, reuse it, otherwise create
895 a new one. */
896 tree
897 cp_build_reference_type (tree to_type, bool rval)
899 tree lvalue_ref, t;
900 lvalue_ref = build_reference_type (to_type);
901 if (!rval)
902 return lvalue_ref;
904 /* This code to create rvalue reference types is based on and tied
905 to the code creating lvalue reference types in the middle-end
906 functions build_reference_type_for_mode and build_reference_type.
908 It works by putting the rvalue reference type nodes after the
909 lvalue reference nodes in the TYPE_NEXT_REF_TO linked list, so
910 they will effectively be ignored by the middle end. */
912 for (t = lvalue_ref; (t = TYPE_NEXT_REF_TO (t)); )
913 if (TYPE_REF_IS_RVALUE (t))
914 return t;
916 t = build_distinct_type_copy (lvalue_ref);
918 TYPE_REF_IS_RVALUE (t) = true;
919 TYPE_NEXT_REF_TO (t) = TYPE_NEXT_REF_TO (lvalue_ref);
920 TYPE_NEXT_REF_TO (lvalue_ref) = t;
922 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
923 SET_TYPE_STRUCTURAL_EQUALITY (t);
924 else if (TYPE_CANONICAL (to_type) != to_type)
925 TYPE_CANONICAL (t)
926 = cp_build_reference_type (TYPE_CANONICAL (to_type), rval);
927 else
928 TYPE_CANONICAL (t) = t;
930 layout_type (t);
932 return t;
936 /* Returns EXPR cast to rvalue reference type, like std::move. */
938 tree
939 move (tree expr)
941 tree type = TREE_TYPE (expr);
942 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
943 type = cp_build_reference_type (type, /*rval*/true);
944 return build_static_cast (type, expr, tf_warning_or_error);
947 /* Used by the C++ front end to build qualified array types. However,
948 the C version of this function does not properly maintain canonical
949 types (which are not used in C). */
950 tree
951 c_build_qualified_type (tree type, int type_quals)
953 return cp_build_qualified_type (type, type_quals);
957 /* Make a variant of TYPE, qualified with the TYPE_QUALS. Handles
958 arrays correctly. In particular, if TYPE is an array of T's, and
959 TYPE_QUALS is non-empty, returns an array of qualified T's.
961 FLAGS determines how to deal with ill-formed qualifications. If
962 tf_ignore_bad_quals is set, then bad qualifications are dropped
963 (this is permitted if TYPE was introduced via a typedef or template
964 type parameter). If bad qualifications are dropped and tf_warning
965 is set, then a warning is issued for non-const qualifications. If
966 tf_ignore_bad_quals is not set and tf_error is not set, we
967 return error_mark_node. Otherwise, we issue an error, and ignore
968 the qualifications.
970 Qualification of a reference type is valid when the reference came
971 via a typedef or template type argument. [dcl.ref] No such
972 dispensation is provided for qualifying a function type. [dcl.fct]
973 DR 295 queries this and the proposed resolution brings it into line
974 with qualifying a reference. We implement the DR. We also behave
975 in a similar manner for restricting non-pointer types. */
977 tree
978 cp_build_qualified_type_real (tree type,
979 int type_quals,
980 tsubst_flags_t complain)
982 tree result;
983 int bad_quals = TYPE_UNQUALIFIED;
985 if (type == error_mark_node)
986 return type;
988 if (type_quals == cp_type_quals (type))
989 return type;
991 if (TREE_CODE (type) == ARRAY_TYPE)
993 /* In C++, the qualification really applies to the array element
994 type. Obtain the appropriately qualified element type. */
995 tree t;
996 tree element_type
997 = cp_build_qualified_type_real (TREE_TYPE (type),
998 type_quals,
999 complain);
1001 if (element_type == error_mark_node)
1002 return error_mark_node;
1004 /* See if we already have an identically qualified type. Tests
1005 should be equivalent to those in check_qualified_type. */
1006 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1007 if (TREE_TYPE (t) == element_type
1008 && TYPE_NAME (t) == TYPE_NAME (type)
1009 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
1010 && attribute_list_equal (TYPE_ATTRIBUTES (t),
1011 TYPE_ATTRIBUTES (type)))
1012 break;
1014 if (!t)
1016 t = build_cplus_array_type (element_type, TYPE_DOMAIN (type));
1018 /* Keep the typedef name. */
1019 if (TYPE_NAME (t) != TYPE_NAME (type))
1021 t = build_variant_type_copy (t);
1022 TYPE_NAME (t) = TYPE_NAME (type);
1026 /* Even if we already had this variant, we update
1027 TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case
1028 they changed since the variant was originally created.
1030 This seems hokey; if there is some way to use a previous
1031 variant *without* coming through here,
1032 TYPE_NEEDS_CONSTRUCTING will never be updated. */
1033 TYPE_NEEDS_CONSTRUCTING (t)
1034 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
1035 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1036 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
1037 return t;
1039 else if (TYPE_PTRMEMFUNC_P (type))
1041 /* For a pointer-to-member type, we can't just return a
1042 cv-qualified version of the RECORD_TYPE. If we do, we
1043 haven't changed the field that contains the actual pointer to
1044 a method, and so TYPE_PTRMEMFUNC_FN_TYPE will be wrong. */
1045 tree t;
1047 t = TYPE_PTRMEMFUNC_FN_TYPE (type);
1048 t = cp_build_qualified_type_real (t, type_quals, complain);
1049 return build_ptrmemfunc_type (t);
1051 else if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
1053 tree t = PACK_EXPANSION_PATTERN (type);
1055 t = cp_build_qualified_type_real (t, type_quals, complain);
1056 return make_pack_expansion (t);
1059 /* A reference or method type shall not be cv-qualified.
1060 [dcl.ref], [dcl.fct]. This used to be an error, but as of DR 295
1061 (in CD1) we always ignore extra cv-quals on functions. */
1062 if (type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)
1063 && (TREE_CODE (type) == REFERENCE_TYPE
1064 || TREE_CODE (type) == FUNCTION_TYPE
1065 || TREE_CODE (type) == METHOD_TYPE))
1067 if (TREE_CODE (type) == REFERENCE_TYPE)
1068 bad_quals |= type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1069 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1072 /* But preserve any function-cv-quals on a FUNCTION_TYPE. */
1073 if (TREE_CODE (type) == FUNCTION_TYPE)
1074 type_quals |= type_memfn_quals (type);
1076 /* A restrict-qualified type must be a pointer (or reference)
1077 to object or incomplete type. */
1078 if ((type_quals & TYPE_QUAL_RESTRICT)
1079 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1080 && TREE_CODE (type) != TYPENAME_TYPE
1081 && !POINTER_TYPE_P (type))
1083 bad_quals |= TYPE_QUAL_RESTRICT;
1084 type_quals &= ~TYPE_QUAL_RESTRICT;
1087 if (bad_quals == TYPE_UNQUALIFIED
1088 || (complain & tf_ignore_bad_quals))
1089 /*OK*/;
1090 else if (!(complain & tf_error))
1091 return error_mark_node;
1092 else
1094 tree bad_type = build_qualified_type (ptr_type_node, bad_quals);
1095 error ("%qV qualifiers cannot be applied to %qT",
1096 bad_type, type);
1099 /* Retrieve (or create) the appropriately qualified variant. */
1100 result = build_qualified_type (type, type_quals);
1102 /* Preserve exception specs and ref-qualifier since build_qualified_type
1103 doesn't know about them. */
1104 if (TREE_CODE (result) == FUNCTION_TYPE
1105 || TREE_CODE (result) == METHOD_TYPE)
1107 result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type));
1108 result = build_ref_qualified_type (result, type_memfn_rqual (type));
1111 /* If this was a pointer-to-method type, and we just made a copy,
1112 then we need to unshare the record that holds the cached
1113 pointer-to-member-function type, because these will be distinct
1114 between the unqualified and qualified types. */
1115 if (result != type
1116 && TYPE_PTR_P (type)
1117 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE
1118 && TYPE_LANG_SPECIFIC (result) == TYPE_LANG_SPECIFIC (type))
1119 TYPE_LANG_SPECIFIC (result) = NULL;
1121 /* We may also have ended up building a new copy of the canonical
1122 type of a pointer-to-method type, which could have the same
1123 sharing problem described above. */
1124 if (TYPE_CANONICAL (result) != TYPE_CANONICAL (type)
1125 && TYPE_PTR_P (type)
1126 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE
1127 && (TYPE_LANG_SPECIFIC (TYPE_CANONICAL (result))
1128 == TYPE_LANG_SPECIFIC (TYPE_CANONICAL (type))))
1129 TYPE_LANG_SPECIFIC (TYPE_CANONICAL (result)) = NULL;
1131 return result;
1134 /* Return TYPE with const and volatile removed. */
1136 tree
1137 cv_unqualified (tree type)
1139 int quals;
1141 if (type == error_mark_node)
1142 return type;
1144 quals = cp_type_quals (type);
1145 quals &= ~(TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE);
1146 return cp_build_qualified_type (type, quals);
1149 /* Builds a qualified variant of T that is not a typedef variant.
1150 E.g. consider the following declarations:
1151 typedef const int ConstInt;
1152 typedef ConstInt* PtrConstInt;
1153 If T is PtrConstInt, this function returns a type representing
1154 const int*.
1155 In other words, if T is a typedef, the function returns the underlying type.
1156 The cv-qualification and attributes of the type returned match the
1157 input type.
1158 They will always be compatible types.
1159 The returned type is built so that all of its subtypes
1160 recursively have their typedefs stripped as well.
1162 This is different from just returning TYPE_CANONICAL (T)
1163 Because of several reasons:
1164 * If T is a type that needs structural equality
1165 its TYPE_CANONICAL (T) will be NULL.
1166 * TYPE_CANONICAL (T) desn't carry type attributes
1167 and loses template parameter names. */
1169 tree
1170 strip_typedefs (tree t)
1172 tree result = NULL, type = NULL, t0 = NULL;
1174 if (!t || t == error_mark_node || t == TYPE_CANONICAL (t))
1175 return t;
1177 gcc_assert (TYPE_P (t));
1179 switch (TREE_CODE (t))
1181 case POINTER_TYPE:
1182 type = strip_typedefs (TREE_TYPE (t));
1183 result = build_pointer_type (type);
1184 break;
1185 case REFERENCE_TYPE:
1186 type = strip_typedefs (TREE_TYPE (t));
1187 result = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
1188 break;
1189 case OFFSET_TYPE:
1190 t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t));
1191 type = strip_typedefs (TREE_TYPE (t));
1192 result = build_offset_type (t0, type);
1193 break;
1194 case RECORD_TYPE:
1195 if (TYPE_PTRMEMFUNC_P (t))
1197 t0 = strip_typedefs (TYPE_PTRMEMFUNC_FN_TYPE (t));
1198 result = build_ptrmemfunc_type (t0);
1200 break;
1201 case ARRAY_TYPE:
1202 type = strip_typedefs (TREE_TYPE (t));
1203 t0 = strip_typedefs (TYPE_DOMAIN (t));;
1204 result = build_cplus_array_type (type, t0);
1205 break;
1206 case FUNCTION_TYPE:
1207 case METHOD_TYPE:
1209 tree arg_types = NULL, arg_node, arg_type;
1210 for (arg_node = TYPE_ARG_TYPES (t);
1211 arg_node;
1212 arg_node = TREE_CHAIN (arg_node))
1214 if (arg_node == void_list_node)
1215 break;
1216 arg_type = strip_typedefs (TREE_VALUE (arg_node));
1217 gcc_assert (arg_type);
1219 arg_types =
1220 tree_cons (TREE_PURPOSE (arg_node), arg_type, arg_types);
1223 if (arg_types)
1224 arg_types = nreverse (arg_types);
1226 /* A list of parameters not ending with an ellipsis
1227 must end with void_list_node. */
1228 if (arg_node)
1229 arg_types = chainon (arg_types, void_list_node);
1231 type = strip_typedefs (TREE_TYPE (t));
1232 if (TREE_CODE (t) == METHOD_TYPE)
1234 tree class_type = TREE_TYPE (TREE_VALUE (arg_types));
1235 gcc_assert (class_type);
1236 result =
1237 build_method_type_directly (class_type, type,
1238 TREE_CHAIN (arg_types));
1240 else
1242 result = build_function_type (type,
1243 arg_types);
1244 result = apply_memfn_quals (result,
1245 type_memfn_quals (t),
1246 type_memfn_rqual (t));
1249 if (TYPE_RAISES_EXCEPTIONS (t))
1250 result = build_exception_variant (result,
1251 TYPE_RAISES_EXCEPTIONS (t));
1253 break;
1254 case TYPENAME_TYPE:
1256 tree fullname = TYPENAME_TYPE_FULLNAME (t);
1257 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
1258 && TREE_OPERAND (fullname, 1))
1260 tree args = TREE_OPERAND (fullname, 1);
1261 tree new_args = copy_node (args);
1262 bool changed = false;
1263 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
1265 tree arg = TREE_VEC_ELT (args, i);
1266 tree strip_arg;
1267 if (TYPE_P (arg))
1268 strip_arg = strip_typedefs (arg);
1269 else
1270 strip_arg = strip_typedefs_expr (arg);
1271 TREE_VEC_ELT (new_args, i) = strip_arg;
1272 if (strip_arg != arg)
1273 changed = true;
1275 if (changed)
1277 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_args)
1278 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
1279 fullname
1280 = lookup_template_function (TREE_OPERAND (fullname, 0),
1281 new_args);
1283 else
1284 ggc_free (new_args);
1286 result = make_typename_type (strip_typedefs (TYPE_CONTEXT (t)),
1287 fullname, typename_type, tf_none);
1289 break;
1290 case DECLTYPE_TYPE:
1291 result = strip_typedefs_expr (DECLTYPE_TYPE_EXPR (t));
1292 if (result == DECLTYPE_TYPE_EXPR (t))
1293 return t;
1294 else
1295 result = (finish_decltype_type
1296 (result,
1297 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t),
1298 tf_none));
1299 break;
1300 default:
1301 break;
1304 if (!result)
1305 result = TYPE_MAIN_VARIANT (t);
1306 if (TYPE_USER_ALIGN (t) != TYPE_USER_ALIGN (result)
1307 || TYPE_ALIGN (t) != TYPE_ALIGN (result))
1309 gcc_assert (TYPE_USER_ALIGN (t));
1310 if (TYPE_ALIGN (t) == TYPE_ALIGN (result))
1311 result = build_variant_type_copy (result);
1312 else
1313 result = build_aligned_type (result, TYPE_ALIGN (t));
1314 TYPE_USER_ALIGN (result) = true;
1316 if (TYPE_ATTRIBUTES (t))
1317 result = cp_build_type_attribute_variant (result, TYPE_ATTRIBUTES (t));
1318 return cp_build_qualified_type (result, cp_type_quals (t));
1321 /* Like strip_typedefs above, but works on expressions, so that in
1323 template<class T> struct A
1325 typedef T TT;
1326 B<sizeof(TT)> b;
1329 sizeof(TT) is replaced by sizeof(T). */
1331 tree
1332 strip_typedefs_expr (tree t)
1334 unsigned i,n;
1335 tree r, type, *ops;
1336 enum tree_code code;
1338 if (t == NULL_TREE || t == error_mark_node)
1339 return t;
1341 if (DECL_P (t) || CONSTANT_CLASS_P (t))
1342 return t;
1344 /* Some expressions have type operands, so let's handle types here rather
1345 than check TYPE_P in multiple places below. */
1346 if (TYPE_P (t))
1347 return strip_typedefs (t);
1349 code = TREE_CODE (t);
1350 switch (code)
1352 case IDENTIFIER_NODE:
1353 case TEMPLATE_PARM_INDEX:
1354 case OVERLOAD:
1355 case BASELINK:
1356 case ARGUMENT_PACK_SELECT:
1357 return t;
1359 case TRAIT_EXPR:
1361 tree type1 = strip_typedefs (TRAIT_EXPR_TYPE1 (t));
1362 tree type2 = strip_typedefs (TRAIT_EXPR_TYPE2 (t));
1363 if (type1 == TRAIT_EXPR_TYPE1 (t)
1364 && type2 == TRAIT_EXPR_TYPE2 (t))
1365 return t;
1366 r = copy_node (t);
1367 TRAIT_EXPR_TYPE1 (t) = type1;
1368 TRAIT_EXPR_TYPE2 (t) = type2;
1369 return r;
1372 case TREE_LIST:
1374 vec<tree, va_gc> *vec = make_tree_vector ();
1375 bool changed = false;
1376 tree it;
1377 for (it = t; it; it = TREE_CHAIN (it))
1379 tree val = strip_typedefs_expr (TREE_VALUE (t));
1380 vec_safe_push (vec, val);
1381 if (val != TREE_VALUE (t))
1382 changed = true;
1383 gcc_assert (TREE_PURPOSE (it) == NULL_TREE);
1385 if (changed)
1387 r = NULL_TREE;
1388 FOR_EACH_VEC_ELT_REVERSE (*vec, i, it)
1389 r = tree_cons (NULL_TREE, it, r);
1391 else
1392 r = t;
1393 release_tree_vector (vec);
1394 return r;
1397 case TREE_VEC:
1399 bool changed = false;
1400 vec<tree, va_gc> *vec = make_tree_vector ();
1401 n = TREE_VEC_LENGTH (t);
1402 vec_safe_reserve (vec, n);
1403 for (i = 0; i < n; ++i)
1405 tree op = strip_typedefs_expr (TREE_VEC_ELT (t, i));
1406 vec->quick_push (op);
1407 if (op != TREE_VEC_ELT (t, i))
1408 changed = true;
1410 if (changed)
1412 r = copy_node (t);
1413 for (i = 0; i < n; ++i)
1414 TREE_VEC_ELT (r, i) = (*vec)[i];
1415 NON_DEFAULT_TEMPLATE_ARGS_COUNT (r)
1416 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
1418 else
1419 r = t;
1420 release_tree_vector (vec);
1421 return r;
1424 case CONSTRUCTOR:
1426 bool changed = false;
1427 vec<constructor_elt, va_gc> *vec
1428 = vec_safe_copy (CONSTRUCTOR_ELTS (t));
1429 n = CONSTRUCTOR_NELTS (t);
1430 type = strip_typedefs (TREE_TYPE (t));
1431 for (i = 0; i < n; ++i)
1433 constructor_elt *e = &(*vec)[i];
1434 tree op = strip_typedefs_expr (e->value);
1435 if (op != e->value)
1437 changed = true;
1438 e->value = op;
1440 gcc_checking_assert (e->index == strip_typedefs_expr (e->index));
1443 if (!changed && type == TREE_TYPE (t))
1445 vec_free (vec);
1446 return t;
1448 else
1450 r = copy_node (t);
1451 TREE_TYPE (r) = type;
1452 CONSTRUCTOR_ELTS (r) = vec;
1453 return r;
1457 case LAMBDA_EXPR:
1458 error ("lambda-expression in a constant expression");
1459 return error_mark_node;
1461 default:
1462 break;
1465 gcc_assert (EXPR_P (t));
1467 n = TREE_OPERAND_LENGTH (t);
1468 ops = XALLOCAVEC (tree, n);
1469 type = TREE_TYPE (t);
1471 switch (code)
1473 CASE_CONVERT:
1474 case IMPLICIT_CONV_EXPR:
1475 case DYNAMIC_CAST_EXPR:
1476 case STATIC_CAST_EXPR:
1477 case CONST_CAST_EXPR:
1478 case REINTERPRET_CAST_EXPR:
1479 case CAST_EXPR:
1480 case NEW_EXPR:
1481 type = strip_typedefs (type);
1482 /* fallthrough */
1484 default:
1485 for (i = 0; i < n; ++i)
1486 ops[i] = strip_typedefs_expr (TREE_OPERAND (t, i));
1487 break;
1490 /* If nothing changed, return t. */
1491 for (i = 0; i < n; ++i)
1492 if (ops[i] != TREE_OPERAND (t, i))
1493 break;
1494 if (i == n && type == TREE_TYPE (t))
1495 return t;
1497 r = copy_node (t);
1498 TREE_TYPE (r) = type;
1499 for (i = 0; i < n; ++i)
1500 TREE_OPERAND (r, i) = ops[i];
1501 return r;
1504 /* Makes a copy of BINFO and TYPE, which is to be inherited into a
1505 graph dominated by T. If BINFO is NULL, TYPE is a dependent base,
1506 and we do a shallow copy. If BINFO is non-NULL, we do a deep copy.
1507 VIRT indicates whether TYPE is inherited virtually or not.
1508 IGO_PREV points at the previous binfo of the inheritance graph
1509 order chain. The newly copied binfo's TREE_CHAIN forms this
1510 ordering.
1512 The CLASSTYPE_VBASECLASSES vector of T is constructed in the
1513 correct order. That is in the order the bases themselves should be
1514 constructed in.
1516 The BINFO_INHERITANCE of a virtual base class points to the binfo
1517 of the most derived type. ??? We could probably change this so that
1518 BINFO_INHERITANCE becomes synonymous with BINFO_PRIMARY, and hence
1519 remove a field. They currently can only differ for primary virtual
1520 virtual bases. */
1522 tree
1523 copy_binfo (tree binfo, tree type, tree t, tree *igo_prev, int virt)
1525 tree new_binfo;
1527 if (virt)
1529 /* See if we've already made this virtual base. */
1530 new_binfo = binfo_for_vbase (type, t);
1531 if (new_binfo)
1532 return new_binfo;
1535 new_binfo = make_tree_binfo (binfo ? BINFO_N_BASE_BINFOS (binfo) : 0);
1536 BINFO_TYPE (new_binfo) = type;
1538 /* Chain it into the inheritance graph. */
1539 TREE_CHAIN (*igo_prev) = new_binfo;
1540 *igo_prev = new_binfo;
1542 if (binfo && !BINFO_DEPENDENT_BASE_P (binfo))
1544 int ix;
1545 tree base_binfo;
1547 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), type));
1549 BINFO_OFFSET (new_binfo) = BINFO_OFFSET (binfo);
1550 BINFO_VIRTUALS (new_binfo) = BINFO_VIRTUALS (binfo);
1552 /* We do not need to copy the accesses, as they are read only. */
1553 BINFO_BASE_ACCESSES (new_binfo) = BINFO_BASE_ACCESSES (binfo);
1555 /* Recursively copy base binfos of BINFO. */
1556 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1558 tree new_base_binfo;
1559 new_base_binfo = copy_binfo (base_binfo, BINFO_TYPE (base_binfo),
1560 t, igo_prev,
1561 BINFO_VIRTUAL_P (base_binfo));
1563 if (!BINFO_INHERITANCE_CHAIN (new_base_binfo))
1564 BINFO_INHERITANCE_CHAIN (new_base_binfo) = new_binfo;
1565 BINFO_BASE_APPEND (new_binfo, new_base_binfo);
1568 else
1569 BINFO_DEPENDENT_BASE_P (new_binfo) = 1;
1571 if (virt)
1573 /* Push it onto the list after any virtual bases it contains
1574 will have been pushed. */
1575 CLASSTYPE_VBASECLASSES (t)->quick_push (new_binfo);
1576 BINFO_VIRTUAL_P (new_binfo) = 1;
1577 BINFO_INHERITANCE_CHAIN (new_binfo) = TYPE_BINFO (t);
1580 return new_binfo;
1583 /* Hashing of lists so that we don't make duplicates.
1584 The entry point is `list_hash_canon'. */
1586 /* Now here is the hash table. When recording a list, it is added
1587 to the slot whose index is the hash code mod the table size.
1588 Note that the hash table is used for several kinds of lists.
1589 While all these live in the same table, they are completely independent,
1590 and the hash code is computed differently for each of these. */
1592 static GTY ((param_is (union tree_node))) htab_t list_hash_table;
1594 struct list_proxy
1596 tree purpose;
1597 tree value;
1598 tree chain;
1601 /* Compare ENTRY (an entry in the hash table) with DATA (a list_proxy
1602 for a node we are thinking about adding). */
1604 static int
1605 list_hash_eq (const void* entry, const void* data)
1607 const_tree const t = (const_tree) entry;
1608 const struct list_proxy *const proxy = (const struct list_proxy *) data;
1610 return (TREE_VALUE (t) == proxy->value
1611 && TREE_PURPOSE (t) == proxy->purpose
1612 && TREE_CHAIN (t) == proxy->chain);
1615 /* Compute a hash code for a list (chain of TREE_LIST nodes
1616 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
1617 TREE_COMMON slots), by adding the hash codes of the individual entries. */
1619 static hashval_t
1620 list_hash_pieces (tree purpose, tree value, tree chain)
1622 hashval_t hashcode = 0;
1624 if (chain)
1625 hashcode += TREE_HASH (chain);
1627 if (value)
1628 hashcode += TREE_HASH (value);
1629 else
1630 hashcode += 1007;
1631 if (purpose)
1632 hashcode += TREE_HASH (purpose);
1633 else
1634 hashcode += 1009;
1635 return hashcode;
1638 /* Hash an already existing TREE_LIST. */
1640 static hashval_t
1641 list_hash (const void* p)
1643 const_tree const t = (const_tree) p;
1644 return list_hash_pieces (TREE_PURPOSE (t),
1645 TREE_VALUE (t),
1646 TREE_CHAIN (t));
1649 /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
1650 object for an identical list if one already exists. Otherwise, build a
1651 new one, and record it as the canonical object. */
1653 tree
1654 hash_tree_cons (tree purpose, tree value, tree chain)
1656 int hashcode = 0;
1657 void **slot;
1658 struct list_proxy proxy;
1660 /* Hash the list node. */
1661 hashcode = list_hash_pieces (purpose, value, chain);
1662 /* Create a proxy for the TREE_LIST we would like to create. We
1663 don't actually create it so as to avoid creating garbage. */
1664 proxy.purpose = purpose;
1665 proxy.value = value;
1666 proxy.chain = chain;
1667 /* See if it is already in the table. */
1668 slot = htab_find_slot_with_hash (list_hash_table, &proxy, hashcode,
1669 INSERT);
1670 /* If not, create a new node. */
1671 if (!*slot)
1672 *slot = tree_cons (purpose, value, chain);
1673 return (tree) *slot;
1676 /* Constructor for hashed lists. */
1678 tree
1679 hash_tree_chain (tree value, tree chain)
1681 return hash_tree_cons (NULL_TREE, value, chain);
1684 void
1685 debug_binfo (tree elem)
1687 HOST_WIDE_INT n;
1688 tree virtuals;
1690 fprintf (stderr, "type \"%s\", offset = " HOST_WIDE_INT_PRINT_DEC
1691 "\nvtable type:\n",
1692 TYPE_NAME_STRING (BINFO_TYPE (elem)),
1693 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
1694 debug_tree (BINFO_TYPE (elem));
1695 if (BINFO_VTABLE (elem))
1696 fprintf (stderr, "vtable decl \"%s\"\n",
1697 IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem))));
1698 else
1699 fprintf (stderr, "no vtable decl yet\n");
1700 fprintf (stderr, "virtuals:\n");
1701 virtuals = BINFO_VIRTUALS (elem);
1702 n = 0;
1704 while (virtuals)
1706 tree fndecl = TREE_VALUE (virtuals);
1707 fprintf (stderr, "%s [%ld =? %ld]\n",
1708 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
1709 (long) n, (long) TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
1710 ++n;
1711 virtuals = TREE_CHAIN (virtuals);
1715 /* Build a representation for the qualified name SCOPE::NAME. TYPE is
1716 the type of the result expression, if known, or NULL_TREE if the
1717 resulting expression is type-dependent. If TEMPLATE_P is true,
1718 NAME is known to be a template because the user explicitly used the
1719 "template" keyword after the "::".
1721 All SCOPE_REFs should be built by use of this function. */
1723 tree
1724 build_qualified_name (tree type, tree scope, tree name, bool template_p)
1726 tree t;
1727 if (type == error_mark_node
1728 || scope == error_mark_node
1729 || name == error_mark_node)
1730 return error_mark_node;
1731 t = build2 (SCOPE_REF, type, scope, name);
1732 QUALIFIED_NAME_IS_TEMPLATE (t) = template_p;
1733 PTRMEM_OK_P (t) = true;
1734 if (type)
1735 t = convert_from_reference (t);
1736 return t;
1739 /* Like check_qualified_type, but also check ref-qualifier and exception
1740 specification. */
1742 static bool
1743 cp_check_qualified_type (const_tree cand, const_tree base, int type_quals,
1744 cp_ref_qualifier rqual, tree raises)
1746 return (check_qualified_type (cand, base, type_quals)
1747 && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (cand),
1748 ce_exact)
1749 && type_memfn_rqual (cand) == rqual);
1752 /* Build the FUNCTION_TYPE or METHOD_TYPE with the ref-qualifier RQUAL. */
1754 tree
1755 build_ref_qualified_type (tree type, cp_ref_qualifier rqual)
1757 tree t;
1759 if (rqual == type_memfn_rqual (type))
1760 return type;
1762 int type_quals = TYPE_QUALS (type);
1763 tree raises = TYPE_RAISES_EXCEPTIONS (type);
1764 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1765 if (cp_check_qualified_type (t, type, type_quals, rqual, raises))
1766 return t;
1768 t = build_variant_type_copy (type);
1769 switch (rqual)
1771 case REF_QUAL_RVALUE:
1772 FUNCTION_RVALUE_QUALIFIED (t) = 1;
1773 FUNCTION_REF_QUALIFIED (t) = 1;
1774 break;
1775 case REF_QUAL_LVALUE:
1776 FUNCTION_RVALUE_QUALIFIED (t) = 0;
1777 FUNCTION_REF_QUALIFIED (t) = 1;
1778 break;
1779 default:
1780 FUNCTION_REF_QUALIFIED (t) = 0;
1781 break;
1784 if (TYPE_STRUCTURAL_EQUALITY_P (type))
1785 /* Propagate structural equality. */
1786 SET_TYPE_STRUCTURAL_EQUALITY (t);
1787 else if (TYPE_CANONICAL (type) != type)
1788 /* Build the underlying canonical type, since it is different
1789 from TYPE. */
1790 TYPE_CANONICAL (t) = build_ref_qualified_type (TYPE_CANONICAL (type),
1791 rqual);
1792 else
1793 /* T is its own canonical type. */
1794 TYPE_CANONICAL (t) = t;
1796 return t;
1799 /* Returns nonzero if X is an expression for a (possibly overloaded)
1800 function. If "f" is a function or function template, "f", "c->f",
1801 "c.f", "C::f", and "f<int>" will all be considered possibly
1802 overloaded functions. Returns 2 if the function is actually
1803 overloaded, i.e., if it is impossible to know the type of the
1804 function without performing overload resolution. */
1807 is_overloaded_fn (tree x)
1809 /* A baselink is also considered an overloaded function. */
1810 if (TREE_CODE (x) == OFFSET_REF
1811 || TREE_CODE (x) == COMPONENT_REF)
1812 x = TREE_OPERAND (x, 1);
1813 if (BASELINK_P (x))
1814 x = BASELINK_FUNCTIONS (x);
1815 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
1816 x = TREE_OPERAND (x, 0);
1817 if (DECL_FUNCTION_TEMPLATE_P (OVL_CURRENT (x))
1818 || (TREE_CODE (x) == OVERLOAD && OVL_CHAIN (x)))
1819 return 2;
1820 return (TREE_CODE (x) == FUNCTION_DECL
1821 || TREE_CODE (x) == OVERLOAD);
1824 /* X is the CALL_EXPR_FN of a CALL_EXPR. If X represents a dependent name
1825 (14.6.2), return the IDENTIFIER_NODE for that name. Otherwise, return
1826 NULL_TREE. */
1828 tree
1829 dependent_name (tree x)
1831 if (identifier_p (x))
1832 return x;
1833 if (TREE_CODE (x) != COMPONENT_REF
1834 && TREE_CODE (x) != OFFSET_REF
1835 && TREE_CODE (x) != BASELINK
1836 && is_overloaded_fn (x))
1837 return DECL_NAME (get_first_fn (x));
1838 return NULL_TREE;
1841 /* Returns true iff X is an expression for an overloaded function
1842 whose type cannot be known without performing overload
1843 resolution. */
1845 bool
1846 really_overloaded_fn (tree x)
1848 return is_overloaded_fn (x) == 2;
1851 tree
1852 get_fns (tree from)
1854 gcc_assert (is_overloaded_fn (from));
1855 /* A baselink is also considered an overloaded function. */
1856 if (TREE_CODE (from) == OFFSET_REF
1857 || TREE_CODE (from) == COMPONENT_REF)
1858 from = TREE_OPERAND (from, 1);
1859 if (BASELINK_P (from))
1860 from = BASELINK_FUNCTIONS (from);
1861 if (TREE_CODE (from) == TEMPLATE_ID_EXPR)
1862 from = TREE_OPERAND (from, 0);
1863 return from;
1866 tree
1867 get_first_fn (tree from)
1869 return OVL_CURRENT (get_fns (from));
1872 /* Return a new OVL node, concatenating it with the old one. */
1874 tree
1875 ovl_cons (tree decl, tree chain)
1877 tree result = make_node (OVERLOAD);
1878 TREE_TYPE (result) = unknown_type_node;
1879 OVL_FUNCTION (result) = decl;
1880 TREE_CHAIN (result) = chain;
1882 return result;
1885 /* Build a new overloaded function. If this is the first one,
1886 just return it; otherwise, ovl_cons the _DECLs */
1888 tree
1889 build_overload (tree decl, tree chain)
1891 if (! chain && TREE_CODE (decl) != TEMPLATE_DECL)
1892 return decl;
1893 return ovl_cons (decl, chain);
1896 /* Return the scope where the overloaded functions OVL were found. */
1898 tree
1899 ovl_scope (tree ovl)
1901 if (TREE_CODE (ovl) == OFFSET_REF
1902 || TREE_CODE (ovl) == COMPONENT_REF)
1903 ovl = TREE_OPERAND (ovl, 1);
1904 if (TREE_CODE (ovl) == BASELINK)
1905 return BINFO_TYPE (BASELINK_BINFO (ovl));
1906 if (TREE_CODE (ovl) == TEMPLATE_ID_EXPR)
1907 ovl = TREE_OPERAND (ovl, 0);
1908 /* Skip using-declarations. */
1909 while (TREE_CODE (ovl) == OVERLOAD && OVL_USED (ovl) && OVL_CHAIN (ovl))
1910 ovl = OVL_CHAIN (ovl);
1911 return CP_DECL_CONTEXT (OVL_CURRENT (ovl));
1914 /* Return TRUE if FN is a non-static member function, FALSE otherwise.
1915 This function looks into BASELINK and OVERLOAD nodes. */
1917 bool
1918 non_static_member_function_p (tree fn)
1920 if (fn == NULL_TREE)
1921 return false;
1923 if (is_overloaded_fn (fn))
1924 fn = get_first_fn (fn);
1926 return (DECL_P (fn)
1927 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn));
1931 #define PRINT_RING_SIZE 4
1933 static const char *
1934 cxx_printable_name_internal (tree decl, int v, bool translate)
1936 static unsigned int uid_ring[PRINT_RING_SIZE];
1937 static char *print_ring[PRINT_RING_SIZE];
1938 static bool trans_ring[PRINT_RING_SIZE];
1939 static int ring_counter;
1940 int i;
1942 /* Only cache functions. */
1943 if (v < 2
1944 || TREE_CODE (decl) != FUNCTION_DECL
1945 || DECL_LANG_SPECIFIC (decl) == 0)
1946 return lang_decl_name (decl, v, translate);
1948 /* See if this print name is lying around. */
1949 for (i = 0; i < PRINT_RING_SIZE; i++)
1950 if (uid_ring[i] == DECL_UID (decl) && translate == trans_ring[i])
1951 /* yes, so return it. */
1952 return print_ring[i];
1954 if (++ring_counter == PRINT_RING_SIZE)
1955 ring_counter = 0;
1957 if (current_function_decl != NULL_TREE)
1959 /* There may be both translated and untranslated versions of the
1960 name cached. */
1961 for (i = 0; i < 2; i++)
1963 if (uid_ring[ring_counter] == DECL_UID (current_function_decl))
1964 ring_counter += 1;
1965 if (ring_counter == PRINT_RING_SIZE)
1966 ring_counter = 0;
1968 gcc_assert (uid_ring[ring_counter] != DECL_UID (current_function_decl));
1971 free (print_ring[ring_counter]);
1973 print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v, translate));
1974 uid_ring[ring_counter] = DECL_UID (decl);
1975 trans_ring[ring_counter] = translate;
1976 return print_ring[ring_counter];
1979 const char *
1980 cxx_printable_name (tree decl, int v)
1982 return cxx_printable_name_internal (decl, v, false);
1985 const char *
1986 cxx_printable_name_translate (tree decl, int v)
1988 return cxx_printable_name_internal (decl, v, true);
1991 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
1992 listed in RAISES. */
1994 tree
1995 build_exception_variant (tree type, tree raises)
1997 tree v;
1998 int type_quals;
2000 if (comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (type), ce_exact))
2001 return type;
2003 type_quals = TYPE_QUALS (type);
2004 cp_ref_qualifier rqual = type_memfn_rqual (type);
2005 for (v = TYPE_MAIN_VARIANT (type); v; v = TYPE_NEXT_VARIANT (v))
2006 if (cp_check_qualified_type (v, type, type_quals, rqual, raises))
2007 return v;
2009 /* Need to build a new variant. */
2010 v = build_variant_type_copy (type);
2011 TYPE_RAISES_EXCEPTIONS (v) = raises;
2012 return v;
2015 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new
2016 BOUND_TEMPLATE_TEMPLATE_PARM bound with NEWARGS as its template
2017 arguments. */
2019 tree
2020 bind_template_template_parm (tree t, tree newargs)
2022 tree decl = TYPE_NAME (t);
2023 tree t2;
2025 t2 = cxx_make_type (BOUND_TEMPLATE_TEMPLATE_PARM);
2026 decl = build_decl (input_location,
2027 TYPE_DECL, DECL_NAME (decl), NULL_TREE);
2029 /* These nodes have to be created to reflect new TYPE_DECL and template
2030 arguments. */
2031 TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t));
2032 TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl;
2033 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
2034 = build_template_info (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t), newargs);
2036 TREE_TYPE (decl) = t2;
2037 TYPE_NAME (t2) = decl;
2038 TYPE_STUB_DECL (t2) = decl;
2039 TYPE_SIZE (t2) = 0;
2040 SET_TYPE_STRUCTURAL_EQUALITY (t2);
2042 return t2;
2045 /* Called from count_trees via walk_tree. */
2047 static tree
2048 count_trees_r (tree *tp, int *walk_subtrees, void *data)
2050 ++*((int *) data);
2052 if (TYPE_P (*tp))
2053 *walk_subtrees = 0;
2055 return NULL_TREE;
2058 /* Debugging function for measuring the rough complexity of a tree
2059 representation. */
2062 count_trees (tree t)
2064 int n_trees = 0;
2065 cp_walk_tree_without_duplicates (&t, count_trees_r, &n_trees);
2066 return n_trees;
2069 /* Called from verify_stmt_tree via walk_tree. */
2071 static tree
2072 verify_stmt_tree_r (tree* tp, int * /*walk_subtrees*/, void* data)
2074 tree t = *tp;
2075 hash_table <pointer_hash <tree_node> > *statements
2076 = static_cast <hash_table <pointer_hash <tree_node> > *> (data);
2077 tree_node **slot;
2079 if (!STATEMENT_CODE_P (TREE_CODE (t)))
2080 return NULL_TREE;
2082 /* If this statement is already present in the hash table, then
2083 there is a circularity in the statement tree. */
2084 gcc_assert (!statements->find (t));
2086 slot = statements->find_slot (t, INSERT);
2087 *slot = t;
2089 return NULL_TREE;
2092 /* Debugging function to check that the statement T has not been
2093 corrupted. For now, this function simply checks that T contains no
2094 circularities. */
2096 void
2097 verify_stmt_tree (tree t)
2099 hash_table <pointer_hash <tree_node> > statements;
2100 statements.create (37);
2101 cp_walk_tree (&t, verify_stmt_tree_r, &statements, NULL);
2102 statements.dispose ();
2105 /* Check if the type T depends on a type with no linkage and if so, return
2106 it. If RELAXED_P then do not consider a class type declared within
2107 a vague-linkage function to have no linkage. */
2109 tree
2110 no_linkage_check (tree t, bool relaxed_p)
2112 tree r;
2114 /* There's no point in checking linkage on template functions; we
2115 can't know their complete types. */
2116 if (processing_template_decl)
2117 return NULL_TREE;
2119 switch (TREE_CODE (t))
2121 case RECORD_TYPE:
2122 if (TYPE_PTRMEMFUNC_P (t))
2123 goto ptrmem;
2124 /* Lambda types that don't have mangling scope have no linkage. We
2125 check CLASSTYPE_LAMBDA_EXPR for error_mark_node because
2126 when we get here from pushtag none of the lambda information is
2127 set up yet, so we want to assume that the lambda has linkage and
2128 fix it up later if not. */
2129 if (CLASSTYPE_LAMBDA_EXPR (t)
2130 && CLASSTYPE_LAMBDA_EXPR (t) != error_mark_node
2131 && LAMBDA_TYPE_EXTRA_SCOPE (t) == NULL_TREE)
2132 return t;
2133 /* Fall through. */
2134 case UNION_TYPE:
2135 if (!CLASS_TYPE_P (t))
2136 return NULL_TREE;
2137 /* Fall through. */
2138 case ENUMERAL_TYPE:
2139 /* Only treat anonymous types as having no linkage if they're at
2140 namespace scope. This is core issue 966. */
2141 if (TYPE_ANONYMOUS_P (t) && TYPE_NAMESPACE_SCOPE_P (t))
2142 return t;
2144 for (r = CP_TYPE_CONTEXT (t); ; )
2146 /* If we're a nested type of a !TREE_PUBLIC class, we might not
2147 have linkage, or we might just be in an anonymous namespace.
2148 If we're in a TREE_PUBLIC class, we have linkage. */
2149 if (TYPE_P (r) && !TREE_PUBLIC (TYPE_NAME (r)))
2150 return no_linkage_check (TYPE_CONTEXT (t), relaxed_p);
2151 else if (TREE_CODE (r) == FUNCTION_DECL)
2153 if (!relaxed_p || !vague_linkage_p (r))
2154 return t;
2155 else
2156 r = CP_DECL_CONTEXT (r);
2158 else
2159 break;
2162 return NULL_TREE;
2164 case ARRAY_TYPE:
2165 case POINTER_TYPE:
2166 case REFERENCE_TYPE:
2167 return no_linkage_check (TREE_TYPE (t), relaxed_p);
2169 case OFFSET_TYPE:
2170 ptrmem:
2171 r = no_linkage_check (TYPE_PTRMEM_POINTED_TO_TYPE (t),
2172 relaxed_p);
2173 if (r)
2174 return r;
2175 return no_linkage_check (TYPE_PTRMEM_CLASS_TYPE (t), relaxed_p);
2177 case METHOD_TYPE:
2178 r = no_linkage_check (TYPE_METHOD_BASETYPE (t), relaxed_p);
2179 if (r)
2180 return r;
2181 /* Fall through. */
2182 case FUNCTION_TYPE:
2184 tree parm;
2185 for (parm = TYPE_ARG_TYPES (t);
2186 parm && parm != void_list_node;
2187 parm = TREE_CHAIN (parm))
2189 r = no_linkage_check (TREE_VALUE (parm), relaxed_p);
2190 if (r)
2191 return r;
2193 return no_linkage_check (TREE_TYPE (t), relaxed_p);
2196 default:
2197 return NULL_TREE;
2201 extern int depth_reached;
2203 void
2204 cxx_print_statistics (void)
2206 print_search_statistics ();
2207 print_class_statistics ();
2208 print_template_statistics ();
2209 if (GATHER_STATISTICS)
2210 fprintf (stderr, "maximum template instantiation depth reached: %d\n",
2211 depth_reached);
2214 /* Return, as an INTEGER_CST node, the number of elements for TYPE
2215 (which is an ARRAY_TYPE). This counts only elements of the top
2216 array. */
2218 tree
2219 array_type_nelts_top (tree type)
2221 return fold_build2_loc (input_location,
2222 PLUS_EXPR, sizetype,
2223 array_type_nelts (type),
2224 size_one_node);
2227 /* Return, as an INTEGER_CST node, the number of elements for TYPE
2228 (which is an ARRAY_TYPE). This one is a recursive count of all
2229 ARRAY_TYPEs that are clumped together. */
2231 tree
2232 array_type_nelts_total (tree type)
2234 tree sz = array_type_nelts_top (type);
2235 type = TREE_TYPE (type);
2236 while (TREE_CODE (type) == ARRAY_TYPE)
2238 tree n = array_type_nelts_top (type);
2239 sz = fold_build2_loc (input_location,
2240 MULT_EXPR, sizetype, sz, n);
2241 type = TREE_TYPE (type);
2243 return sz;
2246 /* Called from break_out_target_exprs via mapcar. */
2248 static tree
2249 bot_manip (tree* tp, int* walk_subtrees, void* data)
2251 splay_tree target_remap = ((splay_tree) data);
2252 tree t = *tp;
2254 if (!TYPE_P (t) && TREE_CONSTANT (t) && !TREE_SIDE_EFFECTS (t))
2256 /* There can't be any TARGET_EXPRs or their slot variables below this
2257 point. But we must make a copy, in case subsequent processing
2258 alters any part of it. For example, during gimplification a cast
2259 of the form (T) &X::f (where "f" is a member function) will lead
2260 to replacing the PTRMEM_CST for &X::f with a VAR_DECL. */
2261 *walk_subtrees = 0;
2262 *tp = unshare_expr (t);
2263 return NULL_TREE;
2265 if (TREE_CODE (t) == TARGET_EXPR)
2267 tree u;
2269 if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
2271 u = build_cplus_new (TREE_TYPE (t), TREE_OPERAND (t, 1),
2272 tf_warning_or_error);
2273 if (AGGR_INIT_ZERO_FIRST (TREE_OPERAND (t, 1)))
2274 AGGR_INIT_ZERO_FIRST (TREE_OPERAND (u, 1)) = true;
2276 else
2277 u = build_target_expr_with_type (TREE_OPERAND (t, 1), TREE_TYPE (t),
2278 tf_warning_or_error);
2280 TARGET_EXPR_IMPLICIT_P (u) = TARGET_EXPR_IMPLICIT_P (t);
2281 TARGET_EXPR_LIST_INIT_P (u) = TARGET_EXPR_LIST_INIT_P (t);
2282 TARGET_EXPR_DIRECT_INIT_P (u) = TARGET_EXPR_DIRECT_INIT_P (t);
2284 /* Map the old variable to the new one. */
2285 splay_tree_insert (target_remap,
2286 (splay_tree_key) TREE_OPERAND (t, 0),
2287 (splay_tree_value) TREE_OPERAND (u, 0));
2289 TREE_OPERAND (u, 1) = break_out_target_exprs (TREE_OPERAND (u, 1));
2291 /* Replace the old expression with the new version. */
2292 *tp = u;
2293 /* We don't have to go below this point; the recursive call to
2294 break_out_target_exprs will have handled anything below this
2295 point. */
2296 *walk_subtrees = 0;
2297 return NULL_TREE;
2300 /* Make a copy of this node. */
2301 t = copy_tree_r (tp, walk_subtrees, NULL);
2302 if (TREE_CODE (*tp) == CALL_EXPR)
2303 set_flags_from_callee (*tp);
2304 return t;
2307 /* Replace all remapped VAR_DECLs in T with their new equivalents.
2308 DATA is really a splay-tree mapping old variables to new
2309 variables. */
2311 static tree
2312 bot_replace (tree* t, int* /*walk_subtrees*/, void* data)
2314 splay_tree target_remap = ((splay_tree) data);
2316 if (VAR_P (*t))
2318 splay_tree_node n = splay_tree_lookup (target_remap,
2319 (splay_tree_key) *t);
2320 if (n)
2321 *t = (tree) n->value;
2323 else if (TREE_CODE (*t) == PARM_DECL
2324 && DECL_NAME (*t) == this_identifier)
2326 /* In an NSDMI we need to replace the 'this' parameter we used for
2327 parsing with the real one for this function. */
2328 *t = current_class_ptr;
2330 else if (TREE_CODE (*t) == CONVERT_EXPR
2331 && CONVERT_EXPR_VBASE_PATH (*t))
2333 /* In an NSDMI build_base_path defers building conversions to virtual
2334 bases, and we handle it here. */
2335 tree basetype = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (*t)));
2336 vec<tree, va_gc> *vbases = CLASSTYPE_VBASECLASSES (current_class_type);
2337 int i; tree binfo;
2338 FOR_EACH_VEC_SAFE_ELT (vbases, i, binfo)
2339 if (BINFO_TYPE (binfo) == basetype)
2340 break;
2341 *t = build_base_path (PLUS_EXPR, TREE_OPERAND (*t, 0), binfo, true,
2342 tf_warning_or_error);
2345 return NULL_TREE;
2348 /* When we parse a default argument expression, we may create
2349 temporary variables via TARGET_EXPRs. When we actually use the
2350 default-argument expression, we make a copy of the expression
2351 and replace the temporaries with appropriate local versions. */
2353 tree
2354 break_out_target_exprs (tree t)
2356 static int target_remap_count;
2357 static splay_tree target_remap;
2359 if (!target_remap_count++)
2360 target_remap = splay_tree_new (splay_tree_compare_pointers,
2361 /*splay_tree_delete_key_fn=*/NULL,
2362 /*splay_tree_delete_value_fn=*/NULL);
2363 cp_walk_tree (&t, bot_manip, target_remap, NULL);
2364 cp_walk_tree (&t, bot_replace, target_remap, NULL);
2366 if (!--target_remap_count)
2368 splay_tree_delete (target_remap);
2369 target_remap = NULL;
2372 return t;
2375 /* Similar to `build_nt', but for template definitions of dependent
2376 expressions */
2378 tree
2379 build_min_nt_loc (location_t loc, enum tree_code code, ...)
2381 tree t;
2382 int length;
2383 int i;
2384 va_list p;
2386 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
2388 va_start (p, code);
2390 t = make_node (code);
2391 SET_EXPR_LOCATION (t, loc);
2392 length = TREE_CODE_LENGTH (code);
2394 for (i = 0; i < length; i++)
2396 tree x = va_arg (p, tree);
2397 TREE_OPERAND (t, i) = x;
2400 va_end (p);
2401 return t;
2405 /* Similar to `build', but for template definitions. */
2407 tree
2408 build_min (enum tree_code code, tree tt, ...)
2410 tree t;
2411 int length;
2412 int i;
2413 va_list p;
2415 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
2417 va_start (p, tt);
2419 t = make_node (code);
2420 length = TREE_CODE_LENGTH (code);
2421 TREE_TYPE (t) = tt;
2423 for (i = 0; i < length; i++)
2425 tree x = va_arg (p, tree);
2426 TREE_OPERAND (t, i) = x;
2427 if (x && !TYPE_P (x) && TREE_SIDE_EFFECTS (x))
2428 TREE_SIDE_EFFECTS (t) = 1;
2431 va_end (p);
2432 return t;
2435 /* Similar to `build', but for template definitions of non-dependent
2436 expressions. NON_DEP is the non-dependent expression that has been
2437 built. */
2439 tree
2440 build_min_non_dep (enum tree_code code, tree non_dep, ...)
2442 tree t;
2443 int length;
2444 int i;
2445 va_list p;
2447 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
2449 va_start (p, non_dep);
2451 if (REFERENCE_REF_P (non_dep))
2452 non_dep = TREE_OPERAND (non_dep, 0);
2454 t = make_node (code);
2455 length = TREE_CODE_LENGTH (code);
2456 TREE_TYPE (t) = TREE_TYPE (non_dep);
2457 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
2459 for (i = 0; i < length; i++)
2461 tree x = va_arg (p, tree);
2462 TREE_OPERAND (t, i) = x;
2465 if (code == COMPOUND_EXPR && TREE_CODE (non_dep) != COMPOUND_EXPR)
2466 /* This should not be considered a COMPOUND_EXPR, because it
2467 resolves to an overload. */
2468 COMPOUND_EXPR_OVERLOADED (t) = 1;
2470 va_end (p);
2471 return convert_from_reference (t);
2474 /* Similar to `build_nt_call_vec', but for template definitions of
2475 non-dependent expressions. NON_DEP is the non-dependent expression
2476 that has been built. */
2478 tree
2479 build_min_non_dep_call_vec (tree non_dep, tree fn, vec<tree, va_gc> *argvec)
2481 tree t = build_nt_call_vec (fn, argvec);
2482 if (REFERENCE_REF_P (non_dep))
2483 non_dep = TREE_OPERAND (non_dep, 0);
2484 TREE_TYPE (t) = TREE_TYPE (non_dep);
2485 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
2486 return convert_from_reference (t);
2489 tree
2490 get_type_decl (tree t)
2492 if (TREE_CODE (t) == TYPE_DECL)
2493 return t;
2494 if (TYPE_P (t))
2495 return TYPE_STUB_DECL (t);
2496 gcc_assert (t == error_mark_node);
2497 return t;
2500 /* Returns the namespace that contains DECL, whether directly or
2501 indirectly. */
2503 tree
2504 decl_namespace_context (tree decl)
2506 while (1)
2508 if (TREE_CODE (decl) == NAMESPACE_DECL)
2509 return decl;
2510 else if (TYPE_P (decl))
2511 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
2512 else
2513 decl = CP_DECL_CONTEXT (decl);
2517 /* Returns true if decl is within an anonymous namespace, however deeply
2518 nested, or false otherwise. */
2520 bool
2521 decl_anon_ns_mem_p (const_tree decl)
2523 while (1)
2525 if (decl == NULL_TREE || decl == error_mark_node)
2526 return false;
2527 if (TREE_CODE (decl) == NAMESPACE_DECL
2528 && DECL_NAME (decl) == NULL_TREE)
2529 return true;
2530 /* Classes and namespaces inside anonymous namespaces have
2531 TREE_PUBLIC == 0, so we can shortcut the search. */
2532 else if (TYPE_P (decl))
2533 return (TREE_PUBLIC (TYPE_MAIN_DECL (decl)) == 0);
2534 else if (TREE_CODE (decl) == NAMESPACE_DECL)
2535 return (TREE_PUBLIC (decl) == 0);
2536 else
2537 decl = DECL_CONTEXT (decl);
2541 /* Subroutine of cp_tree_equal: t1 and t2 are the CALL_EXPR_FNs of two
2542 CALL_EXPRS. Return whether they are equivalent. */
2544 static bool
2545 called_fns_equal (tree t1, tree t2)
2547 /* Core 1321: dependent names are equivalent even if the overload sets
2548 are different. But do compare explicit template arguments. */
2549 tree name1 = dependent_name (t1);
2550 tree name2 = dependent_name (t2);
2551 if (name1 || name2)
2553 tree targs1 = NULL_TREE, targs2 = NULL_TREE;
2555 if (name1 != name2)
2556 return false;
2558 if (TREE_CODE (t1) == TEMPLATE_ID_EXPR)
2559 targs1 = TREE_OPERAND (t1, 1);
2560 if (TREE_CODE (t2) == TEMPLATE_ID_EXPR)
2561 targs2 = TREE_OPERAND (t2, 1);
2562 return cp_tree_equal (targs1, targs2);
2564 else
2565 return cp_tree_equal (t1, t2);
2568 /* Return truthvalue of whether T1 is the same tree structure as T2.
2569 Return 1 if they are the same. Return 0 if they are different. */
2571 bool
2572 cp_tree_equal (tree t1, tree t2)
2574 enum tree_code code1, code2;
2576 if (t1 == t2)
2577 return true;
2578 if (!t1 || !t2)
2579 return false;
2581 for (code1 = TREE_CODE (t1);
2582 CONVERT_EXPR_CODE_P (code1)
2583 || code1 == NON_LVALUE_EXPR;
2584 code1 = TREE_CODE (t1))
2585 t1 = TREE_OPERAND (t1, 0);
2586 for (code2 = TREE_CODE (t2);
2587 CONVERT_EXPR_CODE_P (code2)
2588 || code2 == NON_LVALUE_EXPR;
2589 code2 = TREE_CODE (t2))
2590 t2 = TREE_OPERAND (t2, 0);
2592 /* They might have become equal now. */
2593 if (t1 == t2)
2594 return true;
2596 if (code1 != code2)
2597 return false;
2599 switch (code1)
2601 case INTEGER_CST:
2602 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
2603 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
2605 case REAL_CST:
2606 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2608 case STRING_CST:
2609 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2610 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2611 TREE_STRING_LENGTH (t1));
2613 case FIXED_CST:
2614 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
2615 TREE_FIXED_CST (t2));
2617 case COMPLEX_CST:
2618 return cp_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
2619 && cp_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
2621 case VECTOR_CST:
2622 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
2624 case CONSTRUCTOR:
2625 /* We need to do this when determining whether or not two
2626 non-type pointer to member function template arguments
2627 are the same. */
2628 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
2629 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
2630 return false;
2632 tree field, value;
2633 unsigned int i;
2634 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
2636 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
2637 if (!cp_tree_equal (field, elt2->index)
2638 || !cp_tree_equal (value, elt2->value))
2639 return false;
2642 return true;
2644 case TREE_LIST:
2645 if (!cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
2646 return false;
2647 if (!cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
2648 return false;
2649 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
2651 case SAVE_EXPR:
2652 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2654 case CALL_EXPR:
2656 tree arg1, arg2;
2657 call_expr_arg_iterator iter1, iter2;
2658 if (!called_fns_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
2659 return false;
2660 for (arg1 = first_call_expr_arg (t1, &iter1),
2661 arg2 = first_call_expr_arg (t2, &iter2);
2662 arg1 && arg2;
2663 arg1 = next_call_expr_arg (&iter1),
2664 arg2 = next_call_expr_arg (&iter2))
2665 if (!cp_tree_equal (arg1, arg2))
2666 return false;
2667 if (arg1 || arg2)
2668 return false;
2669 return true;
2672 case TARGET_EXPR:
2674 tree o1 = TREE_OPERAND (t1, 0);
2675 tree o2 = TREE_OPERAND (t2, 0);
2677 /* Special case: if either target is an unallocated VAR_DECL,
2678 it means that it's going to be unified with whatever the
2679 TARGET_EXPR is really supposed to initialize, so treat it
2680 as being equivalent to anything. */
2681 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
2682 && !DECL_RTL_SET_P (o1))
2683 /*Nop*/;
2684 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
2685 && !DECL_RTL_SET_P (o2))
2686 /*Nop*/;
2687 else if (!cp_tree_equal (o1, o2))
2688 return false;
2690 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
2693 case WITH_CLEANUP_EXPR:
2694 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
2695 return false;
2696 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
2698 case COMPONENT_REF:
2699 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
2700 return false;
2701 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2703 case PARM_DECL:
2704 /* For comparing uses of parameters in late-specified return types
2705 with an out-of-class definition of the function, but can also come
2706 up for expressions that involve 'this' in a member function
2707 template. */
2709 if (comparing_specializations)
2710 /* When comparing hash table entries, only an exact match is
2711 good enough; we don't want to replace 'this' with the
2712 version from another function. */
2713 return false;
2715 if (same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2717 if (DECL_ARTIFICIAL (t1) ^ DECL_ARTIFICIAL (t2))
2718 return false;
2719 if (DECL_ARTIFICIAL (t1)
2720 || (DECL_PARM_LEVEL (t1) == DECL_PARM_LEVEL (t2)
2721 && DECL_PARM_INDEX (t1) == DECL_PARM_INDEX (t2)))
2722 return true;
2724 return false;
2726 case VAR_DECL:
2727 case CONST_DECL:
2728 case FIELD_DECL:
2729 case FUNCTION_DECL:
2730 case TEMPLATE_DECL:
2731 case IDENTIFIER_NODE:
2732 case SSA_NAME:
2733 return false;
2735 case BASELINK:
2736 return (BASELINK_BINFO (t1) == BASELINK_BINFO (t2)
2737 && BASELINK_ACCESS_BINFO (t1) == BASELINK_ACCESS_BINFO (t2)
2738 && BASELINK_QUALIFIED_P (t1) == BASELINK_QUALIFIED_P (t2)
2739 && cp_tree_equal (BASELINK_FUNCTIONS (t1),
2740 BASELINK_FUNCTIONS (t2)));
2742 case TEMPLATE_PARM_INDEX:
2743 return (TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
2744 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2)
2745 && (TEMPLATE_PARM_PARAMETER_PACK (t1)
2746 == TEMPLATE_PARM_PARAMETER_PACK (t2))
2747 && same_type_p (TREE_TYPE (TEMPLATE_PARM_DECL (t1)),
2748 TREE_TYPE (TEMPLATE_PARM_DECL (t2))));
2750 case TEMPLATE_ID_EXPR:
2751 return (cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
2752 && cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
2754 case TREE_VEC:
2756 unsigned ix;
2757 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2758 return false;
2759 for (ix = TREE_VEC_LENGTH (t1); ix--;)
2760 if (!cp_tree_equal (TREE_VEC_ELT (t1, ix),
2761 TREE_VEC_ELT (t2, ix)))
2762 return false;
2763 return true;
2766 case SIZEOF_EXPR:
2767 case ALIGNOF_EXPR:
2769 tree o1 = TREE_OPERAND (t1, 0);
2770 tree o2 = TREE_OPERAND (t2, 0);
2772 if (code1 == SIZEOF_EXPR)
2774 if (SIZEOF_EXPR_TYPE_P (t1))
2775 o1 = TREE_TYPE (o1);
2776 if (SIZEOF_EXPR_TYPE_P (t2))
2777 o2 = TREE_TYPE (o2);
2779 if (TREE_CODE (o1) != TREE_CODE (o2))
2780 return false;
2781 if (TYPE_P (o1))
2782 return same_type_p (o1, o2);
2783 else
2784 return cp_tree_equal (o1, o2);
2787 case MODOP_EXPR:
2789 tree t1_op1, t2_op1;
2791 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
2792 return false;
2794 t1_op1 = TREE_OPERAND (t1, 1);
2795 t2_op1 = TREE_OPERAND (t2, 1);
2796 if (TREE_CODE (t1_op1) != TREE_CODE (t2_op1))
2797 return false;
2799 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t2, 2));
2802 case PTRMEM_CST:
2803 /* Two pointer-to-members are the same if they point to the same
2804 field or function in the same class. */
2805 if (PTRMEM_CST_MEMBER (t1) != PTRMEM_CST_MEMBER (t2))
2806 return false;
2808 return same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2));
2810 case OVERLOAD:
2811 if (OVL_FUNCTION (t1) != OVL_FUNCTION (t2))
2812 return false;
2813 return cp_tree_equal (OVL_CHAIN (t1), OVL_CHAIN (t2));
2815 case TRAIT_EXPR:
2816 if (TRAIT_EXPR_KIND (t1) != TRAIT_EXPR_KIND (t2))
2817 return false;
2818 return same_type_p (TRAIT_EXPR_TYPE1 (t1), TRAIT_EXPR_TYPE1 (t2))
2819 && same_type_p (TRAIT_EXPR_TYPE2 (t1), TRAIT_EXPR_TYPE2 (t2));
2821 case CAST_EXPR:
2822 case STATIC_CAST_EXPR:
2823 case REINTERPRET_CAST_EXPR:
2824 case CONST_CAST_EXPR:
2825 case DYNAMIC_CAST_EXPR:
2826 case IMPLICIT_CONV_EXPR:
2827 case NEW_EXPR:
2828 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
2829 return false;
2830 /* Now compare operands as usual. */
2831 break;
2833 case DEFERRED_NOEXCEPT:
2834 return (cp_tree_equal (DEFERRED_NOEXCEPT_PATTERN (t1),
2835 DEFERRED_NOEXCEPT_PATTERN (t2))
2836 && comp_template_args (DEFERRED_NOEXCEPT_ARGS (t1),
2837 DEFERRED_NOEXCEPT_ARGS (t2)));
2838 break;
2840 default:
2841 break;
2844 switch (TREE_CODE_CLASS (code1))
2846 case tcc_unary:
2847 case tcc_binary:
2848 case tcc_comparison:
2849 case tcc_expression:
2850 case tcc_vl_exp:
2851 case tcc_reference:
2852 case tcc_statement:
2854 int i, n;
2856 n = cp_tree_operand_length (t1);
2857 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
2858 && n != TREE_OPERAND_LENGTH (t2))
2859 return false;
2861 for (i = 0; i < n; ++i)
2862 if (!cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
2863 return false;
2865 return true;
2868 case tcc_type:
2869 return same_type_p (t1, t2);
2870 default:
2871 gcc_unreachable ();
2873 /* We can get here with --disable-checking. */
2874 return false;
2877 /* The type of ARG when used as an lvalue. */
2879 tree
2880 lvalue_type (tree arg)
2882 tree type = TREE_TYPE (arg);
2883 return type;
2886 /* The type of ARG for printing error messages; denote lvalues with
2887 reference types. */
2889 tree
2890 error_type (tree arg)
2892 tree type = TREE_TYPE (arg);
2894 if (TREE_CODE (type) == ARRAY_TYPE)
2896 else if (TREE_CODE (type) == ERROR_MARK)
2898 else if (real_lvalue_p (arg))
2899 type = build_reference_type (lvalue_type (arg));
2900 else if (MAYBE_CLASS_TYPE_P (type))
2901 type = lvalue_type (arg);
2903 return type;
2906 /* Does FUNCTION use a variable-length argument list? */
2909 varargs_function_p (const_tree function)
2911 return stdarg_p (TREE_TYPE (function));
2914 /* Returns 1 if decl is a member of a class. */
2917 member_p (const_tree decl)
2919 const_tree const ctx = DECL_CONTEXT (decl);
2920 return (ctx && TYPE_P (ctx));
2923 /* Create a placeholder for member access where we don't actually have an
2924 object that the access is against. */
2926 tree
2927 build_dummy_object (tree type)
2929 tree decl = build1 (NOP_EXPR, build_pointer_type (type), void_zero_node);
2930 return cp_build_indirect_ref (decl, RO_NULL, tf_warning_or_error);
2933 /* We've gotten a reference to a member of TYPE. Return *this if appropriate,
2934 or a dummy object otherwise. If BINFOP is non-0, it is filled with the
2935 binfo path from current_class_type to TYPE, or 0. */
2937 tree
2938 maybe_dummy_object (tree type, tree* binfop)
2940 tree decl, context;
2941 tree binfo;
2942 tree current = current_nonlambda_class_type ();
2944 if (current
2945 && (binfo = lookup_base (current, type, ba_any, NULL,
2946 tf_warning_or_error)))
2947 context = current;
2948 else
2950 /* Reference from a nested class member function. */
2951 context = type;
2952 binfo = TYPE_BINFO (type);
2955 if (binfop)
2956 *binfop = binfo;
2958 if (current_class_ref
2959 /* current_class_ref might not correspond to current_class_type if
2960 we're in tsubst_default_argument or a lambda-declarator; in either
2961 case, we want to use current_class_ref if it matches CONTEXT. */
2962 && (same_type_ignoring_top_level_qualifiers_p
2963 (TREE_TYPE (current_class_ref), context)))
2964 decl = current_class_ref;
2965 else
2966 decl = build_dummy_object (context);
2968 return decl;
2971 /* Returns 1 if OB is a placeholder object, or a pointer to one. */
2974 is_dummy_object (const_tree ob)
2976 if (INDIRECT_REF_P (ob))
2977 ob = TREE_OPERAND (ob, 0);
2978 return (TREE_CODE (ob) == NOP_EXPR
2979 && TREE_OPERAND (ob, 0) == void_zero_node);
2982 /* Returns 1 iff type T is something we want to treat as a scalar type for
2983 the purpose of deciding whether it is trivial/POD/standard-layout. */
2985 bool
2986 scalarish_type_p (const_tree t)
2988 if (t == error_mark_node)
2989 return 1;
2991 return (SCALAR_TYPE_P (t)
2992 || TREE_CODE (t) == VECTOR_TYPE);
2995 /* Returns true iff T requires non-trivial default initialization. */
2997 bool
2998 type_has_nontrivial_default_init (const_tree t)
3000 t = strip_array_types (CONST_CAST_TREE (t));
3002 if (CLASS_TYPE_P (t))
3003 return TYPE_HAS_COMPLEX_DFLT (t);
3004 else
3005 return 0;
3008 /* Returns true iff copying an object of type T (including via move
3009 constructor) is non-trivial. That is, T has no non-trivial copy
3010 constructors and no non-trivial move constructors. */
3012 bool
3013 type_has_nontrivial_copy_init (const_tree t)
3015 t = strip_array_types (CONST_CAST_TREE (t));
3017 if (CLASS_TYPE_P (t))
3019 gcc_assert (COMPLETE_TYPE_P (t));
3020 return ((TYPE_HAS_COPY_CTOR (t)
3021 && TYPE_HAS_COMPLEX_COPY_CTOR (t))
3022 || TYPE_HAS_COMPLEX_MOVE_CTOR (t));
3024 else
3025 return 0;
3028 /* Returns 1 iff type T is a trivially copyable type, as defined in
3029 [basic.types] and [class]. */
3031 bool
3032 trivially_copyable_p (const_tree t)
3034 t = strip_array_types (CONST_CAST_TREE (t));
3036 if (CLASS_TYPE_P (t))
3037 return ((!TYPE_HAS_COPY_CTOR (t)
3038 || !TYPE_HAS_COMPLEX_COPY_CTOR (t))
3039 && !TYPE_HAS_COMPLEX_MOVE_CTOR (t)
3040 && (!TYPE_HAS_COPY_ASSIGN (t)
3041 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (t))
3042 && !TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
3043 && TYPE_HAS_TRIVIAL_DESTRUCTOR (t));
3044 else
3045 return scalarish_type_p (t);
3048 /* Returns 1 iff type T is a trivial type, as defined in [basic.types] and
3049 [class]. */
3051 bool
3052 trivial_type_p (const_tree t)
3054 t = strip_array_types (CONST_CAST_TREE (t));
3056 if (CLASS_TYPE_P (t))
3057 return (TYPE_HAS_TRIVIAL_DFLT (t)
3058 && trivially_copyable_p (t));
3059 else
3060 return scalarish_type_p (t);
3063 /* Returns 1 iff type T is a POD type, as defined in [basic.types]. */
3065 bool
3066 pod_type_p (const_tree t)
3068 /* This CONST_CAST is okay because strip_array_types returns its
3069 argument unmodified and we assign it to a const_tree. */
3070 t = strip_array_types (CONST_CAST_TREE(t));
3072 if (!CLASS_TYPE_P (t))
3073 return scalarish_type_p (t);
3074 else if (cxx_dialect > cxx98)
3075 /* [class]/10: A POD struct is a class that is both a trivial class and a
3076 standard-layout class, and has no non-static data members of type
3077 non-POD struct, non-POD union (or array of such types).
3079 We don't need to check individual members because if a member is
3080 non-std-layout or non-trivial, the class will be too. */
3081 return (std_layout_type_p (t) && trivial_type_p (t));
3082 else
3083 /* The C++98 definition of POD is different. */
3084 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
3087 /* Returns true iff T is POD for the purpose of layout, as defined in the
3088 C++ ABI. */
3090 bool
3091 layout_pod_type_p (const_tree t)
3093 t = strip_array_types (CONST_CAST_TREE (t));
3095 if (CLASS_TYPE_P (t))
3096 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
3097 else
3098 return scalarish_type_p (t);
3101 /* Returns true iff T is a standard-layout type, as defined in
3102 [basic.types]. */
3104 bool
3105 std_layout_type_p (const_tree t)
3107 t = strip_array_types (CONST_CAST_TREE (t));
3109 if (CLASS_TYPE_P (t))
3110 return !CLASSTYPE_NON_STD_LAYOUT (t);
3111 else
3112 return scalarish_type_p (t);
3115 /* Nonzero iff type T is a class template implicit specialization. */
3117 bool
3118 class_tmpl_impl_spec_p (const_tree t)
3120 return CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INSTANTIATION (t);
3123 /* Returns 1 iff zero initialization of type T means actually storing
3124 zeros in it. */
3127 zero_init_p (const_tree t)
3129 /* This CONST_CAST is okay because strip_array_types returns its
3130 argument unmodified and we assign it to a const_tree. */
3131 t = strip_array_types (CONST_CAST_TREE(t));
3133 if (t == error_mark_node)
3134 return 1;
3136 /* NULL pointers to data members are initialized with -1. */
3137 if (TYPE_PTRDATAMEM_P (t))
3138 return 0;
3140 /* Classes that contain types that can't be zero-initialized, cannot
3141 be zero-initialized themselves. */
3142 if (CLASS_TYPE_P (t) && CLASSTYPE_NON_ZERO_INIT_P (t))
3143 return 0;
3145 return 1;
3148 /* Table of valid C++ attributes. */
3149 const struct attribute_spec cxx_attribute_table[] =
3151 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
3152 affects_type_identity } */
3153 { "java_interface", 0, 0, false, false, false,
3154 handle_java_interface_attribute, false },
3155 { "com_interface", 0, 0, false, false, false,
3156 handle_com_interface_attribute, false },
3157 { "init_priority", 1, 1, true, false, false,
3158 handle_init_priority_attribute, false },
3159 { "abi_tag", 1, -1, false, false, false,
3160 handle_abi_tag_attribute, true },
3161 { NULL, 0, 0, false, false, false, NULL, false }
3164 /* Handle a "java_interface" attribute; arguments as in
3165 struct attribute_spec.handler. */
3166 static tree
3167 handle_java_interface_attribute (tree* node,
3168 tree name,
3169 tree /*args*/,
3170 int flags,
3171 bool* no_add_attrs)
3173 if (DECL_P (*node)
3174 || !CLASS_TYPE_P (*node)
3175 || !TYPE_FOR_JAVA (*node))
3177 error ("%qE attribute can only be applied to Java class definitions",
3178 name);
3179 *no_add_attrs = true;
3180 return NULL_TREE;
3182 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
3183 *node = build_variant_type_copy (*node);
3184 TYPE_JAVA_INTERFACE (*node) = 1;
3186 return NULL_TREE;
3189 /* Handle a "com_interface" attribute; arguments as in
3190 struct attribute_spec.handler. */
3191 static tree
3192 handle_com_interface_attribute (tree* node,
3193 tree name,
3194 tree /*args*/,
3195 int /*flags*/,
3196 bool* no_add_attrs)
3198 static int warned;
3200 *no_add_attrs = true;
3202 if (DECL_P (*node)
3203 || !CLASS_TYPE_P (*node)
3204 || *node != TYPE_MAIN_VARIANT (*node))
3206 warning (OPT_Wattributes, "%qE attribute can only be applied "
3207 "to class definitions", name);
3208 return NULL_TREE;
3211 if (!warned++)
3212 warning (0, "%qE is obsolete; g++ vtables are now COM-compatible by default",
3213 name);
3215 return NULL_TREE;
3218 /* Handle an "init_priority" attribute; arguments as in
3219 struct attribute_spec.handler. */
3220 static tree
3221 handle_init_priority_attribute (tree* node,
3222 tree name,
3223 tree args,
3224 int /*flags*/,
3225 bool* no_add_attrs)
3227 tree initp_expr = TREE_VALUE (args);
3228 tree decl = *node;
3229 tree type = TREE_TYPE (decl);
3230 int pri;
3232 STRIP_NOPS (initp_expr);
3234 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
3236 error ("requested init_priority is not an integer constant");
3237 *no_add_attrs = true;
3238 return NULL_TREE;
3241 pri = TREE_INT_CST_LOW (initp_expr);
3243 type = strip_array_types (type);
3245 if (decl == NULL_TREE
3246 || !VAR_P (decl)
3247 || !TREE_STATIC (decl)
3248 || DECL_EXTERNAL (decl)
3249 || (TREE_CODE (type) != RECORD_TYPE
3250 && TREE_CODE (type) != UNION_TYPE)
3251 /* Static objects in functions are initialized the
3252 first time control passes through that
3253 function. This is not precise enough to pin down an
3254 init_priority value, so don't allow it. */
3255 || current_function_decl)
3257 error ("can only use %qE attribute on file-scope definitions "
3258 "of objects of class type", name);
3259 *no_add_attrs = true;
3260 return NULL_TREE;
3263 if (pri > MAX_INIT_PRIORITY || pri <= 0)
3265 error ("requested init_priority is out of range");
3266 *no_add_attrs = true;
3267 return NULL_TREE;
3270 /* Check for init_priorities that are reserved for
3271 language and runtime support implementations.*/
3272 if (pri <= MAX_RESERVED_INIT_PRIORITY)
3274 warning
3275 (0, "requested init_priority is reserved for internal use");
3278 if (SUPPORTS_INIT_PRIORITY)
3280 SET_DECL_INIT_PRIORITY (decl, pri);
3281 DECL_HAS_INIT_PRIORITY_P (decl) = 1;
3282 return NULL_TREE;
3284 else
3286 error ("%qE attribute is not supported on this platform", name);
3287 *no_add_attrs = true;
3288 return NULL_TREE;
3292 /* DECL is being redeclared; the old declaration had the abi tags in OLD,
3293 and the new one has the tags in NEW_. Give an error if there are tags
3294 in NEW_ that weren't in OLD. */
3296 bool
3297 check_abi_tag_redeclaration (const_tree decl, const_tree old, const_tree new_)
3299 if (old && TREE_CODE (TREE_VALUE (old)) == TREE_LIST)
3300 old = TREE_VALUE (old);
3301 if (new_ && TREE_CODE (TREE_VALUE (new_)) == TREE_LIST)
3302 new_ = TREE_VALUE (new_);
3303 bool err = false;
3304 for (const_tree t = new_; t; t = TREE_CHAIN (t))
3306 tree str = TREE_VALUE (t);
3307 for (const_tree in = old; in; in = TREE_CHAIN (in))
3309 tree ostr = TREE_VALUE (in);
3310 if (cp_tree_equal (str, ostr))
3311 goto found;
3313 error ("redeclaration of %qD adds abi tag %E", decl, str);
3314 err = true;
3315 found:;
3317 if (err)
3319 inform (DECL_SOURCE_LOCATION (decl), "previous declaration here");
3320 return false;
3322 return true;
3325 /* Handle an "abi_tag" attribute; arguments as in
3326 struct attribute_spec.handler. */
3328 static tree
3329 handle_abi_tag_attribute (tree* node, tree name, tree args,
3330 int flags, bool* no_add_attrs)
3332 if (TYPE_P (*node))
3334 if (!OVERLOAD_TYPE_P (*node))
3336 error ("%qE attribute applied to non-class, non-enum type %qT",
3337 name, *node);
3338 goto fail;
3340 else if (!(flags & (int)ATTR_FLAG_TYPE_IN_PLACE))
3342 error ("%qE attribute applied to %qT after its definition",
3343 name, *node);
3344 goto fail;
3347 tree attributes = TYPE_ATTRIBUTES (*node);
3348 tree decl = TYPE_NAME (*node);
3350 /* Make sure all declarations have the same abi tags. */
3351 if (DECL_SOURCE_LOCATION (decl) != input_location)
3353 if (!check_abi_tag_redeclaration (decl,
3354 lookup_attribute ("abi_tag",
3355 attributes),
3356 args))
3357 goto fail;
3360 else
3362 if (TREE_CODE (*node) != FUNCTION_DECL)
3364 error ("%qE attribute applied to non-function %qD", name, *node);
3365 goto fail;
3367 else if (DECL_LANGUAGE (*node) == lang_c)
3369 error ("%qE attribute applied to extern \"C\" function %qD",
3370 name, *node);
3371 goto fail;
3375 return NULL_TREE;
3377 fail:
3378 *no_add_attrs = true;
3379 return NULL_TREE;
3382 /* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the
3383 thing pointed to by the constant. */
3385 tree
3386 make_ptrmem_cst (tree type, tree member)
3388 tree ptrmem_cst = make_node (PTRMEM_CST);
3389 TREE_TYPE (ptrmem_cst) = type;
3390 PTRMEM_CST_MEMBER (ptrmem_cst) = member;
3391 return ptrmem_cst;
3394 /* Build a variant of TYPE that has the indicated ATTRIBUTES. May
3395 return an existing type if an appropriate type already exists. */
3397 tree
3398 cp_build_type_attribute_variant (tree type, tree attributes)
3400 tree new_type;
3402 new_type = build_type_attribute_variant (type, attributes);
3403 if (TREE_CODE (new_type) == FUNCTION_TYPE
3404 || TREE_CODE (new_type) == METHOD_TYPE)
3406 new_type = build_exception_variant (new_type,
3407 TYPE_RAISES_EXCEPTIONS (type));
3408 new_type = build_ref_qualified_type (new_type,
3409 type_memfn_rqual (type));
3412 /* Making a new main variant of a class type is broken. */
3413 gcc_assert (!CLASS_TYPE_P (type) || new_type == type);
3415 return new_type;
3418 /* Return TRUE if TYPE1 and TYPE2 are identical for type hashing purposes.
3419 Called only after doing all language independent checks. Only
3420 to check TYPE_RAISES_EXCEPTIONS for FUNCTION_TYPE, the rest is already
3421 compared in type_hash_eq. */
3423 bool
3424 cxx_type_hash_eq (const_tree typea, const_tree typeb)
3426 gcc_assert (TREE_CODE (typea) == FUNCTION_TYPE
3427 || TREE_CODE (typea) == METHOD_TYPE);
3429 return comp_except_specs (TYPE_RAISES_EXCEPTIONS (typea),
3430 TYPE_RAISES_EXCEPTIONS (typeb), ce_exact);
3433 /* Apply FUNC to all language-specific sub-trees of TP in a pre-order
3434 traversal. Called from walk_tree. */
3436 tree
3437 cp_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func,
3438 void *data, struct pointer_set_t *pset)
3440 enum tree_code code = TREE_CODE (*tp);
3441 tree result;
3443 #define WALK_SUBTREE(NODE) \
3444 do \
3446 result = cp_walk_tree (&(NODE), func, data, pset); \
3447 if (result) goto out; \
3449 while (0)
3451 /* Not one of the easy cases. We must explicitly go through the
3452 children. */
3453 result = NULL_TREE;
3454 switch (code)
3456 case DEFAULT_ARG:
3457 case TEMPLATE_TEMPLATE_PARM:
3458 case BOUND_TEMPLATE_TEMPLATE_PARM:
3459 case UNBOUND_CLASS_TEMPLATE:
3460 case TEMPLATE_PARM_INDEX:
3461 case TEMPLATE_TYPE_PARM:
3462 case TYPENAME_TYPE:
3463 case TYPEOF_TYPE:
3464 case UNDERLYING_TYPE:
3465 /* None of these have subtrees other than those already walked
3466 above. */
3467 *walk_subtrees_p = 0;
3468 break;
3470 case BASELINK:
3471 WALK_SUBTREE (BASELINK_FUNCTIONS (*tp));
3472 *walk_subtrees_p = 0;
3473 break;
3475 case PTRMEM_CST:
3476 WALK_SUBTREE (TREE_TYPE (*tp));
3477 *walk_subtrees_p = 0;
3478 break;
3480 case TREE_LIST:
3481 WALK_SUBTREE (TREE_PURPOSE (*tp));
3482 break;
3484 case OVERLOAD:
3485 WALK_SUBTREE (OVL_FUNCTION (*tp));
3486 WALK_SUBTREE (OVL_CHAIN (*tp));
3487 *walk_subtrees_p = 0;
3488 break;
3490 case USING_DECL:
3491 WALK_SUBTREE (DECL_NAME (*tp));
3492 WALK_SUBTREE (USING_DECL_SCOPE (*tp));
3493 WALK_SUBTREE (USING_DECL_DECLS (*tp));
3494 *walk_subtrees_p = 0;
3495 break;
3497 case RECORD_TYPE:
3498 if (TYPE_PTRMEMFUNC_P (*tp))
3499 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (*tp));
3500 break;
3502 case TYPE_ARGUMENT_PACK:
3503 case NONTYPE_ARGUMENT_PACK:
3505 tree args = ARGUMENT_PACK_ARGS (*tp);
3506 int i, len = TREE_VEC_LENGTH (args);
3507 for (i = 0; i < len; i++)
3508 WALK_SUBTREE (TREE_VEC_ELT (args, i));
3510 break;
3512 case TYPE_PACK_EXPANSION:
3513 WALK_SUBTREE (TREE_TYPE (*tp));
3514 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp));
3515 *walk_subtrees_p = 0;
3516 break;
3518 case EXPR_PACK_EXPANSION:
3519 WALK_SUBTREE (TREE_OPERAND (*tp, 0));
3520 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp));
3521 *walk_subtrees_p = 0;
3522 break;
3524 case CAST_EXPR:
3525 case REINTERPRET_CAST_EXPR:
3526 case STATIC_CAST_EXPR:
3527 case CONST_CAST_EXPR:
3528 case DYNAMIC_CAST_EXPR:
3529 case IMPLICIT_CONV_EXPR:
3530 if (TREE_TYPE (*tp))
3531 WALK_SUBTREE (TREE_TYPE (*tp));
3534 int i;
3535 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (*tp)); ++i)
3536 WALK_SUBTREE (TREE_OPERAND (*tp, i));
3538 *walk_subtrees_p = 0;
3539 break;
3541 case TRAIT_EXPR:
3542 WALK_SUBTREE (TRAIT_EXPR_TYPE1 (*tp));
3543 WALK_SUBTREE (TRAIT_EXPR_TYPE2 (*tp));
3544 *walk_subtrees_p = 0;
3545 break;
3547 case DECLTYPE_TYPE:
3548 WALK_SUBTREE (DECLTYPE_TYPE_EXPR (*tp));
3549 *walk_subtrees_p = 0;
3550 break;
3553 default:
3554 return NULL_TREE;
3557 /* We didn't find what we were looking for. */
3558 out:
3559 return result;
3561 #undef WALK_SUBTREE
3564 /* Like save_expr, but for C++. */
3566 tree
3567 cp_save_expr (tree expr)
3569 /* There is no reason to create a SAVE_EXPR within a template; if
3570 needed, we can create the SAVE_EXPR when instantiating the
3571 template. Furthermore, the middle-end cannot handle C++-specific
3572 tree codes. */
3573 if (processing_template_decl)
3574 return expr;
3575 return save_expr (expr);
3578 /* Initialize tree.c. */
3580 void
3581 init_tree (void)
3583 list_hash_table = htab_create_ggc (31, list_hash, list_hash_eq, NULL);
3586 /* Returns the kind of special function that DECL (a FUNCTION_DECL)
3587 is. Note that sfk_none is zero, so this function can be used as a
3588 predicate to test whether or not DECL is a special function. */
3590 special_function_kind
3591 special_function_p (const_tree decl)
3593 /* Rather than doing all this stuff with magic names, we should
3594 probably have a field of type `special_function_kind' in
3595 DECL_LANG_SPECIFIC. */
3596 if (DECL_INHERITED_CTOR_BASE (decl))
3597 return sfk_inheriting_constructor;
3598 if (DECL_COPY_CONSTRUCTOR_P (decl))
3599 return sfk_copy_constructor;
3600 if (DECL_MOVE_CONSTRUCTOR_P (decl))
3601 return sfk_move_constructor;
3602 if (DECL_CONSTRUCTOR_P (decl))
3603 return sfk_constructor;
3604 if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
3606 if (copy_fn_p (decl))
3607 return sfk_copy_assignment;
3608 if (move_fn_p (decl))
3609 return sfk_move_assignment;
3611 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
3612 return sfk_destructor;
3613 if (DECL_COMPLETE_DESTRUCTOR_P (decl))
3614 return sfk_complete_destructor;
3615 if (DECL_BASE_DESTRUCTOR_P (decl))
3616 return sfk_base_destructor;
3617 if (DECL_DELETING_DESTRUCTOR_P (decl))
3618 return sfk_deleting_destructor;
3619 if (DECL_CONV_FN_P (decl))
3620 return sfk_conversion;
3622 return sfk_none;
3625 /* Returns nonzero if TYPE is a character type, including wchar_t. */
3628 char_type_p (tree type)
3630 return (same_type_p (type, char_type_node)
3631 || same_type_p (type, unsigned_char_type_node)
3632 || same_type_p (type, signed_char_type_node)
3633 || same_type_p (type, char16_type_node)
3634 || same_type_p (type, char32_type_node)
3635 || same_type_p (type, wchar_type_node));
3638 /* Returns the kind of linkage associated with the indicated DECL. Th
3639 value returned is as specified by the language standard; it is
3640 independent of implementation details regarding template
3641 instantiation, etc. For example, it is possible that a declaration
3642 to which this function assigns external linkage would not show up
3643 as a global symbol when you run `nm' on the resulting object file. */
3645 linkage_kind
3646 decl_linkage (tree decl)
3648 /* This function doesn't attempt to calculate the linkage from first
3649 principles as given in [basic.link]. Instead, it makes use of
3650 the fact that we have already set TREE_PUBLIC appropriately, and
3651 then handles a few special cases. Ideally, we would calculate
3652 linkage first, and then transform that into a concrete
3653 implementation. */
3655 /* Things that don't have names have no linkage. */
3656 if (!DECL_NAME (decl))
3657 return lk_none;
3659 /* Fields have no linkage. */
3660 if (TREE_CODE (decl) == FIELD_DECL)
3661 return lk_none;
3663 /* Things that are TREE_PUBLIC have external linkage. */
3664 if (TREE_PUBLIC (decl))
3665 return lk_external;
3667 if (TREE_CODE (decl) == NAMESPACE_DECL)
3668 return lk_external;
3670 /* Linkage of a CONST_DECL depends on the linkage of the enumeration
3671 type. */
3672 if (TREE_CODE (decl) == CONST_DECL)
3673 return decl_linkage (TYPE_NAME (DECL_CONTEXT (decl)));
3675 /* Some things that are not TREE_PUBLIC have external linkage, too.
3676 For example, on targets that don't have weak symbols, we make all
3677 template instantiations have internal linkage (in the object
3678 file), but the symbols should still be treated as having external
3679 linkage from the point of view of the language. */
3680 if (VAR_OR_FUNCTION_DECL_P (decl)
3681 && DECL_COMDAT (decl))
3682 return lk_external;
3684 /* Things in local scope do not have linkage, if they don't have
3685 TREE_PUBLIC set. */
3686 if (decl_function_context (decl))
3687 return lk_none;
3689 /* Members of the anonymous namespace also have TREE_PUBLIC unset, but
3690 are considered to have external linkage for language purposes. DECLs
3691 really meant to have internal linkage have DECL_THIS_STATIC set. */
3692 if (TREE_CODE (decl) == TYPE_DECL)
3693 return lk_external;
3694 if (VAR_OR_FUNCTION_DECL_P (decl))
3696 if (!DECL_THIS_STATIC (decl))
3697 return lk_external;
3699 /* Static data members and static member functions from classes
3700 in anonymous namespace also don't have TREE_PUBLIC set. */
3701 if (DECL_CLASS_CONTEXT (decl))
3702 return lk_external;
3705 /* Everything else has internal linkage. */
3706 return lk_internal;
3709 /* Returns the storage duration of the object or reference associated with
3710 the indicated DECL, which should be a VAR_DECL or PARM_DECL. */
3712 duration_kind
3713 decl_storage_duration (tree decl)
3715 if (TREE_CODE (decl) == PARM_DECL)
3716 return dk_auto;
3717 if (TREE_CODE (decl) == FUNCTION_DECL)
3718 return dk_static;
3719 gcc_assert (VAR_P (decl));
3720 if (!TREE_STATIC (decl)
3721 && !DECL_EXTERNAL (decl))
3722 return dk_auto;
3723 if (DECL_THREAD_LOCAL_P (decl))
3724 return dk_thread;
3725 return dk_static;
3728 /* EXP is an expression that we want to pre-evaluate. Returns (in
3729 *INITP) an expression that will perform the pre-evaluation. The
3730 value returned by this function is a side-effect free expression
3731 equivalent to the pre-evaluated expression. Callers must ensure
3732 that *INITP is evaluated before EXP. */
3734 tree
3735 stabilize_expr (tree exp, tree* initp)
3737 tree init_expr;
3739 if (!TREE_SIDE_EFFECTS (exp))
3740 init_expr = NULL_TREE;
3741 else if (VOID_TYPE_P (TREE_TYPE (exp)))
3743 init_expr = exp;
3744 exp = void_zero_node;
3746 /* There are no expressions with REFERENCE_TYPE, but there can be call
3747 arguments with such a type; just treat it as a pointer. */
3748 else if (TREE_CODE (TREE_TYPE (exp)) == REFERENCE_TYPE
3749 || SCALAR_TYPE_P (TREE_TYPE (exp))
3750 || !lvalue_or_rvalue_with_address_p (exp))
3752 init_expr = get_target_expr (exp);
3753 exp = TARGET_EXPR_SLOT (init_expr);
3755 else
3757 bool xval = !real_lvalue_p (exp);
3758 exp = cp_build_addr_expr (exp, tf_warning_or_error);
3759 init_expr = get_target_expr (exp);
3760 exp = TARGET_EXPR_SLOT (init_expr);
3761 exp = cp_build_indirect_ref (exp, RO_NULL, tf_warning_or_error);
3762 if (xval)
3763 exp = move (exp);
3765 *initp = init_expr;
3767 gcc_assert (!TREE_SIDE_EFFECTS (exp));
3768 return exp;
3771 /* Add NEW_EXPR, an expression whose value we don't care about, after the
3772 similar expression ORIG. */
3774 tree
3775 add_stmt_to_compound (tree orig, tree new_expr)
3777 if (!new_expr || !TREE_SIDE_EFFECTS (new_expr))
3778 return orig;
3779 if (!orig || !TREE_SIDE_EFFECTS (orig))
3780 return new_expr;
3781 return build2 (COMPOUND_EXPR, void_type_node, orig, new_expr);
3784 /* Like stabilize_expr, but for a call whose arguments we want to
3785 pre-evaluate. CALL is modified in place to use the pre-evaluated
3786 arguments, while, upon return, *INITP contains an expression to
3787 compute the arguments. */
3789 void
3790 stabilize_call (tree call, tree *initp)
3792 tree inits = NULL_TREE;
3793 int i;
3794 int nargs = call_expr_nargs (call);
3796 if (call == error_mark_node || processing_template_decl)
3798 *initp = NULL_TREE;
3799 return;
3802 gcc_assert (TREE_CODE (call) == CALL_EXPR);
3804 for (i = 0; i < nargs; i++)
3806 tree init;
3807 CALL_EXPR_ARG (call, i) =
3808 stabilize_expr (CALL_EXPR_ARG (call, i), &init);
3809 inits = add_stmt_to_compound (inits, init);
3812 *initp = inits;
3815 /* Like stabilize_expr, but for an AGGR_INIT_EXPR whose arguments we want
3816 to pre-evaluate. CALL is modified in place to use the pre-evaluated
3817 arguments, while, upon return, *INITP contains an expression to
3818 compute the arguments. */
3820 static void
3821 stabilize_aggr_init (tree call, tree *initp)
3823 tree inits = NULL_TREE;
3824 int i;
3825 int nargs = aggr_init_expr_nargs (call);
3827 if (call == error_mark_node)
3828 return;
3830 gcc_assert (TREE_CODE (call) == AGGR_INIT_EXPR);
3832 for (i = 0; i < nargs; i++)
3834 tree init;
3835 AGGR_INIT_EXPR_ARG (call, i) =
3836 stabilize_expr (AGGR_INIT_EXPR_ARG (call, i), &init);
3837 inits = add_stmt_to_compound (inits, init);
3840 *initp = inits;
3843 /* Like stabilize_expr, but for an initialization.
3845 If the initialization is for an object of class type, this function
3846 takes care not to introduce additional temporaries.
3848 Returns TRUE iff the expression was successfully pre-evaluated,
3849 i.e., if INIT is now side-effect free, except for, possibly, a
3850 single call to a constructor. */
3852 bool
3853 stabilize_init (tree init, tree *initp)
3855 tree t = init;
3857 *initp = NULL_TREE;
3859 if (t == error_mark_node || processing_template_decl)
3860 return true;
3862 if (TREE_CODE (t) == INIT_EXPR)
3863 t = TREE_OPERAND (t, 1);
3864 if (TREE_CODE (t) == TARGET_EXPR)
3865 t = TARGET_EXPR_INITIAL (t);
3867 /* If the RHS can be stabilized without breaking copy elision, stabilize
3868 it. We specifically don't stabilize class prvalues here because that
3869 would mean an extra copy, but they might be stabilized below. */
3870 if (TREE_CODE (init) == INIT_EXPR
3871 && TREE_CODE (t) != CONSTRUCTOR
3872 && TREE_CODE (t) != AGGR_INIT_EXPR
3873 && (SCALAR_TYPE_P (TREE_TYPE (t))
3874 || lvalue_or_rvalue_with_address_p (t)))
3876 TREE_OPERAND (init, 1) = stabilize_expr (t, initp);
3877 return true;
3880 if (TREE_CODE (t) == COMPOUND_EXPR
3881 && TREE_CODE (init) == INIT_EXPR)
3883 tree last = expr_last (t);
3884 /* Handle stabilizing the EMPTY_CLASS_EXPR pattern. */
3885 if (!TREE_SIDE_EFFECTS (last))
3887 *initp = t;
3888 TREE_OPERAND (init, 1) = last;
3889 return true;
3893 if (TREE_CODE (t) == CONSTRUCTOR)
3895 /* Aggregate initialization: stabilize each of the field
3896 initializers. */
3897 unsigned i;
3898 constructor_elt *ce;
3899 bool good = true;
3900 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
3901 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
3903 tree type = TREE_TYPE (ce->value);
3904 tree subinit;
3905 if (TREE_CODE (type) == REFERENCE_TYPE
3906 || SCALAR_TYPE_P (type))
3907 ce->value = stabilize_expr (ce->value, &subinit);
3908 else if (!stabilize_init (ce->value, &subinit))
3909 good = false;
3910 *initp = add_stmt_to_compound (*initp, subinit);
3912 return good;
3915 if (TREE_CODE (t) == CALL_EXPR)
3917 stabilize_call (t, initp);
3918 return true;
3921 if (TREE_CODE (t) == AGGR_INIT_EXPR)
3923 stabilize_aggr_init (t, initp);
3924 return true;
3927 /* The initialization is being performed via a bitwise copy -- and
3928 the item copied may have side effects. */
3929 return !TREE_SIDE_EFFECTS (init);
3932 /* Like "fold", but should be used whenever we might be processing the
3933 body of a template. */
3935 tree
3936 fold_if_not_in_template (tree expr)
3938 /* In the body of a template, there is never any need to call
3939 "fold". We will call fold later when actually instantiating the
3940 template. Integral constant expressions in templates will be
3941 evaluated via fold_non_dependent_expr, as necessary. */
3942 if (processing_template_decl)
3943 return expr;
3945 /* Fold C++ front-end specific tree codes. */
3946 if (TREE_CODE (expr) == UNARY_PLUS_EXPR)
3947 return fold_convert (TREE_TYPE (expr), TREE_OPERAND (expr, 0));
3949 return fold (expr);
3952 /* Returns true if a cast to TYPE may appear in an integral constant
3953 expression. */
3955 bool
3956 cast_valid_in_integral_constant_expression_p (tree type)
3958 return (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3959 || cxx_dialect >= cxx11
3960 || dependent_type_p (type)
3961 || type == error_mark_node);
3964 /* Return true if we need to fix linkage information of DECL. */
3966 static bool
3967 cp_fix_function_decl_p (tree decl)
3969 /* Skip if DECL is not externally visible. */
3970 if (!TREE_PUBLIC (decl))
3971 return false;
3973 /* We need to fix DECL if it a appears to be exported but with no
3974 function body. Thunks do not have CFGs and we may need to
3975 handle them specially later. */
3976 if (!gimple_has_body_p (decl)
3977 && !DECL_THUNK_P (decl)
3978 && !DECL_EXTERNAL (decl))
3980 struct cgraph_node *node = cgraph_get_node (decl);
3982 /* Don't fix same_body aliases. Although they don't have their own
3983 CFG, they share it with what they alias to. */
3984 if (!node || !node->symbol.alias
3985 || !vec_safe_length (node->symbol.ref_list.references))
3986 return true;
3989 return false;
3992 /* Clean the C++ specific parts of the tree T. */
3994 void
3995 cp_free_lang_data (tree t)
3997 if (TREE_CODE (t) == METHOD_TYPE
3998 || TREE_CODE (t) == FUNCTION_TYPE)
4000 /* Default args are not interesting anymore. */
4001 tree argtypes = TYPE_ARG_TYPES (t);
4002 while (argtypes)
4004 TREE_PURPOSE (argtypes) = 0;
4005 argtypes = TREE_CHAIN (argtypes);
4008 else if (TREE_CODE (t) == FUNCTION_DECL
4009 && cp_fix_function_decl_p (t))
4011 /* If T is used in this translation unit at all, the definition
4012 must exist somewhere else since we have decided to not emit it
4013 in this TU. So make it an external reference. */
4014 DECL_EXTERNAL (t) = 1;
4015 TREE_STATIC (t) = 0;
4017 if (TREE_CODE (t) == NAMESPACE_DECL)
4019 /* The list of users of a namespace isn't useful for the middle-end
4020 or debug generators. */
4021 DECL_NAMESPACE_USERS (t) = NULL_TREE;
4022 /* Neither do we need the leftover chaining of namespaces
4023 from the binding level. */
4024 DECL_CHAIN (t) = NULL_TREE;
4028 /* Stub for c-common. Please keep in sync with c-decl.c.
4029 FIXME: If address space support is target specific, then this
4030 should be a C target hook. But currently this is not possible,
4031 because this function is called via REGISTER_TARGET_PRAGMAS. */
4032 void
4033 c_register_addr_space (const char * /*word*/, addr_space_t /*as*/)
4037 /* Return the number of operands in T that we care about for things like
4038 mangling. */
4041 cp_tree_operand_length (const_tree t)
4043 enum tree_code code = TREE_CODE (t);
4045 switch (code)
4047 case PREINCREMENT_EXPR:
4048 case PREDECREMENT_EXPR:
4049 case POSTINCREMENT_EXPR:
4050 case POSTDECREMENT_EXPR:
4051 return 1;
4053 case ARRAY_REF:
4054 return 2;
4056 case EXPR_PACK_EXPANSION:
4057 return 1;
4059 default:
4060 return TREE_OPERAND_LENGTH (t);
4064 /* Implement -Wzero_as_null_pointer_constant. Return true if the
4065 conditions for the warning hold, false otherwise. */
4066 bool
4067 maybe_warn_zero_as_null_pointer_constant (tree expr, location_t loc)
4069 if (c_inhibit_evaluation_warnings == 0
4070 && !NULLPTR_TYPE_P (TREE_TYPE (expr)))
4072 warning_at (loc, OPT_Wzero_as_null_pointer_constant,
4073 "zero as null pointer constant");
4074 return true;
4076 return false;
4079 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4080 /* Complain that some language-specific thing hanging off a tree
4081 node has been accessed improperly. */
4083 void
4084 lang_check_failed (const char* file, int line, const char* function)
4086 internal_error ("lang_* check: failed in %s, at %s:%d",
4087 function, trim_filename (file), line);
4089 #endif /* ENABLE_TREE_CHECKING */
4091 #include "gt-cp-tree.h"