c++: track whether we expect a TARGET_EXPR to be elided
[official-gcc.git] / gcc / cp / tree.cc
blob3532e44279fa3f38dda16367bdf990a0e35eac35
1 /* Language-dependent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2022 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 *);
47 static tree handle_init_priority_attribute (tree *, tree, tree, int, bool *);
48 static tree handle_abi_tag_attribute (tree *, tree, tree, int, bool *);
50 /* If REF is an lvalue, returns the kind of lvalue that REF is.
51 Otherwise, returns clk_none. */
53 cp_lvalue_kind
54 lvalue_kind (const_tree ref)
56 cp_lvalue_kind op1_lvalue_kind = clk_none;
57 cp_lvalue_kind op2_lvalue_kind = clk_none;
59 /* Expressions of reference type are sometimes wrapped in
60 INDIRECT_REFs. INDIRECT_REFs are just internal compiler
61 representation, not part of the language, so we have to look
62 through them. */
63 if (REFERENCE_REF_P (ref))
64 return lvalue_kind (TREE_OPERAND (ref, 0));
66 if (TREE_TYPE (ref)
67 && TYPE_REF_P (TREE_TYPE (ref)))
69 /* unnamed rvalue references are rvalues */
70 if (TYPE_REF_IS_RVALUE (TREE_TYPE (ref))
71 && TREE_CODE (ref) != PARM_DECL
72 && !VAR_P (ref)
73 && TREE_CODE (ref) != COMPONENT_REF
74 /* Functions are always lvalues. */
75 && TREE_CODE (TREE_TYPE (TREE_TYPE (ref))) != FUNCTION_TYPE)
77 op1_lvalue_kind = clk_rvalueref;
78 if (implicit_rvalue_p (ref))
79 op1_lvalue_kind |= clk_implicit_rval;
80 return op1_lvalue_kind;
83 /* lvalue references and named rvalue references are lvalues. */
84 return clk_ordinary;
87 if (ref == current_class_ptr)
88 return clk_none;
90 /* Expressions with cv void type are prvalues. */
91 if (TREE_TYPE (ref) && VOID_TYPE_P (TREE_TYPE (ref)))
92 return clk_none;
94 switch (TREE_CODE (ref))
96 case SAVE_EXPR:
97 return clk_none;
99 /* preincrements and predecrements are valid lvals, provided
100 what they refer to are valid lvals. */
101 case PREINCREMENT_EXPR:
102 case PREDECREMENT_EXPR:
103 case TRY_CATCH_EXPR:
104 case REALPART_EXPR:
105 case IMAGPART_EXPR:
106 case VIEW_CONVERT_EXPR:
107 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
108 /* As for ARRAY_REF and COMPONENT_REF, these codes turn a class prvalue
109 into an xvalue: we need to materialize the temporary before we mess
110 with it. Except VIEW_CONVERT_EXPR that doesn't actually change the
111 type, as in location wrapper and REF_PARENTHESIZED_P. */
112 if (op1_lvalue_kind == clk_class
113 && !(TREE_CODE (ref) == VIEW_CONVERT_EXPR
114 && (same_type_ignoring_top_level_qualifiers_p
115 (TREE_TYPE (ref), TREE_TYPE (TREE_OPERAND (ref, 0))))))
116 return clk_rvalueref;
117 return op1_lvalue_kind;
119 case ARRAY_REF:
121 tree op1 = TREE_OPERAND (ref, 0);
122 if (TREE_CODE (TREE_TYPE (op1)) == ARRAY_TYPE)
124 op1_lvalue_kind = lvalue_kind (op1);
125 if (op1_lvalue_kind == clk_class)
126 /* in the case of an array operand, the result is an lvalue if
127 that operand is an lvalue and an xvalue otherwise */
128 op1_lvalue_kind = clk_rvalueref;
129 return op1_lvalue_kind;
131 else
132 return clk_ordinary;
135 case MEMBER_REF:
136 case DOTSTAR_EXPR:
137 if (TREE_CODE (ref) == MEMBER_REF)
138 op1_lvalue_kind = clk_ordinary;
139 else
140 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
141 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
142 op1_lvalue_kind = clk_none;
143 else if (op1_lvalue_kind == clk_class)
144 /* The result of a .* expression whose second operand is a pointer to a
145 data member is an lvalue if the first operand is an lvalue and an
146 xvalue otherwise. */
147 op1_lvalue_kind = clk_rvalueref;
148 return op1_lvalue_kind;
150 case COMPONENT_REF:
151 if (BASELINK_P (TREE_OPERAND (ref, 1)))
153 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (ref, 1));
155 /* For static member function recurse on the BASELINK, we can get
156 here e.g. from reference_binding. If BASELINK_FUNCTIONS is
157 OVERLOAD, the overload is resolved first if possible through
158 resolve_address_of_overloaded_function. */
159 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_STATIC_FUNCTION_P (fn))
160 return lvalue_kind (TREE_OPERAND (ref, 1));
162 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
163 if (op1_lvalue_kind == clk_class)
164 /* If E1 is an lvalue, then E1.E2 is an lvalue;
165 otherwise E1.E2 is an xvalue. */
166 op1_lvalue_kind = clk_rvalueref;
168 /* Look at the member designator. */
169 if (!op1_lvalue_kind)
171 else if (is_overloaded_fn (TREE_OPERAND (ref, 1)))
172 /* The "field" can be a FUNCTION_DECL or an OVERLOAD in some
173 situations. If we're seeing a COMPONENT_REF, it's a non-static
174 member, so it isn't an lvalue. */
175 op1_lvalue_kind = clk_none;
176 else if (TREE_CODE (TREE_OPERAND (ref, 1)) != FIELD_DECL)
177 /* This can be IDENTIFIER_NODE in a template. */;
178 else if (DECL_C_BIT_FIELD (TREE_OPERAND (ref, 1)))
180 /* Clear the ordinary bit. If this object was a class
181 rvalue we want to preserve that information. */
182 op1_lvalue_kind &= ~clk_ordinary;
183 /* The lvalue is for a bitfield. */
184 op1_lvalue_kind |= clk_bitfield;
186 else if (DECL_PACKED (TREE_OPERAND (ref, 1)))
187 op1_lvalue_kind |= clk_packed;
189 return op1_lvalue_kind;
191 case STRING_CST:
192 case COMPOUND_LITERAL_EXPR:
193 return clk_ordinary;
195 case CONST_DECL:
196 /* CONST_DECL without TREE_STATIC are enumeration values and
197 thus not lvalues. With TREE_STATIC they are used by ObjC++
198 in objc_build_string_object and need to be considered as
199 lvalues. */
200 if (! TREE_STATIC (ref))
201 return clk_none;
202 /* FALLTHRU */
203 case VAR_DECL:
204 if (VAR_P (ref) && DECL_HAS_VALUE_EXPR_P (ref))
205 return lvalue_kind (DECL_VALUE_EXPR (CONST_CAST_TREE (ref)));
207 if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
208 && DECL_LANG_SPECIFIC (ref)
209 && DECL_IN_AGGR_P (ref))
210 return clk_none;
211 /* FALLTHRU */
212 case INDIRECT_REF:
213 case ARROW_EXPR:
214 case PARM_DECL:
215 case RESULT_DECL:
216 case PLACEHOLDER_EXPR:
217 return clk_ordinary;
219 /* A scope ref in a template, left as SCOPE_REF to support later
220 access checking. */
221 case SCOPE_REF:
222 gcc_assert (!type_dependent_expression_p (CONST_CAST_TREE (ref)));
224 tree op = TREE_OPERAND (ref, 1);
225 if (TREE_CODE (op) == FIELD_DECL)
226 return (DECL_C_BIT_FIELD (op) ? clk_bitfield : clk_ordinary);
227 else
228 return lvalue_kind (op);
231 case MAX_EXPR:
232 case MIN_EXPR:
233 /* Disallow <? and >? as lvalues if either argument side-effects. */
234 if (TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 0))
235 || TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 1)))
236 return clk_none;
237 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
238 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1));
239 break;
241 case COND_EXPR:
242 if (processing_template_decl)
244 /* Within templates, a REFERENCE_TYPE will indicate whether
245 the COND_EXPR result is an ordinary lvalue or rvalueref.
246 Since REFERENCE_TYPEs are handled above, if we reach this
247 point, we know we got a plain rvalue. Unless we have a
248 type-dependent expr, that is, but we shouldn't be testing
249 lvalueness if we can't even tell the types yet! */
250 gcc_assert (!type_dependent_expression_p (CONST_CAST_TREE (ref)));
251 goto default_;
254 tree op1 = TREE_OPERAND (ref, 1);
255 if (!op1) op1 = TREE_OPERAND (ref, 0);
256 tree op2 = TREE_OPERAND (ref, 2);
257 op1_lvalue_kind = lvalue_kind (op1);
258 op2_lvalue_kind = lvalue_kind (op2);
259 if (!op1_lvalue_kind != !op2_lvalue_kind)
261 /* The second or the third operand (but not both) is a
262 throw-expression; the result is of the type
263 and value category of the other. */
264 if (op1_lvalue_kind && TREE_CODE (op2) == THROW_EXPR)
265 op2_lvalue_kind = op1_lvalue_kind;
266 else if (op2_lvalue_kind && TREE_CODE (op1) == THROW_EXPR)
267 op1_lvalue_kind = op2_lvalue_kind;
270 break;
272 case MODOP_EXPR:
273 /* We expect to see unlowered MODOP_EXPRs only during
274 template processing. */
275 gcc_assert (processing_template_decl);
276 return clk_ordinary;
278 case MODIFY_EXPR:
279 case TYPEID_EXPR:
280 return clk_ordinary;
282 case COMPOUND_EXPR:
283 return lvalue_kind (TREE_OPERAND (ref, 1));
285 case TARGET_EXPR:
286 return clk_class;
288 case VA_ARG_EXPR:
289 return (CLASS_TYPE_P (TREE_TYPE (ref)) ? clk_class : clk_none);
291 case CALL_EXPR:
292 /* We can see calls outside of TARGET_EXPR in templates. */
293 if (CLASS_TYPE_P (TREE_TYPE (ref)))
294 return clk_class;
295 return clk_none;
297 case FUNCTION_DECL:
298 /* All functions (except non-static-member functions) are
299 lvalues. */
300 return (DECL_NONSTATIC_MEMBER_FUNCTION_P (ref)
301 ? clk_none : clk_ordinary);
303 case BASELINK:
304 /* We now represent a reference to a single static member function
305 with a BASELINK. */
306 /* This CONST_CAST is okay because BASELINK_FUNCTIONS returns
307 its argument unmodified and we assign it to a const_tree. */
308 return lvalue_kind (BASELINK_FUNCTIONS (CONST_CAST_TREE (ref)));
310 case NON_DEPENDENT_EXPR:
311 case PAREN_EXPR:
312 return lvalue_kind (TREE_OPERAND (ref, 0));
314 case TEMPLATE_PARM_INDEX:
315 if (CLASS_TYPE_P (TREE_TYPE (ref)))
316 /* A template parameter object is an lvalue. */
317 return clk_ordinary;
318 return clk_none;
320 default:
321 default_:
322 if (!TREE_TYPE (ref))
323 return clk_none;
324 if (CLASS_TYPE_P (TREE_TYPE (ref))
325 || TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE)
326 return clk_class;
327 return clk_none;
330 /* If one operand is not an lvalue at all, then this expression is
331 not an lvalue. */
332 if (!op1_lvalue_kind || !op2_lvalue_kind)
333 return clk_none;
335 /* Otherwise, it's an lvalue, and it has all the odd properties
336 contributed by either operand. */
337 op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind;
338 /* It's not an ordinary lvalue if it involves any other kind. */
339 if ((op1_lvalue_kind & ~clk_ordinary) != clk_none)
340 op1_lvalue_kind &= ~clk_ordinary;
341 /* It can't be both a pseudo-lvalue and a non-addressable lvalue.
342 A COND_EXPR of those should be wrapped in a TARGET_EXPR. */
343 if ((op1_lvalue_kind & (clk_rvalueref|clk_class))
344 && (op1_lvalue_kind & (clk_bitfield|clk_packed)))
345 op1_lvalue_kind = clk_none;
346 return op1_lvalue_kind;
349 /* Returns the kind of lvalue that REF is, in the sense of [basic.lval]. */
351 cp_lvalue_kind
352 real_lvalue_p (const_tree ref)
354 cp_lvalue_kind kind = lvalue_kind (ref);
355 if (kind & (clk_rvalueref|clk_class))
356 return clk_none;
357 else
358 return kind;
361 /* c-common wants us to return bool. */
363 bool
364 lvalue_p (const_tree t)
366 return real_lvalue_p (t);
369 /* This differs from lvalue_p in that xvalues are included. */
371 bool
372 glvalue_p (const_tree ref)
374 cp_lvalue_kind kind = lvalue_kind (ref);
375 if (kind & clk_class)
376 return false;
377 else
378 return (kind != clk_none);
381 /* This differs from glvalue_p in that class prvalues are included. */
383 bool
384 obvalue_p (const_tree ref)
386 return (lvalue_kind (ref) != clk_none);
389 /* Returns true if REF is an xvalue (the result of dereferencing an rvalue
390 reference), false otherwise. */
392 bool
393 xvalue_p (const_tree ref)
395 return (lvalue_kind (ref) & clk_rvalueref);
398 /* True if REF is a bit-field. */
400 bool
401 bitfield_p (const_tree ref)
403 return (lvalue_kind (ref) & clk_bitfield);
406 /* C++-specific version of stabilize_reference. */
408 tree
409 cp_stabilize_reference (tree ref)
411 STRIP_ANY_LOCATION_WRAPPER (ref);
412 switch (TREE_CODE (ref))
414 case NON_DEPENDENT_EXPR:
415 /* We aren't actually evaluating this. */
416 return ref;
418 /* We need to treat specially anything stabilize_reference doesn't
419 handle specifically. */
420 case VAR_DECL:
421 case PARM_DECL:
422 case RESULT_DECL:
423 CASE_CONVERT:
424 case FLOAT_EXPR:
425 case FIX_TRUNC_EXPR:
426 case INDIRECT_REF:
427 case COMPONENT_REF:
428 case BIT_FIELD_REF:
429 case ARRAY_REF:
430 case ARRAY_RANGE_REF:
431 case ERROR_MARK:
432 break;
433 default:
434 cp_lvalue_kind kind = lvalue_kind (ref);
435 if ((kind & ~clk_class) != clk_none)
437 tree type = unlowered_expr_type (ref);
438 bool rval = !!(kind & clk_rvalueref);
439 type = cp_build_reference_type (type, rval);
440 /* This inhibits warnings in, eg, cxx_mark_addressable
441 (c++/60955). */
442 warning_sentinel s (extra_warnings);
443 ref = build_static_cast (input_location, type, ref,
444 tf_error);
448 return stabilize_reference (ref);
451 /* Test whether DECL is a builtin that may appear in a
452 constant-expression. */
454 bool
455 builtin_valid_in_constant_expr_p (const_tree decl)
457 STRIP_ANY_LOCATION_WRAPPER (decl);
458 if (TREE_CODE (decl) != FUNCTION_DECL)
459 /* Not a function. */
460 return false;
461 if (DECL_BUILT_IN_CLASS (decl) != BUILT_IN_NORMAL)
463 if (fndecl_built_in_p (decl, BUILT_IN_FRONTEND))
464 switch (DECL_FE_FUNCTION_CODE (decl))
466 case CP_BUILT_IN_IS_CONSTANT_EVALUATED:
467 case CP_BUILT_IN_SOURCE_LOCATION:
468 case CP_BUILT_IN_IS_CORRESPONDING_MEMBER:
469 case CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS:
470 return true;
471 default:
472 break;
474 /* Not a built-in. */
475 return false;
477 switch (DECL_FUNCTION_CODE (decl))
479 /* These always have constant results like the corresponding
480 macros/symbol. */
481 case BUILT_IN_FILE:
482 case BUILT_IN_FUNCTION:
483 case BUILT_IN_LINE:
485 /* The following built-ins are valid in constant expressions
486 when their arguments are. */
487 case BUILT_IN_ADD_OVERFLOW_P:
488 case BUILT_IN_SUB_OVERFLOW_P:
489 case BUILT_IN_MUL_OVERFLOW_P:
491 /* These have constant results even if their operands are
492 non-constant. */
493 case BUILT_IN_CONSTANT_P:
494 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
495 return true;
496 default:
497 return false;
501 /* Build a TARGET_EXPR, initializing the DECL with the VALUE. */
503 static tree
504 build_target_expr (tree decl, tree value, tsubst_flags_t complain)
506 tree t;
507 tree type = TREE_TYPE (decl);
509 value = mark_rvalue_use (value);
511 gcc_checking_assert (VOID_TYPE_P (TREE_TYPE (value))
512 || TREE_TYPE (decl) == TREE_TYPE (value)
513 /* On ARM ctors return 'this'. */
514 || (TYPE_PTR_P (TREE_TYPE (value))
515 && TREE_CODE (value) == CALL_EXPR)
516 || useless_type_conversion_p (TREE_TYPE (decl),
517 TREE_TYPE (value)));
519 /* Set TREE_READONLY for optimization, such as gimplify_init_constructor
520 moving a constant aggregate into .rodata. */
521 if (CP_TYPE_CONST_NON_VOLATILE_P (type)
522 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
523 && !VOID_TYPE_P (TREE_TYPE (value))
524 && reduced_constant_expression_p (value))
525 TREE_READONLY (decl) = true;
527 if (complain & tf_no_cleanup)
528 /* The caller is building a new-expr and does not need a cleanup. */
529 t = NULL_TREE;
530 else
532 t = cxx_maybe_build_cleanup (decl, complain);
533 if (t == error_mark_node)
534 return error_mark_node;
537 set_target_expr_eliding (value);
539 t = build4 (TARGET_EXPR, type, decl, value, t, NULL_TREE);
540 if (location_t eloc = cp_expr_location (value))
541 SET_EXPR_LOCATION (t, eloc);
542 /* We always set TREE_SIDE_EFFECTS so that expand_expr does not
543 ignore the TARGET_EXPR. If there really turn out to be no
544 side-effects, then the optimizer should be able to get rid of
545 whatever code is generated anyhow. */
546 TREE_SIDE_EFFECTS (t) = 1;
548 return t;
551 /* Return an undeclared local temporary of type TYPE for use in building a
552 TARGET_EXPR. */
554 tree
555 build_local_temp (tree type)
557 tree slot = build_decl (input_location,
558 VAR_DECL, NULL_TREE, type);
559 DECL_ARTIFICIAL (slot) = 1;
560 DECL_IGNORED_P (slot) = 1;
561 DECL_CONTEXT (slot) = current_function_decl;
562 layout_decl (slot, 0);
563 return slot;
566 /* Return whether DECL is such a local temporary (or one from
567 create_tmp_var_raw). */
569 bool
570 is_local_temp (tree decl)
572 return (VAR_P (decl) && DECL_ARTIFICIAL (decl)
573 && !TREE_STATIC (decl));
576 /* Set various status flags when building an AGGR_INIT_EXPR object T. */
578 static void
579 process_aggr_init_operands (tree t)
581 bool side_effects;
583 side_effects = TREE_SIDE_EFFECTS (t);
584 if (!side_effects)
586 int i, n;
587 n = TREE_OPERAND_LENGTH (t);
588 for (i = 1; i < n; i++)
590 tree op = TREE_OPERAND (t, i);
591 if (op && TREE_SIDE_EFFECTS (op))
593 side_effects = 1;
594 break;
598 TREE_SIDE_EFFECTS (t) = side_effects;
601 /* Build an AGGR_INIT_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE,
602 FN, and SLOT. NARGS is the number of call arguments which are specified
603 as a tree array ARGS. */
605 static tree
606 build_aggr_init_array (tree return_type, tree fn, tree slot, int nargs,
607 tree *args)
609 tree t;
610 int i;
612 t = build_vl_exp (AGGR_INIT_EXPR, nargs + 3);
613 TREE_TYPE (t) = return_type;
614 AGGR_INIT_EXPR_FN (t) = fn;
615 AGGR_INIT_EXPR_SLOT (t) = slot;
616 for (i = 0; i < nargs; i++)
617 AGGR_INIT_EXPR_ARG (t, i) = args[i];
618 process_aggr_init_operands (t);
619 return t;
622 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
623 target. TYPE is the type to be initialized.
625 Build an AGGR_INIT_EXPR to represent the initialization. This function
626 differs from build_cplus_new in that an AGGR_INIT_EXPR can only be used
627 to initialize another object, whereas a TARGET_EXPR can either
628 initialize another object or create its own temporary object, and as a
629 result building up a TARGET_EXPR requires that the type's destructor be
630 callable. */
632 tree
633 build_aggr_init_expr (tree type, tree init)
635 tree fn;
636 tree slot;
637 tree rval;
638 int is_ctor;
640 gcc_assert (!VOID_TYPE_P (type));
642 /* Don't build AGGR_INIT_EXPR in a template. */
643 if (processing_template_decl)
644 return init;
646 fn = cp_get_callee (init);
647 if (fn == NULL_TREE)
648 return convert (type, init);
650 is_ctor = (TREE_CODE (fn) == ADDR_EXPR
651 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
652 && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0)));
654 /* We split the CALL_EXPR into its function and its arguments here.
655 Then, in expand_expr, we put them back together. The reason for
656 this is that this expression might be a default argument
657 expression. In that case, we need a new temporary every time the
658 expression is used. That's what break_out_target_exprs does; it
659 replaces every AGGR_INIT_EXPR with a copy that uses a fresh
660 temporary slot. Then, expand_expr builds up a call-expression
661 using the new slot. */
663 /* If we don't need to use a constructor to create an object of this
664 type, don't mess with AGGR_INIT_EXPR. */
665 if (is_ctor || TREE_ADDRESSABLE (type))
667 slot = build_local_temp (type);
669 if (TREE_CODE (init) == CALL_EXPR)
671 rval = build_aggr_init_array (void_type_node, fn, slot,
672 call_expr_nargs (init),
673 CALL_EXPR_ARGP (init));
674 AGGR_INIT_FROM_THUNK_P (rval)
675 = CALL_FROM_THUNK_P (init);
677 else
679 rval = build_aggr_init_array (void_type_node, fn, slot,
680 aggr_init_expr_nargs (init),
681 AGGR_INIT_EXPR_ARGP (init));
682 AGGR_INIT_FROM_THUNK_P (rval)
683 = AGGR_INIT_FROM_THUNK_P (init);
685 TREE_SIDE_EFFECTS (rval) = 1;
686 AGGR_INIT_VIA_CTOR_P (rval) = is_ctor;
687 TREE_NOTHROW (rval) = TREE_NOTHROW (init);
688 CALL_EXPR_OPERATOR_SYNTAX (rval) = CALL_EXPR_OPERATOR_SYNTAX (init);
689 CALL_EXPR_ORDERED_ARGS (rval) = CALL_EXPR_ORDERED_ARGS (init);
690 CALL_EXPR_REVERSE_ARGS (rval) = CALL_EXPR_REVERSE_ARGS (init);
692 else
693 rval = init;
695 return rval;
698 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
699 target. TYPE is the type that this initialization should appear to
700 have.
702 Build an encapsulation of the initialization to perform
703 and return it so that it can be processed by language-independent
704 and language-specific expression expanders. */
706 tree
707 build_cplus_new (tree type, tree init, tsubst_flags_t complain)
709 /* This function should cope with what build_special_member_call
710 can produce. When performing parenthesized aggregate initialization,
711 it can produce a { }. */
712 if (BRACE_ENCLOSED_INITIALIZER_P (init))
714 gcc_assert (cxx_dialect >= cxx20);
715 return finish_compound_literal (type, init, complain);
718 tree rval = build_aggr_init_expr (type, init);
719 tree slot;
721 if (init == error_mark_node)
722 return error_mark_node;
724 if (!complete_type_or_maybe_complain (type, init, complain))
725 return error_mark_node;
727 /* Make sure that we're not trying to create an instance of an
728 abstract class. */
729 if (abstract_virtuals_error (NULL_TREE, type, complain))
730 return error_mark_node;
732 if (TREE_CODE (rval) == AGGR_INIT_EXPR)
733 slot = AGGR_INIT_EXPR_SLOT (rval);
734 else if (TREE_CODE (rval) == CALL_EXPR
735 || TREE_CODE (rval) == CONSTRUCTOR)
736 slot = build_local_temp (type);
737 else
738 return rval;
740 rval = build_target_expr (slot, rval, complain);
742 if (rval != error_mark_node)
743 TARGET_EXPR_IMPLICIT_P (rval) = 1;
745 return rval;
748 /* Subroutine of build_vec_init_expr: Build up a single element
749 intialization as a proxy for the full array initialization to get things
750 marked as used and any appropriate diagnostics.
752 This used to be necessary because we were deferring building the actual
753 constructor calls until gimplification time; now we only do it to set
754 VEC_INIT_EXPR_IS_CONSTEXPR.
756 We assume that init is either NULL_TREE, {}, void_type_node (indicating
757 value-initialization), or another array to copy. */
759 static tree
760 build_vec_init_elt (tree type, tree init, tsubst_flags_t complain)
762 tree inner_type = strip_array_types (type);
764 if (integer_zerop (array_type_nelts_total (type))
765 || !CLASS_TYPE_P (inner_type))
766 /* No interesting initialization to do. */
767 return integer_zero_node;
768 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
770 /* Even if init has initializers for some array elements,
771 we're interested in the {}-init of trailing elements. */
772 if (CP_AGGREGATE_TYPE_P (inner_type))
774 tree empty = build_constructor (init_list_type_node, nullptr);
775 return digest_init (inner_type, empty, complain);
777 else
778 /* It's equivalent to value-init. */
779 init = void_type_node;
781 if (init == void_type_node)
782 return build_value_init (inner_type, complain);
784 releasing_vec argvec;
785 if (init && !BRACE_ENCLOSED_INITIALIZER_P (init))
787 gcc_assert (same_type_ignoring_top_level_qualifiers_p
788 (type, TREE_TYPE (init)));
789 tree init_type = strip_array_types (TREE_TYPE (init));
790 tree dummy = build_dummy_object (init_type);
791 if (!lvalue_p (init))
792 dummy = move (dummy);
793 argvec->quick_push (dummy);
795 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
796 &argvec, inner_type, LOOKUP_NORMAL,
797 complain);
799 /* For a trivial constructor, build_over_call creates a TARGET_EXPR. But
800 we don't want one here because we aren't creating a temporary. */
801 if (TREE_CODE (init) == TARGET_EXPR)
802 init = TARGET_EXPR_INITIAL (init);
804 return init;
807 /* Return a TARGET_EXPR which expresses the initialization of an array to
808 be named later, either default-initialization or copy-initialization
809 from another array of the same type. */
811 tree
812 build_vec_init_expr (tree type, tree init, tsubst_flags_t complain)
814 if (tree vi = get_vec_init_expr (init))
815 return vi;
817 tree elt_init;
818 if (init && TREE_CODE (init) == CONSTRUCTOR
819 && !BRACE_ENCLOSED_INITIALIZER_P (init))
820 /* We built any needed constructor calls in digest_init. */
821 elt_init = init;
822 else
823 elt_init = build_vec_init_elt (type, init, complain);
825 bool value_init = false;
826 if (init == void_type_node)
828 value_init = true;
829 init = NULL_TREE;
832 tree slot = build_local_temp (type);
833 init = build2 (VEC_INIT_EXPR, type, slot, init);
834 TREE_SIDE_EFFECTS (init) = true;
835 SET_EXPR_LOCATION (init, input_location);
837 if (cxx_dialect >= cxx11)
839 bool cx = potential_constant_expression (elt_init);
840 if (BRACE_ENCLOSED_INITIALIZER_P (init))
841 cx &= potential_constant_expression (init);
842 VEC_INIT_EXPR_IS_CONSTEXPR (init) = cx;
844 VEC_INIT_EXPR_VALUE_INIT (init) = value_init;
846 return init;
849 /* Call build_vec_init to expand VEC_INIT into TARGET (for which NULL_TREE
850 means VEC_INIT_EXPR_SLOT). */
852 tree
853 expand_vec_init_expr (tree target, tree vec_init, tsubst_flags_t complain,
854 vec<tree,va_gc> **flags)
856 iloc_sentinel ils = EXPR_LOCATION (vec_init);
858 if (!target)
859 target = VEC_INIT_EXPR_SLOT (vec_init);
860 tree init = VEC_INIT_EXPR_INIT (vec_init);
861 int from_array = (init && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE);
862 return build_vec_init (target, NULL_TREE, init,
863 VEC_INIT_EXPR_VALUE_INIT (vec_init),
864 from_array, complain, flags);
867 /* Give a helpful diagnostic for a non-constexpr VEC_INIT_EXPR in a context
868 that requires a constant expression. */
870 void
871 diagnose_non_constexpr_vec_init (tree expr)
873 tree type = TREE_TYPE (VEC_INIT_EXPR_SLOT (expr));
874 tree init, elt_init;
875 if (VEC_INIT_EXPR_VALUE_INIT (expr))
876 init = void_type_node;
877 else
878 init = VEC_INIT_EXPR_INIT (expr);
880 elt_init = build_vec_init_elt (type, init, tf_warning_or_error);
881 require_potential_constant_expression (elt_init);
884 tree
885 build_array_copy (tree init)
887 return get_target_expr (build_vec_init_expr
888 (TREE_TYPE (init), init, tf_warning_or_error));
891 /* Build a TARGET_EXPR using INIT to initialize a new temporary of the
892 indicated TYPE. */
894 tree
895 build_target_expr_with_type (tree init, tree type, tsubst_flags_t complain)
897 gcc_assert (!VOID_TYPE_P (type));
898 gcc_assert (!VOID_TYPE_P (TREE_TYPE (init)));
900 if (TREE_CODE (init) == TARGET_EXPR
901 || init == error_mark_node)
902 return init;
903 else if (CLASS_TYPE_P (type) && type_has_nontrivial_copy_init (type)
904 && TREE_CODE (init) != COND_EXPR
905 && TREE_CODE (init) != CONSTRUCTOR
906 && TREE_CODE (init) != VA_ARG_EXPR
907 && TREE_CODE (init) != CALL_EXPR)
908 /* We need to build up a copy constructor call. COND_EXPR is a special
909 case because we already have copies on the arms and we don't want
910 another one here. A CONSTRUCTOR is aggregate initialization, which
911 is handled separately. A VA_ARG_EXPR is magic creation of an
912 aggregate; there's no additional work to be done. A CALL_EXPR
913 already creates a prvalue. */
914 return force_rvalue (init, complain);
916 return force_target_expr (type, init, complain);
919 /* Like the above function, but without the checking. This function should
920 only be used by code which is deliberately trying to subvert the type
921 system, such as call_builtin_trap. Or build_over_call, to avoid
922 infinite recursion. */
924 tree
925 force_target_expr (tree type, tree init, tsubst_flags_t complain)
927 tree slot;
929 gcc_assert (!VOID_TYPE_P (type));
931 slot = build_local_temp (type);
932 return build_target_expr (slot, init, complain);
935 /* Like build_target_expr_with_type, but use the type of INIT. */
937 tree
938 get_target_expr (tree init, tsubst_flags_t complain /* = tf_warning_or_error */)
940 if (TREE_CODE (init) == AGGR_INIT_EXPR)
941 return build_target_expr (AGGR_INIT_EXPR_SLOT (init), init, complain);
942 else if (TREE_CODE (init) == VEC_INIT_EXPR)
943 return build_target_expr (VEC_INIT_EXPR_SLOT (init), init, complain);
944 else
946 init = convert_bitfield_to_declared_type (init);
947 return build_target_expr_with_type (init, TREE_TYPE (init), complain);
951 /* If EXPR is a bitfield reference, convert it to the declared type of
952 the bitfield, and return the resulting expression. Otherwise,
953 return EXPR itself. */
955 tree
956 convert_bitfield_to_declared_type (tree expr)
958 tree bitfield_type;
960 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
961 if (bitfield_type)
962 expr = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type),
963 expr);
964 return expr;
967 /* EXPR is being used in an rvalue context. Return a version of EXPR
968 that is marked as an rvalue. */
970 tree
971 rvalue (tree expr)
973 tree type;
975 if (error_operand_p (expr))
976 return expr;
978 expr = mark_rvalue_use (expr);
980 /* [basic.lval]
982 Non-class rvalues always have cv-unqualified types. */
983 type = TREE_TYPE (expr);
984 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
985 type = cv_unqualified (type);
987 /* We need to do this for rvalue refs as well to get the right answer
988 from decltype; see c++/36628. */
989 if (!processing_template_decl && glvalue_p (expr))
991 /* But don't use this function for class lvalues; use move (to treat an
992 lvalue as an xvalue) or force_rvalue (to make a prvalue copy). */
993 gcc_checking_assert (!CLASS_TYPE_P (type));
994 expr = build1 (NON_LVALUE_EXPR, type, expr);
996 else if (type != TREE_TYPE (expr))
997 expr = build_nop (type, expr);
999 return expr;
1003 struct cplus_array_info
1005 tree type;
1006 tree domain;
1009 struct cplus_array_hasher : ggc_ptr_hash<tree_node>
1011 typedef cplus_array_info *compare_type;
1013 static hashval_t hash (tree t);
1014 static bool equal (tree, cplus_array_info *);
1017 /* Hash an ARRAY_TYPE. K is really of type `tree'. */
1019 hashval_t
1020 cplus_array_hasher::hash (tree t)
1022 hashval_t hash;
1024 hash = TYPE_UID (TREE_TYPE (t));
1025 if (TYPE_DOMAIN (t))
1026 hash ^= TYPE_UID (TYPE_DOMAIN (t));
1027 return hash;
1030 /* Compare two ARRAY_TYPEs. K1 is really of type `tree', K2 is really
1031 of type `cplus_array_info*'. */
1033 bool
1034 cplus_array_hasher::equal (tree t1, cplus_array_info *t2)
1036 return (TREE_TYPE (t1) == t2->type && TYPE_DOMAIN (t1) == t2->domain);
1039 /* Hash table containing dependent array types, which are unsuitable for
1040 the language-independent type hash table. */
1041 static GTY (()) hash_table<cplus_array_hasher> *cplus_array_htab;
1043 /* Build an ARRAY_TYPE without laying it out. */
1045 static tree
1046 build_min_array_type (tree elt_type, tree index_type)
1048 tree t = cxx_make_type (ARRAY_TYPE);
1049 TREE_TYPE (t) = elt_type;
1050 TYPE_DOMAIN (t) = index_type;
1051 return t;
1054 /* Set TYPE_CANONICAL like build_array_type_1, but using
1055 build_cplus_array_type. */
1057 static void
1058 set_array_type_canon (tree t, tree elt_type, tree index_type, bool dep)
1060 /* Set the canonical type for this new node. */
1061 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
1062 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
1063 SET_TYPE_STRUCTURAL_EQUALITY (t);
1064 else if (TYPE_CANONICAL (elt_type) != elt_type
1065 || (index_type && TYPE_CANONICAL (index_type) != index_type))
1066 TYPE_CANONICAL (t)
1067 = build_cplus_array_type (TYPE_CANONICAL (elt_type),
1068 index_type
1069 ? TYPE_CANONICAL (index_type) : index_type,
1070 dep);
1071 else
1072 TYPE_CANONICAL (t) = t;
1075 /* Like build_array_type, but handle special C++ semantics: an array of a
1076 variant element type is a variant of the array of the main variant of
1077 the element type. IS_DEPENDENT is -ve if we should determine the
1078 dependency. Otherwise its bool value indicates dependency. */
1080 tree
1081 build_cplus_array_type (tree elt_type, tree index_type, int dependent)
1083 tree t;
1085 if (elt_type == error_mark_node || index_type == error_mark_node)
1086 return error_mark_node;
1088 if (dependent < 0)
1089 dependent = (uses_template_parms (elt_type)
1090 || (index_type && uses_template_parms (index_type)));
1092 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
1093 /* Start with an array of the TYPE_MAIN_VARIANT. */
1094 t = build_cplus_array_type (TYPE_MAIN_VARIANT (elt_type),
1095 index_type, dependent);
1096 else if (dependent)
1098 /* Since type_hash_canon calls layout_type, we need to use our own
1099 hash table. */
1100 cplus_array_info cai;
1101 hashval_t hash;
1103 if (cplus_array_htab == NULL)
1104 cplus_array_htab = hash_table<cplus_array_hasher>::create_ggc (61);
1106 hash = TYPE_UID (elt_type);
1107 if (index_type)
1108 hash ^= TYPE_UID (index_type);
1109 cai.type = elt_type;
1110 cai.domain = index_type;
1112 tree *e = cplus_array_htab->find_slot_with_hash (&cai, hash, INSERT);
1113 if (*e)
1114 /* We have found the type: we're done. */
1115 return (tree) *e;
1116 else
1118 /* Build a new array type. */
1119 t = build_min_array_type (elt_type, index_type);
1121 /* Store it in the hash table. */
1122 *e = t;
1124 /* Set the canonical type for this new node. */
1125 set_array_type_canon (t, elt_type, index_type, dependent);
1127 /* Mark it as dependent now, this saves time later. */
1128 TYPE_DEPENDENT_P_VALID (t) = true;
1129 TYPE_DEPENDENT_P (t) = true;
1132 else
1134 bool typeless_storage = is_byte_access_type (elt_type);
1135 t = build_array_type (elt_type, index_type, typeless_storage);
1137 /* Mark as non-dependenty now, this will save time later. */
1138 TYPE_DEPENDENT_P_VALID (t) = true;
1141 /* Now check whether we already have this array variant. */
1142 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
1144 tree m = t;
1145 for (t = m; t; t = TYPE_NEXT_VARIANT (t))
1146 if (TREE_TYPE (t) == elt_type
1147 && TYPE_NAME (t) == NULL_TREE
1148 && TYPE_ATTRIBUTES (t) == NULL_TREE)
1149 break;
1150 if (!t)
1152 t = build_min_array_type (elt_type, index_type);
1153 /* Mark dependency now, this saves time later. */
1154 TYPE_DEPENDENT_P_VALID (t) = true;
1155 TYPE_DEPENDENT_P (t) = dependent;
1156 set_array_type_canon (t, elt_type, index_type, dependent);
1157 if (!dependent)
1159 layout_type (t);
1160 /* Make sure sizes are shared with the main variant.
1161 layout_type can't be called after setting TYPE_NEXT_VARIANT,
1162 as it will overwrite alignment etc. of all variants. */
1163 TYPE_SIZE (t) = TYPE_SIZE (m);
1164 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (m);
1165 TYPE_TYPELESS_STORAGE (t) = TYPE_TYPELESS_STORAGE (m);
1168 TYPE_MAIN_VARIANT (t) = m;
1169 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
1170 TYPE_NEXT_VARIANT (m) = t;
1174 /* Avoid spurious warnings with VLAs (c++/54583). */
1175 if (TYPE_SIZE (t) && EXPR_P (TYPE_SIZE (t)))
1176 suppress_warning (TYPE_SIZE (t), OPT_Wunused);
1178 /* Push these needs up to the ARRAY_TYPE so that initialization takes
1179 place more easily. */
1180 bool needs_ctor = (TYPE_NEEDS_CONSTRUCTING (t)
1181 = TYPE_NEEDS_CONSTRUCTING (elt_type));
1182 bool needs_dtor = (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1183 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type));
1185 if (!dependent && t == TYPE_MAIN_VARIANT (t)
1186 && !COMPLETE_TYPE_P (t) && COMPLETE_TYPE_P (elt_type))
1188 /* The element type has been completed since the last time we saw
1189 this array type; update the layout and 'tor flags for any variants
1190 that need it. */
1191 layout_type (t);
1192 for (tree v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1194 TYPE_NEEDS_CONSTRUCTING (v) = needs_ctor;
1195 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (v) = needs_dtor;
1199 return t;
1202 /* Return an ARRAY_TYPE with element type ELT and length N. */
1204 tree
1205 build_array_of_n_type (tree elt, int n)
1207 return build_cplus_array_type (elt, build_index_type (size_int (n - 1)));
1210 /* True iff T is an array of unknown bound. */
1212 bool
1213 array_of_unknown_bound_p (const_tree t)
1215 return (TREE_CODE (t) == ARRAY_TYPE
1216 && !TYPE_DOMAIN (t));
1219 /* True iff T is an N3639 array of runtime bound (VLA). These were approved
1220 for C++14 but then removed. This should only be used for N3639
1221 specifically; code wondering more generally if something is a VLA should use
1222 vla_type_p. */
1224 bool
1225 array_of_runtime_bound_p (tree t)
1227 if (!t || TREE_CODE (t) != ARRAY_TYPE)
1228 return false;
1229 if (variably_modified_type_p (TREE_TYPE (t), NULL_TREE))
1230 return false;
1231 tree dom = TYPE_DOMAIN (t);
1232 if (!dom)
1233 return false;
1234 tree max = TYPE_MAX_VALUE (dom);
1235 return (!potential_rvalue_constant_expression (max)
1236 || (!value_dependent_expression_p (max) && !TREE_CONSTANT (max)));
1239 /* True iff T is a variable length array. */
1241 bool
1242 vla_type_p (tree t)
1244 for (; t && TREE_CODE (t) == ARRAY_TYPE;
1245 t = TREE_TYPE (t))
1246 if (tree dom = TYPE_DOMAIN (t))
1248 tree max = TYPE_MAX_VALUE (dom);
1249 if (!potential_rvalue_constant_expression (max)
1250 || (!value_dependent_expression_p (max) && !TREE_CONSTANT (max)))
1251 return true;
1253 return false;
1257 /* Return a reference type node of MODE referring to TO_TYPE. If MODE
1258 is VOIDmode the standard pointer mode will be picked. If RVAL is
1259 true, return an rvalue reference type, otherwise return an lvalue
1260 reference type. If a type node exists, reuse it, otherwise create
1261 a new one. */
1262 tree
1263 cp_build_reference_type_for_mode (tree to_type, machine_mode mode, bool rval)
1265 tree lvalue_ref, t;
1267 if (to_type == error_mark_node)
1268 return error_mark_node;
1270 if (TYPE_REF_P (to_type))
1272 rval = rval && TYPE_REF_IS_RVALUE (to_type);
1273 to_type = TREE_TYPE (to_type);
1276 lvalue_ref = build_reference_type_for_mode (to_type, mode, false);
1278 if (!rval)
1279 return lvalue_ref;
1281 /* This code to create rvalue reference types is based on and tied
1282 to the code creating lvalue reference types in the middle-end
1283 functions build_reference_type_for_mode and build_reference_type.
1285 It works by putting the rvalue reference type nodes after the
1286 lvalue reference nodes in the TYPE_NEXT_REF_TO linked list, so
1287 they will effectively be ignored by the middle end. */
1289 for (t = lvalue_ref; (t = TYPE_NEXT_REF_TO (t)); )
1290 if (TYPE_REF_IS_RVALUE (t))
1291 return t;
1293 t = build_distinct_type_copy (lvalue_ref);
1295 TYPE_REF_IS_RVALUE (t) = true;
1296 TYPE_NEXT_REF_TO (t) = TYPE_NEXT_REF_TO (lvalue_ref);
1297 TYPE_NEXT_REF_TO (lvalue_ref) = t;
1299 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
1300 SET_TYPE_STRUCTURAL_EQUALITY (t);
1301 else if (TYPE_CANONICAL (to_type) != to_type)
1302 TYPE_CANONICAL (t)
1303 = cp_build_reference_type_for_mode (TYPE_CANONICAL (to_type), mode, rval);
1304 else
1305 TYPE_CANONICAL (t) = t;
1307 layout_type (t);
1309 return t;
1313 /* Return a reference type node referring to TO_TYPE. If RVAL is
1314 true, return an rvalue reference type, otherwise return an lvalue
1315 reference type. If a type node exists, reuse it, otherwise create
1316 a new one. */
1317 tree
1318 cp_build_reference_type (tree to_type, bool rval)
1320 return cp_build_reference_type_for_mode (to_type, VOIDmode, rval);
1323 /* Returns EXPR cast to rvalue reference type, like std::move. */
1325 tree
1326 move (tree expr)
1328 tree type = TREE_TYPE (expr);
1329 gcc_assert (!TYPE_REF_P (type));
1330 if (xvalue_p (expr))
1331 return expr;
1332 type = cp_build_reference_type (type, /*rval*/true);
1333 return build_static_cast (input_location, type, expr,
1334 tf_warning_or_error);
1337 /* Used by the C++ front end to build qualified array types. However,
1338 the C version of this function does not properly maintain canonical
1339 types (which are not used in C). */
1340 tree
1341 c_build_qualified_type (tree type, int type_quals, tree /* orig_qual_type */,
1342 size_t /* orig_qual_indirect */)
1344 return cp_build_qualified_type (type, type_quals);
1348 /* Make a variant of TYPE, qualified with the TYPE_QUALS. Handles
1349 arrays correctly. In particular, if TYPE is an array of T's, and
1350 TYPE_QUALS is non-empty, returns an array of qualified T's.
1352 FLAGS determines how to deal with ill-formed qualifications. If
1353 tf_ignore_bad_quals is set, then bad qualifications are dropped
1354 (this is permitted if TYPE was introduced via a typedef or template
1355 type parameter). If bad qualifications are dropped and tf_warning
1356 is set, then a warning is issued for non-const qualifications. If
1357 tf_ignore_bad_quals is not set and tf_error is not set, we
1358 return error_mark_node. Otherwise, we issue an error, and ignore
1359 the qualifications.
1361 Qualification of a reference type is valid when the reference came
1362 via a typedef or template type argument. [dcl.ref] No such
1363 dispensation is provided for qualifying a function type. [dcl.fct]
1364 DR 295 queries this and the proposed resolution brings it into line
1365 with qualifying a reference. We implement the DR. We also behave
1366 in a similar manner for restricting non-pointer types. */
1368 tree
1369 cp_build_qualified_type (tree type, int type_quals,
1370 tsubst_flags_t complain /* = tf_warning_or_error */)
1372 tree result;
1373 int bad_quals = TYPE_UNQUALIFIED;
1375 if (type == error_mark_node)
1376 return type;
1378 if (type_quals == cp_type_quals (type))
1379 return type;
1381 if (TREE_CODE (type) == ARRAY_TYPE)
1383 /* In C++, the qualification really applies to the array element
1384 type. Obtain the appropriately qualified element type. */
1385 tree t;
1386 tree element_type
1387 = cp_build_qualified_type (TREE_TYPE (type), type_quals, complain);
1389 if (element_type == error_mark_node)
1390 return error_mark_node;
1392 /* See if we already have an identically qualified type. Tests
1393 should be equivalent to those in check_qualified_type. */
1394 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1395 if (TREE_TYPE (t) == element_type
1396 && TYPE_NAME (t) == TYPE_NAME (type)
1397 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
1398 && attribute_list_equal (TYPE_ATTRIBUTES (t),
1399 TYPE_ATTRIBUTES (type)))
1400 break;
1402 if (!t)
1404 /* If we already know the dependentness, tell the array type
1405 constructor. This is important for module streaming, as we cannot
1406 dynamically determine that on read in. */
1407 t = build_cplus_array_type (element_type, TYPE_DOMAIN (type),
1408 TYPE_DEPENDENT_P_VALID (type)
1409 ? int (TYPE_DEPENDENT_P (type)) : -1);
1411 /* Keep the typedef name. */
1412 if (TYPE_NAME (t) != TYPE_NAME (type))
1414 t = build_variant_type_copy (t);
1415 TYPE_NAME (t) = TYPE_NAME (type);
1416 SET_TYPE_ALIGN (t, TYPE_ALIGN (type));
1417 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (type);
1421 /* Even if we already had this variant, we update
1422 TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case
1423 they changed since the variant was originally created.
1425 This seems hokey; if there is some way to use a previous
1426 variant *without* coming through here,
1427 TYPE_NEEDS_CONSTRUCTING will never be updated. */
1428 TYPE_NEEDS_CONSTRUCTING (t)
1429 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
1430 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1431 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
1432 return t;
1434 else if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
1436 tree t = PACK_EXPANSION_PATTERN (type);
1438 t = cp_build_qualified_type (t, type_quals, complain);
1439 return make_pack_expansion (t, complain);
1442 /* A reference or method type shall not be cv-qualified.
1443 [dcl.ref], [dcl.fct]. This used to be an error, but as of DR 295
1444 (in CD1) we always ignore extra cv-quals on functions. */
1446 /* [dcl.ref/1] Cv-qualified references are ill-formed except when
1447 the cv-qualifiers are introduced through the use of a typedef-name
1448 ([dcl.typedef], [temp.param]) or decltype-specifier
1449 ([dcl.type.decltype]),in which case the cv-qualifiers are
1450 ignored. */
1451 if (type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)
1452 && (TYPE_REF_P (type)
1453 || FUNC_OR_METHOD_TYPE_P (type)))
1455 if (TYPE_REF_P (type)
1456 && (!typedef_variant_p (type) || FUNC_OR_METHOD_TYPE_P (type)))
1457 bad_quals |= type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1458 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1461 /* But preserve any function-cv-quals on a FUNCTION_TYPE. */
1462 if (TREE_CODE (type) == FUNCTION_TYPE)
1463 type_quals |= type_memfn_quals (type);
1465 /* A restrict-qualified type must be a pointer (or reference)
1466 to object or incomplete type. */
1467 if ((type_quals & TYPE_QUAL_RESTRICT)
1468 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1469 && TREE_CODE (type) != TYPENAME_TYPE
1470 && !INDIRECT_TYPE_P (type))
1472 bad_quals |= TYPE_QUAL_RESTRICT;
1473 type_quals &= ~TYPE_QUAL_RESTRICT;
1476 if (bad_quals == TYPE_UNQUALIFIED
1477 || (complain & tf_ignore_bad_quals))
1478 /*OK*/;
1479 else if (!(complain & tf_error))
1480 return error_mark_node;
1481 else
1483 tree bad_type = build_qualified_type (ptr_type_node, bad_quals);
1484 error ("%qV qualifiers cannot be applied to %qT",
1485 bad_type, type);
1488 /* Retrieve (or create) the appropriately qualified variant. */
1489 result = build_qualified_type (type, type_quals);
1491 return result;
1494 /* Return TYPE with const and volatile removed. */
1496 tree
1497 cv_unqualified (tree type)
1499 int quals;
1501 if (type == error_mark_node)
1502 return type;
1504 quals = cp_type_quals (type);
1505 quals &= ~(TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE);
1506 return cp_build_qualified_type (type, quals);
1509 /* Subroutine of strip_typedefs. We want to apply to RESULT the attributes
1510 from ATTRIBS that affect type identity, and no others. If any are not
1511 applied, set *remove_attributes to true. */
1513 static tree
1514 apply_identity_attributes (tree result, tree attribs, bool *remove_attributes)
1516 tree first_ident = NULL_TREE;
1517 tree new_attribs = NULL_TREE;
1518 tree *p = &new_attribs;
1520 if (OVERLOAD_TYPE_P (result))
1522 /* On classes and enums all attributes are ingrained. */
1523 gcc_assert (attribs == TYPE_ATTRIBUTES (result));
1524 return result;
1527 for (tree a = attribs; a; a = TREE_CHAIN (a))
1529 const attribute_spec *as
1530 = lookup_attribute_spec (get_attribute_name (a));
1531 if (as && as->affects_type_identity)
1533 if (!first_ident)
1534 first_ident = a;
1535 else if (first_ident == error_mark_node)
1537 *p = tree_cons (TREE_PURPOSE (a), TREE_VALUE (a), NULL_TREE);
1538 p = &TREE_CHAIN (*p);
1541 else if (first_ident && first_ident != error_mark_node)
1543 for (tree a2 = first_ident; a2 != a; a2 = TREE_CHAIN (a2))
1545 *p = tree_cons (TREE_PURPOSE (a2), TREE_VALUE (a2), NULL_TREE);
1546 p = &TREE_CHAIN (*p);
1548 first_ident = error_mark_node;
1551 if (first_ident != error_mark_node)
1552 new_attribs = first_ident;
1554 if (first_ident == attribs)
1555 /* All attributes affected type identity. */;
1556 else
1557 *remove_attributes = true;
1559 return cp_build_type_attribute_variant (result, new_attribs);
1562 /* Builds a qualified variant of T that is either not a typedef variant
1563 (the default behavior) or not a typedef variant of a user-facing type
1564 (if FLAGS contains STF_USER_FACING).
1566 E.g. consider the following declarations:
1567 typedef const int ConstInt;
1568 typedef ConstInt* PtrConstInt;
1569 If T is PtrConstInt, this function returns a type representing
1570 const int*.
1571 In other words, if T is a typedef, the function returns the underlying type.
1572 The cv-qualification and attributes of the type returned match the
1573 input type.
1574 They will always be compatible types.
1575 The returned type is built so that all of its subtypes
1576 recursively have their typedefs stripped as well.
1578 This is different from just returning TYPE_CANONICAL (T)
1579 Because of several reasons:
1580 * If T is a type that needs structural equality
1581 its TYPE_CANONICAL (T) will be NULL.
1582 * TYPE_CANONICAL (T) desn't carry type attributes
1583 and loses template parameter names.
1585 If REMOVE_ATTRIBUTES is non-null, also strip attributes that don't
1586 affect type identity, and set the referent to true if any were
1587 stripped. */
1589 tree
1590 strip_typedefs (tree t, bool *remove_attributes /* = NULL */,
1591 unsigned int flags /* = 0 */)
1593 tree result = NULL, type = NULL, t0 = NULL;
1595 if (!t || t == error_mark_node)
1596 return t;
1598 if (TREE_CODE (t) == TREE_LIST)
1600 bool changed = false;
1601 releasing_vec vec;
1602 tree r = t;
1603 for (; t; t = TREE_CHAIN (t))
1605 gcc_assert (!TREE_PURPOSE (t));
1606 tree elt = strip_typedefs (TREE_VALUE (t), remove_attributes, flags);
1607 if (elt != TREE_VALUE (t))
1608 changed = true;
1609 vec_safe_push (vec, elt);
1611 if (changed)
1612 r = build_tree_list_vec (vec);
1613 return r;
1616 gcc_assert (TYPE_P (t));
1618 if (t == TYPE_CANONICAL (t))
1619 return t;
1621 if (!(flags & STF_STRIP_DEPENDENT)
1622 && dependent_alias_template_spec_p (t, nt_opaque))
1623 /* DR 1558: However, if the template-id is dependent, subsequent
1624 template argument substitution still applies to the template-id. */
1625 return t;
1627 switch (TREE_CODE (t))
1629 case POINTER_TYPE:
1630 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1631 result = build_pointer_type_for_mode (type, TYPE_MODE (t), false);
1632 break;
1633 case REFERENCE_TYPE:
1634 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1635 result = cp_build_reference_type_for_mode (type, TYPE_MODE (t), TYPE_REF_IS_RVALUE (t));
1636 break;
1637 case OFFSET_TYPE:
1638 t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t), remove_attributes, flags);
1639 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1640 result = build_offset_type (t0, type);
1641 break;
1642 case RECORD_TYPE:
1643 if (TYPE_PTRMEMFUNC_P (t))
1645 t0 = strip_typedefs (TYPE_PTRMEMFUNC_FN_TYPE (t),
1646 remove_attributes, flags);
1647 result = build_ptrmemfunc_type (t0);
1649 break;
1650 case ARRAY_TYPE:
1651 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1652 t0 = strip_typedefs (TYPE_DOMAIN (t), remove_attributes, flags);
1653 gcc_checking_assert (TYPE_DEPENDENT_P_VALID (t)
1654 || !dependent_type_p (t));
1655 result = build_cplus_array_type (type, t0, TYPE_DEPENDENT_P (t));
1656 break;
1657 case FUNCTION_TYPE:
1658 case METHOD_TYPE:
1660 tree arg_types = NULL, arg_node, arg_node2, arg_type;
1661 bool changed;
1663 /* Because we stomp on TREE_PURPOSE of TYPE_ARG_TYPES in many places
1664 around the compiler (e.g. cp_parser_late_parsing_default_args), we
1665 can't expect that re-hashing a function type will find a previous
1666 equivalent type, so try to reuse the input type if nothing has
1667 changed. If the type is itself a variant, that will change. */
1668 bool is_variant = typedef_variant_p (t);
1669 if (remove_attributes
1670 && (TYPE_ATTRIBUTES (t) || TYPE_USER_ALIGN (t)))
1671 is_variant = true;
1673 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1674 tree canon_spec = (flag_noexcept_type
1675 ? canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (t))
1676 : NULL_TREE);
1677 changed = (type != TREE_TYPE (t) || is_variant
1678 || TYPE_RAISES_EXCEPTIONS (t) != canon_spec);
1680 for (arg_node = TYPE_ARG_TYPES (t);
1681 arg_node;
1682 arg_node = TREE_CHAIN (arg_node))
1684 if (arg_node == void_list_node)
1685 break;
1686 arg_type = strip_typedefs (TREE_VALUE (arg_node),
1687 remove_attributes, flags);
1688 gcc_assert (arg_type);
1689 if (arg_type == TREE_VALUE (arg_node) && !changed)
1690 continue;
1692 if (!changed)
1694 changed = true;
1695 for (arg_node2 = TYPE_ARG_TYPES (t);
1696 arg_node2 != arg_node;
1697 arg_node2 = TREE_CHAIN (arg_node2))
1698 arg_types
1699 = tree_cons (TREE_PURPOSE (arg_node2),
1700 TREE_VALUE (arg_node2), arg_types);
1703 arg_types
1704 = tree_cons (TREE_PURPOSE (arg_node), arg_type, arg_types);
1707 if (!changed)
1708 return t;
1710 if (arg_types)
1711 arg_types = nreverse (arg_types);
1713 /* A list of parameters not ending with an ellipsis
1714 must end with void_list_node. */
1715 if (arg_node)
1716 arg_types = chainon (arg_types, void_list_node);
1718 if (TREE_CODE (t) == METHOD_TYPE)
1720 tree class_type = TREE_TYPE (TREE_VALUE (arg_types));
1721 gcc_assert (class_type);
1722 result =
1723 build_method_type_directly (class_type, type,
1724 TREE_CHAIN (arg_types));
1726 else
1728 result = build_function_type (type, arg_types);
1729 result = apply_memfn_quals (result, type_memfn_quals (t));
1732 result = build_cp_fntype_variant (result,
1733 type_memfn_rqual (t), canon_spec,
1734 TYPE_HAS_LATE_RETURN_TYPE (t));
1736 break;
1737 case TYPENAME_TYPE:
1739 bool changed = false;
1740 tree fullname = TYPENAME_TYPE_FULLNAME (t);
1741 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
1742 && TREE_OPERAND (fullname, 1))
1744 tree args = TREE_OPERAND (fullname, 1);
1745 tree new_args = copy_node (args);
1746 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
1748 tree arg = TREE_VEC_ELT (args, i);
1749 tree strip_arg;
1750 if (TYPE_P (arg))
1751 strip_arg = strip_typedefs (arg, remove_attributes, flags);
1752 else
1753 strip_arg = strip_typedefs_expr (arg, remove_attributes,
1754 flags);
1755 TREE_VEC_ELT (new_args, i) = strip_arg;
1756 if (strip_arg != arg)
1757 changed = true;
1759 if (changed)
1761 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_args)
1762 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
1763 fullname
1764 = lookup_template_function (TREE_OPERAND (fullname, 0),
1765 new_args);
1767 else
1768 ggc_free (new_args);
1770 tree ctx = strip_typedefs (TYPE_CONTEXT (t), remove_attributes, flags);
1771 if (!changed && ctx == TYPE_CONTEXT (t) && !typedef_variant_p (t))
1772 return t;
1773 tree name = fullname;
1774 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
1775 name = TREE_OPERAND (fullname, 0);
1776 /* Use build_typename_type rather than make_typename_type because we
1777 don't want to resolve it here, just strip typedefs. */
1778 result = build_typename_type (ctx, name, fullname, typename_type);
1780 break;
1781 case DECLTYPE_TYPE:
1782 result = strip_typedefs_expr (DECLTYPE_TYPE_EXPR (t),
1783 remove_attributes, flags);
1784 if (result == DECLTYPE_TYPE_EXPR (t))
1785 result = NULL_TREE;
1786 else
1787 result = (finish_decltype_type
1788 (result,
1789 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t),
1790 tf_none));
1791 break;
1792 case TRAIT_TYPE:
1794 tree type1 = strip_typedefs (TRAIT_TYPE_TYPE1 (t),
1795 remove_attributes, flags);
1796 tree type2 = strip_typedefs (TRAIT_TYPE_TYPE2 (t),
1797 remove_attributes, flags);
1798 if (type1 == TRAIT_TYPE_TYPE1 (t) && type2 == TRAIT_TYPE_TYPE2 (t))
1799 result = NULL_TREE;
1800 else
1801 result = finish_trait_type (TRAIT_TYPE_KIND (t), type1, type2);
1803 break;
1804 case TYPE_PACK_EXPANSION:
1806 tree pat = PACK_EXPANSION_PATTERN (t);
1807 if (TYPE_P (pat))
1809 type = strip_typedefs (pat, remove_attributes, flags);
1810 if (type != pat)
1812 result = build_distinct_type_copy (t);
1813 PACK_EXPANSION_PATTERN (result) = type;
1817 break;
1818 default:
1819 break;
1822 if (!result)
1824 if (typedef_variant_p (t))
1826 if ((flags & STF_USER_VISIBLE)
1827 && !user_facing_original_type_p (t))
1828 return t;
1829 /* If T is a non-template alias or typedef, we can assume that
1830 instantiating its definition will hit any substitution failure,
1831 so we don't need to retain it here as well. */
1832 if (!alias_template_specialization_p (t, nt_opaque))
1833 flags |= STF_STRIP_DEPENDENT;
1834 result = strip_typedefs (DECL_ORIGINAL_TYPE (TYPE_NAME (t)),
1835 remove_attributes, flags);
1837 else
1838 result = TYPE_MAIN_VARIANT (t);
1840 /*gcc_assert (!typedef_variant_p (result)
1841 || dependent_alias_template_spec_p (result, nt_opaque)
1842 || ((flags & STF_USER_VISIBLE)
1843 && !user_facing_original_type_p (result)));*/
1845 if (COMPLETE_TYPE_P (result) && !COMPLETE_TYPE_P (t))
1846 /* If RESULT is complete and T isn't, it's likely the case that T
1847 is a variant of RESULT which hasn't been updated yet. Skip the
1848 attribute handling. */;
1849 else
1851 if (TYPE_USER_ALIGN (t) != TYPE_USER_ALIGN (result)
1852 || TYPE_ALIGN (t) != TYPE_ALIGN (result))
1854 gcc_assert (TYPE_USER_ALIGN (t));
1855 if (remove_attributes)
1856 *remove_attributes = true;
1857 else
1859 if (TYPE_ALIGN (t) == TYPE_ALIGN (result))
1860 result = build_variant_type_copy (result);
1861 else
1862 result = build_aligned_type (result, TYPE_ALIGN (t));
1863 TYPE_USER_ALIGN (result) = true;
1867 if (TYPE_ATTRIBUTES (t))
1869 if (remove_attributes)
1870 result = apply_identity_attributes (result, TYPE_ATTRIBUTES (t),
1871 remove_attributes);
1872 else
1873 result = cp_build_type_attribute_variant (result,
1874 TYPE_ATTRIBUTES (t));
1878 return cp_build_qualified_type (result, cp_type_quals (t));
1881 /* Like strip_typedefs above, but works on expressions, so that in
1883 template<class T> struct A
1885 typedef T TT;
1886 B<sizeof(TT)> b;
1889 sizeof(TT) is replaced by sizeof(T). */
1891 tree
1892 strip_typedefs_expr (tree t, bool *remove_attributes, unsigned int flags)
1894 unsigned i,n;
1895 tree r, type, *ops;
1896 enum tree_code code;
1898 if (t == NULL_TREE || t == error_mark_node)
1899 return t;
1901 STRIP_ANY_LOCATION_WRAPPER (t);
1903 if (DECL_P (t) || CONSTANT_CLASS_P (t))
1904 return t;
1906 /* Some expressions have type operands, so let's handle types here rather
1907 than check TYPE_P in multiple places below. */
1908 if (TYPE_P (t))
1909 return strip_typedefs (t, remove_attributes, flags);
1911 code = TREE_CODE (t);
1912 switch (code)
1914 case IDENTIFIER_NODE:
1915 case TEMPLATE_PARM_INDEX:
1916 case OVERLOAD:
1917 case BASELINK:
1918 case ARGUMENT_PACK_SELECT:
1919 return t;
1921 case TRAIT_EXPR:
1923 tree type1 = strip_typedefs (TRAIT_EXPR_TYPE1 (t),
1924 remove_attributes, flags);
1925 tree type2 = strip_typedefs (TRAIT_EXPR_TYPE2 (t),
1926 remove_attributes, flags);
1927 if (type1 == TRAIT_EXPR_TYPE1 (t)
1928 && type2 == TRAIT_EXPR_TYPE2 (t))
1929 return t;
1930 r = copy_node (t);
1931 TRAIT_EXPR_TYPE1 (r) = type1;
1932 TRAIT_EXPR_TYPE2 (r) = type2;
1933 return r;
1936 case TREE_LIST:
1938 releasing_vec vec;
1939 bool changed = false;
1940 tree it;
1941 for (it = t; it; it = TREE_CHAIN (it))
1943 tree val = strip_typedefs_expr (TREE_VALUE (it),
1944 remove_attributes, flags);
1945 vec_safe_push (vec, val);
1946 if (val != TREE_VALUE (it))
1947 changed = true;
1948 gcc_assert (TREE_PURPOSE (it) == NULL_TREE);
1950 if (changed)
1952 r = NULL_TREE;
1953 FOR_EACH_VEC_ELT_REVERSE (*vec, i, it)
1954 r = tree_cons (NULL_TREE, it, r);
1956 else
1957 r = t;
1958 return r;
1961 case TREE_VEC:
1963 bool changed = false;
1964 releasing_vec vec;
1965 n = TREE_VEC_LENGTH (t);
1966 vec_safe_reserve (vec, n);
1967 for (i = 0; i < n; ++i)
1969 tree op = strip_typedefs_expr (TREE_VEC_ELT (t, i),
1970 remove_attributes, flags);
1971 vec->quick_push (op);
1972 if (op != TREE_VEC_ELT (t, i))
1973 changed = true;
1975 if (changed)
1977 r = copy_node (t);
1978 for (i = 0; i < n; ++i)
1979 TREE_VEC_ELT (r, i) = (*vec)[i];
1980 NON_DEFAULT_TEMPLATE_ARGS_COUNT (r)
1981 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
1983 else
1984 r = t;
1985 return r;
1988 case CONSTRUCTOR:
1990 bool changed = false;
1991 vec<constructor_elt, va_gc> *vec
1992 = vec_safe_copy (CONSTRUCTOR_ELTS (t));
1993 n = CONSTRUCTOR_NELTS (t);
1994 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1995 for (i = 0; i < n; ++i)
1997 constructor_elt *e = &(*vec)[i];
1998 tree op = strip_typedefs_expr (e->value, remove_attributes, flags);
1999 if (op != e->value)
2001 changed = true;
2002 e->value = op;
2004 gcc_checking_assert
2005 (e->index == strip_typedefs_expr (e->index, remove_attributes,
2006 flags));
2009 if (!changed && type == TREE_TYPE (t))
2011 vec_free (vec);
2012 return t;
2014 else
2016 r = copy_node (t);
2017 TREE_TYPE (r) = type;
2018 CONSTRUCTOR_ELTS (r) = vec;
2019 return r;
2023 case LAMBDA_EXPR:
2024 return t;
2026 case STATEMENT_LIST:
2027 error ("statement-expression in a constant expression");
2028 return error_mark_node;
2030 default:
2031 break;
2034 gcc_assert (EXPR_P (t));
2036 n = cp_tree_operand_length (t);
2037 ops = XALLOCAVEC (tree, n);
2038 type = TREE_TYPE (t);
2040 switch (code)
2042 CASE_CONVERT:
2043 case IMPLICIT_CONV_EXPR:
2044 case DYNAMIC_CAST_EXPR:
2045 case STATIC_CAST_EXPR:
2046 case CONST_CAST_EXPR:
2047 case REINTERPRET_CAST_EXPR:
2048 case CAST_EXPR:
2049 case NEW_EXPR:
2050 type = strip_typedefs (type, remove_attributes, flags);
2051 /* fallthrough */
2053 default:
2054 for (i = 0; i < n; ++i)
2055 ops[i] = strip_typedefs_expr (TREE_OPERAND (t, i),
2056 remove_attributes, flags);
2057 break;
2060 /* If nothing changed, return t. */
2061 for (i = 0; i < n; ++i)
2062 if (ops[i] != TREE_OPERAND (t, i))
2063 break;
2064 if (i == n && type == TREE_TYPE (t))
2065 return t;
2067 r = copy_node (t);
2068 TREE_TYPE (r) = type;
2069 for (i = 0; i < n; ++i)
2070 TREE_OPERAND (r, i) = ops[i];
2071 return r;
2074 /* Makes a copy of BINFO and TYPE, which is to be inherited into a
2075 graph dominated by T. If BINFO is NULL, TYPE is a dependent base,
2076 and we do a shallow copy. If BINFO is non-NULL, we do a deep copy.
2077 VIRT indicates whether TYPE is inherited virtually or not.
2078 IGO_PREV points at the previous binfo of the inheritance graph
2079 order chain. The newly copied binfo's TREE_CHAIN forms this
2080 ordering.
2082 The CLASSTYPE_VBASECLASSES vector of T is constructed in the
2083 correct order. That is in the order the bases themselves should be
2084 constructed in.
2086 The BINFO_INHERITANCE of a virtual base class points to the binfo
2087 of the most derived type. ??? We could probably change this so that
2088 BINFO_INHERITANCE becomes synonymous with BINFO_PRIMARY, and hence
2089 remove a field. They currently can only differ for primary virtual
2090 virtual bases. */
2092 tree
2093 copy_binfo (tree binfo, tree type, tree t, tree *igo_prev, int virt)
2095 tree new_binfo;
2097 if (virt)
2099 /* See if we've already made this virtual base. */
2100 new_binfo = binfo_for_vbase (type, t);
2101 if (new_binfo)
2102 return new_binfo;
2105 new_binfo = make_tree_binfo (binfo ? BINFO_N_BASE_BINFOS (binfo) : 0);
2106 BINFO_TYPE (new_binfo) = type;
2108 /* Chain it into the inheritance graph. */
2109 TREE_CHAIN (*igo_prev) = new_binfo;
2110 *igo_prev = new_binfo;
2112 if (binfo && !BINFO_DEPENDENT_BASE_P (binfo))
2114 int ix;
2115 tree base_binfo;
2117 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), type));
2119 BINFO_OFFSET (new_binfo) = BINFO_OFFSET (binfo);
2120 BINFO_VIRTUALS (new_binfo) = BINFO_VIRTUALS (binfo);
2122 /* We do not need to copy the accesses, as they are read only. */
2123 BINFO_BASE_ACCESSES (new_binfo) = BINFO_BASE_ACCESSES (binfo);
2125 /* Recursively copy base binfos of BINFO. */
2126 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2128 tree new_base_binfo;
2129 new_base_binfo = copy_binfo (base_binfo, BINFO_TYPE (base_binfo),
2130 t, igo_prev,
2131 BINFO_VIRTUAL_P (base_binfo));
2133 if (!BINFO_INHERITANCE_CHAIN (new_base_binfo))
2134 BINFO_INHERITANCE_CHAIN (new_base_binfo) = new_binfo;
2135 BINFO_BASE_APPEND (new_binfo, new_base_binfo);
2138 else
2139 BINFO_DEPENDENT_BASE_P (new_binfo) = 1;
2141 if (virt)
2143 /* Push it onto the list after any virtual bases it contains
2144 will have been pushed. */
2145 CLASSTYPE_VBASECLASSES (t)->quick_push (new_binfo);
2146 BINFO_VIRTUAL_P (new_binfo) = 1;
2147 BINFO_INHERITANCE_CHAIN (new_binfo) = TYPE_BINFO (t);
2150 return new_binfo;
2153 /* Hashing of lists so that we don't make duplicates.
2154 The entry point is `list_hash_canon'. */
2156 struct list_proxy
2158 tree purpose;
2159 tree value;
2160 tree chain;
2163 struct list_hasher : ggc_ptr_hash<tree_node>
2165 typedef list_proxy *compare_type;
2167 static hashval_t hash (tree);
2168 static bool equal (tree, list_proxy *);
2171 /* Now here is the hash table. When recording a list, it is added
2172 to the slot whose index is the hash code mod the table size.
2173 Note that the hash table is used for several kinds of lists.
2174 While all these live in the same table, they are completely independent,
2175 and the hash code is computed differently for each of these. */
2177 static GTY (()) hash_table<list_hasher> *list_hash_table;
2179 /* Compare ENTRY (an entry in the hash table) with DATA (a list_proxy
2180 for a node we are thinking about adding). */
2182 bool
2183 list_hasher::equal (tree t, list_proxy *proxy)
2185 return (TREE_VALUE (t) == proxy->value
2186 && TREE_PURPOSE (t) == proxy->purpose
2187 && TREE_CHAIN (t) == proxy->chain);
2190 /* Compute a hash code for a list (chain of TREE_LIST nodes
2191 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
2192 TREE_COMMON slots), by adding the hash codes of the individual entries. */
2194 static hashval_t
2195 list_hash_pieces (tree purpose, tree value, tree chain)
2197 hashval_t hashcode = 0;
2199 if (chain)
2200 hashcode += TREE_HASH (chain);
2202 if (value)
2203 hashcode += TREE_HASH (value);
2204 else
2205 hashcode += 1007;
2206 if (purpose)
2207 hashcode += TREE_HASH (purpose);
2208 else
2209 hashcode += 1009;
2210 return hashcode;
2213 /* Hash an already existing TREE_LIST. */
2215 hashval_t
2216 list_hasher::hash (tree t)
2218 return list_hash_pieces (TREE_PURPOSE (t),
2219 TREE_VALUE (t),
2220 TREE_CHAIN (t));
2223 /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
2224 object for an identical list if one already exists. Otherwise, build a
2225 new one, and record it as the canonical object. */
2227 tree
2228 hash_tree_cons (tree purpose, tree value, tree chain)
2230 int hashcode = 0;
2231 tree *slot;
2232 struct list_proxy proxy;
2234 /* Hash the list node. */
2235 hashcode = list_hash_pieces (purpose, value, chain);
2236 /* Create a proxy for the TREE_LIST we would like to create. We
2237 don't actually create it so as to avoid creating garbage. */
2238 proxy.purpose = purpose;
2239 proxy.value = value;
2240 proxy.chain = chain;
2241 /* See if it is already in the table. */
2242 slot = list_hash_table->find_slot_with_hash (&proxy, hashcode, INSERT);
2243 /* If not, create a new node. */
2244 if (!*slot)
2245 *slot = tree_cons (purpose, value, chain);
2246 return (tree) *slot;
2249 /* Constructor for hashed lists. */
2251 tree
2252 hash_tree_chain (tree value, tree chain)
2254 return hash_tree_cons (NULL_TREE, value, chain);
2257 void
2258 debug_binfo (tree elem)
2260 HOST_WIDE_INT n;
2261 tree virtuals;
2263 fprintf (stderr, "type \"%s\", offset = " HOST_WIDE_INT_PRINT_DEC
2264 "\nvtable type:\n",
2265 TYPE_NAME_STRING (BINFO_TYPE (elem)),
2266 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
2267 debug_tree (BINFO_TYPE (elem));
2268 if (BINFO_VTABLE (elem))
2269 fprintf (stderr, "vtable decl \"%s\"\n",
2270 IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem))));
2271 else
2272 fprintf (stderr, "no vtable decl yet\n");
2273 fprintf (stderr, "virtuals:\n");
2274 virtuals = BINFO_VIRTUALS (elem);
2275 n = 0;
2277 while (virtuals)
2279 tree fndecl = TREE_VALUE (virtuals);
2280 fprintf (stderr, "%s [%ld =? %ld]\n",
2281 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
2282 (long) n, (long) TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
2283 ++n;
2284 virtuals = TREE_CHAIN (virtuals);
2288 /* Build a representation for the qualified name SCOPE::NAME. TYPE is
2289 the type of the result expression, if known, or NULL_TREE if the
2290 resulting expression is type-dependent. If TEMPLATE_P is true,
2291 NAME is known to be a template because the user explicitly used the
2292 "template" keyword after the "::".
2294 All SCOPE_REFs should be built by use of this function. */
2296 tree
2297 build_qualified_name (tree type, tree scope, tree name, bool template_p)
2299 tree t;
2300 if (type == error_mark_node
2301 || scope == error_mark_node
2302 || name == error_mark_node)
2303 return error_mark_node;
2304 gcc_assert (TREE_CODE (name) != SCOPE_REF);
2305 t = build2 (SCOPE_REF, type, scope, name);
2306 QUALIFIED_NAME_IS_TEMPLATE (t) = template_p;
2307 PTRMEM_OK_P (t) = true;
2308 if (type)
2309 t = convert_from_reference (t);
2310 return t;
2313 /* Like check_qualified_type, but also check ref-qualifier, exception
2314 specification, and whether the return type was specified after the
2315 parameters. */
2317 static bool
2318 cp_check_qualified_type (const_tree cand, const_tree base, int type_quals,
2319 cp_ref_qualifier rqual, tree raises, bool late)
2321 return (TYPE_QUALS (cand) == type_quals
2322 && check_base_type (cand, base)
2323 && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (cand),
2324 ce_exact)
2325 && TYPE_HAS_LATE_RETURN_TYPE (cand) == late
2326 && type_memfn_rqual (cand) == rqual);
2329 /* Build the FUNCTION_TYPE or METHOD_TYPE with the ref-qualifier RQUAL. */
2331 tree
2332 build_ref_qualified_type (tree type, cp_ref_qualifier rqual)
2334 tree raises = TYPE_RAISES_EXCEPTIONS (type);
2335 bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
2336 return build_cp_fntype_variant (type, rqual, raises, late);
2339 tree
2340 make_binding_vec (tree name, unsigned clusters MEM_STAT_DECL)
2342 /* Stored in an unsigned short, but we're limited to the number of
2343 modules anyway. */
2344 gcc_checking_assert (clusters <= (unsigned short)(~0));
2345 size_t length = (offsetof (tree_binding_vec, vec)
2346 + clusters * sizeof (binding_cluster));
2347 tree vec = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2348 TREE_SET_CODE (vec, BINDING_VECTOR);
2349 BINDING_VECTOR_NAME (vec) = name;
2350 BINDING_VECTOR_ALLOC_CLUSTERS (vec) = clusters;
2351 BINDING_VECTOR_NUM_CLUSTERS (vec) = 0;
2353 return vec;
2356 /* Make a raw overload node containing FN. */
2358 tree
2359 ovl_make (tree fn, tree next)
2361 tree result = make_node (OVERLOAD);
2363 if (TREE_CODE (fn) == OVERLOAD)
2364 OVL_NESTED_P (result) = true;
2366 TREE_TYPE (result) = (next || TREE_CODE (fn) == TEMPLATE_DECL
2367 ? unknown_type_node : TREE_TYPE (fn));
2368 if (next && TREE_CODE (next) == OVERLOAD && OVL_DEDUP_P (next))
2369 OVL_DEDUP_P (result) = true;
2370 OVL_FUNCTION (result) = fn;
2371 OVL_CHAIN (result) = next;
2372 return result;
2375 /* Add FN to the (potentially NULL) overload set OVL. USING_OR_HIDDEN is >
2376 zero if this is a using-decl. It is > 1 if we're exporting the
2377 using decl. USING_OR_HIDDEN is < 0, if FN is hidden. (A decl
2378 cannot be both using and hidden.) We keep the hidden decls first,
2379 but remaining ones are unordered. */
2381 tree
2382 ovl_insert (tree fn, tree maybe_ovl, int using_or_hidden)
2384 tree result = maybe_ovl;
2385 tree insert_after = NULL_TREE;
2387 /* Skip hidden. */
2388 for (; maybe_ovl && TREE_CODE (maybe_ovl) == OVERLOAD
2389 && OVL_HIDDEN_P (maybe_ovl);
2390 maybe_ovl = OVL_CHAIN (maybe_ovl))
2392 gcc_checking_assert (!OVL_LOOKUP_P (maybe_ovl));
2393 insert_after = maybe_ovl;
2396 if (maybe_ovl || using_or_hidden || TREE_CODE (fn) == TEMPLATE_DECL)
2398 maybe_ovl = ovl_make (fn, maybe_ovl);
2400 if (using_or_hidden < 0)
2401 OVL_HIDDEN_P (maybe_ovl) = true;
2402 if (using_or_hidden > 0)
2404 OVL_DEDUP_P (maybe_ovl) = OVL_USING_P (maybe_ovl) = true;
2405 if (using_or_hidden > 1)
2406 OVL_EXPORT_P (maybe_ovl) = true;
2409 else
2410 maybe_ovl = fn;
2412 if (insert_after)
2414 OVL_CHAIN (insert_after) = maybe_ovl;
2415 TREE_TYPE (insert_after) = unknown_type_node;
2417 else
2418 result = maybe_ovl;
2420 return result;
2423 /* Skip any hidden names at the beginning of OVL. */
2425 tree
2426 ovl_skip_hidden (tree ovl)
2428 while (ovl && TREE_CODE (ovl) == OVERLOAD && OVL_HIDDEN_P (ovl))
2429 ovl = OVL_CHAIN (ovl);
2431 return ovl;
2434 /* NODE is an OVL_HIDDEN_P node that is now revealed. */
2436 tree
2437 ovl_iterator::reveal_node (tree overload, tree node)
2439 /* We cannot have returned NODE as part of a lookup overload, so we
2440 don't have to worry about preserving that. */
2442 OVL_HIDDEN_P (node) = false;
2443 if (tree chain = OVL_CHAIN (node))
2444 if (TREE_CODE (chain) == OVERLOAD)
2446 if (OVL_HIDDEN_P (chain))
2448 /* The node needs moving, and the simplest way is to remove it
2449 and reinsert. */
2450 overload = remove_node (overload, node);
2451 overload = ovl_insert (OVL_FUNCTION (node), overload);
2453 else if (OVL_DEDUP_P (chain))
2454 OVL_DEDUP_P (node) = true;
2456 return overload;
2459 /* NODE is on the overloads of OVL. Remove it.
2460 The removed node is unaltered and may continue to be iterated
2461 from (i.e. it is safe to remove a node from an overload one is
2462 currently iterating over). */
2464 tree
2465 ovl_iterator::remove_node (tree overload, tree node)
2467 tree *slot = &overload;
2468 while (*slot != node)
2470 tree probe = *slot;
2471 gcc_checking_assert (!OVL_LOOKUP_P (probe));
2473 slot = &OVL_CHAIN (probe);
2476 /* Stitch out NODE. We don't have to worry about now making a
2477 singleton overload (and consequently maybe setting its type),
2478 because all uses of this function will be followed by inserting a
2479 new node that must follow the place we've cut this out from. */
2480 if (TREE_CODE (node) != OVERLOAD)
2481 /* Cloned inherited ctors don't mark themselves as via_using. */
2482 *slot = NULL_TREE;
2483 else
2484 *slot = OVL_CHAIN (node);
2486 return overload;
2489 /* Mark or unmark a lookup set. */
2491 void
2492 lookup_mark (tree ovl, bool val)
2494 for (lkp_iterator iter (ovl); iter; ++iter)
2496 gcc_checking_assert (LOOKUP_SEEN_P (*iter) != val);
2497 LOOKUP_SEEN_P (*iter) = val;
2501 /* Add a set of new FNS into a lookup. */
2503 tree
2504 lookup_add (tree fns, tree lookup)
2506 if (fns == error_mark_node || lookup == error_mark_node)
2507 return error_mark_node;
2509 if (lookup || TREE_CODE (fns) == TEMPLATE_DECL)
2511 lookup = ovl_make (fns, lookup);
2512 OVL_LOOKUP_P (lookup) = true;
2514 else
2515 lookup = fns;
2517 return lookup;
2520 /* FNS is a new overload set, add them to LOOKUP, if they are not
2521 already present there. */
2523 tree
2524 lookup_maybe_add (tree fns, tree lookup, bool deduping)
2526 if (deduping)
2527 for (tree next, probe = fns; probe; probe = next)
2529 tree fn = probe;
2530 next = NULL_TREE;
2532 if (TREE_CODE (probe) == OVERLOAD)
2534 fn = OVL_FUNCTION (probe);
2535 next = OVL_CHAIN (probe);
2538 if (!LOOKUP_SEEN_P (fn))
2539 LOOKUP_SEEN_P (fn) = true;
2540 else
2542 /* This function was already seen. Insert all the
2543 predecessors onto the lookup. */
2544 for (; fns != probe; fns = OVL_CHAIN (fns))
2546 lookup = lookup_add (OVL_FUNCTION (fns), lookup);
2547 /* Propagate OVL_USING, but OVL_HIDDEN &
2548 OVL_DEDUP_P don't matter. */
2549 if (OVL_USING_P (fns))
2550 OVL_USING_P (lookup) = true;
2553 /* And now skip this function. */
2554 fns = next;
2558 if (fns)
2559 /* We ended in a set of new functions. Add them all in one go. */
2560 lookup = lookup_add (fns, lookup);
2562 return lookup;
2565 /* Returns nonzero if X is an expression for a (possibly overloaded)
2566 function. If "f" is a function or function template, "f", "c->f",
2567 "c.f", "C::f", and "f<int>" will all be considered possibly
2568 overloaded functions. Returns 2 if the function is actually
2569 overloaded, i.e., if it is impossible to know the type of the
2570 function without performing overload resolution. */
2573 is_overloaded_fn (tree x)
2575 STRIP_ANY_LOCATION_WRAPPER (x);
2577 /* A baselink is also considered an overloaded function. */
2578 if (TREE_CODE (x) == OFFSET_REF
2579 || TREE_CODE (x) == COMPONENT_REF)
2580 x = TREE_OPERAND (x, 1);
2581 x = MAYBE_BASELINK_FUNCTIONS (x);
2582 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
2583 x = TREE_OPERAND (x, 0);
2585 if (DECL_FUNCTION_TEMPLATE_P (OVL_FIRST (x))
2586 || (TREE_CODE (x) == OVERLOAD && !OVL_SINGLE_P (x)))
2587 return 2;
2589 return OVL_P (x);
2592 /* X is the CALL_EXPR_FN of a CALL_EXPR. If X represents a dependent name
2593 (14.6.2), return the IDENTIFIER_NODE for that name. Otherwise, return
2594 NULL_TREE. */
2596 tree
2597 dependent_name (tree x)
2599 /* FIXME a dependent name must be unqualified, but this function doesn't
2600 distinguish between qualified and unqualified identifiers. */
2601 if (identifier_p (x))
2602 return x;
2603 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
2604 x = TREE_OPERAND (x, 0);
2605 if (OVL_P (x))
2606 return OVL_NAME (x);
2607 return NULL_TREE;
2610 /* Returns true iff X is an expression for an overloaded function
2611 whose type cannot be known without performing overload
2612 resolution. */
2614 bool
2615 really_overloaded_fn (tree x)
2617 return is_overloaded_fn (x) == 2;
2620 /* Get the overload set FROM refers to. Returns NULL if it's not an
2621 overload set. */
2623 tree
2624 maybe_get_fns (tree from)
2626 STRIP_ANY_LOCATION_WRAPPER (from);
2628 /* A baselink is also considered an overloaded function. */
2629 if (TREE_CODE (from) == OFFSET_REF
2630 || TREE_CODE (from) == COMPONENT_REF)
2631 from = TREE_OPERAND (from, 1);
2632 if (BASELINK_P (from))
2633 from = BASELINK_FUNCTIONS (from);
2634 if (TREE_CODE (from) == TEMPLATE_ID_EXPR)
2635 from = TREE_OPERAND (from, 0);
2637 if (OVL_P (from))
2638 return from;
2640 return NULL;
2643 /* FROM refers to an overload set. Return that set (or die). */
2645 tree
2646 get_fns (tree from)
2648 tree res = maybe_get_fns (from);
2650 gcc_assert (res);
2651 return res;
2654 /* Return the first function of the overload set FROM refers to. */
2656 tree
2657 get_first_fn (tree from)
2659 return OVL_FIRST (get_fns (from));
2662 /* Return the scope where the overloaded functions OVL were found. */
2664 tree
2665 ovl_scope (tree ovl)
2667 if (TREE_CODE (ovl) == OFFSET_REF
2668 || TREE_CODE (ovl) == COMPONENT_REF)
2669 ovl = TREE_OPERAND (ovl, 1);
2670 if (TREE_CODE (ovl) == BASELINK)
2671 return BINFO_TYPE (BASELINK_BINFO (ovl));
2672 if (TREE_CODE (ovl) == TEMPLATE_ID_EXPR)
2673 ovl = TREE_OPERAND (ovl, 0);
2674 /* Skip using-declarations. */
2675 lkp_iterator iter (ovl);
2677 ovl = *iter;
2678 while (iter.using_p () && ++iter);
2680 return CP_DECL_CONTEXT (ovl);
2683 #define PRINT_RING_SIZE 4
2685 static const char *
2686 cxx_printable_name_internal (tree decl, int v, bool translate)
2688 static unsigned int uid_ring[PRINT_RING_SIZE];
2689 static char *print_ring[PRINT_RING_SIZE];
2690 static bool trans_ring[PRINT_RING_SIZE];
2691 static int ring_counter;
2692 int i;
2694 /* Only cache functions. */
2695 if (v < 2
2696 || TREE_CODE (decl) != FUNCTION_DECL
2697 || DECL_LANG_SPECIFIC (decl) == 0)
2698 return lang_decl_name (decl, v, translate);
2700 /* See if this print name is lying around. */
2701 for (i = 0; i < PRINT_RING_SIZE; i++)
2702 if (uid_ring[i] == DECL_UID (decl) && translate == trans_ring[i])
2703 /* yes, so return it. */
2704 return print_ring[i];
2706 if (++ring_counter == PRINT_RING_SIZE)
2707 ring_counter = 0;
2709 if (current_function_decl != NULL_TREE)
2711 /* There may be both translated and untranslated versions of the
2712 name cached. */
2713 for (i = 0; i < 2; i++)
2715 if (uid_ring[ring_counter] == DECL_UID (current_function_decl))
2716 ring_counter += 1;
2717 if (ring_counter == PRINT_RING_SIZE)
2718 ring_counter = 0;
2720 gcc_assert (uid_ring[ring_counter] != DECL_UID (current_function_decl));
2723 free (print_ring[ring_counter]);
2725 print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v, translate));
2726 uid_ring[ring_counter] = DECL_UID (decl);
2727 trans_ring[ring_counter] = translate;
2728 return print_ring[ring_counter];
2731 const char *
2732 cxx_printable_name (tree decl, int v)
2734 return cxx_printable_name_internal (decl, v, false);
2737 const char *
2738 cxx_printable_name_translate (tree decl, int v)
2740 return cxx_printable_name_internal (decl, v, true);
2743 /* Return the canonical version of exception-specification RAISES for a C++17
2744 function type, for use in type comparison and building TYPE_CANONICAL. */
2746 tree
2747 canonical_eh_spec (tree raises)
2749 if (raises == NULL_TREE)
2750 return raises;
2751 else if (DEFERRED_NOEXCEPT_SPEC_P (raises)
2752 || UNPARSED_NOEXCEPT_SPEC_P (raises)
2753 || uses_template_parms (raises)
2754 || uses_template_parms (TREE_PURPOSE (raises)))
2755 /* Keep a dependent or deferred exception specification. */
2756 return raises;
2757 else if (nothrow_spec_p (raises))
2758 /* throw() -> noexcept. */
2759 return noexcept_true_spec;
2760 else
2761 /* For C++17 type matching, anything else -> nothing. */
2762 return NULL_TREE;
2765 tree
2766 build_cp_fntype_variant (tree type, cp_ref_qualifier rqual,
2767 tree raises, bool late)
2769 cp_cv_quals type_quals = TYPE_QUALS (type);
2771 if (cp_check_qualified_type (type, type, type_quals, rqual, raises, late))
2772 return type;
2774 tree v = TYPE_MAIN_VARIANT (type);
2775 for (; v; v = TYPE_NEXT_VARIANT (v))
2776 if (cp_check_qualified_type (v, type, type_quals, rqual, raises, late))
2777 return v;
2779 /* Need to build a new variant. */
2780 v = build_variant_type_copy (type);
2781 if (!TYPE_DEPENDENT_P (v))
2782 /* We no longer know that it's not type-dependent. */
2783 TYPE_DEPENDENT_P_VALID (v) = false;
2784 TYPE_RAISES_EXCEPTIONS (v) = raises;
2785 TYPE_HAS_LATE_RETURN_TYPE (v) = late;
2786 switch (rqual)
2788 case REF_QUAL_RVALUE:
2789 FUNCTION_RVALUE_QUALIFIED (v) = 1;
2790 FUNCTION_REF_QUALIFIED (v) = 1;
2791 break;
2792 case REF_QUAL_LVALUE:
2793 FUNCTION_RVALUE_QUALIFIED (v) = 0;
2794 FUNCTION_REF_QUALIFIED (v) = 1;
2795 break;
2796 default:
2797 FUNCTION_REF_QUALIFIED (v) = 0;
2798 break;
2801 /* Canonicalize the exception specification. */
2802 tree cr = flag_noexcept_type ? canonical_eh_spec (raises) : NULL_TREE;
2804 if (TYPE_STRUCTURAL_EQUALITY_P (type))
2805 /* Propagate structural equality. */
2806 SET_TYPE_STRUCTURAL_EQUALITY (v);
2807 else if (TYPE_CANONICAL (type) != type || cr != raises || late)
2808 /* Build the underlying canonical type, since it is different
2809 from TYPE. */
2810 TYPE_CANONICAL (v) = build_cp_fntype_variant (TYPE_CANONICAL (type),
2811 rqual, cr, false);
2812 else
2813 /* T is its own canonical type. */
2814 TYPE_CANONICAL (v) = v;
2816 return v;
2819 /* TYPE is a function or method type with a deferred exception
2820 specification that has been parsed to RAISES. Fixup all the type
2821 variants that are affected in place. Via decltype &| noexcept
2822 tricks, the unparsed spec could have escaped into the type system.
2823 The general case is hard to fixup canonical types for. */
2825 void
2826 fixup_deferred_exception_variants (tree type, tree raises)
2828 tree original = TYPE_RAISES_EXCEPTIONS (type);
2829 tree cr = flag_noexcept_type ? canonical_eh_spec (raises) : NULL_TREE;
2831 gcc_checking_assert (UNPARSED_NOEXCEPT_SPEC_P (original));
2833 /* Though sucky, this walk will process the canonical variants
2834 first. */
2835 tree prev = NULL_TREE;
2836 for (tree variant = TYPE_MAIN_VARIANT (type);
2837 variant; prev = variant, variant = TYPE_NEXT_VARIANT (variant))
2838 if (TYPE_RAISES_EXCEPTIONS (variant) == original)
2840 gcc_checking_assert (variant != TYPE_MAIN_VARIANT (type));
2842 if (!TYPE_STRUCTURAL_EQUALITY_P (variant))
2844 cp_cv_quals var_quals = TYPE_QUALS (variant);
2845 cp_ref_qualifier rqual = type_memfn_rqual (variant);
2847 /* If VARIANT would become a dup (cp_check_qualified_type-wise)
2848 of an existing variant in the variant list of TYPE after its
2849 exception specification has been parsed, elide it. Otherwise,
2850 build_cp_fntype_variant could use it, leading to "canonical
2851 types differ for identical types." */
2852 tree v = TYPE_MAIN_VARIANT (type);
2853 for (; v; v = TYPE_NEXT_VARIANT (v))
2854 if (cp_check_qualified_type (v, variant, var_quals,
2855 rqual, cr, false))
2857 /* The main variant will not match V, so PREV will never
2858 be null. */
2859 TYPE_NEXT_VARIANT (prev) = TYPE_NEXT_VARIANT (variant);
2860 break;
2862 TYPE_RAISES_EXCEPTIONS (variant) = raises;
2864 if (!v)
2865 v = build_cp_fntype_variant (TYPE_CANONICAL (variant),
2866 rqual, cr, false);
2867 TYPE_CANONICAL (variant) = TYPE_CANONICAL (v);
2869 else
2870 TYPE_RAISES_EXCEPTIONS (variant) = raises;
2872 if (!TYPE_DEPENDENT_P (variant))
2873 /* We no longer know that it's not type-dependent. */
2874 TYPE_DEPENDENT_P_VALID (variant) = false;
2878 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
2879 listed in RAISES. */
2881 tree
2882 build_exception_variant (tree type, tree raises)
2884 cp_ref_qualifier rqual = type_memfn_rqual (type);
2885 bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
2886 return build_cp_fntype_variant (type, rqual, raises, late);
2889 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new
2890 BOUND_TEMPLATE_TEMPLATE_PARM bound with NEWARGS as its template
2891 arguments. */
2893 tree
2894 bind_template_template_parm (tree t, tree newargs)
2896 tree decl = TYPE_NAME (t);
2897 tree t2;
2899 t2 = cxx_make_type (BOUND_TEMPLATE_TEMPLATE_PARM);
2900 decl = build_decl (input_location,
2901 TYPE_DECL, DECL_NAME (decl), NULL_TREE);
2902 SET_DECL_TEMPLATE_PARM_P (decl);
2904 /* These nodes have to be created to reflect new TYPE_DECL and template
2905 arguments. */
2906 TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t));
2907 TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl;
2908 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
2909 = build_template_info (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t), newargs);
2911 TREE_TYPE (decl) = t2;
2912 TYPE_NAME (t2) = decl;
2913 TYPE_STUB_DECL (t2) = decl;
2914 TYPE_SIZE (t2) = 0;
2916 if (any_template_arguments_need_structural_equality_p (newargs))
2917 SET_TYPE_STRUCTURAL_EQUALITY (t2);
2918 else
2919 TYPE_CANONICAL (t2) = canonical_type_parameter (t2);
2921 return t2;
2924 /* Called from count_trees via walk_tree. */
2926 static tree
2927 count_trees_r (tree *tp, int *walk_subtrees, void *data)
2929 ++*((int *) data);
2931 if (TYPE_P (*tp))
2932 *walk_subtrees = 0;
2934 return NULL_TREE;
2937 /* Debugging function for measuring the rough complexity of a tree
2938 representation. */
2941 count_trees (tree t)
2943 int n_trees = 0;
2944 cp_walk_tree_without_duplicates (&t, count_trees_r, &n_trees);
2945 return n_trees;
2948 /* Called from verify_stmt_tree via walk_tree. */
2950 static tree
2951 verify_stmt_tree_r (tree* tp, int * /*walk_subtrees*/, void* data)
2953 tree t = *tp;
2954 hash_table<nofree_ptr_hash <tree_node> > *statements
2955 = static_cast <hash_table<nofree_ptr_hash <tree_node> > *> (data);
2956 tree_node **slot;
2958 if (!STATEMENT_CODE_P (TREE_CODE (t)))
2959 return NULL_TREE;
2961 /* If this statement is already present in the hash table, then
2962 there is a circularity in the statement tree. */
2963 gcc_assert (!statements->find (t));
2965 slot = statements->find_slot (t, INSERT);
2966 *slot = t;
2968 return NULL_TREE;
2971 /* Debugging function to check that the statement T has not been
2972 corrupted. For now, this function simply checks that T contains no
2973 circularities. */
2975 void
2976 verify_stmt_tree (tree t)
2978 hash_table<nofree_ptr_hash <tree_node> > statements (37);
2979 cp_walk_tree (&t, verify_stmt_tree_r, &statements, NULL);
2982 /* Check if the type T depends on a type with no linkage and if so,
2983 return it. If RELAXED_P then do not consider a class type declared
2984 within a vague-linkage function to have no linkage. Remember:
2985 no-linkage is not the same as internal-linkage. */
2987 tree
2988 no_linkage_check (tree t, bool relaxed_p)
2990 tree r;
2992 /* Lambda types that don't have mangling scope have no linkage. We
2993 check CLASSTYPE_LAMBDA_EXPR for error_mark_node because
2994 when we get here from pushtag none of the lambda information is
2995 set up yet, so we want to assume that the lambda has linkage and
2996 fix it up later if not. We need to check this even in templates so
2997 that we properly handle a lambda-expression in the signature. */
2998 if (LAMBDA_TYPE_P (t)
2999 && CLASSTYPE_LAMBDA_EXPR (t) != error_mark_node)
3001 tree extra = LAMBDA_TYPE_EXTRA_SCOPE (t);
3002 if (!extra)
3003 return t;
3006 /* Otherwise there's no point in checking linkage on template functions; we
3007 can't know their complete types. */
3008 if (processing_template_decl)
3009 return NULL_TREE;
3011 switch (TREE_CODE (t))
3013 case RECORD_TYPE:
3014 if (TYPE_PTRMEMFUNC_P (t))
3015 goto ptrmem;
3016 /* Fall through. */
3017 case UNION_TYPE:
3018 if (!CLASS_TYPE_P (t))
3019 return NULL_TREE;
3020 /* Fall through. */
3021 case ENUMERAL_TYPE:
3022 /* Only treat unnamed types as having no linkage if they're at
3023 namespace scope. This is core issue 966. */
3024 if (TYPE_UNNAMED_P (t) && TYPE_NAMESPACE_SCOPE_P (t))
3025 return t;
3027 for (r = CP_TYPE_CONTEXT (t); ; )
3029 /* If we're a nested type of a !TREE_PUBLIC class, we might not
3030 have linkage, or we might just be in an anonymous namespace.
3031 If we're in a TREE_PUBLIC class, we have linkage. */
3032 if (TYPE_P (r) && !TREE_PUBLIC (TYPE_NAME (r)))
3033 return no_linkage_check (TYPE_CONTEXT (t), relaxed_p);
3034 else if (TREE_CODE (r) == FUNCTION_DECL)
3036 if (!relaxed_p || !vague_linkage_p (r))
3037 return t;
3038 else
3039 r = CP_DECL_CONTEXT (r);
3041 else
3042 break;
3045 return NULL_TREE;
3047 case ARRAY_TYPE:
3048 case POINTER_TYPE:
3049 case REFERENCE_TYPE:
3050 case VECTOR_TYPE:
3051 return no_linkage_check (TREE_TYPE (t), relaxed_p);
3053 case OFFSET_TYPE:
3054 ptrmem:
3055 r = no_linkage_check (TYPE_PTRMEM_POINTED_TO_TYPE (t),
3056 relaxed_p);
3057 if (r)
3058 return r;
3059 return no_linkage_check (TYPE_PTRMEM_CLASS_TYPE (t), relaxed_p);
3061 case METHOD_TYPE:
3062 case FUNCTION_TYPE:
3064 tree parm = TYPE_ARG_TYPES (t);
3065 if (TREE_CODE (t) == METHOD_TYPE)
3066 /* The 'this' pointer isn't interesting; a method has the same
3067 linkage (or lack thereof) as its enclosing class. */
3068 parm = TREE_CHAIN (parm);
3069 for (;
3070 parm && parm != void_list_node;
3071 parm = TREE_CHAIN (parm))
3073 r = no_linkage_check (TREE_VALUE (parm), relaxed_p);
3074 if (r)
3075 return r;
3077 return no_linkage_check (TREE_TYPE (t), relaxed_p);
3080 default:
3081 return NULL_TREE;
3085 extern int depth_reached;
3087 void
3088 cxx_print_statistics (void)
3090 print_template_statistics ();
3091 if (GATHER_STATISTICS)
3092 fprintf (stderr, "maximum template instantiation depth reached: %d\n",
3093 depth_reached);
3096 /* Return, as an INTEGER_CST node, the number of elements for TYPE
3097 (which is an ARRAY_TYPE). This counts only elements of the top
3098 array. */
3100 tree
3101 array_type_nelts_top (tree type)
3103 return fold_build2_loc (input_location,
3104 PLUS_EXPR, sizetype,
3105 array_type_nelts (type),
3106 size_one_node);
3109 /* Return, as an INTEGER_CST node, the number of elements for TYPE
3110 (which is an ARRAY_TYPE). This one is a recursive count of all
3111 ARRAY_TYPEs that are clumped together. */
3113 tree
3114 array_type_nelts_total (tree type)
3116 tree sz = array_type_nelts_top (type);
3117 type = TREE_TYPE (type);
3118 while (TREE_CODE (type) == ARRAY_TYPE)
3120 tree n = array_type_nelts_top (type);
3121 sz = fold_build2_loc (input_location,
3122 MULT_EXPR, sizetype, sz, n);
3123 type = TREE_TYPE (type);
3125 return sz;
3128 /* Return true if FNDECL is std::source_location::current () method. */
3130 bool
3131 source_location_current_p (tree fndecl)
3133 gcc_checking_assert (TREE_CODE (fndecl) == FUNCTION_DECL
3134 && DECL_IMMEDIATE_FUNCTION_P (fndecl));
3135 if (DECL_NAME (fndecl) == NULL_TREE
3136 || TREE_CODE (TREE_TYPE (fndecl)) != FUNCTION_TYPE
3137 || TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != RECORD_TYPE
3138 || DECL_CONTEXT (fndecl) != TREE_TYPE (TREE_TYPE (fndecl))
3139 || !id_equal (DECL_NAME (fndecl), "current"))
3140 return false;
3142 tree source_location = DECL_CONTEXT (fndecl);
3143 if (TYPE_NAME (source_location) == NULL_TREE
3144 || TREE_CODE (TYPE_NAME (source_location)) != TYPE_DECL
3145 || TYPE_IDENTIFIER (source_location) == NULL_TREE
3146 || !id_equal (TYPE_IDENTIFIER (source_location),
3147 "source_location")
3148 || !decl_in_std_namespace_p (TYPE_NAME (source_location)))
3149 return false;
3151 return true;
3154 struct bot_data
3156 splay_tree target_remap;
3157 bool clear_location;
3160 /* Called from break_out_target_exprs via mapcar. */
3162 static tree
3163 bot_manip (tree* tp, int* walk_subtrees, void* data_)
3165 bot_data &data = *(bot_data*)data_;
3166 splay_tree target_remap = data.target_remap;
3167 tree t = *tp;
3169 if (!TYPE_P (t) && TREE_CONSTANT (t) && !TREE_SIDE_EFFECTS (t))
3171 /* There can't be any TARGET_EXPRs or their slot variables below this
3172 point. But we must make a copy, in case subsequent processing
3173 alters any part of it. For example, during gimplification a cast
3174 of the form (T) &X::f (where "f" is a member function) will lead
3175 to replacing the PTRMEM_CST for &X::f with a VAR_DECL. */
3176 *walk_subtrees = 0;
3177 *tp = unshare_expr (t);
3178 return NULL_TREE;
3180 if (TREE_CODE (t) == TARGET_EXPR)
3182 tree u;
3184 if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
3186 u = build_cplus_new (TREE_TYPE (t), TREE_OPERAND (t, 1),
3187 tf_warning_or_error);
3188 if (u == error_mark_node)
3189 return u;
3190 if (AGGR_INIT_ZERO_FIRST (TREE_OPERAND (t, 1)))
3191 AGGR_INIT_ZERO_FIRST (TREE_OPERAND (u, 1)) = true;
3193 else
3194 u = force_target_expr (TREE_TYPE (t), TREE_OPERAND (t, 1),
3195 tf_warning_or_error);
3197 TARGET_EXPR_IMPLICIT_P (u) = TARGET_EXPR_IMPLICIT_P (t);
3198 TARGET_EXPR_LIST_INIT_P (u) = TARGET_EXPR_LIST_INIT_P (t);
3199 TARGET_EXPR_DIRECT_INIT_P (u) = TARGET_EXPR_DIRECT_INIT_P (t);
3200 TARGET_EXPR_ELIDING_P (u) = TARGET_EXPR_ELIDING_P (t);
3202 /* Map the old variable to the new one. */
3203 splay_tree_insert (target_remap,
3204 (splay_tree_key) TREE_OPERAND (t, 0),
3205 (splay_tree_value) TREE_OPERAND (u, 0));
3207 TREE_OPERAND (u, 1) = break_out_target_exprs (TREE_OPERAND (u, 1),
3208 data.clear_location);
3209 if (TREE_OPERAND (u, 1) == error_mark_node)
3210 return error_mark_node;
3212 /* Replace the old expression with the new version. */
3213 *tp = u;
3214 /* We don't have to go below this point; the recursive call to
3215 break_out_target_exprs will have handled anything below this
3216 point. */
3217 *walk_subtrees = 0;
3218 return NULL_TREE;
3220 if (TREE_CODE (*tp) == SAVE_EXPR)
3222 t = *tp;
3223 splay_tree_node n = splay_tree_lookup (target_remap,
3224 (splay_tree_key) t);
3225 if (n)
3227 *tp = (tree)n->value;
3228 *walk_subtrees = 0;
3230 else
3232 copy_tree_r (tp, walk_subtrees, NULL);
3233 splay_tree_insert (target_remap,
3234 (splay_tree_key)t,
3235 (splay_tree_value)*tp);
3236 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
3237 splay_tree_insert (target_remap,
3238 (splay_tree_key)*tp,
3239 (splay_tree_value)*tp);
3241 return NULL_TREE;
3243 if (TREE_CODE (*tp) == DECL_EXPR
3244 && VAR_P (DECL_EXPR_DECL (*tp))
3245 && DECL_ARTIFICIAL (DECL_EXPR_DECL (*tp))
3246 && !TREE_STATIC (DECL_EXPR_DECL (*tp)))
3248 tree t;
3249 splay_tree_node n
3250 = splay_tree_lookup (target_remap,
3251 (splay_tree_key) DECL_EXPR_DECL (*tp));
3252 if (n)
3253 t = (tree) n->value;
3254 else
3256 t = create_temporary_var (TREE_TYPE (DECL_EXPR_DECL (*tp)));
3257 DECL_INITIAL (t) = DECL_INITIAL (DECL_EXPR_DECL (*tp));
3258 splay_tree_insert (target_remap,
3259 (splay_tree_key) DECL_EXPR_DECL (*tp),
3260 (splay_tree_value) t);
3262 copy_tree_r (tp, walk_subtrees, NULL);
3263 DECL_EXPR_DECL (*tp) = t;
3264 if (data.clear_location && EXPR_HAS_LOCATION (*tp))
3265 SET_EXPR_LOCATION (*tp, input_location);
3266 return NULL_TREE;
3268 if (TREE_CODE (*tp) == BIND_EXPR && BIND_EXPR_VARS (*tp))
3270 copy_tree_r (tp, walk_subtrees, NULL);
3271 for (tree *p = &BIND_EXPR_VARS (*tp); *p; p = &DECL_CHAIN (*p))
3273 gcc_assert (VAR_P (*p) && DECL_ARTIFICIAL (*p) && !TREE_STATIC (*p));
3274 tree t = create_temporary_var (TREE_TYPE (*p));
3275 DECL_INITIAL (t) = DECL_INITIAL (*p);
3276 DECL_CHAIN (t) = DECL_CHAIN (*p);
3277 splay_tree_insert (target_remap, (splay_tree_key) *p,
3278 (splay_tree_value) t);
3279 *p = t;
3281 if (data.clear_location && EXPR_HAS_LOCATION (*tp))
3282 SET_EXPR_LOCATION (*tp, input_location);
3283 return NULL_TREE;
3286 /* Make a copy of this node. */
3287 t = copy_tree_r (tp, walk_subtrees, NULL);
3288 if (TREE_CODE (*tp) == CALL_EXPR || TREE_CODE (*tp) == AGGR_INIT_EXPR)
3289 if (!processing_template_decl)
3290 set_flags_from_callee (*tp);
3291 if (data.clear_location && EXPR_HAS_LOCATION (*tp))
3292 SET_EXPR_LOCATION (*tp, input_location);
3293 return t;
3296 /* Replace all remapped VAR_DECLs in T with their new equivalents.
3297 DATA is really a splay-tree mapping old variables to new
3298 variables. */
3300 static tree
3301 bot_replace (tree* t, int* /*walk_subtrees*/, void* data_)
3303 bot_data &data = *(bot_data*)data_;
3304 splay_tree target_remap = data.target_remap;
3306 if (VAR_P (*t))
3308 splay_tree_node n = splay_tree_lookup (target_remap,
3309 (splay_tree_key) *t);
3310 if (n)
3311 *t = (tree) n->value;
3313 else if (TREE_CODE (*t) == PARM_DECL
3314 && DECL_NAME (*t) == this_identifier
3315 && !DECL_CONTEXT (*t))
3317 /* In an NSDMI we need to replace the 'this' parameter we used for
3318 parsing with the real one for this function. */
3319 *t = current_class_ptr;
3321 else if (TREE_CODE (*t) == CONVERT_EXPR
3322 && CONVERT_EXPR_VBASE_PATH (*t))
3324 /* In an NSDMI build_base_path defers building conversions to morally
3325 virtual bases, and we handle it here. */
3326 tree basetype = TREE_TYPE (*t);
3327 *t = convert_to_base (TREE_OPERAND (*t, 0), basetype,
3328 /*check_access=*/false, /*nonnull=*/true,
3329 tf_warning_or_error);
3332 return NULL_TREE;
3335 /* When we parse a default argument expression, we may create
3336 temporary variables via TARGET_EXPRs. When we actually use the
3337 default-argument expression, we make a copy of the expression
3338 and replace the temporaries with appropriate local versions.
3340 If CLEAR_LOCATION is true, override any EXPR_LOCATION with
3341 input_location. */
3343 tree
3344 break_out_target_exprs (tree t, bool clear_location /* = false */)
3346 static int target_remap_count;
3347 static splay_tree target_remap;
3349 if (!target_remap_count++)
3350 target_remap = splay_tree_new (splay_tree_compare_pointers,
3351 /*splay_tree_delete_key_fn=*/NULL,
3352 /*splay_tree_delete_value_fn=*/NULL);
3353 bot_data data = { target_remap, clear_location };
3354 if (cp_walk_tree (&t, bot_manip, &data, NULL) == error_mark_node)
3355 t = error_mark_node;
3356 cp_walk_tree (&t, bot_replace, &data, NULL);
3358 if (!--target_remap_count)
3360 splay_tree_delete (target_remap);
3361 target_remap = NULL;
3364 return t;
3367 /* Build an expression for the subobject of OBJ at CONSTRUCTOR index INDEX,
3368 which we expect to have type TYPE. */
3370 tree
3371 build_ctor_subob_ref (tree index, tree type, tree obj)
3373 if (index == NULL_TREE)
3374 /* Can't refer to a particular member of a vector. */
3375 obj = NULL_TREE;
3376 else if (TREE_CODE (index) == INTEGER_CST)
3377 obj = cp_build_array_ref (input_location, obj, index, tf_none);
3378 else
3379 obj = build_class_member_access_expr (obj, index, NULL_TREE,
3380 /*reference*/false, tf_none);
3381 if (obj)
3383 tree objtype = TREE_TYPE (obj);
3384 if (TREE_CODE (objtype) == ARRAY_TYPE && !TYPE_DOMAIN (objtype))
3386 /* When the destination object refers to a flexible array member
3387 verify that it matches the type of the source object except
3388 for its domain and qualifiers. */
3389 gcc_assert (comptypes (TYPE_MAIN_VARIANT (type),
3390 TYPE_MAIN_VARIANT (objtype),
3391 COMPARE_REDECLARATION));
3393 else
3394 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, objtype));
3397 return obj;
3400 struct replace_placeholders_t
3402 tree obj; /* The object to be substituted for a PLACEHOLDER_EXPR. */
3403 tree exp; /* The outermost exp. */
3404 bool seen; /* Whether we've encountered a PLACEHOLDER_EXPR. */
3405 hash_set<tree> *pset; /* To avoid walking same trees multiple times. */
3408 /* Like substitute_placeholder_in_expr, but handle C++ tree codes and
3409 build up subexpressions as we go deeper. */
3411 static tree
3412 replace_placeholders_r (tree* t, int* walk_subtrees, void* data_)
3414 replace_placeholders_t *d = static_cast<replace_placeholders_t*>(data_);
3415 tree obj = d->obj;
3417 if (TYPE_P (*t) || TREE_CONSTANT (*t))
3419 *walk_subtrees = false;
3420 return NULL_TREE;
3423 switch (TREE_CODE (*t))
3425 case PLACEHOLDER_EXPR:
3427 tree x = obj;
3428 for (; !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (*t),
3429 TREE_TYPE (x));
3430 x = TREE_OPERAND (x, 0))
3431 gcc_assert (handled_component_p (x));
3432 *t = unshare_expr (x);
3433 *walk_subtrees = false;
3434 d->seen = true;
3436 break;
3438 case CONSTRUCTOR:
3440 constructor_elt *ce;
3441 vec<constructor_elt,va_gc> *v = CONSTRUCTOR_ELTS (*t);
3442 /* Don't walk into CONSTRUCTOR_PLACEHOLDER_BOUNDARY ctors
3443 other than the d->exp one, those have PLACEHOLDER_EXPRs
3444 related to another object. */
3445 if ((CONSTRUCTOR_PLACEHOLDER_BOUNDARY (*t)
3446 && *t != d->exp)
3447 || d->pset->add (*t))
3449 *walk_subtrees = false;
3450 return NULL_TREE;
3452 for (unsigned i = 0; vec_safe_iterate (v, i, &ce); ++i)
3454 tree *valp = &ce->value;
3455 tree type = TREE_TYPE (*valp);
3456 tree subob = obj;
3458 /* Elements with RANGE_EXPR index shouldn't have any
3459 placeholders in them. */
3460 if (ce->index && TREE_CODE (ce->index) == RANGE_EXPR)
3461 continue;
3463 if (TREE_CODE (*valp) == CONSTRUCTOR
3464 && AGGREGATE_TYPE_P (type))
3466 /* If we're looking at the initializer for OBJ, then build
3467 a sub-object reference. If we're looking at an
3468 initializer for another object, just pass OBJ down. */
3469 if (same_type_ignoring_top_level_qualifiers_p
3470 (TREE_TYPE (*t), TREE_TYPE (obj)))
3471 subob = build_ctor_subob_ref (ce->index, type, obj);
3472 if (TREE_CODE (*valp) == TARGET_EXPR)
3473 valp = &TARGET_EXPR_INITIAL (*valp);
3475 d->obj = subob;
3476 cp_walk_tree (valp, replace_placeholders_r, data_, NULL);
3477 d->obj = obj;
3479 *walk_subtrees = false;
3480 break;
3483 default:
3484 if (d->pset->add (*t))
3485 *walk_subtrees = false;
3486 break;
3489 return NULL_TREE;
3492 /* Replace PLACEHOLDER_EXPRs in EXP with object OBJ. SEEN_P is set if
3493 a PLACEHOLDER_EXPR has been encountered. */
3495 tree
3496 replace_placeholders (tree exp, tree obj, bool *seen_p /*= NULL*/)
3498 /* This is only relevant for C++14. */
3499 if (cxx_dialect < cxx14)
3500 return exp;
3502 /* If the object isn't a (member of a) class, do nothing. */
3503 tree op0 = obj;
3504 while (handled_component_p (op0))
3505 op0 = TREE_OPERAND (op0, 0);
3506 if (!CLASS_TYPE_P (strip_array_types (TREE_TYPE (op0))))
3507 return exp;
3509 tree *tp = &exp;
3510 if (TREE_CODE (exp) == TARGET_EXPR)
3511 tp = &TARGET_EXPR_INITIAL (exp);
3512 hash_set<tree> pset;
3513 replace_placeholders_t data = { obj, *tp, false, &pset };
3514 cp_walk_tree (tp, replace_placeholders_r, &data, NULL);
3515 if (seen_p)
3516 *seen_p = data.seen;
3517 return exp;
3520 /* Callback function for find_placeholders. */
3522 static tree
3523 find_placeholders_r (tree *t, int *walk_subtrees, void *)
3525 if (TYPE_P (*t) || TREE_CONSTANT (*t))
3527 *walk_subtrees = false;
3528 return NULL_TREE;
3531 switch (TREE_CODE (*t))
3533 case PLACEHOLDER_EXPR:
3534 return *t;
3536 case CONSTRUCTOR:
3537 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (*t))
3538 *walk_subtrees = false;
3539 break;
3541 default:
3542 break;
3545 return NULL_TREE;
3548 /* Return true if EXP contains a PLACEHOLDER_EXPR. Don't walk into
3549 ctors with CONSTRUCTOR_PLACEHOLDER_BOUNDARY flag set. */
3551 bool
3552 find_placeholders (tree exp)
3554 /* This is only relevant for C++14. */
3555 if (cxx_dialect < cxx14)
3556 return false;
3558 return cp_walk_tree_without_duplicates (&exp, find_placeholders_r, NULL);
3561 /* Similar to `build_nt', but for template definitions of dependent
3562 expressions */
3564 tree
3565 build_min_nt_loc (location_t loc, enum tree_code code, ...)
3567 tree t;
3568 int length;
3569 int i;
3570 va_list p;
3572 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3574 va_start (p, code);
3576 t = make_node (code);
3577 SET_EXPR_LOCATION (t, loc);
3578 length = TREE_CODE_LENGTH (code);
3580 for (i = 0; i < length; i++)
3581 TREE_OPERAND (t, i) = va_arg (p, tree);
3583 va_end (p);
3584 return t;
3587 /* Similar to `build', but for template definitions. */
3589 tree
3590 build_min (enum tree_code code, tree tt, ...)
3592 tree t;
3593 int length;
3594 int i;
3595 va_list p;
3597 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3599 va_start (p, tt);
3601 t = make_node (code);
3602 length = TREE_CODE_LENGTH (code);
3603 TREE_TYPE (t) = tt;
3605 for (i = 0; i < length; i++)
3607 tree x = va_arg (p, tree);
3608 TREE_OPERAND (t, i) = x;
3609 if (x && !TYPE_P (x) && TREE_SIDE_EFFECTS (x))
3610 TREE_SIDE_EFFECTS (t) = 1;
3613 va_end (p);
3615 return t;
3618 /* Similar to `build', but for template definitions of non-dependent
3619 expressions. NON_DEP is the non-dependent expression that has been
3620 built. */
3622 tree
3623 build_min_non_dep (enum tree_code code, tree non_dep, ...)
3625 tree t;
3626 int length;
3627 int i;
3628 va_list p;
3630 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3632 va_start (p, non_dep);
3634 if (REFERENCE_REF_P (non_dep))
3635 non_dep = TREE_OPERAND (non_dep, 0);
3637 t = make_node (code);
3638 SET_EXPR_LOCATION (t, cp_expr_loc_or_input_loc (non_dep));
3639 length = TREE_CODE_LENGTH (code);
3640 TREE_TYPE (t) = unlowered_expr_type (non_dep);
3641 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
3643 for (i = 0; i < length; i++)
3644 TREE_OPERAND (t, i) = va_arg (p, tree);
3646 va_end (p);
3647 return convert_from_reference (t);
3650 /* Similar to build_min_nt, but call expressions */
3652 tree
3653 build_min_nt_call_vec (tree fn, vec<tree, va_gc> *args)
3655 tree ret, t;
3656 unsigned int ix;
3658 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
3659 CALL_EXPR_FN (ret) = fn;
3660 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
3661 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
3662 CALL_EXPR_ARG (ret, ix) = t;
3664 return ret;
3667 /* Similar to `build_min_nt_call_vec', but for template definitions of
3668 non-dependent expressions. NON_DEP is the non-dependent expression
3669 that has been built. */
3671 tree
3672 build_min_non_dep_call_vec (tree non_dep, tree fn, vec<tree, va_gc> *argvec)
3674 tree t = build_min_nt_call_vec (fn, argvec);
3675 if (REFERENCE_REF_P (non_dep))
3676 non_dep = TREE_OPERAND (non_dep, 0);
3677 TREE_TYPE (t) = TREE_TYPE (non_dep);
3678 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
3679 return convert_from_reference (t);
3682 /* Similar to build_min_non_dep, but for expressions that have been resolved to
3683 a call to an operator overload. OP is the operator that has been
3684 overloaded. NON_DEP is the non-dependent expression that's been built,
3685 which should be a CALL_EXPR or an INDIRECT_REF to a CALL_EXPR. OVERLOAD is
3686 the overload that NON_DEP is calling. */
3688 tree
3689 build_min_non_dep_op_overload (enum tree_code op,
3690 tree non_dep,
3691 tree overload, ...)
3693 va_list p;
3694 int nargs, expected_nargs;
3695 tree fn, call;
3697 non_dep = extract_call_expr (non_dep);
3699 nargs = call_expr_nargs (non_dep);
3701 expected_nargs = cp_tree_code_length (op);
3702 if (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE)
3703 expected_nargs -= 1;
3704 if ((op == POSTINCREMENT_EXPR
3705 || op == POSTDECREMENT_EXPR)
3706 /* With -fpermissive non_dep could be operator++(). */
3707 && (!flag_permissive || nargs != expected_nargs))
3708 expected_nargs += 1;
3709 gcc_assert (nargs == expected_nargs);
3711 releasing_vec args;
3712 va_start (p, overload);
3714 if (TREE_CODE (TREE_TYPE (overload)) == FUNCTION_TYPE)
3716 fn = overload;
3717 for (int i = 0; i < nargs; i++)
3719 tree arg = va_arg (p, tree);
3720 vec_safe_push (args, arg);
3723 else if (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE)
3725 tree object = va_arg (p, tree);
3726 tree binfo = TYPE_BINFO (TREE_TYPE (object));
3727 tree method = build_baselink (binfo, binfo, overload, NULL_TREE);
3728 fn = build_min (COMPONENT_REF, TREE_TYPE (overload),
3729 object, method, NULL_TREE);
3730 for (int i = 0; i < nargs; i++)
3732 tree arg = va_arg (p, tree);
3733 vec_safe_push (args, arg);
3736 else
3737 gcc_unreachable ();
3739 va_end (p);
3740 call = build_min_non_dep_call_vec (non_dep, fn, args);
3742 tree call_expr = extract_call_expr (call);
3743 KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep);
3744 CALL_EXPR_OPERATOR_SYNTAX (call_expr) = true;
3745 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep);
3746 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep);
3748 return call;
3751 /* Similar to above build_min_non_dep_op_overload, but arguments
3752 are taken from ARGS vector. */
3754 tree
3755 build_min_non_dep_op_overload (tree non_dep, tree overload, tree object,
3756 vec<tree, va_gc> *args)
3758 non_dep = extract_call_expr (non_dep);
3760 unsigned int nargs = call_expr_nargs (non_dep);
3761 gcc_assert (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE);
3762 tree binfo = TYPE_BINFO (TREE_TYPE (object));
3763 tree method = build_baselink (binfo, binfo, overload, NULL_TREE);
3764 tree fn = build_min (COMPONENT_REF, TREE_TYPE (overload),
3765 object, method, NULL_TREE);
3766 gcc_assert (vec_safe_length (args) == nargs);
3768 tree call = build_min_non_dep_call_vec (non_dep, fn, args);
3770 tree call_expr = extract_call_expr (call);
3771 KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep);
3772 CALL_EXPR_OPERATOR_SYNTAX (call_expr) = true;
3773 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep);
3774 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep);
3776 return call;
3779 /* Return a new tree vec copied from VEC, with ELT inserted at index IDX. */
3781 vec<tree, va_gc> *
3782 vec_copy_and_insert (vec<tree, va_gc> *old_vec, tree elt, unsigned idx)
3784 unsigned len = vec_safe_length (old_vec);
3785 gcc_assert (idx <= len);
3787 vec<tree, va_gc> *new_vec = NULL;
3788 vec_alloc (new_vec, len + 1);
3790 unsigned i;
3791 for (i = 0; i < len; ++i)
3793 if (i == idx)
3794 new_vec->quick_push (elt);
3795 new_vec->quick_push ((*old_vec)[i]);
3797 if (i == idx)
3798 new_vec->quick_push (elt);
3800 return new_vec;
3803 tree
3804 get_type_decl (tree t)
3806 if (TREE_CODE (t) == TYPE_DECL)
3807 return t;
3808 if (TYPE_P (t))
3809 return TYPE_STUB_DECL (t);
3810 gcc_assert (t == error_mark_node);
3811 return t;
3814 /* Returns the namespace that contains DECL, whether directly or
3815 indirectly. */
3817 tree
3818 decl_namespace_context (tree decl)
3820 while (1)
3822 if (TREE_CODE (decl) == NAMESPACE_DECL)
3823 return decl;
3824 else if (TYPE_P (decl))
3825 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
3826 else
3827 decl = CP_DECL_CONTEXT (decl);
3831 /* Returns true if decl is within an anonymous namespace, however deeply
3832 nested, or false otherwise. */
3834 bool
3835 decl_anon_ns_mem_p (tree decl)
3837 return !TREE_PUBLIC (decl_namespace_context (decl));
3840 /* Returns true if the enclosing scope of DECL has internal or no linkage. */
3842 bool
3843 decl_internal_context_p (const_tree decl)
3845 while (TREE_CODE (decl) != NAMESPACE_DECL)
3847 /* Classes inside anonymous namespaces have TREE_PUBLIC == 0. */
3848 if (TYPE_P (decl))
3849 return !TREE_PUBLIC (TYPE_MAIN_DECL (decl));
3851 decl = CP_DECL_CONTEXT (decl);
3853 return !TREE_PUBLIC (decl);
3856 /* Subroutine of cp_tree_equal: t1 and t2 are the CALL_EXPR_FNs of two
3857 CALL_EXPRS. Return whether they are equivalent. */
3859 static bool
3860 called_fns_equal (tree t1, tree t2)
3862 /* Core 1321: dependent names are equivalent even if the overload sets
3863 are different. But do compare explicit template arguments. */
3864 tree name1 = dependent_name (t1);
3865 tree name2 = dependent_name (t2);
3866 if (name1 || name2)
3868 tree targs1 = NULL_TREE, targs2 = NULL_TREE;
3870 if (name1 != name2)
3871 return false;
3873 /* FIXME dependent_name currently returns an unqualified name regardless
3874 of whether the function was named with a qualified- or unqualified-id.
3875 Until that's fixed, check that we aren't looking at overload sets from
3876 different scopes. */
3877 if (is_overloaded_fn (t1) && is_overloaded_fn (t2)
3878 && (DECL_CONTEXT (get_first_fn (t1))
3879 != DECL_CONTEXT (get_first_fn (t2))))
3880 return false;
3882 if (TREE_CODE (t1) == TEMPLATE_ID_EXPR)
3883 targs1 = TREE_OPERAND (t1, 1);
3884 if (TREE_CODE (t2) == TEMPLATE_ID_EXPR)
3885 targs2 = TREE_OPERAND (t2, 1);
3886 return cp_tree_equal (targs1, targs2);
3888 else
3889 return cp_tree_equal (t1, t2);
3892 /* Return truthvalue of whether T1 is the same tree structure as T2.
3893 Return 1 if they are the same. Return 0 if they are different. */
3895 bool
3896 cp_tree_equal (tree t1, tree t2)
3898 enum tree_code code1, code2;
3900 if (t1 == t2)
3901 return true;
3902 if (!t1 || !t2)
3903 return false;
3905 code1 = TREE_CODE (t1);
3906 code2 = TREE_CODE (t2);
3908 if (code1 != code2)
3909 return false;
3911 if (CONSTANT_CLASS_P (t1)
3912 && !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3913 return false;
3915 switch (code1)
3917 case VOID_CST:
3918 /* There's only a single VOID_CST node, so we should never reach
3919 here. */
3920 gcc_unreachable ();
3922 case INTEGER_CST:
3923 return tree_int_cst_equal (t1, t2);
3925 case REAL_CST:
3926 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
3928 case STRING_CST:
3929 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3930 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3931 TREE_STRING_LENGTH (t1));
3933 case FIXED_CST:
3934 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
3935 TREE_FIXED_CST (t2));
3937 case COMPLEX_CST:
3938 return cp_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
3939 && cp_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
3941 case VECTOR_CST:
3942 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
3944 case CONSTRUCTOR:
3945 /* We need to do this when determining whether or not two
3946 non-type pointer to member function template arguments
3947 are the same. */
3948 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
3949 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
3950 return false;
3952 tree field, value;
3953 unsigned int i;
3954 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
3956 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
3957 if (!cp_tree_equal (field, elt2->index)
3958 || !cp_tree_equal (value, elt2->value))
3959 return false;
3962 return true;
3964 case TREE_LIST:
3965 if (!cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
3966 return false;
3967 if (!cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
3968 return false;
3969 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
3971 case SAVE_EXPR:
3972 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3974 case CALL_EXPR:
3976 if (KOENIG_LOOKUP_P (t1) != KOENIG_LOOKUP_P (t2))
3977 return false;
3979 if (!called_fns_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
3980 return false;
3982 call_expr_arg_iterator iter1, iter2;
3983 init_call_expr_arg_iterator (t1, &iter1);
3984 init_call_expr_arg_iterator (t2, &iter2);
3985 if (iter1.n != iter2.n)
3986 return false;
3988 while (more_call_expr_args_p (&iter1))
3990 tree arg1 = next_call_expr_arg (&iter1);
3991 tree arg2 = next_call_expr_arg (&iter2);
3993 gcc_checking_assert (arg1 && arg2);
3994 if (!cp_tree_equal (arg1, arg2))
3995 return false;
3998 return true;
4001 case TARGET_EXPR:
4003 tree o1 = TREE_OPERAND (t1, 0);
4004 tree o2 = TREE_OPERAND (t2, 0);
4006 /* Special case: if either target is an unallocated VAR_DECL,
4007 it means that it's going to be unified with whatever the
4008 TARGET_EXPR is really supposed to initialize, so treat it
4009 as being equivalent to anything. */
4010 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
4011 && !DECL_RTL_SET_P (o1))
4012 /*Nop*/;
4013 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
4014 && !DECL_RTL_SET_P (o2))
4015 /*Nop*/;
4016 else if (!cp_tree_equal (o1, o2))
4017 return false;
4019 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
4022 case PARM_DECL:
4023 /* For comparing uses of parameters in late-specified return types
4024 with an out-of-class definition of the function, but can also come
4025 up for expressions that involve 'this' in a member function
4026 template. */
4028 if (comparing_specializations
4029 && DECL_CONTEXT (t1) != DECL_CONTEXT (t2))
4030 /* When comparing hash table entries, only an exact match is
4031 good enough; we don't want to replace 'this' with the
4032 version from another function. But be more flexible
4033 with parameters with identical contexts. */
4034 return false;
4036 if (same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
4038 if (DECL_ARTIFICIAL (t1) ^ DECL_ARTIFICIAL (t2))
4039 return false;
4040 if (CONSTRAINT_VAR_P (t1) ^ CONSTRAINT_VAR_P (t2))
4041 return false;
4042 if (DECL_ARTIFICIAL (t1)
4043 || (DECL_PARM_LEVEL (t1) == DECL_PARM_LEVEL (t2)
4044 && DECL_PARM_INDEX (t1) == DECL_PARM_INDEX (t2)))
4045 return true;
4047 return false;
4049 case VAR_DECL:
4050 case CONST_DECL:
4051 case FIELD_DECL:
4052 case FUNCTION_DECL:
4053 case TEMPLATE_DECL:
4054 case IDENTIFIER_NODE:
4055 case SSA_NAME:
4056 case USING_DECL:
4057 case DEFERRED_PARSE:
4058 return false;
4060 case BASELINK:
4061 return (BASELINK_BINFO (t1) == BASELINK_BINFO (t2)
4062 && BASELINK_ACCESS_BINFO (t1) == BASELINK_ACCESS_BINFO (t2)
4063 && BASELINK_QUALIFIED_P (t1) == BASELINK_QUALIFIED_P (t2)
4064 && cp_tree_equal (BASELINK_FUNCTIONS (t1),
4065 BASELINK_FUNCTIONS (t2)));
4067 case TEMPLATE_PARM_INDEX:
4068 return (TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
4069 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2)
4070 && (TEMPLATE_PARM_PARAMETER_PACK (t1)
4071 == TEMPLATE_PARM_PARAMETER_PACK (t2))
4072 && same_type_p (TREE_TYPE (TEMPLATE_PARM_DECL (t1)),
4073 TREE_TYPE (TEMPLATE_PARM_DECL (t2))));
4075 case TEMPLATE_ID_EXPR:
4076 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
4077 return false;
4078 if (!comp_template_args (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)))
4079 return false;
4080 return true;
4082 case CONSTRAINT_INFO:
4083 return cp_tree_equal (CI_ASSOCIATED_CONSTRAINTS (t1),
4084 CI_ASSOCIATED_CONSTRAINTS (t2));
4086 case CHECK_CONSTR:
4087 return (CHECK_CONSTR_CONCEPT (t1) == CHECK_CONSTR_CONCEPT (t2)
4088 && comp_template_args (CHECK_CONSTR_ARGS (t1),
4089 CHECK_CONSTR_ARGS (t2)));
4091 case TREE_VEC:
4092 /* These are template args. Really we should be getting the
4093 caller to do this as it knows it to be true. */
4094 if (!comp_template_args (t1, t2, NULL, NULL, false))
4095 return false;
4096 return true;
4098 case SIZEOF_EXPR:
4099 case ALIGNOF_EXPR:
4101 tree o1 = TREE_OPERAND (t1, 0);
4102 tree o2 = TREE_OPERAND (t2, 0);
4104 if (code1 == SIZEOF_EXPR)
4106 if (SIZEOF_EXPR_TYPE_P (t1))
4107 o1 = TREE_TYPE (o1);
4108 if (SIZEOF_EXPR_TYPE_P (t2))
4109 o2 = TREE_TYPE (o2);
4111 else if (ALIGNOF_EXPR_STD_P (t1) != ALIGNOF_EXPR_STD_P (t2))
4112 return false;
4114 if (TREE_CODE (o1) != TREE_CODE (o2))
4115 return false;
4117 if (ARGUMENT_PACK_P (o1))
4118 return template_args_equal (o1, o2);
4119 else if (TYPE_P (o1))
4120 return same_type_p (o1, o2);
4121 else
4122 return cp_tree_equal (o1, o2);
4125 case MODOP_EXPR:
4127 tree t1_op1, t2_op1;
4129 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
4130 return false;
4132 t1_op1 = TREE_OPERAND (t1, 1);
4133 t2_op1 = TREE_OPERAND (t2, 1);
4134 if (TREE_CODE (t1_op1) != TREE_CODE (t2_op1))
4135 return false;
4137 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t2, 2));
4140 case PTRMEM_CST:
4141 /* Two pointer-to-members are the same if they point to the same
4142 field or function in the same class. */
4143 if (PTRMEM_CST_MEMBER (t1) != PTRMEM_CST_MEMBER (t2))
4144 return false;
4146 return same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2));
4148 case OVERLOAD:
4150 /* Two overloads. Must be exactly the same set of decls. */
4151 lkp_iterator first (t1);
4152 lkp_iterator second (t2);
4154 for (; first && second; ++first, ++second)
4155 if (*first != *second)
4156 return false;
4157 return !(first || second);
4160 case TRAIT_EXPR:
4161 if (TRAIT_EXPR_KIND (t1) != TRAIT_EXPR_KIND (t2))
4162 return false;
4163 return same_type_p (TRAIT_EXPR_TYPE1 (t1), TRAIT_EXPR_TYPE1 (t2))
4164 && cp_tree_equal (TRAIT_EXPR_TYPE2 (t1), TRAIT_EXPR_TYPE2 (t2));
4166 case NON_LVALUE_EXPR:
4167 case VIEW_CONVERT_EXPR:
4168 /* Used for location wrappers with possibly NULL types. */
4169 if (!TREE_TYPE (t1) || !TREE_TYPE (t2))
4171 if (TREE_TYPE (t1) || TREE_TYPE (t2))
4172 return false;
4173 break;
4175 /* FALLTHROUGH */
4177 case CAST_EXPR:
4178 case STATIC_CAST_EXPR:
4179 case REINTERPRET_CAST_EXPR:
4180 case CONST_CAST_EXPR:
4181 case DYNAMIC_CAST_EXPR:
4182 case IMPLICIT_CONV_EXPR:
4183 case NEW_EXPR:
4184 case BIT_CAST_EXPR:
4185 CASE_CONVERT:
4186 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
4187 return false;
4188 /* Now compare operands as usual. */
4189 break;
4191 case DEFERRED_NOEXCEPT:
4192 return (cp_tree_equal (DEFERRED_NOEXCEPT_PATTERN (t1),
4193 DEFERRED_NOEXCEPT_PATTERN (t2))
4194 && comp_template_args (DEFERRED_NOEXCEPT_ARGS (t1),
4195 DEFERRED_NOEXCEPT_ARGS (t2)));
4197 case LAMBDA_EXPR:
4198 /* Two lambda-expressions are never considered equivalent. */
4199 return false;
4201 case TYPE_ARGUMENT_PACK:
4202 case NONTYPE_ARGUMENT_PACK:
4204 tree p1 = ARGUMENT_PACK_ARGS (t1);
4205 tree p2 = ARGUMENT_PACK_ARGS (t2);
4206 int len = TREE_VEC_LENGTH (p1);
4207 if (TREE_VEC_LENGTH (p2) != len)
4208 return false;
4210 for (int ix = 0; ix != len; ix++)
4211 if (!template_args_equal (TREE_VEC_ELT (p1, ix),
4212 TREE_VEC_ELT (p2, ix)))
4213 return false;
4214 return true;
4217 case EXPR_PACK_EXPANSION:
4218 if (!cp_tree_equal (PACK_EXPANSION_PATTERN (t1),
4219 PACK_EXPANSION_PATTERN (t2)))
4220 return false;
4221 if (!comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
4222 PACK_EXPANSION_EXTRA_ARGS (t2)))
4223 return false;
4224 return true;
4226 default:
4227 break;
4230 switch (TREE_CODE_CLASS (code1))
4232 case tcc_unary:
4233 case tcc_binary:
4234 case tcc_comparison:
4235 case tcc_expression:
4236 case tcc_vl_exp:
4237 case tcc_reference:
4238 case tcc_statement:
4240 int n = cp_tree_operand_length (t1);
4241 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
4242 && n != TREE_OPERAND_LENGTH (t2))
4243 return false;
4245 for (int i = 0; i < n; ++i)
4246 if (!cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
4247 return false;
4249 return true;
4252 case tcc_type:
4253 return same_type_p (t1, t2);
4255 default:
4256 gcc_unreachable ();
4259 /* We can get here with --disable-checking. */
4260 return false;
4263 /* The type of ARG when used as an lvalue. */
4265 tree
4266 lvalue_type (tree arg)
4268 tree type = TREE_TYPE (arg);
4269 return type;
4272 /* The type of ARG for printing error messages; denote lvalues with
4273 reference types. */
4275 tree
4276 error_type (tree arg)
4278 tree type = TREE_TYPE (arg);
4280 if (TREE_CODE (type) == ARRAY_TYPE)
4282 else if (TREE_CODE (type) == ERROR_MARK)
4284 else if (lvalue_p (arg))
4285 type = build_reference_type (lvalue_type (arg));
4286 else if (MAYBE_CLASS_TYPE_P (type))
4287 type = lvalue_type (arg);
4289 return type;
4292 /* Does FUNCTION use a variable-length argument list? */
4295 varargs_function_p (const_tree function)
4297 return stdarg_p (TREE_TYPE (function));
4300 /* Returns 1 if decl is a member of a class. */
4303 member_p (const_tree decl)
4305 const_tree const ctx = DECL_CONTEXT (decl);
4306 return (ctx && TYPE_P (ctx));
4309 /* Create a placeholder for member access where we don't actually have an
4310 object that the access is against. For a general declval<T> equivalent,
4311 use build_stub_object instead. */
4313 tree
4314 build_dummy_object (tree type)
4316 tree decl = build1 (CONVERT_EXPR, build_pointer_type (type), void_node);
4317 return cp_build_fold_indirect_ref (decl);
4320 /* We've gotten a reference to a member of TYPE. Return *this if appropriate,
4321 or a dummy object otherwise. If BINFOP is non-0, it is filled with the
4322 binfo path from current_class_type to TYPE, or 0. */
4324 tree
4325 maybe_dummy_object (tree type, tree* binfop)
4327 tree decl, context;
4328 tree binfo;
4329 tree current = current_nonlambda_class_type ();
4331 if (current
4332 && (binfo = lookup_base (current, type, ba_any, NULL,
4333 tf_warning_or_error)))
4334 context = current;
4335 else
4337 /* Reference from a nested class member function. */
4338 context = type;
4339 binfo = TYPE_BINFO (type);
4342 if (binfop)
4343 *binfop = binfo;
4345 /* current_class_ref might not correspond to current_class_type if
4346 we're in tsubst_default_argument or a lambda-declarator; in either
4347 case, we want to use current_class_ref if it matches CONTEXT. */
4348 tree ctype = current_class_ref ? TREE_TYPE (current_class_ref) : NULL_TREE;
4349 if (ctype
4350 && same_type_ignoring_top_level_qualifiers_p (ctype, context))
4351 decl = current_class_ref;
4352 else
4354 /* Return a dummy object whose cv-quals are consistent with (the
4355 non-lambda) 'this' if available. */
4356 if (ctype)
4358 int quals = TYPE_UNQUALIFIED;
4359 if (tree lambda = CLASSTYPE_LAMBDA_EXPR (ctype))
4361 if (tree cap = lambda_expr_this_capture (lambda, false))
4362 quals = cp_type_quals (TREE_TYPE (TREE_TYPE (cap)));
4364 else
4365 quals = cp_type_quals (ctype);
4366 context = cp_build_qualified_type (context, quals);
4368 decl = build_dummy_object (context);
4371 return decl;
4374 /* Returns 1 if OB is a placeholder object, or a pointer to one. */
4376 bool
4377 is_dummy_object (const_tree ob)
4379 if (INDIRECT_REF_P (ob))
4380 ob = TREE_OPERAND (ob, 0);
4381 return (TREE_CODE (ob) == CONVERT_EXPR
4382 && TREE_OPERAND (ob, 0) == void_node);
4385 /* Returns true if TYPE is char, unsigned char, or std::byte. */
4387 bool
4388 is_byte_access_type (tree type)
4390 type = TYPE_MAIN_VARIANT (type);
4391 if (type == char_type_node
4392 || type == unsigned_char_type_node)
4393 return true;
4395 return (TREE_CODE (type) == ENUMERAL_TYPE
4396 && TYPE_CONTEXT (type) == std_node
4397 && !strcmp ("byte", TYPE_NAME_STRING (type)));
4400 /* Returns true if TYPE is unsigned char or std::byte. */
4402 bool
4403 is_byte_access_type_not_plain_char (tree type)
4405 type = TYPE_MAIN_VARIANT (type);
4406 if (type == char_type_node)
4407 return false;
4409 return is_byte_access_type (type);
4412 /* Returns 1 iff type T is something we want to treat as a scalar type for
4413 the purpose of deciding whether it is trivial/POD/standard-layout. */
4415 bool
4416 scalarish_type_p (const_tree t)
4418 if (t == error_mark_node)
4419 return 1;
4421 return (SCALAR_TYPE_P (t) || VECTOR_TYPE_P (t));
4424 /* Returns true iff T requires non-trivial default initialization. */
4426 bool
4427 type_has_nontrivial_default_init (const_tree t)
4429 t = strip_array_types (CONST_CAST_TREE (t));
4431 if (CLASS_TYPE_P (t))
4432 return TYPE_HAS_COMPLEX_DFLT (t);
4433 else
4434 return 0;
4437 /* Track classes with only deleted copy/move constructors so that we can warn
4438 if they are used in call/return by value. */
4440 static GTY(()) hash_set<tree>* deleted_copy_types;
4441 static void
4442 remember_deleted_copy (const_tree t)
4444 if (!deleted_copy_types)
4445 deleted_copy_types = hash_set<tree>::create_ggc(37);
4446 deleted_copy_types->add (CONST_CAST_TREE (t));
4448 void
4449 maybe_warn_parm_abi (tree t, location_t loc)
4451 if (!deleted_copy_types
4452 || !deleted_copy_types->contains (t))
4453 return;
4455 if ((flag_abi_version == 12 || warn_abi_version == 12)
4456 && classtype_has_non_deleted_move_ctor (t))
4458 bool w;
4459 auto_diagnostic_group d;
4460 if (flag_abi_version > 12)
4461 w = warning_at (loc, OPT_Wabi, "%<-fabi-version=13%> (GCC 8.2) fixes "
4462 "the calling convention for %qT, which was "
4463 "accidentally changed in 8.1", t);
4464 else
4465 w = warning_at (loc, OPT_Wabi, "%<-fabi-version=12%> (GCC 8.1) "
4466 "accidentally changes the calling convention for %qT",
4468 if (w)
4469 inform (location_of (t), " declared here");
4470 return;
4473 auto_diagnostic_group d;
4474 if (warning_at (loc, OPT_Wabi, "the calling convention for %qT changes in "
4475 "%<-fabi-version=13%> (GCC 8.2)", t))
4476 inform (location_of (t), " because all of its copy and move "
4477 "constructors are deleted");
4480 /* Returns true iff copying an object of type T (including via move
4481 constructor) is non-trivial. That is, T has no non-trivial copy
4482 constructors and no non-trivial move constructors, and not all copy/move
4483 constructors are deleted. This function implements the ABI notion of
4484 non-trivial copy, which has diverged from the one in the standard. */
4486 bool
4487 type_has_nontrivial_copy_init (const_tree type)
4489 tree t = strip_array_types (CONST_CAST_TREE (type));
4491 if (CLASS_TYPE_P (t))
4493 gcc_assert (COMPLETE_TYPE_P (t));
4495 if (TYPE_HAS_COMPLEX_COPY_CTOR (t)
4496 || TYPE_HAS_COMPLEX_MOVE_CTOR (t))
4497 /* Nontrivial. */
4498 return true;
4500 if (cxx_dialect < cxx11)
4501 /* No deleted functions before C++11. */
4502 return false;
4504 /* Before ABI v12 we did a bitwise copy of types with only deleted
4505 copy/move constructors. */
4506 if (!abi_version_at_least (12)
4507 && !(warn_abi && abi_version_crosses (12)))
4508 return false;
4510 bool saw_copy = false;
4511 bool saw_non_deleted = false;
4512 bool saw_non_deleted_move = false;
4514 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
4515 saw_copy = saw_non_deleted = true;
4516 else if (CLASSTYPE_LAZY_COPY_CTOR (t))
4518 saw_copy = true;
4519 if (classtype_has_move_assign_or_move_ctor_p (t, true))
4520 /* [class.copy]/8 If the class definition declares a move
4521 constructor or move assignment operator, the implicitly declared
4522 copy constructor is defined as deleted.... */;
4523 else
4524 /* Any other reason the implicitly-declared function would be
4525 deleted would also cause TYPE_HAS_COMPLEX_COPY_CTOR to be
4526 set. */
4527 saw_non_deleted = true;
4530 if (!saw_non_deleted)
4531 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4533 tree fn = *iter;
4534 if (copy_fn_p (fn))
4536 saw_copy = true;
4537 if (!DECL_DELETED_FN (fn))
4539 /* Not deleted, therefore trivial. */
4540 saw_non_deleted = true;
4541 break;
4544 else if (move_fn_p (fn))
4545 if (!DECL_DELETED_FN (fn))
4546 saw_non_deleted_move = true;
4549 gcc_assert (saw_copy);
4551 /* ABI v12 buggily ignored move constructors. */
4552 bool v11nontriv = false;
4553 bool v12nontriv = !saw_non_deleted;
4554 bool v13nontriv = !saw_non_deleted && !saw_non_deleted_move;
4555 bool nontriv = (abi_version_at_least (13) ? v13nontriv
4556 : flag_abi_version == 12 ? v12nontriv
4557 : v11nontriv);
4558 bool warn_nontriv = (warn_abi_version >= 13 ? v13nontriv
4559 : warn_abi_version == 12 ? v12nontriv
4560 : v11nontriv);
4561 if (nontriv != warn_nontriv)
4562 remember_deleted_copy (t);
4564 return nontriv;
4566 else
4567 return 0;
4570 /* Returns 1 iff type T is a trivially copyable type, as defined in
4571 [basic.types] and [class]. */
4573 bool
4574 trivially_copyable_p (const_tree t)
4576 t = strip_array_types (CONST_CAST_TREE (t));
4578 if (CLASS_TYPE_P (t))
4579 return ((!TYPE_HAS_COPY_CTOR (t)
4580 || !TYPE_HAS_COMPLEX_COPY_CTOR (t))
4581 && !TYPE_HAS_COMPLEX_MOVE_CTOR (t)
4582 && (!TYPE_HAS_COPY_ASSIGN (t)
4583 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (t))
4584 && !TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
4585 && TYPE_HAS_TRIVIAL_DESTRUCTOR (t));
4586 else
4587 /* CWG 2094 makes volatile-qualified scalars trivially copyable again. */
4588 return scalarish_type_p (t);
4591 /* Returns 1 iff type T is a trivial type, as defined in [basic.types] and
4592 [class]. */
4594 bool
4595 trivial_type_p (const_tree t)
4597 t = strip_array_types (CONST_CAST_TREE (t));
4599 if (CLASS_TYPE_P (t))
4600 return (TYPE_HAS_TRIVIAL_DFLT (t)
4601 && trivially_copyable_p (t));
4602 else
4603 return scalarish_type_p (t);
4606 /* Returns 1 iff type T is a POD type, as defined in [basic.types]. */
4608 bool
4609 pod_type_p (const_tree t)
4611 /* This CONST_CAST is okay because strip_array_types returns its
4612 argument unmodified and we assign it to a const_tree. */
4613 t = strip_array_types (CONST_CAST_TREE(t));
4615 if (!CLASS_TYPE_P (t))
4616 return scalarish_type_p (t);
4617 else if (cxx_dialect > cxx98)
4618 /* [class]/10: A POD struct is a class that is both a trivial class and a
4619 standard-layout class, and has no non-static data members of type
4620 non-POD struct, non-POD union (or array of such types).
4622 We don't need to check individual members because if a member is
4623 non-std-layout or non-trivial, the class will be too. */
4624 return (std_layout_type_p (t) && trivial_type_p (t));
4625 else
4626 /* The C++98 definition of POD is different. */
4627 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
4630 /* Returns true iff T is POD for the purpose of layout, as defined in the
4631 C++ ABI. */
4633 bool
4634 layout_pod_type_p (const_tree t)
4636 t = strip_array_types (CONST_CAST_TREE (t));
4638 if (CLASS_TYPE_P (t))
4639 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
4640 else
4641 return scalarish_type_p (t);
4644 /* Returns true iff T is a standard-layout type, as defined in
4645 [basic.types]. */
4647 bool
4648 std_layout_type_p (const_tree t)
4650 t = strip_array_types (CONST_CAST_TREE (t));
4652 if (CLASS_TYPE_P (t))
4653 return !CLASSTYPE_NON_STD_LAYOUT (t);
4654 else
4655 return scalarish_type_p (t);
4658 static bool record_has_unique_obj_representations (const_tree, const_tree);
4660 /* Returns true iff T satisfies std::has_unique_object_representations<T>,
4661 as defined in [meta.unary.prop]. */
4663 bool
4664 type_has_unique_obj_representations (const_tree t)
4666 bool ret;
4668 t = strip_array_types (CONST_CAST_TREE (t));
4670 if (!trivially_copyable_p (t))
4671 return false;
4673 if (CLASS_TYPE_P (t) && CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t))
4674 return CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t);
4676 switch (TREE_CODE (t))
4678 case INTEGER_TYPE:
4679 case POINTER_TYPE:
4680 case REFERENCE_TYPE:
4681 /* If some backend has any paddings in these types, we should add
4682 a target hook for this and handle it there. */
4683 return true;
4685 case BOOLEAN_TYPE:
4686 /* For bool values other than 0 and 1 should only appear with
4687 undefined behavior. */
4688 return true;
4690 case ENUMERAL_TYPE:
4691 return type_has_unique_obj_representations (ENUM_UNDERLYING_TYPE (t));
4693 case REAL_TYPE:
4694 /* XFmode certainly contains padding on x86, which the CPU doesn't store
4695 when storing long double values, so for that we have to return false.
4696 Other kinds of floating point values are questionable due to +.0/-.0
4697 and NaNs, let's play safe for now. */
4698 return false;
4700 case FIXED_POINT_TYPE:
4701 return false;
4703 case OFFSET_TYPE:
4704 return true;
4706 case COMPLEX_TYPE:
4707 case VECTOR_TYPE:
4708 return type_has_unique_obj_representations (TREE_TYPE (t));
4710 case RECORD_TYPE:
4711 ret = record_has_unique_obj_representations (t, TYPE_SIZE (t));
4712 if (CLASS_TYPE_P (t))
4714 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
4715 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
4717 return ret;
4719 case UNION_TYPE:
4720 ret = true;
4721 bool any_fields;
4722 any_fields = false;
4723 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4724 if (TREE_CODE (field) == FIELD_DECL)
4726 any_fields = true;
4727 if (!type_has_unique_obj_representations (TREE_TYPE (field))
4728 || simple_cst_equal (DECL_SIZE (field), TYPE_SIZE (t)) != 1)
4730 ret = false;
4731 break;
4734 if (!any_fields && !integer_zerop (TYPE_SIZE (t)))
4735 ret = false;
4736 if (CLASS_TYPE_P (t))
4738 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
4739 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
4741 return ret;
4743 case NULLPTR_TYPE:
4744 return false;
4746 case ERROR_MARK:
4747 return false;
4749 default:
4750 gcc_unreachable ();
4754 /* Helper function for type_has_unique_obj_representations. */
4756 static bool
4757 record_has_unique_obj_representations (const_tree t, const_tree sz)
4759 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4760 if (TREE_CODE (field) != FIELD_DECL)
4762 /* For bases, can't use type_has_unique_obj_representations here, as in
4763 struct S { int i : 24; S (); };
4764 struct T : public S { int j : 8; T (); };
4765 S doesn't have unique obj representations, but T does. */
4766 else if (DECL_FIELD_IS_BASE (field))
4768 if (!record_has_unique_obj_representations (TREE_TYPE (field),
4769 DECL_SIZE (field)))
4770 return false;
4772 else if (DECL_C_BIT_FIELD (field))
4774 tree btype = DECL_BIT_FIELD_TYPE (field);
4775 if (!type_has_unique_obj_representations (btype))
4776 return false;
4778 else if (!type_has_unique_obj_representations (TREE_TYPE (field)))
4779 return false;
4781 offset_int cur = 0;
4782 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4783 if (TREE_CODE (field) == FIELD_DECL)
4785 offset_int fld = wi::to_offset (DECL_FIELD_OFFSET (field));
4786 offset_int bitpos = wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
4787 fld = fld * BITS_PER_UNIT + bitpos;
4788 if (cur != fld)
4789 return false;
4790 if (DECL_SIZE (field))
4792 offset_int size = wi::to_offset (DECL_SIZE (field));
4793 cur += size;
4796 if (cur != wi::to_offset (sz))
4797 return false;
4799 return true;
4802 /* Nonzero iff type T is a class template implicit specialization. */
4804 bool
4805 class_tmpl_impl_spec_p (const_tree t)
4807 return CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INSTANTIATION (t);
4810 /* Returns 1 iff zero initialization of type T means actually storing
4811 zeros in it. */
4814 zero_init_p (const_tree t)
4816 /* This CONST_CAST is okay because strip_array_types returns its
4817 argument unmodified and we assign it to a const_tree. */
4818 t = strip_array_types (CONST_CAST_TREE(t));
4820 if (t == error_mark_node)
4821 return 1;
4823 /* NULL pointers to data members are initialized with -1. */
4824 if (TYPE_PTRDATAMEM_P (t))
4825 return 0;
4827 /* Classes that contain types that can't be zero-initialized, cannot
4828 be zero-initialized themselves. */
4829 if (CLASS_TYPE_P (t) && CLASSTYPE_NON_ZERO_INIT_P (t))
4830 return 0;
4832 return 1;
4835 /* Returns true if the expression or initializer T is the result of
4836 zero-initialization for its type, taking pointers to members
4837 into consideration. */
4839 bool
4840 zero_init_expr_p (tree t)
4842 tree type = TREE_TYPE (t);
4843 if (!type || uses_template_parms (type))
4844 return false;
4845 if (TYPE_PTRMEM_P (type))
4846 return null_member_pointer_value_p (t);
4847 if (TREE_CODE (t) == CONSTRUCTOR)
4849 if (COMPOUND_LITERAL_P (t)
4850 || BRACE_ENCLOSED_INITIALIZER_P (t))
4851 /* Undigested, conversions might change the zeroness. */
4852 return false;
4853 for (constructor_elt &elt : CONSTRUCTOR_ELTS (t))
4855 if (TREE_CODE (type) == UNION_TYPE
4856 && elt.index != first_field (type))
4857 return false;
4858 if (!zero_init_expr_p (elt.value))
4859 return false;
4861 return true;
4863 if (zero_init_p (type))
4864 return initializer_zerop (t);
4865 return false;
4868 /* True IFF T is a C++20 structural type (P1907R1) that can be used as a
4869 non-type template parameter. If EXPLAIN, explain why not. */
4871 bool
4872 structural_type_p (tree t, bool explain)
4874 /* A structural type is one of the following: */
4876 /* a scalar type, or */
4877 if (SCALAR_TYPE_P (t))
4878 return true;
4879 /* an lvalue reference type, or */
4880 if (TYPE_REF_P (t) && !TYPE_REF_IS_RVALUE (t))
4881 return true;
4882 /* a literal class type with the following properties:
4883 - all base classes and non-static data members are public and non-mutable
4885 - the types of all bases classes and non-static data members are
4886 structural types or (possibly multi-dimensional) array thereof. */
4887 if (!CLASS_TYPE_P (t))
4888 return false;
4889 if (!literal_type_p (t))
4891 if (explain)
4892 explain_non_literal_class (t);
4893 return false;
4895 for (tree m = next_aggregate_field (TYPE_FIELDS (t)); m;
4896 m = next_aggregate_field (DECL_CHAIN (m)))
4898 if (TREE_PRIVATE (m) || TREE_PROTECTED (m))
4900 if (explain)
4902 if (DECL_FIELD_IS_BASE (m))
4903 inform (location_of (m), "base class %qT is not public",
4904 TREE_TYPE (m));
4905 else
4906 inform (location_of (m), "%qD is not public", m);
4908 return false;
4910 if (DECL_MUTABLE_P (m))
4912 if (explain)
4913 inform (location_of (m), "%qD is mutable", m);
4914 return false;
4916 tree mtype = strip_array_types (TREE_TYPE (m));
4917 if (!structural_type_p (mtype))
4919 if (explain)
4921 inform (location_of (m), "%qD has a non-structural type", m);
4922 structural_type_p (mtype, true);
4924 return false;
4927 return true;
4930 /* Handle the C++17 [[nodiscard]] attribute, which is similar to the GNU
4931 warn_unused_result attribute. */
4933 static tree
4934 handle_nodiscard_attribute (tree *node, tree name, tree args,
4935 int /*flags*/, bool *no_add_attrs)
4937 if (args && TREE_CODE (TREE_VALUE (args)) != STRING_CST)
4939 error ("%qE attribute argument must be a string constant", name);
4940 *no_add_attrs = true;
4942 if (TREE_CODE (*node) == FUNCTION_DECL)
4944 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node)))
4945 && !DECL_CONSTRUCTOR_P (*node))
4946 warning_at (DECL_SOURCE_LOCATION (*node),
4947 OPT_Wattributes, "%qE attribute applied to %qD with void "
4948 "return type", name, *node);
4950 else if (OVERLOAD_TYPE_P (*node))
4951 /* OK */;
4952 else
4954 warning (OPT_Wattributes, "%qE attribute can only be applied to "
4955 "functions or to class or enumeration types", name);
4956 *no_add_attrs = true;
4958 return NULL_TREE;
4961 /* Handle a C++20 "no_unique_address" attribute; arguments as in
4962 struct attribute_spec.handler. */
4963 static tree
4964 handle_no_unique_addr_attribute (tree* node,
4965 tree name,
4966 tree /*args*/,
4967 int /*flags*/,
4968 bool* no_add_attrs)
4970 if (TREE_CODE (*node) != FIELD_DECL)
4972 warning (OPT_Wattributes, "%qE attribute can only be applied to "
4973 "non-static data members", name);
4974 *no_add_attrs = true;
4976 else if (DECL_C_BIT_FIELD (*node))
4978 warning (OPT_Wattributes, "%qE attribute cannot be applied to "
4979 "a bit-field", name);
4980 *no_add_attrs = true;
4983 return NULL_TREE;
4986 /* The C++20 [[likely]] and [[unlikely]] attributes on labels map to the GNU
4987 hot/cold attributes. */
4989 static tree
4990 handle_likeliness_attribute (tree *node, tree name, tree args,
4991 int flags, bool *no_add_attrs)
4993 *no_add_attrs = true;
4994 if (TREE_CODE (*node) == LABEL_DECL
4995 || TREE_CODE (*node) == FUNCTION_DECL)
4997 if (args)
4998 warning (OPT_Wattributes, "%qE attribute takes no arguments", name);
4999 tree bname = (is_attribute_p ("likely", name)
5000 ? get_identifier ("hot") : get_identifier ("cold"));
5001 if (TREE_CODE (*node) == FUNCTION_DECL)
5002 warning (OPT_Wattributes, "ISO C++ %qE attribute does not apply to "
5003 "functions; treating as %<[[gnu::%E]]%>", name, bname);
5004 tree battr = build_tree_list (bname, NULL_TREE);
5005 decl_attributes (node, battr, flags);
5006 return NULL_TREE;
5008 else
5009 return error_mark_node;
5012 /* Table of valid C++ attributes. */
5013 const struct attribute_spec cxx_attribute_table[] =
5015 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
5016 affects_type_identity, handler, exclude } */
5017 { "init_priority", 1, 1, true, false, false, false,
5018 handle_init_priority_attribute, NULL },
5019 { "abi_tag", 1, -1, false, false, false, true,
5020 handle_abi_tag_attribute, NULL },
5021 { NULL, 0, 0, false, false, false, false, NULL, NULL }
5024 /* Table of C++ standard attributes. */
5025 const struct attribute_spec std_attribute_table[] =
5027 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
5028 affects_type_identity, handler, exclude } */
5029 { "maybe_unused", 0, 0, false, false, false, false,
5030 handle_unused_attribute, NULL },
5031 { "nodiscard", 0, 1, false, false, false, false,
5032 handle_nodiscard_attribute, NULL },
5033 { "no_unique_address", 0, 0, true, false, false, false,
5034 handle_no_unique_addr_attribute, NULL },
5035 { "likely", 0, 0, false, false, false, false,
5036 handle_likeliness_attribute, attr_cold_hot_exclusions },
5037 { "unlikely", 0, 0, false, false, false, false,
5038 handle_likeliness_attribute, attr_cold_hot_exclusions },
5039 { "noreturn", 0, 0, true, false, false, false,
5040 handle_noreturn_attribute, attr_noreturn_exclusions },
5041 { NULL, 0, 0, false, false, false, false, NULL, NULL }
5044 /* Handle an "init_priority" attribute; arguments as in
5045 struct attribute_spec.handler. */
5046 static tree
5047 handle_init_priority_attribute (tree* node,
5048 tree name,
5049 tree args,
5050 int /*flags*/,
5051 bool* no_add_attrs)
5053 tree initp_expr = TREE_VALUE (args);
5054 tree decl = *node;
5055 tree type = TREE_TYPE (decl);
5056 int pri;
5058 STRIP_NOPS (initp_expr);
5059 initp_expr = default_conversion (initp_expr);
5060 if (initp_expr)
5061 initp_expr = maybe_constant_value (initp_expr);
5063 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
5065 error ("requested %<init_priority%> is not an integer constant");
5066 cxx_constant_value (initp_expr);
5067 *no_add_attrs = true;
5068 return NULL_TREE;
5071 pri = TREE_INT_CST_LOW (initp_expr);
5073 type = strip_array_types (type);
5075 if (decl == NULL_TREE
5076 || !VAR_P (decl)
5077 || !TREE_STATIC (decl)
5078 || DECL_EXTERNAL (decl)
5079 || (TREE_CODE (type) != RECORD_TYPE
5080 && TREE_CODE (type) != UNION_TYPE)
5081 /* Static objects in functions are initialized the
5082 first time control passes through that
5083 function. This is not precise enough to pin down an
5084 init_priority value, so don't allow it. */
5085 || current_function_decl)
5087 error ("can only use %qE attribute on file-scope definitions "
5088 "of objects of class type", name);
5089 *no_add_attrs = true;
5090 return NULL_TREE;
5093 if (pri > MAX_INIT_PRIORITY || pri <= 0)
5095 error ("requested %<init_priority%> %i is out of range [0, %i]",
5096 pri, MAX_INIT_PRIORITY);
5097 *no_add_attrs = true;
5098 return NULL_TREE;
5101 /* Check for init_priorities that are reserved for
5102 language and runtime support implementations.*/
5103 if (pri <= MAX_RESERVED_INIT_PRIORITY)
5105 warning
5106 (0, "requested %<init_priority%> %i is reserved for internal use",
5107 pri);
5110 if (SUPPORTS_INIT_PRIORITY)
5112 SET_DECL_INIT_PRIORITY (decl, pri);
5113 DECL_HAS_INIT_PRIORITY_P (decl) = 1;
5114 return NULL_TREE;
5116 else
5118 error ("%qE attribute is not supported on this platform", name);
5119 *no_add_attrs = true;
5120 return NULL_TREE;
5124 /* DECL is being redeclared; the old declaration had the abi tags in OLD,
5125 and the new one has the tags in NEW_. Give an error if there are tags
5126 in NEW_ that weren't in OLD. */
5128 bool
5129 check_abi_tag_redeclaration (const_tree decl, const_tree old, const_tree new_)
5131 if (old && TREE_CODE (TREE_VALUE (old)) == TREE_LIST)
5132 old = TREE_VALUE (old);
5133 if (new_ && TREE_CODE (TREE_VALUE (new_)) == TREE_LIST)
5134 new_ = TREE_VALUE (new_);
5135 bool err = false;
5136 for (const_tree t = new_; t; t = TREE_CHAIN (t))
5138 tree str = TREE_VALUE (t);
5139 for (const_tree in = old; in; in = TREE_CHAIN (in))
5141 tree ostr = TREE_VALUE (in);
5142 if (cp_tree_equal (str, ostr))
5143 goto found;
5145 error ("redeclaration of %qD adds abi tag %qE", decl, str);
5146 err = true;
5147 found:;
5149 if (err)
5151 inform (DECL_SOURCE_LOCATION (decl), "previous declaration here");
5152 return false;
5154 return true;
5157 /* The abi_tag attribute with the name NAME was given ARGS. If they are
5158 ill-formed, give an error and return false; otherwise, return true. */
5160 bool
5161 check_abi_tag_args (tree args, tree name)
5163 if (!args)
5165 error ("the %qE attribute requires arguments", name);
5166 return false;
5168 for (tree arg = args; arg; arg = TREE_CHAIN (arg))
5170 tree elt = TREE_VALUE (arg);
5171 if (TREE_CODE (elt) != STRING_CST
5172 || (!same_type_ignoring_top_level_qualifiers_p
5173 (strip_array_types (TREE_TYPE (elt)),
5174 char_type_node)))
5176 error ("arguments to the %qE attribute must be narrow string "
5177 "literals", name);
5178 return false;
5180 const char *begin = TREE_STRING_POINTER (elt);
5181 const char *end = begin + TREE_STRING_LENGTH (elt);
5182 for (const char *p = begin; p != end; ++p)
5184 char c = *p;
5185 if (p == begin)
5187 if (!ISALPHA (c) && c != '_')
5189 error ("arguments to the %qE attribute must contain valid "
5190 "identifiers", name);
5191 inform (input_location, "%<%c%> is not a valid first "
5192 "character for an identifier", c);
5193 return false;
5196 else if (p == end - 1)
5197 gcc_assert (c == 0);
5198 else
5200 if (!ISALNUM (c) && c != '_')
5202 error ("arguments to the %qE attribute must contain valid "
5203 "identifiers", name);
5204 inform (input_location, "%<%c%> is not a valid character "
5205 "in an identifier", c);
5206 return false;
5211 return true;
5214 /* Handle an "abi_tag" attribute; arguments as in
5215 struct attribute_spec.handler. */
5217 static tree
5218 handle_abi_tag_attribute (tree* node, tree name, tree args,
5219 int flags, bool* no_add_attrs)
5221 if (!check_abi_tag_args (args, name))
5222 goto fail;
5224 if (TYPE_P (*node))
5226 if (!OVERLOAD_TYPE_P (*node))
5228 error ("%qE attribute applied to non-class, non-enum type %qT",
5229 name, *node);
5230 goto fail;
5232 else if (!(flags & (int)ATTR_FLAG_TYPE_IN_PLACE))
5234 error ("%qE attribute applied to %qT after its definition",
5235 name, *node);
5236 goto fail;
5238 else if (CLASS_TYPE_P (*node)
5239 && CLASSTYPE_TEMPLATE_INSTANTIATION (*node))
5241 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
5242 "template instantiation %qT", name, *node);
5243 goto fail;
5245 else if (CLASS_TYPE_P (*node)
5246 && CLASSTYPE_TEMPLATE_SPECIALIZATION (*node))
5248 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
5249 "template specialization %qT", name, *node);
5250 goto fail;
5253 tree attributes = TYPE_ATTRIBUTES (*node);
5254 tree decl = TYPE_NAME (*node);
5256 /* Make sure all declarations have the same abi tags. */
5257 if (DECL_SOURCE_LOCATION (decl) != input_location)
5259 if (!check_abi_tag_redeclaration (decl,
5260 lookup_attribute ("abi_tag",
5261 attributes),
5262 args))
5263 goto fail;
5266 else
5268 if (!VAR_OR_FUNCTION_DECL_P (*node))
5270 error ("%qE attribute applied to non-function, non-variable %qD",
5271 name, *node);
5272 goto fail;
5274 else if (DECL_LANGUAGE (*node) == lang_c)
5276 error ("%qE attribute applied to extern \"C\" declaration %qD",
5277 name, *node);
5278 goto fail;
5282 return NULL_TREE;
5284 fail:
5285 *no_add_attrs = true;
5286 return NULL_TREE;
5289 /* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the
5290 thing pointed to by the constant. */
5292 tree
5293 make_ptrmem_cst (tree type, tree member)
5295 tree ptrmem_cst = make_node (PTRMEM_CST);
5296 TREE_TYPE (ptrmem_cst) = type;
5297 PTRMEM_CST_MEMBER (ptrmem_cst) = member;
5298 PTRMEM_CST_LOCATION (ptrmem_cst) = input_location;
5299 return ptrmem_cst;
5302 /* Build a variant of TYPE that has the indicated ATTRIBUTES. May
5303 return an existing type if an appropriate type already exists. */
5305 tree
5306 cp_build_type_attribute_variant (tree type, tree attributes)
5308 tree new_type;
5310 new_type = build_type_attribute_variant (type, attributes);
5311 if (FUNC_OR_METHOD_TYPE_P (new_type))
5312 gcc_checking_assert (cxx_type_hash_eq (type, new_type));
5314 /* Making a new main variant of a class type is broken. */
5315 gcc_assert (!CLASS_TYPE_P (type) || new_type == type);
5317 return new_type;
5320 /* Return TRUE if TYPE1 and TYPE2 are identical for type hashing purposes.
5321 Called only after doing all language independent checks. */
5323 bool
5324 cxx_type_hash_eq (const_tree typea, const_tree typeb)
5326 gcc_assert (FUNC_OR_METHOD_TYPE_P (typea));
5328 if (type_memfn_rqual (typea) != type_memfn_rqual (typeb))
5329 return false;
5330 if (TYPE_HAS_LATE_RETURN_TYPE (typea) != TYPE_HAS_LATE_RETURN_TYPE (typeb))
5331 return false;
5332 return comp_except_specs (TYPE_RAISES_EXCEPTIONS (typea),
5333 TYPE_RAISES_EXCEPTIONS (typeb), ce_exact);
5336 /* Copy the language-specific type variant modifiers from TYPEB to TYPEA. For
5337 C++, these are the exception-specifier and ref-qualifier. */
5339 tree
5340 cxx_copy_lang_qualifiers (const_tree typea, const_tree typeb)
5342 tree type = CONST_CAST_TREE (typea);
5343 if (FUNC_OR_METHOD_TYPE_P (type))
5344 type = build_cp_fntype_variant (type, type_memfn_rqual (typeb),
5345 TYPE_RAISES_EXCEPTIONS (typeb),
5346 TYPE_HAS_LATE_RETURN_TYPE (typeb));
5347 return type;
5350 /* Apply FUNC to all language-specific sub-trees of TP in a pre-order
5351 traversal. Called from walk_tree. */
5353 tree
5354 cp_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func,
5355 void *data, hash_set<tree> *pset)
5357 enum tree_code code = TREE_CODE (*tp);
5358 tree result;
5360 #define WALK_SUBTREE(NODE) \
5361 do \
5363 result = cp_walk_tree (&(NODE), func, data, pset); \
5364 if (result) goto out; \
5366 while (0)
5368 if (TYPE_P (*tp))
5370 /* If *WALK_SUBTREES_P is 1, we're interested in the syntactic form of
5371 the argument, so don't look through typedefs, but do walk into
5372 template arguments for alias templates (and non-typedefed classes).
5374 If *WALK_SUBTREES_P > 1, we're interested in type identity or
5375 equivalence, so look through typedefs, ignoring template arguments for
5376 alias templates, and walk into template args of classes.
5378 See find_abi_tags_r for an example of setting *WALK_SUBTREES_P to 2
5379 when that's the behavior the walk_tree_fn wants. */
5380 if (*walk_subtrees_p == 1 && typedef_variant_p (*tp))
5382 if (tree ti = TYPE_ALIAS_TEMPLATE_INFO (*tp))
5383 WALK_SUBTREE (TI_ARGS (ti));
5384 *walk_subtrees_p = 0;
5385 return NULL_TREE;
5388 if (tree ti = TYPE_TEMPLATE_INFO (*tp))
5389 WALK_SUBTREE (TI_ARGS (ti));
5392 /* Not one of the easy cases. We must explicitly go through the
5393 children. */
5394 result = NULL_TREE;
5395 switch (code)
5397 case TEMPLATE_TYPE_PARM:
5398 if (template_placeholder_p (*tp))
5399 WALK_SUBTREE (CLASS_PLACEHOLDER_TEMPLATE (*tp));
5400 /* Fall through. */
5401 case DEFERRED_PARSE:
5402 case TEMPLATE_TEMPLATE_PARM:
5403 case BOUND_TEMPLATE_TEMPLATE_PARM:
5404 case UNBOUND_CLASS_TEMPLATE:
5405 case TEMPLATE_PARM_INDEX:
5406 case TYPEOF_TYPE:
5407 /* None of these have subtrees other than those already walked
5408 above. */
5409 *walk_subtrees_p = 0;
5410 break;
5412 case TYPENAME_TYPE:
5413 WALK_SUBTREE (TYPE_CONTEXT (*tp));
5414 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (*tp));
5415 *walk_subtrees_p = 0;
5416 break;
5418 case BASELINK:
5419 if (BASELINK_QUALIFIED_P (*tp))
5420 WALK_SUBTREE (BINFO_TYPE (BASELINK_ACCESS_BINFO (*tp)));
5421 WALK_SUBTREE (BASELINK_FUNCTIONS (*tp));
5422 *walk_subtrees_p = 0;
5423 break;
5425 case PTRMEM_CST:
5426 WALK_SUBTREE (TREE_TYPE (*tp));
5427 *walk_subtrees_p = 0;
5428 break;
5430 case TREE_LIST:
5431 WALK_SUBTREE (TREE_PURPOSE (*tp));
5432 break;
5434 case OVERLOAD:
5435 WALK_SUBTREE (OVL_FUNCTION (*tp));
5436 WALK_SUBTREE (OVL_CHAIN (*tp));
5437 *walk_subtrees_p = 0;
5438 break;
5440 case USING_DECL:
5441 WALK_SUBTREE (DECL_NAME (*tp));
5442 WALK_SUBTREE (USING_DECL_SCOPE (*tp));
5443 WALK_SUBTREE (USING_DECL_DECLS (*tp));
5444 *walk_subtrees_p = 0;
5445 break;
5447 case RECORD_TYPE:
5448 if (TYPE_PTRMEMFUNC_P (*tp))
5449 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (*tp));
5450 break;
5452 case TYPE_ARGUMENT_PACK:
5453 case NONTYPE_ARGUMENT_PACK:
5455 tree args = ARGUMENT_PACK_ARGS (*tp);
5456 for (tree arg : tree_vec_range (args))
5457 WALK_SUBTREE (arg);
5459 break;
5461 case TYPE_PACK_EXPANSION:
5462 WALK_SUBTREE (TREE_TYPE (*tp));
5463 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp));
5464 *walk_subtrees_p = 0;
5465 break;
5467 case EXPR_PACK_EXPANSION:
5468 WALK_SUBTREE (TREE_OPERAND (*tp, 0));
5469 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp));
5470 *walk_subtrees_p = 0;
5471 break;
5473 case CAST_EXPR:
5474 case REINTERPRET_CAST_EXPR:
5475 case STATIC_CAST_EXPR:
5476 case CONST_CAST_EXPR:
5477 case DYNAMIC_CAST_EXPR:
5478 case IMPLICIT_CONV_EXPR:
5479 case BIT_CAST_EXPR:
5480 if (TREE_TYPE (*tp))
5481 WALK_SUBTREE (TREE_TYPE (*tp));
5482 break;
5484 case CONSTRUCTOR:
5485 if (COMPOUND_LITERAL_P (*tp))
5486 WALK_SUBTREE (TREE_TYPE (*tp));
5487 break;
5489 case TRAIT_EXPR:
5490 WALK_SUBTREE (TRAIT_EXPR_TYPE1 (*tp));
5491 WALK_SUBTREE (TRAIT_EXPR_TYPE2 (*tp));
5492 *walk_subtrees_p = 0;
5493 break;
5495 case TRAIT_TYPE:
5496 WALK_SUBTREE (TRAIT_TYPE_TYPE1 (*tp));
5497 WALK_SUBTREE (TRAIT_TYPE_TYPE2 (*tp));
5498 *walk_subtrees_p = 0;
5499 break;
5501 case DECLTYPE_TYPE:
5502 ++cp_unevaluated_operand;
5503 /* We can't use WALK_SUBTREE here because of the goto. */
5504 result = cp_walk_tree (&DECLTYPE_TYPE_EXPR (*tp), func, data, pset);
5505 --cp_unevaluated_operand;
5506 *walk_subtrees_p = 0;
5507 break;
5509 case ALIGNOF_EXPR:
5510 case SIZEOF_EXPR:
5511 case NOEXCEPT_EXPR:
5512 ++cp_unevaluated_operand;
5513 result = cp_walk_tree (&TREE_OPERAND (*tp, 0), func, data, pset);
5514 --cp_unevaluated_operand;
5515 *walk_subtrees_p = 0;
5516 break;
5518 case REQUIRES_EXPR:
5519 // Only recurse through the nested expression. Do not
5520 // walk the parameter list. Doing so causes false
5521 // positives in the pack expansion checker since the
5522 // requires parameters are introduced as pack expansions.
5523 ++cp_unevaluated_operand;
5524 result = cp_walk_tree (&REQUIRES_EXPR_REQS (*tp), func, data, pset);
5525 --cp_unevaluated_operand;
5526 *walk_subtrees_p = 0;
5527 break;
5529 case DECL_EXPR:
5530 /* User variables should be mentioned in BIND_EXPR_VARS
5531 and their initializers and sizes walked when walking
5532 the containing BIND_EXPR. Compiler temporaries are
5533 handled here. And also normal variables in templates,
5534 since do_poplevel doesn't build a BIND_EXPR then. */
5535 if (VAR_P (TREE_OPERAND (*tp, 0))
5536 && (processing_template_decl
5537 || (DECL_ARTIFICIAL (TREE_OPERAND (*tp, 0))
5538 && !TREE_STATIC (TREE_OPERAND (*tp, 0)))))
5540 tree decl = TREE_OPERAND (*tp, 0);
5541 WALK_SUBTREE (DECL_INITIAL (decl));
5542 WALK_SUBTREE (DECL_SIZE (decl));
5543 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
5545 break;
5547 case LAMBDA_EXPR:
5548 /* Don't walk into the body of the lambda, but the capture initializers
5549 are part of the enclosing context. */
5550 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (*tp); cap;
5551 cap = TREE_CHAIN (cap))
5552 WALK_SUBTREE (TREE_VALUE (cap));
5553 break;
5555 case CO_YIELD_EXPR:
5556 if (TREE_OPERAND (*tp, 1))
5557 /* Operand 1 is the tree for the relevant co_await which has any
5558 interesting sub-trees. */
5559 WALK_SUBTREE (TREE_OPERAND (*tp, 1));
5560 break;
5562 case CO_AWAIT_EXPR:
5563 if (TREE_OPERAND (*tp, 1))
5564 /* Operand 1 is frame variable. */
5565 WALK_SUBTREE (TREE_OPERAND (*tp, 1));
5566 if (TREE_OPERAND (*tp, 2))
5567 /* Operand 2 has the initialiser, and we need to walk any subtrees
5568 there. */
5569 WALK_SUBTREE (TREE_OPERAND (*tp, 2));
5570 break;
5572 case CO_RETURN_EXPR:
5573 if (TREE_OPERAND (*tp, 0))
5575 if (VOID_TYPE_P (TREE_OPERAND (*tp, 0)))
5576 /* For void expressions, operand 1 is a trivial call, and any
5577 interesting subtrees will be part of operand 0. */
5578 WALK_SUBTREE (TREE_OPERAND (*tp, 0));
5579 else if (TREE_OPERAND (*tp, 1))
5580 /* Interesting sub-trees will be in the return_value () call
5581 arguments. */
5582 WALK_SUBTREE (TREE_OPERAND (*tp, 1));
5584 break;
5586 case STATIC_ASSERT:
5587 WALK_SUBTREE (STATIC_ASSERT_CONDITION (*tp));
5588 WALK_SUBTREE (STATIC_ASSERT_MESSAGE (*tp));
5589 break;
5591 default:
5592 return NULL_TREE;
5595 /* We didn't find what we were looking for. */
5596 out:
5597 return result;
5599 #undef WALK_SUBTREE
5602 /* Like save_expr, but for C++. */
5604 tree
5605 cp_save_expr (tree expr)
5607 /* There is no reason to create a SAVE_EXPR within a template; if
5608 needed, we can create the SAVE_EXPR when instantiating the
5609 template. Furthermore, the middle-end cannot handle C++-specific
5610 tree codes. */
5611 if (processing_template_decl)
5612 return expr;
5614 /* TARGET_EXPRs are only expanded once. */
5615 if (TREE_CODE (expr) == TARGET_EXPR)
5616 return expr;
5618 return save_expr (expr);
5621 /* Initialize tree.cc. */
5623 void
5624 init_tree (void)
5626 list_hash_table = hash_table<list_hasher>::create_ggc (61);
5627 register_scoped_attributes (std_attribute_table, NULL);
5630 /* Returns the kind of special function that DECL (a FUNCTION_DECL)
5631 is. Note that sfk_none is zero, so this function can be used as a
5632 predicate to test whether or not DECL is a special function. */
5634 special_function_kind
5635 special_function_p (const_tree decl)
5637 /* Rather than doing all this stuff with magic names, we should
5638 probably have a field of type `special_function_kind' in
5639 DECL_LANG_SPECIFIC. */
5640 if (DECL_INHERITED_CTOR (decl))
5641 return sfk_inheriting_constructor;
5642 if (DECL_COPY_CONSTRUCTOR_P (decl))
5643 return sfk_copy_constructor;
5644 if (DECL_MOVE_CONSTRUCTOR_P (decl))
5645 return sfk_move_constructor;
5646 if (DECL_CONSTRUCTOR_P (decl))
5647 return sfk_constructor;
5648 if (DECL_ASSIGNMENT_OPERATOR_P (decl)
5649 && DECL_OVERLOADED_OPERATOR_IS (decl, NOP_EXPR))
5651 if (copy_fn_p (decl))
5652 return sfk_copy_assignment;
5653 if (move_fn_p (decl))
5654 return sfk_move_assignment;
5656 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
5657 return sfk_destructor;
5658 if (DECL_COMPLETE_DESTRUCTOR_P (decl))
5659 return sfk_complete_destructor;
5660 if (DECL_BASE_DESTRUCTOR_P (decl))
5661 return sfk_base_destructor;
5662 if (DECL_DELETING_DESTRUCTOR_P (decl))
5663 return sfk_deleting_destructor;
5664 if (DECL_CONV_FN_P (decl))
5665 return sfk_conversion;
5666 if (deduction_guide_p (decl))
5667 return sfk_deduction_guide;
5668 if (DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) >= OVL_OP_EQ_EXPR
5669 && DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) <= OVL_OP_SPACESHIP_EXPR)
5670 return sfk_comparison;
5672 return sfk_none;
5675 /* As above, but only if DECL is a special member function as per 11.3.3
5676 [special]: default/copy/move ctor, copy/move assignment, or destructor. */
5678 special_function_kind
5679 special_memfn_p (const_tree decl)
5681 switch (special_function_kind sfk = special_function_p (decl))
5683 case sfk_constructor:
5684 if (!default_ctor_p (decl))
5685 break;
5686 gcc_fallthrough();
5687 case sfk_copy_constructor:
5688 case sfk_copy_assignment:
5689 case sfk_move_assignment:
5690 case sfk_move_constructor:
5691 case sfk_destructor:
5692 return sfk;
5694 default:
5695 break;
5697 return sfk_none;
5700 /* Returns nonzero if TYPE is a character type, including wchar_t. */
5703 char_type_p (tree type)
5705 return (same_type_p (type, char_type_node)
5706 || same_type_p (type, unsigned_char_type_node)
5707 || same_type_p (type, signed_char_type_node)
5708 || same_type_p (type, char8_type_node)
5709 || same_type_p (type, char16_type_node)
5710 || same_type_p (type, char32_type_node)
5711 || same_type_p (type, wchar_type_node));
5714 /* Returns the kind of linkage associated with the indicated DECL. Th
5715 value returned is as specified by the language standard; it is
5716 independent of implementation details regarding template
5717 instantiation, etc. For example, it is possible that a declaration
5718 to which this function assigns external linkage would not show up
5719 as a global symbol when you run `nm' on the resulting object file. */
5721 linkage_kind
5722 decl_linkage (tree decl)
5724 /* This function doesn't attempt to calculate the linkage from first
5725 principles as given in [basic.link]. Instead, it makes use of
5726 the fact that we have already set TREE_PUBLIC appropriately, and
5727 then handles a few special cases. Ideally, we would calculate
5728 linkage first, and then transform that into a concrete
5729 implementation. */
5731 /* Things that don't have names have no linkage. */
5732 if (!DECL_NAME (decl))
5733 return lk_none;
5735 /* Fields have no linkage. */
5736 if (TREE_CODE (decl) == FIELD_DECL)
5737 return lk_none;
5739 /* Things in local scope do not have linkage. */
5740 if (decl_function_context (decl))
5741 return lk_none;
5743 /* Things that are TREE_PUBLIC have external linkage. */
5744 if (TREE_PUBLIC (decl))
5745 return lk_external;
5747 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor variants,
5748 check one of the "clones" for the real linkage. */
5749 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl)
5750 && DECL_CHAIN (decl)
5751 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)))
5752 return decl_linkage (DECL_CHAIN (decl));
5754 if (TREE_CODE (decl) == NAMESPACE_DECL)
5755 return lk_external;
5757 /* Linkage of a CONST_DECL depends on the linkage of the enumeration
5758 type. */
5759 if (TREE_CODE (decl) == CONST_DECL)
5760 return decl_linkage (TYPE_NAME (DECL_CONTEXT (decl)));
5762 /* Members of the anonymous namespace also have TREE_PUBLIC unset, but
5763 are considered to have external linkage for language purposes, as do
5764 template instantiations on targets without weak symbols. DECLs really
5765 meant to have internal linkage have DECL_THIS_STATIC set. */
5766 if (TREE_CODE (decl) == TYPE_DECL)
5767 return lk_external;
5768 if (VAR_OR_FUNCTION_DECL_P (decl))
5770 if (!DECL_THIS_STATIC (decl))
5771 return lk_external;
5773 /* Static data members and static member functions from classes
5774 in anonymous namespace also don't have TREE_PUBLIC set. */
5775 if (DECL_CLASS_CONTEXT (decl))
5776 return lk_external;
5779 /* Everything else has internal linkage. */
5780 return lk_internal;
5783 /* Returns the storage duration of the object or reference associated with
5784 the indicated DECL, which should be a VAR_DECL or PARM_DECL. */
5786 duration_kind
5787 decl_storage_duration (tree decl)
5789 if (TREE_CODE (decl) == PARM_DECL)
5790 return dk_auto;
5791 if (TREE_CODE (decl) == FUNCTION_DECL)
5792 return dk_static;
5793 gcc_assert (VAR_P (decl));
5794 if (!TREE_STATIC (decl)
5795 && !DECL_EXTERNAL (decl))
5796 return dk_auto;
5797 if (CP_DECL_THREAD_LOCAL_P (decl))
5798 return dk_thread;
5799 return dk_static;
5802 /* EXP is an expression that we want to pre-evaluate. Returns (in
5803 *INITP) an expression that will perform the pre-evaluation. The
5804 value returned by this function is a side-effect free expression
5805 equivalent to the pre-evaluated expression. Callers must ensure
5806 that *INITP is evaluated before EXP. */
5808 tree
5809 stabilize_expr (tree exp, tree* initp)
5811 tree init_expr;
5813 if (!TREE_SIDE_EFFECTS (exp))
5814 init_expr = NULL_TREE;
5815 else if (VOID_TYPE_P (TREE_TYPE (exp)))
5817 init_expr = exp;
5818 exp = void_node;
5820 /* There are no expressions with REFERENCE_TYPE, but there can be call
5821 arguments with such a type; just treat it as a pointer. */
5822 else if (TYPE_REF_P (TREE_TYPE (exp))
5823 || SCALAR_TYPE_P (TREE_TYPE (exp))
5824 || !glvalue_p (exp))
5826 init_expr = get_target_expr (exp);
5827 exp = TARGET_EXPR_SLOT (init_expr);
5828 if (CLASS_TYPE_P (TREE_TYPE (exp)))
5829 exp = move (exp);
5830 else
5831 exp = rvalue (exp);
5833 else
5835 bool xval = !lvalue_p (exp);
5836 exp = cp_build_addr_expr (exp, tf_warning_or_error);
5837 init_expr = get_target_expr (exp);
5838 exp = TARGET_EXPR_SLOT (init_expr);
5839 exp = cp_build_fold_indirect_ref (exp);
5840 if (xval)
5841 exp = move (exp);
5843 *initp = init_expr;
5845 gcc_assert (!TREE_SIDE_EFFECTS (exp));
5846 return exp;
5849 /* Add NEW_EXPR, an expression whose value we don't care about, after the
5850 similar expression ORIG. */
5852 tree
5853 add_stmt_to_compound (tree orig, tree new_expr)
5855 if (!new_expr || !TREE_SIDE_EFFECTS (new_expr))
5856 return orig;
5857 if (!orig || !TREE_SIDE_EFFECTS (orig))
5858 return new_expr;
5859 return build2 (COMPOUND_EXPR, void_type_node, orig, new_expr);
5862 /* Like stabilize_expr, but for a call whose arguments we want to
5863 pre-evaluate. CALL is modified in place to use the pre-evaluated
5864 arguments, while, upon return, *INITP contains an expression to
5865 compute the arguments. */
5867 void
5868 stabilize_call (tree call, tree *initp)
5870 tree inits = NULL_TREE;
5871 int i;
5872 int nargs = call_expr_nargs (call);
5874 if (call == error_mark_node || processing_template_decl)
5876 *initp = NULL_TREE;
5877 return;
5880 gcc_assert (TREE_CODE (call) == CALL_EXPR);
5882 for (i = 0; i < nargs; i++)
5884 tree init;
5885 CALL_EXPR_ARG (call, i) =
5886 stabilize_expr (CALL_EXPR_ARG (call, i), &init);
5887 inits = add_stmt_to_compound (inits, init);
5890 *initp = inits;
5893 /* Like stabilize_expr, but for an AGGR_INIT_EXPR whose arguments we want
5894 to pre-evaluate. CALL is modified in place to use the pre-evaluated
5895 arguments, while, upon return, *INITP contains an expression to
5896 compute the arguments. */
5898 static void
5899 stabilize_aggr_init (tree call, tree *initp)
5901 tree inits = NULL_TREE;
5902 int i;
5903 int nargs = aggr_init_expr_nargs (call);
5905 if (call == error_mark_node)
5906 return;
5908 gcc_assert (TREE_CODE (call) == AGGR_INIT_EXPR);
5910 for (i = 0; i < nargs; i++)
5912 tree init;
5913 AGGR_INIT_EXPR_ARG (call, i) =
5914 stabilize_expr (AGGR_INIT_EXPR_ARG (call, i), &init);
5915 inits = add_stmt_to_compound (inits, init);
5918 *initp = inits;
5921 /* Like stabilize_expr, but for an initialization.
5923 If the initialization is for an object of class type, this function
5924 takes care not to introduce additional temporaries.
5926 Returns TRUE iff the expression was successfully pre-evaluated,
5927 i.e., if INIT is now side-effect free, except for, possibly, a
5928 single call to a constructor. */
5930 bool
5931 stabilize_init (tree init, tree *initp)
5933 tree t = init;
5935 *initp = NULL_TREE;
5937 if (t == error_mark_node || processing_template_decl)
5938 return true;
5940 if (TREE_CODE (t) == INIT_EXPR)
5941 t = TREE_OPERAND (t, 1);
5942 if (TREE_CODE (t) == TARGET_EXPR)
5943 t = TARGET_EXPR_INITIAL (t);
5945 /* If the RHS can be stabilized without breaking copy elision, stabilize
5946 it. We specifically don't stabilize class prvalues here because that
5947 would mean an extra copy, but they might be stabilized below. */
5948 if (TREE_CODE (init) == INIT_EXPR
5949 && TREE_CODE (t) != CONSTRUCTOR
5950 && TREE_CODE (t) != AGGR_INIT_EXPR
5951 && (SCALAR_TYPE_P (TREE_TYPE (t))
5952 || glvalue_p (t)))
5954 TREE_OPERAND (init, 1) = stabilize_expr (t, initp);
5955 return true;
5958 if (TREE_CODE (t) == COMPOUND_EXPR
5959 && TREE_CODE (init) == INIT_EXPR)
5961 tree last = expr_last (t);
5962 /* Handle stabilizing the EMPTY_CLASS_EXPR pattern. */
5963 if (!TREE_SIDE_EFFECTS (last))
5965 *initp = t;
5966 TREE_OPERAND (init, 1) = last;
5967 return true;
5971 if (TREE_CODE (t) == CONSTRUCTOR)
5973 /* Aggregate initialization: stabilize each of the field
5974 initializers. */
5975 unsigned i;
5976 constructor_elt *ce;
5977 bool good = true;
5978 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
5979 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
5981 tree type = TREE_TYPE (ce->value);
5982 tree subinit;
5983 if (TYPE_REF_P (type)
5984 || SCALAR_TYPE_P (type))
5985 ce->value = stabilize_expr (ce->value, &subinit);
5986 else if (!stabilize_init (ce->value, &subinit))
5987 good = false;
5988 *initp = add_stmt_to_compound (*initp, subinit);
5990 return good;
5993 if (TREE_CODE (t) == CALL_EXPR)
5995 stabilize_call (t, initp);
5996 return true;
5999 if (TREE_CODE (t) == AGGR_INIT_EXPR)
6001 stabilize_aggr_init (t, initp);
6002 return true;
6005 /* The initialization is being performed via a bitwise copy -- and
6006 the item copied may have side effects. */
6007 return !TREE_SIDE_EFFECTS (init);
6010 /* Returns true if a cast to TYPE may appear in an integral constant
6011 expression. */
6013 bool
6014 cast_valid_in_integral_constant_expression_p (tree type)
6016 return (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6017 || cxx_dialect >= cxx11
6018 || dependent_type_p (type)
6019 || type == error_mark_node);
6022 /* Return true if we need to fix linkage information of DECL. */
6024 static bool
6025 cp_fix_function_decl_p (tree decl)
6027 /* Skip if DECL is not externally visible. */
6028 if (!TREE_PUBLIC (decl))
6029 return false;
6031 /* We need to fix DECL if it a appears to be exported but with no
6032 function body. Thunks do not have CFGs and we may need to
6033 handle them specially later. */
6034 if (!gimple_has_body_p (decl)
6035 && !DECL_THUNK_P (decl)
6036 && !DECL_EXTERNAL (decl))
6038 struct cgraph_node *node = cgraph_node::get (decl);
6040 /* Don't fix same_body aliases. Although they don't have their own
6041 CFG, they share it with what they alias to. */
6042 if (!node || !node->alias || !node->num_references ())
6043 return true;
6046 return false;
6049 /* Clean the C++ specific parts of the tree T. */
6051 void
6052 cp_free_lang_data (tree t)
6054 if (FUNC_OR_METHOD_TYPE_P (t))
6056 /* Default args are not interesting anymore. */
6057 tree argtypes = TYPE_ARG_TYPES (t);
6058 while (argtypes)
6060 TREE_PURPOSE (argtypes) = 0;
6061 argtypes = TREE_CHAIN (argtypes);
6064 else if (TREE_CODE (t) == FUNCTION_DECL
6065 && cp_fix_function_decl_p (t))
6067 /* If T is used in this translation unit at all, the definition
6068 must exist somewhere else since we have decided to not emit it
6069 in this TU. So make it an external reference. */
6070 DECL_EXTERNAL (t) = 1;
6071 TREE_STATIC (t) = 0;
6073 if (TREE_CODE (t) == NAMESPACE_DECL)
6074 /* We do not need the leftover chaining of namespaces from the
6075 binding level. */
6076 DECL_CHAIN (t) = NULL_TREE;
6079 /* Stub for c-common. Please keep in sync with c-decl.cc.
6080 FIXME: If address space support is target specific, then this
6081 should be a C target hook. But currently this is not possible,
6082 because this function is called via REGISTER_TARGET_PRAGMAS. */
6083 void
6084 c_register_addr_space (const char * /*word*/, addr_space_t /*as*/)
6088 /* Return the number of operands in T that we care about for things like
6089 mangling. */
6092 cp_tree_operand_length (const_tree t)
6094 enum tree_code code = TREE_CODE (t);
6096 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
6097 return VL_EXP_OPERAND_LENGTH (t);
6099 return cp_tree_code_length (code);
6102 /* Like cp_tree_operand_length, but takes a tree_code CODE. */
6105 cp_tree_code_length (enum tree_code code)
6107 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
6109 switch (code)
6111 case PREINCREMENT_EXPR:
6112 case PREDECREMENT_EXPR:
6113 case POSTINCREMENT_EXPR:
6114 case POSTDECREMENT_EXPR:
6115 return 1;
6117 case ARRAY_REF:
6118 return 2;
6120 case EXPR_PACK_EXPANSION:
6121 return 1;
6123 default:
6124 return TREE_CODE_LENGTH (code);
6128 /* Like EXPR_LOCATION, but also handle some tcc_exceptional that have
6129 locations. */
6131 location_t
6132 cp_expr_location (const_tree t_)
6134 tree t = CONST_CAST_TREE (t_);
6135 if (t == NULL_TREE)
6136 return UNKNOWN_LOCATION;
6137 switch (TREE_CODE (t))
6139 case LAMBDA_EXPR:
6140 return LAMBDA_EXPR_LOCATION (t);
6141 case STATIC_ASSERT:
6142 return STATIC_ASSERT_SOURCE_LOCATION (t);
6143 case TRAIT_EXPR:
6144 return TRAIT_EXPR_LOCATION (t);
6145 case PTRMEM_CST:
6146 return PTRMEM_CST_LOCATION (t);
6147 default:
6148 return EXPR_LOCATION (t);
6152 /* Implement -Wzero_as_null_pointer_constant. Return true if the
6153 conditions for the warning hold, false otherwise. */
6154 bool
6155 maybe_warn_zero_as_null_pointer_constant (tree expr, location_t loc)
6157 if (c_inhibit_evaluation_warnings == 0
6158 && !null_node_p (expr) && !NULLPTR_TYPE_P (TREE_TYPE (expr)))
6160 warning_at (loc, OPT_Wzero_as_null_pointer_constant,
6161 "zero as null pointer constant");
6162 return true;
6164 return false;
6167 /* FNDECL is a function declaration whose type may have been altered by
6168 adding extra parameters such as this, in-charge, or VTT. When this
6169 takes place, the positional arguments supplied by the user (as in the
6170 'format' attribute arguments) may refer to the wrong argument. This
6171 function returns an integer indicating how many arguments should be
6172 skipped. */
6175 maybe_adjust_arg_pos_for_attribute (const_tree fndecl)
6177 if (!fndecl)
6178 return 0;
6179 int n = num_artificial_parms_for (fndecl);
6180 /* The manual states that it's the user's responsibility to account
6181 for the implicit this parameter. */
6182 return n > 0 ? n - 1 : 0;
6186 /* Release memory we no longer need after parsing. */
6187 void
6188 cp_tree_c_finish_parsing ()
6190 if (previous_class_level)
6191 invalidate_class_lookup_cache ();
6192 deleted_copy_types = NULL;
6195 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
6196 /* Complain that some language-specific thing hanging off a tree
6197 node has been accessed improperly. */
6199 void
6200 lang_check_failed (const char* file, int line, const char* function)
6202 internal_error ("%<lang_*%> check: failed in %s, at %s:%d",
6203 function, trim_filename (file), line);
6205 #endif /* ENABLE_TREE_CHECKING */
6207 #if CHECKING_P
6209 namespace selftest {
6211 /* Verify that lvalue_kind () works, for various expressions,
6212 and that location wrappers don't affect the results. */
6214 static void
6215 test_lvalue_kind ()
6217 location_t loc = BUILTINS_LOCATION;
6219 /* Verify constants and parameters, without and with
6220 location wrappers. */
6221 tree int_cst = build_int_cst (integer_type_node, 42);
6222 ASSERT_EQ (clk_none, lvalue_kind (int_cst));
6224 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
6225 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
6226 ASSERT_EQ (clk_none, lvalue_kind (wrapped_int_cst));
6228 tree string_lit = build_string (4, "foo");
6229 TREE_TYPE (string_lit) = char_array_type_node;
6230 string_lit = fix_string_type (string_lit);
6231 ASSERT_EQ (clk_ordinary, lvalue_kind (string_lit));
6233 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
6234 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
6235 ASSERT_EQ (clk_ordinary, lvalue_kind (wrapped_string_lit));
6237 tree parm = build_decl (UNKNOWN_LOCATION, PARM_DECL,
6238 get_identifier ("some_parm"),
6239 integer_type_node);
6240 ASSERT_EQ (clk_ordinary, lvalue_kind (parm));
6242 tree wrapped_parm = maybe_wrap_with_location (parm, loc);
6243 ASSERT_TRUE (location_wrapper_p (wrapped_parm));
6244 ASSERT_EQ (clk_ordinary, lvalue_kind (wrapped_parm));
6246 /* Verify that lvalue_kind of std::move on a parm isn't
6247 affected by location wrappers. */
6248 tree rvalue_ref_of_parm = move (parm);
6249 ASSERT_EQ (clk_rvalueref, lvalue_kind (rvalue_ref_of_parm));
6250 tree rvalue_ref_of_wrapped_parm = move (wrapped_parm);
6251 ASSERT_EQ (clk_rvalueref, lvalue_kind (rvalue_ref_of_wrapped_parm));
6253 /* Verify lvalue_p. */
6254 ASSERT_FALSE (lvalue_p (int_cst));
6255 ASSERT_FALSE (lvalue_p (wrapped_int_cst));
6256 ASSERT_TRUE (lvalue_p (parm));
6257 ASSERT_TRUE (lvalue_p (wrapped_parm));
6258 ASSERT_FALSE (lvalue_p (rvalue_ref_of_parm));
6259 ASSERT_FALSE (lvalue_p (rvalue_ref_of_wrapped_parm));
6262 /* Run all of the selftests within this file. */
6264 void
6265 cp_tree_cc_tests ()
6267 test_lvalue_kind ();
6270 } // namespace selftest
6272 #endif /* #if CHECKING_P */
6275 #include "gt-cp-tree.h"