[testsuite] [i386] work around fails with --enable-frame-pointer
[official-gcc.git] / gcc / cp / tree.cc
blobf1a23ffe817950570d3b36f2460b9c32d64d49dd
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 return clk_ordinary;
280 case MODIFY_EXPR:
281 case TYPEID_EXPR:
282 return clk_ordinary;
284 case COMPOUND_EXPR:
285 return lvalue_kind (TREE_OPERAND (ref, 1));
287 case TARGET_EXPR:
288 return clk_class;
290 case VA_ARG_EXPR:
291 return (CLASS_TYPE_P (TREE_TYPE (ref)) ? clk_class : clk_none);
293 case CALL_EXPR:
294 /* We can see calls outside of TARGET_EXPR in templates. */
295 if (CLASS_TYPE_P (TREE_TYPE (ref)))
296 return clk_class;
297 return clk_none;
299 case FUNCTION_DECL:
300 /* All functions (except non-static-member functions) are
301 lvalues. */
302 return (DECL_IOBJ_MEMBER_FUNCTION_P (ref)
303 ? clk_none : clk_ordinary);
305 case BASELINK:
306 /* We now represent a reference to a single static member function
307 with a BASELINK. */
308 /* This CONST_CAST is okay because BASELINK_FUNCTIONS returns
309 its argument unmodified and we assign it to a const_tree. */
310 return lvalue_kind (BASELINK_FUNCTIONS (CONST_CAST_TREE (ref)));
312 case PAREN_EXPR:
313 return lvalue_kind (TREE_OPERAND (ref, 0));
315 case TEMPLATE_PARM_INDEX:
316 if (CLASS_TYPE_P (TREE_TYPE (ref)))
317 /* A template parameter object is an lvalue. */
318 return clk_ordinary;
319 return clk_none;
321 default:
322 default_:
323 if (!TREE_TYPE (ref))
324 return clk_none;
325 if (CLASS_TYPE_P (TREE_TYPE (ref))
326 || TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE)
327 return clk_class;
328 return clk_none;
331 /* If one operand is not an lvalue at all, then this expression is
332 not an lvalue. */
333 if (!op1_lvalue_kind || !op2_lvalue_kind)
334 return clk_none;
336 /* Otherwise, it's an lvalue, and it has all the odd properties
337 contributed by either operand. */
338 op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind;
339 /* It's not an ordinary lvalue if it involves any other kind. */
340 if ((op1_lvalue_kind & ~clk_ordinary) != clk_none)
341 op1_lvalue_kind &= ~clk_ordinary;
342 /* It can't be both a pseudo-lvalue and a non-addressable lvalue.
343 A COND_EXPR of those should be wrapped in a TARGET_EXPR. */
344 if ((op1_lvalue_kind & (clk_rvalueref|clk_class))
345 && (op1_lvalue_kind & (clk_bitfield|clk_packed)))
346 op1_lvalue_kind = clk_none;
347 return op1_lvalue_kind;
350 /* Returns the kind of lvalue that REF is, in the sense of [basic.lval]. */
352 cp_lvalue_kind
353 real_lvalue_p (const_tree ref)
355 cp_lvalue_kind kind = lvalue_kind (ref);
356 if (kind & (clk_rvalueref|clk_class))
357 return clk_none;
358 else
359 return kind;
362 /* c-common wants us to return bool. */
364 bool
365 lvalue_p (const_tree t)
367 return real_lvalue_p (t);
370 /* This differs from lvalue_p in that xvalues are included. */
372 bool
373 glvalue_p (const_tree ref)
375 cp_lvalue_kind kind = lvalue_kind (ref);
376 if (kind & clk_class)
377 return false;
378 else
379 return (kind != clk_none);
382 /* This differs from glvalue_p in that class prvalues are included. */
384 bool
385 obvalue_p (const_tree ref)
387 return (lvalue_kind (ref) != clk_none);
390 /* Returns true if REF is an xvalue (the result of dereferencing an rvalue
391 reference), false otherwise. */
393 bool
394 xvalue_p (const_tree ref)
396 return (lvalue_kind (ref) & clk_rvalueref);
399 /* True if REF is a bit-field. */
401 bool
402 bitfield_p (const_tree ref)
404 return (lvalue_kind (ref) & clk_bitfield);
407 /* C++-specific version of stabilize_reference. */
409 tree
410 cp_stabilize_reference (tree ref)
412 if (processing_template_decl)
413 /* As in cp_save_expr. */
414 return ref;
416 STRIP_ANY_LOCATION_WRAPPER (ref);
417 switch (TREE_CODE (ref))
419 /* We need to treat specially anything stabilize_reference doesn't
420 handle specifically. */
421 case VAR_DECL:
422 case PARM_DECL:
423 case RESULT_DECL:
424 CASE_CONVERT:
425 case FLOAT_EXPR:
426 case FIX_TRUNC_EXPR:
427 case INDIRECT_REF:
428 case COMPONENT_REF:
429 case BIT_FIELD_REF:
430 case ARRAY_REF:
431 case ARRAY_RANGE_REF:
432 case ERROR_MARK:
433 break;
434 default:
435 cp_lvalue_kind kind = lvalue_kind (ref);
436 if ((kind & ~clk_class) != clk_none)
438 tree type = unlowered_expr_type (ref);
439 bool rval = !!(kind & clk_rvalueref);
440 type = cp_build_reference_type (type, rval);
441 /* This inhibits warnings in, eg, cxx_mark_addressable
442 (c++/60955). */
443 warning_sentinel s (extra_warnings);
444 ref = build_static_cast (input_location, type, ref,
445 tf_error);
449 return stabilize_reference (ref);
452 /* Test whether DECL is a builtin that may appear in a
453 constant-expression. */
455 bool
456 builtin_valid_in_constant_expr_p (const_tree decl)
458 STRIP_ANY_LOCATION_WRAPPER (decl);
459 if (TREE_CODE (decl) != FUNCTION_DECL)
460 /* Not a function. */
461 return false;
462 if (DECL_BUILT_IN_CLASS (decl) != BUILT_IN_NORMAL)
464 if (fndecl_built_in_p (decl, BUILT_IN_FRONTEND))
465 switch (DECL_FE_FUNCTION_CODE (decl))
467 case CP_BUILT_IN_IS_CONSTANT_EVALUATED:
468 case CP_BUILT_IN_SOURCE_LOCATION:
469 case CP_BUILT_IN_IS_CORRESPONDING_MEMBER:
470 case CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS:
471 return true;
472 default:
473 break;
475 /* Not a built-in. */
476 return false;
478 switch (DECL_FUNCTION_CODE (decl))
480 /* These always have constant results like the corresponding
481 macros/symbol. */
482 case BUILT_IN_FILE:
483 case BUILT_IN_FUNCTION:
484 case BUILT_IN_LINE:
486 /* The following built-ins are valid in constant expressions
487 when their arguments are. */
488 case BUILT_IN_ADD_OVERFLOW_P:
489 case BUILT_IN_SUB_OVERFLOW_P:
490 case BUILT_IN_MUL_OVERFLOW_P:
492 /* These have constant results even if their operands are
493 non-constant. */
494 case BUILT_IN_CONSTANT_P:
495 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
496 return true;
497 default:
498 return false;
502 /* Build a TARGET_EXPR, initializing the DECL with the VALUE. */
504 static tree
505 build_target_expr (tree decl, tree value, tsubst_flags_t complain)
507 tree t;
508 tree type = TREE_TYPE (decl);
510 value = mark_rvalue_use (value);
512 gcc_checking_assert (VOID_TYPE_P (TREE_TYPE (value))
513 || TREE_TYPE (decl) == TREE_TYPE (value)
514 /* On ARM ctors return 'this'. */
515 || (TYPE_PTR_P (TREE_TYPE (value))
516 && TREE_CODE (value) == CALL_EXPR)
517 || useless_type_conversion_p (TREE_TYPE (decl),
518 TREE_TYPE (value)));
520 /* Set TREE_READONLY for optimization, such as gimplify_init_constructor
521 moving a constant aggregate into .rodata. */
522 if (CP_TYPE_CONST_NON_VOLATILE_P (type)
523 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
524 && !VOID_TYPE_P (TREE_TYPE (value))
525 && !TYPE_HAS_MUTABLE_P (type)
526 && reduced_constant_expression_p (value))
527 TREE_READONLY (decl) = true;
529 if (complain & tf_no_cleanup)
530 /* The caller is building a new-expr and does not need a cleanup. */
531 t = NULL_TREE;
532 else
534 t = cxx_maybe_build_cleanup (decl, complain);
535 if (t == error_mark_node)
536 return error_mark_node;
539 set_target_expr_eliding (value);
541 t = build4 (TARGET_EXPR, type, decl, value, t, NULL_TREE);
542 if (location_t eloc = cp_expr_location (value))
543 SET_EXPR_LOCATION (t, eloc);
544 /* We always set TREE_SIDE_EFFECTS so that expand_expr does not
545 ignore the TARGET_EXPR. If there really turn out to be no
546 side-effects, then the optimizer should be able to get rid of
547 whatever code is generated anyhow. */
548 TREE_SIDE_EFFECTS (t) = 1;
550 return t;
553 /* Return an undeclared local temporary of type TYPE for use in building a
554 TARGET_EXPR. */
556 tree
557 build_local_temp (tree type)
559 tree slot = build_decl (input_location,
560 VAR_DECL, NULL_TREE, type);
561 DECL_ARTIFICIAL (slot) = 1;
562 DECL_IGNORED_P (slot) = 1;
563 DECL_CONTEXT (slot) = current_function_decl;
564 layout_decl (slot, 0);
565 return slot;
568 /* Return whether DECL is such a local temporary (or one from
569 create_tmp_var_raw). */
571 bool
572 is_local_temp (tree decl)
574 return (VAR_P (decl) && DECL_ARTIFICIAL (decl)
575 && !TREE_STATIC (decl));
578 /* Set various status flags when building an AGGR_INIT_EXPR object T. */
580 static void
581 process_aggr_init_operands (tree t)
583 bool side_effects;
585 side_effects = TREE_SIDE_EFFECTS (t);
586 if (!side_effects)
588 int i, n;
589 n = TREE_OPERAND_LENGTH (t);
590 for (i = 1; i < n; i++)
592 tree op = TREE_OPERAND (t, i);
593 if (op && TREE_SIDE_EFFECTS (op))
595 side_effects = 1;
596 break;
600 TREE_SIDE_EFFECTS (t) = side_effects;
603 /* Build an AGGR_INIT_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE,
604 FN, and SLOT. NARGS is the number of call arguments which are specified
605 as a tree array ARGS. */
607 static tree
608 build_aggr_init_array (tree return_type, tree fn, tree slot, int nargs,
609 tree *args)
611 tree t;
612 int i;
614 t = build_vl_exp (AGGR_INIT_EXPR, nargs + 3);
615 TREE_TYPE (t) = return_type;
616 AGGR_INIT_EXPR_FN (t) = fn;
617 AGGR_INIT_EXPR_SLOT (t) = slot;
618 for (i = 0; i < nargs; i++)
619 AGGR_INIT_EXPR_ARG (t, i) = args[i];
620 process_aggr_init_operands (t);
621 return t;
624 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
625 target. TYPE is the type to be initialized.
627 Build an AGGR_INIT_EXPR to represent the initialization. This function
628 differs from build_cplus_new in that an AGGR_INIT_EXPR can only be used
629 to initialize another object, whereas a TARGET_EXPR can either
630 initialize another object or create its own temporary object, and as a
631 result building up a TARGET_EXPR requires that the type's destructor be
632 callable. */
634 tree
635 build_aggr_init_expr (tree type, tree init)
637 tree fn;
638 tree slot;
639 tree rval;
640 int is_ctor;
642 gcc_assert (!VOID_TYPE_P (type));
644 /* Don't build AGGR_INIT_EXPR in a template. */
645 if (processing_template_decl)
646 return init;
648 fn = cp_get_callee (init);
649 if (fn == NULL_TREE)
650 return convert (type, init);
652 is_ctor = (TREE_CODE (fn) == ADDR_EXPR
653 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
654 && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0)));
656 /* We split the CALL_EXPR into its function and its arguments here.
657 Then, in expand_expr, we put them back together. The reason for
658 this is that this expression might be a default argument
659 expression. In that case, we need a new temporary every time the
660 expression is used. That's what break_out_target_exprs does; it
661 replaces every AGGR_INIT_EXPR with a copy that uses a fresh
662 temporary slot. Then, expand_expr builds up a call-expression
663 using the new slot. */
665 /* If we don't need to use a constructor to create an object of this
666 type, don't mess with AGGR_INIT_EXPR. */
667 if (is_ctor || TREE_ADDRESSABLE (type))
669 slot = build_local_temp (type);
671 if (TREE_CODE (init) == CALL_EXPR)
673 rval = build_aggr_init_array (void_type_node, fn, slot,
674 call_expr_nargs (init),
675 CALL_EXPR_ARGP (init));
676 AGGR_INIT_FROM_THUNK_P (rval)
677 = CALL_FROM_THUNK_P (init);
679 else
681 rval = build_aggr_init_array (void_type_node, fn, slot,
682 aggr_init_expr_nargs (init),
683 AGGR_INIT_EXPR_ARGP (init));
684 AGGR_INIT_FROM_THUNK_P (rval)
685 = AGGR_INIT_FROM_THUNK_P (init);
687 TREE_SIDE_EFFECTS (rval) = 1;
688 AGGR_INIT_VIA_CTOR_P (rval) = is_ctor;
689 TREE_NOTHROW (rval) = TREE_NOTHROW (init);
690 CALL_EXPR_OPERATOR_SYNTAX (rval) = CALL_EXPR_OPERATOR_SYNTAX (init);
691 CALL_EXPR_ORDERED_ARGS (rval) = CALL_EXPR_ORDERED_ARGS (init);
692 CALL_EXPR_REVERSE_ARGS (rval) = CALL_EXPR_REVERSE_ARGS (init);
693 SET_EXPR_LOCATION (rval, EXPR_LOCATION (init));
695 else
696 rval = init;
698 return rval;
701 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
702 target. TYPE is the type that this initialization should appear to
703 have.
705 Build an encapsulation of the initialization to perform
706 and return it so that it can be processed by language-independent
707 and language-specific expression expanders. */
709 tree
710 build_cplus_new (tree type, tree init, tsubst_flags_t complain)
712 /* This function should cope with what build_special_member_call
713 can produce. When performing parenthesized aggregate initialization,
714 it can produce a { }. */
715 if (BRACE_ENCLOSED_INITIALIZER_P (init))
717 gcc_assert (cxx_dialect >= cxx20);
718 return finish_compound_literal (type, init, complain);
721 tree rval = build_aggr_init_expr (type, init);
722 tree slot;
724 if (init == error_mark_node)
725 return error_mark_node;
727 if (!complete_type_or_maybe_complain (type, init, complain))
728 return error_mark_node;
730 /* Make sure that we're not trying to create an instance of an
731 abstract class. */
732 if (abstract_virtuals_error (NULL_TREE, type, complain))
733 return error_mark_node;
735 if (TREE_CODE (rval) == AGGR_INIT_EXPR)
736 slot = AGGR_INIT_EXPR_SLOT (rval);
737 else if (TREE_CODE (rval) == CALL_EXPR
738 || TREE_CODE (rval) == CONSTRUCTOR)
739 slot = build_local_temp (type);
740 else
741 return rval;
743 rval = build_target_expr (slot, rval, complain);
745 if (rval != error_mark_node)
746 TARGET_EXPR_IMPLICIT_P (rval) = 1;
748 return rval;
751 /* Subroutine of build_vec_init_expr: Build up a single element
752 intialization as a proxy for the full array initialization to get things
753 marked as used and any appropriate diagnostics.
755 This used to be necessary because we were deferring building the actual
756 constructor calls until gimplification time; now we only do it to set
757 VEC_INIT_EXPR_IS_CONSTEXPR.
759 We assume that init is either NULL_TREE, {}, void_type_node (indicating
760 value-initialization), or another array to copy. */
762 static tree
763 build_vec_init_elt (tree type, tree init, tsubst_flags_t complain)
765 tree inner_type = strip_array_types (type);
767 if (integer_zerop (array_type_nelts_total (type))
768 || !CLASS_TYPE_P (inner_type))
769 /* No interesting initialization to do. */
770 return integer_zero_node;
771 if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
773 /* Even if init has initializers for some array elements,
774 we're interested in the {}-init of trailing elements. */
775 if (CP_AGGREGATE_TYPE_P (inner_type))
777 tree empty = build_constructor (init_list_type_node, nullptr);
778 return digest_init (inner_type, empty, complain);
780 else
781 /* It's equivalent to value-init. */
782 init = void_type_node;
784 if (init == void_type_node)
785 return build_value_init (inner_type, complain);
787 releasing_vec argvec;
788 if (init && !BRACE_ENCLOSED_INITIALIZER_P (init))
790 tree init_type = strip_array_types (TREE_TYPE (init));
791 tree dummy = build_dummy_object (init_type);
792 if (!lvalue_p (init))
793 dummy = move (dummy);
794 argvec->quick_push (dummy);
796 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
797 &argvec, inner_type, LOOKUP_NORMAL,
798 complain);
800 /* For a trivial constructor, build_over_call creates a TARGET_EXPR. But
801 we don't want one here because we aren't creating a temporary. */
802 if (TREE_CODE (init) == TARGET_EXPR)
803 init = TARGET_EXPR_INITIAL (init);
805 return init;
808 /* Return a TARGET_EXPR which expresses the initialization of an array to
809 be named later, either default-initialization or copy-initialization
810 from another array of the same type. */
812 tree
813 build_vec_init_expr (tree type, tree init, tsubst_flags_t complain)
815 if (tree vi = get_vec_init_expr (init))
816 return vi;
818 tree elt_init;
819 if (init && TREE_CODE (init) == CONSTRUCTOR
820 && !BRACE_ENCLOSED_INITIALIZER_P (init))
821 /* We built any needed constructor calls in digest_init. */
822 elt_init = init;
823 else
824 elt_init = build_vec_init_elt (type, init, complain);
826 bool value_init = false;
827 if (init == void_type_node)
829 value_init = true;
830 init = NULL_TREE;
833 tree slot = build_local_temp (type);
834 init = build2 (VEC_INIT_EXPR, type, slot, init);
835 TREE_SIDE_EFFECTS (init) = true;
836 SET_EXPR_LOCATION (init, input_location);
838 if (cxx_dialect >= cxx11)
840 bool cx = potential_constant_expression (elt_init);
841 if (BRACE_ENCLOSED_INITIALIZER_P (init))
842 cx &= potential_constant_expression (init);
843 VEC_INIT_EXPR_IS_CONSTEXPR (init) = cx;
845 VEC_INIT_EXPR_VALUE_INIT (init) = value_init;
847 return init;
850 /* Call build_vec_init to expand VEC_INIT into TARGET (for which NULL_TREE
851 means VEC_INIT_EXPR_SLOT). */
853 tree
854 expand_vec_init_expr (tree target, tree vec_init, tsubst_flags_t complain,
855 vec<tree,va_gc> **flags)
857 iloc_sentinel ils = EXPR_LOCATION (vec_init);
859 if (!target)
860 target = VEC_INIT_EXPR_SLOT (vec_init);
861 tree init = VEC_INIT_EXPR_INIT (vec_init);
862 int from_array = (init && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE);
863 return build_vec_init (target, NULL_TREE, init,
864 VEC_INIT_EXPR_VALUE_INIT (vec_init),
865 from_array, complain, flags);
868 /* Give a helpful diagnostic for a non-constexpr VEC_INIT_EXPR in a context
869 that requires a constant expression. */
871 void
872 diagnose_non_constexpr_vec_init (tree expr)
874 tree type = TREE_TYPE (VEC_INIT_EXPR_SLOT (expr));
875 tree init, elt_init;
876 if (VEC_INIT_EXPR_VALUE_INIT (expr))
877 init = void_type_node;
878 else
879 init = VEC_INIT_EXPR_INIT (expr);
881 elt_init = build_vec_init_elt (type, init, tf_warning_or_error);
882 require_potential_constant_expression (elt_init);
885 tree
886 build_array_copy (tree init)
888 return get_target_expr (build_vec_init_expr
889 (TREE_TYPE (init), init, tf_warning_or_error));
892 /* Build a TARGET_EXPR using INIT to initialize a new temporary of the
893 indicated TYPE. */
895 tree
896 build_target_expr_with_type (tree init, tree type, tsubst_flags_t complain)
898 gcc_assert (!VOID_TYPE_P (type));
899 gcc_assert (!VOID_TYPE_P (TREE_TYPE (init)));
901 if (TREE_CODE (init) == TARGET_EXPR
902 || init == error_mark_node)
903 return init;
904 else if (CLASS_TYPE_P (type) && type_has_nontrivial_copy_init (type)
905 && TREE_CODE (init) != COND_EXPR
906 && TREE_CODE (init) != CONSTRUCTOR
907 && TREE_CODE (init) != VA_ARG_EXPR
908 && TREE_CODE (init) != CALL_EXPR)
909 /* We need to build up a copy constructor call. COND_EXPR is a special
910 case because we already have copies on the arms and we don't want
911 another one here. A CONSTRUCTOR is aggregate initialization, which
912 is handled separately. A VA_ARG_EXPR is magic creation of an
913 aggregate; there's no additional work to be done. A CALL_EXPR
914 already creates a prvalue. */
915 return force_rvalue (init, complain);
917 return force_target_expr (type, init, complain);
920 /* Like the above function, but without the checking. This function should
921 only be used by code which is deliberately trying to subvert the type
922 system, such as call_builtin_trap. Or build_over_call, to avoid
923 infinite recursion. */
925 tree
926 force_target_expr (tree type, tree init, tsubst_flags_t complain)
928 tree slot;
930 gcc_assert (!VOID_TYPE_P (type));
932 slot = build_local_temp (type);
933 return build_target_expr (slot, init, complain);
936 /* Like build_target_expr_with_type, but use the type of INIT. */
938 tree
939 get_target_expr (tree init, tsubst_flags_t complain /* = tf_warning_or_error */)
941 if (TREE_CODE (init) == AGGR_INIT_EXPR)
942 return build_target_expr (AGGR_INIT_EXPR_SLOT (init), init, complain);
943 else if (TREE_CODE (init) == VEC_INIT_EXPR)
944 return build_target_expr (VEC_INIT_EXPR_SLOT (init), init, complain);
945 else
947 init = convert_bitfield_to_declared_type (init);
948 return build_target_expr_with_type (init, TREE_TYPE (init), complain);
952 /* If EXPR is a bitfield reference, convert it to the declared type of
953 the bitfield, and return the resulting expression. Otherwise,
954 return EXPR itself. */
956 tree
957 convert_bitfield_to_declared_type (tree expr)
959 tree bitfield_type;
961 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
962 if (bitfield_type)
963 expr = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type),
964 expr);
965 return expr;
968 /* EXPR is being used in an rvalue context. Return a version of EXPR
969 that is marked as an rvalue. */
971 tree
972 rvalue (tree expr)
974 tree type;
976 if (error_operand_p (expr))
977 return expr;
979 expr = mark_rvalue_use (expr);
981 /* [expr.type]: "If a prvalue initially has the type "cv T", where T is a
982 cv-unqualified non-class, non-array type, the type of the expression is
983 adjusted to T prior to any further analysis. */
984 type = TREE_TYPE (expr);
985 if (!CLASS_TYPE_P (type) && TREE_CODE (type) != ARRAY_TYPE
986 && cv_qualified_p (type))
987 type = cv_unqualified (type);
989 /* We need to do this for rvalue refs as well to get the right answer
990 from decltype; see c++/36628. */
991 if (!processing_template_decl && glvalue_p (expr))
993 /* But don't use this function for class lvalues; use move (to treat an
994 lvalue as an xvalue) or force_rvalue (to make a prvalue copy). */
995 gcc_checking_assert (!CLASS_TYPE_P (type));
996 expr = build1 (NON_LVALUE_EXPR, type, expr);
998 else if (type != TREE_TYPE (expr))
999 expr = build_nop (type, expr);
1001 return expr;
1005 struct cplus_array_info
1007 tree type;
1008 tree domain;
1011 struct cplus_array_hasher : ggc_ptr_hash<tree_node>
1013 typedef cplus_array_info *compare_type;
1015 static hashval_t hash (tree t);
1016 static bool equal (tree, cplus_array_info *);
1019 /* Hash an ARRAY_TYPE. K is really of type `tree'. */
1021 hashval_t
1022 cplus_array_hasher::hash (tree t)
1024 hashval_t hash;
1026 hash = TYPE_UID (TREE_TYPE (t));
1027 if (TYPE_DOMAIN (t))
1028 hash ^= TYPE_UID (TYPE_DOMAIN (t));
1029 return hash;
1032 /* Compare two ARRAY_TYPEs. K1 is really of type `tree', K2 is really
1033 of type `cplus_array_info*'. */
1035 bool
1036 cplus_array_hasher::equal (tree t1, cplus_array_info *t2)
1038 return (TREE_TYPE (t1) == t2->type && TYPE_DOMAIN (t1) == t2->domain);
1041 /* Hash table containing dependent array types, which are unsuitable for
1042 the language-independent type hash table. */
1043 static GTY (()) hash_table<cplus_array_hasher> *cplus_array_htab;
1045 /* Build an ARRAY_TYPE without laying it out. */
1047 static tree
1048 build_min_array_type (tree elt_type, tree index_type)
1050 tree t = cxx_make_type (ARRAY_TYPE);
1051 TREE_TYPE (t) = elt_type;
1052 TYPE_DOMAIN (t) = index_type;
1053 return t;
1056 /* Set TYPE_CANONICAL like build_array_type_1, but using
1057 build_cplus_array_type. */
1059 static void
1060 set_array_type_canon (tree t, tree elt_type, tree index_type, bool dep)
1062 /* Set the canonical type for this new node. */
1063 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
1064 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
1065 SET_TYPE_STRUCTURAL_EQUALITY (t);
1066 else if (TYPE_CANONICAL (elt_type) != elt_type
1067 || (index_type && TYPE_CANONICAL (index_type) != index_type))
1068 TYPE_CANONICAL (t)
1069 = build_cplus_array_type (TYPE_CANONICAL (elt_type),
1070 index_type
1071 ? TYPE_CANONICAL (index_type) : index_type,
1072 dep);
1073 else
1074 TYPE_CANONICAL (t) = t;
1077 /* Like build_array_type, but handle special C++ semantics: an array of a
1078 variant element type is a variant of the array of the main variant of
1079 the element type. IS_DEPENDENT is -ve if we should determine the
1080 dependency. Otherwise its bool value indicates dependency. */
1082 tree
1083 build_cplus_array_type (tree elt_type, tree index_type, int dependent)
1085 tree t;
1087 if (elt_type == error_mark_node || index_type == error_mark_node)
1088 return error_mark_node;
1090 if (dependent < 0)
1091 dependent = (uses_template_parms (elt_type)
1092 || (index_type && uses_template_parms (index_type)));
1094 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
1095 /* Start with an array of the TYPE_MAIN_VARIANT. */
1096 t = build_cplus_array_type (TYPE_MAIN_VARIANT (elt_type),
1097 index_type, dependent);
1098 else if (dependent)
1100 /* Since type_hash_canon calls layout_type, we need to use our own
1101 hash table. */
1102 cplus_array_info cai;
1103 hashval_t hash;
1105 if (cplus_array_htab == NULL)
1106 cplus_array_htab = hash_table<cplus_array_hasher>::create_ggc (61);
1108 hash = TYPE_UID (elt_type);
1109 if (index_type)
1110 hash ^= TYPE_UID (index_type);
1111 cai.type = elt_type;
1112 cai.domain = index_type;
1114 tree *e = cplus_array_htab->find_slot_with_hash (&cai, hash, INSERT);
1115 if (*e)
1116 /* We have found the type: we're done. */
1117 return (tree) *e;
1118 else
1120 /* Build a new array type. */
1121 t = build_min_array_type (elt_type, index_type);
1123 /* Store it in the hash table. */
1124 *e = t;
1126 /* Set the canonical type for this new node. */
1127 set_array_type_canon (t, elt_type, index_type, dependent);
1129 /* Mark it as dependent now, this saves time later. */
1130 TYPE_DEPENDENT_P_VALID (t) = true;
1131 TYPE_DEPENDENT_P (t) = true;
1134 else
1136 bool typeless_storage = is_byte_access_type (elt_type);
1137 t = build_array_type (elt_type, index_type, typeless_storage);
1139 /* Mark as non-dependenty now, this will save time later. */
1140 TYPE_DEPENDENT_P_VALID (t) = true;
1143 /* Now check whether we already have this array variant. */
1144 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
1146 tree m = t;
1147 for (t = m; t; t = TYPE_NEXT_VARIANT (t))
1148 if (TREE_TYPE (t) == elt_type
1149 && TYPE_NAME (t) == NULL_TREE
1150 && TYPE_ATTRIBUTES (t) == NULL_TREE)
1151 break;
1152 if (!t)
1154 t = build_min_array_type (elt_type, index_type);
1155 /* Mark dependency now, this saves time later. */
1156 TYPE_DEPENDENT_P_VALID (t) = true;
1157 TYPE_DEPENDENT_P (t) = dependent;
1158 set_array_type_canon (t, elt_type, index_type, dependent);
1159 if (!dependent)
1161 layout_type (t);
1162 /* Make sure sizes are shared with the main variant.
1163 layout_type can't be called after setting TYPE_NEXT_VARIANT,
1164 as it will overwrite alignment etc. of all variants. */
1165 TYPE_SIZE (t) = TYPE_SIZE (m);
1166 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (m);
1167 TYPE_TYPELESS_STORAGE (t) = TYPE_TYPELESS_STORAGE (m);
1170 TYPE_MAIN_VARIANT (t) = m;
1171 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
1172 TYPE_NEXT_VARIANT (m) = t;
1176 /* Avoid spurious warnings with VLAs (c++/54583). */
1177 if (TYPE_SIZE (t) && EXPR_P (TYPE_SIZE (t)))
1178 suppress_warning (TYPE_SIZE (t), OPT_Wunused);
1180 /* Push these needs up to the ARRAY_TYPE so that initialization takes
1181 place more easily. */
1182 bool needs_ctor = (TYPE_NEEDS_CONSTRUCTING (t)
1183 = TYPE_NEEDS_CONSTRUCTING (elt_type));
1184 bool needs_dtor = (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1185 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type));
1187 if (!dependent && t == TYPE_MAIN_VARIANT (t)
1188 && !COMPLETE_TYPE_P (t) && COMPLETE_TYPE_P (elt_type))
1190 /* The element type has been completed since the last time we saw
1191 this array type; update the layout and 'tor flags for any variants
1192 that need it. */
1193 layout_type (t);
1194 for (tree v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1196 TYPE_NEEDS_CONSTRUCTING (v) = needs_ctor;
1197 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (v) = needs_dtor;
1201 return t;
1204 /* Return an ARRAY_TYPE with element type ELT and length N. */
1206 tree
1207 build_array_of_n_type (tree elt, int n)
1209 return build_cplus_array_type (elt, build_index_type (size_int (n - 1)));
1212 /* True iff T is an array of unknown bound. */
1214 bool
1215 array_of_unknown_bound_p (const_tree t)
1217 return (TREE_CODE (t) == ARRAY_TYPE
1218 && !TYPE_DOMAIN (t));
1221 /* True iff T is an N3639 array of runtime bound (VLA). These were approved
1222 for C++14 but then removed. This should only be used for N3639
1223 specifically; code wondering more generally if something is a VLA should use
1224 vla_type_p. */
1226 bool
1227 array_of_runtime_bound_p (tree t)
1229 if (!t || TREE_CODE (t) != ARRAY_TYPE)
1230 return false;
1231 if (variably_modified_type_p (TREE_TYPE (t), NULL_TREE))
1232 return false;
1233 tree dom = TYPE_DOMAIN (t);
1234 if (!dom)
1235 return false;
1236 tree max = TYPE_MAX_VALUE (dom);
1237 return (!potential_rvalue_constant_expression (max)
1238 || (!value_dependent_expression_p (max) && !TREE_CONSTANT (max)));
1241 /* True iff T is a variable length array. */
1243 bool
1244 vla_type_p (tree t)
1246 for (; t && TREE_CODE (t) == ARRAY_TYPE;
1247 t = TREE_TYPE (t))
1248 if (tree dom = TYPE_DOMAIN (t))
1250 tree max = TYPE_MAX_VALUE (dom);
1251 if (!potential_rvalue_constant_expression (max)
1252 || (!value_dependent_expression_p (max) && !TREE_CONSTANT (max)))
1253 return true;
1255 return false;
1259 /* Return a reference type node of MODE referring to TO_TYPE. If MODE
1260 is VOIDmode the standard pointer mode will be picked. If RVAL is
1261 true, return an rvalue reference type, otherwise return an lvalue
1262 reference type. If a type node exists, reuse it, otherwise create
1263 a new one. */
1264 tree
1265 cp_build_reference_type_for_mode (tree to_type, machine_mode mode, bool rval)
1267 tree lvalue_ref, t;
1269 if (to_type == error_mark_node)
1270 return error_mark_node;
1272 if (TYPE_REF_P (to_type))
1274 rval = rval && TYPE_REF_IS_RVALUE (to_type);
1275 to_type = TREE_TYPE (to_type);
1278 lvalue_ref = build_reference_type_for_mode (to_type, mode, false);
1280 if (!rval)
1281 return lvalue_ref;
1283 /* This code to create rvalue reference types is based on and tied
1284 to the code creating lvalue reference types in the middle-end
1285 functions build_reference_type_for_mode and build_reference_type.
1287 It works by putting the rvalue reference type nodes after the
1288 lvalue reference nodes in the TYPE_NEXT_REF_TO linked list, so
1289 they will effectively be ignored by the middle end. */
1291 for (t = lvalue_ref; (t = TYPE_NEXT_REF_TO (t)); )
1292 if (TYPE_REF_IS_RVALUE (t))
1293 return t;
1295 t = build_distinct_type_copy (lvalue_ref);
1297 TYPE_REF_IS_RVALUE (t) = true;
1298 TYPE_NEXT_REF_TO (t) = TYPE_NEXT_REF_TO (lvalue_ref);
1299 TYPE_NEXT_REF_TO (lvalue_ref) = t;
1301 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
1302 SET_TYPE_STRUCTURAL_EQUALITY (t);
1303 else if (TYPE_CANONICAL (to_type) != to_type)
1304 TYPE_CANONICAL (t)
1305 = cp_build_reference_type_for_mode (TYPE_CANONICAL (to_type), mode, rval);
1306 else
1307 TYPE_CANONICAL (t) = t;
1309 layout_type (t);
1311 return t;
1315 /* Return a reference type node referring to TO_TYPE. If RVAL is
1316 true, return an rvalue reference type, otherwise return an lvalue
1317 reference type. If a type node exists, reuse it, otherwise create
1318 a new one. */
1319 tree
1320 cp_build_reference_type (tree to_type, bool rval)
1322 return cp_build_reference_type_for_mode (to_type, VOIDmode, rval);
1325 /* Returns EXPR cast to rvalue reference type, like std::move. */
1327 tree
1328 move (tree expr)
1330 tree type = TREE_TYPE (expr);
1331 gcc_assert (!TYPE_REF_P (type));
1332 if (xvalue_p (expr))
1333 return expr;
1334 type = cp_build_reference_type (type, /*rval*/true);
1335 return build_static_cast (input_location, type, expr,
1336 tf_warning_or_error);
1339 /* Used by the C++ front end to build qualified array types. However,
1340 the C version of this function does not properly maintain canonical
1341 types (which are not used in C). */
1342 tree
1343 c_build_qualified_type (tree type, int type_quals, tree /* orig_qual_type */,
1344 size_t /* orig_qual_indirect */)
1346 return cp_build_qualified_type (type, type_quals);
1350 /* Make a variant of TYPE, qualified with the TYPE_QUALS. Handles
1351 arrays correctly. In particular, if TYPE is an array of T's, and
1352 TYPE_QUALS is non-empty, returns an array of qualified T's.
1354 FLAGS determines how to deal with ill-formed qualifications. If
1355 tf_ignore_bad_quals is set, then bad qualifications are dropped
1356 (this is permitted if TYPE was introduced via a typedef or template
1357 type parameter). If bad qualifications are dropped and tf_warning
1358 is set, then a warning is issued for non-const qualifications. If
1359 tf_ignore_bad_quals is not set and tf_error is not set, we
1360 return error_mark_node. Otherwise, we issue an error, and ignore
1361 the qualifications.
1363 Qualification of a reference type is valid when the reference came
1364 via a typedef or template type argument. [dcl.ref] No such
1365 dispensation is provided for qualifying a function type. [dcl.fct]
1366 DR 295 queries this and the proposed resolution brings it into line
1367 with qualifying a reference. We implement the DR. We also behave
1368 in a similar manner for restricting non-pointer types. */
1370 tree
1371 cp_build_qualified_type (tree type, int type_quals,
1372 tsubst_flags_t complain /* = tf_warning_or_error */)
1374 tree result;
1375 int bad_quals = TYPE_UNQUALIFIED;
1377 if (type == error_mark_node)
1378 return type;
1380 if (type_quals == cp_type_quals (type))
1381 return type;
1383 if (TREE_CODE (type) == ARRAY_TYPE)
1385 /* In C++, the qualification really applies to the array element
1386 type. Obtain the appropriately qualified element type. */
1387 tree t;
1388 tree element_type
1389 = cp_build_qualified_type (TREE_TYPE (type), type_quals, complain);
1391 if (element_type == error_mark_node)
1392 return error_mark_node;
1394 /* See if we already have an identically qualified type. Tests
1395 should be equivalent to those in check_qualified_type. */
1396 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1397 if (TREE_TYPE (t) == element_type
1398 && TYPE_NAME (t) == TYPE_NAME (type)
1399 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
1400 && attribute_list_equal (TYPE_ATTRIBUTES (t),
1401 TYPE_ATTRIBUTES (type)))
1402 break;
1404 if (!t)
1406 /* If we already know the dependentness, tell the array type
1407 constructor. This is important for module streaming, as we cannot
1408 dynamically determine that on read in. */
1409 t = build_cplus_array_type (element_type, TYPE_DOMAIN (type),
1410 TYPE_DEPENDENT_P_VALID (type)
1411 ? int (TYPE_DEPENDENT_P (type)) : -1);
1413 /* Keep the typedef name. */
1414 if (TYPE_NAME (t) != TYPE_NAME (type))
1416 t = build_variant_type_copy (t);
1417 TYPE_NAME (t) = TYPE_NAME (type);
1418 SET_TYPE_ALIGN (t, TYPE_ALIGN (type));
1419 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (type);
1423 /* Even if we already had this variant, we update
1424 TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case
1425 they changed since the variant was originally created.
1427 This seems hokey; if there is some way to use a previous
1428 variant *without* coming through here,
1429 TYPE_NEEDS_CONSTRUCTING will never be updated. */
1430 TYPE_NEEDS_CONSTRUCTING (t)
1431 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
1432 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1433 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
1434 return t;
1436 else if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
1438 tree t = PACK_EXPANSION_PATTERN (type);
1440 t = cp_build_qualified_type (t, type_quals, complain);
1441 return make_pack_expansion (t, complain);
1444 /* A reference or method type shall not be cv-qualified.
1445 [dcl.ref], [dcl.fct]. This used to be an error, but as of DR 295
1446 (in CD1) we always ignore extra cv-quals on functions. */
1448 /* [dcl.ref/1] Cv-qualified references are ill-formed except when
1449 the cv-qualifiers are introduced through the use of a typedef-name
1450 ([dcl.typedef], [temp.param]) or decltype-specifier
1451 ([dcl.type.decltype]),in which case the cv-qualifiers are
1452 ignored. */
1453 if (type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)
1454 && (TYPE_REF_P (type)
1455 || FUNC_OR_METHOD_TYPE_P (type)))
1457 if (TYPE_REF_P (type)
1458 && (!typedef_variant_p (type) || FUNC_OR_METHOD_TYPE_P (type)))
1459 bad_quals |= type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1460 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1463 /* But preserve any function-cv-quals on a FUNCTION_TYPE. */
1464 if (TREE_CODE (type) == FUNCTION_TYPE)
1465 type_quals |= type_memfn_quals (type);
1467 /* A restrict-qualified type must be a pointer (or reference)
1468 to object or incomplete type. */
1469 if ((type_quals & TYPE_QUAL_RESTRICT)
1470 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1471 && TREE_CODE (type) != TYPENAME_TYPE
1472 && !INDIRECT_TYPE_P (type))
1474 bad_quals |= TYPE_QUAL_RESTRICT;
1475 type_quals &= ~TYPE_QUAL_RESTRICT;
1478 if (bad_quals == TYPE_UNQUALIFIED
1479 || (complain & tf_ignore_bad_quals))
1480 /*OK*/;
1481 else if (!(complain & tf_error))
1482 return error_mark_node;
1483 else
1485 tree bad_type = build_qualified_type (ptr_type_node, bad_quals);
1486 error ("%qV qualifiers cannot be applied to %qT",
1487 bad_type, type);
1490 /* Retrieve (or create) the appropriately qualified variant. */
1491 result = build_qualified_type (type, type_quals);
1493 return result;
1496 /* Return TYPE with const and volatile removed. */
1498 tree
1499 cv_unqualified (tree type)
1501 int quals;
1503 if (type == error_mark_node)
1504 return type;
1506 quals = cp_type_quals (type);
1507 quals &= ~(TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE);
1508 return cp_build_qualified_type (type, quals);
1511 /* Subroutine of strip_typedefs. We want to apply to RESULT the attributes
1512 from ATTRIBS that affect type identity, and no others. If any are not
1513 applied, set *remove_attributes to true. */
1515 static tree
1516 apply_identity_attributes (tree result, tree attribs, bool *remove_attributes)
1518 tree first_ident = NULL_TREE;
1519 tree new_attribs = NULL_TREE;
1520 tree *p = &new_attribs;
1522 if (OVERLOAD_TYPE_P (result))
1524 /* On classes and enums all attributes are ingrained. */
1525 gcc_assert (attribs == TYPE_ATTRIBUTES (result));
1526 return result;
1529 for (tree a = attribs; a; a = TREE_CHAIN (a))
1531 const attribute_spec *as
1532 = lookup_attribute_spec (get_attribute_name (a));
1533 if (as && as->affects_type_identity)
1535 if (!first_ident)
1536 first_ident = a;
1537 else if (first_ident == error_mark_node)
1539 *p = tree_cons (TREE_PURPOSE (a), TREE_VALUE (a), NULL_TREE);
1540 p = &TREE_CHAIN (*p);
1543 else if (first_ident && first_ident != error_mark_node)
1545 for (tree a2 = first_ident; a2 != a; a2 = TREE_CHAIN (a2))
1547 *p = tree_cons (TREE_PURPOSE (a2), TREE_VALUE (a2), NULL_TREE);
1548 p = &TREE_CHAIN (*p);
1550 first_ident = error_mark_node;
1553 if (first_ident != error_mark_node)
1554 new_attribs = first_ident;
1556 if (first_ident == attribs)
1557 /* All attributes affected type identity. */;
1558 else
1559 *remove_attributes = true;
1561 return cp_build_type_attribute_variant (result, new_attribs);
1564 /* Builds a qualified variant of T that is either not a typedef variant
1565 (the default behavior) or not a typedef variant of a user-facing type
1566 (if FLAGS contains STF_USER_FACING). If T is not a type, then this
1567 just dispatches to strip_typedefs_expr.
1569 E.g. consider the following declarations:
1570 typedef const int ConstInt;
1571 typedef ConstInt* PtrConstInt;
1572 If T is PtrConstInt, this function returns a type representing
1573 const int*.
1574 In other words, if T is a typedef, the function returns the underlying type.
1575 The cv-qualification and attributes of the type returned match the
1576 input type.
1577 They will always be compatible types.
1578 The returned type is built so that all of its subtypes
1579 recursively have their typedefs stripped as well.
1581 This is different from just returning TYPE_CANONICAL (T)
1582 Because of several reasons:
1583 * If T is a type that needs structural equality
1584 its TYPE_CANONICAL (T) will be NULL.
1585 * TYPE_CANONICAL (T) desn't carry type attributes
1586 and loses template parameter names.
1588 If REMOVE_ATTRIBUTES is non-null, also strip attributes that don't
1589 affect type identity, and set the referent to true if any were
1590 stripped. */
1592 tree
1593 strip_typedefs (tree t, bool *remove_attributes /* = NULL */,
1594 unsigned int flags /* = 0 */)
1596 tree result = NULL, type = NULL, t0 = NULL;
1598 if (!t || t == error_mark_node)
1599 return t;
1601 if (!TYPE_P (t))
1602 return strip_typedefs_expr (t, remove_attributes, flags);
1604 if (t == TYPE_CANONICAL (t))
1605 return t;
1607 if (!(flags & STF_STRIP_DEPENDENT)
1608 && dependent_alias_template_spec_p (t, nt_opaque))
1609 /* DR 1558: However, if the template-id is dependent, subsequent
1610 template argument substitution still applies to the template-id. */
1611 return t;
1613 switch (TREE_CODE (t))
1615 case POINTER_TYPE:
1616 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1617 result = build_pointer_type_for_mode (type, TYPE_MODE (t), false);
1618 break;
1619 case REFERENCE_TYPE:
1620 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1621 result = cp_build_reference_type_for_mode (type, TYPE_MODE (t), TYPE_REF_IS_RVALUE (t));
1622 break;
1623 case OFFSET_TYPE:
1624 t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t), remove_attributes, flags);
1625 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1626 result = build_offset_type (t0, type);
1627 break;
1628 case RECORD_TYPE:
1629 if (TYPE_PTRMEMFUNC_P (t))
1631 t0 = strip_typedefs (TYPE_PTRMEMFUNC_FN_TYPE (t),
1632 remove_attributes, flags);
1633 result = build_ptrmemfunc_type (t0);
1635 break;
1636 case ARRAY_TYPE:
1637 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1638 t0 = strip_typedefs (TYPE_DOMAIN (t), remove_attributes, flags);
1639 gcc_checking_assert (TYPE_DEPENDENT_P_VALID (t)
1640 || !dependent_type_p (t));
1641 result = build_cplus_array_type (type, t0, TYPE_DEPENDENT_P (t));
1642 break;
1643 case FUNCTION_TYPE:
1644 case METHOD_TYPE:
1646 tree arg_types = NULL, arg_node, arg_node2, arg_type;
1647 bool changed;
1649 /* Because we stomp on TREE_PURPOSE of TYPE_ARG_TYPES in many places
1650 around the compiler (e.g. cp_parser_late_parsing_default_args), we
1651 can't expect that re-hashing a function type will find a previous
1652 equivalent type, so try to reuse the input type if nothing has
1653 changed. If the type is itself a variant, that will change. */
1654 bool is_variant = typedef_variant_p (t);
1655 if (remove_attributes
1656 && (TYPE_ATTRIBUTES (t) || TYPE_USER_ALIGN (t)))
1657 is_variant = true;
1659 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1660 tree canon_spec = (flag_noexcept_type
1661 ? canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (t))
1662 : NULL_TREE);
1663 changed = (type != TREE_TYPE (t) || is_variant
1664 || TYPE_RAISES_EXCEPTIONS (t) != canon_spec);
1666 for (arg_node = TYPE_ARG_TYPES (t);
1667 arg_node;
1668 arg_node = TREE_CHAIN (arg_node))
1670 if (arg_node == void_list_node)
1671 break;
1672 arg_type = strip_typedefs (TREE_VALUE (arg_node),
1673 remove_attributes, flags);
1674 gcc_assert (arg_type);
1675 if (arg_type == TREE_VALUE (arg_node) && !changed)
1676 continue;
1678 if (!changed)
1680 changed = true;
1681 for (arg_node2 = TYPE_ARG_TYPES (t);
1682 arg_node2 != arg_node;
1683 arg_node2 = TREE_CHAIN (arg_node2))
1684 arg_types
1685 = tree_cons (TREE_PURPOSE (arg_node2),
1686 TREE_VALUE (arg_node2), arg_types);
1689 arg_types
1690 = tree_cons (TREE_PURPOSE (arg_node), arg_type, arg_types);
1693 if (!changed)
1694 return t;
1696 if (arg_types)
1697 arg_types = nreverse (arg_types);
1699 /* A list of parameters not ending with an ellipsis
1700 must end with void_list_node. */
1701 if (arg_node)
1702 arg_types = chainon (arg_types, void_list_node);
1704 if (TREE_CODE (t) == METHOD_TYPE)
1706 tree class_type = TREE_TYPE (TREE_VALUE (arg_types));
1707 gcc_assert (class_type);
1708 result =
1709 build_method_type_directly (class_type, type,
1710 TREE_CHAIN (arg_types));
1712 else
1714 result = build_function_type (type, arg_types);
1715 result = apply_memfn_quals (result, type_memfn_quals (t));
1718 result = build_cp_fntype_variant (result,
1719 type_memfn_rqual (t), canon_spec,
1720 TYPE_HAS_LATE_RETURN_TYPE (t));
1722 break;
1723 case TYPENAME_TYPE:
1725 bool changed = false;
1726 tree fullname = TYPENAME_TYPE_FULLNAME (t);
1727 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
1728 && TREE_OPERAND (fullname, 1))
1730 tree args = TREE_OPERAND (fullname, 1);
1731 tree new_args = copy_node (args);
1732 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
1734 tree arg = TREE_VEC_ELT (args, i);
1735 tree strip_arg = strip_typedefs (arg, remove_attributes, flags);
1736 TREE_VEC_ELT (new_args, i) = strip_arg;
1737 if (strip_arg != arg)
1738 changed = true;
1740 if (changed)
1742 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_args)
1743 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
1744 fullname
1745 = lookup_template_function (TREE_OPERAND (fullname, 0),
1746 new_args);
1748 else
1749 ggc_free (new_args);
1751 tree ctx = strip_typedefs (TYPE_CONTEXT (t), remove_attributes, flags);
1752 if (!changed && ctx == TYPE_CONTEXT (t) && !typedef_variant_p (t))
1753 return t;
1754 tree name = fullname;
1755 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
1756 name = TREE_OPERAND (fullname, 0);
1757 /* Use build_typename_type rather than make_typename_type because we
1758 don't want to resolve it here, just strip typedefs. */
1759 result = build_typename_type (ctx, name, fullname, typename_type);
1761 break;
1762 case DECLTYPE_TYPE:
1763 result = strip_typedefs_expr (DECLTYPE_TYPE_EXPR (t),
1764 remove_attributes, flags);
1765 if (result == DECLTYPE_TYPE_EXPR (t))
1766 result = NULL_TREE;
1767 else
1768 result = (finish_decltype_type
1769 (result,
1770 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t),
1771 tf_none));
1772 break;
1773 case TRAIT_TYPE:
1775 tree type1 = strip_typedefs (TRAIT_TYPE_TYPE1 (t),
1776 remove_attributes, flags);
1777 tree type2 = strip_typedefs (TRAIT_TYPE_TYPE2 (t),
1778 remove_attributes, flags);
1779 if (type1 == TRAIT_TYPE_TYPE1 (t) && type2 == TRAIT_TYPE_TYPE2 (t))
1780 result = NULL_TREE;
1781 else
1782 result = finish_trait_type (TRAIT_TYPE_KIND (t), type1, type2,
1783 tf_warning_or_error);
1785 break;
1786 case TYPE_PACK_EXPANSION:
1788 tree pat = PACK_EXPANSION_PATTERN (t);
1789 if (TYPE_P (pat))
1791 type = strip_typedefs (pat, remove_attributes, flags);
1792 if (type != pat)
1794 result = build_distinct_type_copy (t);
1795 PACK_EXPANSION_PATTERN (result) = type;
1799 break;
1800 default:
1801 break;
1804 if (!result)
1806 if (typedef_variant_p (t))
1808 if ((flags & STF_USER_VISIBLE)
1809 && !user_facing_original_type_p (t))
1810 return t;
1811 /* If T is a non-template alias or typedef, we can assume that
1812 instantiating its definition will hit any substitution failure,
1813 so we don't need to retain it here as well. */
1814 if (!alias_template_specialization_p (t, nt_opaque))
1815 flags |= STF_STRIP_DEPENDENT;
1816 result = strip_typedefs (DECL_ORIGINAL_TYPE (TYPE_NAME (t)),
1817 remove_attributes, flags);
1819 else
1820 result = TYPE_MAIN_VARIANT (t);
1822 /*gcc_assert (!typedef_variant_p (result)
1823 || dependent_alias_template_spec_p (result, nt_opaque)
1824 || ((flags & STF_USER_VISIBLE)
1825 && !user_facing_original_type_p (result)));*/
1827 if (COMPLETE_TYPE_P (result) && !COMPLETE_TYPE_P (t))
1828 /* If RESULT is complete and T isn't, it's likely the case that T
1829 is a variant of RESULT which hasn't been updated yet. Skip the
1830 attribute handling. */;
1831 else
1833 if (TYPE_USER_ALIGN (t) != TYPE_USER_ALIGN (result)
1834 || TYPE_ALIGN (t) != TYPE_ALIGN (result))
1836 gcc_assert (TYPE_USER_ALIGN (t));
1837 if (remove_attributes)
1838 *remove_attributes = true;
1839 else
1841 if (TYPE_ALIGN (t) == TYPE_ALIGN (result))
1842 result = build_variant_type_copy (result);
1843 else
1844 result = build_aligned_type (result, TYPE_ALIGN (t));
1845 TYPE_USER_ALIGN (result) = true;
1849 if (TYPE_ATTRIBUTES (t))
1851 if (remove_attributes)
1852 result = apply_identity_attributes (result, TYPE_ATTRIBUTES (t),
1853 remove_attributes);
1854 else
1855 result = cp_build_type_attribute_variant (result,
1856 TYPE_ATTRIBUTES (t));
1860 return cp_build_qualified_type (result, cp_type_quals (t));
1863 /* Like strip_typedefs above, but works on expressions (and other
1864 non-types such as TREE_VEC), so that in
1866 template<class T> struct A
1868 typedef T TT;
1869 B<sizeof(TT)> b;
1872 sizeof(TT) is replaced by sizeof(T). */
1874 tree
1875 strip_typedefs_expr (tree t, bool *remove_attributes, unsigned int flags)
1877 unsigned i,n;
1878 tree r, type, *ops;
1879 enum tree_code code;
1881 if (t == NULL_TREE || t == error_mark_node)
1882 return t;
1884 STRIP_ANY_LOCATION_WRAPPER (t);
1886 if (DECL_P (t) || CONSTANT_CLASS_P (t))
1887 return t;
1889 code = TREE_CODE (t);
1890 switch (code)
1892 case IDENTIFIER_NODE:
1893 case TEMPLATE_PARM_INDEX:
1894 case OVERLOAD:
1895 case BASELINK:
1896 case ARGUMENT_PACK_SELECT:
1897 return t;
1899 case TRAIT_EXPR:
1901 tree type1 = strip_typedefs (TRAIT_EXPR_TYPE1 (t),
1902 remove_attributes, flags);
1903 tree type2 = strip_typedefs (TRAIT_EXPR_TYPE2 (t),
1904 remove_attributes, flags);
1905 if (type1 == TRAIT_EXPR_TYPE1 (t)
1906 && type2 == TRAIT_EXPR_TYPE2 (t))
1907 return t;
1908 r = copy_node (t);
1909 TRAIT_EXPR_TYPE1 (r) = type1;
1910 TRAIT_EXPR_TYPE2 (r) = type2;
1911 return r;
1914 case TREE_LIST:
1916 bool changed = false;
1917 auto_vec<tree_pair, 4> vec;
1918 r = t;
1919 for (; t; t = TREE_CHAIN (t))
1921 tree purpose = strip_typedefs (TREE_PURPOSE (t),
1922 remove_attributes, flags);
1923 tree value = strip_typedefs (TREE_VALUE (t),
1924 remove_attributes, flags);
1925 if (purpose != TREE_PURPOSE (t) || value != TREE_VALUE (t))
1926 changed = true;
1927 vec.safe_push ({purpose, value});
1929 if (changed)
1931 r = NULL_TREE;
1932 for (int i = vec.length () - 1; i >= 0; i--)
1933 r = tree_cons (vec[i].first, vec[i].second, r);
1935 return r;
1938 case TREE_VEC:
1940 bool changed = false;
1941 releasing_vec vec;
1942 n = TREE_VEC_LENGTH (t);
1943 vec_safe_reserve (vec, n);
1944 for (i = 0; i < n; ++i)
1946 tree op = strip_typedefs (TREE_VEC_ELT (t, i),
1947 remove_attributes, flags);
1948 vec->quick_push (op);
1949 if (op != TREE_VEC_ELT (t, i))
1950 changed = true;
1952 if (changed)
1954 r = copy_node (t);
1955 for (i = 0; i < n; ++i)
1956 TREE_VEC_ELT (r, i) = (*vec)[i];
1957 NON_DEFAULT_TEMPLATE_ARGS_COUNT (r)
1958 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
1960 else
1961 r = t;
1962 return r;
1965 case CONSTRUCTOR:
1967 bool changed = false;
1968 vec<constructor_elt, va_gc> *vec
1969 = vec_safe_copy (CONSTRUCTOR_ELTS (t));
1970 n = CONSTRUCTOR_NELTS (t);
1971 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1972 for (i = 0; i < n; ++i)
1974 constructor_elt *e = &(*vec)[i];
1975 tree op = strip_typedefs (e->value, remove_attributes, flags);
1976 if (op != e->value)
1978 changed = true;
1979 e->value = op;
1981 gcc_checking_assert
1982 (e->index == strip_typedefs (e->index, remove_attributes,
1983 flags));
1986 if (!changed && type == TREE_TYPE (t))
1988 vec_free (vec);
1989 return t;
1991 else
1993 r = copy_node (t);
1994 TREE_TYPE (r) = type;
1995 CONSTRUCTOR_ELTS (r) = vec;
1996 return r;
2000 case LAMBDA_EXPR:
2001 return t;
2003 case STATEMENT_LIST:
2004 error ("statement-expression in a constant expression");
2005 return error_mark_node;
2007 default:
2008 break;
2011 gcc_assert (EXPR_P (t));
2013 n = cp_tree_operand_length (t);
2014 ops = XALLOCAVEC (tree, n);
2015 type = TREE_TYPE (t);
2017 switch (code)
2019 CASE_CONVERT:
2020 case IMPLICIT_CONV_EXPR:
2021 case DYNAMIC_CAST_EXPR:
2022 case STATIC_CAST_EXPR:
2023 case CONST_CAST_EXPR:
2024 case REINTERPRET_CAST_EXPR:
2025 case CAST_EXPR:
2026 case NEW_EXPR:
2027 type = strip_typedefs (type, remove_attributes, flags);
2028 /* fallthrough */
2030 default:
2031 for (i = 0; i < n; ++i)
2032 ops[i] = strip_typedefs (TREE_OPERAND (t, i),
2033 remove_attributes, flags);
2034 break;
2037 /* If nothing changed, return t. */
2038 for (i = 0; i < n; ++i)
2039 if (ops[i] != TREE_OPERAND (t, i))
2040 break;
2041 if (i == n && type == TREE_TYPE (t))
2042 return t;
2044 r = copy_node (t);
2045 TREE_TYPE (r) = type;
2046 for (i = 0; i < n; ++i)
2047 TREE_OPERAND (r, i) = ops[i];
2048 return r;
2051 /* Makes a copy of BINFO and TYPE, which is to be inherited into a
2052 graph dominated by T. If BINFO is NULL, TYPE is a dependent base,
2053 and we do a shallow copy. If BINFO is non-NULL, we do a deep copy.
2054 VIRT indicates whether TYPE is inherited virtually or not.
2055 IGO_PREV points at the previous binfo of the inheritance graph
2056 order chain. The newly copied binfo's TREE_CHAIN forms this
2057 ordering.
2059 The CLASSTYPE_VBASECLASSES vector of T is constructed in the
2060 correct order. That is in the order the bases themselves should be
2061 constructed in.
2063 The BINFO_INHERITANCE of a virtual base class points to the binfo
2064 of the most derived type. ??? We could probably change this so that
2065 BINFO_INHERITANCE becomes synonymous with BINFO_PRIMARY, and hence
2066 remove a field. They currently can only differ for primary virtual
2067 virtual bases. */
2069 tree
2070 copy_binfo (tree binfo, tree type, tree t, tree *igo_prev, int virt)
2072 tree new_binfo;
2074 if (virt)
2076 /* See if we've already made this virtual base. */
2077 new_binfo = binfo_for_vbase (type, t);
2078 if (new_binfo)
2079 return new_binfo;
2082 new_binfo = make_tree_binfo (binfo ? BINFO_N_BASE_BINFOS (binfo) : 0);
2083 BINFO_TYPE (new_binfo) = type;
2085 /* Chain it into the inheritance graph. */
2086 TREE_CHAIN (*igo_prev) = new_binfo;
2087 *igo_prev = new_binfo;
2089 if (binfo && !BINFO_DEPENDENT_BASE_P (binfo))
2091 int ix;
2092 tree base_binfo;
2094 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), type));
2096 BINFO_OFFSET (new_binfo) = BINFO_OFFSET (binfo);
2097 BINFO_VIRTUALS (new_binfo) = BINFO_VIRTUALS (binfo);
2099 /* We do not need to copy the accesses, as they are read only. */
2100 BINFO_BASE_ACCESSES (new_binfo) = BINFO_BASE_ACCESSES (binfo);
2102 /* Recursively copy base binfos of BINFO. */
2103 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2105 tree new_base_binfo;
2106 new_base_binfo = copy_binfo (base_binfo, BINFO_TYPE (base_binfo),
2107 t, igo_prev,
2108 BINFO_VIRTUAL_P (base_binfo));
2110 if (!BINFO_INHERITANCE_CHAIN (new_base_binfo))
2111 BINFO_INHERITANCE_CHAIN (new_base_binfo) = new_binfo;
2112 BINFO_BASE_APPEND (new_binfo, new_base_binfo);
2115 else
2116 BINFO_DEPENDENT_BASE_P (new_binfo) = 1;
2118 if (virt)
2120 /* Push it onto the list after any virtual bases it contains
2121 will have been pushed. */
2122 CLASSTYPE_VBASECLASSES (t)->quick_push (new_binfo);
2123 BINFO_VIRTUAL_P (new_binfo) = 1;
2124 BINFO_INHERITANCE_CHAIN (new_binfo) = TYPE_BINFO (t);
2127 return new_binfo;
2130 /* Hashing of lists so that we don't make duplicates.
2131 The entry point is `list_hash_canon'. */
2133 struct list_proxy
2135 tree purpose;
2136 tree value;
2137 tree chain;
2140 struct list_hasher : ggc_ptr_hash<tree_node>
2142 typedef list_proxy *compare_type;
2144 static hashval_t hash (tree);
2145 static bool equal (tree, list_proxy *);
2148 /* Now here is the hash table. When recording a list, it is added
2149 to the slot whose index is the hash code mod the table size.
2150 Note that the hash table is used for several kinds of lists.
2151 While all these live in the same table, they are completely independent,
2152 and the hash code is computed differently for each of these. */
2154 static GTY (()) hash_table<list_hasher> *list_hash_table;
2156 /* Compare ENTRY (an entry in the hash table) with DATA (a list_proxy
2157 for a node we are thinking about adding). */
2159 bool
2160 list_hasher::equal (tree t, list_proxy *proxy)
2162 return (TREE_VALUE (t) == proxy->value
2163 && TREE_PURPOSE (t) == proxy->purpose
2164 && TREE_CHAIN (t) == proxy->chain);
2167 /* Compute a hash code for a list (chain of TREE_LIST nodes
2168 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
2169 TREE_COMMON slots), by adding the hash codes of the individual entries. */
2171 static hashval_t
2172 list_hash_pieces (tree purpose, tree value, tree chain)
2174 hashval_t hashcode = 0;
2176 if (chain)
2177 hashcode += TREE_HASH (chain);
2179 if (value)
2180 hashcode += TREE_HASH (value);
2181 else
2182 hashcode += 1007;
2183 if (purpose)
2184 hashcode += TREE_HASH (purpose);
2185 else
2186 hashcode += 1009;
2187 return hashcode;
2190 /* Hash an already existing TREE_LIST. */
2192 hashval_t
2193 list_hasher::hash (tree t)
2195 return list_hash_pieces (TREE_PURPOSE (t),
2196 TREE_VALUE (t),
2197 TREE_CHAIN (t));
2200 /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
2201 object for an identical list if one already exists. Otherwise, build a
2202 new one, and record it as the canonical object. */
2204 tree
2205 hash_tree_cons (tree purpose, tree value, tree chain)
2207 int hashcode = 0;
2208 tree *slot;
2209 struct list_proxy proxy;
2211 /* Hash the list node. */
2212 hashcode = list_hash_pieces (purpose, value, chain);
2213 /* Create a proxy for the TREE_LIST we would like to create. We
2214 don't actually create it so as to avoid creating garbage. */
2215 proxy.purpose = purpose;
2216 proxy.value = value;
2217 proxy.chain = chain;
2218 /* See if it is already in the table. */
2219 slot = list_hash_table->find_slot_with_hash (&proxy, hashcode, INSERT);
2220 /* If not, create a new node. */
2221 if (!*slot)
2222 *slot = tree_cons (purpose, value, chain);
2223 return (tree) *slot;
2226 /* Constructor for hashed lists. */
2228 tree
2229 hash_tree_chain (tree value, tree chain)
2231 return hash_tree_cons (NULL_TREE, value, chain);
2234 void
2235 debug_binfo (tree elem)
2237 HOST_WIDE_INT n;
2238 tree virtuals;
2240 fprintf (stderr, "type \"%s\", offset = " HOST_WIDE_INT_PRINT_DEC
2241 "\nvtable type:\n",
2242 TYPE_NAME_STRING (BINFO_TYPE (elem)),
2243 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
2244 debug_tree (BINFO_TYPE (elem));
2245 if (BINFO_VTABLE (elem))
2246 fprintf (stderr, "vtable decl \"%s\"\n",
2247 IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem))));
2248 else
2249 fprintf (stderr, "no vtable decl yet\n");
2250 fprintf (stderr, "virtuals:\n");
2251 virtuals = BINFO_VIRTUALS (elem);
2252 n = 0;
2254 while (virtuals)
2256 tree fndecl = TREE_VALUE (virtuals);
2257 fprintf (stderr, "%s [" HOST_WIDE_INT_PRINT_DEC " =? "
2258 HOST_WIDE_INT_PRINT_DEC "]\n",
2259 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
2260 n, TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
2261 ++n;
2262 virtuals = TREE_CHAIN (virtuals);
2266 /* Build a representation for the qualified name SCOPE::NAME. TYPE is
2267 the type of the result expression, if known, or NULL_TREE if the
2268 resulting expression is type-dependent. If TEMPLATE_P is true,
2269 NAME is known to be a template because the user explicitly used the
2270 "template" keyword after the "::".
2272 All SCOPE_REFs should be built by use of this function. */
2274 tree
2275 build_qualified_name (tree type, tree scope, tree name, bool template_p)
2277 tree t;
2278 if (type == error_mark_node
2279 || scope == error_mark_node
2280 || name == error_mark_node)
2281 return error_mark_node;
2282 gcc_assert (TREE_CODE (name) != SCOPE_REF);
2283 t = build2 (SCOPE_REF, type, scope, name);
2284 QUALIFIED_NAME_IS_TEMPLATE (t) = template_p;
2285 PTRMEM_OK_P (t) = true;
2286 if (type)
2287 t = convert_from_reference (t);
2288 return t;
2291 /* Like check_qualified_type, but also check ref-qualifier, exception
2292 specification, and whether the return type was specified after the
2293 parameters. */
2295 static bool
2296 cp_check_qualified_type (const_tree cand, const_tree base, int type_quals,
2297 cp_ref_qualifier rqual, tree raises, bool late)
2299 return (TYPE_QUALS (cand) == type_quals
2300 && check_base_type (cand, base)
2301 && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (cand),
2302 ce_exact)
2303 && TYPE_HAS_LATE_RETURN_TYPE (cand) == late
2304 && type_memfn_rqual (cand) == rqual);
2307 /* Build the FUNCTION_TYPE or METHOD_TYPE with the ref-qualifier RQUAL. */
2309 tree
2310 build_ref_qualified_type (tree type, cp_ref_qualifier rqual)
2312 tree raises = TYPE_RAISES_EXCEPTIONS (type);
2313 bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
2314 return build_cp_fntype_variant (type, rqual, raises, late);
2317 tree
2318 make_binding_vec (tree name, unsigned clusters MEM_STAT_DECL)
2320 /* Stored in an unsigned short, but we're limited to the number of
2321 modules anyway. */
2322 gcc_checking_assert (clusters <= (unsigned short)(~0));
2323 size_t length = (offsetof (tree_binding_vec, vec)
2324 + clusters * sizeof (binding_cluster));
2325 tree vec = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2326 TREE_SET_CODE (vec, BINDING_VECTOR);
2327 BINDING_VECTOR_NAME (vec) = name;
2328 BINDING_VECTOR_ALLOC_CLUSTERS (vec) = clusters;
2329 BINDING_VECTOR_NUM_CLUSTERS (vec) = 0;
2331 return vec;
2334 /* Make a raw overload node containing FN. */
2336 tree
2337 ovl_make (tree fn, tree next)
2339 tree result = make_node (OVERLOAD);
2341 if (TREE_CODE (fn) == OVERLOAD)
2342 OVL_NESTED_P (result) = true;
2344 TREE_TYPE (result) = (next || TREE_CODE (fn) == TEMPLATE_DECL
2345 ? unknown_type_node : TREE_TYPE (fn));
2346 if (next && TREE_CODE (next) == OVERLOAD && OVL_DEDUP_P (next))
2347 OVL_DEDUP_P (result) = true;
2348 OVL_FUNCTION (result) = fn;
2349 OVL_CHAIN (result) = next;
2350 return result;
2353 /* Add FN to the (potentially NULL) overload set OVL. USING_OR_HIDDEN is >
2354 zero if this is a using-decl. It is > 1 if we're exporting the
2355 using decl. USING_OR_HIDDEN is < 0, if FN is hidden. (A decl
2356 cannot be both using and hidden.) We keep the hidden decls first,
2357 but remaining ones are unordered. */
2359 tree
2360 ovl_insert (tree fn, tree maybe_ovl, int using_or_hidden)
2362 tree result = maybe_ovl;
2363 tree insert_after = NULL_TREE;
2365 /* Skip hidden. */
2366 for (; maybe_ovl && TREE_CODE (maybe_ovl) == OVERLOAD
2367 && OVL_HIDDEN_P (maybe_ovl);
2368 maybe_ovl = OVL_CHAIN (maybe_ovl))
2370 gcc_checking_assert (!OVL_LOOKUP_P (maybe_ovl));
2371 insert_after = maybe_ovl;
2374 if (maybe_ovl || using_or_hidden || TREE_CODE (fn) == TEMPLATE_DECL)
2376 maybe_ovl = ovl_make (fn, maybe_ovl);
2378 if (using_or_hidden < 0)
2379 OVL_HIDDEN_P (maybe_ovl) = true;
2380 if (using_or_hidden > 0)
2382 OVL_DEDUP_P (maybe_ovl) = OVL_USING_P (maybe_ovl) = true;
2383 if (using_or_hidden > 1)
2384 OVL_EXPORT_P (maybe_ovl) = true;
2387 else
2388 maybe_ovl = fn;
2390 if (insert_after)
2392 OVL_CHAIN (insert_after) = maybe_ovl;
2393 TREE_TYPE (insert_after) = unknown_type_node;
2395 else
2396 result = maybe_ovl;
2398 return result;
2401 /* Skip any hidden names at the beginning of OVL. */
2403 tree
2404 ovl_skip_hidden (tree ovl)
2406 while (ovl && TREE_CODE (ovl) == OVERLOAD && OVL_HIDDEN_P (ovl))
2407 ovl = OVL_CHAIN (ovl);
2409 return ovl;
2412 /* NODE is an OVL_HIDDEN_P node that is now revealed. */
2414 tree
2415 ovl_iterator::reveal_node (tree overload, tree node)
2417 /* We cannot have returned NODE as part of a lookup overload, so we
2418 don't have to worry about preserving that. */
2420 OVL_HIDDEN_P (node) = false;
2421 if (tree chain = OVL_CHAIN (node))
2422 if (TREE_CODE (chain) == OVERLOAD)
2424 if (OVL_HIDDEN_P (chain))
2426 /* The node needs moving, and the simplest way is to remove it
2427 and reinsert. */
2428 overload = remove_node (overload, node);
2429 overload = ovl_insert (OVL_FUNCTION (node), overload);
2431 else if (OVL_DEDUP_P (chain))
2432 OVL_DEDUP_P (node) = true;
2434 return overload;
2437 /* NODE is on the overloads of OVL. Remove it.
2438 The removed node is unaltered and may continue to be iterated
2439 from (i.e. it is safe to remove a node from an overload one is
2440 currently iterating over). */
2442 tree
2443 ovl_iterator::remove_node (tree overload, tree node)
2445 tree *slot = &overload;
2446 while (*slot != node)
2448 tree probe = *slot;
2449 gcc_checking_assert (!OVL_LOOKUP_P (probe));
2451 slot = &OVL_CHAIN (probe);
2454 /* Stitch out NODE. We don't have to worry about now making a
2455 singleton overload (and consequently maybe setting its type),
2456 because all uses of this function will be followed by inserting a
2457 new node that must follow the place we've cut this out from. */
2458 if (TREE_CODE (node) != OVERLOAD)
2459 /* Cloned inherited ctors don't mark themselves as via_using. */
2460 *slot = NULL_TREE;
2461 else
2462 *slot = OVL_CHAIN (node);
2464 return overload;
2467 /* Mark or unmark a lookup set. */
2469 void
2470 lookup_mark (tree ovl, bool val)
2472 for (lkp_iterator iter (ovl); iter; ++iter)
2474 gcc_checking_assert (LOOKUP_SEEN_P (*iter) != val);
2475 LOOKUP_SEEN_P (*iter) = val;
2479 /* Add a set of new FNS into a lookup. */
2481 tree
2482 lookup_add (tree fns, tree lookup)
2484 if (fns == error_mark_node || lookup == error_mark_node)
2485 return error_mark_node;
2487 if (lookup || TREE_CODE (fns) == TEMPLATE_DECL)
2489 lookup = ovl_make (fns, lookup);
2490 OVL_LOOKUP_P (lookup) = true;
2492 else
2493 lookup = fns;
2495 return lookup;
2498 /* FNS is a new overload set, add them to LOOKUP, if they are not
2499 already present there. */
2501 tree
2502 lookup_maybe_add (tree fns, tree lookup, bool deduping)
2504 if (deduping)
2505 for (tree next, probe = fns; probe; probe = next)
2507 tree fn = probe;
2508 next = NULL_TREE;
2510 if (TREE_CODE (probe) == OVERLOAD)
2512 fn = OVL_FUNCTION (probe);
2513 next = OVL_CHAIN (probe);
2516 if (!LOOKUP_SEEN_P (fn))
2517 LOOKUP_SEEN_P (fn) = true;
2518 else
2520 /* This function was already seen. Insert all the
2521 predecessors onto the lookup. */
2522 for (; fns != probe; fns = OVL_CHAIN (fns))
2524 lookup = lookup_add (OVL_FUNCTION (fns), lookup);
2525 /* Propagate OVL_USING, but OVL_HIDDEN &
2526 OVL_DEDUP_P don't matter. */
2527 if (OVL_USING_P (fns))
2528 OVL_USING_P (lookup) = true;
2531 /* And now skip this function. */
2532 fns = next;
2536 if (fns)
2537 /* We ended in a set of new functions. Add them all in one go. */
2538 lookup = lookup_add (fns, lookup);
2540 return lookup;
2543 /* Returns nonzero if X is an expression for a (possibly overloaded)
2544 function. If "f" is a function or function template, "f", "c->f",
2545 "c.f", "C::f", and "f<int>" will all be considered possibly
2546 overloaded functions. Returns 2 if the function is actually
2547 overloaded, i.e., if it is impossible to know the type of the
2548 function without performing overload resolution. */
2551 is_overloaded_fn (tree x)
2553 STRIP_ANY_LOCATION_WRAPPER (x);
2555 /* A baselink is also considered an overloaded function. */
2556 if (TREE_CODE (x) == OFFSET_REF
2557 || TREE_CODE (x) == COMPONENT_REF)
2558 x = TREE_OPERAND (x, 1);
2559 x = MAYBE_BASELINK_FUNCTIONS (x);
2560 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
2561 x = TREE_OPERAND (x, 0);
2563 if (DECL_FUNCTION_TEMPLATE_P (OVL_FIRST (x))
2564 || (TREE_CODE (x) == OVERLOAD && !OVL_SINGLE_P (x)))
2565 return 2;
2567 return OVL_P (x);
2570 /* X is the CALL_EXPR_FN of a CALL_EXPR. If X represents a dependent name
2571 (14.6.2), return the IDENTIFIER_NODE for that name. Otherwise, return
2572 NULL_TREE. */
2574 tree
2575 dependent_name (tree x)
2577 /* FIXME a dependent name must be unqualified, but this function doesn't
2578 distinguish between qualified and unqualified identifiers. */
2579 if (identifier_p (x))
2580 return x;
2581 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
2582 x = TREE_OPERAND (x, 0);
2583 if (OVL_P (x))
2584 return OVL_NAME (x);
2585 return NULL_TREE;
2588 /* Like dependent_name, but instead takes a CALL_EXPR and also checks
2589 its dependence. */
2591 tree
2592 call_expr_dependent_name (tree x)
2594 if (TREE_TYPE (x) != NULL_TREE)
2595 /* X isn't dependent, so its callee isn't a dependent name. */
2596 return NULL_TREE;
2597 return dependent_name (CALL_EXPR_FN (x));
2600 /* Returns true iff X is an expression for an overloaded function
2601 whose type cannot be known without performing overload
2602 resolution. */
2604 bool
2605 really_overloaded_fn (tree x)
2607 return is_overloaded_fn (x) == 2;
2610 /* Get the overload set FROM refers to. Returns NULL if it's not an
2611 overload set. */
2613 tree
2614 maybe_get_fns (tree from)
2616 STRIP_ANY_LOCATION_WRAPPER (from);
2618 /* A baselink is also considered an overloaded function. */
2619 if (TREE_CODE (from) == OFFSET_REF
2620 || TREE_CODE (from) == COMPONENT_REF)
2621 from = TREE_OPERAND (from, 1);
2622 if (BASELINK_P (from))
2623 from = BASELINK_FUNCTIONS (from);
2624 if (TREE_CODE (from) == TEMPLATE_ID_EXPR)
2625 from = TREE_OPERAND (from, 0);
2627 if (OVL_P (from))
2628 return from;
2630 return NULL;
2633 /* FROM refers to an overload set. Return that set (or die). */
2635 tree
2636 get_fns (tree from)
2638 tree res = maybe_get_fns (from);
2640 gcc_assert (res);
2641 return res;
2644 /* Return the first function of the overload set FROM refers to. */
2646 tree
2647 get_first_fn (tree from)
2649 return OVL_FIRST (get_fns (from));
2652 /* Return the scope where the overloaded functions OVL were found. */
2654 tree
2655 ovl_scope (tree ovl)
2657 if (TREE_CODE (ovl) == OFFSET_REF
2658 || TREE_CODE (ovl) == COMPONENT_REF)
2659 ovl = TREE_OPERAND (ovl, 1);
2660 if (TREE_CODE (ovl) == BASELINK)
2661 return BINFO_TYPE (BASELINK_BINFO (ovl));
2662 if (TREE_CODE (ovl) == TEMPLATE_ID_EXPR)
2663 ovl = TREE_OPERAND (ovl, 0);
2664 /* Skip using-declarations. */
2665 lkp_iterator iter (ovl);
2667 ovl = *iter;
2668 while (iter.using_p () && ++iter);
2670 return CP_DECL_CONTEXT (ovl);
2673 #define PRINT_RING_SIZE 4
2675 static const char *
2676 cxx_printable_name_internal (tree decl, int v, bool translate)
2678 static unsigned int uid_ring[PRINT_RING_SIZE];
2679 static char *print_ring[PRINT_RING_SIZE];
2680 static bool trans_ring[PRINT_RING_SIZE];
2681 static int ring_counter;
2682 int i;
2684 /* Only cache functions. */
2685 if (v < 2
2686 || TREE_CODE (decl) != FUNCTION_DECL
2687 || DECL_LANG_SPECIFIC (decl) == 0)
2688 return lang_decl_name (decl, v, translate);
2690 /* See if this print name is lying around. */
2691 for (i = 0; i < PRINT_RING_SIZE; i++)
2692 if (uid_ring[i] == DECL_UID (decl) && translate == trans_ring[i])
2693 /* yes, so return it. */
2694 return print_ring[i];
2696 if (++ring_counter == PRINT_RING_SIZE)
2697 ring_counter = 0;
2699 if (current_function_decl != NULL_TREE)
2701 /* There may be both translated and untranslated versions of the
2702 name cached. */
2703 for (i = 0; i < 2; i++)
2705 if (uid_ring[ring_counter] == DECL_UID (current_function_decl))
2706 ring_counter += 1;
2707 if (ring_counter == PRINT_RING_SIZE)
2708 ring_counter = 0;
2710 gcc_assert (uid_ring[ring_counter] != DECL_UID (current_function_decl));
2713 free (print_ring[ring_counter]);
2715 print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v, translate));
2716 uid_ring[ring_counter] = DECL_UID (decl);
2717 trans_ring[ring_counter] = translate;
2718 return print_ring[ring_counter];
2721 const char *
2722 cxx_printable_name (tree decl, int v)
2724 return cxx_printable_name_internal (decl, v, false);
2727 const char *
2728 cxx_printable_name_translate (tree decl, int v)
2730 return cxx_printable_name_internal (decl, v, true);
2733 /* Return the canonical version of exception-specification RAISES for a C++17
2734 function type, for use in type comparison and building TYPE_CANONICAL. */
2736 tree
2737 canonical_eh_spec (tree raises)
2739 if (raises == NULL_TREE)
2740 return raises;
2741 else if (DEFERRED_NOEXCEPT_SPEC_P (raises)
2742 || UNPARSED_NOEXCEPT_SPEC_P (raises)
2743 || uses_template_parms (raises)
2744 || uses_template_parms (TREE_PURPOSE (raises)))
2745 /* Keep a dependent or deferred exception specification. */
2746 return raises;
2747 else if (nothrow_spec_p (raises))
2748 /* throw() -> noexcept. */
2749 return noexcept_true_spec;
2750 else
2751 /* For C++17 type matching, anything else -> nothing. */
2752 return NULL_TREE;
2755 tree
2756 build_cp_fntype_variant (tree type, cp_ref_qualifier rqual,
2757 tree raises, bool late)
2759 cp_cv_quals type_quals = TYPE_QUALS (type);
2761 if (cp_check_qualified_type (type, type, type_quals, rqual, raises, late))
2762 return type;
2764 tree v = TYPE_MAIN_VARIANT (type);
2765 for (; v; v = TYPE_NEXT_VARIANT (v))
2766 if (cp_check_qualified_type (v, type, type_quals, rqual, raises, late))
2767 return v;
2769 /* Need to build a new variant. */
2770 v = build_variant_type_copy (type);
2771 if (!TYPE_DEPENDENT_P (v))
2772 /* We no longer know that it's not type-dependent. */
2773 TYPE_DEPENDENT_P_VALID (v) = false;
2774 TYPE_RAISES_EXCEPTIONS (v) = raises;
2775 TYPE_HAS_LATE_RETURN_TYPE (v) = late;
2776 switch (rqual)
2778 case REF_QUAL_RVALUE:
2779 FUNCTION_RVALUE_QUALIFIED (v) = 1;
2780 FUNCTION_REF_QUALIFIED (v) = 1;
2781 break;
2782 case REF_QUAL_LVALUE:
2783 FUNCTION_RVALUE_QUALIFIED (v) = 0;
2784 FUNCTION_REF_QUALIFIED (v) = 1;
2785 break;
2786 default:
2787 FUNCTION_REF_QUALIFIED (v) = 0;
2788 break;
2791 /* Canonicalize the exception specification. */
2792 tree cr = flag_noexcept_type ? canonical_eh_spec (raises) : NULL_TREE;
2794 if (TYPE_STRUCTURAL_EQUALITY_P (type))
2795 /* Propagate structural equality. */
2796 SET_TYPE_STRUCTURAL_EQUALITY (v);
2797 else if (TYPE_CANONICAL (type) != type || cr != raises || late)
2798 /* Build the underlying canonical type, since it is different
2799 from TYPE. */
2800 TYPE_CANONICAL (v) = build_cp_fntype_variant (TYPE_CANONICAL (type),
2801 rqual, cr, false);
2802 else
2803 /* T is its own canonical type. */
2804 TYPE_CANONICAL (v) = v;
2806 return v;
2809 /* TYPE is a function or method type with a deferred exception
2810 specification that has been parsed to RAISES. Fixup all the type
2811 variants that are affected in place. Via decltype &| noexcept
2812 tricks, the unparsed spec could have escaped into the type system.
2813 The general case is hard to fixup canonical types for. */
2815 void
2816 fixup_deferred_exception_variants (tree type, tree raises)
2818 tree original = TYPE_RAISES_EXCEPTIONS (type);
2819 tree cr = flag_noexcept_type ? canonical_eh_spec (raises) : NULL_TREE;
2821 gcc_checking_assert (UNPARSED_NOEXCEPT_SPEC_P (original));
2823 /* Though sucky, this walk will process the canonical variants
2824 first. */
2825 tree prev = NULL_TREE;
2826 for (tree variant = TYPE_MAIN_VARIANT (type);
2827 variant; prev = variant, variant = TYPE_NEXT_VARIANT (variant))
2828 if (TYPE_RAISES_EXCEPTIONS (variant) == original)
2830 gcc_checking_assert (variant != TYPE_MAIN_VARIANT (type));
2832 if (!TYPE_STRUCTURAL_EQUALITY_P (variant))
2834 cp_cv_quals var_quals = TYPE_QUALS (variant);
2835 cp_ref_qualifier rqual = type_memfn_rqual (variant);
2837 /* If VARIANT would become a dup (cp_check_qualified_type-wise)
2838 of an existing variant in the variant list of TYPE after its
2839 exception specification has been parsed, elide it. Otherwise,
2840 build_cp_fntype_variant could use it, leading to "canonical
2841 types differ for identical types." */
2842 tree v = TYPE_MAIN_VARIANT (type);
2843 for (; v; v = TYPE_NEXT_VARIANT (v))
2844 if (cp_check_qualified_type (v, variant, var_quals,
2845 rqual, cr, false))
2847 /* The main variant will not match V, so PREV will never
2848 be null. */
2849 TYPE_NEXT_VARIANT (prev) = TYPE_NEXT_VARIANT (variant);
2850 break;
2852 TYPE_RAISES_EXCEPTIONS (variant) = raises;
2854 if (!v)
2855 v = build_cp_fntype_variant (TYPE_CANONICAL (variant),
2856 rqual, cr, false);
2857 TYPE_CANONICAL (variant) = TYPE_CANONICAL (v);
2859 else
2860 TYPE_RAISES_EXCEPTIONS (variant) = raises;
2862 if (!TYPE_DEPENDENT_P (variant))
2863 /* We no longer know that it's not type-dependent. */
2864 TYPE_DEPENDENT_P_VALID (variant) = false;
2868 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
2869 listed in RAISES. */
2871 tree
2872 build_exception_variant (tree type, tree raises)
2874 cp_ref_qualifier rqual = type_memfn_rqual (type);
2875 bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
2876 return build_cp_fntype_variant (type, rqual, raises, late);
2879 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new
2880 BOUND_TEMPLATE_TEMPLATE_PARM bound with NEWARGS as its template
2881 arguments. */
2883 tree
2884 bind_template_template_parm (tree t, tree newargs)
2886 tree decl = TYPE_NAME (t);
2887 tree t2;
2889 t2 = cxx_make_type (BOUND_TEMPLATE_TEMPLATE_PARM);
2890 decl = build_decl (input_location,
2891 TYPE_DECL, DECL_NAME (decl), NULL_TREE);
2892 SET_DECL_TEMPLATE_PARM_P (decl);
2894 /* These nodes have to be created to reflect new TYPE_DECL and template
2895 arguments. */
2896 TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t));
2897 TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl;
2898 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
2899 = build_template_info (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t), newargs);
2901 TREE_TYPE (decl) = t2;
2902 TYPE_NAME (t2) = decl;
2903 TYPE_STUB_DECL (t2) = decl;
2904 TYPE_SIZE (t2) = 0;
2906 if (any_template_arguments_need_structural_equality_p (newargs))
2907 SET_TYPE_STRUCTURAL_EQUALITY (t2);
2908 else
2909 TYPE_CANONICAL (t2) = canonical_type_parameter (t2);
2911 return t2;
2914 /* Called from count_trees via walk_tree. */
2916 static tree
2917 count_trees_r (tree *tp, int *walk_subtrees, void *data)
2919 ++*((int *) data);
2921 if (TYPE_P (*tp))
2922 *walk_subtrees = 0;
2924 return NULL_TREE;
2927 /* Debugging function for measuring the rough complexity of a tree
2928 representation. */
2931 count_trees (tree t)
2933 int n_trees = 0;
2934 cp_walk_tree_without_duplicates (&t, count_trees_r, &n_trees);
2935 return n_trees;
2938 /* Called from verify_stmt_tree via walk_tree. */
2940 static tree
2941 verify_stmt_tree_r (tree* tp, int * /*walk_subtrees*/, void* data)
2943 tree t = *tp;
2944 hash_table<nofree_ptr_hash <tree_node> > *statements
2945 = static_cast <hash_table<nofree_ptr_hash <tree_node> > *> (data);
2946 tree_node **slot;
2948 if (!STATEMENT_CODE_P (TREE_CODE (t)))
2949 return NULL_TREE;
2951 /* If this statement is already present in the hash table, then
2952 there is a circularity in the statement tree. */
2953 gcc_assert (!statements->find (t));
2955 slot = statements->find_slot (t, INSERT);
2956 *slot = t;
2958 return NULL_TREE;
2961 /* Debugging function to check that the statement T has not been
2962 corrupted. For now, this function simply checks that T contains no
2963 circularities. */
2965 void
2966 verify_stmt_tree (tree t)
2968 hash_table<nofree_ptr_hash <tree_node> > statements (37);
2969 cp_walk_tree (&t, verify_stmt_tree_r, &statements, NULL);
2972 /* Check if the type T depends on a type with no linkage and if so,
2973 return it. If RELAXED_P then do not consider a class type declared
2974 within a vague-linkage function or in a module CMI to have no linkage,
2975 since it can still be accessed within a different TU. Remember:
2976 no-linkage is not the same as internal-linkage. */
2978 tree
2979 no_linkage_check (tree t, bool relaxed_p)
2981 tree r;
2983 /* Lambda types that don't have mangling scope have no linkage. We
2984 check CLASSTYPE_LAMBDA_EXPR for error_mark_node because
2985 when we get here from pushtag none of the lambda information is
2986 set up yet, so we want to assume that the lambda has linkage and
2987 fix it up later if not. We need to check this even in templates so
2988 that we properly handle a lambda-expression in the signature. */
2989 if (LAMBDA_TYPE_P (t)
2990 && CLASSTYPE_LAMBDA_EXPR (t) != error_mark_node)
2992 tree extra = LAMBDA_TYPE_EXTRA_SCOPE (t);
2993 if (!extra)
2994 return t;
2997 /* Otherwise there's no point in checking linkage on template functions; we
2998 can't know their complete types. */
2999 if (processing_template_decl)
3000 return NULL_TREE;
3002 switch (TREE_CODE (t))
3004 case RECORD_TYPE:
3005 if (TYPE_PTRMEMFUNC_P (t))
3006 goto ptrmem;
3007 /* Fall through. */
3008 case UNION_TYPE:
3009 if (!CLASS_TYPE_P (t))
3010 return NULL_TREE;
3011 /* Fall through. */
3012 case ENUMERAL_TYPE:
3013 /* Only treat unnamed types as having no linkage if they're at
3014 namespace scope. This is core issue 966. */
3015 if (TYPE_UNNAMED_P (t) && TYPE_NAMESPACE_SCOPE_P (t))
3017 if (relaxed_p
3018 && TREE_PUBLIC (CP_TYPE_CONTEXT (t))
3019 && module_maybe_has_cmi_p ())
3020 /* This type could possibly be accessed outside this TU. */
3021 return NULL_TREE;
3022 else
3023 return t;
3026 for (r = CP_TYPE_CONTEXT (t); ; )
3028 /* If we're a nested type of a !TREE_PUBLIC class, we might not
3029 have linkage, or we might just be in an anonymous namespace.
3030 If we're in a TREE_PUBLIC class, we have linkage. */
3031 if (TYPE_P (r) && !TREE_PUBLIC (TYPE_NAME (r)))
3032 return no_linkage_check (TYPE_CONTEXT (t), relaxed_p);
3033 else if (TREE_CODE (r) == FUNCTION_DECL)
3035 if (relaxed_p
3036 && (vague_linkage_p (r)
3037 || (TREE_PUBLIC (r) && module_maybe_has_cmi_p ())))
3038 r = CP_DECL_CONTEXT (r);
3039 else
3040 return t;
3042 else
3043 break;
3046 return NULL_TREE;
3048 case ARRAY_TYPE:
3049 case POINTER_TYPE:
3050 case REFERENCE_TYPE:
3051 case VECTOR_TYPE:
3052 return no_linkage_check (TREE_TYPE (t), relaxed_p);
3054 case OFFSET_TYPE:
3055 ptrmem:
3056 r = no_linkage_check (TYPE_PTRMEM_POINTED_TO_TYPE (t),
3057 relaxed_p);
3058 if (r)
3059 return r;
3060 return no_linkage_check (TYPE_PTRMEM_CLASS_TYPE (t), relaxed_p);
3062 case METHOD_TYPE:
3063 case FUNCTION_TYPE:
3065 tree parm = TYPE_ARG_TYPES (t);
3066 if (TREE_CODE (t) == METHOD_TYPE)
3067 /* The 'this' pointer isn't interesting; a method has the same
3068 linkage (or lack thereof) as its enclosing class. */
3069 parm = TREE_CHAIN (parm);
3070 for (;
3071 parm && parm != void_list_node;
3072 parm = TREE_CHAIN (parm))
3074 r = no_linkage_check (TREE_VALUE (parm), relaxed_p);
3075 if (r)
3076 return r;
3078 return no_linkage_check (TREE_TYPE (t), relaxed_p);
3081 default:
3082 return NULL_TREE;
3086 extern int depth_reached;
3088 void
3089 cxx_print_statistics (void)
3091 print_template_statistics ();
3092 if (GATHER_STATISTICS)
3093 fprintf (stderr, "maximum template instantiation depth reached: %d\n",
3094 depth_reached);
3097 /* Return, as an INTEGER_CST node, the number of elements for TYPE
3098 (which is an ARRAY_TYPE). This counts only elements of the top
3099 array. */
3101 tree
3102 array_type_nelts_top (tree type)
3104 return fold_build2_loc (input_location,
3105 PLUS_EXPR, sizetype,
3106 array_type_nelts (type),
3107 size_one_node);
3110 /* Return, as an INTEGER_CST node, the number of elements for TYPE
3111 (which is an ARRAY_TYPE). This one is a recursive count of all
3112 ARRAY_TYPEs that are clumped together. */
3114 tree
3115 array_type_nelts_total (tree type)
3117 tree sz = array_type_nelts_top (type);
3118 type = TREE_TYPE (type);
3119 while (TREE_CODE (type) == ARRAY_TYPE)
3121 tree n = array_type_nelts_top (type);
3122 sz = fold_build2_loc (input_location,
3123 MULT_EXPR, sizetype, sz, n);
3124 type = TREE_TYPE (type);
3126 return sz;
3129 struct bot_data
3131 splay_tree target_remap;
3132 bool clear_location;
3135 /* Called from break_out_target_exprs via mapcar. */
3137 static tree
3138 bot_manip (tree* tp, int* walk_subtrees, void* data_)
3140 bot_data &data = *(bot_data*)data_;
3141 splay_tree target_remap = data.target_remap;
3142 tree t = *tp;
3144 if (!TYPE_P (t) && TREE_CONSTANT (t) && !TREE_SIDE_EFFECTS (t))
3146 /* There can't be any TARGET_EXPRs or their slot variables below this
3147 point. But we must make a copy, in case subsequent processing
3148 alters any part of it. For example, during gimplification a cast
3149 of the form (T) &X::f (where "f" is a member function) will lead
3150 to replacing the PTRMEM_CST for &X::f with a VAR_DECL. */
3151 *walk_subtrees = 0;
3152 *tp = unshare_expr (t);
3153 return NULL_TREE;
3155 if (TREE_CODE (t) == TARGET_EXPR)
3157 tree u;
3159 if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
3161 u = build_cplus_new (TREE_TYPE (t), TREE_OPERAND (t, 1),
3162 tf_warning_or_error);
3163 if (u == error_mark_node)
3164 return u;
3165 if (AGGR_INIT_ZERO_FIRST (TREE_OPERAND (t, 1)))
3166 AGGR_INIT_ZERO_FIRST (TREE_OPERAND (u, 1)) = true;
3168 else
3169 u = force_target_expr (TREE_TYPE (t), TREE_OPERAND (t, 1),
3170 tf_warning_or_error);
3172 TARGET_EXPR_IMPLICIT_P (u) = TARGET_EXPR_IMPLICIT_P (t);
3173 TARGET_EXPR_LIST_INIT_P (u) = TARGET_EXPR_LIST_INIT_P (t);
3174 TARGET_EXPR_DIRECT_INIT_P (u) = TARGET_EXPR_DIRECT_INIT_P (t);
3175 TARGET_EXPR_ELIDING_P (u) = TARGET_EXPR_ELIDING_P (t);
3177 /* Map the old variable to the new one. */
3178 splay_tree_insert (target_remap,
3179 (splay_tree_key) TREE_OPERAND (t, 0),
3180 (splay_tree_value) TREE_OPERAND (u, 0));
3182 TREE_OPERAND (u, 1) = break_out_target_exprs (TREE_OPERAND (u, 1),
3183 data.clear_location);
3184 if (TREE_OPERAND (u, 1) == error_mark_node)
3185 return error_mark_node;
3187 if (data.clear_location)
3188 SET_EXPR_LOCATION (u, input_location);
3190 /* Replace the old expression with the new version. */
3191 *tp = u;
3192 /* We don't have to go below this point; the recursive call to
3193 break_out_target_exprs will have handled anything below this
3194 point. */
3195 *walk_subtrees = 0;
3196 return NULL_TREE;
3198 if (TREE_CODE (*tp) == SAVE_EXPR)
3200 t = *tp;
3201 splay_tree_node n = splay_tree_lookup (target_remap,
3202 (splay_tree_key) t);
3203 if (n)
3205 *tp = (tree)n->value;
3206 *walk_subtrees = 0;
3208 else
3210 copy_tree_r (tp, walk_subtrees, NULL);
3211 splay_tree_insert (target_remap,
3212 (splay_tree_key)t,
3213 (splay_tree_value)*tp);
3214 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
3215 splay_tree_insert (target_remap,
3216 (splay_tree_key)*tp,
3217 (splay_tree_value)*tp);
3219 return NULL_TREE;
3221 if (TREE_CODE (*tp) == DECL_EXPR
3222 && VAR_P (DECL_EXPR_DECL (*tp))
3223 && DECL_ARTIFICIAL (DECL_EXPR_DECL (*tp))
3224 && !TREE_STATIC (DECL_EXPR_DECL (*tp)))
3226 tree t;
3227 splay_tree_node n
3228 = splay_tree_lookup (target_remap,
3229 (splay_tree_key) DECL_EXPR_DECL (*tp));
3230 if (n)
3231 t = (tree) n->value;
3232 else
3234 t = create_temporary_var (TREE_TYPE (DECL_EXPR_DECL (*tp)));
3235 DECL_INITIAL (t) = DECL_INITIAL (DECL_EXPR_DECL (*tp));
3236 splay_tree_insert (target_remap,
3237 (splay_tree_key) DECL_EXPR_DECL (*tp),
3238 (splay_tree_value) t);
3240 copy_tree_r (tp, walk_subtrees, NULL);
3241 DECL_EXPR_DECL (*tp) = t;
3242 if (data.clear_location && EXPR_HAS_LOCATION (*tp))
3243 SET_EXPR_LOCATION (*tp, input_location);
3244 return NULL_TREE;
3246 if (TREE_CODE (*tp) == BIND_EXPR && BIND_EXPR_VARS (*tp))
3248 copy_tree_r (tp, walk_subtrees, NULL);
3249 for (tree *p = &BIND_EXPR_VARS (*tp); *p; p = &DECL_CHAIN (*p))
3251 gcc_assert (VAR_P (*p) && DECL_ARTIFICIAL (*p) && !TREE_STATIC (*p));
3252 tree t = create_temporary_var (TREE_TYPE (*p));
3253 DECL_INITIAL (t) = DECL_INITIAL (*p);
3254 DECL_CHAIN (t) = DECL_CHAIN (*p);
3255 splay_tree_insert (target_remap, (splay_tree_key) *p,
3256 (splay_tree_value) t);
3257 *p = t;
3259 if (data.clear_location && EXPR_HAS_LOCATION (*tp))
3260 SET_EXPR_LOCATION (*tp, input_location);
3261 return NULL_TREE;
3264 /* Make a copy of this node. */
3265 t = copy_tree_r (tp, walk_subtrees, NULL);
3266 if (TREE_CODE (*tp) == CALL_EXPR || TREE_CODE (*tp) == AGGR_INIT_EXPR)
3267 if (!processing_template_decl)
3268 set_flags_from_callee (*tp);
3269 if (data.clear_location && EXPR_HAS_LOCATION (*tp))
3270 SET_EXPR_LOCATION (*tp, input_location);
3271 return t;
3274 /* Replace all remapped VAR_DECLs in T with their new equivalents.
3275 DATA is really a splay-tree mapping old variables to new
3276 variables. */
3278 static tree
3279 bot_replace (tree* t, int */*walk_subtrees*/, void* data_)
3281 bot_data &data = *(bot_data*)data_;
3282 splay_tree target_remap = data.target_remap;
3284 if (VAR_P (*t))
3286 splay_tree_node n = splay_tree_lookup (target_remap,
3287 (splay_tree_key) *t);
3288 if (n)
3289 *t = (tree) n->value;
3291 else if (TREE_CODE (*t) == PARM_DECL
3292 && DECL_NAME (*t) == this_identifier
3293 && !DECL_CONTEXT (*t))
3295 /* In an NSDMI we need to replace the 'this' parameter we used for
3296 parsing with the real one for this function. */
3297 *t = current_class_ptr;
3299 else if (TREE_CODE (*t) == CONVERT_EXPR
3300 && CONVERT_EXPR_VBASE_PATH (*t))
3302 /* In an NSDMI build_base_path defers building conversions to morally
3303 virtual bases, and we handle it here. */
3304 tree basetype = TREE_TYPE (*t);
3305 *t = convert_to_base (TREE_OPERAND (*t, 0), basetype,
3306 /*check_access=*/false, /*nonnull=*/true,
3307 tf_warning_or_error);
3310 return NULL_TREE;
3313 /* When we parse a default argument expression, we may create
3314 temporary variables via TARGET_EXPRs. When we actually use the
3315 default-argument expression, we make a copy of the expression
3316 and replace the temporaries with appropriate local versions.
3318 If CLEAR_LOCATION is true, override any EXPR_LOCATION with
3319 input_location. */
3321 tree
3322 break_out_target_exprs (tree t, bool clear_location /* = false */)
3324 static int target_remap_count;
3325 static splay_tree target_remap;
3327 /* We shouldn't be called on templated trees, nor do we want to
3328 produce them. */
3329 gcc_checking_assert (!processing_template_decl);
3331 if (!target_remap_count++)
3332 target_remap = splay_tree_new (splay_tree_compare_pointers,
3333 /*splay_tree_delete_key_fn=*/NULL,
3334 /*splay_tree_delete_value_fn=*/NULL);
3335 bot_data data = { target_remap, clear_location };
3336 if (cp_walk_tree (&t, bot_manip, &data, NULL) == error_mark_node)
3337 t = error_mark_node;
3338 if (cp_walk_tree (&t, bot_replace, &data, NULL) == error_mark_node)
3339 t = error_mark_node;
3341 if (!--target_remap_count)
3343 splay_tree_delete (target_remap);
3344 target_remap = NULL;
3347 return t;
3350 /* Build an expression for the subobject of OBJ at CONSTRUCTOR index INDEX,
3351 which we expect to have type TYPE. */
3353 tree
3354 build_ctor_subob_ref (tree index, tree type, tree obj)
3356 if (index == NULL_TREE)
3357 /* Can't refer to a particular member of a vector. */
3358 obj = NULL_TREE;
3359 else if (TREE_CODE (index) == INTEGER_CST)
3360 obj = cp_build_array_ref (input_location, obj, index, tf_none);
3361 else
3362 obj = build_class_member_access_expr (obj, index, NULL_TREE,
3363 /*reference*/false, tf_none);
3364 if (obj)
3366 tree objtype = TREE_TYPE (obj);
3367 if (TREE_CODE (objtype) == ARRAY_TYPE && !TYPE_DOMAIN (objtype))
3369 /* When the destination object refers to a flexible array member
3370 verify that it matches the type of the source object except
3371 for its domain and qualifiers. */
3372 gcc_assert (comptypes (TYPE_MAIN_VARIANT (type),
3373 TYPE_MAIN_VARIANT (objtype),
3374 COMPARE_REDECLARATION));
3376 else
3377 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, objtype));
3380 return obj;
3383 struct replace_placeholders_t
3385 tree obj; /* The object to be substituted for a PLACEHOLDER_EXPR. */
3386 tree exp; /* The outermost exp. */
3387 bool seen; /* Whether we've encountered a PLACEHOLDER_EXPR. */
3388 hash_set<tree> *pset; /* To avoid walking same trees multiple times. */
3391 /* Like substitute_placeholder_in_expr, but handle C++ tree codes and
3392 build up subexpressions as we go deeper. */
3394 static tree
3395 replace_placeholders_r (tree* t, int* walk_subtrees, void* data_)
3397 replace_placeholders_t *d = static_cast<replace_placeholders_t*>(data_);
3398 tree obj = d->obj;
3400 if (TYPE_P (*t) || TREE_CONSTANT (*t))
3402 *walk_subtrees = false;
3403 return NULL_TREE;
3406 switch (TREE_CODE (*t))
3408 case PLACEHOLDER_EXPR:
3410 tree x = obj;
3411 for (; !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (*t),
3412 TREE_TYPE (x));
3413 x = TREE_OPERAND (x, 0))
3414 gcc_assert (handled_component_p (x));
3415 *t = unshare_expr (x);
3416 *walk_subtrees = false;
3417 d->seen = true;
3419 break;
3421 case CONSTRUCTOR:
3423 constructor_elt *ce;
3424 vec<constructor_elt,va_gc> *v = CONSTRUCTOR_ELTS (*t);
3425 /* Don't walk into CONSTRUCTOR_PLACEHOLDER_BOUNDARY ctors
3426 other than the d->exp one, those have PLACEHOLDER_EXPRs
3427 related to another object. */
3428 if ((CONSTRUCTOR_PLACEHOLDER_BOUNDARY (*t)
3429 && *t != d->exp)
3430 || d->pset->add (*t))
3432 *walk_subtrees = false;
3433 return NULL_TREE;
3435 for (unsigned i = 0; vec_safe_iterate (v, i, &ce); ++i)
3437 tree *valp = &ce->value;
3438 tree type = TREE_TYPE (*valp);
3439 tree subob = obj;
3441 /* Elements with RANGE_EXPR index shouldn't have any
3442 placeholders in them. */
3443 if (ce->index && TREE_CODE (ce->index) == RANGE_EXPR)
3444 continue;
3446 if (TREE_CODE (*valp) == CONSTRUCTOR
3447 && AGGREGATE_TYPE_P (type))
3449 /* If we're looking at the initializer for OBJ, then build
3450 a sub-object reference. If we're looking at an
3451 initializer for another object, just pass OBJ down. */
3452 if (same_type_ignoring_top_level_qualifiers_p
3453 (TREE_TYPE (*t), TREE_TYPE (obj)))
3454 subob = build_ctor_subob_ref (ce->index, type, obj);
3455 if (TREE_CODE (*valp) == TARGET_EXPR)
3456 valp = &TARGET_EXPR_INITIAL (*valp);
3458 d->obj = subob;
3459 cp_walk_tree (valp, replace_placeholders_r, data_, NULL);
3460 d->obj = obj;
3462 *walk_subtrees = false;
3463 break;
3466 default:
3467 if (d->pset->add (*t))
3468 *walk_subtrees = false;
3469 break;
3472 return NULL_TREE;
3475 /* Replace PLACEHOLDER_EXPRs in EXP with object OBJ. SEEN_P is set if
3476 a PLACEHOLDER_EXPR has been encountered. */
3478 tree
3479 replace_placeholders (tree exp, tree obj, bool *seen_p /*= NULL*/)
3481 /* This is only relevant for C++14. */
3482 if (cxx_dialect < cxx14)
3483 return exp;
3485 /* If the object isn't a (member of a) class, do nothing. */
3486 tree op0 = obj;
3487 while (handled_component_p (op0))
3488 op0 = TREE_OPERAND (op0, 0);
3489 if (!CLASS_TYPE_P (strip_array_types (TREE_TYPE (op0))))
3490 return exp;
3492 tree *tp = &exp;
3493 if (TREE_CODE (exp) == TARGET_EXPR)
3494 tp = &TARGET_EXPR_INITIAL (exp);
3495 hash_set<tree> pset;
3496 replace_placeholders_t data = { obj, *tp, false, &pset };
3497 cp_walk_tree (tp, replace_placeholders_r, &data, NULL);
3498 if (seen_p)
3499 *seen_p = data.seen;
3500 return exp;
3503 /* Callback function for find_placeholders. */
3505 static tree
3506 find_placeholders_r (tree *t, int *walk_subtrees, void *)
3508 if (TYPE_P (*t) || TREE_CONSTANT (*t))
3510 *walk_subtrees = false;
3511 return NULL_TREE;
3514 switch (TREE_CODE (*t))
3516 case PLACEHOLDER_EXPR:
3517 return *t;
3519 case CONSTRUCTOR:
3520 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (*t))
3521 *walk_subtrees = false;
3522 break;
3524 default:
3525 break;
3528 return NULL_TREE;
3531 /* Return true if EXP contains a PLACEHOLDER_EXPR. Don't walk into
3532 ctors with CONSTRUCTOR_PLACEHOLDER_BOUNDARY flag set. */
3534 bool
3535 find_placeholders (tree exp)
3537 /* This is only relevant for C++14. */
3538 if (cxx_dialect < cxx14)
3539 return false;
3541 return cp_walk_tree_without_duplicates (&exp, find_placeholders_r, NULL);
3544 /* Similar to `build_nt', but for template definitions of dependent
3545 expressions */
3547 tree
3548 build_min_nt_loc (location_t loc, enum tree_code code, ...)
3550 tree t;
3551 int length;
3552 int i;
3553 va_list p;
3555 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3557 va_start (p, code);
3559 t = make_node (code);
3560 SET_EXPR_LOCATION (t, loc);
3561 length = TREE_CODE_LENGTH (code);
3563 for (i = 0; i < length; i++)
3564 TREE_OPERAND (t, i) = va_arg (p, tree);
3566 va_end (p);
3567 return t;
3570 /* Similar to `build', but for template definitions. */
3572 tree
3573 build_min (enum tree_code code, tree tt, ...)
3575 tree t;
3576 int length;
3577 int i;
3578 va_list p;
3580 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3582 va_start (p, tt);
3584 t = make_node (code);
3585 length = TREE_CODE_LENGTH (code);
3586 TREE_TYPE (t) = tt;
3588 for (i = 0; i < length; i++)
3590 tree x = va_arg (p, tree);
3591 TREE_OPERAND (t, i) = x;
3592 if (x && !TYPE_P (x) && TREE_SIDE_EFFECTS (x))
3593 TREE_SIDE_EFFECTS (t) = 1;
3596 va_end (p);
3598 return t;
3601 /* Similar to `build', but for template definitions of non-dependent
3602 expressions. NON_DEP is the non-dependent expression that has been
3603 built. */
3605 tree
3606 build_min_non_dep (enum tree_code code, tree non_dep, ...)
3608 tree t;
3609 int length;
3610 int i;
3611 va_list p;
3613 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3615 va_start (p, non_dep);
3617 if (REFERENCE_REF_P (non_dep))
3618 non_dep = TREE_OPERAND (non_dep, 0);
3620 t = make_node (code);
3621 SET_EXPR_LOCATION (t, cp_expr_loc_or_input_loc (non_dep));
3622 length = TREE_CODE_LENGTH (code);
3623 TREE_TYPE (t) = unlowered_expr_type (non_dep);
3624 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
3626 for (i = 0; i < length; i++)
3628 tree x = va_arg (p, tree);
3629 TREE_OPERAND (t, i) = x;
3630 if (x && !TYPE_P (x))
3631 TREE_SIDE_EFFECTS (t) |= TREE_SIDE_EFFECTS (x);
3634 va_end (p);
3635 return convert_from_reference (t);
3638 /* Similar to build_min_nt, but call expressions */
3640 tree
3641 build_min_nt_call_vec (tree fn, vec<tree, va_gc> *args)
3643 tree ret, t;
3644 unsigned int ix;
3646 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
3647 CALL_EXPR_FN (ret) = fn;
3648 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
3649 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
3650 CALL_EXPR_ARG (ret, ix) = t;
3652 return ret;
3655 /* Similar to `build_min_nt_call_vec', but for template definitions of
3656 non-dependent expressions. NON_DEP is the non-dependent expression
3657 that has been built. */
3659 tree
3660 build_min_non_dep_call_vec (tree non_dep, tree fn, vec<tree, va_gc> *argvec)
3662 tree t = build_min_nt_call_vec (fn, argvec);
3663 if (REFERENCE_REF_P (non_dep))
3664 non_dep = TREE_OPERAND (non_dep, 0);
3665 TREE_TYPE (t) = TREE_TYPE (non_dep);
3666 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
3667 if (argvec)
3668 for (tree x : *argvec)
3669 if (x && !TYPE_P (x))
3670 TREE_SIDE_EFFECTS (t) |= TREE_SIDE_EFFECTS (x);
3671 return convert_from_reference (t);
3674 /* Similar to build_min_non_dep, but for expressions that have been resolved to
3675 a call to an operator overload. OP is the operator that has been
3676 overloaded. NON_DEP is the non-dependent expression that's been built,
3677 which should be a CALL_EXPR or an INDIRECT_REF to a CALL_EXPR. OVERLOAD is
3678 the overload that NON_DEP is calling. */
3680 tree
3681 build_min_non_dep_op_overload (enum tree_code op,
3682 tree non_dep,
3683 tree overload, ...)
3685 va_list p;
3686 int nargs, expected_nargs;
3687 tree fn, call, obj = NULL_TREE;
3689 non_dep = extract_call_expr (non_dep);
3691 nargs = call_expr_nargs (non_dep);
3693 expected_nargs = cp_tree_code_length (op);
3694 if (DECL_OBJECT_MEMBER_FUNCTION_P (overload)
3695 /* For ARRAY_REF, operator[] is either a non-static member or newly
3696 static member, never out of class and for the static member case
3697 if user uses single index the operator[] needs to have a single
3698 argument as well, but the function is called with 2 - the object
3699 it is invoked on and the index. */
3700 || op == ARRAY_REF)
3701 expected_nargs -= 1;
3702 if ((op == POSTINCREMENT_EXPR
3703 || op == POSTDECREMENT_EXPR)
3704 /* With -fpermissive non_dep could be operator++(). */
3705 && (!flag_permissive || nargs != expected_nargs))
3706 expected_nargs += 1;
3707 gcc_assert (nargs == expected_nargs);
3709 releasing_vec args;
3710 va_start (p, overload);
3712 if (!DECL_OBJECT_MEMBER_FUNCTION_P (overload))
3714 fn = overload;
3715 if (op == ARRAY_REF)
3716 obj = va_arg (p, tree);
3717 for (int i = 0; i < nargs; i++)
3719 tree arg = va_arg (p, tree);
3720 vec_safe_push (args, arg);
3723 else
3725 tree object = va_arg (p, tree);
3726 tree binfo = TYPE_BINFO (TREE_TYPE (object));
3727 tree method = build_baselink (binfo, binfo, overload, NULL_TREE);
3728 fn = build_min (COMPONENT_REF, TREE_TYPE (overload),
3729 object, method, NULL_TREE);
3730 for (int i = 0; i < nargs; i++)
3732 tree arg = va_arg (p, tree);
3733 vec_safe_push (args, arg);
3737 va_end (p);
3738 call = build_min_non_dep_call_vec (non_dep, fn, args);
3740 tree call_expr = extract_call_expr (call);
3741 KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep);
3742 CALL_EXPR_OPERATOR_SYNTAX (call_expr) = true;
3743 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep);
3744 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep);
3746 if (obj)
3747 return keep_unused_object_arg (call, obj, overload);
3748 return call;
3751 /* Similar to above build_min_non_dep_op_overload, but arguments
3752 are taken from ARGS vector. */
3754 tree
3755 build_min_non_dep_op_overload (tree non_dep, tree overload, tree object,
3756 vec<tree, va_gc> *args)
3758 non_dep = extract_call_expr (non_dep);
3760 unsigned int nargs = call_expr_nargs (non_dep);
3761 tree fn = overload;
3762 if (DECL_OBJECT_MEMBER_FUNCTION_P (overload))
3764 tree binfo = TYPE_BINFO (TREE_TYPE (object));
3765 tree method = build_baselink (binfo, binfo, overload, NULL_TREE);
3766 fn = build_min (COMPONENT_REF, TREE_TYPE (overload),
3767 object, method, NULL_TREE);
3768 object = NULL_TREE;
3770 gcc_assert (vec_safe_length (args) == nargs);
3772 tree call = build_min_non_dep_call_vec (non_dep, fn, args);
3774 tree call_expr = extract_call_expr (call);
3775 KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep);
3776 CALL_EXPR_OPERATOR_SYNTAX (call_expr) = true;
3777 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep);
3778 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep);
3780 if (object)
3781 return keep_unused_object_arg (call, object, overload);
3782 return call;
3785 /* Return a new tree vec copied from VEC, with ELT inserted at index IDX. */
3787 vec<tree, va_gc> *
3788 vec_copy_and_insert (vec<tree, va_gc> *old_vec, tree elt, unsigned idx)
3790 unsigned len = vec_safe_length (old_vec);
3791 gcc_assert (idx <= len);
3793 vec<tree, va_gc> *new_vec = NULL;
3794 vec_alloc (new_vec, len + 1);
3796 unsigned i;
3797 for (i = 0; i < len; ++i)
3799 if (i == idx)
3800 new_vec->quick_push (elt);
3801 new_vec->quick_push ((*old_vec)[i]);
3803 if (i == idx)
3804 new_vec->quick_push (elt);
3806 return new_vec;
3809 tree
3810 get_type_decl (tree t)
3812 if (TREE_CODE (t) == TYPE_DECL)
3813 return t;
3814 if (TYPE_P (t))
3815 return TYPE_STUB_DECL (t);
3816 gcc_assert (t == error_mark_node);
3817 return t;
3820 /* Returns the namespace that contains DECL, whether directly or
3821 indirectly. */
3823 tree
3824 decl_namespace_context (tree decl)
3826 while (1)
3828 if (TREE_CODE (decl) == NAMESPACE_DECL)
3829 return decl;
3830 else if (TYPE_P (decl))
3831 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
3832 else
3833 decl = CP_DECL_CONTEXT (decl);
3837 /* Returns true if decl is within an anonymous namespace, however deeply
3838 nested, or false otherwise. */
3840 bool
3841 decl_anon_ns_mem_p (tree decl)
3843 return !TREE_PUBLIC (decl_namespace_context (decl));
3846 /* Returns true if the enclosing scope of DECL has internal or no linkage. */
3848 bool
3849 decl_internal_context_p (const_tree decl)
3851 while (TREE_CODE (decl) != NAMESPACE_DECL)
3853 /* Classes inside anonymous namespaces have TREE_PUBLIC == 0. */
3854 if (TYPE_P (decl))
3855 return !TREE_PUBLIC (TYPE_MAIN_DECL (decl));
3857 decl = CP_DECL_CONTEXT (decl);
3859 return !TREE_PUBLIC (decl);
3862 /* Subroutine of cp_tree_equal: t1 and t2 are two CALL_EXPRs.
3863 Return whether their CALL_EXPR_FNs are equivalent. */
3865 static bool
3866 called_fns_equal (tree t1, tree t2)
3868 /* Core 1321: dependent names are equivalent even if the overload sets
3869 are different. But do compare explicit template arguments. */
3870 tree name1 = call_expr_dependent_name (t1);
3871 tree name2 = call_expr_dependent_name (t2);
3872 t1 = CALL_EXPR_FN (t1);
3873 t2 = CALL_EXPR_FN (t2);
3874 if (name1 || name2)
3876 tree targs1 = NULL_TREE, targs2 = NULL_TREE;
3878 if (name1 != name2)
3879 return false;
3881 /* FIXME dependent_name currently returns an unqualified name regardless
3882 of whether the function was named with a qualified- or unqualified-id.
3883 Until that's fixed, check that we aren't looking at overload sets from
3884 different scopes. */
3885 if (is_overloaded_fn (t1) && is_overloaded_fn (t2)
3886 && (DECL_CONTEXT (get_first_fn (t1))
3887 != DECL_CONTEXT (get_first_fn (t2))))
3888 return false;
3890 if (TREE_CODE (t1) == TEMPLATE_ID_EXPR)
3891 targs1 = TREE_OPERAND (t1, 1);
3892 if (TREE_CODE (t2) == TEMPLATE_ID_EXPR)
3893 targs2 = TREE_OPERAND (t2, 1);
3894 return cp_tree_equal (targs1, targs2);
3896 else
3897 return cp_tree_equal (t1, t2);
3900 bool comparing_override_contracts;
3902 /* In a component reference, return the innermost object of
3903 the postfix-expression. */
3905 static tree
3906 get_innermost_component (tree t)
3908 gcc_assert (TREE_CODE (t) == COMPONENT_REF);
3909 while (TREE_CODE (t) == COMPONENT_REF)
3910 t = TREE_OPERAND (t, 0);
3911 return t;
3914 /* Returns true if T is a possibly converted 'this' or '*this' expression. */
3916 static bool
3917 is_this_expression (tree t)
3919 t = get_innermost_component (t);
3920 /* See through deferences and no-op conversions. */
3921 if (INDIRECT_REF_P (t))
3922 t = TREE_OPERAND (t, 0);
3923 if (TREE_CODE (t) == NOP_EXPR)
3924 t = TREE_OPERAND (t, 0);
3925 return is_this_parameter (t);
3928 static bool
3929 comparing_this_references (tree t1, tree t2)
3931 return is_this_expression (t1) && is_this_expression (t2);
3934 static bool
3935 equivalent_member_references (tree t1, tree t2)
3937 if (!comparing_this_references (t1, t2))
3938 return false;
3939 t1 = TREE_OPERAND (t1, 1);
3940 t2 = TREE_OPERAND (t2, 1);
3941 return t1 == t2;
3944 /* Return truthvalue of whether T1 is the same tree structure as T2.
3945 Return 1 if they are the same. Return 0 if they are different. */
3947 bool
3948 cp_tree_equal (tree t1, tree t2)
3950 enum tree_code code1, code2;
3952 if (t1 == t2)
3953 return true;
3954 if (!t1 || !t2)
3955 return false;
3957 code1 = TREE_CODE (t1);
3958 code2 = TREE_CODE (t2);
3960 if (code1 != code2)
3961 return false;
3963 if (CONSTANT_CLASS_P (t1)
3964 && !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3965 return false;
3967 switch (code1)
3969 case VOID_CST:
3970 /* There's only a single VOID_CST node, so we should never reach
3971 here. */
3972 gcc_unreachable ();
3974 case INTEGER_CST:
3975 return tree_int_cst_equal (t1, t2);
3977 case REAL_CST:
3978 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
3980 case STRING_CST:
3981 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3982 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3983 TREE_STRING_LENGTH (t1));
3985 case FIXED_CST:
3986 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
3987 TREE_FIXED_CST (t2));
3989 case COMPLEX_CST:
3990 return cp_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
3991 && cp_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
3993 case VECTOR_CST:
3994 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
3996 case CONSTRUCTOR:
3997 /* We need to do this when determining whether or not two
3998 non-type pointer to member function template arguments
3999 are the same. */
4000 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
4001 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
4002 return false;
4004 tree field, value;
4005 unsigned int i;
4006 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
4008 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
4009 if (!cp_tree_equal (field, elt2->index)
4010 || !cp_tree_equal (value, elt2->value))
4011 return false;
4014 return true;
4016 case TREE_LIST:
4017 if (!cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
4018 return false;
4019 if (!cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
4020 return false;
4021 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
4023 case SAVE_EXPR:
4024 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
4026 case CALL_EXPR:
4028 if (KOENIG_LOOKUP_P (t1) != KOENIG_LOOKUP_P (t2))
4029 return false;
4031 if (!called_fns_equal (t1, t2))
4032 return false;
4034 call_expr_arg_iterator iter1, iter2;
4035 init_call_expr_arg_iterator (t1, &iter1);
4036 init_call_expr_arg_iterator (t2, &iter2);
4037 if (iter1.n != iter2.n)
4038 return false;
4040 while (more_call_expr_args_p (&iter1))
4042 tree arg1 = next_call_expr_arg (&iter1);
4043 tree arg2 = next_call_expr_arg (&iter2);
4045 gcc_checking_assert (arg1 && arg2);
4046 if (!cp_tree_equal (arg1, arg2))
4047 return false;
4050 return true;
4053 case TARGET_EXPR:
4055 tree o1 = TREE_OPERAND (t1, 0);
4056 tree o2 = TREE_OPERAND (t2, 0);
4058 /* Special case: if either target is an unallocated VAR_DECL,
4059 it means that it's going to be unified with whatever the
4060 TARGET_EXPR is really supposed to initialize, so treat it
4061 as being equivalent to anything. */
4062 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
4063 && !DECL_RTL_SET_P (o1))
4064 /*Nop*/;
4065 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
4066 && !DECL_RTL_SET_P (o2))
4067 /*Nop*/;
4068 else if (!cp_tree_equal (o1, o2))
4069 return false;
4071 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
4074 case PARM_DECL:
4075 /* For comparing uses of parameters in late-specified return types
4076 with an out-of-class definition of the function, but can also come
4077 up for expressions that involve 'this' in a member function
4078 template. */
4080 if (comparing_specializations
4081 && DECL_CONTEXT (t1) != DECL_CONTEXT (t2))
4082 /* When comparing hash table entries, only an exact match is
4083 good enough; we don't want to replace 'this' with the
4084 version from another function. But be more flexible
4085 with parameters with identical contexts. */
4086 return false;
4088 if (same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
4090 if (DECL_ARTIFICIAL (t1) ^ DECL_ARTIFICIAL (t2))
4091 return false;
4092 if (CONSTRAINT_VAR_P (t1) ^ CONSTRAINT_VAR_P (t2))
4093 return false;
4094 if (DECL_ARTIFICIAL (t1)
4095 || (DECL_PARM_LEVEL (t1) == DECL_PARM_LEVEL (t2)
4096 && DECL_PARM_INDEX (t1) == DECL_PARM_INDEX (t2)))
4097 return true;
4099 return false;
4101 case TEMPLATE_DECL:
4102 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t1)
4103 && DECL_TEMPLATE_TEMPLATE_PARM_P (t2))
4104 return cp_tree_equal (TREE_TYPE (t1), TREE_TYPE (t2));
4105 /* Fall through. */
4106 case VAR_DECL:
4107 case CONST_DECL:
4108 case FIELD_DECL:
4109 case FUNCTION_DECL:
4110 case IDENTIFIER_NODE:
4111 case SSA_NAME:
4112 case USING_DECL:
4113 case DEFERRED_PARSE:
4114 return false;
4116 case BASELINK:
4117 return (BASELINK_BINFO (t1) == BASELINK_BINFO (t2)
4118 && BASELINK_ACCESS_BINFO (t1) == BASELINK_ACCESS_BINFO (t2)
4119 && BASELINK_QUALIFIED_P (t1) == BASELINK_QUALIFIED_P (t2)
4120 && cp_tree_equal (BASELINK_FUNCTIONS (t1),
4121 BASELINK_FUNCTIONS (t2)));
4123 case TEMPLATE_PARM_INDEX:
4124 return (TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
4125 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2)
4126 && (TEMPLATE_PARM_PARAMETER_PACK (t1)
4127 == TEMPLATE_PARM_PARAMETER_PACK (t2))
4128 && same_type_p (TREE_TYPE (TEMPLATE_PARM_DECL (t1)),
4129 TREE_TYPE (TEMPLATE_PARM_DECL (t2))));
4131 case TEMPLATE_ID_EXPR:
4132 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
4133 return false;
4134 if (!comp_template_args (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)))
4135 return false;
4136 return true;
4138 case CONSTRAINT_INFO:
4139 return cp_tree_equal (CI_ASSOCIATED_CONSTRAINTS (t1),
4140 CI_ASSOCIATED_CONSTRAINTS (t2));
4142 case CHECK_CONSTR:
4143 return (CHECK_CONSTR_CONCEPT (t1) == CHECK_CONSTR_CONCEPT (t2)
4144 && comp_template_args (CHECK_CONSTR_ARGS (t1),
4145 CHECK_CONSTR_ARGS (t2)));
4147 case TREE_VEC:
4148 /* These are template args. Really we should be getting the
4149 caller to do this as it knows it to be true. */
4150 if (!comp_template_args (t1, t2))
4151 return false;
4152 return true;
4154 case SIZEOF_EXPR:
4155 case ALIGNOF_EXPR:
4157 tree o1 = TREE_OPERAND (t1, 0);
4158 tree o2 = TREE_OPERAND (t2, 0);
4160 if (code1 == SIZEOF_EXPR)
4162 if (SIZEOF_EXPR_TYPE_P (t1))
4163 o1 = TREE_TYPE (o1);
4164 if (SIZEOF_EXPR_TYPE_P (t2))
4165 o2 = TREE_TYPE (o2);
4167 else if (ALIGNOF_EXPR_STD_P (t1) != ALIGNOF_EXPR_STD_P (t2))
4168 return false;
4170 if (TREE_CODE (o1) != TREE_CODE (o2))
4171 return false;
4173 if (ARGUMENT_PACK_P (o1))
4174 return template_args_equal (o1, o2);
4175 else if (TYPE_P (o1))
4176 return same_type_p (o1, o2);
4177 else
4178 return cp_tree_equal (o1, o2);
4181 case MODOP_EXPR:
4183 tree t1_op1, t2_op1;
4185 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
4186 return false;
4188 t1_op1 = TREE_OPERAND (t1, 1);
4189 t2_op1 = TREE_OPERAND (t2, 1);
4190 if (TREE_CODE (t1_op1) != TREE_CODE (t2_op1))
4191 return false;
4193 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t2, 2));
4196 case PTRMEM_CST:
4197 /* Two pointer-to-members are the same if they point to the same
4198 field or function in the same class. */
4199 if (PTRMEM_CST_MEMBER (t1) != PTRMEM_CST_MEMBER (t2))
4200 return false;
4202 return same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2));
4204 case OVERLOAD:
4206 /* Two overloads. Must be exactly the same set of decls. */
4207 lkp_iterator first (t1);
4208 lkp_iterator second (t2);
4210 for (; first && second; ++first, ++second)
4211 if (*first != *second)
4212 return false;
4213 return !(first || second);
4216 case TRAIT_EXPR:
4217 if (TRAIT_EXPR_KIND (t1) != TRAIT_EXPR_KIND (t2))
4218 return false;
4219 return cp_tree_equal (TRAIT_EXPR_TYPE1 (t1), TRAIT_EXPR_TYPE1 (t2))
4220 && cp_tree_equal (TRAIT_EXPR_TYPE2 (t1), TRAIT_EXPR_TYPE2 (t2));
4222 case NON_LVALUE_EXPR:
4223 case VIEW_CONVERT_EXPR:
4224 /* Used for location wrappers with possibly NULL types. */
4225 if (!TREE_TYPE (t1) || !TREE_TYPE (t2))
4227 if (TREE_TYPE (t1) || TREE_TYPE (t2))
4228 return false;
4229 break;
4231 /* FALLTHROUGH */
4233 case CAST_EXPR:
4234 case STATIC_CAST_EXPR:
4235 case REINTERPRET_CAST_EXPR:
4236 case CONST_CAST_EXPR:
4237 case DYNAMIC_CAST_EXPR:
4238 case IMPLICIT_CONV_EXPR:
4239 case NEW_EXPR:
4240 case BIT_CAST_EXPR:
4241 CASE_CONVERT:
4242 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
4243 return false;
4244 /* Now compare operands as usual. */
4245 break;
4247 case DEFERRED_NOEXCEPT:
4248 return (cp_tree_equal (DEFERRED_NOEXCEPT_PATTERN (t1),
4249 DEFERRED_NOEXCEPT_PATTERN (t2))
4250 && comp_template_args (DEFERRED_NOEXCEPT_ARGS (t1),
4251 DEFERRED_NOEXCEPT_ARGS (t2)));
4253 case LAMBDA_EXPR:
4254 /* Two lambda-expressions are never considered equivalent. */
4255 return false;
4257 case TYPE_ARGUMENT_PACK:
4258 case NONTYPE_ARGUMENT_PACK:
4260 tree p1 = ARGUMENT_PACK_ARGS (t1);
4261 tree p2 = ARGUMENT_PACK_ARGS (t2);
4262 int len = TREE_VEC_LENGTH (p1);
4263 if (TREE_VEC_LENGTH (p2) != len)
4264 return false;
4266 for (int ix = 0; ix != len; ix++)
4267 if (!template_args_equal (TREE_VEC_ELT (p1, ix),
4268 TREE_VEC_ELT (p2, ix)))
4269 return false;
4270 return true;
4273 case EXPR_PACK_EXPANSION:
4274 if (!cp_tree_equal (PACK_EXPANSION_PATTERN (t1),
4275 PACK_EXPANSION_PATTERN (t2)))
4276 return false;
4277 if (!comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
4278 PACK_EXPANSION_EXTRA_ARGS (t2)))
4279 return false;
4280 return true;
4282 case COMPONENT_REF:
4283 /* If we're comparing contract conditions of overrides, member references
4284 compare equal if they designate the same member. */
4285 if (comparing_override_contracts)
4286 return equivalent_member_references (t1, t2);
4287 break;
4289 default:
4290 break;
4293 switch (TREE_CODE_CLASS (code1))
4295 case tcc_unary:
4296 case tcc_binary:
4297 case tcc_comparison:
4298 case tcc_expression:
4299 case tcc_vl_exp:
4300 case tcc_reference:
4301 case tcc_statement:
4303 int n = cp_tree_operand_length (t1);
4304 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
4305 && n != TREE_OPERAND_LENGTH (t2))
4306 return false;
4308 for (int i = 0; i < n; ++i)
4309 if (!cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
4310 return false;
4312 return true;
4315 case tcc_type:
4316 return same_type_p (t1, t2);
4318 default:
4319 gcc_unreachable ();
4322 /* We can get here with --disable-checking. */
4323 return false;
4326 /* The type of ARG when used as an lvalue. */
4328 tree
4329 lvalue_type (tree arg)
4331 tree type = TREE_TYPE (arg);
4332 return type;
4335 /* The type of ARG for printing error messages; denote lvalues with
4336 reference types. */
4338 tree
4339 error_type (tree arg)
4341 tree type = TREE_TYPE (arg);
4343 if (TREE_CODE (type) == ARRAY_TYPE)
4345 else if (TREE_CODE (type) == ERROR_MARK)
4347 else if (lvalue_p (arg))
4348 type = build_reference_type (lvalue_type (arg));
4349 else if (MAYBE_CLASS_TYPE_P (type))
4350 type = lvalue_type (arg);
4352 return type;
4355 /* Does FUNCTION use a variable-length argument list? */
4358 varargs_function_p (const_tree function)
4360 return stdarg_p (TREE_TYPE (function));
4363 /* Returns 1 if decl is a member of a class. */
4366 member_p (const_tree decl)
4368 const_tree const ctx = DECL_CONTEXT (decl);
4369 return (ctx && TYPE_P (ctx));
4372 /* Create a placeholder for member access where we don't actually have an
4373 object that the access is against. For a general declval<T> equivalent,
4374 use build_stub_object instead. */
4376 tree
4377 build_dummy_object (tree type)
4379 tree decl = build1 (CONVERT_EXPR, build_pointer_type (type), void_node);
4380 return cp_build_fold_indirect_ref (decl);
4383 /* We've gotten a reference to a member of TYPE. Return *this if appropriate,
4384 or a dummy object otherwise. If BINFOP is non-0, it is filled with the
4385 binfo path from current_class_type to TYPE, or 0. */
4387 tree
4388 maybe_dummy_object (tree type, tree* binfop)
4390 tree decl, context;
4391 tree binfo;
4392 tree current = current_nonlambda_class_type ();
4394 if (current
4395 && (binfo = lookup_base (current, type, ba_any, NULL,
4396 tf_warning_or_error)))
4397 context = current;
4398 else
4400 /* Reference from a nested class member function. */
4401 context = type;
4402 binfo = TYPE_BINFO (type);
4405 if (binfop)
4406 *binfop = binfo;
4408 /* current_class_ref might not correspond to current_class_type if
4409 we're in tsubst_default_argument or a lambda-declarator; in either
4410 case, we want to use current_class_ref if it matches CONTEXT. */
4411 tree ctype = current_class_ref ? TREE_TYPE (current_class_ref) : NULL_TREE;
4412 if (ctype
4413 && same_type_ignoring_top_level_qualifiers_p (ctype, context))
4414 decl = current_class_ref;
4415 else
4417 /* Return a dummy object whose cv-quals are consistent with (the
4418 non-lambda) 'this' if available. */
4419 if (ctype)
4421 int quals = TYPE_UNQUALIFIED;
4422 if (tree lambda = CLASSTYPE_LAMBDA_EXPR (ctype))
4424 if (tree cap = lambda_expr_this_capture (lambda, false))
4425 quals = cp_type_quals (TREE_TYPE (TREE_TYPE (cap)));
4427 else
4428 quals = cp_type_quals (ctype);
4429 context = cp_build_qualified_type (context, quals);
4431 decl = build_dummy_object (context);
4434 return decl;
4437 /* Returns 1 if OB is a placeholder object, or a pointer to one. */
4439 bool
4440 is_dummy_object (const_tree ob)
4442 if (INDIRECT_REF_P (ob))
4443 ob = TREE_OPERAND (ob, 0);
4444 return (TREE_CODE (ob) == CONVERT_EXPR
4445 && TREE_OPERAND (ob, 0) == void_node);
4448 /* Returns true if TYPE is char, unsigned char, or std::byte. */
4450 bool
4451 is_byte_access_type (tree type)
4453 type = TYPE_MAIN_VARIANT (type);
4454 if (type == char_type_node
4455 || type == unsigned_char_type_node)
4456 return true;
4458 return (TREE_CODE (type) == ENUMERAL_TYPE
4459 && TYPE_CONTEXT (type) == std_node
4460 && !strcmp ("byte", TYPE_NAME_STRING (type)));
4463 /* Returns true if TYPE is unsigned char or std::byte. */
4465 bool
4466 is_byte_access_type_not_plain_char (tree type)
4468 type = TYPE_MAIN_VARIANT (type);
4469 if (type == char_type_node)
4470 return false;
4472 return is_byte_access_type (type);
4475 /* Returns 1 iff type T is something we want to treat as a scalar type for
4476 the purpose of deciding whether it is trivial/POD/standard-layout. */
4478 bool
4479 scalarish_type_p (const_tree t)
4481 if (t == error_mark_node)
4482 return 1;
4484 return (SCALAR_TYPE_P (t) || VECTOR_TYPE_P (t));
4487 /* Returns true iff T requires non-trivial default initialization. */
4489 bool
4490 type_has_nontrivial_default_init (const_tree t)
4492 t = strip_array_types (CONST_CAST_TREE (t));
4494 if (CLASS_TYPE_P (t))
4495 return TYPE_HAS_COMPLEX_DFLT (t);
4496 else
4497 return 0;
4500 /* Track classes with only deleted copy/move constructors so that we can warn
4501 if they are used in call/return by value. */
4503 static GTY(()) hash_set<tree>* deleted_copy_types;
4504 static void
4505 remember_deleted_copy (const_tree t)
4507 if (!deleted_copy_types)
4508 deleted_copy_types = hash_set<tree>::create_ggc(37);
4509 deleted_copy_types->add (CONST_CAST_TREE (t));
4511 void
4512 maybe_warn_parm_abi (tree t, location_t loc)
4514 if (!deleted_copy_types
4515 || !deleted_copy_types->contains (t))
4516 return;
4518 if ((flag_abi_version == 12 || warn_abi_version == 12)
4519 && classtype_has_non_deleted_move_ctor (t))
4521 bool w;
4522 auto_diagnostic_group d;
4523 if (flag_abi_version > 12)
4524 w = warning_at (loc, OPT_Wabi, "%<-fabi-version=13%> (GCC 8.2) fixes "
4525 "the calling convention for %qT, which was "
4526 "accidentally changed in 8.1", t);
4527 else
4528 w = warning_at (loc, OPT_Wabi, "%<-fabi-version=12%> (GCC 8.1) "
4529 "accidentally changes the calling convention for %qT",
4531 if (w)
4532 inform (location_of (t), " declared here");
4533 return;
4536 auto_diagnostic_group d;
4537 if (warning_at (loc, OPT_Wabi, "the calling convention for %qT changes in "
4538 "%<-fabi-version=13%> (GCC 8.2)", t))
4539 inform (location_of (t), " because all of its copy and move "
4540 "constructors are deleted");
4543 /* Returns true iff copying an object of type T (including via move
4544 constructor) is non-trivial. That is, T has no non-trivial copy
4545 constructors and no non-trivial move constructors, and not all copy/move
4546 constructors are deleted. This function implements the ABI notion of
4547 non-trivial copy, which has diverged from the one in the standard. */
4549 bool
4550 type_has_nontrivial_copy_init (const_tree type)
4552 tree t = strip_array_types (CONST_CAST_TREE (type));
4554 if (CLASS_TYPE_P (t))
4556 gcc_assert (COMPLETE_TYPE_P (t));
4558 if (TYPE_HAS_COMPLEX_COPY_CTOR (t)
4559 || TYPE_HAS_COMPLEX_MOVE_CTOR (t))
4560 /* Nontrivial. */
4561 return true;
4563 if (cxx_dialect < cxx11)
4564 /* No deleted functions before C++11. */
4565 return false;
4567 /* Before ABI v12 we did a bitwise copy of types with only deleted
4568 copy/move constructors. */
4569 if (!abi_version_at_least (12)
4570 && !(warn_abi && abi_version_crosses (12)))
4571 return false;
4573 bool saw_copy = false;
4574 bool saw_non_deleted = false;
4575 bool saw_non_deleted_move = false;
4577 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
4578 saw_copy = saw_non_deleted = true;
4579 else if (CLASSTYPE_LAZY_COPY_CTOR (t))
4581 saw_copy = true;
4582 if (classtype_has_move_assign_or_move_ctor_p (t, true))
4583 /* [class.copy]/8 If the class definition declares a move
4584 constructor or move assignment operator, the implicitly declared
4585 copy constructor is defined as deleted.... */;
4586 else
4587 /* Any other reason the implicitly-declared function would be
4588 deleted would also cause TYPE_HAS_COMPLEX_COPY_CTOR to be
4589 set. */
4590 saw_non_deleted = true;
4593 if (!saw_non_deleted)
4594 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4596 tree fn = *iter;
4597 if (copy_fn_p (fn))
4599 saw_copy = true;
4600 if (!DECL_DELETED_FN (fn))
4602 /* Not deleted, therefore trivial. */
4603 saw_non_deleted = true;
4604 break;
4607 else if (move_fn_p (fn))
4608 if (!DECL_DELETED_FN (fn))
4609 saw_non_deleted_move = true;
4612 gcc_assert (saw_copy);
4614 /* ABI v12 buggily ignored move constructors. */
4615 bool v11nontriv = false;
4616 bool v12nontriv = !saw_non_deleted;
4617 bool v13nontriv = !saw_non_deleted && !saw_non_deleted_move;
4618 bool nontriv = (abi_version_at_least (13) ? v13nontriv
4619 : flag_abi_version == 12 ? v12nontriv
4620 : v11nontriv);
4621 bool warn_nontriv = (warn_abi_version >= 13 ? v13nontriv
4622 : warn_abi_version == 12 ? v12nontriv
4623 : v11nontriv);
4624 if (nontriv != warn_nontriv)
4625 remember_deleted_copy (t);
4627 return nontriv;
4629 else
4630 return 0;
4633 /* Returns 1 iff type T is a trivially copyable type, as defined in
4634 [basic.types] and [class]. */
4636 bool
4637 trivially_copyable_p (const_tree t)
4639 t = strip_array_types (CONST_CAST_TREE (t));
4641 if (CLASS_TYPE_P (t))
4642 return ((!TYPE_HAS_COPY_CTOR (t)
4643 || !TYPE_HAS_COMPLEX_COPY_CTOR (t))
4644 && !TYPE_HAS_COMPLEX_MOVE_CTOR (t)
4645 && (!TYPE_HAS_COPY_ASSIGN (t)
4646 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (t))
4647 && !TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
4648 && TYPE_HAS_TRIVIAL_DESTRUCTOR (t));
4649 else
4650 /* CWG 2094 makes volatile-qualified scalars trivially copyable again. */
4651 return scalarish_type_p (t);
4654 /* Returns 1 iff type T is a trivial type, as defined in [basic.types] and
4655 [class]. */
4657 bool
4658 trivial_type_p (const_tree t)
4660 t = strip_array_types (CONST_CAST_TREE (t));
4662 if (CLASS_TYPE_P (t))
4663 return (TYPE_HAS_TRIVIAL_DFLT (t)
4664 && trivially_copyable_p (t));
4665 else
4666 return scalarish_type_p (t);
4669 /* Returns 1 iff type T is a POD type, as defined in [basic.types]. */
4671 bool
4672 pod_type_p (const_tree t)
4674 /* This CONST_CAST is okay because strip_array_types returns its
4675 argument unmodified and we assign it to a const_tree. */
4676 t = strip_array_types (CONST_CAST_TREE(t));
4678 if (!CLASS_TYPE_P (t))
4679 return scalarish_type_p (t);
4680 else if (cxx_dialect > cxx98)
4681 /* [class]/10: A POD struct is a class that is both a trivial class and a
4682 standard-layout class, and has no non-static data members of type
4683 non-POD struct, non-POD union (or array of such types).
4685 We don't need to check individual members because if a member is
4686 non-std-layout or non-trivial, the class will be too. */
4687 return (std_layout_type_p (t) && trivial_type_p (t));
4688 else
4689 /* The C++98 definition of POD is different. */
4690 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
4693 /* Returns true iff T is POD for the purpose of layout, as defined in the
4694 C++ ABI. */
4696 bool
4697 layout_pod_type_p (const_tree t)
4699 t = strip_array_types (CONST_CAST_TREE (t));
4701 if (CLASS_TYPE_P (t))
4702 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
4703 else
4704 return scalarish_type_p (t);
4707 /* Returns true iff T is a standard-layout type, as defined in
4708 [basic.types]. */
4710 bool
4711 std_layout_type_p (const_tree t)
4713 t = strip_array_types (CONST_CAST_TREE (t));
4715 if (CLASS_TYPE_P (t))
4716 return !CLASSTYPE_NON_STD_LAYOUT (t);
4717 else
4718 return scalarish_type_p (t);
4721 static bool record_has_unique_obj_representations (const_tree, const_tree);
4723 /* Returns true iff T satisfies std::has_unique_object_representations<T>,
4724 as defined in [meta.unary.prop]. */
4726 bool
4727 type_has_unique_obj_representations (const_tree t)
4729 bool ret;
4731 t = strip_array_types (CONST_CAST_TREE (t));
4733 if (!trivially_copyable_p (t))
4734 return false;
4736 if (CLASS_TYPE_P (t) && CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t))
4737 return CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t);
4739 switch (TREE_CODE (t))
4741 case INTEGER_TYPE:
4742 case POINTER_TYPE:
4743 case REFERENCE_TYPE:
4744 /* If some backend has any paddings in these types, we should add
4745 a target hook for this and handle it there. */
4746 return true;
4748 case BOOLEAN_TYPE:
4749 /* For bool values other than 0 and 1 should only appear with
4750 undefined behavior. */
4751 return true;
4753 case ENUMERAL_TYPE:
4754 return type_has_unique_obj_representations (ENUM_UNDERLYING_TYPE (t));
4756 case REAL_TYPE:
4757 /* XFmode certainly contains padding on x86, which the CPU doesn't store
4758 when storing long double values, so for that we have to return false.
4759 Other kinds of floating point values are questionable due to +.0/-.0
4760 and NaNs, let's play safe for now. */
4761 return false;
4763 case FIXED_POINT_TYPE:
4764 return false;
4766 case OFFSET_TYPE:
4767 return true;
4769 case COMPLEX_TYPE:
4770 case VECTOR_TYPE:
4771 return type_has_unique_obj_representations (TREE_TYPE (t));
4773 case RECORD_TYPE:
4774 ret = record_has_unique_obj_representations (t, TYPE_SIZE (t));
4775 if (CLASS_TYPE_P (t))
4777 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
4778 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
4780 return ret;
4782 case UNION_TYPE:
4783 ret = true;
4784 bool any_fields;
4785 any_fields = false;
4786 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4787 if (TREE_CODE (field) == FIELD_DECL)
4789 any_fields = true;
4790 if (!type_has_unique_obj_representations (TREE_TYPE (field))
4791 || simple_cst_equal (DECL_SIZE (field), TYPE_SIZE (t)) != 1)
4793 ret = false;
4794 break;
4797 if (!any_fields && !integer_zerop (TYPE_SIZE (t)))
4798 ret = false;
4799 if (CLASS_TYPE_P (t))
4801 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
4802 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
4804 return ret;
4806 case NULLPTR_TYPE:
4807 return false;
4809 case ERROR_MARK:
4810 return false;
4812 default:
4813 gcc_unreachable ();
4817 /* Helper function for type_has_unique_obj_representations. */
4819 static bool
4820 record_has_unique_obj_representations (const_tree t, const_tree sz)
4822 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4823 if (TREE_CODE (field) != FIELD_DECL)
4825 /* For bases, can't use type_has_unique_obj_representations here, as in
4826 struct S { int i : 24; S (); };
4827 struct T : public S { int j : 8; T (); };
4828 S doesn't have unique obj representations, but T does. */
4829 else if (DECL_FIELD_IS_BASE (field))
4831 if (!record_has_unique_obj_representations (TREE_TYPE (field),
4832 DECL_SIZE (field)))
4833 return false;
4835 else if (DECL_C_BIT_FIELD (field) && !DECL_UNNAMED_BIT_FIELD (field))
4837 tree btype = DECL_BIT_FIELD_TYPE (field);
4838 if (!type_has_unique_obj_representations (btype))
4839 return false;
4841 else if (!type_has_unique_obj_representations (TREE_TYPE (field)))
4842 return false;
4844 offset_int cur = 0;
4845 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4846 if (TREE_CODE (field) == FIELD_DECL && !DECL_UNNAMED_BIT_FIELD (field))
4848 offset_int fld = wi::to_offset (DECL_FIELD_OFFSET (field));
4849 offset_int bitpos = wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
4850 fld = fld * BITS_PER_UNIT + bitpos;
4851 if (cur != fld)
4852 return false;
4853 if (DECL_SIZE (field))
4855 offset_int size = wi::to_offset (DECL_SIZE (field));
4856 cur += size;
4859 if (cur != wi::to_offset (sz))
4860 return false;
4862 return true;
4865 /* Nonzero iff type T is a class template implicit specialization. */
4867 bool
4868 class_tmpl_impl_spec_p (const_tree t)
4870 return CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INSTANTIATION (t);
4873 /* Returns 1 iff zero initialization of type T means actually storing
4874 zeros in it. */
4877 zero_init_p (const_tree t)
4879 /* This CONST_CAST is okay because strip_array_types returns its
4880 argument unmodified and we assign it to a const_tree. */
4881 t = strip_array_types (CONST_CAST_TREE(t));
4883 if (t == error_mark_node)
4884 return 1;
4886 /* NULL pointers to data members are initialized with -1. */
4887 if (TYPE_PTRDATAMEM_P (t))
4888 return 0;
4890 /* Classes that contain types that can't be zero-initialized, cannot
4891 be zero-initialized themselves. */
4892 if (CLASS_TYPE_P (t) && CLASSTYPE_NON_ZERO_INIT_P (t))
4893 return 0;
4895 return 1;
4898 /* Returns true if the expression or initializer T is the result of
4899 zero-initialization for its type, taking pointers to members
4900 into consideration. */
4902 bool
4903 zero_init_expr_p (tree t)
4905 tree type = TREE_TYPE (t);
4906 if (!type || uses_template_parms (type))
4907 return false;
4908 if (TYPE_PTRMEM_P (type))
4909 return null_member_pointer_value_p (t);
4910 if (TREE_CODE (t) == CONSTRUCTOR)
4912 if (COMPOUND_LITERAL_P (t)
4913 || BRACE_ENCLOSED_INITIALIZER_P (t))
4914 /* Undigested, conversions might change the zeroness. */
4915 return false;
4916 for (constructor_elt &elt : CONSTRUCTOR_ELTS (t))
4918 if (TREE_CODE (type) == UNION_TYPE
4919 && elt.index != first_field (type))
4920 return false;
4921 if (!zero_init_expr_p (elt.value))
4922 return false;
4924 return true;
4926 if (zero_init_p (type))
4927 return initializer_zerop (t);
4928 return false;
4931 /* True IFF T is a C++20 structural type (P1907R1) that can be used as a
4932 non-type template parameter. If EXPLAIN, explain why not. */
4934 bool
4935 structural_type_p (tree t, bool explain)
4937 /* A structural type is one of the following: */
4939 /* a scalar type, or */
4940 if (SCALAR_TYPE_P (t))
4941 return true;
4942 /* an lvalue reference type, or */
4943 if (TYPE_REF_P (t) && !TYPE_REF_IS_RVALUE (t))
4944 return true;
4945 /* a literal class type with the following properties:
4946 - all base classes and non-static data members are public and non-mutable
4948 - the types of all bases classes and non-static data members are
4949 structural types or (possibly multi-dimensional) array thereof. */
4950 if (!CLASS_TYPE_P (t))
4951 return false;
4952 if (!literal_type_p (t))
4954 if (explain)
4955 explain_non_literal_class (t);
4956 return false;
4958 for (tree m = next_aggregate_field (TYPE_FIELDS (t)); m;
4959 m = next_aggregate_field (DECL_CHAIN (m)))
4961 if (TREE_PRIVATE (m) || TREE_PROTECTED (m))
4963 if (explain)
4965 if (DECL_FIELD_IS_BASE (m))
4966 inform (location_of (m), "base class %qT is not public",
4967 TREE_TYPE (m));
4968 else
4969 inform (location_of (m), "%qD is not public", m);
4971 return false;
4973 if (DECL_MUTABLE_P (m))
4975 if (explain)
4976 inform (location_of (m), "%qD is mutable", m);
4977 return false;
4979 tree mtype = strip_array_types (TREE_TYPE (m));
4980 if (!structural_type_p (mtype))
4982 if (explain)
4984 inform (location_of (m), "%qD has a non-structural type", m);
4985 structural_type_p (mtype, true);
4987 return false;
4990 return true;
4993 /* Partially handle the C++11 [[carries_dependency]] attribute.
4994 Just emit a different diagnostics when it is used on something the
4995 spec doesn't allow vs. where it allows and we just choose to ignore
4996 it. */
4998 static tree
4999 handle_carries_dependency_attribute (tree *node, tree name,
5000 tree ARG_UNUSED (args),
5001 int ARG_UNUSED (flags),
5002 bool *no_add_attrs)
5004 if (TREE_CODE (*node) != FUNCTION_DECL
5005 && TREE_CODE (*node) != PARM_DECL)
5007 warning (OPT_Wattributes, "%qE attribute can only be applied to "
5008 "functions or parameters", name);
5009 *no_add_attrs = true;
5011 else
5013 warning (OPT_Wattributes, "%qE attribute ignored", name);
5014 *no_add_attrs = true;
5016 return NULL_TREE;
5019 /* Handle the C++17 [[nodiscard]] attribute, which is similar to the GNU
5020 warn_unused_result attribute. */
5022 static tree
5023 handle_nodiscard_attribute (tree *node, tree name, tree args,
5024 int /*flags*/, bool *no_add_attrs)
5026 if (args && TREE_CODE (TREE_VALUE (args)) != STRING_CST)
5028 error ("%qE attribute argument must be a string constant", name);
5029 *no_add_attrs = true;
5031 if (TREE_CODE (*node) == FUNCTION_DECL)
5033 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node)))
5034 && !DECL_CONSTRUCTOR_P (*node))
5035 warning_at (DECL_SOURCE_LOCATION (*node),
5036 OPT_Wattributes, "%qE attribute applied to %qD with void "
5037 "return type", name, *node);
5039 else if (OVERLOAD_TYPE_P (*node))
5040 /* OK */;
5041 else
5043 warning (OPT_Wattributes, "%qE attribute can only be applied to "
5044 "functions or to class or enumeration types", name);
5045 *no_add_attrs = true;
5047 return NULL_TREE;
5050 /* Handle a C++20 "no_unique_address" attribute; arguments as in
5051 struct attribute_spec.handler. */
5052 static tree
5053 handle_no_unique_addr_attribute (tree* node,
5054 tree name,
5055 tree /*args*/,
5056 int /*flags*/,
5057 bool* no_add_attrs)
5059 if (TREE_CODE (*node) == VAR_DECL)
5061 DECL_MERGEABLE (*node) = true;
5062 if (pedantic)
5063 warning (OPT_Wattributes, "%qE attribute can only be applied to "
5064 "non-static data members", name);
5066 else if (TREE_CODE (*node) != FIELD_DECL)
5068 warning (OPT_Wattributes, "%qE attribute can only be applied to "
5069 "non-static data members", name);
5070 *no_add_attrs = true;
5072 else if (DECL_C_BIT_FIELD (*node))
5074 warning (OPT_Wattributes, "%qE attribute cannot be applied to "
5075 "a bit-field", name);
5076 *no_add_attrs = true;
5079 return NULL_TREE;
5082 /* The C++20 [[likely]] and [[unlikely]] attributes on labels map to the GNU
5083 hot/cold attributes. */
5085 static tree
5086 handle_likeliness_attribute (tree *node, tree name, tree args,
5087 int flags, bool *no_add_attrs)
5089 *no_add_attrs = true;
5090 if (TREE_CODE (*node) == LABEL_DECL
5091 || TREE_CODE (*node) == FUNCTION_DECL)
5093 if (args)
5094 warning (OPT_Wattributes, "%qE attribute takes no arguments", name);
5095 tree bname = (is_attribute_p ("likely", name)
5096 ? get_identifier ("hot") : get_identifier ("cold"));
5097 if (TREE_CODE (*node) == FUNCTION_DECL)
5098 warning (OPT_Wattributes, "ISO C++ %qE attribute does not apply to "
5099 "functions; treating as %<[[gnu::%E]]%>", name, bname);
5100 tree battr = build_tree_list (bname, NULL_TREE);
5101 decl_attributes (node, battr, flags);
5102 return NULL_TREE;
5104 else
5105 return error_mark_node;
5108 /* Table of valid C++ attributes. */
5109 static const attribute_spec cxx_gnu_attributes[] =
5111 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
5112 affects_type_identity, handler, exclude } */
5113 { "init_priority", 1, 1, true, false, false, false,
5114 handle_init_priority_attribute, NULL },
5115 { "abi_tag", 1, -1, false, false, false, true,
5116 handle_abi_tag_attribute, NULL },
5117 { "no_dangling", 0, 1, false, true, false, false,
5118 handle_no_dangling_attribute, NULL },
5121 const scoped_attribute_specs cxx_gnu_attribute_table =
5123 "gnu", { cxx_gnu_attributes }
5126 /* Table of C++ standard attributes. */
5127 static const attribute_spec std_attributes[] =
5129 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
5130 affects_type_identity, handler, exclude } */
5131 { "maybe_unused", 0, 0, false, false, false, false,
5132 handle_unused_attribute, NULL },
5133 { "nodiscard", 0, 1, false, false, false, false,
5134 handle_nodiscard_attribute, NULL },
5135 { "no_unique_address", 0, 0, true, false, false, false,
5136 handle_no_unique_addr_attribute, NULL },
5137 { "likely", 0, 0, false, false, false, false,
5138 handle_likeliness_attribute, attr_cold_hot_exclusions },
5139 { "unlikely", 0, 0, false, false, false, false,
5140 handle_likeliness_attribute, attr_cold_hot_exclusions },
5141 { "noreturn", 0, 0, true, false, false, false,
5142 handle_noreturn_attribute, attr_noreturn_exclusions },
5143 { "carries_dependency", 0, 0, true, false, false, false,
5144 handle_carries_dependency_attribute, NULL },
5145 { "pre", 0, -1, false, false, false, false,
5146 handle_contract_attribute, NULL },
5147 { "post", 0, -1, false, false, false, false,
5148 handle_contract_attribute, NULL }
5151 const scoped_attribute_specs std_attribute_table =
5153 nullptr, { std_attributes }
5156 /* Handle an "init_priority" attribute; arguments as in
5157 struct attribute_spec.handler. */
5158 static tree
5159 handle_init_priority_attribute (tree* node,
5160 tree name,
5161 tree args,
5162 int /*flags*/,
5163 bool* no_add_attrs)
5165 if (!SUPPORTS_INIT_PRIORITY)
5166 /* Treat init_priority as an unrecognized attribute (mirroring
5167 __has_attribute) if the target doesn't support init priorities. */
5168 return error_mark_node;
5170 tree initp_expr = TREE_VALUE (args);
5171 tree decl = *node;
5172 tree type = TREE_TYPE (decl);
5173 int pri;
5175 STRIP_NOPS (initp_expr);
5176 initp_expr = default_conversion (initp_expr);
5177 if (initp_expr)
5178 initp_expr = maybe_constant_value (initp_expr);
5180 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
5182 error ("requested %<init_priority%> is not an integer constant");
5183 cxx_constant_value (initp_expr);
5184 *no_add_attrs = true;
5185 return NULL_TREE;
5188 pri = TREE_INT_CST_LOW (initp_expr);
5190 type = strip_array_types (type);
5192 if (decl == NULL_TREE
5193 || !VAR_P (decl)
5194 || !TREE_STATIC (decl)
5195 || DECL_EXTERNAL (decl)
5196 || (TREE_CODE (type) != RECORD_TYPE
5197 && TREE_CODE (type) != UNION_TYPE)
5198 /* Static objects in functions are initialized the
5199 first time control passes through that
5200 function. This is not precise enough to pin down an
5201 init_priority value, so don't allow it. */
5202 || current_function_decl)
5204 error ("can only use %qE attribute on file-scope definitions "
5205 "of objects of class type", name);
5206 *no_add_attrs = true;
5207 return NULL_TREE;
5210 if (pri > MAX_INIT_PRIORITY || pri <= 0)
5212 error ("requested %<init_priority%> %i is out of range [0, %i]",
5213 pri, MAX_INIT_PRIORITY);
5214 *no_add_attrs = true;
5215 return NULL_TREE;
5218 /* Check for init_priorities that are reserved for
5219 language and runtime support implementations.*/
5220 if (pri <= MAX_RESERVED_INIT_PRIORITY)
5222 warning
5223 (0, "requested %<init_priority%> %i is reserved for internal use",
5224 pri);
5227 SET_DECL_INIT_PRIORITY (decl, pri);
5228 DECL_HAS_INIT_PRIORITY_P (decl) = 1;
5229 return NULL_TREE;
5232 /* DECL is being redeclared; the old declaration had the abi tags in OLD,
5233 and the new one has the tags in NEW_. Give an error if there are tags
5234 in NEW_ that weren't in OLD. */
5236 bool
5237 check_abi_tag_redeclaration (const_tree decl, const_tree old, const_tree new_)
5239 if (old && TREE_CODE (TREE_VALUE (old)) == TREE_LIST)
5240 old = TREE_VALUE (old);
5241 if (new_ && TREE_CODE (TREE_VALUE (new_)) == TREE_LIST)
5242 new_ = TREE_VALUE (new_);
5243 bool err = false;
5244 for (const_tree t = new_; t; t = TREE_CHAIN (t))
5246 tree str = TREE_VALUE (t);
5247 for (const_tree in = old; in; in = TREE_CHAIN (in))
5249 tree ostr = TREE_VALUE (in);
5250 if (cp_tree_equal (str, ostr))
5251 goto found;
5253 error ("redeclaration of %qD adds abi tag %qE", decl, str);
5254 err = true;
5255 found:;
5257 if (err)
5259 inform (DECL_SOURCE_LOCATION (decl), "previous declaration here");
5260 return false;
5262 return true;
5265 /* The abi_tag attribute with the name NAME was given ARGS. If they are
5266 ill-formed, give an error and return false; otherwise, return true. */
5268 bool
5269 check_abi_tag_args (tree args, tree name)
5271 if (!args)
5273 error ("the %qE attribute requires arguments", name);
5274 return false;
5276 for (tree arg = args; arg; arg = TREE_CHAIN (arg))
5278 tree elt = TREE_VALUE (arg);
5279 if (TREE_CODE (elt) != STRING_CST
5280 || (!same_type_ignoring_top_level_qualifiers_p
5281 (strip_array_types (TREE_TYPE (elt)),
5282 char_type_node)))
5284 error ("arguments to the %qE attribute must be narrow string "
5285 "literals", name);
5286 return false;
5288 const char *begin = TREE_STRING_POINTER (elt);
5289 const char *end = begin + TREE_STRING_LENGTH (elt);
5290 for (const char *p = begin; p != end; ++p)
5292 char c = *p;
5293 if (p == begin)
5295 if (!ISALPHA (c) && c != '_')
5297 error ("arguments to the %qE attribute must contain valid "
5298 "identifiers", name);
5299 inform (input_location, "%<%c%> is not a valid first "
5300 "character for an identifier", c);
5301 return false;
5304 else if (p == end - 1)
5305 gcc_assert (c == 0);
5306 else
5308 if (!ISALNUM (c) && c != '_')
5310 error ("arguments to the %qE attribute must contain valid "
5311 "identifiers", name);
5312 inform (input_location, "%<%c%> is not a valid character "
5313 "in an identifier", c);
5314 return false;
5319 return true;
5322 /* Handle an "abi_tag" attribute; arguments as in
5323 struct attribute_spec.handler. */
5325 static tree
5326 handle_abi_tag_attribute (tree* node, tree name, tree args,
5327 int flags, bool* no_add_attrs)
5329 if (!check_abi_tag_args (args, name))
5330 goto fail;
5332 if (TYPE_P (*node))
5334 if (!OVERLOAD_TYPE_P (*node))
5336 error ("%qE attribute applied to non-class, non-enum type %qT",
5337 name, *node);
5338 goto fail;
5340 else if (!(flags & (int)ATTR_FLAG_TYPE_IN_PLACE))
5342 error ("%qE attribute applied to %qT after its definition",
5343 name, *node);
5344 goto fail;
5346 else if (CLASS_TYPE_P (*node)
5347 && CLASSTYPE_TEMPLATE_INSTANTIATION (*node))
5349 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
5350 "template instantiation %qT", name, *node);
5351 goto fail;
5353 else if (CLASS_TYPE_P (*node)
5354 && CLASSTYPE_TEMPLATE_SPECIALIZATION (*node))
5356 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
5357 "template specialization %qT", name, *node);
5358 goto fail;
5361 tree attributes = TYPE_ATTRIBUTES (*node);
5362 tree decl = TYPE_NAME (*node);
5364 /* Make sure all declarations have the same abi tags. */
5365 if (DECL_SOURCE_LOCATION (decl) != input_location)
5367 if (!check_abi_tag_redeclaration (decl,
5368 lookup_attribute ("abi_tag",
5369 attributes),
5370 args))
5371 goto fail;
5374 else
5376 if (!VAR_OR_FUNCTION_DECL_P (*node))
5378 error ("%qE attribute applied to non-function, non-variable %qD",
5379 name, *node);
5380 goto fail;
5382 else if (DECL_LANGUAGE (*node) == lang_c)
5384 error ("%qE attribute applied to extern \"C\" declaration %qD",
5385 name, *node);
5386 goto fail;
5390 return NULL_TREE;
5392 fail:
5393 *no_add_attrs = true;
5394 return NULL_TREE;
5397 /* Perform checking for contract attributes. */
5399 tree
5400 handle_contract_attribute (tree *ARG_UNUSED (node), tree ARG_UNUSED (name),
5401 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
5402 bool *ARG_UNUSED (no_add_attrs))
5404 /* TODO: Is there any checking we could do here? */
5405 return NULL_TREE;
5408 /* Handle a "no_dangling" attribute; arguments as in
5409 struct attribute_spec.handler. */
5411 tree
5412 handle_no_dangling_attribute (tree *node, tree name, tree args, int,
5413 bool *no_add_attrs)
5415 if (args && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
5417 error ("%qE attribute argument must be an expression that evaluates "
5418 "to true or false", name);
5419 *no_add_attrs = true;
5421 else if (!FUNC_OR_METHOD_TYPE_P (*node)
5422 && !RECORD_OR_UNION_TYPE_P (*node))
5424 warning (OPT_Wattributes, "%qE attribute ignored", name);
5425 *no_add_attrs = true;
5428 return NULL_TREE;
5431 /* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the
5432 thing pointed to by the constant. */
5434 tree
5435 make_ptrmem_cst (tree type, tree member)
5437 tree ptrmem_cst = make_node (PTRMEM_CST);
5438 TREE_TYPE (ptrmem_cst) = type;
5439 PTRMEM_CST_MEMBER (ptrmem_cst) = member;
5440 PTRMEM_CST_LOCATION (ptrmem_cst) = input_location;
5441 return ptrmem_cst;
5444 /* Build a variant of TYPE that has the indicated ATTRIBUTES. May
5445 return an existing type if an appropriate type already exists. */
5447 tree
5448 cp_build_type_attribute_variant (tree type, tree attributes)
5450 tree new_type;
5452 new_type = build_type_attribute_variant (type, attributes);
5453 if (FUNC_OR_METHOD_TYPE_P (new_type))
5454 gcc_checking_assert (cxx_type_hash_eq (type, new_type));
5456 /* Making a new main variant of a class type is broken. */
5457 gcc_assert (!CLASS_TYPE_P (type) || new_type == type);
5459 return new_type;
5462 /* Return TRUE if TYPE1 and TYPE2 are identical for type hashing purposes.
5463 Called only after doing all language independent checks. */
5465 bool
5466 cxx_type_hash_eq (const_tree typea, const_tree typeb)
5468 gcc_assert (FUNC_OR_METHOD_TYPE_P (typea));
5470 if (type_memfn_rqual (typea) != type_memfn_rqual (typeb))
5471 return false;
5472 if (TYPE_HAS_LATE_RETURN_TYPE (typea) != TYPE_HAS_LATE_RETURN_TYPE (typeb))
5473 return false;
5474 return comp_except_specs (TYPE_RAISES_EXCEPTIONS (typea),
5475 TYPE_RAISES_EXCEPTIONS (typeb), ce_exact);
5478 /* Copy the language-specific type variant modifiers from TYPEB to TYPEA. For
5479 C++, these are the exception-specifier and ref-qualifier. */
5481 tree
5482 cxx_copy_lang_qualifiers (const_tree typea, const_tree typeb)
5484 tree type = CONST_CAST_TREE (typea);
5485 if (FUNC_OR_METHOD_TYPE_P (type))
5486 type = build_cp_fntype_variant (type, type_memfn_rqual (typeb),
5487 TYPE_RAISES_EXCEPTIONS (typeb),
5488 TYPE_HAS_LATE_RETURN_TYPE (typeb));
5489 return type;
5492 /* Apply FUNC to all language-specific sub-trees of TP in a pre-order
5493 traversal. Called from walk_tree. */
5495 tree
5496 cp_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func,
5497 void *data, hash_set<tree> *pset)
5499 tree t = *tp;
5500 enum tree_code code = TREE_CODE (t);
5501 tree result;
5503 #define WALK_SUBTREE(NODE) \
5504 do \
5506 result = cp_walk_tree (&(NODE), func, data, pset); \
5507 if (result) goto out; \
5509 while (0)
5511 if (TYPE_P (t))
5513 /* If *WALK_SUBTREES_P is 1, we're interested in the syntactic form of
5514 the argument, so don't look through typedefs, but do walk into
5515 template arguments for alias templates (and non-typedefed classes).
5517 If *WALK_SUBTREES_P > 1, we're interested in type identity or
5518 equivalence, so look through typedefs, ignoring template arguments for
5519 alias templates, and walk into template args of classes.
5521 See find_abi_tags_r for an example of setting *WALK_SUBTREES_P to 2
5522 when that's the behavior the walk_tree_fn wants. */
5523 if (*walk_subtrees_p == 1 && typedef_variant_p (t))
5525 if (tree ti = TYPE_ALIAS_TEMPLATE_INFO (t))
5526 WALK_SUBTREE (TI_ARGS (ti));
5527 *walk_subtrees_p = 0;
5528 return NULL_TREE;
5531 if (tree ti = TYPE_TEMPLATE_INFO (t))
5532 WALK_SUBTREE (TI_ARGS (ti));
5535 /* Not one of the easy cases. We must explicitly go through the
5536 children. */
5537 result = NULL_TREE;
5538 switch (code)
5540 case TEMPLATE_TYPE_PARM:
5541 if (template_placeholder_p (t))
5542 WALK_SUBTREE (CLASS_PLACEHOLDER_TEMPLATE (t));
5543 /* Fall through. */
5544 case DEFERRED_PARSE:
5545 case TEMPLATE_TEMPLATE_PARM:
5546 case BOUND_TEMPLATE_TEMPLATE_PARM:
5547 case UNBOUND_CLASS_TEMPLATE:
5548 case TEMPLATE_PARM_INDEX:
5549 case TYPEOF_TYPE:
5550 /* None of these have subtrees other than those already walked
5551 above. */
5552 *walk_subtrees_p = 0;
5553 break;
5555 case TYPENAME_TYPE:
5556 WALK_SUBTREE (TYPE_CONTEXT (t));
5557 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
5558 *walk_subtrees_p = 0;
5559 break;
5561 case BASELINK:
5562 if (BASELINK_QUALIFIED_P (t))
5563 WALK_SUBTREE (BINFO_TYPE (BASELINK_ACCESS_BINFO (t)));
5564 WALK_SUBTREE (BASELINK_FUNCTIONS (t));
5565 *walk_subtrees_p = 0;
5566 break;
5568 case PTRMEM_CST:
5569 WALK_SUBTREE (TREE_TYPE (t));
5570 *walk_subtrees_p = 0;
5571 break;
5573 case TREE_LIST:
5574 WALK_SUBTREE (TREE_PURPOSE (t));
5575 break;
5577 case OVERLOAD:
5578 WALK_SUBTREE (OVL_FUNCTION (t));
5579 WALK_SUBTREE (OVL_CHAIN (t));
5580 *walk_subtrees_p = 0;
5581 break;
5583 case USING_DECL:
5584 WALK_SUBTREE (DECL_NAME (t));
5585 WALK_SUBTREE (USING_DECL_SCOPE (t));
5586 WALK_SUBTREE (USING_DECL_DECLS (t));
5587 *walk_subtrees_p = 0;
5588 break;
5590 case RECORD_TYPE:
5591 if (TYPE_PTRMEMFUNC_P (t))
5592 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (t));
5593 break;
5595 case TYPE_ARGUMENT_PACK:
5596 case NONTYPE_ARGUMENT_PACK:
5598 tree args = ARGUMENT_PACK_ARGS (t);
5599 for (tree arg : tree_vec_range (args))
5600 WALK_SUBTREE (arg);
5602 break;
5604 case TYPE_PACK_EXPANSION:
5605 WALK_SUBTREE (TREE_TYPE (t));
5606 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (t));
5607 *walk_subtrees_p = 0;
5608 break;
5610 case EXPR_PACK_EXPANSION:
5611 WALK_SUBTREE (TREE_OPERAND (t, 0));
5612 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (t));
5613 *walk_subtrees_p = 0;
5614 break;
5616 case CAST_EXPR:
5617 case REINTERPRET_CAST_EXPR:
5618 case STATIC_CAST_EXPR:
5619 case CONST_CAST_EXPR:
5620 case DYNAMIC_CAST_EXPR:
5621 case IMPLICIT_CONV_EXPR:
5622 case BIT_CAST_EXPR:
5623 if (TREE_TYPE (t))
5624 WALK_SUBTREE (TREE_TYPE (t));
5625 break;
5627 case CONSTRUCTOR:
5628 if (COMPOUND_LITERAL_P (t))
5629 WALK_SUBTREE (TREE_TYPE (t));
5630 break;
5632 case TRAIT_EXPR:
5633 WALK_SUBTREE (TRAIT_EXPR_TYPE1 (t));
5634 WALK_SUBTREE (TRAIT_EXPR_TYPE2 (t));
5635 *walk_subtrees_p = 0;
5636 break;
5638 case TRAIT_TYPE:
5639 WALK_SUBTREE (TRAIT_TYPE_TYPE1 (t));
5640 WALK_SUBTREE (TRAIT_TYPE_TYPE2 (t));
5641 *walk_subtrees_p = 0;
5642 break;
5644 case DECLTYPE_TYPE:
5646 cp_unevaluated u;
5647 WALK_SUBTREE (DECLTYPE_TYPE_EXPR (t));
5648 *walk_subtrees_p = 0;
5649 break;
5652 case ALIGNOF_EXPR:
5653 case SIZEOF_EXPR:
5654 case NOEXCEPT_EXPR:
5656 cp_unevaluated u;
5657 WALK_SUBTREE (TREE_OPERAND (t, 0));
5658 *walk_subtrees_p = 0;
5659 break;
5662 case REQUIRES_EXPR:
5664 cp_unevaluated u;
5665 for (tree parm = REQUIRES_EXPR_PARMS (t); parm; parm = DECL_CHAIN (parm))
5666 /* Walk the types of each parameter, but not the parameter itself,
5667 since doing so would cause false positives in the unexpanded pack
5668 checker if the requires-expr introduces a function parameter pack,
5669 e.g. requires (Ts... ts) { }. */
5670 WALK_SUBTREE (TREE_TYPE (parm));
5671 WALK_SUBTREE (REQUIRES_EXPR_REQS (t));
5672 *walk_subtrees_p = 0;
5673 break;
5676 case DECL_EXPR:
5677 /* User variables should be mentioned in BIND_EXPR_VARS
5678 and their initializers and sizes walked when walking
5679 the containing BIND_EXPR. Compiler temporaries are
5680 handled here. And also normal variables in templates,
5681 since do_poplevel doesn't build a BIND_EXPR then. */
5682 if (VAR_P (TREE_OPERAND (t, 0))
5683 && (processing_template_decl
5684 || (DECL_ARTIFICIAL (TREE_OPERAND (t, 0))
5685 && !TREE_STATIC (TREE_OPERAND (t, 0)))))
5687 tree decl = TREE_OPERAND (t, 0);
5688 WALK_SUBTREE (DECL_INITIAL (decl));
5689 WALK_SUBTREE (DECL_SIZE (decl));
5690 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
5692 break;
5694 case LAMBDA_EXPR:
5695 /* Don't walk into the body of the lambda, but the capture initializers
5696 are part of the enclosing context. */
5697 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
5698 cap = TREE_CHAIN (cap))
5699 WALK_SUBTREE (TREE_VALUE (cap));
5700 break;
5702 case CO_YIELD_EXPR:
5703 if (TREE_OPERAND (t, 1))
5704 /* Operand 1 is the tree for the relevant co_await which has any
5705 interesting sub-trees. */
5706 WALK_SUBTREE (TREE_OPERAND (t, 1));
5707 break;
5709 case CO_AWAIT_EXPR:
5710 if (TREE_OPERAND (t, 1))
5711 /* Operand 1 is frame variable. */
5712 WALK_SUBTREE (TREE_OPERAND (t, 1));
5713 if (TREE_OPERAND (t, 2))
5714 /* Operand 2 has the initialiser, and we need to walk any subtrees
5715 there. */
5716 WALK_SUBTREE (TREE_OPERAND (t, 2));
5717 break;
5719 case CO_RETURN_EXPR:
5720 if (TREE_OPERAND (t, 0))
5722 if (VOID_TYPE_P (TREE_OPERAND (t, 0)))
5723 /* For void expressions, operand 1 is a trivial call, and any
5724 interesting subtrees will be part of operand 0. */
5725 WALK_SUBTREE (TREE_OPERAND (t, 0));
5726 else if (TREE_OPERAND (t, 1))
5727 /* Interesting sub-trees will be in the return_value () call
5728 arguments. */
5729 WALK_SUBTREE (TREE_OPERAND (t, 1));
5731 break;
5733 case STATIC_ASSERT:
5734 WALK_SUBTREE (STATIC_ASSERT_CONDITION (t));
5735 WALK_SUBTREE (STATIC_ASSERT_MESSAGE (t));
5736 break;
5738 default:
5739 return NULL_TREE;
5742 /* We didn't find what we were looking for. */
5743 out:
5744 return result;
5746 #undef WALK_SUBTREE
5749 /* Like save_expr, but for C++. */
5751 tree
5752 cp_save_expr (tree expr)
5754 /* There is no reason to create a SAVE_EXPR within a template; if
5755 needed, we can create the SAVE_EXPR when instantiating the
5756 template. Furthermore, the middle-end cannot handle C++-specific
5757 tree codes. */
5758 if (processing_template_decl)
5759 return expr;
5761 /* TARGET_EXPRs are only expanded once. */
5762 if (TREE_CODE (expr) == TARGET_EXPR)
5763 return expr;
5765 return save_expr (expr);
5768 /* Initialize tree.cc. */
5770 void
5771 init_tree (void)
5773 list_hash_table = hash_table<list_hasher>::create_ggc (61);
5776 /* Returns the kind of special function that DECL (a FUNCTION_DECL)
5777 is. Note that sfk_none is zero, so this function can be used as a
5778 predicate to test whether or not DECL is a special function. */
5780 special_function_kind
5781 special_function_p (const_tree decl)
5783 /* Rather than doing all this stuff with magic names, we should
5784 probably have a field of type `special_function_kind' in
5785 DECL_LANG_SPECIFIC. */
5786 if (DECL_INHERITED_CTOR (decl))
5787 return sfk_inheriting_constructor;
5788 if (DECL_COPY_CONSTRUCTOR_P (decl))
5789 return sfk_copy_constructor;
5790 if (DECL_MOVE_CONSTRUCTOR_P (decl))
5791 return sfk_move_constructor;
5792 if (DECL_CONSTRUCTOR_P (decl))
5793 return sfk_constructor;
5794 if (DECL_ASSIGNMENT_OPERATOR_P (decl)
5795 && DECL_OVERLOADED_OPERATOR_IS (decl, NOP_EXPR))
5797 if (copy_fn_p (decl))
5798 return sfk_copy_assignment;
5799 if (move_fn_p (decl))
5800 return sfk_move_assignment;
5802 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
5803 return sfk_destructor;
5804 if (DECL_COMPLETE_DESTRUCTOR_P (decl))
5805 return sfk_complete_destructor;
5806 if (DECL_BASE_DESTRUCTOR_P (decl))
5807 return sfk_base_destructor;
5808 if (DECL_DELETING_DESTRUCTOR_P (decl))
5809 return sfk_deleting_destructor;
5810 if (DECL_CONV_FN_P (decl))
5811 return sfk_conversion;
5812 if (deduction_guide_p (decl))
5813 return sfk_deduction_guide;
5814 if (DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) >= OVL_OP_EQ_EXPR
5815 && DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) <= OVL_OP_SPACESHIP_EXPR)
5816 return sfk_comparison;
5818 return sfk_none;
5821 /* As above, but only if DECL is a special member function as per 11.3.3
5822 [special]: default/copy/move ctor, copy/move assignment, or destructor. */
5824 special_function_kind
5825 special_memfn_p (const_tree decl)
5827 switch (special_function_kind sfk = special_function_p (decl))
5829 case sfk_constructor:
5830 if (!default_ctor_p (decl))
5831 break;
5832 gcc_fallthrough();
5833 case sfk_copy_constructor:
5834 case sfk_copy_assignment:
5835 case sfk_move_assignment:
5836 case sfk_move_constructor:
5837 case sfk_destructor:
5838 return sfk;
5840 default:
5841 break;
5843 return sfk_none;
5846 /* Returns nonzero if TYPE is a character type, including wchar_t. */
5849 char_type_p (tree type)
5851 return (same_type_p (type, char_type_node)
5852 || same_type_p (type, unsigned_char_type_node)
5853 || same_type_p (type, signed_char_type_node)
5854 || same_type_p (type, char8_type_node)
5855 || same_type_p (type, char16_type_node)
5856 || same_type_p (type, char32_type_node)
5857 || same_type_p (type, wchar_type_node));
5860 /* Returns the kind of linkage associated with the indicated DECL. Th
5861 value returned is as specified by the language standard; it is
5862 independent of implementation details regarding template
5863 instantiation, etc. For example, it is possible that a declaration
5864 to which this function assigns external linkage would not show up
5865 as a global symbol when you run `nm' on the resulting object file. */
5867 linkage_kind
5868 decl_linkage (tree decl)
5870 /* This function doesn't attempt to calculate the linkage from first
5871 principles as given in [basic.link]. Instead, it makes use of
5872 the fact that we have already set TREE_PUBLIC appropriately, and
5873 then handles a few special cases. Ideally, we would calculate
5874 linkage first, and then transform that into a concrete
5875 implementation. */
5877 /* Things that don't have names have no linkage. */
5878 if (!DECL_NAME (decl))
5879 return lk_none;
5881 /* Fields have no linkage. */
5882 if (TREE_CODE (decl) == FIELD_DECL)
5883 return lk_none;
5885 /* Things in local scope do not have linkage. */
5886 if (decl_function_context (decl))
5887 return lk_none;
5889 /* Things that are TREE_PUBLIC have external linkage. */
5890 if (TREE_PUBLIC (decl))
5891 return lk_external;
5893 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor variants,
5894 check one of the "clones" for the real linkage. */
5895 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl)
5896 && DECL_CHAIN (decl)
5897 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)))
5898 return decl_linkage (DECL_CHAIN (decl));
5900 if (TREE_CODE (decl) == NAMESPACE_DECL)
5901 return lk_external;
5903 /* Linkage of a CONST_DECL depends on the linkage of the enumeration
5904 type. */
5905 if (TREE_CODE (decl) == CONST_DECL)
5906 return decl_linkage (TYPE_NAME (DECL_CONTEXT (decl)));
5908 /* Members of the anonymous namespace also have TREE_PUBLIC unset, but
5909 are considered to have external linkage for language purposes, as do
5910 template instantiations on targets without weak symbols. DECLs really
5911 meant to have internal linkage have DECL_THIS_STATIC set. */
5912 if (TREE_CODE (decl) == TYPE_DECL)
5913 return lk_external;
5914 if (VAR_OR_FUNCTION_DECL_P (decl))
5916 if (!DECL_THIS_STATIC (decl))
5917 return lk_external;
5919 /* Static data members and static member functions from classes
5920 in anonymous namespace also don't have TREE_PUBLIC set. */
5921 if (DECL_CLASS_CONTEXT (decl))
5922 return lk_external;
5925 /* Everything else has internal linkage. */
5926 return lk_internal;
5929 /* Returns the storage duration of the object or reference associated with
5930 the indicated DECL, which should be a VAR_DECL or PARM_DECL. */
5932 duration_kind
5933 decl_storage_duration (tree decl)
5935 if (TREE_CODE (decl) == PARM_DECL)
5936 return dk_auto;
5937 if (TREE_CODE (decl) == FUNCTION_DECL)
5938 return dk_static;
5939 gcc_assert (VAR_P (decl));
5940 if (!TREE_STATIC (decl)
5941 && !DECL_EXTERNAL (decl))
5942 return dk_auto;
5943 if (CP_DECL_THREAD_LOCAL_P (decl))
5944 return dk_thread;
5945 return dk_static;
5948 /* EXP is an expression that we want to pre-evaluate. Returns (in
5949 *INITP) an expression that will perform the pre-evaluation. The
5950 value returned by this function is a side-effect free expression
5951 equivalent to the pre-evaluated expression. Callers must ensure
5952 that *INITP is evaluated before EXP. */
5954 tree
5955 stabilize_expr (tree exp, tree* initp)
5957 tree init_expr;
5959 if (!TREE_SIDE_EFFECTS (exp))
5960 init_expr = NULL_TREE;
5961 else if (VOID_TYPE_P (TREE_TYPE (exp)))
5963 init_expr = exp;
5964 exp = void_node;
5966 /* There are no expressions with REFERENCE_TYPE, but there can be call
5967 arguments with such a type; just treat it as a pointer. */
5968 else if (TYPE_REF_P (TREE_TYPE (exp))
5969 || SCALAR_TYPE_P (TREE_TYPE (exp))
5970 || !glvalue_p (exp))
5972 init_expr = get_target_expr (exp);
5973 exp = TARGET_EXPR_SLOT (init_expr);
5974 if (CLASS_TYPE_P (TREE_TYPE (exp)))
5975 exp = move (exp);
5976 else
5977 exp = rvalue (exp);
5979 else
5981 bool xval = !lvalue_p (exp);
5982 exp = cp_build_addr_expr (exp, tf_warning_or_error);
5983 init_expr = get_target_expr (exp);
5984 exp = TARGET_EXPR_SLOT (init_expr);
5985 exp = cp_build_fold_indirect_ref (exp);
5986 if (xval)
5987 exp = move (exp);
5989 *initp = init_expr;
5991 gcc_assert (!TREE_SIDE_EFFECTS (exp));
5992 return exp;
5995 /* Add NEW_EXPR, an expression whose value we don't care about, after the
5996 similar expression ORIG. */
5998 tree
5999 add_stmt_to_compound (tree orig, tree new_expr)
6001 if (!new_expr || !TREE_SIDE_EFFECTS (new_expr))
6002 return orig;
6003 if (!orig || !TREE_SIDE_EFFECTS (orig))
6004 return new_expr;
6005 return build2 (COMPOUND_EXPR, void_type_node, orig, new_expr);
6008 /* Like stabilize_expr, but for a call whose arguments we want to
6009 pre-evaluate. CALL is modified in place to use the pre-evaluated
6010 arguments, while, upon return, *INITP contains an expression to
6011 compute the arguments. */
6013 void
6014 stabilize_call (tree call, tree *initp)
6016 tree inits = NULL_TREE;
6017 int i;
6018 int nargs = call_expr_nargs (call);
6020 if (call == error_mark_node || processing_template_decl)
6022 *initp = NULL_TREE;
6023 return;
6026 gcc_assert (TREE_CODE (call) == CALL_EXPR);
6028 for (i = 0; i < nargs; i++)
6030 tree init;
6031 CALL_EXPR_ARG (call, i) =
6032 stabilize_expr (CALL_EXPR_ARG (call, i), &init);
6033 inits = add_stmt_to_compound (inits, init);
6036 *initp = inits;
6039 /* Like stabilize_expr, but for an AGGR_INIT_EXPR whose arguments we want
6040 to pre-evaluate. CALL is modified in place to use the pre-evaluated
6041 arguments, while, upon return, *INITP contains an expression to
6042 compute the arguments. */
6044 static void
6045 stabilize_aggr_init (tree call, tree *initp)
6047 tree inits = NULL_TREE;
6048 int i;
6049 int nargs = aggr_init_expr_nargs (call);
6051 if (call == error_mark_node)
6052 return;
6054 gcc_assert (TREE_CODE (call) == AGGR_INIT_EXPR);
6056 for (i = 0; i < nargs; i++)
6058 tree init;
6059 AGGR_INIT_EXPR_ARG (call, i) =
6060 stabilize_expr (AGGR_INIT_EXPR_ARG (call, i), &init);
6061 inits = add_stmt_to_compound (inits, init);
6064 *initp = inits;
6067 /* Like stabilize_expr, but for an initialization.
6069 If the initialization is for an object of class type, this function
6070 takes care not to introduce additional temporaries.
6072 Returns TRUE iff the expression was successfully pre-evaluated,
6073 i.e., if INIT is now side-effect free, except for, possibly, a
6074 single call to a constructor. */
6076 bool
6077 stabilize_init (tree init, tree *initp)
6079 tree t = init;
6081 *initp = NULL_TREE;
6083 if (t == error_mark_node || processing_template_decl)
6084 return true;
6086 if (TREE_CODE (t) == INIT_EXPR)
6087 t = TREE_OPERAND (t, 1);
6088 if (TREE_CODE (t) == TARGET_EXPR)
6089 t = TARGET_EXPR_INITIAL (t);
6091 /* If the RHS can be stabilized without breaking copy elision, stabilize
6092 it. We specifically don't stabilize class prvalues here because that
6093 would mean an extra copy, but they might be stabilized below. */
6094 if (TREE_CODE (init) == INIT_EXPR
6095 && TREE_CODE (t) != CONSTRUCTOR
6096 && TREE_CODE (t) != AGGR_INIT_EXPR
6097 && (SCALAR_TYPE_P (TREE_TYPE (t))
6098 || glvalue_p (t)))
6100 TREE_OPERAND (init, 1) = stabilize_expr (t, initp);
6101 return true;
6104 if (TREE_CODE (t) == COMPOUND_EXPR
6105 && TREE_CODE (init) == INIT_EXPR)
6107 tree last = expr_last (t);
6108 /* Handle stabilizing the EMPTY_CLASS_EXPR pattern. */
6109 if (!TREE_SIDE_EFFECTS (last))
6111 *initp = t;
6112 TREE_OPERAND (init, 1) = last;
6113 return true;
6117 if (TREE_CODE (t) == CONSTRUCTOR)
6119 /* Aggregate initialization: stabilize each of the field
6120 initializers. */
6121 unsigned i;
6122 constructor_elt *ce;
6123 bool good = true;
6124 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
6125 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
6127 tree type = TREE_TYPE (ce->value);
6128 tree subinit;
6129 if (TYPE_REF_P (type)
6130 || SCALAR_TYPE_P (type))
6131 ce->value = stabilize_expr (ce->value, &subinit);
6132 else if (!stabilize_init (ce->value, &subinit))
6133 good = false;
6134 *initp = add_stmt_to_compound (*initp, subinit);
6136 return good;
6139 if (TREE_CODE (t) == CALL_EXPR)
6141 stabilize_call (t, initp);
6142 return true;
6145 if (TREE_CODE (t) == AGGR_INIT_EXPR)
6147 stabilize_aggr_init (t, initp);
6148 return true;
6151 /* The initialization is being performed via a bitwise copy -- and
6152 the item copied may have side effects. */
6153 return !TREE_SIDE_EFFECTS (init);
6156 /* Returns true if a cast to TYPE may appear in an integral constant
6157 expression. */
6159 bool
6160 cast_valid_in_integral_constant_expression_p (tree type)
6162 return (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6163 || cxx_dialect >= cxx11
6164 || dependent_type_p (type)
6165 || type == error_mark_node);
6168 /* Return true if we need to fix linkage information of DECL. */
6170 static bool
6171 cp_fix_function_decl_p (tree decl)
6173 /* Skip if DECL is not externally visible. */
6174 if (!TREE_PUBLIC (decl))
6175 return false;
6177 /* We need to fix DECL if it a appears to be exported but with no
6178 function body. Thunks do not have CFGs and we may need to
6179 handle them specially later. */
6180 if (!gimple_has_body_p (decl)
6181 && !DECL_THUNK_P (decl)
6182 && !DECL_EXTERNAL (decl))
6184 struct cgraph_node *node = cgraph_node::get (decl);
6186 /* Don't fix same_body aliases. Although they don't have their own
6187 CFG, they share it with what they alias to. */
6188 if (!node || !node->alias || !node->num_references ())
6189 return true;
6192 return false;
6195 /* Clean the C++ specific parts of the tree T. */
6197 void
6198 cp_free_lang_data (tree t)
6200 if (FUNC_OR_METHOD_TYPE_P (t))
6202 /* Default args are not interesting anymore. */
6203 tree argtypes = TYPE_ARG_TYPES (t);
6204 while (argtypes)
6206 TREE_PURPOSE (argtypes) = 0;
6207 argtypes = TREE_CHAIN (argtypes);
6210 else if (TREE_CODE (t) == FUNCTION_DECL
6211 && cp_fix_function_decl_p (t))
6213 /* If T is used in this translation unit at all, the definition
6214 must exist somewhere else since we have decided to not emit it
6215 in this TU. So make it an external reference. */
6216 DECL_EXTERNAL (t) = 1;
6217 TREE_STATIC (t) = 0;
6219 if (TREE_CODE (t) == NAMESPACE_DECL)
6220 /* We do not need the leftover chaining of namespaces from the
6221 binding level. */
6222 DECL_CHAIN (t) = NULL_TREE;
6225 /* Stub for c-common. Please keep in sync with c-decl.cc.
6226 FIXME: If address space support is target specific, then this
6227 should be a C target hook. But currently this is not possible,
6228 because this function is called via REGISTER_TARGET_PRAGMAS. */
6229 void
6230 c_register_addr_space (const char * /*word*/, addr_space_t /*as*/)
6234 /* Return the number of operands in T that we care about for things like
6235 mangling. */
6238 cp_tree_operand_length (const_tree t)
6240 enum tree_code code = TREE_CODE (t);
6242 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
6243 return VL_EXP_OPERAND_LENGTH (t);
6245 return cp_tree_code_length (code);
6248 /* Like cp_tree_operand_length, but takes a tree_code CODE. */
6251 cp_tree_code_length (enum tree_code code)
6253 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
6255 switch (code)
6257 case PREINCREMENT_EXPR:
6258 case PREDECREMENT_EXPR:
6259 case POSTINCREMENT_EXPR:
6260 case POSTDECREMENT_EXPR:
6261 return 1;
6263 case ARRAY_REF:
6264 return 2;
6266 case EXPR_PACK_EXPANSION:
6267 return 1;
6269 default:
6270 return TREE_CODE_LENGTH (code);
6274 /* Implement -Wzero_as_null_pointer_constant. Return true if the
6275 conditions for the warning hold, false otherwise. */
6276 bool
6277 maybe_warn_zero_as_null_pointer_constant (tree expr, location_t loc)
6279 if (c_inhibit_evaluation_warnings == 0
6280 && !null_node_p (expr) && !NULLPTR_TYPE_P (TREE_TYPE (expr)))
6282 warning_at (loc, OPT_Wzero_as_null_pointer_constant,
6283 "zero as null pointer constant");
6284 return true;
6286 return false;
6289 /* FNDECL is a function declaration whose type may have been altered by
6290 adding extra parameters such as this, in-charge, or VTT. When this
6291 takes place, the positional arguments supplied by the user (as in the
6292 'format' attribute arguments) may refer to the wrong argument. This
6293 function returns an integer indicating how many arguments should be
6294 skipped. */
6297 maybe_adjust_arg_pos_for_attribute (const_tree fndecl)
6299 if (!fndecl)
6300 return 0;
6301 int n = num_artificial_parms_for (fndecl);
6302 /* The manual states that it's the user's responsibility to account
6303 for the implicit this parameter. */
6304 return n > 0 ? n - 1 : 0;
6308 /* Release memory we no longer need after parsing. */
6309 void
6310 cp_tree_c_finish_parsing ()
6312 if (previous_class_level)
6313 invalidate_class_lookup_cache ();
6314 deleted_copy_types = NULL;
6317 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
6318 /* Complain that some language-specific thing hanging off a tree
6319 node has been accessed improperly. */
6321 void
6322 lang_check_failed (const char* file, int line, const char* function)
6324 internal_error ("%<lang_*%> check: failed in %s, at %s:%d",
6325 function, trim_filename (file), line);
6327 #endif /* ENABLE_TREE_CHECKING */
6329 #if CHECKING_P
6331 namespace selftest {
6333 /* Verify that lvalue_kind () works, for various expressions,
6334 and that location wrappers don't affect the results. */
6336 static void
6337 test_lvalue_kind ()
6339 location_t loc = BUILTINS_LOCATION;
6341 /* Verify constants and parameters, without and with
6342 location wrappers. */
6343 tree int_cst = build_int_cst (integer_type_node, 42);
6344 ASSERT_EQ (clk_none, lvalue_kind (int_cst));
6346 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
6347 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
6348 ASSERT_EQ (clk_none, lvalue_kind (wrapped_int_cst));
6350 tree string_lit = build_string (4, "foo");
6351 TREE_TYPE (string_lit) = char_array_type_node;
6352 string_lit = fix_string_type (string_lit);
6353 ASSERT_EQ (clk_ordinary, lvalue_kind (string_lit));
6355 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
6356 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
6357 ASSERT_EQ (clk_ordinary, lvalue_kind (wrapped_string_lit));
6359 tree parm = build_decl (UNKNOWN_LOCATION, PARM_DECL,
6360 get_identifier ("some_parm"),
6361 integer_type_node);
6362 ASSERT_EQ (clk_ordinary, lvalue_kind (parm));
6364 tree wrapped_parm = maybe_wrap_with_location (parm, loc);
6365 ASSERT_TRUE (location_wrapper_p (wrapped_parm));
6366 ASSERT_EQ (clk_ordinary, lvalue_kind (wrapped_parm));
6368 /* Verify that lvalue_kind of std::move on a parm isn't
6369 affected by location wrappers. */
6370 tree rvalue_ref_of_parm = move (parm);
6371 ASSERT_EQ (clk_rvalueref, lvalue_kind (rvalue_ref_of_parm));
6372 tree rvalue_ref_of_wrapped_parm = move (wrapped_parm);
6373 ASSERT_EQ (clk_rvalueref, lvalue_kind (rvalue_ref_of_wrapped_parm));
6375 /* Verify lvalue_p. */
6376 ASSERT_FALSE (lvalue_p (int_cst));
6377 ASSERT_FALSE (lvalue_p (wrapped_int_cst));
6378 ASSERT_TRUE (lvalue_p (parm));
6379 ASSERT_TRUE (lvalue_p (wrapped_parm));
6380 ASSERT_FALSE (lvalue_p (rvalue_ref_of_parm));
6381 ASSERT_FALSE (lvalue_p (rvalue_ref_of_wrapped_parm));
6384 /* Run all of the selftests within this file. */
6386 void
6387 cp_tree_cc_tests ()
6389 test_lvalue_kind ();
6392 } // namespace selftest
6394 #endif /* #if CHECKING_P */
6397 #include "gt-cp-tree.h"