PR tree-optimization/77975
[official-gcc.git] / gcc / cp / tree.c
blob2757af6691d5baee0bfc69d3be025ed1f8e8afed
1 /* Language-dependent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2017 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_init_priority_attribute (tree *, tree, tree, int, bool *);
46 static tree handle_abi_tag_attribute (tree *, tree, tree, int, bool *);
48 /* If REF is an lvalue, returns the kind of lvalue that REF is.
49 Otherwise, returns clk_none. */
51 cp_lvalue_kind
52 lvalue_kind (const_tree ref)
54 cp_lvalue_kind op1_lvalue_kind = clk_none;
55 cp_lvalue_kind op2_lvalue_kind = clk_none;
57 /* Expressions of reference type are sometimes wrapped in
58 INDIRECT_REFs. INDIRECT_REFs are just internal compiler
59 representation, not part of the language, so we have to look
60 through them. */
61 if (REFERENCE_REF_P (ref))
62 return lvalue_kind (TREE_OPERAND (ref, 0));
64 if (TREE_TYPE (ref)
65 && TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
67 /* unnamed rvalue references are rvalues */
68 if (TYPE_REF_IS_RVALUE (TREE_TYPE (ref))
69 && TREE_CODE (ref) != PARM_DECL
70 && !VAR_P (ref)
71 && TREE_CODE (ref) != COMPONENT_REF
72 /* Functions are always lvalues. */
73 && TREE_CODE (TREE_TYPE (TREE_TYPE (ref))) != FUNCTION_TYPE)
74 return clk_rvalueref;
76 /* lvalue references and named rvalue references are lvalues. */
77 return clk_ordinary;
80 if (ref == current_class_ptr)
81 return clk_none;
83 switch (TREE_CODE (ref))
85 case SAVE_EXPR:
86 return clk_none;
87 /* preincrements and predecrements are valid lvals, provided
88 what they refer to are valid lvals. */
89 case PREINCREMENT_EXPR:
90 case PREDECREMENT_EXPR:
91 case TRY_CATCH_EXPR:
92 case WITH_CLEANUP_EXPR:
93 case REALPART_EXPR:
94 case IMAGPART_EXPR:
95 return lvalue_kind (TREE_OPERAND (ref, 0));
97 case MEMBER_REF:
98 case DOTSTAR_EXPR:
99 if (TREE_CODE (ref) == MEMBER_REF)
100 op1_lvalue_kind = clk_ordinary;
101 else
102 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
103 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
104 op1_lvalue_kind = clk_none;
105 return op1_lvalue_kind;
107 case COMPONENT_REF:
108 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
109 /* Look at the member designator. */
110 if (!op1_lvalue_kind)
112 else if (is_overloaded_fn (TREE_OPERAND (ref, 1)))
113 /* The "field" can be a FUNCTION_DECL or an OVERLOAD in some
114 situations. If we're seeing a COMPONENT_REF, it's a non-static
115 member, so it isn't an lvalue. */
116 op1_lvalue_kind = clk_none;
117 else if (TREE_CODE (TREE_OPERAND (ref, 1)) != FIELD_DECL)
118 /* This can be IDENTIFIER_NODE in a template. */;
119 else if (DECL_C_BIT_FIELD (TREE_OPERAND (ref, 1)))
121 /* Clear the ordinary bit. If this object was a class
122 rvalue we want to preserve that information. */
123 op1_lvalue_kind &= ~clk_ordinary;
124 /* The lvalue is for a bitfield. */
125 op1_lvalue_kind |= clk_bitfield;
127 else if (DECL_PACKED (TREE_OPERAND (ref, 1)))
128 op1_lvalue_kind |= clk_packed;
130 return op1_lvalue_kind;
132 case STRING_CST:
133 case COMPOUND_LITERAL_EXPR:
134 return clk_ordinary;
136 case CONST_DECL:
137 /* CONST_DECL without TREE_STATIC are enumeration values and
138 thus not lvalues. With TREE_STATIC they are used by ObjC++
139 in objc_build_string_object and need to be considered as
140 lvalues. */
141 if (! TREE_STATIC (ref))
142 return clk_none;
143 /* FALLTHRU */
144 case VAR_DECL:
145 if (VAR_P (ref) && DECL_HAS_VALUE_EXPR_P (ref))
146 return lvalue_kind (DECL_VALUE_EXPR (CONST_CAST_TREE (ref)));
148 if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
149 && DECL_LANG_SPECIFIC (ref)
150 && DECL_IN_AGGR_P (ref))
151 return clk_none;
152 /* FALLTHRU */
153 case INDIRECT_REF:
154 case ARROW_EXPR:
155 case ARRAY_REF:
156 case ARRAY_NOTATION_REF:
157 case PARM_DECL:
158 case RESULT_DECL:
159 case PLACEHOLDER_EXPR:
160 return clk_ordinary;
162 /* A scope ref in a template, left as SCOPE_REF to support later
163 access checking. */
164 case SCOPE_REF:
165 gcc_assert (!type_dependent_expression_p (CONST_CAST_TREE (ref)));
167 tree op = TREE_OPERAND (ref, 1);
168 if (TREE_CODE (op) == FIELD_DECL)
169 return (DECL_C_BIT_FIELD (op) ? clk_bitfield : clk_ordinary);
170 else
171 return lvalue_kind (op);
174 case MAX_EXPR:
175 case MIN_EXPR:
176 /* Disallow <? and >? as lvalues if either argument side-effects. */
177 if (TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 0))
178 || TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 1)))
179 return clk_none;
180 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
181 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1));
182 break;
184 case COND_EXPR:
185 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1)
186 ? TREE_OPERAND (ref, 1)
187 : TREE_OPERAND (ref, 0));
188 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 2));
189 break;
191 case MODOP_EXPR:
192 /* We expect to see unlowered MODOP_EXPRs only during
193 template processing. */
194 gcc_assert (processing_template_decl);
195 return clk_ordinary;
197 case MODIFY_EXPR:
198 case TYPEID_EXPR:
199 return clk_ordinary;
201 case COMPOUND_EXPR:
202 return lvalue_kind (TREE_OPERAND (ref, 1));
204 case TARGET_EXPR:
205 return clk_class;
207 case VA_ARG_EXPR:
208 return (CLASS_TYPE_P (TREE_TYPE (ref)) ? clk_class : clk_none);
210 case CALL_EXPR:
211 /* We can see calls outside of TARGET_EXPR in templates. */
212 if (CLASS_TYPE_P (TREE_TYPE (ref)))
213 return clk_class;
214 return clk_none;
216 case FUNCTION_DECL:
217 /* All functions (except non-static-member functions) are
218 lvalues. */
219 return (DECL_NONSTATIC_MEMBER_FUNCTION_P (ref)
220 ? clk_none : clk_ordinary);
222 case BASELINK:
223 /* We now represent a reference to a single static member function
224 with a BASELINK. */
225 /* This CONST_CAST is okay because BASELINK_FUNCTIONS returns
226 its argument unmodified and we assign it to a const_tree. */
227 return lvalue_kind (BASELINK_FUNCTIONS (CONST_CAST_TREE (ref)));
229 case NON_DEPENDENT_EXPR:
230 return lvalue_kind (TREE_OPERAND (ref, 0));
232 default:
233 if (!TREE_TYPE (ref))
234 return clk_none;
235 if (CLASS_TYPE_P (TREE_TYPE (ref)))
236 return clk_class;
237 break;
240 /* If one operand is not an lvalue at all, then this expression is
241 not an lvalue. */
242 if (!op1_lvalue_kind || !op2_lvalue_kind)
243 return clk_none;
245 /* Otherwise, it's an lvalue, and it has all the odd properties
246 contributed by either operand. */
247 op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind;
248 /* It's not an ordinary lvalue if it involves any other kind. */
249 if ((op1_lvalue_kind & ~clk_ordinary) != clk_none)
250 op1_lvalue_kind &= ~clk_ordinary;
251 /* It can't be both a pseudo-lvalue and a non-addressable lvalue.
252 A COND_EXPR of those should be wrapped in a TARGET_EXPR. */
253 if ((op1_lvalue_kind & (clk_rvalueref|clk_class))
254 && (op1_lvalue_kind & (clk_bitfield|clk_packed)))
255 op1_lvalue_kind = clk_none;
256 return op1_lvalue_kind;
259 /* Returns the kind of lvalue that REF is, in the sense of [basic.lval]. */
261 cp_lvalue_kind
262 real_lvalue_p (const_tree ref)
264 cp_lvalue_kind kind = lvalue_kind (ref);
265 if (kind & (clk_rvalueref|clk_class))
266 return clk_none;
267 else
268 return kind;
271 /* c-common wants us to return bool. */
273 bool
274 lvalue_p (const_tree t)
276 return real_lvalue_p (t);
279 /* This differs from lvalue_p in that xvalues are included. */
281 bool
282 glvalue_p (const_tree ref)
284 cp_lvalue_kind kind = lvalue_kind (ref);
285 if (kind & clk_class)
286 return false;
287 else
288 return (kind != clk_none);
291 /* This differs from glvalue_p in that class prvalues are included. */
293 bool
294 obvalue_p (const_tree ref)
296 return (lvalue_kind (ref) != clk_none);
299 /* Returns true if REF is an xvalue (the result of dereferencing an rvalue
300 reference), false otherwise. */
302 bool
303 xvalue_p (const_tree ref)
305 return (lvalue_kind (ref) == clk_rvalueref);
308 /* True if REF is a bit-field. */
310 bool
311 bitfield_p (const_tree ref)
313 return (lvalue_kind (ref) & clk_bitfield);
316 /* C++-specific version of stabilize_reference. */
318 tree
319 cp_stabilize_reference (tree ref)
321 switch (TREE_CODE (ref))
323 /* We need to treat specially anything stabilize_reference doesn't
324 handle specifically. */
325 case VAR_DECL:
326 case PARM_DECL:
327 case RESULT_DECL:
328 CASE_CONVERT:
329 case FLOAT_EXPR:
330 case FIX_TRUNC_EXPR:
331 case INDIRECT_REF:
332 case COMPONENT_REF:
333 case BIT_FIELD_REF:
334 case ARRAY_REF:
335 case ARRAY_RANGE_REF:
336 case ERROR_MARK:
337 break;
338 default:
339 cp_lvalue_kind kind = lvalue_kind (ref);
340 if ((kind & ~clk_class) != clk_none)
342 tree type = unlowered_expr_type (ref);
343 bool rval = !!(kind & clk_rvalueref);
344 type = cp_build_reference_type (type, rval);
345 /* This inhibits warnings in, eg, cxx_mark_addressable
346 (c++/60955). */
347 warning_sentinel s (extra_warnings);
348 ref = build_static_cast (type, ref, tf_error);
352 return stabilize_reference (ref);
355 /* Test whether DECL is a builtin that may appear in a
356 constant-expression. */
358 bool
359 builtin_valid_in_constant_expr_p (const_tree decl)
361 if (!(TREE_CODE (decl) == FUNCTION_DECL
362 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL))
363 /* Not a built-in. */
364 return false;
365 switch (DECL_FUNCTION_CODE (decl))
367 /* These always have constant results like the corresponding
368 macros/symbol. */
369 case BUILT_IN_FILE:
370 case BUILT_IN_FUNCTION:
371 case BUILT_IN_LINE:
373 /* The following built-ins are valid in constant expressions
374 when their arguments are. */
375 case BUILT_IN_ADD_OVERFLOW_P:
376 case BUILT_IN_SUB_OVERFLOW_P:
377 case BUILT_IN_MUL_OVERFLOW_P:
379 /* These have constant results even if their operands are
380 non-constant. */
381 case BUILT_IN_CONSTANT_P:
382 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
383 return true;
384 default:
385 return false;
389 /* Build a TARGET_EXPR, initializing the DECL with the VALUE. */
391 static tree
392 build_target_expr (tree decl, tree value, tsubst_flags_t complain)
394 tree t;
395 tree type = TREE_TYPE (decl);
397 value = mark_rvalue_use (value);
399 gcc_checking_assert (VOID_TYPE_P (TREE_TYPE (value))
400 || TREE_TYPE (decl) == TREE_TYPE (value)
401 /* On ARM ctors return 'this'. */
402 || (TYPE_PTR_P (TREE_TYPE (value))
403 && TREE_CODE (value) == CALL_EXPR)
404 || useless_type_conversion_p (TREE_TYPE (decl),
405 TREE_TYPE (value)));
407 if (complain & tf_no_cleanup)
408 /* The caller is building a new-expr and does not need a cleanup. */
409 t = NULL_TREE;
410 else
412 t = cxx_maybe_build_cleanup (decl, complain);
413 if (t == error_mark_node)
414 return error_mark_node;
416 t = build4 (TARGET_EXPR, type, decl, value, t, NULL_TREE);
417 if (EXPR_HAS_LOCATION (value))
418 SET_EXPR_LOCATION (t, EXPR_LOCATION (value));
419 /* We always set TREE_SIDE_EFFECTS so that expand_expr does not
420 ignore the TARGET_EXPR. If there really turn out to be no
421 side-effects, then the optimizer should be able to get rid of
422 whatever code is generated anyhow. */
423 TREE_SIDE_EFFECTS (t) = 1;
425 return t;
428 /* Return an undeclared local temporary of type TYPE for use in building a
429 TARGET_EXPR. */
431 static tree
432 build_local_temp (tree type)
434 tree slot = build_decl (input_location,
435 VAR_DECL, NULL_TREE, type);
436 DECL_ARTIFICIAL (slot) = 1;
437 DECL_IGNORED_P (slot) = 1;
438 DECL_CONTEXT (slot) = current_function_decl;
439 layout_decl (slot, 0);
440 return slot;
443 /* Set various status flags when building an AGGR_INIT_EXPR object T. */
445 static void
446 process_aggr_init_operands (tree t)
448 bool side_effects;
450 side_effects = TREE_SIDE_EFFECTS (t);
451 if (!side_effects)
453 int i, n;
454 n = TREE_OPERAND_LENGTH (t);
455 for (i = 1; i < n; i++)
457 tree op = TREE_OPERAND (t, i);
458 if (op && TREE_SIDE_EFFECTS (op))
460 side_effects = 1;
461 break;
465 TREE_SIDE_EFFECTS (t) = side_effects;
468 /* Build an AGGR_INIT_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE,
469 FN, and SLOT. NARGS is the number of call arguments which are specified
470 as a tree array ARGS. */
472 static tree
473 build_aggr_init_array (tree return_type, tree fn, tree slot, int nargs,
474 tree *args)
476 tree t;
477 int i;
479 t = build_vl_exp (AGGR_INIT_EXPR, nargs + 3);
480 TREE_TYPE (t) = return_type;
481 AGGR_INIT_EXPR_FN (t) = fn;
482 AGGR_INIT_EXPR_SLOT (t) = slot;
483 for (i = 0; i < nargs; i++)
484 AGGR_INIT_EXPR_ARG (t, i) = args[i];
485 process_aggr_init_operands (t);
486 return t;
489 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
490 target. TYPE is the type to be initialized.
492 Build an AGGR_INIT_EXPR to represent the initialization. This function
493 differs from build_cplus_new in that an AGGR_INIT_EXPR can only be used
494 to initialize another object, whereas a TARGET_EXPR can either
495 initialize another object or create its own temporary object, and as a
496 result building up a TARGET_EXPR requires that the type's destructor be
497 callable. */
499 tree
500 build_aggr_init_expr (tree type, tree init)
502 tree fn;
503 tree slot;
504 tree rval;
505 int is_ctor;
507 /* Don't build AGGR_INIT_EXPR in a template. */
508 if (processing_template_decl)
509 return init;
511 fn = cp_get_callee (init);
512 if (fn == NULL_TREE)
513 return convert (type, init);
515 is_ctor = (TREE_CODE (fn) == ADDR_EXPR
516 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
517 && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0)));
519 /* We split the CALL_EXPR into its function and its arguments here.
520 Then, in expand_expr, we put them back together. The reason for
521 this is that this expression might be a default argument
522 expression. In that case, we need a new temporary every time the
523 expression is used. That's what break_out_target_exprs does; it
524 replaces every AGGR_INIT_EXPR with a copy that uses a fresh
525 temporary slot. Then, expand_expr builds up a call-expression
526 using the new slot. */
528 /* If we don't need to use a constructor to create an object of this
529 type, don't mess with AGGR_INIT_EXPR. */
530 if (is_ctor || TREE_ADDRESSABLE (type))
532 slot = build_local_temp (type);
534 if (TREE_CODE (init) == CALL_EXPR)
536 rval = build_aggr_init_array (void_type_node, fn, slot,
537 call_expr_nargs (init),
538 CALL_EXPR_ARGP (init));
539 AGGR_INIT_FROM_THUNK_P (rval)
540 = CALL_FROM_THUNK_P (init);
542 else
544 rval = build_aggr_init_array (void_type_node, fn, slot,
545 aggr_init_expr_nargs (init),
546 AGGR_INIT_EXPR_ARGP (init));
547 AGGR_INIT_FROM_THUNK_P (rval)
548 = AGGR_INIT_FROM_THUNK_P (init);
550 TREE_SIDE_EFFECTS (rval) = 1;
551 AGGR_INIT_VIA_CTOR_P (rval) = is_ctor;
552 TREE_NOTHROW (rval) = TREE_NOTHROW (init);
553 CALL_EXPR_OPERATOR_SYNTAX (rval) = CALL_EXPR_OPERATOR_SYNTAX (init);
554 CALL_EXPR_ORDERED_ARGS (rval) = CALL_EXPR_ORDERED_ARGS (init);
555 CALL_EXPR_REVERSE_ARGS (rval) = CALL_EXPR_REVERSE_ARGS (init);
557 else
558 rval = init;
560 return rval;
563 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
564 target. TYPE is the type that this initialization should appear to
565 have.
567 Build an encapsulation of the initialization to perform
568 and return it so that it can be processed by language-independent
569 and language-specific expression expanders. */
571 tree
572 build_cplus_new (tree type, tree init, tsubst_flags_t complain)
574 tree rval = build_aggr_init_expr (type, init);
575 tree slot;
577 if (!complete_type_or_maybe_complain (type, init, complain))
578 return error_mark_node;
580 /* Make sure that we're not trying to create an instance of an
581 abstract class. */
582 if (abstract_virtuals_error_sfinae (NULL_TREE, type, complain))
583 return error_mark_node;
585 if (TREE_CODE (rval) == AGGR_INIT_EXPR)
586 slot = AGGR_INIT_EXPR_SLOT (rval);
587 else if (TREE_CODE (rval) == CALL_EXPR
588 || TREE_CODE (rval) == CONSTRUCTOR)
589 slot = build_local_temp (type);
590 else
591 return rval;
593 rval = build_target_expr (slot, rval, complain);
595 if (rval != error_mark_node)
596 TARGET_EXPR_IMPLICIT_P (rval) = 1;
598 return rval;
601 /* Subroutine of build_vec_init_expr: Build up a single element
602 intialization as a proxy for the full array initialization to get things
603 marked as used and any appropriate diagnostics.
605 Since we're deferring building the actual constructor calls until
606 gimplification time, we need to build one now and throw it away so
607 that the relevant constructor gets mark_used before cgraph decides
608 what functions are needed. Here we assume that init is either
609 NULL_TREE, void_type_node (indicating value-initialization), or
610 another array to copy. */
612 static tree
613 build_vec_init_elt (tree type, tree init, tsubst_flags_t complain)
615 tree inner_type = strip_array_types (type);
616 vec<tree, va_gc> *argvec;
618 if (integer_zerop (array_type_nelts_total (type))
619 || !CLASS_TYPE_P (inner_type))
620 /* No interesting initialization to do. */
621 return integer_zero_node;
622 else if (init == void_type_node)
623 return build_value_init (inner_type, complain);
625 gcc_assert (init == NULL_TREE
626 || (same_type_ignoring_top_level_qualifiers_p
627 (type, TREE_TYPE (init))));
629 argvec = make_tree_vector ();
630 if (init)
632 tree init_type = strip_array_types (TREE_TYPE (init));
633 tree dummy = build_dummy_object (init_type);
634 if (!lvalue_p (init))
635 dummy = move (dummy);
636 argvec->quick_push (dummy);
638 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
639 &argvec, inner_type, LOOKUP_NORMAL,
640 complain);
641 release_tree_vector (argvec);
643 /* For a trivial constructor, build_over_call creates a TARGET_EXPR. But
644 we don't want one here because we aren't creating a temporary. */
645 if (TREE_CODE (init) == TARGET_EXPR)
646 init = TARGET_EXPR_INITIAL (init);
648 return init;
651 /* Return a TARGET_EXPR which expresses the initialization of an array to
652 be named later, either default-initialization or copy-initialization
653 from another array of the same type. */
655 tree
656 build_vec_init_expr (tree type, tree init, tsubst_flags_t complain)
658 tree slot;
659 bool value_init = false;
660 tree elt_init = build_vec_init_elt (type, init, complain);
662 if (init == void_type_node)
664 value_init = true;
665 init = NULL_TREE;
668 slot = build_local_temp (type);
669 init = build2 (VEC_INIT_EXPR, type, slot, init);
670 TREE_SIDE_EFFECTS (init) = true;
671 SET_EXPR_LOCATION (init, input_location);
673 if (cxx_dialect >= cxx11
674 && potential_constant_expression (elt_init))
675 VEC_INIT_EXPR_IS_CONSTEXPR (init) = true;
676 VEC_INIT_EXPR_VALUE_INIT (init) = value_init;
678 return init;
681 /* Give a helpful diagnostic for a non-constexpr VEC_INIT_EXPR in a context
682 that requires a constant expression. */
684 void
685 diagnose_non_constexpr_vec_init (tree expr)
687 tree type = TREE_TYPE (VEC_INIT_EXPR_SLOT (expr));
688 tree init, elt_init;
689 if (VEC_INIT_EXPR_VALUE_INIT (expr))
690 init = void_type_node;
691 else
692 init = VEC_INIT_EXPR_INIT (expr);
694 elt_init = build_vec_init_elt (type, init, tf_warning_or_error);
695 require_potential_constant_expression (elt_init);
698 tree
699 build_array_copy (tree init)
701 return build_vec_init_expr (TREE_TYPE (init), init, tf_warning_or_error);
704 /* Build a TARGET_EXPR using INIT to initialize a new temporary of the
705 indicated TYPE. */
707 tree
708 build_target_expr_with_type (tree init, tree type, tsubst_flags_t complain)
710 gcc_assert (!VOID_TYPE_P (type));
712 if (TREE_CODE (init) == TARGET_EXPR
713 || init == error_mark_node)
714 return init;
715 else if (CLASS_TYPE_P (type) && type_has_nontrivial_copy_init (type)
716 && !VOID_TYPE_P (TREE_TYPE (init))
717 && TREE_CODE (init) != COND_EXPR
718 && TREE_CODE (init) != CONSTRUCTOR
719 && TREE_CODE (init) != VA_ARG_EXPR)
720 /* We need to build up a copy constructor call. A void initializer
721 means we're being called from bot_manip. COND_EXPR is a special
722 case because we already have copies on the arms and we don't want
723 another one here. A CONSTRUCTOR is aggregate initialization, which
724 is handled separately. A VA_ARG_EXPR is magic creation of an
725 aggregate; there's no additional work to be done. */
726 return force_rvalue (init, complain);
728 return force_target_expr (type, init, complain);
731 /* Like the above function, but without the checking. This function should
732 only be used by code which is deliberately trying to subvert the type
733 system, such as call_builtin_trap. Or build_over_call, to avoid
734 infinite recursion. */
736 tree
737 force_target_expr (tree type, tree init, tsubst_flags_t complain)
739 tree slot;
741 gcc_assert (!VOID_TYPE_P (type));
743 slot = build_local_temp (type);
744 return build_target_expr (slot, init, complain);
747 /* Like build_target_expr_with_type, but use the type of INIT. */
749 tree
750 get_target_expr_sfinae (tree init, tsubst_flags_t complain)
752 if (TREE_CODE (init) == AGGR_INIT_EXPR)
753 return build_target_expr (AGGR_INIT_EXPR_SLOT (init), init, complain);
754 else if (TREE_CODE (init) == VEC_INIT_EXPR)
755 return build_target_expr (VEC_INIT_EXPR_SLOT (init), init, complain);
756 else
758 init = convert_bitfield_to_declared_type (init);
759 return build_target_expr_with_type (init, TREE_TYPE (init), complain);
763 tree
764 get_target_expr (tree init)
766 return get_target_expr_sfinae (init, tf_warning_or_error);
769 /* If EXPR is a bitfield reference, convert it to the declared type of
770 the bitfield, and return the resulting expression. Otherwise,
771 return EXPR itself. */
773 tree
774 convert_bitfield_to_declared_type (tree expr)
776 tree bitfield_type;
778 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
779 if (bitfield_type)
780 expr = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type),
781 expr);
782 return expr;
785 /* EXPR is being used in an rvalue context. Return a version of EXPR
786 that is marked as an rvalue. */
788 tree
789 rvalue (tree expr)
791 tree type;
793 if (error_operand_p (expr))
794 return expr;
796 expr = mark_rvalue_use (expr);
798 /* [basic.lval]
800 Non-class rvalues always have cv-unqualified types. */
801 type = TREE_TYPE (expr);
802 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
803 type = cv_unqualified (type);
805 /* We need to do this for rvalue refs as well to get the right answer
806 from decltype; see c++/36628. */
807 if (!processing_template_decl && glvalue_p (expr))
808 expr = build1 (NON_LVALUE_EXPR, type, expr);
809 else if (type != TREE_TYPE (expr))
810 expr = build_nop (type, expr);
812 return expr;
816 struct cplus_array_info
818 tree type;
819 tree domain;
822 struct cplus_array_hasher : ggc_ptr_hash<tree_node>
824 typedef cplus_array_info *compare_type;
826 static hashval_t hash (tree t);
827 static bool equal (tree, cplus_array_info *);
830 /* Hash an ARRAY_TYPE. K is really of type `tree'. */
832 hashval_t
833 cplus_array_hasher::hash (tree t)
835 hashval_t hash;
837 hash = TYPE_UID (TREE_TYPE (t));
838 if (TYPE_DOMAIN (t))
839 hash ^= TYPE_UID (TYPE_DOMAIN (t));
840 return hash;
843 /* Compare two ARRAY_TYPEs. K1 is really of type `tree', K2 is really
844 of type `cplus_array_info*'. */
846 bool
847 cplus_array_hasher::equal (tree t1, cplus_array_info *t2)
849 return (TREE_TYPE (t1) == t2->type && TYPE_DOMAIN (t1) == t2->domain);
852 /* Hash table containing dependent array types, which are unsuitable for
853 the language-independent type hash table. */
854 static GTY (()) hash_table<cplus_array_hasher> *cplus_array_htab;
856 /* Build an ARRAY_TYPE without laying it out. */
858 static tree
859 build_min_array_type (tree elt_type, tree index_type)
861 tree t = cxx_make_type (ARRAY_TYPE);
862 TREE_TYPE (t) = elt_type;
863 TYPE_DOMAIN (t) = index_type;
864 return t;
867 /* Set TYPE_CANONICAL like build_array_type_1, but using
868 build_cplus_array_type. */
870 static void
871 set_array_type_canon (tree t, tree elt_type, tree index_type)
873 /* Set the canonical type for this new node. */
874 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
875 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
876 SET_TYPE_STRUCTURAL_EQUALITY (t);
877 else if (TYPE_CANONICAL (elt_type) != elt_type
878 || (index_type && TYPE_CANONICAL (index_type) != index_type))
879 TYPE_CANONICAL (t)
880 = build_cplus_array_type (TYPE_CANONICAL (elt_type),
881 index_type
882 ? TYPE_CANONICAL (index_type) : index_type);
883 else
884 TYPE_CANONICAL (t) = t;
887 /* Like build_array_type, but handle special C++ semantics: an array of a
888 variant element type is a variant of the array of the main variant of
889 the element type. */
891 tree
892 build_cplus_array_type (tree elt_type, tree index_type)
894 tree t;
896 if (elt_type == error_mark_node || index_type == error_mark_node)
897 return error_mark_node;
899 bool dependent = (uses_template_parms (elt_type)
900 || (index_type && uses_template_parms (index_type)));
902 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
903 /* Start with an array of the TYPE_MAIN_VARIANT. */
904 t = build_cplus_array_type (TYPE_MAIN_VARIANT (elt_type),
905 index_type);
906 else if (dependent)
908 /* Since type_hash_canon calls layout_type, we need to use our own
909 hash table. */
910 cplus_array_info cai;
911 hashval_t hash;
913 if (cplus_array_htab == NULL)
914 cplus_array_htab = hash_table<cplus_array_hasher>::create_ggc (61);
916 hash = TYPE_UID (elt_type);
917 if (index_type)
918 hash ^= TYPE_UID (index_type);
919 cai.type = elt_type;
920 cai.domain = index_type;
922 tree *e = cplus_array_htab->find_slot_with_hash (&cai, hash, INSERT);
923 if (*e)
924 /* We have found the type: we're done. */
925 return (tree) *e;
926 else
928 /* Build a new array type. */
929 t = build_min_array_type (elt_type, index_type);
931 /* Store it in the hash table. */
932 *e = t;
934 /* Set the canonical type for this new node. */
935 set_array_type_canon (t, elt_type, index_type);
938 else
940 t = build_array_type (elt_type, index_type);
943 /* Now check whether we already have this array variant. */
944 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
946 tree m = t;
947 for (t = m; t; t = TYPE_NEXT_VARIANT (t))
948 if (TREE_TYPE (t) == elt_type
949 && TYPE_NAME (t) == NULL_TREE
950 && TYPE_ATTRIBUTES (t) == NULL_TREE)
951 break;
952 if (!t)
954 t = build_min_array_type (elt_type, index_type);
955 set_array_type_canon (t, elt_type, index_type);
956 if (!dependent)
958 layout_type (t);
959 /* Make sure sizes are shared with the main variant.
960 layout_type can't be called after setting TYPE_NEXT_VARIANT,
961 as it will overwrite alignment etc. of all variants. */
962 TYPE_SIZE (t) = TYPE_SIZE (m);
963 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (m);
966 TYPE_MAIN_VARIANT (t) = m;
967 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
968 TYPE_NEXT_VARIANT (m) = t;
972 /* Avoid spurious warnings with VLAs (c++/54583). */
973 if (TYPE_SIZE (t) && EXPR_P (TYPE_SIZE (t)))
974 TREE_NO_WARNING (TYPE_SIZE (t)) = 1;
976 /* Push these needs up to the ARRAY_TYPE so that initialization takes
977 place more easily. */
978 bool needs_ctor = (TYPE_NEEDS_CONSTRUCTING (t)
979 = TYPE_NEEDS_CONSTRUCTING (elt_type));
980 bool needs_dtor = (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
981 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type));
983 if (!dependent && t == TYPE_MAIN_VARIANT (t)
984 && !COMPLETE_TYPE_P (t) && COMPLETE_TYPE_P (elt_type))
986 /* The element type has been completed since the last time we saw
987 this array type; update the layout and 'tor flags for any variants
988 that need it. */
989 layout_type (t);
990 for (tree v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
992 TYPE_NEEDS_CONSTRUCTING (v) = needs_ctor;
993 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (v) = needs_dtor;
997 return t;
1000 /* Return an ARRAY_TYPE with element type ELT and length N. */
1002 tree
1003 build_array_of_n_type (tree elt, int n)
1005 return build_cplus_array_type (elt, build_index_type (size_int (n - 1)));
1008 /* True iff T is an N3639 array of runtime bound (VLA). These were
1009 approved for C++14 but then removed. */
1011 bool
1012 array_of_runtime_bound_p (tree t)
1014 if (!t || TREE_CODE (t) != ARRAY_TYPE)
1015 return false;
1016 tree dom = TYPE_DOMAIN (t);
1017 if (!dom)
1018 return false;
1019 tree max = TYPE_MAX_VALUE (dom);
1020 return (!potential_rvalue_constant_expression (max)
1021 || (!value_dependent_expression_p (max) && !TREE_CONSTANT (max)));
1024 /* Return a reference type node referring to TO_TYPE. If RVAL is
1025 true, return an rvalue reference type, otherwise return an lvalue
1026 reference type. If a type node exists, reuse it, otherwise create
1027 a new one. */
1028 tree
1029 cp_build_reference_type (tree to_type, bool rval)
1031 tree lvalue_ref, t;
1033 if (TREE_CODE (to_type) == REFERENCE_TYPE)
1035 rval = rval && TYPE_REF_IS_RVALUE (to_type);
1036 to_type = TREE_TYPE (to_type);
1039 lvalue_ref = build_reference_type (to_type);
1040 if (!rval)
1041 return lvalue_ref;
1043 /* This code to create rvalue reference types is based on and tied
1044 to the code creating lvalue reference types in the middle-end
1045 functions build_reference_type_for_mode and build_reference_type.
1047 It works by putting the rvalue reference type nodes after the
1048 lvalue reference nodes in the TYPE_NEXT_REF_TO linked list, so
1049 they will effectively be ignored by the middle end. */
1051 for (t = lvalue_ref; (t = TYPE_NEXT_REF_TO (t)); )
1052 if (TYPE_REF_IS_RVALUE (t))
1053 return t;
1055 t = build_distinct_type_copy (lvalue_ref);
1057 TYPE_REF_IS_RVALUE (t) = true;
1058 TYPE_NEXT_REF_TO (t) = TYPE_NEXT_REF_TO (lvalue_ref);
1059 TYPE_NEXT_REF_TO (lvalue_ref) = t;
1061 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
1062 SET_TYPE_STRUCTURAL_EQUALITY (t);
1063 else if (TYPE_CANONICAL (to_type) != to_type)
1064 TYPE_CANONICAL (t)
1065 = cp_build_reference_type (TYPE_CANONICAL (to_type), rval);
1066 else
1067 TYPE_CANONICAL (t) = t;
1069 layout_type (t);
1071 return t;
1075 /* Returns EXPR cast to rvalue reference type, like std::move. */
1077 tree
1078 move (tree expr)
1080 tree type = TREE_TYPE (expr);
1081 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
1082 type = cp_build_reference_type (type, /*rval*/true);
1083 return build_static_cast (type, expr, tf_warning_or_error);
1086 /* Used by the C++ front end to build qualified array types. However,
1087 the C version of this function does not properly maintain canonical
1088 types (which are not used in C). */
1089 tree
1090 c_build_qualified_type (tree type, int type_quals, tree /* orig_qual_type */,
1091 size_t /* orig_qual_indirect */)
1093 return cp_build_qualified_type (type, type_quals);
1097 /* Make a variant of TYPE, qualified with the TYPE_QUALS. Handles
1098 arrays correctly. In particular, if TYPE is an array of T's, and
1099 TYPE_QUALS is non-empty, returns an array of qualified T's.
1101 FLAGS determines how to deal with ill-formed qualifications. If
1102 tf_ignore_bad_quals is set, then bad qualifications are dropped
1103 (this is permitted if TYPE was introduced via a typedef or template
1104 type parameter). If bad qualifications are dropped and tf_warning
1105 is set, then a warning is issued for non-const qualifications. If
1106 tf_ignore_bad_quals is not set and tf_error is not set, we
1107 return error_mark_node. Otherwise, we issue an error, and ignore
1108 the qualifications.
1110 Qualification of a reference type is valid when the reference came
1111 via a typedef or template type argument. [dcl.ref] No such
1112 dispensation is provided for qualifying a function type. [dcl.fct]
1113 DR 295 queries this and the proposed resolution brings it into line
1114 with qualifying a reference. We implement the DR. We also behave
1115 in a similar manner for restricting non-pointer types. */
1117 tree
1118 cp_build_qualified_type_real (tree type,
1119 int type_quals,
1120 tsubst_flags_t complain)
1122 tree result;
1123 int bad_quals = TYPE_UNQUALIFIED;
1125 if (type == error_mark_node)
1126 return type;
1128 if (type_quals == cp_type_quals (type))
1129 return type;
1131 if (TREE_CODE (type) == ARRAY_TYPE)
1133 /* In C++, the qualification really applies to the array element
1134 type. Obtain the appropriately qualified element type. */
1135 tree t;
1136 tree element_type
1137 = cp_build_qualified_type_real (TREE_TYPE (type),
1138 type_quals,
1139 complain);
1141 if (element_type == error_mark_node)
1142 return error_mark_node;
1144 /* See if we already have an identically qualified type. Tests
1145 should be equivalent to those in check_qualified_type. */
1146 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1147 if (TREE_TYPE (t) == element_type
1148 && TYPE_NAME (t) == TYPE_NAME (type)
1149 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
1150 && attribute_list_equal (TYPE_ATTRIBUTES (t),
1151 TYPE_ATTRIBUTES (type)))
1152 break;
1154 if (!t)
1156 t = build_cplus_array_type (element_type, TYPE_DOMAIN (type));
1158 /* Keep the typedef name. */
1159 if (TYPE_NAME (t) != TYPE_NAME (type))
1161 t = build_variant_type_copy (t);
1162 TYPE_NAME (t) = TYPE_NAME (type);
1163 SET_TYPE_ALIGN (t, TYPE_ALIGN (type));
1164 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (type);
1168 /* Even if we already had this variant, we update
1169 TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case
1170 they changed since the variant was originally created.
1172 This seems hokey; if there is some way to use a previous
1173 variant *without* coming through here,
1174 TYPE_NEEDS_CONSTRUCTING will never be updated. */
1175 TYPE_NEEDS_CONSTRUCTING (t)
1176 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
1177 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1178 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
1179 return t;
1181 else if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
1183 tree t = PACK_EXPANSION_PATTERN (type);
1185 t = cp_build_qualified_type_real (t, type_quals, complain);
1186 return make_pack_expansion (t);
1189 /* A reference or method type shall not be cv-qualified.
1190 [dcl.ref], [dcl.fct]. This used to be an error, but as of DR 295
1191 (in CD1) we always ignore extra cv-quals on functions. */
1192 if (type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)
1193 && (TREE_CODE (type) == REFERENCE_TYPE
1194 || TREE_CODE (type) == FUNCTION_TYPE
1195 || TREE_CODE (type) == METHOD_TYPE))
1197 if (TREE_CODE (type) == REFERENCE_TYPE)
1198 bad_quals |= type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1199 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1202 /* But preserve any function-cv-quals on a FUNCTION_TYPE. */
1203 if (TREE_CODE (type) == FUNCTION_TYPE)
1204 type_quals |= type_memfn_quals (type);
1206 /* A restrict-qualified type must be a pointer (or reference)
1207 to object or incomplete type. */
1208 if ((type_quals & TYPE_QUAL_RESTRICT)
1209 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1210 && TREE_CODE (type) != TYPENAME_TYPE
1211 && !POINTER_TYPE_P (type))
1213 bad_quals |= TYPE_QUAL_RESTRICT;
1214 type_quals &= ~TYPE_QUAL_RESTRICT;
1217 if (bad_quals == TYPE_UNQUALIFIED
1218 || (complain & tf_ignore_bad_quals))
1219 /*OK*/;
1220 else if (!(complain & tf_error))
1221 return error_mark_node;
1222 else
1224 tree bad_type = build_qualified_type (ptr_type_node, bad_quals);
1225 error ("%qV qualifiers cannot be applied to %qT",
1226 bad_type, type);
1229 /* Retrieve (or create) the appropriately qualified variant. */
1230 result = build_qualified_type (type, type_quals);
1232 /* Preserve exception specs and ref-qualifier since build_qualified_type
1233 doesn't know about them. */
1234 if (TREE_CODE (result) == FUNCTION_TYPE
1235 || TREE_CODE (result) == METHOD_TYPE)
1237 result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type));
1238 result = build_ref_qualified_type (result, type_memfn_rqual (type));
1241 return result;
1244 /* Return TYPE with const and volatile removed. */
1246 tree
1247 cv_unqualified (tree type)
1249 int quals;
1251 if (type == error_mark_node)
1252 return type;
1254 quals = cp_type_quals (type);
1255 quals &= ~(TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE);
1256 return cp_build_qualified_type (type, quals);
1259 /* Subroutine of strip_typedefs. We want to apply to RESULT the attributes
1260 from ATTRIBS that affect type identity, and no others. If any are not
1261 applied, set *remove_attributes to true. */
1263 static tree
1264 apply_identity_attributes (tree result, tree attribs, bool *remove_attributes)
1266 tree first_ident = NULL_TREE;
1267 tree new_attribs = NULL_TREE;
1268 tree *p = &new_attribs;
1270 if (OVERLOAD_TYPE_P (result))
1272 /* On classes and enums all attributes are ingrained. */
1273 gcc_assert (attribs == TYPE_ATTRIBUTES (result));
1274 return result;
1277 for (tree a = attribs; a; a = TREE_CHAIN (a))
1279 const attribute_spec *as
1280 = lookup_attribute_spec (get_attribute_name (a));
1281 if (as && as->affects_type_identity)
1283 if (!first_ident)
1284 first_ident = a;
1285 else if (first_ident == error_mark_node)
1287 *p = tree_cons (TREE_PURPOSE (a), TREE_VALUE (a), NULL_TREE);
1288 p = &TREE_CHAIN (*p);
1291 else if (first_ident)
1293 for (tree a2 = first_ident; a2; a2 = TREE_CHAIN (a2))
1295 *p = tree_cons (TREE_PURPOSE (a2), TREE_VALUE (a2), NULL_TREE);
1296 p = &TREE_CHAIN (*p);
1298 first_ident = error_mark_node;
1301 if (first_ident != error_mark_node)
1302 new_attribs = first_ident;
1304 if (first_ident == attribs)
1305 /* All attributes affected type identity. */;
1306 else
1307 *remove_attributes = true;
1309 return cp_build_type_attribute_variant (result, new_attribs);
1312 /* Builds a qualified variant of T that is not a typedef variant.
1313 E.g. consider the following declarations:
1314 typedef const int ConstInt;
1315 typedef ConstInt* PtrConstInt;
1316 If T is PtrConstInt, this function returns a type representing
1317 const int*.
1318 In other words, if T is a typedef, the function returns the underlying type.
1319 The cv-qualification and attributes of the type returned match the
1320 input type.
1321 They will always be compatible types.
1322 The returned type is built so that all of its subtypes
1323 recursively have their typedefs stripped as well.
1325 This is different from just returning TYPE_CANONICAL (T)
1326 Because of several reasons:
1327 * If T is a type that needs structural equality
1328 its TYPE_CANONICAL (T) will be NULL.
1329 * TYPE_CANONICAL (T) desn't carry type attributes
1330 and loses template parameter names.
1332 If REMOVE_ATTRIBUTES is non-null, also strip attributes that don't
1333 affect type identity, and set the referent to true if any were
1334 stripped. */
1336 tree
1337 strip_typedefs (tree t, bool *remove_attributes)
1339 tree result = NULL, type = NULL, t0 = NULL;
1341 if (!t || t == error_mark_node)
1342 return t;
1344 if (TREE_CODE (t) == TREE_LIST)
1346 bool changed = false;
1347 vec<tree,va_gc> *vec = make_tree_vector ();
1348 tree r = t;
1349 for (; t; t = TREE_CHAIN (t))
1351 gcc_assert (!TREE_PURPOSE (t));
1352 tree elt = strip_typedefs (TREE_VALUE (t), remove_attributes);
1353 if (elt != TREE_VALUE (t))
1354 changed = true;
1355 vec_safe_push (vec, elt);
1357 if (changed)
1358 r = build_tree_list_vec (vec);
1359 release_tree_vector (vec);
1360 return r;
1363 gcc_assert (TYPE_P (t));
1365 if (t == TYPE_CANONICAL (t))
1366 return t;
1368 if (dependent_alias_template_spec_p (t))
1369 /* DR 1558: However, if the template-id is dependent, subsequent
1370 template argument substitution still applies to the template-id. */
1371 return t;
1373 switch (TREE_CODE (t))
1375 case POINTER_TYPE:
1376 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1377 result = build_pointer_type (type);
1378 break;
1379 case REFERENCE_TYPE:
1380 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1381 result = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
1382 break;
1383 case OFFSET_TYPE:
1384 t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t), remove_attributes);
1385 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1386 result = build_offset_type (t0, type);
1387 break;
1388 case RECORD_TYPE:
1389 if (TYPE_PTRMEMFUNC_P (t))
1391 t0 = strip_typedefs (TYPE_PTRMEMFUNC_FN_TYPE (t), remove_attributes);
1392 result = build_ptrmemfunc_type (t0);
1394 break;
1395 case ARRAY_TYPE:
1396 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1397 t0 = strip_typedefs (TYPE_DOMAIN (t), remove_attributes);
1398 result = build_cplus_array_type (type, t0);
1399 break;
1400 case FUNCTION_TYPE:
1401 case METHOD_TYPE:
1403 tree arg_types = NULL, arg_node, arg_node2, arg_type;
1404 bool changed;
1406 /* Because we stomp on TREE_PURPOSE of TYPE_ARG_TYPES in many places
1407 around the compiler (e.g. cp_parser_late_parsing_default_args), we
1408 can't expect that re-hashing a function type will find a previous
1409 equivalent type, so try to reuse the input type if nothing has
1410 changed. If the type is itself a variant, that will change. */
1411 bool is_variant = typedef_variant_p (t);
1412 if (remove_attributes
1413 && (TYPE_ATTRIBUTES (t) || TYPE_USER_ALIGN (t)))
1414 is_variant = true;
1416 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1417 changed = type != TREE_TYPE (t) || is_variant;
1419 for (arg_node = TYPE_ARG_TYPES (t);
1420 arg_node;
1421 arg_node = TREE_CHAIN (arg_node))
1423 if (arg_node == void_list_node)
1424 break;
1425 arg_type = strip_typedefs (TREE_VALUE (arg_node),
1426 remove_attributes);
1427 gcc_assert (arg_type);
1428 if (arg_type == TREE_VALUE (arg_node) && !changed)
1429 continue;
1431 if (!changed)
1433 changed = true;
1434 for (arg_node2 = TYPE_ARG_TYPES (t);
1435 arg_node2 != arg_node;
1436 arg_node2 = TREE_CHAIN (arg_node2))
1437 arg_types
1438 = tree_cons (TREE_PURPOSE (arg_node2),
1439 TREE_VALUE (arg_node2), arg_types);
1442 arg_types
1443 = tree_cons (TREE_PURPOSE (arg_node), arg_type, arg_types);
1446 if (!changed)
1447 return t;
1449 if (arg_types)
1450 arg_types = nreverse (arg_types);
1452 /* A list of parameters not ending with an ellipsis
1453 must end with void_list_node. */
1454 if (arg_node)
1455 arg_types = chainon (arg_types, void_list_node);
1457 if (TREE_CODE (t) == METHOD_TYPE)
1459 tree class_type = TREE_TYPE (TREE_VALUE (arg_types));
1460 gcc_assert (class_type);
1461 result =
1462 build_method_type_directly (class_type, type,
1463 TREE_CHAIN (arg_types));
1464 result
1465 = build_ref_qualified_type (result, type_memfn_rqual (t));
1467 else
1469 result = build_function_type (type,
1470 arg_types);
1471 result = apply_memfn_quals (result,
1472 type_memfn_quals (t),
1473 type_memfn_rqual (t));
1476 if (TYPE_RAISES_EXCEPTIONS (t))
1477 result = build_exception_variant (result,
1478 TYPE_RAISES_EXCEPTIONS (t));
1479 if (TYPE_HAS_LATE_RETURN_TYPE (t))
1480 TYPE_HAS_LATE_RETURN_TYPE (result) = 1;
1482 break;
1483 case TYPENAME_TYPE:
1485 tree fullname = TYPENAME_TYPE_FULLNAME (t);
1486 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
1487 && TREE_OPERAND (fullname, 1))
1489 tree args = TREE_OPERAND (fullname, 1);
1490 tree new_args = copy_node (args);
1491 bool changed = false;
1492 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
1494 tree arg = TREE_VEC_ELT (args, i);
1495 tree strip_arg;
1496 if (TYPE_P (arg))
1497 strip_arg = strip_typedefs (arg, remove_attributes);
1498 else
1499 strip_arg = strip_typedefs_expr (arg, remove_attributes);
1500 TREE_VEC_ELT (new_args, i) = strip_arg;
1501 if (strip_arg != arg)
1502 changed = true;
1504 if (changed)
1506 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_args)
1507 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
1508 fullname
1509 = lookup_template_function (TREE_OPERAND (fullname, 0),
1510 new_args);
1512 else
1513 ggc_free (new_args);
1515 result = make_typename_type (strip_typedefs (TYPE_CONTEXT (t),
1516 remove_attributes),
1517 fullname, typename_type, tf_none);
1518 /* Handle 'typedef typename A::N N;' */
1519 if (typedef_variant_p (result))
1520 result = TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (TYPE_NAME (result)));
1522 break;
1523 case DECLTYPE_TYPE:
1524 result = strip_typedefs_expr (DECLTYPE_TYPE_EXPR (t),
1525 remove_attributes);
1526 if (result == DECLTYPE_TYPE_EXPR (t))
1527 result = NULL_TREE;
1528 else
1529 result = (finish_decltype_type
1530 (result,
1531 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t),
1532 tf_none));
1533 break;
1534 default:
1535 break;
1538 if (!result)
1540 if (typedef_variant_p (t))
1542 /* Explicitly get the underlying type, as TYPE_MAIN_VARIANT doesn't
1543 strip typedefs with attributes. */
1544 result = TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (TYPE_NAME (t)));
1545 result = strip_typedefs (result);
1547 else
1548 result = TYPE_MAIN_VARIANT (t);
1550 gcc_assert (!typedef_variant_p (result));
1552 if (COMPLETE_TYPE_P (result) && !COMPLETE_TYPE_P (t))
1553 /* If RESULT is complete and T isn't, it's likely the case that T
1554 is a variant of RESULT which hasn't been updated yet. Skip the
1555 attribute handling. */;
1556 else
1558 if (TYPE_USER_ALIGN (t) != TYPE_USER_ALIGN (result)
1559 || TYPE_ALIGN (t) != TYPE_ALIGN (result))
1561 gcc_assert (TYPE_USER_ALIGN (t));
1562 if (remove_attributes)
1563 *remove_attributes = true;
1564 else
1566 if (TYPE_ALIGN (t) == TYPE_ALIGN (result))
1567 result = build_variant_type_copy (result);
1568 else
1569 result = build_aligned_type (result, TYPE_ALIGN (t));
1570 TYPE_USER_ALIGN (result) = true;
1574 if (TYPE_ATTRIBUTES (t))
1576 if (remove_attributes)
1577 result = apply_identity_attributes (result, TYPE_ATTRIBUTES (t),
1578 remove_attributes);
1579 else
1580 result = cp_build_type_attribute_variant (result,
1581 TYPE_ATTRIBUTES (t));
1585 return cp_build_qualified_type (result, cp_type_quals (t));
1588 /* Like strip_typedefs above, but works on expressions, so that in
1590 template<class T> struct A
1592 typedef T TT;
1593 B<sizeof(TT)> b;
1596 sizeof(TT) is replaced by sizeof(T). */
1598 tree
1599 strip_typedefs_expr (tree t, bool *remove_attributes)
1601 unsigned i,n;
1602 tree r, type, *ops;
1603 enum tree_code code;
1605 if (t == NULL_TREE || t == error_mark_node)
1606 return t;
1608 if (DECL_P (t) || CONSTANT_CLASS_P (t))
1609 return t;
1611 /* Some expressions have type operands, so let's handle types here rather
1612 than check TYPE_P in multiple places below. */
1613 if (TYPE_P (t))
1614 return strip_typedefs (t, remove_attributes);
1616 code = TREE_CODE (t);
1617 switch (code)
1619 case IDENTIFIER_NODE:
1620 case TEMPLATE_PARM_INDEX:
1621 case OVERLOAD:
1622 case BASELINK:
1623 case ARGUMENT_PACK_SELECT:
1624 return t;
1626 case TRAIT_EXPR:
1628 tree type1 = strip_typedefs (TRAIT_EXPR_TYPE1 (t), remove_attributes);
1629 tree type2 = strip_typedefs (TRAIT_EXPR_TYPE2 (t), remove_attributes);
1630 if (type1 == TRAIT_EXPR_TYPE1 (t)
1631 && type2 == TRAIT_EXPR_TYPE2 (t))
1632 return t;
1633 r = copy_node (t);
1634 TRAIT_EXPR_TYPE1 (r) = type1;
1635 TRAIT_EXPR_TYPE2 (r) = type2;
1636 return r;
1639 case TREE_LIST:
1641 vec<tree, va_gc> *vec = make_tree_vector ();
1642 bool changed = false;
1643 tree it;
1644 for (it = t; it; it = TREE_CHAIN (it))
1646 tree val = strip_typedefs_expr (TREE_VALUE (t), remove_attributes);
1647 vec_safe_push (vec, val);
1648 if (val != TREE_VALUE (t))
1649 changed = true;
1650 gcc_assert (TREE_PURPOSE (it) == NULL_TREE);
1652 if (changed)
1654 r = NULL_TREE;
1655 FOR_EACH_VEC_ELT_REVERSE (*vec, i, it)
1656 r = tree_cons (NULL_TREE, it, r);
1658 else
1659 r = t;
1660 release_tree_vector (vec);
1661 return r;
1664 case TREE_VEC:
1666 bool changed = false;
1667 vec<tree, va_gc> *vec = make_tree_vector ();
1668 n = TREE_VEC_LENGTH (t);
1669 vec_safe_reserve (vec, n);
1670 for (i = 0; i < n; ++i)
1672 tree op = strip_typedefs_expr (TREE_VEC_ELT (t, i),
1673 remove_attributes);
1674 vec->quick_push (op);
1675 if (op != TREE_VEC_ELT (t, i))
1676 changed = true;
1678 if (changed)
1680 r = copy_node (t);
1681 for (i = 0; i < n; ++i)
1682 TREE_VEC_ELT (r, i) = (*vec)[i];
1683 NON_DEFAULT_TEMPLATE_ARGS_COUNT (r)
1684 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
1686 else
1687 r = t;
1688 release_tree_vector (vec);
1689 return r;
1692 case CONSTRUCTOR:
1694 bool changed = false;
1695 vec<constructor_elt, va_gc> *vec
1696 = vec_safe_copy (CONSTRUCTOR_ELTS (t));
1697 n = CONSTRUCTOR_NELTS (t);
1698 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1699 for (i = 0; i < n; ++i)
1701 constructor_elt *e = &(*vec)[i];
1702 tree op = strip_typedefs_expr (e->value, remove_attributes);
1703 if (op != e->value)
1705 changed = true;
1706 e->value = op;
1708 gcc_checking_assert
1709 (e->index == strip_typedefs_expr (e->index, remove_attributes));
1712 if (!changed && type == TREE_TYPE (t))
1714 vec_free (vec);
1715 return t;
1717 else
1719 r = copy_node (t);
1720 TREE_TYPE (r) = type;
1721 CONSTRUCTOR_ELTS (r) = vec;
1722 return r;
1726 case LAMBDA_EXPR:
1727 error ("lambda-expression in a constant expression");
1728 return error_mark_node;
1730 default:
1731 break;
1734 gcc_assert (EXPR_P (t));
1736 n = TREE_OPERAND_LENGTH (t);
1737 ops = XALLOCAVEC (tree, n);
1738 type = TREE_TYPE (t);
1740 switch (code)
1742 CASE_CONVERT:
1743 case IMPLICIT_CONV_EXPR:
1744 case DYNAMIC_CAST_EXPR:
1745 case STATIC_CAST_EXPR:
1746 case CONST_CAST_EXPR:
1747 case REINTERPRET_CAST_EXPR:
1748 case CAST_EXPR:
1749 case NEW_EXPR:
1750 type = strip_typedefs (type, remove_attributes);
1751 /* fallthrough */
1753 default:
1754 for (i = 0; i < n; ++i)
1755 ops[i] = strip_typedefs_expr (TREE_OPERAND (t, i), remove_attributes);
1756 break;
1759 /* If nothing changed, return t. */
1760 for (i = 0; i < n; ++i)
1761 if (ops[i] != TREE_OPERAND (t, i))
1762 break;
1763 if (i == n && type == TREE_TYPE (t))
1764 return t;
1766 r = copy_node (t);
1767 TREE_TYPE (r) = type;
1768 for (i = 0; i < n; ++i)
1769 TREE_OPERAND (r, i) = ops[i];
1770 return r;
1773 /* Makes a copy of BINFO and TYPE, which is to be inherited into a
1774 graph dominated by T. If BINFO is NULL, TYPE is a dependent base,
1775 and we do a shallow copy. If BINFO is non-NULL, we do a deep copy.
1776 VIRT indicates whether TYPE is inherited virtually or not.
1777 IGO_PREV points at the previous binfo of the inheritance graph
1778 order chain. The newly copied binfo's TREE_CHAIN forms this
1779 ordering.
1781 The CLASSTYPE_VBASECLASSES vector of T is constructed in the
1782 correct order. That is in the order the bases themselves should be
1783 constructed in.
1785 The BINFO_INHERITANCE of a virtual base class points to the binfo
1786 of the most derived type. ??? We could probably change this so that
1787 BINFO_INHERITANCE becomes synonymous with BINFO_PRIMARY, and hence
1788 remove a field. They currently can only differ for primary virtual
1789 virtual bases. */
1791 tree
1792 copy_binfo (tree binfo, tree type, tree t, tree *igo_prev, int virt)
1794 tree new_binfo;
1796 if (virt)
1798 /* See if we've already made this virtual base. */
1799 new_binfo = binfo_for_vbase (type, t);
1800 if (new_binfo)
1801 return new_binfo;
1804 new_binfo = make_tree_binfo (binfo ? BINFO_N_BASE_BINFOS (binfo) : 0);
1805 BINFO_TYPE (new_binfo) = type;
1807 /* Chain it into the inheritance graph. */
1808 TREE_CHAIN (*igo_prev) = new_binfo;
1809 *igo_prev = new_binfo;
1811 if (binfo && !BINFO_DEPENDENT_BASE_P (binfo))
1813 int ix;
1814 tree base_binfo;
1816 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), type));
1818 BINFO_OFFSET (new_binfo) = BINFO_OFFSET (binfo);
1819 BINFO_VIRTUALS (new_binfo) = BINFO_VIRTUALS (binfo);
1821 /* We do not need to copy the accesses, as they are read only. */
1822 BINFO_BASE_ACCESSES (new_binfo) = BINFO_BASE_ACCESSES (binfo);
1824 /* Recursively copy base binfos of BINFO. */
1825 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1827 tree new_base_binfo;
1828 new_base_binfo = copy_binfo (base_binfo, BINFO_TYPE (base_binfo),
1829 t, igo_prev,
1830 BINFO_VIRTUAL_P (base_binfo));
1832 if (!BINFO_INHERITANCE_CHAIN (new_base_binfo))
1833 BINFO_INHERITANCE_CHAIN (new_base_binfo) = new_binfo;
1834 BINFO_BASE_APPEND (new_binfo, new_base_binfo);
1837 else
1838 BINFO_DEPENDENT_BASE_P (new_binfo) = 1;
1840 if (virt)
1842 /* Push it onto the list after any virtual bases it contains
1843 will have been pushed. */
1844 CLASSTYPE_VBASECLASSES (t)->quick_push (new_binfo);
1845 BINFO_VIRTUAL_P (new_binfo) = 1;
1846 BINFO_INHERITANCE_CHAIN (new_binfo) = TYPE_BINFO (t);
1849 return new_binfo;
1852 /* Hashing of lists so that we don't make duplicates.
1853 The entry point is `list_hash_canon'. */
1855 struct list_proxy
1857 tree purpose;
1858 tree value;
1859 tree chain;
1862 struct list_hasher : ggc_ptr_hash<tree_node>
1864 typedef list_proxy *compare_type;
1866 static hashval_t hash (tree);
1867 static bool equal (tree, list_proxy *);
1870 /* Now here is the hash table. When recording a list, it is added
1871 to the slot whose index is the hash code mod the table size.
1872 Note that the hash table is used for several kinds of lists.
1873 While all these live in the same table, they are completely independent,
1874 and the hash code is computed differently for each of these. */
1876 static GTY (()) hash_table<list_hasher> *list_hash_table;
1878 /* Compare ENTRY (an entry in the hash table) with DATA (a list_proxy
1879 for a node we are thinking about adding). */
1881 bool
1882 list_hasher::equal (tree t, list_proxy *proxy)
1884 return (TREE_VALUE (t) == proxy->value
1885 && TREE_PURPOSE (t) == proxy->purpose
1886 && TREE_CHAIN (t) == proxy->chain);
1889 /* Compute a hash code for a list (chain of TREE_LIST nodes
1890 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
1891 TREE_COMMON slots), by adding the hash codes of the individual entries. */
1893 static hashval_t
1894 list_hash_pieces (tree purpose, tree value, tree chain)
1896 hashval_t hashcode = 0;
1898 if (chain)
1899 hashcode += TREE_HASH (chain);
1901 if (value)
1902 hashcode += TREE_HASH (value);
1903 else
1904 hashcode += 1007;
1905 if (purpose)
1906 hashcode += TREE_HASH (purpose);
1907 else
1908 hashcode += 1009;
1909 return hashcode;
1912 /* Hash an already existing TREE_LIST. */
1914 hashval_t
1915 list_hasher::hash (tree t)
1917 return list_hash_pieces (TREE_PURPOSE (t),
1918 TREE_VALUE (t),
1919 TREE_CHAIN (t));
1922 /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
1923 object for an identical list if one already exists. Otherwise, build a
1924 new one, and record it as the canonical object. */
1926 tree
1927 hash_tree_cons (tree purpose, tree value, tree chain)
1929 int hashcode = 0;
1930 tree *slot;
1931 struct list_proxy proxy;
1933 /* Hash the list node. */
1934 hashcode = list_hash_pieces (purpose, value, chain);
1935 /* Create a proxy for the TREE_LIST we would like to create. We
1936 don't actually create it so as to avoid creating garbage. */
1937 proxy.purpose = purpose;
1938 proxy.value = value;
1939 proxy.chain = chain;
1940 /* See if it is already in the table. */
1941 slot = list_hash_table->find_slot_with_hash (&proxy, hashcode, INSERT);
1942 /* If not, create a new node. */
1943 if (!*slot)
1944 *slot = tree_cons (purpose, value, chain);
1945 return (tree) *slot;
1948 /* Constructor for hashed lists. */
1950 tree
1951 hash_tree_chain (tree value, tree chain)
1953 return hash_tree_cons (NULL_TREE, value, chain);
1956 void
1957 debug_binfo (tree elem)
1959 HOST_WIDE_INT n;
1960 tree virtuals;
1962 fprintf (stderr, "type \"%s\", offset = " HOST_WIDE_INT_PRINT_DEC
1963 "\nvtable type:\n",
1964 TYPE_NAME_STRING (BINFO_TYPE (elem)),
1965 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
1966 debug_tree (BINFO_TYPE (elem));
1967 if (BINFO_VTABLE (elem))
1968 fprintf (stderr, "vtable decl \"%s\"\n",
1969 IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem))));
1970 else
1971 fprintf (stderr, "no vtable decl yet\n");
1972 fprintf (stderr, "virtuals:\n");
1973 virtuals = BINFO_VIRTUALS (elem);
1974 n = 0;
1976 while (virtuals)
1978 tree fndecl = TREE_VALUE (virtuals);
1979 fprintf (stderr, "%s [%ld =? %ld]\n",
1980 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
1981 (long) n, (long) TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
1982 ++n;
1983 virtuals = TREE_CHAIN (virtuals);
1987 /* Build a representation for the qualified name SCOPE::NAME. TYPE is
1988 the type of the result expression, if known, or NULL_TREE if the
1989 resulting expression is type-dependent. If TEMPLATE_P is true,
1990 NAME is known to be a template because the user explicitly used the
1991 "template" keyword after the "::".
1993 All SCOPE_REFs should be built by use of this function. */
1995 tree
1996 build_qualified_name (tree type, tree scope, tree name, bool template_p)
1998 tree t;
1999 if (type == error_mark_node
2000 || scope == error_mark_node
2001 || name == error_mark_node)
2002 return error_mark_node;
2003 t = build2 (SCOPE_REF, type, scope, name);
2004 QUALIFIED_NAME_IS_TEMPLATE (t) = template_p;
2005 PTRMEM_OK_P (t) = true;
2006 if (type)
2007 t = convert_from_reference (t);
2008 return t;
2011 /* Like check_qualified_type, but also check ref-qualifier and exception
2012 specification. */
2014 static bool
2015 cp_check_qualified_type (const_tree cand, const_tree base, int type_quals,
2016 cp_ref_qualifier rqual, tree raises)
2018 return (TYPE_QUALS (cand) == type_quals
2019 && check_base_type (cand, base)
2020 && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (cand),
2021 ce_exact)
2022 && type_memfn_rqual (cand) == rqual);
2025 /* Build the FUNCTION_TYPE or METHOD_TYPE with the ref-qualifier RQUAL. */
2027 tree
2028 build_ref_qualified_type (tree type, cp_ref_qualifier rqual)
2030 tree t;
2032 if (rqual == type_memfn_rqual (type))
2033 return type;
2035 int type_quals = TYPE_QUALS (type);
2036 tree raises = TYPE_RAISES_EXCEPTIONS (type);
2037 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2038 if (cp_check_qualified_type (t, type, type_quals, rqual, raises))
2039 return t;
2041 t = build_variant_type_copy (type);
2042 switch (rqual)
2044 case REF_QUAL_RVALUE:
2045 FUNCTION_RVALUE_QUALIFIED (t) = 1;
2046 FUNCTION_REF_QUALIFIED (t) = 1;
2047 break;
2048 case REF_QUAL_LVALUE:
2049 FUNCTION_RVALUE_QUALIFIED (t) = 0;
2050 FUNCTION_REF_QUALIFIED (t) = 1;
2051 break;
2052 default:
2053 FUNCTION_REF_QUALIFIED (t) = 0;
2054 break;
2057 if (TYPE_STRUCTURAL_EQUALITY_P (type))
2058 /* Propagate structural equality. */
2059 SET_TYPE_STRUCTURAL_EQUALITY (t);
2060 else if (TYPE_CANONICAL (type) != type)
2061 /* Build the underlying canonical type, since it is different
2062 from TYPE. */
2063 TYPE_CANONICAL (t) = build_ref_qualified_type (TYPE_CANONICAL (type),
2064 rqual);
2065 else
2066 /* T is its own canonical type. */
2067 TYPE_CANONICAL (t) = t;
2069 return t;
2072 /* Returns nonzero if X is an expression for a (possibly overloaded)
2073 function. If "f" is a function or function template, "f", "c->f",
2074 "c.f", "C::f", and "f<int>" will all be considered possibly
2075 overloaded functions. Returns 2 if the function is actually
2076 overloaded, i.e., if it is impossible to know the type of the
2077 function without performing overload resolution. */
2080 is_overloaded_fn (tree x)
2082 /* A baselink is also considered an overloaded function. */
2083 if (TREE_CODE (x) == OFFSET_REF
2084 || TREE_CODE (x) == COMPONENT_REF)
2085 x = TREE_OPERAND (x, 1);
2086 if (BASELINK_P (x))
2087 x = BASELINK_FUNCTIONS (x);
2088 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
2089 x = TREE_OPERAND (x, 0);
2090 if (DECL_FUNCTION_TEMPLATE_P (OVL_CURRENT (x))
2091 || (TREE_CODE (x) == OVERLOAD && OVL_CHAIN (x)))
2092 return 2;
2093 return (TREE_CODE (x) == FUNCTION_DECL
2094 || TREE_CODE (x) == OVERLOAD);
2097 /* X is the CALL_EXPR_FN of a CALL_EXPR. If X represents a dependent name
2098 (14.6.2), return the IDENTIFIER_NODE for that name. Otherwise, return
2099 NULL_TREE. */
2101 tree
2102 dependent_name (tree x)
2104 if (identifier_p (x))
2105 return x;
2106 if (TREE_CODE (x) != COMPONENT_REF
2107 && TREE_CODE (x) != OFFSET_REF
2108 && TREE_CODE (x) != BASELINK
2109 && is_overloaded_fn (x))
2110 return DECL_NAME (get_first_fn (x));
2111 return NULL_TREE;
2114 /* Returns true iff X is an expression for an overloaded function
2115 whose type cannot be known without performing overload
2116 resolution. */
2118 bool
2119 really_overloaded_fn (tree x)
2121 return is_overloaded_fn (x) == 2;
2124 tree
2125 get_fns (tree from)
2127 gcc_assert (is_overloaded_fn (from));
2128 /* A baselink is also considered an overloaded function. */
2129 if (TREE_CODE (from) == OFFSET_REF
2130 || TREE_CODE (from) == COMPONENT_REF)
2131 from = TREE_OPERAND (from, 1);
2132 if (BASELINK_P (from))
2133 from = BASELINK_FUNCTIONS (from);
2134 if (TREE_CODE (from) == TEMPLATE_ID_EXPR)
2135 from = TREE_OPERAND (from, 0);
2136 return from;
2139 tree
2140 get_first_fn (tree from)
2142 return OVL_CURRENT (get_fns (from));
2145 /* Return a new OVL node, concatenating it with the old one. */
2147 tree
2148 ovl_cons (tree decl, tree chain)
2150 tree result = make_node (OVERLOAD);
2151 TREE_TYPE (result) = unknown_type_node;
2152 OVL_FUNCTION (result) = decl;
2153 TREE_CHAIN (result) = chain;
2155 return result;
2158 /* Build a new overloaded function. If this is the first one,
2159 just return it; otherwise, ovl_cons the _DECLs */
2161 tree
2162 build_overload (tree decl, tree chain)
2164 if (! chain && TREE_CODE (decl) != TEMPLATE_DECL)
2165 return decl;
2166 return ovl_cons (decl, chain);
2169 /* Return the scope where the overloaded functions OVL were found. */
2171 tree
2172 ovl_scope (tree ovl)
2174 if (TREE_CODE (ovl) == OFFSET_REF
2175 || TREE_CODE (ovl) == COMPONENT_REF)
2176 ovl = TREE_OPERAND (ovl, 1);
2177 if (TREE_CODE (ovl) == BASELINK)
2178 return BINFO_TYPE (BASELINK_BINFO (ovl));
2179 if (TREE_CODE (ovl) == TEMPLATE_ID_EXPR)
2180 ovl = TREE_OPERAND (ovl, 0);
2181 /* Skip using-declarations. */
2182 while (TREE_CODE (ovl) == OVERLOAD && OVL_USED (ovl) && OVL_CHAIN (ovl))
2183 ovl = OVL_CHAIN (ovl);
2184 return CP_DECL_CONTEXT (OVL_CURRENT (ovl));
2187 #define PRINT_RING_SIZE 4
2189 static const char *
2190 cxx_printable_name_internal (tree decl, int v, bool translate)
2192 static unsigned int uid_ring[PRINT_RING_SIZE];
2193 static char *print_ring[PRINT_RING_SIZE];
2194 static bool trans_ring[PRINT_RING_SIZE];
2195 static int ring_counter;
2196 int i;
2198 /* Only cache functions. */
2199 if (v < 2
2200 || TREE_CODE (decl) != FUNCTION_DECL
2201 || DECL_LANG_SPECIFIC (decl) == 0)
2202 return lang_decl_name (decl, v, translate);
2204 /* See if this print name is lying around. */
2205 for (i = 0; i < PRINT_RING_SIZE; i++)
2206 if (uid_ring[i] == DECL_UID (decl) && translate == trans_ring[i])
2207 /* yes, so return it. */
2208 return print_ring[i];
2210 if (++ring_counter == PRINT_RING_SIZE)
2211 ring_counter = 0;
2213 if (current_function_decl != NULL_TREE)
2215 /* There may be both translated and untranslated versions of the
2216 name cached. */
2217 for (i = 0; i < 2; i++)
2219 if (uid_ring[ring_counter] == DECL_UID (current_function_decl))
2220 ring_counter += 1;
2221 if (ring_counter == PRINT_RING_SIZE)
2222 ring_counter = 0;
2224 gcc_assert (uid_ring[ring_counter] != DECL_UID (current_function_decl));
2227 free (print_ring[ring_counter]);
2229 print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v, translate));
2230 uid_ring[ring_counter] = DECL_UID (decl);
2231 trans_ring[ring_counter] = translate;
2232 return print_ring[ring_counter];
2235 const char *
2236 cxx_printable_name (tree decl, int v)
2238 return cxx_printable_name_internal (decl, v, false);
2241 const char *
2242 cxx_printable_name_translate (tree decl, int v)
2244 return cxx_printable_name_internal (decl, v, true);
2247 /* Return the canonical version of exception-specification RAISES for a C++17
2248 function type, for use in type comparison and building TYPE_CANONICAL. */
2250 tree
2251 canonical_eh_spec (tree raises)
2253 if (raises == NULL_TREE)
2254 return raises;
2255 else if (DEFERRED_NOEXCEPT_SPEC_P (raises)
2256 || uses_template_parms (raises)
2257 || uses_template_parms (TREE_PURPOSE (raises)))
2258 /* Keep a dependent or deferred exception specification. */
2259 return raises;
2260 else if (nothrow_spec_p (raises))
2261 /* throw() -> noexcept. */
2262 return noexcept_true_spec;
2263 else
2264 /* For C++17 type matching, anything else -> nothing. */
2265 return NULL_TREE;
2268 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
2269 listed in RAISES. */
2271 tree
2272 build_exception_variant (tree type, tree raises)
2274 tree v;
2275 int type_quals;
2277 if (comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (type), ce_exact))
2278 return type;
2280 type_quals = TYPE_QUALS (type);
2281 cp_ref_qualifier rqual = type_memfn_rqual (type);
2282 for (v = TYPE_MAIN_VARIANT (type); v; v = TYPE_NEXT_VARIANT (v))
2283 if (cp_check_qualified_type (v, type, type_quals, rqual, raises))
2284 return v;
2286 /* Need to build a new variant. */
2287 v = build_variant_type_copy (type);
2288 TYPE_RAISES_EXCEPTIONS (v) = raises;
2290 if (!flag_noexcept_type)
2291 /* The exception-specification is not part of the canonical type. */
2292 return v;
2294 /* Canonicalize the exception specification. */
2295 tree cr = canonical_eh_spec (raises);
2297 if (TYPE_STRUCTURAL_EQUALITY_P (type))
2298 /* Propagate structural equality. */
2299 SET_TYPE_STRUCTURAL_EQUALITY (v);
2300 else if (TYPE_CANONICAL (type) != type || cr != raises)
2301 /* Build the underlying canonical type, since it is different
2302 from TYPE. */
2303 TYPE_CANONICAL (v) = build_exception_variant (TYPE_CANONICAL (type), cr);
2304 else
2305 /* T is its own canonical type. */
2306 TYPE_CANONICAL (v) = v;
2308 return v;
2311 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new
2312 BOUND_TEMPLATE_TEMPLATE_PARM bound with NEWARGS as its template
2313 arguments. */
2315 tree
2316 bind_template_template_parm (tree t, tree newargs)
2318 tree decl = TYPE_NAME (t);
2319 tree t2;
2321 t2 = cxx_make_type (BOUND_TEMPLATE_TEMPLATE_PARM);
2322 decl = build_decl (input_location,
2323 TYPE_DECL, DECL_NAME (decl), NULL_TREE);
2325 /* These nodes have to be created to reflect new TYPE_DECL and template
2326 arguments. */
2327 TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t));
2328 TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl;
2329 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
2330 = build_template_info (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t), newargs);
2332 TREE_TYPE (decl) = t2;
2333 TYPE_NAME (t2) = decl;
2334 TYPE_STUB_DECL (t2) = decl;
2335 TYPE_SIZE (t2) = 0;
2336 SET_TYPE_STRUCTURAL_EQUALITY (t2);
2338 return t2;
2341 /* Called from count_trees via walk_tree. */
2343 static tree
2344 count_trees_r (tree *tp, int *walk_subtrees, void *data)
2346 ++*((int *) data);
2348 if (TYPE_P (*tp))
2349 *walk_subtrees = 0;
2351 return NULL_TREE;
2354 /* Debugging function for measuring the rough complexity of a tree
2355 representation. */
2358 count_trees (tree t)
2360 int n_trees = 0;
2361 cp_walk_tree_without_duplicates (&t, count_trees_r, &n_trees);
2362 return n_trees;
2365 /* Called from verify_stmt_tree via walk_tree. */
2367 static tree
2368 verify_stmt_tree_r (tree* tp, int * /*walk_subtrees*/, void* data)
2370 tree t = *tp;
2371 hash_table<nofree_ptr_hash <tree_node> > *statements
2372 = static_cast <hash_table<nofree_ptr_hash <tree_node> > *> (data);
2373 tree_node **slot;
2375 if (!STATEMENT_CODE_P (TREE_CODE (t)))
2376 return NULL_TREE;
2378 /* If this statement is already present in the hash table, then
2379 there is a circularity in the statement tree. */
2380 gcc_assert (!statements->find (t));
2382 slot = statements->find_slot (t, INSERT);
2383 *slot = t;
2385 return NULL_TREE;
2388 /* Debugging function to check that the statement T has not been
2389 corrupted. For now, this function simply checks that T contains no
2390 circularities. */
2392 void
2393 verify_stmt_tree (tree t)
2395 hash_table<nofree_ptr_hash <tree_node> > statements (37);
2396 cp_walk_tree (&t, verify_stmt_tree_r, &statements, NULL);
2399 /* Check if the type T depends on a type with no linkage and if so, return
2400 it. If RELAXED_P then do not consider a class type declared within
2401 a vague-linkage function to have no linkage. */
2403 tree
2404 no_linkage_check (tree t, bool relaxed_p)
2406 tree r;
2408 /* There's no point in checking linkage on template functions; we
2409 can't know their complete types. */
2410 if (processing_template_decl)
2411 return NULL_TREE;
2413 switch (TREE_CODE (t))
2415 case RECORD_TYPE:
2416 if (TYPE_PTRMEMFUNC_P (t))
2417 goto ptrmem;
2418 /* Lambda types that don't have mangling scope have no linkage. We
2419 check CLASSTYPE_LAMBDA_EXPR for error_mark_node because
2420 when we get here from pushtag none of the lambda information is
2421 set up yet, so we want to assume that the lambda has linkage and
2422 fix it up later if not. */
2423 if (CLASSTYPE_LAMBDA_EXPR (t)
2424 && CLASSTYPE_LAMBDA_EXPR (t) != error_mark_node
2425 && LAMBDA_TYPE_EXTRA_SCOPE (t) == NULL_TREE)
2426 return t;
2427 /* Fall through. */
2428 case UNION_TYPE:
2429 if (!CLASS_TYPE_P (t))
2430 return NULL_TREE;
2431 /* Fall through. */
2432 case ENUMERAL_TYPE:
2433 /* Only treat unnamed types as having no linkage if they're at
2434 namespace scope. This is core issue 966. */
2435 if (TYPE_UNNAMED_P (t) && TYPE_NAMESPACE_SCOPE_P (t))
2436 return t;
2438 for (r = CP_TYPE_CONTEXT (t); ; )
2440 /* If we're a nested type of a !TREE_PUBLIC class, we might not
2441 have linkage, or we might just be in an anonymous namespace.
2442 If we're in a TREE_PUBLIC class, we have linkage. */
2443 if (TYPE_P (r) && !TREE_PUBLIC (TYPE_NAME (r)))
2444 return no_linkage_check (TYPE_CONTEXT (t), relaxed_p);
2445 else if (TREE_CODE (r) == FUNCTION_DECL)
2447 if (!relaxed_p || !vague_linkage_p (r))
2448 return t;
2449 else
2450 r = CP_DECL_CONTEXT (r);
2452 else
2453 break;
2456 return NULL_TREE;
2458 case ARRAY_TYPE:
2459 case POINTER_TYPE:
2460 case REFERENCE_TYPE:
2461 case VECTOR_TYPE:
2462 return no_linkage_check (TREE_TYPE (t), relaxed_p);
2464 case OFFSET_TYPE:
2465 ptrmem:
2466 r = no_linkage_check (TYPE_PTRMEM_POINTED_TO_TYPE (t),
2467 relaxed_p);
2468 if (r)
2469 return r;
2470 return no_linkage_check (TYPE_PTRMEM_CLASS_TYPE (t), relaxed_p);
2472 case METHOD_TYPE:
2473 case FUNCTION_TYPE:
2475 tree parm = TYPE_ARG_TYPES (t);
2476 if (TREE_CODE (t) == METHOD_TYPE)
2477 /* The 'this' pointer isn't interesting; a method has the same
2478 linkage (or lack thereof) as its enclosing class. */
2479 parm = TREE_CHAIN (parm);
2480 for (;
2481 parm && parm != void_list_node;
2482 parm = TREE_CHAIN (parm))
2484 r = no_linkage_check (TREE_VALUE (parm), relaxed_p);
2485 if (r)
2486 return r;
2488 return no_linkage_check (TREE_TYPE (t), relaxed_p);
2491 default:
2492 return NULL_TREE;
2496 extern int depth_reached;
2498 void
2499 cxx_print_statistics (void)
2501 print_search_statistics ();
2502 print_class_statistics ();
2503 print_template_statistics ();
2504 if (GATHER_STATISTICS)
2505 fprintf (stderr, "maximum template instantiation depth reached: %d\n",
2506 depth_reached);
2509 /* Return, as an INTEGER_CST node, the number of elements for TYPE
2510 (which is an ARRAY_TYPE). This counts only elements of the top
2511 array. */
2513 tree
2514 array_type_nelts_top (tree type)
2516 return fold_build2_loc (input_location,
2517 PLUS_EXPR, sizetype,
2518 array_type_nelts (type),
2519 size_one_node);
2522 /* Return, as an INTEGER_CST node, the number of elements for TYPE
2523 (which is an ARRAY_TYPE). This one is a recursive count of all
2524 ARRAY_TYPEs that are clumped together. */
2526 tree
2527 array_type_nelts_total (tree type)
2529 tree sz = array_type_nelts_top (type);
2530 type = TREE_TYPE (type);
2531 while (TREE_CODE (type) == ARRAY_TYPE)
2533 tree n = array_type_nelts_top (type);
2534 sz = fold_build2_loc (input_location,
2535 MULT_EXPR, sizetype, sz, n);
2536 type = TREE_TYPE (type);
2538 return sz;
2541 /* Called from break_out_target_exprs via mapcar. */
2543 static tree
2544 bot_manip (tree* tp, int* walk_subtrees, void* data)
2546 splay_tree target_remap = ((splay_tree) data);
2547 tree t = *tp;
2549 if (!TYPE_P (t) && TREE_CONSTANT (t) && !TREE_SIDE_EFFECTS (t))
2551 /* There can't be any TARGET_EXPRs or their slot variables below this
2552 point. But we must make a copy, in case subsequent processing
2553 alters any part of it. For example, during gimplification a cast
2554 of the form (T) &X::f (where "f" is a member function) will lead
2555 to replacing the PTRMEM_CST for &X::f with a VAR_DECL. */
2556 *walk_subtrees = 0;
2557 *tp = unshare_expr (t);
2558 return NULL_TREE;
2560 if (TREE_CODE (t) == TARGET_EXPR)
2562 tree u;
2564 if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
2566 u = build_cplus_new (TREE_TYPE (t), TREE_OPERAND (t, 1),
2567 tf_warning_or_error);
2568 if (AGGR_INIT_ZERO_FIRST (TREE_OPERAND (t, 1)))
2569 AGGR_INIT_ZERO_FIRST (TREE_OPERAND (u, 1)) = true;
2571 else
2572 u = build_target_expr_with_type (TREE_OPERAND (t, 1), TREE_TYPE (t),
2573 tf_warning_or_error);
2575 TARGET_EXPR_IMPLICIT_P (u) = TARGET_EXPR_IMPLICIT_P (t);
2576 TARGET_EXPR_LIST_INIT_P (u) = TARGET_EXPR_LIST_INIT_P (t);
2577 TARGET_EXPR_DIRECT_INIT_P (u) = TARGET_EXPR_DIRECT_INIT_P (t);
2579 /* Map the old variable to the new one. */
2580 splay_tree_insert (target_remap,
2581 (splay_tree_key) TREE_OPERAND (t, 0),
2582 (splay_tree_value) TREE_OPERAND (u, 0));
2584 TREE_OPERAND (u, 1) = break_out_target_exprs (TREE_OPERAND (u, 1));
2586 /* Replace the old expression with the new version. */
2587 *tp = u;
2588 /* We don't have to go below this point; the recursive call to
2589 break_out_target_exprs will have handled anything below this
2590 point. */
2591 *walk_subtrees = 0;
2592 return NULL_TREE;
2594 if (TREE_CODE (*tp) == SAVE_EXPR)
2596 t = *tp;
2597 splay_tree_node n = splay_tree_lookup (target_remap,
2598 (splay_tree_key) t);
2599 if (n)
2601 *tp = (tree)n->value;
2602 *walk_subtrees = 0;
2604 else
2606 copy_tree_r (tp, walk_subtrees, NULL);
2607 splay_tree_insert (target_remap,
2608 (splay_tree_key)t,
2609 (splay_tree_value)*tp);
2610 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
2611 splay_tree_insert (target_remap,
2612 (splay_tree_key)*tp,
2613 (splay_tree_value)*tp);
2615 return NULL_TREE;
2618 /* Make a copy of this node. */
2619 t = copy_tree_r (tp, walk_subtrees, NULL);
2620 if (TREE_CODE (*tp) == CALL_EXPR)
2622 set_flags_from_callee (*tp);
2624 /* builtin_LINE and builtin_FILE get the location where the default
2625 argument is expanded, not where the call was written. */
2626 tree callee = get_callee_fndecl (*tp);
2627 if (callee && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
2628 switch (DECL_FUNCTION_CODE (callee))
2630 case BUILT_IN_FILE:
2631 case BUILT_IN_LINE:
2632 SET_EXPR_LOCATION (*tp, input_location);
2633 default:
2634 break;
2637 return t;
2640 /* Replace all remapped VAR_DECLs in T with their new equivalents.
2641 DATA is really a splay-tree mapping old variables to new
2642 variables. */
2644 static tree
2645 bot_replace (tree* t, int* /*walk_subtrees*/, void* data)
2647 splay_tree target_remap = ((splay_tree) data);
2649 if (VAR_P (*t))
2651 splay_tree_node n = splay_tree_lookup (target_remap,
2652 (splay_tree_key) *t);
2653 if (n)
2654 *t = (tree) n->value;
2656 else if (TREE_CODE (*t) == PARM_DECL
2657 && DECL_NAME (*t) == this_identifier
2658 && !DECL_CONTEXT (*t))
2660 /* In an NSDMI we need to replace the 'this' parameter we used for
2661 parsing with the real one for this function. */
2662 *t = current_class_ptr;
2664 else if (TREE_CODE (*t) == CONVERT_EXPR
2665 && CONVERT_EXPR_VBASE_PATH (*t))
2667 /* In an NSDMI build_base_path defers building conversions to virtual
2668 bases, and we handle it here. */
2669 tree basetype = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (*t)));
2670 vec<tree, va_gc> *vbases = CLASSTYPE_VBASECLASSES (current_class_type);
2671 int i; tree binfo;
2672 FOR_EACH_VEC_SAFE_ELT (vbases, i, binfo)
2673 if (BINFO_TYPE (binfo) == basetype)
2674 break;
2675 *t = build_base_path (PLUS_EXPR, TREE_OPERAND (*t, 0), binfo, true,
2676 tf_warning_or_error);
2679 return NULL_TREE;
2682 /* When we parse a default argument expression, we may create
2683 temporary variables via TARGET_EXPRs. When we actually use the
2684 default-argument expression, we make a copy of the expression
2685 and replace the temporaries with appropriate local versions. */
2687 tree
2688 break_out_target_exprs (tree t)
2690 static int target_remap_count;
2691 static splay_tree target_remap;
2693 if (!target_remap_count++)
2694 target_remap = splay_tree_new (splay_tree_compare_pointers,
2695 /*splay_tree_delete_key_fn=*/NULL,
2696 /*splay_tree_delete_value_fn=*/NULL);
2697 cp_walk_tree (&t, bot_manip, target_remap, NULL);
2698 cp_walk_tree (&t, bot_replace, target_remap, NULL);
2700 if (!--target_remap_count)
2702 splay_tree_delete (target_remap);
2703 target_remap = NULL;
2706 return t;
2709 /* Build an expression for the subobject of OBJ at CONSTRUCTOR index INDEX,
2710 which we expect to have type TYPE. */
2712 tree
2713 build_ctor_subob_ref (tree index, tree type, tree obj)
2715 if (index == NULL_TREE)
2716 /* Can't refer to a particular member of a vector. */
2717 obj = NULL_TREE;
2718 else if (TREE_CODE (index) == INTEGER_CST)
2719 obj = cp_build_array_ref (input_location, obj, index, tf_none);
2720 else
2721 obj = build_class_member_access_expr (obj, index, NULL_TREE,
2722 /*reference*/false, tf_none);
2723 if (obj)
2725 tree objtype = TREE_TYPE (obj);
2726 if (TREE_CODE (objtype) == ARRAY_TYPE && !TYPE_DOMAIN (objtype))
2728 /* When the destination object refers to a flexible array member
2729 verify that it matches the type of the source object except
2730 for its domain and qualifiers. */
2731 gcc_assert (comptypes (TYPE_MAIN_VARIANT (type),
2732 TYPE_MAIN_VARIANT (objtype),
2733 COMPARE_REDECLARATION));
2735 else
2736 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, objtype));
2739 return obj;
2742 struct replace_placeholders_t
2744 tree obj; /* The object to be substituted for a PLACEHOLDER_EXPR. */
2745 bool seen; /* Whether we've encountered a PLACEHOLDER_EXPR. */
2748 /* Like substitute_placeholder_in_expr, but handle C++ tree codes and
2749 build up subexpressions as we go deeper. */
2751 static tree
2752 replace_placeholders_r (tree* t, int* walk_subtrees, void* data_)
2754 replace_placeholders_t *d = static_cast<replace_placeholders_t*>(data_);
2755 tree obj = d->obj;
2757 if (TREE_CONSTANT (*t))
2759 *walk_subtrees = false;
2760 return NULL_TREE;
2763 switch (TREE_CODE (*t))
2765 case PLACEHOLDER_EXPR:
2767 tree x = obj;
2768 for (; !(same_type_ignoring_top_level_qualifiers_p
2769 (TREE_TYPE (*t), TREE_TYPE (x)));
2770 x = TREE_OPERAND (x, 0))
2771 gcc_assert (TREE_CODE (x) == COMPONENT_REF);
2772 *t = x;
2773 *walk_subtrees = false;
2774 d->seen = true;
2776 break;
2778 case CONSTRUCTOR:
2780 constructor_elt *ce;
2781 vec<constructor_elt,va_gc> *v = CONSTRUCTOR_ELTS (*t);
2782 for (unsigned i = 0; vec_safe_iterate (v, i, &ce); ++i)
2784 tree *valp = &ce->value;
2785 tree type = TREE_TYPE (*valp);
2786 tree subob = obj;
2788 if (TREE_CODE (*valp) == CONSTRUCTOR
2789 && AGGREGATE_TYPE_P (type))
2791 /* If we're looking at the initializer for OBJ, then build
2792 a sub-object reference. If we're looking at an
2793 initializer for another object, just pass OBJ down. */
2794 if (same_type_ignoring_top_level_qualifiers_p
2795 (TREE_TYPE (*t), TREE_TYPE (obj)))
2796 subob = build_ctor_subob_ref (ce->index, type, obj);
2797 if (TREE_CODE (*valp) == TARGET_EXPR)
2798 valp = &TARGET_EXPR_INITIAL (*valp);
2800 d->obj = subob;
2801 cp_walk_tree (valp, replace_placeholders_r,
2802 data_, NULL);
2803 d->obj = obj;
2805 *walk_subtrees = false;
2806 break;
2809 default:
2810 break;
2813 return NULL_TREE;
2816 tree
2817 replace_placeholders (tree exp, tree obj, bool *seen_p)
2819 tree *tp = &exp;
2820 replace_placeholders_t data = { obj, false };
2821 if (TREE_CODE (exp) == TARGET_EXPR)
2822 tp = &TARGET_EXPR_INITIAL (exp);
2823 cp_walk_tree (tp, replace_placeholders_r, &data, NULL);
2824 if (seen_p)
2825 *seen_p = data.seen;
2826 return exp;
2829 /* Similar to `build_nt', but for template definitions of dependent
2830 expressions */
2832 tree
2833 build_min_nt_loc (location_t loc, enum tree_code code, ...)
2835 tree t;
2836 int length;
2837 int i;
2838 va_list p;
2840 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
2842 va_start (p, code);
2844 t = make_node (code);
2845 SET_EXPR_LOCATION (t, loc);
2846 length = TREE_CODE_LENGTH (code);
2848 for (i = 0; i < length; i++)
2850 tree x = va_arg (p, tree);
2851 TREE_OPERAND (t, i) = x;
2854 va_end (p);
2855 return t;
2859 /* Similar to `build', but for template definitions. */
2861 tree
2862 build_min (enum tree_code code, tree tt, ...)
2864 tree t;
2865 int length;
2866 int i;
2867 va_list p;
2869 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
2871 va_start (p, tt);
2873 t = make_node (code);
2874 length = TREE_CODE_LENGTH (code);
2875 TREE_TYPE (t) = tt;
2877 for (i = 0; i < length; i++)
2879 tree x = va_arg (p, tree);
2880 TREE_OPERAND (t, i) = x;
2881 if (x && !TYPE_P (x) && TREE_SIDE_EFFECTS (x))
2882 TREE_SIDE_EFFECTS (t) = 1;
2885 va_end (p);
2886 return t;
2889 /* Similar to `build', but for template definitions of non-dependent
2890 expressions. NON_DEP is the non-dependent expression that has been
2891 built. */
2893 tree
2894 build_min_non_dep (enum tree_code code, tree non_dep, ...)
2896 tree t;
2897 int length;
2898 int i;
2899 va_list p;
2901 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
2903 va_start (p, non_dep);
2905 if (REFERENCE_REF_P (non_dep))
2906 non_dep = TREE_OPERAND (non_dep, 0);
2908 t = make_node (code);
2909 length = TREE_CODE_LENGTH (code);
2910 TREE_TYPE (t) = unlowered_expr_type (non_dep);
2911 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
2913 for (i = 0; i < length; i++)
2915 tree x = va_arg (p, tree);
2916 TREE_OPERAND (t, i) = x;
2919 if (code == COMPOUND_EXPR && TREE_CODE (non_dep) != COMPOUND_EXPR)
2920 /* This should not be considered a COMPOUND_EXPR, because it
2921 resolves to an overload. */
2922 COMPOUND_EXPR_OVERLOADED (t) = 1;
2924 va_end (p);
2925 return convert_from_reference (t);
2928 /* Similar to `build_nt_call_vec', but for template definitions of
2929 non-dependent expressions. NON_DEP is the non-dependent expression
2930 that has been built. */
2932 tree
2933 build_min_non_dep_call_vec (tree non_dep, tree fn, vec<tree, va_gc> *argvec)
2935 tree t = build_nt_call_vec (fn, argvec);
2936 if (REFERENCE_REF_P (non_dep))
2937 non_dep = TREE_OPERAND (non_dep, 0);
2938 TREE_TYPE (t) = TREE_TYPE (non_dep);
2939 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
2940 return convert_from_reference (t);
2943 /* Similar to build_min_non_dep, but for expressions that have been resolved to
2944 a call to an operator overload. OP is the operator that has been
2945 overloaded. NON_DEP is the non-dependent expression that's been built,
2946 which should be a CALL_EXPR or an INDIRECT_REF to a CALL_EXPR. OVERLOAD is
2947 the overload that NON_DEP is calling. */
2949 tree
2950 build_min_non_dep_op_overload (enum tree_code op,
2951 tree non_dep,
2952 tree overload, ...)
2954 va_list p;
2955 int nargs, expected_nargs;
2956 tree fn, call;
2957 vec<tree, va_gc> *args;
2959 non_dep = extract_call_expr (non_dep);
2961 nargs = call_expr_nargs (non_dep);
2963 expected_nargs = cp_tree_code_length (op);
2964 if ((op == POSTINCREMENT_EXPR
2965 || op == POSTDECREMENT_EXPR)
2966 /* With -fpermissive non_dep could be operator++(). */
2967 && (!flag_permissive || nargs != expected_nargs))
2968 expected_nargs += 1;
2969 gcc_assert (nargs == expected_nargs);
2971 args = make_tree_vector ();
2972 va_start (p, overload);
2974 if (TREE_CODE (TREE_TYPE (overload)) == FUNCTION_TYPE)
2976 fn = overload;
2977 for (int i = 0; i < nargs; i++)
2979 tree arg = va_arg (p, tree);
2980 vec_safe_push (args, arg);
2983 else if (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE)
2985 tree object = va_arg (p, tree);
2986 tree binfo = TYPE_BINFO (TREE_TYPE (object));
2987 tree method = build_baselink (binfo, binfo, overload, NULL_TREE);
2988 fn = build_min (COMPONENT_REF, TREE_TYPE (overload),
2989 object, method, NULL_TREE);
2990 for (int i = 1; i < nargs; i++)
2992 tree arg = va_arg (p, tree);
2993 vec_safe_push (args, arg);
2996 else
2997 gcc_unreachable ();
2999 va_end (p);
3000 call = build_min_non_dep_call_vec (non_dep, fn, args);
3001 release_tree_vector (args);
3003 tree call_expr = extract_call_expr (call);
3004 KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep);
3005 CALL_EXPR_OPERATOR_SYNTAX (call_expr) = true;
3006 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep);
3007 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep);
3009 return call;
3012 /* Return a new tree vec copied from VEC, with ELT inserted at index IDX. */
3014 vec<tree, va_gc> *
3015 vec_copy_and_insert (vec<tree, va_gc> *old_vec, tree elt, unsigned idx)
3017 unsigned len = vec_safe_length (old_vec);
3018 gcc_assert (idx <= len);
3020 vec<tree, va_gc> *new_vec = NULL;
3021 vec_alloc (new_vec, len + 1);
3023 unsigned i;
3024 for (i = 0; i < len; ++i)
3026 if (i == idx)
3027 new_vec->quick_push (elt);
3028 new_vec->quick_push ((*old_vec)[i]);
3030 if (i == idx)
3031 new_vec->quick_push (elt);
3033 return new_vec;
3036 tree
3037 get_type_decl (tree t)
3039 if (TREE_CODE (t) == TYPE_DECL)
3040 return t;
3041 if (TYPE_P (t))
3042 return TYPE_STUB_DECL (t);
3043 gcc_assert (t == error_mark_node);
3044 return t;
3047 /* Returns the namespace that contains DECL, whether directly or
3048 indirectly. */
3050 tree
3051 decl_namespace_context (tree decl)
3053 while (1)
3055 if (TREE_CODE (decl) == NAMESPACE_DECL)
3056 return decl;
3057 else if (TYPE_P (decl))
3058 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
3059 else
3060 decl = CP_DECL_CONTEXT (decl);
3064 /* Returns true if decl is within an anonymous namespace, however deeply
3065 nested, or false otherwise. */
3067 bool
3068 decl_anon_ns_mem_p (const_tree decl)
3070 while (1)
3072 if (decl == NULL_TREE || decl == error_mark_node)
3073 return false;
3074 if (TREE_CODE (decl) == NAMESPACE_DECL
3075 && DECL_NAME (decl) == NULL_TREE)
3076 return true;
3077 /* Classes and namespaces inside anonymous namespaces have
3078 TREE_PUBLIC == 0, so we can shortcut the search. */
3079 else if (TYPE_P (decl))
3080 return (TREE_PUBLIC (TYPE_MAIN_DECL (decl)) == 0);
3081 else if (TREE_CODE (decl) == NAMESPACE_DECL)
3082 return (TREE_PUBLIC (decl) == 0);
3083 else
3084 decl = DECL_CONTEXT (decl);
3088 /* Subroutine of cp_tree_equal: t1 and t2 are the CALL_EXPR_FNs of two
3089 CALL_EXPRS. Return whether they are equivalent. */
3091 static bool
3092 called_fns_equal (tree t1, tree t2)
3094 /* Core 1321: dependent names are equivalent even if the overload sets
3095 are different. But do compare explicit template arguments. */
3096 tree name1 = dependent_name (t1);
3097 tree name2 = dependent_name (t2);
3098 if (name1 || name2)
3100 tree targs1 = NULL_TREE, targs2 = NULL_TREE;
3102 if (name1 != name2)
3103 return false;
3105 if (TREE_CODE (t1) == TEMPLATE_ID_EXPR)
3106 targs1 = TREE_OPERAND (t1, 1);
3107 if (TREE_CODE (t2) == TEMPLATE_ID_EXPR)
3108 targs2 = TREE_OPERAND (t2, 1);
3109 return cp_tree_equal (targs1, targs2);
3111 else
3112 return cp_tree_equal (t1, t2);
3115 /* Return truthvalue of whether T1 is the same tree structure as T2.
3116 Return 1 if they are the same. Return 0 if they are different. */
3118 bool
3119 cp_tree_equal (tree t1, tree t2)
3121 enum tree_code code1, code2;
3123 if (t1 == t2)
3124 return true;
3125 if (!t1 || !t2)
3126 return false;
3128 code1 = TREE_CODE (t1);
3129 code2 = TREE_CODE (t2);
3131 if (code1 != code2)
3132 return false;
3134 switch (code1)
3136 case VOID_CST:
3137 /* There's only a single VOID_CST node, so we should never reach
3138 here. */
3139 gcc_unreachable ();
3141 case INTEGER_CST:
3142 return tree_int_cst_equal (t1, t2);
3144 case REAL_CST:
3145 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
3147 case STRING_CST:
3148 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3149 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3150 TREE_STRING_LENGTH (t1));
3152 case FIXED_CST:
3153 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
3154 TREE_FIXED_CST (t2));
3156 case COMPLEX_CST:
3157 return cp_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
3158 && cp_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
3160 case VECTOR_CST:
3161 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
3163 case CONSTRUCTOR:
3164 /* We need to do this when determining whether or not two
3165 non-type pointer to member function template arguments
3166 are the same. */
3167 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
3168 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
3169 return false;
3171 tree field, value;
3172 unsigned int i;
3173 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
3175 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
3176 if (!cp_tree_equal (field, elt2->index)
3177 || !cp_tree_equal (value, elt2->value))
3178 return false;
3181 return true;
3183 case TREE_LIST:
3184 if (!cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
3185 return false;
3186 if (!cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
3187 return false;
3188 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
3190 case SAVE_EXPR:
3191 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3193 case CALL_EXPR:
3195 tree arg1, arg2;
3196 call_expr_arg_iterator iter1, iter2;
3197 if (!called_fns_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
3198 return false;
3199 for (arg1 = first_call_expr_arg (t1, &iter1),
3200 arg2 = first_call_expr_arg (t2, &iter2);
3201 arg1 && arg2;
3202 arg1 = next_call_expr_arg (&iter1),
3203 arg2 = next_call_expr_arg (&iter2))
3204 if (!cp_tree_equal (arg1, arg2))
3205 return false;
3206 if (arg1 || arg2)
3207 return false;
3208 return true;
3211 case TARGET_EXPR:
3213 tree o1 = TREE_OPERAND (t1, 0);
3214 tree o2 = TREE_OPERAND (t2, 0);
3216 /* Special case: if either target is an unallocated VAR_DECL,
3217 it means that it's going to be unified with whatever the
3218 TARGET_EXPR is really supposed to initialize, so treat it
3219 as being equivalent to anything. */
3220 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
3221 && !DECL_RTL_SET_P (o1))
3222 /*Nop*/;
3223 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
3224 && !DECL_RTL_SET_P (o2))
3225 /*Nop*/;
3226 else if (!cp_tree_equal (o1, o2))
3227 return false;
3229 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3232 case WITH_CLEANUP_EXPR:
3233 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
3234 return false;
3235 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
3237 case COMPONENT_REF:
3238 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
3239 return false;
3240 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3242 case PARM_DECL:
3243 /* For comparing uses of parameters in late-specified return types
3244 with an out-of-class definition of the function, but can also come
3245 up for expressions that involve 'this' in a member function
3246 template. */
3248 if (comparing_specializations && !CONSTRAINT_VAR_P (t1))
3249 /* When comparing hash table entries, only an exact match is
3250 good enough; we don't want to replace 'this' with the
3251 version from another function. But be more flexible
3252 with local parameters in a requires-expression. */
3253 return false;
3255 if (same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3257 if (DECL_ARTIFICIAL (t1) ^ DECL_ARTIFICIAL (t2))
3258 return false;
3259 if (CONSTRAINT_VAR_P (t1) ^ CONSTRAINT_VAR_P (t2))
3260 return false;
3261 if (DECL_ARTIFICIAL (t1)
3262 || (DECL_PARM_LEVEL (t1) == DECL_PARM_LEVEL (t2)
3263 && DECL_PARM_INDEX (t1) == DECL_PARM_INDEX (t2)))
3264 return true;
3266 return false;
3268 case VAR_DECL:
3269 case CONST_DECL:
3270 case FIELD_DECL:
3271 case FUNCTION_DECL:
3272 case TEMPLATE_DECL:
3273 case IDENTIFIER_NODE:
3274 case SSA_NAME:
3275 return false;
3277 case BASELINK:
3278 return (BASELINK_BINFO (t1) == BASELINK_BINFO (t2)
3279 && BASELINK_ACCESS_BINFO (t1) == BASELINK_ACCESS_BINFO (t2)
3280 && BASELINK_QUALIFIED_P (t1) == BASELINK_QUALIFIED_P (t2)
3281 && cp_tree_equal (BASELINK_FUNCTIONS (t1),
3282 BASELINK_FUNCTIONS (t2)));
3284 case TEMPLATE_PARM_INDEX:
3285 return (TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
3286 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2)
3287 && (TEMPLATE_PARM_PARAMETER_PACK (t1)
3288 == TEMPLATE_PARM_PARAMETER_PACK (t2))
3289 && same_type_p (TREE_TYPE (TEMPLATE_PARM_DECL (t1)),
3290 TREE_TYPE (TEMPLATE_PARM_DECL (t2))));
3292 case TEMPLATE_ID_EXPR:
3293 return (cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3294 && cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3296 case CONSTRAINT_INFO:
3297 return cp_tree_equal (CI_ASSOCIATED_CONSTRAINTS (t1),
3298 CI_ASSOCIATED_CONSTRAINTS (t2));
3300 case CHECK_CONSTR:
3301 return (CHECK_CONSTR_CONCEPT (t1) == CHECK_CONSTR_CONCEPT (t2)
3302 && comp_template_args (CHECK_CONSTR_ARGS (t1),
3303 CHECK_CONSTR_ARGS (t2)));
3305 case TREE_VEC:
3307 unsigned ix;
3308 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3309 return false;
3310 for (ix = TREE_VEC_LENGTH (t1); ix--;)
3311 if (!cp_tree_equal (TREE_VEC_ELT (t1, ix),
3312 TREE_VEC_ELT (t2, ix)))
3313 return false;
3314 return true;
3317 case SIZEOF_EXPR:
3318 case ALIGNOF_EXPR:
3320 tree o1 = TREE_OPERAND (t1, 0);
3321 tree o2 = TREE_OPERAND (t2, 0);
3323 if (code1 == SIZEOF_EXPR)
3325 if (SIZEOF_EXPR_TYPE_P (t1))
3326 o1 = TREE_TYPE (o1);
3327 if (SIZEOF_EXPR_TYPE_P (t2))
3328 o2 = TREE_TYPE (o2);
3330 if (TREE_CODE (o1) != TREE_CODE (o2))
3331 return false;
3332 if (TYPE_P (o1))
3333 return same_type_p (o1, o2);
3334 else
3335 return cp_tree_equal (o1, o2);
3338 case MODOP_EXPR:
3340 tree t1_op1, t2_op1;
3342 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
3343 return false;
3345 t1_op1 = TREE_OPERAND (t1, 1);
3346 t2_op1 = TREE_OPERAND (t2, 1);
3347 if (TREE_CODE (t1_op1) != TREE_CODE (t2_op1))
3348 return false;
3350 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t2, 2));
3353 case PTRMEM_CST:
3354 /* Two pointer-to-members are the same if they point to the same
3355 field or function in the same class. */
3356 if (PTRMEM_CST_MEMBER (t1) != PTRMEM_CST_MEMBER (t2))
3357 return false;
3359 return same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2));
3361 case OVERLOAD:
3362 if (OVL_FUNCTION (t1) != OVL_FUNCTION (t2))
3363 return false;
3364 return cp_tree_equal (OVL_CHAIN (t1), OVL_CHAIN (t2));
3366 case TRAIT_EXPR:
3367 if (TRAIT_EXPR_KIND (t1) != TRAIT_EXPR_KIND (t2))
3368 return false;
3369 return same_type_p (TRAIT_EXPR_TYPE1 (t1), TRAIT_EXPR_TYPE1 (t2))
3370 && cp_tree_equal (TRAIT_EXPR_TYPE2 (t1), TRAIT_EXPR_TYPE2 (t2));
3372 case CAST_EXPR:
3373 case STATIC_CAST_EXPR:
3374 case REINTERPRET_CAST_EXPR:
3375 case CONST_CAST_EXPR:
3376 case DYNAMIC_CAST_EXPR:
3377 case IMPLICIT_CONV_EXPR:
3378 case NEW_EXPR:
3379 CASE_CONVERT:
3380 case NON_LVALUE_EXPR:
3381 case VIEW_CONVERT_EXPR:
3382 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3383 return false;
3384 /* Now compare operands as usual. */
3385 break;
3387 case DEFERRED_NOEXCEPT:
3388 return (cp_tree_equal (DEFERRED_NOEXCEPT_PATTERN (t1),
3389 DEFERRED_NOEXCEPT_PATTERN (t2))
3390 && comp_template_args (DEFERRED_NOEXCEPT_ARGS (t1),
3391 DEFERRED_NOEXCEPT_ARGS (t2)));
3392 break;
3394 default:
3395 break;
3398 switch (TREE_CODE_CLASS (code1))
3400 case tcc_unary:
3401 case tcc_binary:
3402 case tcc_comparison:
3403 case tcc_expression:
3404 case tcc_vl_exp:
3405 case tcc_reference:
3406 case tcc_statement:
3408 int i, n;
3410 n = cp_tree_operand_length (t1);
3411 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
3412 && n != TREE_OPERAND_LENGTH (t2))
3413 return false;
3415 for (i = 0; i < n; ++i)
3416 if (!cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
3417 return false;
3419 return true;
3422 case tcc_type:
3423 return same_type_p (t1, t2);
3424 default:
3425 gcc_unreachable ();
3427 /* We can get here with --disable-checking. */
3428 return false;
3431 /* The type of ARG when used as an lvalue. */
3433 tree
3434 lvalue_type (tree arg)
3436 tree type = TREE_TYPE (arg);
3437 return type;
3440 /* The type of ARG for printing error messages; denote lvalues with
3441 reference types. */
3443 tree
3444 error_type (tree arg)
3446 tree type = TREE_TYPE (arg);
3448 if (TREE_CODE (type) == ARRAY_TYPE)
3450 else if (TREE_CODE (type) == ERROR_MARK)
3452 else if (lvalue_p (arg))
3453 type = build_reference_type (lvalue_type (arg));
3454 else if (MAYBE_CLASS_TYPE_P (type))
3455 type = lvalue_type (arg);
3457 return type;
3460 /* Does FUNCTION use a variable-length argument list? */
3463 varargs_function_p (const_tree function)
3465 return stdarg_p (TREE_TYPE (function));
3468 /* Returns 1 if decl is a member of a class. */
3471 member_p (const_tree decl)
3473 const_tree const ctx = DECL_CONTEXT (decl);
3474 return (ctx && TYPE_P (ctx));
3477 /* Create a placeholder for member access where we don't actually have an
3478 object that the access is against. */
3480 tree
3481 build_dummy_object (tree type)
3483 tree decl = build1 (CONVERT_EXPR, build_pointer_type (type), void_node);
3484 return cp_build_indirect_ref (decl, RO_NULL, tf_warning_or_error);
3487 /* We've gotten a reference to a member of TYPE. Return *this if appropriate,
3488 or a dummy object otherwise. If BINFOP is non-0, it is filled with the
3489 binfo path from current_class_type to TYPE, or 0. */
3491 tree
3492 maybe_dummy_object (tree type, tree* binfop)
3494 tree decl, context;
3495 tree binfo;
3496 tree current = current_nonlambda_class_type ();
3498 if (current
3499 && (binfo = lookup_base (current, type, ba_any, NULL,
3500 tf_warning_or_error)))
3501 context = current;
3502 else
3504 /* Reference from a nested class member function. */
3505 context = type;
3506 binfo = TYPE_BINFO (type);
3509 if (binfop)
3510 *binfop = binfo;
3512 if (current_class_ref
3513 /* current_class_ref might not correspond to current_class_type if
3514 we're in tsubst_default_argument or a lambda-declarator; in either
3515 case, we want to use current_class_ref if it matches CONTEXT. */
3516 && (same_type_ignoring_top_level_qualifiers_p
3517 (TREE_TYPE (current_class_ref), context)))
3518 decl = current_class_ref;
3519 else
3520 decl = build_dummy_object (context);
3522 return decl;
3525 /* Returns 1 if OB is a placeholder object, or a pointer to one. */
3528 is_dummy_object (const_tree ob)
3530 if (INDIRECT_REF_P (ob))
3531 ob = TREE_OPERAND (ob, 0);
3532 return (TREE_CODE (ob) == CONVERT_EXPR
3533 && TREE_OPERAND (ob, 0) == void_node);
3536 /* Returns 1 iff type T is something we want to treat as a scalar type for
3537 the purpose of deciding whether it is trivial/POD/standard-layout. */
3539 bool
3540 scalarish_type_p (const_tree t)
3542 if (t == error_mark_node)
3543 return 1;
3545 return (SCALAR_TYPE_P (t) || VECTOR_TYPE_P (t));
3548 /* Returns true iff T requires non-trivial default initialization. */
3550 bool
3551 type_has_nontrivial_default_init (const_tree t)
3553 t = strip_array_types (CONST_CAST_TREE (t));
3555 if (CLASS_TYPE_P (t))
3556 return TYPE_HAS_COMPLEX_DFLT (t);
3557 else
3558 return 0;
3561 /* Returns true iff copying an object of type T (including via move
3562 constructor) is non-trivial. That is, T has no non-trivial copy
3563 constructors and no non-trivial move constructors. */
3565 bool
3566 type_has_nontrivial_copy_init (const_tree t)
3568 t = strip_array_types (CONST_CAST_TREE (t));
3570 if (CLASS_TYPE_P (t))
3572 gcc_assert (COMPLETE_TYPE_P (t));
3573 return ((TYPE_HAS_COPY_CTOR (t)
3574 && TYPE_HAS_COMPLEX_COPY_CTOR (t))
3575 || TYPE_HAS_COMPLEX_MOVE_CTOR (t));
3577 else
3578 return 0;
3581 /* Returns 1 iff type T is a trivially copyable type, as defined in
3582 [basic.types] and [class]. */
3584 bool
3585 trivially_copyable_p (const_tree t)
3587 t = strip_array_types (CONST_CAST_TREE (t));
3589 if (CLASS_TYPE_P (t))
3590 return ((!TYPE_HAS_COPY_CTOR (t)
3591 || !TYPE_HAS_COMPLEX_COPY_CTOR (t))
3592 && !TYPE_HAS_COMPLEX_MOVE_CTOR (t)
3593 && (!TYPE_HAS_COPY_ASSIGN (t)
3594 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (t))
3595 && !TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
3596 && TYPE_HAS_TRIVIAL_DESTRUCTOR (t));
3597 else
3598 return !CP_TYPE_VOLATILE_P (t) && scalarish_type_p (t);
3601 /* Returns 1 iff type T is a trivial type, as defined in [basic.types] and
3602 [class]. */
3604 bool
3605 trivial_type_p (const_tree t)
3607 t = strip_array_types (CONST_CAST_TREE (t));
3609 if (CLASS_TYPE_P (t))
3610 return (TYPE_HAS_TRIVIAL_DFLT (t)
3611 && trivially_copyable_p (t));
3612 else
3613 return scalarish_type_p (t);
3616 /* Returns 1 iff type T is a POD type, as defined in [basic.types]. */
3618 bool
3619 pod_type_p (const_tree t)
3621 /* This CONST_CAST is okay because strip_array_types returns its
3622 argument unmodified and we assign it to a const_tree. */
3623 t = strip_array_types (CONST_CAST_TREE(t));
3625 if (!CLASS_TYPE_P (t))
3626 return scalarish_type_p (t);
3627 else if (cxx_dialect > cxx98)
3628 /* [class]/10: A POD struct is a class that is both a trivial class and a
3629 standard-layout class, and has no non-static data members of type
3630 non-POD struct, non-POD union (or array of such types).
3632 We don't need to check individual members because if a member is
3633 non-std-layout or non-trivial, the class will be too. */
3634 return (std_layout_type_p (t) && trivial_type_p (t));
3635 else
3636 /* The C++98 definition of POD is different. */
3637 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
3640 /* Returns true iff T is POD for the purpose of layout, as defined in the
3641 C++ ABI. */
3643 bool
3644 layout_pod_type_p (const_tree t)
3646 t = strip_array_types (CONST_CAST_TREE (t));
3648 if (CLASS_TYPE_P (t))
3649 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
3650 else
3651 return scalarish_type_p (t);
3654 /* Returns true iff T is a standard-layout type, as defined in
3655 [basic.types]. */
3657 bool
3658 std_layout_type_p (const_tree t)
3660 t = strip_array_types (CONST_CAST_TREE (t));
3662 if (CLASS_TYPE_P (t))
3663 return !CLASSTYPE_NON_STD_LAYOUT (t);
3664 else
3665 return scalarish_type_p (t);
3668 static bool record_has_unique_obj_representations (const_tree, const_tree);
3670 /* Returns true iff T satisfies std::has_unique_object_representations<T>,
3671 as defined in [meta.unary.prop]. */
3673 bool
3674 type_has_unique_obj_representations (const_tree t)
3676 bool ret;
3678 t = strip_array_types (CONST_CAST_TREE (t));
3680 if (!trivially_copyable_p (t))
3681 return false;
3683 if (CLASS_TYPE_P (t) && CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t))
3684 return CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t);
3686 switch (TREE_CODE (t))
3688 case INTEGER_TYPE:
3689 case POINTER_TYPE:
3690 case REFERENCE_TYPE:
3691 /* If some backend has any paddings in these types, we should add
3692 a target hook for this and handle it there. */
3693 return true;
3695 case BOOLEAN_TYPE:
3696 /* For bool values other than 0 and 1 should only appear with
3697 undefined behavior. */
3698 return true;
3700 case ENUMERAL_TYPE:
3701 return type_has_unique_obj_representations (ENUM_UNDERLYING_TYPE (t));
3703 case REAL_TYPE:
3704 /* XFmode certainly contains padding on x86, which the CPU doesn't store
3705 when storing long double values, so for that we have to return false.
3706 Other kinds of floating point values are questionable due to +.0/-.0
3707 and NaNs, let's play safe for now. */
3708 return false;
3710 case FIXED_POINT_TYPE:
3711 return false;
3713 case OFFSET_TYPE:
3714 return true;
3716 case COMPLEX_TYPE:
3717 case VECTOR_TYPE:
3718 return type_has_unique_obj_representations (TREE_TYPE (t));
3720 case RECORD_TYPE:
3721 ret = record_has_unique_obj_representations (t, TYPE_SIZE (t));
3722 if (CLASS_TYPE_P (t))
3724 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
3725 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
3727 return ret;
3729 case UNION_TYPE:
3730 ret = true;
3731 bool any_fields;
3732 any_fields = false;
3733 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3734 if (TREE_CODE (field) == FIELD_DECL)
3736 any_fields = true;
3737 if (!type_has_unique_obj_representations (TREE_TYPE (field))
3738 || simple_cst_equal (DECL_SIZE (field), TYPE_SIZE (t)) != 1)
3740 ret = false;
3741 break;
3744 if (!any_fields && !integer_zerop (TYPE_SIZE (t)))
3745 ret = false;
3746 if (CLASS_TYPE_P (t))
3748 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
3749 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
3751 return ret;
3753 case NULLPTR_TYPE:
3754 return false;
3756 case ERROR_MARK:
3757 return false;
3759 default:
3760 gcc_unreachable ();
3764 /* Helper function for type_has_unique_obj_representations. */
3766 static bool
3767 record_has_unique_obj_representations (const_tree t, const_tree sz)
3769 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3770 if (TREE_CODE (field) != FIELD_DECL)
3772 /* For bases, can't use type_has_unique_obj_representations here, as in
3773 struct S { int i : 24; S (); };
3774 struct T : public S { int j : 8; T (); };
3775 S doesn't have unique obj representations, but T does. */
3776 else if (DECL_FIELD_IS_BASE (field))
3778 if (!record_has_unique_obj_representations (TREE_TYPE (field),
3779 DECL_SIZE (field)))
3780 return false;
3782 else if (DECL_C_BIT_FIELD (field))
3784 tree btype = DECL_BIT_FIELD_TYPE (field);
3785 if (!type_has_unique_obj_representations (btype))
3786 return false;
3788 else if (!type_has_unique_obj_representations (TREE_TYPE (field)))
3789 return false;
3791 offset_int cur = 0;
3792 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
3793 if (TREE_CODE (field) == FIELD_DECL)
3795 offset_int fld = wi::to_offset (DECL_FIELD_OFFSET (field));
3796 offset_int bitpos = wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
3797 fld = fld * BITS_PER_UNIT + bitpos;
3798 if (cur != fld)
3799 return false;
3800 if (DECL_SIZE (field))
3802 offset_int size = wi::to_offset (DECL_SIZE (field));
3803 cur += size;
3806 if (cur != wi::to_offset (sz))
3807 return false;
3809 return true;
3812 /* Nonzero iff type T is a class template implicit specialization. */
3814 bool
3815 class_tmpl_impl_spec_p (const_tree t)
3817 return CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INSTANTIATION (t);
3820 /* Returns 1 iff zero initialization of type T means actually storing
3821 zeros in it. */
3824 zero_init_p (const_tree t)
3826 /* This CONST_CAST is okay because strip_array_types returns its
3827 argument unmodified and we assign it to a const_tree. */
3828 t = strip_array_types (CONST_CAST_TREE(t));
3830 if (t == error_mark_node)
3831 return 1;
3833 /* NULL pointers to data members are initialized with -1. */
3834 if (TYPE_PTRDATAMEM_P (t))
3835 return 0;
3837 /* Classes that contain types that can't be zero-initialized, cannot
3838 be zero-initialized themselves. */
3839 if (CLASS_TYPE_P (t) && CLASSTYPE_NON_ZERO_INIT_P (t))
3840 return 0;
3842 return 1;
3845 /* Handle the C++17 [[nodiscard]] attribute, which is similar to the GNU
3846 warn_unused_result attribute. */
3848 static tree
3849 handle_nodiscard_attribute (tree *node, tree name, tree /*args*/,
3850 int /*flags*/, bool *no_add_attrs)
3852 if (TREE_CODE (*node) == FUNCTION_DECL)
3854 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
3855 warning (OPT_Wattributes, "%qE attribute applied to %qD with void "
3856 "return type", name, *node);
3858 else if (OVERLOAD_TYPE_P (*node))
3859 /* OK */;
3860 else
3862 warning (OPT_Wattributes, "%qE attribute can only be applied to "
3863 "functions or to class or enumeration types", name);
3864 *no_add_attrs = true;
3866 return NULL_TREE;
3869 /* Table of valid C++ attributes. */
3870 const struct attribute_spec cxx_attribute_table[] =
3872 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
3873 affects_type_identity } */
3874 { "init_priority", 1, 1, true, false, false,
3875 handle_init_priority_attribute, false },
3876 { "abi_tag", 1, -1, false, false, false,
3877 handle_abi_tag_attribute, true },
3878 { NULL, 0, 0, false, false, false, NULL, false }
3881 /* Table of C++ standard attributes. */
3882 const struct attribute_spec std_attribute_table[] =
3884 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
3885 affects_type_identity } */
3886 { "maybe_unused", 0, 0, false, false, false,
3887 handle_unused_attribute, false },
3888 { "nodiscard", 0, 0, false, false, false,
3889 handle_nodiscard_attribute, false },
3890 { NULL, 0, 0, false, false, false, NULL, false }
3893 /* Handle an "init_priority" attribute; arguments as in
3894 struct attribute_spec.handler. */
3895 static tree
3896 handle_init_priority_attribute (tree* node,
3897 tree name,
3898 tree args,
3899 int /*flags*/,
3900 bool* no_add_attrs)
3902 tree initp_expr = TREE_VALUE (args);
3903 tree decl = *node;
3904 tree type = TREE_TYPE (decl);
3905 int pri;
3907 STRIP_NOPS (initp_expr);
3908 initp_expr = default_conversion (initp_expr);
3909 if (initp_expr)
3910 initp_expr = maybe_constant_value (initp_expr);
3912 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
3914 error ("requested init_priority is not an integer constant");
3915 cxx_constant_value (initp_expr);
3916 *no_add_attrs = true;
3917 return NULL_TREE;
3920 pri = TREE_INT_CST_LOW (initp_expr);
3922 type = strip_array_types (type);
3924 if (decl == NULL_TREE
3925 || !VAR_P (decl)
3926 || !TREE_STATIC (decl)
3927 || DECL_EXTERNAL (decl)
3928 || (TREE_CODE (type) != RECORD_TYPE
3929 && TREE_CODE (type) != UNION_TYPE)
3930 /* Static objects in functions are initialized the
3931 first time control passes through that
3932 function. This is not precise enough to pin down an
3933 init_priority value, so don't allow it. */
3934 || current_function_decl)
3936 error ("can only use %qE attribute on file-scope definitions "
3937 "of objects of class type", name);
3938 *no_add_attrs = true;
3939 return NULL_TREE;
3942 if (pri > MAX_INIT_PRIORITY || pri <= 0)
3944 error ("requested init_priority is out of range");
3945 *no_add_attrs = true;
3946 return NULL_TREE;
3949 /* Check for init_priorities that are reserved for
3950 language and runtime support implementations.*/
3951 if (pri <= MAX_RESERVED_INIT_PRIORITY)
3953 warning
3954 (0, "requested init_priority is reserved for internal use");
3957 if (SUPPORTS_INIT_PRIORITY)
3959 SET_DECL_INIT_PRIORITY (decl, pri);
3960 DECL_HAS_INIT_PRIORITY_P (decl) = 1;
3961 return NULL_TREE;
3963 else
3965 error ("%qE attribute is not supported on this platform", name);
3966 *no_add_attrs = true;
3967 return NULL_TREE;
3971 /* DECL is being redeclared; the old declaration had the abi tags in OLD,
3972 and the new one has the tags in NEW_. Give an error if there are tags
3973 in NEW_ that weren't in OLD. */
3975 bool
3976 check_abi_tag_redeclaration (const_tree decl, const_tree old, const_tree new_)
3978 if (old && TREE_CODE (TREE_VALUE (old)) == TREE_LIST)
3979 old = TREE_VALUE (old);
3980 if (new_ && TREE_CODE (TREE_VALUE (new_)) == TREE_LIST)
3981 new_ = TREE_VALUE (new_);
3982 bool err = false;
3983 for (const_tree t = new_; t; t = TREE_CHAIN (t))
3985 tree str = TREE_VALUE (t);
3986 for (const_tree in = old; in; in = TREE_CHAIN (in))
3988 tree ostr = TREE_VALUE (in);
3989 if (cp_tree_equal (str, ostr))
3990 goto found;
3992 error ("redeclaration of %qD adds abi tag %E", decl, str);
3993 err = true;
3994 found:;
3996 if (err)
3998 inform (DECL_SOURCE_LOCATION (decl), "previous declaration here");
3999 return false;
4001 return true;
4004 /* The abi_tag attribute with the name NAME was given ARGS. If they are
4005 ill-formed, give an error and return false; otherwise, return true. */
4007 bool
4008 check_abi_tag_args (tree args, tree name)
4010 if (!args)
4012 error ("the %qE attribute requires arguments", name);
4013 return false;
4015 for (tree arg = args; arg; arg = TREE_CHAIN (arg))
4017 tree elt = TREE_VALUE (arg);
4018 if (TREE_CODE (elt) != STRING_CST
4019 || (!same_type_ignoring_top_level_qualifiers_p
4020 (strip_array_types (TREE_TYPE (elt)),
4021 char_type_node)))
4023 error ("arguments to the %qE attribute must be narrow string "
4024 "literals", name);
4025 return false;
4027 const char *begin = TREE_STRING_POINTER (elt);
4028 const char *end = begin + TREE_STRING_LENGTH (elt);
4029 for (const char *p = begin; p != end; ++p)
4031 char c = *p;
4032 if (p == begin)
4034 if (!ISALPHA (c) && c != '_')
4036 error ("arguments to the %qE attribute must contain valid "
4037 "identifiers", name);
4038 inform (input_location, "%<%c%> is not a valid first "
4039 "character for an identifier", c);
4040 return false;
4043 else if (p == end - 1)
4044 gcc_assert (c == 0);
4045 else
4047 if (!ISALNUM (c) && c != '_')
4049 error ("arguments to the %qE attribute must contain valid "
4050 "identifiers", name);
4051 inform (input_location, "%<%c%> is not a valid character "
4052 "in an identifier", c);
4053 return false;
4058 return true;
4061 /* Handle an "abi_tag" attribute; arguments as in
4062 struct attribute_spec.handler. */
4064 static tree
4065 handle_abi_tag_attribute (tree* node, tree name, tree args,
4066 int flags, bool* no_add_attrs)
4068 if (!check_abi_tag_args (args, name))
4069 goto fail;
4071 if (TYPE_P (*node))
4073 if (!OVERLOAD_TYPE_P (*node))
4075 error ("%qE attribute applied to non-class, non-enum type %qT",
4076 name, *node);
4077 goto fail;
4079 else if (!(flags & (int)ATTR_FLAG_TYPE_IN_PLACE))
4081 error ("%qE attribute applied to %qT after its definition",
4082 name, *node);
4083 goto fail;
4085 else if (CLASS_TYPE_P (*node)
4086 && CLASSTYPE_TEMPLATE_INSTANTIATION (*node))
4088 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
4089 "template instantiation %qT", name, *node);
4090 goto fail;
4092 else if (CLASS_TYPE_P (*node)
4093 && CLASSTYPE_TEMPLATE_SPECIALIZATION (*node))
4095 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
4096 "template specialization %qT", name, *node);
4097 goto fail;
4100 tree attributes = TYPE_ATTRIBUTES (*node);
4101 tree decl = TYPE_NAME (*node);
4103 /* Make sure all declarations have the same abi tags. */
4104 if (DECL_SOURCE_LOCATION (decl) != input_location)
4106 if (!check_abi_tag_redeclaration (decl,
4107 lookup_attribute ("abi_tag",
4108 attributes),
4109 args))
4110 goto fail;
4113 else
4115 if (!VAR_OR_FUNCTION_DECL_P (*node))
4117 error ("%qE attribute applied to non-function, non-variable %qD",
4118 name, *node);
4119 goto fail;
4121 else if (DECL_LANGUAGE (*node) == lang_c)
4123 error ("%qE attribute applied to extern \"C\" declaration %qD",
4124 name, *node);
4125 goto fail;
4129 return NULL_TREE;
4131 fail:
4132 *no_add_attrs = true;
4133 return NULL_TREE;
4136 /* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the
4137 thing pointed to by the constant. */
4139 tree
4140 make_ptrmem_cst (tree type, tree member)
4142 tree ptrmem_cst = make_node (PTRMEM_CST);
4143 TREE_TYPE (ptrmem_cst) = type;
4144 PTRMEM_CST_MEMBER (ptrmem_cst) = member;
4145 return ptrmem_cst;
4148 /* Build a variant of TYPE that has the indicated ATTRIBUTES. May
4149 return an existing type if an appropriate type already exists. */
4151 tree
4152 cp_build_type_attribute_variant (tree type, tree attributes)
4154 tree new_type;
4156 new_type = build_type_attribute_variant (type, attributes);
4157 if (TREE_CODE (new_type) == FUNCTION_TYPE
4158 || TREE_CODE (new_type) == METHOD_TYPE)
4160 new_type = build_exception_variant (new_type,
4161 TYPE_RAISES_EXCEPTIONS (type));
4162 new_type = build_ref_qualified_type (new_type,
4163 type_memfn_rqual (type));
4166 /* Making a new main variant of a class type is broken. */
4167 gcc_assert (!CLASS_TYPE_P (type) || new_type == type);
4169 return new_type;
4172 /* Return TRUE if TYPE1 and TYPE2 are identical for type hashing purposes.
4173 Called only after doing all language independent checks. */
4175 bool
4176 cxx_type_hash_eq (const_tree typea, const_tree typeb)
4178 gcc_assert (TREE_CODE (typea) == FUNCTION_TYPE
4179 || TREE_CODE (typea) == METHOD_TYPE);
4181 if (type_memfn_rqual (typea) != type_memfn_rqual (typeb))
4182 return false;
4183 return comp_except_specs (TYPE_RAISES_EXCEPTIONS (typea),
4184 TYPE_RAISES_EXCEPTIONS (typeb), ce_exact);
4187 /* Apply FUNC to all language-specific sub-trees of TP in a pre-order
4188 traversal. Called from walk_tree. */
4190 tree
4191 cp_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func,
4192 void *data, hash_set<tree> *pset)
4194 enum tree_code code = TREE_CODE (*tp);
4195 tree result;
4197 #define WALK_SUBTREE(NODE) \
4198 do \
4200 result = cp_walk_tree (&(NODE), func, data, pset); \
4201 if (result) goto out; \
4203 while (0)
4205 /* Not one of the easy cases. We must explicitly go through the
4206 children. */
4207 result = NULL_TREE;
4208 switch (code)
4210 case DEFAULT_ARG:
4211 case TEMPLATE_TEMPLATE_PARM:
4212 case BOUND_TEMPLATE_TEMPLATE_PARM:
4213 case UNBOUND_CLASS_TEMPLATE:
4214 case TEMPLATE_PARM_INDEX:
4215 case TEMPLATE_TYPE_PARM:
4216 case TYPENAME_TYPE:
4217 case TYPEOF_TYPE:
4218 case UNDERLYING_TYPE:
4219 /* None of these have subtrees other than those already walked
4220 above. */
4221 *walk_subtrees_p = 0;
4222 break;
4224 case BASELINK:
4225 WALK_SUBTREE (BASELINK_FUNCTIONS (*tp));
4226 *walk_subtrees_p = 0;
4227 break;
4229 case PTRMEM_CST:
4230 WALK_SUBTREE (TREE_TYPE (*tp));
4231 *walk_subtrees_p = 0;
4232 break;
4234 case TREE_LIST:
4235 WALK_SUBTREE (TREE_PURPOSE (*tp));
4236 break;
4238 case OVERLOAD:
4239 WALK_SUBTREE (OVL_FUNCTION (*tp));
4240 WALK_SUBTREE (OVL_CHAIN (*tp));
4241 *walk_subtrees_p = 0;
4242 break;
4244 case USING_DECL:
4245 WALK_SUBTREE (DECL_NAME (*tp));
4246 WALK_SUBTREE (USING_DECL_SCOPE (*tp));
4247 WALK_SUBTREE (USING_DECL_DECLS (*tp));
4248 *walk_subtrees_p = 0;
4249 break;
4251 case RECORD_TYPE:
4252 if (TYPE_PTRMEMFUNC_P (*tp))
4253 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (*tp));
4254 break;
4256 case TYPE_ARGUMENT_PACK:
4257 case NONTYPE_ARGUMENT_PACK:
4259 tree args = ARGUMENT_PACK_ARGS (*tp);
4260 int i, len = TREE_VEC_LENGTH (args);
4261 for (i = 0; i < len; i++)
4262 WALK_SUBTREE (TREE_VEC_ELT (args, i));
4264 break;
4266 case TYPE_PACK_EXPANSION:
4267 WALK_SUBTREE (TREE_TYPE (*tp));
4268 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp));
4269 *walk_subtrees_p = 0;
4270 break;
4272 case EXPR_PACK_EXPANSION:
4273 WALK_SUBTREE (TREE_OPERAND (*tp, 0));
4274 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp));
4275 *walk_subtrees_p = 0;
4276 break;
4278 case CAST_EXPR:
4279 case REINTERPRET_CAST_EXPR:
4280 case STATIC_CAST_EXPR:
4281 case CONST_CAST_EXPR:
4282 case DYNAMIC_CAST_EXPR:
4283 case IMPLICIT_CONV_EXPR:
4284 if (TREE_TYPE (*tp))
4285 WALK_SUBTREE (TREE_TYPE (*tp));
4288 int i;
4289 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (*tp)); ++i)
4290 WALK_SUBTREE (TREE_OPERAND (*tp, i));
4292 *walk_subtrees_p = 0;
4293 break;
4295 case TRAIT_EXPR:
4296 WALK_SUBTREE (TRAIT_EXPR_TYPE1 (*tp));
4297 WALK_SUBTREE (TRAIT_EXPR_TYPE2 (*tp));
4298 *walk_subtrees_p = 0;
4299 break;
4301 case DECLTYPE_TYPE:
4302 WALK_SUBTREE (DECLTYPE_TYPE_EXPR (*tp));
4303 *walk_subtrees_p = 0;
4304 break;
4306 case REQUIRES_EXPR:
4307 // Only recurse through the nested expression. Do not
4308 // walk the parameter list. Doing so causes false
4309 // positives in the pack expansion checker since the
4310 // requires parameters are introduced as pack expansions.
4311 WALK_SUBTREE (TREE_OPERAND (*tp, 1));
4312 *walk_subtrees_p = 0;
4313 break;
4315 case DECL_EXPR:
4316 /* User variables should be mentioned in BIND_EXPR_VARS
4317 and their initializers and sizes walked when walking
4318 the containing BIND_EXPR. Compiler temporaries are
4319 handled here. */
4320 if (VAR_P (TREE_OPERAND (*tp, 0))
4321 && DECL_ARTIFICIAL (TREE_OPERAND (*tp, 0))
4322 && !TREE_STATIC (TREE_OPERAND (*tp, 0)))
4324 tree decl = TREE_OPERAND (*tp, 0);
4325 WALK_SUBTREE (DECL_INITIAL (decl));
4326 WALK_SUBTREE (DECL_SIZE (decl));
4327 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
4329 break;
4331 default:
4332 return NULL_TREE;
4335 /* We didn't find what we were looking for. */
4336 out:
4337 return result;
4339 #undef WALK_SUBTREE
4342 /* Like save_expr, but for C++. */
4344 tree
4345 cp_save_expr (tree expr)
4347 /* There is no reason to create a SAVE_EXPR within a template; if
4348 needed, we can create the SAVE_EXPR when instantiating the
4349 template. Furthermore, the middle-end cannot handle C++-specific
4350 tree codes. */
4351 if (processing_template_decl)
4352 return expr;
4353 return save_expr (expr);
4356 /* Initialize tree.c. */
4358 void
4359 init_tree (void)
4361 list_hash_table = hash_table<list_hasher>::create_ggc (61);
4362 register_scoped_attributes (std_attribute_table, NULL);
4365 /* Returns the kind of special function that DECL (a FUNCTION_DECL)
4366 is. Note that sfk_none is zero, so this function can be used as a
4367 predicate to test whether or not DECL is a special function. */
4369 special_function_kind
4370 special_function_p (const_tree decl)
4372 /* Rather than doing all this stuff with magic names, we should
4373 probably have a field of type `special_function_kind' in
4374 DECL_LANG_SPECIFIC. */
4375 if (DECL_INHERITED_CTOR (decl))
4376 return sfk_inheriting_constructor;
4377 if (DECL_COPY_CONSTRUCTOR_P (decl))
4378 return sfk_copy_constructor;
4379 if (DECL_MOVE_CONSTRUCTOR_P (decl))
4380 return sfk_move_constructor;
4381 if (DECL_CONSTRUCTOR_P (decl))
4382 return sfk_constructor;
4383 if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
4385 if (copy_fn_p (decl))
4386 return sfk_copy_assignment;
4387 if (move_fn_p (decl))
4388 return sfk_move_assignment;
4390 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
4391 return sfk_destructor;
4392 if (DECL_COMPLETE_DESTRUCTOR_P (decl))
4393 return sfk_complete_destructor;
4394 if (DECL_BASE_DESTRUCTOR_P (decl))
4395 return sfk_base_destructor;
4396 if (DECL_DELETING_DESTRUCTOR_P (decl))
4397 return sfk_deleting_destructor;
4398 if (DECL_CONV_FN_P (decl))
4399 return sfk_conversion;
4400 if (deduction_guide_p (decl))
4401 return sfk_deduction_guide;
4403 return sfk_none;
4406 /* Returns nonzero if TYPE is a character type, including wchar_t. */
4409 char_type_p (tree type)
4411 return (same_type_p (type, char_type_node)
4412 || same_type_p (type, unsigned_char_type_node)
4413 || same_type_p (type, signed_char_type_node)
4414 || same_type_p (type, char16_type_node)
4415 || same_type_p (type, char32_type_node)
4416 || same_type_p (type, wchar_type_node));
4419 /* Returns the kind of linkage associated with the indicated DECL. Th
4420 value returned is as specified by the language standard; it is
4421 independent of implementation details regarding template
4422 instantiation, etc. For example, it is possible that a declaration
4423 to which this function assigns external linkage would not show up
4424 as a global symbol when you run `nm' on the resulting object file. */
4426 linkage_kind
4427 decl_linkage (tree decl)
4429 /* This function doesn't attempt to calculate the linkage from first
4430 principles as given in [basic.link]. Instead, it makes use of
4431 the fact that we have already set TREE_PUBLIC appropriately, and
4432 then handles a few special cases. Ideally, we would calculate
4433 linkage first, and then transform that into a concrete
4434 implementation. */
4436 /* Things that don't have names have no linkage. */
4437 if (!DECL_NAME (decl))
4438 return lk_none;
4440 /* Fields have no linkage. */
4441 if (TREE_CODE (decl) == FIELD_DECL)
4442 return lk_none;
4444 /* Things that are TREE_PUBLIC have external linkage. */
4445 if (TREE_PUBLIC (decl))
4446 return lk_external;
4448 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor variants,
4449 check one of the "clones" for the real linkage. */
4450 if ((DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
4451 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl))
4452 && DECL_CHAIN (decl)
4453 && DECL_CLONED_FUNCTION (DECL_CHAIN (decl)))
4454 return decl_linkage (DECL_CHAIN (decl));
4456 if (TREE_CODE (decl) == NAMESPACE_DECL)
4457 return lk_external;
4459 /* Linkage of a CONST_DECL depends on the linkage of the enumeration
4460 type. */
4461 if (TREE_CODE (decl) == CONST_DECL)
4462 return decl_linkage (TYPE_NAME (DECL_CONTEXT (decl)));
4464 /* Things in local scope do not have linkage, if they don't have
4465 TREE_PUBLIC set. */
4466 if (decl_function_context (decl))
4467 return lk_none;
4469 /* Members of the anonymous namespace also have TREE_PUBLIC unset, but
4470 are considered to have external linkage for language purposes, as do
4471 template instantiations on targets without weak symbols. DECLs really
4472 meant to have internal linkage have DECL_THIS_STATIC set. */
4473 if (TREE_CODE (decl) == TYPE_DECL)
4474 return lk_external;
4475 if (VAR_OR_FUNCTION_DECL_P (decl))
4477 if (!DECL_THIS_STATIC (decl))
4478 return lk_external;
4480 /* Static data members and static member functions from classes
4481 in anonymous namespace also don't have TREE_PUBLIC set. */
4482 if (DECL_CLASS_CONTEXT (decl))
4483 return lk_external;
4486 /* Everything else has internal linkage. */
4487 return lk_internal;
4490 /* Returns the storage duration of the object or reference associated with
4491 the indicated DECL, which should be a VAR_DECL or PARM_DECL. */
4493 duration_kind
4494 decl_storage_duration (tree decl)
4496 if (TREE_CODE (decl) == PARM_DECL)
4497 return dk_auto;
4498 if (TREE_CODE (decl) == FUNCTION_DECL)
4499 return dk_static;
4500 gcc_assert (VAR_P (decl));
4501 if (!TREE_STATIC (decl)
4502 && !DECL_EXTERNAL (decl))
4503 return dk_auto;
4504 if (CP_DECL_THREAD_LOCAL_P (decl))
4505 return dk_thread;
4506 return dk_static;
4509 /* EXP is an expression that we want to pre-evaluate. Returns (in
4510 *INITP) an expression that will perform the pre-evaluation. The
4511 value returned by this function is a side-effect free expression
4512 equivalent to the pre-evaluated expression. Callers must ensure
4513 that *INITP is evaluated before EXP. */
4515 tree
4516 stabilize_expr (tree exp, tree* initp)
4518 tree init_expr;
4520 if (!TREE_SIDE_EFFECTS (exp))
4521 init_expr = NULL_TREE;
4522 else if (VOID_TYPE_P (TREE_TYPE (exp)))
4524 init_expr = exp;
4525 exp = void_node;
4527 /* There are no expressions with REFERENCE_TYPE, but there can be call
4528 arguments with such a type; just treat it as a pointer. */
4529 else if (TREE_CODE (TREE_TYPE (exp)) == REFERENCE_TYPE
4530 || SCALAR_TYPE_P (TREE_TYPE (exp))
4531 || !glvalue_p (exp))
4533 init_expr = get_target_expr (exp);
4534 exp = TARGET_EXPR_SLOT (init_expr);
4535 if (CLASS_TYPE_P (TREE_TYPE (exp)))
4536 exp = move (exp);
4537 else
4538 exp = rvalue (exp);
4540 else
4542 bool xval = !lvalue_p (exp);
4543 exp = cp_build_addr_expr (exp, tf_warning_or_error);
4544 init_expr = get_target_expr (exp);
4545 exp = TARGET_EXPR_SLOT (init_expr);
4546 exp = cp_build_indirect_ref (exp, RO_NULL, tf_warning_or_error);
4547 if (xval)
4548 exp = move (exp);
4550 *initp = init_expr;
4552 gcc_assert (!TREE_SIDE_EFFECTS (exp));
4553 return exp;
4556 /* Add NEW_EXPR, an expression whose value we don't care about, after the
4557 similar expression ORIG. */
4559 tree
4560 add_stmt_to_compound (tree orig, tree new_expr)
4562 if (!new_expr || !TREE_SIDE_EFFECTS (new_expr))
4563 return orig;
4564 if (!orig || !TREE_SIDE_EFFECTS (orig))
4565 return new_expr;
4566 return build2 (COMPOUND_EXPR, void_type_node, orig, new_expr);
4569 /* Like stabilize_expr, but for a call whose arguments we want to
4570 pre-evaluate. CALL is modified in place to use the pre-evaluated
4571 arguments, while, upon return, *INITP contains an expression to
4572 compute the arguments. */
4574 void
4575 stabilize_call (tree call, tree *initp)
4577 tree inits = NULL_TREE;
4578 int i;
4579 int nargs = call_expr_nargs (call);
4581 if (call == error_mark_node || processing_template_decl)
4583 *initp = NULL_TREE;
4584 return;
4587 gcc_assert (TREE_CODE (call) == CALL_EXPR);
4589 for (i = 0; i < nargs; i++)
4591 tree init;
4592 CALL_EXPR_ARG (call, i) =
4593 stabilize_expr (CALL_EXPR_ARG (call, i), &init);
4594 inits = add_stmt_to_compound (inits, init);
4597 *initp = inits;
4600 /* Like stabilize_expr, but for an AGGR_INIT_EXPR whose arguments we want
4601 to pre-evaluate. CALL is modified in place to use the pre-evaluated
4602 arguments, while, upon return, *INITP contains an expression to
4603 compute the arguments. */
4605 static void
4606 stabilize_aggr_init (tree call, tree *initp)
4608 tree inits = NULL_TREE;
4609 int i;
4610 int nargs = aggr_init_expr_nargs (call);
4612 if (call == error_mark_node)
4613 return;
4615 gcc_assert (TREE_CODE (call) == AGGR_INIT_EXPR);
4617 for (i = 0; i < nargs; i++)
4619 tree init;
4620 AGGR_INIT_EXPR_ARG (call, i) =
4621 stabilize_expr (AGGR_INIT_EXPR_ARG (call, i), &init);
4622 inits = add_stmt_to_compound (inits, init);
4625 *initp = inits;
4628 /* Like stabilize_expr, but for an initialization.
4630 If the initialization is for an object of class type, this function
4631 takes care not to introduce additional temporaries.
4633 Returns TRUE iff the expression was successfully pre-evaluated,
4634 i.e., if INIT is now side-effect free, except for, possibly, a
4635 single call to a constructor. */
4637 bool
4638 stabilize_init (tree init, tree *initp)
4640 tree t = init;
4642 *initp = NULL_TREE;
4644 if (t == error_mark_node || processing_template_decl)
4645 return true;
4647 if (TREE_CODE (t) == INIT_EXPR)
4648 t = TREE_OPERAND (t, 1);
4649 if (TREE_CODE (t) == TARGET_EXPR)
4650 t = TARGET_EXPR_INITIAL (t);
4652 /* If the RHS can be stabilized without breaking copy elision, stabilize
4653 it. We specifically don't stabilize class prvalues here because that
4654 would mean an extra copy, but they might be stabilized below. */
4655 if (TREE_CODE (init) == INIT_EXPR
4656 && TREE_CODE (t) != CONSTRUCTOR
4657 && TREE_CODE (t) != AGGR_INIT_EXPR
4658 && (SCALAR_TYPE_P (TREE_TYPE (t))
4659 || glvalue_p (t)))
4661 TREE_OPERAND (init, 1) = stabilize_expr (t, initp);
4662 return true;
4665 if (TREE_CODE (t) == COMPOUND_EXPR
4666 && TREE_CODE (init) == INIT_EXPR)
4668 tree last = expr_last (t);
4669 /* Handle stabilizing the EMPTY_CLASS_EXPR pattern. */
4670 if (!TREE_SIDE_EFFECTS (last))
4672 *initp = t;
4673 TREE_OPERAND (init, 1) = last;
4674 return true;
4678 if (TREE_CODE (t) == CONSTRUCTOR)
4680 /* Aggregate initialization: stabilize each of the field
4681 initializers. */
4682 unsigned i;
4683 constructor_elt *ce;
4684 bool good = true;
4685 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
4686 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
4688 tree type = TREE_TYPE (ce->value);
4689 tree subinit;
4690 if (TREE_CODE (type) == REFERENCE_TYPE
4691 || SCALAR_TYPE_P (type))
4692 ce->value = stabilize_expr (ce->value, &subinit);
4693 else if (!stabilize_init (ce->value, &subinit))
4694 good = false;
4695 *initp = add_stmt_to_compound (*initp, subinit);
4697 return good;
4700 if (TREE_CODE (t) == CALL_EXPR)
4702 stabilize_call (t, initp);
4703 return true;
4706 if (TREE_CODE (t) == AGGR_INIT_EXPR)
4708 stabilize_aggr_init (t, initp);
4709 return true;
4712 /* The initialization is being performed via a bitwise copy -- and
4713 the item copied may have side effects. */
4714 return !TREE_SIDE_EFFECTS (init);
4717 /* Returns true if a cast to TYPE may appear in an integral constant
4718 expression. */
4720 bool
4721 cast_valid_in_integral_constant_expression_p (tree type)
4723 return (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
4724 || cxx_dialect >= cxx11
4725 || dependent_type_p (type)
4726 || type == error_mark_node);
4729 /* Return true if we need to fix linkage information of DECL. */
4731 static bool
4732 cp_fix_function_decl_p (tree decl)
4734 /* Skip if DECL is not externally visible. */
4735 if (!TREE_PUBLIC (decl))
4736 return false;
4738 /* We need to fix DECL if it a appears to be exported but with no
4739 function body. Thunks do not have CFGs and we may need to
4740 handle them specially later. */
4741 if (!gimple_has_body_p (decl)
4742 && !DECL_THUNK_P (decl)
4743 && !DECL_EXTERNAL (decl))
4745 struct cgraph_node *node = cgraph_node::get (decl);
4747 /* Don't fix same_body aliases. Although they don't have their own
4748 CFG, they share it with what they alias to. */
4749 if (!node || !node->alias
4750 || !vec_safe_length (node->ref_list.references))
4751 return true;
4754 return false;
4757 /* Clean the C++ specific parts of the tree T. */
4759 void
4760 cp_free_lang_data (tree t)
4762 if (TREE_CODE (t) == METHOD_TYPE
4763 || TREE_CODE (t) == FUNCTION_TYPE)
4765 /* Default args are not interesting anymore. */
4766 tree argtypes = TYPE_ARG_TYPES (t);
4767 while (argtypes)
4769 TREE_PURPOSE (argtypes) = 0;
4770 argtypes = TREE_CHAIN (argtypes);
4773 else if (TREE_CODE (t) == FUNCTION_DECL
4774 && cp_fix_function_decl_p (t))
4776 /* If T is used in this translation unit at all, the definition
4777 must exist somewhere else since we have decided to not emit it
4778 in this TU. So make it an external reference. */
4779 DECL_EXTERNAL (t) = 1;
4780 TREE_STATIC (t) = 0;
4782 if (TREE_CODE (t) == NAMESPACE_DECL)
4784 /* The list of users of a namespace isn't useful for the middle-end
4785 or debug generators. */
4786 DECL_NAMESPACE_USERS (t) = NULL_TREE;
4787 /* Neither do we need the leftover chaining of namespaces
4788 from the binding level. */
4789 DECL_CHAIN (t) = NULL_TREE;
4793 /* Stub for c-common. Please keep in sync with c-decl.c.
4794 FIXME: If address space support is target specific, then this
4795 should be a C target hook. But currently this is not possible,
4796 because this function is called via REGISTER_TARGET_PRAGMAS. */
4797 void
4798 c_register_addr_space (const char * /*word*/, addr_space_t /*as*/)
4802 /* Return the number of operands in T that we care about for things like
4803 mangling. */
4806 cp_tree_operand_length (const_tree t)
4808 enum tree_code code = TREE_CODE (t);
4810 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
4811 return VL_EXP_OPERAND_LENGTH (t);
4813 return cp_tree_code_length (code);
4816 /* Like cp_tree_operand_length, but takes a tree_code CODE. */
4819 cp_tree_code_length (enum tree_code code)
4821 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
4823 switch (code)
4825 case PREINCREMENT_EXPR:
4826 case PREDECREMENT_EXPR:
4827 case POSTINCREMENT_EXPR:
4828 case POSTDECREMENT_EXPR:
4829 return 1;
4831 case ARRAY_REF:
4832 return 2;
4834 case EXPR_PACK_EXPANSION:
4835 return 1;
4837 default:
4838 return TREE_CODE_LENGTH (code);
4842 /* Implement -Wzero_as_null_pointer_constant. Return true if the
4843 conditions for the warning hold, false otherwise. */
4844 bool
4845 maybe_warn_zero_as_null_pointer_constant (tree expr, location_t loc)
4847 if (c_inhibit_evaluation_warnings == 0
4848 && !NULLPTR_TYPE_P (TREE_TYPE (expr)))
4850 warning_at (loc, OPT_Wzero_as_null_pointer_constant,
4851 "zero as null pointer constant");
4852 return true;
4854 return false;
4857 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4858 /* Complain that some language-specific thing hanging off a tree
4859 node has been accessed improperly. */
4861 void
4862 lang_check_failed (const char* file, int line, const char* function)
4864 internal_error ("lang_* check: failed in %s, at %s:%d",
4865 function, trim_filename (file), line);
4867 #endif /* ENABLE_TREE_CHECKING */
4869 #include "gt-cp-tree.h"