Require target lra in gcc.c-torture/compile/asmgoto-6.c
[official-gcc.git] / gcc / cp / tree.cc
blob799183dc64600cc4428be354b02955f2be1765f4
1 /* Language-dependent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2023 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 *);
49 static tree handle_contract_attribute (tree *, tree, tree, int, bool *);
51 /* If REF is an lvalue, returns the kind of lvalue that REF is.
52 Otherwise, returns clk_none. */
54 cp_lvalue_kind
55 lvalue_kind (const_tree ref)
57 cp_lvalue_kind op1_lvalue_kind = clk_none;
58 cp_lvalue_kind op2_lvalue_kind = clk_none;
60 /* Expressions of reference type are sometimes wrapped in
61 INDIRECT_REFs. INDIRECT_REFs are just internal compiler
62 representation, not part of the language, so we have to look
63 through them. */
64 if (REFERENCE_REF_P (ref))
65 return lvalue_kind (TREE_OPERAND (ref, 0));
67 if (TREE_TYPE (ref)
68 && TYPE_REF_P (TREE_TYPE (ref)))
70 /* unnamed rvalue references are rvalues */
71 if (TYPE_REF_IS_RVALUE (TREE_TYPE (ref))
72 && TREE_CODE (ref) != PARM_DECL
73 && !VAR_P (ref)
74 && TREE_CODE (ref) != COMPONENT_REF
75 /* Functions are always lvalues. */
76 && TREE_CODE (TREE_TYPE (TREE_TYPE (ref))) != FUNCTION_TYPE)
78 op1_lvalue_kind = clk_rvalueref;
79 if (implicit_rvalue_p (ref))
80 op1_lvalue_kind |= clk_implicit_rval;
81 return op1_lvalue_kind;
84 /* lvalue references and named rvalue references are lvalues. */
85 return clk_ordinary;
88 if (ref == current_class_ptr)
89 return clk_none;
91 /* Expressions with cv void type are prvalues. */
92 if (TREE_TYPE (ref) && VOID_TYPE_P (TREE_TYPE (ref)))
93 return clk_none;
95 switch (TREE_CODE (ref))
97 case SAVE_EXPR:
98 return clk_none;
100 /* preincrements and predecrements are valid lvals, provided
101 what they refer to are valid lvals. */
102 case PREINCREMENT_EXPR:
103 case PREDECREMENT_EXPR:
104 case TRY_CATCH_EXPR:
105 case REALPART_EXPR:
106 case IMAGPART_EXPR:
107 case VIEW_CONVERT_EXPR:
108 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
109 /* As for ARRAY_REF and COMPONENT_REF, these codes turn a class prvalue
110 into an xvalue: we need to materialize the temporary before we mess
111 with it. Except VIEW_CONVERT_EXPR that doesn't actually change the
112 type, as in location wrapper and REF_PARENTHESIZED_P. */
113 if (op1_lvalue_kind == clk_class
114 && !(TREE_CODE (ref) == VIEW_CONVERT_EXPR
115 && (same_type_ignoring_top_level_qualifiers_p
116 (TREE_TYPE (ref), TREE_TYPE (TREE_OPERAND (ref, 0))))))
117 return clk_rvalueref;
118 return op1_lvalue_kind;
120 case ARRAY_REF:
122 tree op1 = TREE_OPERAND (ref, 0);
123 if (TREE_CODE (TREE_TYPE (op1)) == ARRAY_TYPE)
125 op1_lvalue_kind = lvalue_kind (op1);
126 if (op1_lvalue_kind == clk_class)
127 /* in the case of an array operand, the result is an lvalue if
128 that operand is an lvalue and an xvalue otherwise */
129 op1_lvalue_kind = clk_rvalueref;
130 return op1_lvalue_kind;
132 else
133 return clk_ordinary;
136 case MEMBER_REF:
137 case DOTSTAR_EXPR:
138 if (TREE_CODE (ref) == MEMBER_REF)
139 op1_lvalue_kind = clk_ordinary;
140 else
141 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
142 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
143 op1_lvalue_kind = clk_none;
144 else if (op1_lvalue_kind == clk_class)
145 /* The result of a .* expression whose second operand is a pointer to a
146 data member is an lvalue if the first operand is an lvalue and an
147 xvalue otherwise. */
148 op1_lvalue_kind = clk_rvalueref;
149 return op1_lvalue_kind;
151 case COMPONENT_REF:
152 if (BASELINK_P (TREE_OPERAND (ref, 1)))
154 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (ref, 1));
156 /* For static member function recurse on the BASELINK, we can get
157 here e.g. from reference_binding. If BASELINK_FUNCTIONS is
158 OVERLOAD, the overload is resolved first if possible through
159 resolve_address_of_overloaded_function. */
160 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_STATIC_FUNCTION_P (fn))
161 return lvalue_kind (TREE_OPERAND (ref, 1));
163 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
164 if (op1_lvalue_kind == clk_class)
165 /* If E1 is an lvalue, then E1.E2 is an lvalue;
166 otherwise E1.E2 is an xvalue. */
167 op1_lvalue_kind = clk_rvalueref;
169 /* Look at the member designator. */
170 if (!op1_lvalue_kind)
172 else if (is_overloaded_fn (TREE_OPERAND (ref, 1)))
173 /* The "field" can be a FUNCTION_DECL or an OVERLOAD in some
174 situations. If we're seeing a COMPONENT_REF, it's a non-static
175 member, so it isn't an lvalue. */
176 op1_lvalue_kind = clk_none;
177 else if (TREE_CODE (TREE_OPERAND (ref, 1)) != FIELD_DECL)
178 /* This can be IDENTIFIER_NODE in a template. */;
179 else if (DECL_C_BIT_FIELD (TREE_OPERAND (ref, 1)))
181 /* Clear the ordinary bit. If this object was a class
182 rvalue we want to preserve that information. */
183 op1_lvalue_kind &= ~clk_ordinary;
184 /* The lvalue is for a bitfield. */
185 op1_lvalue_kind |= clk_bitfield;
187 else if (DECL_PACKED (TREE_OPERAND (ref, 1)))
188 op1_lvalue_kind |= clk_packed;
190 return op1_lvalue_kind;
192 case STRING_CST:
193 case COMPOUND_LITERAL_EXPR:
194 return clk_ordinary;
196 case CONST_DECL:
197 /* CONST_DECL without TREE_STATIC are enumeration values and
198 thus not lvalues. With TREE_STATIC they are used by ObjC++
199 in objc_build_string_object and need to be considered as
200 lvalues. */
201 if (! TREE_STATIC (ref))
202 return clk_none;
203 /* FALLTHRU */
204 case VAR_DECL:
205 if (VAR_P (ref) && DECL_HAS_VALUE_EXPR_P (ref))
206 return lvalue_kind (DECL_VALUE_EXPR (CONST_CAST_TREE (ref)));
208 if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
209 && DECL_LANG_SPECIFIC (ref)
210 && DECL_IN_AGGR_P (ref))
211 return clk_none;
212 /* FALLTHRU */
213 case INDIRECT_REF:
214 case ARROW_EXPR:
215 case PARM_DECL:
216 case RESULT_DECL:
217 case PLACEHOLDER_EXPR:
218 return clk_ordinary;
220 /* A scope ref in a template, left as SCOPE_REF to support later
221 access checking. */
222 case SCOPE_REF:
223 gcc_assert (!type_dependent_expression_p (CONST_CAST_TREE (ref)));
225 tree op = TREE_OPERAND (ref, 1);
226 if (TREE_CODE (op) == FIELD_DECL)
227 return (DECL_C_BIT_FIELD (op) ? clk_bitfield : clk_ordinary);
228 else
229 return lvalue_kind (op);
232 case MAX_EXPR:
233 case MIN_EXPR:
234 /* Disallow <? and >? as lvalues if either argument side-effects. */
235 if (TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 0))
236 || TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 1)))
237 return clk_none;
238 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
239 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1));
240 break;
242 case COND_EXPR:
243 if (processing_template_decl)
245 /* Within templates, a REFERENCE_TYPE will indicate whether
246 the COND_EXPR result is an ordinary lvalue or rvalueref.
247 Since REFERENCE_TYPEs are handled above, if we reach this
248 point, we know we got a plain rvalue. Unless we have a
249 type-dependent expr, that is, but we shouldn't be testing
250 lvalueness if we can't even tell the types yet! */
251 gcc_assert (!type_dependent_expression_p (CONST_CAST_TREE (ref)));
252 goto default_;
255 tree op1 = TREE_OPERAND (ref, 1);
256 if (!op1) op1 = TREE_OPERAND (ref, 0);
257 tree op2 = TREE_OPERAND (ref, 2);
258 op1_lvalue_kind = lvalue_kind (op1);
259 op2_lvalue_kind = lvalue_kind (op2);
260 if (!op1_lvalue_kind != !op2_lvalue_kind)
262 /* The second or the third operand (but not both) is a
263 throw-expression; the result is of the type
264 and value category of the other. */
265 if (op1_lvalue_kind && TREE_CODE (op2) == THROW_EXPR)
266 op2_lvalue_kind = op1_lvalue_kind;
267 else if (op2_lvalue_kind && TREE_CODE (op1) == THROW_EXPR)
268 op1_lvalue_kind = op2_lvalue_kind;
271 break;
273 case MODOP_EXPR:
274 /* We expect to see unlowered MODOP_EXPRs only during
275 template processing. */
276 gcc_assert (processing_template_decl);
277 return clk_ordinary;
279 case MODIFY_EXPR:
280 case TYPEID_EXPR:
281 return clk_ordinary;
283 case COMPOUND_EXPR:
284 return lvalue_kind (TREE_OPERAND (ref, 1));
286 case TARGET_EXPR:
287 return clk_class;
289 case VA_ARG_EXPR:
290 return (CLASS_TYPE_P (TREE_TYPE (ref)) ? clk_class : clk_none);
292 case CALL_EXPR:
293 /* We can see calls outside of TARGET_EXPR in templates. */
294 if (CLASS_TYPE_P (TREE_TYPE (ref)))
295 return clk_class;
296 return clk_none;
298 case FUNCTION_DECL:
299 /* All functions (except non-static-member functions) are
300 lvalues. */
301 return (DECL_NONSTATIC_MEMBER_FUNCTION_P (ref)
302 ? clk_none : clk_ordinary);
304 case BASELINK:
305 /* We now represent a reference to a single static member function
306 with a BASELINK. */
307 /* This CONST_CAST is okay because BASELINK_FUNCTIONS returns
308 its argument unmodified and we assign it to a const_tree. */
309 return lvalue_kind (BASELINK_FUNCTIONS (CONST_CAST_TREE (ref)));
311 case NON_DEPENDENT_EXPR:
312 case PAREN_EXPR:
313 return lvalue_kind (TREE_OPERAND (ref, 0));
315 case TEMPLATE_PARM_INDEX:
316 if (CLASS_TYPE_P (TREE_TYPE (ref)))
317 /* A template parameter object is an lvalue. */
318 return clk_ordinary;
319 return clk_none;
321 default:
322 default_:
323 if (!TREE_TYPE (ref))
324 return clk_none;
325 if (CLASS_TYPE_P (TREE_TYPE (ref))
326 || TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE)
327 return clk_class;
328 return clk_none;
331 /* If one operand is not an lvalue at all, then this expression is
332 not an lvalue. */
333 if (!op1_lvalue_kind || !op2_lvalue_kind)
334 return clk_none;
336 /* Otherwise, it's an lvalue, and it has all the odd properties
337 contributed by either operand. */
338 op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind;
339 /* It's not an ordinary lvalue if it involves any other kind. */
340 if ((op1_lvalue_kind & ~clk_ordinary) != clk_none)
341 op1_lvalue_kind &= ~clk_ordinary;
342 /* It can't be both a pseudo-lvalue and a non-addressable lvalue.
343 A COND_EXPR of those should be wrapped in a TARGET_EXPR. */
344 if ((op1_lvalue_kind & (clk_rvalueref|clk_class))
345 && (op1_lvalue_kind & (clk_bitfield|clk_packed)))
346 op1_lvalue_kind = clk_none;
347 return op1_lvalue_kind;
350 /* Returns the kind of lvalue that REF is, in the sense of [basic.lval]. */
352 cp_lvalue_kind
353 real_lvalue_p (const_tree ref)
355 cp_lvalue_kind kind = lvalue_kind (ref);
356 if (kind & (clk_rvalueref|clk_class))
357 return clk_none;
358 else
359 return kind;
362 /* c-common wants us to return bool. */
364 bool
365 lvalue_p (const_tree t)
367 return real_lvalue_p (t);
370 /* This differs from lvalue_p in that xvalues are included. */
372 bool
373 glvalue_p (const_tree ref)
375 cp_lvalue_kind kind = lvalue_kind (ref);
376 if (kind & clk_class)
377 return false;
378 else
379 return (kind != clk_none);
382 /* This differs from glvalue_p in that class prvalues are included. */
384 bool
385 obvalue_p (const_tree ref)
387 return (lvalue_kind (ref) != clk_none);
390 /* Returns true if REF is an xvalue (the result of dereferencing an rvalue
391 reference), false otherwise. */
393 bool
394 xvalue_p (const_tree ref)
396 return (lvalue_kind (ref) & clk_rvalueref);
399 /* True if REF is a bit-field. */
401 bool
402 bitfield_p (const_tree ref)
404 return (lvalue_kind (ref) & clk_bitfield);
407 /* C++-specific version of stabilize_reference. */
409 tree
410 cp_stabilize_reference (tree ref)
412 STRIP_ANY_LOCATION_WRAPPER (ref);
413 switch (TREE_CODE (ref))
415 case NON_DEPENDENT_EXPR:
416 /* We aren't actually evaluating this. */
417 return ref;
419 /* We need to treat specially anything stabilize_reference doesn't
420 handle specifically. */
421 case VAR_DECL:
422 case PARM_DECL:
423 case RESULT_DECL:
424 CASE_CONVERT:
425 case FLOAT_EXPR:
426 case FIX_TRUNC_EXPR:
427 case INDIRECT_REF:
428 case COMPONENT_REF:
429 case BIT_FIELD_REF:
430 case ARRAY_REF:
431 case ARRAY_RANGE_REF:
432 case ERROR_MARK:
433 break;
434 default:
435 cp_lvalue_kind kind = lvalue_kind (ref);
436 if ((kind & ~clk_class) != clk_none)
438 tree type = unlowered_expr_type (ref);
439 bool rval = !!(kind & clk_rvalueref);
440 type = cp_build_reference_type (type, rval);
441 /* This inhibits warnings in, eg, cxx_mark_addressable
442 (c++/60955). */
443 warning_sentinel s (extra_warnings);
444 ref = build_static_cast (input_location, type, ref,
445 tf_error);
449 return stabilize_reference (ref);
452 /* Test whether DECL is a builtin that may appear in a
453 constant-expression. */
455 bool
456 builtin_valid_in_constant_expr_p (const_tree decl)
458 STRIP_ANY_LOCATION_WRAPPER (decl);
459 if (TREE_CODE (decl) != FUNCTION_DECL)
460 /* Not a function. */
461 return false;
462 if (DECL_BUILT_IN_CLASS (decl) != BUILT_IN_NORMAL)
464 if (fndecl_built_in_p (decl, BUILT_IN_FRONTEND))
465 switch (DECL_FE_FUNCTION_CODE (decl))
467 case CP_BUILT_IN_IS_CONSTANT_EVALUATED:
468 case CP_BUILT_IN_SOURCE_LOCATION:
469 case CP_BUILT_IN_IS_CORRESPONDING_MEMBER:
470 case CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS:
471 return true;
472 default:
473 break;
475 /* Not a built-in. */
476 return false;
478 switch (DECL_FUNCTION_CODE (decl))
480 /* These always have constant results like the corresponding
481 macros/symbol. */
482 case BUILT_IN_FILE:
483 case BUILT_IN_FUNCTION:
484 case BUILT_IN_LINE:
486 /* The following built-ins are valid in constant expressions
487 when their arguments are. */
488 case BUILT_IN_ADD_OVERFLOW_P:
489 case BUILT_IN_SUB_OVERFLOW_P:
490 case BUILT_IN_MUL_OVERFLOW_P:
492 /* These have constant results even if their operands are
493 non-constant. */
494 case BUILT_IN_CONSTANT_P:
495 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
496 return true;
497 default:
498 return false;
502 /* Build a TARGET_EXPR, initializing the DECL with the VALUE. */
504 static tree
505 build_target_expr (tree decl, tree value, tsubst_flags_t complain)
507 tree t;
508 tree type = TREE_TYPE (decl);
510 value = mark_rvalue_use (value);
512 gcc_checking_assert (VOID_TYPE_P (TREE_TYPE (value))
513 || TREE_TYPE (decl) == TREE_TYPE (value)
514 /* On ARM ctors return 'this'. */
515 || (TYPE_PTR_P (TREE_TYPE (value))
516 && TREE_CODE (value) == CALL_EXPR)
517 || useless_type_conversion_p (TREE_TYPE (decl),
518 TREE_TYPE (value)));
520 /* Set TREE_READONLY for optimization, such as gimplify_init_constructor
521 moving a constant aggregate into .rodata. */
522 if (CP_TYPE_CONST_NON_VOLATILE_P (type)
523 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
524 && !VOID_TYPE_P (TREE_TYPE (value))
525 && !TYPE_HAS_MUTABLE_P (type)
526 && reduced_constant_expression_p (value))
527 TREE_READONLY (decl) = true;
529 if (complain & tf_no_cleanup)
530 /* The caller is building a new-expr and does not need a cleanup. */
531 t = NULL_TREE;
532 else
534 t = cxx_maybe_build_cleanup (decl, complain);
535 if (t == error_mark_node)
536 return error_mark_node;
539 set_target_expr_eliding (value);
541 t = build4 (TARGET_EXPR, type, decl, value, t, NULL_TREE);
542 if (location_t eloc = cp_expr_location (value))
543 SET_EXPR_LOCATION (t, eloc);
544 /* We always set TREE_SIDE_EFFECTS so that expand_expr does not
545 ignore the TARGET_EXPR. If there really turn out to be no
546 side-effects, then the optimizer should be able to get rid of
547 whatever code is generated anyhow. */
548 TREE_SIDE_EFFECTS (t) = 1;
550 return t;
553 /* Return an undeclared local temporary of type TYPE for use in building a
554 TARGET_EXPR. */
556 tree
557 build_local_temp (tree type)
559 tree slot = build_decl (input_location,
560 VAR_DECL, NULL_TREE, type);
561 DECL_ARTIFICIAL (slot) = 1;
562 DECL_IGNORED_P (slot) = 1;
563 DECL_CONTEXT (slot) = current_function_decl;
564 layout_decl (slot, 0);
565 return slot;
568 /* Return whether DECL is such a local temporary (or one from
569 create_tmp_var_raw). */
571 bool
572 is_local_temp (tree decl)
574 return (VAR_P (decl) && DECL_ARTIFICIAL (decl)
575 && !TREE_STATIC (decl));
578 /* Set various status flags when building an AGGR_INIT_EXPR object T. */
580 static void
581 process_aggr_init_operands (tree t)
583 bool side_effects;
585 side_effects = TREE_SIDE_EFFECTS (t);
586 if (!side_effects)
588 int i, n;
589 n = TREE_OPERAND_LENGTH (t);
590 for (i = 1; i < n; i++)
592 tree op = TREE_OPERAND (t, i);
593 if (op && TREE_SIDE_EFFECTS (op))
595 side_effects = 1;
596 break;
600 TREE_SIDE_EFFECTS (t) = side_effects;
603 /* Build an AGGR_INIT_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE,
604 FN, and SLOT. NARGS is the number of call arguments which are specified
605 as a tree array ARGS. */
607 static tree
608 build_aggr_init_array (tree return_type, tree fn, tree slot, int nargs,
609 tree *args)
611 tree t;
612 int i;
614 t = build_vl_exp (AGGR_INIT_EXPR, nargs + 3);
615 TREE_TYPE (t) = return_type;
616 AGGR_INIT_EXPR_FN (t) = fn;
617 AGGR_INIT_EXPR_SLOT (t) = slot;
618 for (i = 0; i < nargs; i++)
619 AGGR_INIT_EXPR_ARG (t, i) = args[i];
620 process_aggr_init_operands (t);
621 return t;
624 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
625 target. TYPE is the type to be initialized.
627 Build an AGGR_INIT_EXPR to represent the initialization. This function
628 differs from build_cplus_new in that an AGGR_INIT_EXPR can only be used
629 to initialize another object, whereas a TARGET_EXPR can either
630 initialize another object or create its own temporary object, and as a
631 result building up a TARGET_EXPR requires that the type's destructor be
632 callable. */
634 tree
635 build_aggr_init_expr (tree type, tree init)
637 tree fn;
638 tree slot;
639 tree rval;
640 int is_ctor;
642 gcc_assert (!VOID_TYPE_P (type));
644 /* Don't build AGGR_INIT_EXPR in a template. */
645 if (processing_template_decl)
646 return init;
648 fn = cp_get_callee (init);
649 if (fn == NULL_TREE)
650 return convert (type, init);
652 is_ctor = (TREE_CODE (fn) == ADDR_EXPR
653 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
654 && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0)));
656 /* We split the CALL_EXPR into its function and its arguments here.
657 Then, in expand_expr, we put them back together. The reason for
658 this is that this expression might be a default argument
659 expression. In that case, we need a new temporary every time the
660 expression is used. That's what break_out_target_exprs does; it
661 replaces every AGGR_INIT_EXPR with a copy that uses a fresh
662 temporary slot. Then, expand_expr builds up a call-expression
663 using the new slot. */
665 /* If we don't need to use a constructor to create an object of this
666 type, don't mess with AGGR_INIT_EXPR. */
667 if (is_ctor || TREE_ADDRESSABLE (type))
669 slot = build_local_temp (type);
671 if (TREE_CODE (init) == CALL_EXPR)
673 rval = build_aggr_init_array (void_type_node, fn, slot,
674 call_expr_nargs (init),
675 CALL_EXPR_ARGP (init));
676 AGGR_INIT_FROM_THUNK_P (rval)
677 = CALL_FROM_THUNK_P (init);
679 else
681 rval = build_aggr_init_array (void_type_node, fn, slot,
682 aggr_init_expr_nargs (init),
683 AGGR_INIT_EXPR_ARGP (init));
684 AGGR_INIT_FROM_THUNK_P (rval)
685 = AGGR_INIT_FROM_THUNK_P (init);
687 TREE_SIDE_EFFECTS (rval) = 1;
688 AGGR_INIT_VIA_CTOR_P (rval) = is_ctor;
689 TREE_NOTHROW (rval) = TREE_NOTHROW (init);
690 CALL_EXPR_OPERATOR_SYNTAX (rval) = CALL_EXPR_OPERATOR_SYNTAX (init);
691 CALL_EXPR_ORDERED_ARGS (rval) = CALL_EXPR_ORDERED_ARGS (init);
692 CALL_EXPR_REVERSE_ARGS (rval) = CALL_EXPR_REVERSE_ARGS (init);
694 else
695 rval = init;
697 return rval;
700 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
701 target. TYPE is the type that this initialization should appear to
702 have.
704 Build an encapsulation of the initialization to perform
705 and return it so that it can be processed by language-independent
706 and language-specific expression expanders. */
708 tree
709 build_cplus_new (tree type, tree init, tsubst_flags_t complain)
711 /* This function should cope with what build_special_member_call
712 can produce. When performing parenthesized aggregate initialization,
713 it can produce a { }. */
714 if (BRACE_ENCLOSED_INITIALIZER_P (init))
716 gcc_assert (cxx_dialect >= cxx20);
717 return finish_compound_literal (type, init, complain);
720 tree rval = build_aggr_init_expr (type, init);
721 tree slot;
723 if (init == error_mark_node)
724 return error_mark_node;
726 if (!complete_type_or_maybe_complain (type, init, complain))
727 return error_mark_node;
729 /* Make sure that we're not trying to create an instance of an
730 abstract class. */
731 if (abstract_virtuals_error (NULL_TREE, type, complain))
732 return error_mark_node;
734 if (TREE_CODE (rval) == AGGR_INIT_EXPR)
735 slot = AGGR_INIT_EXPR_SLOT (rval);
736 else if (TREE_CODE (rval) == CALL_EXPR
737 || TREE_CODE (rval) == CONSTRUCTOR)
738 slot = build_local_temp (type);
739 else
740 return rval;
742 rval = build_target_expr (slot, rval, complain);
744 if (rval != error_mark_node)
745 TARGET_EXPR_IMPLICIT_P (rval) = 1;
747 return rval;
750 /* Subroutine of build_vec_init_expr: Build up a single element
751 intialization as a proxy for the full array initialization to get things
752 marked as used and any appropriate diagnostics.
754 This used to be necessary because we were deferring building the actual
755 constructor calls until gimplification time; now we only do it to set
756 VEC_INIT_EXPR_IS_CONSTEXPR.
758 We assume that init is either NULL_TREE, {}, void_type_node (indicating
759 value-initialization), or another array to copy. */
761 static tree
762 build_vec_init_elt (tree type, tree init, tsubst_flags_t complain)
764 tree inner_type = strip_array_types (type);
766 if (integer_zerop (array_type_nelts_total (type))
767 || !CLASS_TYPE_P (inner_type))
768 /* No interesting initialization to do. */
769 return integer_zero_node;
770 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
772 /* Even if init has initializers for some array elements,
773 we're interested in the {}-init of trailing elements. */
774 if (CP_AGGREGATE_TYPE_P (inner_type))
776 tree empty = build_constructor (init_list_type_node, nullptr);
777 return digest_init (inner_type, empty, complain);
779 else
780 /* It's equivalent to value-init. */
781 init = void_type_node;
783 if (init == void_type_node)
784 return build_value_init (inner_type, complain);
786 releasing_vec argvec;
787 if (init && !BRACE_ENCLOSED_INITIALIZER_P (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 (CAN_HAVE_LOCATION_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). If T is not a type, then this
1565 just dispatches to strip_typedefs_expr.
1567 E.g. consider the following declarations:
1568 typedef const int ConstInt;
1569 typedef ConstInt* PtrConstInt;
1570 If T is PtrConstInt, this function returns a type representing
1571 const int*.
1572 In other words, if T is a typedef, the function returns the underlying type.
1573 The cv-qualification and attributes of the type returned match the
1574 input type.
1575 They will always be compatible types.
1576 The returned type is built so that all of its subtypes
1577 recursively have their typedefs stripped as well.
1579 This is different from just returning TYPE_CANONICAL (T)
1580 Because of several reasons:
1581 * If T is a type that needs structural equality
1582 its TYPE_CANONICAL (T) will be NULL.
1583 * TYPE_CANONICAL (T) desn't carry type attributes
1584 and loses template parameter names.
1586 If REMOVE_ATTRIBUTES is non-null, also strip attributes that don't
1587 affect type identity, and set the referent to true if any were
1588 stripped. */
1590 tree
1591 strip_typedefs (tree t, bool *remove_attributes /* = NULL */,
1592 unsigned int flags /* = 0 */)
1594 tree result = NULL, type = NULL, t0 = NULL;
1596 if (!t || t == error_mark_node)
1597 return t;
1599 if (!TYPE_P (t))
1600 return strip_typedefs_expr (t, remove_attributes, flags);
1602 if (t == TYPE_CANONICAL (t))
1603 return t;
1605 if (!(flags & STF_STRIP_DEPENDENT)
1606 && dependent_alias_template_spec_p (t, nt_opaque))
1607 /* DR 1558: However, if the template-id is dependent, subsequent
1608 template argument substitution still applies to the template-id. */
1609 return t;
1611 switch (TREE_CODE (t))
1613 case POINTER_TYPE:
1614 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1615 result = build_pointer_type_for_mode (type, TYPE_MODE (t), false);
1616 break;
1617 case REFERENCE_TYPE:
1618 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1619 result = cp_build_reference_type_for_mode (type, TYPE_MODE (t), TYPE_REF_IS_RVALUE (t));
1620 break;
1621 case OFFSET_TYPE:
1622 t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t), remove_attributes, flags);
1623 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1624 result = build_offset_type (t0, type);
1625 break;
1626 case RECORD_TYPE:
1627 if (TYPE_PTRMEMFUNC_P (t))
1629 t0 = strip_typedefs (TYPE_PTRMEMFUNC_FN_TYPE (t),
1630 remove_attributes, flags);
1631 result = build_ptrmemfunc_type (t0);
1633 break;
1634 case ARRAY_TYPE:
1635 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1636 t0 = strip_typedefs (TYPE_DOMAIN (t), remove_attributes, flags);
1637 gcc_checking_assert (TYPE_DEPENDENT_P_VALID (t)
1638 || !dependent_type_p (t));
1639 result = build_cplus_array_type (type, t0, TYPE_DEPENDENT_P (t));
1640 break;
1641 case FUNCTION_TYPE:
1642 case METHOD_TYPE:
1644 tree arg_types = NULL, arg_node, arg_node2, arg_type;
1645 bool changed;
1647 /* Because we stomp on TREE_PURPOSE of TYPE_ARG_TYPES in many places
1648 around the compiler (e.g. cp_parser_late_parsing_default_args), we
1649 can't expect that re-hashing a function type will find a previous
1650 equivalent type, so try to reuse the input type if nothing has
1651 changed. If the type is itself a variant, that will change. */
1652 bool is_variant = typedef_variant_p (t);
1653 if (remove_attributes
1654 && (TYPE_ATTRIBUTES (t) || TYPE_USER_ALIGN (t)))
1655 is_variant = true;
1657 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1658 tree canon_spec = (flag_noexcept_type
1659 ? canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (t))
1660 : NULL_TREE);
1661 changed = (type != TREE_TYPE (t) || is_variant
1662 || TYPE_RAISES_EXCEPTIONS (t) != canon_spec);
1664 for (arg_node = TYPE_ARG_TYPES (t);
1665 arg_node;
1666 arg_node = TREE_CHAIN (arg_node))
1668 if (arg_node == void_list_node)
1669 break;
1670 arg_type = strip_typedefs (TREE_VALUE (arg_node),
1671 remove_attributes, flags);
1672 gcc_assert (arg_type);
1673 if (arg_type == TREE_VALUE (arg_node) && !changed)
1674 continue;
1676 if (!changed)
1678 changed = true;
1679 for (arg_node2 = TYPE_ARG_TYPES (t);
1680 arg_node2 != arg_node;
1681 arg_node2 = TREE_CHAIN (arg_node2))
1682 arg_types
1683 = tree_cons (TREE_PURPOSE (arg_node2),
1684 TREE_VALUE (arg_node2), arg_types);
1687 arg_types
1688 = tree_cons (TREE_PURPOSE (arg_node), arg_type, arg_types);
1691 if (!changed)
1692 return t;
1694 if (arg_types)
1695 arg_types = nreverse (arg_types);
1697 /* A list of parameters not ending with an ellipsis
1698 must end with void_list_node. */
1699 if (arg_node)
1700 arg_types = chainon (arg_types, void_list_node);
1702 if (TREE_CODE (t) == METHOD_TYPE)
1704 tree class_type = TREE_TYPE (TREE_VALUE (arg_types));
1705 gcc_assert (class_type);
1706 result =
1707 build_method_type_directly (class_type, type,
1708 TREE_CHAIN (arg_types));
1710 else
1712 result = build_function_type (type, arg_types);
1713 result = apply_memfn_quals (result, type_memfn_quals (t));
1716 result = build_cp_fntype_variant (result,
1717 type_memfn_rqual (t), canon_spec,
1718 TYPE_HAS_LATE_RETURN_TYPE (t));
1720 break;
1721 case TYPENAME_TYPE:
1723 bool changed = false;
1724 tree fullname = TYPENAME_TYPE_FULLNAME (t);
1725 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
1726 && TREE_OPERAND (fullname, 1))
1728 tree args = TREE_OPERAND (fullname, 1);
1729 tree new_args = copy_node (args);
1730 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
1732 tree arg = TREE_VEC_ELT (args, i);
1733 tree strip_arg = strip_typedefs (arg, remove_attributes, flags);
1734 TREE_VEC_ELT (new_args, i) = strip_arg;
1735 if (strip_arg != arg)
1736 changed = true;
1738 if (changed)
1740 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_args)
1741 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
1742 fullname
1743 = lookup_template_function (TREE_OPERAND (fullname, 0),
1744 new_args);
1746 else
1747 ggc_free (new_args);
1749 tree ctx = strip_typedefs (TYPE_CONTEXT (t), remove_attributes, flags);
1750 if (!changed && ctx == TYPE_CONTEXT (t) && !typedef_variant_p (t))
1751 return t;
1752 tree name = fullname;
1753 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
1754 name = TREE_OPERAND (fullname, 0);
1755 /* Use build_typename_type rather than make_typename_type because we
1756 don't want to resolve it here, just strip typedefs. */
1757 result = build_typename_type (ctx, name, fullname, typename_type);
1759 break;
1760 case DECLTYPE_TYPE:
1761 result = strip_typedefs_expr (DECLTYPE_TYPE_EXPR (t),
1762 remove_attributes, flags);
1763 if (result == DECLTYPE_TYPE_EXPR (t))
1764 result = NULL_TREE;
1765 else
1766 result = (finish_decltype_type
1767 (result,
1768 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t),
1769 tf_none));
1770 break;
1771 case TRAIT_TYPE:
1773 tree type1 = strip_typedefs (TRAIT_TYPE_TYPE1 (t),
1774 remove_attributes, flags);
1775 tree type2 = strip_typedefs (TRAIT_TYPE_TYPE2 (t),
1776 remove_attributes, flags);
1777 if (type1 == TRAIT_TYPE_TYPE1 (t) && type2 == TRAIT_TYPE_TYPE2 (t))
1778 result = NULL_TREE;
1779 else
1780 result = finish_trait_type (TRAIT_TYPE_KIND (t), type1, type2,
1781 tf_warning_or_error);
1783 break;
1784 case TYPE_PACK_EXPANSION:
1786 tree pat = PACK_EXPANSION_PATTERN (t);
1787 if (TYPE_P (pat))
1789 type = strip_typedefs (pat, remove_attributes, flags);
1790 if (type != pat)
1792 result = build_distinct_type_copy (t);
1793 PACK_EXPANSION_PATTERN (result) = type;
1797 break;
1798 default:
1799 break;
1802 if (!result)
1804 if (typedef_variant_p (t))
1806 if ((flags & STF_USER_VISIBLE)
1807 && !user_facing_original_type_p (t))
1808 return t;
1809 /* If T is a non-template alias or typedef, we can assume that
1810 instantiating its definition will hit any substitution failure,
1811 so we don't need to retain it here as well. */
1812 if (!alias_template_specialization_p (t, nt_opaque))
1813 flags |= STF_STRIP_DEPENDENT;
1814 result = strip_typedefs (DECL_ORIGINAL_TYPE (TYPE_NAME (t)),
1815 remove_attributes, flags);
1817 else
1818 result = TYPE_MAIN_VARIANT (t);
1820 /*gcc_assert (!typedef_variant_p (result)
1821 || dependent_alias_template_spec_p (result, nt_opaque)
1822 || ((flags & STF_USER_VISIBLE)
1823 && !user_facing_original_type_p (result)));*/
1825 if (COMPLETE_TYPE_P (result) && !COMPLETE_TYPE_P (t))
1826 /* If RESULT is complete and T isn't, it's likely the case that T
1827 is a variant of RESULT which hasn't been updated yet. Skip the
1828 attribute handling. */;
1829 else
1831 if (TYPE_USER_ALIGN (t) != TYPE_USER_ALIGN (result)
1832 || TYPE_ALIGN (t) != TYPE_ALIGN (result))
1834 gcc_assert (TYPE_USER_ALIGN (t));
1835 if (remove_attributes)
1836 *remove_attributes = true;
1837 else
1839 if (TYPE_ALIGN (t) == TYPE_ALIGN (result))
1840 result = build_variant_type_copy (result);
1841 else
1842 result = build_aligned_type (result, TYPE_ALIGN (t));
1843 TYPE_USER_ALIGN (result) = true;
1847 if (TYPE_ATTRIBUTES (t))
1849 if (remove_attributes)
1850 result = apply_identity_attributes (result, TYPE_ATTRIBUTES (t),
1851 remove_attributes);
1852 else
1853 result = cp_build_type_attribute_variant (result,
1854 TYPE_ATTRIBUTES (t));
1858 return cp_build_qualified_type (result, cp_type_quals (t));
1861 /* Like strip_typedefs above, but works on expressions (and other
1862 non-types such as TREE_VEC), so that in
1864 template<class T> struct A
1866 typedef T TT;
1867 B<sizeof(TT)> b;
1870 sizeof(TT) is replaced by sizeof(T). */
1872 tree
1873 strip_typedefs_expr (tree t, bool *remove_attributes, unsigned int flags)
1875 unsigned i,n;
1876 tree r, type, *ops;
1877 enum tree_code code;
1879 if (t == NULL_TREE || t == error_mark_node)
1880 return t;
1882 STRIP_ANY_LOCATION_WRAPPER (t);
1884 if (DECL_P (t) || CONSTANT_CLASS_P (t))
1885 return t;
1887 code = TREE_CODE (t);
1888 switch (code)
1890 case IDENTIFIER_NODE:
1891 case TEMPLATE_PARM_INDEX:
1892 case OVERLOAD:
1893 case BASELINK:
1894 case ARGUMENT_PACK_SELECT:
1895 return t;
1897 case TRAIT_EXPR:
1899 tree type1 = strip_typedefs (TRAIT_EXPR_TYPE1 (t),
1900 remove_attributes, flags);
1901 tree type2 = strip_typedefs (TRAIT_EXPR_TYPE2 (t),
1902 remove_attributes, flags);
1903 if (type1 == TRAIT_EXPR_TYPE1 (t)
1904 && type2 == TRAIT_EXPR_TYPE2 (t))
1905 return t;
1906 r = copy_node (t);
1907 TRAIT_EXPR_TYPE1 (r) = type1;
1908 TRAIT_EXPR_TYPE2 (r) = type2;
1909 return r;
1912 case TREE_LIST:
1914 bool changed = false;
1915 releasing_vec vec;
1916 r = t;
1917 for (; t; t = TREE_CHAIN (t))
1919 gcc_assert (!TREE_PURPOSE (t));
1920 tree elt = strip_typedefs (TREE_VALUE (t),
1921 remove_attributes, flags);
1922 if (elt != TREE_VALUE (t))
1923 changed = true;
1924 vec_safe_push (vec, elt);
1926 if (changed)
1927 r = build_tree_list_vec (vec);
1928 return r;
1931 case TREE_VEC:
1933 bool changed = false;
1934 releasing_vec vec;
1935 n = TREE_VEC_LENGTH (t);
1936 vec_safe_reserve (vec, n);
1937 for (i = 0; i < n; ++i)
1939 tree op = strip_typedefs (TREE_VEC_ELT (t, i),
1940 remove_attributes, flags);
1941 vec->quick_push (op);
1942 if (op != TREE_VEC_ELT (t, i))
1943 changed = true;
1945 if (changed)
1947 r = copy_node (t);
1948 for (i = 0; i < n; ++i)
1949 TREE_VEC_ELT (r, i) = (*vec)[i];
1950 NON_DEFAULT_TEMPLATE_ARGS_COUNT (r)
1951 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
1953 else
1954 r = t;
1955 return r;
1958 case CONSTRUCTOR:
1960 bool changed = false;
1961 vec<constructor_elt, va_gc> *vec
1962 = vec_safe_copy (CONSTRUCTOR_ELTS (t));
1963 n = CONSTRUCTOR_NELTS (t);
1964 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1965 for (i = 0; i < n; ++i)
1967 constructor_elt *e = &(*vec)[i];
1968 tree op = strip_typedefs (e->value, remove_attributes, flags);
1969 if (op != e->value)
1971 changed = true;
1972 e->value = op;
1974 gcc_checking_assert
1975 (e->index == strip_typedefs (e->index, remove_attributes,
1976 flags));
1979 if (!changed && type == TREE_TYPE (t))
1981 vec_free (vec);
1982 return t;
1984 else
1986 r = copy_node (t);
1987 TREE_TYPE (r) = type;
1988 CONSTRUCTOR_ELTS (r) = vec;
1989 return r;
1993 case LAMBDA_EXPR:
1994 return t;
1996 case STATEMENT_LIST:
1997 error ("statement-expression in a constant expression");
1998 return error_mark_node;
2000 default:
2001 break;
2004 gcc_assert (EXPR_P (t));
2006 n = cp_tree_operand_length (t);
2007 ops = XALLOCAVEC (tree, n);
2008 type = TREE_TYPE (t);
2010 switch (code)
2012 CASE_CONVERT:
2013 case IMPLICIT_CONV_EXPR:
2014 case DYNAMIC_CAST_EXPR:
2015 case STATIC_CAST_EXPR:
2016 case CONST_CAST_EXPR:
2017 case REINTERPRET_CAST_EXPR:
2018 case CAST_EXPR:
2019 case NEW_EXPR:
2020 type = strip_typedefs (type, remove_attributes, flags);
2021 /* fallthrough */
2023 default:
2024 for (i = 0; i < n; ++i)
2025 ops[i] = strip_typedefs (TREE_OPERAND (t, i),
2026 remove_attributes, flags);
2027 break;
2030 /* If nothing changed, return t. */
2031 for (i = 0; i < n; ++i)
2032 if (ops[i] != TREE_OPERAND (t, i))
2033 break;
2034 if (i == n && type == TREE_TYPE (t))
2035 return t;
2037 r = copy_node (t);
2038 TREE_TYPE (r) = type;
2039 for (i = 0; i < n; ++i)
2040 TREE_OPERAND (r, i) = ops[i];
2041 return r;
2044 /* Makes a copy of BINFO and TYPE, which is to be inherited into a
2045 graph dominated by T. If BINFO is NULL, TYPE is a dependent base,
2046 and we do a shallow copy. If BINFO is non-NULL, we do a deep copy.
2047 VIRT indicates whether TYPE is inherited virtually or not.
2048 IGO_PREV points at the previous binfo of the inheritance graph
2049 order chain. The newly copied binfo's TREE_CHAIN forms this
2050 ordering.
2052 The CLASSTYPE_VBASECLASSES vector of T is constructed in the
2053 correct order. That is in the order the bases themselves should be
2054 constructed in.
2056 The BINFO_INHERITANCE of a virtual base class points to the binfo
2057 of the most derived type. ??? We could probably change this so that
2058 BINFO_INHERITANCE becomes synonymous with BINFO_PRIMARY, and hence
2059 remove a field. They currently can only differ for primary virtual
2060 virtual bases. */
2062 tree
2063 copy_binfo (tree binfo, tree type, tree t, tree *igo_prev, int virt)
2065 tree new_binfo;
2067 if (virt)
2069 /* See if we've already made this virtual base. */
2070 new_binfo = binfo_for_vbase (type, t);
2071 if (new_binfo)
2072 return new_binfo;
2075 new_binfo = make_tree_binfo (binfo ? BINFO_N_BASE_BINFOS (binfo) : 0);
2076 BINFO_TYPE (new_binfo) = type;
2078 /* Chain it into the inheritance graph. */
2079 TREE_CHAIN (*igo_prev) = new_binfo;
2080 *igo_prev = new_binfo;
2082 if (binfo && !BINFO_DEPENDENT_BASE_P (binfo))
2084 int ix;
2085 tree base_binfo;
2087 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), type));
2089 BINFO_OFFSET (new_binfo) = BINFO_OFFSET (binfo);
2090 BINFO_VIRTUALS (new_binfo) = BINFO_VIRTUALS (binfo);
2092 /* We do not need to copy the accesses, as they are read only. */
2093 BINFO_BASE_ACCESSES (new_binfo) = BINFO_BASE_ACCESSES (binfo);
2095 /* Recursively copy base binfos of BINFO. */
2096 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2098 tree new_base_binfo;
2099 new_base_binfo = copy_binfo (base_binfo, BINFO_TYPE (base_binfo),
2100 t, igo_prev,
2101 BINFO_VIRTUAL_P (base_binfo));
2103 if (!BINFO_INHERITANCE_CHAIN (new_base_binfo))
2104 BINFO_INHERITANCE_CHAIN (new_base_binfo) = new_binfo;
2105 BINFO_BASE_APPEND (new_binfo, new_base_binfo);
2108 else
2109 BINFO_DEPENDENT_BASE_P (new_binfo) = 1;
2111 if (virt)
2113 /* Push it onto the list after any virtual bases it contains
2114 will have been pushed. */
2115 CLASSTYPE_VBASECLASSES (t)->quick_push (new_binfo);
2116 BINFO_VIRTUAL_P (new_binfo) = 1;
2117 BINFO_INHERITANCE_CHAIN (new_binfo) = TYPE_BINFO (t);
2120 return new_binfo;
2123 /* Hashing of lists so that we don't make duplicates.
2124 The entry point is `list_hash_canon'. */
2126 struct list_proxy
2128 tree purpose;
2129 tree value;
2130 tree chain;
2133 struct list_hasher : ggc_ptr_hash<tree_node>
2135 typedef list_proxy *compare_type;
2137 static hashval_t hash (tree);
2138 static bool equal (tree, list_proxy *);
2141 /* Now here is the hash table. When recording a list, it is added
2142 to the slot whose index is the hash code mod the table size.
2143 Note that the hash table is used for several kinds of lists.
2144 While all these live in the same table, they are completely independent,
2145 and the hash code is computed differently for each of these. */
2147 static GTY (()) hash_table<list_hasher> *list_hash_table;
2149 /* Compare ENTRY (an entry in the hash table) with DATA (a list_proxy
2150 for a node we are thinking about adding). */
2152 bool
2153 list_hasher::equal (tree t, list_proxy *proxy)
2155 return (TREE_VALUE (t) == proxy->value
2156 && TREE_PURPOSE (t) == proxy->purpose
2157 && TREE_CHAIN (t) == proxy->chain);
2160 /* Compute a hash code for a list (chain of TREE_LIST nodes
2161 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
2162 TREE_COMMON slots), by adding the hash codes of the individual entries. */
2164 static hashval_t
2165 list_hash_pieces (tree purpose, tree value, tree chain)
2167 hashval_t hashcode = 0;
2169 if (chain)
2170 hashcode += TREE_HASH (chain);
2172 if (value)
2173 hashcode += TREE_HASH (value);
2174 else
2175 hashcode += 1007;
2176 if (purpose)
2177 hashcode += TREE_HASH (purpose);
2178 else
2179 hashcode += 1009;
2180 return hashcode;
2183 /* Hash an already existing TREE_LIST. */
2185 hashval_t
2186 list_hasher::hash (tree t)
2188 return list_hash_pieces (TREE_PURPOSE (t),
2189 TREE_VALUE (t),
2190 TREE_CHAIN (t));
2193 /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
2194 object for an identical list if one already exists. Otherwise, build a
2195 new one, and record it as the canonical object. */
2197 tree
2198 hash_tree_cons (tree purpose, tree value, tree chain)
2200 int hashcode = 0;
2201 tree *slot;
2202 struct list_proxy proxy;
2204 /* Hash the list node. */
2205 hashcode = list_hash_pieces (purpose, value, chain);
2206 /* Create a proxy for the TREE_LIST we would like to create. We
2207 don't actually create it so as to avoid creating garbage. */
2208 proxy.purpose = purpose;
2209 proxy.value = value;
2210 proxy.chain = chain;
2211 /* See if it is already in the table. */
2212 slot = list_hash_table->find_slot_with_hash (&proxy, hashcode, INSERT);
2213 /* If not, create a new node. */
2214 if (!*slot)
2215 *slot = tree_cons (purpose, value, chain);
2216 return (tree) *slot;
2219 /* Constructor for hashed lists. */
2221 tree
2222 hash_tree_chain (tree value, tree chain)
2224 return hash_tree_cons (NULL_TREE, value, chain);
2227 void
2228 debug_binfo (tree elem)
2230 HOST_WIDE_INT n;
2231 tree virtuals;
2233 fprintf (stderr, "type \"%s\", offset = " HOST_WIDE_INT_PRINT_DEC
2234 "\nvtable type:\n",
2235 TYPE_NAME_STRING (BINFO_TYPE (elem)),
2236 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
2237 debug_tree (BINFO_TYPE (elem));
2238 if (BINFO_VTABLE (elem))
2239 fprintf (stderr, "vtable decl \"%s\"\n",
2240 IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem))));
2241 else
2242 fprintf (stderr, "no vtable decl yet\n");
2243 fprintf (stderr, "virtuals:\n");
2244 virtuals = BINFO_VIRTUALS (elem);
2245 n = 0;
2247 while (virtuals)
2249 tree fndecl = TREE_VALUE (virtuals);
2250 fprintf (stderr, "%s [%ld =? %ld]\n",
2251 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
2252 (long) n, (long) TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
2253 ++n;
2254 virtuals = TREE_CHAIN (virtuals);
2258 /* Build a representation for the qualified name SCOPE::NAME. TYPE is
2259 the type of the result expression, if known, or NULL_TREE if the
2260 resulting expression is type-dependent. If TEMPLATE_P is true,
2261 NAME is known to be a template because the user explicitly used the
2262 "template" keyword after the "::".
2264 All SCOPE_REFs should be built by use of this function. */
2266 tree
2267 build_qualified_name (tree type, tree scope, tree name, bool template_p)
2269 tree t;
2270 if (type == error_mark_node
2271 || scope == error_mark_node
2272 || name == error_mark_node)
2273 return error_mark_node;
2274 gcc_assert (TREE_CODE (name) != SCOPE_REF);
2275 t = build2 (SCOPE_REF, type, scope, name);
2276 QUALIFIED_NAME_IS_TEMPLATE (t) = template_p;
2277 PTRMEM_OK_P (t) = true;
2278 if (type)
2279 t = convert_from_reference (t);
2280 return t;
2283 /* Like check_qualified_type, but also check ref-qualifier, exception
2284 specification, and whether the return type was specified after the
2285 parameters. */
2287 static bool
2288 cp_check_qualified_type (const_tree cand, const_tree base, int type_quals,
2289 cp_ref_qualifier rqual, tree raises, bool late)
2291 return (TYPE_QUALS (cand) == type_quals
2292 && check_base_type (cand, base)
2293 && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (cand),
2294 ce_exact)
2295 && TYPE_HAS_LATE_RETURN_TYPE (cand) == late
2296 && type_memfn_rqual (cand) == rqual);
2299 /* Build the FUNCTION_TYPE or METHOD_TYPE with the ref-qualifier RQUAL. */
2301 tree
2302 build_ref_qualified_type (tree type, cp_ref_qualifier rqual)
2304 tree raises = TYPE_RAISES_EXCEPTIONS (type);
2305 bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
2306 return build_cp_fntype_variant (type, rqual, raises, late);
2309 tree
2310 make_binding_vec (tree name, unsigned clusters MEM_STAT_DECL)
2312 /* Stored in an unsigned short, but we're limited to the number of
2313 modules anyway. */
2314 gcc_checking_assert (clusters <= (unsigned short)(~0));
2315 size_t length = (offsetof (tree_binding_vec, vec)
2316 + clusters * sizeof (binding_cluster));
2317 tree vec = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2318 TREE_SET_CODE (vec, BINDING_VECTOR);
2319 BINDING_VECTOR_NAME (vec) = name;
2320 BINDING_VECTOR_ALLOC_CLUSTERS (vec) = clusters;
2321 BINDING_VECTOR_NUM_CLUSTERS (vec) = 0;
2323 return vec;
2326 /* Make a raw overload node containing FN. */
2328 tree
2329 ovl_make (tree fn, tree next)
2331 tree result = make_node (OVERLOAD);
2333 if (TREE_CODE (fn) == OVERLOAD)
2334 OVL_NESTED_P (result) = true;
2336 TREE_TYPE (result) = (next || TREE_CODE (fn) == TEMPLATE_DECL
2337 ? unknown_type_node : TREE_TYPE (fn));
2338 if (next && TREE_CODE (next) == OVERLOAD && OVL_DEDUP_P (next))
2339 OVL_DEDUP_P (result) = true;
2340 OVL_FUNCTION (result) = fn;
2341 OVL_CHAIN (result) = next;
2342 return result;
2345 /* Add FN to the (potentially NULL) overload set OVL. USING_OR_HIDDEN is >
2346 zero if this is a using-decl. It is > 1 if we're exporting the
2347 using decl. USING_OR_HIDDEN is < 0, if FN is hidden. (A decl
2348 cannot be both using and hidden.) We keep the hidden decls first,
2349 but remaining ones are unordered. */
2351 tree
2352 ovl_insert (tree fn, tree maybe_ovl, int using_or_hidden)
2354 tree result = maybe_ovl;
2355 tree insert_after = NULL_TREE;
2357 /* Skip hidden. */
2358 for (; maybe_ovl && TREE_CODE (maybe_ovl) == OVERLOAD
2359 && OVL_HIDDEN_P (maybe_ovl);
2360 maybe_ovl = OVL_CHAIN (maybe_ovl))
2362 gcc_checking_assert (!OVL_LOOKUP_P (maybe_ovl));
2363 insert_after = maybe_ovl;
2366 if (maybe_ovl || using_or_hidden || TREE_CODE (fn) == TEMPLATE_DECL)
2368 maybe_ovl = ovl_make (fn, maybe_ovl);
2370 if (using_or_hidden < 0)
2371 OVL_HIDDEN_P (maybe_ovl) = true;
2372 if (using_or_hidden > 0)
2374 OVL_DEDUP_P (maybe_ovl) = OVL_USING_P (maybe_ovl) = true;
2375 if (using_or_hidden > 1)
2376 OVL_EXPORT_P (maybe_ovl) = true;
2379 else
2380 maybe_ovl = fn;
2382 if (insert_after)
2384 OVL_CHAIN (insert_after) = maybe_ovl;
2385 TREE_TYPE (insert_after) = unknown_type_node;
2387 else
2388 result = maybe_ovl;
2390 return result;
2393 /* Skip any hidden names at the beginning of OVL. */
2395 tree
2396 ovl_skip_hidden (tree ovl)
2398 while (ovl && TREE_CODE (ovl) == OVERLOAD && OVL_HIDDEN_P (ovl))
2399 ovl = OVL_CHAIN (ovl);
2401 return ovl;
2404 /* NODE is an OVL_HIDDEN_P node that is now revealed. */
2406 tree
2407 ovl_iterator::reveal_node (tree overload, tree node)
2409 /* We cannot have returned NODE as part of a lookup overload, so we
2410 don't have to worry about preserving that. */
2412 OVL_HIDDEN_P (node) = false;
2413 if (tree chain = OVL_CHAIN (node))
2414 if (TREE_CODE (chain) == OVERLOAD)
2416 if (OVL_HIDDEN_P (chain))
2418 /* The node needs moving, and the simplest way is to remove it
2419 and reinsert. */
2420 overload = remove_node (overload, node);
2421 overload = ovl_insert (OVL_FUNCTION (node), overload);
2423 else if (OVL_DEDUP_P (chain))
2424 OVL_DEDUP_P (node) = true;
2426 return overload;
2429 /* NODE is on the overloads of OVL. Remove it.
2430 The removed node is unaltered and may continue to be iterated
2431 from (i.e. it is safe to remove a node from an overload one is
2432 currently iterating over). */
2434 tree
2435 ovl_iterator::remove_node (tree overload, tree node)
2437 tree *slot = &overload;
2438 while (*slot != node)
2440 tree probe = *slot;
2441 gcc_checking_assert (!OVL_LOOKUP_P (probe));
2443 slot = &OVL_CHAIN (probe);
2446 /* Stitch out NODE. We don't have to worry about now making a
2447 singleton overload (and consequently maybe setting its type),
2448 because all uses of this function will be followed by inserting a
2449 new node that must follow the place we've cut this out from. */
2450 if (TREE_CODE (node) != OVERLOAD)
2451 /* Cloned inherited ctors don't mark themselves as via_using. */
2452 *slot = NULL_TREE;
2453 else
2454 *slot = OVL_CHAIN (node);
2456 return overload;
2459 /* Mark or unmark a lookup set. */
2461 void
2462 lookup_mark (tree ovl, bool val)
2464 for (lkp_iterator iter (ovl); iter; ++iter)
2466 gcc_checking_assert (LOOKUP_SEEN_P (*iter) != val);
2467 LOOKUP_SEEN_P (*iter) = val;
2471 /* Add a set of new FNS into a lookup. */
2473 tree
2474 lookup_add (tree fns, tree lookup)
2476 if (fns == error_mark_node || lookup == error_mark_node)
2477 return error_mark_node;
2479 if (lookup || TREE_CODE (fns) == TEMPLATE_DECL)
2481 lookup = ovl_make (fns, lookup);
2482 OVL_LOOKUP_P (lookup) = true;
2484 else
2485 lookup = fns;
2487 return lookup;
2490 /* FNS is a new overload set, add them to LOOKUP, if they are not
2491 already present there. */
2493 tree
2494 lookup_maybe_add (tree fns, tree lookup, bool deduping)
2496 if (deduping)
2497 for (tree next, probe = fns; probe; probe = next)
2499 tree fn = probe;
2500 next = NULL_TREE;
2502 if (TREE_CODE (probe) == OVERLOAD)
2504 fn = OVL_FUNCTION (probe);
2505 next = OVL_CHAIN (probe);
2508 if (!LOOKUP_SEEN_P (fn))
2509 LOOKUP_SEEN_P (fn) = true;
2510 else
2512 /* This function was already seen. Insert all the
2513 predecessors onto the lookup. */
2514 for (; fns != probe; fns = OVL_CHAIN (fns))
2516 lookup = lookup_add (OVL_FUNCTION (fns), lookup);
2517 /* Propagate OVL_USING, but OVL_HIDDEN &
2518 OVL_DEDUP_P don't matter. */
2519 if (OVL_USING_P (fns))
2520 OVL_USING_P (lookup) = true;
2523 /* And now skip this function. */
2524 fns = next;
2528 if (fns)
2529 /* We ended in a set of new functions. Add them all in one go. */
2530 lookup = lookup_add (fns, lookup);
2532 return lookup;
2535 /* Returns nonzero if X is an expression for a (possibly overloaded)
2536 function. If "f" is a function or function template, "f", "c->f",
2537 "c.f", "C::f", and "f<int>" will all be considered possibly
2538 overloaded functions. Returns 2 if the function is actually
2539 overloaded, i.e., if it is impossible to know the type of the
2540 function without performing overload resolution. */
2543 is_overloaded_fn (tree x)
2545 STRIP_ANY_LOCATION_WRAPPER (x);
2547 /* A baselink is also considered an overloaded function. */
2548 if (TREE_CODE (x) == OFFSET_REF
2549 || TREE_CODE (x) == COMPONENT_REF)
2550 x = TREE_OPERAND (x, 1);
2551 x = MAYBE_BASELINK_FUNCTIONS (x);
2552 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
2553 x = TREE_OPERAND (x, 0);
2555 if (DECL_FUNCTION_TEMPLATE_P (OVL_FIRST (x))
2556 || (TREE_CODE (x) == OVERLOAD && !OVL_SINGLE_P (x)))
2557 return 2;
2559 return OVL_P (x);
2562 /* X is the CALL_EXPR_FN of a CALL_EXPR. If X represents a dependent name
2563 (14.6.2), return the IDENTIFIER_NODE for that name. Otherwise, return
2564 NULL_TREE. */
2566 tree
2567 dependent_name (tree x)
2569 /* FIXME a dependent name must be unqualified, but this function doesn't
2570 distinguish between qualified and unqualified identifiers. */
2571 if (identifier_p (x))
2572 return x;
2573 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
2574 x = TREE_OPERAND (x, 0);
2575 if (OVL_P (x))
2576 return OVL_NAME (x);
2577 return NULL_TREE;
2580 /* Like dependent_name, but instead takes a CALL_EXPR and also checks
2581 its dependence. */
2583 tree
2584 call_expr_dependent_name (tree x)
2586 if (TREE_TYPE (x) != NULL_TREE)
2587 /* X isn't dependent, so its callee isn't a dependent name. */
2588 return NULL_TREE;
2589 return dependent_name (CALL_EXPR_FN (x));
2592 /* Returns true iff X is an expression for an overloaded function
2593 whose type cannot be known without performing overload
2594 resolution. */
2596 bool
2597 really_overloaded_fn (tree x)
2599 return is_overloaded_fn (x) == 2;
2602 /* Get the overload set FROM refers to. Returns NULL if it's not an
2603 overload set. */
2605 tree
2606 maybe_get_fns (tree from)
2608 STRIP_ANY_LOCATION_WRAPPER (from);
2610 /* A baselink is also considered an overloaded function. */
2611 if (TREE_CODE (from) == OFFSET_REF
2612 || TREE_CODE (from) == COMPONENT_REF)
2613 from = TREE_OPERAND (from, 1);
2614 if (BASELINK_P (from))
2615 from = BASELINK_FUNCTIONS (from);
2616 if (TREE_CODE (from) == TEMPLATE_ID_EXPR)
2617 from = TREE_OPERAND (from, 0);
2619 if (OVL_P (from))
2620 return from;
2622 return NULL;
2625 /* FROM refers to an overload set. Return that set (or die). */
2627 tree
2628 get_fns (tree from)
2630 tree res = maybe_get_fns (from);
2632 gcc_assert (res);
2633 return res;
2636 /* Return the first function of the overload set FROM refers to. */
2638 tree
2639 get_first_fn (tree from)
2641 return OVL_FIRST (get_fns (from));
2644 /* Return the scope where the overloaded functions OVL were found. */
2646 tree
2647 ovl_scope (tree ovl)
2649 if (TREE_CODE (ovl) == OFFSET_REF
2650 || TREE_CODE (ovl) == COMPONENT_REF)
2651 ovl = TREE_OPERAND (ovl, 1);
2652 if (TREE_CODE (ovl) == BASELINK)
2653 return BINFO_TYPE (BASELINK_BINFO (ovl));
2654 if (TREE_CODE (ovl) == TEMPLATE_ID_EXPR)
2655 ovl = TREE_OPERAND (ovl, 0);
2656 /* Skip using-declarations. */
2657 lkp_iterator iter (ovl);
2659 ovl = *iter;
2660 while (iter.using_p () && ++iter);
2662 return CP_DECL_CONTEXT (ovl);
2665 #define PRINT_RING_SIZE 4
2667 static const char *
2668 cxx_printable_name_internal (tree decl, int v, bool translate)
2670 static unsigned int uid_ring[PRINT_RING_SIZE];
2671 static char *print_ring[PRINT_RING_SIZE];
2672 static bool trans_ring[PRINT_RING_SIZE];
2673 static int ring_counter;
2674 int i;
2676 /* Only cache functions. */
2677 if (v < 2
2678 || TREE_CODE (decl) != FUNCTION_DECL
2679 || DECL_LANG_SPECIFIC (decl) == 0)
2680 return lang_decl_name (decl, v, translate);
2682 /* See if this print name is lying around. */
2683 for (i = 0; i < PRINT_RING_SIZE; i++)
2684 if (uid_ring[i] == DECL_UID (decl) && translate == trans_ring[i])
2685 /* yes, so return it. */
2686 return print_ring[i];
2688 if (++ring_counter == PRINT_RING_SIZE)
2689 ring_counter = 0;
2691 if (current_function_decl != NULL_TREE)
2693 /* There may be both translated and untranslated versions of the
2694 name cached. */
2695 for (i = 0; i < 2; i++)
2697 if (uid_ring[ring_counter] == DECL_UID (current_function_decl))
2698 ring_counter += 1;
2699 if (ring_counter == PRINT_RING_SIZE)
2700 ring_counter = 0;
2702 gcc_assert (uid_ring[ring_counter] != DECL_UID (current_function_decl));
2705 free (print_ring[ring_counter]);
2707 print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v, translate));
2708 uid_ring[ring_counter] = DECL_UID (decl);
2709 trans_ring[ring_counter] = translate;
2710 return print_ring[ring_counter];
2713 const char *
2714 cxx_printable_name (tree decl, int v)
2716 return cxx_printable_name_internal (decl, v, false);
2719 const char *
2720 cxx_printable_name_translate (tree decl, int v)
2722 return cxx_printable_name_internal (decl, v, true);
2725 /* Return the canonical version of exception-specification RAISES for a C++17
2726 function type, for use in type comparison and building TYPE_CANONICAL. */
2728 tree
2729 canonical_eh_spec (tree raises)
2731 if (raises == NULL_TREE)
2732 return raises;
2733 else if (DEFERRED_NOEXCEPT_SPEC_P (raises)
2734 || UNPARSED_NOEXCEPT_SPEC_P (raises)
2735 || uses_template_parms (raises)
2736 || uses_template_parms (TREE_PURPOSE (raises)))
2737 /* Keep a dependent or deferred exception specification. */
2738 return raises;
2739 else if (nothrow_spec_p (raises))
2740 /* throw() -> noexcept. */
2741 return noexcept_true_spec;
2742 else
2743 /* For C++17 type matching, anything else -> nothing. */
2744 return NULL_TREE;
2747 tree
2748 build_cp_fntype_variant (tree type, cp_ref_qualifier rqual,
2749 tree raises, bool late)
2751 cp_cv_quals type_quals = TYPE_QUALS (type);
2753 if (cp_check_qualified_type (type, type, type_quals, rqual, raises, late))
2754 return type;
2756 tree v = TYPE_MAIN_VARIANT (type);
2757 for (; v; v = TYPE_NEXT_VARIANT (v))
2758 if (cp_check_qualified_type (v, type, type_quals, rqual, raises, late))
2759 return v;
2761 /* Need to build a new variant. */
2762 v = build_variant_type_copy (type);
2763 if (!TYPE_DEPENDENT_P (v))
2764 /* We no longer know that it's not type-dependent. */
2765 TYPE_DEPENDENT_P_VALID (v) = false;
2766 TYPE_RAISES_EXCEPTIONS (v) = raises;
2767 TYPE_HAS_LATE_RETURN_TYPE (v) = late;
2768 switch (rqual)
2770 case REF_QUAL_RVALUE:
2771 FUNCTION_RVALUE_QUALIFIED (v) = 1;
2772 FUNCTION_REF_QUALIFIED (v) = 1;
2773 break;
2774 case REF_QUAL_LVALUE:
2775 FUNCTION_RVALUE_QUALIFIED (v) = 0;
2776 FUNCTION_REF_QUALIFIED (v) = 1;
2777 break;
2778 default:
2779 FUNCTION_REF_QUALIFIED (v) = 0;
2780 break;
2783 /* Canonicalize the exception specification. */
2784 tree cr = flag_noexcept_type ? canonical_eh_spec (raises) : NULL_TREE;
2786 if (TYPE_STRUCTURAL_EQUALITY_P (type))
2787 /* Propagate structural equality. */
2788 SET_TYPE_STRUCTURAL_EQUALITY (v);
2789 else if (TYPE_CANONICAL (type) != type || cr != raises || late)
2790 /* Build the underlying canonical type, since it is different
2791 from TYPE. */
2792 TYPE_CANONICAL (v) = build_cp_fntype_variant (TYPE_CANONICAL (type),
2793 rqual, cr, false);
2794 else
2795 /* T is its own canonical type. */
2796 TYPE_CANONICAL (v) = v;
2798 return v;
2801 /* TYPE is a function or method type with a deferred exception
2802 specification that has been parsed to RAISES. Fixup all the type
2803 variants that are affected in place. Via decltype &| noexcept
2804 tricks, the unparsed spec could have escaped into the type system.
2805 The general case is hard to fixup canonical types for. */
2807 void
2808 fixup_deferred_exception_variants (tree type, tree raises)
2810 tree original = TYPE_RAISES_EXCEPTIONS (type);
2811 tree cr = flag_noexcept_type ? canonical_eh_spec (raises) : NULL_TREE;
2813 gcc_checking_assert (UNPARSED_NOEXCEPT_SPEC_P (original));
2815 /* Though sucky, this walk will process the canonical variants
2816 first. */
2817 tree prev = NULL_TREE;
2818 for (tree variant = TYPE_MAIN_VARIANT (type);
2819 variant; prev = variant, variant = TYPE_NEXT_VARIANT (variant))
2820 if (TYPE_RAISES_EXCEPTIONS (variant) == original)
2822 gcc_checking_assert (variant != TYPE_MAIN_VARIANT (type));
2824 if (!TYPE_STRUCTURAL_EQUALITY_P (variant))
2826 cp_cv_quals var_quals = TYPE_QUALS (variant);
2827 cp_ref_qualifier rqual = type_memfn_rqual (variant);
2829 /* If VARIANT would become a dup (cp_check_qualified_type-wise)
2830 of an existing variant in the variant list of TYPE after its
2831 exception specification has been parsed, elide it. Otherwise,
2832 build_cp_fntype_variant could use it, leading to "canonical
2833 types differ for identical types." */
2834 tree v = TYPE_MAIN_VARIANT (type);
2835 for (; v; v = TYPE_NEXT_VARIANT (v))
2836 if (cp_check_qualified_type (v, variant, var_quals,
2837 rqual, cr, false))
2839 /* The main variant will not match V, so PREV will never
2840 be null. */
2841 TYPE_NEXT_VARIANT (prev) = TYPE_NEXT_VARIANT (variant);
2842 break;
2844 TYPE_RAISES_EXCEPTIONS (variant) = raises;
2846 if (!v)
2847 v = build_cp_fntype_variant (TYPE_CANONICAL (variant),
2848 rqual, cr, false);
2849 TYPE_CANONICAL (variant) = TYPE_CANONICAL (v);
2851 else
2852 TYPE_RAISES_EXCEPTIONS (variant) = raises;
2854 if (!TYPE_DEPENDENT_P (variant))
2855 /* We no longer know that it's not type-dependent. */
2856 TYPE_DEPENDENT_P_VALID (variant) = false;
2860 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
2861 listed in RAISES. */
2863 tree
2864 build_exception_variant (tree type, tree raises)
2866 cp_ref_qualifier rqual = type_memfn_rqual (type);
2867 bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
2868 return build_cp_fntype_variant (type, rqual, raises, late);
2871 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new
2872 BOUND_TEMPLATE_TEMPLATE_PARM bound with NEWARGS as its template
2873 arguments. */
2875 tree
2876 bind_template_template_parm (tree t, tree newargs)
2878 tree decl = TYPE_NAME (t);
2879 tree t2;
2881 t2 = cxx_make_type (BOUND_TEMPLATE_TEMPLATE_PARM);
2882 decl = build_decl (input_location,
2883 TYPE_DECL, DECL_NAME (decl), NULL_TREE);
2884 SET_DECL_TEMPLATE_PARM_P (decl);
2886 /* These nodes have to be created to reflect new TYPE_DECL and template
2887 arguments. */
2888 TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t));
2889 TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl;
2890 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
2891 = build_template_info (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t), newargs);
2893 TREE_TYPE (decl) = t2;
2894 TYPE_NAME (t2) = decl;
2895 TYPE_STUB_DECL (t2) = decl;
2896 TYPE_SIZE (t2) = 0;
2898 if (any_template_arguments_need_structural_equality_p (newargs))
2899 SET_TYPE_STRUCTURAL_EQUALITY (t2);
2900 else
2901 TYPE_CANONICAL (t2) = canonical_type_parameter (t2);
2903 return t2;
2906 /* Called from count_trees via walk_tree. */
2908 static tree
2909 count_trees_r (tree *tp, int *walk_subtrees, void *data)
2911 ++*((int *) data);
2913 if (TYPE_P (*tp))
2914 *walk_subtrees = 0;
2916 return NULL_TREE;
2919 /* Debugging function for measuring the rough complexity of a tree
2920 representation. */
2923 count_trees (tree t)
2925 int n_trees = 0;
2926 cp_walk_tree_without_duplicates (&t, count_trees_r, &n_trees);
2927 return n_trees;
2930 /* Called from verify_stmt_tree via walk_tree. */
2932 static tree
2933 verify_stmt_tree_r (tree* tp, int * /*walk_subtrees*/, void* data)
2935 tree t = *tp;
2936 hash_table<nofree_ptr_hash <tree_node> > *statements
2937 = static_cast <hash_table<nofree_ptr_hash <tree_node> > *> (data);
2938 tree_node **slot;
2940 if (!STATEMENT_CODE_P (TREE_CODE (t)))
2941 return NULL_TREE;
2943 /* If this statement is already present in the hash table, then
2944 there is a circularity in the statement tree. */
2945 gcc_assert (!statements->find (t));
2947 slot = statements->find_slot (t, INSERT);
2948 *slot = t;
2950 return NULL_TREE;
2953 /* Debugging function to check that the statement T has not been
2954 corrupted. For now, this function simply checks that T contains no
2955 circularities. */
2957 void
2958 verify_stmt_tree (tree t)
2960 hash_table<nofree_ptr_hash <tree_node> > statements (37);
2961 cp_walk_tree (&t, verify_stmt_tree_r, &statements, NULL);
2964 /* Check if the type T depends on a type with no linkage and if so,
2965 return it. If RELAXED_P then do not consider a class type declared
2966 within a vague-linkage function to have no linkage. Remember:
2967 no-linkage is not the same as internal-linkage. */
2969 tree
2970 no_linkage_check (tree t, bool relaxed_p)
2972 tree r;
2974 /* Lambda types that don't have mangling scope have no linkage. We
2975 check CLASSTYPE_LAMBDA_EXPR for error_mark_node because
2976 when we get here from pushtag none of the lambda information is
2977 set up yet, so we want to assume that the lambda has linkage and
2978 fix it up later if not. We need to check this even in templates so
2979 that we properly handle a lambda-expression in the signature. */
2980 if (LAMBDA_TYPE_P (t)
2981 && CLASSTYPE_LAMBDA_EXPR (t) != error_mark_node)
2983 tree extra = LAMBDA_TYPE_EXTRA_SCOPE (t);
2984 if (!extra)
2985 return t;
2988 /* Otherwise there's no point in checking linkage on template functions; we
2989 can't know their complete types. */
2990 if (processing_template_decl)
2991 return NULL_TREE;
2993 switch (TREE_CODE (t))
2995 case RECORD_TYPE:
2996 if (TYPE_PTRMEMFUNC_P (t))
2997 goto ptrmem;
2998 /* Fall through. */
2999 case UNION_TYPE:
3000 if (!CLASS_TYPE_P (t))
3001 return NULL_TREE;
3002 /* Fall through. */
3003 case ENUMERAL_TYPE:
3004 /* Only treat unnamed types as having no linkage if they're at
3005 namespace scope. This is core issue 966. */
3006 if (TYPE_UNNAMED_P (t) && TYPE_NAMESPACE_SCOPE_P (t))
3007 return t;
3009 for (r = CP_TYPE_CONTEXT (t); ; )
3011 /* If we're a nested type of a !TREE_PUBLIC class, we might not
3012 have linkage, or we might just be in an anonymous namespace.
3013 If we're in a TREE_PUBLIC class, we have linkage. */
3014 if (TYPE_P (r) && !TREE_PUBLIC (TYPE_NAME (r)))
3015 return no_linkage_check (TYPE_CONTEXT (t), relaxed_p);
3016 else if (TREE_CODE (r) == FUNCTION_DECL)
3018 if (!relaxed_p || !vague_linkage_p (r))
3019 return t;
3020 else
3021 r = CP_DECL_CONTEXT (r);
3023 else
3024 break;
3027 return NULL_TREE;
3029 case ARRAY_TYPE:
3030 case POINTER_TYPE:
3031 case REFERENCE_TYPE:
3032 case VECTOR_TYPE:
3033 return no_linkage_check (TREE_TYPE (t), relaxed_p);
3035 case OFFSET_TYPE:
3036 ptrmem:
3037 r = no_linkage_check (TYPE_PTRMEM_POINTED_TO_TYPE (t),
3038 relaxed_p);
3039 if (r)
3040 return r;
3041 return no_linkage_check (TYPE_PTRMEM_CLASS_TYPE (t), relaxed_p);
3043 case METHOD_TYPE:
3044 case FUNCTION_TYPE:
3046 tree parm = TYPE_ARG_TYPES (t);
3047 if (TREE_CODE (t) == METHOD_TYPE)
3048 /* The 'this' pointer isn't interesting; a method has the same
3049 linkage (or lack thereof) as its enclosing class. */
3050 parm = TREE_CHAIN (parm);
3051 for (;
3052 parm && parm != void_list_node;
3053 parm = TREE_CHAIN (parm))
3055 r = no_linkage_check (TREE_VALUE (parm), relaxed_p);
3056 if (r)
3057 return r;
3059 return no_linkage_check (TREE_TYPE (t), relaxed_p);
3062 default:
3063 return NULL_TREE;
3067 extern int depth_reached;
3069 void
3070 cxx_print_statistics (void)
3072 print_template_statistics ();
3073 if (GATHER_STATISTICS)
3074 fprintf (stderr, "maximum template instantiation depth reached: %d\n",
3075 depth_reached);
3078 /* Return, as an INTEGER_CST node, the number of elements for TYPE
3079 (which is an ARRAY_TYPE). This counts only elements of the top
3080 array. */
3082 tree
3083 array_type_nelts_top (tree type)
3085 return fold_build2_loc (input_location,
3086 PLUS_EXPR, sizetype,
3087 array_type_nelts (type),
3088 size_one_node);
3091 /* Return, as an INTEGER_CST node, the number of elements for TYPE
3092 (which is an ARRAY_TYPE). This one is a recursive count of all
3093 ARRAY_TYPEs that are clumped together. */
3095 tree
3096 array_type_nelts_total (tree type)
3098 tree sz = array_type_nelts_top (type);
3099 type = TREE_TYPE (type);
3100 while (TREE_CODE (type) == ARRAY_TYPE)
3102 tree n = array_type_nelts_top (type);
3103 sz = fold_build2_loc (input_location,
3104 MULT_EXPR, sizetype, sz, n);
3105 type = TREE_TYPE (type);
3107 return sz;
3110 struct bot_data
3112 splay_tree target_remap;
3113 bool clear_location;
3116 /* Called from break_out_target_exprs via mapcar. */
3118 static tree
3119 bot_manip (tree* tp, int* walk_subtrees, void* data_)
3121 bot_data &data = *(bot_data*)data_;
3122 splay_tree target_remap = data.target_remap;
3123 tree t = *tp;
3125 if (!TYPE_P (t) && TREE_CONSTANT (t) && !TREE_SIDE_EFFECTS (t))
3127 /* There can't be any TARGET_EXPRs or their slot variables below this
3128 point. But we must make a copy, in case subsequent processing
3129 alters any part of it. For example, during gimplification a cast
3130 of the form (T) &X::f (where "f" is a member function) will lead
3131 to replacing the PTRMEM_CST for &X::f with a VAR_DECL. */
3132 *walk_subtrees = 0;
3133 *tp = unshare_expr (t);
3134 return NULL_TREE;
3136 if (TREE_CODE (t) == TARGET_EXPR)
3138 tree u;
3140 if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
3142 u = build_cplus_new (TREE_TYPE (t), TREE_OPERAND (t, 1),
3143 tf_warning_or_error);
3144 if (u == error_mark_node)
3145 return u;
3146 if (AGGR_INIT_ZERO_FIRST (TREE_OPERAND (t, 1)))
3147 AGGR_INIT_ZERO_FIRST (TREE_OPERAND (u, 1)) = true;
3149 else
3150 u = force_target_expr (TREE_TYPE (t), TREE_OPERAND (t, 1),
3151 tf_warning_or_error);
3153 TARGET_EXPR_IMPLICIT_P (u) = TARGET_EXPR_IMPLICIT_P (t);
3154 TARGET_EXPR_LIST_INIT_P (u) = TARGET_EXPR_LIST_INIT_P (t);
3155 TARGET_EXPR_DIRECT_INIT_P (u) = TARGET_EXPR_DIRECT_INIT_P (t);
3156 TARGET_EXPR_ELIDING_P (u) = TARGET_EXPR_ELIDING_P (t);
3158 /* Map the old variable to the new one. */
3159 splay_tree_insert (target_remap,
3160 (splay_tree_key) TREE_OPERAND (t, 0),
3161 (splay_tree_value) TREE_OPERAND (u, 0));
3163 TREE_OPERAND (u, 1) = break_out_target_exprs (TREE_OPERAND (u, 1),
3164 data.clear_location);
3165 if (TREE_OPERAND (u, 1) == error_mark_node)
3166 return error_mark_node;
3168 /* Replace the old expression with the new version. */
3169 *tp = u;
3170 /* We don't have to go below this point; the recursive call to
3171 break_out_target_exprs will have handled anything below this
3172 point. */
3173 *walk_subtrees = 0;
3174 return NULL_TREE;
3176 if (TREE_CODE (*tp) == SAVE_EXPR)
3178 t = *tp;
3179 splay_tree_node n = splay_tree_lookup (target_remap,
3180 (splay_tree_key) t);
3181 if (n)
3183 *tp = (tree)n->value;
3184 *walk_subtrees = 0;
3186 else
3188 copy_tree_r (tp, walk_subtrees, NULL);
3189 splay_tree_insert (target_remap,
3190 (splay_tree_key)t,
3191 (splay_tree_value)*tp);
3192 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
3193 splay_tree_insert (target_remap,
3194 (splay_tree_key)*tp,
3195 (splay_tree_value)*tp);
3197 return NULL_TREE;
3199 if (TREE_CODE (*tp) == DECL_EXPR
3200 && VAR_P (DECL_EXPR_DECL (*tp))
3201 && DECL_ARTIFICIAL (DECL_EXPR_DECL (*tp))
3202 && !TREE_STATIC (DECL_EXPR_DECL (*tp)))
3204 tree t;
3205 splay_tree_node n
3206 = splay_tree_lookup (target_remap,
3207 (splay_tree_key) DECL_EXPR_DECL (*tp));
3208 if (n)
3209 t = (tree) n->value;
3210 else
3212 t = create_temporary_var (TREE_TYPE (DECL_EXPR_DECL (*tp)));
3213 DECL_INITIAL (t) = DECL_INITIAL (DECL_EXPR_DECL (*tp));
3214 splay_tree_insert (target_remap,
3215 (splay_tree_key) DECL_EXPR_DECL (*tp),
3216 (splay_tree_value) t);
3218 copy_tree_r (tp, walk_subtrees, NULL);
3219 DECL_EXPR_DECL (*tp) = t;
3220 if (data.clear_location && EXPR_HAS_LOCATION (*tp))
3221 SET_EXPR_LOCATION (*tp, input_location);
3222 return NULL_TREE;
3224 if (TREE_CODE (*tp) == BIND_EXPR && BIND_EXPR_VARS (*tp))
3226 copy_tree_r (tp, walk_subtrees, NULL);
3227 for (tree *p = &BIND_EXPR_VARS (*tp); *p; p = &DECL_CHAIN (*p))
3229 gcc_assert (VAR_P (*p) && DECL_ARTIFICIAL (*p) && !TREE_STATIC (*p));
3230 tree t = create_temporary_var (TREE_TYPE (*p));
3231 DECL_INITIAL (t) = DECL_INITIAL (*p);
3232 DECL_CHAIN (t) = DECL_CHAIN (*p);
3233 splay_tree_insert (target_remap, (splay_tree_key) *p,
3234 (splay_tree_value) t);
3235 *p = t;
3237 if (data.clear_location && EXPR_HAS_LOCATION (*tp))
3238 SET_EXPR_LOCATION (*tp, input_location);
3239 return NULL_TREE;
3242 /* Make a copy of this node. */
3243 t = copy_tree_r (tp, walk_subtrees, NULL);
3244 if (TREE_CODE (*tp) == CALL_EXPR || TREE_CODE (*tp) == AGGR_INIT_EXPR)
3245 if (!processing_template_decl)
3246 set_flags_from_callee (*tp);
3247 if (data.clear_location && EXPR_HAS_LOCATION (*tp))
3248 SET_EXPR_LOCATION (*tp, input_location);
3249 return t;
3252 /* Replace all remapped VAR_DECLs in T with their new equivalents.
3253 DATA is really a splay-tree mapping old variables to new
3254 variables. */
3256 static tree
3257 bot_replace (tree* t, int* walk_subtrees, void* data_)
3259 bot_data &data = *(bot_data*)data_;
3260 splay_tree target_remap = data.target_remap;
3262 if (VAR_P (*t))
3264 splay_tree_node n = splay_tree_lookup (target_remap,
3265 (splay_tree_key) *t);
3266 if (n)
3267 *t = (tree) n->value;
3269 else if (TREE_CODE (*t) == PARM_DECL
3270 && DECL_NAME (*t) == this_identifier
3271 && !DECL_CONTEXT (*t))
3273 /* In an NSDMI we need to replace the 'this' parameter we used for
3274 parsing with the real one for this function. */
3275 *t = current_class_ptr;
3277 else if (TREE_CODE (*t) == CONVERT_EXPR
3278 && CONVERT_EXPR_VBASE_PATH (*t))
3280 /* In an NSDMI build_base_path defers building conversions to morally
3281 virtual bases, and we handle it here. */
3282 tree basetype = TREE_TYPE (*t);
3283 *t = convert_to_base (TREE_OPERAND (*t, 0), basetype,
3284 /*check_access=*/false, /*nonnull=*/true,
3285 tf_warning_or_error);
3287 else if (cxx_dialect >= cxx20
3288 && (TREE_CODE (*t) == CALL_EXPR
3289 || TREE_CODE (*t) == AGGR_INIT_EXPR)
3290 && !in_immediate_context ())
3292 /* Expand immediate invocations. */
3293 if (tree fndecl = cp_get_callee_fndecl_nofold (*t))
3294 if (DECL_IMMEDIATE_FUNCTION_P (fndecl))
3296 /* Make in_immediate_context true within the args. */
3297 in_consteval_if_p_temp_override ito;
3298 in_consteval_if_p = true;
3299 int nargs = call_expr_nargs (*t);
3300 for (int i = 0; i < nargs; ++i)
3301 cp_walk_tree (&get_nth_callarg (*t, i), bot_replace, data_, NULL);
3302 *t = cxx_constant_value (*t);
3303 if (*t == error_mark_node)
3304 return error_mark_node;
3305 *walk_subtrees = 0;
3309 return NULL_TREE;
3312 /* When we parse a default argument expression, we may create
3313 temporary variables via TARGET_EXPRs. When we actually use the
3314 default-argument expression, we make a copy of the expression
3315 and replace the temporaries with appropriate local versions.
3317 If CLEAR_LOCATION is true, override any EXPR_LOCATION with
3318 input_location. */
3320 tree
3321 break_out_target_exprs (tree t, bool clear_location /* = false */)
3323 static int target_remap_count;
3324 static splay_tree target_remap;
3326 /* We shouldn't be called on templated trees, nor do we want to
3327 produce them. */
3328 gcc_checking_assert (!processing_template_decl);
3330 if (!target_remap_count++)
3331 target_remap = splay_tree_new (splay_tree_compare_pointers,
3332 /*splay_tree_delete_key_fn=*/NULL,
3333 /*splay_tree_delete_value_fn=*/NULL);
3334 bot_data data = { target_remap, clear_location };
3335 if (cp_walk_tree (&t, bot_manip, &data, NULL) == error_mark_node)
3336 t = error_mark_node;
3337 if (cp_walk_tree (&t, bot_replace, &data, NULL) == error_mark_node)
3338 t = error_mark_node;
3340 if (!--target_remap_count)
3342 splay_tree_delete (target_remap);
3343 target_remap = NULL;
3346 return t;
3349 /* Build an expression for the subobject of OBJ at CONSTRUCTOR index INDEX,
3350 which we expect to have type TYPE. */
3352 tree
3353 build_ctor_subob_ref (tree index, tree type, tree obj)
3355 if (index == NULL_TREE)
3356 /* Can't refer to a particular member of a vector. */
3357 obj = NULL_TREE;
3358 else if (TREE_CODE (index) == INTEGER_CST)
3359 obj = cp_build_array_ref (input_location, obj, index, tf_none);
3360 else
3361 obj = build_class_member_access_expr (obj, index, NULL_TREE,
3362 /*reference*/false, tf_none);
3363 if (obj)
3365 tree objtype = TREE_TYPE (obj);
3366 if (TREE_CODE (objtype) == ARRAY_TYPE && !TYPE_DOMAIN (objtype))
3368 /* When the destination object refers to a flexible array member
3369 verify that it matches the type of the source object except
3370 for its domain and qualifiers. */
3371 gcc_assert (comptypes (TYPE_MAIN_VARIANT (type),
3372 TYPE_MAIN_VARIANT (objtype),
3373 COMPARE_REDECLARATION));
3375 else
3376 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, objtype));
3379 return obj;
3382 struct replace_placeholders_t
3384 tree obj; /* The object to be substituted for a PLACEHOLDER_EXPR. */
3385 tree exp; /* The outermost exp. */
3386 bool seen; /* Whether we've encountered a PLACEHOLDER_EXPR. */
3387 hash_set<tree> *pset; /* To avoid walking same trees multiple times. */
3390 /* Like substitute_placeholder_in_expr, but handle C++ tree codes and
3391 build up subexpressions as we go deeper. */
3393 static tree
3394 replace_placeholders_r (tree* t, int* walk_subtrees, void* data_)
3396 replace_placeholders_t *d = static_cast<replace_placeholders_t*>(data_);
3397 tree obj = d->obj;
3399 if (TYPE_P (*t) || TREE_CONSTANT (*t))
3401 *walk_subtrees = false;
3402 return NULL_TREE;
3405 switch (TREE_CODE (*t))
3407 case PLACEHOLDER_EXPR:
3409 tree x = obj;
3410 for (; !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (*t),
3411 TREE_TYPE (x));
3412 x = TREE_OPERAND (x, 0))
3413 gcc_assert (handled_component_p (x));
3414 *t = unshare_expr (x);
3415 *walk_subtrees = false;
3416 d->seen = true;
3418 break;
3420 case CONSTRUCTOR:
3422 constructor_elt *ce;
3423 vec<constructor_elt,va_gc> *v = CONSTRUCTOR_ELTS (*t);
3424 /* Don't walk into CONSTRUCTOR_PLACEHOLDER_BOUNDARY ctors
3425 other than the d->exp one, those have PLACEHOLDER_EXPRs
3426 related to another object. */
3427 if ((CONSTRUCTOR_PLACEHOLDER_BOUNDARY (*t)
3428 && *t != d->exp)
3429 || d->pset->add (*t))
3431 *walk_subtrees = false;
3432 return NULL_TREE;
3434 for (unsigned i = 0; vec_safe_iterate (v, i, &ce); ++i)
3436 tree *valp = &ce->value;
3437 tree type = TREE_TYPE (*valp);
3438 tree subob = obj;
3440 /* Elements with RANGE_EXPR index shouldn't have any
3441 placeholders in them. */
3442 if (ce->index && TREE_CODE (ce->index) == RANGE_EXPR)
3443 continue;
3445 if (TREE_CODE (*valp) == CONSTRUCTOR
3446 && AGGREGATE_TYPE_P (type))
3448 /* If we're looking at the initializer for OBJ, then build
3449 a sub-object reference. If we're looking at an
3450 initializer for another object, just pass OBJ down. */
3451 if (same_type_ignoring_top_level_qualifiers_p
3452 (TREE_TYPE (*t), TREE_TYPE (obj)))
3453 subob = build_ctor_subob_ref (ce->index, type, obj);
3454 if (TREE_CODE (*valp) == TARGET_EXPR)
3455 valp = &TARGET_EXPR_INITIAL (*valp);
3457 d->obj = subob;
3458 cp_walk_tree (valp, replace_placeholders_r, data_, NULL);
3459 d->obj = obj;
3461 *walk_subtrees = false;
3462 break;
3465 default:
3466 if (d->pset->add (*t))
3467 *walk_subtrees = false;
3468 break;
3471 return NULL_TREE;
3474 /* Replace PLACEHOLDER_EXPRs in EXP with object OBJ. SEEN_P is set if
3475 a PLACEHOLDER_EXPR has been encountered. */
3477 tree
3478 replace_placeholders (tree exp, tree obj, bool *seen_p /*= NULL*/)
3480 /* This is only relevant for C++14. */
3481 if (cxx_dialect < cxx14)
3482 return exp;
3484 /* If the object isn't a (member of a) class, do nothing. */
3485 tree op0 = obj;
3486 while (handled_component_p (op0))
3487 op0 = TREE_OPERAND (op0, 0);
3488 if (!CLASS_TYPE_P (strip_array_types (TREE_TYPE (op0))))
3489 return exp;
3491 tree *tp = &exp;
3492 if (TREE_CODE (exp) == TARGET_EXPR)
3493 tp = &TARGET_EXPR_INITIAL (exp);
3494 hash_set<tree> pset;
3495 replace_placeholders_t data = { obj, *tp, false, &pset };
3496 cp_walk_tree (tp, replace_placeholders_r, &data, NULL);
3497 if (seen_p)
3498 *seen_p = data.seen;
3499 return exp;
3502 /* Callback function for find_placeholders. */
3504 static tree
3505 find_placeholders_r (tree *t, int *walk_subtrees, void *)
3507 if (TYPE_P (*t) || TREE_CONSTANT (*t))
3509 *walk_subtrees = false;
3510 return NULL_TREE;
3513 switch (TREE_CODE (*t))
3515 case PLACEHOLDER_EXPR:
3516 return *t;
3518 case CONSTRUCTOR:
3519 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (*t))
3520 *walk_subtrees = false;
3521 break;
3523 default:
3524 break;
3527 return NULL_TREE;
3530 /* Return true if EXP contains a PLACEHOLDER_EXPR. Don't walk into
3531 ctors with CONSTRUCTOR_PLACEHOLDER_BOUNDARY flag set. */
3533 bool
3534 find_placeholders (tree exp)
3536 /* This is only relevant for C++14. */
3537 if (cxx_dialect < cxx14)
3538 return false;
3540 return cp_walk_tree_without_duplicates (&exp, find_placeholders_r, NULL);
3543 /* Similar to `build_nt', but for template definitions of dependent
3544 expressions */
3546 tree
3547 build_min_nt_loc (location_t loc, enum tree_code code, ...)
3549 tree t;
3550 int length;
3551 int i;
3552 va_list p;
3554 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3556 va_start (p, code);
3558 t = make_node (code);
3559 SET_EXPR_LOCATION (t, loc);
3560 length = TREE_CODE_LENGTH (code);
3562 for (i = 0; i < length; i++)
3563 TREE_OPERAND (t, i) = va_arg (p, tree);
3565 va_end (p);
3566 return t;
3569 /* Similar to `build', but for template definitions. */
3571 tree
3572 build_min (enum tree_code code, tree tt, ...)
3574 tree t;
3575 int length;
3576 int i;
3577 va_list p;
3579 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3581 va_start (p, tt);
3583 t = make_node (code);
3584 length = TREE_CODE_LENGTH (code);
3585 TREE_TYPE (t) = tt;
3587 for (i = 0; i < length; i++)
3589 tree x = va_arg (p, tree);
3590 TREE_OPERAND (t, i) = x;
3591 if (x && !TYPE_P (x) && TREE_SIDE_EFFECTS (x))
3592 TREE_SIDE_EFFECTS (t) = 1;
3595 va_end (p);
3597 return t;
3600 /* Similar to `build', but for template definitions of non-dependent
3601 expressions. NON_DEP is the non-dependent expression that has been
3602 built. */
3604 tree
3605 build_min_non_dep (enum tree_code code, tree non_dep, ...)
3607 tree t;
3608 int length;
3609 int i;
3610 va_list p;
3612 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3614 va_start (p, non_dep);
3616 if (REFERENCE_REF_P (non_dep))
3617 non_dep = TREE_OPERAND (non_dep, 0);
3619 t = make_node (code);
3620 SET_EXPR_LOCATION (t, cp_expr_loc_or_input_loc (non_dep));
3621 length = TREE_CODE_LENGTH (code);
3622 TREE_TYPE (t) = unlowered_expr_type (non_dep);
3623 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
3625 for (i = 0; i < length; i++)
3626 TREE_OPERAND (t, i) = va_arg (p, tree);
3628 va_end (p);
3629 return convert_from_reference (t);
3632 /* Similar to build_min_nt, but call expressions */
3634 tree
3635 build_min_nt_call_vec (tree fn, vec<tree, va_gc> *args)
3637 tree ret, t;
3638 unsigned int ix;
3640 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
3641 CALL_EXPR_FN (ret) = fn;
3642 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
3643 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
3644 CALL_EXPR_ARG (ret, ix) = t;
3646 return ret;
3649 /* Similar to `build_min_nt_call_vec', but for template definitions of
3650 non-dependent expressions. NON_DEP is the non-dependent expression
3651 that has been built. */
3653 tree
3654 build_min_non_dep_call_vec (tree non_dep, tree fn, vec<tree, va_gc> *argvec)
3656 tree t = build_min_nt_call_vec (fn, argvec);
3657 if (REFERENCE_REF_P (non_dep))
3658 non_dep = TREE_OPERAND (non_dep, 0);
3659 TREE_TYPE (t) = TREE_TYPE (non_dep);
3660 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
3661 return convert_from_reference (t);
3664 /* Similar to build_min_non_dep, but for expressions that have been resolved to
3665 a call to an operator overload. OP is the operator that has been
3666 overloaded. NON_DEP is the non-dependent expression that's been built,
3667 which should be a CALL_EXPR or an INDIRECT_REF to a CALL_EXPR. OVERLOAD is
3668 the overload that NON_DEP is calling. */
3670 tree
3671 build_min_non_dep_op_overload (enum tree_code op,
3672 tree non_dep,
3673 tree overload, ...)
3675 va_list p;
3676 int nargs, expected_nargs;
3677 tree fn, call, obj = NULL_TREE;
3679 non_dep = extract_call_expr (non_dep);
3681 nargs = call_expr_nargs (non_dep);
3683 expected_nargs = cp_tree_code_length (op);
3684 if (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE
3685 /* For ARRAY_REF, operator[] is either a non-static member or newly
3686 static member, never out of class and for the static member case
3687 if user uses single index the operator[] needs to have a single
3688 argument as well, but the function is called with 2 - the object
3689 it is invoked on and the index. */
3690 || op == ARRAY_REF)
3691 expected_nargs -= 1;
3692 if ((op == POSTINCREMENT_EXPR
3693 || op == POSTDECREMENT_EXPR)
3694 /* With -fpermissive non_dep could be operator++(). */
3695 && (!flag_permissive || nargs != expected_nargs))
3696 expected_nargs += 1;
3697 gcc_assert (nargs == expected_nargs);
3699 releasing_vec args;
3700 va_start (p, overload);
3702 if (TREE_CODE (TREE_TYPE (overload)) == FUNCTION_TYPE)
3704 fn = overload;
3705 if (op == ARRAY_REF)
3706 obj = va_arg (p, tree);
3707 for (int i = 0; i < nargs; i++)
3709 tree arg = va_arg (p, tree);
3710 vec_safe_push (args, arg);
3713 else if (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE)
3715 tree object = va_arg (p, tree);
3716 tree binfo = TYPE_BINFO (TREE_TYPE (object));
3717 tree method = build_baselink (binfo, binfo, overload, NULL_TREE);
3718 fn = build_min (COMPONENT_REF, TREE_TYPE (overload),
3719 object, method, NULL_TREE);
3720 for (int i = 0; i < nargs; i++)
3722 tree arg = va_arg (p, tree);
3723 vec_safe_push (args, arg);
3726 else
3727 gcc_unreachable ();
3729 va_end (p);
3730 call = build_min_non_dep_call_vec (non_dep, fn, args);
3732 tree call_expr = extract_call_expr (call);
3733 KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep);
3734 CALL_EXPR_OPERATOR_SYNTAX (call_expr) = true;
3735 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep);
3736 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep);
3738 if (obj)
3739 return keep_unused_object_arg (call, obj, overload);
3740 return call;
3743 /* Similar to above build_min_non_dep_op_overload, but arguments
3744 are taken from ARGS vector. */
3746 tree
3747 build_min_non_dep_op_overload (tree non_dep, tree overload, tree object,
3748 vec<tree, va_gc> *args)
3750 non_dep = extract_call_expr (non_dep);
3752 unsigned int nargs = call_expr_nargs (non_dep);
3753 tree fn = overload;
3754 if (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE)
3756 tree binfo = TYPE_BINFO (TREE_TYPE (object));
3757 tree method = build_baselink (binfo, binfo, overload, NULL_TREE);
3758 fn = build_min (COMPONENT_REF, TREE_TYPE (overload),
3759 object, method, NULL_TREE);
3760 object = NULL_TREE;
3762 gcc_assert (vec_safe_length (args) == nargs);
3764 tree call = build_min_non_dep_call_vec (non_dep, fn, args);
3766 tree call_expr = extract_call_expr (call);
3767 KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep);
3768 CALL_EXPR_OPERATOR_SYNTAX (call_expr) = true;
3769 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep);
3770 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep);
3772 if (object)
3773 return keep_unused_object_arg (call, object, overload);
3774 return call;
3777 /* Return a new tree vec copied from VEC, with ELT inserted at index IDX. */
3779 vec<tree, va_gc> *
3780 vec_copy_and_insert (vec<tree, va_gc> *old_vec, tree elt, unsigned idx)
3782 unsigned len = vec_safe_length (old_vec);
3783 gcc_assert (idx <= len);
3785 vec<tree, va_gc> *new_vec = NULL;
3786 vec_alloc (new_vec, len + 1);
3788 unsigned i;
3789 for (i = 0; i < len; ++i)
3791 if (i == idx)
3792 new_vec->quick_push (elt);
3793 new_vec->quick_push ((*old_vec)[i]);
3795 if (i == idx)
3796 new_vec->quick_push (elt);
3798 return new_vec;
3801 tree
3802 get_type_decl (tree t)
3804 if (TREE_CODE (t) == TYPE_DECL)
3805 return t;
3806 if (TYPE_P (t))
3807 return TYPE_STUB_DECL (t);
3808 gcc_assert (t == error_mark_node);
3809 return t;
3812 /* Returns the namespace that contains DECL, whether directly or
3813 indirectly. */
3815 tree
3816 decl_namespace_context (tree decl)
3818 while (1)
3820 if (TREE_CODE (decl) == NAMESPACE_DECL)
3821 return decl;
3822 else if (TYPE_P (decl))
3823 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
3824 else
3825 decl = CP_DECL_CONTEXT (decl);
3829 /* Returns true if decl is within an anonymous namespace, however deeply
3830 nested, or false otherwise. */
3832 bool
3833 decl_anon_ns_mem_p (tree decl)
3835 return !TREE_PUBLIC (decl_namespace_context (decl));
3838 /* Returns true if the enclosing scope of DECL has internal or no linkage. */
3840 bool
3841 decl_internal_context_p (const_tree decl)
3843 while (TREE_CODE (decl) != NAMESPACE_DECL)
3845 /* Classes inside anonymous namespaces have TREE_PUBLIC == 0. */
3846 if (TYPE_P (decl))
3847 return !TREE_PUBLIC (TYPE_MAIN_DECL (decl));
3849 decl = CP_DECL_CONTEXT (decl);
3851 return !TREE_PUBLIC (decl);
3854 /* Subroutine of cp_tree_equal: t1 and t2 are two CALL_EXPRs.
3855 Return whether their CALL_EXPR_FNs are equivalent. */
3857 static bool
3858 called_fns_equal (tree t1, tree t2)
3860 /* Core 1321: dependent names are equivalent even if the overload sets
3861 are different. But do compare explicit template arguments. */
3862 tree name1 = call_expr_dependent_name (t1);
3863 tree name2 = call_expr_dependent_name (t2);
3864 t1 = CALL_EXPR_FN (t1);
3865 t2 = CALL_EXPR_FN (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 bool comparing_override_contracts;
3894 /* In a component reference, return the innermost object of
3895 the postfix-expression. */
3897 static tree
3898 get_innermost_component (tree t)
3900 gcc_assert (TREE_CODE (t) == COMPONENT_REF);
3901 while (TREE_CODE (t) == COMPONENT_REF)
3902 t = TREE_OPERAND (t, 0);
3903 return t;
3906 /* Returns true if T is a possibly converted 'this' or '*this' expression. */
3908 static bool
3909 is_this_expression (tree t)
3911 t = get_innermost_component (t);
3912 /* See through deferences and no-op conversions. */
3913 if (INDIRECT_REF_P (t))
3914 t = TREE_OPERAND (t, 0);
3915 if (TREE_CODE (t) == NOP_EXPR)
3916 t = TREE_OPERAND (t, 0);
3917 return is_this_parameter (t);
3920 static bool
3921 comparing_this_references (tree t1, tree t2)
3923 return is_this_expression (t1) && is_this_expression (t2);
3926 static bool
3927 equivalent_member_references (tree t1, tree t2)
3929 if (!comparing_this_references (t1, t2))
3930 return false;
3931 t1 = TREE_OPERAND (t1, 1);
3932 t2 = TREE_OPERAND (t2, 1);
3933 return t1 == t2;
3936 /* Return truthvalue of whether T1 is the same tree structure as T2.
3937 Return 1 if they are the same. Return 0 if they are different. */
3939 bool
3940 cp_tree_equal (tree t1, tree t2)
3942 enum tree_code code1, code2;
3944 if (t1 == t2)
3945 return true;
3946 if (!t1 || !t2)
3947 return false;
3949 code1 = TREE_CODE (t1);
3950 code2 = TREE_CODE (t2);
3952 if (code1 != code2)
3953 return false;
3955 if (CONSTANT_CLASS_P (t1)
3956 && !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3957 return false;
3959 switch (code1)
3961 case VOID_CST:
3962 /* There's only a single VOID_CST node, so we should never reach
3963 here. */
3964 gcc_unreachable ();
3966 case INTEGER_CST:
3967 return tree_int_cst_equal (t1, t2);
3969 case REAL_CST:
3970 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
3972 case STRING_CST:
3973 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3974 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3975 TREE_STRING_LENGTH (t1));
3977 case FIXED_CST:
3978 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
3979 TREE_FIXED_CST (t2));
3981 case COMPLEX_CST:
3982 return cp_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
3983 && cp_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
3985 case VECTOR_CST:
3986 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
3988 case CONSTRUCTOR:
3989 /* We need to do this when determining whether or not two
3990 non-type pointer to member function template arguments
3991 are the same. */
3992 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
3993 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
3994 return false;
3996 tree field, value;
3997 unsigned int i;
3998 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
4000 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
4001 if (!cp_tree_equal (field, elt2->index)
4002 || !cp_tree_equal (value, elt2->value))
4003 return false;
4006 return true;
4008 case TREE_LIST:
4009 if (!cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
4010 return false;
4011 if (!cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
4012 return false;
4013 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
4015 case SAVE_EXPR:
4016 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
4018 case CALL_EXPR:
4020 if (KOENIG_LOOKUP_P (t1) != KOENIG_LOOKUP_P (t2))
4021 return false;
4023 if (!called_fns_equal (t1, t2))
4024 return false;
4026 call_expr_arg_iterator iter1, iter2;
4027 init_call_expr_arg_iterator (t1, &iter1);
4028 init_call_expr_arg_iterator (t2, &iter2);
4029 if (iter1.n != iter2.n)
4030 return false;
4032 while (more_call_expr_args_p (&iter1))
4034 tree arg1 = next_call_expr_arg (&iter1);
4035 tree arg2 = next_call_expr_arg (&iter2);
4037 gcc_checking_assert (arg1 && arg2);
4038 if (!cp_tree_equal (arg1, arg2))
4039 return false;
4042 return true;
4045 case TARGET_EXPR:
4047 tree o1 = TREE_OPERAND (t1, 0);
4048 tree o2 = TREE_OPERAND (t2, 0);
4050 /* Special case: if either target is an unallocated VAR_DECL,
4051 it means that it's going to be unified with whatever the
4052 TARGET_EXPR is really supposed to initialize, so treat it
4053 as being equivalent to anything. */
4054 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
4055 && !DECL_RTL_SET_P (o1))
4056 /*Nop*/;
4057 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
4058 && !DECL_RTL_SET_P (o2))
4059 /*Nop*/;
4060 else if (!cp_tree_equal (o1, o2))
4061 return false;
4063 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
4066 case PARM_DECL:
4067 /* For comparing uses of parameters in late-specified return types
4068 with an out-of-class definition of the function, but can also come
4069 up for expressions that involve 'this' in a member function
4070 template. */
4072 if (comparing_specializations
4073 && DECL_CONTEXT (t1) != DECL_CONTEXT (t2))
4074 /* When comparing hash table entries, only an exact match is
4075 good enough; we don't want to replace 'this' with the
4076 version from another function. But be more flexible
4077 with parameters with identical contexts. */
4078 return false;
4080 if (same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
4082 if (DECL_ARTIFICIAL (t1) ^ DECL_ARTIFICIAL (t2))
4083 return false;
4084 if (CONSTRAINT_VAR_P (t1) ^ CONSTRAINT_VAR_P (t2))
4085 return false;
4086 if (DECL_ARTIFICIAL (t1)
4087 || (DECL_PARM_LEVEL (t1) == DECL_PARM_LEVEL (t2)
4088 && DECL_PARM_INDEX (t1) == DECL_PARM_INDEX (t2)))
4089 return true;
4091 return false;
4093 case VAR_DECL:
4094 case CONST_DECL:
4095 case FIELD_DECL:
4096 case FUNCTION_DECL:
4097 case TEMPLATE_DECL:
4098 case IDENTIFIER_NODE:
4099 case SSA_NAME:
4100 case USING_DECL:
4101 case DEFERRED_PARSE:
4102 return false;
4104 case BASELINK:
4105 return (BASELINK_BINFO (t1) == BASELINK_BINFO (t2)
4106 && BASELINK_ACCESS_BINFO (t1) == BASELINK_ACCESS_BINFO (t2)
4107 && BASELINK_QUALIFIED_P (t1) == BASELINK_QUALIFIED_P (t2)
4108 && cp_tree_equal (BASELINK_FUNCTIONS (t1),
4109 BASELINK_FUNCTIONS (t2)));
4111 case TEMPLATE_PARM_INDEX:
4112 return (TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
4113 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2)
4114 && (TEMPLATE_PARM_PARAMETER_PACK (t1)
4115 == TEMPLATE_PARM_PARAMETER_PACK (t2))
4116 && same_type_p (TREE_TYPE (TEMPLATE_PARM_DECL (t1)),
4117 TREE_TYPE (TEMPLATE_PARM_DECL (t2))));
4119 case TEMPLATE_ID_EXPR:
4120 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
4121 return false;
4122 if (!comp_template_args (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)))
4123 return false;
4124 return true;
4126 case CONSTRAINT_INFO:
4127 return cp_tree_equal (CI_ASSOCIATED_CONSTRAINTS (t1),
4128 CI_ASSOCIATED_CONSTRAINTS (t2));
4130 case CHECK_CONSTR:
4131 return (CHECK_CONSTR_CONCEPT (t1) == CHECK_CONSTR_CONCEPT (t2)
4132 && comp_template_args (CHECK_CONSTR_ARGS (t1),
4133 CHECK_CONSTR_ARGS (t2)));
4135 case TREE_VEC:
4136 /* These are template args. Really we should be getting the
4137 caller to do this as it knows it to be true. */
4138 if (!comp_template_args (t1, t2, NULL, NULL, false))
4139 return false;
4140 return true;
4142 case SIZEOF_EXPR:
4143 case ALIGNOF_EXPR:
4145 tree o1 = TREE_OPERAND (t1, 0);
4146 tree o2 = TREE_OPERAND (t2, 0);
4148 if (code1 == SIZEOF_EXPR)
4150 if (SIZEOF_EXPR_TYPE_P (t1))
4151 o1 = TREE_TYPE (o1);
4152 if (SIZEOF_EXPR_TYPE_P (t2))
4153 o2 = TREE_TYPE (o2);
4155 else if (ALIGNOF_EXPR_STD_P (t1) != ALIGNOF_EXPR_STD_P (t2))
4156 return false;
4158 if (TREE_CODE (o1) != TREE_CODE (o2))
4159 return false;
4161 if (ARGUMENT_PACK_P (o1))
4162 return template_args_equal (o1, o2);
4163 else if (TYPE_P (o1))
4164 return same_type_p (o1, o2);
4165 else
4166 return cp_tree_equal (o1, o2);
4169 case MODOP_EXPR:
4171 tree t1_op1, t2_op1;
4173 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
4174 return false;
4176 t1_op1 = TREE_OPERAND (t1, 1);
4177 t2_op1 = TREE_OPERAND (t2, 1);
4178 if (TREE_CODE (t1_op1) != TREE_CODE (t2_op1))
4179 return false;
4181 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t2, 2));
4184 case PTRMEM_CST:
4185 /* Two pointer-to-members are the same if they point to the same
4186 field or function in the same class. */
4187 if (PTRMEM_CST_MEMBER (t1) != PTRMEM_CST_MEMBER (t2))
4188 return false;
4190 return same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2));
4192 case OVERLOAD:
4194 /* Two overloads. Must be exactly the same set of decls. */
4195 lkp_iterator first (t1);
4196 lkp_iterator second (t2);
4198 for (; first && second; ++first, ++second)
4199 if (*first != *second)
4200 return false;
4201 return !(first || second);
4204 case TRAIT_EXPR:
4205 if (TRAIT_EXPR_KIND (t1) != TRAIT_EXPR_KIND (t2))
4206 return false;
4207 return cp_tree_equal (TRAIT_EXPR_TYPE1 (t1), TRAIT_EXPR_TYPE1 (t2))
4208 && cp_tree_equal (TRAIT_EXPR_TYPE2 (t1), TRAIT_EXPR_TYPE2 (t2));
4210 case NON_LVALUE_EXPR:
4211 case VIEW_CONVERT_EXPR:
4212 /* Used for location wrappers with possibly NULL types. */
4213 if (!TREE_TYPE (t1) || !TREE_TYPE (t2))
4215 if (TREE_TYPE (t1) || TREE_TYPE (t2))
4216 return false;
4217 break;
4219 /* FALLTHROUGH */
4221 case CAST_EXPR:
4222 case STATIC_CAST_EXPR:
4223 case REINTERPRET_CAST_EXPR:
4224 case CONST_CAST_EXPR:
4225 case DYNAMIC_CAST_EXPR:
4226 case IMPLICIT_CONV_EXPR:
4227 case NEW_EXPR:
4228 case BIT_CAST_EXPR:
4229 CASE_CONVERT:
4230 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
4231 return false;
4232 /* Now compare operands as usual. */
4233 break;
4235 case DEFERRED_NOEXCEPT:
4236 return (cp_tree_equal (DEFERRED_NOEXCEPT_PATTERN (t1),
4237 DEFERRED_NOEXCEPT_PATTERN (t2))
4238 && comp_template_args (DEFERRED_NOEXCEPT_ARGS (t1),
4239 DEFERRED_NOEXCEPT_ARGS (t2)));
4241 case LAMBDA_EXPR:
4242 /* Two lambda-expressions are never considered equivalent. */
4243 return false;
4245 case TYPE_ARGUMENT_PACK:
4246 case NONTYPE_ARGUMENT_PACK:
4248 tree p1 = ARGUMENT_PACK_ARGS (t1);
4249 tree p2 = ARGUMENT_PACK_ARGS (t2);
4250 int len = TREE_VEC_LENGTH (p1);
4251 if (TREE_VEC_LENGTH (p2) != len)
4252 return false;
4254 for (int ix = 0; ix != len; ix++)
4255 if (!template_args_equal (TREE_VEC_ELT (p1, ix),
4256 TREE_VEC_ELT (p2, ix)))
4257 return false;
4258 return true;
4261 case EXPR_PACK_EXPANSION:
4262 if (!cp_tree_equal (PACK_EXPANSION_PATTERN (t1),
4263 PACK_EXPANSION_PATTERN (t2)))
4264 return false;
4265 if (!comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
4266 PACK_EXPANSION_EXTRA_ARGS (t2)))
4267 return false;
4268 return true;
4270 case COMPONENT_REF:
4271 /* If we're comparing contract conditions of overrides, member references
4272 compare equal if they designate the same member. */
4273 if (comparing_override_contracts)
4274 return equivalent_member_references (t1, t2);
4275 break;
4277 default:
4278 break;
4281 switch (TREE_CODE_CLASS (code1))
4283 case tcc_unary:
4284 case tcc_binary:
4285 case tcc_comparison:
4286 case tcc_expression:
4287 case tcc_vl_exp:
4288 case tcc_reference:
4289 case tcc_statement:
4291 int n = cp_tree_operand_length (t1);
4292 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
4293 && n != TREE_OPERAND_LENGTH (t2))
4294 return false;
4296 for (int i = 0; i < n; ++i)
4297 if (!cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
4298 return false;
4300 return true;
4303 case tcc_type:
4304 return same_type_p (t1, t2);
4306 default:
4307 gcc_unreachable ();
4310 /* We can get here with --disable-checking. */
4311 return false;
4314 /* The type of ARG when used as an lvalue. */
4316 tree
4317 lvalue_type (tree arg)
4319 tree type = TREE_TYPE (arg);
4320 return type;
4323 /* The type of ARG for printing error messages; denote lvalues with
4324 reference types. */
4326 tree
4327 error_type (tree arg)
4329 tree type = TREE_TYPE (arg);
4331 if (TREE_CODE (type) == ARRAY_TYPE)
4333 else if (TREE_CODE (type) == ERROR_MARK)
4335 else if (lvalue_p (arg))
4336 type = build_reference_type (lvalue_type (arg));
4337 else if (MAYBE_CLASS_TYPE_P (type))
4338 type = lvalue_type (arg);
4340 return type;
4343 /* Does FUNCTION use a variable-length argument list? */
4346 varargs_function_p (const_tree function)
4348 return stdarg_p (TREE_TYPE (function));
4351 /* Returns 1 if decl is a member of a class. */
4354 member_p (const_tree decl)
4356 const_tree const ctx = DECL_CONTEXT (decl);
4357 return (ctx && TYPE_P (ctx));
4360 /* Create a placeholder for member access where we don't actually have an
4361 object that the access is against. For a general declval<T> equivalent,
4362 use build_stub_object instead. */
4364 tree
4365 build_dummy_object (tree type)
4367 tree decl = build1 (CONVERT_EXPR, build_pointer_type (type), void_node);
4368 return cp_build_fold_indirect_ref (decl);
4371 /* We've gotten a reference to a member of TYPE. Return *this if appropriate,
4372 or a dummy object otherwise. If BINFOP is non-0, it is filled with the
4373 binfo path from current_class_type to TYPE, or 0. */
4375 tree
4376 maybe_dummy_object (tree type, tree* binfop)
4378 tree decl, context;
4379 tree binfo;
4380 tree current = current_nonlambda_class_type ();
4382 if (current
4383 && (binfo = lookup_base (current, type, ba_any, NULL,
4384 tf_warning_or_error)))
4385 context = current;
4386 else
4388 /* Reference from a nested class member function. */
4389 context = type;
4390 binfo = TYPE_BINFO (type);
4393 if (binfop)
4394 *binfop = binfo;
4396 /* current_class_ref might not correspond to current_class_type if
4397 we're in tsubst_default_argument or a lambda-declarator; in either
4398 case, we want to use current_class_ref if it matches CONTEXT. */
4399 tree ctype = current_class_ref ? TREE_TYPE (current_class_ref) : NULL_TREE;
4400 if (ctype
4401 && same_type_ignoring_top_level_qualifiers_p (ctype, context))
4402 decl = current_class_ref;
4403 else
4405 /* Return a dummy object whose cv-quals are consistent with (the
4406 non-lambda) 'this' if available. */
4407 if (ctype)
4409 int quals = TYPE_UNQUALIFIED;
4410 if (tree lambda = CLASSTYPE_LAMBDA_EXPR (ctype))
4412 if (tree cap = lambda_expr_this_capture (lambda, false))
4413 quals = cp_type_quals (TREE_TYPE (TREE_TYPE (cap)));
4415 else
4416 quals = cp_type_quals (ctype);
4417 context = cp_build_qualified_type (context, quals);
4419 decl = build_dummy_object (context);
4422 return decl;
4425 /* Returns 1 if OB is a placeholder object, or a pointer to one. */
4427 bool
4428 is_dummy_object (const_tree ob)
4430 if (INDIRECT_REF_P (ob))
4431 ob = TREE_OPERAND (ob, 0);
4432 return (TREE_CODE (ob) == CONVERT_EXPR
4433 && TREE_OPERAND (ob, 0) == void_node);
4436 /* Returns true if TYPE is char, unsigned char, or std::byte. */
4438 bool
4439 is_byte_access_type (tree type)
4441 type = TYPE_MAIN_VARIANT (type);
4442 if (type == char_type_node
4443 || type == unsigned_char_type_node)
4444 return true;
4446 return (TREE_CODE (type) == ENUMERAL_TYPE
4447 && TYPE_CONTEXT (type) == std_node
4448 && !strcmp ("byte", TYPE_NAME_STRING (type)));
4451 /* Returns true if TYPE is unsigned char or std::byte. */
4453 bool
4454 is_byte_access_type_not_plain_char (tree type)
4456 type = TYPE_MAIN_VARIANT (type);
4457 if (type == char_type_node)
4458 return false;
4460 return is_byte_access_type (type);
4463 /* Returns 1 iff type T is something we want to treat as a scalar type for
4464 the purpose of deciding whether it is trivial/POD/standard-layout. */
4466 bool
4467 scalarish_type_p (const_tree t)
4469 if (t == error_mark_node)
4470 return 1;
4472 return (SCALAR_TYPE_P (t) || VECTOR_TYPE_P (t));
4475 /* Returns true iff T requires non-trivial default initialization. */
4477 bool
4478 type_has_nontrivial_default_init (const_tree t)
4480 t = strip_array_types (CONST_CAST_TREE (t));
4482 if (CLASS_TYPE_P (t))
4483 return TYPE_HAS_COMPLEX_DFLT (t);
4484 else
4485 return 0;
4488 /* Track classes with only deleted copy/move constructors so that we can warn
4489 if they are used in call/return by value. */
4491 static GTY(()) hash_set<tree>* deleted_copy_types;
4492 static void
4493 remember_deleted_copy (const_tree t)
4495 if (!deleted_copy_types)
4496 deleted_copy_types = hash_set<tree>::create_ggc(37);
4497 deleted_copy_types->add (CONST_CAST_TREE (t));
4499 void
4500 maybe_warn_parm_abi (tree t, location_t loc)
4502 if (!deleted_copy_types
4503 || !deleted_copy_types->contains (t))
4504 return;
4506 if ((flag_abi_version == 12 || warn_abi_version == 12)
4507 && classtype_has_non_deleted_move_ctor (t))
4509 bool w;
4510 auto_diagnostic_group d;
4511 if (flag_abi_version > 12)
4512 w = warning_at (loc, OPT_Wabi, "%<-fabi-version=13%> (GCC 8.2) fixes "
4513 "the calling convention for %qT, which was "
4514 "accidentally changed in 8.1", t);
4515 else
4516 w = warning_at (loc, OPT_Wabi, "%<-fabi-version=12%> (GCC 8.1) "
4517 "accidentally changes the calling convention for %qT",
4519 if (w)
4520 inform (location_of (t), " declared here");
4521 return;
4524 auto_diagnostic_group d;
4525 if (warning_at (loc, OPT_Wabi, "the calling convention for %qT changes in "
4526 "%<-fabi-version=13%> (GCC 8.2)", t))
4527 inform (location_of (t), " because all of its copy and move "
4528 "constructors are deleted");
4531 /* Returns true iff copying an object of type T (including via move
4532 constructor) is non-trivial. That is, T has no non-trivial copy
4533 constructors and no non-trivial move constructors, and not all copy/move
4534 constructors are deleted. This function implements the ABI notion of
4535 non-trivial copy, which has diverged from the one in the standard. */
4537 bool
4538 type_has_nontrivial_copy_init (const_tree type)
4540 tree t = strip_array_types (CONST_CAST_TREE (type));
4542 if (CLASS_TYPE_P (t))
4544 gcc_assert (COMPLETE_TYPE_P (t));
4546 if (TYPE_HAS_COMPLEX_COPY_CTOR (t)
4547 || TYPE_HAS_COMPLEX_MOVE_CTOR (t))
4548 /* Nontrivial. */
4549 return true;
4551 if (cxx_dialect < cxx11)
4552 /* No deleted functions before C++11. */
4553 return false;
4555 /* Before ABI v12 we did a bitwise copy of types with only deleted
4556 copy/move constructors. */
4557 if (!abi_version_at_least (12)
4558 && !(warn_abi && abi_version_crosses (12)))
4559 return false;
4561 bool saw_copy = false;
4562 bool saw_non_deleted = false;
4563 bool saw_non_deleted_move = false;
4565 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
4566 saw_copy = saw_non_deleted = true;
4567 else if (CLASSTYPE_LAZY_COPY_CTOR (t))
4569 saw_copy = true;
4570 if (classtype_has_move_assign_or_move_ctor_p (t, true))
4571 /* [class.copy]/8 If the class definition declares a move
4572 constructor or move assignment operator, the implicitly declared
4573 copy constructor is defined as deleted.... */;
4574 else
4575 /* Any other reason the implicitly-declared function would be
4576 deleted would also cause TYPE_HAS_COMPLEX_COPY_CTOR to be
4577 set. */
4578 saw_non_deleted = true;
4581 if (!saw_non_deleted)
4582 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4584 tree fn = *iter;
4585 if (copy_fn_p (fn))
4587 saw_copy = true;
4588 if (!DECL_DELETED_FN (fn))
4590 /* Not deleted, therefore trivial. */
4591 saw_non_deleted = true;
4592 break;
4595 else if (move_fn_p (fn))
4596 if (!DECL_DELETED_FN (fn))
4597 saw_non_deleted_move = true;
4600 gcc_assert (saw_copy);
4602 /* ABI v12 buggily ignored move constructors. */
4603 bool v11nontriv = false;
4604 bool v12nontriv = !saw_non_deleted;
4605 bool v13nontriv = !saw_non_deleted && !saw_non_deleted_move;
4606 bool nontriv = (abi_version_at_least (13) ? v13nontriv
4607 : flag_abi_version == 12 ? v12nontriv
4608 : v11nontriv);
4609 bool warn_nontriv = (warn_abi_version >= 13 ? v13nontriv
4610 : warn_abi_version == 12 ? v12nontriv
4611 : v11nontriv);
4612 if (nontriv != warn_nontriv)
4613 remember_deleted_copy (t);
4615 return nontriv;
4617 else
4618 return 0;
4621 /* Returns 1 iff type T is a trivially copyable type, as defined in
4622 [basic.types] and [class]. */
4624 bool
4625 trivially_copyable_p (const_tree t)
4627 t = strip_array_types (CONST_CAST_TREE (t));
4629 if (CLASS_TYPE_P (t))
4630 return ((!TYPE_HAS_COPY_CTOR (t)
4631 || !TYPE_HAS_COMPLEX_COPY_CTOR (t))
4632 && !TYPE_HAS_COMPLEX_MOVE_CTOR (t)
4633 && (!TYPE_HAS_COPY_ASSIGN (t)
4634 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (t))
4635 && !TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
4636 && TYPE_HAS_TRIVIAL_DESTRUCTOR (t));
4637 else
4638 /* CWG 2094 makes volatile-qualified scalars trivially copyable again. */
4639 return scalarish_type_p (t);
4642 /* Returns 1 iff type T is a trivial type, as defined in [basic.types] and
4643 [class]. */
4645 bool
4646 trivial_type_p (const_tree t)
4648 t = strip_array_types (CONST_CAST_TREE (t));
4650 if (CLASS_TYPE_P (t))
4651 return (TYPE_HAS_TRIVIAL_DFLT (t)
4652 && trivially_copyable_p (t));
4653 else
4654 return scalarish_type_p (t);
4657 /* Returns 1 iff type T is a POD type, as defined in [basic.types]. */
4659 bool
4660 pod_type_p (const_tree t)
4662 /* This CONST_CAST is okay because strip_array_types returns its
4663 argument unmodified and we assign it to a const_tree. */
4664 t = strip_array_types (CONST_CAST_TREE(t));
4666 if (!CLASS_TYPE_P (t))
4667 return scalarish_type_p (t);
4668 else if (cxx_dialect > cxx98)
4669 /* [class]/10: A POD struct is a class that is both a trivial class and a
4670 standard-layout class, and has no non-static data members of type
4671 non-POD struct, non-POD union (or array of such types).
4673 We don't need to check individual members because if a member is
4674 non-std-layout or non-trivial, the class will be too. */
4675 return (std_layout_type_p (t) && trivial_type_p (t));
4676 else
4677 /* The C++98 definition of POD is different. */
4678 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
4681 /* Returns true iff T is POD for the purpose of layout, as defined in the
4682 C++ ABI. */
4684 bool
4685 layout_pod_type_p (const_tree t)
4687 t = strip_array_types (CONST_CAST_TREE (t));
4689 if (CLASS_TYPE_P (t))
4690 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
4691 else
4692 return scalarish_type_p (t);
4695 /* Returns true iff T is a standard-layout type, as defined in
4696 [basic.types]. */
4698 bool
4699 std_layout_type_p (const_tree t)
4701 t = strip_array_types (CONST_CAST_TREE (t));
4703 if (CLASS_TYPE_P (t))
4704 return !CLASSTYPE_NON_STD_LAYOUT (t);
4705 else
4706 return scalarish_type_p (t);
4709 static bool record_has_unique_obj_representations (const_tree, const_tree);
4711 /* Returns true iff T satisfies std::has_unique_object_representations<T>,
4712 as defined in [meta.unary.prop]. */
4714 bool
4715 type_has_unique_obj_representations (const_tree t)
4717 bool ret;
4719 t = strip_array_types (CONST_CAST_TREE (t));
4721 if (!trivially_copyable_p (t))
4722 return false;
4724 if (CLASS_TYPE_P (t) && CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t))
4725 return CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t);
4727 switch (TREE_CODE (t))
4729 case INTEGER_TYPE:
4730 case POINTER_TYPE:
4731 case REFERENCE_TYPE:
4732 /* If some backend has any paddings in these types, we should add
4733 a target hook for this and handle it there. */
4734 return true;
4736 case BOOLEAN_TYPE:
4737 /* For bool values other than 0 and 1 should only appear with
4738 undefined behavior. */
4739 return true;
4741 case ENUMERAL_TYPE:
4742 return type_has_unique_obj_representations (ENUM_UNDERLYING_TYPE (t));
4744 case REAL_TYPE:
4745 /* XFmode certainly contains padding on x86, which the CPU doesn't store
4746 when storing long double values, so for that we have to return false.
4747 Other kinds of floating point values are questionable due to +.0/-.0
4748 and NaNs, let's play safe for now. */
4749 return false;
4751 case FIXED_POINT_TYPE:
4752 return false;
4754 case OFFSET_TYPE:
4755 return true;
4757 case COMPLEX_TYPE:
4758 case VECTOR_TYPE:
4759 return type_has_unique_obj_representations (TREE_TYPE (t));
4761 case RECORD_TYPE:
4762 ret = record_has_unique_obj_representations (t, TYPE_SIZE (t));
4763 if (CLASS_TYPE_P (t))
4765 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
4766 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
4768 return ret;
4770 case UNION_TYPE:
4771 ret = true;
4772 bool any_fields;
4773 any_fields = false;
4774 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4775 if (TREE_CODE (field) == FIELD_DECL)
4777 any_fields = true;
4778 if (!type_has_unique_obj_representations (TREE_TYPE (field))
4779 || simple_cst_equal (DECL_SIZE (field), TYPE_SIZE (t)) != 1)
4781 ret = false;
4782 break;
4785 if (!any_fields && !integer_zerop (TYPE_SIZE (t)))
4786 ret = false;
4787 if (CLASS_TYPE_P (t))
4789 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
4790 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
4792 return ret;
4794 case NULLPTR_TYPE:
4795 return false;
4797 case ERROR_MARK:
4798 return false;
4800 default:
4801 gcc_unreachable ();
4805 /* Helper function for type_has_unique_obj_representations. */
4807 static bool
4808 record_has_unique_obj_representations (const_tree t, const_tree sz)
4810 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4811 if (TREE_CODE (field) != FIELD_DECL)
4813 /* For bases, can't use type_has_unique_obj_representations here, as in
4814 struct S { int i : 24; S (); };
4815 struct T : public S { int j : 8; T (); };
4816 S doesn't have unique obj representations, but T does. */
4817 else if (DECL_FIELD_IS_BASE (field))
4819 if (!record_has_unique_obj_representations (TREE_TYPE (field),
4820 DECL_SIZE (field)))
4821 return false;
4823 else if (DECL_C_BIT_FIELD (field) && !DECL_UNNAMED_BIT_FIELD (field))
4825 tree btype = DECL_BIT_FIELD_TYPE (field);
4826 if (!type_has_unique_obj_representations (btype))
4827 return false;
4829 else if (!type_has_unique_obj_representations (TREE_TYPE (field)))
4830 return false;
4832 offset_int cur = 0;
4833 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4834 if (TREE_CODE (field) == FIELD_DECL && !DECL_UNNAMED_BIT_FIELD (field))
4836 offset_int fld = wi::to_offset (DECL_FIELD_OFFSET (field));
4837 offset_int bitpos = wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
4838 fld = fld * BITS_PER_UNIT + bitpos;
4839 if (cur != fld)
4840 return false;
4841 if (DECL_SIZE (field))
4843 offset_int size = wi::to_offset (DECL_SIZE (field));
4844 cur += size;
4847 if (cur != wi::to_offset (sz))
4848 return false;
4850 return true;
4853 /* Nonzero iff type T is a class template implicit specialization. */
4855 bool
4856 class_tmpl_impl_spec_p (const_tree t)
4858 return CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INSTANTIATION (t);
4861 /* Returns 1 iff zero initialization of type T means actually storing
4862 zeros in it. */
4865 zero_init_p (const_tree t)
4867 /* This CONST_CAST is okay because strip_array_types returns its
4868 argument unmodified and we assign it to a const_tree. */
4869 t = strip_array_types (CONST_CAST_TREE(t));
4871 if (t == error_mark_node)
4872 return 1;
4874 /* NULL pointers to data members are initialized with -1. */
4875 if (TYPE_PTRDATAMEM_P (t))
4876 return 0;
4878 /* Classes that contain types that can't be zero-initialized, cannot
4879 be zero-initialized themselves. */
4880 if (CLASS_TYPE_P (t) && CLASSTYPE_NON_ZERO_INIT_P (t))
4881 return 0;
4883 return 1;
4886 /* Returns true if the expression or initializer T is the result of
4887 zero-initialization for its type, taking pointers to members
4888 into consideration. */
4890 bool
4891 zero_init_expr_p (tree t)
4893 tree type = TREE_TYPE (t);
4894 if (!type || uses_template_parms (type))
4895 return false;
4896 if (TYPE_PTRMEM_P (type))
4897 return null_member_pointer_value_p (t);
4898 if (TREE_CODE (t) == CONSTRUCTOR)
4900 if (COMPOUND_LITERAL_P (t)
4901 || BRACE_ENCLOSED_INITIALIZER_P (t))
4902 /* Undigested, conversions might change the zeroness. */
4903 return false;
4904 for (constructor_elt &elt : CONSTRUCTOR_ELTS (t))
4906 if (TREE_CODE (type) == UNION_TYPE
4907 && elt.index != first_field (type))
4908 return false;
4909 if (!zero_init_expr_p (elt.value))
4910 return false;
4912 return true;
4914 if (zero_init_p (type))
4915 return initializer_zerop (t);
4916 return false;
4919 /* True IFF T is a C++20 structural type (P1907R1) that can be used as a
4920 non-type template parameter. If EXPLAIN, explain why not. */
4922 bool
4923 structural_type_p (tree t, bool explain)
4925 /* A structural type is one of the following: */
4927 /* a scalar type, or */
4928 if (SCALAR_TYPE_P (t))
4929 return true;
4930 /* an lvalue reference type, or */
4931 if (TYPE_REF_P (t) && !TYPE_REF_IS_RVALUE (t))
4932 return true;
4933 /* a literal class type with the following properties:
4934 - all base classes and non-static data members are public and non-mutable
4936 - the types of all bases classes and non-static data members are
4937 structural types or (possibly multi-dimensional) array thereof. */
4938 if (!CLASS_TYPE_P (t))
4939 return false;
4940 if (!literal_type_p (t))
4942 if (explain)
4943 explain_non_literal_class (t);
4944 return false;
4946 for (tree m = next_aggregate_field (TYPE_FIELDS (t)); m;
4947 m = next_aggregate_field (DECL_CHAIN (m)))
4949 if (TREE_PRIVATE (m) || TREE_PROTECTED (m))
4951 if (explain)
4953 if (DECL_FIELD_IS_BASE (m))
4954 inform (location_of (m), "base class %qT is not public",
4955 TREE_TYPE (m));
4956 else
4957 inform (location_of (m), "%qD is not public", m);
4959 return false;
4961 if (DECL_MUTABLE_P (m))
4963 if (explain)
4964 inform (location_of (m), "%qD is mutable", m);
4965 return false;
4967 tree mtype = strip_array_types (TREE_TYPE (m));
4968 if (!structural_type_p (mtype))
4970 if (explain)
4972 inform (location_of (m), "%qD has a non-structural type", m);
4973 structural_type_p (mtype, true);
4975 return false;
4978 return true;
4981 /* Partially handle the C++11 [[carries_dependency]] attribute.
4982 Just emit a different diagnostics when it is used on something the
4983 spec doesn't allow vs. where it allows and we just choose to ignore
4984 it. */
4986 static tree
4987 handle_carries_dependency_attribute (tree *node, tree name,
4988 tree ARG_UNUSED (args),
4989 int ARG_UNUSED (flags),
4990 bool *no_add_attrs)
4992 if (TREE_CODE (*node) != FUNCTION_DECL
4993 && TREE_CODE (*node) != PARM_DECL)
4995 warning (OPT_Wattributes, "%qE attribute can only be applied to "
4996 "functions or parameters", name);
4997 *no_add_attrs = true;
4999 else
5001 warning (OPT_Wattributes, "%qE attribute ignored", name);
5002 *no_add_attrs = true;
5004 return NULL_TREE;
5007 /* Handle the C++17 [[nodiscard]] attribute, which is similar to the GNU
5008 warn_unused_result attribute. */
5010 static tree
5011 handle_nodiscard_attribute (tree *node, tree name, tree args,
5012 int /*flags*/, bool *no_add_attrs)
5014 if (args && TREE_CODE (TREE_VALUE (args)) != STRING_CST)
5016 error ("%qE attribute argument must be a string constant", name);
5017 *no_add_attrs = true;
5019 if (TREE_CODE (*node) == FUNCTION_DECL)
5021 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node)))
5022 && !DECL_CONSTRUCTOR_P (*node))
5023 warning_at (DECL_SOURCE_LOCATION (*node),
5024 OPT_Wattributes, "%qE attribute applied to %qD with void "
5025 "return type", name, *node);
5027 else if (OVERLOAD_TYPE_P (*node))
5028 /* OK */;
5029 else
5031 warning (OPT_Wattributes, "%qE attribute can only be applied to "
5032 "functions or to class or enumeration types", name);
5033 *no_add_attrs = true;
5035 return NULL_TREE;
5038 /* Handle a C++20 "no_unique_address" attribute; arguments as in
5039 struct attribute_spec.handler. */
5040 static tree
5041 handle_no_unique_addr_attribute (tree* node,
5042 tree name,
5043 tree /*args*/,
5044 int /*flags*/,
5045 bool* no_add_attrs)
5047 if (TREE_CODE (*node) == VAR_DECL)
5049 DECL_MERGEABLE (*node) = true;
5050 if (pedantic)
5051 warning (OPT_Wattributes, "%qE attribute can only be applied to "
5052 "non-static data members", name);
5054 else if (TREE_CODE (*node) != FIELD_DECL)
5056 warning (OPT_Wattributes, "%qE attribute can only be applied to "
5057 "non-static data members", name);
5058 *no_add_attrs = true;
5060 else if (DECL_C_BIT_FIELD (*node))
5062 warning (OPT_Wattributes, "%qE attribute cannot be applied to "
5063 "a bit-field", name);
5064 *no_add_attrs = true;
5067 return NULL_TREE;
5070 /* The C++20 [[likely]] and [[unlikely]] attributes on labels map to the GNU
5071 hot/cold attributes. */
5073 static tree
5074 handle_likeliness_attribute (tree *node, tree name, tree args,
5075 int flags, bool *no_add_attrs)
5077 *no_add_attrs = true;
5078 if (TREE_CODE (*node) == LABEL_DECL
5079 || TREE_CODE (*node) == FUNCTION_DECL)
5081 if (args)
5082 warning (OPT_Wattributes, "%qE attribute takes no arguments", name);
5083 tree bname = (is_attribute_p ("likely", name)
5084 ? get_identifier ("hot") : get_identifier ("cold"));
5085 if (TREE_CODE (*node) == FUNCTION_DECL)
5086 warning (OPT_Wattributes, "ISO C++ %qE attribute does not apply to "
5087 "functions; treating as %<[[gnu::%E]]%>", name, bname);
5088 tree battr = build_tree_list (bname, NULL_TREE);
5089 decl_attributes (node, battr, flags);
5090 return NULL_TREE;
5092 else
5093 return error_mark_node;
5096 /* Table of valid C++ attributes. */
5097 const struct attribute_spec cxx_attribute_table[] =
5099 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
5100 affects_type_identity, handler, exclude } */
5101 { "init_priority", 1, 1, true, false, false, false,
5102 handle_init_priority_attribute, NULL },
5103 { "abi_tag", 1, -1, false, false, false, true,
5104 handle_abi_tag_attribute, NULL },
5105 { NULL, 0, 0, false, false, false, false, NULL, NULL }
5108 /* Table of C++ standard attributes. */
5109 const struct attribute_spec std_attribute_table[] =
5111 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
5112 affects_type_identity, handler, exclude } */
5113 { "maybe_unused", 0, 0, false, false, false, false,
5114 handle_unused_attribute, NULL },
5115 { "nodiscard", 0, 1, false, false, false, false,
5116 handle_nodiscard_attribute, NULL },
5117 { "no_unique_address", 0, 0, true, false, false, false,
5118 handle_no_unique_addr_attribute, NULL },
5119 { "likely", 0, 0, false, false, false, false,
5120 handle_likeliness_attribute, attr_cold_hot_exclusions },
5121 { "unlikely", 0, 0, false, false, false, false,
5122 handle_likeliness_attribute, attr_cold_hot_exclusions },
5123 { "noreturn", 0, 0, true, false, false, false,
5124 handle_noreturn_attribute, attr_noreturn_exclusions },
5125 { "carries_dependency", 0, 0, true, false, false, false,
5126 handle_carries_dependency_attribute, NULL },
5127 { "pre", 0, -1, false, false, false, false,
5128 handle_contract_attribute, NULL },
5129 { "post", 0, -1, false, false, false, false,
5130 handle_contract_attribute, NULL },
5131 { NULL, 0, 0, false, false, false, false, NULL, NULL }
5134 /* Handle an "init_priority" attribute; arguments as in
5135 struct attribute_spec.handler. */
5136 static tree
5137 handle_init_priority_attribute (tree* node,
5138 tree name,
5139 tree args,
5140 int /*flags*/,
5141 bool* no_add_attrs)
5143 if (!SUPPORTS_INIT_PRIORITY)
5144 /* Treat init_priority as an unrecognized attribute (mirroring
5145 __has_attribute) if the target doesn't support init priorities. */
5146 return error_mark_node;
5148 tree initp_expr = TREE_VALUE (args);
5149 tree decl = *node;
5150 tree type = TREE_TYPE (decl);
5151 int pri;
5153 STRIP_NOPS (initp_expr);
5154 initp_expr = default_conversion (initp_expr);
5155 if (initp_expr)
5156 initp_expr = maybe_constant_value (initp_expr);
5158 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
5160 error ("requested %<init_priority%> is not an integer constant");
5161 cxx_constant_value (initp_expr);
5162 *no_add_attrs = true;
5163 return NULL_TREE;
5166 pri = TREE_INT_CST_LOW (initp_expr);
5168 type = strip_array_types (type);
5170 if (decl == NULL_TREE
5171 || !VAR_P (decl)
5172 || !TREE_STATIC (decl)
5173 || DECL_EXTERNAL (decl)
5174 || (TREE_CODE (type) != RECORD_TYPE
5175 && TREE_CODE (type) != UNION_TYPE)
5176 /* Static objects in functions are initialized the
5177 first time control passes through that
5178 function. This is not precise enough to pin down an
5179 init_priority value, so don't allow it. */
5180 || current_function_decl)
5182 error ("can only use %qE attribute on file-scope definitions "
5183 "of objects of class type", name);
5184 *no_add_attrs = true;
5185 return NULL_TREE;
5188 if (pri > MAX_INIT_PRIORITY || pri <= 0)
5190 error ("requested %<init_priority%> %i is out of range [0, %i]",
5191 pri, MAX_INIT_PRIORITY);
5192 *no_add_attrs = true;
5193 return NULL_TREE;
5196 /* Check for init_priorities that are reserved for
5197 language and runtime support implementations.*/
5198 if (pri <= MAX_RESERVED_INIT_PRIORITY)
5200 warning
5201 (0, "requested %<init_priority%> %i is reserved for internal use",
5202 pri);
5205 SET_DECL_INIT_PRIORITY (decl, pri);
5206 DECL_HAS_INIT_PRIORITY_P (decl) = 1;
5207 return NULL_TREE;
5210 /* DECL is being redeclared; the old declaration had the abi tags in OLD,
5211 and the new one has the tags in NEW_. Give an error if there are tags
5212 in NEW_ that weren't in OLD. */
5214 bool
5215 check_abi_tag_redeclaration (const_tree decl, const_tree old, const_tree new_)
5217 if (old && TREE_CODE (TREE_VALUE (old)) == TREE_LIST)
5218 old = TREE_VALUE (old);
5219 if (new_ && TREE_CODE (TREE_VALUE (new_)) == TREE_LIST)
5220 new_ = TREE_VALUE (new_);
5221 bool err = false;
5222 for (const_tree t = new_; t; t = TREE_CHAIN (t))
5224 tree str = TREE_VALUE (t);
5225 for (const_tree in = old; in; in = TREE_CHAIN (in))
5227 tree ostr = TREE_VALUE (in);
5228 if (cp_tree_equal (str, ostr))
5229 goto found;
5231 error ("redeclaration of %qD adds abi tag %qE", decl, str);
5232 err = true;
5233 found:;
5235 if (err)
5237 inform (DECL_SOURCE_LOCATION (decl), "previous declaration here");
5238 return false;
5240 return true;
5243 /* The abi_tag attribute with the name NAME was given ARGS. If they are
5244 ill-formed, give an error and return false; otherwise, return true. */
5246 bool
5247 check_abi_tag_args (tree args, tree name)
5249 if (!args)
5251 error ("the %qE attribute requires arguments", name);
5252 return false;
5254 for (tree arg = args; arg; arg = TREE_CHAIN (arg))
5256 tree elt = TREE_VALUE (arg);
5257 if (TREE_CODE (elt) != STRING_CST
5258 || (!same_type_ignoring_top_level_qualifiers_p
5259 (strip_array_types (TREE_TYPE (elt)),
5260 char_type_node)))
5262 error ("arguments to the %qE attribute must be narrow string "
5263 "literals", name);
5264 return false;
5266 const char *begin = TREE_STRING_POINTER (elt);
5267 const char *end = begin + TREE_STRING_LENGTH (elt);
5268 for (const char *p = begin; p != end; ++p)
5270 char c = *p;
5271 if (p == begin)
5273 if (!ISALPHA (c) && c != '_')
5275 error ("arguments to the %qE attribute must contain valid "
5276 "identifiers", name);
5277 inform (input_location, "%<%c%> is not a valid first "
5278 "character for an identifier", c);
5279 return false;
5282 else if (p == end - 1)
5283 gcc_assert (c == 0);
5284 else
5286 if (!ISALNUM (c) && c != '_')
5288 error ("arguments to the %qE attribute must contain valid "
5289 "identifiers", name);
5290 inform (input_location, "%<%c%> is not a valid character "
5291 "in an identifier", c);
5292 return false;
5297 return true;
5300 /* Handle an "abi_tag" attribute; arguments as in
5301 struct attribute_spec.handler. */
5303 static tree
5304 handle_abi_tag_attribute (tree* node, tree name, tree args,
5305 int flags, bool* no_add_attrs)
5307 if (!check_abi_tag_args (args, name))
5308 goto fail;
5310 if (TYPE_P (*node))
5312 if (!OVERLOAD_TYPE_P (*node))
5314 error ("%qE attribute applied to non-class, non-enum type %qT",
5315 name, *node);
5316 goto fail;
5318 else if (!(flags & (int)ATTR_FLAG_TYPE_IN_PLACE))
5320 error ("%qE attribute applied to %qT after its definition",
5321 name, *node);
5322 goto fail;
5324 else if (CLASS_TYPE_P (*node)
5325 && CLASSTYPE_TEMPLATE_INSTANTIATION (*node))
5327 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
5328 "template instantiation %qT", name, *node);
5329 goto fail;
5331 else if (CLASS_TYPE_P (*node)
5332 && CLASSTYPE_TEMPLATE_SPECIALIZATION (*node))
5334 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
5335 "template specialization %qT", name, *node);
5336 goto fail;
5339 tree attributes = TYPE_ATTRIBUTES (*node);
5340 tree decl = TYPE_NAME (*node);
5342 /* Make sure all declarations have the same abi tags. */
5343 if (DECL_SOURCE_LOCATION (decl) != input_location)
5345 if (!check_abi_tag_redeclaration (decl,
5346 lookup_attribute ("abi_tag",
5347 attributes),
5348 args))
5349 goto fail;
5352 else
5354 if (!VAR_OR_FUNCTION_DECL_P (*node))
5356 error ("%qE attribute applied to non-function, non-variable %qD",
5357 name, *node);
5358 goto fail;
5360 else if (DECL_LANGUAGE (*node) == lang_c)
5362 error ("%qE attribute applied to extern \"C\" declaration %qD",
5363 name, *node);
5364 goto fail;
5368 return NULL_TREE;
5370 fail:
5371 *no_add_attrs = true;
5372 return NULL_TREE;
5375 /* Perform checking for contract attributes. */
5377 tree
5378 handle_contract_attribute (tree *ARG_UNUSED (node), tree ARG_UNUSED (name),
5379 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
5380 bool *ARG_UNUSED (no_add_attrs))
5382 /* TODO: Is there any checking we could do here? */
5383 return NULL_TREE;
5386 /* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the
5387 thing pointed to by the constant. */
5389 tree
5390 make_ptrmem_cst (tree type, tree member)
5392 tree ptrmem_cst = make_node (PTRMEM_CST);
5393 TREE_TYPE (ptrmem_cst) = type;
5394 PTRMEM_CST_MEMBER (ptrmem_cst) = member;
5395 PTRMEM_CST_LOCATION (ptrmem_cst) = input_location;
5396 return ptrmem_cst;
5399 /* Build a variant of TYPE that has the indicated ATTRIBUTES. May
5400 return an existing type if an appropriate type already exists. */
5402 tree
5403 cp_build_type_attribute_variant (tree type, tree attributes)
5405 tree new_type;
5407 new_type = build_type_attribute_variant (type, attributes);
5408 if (FUNC_OR_METHOD_TYPE_P (new_type))
5409 gcc_checking_assert (cxx_type_hash_eq (type, new_type));
5411 /* Making a new main variant of a class type is broken. */
5412 gcc_assert (!CLASS_TYPE_P (type) || new_type == type);
5414 return new_type;
5417 /* Return TRUE if TYPE1 and TYPE2 are identical for type hashing purposes.
5418 Called only after doing all language independent checks. */
5420 bool
5421 cxx_type_hash_eq (const_tree typea, const_tree typeb)
5423 gcc_assert (FUNC_OR_METHOD_TYPE_P (typea));
5425 if (type_memfn_rqual (typea) != type_memfn_rqual (typeb))
5426 return false;
5427 if (TYPE_HAS_LATE_RETURN_TYPE (typea) != TYPE_HAS_LATE_RETURN_TYPE (typeb))
5428 return false;
5429 return comp_except_specs (TYPE_RAISES_EXCEPTIONS (typea),
5430 TYPE_RAISES_EXCEPTIONS (typeb), ce_exact);
5433 /* Copy the language-specific type variant modifiers from TYPEB to TYPEA. For
5434 C++, these are the exception-specifier and ref-qualifier. */
5436 tree
5437 cxx_copy_lang_qualifiers (const_tree typea, const_tree typeb)
5439 tree type = CONST_CAST_TREE (typea);
5440 if (FUNC_OR_METHOD_TYPE_P (type))
5441 type = build_cp_fntype_variant (type, type_memfn_rqual (typeb),
5442 TYPE_RAISES_EXCEPTIONS (typeb),
5443 TYPE_HAS_LATE_RETURN_TYPE (typeb));
5444 return type;
5447 /* Apply FUNC to all language-specific sub-trees of TP in a pre-order
5448 traversal. Called from walk_tree. */
5450 tree
5451 cp_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func,
5452 void *data, hash_set<tree> *pset)
5454 tree t = *tp;
5455 enum tree_code code = TREE_CODE (t);
5456 tree result;
5458 #define WALK_SUBTREE(NODE) \
5459 do \
5461 result = cp_walk_tree (&(NODE), func, data, pset); \
5462 if (result) goto out; \
5464 while (0)
5466 if (TYPE_P (t))
5468 /* If *WALK_SUBTREES_P is 1, we're interested in the syntactic form of
5469 the argument, so don't look through typedefs, but do walk into
5470 template arguments for alias templates (and non-typedefed classes).
5472 If *WALK_SUBTREES_P > 1, we're interested in type identity or
5473 equivalence, so look through typedefs, ignoring template arguments for
5474 alias templates, and walk into template args of classes.
5476 See find_abi_tags_r for an example of setting *WALK_SUBTREES_P to 2
5477 when that's the behavior the walk_tree_fn wants. */
5478 if (*walk_subtrees_p == 1 && typedef_variant_p (t))
5480 if (tree ti = TYPE_ALIAS_TEMPLATE_INFO (t))
5481 WALK_SUBTREE (TI_ARGS (ti));
5482 *walk_subtrees_p = 0;
5483 return NULL_TREE;
5486 if (tree ti = TYPE_TEMPLATE_INFO (t))
5487 WALK_SUBTREE (TI_ARGS (ti));
5490 /* Not one of the easy cases. We must explicitly go through the
5491 children. */
5492 result = NULL_TREE;
5493 switch (code)
5495 case TEMPLATE_TYPE_PARM:
5496 if (template_placeholder_p (t))
5497 WALK_SUBTREE (CLASS_PLACEHOLDER_TEMPLATE (t));
5498 /* Fall through. */
5499 case DEFERRED_PARSE:
5500 case TEMPLATE_TEMPLATE_PARM:
5501 case BOUND_TEMPLATE_TEMPLATE_PARM:
5502 case UNBOUND_CLASS_TEMPLATE:
5503 case TEMPLATE_PARM_INDEX:
5504 case TYPEOF_TYPE:
5505 /* None of these have subtrees other than those already walked
5506 above. */
5507 *walk_subtrees_p = 0;
5508 break;
5510 case TYPENAME_TYPE:
5511 WALK_SUBTREE (TYPE_CONTEXT (t));
5512 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
5513 *walk_subtrees_p = 0;
5514 break;
5516 case BASELINK:
5517 if (BASELINK_QUALIFIED_P (t))
5518 WALK_SUBTREE (BINFO_TYPE (BASELINK_ACCESS_BINFO (t)));
5519 WALK_SUBTREE (BASELINK_FUNCTIONS (t));
5520 *walk_subtrees_p = 0;
5521 break;
5523 case PTRMEM_CST:
5524 WALK_SUBTREE (TREE_TYPE (t));
5525 *walk_subtrees_p = 0;
5526 break;
5528 case TREE_LIST:
5529 WALK_SUBTREE (TREE_PURPOSE (t));
5530 break;
5532 case OVERLOAD:
5533 WALK_SUBTREE (OVL_FUNCTION (t));
5534 WALK_SUBTREE (OVL_CHAIN (t));
5535 *walk_subtrees_p = 0;
5536 break;
5538 case USING_DECL:
5539 WALK_SUBTREE (DECL_NAME (t));
5540 WALK_SUBTREE (USING_DECL_SCOPE (t));
5541 WALK_SUBTREE (USING_DECL_DECLS (t));
5542 *walk_subtrees_p = 0;
5543 break;
5545 case RECORD_TYPE:
5546 if (TYPE_PTRMEMFUNC_P (t))
5547 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (t));
5548 break;
5550 case TYPE_ARGUMENT_PACK:
5551 case NONTYPE_ARGUMENT_PACK:
5553 tree args = ARGUMENT_PACK_ARGS (t);
5554 for (tree arg : tree_vec_range (args))
5555 WALK_SUBTREE (arg);
5557 break;
5559 case TYPE_PACK_EXPANSION:
5560 WALK_SUBTREE (TREE_TYPE (t));
5561 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (t));
5562 *walk_subtrees_p = 0;
5563 break;
5565 case EXPR_PACK_EXPANSION:
5566 WALK_SUBTREE (TREE_OPERAND (t, 0));
5567 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (t));
5568 *walk_subtrees_p = 0;
5569 break;
5571 case CAST_EXPR:
5572 case REINTERPRET_CAST_EXPR:
5573 case STATIC_CAST_EXPR:
5574 case CONST_CAST_EXPR:
5575 case DYNAMIC_CAST_EXPR:
5576 case IMPLICIT_CONV_EXPR:
5577 case BIT_CAST_EXPR:
5578 if (TREE_TYPE (t))
5579 WALK_SUBTREE (TREE_TYPE (t));
5580 break;
5582 case CONSTRUCTOR:
5583 if (COMPOUND_LITERAL_P (t))
5584 WALK_SUBTREE (TREE_TYPE (t));
5585 break;
5587 case TRAIT_EXPR:
5588 WALK_SUBTREE (TRAIT_EXPR_TYPE1 (t));
5589 WALK_SUBTREE (TRAIT_EXPR_TYPE2 (t));
5590 *walk_subtrees_p = 0;
5591 break;
5593 case TRAIT_TYPE:
5594 WALK_SUBTREE (TRAIT_TYPE_TYPE1 (t));
5595 WALK_SUBTREE (TRAIT_TYPE_TYPE2 (t));
5596 *walk_subtrees_p = 0;
5597 break;
5599 case DECLTYPE_TYPE:
5601 cp_unevaluated u;
5602 WALK_SUBTREE (DECLTYPE_TYPE_EXPR (t));
5603 *walk_subtrees_p = 0;
5604 break;
5607 case ALIGNOF_EXPR:
5608 case SIZEOF_EXPR:
5609 case NOEXCEPT_EXPR:
5611 cp_unevaluated u;
5612 WALK_SUBTREE (TREE_OPERAND (t, 0));
5613 *walk_subtrees_p = 0;
5614 break;
5617 case REQUIRES_EXPR:
5619 cp_unevaluated u;
5620 for (tree parm = REQUIRES_EXPR_PARMS (t); parm; parm = DECL_CHAIN (parm))
5621 /* Walk the types of each parameter, but not the parameter itself,
5622 since doing so would cause false positives in the unexpanded pack
5623 checker if the requires-expr introduces a function parameter pack,
5624 e.g. requires (Ts... ts) { }. */
5625 WALK_SUBTREE (TREE_TYPE (parm));
5626 WALK_SUBTREE (REQUIRES_EXPR_REQS (t));
5627 *walk_subtrees_p = 0;
5628 break;
5631 case DECL_EXPR:
5632 /* User variables should be mentioned in BIND_EXPR_VARS
5633 and their initializers and sizes walked when walking
5634 the containing BIND_EXPR. Compiler temporaries are
5635 handled here. And also normal variables in templates,
5636 since do_poplevel doesn't build a BIND_EXPR then. */
5637 if (VAR_P (TREE_OPERAND (t, 0))
5638 && (processing_template_decl
5639 || (DECL_ARTIFICIAL (TREE_OPERAND (t, 0))
5640 && !TREE_STATIC (TREE_OPERAND (t, 0)))))
5642 tree decl = TREE_OPERAND (t, 0);
5643 WALK_SUBTREE (DECL_INITIAL (decl));
5644 WALK_SUBTREE (DECL_SIZE (decl));
5645 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
5647 break;
5649 case LAMBDA_EXPR:
5650 /* Don't walk into the body of the lambda, but the capture initializers
5651 are part of the enclosing context. */
5652 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
5653 cap = TREE_CHAIN (cap))
5654 WALK_SUBTREE (TREE_VALUE (cap));
5655 break;
5657 case CO_YIELD_EXPR:
5658 if (TREE_OPERAND (t, 1))
5659 /* Operand 1 is the tree for the relevant co_await which has any
5660 interesting sub-trees. */
5661 WALK_SUBTREE (TREE_OPERAND (t, 1));
5662 break;
5664 case CO_AWAIT_EXPR:
5665 if (TREE_OPERAND (t, 1))
5666 /* Operand 1 is frame variable. */
5667 WALK_SUBTREE (TREE_OPERAND (t, 1));
5668 if (TREE_OPERAND (t, 2))
5669 /* Operand 2 has the initialiser, and we need to walk any subtrees
5670 there. */
5671 WALK_SUBTREE (TREE_OPERAND (t, 2));
5672 break;
5674 case CO_RETURN_EXPR:
5675 if (TREE_OPERAND (t, 0))
5677 if (VOID_TYPE_P (TREE_OPERAND (t, 0)))
5678 /* For void expressions, operand 1 is a trivial call, and any
5679 interesting subtrees will be part of operand 0. */
5680 WALK_SUBTREE (TREE_OPERAND (t, 0));
5681 else if (TREE_OPERAND (t, 1))
5682 /* Interesting sub-trees will be in the return_value () call
5683 arguments. */
5684 WALK_SUBTREE (TREE_OPERAND (t, 1));
5686 break;
5688 case STATIC_ASSERT:
5689 WALK_SUBTREE (STATIC_ASSERT_CONDITION (t));
5690 WALK_SUBTREE (STATIC_ASSERT_MESSAGE (t));
5691 break;
5693 default:
5694 return NULL_TREE;
5697 /* We didn't find what we were looking for. */
5698 out:
5699 return result;
5701 #undef WALK_SUBTREE
5704 /* Like save_expr, but for C++. */
5706 tree
5707 cp_save_expr (tree expr)
5709 /* There is no reason to create a SAVE_EXPR within a template; if
5710 needed, we can create the SAVE_EXPR when instantiating the
5711 template. Furthermore, the middle-end cannot handle C++-specific
5712 tree codes. */
5713 if (processing_template_decl)
5714 return expr;
5716 /* TARGET_EXPRs are only expanded once. */
5717 if (TREE_CODE (expr) == TARGET_EXPR)
5718 return expr;
5720 return save_expr (expr);
5723 /* Initialize tree.cc. */
5725 void
5726 init_tree (void)
5728 list_hash_table = hash_table<list_hasher>::create_ggc (61);
5729 register_scoped_attributes (std_attribute_table, NULL);
5732 /* Returns the kind of special function that DECL (a FUNCTION_DECL)
5733 is. Note that sfk_none is zero, so this function can be used as a
5734 predicate to test whether or not DECL is a special function. */
5736 special_function_kind
5737 special_function_p (const_tree decl)
5739 /* Rather than doing all this stuff with magic names, we should
5740 probably have a field of type `special_function_kind' in
5741 DECL_LANG_SPECIFIC. */
5742 if (DECL_INHERITED_CTOR (decl))
5743 return sfk_inheriting_constructor;
5744 if (DECL_COPY_CONSTRUCTOR_P (decl))
5745 return sfk_copy_constructor;
5746 if (DECL_MOVE_CONSTRUCTOR_P (decl))
5747 return sfk_move_constructor;
5748 if (DECL_CONSTRUCTOR_P (decl))
5749 return sfk_constructor;
5750 if (DECL_ASSIGNMENT_OPERATOR_P (decl)
5751 && DECL_OVERLOADED_OPERATOR_IS (decl, NOP_EXPR))
5753 if (copy_fn_p (decl))
5754 return sfk_copy_assignment;
5755 if (move_fn_p (decl))
5756 return sfk_move_assignment;
5758 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
5759 return sfk_destructor;
5760 if (DECL_COMPLETE_DESTRUCTOR_P (decl))
5761 return sfk_complete_destructor;
5762 if (DECL_BASE_DESTRUCTOR_P (decl))
5763 return sfk_base_destructor;
5764 if (DECL_DELETING_DESTRUCTOR_P (decl))
5765 return sfk_deleting_destructor;
5766 if (DECL_CONV_FN_P (decl))
5767 return sfk_conversion;
5768 if (deduction_guide_p (decl))
5769 return sfk_deduction_guide;
5770 if (DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) >= OVL_OP_EQ_EXPR
5771 && DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) <= OVL_OP_SPACESHIP_EXPR)
5772 return sfk_comparison;
5774 return sfk_none;
5777 /* As above, but only if DECL is a special member function as per 11.3.3
5778 [special]: default/copy/move ctor, copy/move assignment, or destructor. */
5780 special_function_kind
5781 special_memfn_p (const_tree decl)
5783 switch (special_function_kind sfk = special_function_p (decl))
5785 case sfk_constructor:
5786 if (!default_ctor_p (decl))
5787 break;
5788 gcc_fallthrough();
5789 case sfk_copy_constructor:
5790 case sfk_copy_assignment:
5791 case sfk_move_assignment:
5792 case sfk_move_constructor:
5793 case sfk_destructor:
5794 return sfk;
5796 default:
5797 break;
5799 return sfk_none;
5802 /* Returns nonzero if TYPE is a character type, including wchar_t. */
5805 char_type_p (tree type)
5807 return (same_type_p (type, char_type_node)
5808 || same_type_p (type, unsigned_char_type_node)
5809 || same_type_p (type, signed_char_type_node)
5810 || same_type_p (type, char8_type_node)
5811 || same_type_p (type, char16_type_node)
5812 || same_type_p (type, char32_type_node)
5813 || same_type_p (type, wchar_type_node));
5816 /* Returns the kind of linkage associated with the indicated DECL. Th
5817 value returned is as specified by the language standard; it is
5818 independent of implementation details regarding template
5819 instantiation, etc. For example, it is possible that a declaration
5820 to which this function assigns external linkage would not show up
5821 as a global symbol when you run `nm' on the resulting object file. */
5823 linkage_kind
5824 decl_linkage (tree decl)
5826 /* This function doesn't attempt to calculate the linkage from first
5827 principles as given in [basic.link]. Instead, it makes use of
5828 the fact that we have already set TREE_PUBLIC appropriately, and
5829 then handles a few special cases. Ideally, we would calculate
5830 linkage first, and then transform that into a concrete
5831 implementation. */
5833 /* Things that don't have names have no linkage. */
5834 if (!DECL_NAME (decl))
5835 return lk_none;
5837 /* Fields have no linkage. */
5838 if (TREE_CODE (decl) == FIELD_DECL)
5839 return lk_none;
5841 /* Things in local scope do not have linkage. */
5842 if (decl_function_context (decl))
5843 return lk_none;
5845 /* Things that are TREE_PUBLIC have external linkage. */
5846 if (TREE_PUBLIC (decl))
5847 return lk_external;
5849 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor variants,
5850 check one of the "clones" for the real linkage. */
5851 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl)
5852 && DECL_CHAIN (decl)
5853 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)))
5854 return decl_linkage (DECL_CHAIN (decl));
5856 if (TREE_CODE (decl) == NAMESPACE_DECL)
5857 return lk_external;
5859 /* Linkage of a CONST_DECL depends on the linkage of the enumeration
5860 type. */
5861 if (TREE_CODE (decl) == CONST_DECL)
5862 return decl_linkage (TYPE_NAME (DECL_CONTEXT (decl)));
5864 /* Members of the anonymous namespace also have TREE_PUBLIC unset, but
5865 are considered to have external linkage for language purposes, as do
5866 template instantiations on targets without weak symbols. DECLs really
5867 meant to have internal linkage have DECL_THIS_STATIC set. */
5868 if (TREE_CODE (decl) == TYPE_DECL)
5869 return lk_external;
5870 if (VAR_OR_FUNCTION_DECL_P (decl))
5872 if (!DECL_THIS_STATIC (decl))
5873 return lk_external;
5875 /* Static data members and static member functions from classes
5876 in anonymous namespace also don't have TREE_PUBLIC set. */
5877 if (DECL_CLASS_CONTEXT (decl))
5878 return lk_external;
5881 /* Everything else has internal linkage. */
5882 return lk_internal;
5885 /* Returns the storage duration of the object or reference associated with
5886 the indicated DECL, which should be a VAR_DECL or PARM_DECL. */
5888 duration_kind
5889 decl_storage_duration (tree decl)
5891 if (TREE_CODE (decl) == PARM_DECL)
5892 return dk_auto;
5893 if (TREE_CODE (decl) == FUNCTION_DECL)
5894 return dk_static;
5895 gcc_assert (VAR_P (decl));
5896 if (!TREE_STATIC (decl)
5897 && !DECL_EXTERNAL (decl))
5898 return dk_auto;
5899 if (CP_DECL_THREAD_LOCAL_P (decl))
5900 return dk_thread;
5901 return dk_static;
5904 /* EXP is an expression that we want to pre-evaluate. Returns (in
5905 *INITP) an expression that will perform the pre-evaluation. The
5906 value returned by this function is a side-effect free expression
5907 equivalent to the pre-evaluated expression. Callers must ensure
5908 that *INITP is evaluated before EXP. */
5910 tree
5911 stabilize_expr (tree exp, tree* initp)
5913 tree init_expr;
5915 if (!TREE_SIDE_EFFECTS (exp))
5916 init_expr = NULL_TREE;
5917 else if (VOID_TYPE_P (TREE_TYPE (exp)))
5919 init_expr = exp;
5920 exp = void_node;
5922 /* There are no expressions with REFERENCE_TYPE, but there can be call
5923 arguments with such a type; just treat it as a pointer. */
5924 else if (TYPE_REF_P (TREE_TYPE (exp))
5925 || SCALAR_TYPE_P (TREE_TYPE (exp))
5926 || !glvalue_p (exp))
5928 init_expr = get_target_expr (exp);
5929 exp = TARGET_EXPR_SLOT (init_expr);
5930 if (CLASS_TYPE_P (TREE_TYPE (exp)))
5931 exp = move (exp);
5932 else
5933 exp = rvalue (exp);
5935 else
5937 bool xval = !lvalue_p (exp);
5938 exp = cp_build_addr_expr (exp, tf_warning_or_error);
5939 init_expr = get_target_expr (exp);
5940 exp = TARGET_EXPR_SLOT (init_expr);
5941 exp = cp_build_fold_indirect_ref (exp);
5942 if (xval)
5943 exp = move (exp);
5945 *initp = init_expr;
5947 gcc_assert (!TREE_SIDE_EFFECTS (exp));
5948 return exp;
5951 /* Add NEW_EXPR, an expression whose value we don't care about, after the
5952 similar expression ORIG. */
5954 tree
5955 add_stmt_to_compound (tree orig, tree new_expr)
5957 if (!new_expr || !TREE_SIDE_EFFECTS (new_expr))
5958 return orig;
5959 if (!orig || !TREE_SIDE_EFFECTS (orig))
5960 return new_expr;
5961 return build2 (COMPOUND_EXPR, void_type_node, orig, new_expr);
5964 /* Like stabilize_expr, but for a call whose arguments we want to
5965 pre-evaluate. CALL is modified in place to use the pre-evaluated
5966 arguments, while, upon return, *INITP contains an expression to
5967 compute the arguments. */
5969 void
5970 stabilize_call (tree call, tree *initp)
5972 tree inits = NULL_TREE;
5973 int i;
5974 int nargs = call_expr_nargs (call);
5976 if (call == error_mark_node || processing_template_decl)
5978 *initp = NULL_TREE;
5979 return;
5982 gcc_assert (TREE_CODE (call) == CALL_EXPR);
5984 for (i = 0; i < nargs; i++)
5986 tree init;
5987 CALL_EXPR_ARG (call, i) =
5988 stabilize_expr (CALL_EXPR_ARG (call, i), &init);
5989 inits = add_stmt_to_compound (inits, init);
5992 *initp = inits;
5995 /* Like stabilize_expr, but for an AGGR_INIT_EXPR whose arguments we want
5996 to pre-evaluate. CALL is modified in place to use the pre-evaluated
5997 arguments, while, upon return, *INITP contains an expression to
5998 compute the arguments. */
6000 static void
6001 stabilize_aggr_init (tree call, tree *initp)
6003 tree inits = NULL_TREE;
6004 int i;
6005 int nargs = aggr_init_expr_nargs (call);
6007 if (call == error_mark_node)
6008 return;
6010 gcc_assert (TREE_CODE (call) == AGGR_INIT_EXPR);
6012 for (i = 0; i < nargs; i++)
6014 tree init;
6015 AGGR_INIT_EXPR_ARG (call, i) =
6016 stabilize_expr (AGGR_INIT_EXPR_ARG (call, i), &init);
6017 inits = add_stmt_to_compound (inits, init);
6020 *initp = inits;
6023 /* Like stabilize_expr, but for an initialization.
6025 If the initialization is for an object of class type, this function
6026 takes care not to introduce additional temporaries.
6028 Returns TRUE iff the expression was successfully pre-evaluated,
6029 i.e., if INIT is now side-effect free, except for, possibly, a
6030 single call to a constructor. */
6032 bool
6033 stabilize_init (tree init, tree *initp)
6035 tree t = init;
6037 *initp = NULL_TREE;
6039 if (t == error_mark_node || processing_template_decl)
6040 return true;
6042 if (TREE_CODE (t) == INIT_EXPR)
6043 t = TREE_OPERAND (t, 1);
6044 if (TREE_CODE (t) == TARGET_EXPR)
6045 t = TARGET_EXPR_INITIAL (t);
6047 /* If the RHS can be stabilized without breaking copy elision, stabilize
6048 it. We specifically don't stabilize class prvalues here because that
6049 would mean an extra copy, but they might be stabilized below. */
6050 if (TREE_CODE (init) == INIT_EXPR
6051 && TREE_CODE (t) != CONSTRUCTOR
6052 && TREE_CODE (t) != AGGR_INIT_EXPR
6053 && (SCALAR_TYPE_P (TREE_TYPE (t))
6054 || glvalue_p (t)))
6056 TREE_OPERAND (init, 1) = stabilize_expr (t, initp);
6057 return true;
6060 if (TREE_CODE (t) == COMPOUND_EXPR
6061 && TREE_CODE (init) == INIT_EXPR)
6063 tree last = expr_last (t);
6064 /* Handle stabilizing the EMPTY_CLASS_EXPR pattern. */
6065 if (!TREE_SIDE_EFFECTS (last))
6067 *initp = t;
6068 TREE_OPERAND (init, 1) = last;
6069 return true;
6073 if (TREE_CODE (t) == CONSTRUCTOR)
6075 /* Aggregate initialization: stabilize each of the field
6076 initializers. */
6077 unsigned i;
6078 constructor_elt *ce;
6079 bool good = true;
6080 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
6081 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
6083 tree type = TREE_TYPE (ce->value);
6084 tree subinit;
6085 if (TYPE_REF_P (type)
6086 || SCALAR_TYPE_P (type))
6087 ce->value = stabilize_expr (ce->value, &subinit);
6088 else if (!stabilize_init (ce->value, &subinit))
6089 good = false;
6090 *initp = add_stmt_to_compound (*initp, subinit);
6092 return good;
6095 if (TREE_CODE (t) == CALL_EXPR)
6097 stabilize_call (t, initp);
6098 return true;
6101 if (TREE_CODE (t) == AGGR_INIT_EXPR)
6103 stabilize_aggr_init (t, initp);
6104 return true;
6107 /* The initialization is being performed via a bitwise copy -- and
6108 the item copied may have side effects. */
6109 return !TREE_SIDE_EFFECTS (init);
6112 /* Returns true if a cast to TYPE may appear in an integral constant
6113 expression. */
6115 bool
6116 cast_valid_in_integral_constant_expression_p (tree type)
6118 return (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6119 || cxx_dialect >= cxx11
6120 || dependent_type_p (type)
6121 || type == error_mark_node);
6124 /* Return true if we need to fix linkage information of DECL. */
6126 static bool
6127 cp_fix_function_decl_p (tree decl)
6129 /* Skip if DECL is not externally visible. */
6130 if (!TREE_PUBLIC (decl))
6131 return false;
6133 /* We need to fix DECL if it a appears to be exported but with no
6134 function body. Thunks do not have CFGs and we may need to
6135 handle them specially later. */
6136 if (!gimple_has_body_p (decl)
6137 && !DECL_THUNK_P (decl)
6138 && !DECL_EXTERNAL (decl))
6140 struct cgraph_node *node = cgraph_node::get (decl);
6142 /* Don't fix same_body aliases. Although they don't have their own
6143 CFG, they share it with what they alias to. */
6144 if (!node || !node->alias || !node->num_references ())
6145 return true;
6148 return false;
6151 /* Clean the C++ specific parts of the tree T. */
6153 void
6154 cp_free_lang_data (tree t)
6156 if (FUNC_OR_METHOD_TYPE_P (t))
6158 /* Default args are not interesting anymore. */
6159 tree argtypes = TYPE_ARG_TYPES (t);
6160 while (argtypes)
6162 TREE_PURPOSE (argtypes) = 0;
6163 argtypes = TREE_CHAIN (argtypes);
6166 else if (TREE_CODE (t) == FUNCTION_DECL
6167 && cp_fix_function_decl_p (t))
6169 /* If T is used in this translation unit at all, the definition
6170 must exist somewhere else since we have decided to not emit it
6171 in this TU. So make it an external reference. */
6172 DECL_EXTERNAL (t) = 1;
6173 TREE_STATIC (t) = 0;
6175 if (TREE_CODE (t) == NAMESPACE_DECL)
6176 /* We do not need the leftover chaining of namespaces from the
6177 binding level. */
6178 DECL_CHAIN (t) = NULL_TREE;
6181 /* Stub for c-common. Please keep in sync with c-decl.cc.
6182 FIXME: If address space support is target specific, then this
6183 should be a C target hook. But currently this is not possible,
6184 because this function is called via REGISTER_TARGET_PRAGMAS. */
6185 void
6186 c_register_addr_space (const char * /*word*/, addr_space_t /*as*/)
6190 /* Return the number of operands in T that we care about for things like
6191 mangling. */
6194 cp_tree_operand_length (const_tree t)
6196 enum tree_code code = TREE_CODE (t);
6198 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
6199 return VL_EXP_OPERAND_LENGTH (t);
6201 return cp_tree_code_length (code);
6204 /* Like cp_tree_operand_length, but takes a tree_code CODE. */
6207 cp_tree_code_length (enum tree_code code)
6209 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
6211 switch (code)
6213 case PREINCREMENT_EXPR:
6214 case PREDECREMENT_EXPR:
6215 case POSTINCREMENT_EXPR:
6216 case POSTDECREMENT_EXPR:
6217 return 1;
6219 case ARRAY_REF:
6220 return 2;
6222 case EXPR_PACK_EXPANSION:
6223 return 1;
6225 default:
6226 return TREE_CODE_LENGTH (code);
6230 /* Implement -Wzero_as_null_pointer_constant. Return true if the
6231 conditions for the warning hold, false otherwise. */
6232 bool
6233 maybe_warn_zero_as_null_pointer_constant (tree expr, location_t loc)
6235 if (c_inhibit_evaluation_warnings == 0
6236 && !null_node_p (expr) && !NULLPTR_TYPE_P (TREE_TYPE (expr)))
6238 warning_at (loc, OPT_Wzero_as_null_pointer_constant,
6239 "zero as null pointer constant");
6240 return true;
6242 return false;
6245 /* FNDECL is a function declaration whose type may have been altered by
6246 adding extra parameters such as this, in-charge, or VTT. When this
6247 takes place, the positional arguments supplied by the user (as in the
6248 'format' attribute arguments) may refer to the wrong argument. This
6249 function returns an integer indicating how many arguments should be
6250 skipped. */
6253 maybe_adjust_arg_pos_for_attribute (const_tree fndecl)
6255 if (!fndecl)
6256 return 0;
6257 int n = num_artificial_parms_for (fndecl);
6258 /* The manual states that it's the user's responsibility to account
6259 for the implicit this parameter. */
6260 return n > 0 ? n - 1 : 0;
6264 /* Release memory we no longer need after parsing. */
6265 void
6266 cp_tree_c_finish_parsing ()
6268 if (previous_class_level)
6269 invalidate_class_lookup_cache ();
6270 deleted_copy_types = NULL;
6273 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
6274 /* Complain that some language-specific thing hanging off a tree
6275 node has been accessed improperly. */
6277 void
6278 lang_check_failed (const char* file, int line, const char* function)
6280 internal_error ("%<lang_*%> check: failed in %s, at %s:%d",
6281 function, trim_filename (file), line);
6283 #endif /* ENABLE_TREE_CHECKING */
6285 #if CHECKING_P
6287 namespace selftest {
6289 /* Verify that lvalue_kind () works, for various expressions,
6290 and that location wrappers don't affect the results. */
6292 static void
6293 test_lvalue_kind ()
6295 location_t loc = BUILTINS_LOCATION;
6297 /* Verify constants and parameters, without and with
6298 location wrappers. */
6299 tree int_cst = build_int_cst (integer_type_node, 42);
6300 ASSERT_EQ (clk_none, lvalue_kind (int_cst));
6302 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
6303 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
6304 ASSERT_EQ (clk_none, lvalue_kind (wrapped_int_cst));
6306 tree string_lit = build_string (4, "foo");
6307 TREE_TYPE (string_lit) = char_array_type_node;
6308 string_lit = fix_string_type (string_lit);
6309 ASSERT_EQ (clk_ordinary, lvalue_kind (string_lit));
6311 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
6312 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
6313 ASSERT_EQ (clk_ordinary, lvalue_kind (wrapped_string_lit));
6315 tree parm = build_decl (UNKNOWN_LOCATION, PARM_DECL,
6316 get_identifier ("some_parm"),
6317 integer_type_node);
6318 ASSERT_EQ (clk_ordinary, lvalue_kind (parm));
6320 tree wrapped_parm = maybe_wrap_with_location (parm, loc);
6321 ASSERT_TRUE (location_wrapper_p (wrapped_parm));
6322 ASSERT_EQ (clk_ordinary, lvalue_kind (wrapped_parm));
6324 /* Verify that lvalue_kind of std::move on a parm isn't
6325 affected by location wrappers. */
6326 tree rvalue_ref_of_parm = move (parm);
6327 ASSERT_EQ (clk_rvalueref, lvalue_kind (rvalue_ref_of_parm));
6328 tree rvalue_ref_of_wrapped_parm = move (wrapped_parm);
6329 ASSERT_EQ (clk_rvalueref, lvalue_kind (rvalue_ref_of_wrapped_parm));
6331 /* Verify lvalue_p. */
6332 ASSERT_FALSE (lvalue_p (int_cst));
6333 ASSERT_FALSE (lvalue_p (wrapped_int_cst));
6334 ASSERT_TRUE (lvalue_p (parm));
6335 ASSERT_TRUE (lvalue_p (wrapped_parm));
6336 ASSERT_FALSE (lvalue_p (rvalue_ref_of_parm));
6337 ASSERT_FALSE (lvalue_p (rvalue_ref_of_wrapped_parm));
6340 /* Run all of the selftests within this file. */
6342 void
6343 cp_tree_cc_tests ()
6345 test_lvalue_kind ();
6348 } // namespace selftest
6350 #endif /* #if CHECKING_P */
6353 #include "gt-cp-tree.h"