libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / cp / tree.cc
blob0a7a56cc6e2eb4eb3cee6f7b8c5b16f942b90840
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 (typedef_variant_p (t))
1612 if ((flags & STF_USER_VISIBLE)
1613 && !user_facing_original_type_p (t))
1614 return t;
1616 if (dependent_opaque_alias_p (t))
1617 return t;
1619 if (alias_template_specialization_p (t, nt_opaque))
1621 if (dependent_alias_template_spec_p (t, nt_opaque)
1622 && !(flags & STF_STRIP_DEPENDENT))
1623 /* DR 1558: However, if the template-id is dependent, subsequent
1624 template argument substitution still applies to the template-id. */
1625 return t;
1627 else
1628 /* If T is a non-template alias or typedef, we can assume that
1629 instantiating its definition will hit any substitution failure,
1630 so we don't need to retain it here as well. */
1631 flags |= STF_STRIP_DEPENDENT;
1633 result = strip_typedefs (DECL_ORIGINAL_TYPE (TYPE_NAME (t)),
1634 remove_attributes, flags);
1635 goto stripped;
1638 switch (TREE_CODE (t))
1640 case POINTER_TYPE:
1641 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1642 result = build_pointer_type_for_mode (type, TYPE_MODE (t), false);
1643 break;
1644 case REFERENCE_TYPE:
1645 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1646 result = cp_build_reference_type_for_mode (type, TYPE_MODE (t), TYPE_REF_IS_RVALUE (t));
1647 break;
1648 case OFFSET_TYPE:
1649 t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t), remove_attributes, flags);
1650 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1651 result = build_offset_type (t0, type);
1652 break;
1653 case RECORD_TYPE:
1654 if (TYPE_PTRMEMFUNC_P (t))
1656 t0 = strip_typedefs (TYPE_PTRMEMFUNC_FN_TYPE (t),
1657 remove_attributes, flags);
1658 result = build_ptrmemfunc_type (t0);
1660 break;
1661 case ARRAY_TYPE:
1662 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1663 t0 = strip_typedefs (TYPE_DOMAIN (t), remove_attributes, flags);
1664 gcc_checking_assert (TYPE_DEPENDENT_P_VALID (t)
1665 || !dependent_type_p (t));
1666 result = build_cplus_array_type (type, t0, TYPE_DEPENDENT_P (t));
1667 break;
1668 case FUNCTION_TYPE:
1669 case METHOD_TYPE:
1671 tree arg_types = NULL, arg_node, arg_node2, arg_type;
1672 bool changed;
1674 /* Because we stomp on TREE_PURPOSE of TYPE_ARG_TYPES in many places
1675 around the compiler (e.g. cp_parser_late_parsing_default_args), we
1676 can't expect that re-hashing a function type will find a previous
1677 equivalent type, so try to reuse the input type if nothing has
1678 changed. If the type is itself a variant, that will change. */
1679 bool is_variant = typedef_variant_p (t);
1680 if (remove_attributes
1681 && (TYPE_ATTRIBUTES (t) || TYPE_USER_ALIGN (t)))
1682 is_variant = true;
1684 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1685 tree canon_spec = (flag_noexcept_type
1686 ? canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (t))
1687 : NULL_TREE);
1688 changed = (type != TREE_TYPE (t) || is_variant
1689 || TYPE_RAISES_EXCEPTIONS (t) != canon_spec);
1691 for (arg_node = TYPE_ARG_TYPES (t);
1692 arg_node;
1693 arg_node = TREE_CHAIN (arg_node))
1695 if (arg_node == void_list_node)
1696 break;
1697 arg_type = strip_typedefs (TREE_VALUE (arg_node),
1698 remove_attributes, flags);
1699 gcc_assert (arg_type);
1700 if (arg_type == TREE_VALUE (arg_node) && !changed)
1701 continue;
1703 if (!changed)
1705 changed = true;
1706 for (arg_node2 = TYPE_ARG_TYPES (t);
1707 arg_node2 != arg_node;
1708 arg_node2 = TREE_CHAIN (arg_node2))
1709 arg_types
1710 = tree_cons (TREE_PURPOSE (arg_node2),
1711 TREE_VALUE (arg_node2), arg_types);
1714 arg_types
1715 = tree_cons (TREE_PURPOSE (arg_node), arg_type, arg_types);
1718 if (!changed)
1719 return t;
1721 if (arg_types)
1722 arg_types = nreverse (arg_types);
1724 /* A list of parameters not ending with an ellipsis
1725 must end with void_list_node. */
1726 if (arg_node)
1727 arg_types = chainon (arg_types, void_list_node);
1729 if (TREE_CODE (t) == METHOD_TYPE)
1731 tree class_type = TREE_TYPE (TREE_VALUE (arg_types));
1732 gcc_assert (class_type);
1733 result =
1734 build_method_type_directly (class_type, type,
1735 TREE_CHAIN (arg_types));
1737 else
1739 result = build_function_type (type, arg_types);
1740 result = apply_memfn_quals (result, type_memfn_quals (t));
1743 result = build_cp_fntype_variant (result,
1744 type_memfn_rqual (t), canon_spec,
1745 TYPE_HAS_LATE_RETURN_TYPE (t));
1747 break;
1748 case TYPENAME_TYPE:
1750 bool changed = false;
1751 tree fullname = TYPENAME_TYPE_FULLNAME (t);
1752 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
1753 && TREE_OPERAND (fullname, 1))
1755 tree args = TREE_OPERAND (fullname, 1);
1756 tree new_args = copy_node (args);
1757 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
1759 tree arg = TREE_VEC_ELT (args, i);
1760 tree strip_arg = strip_typedefs (arg, remove_attributes, flags);
1761 TREE_VEC_ELT (new_args, i) = strip_arg;
1762 if (strip_arg != arg)
1763 changed = true;
1765 if (changed)
1767 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_args)
1768 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
1769 fullname
1770 = lookup_template_function (TREE_OPERAND (fullname, 0),
1771 new_args);
1773 else
1774 ggc_free (new_args);
1776 tree ctx = strip_typedefs (TYPE_CONTEXT (t), remove_attributes, flags);
1777 if (!changed && ctx == TYPE_CONTEXT (t) && !typedef_variant_p (t))
1778 return t;
1779 tree name = fullname;
1780 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
1781 name = TREE_OPERAND (fullname, 0);
1782 /* Use build_typename_type rather than make_typename_type because we
1783 don't want to resolve it here, just strip typedefs. */
1784 result = build_typename_type (ctx, name, fullname, typename_type);
1786 break;
1787 case DECLTYPE_TYPE:
1788 result = strip_typedefs_expr (DECLTYPE_TYPE_EXPR (t),
1789 remove_attributes, flags);
1790 if (result == DECLTYPE_TYPE_EXPR (t))
1791 result = NULL_TREE;
1792 else
1793 result = (finish_decltype_type
1794 (result,
1795 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t),
1796 tf_none));
1797 break;
1798 case TRAIT_TYPE:
1800 tree type1 = strip_typedefs (TRAIT_TYPE_TYPE1 (t),
1801 remove_attributes, flags);
1802 tree type2 = strip_typedefs (TRAIT_TYPE_TYPE2 (t),
1803 remove_attributes, flags);
1804 if (type1 == TRAIT_TYPE_TYPE1 (t) && type2 == TRAIT_TYPE_TYPE2 (t))
1805 result = NULL_TREE;
1806 else
1807 result = finish_trait_type (TRAIT_TYPE_KIND (t), type1, type2,
1808 tf_warning_or_error);
1810 break;
1811 case TYPE_PACK_EXPANSION:
1813 tree pat = PACK_EXPANSION_PATTERN (t);
1814 if (TYPE_P (pat))
1816 type = strip_typedefs (pat, remove_attributes, flags);
1817 if (type != pat)
1819 result = build_distinct_type_copy (t);
1820 PACK_EXPANSION_PATTERN (result) = type;
1824 break;
1825 default:
1826 break;
1829 if (!result)
1830 result = TYPE_MAIN_VARIANT (t);
1832 stripped:
1833 /*gcc_assert (!typedef_variant_p (result)
1834 || dependent_alias_template_spec_p (result, nt_opaque)
1835 || ((flags & STF_USER_VISIBLE)
1836 && !user_facing_original_type_p (result)));*/
1838 if (COMPLETE_TYPE_P (result) && !COMPLETE_TYPE_P (t))
1839 /* If RESULT is complete and T isn't, it's likely the case that T
1840 is a variant of RESULT which hasn't been updated yet. Skip the
1841 attribute handling. */;
1842 else
1844 if (TYPE_USER_ALIGN (t) != TYPE_USER_ALIGN (result)
1845 || TYPE_ALIGN (t) != TYPE_ALIGN (result))
1847 gcc_assert (TYPE_USER_ALIGN (t));
1848 if (remove_attributes)
1849 *remove_attributes = true;
1850 else
1852 if (TYPE_ALIGN (t) == TYPE_ALIGN (result))
1853 result = build_variant_type_copy (result);
1854 else
1855 result = build_aligned_type (result, TYPE_ALIGN (t));
1856 TYPE_USER_ALIGN (result) = true;
1860 if (TYPE_ATTRIBUTES (t))
1862 if (remove_attributes)
1863 result = apply_identity_attributes (result, TYPE_ATTRIBUTES (t),
1864 remove_attributes);
1865 else
1866 result = cp_build_type_attribute_variant (result,
1867 TYPE_ATTRIBUTES (t));
1871 return cp_build_qualified_type (result, cp_type_quals (t));
1874 /* Like strip_typedefs above, but works on expressions (and other
1875 non-types such as TREE_VEC), so that in
1877 template<class T> struct A
1879 typedef T TT;
1880 B<sizeof(TT)> b;
1883 sizeof(TT) is replaced by sizeof(T). */
1885 tree
1886 strip_typedefs_expr (tree t, bool *remove_attributes, unsigned int flags)
1888 unsigned i,n;
1889 tree r, type, *ops;
1890 enum tree_code code;
1892 if (t == NULL_TREE || t == error_mark_node)
1893 return t;
1895 STRIP_ANY_LOCATION_WRAPPER (t);
1897 if (DECL_P (t) || CONSTANT_CLASS_P (t))
1898 return t;
1900 code = TREE_CODE (t);
1901 switch (code)
1903 case IDENTIFIER_NODE:
1904 case TEMPLATE_PARM_INDEX:
1905 case OVERLOAD:
1906 case BASELINK:
1907 case ARGUMENT_PACK_SELECT:
1908 return t;
1910 case TRAIT_EXPR:
1912 tree type1 = strip_typedefs (TRAIT_EXPR_TYPE1 (t),
1913 remove_attributes, flags);
1914 tree type2 = strip_typedefs (TRAIT_EXPR_TYPE2 (t),
1915 remove_attributes, flags);
1916 if (type1 == TRAIT_EXPR_TYPE1 (t)
1917 && type2 == TRAIT_EXPR_TYPE2 (t))
1918 return t;
1919 r = copy_node (t);
1920 TRAIT_EXPR_TYPE1 (r) = type1;
1921 TRAIT_EXPR_TYPE2 (r) = type2;
1922 return r;
1925 case TREE_LIST:
1927 bool changed = false;
1928 auto_vec<tree_pair, 4> vec;
1929 r = t;
1930 for (; t; t = TREE_CHAIN (t))
1932 tree purpose = strip_typedefs (TREE_PURPOSE (t),
1933 remove_attributes, flags);
1934 tree value = strip_typedefs (TREE_VALUE (t),
1935 remove_attributes, flags);
1936 if (purpose != TREE_PURPOSE (t) || value != TREE_VALUE (t))
1937 changed = true;
1938 vec.safe_push ({purpose, value});
1940 if (changed)
1942 r = NULL_TREE;
1943 for (int i = vec.length () - 1; i >= 0; i--)
1944 r = tree_cons (vec[i].first, vec[i].second, r);
1946 return r;
1949 case TREE_VEC:
1951 bool changed = false;
1952 releasing_vec vec;
1953 n = TREE_VEC_LENGTH (t);
1954 vec_safe_reserve (vec, n);
1955 for (i = 0; i < n; ++i)
1957 tree op = strip_typedefs (TREE_VEC_ELT (t, i),
1958 remove_attributes, flags);
1959 vec->quick_push (op);
1960 if (op != TREE_VEC_ELT (t, i))
1961 changed = true;
1963 if (changed)
1965 r = copy_node (t);
1966 for (i = 0; i < n; ++i)
1967 TREE_VEC_ELT (r, i) = (*vec)[i];
1968 NON_DEFAULT_TEMPLATE_ARGS_COUNT (r)
1969 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
1971 else
1972 r = t;
1973 return r;
1976 case CONSTRUCTOR:
1978 bool changed = false;
1979 vec<constructor_elt, va_gc> *vec
1980 = vec_safe_copy (CONSTRUCTOR_ELTS (t));
1981 n = CONSTRUCTOR_NELTS (t);
1982 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1983 for (i = 0; i < n; ++i)
1985 constructor_elt *e = &(*vec)[i];
1986 tree op = strip_typedefs (e->value, remove_attributes, flags);
1987 if (op != e->value)
1989 changed = true;
1990 e->value = op;
1992 gcc_checking_assert
1993 (e->index == strip_typedefs (e->index, remove_attributes,
1994 flags));
1997 if (!changed && type == TREE_TYPE (t))
1999 vec_free (vec);
2000 return t;
2002 else
2004 r = copy_node (t);
2005 TREE_TYPE (r) = type;
2006 CONSTRUCTOR_ELTS (r) = vec;
2007 return r;
2011 case LAMBDA_EXPR:
2012 case STMT_EXPR:
2013 return t;
2015 default:
2016 break;
2019 gcc_assert (EXPR_P (t));
2021 n = cp_tree_operand_length (t);
2022 ops = XALLOCAVEC (tree, n);
2023 type = TREE_TYPE (t);
2025 switch (code)
2027 CASE_CONVERT:
2028 case IMPLICIT_CONV_EXPR:
2029 case DYNAMIC_CAST_EXPR:
2030 case STATIC_CAST_EXPR:
2031 case CONST_CAST_EXPR:
2032 case REINTERPRET_CAST_EXPR:
2033 case CAST_EXPR:
2034 case NEW_EXPR:
2035 type = strip_typedefs (type, remove_attributes, flags);
2036 /* fallthrough */
2038 default:
2039 for (i = 0; i < n; ++i)
2040 ops[i] = strip_typedefs (TREE_OPERAND (t, i),
2041 remove_attributes, flags);
2042 break;
2045 /* If nothing changed, return t. */
2046 for (i = 0; i < n; ++i)
2047 if (ops[i] != TREE_OPERAND (t, i))
2048 break;
2049 if (i == n && type == TREE_TYPE (t))
2050 return t;
2052 r = copy_node (t);
2053 TREE_TYPE (r) = type;
2054 for (i = 0; i < n; ++i)
2055 TREE_OPERAND (r, i) = ops[i];
2056 return r;
2059 /* Makes a copy of BINFO and TYPE, which is to be inherited into a
2060 graph dominated by T. If BINFO is NULL, TYPE is a dependent base,
2061 and we do a shallow copy. If BINFO is non-NULL, we do a deep copy.
2062 VIRT indicates whether TYPE is inherited virtually or not.
2063 IGO_PREV points at the previous binfo of the inheritance graph
2064 order chain. The newly copied binfo's TREE_CHAIN forms this
2065 ordering.
2067 The CLASSTYPE_VBASECLASSES vector of T is constructed in the
2068 correct order. That is in the order the bases themselves should be
2069 constructed in.
2071 The BINFO_INHERITANCE of a virtual base class points to the binfo
2072 of the most derived type. ??? We could probably change this so that
2073 BINFO_INHERITANCE becomes synonymous with BINFO_PRIMARY, and hence
2074 remove a field. They currently can only differ for primary virtual
2075 virtual bases. */
2077 tree
2078 copy_binfo (tree binfo, tree type, tree t, tree *igo_prev, int virt)
2080 tree new_binfo;
2082 if (virt)
2084 /* See if we've already made this virtual base. */
2085 new_binfo = binfo_for_vbase (type, t);
2086 if (new_binfo)
2087 return new_binfo;
2090 new_binfo = make_tree_binfo (binfo ? BINFO_N_BASE_BINFOS (binfo) : 0);
2091 BINFO_TYPE (new_binfo) = type;
2093 /* Chain it into the inheritance graph. */
2094 TREE_CHAIN (*igo_prev) = new_binfo;
2095 *igo_prev = new_binfo;
2097 if (binfo && !BINFO_DEPENDENT_BASE_P (binfo))
2099 int ix;
2100 tree base_binfo;
2102 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), type));
2104 BINFO_OFFSET (new_binfo) = BINFO_OFFSET (binfo);
2105 BINFO_VIRTUALS (new_binfo) = BINFO_VIRTUALS (binfo);
2107 /* We do not need to copy the accesses, as they are read only. */
2108 BINFO_BASE_ACCESSES (new_binfo) = BINFO_BASE_ACCESSES (binfo);
2110 /* Recursively copy base binfos of BINFO. */
2111 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2113 tree new_base_binfo;
2114 new_base_binfo = copy_binfo (base_binfo, BINFO_TYPE (base_binfo),
2115 t, igo_prev,
2116 BINFO_VIRTUAL_P (base_binfo));
2118 if (!BINFO_INHERITANCE_CHAIN (new_base_binfo))
2119 BINFO_INHERITANCE_CHAIN (new_base_binfo) = new_binfo;
2120 BINFO_BASE_APPEND (new_binfo, new_base_binfo);
2123 else
2124 BINFO_DEPENDENT_BASE_P (new_binfo) = 1;
2126 if (virt)
2128 /* Push it onto the list after any virtual bases it contains
2129 will have been pushed. */
2130 CLASSTYPE_VBASECLASSES (t)->quick_push (new_binfo);
2131 BINFO_VIRTUAL_P (new_binfo) = 1;
2132 BINFO_INHERITANCE_CHAIN (new_binfo) = TYPE_BINFO (t);
2135 return new_binfo;
2138 /* Hashing of lists so that we don't make duplicates.
2139 The entry point is `list_hash_canon'. */
2141 struct list_proxy
2143 tree purpose;
2144 tree value;
2145 tree chain;
2148 struct list_hasher : ggc_ptr_hash<tree_node>
2150 typedef list_proxy *compare_type;
2152 static hashval_t hash (tree);
2153 static bool equal (tree, list_proxy *);
2156 /* Now here is the hash table. When recording a list, it is added
2157 to the slot whose index is the hash code mod the table size.
2158 Note that the hash table is used for several kinds of lists.
2159 While all these live in the same table, they are completely independent,
2160 and the hash code is computed differently for each of these. */
2162 static GTY (()) hash_table<list_hasher> *list_hash_table;
2164 /* Compare ENTRY (an entry in the hash table) with DATA (a list_proxy
2165 for a node we are thinking about adding). */
2167 bool
2168 list_hasher::equal (tree t, list_proxy *proxy)
2170 return (TREE_VALUE (t) == proxy->value
2171 && TREE_PURPOSE (t) == proxy->purpose
2172 && TREE_CHAIN (t) == proxy->chain);
2175 /* Compute a hash code for a list (chain of TREE_LIST nodes
2176 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
2177 TREE_COMMON slots), by adding the hash codes of the individual entries. */
2179 static hashval_t
2180 list_hash_pieces (tree purpose, tree value, tree chain)
2182 hashval_t hashcode = 0;
2184 if (chain)
2185 hashcode += TREE_HASH (chain);
2187 if (value)
2188 hashcode += TREE_HASH (value);
2189 else
2190 hashcode += 1007;
2191 if (purpose)
2192 hashcode += TREE_HASH (purpose);
2193 else
2194 hashcode += 1009;
2195 return hashcode;
2198 /* Hash an already existing TREE_LIST. */
2200 hashval_t
2201 list_hasher::hash (tree t)
2203 return list_hash_pieces (TREE_PURPOSE (t),
2204 TREE_VALUE (t),
2205 TREE_CHAIN (t));
2208 /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
2209 object for an identical list if one already exists. Otherwise, build a
2210 new one, and record it as the canonical object. */
2212 tree
2213 hash_tree_cons (tree purpose, tree value, tree chain)
2215 int hashcode = 0;
2216 tree *slot;
2217 struct list_proxy proxy;
2219 /* Hash the list node. */
2220 hashcode = list_hash_pieces (purpose, value, chain);
2221 /* Create a proxy for the TREE_LIST we would like to create. We
2222 don't actually create it so as to avoid creating garbage. */
2223 proxy.purpose = purpose;
2224 proxy.value = value;
2225 proxy.chain = chain;
2226 /* See if it is already in the table. */
2227 slot = list_hash_table->find_slot_with_hash (&proxy, hashcode, INSERT);
2228 /* If not, create a new node. */
2229 if (!*slot)
2230 *slot = tree_cons (purpose, value, chain);
2231 return (tree) *slot;
2234 /* Constructor for hashed lists. */
2236 tree
2237 hash_tree_chain (tree value, tree chain)
2239 return hash_tree_cons (NULL_TREE, value, chain);
2242 void
2243 debug_binfo (tree elem)
2245 HOST_WIDE_INT n;
2246 tree virtuals;
2248 fprintf (stderr, "type \"%s\", offset = " HOST_WIDE_INT_PRINT_DEC
2249 "\nvtable type:\n",
2250 TYPE_NAME_STRING (BINFO_TYPE (elem)),
2251 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
2252 debug_tree (BINFO_TYPE (elem));
2253 if (BINFO_VTABLE (elem))
2254 fprintf (stderr, "vtable decl \"%s\"\n",
2255 IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem))));
2256 else
2257 fprintf (stderr, "no vtable decl yet\n");
2258 fprintf (stderr, "virtuals:\n");
2259 virtuals = BINFO_VIRTUALS (elem);
2260 n = 0;
2262 while (virtuals)
2264 tree fndecl = TREE_VALUE (virtuals);
2265 fprintf (stderr, "%s [" HOST_WIDE_INT_PRINT_DEC " =? "
2266 HOST_WIDE_INT_PRINT_DEC "]\n",
2267 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
2268 n, TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
2269 ++n;
2270 virtuals = TREE_CHAIN (virtuals);
2274 /* Build a representation for the qualified name SCOPE::NAME. TYPE is
2275 the type of the result expression, if known, or NULL_TREE if the
2276 resulting expression is type-dependent. If TEMPLATE_P is true,
2277 NAME is known to be a template because the user explicitly used the
2278 "template" keyword after the "::".
2280 All SCOPE_REFs should be built by use of this function. */
2282 tree
2283 build_qualified_name (tree type, tree scope, tree name, bool template_p)
2285 tree t;
2286 if (type == error_mark_node
2287 || scope == error_mark_node
2288 || name == error_mark_node)
2289 return error_mark_node;
2290 gcc_assert (TREE_CODE (name) != SCOPE_REF);
2291 t = build2 (SCOPE_REF, type, scope, name);
2292 QUALIFIED_NAME_IS_TEMPLATE (t) = template_p;
2293 PTRMEM_OK_P (t) = true;
2294 if (type)
2295 t = convert_from_reference (t);
2296 return t;
2299 /* Like check_qualified_type, but also check ref-qualifier, exception
2300 specification, and whether the return type was specified after the
2301 parameters. */
2303 static bool
2304 cp_check_qualified_type (const_tree cand, const_tree base, int type_quals,
2305 cp_ref_qualifier rqual, tree raises, bool late)
2307 return (TYPE_QUALS (cand) == type_quals
2308 && check_base_type (cand, base)
2309 && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (cand),
2310 ce_exact)
2311 && TYPE_HAS_LATE_RETURN_TYPE (cand) == late
2312 && type_memfn_rqual (cand) == rqual);
2315 /* Build the FUNCTION_TYPE or METHOD_TYPE with the ref-qualifier RQUAL. */
2317 tree
2318 build_ref_qualified_type (tree type, cp_ref_qualifier rqual)
2320 tree raises = TYPE_RAISES_EXCEPTIONS (type);
2321 bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
2322 return build_cp_fntype_variant (type, rqual, raises, late);
2325 tree
2326 make_binding_vec (tree name, unsigned clusters MEM_STAT_DECL)
2328 /* Stored in an unsigned short, but we're limited to the number of
2329 modules anyway. */
2330 gcc_checking_assert (clusters <= (unsigned short)(~0));
2331 size_t length = (offsetof (tree_binding_vec, vec)
2332 + clusters * sizeof (binding_cluster));
2333 tree vec = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2334 TREE_SET_CODE (vec, BINDING_VECTOR);
2335 BINDING_VECTOR_NAME (vec) = name;
2336 BINDING_VECTOR_ALLOC_CLUSTERS (vec) = clusters;
2337 BINDING_VECTOR_NUM_CLUSTERS (vec) = 0;
2339 return vec;
2342 /* Make a raw overload node containing FN. */
2344 tree
2345 ovl_make (tree fn, tree next)
2347 tree result = make_node (OVERLOAD);
2349 if (TREE_CODE (fn) == OVERLOAD)
2350 OVL_NESTED_P (result) = true;
2352 TREE_TYPE (result) = (next || TREE_CODE (fn) == TEMPLATE_DECL
2353 ? unknown_type_node : TREE_TYPE (fn));
2354 if (next && TREE_CODE (next) == OVERLOAD && OVL_DEDUP_P (next))
2355 OVL_DEDUP_P (result) = true;
2356 OVL_FUNCTION (result) = fn;
2357 OVL_CHAIN (result) = next;
2358 return result;
2361 /* Add FN to the (potentially NULL) overload set OVL. USING_OR_HIDDEN is > 0
2362 if this is a using-decl. It is > 1 if we're revealing the using decl.
2363 It is > 2 if we're also exporting it. USING_OR_HIDDEN is < 0, if FN is
2364 hidden. (A decl cannot be both using and hidden.) We keep the hidden
2365 decls first, but remaining ones are unordered. */
2367 tree
2368 ovl_insert (tree fn, tree maybe_ovl, int using_or_hidden)
2370 tree result = maybe_ovl;
2371 tree insert_after = NULL_TREE;
2373 /* Skip hidden. */
2374 for (; maybe_ovl && TREE_CODE (maybe_ovl) == OVERLOAD
2375 && OVL_HIDDEN_P (maybe_ovl);
2376 maybe_ovl = OVL_CHAIN (maybe_ovl))
2378 gcc_checking_assert (!OVL_LOOKUP_P (maybe_ovl));
2379 insert_after = maybe_ovl;
2382 if (maybe_ovl || using_or_hidden || TREE_CODE (fn) == TEMPLATE_DECL)
2384 maybe_ovl = ovl_make (fn, maybe_ovl);
2386 if (using_or_hidden < 0)
2387 OVL_HIDDEN_P (maybe_ovl) = true;
2388 if (using_or_hidden > 0)
2390 OVL_DEDUP_P (maybe_ovl) = OVL_USING_P (maybe_ovl) = true;
2391 if (using_or_hidden > 1)
2392 OVL_PURVIEW_P (maybe_ovl) = true;
2393 if (using_or_hidden > 2)
2394 OVL_EXPORT_P (maybe_ovl) = true;
2397 else
2398 maybe_ovl = fn;
2400 if (insert_after)
2402 OVL_CHAIN (insert_after) = maybe_ovl;
2403 TREE_TYPE (insert_after) = unknown_type_node;
2405 else
2406 result = maybe_ovl;
2408 return result;
2411 /* Skip any hidden names at the beginning of OVL. */
2413 tree
2414 ovl_skip_hidden (tree ovl)
2416 while (ovl && TREE_CODE (ovl) == OVERLOAD && OVL_HIDDEN_P (ovl))
2417 ovl = OVL_CHAIN (ovl);
2419 return ovl;
2422 /* NODE is an OVL_HIDDEN_P node that is now revealed. */
2424 tree
2425 ovl_iterator::reveal_node (tree overload, tree node)
2427 /* We cannot have returned NODE as part of a lookup overload, so we
2428 don't have to worry about preserving that. */
2430 OVL_HIDDEN_P (node) = false;
2431 if (tree chain = OVL_CHAIN (node))
2432 if (TREE_CODE (chain) == OVERLOAD)
2434 if (OVL_HIDDEN_P (chain))
2436 /* The node needs moving, and the simplest way is to remove it
2437 and reinsert. */
2438 overload = remove_node (overload, node);
2439 overload = ovl_insert (OVL_FUNCTION (node), overload);
2441 else if (OVL_DEDUP_P (chain))
2442 OVL_DEDUP_P (node) = true;
2444 return overload;
2447 /* NODE is on the overloads of OVL. Remove it.
2448 The removed node is unaltered and may continue to be iterated
2449 from (i.e. it is safe to remove a node from an overload one is
2450 currently iterating over). */
2452 tree
2453 ovl_iterator::remove_node (tree overload, tree node)
2455 tree *slot = &overload;
2456 while (*slot != node)
2458 tree probe = *slot;
2459 gcc_checking_assert (!OVL_LOOKUP_P (probe));
2461 slot = &OVL_CHAIN (probe);
2464 /* Stitch out NODE. We don't have to worry about now making a
2465 singleton overload (and consequently maybe setting its type),
2466 because all uses of this function will be followed by inserting a
2467 new node that must follow the place we've cut this out from. */
2468 if (TREE_CODE (node) != OVERLOAD)
2469 /* Cloned inherited ctors don't mark themselves as via_using. */
2470 *slot = NULL_TREE;
2471 else
2472 *slot = OVL_CHAIN (node);
2474 return overload;
2477 /* Mark or unmark a lookup set. */
2479 void
2480 lookup_mark (tree ovl, bool val)
2482 for (lkp_iterator iter (ovl); iter; ++iter)
2484 gcc_checking_assert (LOOKUP_SEEN_P (*iter) != val);
2485 LOOKUP_SEEN_P (*iter) = val;
2489 /* Add a set of new FNS into a lookup. */
2491 tree
2492 lookup_add (tree fns, tree lookup)
2494 if (fns == error_mark_node || lookup == error_mark_node)
2495 return error_mark_node;
2497 if (lookup || TREE_CODE (fns) == TEMPLATE_DECL)
2499 lookup = ovl_make (fns, lookup);
2500 OVL_LOOKUP_P (lookup) = true;
2502 else
2503 lookup = fns;
2505 return lookup;
2508 /* FNS is a new overload set, add them to LOOKUP, if they are not
2509 already present there. */
2511 tree
2512 lookup_maybe_add (tree fns, tree lookup, bool deduping)
2514 if (deduping)
2515 for (tree next, probe = fns; probe; probe = next)
2517 tree fn = probe;
2518 next = NULL_TREE;
2520 if (TREE_CODE (probe) == OVERLOAD)
2522 fn = OVL_FUNCTION (probe);
2523 next = OVL_CHAIN (probe);
2526 if (!LOOKUP_SEEN_P (fn))
2527 LOOKUP_SEEN_P (fn) = true;
2528 else
2530 /* This function was already seen. Insert all the
2531 predecessors onto the lookup. */
2532 for (; fns != probe; fns = OVL_CHAIN (fns))
2534 /* Propagate OVL_USING, but OVL_HIDDEN &
2535 OVL_DEDUP_P don't matter. */
2536 if (OVL_USING_P (fns))
2538 lookup = ovl_make (OVL_FUNCTION (fns), lookup);
2539 OVL_USING_P (lookup) = true;
2541 else
2542 lookup = lookup_add (OVL_FUNCTION (fns), lookup);
2545 /* And now skip this function. */
2546 fns = next;
2550 if (fns)
2551 /* We ended in a set of new functions. Add them all in one go. */
2552 lookup = lookup_add (fns, lookup);
2554 return lookup;
2557 /* Returns nonzero if X is an expression for a (possibly overloaded)
2558 function. If "f" is a function or function template, "f", "c->f",
2559 "c.f", "C::f", and "f<int>" will all be considered possibly
2560 overloaded functions. Returns 2 if the function is actually
2561 overloaded, i.e., if it is impossible to know the type of the
2562 function without performing overload resolution. */
2565 is_overloaded_fn (tree x)
2567 STRIP_ANY_LOCATION_WRAPPER (x);
2569 /* A baselink is also considered an overloaded function. */
2570 if (TREE_CODE (x) == OFFSET_REF
2571 || TREE_CODE (x) == COMPONENT_REF)
2572 x = TREE_OPERAND (x, 1);
2573 x = MAYBE_BASELINK_FUNCTIONS (x);
2574 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
2575 x = TREE_OPERAND (x, 0);
2577 if (DECL_FUNCTION_TEMPLATE_P (OVL_FIRST (x))
2578 || (TREE_CODE (x) == OVERLOAD && !OVL_SINGLE_P (x)))
2579 return 2;
2581 return OVL_P (x);
2584 /* X is the CALL_EXPR_FN of a CALL_EXPR. If X represents a dependent name
2585 (14.6.2), return the IDENTIFIER_NODE for that name. Otherwise, return
2586 NULL_TREE. */
2588 tree
2589 dependent_name (tree x)
2591 /* FIXME a dependent name must be unqualified, but this function doesn't
2592 distinguish between qualified and unqualified identifiers. */
2593 if (identifier_p (x))
2594 return x;
2595 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
2596 x = TREE_OPERAND (x, 0);
2597 if (OVL_P (x))
2598 return OVL_NAME (x);
2599 return NULL_TREE;
2602 /* Like dependent_name, but instead takes a CALL_EXPR and also checks
2603 its dependence. */
2605 tree
2606 call_expr_dependent_name (tree x)
2608 if (TREE_TYPE (x) != NULL_TREE)
2609 /* X isn't dependent, so its callee isn't a dependent name. */
2610 return NULL_TREE;
2611 return dependent_name (CALL_EXPR_FN (x));
2614 /* Returns true iff X is an expression for an overloaded function
2615 whose type cannot be known without performing overload
2616 resolution. */
2618 bool
2619 really_overloaded_fn (tree x)
2621 return is_overloaded_fn (x) == 2;
2624 /* Get the overload set FROM refers to. Returns NULL if it's not an
2625 overload set. */
2627 tree
2628 maybe_get_fns (tree from)
2630 STRIP_ANY_LOCATION_WRAPPER (from);
2632 /* A baselink is also considered an overloaded function. */
2633 if (TREE_CODE (from) == OFFSET_REF
2634 || TREE_CODE (from) == COMPONENT_REF)
2635 from = TREE_OPERAND (from, 1);
2636 if (BASELINK_P (from))
2637 from = BASELINK_FUNCTIONS (from);
2638 if (TREE_CODE (from) == TEMPLATE_ID_EXPR)
2639 from = TREE_OPERAND (from, 0);
2641 if (OVL_P (from))
2642 return from;
2644 return NULL;
2647 /* FROM refers to an overload set. Return that set (or die). */
2649 tree
2650 get_fns (tree from)
2652 tree res = maybe_get_fns (from);
2654 gcc_assert (res);
2655 return res;
2658 /* Return the first function of the overload set FROM refers to. */
2660 tree
2661 get_first_fn (tree from)
2663 return OVL_FIRST (get_fns (from));
2666 /* Return the scope where the overloaded functions OVL were found. */
2668 tree
2669 ovl_scope (tree ovl)
2671 if (TREE_CODE (ovl) == OFFSET_REF
2672 || TREE_CODE (ovl) == COMPONENT_REF)
2673 ovl = TREE_OPERAND (ovl, 1);
2674 if (TREE_CODE (ovl) == BASELINK)
2675 return BINFO_TYPE (BASELINK_BINFO (ovl));
2676 if (TREE_CODE (ovl) == TEMPLATE_ID_EXPR)
2677 ovl = TREE_OPERAND (ovl, 0);
2678 /* Skip using-declarations. */
2679 lkp_iterator iter (ovl);
2681 ovl = *iter;
2682 while (iter.using_p () && ++iter);
2684 return CP_DECL_CONTEXT (ovl);
2687 #define PRINT_RING_SIZE 4
2689 static const char *
2690 cxx_printable_name_internal (tree decl, int v, bool translate)
2692 static unsigned int uid_ring[PRINT_RING_SIZE];
2693 static char *print_ring[PRINT_RING_SIZE];
2694 static bool trans_ring[PRINT_RING_SIZE];
2695 static int ring_counter;
2696 int i;
2698 /* Only cache functions. */
2699 if (v < 2
2700 || TREE_CODE (decl) != FUNCTION_DECL
2701 || DECL_LANG_SPECIFIC (decl) == 0)
2702 return lang_decl_name (decl, v, translate);
2704 /* See if this print name is lying around. */
2705 for (i = 0; i < PRINT_RING_SIZE; i++)
2706 if (uid_ring[i] == DECL_UID (decl) && translate == trans_ring[i])
2707 /* yes, so return it. */
2708 return print_ring[i];
2710 if (++ring_counter == PRINT_RING_SIZE)
2711 ring_counter = 0;
2713 if (current_function_decl != NULL_TREE)
2715 /* There may be both translated and untranslated versions of the
2716 name cached. */
2717 for (i = 0; i < 2; i++)
2719 if (uid_ring[ring_counter] == DECL_UID (current_function_decl))
2720 ring_counter += 1;
2721 if (ring_counter == PRINT_RING_SIZE)
2722 ring_counter = 0;
2724 gcc_assert (uid_ring[ring_counter] != DECL_UID (current_function_decl));
2727 free (print_ring[ring_counter]);
2729 print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v, translate));
2730 uid_ring[ring_counter] = DECL_UID (decl);
2731 trans_ring[ring_counter] = translate;
2732 return print_ring[ring_counter];
2735 const char *
2736 cxx_printable_name (tree decl, int v)
2738 return cxx_printable_name_internal (decl, v, false);
2741 const char *
2742 cxx_printable_name_translate (tree decl, int v)
2744 return cxx_printable_name_internal (decl, v, true);
2747 /* Return the canonical version of exception-specification RAISES for a C++17
2748 function type, for use in type comparison and building TYPE_CANONICAL. */
2750 tree
2751 canonical_eh_spec (tree raises)
2753 if (raises == NULL_TREE)
2754 return raises;
2755 else if (DEFERRED_NOEXCEPT_SPEC_P (raises)
2756 || UNPARSED_NOEXCEPT_SPEC_P (raises)
2757 || uses_template_parms (raises)
2758 || uses_template_parms (TREE_PURPOSE (raises)))
2759 /* Keep a dependent or deferred exception specification. */
2760 return raises;
2761 else if (nothrow_spec_p (raises))
2762 /* throw() -> noexcept. */
2763 return noexcept_true_spec;
2764 else
2765 /* For C++17 type matching, anything else -> nothing. */
2766 return NULL_TREE;
2769 tree
2770 build_cp_fntype_variant (tree type, cp_ref_qualifier rqual,
2771 tree raises, bool late)
2773 cp_cv_quals type_quals = TYPE_QUALS (type);
2775 if (cp_check_qualified_type (type, type, type_quals, rqual, raises, late))
2776 return type;
2778 tree v = TYPE_MAIN_VARIANT (type);
2779 for (; v; v = TYPE_NEXT_VARIANT (v))
2780 if (cp_check_qualified_type (v, type, type_quals, rqual, raises, late))
2781 return v;
2783 /* Need to build a new variant. */
2784 v = build_variant_type_copy (type);
2785 if (!TYPE_DEPENDENT_P (v))
2786 /* We no longer know that it's not type-dependent. */
2787 TYPE_DEPENDENT_P_VALID (v) = false;
2788 TYPE_RAISES_EXCEPTIONS (v) = raises;
2789 TYPE_HAS_LATE_RETURN_TYPE (v) = late;
2790 switch (rqual)
2792 case REF_QUAL_RVALUE:
2793 FUNCTION_RVALUE_QUALIFIED (v) = 1;
2794 FUNCTION_REF_QUALIFIED (v) = 1;
2795 break;
2796 case REF_QUAL_LVALUE:
2797 FUNCTION_RVALUE_QUALIFIED (v) = 0;
2798 FUNCTION_REF_QUALIFIED (v) = 1;
2799 break;
2800 default:
2801 FUNCTION_REF_QUALIFIED (v) = 0;
2802 break;
2805 /* Canonicalize the exception specification. */
2806 tree cr = flag_noexcept_type ? canonical_eh_spec (raises) : NULL_TREE;
2807 bool complex_eh_spec_p = (cr && cr != noexcept_true_spec
2808 && !UNPARSED_NOEXCEPT_SPEC_P (cr));
2810 if (!complex_eh_spec_p && TYPE_RAISES_EXCEPTIONS (type))
2811 /* We want to consider structural equality of the exception-less
2812 variant since we'll be replacing the exception specification. */
2813 type = build_cp_fntype_variant (type, rqual, /*raises=*/NULL_TREE, late);
2814 if (TYPE_STRUCTURAL_EQUALITY_P (type) || complex_eh_spec_p)
2815 /* Propagate structural equality. And always use structural equality
2816 for function types with a complex noexcept-spec since their identity
2817 may depend on e.g. whether comparing_specializations is set. */
2818 SET_TYPE_STRUCTURAL_EQUALITY (v);
2819 else if (TYPE_CANONICAL (type) != type || cr != raises || late)
2820 /* Build the underlying canonical type, since it is different
2821 from TYPE. */
2822 TYPE_CANONICAL (v) = build_cp_fntype_variant (TYPE_CANONICAL (type),
2823 rqual, cr, false);
2824 else
2825 /* T is its own canonical type. */
2826 TYPE_CANONICAL (v) = v;
2828 return v;
2831 /* TYPE is a function or method type with a deferred exception
2832 specification that has been parsed to RAISES. Fixup all the type
2833 variants that are affected in place. Via decltype &| noexcept
2834 tricks, the unparsed spec could have escaped into the type system. */
2836 void
2837 fixup_deferred_exception_variants (tree type, tree raises)
2839 tree original = TYPE_RAISES_EXCEPTIONS (type);
2841 gcc_checking_assert (UNPARSED_NOEXCEPT_SPEC_P (original));
2843 for (tree variant = TYPE_MAIN_VARIANT (type);
2844 variant; variant = TYPE_NEXT_VARIANT (variant))
2845 if (TYPE_RAISES_EXCEPTIONS (variant) == original)
2847 gcc_checking_assert (variant != TYPE_MAIN_VARIANT (type));
2849 SET_TYPE_STRUCTURAL_EQUALITY (variant);
2850 TYPE_RAISES_EXCEPTIONS (variant) = raises;
2852 if (!TYPE_DEPENDENT_P (variant))
2853 /* We no longer know that it's not type-dependent. */
2854 TYPE_DEPENDENT_P_VALID (variant) = false;
2858 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
2859 listed in RAISES. */
2861 tree
2862 build_exception_variant (tree type, tree raises)
2864 cp_ref_qualifier rqual = type_memfn_rqual (type);
2865 bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
2866 return build_cp_fntype_variant (type, rqual, raises, late);
2869 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new
2870 BOUND_TEMPLATE_TEMPLATE_PARM bound with NEWARGS as its template
2871 arguments. */
2873 tree
2874 bind_template_template_parm (tree t, tree newargs)
2876 tree decl = TYPE_NAME (t);
2877 tree t2;
2879 t2 = cxx_make_type (BOUND_TEMPLATE_TEMPLATE_PARM);
2880 decl = build_decl (input_location,
2881 TYPE_DECL, DECL_NAME (decl), NULL_TREE);
2882 SET_DECL_TEMPLATE_PARM_P (decl);
2884 /* These nodes have to be created to reflect new TYPE_DECL and template
2885 arguments. */
2886 TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t));
2887 TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl;
2888 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
2889 = build_template_info (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t), newargs);
2891 TREE_TYPE (decl) = t2;
2892 TYPE_NAME (t2) = decl;
2893 TYPE_STUB_DECL (t2) = decl;
2894 TYPE_SIZE (t2) = 0;
2896 if (any_template_arguments_need_structural_equality_p (newargs))
2897 SET_TYPE_STRUCTURAL_EQUALITY (t2);
2898 else
2899 TYPE_CANONICAL (t2) = canonical_type_parameter (t2);
2901 return t2;
2904 /* Called from count_trees via walk_tree. */
2906 static tree
2907 count_trees_r (tree *tp, int *walk_subtrees, void *data)
2909 ++*((int *) data);
2911 if (TYPE_P (*tp))
2912 *walk_subtrees = 0;
2914 return NULL_TREE;
2917 /* Debugging function for measuring the rough complexity of a tree
2918 representation. */
2921 count_trees (tree t)
2923 int n_trees = 0;
2924 cp_walk_tree_without_duplicates (&t, count_trees_r, &n_trees);
2925 return n_trees;
2928 /* Called from verify_stmt_tree via walk_tree. */
2930 static tree
2931 verify_stmt_tree_r (tree* tp, int * /*walk_subtrees*/, void* data)
2933 tree t = *tp;
2934 hash_table<nofree_ptr_hash <tree_node> > *statements
2935 = static_cast <hash_table<nofree_ptr_hash <tree_node> > *> (data);
2936 tree_node **slot;
2938 if (!STATEMENT_CODE_P (TREE_CODE (t)))
2939 return NULL_TREE;
2941 /* If this statement is already present in the hash table, then
2942 there is a circularity in the statement tree. */
2943 gcc_assert (!statements->find (t));
2945 slot = statements->find_slot (t, INSERT);
2946 *slot = t;
2948 return NULL_TREE;
2951 /* Debugging function to check that the statement T has not been
2952 corrupted. For now, this function simply checks that T contains no
2953 circularities. */
2955 void
2956 verify_stmt_tree (tree t)
2958 hash_table<nofree_ptr_hash <tree_node> > statements (37);
2959 cp_walk_tree (&t, verify_stmt_tree_r, &statements, NULL);
2962 /* Check if the type T depends on a type with no linkage and if so,
2963 return it. If RELAXED_P then do not consider a class type declared
2964 within a vague-linkage function or in a module CMI to have no linkage,
2965 since it can still be accessed within a different TU. Remember:
2966 no-linkage is not the same as internal-linkage. */
2968 tree
2969 no_linkage_check (tree t, bool relaxed_p)
2971 tree r;
2973 /* Lambda types that don't have mangling scope have no linkage. We
2974 check CLASSTYPE_LAMBDA_EXPR for error_mark_node because
2975 when we get here from pushtag none of the lambda information is
2976 set up yet, so we want to assume that the lambda has linkage and
2977 fix it up later if not. We need to check this even in templates so
2978 that we properly handle a lambda-expression in the signature. */
2979 if (LAMBDA_TYPE_P (t)
2980 && CLASSTYPE_LAMBDA_EXPR (t) != error_mark_node)
2982 tree extra = LAMBDA_TYPE_EXTRA_SCOPE (t);
2983 if (!extra)
2984 return t;
2987 /* Otherwise there's no point in checking linkage on template functions; we
2988 can't know their complete types. */
2989 if (processing_template_decl)
2990 return NULL_TREE;
2992 switch (TREE_CODE (t))
2994 case RECORD_TYPE:
2995 if (TYPE_PTRMEMFUNC_P (t))
2996 goto ptrmem;
2997 /* Fall through. */
2998 case UNION_TYPE:
2999 if (!CLASS_TYPE_P (t))
3000 return NULL_TREE;
3001 /* Fall through. */
3002 case ENUMERAL_TYPE:
3003 /* Only treat unnamed types as having no linkage if they're at
3004 namespace scope. This is core issue 966. */
3005 if (TYPE_UNNAMED_P (t) && TYPE_NAMESPACE_SCOPE_P (t))
3006 return t;
3008 for (r = CP_TYPE_CONTEXT (t); ; )
3010 /* If we're a nested type of a !TREE_PUBLIC class, we might not
3011 have linkage, or we might just be in an anonymous namespace.
3012 If we're in a TREE_PUBLIC class, we have linkage. */
3013 if (TYPE_P (r) && !TREE_PUBLIC (TYPE_NAME (r)))
3014 return no_linkage_check (TYPE_CONTEXT (t), relaxed_p);
3015 else if (TREE_CODE (r) == FUNCTION_DECL)
3017 if (relaxed_p
3018 && (vague_linkage_p (r)
3019 || (TREE_PUBLIC (r) && module_maybe_has_cmi_p ())))
3020 r = CP_DECL_CONTEXT (r);
3021 else
3022 return t;
3024 else
3025 break;
3028 return NULL_TREE;
3030 case ARRAY_TYPE:
3031 case POINTER_TYPE:
3032 case REFERENCE_TYPE:
3033 case VECTOR_TYPE:
3034 return no_linkage_check (TREE_TYPE (t), relaxed_p);
3036 case OFFSET_TYPE:
3037 ptrmem:
3038 r = no_linkage_check (TYPE_PTRMEM_POINTED_TO_TYPE (t),
3039 relaxed_p);
3040 if (r)
3041 return r;
3042 return no_linkage_check (TYPE_PTRMEM_CLASS_TYPE (t), relaxed_p);
3044 case METHOD_TYPE:
3045 case FUNCTION_TYPE:
3047 tree parm = TYPE_ARG_TYPES (t);
3048 if (TREE_CODE (t) == METHOD_TYPE)
3049 /* The 'this' pointer isn't interesting; a method has the same
3050 linkage (or lack thereof) as its enclosing class. */
3051 parm = TREE_CHAIN (parm);
3052 for (;
3053 parm && parm != void_list_node;
3054 parm = TREE_CHAIN (parm))
3056 r = no_linkage_check (TREE_VALUE (parm), relaxed_p);
3057 if (r)
3058 return r;
3060 return no_linkage_check (TREE_TYPE (t), relaxed_p);
3063 default:
3064 return NULL_TREE;
3068 extern int depth_reached;
3070 void
3071 cxx_print_statistics (void)
3073 print_template_statistics ();
3074 if (GATHER_STATISTICS)
3075 fprintf (stderr, "maximum template instantiation depth reached: %d\n",
3076 depth_reached);
3079 /* Return, as an INTEGER_CST node, the number of elements for TYPE
3080 (which is an ARRAY_TYPE). This counts only elements of the top
3081 array. */
3083 tree
3084 array_type_nelts_top (tree type)
3086 return fold_build2_loc (input_location,
3087 PLUS_EXPR, sizetype,
3088 array_type_nelts (type),
3089 size_one_node);
3092 /* Return, as an INTEGER_CST node, the number of elements for TYPE
3093 (which is an ARRAY_TYPE). This one is a recursive count of all
3094 ARRAY_TYPEs that are clumped together. */
3096 tree
3097 array_type_nelts_total (tree type)
3099 tree sz = array_type_nelts_top (type);
3100 type = TREE_TYPE (type);
3101 while (TREE_CODE (type) == ARRAY_TYPE)
3103 tree n = array_type_nelts_top (type);
3104 sz = fold_build2_loc (input_location,
3105 MULT_EXPR, sizetype, sz, n);
3106 type = TREE_TYPE (type);
3108 return sz;
3111 struct bot_data
3113 splay_tree target_remap;
3114 bool clear_location;
3117 /* Called from break_out_target_exprs via mapcar. */
3119 static tree
3120 bot_manip (tree* tp, int* walk_subtrees, void* data_)
3122 bot_data &data = *(bot_data*)data_;
3123 splay_tree target_remap = data.target_remap;
3124 tree t = *tp;
3126 if (!TYPE_P (t) && TREE_CONSTANT (t) && !TREE_SIDE_EFFECTS (t))
3128 /* There can't be any TARGET_EXPRs or their slot variables below this
3129 point. But we must make a copy, in case subsequent processing
3130 alters any part of it. For example, during gimplification a cast
3131 of the form (T) &X::f (where "f" is a member function) will lead
3132 to replacing the PTRMEM_CST for &X::f with a VAR_DECL. */
3133 *walk_subtrees = 0;
3134 *tp = unshare_expr (t);
3135 return NULL_TREE;
3137 if (TREE_CODE (t) == TARGET_EXPR)
3139 tree u;
3141 if (TREE_CODE (TARGET_EXPR_INITIAL (t)) == AGGR_INIT_EXPR)
3143 u = build_cplus_new (TREE_TYPE (t), TARGET_EXPR_INITIAL (t),
3144 tf_warning_or_error);
3145 if (u == error_mark_node)
3146 return u;
3147 if (AGGR_INIT_ZERO_FIRST (TARGET_EXPR_INITIAL (t)))
3148 AGGR_INIT_ZERO_FIRST (TARGET_EXPR_INITIAL (u)) = true;
3150 else
3151 u = force_target_expr (TREE_TYPE (t), TARGET_EXPR_INITIAL (t),
3152 tf_warning_or_error);
3154 TARGET_EXPR_IMPLICIT_P (u) = TARGET_EXPR_IMPLICIT_P (t);
3155 TARGET_EXPR_LIST_INIT_P (u) = TARGET_EXPR_LIST_INIT_P (t);
3156 TARGET_EXPR_DIRECT_INIT_P (u) = TARGET_EXPR_DIRECT_INIT_P (t);
3157 TARGET_EXPR_ELIDING_P (u) = TARGET_EXPR_ELIDING_P (t);
3159 /* Map the old variable to the new one. */
3160 splay_tree_insert (target_remap,
3161 (splay_tree_key) TARGET_EXPR_SLOT (t),
3162 (splay_tree_value) TARGET_EXPR_SLOT (u));
3164 TARGET_EXPR_INITIAL (u) = break_out_target_exprs (TARGET_EXPR_INITIAL (u),
3165 data.clear_location);
3166 if (TARGET_EXPR_INITIAL (u) == error_mark_node)
3167 return error_mark_node;
3169 if (data.clear_location)
3170 SET_EXPR_LOCATION (u, input_location);
3172 /* Replace the old expression with the new version. */
3173 *tp = u;
3174 /* We don't have to go below this point; the recursive call to
3175 break_out_target_exprs will have handled anything below this
3176 point. */
3177 *walk_subtrees = 0;
3178 return NULL_TREE;
3180 if (TREE_CODE (*tp) == SAVE_EXPR)
3182 t = *tp;
3183 splay_tree_node n = splay_tree_lookup (target_remap,
3184 (splay_tree_key) t);
3185 if (n)
3187 *tp = (tree)n->value;
3188 *walk_subtrees = 0;
3190 else
3192 copy_tree_r (tp, walk_subtrees, NULL);
3193 splay_tree_insert (target_remap,
3194 (splay_tree_key)t,
3195 (splay_tree_value)*tp);
3196 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
3197 splay_tree_insert (target_remap,
3198 (splay_tree_key)*tp,
3199 (splay_tree_value)*tp);
3201 return NULL_TREE;
3203 if (TREE_CODE (*tp) == DECL_EXPR
3204 && VAR_P (DECL_EXPR_DECL (*tp))
3205 && DECL_ARTIFICIAL (DECL_EXPR_DECL (*tp))
3206 && !TREE_STATIC (DECL_EXPR_DECL (*tp)))
3208 tree t;
3209 splay_tree_node n
3210 = splay_tree_lookup (target_remap,
3211 (splay_tree_key) DECL_EXPR_DECL (*tp));
3212 if (n)
3213 t = (tree) n->value;
3214 else
3216 t = create_temporary_var (TREE_TYPE (DECL_EXPR_DECL (*tp)));
3217 DECL_INITIAL (t) = DECL_INITIAL (DECL_EXPR_DECL (*tp));
3218 splay_tree_insert (target_remap,
3219 (splay_tree_key) DECL_EXPR_DECL (*tp),
3220 (splay_tree_value) t);
3222 copy_tree_r (tp, walk_subtrees, NULL);
3223 DECL_EXPR_DECL (*tp) = t;
3224 if (data.clear_location && EXPR_HAS_LOCATION (*tp))
3225 SET_EXPR_LOCATION (*tp, input_location);
3226 return NULL_TREE;
3228 if (TREE_CODE (*tp) == BIND_EXPR && BIND_EXPR_VARS (*tp))
3230 copy_tree_r (tp, walk_subtrees, NULL);
3231 for (tree *p = &BIND_EXPR_VARS (*tp); *p; p = &DECL_CHAIN (*p))
3233 gcc_assert (VAR_P (*p) && DECL_ARTIFICIAL (*p) && !TREE_STATIC (*p));
3234 tree t = create_temporary_var (TREE_TYPE (*p));
3235 DECL_INITIAL (t) = DECL_INITIAL (*p);
3236 DECL_CHAIN (t) = DECL_CHAIN (*p);
3237 splay_tree_insert (target_remap, (splay_tree_key) *p,
3238 (splay_tree_value) t);
3239 *p = t;
3241 if (data.clear_location && EXPR_HAS_LOCATION (*tp))
3242 SET_EXPR_LOCATION (*tp, input_location);
3243 return NULL_TREE;
3246 /* Make a copy of this node. */
3247 t = copy_tree_r (tp, walk_subtrees, NULL);
3248 if (TREE_CODE (*tp) == CALL_EXPR || TREE_CODE (*tp) == AGGR_INIT_EXPR)
3249 if (!processing_template_decl)
3250 set_flags_from_callee (*tp);
3251 if (data.clear_location && EXPR_HAS_LOCATION (*tp))
3252 SET_EXPR_LOCATION (*tp, input_location);
3253 return t;
3256 /* Replace all remapped VAR_DECLs in T with their new equivalents.
3257 DATA is really a splay-tree mapping old variables to new
3258 variables. */
3260 static tree
3261 bot_replace (tree* t, int */*walk_subtrees*/, void* data_)
3263 bot_data &data = *(bot_data*)data_;
3264 splay_tree target_remap = data.target_remap;
3266 if (VAR_P (*t))
3268 splay_tree_node n = splay_tree_lookup (target_remap,
3269 (splay_tree_key) *t);
3270 if (n)
3271 *t = (tree) n->value;
3273 else if (TREE_CODE (*t) == PARM_DECL
3274 && DECL_NAME (*t) == this_identifier
3275 && !DECL_CONTEXT (*t))
3277 /* In an NSDMI we need to replace the 'this' parameter we used for
3278 parsing with the real one for this function. */
3279 *t = current_class_ptr;
3281 else if (TREE_CODE (*t) == CONVERT_EXPR
3282 && CONVERT_EXPR_VBASE_PATH (*t))
3284 /* In an NSDMI build_base_path defers building conversions to morally
3285 virtual bases, and we handle it here. */
3286 tree basetype = TREE_TYPE (*t);
3287 *t = convert_to_base (TREE_OPERAND (*t, 0), basetype,
3288 /*check_access=*/false, /*nonnull=*/true,
3289 tf_warning_or_error);
3292 return NULL_TREE;
3295 /* When we parse a default argument expression, we may create
3296 temporary variables via TARGET_EXPRs. When we actually use the
3297 default-argument expression, we make a copy of the expression
3298 and replace the temporaries with appropriate local versions.
3300 If CLEAR_LOCATION is true, override any EXPR_LOCATION with
3301 input_location. */
3303 tree
3304 break_out_target_exprs (tree t, bool clear_location /* = false */)
3306 static int target_remap_count;
3307 static splay_tree target_remap;
3309 /* We shouldn't be called on templated trees, nor do we want to
3310 produce them. */
3311 gcc_checking_assert (!processing_template_decl);
3313 if (!target_remap_count++)
3314 target_remap = splay_tree_new (splay_tree_compare_pointers,
3315 /*splay_tree_delete_key_fn=*/NULL,
3316 /*splay_tree_delete_value_fn=*/NULL);
3317 bot_data data = { target_remap, clear_location };
3318 if (cp_walk_tree (&t, bot_manip, &data, NULL) == error_mark_node)
3319 t = error_mark_node;
3320 if (cp_walk_tree (&t, bot_replace, &data, NULL) == error_mark_node)
3321 t = error_mark_node;
3323 if (!--target_remap_count)
3325 splay_tree_delete (target_remap);
3326 target_remap = NULL;
3329 return t;
3332 /* Build an expression for the subobject of OBJ at CONSTRUCTOR index INDEX,
3333 which we expect to have type TYPE. */
3335 tree
3336 build_ctor_subob_ref (tree index, tree type, tree obj)
3338 if (index == NULL_TREE)
3339 /* Can't refer to a particular member of a vector. */
3340 obj = NULL_TREE;
3341 else if (TREE_CODE (index) == INTEGER_CST)
3342 obj = cp_build_array_ref (input_location, obj, index, tf_none);
3343 else
3344 obj = build_class_member_access_expr (obj, index, NULL_TREE,
3345 /*reference*/false, tf_none);
3346 if (obj)
3348 tree objtype = TREE_TYPE (obj);
3349 if (TREE_CODE (objtype) == ARRAY_TYPE && !TYPE_DOMAIN (objtype))
3351 /* When the destination object refers to a flexible array member
3352 verify that it matches the type of the source object except
3353 for its domain and qualifiers. */
3354 gcc_assert (comptypes (TYPE_MAIN_VARIANT (type),
3355 TYPE_MAIN_VARIANT (objtype),
3356 COMPARE_REDECLARATION));
3358 else
3359 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, objtype));
3362 return obj;
3365 struct replace_placeholders_t
3367 tree obj; /* The object to be substituted for a PLACEHOLDER_EXPR. */
3368 tree exp; /* The outermost exp. */
3369 bool seen; /* Whether we've encountered a PLACEHOLDER_EXPR. */
3370 hash_set<tree> *pset; /* To avoid walking same trees multiple times. */
3373 /* Like substitute_placeholder_in_expr, but handle C++ tree codes and
3374 build up subexpressions as we go deeper. */
3376 static tree
3377 replace_placeholders_r (tree* t, int* walk_subtrees, void* data_)
3379 replace_placeholders_t *d = static_cast<replace_placeholders_t*>(data_);
3380 tree obj = d->obj;
3382 if (TYPE_P (*t) || TREE_CONSTANT (*t))
3384 *walk_subtrees = false;
3385 return NULL_TREE;
3388 switch (TREE_CODE (*t))
3390 case PLACEHOLDER_EXPR:
3392 tree x = obj;
3393 for (; !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (*t),
3394 TREE_TYPE (x));
3395 x = TREE_OPERAND (x, 0))
3396 gcc_assert (handled_component_p (x));
3397 *t = unshare_expr (x);
3398 *walk_subtrees = false;
3399 d->seen = true;
3401 break;
3403 case CONSTRUCTOR:
3405 constructor_elt *ce;
3406 vec<constructor_elt,va_gc> *v = CONSTRUCTOR_ELTS (*t);
3407 /* Don't walk into CONSTRUCTOR_PLACEHOLDER_BOUNDARY ctors
3408 other than the d->exp one, those have PLACEHOLDER_EXPRs
3409 related to another object. */
3410 if ((CONSTRUCTOR_PLACEHOLDER_BOUNDARY (*t)
3411 && *t != d->exp)
3412 || d->pset->add (*t))
3414 *walk_subtrees = false;
3415 return NULL_TREE;
3417 for (unsigned i = 0; vec_safe_iterate (v, i, &ce); ++i)
3419 tree *valp = &ce->value;
3420 tree type = TREE_TYPE (*valp);
3421 tree subob = obj;
3423 /* Elements with RANGE_EXPR index shouldn't have any
3424 placeholders in them. */
3425 if (ce->index && TREE_CODE (ce->index) == RANGE_EXPR)
3426 continue;
3428 if (TREE_CODE (*valp) == CONSTRUCTOR
3429 && AGGREGATE_TYPE_P (type))
3431 /* If we're looking at the initializer for OBJ, then build
3432 a sub-object reference. If we're looking at an
3433 initializer for another object, just pass OBJ down. */
3434 if (same_type_ignoring_top_level_qualifiers_p
3435 (TREE_TYPE (*t), TREE_TYPE (obj)))
3436 subob = build_ctor_subob_ref (ce->index, type, obj);
3437 if (TREE_CODE (*valp) == TARGET_EXPR)
3438 valp = &TARGET_EXPR_INITIAL (*valp);
3440 d->obj = subob;
3441 cp_walk_tree (valp, replace_placeholders_r, data_, NULL);
3442 d->obj = obj;
3444 *walk_subtrees = false;
3445 break;
3448 default:
3449 if (d->pset->add (*t))
3450 *walk_subtrees = false;
3451 break;
3454 return NULL_TREE;
3457 /* Replace PLACEHOLDER_EXPRs in EXP with object OBJ. SEEN_P is set if
3458 a PLACEHOLDER_EXPR has been encountered. */
3460 tree
3461 replace_placeholders (tree exp, tree obj, bool *seen_p /*= NULL*/)
3463 /* This is only relevant for C++14. */
3464 if (cxx_dialect < cxx14)
3465 return exp;
3467 /* If the object isn't a (member of a) class, do nothing. */
3468 tree op0 = obj;
3469 while (handled_component_p (op0))
3470 op0 = TREE_OPERAND (op0, 0);
3471 if (!CLASS_TYPE_P (strip_array_types (TREE_TYPE (op0))))
3472 return exp;
3474 tree *tp = &exp;
3475 if (TREE_CODE (exp) == TARGET_EXPR)
3476 tp = &TARGET_EXPR_INITIAL (exp);
3477 hash_set<tree> pset;
3478 replace_placeholders_t data = { obj, *tp, false, &pset };
3479 cp_walk_tree (tp, replace_placeholders_r, &data, NULL);
3480 if (seen_p)
3481 *seen_p = data.seen;
3482 return exp;
3485 /* Callback function for find_placeholders. */
3487 static tree
3488 find_placeholders_r (tree *t, int *walk_subtrees, void *)
3490 if (TYPE_P (*t) || TREE_CONSTANT (*t))
3492 *walk_subtrees = false;
3493 return NULL_TREE;
3496 switch (TREE_CODE (*t))
3498 case PLACEHOLDER_EXPR:
3499 return *t;
3501 case CONSTRUCTOR:
3502 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (*t))
3503 *walk_subtrees = false;
3504 break;
3506 default:
3507 break;
3510 return NULL_TREE;
3513 /* Return true if EXP contains a PLACEHOLDER_EXPR. Don't walk into
3514 ctors with CONSTRUCTOR_PLACEHOLDER_BOUNDARY flag set. */
3516 bool
3517 find_placeholders (tree exp)
3519 /* This is only relevant for C++14. */
3520 if (cxx_dialect < cxx14)
3521 return false;
3523 return cp_walk_tree_without_duplicates (&exp, find_placeholders_r, NULL);
3526 /* Similar to `build_nt', but for template definitions of dependent
3527 expressions */
3529 tree
3530 build_min_nt_loc (location_t loc, enum tree_code code, ...)
3532 tree t;
3533 int length;
3534 int i;
3535 va_list p;
3537 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3539 va_start (p, code);
3541 t = make_node (code);
3542 SET_EXPR_LOCATION (t, loc);
3543 length = TREE_CODE_LENGTH (code);
3545 for (i = 0; i < length; i++)
3546 TREE_OPERAND (t, i) = va_arg (p, tree);
3548 va_end (p);
3549 return t;
3552 /* Similar to `build', but for template definitions. */
3554 tree
3555 build_min (enum tree_code code, tree tt, ...)
3557 tree t;
3558 int length;
3559 int i;
3560 va_list p;
3562 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3564 va_start (p, tt);
3566 t = make_node (code);
3567 length = TREE_CODE_LENGTH (code);
3568 TREE_TYPE (t) = tt;
3570 for (i = 0; i < length; i++)
3572 tree x = va_arg (p, tree);
3573 TREE_OPERAND (t, i) = x;
3574 if (x && !TYPE_P (x) && TREE_SIDE_EFFECTS (x))
3575 TREE_SIDE_EFFECTS (t) = 1;
3578 va_end (p);
3580 return t;
3583 /* Similar to `build', but for template definitions of non-dependent
3584 expressions. NON_DEP is the non-dependent expression that has been
3585 built. */
3587 tree
3588 build_min_non_dep (enum tree_code code, tree non_dep, ...)
3590 tree t;
3591 int length;
3592 int i;
3593 va_list p;
3595 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3597 va_start (p, non_dep);
3599 if (REFERENCE_REF_P (non_dep))
3600 non_dep = TREE_OPERAND (non_dep, 0);
3602 t = make_node (code);
3603 SET_EXPR_LOCATION (t, cp_expr_loc_or_input_loc (non_dep));
3604 length = TREE_CODE_LENGTH (code);
3605 TREE_TYPE (t) = unlowered_expr_type (non_dep);
3606 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
3608 for (i = 0; i < length; i++)
3610 tree x = va_arg (p, tree);
3611 TREE_OPERAND (t, i) = x;
3612 if (x && !TYPE_P (x))
3613 TREE_SIDE_EFFECTS (t) |= TREE_SIDE_EFFECTS (x);
3616 va_end (p);
3617 return convert_from_reference (t);
3620 /* Similar to build_min_nt, but call expressions */
3622 tree
3623 build_min_nt_call_vec (tree fn, vec<tree, va_gc> *args)
3625 tree ret, t;
3626 unsigned int ix;
3628 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
3629 CALL_EXPR_FN (ret) = fn;
3630 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
3631 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
3632 CALL_EXPR_ARG (ret, ix) = t;
3634 return ret;
3637 /* Similar to `build_min_nt_call_vec', but for template definitions of
3638 non-dependent expressions. NON_DEP is the non-dependent expression
3639 that has been built. */
3641 tree
3642 build_min_non_dep_call_vec (tree non_dep, tree fn, vec<tree, va_gc> *argvec)
3644 tree t = build_min_nt_call_vec (fn, argvec);
3645 if (REFERENCE_REF_P (non_dep))
3646 non_dep = TREE_OPERAND (non_dep, 0);
3647 TREE_TYPE (t) = TREE_TYPE (non_dep);
3648 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
3649 if (argvec)
3650 for (tree x : *argvec)
3651 if (x && !TYPE_P (x))
3652 TREE_SIDE_EFFECTS (t) |= TREE_SIDE_EFFECTS (x);
3653 return convert_from_reference (t);
3656 /* Similar to build_min_non_dep, but for expressions that have been resolved to
3657 a call to an operator overload. OP is the operator that has been
3658 overloaded. NON_DEP is the non-dependent expression that's been built,
3659 which should be a CALL_EXPR or an INDIRECT_REF to a CALL_EXPR. OVERLOAD is
3660 the overload that NON_DEP is calling. */
3662 tree
3663 build_min_non_dep_op_overload (enum tree_code op,
3664 tree non_dep,
3665 tree overload, ...)
3667 va_list p;
3668 int nargs, expected_nargs;
3669 tree fn, call, obj = NULL_TREE;
3671 non_dep = extract_call_expr (non_dep);
3673 nargs = call_expr_nargs (non_dep);
3675 expected_nargs = cp_tree_code_length (op);
3676 if (DECL_OBJECT_MEMBER_FUNCTION_P (overload)
3677 /* For ARRAY_REF, operator[] is either a non-static member or newly
3678 static member, never out of class and for the static member case
3679 if user uses single index the operator[] needs to have a single
3680 argument as well, but the function is called with 2 - the object
3681 it is invoked on and the index. */
3682 || op == ARRAY_REF)
3683 expected_nargs -= 1;
3684 if ((op == POSTINCREMENT_EXPR
3685 || op == POSTDECREMENT_EXPR)
3686 /* With -fpermissive non_dep could be operator++(). */
3687 && (!flag_permissive || nargs != expected_nargs))
3688 expected_nargs += 1;
3689 gcc_assert (nargs == expected_nargs);
3691 releasing_vec args;
3692 va_start (p, overload);
3694 if (!DECL_OBJECT_MEMBER_FUNCTION_P (overload))
3696 fn = overload;
3697 if (op == ARRAY_REF)
3698 obj = va_arg (p, tree);
3699 for (int i = 0; i < nargs; i++)
3701 tree arg = va_arg (p, tree);
3702 vec_safe_push (args, arg);
3705 else
3707 tree object = va_arg (p, tree);
3708 tree binfo = TYPE_BINFO (TREE_TYPE (object));
3709 tree method = build_baselink (binfo, binfo, overload, NULL_TREE);
3710 fn = build_min (COMPONENT_REF, TREE_TYPE (overload),
3711 object, method, NULL_TREE);
3712 for (int i = 0; i < nargs; i++)
3714 tree arg = va_arg (p, tree);
3715 vec_safe_push (args, arg);
3719 va_end (p);
3720 call = build_min_non_dep_call_vec (non_dep, fn, args);
3722 tree call_expr = extract_call_expr (call);
3723 KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep);
3724 CALL_EXPR_OPERATOR_SYNTAX (call_expr) = true;
3725 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep);
3726 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep);
3728 if (obj)
3729 return keep_unused_object_arg (call, obj, overload);
3730 return call;
3733 /* Similar to above build_min_non_dep_op_overload, but arguments
3734 are taken from ARGS vector. */
3736 tree
3737 build_min_non_dep_op_overload (tree non_dep, tree overload, tree object,
3738 vec<tree, va_gc> *args)
3740 non_dep = extract_call_expr (non_dep);
3742 unsigned int nargs = call_expr_nargs (non_dep);
3743 tree fn = overload;
3744 if (DECL_OBJECT_MEMBER_FUNCTION_P (overload))
3746 tree binfo = TYPE_BINFO (TREE_TYPE (object));
3747 tree method = build_baselink (binfo, binfo, overload, NULL_TREE);
3748 fn = build_min (COMPONENT_REF, TREE_TYPE (overload),
3749 object, method, NULL_TREE);
3750 object = NULL_TREE;
3752 gcc_assert (vec_safe_length (args) == nargs);
3754 tree call = build_min_non_dep_call_vec (non_dep, fn, args);
3756 tree call_expr = extract_call_expr (call);
3757 KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep);
3758 CALL_EXPR_OPERATOR_SYNTAX (call_expr) = true;
3759 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep);
3760 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep);
3762 if (object)
3763 return keep_unused_object_arg (call, object, overload);
3764 return call;
3767 /* Return a new tree vec copied from VEC, with ELT inserted at index IDX. */
3769 vec<tree, va_gc> *
3770 vec_copy_and_insert (vec<tree, va_gc> *old_vec, tree elt, unsigned idx)
3772 unsigned len = vec_safe_length (old_vec);
3773 gcc_assert (idx <= len);
3775 vec<tree, va_gc> *new_vec = NULL;
3776 vec_alloc (new_vec, len + 1);
3778 unsigned i;
3779 for (i = 0; i < len; ++i)
3781 if (i == idx)
3782 new_vec->quick_push (elt);
3783 new_vec->quick_push ((*old_vec)[i]);
3785 if (i == idx)
3786 new_vec->quick_push (elt);
3788 return new_vec;
3791 tree
3792 get_type_decl (tree t)
3794 if (TREE_CODE (t) == TYPE_DECL)
3795 return t;
3796 if (TYPE_P (t))
3797 return TYPE_STUB_DECL (t);
3798 gcc_assert (t == error_mark_node);
3799 return t;
3802 /* Returns the namespace that contains DECL, whether directly or
3803 indirectly. */
3805 tree
3806 decl_namespace_context (tree decl)
3808 while (1)
3810 if (TREE_CODE (decl) == NAMESPACE_DECL)
3811 return decl;
3812 else if (TYPE_P (decl))
3813 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
3814 else
3815 decl = CP_DECL_CONTEXT (decl);
3819 /* Returns true if decl is within an anonymous namespace, however deeply
3820 nested, or false otherwise. */
3822 bool
3823 decl_anon_ns_mem_p (tree decl)
3825 return !TREE_PUBLIC (decl_namespace_context (decl));
3828 /* Returns true if the enclosing scope of DECL has internal or no linkage. */
3830 bool
3831 decl_internal_context_p (const_tree decl)
3833 while (TREE_CODE (decl) != NAMESPACE_DECL)
3835 /* Classes inside anonymous namespaces have TREE_PUBLIC == 0. */
3836 if (TYPE_P (decl))
3837 return !TREE_PUBLIC (TYPE_MAIN_DECL (decl));
3839 decl = CP_DECL_CONTEXT (decl);
3841 return !TREE_PUBLIC (decl);
3844 /* Subroutine of cp_tree_equal: t1 and t2 are two CALL_EXPRs.
3845 Return whether their CALL_EXPR_FNs are equivalent. */
3847 static bool
3848 called_fns_equal (tree t1, tree t2)
3850 /* Core 1321: dependent names are equivalent even if the overload sets
3851 are different. But do compare explicit template arguments. */
3852 tree name1 = call_expr_dependent_name (t1);
3853 tree name2 = call_expr_dependent_name (t2);
3854 t1 = CALL_EXPR_FN (t1);
3855 t2 = CALL_EXPR_FN (t2);
3856 if (name1 || name2)
3858 tree targs1 = NULL_TREE, targs2 = NULL_TREE;
3860 if (name1 != name2)
3861 return false;
3863 /* FIXME dependent_name currently returns an unqualified name regardless
3864 of whether the function was named with a qualified- or unqualified-id.
3865 Until that's fixed, check that we aren't looking at overload sets from
3866 different scopes. */
3867 if (is_overloaded_fn (t1) && is_overloaded_fn (t2)
3868 && (DECL_CONTEXT (get_first_fn (t1))
3869 != DECL_CONTEXT (get_first_fn (t2))))
3870 return false;
3872 if (TREE_CODE (t1) == TEMPLATE_ID_EXPR)
3873 targs1 = TREE_OPERAND (t1, 1);
3874 if (TREE_CODE (t2) == TEMPLATE_ID_EXPR)
3875 targs2 = TREE_OPERAND (t2, 1);
3876 return cp_tree_equal (targs1, targs2);
3878 else
3879 return cp_tree_equal (t1, t2);
3882 bool comparing_override_contracts;
3884 /* In a component reference, return the innermost object of
3885 the postfix-expression. */
3887 static tree
3888 get_innermost_component (tree t)
3890 gcc_assert (TREE_CODE (t) == COMPONENT_REF);
3891 while (TREE_CODE (t) == COMPONENT_REF)
3892 t = TREE_OPERAND (t, 0);
3893 return t;
3896 /* Returns true if T is a possibly converted 'this' or '*this' expression. */
3898 static bool
3899 is_this_expression (tree t)
3901 t = get_innermost_component (t);
3902 /* See through deferences and no-op conversions. */
3903 if (INDIRECT_REF_P (t))
3904 t = TREE_OPERAND (t, 0);
3905 if (TREE_CODE (t) == NOP_EXPR)
3906 t = TREE_OPERAND (t, 0);
3907 return is_this_parameter (t);
3910 static bool
3911 comparing_this_references (tree t1, tree t2)
3913 return is_this_expression (t1) && is_this_expression (t2);
3916 static bool
3917 equivalent_member_references (tree t1, tree t2)
3919 if (!comparing_this_references (t1, t2))
3920 return false;
3921 t1 = TREE_OPERAND (t1, 1);
3922 t2 = TREE_OPERAND (t2, 1);
3923 return t1 == t2;
3926 /* Return truthvalue of whether T1 is the same tree structure as T2.
3927 Return 1 if they are the same. Return 0 if they are different. */
3929 bool
3930 cp_tree_equal (tree t1, tree t2)
3932 enum tree_code code1, code2;
3934 if (t1 == t2)
3935 return true;
3936 if (!t1 || !t2)
3937 return false;
3939 code1 = TREE_CODE (t1);
3940 code2 = TREE_CODE (t2);
3942 if (code1 != code2)
3943 return false;
3945 if (CONSTANT_CLASS_P (t1)
3946 && !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3947 return false;
3949 switch (code1)
3951 case VOID_CST:
3952 /* There's only a single VOID_CST node, so we should never reach
3953 here. */
3954 gcc_unreachable ();
3956 case INTEGER_CST:
3957 return tree_int_cst_equal (t1, t2);
3959 case REAL_CST:
3960 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
3962 case STRING_CST:
3963 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3964 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3965 TREE_STRING_LENGTH (t1));
3967 case FIXED_CST:
3968 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
3969 TREE_FIXED_CST (t2));
3971 case COMPLEX_CST:
3972 return cp_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
3973 && cp_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
3975 case VECTOR_CST:
3976 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
3978 case CONSTRUCTOR:
3979 /* We need to do this when determining whether or not two
3980 non-type pointer to member function template arguments
3981 are the same. */
3982 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
3983 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
3984 return false;
3986 tree field, value;
3987 unsigned int i;
3988 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
3990 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
3991 if (!cp_tree_equal (field, elt2->index)
3992 || !cp_tree_equal (value, elt2->value))
3993 return false;
3996 return true;
3998 case TREE_LIST:
3999 if (!cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
4000 return false;
4001 if (!cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
4002 return false;
4003 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
4005 case SAVE_EXPR:
4006 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
4008 case CALL_EXPR:
4010 if (KOENIG_LOOKUP_P (t1) != KOENIG_LOOKUP_P (t2))
4011 return false;
4013 if (!called_fns_equal (t1, t2))
4014 return false;
4016 call_expr_arg_iterator iter1, iter2;
4017 init_call_expr_arg_iterator (t1, &iter1);
4018 init_call_expr_arg_iterator (t2, &iter2);
4019 if (iter1.n != iter2.n)
4020 return false;
4022 while (more_call_expr_args_p (&iter1))
4024 tree arg1 = next_call_expr_arg (&iter1);
4025 tree arg2 = next_call_expr_arg (&iter2);
4027 gcc_checking_assert (arg1 && arg2);
4028 if (!cp_tree_equal (arg1, arg2))
4029 return false;
4032 return true;
4035 case TARGET_EXPR:
4037 tree o1 = TARGET_EXPR_SLOT (t1);
4038 tree o2 = TARGET_EXPR_SLOT (t2);
4040 /* Special case: if either target is an unallocated VAR_DECL,
4041 it means that it's going to be unified with whatever the
4042 TARGET_EXPR is really supposed to initialize, so treat it
4043 as being equivalent to anything. */
4044 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
4045 && !DECL_RTL_SET_P (o1))
4046 /*Nop*/;
4047 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
4048 && !DECL_RTL_SET_P (o2))
4049 /*Nop*/;
4050 else if (!cp_tree_equal (o1, o2))
4051 return false;
4053 return cp_tree_equal (TARGET_EXPR_INITIAL (t1),
4054 TARGET_EXPR_INITIAL (t2));
4057 case PARM_DECL:
4058 /* For comparing uses of parameters in late-specified return types
4059 with an out-of-class definition of the function, but can also come
4060 up for expressions that involve 'this' in a member function
4061 template. */
4063 if (comparing_specializations
4064 && DECL_CONTEXT (t1) != DECL_CONTEXT (t2))
4065 /* When comparing hash table entries, only an exact match is
4066 good enough; we don't want to replace 'this' with the
4067 version from another function. But be more flexible
4068 with parameters with identical contexts. */
4069 return false;
4071 if (same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
4073 if (DECL_ARTIFICIAL (t1) ^ DECL_ARTIFICIAL (t2))
4074 return false;
4075 if (CONSTRAINT_VAR_P (t1) ^ CONSTRAINT_VAR_P (t2))
4076 return false;
4077 if (DECL_ARTIFICIAL (t1)
4078 || (DECL_PARM_LEVEL (t1) == DECL_PARM_LEVEL (t2)
4079 && DECL_PARM_INDEX (t1) == DECL_PARM_INDEX (t2)))
4080 return true;
4082 return false;
4084 case TEMPLATE_DECL:
4085 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t1)
4086 && DECL_TEMPLATE_TEMPLATE_PARM_P (t2))
4087 return cp_tree_equal (TREE_TYPE (t1), TREE_TYPE (t2));
4088 /* Fall through. */
4089 case VAR_DECL:
4090 case CONST_DECL:
4091 case FIELD_DECL:
4092 case FUNCTION_DECL:
4093 case IDENTIFIER_NODE:
4094 case SSA_NAME:
4095 case USING_DECL:
4096 case DEFERRED_PARSE:
4097 return false;
4099 case BASELINK:
4100 return (BASELINK_BINFO (t1) == BASELINK_BINFO (t2)
4101 && BASELINK_ACCESS_BINFO (t1) == BASELINK_ACCESS_BINFO (t2)
4102 && BASELINK_QUALIFIED_P (t1) == BASELINK_QUALIFIED_P (t2)
4103 && cp_tree_equal (BASELINK_FUNCTIONS (t1),
4104 BASELINK_FUNCTIONS (t2)));
4106 case TEMPLATE_PARM_INDEX:
4107 return (TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
4108 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2)
4109 && (TEMPLATE_PARM_PARAMETER_PACK (t1)
4110 == TEMPLATE_PARM_PARAMETER_PACK (t2))
4111 && same_type_p (TREE_TYPE (TEMPLATE_PARM_DECL (t1)),
4112 TREE_TYPE (TEMPLATE_PARM_DECL (t2))));
4114 case TEMPLATE_ID_EXPR:
4115 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
4116 return false;
4117 if (!comp_template_args (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)))
4118 return false;
4119 return true;
4121 case CONSTRAINT_INFO:
4122 return cp_tree_equal (CI_ASSOCIATED_CONSTRAINTS (t1),
4123 CI_ASSOCIATED_CONSTRAINTS (t2));
4125 case TREE_VEC:
4126 /* These are template args. Really we should be getting the
4127 caller to do this as it knows it to be true. */
4128 if (!comp_template_args (t1, t2))
4129 return false;
4130 return true;
4132 case SIZEOF_EXPR:
4133 case ALIGNOF_EXPR:
4135 tree o1 = TREE_OPERAND (t1, 0);
4136 tree o2 = TREE_OPERAND (t2, 0);
4138 if (code1 == SIZEOF_EXPR)
4140 if (SIZEOF_EXPR_TYPE_P (t1))
4141 o1 = TREE_TYPE (o1);
4142 if (SIZEOF_EXPR_TYPE_P (t2))
4143 o2 = TREE_TYPE (o2);
4145 else if (ALIGNOF_EXPR_STD_P (t1) != ALIGNOF_EXPR_STD_P (t2))
4146 return false;
4148 if (TREE_CODE (o1) != TREE_CODE (o2))
4149 return false;
4151 if (ARGUMENT_PACK_P (o1))
4152 return template_args_equal (o1, o2);
4153 else if (TYPE_P (o1))
4154 return same_type_p (o1, o2);
4155 else
4156 return cp_tree_equal (o1, o2);
4159 case MODOP_EXPR:
4161 tree t1_op1, t2_op1;
4163 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
4164 return false;
4166 t1_op1 = TREE_OPERAND (t1, 1);
4167 t2_op1 = TREE_OPERAND (t2, 1);
4168 if (TREE_CODE (t1_op1) != TREE_CODE (t2_op1))
4169 return false;
4171 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t2, 2));
4174 case PTRMEM_CST:
4175 /* Two pointer-to-members are the same if they point to the same
4176 field or function in the same class. */
4177 if (PTRMEM_CST_MEMBER (t1) != PTRMEM_CST_MEMBER (t2))
4178 return false;
4180 return same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2));
4182 case OVERLOAD:
4184 /* Two overloads. Must be exactly the same set of decls. */
4185 lkp_iterator first (t1);
4186 lkp_iterator second (t2);
4188 for (; first && second; ++first, ++second)
4189 if (*first != *second)
4190 return false;
4191 return !(first || second);
4194 case TRAIT_EXPR:
4195 if (TRAIT_EXPR_KIND (t1) != TRAIT_EXPR_KIND (t2))
4196 return false;
4197 return cp_tree_equal (TRAIT_EXPR_TYPE1 (t1), TRAIT_EXPR_TYPE1 (t2))
4198 && cp_tree_equal (TRAIT_EXPR_TYPE2 (t1), TRAIT_EXPR_TYPE2 (t2));
4200 case NON_LVALUE_EXPR:
4201 case VIEW_CONVERT_EXPR:
4202 /* Used for location wrappers with possibly NULL types. */
4203 if (!TREE_TYPE (t1) || !TREE_TYPE (t2))
4205 if (TREE_TYPE (t1) || TREE_TYPE (t2))
4206 return false;
4207 break;
4209 /* FALLTHROUGH */
4211 case CAST_EXPR:
4212 case STATIC_CAST_EXPR:
4213 case REINTERPRET_CAST_EXPR:
4214 case CONST_CAST_EXPR:
4215 case DYNAMIC_CAST_EXPR:
4216 case IMPLICIT_CONV_EXPR:
4217 case NEW_EXPR:
4218 case BIT_CAST_EXPR:
4219 CASE_CONVERT:
4220 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
4221 return false;
4222 /* Now compare operands as usual. */
4223 break;
4225 case DEFERRED_NOEXCEPT:
4226 return (cp_tree_equal (DEFERRED_NOEXCEPT_PATTERN (t1),
4227 DEFERRED_NOEXCEPT_PATTERN (t2))
4228 && comp_template_args (DEFERRED_NOEXCEPT_ARGS (t1),
4229 DEFERRED_NOEXCEPT_ARGS (t2)));
4231 case LAMBDA_EXPR:
4232 /* Two lambda-expressions are never considered equivalent. */
4233 return false;
4235 case TYPE_ARGUMENT_PACK:
4236 case NONTYPE_ARGUMENT_PACK:
4238 tree p1 = ARGUMENT_PACK_ARGS (t1);
4239 tree p2 = ARGUMENT_PACK_ARGS (t2);
4240 int len = TREE_VEC_LENGTH (p1);
4241 if (TREE_VEC_LENGTH (p2) != len)
4242 return false;
4244 for (int ix = 0; ix != len; ix++)
4245 if (!template_args_equal (TREE_VEC_ELT (p1, ix),
4246 TREE_VEC_ELT (p2, ix)))
4247 return false;
4248 return true;
4251 case EXPR_PACK_EXPANSION:
4252 if (!cp_tree_equal (PACK_EXPANSION_PATTERN (t1),
4253 PACK_EXPANSION_PATTERN (t2)))
4254 return false;
4255 if (!comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
4256 PACK_EXPANSION_EXTRA_ARGS (t2)))
4257 return false;
4258 return true;
4260 case COMPONENT_REF:
4261 /* If we're comparing contract conditions of overrides, member references
4262 compare equal if they designate the same member. */
4263 if (comparing_override_contracts)
4264 return equivalent_member_references (t1, t2);
4265 break;
4267 default:
4268 break;
4271 switch (TREE_CODE_CLASS (code1))
4273 case tcc_unary:
4274 case tcc_binary:
4275 case tcc_comparison:
4276 case tcc_expression:
4277 case tcc_vl_exp:
4278 case tcc_reference:
4279 case tcc_statement:
4281 int n = cp_tree_operand_length (t1);
4282 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
4283 && n != TREE_OPERAND_LENGTH (t2))
4284 return false;
4286 for (int i = 0; i < n; ++i)
4287 if (!cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
4288 return false;
4290 return true;
4293 case tcc_type:
4294 return same_type_p (t1, t2);
4296 default:
4297 gcc_unreachable ();
4300 /* We can get here with --disable-checking. */
4301 return false;
4304 /* The type of ARG when used as an lvalue. */
4306 tree
4307 lvalue_type (tree arg)
4309 tree type = TREE_TYPE (arg);
4310 return type;
4313 /* The type of ARG for printing error messages; denote lvalues with
4314 reference types. */
4316 tree
4317 error_type (tree arg)
4319 tree type = TREE_TYPE (arg);
4321 if (TREE_CODE (type) == ARRAY_TYPE)
4323 else if (TREE_CODE (type) == ERROR_MARK)
4325 else if (lvalue_p (arg))
4326 type = build_reference_type (lvalue_type (arg));
4327 else if (MAYBE_CLASS_TYPE_P (type))
4328 type = lvalue_type (arg);
4330 return type;
4333 /* Does FUNCTION use a variable-length argument list? */
4336 varargs_function_p (const_tree function)
4338 return stdarg_p (TREE_TYPE (function));
4341 /* Returns 1 if decl is a member of a class. */
4344 member_p (const_tree decl)
4346 const_tree const ctx = DECL_CONTEXT (decl);
4347 return (ctx && TYPE_P (ctx));
4350 /* Create a placeholder for member access where we don't actually have an
4351 object that the access is against. For a general declval<T> equivalent,
4352 use build_stub_object instead. */
4354 tree
4355 build_dummy_object (tree type)
4357 tree decl = build1 (CONVERT_EXPR, build_pointer_type (type), void_node);
4358 return cp_build_fold_indirect_ref (decl);
4361 /* We've gotten a reference to a member of TYPE. Return *this if appropriate,
4362 or a dummy object otherwise. If BINFOP is non-0, it is filled with the
4363 binfo path from current_class_type to TYPE, or 0. */
4365 tree
4366 maybe_dummy_object (tree type, tree* binfop)
4368 tree decl, context;
4369 tree binfo;
4370 tree current = current_nonlambda_class_type ();
4372 if (current
4373 && (binfo = lookup_base (current, type, ba_any, NULL,
4374 tf_warning_or_error)))
4375 context = current;
4376 else
4378 /* Reference from a nested class member function. */
4379 context = type;
4380 binfo = TYPE_BINFO (type);
4383 if (binfop)
4384 *binfop = binfo;
4386 /* current_class_ref might not correspond to current_class_type if
4387 we're in tsubst_default_argument or a lambda-declarator; in either
4388 case, we want to use current_class_ref if it matches CONTEXT. */
4389 tree ctype = current_class_ref ? TREE_TYPE (current_class_ref) : NULL_TREE;
4390 if (ctype
4391 && same_type_ignoring_top_level_qualifiers_p (ctype, context))
4392 decl = current_class_ref;
4393 else
4395 /* Return a dummy object whose cv-quals are consistent with (the
4396 non-lambda) 'this' if available. */
4397 if (ctype)
4399 int quals = TYPE_UNQUALIFIED;
4400 if (tree lambda = CLASSTYPE_LAMBDA_EXPR (ctype))
4402 if (tree cap = lambda_expr_this_capture (lambda, false))
4403 quals = cp_type_quals (TREE_TYPE (TREE_TYPE (cap)));
4405 else
4406 quals = cp_type_quals (ctype);
4407 context = cp_build_qualified_type (context, quals);
4409 decl = build_dummy_object (context);
4412 return decl;
4415 /* Returns 1 if OB is a placeholder object, or a pointer to one. */
4417 bool
4418 is_dummy_object (const_tree ob)
4420 if (INDIRECT_REF_P (ob))
4421 ob = TREE_OPERAND (ob, 0);
4422 return (TREE_CODE (ob) == CONVERT_EXPR
4423 && TREE_OPERAND (ob, 0) == void_node);
4426 /* Returns true if TYPE is char, unsigned char, or std::byte. */
4428 bool
4429 is_byte_access_type (tree type)
4431 type = TYPE_MAIN_VARIANT (type);
4432 if (type == char_type_node
4433 || type == unsigned_char_type_node)
4434 return true;
4436 return (TREE_CODE (type) == ENUMERAL_TYPE
4437 && TYPE_CONTEXT (type) == std_node
4438 && !strcmp ("byte", TYPE_NAME_STRING (type)));
4441 /* Returns true if TYPE is unsigned char or std::byte. */
4443 bool
4444 is_byte_access_type_not_plain_char (tree type)
4446 type = TYPE_MAIN_VARIANT (type);
4447 if (type == char_type_node)
4448 return false;
4450 return is_byte_access_type (type);
4453 /* Returns 1 iff type T is something we want to treat as a scalar type for
4454 the purpose of deciding whether it is trivial/POD/standard-layout. */
4456 bool
4457 scalarish_type_p (const_tree t)
4459 if (t == error_mark_node)
4460 return 1;
4462 return (SCALAR_TYPE_P (t) || VECTOR_TYPE_P (t));
4465 /* Returns true iff T requires non-trivial default initialization. */
4467 bool
4468 type_has_nontrivial_default_init (const_tree t)
4470 t = strip_array_types (CONST_CAST_TREE (t));
4472 if (CLASS_TYPE_P (t))
4473 return TYPE_HAS_COMPLEX_DFLT (t);
4474 else
4475 return 0;
4478 /* Track classes with only deleted copy/move constructors so that we can warn
4479 if they are used in call/return by value. */
4481 static GTY(()) hash_set<tree>* deleted_copy_types;
4482 static void
4483 remember_deleted_copy (const_tree t)
4485 if (!deleted_copy_types)
4486 deleted_copy_types = hash_set<tree>::create_ggc(37);
4487 deleted_copy_types->add (CONST_CAST_TREE (t));
4489 void
4490 maybe_warn_parm_abi (tree t, location_t loc)
4492 if (!deleted_copy_types
4493 || !deleted_copy_types->contains (t))
4494 return;
4496 if ((flag_abi_version == 12 || warn_abi_version == 12)
4497 && classtype_has_non_deleted_move_ctor (t))
4499 bool w;
4500 auto_diagnostic_group d;
4501 if (flag_abi_version > 12)
4502 w = warning_at (loc, OPT_Wabi, "%<-fabi-version=13%> (GCC 8.2) fixes "
4503 "the calling convention for %qT, which was "
4504 "accidentally changed in 8.1", t);
4505 else
4506 w = warning_at (loc, OPT_Wabi, "%<-fabi-version=12%> (GCC 8.1) "
4507 "accidentally changes the calling convention for %qT",
4509 if (w)
4510 inform (location_of (t), " declared here");
4511 return;
4514 auto_diagnostic_group d;
4515 if (warning_at (loc, OPT_Wabi, "the calling convention for %qT changes in "
4516 "%<-fabi-version=13%> (GCC 8.2)", t))
4517 inform (location_of (t), " because all of its copy and move "
4518 "constructors are deleted");
4521 /* Returns true iff copying an object of type T (including via move
4522 constructor) is non-trivial. That is, T has no non-trivial copy
4523 constructors and no non-trivial move constructors, and not all copy/move
4524 constructors are deleted. This function implements the ABI notion of
4525 non-trivial copy, which has diverged from the one in the standard. */
4527 bool
4528 type_has_nontrivial_copy_init (const_tree type)
4530 tree t = strip_array_types (CONST_CAST_TREE (type));
4532 if (CLASS_TYPE_P (t))
4534 gcc_assert (COMPLETE_TYPE_P (t));
4536 if (TYPE_HAS_COMPLEX_COPY_CTOR (t)
4537 || TYPE_HAS_COMPLEX_MOVE_CTOR (t))
4538 /* Nontrivial. */
4539 return true;
4541 if (cxx_dialect < cxx11)
4542 /* No deleted functions before C++11. */
4543 return false;
4545 /* Before ABI v12 we did a bitwise copy of types with only deleted
4546 copy/move constructors. */
4547 if (!abi_version_at_least (12)
4548 && !(warn_abi && abi_version_crosses (12)))
4549 return false;
4551 bool saw_copy = false;
4552 bool saw_non_deleted = false;
4553 bool saw_non_deleted_move = false;
4555 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
4556 saw_copy = saw_non_deleted = true;
4557 else if (CLASSTYPE_LAZY_COPY_CTOR (t))
4559 saw_copy = true;
4560 if (classtype_has_move_assign_or_move_ctor_p (t, true))
4561 /* [class.copy]/8 If the class definition declares a move
4562 constructor or move assignment operator, the implicitly declared
4563 copy constructor is defined as deleted.... */;
4564 else
4565 /* Any other reason the implicitly-declared function would be
4566 deleted would also cause TYPE_HAS_COMPLEX_COPY_CTOR to be
4567 set. */
4568 saw_non_deleted = true;
4571 if (!saw_non_deleted)
4572 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4574 tree fn = *iter;
4575 if (copy_fn_p (fn))
4577 saw_copy = true;
4578 if (!DECL_DELETED_FN (fn))
4580 /* Not deleted, therefore trivial. */
4581 saw_non_deleted = true;
4582 break;
4585 else if (move_fn_p (fn))
4586 if (!DECL_DELETED_FN (fn))
4587 saw_non_deleted_move = true;
4590 gcc_assert (saw_copy);
4592 /* ABI v12 buggily ignored move constructors. */
4593 bool v11nontriv = false;
4594 bool v12nontriv = !saw_non_deleted;
4595 bool v13nontriv = !saw_non_deleted && !saw_non_deleted_move;
4596 bool nontriv = (abi_version_at_least (13) ? v13nontriv
4597 : flag_abi_version == 12 ? v12nontriv
4598 : v11nontriv);
4599 bool warn_nontriv = (warn_abi_version >= 13 ? v13nontriv
4600 : warn_abi_version == 12 ? v12nontriv
4601 : v11nontriv);
4602 if (nontriv != warn_nontriv)
4603 remember_deleted_copy (t);
4605 return nontriv;
4607 else
4608 return 0;
4611 /* Returns 1 iff type T is a trivially copyable type, as defined in
4612 [basic.types] and [class]. */
4614 bool
4615 trivially_copyable_p (const_tree t)
4617 t = strip_array_types (CONST_CAST_TREE (t));
4619 if (CLASS_TYPE_P (t))
4620 return ((!TYPE_HAS_COPY_CTOR (t)
4621 || !TYPE_HAS_COMPLEX_COPY_CTOR (t))
4622 && !TYPE_HAS_COMPLEX_MOVE_CTOR (t)
4623 && (!TYPE_HAS_COPY_ASSIGN (t)
4624 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (t))
4625 && !TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
4626 && TYPE_HAS_TRIVIAL_DESTRUCTOR (t));
4627 else
4628 /* CWG 2094 makes volatile-qualified scalars trivially copyable again. */
4629 return scalarish_type_p (t);
4632 /* Returns 1 iff type T is a trivial type, as defined in [basic.types] and
4633 [class]. */
4635 bool
4636 trivial_type_p (const_tree t)
4638 t = strip_array_types (CONST_CAST_TREE (t));
4640 if (CLASS_TYPE_P (t))
4641 /* A trivial class is a class that is trivially copyable and has one or
4642 more eligible default constructors, all of which are trivial. */
4643 return (type_has_non_deleted_trivial_default_ctor (CONST_CAST_TREE (t))
4644 && trivially_copyable_p (t));
4645 else
4646 return scalarish_type_p (t);
4649 /* Returns 1 iff type T is a POD type, as defined in [basic.types]. */
4651 bool
4652 pod_type_p (const_tree t)
4654 /* This CONST_CAST is okay because strip_array_types returns its
4655 argument unmodified and we assign it to a const_tree. */
4656 t = strip_array_types (CONST_CAST_TREE(t));
4658 if (!CLASS_TYPE_P (t))
4659 return scalarish_type_p (t);
4660 else if (cxx_dialect > cxx98)
4661 /* [class]/10: A POD struct is a class that is both a trivial class and a
4662 standard-layout class, and has no non-static data members of type
4663 non-POD struct, non-POD union (or array of such types).
4665 We don't need to check individual members because if a member is
4666 non-std-layout or non-trivial, the class will be too. */
4667 return (std_layout_type_p (t) && trivial_type_p (t));
4668 else
4669 /* The C++98 definition of POD is different. */
4670 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
4673 /* Returns true iff T is POD for the purpose of layout, as defined in the
4674 C++ ABI. */
4676 bool
4677 layout_pod_type_p (const_tree t)
4679 t = strip_array_types (CONST_CAST_TREE (t));
4681 if (CLASS_TYPE_P (t))
4682 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
4683 else
4684 return scalarish_type_p (t);
4687 /* Returns true iff T is a standard-layout type, as defined in
4688 [basic.types]. */
4690 bool
4691 std_layout_type_p (const_tree t)
4693 t = strip_array_types (CONST_CAST_TREE (t));
4695 if (CLASS_TYPE_P (t))
4696 return !CLASSTYPE_NON_STD_LAYOUT (t);
4697 else
4698 return scalarish_type_p (t);
4701 static bool record_has_unique_obj_representations (const_tree, const_tree);
4703 /* Returns true iff T satisfies std::has_unique_object_representations<T>,
4704 as defined in [meta.unary.prop]. */
4706 bool
4707 type_has_unique_obj_representations (const_tree t)
4709 bool ret;
4711 t = strip_array_types (CONST_CAST_TREE (t));
4713 if (!trivially_copyable_p (t))
4714 return false;
4716 if (CLASS_TYPE_P (t) && CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t))
4717 return CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t);
4719 switch (TREE_CODE (t))
4721 case INTEGER_TYPE:
4722 case POINTER_TYPE:
4723 case REFERENCE_TYPE:
4724 /* If some backend has any paddings in these types, we should add
4725 a target hook for this and handle it there. */
4726 return true;
4728 case BOOLEAN_TYPE:
4729 /* For bool values other than 0 and 1 should only appear with
4730 undefined behavior. */
4731 return true;
4733 case ENUMERAL_TYPE:
4734 return type_has_unique_obj_representations (ENUM_UNDERLYING_TYPE (t));
4736 case REAL_TYPE:
4737 /* XFmode certainly contains padding on x86, which the CPU doesn't store
4738 when storing long double values, so for that we have to return false.
4739 Other kinds of floating point values are questionable due to +.0/-.0
4740 and NaNs, let's play safe for now. */
4741 return false;
4743 case FIXED_POINT_TYPE:
4744 return false;
4746 case OFFSET_TYPE:
4747 return true;
4749 case COMPLEX_TYPE:
4750 case VECTOR_TYPE:
4751 return type_has_unique_obj_representations (TREE_TYPE (t));
4753 case RECORD_TYPE:
4754 ret = record_has_unique_obj_representations (t, TYPE_SIZE (t));
4755 if (CLASS_TYPE_P (t))
4757 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
4758 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
4760 return ret;
4762 case UNION_TYPE:
4763 ret = true;
4764 bool any_fields;
4765 any_fields = false;
4766 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4767 if (TREE_CODE (field) == FIELD_DECL)
4769 any_fields = true;
4770 if (!type_has_unique_obj_representations (TREE_TYPE (field))
4771 || simple_cst_equal (DECL_SIZE (field), TYPE_SIZE (t)) != 1)
4773 ret = false;
4774 break;
4777 if (!any_fields && !integer_zerop (TYPE_SIZE (t)))
4778 ret = false;
4779 if (CLASS_TYPE_P (t))
4781 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
4782 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
4784 return ret;
4786 case NULLPTR_TYPE:
4787 return false;
4789 case ERROR_MARK:
4790 return false;
4792 default:
4793 gcc_unreachable ();
4797 /* Helper function for type_has_unique_obj_representations. */
4799 static bool
4800 record_has_unique_obj_representations (const_tree t, const_tree sz)
4802 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4803 if (TREE_CODE (field) != FIELD_DECL)
4805 /* For bases, can't use type_has_unique_obj_representations here, as in
4806 struct S { int i : 24; S (); };
4807 struct T : public S { int j : 8; T (); };
4808 S doesn't have unique obj representations, but T does. */
4809 else if (DECL_FIELD_IS_BASE (field))
4811 if (!record_has_unique_obj_representations (TREE_TYPE (field),
4812 DECL_SIZE (field)))
4813 return false;
4815 else if (DECL_C_BIT_FIELD (field) && !DECL_UNNAMED_BIT_FIELD (field))
4817 tree btype = DECL_BIT_FIELD_TYPE (field);
4818 if (!type_has_unique_obj_representations (btype))
4819 return false;
4821 else if (!type_has_unique_obj_representations (TREE_TYPE (field)))
4822 return false;
4824 offset_int cur = 0;
4825 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4826 if (TREE_CODE (field) == FIELD_DECL && !DECL_UNNAMED_BIT_FIELD (field))
4828 offset_int fld = wi::to_offset (DECL_FIELD_OFFSET (field));
4829 offset_int bitpos = wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
4830 fld = fld * BITS_PER_UNIT + bitpos;
4831 if (cur != fld)
4832 return false;
4833 if (DECL_SIZE (field))
4835 offset_int size = wi::to_offset (DECL_SIZE (field));
4836 cur += size;
4839 if (cur != wi::to_offset (sz))
4840 return false;
4842 return true;
4845 /* Nonzero iff type T is a class template implicit specialization. */
4847 bool
4848 class_tmpl_impl_spec_p (const_tree t)
4850 return CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INSTANTIATION (t);
4853 /* Returns 1 iff zero initialization of type T means actually storing
4854 zeros in it. */
4857 zero_init_p (const_tree t)
4859 /* This CONST_CAST is okay because strip_array_types returns its
4860 argument unmodified and we assign it to a const_tree. */
4861 t = strip_array_types (CONST_CAST_TREE(t));
4863 if (t == error_mark_node)
4864 return 1;
4866 /* NULL pointers to data members are initialized with -1. */
4867 if (TYPE_PTRDATAMEM_P (t))
4868 return 0;
4870 /* Classes that contain types that can't be zero-initialized, cannot
4871 be zero-initialized themselves. */
4872 if (CLASS_TYPE_P (t) && CLASSTYPE_NON_ZERO_INIT_P (t))
4873 return 0;
4875 return 1;
4878 /* Returns true if the expression or initializer T is the result of
4879 zero-initialization for its type, taking pointers to members
4880 into consideration. */
4882 bool
4883 zero_init_expr_p (tree t)
4885 tree type = TREE_TYPE (t);
4886 if (!type || uses_template_parms (type))
4887 return false;
4888 if (TYPE_PTRMEM_P (type))
4889 return null_member_pointer_value_p (t);
4890 if (TREE_CODE (t) == CONSTRUCTOR)
4892 if (COMPOUND_LITERAL_P (t)
4893 || BRACE_ENCLOSED_INITIALIZER_P (t))
4894 /* Undigested, conversions might change the zeroness. */
4895 return false;
4896 for (constructor_elt &elt : CONSTRUCTOR_ELTS (t))
4898 if (TREE_CODE (type) == UNION_TYPE
4899 && elt.index != first_field (type))
4900 return false;
4901 if (!zero_init_expr_p (elt.value))
4902 return false;
4904 return true;
4906 if (zero_init_p (type))
4907 return initializer_zerop (t);
4908 return false;
4911 /* True IFF T is a C++20 structural type (P1907R1) that can be used as a
4912 non-type template parameter. If EXPLAIN, explain why not. */
4914 bool
4915 structural_type_p (tree t, bool explain)
4917 /* A structural type is one of the following: */
4919 /* a scalar type, or */
4920 if (SCALAR_TYPE_P (t))
4921 return true;
4922 /* an lvalue reference type, or */
4923 if (TYPE_REF_P (t) && !TYPE_REF_IS_RVALUE (t))
4924 return true;
4925 /* a literal class type with the following properties:
4926 - all base classes and non-static data members are public and non-mutable
4928 - the types of all bases classes and non-static data members are
4929 structural types or (possibly multi-dimensional) array thereof. */
4930 if (!CLASS_TYPE_P (t))
4931 return false;
4932 if (!literal_type_p (t))
4934 if (explain)
4935 explain_non_literal_class (t);
4936 return false;
4938 for (tree m = next_aggregate_field (TYPE_FIELDS (t)); m;
4939 m = next_aggregate_field (DECL_CHAIN (m)))
4941 if (TREE_PRIVATE (m) || TREE_PROTECTED (m))
4943 if (explain)
4945 if (DECL_FIELD_IS_BASE (m))
4946 inform (location_of (m), "base class %qT is not public",
4947 TREE_TYPE (m));
4948 else
4949 inform (location_of (m), "%qD is not public", m);
4951 return false;
4953 if (DECL_MUTABLE_P (m))
4955 if (explain)
4956 inform (location_of (m), "%qD is mutable", m);
4957 return false;
4959 tree mtype = strip_array_types (TREE_TYPE (m));
4960 if (!structural_type_p (mtype))
4962 if (explain)
4964 inform (location_of (m), "%qD has a non-structural type", m);
4965 structural_type_p (mtype, true);
4967 return false;
4970 return true;
4973 /* Partially handle the C++11 [[carries_dependency]] attribute.
4974 Just emit a different diagnostics when it is used on something the
4975 spec doesn't allow vs. where it allows and we just choose to ignore
4976 it. */
4978 static tree
4979 handle_carries_dependency_attribute (tree *node, tree name,
4980 tree ARG_UNUSED (args),
4981 int ARG_UNUSED (flags),
4982 bool *no_add_attrs)
4984 if (TREE_CODE (*node) != FUNCTION_DECL
4985 && TREE_CODE (*node) != PARM_DECL)
4987 warning (OPT_Wattributes, "%qE attribute can only be applied to "
4988 "functions or parameters", name);
4989 *no_add_attrs = true;
4991 else
4993 warning (OPT_Wattributes, "%qE attribute ignored", name);
4994 *no_add_attrs = true;
4996 return NULL_TREE;
4999 /* Handle the C++17 [[nodiscard]] attribute, which is similar to the GNU
5000 warn_unused_result attribute. */
5002 static tree
5003 handle_nodiscard_attribute (tree *node, tree name, tree args,
5004 int /*flags*/, bool *no_add_attrs)
5006 if (args && TREE_CODE (TREE_VALUE (args)) != STRING_CST)
5008 error ("%qE attribute argument must be a string constant", name);
5009 *no_add_attrs = true;
5011 if (TREE_CODE (*node) == FUNCTION_DECL)
5013 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node)))
5014 && !DECL_CONSTRUCTOR_P (*node))
5015 warning_at (DECL_SOURCE_LOCATION (*node),
5016 OPT_Wattributes, "%qE attribute applied to %qD with void "
5017 "return type", name, *node);
5019 else if (OVERLOAD_TYPE_P (*node))
5020 /* OK */;
5021 else
5023 warning (OPT_Wattributes, "%qE attribute can only be applied to "
5024 "functions or to class or enumeration types", name);
5025 *no_add_attrs = true;
5027 return NULL_TREE;
5030 /* Handle a C++20 "no_unique_address" attribute; arguments as in
5031 struct attribute_spec.handler. */
5032 static tree
5033 handle_no_unique_addr_attribute (tree* node,
5034 tree name,
5035 tree /*args*/,
5036 int /*flags*/,
5037 bool* no_add_attrs)
5039 if (TREE_CODE (*node) == VAR_DECL)
5041 DECL_MERGEABLE (*node) = true;
5042 if (pedantic)
5043 warning (OPT_Wattributes, "%qE attribute can only be applied to "
5044 "non-static data members", name);
5046 else if (TREE_CODE (*node) != FIELD_DECL)
5048 warning (OPT_Wattributes, "%qE attribute can only be applied to "
5049 "non-static data members", name);
5050 *no_add_attrs = true;
5052 else if (DECL_C_BIT_FIELD (*node))
5054 warning (OPT_Wattributes, "%qE attribute cannot be applied to "
5055 "a bit-field", name);
5056 *no_add_attrs = true;
5059 return NULL_TREE;
5062 /* The C++20 [[likely]] and [[unlikely]] attributes on labels map to the GNU
5063 hot/cold attributes. */
5065 static tree
5066 handle_likeliness_attribute (tree *node, tree name, tree args,
5067 int flags, bool *no_add_attrs)
5069 *no_add_attrs = true;
5070 if (TREE_CODE (*node) == LABEL_DECL
5071 || TREE_CODE (*node) == FUNCTION_DECL)
5073 if (args)
5074 warning (OPT_Wattributes, "%qE attribute takes no arguments", name);
5075 tree bname = (is_attribute_p ("likely", name)
5076 ? get_identifier ("hot") : get_identifier ("cold"));
5077 if (TREE_CODE (*node) == FUNCTION_DECL)
5078 warning (OPT_Wattributes, "ISO C++ %qE attribute does not apply to "
5079 "functions; treating as %<[[gnu::%E]]%>", name, bname);
5080 tree battr = build_tree_list (bname, NULL_TREE);
5081 decl_attributes (node, battr, flags);
5082 return NULL_TREE;
5084 else
5085 return error_mark_node;
5088 /* Table of valid C++ attributes. */
5089 static const attribute_spec cxx_gnu_attributes[] =
5091 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
5092 affects_type_identity, handler, exclude } */
5093 { "init_priority", 1, 1, true, false, false, false,
5094 handle_init_priority_attribute, NULL },
5095 { "abi_tag", 1, -1, false, false, false, true,
5096 handle_abi_tag_attribute, NULL },
5097 { "no_dangling", 0, 1, false, true, false, false,
5098 handle_no_dangling_attribute, NULL },
5101 const scoped_attribute_specs cxx_gnu_attribute_table =
5103 "gnu", { cxx_gnu_attributes }
5106 /* Table of C++ standard attributes. */
5107 static const attribute_spec std_attributes[] =
5109 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
5110 affects_type_identity, handler, exclude } */
5111 { "maybe_unused", 0, 0, false, false, false, false,
5112 handle_unused_attribute, NULL },
5113 { "nodiscard", 0, 1, false, false, false, false,
5114 handle_nodiscard_attribute, NULL },
5115 { "no_unique_address", 0, 0, true, false, false, false,
5116 handle_no_unique_addr_attribute, NULL },
5117 { "likely", 0, 0, false, false, false, false,
5118 handle_likeliness_attribute, attr_cold_hot_exclusions },
5119 { "unlikely", 0, 0, false, false, false, false,
5120 handle_likeliness_attribute, attr_cold_hot_exclusions },
5121 { "noreturn", 0, 0, true, false, false, false,
5122 handle_noreturn_attribute, attr_noreturn_exclusions },
5123 { "carries_dependency", 0, 0, true, false, false, false,
5124 handle_carries_dependency_attribute, NULL },
5125 { "pre", 0, -1, false, false, false, false,
5126 handle_contract_attribute, NULL },
5127 { "post", 0, -1, false, false, false, false,
5128 handle_contract_attribute, NULL }
5131 const scoped_attribute_specs std_attribute_table =
5133 nullptr, { std_attributes }
5136 /* Handle an "init_priority" attribute; arguments as in
5137 struct attribute_spec.handler. */
5138 static tree
5139 handle_init_priority_attribute (tree* node,
5140 tree name,
5141 tree args,
5142 int /*flags*/,
5143 bool* no_add_attrs)
5145 if (!SUPPORTS_INIT_PRIORITY)
5146 /* Treat init_priority as an unrecognized attribute (mirroring
5147 __has_attribute) if the target doesn't support init priorities. */
5148 return error_mark_node;
5150 tree initp_expr = TREE_VALUE (args);
5151 tree decl = *node;
5152 tree type = TREE_TYPE (decl);
5153 int pri;
5155 STRIP_NOPS (initp_expr);
5156 initp_expr = default_conversion (initp_expr);
5157 if (initp_expr)
5158 initp_expr = maybe_constant_value (initp_expr);
5160 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
5162 error ("requested %<init_priority%> is not an integer constant");
5163 cxx_constant_value (initp_expr);
5164 *no_add_attrs = true;
5165 return NULL_TREE;
5168 pri = TREE_INT_CST_LOW (initp_expr);
5170 type = strip_array_types (type);
5172 if (decl == NULL_TREE
5173 || !VAR_P (decl)
5174 || !TREE_STATIC (decl)
5175 || DECL_EXTERNAL (decl)
5176 || (TREE_CODE (type) != RECORD_TYPE
5177 && TREE_CODE (type) != UNION_TYPE)
5178 /* Static objects in functions are initialized the
5179 first time control passes through that
5180 function. This is not precise enough to pin down an
5181 init_priority value, so don't allow it. */
5182 || current_function_decl)
5184 error ("can only use %qE attribute on file-scope definitions "
5185 "of objects of class type", name);
5186 *no_add_attrs = true;
5187 return NULL_TREE;
5190 if (pri > MAX_INIT_PRIORITY || pri <= 0)
5192 error ("requested %<init_priority%> %i is out of range [0, %i]",
5193 pri, MAX_INIT_PRIORITY);
5194 *no_add_attrs = true;
5195 return NULL_TREE;
5198 /* Check for init_priorities that are reserved for
5199 language and runtime support implementations.*/
5200 if (pri <= MAX_RESERVED_INIT_PRIORITY
5201 && !in_system_header_at (input_location))
5203 warning
5204 (0, "requested %<init_priority%> %i is reserved for internal use",
5205 pri);
5208 SET_DECL_INIT_PRIORITY (decl, pri);
5209 DECL_HAS_INIT_PRIORITY_P (decl) = 1;
5210 return NULL_TREE;
5213 /* DECL is being redeclared; the old declaration had the abi tags in OLD,
5214 and the new one has the tags in NEW_. Give an error if there are tags
5215 in NEW_ that weren't in OLD. */
5217 bool
5218 check_abi_tag_redeclaration (const_tree decl, const_tree old, const_tree new_)
5220 if (old && TREE_CODE (TREE_VALUE (old)) == TREE_LIST)
5221 old = TREE_VALUE (old);
5222 if (new_ && TREE_CODE (TREE_VALUE (new_)) == TREE_LIST)
5223 new_ = TREE_VALUE (new_);
5224 bool err = false;
5225 auto_diagnostic_group d;
5226 for (const_tree t = new_; t; t = TREE_CHAIN (t))
5228 tree str = TREE_VALUE (t);
5229 for (const_tree in = old; in; in = TREE_CHAIN (in))
5231 tree ostr = TREE_VALUE (in);
5232 if (cp_tree_equal (str, ostr))
5233 goto found;
5235 error ("redeclaration of %qD adds abi tag %qE", decl, str);
5236 err = true;
5237 found:;
5239 if (err)
5241 inform (DECL_SOURCE_LOCATION (decl), "previous declaration here");
5242 return false;
5244 return true;
5247 /* The abi_tag attribute with the name NAME was given ARGS. If they are
5248 ill-formed, give an error and return false; otherwise, return true. */
5250 bool
5251 check_abi_tag_args (tree args, tree name)
5253 if (!args)
5255 error ("the %qE attribute requires arguments", name);
5256 return false;
5258 for (tree arg = args; arg; arg = TREE_CHAIN (arg))
5260 tree elt = TREE_VALUE (arg);
5261 if (TREE_CODE (elt) != STRING_CST
5262 || (!same_type_ignoring_top_level_qualifiers_p
5263 (strip_array_types (TREE_TYPE (elt)),
5264 char_type_node)))
5266 error ("arguments to the %qE attribute must be narrow string "
5267 "literals", name);
5268 return false;
5270 const char *begin = TREE_STRING_POINTER (elt);
5271 const char *end = begin + TREE_STRING_LENGTH (elt);
5272 for (const char *p = begin; p != end; ++p)
5274 char c = *p;
5275 if (p == begin)
5277 if (!ISALPHA (c) && c != '_')
5279 auto_diagnostic_group d;
5280 error ("arguments to the %qE attribute must contain valid "
5281 "identifiers", name);
5282 inform (input_location, "%<%c%> is not a valid first "
5283 "character for an identifier", c);
5284 return false;
5287 else if (p == end - 1)
5288 gcc_assert (c == 0);
5289 else
5291 if (!ISALNUM (c) && c != '_')
5293 auto_diagnostic_group d;
5294 error ("arguments to the %qE attribute must contain valid "
5295 "identifiers", name);
5296 inform (input_location, "%<%c%> is not a valid character "
5297 "in an identifier", c);
5298 return false;
5303 return true;
5306 /* Handle an "abi_tag" attribute; arguments as in
5307 struct attribute_spec.handler. */
5309 static tree
5310 handle_abi_tag_attribute (tree* node, tree name, tree args,
5311 int flags, bool* no_add_attrs)
5313 if (!check_abi_tag_args (args, name))
5314 goto fail;
5316 if (TYPE_P (*node))
5318 if (!OVERLOAD_TYPE_P (*node))
5320 error ("%qE attribute applied to non-class, non-enum type %qT",
5321 name, *node);
5322 goto fail;
5324 else if (!(flags & (int)ATTR_FLAG_TYPE_IN_PLACE))
5326 error ("%qE attribute applied to %qT after its definition",
5327 name, *node);
5328 goto fail;
5330 else if (CLASS_TYPE_P (*node)
5331 && CLASSTYPE_TEMPLATE_INSTANTIATION (*node))
5333 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
5334 "template instantiation %qT", name, *node);
5335 goto fail;
5337 else if (CLASS_TYPE_P (*node)
5338 && CLASSTYPE_TEMPLATE_SPECIALIZATION (*node))
5340 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
5341 "template specialization %qT", name, *node);
5342 goto fail;
5345 tree attributes = TYPE_ATTRIBUTES (*node);
5346 tree decl = TYPE_NAME (*node);
5348 /* Make sure all declarations have the same abi tags. */
5349 if (DECL_SOURCE_LOCATION (decl) != input_location)
5351 if (!check_abi_tag_redeclaration (decl,
5352 lookup_attribute ("abi_tag",
5353 attributes),
5354 args))
5355 goto fail;
5358 else
5360 if (!VAR_OR_FUNCTION_DECL_P (*node))
5362 error ("%qE attribute applied to non-function, non-variable %qD",
5363 name, *node);
5364 goto fail;
5366 else if (DECL_LANGUAGE (*node) == lang_c)
5368 error ("%qE attribute applied to extern \"C\" declaration %qD",
5369 name, *node);
5370 goto fail;
5374 return NULL_TREE;
5376 fail:
5377 *no_add_attrs = true;
5378 return NULL_TREE;
5381 /* Perform checking for contract attributes. */
5383 tree
5384 handle_contract_attribute (tree *ARG_UNUSED (node), tree ARG_UNUSED (name),
5385 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
5386 bool *ARG_UNUSED (no_add_attrs))
5388 /* TODO: Is there any checking we could do here? */
5389 return NULL_TREE;
5392 /* Handle a "no_dangling" attribute; arguments as in
5393 struct attribute_spec.handler. */
5395 tree
5396 handle_no_dangling_attribute (tree *node, tree name, tree args, int,
5397 bool *no_add_attrs)
5399 if (args && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
5401 error ("%qE attribute argument must be an expression that evaluates "
5402 "to true or false", name);
5403 *no_add_attrs = true;
5405 else if (!FUNC_OR_METHOD_TYPE_P (*node)
5406 && !RECORD_OR_UNION_TYPE_P (*node))
5408 warning (OPT_Wattributes, "%qE attribute ignored", name);
5409 *no_add_attrs = true;
5412 return NULL_TREE;
5415 /* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the
5416 thing pointed to by the constant. */
5418 tree
5419 make_ptrmem_cst (tree type, tree member)
5421 tree ptrmem_cst = make_node (PTRMEM_CST);
5422 TREE_TYPE (ptrmem_cst) = type;
5423 PTRMEM_CST_MEMBER (ptrmem_cst) = member;
5424 PTRMEM_CST_LOCATION (ptrmem_cst) = input_location;
5425 return ptrmem_cst;
5428 /* Build a variant of TYPE that has the indicated ATTRIBUTES. May
5429 return an existing type if an appropriate type already exists. */
5431 tree
5432 cp_build_type_attribute_variant (tree type, tree attributes)
5434 tree new_type;
5436 new_type = build_type_attribute_variant (type, attributes);
5437 if (FUNC_OR_METHOD_TYPE_P (new_type))
5438 gcc_checking_assert (cxx_type_hash_eq (type, new_type));
5440 /* Making a new main variant of a class type is broken. */
5441 gcc_assert (!CLASS_TYPE_P (type) || new_type == type);
5443 return new_type;
5446 /* Return TRUE if TYPE1 and TYPE2 are identical for type hashing purposes.
5447 Called only after doing all language independent checks. */
5449 bool
5450 cxx_type_hash_eq (const_tree typea, const_tree typeb)
5452 gcc_assert (FUNC_OR_METHOD_TYPE_P (typea));
5454 if (type_memfn_rqual (typea) != type_memfn_rqual (typeb))
5455 return false;
5456 if (TYPE_HAS_LATE_RETURN_TYPE (typea) != TYPE_HAS_LATE_RETURN_TYPE (typeb))
5457 return false;
5458 return comp_except_specs (TYPE_RAISES_EXCEPTIONS (typea),
5459 TYPE_RAISES_EXCEPTIONS (typeb), ce_exact);
5462 /* Copy the language-specific type variant modifiers from TYPEB to TYPEA. For
5463 C++, these are the exception-specifier and ref-qualifier. */
5465 tree
5466 cxx_copy_lang_qualifiers (const_tree typea, const_tree typeb)
5468 tree type = CONST_CAST_TREE (typea);
5469 if (FUNC_OR_METHOD_TYPE_P (type))
5470 type = build_cp_fntype_variant (type, type_memfn_rqual (typeb),
5471 TYPE_RAISES_EXCEPTIONS (typeb),
5472 TYPE_HAS_LATE_RETURN_TYPE (typeb));
5473 return type;
5476 /* Apply FUNC to all language-specific sub-trees of TP in a pre-order
5477 traversal. Called from walk_tree. */
5479 tree
5480 cp_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func,
5481 void *data, hash_set<tree> *pset)
5483 tree t = *tp;
5484 enum tree_code code = TREE_CODE (t);
5485 tree result;
5487 #define WALK_SUBTREE(NODE) \
5488 do \
5490 result = cp_walk_tree (&(NODE), func, data, pset); \
5491 if (result) goto out; \
5493 while (0)
5495 if (TYPE_P (t))
5497 /* If *WALK_SUBTREES_P is 1, we're interested in the syntactic form of
5498 the argument, so don't look through typedefs, but do walk into
5499 template arguments for alias templates (and non-typedefed classes).
5501 If *WALK_SUBTREES_P > 1, we're interested in type identity or
5502 equivalence, so look through typedefs, ignoring template arguments for
5503 alias templates, and walk into template args of classes.
5505 See find_abi_tags_r for an example of setting *WALK_SUBTREES_P to 2
5506 when that's the behavior the walk_tree_fn wants. */
5507 if (*walk_subtrees_p == 1 && typedef_variant_p (t))
5509 if (tree ti = TYPE_ALIAS_TEMPLATE_INFO (t))
5510 WALK_SUBTREE (TI_ARGS (ti));
5511 *walk_subtrees_p = 0;
5512 return NULL_TREE;
5515 if (tree ti = TYPE_TEMPLATE_INFO (t))
5516 WALK_SUBTREE (TI_ARGS (ti));
5519 /* Not one of the easy cases. We must explicitly go through the
5520 children. */
5521 result = NULL_TREE;
5522 switch (code)
5524 case TEMPLATE_TYPE_PARM:
5525 if (template_placeholder_p (t))
5526 WALK_SUBTREE (CLASS_PLACEHOLDER_TEMPLATE (t));
5527 /* Fall through. */
5528 case DEFERRED_PARSE:
5529 case TEMPLATE_TEMPLATE_PARM:
5530 case BOUND_TEMPLATE_TEMPLATE_PARM:
5531 case UNBOUND_CLASS_TEMPLATE:
5532 case TEMPLATE_PARM_INDEX:
5533 case TYPEOF_TYPE:
5534 /* None of these have subtrees other than those already walked
5535 above. */
5536 *walk_subtrees_p = 0;
5537 break;
5539 case TYPENAME_TYPE:
5540 WALK_SUBTREE (TYPE_CONTEXT (t));
5541 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
5542 *walk_subtrees_p = 0;
5543 break;
5545 case BASELINK:
5546 if (BASELINK_QUALIFIED_P (t))
5547 WALK_SUBTREE (BINFO_TYPE (BASELINK_ACCESS_BINFO (t)));
5548 WALK_SUBTREE (BASELINK_FUNCTIONS (t));
5549 *walk_subtrees_p = 0;
5550 break;
5552 case PTRMEM_CST:
5553 WALK_SUBTREE (TREE_TYPE (t));
5554 *walk_subtrees_p = 0;
5555 break;
5557 case TREE_LIST:
5558 WALK_SUBTREE (TREE_PURPOSE (t));
5559 break;
5561 case OVERLOAD:
5562 WALK_SUBTREE (OVL_FUNCTION (t));
5563 WALK_SUBTREE (OVL_CHAIN (t));
5564 *walk_subtrees_p = 0;
5565 break;
5567 case USING_DECL:
5568 WALK_SUBTREE (DECL_NAME (t));
5569 WALK_SUBTREE (USING_DECL_SCOPE (t));
5570 WALK_SUBTREE (USING_DECL_DECLS (t));
5571 *walk_subtrees_p = 0;
5572 break;
5574 case RECORD_TYPE:
5575 if (TYPE_PTRMEMFUNC_P (t))
5576 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (t));
5577 break;
5579 case TYPE_ARGUMENT_PACK:
5580 case NONTYPE_ARGUMENT_PACK:
5582 tree args = ARGUMENT_PACK_ARGS (t);
5583 for (tree arg : tree_vec_range (args))
5584 WALK_SUBTREE (arg);
5586 break;
5588 case TYPE_PACK_EXPANSION:
5589 WALK_SUBTREE (TREE_TYPE (t));
5590 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (t));
5591 *walk_subtrees_p = 0;
5592 break;
5594 case EXPR_PACK_EXPANSION:
5595 WALK_SUBTREE (TREE_OPERAND (t, 0));
5596 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (t));
5597 *walk_subtrees_p = 0;
5598 break;
5600 case CAST_EXPR:
5601 case REINTERPRET_CAST_EXPR:
5602 case STATIC_CAST_EXPR:
5603 case CONST_CAST_EXPR:
5604 case DYNAMIC_CAST_EXPR:
5605 case IMPLICIT_CONV_EXPR:
5606 case BIT_CAST_EXPR:
5607 if (TREE_TYPE (t))
5608 WALK_SUBTREE (TREE_TYPE (t));
5609 break;
5611 case CONSTRUCTOR:
5612 if (COMPOUND_LITERAL_P (t))
5613 WALK_SUBTREE (TREE_TYPE (t));
5614 break;
5616 case TRAIT_EXPR:
5617 WALK_SUBTREE (TRAIT_EXPR_TYPE1 (t));
5618 WALK_SUBTREE (TRAIT_EXPR_TYPE2 (t));
5619 *walk_subtrees_p = 0;
5620 break;
5622 case TRAIT_TYPE:
5623 WALK_SUBTREE (TRAIT_TYPE_TYPE1 (t));
5624 WALK_SUBTREE (TRAIT_TYPE_TYPE2 (t));
5625 *walk_subtrees_p = 0;
5626 break;
5628 case DECLTYPE_TYPE:
5630 cp_unevaluated u;
5631 WALK_SUBTREE (DECLTYPE_TYPE_EXPR (t));
5632 *walk_subtrees_p = 0;
5633 break;
5636 case ALIGNOF_EXPR:
5637 case SIZEOF_EXPR:
5638 case NOEXCEPT_EXPR:
5640 cp_unevaluated u;
5641 WALK_SUBTREE (TREE_OPERAND (t, 0));
5642 *walk_subtrees_p = 0;
5643 break;
5646 case REQUIRES_EXPR:
5648 cp_unevaluated u;
5649 for (tree parm = REQUIRES_EXPR_PARMS (t); parm; parm = DECL_CHAIN (parm))
5650 /* Walk the types of each parameter, but not the parameter itself,
5651 since doing so would cause false positives in the unexpanded pack
5652 checker if the requires-expr introduces a function parameter pack,
5653 e.g. requires (Ts... ts) { }. */
5654 WALK_SUBTREE (TREE_TYPE (parm));
5655 WALK_SUBTREE (REQUIRES_EXPR_REQS (t));
5656 *walk_subtrees_p = 0;
5657 break;
5660 case DECL_EXPR:
5661 /* User variables should be mentioned in BIND_EXPR_VARS
5662 and their initializers and sizes walked when walking
5663 the containing BIND_EXPR. Compiler temporaries are
5664 handled here. And also normal variables in templates,
5665 since do_poplevel doesn't build a BIND_EXPR then. */
5666 if (VAR_P (TREE_OPERAND (t, 0))
5667 && (processing_template_decl
5668 || (DECL_ARTIFICIAL (TREE_OPERAND (t, 0))
5669 && !TREE_STATIC (TREE_OPERAND (t, 0)))))
5671 tree decl = TREE_OPERAND (t, 0);
5672 WALK_SUBTREE (DECL_INITIAL (decl));
5673 WALK_SUBTREE (DECL_SIZE (decl));
5674 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
5676 break;
5678 case LAMBDA_EXPR:
5679 /* Don't walk into the body of the lambda, but the capture initializers
5680 are part of the enclosing context. */
5681 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
5682 cap = TREE_CHAIN (cap))
5683 WALK_SUBTREE (TREE_VALUE (cap));
5684 break;
5686 case CO_YIELD_EXPR:
5687 if (TREE_OPERAND (t, 1))
5688 /* Operand 1 is the tree for the relevant co_await which has any
5689 interesting sub-trees. */
5690 WALK_SUBTREE (TREE_OPERAND (t, 1));
5691 break;
5693 case CO_AWAIT_EXPR:
5694 if (TREE_OPERAND (t, 1))
5695 /* Operand 1 is frame variable. */
5696 WALK_SUBTREE (TREE_OPERAND (t, 1));
5697 if (TREE_OPERAND (t, 2))
5698 /* Operand 2 has the initialiser, and we need to walk any subtrees
5699 there. */
5700 WALK_SUBTREE (TREE_OPERAND (t, 2));
5701 break;
5703 case CO_RETURN_EXPR:
5704 if (TREE_OPERAND (t, 0))
5706 if (VOID_TYPE_P (TREE_OPERAND (t, 0)))
5707 /* For void expressions, operand 1 is a trivial call, and any
5708 interesting subtrees will be part of operand 0. */
5709 WALK_SUBTREE (TREE_OPERAND (t, 0));
5710 else if (TREE_OPERAND (t, 1))
5711 /* Interesting sub-trees will be in the return_value () call
5712 arguments. */
5713 WALK_SUBTREE (TREE_OPERAND (t, 1));
5715 break;
5717 case STATIC_ASSERT:
5718 WALK_SUBTREE (STATIC_ASSERT_CONDITION (t));
5719 WALK_SUBTREE (STATIC_ASSERT_MESSAGE (t));
5720 break;
5722 default:
5723 return NULL_TREE;
5726 /* We didn't find what we were looking for. */
5727 out:
5728 return result;
5730 #undef WALK_SUBTREE
5733 /* Like save_expr, but for C++. */
5735 tree
5736 cp_save_expr (tree expr)
5738 /* There is no reason to create a SAVE_EXPR within a template; if
5739 needed, we can create the SAVE_EXPR when instantiating the
5740 template. Furthermore, the middle-end cannot handle C++-specific
5741 tree codes. */
5742 if (processing_template_decl)
5743 return expr;
5745 /* TARGET_EXPRs are only expanded once. */
5746 if (TREE_CODE (expr) == TARGET_EXPR)
5747 return expr;
5749 return save_expr (expr);
5752 /* Initialize tree.cc. */
5754 void
5755 init_tree (void)
5757 list_hash_table = hash_table<list_hasher>::create_ggc (61);
5760 /* Returns the kind of special function that DECL (a FUNCTION_DECL)
5761 is. Note that sfk_none is zero, so this function can be used as a
5762 predicate to test whether or not DECL is a special function. */
5764 special_function_kind
5765 special_function_p (const_tree decl)
5767 /* Rather than doing all this stuff with magic names, we should
5768 probably have a field of type `special_function_kind' in
5769 DECL_LANG_SPECIFIC. */
5770 if (DECL_INHERITED_CTOR (decl))
5771 return sfk_inheriting_constructor;
5772 if (DECL_COPY_CONSTRUCTOR_P (decl))
5773 return sfk_copy_constructor;
5774 if (DECL_MOVE_CONSTRUCTOR_P (decl))
5775 return sfk_move_constructor;
5776 if (DECL_CONSTRUCTOR_P (decl))
5777 return sfk_constructor;
5778 if (DECL_ASSIGNMENT_OPERATOR_P (decl)
5779 && DECL_OVERLOADED_OPERATOR_IS (decl, NOP_EXPR))
5781 if (copy_fn_p (decl))
5782 return sfk_copy_assignment;
5783 if (move_fn_p (decl))
5784 return sfk_move_assignment;
5786 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
5787 return sfk_destructor;
5788 if (DECL_COMPLETE_DESTRUCTOR_P (decl))
5789 return sfk_complete_destructor;
5790 if (DECL_BASE_DESTRUCTOR_P (decl))
5791 return sfk_base_destructor;
5792 if (DECL_DELETING_DESTRUCTOR_P (decl))
5793 return sfk_deleting_destructor;
5794 if (DECL_CONV_FN_P (decl))
5795 return sfk_conversion;
5796 if (deduction_guide_p (decl))
5797 return sfk_deduction_guide;
5798 if (DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) >= OVL_OP_EQ_EXPR
5799 && DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) <= OVL_OP_SPACESHIP_EXPR)
5800 return sfk_comparison;
5802 return sfk_none;
5805 /* As above, but only if DECL is a special member function as per 11.3.3
5806 [special]: default/copy/move ctor, copy/move assignment, or destructor. */
5808 special_function_kind
5809 special_memfn_p (const_tree decl)
5811 switch (special_function_kind sfk = special_function_p (decl))
5813 case sfk_constructor:
5814 if (!default_ctor_p (decl))
5815 break;
5816 gcc_fallthrough();
5817 case sfk_copy_constructor:
5818 case sfk_copy_assignment:
5819 case sfk_move_assignment:
5820 case sfk_move_constructor:
5821 case sfk_destructor:
5822 return sfk;
5824 default:
5825 break;
5827 return sfk_none;
5830 /* Returns nonzero if TYPE is a character type, including wchar_t. */
5833 char_type_p (tree type)
5835 return (same_type_p (type, char_type_node)
5836 || same_type_p (type, unsigned_char_type_node)
5837 || same_type_p (type, signed_char_type_node)
5838 || same_type_p (type, char8_type_node)
5839 || same_type_p (type, char16_type_node)
5840 || same_type_p (type, char32_type_node)
5841 || same_type_p (type, wchar_type_node));
5844 /* Returns the kind of linkage associated with the indicated DECL. The
5845 value returned is as specified by the language standard; it is
5846 independent of implementation details regarding template
5847 instantiation, etc. For example, it is possible that a declaration
5848 to which this function assigns external linkage would not show up
5849 as a global symbol when you run `nm' on the resulting object file. */
5851 linkage_kind
5852 decl_linkage (tree decl)
5854 /* This function doesn't attempt to calculate the linkage from first
5855 principles as given in [basic.link]. Instead, it makes use of
5856 the fact that we have already set TREE_PUBLIC appropriately, and
5857 then handles a few special cases. Ideally, we would calculate
5858 linkage first, and then transform that into a concrete
5859 implementation. */
5861 /* An explicit type alias has no linkage. */
5862 if (TREE_CODE (decl) == TYPE_DECL
5863 && !DECL_IMPLICIT_TYPEDEF_P (decl)
5864 && !DECL_SELF_REFERENCE_P (decl))
5866 /* But this could be a typedef name for linkage purposes, in which
5867 case we're interested in the linkage of the main decl. */
5868 if (decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl))))
5869 decl = TYPE_MAIN_DECL (TREE_TYPE (decl));
5870 else
5871 return lk_none;
5874 /* Namespace-scope entities with no name usually have no linkage. */
5875 if (NAMESPACE_SCOPE_P (decl)
5876 && (!DECL_NAME (decl) || IDENTIFIER_ANON_P (DECL_NAME (decl))))
5878 if (TREE_CODE (decl) == TYPE_DECL && !TYPE_ANON_P (TREE_TYPE (decl)))
5879 /* This entity has a typedef name for linkage purposes. */;
5880 else if (TREE_CODE (decl) == NAMESPACE_DECL && cxx_dialect >= cxx11)
5881 /* An anonymous namespace has internal linkage since C++11. */
5882 return lk_internal;
5883 else
5884 return lk_none;
5887 /* Fields and parameters have no linkage. */
5888 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == PARM_DECL)
5889 return lk_none;
5891 /* Things in block scope do not have linkage. */
5892 if (decl_function_context (decl))
5893 return lk_none;
5895 /* Things in class scope have the linkage of their owning class. */
5896 if (tree ctype = DECL_CLASS_CONTEXT (decl))
5897 return decl_linkage (TYPE_NAME (ctype));
5899 /* Anonymous namespaces don't provide internal linkage in C++98,
5900 but otherwise consider such declarations to be internal. */
5901 if (cxx_dialect >= cxx11 && decl_internal_context_p (decl))
5902 return lk_internal;
5904 /* Templates don't properly propagate TREE_PUBLIC, consider the
5905 template result instead. Any template that isn't a variable
5906 or function must be external linkage by this point. */
5907 if (TREE_CODE (decl) == TEMPLATE_DECL)
5909 decl = DECL_TEMPLATE_RESULT (decl);
5910 if (!decl || !VAR_OR_FUNCTION_DECL_P (decl))
5911 return lk_external;
5914 /* Things that are TREE_PUBLIC have external linkage. */
5915 if (TREE_PUBLIC (decl))
5916 return lk_external;
5918 /* All types have external linkage in C++98, since anonymous namespaces
5919 didn't explicitly confer internal linkage. */
5920 if (TREE_CODE (decl) == TYPE_DECL && cxx_dialect < cxx11)
5921 return lk_external;
5923 /* Variables or function decls not marked as TREE_PUBLIC might still
5924 be external linkage, such as for template instantiations on targets
5925 without weak symbols, decls referring to internal-linkage entities,
5926 or compiler-generated entities; in such cases, decls really meant to
5927 have internal linkage will have DECL_THIS_STATIC set. */
5928 if (VAR_OR_FUNCTION_DECL_P (decl) && !DECL_THIS_STATIC (decl))
5929 return lk_external;
5931 /* Everything else has internal linkage. */
5932 return lk_internal;
5935 /* Returns the storage duration of the object or reference associated with
5936 the indicated DECL, which should be a VAR_DECL or PARM_DECL. */
5938 duration_kind
5939 decl_storage_duration (tree decl)
5941 if (TREE_CODE (decl) == PARM_DECL)
5942 return dk_auto;
5943 if (TREE_CODE (decl) == FUNCTION_DECL)
5944 return dk_static;
5945 gcc_assert (VAR_P (decl));
5946 if (!TREE_STATIC (decl)
5947 && !DECL_EXTERNAL (decl))
5948 return dk_auto;
5949 if (CP_DECL_THREAD_LOCAL_P (decl))
5950 return dk_thread;
5951 return dk_static;
5954 /* EXP is an expression that we want to pre-evaluate. Returns (in
5955 *INITP) an expression that will perform the pre-evaluation. The
5956 value returned by this function is a side-effect free expression
5957 equivalent to the pre-evaluated expression. Callers must ensure
5958 that *INITP is evaluated before EXP.
5960 Note that if EXPR is a glvalue, the return value is a glvalue denoting the
5961 same address; this function does not guard against modification of the
5962 stored value like save_expr or get_target_expr do. */
5964 tree
5965 stabilize_expr (tree exp, tree* initp)
5967 tree init_expr;
5969 if (!TREE_SIDE_EFFECTS (exp))
5970 init_expr = NULL_TREE;
5971 else if (VOID_TYPE_P (TREE_TYPE (exp)))
5973 init_expr = exp;
5974 exp = void_node;
5976 /* There are no expressions with REFERENCE_TYPE, but there can be call
5977 arguments with such a type; just treat it as a pointer. */
5978 else if (TYPE_REF_P (TREE_TYPE (exp))
5979 || SCALAR_TYPE_P (TREE_TYPE (exp))
5980 || !glvalue_p (exp))
5982 init_expr = get_target_expr (exp);
5983 exp = TARGET_EXPR_SLOT (init_expr);
5984 if (CLASS_TYPE_P (TREE_TYPE (exp)))
5985 exp = move (exp);
5986 else
5987 exp = rvalue (exp);
5989 else
5991 bool xval = !lvalue_p (exp);
5992 exp = cp_build_addr_expr (exp, tf_warning_or_error);
5993 init_expr = get_target_expr (exp);
5994 exp = TARGET_EXPR_SLOT (init_expr);
5995 exp = cp_build_fold_indirect_ref (exp);
5996 if (xval)
5997 exp = move (exp);
5999 *initp = init_expr;
6001 gcc_assert (!TREE_SIDE_EFFECTS (exp) || TREE_THIS_VOLATILE (exp));
6002 return exp;
6005 /* Add NEW_EXPR, an expression whose value we don't care about, after the
6006 similar expression ORIG. */
6008 tree
6009 add_stmt_to_compound (tree orig, tree new_expr)
6011 if (!new_expr || !TREE_SIDE_EFFECTS (new_expr))
6012 return orig;
6013 if (!orig || !TREE_SIDE_EFFECTS (orig))
6014 return new_expr;
6015 return build2 (COMPOUND_EXPR, void_type_node, orig, new_expr);
6018 /* Like stabilize_expr, but for a call whose arguments we want to
6019 pre-evaluate. CALL is modified in place to use the pre-evaluated
6020 arguments, while, upon return, *INITP contains an expression to
6021 compute the arguments. */
6023 void
6024 stabilize_call (tree call, tree *initp)
6026 tree inits = NULL_TREE;
6027 int i;
6028 int nargs = call_expr_nargs (call);
6030 if (call == error_mark_node || processing_template_decl)
6032 *initp = NULL_TREE;
6033 return;
6036 gcc_assert (TREE_CODE (call) == CALL_EXPR);
6038 for (i = 0; i < nargs; i++)
6040 tree init;
6041 CALL_EXPR_ARG (call, i) =
6042 stabilize_expr (CALL_EXPR_ARG (call, i), &init);
6043 inits = add_stmt_to_compound (inits, init);
6046 *initp = inits;
6049 /* Like stabilize_expr, but for an AGGR_INIT_EXPR whose arguments we want
6050 to pre-evaluate. CALL is modified in place to use the pre-evaluated
6051 arguments, while, upon return, *INITP contains an expression to
6052 compute the arguments. */
6054 static void
6055 stabilize_aggr_init (tree call, tree *initp)
6057 tree inits = NULL_TREE;
6058 int i;
6059 int nargs = aggr_init_expr_nargs (call);
6061 if (call == error_mark_node)
6062 return;
6064 gcc_assert (TREE_CODE (call) == AGGR_INIT_EXPR);
6066 for (i = 0; i < nargs; i++)
6068 tree init;
6069 AGGR_INIT_EXPR_ARG (call, i) =
6070 stabilize_expr (AGGR_INIT_EXPR_ARG (call, i), &init);
6071 inits = add_stmt_to_compound (inits, init);
6074 *initp = inits;
6077 /* Like stabilize_expr, but for an initialization.
6079 If the initialization is for an object of class type, this function
6080 takes care not to introduce additional temporaries.
6082 Returns TRUE iff the expression was successfully pre-evaluated,
6083 i.e., if INIT is now side-effect free, except for, possibly, a
6084 single call to a constructor. */
6086 bool
6087 stabilize_init (tree init, tree *initp)
6089 tree t = init;
6091 *initp = NULL_TREE;
6093 if (t == error_mark_node || processing_template_decl)
6094 return true;
6096 if (TREE_CODE (t) == INIT_EXPR)
6097 t = TREE_OPERAND (t, 1);
6098 if (TREE_CODE (t) == TARGET_EXPR)
6099 t = TARGET_EXPR_INITIAL (t);
6101 /* If the RHS can be stabilized without breaking copy elision, stabilize
6102 it. We specifically don't stabilize class prvalues here because that
6103 would mean an extra copy, but they might be stabilized below. */
6104 if (TREE_CODE (init) == INIT_EXPR
6105 && TREE_CODE (t) != CONSTRUCTOR
6106 && TREE_CODE (t) != AGGR_INIT_EXPR
6107 && (SCALAR_TYPE_P (TREE_TYPE (t))
6108 || glvalue_p (t)))
6110 TREE_OPERAND (init, 1) = stabilize_expr (t, initp);
6111 return true;
6114 if (TREE_CODE (t) == COMPOUND_EXPR
6115 && TREE_CODE (init) == INIT_EXPR)
6117 tree last = expr_last (t);
6118 /* Handle stabilizing the EMPTY_CLASS_EXPR pattern. */
6119 if (!TREE_SIDE_EFFECTS (last))
6121 *initp = t;
6122 TREE_OPERAND (init, 1) = last;
6123 return true;
6127 if (TREE_CODE (t) == CONSTRUCTOR)
6129 /* Aggregate initialization: stabilize each of the field
6130 initializers. */
6131 unsigned i;
6132 constructor_elt *ce;
6133 bool good = true;
6134 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
6135 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
6137 tree type = TREE_TYPE (ce->value);
6138 tree subinit;
6139 if (TYPE_REF_P (type)
6140 || SCALAR_TYPE_P (type))
6141 ce->value = stabilize_expr (ce->value, &subinit);
6142 else if (!stabilize_init (ce->value, &subinit))
6143 good = false;
6144 *initp = add_stmt_to_compound (*initp, subinit);
6146 return good;
6149 if (TREE_CODE (t) == CALL_EXPR)
6151 stabilize_call (t, initp);
6152 return true;
6155 if (TREE_CODE (t) == AGGR_INIT_EXPR)
6157 stabilize_aggr_init (t, initp);
6158 return true;
6161 /* The initialization is being performed via a bitwise copy -- and
6162 the item copied may have side effects. */
6163 return !TREE_SIDE_EFFECTS (init);
6166 /* Returns true if a cast to TYPE may appear in an integral constant
6167 expression. */
6169 bool
6170 cast_valid_in_integral_constant_expression_p (tree type)
6172 return (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6173 || cxx_dialect >= cxx11
6174 || dependent_type_p (type)
6175 || type == error_mark_node);
6178 /* Return true if we need to fix linkage information of DECL. */
6180 static bool
6181 cp_fix_function_decl_p (tree decl)
6183 /* Skip if DECL is not externally visible. */
6184 if (!TREE_PUBLIC (decl))
6185 return false;
6187 /* We need to fix DECL if it a appears to be exported but with no
6188 function body. Thunks do not have CFGs and we may need to
6189 handle them specially later. */
6190 if (!gimple_has_body_p (decl)
6191 && !DECL_THUNK_P (decl)
6192 && !DECL_EXTERNAL (decl))
6194 struct cgraph_node *node = cgraph_node::get (decl);
6196 /* Don't fix same_body aliases. Although they don't have their own
6197 CFG, they share it with what they alias to. */
6198 if (!node || !node->alias || !node->num_references ())
6199 return true;
6202 return false;
6205 /* Clean the C++ specific parts of the tree T. */
6207 void
6208 cp_free_lang_data (tree t)
6210 if (FUNC_OR_METHOD_TYPE_P (t))
6212 /* Default args are not interesting anymore. */
6213 tree argtypes = TYPE_ARG_TYPES (t);
6214 while (argtypes)
6216 TREE_PURPOSE (argtypes) = 0;
6217 argtypes = TREE_CHAIN (argtypes);
6220 else if (TREE_CODE (t) == FUNCTION_DECL
6221 && cp_fix_function_decl_p (t))
6223 /* If T is used in this translation unit at all, the definition
6224 must exist somewhere else since we have decided to not emit it
6225 in this TU. So make it an external reference. */
6226 DECL_EXTERNAL (t) = 1;
6227 TREE_STATIC (t) = 0;
6229 if (TREE_CODE (t) == NAMESPACE_DECL)
6230 /* We do not need the leftover chaining of namespaces from the
6231 binding level. */
6232 DECL_CHAIN (t) = NULL_TREE;
6235 /* Stub for c-common. Please keep in sync with c-decl.cc.
6236 FIXME: If address space support is target specific, then this
6237 should be a C target hook. But currently this is not possible,
6238 because this function is called via REGISTER_TARGET_PRAGMAS. */
6239 void
6240 c_register_addr_space (const char * /*word*/, addr_space_t /*as*/)
6244 /* Return the number of operands in T that we care about for things like
6245 mangling. */
6248 cp_tree_operand_length (const_tree t)
6250 enum tree_code code = TREE_CODE (t);
6252 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
6253 return VL_EXP_OPERAND_LENGTH (t);
6255 return cp_tree_code_length (code);
6258 /* Like cp_tree_operand_length, but takes a tree_code CODE. */
6261 cp_tree_code_length (enum tree_code code)
6263 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
6265 switch (code)
6267 case PREINCREMENT_EXPR:
6268 case PREDECREMENT_EXPR:
6269 case POSTINCREMENT_EXPR:
6270 case POSTDECREMENT_EXPR:
6271 return 1;
6273 case ARRAY_REF:
6274 return 2;
6276 case EXPR_PACK_EXPANSION:
6277 return 1;
6279 default:
6280 return TREE_CODE_LENGTH (code);
6284 /* Implement -Wzero_as_null_pointer_constant. Return true if the
6285 conditions for the warning hold, false otherwise. */
6286 bool
6287 maybe_warn_zero_as_null_pointer_constant (tree expr, location_t loc)
6289 if (c_inhibit_evaluation_warnings == 0
6290 && !null_node_p (expr) && !NULLPTR_TYPE_P (TREE_TYPE (expr)))
6292 warning_at (loc, OPT_Wzero_as_null_pointer_constant,
6293 "zero as null pointer constant");
6294 return true;
6296 return false;
6299 /* FNDECL is a function declaration whose type may have been altered by
6300 adding extra parameters such as this, in-charge, or VTT. When this
6301 takes place, the positional arguments supplied by the user (as in the
6302 'format' attribute arguments) may refer to the wrong argument. This
6303 function returns an integer indicating how many arguments should be
6304 skipped. */
6307 maybe_adjust_arg_pos_for_attribute (const_tree fndecl)
6309 if (!fndecl)
6310 return 0;
6311 int n = num_artificial_parms_for (fndecl);
6312 /* The manual states that it's the user's responsibility to account
6313 for the implicit this parameter. */
6314 return n > 0 ? n - 1 : 0;
6318 /* Release memory we no longer need after parsing. */
6319 void
6320 cp_tree_c_finish_parsing ()
6322 if (previous_class_level)
6323 invalidate_class_lookup_cache ();
6324 deleted_copy_types = NULL;
6327 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
6328 /* Complain that some language-specific thing hanging off a tree
6329 node has been accessed improperly. */
6331 void
6332 lang_check_failed (const char* file, int line, const char* function)
6334 internal_error ("%<lang_*%> check: failed in %s, at %s:%d",
6335 function, trim_filename (file), line);
6337 #endif /* ENABLE_TREE_CHECKING */
6339 #if CHECKING_P
6341 namespace selftest {
6343 /* Verify that lvalue_kind () works, for various expressions,
6344 and that location wrappers don't affect the results. */
6346 static void
6347 test_lvalue_kind ()
6349 location_t loc = BUILTINS_LOCATION;
6351 /* Verify constants and parameters, without and with
6352 location wrappers. */
6353 tree int_cst = build_int_cst (integer_type_node, 42);
6354 ASSERT_EQ (clk_none, lvalue_kind (int_cst));
6356 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
6357 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
6358 ASSERT_EQ (clk_none, lvalue_kind (wrapped_int_cst));
6360 tree string_lit = build_string (4, "foo");
6361 TREE_TYPE (string_lit) = char_array_type_node;
6362 string_lit = fix_string_type (string_lit);
6363 ASSERT_EQ (clk_ordinary, lvalue_kind (string_lit));
6365 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
6366 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
6367 ASSERT_EQ (clk_ordinary, lvalue_kind (wrapped_string_lit));
6369 tree parm = build_decl (UNKNOWN_LOCATION, PARM_DECL,
6370 get_identifier ("some_parm"),
6371 integer_type_node);
6372 ASSERT_EQ (clk_ordinary, lvalue_kind (parm));
6374 tree wrapped_parm = maybe_wrap_with_location (parm, loc);
6375 ASSERT_TRUE (location_wrapper_p (wrapped_parm));
6376 ASSERT_EQ (clk_ordinary, lvalue_kind (wrapped_parm));
6378 /* Verify that lvalue_kind of std::move on a parm isn't
6379 affected by location wrappers. */
6380 tree rvalue_ref_of_parm = move (parm);
6381 ASSERT_EQ (clk_rvalueref, lvalue_kind (rvalue_ref_of_parm));
6382 tree rvalue_ref_of_wrapped_parm = move (wrapped_parm);
6383 ASSERT_EQ (clk_rvalueref, lvalue_kind (rvalue_ref_of_wrapped_parm));
6385 /* Verify lvalue_p. */
6386 ASSERT_FALSE (lvalue_p (int_cst));
6387 ASSERT_FALSE (lvalue_p (wrapped_int_cst));
6388 ASSERT_TRUE (lvalue_p (parm));
6389 ASSERT_TRUE (lvalue_p (wrapped_parm));
6390 ASSERT_FALSE (lvalue_p (rvalue_ref_of_parm));
6391 ASSERT_FALSE (lvalue_p (rvalue_ref_of_wrapped_parm));
6394 /* Run all of the selftests within this file. */
6396 void
6397 cp_tree_cc_tests ()
6399 test_lvalue_kind ();
6402 } // namespace selftest
6404 #endif /* #if CHECKING_P */
6407 #include "gt-cp-tree.h"