LoongArch: Organize the code related to split move and merge the same functions.
[official-gcc.git] / gcc / cp / tree.cc
blob0e32d908b060892cd0c1b4d3c109060de8ee59dd
1 /* Language-dependent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2024 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 *);
50 static tree handle_no_dangling_attribute (tree *, tree, tree, int, bool *);
52 /* If REF is an lvalue, returns the kind of lvalue that REF is.
53 Otherwise, returns clk_none. */
55 cp_lvalue_kind
56 lvalue_kind (const_tree ref)
58 cp_lvalue_kind op1_lvalue_kind = clk_none;
59 cp_lvalue_kind op2_lvalue_kind = clk_none;
61 /* Expressions of reference type are sometimes wrapped in
62 INDIRECT_REFs. INDIRECT_REFs are just internal compiler
63 representation, not part of the language, so we have to look
64 through them. */
65 if (REFERENCE_REF_P (ref))
66 return lvalue_kind (TREE_OPERAND (ref, 0));
68 if (TREE_TYPE (ref)
69 && TYPE_REF_P (TREE_TYPE (ref)))
71 /* unnamed rvalue references are rvalues */
72 if (TYPE_REF_IS_RVALUE (TREE_TYPE (ref))
73 && TREE_CODE (ref) != PARM_DECL
74 && !VAR_P (ref)
75 && TREE_CODE (ref) != COMPONENT_REF
76 /* Functions are always lvalues. */
77 && TREE_CODE (TREE_TYPE (TREE_TYPE (ref))) != FUNCTION_TYPE)
79 op1_lvalue_kind = clk_rvalueref;
80 if (implicit_rvalue_p (ref))
81 op1_lvalue_kind |= clk_implicit_rval;
82 return op1_lvalue_kind;
85 /* lvalue references and named rvalue references are lvalues. */
86 return clk_ordinary;
89 if (ref == current_class_ptr)
90 return clk_none;
92 /* Expressions with cv void type are prvalues. */
93 if (TREE_TYPE (ref) && VOID_TYPE_P (TREE_TYPE (ref)))
94 return clk_none;
96 switch (TREE_CODE (ref))
98 case SAVE_EXPR:
99 return clk_none;
101 /* preincrements and predecrements are valid lvals, provided
102 what they refer to are valid lvals. */
103 case PREINCREMENT_EXPR:
104 case PREDECREMENT_EXPR:
105 case TRY_CATCH_EXPR:
106 case REALPART_EXPR:
107 case IMAGPART_EXPR:
108 case VIEW_CONVERT_EXPR:
109 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
110 /* As for ARRAY_REF and COMPONENT_REF, these codes turn a class prvalue
111 into an xvalue: we need to materialize the temporary before we mess
112 with it. Except VIEW_CONVERT_EXPR that doesn't actually change the
113 type, as in location wrapper and REF_PARENTHESIZED_P. */
114 if (op1_lvalue_kind == clk_class
115 && !(TREE_CODE (ref) == VIEW_CONVERT_EXPR
116 && (same_type_ignoring_top_level_qualifiers_p
117 (TREE_TYPE (ref), TREE_TYPE (TREE_OPERAND (ref, 0))))))
118 return clk_rvalueref;
119 return op1_lvalue_kind;
121 case ARRAY_REF:
123 tree op1 = TREE_OPERAND (ref, 0);
124 if (TREE_CODE (TREE_TYPE (op1)) == ARRAY_TYPE)
126 op1_lvalue_kind = lvalue_kind (op1);
127 if (op1_lvalue_kind == clk_class)
128 /* in the case of an array operand, the result is an lvalue if
129 that operand is an lvalue and an xvalue otherwise */
130 op1_lvalue_kind = clk_rvalueref;
131 return op1_lvalue_kind;
133 else
134 return clk_ordinary;
137 case MEMBER_REF:
138 case DOTSTAR_EXPR:
139 if (TREE_CODE (ref) == MEMBER_REF)
140 op1_lvalue_kind = clk_ordinary;
141 else
142 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
143 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
144 op1_lvalue_kind = clk_none;
145 else if (op1_lvalue_kind == clk_class)
146 /* The result of a .* expression whose second operand is a pointer to a
147 data member is an lvalue if the first operand is an lvalue and an
148 xvalue otherwise. */
149 op1_lvalue_kind = clk_rvalueref;
150 return op1_lvalue_kind;
152 case COMPONENT_REF:
153 if (BASELINK_P (TREE_OPERAND (ref, 1)))
155 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (ref, 1));
157 /* For static member function recurse on the BASELINK, we can get
158 here e.g. from reference_binding. If BASELINK_FUNCTIONS is
159 OVERLOAD, the overload is resolved first if possible through
160 resolve_address_of_overloaded_function. */
161 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_STATIC_FUNCTION_P (fn))
162 return lvalue_kind (TREE_OPERAND (ref, 1));
164 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
165 if (op1_lvalue_kind == clk_class)
166 /* If E1 is an lvalue, then E1.E2 is an lvalue;
167 otherwise E1.E2 is an xvalue. */
168 op1_lvalue_kind = clk_rvalueref;
170 /* Look at the member designator. */
171 if (!op1_lvalue_kind)
173 else if (is_overloaded_fn (TREE_OPERAND (ref, 1)))
174 /* The "field" can be a FUNCTION_DECL or an OVERLOAD in some
175 situations. If we're seeing a COMPONENT_REF, it's a non-static
176 member, so it isn't an lvalue. */
177 op1_lvalue_kind = clk_none;
178 else if (TREE_CODE (TREE_OPERAND (ref, 1)) != FIELD_DECL)
179 /* This can be IDENTIFIER_NODE in a template. */;
180 else if (DECL_C_BIT_FIELD (TREE_OPERAND (ref, 1)))
182 /* Clear the ordinary bit. If this object was a class
183 rvalue we want to preserve that information. */
184 op1_lvalue_kind &= ~clk_ordinary;
185 /* The lvalue is for a bitfield. */
186 op1_lvalue_kind |= clk_bitfield;
188 else if (DECL_PACKED (TREE_OPERAND (ref, 1)))
189 op1_lvalue_kind |= clk_packed;
191 return op1_lvalue_kind;
193 case STRING_CST:
194 case COMPOUND_LITERAL_EXPR:
195 return clk_ordinary;
197 case CONST_DECL:
198 /* CONST_DECL without TREE_STATIC are enumeration values and
199 thus not lvalues. With TREE_STATIC they are used by ObjC++
200 in objc_build_string_object and need to be considered as
201 lvalues. */
202 if (! TREE_STATIC (ref))
203 return clk_none;
204 /* FALLTHRU */
205 case VAR_DECL:
206 if (VAR_P (ref) && DECL_HAS_VALUE_EXPR_P (ref))
207 return lvalue_kind (DECL_VALUE_EXPR (CONST_CAST_TREE (ref)));
209 if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
210 && DECL_LANG_SPECIFIC (ref)
211 && DECL_IN_AGGR_P (ref))
212 return clk_none;
213 /* FALLTHRU */
214 case INDIRECT_REF:
215 case ARROW_EXPR:
216 case PARM_DECL:
217 case RESULT_DECL:
218 case PLACEHOLDER_EXPR:
219 return clk_ordinary;
221 /* A scope ref in a template, left as SCOPE_REF to support later
222 access checking. */
223 case SCOPE_REF:
224 gcc_assert (!type_dependent_expression_p (CONST_CAST_TREE (ref)));
226 tree op = TREE_OPERAND (ref, 1);
227 if (TREE_CODE (op) == FIELD_DECL)
228 return (DECL_C_BIT_FIELD (op) ? clk_bitfield : clk_ordinary);
229 else
230 return lvalue_kind (op);
233 case MAX_EXPR:
234 case MIN_EXPR:
235 /* Disallow <? and >? as lvalues if either argument side-effects. */
236 if (TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 0))
237 || TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 1)))
238 return clk_none;
239 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
240 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1));
241 break;
243 case COND_EXPR:
244 if (processing_template_decl)
246 /* Within templates, a REFERENCE_TYPE will indicate whether
247 the COND_EXPR result is an ordinary lvalue or rvalueref.
248 Since REFERENCE_TYPEs are handled above, if we reach this
249 point, we know we got a plain rvalue. Unless we have a
250 type-dependent expr, that is, but we shouldn't be testing
251 lvalueness if we can't even tell the types yet! */
252 gcc_assert (!type_dependent_expression_p (CONST_CAST_TREE (ref)));
253 goto default_;
256 tree op1 = TREE_OPERAND (ref, 1);
257 if (!op1) op1 = TREE_OPERAND (ref, 0);
258 tree op2 = TREE_OPERAND (ref, 2);
259 op1_lvalue_kind = lvalue_kind (op1);
260 op2_lvalue_kind = lvalue_kind (op2);
261 if (!op1_lvalue_kind != !op2_lvalue_kind)
263 /* The second or the third operand (but not both) is a
264 throw-expression; the result is of the type
265 and value category of the other. */
266 if (op1_lvalue_kind && TREE_CODE (op2) == THROW_EXPR)
267 op2_lvalue_kind = op1_lvalue_kind;
268 else if (op2_lvalue_kind && TREE_CODE (op1) == THROW_EXPR)
269 op1_lvalue_kind = op2_lvalue_kind;
272 break;
274 case MODOP_EXPR:
275 /* We expect to see unlowered MODOP_EXPRs only during
276 template processing. */
277 gcc_assert (processing_template_decl);
278 if (CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (ref, 0))))
279 goto default_;
280 else
281 return clk_ordinary;
283 case MODIFY_EXPR:
284 case TYPEID_EXPR:
285 return clk_ordinary;
287 case COMPOUND_EXPR:
288 return lvalue_kind (TREE_OPERAND (ref, 1));
290 case TARGET_EXPR:
291 return clk_class;
293 case VA_ARG_EXPR:
294 return (CLASS_TYPE_P (TREE_TYPE (ref)) ? clk_class : clk_none);
296 case CALL_EXPR:
297 /* We can see calls outside of TARGET_EXPR in templates. */
298 if (CLASS_TYPE_P (TREE_TYPE (ref)))
299 return clk_class;
300 return clk_none;
302 case FUNCTION_DECL:
303 /* All functions (except non-static-member functions) are
304 lvalues. */
305 return (DECL_IOBJ_MEMBER_FUNCTION_P (ref)
306 ? clk_none : clk_ordinary);
308 case BASELINK:
309 /* We now represent a reference to a single static member function
310 with a BASELINK. */
311 /* This CONST_CAST is okay because BASELINK_FUNCTIONS returns
312 its argument unmodified and we assign it to a const_tree. */
313 return lvalue_kind (BASELINK_FUNCTIONS (CONST_CAST_TREE (ref)));
315 case PAREN_EXPR:
316 return lvalue_kind (TREE_OPERAND (ref, 0));
318 case TEMPLATE_PARM_INDEX:
319 if (CLASS_TYPE_P (TREE_TYPE (ref)))
320 /* A template parameter object is an lvalue. */
321 return clk_ordinary;
322 return clk_none;
324 default:
325 default_:
326 if (!TREE_TYPE (ref))
327 return clk_none;
328 if (CLASS_TYPE_P (TREE_TYPE (ref))
329 || TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE)
330 return clk_class;
331 return clk_none;
334 /* If one operand is not an lvalue at all, then this expression is
335 not an lvalue. */
336 if (!op1_lvalue_kind || !op2_lvalue_kind)
337 return clk_none;
339 /* Otherwise, it's an lvalue, and it has all the odd properties
340 contributed by either operand. */
341 op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind;
342 /* It's not an ordinary lvalue if it involves any other kind. */
343 if ((op1_lvalue_kind & ~clk_ordinary) != clk_none)
344 op1_lvalue_kind &= ~clk_ordinary;
345 /* It can't be both a pseudo-lvalue and a non-addressable lvalue.
346 A COND_EXPR of those should be wrapped in a TARGET_EXPR. */
347 if ((op1_lvalue_kind & (clk_rvalueref|clk_class))
348 && (op1_lvalue_kind & (clk_bitfield|clk_packed)))
349 op1_lvalue_kind = clk_none;
350 return op1_lvalue_kind;
353 /* Returns the kind of lvalue that REF is, in the sense of [basic.lval]. */
355 cp_lvalue_kind
356 real_lvalue_p (const_tree ref)
358 cp_lvalue_kind kind = lvalue_kind (ref);
359 if (kind & (clk_rvalueref|clk_class))
360 return clk_none;
361 else
362 return kind;
365 /* c-common wants us to return bool. */
367 bool
368 lvalue_p (const_tree t)
370 return real_lvalue_p (t);
373 /* This differs from lvalue_p in that xvalues are included. */
375 bool
376 glvalue_p (const_tree ref)
378 cp_lvalue_kind kind = lvalue_kind (ref);
379 if (kind & clk_class)
380 return false;
381 else
382 return (kind != clk_none);
385 /* This differs from glvalue_p in that class prvalues are included. */
387 bool
388 obvalue_p (const_tree ref)
390 return (lvalue_kind (ref) != clk_none);
393 /* Returns true if REF is an xvalue (the result of dereferencing an rvalue
394 reference), false otherwise. */
396 bool
397 xvalue_p (const_tree ref)
399 return (lvalue_kind (ref) & clk_rvalueref);
402 /* True if REF is a bit-field. */
404 bool
405 bitfield_p (const_tree ref)
407 return (lvalue_kind (ref) & clk_bitfield);
410 /* C++-specific version of stabilize_reference. */
412 tree
413 cp_stabilize_reference (tree ref)
415 if (processing_template_decl)
416 /* As in cp_save_expr. */
417 return ref;
419 STRIP_ANY_LOCATION_WRAPPER (ref);
420 switch (TREE_CODE (ref))
422 /* We need to treat specially anything stabilize_reference doesn't
423 handle specifically. */
424 case VAR_DECL:
425 case PARM_DECL:
426 case RESULT_DECL:
427 CASE_CONVERT:
428 case FLOAT_EXPR:
429 case FIX_TRUNC_EXPR:
430 case INDIRECT_REF:
431 case COMPONENT_REF:
432 case BIT_FIELD_REF:
433 case ARRAY_REF:
434 case ARRAY_RANGE_REF:
435 case ERROR_MARK:
436 break;
437 default:
438 cp_lvalue_kind kind = lvalue_kind (ref);
439 if ((kind & ~clk_class) != clk_none)
441 tree type = unlowered_expr_type (ref);
442 bool rval = !!(kind & clk_rvalueref);
443 type = cp_build_reference_type (type, rval);
444 /* This inhibits warnings in, eg, cxx_mark_addressable
445 (c++/60955). */
446 warning_sentinel s (extra_warnings);
447 ref = build_static_cast (input_location, type, ref,
448 tf_error);
452 return stabilize_reference (ref);
455 /* Test whether DECL is a builtin that may appear in a
456 constant-expression. */
458 bool
459 builtin_valid_in_constant_expr_p (const_tree decl)
461 STRIP_ANY_LOCATION_WRAPPER (decl);
462 if (TREE_CODE (decl) != FUNCTION_DECL)
463 /* Not a function. */
464 return false;
465 if (DECL_BUILT_IN_CLASS (decl) != BUILT_IN_NORMAL)
467 if (fndecl_built_in_p (decl, BUILT_IN_FRONTEND))
468 switch (DECL_FE_FUNCTION_CODE (decl))
470 case CP_BUILT_IN_IS_CONSTANT_EVALUATED:
471 case CP_BUILT_IN_SOURCE_LOCATION:
472 case CP_BUILT_IN_IS_CORRESPONDING_MEMBER:
473 case CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS:
474 return true;
475 default:
476 break;
478 /* Not a built-in. */
479 return false;
481 switch (DECL_FUNCTION_CODE (decl))
483 /* These always have constant results like the corresponding
484 macros/symbol. */
485 case BUILT_IN_FILE:
486 case BUILT_IN_FUNCTION:
487 case BUILT_IN_LINE:
489 /* The following built-ins are valid in constant expressions
490 when their arguments are. */
491 case BUILT_IN_ADD_OVERFLOW_P:
492 case BUILT_IN_SUB_OVERFLOW_P:
493 case BUILT_IN_MUL_OVERFLOW_P:
495 /* These have constant results even if their operands are
496 non-constant. */
497 case BUILT_IN_CONSTANT_P:
498 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
499 return true;
500 default:
501 return false;
505 /* Build a TARGET_EXPR, initializing the DECL with the VALUE. */
507 static tree
508 build_target_expr (tree decl, tree value, tsubst_flags_t complain)
510 tree t;
511 tree type = TREE_TYPE (decl);
513 value = mark_rvalue_use (value);
515 gcc_checking_assert (VOID_TYPE_P (TREE_TYPE (value))
516 || TREE_TYPE (decl) == TREE_TYPE (value)
517 /* On ARM ctors return 'this'. */
518 || (TYPE_PTR_P (TREE_TYPE (value))
519 && TREE_CODE (value) == CALL_EXPR)
520 || useless_type_conversion_p (TREE_TYPE (decl),
521 TREE_TYPE (value)));
523 /* Set TREE_READONLY for optimization, such as gimplify_init_constructor
524 moving a constant aggregate into .rodata. */
525 if (CP_TYPE_CONST_NON_VOLATILE_P (type)
526 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
527 && !VOID_TYPE_P (TREE_TYPE (value))
528 && !TYPE_HAS_MUTABLE_P (type)
529 && reduced_constant_expression_p (value))
530 TREE_READONLY (decl) = true;
532 if (complain & tf_no_cleanup)
533 /* The caller is building a new-expr and does not need a cleanup. */
534 t = NULL_TREE;
535 else
537 t = cxx_maybe_build_cleanup (decl, complain);
538 if (t == error_mark_node)
539 return error_mark_node;
542 set_target_expr_eliding (value);
544 t = build4 (TARGET_EXPR, type, decl, value, t, NULL_TREE);
545 if (location_t eloc = cp_expr_location (value))
546 SET_EXPR_LOCATION (t, eloc);
547 /* We always set TREE_SIDE_EFFECTS so that expand_expr does not
548 ignore the TARGET_EXPR. If there really turn out to be no
549 side-effects, then the optimizer should be able to get rid of
550 whatever code is generated anyhow. */
551 TREE_SIDE_EFFECTS (t) = 1;
553 return t;
556 /* Return an undeclared local temporary of type TYPE for use in building a
557 TARGET_EXPR. */
559 tree
560 build_local_temp (tree type)
562 tree slot = build_decl (input_location,
563 VAR_DECL, NULL_TREE, type);
564 DECL_ARTIFICIAL (slot) = 1;
565 DECL_IGNORED_P (slot) = 1;
566 DECL_CONTEXT (slot) = current_function_decl;
567 layout_decl (slot, 0);
568 return slot;
571 /* Return whether DECL is such a local temporary (or one from
572 create_tmp_var_raw). */
574 bool
575 is_local_temp (tree decl)
577 return (VAR_P (decl) && DECL_ARTIFICIAL (decl)
578 && !TREE_STATIC (decl));
581 /* Set various status flags when building an AGGR_INIT_EXPR object T. */
583 static void
584 process_aggr_init_operands (tree t)
586 bool side_effects;
588 side_effects = TREE_SIDE_EFFECTS (t);
589 if (!side_effects)
591 int i, n;
592 n = TREE_OPERAND_LENGTH (t);
593 for (i = 1; i < n; i++)
595 tree op = TREE_OPERAND (t, i);
596 if (op && TREE_SIDE_EFFECTS (op))
598 side_effects = 1;
599 break;
603 TREE_SIDE_EFFECTS (t) = side_effects;
606 /* Build an AGGR_INIT_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE,
607 FN, and SLOT. NARGS is the number of call arguments which are specified
608 as a tree array ARGS. */
610 static tree
611 build_aggr_init_array (tree return_type, tree fn, tree slot, int nargs,
612 tree *args)
614 tree t;
615 int i;
617 t = build_vl_exp (AGGR_INIT_EXPR, nargs + 3);
618 TREE_TYPE (t) = return_type;
619 AGGR_INIT_EXPR_FN (t) = fn;
620 AGGR_INIT_EXPR_SLOT (t) = slot;
621 for (i = 0; i < nargs; i++)
622 AGGR_INIT_EXPR_ARG (t, i) = args[i];
623 process_aggr_init_operands (t);
624 return t;
627 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
628 target. TYPE is the type to be initialized.
630 Build an AGGR_INIT_EXPR to represent the initialization. This function
631 differs from build_cplus_new in that an AGGR_INIT_EXPR can only be used
632 to initialize another object, whereas a TARGET_EXPR can either
633 initialize another object or create its own temporary object, and as a
634 result building up a TARGET_EXPR requires that the type's destructor be
635 callable. */
637 tree
638 build_aggr_init_expr (tree type, tree init)
640 tree fn;
641 tree slot;
642 tree rval;
643 int is_ctor;
645 gcc_assert (!VOID_TYPE_P (type));
647 /* Don't build AGGR_INIT_EXPR in a template. */
648 if (processing_template_decl)
649 return init;
651 fn = cp_get_callee (init);
652 if (fn == NULL_TREE)
653 return convert (type, init);
655 is_ctor = (TREE_CODE (fn) == ADDR_EXPR
656 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
657 && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0)));
659 /* We split the CALL_EXPR into its function and its arguments here.
660 Then, in expand_expr, we put them back together. The reason for
661 this is that this expression might be a default argument
662 expression. In that case, we need a new temporary every time the
663 expression is used. That's what break_out_target_exprs does; it
664 replaces every AGGR_INIT_EXPR with a copy that uses a fresh
665 temporary slot. Then, expand_expr builds up a call-expression
666 using the new slot. */
668 /* If we don't need to use a constructor to create an object of this
669 type, don't mess with AGGR_INIT_EXPR. */
670 if (is_ctor || TREE_ADDRESSABLE (type))
672 slot = build_local_temp (type);
674 if (TREE_CODE (init) == CALL_EXPR)
676 rval = build_aggr_init_array (void_type_node, fn, slot,
677 call_expr_nargs (init),
678 CALL_EXPR_ARGP (init));
679 AGGR_INIT_FROM_THUNK_P (rval)
680 = CALL_FROM_THUNK_P (init);
682 else
684 rval = build_aggr_init_array (void_type_node, fn, slot,
685 aggr_init_expr_nargs (init),
686 AGGR_INIT_EXPR_ARGP (init));
687 AGGR_INIT_FROM_THUNK_P (rval)
688 = AGGR_INIT_FROM_THUNK_P (init);
690 TREE_SIDE_EFFECTS (rval) = 1;
691 AGGR_INIT_VIA_CTOR_P (rval) = is_ctor;
692 TREE_NOTHROW (rval) = TREE_NOTHROW (init);
693 CALL_EXPR_OPERATOR_SYNTAX (rval) = CALL_EXPR_OPERATOR_SYNTAX (init);
694 CALL_EXPR_ORDERED_ARGS (rval) = CALL_EXPR_ORDERED_ARGS (init);
695 CALL_EXPR_REVERSE_ARGS (rval) = CALL_EXPR_REVERSE_ARGS (init);
696 SET_EXPR_LOCATION (rval, EXPR_LOCATION (init));
698 else
699 rval = init;
701 return rval;
704 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
705 target. TYPE is the type that this initialization should appear to
706 have.
708 Build an encapsulation of the initialization to perform
709 and return it so that it can be processed by language-independent
710 and language-specific expression expanders. */
712 tree
713 build_cplus_new (tree type, tree init, tsubst_flags_t complain)
715 /* This function should cope with what build_special_member_call
716 can produce. When performing parenthesized aggregate initialization,
717 it can produce a { }. */
718 if (BRACE_ENCLOSED_INITIALIZER_P (init))
720 gcc_assert (cxx_dialect >= cxx20);
721 return finish_compound_literal (type, init, complain);
724 tree rval = build_aggr_init_expr (type, init);
725 tree slot;
727 if (init == error_mark_node)
728 return error_mark_node;
730 if (!complete_type_or_maybe_complain (type, init, complain))
731 return error_mark_node;
733 /* Make sure that we're not trying to create an instance of an
734 abstract class. */
735 if (abstract_virtuals_error (NULL_TREE, type, complain))
736 return error_mark_node;
738 if (TREE_CODE (rval) == AGGR_INIT_EXPR)
739 slot = AGGR_INIT_EXPR_SLOT (rval);
740 else if (TREE_CODE (rval) == CALL_EXPR
741 || TREE_CODE (rval) == CONSTRUCTOR)
742 slot = build_local_temp (type);
743 else
744 return rval;
746 rval = build_target_expr (slot, rval, complain);
748 if (rval != error_mark_node)
749 TARGET_EXPR_IMPLICIT_P (rval) = 1;
751 return rval;
754 /* Subroutine of build_vec_init_expr: Build up a single element
755 intialization as a proxy for the full array initialization to get things
756 marked as used and any appropriate diagnostics.
758 This used to be necessary because we were deferring building the actual
759 constructor calls until gimplification time; now we only do it to set
760 VEC_INIT_EXPR_IS_CONSTEXPR.
762 We assume that init is either NULL_TREE, {}, void_type_node (indicating
763 value-initialization), or another array to copy. */
765 static tree
766 build_vec_init_elt (tree type, tree init, tsubst_flags_t complain)
768 tree inner_type = strip_array_types (type);
770 if (integer_zerop (array_type_nelts_total (type))
771 || !CLASS_TYPE_P (inner_type))
772 /* No interesting initialization to do. */
773 return integer_zero_node;
774 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
776 /* Even if init has initializers for some array elements,
777 we're interested in the {}-init of trailing elements. */
778 if (CP_AGGREGATE_TYPE_P (inner_type))
780 tree empty = build_constructor (init_list_type_node, nullptr);
781 return digest_init (inner_type, empty, complain);
783 else
784 /* It's equivalent to value-init. */
785 init = void_type_node;
787 if (init == void_type_node)
788 return build_value_init (inner_type, complain);
790 releasing_vec argvec;
791 if (init && !BRACE_ENCLOSED_INITIALIZER_P (init))
793 tree init_type = strip_array_types (TREE_TYPE (init));
794 tree dummy = build_dummy_object (init_type);
795 if (!lvalue_p (init))
796 dummy = move (dummy);
797 argvec->quick_push (dummy);
799 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
800 &argvec, inner_type, LOOKUP_NORMAL,
801 complain);
803 /* For a trivial constructor, build_over_call creates a TARGET_EXPR. But
804 we don't want one here because we aren't creating a temporary. */
805 if (TREE_CODE (init) == TARGET_EXPR)
806 init = TARGET_EXPR_INITIAL (init);
808 return init;
811 /* Return a TARGET_EXPR which expresses the initialization of an array to
812 be named later, either default-initialization or copy-initialization
813 from another array of the same type. */
815 tree
816 build_vec_init_expr (tree type, tree init, tsubst_flags_t complain)
818 if (tree vi = get_vec_init_expr (init))
819 return vi;
821 tree elt_init;
822 if (init && TREE_CODE (init) == CONSTRUCTOR
823 && !BRACE_ENCLOSED_INITIALIZER_P (init))
824 /* We built any needed constructor calls in digest_init. */
825 elt_init = init;
826 else
827 elt_init = build_vec_init_elt (type, init, complain);
829 bool value_init = false;
830 if (init == void_type_node)
832 value_init = true;
833 init = NULL_TREE;
836 tree slot = build_local_temp (type);
837 init = build2 (VEC_INIT_EXPR, type, slot, init);
838 TREE_SIDE_EFFECTS (init) = true;
839 SET_EXPR_LOCATION (init, input_location);
841 if (cxx_dialect >= cxx11)
843 bool cx = potential_constant_expression (elt_init);
844 if (BRACE_ENCLOSED_INITIALIZER_P (init))
845 cx &= potential_constant_expression (init);
846 VEC_INIT_EXPR_IS_CONSTEXPR (init) = cx;
848 VEC_INIT_EXPR_VALUE_INIT (init) = value_init;
850 return init;
853 /* Call build_vec_init to expand VEC_INIT into TARGET (for which NULL_TREE
854 means VEC_INIT_EXPR_SLOT). */
856 tree
857 expand_vec_init_expr (tree target, tree vec_init, tsubst_flags_t complain,
858 vec<tree,va_gc> **flags)
860 iloc_sentinel ils = EXPR_LOCATION (vec_init);
862 if (!target)
863 target = VEC_INIT_EXPR_SLOT (vec_init);
864 tree init = VEC_INIT_EXPR_INIT (vec_init);
865 int from_array = (init && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE);
866 return build_vec_init (target, NULL_TREE, init,
867 VEC_INIT_EXPR_VALUE_INIT (vec_init),
868 from_array, complain, flags);
871 /* Give a helpful diagnostic for a non-constexpr VEC_INIT_EXPR in a context
872 that requires a constant expression. */
874 void
875 diagnose_non_constexpr_vec_init (tree expr)
877 tree type = TREE_TYPE (VEC_INIT_EXPR_SLOT (expr));
878 tree init, elt_init;
879 if (VEC_INIT_EXPR_VALUE_INIT (expr))
880 init = void_type_node;
881 else
882 init = VEC_INIT_EXPR_INIT (expr);
884 elt_init = build_vec_init_elt (type, init, tf_warning_or_error);
885 require_potential_constant_expression (elt_init);
888 tree
889 build_array_copy (tree init)
891 return get_target_expr (build_vec_init_expr
892 (TREE_TYPE (init), init, tf_warning_or_error));
895 /* Build a TARGET_EXPR using INIT to initialize a new temporary of the
896 indicated TYPE. */
898 tree
899 build_target_expr_with_type (tree init, tree type, tsubst_flags_t complain)
901 gcc_assert (!VOID_TYPE_P (type));
902 gcc_assert (!VOID_TYPE_P (TREE_TYPE (init)));
904 if (TREE_CODE (init) == TARGET_EXPR
905 || init == error_mark_node)
906 return init;
907 else if (CLASS_TYPE_P (type) && type_has_nontrivial_copy_init (type)
908 && TREE_CODE (init) != COND_EXPR
909 && TREE_CODE (init) != CONSTRUCTOR
910 && TREE_CODE (init) != VA_ARG_EXPR
911 && TREE_CODE (init) != CALL_EXPR)
912 /* We need to build up a copy constructor call. COND_EXPR is a special
913 case because we already have copies on the arms and we don't want
914 another one here. A CONSTRUCTOR is aggregate initialization, which
915 is handled separately. A VA_ARG_EXPR is magic creation of an
916 aggregate; there's no additional work to be done. A CALL_EXPR
917 already creates a prvalue. */
918 return force_rvalue (init, complain);
920 return force_target_expr (type, init, complain);
923 /* Like the above function, but without the checking. This function should
924 only be used by code which is deliberately trying to subvert the type
925 system, such as call_builtin_trap. Or build_over_call, to avoid
926 infinite recursion. */
928 tree
929 force_target_expr (tree type, tree init, tsubst_flags_t complain)
931 tree slot;
933 gcc_assert (!VOID_TYPE_P (type));
935 slot = build_local_temp (type);
936 return build_target_expr (slot, init, complain);
939 /* Like build_target_expr_with_type, but use the type of INIT. */
941 tree
942 get_target_expr (tree init, tsubst_flags_t complain /* = tf_warning_or_error */)
944 if (TREE_CODE (init) == AGGR_INIT_EXPR)
945 return build_target_expr (AGGR_INIT_EXPR_SLOT (init), init, complain);
946 else if (TREE_CODE (init) == VEC_INIT_EXPR)
947 return build_target_expr (VEC_INIT_EXPR_SLOT (init), init, complain);
948 else
950 init = convert_bitfield_to_declared_type (init);
951 return build_target_expr_with_type (init, TREE_TYPE (init), complain);
955 /* If EXPR is a bitfield reference, convert it to the declared type of
956 the bitfield, and return the resulting expression. Otherwise,
957 return EXPR itself. */
959 tree
960 convert_bitfield_to_declared_type (tree expr)
962 tree bitfield_type;
964 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
965 if (bitfield_type)
966 expr = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type),
967 expr);
968 return expr;
971 /* EXPR is being used in an rvalue context. Return a version of EXPR
972 that is marked as an rvalue. */
974 tree
975 rvalue (tree expr)
977 tree type;
979 if (error_operand_p (expr))
980 return expr;
982 expr = mark_rvalue_use (expr);
984 /* [expr.type]: "If a prvalue initially has the type "cv T", where T is a
985 cv-unqualified non-class, non-array type, the type of the expression is
986 adjusted to T prior to any further analysis. */
987 type = TREE_TYPE (expr);
988 if (!CLASS_TYPE_P (type) && TREE_CODE (type) != ARRAY_TYPE
989 && cv_qualified_p (type))
990 type = cv_unqualified (type);
992 /* We need to do this for rvalue refs as well to get the right answer
993 from decltype; see c++/36628. */
994 if (!processing_template_decl && glvalue_p (expr))
996 /* But don't use this function for class lvalues; use move (to treat an
997 lvalue as an xvalue) or force_rvalue (to make a prvalue copy). */
998 gcc_checking_assert (!CLASS_TYPE_P (type));
999 expr = build1 (NON_LVALUE_EXPR, type, expr);
1001 else if (type != TREE_TYPE (expr))
1002 expr = build_nop (type, expr);
1004 return expr;
1008 struct cplus_array_info
1010 tree type;
1011 tree domain;
1014 struct cplus_array_hasher : ggc_ptr_hash<tree_node>
1016 typedef cplus_array_info *compare_type;
1018 static hashval_t hash (tree t);
1019 static bool equal (tree, cplus_array_info *);
1022 /* Hash an ARRAY_TYPE. K is really of type `tree'. */
1024 hashval_t
1025 cplus_array_hasher::hash (tree t)
1027 hashval_t hash;
1029 hash = TYPE_UID (TREE_TYPE (t));
1030 if (TYPE_DOMAIN (t))
1031 hash ^= TYPE_UID (TYPE_DOMAIN (t));
1032 return hash;
1035 /* Compare two ARRAY_TYPEs. K1 is really of type `tree', K2 is really
1036 of type `cplus_array_info*'. */
1038 bool
1039 cplus_array_hasher::equal (tree t1, cplus_array_info *t2)
1041 return (TREE_TYPE (t1) == t2->type && TYPE_DOMAIN (t1) == t2->domain);
1044 /* Hash table containing dependent array types, which are unsuitable for
1045 the language-independent type hash table. */
1046 static GTY (()) hash_table<cplus_array_hasher> *cplus_array_htab;
1048 /* Build an ARRAY_TYPE without laying it out. */
1050 static tree
1051 build_min_array_type (tree elt_type, tree index_type)
1053 tree t = cxx_make_type (ARRAY_TYPE);
1054 TREE_TYPE (t) = elt_type;
1055 TYPE_DOMAIN (t) = index_type;
1056 return t;
1059 /* Set TYPE_CANONICAL like build_array_type_1, but using
1060 build_cplus_array_type. */
1062 static void
1063 set_array_type_canon (tree t, tree elt_type, tree index_type, bool dep)
1065 /* Set the canonical type for this new node. */
1066 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
1067 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
1068 SET_TYPE_STRUCTURAL_EQUALITY (t);
1069 else if (TYPE_CANONICAL (elt_type) != elt_type
1070 || (index_type && TYPE_CANONICAL (index_type) != index_type))
1071 TYPE_CANONICAL (t)
1072 = build_cplus_array_type (TYPE_CANONICAL (elt_type),
1073 index_type
1074 ? TYPE_CANONICAL (index_type) : index_type,
1075 dep);
1076 else
1077 TYPE_CANONICAL (t) = t;
1080 /* Like build_array_type, but handle special C++ semantics: an array of a
1081 variant element type is a variant of the array of the main variant of
1082 the element type. IS_DEPENDENT is -ve if we should determine the
1083 dependency. Otherwise its bool value indicates dependency. */
1085 tree
1086 build_cplus_array_type (tree elt_type, tree index_type, int dependent)
1088 tree t;
1090 if (elt_type == error_mark_node || index_type == error_mark_node)
1091 return error_mark_node;
1093 if (dependent < 0)
1094 dependent = (uses_template_parms (elt_type)
1095 || (index_type && uses_template_parms (index_type)));
1097 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
1098 /* Start with an array of the TYPE_MAIN_VARIANT. */
1099 t = build_cplus_array_type (TYPE_MAIN_VARIANT (elt_type),
1100 index_type, dependent);
1101 else if (dependent)
1103 /* Since type_hash_canon calls layout_type, we need to use our own
1104 hash table. */
1105 cplus_array_info cai;
1106 hashval_t hash;
1108 if (cplus_array_htab == NULL)
1109 cplus_array_htab = hash_table<cplus_array_hasher>::create_ggc (61);
1111 hash = TYPE_UID (elt_type);
1112 if (index_type)
1113 hash ^= TYPE_UID (index_type);
1114 cai.type = elt_type;
1115 cai.domain = index_type;
1117 tree *e = cplus_array_htab->find_slot_with_hash (&cai, hash, INSERT);
1118 if (*e)
1119 /* We have found the type: we're done. */
1120 return (tree) *e;
1121 else
1123 /* Build a new array type. */
1124 t = build_min_array_type (elt_type, index_type);
1126 /* Store it in the hash table. */
1127 *e = t;
1129 /* Set the canonical type for this new node. */
1130 set_array_type_canon (t, elt_type, index_type, dependent);
1132 /* Mark it as dependent now, this saves time later. */
1133 TYPE_DEPENDENT_P_VALID (t) = true;
1134 TYPE_DEPENDENT_P (t) = true;
1137 else
1139 bool typeless_storage = is_byte_access_type (elt_type);
1140 t = build_array_type (elt_type, index_type, typeless_storage);
1142 /* Mark as non-dependenty now, this will save time later. */
1143 TYPE_DEPENDENT_P_VALID (t) = true;
1146 /* Now check whether we already have this array variant. */
1147 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
1149 tree m = t;
1150 for (t = m; t; t = TYPE_NEXT_VARIANT (t))
1151 if (TREE_TYPE (t) == elt_type
1152 && TYPE_NAME (t) == NULL_TREE
1153 && TYPE_ATTRIBUTES (t) == NULL_TREE)
1154 break;
1155 if (!t)
1157 t = build_min_array_type (elt_type, index_type);
1158 /* Mark dependency now, this saves time later. */
1159 TYPE_DEPENDENT_P_VALID (t) = true;
1160 TYPE_DEPENDENT_P (t) = dependent;
1161 set_array_type_canon (t, elt_type, index_type, dependent);
1162 if (!dependent)
1164 layout_type (t);
1165 /* Make sure sizes are shared with the main variant.
1166 layout_type can't be called after setting TYPE_NEXT_VARIANT,
1167 as it will overwrite alignment etc. of all variants. */
1168 TYPE_SIZE (t) = TYPE_SIZE (m);
1169 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (m);
1170 TYPE_TYPELESS_STORAGE (t) = TYPE_TYPELESS_STORAGE (m);
1173 TYPE_MAIN_VARIANT (t) = m;
1174 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
1175 TYPE_NEXT_VARIANT (m) = t;
1179 /* Avoid spurious warnings with VLAs (c++/54583). */
1180 if (TYPE_SIZE (t) && EXPR_P (TYPE_SIZE (t)))
1181 suppress_warning (TYPE_SIZE (t), OPT_Wunused);
1183 /* Push these needs up to the ARRAY_TYPE so that initialization takes
1184 place more easily. */
1185 bool needs_ctor = (TYPE_NEEDS_CONSTRUCTING (t)
1186 = TYPE_NEEDS_CONSTRUCTING (elt_type));
1187 bool needs_dtor = (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1188 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type));
1190 if (!dependent && t == TYPE_MAIN_VARIANT (t)
1191 && !COMPLETE_TYPE_P (t) && COMPLETE_TYPE_P (elt_type))
1193 /* The element type has been completed since the last time we saw
1194 this array type; update the layout and 'tor flags for any variants
1195 that need it. */
1196 layout_type (t);
1197 for (tree v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1199 TYPE_NEEDS_CONSTRUCTING (v) = needs_ctor;
1200 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (v) = needs_dtor;
1204 return t;
1207 /* Return an ARRAY_TYPE with element type ELT and length N. */
1209 tree
1210 build_array_of_n_type (tree elt, int n)
1212 return build_cplus_array_type (elt, build_index_type (size_int (n - 1)));
1215 /* True iff T is an array of unknown bound. */
1217 bool
1218 array_of_unknown_bound_p (const_tree t)
1220 return (TREE_CODE (t) == ARRAY_TYPE
1221 && !TYPE_DOMAIN (t));
1224 /* True iff T is an N3639 array of runtime bound (VLA). These were approved
1225 for C++14 but then removed. This should only be used for N3639
1226 specifically; code wondering more generally if something is a VLA should use
1227 vla_type_p. */
1229 bool
1230 array_of_runtime_bound_p (tree t)
1232 if (!t || TREE_CODE (t) != ARRAY_TYPE)
1233 return false;
1234 if (variably_modified_type_p (TREE_TYPE (t), NULL_TREE))
1235 return false;
1236 tree dom = TYPE_DOMAIN (t);
1237 if (!dom)
1238 return false;
1239 tree max = TYPE_MAX_VALUE (dom);
1240 return (!potential_rvalue_constant_expression (max)
1241 || (!value_dependent_expression_p (max) && !TREE_CONSTANT (max)));
1244 /* True iff T is a variable length array. */
1246 bool
1247 vla_type_p (tree t)
1249 for (; t && TREE_CODE (t) == ARRAY_TYPE;
1250 t = TREE_TYPE (t))
1251 if (tree dom = TYPE_DOMAIN (t))
1253 tree max = TYPE_MAX_VALUE (dom);
1254 if (!potential_rvalue_constant_expression (max)
1255 || (!value_dependent_expression_p (max) && !TREE_CONSTANT (max)))
1256 return true;
1258 return false;
1262 /* Return a reference type node of MODE referring to TO_TYPE. If MODE
1263 is VOIDmode the standard pointer mode will be picked. If RVAL is
1264 true, return an rvalue reference type, otherwise return an lvalue
1265 reference type. If a type node exists, reuse it, otherwise create
1266 a new one. */
1267 tree
1268 cp_build_reference_type_for_mode (tree to_type, machine_mode mode, bool rval)
1270 tree lvalue_ref, t;
1272 if (to_type == error_mark_node)
1273 return error_mark_node;
1275 if (TYPE_REF_P (to_type))
1277 rval = rval && TYPE_REF_IS_RVALUE (to_type);
1278 to_type = TREE_TYPE (to_type);
1281 lvalue_ref = build_reference_type_for_mode (to_type, mode, false);
1283 if (!rval)
1284 return lvalue_ref;
1286 /* This code to create rvalue reference types is based on and tied
1287 to the code creating lvalue reference types in the middle-end
1288 functions build_reference_type_for_mode and build_reference_type.
1290 It works by putting the rvalue reference type nodes after the
1291 lvalue reference nodes in the TYPE_NEXT_REF_TO linked list, so
1292 they will effectively be ignored by the middle end. */
1294 for (t = lvalue_ref; (t = TYPE_NEXT_REF_TO (t)); )
1295 if (TYPE_REF_IS_RVALUE (t))
1296 return t;
1298 t = build_distinct_type_copy (lvalue_ref);
1300 TYPE_REF_IS_RVALUE (t) = true;
1301 TYPE_NEXT_REF_TO (t) = TYPE_NEXT_REF_TO (lvalue_ref);
1302 TYPE_NEXT_REF_TO (lvalue_ref) = t;
1304 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
1305 SET_TYPE_STRUCTURAL_EQUALITY (t);
1306 else if (TYPE_CANONICAL (to_type) != to_type)
1307 TYPE_CANONICAL (t)
1308 = cp_build_reference_type_for_mode (TYPE_CANONICAL (to_type), mode, rval);
1309 else
1310 TYPE_CANONICAL (t) = t;
1312 layout_type (t);
1314 return t;
1318 /* Return a reference type node referring to TO_TYPE. If RVAL is
1319 true, return an rvalue reference type, otherwise return an lvalue
1320 reference type. If a type node exists, reuse it, otherwise create
1321 a new one. */
1322 tree
1323 cp_build_reference_type (tree to_type, bool rval)
1325 return cp_build_reference_type_for_mode (to_type, VOIDmode, rval);
1328 /* Returns EXPR cast to rvalue reference type, like std::move. */
1330 tree
1331 move (tree expr)
1333 tree type = TREE_TYPE (expr);
1334 gcc_assert (!TYPE_REF_P (type));
1335 if (xvalue_p (expr))
1336 return expr;
1337 type = cp_build_reference_type (type, /*rval*/true);
1338 return build_static_cast (input_location, type, expr,
1339 tf_warning_or_error);
1342 /* Used by the C++ front end to build qualified array types. However,
1343 the C version of this function does not properly maintain canonical
1344 types (which are not used in C). */
1345 tree
1346 c_build_qualified_type (tree type, int type_quals, tree /* orig_qual_type */,
1347 size_t /* orig_qual_indirect */)
1349 return cp_build_qualified_type (type, type_quals);
1353 /* Make a variant of TYPE, qualified with the TYPE_QUALS. Handles
1354 arrays correctly. In particular, if TYPE is an array of T's, and
1355 TYPE_QUALS is non-empty, returns an array of qualified T's.
1357 FLAGS determines how to deal with ill-formed qualifications. If
1358 tf_ignore_bad_quals is set, then bad qualifications are dropped
1359 (this is permitted if TYPE was introduced via a typedef or template
1360 type parameter). If bad qualifications are dropped and tf_warning
1361 is set, then a warning is issued for non-const qualifications. If
1362 tf_ignore_bad_quals is not set and tf_error is not set, we
1363 return error_mark_node. Otherwise, we issue an error, and ignore
1364 the qualifications.
1366 Qualification of a reference type is valid when the reference came
1367 via a typedef or template type argument. [dcl.ref] No such
1368 dispensation is provided for qualifying a function type. [dcl.fct]
1369 DR 295 queries this and the proposed resolution brings it into line
1370 with qualifying a reference. We implement the DR. We also behave
1371 in a similar manner for restricting non-pointer types. */
1373 tree
1374 cp_build_qualified_type (tree type, int type_quals,
1375 tsubst_flags_t complain /* = tf_warning_or_error */)
1377 tree result;
1378 int bad_quals = TYPE_UNQUALIFIED;
1380 if (type == error_mark_node)
1381 return type;
1383 if (type_quals == cp_type_quals (type))
1384 return type;
1386 if (TREE_CODE (type) == ARRAY_TYPE)
1388 /* In C++, the qualification really applies to the array element
1389 type. Obtain the appropriately qualified element type. */
1390 tree t;
1391 tree element_type
1392 = cp_build_qualified_type (TREE_TYPE (type), type_quals, complain);
1394 if (element_type == error_mark_node)
1395 return error_mark_node;
1397 /* See if we already have an identically qualified type. Tests
1398 should be equivalent to those in check_qualified_type. */
1399 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1400 if (TREE_TYPE (t) == element_type
1401 && TYPE_NAME (t) == TYPE_NAME (type)
1402 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
1403 && attribute_list_equal (TYPE_ATTRIBUTES (t),
1404 TYPE_ATTRIBUTES (type)))
1405 break;
1407 if (!t)
1409 /* If we already know the dependentness, tell the array type
1410 constructor. This is important for module streaming, as we cannot
1411 dynamically determine that on read in. */
1412 t = build_cplus_array_type (element_type, TYPE_DOMAIN (type),
1413 TYPE_DEPENDENT_P_VALID (type)
1414 ? int (TYPE_DEPENDENT_P (type)) : -1);
1416 /* Keep the typedef name. */
1417 if (TYPE_NAME (t) != TYPE_NAME (type))
1419 t = build_variant_type_copy (t);
1420 TYPE_NAME (t) = TYPE_NAME (type);
1421 SET_TYPE_ALIGN (t, TYPE_ALIGN (type));
1422 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (type);
1426 /* Even if we already had this variant, we update
1427 TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case
1428 they changed since the variant was originally created.
1430 This seems hokey; if there is some way to use a previous
1431 variant *without* coming through here,
1432 TYPE_NEEDS_CONSTRUCTING will never be updated. */
1433 TYPE_NEEDS_CONSTRUCTING (t)
1434 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
1435 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1436 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
1437 return t;
1439 else if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
1441 tree t = PACK_EXPANSION_PATTERN (type);
1443 t = cp_build_qualified_type (t, type_quals, complain);
1444 return make_pack_expansion (t, complain);
1447 /* A reference or method type shall not be cv-qualified.
1448 [dcl.ref], [dcl.fct]. This used to be an error, but as of DR 295
1449 (in CD1) we always ignore extra cv-quals on functions. */
1451 /* [dcl.ref/1] Cv-qualified references are ill-formed except when
1452 the cv-qualifiers are introduced through the use of a typedef-name
1453 ([dcl.typedef], [temp.param]) or decltype-specifier
1454 ([dcl.type.decltype]),in which case the cv-qualifiers are
1455 ignored. */
1456 if (type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)
1457 && (TYPE_REF_P (type)
1458 || FUNC_OR_METHOD_TYPE_P (type)))
1460 if (TYPE_REF_P (type)
1461 && (!typedef_variant_p (type) || FUNC_OR_METHOD_TYPE_P (type)))
1462 bad_quals |= type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1463 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1466 /* But preserve any function-cv-quals on a FUNCTION_TYPE. */
1467 if (TREE_CODE (type) == FUNCTION_TYPE)
1468 type_quals |= type_memfn_quals (type);
1470 /* A restrict-qualified type must be a pointer (or reference)
1471 to object or incomplete type. */
1472 if ((type_quals & TYPE_QUAL_RESTRICT)
1473 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1474 && TREE_CODE (type) != TYPENAME_TYPE
1475 && !INDIRECT_TYPE_P (type))
1477 bad_quals |= TYPE_QUAL_RESTRICT;
1478 type_quals &= ~TYPE_QUAL_RESTRICT;
1481 if (bad_quals == TYPE_UNQUALIFIED
1482 || (complain & tf_ignore_bad_quals))
1483 /*OK*/;
1484 else if (!(complain & tf_error))
1485 return error_mark_node;
1486 else
1488 tree bad_type = build_qualified_type (ptr_type_node, bad_quals);
1489 error ("%qV qualifiers cannot be applied to %qT",
1490 bad_type, type);
1493 /* Retrieve (or create) the appropriately qualified variant. */
1494 result = build_qualified_type (type, type_quals);
1496 return result;
1499 /* Return TYPE with const and volatile removed. */
1501 tree
1502 cv_unqualified (tree type)
1504 int quals;
1506 if (type == error_mark_node)
1507 return type;
1509 quals = cp_type_quals (type);
1510 quals &= ~(TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE);
1511 return cp_build_qualified_type (type, quals);
1514 /* Subroutine of strip_typedefs. We want to apply to RESULT the attributes
1515 from ATTRIBS that affect type identity, and no others. If any are not
1516 applied, set *remove_attributes to true. */
1518 static tree
1519 apply_identity_attributes (tree result, tree attribs, bool *remove_attributes)
1521 tree first_ident = NULL_TREE;
1522 tree new_attribs = NULL_TREE;
1523 tree *p = &new_attribs;
1525 if (OVERLOAD_TYPE_P (result))
1527 /* On classes and enums all attributes are ingrained. */
1528 gcc_assert (attribs == TYPE_ATTRIBUTES (result));
1529 return result;
1532 for (tree a = attribs; a; a = TREE_CHAIN (a))
1534 const attribute_spec *as
1535 = lookup_attribute_spec (get_attribute_name (a));
1536 if (as && as->affects_type_identity)
1538 if (!first_ident)
1539 first_ident = a;
1540 else if (first_ident == error_mark_node)
1542 *p = tree_cons (TREE_PURPOSE (a), TREE_VALUE (a), NULL_TREE);
1543 p = &TREE_CHAIN (*p);
1546 else if (first_ident && first_ident != error_mark_node)
1548 for (tree a2 = first_ident; a2 != a; a2 = TREE_CHAIN (a2))
1550 *p = tree_cons (TREE_PURPOSE (a2), TREE_VALUE (a2), NULL_TREE);
1551 p = &TREE_CHAIN (*p);
1553 first_ident = error_mark_node;
1556 if (first_ident != error_mark_node)
1557 new_attribs = first_ident;
1559 if (first_ident == attribs)
1560 /* All attributes affected type identity. */;
1561 else
1562 *remove_attributes = true;
1564 return cp_build_type_attribute_variant (result, new_attribs);
1567 /* Builds a qualified variant of T that is either not a typedef variant
1568 (the default behavior) or not a typedef variant of a user-facing type
1569 (if FLAGS contains STF_USER_FACING). If T is not a type, then this
1570 just dispatches to strip_typedefs_expr.
1572 E.g. consider the following declarations:
1573 typedef const int ConstInt;
1574 typedef ConstInt* PtrConstInt;
1575 If T is PtrConstInt, this function returns a type representing
1576 const int*.
1577 In other words, if T is a typedef, the function returns the underlying type.
1578 The cv-qualification and attributes of the type returned match the
1579 input type.
1580 They will always be compatible types.
1581 The returned type is built so that all of its subtypes
1582 recursively have their typedefs stripped as well.
1584 This is different from just returning TYPE_CANONICAL (T)
1585 Because of several reasons:
1586 * If T is a type that needs structural equality
1587 its TYPE_CANONICAL (T) will be NULL.
1588 * TYPE_CANONICAL (T) desn't carry type attributes
1589 and loses template parameter names.
1591 If REMOVE_ATTRIBUTES is non-null, also strip attributes that don't
1592 affect type identity, and set the referent to true if any were
1593 stripped. */
1595 tree
1596 strip_typedefs (tree t, bool *remove_attributes /* = NULL */,
1597 unsigned int flags /* = 0 */)
1599 tree result = NULL, type = NULL, t0 = NULL;
1601 if (!t || t == error_mark_node)
1602 return t;
1604 if (!TYPE_P (t))
1605 return strip_typedefs_expr (t, remove_attributes, flags);
1607 if (t == TYPE_CANONICAL (t))
1608 return t;
1610 if (!(flags & STF_STRIP_DEPENDENT)
1611 && dependent_alias_template_spec_p (t, nt_opaque))
1612 /* DR 1558: However, if the template-id is dependent, subsequent
1613 template argument substitution still applies to the template-id. */
1614 return t;
1616 switch (TREE_CODE (t))
1618 case POINTER_TYPE:
1619 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1620 result = build_pointer_type_for_mode (type, TYPE_MODE (t), false);
1621 break;
1622 case REFERENCE_TYPE:
1623 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1624 result = cp_build_reference_type_for_mode (type, TYPE_MODE (t), TYPE_REF_IS_RVALUE (t));
1625 break;
1626 case OFFSET_TYPE:
1627 t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t), remove_attributes, flags);
1628 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1629 result = build_offset_type (t0, type);
1630 break;
1631 case RECORD_TYPE:
1632 if (TYPE_PTRMEMFUNC_P (t))
1634 t0 = strip_typedefs (TYPE_PTRMEMFUNC_FN_TYPE (t),
1635 remove_attributes, flags);
1636 result = build_ptrmemfunc_type (t0);
1638 break;
1639 case ARRAY_TYPE:
1640 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1641 t0 = strip_typedefs (TYPE_DOMAIN (t), remove_attributes, flags);
1642 gcc_checking_assert (TYPE_DEPENDENT_P_VALID (t)
1643 || !dependent_type_p (t));
1644 result = build_cplus_array_type (type, t0, TYPE_DEPENDENT_P (t));
1645 break;
1646 case FUNCTION_TYPE:
1647 case METHOD_TYPE:
1649 tree arg_types = NULL, arg_node, arg_node2, arg_type;
1650 bool changed;
1652 /* Because we stomp on TREE_PURPOSE of TYPE_ARG_TYPES in many places
1653 around the compiler (e.g. cp_parser_late_parsing_default_args), we
1654 can't expect that re-hashing a function type will find a previous
1655 equivalent type, so try to reuse the input type if nothing has
1656 changed. If the type is itself a variant, that will change. */
1657 bool is_variant = typedef_variant_p (t);
1658 if (remove_attributes
1659 && (TYPE_ATTRIBUTES (t) || TYPE_USER_ALIGN (t)))
1660 is_variant = true;
1662 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1663 tree canon_spec = (flag_noexcept_type
1664 ? canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (t))
1665 : NULL_TREE);
1666 changed = (type != TREE_TYPE (t) || is_variant
1667 || TYPE_RAISES_EXCEPTIONS (t) != canon_spec);
1669 for (arg_node = TYPE_ARG_TYPES (t);
1670 arg_node;
1671 arg_node = TREE_CHAIN (arg_node))
1673 if (arg_node == void_list_node)
1674 break;
1675 arg_type = strip_typedefs (TREE_VALUE (arg_node),
1676 remove_attributes, flags);
1677 gcc_assert (arg_type);
1678 if (arg_type == TREE_VALUE (arg_node) && !changed)
1679 continue;
1681 if (!changed)
1683 changed = true;
1684 for (arg_node2 = TYPE_ARG_TYPES (t);
1685 arg_node2 != arg_node;
1686 arg_node2 = TREE_CHAIN (arg_node2))
1687 arg_types
1688 = tree_cons (TREE_PURPOSE (arg_node2),
1689 TREE_VALUE (arg_node2), arg_types);
1692 arg_types
1693 = tree_cons (TREE_PURPOSE (arg_node), arg_type, arg_types);
1696 if (!changed)
1697 return t;
1699 if (arg_types)
1700 arg_types = nreverse (arg_types);
1702 /* A list of parameters not ending with an ellipsis
1703 must end with void_list_node. */
1704 if (arg_node)
1705 arg_types = chainon (arg_types, void_list_node);
1707 if (TREE_CODE (t) == METHOD_TYPE)
1709 tree class_type = TREE_TYPE (TREE_VALUE (arg_types));
1710 gcc_assert (class_type);
1711 result =
1712 build_method_type_directly (class_type, type,
1713 TREE_CHAIN (arg_types));
1715 else
1717 result = build_function_type (type, arg_types);
1718 result = apply_memfn_quals (result, type_memfn_quals (t));
1721 result = build_cp_fntype_variant (result,
1722 type_memfn_rqual (t), canon_spec,
1723 TYPE_HAS_LATE_RETURN_TYPE (t));
1725 break;
1726 case TYPENAME_TYPE:
1728 bool changed = false;
1729 tree fullname = TYPENAME_TYPE_FULLNAME (t);
1730 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
1731 && TREE_OPERAND (fullname, 1))
1733 tree args = TREE_OPERAND (fullname, 1);
1734 tree new_args = copy_node (args);
1735 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
1737 tree arg = TREE_VEC_ELT (args, i);
1738 tree strip_arg = strip_typedefs (arg, remove_attributes, flags);
1739 TREE_VEC_ELT (new_args, i) = strip_arg;
1740 if (strip_arg != arg)
1741 changed = true;
1743 if (changed)
1745 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_args)
1746 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
1747 fullname
1748 = lookup_template_function (TREE_OPERAND (fullname, 0),
1749 new_args);
1751 else
1752 ggc_free (new_args);
1754 tree ctx = strip_typedefs (TYPE_CONTEXT (t), remove_attributes, flags);
1755 if (!changed && ctx == TYPE_CONTEXT (t) && !typedef_variant_p (t))
1756 return t;
1757 tree name = fullname;
1758 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
1759 name = TREE_OPERAND (fullname, 0);
1760 /* Use build_typename_type rather than make_typename_type because we
1761 don't want to resolve it here, just strip typedefs. */
1762 result = build_typename_type (ctx, name, fullname, typename_type);
1764 break;
1765 case DECLTYPE_TYPE:
1766 result = strip_typedefs_expr (DECLTYPE_TYPE_EXPR (t),
1767 remove_attributes, flags);
1768 if (result == DECLTYPE_TYPE_EXPR (t))
1769 result = NULL_TREE;
1770 else
1771 result = (finish_decltype_type
1772 (result,
1773 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t),
1774 tf_none));
1775 break;
1776 case TRAIT_TYPE:
1778 tree type1 = strip_typedefs (TRAIT_TYPE_TYPE1 (t),
1779 remove_attributes, flags);
1780 tree type2 = strip_typedefs (TRAIT_TYPE_TYPE2 (t),
1781 remove_attributes, flags);
1782 if (type1 == TRAIT_TYPE_TYPE1 (t) && type2 == TRAIT_TYPE_TYPE2 (t))
1783 result = NULL_TREE;
1784 else
1785 result = finish_trait_type (TRAIT_TYPE_KIND (t), type1, type2,
1786 tf_warning_or_error);
1788 break;
1789 case TYPE_PACK_EXPANSION:
1791 tree pat = PACK_EXPANSION_PATTERN (t);
1792 if (TYPE_P (pat))
1794 type = strip_typedefs (pat, remove_attributes, flags);
1795 if (type != pat)
1797 result = build_distinct_type_copy (t);
1798 PACK_EXPANSION_PATTERN (result) = type;
1802 break;
1803 default:
1804 break;
1807 if (!result)
1809 if (typedef_variant_p (t))
1811 if ((flags & STF_USER_VISIBLE)
1812 && !user_facing_original_type_p (t))
1813 return t;
1814 /* If T is a non-template alias or typedef, we can assume that
1815 instantiating its definition will hit any substitution failure,
1816 so we don't need to retain it here as well. */
1817 if (!alias_template_specialization_p (t, nt_opaque))
1818 flags |= STF_STRIP_DEPENDENT;
1819 result = strip_typedefs (DECL_ORIGINAL_TYPE (TYPE_NAME (t)),
1820 remove_attributes, flags);
1822 else
1823 result = TYPE_MAIN_VARIANT (t);
1825 /*gcc_assert (!typedef_variant_p (result)
1826 || dependent_alias_template_spec_p (result, nt_opaque)
1827 || ((flags & STF_USER_VISIBLE)
1828 && !user_facing_original_type_p (result)));*/
1830 if (COMPLETE_TYPE_P (result) && !COMPLETE_TYPE_P (t))
1831 /* If RESULT is complete and T isn't, it's likely the case that T
1832 is a variant of RESULT which hasn't been updated yet. Skip the
1833 attribute handling. */;
1834 else
1836 if (TYPE_USER_ALIGN (t) != TYPE_USER_ALIGN (result)
1837 || TYPE_ALIGN (t) != TYPE_ALIGN (result))
1839 gcc_assert (TYPE_USER_ALIGN (t));
1840 if (remove_attributes)
1841 *remove_attributes = true;
1842 else
1844 if (TYPE_ALIGN (t) == TYPE_ALIGN (result))
1845 result = build_variant_type_copy (result);
1846 else
1847 result = build_aligned_type (result, TYPE_ALIGN (t));
1848 TYPE_USER_ALIGN (result) = true;
1852 if (TYPE_ATTRIBUTES (t))
1854 if (remove_attributes)
1855 result = apply_identity_attributes (result, TYPE_ATTRIBUTES (t),
1856 remove_attributes);
1857 else
1858 result = cp_build_type_attribute_variant (result,
1859 TYPE_ATTRIBUTES (t));
1863 return cp_build_qualified_type (result, cp_type_quals (t));
1866 /* Like strip_typedefs above, but works on expressions (and other
1867 non-types such as TREE_VEC), so that in
1869 template<class T> struct A
1871 typedef T TT;
1872 B<sizeof(TT)> b;
1875 sizeof(TT) is replaced by sizeof(T). */
1877 tree
1878 strip_typedefs_expr (tree t, bool *remove_attributes, unsigned int flags)
1880 unsigned i,n;
1881 tree r, type, *ops;
1882 enum tree_code code;
1884 if (t == NULL_TREE || t == error_mark_node)
1885 return t;
1887 STRIP_ANY_LOCATION_WRAPPER (t);
1889 if (DECL_P (t) || CONSTANT_CLASS_P (t))
1890 return t;
1892 code = TREE_CODE (t);
1893 switch (code)
1895 case IDENTIFIER_NODE:
1896 case TEMPLATE_PARM_INDEX:
1897 case OVERLOAD:
1898 case BASELINK:
1899 case ARGUMENT_PACK_SELECT:
1900 return t;
1902 case TRAIT_EXPR:
1904 tree type1 = strip_typedefs (TRAIT_EXPR_TYPE1 (t),
1905 remove_attributes, flags);
1906 tree type2 = strip_typedefs (TRAIT_EXPR_TYPE2 (t),
1907 remove_attributes, flags);
1908 if (type1 == TRAIT_EXPR_TYPE1 (t)
1909 && type2 == TRAIT_EXPR_TYPE2 (t))
1910 return t;
1911 r = copy_node (t);
1912 TRAIT_EXPR_TYPE1 (r) = type1;
1913 TRAIT_EXPR_TYPE2 (r) = type2;
1914 return r;
1917 case TREE_LIST:
1919 bool changed = false;
1920 auto_vec<tree_pair, 4> vec;
1921 r = t;
1922 for (; t; t = TREE_CHAIN (t))
1924 tree purpose = strip_typedefs (TREE_PURPOSE (t),
1925 remove_attributes, flags);
1926 tree value = strip_typedefs (TREE_VALUE (t),
1927 remove_attributes, flags);
1928 if (purpose != TREE_PURPOSE (t) || value != TREE_VALUE (t))
1929 changed = true;
1930 vec.safe_push ({purpose, value});
1932 if (changed)
1934 r = NULL_TREE;
1935 for (int i = vec.length () - 1; i >= 0; i--)
1936 r = tree_cons (vec[i].first, vec[i].second, r);
1938 return r;
1941 case TREE_VEC:
1943 bool changed = false;
1944 releasing_vec vec;
1945 n = TREE_VEC_LENGTH (t);
1946 vec_safe_reserve (vec, n);
1947 for (i = 0; i < n; ++i)
1949 tree op = strip_typedefs (TREE_VEC_ELT (t, i),
1950 remove_attributes, flags);
1951 vec->quick_push (op);
1952 if (op != TREE_VEC_ELT (t, i))
1953 changed = true;
1955 if (changed)
1957 r = copy_node (t);
1958 for (i = 0; i < n; ++i)
1959 TREE_VEC_ELT (r, i) = (*vec)[i];
1960 NON_DEFAULT_TEMPLATE_ARGS_COUNT (r)
1961 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
1963 else
1964 r = t;
1965 return r;
1968 case CONSTRUCTOR:
1970 bool changed = false;
1971 vec<constructor_elt, va_gc> *vec
1972 = vec_safe_copy (CONSTRUCTOR_ELTS (t));
1973 n = CONSTRUCTOR_NELTS (t);
1974 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1975 for (i = 0; i < n; ++i)
1977 constructor_elt *e = &(*vec)[i];
1978 tree op = strip_typedefs (e->value, remove_attributes, flags);
1979 if (op != e->value)
1981 changed = true;
1982 e->value = op;
1984 gcc_checking_assert
1985 (e->index == strip_typedefs (e->index, remove_attributes,
1986 flags));
1989 if (!changed && type == TREE_TYPE (t))
1991 vec_free (vec);
1992 return t;
1994 else
1996 r = copy_node (t);
1997 TREE_TYPE (r) = type;
1998 CONSTRUCTOR_ELTS (r) = vec;
1999 return r;
2003 case LAMBDA_EXPR:
2004 return t;
2006 case STATEMENT_LIST:
2007 error ("statement-expression in a constant expression");
2008 return error_mark_node;
2010 default:
2011 break;
2014 gcc_assert (EXPR_P (t));
2016 n = cp_tree_operand_length (t);
2017 ops = XALLOCAVEC (tree, n);
2018 type = TREE_TYPE (t);
2020 switch (code)
2022 CASE_CONVERT:
2023 case IMPLICIT_CONV_EXPR:
2024 case DYNAMIC_CAST_EXPR:
2025 case STATIC_CAST_EXPR:
2026 case CONST_CAST_EXPR:
2027 case REINTERPRET_CAST_EXPR:
2028 case CAST_EXPR:
2029 case NEW_EXPR:
2030 type = strip_typedefs (type, remove_attributes, flags);
2031 /* fallthrough */
2033 default:
2034 for (i = 0; i < n; ++i)
2035 ops[i] = strip_typedefs (TREE_OPERAND (t, i),
2036 remove_attributes, flags);
2037 break;
2040 /* If nothing changed, return t. */
2041 for (i = 0; i < n; ++i)
2042 if (ops[i] != TREE_OPERAND (t, i))
2043 break;
2044 if (i == n && type == TREE_TYPE (t))
2045 return t;
2047 r = copy_node (t);
2048 TREE_TYPE (r) = type;
2049 for (i = 0; i < n; ++i)
2050 TREE_OPERAND (r, i) = ops[i];
2051 return r;
2054 /* Makes a copy of BINFO and TYPE, which is to be inherited into a
2055 graph dominated by T. If BINFO is NULL, TYPE is a dependent base,
2056 and we do a shallow copy. If BINFO is non-NULL, we do a deep copy.
2057 VIRT indicates whether TYPE is inherited virtually or not.
2058 IGO_PREV points at the previous binfo of the inheritance graph
2059 order chain. The newly copied binfo's TREE_CHAIN forms this
2060 ordering.
2062 The CLASSTYPE_VBASECLASSES vector of T is constructed in the
2063 correct order. That is in the order the bases themselves should be
2064 constructed in.
2066 The BINFO_INHERITANCE of a virtual base class points to the binfo
2067 of the most derived type. ??? We could probably change this so that
2068 BINFO_INHERITANCE becomes synonymous with BINFO_PRIMARY, and hence
2069 remove a field. They currently can only differ for primary virtual
2070 virtual bases. */
2072 tree
2073 copy_binfo (tree binfo, tree type, tree t, tree *igo_prev, int virt)
2075 tree new_binfo;
2077 if (virt)
2079 /* See if we've already made this virtual base. */
2080 new_binfo = binfo_for_vbase (type, t);
2081 if (new_binfo)
2082 return new_binfo;
2085 new_binfo = make_tree_binfo (binfo ? BINFO_N_BASE_BINFOS (binfo) : 0);
2086 BINFO_TYPE (new_binfo) = type;
2088 /* Chain it into the inheritance graph. */
2089 TREE_CHAIN (*igo_prev) = new_binfo;
2090 *igo_prev = new_binfo;
2092 if (binfo && !BINFO_DEPENDENT_BASE_P (binfo))
2094 int ix;
2095 tree base_binfo;
2097 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), type));
2099 BINFO_OFFSET (new_binfo) = BINFO_OFFSET (binfo);
2100 BINFO_VIRTUALS (new_binfo) = BINFO_VIRTUALS (binfo);
2102 /* We do not need to copy the accesses, as they are read only. */
2103 BINFO_BASE_ACCESSES (new_binfo) = BINFO_BASE_ACCESSES (binfo);
2105 /* Recursively copy base binfos of BINFO. */
2106 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2108 tree new_base_binfo;
2109 new_base_binfo = copy_binfo (base_binfo, BINFO_TYPE (base_binfo),
2110 t, igo_prev,
2111 BINFO_VIRTUAL_P (base_binfo));
2113 if (!BINFO_INHERITANCE_CHAIN (new_base_binfo))
2114 BINFO_INHERITANCE_CHAIN (new_base_binfo) = new_binfo;
2115 BINFO_BASE_APPEND (new_binfo, new_base_binfo);
2118 else
2119 BINFO_DEPENDENT_BASE_P (new_binfo) = 1;
2121 if (virt)
2123 /* Push it onto the list after any virtual bases it contains
2124 will have been pushed. */
2125 CLASSTYPE_VBASECLASSES (t)->quick_push (new_binfo);
2126 BINFO_VIRTUAL_P (new_binfo) = 1;
2127 BINFO_INHERITANCE_CHAIN (new_binfo) = TYPE_BINFO (t);
2130 return new_binfo;
2133 /* Hashing of lists so that we don't make duplicates.
2134 The entry point is `list_hash_canon'. */
2136 struct list_proxy
2138 tree purpose;
2139 tree value;
2140 tree chain;
2143 struct list_hasher : ggc_ptr_hash<tree_node>
2145 typedef list_proxy *compare_type;
2147 static hashval_t hash (tree);
2148 static bool equal (tree, list_proxy *);
2151 /* Now here is the hash table. When recording a list, it is added
2152 to the slot whose index is the hash code mod the table size.
2153 Note that the hash table is used for several kinds of lists.
2154 While all these live in the same table, they are completely independent,
2155 and the hash code is computed differently for each of these. */
2157 static GTY (()) hash_table<list_hasher> *list_hash_table;
2159 /* Compare ENTRY (an entry in the hash table) with DATA (a list_proxy
2160 for a node we are thinking about adding). */
2162 bool
2163 list_hasher::equal (tree t, list_proxy *proxy)
2165 return (TREE_VALUE (t) == proxy->value
2166 && TREE_PURPOSE (t) == proxy->purpose
2167 && TREE_CHAIN (t) == proxy->chain);
2170 /* Compute a hash code for a list (chain of TREE_LIST nodes
2171 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
2172 TREE_COMMON slots), by adding the hash codes of the individual entries. */
2174 static hashval_t
2175 list_hash_pieces (tree purpose, tree value, tree chain)
2177 hashval_t hashcode = 0;
2179 if (chain)
2180 hashcode += TREE_HASH (chain);
2182 if (value)
2183 hashcode += TREE_HASH (value);
2184 else
2185 hashcode += 1007;
2186 if (purpose)
2187 hashcode += TREE_HASH (purpose);
2188 else
2189 hashcode += 1009;
2190 return hashcode;
2193 /* Hash an already existing TREE_LIST. */
2195 hashval_t
2196 list_hasher::hash (tree t)
2198 return list_hash_pieces (TREE_PURPOSE (t),
2199 TREE_VALUE (t),
2200 TREE_CHAIN (t));
2203 /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
2204 object for an identical list if one already exists. Otherwise, build a
2205 new one, and record it as the canonical object. */
2207 tree
2208 hash_tree_cons (tree purpose, tree value, tree chain)
2210 int hashcode = 0;
2211 tree *slot;
2212 struct list_proxy proxy;
2214 /* Hash the list node. */
2215 hashcode = list_hash_pieces (purpose, value, chain);
2216 /* Create a proxy for the TREE_LIST we would like to create. We
2217 don't actually create it so as to avoid creating garbage. */
2218 proxy.purpose = purpose;
2219 proxy.value = value;
2220 proxy.chain = chain;
2221 /* See if it is already in the table. */
2222 slot = list_hash_table->find_slot_with_hash (&proxy, hashcode, INSERT);
2223 /* If not, create a new node. */
2224 if (!*slot)
2225 *slot = tree_cons (purpose, value, chain);
2226 return (tree) *slot;
2229 /* Constructor for hashed lists. */
2231 tree
2232 hash_tree_chain (tree value, tree chain)
2234 return hash_tree_cons (NULL_TREE, value, chain);
2237 void
2238 debug_binfo (tree elem)
2240 HOST_WIDE_INT n;
2241 tree virtuals;
2243 fprintf (stderr, "type \"%s\", offset = " HOST_WIDE_INT_PRINT_DEC
2244 "\nvtable type:\n",
2245 TYPE_NAME_STRING (BINFO_TYPE (elem)),
2246 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
2247 debug_tree (BINFO_TYPE (elem));
2248 if (BINFO_VTABLE (elem))
2249 fprintf (stderr, "vtable decl \"%s\"\n",
2250 IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem))));
2251 else
2252 fprintf (stderr, "no vtable decl yet\n");
2253 fprintf (stderr, "virtuals:\n");
2254 virtuals = BINFO_VIRTUALS (elem);
2255 n = 0;
2257 while (virtuals)
2259 tree fndecl = TREE_VALUE (virtuals);
2260 fprintf (stderr, "%s [" HOST_WIDE_INT_PRINT_DEC " =? "
2261 HOST_WIDE_INT_PRINT_DEC "]\n",
2262 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
2263 n, TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
2264 ++n;
2265 virtuals = TREE_CHAIN (virtuals);
2269 /* Build a representation for the qualified name SCOPE::NAME. TYPE is
2270 the type of the result expression, if known, or NULL_TREE if the
2271 resulting expression is type-dependent. If TEMPLATE_P is true,
2272 NAME is known to be a template because the user explicitly used the
2273 "template" keyword after the "::".
2275 All SCOPE_REFs should be built by use of this function. */
2277 tree
2278 build_qualified_name (tree type, tree scope, tree name, bool template_p)
2280 tree t;
2281 if (type == error_mark_node
2282 || scope == error_mark_node
2283 || name == error_mark_node)
2284 return error_mark_node;
2285 gcc_assert (TREE_CODE (name) != SCOPE_REF);
2286 t = build2 (SCOPE_REF, type, scope, name);
2287 QUALIFIED_NAME_IS_TEMPLATE (t) = template_p;
2288 PTRMEM_OK_P (t) = true;
2289 if (type)
2290 t = convert_from_reference (t);
2291 return t;
2294 /* Like check_qualified_type, but also check ref-qualifier, exception
2295 specification, and whether the return type was specified after the
2296 parameters. */
2298 static bool
2299 cp_check_qualified_type (const_tree cand, const_tree base, int type_quals,
2300 cp_ref_qualifier rqual, tree raises, bool late)
2302 return (TYPE_QUALS (cand) == type_quals
2303 && check_base_type (cand, base)
2304 && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (cand),
2305 ce_exact)
2306 && TYPE_HAS_LATE_RETURN_TYPE (cand) == late
2307 && type_memfn_rqual (cand) == rqual);
2310 /* Build the FUNCTION_TYPE or METHOD_TYPE with the ref-qualifier RQUAL. */
2312 tree
2313 build_ref_qualified_type (tree type, cp_ref_qualifier rqual)
2315 tree raises = TYPE_RAISES_EXCEPTIONS (type);
2316 bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
2317 return build_cp_fntype_variant (type, rqual, raises, late);
2320 tree
2321 make_binding_vec (tree name, unsigned clusters MEM_STAT_DECL)
2323 /* Stored in an unsigned short, but we're limited to the number of
2324 modules anyway. */
2325 gcc_checking_assert (clusters <= (unsigned short)(~0));
2326 size_t length = (offsetof (tree_binding_vec, vec)
2327 + clusters * sizeof (binding_cluster));
2328 tree vec = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2329 TREE_SET_CODE (vec, BINDING_VECTOR);
2330 BINDING_VECTOR_NAME (vec) = name;
2331 BINDING_VECTOR_ALLOC_CLUSTERS (vec) = clusters;
2332 BINDING_VECTOR_NUM_CLUSTERS (vec) = 0;
2334 return vec;
2337 /* Make a raw overload node containing FN. */
2339 tree
2340 ovl_make (tree fn, tree next)
2342 tree result = make_node (OVERLOAD);
2344 if (TREE_CODE (fn) == OVERLOAD)
2345 OVL_NESTED_P (result) = true;
2347 TREE_TYPE (result) = (next || TREE_CODE (fn) == TEMPLATE_DECL
2348 ? unknown_type_node : TREE_TYPE (fn));
2349 if (next && TREE_CODE (next) == OVERLOAD && OVL_DEDUP_P (next))
2350 OVL_DEDUP_P (result) = true;
2351 OVL_FUNCTION (result) = fn;
2352 OVL_CHAIN (result) = next;
2353 return result;
2356 /* Add FN to the (potentially NULL) overload set OVL. USING_OR_HIDDEN is > 0
2357 if this is a using-decl. It is > 1 if we're revealing the using decl.
2358 It is > 2 if we're also exporting it. USING_OR_HIDDEN is < 0, if FN is
2359 hidden. (A decl cannot be both using and hidden.) We keep the hidden
2360 decls first, but remaining ones are unordered. */
2362 tree
2363 ovl_insert (tree fn, tree maybe_ovl, int using_or_hidden)
2365 tree result = maybe_ovl;
2366 tree insert_after = NULL_TREE;
2368 /* Skip hidden. */
2369 for (; maybe_ovl && TREE_CODE (maybe_ovl) == OVERLOAD
2370 && OVL_HIDDEN_P (maybe_ovl);
2371 maybe_ovl = OVL_CHAIN (maybe_ovl))
2373 gcc_checking_assert (!OVL_LOOKUP_P (maybe_ovl));
2374 insert_after = maybe_ovl;
2377 if (maybe_ovl || using_or_hidden || TREE_CODE (fn) == TEMPLATE_DECL)
2379 maybe_ovl = ovl_make (fn, maybe_ovl);
2381 if (using_or_hidden < 0)
2382 OVL_HIDDEN_P (maybe_ovl) = true;
2383 if (using_or_hidden > 0)
2385 OVL_DEDUP_P (maybe_ovl) = OVL_USING_P (maybe_ovl) = true;
2386 if (using_or_hidden > 1)
2387 OVL_PURVIEW_P (maybe_ovl) = true;
2388 if (using_or_hidden > 2)
2389 OVL_EXPORT_P (maybe_ovl) = true;
2392 else
2393 maybe_ovl = fn;
2395 if (insert_after)
2397 OVL_CHAIN (insert_after) = maybe_ovl;
2398 TREE_TYPE (insert_after) = unknown_type_node;
2400 else
2401 result = maybe_ovl;
2403 return result;
2406 /* Skip any hidden names at the beginning of OVL. */
2408 tree
2409 ovl_skip_hidden (tree ovl)
2411 while (ovl && TREE_CODE (ovl) == OVERLOAD && OVL_HIDDEN_P (ovl))
2412 ovl = OVL_CHAIN (ovl);
2414 return ovl;
2417 /* NODE is an OVL_HIDDEN_P node that is now revealed. */
2419 tree
2420 ovl_iterator::reveal_node (tree overload, tree node)
2422 /* We cannot have returned NODE as part of a lookup overload, so we
2423 don't have to worry about preserving that. */
2425 OVL_HIDDEN_P (node) = false;
2426 if (tree chain = OVL_CHAIN (node))
2427 if (TREE_CODE (chain) == OVERLOAD)
2429 if (OVL_HIDDEN_P (chain))
2431 /* The node needs moving, and the simplest way is to remove it
2432 and reinsert. */
2433 overload = remove_node (overload, node);
2434 overload = ovl_insert (OVL_FUNCTION (node), overload);
2436 else if (OVL_DEDUP_P (chain))
2437 OVL_DEDUP_P (node) = true;
2439 return overload;
2442 /* NODE is on the overloads of OVL. Remove it.
2443 The removed node is unaltered and may continue to be iterated
2444 from (i.e. it is safe to remove a node from an overload one is
2445 currently iterating over). */
2447 tree
2448 ovl_iterator::remove_node (tree overload, tree node)
2450 tree *slot = &overload;
2451 while (*slot != node)
2453 tree probe = *slot;
2454 gcc_checking_assert (!OVL_LOOKUP_P (probe));
2456 slot = &OVL_CHAIN (probe);
2459 /* Stitch out NODE. We don't have to worry about now making a
2460 singleton overload (and consequently maybe setting its type),
2461 because all uses of this function will be followed by inserting a
2462 new node that must follow the place we've cut this out from. */
2463 if (TREE_CODE (node) != OVERLOAD)
2464 /* Cloned inherited ctors don't mark themselves as via_using. */
2465 *slot = NULL_TREE;
2466 else
2467 *slot = OVL_CHAIN (node);
2469 return overload;
2472 /* Mark or unmark a lookup set. */
2474 void
2475 lookup_mark (tree ovl, bool val)
2477 for (lkp_iterator iter (ovl); iter; ++iter)
2479 gcc_checking_assert (LOOKUP_SEEN_P (*iter) != val);
2480 LOOKUP_SEEN_P (*iter) = val;
2484 /* Add a set of new FNS into a lookup. */
2486 tree
2487 lookup_add (tree fns, tree lookup)
2489 if (fns == error_mark_node || lookup == error_mark_node)
2490 return error_mark_node;
2492 if (lookup || TREE_CODE (fns) == TEMPLATE_DECL)
2494 lookup = ovl_make (fns, lookup);
2495 OVL_LOOKUP_P (lookup) = true;
2497 else
2498 lookup = fns;
2500 return lookup;
2503 /* FNS is a new overload set, add them to LOOKUP, if they are not
2504 already present there. */
2506 tree
2507 lookup_maybe_add (tree fns, tree lookup, bool deduping)
2509 if (deduping)
2510 for (tree next, probe = fns; probe; probe = next)
2512 tree fn = probe;
2513 next = NULL_TREE;
2515 if (TREE_CODE (probe) == OVERLOAD)
2517 fn = OVL_FUNCTION (probe);
2518 next = OVL_CHAIN (probe);
2521 if (!LOOKUP_SEEN_P (fn))
2522 LOOKUP_SEEN_P (fn) = true;
2523 else
2525 /* This function was already seen. Insert all the
2526 predecessors onto the lookup. */
2527 for (; fns != probe; fns = OVL_CHAIN (fns))
2529 /* Propagate OVL_USING, but OVL_HIDDEN &
2530 OVL_DEDUP_P don't matter. */
2531 if (OVL_USING_P (fns))
2533 lookup = ovl_make (OVL_FUNCTION (fns), lookup);
2534 OVL_USING_P (lookup) = true;
2536 else
2537 lookup = lookup_add (OVL_FUNCTION (fns), lookup);
2540 /* And now skip this function. */
2541 fns = next;
2545 if (fns)
2546 /* We ended in a set of new functions. Add them all in one go. */
2547 lookup = lookup_add (fns, lookup);
2549 return lookup;
2552 /* Returns nonzero if X is an expression for a (possibly overloaded)
2553 function. If "f" is a function or function template, "f", "c->f",
2554 "c.f", "C::f", and "f<int>" will all be considered possibly
2555 overloaded functions. Returns 2 if the function is actually
2556 overloaded, i.e., if it is impossible to know the type of the
2557 function without performing overload resolution. */
2560 is_overloaded_fn (tree x)
2562 STRIP_ANY_LOCATION_WRAPPER (x);
2564 /* A baselink is also considered an overloaded function. */
2565 if (TREE_CODE (x) == OFFSET_REF
2566 || TREE_CODE (x) == COMPONENT_REF)
2567 x = TREE_OPERAND (x, 1);
2568 x = MAYBE_BASELINK_FUNCTIONS (x);
2569 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
2570 x = TREE_OPERAND (x, 0);
2572 if (DECL_FUNCTION_TEMPLATE_P (OVL_FIRST (x))
2573 || (TREE_CODE (x) == OVERLOAD && !OVL_SINGLE_P (x)))
2574 return 2;
2576 return OVL_P (x);
2579 /* X is the CALL_EXPR_FN of a CALL_EXPR. If X represents a dependent name
2580 (14.6.2), return the IDENTIFIER_NODE for that name. Otherwise, return
2581 NULL_TREE. */
2583 tree
2584 dependent_name (tree x)
2586 /* FIXME a dependent name must be unqualified, but this function doesn't
2587 distinguish between qualified and unqualified identifiers. */
2588 if (identifier_p (x))
2589 return x;
2590 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
2591 x = TREE_OPERAND (x, 0);
2592 if (OVL_P (x))
2593 return OVL_NAME (x);
2594 return NULL_TREE;
2597 /* Like dependent_name, but instead takes a CALL_EXPR and also checks
2598 its dependence. */
2600 tree
2601 call_expr_dependent_name (tree x)
2603 if (TREE_TYPE (x) != NULL_TREE)
2604 /* X isn't dependent, so its callee isn't a dependent name. */
2605 return NULL_TREE;
2606 return dependent_name (CALL_EXPR_FN (x));
2609 /* Returns true iff X is an expression for an overloaded function
2610 whose type cannot be known without performing overload
2611 resolution. */
2613 bool
2614 really_overloaded_fn (tree x)
2616 return is_overloaded_fn (x) == 2;
2619 /* Get the overload set FROM refers to. Returns NULL if it's not an
2620 overload set. */
2622 tree
2623 maybe_get_fns (tree from)
2625 STRIP_ANY_LOCATION_WRAPPER (from);
2627 /* A baselink is also considered an overloaded function. */
2628 if (TREE_CODE (from) == OFFSET_REF
2629 || TREE_CODE (from) == COMPONENT_REF)
2630 from = TREE_OPERAND (from, 1);
2631 if (BASELINK_P (from))
2632 from = BASELINK_FUNCTIONS (from);
2633 if (TREE_CODE (from) == TEMPLATE_ID_EXPR)
2634 from = TREE_OPERAND (from, 0);
2636 if (OVL_P (from))
2637 return from;
2639 return NULL;
2642 /* FROM refers to an overload set. Return that set (or die). */
2644 tree
2645 get_fns (tree from)
2647 tree res = maybe_get_fns (from);
2649 gcc_assert (res);
2650 return res;
2653 /* Return the first function of the overload set FROM refers to. */
2655 tree
2656 get_first_fn (tree from)
2658 return OVL_FIRST (get_fns (from));
2661 /* Return the scope where the overloaded functions OVL were found. */
2663 tree
2664 ovl_scope (tree ovl)
2666 if (TREE_CODE (ovl) == OFFSET_REF
2667 || TREE_CODE (ovl) == COMPONENT_REF)
2668 ovl = TREE_OPERAND (ovl, 1);
2669 if (TREE_CODE (ovl) == BASELINK)
2670 return BINFO_TYPE (BASELINK_BINFO (ovl));
2671 if (TREE_CODE (ovl) == TEMPLATE_ID_EXPR)
2672 ovl = TREE_OPERAND (ovl, 0);
2673 /* Skip using-declarations. */
2674 lkp_iterator iter (ovl);
2676 ovl = *iter;
2677 while (iter.using_p () && ++iter);
2679 return CP_DECL_CONTEXT (ovl);
2682 #define PRINT_RING_SIZE 4
2684 static const char *
2685 cxx_printable_name_internal (tree decl, int v, bool translate)
2687 static unsigned int uid_ring[PRINT_RING_SIZE];
2688 static char *print_ring[PRINT_RING_SIZE];
2689 static bool trans_ring[PRINT_RING_SIZE];
2690 static int ring_counter;
2691 int i;
2693 /* Only cache functions. */
2694 if (v < 2
2695 || TREE_CODE (decl) != FUNCTION_DECL
2696 || DECL_LANG_SPECIFIC (decl) == 0)
2697 return lang_decl_name (decl, v, translate);
2699 /* See if this print name is lying around. */
2700 for (i = 0; i < PRINT_RING_SIZE; i++)
2701 if (uid_ring[i] == DECL_UID (decl) && translate == trans_ring[i])
2702 /* yes, so return it. */
2703 return print_ring[i];
2705 if (++ring_counter == PRINT_RING_SIZE)
2706 ring_counter = 0;
2708 if (current_function_decl != NULL_TREE)
2710 /* There may be both translated and untranslated versions of the
2711 name cached. */
2712 for (i = 0; i < 2; i++)
2714 if (uid_ring[ring_counter] == DECL_UID (current_function_decl))
2715 ring_counter += 1;
2716 if (ring_counter == PRINT_RING_SIZE)
2717 ring_counter = 0;
2719 gcc_assert (uid_ring[ring_counter] != DECL_UID (current_function_decl));
2722 free (print_ring[ring_counter]);
2724 print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v, translate));
2725 uid_ring[ring_counter] = DECL_UID (decl);
2726 trans_ring[ring_counter] = translate;
2727 return print_ring[ring_counter];
2730 const char *
2731 cxx_printable_name (tree decl, int v)
2733 return cxx_printable_name_internal (decl, v, false);
2736 const char *
2737 cxx_printable_name_translate (tree decl, int v)
2739 return cxx_printable_name_internal (decl, v, true);
2742 /* Return the canonical version of exception-specification RAISES for a C++17
2743 function type, for use in type comparison and building TYPE_CANONICAL. */
2745 tree
2746 canonical_eh_spec (tree raises)
2748 if (raises == NULL_TREE)
2749 return raises;
2750 else if (DEFERRED_NOEXCEPT_SPEC_P (raises)
2751 || UNPARSED_NOEXCEPT_SPEC_P (raises)
2752 || uses_template_parms (raises)
2753 || uses_template_parms (TREE_PURPOSE (raises)))
2754 /* Keep a dependent or deferred exception specification. */
2755 return raises;
2756 else if (nothrow_spec_p (raises))
2757 /* throw() -> noexcept. */
2758 return noexcept_true_spec;
2759 else
2760 /* For C++17 type matching, anything else -> nothing. */
2761 return NULL_TREE;
2764 tree
2765 build_cp_fntype_variant (tree type, cp_ref_qualifier rqual,
2766 tree raises, bool late)
2768 cp_cv_quals type_quals = TYPE_QUALS (type);
2770 if (cp_check_qualified_type (type, type, type_quals, rqual, raises, late))
2771 return type;
2773 tree v = TYPE_MAIN_VARIANT (type);
2774 for (; v; v = TYPE_NEXT_VARIANT (v))
2775 if (cp_check_qualified_type (v, type, type_quals, rqual, raises, late))
2776 return v;
2778 /* Need to build a new variant. */
2779 v = build_variant_type_copy (type);
2780 if (!TYPE_DEPENDENT_P (v))
2781 /* We no longer know that it's not type-dependent. */
2782 TYPE_DEPENDENT_P_VALID (v) = false;
2783 TYPE_RAISES_EXCEPTIONS (v) = raises;
2784 TYPE_HAS_LATE_RETURN_TYPE (v) = late;
2785 switch (rqual)
2787 case REF_QUAL_RVALUE:
2788 FUNCTION_RVALUE_QUALIFIED (v) = 1;
2789 FUNCTION_REF_QUALIFIED (v) = 1;
2790 break;
2791 case REF_QUAL_LVALUE:
2792 FUNCTION_RVALUE_QUALIFIED (v) = 0;
2793 FUNCTION_REF_QUALIFIED (v) = 1;
2794 break;
2795 default:
2796 FUNCTION_REF_QUALIFIED (v) = 0;
2797 break;
2800 /* Canonicalize the exception specification. */
2801 tree cr = flag_noexcept_type ? canonical_eh_spec (raises) : NULL_TREE;
2802 bool complex_eh_spec_p = (cr && cr != noexcept_true_spec
2803 && !UNPARSED_NOEXCEPT_SPEC_P (cr));
2805 if (!complex_eh_spec_p && TYPE_RAISES_EXCEPTIONS (type))
2806 /* We want to consider structural equality of the exception-less
2807 variant since we'll be replacing the exception specification. */
2808 type = build_cp_fntype_variant (type, rqual, /*raises=*/NULL_TREE, late);
2809 if (TYPE_STRUCTURAL_EQUALITY_P (type) || complex_eh_spec_p)
2810 /* Propagate structural equality. And always use structural equality
2811 for function types with a complex noexcept-spec since their identity
2812 may depend on e.g. whether comparing_specializations is set. */
2813 SET_TYPE_STRUCTURAL_EQUALITY (v);
2814 else if (TYPE_CANONICAL (type) != type || cr != raises || late)
2815 /* Build the underlying canonical type, since it is different
2816 from TYPE. */
2817 TYPE_CANONICAL (v) = build_cp_fntype_variant (TYPE_CANONICAL (type),
2818 rqual, cr, false);
2819 else
2820 /* T is its own canonical type. */
2821 TYPE_CANONICAL (v) = v;
2823 return v;
2826 /* TYPE is a function or method type with a deferred exception
2827 specification that has been parsed to RAISES. Fixup all the type
2828 variants that are affected in place. Via decltype &| noexcept
2829 tricks, the unparsed spec could have escaped into the type system. */
2831 void
2832 fixup_deferred_exception_variants (tree type, tree raises)
2834 tree original = TYPE_RAISES_EXCEPTIONS (type);
2836 gcc_checking_assert (UNPARSED_NOEXCEPT_SPEC_P (original));
2838 for (tree variant = TYPE_MAIN_VARIANT (type);
2839 variant; variant = TYPE_NEXT_VARIANT (variant))
2840 if (TYPE_RAISES_EXCEPTIONS (variant) == original)
2842 gcc_checking_assert (variant != TYPE_MAIN_VARIANT (type));
2844 SET_TYPE_STRUCTURAL_EQUALITY (variant);
2845 TYPE_RAISES_EXCEPTIONS (variant) = raises;
2847 if (!TYPE_DEPENDENT_P (variant))
2848 /* We no longer know that it's not type-dependent. */
2849 TYPE_DEPENDENT_P_VALID (variant) = false;
2853 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
2854 listed in RAISES. */
2856 tree
2857 build_exception_variant (tree type, tree raises)
2859 cp_ref_qualifier rqual = type_memfn_rqual (type);
2860 bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
2861 return build_cp_fntype_variant (type, rqual, raises, late);
2864 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new
2865 BOUND_TEMPLATE_TEMPLATE_PARM bound with NEWARGS as its template
2866 arguments. */
2868 tree
2869 bind_template_template_parm (tree t, tree newargs)
2871 tree decl = TYPE_NAME (t);
2872 tree t2;
2874 t2 = cxx_make_type (BOUND_TEMPLATE_TEMPLATE_PARM);
2875 decl = build_decl (input_location,
2876 TYPE_DECL, DECL_NAME (decl), NULL_TREE);
2877 SET_DECL_TEMPLATE_PARM_P (decl);
2879 /* These nodes have to be created to reflect new TYPE_DECL and template
2880 arguments. */
2881 TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t));
2882 TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl;
2883 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
2884 = build_template_info (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t), newargs);
2886 TREE_TYPE (decl) = t2;
2887 TYPE_NAME (t2) = decl;
2888 TYPE_STUB_DECL (t2) = decl;
2889 TYPE_SIZE (t2) = 0;
2891 if (any_template_arguments_need_structural_equality_p (newargs))
2892 SET_TYPE_STRUCTURAL_EQUALITY (t2);
2893 else
2894 TYPE_CANONICAL (t2) = canonical_type_parameter (t2);
2896 return t2;
2899 /* Called from count_trees via walk_tree. */
2901 static tree
2902 count_trees_r (tree *tp, int *walk_subtrees, void *data)
2904 ++*((int *) data);
2906 if (TYPE_P (*tp))
2907 *walk_subtrees = 0;
2909 return NULL_TREE;
2912 /* Debugging function for measuring the rough complexity of a tree
2913 representation. */
2916 count_trees (tree t)
2918 int n_trees = 0;
2919 cp_walk_tree_without_duplicates (&t, count_trees_r, &n_trees);
2920 return n_trees;
2923 /* Called from verify_stmt_tree via walk_tree. */
2925 static tree
2926 verify_stmt_tree_r (tree* tp, int * /*walk_subtrees*/, void* data)
2928 tree t = *tp;
2929 hash_table<nofree_ptr_hash <tree_node> > *statements
2930 = static_cast <hash_table<nofree_ptr_hash <tree_node> > *> (data);
2931 tree_node **slot;
2933 if (!STATEMENT_CODE_P (TREE_CODE (t)))
2934 return NULL_TREE;
2936 /* If this statement is already present in the hash table, then
2937 there is a circularity in the statement tree. */
2938 gcc_assert (!statements->find (t));
2940 slot = statements->find_slot (t, INSERT);
2941 *slot = t;
2943 return NULL_TREE;
2946 /* Debugging function to check that the statement T has not been
2947 corrupted. For now, this function simply checks that T contains no
2948 circularities. */
2950 void
2951 verify_stmt_tree (tree t)
2953 hash_table<nofree_ptr_hash <tree_node> > statements (37);
2954 cp_walk_tree (&t, verify_stmt_tree_r, &statements, NULL);
2957 /* Check if the type T depends on a type with no linkage and if so,
2958 return it. If RELAXED_P then do not consider a class type declared
2959 within a vague-linkage function or in a module CMI to have no linkage,
2960 since it can still be accessed within a different TU. Remember:
2961 no-linkage is not the same as internal-linkage. */
2963 tree
2964 no_linkage_check (tree t, bool relaxed_p)
2966 tree r;
2968 /* Lambda types that don't have mangling scope have no linkage. We
2969 check CLASSTYPE_LAMBDA_EXPR for error_mark_node because
2970 when we get here from pushtag none of the lambda information is
2971 set up yet, so we want to assume that the lambda has linkage and
2972 fix it up later if not. We need to check this even in templates so
2973 that we properly handle a lambda-expression in the signature. */
2974 if (LAMBDA_TYPE_P (t)
2975 && CLASSTYPE_LAMBDA_EXPR (t) != error_mark_node)
2977 tree extra = LAMBDA_TYPE_EXTRA_SCOPE (t);
2978 if (!extra)
2979 return t;
2982 /* Otherwise there's no point in checking linkage on template functions; we
2983 can't know their complete types. */
2984 if (processing_template_decl)
2985 return NULL_TREE;
2987 switch (TREE_CODE (t))
2989 case RECORD_TYPE:
2990 if (TYPE_PTRMEMFUNC_P (t))
2991 goto ptrmem;
2992 /* Fall through. */
2993 case UNION_TYPE:
2994 if (!CLASS_TYPE_P (t))
2995 return NULL_TREE;
2996 /* Fall through. */
2997 case ENUMERAL_TYPE:
2998 /* Only treat unnamed types as having no linkage if they're at
2999 namespace scope. This is core issue 966. */
3000 if (TYPE_UNNAMED_P (t) && TYPE_NAMESPACE_SCOPE_P (t))
3001 return t;
3003 for (r = CP_TYPE_CONTEXT (t); ; )
3005 /* If we're a nested type of a !TREE_PUBLIC class, we might not
3006 have linkage, or we might just be in an anonymous namespace.
3007 If we're in a TREE_PUBLIC class, we have linkage. */
3008 if (TYPE_P (r) && !TREE_PUBLIC (TYPE_NAME (r)))
3009 return no_linkage_check (TYPE_CONTEXT (t), relaxed_p);
3010 else if (TREE_CODE (r) == FUNCTION_DECL)
3012 if (relaxed_p
3013 && (vague_linkage_p (r)
3014 || (TREE_PUBLIC (r) && module_maybe_has_cmi_p ())))
3015 r = CP_DECL_CONTEXT (r);
3016 else
3017 return t;
3019 else
3020 break;
3023 return NULL_TREE;
3025 case ARRAY_TYPE:
3026 case POINTER_TYPE:
3027 case REFERENCE_TYPE:
3028 case VECTOR_TYPE:
3029 return no_linkage_check (TREE_TYPE (t), relaxed_p);
3031 case OFFSET_TYPE:
3032 ptrmem:
3033 r = no_linkage_check (TYPE_PTRMEM_POINTED_TO_TYPE (t),
3034 relaxed_p);
3035 if (r)
3036 return r;
3037 return no_linkage_check (TYPE_PTRMEM_CLASS_TYPE (t), relaxed_p);
3039 case METHOD_TYPE:
3040 case FUNCTION_TYPE:
3042 tree parm = TYPE_ARG_TYPES (t);
3043 if (TREE_CODE (t) == METHOD_TYPE)
3044 /* The 'this' pointer isn't interesting; a method has the same
3045 linkage (or lack thereof) as its enclosing class. */
3046 parm = TREE_CHAIN (parm);
3047 for (;
3048 parm && parm != void_list_node;
3049 parm = TREE_CHAIN (parm))
3051 r = no_linkage_check (TREE_VALUE (parm), relaxed_p);
3052 if (r)
3053 return r;
3055 return no_linkage_check (TREE_TYPE (t), relaxed_p);
3058 default:
3059 return NULL_TREE;
3063 extern int depth_reached;
3065 void
3066 cxx_print_statistics (void)
3068 print_template_statistics ();
3069 if (GATHER_STATISTICS)
3070 fprintf (stderr, "maximum template instantiation depth reached: %d\n",
3071 depth_reached);
3074 /* Return, as an INTEGER_CST node, the number of elements for TYPE
3075 (which is an ARRAY_TYPE). This counts only elements of the top
3076 array. */
3078 tree
3079 array_type_nelts_top (tree type)
3081 return fold_build2_loc (input_location,
3082 PLUS_EXPR, sizetype,
3083 array_type_nelts (type),
3084 size_one_node);
3087 /* Return, as an INTEGER_CST node, the number of elements for TYPE
3088 (which is an ARRAY_TYPE). This one is a recursive count of all
3089 ARRAY_TYPEs that are clumped together. */
3091 tree
3092 array_type_nelts_total (tree type)
3094 tree sz = array_type_nelts_top (type);
3095 type = TREE_TYPE (type);
3096 while (TREE_CODE (type) == ARRAY_TYPE)
3098 tree n = array_type_nelts_top (type);
3099 sz = fold_build2_loc (input_location,
3100 MULT_EXPR, sizetype, sz, n);
3101 type = TREE_TYPE (type);
3103 return sz;
3106 struct bot_data
3108 splay_tree target_remap;
3109 bool clear_location;
3112 /* Called from break_out_target_exprs via mapcar. */
3114 static tree
3115 bot_manip (tree* tp, int* walk_subtrees, void* data_)
3117 bot_data &data = *(bot_data*)data_;
3118 splay_tree target_remap = data.target_remap;
3119 tree t = *tp;
3121 if (!TYPE_P (t) && TREE_CONSTANT (t) && !TREE_SIDE_EFFECTS (t))
3123 /* There can't be any TARGET_EXPRs or their slot variables below this
3124 point. But we must make a copy, in case subsequent processing
3125 alters any part of it. For example, during gimplification a cast
3126 of the form (T) &X::f (where "f" is a member function) will lead
3127 to replacing the PTRMEM_CST for &X::f with a VAR_DECL. */
3128 *walk_subtrees = 0;
3129 *tp = unshare_expr (t);
3130 return NULL_TREE;
3132 if (TREE_CODE (t) == TARGET_EXPR)
3134 tree u;
3136 if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
3138 u = build_cplus_new (TREE_TYPE (t), TREE_OPERAND (t, 1),
3139 tf_warning_or_error);
3140 if (u == error_mark_node)
3141 return u;
3142 if (AGGR_INIT_ZERO_FIRST (TREE_OPERAND (t, 1)))
3143 AGGR_INIT_ZERO_FIRST (TREE_OPERAND (u, 1)) = true;
3145 else
3146 u = force_target_expr (TREE_TYPE (t), TREE_OPERAND (t, 1),
3147 tf_warning_or_error);
3149 TARGET_EXPR_IMPLICIT_P (u) = TARGET_EXPR_IMPLICIT_P (t);
3150 TARGET_EXPR_LIST_INIT_P (u) = TARGET_EXPR_LIST_INIT_P (t);
3151 TARGET_EXPR_DIRECT_INIT_P (u) = TARGET_EXPR_DIRECT_INIT_P (t);
3152 TARGET_EXPR_ELIDING_P (u) = TARGET_EXPR_ELIDING_P (t);
3154 /* Map the old variable to the new one. */
3155 splay_tree_insert (target_remap,
3156 (splay_tree_key) TREE_OPERAND (t, 0),
3157 (splay_tree_value) TREE_OPERAND (u, 0));
3159 TREE_OPERAND (u, 1) = break_out_target_exprs (TREE_OPERAND (u, 1),
3160 data.clear_location);
3161 if (TREE_OPERAND (u, 1) == error_mark_node)
3162 return error_mark_node;
3164 if (data.clear_location)
3165 SET_EXPR_LOCATION (u, input_location);
3167 /* Replace the old expression with the new version. */
3168 *tp = u;
3169 /* We don't have to go below this point; the recursive call to
3170 break_out_target_exprs will have handled anything below this
3171 point. */
3172 *walk_subtrees = 0;
3173 return NULL_TREE;
3175 if (TREE_CODE (*tp) == SAVE_EXPR)
3177 t = *tp;
3178 splay_tree_node n = splay_tree_lookup (target_remap,
3179 (splay_tree_key) t);
3180 if (n)
3182 *tp = (tree)n->value;
3183 *walk_subtrees = 0;
3185 else
3187 copy_tree_r (tp, walk_subtrees, NULL);
3188 splay_tree_insert (target_remap,
3189 (splay_tree_key)t,
3190 (splay_tree_value)*tp);
3191 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
3192 splay_tree_insert (target_remap,
3193 (splay_tree_key)*tp,
3194 (splay_tree_value)*tp);
3196 return NULL_TREE;
3198 if (TREE_CODE (*tp) == DECL_EXPR
3199 && VAR_P (DECL_EXPR_DECL (*tp))
3200 && DECL_ARTIFICIAL (DECL_EXPR_DECL (*tp))
3201 && !TREE_STATIC (DECL_EXPR_DECL (*tp)))
3203 tree t;
3204 splay_tree_node n
3205 = splay_tree_lookup (target_remap,
3206 (splay_tree_key) DECL_EXPR_DECL (*tp));
3207 if (n)
3208 t = (tree) n->value;
3209 else
3211 t = create_temporary_var (TREE_TYPE (DECL_EXPR_DECL (*tp)));
3212 DECL_INITIAL (t) = DECL_INITIAL (DECL_EXPR_DECL (*tp));
3213 splay_tree_insert (target_remap,
3214 (splay_tree_key) DECL_EXPR_DECL (*tp),
3215 (splay_tree_value) t);
3217 copy_tree_r (tp, walk_subtrees, NULL);
3218 DECL_EXPR_DECL (*tp) = t;
3219 if (data.clear_location && EXPR_HAS_LOCATION (*tp))
3220 SET_EXPR_LOCATION (*tp, input_location);
3221 return NULL_TREE;
3223 if (TREE_CODE (*tp) == BIND_EXPR && BIND_EXPR_VARS (*tp))
3225 copy_tree_r (tp, walk_subtrees, NULL);
3226 for (tree *p = &BIND_EXPR_VARS (*tp); *p; p = &DECL_CHAIN (*p))
3228 gcc_assert (VAR_P (*p) && DECL_ARTIFICIAL (*p) && !TREE_STATIC (*p));
3229 tree t = create_temporary_var (TREE_TYPE (*p));
3230 DECL_INITIAL (t) = DECL_INITIAL (*p);
3231 DECL_CHAIN (t) = DECL_CHAIN (*p);
3232 splay_tree_insert (target_remap, (splay_tree_key) *p,
3233 (splay_tree_value) t);
3234 *p = t;
3236 if (data.clear_location && EXPR_HAS_LOCATION (*tp))
3237 SET_EXPR_LOCATION (*tp, input_location);
3238 return NULL_TREE;
3241 /* Make a copy of this node. */
3242 t = copy_tree_r (tp, walk_subtrees, NULL);
3243 if (TREE_CODE (*tp) == CALL_EXPR || TREE_CODE (*tp) == AGGR_INIT_EXPR)
3244 if (!processing_template_decl)
3245 set_flags_from_callee (*tp);
3246 if (data.clear_location && EXPR_HAS_LOCATION (*tp))
3247 SET_EXPR_LOCATION (*tp, input_location);
3248 return t;
3251 /* Replace all remapped VAR_DECLs in T with their new equivalents.
3252 DATA is really a splay-tree mapping old variables to new
3253 variables. */
3255 static tree
3256 bot_replace (tree* t, int */*walk_subtrees*/, void* data_)
3258 bot_data &data = *(bot_data*)data_;
3259 splay_tree target_remap = data.target_remap;
3261 if (VAR_P (*t))
3263 splay_tree_node n = splay_tree_lookup (target_remap,
3264 (splay_tree_key) *t);
3265 if (n)
3266 *t = (tree) n->value;
3268 else if (TREE_CODE (*t) == PARM_DECL
3269 && DECL_NAME (*t) == this_identifier
3270 && !DECL_CONTEXT (*t))
3272 /* In an NSDMI we need to replace the 'this' parameter we used for
3273 parsing with the real one for this function. */
3274 *t = current_class_ptr;
3276 else if (TREE_CODE (*t) == CONVERT_EXPR
3277 && CONVERT_EXPR_VBASE_PATH (*t))
3279 /* In an NSDMI build_base_path defers building conversions to morally
3280 virtual bases, and we handle it here. */
3281 tree basetype = TREE_TYPE (*t);
3282 *t = convert_to_base (TREE_OPERAND (*t, 0), basetype,
3283 /*check_access=*/false, /*nonnull=*/true,
3284 tf_warning_or_error);
3287 return NULL_TREE;
3290 /* When we parse a default argument expression, we may create
3291 temporary variables via TARGET_EXPRs. When we actually use the
3292 default-argument expression, we make a copy of the expression
3293 and replace the temporaries with appropriate local versions.
3295 If CLEAR_LOCATION is true, override any EXPR_LOCATION with
3296 input_location. */
3298 tree
3299 break_out_target_exprs (tree t, bool clear_location /* = false */)
3301 static int target_remap_count;
3302 static splay_tree target_remap;
3304 /* We shouldn't be called on templated trees, nor do we want to
3305 produce them. */
3306 gcc_checking_assert (!processing_template_decl);
3308 if (!target_remap_count++)
3309 target_remap = splay_tree_new (splay_tree_compare_pointers,
3310 /*splay_tree_delete_key_fn=*/NULL,
3311 /*splay_tree_delete_value_fn=*/NULL);
3312 bot_data data = { target_remap, clear_location };
3313 if (cp_walk_tree (&t, bot_manip, &data, NULL) == error_mark_node)
3314 t = error_mark_node;
3315 if (cp_walk_tree (&t, bot_replace, &data, NULL) == error_mark_node)
3316 t = error_mark_node;
3318 if (!--target_remap_count)
3320 splay_tree_delete (target_remap);
3321 target_remap = NULL;
3324 return t;
3327 /* Build an expression for the subobject of OBJ at CONSTRUCTOR index INDEX,
3328 which we expect to have type TYPE. */
3330 tree
3331 build_ctor_subob_ref (tree index, tree type, tree obj)
3333 if (index == NULL_TREE)
3334 /* Can't refer to a particular member of a vector. */
3335 obj = NULL_TREE;
3336 else if (TREE_CODE (index) == INTEGER_CST)
3337 obj = cp_build_array_ref (input_location, obj, index, tf_none);
3338 else
3339 obj = build_class_member_access_expr (obj, index, NULL_TREE,
3340 /*reference*/false, tf_none);
3341 if (obj)
3343 tree objtype = TREE_TYPE (obj);
3344 if (TREE_CODE (objtype) == ARRAY_TYPE && !TYPE_DOMAIN (objtype))
3346 /* When the destination object refers to a flexible array member
3347 verify that it matches the type of the source object except
3348 for its domain and qualifiers. */
3349 gcc_assert (comptypes (TYPE_MAIN_VARIANT (type),
3350 TYPE_MAIN_VARIANT (objtype),
3351 COMPARE_REDECLARATION));
3353 else
3354 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, objtype));
3357 return obj;
3360 struct replace_placeholders_t
3362 tree obj; /* The object to be substituted for a PLACEHOLDER_EXPR. */
3363 tree exp; /* The outermost exp. */
3364 bool seen; /* Whether we've encountered a PLACEHOLDER_EXPR. */
3365 hash_set<tree> *pset; /* To avoid walking same trees multiple times. */
3368 /* Like substitute_placeholder_in_expr, but handle C++ tree codes and
3369 build up subexpressions as we go deeper. */
3371 static tree
3372 replace_placeholders_r (tree* t, int* walk_subtrees, void* data_)
3374 replace_placeholders_t *d = static_cast<replace_placeholders_t*>(data_);
3375 tree obj = d->obj;
3377 if (TYPE_P (*t) || TREE_CONSTANT (*t))
3379 *walk_subtrees = false;
3380 return NULL_TREE;
3383 switch (TREE_CODE (*t))
3385 case PLACEHOLDER_EXPR:
3387 tree x = obj;
3388 for (; !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (*t),
3389 TREE_TYPE (x));
3390 x = TREE_OPERAND (x, 0))
3391 gcc_assert (handled_component_p (x));
3392 *t = unshare_expr (x);
3393 *walk_subtrees = false;
3394 d->seen = true;
3396 break;
3398 case CONSTRUCTOR:
3400 constructor_elt *ce;
3401 vec<constructor_elt,va_gc> *v = CONSTRUCTOR_ELTS (*t);
3402 /* Don't walk into CONSTRUCTOR_PLACEHOLDER_BOUNDARY ctors
3403 other than the d->exp one, those have PLACEHOLDER_EXPRs
3404 related to another object. */
3405 if ((CONSTRUCTOR_PLACEHOLDER_BOUNDARY (*t)
3406 && *t != d->exp)
3407 || d->pset->add (*t))
3409 *walk_subtrees = false;
3410 return NULL_TREE;
3412 for (unsigned i = 0; vec_safe_iterate (v, i, &ce); ++i)
3414 tree *valp = &ce->value;
3415 tree type = TREE_TYPE (*valp);
3416 tree subob = obj;
3418 /* Elements with RANGE_EXPR index shouldn't have any
3419 placeholders in them. */
3420 if (ce->index && TREE_CODE (ce->index) == RANGE_EXPR)
3421 continue;
3423 if (TREE_CODE (*valp) == CONSTRUCTOR
3424 && AGGREGATE_TYPE_P (type))
3426 /* If we're looking at the initializer for OBJ, then build
3427 a sub-object reference. If we're looking at an
3428 initializer for another object, just pass OBJ down. */
3429 if (same_type_ignoring_top_level_qualifiers_p
3430 (TREE_TYPE (*t), TREE_TYPE (obj)))
3431 subob = build_ctor_subob_ref (ce->index, type, obj);
3432 if (TREE_CODE (*valp) == TARGET_EXPR)
3433 valp = &TARGET_EXPR_INITIAL (*valp);
3435 d->obj = subob;
3436 cp_walk_tree (valp, replace_placeholders_r, data_, NULL);
3437 d->obj = obj;
3439 *walk_subtrees = false;
3440 break;
3443 default:
3444 if (d->pset->add (*t))
3445 *walk_subtrees = false;
3446 break;
3449 return NULL_TREE;
3452 /* Replace PLACEHOLDER_EXPRs in EXP with object OBJ. SEEN_P is set if
3453 a PLACEHOLDER_EXPR has been encountered. */
3455 tree
3456 replace_placeholders (tree exp, tree obj, bool *seen_p /*= NULL*/)
3458 /* This is only relevant for C++14. */
3459 if (cxx_dialect < cxx14)
3460 return exp;
3462 /* If the object isn't a (member of a) class, do nothing. */
3463 tree op0 = obj;
3464 while (handled_component_p (op0))
3465 op0 = TREE_OPERAND (op0, 0);
3466 if (!CLASS_TYPE_P (strip_array_types (TREE_TYPE (op0))))
3467 return exp;
3469 tree *tp = &exp;
3470 if (TREE_CODE (exp) == TARGET_EXPR)
3471 tp = &TARGET_EXPR_INITIAL (exp);
3472 hash_set<tree> pset;
3473 replace_placeholders_t data = { obj, *tp, false, &pset };
3474 cp_walk_tree (tp, replace_placeholders_r, &data, NULL);
3475 if (seen_p)
3476 *seen_p = data.seen;
3477 return exp;
3480 /* Callback function for find_placeholders. */
3482 static tree
3483 find_placeholders_r (tree *t, int *walk_subtrees, void *)
3485 if (TYPE_P (*t) || TREE_CONSTANT (*t))
3487 *walk_subtrees = false;
3488 return NULL_TREE;
3491 switch (TREE_CODE (*t))
3493 case PLACEHOLDER_EXPR:
3494 return *t;
3496 case CONSTRUCTOR:
3497 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (*t))
3498 *walk_subtrees = false;
3499 break;
3501 default:
3502 break;
3505 return NULL_TREE;
3508 /* Return true if EXP contains a PLACEHOLDER_EXPR. Don't walk into
3509 ctors with CONSTRUCTOR_PLACEHOLDER_BOUNDARY flag set. */
3511 bool
3512 find_placeholders (tree exp)
3514 /* This is only relevant for C++14. */
3515 if (cxx_dialect < cxx14)
3516 return false;
3518 return cp_walk_tree_without_duplicates (&exp, find_placeholders_r, NULL);
3521 /* Similar to `build_nt', but for template definitions of dependent
3522 expressions */
3524 tree
3525 build_min_nt_loc (location_t loc, enum tree_code code, ...)
3527 tree t;
3528 int length;
3529 int i;
3530 va_list p;
3532 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3534 va_start (p, code);
3536 t = make_node (code);
3537 SET_EXPR_LOCATION (t, loc);
3538 length = TREE_CODE_LENGTH (code);
3540 for (i = 0; i < length; i++)
3541 TREE_OPERAND (t, i) = va_arg (p, tree);
3543 va_end (p);
3544 return t;
3547 /* Similar to `build', but for template definitions. */
3549 tree
3550 build_min (enum tree_code code, tree tt, ...)
3552 tree t;
3553 int length;
3554 int i;
3555 va_list p;
3557 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3559 va_start (p, tt);
3561 t = make_node (code);
3562 length = TREE_CODE_LENGTH (code);
3563 TREE_TYPE (t) = tt;
3565 for (i = 0; i < length; i++)
3567 tree x = va_arg (p, tree);
3568 TREE_OPERAND (t, i) = x;
3569 if (x && !TYPE_P (x) && TREE_SIDE_EFFECTS (x))
3570 TREE_SIDE_EFFECTS (t) = 1;
3573 va_end (p);
3575 return t;
3578 /* Similar to `build', but for template definitions of non-dependent
3579 expressions. NON_DEP is the non-dependent expression that has been
3580 built. */
3582 tree
3583 build_min_non_dep (enum tree_code code, tree non_dep, ...)
3585 tree t;
3586 int length;
3587 int i;
3588 va_list p;
3590 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3592 va_start (p, non_dep);
3594 if (REFERENCE_REF_P (non_dep))
3595 non_dep = TREE_OPERAND (non_dep, 0);
3597 t = make_node (code);
3598 SET_EXPR_LOCATION (t, cp_expr_loc_or_input_loc (non_dep));
3599 length = TREE_CODE_LENGTH (code);
3600 TREE_TYPE (t) = unlowered_expr_type (non_dep);
3601 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
3603 for (i = 0; i < length; i++)
3605 tree x = va_arg (p, tree);
3606 TREE_OPERAND (t, i) = x;
3607 if (x && !TYPE_P (x))
3608 TREE_SIDE_EFFECTS (t) |= TREE_SIDE_EFFECTS (x);
3611 va_end (p);
3612 return convert_from_reference (t);
3615 /* Similar to build_min_nt, but call expressions */
3617 tree
3618 build_min_nt_call_vec (tree fn, vec<tree, va_gc> *args)
3620 tree ret, t;
3621 unsigned int ix;
3623 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
3624 CALL_EXPR_FN (ret) = fn;
3625 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
3626 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
3627 CALL_EXPR_ARG (ret, ix) = t;
3629 return ret;
3632 /* Similar to `build_min_nt_call_vec', but for template definitions of
3633 non-dependent expressions. NON_DEP is the non-dependent expression
3634 that has been built. */
3636 tree
3637 build_min_non_dep_call_vec (tree non_dep, tree fn, vec<tree, va_gc> *argvec)
3639 tree t = build_min_nt_call_vec (fn, argvec);
3640 if (REFERENCE_REF_P (non_dep))
3641 non_dep = TREE_OPERAND (non_dep, 0);
3642 TREE_TYPE (t) = TREE_TYPE (non_dep);
3643 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
3644 if (argvec)
3645 for (tree x : *argvec)
3646 if (x && !TYPE_P (x))
3647 TREE_SIDE_EFFECTS (t) |= TREE_SIDE_EFFECTS (x);
3648 return convert_from_reference (t);
3651 /* Similar to build_min_non_dep, but for expressions that have been resolved to
3652 a call to an operator overload. OP is the operator that has been
3653 overloaded. NON_DEP is the non-dependent expression that's been built,
3654 which should be a CALL_EXPR or an INDIRECT_REF to a CALL_EXPR. OVERLOAD is
3655 the overload that NON_DEP is calling. */
3657 tree
3658 build_min_non_dep_op_overload (enum tree_code op,
3659 tree non_dep,
3660 tree overload, ...)
3662 va_list p;
3663 int nargs, expected_nargs;
3664 tree fn, call, obj = NULL_TREE;
3666 non_dep = extract_call_expr (non_dep);
3668 nargs = call_expr_nargs (non_dep);
3670 expected_nargs = cp_tree_code_length (op);
3671 if (DECL_OBJECT_MEMBER_FUNCTION_P (overload)
3672 /* For ARRAY_REF, operator[] is either a non-static member or newly
3673 static member, never out of class and for the static member case
3674 if user uses single index the operator[] needs to have a single
3675 argument as well, but the function is called with 2 - the object
3676 it is invoked on and the index. */
3677 || op == ARRAY_REF)
3678 expected_nargs -= 1;
3679 if ((op == POSTINCREMENT_EXPR
3680 || op == POSTDECREMENT_EXPR)
3681 /* With -fpermissive non_dep could be operator++(). */
3682 && (!flag_permissive || nargs != expected_nargs))
3683 expected_nargs += 1;
3684 gcc_assert (nargs == expected_nargs);
3686 releasing_vec args;
3687 va_start (p, overload);
3689 if (!DECL_OBJECT_MEMBER_FUNCTION_P (overload))
3691 fn = overload;
3692 if (op == ARRAY_REF)
3693 obj = va_arg (p, tree);
3694 for (int i = 0; i < nargs; i++)
3696 tree arg = va_arg (p, tree);
3697 vec_safe_push (args, arg);
3700 else
3702 tree object = va_arg (p, tree);
3703 tree binfo = TYPE_BINFO (TREE_TYPE (object));
3704 tree method = build_baselink (binfo, binfo, overload, NULL_TREE);
3705 fn = build_min (COMPONENT_REF, TREE_TYPE (overload),
3706 object, method, NULL_TREE);
3707 for (int i = 0; i < nargs; i++)
3709 tree arg = va_arg (p, tree);
3710 vec_safe_push (args, arg);
3714 va_end (p);
3715 call = build_min_non_dep_call_vec (non_dep, fn, args);
3717 tree call_expr = extract_call_expr (call);
3718 KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep);
3719 CALL_EXPR_OPERATOR_SYNTAX (call_expr) = true;
3720 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep);
3721 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep);
3723 if (obj)
3724 return keep_unused_object_arg (call, obj, overload);
3725 return call;
3728 /* Similar to above build_min_non_dep_op_overload, but arguments
3729 are taken from ARGS vector. */
3731 tree
3732 build_min_non_dep_op_overload (tree non_dep, tree overload, tree object,
3733 vec<tree, va_gc> *args)
3735 non_dep = extract_call_expr (non_dep);
3737 unsigned int nargs = call_expr_nargs (non_dep);
3738 tree fn = overload;
3739 if (DECL_OBJECT_MEMBER_FUNCTION_P (overload))
3741 tree binfo = TYPE_BINFO (TREE_TYPE (object));
3742 tree method = build_baselink (binfo, binfo, overload, NULL_TREE);
3743 fn = build_min (COMPONENT_REF, TREE_TYPE (overload),
3744 object, method, NULL_TREE);
3745 object = NULL_TREE;
3747 gcc_assert (vec_safe_length (args) == nargs);
3749 tree call = build_min_non_dep_call_vec (non_dep, fn, args);
3751 tree call_expr = extract_call_expr (call);
3752 KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep);
3753 CALL_EXPR_OPERATOR_SYNTAX (call_expr) = true;
3754 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep);
3755 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep);
3757 if (object)
3758 return keep_unused_object_arg (call, object, overload);
3759 return call;
3762 /* Return a new tree vec copied from VEC, with ELT inserted at index IDX. */
3764 vec<tree, va_gc> *
3765 vec_copy_and_insert (vec<tree, va_gc> *old_vec, tree elt, unsigned idx)
3767 unsigned len = vec_safe_length (old_vec);
3768 gcc_assert (idx <= len);
3770 vec<tree, va_gc> *new_vec = NULL;
3771 vec_alloc (new_vec, len + 1);
3773 unsigned i;
3774 for (i = 0; i < len; ++i)
3776 if (i == idx)
3777 new_vec->quick_push (elt);
3778 new_vec->quick_push ((*old_vec)[i]);
3780 if (i == idx)
3781 new_vec->quick_push (elt);
3783 return new_vec;
3786 tree
3787 get_type_decl (tree t)
3789 if (TREE_CODE (t) == TYPE_DECL)
3790 return t;
3791 if (TYPE_P (t))
3792 return TYPE_STUB_DECL (t);
3793 gcc_assert (t == error_mark_node);
3794 return t;
3797 /* Returns the namespace that contains DECL, whether directly or
3798 indirectly. */
3800 tree
3801 decl_namespace_context (tree decl)
3803 while (1)
3805 if (TREE_CODE (decl) == NAMESPACE_DECL)
3806 return decl;
3807 else if (TYPE_P (decl))
3808 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
3809 else
3810 decl = CP_DECL_CONTEXT (decl);
3814 /* Returns true if decl is within an anonymous namespace, however deeply
3815 nested, or false otherwise. */
3817 bool
3818 decl_anon_ns_mem_p (tree decl)
3820 return !TREE_PUBLIC (decl_namespace_context (decl));
3823 /* Returns true if the enclosing scope of DECL has internal or no linkage. */
3825 bool
3826 decl_internal_context_p (const_tree decl)
3828 while (TREE_CODE (decl) != NAMESPACE_DECL)
3830 /* Classes inside anonymous namespaces have TREE_PUBLIC == 0. */
3831 if (TYPE_P (decl))
3832 return !TREE_PUBLIC (TYPE_MAIN_DECL (decl));
3834 decl = CP_DECL_CONTEXT (decl);
3836 return !TREE_PUBLIC (decl);
3839 /* Subroutine of cp_tree_equal: t1 and t2 are two CALL_EXPRs.
3840 Return whether their CALL_EXPR_FNs are equivalent. */
3842 static bool
3843 called_fns_equal (tree t1, tree t2)
3845 /* Core 1321: dependent names are equivalent even if the overload sets
3846 are different. But do compare explicit template arguments. */
3847 tree name1 = call_expr_dependent_name (t1);
3848 tree name2 = call_expr_dependent_name (t2);
3849 t1 = CALL_EXPR_FN (t1);
3850 t2 = CALL_EXPR_FN (t2);
3851 if (name1 || name2)
3853 tree targs1 = NULL_TREE, targs2 = NULL_TREE;
3855 if (name1 != name2)
3856 return false;
3858 /* FIXME dependent_name currently returns an unqualified name regardless
3859 of whether the function was named with a qualified- or unqualified-id.
3860 Until that's fixed, check that we aren't looking at overload sets from
3861 different scopes. */
3862 if (is_overloaded_fn (t1) && is_overloaded_fn (t2)
3863 && (DECL_CONTEXT (get_first_fn (t1))
3864 != DECL_CONTEXT (get_first_fn (t2))))
3865 return false;
3867 if (TREE_CODE (t1) == TEMPLATE_ID_EXPR)
3868 targs1 = TREE_OPERAND (t1, 1);
3869 if (TREE_CODE (t2) == TEMPLATE_ID_EXPR)
3870 targs2 = TREE_OPERAND (t2, 1);
3871 return cp_tree_equal (targs1, targs2);
3873 else
3874 return cp_tree_equal (t1, t2);
3877 bool comparing_override_contracts;
3879 /* In a component reference, return the innermost object of
3880 the postfix-expression. */
3882 static tree
3883 get_innermost_component (tree t)
3885 gcc_assert (TREE_CODE (t) == COMPONENT_REF);
3886 while (TREE_CODE (t) == COMPONENT_REF)
3887 t = TREE_OPERAND (t, 0);
3888 return t;
3891 /* Returns true if T is a possibly converted 'this' or '*this' expression. */
3893 static bool
3894 is_this_expression (tree t)
3896 t = get_innermost_component (t);
3897 /* See through deferences and no-op conversions. */
3898 if (INDIRECT_REF_P (t))
3899 t = TREE_OPERAND (t, 0);
3900 if (TREE_CODE (t) == NOP_EXPR)
3901 t = TREE_OPERAND (t, 0);
3902 return is_this_parameter (t);
3905 static bool
3906 comparing_this_references (tree t1, tree t2)
3908 return is_this_expression (t1) && is_this_expression (t2);
3911 static bool
3912 equivalent_member_references (tree t1, tree t2)
3914 if (!comparing_this_references (t1, t2))
3915 return false;
3916 t1 = TREE_OPERAND (t1, 1);
3917 t2 = TREE_OPERAND (t2, 1);
3918 return t1 == t2;
3921 /* Return truthvalue of whether T1 is the same tree structure as T2.
3922 Return 1 if they are the same. Return 0 if they are different. */
3924 bool
3925 cp_tree_equal (tree t1, tree t2)
3927 enum tree_code code1, code2;
3929 if (t1 == t2)
3930 return true;
3931 if (!t1 || !t2)
3932 return false;
3934 code1 = TREE_CODE (t1);
3935 code2 = TREE_CODE (t2);
3937 if (code1 != code2)
3938 return false;
3940 if (CONSTANT_CLASS_P (t1)
3941 && !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3942 return false;
3944 switch (code1)
3946 case VOID_CST:
3947 /* There's only a single VOID_CST node, so we should never reach
3948 here. */
3949 gcc_unreachable ();
3951 case INTEGER_CST:
3952 return tree_int_cst_equal (t1, t2);
3954 case REAL_CST:
3955 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
3957 case STRING_CST:
3958 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3959 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3960 TREE_STRING_LENGTH (t1));
3962 case FIXED_CST:
3963 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
3964 TREE_FIXED_CST (t2));
3966 case COMPLEX_CST:
3967 return cp_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
3968 && cp_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
3970 case VECTOR_CST:
3971 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
3973 case CONSTRUCTOR:
3974 /* We need to do this when determining whether or not two
3975 non-type pointer to member function template arguments
3976 are the same. */
3977 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
3978 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
3979 return false;
3981 tree field, value;
3982 unsigned int i;
3983 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
3985 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
3986 if (!cp_tree_equal (field, elt2->index)
3987 || !cp_tree_equal (value, elt2->value))
3988 return false;
3991 return true;
3993 case TREE_LIST:
3994 if (!cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
3995 return false;
3996 if (!cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
3997 return false;
3998 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
4000 case SAVE_EXPR:
4001 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
4003 case CALL_EXPR:
4005 if (KOENIG_LOOKUP_P (t1) != KOENIG_LOOKUP_P (t2))
4006 return false;
4008 if (!called_fns_equal (t1, t2))
4009 return false;
4011 call_expr_arg_iterator iter1, iter2;
4012 init_call_expr_arg_iterator (t1, &iter1);
4013 init_call_expr_arg_iterator (t2, &iter2);
4014 if (iter1.n != iter2.n)
4015 return false;
4017 while (more_call_expr_args_p (&iter1))
4019 tree arg1 = next_call_expr_arg (&iter1);
4020 tree arg2 = next_call_expr_arg (&iter2);
4022 gcc_checking_assert (arg1 && arg2);
4023 if (!cp_tree_equal (arg1, arg2))
4024 return false;
4027 return true;
4030 case TARGET_EXPR:
4032 tree o1 = TREE_OPERAND (t1, 0);
4033 tree o2 = TREE_OPERAND (t2, 0);
4035 /* Special case: if either target is an unallocated VAR_DECL,
4036 it means that it's going to be unified with whatever the
4037 TARGET_EXPR is really supposed to initialize, so treat it
4038 as being equivalent to anything. */
4039 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
4040 && !DECL_RTL_SET_P (o1))
4041 /*Nop*/;
4042 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
4043 && !DECL_RTL_SET_P (o2))
4044 /*Nop*/;
4045 else if (!cp_tree_equal (o1, o2))
4046 return false;
4048 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
4051 case PARM_DECL:
4052 /* For comparing uses of parameters in late-specified return types
4053 with an out-of-class definition of the function, but can also come
4054 up for expressions that involve 'this' in a member function
4055 template. */
4057 if (comparing_specializations
4058 && DECL_CONTEXT (t1) != DECL_CONTEXT (t2))
4059 /* When comparing hash table entries, only an exact match is
4060 good enough; we don't want to replace 'this' with the
4061 version from another function. But be more flexible
4062 with parameters with identical contexts. */
4063 return false;
4065 if (same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
4067 if (DECL_ARTIFICIAL (t1) ^ DECL_ARTIFICIAL (t2))
4068 return false;
4069 if (CONSTRAINT_VAR_P (t1) ^ CONSTRAINT_VAR_P (t2))
4070 return false;
4071 if (DECL_ARTIFICIAL (t1)
4072 || (DECL_PARM_LEVEL (t1) == DECL_PARM_LEVEL (t2)
4073 && DECL_PARM_INDEX (t1) == DECL_PARM_INDEX (t2)))
4074 return true;
4076 return false;
4078 case TEMPLATE_DECL:
4079 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t1)
4080 && DECL_TEMPLATE_TEMPLATE_PARM_P (t2))
4081 return cp_tree_equal (TREE_TYPE (t1), TREE_TYPE (t2));
4082 /* Fall through. */
4083 case VAR_DECL:
4084 case CONST_DECL:
4085 case FIELD_DECL:
4086 case FUNCTION_DECL:
4087 case IDENTIFIER_NODE:
4088 case SSA_NAME:
4089 case USING_DECL:
4090 case DEFERRED_PARSE:
4091 return false;
4093 case BASELINK:
4094 return (BASELINK_BINFO (t1) == BASELINK_BINFO (t2)
4095 && BASELINK_ACCESS_BINFO (t1) == BASELINK_ACCESS_BINFO (t2)
4096 && BASELINK_QUALIFIED_P (t1) == BASELINK_QUALIFIED_P (t2)
4097 && cp_tree_equal (BASELINK_FUNCTIONS (t1),
4098 BASELINK_FUNCTIONS (t2)));
4100 case TEMPLATE_PARM_INDEX:
4101 return (TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
4102 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2)
4103 && (TEMPLATE_PARM_PARAMETER_PACK (t1)
4104 == TEMPLATE_PARM_PARAMETER_PACK (t2))
4105 && same_type_p (TREE_TYPE (TEMPLATE_PARM_DECL (t1)),
4106 TREE_TYPE (TEMPLATE_PARM_DECL (t2))));
4108 case TEMPLATE_ID_EXPR:
4109 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
4110 return false;
4111 if (!comp_template_args (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)))
4112 return false;
4113 return true;
4115 case CONSTRAINT_INFO:
4116 return cp_tree_equal (CI_ASSOCIATED_CONSTRAINTS (t1),
4117 CI_ASSOCIATED_CONSTRAINTS (t2));
4119 case CHECK_CONSTR:
4120 return (CHECK_CONSTR_CONCEPT (t1) == CHECK_CONSTR_CONCEPT (t2)
4121 && comp_template_args (CHECK_CONSTR_ARGS (t1),
4122 CHECK_CONSTR_ARGS (t2)));
4124 case TREE_VEC:
4125 /* These are template args. Really we should be getting the
4126 caller to do this as it knows it to be true. */
4127 if (!comp_template_args (t1, t2))
4128 return false;
4129 return true;
4131 case SIZEOF_EXPR:
4132 case ALIGNOF_EXPR:
4134 tree o1 = TREE_OPERAND (t1, 0);
4135 tree o2 = TREE_OPERAND (t2, 0);
4137 if (code1 == SIZEOF_EXPR)
4139 if (SIZEOF_EXPR_TYPE_P (t1))
4140 o1 = TREE_TYPE (o1);
4141 if (SIZEOF_EXPR_TYPE_P (t2))
4142 o2 = TREE_TYPE (o2);
4144 else if (ALIGNOF_EXPR_STD_P (t1) != ALIGNOF_EXPR_STD_P (t2))
4145 return false;
4147 if (TREE_CODE (o1) != TREE_CODE (o2))
4148 return false;
4150 if (ARGUMENT_PACK_P (o1))
4151 return template_args_equal (o1, o2);
4152 else if (TYPE_P (o1))
4153 return same_type_p (o1, o2);
4154 else
4155 return cp_tree_equal (o1, o2);
4158 case MODOP_EXPR:
4160 tree t1_op1, t2_op1;
4162 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
4163 return false;
4165 t1_op1 = TREE_OPERAND (t1, 1);
4166 t2_op1 = TREE_OPERAND (t2, 1);
4167 if (TREE_CODE (t1_op1) != TREE_CODE (t2_op1))
4168 return false;
4170 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t2, 2));
4173 case PTRMEM_CST:
4174 /* Two pointer-to-members are the same if they point to the same
4175 field or function in the same class. */
4176 if (PTRMEM_CST_MEMBER (t1) != PTRMEM_CST_MEMBER (t2))
4177 return false;
4179 return same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2));
4181 case OVERLOAD:
4183 /* Two overloads. Must be exactly the same set of decls. */
4184 lkp_iterator first (t1);
4185 lkp_iterator second (t2);
4187 for (; first && second; ++first, ++second)
4188 if (*first != *second)
4189 return false;
4190 return !(first || second);
4193 case TRAIT_EXPR:
4194 if (TRAIT_EXPR_KIND (t1) != TRAIT_EXPR_KIND (t2))
4195 return false;
4196 return cp_tree_equal (TRAIT_EXPR_TYPE1 (t1), TRAIT_EXPR_TYPE1 (t2))
4197 && cp_tree_equal (TRAIT_EXPR_TYPE2 (t1), TRAIT_EXPR_TYPE2 (t2));
4199 case NON_LVALUE_EXPR:
4200 case VIEW_CONVERT_EXPR:
4201 /* Used for location wrappers with possibly NULL types. */
4202 if (!TREE_TYPE (t1) || !TREE_TYPE (t2))
4204 if (TREE_TYPE (t1) || TREE_TYPE (t2))
4205 return false;
4206 break;
4208 /* FALLTHROUGH */
4210 case CAST_EXPR:
4211 case STATIC_CAST_EXPR:
4212 case REINTERPRET_CAST_EXPR:
4213 case CONST_CAST_EXPR:
4214 case DYNAMIC_CAST_EXPR:
4215 case IMPLICIT_CONV_EXPR:
4216 case NEW_EXPR:
4217 case BIT_CAST_EXPR:
4218 CASE_CONVERT:
4219 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
4220 return false;
4221 /* Now compare operands as usual. */
4222 break;
4224 case DEFERRED_NOEXCEPT:
4225 return (cp_tree_equal (DEFERRED_NOEXCEPT_PATTERN (t1),
4226 DEFERRED_NOEXCEPT_PATTERN (t2))
4227 && comp_template_args (DEFERRED_NOEXCEPT_ARGS (t1),
4228 DEFERRED_NOEXCEPT_ARGS (t2)));
4230 case LAMBDA_EXPR:
4231 /* Two lambda-expressions are never considered equivalent. */
4232 return false;
4234 case TYPE_ARGUMENT_PACK:
4235 case NONTYPE_ARGUMENT_PACK:
4237 tree p1 = ARGUMENT_PACK_ARGS (t1);
4238 tree p2 = ARGUMENT_PACK_ARGS (t2);
4239 int len = TREE_VEC_LENGTH (p1);
4240 if (TREE_VEC_LENGTH (p2) != len)
4241 return false;
4243 for (int ix = 0; ix != len; ix++)
4244 if (!template_args_equal (TREE_VEC_ELT (p1, ix),
4245 TREE_VEC_ELT (p2, ix)))
4246 return false;
4247 return true;
4250 case EXPR_PACK_EXPANSION:
4251 if (!cp_tree_equal (PACK_EXPANSION_PATTERN (t1),
4252 PACK_EXPANSION_PATTERN (t2)))
4253 return false;
4254 if (!comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
4255 PACK_EXPANSION_EXTRA_ARGS (t2)))
4256 return false;
4257 return true;
4259 case COMPONENT_REF:
4260 /* If we're comparing contract conditions of overrides, member references
4261 compare equal if they designate the same member. */
4262 if (comparing_override_contracts)
4263 return equivalent_member_references (t1, t2);
4264 break;
4266 default:
4267 break;
4270 switch (TREE_CODE_CLASS (code1))
4272 case tcc_unary:
4273 case tcc_binary:
4274 case tcc_comparison:
4275 case tcc_expression:
4276 case tcc_vl_exp:
4277 case tcc_reference:
4278 case tcc_statement:
4280 int n = cp_tree_operand_length (t1);
4281 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
4282 && n != TREE_OPERAND_LENGTH (t2))
4283 return false;
4285 for (int i = 0; i < n; ++i)
4286 if (!cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
4287 return false;
4289 return true;
4292 case tcc_type:
4293 return same_type_p (t1, t2);
4295 default:
4296 gcc_unreachable ();
4299 /* We can get here with --disable-checking. */
4300 return false;
4303 /* The type of ARG when used as an lvalue. */
4305 tree
4306 lvalue_type (tree arg)
4308 tree type = TREE_TYPE (arg);
4309 return type;
4312 /* The type of ARG for printing error messages; denote lvalues with
4313 reference types. */
4315 tree
4316 error_type (tree arg)
4318 tree type = TREE_TYPE (arg);
4320 if (TREE_CODE (type) == ARRAY_TYPE)
4322 else if (TREE_CODE (type) == ERROR_MARK)
4324 else if (lvalue_p (arg))
4325 type = build_reference_type (lvalue_type (arg));
4326 else if (MAYBE_CLASS_TYPE_P (type))
4327 type = lvalue_type (arg);
4329 return type;
4332 /* Does FUNCTION use a variable-length argument list? */
4335 varargs_function_p (const_tree function)
4337 return stdarg_p (TREE_TYPE (function));
4340 /* Returns 1 if decl is a member of a class. */
4343 member_p (const_tree decl)
4345 const_tree const ctx = DECL_CONTEXT (decl);
4346 return (ctx && TYPE_P (ctx));
4349 /* Create a placeholder for member access where we don't actually have an
4350 object that the access is against. For a general declval<T> equivalent,
4351 use build_stub_object instead. */
4353 tree
4354 build_dummy_object (tree type)
4356 tree decl = build1 (CONVERT_EXPR, build_pointer_type (type), void_node);
4357 return cp_build_fold_indirect_ref (decl);
4360 /* We've gotten a reference to a member of TYPE. Return *this if appropriate,
4361 or a dummy object otherwise. If BINFOP is non-0, it is filled with the
4362 binfo path from current_class_type to TYPE, or 0. */
4364 tree
4365 maybe_dummy_object (tree type, tree* binfop)
4367 tree decl, context;
4368 tree binfo;
4369 tree current = current_nonlambda_class_type ();
4371 if (current
4372 && (binfo = lookup_base (current, type, ba_any, NULL,
4373 tf_warning_or_error)))
4374 context = current;
4375 else
4377 /* Reference from a nested class member function. */
4378 context = type;
4379 binfo = TYPE_BINFO (type);
4382 if (binfop)
4383 *binfop = binfo;
4385 /* current_class_ref might not correspond to current_class_type if
4386 we're in tsubst_default_argument or a lambda-declarator; in either
4387 case, we want to use current_class_ref if it matches CONTEXT. */
4388 tree ctype = current_class_ref ? TREE_TYPE (current_class_ref) : NULL_TREE;
4389 if (ctype
4390 && same_type_ignoring_top_level_qualifiers_p (ctype, context))
4391 decl = current_class_ref;
4392 else
4394 /* Return a dummy object whose cv-quals are consistent with (the
4395 non-lambda) 'this' if available. */
4396 if (ctype)
4398 int quals = TYPE_UNQUALIFIED;
4399 if (tree lambda = CLASSTYPE_LAMBDA_EXPR (ctype))
4401 if (tree cap = lambda_expr_this_capture (lambda, false))
4402 quals = cp_type_quals (TREE_TYPE (TREE_TYPE (cap)));
4404 else
4405 quals = cp_type_quals (ctype);
4406 context = cp_build_qualified_type (context, quals);
4408 decl = build_dummy_object (context);
4411 return decl;
4414 /* Returns 1 if OB is a placeholder object, or a pointer to one. */
4416 bool
4417 is_dummy_object (const_tree ob)
4419 if (INDIRECT_REF_P (ob))
4420 ob = TREE_OPERAND (ob, 0);
4421 return (TREE_CODE (ob) == CONVERT_EXPR
4422 && TREE_OPERAND (ob, 0) == void_node);
4425 /* Returns true if TYPE is char, unsigned char, or std::byte. */
4427 bool
4428 is_byte_access_type (tree type)
4430 type = TYPE_MAIN_VARIANT (type);
4431 if (type == char_type_node
4432 || type == unsigned_char_type_node)
4433 return true;
4435 return (TREE_CODE (type) == ENUMERAL_TYPE
4436 && TYPE_CONTEXT (type) == std_node
4437 && !strcmp ("byte", TYPE_NAME_STRING (type)));
4440 /* Returns true if TYPE is unsigned char or std::byte. */
4442 bool
4443 is_byte_access_type_not_plain_char (tree type)
4445 type = TYPE_MAIN_VARIANT (type);
4446 if (type == char_type_node)
4447 return false;
4449 return is_byte_access_type (type);
4452 /* Returns 1 iff type T is something we want to treat as a scalar type for
4453 the purpose of deciding whether it is trivial/POD/standard-layout. */
4455 bool
4456 scalarish_type_p (const_tree t)
4458 if (t == error_mark_node)
4459 return 1;
4461 return (SCALAR_TYPE_P (t) || VECTOR_TYPE_P (t));
4464 /* Returns true iff T requires non-trivial default initialization. */
4466 bool
4467 type_has_nontrivial_default_init (const_tree t)
4469 t = strip_array_types (CONST_CAST_TREE (t));
4471 if (CLASS_TYPE_P (t))
4472 return TYPE_HAS_COMPLEX_DFLT (t);
4473 else
4474 return 0;
4477 /* Track classes with only deleted copy/move constructors so that we can warn
4478 if they are used in call/return by value. */
4480 static GTY(()) hash_set<tree>* deleted_copy_types;
4481 static void
4482 remember_deleted_copy (const_tree t)
4484 if (!deleted_copy_types)
4485 deleted_copy_types = hash_set<tree>::create_ggc(37);
4486 deleted_copy_types->add (CONST_CAST_TREE (t));
4488 void
4489 maybe_warn_parm_abi (tree t, location_t loc)
4491 if (!deleted_copy_types
4492 || !deleted_copy_types->contains (t))
4493 return;
4495 if ((flag_abi_version == 12 || warn_abi_version == 12)
4496 && classtype_has_non_deleted_move_ctor (t))
4498 bool w;
4499 auto_diagnostic_group d;
4500 if (flag_abi_version > 12)
4501 w = warning_at (loc, OPT_Wabi, "%<-fabi-version=13%> (GCC 8.2) fixes "
4502 "the calling convention for %qT, which was "
4503 "accidentally changed in 8.1", t);
4504 else
4505 w = warning_at (loc, OPT_Wabi, "%<-fabi-version=12%> (GCC 8.1) "
4506 "accidentally changes the calling convention for %qT",
4508 if (w)
4509 inform (location_of (t), " declared here");
4510 return;
4513 auto_diagnostic_group d;
4514 if (warning_at (loc, OPT_Wabi, "the calling convention for %qT changes in "
4515 "%<-fabi-version=13%> (GCC 8.2)", t))
4516 inform (location_of (t), " because all of its copy and move "
4517 "constructors are deleted");
4520 /* Returns true iff copying an object of type T (including via move
4521 constructor) is non-trivial. That is, T has no non-trivial copy
4522 constructors and no non-trivial move constructors, and not all copy/move
4523 constructors are deleted. This function implements the ABI notion of
4524 non-trivial copy, which has diverged from the one in the standard. */
4526 bool
4527 type_has_nontrivial_copy_init (const_tree type)
4529 tree t = strip_array_types (CONST_CAST_TREE (type));
4531 if (CLASS_TYPE_P (t))
4533 gcc_assert (COMPLETE_TYPE_P (t));
4535 if (TYPE_HAS_COMPLEX_COPY_CTOR (t)
4536 || TYPE_HAS_COMPLEX_MOVE_CTOR (t))
4537 /* Nontrivial. */
4538 return true;
4540 if (cxx_dialect < cxx11)
4541 /* No deleted functions before C++11. */
4542 return false;
4544 /* Before ABI v12 we did a bitwise copy of types with only deleted
4545 copy/move constructors. */
4546 if (!abi_version_at_least (12)
4547 && !(warn_abi && abi_version_crosses (12)))
4548 return false;
4550 bool saw_copy = false;
4551 bool saw_non_deleted = false;
4552 bool saw_non_deleted_move = false;
4554 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
4555 saw_copy = saw_non_deleted = true;
4556 else if (CLASSTYPE_LAZY_COPY_CTOR (t))
4558 saw_copy = true;
4559 if (classtype_has_move_assign_or_move_ctor_p (t, true))
4560 /* [class.copy]/8 If the class definition declares a move
4561 constructor or move assignment operator, the implicitly declared
4562 copy constructor is defined as deleted.... */;
4563 else
4564 /* Any other reason the implicitly-declared function would be
4565 deleted would also cause TYPE_HAS_COMPLEX_COPY_CTOR to be
4566 set. */
4567 saw_non_deleted = true;
4570 if (!saw_non_deleted)
4571 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4573 tree fn = *iter;
4574 if (copy_fn_p (fn))
4576 saw_copy = true;
4577 if (!DECL_DELETED_FN (fn))
4579 /* Not deleted, therefore trivial. */
4580 saw_non_deleted = true;
4581 break;
4584 else if (move_fn_p (fn))
4585 if (!DECL_DELETED_FN (fn))
4586 saw_non_deleted_move = true;
4589 gcc_assert (saw_copy);
4591 /* ABI v12 buggily ignored move constructors. */
4592 bool v11nontriv = false;
4593 bool v12nontriv = !saw_non_deleted;
4594 bool v13nontriv = !saw_non_deleted && !saw_non_deleted_move;
4595 bool nontriv = (abi_version_at_least (13) ? v13nontriv
4596 : flag_abi_version == 12 ? v12nontriv
4597 : v11nontriv);
4598 bool warn_nontriv = (warn_abi_version >= 13 ? v13nontriv
4599 : warn_abi_version == 12 ? v12nontriv
4600 : v11nontriv);
4601 if (nontriv != warn_nontriv)
4602 remember_deleted_copy (t);
4604 return nontriv;
4606 else
4607 return 0;
4610 /* Returns 1 iff type T is a trivially copyable type, as defined in
4611 [basic.types] and [class]. */
4613 bool
4614 trivially_copyable_p (const_tree t)
4616 t = strip_array_types (CONST_CAST_TREE (t));
4618 if (CLASS_TYPE_P (t))
4619 return ((!TYPE_HAS_COPY_CTOR (t)
4620 || !TYPE_HAS_COMPLEX_COPY_CTOR (t))
4621 && !TYPE_HAS_COMPLEX_MOVE_CTOR (t)
4622 && (!TYPE_HAS_COPY_ASSIGN (t)
4623 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (t))
4624 && !TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
4625 && TYPE_HAS_TRIVIAL_DESTRUCTOR (t));
4626 else
4627 /* CWG 2094 makes volatile-qualified scalars trivially copyable again. */
4628 return scalarish_type_p (t);
4631 /* Returns 1 iff type T is a trivial type, as defined in [basic.types] and
4632 [class]. */
4634 bool
4635 trivial_type_p (const_tree t)
4637 t = strip_array_types (CONST_CAST_TREE (t));
4639 if (CLASS_TYPE_P (t))
4640 /* A trivial class is a class that is trivially copyable and has one or
4641 more eligible default constructors, all of which are trivial. */
4642 return (type_has_non_deleted_trivial_default_ctor (CONST_CAST_TREE (t))
4643 && trivially_copyable_p (t));
4644 else
4645 return scalarish_type_p (t);
4648 /* Returns 1 iff type T is a POD type, as defined in [basic.types]. */
4650 bool
4651 pod_type_p (const_tree t)
4653 /* This CONST_CAST is okay because strip_array_types returns its
4654 argument unmodified and we assign it to a const_tree. */
4655 t = strip_array_types (CONST_CAST_TREE(t));
4657 if (!CLASS_TYPE_P (t))
4658 return scalarish_type_p (t);
4659 else if (cxx_dialect > cxx98)
4660 /* [class]/10: A POD struct is a class that is both a trivial class and a
4661 standard-layout class, and has no non-static data members of type
4662 non-POD struct, non-POD union (or array of such types).
4664 We don't need to check individual members because if a member is
4665 non-std-layout or non-trivial, the class will be too. */
4666 return (std_layout_type_p (t) && trivial_type_p (t));
4667 else
4668 /* The C++98 definition of POD is different. */
4669 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
4672 /* Returns true iff T is POD for the purpose of layout, as defined in the
4673 C++ ABI. */
4675 bool
4676 layout_pod_type_p (const_tree t)
4678 t = strip_array_types (CONST_CAST_TREE (t));
4680 if (CLASS_TYPE_P (t))
4681 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
4682 else
4683 return scalarish_type_p (t);
4686 /* Returns true iff T is a standard-layout type, as defined in
4687 [basic.types]. */
4689 bool
4690 std_layout_type_p (const_tree t)
4692 t = strip_array_types (CONST_CAST_TREE (t));
4694 if (CLASS_TYPE_P (t))
4695 return !CLASSTYPE_NON_STD_LAYOUT (t);
4696 else
4697 return scalarish_type_p (t);
4700 static bool record_has_unique_obj_representations (const_tree, const_tree);
4702 /* Returns true iff T satisfies std::has_unique_object_representations<T>,
4703 as defined in [meta.unary.prop]. */
4705 bool
4706 type_has_unique_obj_representations (const_tree t)
4708 bool ret;
4710 t = strip_array_types (CONST_CAST_TREE (t));
4712 if (!trivially_copyable_p (t))
4713 return false;
4715 if (CLASS_TYPE_P (t) && CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t))
4716 return CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t);
4718 switch (TREE_CODE (t))
4720 case INTEGER_TYPE:
4721 case POINTER_TYPE:
4722 case REFERENCE_TYPE:
4723 /* If some backend has any paddings in these types, we should add
4724 a target hook for this and handle it there. */
4725 return true;
4727 case BOOLEAN_TYPE:
4728 /* For bool values other than 0 and 1 should only appear with
4729 undefined behavior. */
4730 return true;
4732 case ENUMERAL_TYPE:
4733 return type_has_unique_obj_representations (ENUM_UNDERLYING_TYPE (t));
4735 case REAL_TYPE:
4736 /* XFmode certainly contains padding on x86, which the CPU doesn't store
4737 when storing long double values, so for that we have to return false.
4738 Other kinds of floating point values are questionable due to +.0/-.0
4739 and NaNs, let's play safe for now. */
4740 return false;
4742 case FIXED_POINT_TYPE:
4743 return false;
4745 case OFFSET_TYPE:
4746 return true;
4748 case COMPLEX_TYPE:
4749 case VECTOR_TYPE:
4750 return type_has_unique_obj_representations (TREE_TYPE (t));
4752 case RECORD_TYPE:
4753 ret = record_has_unique_obj_representations (t, TYPE_SIZE (t));
4754 if (CLASS_TYPE_P (t))
4756 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
4757 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
4759 return ret;
4761 case UNION_TYPE:
4762 ret = true;
4763 bool any_fields;
4764 any_fields = false;
4765 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4766 if (TREE_CODE (field) == FIELD_DECL)
4768 any_fields = true;
4769 if (!type_has_unique_obj_representations (TREE_TYPE (field))
4770 || simple_cst_equal (DECL_SIZE (field), TYPE_SIZE (t)) != 1)
4772 ret = false;
4773 break;
4776 if (!any_fields && !integer_zerop (TYPE_SIZE (t)))
4777 ret = false;
4778 if (CLASS_TYPE_P (t))
4780 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
4781 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
4783 return ret;
4785 case NULLPTR_TYPE:
4786 return false;
4788 case ERROR_MARK:
4789 return false;
4791 default:
4792 gcc_unreachable ();
4796 /* Helper function for type_has_unique_obj_representations. */
4798 static bool
4799 record_has_unique_obj_representations (const_tree t, const_tree sz)
4801 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4802 if (TREE_CODE (field) != FIELD_DECL)
4804 /* For bases, can't use type_has_unique_obj_representations here, as in
4805 struct S { int i : 24; S (); };
4806 struct T : public S { int j : 8; T (); };
4807 S doesn't have unique obj representations, but T does. */
4808 else if (DECL_FIELD_IS_BASE (field))
4810 if (!record_has_unique_obj_representations (TREE_TYPE (field),
4811 DECL_SIZE (field)))
4812 return false;
4814 else if (DECL_C_BIT_FIELD (field) && !DECL_UNNAMED_BIT_FIELD (field))
4816 tree btype = DECL_BIT_FIELD_TYPE (field);
4817 if (!type_has_unique_obj_representations (btype))
4818 return false;
4820 else if (!type_has_unique_obj_representations (TREE_TYPE (field)))
4821 return false;
4823 offset_int cur = 0;
4824 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4825 if (TREE_CODE (field) == FIELD_DECL && !DECL_UNNAMED_BIT_FIELD (field))
4827 offset_int fld = wi::to_offset (DECL_FIELD_OFFSET (field));
4828 offset_int bitpos = wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
4829 fld = fld * BITS_PER_UNIT + bitpos;
4830 if (cur != fld)
4831 return false;
4832 if (DECL_SIZE (field))
4834 offset_int size = wi::to_offset (DECL_SIZE (field));
4835 cur += size;
4838 if (cur != wi::to_offset (sz))
4839 return false;
4841 return true;
4844 /* Nonzero iff type T is a class template implicit specialization. */
4846 bool
4847 class_tmpl_impl_spec_p (const_tree t)
4849 return CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INSTANTIATION (t);
4852 /* Returns 1 iff zero initialization of type T means actually storing
4853 zeros in it. */
4856 zero_init_p (const_tree t)
4858 /* This CONST_CAST is okay because strip_array_types returns its
4859 argument unmodified and we assign it to a const_tree. */
4860 t = strip_array_types (CONST_CAST_TREE(t));
4862 if (t == error_mark_node)
4863 return 1;
4865 /* NULL pointers to data members are initialized with -1. */
4866 if (TYPE_PTRDATAMEM_P (t))
4867 return 0;
4869 /* Classes that contain types that can't be zero-initialized, cannot
4870 be zero-initialized themselves. */
4871 if (CLASS_TYPE_P (t) && CLASSTYPE_NON_ZERO_INIT_P (t))
4872 return 0;
4874 return 1;
4877 /* Returns true if the expression or initializer T is the result of
4878 zero-initialization for its type, taking pointers to members
4879 into consideration. */
4881 bool
4882 zero_init_expr_p (tree t)
4884 tree type = TREE_TYPE (t);
4885 if (!type || uses_template_parms (type))
4886 return false;
4887 if (TYPE_PTRMEM_P (type))
4888 return null_member_pointer_value_p (t);
4889 if (TREE_CODE (t) == CONSTRUCTOR)
4891 if (COMPOUND_LITERAL_P (t)
4892 || BRACE_ENCLOSED_INITIALIZER_P (t))
4893 /* Undigested, conversions might change the zeroness. */
4894 return false;
4895 for (constructor_elt &elt : CONSTRUCTOR_ELTS (t))
4897 if (TREE_CODE (type) == UNION_TYPE
4898 && elt.index != first_field (type))
4899 return false;
4900 if (!zero_init_expr_p (elt.value))
4901 return false;
4903 return true;
4905 if (zero_init_p (type))
4906 return initializer_zerop (t);
4907 return false;
4910 /* True IFF T is a C++20 structural type (P1907R1) that can be used as a
4911 non-type template parameter. If EXPLAIN, explain why not. */
4913 bool
4914 structural_type_p (tree t, bool explain)
4916 /* A structural type is one of the following: */
4918 /* a scalar type, or */
4919 if (SCALAR_TYPE_P (t))
4920 return true;
4921 /* an lvalue reference type, or */
4922 if (TYPE_REF_P (t) && !TYPE_REF_IS_RVALUE (t))
4923 return true;
4924 /* a literal class type with the following properties:
4925 - all base classes and non-static data members are public and non-mutable
4927 - the types of all bases classes and non-static data members are
4928 structural types or (possibly multi-dimensional) array thereof. */
4929 if (!CLASS_TYPE_P (t))
4930 return false;
4931 if (!literal_type_p (t))
4933 if (explain)
4934 explain_non_literal_class (t);
4935 return false;
4937 for (tree m = next_aggregate_field (TYPE_FIELDS (t)); m;
4938 m = next_aggregate_field (DECL_CHAIN (m)))
4940 if (TREE_PRIVATE (m) || TREE_PROTECTED (m))
4942 if (explain)
4944 if (DECL_FIELD_IS_BASE (m))
4945 inform (location_of (m), "base class %qT is not public",
4946 TREE_TYPE (m));
4947 else
4948 inform (location_of (m), "%qD is not public", m);
4950 return false;
4952 if (DECL_MUTABLE_P (m))
4954 if (explain)
4955 inform (location_of (m), "%qD is mutable", m);
4956 return false;
4958 tree mtype = strip_array_types (TREE_TYPE (m));
4959 if (!structural_type_p (mtype))
4961 if (explain)
4963 inform (location_of (m), "%qD has a non-structural type", m);
4964 structural_type_p (mtype, true);
4966 return false;
4969 return true;
4972 /* Partially handle the C++11 [[carries_dependency]] attribute.
4973 Just emit a different diagnostics when it is used on something the
4974 spec doesn't allow vs. where it allows and we just choose to ignore
4975 it. */
4977 static tree
4978 handle_carries_dependency_attribute (tree *node, tree name,
4979 tree ARG_UNUSED (args),
4980 int ARG_UNUSED (flags),
4981 bool *no_add_attrs)
4983 if (TREE_CODE (*node) != FUNCTION_DECL
4984 && TREE_CODE (*node) != PARM_DECL)
4986 warning (OPT_Wattributes, "%qE attribute can only be applied to "
4987 "functions or parameters", name);
4988 *no_add_attrs = true;
4990 else
4992 warning (OPT_Wattributes, "%qE attribute ignored", name);
4993 *no_add_attrs = true;
4995 return NULL_TREE;
4998 /* Handle the C++17 [[nodiscard]] attribute, which is similar to the GNU
4999 warn_unused_result attribute. */
5001 static tree
5002 handle_nodiscard_attribute (tree *node, tree name, tree args,
5003 int /*flags*/, bool *no_add_attrs)
5005 if (args && TREE_CODE (TREE_VALUE (args)) != STRING_CST)
5007 error ("%qE attribute argument must be a string constant", name);
5008 *no_add_attrs = true;
5010 if (TREE_CODE (*node) == FUNCTION_DECL)
5012 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node)))
5013 && !DECL_CONSTRUCTOR_P (*node))
5014 warning_at (DECL_SOURCE_LOCATION (*node),
5015 OPT_Wattributes, "%qE attribute applied to %qD with void "
5016 "return type", name, *node);
5018 else if (OVERLOAD_TYPE_P (*node))
5019 /* OK */;
5020 else
5022 warning (OPT_Wattributes, "%qE attribute can only be applied to "
5023 "functions or to class or enumeration types", name);
5024 *no_add_attrs = true;
5026 return NULL_TREE;
5029 /* Handle a C++20 "no_unique_address" attribute; arguments as in
5030 struct attribute_spec.handler. */
5031 static tree
5032 handle_no_unique_addr_attribute (tree* node,
5033 tree name,
5034 tree /*args*/,
5035 int /*flags*/,
5036 bool* no_add_attrs)
5038 if (TREE_CODE (*node) == VAR_DECL)
5040 DECL_MERGEABLE (*node) = true;
5041 if (pedantic)
5042 warning (OPT_Wattributes, "%qE attribute can only be applied to "
5043 "non-static data members", name);
5045 else if (TREE_CODE (*node) != FIELD_DECL)
5047 warning (OPT_Wattributes, "%qE attribute can only be applied to "
5048 "non-static data members", name);
5049 *no_add_attrs = true;
5051 else if (DECL_C_BIT_FIELD (*node))
5053 warning (OPT_Wattributes, "%qE attribute cannot be applied to "
5054 "a bit-field", name);
5055 *no_add_attrs = true;
5058 return NULL_TREE;
5061 /* The C++20 [[likely]] and [[unlikely]] attributes on labels map to the GNU
5062 hot/cold attributes. */
5064 static tree
5065 handle_likeliness_attribute (tree *node, tree name, tree args,
5066 int flags, bool *no_add_attrs)
5068 *no_add_attrs = true;
5069 if (TREE_CODE (*node) == LABEL_DECL
5070 || TREE_CODE (*node) == FUNCTION_DECL)
5072 if (args)
5073 warning (OPT_Wattributes, "%qE attribute takes no arguments", name);
5074 tree bname = (is_attribute_p ("likely", name)
5075 ? get_identifier ("hot") : get_identifier ("cold"));
5076 if (TREE_CODE (*node) == FUNCTION_DECL)
5077 warning (OPT_Wattributes, "ISO C++ %qE attribute does not apply to "
5078 "functions; treating as %<[[gnu::%E]]%>", name, bname);
5079 tree battr = build_tree_list (bname, NULL_TREE);
5080 decl_attributes (node, battr, flags);
5081 return NULL_TREE;
5083 else
5084 return error_mark_node;
5087 /* Table of valid C++ attributes. */
5088 static const attribute_spec cxx_gnu_attributes[] =
5090 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
5091 affects_type_identity, handler, exclude } */
5092 { "init_priority", 1, 1, true, false, false, false,
5093 handle_init_priority_attribute, NULL },
5094 { "abi_tag", 1, -1, false, false, false, true,
5095 handle_abi_tag_attribute, NULL },
5096 { "no_dangling", 0, 1, false, true, false, false,
5097 handle_no_dangling_attribute, NULL },
5100 const scoped_attribute_specs cxx_gnu_attribute_table =
5102 "gnu", { cxx_gnu_attributes }
5105 /* Table of C++ standard attributes. */
5106 static const attribute_spec std_attributes[] =
5108 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
5109 affects_type_identity, handler, exclude } */
5110 { "maybe_unused", 0, 0, false, false, false, false,
5111 handle_unused_attribute, NULL },
5112 { "nodiscard", 0, 1, false, false, false, false,
5113 handle_nodiscard_attribute, NULL },
5114 { "no_unique_address", 0, 0, true, false, false, false,
5115 handle_no_unique_addr_attribute, NULL },
5116 { "likely", 0, 0, false, false, false, false,
5117 handle_likeliness_attribute, attr_cold_hot_exclusions },
5118 { "unlikely", 0, 0, false, false, false, false,
5119 handle_likeliness_attribute, attr_cold_hot_exclusions },
5120 { "noreturn", 0, 0, true, false, false, false,
5121 handle_noreturn_attribute, attr_noreturn_exclusions },
5122 { "carries_dependency", 0, 0, true, false, false, false,
5123 handle_carries_dependency_attribute, NULL },
5124 { "pre", 0, -1, false, false, false, false,
5125 handle_contract_attribute, NULL },
5126 { "post", 0, -1, false, false, false, false,
5127 handle_contract_attribute, NULL }
5130 const scoped_attribute_specs std_attribute_table =
5132 nullptr, { std_attributes }
5135 /* Handle an "init_priority" attribute; arguments as in
5136 struct attribute_spec.handler. */
5137 static tree
5138 handle_init_priority_attribute (tree* node,
5139 tree name,
5140 tree args,
5141 int /*flags*/,
5142 bool* no_add_attrs)
5144 if (!SUPPORTS_INIT_PRIORITY)
5145 /* Treat init_priority as an unrecognized attribute (mirroring
5146 __has_attribute) if the target doesn't support init priorities. */
5147 return error_mark_node;
5149 tree initp_expr = TREE_VALUE (args);
5150 tree decl = *node;
5151 tree type = TREE_TYPE (decl);
5152 int pri;
5154 STRIP_NOPS (initp_expr);
5155 initp_expr = default_conversion (initp_expr);
5156 if (initp_expr)
5157 initp_expr = maybe_constant_value (initp_expr);
5159 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
5161 error ("requested %<init_priority%> is not an integer constant");
5162 cxx_constant_value (initp_expr);
5163 *no_add_attrs = true;
5164 return NULL_TREE;
5167 pri = TREE_INT_CST_LOW (initp_expr);
5169 type = strip_array_types (type);
5171 if (decl == NULL_TREE
5172 || !VAR_P (decl)
5173 || !TREE_STATIC (decl)
5174 || DECL_EXTERNAL (decl)
5175 || (TREE_CODE (type) != RECORD_TYPE
5176 && TREE_CODE (type) != UNION_TYPE)
5177 /* Static objects in functions are initialized the
5178 first time control passes through that
5179 function. This is not precise enough to pin down an
5180 init_priority value, so don't allow it. */
5181 || current_function_decl)
5183 error ("can only use %qE attribute on file-scope definitions "
5184 "of objects of class type", name);
5185 *no_add_attrs = true;
5186 return NULL_TREE;
5189 if (pri > MAX_INIT_PRIORITY || pri <= 0)
5191 error ("requested %<init_priority%> %i is out of range [0, %i]",
5192 pri, MAX_INIT_PRIORITY);
5193 *no_add_attrs = true;
5194 return NULL_TREE;
5197 /* Check for init_priorities that are reserved for
5198 language and runtime support implementations.*/
5199 if (pri <= MAX_RESERVED_INIT_PRIORITY)
5201 warning
5202 (0, "requested %<init_priority%> %i is reserved for internal use",
5203 pri);
5206 SET_DECL_INIT_PRIORITY (decl, pri);
5207 DECL_HAS_INIT_PRIORITY_P (decl) = 1;
5208 return NULL_TREE;
5211 /* DECL is being redeclared; the old declaration had the abi tags in OLD,
5212 and the new one has the tags in NEW_. Give an error if there are tags
5213 in NEW_ that weren't in OLD. */
5215 bool
5216 check_abi_tag_redeclaration (const_tree decl, const_tree old, const_tree new_)
5218 if (old && TREE_CODE (TREE_VALUE (old)) == TREE_LIST)
5219 old = TREE_VALUE (old);
5220 if (new_ && TREE_CODE (TREE_VALUE (new_)) == TREE_LIST)
5221 new_ = TREE_VALUE (new_);
5222 bool err = false;
5223 for (const_tree t = new_; t; t = TREE_CHAIN (t))
5225 tree str = TREE_VALUE (t);
5226 for (const_tree in = old; in; in = TREE_CHAIN (in))
5228 tree ostr = TREE_VALUE (in);
5229 if (cp_tree_equal (str, ostr))
5230 goto found;
5232 error ("redeclaration of %qD adds abi tag %qE", decl, str);
5233 err = true;
5234 found:;
5236 if (err)
5238 inform (DECL_SOURCE_LOCATION (decl), "previous declaration here");
5239 return false;
5241 return true;
5244 /* The abi_tag attribute with the name NAME was given ARGS. If they are
5245 ill-formed, give an error and return false; otherwise, return true. */
5247 bool
5248 check_abi_tag_args (tree args, tree name)
5250 if (!args)
5252 error ("the %qE attribute requires arguments", name);
5253 return false;
5255 for (tree arg = args; arg; arg = TREE_CHAIN (arg))
5257 tree elt = TREE_VALUE (arg);
5258 if (TREE_CODE (elt) != STRING_CST
5259 || (!same_type_ignoring_top_level_qualifiers_p
5260 (strip_array_types (TREE_TYPE (elt)),
5261 char_type_node)))
5263 error ("arguments to the %qE attribute must be narrow string "
5264 "literals", name);
5265 return false;
5267 const char *begin = TREE_STRING_POINTER (elt);
5268 const char *end = begin + TREE_STRING_LENGTH (elt);
5269 for (const char *p = begin; p != end; ++p)
5271 char c = *p;
5272 if (p == begin)
5274 if (!ISALPHA (c) && c != '_')
5276 error ("arguments to the %qE attribute must contain valid "
5277 "identifiers", name);
5278 inform (input_location, "%<%c%> is not a valid first "
5279 "character for an identifier", c);
5280 return false;
5283 else if (p == end - 1)
5284 gcc_assert (c == 0);
5285 else
5287 if (!ISALNUM (c) && c != '_')
5289 error ("arguments to the %qE attribute must contain valid "
5290 "identifiers", name);
5291 inform (input_location, "%<%c%> is not a valid character "
5292 "in an identifier", c);
5293 return false;
5298 return true;
5301 /* Handle an "abi_tag" attribute; arguments as in
5302 struct attribute_spec.handler. */
5304 static tree
5305 handle_abi_tag_attribute (tree* node, tree name, tree args,
5306 int flags, bool* no_add_attrs)
5308 if (!check_abi_tag_args (args, name))
5309 goto fail;
5311 if (TYPE_P (*node))
5313 if (!OVERLOAD_TYPE_P (*node))
5315 error ("%qE attribute applied to non-class, non-enum type %qT",
5316 name, *node);
5317 goto fail;
5319 else if (!(flags & (int)ATTR_FLAG_TYPE_IN_PLACE))
5321 error ("%qE attribute applied to %qT after its definition",
5322 name, *node);
5323 goto fail;
5325 else if (CLASS_TYPE_P (*node)
5326 && CLASSTYPE_TEMPLATE_INSTANTIATION (*node))
5328 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
5329 "template instantiation %qT", name, *node);
5330 goto fail;
5332 else if (CLASS_TYPE_P (*node)
5333 && CLASSTYPE_TEMPLATE_SPECIALIZATION (*node))
5335 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
5336 "template specialization %qT", name, *node);
5337 goto fail;
5340 tree attributes = TYPE_ATTRIBUTES (*node);
5341 tree decl = TYPE_NAME (*node);
5343 /* Make sure all declarations have the same abi tags. */
5344 if (DECL_SOURCE_LOCATION (decl) != input_location)
5346 if (!check_abi_tag_redeclaration (decl,
5347 lookup_attribute ("abi_tag",
5348 attributes),
5349 args))
5350 goto fail;
5353 else
5355 if (!VAR_OR_FUNCTION_DECL_P (*node))
5357 error ("%qE attribute applied to non-function, non-variable %qD",
5358 name, *node);
5359 goto fail;
5361 else if (DECL_LANGUAGE (*node) == lang_c)
5363 error ("%qE attribute applied to extern \"C\" declaration %qD",
5364 name, *node);
5365 goto fail;
5369 return NULL_TREE;
5371 fail:
5372 *no_add_attrs = true;
5373 return NULL_TREE;
5376 /* Perform checking for contract attributes. */
5378 tree
5379 handle_contract_attribute (tree *ARG_UNUSED (node), tree ARG_UNUSED (name),
5380 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
5381 bool *ARG_UNUSED (no_add_attrs))
5383 /* TODO: Is there any checking we could do here? */
5384 return NULL_TREE;
5387 /* Handle a "no_dangling" attribute; arguments as in
5388 struct attribute_spec.handler. */
5390 tree
5391 handle_no_dangling_attribute (tree *node, tree name, tree args, int,
5392 bool *no_add_attrs)
5394 if (args && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
5396 error ("%qE attribute argument must be an expression that evaluates "
5397 "to true or false", name);
5398 *no_add_attrs = true;
5400 else if (!FUNC_OR_METHOD_TYPE_P (*node)
5401 && !RECORD_OR_UNION_TYPE_P (*node))
5403 warning (OPT_Wattributes, "%qE attribute ignored", name);
5404 *no_add_attrs = true;
5407 return NULL_TREE;
5410 /* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the
5411 thing pointed to by the constant. */
5413 tree
5414 make_ptrmem_cst (tree type, tree member)
5416 tree ptrmem_cst = make_node (PTRMEM_CST);
5417 TREE_TYPE (ptrmem_cst) = type;
5418 PTRMEM_CST_MEMBER (ptrmem_cst) = member;
5419 PTRMEM_CST_LOCATION (ptrmem_cst) = input_location;
5420 return ptrmem_cst;
5423 /* Build a variant of TYPE that has the indicated ATTRIBUTES. May
5424 return an existing type if an appropriate type already exists. */
5426 tree
5427 cp_build_type_attribute_variant (tree type, tree attributes)
5429 tree new_type;
5431 new_type = build_type_attribute_variant (type, attributes);
5432 if (FUNC_OR_METHOD_TYPE_P (new_type))
5433 gcc_checking_assert (cxx_type_hash_eq (type, new_type));
5435 /* Making a new main variant of a class type is broken. */
5436 gcc_assert (!CLASS_TYPE_P (type) || new_type == type);
5438 return new_type;
5441 /* Return TRUE if TYPE1 and TYPE2 are identical for type hashing purposes.
5442 Called only after doing all language independent checks. */
5444 bool
5445 cxx_type_hash_eq (const_tree typea, const_tree typeb)
5447 gcc_assert (FUNC_OR_METHOD_TYPE_P (typea));
5449 if (type_memfn_rqual (typea) != type_memfn_rqual (typeb))
5450 return false;
5451 if (TYPE_HAS_LATE_RETURN_TYPE (typea) != TYPE_HAS_LATE_RETURN_TYPE (typeb))
5452 return false;
5453 return comp_except_specs (TYPE_RAISES_EXCEPTIONS (typea),
5454 TYPE_RAISES_EXCEPTIONS (typeb), ce_exact);
5457 /* Copy the language-specific type variant modifiers from TYPEB to TYPEA. For
5458 C++, these are the exception-specifier and ref-qualifier. */
5460 tree
5461 cxx_copy_lang_qualifiers (const_tree typea, const_tree typeb)
5463 tree type = CONST_CAST_TREE (typea);
5464 if (FUNC_OR_METHOD_TYPE_P (type))
5465 type = build_cp_fntype_variant (type, type_memfn_rqual (typeb),
5466 TYPE_RAISES_EXCEPTIONS (typeb),
5467 TYPE_HAS_LATE_RETURN_TYPE (typeb));
5468 return type;
5471 /* Apply FUNC to all language-specific sub-trees of TP in a pre-order
5472 traversal. Called from walk_tree. */
5474 tree
5475 cp_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func,
5476 void *data, hash_set<tree> *pset)
5478 tree t = *tp;
5479 enum tree_code code = TREE_CODE (t);
5480 tree result;
5482 #define WALK_SUBTREE(NODE) \
5483 do \
5485 result = cp_walk_tree (&(NODE), func, data, pset); \
5486 if (result) goto out; \
5488 while (0)
5490 if (TYPE_P (t))
5492 /* If *WALK_SUBTREES_P is 1, we're interested in the syntactic form of
5493 the argument, so don't look through typedefs, but do walk into
5494 template arguments for alias templates (and non-typedefed classes).
5496 If *WALK_SUBTREES_P > 1, we're interested in type identity or
5497 equivalence, so look through typedefs, ignoring template arguments for
5498 alias templates, and walk into template args of classes.
5500 See find_abi_tags_r for an example of setting *WALK_SUBTREES_P to 2
5501 when that's the behavior the walk_tree_fn wants. */
5502 if (*walk_subtrees_p == 1 && typedef_variant_p (t))
5504 if (tree ti = TYPE_ALIAS_TEMPLATE_INFO (t))
5505 WALK_SUBTREE (TI_ARGS (ti));
5506 *walk_subtrees_p = 0;
5507 return NULL_TREE;
5510 if (tree ti = TYPE_TEMPLATE_INFO (t))
5511 WALK_SUBTREE (TI_ARGS (ti));
5514 /* Not one of the easy cases. We must explicitly go through the
5515 children. */
5516 result = NULL_TREE;
5517 switch (code)
5519 case TEMPLATE_TYPE_PARM:
5520 if (template_placeholder_p (t))
5521 WALK_SUBTREE (CLASS_PLACEHOLDER_TEMPLATE (t));
5522 /* Fall through. */
5523 case DEFERRED_PARSE:
5524 case TEMPLATE_TEMPLATE_PARM:
5525 case BOUND_TEMPLATE_TEMPLATE_PARM:
5526 case UNBOUND_CLASS_TEMPLATE:
5527 case TEMPLATE_PARM_INDEX:
5528 case TYPEOF_TYPE:
5529 /* None of these have subtrees other than those already walked
5530 above. */
5531 *walk_subtrees_p = 0;
5532 break;
5534 case TYPENAME_TYPE:
5535 WALK_SUBTREE (TYPE_CONTEXT (t));
5536 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
5537 *walk_subtrees_p = 0;
5538 break;
5540 case BASELINK:
5541 if (BASELINK_QUALIFIED_P (t))
5542 WALK_SUBTREE (BINFO_TYPE (BASELINK_ACCESS_BINFO (t)));
5543 WALK_SUBTREE (BASELINK_FUNCTIONS (t));
5544 *walk_subtrees_p = 0;
5545 break;
5547 case PTRMEM_CST:
5548 WALK_SUBTREE (TREE_TYPE (t));
5549 *walk_subtrees_p = 0;
5550 break;
5552 case TREE_LIST:
5553 WALK_SUBTREE (TREE_PURPOSE (t));
5554 break;
5556 case OVERLOAD:
5557 WALK_SUBTREE (OVL_FUNCTION (t));
5558 WALK_SUBTREE (OVL_CHAIN (t));
5559 *walk_subtrees_p = 0;
5560 break;
5562 case USING_DECL:
5563 WALK_SUBTREE (DECL_NAME (t));
5564 WALK_SUBTREE (USING_DECL_SCOPE (t));
5565 WALK_SUBTREE (USING_DECL_DECLS (t));
5566 *walk_subtrees_p = 0;
5567 break;
5569 case RECORD_TYPE:
5570 if (TYPE_PTRMEMFUNC_P (t))
5571 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (t));
5572 break;
5574 case TYPE_ARGUMENT_PACK:
5575 case NONTYPE_ARGUMENT_PACK:
5577 tree args = ARGUMENT_PACK_ARGS (t);
5578 for (tree arg : tree_vec_range (args))
5579 WALK_SUBTREE (arg);
5581 break;
5583 case TYPE_PACK_EXPANSION:
5584 WALK_SUBTREE (TREE_TYPE (t));
5585 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (t));
5586 *walk_subtrees_p = 0;
5587 break;
5589 case EXPR_PACK_EXPANSION:
5590 WALK_SUBTREE (TREE_OPERAND (t, 0));
5591 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (t));
5592 *walk_subtrees_p = 0;
5593 break;
5595 case CAST_EXPR:
5596 case REINTERPRET_CAST_EXPR:
5597 case STATIC_CAST_EXPR:
5598 case CONST_CAST_EXPR:
5599 case DYNAMIC_CAST_EXPR:
5600 case IMPLICIT_CONV_EXPR:
5601 case BIT_CAST_EXPR:
5602 if (TREE_TYPE (t))
5603 WALK_SUBTREE (TREE_TYPE (t));
5604 break;
5606 case CONSTRUCTOR:
5607 if (COMPOUND_LITERAL_P (t))
5608 WALK_SUBTREE (TREE_TYPE (t));
5609 break;
5611 case TRAIT_EXPR:
5612 WALK_SUBTREE (TRAIT_EXPR_TYPE1 (t));
5613 WALK_SUBTREE (TRAIT_EXPR_TYPE2 (t));
5614 *walk_subtrees_p = 0;
5615 break;
5617 case TRAIT_TYPE:
5618 WALK_SUBTREE (TRAIT_TYPE_TYPE1 (t));
5619 WALK_SUBTREE (TRAIT_TYPE_TYPE2 (t));
5620 *walk_subtrees_p = 0;
5621 break;
5623 case DECLTYPE_TYPE:
5625 cp_unevaluated u;
5626 WALK_SUBTREE (DECLTYPE_TYPE_EXPR (t));
5627 *walk_subtrees_p = 0;
5628 break;
5631 case ALIGNOF_EXPR:
5632 case SIZEOF_EXPR:
5633 case NOEXCEPT_EXPR:
5635 cp_unevaluated u;
5636 WALK_SUBTREE (TREE_OPERAND (t, 0));
5637 *walk_subtrees_p = 0;
5638 break;
5641 case REQUIRES_EXPR:
5643 cp_unevaluated u;
5644 for (tree parm = REQUIRES_EXPR_PARMS (t); parm; parm = DECL_CHAIN (parm))
5645 /* Walk the types of each parameter, but not the parameter itself,
5646 since doing so would cause false positives in the unexpanded pack
5647 checker if the requires-expr introduces a function parameter pack,
5648 e.g. requires (Ts... ts) { }. */
5649 WALK_SUBTREE (TREE_TYPE (parm));
5650 WALK_SUBTREE (REQUIRES_EXPR_REQS (t));
5651 *walk_subtrees_p = 0;
5652 break;
5655 case DECL_EXPR:
5656 /* User variables should be mentioned in BIND_EXPR_VARS
5657 and their initializers and sizes walked when walking
5658 the containing BIND_EXPR. Compiler temporaries are
5659 handled here. And also normal variables in templates,
5660 since do_poplevel doesn't build a BIND_EXPR then. */
5661 if (VAR_P (TREE_OPERAND (t, 0))
5662 && (processing_template_decl
5663 || (DECL_ARTIFICIAL (TREE_OPERAND (t, 0))
5664 && !TREE_STATIC (TREE_OPERAND (t, 0)))))
5666 tree decl = TREE_OPERAND (t, 0);
5667 WALK_SUBTREE (DECL_INITIAL (decl));
5668 WALK_SUBTREE (DECL_SIZE (decl));
5669 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
5671 break;
5673 case LAMBDA_EXPR:
5674 /* Don't walk into the body of the lambda, but the capture initializers
5675 are part of the enclosing context. */
5676 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
5677 cap = TREE_CHAIN (cap))
5678 WALK_SUBTREE (TREE_VALUE (cap));
5679 break;
5681 case CO_YIELD_EXPR:
5682 if (TREE_OPERAND (t, 1))
5683 /* Operand 1 is the tree for the relevant co_await which has any
5684 interesting sub-trees. */
5685 WALK_SUBTREE (TREE_OPERAND (t, 1));
5686 break;
5688 case CO_AWAIT_EXPR:
5689 if (TREE_OPERAND (t, 1))
5690 /* Operand 1 is frame variable. */
5691 WALK_SUBTREE (TREE_OPERAND (t, 1));
5692 if (TREE_OPERAND (t, 2))
5693 /* Operand 2 has the initialiser, and we need to walk any subtrees
5694 there. */
5695 WALK_SUBTREE (TREE_OPERAND (t, 2));
5696 break;
5698 case CO_RETURN_EXPR:
5699 if (TREE_OPERAND (t, 0))
5701 if (VOID_TYPE_P (TREE_OPERAND (t, 0)))
5702 /* For void expressions, operand 1 is a trivial call, and any
5703 interesting subtrees will be part of operand 0. */
5704 WALK_SUBTREE (TREE_OPERAND (t, 0));
5705 else if (TREE_OPERAND (t, 1))
5706 /* Interesting sub-trees will be in the return_value () call
5707 arguments. */
5708 WALK_SUBTREE (TREE_OPERAND (t, 1));
5710 break;
5712 case STATIC_ASSERT:
5713 WALK_SUBTREE (STATIC_ASSERT_CONDITION (t));
5714 WALK_SUBTREE (STATIC_ASSERT_MESSAGE (t));
5715 break;
5717 default:
5718 return NULL_TREE;
5721 /* We didn't find what we were looking for. */
5722 out:
5723 return result;
5725 #undef WALK_SUBTREE
5728 /* Like save_expr, but for C++. */
5730 tree
5731 cp_save_expr (tree expr)
5733 /* There is no reason to create a SAVE_EXPR within a template; if
5734 needed, we can create the SAVE_EXPR when instantiating the
5735 template. Furthermore, the middle-end cannot handle C++-specific
5736 tree codes. */
5737 if (processing_template_decl)
5738 return expr;
5740 /* TARGET_EXPRs are only expanded once. */
5741 if (TREE_CODE (expr) == TARGET_EXPR)
5742 return expr;
5744 return save_expr (expr);
5747 /* Initialize tree.cc. */
5749 void
5750 init_tree (void)
5752 list_hash_table = hash_table<list_hasher>::create_ggc (61);
5755 /* Returns the kind of special function that DECL (a FUNCTION_DECL)
5756 is. Note that sfk_none is zero, so this function can be used as a
5757 predicate to test whether or not DECL is a special function. */
5759 special_function_kind
5760 special_function_p (const_tree decl)
5762 /* Rather than doing all this stuff with magic names, we should
5763 probably have a field of type `special_function_kind' in
5764 DECL_LANG_SPECIFIC. */
5765 if (DECL_INHERITED_CTOR (decl))
5766 return sfk_inheriting_constructor;
5767 if (DECL_COPY_CONSTRUCTOR_P (decl))
5768 return sfk_copy_constructor;
5769 if (DECL_MOVE_CONSTRUCTOR_P (decl))
5770 return sfk_move_constructor;
5771 if (DECL_CONSTRUCTOR_P (decl))
5772 return sfk_constructor;
5773 if (DECL_ASSIGNMENT_OPERATOR_P (decl)
5774 && DECL_OVERLOADED_OPERATOR_IS (decl, NOP_EXPR))
5776 if (copy_fn_p (decl))
5777 return sfk_copy_assignment;
5778 if (move_fn_p (decl))
5779 return sfk_move_assignment;
5781 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
5782 return sfk_destructor;
5783 if (DECL_COMPLETE_DESTRUCTOR_P (decl))
5784 return sfk_complete_destructor;
5785 if (DECL_BASE_DESTRUCTOR_P (decl))
5786 return sfk_base_destructor;
5787 if (DECL_DELETING_DESTRUCTOR_P (decl))
5788 return sfk_deleting_destructor;
5789 if (DECL_CONV_FN_P (decl))
5790 return sfk_conversion;
5791 if (deduction_guide_p (decl))
5792 return sfk_deduction_guide;
5793 if (DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) >= OVL_OP_EQ_EXPR
5794 && DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) <= OVL_OP_SPACESHIP_EXPR)
5795 return sfk_comparison;
5797 return sfk_none;
5800 /* As above, but only if DECL is a special member function as per 11.3.3
5801 [special]: default/copy/move ctor, copy/move assignment, or destructor. */
5803 special_function_kind
5804 special_memfn_p (const_tree decl)
5806 switch (special_function_kind sfk = special_function_p (decl))
5808 case sfk_constructor:
5809 if (!default_ctor_p (decl))
5810 break;
5811 gcc_fallthrough();
5812 case sfk_copy_constructor:
5813 case sfk_copy_assignment:
5814 case sfk_move_assignment:
5815 case sfk_move_constructor:
5816 case sfk_destructor:
5817 return sfk;
5819 default:
5820 break;
5822 return sfk_none;
5825 /* Returns nonzero if TYPE is a character type, including wchar_t. */
5828 char_type_p (tree type)
5830 return (same_type_p (type, char_type_node)
5831 || same_type_p (type, unsigned_char_type_node)
5832 || same_type_p (type, signed_char_type_node)
5833 || same_type_p (type, char8_type_node)
5834 || same_type_p (type, char16_type_node)
5835 || same_type_p (type, char32_type_node)
5836 || same_type_p (type, wchar_type_node));
5839 /* Returns the kind of linkage associated with the indicated DECL. Th
5840 value returned is as specified by the language standard; it is
5841 independent of implementation details regarding template
5842 instantiation, etc. For example, it is possible that a declaration
5843 to which this function assigns external linkage would not show up
5844 as a global symbol when you run `nm' on the resulting object file. */
5846 linkage_kind
5847 decl_linkage (tree decl)
5849 /* This function doesn't attempt to calculate the linkage from first
5850 principles as given in [basic.link]. Instead, it makes use of
5851 the fact that we have already set TREE_PUBLIC appropriately, and
5852 then handles a few special cases. Ideally, we would calculate
5853 linkage first, and then transform that into a concrete
5854 implementation. */
5856 /* Things that don't have names have no linkage. */
5857 if (!DECL_NAME (decl))
5858 return lk_none;
5860 /* Fields have no linkage. */
5861 if (TREE_CODE (decl) == FIELD_DECL)
5862 return lk_none;
5864 /* Things in local scope do not have linkage. */
5865 if (decl_function_context (decl))
5866 return lk_none;
5868 /* Things that are TREE_PUBLIC have external linkage. */
5869 if (TREE_PUBLIC (decl))
5870 return lk_external;
5872 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor variants,
5873 check one of the "clones" for the real linkage. */
5874 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl)
5875 && DECL_CHAIN (decl)
5876 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)))
5877 return decl_linkage (DECL_CHAIN (decl));
5879 if (TREE_CODE (decl) == NAMESPACE_DECL)
5880 return lk_external;
5882 /* Linkage of a CONST_DECL depends on the linkage of the enumeration
5883 type. */
5884 if (TREE_CODE (decl) == CONST_DECL)
5885 return decl_linkage (TYPE_NAME (DECL_CONTEXT (decl)));
5887 /* Members of the anonymous namespace also have TREE_PUBLIC unset, but
5888 are considered to have external linkage for language purposes, as do
5889 template instantiations on targets without weak symbols. DECLs really
5890 meant to have internal linkage have DECL_THIS_STATIC set. */
5891 if (TREE_CODE (decl) == TYPE_DECL)
5892 return lk_external;
5893 if (VAR_OR_FUNCTION_DECL_P (decl))
5895 if (!DECL_THIS_STATIC (decl))
5896 return lk_external;
5898 /* Static data members and static member functions from classes
5899 in anonymous namespace also don't have TREE_PUBLIC set. */
5900 if (DECL_CLASS_CONTEXT (decl))
5901 return lk_external;
5904 /* Everything else has internal linkage. */
5905 return lk_internal;
5908 /* Returns the storage duration of the object or reference associated with
5909 the indicated DECL, which should be a VAR_DECL or PARM_DECL. */
5911 duration_kind
5912 decl_storage_duration (tree decl)
5914 if (TREE_CODE (decl) == PARM_DECL)
5915 return dk_auto;
5916 if (TREE_CODE (decl) == FUNCTION_DECL)
5917 return dk_static;
5918 gcc_assert (VAR_P (decl));
5919 if (!TREE_STATIC (decl)
5920 && !DECL_EXTERNAL (decl))
5921 return dk_auto;
5922 if (CP_DECL_THREAD_LOCAL_P (decl))
5923 return dk_thread;
5924 return dk_static;
5927 /* EXP is an expression that we want to pre-evaluate. Returns (in
5928 *INITP) an expression that will perform the pre-evaluation. The
5929 value returned by this function is a side-effect free expression
5930 equivalent to the pre-evaluated expression. Callers must ensure
5931 that *INITP is evaluated before EXP.
5933 Note that if EXPR is a glvalue, the return value is a glvalue denoting the
5934 same address; this function does not guard against modification of the
5935 stored value like save_expr or get_target_expr do. */
5937 tree
5938 stabilize_expr (tree exp, tree* initp)
5940 tree init_expr;
5942 if (!TREE_SIDE_EFFECTS (exp))
5943 init_expr = NULL_TREE;
5944 else if (VOID_TYPE_P (TREE_TYPE (exp)))
5946 init_expr = exp;
5947 exp = void_node;
5949 /* There are no expressions with REFERENCE_TYPE, but there can be call
5950 arguments with such a type; just treat it as a pointer. */
5951 else if (TYPE_REF_P (TREE_TYPE (exp))
5952 || SCALAR_TYPE_P (TREE_TYPE (exp))
5953 || !glvalue_p (exp))
5955 init_expr = get_target_expr (exp);
5956 exp = TARGET_EXPR_SLOT (init_expr);
5957 if (CLASS_TYPE_P (TREE_TYPE (exp)))
5958 exp = move (exp);
5959 else
5960 exp = rvalue (exp);
5962 else
5964 bool xval = !lvalue_p (exp);
5965 exp = cp_build_addr_expr (exp, tf_warning_or_error);
5966 init_expr = get_target_expr (exp);
5967 exp = TARGET_EXPR_SLOT (init_expr);
5968 exp = cp_build_fold_indirect_ref (exp);
5969 if (xval)
5970 exp = move (exp);
5972 *initp = init_expr;
5974 gcc_assert (!TREE_SIDE_EFFECTS (exp) || TREE_THIS_VOLATILE (exp));
5975 return exp;
5978 /* Add NEW_EXPR, an expression whose value we don't care about, after the
5979 similar expression ORIG. */
5981 tree
5982 add_stmt_to_compound (tree orig, tree new_expr)
5984 if (!new_expr || !TREE_SIDE_EFFECTS (new_expr))
5985 return orig;
5986 if (!orig || !TREE_SIDE_EFFECTS (orig))
5987 return new_expr;
5988 return build2 (COMPOUND_EXPR, void_type_node, orig, new_expr);
5991 /* Like stabilize_expr, but for a call whose arguments we want to
5992 pre-evaluate. CALL is modified in place to use the pre-evaluated
5993 arguments, while, upon return, *INITP contains an expression to
5994 compute the arguments. */
5996 void
5997 stabilize_call (tree call, tree *initp)
5999 tree inits = NULL_TREE;
6000 int i;
6001 int nargs = call_expr_nargs (call);
6003 if (call == error_mark_node || processing_template_decl)
6005 *initp = NULL_TREE;
6006 return;
6009 gcc_assert (TREE_CODE (call) == CALL_EXPR);
6011 for (i = 0; i < nargs; i++)
6013 tree init;
6014 CALL_EXPR_ARG (call, i) =
6015 stabilize_expr (CALL_EXPR_ARG (call, i), &init);
6016 inits = add_stmt_to_compound (inits, init);
6019 *initp = inits;
6022 /* Like stabilize_expr, but for an AGGR_INIT_EXPR whose arguments we want
6023 to pre-evaluate. CALL is modified in place to use the pre-evaluated
6024 arguments, while, upon return, *INITP contains an expression to
6025 compute the arguments. */
6027 static void
6028 stabilize_aggr_init (tree call, tree *initp)
6030 tree inits = NULL_TREE;
6031 int i;
6032 int nargs = aggr_init_expr_nargs (call);
6034 if (call == error_mark_node)
6035 return;
6037 gcc_assert (TREE_CODE (call) == AGGR_INIT_EXPR);
6039 for (i = 0; i < nargs; i++)
6041 tree init;
6042 AGGR_INIT_EXPR_ARG (call, i) =
6043 stabilize_expr (AGGR_INIT_EXPR_ARG (call, i), &init);
6044 inits = add_stmt_to_compound (inits, init);
6047 *initp = inits;
6050 /* Like stabilize_expr, but for an initialization.
6052 If the initialization is for an object of class type, this function
6053 takes care not to introduce additional temporaries.
6055 Returns TRUE iff the expression was successfully pre-evaluated,
6056 i.e., if INIT is now side-effect free, except for, possibly, a
6057 single call to a constructor. */
6059 bool
6060 stabilize_init (tree init, tree *initp)
6062 tree t = init;
6064 *initp = NULL_TREE;
6066 if (t == error_mark_node || processing_template_decl)
6067 return true;
6069 if (TREE_CODE (t) == INIT_EXPR)
6070 t = TREE_OPERAND (t, 1);
6071 if (TREE_CODE (t) == TARGET_EXPR)
6072 t = TARGET_EXPR_INITIAL (t);
6074 /* If the RHS can be stabilized without breaking copy elision, stabilize
6075 it. We specifically don't stabilize class prvalues here because that
6076 would mean an extra copy, but they might be stabilized below. */
6077 if (TREE_CODE (init) == INIT_EXPR
6078 && TREE_CODE (t) != CONSTRUCTOR
6079 && TREE_CODE (t) != AGGR_INIT_EXPR
6080 && (SCALAR_TYPE_P (TREE_TYPE (t))
6081 || glvalue_p (t)))
6083 TREE_OPERAND (init, 1) = stabilize_expr (t, initp);
6084 return true;
6087 if (TREE_CODE (t) == COMPOUND_EXPR
6088 && TREE_CODE (init) == INIT_EXPR)
6090 tree last = expr_last (t);
6091 /* Handle stabilizing the EMPTY_CLASS_EXPR pattern. */
6092 if (!TREE_SIDE_EFFECTS (last))
6094 *initp = t;
6095 TREE_OPERAND (init, 1) = last;
6096 return true;
6100 if (TREE_CODE (t) == CONSTRUCTOR)
6102 /* Aggregate initialization: stabilize each of the field
6103 initializers. */
6104 unsigned i;
6105 constructor_elt *ce;
6106 bool good = true;
6107 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
6108 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
6110 tree type = TREE_TYPE (ce->value);
6111 tree subinit;
6112 if (TYPE_REF_P (type)
6113 || SCALAR_TYPE_P (type))
6114 ce->value = stabilize_expr (ce->value, &subinit);
6115 else if (!stabilize_init (ce->value, &subinit))
6116 good = false;
6117 *initp = add_stmt_to_compound (*initp, subinit);
6119 return good;
6122 if (TREE_CODE (t) == CALL_EXPR)
6124 stabilize_call (t, initp);
6125 return true;
6128 if (TREE_CODE (t) == AGGR_INIT_EXPR)
6130 stabilize_aggr_init (t, initp);
6131 return true;
6134 /* The initialization is being performed via a bitwise copy -- and
6135 the item copied may have side effects. */
6136 return !TREE_SIDE_EFFECTS (init);
6139 /* Returns true if a cast to TYPE may appear in an integral constant
6140 expression. */
6142 bool
6143 cast_valid_in_integral_constant_expression_p (tree type)
6145 return (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6146 || cxx_dialect >= cxx11
6147 || dependent_type_p (type)
6148 || type == error_mark_node);
6151 /* Return true if we need to fix linkage information of DECL. */
6153 static bool
6154 cp_fix_function_decl_p (tree decl)
6156 /* Skip if DECL is not externally visible. */
6157 if (!TREE_PUBLIC (decl))
6158 return false;
6160 /* We need to fix DECL if it a appears to be exported but with no
6161 function body. Thunks do not have CFGs and we may need to
6162 handle them specially later. */
6163 if (!gimple_has_body_p (decl)
6164 && !DECL_THUNK_P (decl)
6165 && !DECL_EXTERNAL (decl))
6167 struct cgraph_node *node = cgraph_node::get (decl);
6169 /* Don't fix same_body aliases. Although they don't have their own
6170 CFG, they share it with what they alias to. */
6171 if (!node || !node->alias || !node->num_references ())
6172 return true;
6175 return false;
6178 /* Clean the C++ specific parts of the tree T. */
6180 void
6181 cp_free_lang_data (tree t)
6183 if (FUNC_OR_METHOD_TYPE_P (t))
6185 /* Default args are not interesting anymore. */
6186 tree argtypes = TYPE_ARG_TYPES (t);
6187 while (argtypes)
6189 TREE_PURPOSE (argtypes) = 0;
6190 argtypes = TREE_CHAIN (argtypes);
6193 else if (TREE_CODE (t) == FUNCTION_DECL
6194 && cp_fix_function_decl_p (t))
6196 /* If T is used in this translation unit at all, the definition
6197 must exist somewhere else since we have decided to not emit it
6198 in this TU. So make it an external reference. */
6199 DECL_EXTERNAL (t) = 1;
6200 TREE_STATIC (t) = 0;
6202 if (TREE_CODE (t) == NAMESPACE_DECL)
6203 /* We do not need the leftover chaining of namespaces from the
6204 binding level. */
6205 DECL_CHAIN (t) = NULL_TREE;
6208 /* Stub for c-common. Please keep in sync with c-decl.cc.
6209 FIXME: If address space support is target specific, then this
6210 should be a C target hook. But currently this is not possible,
6211 because this function is called via REGISTER_TARGET_PRAGMAS. */
6212 void
6213 c_register_addr_space (const char * /*word*/, addr_space_t /*as*/)
6217 /* Return the number of operands in T that we care about for things like
6218 mangling. */
6221 cp_tree_operand_length (const_tree t)
6223 enum tree_code code = TREE_CODE (t);
6225 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
6226 return VL_EXP_OPERAND_LENGTH (t);
6228 return cp_tree_code_length (code);
6231 /* Like cp_tree_operand_length, but takes a tree_code CODE. */
6234 cp_tree_code_length (enum tree_code code)
6236 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
6238 switch (code)
6240 case PREINCREMENT_EXPR:
6241 case PREDECREMENT_EXPR:
6242 case POSTINCREMENT_EXPR:
6243 case POSTDECREMENT_EXPR:
6244 return 1;
6246 case ARRAY_REF:
6247 return 2;
6249 case EXPR_PACK_EXPANSION:
6250 return 1;
6252 default:
6253 return TREE_CODE_LENGTH (code);
6257 /* Implement -Wzero_as_null_pointer_constant. Return true if the
6258 conditions for the warning hold, false otherwise. */
6259 bool
6260 maybe_warn_zero_as_null_pointer_constant (tree expr, location_t loc)
6262 if (c_inhibit_evaluation_warnings == 0
6263 && !null_node_p (expr) && !NULLPTR_TYPE_P (TREE_TYPE (expr)))
6265 warning_at (loc, OPT_Wzero_as_null_pointer_constant,
6266 "zero as null pointer constant");
6267 return true;
6269 return false;
6272 /* FNDECL is a function declaration whose type may have been altered by
6273 adding extra parameters such as this, in-charge, or VTT. When this
6274 takes place, the positional arguments supplied by the user (as in the
6275 'format' attribute arguments) may refer to the wrong argument. This
6276 function returns an integer indicating how many arguments should be
6277 skipped. */
6280 maybe_adjust_arg_pos_for_attribute (const_tree fndecl)
6282 if (!fndecl)
6283 return 0;
6284 int n = num_artificial_parms_for (fndecl);
6285 /* The manual states that it's the user's responsibility to account
6286 for the implicit this parameter. */
6287 return n > 0 ? n - 1 : 0;
6291 /* Release memory we no longer need after parsing. */
6292 void
6293 cp_tree_c_finish_parsing ()
6295 if (previous_class_level)
6296 invalidate_class_lookup_cache ();
6297 deleted_copy_types = NULL;
6300 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
6301 /* Complain that some language-specific thing hanging off a tree
6302 node has been accessed improperly. */
6304 void
6305 lang_check_failed (const char* file, int line, const char* function)
6307 internal_error ("%<lang_*%> check: failed in %s, at %s:%d",
6308 function, trim_filename (file), line);
6310 #endif /* ENABLE_TREE_CHECKING */
6312 #if CHECKING_P
6314 namespace selftest {
6316 /* Verify that lvalue_kind () works, for various expressions,
6317 and that location wrappers don't affect the results. */
6319 static void
6320 test_lvalue_kind ()
6322 location_t loc = BUILTINS_LOCATION;
6324 /* Verify constants and parameters, without and with
6325 location wrappers. */
6326 tree int_cst = build_int_cst (integer_type_node, 42);
6327 ASSERT_EQ (clk_none, lvalue_kind (int_cst));
6329 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
6330 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
6331 ASSERT_EQ (clk_none, lvalue_kind (wrapped_int_cst));
6333 tree string_lit = build_string (4, "foo");
6334 TREE_TYPE (string_lit) = char_array_type_node;
6335 string_lit = fix_string_type (string_lit);
6336 ASSERT_EQ (clk_ordinary, lvalue_kind (string_lit));
6338 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
6339 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
6340 ASSERT_EQ (clk_ordinary, lvalue_kind (wrapped_string_lit));
6342 tree parm = build_decl (UNKNOWN_LOCATION, PARM_DECL,
6343 get_identifier ("some_parm"),
6344 integer_type_node);
6345 ASSERT_EQ (clk_ordinary, lvalue_kind (parm));
6347 tree wrapped_parm = maybe_wrap_with_location (parm, loc);
6348 ASSERT_TRUE (location_wrapper_p (wrapped_parm));
6349 ASSERT_EQ (clk_ordinary, lvalue_kind (wrapped_parm));
6351 /* Verify that lvalue_kind of std::move on a parm isn't
6352 affected by location wrappers. */
6353 tree rvalue_ref_of_parm = move (parm);
6354 ASSERT_EQ (clk_rvalueref, lvalue_kind (rvalue_ref_of_parm));
6355 tree rvalue_ref_of_wrapped_parm = move (wrapped_parm);
6356 ASSERT_EQ (clk_rvalueref, lvalue_kind (rvalue_ref_of_wrapped_parm));
6358 /* Verify lvalue_p. */
6359 ASSERT_FALSE (lvalue_p (int_cst));
6360 ASSERT_FALSE (lvalue_p (wrapped_int_cst));
6361 ASSERT_TRUE (lvalue_p (parm));
6362 ASSERT_TRUE (lvalue_p (wrapped_parm));
6363 ASSERT_FALSE (lvalue_p (rvalue_ref_of_parm));
6364 ASSERT_FALSE (lvalue_p (rvalue_ref_of_wrapped_parm));
6367 /* Run all of the selftests within this file. */
6369 void
6370 cp_tree_cc_tests ()
6372 test_lvalue_kind ();
6375 } // namespace selftest
6377 #endif /* #if CHECKING_P */
6380 #include "gt-cp-tree.h"