[PR c++/85008] ICE looking for clone
[official-gcc.git] / gcc / cp / tree.c
blobf1a90bdec0fc4639957b259658276a6293cc0687
1 /* Language-dependent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2018 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 "stringpool.h"
36 #include "attribs.h"
37 #include "flags.h"
38 #include "selftest.h"
40 static tree bot_manip (tree *, int *, void *);
41 static tree bot_replace (tree *, int *, void *);
42 static hashval_t list_hash_pieces (tree, tree, tree);
43 static tree build_target_expr (tree, tree, tsubst_flags_t);
44 static tree count_trees_r (tree *, int *, void *);
45 static tree verify_stmt_tree_r (tree *, int *, void *);
46 static tree build_local_temp (tree);
48 static tree handle_init_priority_attribute (tree *, tree, tree, int, bool *);
49 static tree handle_abi_tag_attribute (tree *, tree, tree, int, bool *);
51 /* If REF is an lvalue, returns the kind of lvalue that REF is.
52 Otherwise, returns clk_none. */
54 cp_lvalue_kind
55 lvalue_kind (const_tree ref)
57 cp_lvalue_kind op1_lvalue_kind = clk_none;
58 cp_lvalue_kind op2_lvalue_kind = clk_none;
60 /* Expressions of reference type are sometimes wrapped in
61 INDIRECT_REFs. INDIRECT_REFs are just internal compiler
62 representation, not part of the language, so we have to look
63 through them. */
64 if (REFERENCE_REF_P (ref))
65 return lvalue_kind (TREE_OPERAND (ref, 0));
67 if (TREE_TYPE (ref)
68 && TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
70 /* unnamed rvalue references are rvalues */
71 if (TYPE_REF_IS_RVALUE (TREE_TYPE (ref))
72 && TREE_CODE (ref) != PARM_DECL
73 && !VAR_P (ref)
74 && TREE_CODE (ref) != COMPONENT_REF
75 /* Functions are always lvalues. */
76 && TREE_CODE (TREE_TYPE (TREE_TYPE (ref))) != FUNCTION_TYPE)
77 return clk_rvalueref;
79 /* lvalue references and named rvalue references are lvalues. */
80 return clk_ordinary;
83 if (ref == current_class_ptr)
84 return clk_none;
86 switch (TREE_CODE (ref))
88 case SAVE_EXPR:
89 return clk_none;
90 /* preincrements and predecrements are valid lvals, provided
91 what they refer to are valid lvals. */
92 case PREINCREMENT_EXPR:
93 case PREDECREMENT_EXPR:
94 case TRY_CATCH_EXPR:
95 case REALPART_EXPR:
96 case IMAGPART_EXPR:
97 return lvalue_kind (TREE_OPERAND (ref, 0));
99 case MEMBER_REF:
100 case DOTSTAR_EXPR:
101 if (TREE_CODE (ref) == MEMBER_REF)
102 op1_lvalue_kind = clk_ordinary;
103 else
104 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
105 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
106 op1_lvalue_kind = clk_none;
107 return op1_lvalue_kind;
109 case COMPONENT_REF:
110 if (BASELINK_P (TREE_OPERAND (ref, 1)))
112 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (ref, 1));
114 /* For static member function recurse on the BASELINK, we can get
115 here e.g. from reference_binding. If BASELINK_FUNCTIONS is
116 OVERLOAD, the overload is resolved first if possible through
117 resolve_address_of_overloaded_function. */
118 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_STATIC_FUNCTION_P (fn))
119 return lvalue_kind (TREE_OPERAND (ref, 1));
121 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
122 /* Look at the member designator. */
123 if (!op1_lvalue_kind)
125 else if (is_overloaded_fn (TREE_OPERAND (ref, 1)))
126 /* The "field" can be a FUNCTION_DECL or an OVERLOAD in some
127 situations. If we're seeing a COMPONENT_REF, it's a non-static
128 member, so it isn't an lvalue. */
129 op1_lvalue_kind = clk_none;
130 else if (TREE_CODE (TREE_OPERAND (ref, 1)) != FIELD_DECL)
131 /* This can be IDENTIFIER_NODE in a template. */;
132 else if (DECL_C_BIT_FIELD (TREE_OPERAND (ref, 1)))
134 /* Clear the ordinary bit. If this object was a class
135 rvalue we want to preserve that information. */
136 op1_lvalue_kind &= ~clk_ordinary;
137 /* The lvalue is for a bitfield. */
138 op1_lvalue_kind |= clk_bitfield;
140 else if (DECL_PACKED (TREE_OPERAND (ref, 1)))
141 op1_lvalue_kind |= clk_packed;
143 return op1_lvalue_kind;
145 case STRING_CST:
146 case COMPOUND_LITERAL_EXPR:
147 return clk_ordinary;
149 case CONST_DECL:
150 /* CONST_DECL without TREE_STATIC are enumeration values and
151 thus not lvalues. With TREE_STATIC they are used by ObjC++
152 in objc_build_string_object and need to be considered as
153 lvalues. */
154 if (! TREE_STATIC (ref))
155 return clk_none;
156 /* FALLTHRU */
157 case VAR_DECL:
158 if (VAR_P (ref) && DECL_HAS_VALUE_EXPR_P (ref))
159 return lvalue_kind (DECL_VALUE_EXPR (CONST_CAST_TREE (ref)));
161 if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
162 && DECL_LANG_SPECIFIC (ref)
163 && DECL_IN_AGGR_P (ref))
164 return clk_none;
165 /* FALLTHRU */
166 case INDIRECT_REF:
167 case ARROW_EXPR:
168 case ARRAY_REF:
169 case PARM_DECL:
170 case RESULT_DECL:
171 case PLACEHOLDER_EXPR:
172 return clk_ordinary;
174 /* A scope ref in a template, left as SCOPE_REF to support later
175 access checking. */
176 case SCOPE_REF:
177 gcc_assert (!type_dependent_expression_p (CONST_CAST_TREE (ref)));
179 tree op = TREE_OPERAND (ref, 1);
180 if (TREE_CODE (op) == FIELD_DECL)
181 return (DECL_C_BIT_FIELD (op) ? clk_bitfield : clk_ordinary);
182 else
183 return lvalue_kind (op);
186 case MAX_EXPR:
187 case MIN_EXPR:
188 /* Disallow <? and >? as lvalues if either argument side-effects. */
189 if (TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 0))
190 || TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 1)))
191 return clk_none;
192 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
193 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1));
194 break;
196 case COND_EXPR:
197 if (processing_template_decl)
199 /* Within templates, a REFERENCE_TYPE will indicate whether
200 the COND_EXPR result is an ordinary lvalue or rvalueref.
201 Since REFERENCE_TYPEs are handled above, if we reach this
202 point, we know we got a plain rvalue. Unless we have a
203 type-dependent expr, that is, but we shouldn't be testing
204 lvalueness if we can't even tell the types yet! */
205 gcc_assert (!type_dependent_expression_p (CONST_CAST_TREE (ref)));
206 if (CLASS_TYPE_P (TREE_TYPE (ref))
207 || TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE)
208 return clk_class;
209 else
210 return clk_none;
212 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1)
213 ? TREE_OPERAND (ref, 1)
214 : TREE_OPERAND (ref, 0));
215 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 2));
216 break;
218 case MODOP_EXPR:
219 /* We expect to see unlowered MODOP_EXPRs only during
220 template processing. */
221 gcc_assert (processing_template_decl);
222 return clk_ordinary;
224 case MODIFY_EXPR:
225 case TYPEID_EXPR:
226 return clk_ordinary;
228 case COMPOUND_EXPR:
229 return lvalue_kind (TREE_OPERAND (ref, 1));
231 case TARGET_EXPR:
232 return clk_class;
234 case VA_ARG_EXPR:
235 return (CLASS_TYPE_P (TREE_TYPE (ref)) ? clk_class : clk_none);
237 case CALL_EXPR:
238 /* We can see calls outside of TARGET_EXPR in templates. */
239 if (CLASS_TYPE_P (TREE_TYPE (ref)))
240 return clk_class;
241 return clk_none;
243 case FUNCTION_DECL:
244 /* All functions (except non-static-member functions) are
245 lvalues. */
246 return (DECL_NONSTATIC_MEMBER_FUNCTION_P (ref)
247 ? clk_none : clk_ordinary);
249 case BASELINK:
250 /* We now represent a reference to a single static member function
251 with a BASELINK. */
252 /* This CONST_CAST is okay because BASELINK_FUNCTIONS returns
253 its argument unmodified and we assign it to a const_tree. */
254 return lvalue_kind (BASELINK_FUNCTIONS (CONST_CAST_TREE (ref)));
256 case NON_DEPENDENT_EXPR:
257 case PAREN_EXPR:
258 return lvalue_kind (TREE_OPERAND (ref, 0));
260 case VIEW_CONVERT_EXPR:
261 if (location_wrapper_p (ref))
262 return lvalue_kind (TREE_OPERAND (ref, 0));
263 /* Fallthrough. */
265 default:
266 if (!TREE_TYPE (ref))
267 return clk_none;
268 if (CLASS_TYPE_P (TREE_TYPE (ref))
269 || TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE)
270 return clk_class;
271 break;
274 /* If one operand is not an lvalue at all, then this expression is
275 not an lvalue. */
276 if (!op1_lvalue_kind || !op2_lvalue_kind)
277 return clk_none;
279 /* Otherwise, it's an lvalue, and it has all the odd properties
280 contributed by either operand. */
281 op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind;
282 /* It's not an ordinary lvalue if it involves any other kind. */
283 if ((op1_lvalue_kind & ~clk_ordinary) != clk_none)
284 op1_lvalue_kind &= ~clk_ordinary;
285 /* It can't be both a pseudo-lvalue and a non-addressable lvalue.
286 A COND_EXPR of those should be wrapped in a TARGET_EXPR. */
287 if ((op1_lvalue_kind & (clk_rvalueref|clk_class))
288 && (op1_lvalue_kind & (clk_bitfield|clk_packed)))
289 op1_lvalue_kind = clk_none;
290 return op1_lvalue_kind;
293 /* Returns the kind of lvalue that REF is, in the sense of [basic.lval]. */
295 cp_lvalue_kind
296 real_lvalue_p (const_tree ref)
298 cp_lvalue_kind kind = lvalue_kind (ref);
299 if (kind & (clk_rvalueref|clk_class))
300 return clk_none;
301 else
302 return kind;
305 /* c-common wants us to return bool. */
307 bool
308 lvalue_p (const_tree t)
310 return real_lvalue_p (t);
313 /* This differs from lvalue_p in that xvalues are included. */
315 bool
316 glvalue_p (const_tree ref)
318 cp_lvalue_kind kind = lvalue_kind (ref);
319 if (kind & clk_class)
320 return false;
321 else
322 return (kind != clk_none);
325 /* This differs from glvalue_p in that class prvalues are included. */
327 bool
328 obvalue_p (const_tree ref)
330 return (lvalue_kind (ref) != clk_none);
333 /* Returns true if REF is an xvalue (the result of dereferencing an rvalue
334 reference), false otherwise. */
336 bool
337 xvalue_p (const_tree ref)
339 return (lvalue_kind (ref) == clk_rvalueref);
342 /* True if REF is a bit-field. */
344 bool
345 bitfield_p (const_tree ref)
347 return (lvalue_kind (ref) & clk_bitfield);
350 /* C++-specific version of stabilize_reference. */
352 tree
353 cp_stabilize_reference (tree ref)
355 switch (TREE_CODE (ref))
357 case NON_DEPENDENT_EXPR:
358 /* We aren't actually evaluating this. */
359 return ref;
361 /* We need to treat specially anything stabilize_reference doesn't
362 handle specifically. */
363 case VAR_DECL:
364 case PARM_DECL:
365 case RESULT_DECL:
366 CASE_CONVERT:
367 case FLOAT_EXPR:
368 case FIX_TRUNC_EXPR:
369 case INDIRECT_REF:
370 case COMPONENT_REF:
371 case BIT_FIELD_REF:
372 case ARRAY_REF:
373 case ARRAY_RANGE_REF:
374 case ERROR_MARK:
375 break;
376 default:
377 cp_lvalue_kind kind = lvalue_kind (ref);
378 if ((kind & ~clk_class) != clk_none)
380 tree type = unlowered_expr_type (ref);
381 bool rval = !!(kind & clk_rvalueref);
382 type = cp_build_reference_type (type, rval);
383 /* This inhibits warnings in, eg, cxx_mark_addressable
384 (c++/60955). */
385 warning_sentinel s (extra_warnings);
386 ref = build_static_cast (type, ref, tf_error);
390 return stabilize_reference (ref);
393 /* Test whether DECL is a builtin that may appear in a
394 constant-expression. */
396 bool
397 builtin_valid_in_constant_expr_p (const_tree decl)
399 if (!(TREE_CODE (decl) == FUNCTION_DECL
400 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL))
401 /* Not a built-in. */
402 return false;
403 switch (DECL_FUNCTION_CODE (decl))
405 /* These always have constant results like the corresponding
406 macros/symbol. */
407 case BUILT_IN_FILE:
408 case BUILT_IN_FUNCTION:
409 case BUILT_IN_LINE:
411 /* The following built-ins are valid in constant expressions
412 when their arguments are. */
413 case BUILT_IN_ADD_OVERFLOW_P:
414 case BUILT_IN_SUB_OVERFLOW_P:
415 case BUILT_IN_MUL_OVERFLOW_P:
417 /* These have constant results even if their operands are
418 non-constant. */
419 case BUILT_IN_CONSTANT_P:
420 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
421 return true;
422 default:
423 return false;
427 /* Build a TARGET_EXPR, initializing the DECL with the VALUE. */
429 static tree
430 build_target_expr (tree decl, tree value, tsubst_flags_t complain)
432 tree t;
433 tree type = TREE_TYPE (decl);
435 value = mark_rvalue_use (value);
437 gcc_checking_assert (VOID_TYPE_P (TREE_TYPE (value))
438 || TREE_TYPE (decl) == TREE_TYPE (value)
439 /* On ARM ctors return 'this'. */
440 || (TYPE_PTR_P (TREE_TYPE (value))
441 && TREE_CODE (value) == CALL_EXPR)
442 || useless_type_conversion_p (TREE_TYPE (decl),
443 TREE_TYPE (value)));
445 if (complain & tf_no_cleanup)
446 /* The caller is building a new-expr and does not need a cleanup. */
447 t = NULL_TREE;
448 else
450 t = cxx_maybe_build_cleanup (decl, complain);
451 if (t == error_mark_node)
452 return error_mark_node;
454 t = build4 (TARGET_EXPR, type, decl, value, t, NULL_TREE);
455 if (EXPR_HAS_LOCATION (value))
456 SET_EXPR_LOCATION (t, EXPR_LOCATION (value));
457 /* We always set TREE_SIDE_EFFECTS so that expand_expr does not
458 ignore the TARGET_EXPR. If there really turn out to be no
459 side-effects, then the optimizer should be able to get rid of
460 whatever code is generated anyhow. */
461 TREE_SIDE_EFFECTS (t) = 1;
463 return t;
466 /* Return an undeclared local temporary of type TYPE for use in building a
467 TARGET_EXPR. */
469 static tree
470 build_local_temp (tree type)
472 tree slot = build_decl (input_location,
473 VAR_DECL, NULL_TREE, type);
474 DECL_ARTIFICIAL (slot) = 1;
475 DECL_IGNORED_P (slot) = 1;
476 DECL_CONTEXT (slot) = current_function_decl;
477 layout_decl (slot, 0);
478 return slot;
481 /* Set various status flags when building an AGGR_INIT_EXPR object T. */
483 static void
484 process_aggr_init_operands (tree t)
486 bool side_effects;
488 side_effects = TREE_SIDE_EFFECTS (t);
489 if (!side_effects)
491 int i, n;
492 n = TREE_OPERAND_LENGTH (t);
493 for (i = 1; i < n; i++)
495 tree op = TREE_OPERAND (t, i);
496 if (op && TREE_SIDE_EFFECTS (op))
498 side_effects = 1;
499 break;
503 TREE_SIDE_EFFECTS (t) = side_effects;
506 /* Build an AGGR_INIT_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE,
507 FN, and SLOT. NARGS is the number of call arguments which are specified
508 as a tree array ARGS. */
510 static tree
511 build_aggr_init_array (tree return_type, tree fn, tree slot, int nargs,
512 tree *args)
514 tree t;
515 int i;
517 t = build_vl_exp (AGGR_INIT_EXPR, nargs + 3);
518 TREE_TYPE (t) = return_type;
519 AGGR_INIT_EXPR_FN (t) = fn;
520 AGGR_INIT_EXPR_SLOT (t) = slot;
521 for (i = 0; i < nargs; i++)
522 AGGR_INIT_EXPR_ARG (t, i) = args[i];
523 process_aggr_init_operands (t);
524 return t;
527 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
528 target. TYPE is the type to be initialized.
530 Build an AGGR_INIT_EXPR to represent the initialization. This function
531 differs from build_cplus_new in that an AGGR_INIT_EXPR can only be used
532 to initialize another object, whereas a TARGET_EXPR can either
533 initialize another object or create its own temporary object, and as a
534 result building up a TARGET_EXPR requires that the type's destructor be
535 callable. */
537 tree
538 build_aggr_init_expr (tree type, tree init)
540 tree fn;
541 tree slot;
542 tree rval;
543 int is_ctor;
545 /* Don't build AGGR_INIT_EXPR in a template. */
546 if (processing_template_decl)
547 return init;
549 fn = cp_get_callee (init);
550 if (fn == NULL_TREE)
551 return convert (type, init);
553 is_ctor = (TREE_CODE (fn) == ADDR_EXPR
554 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
555 && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0)));
557 /* We split the CALL_EXPR into its function and its arguments here.
558 Then, in expand_expr, we put them back together. The reason for
559 this is that this expression might be a default argument
560 expression. In that case, we need a new temporary every time the
561 expression is used. That's what break_out_target_exprs does; it
562 replaces every AGGR_INIT_EXPR with a copy that uses a fresh
563 temporary slot. Then, expand_expr builds up a call-expression
564 using the new slot. */
566 /* If we don't need to use a constructor to create an object of this
567 type, don't mess with AGGR_INIT_EXPR. */
568 if (is_ctor || TREE_ADDRESSABLE (type))
570 slot = build_local_temp (type);
572 if (TREE_CODE (init) == CALL_EXPR)
574 rval = build_aggr_init_array (void_type_node, fn, slot,
575 call_expr_nargs (init),
576 CALL_EXPR_ARGP (init));
577 AGGR_INIT_FROM_THUNK_P (rval)
578 = CALL_FROM_THUNK_P (init);
580 else
582 rval = build_aggr_init_array (void_type_node, fn, slot,
583 aggr_init_expr_nargs (init),
584 AGGR_INIT_EXPR_ARGP (init));
585 AGGR_INIT_FROM_THUNK_P (rval)
586 = AGGR_INIT_FROM_THUNK_P (init);
588 TREE_SIDE_EFFECTS (rval) = 1;
589 AGGR_INIT_VIA_CTOR_P (rval) = is_ctor;
590 TREE_NOTHROW (rval) = TREE_NOTHROW (init);
591 CALL_EXPR_OPERATOR_SYNTAX (rval) = CALL_EXPR_OPERATOR_SYNTAX (init);
592 CALL_EXPR_ORDERED_ARGS (rval) = CALL_EXPR_ORDERED_ARGS (init);
593 CALL_EXPR_REVERSE_ARGS (rval) = CALL_EXPR_REVERSE_ARGS (init);
595 else
596 rval = init;
598 return rval;
601 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
602 target. TYPE is the type that this initialization should appear to
603 have.
605 Build an encapsulation of the initialization to perform
606 and return it so that it can be processed by language-independent
607 and language-specific expression expanders. */
609 tree
610 build_cplus_new (tree type, tree init, tsubst_flags_t complain)
612 tree rval = build_aggr_init_expr (type, init);
613 tree slot;
615 if (!complete_type_or_maybe_complain (type, init, complain))
616 return error_mark_node;
618 /* Make sure that we're not trying to create an instance of an
619 abstract class. */
620 if (abstract_virtuals_error_sfinae (NULL_TREE, type, complain))
621 return error_mark_node;
623 if (TREE_CODE (rval) == AGGR_INIT_EXPR)
624 slot = AGGR_INIT_EXPR_SLOT (rval);
625 else if (TREE_CODE (rval) == CALL_EXPR
626 || TREE_CODE (rval) == CONSTRUCTOR)
627 slot = build_local_temp (type);
628 else
629 return rval;
631 rval = build_target_expr (slot, rval, complain);
633 if (rval != error_mark_node)
634 TARGET_EXPR_IMPLICIT_P (rval) = 1;
636 return rval;
639 /* Subroutine of build_vec_init_expr: Build up a single element
640 intialization as a proxy for the full array initialization to get things
641 marked as used and any appropriate diagnostics.
643 Since we're deferring building the actual constructor calls until
644 gimplification time, we need to build one now and throw it away so
645 that the relevant constructor gets mark_used before cgraph decides
646 what functions are needed. Here we assume that init is either
647 NULL_TREE, void_type_node (indicating value-initialization), or
648 another array to copy. */
650 static tree
651 build_vec_init_elt (tree type, tree init, tsubst_flags_t complain)
653 tree inner_type = strip_array_types (type);
654 vec<tree, va_gc> *argvec;
656 if (integer_zerop (array_type_nelts_total (type))
657 || !CLASS_TYPE_P (inner_type))
658 /* No interesting initialization to do. */
659 return integer_zero_node;
660 else if (init == void_type_node)
661 return build_value_init (inner_type, complain);
663 gcc_assert (init == NULL_TREE
664 || (same_type_ignoring_top_level_qualifiers_p
665 (type, TREE_TYPE (init))));
667 argvec = make_tree_vector ();
668 if (init)
670 tree init_type = strip_array_types (TREE_TYPE (init));
671 tree dummy = build_dummy_object (init_type);
672 if (!lvalue_p (init))
673 dummy = move (dummy);
674 argvec->quick_push (dummy);
676 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
677 &argvec, inner_type, LOOKUP_NORMAL,
678 complain);
679 release_tree_vector (argvec);
681 /* For a trivial constructor, build_over_call creates a TARGET_EXPR. But
682 we don't want one here because we aren't creating a temporary. */
683 if (TREE_CODE (init) == TARGET_EXPR)
684 init = TARGET_EXPR_INITIAL (init);
686 return init;
689 /* Return a TARGET_EXPR which expresses the initialization of an array to
690 be named later, either default-initialization or copy-initialization
691 from another array of the same type. */
693 tree
694 build_vec_init_expr (tree type, tree init, tsubst_flags_t complain)
696 tree slot;
697 bool value_init = false;
698 tree elt_init = build_vec_init_elt (type, init, complain);
700 if (init == void_type_node)
702 value_init = true;
703 init = NULL_TREE;
706 slot = build_local_temp (type);
707 init = build2 (VEC_INIT_EXPR, type, slot, init);
708 TREE_SIDE_EFFECTS (init) = true;
709 SET_EXPR_LOCATION (init, input_location);
711 if (cxx_dialect >= cxx11
712 && potential_constant_expression (elt_init))
713 VEC_INIT_EXPR_IS_CONSTEXPR (init) = true;
714 VEC_INIT_EXPR_VALUE_INIT (init) = value_init;
716 return init;
719 /* Give a helpful diagnostic for a non-constexpr VEC_INIT_EXPR in a context
720 that requires a constant expression. */
722 void
723 diagnose_non_constexpr_vec_init (tree expr)
725 tree type = TREE_TYPE (VEC_INIT_EXPR_SLOT (expr));
726 tree init, elt_init;
727 if (VEC_INIT_EXPR_VALUE_INIT (expr))
728 init = void_type_node;
729 else
730 init = VEC_INIT_EXPR_INIT (expr);
732 elt_init = build_vec_init_elt (type, init, tf_warning_or_error);
733 require_potential_constant_expression (elt_init);
736 tree
737 build_array_copy (tree init)
739 return build_vec_init_expr (TREE_TYPE (init), init, tf_warning_or_error);
742 /* Build a TARGET_EXPR using INIT to initialize a new temporary of the
743 indicated TYPE. */
745 tree
746 build_target_expr_with_type (tree init, tree type, tsubst_flags_t complain)
748 gcc_assert (!VOID_TYPE_P (type));
750 if (TREE_CODE (init) == TARGET_EXPR
751 || init == error_mark_node)
752 return init;
753 else if (CLASS_TYPE_P (type) && type_has_nontrivial_copy_init (type)
754 && !VOID_TYPE_P (TREE_TYPE (init))
755 && TREE_CODE (init) != COND_EXPR
756 && TREE_CODE (init) != CONSTRUCTOR
757 && TREE_CODE (init) != VA_ARG_EXPR)
758 /* We need to build up a copy constructor call. A void initializer
759 means we're being called from bot_manip. COND_EXPR is a special
760 case because we already have copies on the arms and we don't want
761 another one here. A CONSTRUCTOR is aggregate initialization, which
762 is handled separately. A VA_ARG_EXPR is magic creation of an
763 aggregate; there's no additional work to be done. */
764 return force_rvalue (init, complain);
766 return force_target_expr (type, init, complain);
769 /* Like the above function, but without the checking. This function should
770 only be used by code which is deliberately trying to subvert the type
771 system, such as call_builtin_trap. Or build_over_call, to avoid
772 infinite recursion. */
774 tree
775 force_target_expr (tree type, tree init, tsubst_flags_t complain)
777 tree slot;
779 gcc_assert (!VOID_TYPE_P (type));
781 slot = build_local_temp (type);
782 return build_target_expr (slot, init, complain);
785 /* Like build_target_expr_with_type, but use the type of INIT. */
787 tree
788 get_target_expr_sfinae (tree init, tsubst_flags_t complain)
790 if (TREE_CODE (init) == AGGR_INIT_EXPR)
791 return build_target_expr (AGGR_INIT_EXPR_SLOT (init), init, complain);
792 else if (TREE_CODE (init) == VEC_INIT_EXPR)
793 return build_target_expr (VEC_INIT_EXPR_SLOT (init), init, complain);
794 else
796 init = convert_bitfield_to_declared_type (init);
797 return build_target_expr_with_type (init, TREE_TYPE (init), complain);
801 tree
802 get_target_expr (tree init)
804 return get_target_expr_sfinae (init, tf_warning_or_error);
807 /* If EXPR is a bitfield reference, convert it to the declared type of
808 the bitfield, and return the resulting expression. Otherwise,
809 return EXPR itself. */
811 tree
812 convert_bitfield_to_declared_type (tree expr)
814 tree bitfield_type;
816 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
817 if (bitfield_type)
818 expr = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type),
819 expr);
820 return expr;
823 /* EXPR is being used in an rvalue context. Return a version of EXPR
824 that is marked as an rvalue. */
826 tree
827 rvalue (tree expr)
829 tree type;
831 if (error_operand_p (expr))
832 return expr;
834 expr = mark_rvalue_use (expr);
836 /* [basic.lval]
838 Non-class rvalues always have cv-unqualified types. */
839 type = TREE_TYPE (expr);
840 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
841 type = cv_unqualified (type);
843 /* We need to do this for rvalue refs as well to get the right answer
844 from decltype; see c++/36628. */
845 if (!processing_template_decl && glvalue_p (expr))
846 expr = build1 (NON_LVALUE_EXPR, type, expr);
847 else if (type != TREE_TYPE (expr))
848 expr = build_nop (type, expr);
850 return expr;
854 struct cplus_array_info
856 tree type;
857 tree domain;
860 struct cplus_array_hasher : ggc_ptr_hash<tree_node>
862 typedef cplus_array_info *compare_type;
864 static hashval_t hash (tree t);
865 static bool equal (tree, cplus_array_info *);
868 /* Hash an ARRAY_TYPE. K is really of type `tree'. */
870 hashval_t
871 cplus_array_hasher::hash (tree t)
873 hashval_t hash;
875 hash = TYPE_UID (TREE_TYPE (t));
876 if (TYPE_DOMAIN (t))
877 hash ^= TYPE_UID (TYPE_DOMAIN (t));
878 return hash;
881 /* Compare two ARRAY_TYPEs. K1 is really of type `tree', K2 is really
882 of type `cplus_array_info*'. */
884 bool
885 cplus_array_hasher::equal (tree t1, cplus_array_info *t2)
887 return (TREE_TYPE (t1) == t2->type && TYPE_DOMAIN (t1) == t2->domain);
890 /* Hash table containing dependent array types, which are unsuitable for
891 the language-independent type hash table. */
892 static GTY (()) hash_table<cplus_array_hasher> *cplus_array_htab;
894 /* Build an ARRAY_TYPE without laying it out. */
896 static tree
897 build_min_array_type (tree elt_type, tree index_type)
899 tree t = cxx_make_type (ARRAY_TYPE);
900 TREE_TYPE (t) = elt_type;
901 TYPE_DOMAIN (t) = index_type;
902 return t;
905 /* Set TYPE_CANONICAL like build_array_type_1, but using
906 build_cplus_array_type. */
908 static void
909 set_array_type_canon (tree t, tree elt_type, tree index_type)
911 /* Set the canonical type for this new node. */
912 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
913 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
914 SET_TYPE_STRUCTURAL_EQUALITY (t);
915 else if (TYPE_CANONICAL (elt_type) != elt_type
916 || (index_type && TYPE_CANONICAL (index_type) != index_type))
917 TYPE_CANONICAL (t)
918 = build_cplus_array_type (TYPE_CANONICAL (elt_type),
919 index_type
920 ? TYPE_CANONICAL (index_type) : index_type);
921 else
922 TYPE_CANONICAL (t) = t;
925 /* Like build_array_type, but handle special C++ semantics: an array of a
926 variant element type is a variant of the array of the main variant of
927 the element type. */
929 tree
930 build_cplus_array_type (tree elt_type, tree index_type)
932 tree t;
934 if (elt_type == error_mark_node || index_type == error_mark_node)
935 return error_mark_node;
937 bool dependent = (uses_template_parms (elt_type)
938 || (index_type && uses_template_parms (index_type)));
940 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
941 /* Start with an array of the TYPE_MAIN_VARIANT. */
942 t = build_cplus_array_type (TYPE_MAIN_VARIANT (elt_type),
943 index_type);
944 else if (dependent)
946 /* Since type_hash_canon calls layout_type, we need to use our own
947 hash table. */
948 cplus_array_info cai;
949 hashval_t hash;
951 if (cplus_array_htab == NULL)
952 cplus_array_htab = hash_table<cplus_array_hasher>::create_ggc (61);
954 hash = TYPE_UID (elt_type);
955 if (index_type)
956 hash ^= TYPE_UID (index_type);
957 cai.type = elt_type;
958 cai.domain = index_type;
960 tree *e = cplus_array_htab->find_slot_with_hash (&cai, hash, INSERT);
961 if (*e)
962 /* We have found the type: we're done. */
963 return (tree) *e;
964 else
966 /* Build a new array type. */
967 t = build_min_array_type (elt_type, index_type);
969 /* Store it in the hash table. */
970 *e = t;
972 /* Set the canonical type for this new node. */
973 set_array_type_canon (t, elt_type, index_type);
976 else
978 bool typeless_storage
979 = (elt_type == unsigned_char_type_node
980 || elt_type == signed_char_type_node
981 || elt_type == char_type_node
982 || (TREE_CODE (elt_type) == ENUMERAL_TYPE
983 && TYPE_CONTEXT (elt_type) == std_node
984 && !strcmp ("byte", TYPE_NAME_STRING (elt_type))));
985 t = build_array_type (elt_type, index_type, typeless_storage);
988 /* Now check whether we already have this array variant. */
989 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
991 tree m = t;
992 for (t = m; t; t = TYPE_NEXT_VARIANT (t))
993 if (TREE_TYPE (t) == elt_type
994 && TYPE_NAME (t) == NULL_TREE
995 && TYPE_ATTRIBUTES (t) == NULL_TREE)
996 break;
997 if (!t)
999 t = build_min_array_type (elt_type, index_type);
1000 set_array_type_canon (t, elt_type, index_type);
1001 if (!dependent)
1003 layout_type (t);
1004 /* Make sure sizes are shared with the main variant.
1005 layout_type can't be called after setting TYPE_NEXT_VARIANT,
1006 as it will overwrite alignment etc. of all variants. */
1007 TYPE_SIZE (t) = TYPE_SIZE (m);
1008 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (m);
1009 TYPE_TYPELESS_STORAGE (t) = TYPE_TYPELESS_STORAGE (m);
1012 TYPE_MAIN_VARIANT (t) = m;
1013 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
1014 TYPE_NEXT_VARIANT (m) = t;
1018 /* Avoid spurious warnings with VLAs (c++/54583). */
1019 if (TYPE_SIZE (t) && EXPR_P (TYPE_SIZE (t)))
1020 TREE_NO_WARNING (TYPE_SIZE (t)) = 1;
1022 /* Push these needs up to the ARRAY_TYPE so that initialization takes
1023 place more easily. */
1024 bool needs_ctor = (TYPE_NEEDS_CONSTRUCTING (t)
1025 = TYPE_NEEDS_CONSTRUCTING (elt_type));
1026 bool needs_dtor = (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1027 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type));
1029 if (!dependent && t == TYPE_MAIN_VARIANT (t)
1030 && !COMPLETE_TYPE_P (t) && COMPLETE_TYPE_P (elt_type))
1032 /* The element type has been completed since the last time we saw
1033 this array type; update the layout and 'tor flags for any variants
1034 that need it. */
1035 layout_type (t);
1036 for (tree v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1038 TYPE_NEEDS_CONSTRUCTING (v) = needs_ctor;
1039 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (v) = needs_dtor;
1043 return t;
1046 /* Return an ARRAY_TYPE with element type ELT and length N. */
1048 tree
1049 build_array_of_n_type (tree elt, int n)
1051 return build_cplus_array_type (elt, build_index_type (size_int (n - 1)));
1054 /* True iff T is an N3639 array of runtime bound (VLA). These were
1055 approved for C++14 but then removed. */
1057 bool
1058 array_of_runtime_bound_p (tree t)
1060 if (!t || TREE_CODE (t) != ARRAY_TYPE)
1061 return false;
1062 if (variably_modified_type_p (TREE_TYPE (t), NULL_TREE))
1063 return false;
1064 tree dom = TYPE_DOMAIN (t);
1065 if (!dom)
1066 return false;
1067 tree max = TYPE_MAX_VALUE (dom);
1068 return (!potential_rvalue_constant_expression (max)
1069 || (!value_dependent_expression_p (max) && !TREE_CONSTANT (max)));
1072 /* Return a reference type node referring to TO_TYPE. If RVAL is
1073 true, return an rvalue reference type, otherwise return an lvalue
1074 reference type. If a type node exists, reuse it, otherwise create
1075 a new one. */
1076 tree
1077 cp_build_reference_type (tree to_type, bool rval)
1079 tree lvalue_ref, t;
1081 if (TREE_CODE (to_type) == REFERENCE_TYPE)
1083 rval = rval && TYPE_REF_IS_RVALUE (to_type);
1084 to_type = TREE_TYPE (to_type);
1087 lvalue_ref = build_reference_type (to_type);
1088 if (!rval)
1089 return lvalue_ref;
1091 /* This code to create rvalue reference types is based on and tied
1092 to the code creating lvalue reference types in the middle-end
1093 functions build_reference_type_for_mode and build_reference_type.
1095 It works by putting the rvalue reference type nodes after the
1096 lvalue reference nodes in the TYPE_NEXT_REF_TO linked list, so
1097 they will effectively be ignored by the middle end. */
1099 for (t = lvalue_ref; (t = TYPE_NEXT_REF_TO (t)); )
1100 if (TYPE_REF_IS_RVALUE (t))
1101 return t;
1103 t = build_distinct_type_copy (lvalue_ref);
1105 TYPE_REF_IS_RVALUE (t) = true;
1106 TYPE_NEXT_REF_TO (t) = TYPE_NEXT_REF_TO (lvalue_ref);
1107 TYPE_NEXT_REF_TO (lvalue_ref) = t;
1109 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
1110 SET_TYPE_STRUCTURAL_EQUALITY (t);
1111 else if (TYPE_CANONICAL (to_type) != to_type)
1112 TYPE_CANONICAL (t)
1113 = cp_build_reference_type (TYPE_CANONICAL (to_type), rval);
1114 else
1115 TYPE_CANONICAL (t) = t;
1117 layout_type (t);
1119 return t;
1123 /* Returns EXPR cast to rvalue reference type, like std::move. */
1125 tree
1126 move (tree expr)
1128 tree type = TREE_TYPE (expr);
1129 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
1130 type = cp_build_reference_type (type, /*rval*/true);
1131 return build_static_cast (type, expr, tf_warning_or_error);
1134 /* Used by the C++ front end to build qualified array types. However,
1135 the C version of this function does not properly maintain canonical
1136 types (which are not used in C). */
1137 tree
1138 c_build_qualified_type (tree type, int type_quals, tree /* orig_qual_type */,
1139 size_t /* orig_qual_indirect */)
1141 return cp_build_qualified_type (type, type_quals);
1145 /* Make a variant of TYPE, qualified with the TYPE_QUALS. Handles
1146 arrays correctly. In particular, if TYPE is an array of T's, and
1147 TYPE_QUALS is non-empty, returns an array of qualified T's.
1149 FLAGS determines how to deal with ill-formed qualifications. If
1150 tf_ignore_bad_quals is set, then bad qualifications are dropped
1151 (this is permitted if TYPE was introduced via a typedef or template
1152 type parameter). If bad qualifications are dropped and tf_warning
1153 is set, then a warning is issued for non-const qualifications. If
1154 tf_ignore_bad_quals is not set and tf_error is not set, we
1155 return error_mark_node. Otherwise, we issue an error, and ignore
1156 the qualifications.
1158 Qualification of a reference type is valid when the reference came
1159 via a typedef or template type argument. [dcl.ref] No such
1160 dispensation is provided for qualifying a function type. [dcl.fct]
1161 DR 295 queries this and the proposed resolution brings it into line
1162 with qualifying a reference. We implement the DR. We also behave
1163 in a similar manner for restricting non-pointer types. */
1165 tree
1166 cp_build_qualified_type_real (tree type,
1167 int type_quals,
1168 tsubst_flags_t complain)
1170 tree result;
1171 int bad_quals = TYPE_UNQUALIFIED;
1173 if (type == error_mark_node)
1174 return type;
1176 if (type_quals == cp_type_quals (type))
1177 return type;
1179 if (TREE_CODE (type) == ARRAY_TYPE)
1181 /* In C++, the qualification really applies to the array element
1182 type. Obtain the appropriately qualified element type. */
1183 tree t;
1184 tree element_type
1185 = cp_build_qualified_type_real (TREE_TYPE (type),
1186 type_quals,
1187 complain);
1189 if (element_type == error_mark_node)
1190 return error_mark_node;
1192 /* See if we already have an identically qualified type. Tests
1193 should be equivalent to those in check_qualified_type. */
1194 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1195 if (TREE_TYPE (t) == element_type
1196 && TYPE_NAME (t) == TYPE_NAME (type)
1197 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
1198 && attribute_list_equal (TYPE_ATTRIBUTES (t),
1199 TYPE_ATTRIBUTES (type)))
1200 break;
1202 if (!t)
1204 t = build_cplus_array_type (element_type, TYPE_DOMAIN (type));
1206 /* Keep the typedef name. */
1207 if (TYPE_NAME (t) != TYPE_NAME (type))
1209 t = build_variant_type_copy (t);
1210 TYPE_NAME (t) = TYPE_NAME (type);
1211 SET_TYPE_ALIGN (t, TYPE_ALIGN (type));
1212 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (type);
1216 /* Even if we already had this variant, we update
1217 TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case
1218 they changed since the variant was originally created.
1220 This seems hokey; if there is some way to use a previous
1221 variant *without* coming through here,
1222 TYPE_NEEDS_CONSTRUCTING will never be updated. */
1223 TYPE_NEEDS_CONSTRUCTING (t)
1224 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
1225 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1226 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
1227 return t;
1229 else if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
1231 tree t = PACK_EXPANSION_PATTERN (type);
1233 t = cp_build_qualified_type_real (t, type_quals, complain);
1234 return make_pack_expansion (t, complain);
1237 /* A reference or method type shall not be cv-qualified.
1238 [dcl.ref], [dcl.fct]. This used to be an error, but as of DR 295
1239 (in CD1) we always ignore extra cv-quals on functions. */
1240 if (type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)
1241 && (TREE_CODE (type) == REFERENCE_TYPE
1242 || TREE_CODE (type) == FUNCTION_TYPE
1243 || TREE_CODE (type) == METHOD_TYPE))
1245 if (TREE_CODE (type) == REFERENCE_TYPE)
1246 bad_quals |= type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1247 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1250 /* But preserve any function-cv-quals on a FUNCTION_TYPE. */
1251 if (TREE_CODE (type) == FUNCTION_TYPE)
1252 type_quals |= type_memfn_quals (type);
1254 /* A restrict-qualified type must be a pointer (or reference)
1255 to object or incomplete type. */
1256 if ((type_quals & TYPE_QUAL_RESTRICT)
1257 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1258 && TREE_CODE (type) != TYPENAME_TYPE
1259 && !POINTER_TYPE_P (type))
1261 bad_quals |= TYPE_QUAL_RESTRICT;
1262 type_quals &= ~TYPE_QUAL_RESTRICT;
1265 if (bad_quals == TYPE_UNQUALIFIED
1266 || (complain & tf_ignore_bad_quals))
1267 /*OK*/;
1268 else if (!(complain & tf_error))
1269 return error_mark_node;
1270 else
1272 tree bad_type = build_qualified_type (ptr_type_node, bad_quals);
1273 error ("%qV qualifiers cannot be applied to %qT",
1274 bad_type, type);
1277 /* Retrieve (or create) the appropriately qualified variant. */
1278 result = build_qualified_type (type, type_quals);
1280 /* Preserve exception specs and ref-qualifier since build_qualified_type
1281 doesn't know about them. */
1282 if (TREE_CODE (result) == FUNCTION_TYPE
1283 || TREE_CODE (result) == METHOD_TYPE)
1285 result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type));
1286 result = build_ref_qualified_type (result, type_memfn_rqual (type));
1289 return result;
1292 /* Return TYPE with const and volatile removed. */
1294 tree
1295 cv_unqualified (tree type)
1297 int quals;
1299 if (type == error_mark_node)
1300 return type;
1302 quals = cp_type_quals (type);
1303 quals &= ~(TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE);
1304 return cp_build_qualified_type (type, quals);
1307 /* Subroutine of strip_typedefs. We want to apply to RESULT the attributes
1308 from ATTRIBS that affect type identity, and no others. If any are not
1309 applied, set *remove_attributes to true. */
1311 static tree
1312 apply_identity_attributes (tree result, tree attribs, bool *remove_attributes)
1314 tree first_ident = NULL_TREE;
1315 tree new_attribs = NULL_TREE;
1316 tree *p = &new_attribs;
1318 if (OVERLOAD_TYPE_P (result))
1320 /* On classes and enums all attributes are ingrained. */
1321 gcc_assert (attribs == TYPE_ATTRIBUTES (result));
1322 return result;
1325 for (tree a = attribs; a; a = TREE_CHAIN (a))
1327 const attribute_spec *as
1328 = lookup_attribute_spec (get_attribute_name (a));
1329 if (as && as->affects_type_identity)
1331 if (!first_ident)
1332 first_ident = a;
1333 else if (first_ident == error_mark_node)
1335 *p = tree_cons (TREE_PURPOSE (a), TREE_VALUE (a), NULL_TREE);
1336 p = &TREE_CHAIN (*p);
1339 else if (first_ident)
1341 for (tree a2 = first_ident; a2; a2 = TREE_CHAIN (a2))
1343 *p = tree_cons (TREE_PURPOSE (a2), TREE_VALUE (a2), NULL_TREE);
1344 p = &TREE_CHAIN (*p);
1346 first_ident = error_mark_node;
1349 if (first_ident != error_mark_node)
1350 new_attribs = first_ident;
1352 if (first_ident == attribs)
1353 /* All attributes affected type identity. */;
1354 else
1355 *remove_attributes = true;
1357 return cp_build_type_attribute_variant (result, new_attribs);
1360 /* Builds a qualified variant of T that is not a typedef variant.
1361 E.g. consider the following declarations:
1362 typedef const int ConstInt;
1363 typedef ConstInt* PtrConstInt;
1364 If T is PtrConstInt, this function returns a type representing
1365 const int*.
1366 In other words, if T is a typedef, the function returns the underlying type.
1367 The cv-qualification and attributes of the type returned match the
1368 input type.
1369 They will always be compatible types.
1370 The returned type is built so that all of its subtypes
1371 recursively have their typedefs stripped as well.
1373 This is different from just returning TYPE_CANONICAL (T)
1374 Because of several reasons:
1375 * If T is a type that needs structural equality
1376 its TYPE_CANONICAL (T) will be NULL.
1377 * TYPE_CANONICAL (T) desn't carry type attributes
1378 and loses template parameter names.
1380 If REMOVE_ATTRIBUTES is non-null, also strip attributes that don't
1381 affect type identity, and set the referent to true if any were
1382 stripped. */
1384 tree
1385 strip_typedefs (tree t, bool *remove_attributes)
1387 tree result = NULL, type = NULL, t0 = NULL;
1389 if (!t || t == error_mark_node)
1390 return t;
1392 if (TREE_CODE (t) == TREE_LIST)
1394 bool changed = false;
1395 vec<tree,va_gc> *vec = make_tree_vector ();
1396 tree r = t;
1397 for (; t; t = TREE_CHAIN (t))
1399 gcc_assert (!TREE_PURPOSE (t));
1400 tree elt = strip_typedefs (TREE_VALUE (t), remove_attributes);
1401 if (elt != TREE_VALUE (t))
1402 changed = true;
1403 vec_safe_push (vec, elt);
1405 if (changed)
1406 r = build_tree_list_vec (vec);
1407 release_tree_vector (vec);
1408 return r;
1411 gcc_assert (TYPE_P (t));
1413 if (t == TYPE_CANONICAL (t))
1414 return t;
1416 if (dependent_alias_template_spec_p (t))
1417 /* DR 1558: However, if the template-id is dependent, subsequent
1418 template argument substitution still applies to the template-id. */
1419 return t;
1421 switch (TREE_CODE (t))
1423 case POINTER_TYPE:
1424 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1425 result = build_pointer_type (type);
1426 break;
1427 case REFERENCE_TYPE:
1428 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1429 result = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
1430 break;
1431 case OFFSET_TYPE:
1432 t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t), remove_attributes);
1433 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1434 result = build_offset_type (t0, type);
1435 break;
1436 case RECORD_TYPE:
1437 if (TYPE_PTRMEMFUNC_P (t))
1439 t0 = strip_typedefs (TYPE_PTRMEMFUNC_FN_TYPE (t), remove_attributes);
1440 result = build_ptrmemfunc_type (t0);
1442 break;
1443 case ARRAY_TYPE:
1444 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1445 t0 = strip_typedefs (TYPE_DOMAIN (t), remove_attributes);
1446 result = build_cplus_array_type (type, t0);
1447 break;
1448 case FUNCTION_TYPE:
1449 case METHOD_TYPE:
1451 tree arg_types = NULL, arg_node, arg_node2, arg_type;
1452 bool changed;
1454 /* Because we stomp on TREE_PURPOSE of TYPE_ARG_TYPES in many places
1455 around the compiler (e.g. cp_parser_late_parsing_default_args), we
1456 can't expect that re-hashing a function type will find a previous
1457 equivalent type, so try to reuse the input type if nothing has
1458 changed. If the type is itself a variant, that will change. */
1459 bool is_variant = typedef_variant_p (t);
1460 if (remove_attributes
1461 && (TYPE_ATTRIBUTES (t) || TYPE_USER_ALIGN (t)))
1462 is_variant = true;
1464 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1465 tree canon_spec = (flag_noexcept_type
1466 ? canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (t))
1467 : NULL_TREE);
1468 changed = (type != TREE_TYPE (t) || is_variant
1469 || TYPE_RAISES_EXCEPTIONS (t) != canon_spec);
1471 for (arg_node = TYPE_ARG_TYPES (t);
1472 arg_node;
1473 arg_node = TREE_CHAIN (arg_node))
1475 if (arg_node == void_list_node)
1476 break;
1477 arg_type = strip_typedefs (TREE_VALUE (arg_node),
1478 remove_attributes);
1479 gcc_assert (arg_type);
1480 if (arg_type == TREE_VALUE (arg_node) && !changed)
1481 continue;
1483 if (!changed)
1485 changed = true;
1486 for (arg_node2 = TYPE_ARG_TYPES (t);
1487 arg_node2 != arg_node;
1488 arg_node2 = TREE_CHAIN (arg_node2))
1489 arg_types
1490 = tree_cons (TREE_PURPOSE (arg_node2),
1491 TREE_VALUE (arg_node2), arg_types);
1494 arg_types
1495 = tree_cons (TREE_PURPOSE (arg_node), arg_type, arg_types);
1498 if (!changed)
1499 return t;
1501 if (arg_types)
1502 arg_types = nreverse (arg_types);
1504 /* A list of parameters not ending with an ellipsis
1505 must end with void_list_node. */
1506 if (arg_node)
1507 arg_types = chainon (arg_types, void_list_node);
1509 if (TREE_CODE (t) == METHOD_TYPE)
1511 tree class_type = TREE_TYPE (TREE_VALUE (arg_types));
1512 gcc_assert (class_type);
1513 result =
1514 build_method_type_directly (class_type, type,
1515 TREE_CHAIN (arg_types));
1516 result
1517 = build_ref_qualified_type (result, type_memfn_rqual (t));
1519 else
1521 result = build_function_type (type,
1522 arg_types);
1523 result = apply_memfn_quals (result,
1524 type_memfn_quals (t),
1525 type_memfn_rqual (t));
1528 if (canon_spec)
1529 result = build_exception_variant (result, canon_spec);
1530 if (TYPE_HAS_LATE_RETURN_TYPE (t))
1531 TYPE_HAS_LATE_RETURN_TYPE (result) = 1;
1533 break;
1534 case TYPENAME_TYPE:
1536 bool changed = false;
1537 tree fullname = TYPENAME_TYPE_FULLNAME (t);
1538 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
1539 && TREE_OPERAND (fullname, 1))
1541 tree args = TREE_OPERAND (fullname, 1);
1542 tree new_args = copy_node (args);
1543 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
1545 tree arg = TREE_VEC_ELT (args, i);
1546 tree strip_arg;
1547 if (TYPE_P (arg))
1548 strip_arg = strip_typedefs (arg, remove_attributes);
1549 else
1550 strip_arg = strip_typedefs_expr (arg, remove_attributes);
1551 TREE_VEC_ELT (new_args, i) = strip_arg;
1552 if (strip_arg != arg)
1553 changed = true;
1555 if (changed)
1557 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_args)
1558 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
1559 fullname
1560 = lookup_template_function (TREE_OPERAND (fullname, 0),
1561 new_args);
1563 else
1564 ggc_free (new_args);
1566 tree ctx = strip_typedefs (TYPE_CONTEXT (t), remove_attributes);
1567 if (!changed && ctx == TYPE_CONTEXT (t) && !typedef_variant_p (t))
1568 return t;
1569 tree name = fullname;
1570 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
1571 name = TREE_OPERAND (fullname, 0);
1572 /* Use build_typename_type rather than make_typename_type because we
1573 don't want to resolve it here, just strip typedefs. */
1574 result = build_typename_type (ctx, name, fullname, typename_type);
1576 break;
1577 case DECLTYPE_TYPE:
1578 result = strip_typedefs_expr (DECLTYPE_TYPE_EXPR (t),
1579 remove_attributes);
1580 if (result == DECLTYPE_TYPE_EXPR (t))
1581 result = NULL_TREE;
1582 else
1583 result = (finish_decltype_type
1584 (result,
1585 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t),
1586 tf_none));
1587 break;
1588 case UNDERLYING_TYPE:
1589 type = strip_typedefs (UNDERLYING_TYPE_TYPE (t), remove_attributes);
1590 result = finish_underlying_type (type);
1591 break;
1592 default:
1593 break;
1596 if (!result)
1598 if (typedef_variant_p (t))
1600 /* Explicitly get the underlying type, as TYPE_MAIN_VARIANT doesn't
1601 strip typedefs with attributes. */
1602 result = TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (TYPE_NAME (t)));
1603 result = strip_typedefs (result);
1605 else
1606 result = TYPE_MAIN_VARIANT (t);
1608 gcc_assert (!typedef_variant_p (result));
1610 if (COMPLETE_TYPE_P (result) && !COMPLETE_TYPE_P (t))
1611 /* If RESULT is complete and T isn't, it's likely the case that T
1612 is a variant of RESULT which hasn't been updated yet. Skip the
1613 attribute handling. */;
1614 else
1616 if (TYPE_USER_ALIGN (t) != TYPE_USER_ALIGN (result)
1617 || TYPE_ALIGN (t) != TYPE_ALIGN (result))
1619 gcc_assert (TYPE_USER_ALIGN (t));
1620 if (remove_attributes)
1621 *remove_attributes = true;
1622 else
1624 if (TYPE_ALIGN (t) == TYPE_ALIGN (result))
1625 result = build_variant_type_copy (result);
1626 else
1627 result = build_aligned_type (result, TYPE_ALIGN (t));
1628 TYPE_USER_ALIGN (result) = true;
1632 if (TYPE_ATTRIBUTES (t))
1634 if (remove_attributes)
1635 result = apply_identity_attributes (result, TYPE_ATTRIBUTES (t),
1636 remove_attributes);
1637 else
1638 result = cp_build_type_attribute_variant (result,
1639 TYPE_ATTRIBUTES (t));
1643 return cp_build_qualified_type (result, cp_type_quals (t));
1646 /* Like strip_typedefs above, but works on expressions, so that in
1648 template<class T> struct A
1650 typedef T TT;
1651 B<sizeof(TT)> b;
1654 sizeof(TT) is replaced by sizeof(T). */
1656 tree
1657 strip_typedefs_expr (tree t, bool *remove_attributes)
1659 unsigned i,n;
1660 tree r, type, *ops;
1661 enum tree_code code;
1663 if (t == NULL_TREE || t == error_mark_node)
1664 return t;
1666 if (DECL_P (t) || CONSTANT_CLASS_P (t))
1667 return t;
1669 /* Some expressions have type operands, so let's handle types here rather
1670 than check TYPE_P in multiple places below. */
1671 if (TYPE_P (t))
1672 return strip_typedefs (t, remove_attributes);
1674 code = TREE_CODE (t);
1675 switch (code)
1677 case IDENTIFIER_NODE:
1678 case TEMPLATE_PARM_INDEX:
1679 case OVERLOAD:
1680 case BASELINK:
1681 case ARGUMENT_PACK_SELECT:
1682 return t;
1684 case TRAIT_EXPR:
1686 tree type1 = strip_typedefs (TRAIT_EXPR_TYPE1 (t), remove_attributes);
1687 tree type2 = strip_typedefs (TRAIT_EXPR_TYPE2 (t), remove_attributes);
1688 if (type1 == TRAIT_EXPR_TYPE1 (t)
1689 && type2 == TRAIT_EXPR_TYPE2 (t))
1690 return t;
1691 r = copy_node (t);
1692 TRAIT_EXPR_TYPE1 (r) = type1;
1693 TRAIT_EXPR_TYPE2 (r) = type2;
1694 return r;
1697 case TREE_LIST:
1699 vec<tree, va_gc> *vec = make_tree_vector ();
1700 bool changed = false;
1701 tree it;
1702 for (it = t; it; it = TREE_CHAIN (it))
1704 tree val = strip_typedefs_expr (TREE_VALUE (t), remove_attributes);
1705 vec_safe_push (vec, val);
1706 if (val != TREE_VALUE (t))
1707 changed = true;
1708 gcc_assert (TREE_PURPOSE (it) == NULL_TREE);
1710 if (changed)
1712 r = NULL_TREE;
1713 FOR_EACH_VEC_ELT_REVERSE (*vec, i, it)
1714 r = tree_cons (NULL_TREE, it, r);
1716 else
1717 r = t;
1718 release_tree_vector (vec);
1719 return r;
1722 case TREE_VEC:
1724 bool changed = false;
1725 vec<tree, va_gc> *vec = make_tree_vector ();
1726 n = TREE_VEC_LENGTH (t);
1727 vec_safe_reserve (vec, n);
1728 for (i = 0; i < n; ++i)
1730 tree op = strip_typedefs_expr (TREE_VEC_ELT (t, i),
1731 remove_attributes);
1732 vec->quick_push (op);
1733 if (op != TREE_VEC_ELT (t, i))
1734 changed = true;
1736 if (changed)
1738 r = copy_node (t);
1739 for (i = 0; i < n; ++i)
1740 TREE_VEC_ELT (r, i) = (*vec)[i];
1741 NON_DEFAULT_TEMPLATE_ARGS_COUNT (r)
1742 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
1744 else
1745 r = t;
1746 release_tree_vector (vec);
1747 return r;
1750 case CONSTRUCTOR:
1752 bool changed = false;
1753 vec<constructor_elt, va_gc> *vec
1754 = vec_safe_copy (CONSTRUCTOR_ELTS (t));
1755 n = CONSTRUCTOR_NELTS (t);
1756 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1757 for (i = 0; i < n; ++i)
1759 constructor_elt *e = &(*vec)[i];
1760 tree op = strip_typedefs_expr (e->value, remove_attributes);
1761 if (op != e->value)
1763 changed = true;
1764 e->value = op;
1766 gcc_checking_assert
1767 (e->index == strip_typedefs_expr (e->index, remove_attributes));
1770 if (!changed && type == TREE_TYPE (t))
1772 vec_free (vec);
1773 return t;
1775 else
1777 r = copy_node (t);
1778 TREE_TYPE (r) = type;
1779 CONSTRUCTOR_ELTS (r) = vec;
1780 return r;
1784 case LAMBDA_EXPR:
1785 error ("lambda-expression in a constant expression");
1786 return error_mark_node;
1788 default:
1789 break;
1792 gcc_assert (EXPR_P (t));
1794 n = cp_tree_operand_length (t);
1795 ops = XALLOCAVEC (tree, n);
1796 type = TREE_TYPE (t);
1798 switch (code)
1800 CASE_CONVERT:
1801 case IMPLICIT_CONV_EXPR:
1802 case DYNAMIC_CAST_EXPR:
1803 case STATIC_CAST_EXPR:
1804 case CONST_CAST_EXPR:
1805 case REINTERPRET_CAST_EXPR:
1806 case CAST_EXPR:
1807 case NEW_EXPR:
1808 type = strip_typedefs (type, remove_attributes);
1809 /* fallthrough */
1811 default:
1812 for (i = 0; i < n; ++i)
1813 ops[i] = strip_typedefs_expr (TREE_OPERAND (t, i), remove_attributes);
1814 break;
1817 /* If nothing changed, return t. */
1818 for (i = 0; i < n; ++i)
1819 if (ops[i] != TREE_OPERAND (t, i))
1820 break;
1821 if (i == n && type == TREE_TYPE (t))
1822 return t;
1824 r = copy_node (t);
1825 TREE_TYPE (r) = type;
1826 for (i = 0; i < n; ++i)
1827 TREE_OPERAND (r, i) = ops[i];
1828 return r;
1831 /* Makes a copy of BINFO and TYPE, which is to be inherited into a
1832 graph dominated by T. If BINFO is NULL, TYPE is a dependent base,
1833 and we do a shallow copy. If BINFO is non-NULL, we do a deep copy.
1834 VIRT indicates whether TYPE is inherited virtually or not.
1835 IGO_PREV points at the previous binfo of the inheritance graph
1836 order chain. The newly copied binfo's TREE_CHAIN forms this
1837 ordering.
1839 The CLASSTYPE_VBASECLASSES vector of T is constructed in the
1840 correct order. That is in the order the bases themselves should be
1841 constructed in.
1843 The BINFO_INHERITANCE of a virtual base class points to the binfo
1844 of the most derived type. ??? We could probably change this so that
1845 BINFO_INHERITANCE becomes synonymous with BINFO_PRIMARY, and hence
1846 remove a field. They currently can only differ for primary virtual
1847 virtual bases. */
1849 tree
1850 copy_binfo (tree binfo, tree type, tree t, tree *igo_prev, int virt)
1852 tree new_binfo;
1854 if (virt)
1856 /* See if we've already made this virtual base. */
1857 new_binfo = binfo_for_vbase (type, t);
1858 if (new_binfo)
1859 return new_binfo;
1862 new_binfo = make_tree_binfo (binfo ? BINFO_N_BASE_BINFOS (binfo) : 0);
1863 BINFO_TYPE (new_binfo) = type;
1865 /* Chain it into the inheritance graph. */
1866 TREE_CHAIN (*igo_prev) = new_binfo;
1867 *igo_prev = new_binfo;
1869 if (binfo && !BINFO_DEPENDENT_BASE_P (binfo))
1871 int ix;
1872 tree base_binfo;
1874 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), type));
1876 BINFO_OFFSET (new_binfo) = BINFO_OFFSET (binfo);
1877 BINFO_VIRTUALS (new_binfo) = BINFO_VIRTUALS (binfo);
1879 /* We do not need to copy the accesses, as they are read only. */
1880 BINFO_BASE_ACCESSES (new_binfo) = BINFO_BASE_ACCESSES (binfo);
1882 /* Recursively copy base binfos of BINFO. */
1883 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1885 tree new_base_binfo;
1886 new_base_binfo = copy_binfo (base_binfo, BINFO_TYPE (base_binfo),
1887 t, igo_prev,
1888 BINFO_VIRTUAL_P (base_binfo));
1890 if (!BINFO_INHERITANCE_CHAIN (new_base_binfo))
1891 BINFO_INHERITANCE_CHAIN (new_base_binfo) = new_binfo;
1892 BINFO_BASE_APPEND (new_binfo, new_base_binfo);
1895 else
1896 BINFO_DEPENDENT_BASE_P (new_binfo) = 1;
1898 if (virt)
1900 /* Push it onto the list after any virtual bases it contains
1901 will have been pushed. */
1902 CLASSTYPE_VBASECLASSES (t)->quick_push (new_binfo);
1903 BINFO_VIRTUAL_P (new_binfo) = 1;
1904 BINFO_INHERITANCE_CHAIN (new_binfo) = TYPE_BINFO (t);
1907 return new_binfo;
1910 /* Hashing of lists so that we don't make duplicates.
1911 The entry point is `list_hash_canon'. */
1913 struct list_proxy
1915 tree purpose;
1916 tree value;
1917 tree chain;
1920 struct list_hasher : ggc_ptr_hash<tree_node>
1922 typedef list_proxy *compare_type;
1924 static hashval_t hash (tree);
1925 static bool equal (tree, list_proxy *);
1928 /* Now here is the hash table. When recording a list, it is added
1929 to the slot whose index is the hash code mod the table size.
1930 Note that the hash table is used for several kinds of lists.
1931 While all these live in the same table, they are completely independent,
1932 and the hash code is computed differently for each of these. */
1934 static GTY (()) hash_table<list_hasher> *list_hash_table;
1936 /* Compare ENTRY (an entry in the hash table) with DATA (a list_proxy
1937 for a node we are thinking about adding). */
1939 bool
1940 list_hasher::equal (tree t, list_proxy *proxy)
1942 return (TREE_VALUE (t) == proxy->value
1943 && TREE_PURPOSE (t) == proxy->purpose
1944 && TREE_CHAIN (t) == proxy->chain);
1947 /* Compute a hash code for a list (chain of TREE_LIST nodes
1948 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
1949 TREE_COMMON slots), by adding the hash codes of the individual entries. */
1951 static hashval_t
1952 list_hash_pieces (tree purpose, tree value, tree chain)
1954 hashval_t hashcode = 0;
1956 if (chain)
1957 hashcode += TREE_HASH (chain);
1959 if (value)
1960 hashcode += TREE_HASH (value);
1961 else
1962 hashcode += 1007;
1963 if (purpose)
1964 hashcode += TREE_HASH (purpose);
1965 else
1966 hashcode += 1009;
1967 return hashcode;
1970 /* Hash an already existing TREE_LIST. */
1972 hashval_t
1973 list_hasher::hash (tree t)
1975 return list_hash_pieces (TREE_PURPOSE (t),
1976 TREE_VALUE (t),
1977 TREE_CHAIN (t));
1980 /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
1981 object for an identical list if one already exists. Otherwise, build a
1982 new one, and record it as the canonical object. */
1984 tree
1985 hash_tree_cons (tree purpose, tree value, tree chain)
1987 int hashcode = 0;
1988 tree *slot;
1989 struct list_proxy proxy;
1991 /* Hash the list node. */
1992 hashcode = list_hash_pieces (purpose, value, chain);
1993 /* Create a proxy for the TREE_LIST we would like to create. We
1994 don't actually create it so as to avoid creating garbage. */
1995 proxy.purpose = purpose;
1996 proxy.value = value;
1997 proxy.chain = chain;
1998 /* See if it is already in the table. */
1999 slot = list_hash_table->find_slot_with_hash (&proxy, hashcode, INSERT);
2000 /* If not, create a new node. */
2001 if (!*slot)
2002 *slot = tree_cons (purpose, value, chain);
2003 return (tree) *slot;
2006 /* Constructor for hashed lists. */
2008 tree
2009 hash_tree_chain (tree value, tree chain)
2011 return hash_tree_cons (NULL_TREE, value, chain);
2014 void
2015 debug_binfo (tree elem)
2017 HOST_WIDE_INT n;
2018 tree virtuals;
2020 fprintf (stderr, "type \"%s\", offset = " HOST_WIDE_INT_PRINT_DEC
2021 "\nvtable type:\n",
2022 TYPE_NAME_STRING (BINFO_TYPE (elem)),
2023 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
2024 debug_tree (BINFO_TYPE (elem));
2025 if (BINFO_VTABLE (elem))
2026 fprintf (stderr, "vtable decl \"%s\"\n",
2027 IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem))));
2028 else
2029 fprintf (stderr, "no vtable decl yet\n");
2030 fprintf (stderr, "virtuals:\n");
2031 virtuals = BINFO_VIRTUALS (elem);
2032 n = 0;
2034 while (virtuals)
2036 tree fndecl = TREE_VALUE (virtuals);
2037 fprintf (stderr, "%s [%ld =? %ld]\n",
2038 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
2039 (long) n, (long) TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
2040 ++n;
2041 virtuals = TREE_CHAIN (virtuals);
2045 /* Build a representation for the qualified name SCOPE::NAME. TYPE is
2046 the type of the result expression, if known, or NULL_TREE if the
2047 resulting expression is type-dependent. If TEMPLATE_P is true,
2048 NAME is known to be a template because the user explicitly used the
2049 "template" keyword after the "::".
2051 All SCOPE_REFs should be built by use of this function. */
2053 tree
2054 build_qualified_name (tree type, tree scope, tree name, bool template_p)
2056 tree t;
2057 if (type == error_mark_node
2058 || scope == error_mark_node
2059 || name == error_mark_node)
2060 return error_mark_node;
2061 gcc_assert (TREE_CODE (name) != SCOPE_REF);
2062 t = build2 (SCOPE_REF, type, scope, name);
2063 QUALIFIED_NAME_IS_TEMPLATE (t) = template_p;
2064 PTRMEM_OK_P (t) = true;
2065 if (type)
2066 t = convert_from_reference (t);
2067 return t;
2070 /* Like check_qualified_type, but also check ref-qualifier and exception
2071 specification. */
2073 static bool
2074 cp_check_qualified_type (const_tree cand, const_tree base, int type_quals,
2075 cp_ref_qualifier rqual, tree raises)
2077 return (TYPE_QUALS (cand) == type_quals
2078 && check_base_type (cand, base)
2079 && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (cand),
2080 ce_exact)
2081 && type_memfn_rqual (cand) == rqual);
2084 /* Build the FUNCTION_TYPE or METHOD_TYPE with the ref-qualifier RQUAL. */
2086 tree
2087 build_ref_qualified_type (tree type, cp_ref_qualifier rqual)
2089 tree t;
2091 if (rqual == type_memfn_rqual (type))
2092 return type;
2094 int type_quals = TYPE_QUALS (type);
2095 tree raises = TYPE_RAISES_EXCEPTIONS (type);
2096 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2097 if (cp_check_qualified_type (t, type, type_quals, rqual, raises))
2098 return t;
2100 t = build_variant_type_copy (type);
2101 switch (rqual)
2103 case REF_QUAL_RVALUE:
2104 FUNCTION_RVALUE_QUALIFIED (t) = 1;
2105 FUNCTION_REF_QUALIFIED (t) = 1;
2106 break;
2107 case REF_QUAL_LVALUE:
2108 FUNCTION_RVALUE_QUALIFIED (t) = 0;
2109 FUNCTION_REF_QUALIFIED (t) = 1;
2110 break;
2111 default:
2112 FUNCTION_REF_QUALIFIED (t) = 0;
2113 break;
2116 if (TYPE_STRUCTURAL_EQUALITY_P (type))
2117 /* Propagate structural equality. */
2118 SET_TYPE_STRUCTURAL_EQUALITY (t);
2119 else if (TYPE_CANONICAL (type) != type)
2120 /* Build the underlying canonical type, since it is different
2121 from TYPE. */
2122 TYPE_CANONICAL (t) = build_ref_qualified_type (TYPE_CANONICAL (type),
2123 rqual);
2124 else
2125 /* T is its own canonical type. */
2126 TYPE_CANONICAL (t) = t;
2128 return t;
2131 /* Cache of free ovl nodes. Uses OVL_FUNCTION for chaining. */
2132 static GTY((deletable)) tree ovl_cache;
2134 /* Make a raw overload node containing FN. */
2136 tree
2137 ovl_make (tree fn, tree next)
2139 tree result = ovl_cache;
2141 if (result)
2143 ovl_cache = OVL_FUNCTION (result);
2144 /* Zap the flags. */
2145 memset (result, 0, sizeof (tree_base));
2146 TREE_SET_CODE (result, OVERLOAD);
2148 else
2149 result = make_node (OVERLOAD);
2151 if (TREE_CODE (fn) == OVERLOAD)
2152 OVL_NESTED_P (result) = true;
2154 TREE_TYPE (result) = (next || TREE_CODE (fn) == TEMPLATE_DECL
2155 ? unknown_type_node : TREE_TYPE (fn));
2156 OVL_FUNCTION (result) = fn;
2157 OVL_CHAIN (result) = next;
2158 return result;
2161 static tree
2162 ovl_copy (tree ovl)
2164 tree result = ovl_cache;
2166 if (result)
2168 ovl_cache = OVL_FUNCTION (result);
2169 /* Zap the flags. */
2170 memset (result, 0, sizeof (tree_base));
2171 TREE_SET_CODE (result, OVERLOAD);
2173 else
2174 result = make_node (OVERLOAD);
2176 gcc_checking_assert (!OVL_NESTED_P (ovl) && OVL_USED_P (ovl));
2177 TREE_TYPE (result) = TREE_TYPE (ovl);
2178 OVL_FUNCTION (result) = OVL_FUNCTION (ovl);
2179 OVL_CHAIN (result) = OVL_CHAIN (ovl);
2180 OVL_HIDDEN_P (result) = OVL_HIDDEN_P (ovl);
2181 OVL_USING_P (result) = OVL_USING_P (ovl);
2182 OVL_LOOKUP_P (result) = OVL_LOOKUP_P (ovl);
2184 return result;
2187 /* Add FN to the (potentially NULL) overload set OVL. USING_P is
2188 true, if FN is via a using declaration. We also pay attention to
2189 DECL_HIDDEN. Overloads are ordered as hidden, using, regular. */
2191 tree
2192 ovl_insert (tree fn, tree maybe_ovl, bool using_p)
2194 bool copying = false; /* Checking use only. */
2195 bool hidden_p = DECL_HIDDEN_P (fn);
2196 int weight = (hidden_p << 1) | (using_p << 0);
2198 tree result = NULL_TREE;
2199 tree insert_after = NULL_TREE;
2201 /* Find insertion point. */
2202 while (maybe_ovl && TREE_CODE (maybe_ovl) == OVERLOAD
2203 && (weight < ((OVL_HIDDEN_P (maybe_ovl) << 1)
2204 | (OVL_USING_P (maybe_ovl) << 0))))
2206 gcc_checking_assert (!OVL_LOOKUP_P (maybe_ovl)
2207 && (!copying || OVL_USED_P (maybe_ovl)));
2208 if (OVL_USED_P (maybe_ovl))
2210 copying = true;
2211 maybe_ovl = ovl_copy (maybe_ovl);
2212 if (insert_after)
2213 OVL_CHAIN (insert_after) = maybe_ovl;
2215 if (!result)
2216 result = maybe_ovl;
2217 insert_after = maybe_ovl;
2218 maybe_ovl = OVL_CHAIN (maybe_ovl);
2221 tree trail = fn;
2222 if (maybe_ovl || using_p || hidden_p || TREE_CODE (fn) == TEMPLATE_DECL)
2224 trail = ovl_make (fn, maybe_ovl);
2225 if (hidden_p)
2226 OVL_HIDDEN_P (trail) = true;
2227 if (using_p)
2228 OVL_USING_P (trail) = true;
2231 if (insert_after)
2233 OVL_CHAIN (insert_after) = trail;
2234 TREE_TYPE (insert_after) = unknown_type_node;
2236 else
2237 result = trail;
2239 return result;
2242 /* Skip any hidden names at the beginning of OVL. */
2244 tree
2245 ovl_skip_hidden (tree ovl)
2247 for (;
2248 ovl && TREE_CODE (ovl) == OVERLOAD && OVL_HIDDEN_P (ovl);
2249 ovl = OVL_CHAIN (ovl))
2250 gcc_checking_assert (DECL_HIDDEN_P (OVL_FUNCTION (ovl)));
2252 if (ovl && TREE_CODE (ovl) != OVERLOAD && DECL_HIDDEN_P (ovl))
2254 /* Any hidden functions should have been wrapped in an
2255 overload, but injected friend classes will not. */
2256 gcc_checking_assert (!DECL_DECLARES_FUNCTION_P (ovl));
2257 ovl = NULL_TREE;
2260 return ovl;
2263 /* NODE is an OVL_HIDDEN_P node which is now revealed. */
2265 tree
2266 ovl_iterator::reveal_node (tree overload, tree node)
2268 /* We cannot have returned NODE as part of a lookup overload, so it
2269 cannot be USED. */
2270 gcc_checking_assert (!OVL_USED_P (node));
2272 OVL_HIDDEN_P (node) = false;
2273 if (tree chain = OVL_CHAIN (node))
2274 if (TREE_CODE (chain) == OVERLOAD
2275 && (OVL_USING_P (chain) || OVL_HIDDEN_P (chain)))
2277 /* The node needs moving, and the simplest way is to remove it
2278 and reinsert. */
2279 overload = remove_node (overload, node);
2280 overload = ovl_insert (OVL_FUNCTION (node), overload);
2282 return overload;
2285 /* NODE is on the overloads of OVL. Remove it. If a predecessor is
2286 OVL_USED_P we must copy OVL nodes, because those are immutable.
2287 The removed node is unaltered and may continue to be iterated
2288 from (i.e. it is safe to remove a node from an overload one is
2289 currently iterating over). */
2291 tree
2292 ovl_iterator::remove_node (tree overload, tree node)
2294 bool copying = false; /* Checking use only. */
2296 tree *slot = &overload;
2297 while (*slot != node)
2299 tree probe = *slot;
2300 gcc_checking_assert (!OVL_LOOKUP_P (probe)
2301 && (!copying || OVL_USED_P (probe)));
2302 if (OVL_USED_P (probe))
2304 copying = true;
2305 probe = ovl_copy (probe);
2306 *slot = probe;
2309 slot = &OVL_CHAIN (probe);
2312 /* Stitch out NODE. We don't have to worry about now making a
2313 singleton overload (and consequently maybe setting its type),
2314 because all uses of this function will be followed by inserting a
2315 new node that must follow the place we've cut this out from. */
2316 if (TREE_CODE (node) != OVERLOAD)
2317 /* Cloned inherited ctors don't mark themselves as via_using. */
2318 *slot = NULL_TREE;
2319 else
2320 *slot = OVL_CHAIN (node);
2322 return overload;
2325 /* Mark or unmark a lookup set. */
2327 void
2328 lookup_mark (tree ovl, bool val)
2330 for (lkp_iterator iter (ovl); iter; ++iter)
2332 gcc_checking_assert (LOOKUP_SEEN_P (*iter) != val);
2333 LOOKUP_SEEN_P (*iter) = val;
2337 /* Add a set of new FNS into a lookup. */
2339 tree
2340 lookup_add (tree fns, tree lookup)
2342 if (lookup || TREE_CODE (fns) == TEMPLATE_DECL)
2344 lookup = ovl_make (fns, lookup);
2345 OVL_LOOKUP_P (lookup) = true;
2347 else
2348 lookup = fns;
2350 return lookup;
2353 /* FNS is a new overload set, add them to LOOKUP, if they are not
2354 already present there. */
2356 tree
2357 lookup_maybe_add (tree fns, tree lookup, bool deduping)
2359 if (deduping)
2360 for (tree next, probe = fns; probe; probe = next)
2362 tree fn = probe;
2363 next = NULL_TREE;
2365 if (TREE_CODE (probe) == OVERLOAD)
2367 fn = OVL_FUNCTION (probe);
2368 next = OVL_CHAIN (probe);
2371 if (!LOOKUP_SEEN_P (fn))
2372 LOOKUP_SEEN_P (fn) = true;
2373 else
2375 /* This function was already seen. Insert all the
2376 predecessors onto the lookup. */
2377 for (; fns != probe; fns = OVL_CHAIN (fns))
2379 lookup = lookup_add (OVL_FUNCTION (fns), lookup);
2380 /* Propagate OVL_USING, but OVL_HIDDEN doesn't matter. */
2381 if (OVL_USING_P (fns))
2382 OVL_USING_P (lookup) = true;
2385 /* And now skip this function. */
2386 fns = next;
2390 if (fns)
2391 /* We ended in a set of new functions. Add them all in one go. */
2392 lookup = lookup_add (fns, lookup);
2394 return lookup;
2397 /* Regular overload OVL is part of a kept lookup. Mark the nodes on
2398 it as immutable. */
2400 static void
2401 ovl_used (tree ovl)
2403 for (;
2404 ovl && TREE_CODE (ovl) == OVERLOAD
2405 && !OVL_USED_P (ovl);
2406 ovl = OVL_CHAIN (ovl))
2408 gcc_checking_assert (!OVL_LOOKUP_P (ovl));
2409 OVL_USED_P (ovl) = true;
2413 /* If KEEP is true, preserve the contents of a lookup so that it is
2414 available for a later instantiation. Otherwise release the LOOKUP
2415 nodes for reuse. */
2417 void
2418 lookup_keep (tree lookup, bool keep)
2420 for (;
2421 lookup && TREE_CODE (lookup) == OVERLOAD
2422 && OVL_LOOKUP_P (lookup) && !OVL_USED_P (lookup);
2423 lookup = OVL_CHAIN (lookup))
2424 if (keep)
2426 OVL_USED_P (lookup) = true;
2427 ovl_used (OVL_FUNCTION (lookup));
2429 else
2431 OVL_FUNCTION (lookup) = ovl_cache;
2432 ovl_cache = lookup;
2435 if (keep)
2436 ovl_used (lookup);
2439 /* LIST is a TREE_LIST whose TREE_VALUEs may be OVERLOADS that need
2440 keeping, or may be ignored. */
2442 void
2443 lookup_list_keep (tree list, bool keep)
2445 for (; list; list = TREE_CHAIN (list))
2447 tree v = TREE_VALUE (list);
2448 if (TREE_CODE (v) == OVERLOAD)
2449 lookup_keep (v, keep);
2453 /* Returns nonzero if X is an expression for a (possibly overloaded)
2454 function. If "f" is a function or function template, "f", "c->f",
2455 "c.f", "C::f", and "f<int>" will all be considered possibly
2456 overloaded functions. Returns 2 if the function is actually
2457 overloaded, i.e., if it is impossible to know the type of the
2458 function without performing overload resolution. */
2461 is_overloaded_fn (tree x)
2463 /* A baselink is also considered an overloaded function. */
2464 if (TREE_CODE (x) == OFFSET_REF
2465 || TREE_CODE (x) == COMPONENT_REF)
2466 x = TREE_OPERAND (x, 1);
2467 x = MAYBE_BASELINK_FUNCTIONS (x);
2468 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
2469 x = TREE_OPERAND (x, 0);
2471 if (DECL_FUNCTION_TEMPLATE_P (OVL_FIRST (x))
2472 || (TREE_CODE (x) == OVERLOAD && !OVL_SINGLE_P (x)))
2473 return 2;
2475 return (TREE_CODE (x) == FUNCTION_DECL
2476 || TREE_CODE (x) == OVERLOAD);
2479 /* X is the CALL_EXPR_FN of a CALL_EXPR. If X represents a dependent name
2480 (14.6.2), return the IDENTIFIER_NODE for that name. Otherwise, return
2481 NULL_TREE. */
2483 tree
2484 dependent_name (tree x)
2486 if (identifier_p (x))
2487 return x;
2488 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
2489 x = TREE_OPERAND (x, 0);
2490 if (TREE_CODE (x) == OVERLOAD || TREE_CODE (x) == FUNCTION_DECL)
2491 return OVL_NAME (x);
2492 return NULL_TREE;
2495 /* Returns true iff X is an expression for an overloaded function
2496 whose type cannot be known without performing overload
2497 resolution. */
2499 bool
2500 really_overloaded_fn (tree x)
2502 return is_overloaded_fn (x) == 2;
2505 /* Get the overload set FROM refers to. */
2507 tree
2508 get_fns (tree from)
2510 /* A baselink is also considered an overloaded function. */
2511 if (TREE_CODE (from) == OFFSET_REF
2512 || TREE_CODE (from) == COMPONENT_REF)
2513 from = TREE_OPERAND (from, 1);
2514 if (BASELINK_P (from))
2515 from = BASELINK_FUNCTIONS (from);
2516 if (TREE_CODE (from) == TEMPLATE_ID_EXPR)
2517 from = TREE_OPERAND (from, 0);
2518 gcc_assert (TREE_CODE (from) == OVERLOAD
2519 || TREE_CODE (from) == FUNCTION_DECL);
2520 return from;
2523 /* Return the first function of the overload set FROM refers to. */
2525 tree
2526 get_first_fn (tree from)
2528 return OVL_FIRST (get_fns (from));
2531 /* Return the scope where the overloaded functions OVL were found. */
2533 tree
2534 ovl_scope (tree ovl)
2536 if (TREE_CODE (ovl) == OFFSET_REF
2537 || TREE_CODE (ovl) == COMPONENT_REF)
2538 ovl = TREE_OPERAND (ovl, 1);
2539 if (TREE_CODE (ovl) == BASELINK)
2540 return BINFO_TYPE (BASELINK_BINFO (ovl));
2541 if (TREE_CODE (ovl) == TEMPLATE_ID_EXPR)
2542 ovl = TREE_OPERAND (ovl, 0);
2543 /* Skip using-declarations. */
2544 lkp_iterator iter (ovl);
2546 ovl = *iter;
2547 while (iter.using_p () && ++iter);
2549 return CP_DECL_CONTEXT (ovl);
2552 #define PRINT_RING_SIZE 4
2554 static const char *
2555 cxx_printable_name_internal (tree decl, int v, bool translate)
2557 static unsigned int uid_ring[PRINT_RING_SIZE];
2558 static char *print_ring[PRINT_RING_SIZE];
2559 static bool trans_ring[PRINT_RING_SIZE];
2560 static int ring_counter;
2561 int i;
2563 /* Only cache functions. */
2564 if (v < 2
2565 || TREE_CODE (decl) != FUNCTION_DECL
2566 || DECL_LANG_SPECIFIC (decl) == 0)
2567 return lang_decl_name (decl, v, translate);
2569 /* See if this print name is lying around. */
2570 for (i = 0; i < PRINT_RING_SIZE; i++)
2571 if (uid_ring[i] == DECL_UID (decl) && translate == trans_ring[i])
2572 /* yes, so return it. */
2573 return print_ring[i];
2575 if (++ring_counter == PRINT_RING_SIZE)
2576 ring_counter = 0;
2578 if (current_function_decl != NULL_TREE)
2580 /* There may be both translated and untranslated versions of the
2581 name cached. */
2582 for (i = 0; i < 2; i++)
2584 if (uid_ring[ring_counter] == DECL_UID (current_function_decl))
2585 ring_counter += 1;
2586 if (ring_counter == PRINT_RING_SIZE)
2587 ring_counter = 0;
2589 gcc_assert (uid_ring[ring_counter] != DECL_UID (current_function_decl));
2592 free (print_ring[ring_counter]);
2594 print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v, translate));
2595 uid_ring[ring_counter] = DECL_UID (decl);
2596 trans_ring[ring_counter] = translate;
2597 return print_ring[ring_counter];
2600 const char *
2601 cxx_printable_name (tree decl, int v)
2603 return cxx_printable_name_internal (decl, v, false);
2606 const char *
2607 cxx_printable_name_translate (tree decl, int v)
2609 return cxx_printable_name_internal (decl, v, true);
2612 /* Return the canonical version of exception-specification RAISES for a C++17
2613 function type, for use in type comparison and building TYPE_CANONICAL. */
2615 tree
2616 canonical_eh_spec (tree raises)
2618 if (raises == NULL_TREE)
2619 return raises;
2620 else if (DEFERRED_NOEXCEPT_SPEC_P (raises)
2621 || uses_template_parms (raises)
2622 || uses_template_parms (TREE_PURPOSE (raises)))
2623 /* Keep a dependent or deferred exception specification. */
2624 return raises;
2625 else if (nothrow_spec_p (raises))
2626 /* throw() -> noexcept. */
2627 return noexcept_true_spec;
2628 else
2629 /* For C++17 type matching, anything else -> nothing. */
2630 return NULL_TREE;
2633 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
2634 listed in RAISES. */
2636 tree
2637 build_exception_variant (tree type, tree raises)
2639 tree v;
2640 int type_quals;
2642 if (comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (type), ce_exact))
2643 return type;
2645 type_quals = TYPE_QUALS (type);
2646 cp_ref_qualifier rqual = type_memfn_rqual (type);
2647 for (v = TYPE_MAIN_VARIANT (type); v; v = TYPE_NEXT_VARIANT (v))
2648 if (cp_check_qualified_type (v, type, type_quals, rqual, raises))
2649 return v;
2651 /* Need to build a new variant. */
2652 v = build_variant_type_copy (type);
2653 TYPE_RAISES_EXCEPTIONS (v) = raises;
2655 if (!flag_noexcept_type)
2656 /* The exception-specification is not part of the canonical type. */
2657 return v;
2659 /* Canonicalize the exception specification. */
2660 tree cr = canonical_eh_spec (raises);
2662 if (TYPE_STRUCTURAL_EQUALITY_P (type))
2663 /* Propagate structural equality. */
2664 SET_TYPE_STRUCTURAL_EQUALITY (v);
2665 else if (TYPE_CANONICAL (type) != type || cr != raises)
2666 /* Build the underlying canonical type, since it is different
2667 from TYPE. */
2668 TYPE_CANONICAL (v) = build_exception_variant (TYPE_CANONICAL (type), cr);
2669 else
2670 /* T is its own canonical type. */
2671 TYPE_CANONICAL (v) = v;
2673 return v;
2676 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new
2677 BOUND_TEMPLATE_TEMPLATE_PARM bound with NEWARGS as its template
2678 arguments. */
2680 tree
2681 bind_template_template_parm (tree t, tree newargs)
2683 tree decl = TYPE_NAME (t);
2684 tree t2;
2686 t2 = cxx_make_type (BOUND_TEMPLATE_TEMPLATE_PARM);
2687 decl = build_decl (input_location,
2688 TYPE_DECL, DECL_NAME (decl), NULL_TREE);
2690 /* These nodes have to be created to reflect new TYPE_DECL and template
2691 arguments. */
2692 TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t));
2693 TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl;
2694 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
2695 = build_template_info (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t), newargs);
2697 TREE_TYPE (decl) = t2;
2698 TYPE_NAME (t2) = decl;
2699 TYPE_STUB_DECL (t2) = decl;
2700 TYPE_SIZE (t2) = 0;
2701 SET_TYPE_STRUCTURAL_EQUALITY (t2);
2703 return t2;
2706 /* Called from count_trees via walk_tree. */
2708 static tree
2709 count_trees_r (tree *tp, int *walk_subtrees, void *data)
2711 ++*((int *) data);
2713 if (TYPE_P (*tp))
2714 *walk_subtrees = 0;
2716 return NULL_TREE;
2719 /* Debugging function for measuring the rough complexity of a tree
2720 representation. */
2723 count_trees (tree t)
2725 int n_trees = 0;
2726 cp_walk_tree_without_duplicates (&t, count_trees_r, &n_trees);
2727 return n_trees;
2730 /* Called from verify_stmt_tree via walk_tree. */
2732 static tree
2733 verify_stmt_tree_r (tree* tp, int * /*walk_subtrees*/, void* data)
2735 tree t = *tp;
2736 hash_table<nofree_ptr_hash <tree_node> > *statements
2737 = static_cast <hash_table<nofree_ptr_hash <tree_node> > *> (data);
2738 tree_node **slot;
2740 if (!STATEMENT_CODE_P (TREE_CODE (t)))
2741 return NULL_TREE;
2743 /* If this statement is already present in the hash table, then
2744 there is a circularity in the statement tree. */
2745 gcc_assert (!statements->find (t));
2747 slot = statements->find_slot (t, INSERT);
2748 *slot = t;
2750 return NULL_TREE;
2753 /* Debugging function to check that the statement T has not been
2754 corrupted. For now, this function simply checks that T contains no
2755 circularities. */
2757 void
2758 verify_stmt_tree (tree t)
2760 hash_table<nofree_ptr_hash <tree_node> > statements (37);
2761 cp_walk_tree (&t, verify_stmt_tree_r, &statements, NULL);
2764 /* Check if the type T depends on a type with no linkage and if so, return
2765 it. If RELAXED_P then do not consider a class type declared within
2766 a vague-linkage function to have no linkage. */
2768 tree
2769 no_linkage_check (tree t, bool relaxed_p)
2771 tree r;
2773 /* There's no point in checking linkage on template functions; we
2774 can't know their complete types. */
2775 if (processing_template_decl)
2776 return NULL_TREE;
2778 switch (TREE_CODE (t))
2780 case RECORD_TYPE:
2781 if (TYPE_PTRMEMFUNC_P (t))
2782 goto ptrmem;
2783 /* Lambda types that don't have mangling scope have no linkage. We
2784 check CLASSTYPE_LAMBDA_EXPR for error_mark_node because
2785 when we get here from pushtag none of the lambda information is
2786 set up yet, so we want to assume that the lambda has linkage and
2787 fix it up later if not. */
2788 if (CLASSTYPE_LAMBDA_EXPR (t)
2789 && CLASSTYPE_LAMBDA_EXPR (t) != error_mark_node
2790 && LAMBDA_TYPE_EXTRA_SCOPE (t) == NULL_TREE)
2791 return t;
2792 /* Fall through. */
2793 case UNION_TYPE:
2794 if (!CLASS_TYPE_P (t))
2795 return NULL_TREE;
2796 /* Fall through. */
2797 case ENUMERAL_TYPE:
2798 /* Only treat unnamed types as having no linkage if they're at
2799 namespace scope. This is core issue 966. */
2800 if (TYPE_UNNAMED_P (t) && TYPE_NAMESPACE_SCOPE_P (t))
2801 return t;
2803 for (r = CP_TYPE_CONTEXT (t); ; )
2805 /* If we're a nested type of a !TREE_PUBLIC class, we might not
2806 have linkage, or we might just be in an anonymous namespace.
2807 If we're in a TREE_PUBLIC class, we have linkage. */
2808 if (TYPE_P (r) && !TREE_PUBLIC (TYPE_NAME (r)))
2809 return no_linkage_check (TYPE_CONTEXT (t), relaxed_p);
2810 else if (TREE_CODE (r) == FUNCTION_DECL)
2812 if (!relaxed_p || !vague_linkage_p (r))
2813 return t;
2814 else
2815 r = CP_DECL_CONTEXT (r);
2817 else
2818 break;
2821 return NULL_TREE;
2823 case ARRAY_TYPE:
2824 case POINTER_TYPE:
2825 case REFERENCE_TYPE:
2826 case VECTOR_TYPE:
2827 return no_linkage_check (TREE_TYPE (t), relaxed_p);
2829 case OFFSET_TYPE:
2830 ptrmem:
2831 r = no_linkage_check (TYPE_PTRMEM_POINTED_TO_TYPE (t),
2832 relaxed_p);
2833 if (r)
2834 return r;
2835 return no_linkage_check (TYPE_PTRMEM_CLASS_TYPE (t), relaxed_p);
2837 case METHOD_TYPE:
2838 case FUNCTION_TYPE:
2840 tree parm = TYPE_ARG_TYPES (t);
2841 if (TREE_CODE (t) == METHOD_TYPE)
2842 /* The 'this' pointer isn't interesting; a method has the same
2843 linkage (or lack thereof) as its enclosing class. */
2844 parm = TREE_CHAIN (parm);
2845 for (;
2846 parm && parm != void_list_node;
2847 parm = TREE_CHAIN (parm))
2849 r = no_linkage_check (TREE_VALUE (parm), relaxed_p);
2850 if (r)
2851 return r;
2853 return no_linkage_check (TREE_TYPE (t), relaxed_p);
2856 default:
2857 return NULL_TREE;
2861 extern int depth_reached;
2863 void
2864 cxx_print_statistics (void)
2866 print_template_statistics ();
2867 if (GATHER_STATISTICS)
2868 fprintf (stderr, "maximum template instantiation depth reached: %d\n",
2869 depth_reached);
2872 /* Return, as an INTEGER_CST node, the number of elements for TYPE
2873 (which is an ARRAY_TYPE). This counts only elements of the top
2874 array. */
2876 tree
2877 array_type_nelts_top (tree type)
2879 return fold_build2_loc (input_location,
2880 PLUS_EXPR, sizetype,
2881 array_type_nelts (type),
2882 size_one_node);
2885 /* Return, as an INTEGER_CST node, the number of elements for TYPE
2886 (which is an ARRAY_TYPE). This one is a recursive count of all
2887 ARRAY_TYPEs that are clumped together. */
2889 tree
2890 array_type_nelts_total (tree type)
2892 tree sz = array_type_nelts_top (type);
2893 type = TREE_TYPE (type);
2894 while (TREE_CODE (type) == ARRAY_TYPE)
2896 tree n = array_type_nelts_top (type);
2897 sz = fold_build2_loc (input_location,
2898 MULT_EXPR, sizetype, sz, n);
2899 type = TREE_TYPE (type);
2901 return sz;
2904 /* Called from break_out_target_exprs via mapcar. */
2906 static tree
2907 bot_manip (tree* tp, int* walk_subtrees, void* data)
2909 splay_tree target_remap = ((splay_tree) data);
2910 tree t = *tp;
2912 if (!TYPE_P (t) && TREE_CONSTANT (t) && !TREE_SIDE_EFFECTS (t))
2914 /* There can't be any TARGET_EXPRs or their slot variables below this
2915 point. But we must make a copy, in case subsequent processing
2916 alters any part of it. For example, during gimplification a cast
2917 of the form (T) &X::f (where "f" is a member function) will lead
2918 to replacing the PTRMEM_CST for &X::f with a VAR_DECL. */
2919 *walk_subtrees = 0;
2920 *tp = unshare_expr (t);
2921 return NULL_TREE;
2923 if (TREE_CODE (t) == TARGET_EXPR)
2925 tree u;
2927 if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
2929 u = build_cplus_new (TREE_TYPE (t), TREE_OPERAND (t, 1),
2930 tf_warning_or_error);
2931 if (u == error_mark_node)
2932 return u;
2933 if (AGGR_INIT_ZERO_FIRST (TREE_OPERAND (t, 1)))
2934 AGGR_INIT_ZERO_FIRST (TREE_OPERAND (u, 1)) = true;
2936 else
2937 u = build_target_expr_with_type (TREE_OPERAND (t, 1), TREE_TYPE (t),
2938 tf_warning_or_error);
2940 TARGET_EXPR_IMPLICIT_P (u) = TARGET_EXPR_IMPLICIT_P (t);
2941 TARGET_EXPR_LIST_INIT_P (u) = TARGET_EXPR_LIST_INIT_P (t);
2942 TARGET_EXPR_DIRECT_INIT_P (u) = TARGET_EXPR_DIRECT_INIT_P (t);
2944 /* Map the old variable to the new one. */
2945 splay_tree_insert (target_remap,
2946 (splay_tree_key) TREE_OPERAND (t, 0),
2947 (splay_tree_value) TREE_OPERAND (u, 0));
2949 TREE_OPERAND (u, 1) = break_out_target_exprs (TREE_OPERAND (u, 1));
2950 if (TREE_OPERAND (u, 1) == error_mark_node)
2951 return error_mark_node;
2953 /* Replace the old expression with the new version. */
2954 *tp = u;
2955 /* We don't have to go below this point; the recursive call to
2956 break_out_target_exprs will have handled anything below this
2957 point. */
2958 *walk_subtrees = 0;
2959 return NULL_TREE;
2961 if (TREE_CODE (*tp) == SAVE_EXPR)
2963 t = *tp;
2964 splay_tree_node n = splay_tree_lookup (target_remap,
2965 (splay_tree_key) t);
2966 if (n)
2968 *tp = (tree)n->value;
2969 *walk_subtrees = 0;
2971 else
2973 copy_tree_r (tp, walk_subtrees, NULL);
2974 splay_tree_insert (target_remap,
2975 (splay_tree_key)t,
2976 (splay_tree_value)*tp);
2977 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
2978 splay_tree_insert (target_remap,
2979 (splay_tree_key)*tp,
2980 (splay_tree_value)*tp);
2982 return NULL_TREE;
2985 /* Make a copy of this node. */
2986 t = copy_tree_r (tp, walk_subtrees, NULL);
2987 if (TREE_CODE (*tp) == CALL_EXPR)
2989 set_flags_from_callee (*tp);
2991 /* builtin_LINE and builtin_FILE get the location where the default
2992 argument is expanded, not where the call was written. */
2993 tree callee = get_callee_fndecl (*tp);
2994 if (callee && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
2995 switch (DECL_FUNCTION_CODE (callee))
2997 case BUILT_IN_FILE:
2998 case BUILT_IN_LINE:
2999 SET_EXPR_LOCATION (*tp, input_location);
3000 default:
3001 break;
3004 return t;
3007 /* Replace all remapped VAR_DECLs in T with their new equivalents.
3008 DATA is really a splay-tree mapping old variables to new
3009 variables. */
3011 static tree
3012 bot_replace (tree* t, int* /*walk_subtrees*/, void* data)
3014 splay_tree target_remap = ((splay_tree) data);
3016 if (VAR_P (*t))
3018 splay_tree_node n = splay_tree_lookup (target_remap,
3019 (splay_tree_key) *t);
3020 if (n)
3021 *t = (tree) n->value;
3023 else if (TREE_CODE (*t) == PARM_DECL
3024 && DECL_NAME (*t) == this_identifier
3025 && !DECL_CONTEXT (*t))
3027 /* In an NSDMI we need to replace the 'this' parameter we used for
3028 parsing with the real one for this function. */
3029 *t = current_class_ptr;
3031 else if (TREE_CODE (*t) == CONVERT_EXPR
3032 && CONVERT_EXPR_VBASE_PATH (*t))
3034 /* In an NSDMI build_base_path defers building conversions to virtual
3035 bases, and we handle it here. */
3036 tree basetype = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (*t)));
3037 vec<tree, va_gc> *vbases = CLASSTYPE_VBASECLASSES (current_class_type);
3038 int i; tree binfo;
3039 FOR_EACH_VEC_SAFE_ELT (vbases, i, binfo)
3040 if (BINFO_TYPE (binfo) == basetype)
3041 break;
3042 *t = build_base_path (PLUS_EXPR, TREE_OPERAND (*t, 0), binfo, true,
3043 tf_warning_or_error);
3046 return NULL_TREE;
3049 /* When we parse a default argument expression, we may create
3050 temporary variables via TARGET_EXPRs. When we actually use the
3051 default-argument expression, we make a copy of the expression
3052 and replace the temporaries with appropriate local versions. */
3054 tree
3055 break_out_target_exprs (tree t)
3057 static int target_remap_count;
3058 static splay_tree target_remap;
3060 if (!target_remap_count++)
3061 target_remap = splay_tree_new (splay_tree_compare_pointers,
3062 /*splay_tree_delete_key_fn=*/NULL,
3063 /*splay_tree_delete_value_fn=*/NULL);
3064 if (cp_walk_tree (&t, bot_manip, target_remap, NULL) == error_mark_node)
3065 t = error_mark_node;
3066 cp_walk_tree (&t, bot_replace, target_remap, NULL);
3068 if (!--target_remap_count)
3070 splay_tree_delete (target_remap);
3071 target_remap = NULL;
3074 return t;
3077 /* Build an expression for the subobject of OBJ at CONSTRUCTOR index INDEX,
3078 which we expect to have type TYPE. */
3080 tree
3081 build_ctor_subob_ref (tree index, tree type, tree obj)
3083 if (index == NULL_TREE)
3084 /* Can't refer to a particular member of a vector. */
3085 obj = NULL_TREE;
3086 else if (TREE_CODE (index) == INTEGER_CST)
3087 obj = cp_build_array_ref (input_location, obj, index, tf_none);
3088 else
3089 obj = build_class_member_access_expr (obj, index, NULL_TREE,
3090 /*reference*/false, tf_none);
3091 if (obj)
3093 tree objtype = TREE_TYPE (obj);
3094 if (TREE_CODE (objtype) == ARRAY_TYPE && !TYPE_DOMAIN (objtype))
3096 /* When the destination object refers to a flexible array member
3097 verify that it matches the type of the source object except
3098 for its domain and qualifiers. */
3099 gcc_assert (comptypes (TYPE_MAIN_VARIANT (type),
3100 TYPE_MAIN_VARIANT (objtype),
3101 COMPARE_REDECLARATION));
3103 else
3104 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, objtype));
3107 return obj;
3110 struct replace_placeholders_t
3112 tree obj; /* The object to be substituted for a PLACEHOLDER_EXPR. */
3113 tree exp; /* The outermost exp. */
3114 bool seen; /* Whether we've encountered a PLACEHOLDER_EXPR. */
3115 hash_set<tree> *pset; /* To avoid walking same trees multiple times. */
3118 /* Like substitute_placeholder_in_expr, but handle C++ tree codes and
3119 build up subexpressions as we go deeper. */
3121 static tree
3122 replace_placeholders_r (tree* t, int* walk_subtrees, void* data_)
3124 replace_placeholders_t *d = static_cast<replace_placeholders_t*>(data_);
3125 tree obj = d->obj;
3127 if (TYPE_P (*t) || TREE_CONSTANT (*t))
3129 *walk_subtrees = false;
3130 return NULL_TREE;
3133 switch (TREE_CODE (*t))
3135 case PLACEHOLDER_EXPR:
3137 tree x = obj;
3138 for (; !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (*t),
3139 TREE_TYPE (x));
3140 x = TREE_OPERAND (x, 0))
3141 gcc_assert (TREE_CODE (x) == COMPONENT_REF);
3142 *t = unshare_expr (x);
3143 *walk_subtrees = false;
3144 d->seen = true;
3146 break;
3148 case CONSTRUCTOR:
3150 constructor_elt *ce;
3151 vec<constructor_elt,va_gc> *v = CONSTRUCTOR_ELTS (*t);
3152 /* Don't walk into CONSTRUCTOR_PLACEHOLDER_BOUNDARY ctors
3153 other than the d->exp one, those have PLACEHOLDER_EXPRs
3154 related to another object. */
3155 if ((CONSTRUCTOR_PLACEHOLDER_BOUNDARY (*t)
3156 && *t != d->exp)
3157 || d->pset->add (*t))
3159 *walk_subtrees = false;
3160 return NULL_TREE;
3162 for (unsigned i = 0; vec_safe_iterate (v, i, &ce); ++i)
3164 tree *valp = &ce->value;
3165 tree type = TREE_TYPE (*valp);
3166 tree subob = obj;
3168 if (TREE_CODE (*valp) == CONSTRUCTOR
3169 && AGGREGATE_TYPE_P (type))
3171 /* If we're looking at the initializer for OBJ, then build
3172 a sub-object reference. If we're looking at an
3173 initializer for another object, just pass OBJ down. */
3174 if (same_type_ignoring_top_level_qualifiers_p
3175 (TREE_TYPE (*t), TREE_TYPE (obj)))
3176 subob = build_ctor_subob_ref (ce->index, type, obj);
3177 if (TREE_CODE (*valp) == TARGET_EXPR)
3178 valp = &TARGET_EXPR_INITIAL (*valp);
3180 d->obj = subob;
3181 cp_walk_tree (valp, replace_placeholders_r, data_, NULL);
3182 d->obj = obj;
3184 *walk_subtrees = false;
3185 break;
3188 default:
3189 if (d->pset->add (*t))
3190 *walk_subtrees = false;
3191 break;
3194 return NULL_TREE;
3197 /* Replace PLACEHOLDER_EXPRs in EXP with object OBJ. SEEN_P is set if
3198 a PLACEHOLDER_EXPR has been encountered. */
3200 tree
3201 replace_placeholders (tree exp, tree obj, bool *seen_p)
3203 /* This is only relevant for C++14. */
3204 if (cxx_dialect < cxx14)
3205 return exp;
3207 /* If the object isn't a (member of a) class, do nothing. */
3208 tree op0 = obj;
3209 while (TREE_CODE (op0) == COMPONENT_REF)
3210 op0 = TREE_OPERAND (op0, 0);
3211 if (!CLASS_TYPE_P (strip_array_types (TREE_TYPE (op0))))
3212 return exp;
3214 tree *tp = &exp;
3215 if (TREE_CODE (exp) == TARGET_EXPR)
3216 tp = &TARGET_EXPR_INITIAL (exp);
3217 hash_set<tree> pset;
3218 replace_placeholders_t data = { obj, *tp, false, &pset };
3219 cp_walk_tree (tp, replace_placeholders_r, &data, NULL);
3220 if (seen_p)
3221 *seen_p = data.seen;
3222 return exp;
3225 /* Callback function for find_placeholders. */
3227 static tree
3228 find_placeholders_r (tree *t, int *walk_subtrees, void *)
3230 if (TYPE_P (*t) || TREE_CONSTANT (*t))
3232 *walk_subtrees = false;
3233 return NULL_TREE;
3236 switch (TREE_CODE (*t))
3238 case PLACEHOLDER_EXPR:
3239 return *t;
3241 case CONSTRUCTOR:
3242 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (*t))
3243 *walk_subtrees = false;
3244 break;
3246 default:
3247 break;
3250 return NULL_TREE;
3253 /* Return true if EXP contains a PLACEHOLDER_EXPR. Don't walk into
3254 ctors with CONSTRUCTOR_PLACEHOLDER_BOUNDARY flag set. */
3256 bool
3257 find_placeholders (tree exp)
3259 /* This is only relevant for C++14. */
3260 if (cxx_dialect < cxx14)
3261 return false;
3263 return cp_walk_tree_without_duplicates (&exp, find_placeholders_r, NULL);
3266 /* Similar to `build_nt', but for template definitions of dependent
3267 expressions */
3269 tree
3270 build_min_nt_loc (location_t loc, enum tree_code code, ...)
3272 tree t;
3273 int length;
3274 int i;
3275 va_list p;
3277 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3279 va_start (p, code);
3281 t = make_node (code);
3282 SET_EXPR_LOCATION (t, loc);
3283 length = TREE_CODE_LENGTH (code);
3285 for (i = 0; i < length; i++)
3287 tree x = va_arg (p, tree);
3288 TREE_OPERAND (t, i) = x;
3289 if (x && TREE_CODE (x) == OVERLOAD)
3290 lookup_keep (x, true);
3293 va_end (p);
3294 return t;
3297 /* Similar to `build', but for template definitions. */
3299 tree
3300 build_min (enum tree_code code, tree tt, ...)
3302 tree t;
3303 int length;
3304 int i;
3305 va_list p;
3307 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3309 va_start (p, tt);
3311 t = make_node (code);
3312 length = TREE_CODE_LENGTH (code);
3313 TREE_TYPE (t) = tt;
3315 for (i = 0; i < length; i++)
3317 tree x = va_arg (p, tree);
3318 TREE_OPERAND (t, i) = x;
3319 if (x)
3321 if (!TYPE_P (x) && TREE_SIDE_EFFECTS (x))
3322 TREE_SIDE_EFFECTS (t) = 1;
3323 if (TREE_CODE (x) == OVERLOAD)
3324 lookup_keep (x, true);
3328 va_end (p);
3330 if (code == CAST_EXPR)
3331 /* The single operand is a TREE_LIST, which we have to check. */
3332 lookup_list_keep (TREE_OPERAND (t, 0), true);
3334 return t;
3337 /* Similar to `build', but for template definitions of non-dependent
3338 expressions. NON_DEP is the non-dependent expression that has been
3339 built. */
3341 tree
3342 build_min_non_dep (enum tree_code code, tree non_dep, ...)
3344 tree t;
3345 int length;
3346 int i;
3347 va_list p;
3349 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3351 va_start (p, non_dep);
3353 if (REFERENCE_REF_P (non_dep))
3354 non_dep = TREE_OPERAND (non_dep, 0);
3356 t = make_node (code);
3357 length = TREE_CODE_LENGTH (code);
3358 TREE_TYPE (t) = unlowered_expr_type (non_dep);
3359 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
3361 for (i = 0; i < length; i++)
3363 tree x = va_arg (p, tree);
3364 TREE_OPERAND (t, i) = x;
3365 if (x && TREE_CODE (x) == OVERLOAD)
3366 lookup_keep (x, true);
3369 if (code == COMPOUND_EXPR && TREE_CODE (non_dep) != COMPOUND_EXPR)
3370 /* This should not be considered a COMPOUND_EXPR, because it
3371 resolves to an overload. */
3372 COMPOUND_EXPR_OVERLOADED (t) = 1;
3374 va_end (p);
3375 return convert_from_reference (t);
3378 /* Similar to build_min_nt, but call expressions */
3380 tree
3381 build_min_nt_call_vec (tree fn, vec<tree, va_gc> *args)
3383 tree ret, t;
3384 unsigned int ix;
3386 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
3387 CALL_EXPR_FN (ret) = fn;
3388 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
3389 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
3391 CALL_EXPR_ARG (ret, ix) = t;
3392 if (TREE_CODE (t) == OVERLOAD)
3393 lookup_keep (t, true);
3395 return ret;
3398 /* Similar to `build_min_nt_call_vec', but for template definitions of
3399 non-dependent expressions. NON_DEP is the non-dependent expression
3400 that has been built. */
3402 tree
3403 build_min_non_dep_call_vec (tree non_dep, tree fn, vec<tree, va_gc> *argvec)
3405 tree t = build_min_nt_call_vec (fn, argvec);
3406 if (REFERENCE_REF_P (non_dep))
3407 non_dep = TREE_OPERAND (non_dep, 0);
3408 TREE_TYPE (t) = TREE_TYPE (non_dep);
3409 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
3410 return convert_from_reference (t);
3413 /* Similar to build_min_non_dep, but for expressions that have been resolved to
3414 a call to an operator overload. OP is the operator that has been
3415 overloaded. NON_DEP is the non-dependent expression that's been built,
3416 which should be a CALL_EXPR or an INDIRECT_REF to a CALL_EXPR. OVERLOAD is
3417 the overload that NON_DEP is calling. */
3419 tree
3420 build_min_non_dep_op_overload (enum tree_code op,
3421 tree non_dep,
3422 tree overload, ...)
3424 va_list p;
3425 int nargs, expected_nargs;
3426 tree fn, call;
3427 vec<tree, va_gc> *args;
3429 non_dep = extract_call_expr (non_dep);
3431 nargs = call_expr_nargs (non_dep);
3433 expected_nargs = cp_tree_code_length (op);
3434 if ((op == POSTINCREMENT_EXPR
3435 || op == POSTDECREMENT_EXPR)
3436 /* With -fpermissive non_dep could be operator++(). */
3437 && (!flag_permissive || nargs != expected_nargs))
3438 expected_nargs += 1;
3439 gcc_assert (nargs == expected_nargs);
3441 args = make_tree_vector ();
3442 va_start (p, overload);
3444 if (TREE_CODE (TREE_TYPE (overload)) == FUNCTION_TYPE)
3446 fn = overload;
3447 for (int i = 0; i < nargs; i++)
3449 tree arg = va_arg (p, tree);
3450 vec_safe_push (args, arg);
3453 else if (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE)
3455 tree object = va_arg (p, tree);
3456 tree binfo = TYPE_BINFO (TREE_TYPE (object));
3457 tree method = build_baselink (binfo, binfo, overload, NULL_TREE);
3458 fn = build_min (COMPONENT_REF, TREE_TYPE (overload),
3459 object, method, NULL_TREE);
3460 for (int i = 1; i < nargs; i++)
3462 tree arg = va_arg (p, tree);
3463 vec_safe_push (args, arg);
3466 else
3467 gcc_unreachable ();
3469 va_end (p);
3470 call = build_min_non_dep_call_vec (non_dep, fn, args);
3471 release_tree_vector (args);
3473 tree call_expr = extract_call_expr (call);
3474 KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep);
3475 CALL_EXPR_OPERATOR_SYNTAX (call_expr) = true;
3476 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep);
3477 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep);
3479 return call;
3482 /* Return a new tree vec copied from VEC, with ELT inserted at index IDX. */
3484 vec<tree, va_gc> *
3485 vec_copy_and_insert (vec<tree, va_gc> *old_vec, tree elt, unsigned idx)
3487 unsigned len = vec_safe_length (old_vec);
3488 gcc_assert (idx <= len);
3490 vec<tree, va_gc> *new_vec = NULL;
3491 vec_alloc (new_vec, len + 1);
3493 unsigned i;
3494 for (i = 0; i < len; ++i)
3496 if (i == idx)
3497 new_vec->quick_push (elt);
3498 new_vec->quick_push ((*old_vec)[i]);
3500 if (i == idx)
3501 new_vec->quick_push (elt);
3503 return new_vec;
3506 tree
3507 get_type_decl (tree t)
3509 if (TREE_CODE (t) == TYPE_DECL)
3510 return t;
3511 if (TYPE_P (t))
3512 return TYPE_STUB_DECL (t);
3513 gcc_assert (t == error_mark_node);
3514 return t;
3517 /* Returns the namespace that contains DECL, whether directly or
3518 indirectly. */
3520 tree
3521 decl_namespace_context (tree decl)
3523 while (1)
3525 if (TREE_CODE (decl) == NAMESPACE_DECL)
3526 return decl;
3527 else if (TYPE_P (decl))
3528 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
3529 else
3530 decl = CP_DECL_CONTEXT (decl);
3534 /* Returns true if decl is within an anonymous namespace, however deeply
3535 nested, or false otherwise. */
3537 bool
3538 decl_anon_ns_mem_p (const_tree decl)
3540 while (TREE_CODE (decl) != NAMESPACE_DECL)
3542 /* Classes inside anonymous namespaces have TREE_PUBLIC == 0. */
3543 if (TYPE_P (decl))
3544 return !TREE_PUBLIC (TYPE_MAIN_DECL (decl));
3546 decl = CP_DECL_CONTEXT (decl);
3548 return !TREE_PUBLIC (decl);
3551 /* Subroutine of cp_tree_equal: t1 and t2 are the CALL_EXPR_FNs of two
3552 CALL_EXPRS. Return whether they are equivalent. */
3554 static bool
3555 called_fns_equal (tree t1, tree t2)
3557 /* Core 1321: dependent names are equivalent even if the overload sets
3558 are different. But do compare explicit template arguments. */
3559 tree name1 = dependent_name (t1);
3560 tree name2 = dependent_name (t2);
3561 if (name1 || name2)
3563 tree targs1 = NULL_TREE, targs2 = NULL_TREE;
3565 if (name1 != name2)
3566 return false;
3568 if (TREE_CODE (t1) == TEMPLATE_ID_EXPR)
3569 targs1 = TREE_OPERAND (t1, 1);
3570 if (TREE_CODE (t2) == TEMPLATE_ID_EXPR)
3571 targs2 = TREE_OPERAND (t2, 1);
3572 return cp_tree_equal (targs1, targs2);
3574 else
3575 return cp_tree_equal (t1, t2);
3578 /* Return truthvalue of whether T1 is the same tree structure as T2.
3579 Return 1 if they are the same. Return 0 if they are different. */
3581 bool
3582 cp_tree_equal (tree t1, tree t2)
3584 enum tree_code code1, code2;
3586 if (t1 == t2)
3587 return true;
3588 if (!t1 || !t2)
3589 return false;
3591 code1 = TREE_CODE (t1);
3592 code2 = TREE_CODE (t2);
3594 if (code1 != code2)
3595 return false;
3597 if (CONSTANT_CLASS_P (t1)
3598 && !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3599 return false;
3601 switch (code1)
3603 case VOID_CST:
3604 /* There's only a single VOID_CST node, so we should never reach
3605 here. */
3606 gcc_unreachable ();
3608 case INTEGER_CST:
3609 return tree_int_cst_equal (t1, t2);
3611 case REAL_CST:
3612 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
3614 case STRING_CST:
3615 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3616 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3617 TREE_STRING_LENGTH (t1));
3619 case FIXED_CST:
3620 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
3621 TREE_FIXED_CST (t2));
3623 case COMPLEX_CST:
3624 return cp_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
3625 && cp_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
3627 case VECTOR_CST:
3628 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
3630 case CONSTRUCTOR:
3631 /* We need to do this when determining whether or not two
3632 non-type pointer to member function template arguments
3633 are the same. */
3634 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
3635 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
3636 return false;
3638 tree field, value;
3639 unsigned int i;
3640 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
3642 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
3643 if (!cp_tree_equal (field, elt2->index)
3644 || !cp_tree_equal (value, elt2->value))
3645 return false;
3648 return true;
3650 case TREE_LIST:
3651 if (!cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
3652 return false;
3653 if (!cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
3654 return false;
3655 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
3657 case SAVE_EXPR:
3658 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3660 case CALL_EXPR:
3662 tree arg1, arg2;
3663 call_expr_arg_iterator iter1, iter2;
3664 if (!called_fns_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
3665 return false;
3666 for (arg1 = first_call_expr_arg (t1, &iter1),
3667 arg2 = first_call_expr_arg (t2, &iter2);
3668 arg1 && arg2;
3669 arg1 = next_call_expr_arg (&iter1),
3670 arg2 = next_call_expr_arg (&iter2))
3671 if (!cp_tree_equal (arg1, arg2))
3672 return false;
3673 if (arg1 || arg2)
3674 return false;
3675 return true;
3678 case TARGET_EXPR:
3680 tree o1 = TREE_OPERAND (t1, 0);
3681 tree o2 = TREE_OPERAND (t2, 0);
3683 /* Special case: if either target is an unallocated VAR_DECL,
3684 it means that it's going to be unified with whatever the
3685 TARGET_EXPR is really supposed to initialize, so treat it
3686 as being equivalent to anything. */
3687 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
3688 && !DECL_RTL_SET_P (o1))
3689 /*Nop*/;
3690 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
3691 && !DECL_RTL_SET_P (o2))
3692 /*Nop*/;
3693 else if (!cp_tree_equal (o1, o2))
3694 return false;
3696 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3699 case PARM_DECL:
3700 /* For comparing uses of parameters in late-specified return types
3701 with an out-of-class definition of the function, but can also come
3702 up for expressions that involve 'this' in a member function
3703 template. */
3705 if (comparing_specializations && !CONSTRAINT_VAR_P (t1))
3706 /* When comparing hash table entries, only an exact match is
3707 good enough; we don't want to replace 'this' with the
3708 version from another function. But be more flexible
3709 with local parameters in a requires-expression. */
3710 return false;
3712 if (same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3714 if (DECL_ARTIFICIAL (t1) ^ DECL_ARTIFICIAL (t2))
3715 return false;
3716 if (CONSTRAINT_VAR_P (t1) ^ CONSTRAINT_VAR_P (t2))
3717 return false;
3718 if (DECL_ARTIFICIAL (t1)
3719 || (DECL_PARM_LEVEL (t1) == DECL_PARM_LEVEL (t2)
3720 && DECL_PARM_INDEX (t1) == DECL_PARM_INDEX (t2)))
3721 return true;
3723 return false;
3725 case VAR_DECL:
3726 case CONST_DECL:
3727 case FIELD_DECL:
3728 case FUNCTION_DECL:
3729 case TEMPLATE_DECL:
3730 case IDENTIFIER_NODE:
3731 case SSA_NAME:
3732 return false;
3734 case BASELINK:
3735 return (BASELINK_BINFO (t1) == BASELINK_BINFO (t2)
3736 && BASELINK_ACCESS_BINFO (t1) == BASELINK_ACCESS_BINFO (t2)
3737 && BASELINK_QUALIFIED_P (t1) == BASELINK_QUALIFIED_P (t2)
3738 && cp_tree_equal (BASELINK_FUNCTIONS (t1),
3739 BASELINK_FUNCTIONS (t2)));
3741 case TEMPLATE_PARM_INDEX:
3742 return (TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
3743 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2)
3744 && (TEMPLATE_PARM_PARAMETER_PACK (t1)
3745 == TEMPLATE_PARM_PARAMETER_PACK (t2))
3746 && same_type_p (TREE_TYPE (TEMPLATE_PARM_DECL (t1)),
3747 TREE_TYPE (TEMPLATE_PARM_DECL (t2))));
3749 case TEMPLATE_ID_EXPR:
3750 return (cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3751 && cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3753 case CONSTRAINT_INFO:
3754 return cp_tree_equal (CI_ASSOCIATED_CONSTRAINTS (t1),
3755 CI_ASSOCIATED_CONSTRAINTS (t2));
3757 case CHECK_CONSTR:
3758 return (CHECK_CONSTR_CONCEPT (t1) == CHECK_CONSTR_CONCEPT (t2)
3759 && comp_template_args (CHECK_CONSTR_ARGS (t1),
3760 CHECK_CONSTR_ARGS (t2)));
3762 case TREE_VEC:
3764 unsigned ix;
3765 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3766 return false;
3767 for (ix = TREE_VEC_LENGTH (t1); ix--;)
3768 if (!cp_tree_equal (TREE_VEC_ELT (t1, ix),
3769 TREE_VEC_ELT (t2, ix)))
3770 return false;
3771 return true;
3774 case SIZEOF_EXPR:
3775 case ALIGNOF_EXPR:
3777 tree o1 = TREE_OPERAND (t1, 0);
3778 tree o2 = TREE_OPERAND (t2, 0);
3780 if (code1 == SIZEOF_EXPR)
3782 if (SIZEOF_EXPR_TYPE_P (t1))
3783 o1 = TREE_TYPE (o1);
3784 if (SIZEOF_EXPR_TYPE_P (t2))
3785 o2 = TREE_TYPE (o2);
3787 if (TREE_CODE (o1) != TREE_CODE (o2))
3788 return false;
3789 if (TYPE_P (o1))
3790 return same_type_p (o1, o2);
3791 else
3792 return cp_tree_equal (o1, o2);
3795 case MODOP_EXPR:
3797 tree t1_op1, t2_op1;
3799 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
3800 return false;
3802 t1_op1 = TREE_OPERAND (t1, 1);
3803 t2_op1 = TREE_OPERAND (t2, 1);
3804 if (TREE_CODE (t1_op1) != TREE_CODE (t2_op1))
3805 return false;
3807 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t2, 2));
3810 case PTRMEM_CST:
3811 /* Two pointer-to-members are the same if they point to the same
3812 field or function in the same class. */
3813 if (PTRMEM_CST_MEMBER (t1) != PTRMEM_CST_MEMBER (t2))
3814 return false;
3816 return same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2));
3818 case OVERLOAD:
3820 /* Two overloads. Must be exactly the same set of decls. */
3821 lkp_iterator first (t1);
3822 lkp_iterator second (t2);
3824 for (; first && second; ++first, ++second)
3825 if (*first != *second)
3826 return false;
3827 return !(first || second);
3830 case TRAIT_EXPR:
3831 if (TRAIT_EXPR_KIND (t1) != TRAIT_EXPR_KIND (t2))
3832 return false;
3833 return same_type_p (TRAIT_EXPR_TYPE1 (t1), TRAIT_EXPR_TYPE1 (t2))
3834 && cp_tree_equal (TRAIT_EXPR_TYPE2 (t1), TRAIT_EXPR_TYPE2 (t2));
3836 case CAST_EXPR:
3837 case STATIC_CAST_EXPR:
3838 case REINTERPRET_CAST_EXPR:
3839 case CONST_CAST_EXPR:
3840 case DYNAMIC_CAST_EXPR:
3841 case IMPLICIT_CONV_EXPR:
3842 case NEW_EXPR:
3843 CASE_CONVERT:
3844 case NON_LVALUE_EXPR:
3845 case VIEW_CONVERT_EXPR:
3846 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3847 return false;
3848 /* Now compare operands as usual. */
3849 break;
3851 case DEFERRED_NOEXCEPT:
3852 return (cp_tree_equal (DEFERRED_NOEXCEPT_PATTERN (t1),
3853 DEFERRED_NOEXCEPT_PATTERN (t2))
3854 && comp_template_args (DEFERRED_NOEXCEPT_ARGS (t1),
3855 DEFERRED_NOEXCEPT_ARGS (t2)));
3856 break;
3858 default:
3859 break;
3862 switch (TREE_CODE_CLASS (code1))
3864 case tcc_unary:
3865 case tcc_binary:
3866 case tcc_comparison:
3867 case tcc_expression:
3868 case tcc_vl_exp:
3869 case tcc_reference:
3870 case tcc_statement:
3872 int i, n;
3874 n = cp_tree_operand_length (t1);
3875 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
3876 && n != TREE_OPERAND_LENGTH (t2))
3877 return false;
3879 for (i = 0; i < n; ++i)
3880 if (!cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
3881 return false;
3883 return true;
3886 case tcc_type:
3887 return same_type_p (t1, t2);
3888 default:
3889 gcc_unreachable ();
3891 /* We can get here with --disable-checking. */
3892 return false;
3895 /* The type of ARG when used as an lvalue. */
3897 tree
3898 lvalue_type (tree arg)
3900 tree type = TREE_TYPE (arg);
3901 return type;
3904 /* The type of ARG for printing error messages; denote lvalues with
3905 reference types. */
3907 tree
3908 error_type (tree arg)
3910 tree type = TREE_TYPE (arg);
3912 if (TREE_CODE (type) == ARRAY_TYPE)
3914 else if (TREE_CODE (type) == ERROR_MARK)
3916 else if (lvalue_p (arg))
3917 type = build_reference_type (lvalue_type (arg));
3918 else if (MAYBE_CLASS_TYPE_P (type))
3919 type = lvalue_type (arg);
3921 return type;
3924 /* Does FUNCTION use a variable-length argument list? */
3927 varargs_function_p (const_tree function)
3929 return stdarg_p (TREE_TYPE (function));
3932 /* Returns 1 if decl is a member of a class. */
3935 member_p (const_tree decl)
3937 const_tree const ctx = DECL_CONTEXT (decl);
3938 return (ctx && TYPE_P (ctx));
3941 /* Create a placeholder for member access where we don't actually have an
3942 object that the access is against. */
3944 tree
3945 build_dummy_object (tree type)
3947 tree decl = build1 (CONVERT_EXPR, build_pointer_type (type), void_node);
3948 return cp_build_fold_indirect_ref (decl);
3951 /* We've gotten a reference to a member of TYPE. Return *this if appropriate,
3952 or a dummy object otherwise. If BINFOP is non-0, it is filled with the
3953 binfo path from current_class_type to TYPE, or 0. */
3955 tree
3956 maybe_dummy_object (tree type, tree* binfop)
3958 tree decl, context;
3959 tree binfo;
3960 tree current = current_nonlambda_class_type ();
3962 if (current
3963 && (binfo = lookup_base (current, type, ba_any, NULL,
3964 tf_warning_or_error)))
3965 context = current;
3966 else
3968 /* Reference from a nested class member function. */
3969 context = type;
3970 binfo = TYPE_BINFO (type);
3973 if (binfop)
3974 *binfop = binfo;
3976 if (current_class_ref
3977 /* current_class_ref might not correspond to current_class_type if
3978 we're in tsubst_default_argument or a lambda-declarator; in either
3979 case, we want to use current_class_ref if it matches CONTEXT. */
3980 && (same_type_ignoring_top_level_qualifiers_p
3981 (TREE_TYPE (current_class_ref), context)))
3982 decl = current_class_ref;
3983 else
3984 decl = build_dummy_object (context);
3986 return decl;
3989 /* Returns 1 if OB is a placeholder object, or a pointer to one. */
3992 is_dummy_object (const_tree ob)
3994 if (INDIRECT_REF_P (ob))
3995 ob = TREE_OPERAND (ob, 0);
3996 return (TREE_CODE (ob) == CONVERT_EXPR
3997 && TREE_OPERAND (ob, 0) == void_node);
4000 /* Returns 1 iff type T is something we want to treat as a scalar type for
4001 the purpose of deciding whether it is trivial/POD/standard-layout. */
4003 bool
4004 scalarish_type_p (const_tree t)
4006 if (t == error_mark_node)
4007 return 1;
4009 return (SCALAR_TYPE_P (t) || VECTOR_TYPE_P (t));
4012 /* Returns true iff T requires non-trivial default initialization. */
4014 bool
4015 type_has_nontrivial_default_init (const_tree t)
4017 t = strip_array_types (CONST_CAST_TREE (t));
4019 if (CLASS_TYPE_P (t))
4020 return TYPE_HAS_COMPLEX_DFLT (t);
4021 else
4022 return 0;
4025 /* Track classes with only deleted copy/move constructors so that we can warn
4026 if they are used in call/return by value. */
4028 static GTY(()) hash_set<tree>* deleted_copy_types;
4029 static void
4030 remember_deleted_copy (const_tree t)
4032 if (!deleted_copy_types)
4033 deleted_copy_types = hash_set<tree>::create_ggc(37);
4034 deleted_copy_types->add (CONST_CAST_TREE (t));
4036 void
4037 maybe_warn_parm_abi (tree t, location_t loc)
4039 if (!deleted_copy_types
4040 || !deleted_copy_types->contains (t))
4041 return;
4043 warning_at (loc, OPT_Wabi, "the calling convention for %qT changes in "
4044 "-fabi-version=12 (GCC 8)", t);
4045 static bool explained = false;
4046 if (!explained)
4048 inform (loc, " because all of its copy and move constructors "
4049 "are deleted");
4050 explained = true;
4054 /* Returns true iff copying an object of type T (including via move
4055 constructor) is non-trivial. That is, T has no non-trivial copy
4056 constructors and no non-trivial move constructors, and not all copy/move
4057 constructors are deleted. This function implements the ABI notion of
4058 non-trivial copy, which has diverged from the one in the standard. */
4060 bool
4061 type_has_nontrivial_copy_init (const_tree type)
4063 tree t = strip_array_types (CONST_CAST_TREE (type));
4065 if (CLASS_TYPE_P (t))
4067 gcc_assert (COMPLETE_TYPE_P (t));
4069 if (TYPE_HAS_COMPLEX_COPY_CTOR (t)
4070 || TYPE_HAS_COMPLEX_MOVE_CTOR (t))
4071 /* Nontrivial. */
4072 return true;
4074 if (cxx_dialect < cxx11)
4075 /* No deleted functions before C++11. */
4076 return false;
4078 /* Before ABI v12 we did a bitwise copy of types with only deleted
4079 copy/move constructors. */
4080 if (!abi_version_at_least (12)
4081 && !(warn_abi && abi_version_crosses (12)))
4082 return false;
4084 bool saw_copy = false;
4085 bool saw_non_deleted = false;
4087 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
4088 saw_copy = saw_non_deleted = true;
4089 else if (CLASSTYPE_LAZY_COPY_CTOR (t))
4091 saw_copy = true;
4092 if (classtype_has_move_assign_or_move_ctor_p (t, true))
4093 /* [class.copy]/8 If the class definition declares a move
4094 constructor or move assignment operator, the implicitly declared
4095 copy constructor is defined as deleted.... */;
4096 else
4097 /* Any other reason the implicitly-declared function would be
4098 deleted would also cause TYPE_HAS_COMPLEX_COPY_CTOR to be
4099 set. */
4100 saw_non_deleted = true;
4103 if (!saw_non_deleted)
4104 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4106 tree fn = *iter;
4107 if (copy_fn_p (fn))
4109 saw_copy = true;
4110 if (!DECL_DELETED_FN (fn))
4112 /* Not deleted, therefore trivial. */
4113 saw_non_deleted = true;
4114 break;
4119 gcc_assert (saw_copy);
4121 if (saw_copy && !saw_non_deleted)
4123 if (warn_abi && abi_version_crosses (12))
4124 remember_deleted_copy (t);
4125 if (abi_version_at_least (12))
4126 return true;
4129 return false;
4131 else
4132 return 0;
4135 /* Returns 1 iff type T is a trivially copyable type, as defined in
4136 [basic.types] and [class]. */
4138 bool
4139 trivially_copyable_p (const_tree t)
4141 t = strip_array_types (CONST_CAST_TREE (t));
4143 if (CLASS_TYPE_P (t))
4144 return ((!TYPE_HAS_COPY_CTOR (t)
4145 || !TYPE_HAS_COMPLEX_COPY_CTOR (t))
4146 && !TYPE_HAS_COMPLEX_MOVE_CTOR (t)
4147 && (!TYPE_HAS_COPY_ASSIGN (t)
4148 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (t))
4149 && !TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
4150 && TYPE_HAS_TRIVIAL_DESTRUCTOR (t));
4151 else
4152 return !CP_TYPE_VOLATILE_P (t) && scalarish_type_p (t);
4155 /* Returns 1 iff type T is a trivial type, as defined in [basic.types] and
4156 [class]. */
4158 bool
4159 trivial_type_p (const_tree t)
4161 t = strip_array_types (CONST_CAST_TREE (t));
4163 if (CLASS_TYPE_P (t))
4164 return (TYPE_HAS_TRIVIAL_DFLT (t)
4165 && trivially_copyable_p (t));
4166 else
4167 return scalarish_type_p (t);
4170 /* Returns 1 iff type T is a POD type, as defined in [basic.types]. */
4172 bool
4173 pod_type_p (const_tree t)
4175 /* This CONST_CAST is okay because strip_array_types returns its
4176 argument unmodified and we assign it to a const_tree. */
4177 t = strip_array_types (CONST_CAST_TREE(t));
4179 if (!CLASS_TYPE_P (t))
4180 return scalarish_type_p (t);
4181 else if (cxx_dialect > cxx98)
4182 /* [class]/10: A POD struct is a class that is both a trivial class and a
4183 standard-layout class, and has no non-static data members of type
4184 non-POD struct, non-POD union (or array of such types).
4186 We don't need to check individual members because if a member is
4187 non-std-layout or non-trivial, the class will be too. */
4188 return (std_layout_type_p (t) && trivial_type_p (t));
4189 else
4190 /* The C++98 definition of POD is different. */
4191 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
4194 /* Returns true iff T is POD for the purpose of layout, as defined in the
4195 C++ ABI. */
4197 bool
4198 layout_pod_type_p (const_tree t)
4200 t = strip_array_types (CONST_CAST_TREE (t));
4202 if (CLASS_TYPE_P (t))
4203 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
4204 else
4205 return scalarish_type_p (t);
4208 /* Returns true iff T is a standard-layout type, as defined in
4209 [basic.types]. */
4211 bool
4212 std_layout_type_p (const_tree t)
4214 t = strip_array_types (CONST_CAST_TREE (t));
4216 if (CLASS_TYPE_P (t))
4217 return !CLASSTYPE_NON_STD_LAYOUT (t);
4218 else
4219 return scalarish_type_p (t);
4222 static bool record_has_unique_obj_representations (const_tree, const_tree);
4224 /* Returns true iff T satisfies std::has_unique_object_representations<T>,
4225 as defined in [meta.unary.prop]. */
4227 bool
4228 type_has_unique_obj_representations (const_tree t)
4230 bool ret;
4232 t = strip_array_types (CONST_CAST_TREE (t));
4234 if (!trivially_copyable_p (t))
4235 return false;
4237 if (CLASS_TYPE_P (t) && CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t))
4238 return CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t);
4240 switch (TREE_CODE (t))
4242 case INTEGER_TYPE:
4243 case POINTER_TYPE:
4244 case REFERENCE_TYPE:
4245 /* If some backend has any paddings in these types, we should add
4246 a target hook for this and handle it there. */
4247 return true;
4249 case BOOLEAN_TYPE:
4250 /* For bool values other than 0 and 1 should only appear with
4251 undefined behavior. */
4252 return true;
4254 case ENUMERAL_TYPE:
4255 return type_has_unique_obj_representations (ENUM_UNDERLYING_TYPE (t));
4257 case REAL_TYPE:
4258 /* XFmode certainly contains padding on x86, which the CPU doesn't store
4259 when storing long double values, so for that we have to return false.
4260 Other kinds of floating point values are questionable due to +.0/-.0
4261 and NaNs, let's play safe for now. */
4262 return false;
4264 case FIXED_POINT_TYPE:
4265 return false;
4267 case OFFSET_TYPE:
4268 return true;
4270 case COMPLEX_TYPE:
4271 case VECTOR_TYPE:
4272 return type_has_unique_obj_representations (TREE_TYPE (t));
4274 case RECORD_TYPE:
4275 ret = record_has_unique_obj_representations (t, TYPE_SIZE (t));
4276 if (CLASS_TYPE_P (t))
4278 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
4279 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
4281 return ret;
4283 case UNION_TYPE:
4284 ret = true;
4285 bool any_fields;
4286 any_fields = false;
4287 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4288 if (TREE_CODE (field) == FIELD_DECL)
4290 any_fields = true;
4291 if (!type_has_unique_obj_representations (TREE_TYPE (field))
4292 || simple_cst_equal (DECL_SIZE (field), TYPE_SIZE (t)) != 1)
4294 ret = false;
4295 break;
4298 if (!any_fields && !integer_zerop (TYPE_SIZE (t)))
4299 ret = false;
4300 if (CLASS_TYPE_P (t))
4302 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
4303 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
4305 return ret;
4307 case NULLPTR_TYPE:
4308 return false;
4310 case ERROR_MARK:
4311 return false;
4313 default:
4314 gcc_unreachable ();
4318 /* Helper function for type_has_unique_obj_representations. */
4320 static bool
4321 record_has_unique_obj_representations (const_tree t, const_tree sz)
4323 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4324 if (TREE_CODE (field) != FIELD_DECL)
4326 /* For bases, can't use type_has_unique_obj_representations here, as in
4327 struct S { int i : 24; S (); };
4328 struct T : public S { int j : 8; T (); };
4329 S doesn't have unique obj representations, but T does. */
4330 else if (DECL_FIELD_IS_BASE (field))
4332 if (!record_has_unique_obj_representations (TREE_TYPE (field),
4333 DECL_SIZE (field)))
4334 return false;
4336 else if (DECL_C_BIT_FIELD (field))
4338 tree btype = DECL_BIT_FIELD_TYPE (field);
4339 if (!type_has_unique_obj_representations (btype))
4340 return false;
4342 else if (!type_has_unique_obj_representations (TREE_TYPE (field)))
4343 return false;
4345 offset_int cur = 0;
4346 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4347 if (TREE_CODE (field) == FIELD_DECL)
4349 offset_int fld = wi::to_offset (DECL_FIELD_OFFSET (field));
4350 offset_int bitpos = wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
4351 fld = fld * BITS_PER_UNIT + bitpos;
4352 if (cur != fld)
4353 return false;
4354 if (DECL_SIZE (field))
4356 offset_int size = wi::to_offset (DECL_SIZE (field));
4357 cur += size;
4360 if (cur != wi::to_offset (sz))
4361 return false;
4363 return true;
4366 /* Nonzero iff type T is a class template implicit specialization. */
4368 bool
4369 class_tmpl_impl_spec_p (const_tree t)
4371 return CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INSTANTIATION (t);
4374 /* Returns 1 iff zero initialization of type T means actually storing
4375 zeros in it. */
4378 zero_init_p (const_tree t)
4380 /* This CONST_CAST is okay because strip_array_types returns its
4381 argument unmodified and we assign it to a const_tree. */
4382 t = strip_array_types (CONST_CAST_TREE(t));
4384 if (t == error_mark_node)
4385 return 1;
4387 /* NULL pointers to data members are initialized with -1. */
4388 if (TYPE_PTRDATAMEM_P (t))
4389 return 0;
4391 /* Classes that contain types that can't be zero-initialized, cannot
4392 be zero-initialized themselves. */
4393 if (CLASS_TYPE_P (t) && CLASSTYPE_NON_ZERO_INIT_P (t))
4394 return 0;
4396 return 1;
4399 /* Handle the C++17 [[nodiscard]] attribute, which is similar to the GNU
4400 warn_unused_result attribute. */
4402 static tree
4403 handle_nodiscard_attribute (tree *node, tree name, tree /*args*/,
4404 int /*flags*/, bool *no_add_attrs)
4406 if (TREE_CODE (*node) == FUNCTION_DECL)
4408 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
4409 warning (OPT_Wattributes, "%qE attribute applied to %qD with void "
4410 "return type", name, *node);
4412 else if (OVERLOAD_TYPE_P (*node))
4413 /* OK */;
4414 else
4416 warning (OPT_Wattributes, "%qE attribute can only be applied to "
4417 "functions or to class or enumeration types", name);
4418 *no_add_attrs = true;
4420 return NULL_TREE;
4423 /* Table of valid C++ attributes. */
4424 const struct attribute_spec cxx_attribute_table[] =
4426 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4427 affects_type_identity, handler, exclude } */
4428 { "init_priority", 1, 1, true, false, false, false,
4429 handle_init_priority_attribute, NULL },
4430 { "abi_tag", 1, -1, false, false, false, true,
4431 handle_abi_tag_attribute, NULL },
4432 { NULL, 0, 0, false, false, false, false, NULL, NULL }
4435 /* Table of C++ standard attributes. */
4436 const struct attribute_spec std_attribute_table[] =
4438 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4439 affects_type_identity, handler, exclude } */
4440 { "maybe_unused", 0, 0, false, false, false, false,
4441 handle_unused_attribute, NULL },
4442 { "nodiscard", 0, 0, false, false, false, false,
4443 handle_nodiscard_attribute, NULL },
4444 { NULL, 0, 0, false, false, false, false, NULL, NULL }
4447 /* Handle an "init_priority" attribute; arguments as in
4448 struct attribute_spec.handler. */
4449 static tree
4450 handle_init_priority_attribute (tree* node,
4451 tree name,
4452 tree args,
4453 int /*flags*/,
4454 bool* no_add_attrs)
4456 tree initp_expr = TREE_VALUE (args);
4457 tree decl = *node;
4458 tree type = TREE_TYPE (decl);
4459 int pri;
4461 STRIP_NOPS (initp_expr);
4462 initp_expr = default_conversion (initp_expr);
4463 if (initp_expr)
4464 initp_expr = maybe_constant_value (initp_expr);
4466 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
4468 error ("requested init_priority is not an integer constant");
4469 cxx_constant_value (initp_expr);
4470 *no_add_attrs = true;
4471 return NULL_TREE;
4474 pri = TREE_INT_CST_LOW (initp_expr);
4476 type = strip_array_types (type);
4478 if (decl == NULL_TREE
4479 || !VAR_P (decl)
4480 || !TREE_STATIC (decl)
4481 || DECL_EXTERNAL (decl)
4482 || (TREE_CODE (type) != RECORD_TYPE
4483 && TREE_CODE (type) != UNION_TYPE)
4484 /* Static objects in functions are initialized the
4485 first time control passes through that
4486 function. This is not precise enough to pin down an
4487 init_priority value, so don't allow it. */
4488 || current_function_decl)
4490 error ("can only use %qE attribute on file-scope definitions "
4491 "of objects of class type", name);
4492 *no_add_attrs = true;
4493 return NULL_TREE;
4496 if (pri > MAX_INIT_PRIORITY || pri <= 0)
4498 error ("requested init_priority is out of range");
4499 *no_add_attrs = true;
4500 return NULL_TREE;
4503 /* Check for init_priorities that are reserved for
4504 language and runtime support implementations.*/
4505 if (pri <= MAX_RESERVED_INIT_PRIORITY)
4507 warning
4508 (0, "requested init_priority is reserved for internal use");
4511 if (SUPPORTS_INIT_PRIORITY)
4513 SET_DECL_INIT_PRIORITY (decl, pri);
4514 DECL_HAS_INIT_PRIORITY_P (decl) = 1;
4515 return NULL_TREE;
4517 else
4519 error ("%qE attribute is not supported on this platform", name);
4520 *no_add_attrs = true;
4521 return NULL_TREE;
4525 /* DECL is being redeclared; the old declaration had the abi tags in OLD,
4526 and the new one has the tags in NEW_. Give an error if there are tags
4527 in NEW_ that weren't in OLD. */
4529 bool
4530 check_abi_tag_redeclaration (const_tree decl, const_tree old, const_tree new_)
4532 if (old && TREE_CODE (TREE_VALUE (old)) == TREE_LIST)
4533 old = TREE_VALUE (old);
4534 if (new_ && TREE_CODE (TREE_VALUE (new_)) == TREE_LIST)
4535 new_ = TREE_VALUE (new_);
4536 bool err = false;
4537 for (const_tree t = new_; t; t = TREE_CHAIN (t))
4539 tree str = TREE_VALUE (t);
4540 for (const_tree in = old; in; in = TREE_CHAIN (in))
4542 tree ostr = TREE_VALUE (in);
4543 if (cp_tree_equal (str, ostr))
4544 goto found;
4546 error ("redeclaration of %qD adds abi tag %qE", decl, str);
4547 err = true;
4548 found:;
4550 if (err)
4552 inform (DECL_SOURCE_LOCATION (decl), "previous declaration here");
4553 return false;
4555 return true;
4558 /* The abi_tag attribute with the name NAME was given ARGS. If they are
4559 ill-formed, give an error and return false; otherwise, return true. */
4561 bool
4562 check_abi_tag_args (tree args, tree name)
4564 if (!args)
4566 error ("the %qE attribute requires arguments", name);
4567 return false;
4569 for (tree arg = args; arg; arg = TREE_CHAIN (arg))
4571 tree elt = TREE_VALUE (arg);
4572 if (TREE_CODE (elt) != STRING_CST
4573 || (!same_type_ignoring_top_level_qualifiers_p
4574 (strip_array_types (TREE_TYPE (elt)),
4575 char_type_node)))
4577 error ("arguments to the %qE attribute must be narrow string "
4578 "literals", name);
4579 return false;
4581 const char *begin = TREE_STRING_POINTER (elt);
4582 const char *end = begin + TREE_STRING_LENGTH (elt);
4583 for (const char *p = begin; p != end; ++p)
4585 char c = *p;
4586 if (p == begin)
4588 if (!ISALPHA (c) && c != '_')
4590 error ("arguments to the %qE attribute must contain valid "
4591 "identifiers", name);
4592 inform (input_location, "%<%c%> is not a valid first "
4593 "character for an identifier", c);
4594 return false;
4597 else if (p == end - 1)
4598 gcc_assert (c == 0);
4599 else
4601 if (!ISALNUM (c) && c != '_')
4603 error ("arguments to the %qE attribute must contain valid "
4604 "identifiers", name);
4605 inform (input_location, "%<%c%> is not a valid character "
4606 "in an identifier", c);
4607 return false;
4612 return true;
4615 /* Handle an "abi_tag" attribute; arguments as in
4616 struct attribute_spec.handler. */
4618 static tree
4619 handle_abi_tag_attribute (tree* node, tree name, tree args,
4620 int flags, bool* no_add_attrs)
4622 if (!check_abi_tag_args (args, name))
4623 goto fail;
4625 if (TYPE_P (*node))
4627 if (!OVERLOAD_TYPE_P (*node))
4629 error ("%qE attribute applied to non-class, non-enum type %qT",
4630 name, *node);
4631 goto fail;
4633 else if (!(flags & (int)ATTR_FLAG_TYPE_IN_PLACE))
4635 error ("%qE attribute applied to %qT after its definition",
4636 name, *node);
4637 goto fail;
4639 else if (CLASS_TYPE_P (*node)
4640 && CLASSTYPE_TEMPLATE_INSTANTIATION (*node))
4642 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
4643 "template instantiation %qT", name, *node);
4644 goto fail;
4646 else if (CLASS_TYPE_P (*node)
4647 && CLASSTYPE_TEMPLATE_SPECIALIZATION (*node))
4649 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
4650 "template specialization %qT", name, *node);
4651 goto fail;
4654 tree attributes = TYPE_ATTRIBUTES (*node);
4655 tree decl = TYPE_NAME (*node);
4657 /* Make sure all declarations have the same abi tags. */
4658 if (DECL_SOURCE_LOCATION (decl) != input_location)
4660 if (!check_abi_tag_redeclaration (decl,
4661 lookup_attribute ("abi_tag",
4662 attributes),
4663 args))
4664 goto fail;
4667 else
4669 if (!VAR_OR_FUNCTION_DECL_P (*node))
4671 error ("%qE attribute applied to non-function, non-variable %qD",
4672 name, *node);
4673 goto fail;
4675 else if (DECL_LANGUAGE (*node) == lang_c)
4677 error ("%qE attribute applied to extern \"C\" declaration %qD",
4678 name, *node);
4679 goto fail;
4683 return NULL_TREE;
4685 fail:
4686 *no_add_attrs = true;
4687 return NULL_TREE;
4690 /* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the
4691 thing pointed to by the constant. */
4693 tree
4694 make_ptrmem_cst (tree type, tree member)
4696 tree ptrmem_cst = make_node (PTRMEM_CST);
4697 TREE_TYPE (ptrmem_cst) = type;
4698 PTRMEM_CST_MEMBER (ptrmem_cst) = member;
4699 return ptrmem_cst;
4702 /* Build a variant of TYPE that has the indicated ATTRIBUTES. May
4703 return an existing type if an appropriate type already exists. */
4705 tree
4706 cp_build_type_attribute_variant (tree type, tree attributes)
4708 tree new_type;
4710 new_type = build_type_attribute_variant (type, attributes);
4711 if (TREE_CODE (new_type) == FUNCTION_TYPE
4712 || TREE_CODE (new_type) == METHOD_TYPE)
4714 new_type = build_exception_variant (new_type,
4715 TYPE_RAISES_EXCEPTIONS (type));
4716 new_type = build_ref_qualified_type (new_type,
4717 type_memfn_rqual (type));
4720 /* Making a new main variant of a class type is broken. */
4721 gcc_assert (!CLASS_TYPE_P (type) || new_type == type);
4723 return new_type;
4726 /* Return TRUE if TYPE1 and TYPE2 are identical for type hashing purposes.
4727 Called only after doing all language independent checks. */
4729 bool
4730 cxx_type_hash_eq (const_tree typea, const_tree typeb)
4732 gcc_assert (TREE_CODE (typea) == FUNCTION_TYPE
4733 || TREE_CODE (typea) == METHOD_TYPE);
4735 if (type_memfn_rqual (typea) != type_memfn_rqual (typeb))
4736 return false;
4737 return comp_except_specs (TYPE_RAISES_EXCEPTIONS (typea),
4738 TYPE_RAISES_EXCEPTIONS (typeb), ce_exact);
4741 /* Copy the language-specific type variant modifiers from TYPEB to TYPEA. For
4742 C++, these are the exception-specifier and ref-qualifier. */
4744 tree
4745 cxx_copy_lang_qualifiers (const_tree typea, const_tree typeb)
4747 tree type = CONST_CAST_TREE (typea);
4748 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
4750 type = build_exception_variant (type, TYPE_RAISES_EXCEPTIONS (typeb));
4751 type = build_ref_qualified_type (type, type_memfn_rqual (typeb));
4753 return type;
4756 /* Apply FUNC to all language-specific sub-trees of TP in a pre-order
4757 traversal. Called from walk_tree. */
4759 tree
4760 cp_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func,
4761 void *data, hash_set<tree> *pset)
4763 enum tree_code code = TREE_CODE (*tp);
4764 tree result;
4766 #define WALK_SUBTREE(NODE) \
4767 do \
4769 result = cp_walk_tree (&(NODE), func, data, pset); \
4770 if (result) goto out; \
4772 while (0)
4774 /* Not one of the easy cases. We must explicitly go through the
4775 children. */
4776 result = NULL_TREE;
4777 switch (code)
4779 case DEFAULT_ARG:
4780 case TEMPLATE_TEMPLATE_PARM:
4781 case BOUND_TEMPLATE_TEMPLATE_PARM:
4782 case UNBOUND_CLASS_TEMPLATE:
4783 case TEMPLATE_PARM_INDEX:
4784 case TEMPLATE_TYPE_PARM:
4785 case TYPENAME_TYPE:
4786 case TYPEOF_TYPE:
4787 case UNDERLYING_TYPE:
4788 /* None of these have subtrees other than those already walked
4789 above. */
4790 *walk_subtrees_p = 0;
4791 break;
4793 case BASELINK:
4794 if (BASELINK_QUALIFIED_P (*tp))
4795 WALK_SUBTREE (BINFO_TYPE (BASELINK_ACCESS_BINFO (*tp)));
4796 WALK_SUBTREE (BASELINK_FUNCTIONS (*tp));
4797 *walk_subtrees_p = 0;
4798 break;
4800 case PTRMEM_CST:
4801 WALK_SUBTREE (TREE_TYPE (*tp));
4802 *walk_subtrees_p = 0;
4803 break;
4805 case TREE_LIST:
4806 WALK_SUBTREE (TREE_PURPOSE (*tp));
4807 break;
4809 case OVERLOAD:
4810 WALK_SUBTREE (OVL_FUNCTION (*tp));
4811 WALK_SUBTREE (OVL_CHAIN (*tp));
4812 *walk_subtrees_p = 0;
4813 break;
4815 case USING_DECL:
4816 WALK_SUBTREE (DECL_NAME (*tp));
4817 WALK_SUBTREE (USING_DECL_SCOPE (*tp));
4818 WALK_SUBTREE (USING_DECL_DECLS (*tp));
4819 *walk_subtrees_p = 0;
4820 break;
4822 case RECORD_TYPE:
4823 if (TYPE_PTRMEMFUNC_P (*tp))
4824 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (*tp));
4825 break;
4827 case TYPE_ARGUMENT_PACK:
4828 case NONTYPE_ARGUMENT_PACK:
4830 tree args = ARGUMENT_PACK_ARGS (*tp);
4831 int i, len = TREE_VEC_LENGTH (args);
4832 for (i = 0; i < len; i++)
4833 WALK_SUBTREE (TREE_VEC_ELT (args, i));
4835 break;
4837 case TYPE_PACK_EXPANSION:
4838 WALK_SUBTREE (TREE_TYPE (*tp));
4839 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp));
4840 *walk_subtrees_p = 0;
4841 break;
4843 case EXPR_PACK_EXPANSION:
4844 WALK_SUBTREE (TREE_OPERAND (*tp, 0));
4845 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp));
4846 *walk_subtrees_p = 0;
4847 break;
4849 case CAST_EXPR:
4850 case REINTERPRET_CAST_EXPR:
4851 case STATIC_CAST_EXPR:
4852 case CONST_CAST_EXPR:
4853 case DYNAMIC_CAST_EXPR:
4854 case IMPLICIT_CONV_EXPR:
4855 if (TREE_TYPE (*tp))
4856 WALK_SUBTREE (TREE_TYPE (*tp));
4859 int i;
4860 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (*tp)); ++i)
4861 WALK_SUBTREE (TREE_OPERAND (*tp, i));
4863 *walk_subtrees_p = 0;
4864 break;
4866 case TRAIT_EXPR:
4867 WALK_SUBTREE (TRAIT_EXPR_TYPE1 (*tp));
4868 WALK_SUBTREE (TRAIT_EXPR_TYPE2 (*tp));
4869 *walk_subtrees_p = 0;
4870 break;
4872 case DECLTYPE_TYPE:
4873 WALK_SUBTREE (DECLTYPE_TYPE_EXPR (*tp));
4874 *walk_subtrees_p = 0;
4875 break;
4877 case REQUIRES_EXPR:
4878 // Only recurse through the nested expression. Do not
4879 // walk the parameter list. Doing so causes false
4880 // positives in the pack expansion checker since the
4881 // requires parameters are introduced as pack expansions.
4882 WALK_SUBTREE (TREE_OPERAND (*tp, 1));
4883 *walk_subtrees_p = 0;
4884 break;
4886 case DECL_EXPR:
4887 /* User variables should be mentioned in BIND_EXPR_VARS
4888 and their initializers and sizes walked when walking
4889 the containing BIND_EXPR. Compiler temporaries are
4890 handled here. */
4891 if (VAR_P (TREE_OPERAND (*tp, 0))
4892 && DECL_ARTIFICIAL (TREE_OPERAND (*tp, 0))
4893 && !TREE_STATIC (TREE_OPERAND (*tp, 0)))
4895 tree decl = TREE_OPERAND (*tp, 0);
4896 WALK_SUBTREE (DECL_INITIAL (decl));
4897 WALK_SUBTREE (DECL_SIZE (decl));
4898 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
4900 break;
4902 default:
4903 return NULL_TREE;
4906 /* We didn't find what we were looking for. */
4907 out:
4908 return result;
4910 #undef WALK_SUBTREE
4913 /* Like save_expr, but for C++. */
4915 tree
4916 cp_save_expr (tree expr)
4918 /* There is no reason to create a SAVE_EXPR within a template; if
4919 needed, we can create the SAVE_EXPR when instantiating the
4920 template. Furthermore, the middle-end cannot handle C++-specific
4921 tree codes. */
4922 if (processing_template_decl)
4923 return expr;
4924 return save_expr (expr);
4927 /* Initialize tree.c. */
4929 void
4930 init_tree (void)
4932 list_hash_table = hash_table<list_hasher>::create_ggc (61);
4933 register_scoped_attributes (std_attribute_table, NULL);
4936 /* Returns the kind of special function that DECL (a FUNCTION_DECL)
4937 is. Note that sfk_none is zero, so this function can be used as a
4938 predicate to test whether or not DECL is a special function. */
4940 special_function_kind
4941 special_function_p (const_tree decl)
4943 /* Rather than doing all this stuff with magic names, we should
4944 probably have a field of type `special_function_kind' in
4945 DECL_LANG_SPECIFIC. */
4946 if (DECL_INHERITED_CTOR (decl))
4947 return sfk_inheriting_constructor;
4948 if (DECL_COPY_CONSTRUCTOR_P (decl))
4949 return sfk_copy_constructor;
4950 if (DECL_MOVE_CONSTRUCTOR_P (decl))
4951 return sfk_move_constructor;
4952 if (DECL_CONSTRUCTOR_P (decl))
4953 return sfk_constructor;
4954 if (DECL_ASSIGNMENT_OPERATOR_P (decl)
4955 && DECL_OVERLOADED_OPERATOR_IS (decl, NOP_EXPR))
4957 if (copy_fn_p (decl))
4958 return sfk_copy_assignment;
4959 if (move_fn_p (decl))
4960 return sfk_move_assignment;
4962 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
4963 return sfk_destructor;
4964 if (DECL_COMPLETE_DESTRUCTOR_P (decl))
4965 return sfk_complete_destructor;
4966 if (DECL_BASE_DESTRUCTOR_P (decl))
4967 return sfk_base_destructor;
4968 if (DECL_DELETING_DESTRUCTOR_P (decl))
4969 return sfk_deleting_destructor;
4970 if (DECL_CONV_FN_P (decl))
4971 return sfk_conversion;
4972 if (deduction_guide_p (decl))
4973 return sfk_deduction_guide;
4975 return sfk_none;
4978 /* Returns nonzero if TYPE is a character type, including wchar_t. */
4981 char_type_p (tree type)
4983 return (same_type_p (type, char_type_node)
4984 || same_type_p (type, unsigned_char_type_node)
4985 || same_type_p (type, signed_char_type_node)
4986 || same_type_p (type, char16_type_node)
4987 || same_type_p (type, char32_type_node)
4988 || same_type_p (type, wchar_type_node));
4991 /* Returns the kind of linkage associated with the indicated DECL. Th
4992 value returned is as specified by the language standard; it is
4993 independent of implementation details regarding template
4994 instantiation, etc. For example, it is possible that a declaration
4995 to which this function assigns external linkage would not show up
4996 as a global symbol when you run `nm' on the resulting object file. */
4998 linkage_kind
4999 decl_linkage (tree decl)
5001 /* This function doesn't attempt to calculate the linkage from first
5002 principles as given in [basic.link]. Instead, it makes use of
5003 the fact that we have already set TREE_PUBLIC appropriately, and
5004 then handles a few special cases. Ideally, we would calculate
5005 linkage first, and then transform that into a concrete
5006 implementation. */
5008 /* Things that don't have names have no linkage. */
5009 if (!DECL_NAME (decl))
5010 return lk_none;
5012 /* Fields have no linkage. */
5013 if (TREE_CODE (decl) == FIELD_DECL)
5014 return lk_none;
5016 /* Things that are TREE_PUBLIC have external linkage. */
5017 if (TREE_PUBLIC (decl))
5018 return lk_external;
5020 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor variants,
5021 check one of the "clones" for the real linkage. */
5022 if ((DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
5023 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl))
5024 && DECL_CHAIN (decl)
5025 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)))
5026 return decl_linkage (DECL_CHAIN (decl));
5028 if (TREE_CODE (decl) == NAMESPACE_DECL)
5029 return lk_external;
5031 /* Linkage of a CONST_DECL depends on the linkage of the enumeration
5032 type. */
5033 if (TREE_CODE (decl) == CONST_DECL)
5034 return decl_linkage (TYPE_NAME (DECL_CONTEXT (decl)));
5036 /* Things in local scope do not have linkage, if they don't have
5037 TREE_PUBLIC set. */
5038 if (decl_function_context (decl))
5039 return lk_none;
5041 /* Members of the anonymous namespace also have TREE_PUBLIC unset, but
5042 are considered to have external linkage for language purposes, as do
5043 template instantiations on targets without weak symbols. DECLs really
5044 meant to have internal linkage have DECL_THIS_STATIC set. */
5045 if (TREE_CODE (decl) == TYPE_DECL)
5046 return lk_external;
5047 if (VAR_OR_FUNCTION_DECL_P (decl))
5049 if (!DECL_THIS_STATIC (decl))
5050 return lk_external;
5052 /* Static data members and static member functions from classes
5053 in anonymous namespace also don't have TREE_PUBLIC set. */
5054 if (DECL_CLASS_CONTEXT (decl))
5055 return lk_external;
5058 /* Everything else has internal linkage. */
5059 return lk_internal;
5062 /* Returns the storage duration of the object or reference associated with
5063 the indicated DECL, which should be a VAR_DECL or PARM_DECL. */
5065 duration_kind
5066 decl_storage_duration (tree decl)
5068 if (TREE_CODE (decl) == PARM_DECL)
5069 return dk_auto;
5070 if (TREE_CODE (decl) == FUNCTION_DECL)
5071 return dk_static;
5072 gcc_assert (VAR_P (decl));
5073 if (!TREE_STATIC (decl)
5074 && !DECL_EXTERNAL (decl))
5075 return dk_auto;
5076 if (CP_DECL_THREAD_LOCAL_P (decl))
5077 return dk_thread;
5078 return dk_static;
5081 /* EXP is an expression that we want to pre-evaluate. Returns (in
5082 *INITP) an expression that will perform the pre-evaluation. The
5083 value returned by this function is a side-effect free expression
5084 equivalent to the pre-evaluated expression. Callers must ensure
5085 that *INITP is evaluated before EXP. */
5087 tree
5088 stabilize_expr (tree exp, tree* initp)
5090 tree init_expr;
5092 if (!TREE_SIDE_EFFECTS (exp))
5093 init_expr = NULL_TREE;
5094 else if (VOID_TYPE_P (TREE_TYPE (exp)))
5096 init_expr = exp;
5097 exp = void_node;
5099 /* There are no expressions with REFERENCE_TYPE, but there can be call
5100 arguments with such a type; just treat it as a pointer. */
5101 else if (TREE_CODE (TREE_TYPE (exp)) == REFERENCE_TYPE
5102 || SCALAR_TYPE_P (TREE_TYPE (exp))
5103 || !glvalue_p (exp))
5105 init_expr = get_target_expr (exp);
5106 exp = TARGET_EXPR_SLOT (init_expr);
5107 if (CLASS_TYPE_P (TREE_TYPE (exp)))
5108 exp = move (exp);
5109 else
5110 exp = rvalue (exp);
5112 else
5114 bool xval = !lvalue_p (exp);
5115 exp = cp_build_addr_expr (exp, tf_warning_or_error);
5116 init_expr = get_target_expr (exp);
5117 exp = TARGET_EXPR_SLOT (init_expr);
5118 exp = cp_build_fold_indirect_ref (exp);
5119 if (xval)
5120 exp = move (exp);
5122 *initp = init_expr;
5124 gcc_assert (!TREE_SIDE_EFFECTS (exp));
5125 return exp;
5128 /* Add NEW_EXPR, an expression whose value we don't care about, after the
5129 similar expression ORIG. */
5131 tree
5132 add_stmt_to_compound (tree orig, tree new_expr)
5134 if (!new_expr || !TREE_SIDE_EFFECTS (new_expr))
5135 return orig;
5136 if (!orig || !TREE_SIDE_EFFECTS (orig))
5137 return new_expr;
5138 return build2 (COMPOUND_EXPR, void_type_node, orig, new_expr);
5141 /* Like stabilize_expr, but for a call whose arguments we want to
5142 pre-evaluate. CALL is modified in place to use the pre-evaluated
5143 arguments, while, upon return, *INITP contains an expression to
5144 compute the arguments. */
5146 void
5147 stabilize_call (tree call, tree *initp)
5149 tree inits = NULL_TREE;
5150 int i;
5151 int nargs = call_expr_nargs (call);
5153 if (call == error_mark_node || processing_template_decl)
5155 *initp = NULL_TREE;
5156 return;
5159 gcc_assert (TREE_CODE (call) == CALL_EXPR);
5161 for (i = 0; i < nargs; i++)
5163 tree init;
5164 CALL_EXPR_ARG (call, i) =
5165 stabilize_expr (CALL_EXPR_ARG (call, i), &init);
5166 inits = add_stmt_to_compound (inits, init);
5169 *initp = inits;
5172 /* Like stabilize_expr, but for an AGGR_INIT_EXPR whose arguments we want
5173 to pre-evaluate. CALL is modified in place to use the pre-evaluated
5174 arguments, while, upon return, *INITP contains an expression to
5175 compute the arguments. */
5177 static void
5178 stabilize_aggr_init (tree call, tree *initp)
5180 tree inits = NULL_TREE;
5181 int i;
5182 int nargs = aggr_init_expr_nargs (call);
5184 if (call == error_mark_node)
5185 return;
5187 gcc_assert (TREE_CODE (call) == AGGR_INIT_EXPR);
5189 for (i = 0; i < nargs; i++)
5191 tree init;
5192 AGGR_INIT_EXPR_ARG (call, i) =
5193 stabilize_expr (AGGR_INIT_EXPR_ARG (call, i), &init);
5194 inits = add_stmt_to_compound (inits, init);
5197 *initp = inits;
5200 /* Like stabilize_expr, but for an initialization.
5202 If the initialization is for an object of class type, this function
5203 takes care not to introduce additional temporaries.
5205 Returns TRUE iff the expression was successfully pre-evaluated,
5206 i.e., if INIT is now side-effect free, except for, possibly, a
5207 single call to a constructor. */
5209 bool
5210 stabilize_init (tree init, tree *initp)
5212 tree t = init;
5214 *initp = NULL_TREE;
5216 if (t == error_mark_node || processing_template_decl)
5217 return true;
5219 if (TREE_CODE (t) == INIT_EXPR)
5220 t = TREE_OPERAND (t, 1);
5221 if (TREE_CODE (t) == TARGET_EXPR)
5222 t = TARGET_EXPR_INITIAL (t);
5224 /* If the RHS can be stabilized without breaking copy elision, stabilize
5225 it. We specifically don't stabilize class prvalues here because that
5226 would mean an extra copy, but they might be stabilized below. */
5227 if (TREE_CODE (init) == INIT_EXPR
5228 && TREE_CODE (t) != CONSTRUCTOR
5229 && TREE_CODE (t) != AGGR_INIT_EXPR
5230 && (SCALAR_TYPE_P (TREE_TYPE (t))
5231 || glvalue_p (t)))
5233 TREE_OPERAND (init, 1) = stabilize_expr (t, initp);
5234 return true;
5237 if (TREE_CODE (t) == COMPOUND_EXPR
5238 && TREE_CODE (init) == INIT_EXPR)
5240 tree last = expr_last (t);
5241 /* Handle stabilizing the EMPTY_CLASS_EXPR pattern. */
5242 if (!TREE_SIDE_EFFECTS (last))
5244 *initp = t;
5245 TREE_OPERAND (init, 1) = last;
5246 return true;
5250 if (TREE_CODE (t) == CONSTRUCTOR)
5252 /* Aggregate initialization: stabilize each of the field
5253 initializers. */
5254 unsigned i;
5255 constructor_elt *ce;
5256 bool good = true;
5257 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
5258 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
5260 tree type = TREE_TYPE (ce->value);
5261 tree subinit;
5262 if (TREE_CODE (type) == REFERENCE_TYPE
5263 || SCALAR_TYPE_P (type))
5264 ce->value = stabilize_expr (ce->value, &subinit);
5265 else if (!stabilize_init (ce->value, &subinit))
5266 good = false;
5267 *initp = add_stmt_to_compound (*initp, subinit);
5269 return good;
5272 if (TREE_CODE (t) == CALL_EXPR)
5274 stabilize_call (t, initp);
5275 return true;
5278 if (TREE_CODE (t) == AGGR_INIT_EXPR)
5280 stabilize_aggr_init (t, initp);
5281 return true;
5284 /* The initialization is being performed via a bitwise copy -- and
5285 the item copied may have side effects. */
5286 return !TREE_SIDE_EFFECTS (init);
5289 /* Returns true if a cast to TYPE may appear in an integral constant
5290 expression. */
5292 bool
5293 cast_valid_in_integral_constant_expression_p (tree type)
5295 return (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5296 || cxx_dialect >= cxx11
5297 || dependent_type_p (type)
5298 || type == error_mark_node);
5301 /* Return true if we need to fix linkage information of DECL. */
5303 static bool
5304 cp_fix_function_decl_p (tree decl)
5306 /* Skip if DECL is not externally visible. */
5307 if (!TREE_PUBLIC (decl))
5308 return false;
5310 /* We need to fix DECL if it a appears to be exported but with no
5311 function body. Thunks do not have CFGs and we may need to
5312 handle them specially later. */
5313 if (!gimple_has_body_p (decl)
5314 && !DECL_THUNK_P (decl)
5315 && !DECL_EXTERNAL (decl))
5317 struct cgraph_node *node = cgraph_node::get (decl);
5319 /* Don't fix same_body aliases. Although they don't have their own
5320 CFG, they share it with what they alias to. */
5321 if (!node || !node->alias
5322 || !vec_safe_length (node->ref_list.references))
5323 return true;
5326 return false;
5329 /* Clean the C++ specific parts of the tree T. */
5331 void
5332 cp_free_lang_data (tree t)
5334 if (TREE_CODE (t) == METHOD_TYPE
5335 || TREE_CODE (t) == FUNCTION_TYPE)
5337 /* Default args are not interesting anymore. */
5338 tree argtypes = TYPE_ARG_TYPES (t);
5339 while (argtypes)
5341 TREE_PURPOSE (argtypes) = 0;
5342 argtypes = TREE_CHAIN (argtypes);
5345 else if (TREE_CODE (t) == FUNCTION_DECL
5346 && cp_fix_function_decl_p (t))
5348 /* If T is used in this translation unit at all, the definition
5349 must exist somewhere else since we have decided to not emit it
5350 in this TU. So make it an external reference. */
5351 DECL_EXTERNAL (t) = 1;
5352 TREE_STATIC (t) = 0;
5354 if (TREE_CODE (t) == NAMESPACE_DECL)
5355 /* We do not need the leftover chaining of namespaces from the
5356 binding level. */
5357 DECL_CHAIN (t) = NULL_TREE;
5360 /* Stub for c-common. Please keep in sync with c-decl.c.
5361 FIXME: If address space support is target specific, then this
5362 should be a C target hook. But currently this is not possible,
5363 because this function is called via REGISTER_TARGET_PRAGMAS. */
5364 void
5365 c_register_addr_space (const char * /*word*/, addr_space_t /*as*/)
5369 /* Return the number of operands in T that we care about for things like
5370 mangling. */
5373 cp_tree_operand_length (const_tree t)
5375 enum tree_code code = TREE_CODE (t);
5377 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
5378 return VL_EXP_OPERAND_LENGTH (t);
5380 return cp_tree_code_length (code);
5383 /* Like cp_tree_operand_length, but takes a tree_code CODE. */
5386 cp_tree_code_length (enum tree_code code)
5388 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
5390 switch (code)
5392 case PREINCREMENT_EXPR:
5393 case PREDECREMENT_EXPR:
5394 case POSTINCREMENT_EXPR:
5395 case POSTDECREMENT_EXPR:
5396 return 1;
5398 case ARRAY_REF:
5399 return 2;
5401 case EXPR_PACK_EXPANSION:
5402 return 1;
5404 default:
5405 return TREE_CODE_LENGTH (code);
5409 /* Wrapper around warn_deprecated_use that doesn't warn for
5410 current_class_type. */
5412 void
5413 cp_warn_deprecated_use (tree node)
5415 if (TYPE_P (node)
5416 && current_class_type
5417 && TYPE_MAIN_VARIANT (node) == current_class_type)
5418 return;
5419 warn_deprecated_use (node, NULL_TREE);
5422 /* Implement -Wzero_as_null_pointer_constant. Return true if the
5423 conditions for the warning hold, false otherwise. */
5424 bool
5425 maybe_warn_zero_as_null_pointer_constant (tree expr, location_t loc)
5427 if (c_inhibit_evaluation_warnings == 0
5428 && !NULLPTR_TYPE_P (TREE_TYPE (expr)))
5430 warning_at (loc, OPT_Wzero_as_null_pointer_constant,
5431 "zero as null pointer constant");
5432 return true;
5434 return false;
5437 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
5438 /* Complain that some language-specific thing hanging off a tree
5439 node has been accessed improperly. */
5441 void
5442 lang_check_failed (const char* file, int line, const char* function)
5444 internal_error ("lang_* check: failed in %s, at %s:%d",
5445 function, trim_filename (file), line);
5447 #endif /* ENABLE_TREE_CHECKING */
5449 #if CHECKING_P
5451 namespace selftest {
5453 /* Verify that lvalue_kind () works, for various expressions,
5454 and that location wrappers don't affect the results. */
5456 static void
5457 test_lvalue_kind ()
5459 location_t loc = BUILTINS_LOCATION;
5461 /* Verify constants and parameters, without and with
5462 location wrappers. */
5463 tree int_cst = build_int_cst (integer_type_node, 42);
5464 ASSERT_EQ (clk_none, lvalue_kind (int_cst));
5466 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
5467 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
5468 ASSERT_EQ (clk_none, lvalue_kind (wrapped_int_cst));
5470 tree string_lit = build_string (4, "foo");
5471 TREE_TYPE (string_lit) = char_array_type_node;
5472 string_lit = fix_string_type (string_lit);
5473 ASSERT_EQ (clk_ordinary, lvalue_kind (string_lit));
5475 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
5476 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
5477 ASSERT_EQ (clk_ordinary, lvalue_kind (wrapped_string_lit));
5479 tree parm = build_decl (UNKNOWN_LOCATION, PARM_DECL,
5480 get_identifier ("some_parm"),
5481 integer_type_node);
5482 ASSERT_EQ (clk_ordinary, lvalue_kind (parm));
5484 tree wrapped_parm = maybe_wrap_with_location (parm, loc);
5485 ASSERT_TRUE (location_wrapper_p (wrapped_parm));
5486 ASSERT_EQ (clk_ordinary, lvalue_kind (wrapped_parm));
5488 /* Verify that lvalue_kind of std::move on a parm isn't
5489 affected by location wrappers. */
5490 tree rvalue_ref_of_parm = move (parm);
5491 ASSERT_EQ (clk_rvalueref, lvalue_kind (rvalue_ref_of_parm));
5492 tree rvalue_ref_of_wrapped_parm = move (wrapped_parm);
5493 ASSERT_EQ (clk_rvalueref, lvalue_kind (rvalue_ref_of_wrapped_parm));
5496 /* Run all of the selftests within this file. */
5498 void
5499 cp_tree_c_tests ()
5501 test_lvalue_kind ();
5504 } // namespace selftest
5506 #endif /* #if CHECKING_P */
5509 #include "gt-cp-tree.h"