Daily bump.
[official-gcc.git] / gcc / cp / tree.c
blobdf2981fd3f4279e52de8b247642a401738da89bd
1 /* Language-dependent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2016 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 "tree.h"
25 #include "cp-tree.h"
26 #include "gimple-expr.h"
27 #include "cgraph.h"
28 #include "stor-layout.h"
29 #include "print-tree.h"
30 #include "tree-iterator.h"
31 #include "tree-inline.h"
32 #include "debug.h"
33 #include "convert.h"
34 #include "gimplify.h"
35 #include "attribs.h"
37 static tree bot_manip (tree *, int *, void *);
38 static tree bot_replace (tree *, int *, void *);
39 static hashval_t list_hash_pieces (tree, tree, tree);
40 static tree build_target_expr (tree, tree, tsubst_flags_t);
41 static tree count_trees_r (tree *, int *, void *);
42 static tree verify_stmt_tree_r (tree *, int *, void *);
43 static tree build_local_temp (tree);
45 static tree handle_java_interface_attribute (tree *, tree, tree, int, bool *);
46 static tree handle_com_interface_attribute (tree *, tree, tree, int, bool *);
47 static tree handle_init_priority_attribute (tree *, tree, tree, int, bool *);
48 static tree handle_abi_tag_attribute (tree *, tree, tree, int, bool *);
50 /* If REF is an lvalue, returns the kind of lvalue that REF is.
51 Otherwise, returns clk_none. */
53 cp_lvalue_kind
54 lvalue_kind (const_tree ref)
56 cp_lvalue_kind op1_lvalue_kind = clk_none;
57 cp_lvalue_kind op2_lvalue_kind = clk_none;
59 /* Expressions of reference type are sometimes wrapped in
60 INDIRECT_REFs. INDIRECT_REFs are just internal compiler
61 representation, not part of the language, so we have to look
62 through them. */
63 if (REFERENCE_REF_P (ref))
64 return lvalue_kind (TREE_OPERAND (ref, 0));
66 if (TREE_TYPE (ref)
67 && TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
69 /* unnamed rvalue references are rvalues */
70 if (TYPE_REF_IS_RVALUE (TREE_TYPE (ref))
71 && TREE_CODE (ref) != PARM_DECL
72 && !VAR_P (ref)
73 && TREE_CODE (ref) != COMPONENT_REF
74 /* Functions are always lvalues. */
75 && TREE_CODE (TREE_TYPE (TREE_TYPE (ref))) != FUNCTION_TYPE)
76 return clk_rvalueref;
78 /* lvalue references and named rvalue references are lvalues. */
79 return clk_ordinary;
82 if (ref == current_class_ptr)
83 return clk_none;
85 switch (TREE_CODE (ref))
87 case SAVE_EXPR:
88 return clk_none;
89 /* preincrements and predecrements are valid lvals, provided
90 what they refer to are valid lvals. */
91 case PREINCREMENT_EXPR:
92 case PREDECREMENT_EXPR:
93 case TRY_CATCH_EXPR:
94 case WITH_CLEANUP_EXPR:
95 case REALPART_EXPR:
96 case IMAGPART_EXPR:
97 return lvalue_kind (TREE_OPERAND (ref, 0));
99 case MEMBER_REF:
100 case DOTSTAR_EXPR:
101 if (TREE_CODE (ref) == MEMBER_REF)
102 op1_lvalue_kind = clk_ordinary;
103 else
104 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
105 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
106 op1_lvalue_kind = clk_none;
107 return op1_lvalue_kind;
109 case COMPONENT_REF:
110 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
111 /* Look at the member designator. */
112 if (!op1_lvalue_kind)
114 else if (is_overloaded_fn (TREE_OPERAND (ref, 1)))
115 /* The "field" can be a FUNCTION_DECL or an OVERLOAD in some
116 situations. If we're seeing a COMPONENT_REF, it's a non-static
117 member, so it isn't an lvalue. */
118 op1_lvalue_kind = clk_none;
119 else if (TREE_CODE (TREE_OPERAND (ref, 1)) != FIELD_DECL)
120 /* This can be IDENTIFIER_NODE in a template. */;
121 else if (DECL_C_BIT_FIELD (TREE_OPERAND (ref, 1)))
123 /* Clear the ordinary bit. If this object was a class
124 rvalue we want to preserve that information. */
125 op1_lvalue_kind &= ~clk_ordinary;
126 /* The lvalue is for a bitfield. */
127 op1_lvalue_kind |= clk_bitfield;
129 else if (DECL_PACKED (TREE_OPERAND (ref, 1)))
130 op1_lvalue_kind |= clk_packed;
132 return op1_lvalue_kind;
134 case STRING_CST:
135 case COMPOUND_LITERAL_EXPR:
136 return clk_ordinary;
138 case CONST_DECL:
139 /* CONST_DECL without TREE_STATIC are enumeration values and
140 thus not lvalues. With TREE_STATIC they are used by ObjC++
141 in objc_build_string_object and need to be considered as
142 lvalues. */
143 if (! TREE_STATIC (ref))
144 return clk_none;
145 case VAR_DECL:
146 if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
147 && DECL_LANG_SPECIFIC (ref)
148 && DECL_IN_AGGR_P (ref))
149 return clk_none;
150 case INDIRECT_REF:
151 case ARROW_EXPR:
152 case ARRAY_REF:
153 case ARRAY_NOTATION_REF:
154 case PARM_DECL:
155 case RESULT_DECL:
156 case PLACEHOLDER_EXPR:
157 return clk_ordinary;
159 /* A scope ref in a template, left as SCOPE_REF to support later
160 access checking. */
161 case SCOPE_REF:
162 gcc_assert (!type_dependent_expression_p (CONST_CAST_TREE (ref)));
164 tree op = TREE_OPERAND (ref, 1);
165 if (TREE_CODE (op) == FIELD_DECL)
166 return (DECL_C_BIT_FIELD (op) ? clk_bitfield : clk_ordinary);
167 else
168 return lvalue_kind (op);
171 case MAX_EXPR:
172 case MIN_EXPR:
173 /* Disallow <? and >? as lvalues if either argument side-effects. */
174 if (TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 0))
175 || TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 1)))
176 return clk_none;
177 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
178 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1));
179 break;
181 case COND_EXPR:
182 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1)
183 ? TREE_OPERAND (ref, 1)
184 : TREE_OPERAND (ref, 0));
185 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 2));
186 break;
188 case MODOP_EXPR:
189 /* We expect to see unlowered MODOP_EXPRs only during
190 template processing. */
191 gcc_assert (processing_template_decl);
192 return clk_ordinary;
194 case MODIFY_EXPR:
195 case TYPEID_EXPR:
196 return clk_ordinary;
198 case COMPOUND_EXPR:
199 return lvalue_kind (TREE_OPERAND (ref, 1));
201 case TARGET_EXPR:
202 return clk_class;
204 case VA_ARG_EXPR:
205 return (CLASS_TYPE_P (TREE_TYPE (ref)) ? clk_class : clk_none);
207 case CALL_EXPR:
208 /* We can see calls outside of TARGET_EXPR in templates. */
209 if (CLASS_TYPE_P (TREE_TYPE (ref)))
210 return clk_class;
211 return clk_none;
213 case FUNCTION_DECL:
214 /* All functions (except non-static-member functions) are
215 lvalues. */
216 return (DECL_NONSTATIC_MEMBER_FUNCTION_P (ref)
217 ? clk_none : clk_ordinary);
219 case BASELINK:
220 /* We now represent a reference to a single static member function
221 with a BASELINK. */
222 /* This CONST_CAST is okay because BASELINK_FUNCTIONS returns
223 its argument unmodified and we assign it to a const_tree. */
224 return lvalue_kind (BASELINK_FUNCTIONS (CONST_CAST_TREE (ref)));
226 case NON_DEPENDENT_EXPR:
227 return lvalue_kind (TREE_OPERAND (ref, 0));
229 default:
230 if (!TREE_TYPE (ref))
231 return clk_none;
232 if (CLASS_TYPE_P (TREE_TYPE (ref)))
233 return clk_class;
234 break;
237 /* If one operand is not an lvalue at all, then this expression is
238 not an lvalue. */
239 if (!op1_lvalue_kind || !op2_lvalue_kind)
240 return clk_none;
242 /* Otherwise, it's an lvalue, and it has all the odd properties
243 contributed by either operand. */
244 op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind;
245 /* It's not an ordinary lvalue if it involves any other kind. */
246 if ((op1_lvalue_kind & ~clk_ordinary) != clk_none)
247 op1_lvalue_kind &= ~clk_ordinary;
248 /* It can't be both a pseudo-lvalue and a non-addressable lvalue.
249 A COND_EXPR of those should be wrapped in a TARGET_EXPR. */
250 if ((op1_lvalue_kind & (clk_rvalueref|clk_class))
251 && (op1_lvalue_kind & (clk_bitfield|clk_packed)))
252 op1_lvalue_kind = clk_none;
253 return op1_lvalue_kind;
256 /* Returns the kind of lvalue that REF is, in the sense of
257 [basic.lval]. This function should really be named lvalue_p; it
258 computes the C++ definition of lvalue. */
260 cp_lvalue_kind
261 real_lvalue_p (const_tree ref)
263 cp_lvalue_kind kind = lvalue_kind (ref);
264 if (kind & (clk_rvalueref|clk_class))
265 return clk_none;
266 else
267 return kind;
270 /* This differs from real_lvalue_p in that class rvalues are considered
271 lvalues. */
273 bool
274 lvalue_p (const_tree ref)
276 return (lvalue_kind (ref) != clk_none);
279 /* This differs from real_lvalue_p in that rvalues formed by dereferencing
280 rvalue references are considered rvalues. */
282 bool
283 lvalue_or_rvalue_with_address_p (const_tree ref)
285 cp_lvalue_kind kind = lvalue_kind (ref);
286 if (kind & clk_class)
287 return false;
288 else
289 return (kind != clk_none);
292 /* Returns true if REF is an xvalue, false otherwise. */
294 bool
295 xvalue_p (const_tree ref)
297 return (lvalue_kind (ref) == clk_rvalueref);
300 /* Test whether DECL is a builtin that may appear in a
301 constant-expression. */
303 bool
304 builtin_valid_in_constant_expr_p (const_tree decl)
306 if (!(TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl)))
307 /* Not a built-in. */
308 return false;
309 switch (DECL_FUNCTION_CODE (decl))
311 case BUILT_IN_CONSTANT_P:
312 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
313 /* These have constant results even if their operands are
314 non-constant. */
315 return true;
316 default:
317 return false;
321 /* Build a TARGET_EXPR, initializing the DECL with the VALUE. */
323 static tree
324 build_target_expr (tree decl, tree value, tsubst_flags_t complain)
326 tree t;
327 tree type = TREE_TYPE (decl);
329 gcc_checking_assert (VOID_TYPE_P (TREE_TYPE (value))
330 || TREE_TYPE (decl) == TREE_TYPE (value)
331 /* On ARM ctors return 'this'. */
332 || (TYPE_PTR_P (TREE_TYPE (value))
333 && TREE_CODE (value) == CALL_EXPR)
334 || useless_type_conversion_p (TREE_TYPE (decl),
335 TREE_TYPE (value)));
337 t = cxx_maybe_build_cleanup (decl, complain);
338 if (t == error_mark_node)
339 return error_mark_node;
340 t = build4 (TARGET_EXPR, type, decl, value, t, NULL_TREE);
341 if (EXPR_HAS_LOCATION (value))
342 SET_EXPR_LOCATION (t, EXPR_LOCATION (value));
343 /* We always set TREE_SIDE_EFFECTS so that expand_expr does not
344 ignore the TARGET_EXPR. If there really turn out to be no
345 side-effects, then the optimizer should be able to get rid of
346 whatever code is generated anyhow. */
347 TREE_SIDE_EFFECTS (t) = 1;
349 return t;
352 /* Return an undeclared local temporary of type TYPE for use in building a
353 TARGET_EXPR. */
355 static tree
356 build_local_temp (tree type)
358 tree slot = build_decl (input_location,
359 VAR_DECL, NULL_TREE, type);
360 DECL_ARTIFICIAL (slot) = 1;
361 DECL_IGNORED_P (slot) = 1;
362 DECL_CONTEXT (slot) = current_function_decl;
363 layout_decl (slot, 0);
364 return slot;
367 /* Set various status flags when building an AGGR_INIT_EXPR object T. */
369 static void
370 process_aggr_init_operands (tree t)
372 bool side_effects;
374 side_effects = TREE_SIDE_EFFECTS (t);
375 if (!side_effects)
377 int i, n;
378 n = TREE_OPERAND_LENGTH (t);
379 for (i = 1; i < n; i++)
381 tree op = TREE_OPERAND (t, i);
382 if (op && TREE_SIDE_EFFECTS (op))
384 side_effects = 1;
385 break;
389 TREE_SIDE_EFFECTS (t) = side_effects;
392 /* Build an AGGR_INIT_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE,
393 FN, and SLOT. NARGS is the number of call arguments which are specified
394 as a tree array ARGS. */
396 static tree
397 build_aggr_init_array (tree return_type, tree fn, tree slot, int nargs,
398 tree *args)
400 tree t;
401 int i;
403 t = build_vl_exp (AGGR_INIT_EXPR, nargs + 3);
404 TREE_TYPE (t) = return_type;
405 AGGR_INIT_EXPR_FN (t) = fn;
406 AGGR_INIT_EXPR_SLOT (t) = slot;
407 for (i = 0; i < nargs; i++)
408 AGGR_INIT_EXPR_ARG (t, i) = args[i];
409 process_aggr_init_operands (t);
410 return t;
413 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
414 target. TYPE is the type to be initialized.
416 Build an AGGR_INIT_EXPR to represent the initialization. This function
417 differs from build_cplus_new in that an AGGR_INIT_EXPR can only be used
418 to initialize another object, whereas a TARGET_EXPR can either
419 initialize another object or create its own temporary object, and as a
420 result building up a TARGET_EXPR requires that the type's destructor be
421 callable. */
423 tree
424 build_aggr_init_expr (tree type, tree init)
426 tree fn;
427 tree slot;
428 tree rval;
429 int is_ctor;
431 /* Don't build AGGR_INIT_EXPR in a template. */
432 if (processing_template_decl)
433 return init;
435 if (TREE_CODE (init) == CALL_EXPR)
436 fn = CALL_EXPR_FN (init);
437 else if (TREE_CODE (init) == AGGR_INIT_EXPR)
438 fn = AGGR_INIT_EXPR_FN (init);
439 else
440 return convert (type, init);
442 is_ctor = (TREE_CODE (fn) == ADDR_EXPR
443 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
444 && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0)));
446 /* We split the CALL_EXPR into its function and its arguments here.
447 Then, in expand_expr, we put them back together. The reason for
448 this is that this expression might be a default argument
449 expression. In that case, we need a new temporary every time the
450 expression is used. That's what break_out_target_exprs does; it
451 replaces every AGGR_INIT_EXPR with a copy that uses a fresh
452 temporary slot. Then, expand_expr builds up a call-expression
453 using the new slot. */
455 /* If we don't need to use a constructor to create an object of this
456 type, don't mess with AGGR_INIT_EXPR. */
457 if (is_ctor || TREE_ADDRESSABLE (type))
459 slot = build_local_temp (type);
461 if (TREE_CODE (init) == CALL_EXPR)
463 rval = build_aggr_init_array (void_type_node, fn, slot,
464 call_expr_nargs (init),
465 CALL_EXPR_ARGP (init));
466 AGGR_INIT_FROM_THUNK_P (rval)
467 = CALL_FROM_THUNK_P (init);
469 else
471 rval = build_aggr_init_array (void_type_node, fn, slot,
472 aggr_init_expr_nargs (init),
473 AGGR_INIT_EXPR_ARGP (init));
474 AGGR_INIT_FROM_THUNK_P (rval)
475 = AGGR_INIT_FROM_THUNK_P (init);
477 TREE_SIDE_EFFECTS (rval) = 1;
478 AGGR_INIT_VIA_CTOR_P (rval) = is_ctor;
479 TREE_NOTHROW (rval) = TREE_NOTHROW (init);
480 CALL_EXPR_LIST_INIT_P (rval) = CALL_EXPR_LIST_INIT_P (init);
482 else
483 rval = init;
485 return rval;
488 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
489 target. TYPE is the type that this initialization should appear to
490 have.
492 Build an encapsulation of the initialization to perform
493 and return it so that it can be processed by language-independent
494 and language-specific expression expanders. */
496 tree
497 build_cplus_new (tree type, tree init, tsubst_flags_t complain)
499 tree rval = build_aggr_init_expr (type, init);
500 tree slot;
502 if (!complete_type_or_maybe_complain (type, init, complain))
503 return error_mark_node;
505 /* Make sure that we're not trying to create an instance of an
506 abstract class. */
507 if (abstract_virtuals_error_sfinae (NULL_TREE, type, complain))
508 return error_mark_node;
510 if (TREE_CODE (rval) == AGGR_INIT_EXPR)
511 slot = AGGR_INIT_EXPR_SLOT (rval);
512 else if (TREE_CODE (rval) == CALL_EXPR
513 || TREE_CODE (rval) == CONSTRUCTOR)
514 slot = build_local_temp (type);
515 else
516 return rval;
518 rval = build_target_expr (slot, rval, complain);
520 if (rval != error_mark_node)
521 TARGET_EXPR_IMPLICIT_P (rval) = 1;
523 return rval;
526 /* Subroutine of build_vec_init_expr: Build up a single element
527 intialization as a proxy for the full array initialization to get things
528 marked as used and any appropriate diagnostics.
530 Since we're deferring building the actual constructor calls until
531 gimplification time, we need to build one now and throw it away so
532 that the relevant constructor gets mark_used before cgraph decides
533 what functions are needed. Here we assume that init is either
534 NULL_TREE, void_type_node (indicating value-initialization), or
535 another array to copy. */
537 static tree
538 build_vec_init_elt (tree type, tree init, tsubst_flags_t complain)
540 tree inner_type = strip_array_types (type);
541 vec<tree, va_gc> *argvec;
543 if (integer_zerop (array_type_nelts_total (type))
544 || !CLASS_TYPE_P (inner_type))
545 /* No interesting initialization to do. */
546 return integer_zero_node;
547 else if (init == void_type_node)
548 return build_value_init (inner_type, complain);
550 gcc_assert (init == NULL_TREE
551 || (same_type_ignoring_top_level_qualifiers_p
552 (type, TREE_TYPE (init))));
554 argvec = make_tree_vector ();
555 if (init)
557 tree init_type = strip_array_types (TREE_TYPE (init));
558 tree dummy = build_dummy_object (init_type);
559 if (!real_lvalue_p (init))
560 dummy = move (dummy);
561 argvec->quick_push (dummy);
563 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
564 &argvec, inner_type, LOOKUP_NORMAL,
565 complain);
566 release_tree_vector (argvec);
568 /* For a trivial constructor, build_over_call creates a TARGET_EXPR. But
569 we don't want one here because we aren't creating a temporary. */
570 if (TREE_CODE (init) == TARGET_EXPR)
571 init = TARGET_EXPR_INITIAL (init);
573 return init;
576 /* Return a TARGET_EXPR which expresses the initialization of an array to
577 be named later, either default-initialization or copy-initialization
578 from another array of the same type. */
580 tree
581 build_vec_init_expr (tree type, tree init, tsubst_flags_t complain)
583 tree slot;
584 bool value_init = false;
585 tree elt_init = build_vec_init_elt (type, init, complain);
587 if (init == void_type_node)
589 value_init = true;
590 init = NULL_TREE;
593 slot = build_local_temp (type);
594 init = build2 (VEC_INIT_EXPR, type, slot, init);
595 TREE_SIDE_EFFECTS (init) = true;
596 SET_EXPR_LOCATION (init, input_location);
598 if (cxx_dialect >= cxx11
599 && potential_constant_expression (elt_init))
600 VEC_INIT_EXPR_IS_CONSTEXPR (init) = true;
601 VEC_INIT_EXPR_VALUE_INIT (init) = value_init;
603 return init;
606 /* Give a helpful diagnostic for a non-constexpr VEC_INIT_EXPR in a context
607 that requires a constant expression. */
609 void
610 diagnose_non_constexpr_vec_init (tree expr)
612 tree type = TREE_TYPE (VEC_INIT_EXPR_SLOT (expr));
613 tree init, elt_init;
614 if (VEC_INIT_EXPR_VALUE_INIT (expr))
615 init = void_type_node;
616 else
617 init = VEC_INIT_EXPR_INIT (expr);
619 elt_init = build_vec_init_elt (type, init, tf_warning_or_error);
620 require_potential_constant_expression (elt_init);
623 tree
624 build_array_copy (tree init)
626 return build_vec_init_expr (TREE_TYPE (init), init, tf_warning_or_error);
629 /* Build a TARGET_EXPR using INIT to initialize a new temporary of the
630 indicated TYPE. */
632 tree
633 build_target_expr_with_type (tree init, tree type, tsubst_flags_t complain)
635 gcc_assert (!VOID_TYPE_P (type));
637 if (TREE_CODE (init) == TARGET_EXPR
638 || init == error_mark_node)
639 return init;
640 else if (CLASS_TYPE_P (type) && type_has_nontrivial_copy_init (type)
641 && !VOID_TYPE_P (TREE_TYPE (init))
642 && TREE_CODE (init) != COND_EXPR
643 && TREE_CODE (init) != CONSTRUCTOR
644 && TREE_CODE (init) != VA_ARG_EXPR)
645 /* We need to build up a copy constructor call. A void initializer
646 means we're being called from bot_manip. COND_EXPR is a special
647 case because we already have copies on the arms and we don't want
648 another one here. A CONSTRUCTOR is aggregate initialization, which
649 is handled separately. A VA_ARG_EXPR is magic creation of an
650 aggregate; there's no additional work to be done. */
651 return force_rvalue (init, complain);
653 return force_target_expr (type, init, complain);
656 /* Like the above function, but without the checking. This function should
657 only be used by code which is deliberately trying to subvert the type
658 system, such as call_builtin_trap. Or build_over_call, to avoid
659 infinite recursion. */
661 tree
662 force_target_expr (tree type, tree init, tsubst_flags_t complain)
664 tree slot;
666 gcc_assert (!VOID_TYPE_P (type));
668 slot = build_local_temp (type);
669 return build_target_expr (slot, init, complain);
672 /* Like build_target_expr_with_type, but use the type of INIT. */
674 tree
675 get_target_expr_sfinae (tree init, tsubst_flags_t complain)
677 if (TREE_CODE (init) == AGGR_INIT_EXPR)
678 return build_target_expr (AGGR_INIT_EXPR_SLOT (init), init, complain);
679 else if (TREE_CODE (init) == VEC_INIT_EXPR)
680 return build_target_expr (VEC_INIT_EXPR_SLOT (init), init, complain);
681 else
682 return build_target_expr_with_type (init, TREE_TYPE (init), complain);
685 tree
686 get_target_expr (tree init)
688 return get_target_expr_sfinae (init, tf_warning_or_error);
691 /* If EXPR is a bitfield reference, convert it to the declared type of
692 the bitfield, and return the resulting expression. Otherwise,
693 return EXPR itself. */
695 tree
696 convert_bitfield_to_declared_type (tree expr)
698 tree bitfield_type;
700 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
701 if (bitfield_type)
702 expr = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type),
703 expr);
704 return expr;
707 /* EXPR is being used in an rvalue context. Return a version of EXPR
708 that is marked as an rvalue. */
710 tree
711 rvalue (tree expr)
713 tree type;
715 if (error_operand_p (expr))
716 return expr;
718 expr = mark_rvalue_use (expr);
720 /* [basic.lval]
722 Non-class rvalues always have cv-unqualified types. */
723 type = TREE_TYPE (expr);
724 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
725 type = cv_unqualified (type);
727 /* We need to do this for rvalue refs as well to get the right answer
728 from decltype; see c++/36628. */
729 if (!processing_template_decl && lvalue_or_rvalue_with_address_p (expr))
730 expr = build1 (NON_LVALUE_EXPR, type, expr);
731 else if (type != TREE_TYPE (expr))
732 expr = build_nop (type, expr);
734 return expr;
738 struct cplus_array_info
740 tree type;
741 tree domain;
744 struct cplus_array_hasher : ggc_ptr_hash<tree_node>
746 typedef cplus_array_info *compare_type;
748 static hashval_t hash (tree t);
749 static bool equal (tree, cplus_array_info *);
752 /* Hash an ARRAY_TYPE. K is really of type `tree'. */
754 hashval_t
755 cplus_array_hasher::hash (tree t)
757 hashval_t hash;
759 hash = TYPE_UID (TREE_TYPE (t));
760 if (TYPE_DOMAIN (t))
761 hash ^= TYPE_UID (TYPE_DOMAIN (t));
762 return hash;
765 /* Compare two ARRAY_TYPEs. K1 is really of type `tree', K2 is really
766 of type `cplus_array_info*'. */
768 bool
769 cplus_array_hasher::equal (tree t1, cplus_array_info *t2)
771 return (TREE_TYPE (t1) == t2->type && TYPE_DOMAIN (t1) == t2->domain);
774 /* Hash table containing dependent array types, which are unsuitable for
775 the language-independent type hash table. */
776 static GTY (()) hash_table<cplus_array_hasher> *cplus_array_htab;
778 /* Build an ARRAY_TYPE without laying it out. */
780 static tree
781 build_min_array_type (tree elt_type, tree index_type)
783 tree t = cxx_make_type (ARRAY_TYPE);
784 TREE_TYPE (t) = elt_type;
785 TYPE_DOMAIN (t) = index_type;
786 return t;
789 /* Set TYPE_CANONICAL like build_array_type_1, but using
790 build_cplus_array_type. */
792 static void
793 set_array_type_canon (tree t, tree elt_type, tree index_type)
795 /* Set the canonical type for this new node. */
796 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
797 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
798 SET_TYPE_STRUCTURAL_EQUALITY (t);
799 else if (TYPE_CANONICAL (elt_type) != elt_type
800 || (index_type && TYPE_CANONICAL (index_type) != index_type))
801 TYPE_CANONICAL (t)
802 = build_cplus_array_type (TYPE_CANONICAL (elt_type),
803 index_type
804 ? TYPE_CANONICAL (index_type) : index_type);
805 else
806 TYPE_CANONICAL (t) = t;
809 /* Like build_array_type, but handle special C++ semantics: an array of a
810 variant element type is a variant of the array of the main variant of
811 the element type. */
813 tree
814 build_cplus_array_type (tree elt_type, tree index_type)
816 tree t;
818 if (elt_type == error_mark_node || index_type == error_mark_node)
819 return error_mark_node;
821 bool dependent = (uses_template_parms (elt_type)
822 || (index_type && uses_template_parms (index_type)));
824 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
825 /* Start with an array of the TYPE_MAIN_VARIANT. */
826 t = build_cplus_array_type (TYPE_MAIN_VARIANT (elt_type),
827 index_type);
828 else if (dependent)
830 /* Since type_hash_canon calls layout_type, we need to use our own
831 hash table. */
832 cplus_array_info cai;
833 hashval_t hash;
835 if (cplus_array_htab == NULL)
836 cplus_array_htab = hash_table<cplus_array_hasher>::create_ggc (61);
838 hash = TYPE_UID (elt_type);
839 if (index_type)
840 hash ^= TYPE_UID (index_type);
841 cai.type = elt_type;
842 cai.domain = index_type;
844 tree *e = cplus_array_htab->find_slot_with_hash (&cai, hash, INSERT);
845 if (*e)
846 /* We have found the type: we're done. */
847 return (tree) *e;
848 else
850 /* Build a new array type. */
851 t = build_min_array_type (elt_type, index_type);
853 /* Store it in the hash table. */
854 *e = t;
856 /* Set the canonical type for this new node. */
857 set_array_type_canon (t, elt_type, index_type);
860 else
862 t = build_array_type (elt_type, index_type);
865 /* Now check whether we already have this array variant. */
866 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
868 tree m = t;
869 for (t = m; t; t = TYPE_NEXT_VARIANT (t))
870 if (TREE_TYPE (t) == elt_type
871 && TYPE_NAME (t) == NULL_TREE
872 && TYPE_ATTRIBUTES (t) == NULL_TREE)
873 break;
874 if (!t)
876 t = build_min_array_type (elt_type, index_type);
877 set_array_type_canon (t, elt_type, index_type);
878 if (!dependent)
880 layout_type (t);
881 /* Make sure sizes are shared with the main variant.
882 layout_type can't be called after setting TYPE_NEXT_VARIANT,
883 as it will overwrite alignment etc. of all variants. */
884 TYPE_SIZE (t) = TYPE_SIZE (m);
885 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (m);
888 TYPE_MAIN_VARIANT (t) = m;
889 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
890 TYPE_NEXT_VARIANT (m) = t;
894 /* Avoid spurious warnings with VLAs (c++/54583). */
895 if (TYPE_SIZE (t) && EXPR_P (TYPE_SIZE (t)))
896 TREE_NO_WARNING (TYPE_SIZE (t)) = 1;
898 /* Push these needs up to the ARRAY_TYPE so that initialization takes
899 place more easily. */
900 bool needs_ctor = (TYPE_NEEDS_CONSTRUCTING (t)
901 = TYPE_NEEDS_CONSTRUCTING (elt_type));
902 bool needs_dtor = (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
903 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type));
905 if (!dependent && t == TYPE_MAIN_VARIANT (t)
906 && !COMPLETE_TYPE_P (t) && COMPLETE_TYPE_P (elt_type))
908 /* The element type has been completed since the last time we saw
909 this array type; update the layout and 'tor flags for any variants
910 that need it. */
911 layout_type (t);
912 for (tree v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
914 TYPE_NEEDS_CONSTRUCTING (v) = needs_ctor;
915 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (v) = needs_dtor;
919 return t;
922 /* Return an ARRAY_TYPE with element type ELT and length N. */
924 tree
925 build_array_of_n_type (tree elt, int n)
927 return build_cplus_array_type (elt, build_index_type (size_int (n - 1)));
930 /* True iff T is an N3639 array of runtime bound (VLA). These were
931 approved for C++14 but then removed. */
933 bool
934 array_of_runtime_bound_p (tree t)
936 if (!t || TREE_CODE (t) != ARRAY_TYPE)
937 return false;
938 tree dom = TYPE_DOMAIN (t);
939 if (!dom)
940 return false;
941 tree max = TYPE_MAX_VALUE (dom);
942 return (!potential_rvalue_constant_expression (max)
943 || (!value_dependent_expression_p (max) && !TREE_CONSTANT (max)));
946 /* Return a reference type node referring to TO_TYPE. If RVAL is
947 true, return an rvalue reference type, otherwise return an lvalue
948 reference type. If a type node exists, reuse it, otherwise create
949 a new one. */
950 tree
951 cp_build_reference_type (tree to_type, bool rval)
953 tree lvalue_ref, t;
954 lvalue_ref = build_reference_type (to_type);
955 if (!rval)
956 return lvalue_ref;
958 /* This code to create rvalue reference types is based on and tied
959 to the code creating lvalue reference types in the middle-end
960 functions build_reference_type_for_mode and build_reference_type.
962 It works by putting the rvalue reference type nodes after the
963 lvalue reference nodes in the TYPE_NEXT_REF_TO linked list, so
964 they will effectively be ignored by the middle end. */
966 for (t = lvalue_ref; (t = TYPE_NEXT_REF_TO (t)); )
967 if (TYPE_REF_IS_RVALUE (t))
968 return t;
970 t = build_distinct_type_copy (lvalue_ref);
972 TYPE_REF_IS_RVALUE (t) = true;
973 TYPE_NEXT_REF_TO (t) = TYPE_NEXT_REF_TO (lvalue_ref);
974 TYPE_NEXT_REF_TO (lvalue_ref) = t;
976 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
977 SET_TYPE_STRUCTURAL_EQUALITY (t);
978 else if (TYPE_CANONICAL (to_type) != to_type)
979 TYPE_CANONICAL (t)
980 = cp_build_reference_type (TYPE_CANONICAL (to_type), rval);
981 else
982 TYPE_CANONICAL (t) = t;
984 layout_type (t);
986 return t;
990 /* Returns EXPR cast to rvalue reference type, like std::move. */
992 tree
993 move (tree expr)
995 tree type = TREE_TYPE (expr);
996 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
997 type = cp_build_reference_type (type, /*rval*/true);
998 return build_static_cast (type, expr, tf_warning_or_error);
1001 /* Used by the C++ front end to build qualified array types. However,
1002 the C version of this function does not properly maintain canonical
1003 types (which are not used in C). */
1004 tree
1005 c_build_qualified_type (tree type, int type_quals, tree /* orig_qual_type */,
1006 size_t /* orig_qual_indirect */)
1008 return cp_build_qualified_type (type, type_quals);
1012 /* Make a variant of TYPE, qualified with the TYPE_QUALS. Handles
1013 arrays correctly. In particular, if TYPE is an array of T's, and
1014 TYPE_QUALS is non-empty, returns an array of qualified T's.
1016 FLAGS determines how to deal with ill-formed qualifications. If
1017 tf_ignore_bad_quals is set, then bad qualifications are dropped
1018 (this is permitted if TYPE was introduced via a typedef or template
1019 type parameter). If bad qualifications are dropped and tf_warning
1020 is set, then a warning is issued for non-const qualifications. If
1021 tf_ignore_bad_quals is not set and tf_error is not set, we
1022 return error_mark_node. Otherwise, we issue an error, and ignore
1023 the qualifications.
1025 Qualification of a reference type is valid when the reference came
1026 via a typedef or template type argument. [dcl.ref] No such
1027 dispensation is provided for qualifying a function type. [dcl.fct]
1028 DR 295 queries this and the proposed resolution brings it into line
1029 with qualifying a reference. We implement the DR. We also behave
1030 in a similar manner for restricting non-pointer types. */
1032 tree
1033 cp_build_qualified_type_real (tree type,
1034 int type_quals,
1035 tsubst_flags_t complain)
1037 tree result;
1038 int bad_quals = TYPE_UNQUALIFIED;
1040 if (type == error_mark_node)
1041 return type;
1043 if (type_quals == cp_type_quals (type))
1044 return type;
1046 if (TREE_CODE (type) == ARRAY_TYPE)
1048 /* In C++, the qualification really applies to the array element
1049 type. Obtain the appropriately qualified element type. */
1050 tree t;
1051 tree element_type
1052 = cp_build_qualified_type_real (TREE_TYPE (type),
1053 type_quals,
1054 complain);
1056 if (element_type == error_mark_node)
1057 return error_mark_node;
1059 /* See if we already have an identically qualified type. Tests
1060 should be equivalent to those in check_qualified_type. */
1061 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1062 if (TREE_TYPE (t) == element_type
1063 && TYPE_NAME (t) == TYPE_NAME (type)
1064 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
1065 && attribute_list_equal (TYPE_ATTRIBUTES (t),
1066 TYPE_ATTRIBUTES (type)))
1067 break;
1069 if (!t)
1071 t = build_cplus_array_type (element_type, TYPE_DOMAIN (type));
1073 /* Keep the typedef name. */
1074 if (TYPE_NAME (t) != TYPE_NAME (type))
1076 t = build_variant_type_copy (t);
1077 TYPE_NAME (t) = TYPE_NAME (type);
1078 TYPE_ALIGN (t) = TYPE_ALIGN (type);
1079 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (type);
1083 /* Even if we already had this variant, we update
1084 TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case
1085 they changed since the variant was originally created.
1087 This seems hokey; if there is some way to use a previous
1088 variant *without* coming through here,
1089 TYPE_NEEDS_CONSTRUCTING will never be updated. */
1090 TYPE_NEEDS_CONSTRUCTING (t)
1091 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
1092 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1093 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
1094 return t;
1096 else if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
1098 tree t = PACK_EXPANSION_PATTERN (type);
1100 t = cp_build_qualified_type_real (t, type_quals, complain);
1101 return make_pack_expansion (t);
1104 /* A reference or method type shall not be cv-qualified.
1105 [dcl.ref], [dcl.fct]. This used to be an error, but as of DR 295
1106 (in CD1) we always ignore extra cv-quals on functions. */
1107 if (type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)
1108 && (TREE_CODE (type) == REFERENCE_TYPE
1109 || TREE_CODE (type) == FUNCTION_TYPE
1110 || TREE_CODE (type) == METHOD_TYPE))
1112 if (TREE_CODE (type) == REFERENCE_TYPE)
1113 bad_quals |= type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1114 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1117 /* But preserve any function-cv-quals on a FUNCTION_TYPE. */
1118 if (TREE_CODE (type) == FUNCTION_TYPE)
1119 type_quals |= type_memfn_quals (type);
1121 /* A restrict-qualified type must be a pointer (or reference)
1122 to object or incomplete type. */
1123 if ((type_quals & TYPE_QUAL_RESTRICT)
1124 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1125 && TREE_CODE (type) != TYPENAME_TYPE
1126 && !POINTER_TYPE_P (type))
1128 bad_quals |= TYPE_QUAL_RESTRICT;
1129 type_quals &= ~TYPE_QUAL_RESTRICT;
1132 if (bad_quals == TYPE_UNQUALIFIED
1133 || (complain & tf_ignore_bad_quals))
1134 /*OK*/;
1135 else if (!(complain & tf_error))
1136 return error_mark_node;
1137 else
1139 tree bad_type = build_qualified_type (ptr_type_node, bad_quals);
1140 error ("%qV qualifiers cannot be applied to %qT",
1141 bad_type, type);
1144 /* Retrieve (or create) the appropriately qualified variant. */
1145 result = build_qualified_type (type, type_quals);
1147 /* Preserve exception specs and ref-qualifier since build_qualified_type
1148 doesn't know about them. */
1149 if (TREE_CODE (result) == FUNCTION_TYPE
1150 || TREE_CODE (result) == METHOD_TYPE)
1152 result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type));
1153 result = build_ref_qualified_type (result, type_memfn_rqual (type));
1156 return result;
1159 /* Return TYPE with const and volatile removed. */
1161 tree
1162 cv_unqualified (tree type)
1164 int quals;
1166 if (type == error_mark_node)
1167 return type;
1169 quals = cp_type_quals (type);
1170 quals &= ~(TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE);
1171 return cp_build_qualified_type (type, quals);
1174 /* Subroutine of strip_typedefs. We want to apply to RESULT the attributes
1175 from ATTRIBS that affect type identity, and no others. If any are not
1176 applied, set *remove_attributes to true. */
1178 static tree
1179 apply_identity_attributes (tree result, tree attribs, bool *remove_attributes)
1181 tree first_ident = NULL_TREE;
1182 tree new_attribs = NULL_TREE;
1183 tree *p = &new_attribs;
1185 if (OVERLOAD_TYPE_P (result))
1187 /* On classes and enums all attributes are ingrained. */
1188 gcc_assert (attribs == TYPE_ATTRIBUTES (result));
1189 return result;
1192 for (tree a = attribs; a; a = TREE_CHAIN (a))
1194 const attribute_spec *as
1195 = lookup_attribute_spec (get_attribute_name (a));
1196 if (as && as->affects_type_identity)
1198 if (!first_ident)
1199 first_ident = a;
1200 else if (first_ident == error_mark_node)
1202 *p = tree_cons (TREE_PURPOSE (a), TREE_VALUE (a), NULL_TREE);
1203 p = &TREE_CHAIN (*p);
1206 else if (first_ident)
1208 for (tree a2 = first_ident; a2; a2 = TREE_CHAIN (a2))
1210 *p = tree_cons (TREE_PURPOSE (a2), TREE_VALUE (a2), NULL_TREE);
1211 p = &TREE_CHAIN (*p);
1213 first_ident = error_mark_node;
1216 if (first_ident != error_mark_node)
1217 new_attribs = first_ident;
1219 if (first_ident == attribs)
1220 /* All attributes affected type identity. */;
1221 else
1222 *remove_attributes = true;
1224 return cp_build_type_attribute_variant (result, new_attribs);
1227 /* Builds a qualified variant of T that is not a typedef variant.
1228 E.g. consider the following declarations:
1229 typedef const int ConstInt;
1230 typedef ConstInt* PtrConstInt;
1231 If T is PtrConstInt, this function returns a type representing
1232 const int*.
1233 In other words, if T is a typedef, the function returns the underlying type.
1234 The cv-qualification and attributes of the type returned match the
1235 input type.
1236 They will always be compatible types.
1237 The returned type is built so that all of its subtypes
1238 recursively have their typedefs stripped as well.
1240 This is different from just returning TYPE_CANONICAL (T)
1241 Because of several reasons:
1242 * If T is a type that needs structural equality
1243 its TYPE_CANONICAL (T) will be NULL.
1244 * TYPE_CANONICAL (T) desn't carry type attributes
1245 and loses template parameter names.
1247 If REMOVE_ATTRIBUTES is non-null, also strip attributes that don't
1248 affect type identity, and set the referent to true if any were
1249 stripped. */
1251 tree
1252 strip_typedefs (tree t, bool *remove_attributes)
1254 tree result = NULL, type = NULL, t0 = NULL;
1256 if (!t || t == error_mark_node)
1257 return t;
1259 if (TREE_CODE (t) == TREE_LIST)
1261 bool changed = false;
1262 vec<tree,va_gc> *vec = make_tree_vector ();
1263 tree r = t;
1264 for (; t; t = TREE_CHAIN (t))
1266 gcc_assert (!TREE_PURPOSE (t));
1267 tree elt = strip_typedefs (TREE_VALUE (t), remove_attributes);
1268 if (elt != TREE_VALUE (t))
1269 changed = true;
1270 vec_safe_push (vec, elt);
1272 if (changed)
1273 r = build_tree_list_vec (vec);
1274 release_tree_vector (vec);
1275 return r;
1278 gcc_assert (TYPE_P (t));
1280 if (t == TYPE_CANONICAL (t))
1281 return t;
1283 if (dependent_alias_template_spec_p (t))
1284 /* DR 1558: However, if the template-id is dependent, subsequent
1285 template argument substitution still applies to the template-id. */
1286 return t;
1288 switch (TREE_CODE (t))
1290 case POINTER_TYPE:
1291 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1292 result = build_pointer_type (type);
1293 break;
1294 case REFERENCE_TYPE:
1295 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1296 result = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
1297 break;
1298 case OFFSET_TYPE:
1299 t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t), remove_attributes);
1300 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1301 result = build_offset_type (t0, type);
1302 break;
1303 case RECORD_TYPE:
1304 if (TYPE_PTRMEMFUNC_P (t))
1306 t0 = strip_typedefs (TYPE_PTRMEMFUNC_FN_TYPE (t), remove_attributes);
1307 result = build_ptrmemfunc_type (t0);
1309 break;
1310 case ARRAY_TYPE:
1311 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1312 t0 = strip_typedefs (TYPE_DOMAIN (t), remove_attributes);
1313 result = build_cplus_array_type (type, t0);
1314 break;
1315 case FUNCTION_TYPE:
1316 case METHOD_TYPE:
1318 tree arg_types = NULL, arg_node, arg_node2, arg_type;
1319 bool changed;
1321 /* Because we stomp on TREE_PURPOSE of TYPE_ARG_TYPES in many places
1322 around the compiler (e.g. cp_parser_late_parsing_default_args), we
1323 can't expect that re-hashing a function type will find a previous
1324 equivalent type, so try to reuse the input type if nothing has
1325 changed. If the type is itself a variant, that will change. */
1326 bool is_variant = typedef_variant_p (t);
1327 if (remove_attributes
1328 && (TYPE_ATTRIBUTES (t) || TYPE_USER_ALIGN (t)))
1329 is_variant = true;
1331 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1332 changed = type != TREE_TYPE (t) || is_variant;
1334 for (arg_node = TYPE_ARG_TYPES (t);
1335 arg_node;
1336 arg_node = TREE_CHAIN (arg_node))
1338 if (arg_node == void_list_node)
1339 break;
1340 arg_type = strip_typedefs (TREE_VALUE (arg_node),
1341 remove_attributes);
1342 gcc_assert (arg_type);
1343 if (arg_type == TREE_VALUE (arg_node) && !changed)
1344 continue;
1346 if (!changed)
1348 changed = true;
1349 for (arg_node2 = TYPE_ARG_TYPES (t);
1350 arg_node2 != arg_node;
1351 arg_node2 = TREE_CHAIN (arg_node2))
1352 arg_types
1353 = tree_cons (TREE_PURPOSE (arg_node2),
1354 TREE_VALUE (arg_node2), arg_types);
1357 arg_types
1358 = tree_cons (TREE_PURPOSE (arg_node), arg_type, arg_types);
1361 if (!changed)
1362 return t;
1364 if (arg_types)
1365 arg_types = nreverse (arg_types);
1367 /* A list of parameters not ending with an ellipsis
1368 must end with void_list_node. */
1369 if (arg_node)
1370 arg_types = chainon (arg_types, void_list_node);
1372 if (TREE_CODE (t) == METHOD_TYPE)
1374 tree class_type = TREE_TYPE (TREE_VALUE (arg_types));
1375 gcc_assert (class_type);
1376 result =
1377 build_method_type_directly (class_type, type,
1378 TREE_CHAIN (arg_types));
1379 result
1380 = build_ref_qualified_type (result, type_memfn_rqual (t));
1382 else
1384 result = build_function_type (type,
1385 arg_types);
1386 result = apply_memfn_quals (result,
1387 type_memfn_quals (t),
1388 type_memfn_rqual (t));
1391 if (TYPE_RAISES_EXCEPTIONS (t))
1392 result = build_exception_variant (result,
1393 TYPE_RAISES_EXCEPTIONS (t));
1394 if (TYPE_HAS_LATE_RETURN_TYPE (t))
1395 TYPE_HAS_LATE_RETURN_TYPE (result) = 1;
1397 break;
1398 case TYPENAME_TYPE:
1400 tree fullname = TYPENAME_TYPE_FULLNAME (t);
1401 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
1402 && TREE_OPERAND (fullname, 1))
1404 tree args = TREE_OPERAND (fullname, 1);
1405 tree new_args = copy_node (args);
1406 bool changed = false;
1407 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
1409 tree arg = TREE_VEC_ELT (args, i);
1410 tree strip_arg;
1411 if (TYPE_P (arg))
1412 strip_arg = strip_typedefs (arg, remove_attributes);
1413 else
1414 strip_arg = strip_typedefs_expr (arg, remove_attributes);
1415 TREE_VEC_ELT (new_args, i) = strip_arg;
1416 if (strip_arg != arg)
1417 changed = true;
1419 if (changed)
1421 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_args)
1422 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
1423 fullname
1424 = lookup_template_function (TREE_OPERAND (fullname, 0),
1425 new_args);
1427 else
1428 ggc_free (new_args);
1430 result = make_typename_type (strip_typedefs (TYPE_CONTEXT (t),
1431 remove_attributes),
1432 fullname, typename_type, tf_none);
1433 /* Handle 'typedef typename A::N N;' */
1434 if (typedef_variant_p (result))
1435 result = TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (TYPE_NAME (result)));
1437 break;
1438 case DECLTYPE_TYPE:
1439 result = strip_typedefs_expr (DECLTYPE_TYPE_EXPR (t),
1440 remove_attributes);
1441 if (result == DECLTYPE_TYPE_EXPR (t))
1442 result = NULL_TREE;
1443 else
1444 result = (finish_decltype_type
1445 (result,
1446 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t),
1447 tf_none));
1448 break;
1449 default:
1450 break;
1453 if (!result)
1455 if (typedef_variant_p (t))
1457 /* Explicitly get the underlying type, as TYPE_MAIN_VARIANT doesn't
1458 strip typedefs with attributes. */
1459 result = TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (TYPE_NAME (t)));
1460 result = strip_typedefs (result);
1462 else
1463 result = TYPE_MAIN_VARIANT (t);
1465 gcc_assert (!typedef_variant_p (result));
1466 if (TYPE_USER_ALIGN (t) != TYPE_USER_ALIGN (result)
1467 || TYPE_ALIGN (t) != TYPE_ALIGN (result))
1469 gcc_assert (TYPE_USER_ALIGN (t));
1470 if (remove_attributes)
1471 *remove_attributes = true;
1472 else
1474 if (TYPE_ALIGN (t) == TYPE_ALIGN (result))
1475 result = build_variant_type_copy (result);
1476 else
1477 result = build_aligned_type (result, TYPE_ALIGN (t));
1478 TYPE_USER_ALIGN (result) = true;
1481 if (TYPE_ATTRIBUTES (t))
1483 if (remove_attributes)
1484 result = apply_identity_attributes (result, TYPE_ATTRIBUTES (t),
1485 remove_attributes);
1486 else
1487 result = cp_build_type_attribute_variant (result, TYPE_ATTRIBUTES (t));
1489 return cp_build_qualified_type (result, cp_type_quals (t));
1492 /* Like strip_typedefs above, but works on expressions, so that in
1494 template<class T> struct A
1496 typedef T TT;
1497 B<sizeof(TT)> b;
1500 sizeof(TT) is replaced by sizeof(T). */
1502 tree
1503 strip_typedefs_expr (tree t, bool *remove_attributes)
1505 unsigned i,n;
1506 tree r, type, *ops;
1507 enum tree_code code;
1509 if (t == NULL_TREE || t == error_mark_node)
1510 return t;
1512 if (DECL_P (t) || CONSTANT_CLASS_P (t))
1513 return t;
1515 /* Some expressions have type operands, so let's handle types here rather
1516 than check TYPE_P in multiple places below. */
1517 if (TYPE_P (t))
1518 return strip_typedefs (t, remove_attributes);
1520 code = TREE_CODE (t);
1521 switch (code)
1523 case IDENTIFIER_NODE:
1524 case TEMPLATE_PARM_INDEX:
1525 case OVERLOAD:
1526 case BASELINK:
1527 case ARGUMENT_PACK_SELECT:
1528 return t;
1530 case TRAIT_EXPR:
1532 tree type1 = strip_typedefs (TRAIT_EXPR_TYPE1 (t), remove_attributes);
1533 tree type2 = strip_typedefs (TRAIT_EXPR_TYPE2 (t), remove_attributes);
1534 if (type1 == TRAIT_EXPR_TYPE1 (t)
1535 && type2 == TRAIT_EXPR_TYPE2 (t))
1536 return t;
1537 r = copy_node (t);
1538 TRAIT_EXPR_TYPE1 (r) = type1;
1539 TRAIT_EXPR_TYPE2 (r) = type2;
1540 return r;
1543 case TREE_LIST:
1545 vec<tree, va_gc> *vec = make_tree_vector ();
1546 bool changed = false;
1547 tree it;
1548 for (it = t; it; it = TREE_CHAIN (it))
1550 tree val = strip_typedefs_expr (TREE_VALUE (t), remove_attributes);
1551 vec_safe_push (vec, val);
1552 if (val != TREE_VALUE (t))
1553 changed = true;
1554 gcc_assert (TREE_PURPOSE (it) == NULL_TREE);
1556 if (changed)
1558 r = NULL_TREE;
1559 FOR_EACH_VEC_ELT_REVERSE (*vec, i, it)
1560 r = tree_cons (NULL_TREE, it, r);
1562 else
1563 r = t;
1564 release_tree_vector (vec);
1565 return r;
1568 case TREE_VEC:
1570 bool changed = false;
1571 vec<tree, va_gc> *vec = make_tree_vector ();
1572 n = TREE_VEC_LENGTH (t);
1573 vec_safe_reserve (vec, n);
1574 for (i = 0; i < n; ++i)
1576 tree op = strip_typedefs_expr (TREE_VEC_ELT (t, i),
1577 remove_attributes);
1578 vec->quick_push (op);
1579 if (op != TREE_VEC_ELT (t, i))
1580 changed = true;
1582 if (changed)
1584 r = copy_node (t);
1585 for (i = 0; i < n; ++i)
1586 TREE_VEC_ELT (r, i) = (*vec)[i];
1587 NON_DEFAULT_TEMPLATE_ARGS_COUNT (r)
1588 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
1590 else
1591 r = t;
1592 release_tree_vector (vec);
1593 return r;
1596 case CONSTRUCTOR:
1598 bool changed = false;
1599 vec<constructor_elt, va_gc> *vec
1600 = vec_safe_copy (CONSTRUCTOR_ELTS (t));
1601 n = CONSTRUCTOR_NELTS (t);
1602 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1603 for (i = 0; i < n; ++i)
1605 constructor_elt *e = &(*vec)[i];
1606 tree op = strip_typedefs_expr (e->value, remove_attributes);
1607 if (op != e->value)
1609 changed = true;
1610 e->value = op;
1612 gcc_checking_assert
1613 (e->index == strip_typedefs_expr (e->index, remove_attributes));
1616 if (!changed && type == TREE_TYPE (t))
1618 vec_free (vec);
1619 return t;
1621 else
1623 r = copy_node (t);
1624 TREE_TYPE (r) = type;
1625 CONSTRUCTOR_ELTS (r) = vec;
1626 return r;
1630 case LAMBDA_EXPR:
1631 error ("lambda-expression in a constant expression");
1632 return error_mark_node;
1634 default:
1635 break;
1638 gcc_assert (EXPR_P (t));
1640 n = TREE_OPERAND_LENGTH (t);
1641 ops = XALLOCAVEC (tree, n);
1642 type = TREE_TYPE (t);
1644 switch (code)
1646 CASE_CONVERT:
1647 case IMPLICIT_CONV_EXPR:
1648 case DYNAMIC_CAST_EXPR:
1649 case STATIC_CAST_EXPR:
1650 case CONST_CAST_EXPR:
1651 case REINTERPRET_CAST_EXPR:
1652 case CAST_EXPR:
1653 case NEW_EXPR:
1654 type = strip_typedefs (type, remove_attributes);
1655 /* fallthrough */
1657 default:
1658 for (i = 0; i < n; ++i)
1659 ops[i] = strip_typedefs_expr (TREE_OPERAND (t, i), remove_attributes);
1660 break;
1663 /* If nothing changed, return t. */
1664 for (i = 0; i < n; ++i)
1665 if (ops[i] != TREE_OPERAND (t, i))
1666 break;
1667 if (i == n && type == TREE_TYPE (t))
1668 return t;
1670 r = copy_node (t);
1671 TREE_TYPE (r) = type;
1672 for (i = 0; i < n; ++i)
1673 TREE_OPERAND (r, i) = ops[i];
1674 return r;
1677 /* Makes a copy of BINFO and TYPE, which is to be inherited into a
1678 graph dominated by T. If BINFO is NULL, TYPE is a dependent base,
1679 and we do a shallow copy. If BINFO is non-NULL, we do a deep copy.
1680 VIRT indicates whether TYPE is inherited virtually or not.
1681 IGO_PREV points at the previous binfo of the inheritance graph
1682 order chain. The newly copied binfo's TREE_CHAIN forms this
1683 ordering.
1685 The CLASSTYPE_VBASECLASSES vector of T is constructed in the
1686 correct order. That is in the order the bases themselves should be
1687 constructed in.
1689 The BINFO_INHERITANCE of a virtual base class points to the binfo
1690 of the most derived type. ??? We could probably change this so that
1691 BINFO_INHERITANCE becomes synonymous with BINFO_PRIMARY, and hence
1692 remove a field. They currently can only differ for primary virtual
1693 virtual bases. */
1695 tree
1696 copy_binfo (tree binfo, tree type, tree t, tree *igo_prev, int virt)
1698 tree new_binfo;
1700 if (virt)
1702 /* See if we've already made this virtual base. */
1703 new_binfo = binfo_for_vbase (type, t);
1704 if (new_binfo)
1705 return new_binfo;
1708 new_binfo = make_tree_binfo (binfo ? BINFO_N_BASE_BINFOS (binfo) : 0);
1709 BINFO_TYPE (new_binfo) = type;
1711 /* Chain it into the inheritance graph. */
1712 TREE_CHAIN (*igo_prev) = new_binfo;
1713 *igo_prev = new_binfo;
1715 if (binfo && !BINFO_DEPENDENT_BASE_P (binfo))
1717 int ix;
1718 tree base_binfo;
1720 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), type));
1722 BINFO_OFFSET (new_binfo) = BINFO_OFFSET (binfo);
1723 BINFO_VIRTUALS (new_binfo) = BINFO_VIRTUALS (binfo);
1725 /* We do not need to copy the accesses, as they are read only. */
1726 BINFO_BASE_ACCESSES (new_binfo) = BINFO_BASE_ACCESSES (binfo);
1728 /* Recursively copy base binfos of BINFO. */
1729 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1731 tree new_base_binfo;
1732 new_base_binfo = copy_binfo (base_binfo, BINFO_TYPE (base_binfo),
1733 t, igo_prev,
1734 BINFO_VIRTUAL_P (base_binfo));
1736 if (!BINFO_INHERITANCE_CHAIN (new_base_binfo))
1737 BINFO_INHERITANCE_CHAIN (new_base_binfo) = new_binfo;
1738 BINFO_BASE_APPEND (new_binfo, new_base_binfo);
1741 else
1742 BINFO_DEPENDENT_BASE_P (new_binfo) = 1;
1744 if (virt)
1746 /* Push it onto the list after any virtual bases it contains
1747 will have been pushed. */
1748 CLASSTYPE_VBASECLASSES (t)->quick_push (new_binfo);
1749 BINFO_VIRTUAL_P (new_binfo) = 1;
1750 BINFO_INHERITANCE_CHAIN (new_binfo) = TYPE_BINFO (t);
1753 return new_binfo;
1756 /* Hashing of lists so that we don't make duplicates.
1757 The entry point is `list_hash_canon'. */
1759 struct list_proxy
1761 tree purpose;
1762 tree value;
1763 tree chain;
1766 struct list_hasher : ggc_ptr_hash<tree_node>
1768 typedef list_proxy *compare_type;
1770 static hashval_t hash (tree);
1771 static bool equal (tree, list_proxy *);
1774 /* Now here is the hash table. When recording a list, it is added
1775 to the slot whose index is the hash code mod the table size.
1776 Note that the hash table is used for several kinds of lists.
1777 While all these live in the same table, they are completely independent,
1778 and the hash code is computed differently for each of these. */
1780 static GTY (()) hash_table<list_hasher> *list_hash_table;
1782 /* Compare ENTRY (an entry in the hash table) with DATA (a list_proxy
1783 for a node we are thinking about adding). */
1785 bool
1786 list_hasher::equal (tree t, list_proxy *proxy)
1788 return (TREE_VALUE (t) == proxy->value
1789 && TREE_PURPOSE (t) == proxy->purpose
1790 && TREE_CHAIN (t) == proxy->chain);
1793 /* Compute a hash code for a list (chain of TREE_LIST nodes
1794 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
1795 TREE_COMMON slots), by adding the hash codes of the individual entries. */
1797 static hashval_t
1798 list_hash_pieces (tree purpose, tree value, tree chain)
1800 hashval_t hashcode = 0;
1802 if (chain)
1803 hashcode += TREE_HASH (chain);
1805 if (value)
1806 hashcode += TREE_HASH (value);
1807 else
1808 hashcode += 1007;
1809 if (purpose)
1810 hashcode += TREE_HASH (purpose);
1811 else
1812 hashcode += 1009;
1813 return hashcode;
1816 /* Hash an already existing TREE_LIST. */
1818 hashval_t
1819 list_hasher::hash (tree t)
1821 return list_hash_pieces (TREE_PURPOSE (t),
1822 TREE_VALUE (t),
1823 TREE_CHAIN (t));
1826 /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
1827 object for an identical list if one already exists. Otherwise, build a
1828 new one, and record it as the canonical object. */
1830 tree
1831 hash_tree_cons (tree purpose, tree value, tree chain)
1833 int hashcode = 0;
1834 tree *slot;
1835 struct list_proxy proxy;
1837 /* Hash the list node. */
1838 hashcode = list_hash_pieces (purpose, value, chain);
1839 /* Create a proxy for the TREE_LIST we would like to create. We
1840 don't actually create it so as to avoid creating garbage. */
1841 proxy.purpose = purpose;
1842 proxy.value = value;
1843 proxy.chain = chain;
1844 /* See if it is already in the table. */
1845 slot = list_hash_table->find_slot_with_hash (&proxy, hashcode, INSERT);
1846 /* If not, create a new node. */
1847 if (!*slot)
1848 *slot = tree_cons (purpose, value, chain);
1849 return (tree) *slot;
1852 /* Constructor for hashed lists. */
1854 tree
1855 hash_tree_chain (tree value, tree chain)
1857 return hash_tree_cons (NULL_TREE, value, chain);
1860 void
1861 debug_binfo (tree elem)
1863 HOST_WIDE_INT n;
1864 tree virtuals;
1866 fprintf (stderr, "type \"%s\", offset = " HOST_WIDE_INT_PRINT_DEC
1867 "\nvtable type:\n",
1868 TYPE_NAME_STRING (BINFO_TYPE (elem)),
1869 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
1870 debug_tree (BINFO_TYPE (elem));
1871 if (BINFO_VTABLE (elem))
1872 fprintf (stderr, "vtable decl \"%s\"\n",
1873 IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem))));
1874 else
1875 fprintf (stderr, "no vtable decl yet\n");
1876 fprintf (stderr, "virtuals:\n");
1877 virtuals = BINFO_VIRTUALS (elem);
1878 n = 0;
1880 while (virtuals)
1882 tree fndecl = TREE_VALUE (virtuals);
1883 fprintf (stderr, "%s [%ld =? %ld]\n",
1884 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
1885 (long) n, (long) TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
1886 ++n;
1887 virtuals = TREE_CHAIN (virtuals);
1891 /* Build a representation for the qualified name SCOPE::NAME. TYPE is
1892 the type of the result expression, if known, or NULL_TREE if the
1893 resulting expression is type-dependent. If TEMPLATE_P is true,
1894 NAME is known to be a template because the user explicitly used the
1895 "template" keyword after the "::".
1897 All SCOPE_REFs should be built by use of this function. */
1899 tree
1900 build_qualified_name (tree type, tree scope, tree name, bool template_p)
1902 tree t;
1903 if (type == error_mark_node
1904 || scope == error_mark_node
1905 || name == error_mark_node)
1906 return error_mark_node;
1907 t = build2 (SCOPE_REF, type, scope, name);
1908 QUALIFIED_NAME_IS_TEMPLATE (t) = template_p;
1909 PTRMEM_OK_P (t) = true;
1910 if (type)
1911 t = convert_from_reference (t);
1912 return t;
1915 /* Like check_qualified_type, but also check ref-qualifier and exception
1916 specification. */
1918 static bool
1919 cp_check_qualified_type (const_tree cand, const_tree base, int type_quals,
1920 cp_ref_qualifier rqual, tree raises)
1922 return (check_qualified_type (cand, base, type_quals)
1923 && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (cand),
1924 ce_exact)
1925 && type_memfn_rqual (cand) == rqual);
1928 /* Build the FUNCTION_TYPE or METHOD_TYPE with the ref-qualifier RQUAL. */
1930 tree
1931 build_ref_qualified_type (tree type, cp_ref_qualifier rqual)
1933 tree t;
1935 if (rqual == type_memfn_rqual (type))
1936 return type;
1938 int type_quals = TYPE_QUALS (type);
1939 tree raises = TYPE_RAISES_EXCEPTIONS (type);
1940 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1941 if (cp_check_qualified_type (t, type, type_quals, rqual, raises))
1942 return t;
1944 t = build_variant_type_copy (type);
1945 switch (rqual)
1947 case REF_QUAL_RVALUE:
1948 FUNCTION_RVALUE_QUALIFIED (t) = 1;
1949 FUNCTION_REF_QUALIFIED (t) = 1;
1950 break;
1951 case REF_QUAL_LVALUE:
1952 FUNCTION_RVALUE_QUALIFIED (t) = 0;
1953 FUNCTION_REF_QUALIFIED (t) = 1;
1954 break;
1955 default:
1956 FUNCTION_REF_QUALIFIED (t) = 0;
1957 break;
1960 if (TYPE_STRUCTURAL_EQUALITY_P (type))
1961 /* Propagate structural equality. */
1962 SET_TYPE_STRUCTURAL_EQUALITY (t);
1963 else if (TYPE_CANONICAL (type) != type)
1964 /* Build the underlying canonical type, since it is different
1965 from TYPE. */
1966 TYPE_CANONICAL (t) = build_ref_qualified_type (TYPE_CANONICAL (type),
1967 rqual);
1968 else
1969 /* T is its own canonical type. */
1970 TYPE_CANONICAL (t) = t;
1972 return t;
1975 /* Returns nonzero if X is an expression for a (possibly overloaded)
1976 function. If "f" is a function or function template, "f", "c->f",
1977 "c.f", "C::f", and "f<int>" will all be considered possibly
1978 overloaded functions. Returns 2 if the function is actually
1979 overloaded, i.e., if it is impossible to know the type of the
1980 function without performing overload resolution. */
1983 is_overloaded_fn (tree x)
1985 /* A baselink is also considered an overloaded function. */
1986 if (TREE_CODE (x) == OFFSET_REF
1987 || TREE_CODE (x) == COMPONENT_REF)
1988 x = TREE_OPERAND (x, 1);
1989 if (BASELINK_P (x))
1990 x = BASELINK_FUNCTIONS (x);
1991 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
1992 x = TREE_OPERAND (x, 0);
1993 if (DECL_FUNCTION_TEMPLATE_P (OVL_CURRENT (x))
1994 || (TREE_CODE (x) == OVERLOAD && OVL_CHAIN (x)))
1995 return 2;
1996 return (TREE_CODE (x) == FUNCTION_DECL
1997 || TREE_CODE (x) == OVERLOAD);
2000 /* X is the CALL_EXPR_FN of a CALL_EXPR. If X represents a dependent name
2001 (14.6.2), return the IDENTIFIER_NODE for that name. Otherwise, return
2002 NULL_TREE. */
2004 tree
2005 dependent_name (tree x)
2007 if (identifier_p (x))
2008 return x;
2009 if (TREE_CODE (x) != COMPONENT_REF
2010 && TREE_CODE (x) != OFFSET_REF
2011 && TREE_CODE (x) != BASELINK
2012 && is_overloaded_fn (x))
2013 return DECL_NAME (get_first_fn (x));
2014 return NULL_TREE;
2017 /* Returns true iff X is an expression for an overloaded function
2018 whose type cannot be known without performing overload
2019 resolution. */
2021 bool
2022 really_overloaded_fn (tree x)
2024 return is_overloaded_fn (x) == 2;
2027 tree
2028 get_fns (tree from)
2030 gcc_assert (is_overloaded_fn (from));
2031 /* A baselink is also considered an overloaded function. */
2032 if (TREE_CODE (from) == OFFSET_REF
2033 || TREE_CODE (from) == COMPONENT_REF)
2034 from = TREE_OPERAND (from, 1);
2035 if (BASELINK_P (from))
2036 from = BASELINK_FUNCTIONS (from);
2037 if (TREE_CODE (from) == TEMPLATE_ID_EXPR)
2038 from = TREE_OPERAND (from, 0);
2039 return from;
2042 tree
2043 get_first_fn (tree from)
2045 return OVL_CURRENT (get_fns (from));
2048 /* Return a new OVL node, concatenating it with the old one. */
2050 tree
2051 ovl_cons (tree decl, tree chain)
2053 tree result = make_node (OVERLOAD);
2054 TREE_TYPE (result) = unknown_type_node;
2055 OVL_FUNCTION (result) = decl;
2056 TREE_CHAIN (result) = chain;
2058 return result;
2061 /* Build a new overloaded function. If this is the first one,
2062 just return it; otherwise, ovl_cons the _DECLs */
2064 tree
2065 build_overload (tree decl, tree chain)
2067 if (! chain && TREE_CODE (decl) != TEMPLATE_DECL)
2068 return decl;
2069 return ovl_cons (decl, chain);
2072 /* Return the scope where the overloaded functions OVL were found. */
2074 tree
2075 ovl_scope (tree ovl)
2077 if (TREE_CODE (ovl) == OFFSET_REF
2078 || TREE_CODE (ovl) == COMPONENT_REF)
2079 ovl = TREE_OPERAND (ovl, 1);
2080 if (TREE_CODE (ovl) == BASELINK)
2081 return BINFO_TYPE (BASELINK_BINFO (ovl));
2082 if (TREE_CODE (ovl) == TEMPLATE_ID_EXPR)
2083 ovl = TREE_OPERAND (ovl, 0);
2084 /* Skip using-declarations. */
2085 while (TREE_CODE (ovl) == OVERLOAD && OVL_USED (ovl) && OVL_CHAIN (ovl))
2086 ovl = OVL_CHAIN (ovl);
2087 return CP_DECL_CONTEXT (OVL_CURRENT (ovl));
2090 /* Return TRUE if FN is a non-static member function, FALSE otherwise.
2091 This function looks into BASELINK and OVERLOAD nodes. */
2093 bool
2094 non_static_member_function_p (tree fn)
2096 if (fn == NULL_TREE)
2097 return false;
2099 if (is_overloaded_fn (fn))
2100 fn = get_first_fn (fn);
2102 return (DECL_P (fn)
2103 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn));
2107 #define PRINT_RING_SIZE 4
2109 static const char *
2110 cxx_printable_name_internal (tree decl, int v, bool translate)
2112 static unsigned int uid_ring[PRINT_RING_SIZE];
2113 static char *print_ring[PRINT_RING_SIZE];
2114 static bool trans_ring[PRINT_RING_SIZE];
2115 static int ring_counter;
2116 int i;
2118 /* Only cache functions. */
2119 if (v < 2
2120 || TREE_CODE (decl) != FUNCTION_DECL
2121 || DECL_LANG_SPECIFIC (decl) == 0)
2122 return lang_decl_name (decl, v, translate);
2124 /* See if this print name is lying around. */
2125 for (i = 0; i < PRINT_RING_SIZE; i++)
2126 if (uid_ring[i] == DECL_UID (decl) && translate == trans_ring[i])
2127 /* yes, so return it. */
2128 return print_ring[i];
2130 if (++ring_counter == PRINT_RING_SIZE)
2131 ring_counter = 0;
2133 if (current_function_decl != NULL_TREE)
2135 /* There may be both translated and untranslated versions of the
2136 name cached. */
2137 for (i = 0; i < 2; i++)
2139 if (uid_ring[ring_counter] == DECL_UID (current_function_decl))
2140 ring_counter += 1;
2141 if (ring_counter == PRINT_RING_SIZE)
2142 ring_counter = 0;
2144 gcc_assert (uid_ring[ring_counter] != DECL_UID (current_function_decl));
2147 free (print_ring[ring_counter]);
2149 print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v, translate));
2150 uid_ring[ring_counter] = DECL_UID (decl);
2151 trans_ring[ring_counter] = translate;
2152 return print_ring[ring_counter];
2155 const char *
2156 cxx_printable_name (tree decl, int v)
2158 return cxx_printable_name_internal (decl, v, false);
2161 const char *
2162 cxx_printable_name_translate (tree decl, int v)
2164 return cxx_printable_name_internal (decl, v, true);
2167 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
2168 listed in RAISES. */
2170 tree
2171 build_exception_variant (tree type, tree raises)
2173 tree v;
2174 int type_quals;
2176 if (comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (type), ce_exact))
2177 return type;
2179 type_quals = TYPE_QUALS (type);
2180 cp_ref_qualifier rqual = type_memfn_rqual (type);
2181 for (v = TYPE_MAIN_VARIANT (type); v; v = TYPE_NEXT_VARIANT (v))
2182 if (cp_check_qualified_type (v, type, type_quals, rqual, raises))
2183 return v;
2185 /* Need to build a new variant. */
2186 v = build_variant_type_copy (type);
2187 TYPE_RAISES_EXCEPTIONS (v) = raises;
2188 return v;
2191 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new
2192 BOUND_TEMPLATE_TEMPLATE_PARM bound with NEWARGS as its template
2193 arguments. */
2195 tree
2196 bind_template_template_parm (tree t, tree newargs)
2198 tree decl = TYPE_NAME (t);
2199 tree t2;
2201 t2 = cxx_make_type (BOUND_TEMPLATE_TEMPLATE_PARM);
2202 decl = build_decl (input_location,
2203 TYPE_DECL, DECL_NAME (decl), NULL_TREE);
2205 /* These nodes have to be created to reflect new TYPE_DECL and template
2206 arguments. */
2207 TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t));
2208 TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl;
2209 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
2210 = build_template_info (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t), newargs);
2212 TREE_TYPE (decl) = t2;
2213 TYPE_NAME (t2) = decl;
2214 TYPE_STUB_DECL (t2) = decl;
2215 TYPE_SIZE (t2) = 0;
2216 SET_TYPE_STRUCTURAL_EQUALITY (t2);
2218 return t2;
2221 /* Called from count_trees via walk_tree. */
2223 static tree
2224 count_trees_r (tree *tp, int *walk_subtrees, void *data)
2226 ++*((int *) data);
2228 if (TYPE_P (*tp))
2229 *walk_subtrees = 0;
2231 return NULL_TREE;
2234 /* Debugging function for measuring the rough complexity of a tree
2235 representation. */
2238 count_trees (tree t)
2240 int n_trees = 0;
2241 cp_walk_tree_without_duplicates (&t, count_trees_r, &n_trees);
2242 return n_trees;
2245 /* Called from verify_stmt_tree via walk_tree. */
2247 static tree
2248 verify_stmt_tree_r (tree* tp, int * /*walk_subtrees*/, void* data)
2250 tree t = *tp;
2251 hash_table<nofree_ptr_hash <tree_node> > *statements
2252 = static_cast <hash_table<nofree_ptr_hash <tree_node> > *> (data);
2253 tree_node **slot;
2255 if (!STATEMENT_CODE_P (TREE_CODE (t)))
2256 return NULL_TREE;
2258 /* If this statement is already present in the hash table, then
2259 there is a circularity in the statement tree. */
2260 gcc_assert (!statements->find (t));
2262 slot = statements->find_slot (t, INSERT);
2263 *slot = t;
2265 return NULL_TREE;
2268 /* Debugging function to check that the statement T has not been
2269 corrupted. For now, this function simply checks that T contains no
2270 circularities. */
2272 void
2273 verify_stmt_tree (tree t)
2275 hash_table<nofree_ptr_hash <tree_node> > statements (37);
2276 cp_walk_tree (&t, verify_stmt_tree_r, &statements, NULL);
2279 /* Check if the type T depends on a type with no linkage and if so, return
2280 it. If RELAXED_P then do not consider a class type declared within
2281 a vague-linkage function to have no linkage. */
2283 tree
2284 no_linkage_check (tree t, bool relaxed_p)
2286 tree r;
2288 /* There's no point in checking linkage on template functions; we
2289 can't know their complete types. */
2290 if (processing_template_decl)
2291 return NULL_TREE;
2293 switch (TREE_CODE (t))
2295 case RECORD_TYPE:
2296 if (TYPE_PTRMEMFUNC_P (t))
2297 goto ptrmem;
2298 /* Lambda types that don't have mangling scope have no linkage. We
2299 check CLASSTYPE_LAMBDA_EXPR for error_mark_node because
2300 when we get here from pushtag none of the lambda information is
2301 set up yet, so we want to assume that the lambda has linkage and
2302 fix it up later if not. */
2303 if (CLASSTYPE_LAMBDA_EXPR (t)
2304 && CLASSTYPE_LAMBDA_EXPR (t) != error_mark_node
2305 && LAMBDA_TYPE_EXTRA_SCOPE (t) == NULL_TREE)
2306 return t;
2307 /* Fall through. */
2308 case UNION_TYPE:
2309 if (!CLASS_TYPE_P (t))
2310 return NULL_TREE;
2311 /* Fall through. */
2312 case ENUMERAL_TYPE:
2313 /* Only treat anonymous types as having no linkage if they're at
2314 namespace scope. This is core issue 966. */
2315 if (TYPE_ANONYMOUS_P (t) && TYPE_NAMESPACE_SCOPE_P (t))
2316 return t;
2318 for (r = CP_TYPE_CONTEXT (t); ; )
2320 /* If we're a nested type of a !TREE_PUBLIC class, we might not
2321 have linkage, or we might just be in an anonymous namespace.
2322 If we're in a TREE_PUBLIC class, we have linkage. */
2323 if (TYPE_P (r) && !TREE_PUBLIC (TYPE_NAME (r)))
2324 return no_linkage_check (TYPE_CONTEXT (t), relaxed_p);
2325 else if (TREE_CODE (r) == FUNCTION_DECL)
2327 if (!relaxed_p || !vague_linkage_p (r))
2328 return t;
2329 else
2330 r = CP_DECL_CONTEXT (r);
2332 else
2333 break;
2336 return NULL_TREE;
2338 case ARRAY_TYPE:
2339 case POINTER_TYPE:
2340 case REFERENCE_TYPE:
2341 case VECTOR_TYPE:
2342 return no_linkage_check (TREE_TYPE (t), relaxed_p);
2344 case OFFSET_TYPE:
2345 ptrmem:
2346 r = no_linkage_check (TYPE_PTRMEM_POINTED_TO_TYPE (t),
2347 relaxed_p);
2348 if (r)
2349 return r;
2350 return no_linkage_check (TYPE_PTRMEM_CLASS_TYPE (t), relaxed_p);
2352 case METHOD_TYPE:
2353 case FUNCTION_TYPE:
2355 tree parm = TYPE_ARG_TYPES (t);
2356 if (TREE_CODE (t) == METHOD_TYPE)
2357 /* The 'this' pointer isn't interesting; a method has the same
2358 linkage (or lack thereof) as its enclosing class. */
2359 parm = TREE_CHAIN (parm);
2360 for (;
2361 parm && parm != void_list_node;
2362 parm = TREE_CHAIN (parm))
2364 r = no_linkage_check (TREE_VALUE (parm), relaxed_p);
2365 if (r)
2366 return r;
2368 return no_linkage_check (TREE_TYPE (t), relaxed_p);
2371 default:
2372 return NULL_TREE;
2376 extern int depth_reached;
2378 void
2379 cxx_print_statistics (void)
2381 print_search_statistics ();
2382 print_class_statistics ();
2383 print_template_statistics ();
2384 if (GATHER_STATISTICS)
2385 fprintf (stderr, "maximum template instantiation depth reached: %d\n",
2386 depth_reached);
2389 /* Return, as an INTEGER_CST node, the number of elements for TYPE
2390 (which is an ARRAY_TYPE). This counts only elements of the top
2391 array. */
2393 tree
2394 array_type_nelts_top (tree type)
2396 return fold_build2_loc (input_location,
2397 PLUS_EXPR, sizetype,
2398 array_type_nelts (type),
2399 size_one_node);
2402 /* Return, as an INTEGER_CST node, the number of elements for TYPE
2403 (which is an ARRAY_TYPE). This one is a recursive count of all
2404 ARRAY_TYPEs that are clumped together. */
2406 tree
2407 array_type_nelts_total (tree type)
2409 tree sz = array_type_nelts_top (type);
2410 type = TREE_TYPE (type);
2411 while (TREE_CODE (type) == ARRAY_TYPE)
2413 tree n = array_type_nelts_top (type);
2414 sz = fold_build2_loc (input_location,
2415 MULT_EXPR, sizetype, sz, n);
2416 type = TREE_TYPE (type);
2418 return sz;
2421 /* Called from break_out_target_exprs via mapcar. */
2423 static tree
2424 bot_manip (tree* tp, int* walk_subtrees, void* data)
2426 splay_tree target_remap = ((splay_tree) data);
2427 tree t = *tp;
2429 if (!TYPE_P (t) && TREE_CONSTANT (t) && !TREE_SIDE_EFFECTS (t))
2431 /* There can't be any TARGET_EXPRs or their slot variables below this
2432 point. But we must make a copy, in case subsequent processing
2433 alters any part of it. For example, during gimplification a cast
2434 of the form (T) &X::f (where "f" is a member function) will lead
2435 to replacing the PTRMEM_CST for &X::f with a VAR_DECL. */
2436 *walk_subtrees = 0;
2437 *tp = unshare_expr (t);
2438 return NULL_TREE;
2440 if (TREE_CODE (t) == TARGET_EXPR)
2442 tree u;
2444 if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
2446 u = build_cplus_new (TREE_TYPE (t), TREE_OPERAND (t, 1),
2447 tf_warning_or_error);
2448 if (AGGR_INIT_ZERO_FIRST (TREE_OPERAND (t, 1)))
2449 AGGR_INIT_ZERO_FIRST (TREE_OPERAND (u, 1)) = true;
2451 else
2452 u = build_target_expr_with_type (TREE_OPERAND (t, 1), TREE_TYPE (t),
2453 tf_warning_or_error);
2455 TARGET_EXPR_IMPLICIT_P (u) = TARGET_EXPR_IMPLICIT_P (t);
2456 TARGET_EXPR_LIST_INIT_P (u) = TARGET_EXPR_LIST_INIT_P (t);
2457 TARGET_EXPR_DIRECT_INIT_P (u) = TARGET_EXPR_DIRECT_INIT_P (t);
2459 /* Map the old variable to the new one. */
2460 splay_tree_insert (target_remap,
2461 (splay_tree_key) TREE_OPERAND (t, 0),
2462 (splay_tree_value) TREE_OPERAND (u, 0));
2464 TREE_OPERAND (u, 1) = break_out_target_exprs (TREE_OPERAND (u, 1));
2466 /* Replace the old expression with the new version. */
2467 *tp = u;
2468 /* We don't have to go below this point; the recursive call to
2469 break_out_target_exprs will have handled anything below this
2470 point. */
2471 *walk_subtrees = 0;
2472 return NULL_TREE;
2474 if (TREE_CODE (*tp) == SAVE_EXPR)
2476 t = *tp;
2477 splay_tree_node n = splay_tree_lookup (target_remap,
2478 (splay_tree_key) t);
2479 if (n)
2481 *tp = (tree)n->value;
2482 *walk_subtrees = 0;
2484 else
2486 copy_tree_r (tp, walk_subtrees, NULL);
2487 splay_tree_insert (target_remap,
2488 (splay_tree_key)t,
2489 (splay_tree_value)*tp);
2490 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
2491 splay_tree_insert (target_remap,
2492 (splay_tree_key)*tp,
2493 (splay_tree_value)*tp);
2495 return NULL_TREE;
2498 /* Make a copy of this node. */
2499 t = copy_tree_r (tp, walk_subtrees, NULL);
2500 if (TREE_CODE (*tp) == CALL_EXPR)
2502 set_flags_from_callee (*tp);
2504 /* builtin_LINE and builtin_FILE get the location where the default
2505 argument is expanded, not where the call was written. */
2506 tree callee = get_callee_fndecl (*tp);
2507 if (callee && DECL_BUILT_IN (callee))
2508 switch (DECL_FUNCTION_CODE (callee))
2510 case BUILT_IN_FILE:
2511 case BUILT_IN_LINE:
2512 SET_EXPR_LOCATION (*tp, input_location);
2513 default:
2514 break;
2517 return t;
2520 /* Replace all remapped VAR_DECLs in T with their new equivalents.
2521 DATA is really a splay-tree mapping old variables to new
2522 variables. */
2524 static tree
2525 bot_replace (tree* t, int* /*walk_subtrees*/, void* data)
2527 splay_tree target_remap = ((splay_tree) data);
2529 if (VAR_P (*t))
2531 splay_tree_node n = splay_tree_lookup (target_remap,
2532 (splay_tree_key) *t);
2533 if (n)
2534 *t = (tree) n->value;
2536 else if (TREE_CODE (*t) == PARM_DECL
2537 && DECL_NAME (*t) == this_identifier
2538 && !DECL_CONTEXT (*t))
2540 /* In an NSDMI we need to replace the 'this' parameter we used for
2541 parsing with the real one for this function. */
2542 *t = current_class_ptr;
2544 else if (TREE_CODE (*t) == CONVERT_EXPR
2545 && CONVERT_EXPR_VBASE_PATH (*t))
2547 /* In an NSDMI build_base_path defers building conversions to virtual
2548 bases, and we handle it here. */
2549 tree basetype = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (*t)));
2550 vec<tree, va_gc> *vbases = CLASSTYPE_VBASECLASSES (current_class_type);
2551 int i; tree binfo;
2552 FOR_EACH_VEC_SAFE_ELT (vbases, i, binfo)
2553 if (BINFO_TYPE (binfo) == basetype)
2554 break;
2555 *t = build_base_path (PLUS_EXPR, TREE_OPERAND (*t, 0), binfo, true,
2556 tf_warning_or_error);
2559 return NULL_TREE;
2562 /* When we parse a default argument expression, we may create
2563 temporary variables via TARGET_EXPRs. When we actually use the
2564 default-argument expression, we make a copy of the expression
2565 and replace the temporaries with appropriate local versions. */
2567 tree
2568 break_out_target_exprs (tree t)
2570 static int target_remap_count;
2571 static splay_tree target_remap;
2573 if (!target_remap_count++)
2574 target_remap = splay_tree_new (splay_tree_compare_pointers,
2575 /*splay_tree_delete_key_fn=*/NULL,
2576 /*splay_tree_delete_value_fn=*/NULL);
2577 cp_walk_tree (&t, bot_manip, target_remap, NULL);
2578 cp_walk_tree (&t, bot_replace, target_remap, NULL);
2580 if (!--target_remap_count)
2582 splay_tree_delete (target_remap);
2583 target_remap = NULL;
2586 return t;
2589 /* Build an expression for the subobject of OBJ at CONSTRUCTOR index INDEX,
2590 which we expect to have type TYPE. */
2592 tree
2593 build_ctor_subob_ref (tree index, tree type, tree obj)
2595 if (index == NULL_TREE)
2596 /* Can't refer to a particular member of a vector. */
2597 obj = NULL_TREE;
2598 else if (TREE_CODE (index) == INTEGER_CST)
2599 obj = cp_build_array_ref (input_location, obj, index, tf_none);
2600 else
2601 obj = build_class_member_access_expr (obj, index, NULL_TREE,
2602 /*reference*/false, tf_none);
2603 if (obj)
2605 tree objtype = TREE_TYPE (obj);
2606 if (TREE_CODE (objtype) == ARRAY_TYPE && !TYPE_DOMAIN (objtype))
2608 /* When the destination object refers to a flexible array member
2609 verify that it matches the type of the source object except
2610 for its domain and qualifiers. */
2611 gcc_assert (comptypes (TYPE_MAIN_VARIANT (type),
2612 TYPE_MAIN_VARIANT (objtype),
2613 COMPARE_REDECLARATION));
2615 else
2616 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, objtype));
2619 return obj;
2622 /* Like substitute_placeholder_in_expr, but handle C++ tree codes and
2623 build up subexpressions as we go deeper. */
2625 static tree
2626 replace_placeholders_r (tree* t, int* walk_subtrees, void* data_)
2628 tree obj = static_cast<tree>(data_);
2630 if (TREE_CONSTANT (*t))
2632 *walk_subtrees = false;
2633 return NULL_TREE;
2636 switch (TREE_CODE (*t))
2638 case PLACEHOLDER_EXPR:
2640 tree x = obj;
2641 for (; !(same_type_ignoring_top_level_qualifiers_p
2642 (TREE_TYPE (*t), TREE_TYPE (x)));
2643 x = TREE_OPERAND (x, 0))
2644 gcc_assert (TREE_CODE (x) == COMPONENT_REF);
2645 *t = x;
2646 *walk_subtrees = false;
2648 break;
2650 case CONSTRUCTOR:
2652 constructor_elt *ce;
2653 vec<constructor_elt,va_gc> *v = CONSTRUCTOR_ELTS (*t);
2654 for (unsigned i = 0; vec_safe_iterate (v, i, &ce); ++i)
2656 tree *valp = &ce->value;
2657 tree type = TREE_TYPE (*valp);
2658 tree subob = obj;
2660 if (TREE_CODE (*valp) == CONSTRUCTOR
2661 && AGGREGATE_TYPE_P (type))
2663 /* If we're looking at the initializer for OBJ, then build
2664 a sub-object reference. If we're looking at an
2665 initializer for another object, just pass OBJ down. */
2666 if (same_type_ignoring_top_level_qualifiers_p
2667 (TREE_TYPE (*t), TREE_TYPE (obj)))
2668 subob = build_ctor_subob_ref (ce->index, type, obj);
2669 if (TREE_CODE (*valp) == TARGET_EXPR)
2670 valp = &TARGET_EXPR_INITIAL (*valp);
2673 cp_walk_tree (valp, replace_placeholders_r,
2674 subob, NULL);
2676 *walk_subtrees = false;
2677 break;
2680 default:
2681 break;
2684 return NULL_TREE;
2687 tree
2688 replace_placeholders (tree exp, tree obj)
2690 tree *tp = &exp;
2691 if (TREE_CODE (exp) == TARGET_EXPR)
2692 tp = &TARGET_EXPR_INITIAL (exp);
2693 cp_walk_tree (tp, replace_placeholders_r, obj, NULL);
2694 return exp;
2697 /* Similar to `build_nt', but for template definitions of dependent
2698 expressions */
2700 tree
2701 build_min_nt_loc (location_t loc, enum tree_code code, ...)
2703 tree t;
2704 int length;
2705 int i;
2706 va_list p;
2708 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
2710 va_start (p, code);
2712 t = make_node (code);
2713 SET_EXPR_LOCATION (t, loc);
2714 length = TREE_CODE_LENGTH (code);
2716 for (i = 0; i < length; i++)
2718 tree x = va_arg (p, tree);
2719 TREE_OPERAND (t, i) = x;
2722 va_end (p);
2723 return t;
2727 /* Similar to `build', but for template definitions. */
2729 tree
2730 build_min (enum tree_code code, tree tt, ...)
2732 tree t;
2733 int length;
2734 int i;
2735 va_list p;
2737 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
2739 va_start (p, tt);
2741 t = make_node (code);
2742 length = TREE_CODE_LENGTH (code);
2743 TREE_TYPE (t) = tt;
2745 for (i = 0; i < length; i++)
2747 tree x = va_arg (p, tree);
2748 TREE_OPERAND (t, i) = x;
2749 if (x && !TYPE_P (x) && TREE_SIDE_EFFECTS (x))
2750 TREE_SIDE_EFFECTS (t) = 1;
2753 va_end (p);
2754 return t;
2757 /* Similar to `build', but for template definitions of non-dependent
2758 expressions. NON_DEP is the non-dependent expression that has been
2759 built. */
2761 tree
2762 build_min_non_dep (enum tree_code code, tree non_dep, ...)
2764 tree t;
2765 int length;
2766 int i;
2767 va_list p;
2769 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
2771 va_start (p, non_dep);
2773 if (REFERENCE_REF_P (non_dep))
2774 non_dep = TREE_OPERAND (non_dep, 0);
2776 t = make_node (code);
2777 length = TREE_CODE_LENGTH (code);
2778 TREE_TYPE (t) = TREE_TYPE (non_dep);
2779 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
2781 for (i = 0; i < length; i++)
2783 tree x = va_arg (p, tree);
2784 TREE_OPERAND (t, i) = x;
2787 if (code == COMPOUND_EXPR && TREE_CODE (non_dep) != COMPOUND_EXPR)
2788 /* This should not be considered a COMPOUND_EXPR, because it
2789 resolves to an overload. */
2790 COMPOUND_EXPR_OVERLOADED (t) = 1;
2792 va_end (p);
2793 return convert_from_reference (t);
2796 /* Similar to `build_nt_call_vec', but for template definitions of
2797 non-dependent expressions. NON_DEP is the non-dependent expression
2798 that has been built. */
2800 tree
2801 build_min_non_dep_call_vec (tree non_dep, tree fn, vec<tree, va_gc> *argvec)
2803 tree t = build_nt_call_vec (fn, argvec);
2804 if (REFERENCE_REF_P (non_dep))
2805 non_dep = TREE_OPERAND (non_dep, 0);
2806 TREE_TYPE (t) = TREE_TYPE (non_dep);
2807 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
2808 return convert_from_reference (t);
2811 /* Similar to build_min_non_dep, but for expressions that have been resolved to
2812 a call to an operator overload. OP is the operator that has been
2813 overloaded. NON_DEP is the non-dependent expression that's been built,
2814 which should be a CALL_EXPR or an INDIRECT_REF to a CALL_EXPR. OVERLOAD is
2815 the overload that NON_DEP is calling. */
2817 tree
2818 build_min_non_dep_op_overload (enum tree_code op,
2819 tree non_dep,
2820 tree overload, ...)
2822 va_list p;
2823 int nargs, expected_nargs;
2824 tree fn, call;
2825 vec<tree, va_gc> *args;
2827 if (REFERENCE_REF_P (non_dep))
2828 non_dep = TREE_OPERAND (non_dep, 0);
2830 nargs = call_expr_nargs (non_dep);
2832 expected_nargs = cp_tree_code_length (op);
2833 if (op == POSTINCREMENT_EXPR
2834 || op == POSTDECREMENT_EXPR)
2835 expected_nargs += 1;
2836 gcc_assert (nargs == expected_nargs);
2838 args = make_tree_vector ();
2839 va_start (p, overload);
2841 if (TREE_CODE (TREE_TYPE (overload)) == FUNCTION_TYPE)
2843 fn = overload;
2844 for (int i = 0; i < nargs; i++)
2846 tree arg = va_arg (p, tree);
2847 vec_safe_push (args, arg);
2850 else if (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE)
2852 tree object = va_arg (p, tree);
2853 tree binfo = TYPE_BINFO (TREE_TYPE (object));
2854 tree method = build_baselink (binfo, binfo, overload, NULL_TREE);
2855 fn = build_min (COMPONENT_REF, TREE_TYPE (overload),
2856 object, method, NULL_TREE);
2857 for (int i = 1; i < nargs; i++)
2859 tree arg = va_arg (p, tree);
2860 vec_safe_push (args, arg);
2863 else
2864 gcc_unreachable ();
2866 va_end (p);
2867 call = build_min_non_dep_call_vec (non_dep, fn, args);
2868 release_tree_vector (args);
2870 tree call_expr = call;
2871 if (REFERENCE_REF_P (call_expr))
2872 call_expr = TREE_OPERAND (call_expr, 0);
2873 KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep);
2875 return call;
2878 tree
2879 get_type_decl (tree t)
2881 if (TREE_CODE (t) == TYPE_DECL)
2882 return t;
2883 if (TYPE_P (t))
2884 return TYPE_STUB_DECL (t);
2885 gcc_assert (t == error_mark_node);
2886 return t;
2889 /* Returns the namespace that contains DECL, whether directly or
2890 indirectly. */
2892 tree
2893 decl_namespace_context (tree decl)
2895 while (1)
2897 if (TREE_CODE (decl) == NAMESPACE_DECL)
2898 return decl;
2899 else if (TYPE_P (decl))
2900 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
2901 else
2902 decl = CP_DECL_CONTEXT (decl);
2906 /* Returns true if decl is within an anonymous namespace, however deeply
2907 nested, or false otherwise. */
2909 bool
2910 decl_anon_ns_mem_p (const_tree decl)
2912 while (1)
2914 if (decl == NULL_TREE || decl == error_mark_node)
2915 return false;
2916 if (TREE_CODE (decl) == NAMESPACE_DECL
2917 && DECL_NAME (decl) == NULL_TREE)
2918 return true;
2919 /* Classes and namespaces inside anonymous namespaces have
2920 TREE_PUBLIC == 0, so we can shortcut the search. */
2921 else if (TYPE_P (decl))
2922 return (TREE_PUBLIC (TYPE_MAIN_DECL (decl)) == 0);
2923 else if (TREE_CODE (decl) == NAMESPACE_DECL)
2924 return (TREE_PUBLIC (decl) == 0);
2925 else
2926 decl = DECL_CONTEXT (decl);
2930 /* Subroutine of cp_tree_equal: t1 and t2 are the CALL_EXPR_FNs of two
2931 CALL_EXPRS. Return whether they are equivalent. */
2933 static bool
2934 called_fns_equal (tree t1, tree t2)
2936 /* Core 1321: dependent names are equivalent even if the overload sets
2937 are different. But do compare explicit template arguments. */
2938 tree name1 = dependent_name (t1);
2939 tree name2 = dependent_name (t2);
2940 if (name1 || name2)
2942 tree targs1 = NULL_TREE, targs2 = NULL_TREE;
2944 if (name1 != name2)
2945 return false;
2947 if (TREE_CODE (t1) == TEMPLATE_ID_EXPR)
2948 targs1 = TREE_OPERAND (t1, 1);
2949 if (TREE_CODE (t2) == TEMPLATE_ID_EXPR)
2950 targs2 = TREE_OPERAND (t2, 1);
2951 return cp_tree_equal (targs1, targs2);
2953 else
2954 return cp_tree_equal (t1, t2);
2957 /* Return truthvalue of whether T1 is the same tree structure as T2.
2958 Return 1 if they are the same. Return 0 if they are different. */
2960 bool
2961 cp_tree_equal (tree t1, tree t2)
2963 enum tree_code code1, code2;
2965 if (t1 == t2)
2966 return true;
2967 if (!t1 || !t2)
2968 return false;
2970 code1 = TREE_CODE (t1);
2971 code2 = TREE_CODE (t2);
2973 if (code1 != code2)
2974 return false;
2976 switch (code1)
2978 case VOID_CST:
2979 /* There's only a single VOID_CST node, so we should never reach
2980 here. */
2981 gcc_unreachable ();
2983 case INTEGER_CST:
2984 return tree_int_cst_equal (t1, t2);
2986 case REAL_CST:
2987 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
2989 case STRING_CST:
2990 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2991 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2992 TREE_STRING_LENGTH (t1));
2994 case FIXED_CST:
2995 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
2996 TREE_FIXED_CST (t2));
2998 case COMPLEX_CST:
2999 return cp_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
3000 && cp_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
3002 case VECTOR_CST:
3003 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
3005 case CONSTRUCTOR:
3006 /* We need to do this when determining whether or not two
3007 non-type pointer to member function template arguments
3008 are the same. */
3009 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
3010 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
3011 return false;
3013 tree field, value;
3014 unsigned int i;
3015 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
3017 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
3018 if (!cp_tree_equal (field, elt2->index)
3019 || !cp_tree_equal (value, elt2->value))
3020 return false;
3023 return true;
3025 case TREE_LIST:
3026 if (!cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
3027 return false;
3028 if (!cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
3029 return false;
3030 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
3032 case SAVE_EXPR:
3033 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3035 case CALL_EXPR:
3037 tree arg1, arg2;
3038 call_expr_arg_iterator iter1, iter2;
3039 if (!called_fns_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
3040 return false;
3041 for (arg1 = first_call_expr_arg (t1, &iter1),
3042 arg2 = first_call_expr_arg (t2, &iter2);
3043 arg1 && arg2;
3044 arg1 = next_call_expr_arg (&iter1),
3045 arg2 = next_call_expr_arg (&iter2))
3046 if (!cp_tree_equal (arg1, arg2))
3047 return false;
3048 if (arg1 || arg2)
3049 return false;
3050 return true;
3053 case TARGET_EXPR:
3055 tree o1 = TREE_OPERAND (t1, 0);
3056 tree o2 = TREE_OPERAND (t2, 0);
3058 /* Special case: if either target is an unallocated VAR_DECL,
3059 it means that it's going to be unified with whatever the
3060 TARGET_EXPR is really supposed to initialize, so treat it
3061 as being equivalent to anything. */
3062 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
3063 && !DECL_RTL_SET_P (o1))
3064 /*Nop*/;
3065 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
3066 && !DECL_RTL_SET_P (o2))
3067 /*Nop*/;
3068 else if (!cp_tree_equal (o1, o2))
3069 return false;
3071 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3074 case WITH_CLEANUP_EXPR:
3075 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
3076 return false;
3077 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
3079 case COMPONENT_REF:
3080 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
3081 return false;
3082 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3084 case PARM_DECL:
3085 /* For comparing uses of parameters in late-specified return types
3086 with an out-of-class definition of the function, but can also come
3087 up for expressions that involve 'this' in a member function
3088 template. */
3090 if (comparing_specializations && !CONSTRAINT_VAR_P (t1))
3091 /* When comparing hash table entries, only an exact match is
3092 good enough; we don't want to replace 'this' with the
3093 version from another function. But be more flexible
3094 with local parameters in a requires-expression. */
3095 return false;
3097 if (same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3099 if (DECL_ARTIFICIAL (t1) ^ DECL_ARTIFICIAL (t2))
3100 return false;
3101 if (CONSTRAINT_VAR_P (t1) ^ CONSTRAINT_VAR_P (t2))
3102 return false;
3103 if (DECL_ARTIFICIAL (t1)
3104 || (DECL_PARM_LEVEL (t1) == DECL_PARM_LEVEL (t2)
3105 && DECL_PARM_INDEX (t1) == DECL_PARM_INDEX (t2)))
3106 return true;
3108 return false;
3110 case VAR_DECL:
3111 case CONST_DECL:
3112 case FIELD_DECL:
3113 case FUNCTION_DECL:
3114 case TEMPLATE_DECL:
3115 case IDENTIFIER_NODE:
3116 case SSA_NAME:
3117 return false;
3119 case BASELINK:
3120 return (BASELINK_BINFO (t1) == BASELINK_BINFO (t2)
3121 && BASELINK_ACCESS_BINFO (t1) == BASELINK_ACCESS_BINFO (t2)
3122 && BASELINK_QUALIFIED_P (t1) == BASELINK_QUALIFIED_P (t2)
3123 && cp_tree_equal (BASELINK_FUNCTIONS (t1),
3124 BASELINK_FUNCTIONS (t2)));
3126 case TEMPLATE_PARM_INDEX:
3127 return (TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
3128 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2)
3129 && (TEMPLATE_PARM_PARAMETER_PACK (t1)
3130 == TEMPLATE_PARM_PARAMETER_PACK (t2))
3131 && same_type_p (TREE_TYPE (TEMPLATE_PARM_DECL (t1)),
3132 TREE_TYPE (TEMPLATE_PARM_DECL (t2))));
3134 case TEMPLATE_ID_EXPR:
3135 return (cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3136 && cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3138 case CONSTRAINT_INFO:
3139 return cp_tree_equal (CI_ASSOCIATED_CONSTRAINTS (t1),
3140 CI_ASSOCIATED_CONSTRAINTS (t2));
3142 case TREE_VEC:
3144 unsigned ix;
3145 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3146 return false;
3147 for (ix = TREE_VEC_LENGTH (t1); ix--;)
3148 if (!cp_tree_equal (TREE_VEC_ELT (t1, ix),
3149 TREE_VEC_ELT (t2, ix)))
3150 return false;
3151 return true;
3154 case SIZEOF_EXPR:
3155 case ALIGNOF_EXPR:
3157 tree o1 = TREE_OPERAND (t1, 0);
3158 tree o2 = TREE_OPERAND (t2, 0);
3160 if (code1 == SIZEOF_EXPR)
3162 if (SIZEOF_EXPR_TYPE_P (t1))
3163 o1 = TREE_TYPE (o1);
3164 if (SIZEOF_EXPR_TYPE_P (t2))
3165 o2 = TREE_TYPE (o2);
3167 if (TREE_CODE (o1) != TREE_CODE (o2))
3168 return false;
3169 if (TYPE_P (o1))
3170 return same_type_p (o1, o2);
3171 else
3172 return cp_tree_equal (o1, o2);
3175 case MODOP_EXPR:
3177 tree t1_op1, t2_op1;
3179 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
3180 return false;
3182 t1_op1 = TREE_OPERAND (t1, 1);
3183 t2_op1 = TREE_OPERAND (t2, 1);
3184 if (TREE_CODE (t1_op1) != TREE_CODE (t2_op1))
3185 return false;
3187 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t2, 2));
3190 case PTRMEM_CST:
3191 /* Two pointer-to-members are the same if they point to the same
3192 field or function in the same class. */
3193 if (PTRMEM_CST_MEMBER (t1) != PTRMEM_CST_MEMBER (t2))
3194 return false;
3196 return same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2));
3198 case OVERLOAD:
3199 if (OVL_FUNCTION (t1) != OVL_FUNCTION (t2))
3200 return false;
3201 return cp_tree_equal (OVL_CHAIN (t1), OVL_CHAIN (t2));
3203 case TRAIT_EXPR:
3204 if (TRAIT_EXPR_KIND (t1) != TRAIT_EXPR_KIND (t2))
3205 return false;
3206 return same_type_p (TRAIT_EXPR_TYPE1 (t1), TRAIT_EXPR_TYPE1 (t2))
3207 && cp_tree_equal (TRAIT_EXPR_TYPE2 (t1), TRAIT_EXPR_TYPE2 (t2));
3209 case CAST_EXPR:
3210 case STATIC_CAST_EXPR:
3211 case REINTERPRET_CAST_EXPR:
3212 case CONST_CAST_EXPR:
3213 case DYNAMIC_CAST_EXPR:
3214 case IMPLICIT_CONV_EXPR:
3215 case NEW_EXPR:
3216 CASE_CONVERT:
3217 case NON_LVALUE_EXPR:
3218 case VIEW_CONVERT_EXPR:
3219 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3220 return false;
3221 /* Now compare operands as usual. */
3222 break;
3224 case DEFERRED_NOEXCEPT:
3225 return (cp_tree_equal (DEFERRED_NOEXCEPT_PATTERN (t1),
3226 DEFERRED_NOEXCEPT_PATTERN (t2))
3227 && comp_template_args (DEFERRED_NOEXCEPT_ARGS (t1),
3228 DEFERRED_NOEXCEPT_ARGS (t2)));
3229 break;
3231 default:
3232 break;
3235 switch (TREE_CODE_CLASS (code1))
3237 case tcc_unary:
3238 case tcc_binary:
3239 case tcc_comparison:
3240 case tcc_expression:
3241 case tcc_vl_exp:
3242 case tcc_reference:
3243 case tcc_statement:
3245 int i, n;
3247 n = cp_tree_operand_length (t1);
3248 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
3249 && n != TREE_OPERAND_LENGTH (t2))
3250 return false;
3252 for (i = 0; i < n; ++i)
3253 if (!cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
3254 return false;
3256 return true;
3259 case tcc_type:
3260 return same_type_p (t1, t2);
3261 default:
3262 gcc_unreachable ();
3264 /* We can get here with --disable-checking. */
3265 return false;
3268 /* The type of ARG when used as an lvalue. */
3270 tree
3271 lvalue_type (tree arg)
3273 tree type = TREE_TYPE (arg);
3274 return type;
3277 /* The type of ARG for printing error messages; denote lvalues with
3278 reference types. */
3280 tree
3281 error_type (tree arg)
3283 tree type = TREE_TYPE (arg);
3285 if (TREE_CODE (type) == ARRAY_TYPE)
3287 else if (TREE_CODE (type) == ERROR_MARK)
3289 else if (real_lvalue_p (arg))
3290 type = build_reference_type (lvalue_type (arg));
3291 else if (MAYBE_CLASS_TYPE_P (type))
3292 type = lvalue_type (arg);
3294 return type;
3297 /* Does FUNCTION use a variable-length argument list? */
3300 varargs_function_p (const_tree function)
3302 return stdarg_p (TREE_TYPE (function));
3305 /* Returns 1 if decl is a member of a class. */
3308 member_p (const_tree decl)
3310 const_tree const ctx = DECL_CONTEXT (decl);
3311 return (ctx && TYPE_P (ctx));
3314 /* Create a placeholder for member access where we don't actually have an
3315 object that the access is against. */
3317 tree
3318 build_dummy_object (tree type)
3320 tree decl = build1 (CONVERT_EXPR, build_pointer_type (type), void_node);
3321 return cp_build_indirect_ref (decl, RO_NULL, tf_warning_or_error);
3324 /* We've gotten a reference to a member of TYPE. Return *this if appropriate,
3325 or a dummy object otherwise. If BINFOP is non-0, it is filled with the
3326 binfo path from current_class_type to TYPE, or 0. */
3328 tree
3329 maybe_dummy_object (tree type, tree* binfop)
3331 tree decl, context;
3332 tree binfo;
3333 tree current = current_nonlambda_class_type ();
3335 if (current
3336 && (binfo = lookup_base (current, type, ba_any, NULL,
3337 tf_warning_or_error)))
3338 context = current;
3339 else
3341 /* Reference from a nested class member function. */
3342 context = type;
3343 binfo = TYPE_BINFO (type);
3346 if (binfop)
3347 *binfop = binfo;
3349 if (current_class_ref
3350 /* current_class_ref might not correspond to current_class_type if
3351 we're in tsubst_default_argument or a lambda-declarator; in either
3352 case, we want to use current_class_ref if it matches CONTEXT. */
3353 && (same_type_ignoring_top_level_qualifiers_p
3354 (TREE_TYPE (current_class_ref), context)))
3355 decl = current_class_ref;
3356 else
3357 decl = build_dummy_object (context);
3359 return decl;
3362 /* Returns 1 if OB is a placeholder object, or a pointer to one. */
3365 is_dummy_object (const_tree ob)
3367 if (INDIRECT_REF_P (ob))
3368 ob = TREE_OPERAND (ob, 0);
3369 return (TREE_CODE (ob) == CONVERT_EXPR
3370 && TREE_OPERAND (ob, 0) == void_node);
3373 /* Returns 1 iff type T is something we want to treat as a scalar type for
3374 the purpose of deciding whether it is trivial/POD/standard-layout. */
3376 bool
3377 scalarish_type_p (const_tree t)
3379 if (t == error_mark_node)
3380 return 1;
3382 return (SCALAR_TYPE_P (t) || VECTOR_TYPE_P (t));
3385 /* Returns true iff T requires non-trivial default initialization. */
3387 bool
3388 type_has_nontrivial_default_init (const_tree t)
3390 t = strip_array_types (CONST_CAST_TREE (t));
3392 if (CLASS_TYPE_P (t))
3393 return TYPE_HAS_COMPLEX_DFLT (t);
3394 else
3395 return 0;
3398 /* Returns true iff copying an object of type T (including via move
3399 constructor) is non-trivial. That is, T has no non-trivial copy
3400 constructors and no non-trivial move constructors. */
3402 bool
3403 type_has_nontrivial_copy_init (const_tree t)
3405 t = strip_array_types (CONST_CAST_TREE (t));
3407 if (CLASS_TYPE_P (t))
3409 gcc_assert (COMPLETE_TYPE_P (t));
3410 return ((TYPE_HAS_COPY_CTOR (t)
3411 && TYPE_HAS_COMPLEX_COPY_CTOR (t))
3412 || TYPE_HAS_COMPLEX_MOVE_CTOR (t));
3414 else
3415 return 0;
3418 /* Returns 1 iff type T is a trivially copyable type, as defined in
3419 [basic.types] and [class]. */
3421 bool
3422 trivially_copyable_p (const_tree t)
3424 t = strip_array_types (CONST_CAST_TREE (t));
3426 if (CLASS_TYPE_P (t))
3427 return ((!TYPE_HAS_COPY_CTOR (t)
3428 || !TYPE_HAS_COMPLEX_COPY_CTOR (t))
3429 && !TYPE_HAS_COMPLEX_MOVE_CTOR (t)
3430 && (!TYPE_HAS_COPY_ASSIGN (t)
3431 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (t))
3432 && !TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
3433 && TYPE_HAS_TRIVIAL_DESTRUCTOR (t));
3434 else
3435 return !CP_TYPE_VOLATILE_P (t) && scalarish_type_p (t);
3438 /* Returns 1 iff type T is a trivial type, as defined in [basic.types] and
3439 [class]. */
3441 bool
3442 trivial_type_p (const_tree t)
3444 t = strip_array_types (CONST_CAST_TREE (t));
3446 if (CLASS_TYPE_P (t))
3447 return (TYPE_HAS_TRIVIAL_DFLT (t)
3448 && trivially_copyable_p (t));
3449 else
3450 return scalarish_type_p (t);
3453 /* Returns 1 iff type T is a POD type, as defined in [basic.types]. */
3455 bool
3456 pod_type_p (const_tree t)
3458 /* This CONST_CAST is okay because strip_array_types returns its
3459 argument unmodified and we assign it to a const_tree. */
3460 t = strip_array_types (CONST_CAST_TREE(t));
3462 if (!CLASS_TYPE_P (t))
3463 return scalarish_type_p (t);
3464 else if (cxx_dialect > cxx98)
3465 /* [class]/10: A POD struct is a class that is both a trivial class and a
3466 standard-layout class, and has no non-static data members of type
3467 non-POD struct, non-POD union (or array of such types).
3469 We don't need to check individual members because if a member is
3470 non-std-layout or non-trivial, the class will be too. */
3471 return (std_layout_type_p (t) && trivial_type_p (t));
3472 else
3473 /* The C++98 definition of POD is different. */
3474 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
3477 /* Returns true iff T is POD for the purpose of layout, as defined in the
3478 C++ ABI. */
3480 bool
3481 layout_pod_type_p (const_tree t)
3483 t = strip_array_types (CONST_CAST_TREE (t));
3485 if (CLASS_TYPE_P (t))
3486 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
3487 else
3488 return scalarish_type_p (t);
3491 /* Returns true iff T is a standard-layout type, as defined in
3492 [basic.types]. */
3494 bool
3495 std_layout_type_p (const_tree t)
3497 t = strip_array_types (CONST_CAST_TREE (t));
3499 if (CLASS_TYPE_P (t))
3500 return !CLASSTYPE_NON_STD_LAYOUT (t);
3501 else
3502 return scalarish_type_p (t);
3505 /* Nonzero iff type T is a class template implicit specialization. */
3507 bool
3508 class_tmpl_impl_spec_p (const_tree t)
3510 return CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INSTANTIATION (t);
3513 /* Returns 1 iff zero initialization of type T means actually storing
3514 zeros in it. */
3517 zero_init_p (const_tree t)
3519 /* This CONST_CAST is okay because strip_array_types returns its
3520 argument unmodified and we assign it to a const_tree. */
3521 t = strip_array_types (CONST_CAST_TREE(t));
3523 if (t == error_mark_node)
3524 return 1;
3526 /* NULL pointers to data members are initialized with -1. */
3527 if (TYPE_PTRDATAMEM_P (t))
3528 return 0;
3530 /* Classes that contain types that can't be zero-initialized, cannot
3531 be zero-initialized themselves. */
3532 if (CLASS_TYPE_P (t) && CLASSTYPE_NON_ZERO_INIT_P (t))
3533 return 0;
3535 return 1;
3538 /* Table of valid C++ attributes. */
3539 const struct attribute_spec cxx_attribute_table[] =
3541 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
3542 affects_type_identity } */
3543 { "java_interface", 0, 0, false, false, false,
3544 handle_java_interface_attribute, false },
3545 { "com_interface", 0, 0, false, false, false,
3546 handle_com_interface_attribute, false },
3547 { "init_priority", 1, 1, true, false, false,
3548 handle_init_priority_attribute, false },
3549 { "abi_tag", 1, -1, false, false, false,
3550 handle_abi_tag_attribute, true },
3551 { NULL, 0, 0, false, false, false, NULL, false }
3554 /* Handle a "java_interface" attribute; arguments as in
3555 struct attribute_spec.handler. */
3556 static tree
3557 handle_java_interface_attribute (tree* node,
3558 tree name,
3559 tree /*args*/,
3560 int flags,
3561 bool* no_add_attrs)
3563 if (DECL_P (*node)
3564 || !CLASS_TYPE_P (*node)
3565 || !TYPE_FOR_JAVA (*node))
3567 error ("%qE attribute can only be applied to Java class definitions",
3568 name);
3569 *no_add_attrs = true;
3570 return NULL_TREE;
3572 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
3573 *node = build_variant_type_copy (*node);
3574 TYPE_JAVA_INTERFACE (*node) = 1;
3576 return NULL_TREE;
3579 /* Handle a "com_interface" attribute; arguments as in
3580 struct attribute_spec.handler. */
3581 static tree
3582 handle_com_interface_attribute (tree* node,
3583 tree name,
3584 tree /*args*/,
3585 int /*flags*/,
3586 bool* no_add_attrs)
3588 static int warned;
3590 *no_add_attrs = true;
3592 if (DECL_P (*node)
3593 || !CLASS_TYPE_P (*node)
3594 || *node != TYPE_MAIN_VARIANT (*node))
3596 warning (OPT_Wattributes, "%qE attribute can only be applied "
3597 "to class definitions", name);
3598 return NULL_TREE;
3601 if (!warned++)
3602 warning (0, "%qE is obsolete; g++ vtables are now COM-compatible by default",
3603 name);
3605 return NULL_TREE;
3608 /* Handle an "init_priority" attribute; arguments as in
3609 struct attribute_spec.handler. */
3610 static tree
3611 handle_init_priority_attribute (tree* node,
3612 tree name,
3613 tree args,
3614 int /*flags*/,
3615 bool* no_add_attrs)
3617 tree initp_expr = TREE_VALUE (args);
3618 tree decl = *node;
3619 tree type = TREE_TYPE (decl);
3620 int pri;
3622 STRIP_NOPS (initp_expr);
3623 initp_expr = default_conversion (initp_expr);
3624 if (initp_expr)
3625 initp_expr = maybe_constant_value (initp_expr);
3627 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
3629 error ("requested init_priority is not an integer constant");
3630 cxx_constant_value (initp_expr);
3631 *no_add_attrs = true;
3632 return NULL_TREE;
3635 pri = TREE_INT_CST_LOW (initp_expr);
3637 type = strip_array_types (type);
3639 if (decl == NULL_TREE
3640 || !VAR_P (decl)
3641 || !TREE_STATIC (decl)
3642 || DECL_EXTERNAL (decl)
3643 || (TREE_CODE (type) != RECORD_TYPE
3644 && TREE_CODE (type) != UNION_TYPE)
3645 /* Static objects in functions are initialized the
3646 first time control passes through that
3647 function. This is not precise enough to pin down an
3648 init_priority value, so don't allow it. */
3649 || current_function_decl)
3651 error ("can only use %qE attribute on file-scope definitions "
3652 "of objects of class type", name);
3653 *no_add_attrs = true;
3654 return NULL_TREE;
3657 if (pri > MAX_INIT_PRIORITY || pri <= 0)
3659 error ("requested init_priority is out of range");
3660 *no_add_attrs = true;
3661 return NULL_TREE;
3664 /* Check for init_priorities that are reserved for
3665 language and runtime support implementations.*/
3666 if (pri <= MAX_RESERVED_INIT_PRIORITY)
3668 warning
3669 (0, "requested init_priority is reserved for internal use");
3672 if (SUPPORTS_INIT_PRIORITY)
3674 SET_DECL_INIT_PRIORITY (decl, pri);
3675 DECL_HAS_INIT_PRIORITY_P (decl) = 1;
3676 return NULL_TREE;
3678 else
3680 error ("%qE attribute is not supported on this platform", name);
3681 *no_add_attrs = true;
3682 return NULL_TREE;
3686 /* DECL is being redeclared; the old declaration had the abi tags in OLD,
3687 and the new one has the tags in NEW_. Give an error if there are tags
3688 in NEW_ that weren't in OLD. */
3690 bool
3691 check_abi_tag_redeclaration (const_tree decl, const_tree old, const_tree new_)
3693 if (old && TREE_CODE (TREE_VALUE (old)) == TREE_LIST)
3694 old = TREE_VALUE (old);
3695 if (new_ && TREE_CODE (TREE_VALUE (new_)) == TREE_LIST)
3696 new_ = TREE_VALUE (new_);
3697 bool err = false;
3698 for (const_tree t = new_; t; t = TREE_CHAIN (t))
3700 tree str = TREE_VALUE (t);
3701 for (const_tree in = old; in; in = TREE_CHAIN (in))
3703 tree ostr = TREE_VALUE (in);
3704 if (cp_tree_equal (str, ostr))
3705 goto found;
3707 error ("redeclaration of %qD adds abi tag %E", decl, str);
3708 err = true;
3709 found:;
3711 if (err)
3713 inform (DECL_SOURCE_LOCATION (decl), "previous declaration here");
3714 return false;
3716 return true;
3719 /* The abi_tag attribute with the name NAME was given ARGS. If they are
3720 ill-formed, give an error and return false; otherwise, return true. */
3722 bool
3723 check_abi_tag_args (tree args, tree name)
3725 if (!args)
3727 error ("the %qE attribute requires arguments", name);
3728 return false;
3730 for (tree arg = args; arg; arg = TREE_CHAIN (arg))
3732 tree elt = TREE_VALUE (arg);
3733 if (TREE_CODE (elt) != STRING_CST
3734 || (!same_type_ignoring_top_level_qualifiers_p
3735 (strip_array_types (TREE_TYPE (elt)),
3736 char_type_node)))
3738 error ("arguments to the %qE attribute must be narrow string "
3739 "literals", name);
3740 return false;
3742 const char *begin = TREE_STRING_POINTER (elt);
3743 const char *end = begin + TREE_STRING_LENGTH (elt);
3744 for (const char *p = begin; p != end; ++p)
3746 char c = *p;
3747 if (p == begin)
3749 if (!ISALPHA (c) && c != '_')
3751 error ("arguments to the %qE attribute must contain valid "
3752 "identifiers", name);
3753 inform (input_location, "%<%c%> is not a valid first "
3754 "character for an identifier", c);
3755 return false;
3758 else if (p == end - 1)
3759 gcc_assert (c == 0);
3760 else
3762 if (!ISALNUM (c) && c != '_')
3764 error ("arguments to the %qE attribute must contain valid "
3765 "identifiers", name);
3766 inform (input_location, "%<%c%> is not a valid character "
3767 "in an identifier", c);
3768 return false;
3773 return true;
3776 /* Handle an "abi_tag" attribute; arguments as in
3777 struct attribute_spec.handler. */
3779 static tree
3780 handle_abi_tag_attribute (tree* node, tree name, tree args,
3781 int flags, bool* no_add_attrs)
3783 if (!check_abi_tag_args (args, name))
3784 goto fail;
3786 if (TYPE_P (*node))
3788 if (!OVERLOAD_TYPE_P (*node))
3790 error ("%qE attribute applied to non-class, non-enum type %qT",
3791 name, *node);
3792 goto fail;
3794 else if (!(flags & (int)ATTR_FLAG_TYPE_IN_PLACE))
3796 error ("%qE attribute applied to %qT after its definition",
3797 name, *node);
3798 goto fail;
3800 else if (CLASS_TYPE_P (*node)
3801 && CLASSTYPE_TEMPLATE_INSTANTIATION (*node))
3803 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
3804 "template instantiation %qT", name, *node);
3805 goto fail;
3807 else if (CLASS_TYPE_P (*node)
3808 && CLASSTYPE_TEMPLATE_SPECIALIZATION (*node))
3810 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
3811 "template specialization %qT", name, *node);
3812 goto fail;
3815 tree attributes = TYPE_ATTRIBUTES (*node);
3816 tree decl = TYPE_NAME (*node);
3818 /* Make sure all declarations have the same abi tags. */
3819 if (DECL_SOURCE_LOCATION (decl) != input_location)
3821 if (!check_abi_tag_redeclaration (decl,
3822 lookup_attribute ("abi_tag",
3823 attributes),
3824 args))
3825 goto fail;
3828 else
3830 if (!VAR_OR_FUNCTION_DECL_P (*node))
3832 error ("%qE attribute applied to non-function, non-variable %qD",
3833 name, *node);
3834 goto fail;
3836 else if (DECL_LANGUAGE (*node) == lang_c)
3838 error ("%qE attribute applied to extern \"C\" declaration %qD",
3839 name, *node);
3840 goto fail;
3844 return NULL_TREE;
3846 fail:
3847 *no_add_attrs = true;
3848 return NULL_TREE;
3851 /* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the
3852 thing pointed to by the constant. */
3854 tree
3855 make_ptrmem_cst (tree type, tree member)
3857 tree ptrmem_cst = make_node (PTRMEM_CST);
3858 TREE_TYPE (ptrmem_cst) = type;
3859 PTRMEM_CST_MEMBER (ptrmem_cst) = member;
3860 return ptrmem_cst;
3863 /* Build a variant of TYPE that has the indicated ATTRIBUTES. May
3864 return an existing type if an appropriate type already exists. */
3866 tree
3867 cp_build_type_attribute_variant (tree type, tree attributes)
3869 tree new_type;
3871 new_type = build_type_attribute_variant (type, attributes);
3872 if (TREE_CODE (new_type) == FUNCTION_TYPE
3873 || TREE_CODE (new_type) == METHOD_TYPE)
3875 new_type = build_exception_variant (new_type,
3876 TYPE_RAISES_EXCEPTIONS (type));
3877 new_type = build_ref_qualified_type (new_type,
3878 type_memfn_rqual (type));
3881 /* Making a new main variant of a class type is broken. */
3882 gcc_assert (!CLASS_TYPE_P (type) || new_type == type);
3884 return new_type;
3887 /* Return TRUE if TYPE1 and TYPE2 are identical for type hashing purposes.
3888 Called only after doing all language independent checks. Only
3889 to check TYPE_RAISES_EXCEPTIONS for FUNCTION_TYPE, the rest is already
3890 compared in type_hash_eq. */
3892 bool
3893 cxx_type_hash_eq (const_tree typea, const_tree typeb)
3895 gcc_assert (TREE_CODE (typea) == FUNCTION_TYPE
3896 || TREE_CODE (typea) == METHOD_TYPE);
3898 return comp_except_specs (TYPE_RAISES_EXCEPTIONS (typea),
3899 TYPE_RAISES_EXCEPTIONS (typeb), ce_exact);
3902 /* Apply FUNC to all language-specific sub-trees of TP in a pre-order
3903 traversal. Called from walk_tree. */
3905 tree
3906 cp_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func,
3907 void *data, hash_set<tree> *pset)
3909 enum tree_code code = TREE_CODE (*tp);
3910 tree result;
3912 #define WALK_SUBTREE(NODE) \
3913 do \
3915 result = cp_walk_tree (&(NODE), func, data, pset); \
3916 if (result) goto out; \
3918 while (0)
3920 /* Not one of the easy cases. We must explicitly go through the
3921 children. */
3922 result = NULL_TREE;
3923 switch (code)
3925 case DEFAULT_ARG:
3926 case TEMPLATE_TEMPLATE_PARM:
3927 case BOUND_TEMPLATE_TEMPLATE_PARM:
3928 case UNBOUND_CLASS_TEMPLATE:
3929 case TEMPLATE_PARM_INDEX:
3930 case TEMPLATE_TYPE_PARM:
3931 case TYPENAME_TYPE:
3932 case TYPEOF_TYPE:
3933 case UNDERLYING_TYPE:
3934 /* None of these have subtrees other than those already walked
3935 above. */
3936 *walk_subtrees_p = 0;
3937 break;
3939 case BASELINK:
3940 WALK_SUBTREE (BASELINK_FUNCTIONS (*tp));
3941 *walk_subtrees_p = 0;
3942 break;
3944 case PTRMEM_CST:
3945 WALK_SUBTREE (TREE_TYPE (*tp));
3946 *walk_subtrees_p = 0;
3947 break;
3949 case TREE_LIST:
3950 WALK_SUBTREE (TREE_PURPOSE (*tp));
3951 break;
3953 case OVERLOAD:
3954 WALK_SUBTREE (OVL_FUNCTION (*tp));
3955 WALK_SUBTREE (OVL_CHAIN (*tp));
3956 *walk_subtrees_p = 0;
3957 break;
3959 case USING_DECL:
3960 WALK_SUBTREE (DECL_NAME (*tp));
3961 WALK_SUBTREE (USING_DECL_SCOPE (*tp));
3962 WALK_SUBTREE (USING_DECL_DECLS (*tp));
3963 *walk_subtrees_p = 0;
3964 break;
3966 case RECORD_TYPE:
3967 if (TYPE_PTRMEMFUNC_P (*tp))
3968 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (*tp));
3969 break;
3971 case TYPE_ARGUMENT_PACK:
3972 case NONTYPE_ARGUMENT_PACK:
3974 tree args = ARGUMENT_PACK_ARGS (*tp);
3975 int i, len = TREE_VEC_LENGTH (args);
3976 for (i = 0; i < len; i++)
3977 WALK_SUBTREE (TREE_VEC_ELT (args, i));
3979 break;
3981 case TYPE_PACK_EXPANSION:
3982 WALK_SUBTREE (TREE_TYPE (*tp));
3983 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp));
3984 *walk_subtrees_p = 0;
3985 break;
3987 case EXPR_PACK_EXPANSION:
3988 WALK_SUBTREE (TREE_OPERAND (*tp, 0));
3989 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp));
3990 *walk_subtrees_p = 0;
3991 break;
3993 case CAST_EXPR:
3994 case REINTERPRET_CAST_EXPR:
3995 case STATIC_CAST_EXPR:
3996 case CONST_CAST_EXPR:
3997 case DYNAMIC_CAST_EXPR:
3998 case IMPLICIT_CONV_EXPR:
3999 if (TREE_TYPE (*tp))
4000 WALK_SUBTREE (TREE_TYPE (*tp));
4003 int i;
4004 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (*tp)); ++i)
4005 WALK_SUBTREE (TREE_OPERAND (*tp, i));
4007 *walk_subtrees_p = 0;
4008 break;
4010 case TRAIT_EXPR:
4011 WALK_SUBTREE (TRAIT_EXPR_TYPE1 (*tp));
4012 WALK_SUBTREE (TRAIT_EXPR_TYPE2 (*tp));
4013 *walk_subtrees_p = 0;
4014 break;
4016 case DECLTYPE_TYPE:
4017 WALK_SUBTREE (DECLTYPE_TYPE_EXPR (*tp));
4018 *walk_subtrees_p = 0;
4019 break;
4021 case REQUIRES_EXPR:
4022 // Only recurse through the nested expression. Do not
4023 // walk the parameter list. Doing so causes false
4024 // positives in the pack expansion checker since the
4025 // requires parameters are introduced as pack expansions.
4026 WALK_SUBTREE (TREE_OPERAND (*tp, 1));
4027 *walk_subtrees_p = 0;
4028 break;
4030 default:
4031 return NULL_TREE;
4034 /* We didn't find what we were looking for. */
4035 out:
4036 return result;
4038 #undef WALK_SUBTREE
4041 /* Like save_expr, but for C++. */
4043 tree
4044 cp_save_expr (tree expr)
4046 /* There is no reason to create a SAVE_EXPR within a template; if
4047 needed, we can create the SAVE_EXPR when instantiating the
4048 template. Furthermore, the middle-end cannot handle C++-specific
4049 tree codes. */
4050 if (processing_template_decl)
4051 return expr;
4052 return save_expr (expr);
4055 /* Initialize tree.c. */
4057 void
4058 init_tree (void)
4060 list_hash_table = hash_table<list_hasher>::create_ggc (61);
4063 /* Returns the kind of special function that DECL (a FUNCTION_DECL)
4064 is. Note that sfk_none is zero, so this function can be used as a
4065 predicate to test whether or not DECL is a special function. */
4067 special_function_kind
4068 special_function_p (const_tree decl)
4070 /* Rather than doing all this stuff with magic names, we should
4071 probably have a field of type `special_function_kind' in
4072 DECL_LANG_SPECIFIC. */
4073 if (DECL_INHERITED_CTOR_BASE (decl))
4074 return sfk_inheriting_constructor;
4075 if (DECL_COPY_CONSTRUCTOR_P (decl))
4076 return sfk_copy_constructor;
4077 if (DECL_MOVE_CONSTRUCTOR_P (decl))
4078 return sfk_move_constructor;
4079 if (DECL_CONSTRUCTOR_P (decl))
4080 return sfk_constructor;
4081 if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
4083 if (copy_fn_p (decl))
4084 return sfk_copy_assignment;
4085 if (move_fn_p (decl))
4086 return sfk_move_assignment;
4088 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
4089 return sfk_destructor;
4090 if (DECL_COMPLETE_DESTRUCTOR_P (decl))
4091 return sfk_complete_destructor;
4092 if (DECL_BASE_DESTRUCTOR_P (decl))
4093 return sfk_base_destructor;
4094 if (DECL_DELETING_DESTRUCTOR_P (decl))
4095 return sfk_deleting_destructor;
4096 if (DECL_CONV_FN_P (decl))
4097 return sfk_conversion;
4099 return sfk_none;
4102 /* Returns nonzero if TYPE is a character type, including wchar_t. */
4105 char_type_p (tree type)
4107 return (same_type_p (type, char_type_node)
4108 || same_type_p (type, unsigned_char_type_node)
4109 || same_type_p (type, signed_char_type_node)
4110 || same_type_p (type, char16_type_node)
4111 || same_type_p (type, char32_type_node)
4112 || same_type_p (type, wchar_type_node));
4115 /* Returns the kind of linkage associated with the indicated DECL. Th
4116 value returned is as specified by the language standard; it is
4117 independent of implementation details regarding template
4118 instantiation, etc. For example, it is possible that a declaration
4119 to which this function assigns external linkage would not show up
4120 as a global symbol when you run `nm' on the resulting object file. */
4122 linkage_kind
4123 decl_linkage (tree decl)
4125 /* This function doesn't attempt to calculate the linkage from first
4126 principles as given in [basic.link]. Instead, it makes use of
4127 the fact that we have already set TREE_PUBLIC appropriately, and
4128 then handles a few special cases. Ideally, we would calculate
4129 linkage first, and then transform that into a concrete
4130 implementation. */
4132 /* Things that don't have names have no linkage. */
4133 if (!DECL_NAME (decl))
4134 return lk_none;
4136 /* Fields have no linkage. */
4137 if (TREE_CODE (decl) == FIELD_DECL)
4138 return lk_none;
4140 /* Things that are TREE_PUBLIC have external linkage. */
4141 if (TREE_PUBLIC (decl))
4142 return lk_external;
4144 if (TREE_CODE (decl) == NAMESPACE_DECL)
4145 return lk_external;
4147 /* Linkage of a CONST_DECL depends on the linkage of the enumeration
4148 type. */
4149 if (TREE_CODE (decl) == CONST_DECL)
4150 return decl_linkage (TYPE_NAME (DECL_CONTEXT (decl)));
4152 /* Things in local scope do not have linkage, if they don't have
4153 TREE_PUBLIC set. */
4154 if (decl_function_context (decl))
4155 return lk_none;
4157 /* Members of the anonymous namespace also have TREE_PUBLIC unset, but
4158 are considered to have external linkage for language purposes, as do
4159 template instantiations on targets without weak symbols. DECLs really
4160 meant to have internal linkage have DECL_THIS_STATIC set. */
4161 if (TREE_CODE (decl) == TYPE_DECL)
4162 return lk_external;
4163 if (VAR_OR_FUNCTION_DECL_P (decl))
4165 if (!DECL_THIS_STATIC (decl))
4166 return lk_external;
4168 /* Static data members and static member functions from classes
4169 in anonymous namespace also don't have TREE_PUBLIC set. */
4170 if (DECL_CLASS_CONTEXT (decl))
4171 return lk_external;
4174 /* Everything else has internal linkage. */
4175 return lk_internal;
4178 /* Returns the storage duration of the object or reference associated with
4179 the indicated DECL, which should be a VAR_DECL or PARM_DECL. */
4181 duration_kind
4182 decl_storage_duration (tree decl)
4184 if (TREE_CODE (decl) == PARM_DECL)
4185 return dk_auto;
4186 if (TREE_CODE (decl) == FUNCTION_DECL)
4187 return dk_static;
4188 gcc_assert (VAR_P (decl));
4189 if (!TREE_STATIC (decl)
4190 && !DECL_EXTERNAL (decl))
4191 return dk_auto;
4192 if (CP_DECL_THREAD_LOCAL_P (decl))
4193 return dk_thread;
4194 return dk_static;
4197 /* EXP is an expression that we want to pre-evaluate. Returns (in
4198 *INITP) an expression that will perform the pre-evaluation. The
4199 value returned by this function is a side-effect free expression
4200 equivalent to the pre-evaluated expression. Callers must ensure
4201 that *INITP is evaluated before EXP. */
4203 tree
4204 stabilize_expr (tree exp, tree* initp)
4206 tree init_expr;
4208 if (!TREE_SIDE_EFFECTS (exp))
4209 init_expr = NULL_TREE;
4210 else if (VOID_TYPE_P (TREE_TYPE (exp)))
4212 init_expr = exp;
4213 exp = void_node;
4215 /* There are no expressions with REFERENCE_TYPE, but there can be call
4216 arguments with such a type; just treat it as a pointer. */
4217 else if (TREE_CODE (TREE_TYPE (exp)) == REFERENCE_TYPE
4218 || SCALAR_TYPE_P (TREE_TYPE (exp))
4219 || !lvalue_or_rvalue_with_address_p (exp))
4221 init_expr = get_target_expr (exp);
4222 exp = TARGET_EXPR_SLOT (init_expr);
4223 if (CLASS_TYPE_P (TREE_TYPE (exp)))
4224 exp = move (exp);
4225 else
4226 exp = rvalue (exp);
4228 else
4230 bool xval = !real_lvalue_p (exp);
4231 exp = cp_build_addr_expr (exp, tf_warning_or_error);
4232 init_expr = get_target_expr (exp);
4233 exp = TARGET_EXPR_SLOT (init_expr);
4234 exp = cp_build_indirect_ref (exp, RO_NULL, tf_warning_or_error);
4235 if (xval)
4236 exp = move (exp);
4238 *initp = init_expr;
4240 gcc_assert (!TREE_SIDE_EFFECTS (exp));
4241 return exp;
4244 /* Add NEW_EXPR, an expression whose value we don't care about, after the
4245 similar expression ORIG. */
4247 tree
4248 add_stmt_to_compound (tree orig, tree new_expr)
4250 if (!new_expr || !TREE_SIDE_EFFECTS (new_expr))
4251 return orig;
4252 if (!orig || !TREE_SIDE_EFFECTS (orig))
4253 return new_expr;
4254 return build2 (COMPOUND_EXPR, void_type_node, orig, new_expr);
4257 /* Like stabilize_expr, but for a call whose arguments we want to
4258 pre-evaluate. CALL is modified in place to use the pre-evaluated
4259 arguments, while, upon return, *INITP contains an expression to
4260 compute the arguments. */
4262 void
4263 stabilize_call (tree call, tree *initp)
4265 tree inits = NULL_TREE;
4266 int i;
4267 int nargs = call_expr_nargs (call);
4269 if (call == error_mark_node || processing_template_decl)
4271 *initp = NULL_TREE;
4272 return;
4275 gcc_assert (TREE_CODE (call) == CALL_EXPR);
4277 for (i = 0; i < nargs; i++)
4279 tree init;
4280 CALL_EXPR_ARG (call, i) =
4281 stabilize_expr (CALL_EXPR_ARG (call, i), &init);
4282 inits = add_stmt_to_compound (inits, init);
4285 *initp = inits;
4288 /* Like stabilize_expr, but for an AGGR_INIT_EXPR whose arguments we want
4289 to pre-evaluate. CALL is modified in place to use the pre-evaluated
4290 arguments, while, upon return, *INITP contains an expression to
4291 compute the arguments. */
4293 static void
4294 stabilize_aggr_init (tree call, tree *initp)
4296 tree inits = NULL_TREE;
4297 int i;
4298 int nargs = aggr_init_expr_nargs (call);
4300 if (call == error_mark_node)
4301 return;
4303 gcc_assert (TREE_CODE (call) == AGGR_INIT_EXPR);
4305 for (i = 0; i < nargs; i++)
4307 tree init;
4308 AGGR_INIT_EXPR_ARG (call, i) =
4309 stabilize_expr (AGGR_INIT_EXPR_ARG (call, i), &init);
4310 inits = add_stmt_to_compound (inits, init);
4313 *initp = inits;
4316 /* Like stabilize_expr, but for an initialization.
4318 If the initialization is for an object of class type, this function
4319 takes care not to introduce additional temporaries.
4321 Returns TRUE iff the expression was successfully pre-evaluated,
4322 i.e., if INIT is now side-effect free, except for, possibly, a
4323 single call to a constructor. */
4325 bool
4326 stabilize_init (tree init, tree *initp)
4328 tree t = init;
4330 *initp = NULL_TREE;
4332 if (t == error_mark_node || processing_template_decl)
4333 return true;
4335 if (TREE_CODE (t) == INIT_EXPR)
4336 t = TREE_OPERAND (t, 1);
4337 if (TREE_CODE (t) == TARGET_EXPR)
4338 t = TARGET_EXPR_INITIAL (t);
4340 /* If the RHS can be stabilized without breaking copy elision, stabilize
4341 it. We specifically don't stabilize class prvalues here because that
4342 would mean an extra copy, but they might be stabilized below. */
4343 if (TREE_CODE (init) == INIT_EXPR
4344 && TREE_CODE (t) != CONSTRUCTOR
4345 && TREE_CODE (t) != AGGR_INIT_EXPR
4346 && (SCALAR_TYPE_P (TREE_TYPE (t))
4347 || lvalue_or_rvalue_with_address_p (t)))
4349 TREE_OPERAND (init, 1) = stabilize_expr (t, initp);
4350 return true;
4353 if (TREE_CODE (t) == COMPOUND_EXPR
4354 && TREE_CODE (init) == INIT_EXPR)
4356 tree last = expr_last (t);
4357 /* Handle stabilizing the EMPTY_CLASS_EXPR pattern. */
4358 if (!TREE_SIDE_EFFECTS (last))
4360 *initp = t;
4361 TREE_OPERAND (init, 1) = last;
4362 return true;
4366 if (TREE_CODE (t) == CONSTRUCTOR)
4368 /* Aggregate initialization: stabilize each of the field
4369 initializers. */
4370 unsigned i;
4371 constructor_elt *ce;
4372 bool good = true;
4373 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
4374 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
4376 tree type = TREE_TYPE (ce->value);
4377 tree subinit;
4378 if (TREE_CODE (type) == REFERENCE_TYPE
4379 || SCALAR_TYPE_P (type))
4380 ce->value = stabilize_expr (ce->value, &subinit);
4381 else if (!stabilize_init (ce->value, &subinit))
4382 good = false;
4383 *initp = add_stmt_to_compound (*initp, subinit);
4385 return good;
4388 if (TREE_CODE (t) == CALL_EXPR)
4390 stabilize_call (t, initp);
4391 return true;
4394 if (TREE_CODE (t) == AGGR_INIT_EXPR)
4396 stabilize_aggr_init (t, initp);
4397 return true;
4400 /* The initialization is being performed via a bitwise copy -- and
4401 the item copied may have side effects. */
4402 return !TREE_SIDE_EFFECTS (init);
4405 /* Returns true if a cast to TYPE may appear in an integral constant
4406 expression. */
4408 bool
4409 cast_valid_in_integral_constant_expression_p (tree type)
4411 return (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4412 || cxx_dialect >= cxx11
4413 || dependent_type_p (type)
4414 || type == error_mark_node);
4417 /* Return true if we need to fix linkage information of DECL. */
4419 static bool
4420 cp_fix_function_decl_p (tree decl)
4422 /* Skip if DECL is not externally visible. */
4423 if (!TREE_PUBLIC (decl))
4424 return false;
4426 /* We need to fix DECL if it a appears to be exported but with no
4427 function body. Thunks do not have CFGs and we may need to
4428 handle them specially later. */
4429 if (!gimple_has_body_p (decl)
4430 && !DECL_THUNK_P (decl)
4431 && !DECL_EXTERNAL (decl))
4433 struct cgraph_node *node = cgraph_node::get (decl);
4435 /* Don't fix same_body aliases. Although they don't have their own
4436 CFG, they share it with what they alias to. */
4437 if (!node || !node->alias
4438 || !vec_safe_length (node->ref_list.references))
4439 return true;
4442 return false;
4445 /* Clean the C++ specific parts of the tree T. */
4447 void
4448 cp_free_lang_data (tree t)
4450 if (TREE_CODE (t) == METHOD_TYPE
4451 || TREE_CODE (t) == FUNCTION_TYPE)
4453 /* Default args are not interesting anymore. */
4454 tree argtypes = TYPE_ARG_TYPES (t);
4455 while (argtypes)
4457 TREE_PURPOSE (argtypes) = 0;
4458 argtypes = TREE_CHAIN (argtypes);
4461 else if (TREE_CODE (t) == FUNCTION_DECL
4462 && cp_fix_function_decl_p (t))
4464 /* If T is used in this translation unit at all, the definition
4465 must exist somewhere else since we have decided to not emit it
4466 in this TU. So make it an external reference. */
4467 DECL_EXTERNAL (t) = 1;
4468 TREE_STATIC (t) = 0;
4470 if (TREE_CODE (t) == NAMESPACE_DECL)
4472 /* The list of users of a namespace isn't useful for the middle-end
4473 or debug generators. */
4474 DECL_NAMESPACE_USERS (t) = NULL_TREE;
4475 /* Neither do we need the leftover chaining of namespaces
4476 from the binding level. */
4477 DECL_CHAIN (t) = NULL_TREE;
4481 /* Stub for c-common. Please keep in sync with c-decl.c.
4482 FIXME: If address space support is target specific, then this
4483 should be a C target hook. But currently this is not possible,
4484 because this function is called via REGISTER_TARGET_PRAGMAS. */
4485 void
4486 c_register_addr_space (const char * /*word*/, addr_space_t /*as*/)
4490 /* Return the number of operands in T that we care about for things like
4491 mangling. */
4494 cp_tree_operand_length (const_tree t)
4496 enum tree_code code = TREE_CODE (t);
4498 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
4499 return VL_EXP_OPERAND_LENGTH (t);
4501 return cp_tree_code_length (code);
4504 /* Like cp_tree_operand_length, but takes a tree_code CODE. */
4507 cp_tree_code_length (enum tree_code code)
4509 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4511 switch (code)
4513 case PREINCREMENT_EXPR:
4514 case PREDECREMENT_EXPR:
4515 case POSTINCREMENT_EXPR:
4516 case POSTDECREMENT_EXPR:
4517 return 1;
4519 case ARRAY_REF:
4520 return 2;
4522 case EXPR_PACK_EXPANSION:
4523 return 1;
4525 default:
4526 return TREE_CODE_LENGTH (code);
4530 /* Implement -Wzero_as_null_pointer_constant. Return true if the
4531 conditions for the warning hold, false otherwise. */
4532 bool
4533 maybe_warn_zero_as_null_pointer_constant (tree expr, location_t loc)
4535 if (c_inhibit_evaluation_warnings == 0
4536 && !NULLPTR_TYPE_P (TREE_TYPE (expr)))
4538 warning_at (loc, OPT_Wzero_as_null_pointer_constant,
4539 "zero as null pointer constant");
4540 return true;
4542 return false;
4545 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4546 /* Complain that some language-specific thing hanging off a tree
4547 node has been accessed improperly. */
4549 void
4550 lang_check_failed (const char* file, int line, const char* function)
4552 internal_error ("lang_* check: failed in %s, at %s:%d",
4553 function, trim_filename (file), line);
4555 #endif /* ENABLE_TREE_CHECKING */
4557 #include "gt-cp-tree.h"