c++, tree: declare some basic functions inline
[official-gcc.git] / gcc / cp / tree.cc
blob131b212ff73e2e5d4a2a154d33c55f4237dfcb8a
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 && reduced_constant_expression_p (value))
526 TREE_READONLY (decl) = true;
528 if (complain & tf_no_cleanup)
529 /* The caller is building a new-expr and does not need a cleanup. */
530 t = NULL_TREE;
531 else
533 t = cxx_maybe_build_cleanup (decl, complain);
534 if (t == error_mark_node)
535 return error_mark_node;
538 set_target_expr_eliding (value);
540 t = build4 (TARGET_EXPR, type, decl, value, t, NULL_TREE);
541 if (location_t eloc = cp_expr_location (value))
542 SET_EXPR_LOCATION (t, eloc);
543 /* We always set TREE_SIDE_EFFECTS so that expand_expr does not
544 ignore the TARGET_EXPR. If there really turn out to be no
545 side-effects, then the optimizer should be able to get rid of
546 whatever code is generated anyhow. */
547 TREE_SIDE_EFFECTS (t) = 1;
549 return t;
552 /* Return an undeclared local temporary of type TYPE for use in building a
553 TARGET_EXPR. */
555 tree
556 build_local_temp (tree type)
558 tree slot = build_decl (input_location,
559 VAR_DECL, NULL_TREE, type);
560 DECL_ARTIFICIAL (slot) = 1;
561 DECL_IGNORED_P (slot) = 1;
562 DECL_CONTEXT (slot) = current_function_decl;
563 layout_decl (slot, 0);
564 return slot;
567 /* Return whether DECL is such a local temporary (or one from
568 create_tmp_var_raw). */
570 bool
571 is_local_temp (tree decl)
573 return (VAR_P (decl) && DECL_ARTIFICIAL (decl)
574 && !TREE_STATIC (decl));
577 /* Set various status flags when building an AGGR_INIT_EXPR object T. */
579 static void
580 process_aggr_init_operands (tree t)
582 bool side_effects;
584 side_effects = TREE_SIDE_EFFECTS (t);
585 if (!side_effects)
587 int i, n;
588 n = TREE_OPERAND_LENGTH (t);
589 for (i = 1; i < n; i++)
591 tree op = TREE_OPERAND (t, i);
592 if (op && TREE_SIDE_EFFECTS (op))
594 side_effects = 1;
595 break;
599 TREE_SIDE_EFFECTS (t) = side_effects;
602 /* Build an AGGR_INIT_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE,
603 FN, and SLOT. NARGS is the number of call arguments which are specified
604 as a tree array ARGS. */
606 static tree
607 build_aggr_init_array (tree return_type, tree fn, tree slot, int nargs,
608 tree *args)
610 tree t;
611 int i;
613 t = build_vl_exp (AGGR_INIT_EXPR, nargs + 3);
614 TREE_TYPE (t) = return_type;
615 AGGR_INIT_EXPR_FN (t) = fn;
616 AGGR_INIT_EXPR_SLOT (t) = slot;
617 for (i = 0; i < nargs; i++)
618 AGGR_INIT_EXPR_ARG (t, i) = args[i];
619 process_aggr_init_operands (t);
620 return t;
623 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
624 target. TYPE is the type to be initialized.
626 Build an AGGR_INIT_EXPR to represent the initialization. This function
627 differs from build_cplus_new in that an AGGR_INIT_EXPR can only be used
628 to initialize another object, whereas a TARGET_EXPR can either
629 initialize another object or create its own temporary object, and as a
630 result building up a TARGET_EXPR requires that the type's destructor be
631 callable. */
633 tree
634 build_aggr_init_expr (tree type, tree init)
636 tree fn;
637 tree slot;
638 tree rval;
639 int is_ctor;
641 gcc_assert (!VOID_TYPE_P (type));
643 /* Don't build AGGR_INIT_EXPR in a template. */
644 if (processing_template_decl)
645 return init;
647 fn = cp_get_callee (init);
648 if (fn == NULL_TREE)
649 return convert (type, init);
651 is_ctor = (TREE_CODE (fn) == ADDR_EXPR
652 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
653 && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0)));
655 /* We split the CALL_EXPR into its function and its arguments here.
656 Then, in expand_expr, we put them back together. The reason for
657 this is that this expression might be a default argument
658 expression. In that case, we need a new temporary every time the
659 expression is used. That's what break_out_target_exprs does; it
660 replaces every AGGR_INIT_EXPR with a copy that uses a fresh
661 temporary slot. Then, expand_expr builds up a call-expression
662 using the new slot. */
664 /* If we don't need to use a constructor to create an object of this
665 type, don't mess with AGGR_INIT_EXPR. */
666 if (is_ctor || TREE_ADDRESSABLE (type))
668 slot = build_local_temp (type);
670 if (TREE_CODE (init) == CALL_EXPR)
672 rval = build_aggr_init_array (void_type_node, fn, slot,
673 call_expr_nargs (init),
674 CALL_EXPR_ARGP (init));
675 AGGR_INIT_FROM_THUNK_P (rval)
676 = CALL_FROM_THUNK_P (init);
678 else
680 rval = build_aggr_init_array (void_type_node, fn, slot,
681 aggr_init_expr_nargs (init),
682 AGGR_INIT_EXPR_ARGP (init));
683 AGGR_INIT_FROM_THUNK_P (rval)
684 = AGGR_INIT_FROM_THUNK_P (init);
686 TREE_SIDE_EFFECTS (rval) = 1;
687 AGGR_INIT_VIA_CTOR_P (rval) = is_ctor;
688 TREE_NOTHROW (rval) = TREE_NOTHROW (init);
689 CALL_EXPR_OPERATOR_SYNTAX (rval) = CALL_EXPR_OPERATOR_SYNTAX (init);
690 CALL_EXPR_ORDERED_ARGS (rval) = CALL_EXPR_ORDERED_ARGS (init);
691 CALL_EXPR_REVERSE_ARGS (rval) = CALL_EXPR_REVERSE_ARGS (init);
693 else
694 rval = init;
696 return rval;
699 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
700 target. TYPE is the type that this initialization should appear to
701 have.
703 Build an encapsulation of the initialization to perform
704 and return it so that it can be processed by language-independent
705 and language-specific expression expanders. */
707 tree
708 build_cplus_new (tree type, tree init, tsubst_flags_t complain)
710 /* This function should cope with what build_special_member_call
711 can produce. When performing parenthesized aggregate initialization,
712 it can produce a { }. */
713 if (BRACE_ENCLOSED_INITIALIZER_P (init))
715 gcc_assert (cxx_dialect >= cxx20);
716 return finish_compound_literal (type, init, complain);
719 tree rval = build_aggr_init_expr (type, init);
720 tree slot;
722 if (init == error_mark_node)
723 return error_mark_node;
725 if (!complete_type_or_maybe_complain (type, init, complain))
726 return error_mark_node;
728 /* Make sure that we're not trying to create an instance of an
729 abstract class. */
730 if (abstract_virtuals_error (NULL_TREE, type, complain))
731 return error_mark_node;
733 if (TREE_CODE (rval) == AGGR_INIT_EXPR)
734 slot = AGGR_INIT_EXPR_SLOT (rval);
735 else if (TREE_CODE (rval) == CALL_EXPR
736 || TREE_CODE (rval) == CONSTRUCTOR)
737 slot = build_local_temp (type);
738 else
739 return rval;
741 rval = build_target_expr (slot, rval, complain);
743 if (rval != error_mark_node)
744 TARGET_EXPR_IMPLICIT_P (rval) = 1;
746 return rval;
749 /* Subroutine of build_vec_init_expr: Build up a single element
750 intialization as a proxy for the full array initialization to get things
751 marked as used and any appropriate diagnostics.
753 This used to be necessary because we were deferring building the actual
754 constructor calls until gimplification time; now we only do it to set
755 VEC_INIT_EXPR_IS_CONSTEXPR.
757 We assume that init is either NULL_TREE, {}, void_type_node (indicating
758 value-initialization), or another array to copy. */
760 static tree
761 build_vec_init_elt (tree type, tree init, tsubst_flags_t complain)
763 tree inner_type = strip_array_types (type);
765 if (integer_zerop (array_type_nelts_total (type))
766 || !CLASS_TYPE_P (inner_type))
767 /* No interesting initialization to do. */
768 return integer_zero_node;
769 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
771 /* Even if init has initializers for some array elements,
772 we're interested in the {}-init of trailing elements. */
773 if (CP_AGGREGATE_TYPE_P (inner_type))
775 tree empty = build_constructor (init_list_type_node, nullptr);
776 return digest_init (inner_type, empty, complain);
778 else
779 /* It's equivalent to value-init. */
780 init = void_type_node;
782 if (init == void_type_node)
783 return build_value_init (inner_type, complain);
785 releasing_vec argvec;
786 if (init && !BRACE_ENCLOSED_INITIALIZER_P (init))
788 gcc_assert (same_type_ignoring_top_level_qualifiers_p
789 (type, TREE_TYPE (init)));
790 tree init_type = strip_array_types (TREE_TYPE (init));
791 tree dummy = build_dummy_object (init_type);
792 if (!lvalue_p (init))
793 dummy = move (dummy);
794 argvec->quick_push (dummy);
796 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
797 &argvec, inner_type, LOOKUP_NORMAL,
798 complain);
800 /* For a trivial constructor, build_over_call creates a TARGET_EXPR. But
801 we don't want one here because we aren't creating a temporary. */
802 if (TREE_CODE (init) == TARGET_EXPR)
803 init = TARGET_EXPR_INITIAL (init);
805 return init;
808 /* Return a TARGET_EXPR which expresses the initialization of an array to
809 be named later, either default-initialization or copy-initialization
810 from another array of the same type. */
812 tree
813 build_vec_init_expr (tree type, tree init, tsubst_flags_t complain)
815 if (tree vi = get_vec_init_expr (init))
816 return vi;
818 tree elt_init;
819 if (init && TREE_CODE (init) == CONSTRUCTOR
820 && !BRACE_ENCLOSED_INITIALIZER_P (init))
821 /* We built any needed constructor calls in digest_init. */
822 elt_init = init;
823 else
824 elt_init = build_vec_init_elt (type, init, complain);
826 bool value_init = false;
827 if (init == void_type_node)
829 value_init = true;
830 init = NULL_TREE;
833 tree slot = build_local_temp (type);
834 init = build2 (VEC_INIT_EXPR, type, slot, init);
835 TREE_SIDE_EFFECTS (init) = true;
836 SET_EXPR_LOCATION (init, input_location);
838 if (cxx_dialect >= cxx11)
840 bool cx = potential_constant_expression (elt_init);
841 if (BRACE_ENCLOSED_INITIALIZER_P (init))
842 cx &= potential_constant_expression (init);
843 VEC_INIT_EXPR_IS_CONSTEXPR (init) = cx;
845 VEC_INIT_EXPR_VALUE_INIT (init) = value_init;
847 return init;
850 /* Call build_vec_init to expand VEC_INIT into TARGET (for which NULL_TREE
851 means VEC_INIT_EXPR_SLOT). */
853 tree
854 expand_vec_init_expr (tree target, tree vec_init, tsubst_flags_t complain,
855 vec<tree,va_gc> **flags)
857 iloc_sentinel ils = EXPR_LOCATION (vec_init);
859 if (!target)
860 target = VEC_INIT_EXPR_SLOT (vec_init);
861 tree init = VEC_INIT_EXPR_INIT (vec_init);
862 int from_array = (init && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE);
863 return build_vec_init (target, NULL_TREE, init,
864 VEC_INIT_EXPR_VALUE_INIT (vec_init),
865 from_array, complain, flags);
868 /* Give a helpful diagnostic for a non-constexpr VEC_INIT_EXPR in a context
869 that requires a constant expression. */
871 void
872 diagnose_non_constexpr_vec_init (tree expr)
874 tree type = TREE_TYPE (VEC_INIT_EXPR_SLOT (expr));
875 tree init, elt_init;
876 if (VEC_INIT_EXPR_VALUE_INIT (expr))
877 init = void_type_node;
878 else
879 init = VEC_INIT_EXPR_INIT (expr);
881 elt_init = build_vec_init_elt (type, init, tf_warning_or_error);
882 require_potential_constant_expression (elt_init);
885 tree
886 build_array_copy (tree init)
888 return get_target_expr (build_vec_init_expr
889 (TREE_TYPE (init), init, tf_warning_or_error));
892 /* Build a TARGET_EXPR using INIT to initialize a new temporary of the
893 indicated TYPE. */
895 tree
896 build_target_expr_with_type (tree init, tree type, tsubst_flags_t complain)
898 gcc_assert (!VOID_TYPE_P (type));
899 gcc_assert (!VOID_TYPE_P (TREE_TYPE (init)));
901 if (TREE_CODE (init) == TARGET_EXPR
902 || init == error_mark_node)
903 return init;
904 else if (CLASS_TYPE_P (type) && type_has_nontrivial_copy_init (type)
905 && TREE_CODE (init) != COND_EXPR
906 && TREE_CODE (init) != CONSTRUCTOR
907 && TREE_CODE (init) != VA_ARG_EXPR
908 && TREE_CODE (init) != CALL_EXPR)
909 /* We need to build up a copy constructor call. COND_EXPR is a special
910 case because we already have copies on the arms and we don't want
911 another one here. A CONSTRUCTOR is aggregate initialization, which
912 is handled separately. A VA_ARG_EXPR is magic creation of an
913 aggregate; there's no additional work to be done. A CALL_EXPR
914 already creates a prvalue. */
915 return force_rvalue (init, complain);
917 return force_target_expr (type, init, complain);
920 /* Like the above function, but without the checking. This function should
921 only be used by code which is deliberately trying to subvert the type
922 system, such as call_builtin_trap. Or build_over_call, to avoid
923 infinite recursion. */
925 tree
926 force_target_expr (tree type, tree init, tsubst_flags_t complain)
928 tree slot;
930 gcc_assert (!VOID_TYPE_P (type));
932 slot = build_local_temp (type);
933 return build_target_expr (slot, init, complain);
936 /* Like build_target_expr_with_type, but use the type of INIT. */
938 tree
939 get_target_expr (tree init, tsubst_flags_t complain /* = tf_warning_or_error */)
941 if (TREE_CODE (init) == AGGR_INIT_EXPR)
942 return build_target_expr (AGGR_INIT_EXPR_SLOT (init), init, complain);
943 else if (TREE_CODE (init) == VEC_INIT_EXPR)
944 return build_target_expr (VEC_INIT_EXPR_SLOT (init), init, complain);
945 else
947 init = convert_bitfield_to_declared_type (init);
948 return build_target_expr_with_type (init, TREE_TYPE (init), complain);
952 /* If EXPR is a bitfield reference, convert it to the declared type of
953 the bitfield, and return the resulting expression. Otherwise,
954 return EXPR itself. */
956 tree
957 convert_bitfield_to_declared_type (tree expr)
959 tree bitfield_type;
961 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
962 if (bitfield_type)
963 expr = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type),
964 expr);
965 return expr;
968 /* EXPR is being used in an rvalue context. Return a version of EXPR
969 that is marked as an rvalue. */
971 tree
972 rvalue (tree expr)
974 tree type;
976 if (error_operand_p (expr))
977 return expr;
979 expr = mark_rvalue_use (expr);
981 /* [basic.lval]
983 Non-class rvalues always have cv-unqualified types. */
984 type = TREE_TYPE (expr);
985 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
986 type = cv_unqualified (type);
988 /* We need to do this for rvalue refs as well to get the right answer
989 from decltype; see c++/36628. */
990 if (!processing_template_decl && glvalue_p (expr))
992 /* But don't use this function for class lvalues; use move (to treat an
993 lvalue as an xvalue) or force_rvalue (to make a prvalue copy). */
994 gcc_checking_assert (!CLASS_TYPE_P (type));
995 expr = build1 (NON_LVALUE_EXPR, type, expr);
997 else if (type != TREE_TYPE (expr))
998 expr = build_nop (type, expr);
1000 return expr;
1004 struct cplus_array_info
1006 tree type;
1007 tree domain;
1010 struct cplus_array_hasher : ggc_ptr_hash<tree_node>
1012 typedef cplus_array_info *compare_type;
1014 static hashval_t hash (tree t);
1015 static bool equal (tree, cplus_array_info *);
1018 /* Hash an ARRAY_TYPE. K is really of type `tree'. */
1020 hashval_t
1021 cplus_array_hasher::hash (tree t)
1023 hashval_t hash;
1025 hash = TYPE_UID (TREE_TYPE (t));
1026 if (TYPE_DOMAIN (t))
1027 hash ^= TYPE_UID (TYPE_DOMAIN (t));
1028 return hash;
1031 /* Compare two ARRAY_TYPEs. K1 is really of type `tree', K2 is really
1032 of type `cplus_array_info*'. */
1034 bool
1035 cplus_array_hasher::equal (tree t1, cplus_array_info *t2)
1037 return (TREE_TYPE (t1) == t2->type && TYPE_DOMAIN (t1) == t2->domain);
1040 /* Hash table containing dependent array types, which are unsuitable for
1041 the language-independent type hash table. */
1042 static GTY (()) hash_table<cplus_array_hasher> *cplus_array_htab;
1044 /* Build an ARRAY_TYPE without laying it out. */
1046 static tree
1047 build_min_array_type (tree elt_type, tree index_type)
1049 tree t = cxx_make_type (ARRAY_TYPE);
1050 TREE_TYPE (t) = elt_type;
1051 TYPE_DOMAIN (t) = index_type;
1052 return t;
1055 /* Set TYPE_CANONICAL like build_array_type_1, but using
1056 build_cplus_array_type. */
1058 static void
1059 set_array_type_canon (tree t, tree elt_type, tree index_type, bool dep)
1061 /* Set the canonical type for this new node. */
1062 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
1063 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
1064 SET_TYPE_STRUCTURAL_EQUALITY (t);
1065 else if (TYPE_CANONICAL (elt_type) != elt_type
1066 || (index_type && TYPE_CANONICAL (index_type) != index_type))
1067 TYPE_CANONICAL (t)
1068 = build_cplus_array_type (TYPE_CANONICAL (elt_type),
1069 index_type
1070 ? TYPE_CANONICAL (index_type) : index_type,
1071 dep);
1072 else
1073 TYPE_CANONICAL (t) = t;
1076 /* Like build_array_type, but handle special C++ semantics: an array of a
1077 variant element type is a variant of the array of the main variant of
1078 the element type. IS_DEPENDENT is -ve if we should determine the
1079 dependency. Otherwise its bool value indicates dependency. */
1081 tree
1082 build_cplus_array_type (tree elt_type, tree index_type, int dependent)
1084 tree t;
1086 if (elt_type == error_mark_node || index_type == error_mark_node)
1087 return error_mark_node;
1089 if (dependent < 0)
1090 dependent = (uses_template_parms (elt_type)
1091 || (index_type && uses_template_parms (index_type)));
1093 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
1094 /* Start with an array of the TYPE_MAIN_VARIANT. */
1095 t = build_cplus_array_type (TYPE_MAIN_VARIANT (elt_type),
1096 index_type, dependent);
1097 else if (dependent)
1099 /* Since type_hash_canon calls layout_type, we need to use our own
1100 hash table. */
1101 cplus_array_info cai;
1102 hashval_t hash;
1104 if (cplus_array_htab == NULL)
1105 cplus_array_htab = hash_table<cplus_array_hasher>::create_ggc (61);
1107 hash = TYPE_UID (elt_type);
1108 if (index_type)
1109 hash ^= TYPE_UID (index_type);
1110 cai.type = elt_type;
1111 cai.domain = index_type;
1113 tree *e = cplus_array_htab->find_slot_with_hash (&cai, hash, INSERT);
1114 if (*e)
1115 /* We have found the type: we're done. */
1116 return (tree) *e;
1117 else
1119 /* Build a new array type. */
1120 t = build_min_array_type (elt_type, index_type);
1122 /* Store it in the hash table. */
1123 *e = t;
1125 /* Set the canonical type for this new node. */
1126 set_array_type_canon (t, elt_type, index_type, dependent);
1128 /* Mark it as dependent now, this saves time later. */
1129 TYPE_DEPENDENT_P_VALID (t) = true;
1130 TYPE_DEPENDENT_P (t) = true;
1133 else
1135 bool typeless_storage = is_byte_access_type (elt_type);
1136 t = build_array_type (elt_type, index_type, typeless_storage);
1138 /* Mark as non-dependenty now, this will save time later. */
1139 TYPE_DEPENDENT_P_VALID (t) = true;
1142 /* Now check whether we already have this array variant. */
1143 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
1145 tree m = t;
1146 for (t = m; t; t = TYPE_NEXT_VARIANT (t))
1147 if (TREE_TYPE (t) == elt_type
1148 && TYPE_NAME (t) == NULL_TREE
1149 && TYPE_ATTRIBUTES (t) == NULL_TREE)
1150 break;
1151 if (!t)
1153 t = build_min_array_type (elt_type, index_type);
1154 /* Mark dependency now, this saves time later. */
1155 TYPE_DEPENDENT_P_VALID (t) = true;
1156 TYPE_DEPENDENT_P (t) = dependent;
1157 set_array_type_canon (t, elt_type, index_type, dependent);
1158 if (!dependent)
1160 layout_type (t);
1161 /* Make sure sizes are shared with the main variant.
1162 layout_type can't be called after setting TYPE_NEXT_VARIANT,
1163 as it will overwrite alignment etc. of all variants. */
1164 TYPE_SIZE (t) = TYPE_SIZE (m);
1165 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (m);
1166 TYPE_TYPELESS_STORAGE (t) = TYPE_TYPELESS_STORAGE (m);
1169 TYPE_MAIN_VARIANT (t) = m;
1170 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
1171 TYPE_NEXT_VARIANT (m) = t;
1175 /* Avoid spurious warnings with VLAs (c++/54583). */
1176 if (TYPE_SIZE (t) && EXPR_P (TYPE_SIZE (t)))
1177 suppress_warning (TYPE_SIZE (t), OPT_Wunused);
1179 /* Push these needs up to the ARRAY_TYPE so that initialization takes
1180 place more easily. */
1181 bool needs_ctor = (TYPE_NEEDS_CONSTRUCTING (t)
1182 = TYPE_NEEDS_CONSTRUCTING (elt_type));
1183 bool needs_dtor = (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1184 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type));
1186 if (!dependent && t == TYPE_MAIN_VARIANT (t)
1187 && !COMPLETE_TYPE_P (t) && COMPLETE_TYPE_P (elt_type))
1189 /* The element type has been completed since the last time we saw
1190 this array type; update the layout and 'tor flags for any variants
1191 that need it. */
1192 layout_type (t);
1193 for (tree v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1195 TYPE_NEEDS_CONSTRUCTING (v) = needs_ctor;
1196 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (v) = needs_dtor;
1200 return t;
1203 /* Return an ARRAY_TYPE with element type ELT and length N. */
1205 tree
1206 build_array_of_n_type (tree elt, int n)
1208 return build_cplus_array_type (elt, build_index_type (size_int (n - 1)));
1211 /* True iff T is an array of unknown bound. */
1213 bool
1214 array_of_unknown_bound_p (const_tree t)
1216 return (TREE_CODE (t) == ARRAY_TYPE
1217 && !TYPE_DOMAIN (t));
1220 /* True iff T is an N3639 array of runtime bound (VLA). These were approved
1221 for C++14 but then removed. This should only be used for N3639
1222 specifically; code wondering more generally if something is a VLA should use
1223 vla_type_p. */
1225 bool
1226 array_of_runtime_bound_p (tree t)
1228 if (!t || TREE_CODE (t) != ARRAY_TYPE)
1229 return false;
1230 if (variably_modified_type_p (TREE_TYPE (t), NULL_TREE))
1231 return false;
1232 tree dom = TYPE_DOMAIN (t);
1233 if (!dom)
1234 return false;
1235 tree max = TYPE_MAX_VALUE (dom);
1236 return (!potential_rvalue_constant_expression (max)
1237 || (!value_dependent_expression_p (max) && !TREE_CONSTANT (max)));
1240 /* True iff T is a variable length array. */
1242 bool
1243 vla_type_p (tree t)
1245 for (; t && TREE_CODE (t) == ARRAY_TYPE;
1246 t = TREE_TYPE (t))
1247 if (tree dom = TYPE_DOMAIN (t))
1249 tree max = TYPE_MAX_VALUE (dom);
1250 if (!potential_rvalue_constant_expression (max)
1251 || (!value_dependent_expression_p (max) && !TREE_CONSTANT (max)))
1252 return true;
1254 return false;
1258 /* Return a reference type node of MODE referring to TO_TYPE. If MODE
1259 is VOIDmode the standard pointer mode will be picked. If RVAL is
1260 true, return an rvalue reference type, otherwise return an lvalue
1261 reference type. If a type node exists, reuse it, otherwise create
1262 a new one. */
1263 tree
1264 cp_build_reference_type_for_mode (tree to_type, machine_mode mode, bool rval)
1266 tree lvalue_ref, t;
1268 if (to_type == error_mark_node)
1269 return error_mark_node;
1271 if (TYPE_REF_P (to_type))
1273 rval = rval && TYPE_REF_IS_RVALUE (to_type);
1274 to_type = TREE_TYPE (to_type);
1277 lvalue_ref = build_reference_type_for_mode (to_type, mode, false);
1279 if (!rval)
1280 return lvalue_ref;
1282 /* This code to create rvalue reference types is based on and tied
1283 to the code creating lvalue reference types in the middle-end
1284 functions build_reference_type_for_mode and build_reference_type.
1286 It works by putting the rvalue reference type nodes after the
1287 lvalue reference nodes in the TYPE_NEXT_REF_TO linked list, so
1288 they will effectively be ignored by the middle end. */
1290 for (t = lvalue_ref; (t = TYPE_NEXT_REF_TO (t)); )
1291 if (TYPE_REF_IS_RVALUE (t))
1292 return t;
1294 t = build_distinct_type_copy (lvalue_ref);
1296 TYPE_REF_IS_RVALUE (t) = true;
1297 TYPE_NEXT_REF_TO (t) = TYPE_NEXT_REF_TO (lvalue_ref);
1298 TYPE_NEXT_REF_TO (lvalue_ref) = t;
1300 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
1301 SET_TYPE_STRUCTURAL_EQUALITY (t);
1302 else if (TYPE_CANONICAL (to_type) != to_type)
1303 TYPE_CANONICAL (t)
1304 = cp_build_reference_type_for_mode (TYPE_CANONICAL (to_type), mode, rval);
1305 else
1306 TYPE_CANONICAL (t) = t;
1308 layout_type (t);
1310 return t;
1314 /* Return a reference type node referring to TO_TYPE. If RVAL is
1315 true, return an rvalue reference type, otherwise return an lvalue
1316 reference type. If a type node exists, reuse it, otherwise create
1317 a new one. */
1318 tree
1319 cp_build_reference_type (tree to_type, bool rval)
1321 return cp_build_reference_type_for_mode (to_type, VOIDmode, rval);
1324 /* Returns EXPR cast to rvalue reference type, like std::move. */
1326 tree
1327 move (tree expr)
1329 tree type = TREE_TYPE (expr);
1330 gcc_assert (!TYPE_REF_P (type));
1331 if (xvalue_p (expr))
1332 return expr;
1333 type = cp_build_reference_type (type, /*rval*/true);
1334 return build_static_cast (input_location, type, expr,
1335 tf_warning_or_error);
1338 /* Used by the C++ front end to build qualified array types. However,
1339 the C version of this function does not properly maintain canonical
1340 types (which are not used in C). */
1341 tree
1342 c_build_qualified_type (tree type, int type_quals, tree /* orig_qual_type */,
1343 size_t /* orig_qual_indirect */)
1345 return cp_build_qualified_type (type, type_quals);
1349 /* Make a variant of TYPE, qualified with the TYPE_QUALS. Handles
1350 arrays correctly. In particular, if TYPE is an array of T's, and
1351 TYPE_QUALS is non-empty, returns an array of qualified T's.
1353 FLAGS determines how to deal with ill-formed qualifications. If
1354 tf_ignore_bad_quals is set, then bad qualifications are dropped
1355 (this is permitted if TYPE was introduced via a typedef or template
1356 type parameter). If bad qualifications are dropped and tf_warning
1357 is set, then a warning is issued for non-const qualifications. If
1358 tf_ignore_bad_quals is not set and tf_error is not set, we
1359 return error_mark_node. Otherwise, we issue an error, and ignore
1360 the qualifications.
1362 Qualification of a reference type is valid when the reference came
1363 via a typedef or template type argument. [dcl.ref] No such
1364 dispensation is provided for qualifying a function type. [dcl.fct]
1365 DR 295 queries this and the proposed resolution brings it into line
1366 with qualifying a reference. We implement the DR. We also behave
1367 in a similar manner for restricting non-pointer types. */
1369 tree
1370 cp_build_qualified_type (tree type, int type_quals,
1371 tsubst_flags_t complain /* = tf_warning_or_error */)
1373 tree result;
1374 int bad_quals = TYPE_UNQUALIFIED;
1376 if (type == error_mark_node)
1377 return type;
1379 if (type_quals == cp_type_quals (type))
1380 return type;
1382 if (TREE_CODE (type) == ARRAY_TYPE)
1384 /* In C++, the qualification really applies to the array element
1385 type. Obtain the appropriately qualified element type. */
1386 tree t;
1387 tree element_type
1388 = cp_build_qualified_type (TREE_TYPE (type), type_quals, complain);
1390 if (element_type == error_mark_node)
1391 return error_mark_node;
1393 /* See if we already have an identically qualified type. Tests
1394 should be equivalent to those in check_qualified_type. */
1395 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1396 if (TREE_TYPE (t) == element_type
1397 && TYPE_NAME (t) == TYPE_NAME (type)
1398 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
1399 && attribute_list_equal (TYPE_ATTRIBUTES (t),
1400 TYPE_ATTRIBUTES (type)))
1401 break;
1403 if (!t)
1405 /* If we already know the dependentness, tell the array type
1406 constructor. This is important for module streaming, as we cannot
1407 dynamically determine that on read in. */
1408 t = build_cplus_array_type (element_type, TYPE_DOMAIN (type),
1409 TYPE_DEPENDENT_P_VALID (type)
1410 ? int (TYPE_DEPENDENT_P (type)) : -1);
1412 /* Keep the typedef name. */
1413 if (TYPE_NAME (t) != TYPE_NAME (type))
1415 t = build_variant_type_copy (t);
1416 TYPE_NAME (t) = TYPE_NAME (type);
1417 SET_TYPE_ALIGN (t, TYPE_ALIGN (type));
1418 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (type);
1422 /* Even if we already had this variant, we update
1423 TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case
1424 they changed since the variant was originally created.
1426 This seems hokey; if there is some way to use a previous
1427 variant *without* coming through here,
1428 TYPE_NEEDS_CONSTRUCTING will never be updated. */
1429 TYPE_NEEDS_CONSTRUCTING (t)
1430 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
1431 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1432 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
1433 return t;
1435 else if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
1437 tree t = PACK_EXPANSION_PATTERN (type);
1439 t = cp_build_qualified_type (t, type_quals, complain);
1440 return make_pack_expansion (t, complain);
1443 /* A reference or method type shall not be cv-qualified.
1444 [dcl.ref], [dcl.fct]. This used to be an error, but as of DR 295
1445 (in CD1) we always ignore extra cv-quals on functions. */
1447 /* [dcl.ref/1] Cv-qualified references are ill-formed except when
1448 the cv-qualifiers are introduced through the use of a typedef-name
1449 ([dcl.typedef], [temp.param]) or decltype-specifier
1450 ([dcl.type.decltype]),in which case the cv-qualifiers are
1451 ignored. */
1452 if (type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)
1453 && (TYPE_REF_P (type)
1454 || FUNC_OR_METHOD_TYPE_P (type)))
1456 if (TYPE_REF_P (type)
1457 && (!typedef_variant_p (type) || FUNC_OR_METHOD_TYPE_P (type)))
1458 bad_quals |= type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1459 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1462 /* But preserve any function-cv-quals on a FUNCTION_TYPE. */
1463 if (TREE_CODE (type) == FUNCTION_TYPE)
1464 type_quals |= type_memfn_quals (type);
1466 /* A restrict-qualified type must be a pointer (or reference)
1467 to object or incomplete type. */
1468 if ((type_quals & TYPE_QUAL_RESTRICT)
1469 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1470 && TREE_CODE (type) != TYPENAME_TYPE
1471 && !INDIRECT_TYPE_P (type))
1473 bad_quals |= TYPE_QUAL_RESTRICT;
1474 type_quals &= ~TYPE_QUAL_RESTRICT;
1477 if (bad_quals == TYPE_UNQUALIFIED
1478 || (complain & tf_ignore_bad_quals))
1479 /*OK*/;
1480 else if (!(complain & tf_error))
1481 return error_mark_node;
1482 else
1484 tree bad_type = build_qualified_type (ptr_type_node, bad_quals);
1485 error ("%qV qualifiers cannot be applied to %qT",
1486 bad_type, type);
1489 /* Retrieve (or create) the appropriately qualified variant. */
1490 result = build_qualified_type (type, type_quals);
1492 return result;
1495 /* Return TYPE with const and volatile removed. */
1497 tree
1498 cv_unqualified (tree type)
1500 int quals;
1502 if (type == error_mark_node)
1503 return type;
1505 quals = cp_type_quals (type);
1506 quals &= ~(TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE);
1507 return cp_build_qualified_type (type, quals);
1510 /* Subroutine of strip_typedefs. We want to apply to RESULT the attributes
1511 from ATTRIBS that affect type identity, and no others. If any are not
1512 applied, set *remove_attributes to true. */
1514 static tree
1515 apply_identity_attributes (tree result, tree attribs, bool *remove_attributes)
1517 tree first_ident = NULL_TREE;
1518 tree new_attribs = NULL_TREE;
1519 tree *p = &new_attribs;
1521 if (OVERLOAD_TYPE_P (result))
1523 /* On classes and enums all attributes are ingrained. */
1524 gcc_assert (attribs == TYPE_ATTRIBUTES (result));
1525 return result;
1528 for (tree a = attribs; a; a = TREE_CHAIN (a))
1530 const attribute_spec *as
1531 = lookup_attribute_spec (get_attribute_name (a));
1532 if (as && as->affects_type_identity)
1534 if (!first_ident)
1535 first_ident = a;
1536 else if (first_ident == error_mark_node)
1538 *p = tree_cons (TREE_PURPOSE (a), TREE_VALUE (a), NULL_TREE);
1539 p = &TREE_CHAIN (*p);
1542 else if (first_ident && first_ident != error_mark_node)
1544 for (tree a2 = first_ident; a2 != a; a2 = TREE_CHAIN (a2))
1546 *p = tree_cons (TREE_PURPOSE (a2), TREE_VALUE (a2), NULL_TREE);
1547 p = &TREE_CHAIN (*p);
1549 first_ident = error_mark_node;
1552 if (first_ident != error_mark_node)
1553 new_attribs = first_ident;
1555 if (first_ident == attribs)
1556 /* All attributes affected type identity. */;
1557 else
1558 *remove_attributes = true;
1560 return cp_build_type_attribute_variant (result, new_attribs);
1563 /* Builds a qualified variant of T that is either not a typedef variant
1564 (the default behavior) or not a typedef variant of a user-facing type
1565 (if FLAGS contains STF_USER_FACING). If T is not a type, then this
1566 just dispatches to strip_typedefs_expr.
1568 E.g. consider the following declarations:
1569 typedef const int ConstInt;
1570 typedef ConstInt* PtrConstInt;
1571 If T is PtrConstInt, this function returns a type representing
1572 const int*.
1573 In other words, if T is a typedef, the function returns the underlying type.
1574 The cv-qualification and attributes of the type returned match the
1575 input type.
1576 They will always be compatible types.
1577 The returned type is built so that all of its subtypes
1578 recursively have their typedefs stripped as well.
1580 This is different from just returning TYPE_CANONICAL (T)
1581 Because of several reasons:
1582 * If T is a type that needs structural equality
1583 its TYPE_CANONICAL (T) will be NULL.
1584 * TYPE_CANONICAL (T) desn't carry type attributes
1585 and loses template parameter names.
1587 If REMOVE_ATTRIBUTES is non-null, also strip attributes that don't
1588 affect type identity, and set the referent to true if any were
1589 stripped. */
1591 tree
1592 strip_typedefs (tree t, bool *remove_attributes /* = NULL */,
1593 unsigned int flags /* = 0 */)
1595 tree result = NULL, type = NULL, t0 = NULL;
1597 if (!t || t == error_mark_node)
1598 return t;
1600 if (!TYPE_P (t))
1601 return strip_typedefs_expr (t, remove_attributes, flags);
1603 if (t == TYPE_CANONICAL (t))
1604 return t;
1606 if (!(flags & STF_STRIP_DEPENDENT)
1607 && dependent_alias_template_spec_p (t, nt_opaque))
1608 /* DR 1558: However, if the template-id is dependent, subsequent
1609 template argument substitution still applies to the template-id. */
1610 return t;
1612 switch (TREE_CODE (t))
1614 case POINTER_TYPE:
1615 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1616 result = build_pointer_type_for_mode (type, TYPE_MODE (t), false);
1617 break;
1618 case REFERENCE_TYPE:
1619 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1620 result = cp_build_reference_type_for_mode (type, TYPE_MODE (t), TYPE_REF_IS_RVALUE (t));
1621 break;
1622 case OFFSET_TYPE:
1623 t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t), remove_attributes, flags);
1624 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1625 result = build_offset_type (t0, type);
1626 break;
1627 case RECORD_TYPE:
1628 if (TYPE_PTRMEMFUNC_P (t))
1630 t0 = strip_typedefs (TYPE_PTRMEMFUNC_FN_TYPE (t),
1631 remove_attributes, flags);
1632 result = build_ptrmemfunc_type (t0);
1634 break;
1635 case ARRAY_TYPE:
1636 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1637 t0 = strip_typedefs (TYPE_DOMAIN (t), remove_attributes, flags);
1638 gcc_checking_assert (TYPE_DEPENDENT_P_VALID (t)
1639 || !dependent_type_p (t));
1640 result = build_cplus_array_type (type, t0, TYPE_DEPENDENT_P (t));
1641 break;
1642 case FUNCTION_TYPE:
1643 case METHOD_TYPE:
1645 tree arg_types = NULL, arg_node, arg_node2, arg_type;
1646 bool changed;
1648 /* Because we stomp on TREE_PURPOSE of TYPE_ARG_TYPES in many places
1649 around the compiler (e.g. cp_parser_late_parsing_default_args), we
1650 can't expect that re-hashing a function type will find a previous
1651 equivalent type, so try to reuse the input type if nothing has
1652 changed. If the type is itself a variant, that will change. */
1653 bool is_variant = typedef_variant_p (t);
1654 if (remove_attributes
1655 && (TYPE_ATTRIBUTES (t) || TYPE_USER_ALIGN (t)))
1656 is_variant = true;
1658 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1659 tree canon_spec = (flag_noexcept_type
1660 ? canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (t))
1661 : NULL_TREE);
1662 changed = (type != TREE_TYPE (t) || is_variant
1663 || TYPE_RAISES_EXCEPTIONS (t) != canon_spec);
1665 for (arg_node = TYPE_ARG_TYPES (t);
1666 arg_node;
1667 arg_node = TREE_CHAIN (arg_node))
1669 if (arg_node == void_list_node)
1670 break;
1671 arg_type = strip_typedefs (TREE_VALUE (arg_node),
1672 remove_attributes, flags);
1673 gcc_assert (arg_type);
1674 if (arg_type == TREE_VALUE (arg_node) && !changed)
1675 continue;
1677 if (!changed)
1679 changed = true;
1680 for (arg_node2 = TYPE_ARG_TYPES (t);
1681 arg_node2 != arg_node;
1682 arg_node2 = TREE_CHAIN (arg_node2))
1683 arg_types
1684 = tree_cons (TREE_PURPOSE (arg_node2),
1685 TREE_VALUE (arg_node2), arg_types);
1688 arg_types
1689 = tree_cons (TREE_PURPOSE (arg_node), arg_type, arg_types);
1692 if (!changed)
1693 return t;
1695 if (arg_types)
1696 arg_types = nreverse (arg_types);
1698 /* A list of parameters not ending with an ellipsis
1699 must end with void_list_node. */
1700 if (arg_node)
1701 arg_types = chainon (arg_types, void_list_node);
1703 if (TREE_CODE (t) == METHOD_TYPE)
1705 tree class_type = TREE_TYPE (TREE_VALUE (arg_types));
1706 gcc_assert (class_type);
1707 result =
1708 build_method_type_directly (class_type, type,
1709 TREE_CHAIN (arg_types));
1711 else
1713 result = build_function_type (type, arg_types);
1714 result = apply_memfn_quals (result, type_memfn_quals (t));
1717 result = build_cp_fntype_variant (result,
1718 type_memfn_rqual (t), canon_spec,
1719 TYPE_HAS_LATE_RETURN_TYPE (t));
1721 break;
1722 case TYPENAME_TYPE:
1724 bool changed = false;
1725 tree fullname = TYPENAME_TYPE_FULLNAME (t);
1726 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
1727 && TREE_OPERAND (fullname, 1))
1729 tree args = TREE_OPERAND (fullname, 1);
1730 tree new_args = copy_node (args);
1731 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
1733 tree arg = TREE_VEC_ELT (args, i);
1734 tree strip_arg = strip_typedefs (arg, remove_attributes, flags);
1735 TREE_VEC_ELT (new_args, i) = strip_arg;
1736 if (strip_arg != arg)
1737 changed = true;
1739 if (changed)
1741 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_args)
1742 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
1743 fullname
1744 = lookup_template_function (TREE_OPERAND (fullname, 0),
1745 new_args);
1747 else
1748 ggc_free (new_args);
1750 tree ctx = strip_typedefs (TYPE_CONTEXT (t), remove_attributes, flags);
1751 if (!changed && ctx == TYPE_CONTEXT (t) && !typedef_variant_p (t))
1752 return t;
1753 tree name = fullname;
1754 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
1755 name = TREE_OPERAND (fullname, 0);
1756 /* Use build_typename_type rather than make_typename_type because we
1757 don't want to resolve it here, just strip typedefs. */
1758 result = build_typename_type (ctx, name, fullname, typename_type);
1760 break;
1761 case DECLTYPE_TYPE:
1762 result = strip_typedefs_expr (DECLTYPE_TYPE_EXPR (t),
1763 remove_attributes, flags);
1764 if (result == DECLTYPE_TYPE_EXPR (t))
1765 result = NULL_TREE;
1766 else
1767 result = (finish_decltype_type
1768 (result,
1769 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t),
1770 tf_none));
1771 break;
1772 case TRAIT_TYPE:
1774 tree type1 = strip_typedefs (TRAIT_TYPE_TYPE1 (t),
1775 remove_attributes, flags);
1776 tree type2 = strip_typedefs (TRAIT_TYPE_TYPE2 (t),
1777 remove_attributes, flags);
1778 if (type1 == TRAIT_TYPE_TYPE1 (t) && type2 == TRAIT_TYPE_TYPE2 (t))
1779 result = NULL_TREE;
1780 else
1781 result = finish_trait_type (TRAIT_TYPE_KIND (t), type1, type2,
1782 tf_warning_or_error);
1784 break;
1785 case TYPE_PACK_EXPANSION:
1787 tree pat = PACK_EXPANSION_PATTERN (t);
1788 if (TYPE_P (pat))
1790 type = strip_typedefs (pat, remove_attributes, flags);
1791 if (type != pat)
1793 result = build_distinct_type_copy (t);
1794 PACK_EXPANSION_PATTERN (result) = type;
1798 break;
1799 default:
1800 break;
1803 if (!result)
1805 if (typedef_variant_p (t))
1807 if ((flags & STF_USER_VISIBLE)
1808 && !user_facing_original_type_p (t))
1809 return t;
1810 /* If T is a non-template alias or typedef, we can assume that
1811 instantiating its definition will hit any substitution failure,
1812 so we don't need to retain it here as well. */
1813 if (!alias_template_specialization_p (t, nt_opaque))
1814 flags |= STF_STRIP_DEPENDENT;
1815 result = strip_typedefs (DECL_ORIGINAL_TYPE (TYPE_NAME (t)),
1816 remove_attributes, flags);
1818 else
1819 result = TYPE_MAIN_VARIANT (t);
1821 /*gcc_assert (!typedef_variant_p (result)
1822 || dependent_alias_template_spec_p (result, nt_opaque)
1823 || ((flags & STF_USER_VISIBLE)
1824 && !user_facing_original_type_p (result)));*/
1826 if (COMPLETE_TYPE_P (result) && !COMPLETE_TYPE_P (t))
1827 /* If RESULT is complete and T isn't, it's likely the case that T
1828 is a variant of RESULT which hasn't been updated yet. Skip the
1829 attribute handling. */;
1830 else
1832 if (TYPE_USER_ALIGN (t) != TYPE_USER_ALIGN (result)
1833 || TYPE_ALIGN (t) != TYPE_ALIGN (result))
1835 gcc_assert (TYPE_USER_ALIGN (t));
1836 if (remove_attributes)
1837 *remove_attributes = true;
1838 else
1840 if (TYPE_ALIGN (t) == TYPE_ALIGN (result))
1841 result = build_variant_type_copy (result);
1842 else
1843 result = build_aligned_type (result, TYPE_ALIGN (t));
1844 TYPE_USER_ALIGN (result) = true;
1848 if (TYPE_ATTRIBUTES (t))
1850 if (remove_attributes)
1851 result = apply_identity_attributes (result, TYPE_ATTRIBUTES (t),
1852 remove_attributes);
1853 else
1854 result = cp_build_type_attribute_variant (result,
1855 TYPE_ATTRIBUTES (t));
1859 return cp_build_qualified_type (result, cp_type_quals (t));
1862 /* Like strip_typedefs above, but works on expressions (and other
1863 non-types such as TREE_VEC), so that in
1865 template<class T> struct A
1867 typedef T TT;
1868 B<sizeof(TT)> b;
1871 sizeof(TT) is replaced by sizeof(T). */
1873 tree
1874 strip_typedefs_expr (tree t, bool *remove_attributes, unsigned int flags)
1876 unsigned i,n;
1877 tree r, type, *ops;
1878 enum tree_code code;
1880 if (t == NULL_TREE || t == error_mark_node)
1881 return t;
1883 STRIP_ANY_LOCATION_WRAPPER (t);
1885 if (DECL_P (t) || CONSTANT_CLASS_P (t))
1886 return t;
1888 code = TREE_CODE (t);
1889 switch (code)
1891 case IDENTIFIER_NODE:
1892 case TEMPLATE_PARM_INDEX:
1893 case OVERLOAD:
1894 case BASELINK:
1895 case ARGUMENT_PACK_SELECT:
1896 return t;
1898 case TRAIT_EXPR:
1900 tree type1 = strip_typedefs (TRAIT_EXPR_TYPE1 (t),
1901 remove_attributes, flags);
1902 tree type2 = strip_typedefs (TRAIT_EXPR_TYPE2 (t),
1903 remove_attributes, flags);
1904 if (type1 == TRAIT_EXPR_TYPE1 (t)
1905 && type2 == TRAIT_EXPR_TYPE2 (t))
1906 return t;
1907 r = copy_node (t);
1908 TRAIT_EXPR_TYPE1 (r) = type1;
1909 TRAIT_EXPR_TYPE2 (r) = type2;
1910 return r;
1913 case TREE_LIST:
1915 bool changed = false;
1916 releasing_vec vec;
1917 r = t;
1918 for (; t; t = TREE_CHAIN (t))
1920 gcc_assert (!TREE_PURPOSE (t));
1921 tree elt = strip_typedefs (TREE_VALUE (t),
1922 remove_attributes, flags);
1923 if (elt != TREE_VALUE (t))
1924 changed = true;
1925 vec_safe_push (vec, elt);
1927 if (changed)
1928 r = build_tree_list_vec (vec);
1929 return r;
1932 case TREE_VEC:
1934 bool changed = false;
1935 releasing_vec vec;
1936 n = TREE_VEC_LENGTH (t);
1937 vec_safe_reserve (vec, n);
1938 for (i = 0; i < n; ++i)
1940 tree op = strip_typedefs (TREE_VEC_ELT (t, i),
1941 remove_attributes, flags);
1942 vec->quick_push (op);
1943 if (op != TREE_VEC_ELT (t, i))
1944 changed = true;
1946 if (changed)
1948 r = copy_node (t);
1949 for (i = 0; i < n; ++i)
1950 TREE_VEC_ELT (r, i) = (*vec)[i];
1951 NON_DEFAULT_TEMPLATE_ARGS_COUNT (r)
1952 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
1954 else
1955 r = t;
1956 return r;
1959 case CONSTRUCTOR:
1961 bool changed = false;
1962 vec<constructor_elt, va_gc> *vec
1963 = vec_safe_copy (CONSTRUCTOR_ELTS (t));
1964 n = CONSTRUCTOR_NELTS (t);
1965 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1966 for (i = 0; i < n; ++i)
1968 constructor_elt *e = &(*vec)[i];
1969 tree op = strip_typedefs (e->value, remove_attributes, flags);
1970 if (op != e->value)
1972 changed = true;
1973 e->value = op;
1975 gcc_checking_assert
1976 (e->index == strip_typedefs (e->index, remove_attributes,
1977 flags));
1980 if (!changed && type == TREE_TYPE (t))
1982 vec_free (vec);
1983 return t;
1985 else
1987 r = copy_node (t);
1988 TREE_TYPE (r) = type;
1989 CONSTRUCTOR_ELTS (r) = vec;
1990 return r;
1994 case LAMBDA_EXPR:
1995 return t;
1997 case STATEMENT_LIST:
1998 error ("statement-expression in a constant expression");
1999 return error_mark_node;
2001 default:
2002 break;
2005 gcc_assert (EXPR_P (t));
2007 n = cp_tree_operand_length (t);
2008 ops = XALLOCAVEC (tree, n);
2009 type = TREE_TYPE (t);
2011 switch (code)
2013 CASE_CONVERT:
2014 case IMPLICIT_CONV_EXPR:
2015 case DYNAMIC_CAST_EXPR:
2016 case STATIC_CAST_EXPR:
2017 case CONST_CAST_EXPR:
2018 case REINTERPRET_CAST_EXPR:
2019 case CAST_EXPR:
2020 case NEW_EXPR:
2021 type = strip_typedefs (type, remove_attributes, flags);
2022 /* fallthrough */
2024 default:
2025 for (i = 0; i < n; ++i)
2026 ops[i] = strip_typedefs (TREE_OPERAND (t, i),
2027 remove_attributes, flags);
2028 break;
2031 /* If nothing changed, return t. */
2032 for (i = 0; i < n; ++i)
2033 if (ops[i] != TREE_OPERAND (t, i))
2034 break;
2035 if (i == n && type == TREE_TYPE (t))
2036 return t;
2038 r = copy_node (t);
2039 TREE_TYPE (r) = type;
2040 for (i = 0; i < n; ++i)
2041 TREE_OPERAND (r, i) = ops[i];
2042 return r;
2045 /* Makes a copy of BINFO and TYPE, which is to be inherited into a
2046 graph dominated by T. If BINFO is NULL, TYPE is a dependent base,
2047 and we do a shallow copy. If BINFO is non-NULL, we do a deep copy.
2048 VIRT indicates whether TYPE is inherited virtually or not.
2049 IGO_PREV points at the previous binfo of the inheritance graph
2050 order chain. The newly copied binfo's TREE_CHAIN forms this
2051 ordering.
2053 The CLASSTYPE_VBASECLASSES vector of T is constructed in the
2054 correct order. That is in the order the bases themselves should be
2055 constructed in.
2057 The BINFO_INHERITANCE of a virtual base class points to the binfo
2058 of the most derived type. ??? We could probably change this so that
2059 BINFO_INHERITANCE becomes synonymous with BINFO_PRIMARY, and hence
2060 remove a field. They currently can only differ for primary virtual
2061 virtual bases. */
2063 tree
2064 copy_binfo (tree binfo, tree type, tree t, tree *igo_prev, int virt)
2066 tree new_binfo;
2068 if (virt)
2070 /* See if we've already made this virtual base. */
2071 new_binfo = binfo_for_vbase (type, t);
2072 if (new_binfo)
2073 return new_binfo;
2076 new_binfo = make_tree_binfo (binfo ? BINFO_N_BASE_BINFOS (binfo) : 0);
2077 BINFO_TYPE (new_binfo) = type;
2079 /* Chain it into the inheritance graph. */
2080 TREE_CHAIN (*igo_prev) = new_binfo;
2081 *igo_prev = new_binfo;
2083 if (binfo && !BINFO_DEPENDENT_BASE_P (binfo))
2085 int ix;
2086 tree base_binfo;
2088 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), type));
2090 BINFO_OFFSET (new_binfo) = BINFO_OFFSET (binfo);
2091 BINFO_VIRTUALS (new_binfo) = BINFO_VIRTUALS (binfo);
2093 /* We do not need to copy the accesses, as they are read only. */
2094 BINFO_BASE_ACCESSES (new_binfo) = BINFO_BASE_ACCESSES (binfo);
2096 /* Recursively copy base binfos of BINFO. */
2097 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2099 tree new_base_binfo;
2100 new_base_binfo = copy_binfo (base_binfo, BINFO_TYPE (base_binfo),
2101 t, igo_prev,
2102 BINFO_VIRTUAL_P (base_binfo));
2104 if (!BINFO_INHERITANCE_CHAIN (new_base_binfo))
2105 BINFO_INHERITANCE_CHAIN (new_base_binfo) = new_binfo;
2106 BINFO_BASE_APPEND (new_binfo, new_base_binfo);
2109 else
2110 BINFO_DEPENDENT_BASE_P (new_binfo) = 1;
2112 if (virt)
2114 /* Push it onto the list after any virtual bases it contains
2115 will have been pushed. */
2116 CLASSTYPE_VBASECLASSES (t)->quick_push (new_binfo);
2117 BINFO_VIRTUAL_P (new_binfo) = 1;
2118 BINFO_INHERITANCE_CHAIN (new_binfo) = TYPE_BINFO (t);
2121 return new_binfo;
2124 /* Hashing of lists so that we don't make duplicates.
2125 The entry point is `list_hash_canon'. */
2127 struct list_proxy
2129 tree purpose;
2130 tree value;
2131 tree chain;
2134 struct list_hasher : ggc_ptr_hash<tree_node>
2136 typedef list_proxy *compare_type;
2138 static hashval_t hash (tree);
2139 static bool equal (tree, list_proxy *);
2142 /* Now here is the hash table. When recording a list, it is added
2143 to the slot whose index is the hash code mod the table size.
2144 Note that the hash table is used for several kinds of lists.
2145 While all these live in the same table, they are completely independent,
2146 and the hash code is computed differently for each of these. */
2148 static GTY (()) hash_table<list_hasher> *list_hash_table;
2150 /* Compare ENTRY (an entry in the hash table) with DATA (a list_proxy
2151 for a node we are thinking about adding). */
2153 bool
2154 list_hasher::equal (tree t, list_proxy *proxy)
2156 return (TREE_VALUE (t) == proxy->value
2157 && TREE_PURPOSE (t) == proxy->purpose
2158 && TREE_CHAIN (t) == proxy->chain);
2161 /* Compute a hash code for a list (chain of TREE_LIST nodes
2162 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
2163 TREE_COMMON slots), by adding the hash codes of the individual entries. */
2165 static hashval_t
2166 list_hash_pieces (tree purpose, tree value, tree chain)
2168 hashval_t hashcode = 0;
2170 if (chain)
2171 hashcode += TREE_HASH (chain);
2173 if (value)
2174 hashcode += TREE_HASH (value);
2175 else
2176 hashcode += 1007;
2177 if (purpose)
2178 hashcode += TREE_HASH (purpose);
2179 else
2180 hashcode += 1009;
2181 return hashcode;
2184 /* Hash an already existing TREE_LIST. */
2186 hashval_t
2187 list_hasher::hash (tree t)
2189 return list_hash_pieces (TREE_PURPOSE (t),
2190 TREE_VALUE (t),
2191 TREE_CHAIN (t));
2194 /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
2195 object for an identical list if one already exists. Otherwise, build a
2196 new one, and record it as the canonical object. */
2198 tree
2199 hash_tree_cons (tree purpose, tree value, tree chain)
2201 int hashcode = 0;
2202 tree *slot;
2203 struct list_proxy proxy;
2205 /* Hash the list node. */
2206 hashcode = list_hash_pieces (purpose, value, chain);
2207 /* Create a proxy for the TREE_LIST we would like to create. We
2208 don't actually create it so as to avoid creating garbage. */
2209 proxy.purpose = purpose;
2210 proxy.value = value;
2211 proxy.chain = chain;
2212 /* See if it is already in the table. */
2213 slot = list_hash_table->find_slot_with_hash (&proxy, hashcode, INSERT);
2214 /* If not, create a new node. */
2215 if (!*slot)
2216 *slot = tree_cons (purpose, value, chain);
2217 return (tree) *slot;
2220 /* Constructor for hashed lists. */
2222 tree
2223 hash_tree_chain (tree value, tree chain)
2225 return hash_tree_cons (NULL_TREE, value, chain);
2228 void
2229 debug_binfo (tree elem)
2231 HOST_WIDE_INT n;
2232 tree virtuals;
2234 fprintf (stderr, "type \"%s\", offset = " HOST_WIDE_INT_PRINT_DEC
2235 "\nvtable type:\n",
2236 TYPE_NAME_STRING (BINFO_TYPE (elem)),
2237 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
2238 debug_tree (BINFO_TYPE (elem));
2239 if (BINFO_VTABLE (elem))
2240 fprintf (stderr, "vtable decl \"%s\"\n",
2241 IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem))));
2242 else
2243 fprintf (stderr, "no vtable decl yet\n");
2244 fprintf (stderr, "virtuals:\n");
2245 virtuals = BINFO_VIRTUALS (elem);
2246 n = 0;
2248 while (virtuals)
2250 tree fndecl = TREE_VALUE (virtuals);
2251 fprintf (stderr, "%s [%ld =? %ld]\n",
2252 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
2253 (long) n, (long) TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
2254 ++n;
2255 virtuals = TREE_CHAIN (virtuals);
2259 /* Build a representation for the qualified name SCOPE::NAME. TYPE is
2260 the type of the result expression, if known, or NULL_TREE if the
2261 resulting expression is type-dependent. If TEMPLATE_P is true,
2262 NAME is known to be a template because the user explicitly used the
2263 "template" keyword after the "::".
2265 All SCOPE_REFs should be built by use of this function. */
2267 tree
2268 build_qualified_name (tree type, tree scope, tree name, bool template_p)
2270 tree t;
2271 if (type == error_mark_node
2272 || scope == error_mark_node
2273 || name == error_mark_node)
2274 return error_mark_node;
2275 gcc_assert (TREE_CODE (name) != SCOPE_REF);
2276 t = build2 (SCOPE_REF, type, scope, name);
2277 QUALIFIED_NAME_IS_TEMPLATE (t) = template_p;
2278 PTRMEM_OK_P (t) = true;
2279 if (type)
2280 t = convert_from_reference (t);
2281 return t;
2284 /* Like check_qualified_type, but also check ref-qualifier, exception
2285 specification, and whether the return type was specified after the
2286 parameters. */
2288 static bool
2289 cp_check_qualified_type (const_tree cand, const_tree base, int type_quals,
2290 cp_ref_qualifier rqual, tree raises, bool late)
2292 return (TYPE_QUALS (cand) == type_quals
2293 && check_base_type (cand, base)
2294 && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (cand),
2295 ce_exact)
2296 && TYPE_HAS_LATE_RETURN_TYPE (cand) == late
2297 && type_memfn_rqual (cand) == rqual);
2300 /* Build the FUNCTION_TYPE or METHOD_TYPE with the ref-qualifier RQUAL. */
2302 tree
2303 build_ref_qualified_type (tree type, cp_ref_qualifier rqual)
2305 tree raises = TYPE_RAISES_EXCEPTIONS (type);
2306 bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
2307 return build_cp_fntype_variant (type, rqual, raises, late);
2310 tree
2311 make_binding_vec (tree name, unsigned clusters MEM_STAT_DECL)
2313 /* Stored in an unsigned short, but we're limited to the number of
2314 modules anyway. */
2315 gcc_checking_assert (clusters <= (unsigned short)(~0));
2316 size_t length = (offsetof (tree_binding_vec, vec)
2317 + clusters * sizeof (binding_cluster));
2318 tree vec = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2319 TREE_SET_CODE (vec, BINDING_VECTOR);
2320 BINDING_VECTOR_NAME (vec) = name;
2321 BINDING_VECTOR_ALLOC_CLUSTERS (vec) = clusters;
2322 BINDING_VECTOR_NUM_CLUSTERS (vec) = 0;
2324 return vec;
2327 /* Make a raw overload node containing FN. */
2329 tree
2330 ovl_make (tree fn, tree next)
2332 tree result = make_node (OVERLOAD);
2334 if (TREE_CODE (fn) == OVERLOAD)
2335 OVL_NESTED_P (result) = true;
2337 TREE_TYPE (result) = (next || TREE_CODE (fn) == TEMPLATE_DECL
2338 ? unknown_type_node : TREE_TYPE (fn));
2339 if (next && TREE_CODE (next) == OVERLOAD && OVL_DEDUP_P (next))
2340 OVL_DEDUP_P (result) = true;
2341 OVL_FUNCTION (result) = fn;
2342 OVL_CHAIN (result) = next;
2343 return result;
2346 /* Add FN to the (potentially NULL) overload set OVL. USING_OR_HIDDEN is >
2347 zero if this is a using-decl. It is > 1 if we're exporting the
2348 using decl. USING_OR_HIDDEN is < 0, if FN is hidden. (A decl
2349 cannot be both using and hidden.) We keep the hidden decls first,
2350 but remaining ones are unordered. */
2352 tree
2353 ovl_insert (tree fn, tree maybe_ovl, int using_or_hidden)
2355 tree result = maybe_ovl;
2356 tree insert_after = NULL_TREE;
2358 /* Skip hidden. */
2359 for (; maybe_ovl && TREE_CODE (maybe_ovl) == OVERLOAD
2360 && OVL_HIDDEN_P (maybe_ovl);
2361 maybe_ovl = OVL_CHAIN (maybe_ovl))
2363 gcc_checking_assert (!OVL_LOOKUP_P (maybe_ovl));
2364 insert_after = maybe_ovl;
2367 if (maybe_ovl || using_or_hidden || TREE_CODE (fn) == TEMPLATE_DECL)
2369 maybe_ovl = ovl_make (fn, maybe_ovl);
2371 if (using_or_hidden < 0)
2372 OVL_HIDDEN_P (maybe_ovl) = true;
2373 if (using_or_hidden > 0)
2375 OVL_DEDUP_P (maybe_ovl) = OVL_USING_P (maybe_ovl) = true;
2376 if (using_or_hidden > 1)
2377 OVL_EXPORT_P (maybe_ovl) = true;
2380 else
2381 maybe_ovl = fn;
2383 if (insert_after)
2385 OVL_CHAIN (insert_after) = maybe_ovl;
2386 TREE_TYPE (insert_after) = unknown_type_node;
2388 else
2389 result = maybe_ovl;
2391 return result;
2394 /* Skip any hidden names at the beginning of OVL. */
2396 tree
2397 ovl_skip_hidden (tree ovl)
2399 while (ovl && TREE_CODE (ovl) == OVERLOAD && OVL_HIDDEN_P (ovl))
2400 ovl = OVL_CHAIN (ovl);
2402 return ovl;
2405 /* NODE is an OVL_HIDDEN_P node that is now revealed. */
2407 tree
2408 ovl_iterator::reveal_node (tree overload, tree node)
2410 /* We cannot have returned NODE as part of a lookup overload, so we
2411 don't have to worry about preserving that. */
2413 OVL_HIDDEN_P (node) = false;
2414 if (tree chain = OVL_CHAIN (node))
2415 if (TREE_CODE (chain) == OVERLOAD)
2417 if (OVL_HIDDEN_P (chain))
2419 /* The node needs moving, and the simplest way is to remove it
2420 and reinsert. */
2421 overload = remove_node (overload, node);
2422 overload = ovl_insert (OVL_FUNCTION (node), overload);
2424 else if (OVL_DEDUP_P (chain))
2425 OVL_DEDUP_P (node) = true;
2427 return overload;
2430 /* NODE is on the overloads of OVL. Remove it.
2431 The removed node is unaltered and may continue to be iterated
2432 from (i.e. it is safe to remove a node from an overload one is
2433 currently iterating over). */
2435 tree
2436 ovl_iterator::remove_node (tree overload, tree node)
2438 tree *slot = &overload;
2439 while (*slot != node)
2441 tree probe = *slot;
2442 gcc_checking_assert (!OVL_LOOKUP_P (probe));
2444 slot = &OVL_CHAIN (probe);
2447 /* Stitch out NODE. We don't have to worry about now making a
2448 singleton overload (and consequently maybe setting its type),
2449 because all uses of this function will be followed by inserting a
2450 new node that must follow the place we've cut this out from. */
2451 if (TREE_CODE (node) != OVERLOAD)
2452 /* Cloned inherited ctors don't mark themselves as via_using. */
2453 *slot = NULL_TREE;
2454 else
2455 *slot = OVL_CHAIN (node);
2457 return overload;
2460 /* Mark or unmark a lookup set. */
2462 void
2463 lookup_mark (tree ovl, bool val)
2465 for (lkp_iterator iter (ovl); iter; ++iter)
2467 gcc_checking_assert (LOOKUP_SEEN_P (*iter) != val);
2468 LOOKUP_SEEN_P (*iter) = val;
2472 /* Add a set of new FNS into a lookup. */
2474 tree
2475 lookup_add (tree fns, tree lookup)
2477 if (fns == error_mark_node || lookup == error_mark_node)
2478 return error_mark_node;
2480 if (lookup || TREE_CODE (fns) == TEMPLATE_DECL)
2482 lookup = ovl_make (fns, lookup);
2483 OVL_LOOKUP_P (lookup) = true;
2485 else
2486 lookup = fns;
2488 return lookup;
2491 /* FNS is a new overload set, add them to LOOKUP, if they are not
2492 already present there. */
2494 tree
2495 lookup_maybe_add (tree fns, tree lookup, bool deduping)
2497 if (deduping)
2498 for (tree next, probe = fns; probe; probe = next)
2500 tree fn = probe;
2501 next = NULL_TREE;
2503 if (TREE_CODE (probe) == OVERLOAD)
2505 fn = OVL_FUNCTION (probe);
2506 next = OVL_CHAIN (probe);
2509 if (!LOOKUP_SEEN_P (fn))
2510 LOOKUP_SEEN_P (fn) = true;
2511 else
2513 /* This function was already seen. Insert all the
2514 predecessors onto the lookup. */
2515 for (; fns != probe; fns = OVL_CHAIN (fns))
2517 lookup = lookup_add (OVL_FUNCTION (fns), lookup);
2518 /* Propagate OVL_USING, but OVL_HIDDEN &
2519 OVL_DEDUP_P don't matter. */
2520 if (OVL_USING_P (fns))
2521 OVL_USING_P (lookup) = true;
2524 /* And now skip this function. */
2525 fns = next;
2529 if (fns)
2530 /* We ended in a set of new functions. Add them all in one go. */
2531 lookup = lookup_add (fns, lookup);
2533 return lookup;
2536 /* Returns nonzero if X is an expression for a (possibly overloaded)
2537 function. If "f" is a function or function template, "f", "c->f",
2538 "c.f", "C::f", and "f<int>" will all be considered possibly
2539 overloaded functions. Returns 2 if the function is actually
2540 overloaded, i.e., if it is impossible to know the type of the
2541 function without performing overload resolution. */
2544 is_overloaded_fn (tree x)
2546 STRIP_ANY_LOCATION_WRAPPER (x);
2548 /* A baselink is also considered an overloaded function. */
2549 if (TREE_CODE (x) == OFFSET_REF
2550 || TREE_CODE (x) == COMPONENT_REF)
2551 x = TREE_OPERAND (x, 1);
2552 x = MAYBE_BASELINK_FUNCTIONS (x);
2553 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
2554 x = TREE_OPERAND (x, 0);
2556 if (DECL_FUNCTION_TEMPLATE_P (OVL_FIRST (x))
2557 || (TREE_CODE (x) == OVERLOAD && !OVL_SINGLE_P (x)))
2558 return 2;
2560 return OVL_P (x);
2563 /* X is the CALL_EXPR_FN of a CALL_EXPR. If X represents a dependent name
2564 (14.6.2), return the IDENTIFIER_NODE for that name. Otherwise, return
2565 NULL_TREE. */
2567 tree
2568 dependent_name (tree x)
2570 /* FIXME a dependent name must be unqualified, but this function doesn't
2571 distinguish between qualified and unqualified identifiers. */
2572 if (identifier_p (x))
2573 return x;
2574 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
2575 x = TREE_OPERAND (x, 0);
2576 if (OVL_P (x))
2577 return OVL_NAME (x);
2578 return NULL_TREE;
2581 /* Like dependent_name, but instead takes a CALL_EXPR and also checks
2582 its dependence. */
2584 tree
2585 call_expr_dependent_name (tree x)
2587 if (TREE_TYPE (x) != NULL_TREE)
2588 /* X isn't dependent, so its callee isn't a dependent name. */
2589 return NULL_TREE;
2590 return dependent_name (CALL_EXPR_FN (x));
2593 /* Returns true iff X is an expression for an overloaded function
2594 whose type cannot be known without performing overload
2595 resolution. */
2597 bool
2598 really_overloaded_fn (tree x)
2600 return is_overloaded_fn (x) == 2;
2603 /* Get the overload set FROM refers to. Returns NULL if it's not an
2604 overload set. */
2606 tree
2607 maybe_get_fns (tree from)
2609 STRIP_ANY_LOCATION_WRAPPER (from);
2611 /* A baselink is also considered an overloaded function. */
2612 if (TREE_CODE (from) == OFFSET_REF
2613 || TREE_CODE (from) == COMPONENT_REF)
2614 from = TREE_OPERAND (from, 1);
2615 if (BASELINK_P (from))
2616 from = BASELINK_FUNCTIONS (from);
2617 if (TREE_CODE (from) == TEMPLATE_ID_EXPR)
2618 from = TREE_OPERAND (from, 0);
2620 if (OVL_P (from))
2621 return from;
2623 return NULL;
2626 /* FROM refers to an overload set. Return that set (or die). */
2628 tree
2629 get_fns (tree from)
2631 tree res = maybe_get_fns (from);
2633 gcc_assert (res);
2634 return res;
2637 /* Return the first function of the overload set FROM refers to. */
2639 tree
2640 get_first_fn (tree from)
2642 return OVL_FIRST (get_fns (from));
2645 /* Return the scope where the overloaded functions OVL were found. */
2647 tree
2648 ovl_scope (tree ovl)
2650 if (TREE_CODE (ovl) == OFFSET_REF
2651 || TREE_CODE (ovl) == COMPONENT_REF)
2652 ovl = TREE_OPERAND (ovl, 1);
2653 if (TREE_CODE (ovl) == BASELINK)
2654 return BINFO_TYPE (BASELINK_BINFO (ovl));
2655 if (TREE_CODE (ovl) == TEMPLATE_ID_EXPR)
2656 ovl = TREE_OPERAND (ovl, 0);
2657 /* Skip using-declarations. */
2658 lkp_iterator iter (ovl);
2660 ovl = *iter;
2661 while (iter.using_p () && ++iter);
2663 return CP_DECL_CONTEXT (ovl);
2666 #define PRINT_RING_SIZE 4
2668 static const char *
2669 cxx_printable_name_internal (tree decl, int v, bool translate)
2671 static unsigned int uid_ring[PRINT_RING_SIZE];
2672 static char *print_ring[PRINT_RING_SIZE];
2673 static bool trans_ring[PRINT_RING_SIZE];
2674 static int ring_counter;
2675 int i;
2677 /* Only cache functions. */
2678 if (v < 2
2679 || TREE_CODE (decl) != FUNCTION_DECL
2680 || DECL_LANG_SPECIFIC (decl) == 0)
2681 return lang_decl_name (decl, v, translate);
2683 /* See if this print name is lying around. */
2684 for (i = 0; i < PRINT_RING_SIZE; i++)
2685 if (uid_ring[i] == DECL_UID (decl) && translate == trans_ring[i])
2686 /* yes, so return it. */
2687 return print_ring[i];
2689 if (++ring_counter == PRINT_RING_SIZE)
2690 ring_counter = 0;
2692 if (current_function_decl != NULL_TREE)
2694 /* There may be both translated and untranslated versions of the
2695 name cached. */
2696 for (i = 0; i < 2; i++)
2698 if (uid_ring[ring_counter] == DECL_UID (current_function_decl))
2699 ring_counter += 1;
2700 if (ring_counter == PRINT_RING_SIZE)
2701 ring_counter = 0;
2703 gcc_assert (uid_ring[ring_counter] != DECL_UID (current_function_decl));
2706 free (print_ring[ring_counter]);
2708 print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v, translate));
2709 uid_ring[ring_counter] = DECL_UID (decl);
2710 trans_ring[ring_counter] = translate;
2711 return print_ring[ring_counter];
2714 const char *
2715 cxx_printable_name (tree decl, int v)
2717 return cxx_printable_name_internal (decl, v, false);
2720 const char *
2721 cxx_printable_name_translate (tree decl, int v)
2723 return cxx_printable_name_internal (decl, v, true);
2726 /* Return the canonical version of exception-specification RAISES for a C++17
2727 function type, for use in type comparison and building TYPE_CANONICAL. */
2729 tree
2730 canonical_eh_spec (tree raises)
2732 if (raises == NULL_TREE)
2733 return raises;
2734 else if (DEFERRED_NOEXCEPT_SPEC_P (raises)
2735 || UNPARSED_NOEXCEPT_SPEC_P (raises)
2736 || uses_template_parms (raises)
2737 || uses_template_parms (TREE_PURPOSE (raises)))
2738 /* Keep a dependent or deferred exception specification. */
2739 return raises;
2740 else if (nothrow_spec_p (raises))
2741 /* throw() -> noexcept. */
2742 return noexcept_true_spec;
2743 else
2744 /* For C++17 type matching, anything else -> nothing. */
2745 return NULL_TREE;
2748 tree
2749 build_cp_fntype_variant (tree type, cp_ref_qualifier rqual,
2750 tree raises, bool late)
2752 cp_cv_quals type_quals = TYPE_QUALS (type);
2754 if (cp_check_qualified_type (type, type, type_quals, rqual, raises, late))
2755 return type;
2757 tree v = TYPE_MAIN_VARIANT (type);
2758 for (; v; v = TYPE_NEXT_VARIANT (v))
2759 if (cp_check_qualified_type (v, type, type_quals, rqual, raises, late))
2760 return v;
2762 /* Need to build a new variant. */
2763 v = build_variant_type_copy (type);
2764 if (!TYPE_DEPENDENT_P (v))
2765 /* We no longer know that it's not type-dependent. */
2766 TYPE_DEPENDENT_P_VALID (v) = false;
2767 TYPE_RAISES_EXCEPTIONS (v) = raises;
2768 TYPE_HAS_LATE_RETURN_TYPE (v) = late;
2769 switch (rqual)
2771 case REF_QUAL_RVALUE:
2772 FUNCTION_RVALUE_QUALIFIED (v) = 1;
2773 FUNCTION_REF_QUALIFIED (v) = 1;
2774 break;
2775 case REF_QUAL_LVALUE:
2776 FUNCTION_RVALUE_QUALIFIED (v) = 0;
2777 FUNCTION_REF_QUALIFIED (v) = 1;
2778 break;
2779 default:
2780 FUNCTION_REF_QUALIFIED (v) = 0;
2781 break;
2784 /* Canonicalize the exception specification. */
2785 tree cr = flag_noexcept_type ? canonical_eh_spec (raises) : NULL_TREE;
2787 if (TYPE_STRUCTURAL_EQUALITY_P (type))
2788 /* Propagate structural equality. */
2789 SET_TYPE_STRUCTURAL_EQUALITY (v);
2790 else if (TYPE_CANONICAL (type) != type || cr != raises || late)
2791 /* Build the underlying canonical type, since it is different
2792 from TYPE. */
2793 TYPE_CANONICAL (v) = build_cp_fntype_variant (TYPE_CANONICAL (type),
2794 rqual, cr, false);
2795 else
2796 /* T is its own canonical type. */
2797 TYPE_CANONICAL (v) = v;
2799 return v;
2802 /* TYPE is a function or method type with a deferred exception
2803 specification that has been parsed to RAISES. Fixup all the type
2804 variants that are affected in place. Via decltype &| noexcept
2805 tricks, the unparsed spec could have escaped into the type system.
2806 The general case is hard to fixup canonical types for. */
2808 void
2809 fixup_deferred_exception_variants (tree type, tree raises)
2811 tree original = TYPE_RAISES_EXCEPTIONS (type);
2812 tree cr = flag_noexcept_type ? canonical_eh_spec (raises) : NULL_TREE;
2814 gcc_checking_assert (UNPARSED_NOEXCEPT_SPEC_P (original));
2816 /* Though sucky, this walk will process the canonical variants
2817 first. */
2818 tree prev = NULL_TREE;
2819 for (tree variant = TYPE_MAIN_VARIANT (type);
2820 variant; prev = variant, variant = TYPE_NEXT_VARIANT (variant))
2821 if (TYPE_RAISES_EXCEPTIONS (variant) == original)
2823 gcc_checking_assert (variant != TYPE_MAIN_VARIANT (type));
2825 if (!TYPE_STRUCTURAL_EQUALITY_P (variant))
2827 cp_cv_quals var_quals = TYPE_QUALS (variant);
2828 cp_ref_qualifier rqual = type_memfn_rqual (variant);
2830 /* If VARIANT would become a dup (cp_check_qualified_type-wise)
2831 of an existing variant in the variant list of TYPE after its
2832 exception specification has been parsed, elide it. Otherwise,
2833 build_cp_fntype_variant could use it, leading to "canonical
2834 types differ for identical types." */
2835 tree v = TYPE_MAIN_VARIANT (type);
2836 for (; v; v = TYPE_NEXT_VARIANT (v))
2837 if (cp_check_qualified_type (v, variant, var_quals,
2838 rqual, cr, false))
2840 /* The main variant will not match V, so PREV will never
2841 be null. */
2842 TYPE_NEXT_VARIANT (prev) = TYPE_NEXT_VARIANT (variant);
2843 break;
2845 TYPE_RAISES_EXCEPTIONS (variant) = raises;
2847 if (!v)
2848 v = build_cp_fntype_variant (TYPE_CANONICAL (variant),
2849 rqual, cr, false);
2850 TYPE_CANONICAL (variant) = TYPE_CANONICAL (v);
2852 else
2853 TYPE_RAISES_EXCEPTIONS (variant) = raises;
2855 if (!TYPE_DEPENDENT_P (variant))
2856 /* We no longer know that it's not type-dependent. */
2857 TYPE_DEPENDENT_P_VALID (variant) = false;
2861 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
2862 listed in RAISES. */
2864 tree
2865 build_exception_variant (tree type, tree raises)
2867 cp_ref_qualifier rqual = type_memfn_rqual (type);
2868 bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
2869 return build_cp_fntype_variant (type, rqual, raises, late);
2872 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new
2873 BOUND_TEMPLATE_TEMPLATE_PARM bound with NEWARGS as its template
2874 arguments. */
2876 tree
2877 bind_template_template_parm (tree t, tree newargs)
2879 tree decl = TYPE_NAME (t);
2880 tree t2;
2882 t2 = cxx_make_type (BOUND_TEMPLATE_TEMPLATE_PARM);
2883 decl = build_decl (input_location,
2884 TYPE_DECL, DECL_NAME (decl), NULL_TREE);
2885 SET_DECL_TEMPLATE_PARM_P (decl);
2887 /* These nodes have to be created to reflect new TYPE_DECL and template
2888 arguments. */
2889 TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t));
2890 TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl;
2891 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
2892 = build_template_info (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t), newargs);
2894 TREE_TYPE (decl) = t2;
2895 TYPE_NAME (t2) = decl;
2896 TYPE_STUB_DECL (t2) = decl;
2897 TYPE_SIZE (t2) = 0;
2899 if (any_template_arguments_need_structural_equality_p (newargs))
2900 SET_TYPE_STRUCTURAL_EQUALITY (t2);
2901 else
2902 TYPE_CANONICAL (t2) = canonical_type_parameter (t2);
2904 return t2;
2907 /* Called from count_trees via walk_tree. */
2909 static tree
2910 count_trees_r (tree *tp, int *walk_subtrees, void *data)
2912 ++*((int *) data);
2914 if (TYPE_P (*tp))
2915 *walk_subtrees = 0;
2917 return NULL_TREE;
2920 /* Debugging function for measuring the rough complexity of a tree
2921 representation. */
2924 count_trees (tree t)
2926 int n_trees = 0;
2927 cp_walk_tree_without_duplicates (&t, count_trees_r, &n_trees);
2928 return n_trees;
2931 /* Called from verify_stmt_tree via walk_tree. */
2933 static tree
2934 verify_stmt_tree_r (tree* tp, int * /*walk_subtrees*/, void* data)
2936 tree t = *tp;
2937 hash_table<nofree_ptr_hash <tree_node> > *statements
2938 = static_cast <hash_table<nofree_ptr_hash <tree_node> > *> (data);
2939 tree_node **slot;
2941 if (!STATEMENT_CODE_P (TREE_CODE (t)))
2942 return NULL_TREE;
2944 /* If this statement is already present in the hash table, then
2945 there is a circularity in the statement tree. */
2946 gcc_assert (!statements->find (t));
2948 slot = statements->find_slot (t, INSERT);
2949 *slot = t;
2951 return NULL_TREE;
2954 /* Debugging function to check that the statement T has not been
2955 corrupted. For now, this function simply checks that T contains no
2956 circularities. */
2958 void
2959 verify_stmt_tree (tree t)
2961 hash_table<nofree_ptr_hash <tree_node> > statements (37);
2962 cp_walk_tree (&t, verify_stmt_tree_r, &statements, NULL);
2965 /* Check if the type T depends on a type with no linkage and if so,
2966 return it. If RELAXED_P then do not consider a class type declared
2967 within a vague-linkage function to have no linkage. Remember:
2968 no-linkage is not the same as internal-linkage. */
2970 tree
2971 no_linkage_check (tree t, bool relaxed_p)
2973 tree r;
2975 /* Lambda types that don't have mangling scope have no linkage. We
2976 check CLASSTYPE_LAMBDA_EXPR for error_mark_node because
2977 when we get here from pushtag none of the lambda information is
2978 set up yet, so we want to assume that the lambda has linkage and
2979 fix it up later if not. We need to check this even in templates so
2980 that we properly handle a lambda-expression in the signature. */
2981 if (LAMBDA_TYPE_P (t)
2982 && CLASSTYPE_LAMBDA_EXPR (t) != error_mark_node)
2984 tree extra = LAMBDA_TYPE_EXTRA_SCOPE (t);
2985 if (!extra)
2986 return t;
2989 /* Otherwise there's no point in checking linkage on template functions; we
2990 can't know their complete types. */
2991 if (processing_template_decl)
2992 return NULL_TREE;
2994 switch (TREE_CODE (t))
2996 case RECORD_TYPE:
2997 if (TYPE_PTRMEMFUNC_P (t))
2998 goto ptrmem;
2999 /* Fall through. */
3000 case UNION_TYPE:
3001 if (!CLASS_TYPE_P (t))
3002 return NULL_TREE;
3003 /* Fall through. */
3004 case ENUMERAL_TYPE:
3005 /* Only treat unnamed types as having no linkage if they're at
3006 namespace scope. This is core issue 966. */
3007 if (TYPE_UNNAMED_P (t) && TYPE_NAMESPACE_SCOPE_P (t))
3008 return t;
3010 for (r = CP_TYPE_CONTEXT (t); ; )
3012 /* If we're a nested type of a !TREE_PUBLIC class, we might not
3013 have linkage, or we might just be in an anonymous namespace.
3014 If we're in a TREE_PUBLIC class, we have linkage. */
3015 if (TYPE_P (r) && !TREE_PUBLIC (TYPE_NAME (r)))
3016 return no_linkage_check (TYPE_CONTEXT (t), relaxed_p);
3017 else if (TREE_CODE (r) == FUNCTION_DECL)
3019 if (!relaxed_p || !vague_linkage_p (r))
3020 return t;
3021 else
3022 r = CP_DECL_CONTEXT (r);
3024 else
3025 break;
3028 return NULL_TREE;
3030 case ARRAY_TYPE:
3031 case POINTER_TYPE:
3032 case REFERENCE_TYPE:
3033 case VECTOR_TYPE:
3034 return no_linkage_check (TREE_TYPE (t), relaxed_p);
3036 case OFFSET_TYPE:
3037 ptrmem:
3038 r = no_linkage_check (TYPE_PTRMEM_POINTED_TO_TYPE (t),
3039 relaxed_p);
3040 if (r)
3041 return r;
3042 return no_linkage_check (TYPE_PTRMEM_CLASS_TYPE (t), relaxed_p);
3044 case METHOD_TYPE:
3045 case FUNCTION_TYPE:
3047 tree parm = TYPE_ARG_TYPES (t);
3048 if (TREE_CODE (t) == METHOD_TYPE)
3049 /* The 'this' pointer isn't interesting; a method has the same
3050 linkage (or lack thereof) as its enclosing class. */
3051 parm = TREE_CHAIN (parm);
3052 for (;
3053 parm && parm != void_list_node;
3054 parm = TREE_CHAIN (parm))
3056 r = no_linkage_check (TREE_VALUE (parm), relaxed_p);
3057 if (r)
3058 return r;
3060 return no_linkage_check (TREE_TYPE (t), relaxed_p);
3063 default:
3064 return NULL_TREE;
3068 extern int depth_reached;
3070 void
3071 cxx_print_statistics (void)
3073 print_template_statistics ();
3074 if (GATHER_STATISTICS)
3075 fprintf (stderr, "maximum template instantiation depth reached: %d\n",
3076 depth_reached);
3079 /* Return, as an INTEGER_CST node, the number of elements for TYPE
3080 (which is an ARRAY_TYPE). This counts only elements of the top
3081 array. */
3083 tree
3084 array_type_nelts_top (tree type)
3086 return fold_build2_loc (input_location,
3087 PLUS_EXPR, sizetype,
3088 array_type_nelts (type),
3089 size_one_node);
3092 /* Return, as an INTEGER_CST node, the number of elements for TYPE
3093 (which is an ARRAY_TYPE). This one is a recursive count of all
3094 ARRAY_TYPEs that are clumped together. */
3096 tree
3097 array_type_nelts_total (tree type)
3099 tree sz = array_type_nelts_top (type);
3100 type = TREE_TYPE (type);
3101 while (TREE_CODE (type) == ARRAY_TYPE)
3103 tree n = array_type_nelts_top (type);
3104 sz = fold_build2_loc (input_location,
3105 MULT_EXPR, sizetype, sz, n);
3106 type = TREE_TYPE (type);
3108 return sz;
3111 struct bot_data
3113 splay_tree target_remap;
3114 bool clear_location;
3117 /* Called from break_out_target_exprs via mapcar. */
3119 static tree
3120 bot_manip (tree* tp, int* walk_subtrees, void* data_)
3122 bot_data &data = *(bot_data*)data_;
3123 splay_tree target_remap = data.target_remap;
3124 tree t = *tp;
3126 if (!TYPE_P (t) && TREE_CONSTANT (t) && !TREE_SIDE_EFFECTS (t))
3128 /* There can't be any TARGET_EXPRs or their slot variables below this
3129 point. But we must make a copy, in case subsequent processing
3130 alters any part of it. For example, during gimplification a cast
3131 of the form (T) &X::f (where "f" is a member function) will lead
3132 to replacing the PTRMEM_CST for &X::f with a VAR_DECL. */
3133 *walk_subtrees = 0;
3134 *tp = unshare_expr (t);
3135 return NULL_TREE;
3137 if (TREE_CODE (t) == TARGET_EXPR)
3139 tree u;
3141 if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
3143 u = build_cplus_new (TREE_TYPE (t), TREE_OPERAND (t, 1),
3144 tf_warning_or_error);
3145 if (u == error_mark_node)
3146 return u;
3147 if (AGGR_INIT_ZERO_FIRST (TREE_OPERAND (t, 1)))
3148 AGGR_INIT_ZERO_FIRST (TREE_OPERAND (u, 1)) = true;
3150 else
3151 u = force_target_expr (TREE_TYPE (t), TREE_OPERAND (t, 1),
3152 tf_warning_or_error);
3154 TARGET_EXPR_IMPLICIT_P (u) = TARGET_EXPR_IMPLICIT_P (t);
3155 TARGET_EXPR_LIST_INIT_P (u) = TARGET_EXPR_LIST_INIT_P (t);
3156 TARGET_EXPR_DIRECT_INIT_P (u) = TARGET_EXPR_DIRECT_INIT_P (t);
3157 TARGET_EXPR_ELIDING_P (u) = TARGET_EXPR_ELIDING_P (t);
3159 /* Map the old variable to the new one. */
3160 splay_tree_insert (target_remap,
3161 (splay_tree_key) TREE_OPERAND (t, 0),
3162 (splay_tree_value) TREE_OPERAND (u, 0));
3164 TREE_OPERAND (u, 1) = break_out_target_exprs (TREE_OPERAND (u, 1),
3165 data.clear_location);
3166 if (TREE_OPERAND (u, 1) == error_mark_node)
3167 return error_mark_node;
3169 /* Replace the old expression with the new version. */
3170 *tp = u;
3171 /* We don't have to go below this point; the recursive call to
3172 break_out_target_exprs will have handled anything below this
3173 point. */
3174 *walk_subtrees = 0;
3175 return NULL_TREE;
3177 if (TREE_CODE (*tp) == SAVE_EXPR)
3179 t = *tp;
3180 splay_tree_node n = splay_tree_lookup (target_remap,
3181 (splay_tree_key) t);
3182 if (n)
3184 *tp = (tree)n->value;
3185 *walk_subtrees = 0;
3187 else
3189 copy_tree_r (tp, walk_subtrees, NULL);
3190 splay_tree_insert (target_remap,
3191 (splay_tree_key)t,
3192 (splay_tree_value)*tp);
3193 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
3194 splay_tree_insert (target_remap,
3195 (splay_tree_key)*tp,
3196 (splay_tree_value)*tp);
3198 return NULL_TREE;
3200 if (TREE_CODE (*tp) == DECL_EXPR
3201 && VAR_P (DECL_EXPR_DECL (*tp))
3202 && DECL_ARTIFICIAL (DECL_EXPR_DECL (*tp))
3203 && !TREE_STATIC (DECL_EXPR_DECL (*tp)))
3205 tree t;
3206 splay_tree_node n
3207 = splay_tree_lookup (target_remap,
3208 (splay_tree_key) DECL_EXPR_DECL (*tp));
3209 if (n)
3210 t = (tree) n->value;
3211 else
3213 t = create_temporary_var (TREE_TYPE (DECL_EXPR_DECL (*tp)));
3214 DECL_INITIAL (t) = DECL_INITIAL (DECL_EXPR_DECL (*tp));
3215 splay_tree_insert (target_remap,
3216 (splay_tree_key) DECL_EXPR_DECL (*tp),
3217 (splay_tree_value) t);
3219 copy_tree_r (tp, walk_subtrees, NULL);
3220 DECL_EXPR_DECL (*tp) = t;
3221 if (data.clear_location && EXPR_HAS_LOCATION (*tp))
3222 SET_EXPR_LOCATION (*tp, input_location);
3223 return NULL_TREE;
3225 if (TREE_CODE (*tp) == BIND_EXPR && BIND_EXPR_VARS (*tp))
3227 copy_tree_r (tp, walk_subtrees, NULL);
3228 for (tree *p = &BIND_EXPR_VARS (*tp); *p; p = &DECL_CHAIN (*p))
3230 gcc_assert (VAR_P (*p) && DECL_ARTIFICIAL (*p) && !TREE_STATIC (*p));
3231 tree t = create_temporary_var (TREE_TYPE (*p));
3232 DECL_INITIAL (t) = DECL_INITIAL (*p);
3233 DECL_CHAIN (t) = DECL_CHAIN (*p);
3234 splay_tree_insert (target_remap, (splay_tree_key) *p,
3235 (splay_tree_value) t);
3236 *p = t;
3238 if (data.clear_location && EXPR_HAS_LOCATION (*tp))
3239 SET_EXPR_LOCATION (*tp, input_location);
3240 return NULL_TREE;
3243 /* Make a copy of this node. */
3244 t = copy_tree_r (tp, walk_subtrees, NULL);
3245 if (TREE_CODE (*tp) == CALL_EXPR || TREE_CODE (*tp) == AGGR_INIT_EXPR)
3246 if (!processing_template_decl)
3247 set_flags_from_callee (*tp);
3248 if (data.clear_location && EXPR_HAS_LOCATION (*tp))
3249 SET_EXPR_LOCATION (*tp, input_location);
3250 return t;
3253 /* Replace all remapped VAR_DECLs in T with their new equivalents.
3254 DATA is really a splay-tree mapping old variables to new
3255 variables. */
3257 static tree
3258 bot_replace (tree* t, int* walk_subtrees, void* data_)
3260 bot_data &data = *(bot_data*)data_;
3261 splay_tree target_remap = data.target_remap;
3263 if (VAR_P (*t))
3265 splay_tree_node n = splay_tree_lookup (target_remap,
3266 (splay_tree_key) *t);
3267 if (n)
3268 *t = (tree) n->value;
3270 else if (TREE_CODE (*t) == PARM_DECL
3271 && DECL_NAME (*t) == this_identifier
3272 && !DECL_CONTEXT (*t))
3274 /* In an NSDMI we need to replace the 'this' parameter we used for
3275 parsing with the real one for this function. */
3276 *t = current_class_ptr;
3278 else if (TREE_CODE (*t) == CONVERT_EXPR
3279 && CONVERT_EXPR_VBASE_PATH (*t))
3281 /* In an NSDMI build_base_path defers building conversions to morally
3282 virtual bases, and we handle it here. */
3283 tree basetype = TREE_TYPE (*t);
3284 *t = convert_to_base (TREE_OPERAND (*t, 0), basetype,
3285 /*check_access=*/false, /*nonnull=*/true,
3286 tf_warning_or_error);
3288 else if (cxx_dialect >= cxx20
3289 && (TREE_CODE (*t) == CALL_EXPR
3290 || TREE_CODE (*t) == AGGR_INIT_EXPR)
3291 && !in_immediate_context ())
3293 /* Expand immediate invocations. */
3294 if (tree fndecl = cp_get_callee_fndecl_nofold (*t))
3295 if (DECL_IMMEDIATE_FUNCTION_P (fndecl))
3297 /* Make in_immediate_context true within the args. */
3298 in_consteval_if_p_temp_override ito;
3299 in_consteval_if_p = true;
3300 int nargs = call_expr_nargs (*t);
3301 for (int i = 0; i < nargs; ++i)
3302 cp_walk_tree (&get_nth_callarg (*t, i), bot_replace, data_, NULL);
3303 *t = cxx_constant_value (*t);
3304 if (*t == error_mark_node)
3305 return error_mark_node;
3306 *walk_subtrees = 0;
3310 return NULL_TREE;
3313 /* When we parse a default argument expression, we may create
3314 temporary variables via TARGET_EXPRs. When we actually use the
3315 default-argument expression, we make a copy of the expression
3316 and replace the temporaries with appropriate local versions.
3318 If CLEAR_LOCATION is true, override any EXPR_LOCATION with
3319 input_location. */
3321 tree
3322 break_out_target_exprs (tree t, bool clear_location /* = false */)
3324 static int target_remap_count;
3325 static splay_tree target_remap;
3327 /* We shouldn't be called on templated trees, nor do we want to
3328 produce them. */
3329 gcc_checking_assert (!processing_template_decl);
3331 if (!target_remap_count++)
3332 target_remap = splay_tree_new (splay_tree_compare_pointers,
3333 /*splay_tree_delete_key_fn=*/NULL,
3334 /*splay_tree_delete_value_fn=*/NULL);
3335 bot_data data = { target_remap, clear_location };
3336 if (cp_walk_tree (&t, bot_manip, &data, NULL) == error_mark_node)
3337 t = error_mark_node;
3338 if (cp_walk_tree (&t, bot_replace, &data, NULL) == error_mark_node)
3339 t = error_mark_node;
3341 if (!--target_remap_count)
3343 splay_tree_delete (target_remap);
3344 target_remap = NULL;
3347 return t;
3350 /* Build an expression for the subobject of OBJ at CONSTRUCTOR index INDEX,
3351 which we expect to have type TYPE. */
3353 tree
3354 build_ctor_subob_ref (tree index, tree type, tree obj)
3356 if (index == NULL_TREE)
3357 /* Can't refer to a particular member of a vector. */
3358 obj = NULL_TREE;
3359 else if (TREE_CODE (index) == INTEGER_CST)
3360 obj = cp_build_array_ref (input_location, obj, index, tf_none);
3361 else
3362 obj = build_class_member_access_expr (obj, index, NULL_TREE,
3363 /*reference*/false, tf_none);
3364 if (obj)
3366 tree objtype = TREE_TYPE (obj);
3367 if (TREE_CODE (objtype) == ARRAY_TYPE && !TYPE_DOMAIN (objtype))
3369 /* When the destination object refers to a flexible array member
3370 verify that it matches the type of the source object except
3371 for its domain and qualifiers. */
3372 gcc_assert (comptypes (TYPE_MAIN_VARIANT (type),
3373 TYPE_MAIN_VARIANT (objtype),
3374 COMPARE_REDECLARATION));
3376 else
3377 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, objtype));
3380 return obj;
3383 struct replace_placeholders_t
3385 tree obj; /* The object to be substituted for a PLACEHOLDER_EXPR. */
3386 tree exp; /* The outermost exp. */
3387 bool seen; /* Whether we've encountered a PLACEHOLDER_EXPR. */
3388 hash_set<tree> *pset; /* To avoid walking same trees multiple times. */
3391 /* Like substitute_placeholder_in_expr, but handle C++ tree codes and
3392 build up subexpressions as we go deeper. */
3394 static tree
3395 replace_placeholders_r (tree* t, int* walk_subtrees, void* data_)
3397 replace_placeholders_t *d = static_cast<replace_placeholders_t*>(data_);
3398 tree obj = d->obj;
3400 if (TYPE_P (*t) || TREE_CONSTANT (*t))
3402 *walk_subtrees = false;
3403 return NULL_TREE;
3406 switch (TREE_CODE (*t))
3408 case PLACEHOLDER_EXPR:
3410 tree x = obj;
3411 for (; !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (*t),
3412 TREE_TYPE (x));
3413 x = TREE_OPERAND (x, 0))
3414 gcc_assert (handled_component_p (x));
3415 *t = unshare_expr (x);
3416 *walk_subtrees = false;
3417 d->seen = true;
3419 break;
3421 case CONSTRUCTOR:
3423 constructor_elt *ce;
3424 vec<constructor_elt,va_gc> *v = CONSTRUCTOR_ELTS (*t);
3425 /* Don't walk into CONSTRUCTOR_PLACEHOLDER_BOUNDARY ctors
3426 other than the d->exp one, those have PLACEHOLDER_EXPRs
3427 related to another object. */
3428 if ((CONSTRUCTOR_PLACEHOLDER_BOUNDARY (*t)
3429 && *t != d->exp)
3430 || d->pset->add (*t))
3432 *walk_subtrees = false;
3433 return NULL_TREE;
3435 for (unsigned i = 0; vec_safe_iterate (v, i, &ce); ++i)
3437 tree *valp = &ce->value;
3438 tree type = TREE_TYPE (*valp);
3439 tree subob = obj;
3441 /* Elements with RANGE_EXPR index shouldn't have any
3442 placeholders in them. */
3443 if (ce->index && TREE_CODE (ce->index) == RANGE_EXPR)
3444 continue;
3446 if (TREE_CODE (*valp) == CONSTRUCTOR
3447 && AGGREGATE_TYPE_P (type))
3449 /* If we're looking at the initializer for OBJ, then build
3450 a sub-object reference. If we're looking at an
3451 initializer for another object, just pass OBJ down. */
3452 if (same_type_ignoring_top_level_qualifiers_p
3453 (TREE_TYPE (*t), TREE_TYPE (obj)))
3454 subob = build_ctor_subob_ref (ce->index, type, obj);
3455 if (TREE_CODE (*valp) == TARGET_EXPR)
3456 valp = &TARGET_EXPR_INITIAL (*valp);
3458 d->obj = subob;
3459 cp_walk_tree (valp, replace_placeholders_r, data_, NULL);
3460 d->obj = obj;
3462 *walk_subtrees = false;
3463 break;
3466 default:
3467 if (d->pset->add (*t))
3468 *walk_subtrees = false;
3469 break;
3472 return NULL_TREE;
3475 /* Replace PLACEHOLDER_EXPRs in EXP with object OBJ. SEEN_P is set if
3476 a PLACEHOLDER_EXPR has been encountered. */
3478 tree
3479 replace_placeholders (tree exp, tree obj, bool *seen_p /*= NULL*/)
3481 /* This is only relevant for C++14. */
3482 if (cxx_dialect < cxx14)
3483 return exp;
3485 /* If the object isn't a (member of a) class, do nothing. */
3486 tree op0 = obj;
3487 while (handled_component_p (op0))
3488 op0 = TREE_OPERAND (op0, 0);
3489 if (!CLASS_TYPE_P (strip_array_types (TREE_TYPE (op0))))
3490 return exp;
3492 tree *tp = &exp;
3493 if (TREE_CODE (exp) == TARGET_EXPR)
3494 tp = &TARGET_EXPR_INITIAL (exp);
3495 hash_set<tree> pset;
3496 replace_placeholders_t data = { obj, *tp, false, &pset };
3497 cp_walk_tree (tp, replace_placeholders_r, &data, NULL);
3498 if (seen_p)
3499 *seen_p = data.seen;
3500 return exp;
3503 /* Callback function for find_placeholders. */
3505 static tree
3506 find_placeholders_r (tree *t, int *walk_subtrees, void *)
3508 if (TYPE_P (*t) || TREE_CONSTANT (*t))
3510 *walk_subtrees = false;
3511 return NULL_TREE;
3514 switch (TREE_CODE (*t))
3516 case PLACEHOLDER_EXPR:
3517 return *t;
3519 case CONSTRUCTOR:
3520 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (*t))
3521 *walk_subtrees = false;
3522 break;
3524 default:
3525 break;
3528 return NULL_TREE;
3531 /* Return true if EXP contains a PLACEHOLDER_EXPR. Don't walk into
3532 ctors with CONSTRUCTOR_PLACEHOLDER_BOUNDARY flag set. */
3534 bool
3535 find_placeholders (tree exp)
3537 /* This is only relevant for C++14. */
3538 if (cxx_dialect < cxx14)
3539 return false;
3541 return cp_walk_tree_without_duplicates (&exp, find_placeholders_r, NULL);
3544 /* Similar to `build_nt', but for template definitions of dependent
3545 expressions */
3547 tree
3548 build_min_nt_loc (location_t loc, enum tree_code code, ...)
3550 tree t;
3551 int length;
3552 int i;
3553 va_list p;
3555 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3557 va_start (p, code);
3559 t = make_node (code);
3560 SET_EXPR_LOCATION (t, loc);
3561 length = TREE_CODE_LENGTH (code);
3563 for (i = 0; i < length; i++)
3564 TREE_OPERAND (t, i) = va_arg (p, tree);
3566 va_end (p);
3567 return t;
3570 /* Similar to `build', but for template definitions. */
3572 tree
3573 build_min (enum tree_code code, tree tt, ...)
3575 tree t;
3576 int length;
3577 int i;
3578 va_list p;
3580 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3582 va_start (p, tt);
3584 t = make_node (code);
3585 length = TREE_CODE_LENGTH (code);
3586 TREE_TYPE (t) = tt;
3588 for (i = 0; i < length; i++)
3590 tree x = va_arg (p, tree);
3591 TREE_OPERAND (t, i) = x;
3592 if (x && !TYPE_P (x) && TREE_SIDE_EFFECTS (x))
3593 TREE_SIDE_EFFECTS (t) = 1;
3596 va_end (p);
3598 return t;
3601 /* Similar to `build', but for template definitions of non-dependent
3602 expressions. NON_DEP is the non-dependent expression that has been
3603 built. */
3605 tree
3606 build_min_non_dep (enum tree_code code, tree non_dep, ...)
3608 tree t;
3609 int length;
3610 int i;
3611 va_list p;
3613 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3615 va_start (p, non_dep);
3617 if (REFERENCE_REF_P (non_dep))
3618 non_dep = TREE_OPERAND (non_dep, 0);
3620 t = make_node (code);
3621 SET_EXPR_LOCATION (t, cp_expr_loc_or_input_loc (non_dep));
3622 length = TREE_CODE_LENGTH (code);
3623 TREE_TYPE (t) = unlowered_expr_type (non_dep);
3624 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
3626 for (i = 0; i < length; i++)
3627 TREE_OPERAND (t, i) = va_arg (p, tree);
3629 va_end (p);
3630 return convert_from_reference (t);
3633 /* Similar to build_min_nt, but call expressions */
3635 tree
3636 build_min_nt_call_vec (tree fn, vec<tree, va_gc> *args)
3638 tree ret, t;
3639 unsigned int ix;
3641 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
3642 CALL_EXPR_FN (ret) = fn;
3643 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
3644 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
3645 CALL_EXPR_ARG (ret, ix) = t;
3647 return ret;
3650 /* Similar to `build_min_nt_call_vec', but for template definitions of
3651 non-dependent expressions. NON_DEP is the non-dependent expression
3652 that has been built. */
3654 tree
3655 build_min_non_dep_call_vec (tree non_dep, tree fn, vec<tree, va_gc> *argvec)
3657 tree t = build_min_nt_call_vec (fn, argvec);
3658 if (REFERENCE_REF_P (non_dep))
3659 non_dep = TREE_OPERAND (non_dep, 0);
3660 TREE_TYPE (t) = TREE_TYPE (non_dep);
3661 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
3662 return convert_from_reference (t);
3665 /* Similar to build_min_non_dep, but for expressions that have been resolved to
3666 a call to an operator overload. OP is the operator that has been
3667 overloaded. NON_DEP is the non-dependent expression that's been built,
3668 which should be a CALL_EXPR or an INDIRECT_REF to a CALL_EXPR. OVERLOAD is
3669 the overload that NON_DEP is calling. */
3671 tree
3672 build_min_non_dep_op_overload (enum tree_code op,
3673 tree non_dep,
3674 tree overload, ...)
3676 va_list p;
3677 int nargs, expected_nargs;
3678 tree fn, call, obj = NULL_TREE;
3680 non_dep = extract_call_expr (non_dep);
3682 nargs = call_expr_nargs (non_dep);
3684 expected_nargs = cp_tree_code_length (op);
3685 if (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE
3686 /* For ARRAY_REF, operator[] is either a non-static member or newly
3687 static member, never out of class and for the static member case
3688 if user uses single index the operator[] needs to have a single
3689 argument as well, but the function is called with 2 - the object
3690 it is invoked on and the index. */
3691 || op == ARRAY_REF)
3692 expected_nargs -= 1;
3693 if ((op == POSTINCREMENT_EXPR
3694 || op == POSTDECREMENT_EXPR)
3695 /* With -fpermissive non_dep could be operator++(). */
3696 && (!flag_permissive || nargs != expected_nargs))
3697 expected_nargs += 1;
3698 gcc_assert (nargs == expected_nargs);
3700 releasing_vec args;
3701 va_start (p, overload);
3703 if (TREE_CODE (TREE_TYPE (overload)) == FUNCTION_TYPE)
3705 fn = overload;
3706 if (op == ARRAY_REF)
3707 obj = va_arg (p, tree);
3708 for (int i = 0; i < nargs; i++)
3710 tree arg = va_arg (p, tree);
3711 vec_safe_push (args, arg);
3714 else if (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE)
3716 tree object = va_arg (p, tree);
3717 tree binfo = TYPE_BINFO (TREE_TYPE (object));
3718 tree method = build_baselink (binfo, binfo, overload, NULL_TREE);
3719 fn = build_min (COMPONENT_REF, TREE_TYPE (overload),
3720 object, method, NULL_TREE);
3721 for (int i = 0; i < nargs; i++)
3723 tree arg = va_arg (p, tree);
3724 vec_safe_push (args, arg);
3727 else
3728 gcc_unreachable ();
3730 va_end (p);
3731 call = build_min_non_dep_call_vec (non_dep, fn, args);
3733 tree call_expr = extract_call_expr (call);
3734 KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep);
3735 CALL_EXPR_OPERATOR_SYNTAX (call_expr) = true;
3736 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep);
3737 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep);
3739 if (obj)
3740 return keep_unused_object_arg (call, obj, overload);
3741 return call;
3744 /* Similar to above build_min_non_dep_op_overload, but arguments
3745 are taken from ARGS vector. */
3747 tree
3748 build_min_non_dep_op_overload (tree non_dep, tree overload, tree object,
3749 vec<tree, va_gc> *args)
3751 non_dep = extract_call_expr (non_dep);
3753 unsigned int nargs = call_expr_nargs (non_dep);
3754 tree fn = overload;
3755 if (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE)
3757 tree binfo = TYPE_BINFO (TREE_TYPE (object));
3758 tree method = build_baselink (binfo, binfo, overload, NULL_TREE);
3759 fn = build_min (COMPONENT_REF, TREE_TYPE (overload),
3760 object, method, NULL_TREE);
3761 object = NULL_TREE;
3763 gcc_assert (vec_safe_length (args) == nargs);
3765 tree call = build_min_non_dep_call_vec (non_dep, fn, args);
3767 tree call_expr = extract_call_expr (call);
3768 KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep);
3769 CALL_EXPR_OPERATOR_SYNTAX (call_expr) = true;
3770 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep);
3771 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep);
3773 if (object)
3774 return keep_unused_object_arg (call, object, overload);
3775 return call;
3778 /* Return a new tree vec copied from VEC, with ELT inserted at index IDX. */
3780 vec<tree, va_gc> *
3781 vec_copy_and_insert (vec<tree, va_gc> *old_vec, tree elt, unsigned idx)
3783 unsigned len = vec_safe_length (old_vec);
3784 gcc_assert (idx <= len);
3786 vec<tree, va_gc> *new_vec = NULL;
3787 vec_alloc (new_vec, len + 1);
3789 unsigned i;
3790 for (i = 0; i < len; ++i)
3792 if (i == idx)
3793 new_vec->quick_push (elt);
3794 new_vec->quick_push ((*old_vec)[i]);
3796 if (i == idx)
3797 new_vec->quick_push (elt);
3799 return new_vec;
3802 tree
3803 get_type_decl (tree t)
3805 if (TREE_CODE (t) == TYPE_DECL)
3806 return t;
3807 if (TYPE_P (t))
3808 return TYPE_STUB_DECL (t);
3809 gcc_assert (t == error_mark_node);
3810 return t;
3813 /* Returns the namespace that contains DECL, whether directly or
3814 indirectly. */
3816 tree
3817 decl_namespace_context (tree decl)
3819 while (1)
3821 if (TREE_CODE (decl) == NAMESPACE_DECL)
3822 return decl;
3823 else if (TYPE_P (decl))
3824 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
3825 else
3826 decl = CP_DECL_CONTEXT (decl);
3830 /* Returns true if decl is within an anonymous namespace, however deeply
3831 nested, or false otherwise. */
3833 bool
3834 decl_anon_ns_mem_p (tree decl)
3836 return !TREE_PUBLIC (decl_namespace_context (decl));
3839 /* Returns true if the enclosing scope of DECL has internal or no linkage. */
3841 bool
3842 decl_internal_context_p (const_tree decl)
3844 while (TREE_CODE (decl) != NAMESPACE_DECL)
3846 /* Classes inside anonymous namespaces have TREE_PUBLIC == 0. */
3847 if (TYPE_P (decl))
3848 return !TREE_PUBLIC (TYPE_MAIN_DECL (decl));
3850 decl = CP_DECL_CONTEXT (decl);
3852 return !TREE_PUBLIC (decl);
3855 /* Subroutine of cp_tree_equal: t1 and t2 are two CALL_EXPRs.
3856 Return whether their CALL_EXPR_FNs are equivalent. */
3858 static bool
3859 called_fns_equal (tree t1, tree t2)
3861 /* Core 1321: dependent names are equivalent even if the overload sets
3862 are different. But do compare explicit template arguments. */
3863 tree name1 = call_expr_dependent_name (t1);
3864 tree name2 = call_expr_dependent_name (t2);
3865 t1 = CALL_EXPR_FN (t1);
3866 t2 = CALL_EXPR_FN (t2);
3867 if (name1 || name2)
3869 tree targs1 = NULL_TREE, targs2 = NULL_TREE;
3871 if (name1 != name2)
3872 return false;
3874 /* FIXME dependent_name currently returns an unqualified name regardless
3875 of whether the function was named with a qualified- or unqualified-id.
3876 Until that's fixed, check that we aren't looking at overload sets from
3877 different scopes. */
3878 if (is_overloaded_fn (t1) && is_overloaded_fn (t2)
3879 && (DECL_CONTEXT (get_first_fn (t1))
3880 != DECL_CONTEXT (get_first_fn (t2))))
3881 return false;
3883 if (TREE_CODE (t1) == TEMPLATE_ID_EXPR)
3884 targs1 = TREE_OPERAND (t1, 1);
3885 if (TREE_CODE (t2) == TEMPLATE_ID_EXPR)
3886 targs2 = TREE_OPERAND (t2, 1);
3887 return cp_tree_equal (targs1, targs2);
3889 else
3890 return cp_tree_equal (t1, t2);
3893 bool comparing_override_contracts;
3895 /* In a component reference, return the innermost object of
3896 the postfix-expression. */
3898 static tree
3899 get_innermost_component (tree t)
3901 gcc_assert (TREE_CODE (t) == COMPONENT_REF);
3902 while (TREE_CODE (t) == COMPONENT_REF)
3903 t = TREE_OPERAND (t, 0);
3904 return t;
3907 /* Returns true if T is a possibly converted 'this' or '*this' expression. */
3909 static bool
3910 is_this_expression (tree t)
3912 t = get_innermost_component (t);
3913 /* See through deferences and no-op conversions. */
3914 if (TREE_CODE (t) == INDIRECT_REF)
3915 t = TREE_OPERAND (t, 0);
3916 if (TREE_CODE (t) == NOP_EXPR)
3917 t = TREE_OPERAND (t, 0);
3918 return is_this_parameter (t);
3921 static bool
3922 comparing_this_references (tree t1, tree t2)
3924 return is_this_expression (t1) && is_this_expression (t2);
3927 static bool
3928 equivalent_member_references (tree t1, tree t2)
3930 if (!comparing_this_references (t1, t2))
3931 return false;
3932 t1 = TREE_OPERAND (t1, 1);
3933 t2 = TREE_OPERAND (t2, 1);
3934 return t1 == t2;
3937 /* Return truthvalue of whether T1 is the same tree structure as T2.
3938 Return 1 if they are the same. Return 0 if they are different. */
3940 bool
3941 cp_tree_equal (tree t1, tree t2)
3943 enum tree_code code1, code2;
3945 if (t1 == t2)
3946 return true;
3947 if (!t1 || !t2)
3948 return false;
3950 code1 = TREE_CODE (t1);
3951 code2 = TREE_CODE (t2);
3953 if (code1 != code2)
3954 return false;
3956 if (CONSTANT_CLASS_P (t1)
3957 && !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3958 return false;
3960 switch (code1)
3962 case VOID_CST:
3963 /* There's only a single VOID_CST node, so we should never reach
3964 here. */
3965 gcc_unreachable ();
3967 case INTEGER_CST:
3968 return tree_int_cst_equal (t1, t2);
3970 case REAL_CST:
3971 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
3973 case STRING_CST:
3974 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3975 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3976 TREE_STRING_LENGTH (t1));
3978 case FIXED_CST:
3979 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
3980 TREE_FIXED_CST (t2));
3982 case COMPLEX_CST:
3983 return cp_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
3984 && cp_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
3986 case VECTOR_CST:
3987 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
3989 case CONSTRUCTOR:
3990 /* We need to do this when determining whether or not two
3991 non-type pointer to member function template arguments
3992 are the same. */
3993 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
3994 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
3995 return false;
3997 tree field, value;
3998 unsigned int i;
3999 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
4001 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
4002 if (!cp_tree_equal (field, elt2->index)
4003 || !cp_tree_equal (value, elt2->value))
4004 return false;
4007 return true;
4009 case TREE_LIST:
4010 if (!cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
4011 return false;
4012 if (!cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
4013 return false;
4014 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
4016 case SAVE_EXPR:
4017 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
4019 case CALL_EXPR:
4021 if (KOENIG_LOOKUP_P (t1) != KOENIG_LOOKUP_P (t2))
4022 return false;
4024 if (!called_fns_equal (t1, t2))
4025 return false;
4027 call_expr_arg_iterator iter1, iter2;
4028 init_call_expr_arg_iterator (t1, &iter1);
4029 init_call_expr_arg_iterator (t2, &iter2);
4030 if (iter1.n != iter2.n)
4031 return false;
4033 while (more_call_expr_args_p (&iter1))
4035 tree arg1 = next_call_expr_arg (&iter1);
4036 tree arg2 = next_call_expr_arg (&iter2);
4038 gcc_checking_assert (arg1 && arg2);
4039 if (!cp_tree_equal (arg1, arg2))
4040 return false;
4043 return true;
4046 case TARGET_EXPR:
4048 tree o1 = TREE_OPERAND (t1, 0);
4049 tree o2 = TREE_OPERAND (t2, 0);
4051 /* Special case: if either target is an unallocated VAR_DECL,
4052 it means that it's going to be unified with whatever the
4053 TARGET_EXPR is really supposed to initialize, so treat it
4054 as being equivalent to anything. */
4055 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
4056 && !DECL_RTL_SET_P (o1))
4057 /*Nop*/;
4058 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
4059 && !DECL_RTL_SET_P (o2))
4060 /*Nop*/;
4061 else if (!cp_tree_equal (o1, o2))
4062 return false;
4064 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
4067 case PARM_DECL:
4068 /* For comparing uses of parameters in late-specified return types
4069 with an out-of-class definition of the function, but can also come
4070 up for expressions that involve 'this' in a member function
4071 template. */
4073 if (comparing_specializations
4074 && DECL_CONTEXT (t1) != DECL_CONTEXT (t2))
4075 /* When comparing hash table entries, only an exact match is
4076 good enough; we don't want to replace 'this' with the
4077 version from another function. But be more flexible
4078 with parameters with identical contexts. */
4079 return false;
4081 if (same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
4083 if (DECL_ARTIFICIAL (t1) ^ DECL_ARTIFICIAL (t2))
4084 return false;
4085 if (CONSTRAINT_VAR_P (t1) ^ CONSTRAINT_VAR_P (t2))
4086 return false;
4087 if (DECL_ARTIFICIAL (t1)
4088 || (DECL_PARM_LEVEL (t1) == DECL_PARM_LEVEL (t2)
4089 && DECL_PARM_INDEX (t1) == DECL_PARM_INDEX (t2)))
4090 return true;
4092 return false;
4094 case VAR_DECL:
4095 case CONST_DECL:
4096 case FIELD_DECL:
4097 case FUNCTION_DECL:
4098 case TEMPLATE_DECL:
4099 case IDENTIFIER_NODE:
4100 case SSA_NAME:
4101 case USING_DECL:
4102 case DEFERRED_PARSE:
4103 return false;
4105 case BASELINK:
4106 return (BASELINK_BINFO (t1) == BASELINK_BINFO (t2)
4107 && BASELINK_ACCESS_BINFO (t1) == BASELINK_ACCESS_BINFO (t2)
4108 && BASELINK_QUALIFIED_P (t1) == BASELINK_QUALIFIED_P (t2)
4109 && cp_tree_equal (BASELINK_FUNCTIONS (t1),
4110 BASELINK_FUNCTIONS (t2)));
4112 case TEMPLATE_PARM_INDEX:
4113 return (TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
4114 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2)
4115 && (TEMPLATE_PARM_PARAMETER_PACK (t1)
4116 == TEMPLATE_PARM_PARAMETER_PACK (t2))
4117 && same_type_p (TREE_TYPE (TEMPLATE_PARM_DECL (t1)),
4118 TREE_TYPE (TEMPLATE_PARM_DECL (t2))));
4120 case TEMPLATE_ID_EXPR:
4121 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
4122 return false;
4123 if (!comp_template_args (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)))
4124 return false;
4125 return true;
4127 case CONSTRAINT_INFO:
4128 return cp_tree_equal (CI_ASSOCIATED_CONSTRAINTS (t1),
4129 CI_ASSOCIATED_CONSTRAINTS (t2));
4131 case CHECK_CONSTR:
4132 return (CHECK_CONSTR_CONCEPT (t1) == CHECK_CONSTR_CONCEPT (t2)
4133 && comp_template_args (CHECK_CONSTR_ARGS (t1),
4134 CHECK_CONSTR_ARGS (t2)));
4136 case TREE_VEC:
4137 /* These are template args. Really we should be getting the
4138 caller to do this as it knows it to be true. */
4139 if (!comp_template_args (t1, t2, NULL, NULL, false))
4140 return false;
4141 return true;
4143 case SIZEOF_EXPR:
4144 case ALIGNOF_EXPR:
4146 tree o1 = TREE_OPERAND (t1, 0);
4147 tree o2 = TREE_OPERAND (t2, 0);
4149 if (code1 == SIZEOF_EXPR)
4151 if (SIZEOF_EXPR_TYPE_P (t1))
4152 o1 = TREE_TYPE (o1);
4153 if (SIZEOF_EXPR_TYPE_P (t2))
4154 o2 = TREE_TYPE (o2);
4156 else if (ALIGNOF_EXPR_STD_P (t1) != ALIGNOF_EXPR_STD_P (t2))
4157 return false;
4159 if (TREE_CODE (o1) != TREE_CODE (o2))
4160 return false;
4162 if (ARGUMENT_PACK_P (o1))
4163 return template_args_equal (o1, o2);
4164 else if (TYPE_P (o1))
4165 return same_type_p (o1, o2);
4166 else
4167 return cp_tree_equal (o1, o2);
4170 case MODOP_EXPR:
4172 tree t1_op1, t2_op1;
4174 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
4175 return false;
4177 t1_op1 = TREE_OPERAND (t1, 1);
4178 t2_op1 = TREE_OPERAND (t2, 1);
4179 if (TREE_CODE (t1_op1) != TREE_CODE (t2_op1))
4180 return false;
4182 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t2, 2));
4185 case PTRMEM_CST:
4186 /* Two pointer-to-members are the same if they point to the same
4187 field or function in the same class. */
4188 if (PTRMEM_CST_MEMBER (t1) != PTRMEM_CST_MEMBER (t2))
4189 return false;
4191 return same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2));
4193 case OVERLOAD:
4195 /* Two overloads. Must be exactly the same set of decls. */
4196 lkp_iterator first (t1);
4197 lkp_iterator second (t2);
4199 for (; first && second; ++first, ++second)
4200 if (*first != *second)
4201 return false;
4202 return !(first || second);
4205 case TRAIT_EXPR:
4206 if (TRAIT_EXPR_KIND (t1) != TRAIT_EXPR_KIND (t2))
4207 return false;
4208 return cp_tree_equal (TRAIT_EXPR_TYPE1 (t1), TRAIT_EXPR_TYPE1 (t2))
4209 && cp_tree_equal (TRAIT_EXPR_TYPE2 (t1), TRAIT_EXPR_TYPE2 (t2));
4211 case NON_LVALUE_EXPR:
4212 case VIEW_CONVERT_EXPR:
4213 /* Used for location wrappers with possibly NULL types. */
4214 if (!TREE_TYPE (t1) || !TREE_TYPE (t2))
4216 if (TREE_TYPE (t1) || TREE_TYPE (t2))
4217 return false;
4218 break;
4220 /* FALLTHROUGH */
4222 case CAST_EXPR:
4223 case STATIC_CAST_EXPR:
4224 case REINTERPRET_CAST_EXPR:
4225 case CONST_CAST_EXPR:
4226 case DYNAMIC_CAST_EXPR:
4227 case IMPLICIT_CONV_EXPR:
4228 case NEW_EXPR:
4229 case BIT_CAST_EXPR:
4230 CASE_CONVERT:
4231 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
4232 return false;
4233 /* Now compare operands as usual. */
4234 break;
4236 case DEFERRED_NOEXCEPT:
4237 return (cp_tree_equal (DEFERRED_NOEXCEPT_PATTERN (t1),
4238 DEFERRED_NOEXCEPT_PATTERN (t2))
4239 && comp_template_args (DEFERRED_NOEXCEPT_ARGS (t1),
4240 DEFERRED_NOEXCEPT_ARGS (t2)));
4242 case LAMBDA_EXPR:
4243 /* Two lambda-expressions are never considered equivalent. */
4244 return false;
4246 case TYPE_ARGUMENT_PACK:
4247 case NONTYPE_ARGUMENT_PACK:
4249 tree p1 = ARGUMENT_PACK_ARGS (t1);
4250 tree p2 = ARGUMENT_PACK_ARGS (t2);
4251 int len = TREE_VEC_LENGTH (p1);
4252 if (TREE_VEC_LENGTH (p2) != len)
4253 return false;
4255 for (int ix = 0; ix != len; ix++)
4256 if (!template_args_equal (TREE_VEC_ELT (p1, ix),
4257 TREE_VEC_ELT (p2, ix)))
4258 return false;
4259 return true;
4262 case EXPR_PACK_EXPANSION:
4263 if (!cp_tree_equal (PACK_EXPANSION_PATTERN (t1),
4264 PACK_EXPANSION_PATTERN (t2)))
4265 return false;
4266 if (!comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
4267 PACK_EXPANSION_EXTRA_ARGS (t2)))
4268 return false;
4269 return true;
4271 case COMPONENT_REF:
4272 /* If we're comparing contract conditions of overrides, member references
4273 compare equal if they designate the same member. */
4274 if (comparing_override_contracts)
4275 return equivalent_member_references (t1, t2);
4276 break;
4278 default:
4279 break;
4282 switch (TREE_CODE_CLASS (code1))
4284 case tcc_unary:
4285 case tcc_binary:
4286 case tcc_comparison:
4287 case tcc_expression:
4288 case tcc_vl_exp:
4289 case tcc_reference:
4290 case tcc_statement:
4292 int n = cp_tree_operand_length (t1);
4293 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
4294 && n != TREE_OPERAND_LENGTH (t2))
4295 return false;
4297 for (int i = 0; i < n; ++i)
4298 if (!cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
4299 return false;
4301 return true;
4304 case tcc_type:
4305 return same_type_p (t1, t2);
4307 default:
4308 gcc_unreachable ();
4311 /* We can get here with --disable-checking. */
4312 return false;
4315 /* The type of ARG when used as an lvalue. */
4317 tree
4318 lvalue_type (tree arg)
4320 tree type = TREE_TYPE (arg);
4321 return type;
4324 /* The type of ARG for printing error messages; denote lvalues with
4325 reference types. */
4327 tree
4328 error_type (tree arg)
4330 tree type = TREE_TYPE (arg);
4332 if (TREE_CODE (type) == ARRAY_TYPE)
4334 else if (TREE_CODE (type) == ERROR_MARK)
4336 else if (lvalue_p (arg))
4337 type = build_reference_type (lvalue_type (arg));
4338 else if (MAYBE_CLASS_TYPE_P (type))
4339 type = lvalue_type (arg);
4341 return type;
4344 /* Does FUNCTION use a variable-length argument list? */
4347 varargs_function_p (const_tree function)
4349 return stdarg_p (TREE_TYPE (function));
4352 /* Returns 1 if decl is a member of a class. */
4355 member_p (const_tree decl)
4357 const_tree const ctx = DECL_CONTEXT (decl);
4358 return (ctx && TYPE_P (ctx));
4361 /* Create a placeholder for member access where we don't actually have an
4362 object that the access is against. For a general declval<T> equivalent,
4363 use build_stub_object instead. */
4365 tree
4366 build_dummy_object (tree type)
4368 tree decl = build1 (CONVERT_EXPR, build_pointer_type (type), void_node);
4369 return cp_build_fold_indirect_ref (decl);
4372 /* We've gotten a reference to a member of TYPE. Return *this if appropriate,
4373 or a dummy object otherwise. If BINFOP is non-0, it is filled with the
4374 binfo path from current_class_type to TYPE, or 0. */
4376 tree
4377 maybe_dummy_object (tree type, tree* binfop)
4379 tree decl, context;
4380 tree binfo;
4381 tree current = current_nonlambda_class_type ();
4383 if (current
4384 && (binfo = lookup_base (current, type, ba_any, NULL,
4385 tf_warning_or_error)))
4386 context = current;
4387 else
4389 /* Reference from a nested class member function. */
4390 context = type;
4391 binfo = TYPE_BINFO (type);
4394 if (binfop)
4395 *binfop = binfo;
4397 /* current_class_ref might not correspond to current_class_type if
4398 we're in tsubst_default_argument or a lambda-declarator; in either
4399 case, we want to use current_class_ref if it matches CONTEXT. */
4400 tree ctype = current_class_ref ? TREE_TYPE (current_class_ref) : NULL_TREE;
4401 if (ctype
4402 && same_type_ignoring_top_level_qualifiers_p (ctype, context))
4403 decl = current_class_ref;
4404 else
4406 /* Return a dummy object whose cv-quals are consistent with (the
4407 non-lambda) 'this' if available. */
4408 if (ctype)
4410 int quals = TYPE_UNQUALIFIED;
4411 if (tree lambda = CLASSTYPE_LAMBDA_EXPR (ctype))
4413 if (tree cap = lambda_expr_this_capture (lambda, false))
4414 quals = cp_type_quals (TREE_TYPE (TREE_TYPE (cap)));
4416 else
4417 quals = cp_type_quals (ctype);
4418 context = cp_build_qualified_type (context, quals);
4420 decl = build_dummy_object (context);
4423 return decl;
4426 /* Returns 1 if OB is a placeholder object, or a pointer to one. */
4428 bool
4429 is_dummy_object (const_tree ob)
4431 if (INDIRECT_REF_P (ob))
4432 ob = TREE_OPERAND (ob, 0);
4433 return (TREE_CODE (ob) == CONVERT_EXPR
4434 && TREE_OPERAND (ob, 0) == void_node);
4437 /* Returns true if TYPE is char, unsigned char, or std::byte. */
4439 bool
4440 is_byte_access_type (tree type)
4442 type = TYPE_MAIN_VARIANT (type);
4443 if (type == char_type_node
4444 || type == unsigned_char_type_node)
4445 return true;
4447 return (TREE_CODE (type) == ENUMERAL_TYPE
4448 && TYPE_CONTEXT (type) == std_node
4449 && !strcmp ("byte", TYPE_NAME_STRING (type)));
4452 /* Returns true if TYPE is unsigned char or std::byte. */
4454 bool
4455 is_byte_access_type_not_plain_char (tree type)
4457 type = TYPE_MAIN_VARIANT (type);
4458 if (type == char_type_node)
4459 return false;
4461 return is_byte_access_type (type);
4464 /* Returns 1 iff type T is something we want to treat as a scalar type for
4465 the purpose of deciding whether it is trivial/POD/standard-layout. */
4467 bool
4468 scalarish_type_p (const_tree t)
4470 if (t == error_mark_node)
4471 return 1;
4473 return (SCALAR_TYPE_P (t) || VECTOR_TYPE_P (t));
4476 /* Returns true iff T requires non-trivial default initialization. */
4478 bool
4479 type_has_nontrivial_default_init (const_tree t)
4481 t = strip_array_types (CONST_CAST_TREE (t));
4483 if (CLASS_TYPE_P (t))
4484 return TYPE_HAS_COMPLEX_DFLT (t);
4485 else
4486 return 0;
4489 /* Track classes with only deleted copy/move constructors so that we can warn
4490 if they are used in call/return by value. */
4492 static GTY(()) hash_set<tree>* deleted_copy_types;
4493 static void
4494 remember_deleted_copy (const_tree t)
4496 if (!deleted_copy_types)
4497 deleted_copy_types = hash_set<tree>::create_ggc(37);
4498 deleted_copy_types->add (CONST_CAST_TREE (t));
4500 void
4501 maybe_warn_parm_abi (tree t, location_t loc)
4503 if (!deleted_copy_types
4504 || !deleted_copy_types->contains (t))
4505 return;
4507 if ((flag_abi_version == 12 || warn_abi_version == 12)
4508 && classtype_has_non_deleted_move_ctor (t))
4510 bool w;
4511 auto_diagnostic_group d;
4512 if (flag_abi_version > 12)
4513 w = warning_at (loc, OPT_Wabi, "%<-fabi-version=13%> (GCC 8.2) fixes "
4514 "the calling convention for %qT, which was "
4515 "accidentally changed in 8.1", t);
4516 else
4517 w = warning_at (loc, OPT_Wabi, "%<-fabi-version=12%> (GCC 8.1) "
4518 "accidentally changes the calling convention for %qT",
4520 if (w)
4521 inform (location_of (t), " declared here");
4522 return;
4525 auto_diagnostic_group d;
4526 if (warning_at (loc, OPT_Wabi, "the calling convention for %qT changes in "
4527 "%<-fabi-version=13%> (GCC 8.2)", t))
4528 inform (location_of (t), " because all of its copy and move "
4529 "constructors are deleted");
4532 /* Returns true iff copying an object of type T (including via move
4533 constructor) is non-trivial. That is, T has no non-trivial copy
4534 constructors and no non-trivial move constructors, and not all copy/move
4535 constructors are deleted. This function implements the ABI notion of
4536 non-trivial copy, which has diverged from the one in the standard. */
4538 bool
4539 type_has_nontrivial_copy_init (const_tree type)
4541 tree t = strip_array_types (CONST_CAST_TREE (type));
4543 if (CLASS_TYPE_P (t))
4545 gcc_assert (COMPLETE_TYPE_P (t));
4547 if (TYPE_HAS_COMPLEX_COPY_CTOR (t)
4548 || TYPE_HAS_COMPLEX_MOVE_CTOR (t))
4549 /* Nontrivial. */
4550 return true;
4552 if (cxx_dialect < cxx11)
4553 /* No deleted functions before C++11. */
4554 return false;
4556 /* Before ABI v12 we did a bitwise copy of types with only deleted
4557 copy/move constructors. */
4558 if (!abi_version_at_least (12)
4559 && !(warn_abi && abi_version_crosses (12)))
4560 return false;
4562 bool saw_copy = false;
4563 bool saw_non_deleted = false;
4564 bool saw_non_deleted_move = false;
4566 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
4567 saw_copy = saw_non_deleted = true;
4568 else if (CLASSTYPE_LAZY_COPY_CTOR (t))
4570 saw_copy = true;
4571 if (classtype_has_move_assign_or_move_ctor_p (t, true))
4572 /* [class.copy]/8 If the class definition declares a move
4573 constructor or move assignment operator, the implicitly declared
4574 copy constructor is defined as deleted.... */;
4575 else
4576 /* Any other reason the implicitly-declared function would be
4577 deleted would also cause TYPE_HAS_COMPLEX_COPY_CTOR to be
4578 set. */
4579 saw_non_deleted = true;
4582 if (!saw_non_deleted)
4583 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4585 tree fn = *iter;
4586 if (copy_fn_p (fn))
4588 saw_copy = true;
4589 if (!DECL_DELETED_FN (fn))
4591 /* Not deleted, therefore trivial. */
4592 saw_non_deleted = true;
4593 break;
4596 else if (move_fn_p (fn))
4597 if (!DECL_DELETED_FN (fn))
4598 saw_non_deleted_move = true;
4601 gcc_assert (saw_copy);
4603 /* ABI v12 buggily ignored move constructors. */
4604 bool v11nontriv = false;
4605 bool v12nontriv = !saw_non_deleted;
4606 bool v13nontriv = !saw_non_deleted && !saw_non_deleted_move;
4607 bool nontriv = (abi_version_at_least (13) ? v13nontriv
4608 : flag_abi_version == 12 ? v12nontriv
4609 : v11nontriv);
4610 bool warn_nontriv = (warn_abi_version >= 13 ? v13nontriv
4611 : warn_abi_version == 12 ? v12nontriv
4612 : v11nontriv);
4613 if (nontriv != warn_nontriv)
4614 remember_deleted_copy (t);
4616 return nontriv;
4618 else
4619 return 0;
4622 /* Returns 1 iff type T is a trivially copyable type, as defined in
4623 [basic.types] and [class]. */
4625 bool
4626 trivially_copyable_p (const_tree t)
4628 t = strip_array_types (CONST_CAST_TREE (t));
4630 if (CLASS_TYPE_P (t))
4631 return ((!TYPE_HAS_COPY_CTOR (t)
4632 || !TYPE_HAS_COMPLEX_COPY_CTOR (t))
4633 && !TYPE_HAS_COMPLEX_MOVE_CTOR (t)
4634 && (!TYPE_HAS_COPY_ASSIGN (t)
4635 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (t))
4636 && !TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
4637 && TYPE_HAS_TRIVIAL_DESTRUCTOR (t));
4638 else
4639 /* CWG 2094 makes volatile-qualified scalars trivially copyable again. */
4640 return scalarish_type_p (t);
4643 /* Returns 1 iff type T is a trivial type, as defined in [basic.types] and
4644 [class]. */
4646 bool
4647 trivial_type_p (const_tree t)
4649 t = strip_array_types (CONST_CAST_TREE (t));
4651 if (CLASS_TYPE_P (t))
4652 return (TYPE_HAS_TRIVIAL_DFLT (t)
4653 && trivially_copyable_p (t));
4654 else
4655 return scalarish_type_p (t);
4658 /* Returns 1 iff type T is a POD type, as defined in [basic.types]. */
4660 bool
4661 pod_type_p (const_tree t)
4663 /* This CONST_CAST is okay because strip_array_types returns its
4664 argument unmodified and we assign it to a const_tree. */
4665 t = strip_array_types (CONST_CAST_TREE(t));
4667 if (!CLASS_TYPE_P (t))
4668 return scalarish_type_p (t);
4669 else if (cxx_dialect > cxx98)
4670 /* [class]/10: A POD struct is a class that is both a trivial class and a
4671 standard-layout class, and has no non-static data members of type
4672 non-POD struct, non-POD union (or array of such types).
4674 We don't need to check individual members because if a member is
4675 non-std-layout or non-trivial, the class will be too. */
4676 return (std_layout_type_p (t) && trivial_type_p (t));
4677 else
4678 /* The C++98 definition of POD is different. */
4679 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
4682 /* Returns true iff T is POD for the purpose of layout, as defined in the
4683 C++ ABI. */
4685 bool
4686 layout_pod_type_p (const_tree t)
4688 t = strip_array_types (CONST_CAST_TREE (t));
4690 if (CLASS_TYPE_P (t))
4691 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
4692 else
4693 return scalarish_type_p (t);
4696 /* Returns true iff T is a standard-layout type, as defined in
4697 [basic.types]. */
4699 bool
4700 std_layout_type_p (const_tree t)
4702 t = strip_array_types (CONST_CAST_TREE (t));
4704 if (CLASS_TYPE_P (t))
4705 return !CLASSTYPE_NON_STD_LAYOUT (t);
4706 else
4707 return scalarish_type_p (t);
4710 static bool record_has_unique_obj_representations (const_tree, const_tree);
4712 /* Returns true iff T satisfies std::has_unique_object_representations<T>,
4713 as defined in [meta.unary.prop]. */
4715 bool
4716 type_has_unique_obj_representations (const_tree t)
4718 bool ret;
4720 t = strip_array_types (CONST_CAST_TREE (t));
4722 if (!trivially_copyable_p (t))
4723 return false;
4725 if (CLASS_TYPE_P (t) && CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t))
4726 return CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t);
4728 switch (TREE_CODE (t))
4730 case INTEGER_TYPE:
4731 case POINTER_TYPE:
4732 case REFERENCE_TYPE:
4733 /* If some backend has any paddings in these types, we should add
4734 a target hook for this and handle it there. */
4735 return true;
4737 case BOOLEAN_TYPE:
4738 /* For bool values other than 0 and 1 should only appear with
4739 undefined behavior. */
4740 return true;
4742 case ENUMERAL_TYPE:
4743 return type_has_unique_obj_representations (ENUM_UNDERLYING_TYPE (t));
4745 case REAL_TYPE:
4746 /* XFmode certainly contains padding on x86, which the CPU doesn't store
4747 when storing long double values, so for that we have to return false.
4748 Other kinds of floating point values are questionable due to +.0/-.0
4749 and NaNs, let's play safe for now. */
4750 return false;
4752 case FIXED_POINT_TYPE:
4753 return false;
4755 case OFFSET_TYPE:
4756 return true;
4758 case COMPLEX_TYPE:
4759 case VECTOR_TYPE:
4760 return type_has_unique_obj_representations (TREE_TYPE (t));
4762 case RECORD_TYPE:
4763 ret = record_has_unique_obj_representations (t, TYPE_SIZE (t));
4764 if (CLASS_TYPE_P (t))
4766 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
4767 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
4769 return ret;
4771 case UNION_TYPE:
4772 ret = true;
4773 bool any_fields;
4774 any_fields = false;
4775 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4776 if (TREE_CODE (field) == FIELD_DECL)
4778 any_fields = true;
4779 if (!type_has_unique_obj_representations (TREE_TYPE (field))
4780 || simple_cst_equal (DECL_SIZE (field), TYPE_SIZE (t)) != 1)
4782 ret = false;
4783 break;
4786 if (!any_fields && !integer_zerop (TYPE_SIZE (t)))
4787 ret = false;
4788 if (CLASS_TYPE_P (t))
4790 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
4791 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
4793 return ret;
4795 case NULLPTR_TYPE:
4796 return false;
4798 case ERROR_MARK:
4799 return false;
4801 default:
4802 gcc_unreachable ();
4806 /* Helper function for type_has_unique_obj_representations. */
4808 static bool
4809 record_has_unique_obj_representations (const_tree t, const_tree sz)
4811 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4812 if (TREE_CODE (field) != FIELD_DECL)
4814 /* For bases, can't use type_has_unique_obj_representations here, as in
4815 struct S { int i : 24; S (); };
4816 struct T : public S { int j : 8; T (); };
4817 S doesn't have unique obj representations, but T does. */
4818 else if (DECL_FIELD_IS_BASE (field))
4820 if (!record_has_unique_obj_representations (TREE_TYPE (field),
4821 DECL_SIZE (field)))
4822 return false;
4824 else if (DECL_C_BIT_FIELD (field) && !DECL_UNNAMED_BIT_FIELD (field))
4826 tree btype = DECL_BIT_FIELD_TYPE (field);
4827 if (!type_has_unique_obj_representations (btype))
4828 return false;
4830 else if (!type_has_unique_obj_representations (TREE_TYPE (field)))
4831 return false;
4833 offset_int cur = 0;
4834 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4835 if (TREE_CODE (field) == FIELD_DECL && !DECL_UNNAMED_BIT_FIELD (field))
4837 offset_int fld = wi::to_offset (DECL_FIELD_OFFSET (field));
4838 offset_int bitpos = wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
4839 fld = fld * BITS_PER_UNIT + bitpos;
4840 if (cur != fld)
4841 return false;
4842 if (DECL_SIZE (field))
4844 offset_int size = wi::to_offset (DECL_SIZE (field));
4845 cur += size;
4848 if (cur != wi::to_offset (sz))
4849 return false;
4851 return true;
4854 /* Nonzero iff type T is a class template implicit specialization. */
4856 bool
4857 class_tmpl_impl_spec_p (const_tree t)
4859 return CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INSTANTIATION (t);
4862 /* Returns 1 iff zero initialization of type T means actually storing
4863 zeros in it. */
4866 zero_init_p (const_tree t)
4868 /* This CONST_CAST is okay because strip_array_types returns its
4869 argument unmodified and we assign it to a const_tree. */
4870 t = strip_array_types (CONST_CAST_TREE(t));
4872 if (t == error_mark_node)
4873 return 1;
4875 /* NULL pointers to data members are initialized with -1. */
4876 if (TYPE_PTRDATAMEM_P (t))
4877 return 0;
4879 /* Classes that contain types that can't be zero-initialized, cannot
4880 be zero-initialized themselves. */
4881 if (CLASS_TYPE_P (t) && CLASSTYPE_NON_ZERO_INIT_P (t))
4882 return 0;
4884 return 1;
4887 /* Returns true if the expression or initializer T is the result of
4888 zero-initialization for its type, taking pointers to members
4889 into consideration. */
4891 bool
4892 zero_init_expr_p (tree t)
4894 tree type = TREE_TYPE (t);
4895 if (!type || uses_template_parms (type))
4896 return false;
4897 if (TYPE_PTRMEM_P (type))
4898 return null_member_pointer_value_p (t);
4899 if (TREE_CODE (t) == CONSTRUCTOR)
4901 if (COMPOUND_LITERAL_P (t)
4902 || BRACE_ENCLOSED_INITIALIZER_P (t))
4903 /* Undigested, conversions might change the zeroness. */
4904 return false;
4905 for (constructor_elt &elt : CONSTRUCTOR_ELTS (t))
4907 if (TREE_CODE (type) == UNION_TYPE
4908 && elt.index != first_field (type))
4909 return false;
4910 if (!zero_init_expr_p (elt.value))
4911 return false;
4913 return true;
4915 if (zero_init_p (type))
4916 return initializer_zerop (t);
4917 return false;
4920 /* True IFF T is a C++20 structural type (P1907R1) that can be used as a
4921 non-type template parameter. If EXPLAIN, explain why not. */
4923 bool
4924 structural_type_p (tree t, bool explain)
4926 /* A structural type is one of the following: */
4928 /* a scalar type, or */
4929 if (SCALAR_TYPE_P (t))
4930 return true;
4931 /* an lvalue reference type, or */
4932 if (TYPE_REF_P (t) && !TYPE_REF_IS_RVALUE (t))
4933 return true;
4934 /* a literal class type with the following properties:
4935 - all base classes and non-static data members are public and non-mutable
4937 - the types of all bases classes and non-static data members are
4938 structural types or (possibly multi-dimensional) array thereof. */
4939 if (!CLASS_TYPE_P (t))
4940 return false;
4941 if (!literal_type_p (t))
4943 if (explain)
4944 explain_non_literal_class (t);
4945 return false;
4947 for (tree m = next_aggregate_field (TYPE_FIELDS (t)); m;
4948 m = next_aggregate_field (DECL_CHAIN (m)))
4950 if (TREE_PRIVATE (m) || TREE_PROTECTED (m))
4952 if (explain)
4954 if (DECL_FIELD_IS_BASE (m))
4955 inform (location_of (m), "base class %qT is not public",
4956 TREE_TYPE (m));
4957 else
4958 inform (location_of (m), "%qD is not public", m);
4960 return false;
4962 if (DECL_MUTABLE_P (m))
4964 if (explain)
4965 inform (location_of (m), "%qD is mutable", m);
4966 return false;
4968 tree mtype = strip_array_types (TREE_TYPE (m));
4969 if (!structural_type_p (mtype))
4971 if (explain)
4973 inform (location_of (m), "%qD has a non-structural type", m);
4974 structural_type_p (mtype, true);
4976 return false;
4979 return true;
4982 /* Partially handle the C++11 [[carries_dependency]] attribute.
4983 Just emit a different diagnostics when it is used on something the
4984 spec doesn't allow vs. where it allows and we just choose to ignore
4985 it. */
4987 static tree
4988 handle_carries_dependency_attribute (tree *node, tree name,
4989 tree ARG_UNUSED (args),
4990 int ARG_UNUSED (flags),
4991 bool *no_add_attrs)
4993 if (TREE_CODE (*node) != FUNCTION_DECL
4994 && TREE_CODE (*node) != PARM_DECL)
4996 warning (OPT_Wattributes, "%qE attribute can only be applied to "
4997 "functions or parameters", name);
4998 *no_add_attrs = true;
5000 else
5002 warning (OPT_Wattributes, "%qE attribute ignored", name);
5003 *no_add_attrs = true;
5005 return NULL_TREE;
5008 /* Handle the C++17 [[nodiscard]] attribute, which is similar to the GNU
5009 warn_unused_result attribute. */
5011 static tree
5012 handle_nodiscard_attribute (tree *node, tree name, tree args,
5013 int /*flags*/, bool *no_add_attrs)
5015 if (args && TREE_CODE (TREE_VALUE (args)) != STRING_CST)
5017 error ("%qE attribute argument must be a string constant", name);
5018 *no_add_attrs = true;
5020 if (TREE_CODE (*node) == FUNCTION_DECL)
5022 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node)))
5023 && !DECL_CONSTRUCTOR_P (*node))
5024 warning_at (DECL_SOURCE_LOCATION (*node),
5025 OPT_Wattributes, "%qE attribute applied to %qD with void "
5026 "return type", name, *node);
5028 else if (OVERLOAD_TYPE_P (*node))
5029 /* OK */;
5030 else
5032 warning (OPT_Wattributes, "%qE attribute can only be applied to "
5033 "functions or to class or enumeration types", name);
5034 *no_add_attrs = true;
5036 return NULL_TREE;
5039 /* Handle a C++20 "no_unique_address" attribute; arguments as in
5040 struct attribute_spec.handler. */
5041 static tree
5042 handle_no_unique_addr_attribute (tree* node,
5043 tree name,
5044 tree /*args*/,
5045 int /*flags*/,
5046 bool* no_add_attrs)
5048 if (TREE_CODE (*node) != FIELD_DECL)
5050 warning (OPT_Wattributes, "%qE attribute can only be applied to "
5051 "non-static data members", name);
5052 *no_add_attrs = true;
5054 else if (DECL_C_BIT_FIELD (*node))
5056 warning (OPT_Wattributes, "%qE attribute cannot be applied to "
5057 "a bit-field", name);
5058 *no_add_attrs = true;
5061 return NULL_TREE;
5064 /* The C++20 [[likely]] and [[unlikely]] attributes on labels map to the GNU
5065 hot/cold attributes. */
5067 static tree
5068 handle_likeliness_attribute (tree *node, tree name, tree args,
5069 int flags, bool *no_add_attrs)
5071 *no_add_attrs = true;
5072 if (TREE_CODE (*node) == LABEL_DECL
5073 || TREE_CODE (*node) == FUNCTION_DECL)
5075 if (args)
5076 warning (OPT_Wattributes, "%qE attribute takes no arguments", name);
5077 tree bname = (is_attribute_p ("likely", name)
5078 ? get_identifier ("hot") : get_identifier ("cold"));
5079 if (TREE_CODE (*node) == FUNCTION_DECL)
5080 warning (OPT_Wattributes, "ISO C++ %qE attribute does not apply to "
5081 "functions; treating as %<[[gnu::%E]]%>", name, bname);
5082 tree battr = build_tree_list (bname, NULL_TREE);
5083 decl_attributes (node, battr, flags);
5084 return NULL_TREE;
5086 else
5087 return error_mark_node;
5090 /* Table of valid C++ attributes. */
5091 const struct attribute_spec cxx_attribute_table[] =
5093 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
5094 affects_type_identity, handler, exclude } */
5095 { "init_priority", 1, 1, true, false, false, false,
5096 handle_init_priority_attribute, NULL },
5097 { "abi_tag", 1, -1, false, false, false, true,
5098 handle_abi_tag_attribute, NULL },
5099 { NULL, 0, 0, false, false, false, false, NULL, NULL }
5102 /* Table of C++ standard attributes. */
5103 const struct attribute_spec std_attribute_table[] =
5105 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
5106 affects_type_identity, handler, exclude } */
5107 { "maybe_unused", 0, 0, false, false, false, false,
5108 handle_unused_attribute, NULL },
5109 { "nodiscard", 0, 1, false, false, false, false,
5110 handle_nodiscard_attribute, NULL },
5111 { "no_unique_address", 0, 0, true, false, false, false,
5112 handle_no_unique_addr_attribute, NULL },
5113 { "likely", 0, 0, false, false, false, false,
5114 handle_likeliness_attribute, attr_cold_hot_exclusions },
5115 { "unlikely", 0, 0, false, false, false, false,
5116 handle_likeliness_attribute, attr_cold_hot_exclusions },
5117 { "noreturn", 0, 0, true, false, false, false,
5118 handle_noreturn_attribute, attr_noreturn_exclusions },
5119 { "carries_dependency", 0, 0, true, false, false, false,
5120 handle_carries_dependency_attribute, NULL },
5121 { "pre", 0, -1, false, false, false, false,
5122 handle_contract_attribute, NULL },
5123 { "post", 0, -1, false, false, false, false,
5124 handle_contract_attribute, NULL },
5125 { NULL, 0, 0, false, false, false, false, NULL, NULL }
5128 /* Handle an "init_priority" attribute; arguments as in
5129 struct attribute_spec.handler. */
5130 static tree
5131 handle_init_priority_attribute (tree* node,
5132 tree name,
5133 tree args,
5134 int /*flags*/,
5135 bool* no_add_attrs)
5137 if (!SUPPORTS_INIT_PRIORITY)
5138 /* Treat init_priority as an unrecognized attribute (mirroring
5139 __has_attribute) if the target doesn't support init priorities. */
5140 return error_mark_node;
5142 tree initp_expr = TREE_VALUE (args);
5143 tree decl = *node;
5144 tree type = TREE_TYPE (decl);
5145 int pri;
5147 STRIP_NOPS (initp_expr);
5148 initp_expr = default_conversion (initp_expr);
5149 if (initp_expr)
5150 initp_expr = maybe_constant_value (initp_expr);
5152 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
5154 error ("requested %<init_priority%> is not an integer constant");
5155 cxx_constant_value (initp_expr);
5156 *no_add_attrs = true;
5157 return NULL_TREE;
5160 pri = TREE_INT_CST_LOW (initp_expr);
5162 type = strip_array_types (type);
5164 if (decl == NULL_TREE
5165 || !VAR_P (decl)
5166 || !TREE_STATIC (decl)
5167 || DECL_EXTERNAL (decl)
5168 || (TREE_CODE (type) != RECORD_TYPE
5169 && TREE_CODE (type) != UNION_TYPE)
5170 /* Static objects in functions are initialized the
5171 first time control passes through that
5172 function. This is not precise enough to pin down an
5173 init_priority value, so don't allow it. */
5174 || current_function_decl)
5176 error ("can only use %qE attribute on file-scope definitions "
5177 "of objects of class type", name);
5178 *no_add_attrs = true;
5179 return NULL_TREE;
5182 if (pri > MAX_INIT_PRIORITY || pri <= 0)
5184 error ("requested %<init_priority%> %i is out of range [0, %i]",
5185 pri, MAX_INIT_PRIORITY);
5186 *no_add_attrs = true;
5187 return NULL_TREE;
5190 /* Check for init_priorities that are reserved for
5191 language and runtime support implementations.*/
5192 if (pri <= MAX_RESERVED_INIT_PRIORITY)
5194 warning
5195 (0, "requested %<init_priority%> %i is reserved for internal use",
5196 pri);
5199 SET_DECL_INIT_PRIORITY (decl, pri);
5200 DECL_HAS_INIT_PRIORITY_P (decl) = 1;
5201 return NULL_TREE;
5204 /* DECL is being redeclared; the old declaration had the abi tags in OLD,
5205 and the new one has the tags in NEW_. Give an error if there are tags
5206 in NEW_ that weren't in OLD. */
5208 bool
5209 check_abi_tag_redeclaration (const_tree decl, const_tree old, const_tree new_)
5211 if (old && TREE_CODE (TREE_VALUE (old)) == TREE_LIST)
5212 old = TREE_VALUE (old);
5213 if (new_ && TREE_CODE (TREE_VALUE (new_)) == TREE_LIST)
5214 new_ = TREE_VALUE (new_);
5215 bool err = false;
5216 for (const_tree t = new_; t; t = TREE_CHAIN (t))
5218 tree str = TREE_VALUE (t);
5219 for (const_tree in = old; in; in = TREE_CHAIN (in))
5221 tree ostr = TREE_VALUE (in);
5222 if (cp_tree_equal (str, ostr))
5223 goto found;
5225 error ("redeclaration of %qD adds abi tag %qE", decl, str);
5226 err = true;
5227 found:;
5229 if (err)
5231 inform (DECL_SOURCE_LOCATION (decl), "previous declaration here");
5232 return false;
5234 return true;
5237 /* The abi_tag attribute with the name NAME was given ARGS. If they are
5238 ill-formed, give an error and return false; otherwise, return true. */
5240 bool
5241 check_abi_tag_args (tree args, tree name)
5243 if (!args)
5245 error ("the %qE attribute requires arguments", name);
5246 return false;
5248 for (tree arg = args; arg; arg = TREE_CHAIN (arg))
5250 tree elt = TREE_VALUE (arg);
5251 if (TREE_CODE (elt) != STRING_CST
5252 || (!same_type_ignoring_top_level_qualifiers_p
5253 (strip_array_types (TREE_TYPE (elt)),
5254 char_type_node)))
5256 error ("arguments to the %qE attribute must be narrow string "
5257 "literals", name);
5258 return false;
5260 const char *begin = TREE_STRING_POINTER (elt);
5261 const char *end = begin + TREE_STRING_LENGTH (elt);
5262 for (const char *p = begin; p != end; ++p)
5264 char c = *p;
5265 if (p == begin)
5267 if (!ISALPHA (c) && c != '_')
5269 error ("arguments to the %qE attribute must contain valid "
5270 "identifiers", name);
5271 inform (input_location, "%<%c%> is not a valid first "
5272 "character for an identifier", c);
5273 return false;
5276 else if (p == end - 1)
5277 gcc_assert (c == 0);
5278 else
5280 if (!ISALNUM (c) && c != '_')
5282 error ("arguments to the %qE attribute must contain valid "
5283 "identifiers", name);
5284 inform (input_location, "%<%c%> is not a valid character "
5285 "in an identifier", c);
5286 return false;
5291 return true;
5294 /* Handle an "abi_tag" attribute; arguments as in
5295 struct attribute_spec.handler. */
5297 static tree
5298 handle_abi_tag_attribute (tree* node, tree name, tree args,
5299 int flags, bool* no_add_attrs)
5301 if (!check_abi_tag_args (args, name))
5302 goto fail;
5304 if (TYPE_P (*node))
5306 if (!OVERLOAD_TYPE_P (*node))
5308 error ("%qE attribute applied to non-class, non-enum type %qT",
5309 name, *node);
5310 goto fail;
5312 else if (!(flags & (int)ATTR_FLAG_TYPE_IN_PLACE))
5314 error ("%qE attribute applied to %qT after its definition",
5315 name, *node);
5316 goto fail;
5318 else if (CLASS_TYPE_P (*node)
5319 && CLASSTYPE_TEMPLATE_INSTANTIATION (*node))
5321 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
5322 "template instantiation %qT", name, *node);
5323 goto fail;
5325 else if (CLASS_TYPE_P (*node)
5326 && CLASSTYPE_TEMPLATE_SPECIALIZATION (*node))
5328 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
5329 "template specialization %qT", name, *node);
5330 goto fail;
5333 tree attributes = TYPE_ATTRIBUTES (*node);
5334 tree decl = TYPE_NAME (*node);
5336 /* Make sure all declarations have the same abi tags. */
5337 if (DECL_SOURCE_LOCATION (decl) != input_location)
5339 if (!check_abi_tag_redeclaration (decl,
5340 lookup_attribute ("abi_tag",
5341 attributes),
5342 args))
5343 goto fail;
5346 else
5348 if (!VAR_OR_FUNCTION_DECL_P (*node))
5350 error ("%qE attribute applied to non-function, non-variable %qD",
5351 name, *node);
5352 goto fail;
5354 else if (DECL_LANGUAGE (*node) == lang_c)
5356 error ("%qE attribute applied to extern \"C\" declaration %qD",
5357 name, *node);
5358 goto fail;
5362 return NULL_TREE;
5364 fail:
5365 *no_add_attrs = true;
5366 return NULL_TREE;
5369 /* Perform checking for contract attributes. */
5371 tree
5372 handle_contract_attribute (tree *ARG_UNUSED (node), tree ARG_UNUSED (name),
5373 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
5374 bool *ARG_UNUSED (no_add_attrs))
5376 /* TODO: Is there any checking we could do here? */
5377 return NULL_TREE;
5380 /* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the
5381 thing pointed to by the constant. */
5383 tree
5384 make_ptrmem_cst (tree type, tree member)
5386 tree ptrmem_cst = make_node (PTRMEM_CST);
5387 TREE_TYPE (ptrmem_cst) = type;
5388 PTRMEM_CST_MEMBER (ptrmem_cst) = member;
5389 PTRMEM_CST_LOCATION (ptrmem_cst) = input_location;
5390 return ptrmem_cst;
5393 /* Build a variant of TYPE that has the indicated ATTRIBUTES. May
5394 return an existing type if an appropriate type already exists. */
5396 tree
5397 cp_build_type_attribute_variant (tree type, tree attributes)
5399 tree new_type;
5401 new_type = build_type_attribute_variant (type, attributes);
5402 if (FUNC_OR_METHOD_TYPE_P (new_type))
5403 gcc_checking_assert (cxx_type_hash_eq (type, new_type));
5405 /* Making a new main variant of a class type is broken. */
5406 gcc_assert (!CLASS_TYPE_P (type) || new_type == type);
5408 return new_type;
5411 /* Return TRUE if TYPE1 and TYPE2 are identical for type hashing purposes.
5412 Called only after doing all language independent checks. */
5414 bool
5415 cxx_type_hash_eq (const_tree typea, const_tree typeb)
5417 gcc_assert (FUNC_OR_METHOD_TYPE_P (typea));
5419 if (type_memfn_rqual (typea) != type_memfn_rqual (typeb))
5420 return false;
5421 if (TYPE_HAS_LATE_RETURN_TYPE (typea) != TYPE_HAS_LATE_RETURN_TYPE (typeb))
5422 return false;
5423 return comp_except_specs (TYPE_RAISES_EXCEPTIONS (typea),
5424 TYPE_RAISES_EXCEPTIONS (typeb), ce_exact);
5427 /* Copy the language-specific type variant modifiers from TYPEB to TYPEA. For
5428 C++, these are the exception-specifier and ref-qualifier. */
5430 tree
5431 cxx_copy_lang_qualifiers (const_tree typea, const_tree typeb)
5433 tree type = CONST_CAST_TREE (typea);
5434 if (FUNC_OR_METHOD_TYPE_P (type))
5435 type = build_cp_fntype_variant (type, type_memfn_rqual (typeb),
5436 TYPE_RAISES_EXCEPTIONS (typeb),
5437 TYPE_HAS_LATE_RETURN_TYPE (typeb));
5438 return type;
5441 /* Apply FUNC to all language-specific sub-trees of TP in a pre-order
5442 traversal. Called from walk_tree. */
5444 tree
5445 cp_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func,
5446 void *data, hash_set<tree> *pset)
5448 tree t = *tp;
5449 enum tree_code code = TREE_CODE (t);
5450 tree result;
5452 #define WALK_SUBTREE(NODE) \
5453 do \
5455 result = cp_walk_tree (&(NODE), func, data, pset); \
5456 if (result) goto out; \
5458 while (0)
5460 if (TYPE_P (t))
5462 /* If *WALK_SUBTREES_P is 1, we're interested in the syntactic form of
5463 the argument, so don't look through typedefs, but do walk into
5464 template arguments for alias templates (and non-typedefed classes).
5466 If *WALK_SUBTREES_P > 1, we're interested in type identity or
5467 equivalence, so look through typedefs, ignoring template arguments for
5468 alias templates, and walk into template args of classes.
5470 See find_abi_tags_r for an example of setting *WALK_SUBTREES_P to 2
5471 when that's the behavior the walk_tree_fn wants. */
5472 if (*walk_subtrees_p == 1 && typedef_variant_p (t))
5474 if (tree ti = TYPE_ALIAS_TEMPLATE_INFO (t))
5475 WALK_SUBTREE (TI_ARGS (ti));
5476 *walk_subtrees_p = 0;
5477 return NULL_TREE;
5480 if (tree ti = TYPE_TEMPLATE_INFO (t))
5481 WALK_SUBTREE (TI_ARGS (ti));
5484 /* Not one of the easy cases. We must explicitly go through the
5485 children. */
5486 result = NULL_TREE;
5487 switch (code)
5489 case TEMPLATE_TYPE_PARM:
5490 if (template_placeholder_p (t))
5491 WALK_SUBTREE (CLASS_PLACEHOLDER_TEMPLATE (t));
5492 /* Fall through. */
5493 case DEFERRED_PARSE:
5494 case TEMPLATE_TEMPLATE_PARM:
5495 case BOUND_TEMPLATE_TEMPLATE_PARM:
5496 case UNBOUND_CLASS_TEMPLATE:
5497 case TEMPLATE_PARM_INDEX:
5498 case TYPEOF_TYPE:
5499 /* None of these have subtrees other than those already walked
5500 above. */
5501 *walk_subtrees_p = 0;
5502 break;
5504 case TYPENAME_TYPE:
5505 WALK_SUBTREE (TYPE_CONTEXT (t));
5506 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
5507 *walk_subtrees_p = 0;
5508 break;
5510 case BASELINK:
5511 if (BASELINK_QUALIFIED_P (t))
5512 WALK_SUBTREE (BINFO_TYPE (BASELINK_ACCESS_BINFO (t)));
5513 WALK_SUBTREE (BASELINK_FUNCTIONS (t));
5514 *walk_subtrees_p = 0;
5515 break;
5517 case PTRMEM_CST:
5518 WALK_SUBTREE (TREE_TYPE (t));
5519 *walk_subtrees_p = 0;
5520 break;
5522 case TREE_LIST:
5523 WALK_SUBTREE (TREE_PURPOSE (t));
5524 break;
5526 case OVERLOAD:
5527 WALK_SUBTREE (OVL_FUNCTION (t));
5528 WALK_SUBTREE (OVL_CHAIN (t));
5529 *walk_subtrees_p = 0;
5530 break;
5532 case USING_DECL:
5533 WALK_SUBTREE (DECL_NAME (t));
5534 WALK_SUBTREE (USING_DECL_SCOPE (t));
5535 WALK_SUBTREE (USING_DECL_DECLS (t));
5536 *walk_subtrees_p = 0;
5537 break;
5539 case RECORD_TYPE:
5540 if (TYPE_PTRMEMFUNC_P (t))
5541 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (t));
5542 break;
5544 case TYPE_ARGUMENT_PACK:
5545 case NONTYPE_ARGUMENT_PACK:
5547 tree args = ARGUMENT_PACK_ARGS (t);
5548 for (tree arg : tree_vec_range (args))
5549 WALK_SUBTREE (arg);
5551 break;
5553 case TYPE_PACK_EXPANSION:
5554 WALK_SUBTREE (TREE_TYPE (t));
5555 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (t));
5556 *walk_subtrees_p = 0;
5557 break;
5559 case EXPR_PACK_EXPANSION:
5560 WALK_SUBTREE (TREE_OPERAND (t, 0));
5561 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (t));
5562 *walk_subtrees_p = 0;
5563 break;
5565 case CAST_EXPR:
5566 case REINTERPRET_CAST_EXPR:
5567 case STATIC_CAST_EXPR:
5568 case CONST_CAST_EXPR:
5569 case DYNAMIC_CAST_EXPR:
5570 case IMPLICIT_CONV_EXPR:
5571 case BIT_CAST_EXPR:
5572 if (TREE_TYPE (t))
5573 WALK_SUBTREE (TREE_TYPE (t));
5574 break;
5576 case CONSTRUCTOR:
5577 if (COMPOUND_LITERAL_P (t))
5578 WALK_SUBTREE (TREE_TYPE (t));
5579 break;
5581 case TRAIT_EXPR:
5582 WALK_SUBTREE (TRAIT_EXPR_TYPE1 (t));
5583 WALK_SUBTREE (TRAIT_EXPR_TYPE2 (t));
5584 *walk_subtrees_p = 0;
5585 break;
5587 case TRAIT_TYPE:
5588 WALK_SUBTREE (TRAIT_TYPE_TYPE1 (t));
5589 WALK_SUBTREE (TRAIT_TYPE_TYPE2 (t));
5590 *walk_subtrees_p = 0;
5591 break;
5593 case DECLTYPE_TYPE:
5595 cp_unevaluated u;
5596 WALK_SUBTREE (DECLTYPE_TYPE_EXPR (t));
5597 *walk_subtrees_p = 0;
5598 break;
5601 case ALIGNOF_EXPR:
5602 case SIZEOF_EXPR:
5603 case NOEXCEPT_EXPR:
5605 cp_unevaluated u;
5606 WALK_SUBTREE (TREE_OPERAND (t, 0));
5607 *walk_subtrees_p = 0;
5608 break;
5611 case REQUIRES_EXPR:
5613 cp_unevaluated u;
5614 for (tree parm = REQUIRES_EXPR_PARMS (t); parm; parm = DECL_CHAIN (parm))
5615 /* Walk the types of each parameter, but not the parameter itself,
5616 since doing so would cause false positives in the unexpanded pack
5617 checker if the requires-expr introduces a function parameter pack,
5618 e.g. requires (Ts... ts) { }. */
5619 WALK_SUBTREE (TREE_TYPE (parm));
5620 WALK_SUBTREE (REQUIRES_EXPR_REQS (t));
5621 *walk_subtrees_p = 0;
5622 break;
5625 case DECL_EXPR:
5626 /* User variables should be mentioned in BIND_EXPR_VARS
5627 and their initializers and sizes walked when walking
5628 the containing BIND_EXPR. Compiler temporaries are
5629 handled here. And also normal variables in templates,
5630 since do_poplevel doesn't build a BIND_EXPR then. */
5631 if (VAR_P (TREE_OPERAND (t, 0))
5632 && (processing_template_decl
5633 || (DECL_ARTIFICIAL (TREE_OPERAND (t, 0))
5634 && !TREE_STATIC (TREE_OPERAND (t, 0)))))
5636 tree decl = TREE_OPERAND (t, 0);
5637 WALK_SUBTREE (DECL_INITIAL (decl));
5638 WALK_SUBTREE (DECL_SIZE (decl));
5639 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
5641 break;
5643 case LAMBDA_EXPR:
5644 /* Don't walk into the body of the lambda, but the capture initializers
5645 are part of the enclosing context. */
5646 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
5647 cap = TREE_CHAIN (cap))
5648 WALK_SUBTREE (TREE_VALUE (cap));
5649 break;
5651 case CO_YIELD_EXPR:
5652 if (TREE_OPERAND (t, 1))
5653 /* Operand 1 is the tree for the relevant co_await which has any
5654 interesting sub-trees. */
5655 WALK_SUBTREE (TREE_OPERAND (t, 1));
5656 break;
5658 case CO_AWAIT_EXPR:
5659 if (TREE_OPERAND (t, 1))
5660 /* Operand 1 is frame variable. */
5661 WALK_SUBTREE (TREE_OPERAND (t, 1));
5662 if (TREE_OPERAND (t, 2))
5663 /* Operand 2 has the initialiser, and we need to walk any subtrees
5664 there. */
5665 WALK_SUBTREE (TREE_OPERAND (t, 2));
5666 break;
5668 case CO_RETURN_EXPR:
5669 if (TREE_OPERAND (t, 0))
5671 if (VOID_TYPE_P (TREE_OPERAND (t, 0)))
5672 /* For void expressions, operand 1 is a trivial call, and any
5673 interesting subtrees will be part of operand 0. */
5674 WALK_SUBTREE (TREE_OPERAND (t, 0));
5675 else if (TREE_OPERAND (t, 1))
5676 /* Interesting sub-trees will be in the return_value () call
5677 arguments. */
5678 WALK_SUBTREE (TREE_OPERAND (t, 1));
5680 break;
5682 case STATIC_ASSERT:
5683 WALK_SUBTREE (STATIC_ASSERT_CONDITION (t));
5684 WALK_SUBTREE (STATIC_ASSERT_MESSAGE (t));
5685 break;
5687 default:
5688 return NULL_TREE;
5691 /* We didn't find what we were looking for. */
5692 out:
5693 return result;
5695 #undef WALK_SUBTREE
5698 /* Like save_expr, but for C++. */
5700 tree
5701 cp_save_expr (tree expr)
5703 /* There is no reason to create a SAVE_EXPR within a template; if
5704 needed, we can create the SAVE_EXPR when instantiating the
5705 template. Furthermore, the middle-end cannot handle C++-specific
5706 tree codes. */
5707 if (processing_template_decl)
5708 return expr;
5710 /* TARGET_EXPRs are only expanded once. */
5711 if (TREE_CODE (expr) == TARGET_EXPR)
5712 return expr;
5714 return save_expr (expr);
5717 /* Initialize tree.cc. */
5719 void
5720 init_tree (void)
5722 list_hash_table = hash_table<list_hasher>::create_ggc (61);
5723 register_scoped_attributes (std_attribute_table, NULL);
5726 /* Returns the kind of special function that DECL (a FUNCTION_DECL)
5727 is. Note that sfk_none is zero, so this function can be used as a
5728 predicate to test whether or not DECL is a special function. */
5730 special_function_kind
5731 special_function_p (const_tree decl)
5733 /* Rather than doing all this stuff with magic names, we should
5734 probably have a field of type `special_function_kind' in
5735 DECL_LANG_SPECIFIC. */
5736 if (DECL_INHERITED_CTOR (decl))
5737 return sfk_inheriting_constructor;
5738 if (DECL_COPY_CONSTRUCTOR_P (decl))
5739 return sfk_copy_constructor;
5740 if (DECL_MOVE_CONSTRUCTOR_P (decl))
5741 return sfk_move_constructor;
5742 if (DECL_CONSTRUCTOR_P (decl))
5743 return sfk_constructor;
5744 if (DECL_ASSIGNMENT_OPERATOR_P (decl)
5745 && DECL_OVERLOADED_OPERATOR_IS (decl, NOP_EXPR))
5747 if (copy_fn_p (decl))
5748 return sfk_copy_assignment;
5749 if (move_fn_p (decl))
5750 return sfk_move_assignment;
5752 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
5753 return sfk_destructor;
5754 if (DECL_COMPLETE_DESTRUCTOR_P (decl))
5755 return sfk_complete_destructor;
5756 if (DECL_BASE_DESTRUCTOR_P (decl))
5757 return sfk_base_destructor;
5758 if (DECL_DELETING_DESTRUCTOR_P (decl))
5759 return sfk_deleting_destructor;
5760 if (DECL_CONV_FN_P (decl))
5761 return sfk_conversion;
5762 if (deduction_guide_p (decl))
5763 return sfk_deduction_guide;
5764 if (DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) >= OVL_OP_EQ_EXPR
5765 && DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) <= OVL_OP_SPACESHIP_EXPR)
5766 return sfk_comparison;
5768 return sfk_none;
5771 /* As above, but only if DECL is a special member function as per 11.3.3
5772 [special]: default/copy/move ctor, copy/move assignment, or destructor. */
5774 special_function_kind
5775 special_memfn_p (const_tree decl)
5777 switch (special_function_kind sfk = special_function_p (decl))
5779 case sfk_constructor:
5780 if (!default_ctor_p (decl))
5781 break;
5782 gcc_fallthrough();
5783 case sfk_copy_constructor:
5784 case sfk_copy_assignment:
5785 case sfk_move_assignment:
5786 case sfk_move_constructor:
5787 case sfk_destructor:
5788 return sfk;
5790 default:
5791 break;
5793 return sfk_none;
5796 /* Returns nonzero if TYPE is a character type, including wchar_t. */
5799 char_type_p (tree type)
5801 return (same_type_p (type, char_type_node)
5802 || same_type_p (type, unsigned_char_type_node)
5803 || same_type_p (type, signed_char_type_node)
5804 || same_type_p (type, char8_type_node)
5805 || same_type_p (type, char16_type_node)
5806 || same_type_p (type, char32_type_node)
5807 || same_type_p (type, wchar_type_node));
5810 /* Returns the kind of linkage associated with the indicated DECL. Th
5811 value returned is as specified by the language standard; it is
5812 independent of implementation details regarding template
5813 instantiation, etc. For example, it is possible that a declaration
5814 to which this function assigns external linkage would not show up
5815 as a global symbol when you run `nm' on the resulting object file. */
5817 linkage_kind
5818 decl_linkage (tree decl)
5820 /* This function doesn't attempt to calculate the linkage from first
5821 principles as given in [basic.link]. Instead, it makes use of
5822 the fact that we have already set TREE_PUBLIC appropriately, and
5823 then handles a few special cases. Ideally, we would calculate
5824 linkage first, and then transform that into a concrete
5825 implementation. */
5827 /* Things that don't have names have no linkage. */
5828 if (!DECL_NAME (decl))
5829 return lk_none;
5831 /* Fields have no linkage. */
5832 if (TREE_CODE (decl) == FIELD_DECL)
5833 return lk_none;
5835 /* Things in local scope do not have linkage. */
5836 if (decl_function_context (decl))
5837 return lk_none;
5839 /* Things that are TREE_PUBLIC have external linkage. */
5840 if (TREE_PUBLIC (decl))
5841 return lk_external;
5843 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor variants,
5844 check one of the "clones" for the real linkage. */
5845 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl)
5846 && DECL_CHAIN (decl)
5847 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)))
5848 return decl_linkage (DECL_CHAIN (decl));
5850 if (TREE_CODE (decl) == NAMESPACE_DECL)
5851 return lk_external;
5853 /* Linkage of a CONST_DECL depends on the linkage of the enumeration
5854 type. */
5855 if (TREE_CODE (decl) == CONST_DECL)
5856 return decl_linkage (TYPE_NAME (DECL_CONTEXT (decl)));
5858 /* Members of the anonymous namespace also have TREE_PUBLIC unset, but
5859 are considered to have external linkage for language purposes, as do
5860 template instantiations on targets without weak symbols. DECLs really
5861 meant to have internal linkage have DECL_THIS_STATIC set. */
5862 if (TREE_CODE (decl) == TYPE_DECL)
5863 return lk_external;
5864 if (VAR_OR_FUNCTION_DECL_P (decl))
5866 if (!DECL_THIS_STATIC (decl))
5867 return lk_external;
5869 /* Static data members and static member functions from classes
5870 in anonymous namespace also don't have TREE_PUBLIC set. */
5871 if (DECL_CLASS_CONTEXT (decl))
5872 return lk_external;
5875 /* Everything else has internal linkage. */
5876 return lk_internal;
5879 /* Returns the storage duration of the object or reference associated with
5880 the indicated DECL, which should be a VAR_DECL or PARM_DECL. */
5882 duration_kind
5883 decl_storage_duration (tree decl)
5885 if (TREE_CODE (decl) == PARM_DECL)
5886 return dk_auto;
5887 if (TREE_CODE (decl) == FUNCTION_DECL)
5888 return dk_static;
5889 gcc_assert (VAR_P (decl));
5890 if (!TREE_STATIC (decl)
5891 && !DECL_EXTERNAL (decl))
5892 return dk_auto;
5893 if (CP_DECL_THREAD_LOCAL_P (decl))
5894 return dk_thread;
5895 return dk_static;
5898 /* EXP is an expression that we want to pre-evaluate. Returns (in
5899 *INITP) an expression that will perform the pre-evaluation. The
5900 value returned by this function is a side-effect free expression
5901 equivalent to the pre-evaluated expression. Callers must ensure
5902 that *INITP is evaluated before EXP. */
5904 tree
5905 stabilize_expr (tree exp, tree* initp)
5907 tree init_expr;
5909 if (!TREE_SIDE_EFFECTS (exp))
5910 init_expr = NULL_TREE;
5911 else if (VOID_TYPE_P (TREE_TYPE (exp)))
5913 init_expr = exp;
5914 exp = void_node;
5916 /* There are no expressions with REFERENCE_TYPE, but there can be call
5917 arguments with such a type; just treat it as a pointer. */
5918 else if (TYPE_REF_P (TREE_TYPE (exp))
5919 || SCALAR_TYPE_P (TREE_TYPE (exp))
5920 || !glvalue_p (exp))
5922 init_expr = get_target_expr (exp);
5923 exp = TARGET_EXPR_SLOT (init_expr);
5924 if (CLASS_TYPE_P (TREE_TYPE (exp)))
5925 exp = move (exp);
5926 else
5927 exp = rvalue (exp);
5929 else
5931 bool xval = !lvalue_p (exp);
5932 exp = cp_build_addr_expr (exp, tf_warning_or_error);
5933 init_expr = get_target_expr (exp);
5934 exp = TARGET_EXPR_SLOT (init_expr);
5935 exp = cp_build_fold_indirect_ref (exp);
5936 if (xval)
5937 exp = move (exp);
5939 *initp = init_expr;
5941 gcc_assert (!TREE_SIDE_EFFECTS (exp));
5942 return exp;
5945 /* Add NEW_EXPR, an expression whose value we don't care about, after the
5946 similar expression ORIG. */
5948 tree
5949 add_stmt_to_compound (tree orig, tree new_expr)
5951 if (!new_expr || !TREE_SIDE_EFFECTS (new_expr))
5952 return orig;
5953 if (!orig || !TREE_SIDE_EFFECTS (orig))
5954 return new_expr;
5955 return build2 (COMPOUND_EXPR, void_type_node, orig, new_expr);
5958 /* Like stabilize_expr, but for a call whose arguments we want to
5959 pre-evaluate. CALL is modified in place to use the pre-evaluated
5960 arguments, while, upon return, *INITP contains an expression to
5961 compute the arguments. */
5963 void
5964 stabilize_call (tree call, tree *initp)
5966 tree inits = NULL_TREE;
5967 int i;
5968 int nargs = call_expr_nargs (call);
5970 if (call == error_mark_node || processing_template_decl)
5972 *initp = NULL_TREE;
5973 return;
5976 gcc_assert (TREE_CODE (call) == CALL_EXPR);
5978 for (i = 0; i < nargs; i++)
5980 tree init;
5981 CALL_EXPR_ARG (call, i) =
5982 stabilize_expr (CALL_EXPR_ARG (call, i), &init);
5983 inits = add_stmt_to_compound (inits, init);
5986 *initp = inits;
5989 /* Like stabilize_expr, but for an AGGR_INIT_EXPR whose arguments we want
5990 to pre-evaluate. CALL is modified in place to use the pre-evaluated
5991 arguments, while, upon return, *INITP contains an expression to
5992 compute the arguments. */
5994 static void
5995 stabilize_aggr_init (tree call, tree *initp)
5997 tree inits = NULL_TREE;
5998 int i;
5999 int nargs = aggr_init_expr_nargs (call);
6001 if (call == error_mark_node)
6002 return;
6004 gcc_assert (TREE_CODE (call) == AGGR_INIT_EXPR);
6006 for (i = 0; i < nargs; i++)
6008 tree init;
6009 AGGR_INIT_EXPR_ARG (call, i) =
6010 stabilize_expr (AGGR_INIT_EXPR_ARG (call, i), &init);
6011 inits = add_stmt_to_compound (inits, init);
6014 *initp = inits;
6017 /* Like stabilize_expr, but for an initialization.
6019 If the initialization is for an object of class type, this function
6020 takes care not to introduce additional temporaries.
6022 Returns TRUE iff the expression was successfully pre-evaluated,
6023 i.e., if INIT is now side-effect free, except for, possibly, a
6024 single call to a constructor. */
6026 bool
6027 stabilize_init (tree init, tree *initp)
6029 tree t = init;
6031 *initp = NULL_TREE;
6033 if (t == error_mark_node || processing_template_decl)
6034 return true;
6036 if (TREE_CODE (t) == INIT_EXPR)
6037 t = TREE_OPERAND (t, 1);
6038 if (TREE_CODE (t) == TARGET_EXPR)
6039 t = TARGET_EXPR_INITIAL (t);
6041 /* If the RHS can be stabilized without breaking copy elision, stabilize
6042 it. We specifically don't stabilize class prvalues here because that
6043 would mean an extra copy, but they might be stabilized below. */
6044 if (TREE_CODE (init) == INIT_EXPR
6045 && TREE_CODE (t) != CONSTRUCTOR
6046 && TREE_CODE (t) != AGGR_INIT_EXPR
6047 && (SCALAR_TYPE_P (TREE_TYPE (t))
6048 || glvalue_p (t)))
6050 TREE_OPERAND (init, 1) = stabilize_expr (t, initp);
6051 return true;
6054 if (TREE_CODE (t) == COMPOUND_EXPR
6055 && TREE_CODE (init) == INIT_EXPR)
6057 tree last = expr_last (t);
6058 /* Handle stabilizing the EMPTY_CLASS_EXPR pattern. */
6059 if (!TREE_SIDE_EFFECTS (last))
6061 *initp = t;
6062 TREE_OPERAND (init, 1) = last;
6063 return true;
6067 if (TREE_CODE (t) == CONSTRUCTOR)
6069 /* Aggregate initialization: stabilize each of the field
6070 initializers. */
6071 unsigned i;
6072 constructor_elt *ce;
6073 bool good = true;
6074 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
6075 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
6077 tree type = TREE_TYPE (ce->value);
6078 tree subinit;
6079 if (TYPE_REF_P (type)
6080 || SCALAR_TYPE_P (type))
6081 ce->value = stabilize_expr (ce->value, &subinit);
6082 else if (!stabilize_init (ce->value, &subinit))
6083 good = false;
6084 *initp = add_stmt_to_compound (*initp, subinit);
6086 return good;
6089 if (TREE_CODE (t) == CALL_EXPR)
6091 stabilize_call (t, initp);
6092 return true;
6095 if (TREE_CODE (t) == AGGR_INIT_EXPR)
6097 stabilize_aggr_init (t, initp);
6098 return true;
6101 /* The initialization is being performed via a bitwise copy -- and
6102 the item copied may have side effects. */
6103 return !TREE_SIDE_EFFECTS (init);
6106 /* Returns true if a cast to TYPE may appear in an integral constant
6107 expression. */
6109 bool
6110 cast_valid_in_integral_constant_expression_p (tree type)
6112 return (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6113 || cxx_dialect >= cxx11
6114 || dependent_type_p (type)
6115 || type == error_mark_node);
6118 /* Return true if we need to fix linkage information of DECL. */
6120 static bool
6121 cp_fix_function_decl_p (tree decl)
6123 /* Skip if DECL is not externally visible. */
6124 if (!TREE_PUBLIC (decl))
6125 return false;
6127 /* We need to fix DECL if it a appears to be exported but with no
6128 function body. Thunks do not have CFGs and we may need to
6129 handle them specially later. */
6130 if (!gimple_has_body_p (decl)
6131 && !DECL_THUNK_P (decl)
6132 && !DECL_EXTERNAL (decl))
6134 struct cgraph_node *node = cgraph_node::get (decl);
6136 /* Don't fix same_body aliases. Although they don't have their own
6137 CFG, they share it with what they alias to. */
6138 if (!node || !node->alias || !node->num_references ())
6139 return true;
6142 return false;
6145 /* Clean the C++ specific parts of the tree T. */
6147 void
6148 cp_free_lang_data (tree t)
6150 if (FUNC_OR_METHOD_TYPE_P (t))
6152 /* Default args are not interesting anymore. */
6153 tree argtypes = TYPE_ARG_TYPES (t);
6154 while (argtypes)
6156 TREE_PURPOSE (argtypes) = 0;
6157 argtypes = TREE_CHAIN (argtypes);
6160 else if (TREE_CODE (t) == FUNCTION_DECL
6161 && cp_fix_function_decl_p (t))
6163 /* If T is used in this translation unit at all, the definition
6164 must exist somewhere else since we have decided to not emit it
6165 in this TU. So make it an external reference. */
6166 DECL_EXTERNAL (t) = 1;
6167 TREE_STATIC (t) = 0;
6169 if (TREE_CODE (t) == NAMESPACE_DECL)
6170 /* We do not need the leftover chaining of namespaces from the
6171 binding level. */
6172 DECL_CHAIN (t) = NULL_TREE;
6175 /* Stub for c-common. Please keep in sync with c-decl.cc.
6176 FIXME: If address space support is target specific, then this
6177 should be a C target hook. But currently this is not possible,
6178 because this function is called via REGISTER_TARGET_PRAGMAS. */
6179 void
6180 c_register_addr_space (const char * /*word*/, addr_space_t /*as*/)
6184 /* Return the number of operands in T that we care about for things like
6185 mangling. */
6188 cp_tree_operand_length (const_tree t)
6190 enum tree_code code = TREE_CODE (t);
6192 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
6193 return VL_EXP_OPERAND_LENGTH (t);
6195 return cp_tree_code_length (code);
6198 /* Like cp_tree_operand_length, but takes a tree_code CODE. */
6201 cp_tree_code_length (enum tree_code code)
6203 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
6205 switch (code)
6207 case PREINCREMENT_EXPR:
6208 case PREDECREMENT_EXPR:
6209 case POSTINCREMENT_EXPR:
6210 case POSTDECREMENT_EXPR:
6211 return 1;
6213 case ARRAY_REF:
6214 return 2;
6216 case EXPR_PACK_EXPANSION:
6217 return 1;
6219 default:
6220 return TREE_CODE_LENGTH (code);
6224 /* Implement -Wzero_as_null_pointer_constant. Return true if the
6225 conditions for the warning hold, false otherwise. */
6226 bool
6227 maybe_warn_zero_as_null_pointer_constant (tree expr, location_t loc)
6229 if (c_inhibit_evaluation_warnings == 0
6230 && !null_node_p (expr) && !NULLPTR_TYPE_P (TREE_TYPE (expr)))
6232 warning_at (loc, OPT_Wzero_as_null_pointer_constant,
6233 "zero as null pointer constant");
6234 return true;
6236 return false;
6239 /* FNDECL is a function declaration whose type may have been altered by
6240 adding extra parameters such as this, in-charge, or VTT. When this
6241 takes place, the positional arguments supplied by the user (as in the
6242 'format' attribute arguments) may refer to the wrong argument. This
6243 function returns an integer indicating how many arguments should be
6244 skipped. */
6247 maybe_adjust_arg_pos_for_attribute (const_tree fndecl)
6249 if (!fndecl)
6250 return 0;
6251 int n = num_artificial_parms_for (fndecl);
6252 /* The manual states that it's the user's responsibility to account
6253 for the implicit this parameter. */
6254 return n > 0 ? n - 1 : 0;
6258 /* Release memory we no longer need after parsing. */
6259 void
6260 cp_tree_c_finish_parsing ()
6262 if (previous_class_level)
6263 invalidate_class_lookup_cache ();
6264 deleted_copy_types = NULL;
6267 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
6268 /* Complain that some language-specific thing hanging off a tree
6269 node has been accessed improperly. */
6271 void
6272 lang_check_failed (const char* file, int line, const char* function)
6274 internal_error ("%<lang_*%> check: failed in %s, at %s:%d",
6275 function, trim_filename (file), line);
6277 #endif /* ENABLE_TREE_CHECKING */
6279 #if CHECKING_P
6281 namespace selftest {
6283 /* Verify that lvalue_kind () works, for various expressions,
6284 and that location wrappers don't affect the results. */
6286 static void
6287 test_lvalue_kind ()
6289 location_t loc = BUILTINS_LOCATION;
6291 /* Verify constants and parameters, without and with
6292 location wrappers. */
6293 tree int_cst = build_int_cst (integer_type_node, 42);
6294 ASSERT_EQ (clk_none, lvalue_kind (int_cst));
6296 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
6297 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
6298 ASSERT_EQ (clk_none, lvalue_kind (wrapped_int_cst));
6300 tree string_lit = build_string (4, "foo");
6301 TREE_TYPE (string_lit) = char_array_type_node;
6302 string_lit = fix_string_type (string_lit);
6303 ASSERT_EQ (clk_ordinary, lvalue_kind (string_lit));
6305 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
6306 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
6307 ASSERT_EQ (clk_ordinary, lvalue_kind (wrapped_string_lit));
6309 tree parm = build_decl (UNKNOWN_LOCATION, PARM_DECL,
6310 get_identifier ("some_parm"),
6311 integer_type_node);
6312 ASSERT_EQ (clk_ordinary, lvalue_kind (parm));
6314 tree wrapped_parm = maybe_wrap_with_location (parm, loc);
6315 ASSERT_TRUE (location_wrapper_p (wrapped_parm));
6316 ASSERT_EQ (clk_ordinary, lvalue_kind (wrapped_parm));
6318 /* Verify that lvalue_kind of std::move on a parm isn't
6319 affected by location wrappers. */
6320 tree rvalue_ref_of_parm = move (parm);
6321 ASSERT_EQ (clk_rvalueref, lvalue_kind (rvalue_ref_of_parm));
6322 tree rvalue_ref_of_wrapped_parm = move (wrapped_parm);
6323 ASSERT_EQ (clk_rvalueref, lvalue_kind (rvalue_ref_of_wrapped_parm));
6325 /* Verify lvalue_p. */
6326 ASSERT_FALSE (lvalue_p (int_cst));
6327 ASSERT_FALSE (lvalue_p (wrapped_int_cst));
6328 ASSERT_TRUE (lvalue_p (parm));
6329 ASSERT_TRUE (lvalue_p (wrapped_parm));
6330 ASSERT_FALSE (lvalue_p (rvalue_ref_of_parm));
6331 ASSERT_FALSE (lvalue_p (rvalue_ref_of_wrapped_parm));
6334 /* Run all of the selftests within this file. */
6336 void
6337 cp_tree_cc_tests ()
6339 test_lvalue_kind ();
6342 } // namespace selftest
6344 #endif /* #if CHECKING_P */
6347 #include "gt-cp-tree.h"