c++: ICE with alias in pack expansion [PR103769]
[official-gcc.git] / gcc / cp / tree.cc
blob492921721f2d69fca88fa7abadc1de348c7a1c96
1 /* Language-dependent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2022 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 *);
50 /* If REF is an lvalue, returns the kind of lvalue that REF is.
51 Otherwise, returns clk_none. */
53 cp_lvalue_kind
54 lvalue_kind (const_tree ref)
56 cp_lvalue_kind op1_lvalue_kind = clk_none;
57 cp_lvalue_kind op2_lvalue_kind = clk_none;
59 /* Expressions of reference type are sometimes wrapped in
60 INDIRECT_REFs. INDIRECT_REFs are just internal compiler
61 representation, not part of the language, so we have to look
62 through them. */
63 if (REFERENCE_REF_P (ref))
64 return lvalue_kind (TREE_OPERAND (ref, 0));
66 if (TREE_TYPE (ref)
67 && TYPE_REF_P (TREE_TYPE (ref)))
69 /* unnamed rvalue references are rvalues */
70 if (TYPE_REF_IS_RVALUE (TREE_TYPE (ref))
71 && TREE_CODE (ref) != PARM_DECL
72 && !VAR_P (ref)
73 && TREE_CODE (ref) != COMPONENT_REF
74 /* Functions are always lvalues. */
75 && TREE_CODE (TREE_TYPE (TREE_TYPE (ref))) != FUNCTION_TYPE)
77 op1_lvalue_kind = clk_rvalueref;
78 if (implicit_rvalue_p (ref))
79 op1_lvalue_kind |= clk_implicit_rval;
80 return op1_lvalue_kind;
83 /* lvalue references and named rvalue references are lvalues. */
84 return clk_ordinary;
87 if (ref == current_class_ptr)
88 return clk_none;
90 /* Expressions with cv void type are prvalues. */
91 if (TREE_TYPE (ref) && VOID_TYPE_P (TREE_TYPE (ref)))
92 return clk_none;
94 switch (TREE_CODE (ref))
96 case SAVE_EXPR:
97 return clk_none;
99 /* preincrements and predecrements are valid lvals, provided
100 what they refer to are valid lvals. */
101 case PREINCREMENT_EXPR:
102 case PREDECREMENT_EXPR:
103 case TRY_CATCH_EXPR:
104 case REALPART_EXPR:
105 case IMAGPART_EXPR:
106 case VIEW_CONVERT_EXPR:
107 return lvalue_kind (TREE_OPERAND (ref, 0));
109 case ARRAY_REF:
111 tree op1 = TREE_OPERAND (ref, 0);
112 if (TREE_CODE (TREE_TYPE (op1)) == ARRAY_TYPE)
114 op1_lvalue_kind = lvalue_kind (op1);
115 if (op1_lvalue_kind == clk_class)
116 /* in the case of an array operand, the result is an lvalue if
117 that operand is an lvalue and an xvalue otherwise */
118 op1_lvalue_kind = clk_rvalueref;
119 return op1_lvalue_kind;
121 else
122 return clk_ordinary;
125 case MEMBER_REF:
126 case DOTSTAR_EXPR:
127 if (TREE_CODE (ref) == MEMBER_REF)
128 op1_lvalue_kind = clk_ordinary;
129 else
130 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
131 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
132 op1_lvalue_kind = clk_none;
133 else if (op1_lvalue_kind == clk_class)
134 /* The result of a .* expression whose second operand is a pointer to a
135 data member is an lvalue if the first operand is an lvalue and an
136 xvalue otherwise. */
137 op1_lvalue_kind = clk_rvalueref;
138 return op1_lvalue_kind;
140 case COMPONENT_REF:
141 if (BASELINK_P (TREE_OPERAND (ref, 1)))
143 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (ref, 1));
145 /* For static member function recurse on the BASELINK, we can get
146 here e.g. from reference_binding. If BASELINK_FUNCTIONS is
147 OVERLOAD, the overload is resolved first if possible through
148 resolve_address_of_overloaded_function. */
149 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_STATIC_FUNCTION_P (fn))
150 return lvalue_kind (TREE_OPERAND (ref, 1));
152 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
153 if (op1_lvalue_kind == clk_class)
154 /* If E1 is an lvalue, then E1.E2 is an lvalue;
155 otherwise E1.E2 is an xvalue. */
156 op1_lvalue_kind = clk_rvalueref;
158 /* Look at the member designator. */
159 if (!op1_lvalue_kind)
161 else if (is_overloaded_fn (TREE_OPERAND (ref, 1)))
162 /* The "field" can be a FUNCTION_DECL or an OVERLOAD in some
163 situations. If we're seeing a COMPONENT_REF, it's a non-static
164 member, so it isn't an lvalue. */
165 op1_lvalue_kind = clk_none;
166 else if (TREE_CODE (TREE_OPERAND (ref, 1)) != FIELD_DECL)
167 /* This can be IDENTIFIER_NODE in a template. */;
168 else if (DECL_C_BIT_FIELD (TREE_OPERAND (ref, 1)))
170 /* Clear the ordinary bit. If this object was a class
171 rvalue we want to preserve that information. */
172 op1_lvalue_kind &= ~clk_ordinary;
173 /* The lvalue is for a bitfield. */
174 op1_lvalue_kind |= clk_bitfield;
176 else if (DECL_PACKED (TREE_OPERAND (ref, 1)))
177 op1_lvalue_kind |= clk_packed;
179 return op1_lvalue_kind;
181 case STRING_CST:
182 case COMPOUND_LITERAL_EXPR:
183 return clk_ordinary;
185 case CONST_DECL:
186 /* CONST_DECL without TREE_STATIC are enumeration values and
187 thus not lvalues. With TREE_STATIC they are used by ObjC++
188 in objc_build_string_object and need to be considered as
189 lvalues. */
190 if (! TREE_STATIC (ref))
191 return clk_none;
192 /* FALLTHRU */
193 case VAR_DECL:
194 if (VAR_P (ref) && DECL_HAS_VALUE_EXPR_P (ref))
195 return lvalue_kind (DECL_VALUE_EXPR (CONST_CAST_TREE (ref)));
197 if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
198 && DECL_LANG_SPECIFIC (ref)
199 && DECL_IN_AGGR_P (ref))
200 return clk_none;
201 /* FALLTHRU */
202 case INDIRECT_REF:
203 case ARROW_EXPR:
204 case PARM_DECL:
205 case RESULT_DECL:
206 case PLACEHOLDER_EXPR:
207 return clk_ordinary;
209 /* A scope ref in a template, left as SCOPE_REF to support later
210 access checking. */
211 case SCOPE_REF:
212 gcc_assert (!type_dependent_expression_p (CONST_CAST_TREE (ref)));
214 tree op = TREE_OPERAND (ref, 1);
215 if (TREE_CODE (op) == FIELD_DECL)
216 return (DECL_C_BIT_FIELD (op) ? clk_bitfield : clk_ordinary);
217 else
218 return lvalue_kind (op);
221 case MAX_EXPR:
222 case MIN_EXPR:
223 /* Disallow <? and >? as lvalues if either argument side-effects. */
224 if (TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 0))
225 || TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 1)))
226 return clk_none;
227 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
228 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1));
229 break;
231 case COND_EXPR:
232 if (processing_template_decl)
234 /* Within templates, a REFERENCE_TYPE will indicate whether
235 the COND_EXPR result is an ordinary lvalue or rvalueref.
236 Since REFERENCE_TYPEs are handled above, if we reach this
237 point, we know we got a plain rvalue. Unless we have a
238 type-dependent expr, that is, but we shouldn't be testing
239 lvalueness if we can't even tell the types yet! */
240 gcc_assert (!type_dependent_expression_p (CONST_CAST_TREE (ref)));
241 goto default_;
244 tree op1 = TREE_OPERAND (ref, 1);
245 if (!op1) op1 = TREE_OPERAND (ref, 0);
246 tree op2 = TREE_OPERAND (ref, 2);
247 op1_lvalue_kind = lvalue_kind (op1);
248 op2_lvalue_kind = lvalue_kind (op2);
249 if (!op1_lvalue_kind != !op2_lvalue_kind)
251 /* The second or the third operand (but not both) is a
252 throw-expression; the result is of the type
253 and value category of the other. */
254 if (op1_lvalue_kind && TREE_CODE (op2) == THROW_EXPR)
255 op2_lvalue_kind = op1_lvalue_kind;
256 else if (op2_lvalue_kind && TREE_CODE (op1) == THROW_EXPR)
257 op1_lvalue_kind = op2_lvalue_kind;
260 break;
262 case MODOP_EXPR:
263 /* We expect to see unlowered MODOP_EXPRs only during
264 template processing. */
265 gcc_assert (processing_template_decl);
266 return clk_ordinary;
268 case MODIFY_EXPR:
269 case TYPEID_EXPR:
270 return clk_ordinary;
272 case COMPOUND_EXPR:
273 return lvalue_kind (TREE_OPERAND (ref, 1));
275 case TARGET_EXPR:
276 return clk_class;
278 case VA_ARG_EXPR:
279 return (CLASS_TYPE_P (TREE_TYPE (ref)) ? clk_class : clk_none);
281 case CALL_EXPR:
282 /* We can see calls outside of TARGET_EXPR in templates. */
283 if (CLASS_TYPE_P (TREE_TYPE (ref)))
284 return clk_class;
285 return clk_none;
287 case FUNCTION_DECL:
288 /* All functions (except non-static-member functions) are
289 lvalues. */
290 return (DECL_NONSTATIC_MEMBER_FUNCTION_P (ref)
291 ? clk_none : clk_ordinary);
293 case BASELINK:
294 /* We now represent a reference to a single static member function
295 with a BASELINK. */
296 /* This CONST_CAST is okay because BASELINK_FUNCTIONS returns
297 its argument unmodified and we assign it to a const_tree. */
298 return lvalue_kind (BASELINK_FUNCTIONS (CONST_CAST_TREE (ref)));
300 case NON_DEPENDENT_EXPR:
301 case PAREN_EXPR:
302 return lvalue_kind (TREE_OPERAND (ref, 0));
304 case TEMPLATE_PARM_INDEX:
305 if (CLASS_TYPE_P (TREE_TYPE (ref)))
306 /* A template parameter object is an lvalue. */
307 return clk_ordinary;
308 return clk_none;
310 default:
311 default_:
312 if (!TREE_TYPE (ref))
313 return clk_none;
314 if (CLASS_TYPE_P (TREE_TYPE (ref))
315 || TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE)
316 return clk_class;
317 return clk_none;
320 /* If one operand is not an lvalue at all, then this expression is
321 not an lvalue. */
322 if (!op1_lvalue_kind || !op2_lvalue_kind)
323 return clk_none;
325 /* Otherwise, it's an lvalue, and it has all the odd properties
326 contributed by either operand. */
327 op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind;
328 /* It's not an ordinary lvalue if it involves any other kind. */
329 if ((op1_lvalue_kind & ~clk_ordinary) != clk_none)
330 op1_lvalue_kind &= ~clk_ordinary;
331 /* It can't be both a pseudo-lvalue and a non-addressable lvalue.
332 A COND_EXPR of those should be wrapped in a TARGET_EXPR. */
333 if ((op1_lvalue_kind & (clk_rvalueref|clk_class))
334 && (op1_lvalue_kind & (clk_bitfield|clk_packed)))
335 op1_lvalue_kind = clk_none;
336 return op1_lvalue_kind;
339 /* Returns the kind of lvalue that REF is, in the sense of [basic.lval]. */
341 cp_lvalue_kind
342 real_lvalue_p (const_tree ref)
344 cp_lvalue_kind kind = lvalue_kind (ref);
345 if (kind & (clk_rvalueref|clk_class))
346 return clk_none;
347 else
348 return kind;
351 /* c-common wants us to return bool. */
353 bool
354 lvalue_p (const_tree t)
356 return real_lvalue_p (t);
359 /* This differs from lvalue_p in that xvalues are included. */
361 bool
362 glvalue_p (const_tree ref)
364 cp_lvalue_kind kind = lvalue_kind (ref);
365 if (kind & clk_class)
366 return false;
367 else
368 return (kind != clk_none);
371 /* This differs from glvalue_p in that class prvalues are included. */
373 bool
374 obvalue_p (const_tree ref)
376 return (lvalue_kind (ref) != clk_none);
379 /* Returns true if REF is an xvalue (the result of dereferencing an rvalue
380 reference), false otherwise. */
382 bool
383 xvalue_p (const_tree ref)
385 return (lvalue_kind (ref) == clk_rvalueref);
388 /* True if REF is a bit-field. */
390 bool
391 bitfield_p (const_tree ref)
393 return (lvalue_kind (ref) & clk_bitfield);
396 /* C++-specific version of stabilize_reference. */
398 tree
399 cp_stabilize_reference (tree ref)
401 STRIP_ANY_LOCATION_WRAPPER (ref);
402 switch (TREE_CODE (ref))
404 case NON_DEPENDENT_EXPR:
405 /* We aren't actually evaluating this. */
406 return ref;
408 /* We need to treat specially anything stabilize_reference doesn't
409 handle specifically. */
410 case VAR_DECL:
411 case PARM_DECL:
412 case RESULT_DECL:
413 CASE_CONVERT:
414 case FLOAT_EXPR:
415 case FIX_TRUNC_EXPR:
416 case INDIRECT_REF:
417 case COMPONENT_REF:
418 case BIT_FIELD_REF:
419 case ARRAY_REF:
420 case ARRAY_RANGE_REF:
421 case ERROR_MARK:
422 break;
423 default:
424 cp_lvalue_kind kind = lvalue_kind (ref);
425 if ((kind & ~clk_class) != clk_none)
427 tree type = unlowered_expr_type (ref);
428 bool rval = !!(kind & clk_rvalueref);
429 type = cp_build_reference_type (type, rval);
430 /* This inhibits warnings in, eg, cxx_mark_addressable
431 (c++/60955). */
432 warning_sentinel s (extra_warnings);
433 ref = build_static_cast (input_location, type, ref,
434 tf_error);
438 return stabilize_reference (ref);
441 /* Test whether DECL is a builtin that may appear in a
442 constant-expression. */
444 bool
445 builtin_valid_in_constant_expr_p (const_tree decl)
447 STRIP_ANY_LOCATION_WRAPPER (decl);
448 if (TREE_CODE (decl) != FUNCTION_DECL)
449 /* Not a function. */
450 return false;
451 if (DECL_BUILT_IN_CLASS (decl) != BUILT_IN_NORMAL)
453 if (fndecl_built_in_p (decl, BUILT_IN_FRONTEND))
454 switch (DECL_FE_FUNCTION_CODE (decl))
456 case CP_BUILT_IN_IS_CONSTANT_EVALUATED:
457 case CP_BUILT_IN_SOURCE_LOCATION:
458 case CP_BUILT_IN_IS_CORRESPONDING_MEMBER:
459 case CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS:
460 return true;
461 default:
462 break;
464 /* Not a built-in. */
465 return false;
467 switch (DECL_FUNCTION_CODE (decl))
469 /* These always have constant results like the corresponding
470 macros/symbol. */
471 case BUILT_IN_FILE:
472 case BUILT_IN_FUNCTION:
473 case BUILT_IN_LINE:
475 /* The following built-ins are valid in constant expressions
476 when their arguments are. */
477 case BUILT_IN_ADD_OVERFLOW_P:
478 case BUILT_IN_SUB_OVERFLOW_P:
479 case BUILT_IN_MUL_OVERFLOW_P:
481 /* These have constant results even if their operands are
482 non-constant. */
483 case BUILT_IN_CONSTANT_P:
484 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
485 return true;
486 default:
487 return false;
491 /* Build a TARGET_EXPR, initializing the DECL with the VALUE. */
493 static tree
494 build_target_expr (tree decl, tree value, tsubst_flags_t complain)
496 tree t;
497 tree type = TREE_TYPE (decl);
499 value = mark_rvalue_use (value);
501 gcc_checking_assert (VOID_TYPE_P (TREE_TYPE (value))
502 || TREE_TYPE (decl) == TREE_TYPE (value)
503 /* On ARM ctors return 'this'. */
504 || (TYPE_PTR_P (TREE_TYPE (value))
505 && TREE_CODE (value) == CALL_EXPR)
506 || useless_type_conversion_p (TREE_TYPE (decl),
507 TREE_TYPE (value)));
509 /* Set TREE_READONLY for optimization, such as gimplify_init_constructor
510 moving a constant aggregate into .rodata. */
511 if (CP_TYPE_CONST_NON_VOLATILE_P (type)
512 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
513 && !VOID_TYPE_P (TREE_TYPE (value))
514 && reduced_constant_expression_p (value))
515 TREE_READONLY (decl) = true;
517 if (complain & tf_no_cleanup)
518 /* The caller is building a new-expr and does not need a cleanup. */
519 t = NULL_TREE;
520 else
522 t = cxx_maybe_build_cleanup (decl, complain);
523 if (t == error_mark_node)
524 return error_mark_node;
526 t = build4 (TARGET_EXPR, type, decl, value, t, NULL_TREE);
527 if (location_t eloc = cp_expr_location (value))
528 SET_EXPR_LOCATION (t, eloc);
529 /* We always set TREE_SIDE_EFFECTS so that expand_expr does not
530 ignore the TARGET_EXPR. If there really turn out to be no
531 side-effects, then the optimizer should be able to get rid of
532 whatever code is generated anyhow. */
533 TREE_SIDE_EFFECTS (t) = 1;
535 return t;
538 /* Return an undeclared local temporary of type TYPE for use in building a
539 TARGET_EXPR. */
541 tree
542 build_local_temp (tree type)
544 tree slot = build_decl (input_location,
545 VAR_DECL, NULL_TREE, type);
546 DECL_ARTIFICIAL (slot) = 1;
547 DECL_IGNORED_P (slot) = 1;
548 DECL_CONTEXT (slot) = current_function_decl;
549 layout_decl (slot, 0);
550 return slot;
553 /* Return whether DECL is such a local temporary (or one from
554 create_tmp_var_raw). */
556 bool
557 is_local_temp (tree decl)
559 return (VAR_P (decl) && DECL_ARTIFICIAL (decl)
560 && !TREE_STATIC (decl));
563 /* Set various status flags when building an AGGR_INIT_EXPR object T. */
565 static void
566 process_aggr_init_operands (tree t)
568 bool side_effects;
570 side_effects = TREE_SIDE_EFFECTS (t);
571 if (!side_effects)
573 int i, n;
574 n = TREE_OPERAND_LENGTH (t);
575 for (i = 1; i < n; i++)
577 tree op = TREE_OPERAND (t, i);
578 if (op && TREE_SIDE_EFFECTS (op))
580 side_effects = 1;
581 break;
585 TREE_SIDE_EFFECTS (t) = side_effects;
588 /* Build an AGGR_INIT_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE,
589 FN, and SLOT. NARGS is the number of call arguments which are specified
590 as a tree array ARGS. */
592 static tree
593 build_aggr_init_array (tree return_type, tree fn, tree slot, int nargs,
594 tree *args)
596 tree t;
597 int i;
599 t = build_vl_exp (AGGR_INIT_EXPR, nargs + 3);
600 TREE_TYPE (t) = return_type;
601 AGGR_INIT_EXPR_FN (t) = fn;
602 AGGR_INIT_EXPR_SLOT (t) = slot;
603 for (i = 0; i < nargs; i++)
604 AGGR_INIT_EXPR_ARG (t, i) = args[i];
605 process_aggr_init_operands (t);
606 return t;
609 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
610 target. TYPE is the type to be initialized.
612 Build an AGGR_INIT_EXPR to represent the initialization. This function
613 differs from build_cplus_new in that an AGGR_INIT_EXPR can only be used
614 to initialize another object, whereas a TARGET_EXPR can either
615 initialize another object or create its own temporary object, and as a
616 result building up a TARGET_EXPR requires that the type's destructor be
617 callable. */
619 tree
620 build_aggr_init_expr (tree type, tree init)
622 tree fn;
623 tree slot;
624 tree rval;
625 int is_ctor;
627 gcc_assert (!VOID_TYPE_P (type));
629 /* Don't build AGGR_INIT_EXPR in a template. */
630 if (processing_template_decl)
631 return init;
633 fn = cp_get_callee (init);
634 if (fn == NULL_TREE)
635 return convert (type, init);
637 is_ctor = (TREE_CODE (fn) == ADDR_EXPR
638 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
639 && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0)));
641 /* We split the CALL_EXPR into its function and its arguments here.
642 Then, in expand_expr, we put them back together. The reason for
643 this is that this expression might be a default argument
644 expression. In that case, we need a new temporary every time the
645 expression is used. That's what break_out_target_exprs does; it
646 replaces every AGGR_INIT_EXPR with a copy that uses a fresh
647 temporary slot. Then, expand_expr builds up a call-expression
648 using the new slot. */
650 /* If we don't need to use a constructor to create an object of this
651 type, don't mess with AGGR_INIT_EXPR. */
652 if (is_ctor || TREE_ADDRESSABLE (type))
654 slot = build_local_temp (type);
656 if (TREE_CODE (init) == CALL_EXPR)
658 rval = build_aggr_init_array (void_type_node, fn, slot,
659 call_expr_nargs (init),
660 CALL_EXPR_ARGP (init));
661 AGGR_INIT_FROM_THUNK_P (rval)
662 = CALL_FROM_THUNK_P (init);
664 else
666 rval = build_aggr_init_array (void_type_node, fn, slot,
667 aggr_init_expr_nargs (init),
668 AGGR_INIT_EXPR_ARGP (init));
669 AGGR_INIT_FROM_THUNK_P (rval)
670 = AGGR_INIT_FROM_THUNK_P (init);
672 TREE_SIDE_EFFECTS (rval) = 1;
673 AGGR_INIT_VIA_CTOR_P (rval) = is_ctor;
674 TREE_NOTHROW (rval) = TREE_NOTHROW (init);
675 CALL_EXPR_OPERATOR_SYNTAX (rval) = CALL_EXPR_OPERATOR_SYNTAX (init);
676 CALL_EXPR_ORDERED_ARGS (rval) = CALL_EXPR_ORDERED_ARGS (init);
677 CALL_EXPR_REVERSE_ARGS (rval) = CALL_EXPR_REVERSE_ARGS (init);
679 else
680 rval = init;
682 return rval;
685 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
686 target. TYPE is the type that this initialization should appear to
687 have.
689 Build an encapsulation of the initialization to perform
690 and return it so that it can be processed by language-independent
691 and language-specific expression expanders. */
693 tree
694 build_cplus_new (tree type, tree init, tsubst_flags_t complain)
696 /* This function should cope with what build_special_member_call
697 can produce. When performing parenthesized aggregate initialization,
698 it can produce a { }. */
699 if (BRACE_ENCLOSED_INITIALIZER_P (init))
701 gcc_assert (cxx_dialect >= cxx20);
702 return finish_compound_literal (type, init, complain);
705 tree rval = build_aggr_init_expr (type, init);
706 tree slot;
708 if (init == error_mark_node)
709 return error_mark_node;
711 if (!complete_type_or_maybe_complain (type, init, complain))
712 return error_mark_node;
714 /* Make sure that we're not trying to create an instance of an
715 abstract class. */
716 if (abstract_virtuals_error_sfinae (NULL_TREE, type, complain))
717 return error_mark_node;
719 if (TREE_CODE (rval) == AGGR_INIT_EXPR)
720 slot = AGGR_INIT_EXPR_SLOT (rval);
721 else if (TREE_CODE (rval) == CALL_EXPR
722 || TREE_CODE (rval) == CONSTRUCTOR)
723 slot = build_local_temp (type);
724 else
725 return rval;
727 rval = build_target_expr (slot, rval, complain);
729 if (rval != error_mark_node)
730 TARGET_EXPR_IMPLICIT_P (rval) = 1;
732 return rval;
735 /* Subroutine of build_vec_init_expr: Build up a single element
736 intialization as a proxy for the full array initialization to get things
737 marked as used and any appropriate diagnostics.
739 This used to be necessary because we were deferring building the actual
740 constructor calls until gimplification time; now we only do it to set
741 VEC_INIT_EXPR_IS_CONSTEXPR.
743 We assume that init is either NULL_TREE, void_type_node (indicating
744 value-initialization), or another array to copy. */
746 static tree
747 build_vec_init_elt (tree type, tree init, tsubst_flags_t complain)
749 tree inner_type = strip_array_types (type);
751 if (integer_zerop (array_type_nelts_total (type))
752 || !CLASS_TYPE_P (inner_type))
753 /* No interesting initialization to do. */
754 return integer_zero_node;
755 else if (init == void_type_node)
756 return build_value_init (inner_type, complain);
758 releasing_vec argvec;
759 if (init && !BRACE_ENCLOSED_INITIALIZER_P (init))
761 gcc_assert (same_type_ignoring_top_level_qualifiers_p
762 (type, TREE_TYPE (init)));
763 tree init_type = strip_array_types (TREE_TYPE (init));
764 tree dummy = build_dummy_object (init_type);
765 if (!lvalue_p (init))
766 dummy = move (dummy);
767 argvec->quick_push (dummy);
769 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
770 &argvec, inner_type, LOOKUP_NORMAL,
771 complain);
773 /* For a trivial constructor, build_over_call creates a TARGET_EXPR. But
774 we don't want one here because we aren't creating a temporary. */
775 if (TREE_CODE (init) == TARGET_EXPR)
776 init = TARGET_EXPR_INITIAL (init);
778 return init;
781 /* Return a TARGET_EXPR which expresses the initialization of an array to
782 be named later, either default-initialization or copy-initialization
783 from another array of the same type. */
785 tree
786 build_vec_init_expr (tree type, tree init, tsubst_flags_t complain)
788 if (tree vi = get_vec_init_expr (init))
789 return vi;
791 tree elt_init;
792 if (init && TREE_CODE (init) == CONSTRUCTOR
793 && !BRACE_ENCLOSED_INITIALIZER_P (init))
794 /* We built any needed constructor calls in digest_init. */
795 elt_init = init;
796 else
797 elt_init = build_vec_init_elt (type, init, complain);
799 bool value_init = false;
800 if (init == void_type_node)
802 value_init = true;
803 init = NULL_TREE;
806 tree slot = build_local_temp (type);
807 init = build2 (VEC_INIT_EXPR, type, slot, init);
808 TREE_SIDE_EFFECTS (init) = true;
809 SET_EXPR_LOCATION (init, input_location);
811 if (cxx_dialect >= cxx11
812 && potential_constant_expression (elt_init))
813 VEC_INIT_EXPR_IS_CONSTEXPR (init) = true;
814 VEC_INIT_EXPR_VALUE_INIT (init) = value_init;
816 return init;
819 /* Call build_vec_init to expand VEC_INIT into TARGET (for which NULL_TREE
820 means VEC_INIT_EXPR_SLOT). */
822 tree
823 expand_vec_init_expr (tree target, tree vec_init, tsubst_flags_t complain,
824 vec<tree,va_gc> **flags)
826 iloc_sentinel ils = EXPR_LOCATION (vec_init);
828 if (!target)
829 target = VEC_INIT_EXPR_SLOT (vec_init);
830 tree init = VEC_INIT_EXPR_INIT (vec_init);
831 int from_array = (init && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE);
832 return build_vec_init (target, NULL_TREE, init,
833 VEC_INIT_EXPR_VALUE_INIT (vec_init),
834 from_array, complain, flags);
837 /* Give a helpful diagnostic for a non-constexpr VEC_INIT_EXPR in a context
838 that requires a constant expression. */
840 void
841 diagnose_non_constexpr_vec_init (tree expr)
843 tree type = TREE_TYPE (VEC_INIT_EXPR_SLOT (expr));
844 tree init, elt_init;
845 if (VEC_INIT_EXPR_VALUE_INIT (expr))
846 init = void_type_node;
847 else
848 init = VEC_INIT_EXPR_INIT (expr);
850 elt_init = build_vec_init_elt (type, init, tf_warning_or_error);
851 require_potential_constant_expression (elt_init);
854 tree
855 build_array_copy (tree init)
857 return get_target_expr (build_vec_init_expr
858 (TREE_TYPE (init), init, tf_warning_or_error));
861 /* Build a TARGET_EXPR using INIT to initialize a new temporary of the
862 indicated TYPE. */
864 tree
865 build_target_expr_with_type (tree init, tree type, tsubst_flags_t complain)
867 gcc_assert (!VOID_TYPE_P (type));
868 gcc_assert (!VOID_TYPE_P (TREE_TYPE (init)));
870 if (TREE_CODE (init) == TARGET_EXPR
871 || init == error_mark_node)
872 return init;
873 else if (CLASS_TYPE_P (type) && type_has_nontrivial_copy_init (type)
874 && TREE_CODE (init) != COND_EXPR
875 && TREE_CODE (init) != CONSTRUCTOR
876 && TREE_CODE (init) != VA_ARG_EXPR
877 && TREE_CODE (init) != CALL_EXPR)
878 /* We need to build up a copy constructor call. COND_EXPR is a special
879 case because we already have copies on the arms and we don't want
880 another one here. A CONSTRUCTOR is aggregate initialization, which
881 is handled separately. A VA_ARG_EXPR is magic creation of an
882 aggregate; there's no additional work to be done. A CALL_EXPR
883 already creates a prvalue. */
884 return force_rvalue (init, complain);
886 return force_target_expr (type, init, complain);
889 /* Like the above function, but without the checking. This function should
890 only be used by code which is deliberately trying to subvert the type
891 system, such as call_builtin_trap. Or build_over_call, to avoid
892 infinite recursion. */
894 tree
895 force_target_expr (tree type, tree init, tsubst_flags_t complain)
897 tree slot;
899 gcc_assert (!VOID_TYPE_P (type));
901 slot = build_local_temp (type);
902 return build_target_expr (slot, init, complain);
905 /* Like build_target_expr_with_type, but use the type of INIT. */
907 tree
908 get_target_expr_sfinae (tree init, tsubst_flags_t complain)
910 if (TREE_CODE (init) == AGGR_INIT_EXPR)
911 return build_target_expr (AGGR_INIT_EXPR_SLOT (init), init, complain);
912 else if (TREE_CODE (init) == VEC_INIT_EXPR)
913 return build_target_expr (VEC_INIT_EXPR_SLOT (init), init, complain);
914 else
916 init = convert_bitfield_to_declared_type (init);
917 return build_target_expr_with_type (init, TREE_TYPE (init), complain);
921 tree
922 get_target_expr (tree init)
924 return get_target_expr_sfinae (init, tf_warning_or_error);
927 /* If EXPR is a bitfield reference, convert it to the declared type of
928 the bitfield, and return the resulting expression. Otherwise,
929 return EXPR itself. */
931 tree
932 convert_bitfield_to_declared_type (tree expr)
934 tree bitfield_type;
936 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
937 if (bitfield_type)
938 expr = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type),
939 expr);
940 return expr;
943 /* EXPR is being used in an rvalue context. Return a version of EXPR
944 that is marked as an rvalue. */
946 tree
947 rvalue (tree expr)
949 tree type;
951 if (error_operand_p (expr))
952 return expr;
954 expr = mark_rvalue_use (expr);
956 /* [basic.lval]
958 Non-class rvalues always have cv-unqualified types. */
959 type = TREE_TYPE (expr);
960 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
961 type = cv_unqualified (type);
963 /* We need to do this for rvalue refs as well to get the right answer
964 from decltype; see c++/36628. */
965 if (!processing_template_decl && glvalue_p (expr))
967 /* But don't use this function for class lvalues; use move (to treat an
968 lvalue as an xvalue) or force_rvalue (to make a prvalue copy). */
969 gcc_checking_assert (!CLASS_TYPE_P (type));
970 expr = build1 (NON_LVALUE_EXPR, type, expr);
972 else if (type != TREE_TYPE (expr))
973 expr = build_nop (type, expr);
975 return expr;
979 struct cplus_array_info
981 tree type;
982 tree domain;
985 struct cplus_array_hasher : ggc_ptr_hash<tree_node>
987 typedef cplus_array_info *compare_type;
989 static hashval_t hash (tree t);
990 static bool equal (tree, cplus_array_info *);
993 /* Hash an ARRAY_TYPE. K is really of type `tree'. */
995 hashval_t
996 cplus_array_hasher::hash (tree t)
998 hashval_t hash;
1000 hash = TYPE_UID (TREE_TYPE (t));
1001 if (TYPE_DOMAIN (t))
1002 hash ^= TYPE_UID (TYPE_DOMAIN (t));
1003 return hash;
1006 /* Compare two ARRAY_TYPEs. K1 is really of type `tree', K2 is really
1007 of type `cplus_array_info*'. */
1009 bool
1010 cplus_array_hasher::equal (tree t1, cplus_array_info *t2)
1012 return (TREE_TYPE (t1) == t2->type && TYPE_DOMAIN (t1) == t2->domain);
1015 /* Hash table containing dependent array types, which are unsuitable for
1016 the language-independent type hash table. */
1017 static GTY (()) hash_table<cplus_array_hasher> *cplus_array_htab;
1019 /* Build an ARRAY_TYPE without laying it out. */
1021 static tree
1022 build_min_array_type (tree elt_type, tree index_type)
1024 tree t = cxx_make_type (ARRAY_TYPE);
1025 TREE_TYPE (t) = elt_type;
1026 TYPE_DOMAIN (t) = index_type;
1027 return t;
1030 /* Set TYPE_CANONICAL like build_array_type_1, but using
1031 build_cplus_array_type. */
1033 static void
1034 set_array_type_canon (tree t, tree elt_type, tree index_type, bool dep)
1036 /* Set the canonical type for this new node. */
1037 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
1038 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
1039 SET_TYPE_STRUCTURAL_EQUALITY (t);
1040 else if (TYPE_CANONICAL (elt_type) != elt_type
1041 || (index_type && TYPE_CANONICAL (index_type) != index_type))
1042 TYPE_CANONICAL (t)
1043 = build_cplus_array_type (TYPE_CANONICAL (elt_type),
1044 index_type
1045 ? TYPE_CANONICAL (index_type) : index_type,
1046 dep);
1047 else
1048 TYPE_CANONICAL (t) = t;
1051 /* Like build_array_type, but handle special C++ semantics: an array of a
1052 variant element type is a variant of the array of the main variant of
1053 the element type. IS_DEPENDENT is -ve if we should determine the
1054 dependency. Otherwise its bool value indicates dependency. */
1056 tree
1057 build_cplus_array_type (tree elt_type, tree index_type, int dependent)
1059 tree t;
1061 if (elt_type == error_mark_node || index_type == error_mark_node)
1062 return error_mark_node;
1064 if (dependent < 0)
1065 dependent = (uses_template_parms (elt_type)
1066 || (index_type && uses_template_parms (index_type)));
1068 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
1069 /* Start with an array of the TYPE_MAIN_VARIANT. */
1070 t = build_cplus_array_type (TYPE_MAIN_VARIANT (elt_type),
1071 index_type, dependent);
1072 else if (dependent)
1074 /* Since type_hash_canon calls layout_type, we need to use our own
1075 hash table. */
1076 cplus_array_info cai;
1077 hashval_t hash;
1079 if (cplus_array_htab == NULL)
1080 cplus_array_htab = hash_table<cplus_array_hasher>::create_ggc (61);
1082 hash = TYPE_UID (elt_type);
1083 if (index_type)
1084 hash ^= TYPE_UID (index_type);
1085 cai.type = elt_type;
1086 cai.domain = index_type;
1088 tree *e = cplus_array_htab->find_slot_with_hash (&cai, hash, INSERT);
1089 if (*e)
1090 /* We have found the type: we're done. */
1091 return (tree) *e;
1092 else
1094 /* Build a new array type. */
1095 t = build_min_array_type (elt_type, index_type);
1097 /* Store it in the hash table. */
1098 *e = t;
1100 /* Set the canonical type for this new node. */
1101 set_array_type_canon (t, elt_type, index_type, dependent);
1103 /* Mark it as dependent now, this saves time later. */
1104 TYPE_DEPENDENT_P_VALID (t) = true;
1105 TYPE_DEPENDENT_P (t) = true;
1108 else
1110 bool typeless_storage = is_byte_access_type (elt_type);
1111 t = build_array_type (elt_type, index_type, typeless_storage);
1113 /* Mark as non-dependenty now, this will save time later. */
1114 TYPE_DEPENDENT_P_VALID (t) = true;
1117 /* Now check whether we already have this array variant. */
1118 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
1120 tree m = t;
1121 for (t = m; t; t = TYPE_NEXT_VARIANT (t))
1122 if (TREE_TYPE (t) == elt_type
1123 && TYPE_NAME (t) == NULL_TREE
1124 && TYPE_ATTRIBUTES (t) == NULL_TREE)
1125 break;
1126 if (!t)
1128 t = build_min_array_type (elt_type, index_type);
1129 /* Mark dependency now, this saves time later. */
1130 TYPE_DEPENDENT_P_VALID (t) = true;
1131 TYPE_DEPENDENT_P (t) = dependent;
1132 set_array_type_canon (t, elt_type, index_type, dependent);
1133 if (!dependent)
1135 layout_type (t);
1136 /* Make sure sizes are shared with the main variant.
1137 layout_type can't be called after setting TYPE_NEXT_VARIANT,
1138 as it will overwrite alignment etc. of all variants. */
1139 TYPE_SIZE (t) = TYPE_SIZE (m);
1140 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (m);
1141 TYPE_TYPELESS_STORAGE (t) = TYPE_TYPELESS_STORAGE (m);
1144 TYPE_MAIN_VARIANT (t) = m;
1145 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
1146 TYPE_NEXT_VARIANT (m) = t;
1150 /* Avoid spurious warnings with VLAs (c++/54583). */
1151 if (TYPE_SIZE (t) && EXPR_P (TYPE_SIZE (t)))
1152 suppress_warning (TYPE_SIZE (t), OPT_Wunused);
1154 /* Push these needs up to the ARRAY_TYPE so that initialization takes
1155 place more easily. */
1156 bool needs_ctor = (TYPE_NEEDS_CONSTRUCTING (t)
1157 = TYPE_NEEDS_CONSTRUCTING (elt_type));
1158 bool needs_dtor = (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1159 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type));
1161 if (!dependent && t == TYPE_MAIN_VARIANT (t)
1162 && !COMPLETE_TYPE_P (t) && COMPLETE_TYPE_P (elt_type))
1164 /* The element type has been completed since the last time we saw
1165 this array type; update the layout and 'tor flags for any variants
1166 that need it. */
1167 layout_type (t);
1168 for (tree v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1170 TYPE_NEEDS_CONSTRUCTING (v) = needs_ctor;
1171 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (v) = needs_dtor;
1175 return t;
1178 /* Return an ARRAY_TYPE with element type ELT and length N. */
1180 tree
1181 build_array_of_n_type (tree elt, int n)
1183 return build_cplus_array_type (elt, build_index_type (size_int (n - 1)));
1186 /* True iff T is an array of unknown bound. */
1188 bool
1189 array_of_unknown_bound_p (const_tree t)
1191 return (TREE_CODE (t) == ARRAY_TYPE
1192 && !TYPE_DOMAIN (t));
1195 /* True iff T is an N3639 array of runtime bound (VLA). These were approved
1196 for C++14 but then removed. This should only be used for N3639
1197 specifically; code wondering more generally if something is a VLA should use
1198 vla_type_p. */
1200 bool
1201 array_of_runtime_bound_p (tree t)
1203 if (!t || TREE_CODE (t) != ARRAY_TYPE)
1204 return false;
1205 if (variably_modified_type_p (TREE_TYPE (t), NULL_TREE))
1206 return false;
1207 tree dom = TYPE_DOMAIN (t);
1208 if (!dom)
1209 return false;
1210 tree max = TYPE_MAX_VALUE (dom);
1211 return (!potential_rvalue_constant_expression (max)
1212 || (!value_dependent_expression_p (max) && !TREE_CONSTANT (max)));
1215 /* True iff T is a variable length array. */
1217 bool
1218 vla_type_p (tree t)
1220 for (; t && TREE_CODE (t) == ARRAY_TYPE;
1221 t = TREE_TYPE (t))
1222 if (tree dom = TYPE_DOMAIN (t))
1224 tree max = TYPE_MAX_VALUE (dom);
1225 if (!potential_rvalue_constant_expression (max)
1226 || (!value_dependent_expression_p (max) && !TREE_CONSTANT (max)))
1227 return true;
1229 return false;
1233 /* Return a reference type node of MODE referring to TO_TYPE. If MODE
1234 is VOIDmode the standard pointer mode will be picked. If RVAL is
1235 true, return an rvalue reference type, otherwise return an lvalue
1236 reference type. If a type node exists, reuse it, otherwise create
1237 a new one. */
1238 tree
1239 cp_build_reference_type_for_mode (tree to_type, machine_mode mode, bool rval)
1241 tree lvalue_ref, t;
1243 if (to_type == error_mark_node)
1244 return error_mark_node;
1246 if (TYPE_REF_P (to_type))
1248 rval = rval && TYPE_REF_IS_RVALUE (to_type);
1249 to_type = TREE_TYPE (to_type);
1252 lvalue_ref = build_reference_type_for_mode (to_type, mode, false);
1254 if (!rval)
1255 return lvalue_ref;
1257 /* This code to create rvalue reference types is based on and tied
1258 to the code creating lvalue reference types in the middle-end
1259 functions build_reference_type_for_mode and build_reference_type.
1261 It works by putting the rvalue reference type nodes after the
1262 lvalue reference nodes in the TYPE_NEXT_REF_TO linked list, so
1263 they will effectively be ignored by the middle end. */
1265 for (t = lvalue_ref; (t = TYPE_NEXT_REF_TO (t)); )
1266 if (TYPE_REF_IS_RVALUE (t))
1267 return t;
1269 t = build_distinct_type_copy (lvalue_ref);
1271 TYPE_REF_IS_RVALUE (t) = true;
1272 TYPE_NEXT_REF_TO (t) = TYPE_NEXT_REF_TO (lvalue_ref);
1273 TYPE_NEXT_REF_TO (lvalue_ref) = t;
1275 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
1276 SET_TYPE_STRUCTURAL_EQUALITY (t);
1277 else if (TYPE_CANONICAL (to_type) != to_type)
1278 TYPE_CANONICAL (t)
1279 = cp_build_reference_type_for_mode (TYPE_CANONICAL (to_type), mode, rval);
1280 else
1281 TYPE_CANONICAL (t) = t;
1283 layout_type (t);
1285 return t;
1289 /* Return a reference type node referring to TO_TYPE. If RVAL is
1290 true, return an rvalue reference type, otherwise return an lvalue
1291 reference type. If a type node exists, reuse it, otherwise create
1292 a new one. */
1293 tree
1294 cp_build_reference_type (tree to_type, bool rval)
1296 return cp_build_reference_type_for_mode (to_type, VOIDmode, rval);
1299 /* Returns EXPR cast to rvalue reference type, like std::move. */
1301 tree
1302 move (tree expr)
1304 tree type = TREE_TYPE (expr);
1305 gcc_assert (!TYPE_REF_P (type));
1306 if (xvalue_p (expr))
1307 return expr;
1308 type = cp_build_reference_type (type, /*rval*/true);
1309 return build_static_cast (input_location, type, expr,
1310 tf_warning_or_error);
1313 /* Used by the C++ front end to build qualified array types. However,
1314 the C version of this function does not properly maintain canonical
1315 types (which are not used in C). */
1316 tree
1317 c_build_qualified_type (tree type, int type_quals, tree /* orig_qual_type */,
1318 size_t /* orig_qual_indirect */)
1320 return cp_build_qualified_type (type, type_quals);
1324 /* Make a variant of TYPE, qualified with the TYPE_QUALS. Handles
1325 arrays correctly. In particular, if TYPE is an array of T's, and
1326 TYPE_QUALS is non-empty, returns an array of qualified T's.
1328 FLAGS determines how to deal with ill-formed qualifications. If
1329 tf_ignore_bad_quals is set, then bad qualifications are dropped
1330 (this is permitted if TYPE was introduced via a typedef or template
1331 type parameter). If bad qualifications are dropped and tf_warning
1332 is set, then a warning is issued for non-const qualifications. If
1333 tf_ignore_bad_quals is not set and tf_error is not set, we
1334 return error_mark_node. Otherwise, we issue an error, and ignore
1335 the qualifications.
1337 Qualification of a reference type is valid when the reference came
1338 via a typedef or template type argument. [dcl.ref] No such
1339 dispensation is provided for qualifying a function type. [dcl.fct]
1340 DR 295 queries this and the proposed resolution brings it into line
1341 with qualifying a reference. We implement the DR. We also behave
1342 in a similar manner for restricting non-pointer types. */
1344 tree
1345 cp_build_qualified_type_real (tree type,
1346 int type_quals,
1347 tsubst_flags_t complain)
1349 tree result;
1350 int bad_quals = TYPE_UNQUALIFIED;
1352 if (type == error_mark_node)
1353 return type;
1355 if (type_quals == cp_type_quals (type))
1356 return type;
1358 if (TREE_CODE (type) == ARRAY_TYPE)
1360 /* In C++, the qualification really applies to the array element
1361 type. Obtain the appropriately qualified element type. */
1362 tree t;
1363 tree element_type
1364 = cp_build_qualified_type_real (TREE_TYPE (type),
1365 type_quals,
1366 complain);
1368 if (element_type == error_mark_node)
1369 return error_mark_node;
1371 /* See if we already have an identically qualified type. Tests
1372 should be equivalent to those in check_qualified_type. */
1373 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1374 if (TREE_TYPE (t) == element_type
1375 && TYPE_NAME (t) == TYPE_NAME (type)
1376 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
1377 && attribute_list_equal (TYPE_ATTRIBUTES (t),
1378 TYPE_ATTRIBUTES (type)))
1379 break;
1381 if (!t)
1383 /* If we already know the dependentness, tell the array type
1384 constructor. This is important for module streaming, as we cannot
1385 dynamically determine that on read in. */
1386 t = build_cplus_array_type (element_type, TYPE_DOMAIN (type),
1387 TYPE_DEPENDENT_P_VALID (type)
1388 ? int (TYPE_DEPENDENT_P (type)) : -1);
1390 /* Keep the typedef name. */
1391 if (TYPE_NAME (t) != TYPE_NAME (type))
1393 t = build_variant_type_copy (t);
1394 TYPE_NAME (t) = TYPE_NAME (type);
1395 SET_TYPE_ALIGN (t, TYPE_ALIGN (type));
1396 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (type);
1400 /* Even if we already had this variant, we update
1401 TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case
1402 they changed since the variant was originally created.
1404 This seems hokey; if there is some way to use a previous
1405 variant *without* coming through here,
1406 TYPE_NEEDS_CONSTRUCTING will never be updated. */
1407 TYPE_NEEDS_CONSTRUCTING (t)
1408 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
1409 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1410 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
1411 return t;
1413 else if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
1415 tree t = PACK_EXPANSION_PATTERN (type);
1417 t = cp_build_qualified_type_real (t, type_quals, complain);
1418 return make_pack_expansion (t, complain);
1421 /* A reference or method type shall not be cv-qualified.
1422 [dcl.ref], [dcl.fct]. This used to be an error, but as of DR 295
1423 (in CD1) we always ignore extra cv-quals on functions. */
1425 /* [dcl.ref/1] Cv-qualified references are ill-formed except when
1426 the cv-qualifiers are introduced through the use of a typedef-name
1427 ([dcl.typedef], [temp.param]) or decltype-specifier
1428 ([dcl.type.decltype]),in which case the cv-qualifiers are
1429 ignored. */
1430 if (type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)
1431 && (TYPE_REF_P (type)
1432 || FUNC_OR_METHOD_TYPE_P (type)))
1434 if (TYPE_REF_P (type)
1435 && (!typedef_variant_p (type) || FUNC_OR_METHOD_TYPE_P (type)))
1436 bad_quals |= type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1437 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1440 /* But preserve any function-cv-quals on a FUNCTION_TYPE. */
1441 if (TREE_CODE (type) == FUNCTION_TYPE)
1442 type_quals |= type_memfn_quals (type);
1444 /* A restrict-qualified type must be a pointer (or reference)
1445 to object or incomplete type. */
1446 if ((type_quals & TYPE_QUAL_RESTRICT)
1447 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1448 && TREE_CODE (type) != TYPENAME_TYPE
1449 && !INDIRECT_TYPE_P (type))
1451 bad_quals |= TYPE_QUAL_RESTRICT;
1452 type_quals &= ~TYPE_QUAL_RESTRICT;
1455 if (bad_quals == TYPE_UNQUALIFIED
1456 || (complain & tf_ignore_bad_quals))
1457 /*OK*/;
1458 else if (!(complain & tf_error))
1459 return error_mark_node;
1460 else
1462 tree bad_type = build_qualified_type (ptr_type_node, bad_quals);
1463 error ("%qV qualifiers cannot be applied to %qT",
1464 bad_type, type);
1467 /* Retrieve (or create) the appropriately qualified variant. */
1468 result = build_qualified_type (type, type_quals);
1470 return result;
1473 /* Return TYPE with const and volatile removed. */
1475 tree
1476 cv_unqualified (tree type)
1478 int quals;
1480 if (type == error_mark_node)
1481 return type;
1483 quals = cp_type_quals (type);
1484 quals &= ~(TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE);
1485 return cp_build_qualified_type (type, quals);
1488 /* Subroutine of strip_typedefs. We want to apply to RESULT the attributes
1489 from ATTRIBS that affect type identity, and no others. If any are not
1490 applied, set *remove_attributes to true. */
1492 static tree
1493 apply_identity_attributes (tree result, tree attribs, bool *remove_attributes)
1495 tree first_ident = NULL_TREE;
1496 tree new_attribs = NULL_TREE;
1497 tree *p = &new_attribs;
1499 if (OVERLOAD_TYPE_P (result))
1501 /* On classes and enums all attributes are ingrained. */
1502 gcc_assert (attribs == TYPE_ATTRIBUTES (result));
1503 return result;
1506 for (tree a = attribs; a; a = TREE_CHAIN (a))
1508 const attribute_spec *as
1509 = lookup_attribute_spec (get_attribute_name (a));
1510 if (as && as->affects_type_identity)
1512 if (!first_ident)
1513 first_ident = a;
1514 else if (first_ident == error_mark_node)
1516 *p = tree_cons (TREE_PURPOSE (a), TREE_VALUE (a), NULL_TREE);
1517 p = &TREE_CHAIN (*p);
1520 else if (first_ident && first_ident != error_mark_node)
1522 for (tree a2 = first_ident; a2 != a; a2 = TREE_CHAIN (a2))
1524 *p = tree_cons (TREE_PURPOSE (a2), TREE_VALUE (a2), NULL_TREE);
1525 p = &TREE_CHAIN (*p);
1527 first_ident = error_mark_node;
1530 if (first_ident != error_mark_node)
1531 new_attribs = first_ident;
1533 if (first_ident == attribs)
1534 /* All attributes affected type identity. */;
1535 else
1536 *remove_attributes = true;
1538 return cp_build_type_attribute_variant (result, new_attribs);
1541 /* Builds a qualified variant of T that is either not a typedef variant
1542 (the default behavior) or not a typedef variant of a user-facing type
1543 (if FLAGS contains STF_USER_FACING).
1545 E.g. consider the following declarations:
1546 typedef const int ConstInt;
1547 typedef ConstInt* PtrConstInt;
1548 If T is PtrConstInt, this function returns a type representing
1549 const int*.
1550 In other words, if T is a typedef, the function returns the underlying type.
1551 The cv-qualification and attributes of the type returned match the
1552 input type.
1553 They will always be compatible types.
1554 The returned type is built so that all of its subtypes
1555 recursively have their typedefs stripped as well.
1557 This is different from just returning TYPE_CANONICAL (T)
1558 Because of several reasons:
1559 * If T is a type that needs structural equality
1560 its TYPE_CANONICAL (T) will be NULL.
1561 * TYPE_CANONICAL (T) desn't carry type attributes
1562 and loses template parameter names.
1564 If REMOVE_ATTRIBUTES is non-null, also strip attributes that don't
1565 affect type identity, and set the referent to true if any were
1566 stripped. */
1568 tree
1569 strip_typedefs (tree t, bool *remove_attributes, unsigned int flags)
1571 tree result = NULL, type = NULL, t0 = NULL;
1573 if (!t || t == error_mark_node)
1574 return t;
1576 if (TREE_CODE (t) == TREE_LIST)
1578 bool changed = false;
1579 releasing_vec vec;
1580 tree r = t;
1581 for (; t; t = TREE_CHAIN (t))
1583 gcc_assert (!TREE_PURPOSE (t));
1584 tree elt = strip_typedefs (TREE_VALUE (t), remove_attributes, flags);
1585 if (elt != TREE_VALUE (t))
1586 changed = true;
1587 vec_safe_push (vec, elt);
1589 if (changed)
1590 r = build_tree_list_vec (vec);
1591 return r;
1594 gcc_assert (TYPE_P (t));
1596 if (t == TYPE_CANONICAL (t))
1597 return t;
1599 if (!(flags & STF_STRIP_DEPENDENT)
1600 && dependent_alias_template_spec_p (t, nt_opaque))
1601 /* DR 1558: However, if the template-id is dependent, subsequent
1602 template argument substitution still applies to the template-id. */
1603 return t;
1605 switch (TREE_CODE (t))
1607 case POINTER_TYPE:
1608 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1609 result = build_pointer_type_for_mode (type, TYPE_MODE (t), false);
1610 break;
1611 case REFERENCE_TYPE:
1612 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1613 result = cp_build_reference_type_for_mode (type, TYPE_MODE (t), TYPE_REF_IS_RVALUE (t));
1614 break;
1615 case OFFSET_TYPE:
1616 t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t), remove_attributes, flags);
1617 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1618 result = build_offset_type (t0, type);
1619 break;
1620 case RECORD_TYPE:
1621 if (TYPE_PTRMEMFUNC_P (t))
1623 t0 = strip_typedefs (TYPE_PTRMEMFUNC_FN_TYPE (t),
1624 remove_attributes, flags);
1625 result = build_ptrmemfunc_type (t0);
1627 break;
1628 case ARRAY_TYPE:
1629 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1630 t0 = strip_typedefs (TYPE_DOMAIN (t), remove_attributes, flags);
1631 gcc_checking_assert (TYPE_DEPENDENT_P_VALID (t)
1632 || !dependent_type_p (t));
1633 result = build_cplus_array_type (type, t0, TYPE_DEPENDENT_P (t));
1634 break;
1635 case FUNCTION_TYPE:
1636 case METHOD_TYPE:
1638 tree arg_types = NULL, arg_node, arg_node2, arg_type;
1639 bool changed;
1641 /* Because we stomp on TREE_PURPOSE of TYPE_ARG_TYPES in many places
1642 around the compiler (e.g. cp_parser_late_parsing_default_args), we
1643 can't expect that re-hashing a function type will find a previous
1644 equivalent type, so try to reuse the input type if nothing has
1645 changed. If the type is itself a variant, that will change. */
1646 bool is_variant = typedef_variant_p (t);
1647 if (remove_attributes
1648 && (TYPE_ATTRIBUTES (t) || TYPE_USER_ALIGN (t)))
1649 is_variant = true;
1651 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1652 tree canon_spec = (flag_noexcept_type
1653 ? canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (t))
1654 : NULL_TREE);
1655 changed = (type != TREE_TYPE (t) || is_variant
1656 || TYPE_RAISES_EXCEPTIONS (t) != canon_spec);
1658 for (arg_node = TYPE_ARG_TYPES (t);
1659 arg_node;
1660 arg_node = TREE_CHAIN (arg_node))
1662 if (arg_node == void_list_node)
1663 break;
1664 arg_type = strip_typedefs (TREE_VALUE (arg_node),
1665 remove_attributes, flags);
1666 gcc_assert (arg_type);
1667 if (arg_type == TREE_VALUE (arg_node) && !changed)
1668 continue;
1670 if (!changed)
1672 changed = true;
1673 for (arg_node2 = TYPE_ARG_TYPES (t);
1674 arg_node2 != arg_node;
1675 arg_node2 = TREE_CHAIN (arg_node2))
1676 arg_types
1677 = tree_cons (TREE_PURPOSE (arg_node2),
1678 TREE_VALUE (arg_node2), arg_types);
1681 arg_types
1682 = tree_cons (TREE_PURPOSE (arg_node), arg_type, arg_types);
1685 if (!changed)
1686 return t;
1688 if (arg_types)
1689 arg_types = nreverse (arg_types);
1691 /* A list of parameters not ending with an ellipsis
1692 must end with void_list_node. */
1693 if (arg_node)
1694 arg_types = chainon (arg_types, void_list_node);
1696 if (TREE_CODE (t) == METHOD_TYPE)
1698 tree class_type = TREE_TYPE (TREE_VALUE (arg_types));
1699 gcc_assert (class_type);
1700 result =
1701 build_method_type_directly (class_type, type,
1702 TREE_CHAIN (arg_types));
1704 else
1706 result = build_function_type (type, arg_types);
1707 result = apply_memfn_quals (result, type_memfn_quals (t));
1710 result = build_cp_fntype_variant (result,
1711 type_memfn_rqual (t), canon_spec,
1712 TYPE_HAS_LATE_RETURN_TYPE (t));
1714 break;
1715 case TYPENAME_TYPE:
1717 bool changed = false;
1718 tree fullname = TYPENAME_TYPE_FULLNAME (t);
1719 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
1720 && TREE_OPERAND (fullname, 1))
1722 tree args = TREE_OPERAND (fullname, 1);
1723 tree new_args = copy_node (args);
1724 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
1726 tree arg = TREE_VEC_ELT (args, i);
1727 tree strip_arg;
1728 if (TYPE_P (arg))
1729 strip_arg = strip_typedefs (arg, remove_attributes, flags);
1730 else
1731 strip_arg = strip_typedefs_expr (arg, remove_attributes,
1732 flags);
1733 TREE_VEC_ELT (new_args, i) = strip_arg;
1734 if (strip_arg != arg)
1735 changed = true;
1737 if (changed)
1739 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_args)
1740 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
1741 fullname
1742 = lookup_template_function (TREE_OPERAND (fullname, 0),
1743 new_args);
1745 else
1746 ggc_free (new_args);
1748 tree ctx = strip_typedefs (TYPE_CONTEXT (t), remove_attributes, flags);
1749 if (!changed && ctx == TYPE_CONTEXT (t) && !typedef_variant_p (t))
1750 return t;
1751 tree name = fullname;
1752 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
1753 name = TREE_OPERAND (fullname, 0);
1754 /* Use build_typename_type rather than make_typename_type because we
1755 don't want to resolve it here, just strip typedefs. */
1756 result = build_typename_type (ctx, name, fullname, typename_type);
1758 break;
1759 case DECLTYPE_TYPE:
1760 result = strip_typedefs_expr (DECLTYPE_TYPE_EXPR (t),
1761 remove_attributes, flags);
1762 if (result == DECLTYPE_TYPE_EXPR (t))
1763 result = NULL_TREE;
1764 else
1765 result = (finish_decltype_type
1766 (result,
1767 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t),
1768 tf_none));
1769 break;
1770 case UNDERLYING_TYPE:
1771 type = strip_typedefs (UNDERLYING_TYPE_TYPE (t),
1772 remove_attributes, flags);
1773 result = finish_underlying_type (type);
1774 break;
1775 case TYPE_PACK_EXPANSION:
1777 tree pat = PACK_EXPANSION_PATTERN (t);
1778 if (TYPE_P (pat))
1780 type = strip_typedefs (pat, remove_attributes, flags);
1781 /* Empty packs can thwart our efforts here. Consider
1783 template <typename T, typename... Ts>
1784 using IsOneOf = disjunction<is_same<T, Ts>...>;
1786 where IsOneOf seemingly uses all of its template parameters in
1787 its expansion (and does not expand a pack from the enclosing
1788 class), so the alias is not marked as complex. However, it may
1789 be used as in "IsOneOf<Ts>", where Ts is an empty parameter pack,
1790 and stripping it down into "disjunction<>" here would exclude the
1791 Ts pack, resulting in an error. */
1792 if (type != pat && uses_parameter_packs (type))
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, so that in
1865 template<class T> struct A
1867 typedef T TT;
1868 B<sizeof(TT)> b;
1871 sizeof(TT) is replaced by sizeof(T). */
1873 tree
1874 strip_typedefs_expr (tree t, bool *remove_attributes, unsigned int flags)
1876 unsigned i,n;
1877 tree r, type, *ops;
1878 enum tree_code code;
1880 if (t == NULL_TREE || t == error_mark_node)
1881 return t;
1883 STRIP_ANY_LOCATION_WRAPPER (t);
1885 if (DECL_P (t) || CONSTANT_CLASS_P (t))
1886 return t;
1888 /* Some expressions have type operands, so let's handle types here rather
1889 than check TYPE_P in multiple places below. */
1890 if (TYPE_P (t))
1891 return strip_typedefs (t, remove_attributes, flags);
1893 code = TREE_CODE (t);
1894 switch (code)
1896 case IDENTIFIER_NODE:
1897 case TEMPLATE_PARM_INDEX:
1898 case OVERLOAD:
1899 case BASELINK:
1900 case ARGUMENT_PACK_SELECT:
1901 return t;
1903 case TRAIT_EXPR:
1905 tree type1 = strip_typedefs (TRAIT_EXPR_TYPE1 (t),
1906 remove_attributes, flags);
1907 tree type2 = strip_typedefs (TRAIT_EXPR_TYPE2 (t),
1908 remove_attributes, flags);
1909 if (type1 == TRAIT_EXPR_TYPE1 (t)
1910 && type2 == TRAIT_EXPR_TYPE2 (t))
1911 return t;
1912 r = copy_node (t);
1913 TRAIT_EXPR_TYPE1 (r) = type1;
1914 TRAIT_EXPR_TYPE2 (r) = type2;
1915 return r;
1918 case TREE_LIST:
1920 releasing_vec vec;
1921 bool changed = false;
1922 tree it;
1923 for (it = t; it; it = TREE_CHAIN (it))
1925 tree val = strip_typedefs_expr (TREE_VALUE (it),
1926 remove_attributes, flags);
1927 vec_safe_push (vec, val);
1928 if (val != TREE_VALUE (it))
1929 changed = true;
1930 gcc_assert (TREE_PURPOSE (it) == NULL_TREE);
1932 if (changed)
1934 r = NULL_TREE;
1935 FOR_EACH_VEC_ELT_REVERSE (*vec, i, it)
1936 r = tree_cons (NULL_TREE, it, r);
1938 else
1939 r = t;
1940 return r;
1943 case TREE_VEC:
1945 bool changed = false;
1946 releasing_vec vec;
1947 n = TREE_VEC_LENGTH (t);
1948 vec_safe_reserve (vec, n);
1949 for (i = 0; i < n; ++i)
1951 tree op = strip_typedefs_expr (TREE_VEC_ELT (t, i),
1952 remove_attributes, flags);
1953 vec->quick_push (op);
1954 if (op != TREE_VEC_ELT (t, i))
1955 changed = true;
1957 if (changed)
1959 r = copy_node (t);
1960 for (i = 0; i < n; ++i)
1961 TREE_VEC_ELT (r, i) = (*vec)[i];
1962 NON_DEFAULT_TEMPLATE_ARGS_COUNT (r)
1963 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
1965 else
1966 r = t;
1967 return r;
1970 case CONSTRUCTOR:
1972 bool changed = false;
1973 vec<constructor_elt, va_gc> *vec
1974 = vec_safe_copy (CONSTRUCTOR_ELTS (t));
1975 n = CONSTRUCTOR_NELTS (t);
1976 type = strip_typedefs (TREE_TYPE (t), remove_attributes, flags);
1977 for (i = 0; i < n; ++i)
1979 constructor_elt *e = &(*vec)[i];
1980 tree op = strip_typedefs_expr (e->value, remove_attributes, flags);
1981 if (op != e->value)
1983 changed = true;
1984 e->value = op;
1986 gcc_checking_assert
1987 (e->index == strip_typedefs_expr (e->index, remove_attributes,
1988 flags));
1991 if (!changed && type == TREE_TYPE (t))
1993 vec_free (vec);
1994 return t;
1996 else
1998 r = copy_node (t);
1999 TREE_TYPE (r) = type;
2000 CONSTRUCTOR_ELTS (r) = vec;
2001 return r;
2005 case LAMBDA_EXPR:
2006 return t;
2008 case STATEMENT_LIST:
2009 error ("statement-expression in a constant expression");
2010 return error_mark_node;
2012 default:
2013 break;
2016 gcc_assert (EXPR_P (t));
2018 n = cp_tree_operand_length (t);
2019 ops = XALLOCAVEC (tree, n);
2020 type = TREE_TYPE (t);
2022 switch (code)
2024 CASE_CONVERT:
2025 case IMPLICIT_CONV_EXPR:
2026 case DYNAMIC_CAST_EXPR:
2027 case STATIC_CAST_EXPR:
2028 case CONST_CAST_EXPR:
2029 case REINTERPRET_CAST_EXPR:
2030 case CAST_EXPR:
2031 case NEW_EXPR:
2032 type = strip_typedefs (type, remove_attributes, flags);
2033 /* fallthrough */
2035 default:
2036 for (i = 0; i < n; ++i)
2037 ops[i] = strip_typedefs_expr (TREE_OPERAND (t, i),
2038 remove_attributes, flags);
2039 break;
2042 /* If nothing changed, return t. */
2043 for (i = 0; i < n; ++i)
2044 if (ops[i] != TREE_OPERAND (t, i))
2045 break;
2046 if (i == n && type == TREE_TYPE (t))
2047 return t;
2049 r = copy_node (t);
2050 TREE_TYPE (r) = type;
2051 for (i = 0; i < n; ++i)
2052 TREE_OPERAND (r, i) = ops[i];
2053 return r;
2056 /* Makes a copy of BINFO and TYPE, which is to be inherited into a
2057 graph dominated by T. If BINFO is NULL, TYPE is a dependent base,
2058 and we do a shallow copy. If BINFO is non-NULL, we do a deep copy.
2059 VIRT indicates whether TYPE is inherited virtually or not.
2060 IGO_PREV points at the previous binfo of the inheritance graph
2061 order chain. The newly copied binfo's TREE_CHAIN forms this
2062 ordering.
2064 The CLASSTYPE_VBASECLASSES vector of T is constructed in the
2065 correct order. That is in the order the bases themselves should be
2066 constructed in.
2068 The BINFO_INHERITANCE of a virtual base class points to the binfo
2069 of the most derived type. ??? We could probably change this so that
2070 BINFO_INHERITANCE becomes synonymous with BINFO_PRIMARY, and hence
2071 remove a field. They currently can only differ for primary virtual
2072 virtual bases. */
2074 tree
2075 copy_binfo (tree binfo, tree type, tree t, tree *igo_prev, int virt)
2077 tree new_binfo;
2079 if (virt)
2081 /* See if we've already made this virtual base. */
2082 new_binfo = binfo_for_vbase (type, t);
2083 if (new_binfo)
2084 return new_binfo;
2087 new_binfo = make_tree_binfo (binfo ? BINFO_N_BASE_BINFOS (binfo) : 0);
2088 BINFO_TYPE (new_binfo) = type;
2090 /* Chain it into the inheritance graph. */
2091 TREE_CHAIN (*igo_prev) = new_binfo;
2092 *igo_prev = new_binfo;
2094 if (binfo && !BINFO_DEPENDENT_BASE_P (binfo))
2096 int ix;
2097 tree base_binfo;
2099 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), type));
2101 BINFO_OFFSET (new_binfo) = BINFO_OFFSET (binfo);
2102 BINFO_VIRTUALS (new_binfo) = BINFO_VIRTUALS (binfo);
2104 /* We do not need to copy the accesses, as they are read only. */
2105 BINFO_BASE_ACCESSES (new_binfo) = BINFO_BASE_ACCESSES (binfo);
2107 /* Recursively copy base binfos of BINFO. */
2108 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
2110 tree new_base_binfo;
2111 new_base_binfo = copy_binfo (base_binfo, BINFO_TYPE (base_binfo),
2112 t, igo_prev,
2113 BINFO_VIRTUAL_P (base_binfo));
2115 if (!BINFO_INHERITANCE_CHAIN (new_base_binfo))
2116 BINFO_INHERITANCE_CHAIN (new_base_binfo) = new_binfo;
2117 BINFO_BASE_APPEND (new_binfo, new_base_binfo);
2120 else
2121 BINFO_DEPENDENT_BASE_P (new_binfo) = 1;
2123 if (virt)
2125 /* Push it onto the list after any virtual bases it contains
2126 will have been pushed. */
2127 CLASSTYPE_VBASECLASSES (t)->quick_push (new_binfo);
2128 BINFO_VIRTUAL_P (new_binfo) = 1;
2129 BINFO_INHERITANCE_CHAIN (new_binfo) = TYPE_BINFO (t);
2132 return new_binfo;
2135 /* Hashing of lists so that we don't make duplicates.
2136 The entry point is `list_hash_canon'. */
2138 struct list_proxy
2140 tree purpose;
2141 tree value;
2142 tree chain;
2145 struct list_hasher : ggc_ptr_hash<tree_node>
2147 typedef list_proxy *compare_type;
2149 static hashval_t hash (tree);
2150 static bool equal (tree, list_proxy *);
2153 /* Now here is the hash table. When recording a list, it is added
2154 to the slot whose index is the hash code mod the table size.
2155 Note that the hash table is used for several kinds of lists.
2156 While all these live in the same table, they are completely independent,
2157 and the hash code is computed differently for each of these. */
2159 static GTY (()) hash_table<list_hasher> *list_hash_table;
2161 /* Compare ENTRY (an entry in the hash table) with DATA (a list_proxy
2162 for a node we are thinking about adding). */
2164 bool
2165 list_hasher::equal (tree t, list_proxy *proxy)
2167 return (TREE_VALUE (t) == proxy->value
2168 && TREE_PURPOSE (t) == proxy->purpose
2169 && TREE_CHAIN (t) == proxy->chain);
2172 /* Compute a hash code for a list (chain of TREE_LIST nodes
2173 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
2174 TREE_COMMON slots), by adding the hash codes of the individual entries. */
2176 static hashval_t
2177 list_hash_pieces (tree purpose, tree value, tree chain)
2179 hashval_t hashcode = 0;
2181 if (chain)
2182 hashcode += TREE_HASH (chain);
2184 if (value)
2185 hashcode += TREE_HASH (value);
2186 else
2187 hashcode += 1007;
2188 if (purpose)
2189 hashcode += TREE_HASH (purpose);
2190 else
2191 hashcode += 1009;
2192 return hashcode;
2195 /* Hash an already existing TREE_LIST. */
2197 hashval_t
2198 list_hasher::hash (tree t)
2200 return list_hash_pieces (TREE_PURPOSE (t),
2201 TREE_VALUE (t),
2202 TREE_CHAIN (t));
2205 /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
2206 object for an identical list if one already exists. Otherwise, build a
2207 new one, and record it as the canonical object. */
2209 tree
2210 hash_tree_cons (tree purpose, tree value, tree chain)
2212 int hashcode = 0;
2213 tree *slot;
2214 struct list_proxy proxy;
2216 /* Hash the list node. */
2217 hashcode = list_hash_pieces (purpose, value, chain);
2218 /* Create a proxy for the TREE_LIST we would like to create. We
2219 don't actually create it so as to avoid creating garbage. */
2220 proxy.purpose = purpose;
2221 proxy.value = value;
2222 proxy.chain = chain;
2223 /* See if it is already in the table. */
2224 slot = list_hash_table->find_slot_with_hash (&proxy, hashcode, INSERT);
2225 /* If not, create a new node. */
2226 if (!*slot)
2227 *slot = tree_cons (purpose, value, chain);
2228 return (tree) *slot;
2231 /* Constructor for hashed lists. */
2233 tree
2234 hash_tree_chain (tree value, tree chain)
2236 return hash_tree_cons (NULL_TREE, value, chain);
2239 void
2240 debug_binfo (tree elem)
2242 HOST_WIDE_INT n;
2243 tree virtuals;
2245 fprintf (stderr, "type \"%s\", offset = " HOST_WIDE_INT_PRINT_DEC
2246 "\nvtable type:\n",
2247 TYPE_NAME_STRING (BINFO_TYPE (elem)),
2248 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
2249 debug_tree (BINFO_TYPE (elem));
2250 if (BINFO_VTABLE (elem))
2251 fprintf (stderr, "vtable decl \"%s\"\n",
2252 IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem))));
2253 else
2254 fprintf (stderr, "no vtable decl yet\n");
2255 fprintf (stderr, "virtuals:\n");
2256 virtuals = BINFO_VIRTUALS (elem);
2257 n = 0;
2259 while (virtuals)
2261 tree fndecl = TREE_VALUE (virtuals);
2262 fprintf (stderr, "%s [%ld =? %ld]\n",
2263 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
2264 (long) n, (long) TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
2265 ++n;
2266 virtuals = TREE_CHAIN (virtuals);
2270 /* Build a representation for the qualified name SCOPE::NAME. TYPE is
2271 the type of the result expression, if known, or NULL_TREE if the
2272 resulting expression is type-dependent. If TEMPLATE_P is true,
2273 NAME is known to be a template because the user explicitly used the
2274 "template" keyword after the "::".
2276 All SCOPE_REFs should be built by use of this function. */
2278 tree
2279 build_qualified_name (tree type, tree scope, tree name, bool template_p)
2281 tree t;
2282 if (type == error_mark_node
2283 || scope == error_mark_node
2284 || name == error_mark_node)
2285 return error_mark_node;
2286 gcc_assert (TREE_CODE (name) != SCOPE_REF);
2287 t = build2 (SCOPE_REF, type, scope, name);
2288 QUALIFIED_NAME_IS_TEMPLATE (t) = template_p;
2289 PTRMEM_OK_P (t) = true;
2290 if (type)
2291 t = convert_from_reference (t);
2292 return t;
2295 /* Like check_qualified_type, but also check ref-qualifier, exception
2296 specification, and whether the return type was specified after the
2297 parameters. */
2299 static bool
2300 cp_check_qualified_type (const_tree cand, const_tree base, int type_quals,
2301 cp_ref_qualifier rqual, tree raises, bool late)
2303 return (TYPE_QUALS (cand) == type_quals
2304 && check_base_type (cand, base)
2305 && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (cand),
2306 ce_exact)
2307 && TYPE_HAS_LATE_RETURN_TYPE (cand) == late
2308 && type_memfn_rqual (cand) == rqual);
2311 /* Build the FUNCTION_TYPE or METHOD_TYPE with the ref-qualifier RQUAL. */
2313 tree
2314 build_ref_qualified_type (tree type, cp_ref_qualifier rqual)
2316 tree raises = TYPE_RAISES_EXCEPTIONS (type);
2317 bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
2318 return build_cp_fntype_variant (type, rqual, raises, late);
2321 tree
2322 make_binding_vec (tree name, unsigned clusters MEM_STAT_DECL)
2324 /* Stored in an unsigned short, but we're limited to the number of
2325 modules anyway. */
2326 gcc_checking_assert (clusters <= (unsigned short)(~0));
2327 size_t length = (offsetof (tree_binding_vec, vec)
2328 + clusters * sizeof (binding_cluster));
2329 tree vec = ggc_alloc_cleared_tree_node_stat (length PASS_MEM_STAT);
2330 TREE_SET_CODE (vec, BINDING_VECTOR);
2331 BINDING_VECTOR_NAME (vec) = name;
2332 BINDING_VECTOR_ALLOC_CLUSTERS (vec) = clusters;
2333 BINDING_VECTOR_NUM_CLUSTERS (vec) = 0;
2335 return vec;
2338 /* Make a raw overload node containing FN. */
2340 tree
2341 ovl_make (tree fn, tree next)
2343 tree result = make_node (OVERLOAD);
2345 if (TREE_CODE (fn) == OVERLOAD)
2346 OVL_NESTED_P (result) = true;
2348 TREE_TYPE (result) = (next || TREE_CODE (fn) == TEMPLATE_DECL
2349 ? unknown_type_node : TREE_TYPE (fn));
2350 if (next && TREE_CODE (next) == OVERLOAD && OVL_DEDUP_P (next))
2351 OVL_DEDUP_P (result) = true;
2352 OVL_FUNCTION (result) = fn;
2353 OVL_CHAIN (result) = next;
2354 return result;
2357 /* Add FN to the (potentially NULL) overload set OVL. USING_OR_HIDDEN is >
2358 zero if this is a using-decl. It is > 1 if we're exporting the
2359 using decl. USING_OR_HIDDEN is < 0, if FN is hidden. (A decl
2360 cannot be both using and hidden.) We keep the hidden decls first,
2361 but remaining ones are unordered. */
2363 tree
2364 ovl_insert (tree fn, tree maybe_ovl, int using_or_hidden)
2366 tree result = maybe_ovl;
2367 tree insert_after = NULL_TREE;
2369 /* Skip hidden. */
2370 for (; maybe_ovl && TREE_CODE (maybe_ovl) == OVERLOAD
2371 && OVL_HIDDEN_P (maybe_ovl);
2372 maybe_ovl = OVL_CHAIN (maybe_ovl))
2374 gcc_checking_assert (!OVL_LOOKUP_P (maybe_ovl));
2375 insert_after = maybe_ovl;
2378 if (maybe_ovl || using_or_hidden || TREE_CODE (fn) == TEMPLATE_DECL)
2380 maybe_ovl = ovl_make (fn, maybe_ovl);
2382 if (using_or_hidden < 0)
2383 OVL_HIDDEN_P (maybe_ovl) = true;
2384 if (using_or_hidden > 0)
2386 OVL_DEDUP_P (maybe_ovl) = OVL_USING_P (maybe_ovl) = true;
2387 if (using_or_hidden > 1)
2388 OVL_EXPORT_P (maybe_ovl) = true;
2391 else
2392 maybe_ovl = fn;
2394 if (insert_after)
2396 OVL_CHAIN (insert_after) = maybe_ovl;
2397 TREE_TYPE (insert_after) = unknown_type_node;
2399 else
2400 result = maybe_ovl;
2402 return result;
2405 /* Skip any hidden names at the beginning of OVL. */
2407 tree
2408 ovl_skip_hidden (tree ovl)
2410 while (ovl && TREE_CODE (ovl) == OVERLOAD && OVL_HIDDEN_P (ovl))
2411 ovl = OVL_CHAIN (ovl);
2413 return ovl;
2416 /* NODE is an OVL_HIDDEN_P node that is now revealed. */
2418 tree
2419 ovl_iterator::reveal_node (tree overload, tree node)
2421 /* We cannot have returned NODE as part of a lookup overload, so we
2422 don't have to worry about preserving that. */
2424 OVL_HIDDEN_P (node) = false;
2425 if (tree chain = OVL_CHAIN (node))
2426 if (TREE_CODE (chain) == OVERLOAD)
2428 if (OVL_HIDDEN_P (chain))
2430 /* The node needs moving, and the simplest way is to remove it
2431 and reinsert. */
2432 overload = remove_node (overload, node);
2433 overload = ovl_insert (OVL_FUNCTION (node), overload);
2435 else if (OVL_DEDUP_P (chain))
2436 OVL_DEDUP_P (node) = true;
2438 return overload;
2441 /* NODE is on the overloads of OVL. Remove it.
2442 The removed node is unaltered and may continue to be iterated
2443 from (i.e. it is safe to remove a node from an overload one is
2444 currently iterating over). */
2446 tree
2447 ovl_iterator::remove_node (tree overload, tree node)
2449 tree *slot = &overload;
2450 while (*slot != node)
2452 tree probe = *slot;
2453 gcc_checking_assert (!OVL_LOOKUP_P (probe));
2455 slot = &OVL_CHAIN (probe);
2458 /* Stitch out NODE. We don't have to worry about now making a
2459 singleton overload (and consequently maybe setting its type),
2460 because all uses of this function will be followed by inserting a
2461 new node that must follow the place we've cut this out from. */
2462 if (TREE_CODE (node) != OVERLOAD)
2463 /* Cloned inherited ctors don't mark themselves as via_using. */
2464 *slot = NULL_TREE;
2465 else
2466 *slot = OVL_CHAIN (node);
2468 return overload;
2471 /* Mark or unmark a lookup set. */
2473 void
2474 lookup_mark (tree ovl, bool val)
2476 for (lkp_iterator iter (ovl); iter; ++iter)
2478 gcc_checking_assert (LOOKUP_SEEN_P (*iter) != val);
2479 LOOKUP_SEEN_P (*iter) = val;
2483 /* Add a set of new FNS into a lookup. */
2485 tree
2486 lookup_add (tree fns, tree lookup)
2488 if (fns == error_mark_node || lookup == error_mark_node)
2489 return error_mark_node;
2491 if (lookup || TREE_CODE (fns) == TEMPLATE_DECL)
2493 lookup = ovl_make (fns, lookup);
2494 OVL_LOOKUP_P (lookup) = true;
2496 else
2497 lookup = fns;
2499 return lookup;
2502 /* FNS is a new overload set, add them to LOOKUP, if they are not
2503 already present there. */
2505 tree
2506 lookup_maybe_add (tree fns, tree lookup, bool deduping)
2508 if (deduping)
2509 for (tree next, probe = fns; probe; probe = next)
2511 tree fn = probe;
2512 next = NULL_TREE;
2514 if (TREE_CODE (probe) == OVERLOAD)
2516 fn = OVL_FUNCTION (probe);
2517 next = OVL_CHAIN (probe);
2520 if (!LOOKUP_SEEN_P (fn))
2521 LOOKUP_SEEN_P (fn) = true;
2522 else
2524 /* This function was already seen. Insert all the
2525 predecessors onto the lookup. */
2526 for (; fns != probe; fns = OVL_CHAIN (fns))
2528 lookup = lookup_add (OVL_FUNCTION (fns), lookup);
2529 /* Propagate OVL_USING, but OVL_HIDDEN &
2530 OVL_DEDUP_P don't matter. */
2531 if (OVL_USING_P (fns))
2532 OVL_USING_P (lookup) = true;
2535 /* And now skip this function. */
2536 fns = next;
2540 if (fns)
2541 /* We ended in a set of new functions. Add them all in one go. */
2542 lookup = lookup_add (fns, lookup);
2544 return lookup;
2547 /* Returns nonzero if X is an expression for a (possibly overloaded)
2548 function. If "f" is a function or function template, "f", "c->f",
2549 "c.f", "C::f", and "f<int>" will all be considered possibly
2550 overloaded functions. Returns 2 if the function is actually
2551 overloaded, i.e., if it is impossible to know the type of the
2552 function without performing overload resolution. */
2555 is_overloaded_fn (tree x)
2557 STRIP_ANY_LOCATION_WRAPPER (x);
2559 /* A baselink is also considered an overloaded function. */
2560 if (TREE_CODE (x) == OFFSET_REF
2561 || TREE_CODE (x) == COMPONENT_REF)
2562 x = TREE_OPERAND (x, 1);
2563 x = MAYBE_BASELINK_FUNCTIONS (x);
2564 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
2565 x = TREE_OPERAND (x, 0);
2567 if (DECL_FUNCTION_TEMPLATE_P (OVL_FIRST (x))
2568 || (TREE_CODE (x) == OVERLOAD && !OVL_SINGLE_P (x)))
2569 return 2;
2571 return OVL_P (x);
2574 /* X is the CALL_EXPR_FN of a CALL_EXPR. If X represents a dependent name
2575 (14.6.2), return the IDENTIFIER_NODE for that name. Otherwise, return
2576 NULL_TREE. */
2578 tree
2579 dependent_name (tree x)
2581 /* FIXME a dependent name must be unqualified, but this function doesn't
2582 distinguish between qualified and unqualified identifiers. */
2583 if (identifier_p (x))
2584 return x;
2585 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
2586 x = TREE_OPERAND (x, 0);
2587 if (OVL_P (x))
2588 return OVL_NAME (x);
2589 return NULL_TREE;
2592 /* Returns true iff X is an expression for an overloaded function
2593 whose type cannot be known without performing overload
2594 resolution. */
2596 bool
2597 really_overloaded_fn (tree x)
2599 return is_overloaded_fn (x) == 2;
2602 /* Get the overload set FROM refers to. Returns NULL if it's not an
2603 overload set. */
2605 tree
2606 maybe_get_fns (tree from)
2608 STRIP_ANY_LOCATION_WRAPPER (from);
2610 /* A baselink is also considered an overloaded function. */
2611 if (TREE_CODE (from) == OFFSET_REF
2612 || TREE_CODE (from) == COMPONENT_REF)
2613 from = TREE_OPERAND (from, 1);
2614 if (BASELINK_P (from))
2615 from = BASELINK_FUNCTIONS (from);
2616 if (TREE_CODE (from) == TEMPLATE_ID_EXPR)
2617 from = TREE_OPERAND (from, 0);
2619 if (OVL_P (from))
2620 return from;
2622 return NULL;
2625 /* FROM refers to an overload set. Return that set (or die). */
2627 tree
2628 get_fns (tree from)
2630 tree res = maybe_get_fns (from);
2632 gcc_assert (res);
2633 return res;
2636 /* Return the first function of the overload set FROM refers to. */
2638 tree
2639 get_first_fn (tree from)
2641 return OVL_FIRST (get_fns (from));
2644 /* Return the scope where the overloaded functions OVL were found. */
2646 tree
2647 ovl_scope (tree ovl)
2649 if (TREE_CODE (ovl) == OFFSET_REF
2650 || TREE_CODE (ovl) == COMPONENT_REF)
2651 ovl = TREE_OPERAND (ovl, 1);
2652 if (TREE_CODE (ovl) == BASELINK)
2653 return BINFO_TYPE (BASELINK_BINFO (ovl));
2654 if (TREE_CODE (ovl) == TEMPLATE_ID_EXPR)
2655 ovl = TREE_OPERAND (ovl, 0);
2656 /* Skip using-declarations. */
2657 lkp_iterator iter (ovl);
2659 ovl = *iter;
2660 while (iter.using_p () && ++iter);
2662 return CP_DECL_CONTEXT (ovl);
2665 #define PRINT_RING_SIZE 4
2667 static const char *
2668 cxx_printable_name_internal (tree decl, int v, bool translate)
2670 static unsigned int uid_ring[PRINT_RING_SIZE];
2671 static char *print_ring[PRINT_RING_SIZE];
2672 static bool trans_ring[PRINT_RING_SIZE];
2673 static int ring_counter;
2674 int i;
2676 /* Only cache functions. */
2677 if (v < 2
2678 || TREE_CODE (decl) != FUNCTION_DECL
2679 || DECL_LANG_SPECIFIC (decl) == 0)
2680 return lang_decl_name (decl, v, translate);
2682 /* See if this print name is lying around. */
2683 for (i = 0; i < PRINT_RING_SIZE; i++)
2684 if (uid_ring[i] == DECL_UID (decl) && translate == trans_ring[i])
2685 /* yes, so return it. */
2686 return print_ring[i];
2688 if (++ring_counter == PRINT_RING_SIZE)
2689 ring_counter = 0;
2691 if (current_function_decl != NULL_TREE)
2693 /* There may be both translated and untranslated versions of the
2694 name cached. */
2695 for (i = 0; i < 2; i++)
2697 if (uid_ring[ring_counter] == DECL_UID (current_function_decl))
2698 ring_counter += 1;
2699 if (ring_counter == PRINT_RING_SIZE)
2700 ring_counter = 0;
2702 gcc_assert (uid_ring[ring_counter] != DECL_UID (current_function_decl));
2705 free (print_ring[ring_counter]);
2707 print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v, translate));
2708 uid_ring[ring_counter] = DECL_UID (decl);
2709 trans_ring[ring_counter] = translate;
2710 return print_ring[ring_counter];
2713 const char *
2714 cxx_printable_name (tree decl, int v)
2716 return cxx_printable_name_internal (decl, v, false);
2719 const char *
2720 cxx_printable_name_translate (tree decl, int v)
2722 return cxx_printable_name_internal (decl, v, true);
2725 /* Return the canonical version of exception-specification RAISES for a C++17
2726 function type, for use in type comparison and building TYPE_CANONICAL. */
2728 tree
2729 canonical_eh_spec (tree raises)
2731 if (raises == NULL_TREE)
2732 return raises;
2733 else if (DEFERRED_NOEXCEPT_SPEC_P (raises)
2734 || UNPARSED_NOEXCEPT_SPEC_P (raises)
2735 || uses_template_parms (raises)
2736 || uses_template_parms (TREE_PURPOSE (raises)))
2737 /* Keep a dependent or deferred exception specification. */
2738 return raises;
2739 else if (nothrow_spec_p (raises))
2740 /* throw() -> noexcept. */
2741 return noexcept_true_spec;
2742 else
2743 /* For C++17 type matching, anything else -> nothing. */
2744 return NULL_TREE;
2747 tree
2748 build_cp_fntype_variant (tree type, cp_ref_qualifier rqual,
2749 tree raises, bool late)
2751 cp_cv_quals type_quals = TYPE_QUALS (type);
2753 if (cp_check_qualified_type (type, type, type_quals, rqual, raises, late))
2754 return type;
2756 tree v = TYPE_MAIN_VARIANT (type);
2757 for (; v; v = TYPE_NEXT_VARIANT (v))
2758 if (cp_check_qualified_type (v, type, type_quals, rqual, raises, late))
2759 return v;
2761 /* Need to build a new variant. */
2762 v = build_variant_type_copy (type);
2763 if (!TYPE_DEPENDENT_P (v))
2764 /* We no longer know that it's not type-dependent. */
2765 TYPE_DEPENDENT_P_VALID (v) = false;
2766 TYPE_RAISES_EXCEPTIONS (v) = raises;
2767 TYPE_HAS_LATE_RETURN_TYPE (v) = late;
2768 switch (rqual)
2770 case REF_QUAL_RVALUE:
2771 FUNCTION_RVALUE_QUALIFIED (v) = 1;
2772 FUNCTION_REF_QUALIFIED (v) = 1;
2773 break;
2774 case REF_QUAL_LVALUE:
2775 FUNCTION_RVALUE_QUALIFIED (v) = 0;
2776 FUNCTION_REF_QUALIFIED (v) = 1;
2777 break;
2778 default:
2779 FUNCTION_REF_QUALIFIED (v) = 0;
2780 break;
2783 /* Canonicalize the exception specification. */
2784 tree cr = flag_noexcept_type ? canonical_eh_spec (raises) : NULL_TREE;
2786 if (TYPE_STRUCTURAL_EQUALITY_P (type))
2787 /* Propagate structural equality. */
2788 SET_TYPE_STRUCTURAL_EQUALITY (v);
2789 else if (TYPE_CANONICAL (type) != type || cr != raises || late)
2790 /* Build the underlying canonical type, since it is different
2791 from TYPE. */
2792 TYPE_CANONICAL (v) = build_cp_fntype_variant (TYPE_CANONICAL (type),
2793 rqual, cr, false);
2794 else
2795 /* T is its own canonical type. */
2796 TYPE_CANONICAL (v) = v;
2798 return v;
2801 /* TYPE is a function or method type with a deferred exception
2802 specification that has been parsed to RAISES. Fixup all the type
2803 variants that are affected in place. Via decltype &| noexcept
2804 tricks, the unparsed spec could have escaped into the type system.
2805 The general case is hard to fixup canonical types for. */
2807 void
2808 fixup_deferred_exception_variants (tree type, tree raises)
2810 tree original = TYPE_RAISES_EXCEPTIONS (type);
2811 tree cr = flag_noexcept_type ? canonical_eh_spec (raises) : NULL_TREE;
2813 gcc_checking_assert (UNPARSED_NOEXCEPT_SPEC_P (original));
2815 /* Though sucky, this walk will process the canonical variants
2816 first. */
2817 tree prev = NULL_TREE;
2818 for (tree variant = TYPE_MAIN_VARIANT (type);
2819 variant; prev = variant, variant = TYPE_NEXT_VARIANT (variant))
2820 if (TYPE_RAISES_EXCEPTIONS (variant) == original)
2822 gcc_checking_assert (variant != TYPE_MAIN_VARIANT (type));
2824 if (!TYPE_STRUCTURAL_EQUALITY_P (variant))
2826 cp_cv_quals var_quals = TYPE_QUALS (variant);
2827 cp_ref_qualifier rqual = type_memfn_rqual (variant);
2829 /* If VARIANT would become a dup (cp_check_qualified_type-wise)
2830 of an existing variant in the variant list of TYPE after its
2831 exception specification has been parsed, elide it. Otherwise,
2832 build_cp_fntype_variant could use it, leading to "canonical
2833 types differ for identical types." */
2834 tree v = TYPE_MAIN_VARIANT (type);
2835 for (; v; v = TYPE_NEXT_VARIANT (v))
2836 if (cp_check_qualified_type (v, variant, var_quals,
2837 rqual, cr, false))
2839 /* The main variant will not match V, so PREV will never
2840 be null. */
2841 TYPE_NEXT_VARIANT (prev) = TYPE_NEXT_VARIANT (variant);
2842 break;
2844 TYPE_RAISES_EXCEPTIONS (variant) = raises;
2846 if (!v)
2847 v = build_cp_fntype_variant (TYPE_CANONICAL (variant),
2848 rqual, cr, false);
2849 TYPE_CANONICAL (variant) = TYPE_CANONICAL (v);
2851 else
2852 TYPE_RAISES_EXCEPTIONS (variant) = raises;
2854 if (!TYPE_DEPENDENT_P (variant))
2855 /* We no longer know that it's not type-dependent. */
2856 TYPE_DEPENDENT_P_VALID (variant) = false;
2860 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
2861 listed in RAISES. */
2863 tree
2864 build_exception_variant (tree type, tree raises)
2866 cp_ref_qualifier rqual = type_memfn_rqual (type);
2867 bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
2868 return build_cp_fntype_variant (type, rqual, raises, late);
2871 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new
2872 BOUND_TEMPLATE_TEMPLATE_PARM bound with NEWARGS as its template
2873 arguments. */
2875 tree
2876 bind_template_template_parm (tree t, tree newargs)
2878 tree decl = TYPE_NAME (t);
2879 tree t2;
2881 t2 = cxx_make_type (BOUND_TEMPLATE_TEMPLATE_PARM);
2882 decl = build_decl (input_location,
2883 TYPE_DECL, DECL_NAME (decl), NULL_TREE);
2884 SET_DECL_TEMPLATE_PARM_P (decl);
2886 /* These nodes have to be created to reflect new TYPE_DECL and template
2887 arguments. */
2888 TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t));
2889 TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl;
2890 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
2891 = build_template_info (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t), newargs);
2893 TREE_TYPE (decl) = t2;
2894 TYPE_NAME (t2) = decl;
2895 TYPE_STUB_DECL (t2) = decl;
2896 TYPE_SIZE (t2) = 0;
2897 SET_TYPE_STRUCTURAL_EQUALITY (t2);
2899 return t2;
2902 /* Called from count_trees via walk_tree. */
2904 static tree
2905 count_trees_r (tree *tp, int *walk_subtrees, void *data)
2907 ++*((int *) data);
2909 if (TYPE_P (*tp))
2910 *walk_subtrees = 0;
2912 return NULL_TREE;
2915 /* Debugging function for measuring the rough complexity of a tree
2916 representation. */
2919 count_trees (tree t)
2921 int n_trees = 0;
2922 cp_walk_tree_without_duplicates (&t, count_trees_r, &n_trees);
2923 return n_trees;
2926 /* Called from verify_stmt_tree via walk_tree. */
2928 static tree
2929 verify_stmt_tree_r (tree* tp, int * /*walk_subtrees*/, void* data)
2931 tree t = *tp;
2932 hash_table<nofree_ptr_hash <tree_node> > *statements
2933 = static_cast <hash_table<nofree_ptr_hash <tree_node> > *> (data);
2934 tree_node **slot;
2936 if (!STATEMENT_CODE_P (TREE_CODE (t)))
2937 return NULL_TREE;
2939 /* If this statement is already present in the hash table, then
2940 there is a circularity in the statement tree. */
2941 gcc_assert (!statements->find (t));
2943 slot = statements->find_slot (t, INSERT);
2944 *slot = t;
2946 return NULL_TREE;
2949 /* Debugging function to check that the statement T has not been
2950 corrupted. For now, this function simply checks that T contains no
2951 circularities. */
2953 void
2954 verify_stmt_tree (tree t)
2956 hash_table<nofree_ptr_hash <tree_node> > statements (37);
2957 cp_walk_tree (&t, verify_stmt_tree_r, &statements, NULL);
2960 /* Check if the type T depends on a type with no linkage and if so,
2961 return it. If RELAXED_P then do not consider a class type declared
2962 within a vague-linkage function to have no linkage. Remember:
2963 no-linkage is not the same as internal-linkage*/
2965 tree
2966 no_linkage_check (tree t, bool relaxed_p)
2968 tree r;
2970 /* Lambda types that don't have mangling scope have no linkage. We
2971 check CLASSTYPE_LAMBDA_EXPR for error_mark_node because
2972 when we get here from pushtag none of the lambda information is
2973 set up yet, so we want to assume that the lambda has linkage and
2974 fix it up later if not. We need to check this even in templates so
2975 that we properly handle a lambda-expression in the signature. */
2976 if (LAMBDA_TYPE_P (t)
2977 && CLASSTYPE_LAMBDA_EXPR (t) != error_mark_node)
2979 tree extra = LAMBDA_TYPE_EXTRA_SCOPE (t);
2980 if (!extra)
2981 return t;
2984 /* Otherwise there's no point in checking linkage on template functions; we
2985 can't know their complete types. */
2986 if (processing_template_decl)
2987 return NULL_TREE;
2989 switch (TREE_CODE (t))
2991 case RECORD_TYPE:
2992 if (TYPE_PTRMEMFUNC_P (t))
2993 goto ptrmem;
2994 /* Fall through. */
2995 case UNION_TYPE:
2996 if (!CLASS_TYPE_P (t))
2997 return NULL_TREE;
2998 /* Fall through. */
2999 case ENUMERAL_TYPE:
3000 /* Only treat unnamed types as having no linkage if they're at
3001 namespace scope. This is core issue 966. */
3002 if (TYPE_UNNAMED_P (t) && TYPE_NAMESPACE_SCOPE_P (t))
3003 return t;
3005 for (r = CP_TYPE_CONTEXT (t); ; )
3007 /* If we're a nested type of a !TREE_PUBLIC class, we might not
3008 have linkage, or we might just be in an anonymous namespace.
3009 If we're in a TREE_PUBLIC class, we have linkage. */
3010 if (TYPE_P (r) && !TREE_PUBLIC (TYPE_NAME (r)))
3011 return no_linkage_check (TYPE_CONTEXT (t), relaxed_p);
3012 else if (TREE_CODE (r) == FUNCTION_DECL)
3014 if (!relaxed_p || !vague_linkage_p (r))
3015 return t;
3016 else
3017 r = CP_DECL_CONTEXT (r);
3019 else
3020 break;
3023 return NULL_TREE;
3025 case ARRAY_TYPE:
3026 case POINTER_TYPE:
3027 case REFERENCE_TYPE:
3028 case VECTOR_TYPE:
3029 return no_linkage_check (TREE_TYPE (t), relaxed_p);
3031 case OFFSET_TYPE:
3032 ptrmem:
3033 r = no_linkage_check (TYPE_PTRMEM_POINTED_TO_TYPE (t),
3034 relaxed_p);
3035 if (r)
3036 return r;
3037 return no_linkage_check (TYPE_PTRMEM_CLASS_TYPE (t), relaxed_p);
3039 case METHOD_TYPE:
3040 case FUNCTION_TYPE:
3042 tree parm = TYPE_ARG_TYPES (t);
3043 if (TREE_CODE (t) == METHOD_TYPE)
3044 /* The 'this' pointer isn't interesting; a method has the same
3045 linkage (or lack thereof) as its enclosing class. */
3046 parm = TREE_CHAIN (parm);
3047 for (;
3048 parm && parm != void_list_node;
3049 parm = TREE_CHAIN (parm))
3051 r = no_linkage_check (TREE_VALUE (parm), relaxed_p);
3052 if (r)
3053 return r;
3055 return no_linkage_check (TREE_TYPE (t), relaxed_p);
3058 default:
3059 return NULL_TREE;
3063 extern int depth_reached;
3065 void
3066 cxx_print_statistics (void)
3068 print_template_statistics ();
3069 if (GATHER_STATISTICS)
3070 fprintf (stderr, "maximum template instantiation depth reached: %d\n",
3071 depth_reached);
3074 /* Return, as an INTEGER_CST node, the number of elements for TYPE
3075 (which is an ARRAY_TYPE). This counts only elements of the top
3076 array. */
3078 tree
3079 array_type_nelts_top (tree type)
3081 return fold_build2_loc (input_location,
3082 PLUS_EXPR, sizetype,
3083 array_type_nelts (type),
3084 size_one_node);
3087 /* Return, as an INTEGER_CST node, the number of elements for TYPE
3088 (which is an ARRAY_TYPE). This one is a recursive count of all
3089 ARRAY_TYPEs that are clumped together. */
3091 tree
3092 array_type_nelts_total (tree type)
3094 tree sz = array_type_nelts_top (type);
3095 type = TREE_TYPE (type);
3096 while (TREE_CODE (type) == ARRAY_TYPE)
3098 tree n = array_type_nelts_top (type);
3099 sz = fold_build2_loc (input_location,
3100 MULT_EXPR, sizetype, sz, n);
3101 type = TREE_TYPE (type);
3103 return sz;
3106 /* Return true if FNDECL is std::source_location::current () method. */
3108 bool
3109 source_location_current_p (tree fndecl)
3111 gcc_checking_assert (TREE_CODE (fndecl) == FUNCTION_DECL
3112 && DECL_IMMEDIATE_FUNCTION_P (fndecl));
3113 if (DECL_NAME (fndecl) == NULL_TREE
3114 || TREE_CODE (TREE_TYPE (fndecl)) != FUNCTION_TYPE
3115 || TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != RECORD_TYPE
3116 || DECL_CONTEXT (fndecl) != TREE_TYPE (TREE_TYPE (fndecl))
3117 || !id_equal (DECL_NAME (fndecl), "current"))
3118 return false;
3120 tree source_location = DECL_CONTEXT (fndecl);
3121 if (TYPE_NAME (source_location) == NULL_TREE
3122 || TREE_CODE (TYPE_NAME (source_location)) != TYPE_DECL
3123 || TYPE_IDENTIFIER (source_location) == NULL_TREE
3124 || !id_equal (TYPE_IDENTIFIER (source_location),
3125 "source_location")
3126 || !decl_in_std_namespace_p (TYPE_NAME (source_location)))
3127 return false;
3129 return true;
3132 struct bot_data
3134 splay_tree target_remap;
3135 bool clear_location;
3138 /* Called from break_out_target_exprs via mapcar. */
3140 static tree
3141 bot_manip (tree* tp, int* walk_subtrees, void* data_)
3143 bot_data &data = *(bot_data*)data_;
3144 splay_tree target_remap = data.target_remap;
3145 tree t = *tp;
3147 if (!TYPE_P (t) && TREE_CONSTANT (t) && !TREE_SIDE_EFFECTS (t))
3149 /* There can't be any TARGET_EXPRs or their slot variables below this
3150 point. But we must make a copy, in case subsequent processing
3151 alters any part of it. For example, during gimplification a cast
3152 of the form (T) &X::f (where "f" is a member function) will lead
3153 to replacing the PTRMEM_CST for &X::f with a VAR_DECL. */
3154 *walk_subtrees = 0;
3155 *tp = unshare_expr (t);
3156 return NULL_TREE;
3158 if (TREE_CODE (t) == TARGET_EXPR)
3160 tree u;
3162 if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
3164 u = build_cplus_new (TREE_TYPE (t), TREE_OPERAND (t, 1),
3165 tf_warning_or_error);
3166 if (u == error_mark_node)
3167 return u;
3168 if (AGGR_INIT_ZERO_FIRST (TREE_OPERAND (t, 1)))
3169 AGGR_INIT_ZERO_FIRST (TREE_OPERAND (u, 1)) = true;
3171 else
3172 u = force_target_expr (TREE_TYPE (t), TREE_OPERAND (t, 1),
3173 tf_warning_or_error);
3175 TARGET_EXPR_IMPLICIT_P (u) = TARGET_EXPR_IMPLICIT_P (t);
3176 TARGET_EXPR_LIST_INIT_P (u) = TARGET_EXPR_LIST_INIT_P (t);
3177 TARGET_EXPR_DIRECT_INIT_P (u) = TARGET_EXPR_DIRECT_INIT_P (t);
3179 /* Map the old variable to the new one. */
3180 splay_tree_insert (target_remap,
3181 (splay_tree_key) TREE_OPERAND (t, 0),
3182 (splay_tree_value) TREE_OPERAND (u, 0));
3184 TREE_OPERAND (u, 1) = break_out_target_exprs (TREE_OPERAND (u, 1),
3185 data.clear_location);
3186 if (TREE_OPERAND (u, 1) == error_mark_node)
3187 return error_mark_node;
3189 /* Replace the old expression with the new version. */
3190 *tp = u;
3191 /* We don't have to go below this point; the recursive call to
3192 break_out_target_exprs will have handled anything below this
3193 point. */
3194 *walk_subtrees = 0;
3195 return NULL_TREE;
3197 if (TREE_CODE (*tp) == SAVE_EXPR)
3199 t = *tp;
3200 splay_tree_node n = splay_tree_lookup (target_remap,
3201 (splay_tree_key) t);
3202 if (n)
3204 *tp = (tree)n->value;
3205 *walk_subtrees = 0;
3207 else
3209 copy_tree_r (tp, walk_subtrees, NULL);
3210 splay_tree_insert (target_remap,
3211 (splay_tree_key)t,
3212 (splay_tree_value)*tp);
3213 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
3214 splay_tree_insert (target_remap,
3215 (splay_tree_key)*tp,
3216 (splay_tree_value)*tp);
3218 return NULL_TREE;
3220 if (TREE_CODE (*tp) == DECL_EXPR
3221 && VAR_P (DECL_EXPR_DECL (*tp))
3222 && DECL_ARTIFICIAL (DECL_EXPR_DECL (*tp))
3223 && !TREE_STATIC (DECL_EXPR_DECL (*tp)))
3225 tree t;
3226 splay_tree_node n
3227 = splay_tree_lookup (target_remap,
3228 (splay_tree_key) DECL_EXPR_DECL (*tp));
3229 if (n)
3230 t = (tree) n->value;
3231 else
3233 t = create_temporary_var (TREE_TYPE (DECL_EXPR_DECL (*tp)));
3234 DECL_INITIAL (t) = DECL_INITIAL (DECL_EXPR_DECL (*tp));
3235 splay_tree_insert (target_remap,
3236 (splay_tree_key) DECL_EXPR_DECL (*tp),
3237 (splay_tree_value) t);
3239 copy_tree_r (tp, walk_subtrees, NULL);
3240 DECL_EXPR_DECL (*tp) = t;
3241 if (data.clear_location && EXPR_HAS_LOCATION (*tp))
3242 SET_EXPR_LOCATION (*tp, input_location);
3243 return NULL_TREE;
3245 if (TREE_CODE (*tp) == BIND_EXPR && BIND_EXPR_VARS (*tp))
3247 copy_tree_r (tp, walk_subtrees, NULL);
3248 for (tree *p = &BIND_EXPR_VARS (*tp); *p; p = &DECL_CHAIN (*p))
3250 gcc_assert (VAR_P (*p) && DECL_ARTIFICIAL (*p) && !TREE_STATIC (*p));
3251 tree t = create_temporary_var (TREE_TYPE (*p));
3252 DECL_INITIAL (t) = DECL_INITIAL (*p);
3253 DECL_CHAIN (t) = DECL_CHAIN (*p);
3254 splay_tree_insert (target_remap, (splay_tree_key) *p,
3255 (splay_tree_value) t);
3256 *p = t;
3258 if (data.clear_location && EXPR_HAS_LOCATION (*tp))
3259 SET_EXPR_LOCATION (*tp, input_location);
3260 return NULL_TREE;
3263 /* Make a copy of this node. */
3264 t = copy_tree_r (tp, walk_subtrees, NULL);
3265 if (TREE_CODE (*tp) == CALL_EXPR || TREE_CODE (*tp) == AGGR_INIT_EXPR)
3266 if (!processing_template_decl)
3267 set_flags_from_callee (*tp);
3268 if (data.clear_location && EXPR_HAS_LOCATION (*tp))
3269 SET_EXPR_LOCATION (*tp, input_location);
3270 return t;
3273 /* Replace all remapped VAR_DECLs in T with their new equivalents.
3274 DATA is really a splay-tree mapping old variables to new
3275 variables. */
3277 static tree
3278 bot_replace (tree* t, int* /*walk_subtrees*/, void* data_)
3280 bot_data &data = *(bot_data*)data_;
3281 splay_tree target_remap = data.target_remap;
3283 if (VAR_P (*t))
3285 splay_tree_node n = splay_tree_lookup (target_remap,
3286 (splay_tree_key) *t);
3287 if (n)
3288 *t = (tree) n->value;
3290 else if (TREE_CODE (*t) == PARM_DECL
3291 && DECL_NAME (*t) == this_identifier
3292 && !DECL_CONTEXT (*t))
3294 /* In an NSDMI we need to replace the 'this' parameter we used for
3295 parsing with the real one for this function. */
3296 *t = current_class_ptr;
3298 else if (TREE_CODE (*t) == CONVERT_EXPR
3299 && CONVERT_EXPR_VBASE_PATH (*t))
3301 /* In an NSDMI build_base_path defers building conversions to morally
3302 virtual bases, and we handle it here. */
3303 tree basetype = TREE_TYPE (*t);
3304 *t = convert_to_base (TREE_OPERAND (*t, 0), basetype,
3305 /*check_access=*/false, /*nonnull=*/true,
3306 tf_warning_or_error);
3309 return NULL_TREE;
3312 /* When we parse a default argument expression, we may create
3313 temporary variables via TARGET_EXPRs. When we actually use the
3314 default-argument expression, we make a copy of the expression
3315 and replace the temporaries with appropriate local versions.
3317 If CLEAR_LOCATION is true, override any EXPR_LOCATION with
3318 input_location. */
3320 tree
3321 break_out_target_exprs (tree t, bool clear_location /* = false */)
3323 static int target_remap_count;
3324 static splay_tree target_remap;
3326 if (!target_remap_count++)
3327 target_remap = splay_tree_new (splay_tree_compare_pointers,
3328 /*splay_tree_delete_key_fn=*/NULL,
3329 /*splay_tree_delete_value_fn=*/NULL);
3330 bot_data data = { target_remap, clear_location };
3331 if (cp_walk_tree (&t, bot_manip, &data, NULL) == error_mark_node)
3332 t = error_mark_node;
3333 cp_walk_tree (&t, bot_replace, &data, NULL);
3335 if (!--target_remap_count)
3337 splay_tree_delete (target_remap);
3338 target_remap = NULL;
3341 return t;
3344 /* Build an expression for the subobject of OBJ at CONSTRUCTOR index INDEX,
3345 which we expect to have type TYPE. */
3347 tree
3348 build_ctor_subob_ref (tree index, tree type, tree obj)
3350 if (index == NULL_TREE)
3351 /* Can't refer to a particular member of a vector. */
3352 obj = NULL_TREE;
3353 else if (TREE_CODE (index) == INTEGER_CST)
3354 obj = cp_build_array_ref (input_location, obj, index, tf_none);
3355 else
3356 obj = build_class_member_access_expr (obj, index, NULL_TREE,
3357 /*reference*/false, tf_none);
3358 if (obj)
3360 tree objtype = TREE_TYPE (obj);
3361 if (TREE_CODE (objtype) == ARRAY_TYPE && !TYPE_DOMAIN (objtype))
3363 /* When the destination object refers to a flexible array member
3364 verify that it matches the type of the source object except
3365 for its domain and qualifiers. */
3366 gcc_assert (comptypes (TYPE_MAIN_VARIANT (type),
3367 TYPE_MAIN_VARIANT (objtype),
3368 COMPARE_REDECLARATION));
3370 else
3371 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, objtype));
3374 return obj;
3377 struct replace_placeholders_t
3379 tree obj; /* The object to be substituted for a PLACEHOLDER_EXPR. */
3380 tree exp; /* The outermost exp. */
3381 bool seen; /* Whether we've encountered a PLACEHOLDER_EXPR. */
3382 hash_set<tree> *pset; /* To avoid walking same trees multiple times. */
3385 /* Like substitute_placeholder_in_expr, but handle C++ tree codes and
3386 build up subexpressions as we go deeper. */
3388 static tree
3389 replace_placeholders_r (tree* t, int* walk_subtrees, void* data_)
3391 replace_placeholders_t *d = static_cast<replace_placeholders_t*>(data_);
3392 tree obj = d->obj;
3394 if (TYPE_P (*t) || TREE_CONSTANT (*t))
3396 *walk_subtrees = false;
3397 return NULL_TREE;
3400 switch (TREE_CODE (*t))
3402 case PLACEHOLDER_EXPR:
3404 tree x = obj;
3405 for (; !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (*t),
3406 TREE_TYPE (x));
3407 x = TREE_OPERAND (x, 0))
3408 gcc_assert (handled_component_p (x));
3409 *t = unshare_expr (x);
3410 *walk_subtrees = false;
3411 d->seen = true;
3413 break;
3415 case CONSTRUCTOR:
3417 constructor_elt *ce;
3418 vec<constructor_elt,va_gc> *v = CONSTRUCTOR_ELTS (*t);
3419 /* Don't walk into CONSTRUCTOR_PLACEHOLDER_BOUNDARY ctors
3420 other than the d->exp one, those have PLACEHOLDER_EXPRs
3421 related to another object. */
3422 if ((CONSTRUCTOR_PLACEHOLDER_BOUNDARY (*t)
3423 && *t != d->exp)
3424 || d->pset->add (*t))
3426 *walk_subtrees = false;
3427 return NULL_TREE;
3429 for (unsigned i = 0; vec_safe_iterate (v, i, &ce); ++i)
3431 tree *valp = &ce->value;
3432 tree type = TREE_TYPE (*valp);
3433 tree subob = obj;
3435 /* Elements with RANGE_EXPR index shouldn't have any
3436 placeholders in them. */
3437 if (ce->index && TREE_CODE (ce->index) == RANGE_EXPR)
3438 continue;
3440 if (TREE_CODE (*valp) == CONSTRUCTOR
3441 && AGGREGATE_TYPE_P (type))
3443 /* If we're looking at the initializer for OBJ, then build
3444 a sub-object reference. If we're looking at an
3445 initializer for another object, just pass OBJ down. */
3446 if (same_type_ignoring_top_level_qualifiers_p
3447 (TREE_TYPE (*t), TREE_TYPE (obj)))
3448 subob = build_ctor_subob_ref (ce->index, type, obj);
3449 if (TREE_CODE (*valp) == TARGET_EXPR)
3450 valp = &TARGET_EXPR_INITIAL (*valp);
3452 d->obj = subob;
3453 cp_walk_tree (valp, replace_placeholders_r, data_, NULL);
3454 d->obj = obj;
3456 *walk_subtrees = false;
3457 break;
3460 default:
3461 if (d->pset->add (*t))
3462 *walk_subtrees = false;
3463 break;
3466 return NULL_TREE;
3469 /* Replace PLACEHOLDER_EXPRs in EXP with object OBJ. SEEN_P is set if
3470 a PLACEHOLDER_EXPR has been encountered. */
3472 tree
3473 replace_placeholders (tree exp, tree obj, bool *seen_p /*= NULL*/)
3475 /* This is only relevant for C++14. */
3476 if (cxx_dialect < cxx14)
3477 return exp;
3479 /* If the object isn't a (member of a) class, do nothing. */
3480 tree op0 = obj;
3481 while (handled_component_p (op0))
3482 op0 = TREE_OPERAND (op0, 0);
3483 if (!CLASS_TYPE_P (strip_array_types (TREE_TYPE (op0))))
3484 return exp;
3486 tree *tp = &exp;
3487 if (TREE_CODE (exp) == TARGET_EXPR)
3488 tp = &TARGET_EXPR_INITIAL (exp);
3489 hash_set<tree> pset;
3490 replace_placeholders_t data = { obj, *tp, false, &pset };
3491 cp_walk_tree (tp, replace_placeholders_r, &data, NULL);
3492 if (seen_p)
3493 *seen_p = data.seen;
3494 return exp;
3497 /* Callback function for find_placeholders. */
3499 static tree
3500 find_placeholders_r (tree *t, int *walk_subtrees, void *)
3502 if (TYPE_P (*t) || TREE_CONSTANT (*t))
3504 *walk_subtrees = false;
3505 return NULL_TREE;
3508 switch (TREE_CODE (*t))
3510 case PLACEHOLDER_EXPR:
3511 return *t;
3513 case CONSTRUCTOR:
3514 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (*t))
3515 *walk_subtrees = false;
3516 break;
3518 default:
3519 break;
3522 return NULL_TREE;
3525 /* Return true if EXP contains a PLACEHOLDER_EXPR. Don't walk into
3526 ctors with CONSTRUCTOR_PLACEHOLDER_BOUNDARY flag set. */
3528 bool
3529 find_placeholders (tree exp)
3531 /* This is only relevant for C++14. */
3532 if (cxx_dialect < cxx14)
3533 return false;
3535 return cp_walk_tree_without_duplicates (&exp, find_placeholders_r, NULL);
3538 /* Similar to `build_nt', but for template definitions of dependent
3539 expressions */
3541 tree
3542 build_min_nt_loc (location_t loc, enum tree_code code, ...)
3544 tree t;
3545 int length;
3546 int i;
3547 va_list p;
3549 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3551 va_start (p, code);
3553 t = make_node (code);
3554 SET_EXPR_LOCATION (t, loc);
3555 length = TREE_CODE_LENGTH (code);
3557 for (i = 0; i < length; i++)
3558 TREE_OPERAND (t, i) = va_arg (p, tree);
3560 va_end (p);
3561 return t;
3564 /* Similar to `build', but for template definitions. */
3566 tree
3567 build_min (enum tree_code code, tree tt, ...)
3569 tree t;
3570 int length;
3571 int i;
3572 va_list p;
3574 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3576 va_start (p, tt);
3578 t = make_node (code);
3579 length = TREE_CODE_LENGTH (code);
3580 TREE_TYPE (t) = tt;
3582 for (i = 0; i < length; i++)
3584 tree x = va_arg (p, tree);
3585 TREE_OPERAND (t, i) = x;
3586 if (x && !TYPE_P (x) && TREE_SIDE_EFFECTS (x))
3587 TREE_SIDE_EFFECTS (t) = 1;
3590 va_end (p);
3592 return t;
3595 /* Similar to `build', but for template definitions of non-dependent
3596 expressions. NON_DEP is the non-dependent expression that has been
3597 built. */
3599 tree
3600 build_min_non_dep (enum tree_code code, tree non_dep, ...)
3602 tree t;
3603 int length;
3604 int i;
3605 va_list p;
3607 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3609 va_start (p, non_dep);
3611 if (REFERENCE_REF_P (non_dep))
3612 non_dep = TREE_OPERAND (non_dep, 0);
3614 t = make_node (code);
3615 SET_EXPR_LOCATION (t, cp_expr_loc_or_input_loc (non_dep));
3616 length = TREE_CODE_LENGTH (code);
3617 TREE_TYPE (t) = unlowered_expr_type (non_dep);
3618 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
3620 for (i = 0; i < length; i++)
3621 TREE_OPERAND (t, i) = va_arg (p, tree);
3623 va_end (p);
3624 return convert_from_reference (t);
3627 /* Similar to build_min_nt, but call expressions */
3629 tree
3630 build_min_nt_call_vec (tree fn, vec<tree, va_gc> *args)
3632 tree ret, t;
3633 unsigned int ix;
3635 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
3636 CALL_EXPR_FN (ret) = fn;
3637 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
3638 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
3639 CALL_EXPR_ARG (ret, ix) = t;
3641 return ret;
3644 /* Similar to `build_min_nt_call_vec', but for template definitions of
3645 non-dependent expressions. NON_DEP is the non-dependent expression
3646 that has been built. */
3648 tree
3649 build_min_non_dep_call_vec (tree non_dep, tree fn, vec<tree, va_gc> *argvec)
3651 tree t = build_min_nt_call_vec (fn, argvec);
3652 if (REFERENCE_REF_P (non_dep))
3653 non_dep = TREE_OPERAND (non_dep, 0);
3654 TREE_TYPE (t) = TREE_TYPE (non_dep);
3655 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
3656 return convert_from_reference (t);
3659 /* Similar to build_min_non_dep, but for expressions that have been resolved to
3660 a call to an operator overload. OP is the operator that has been
3661 overloaded. NON_DEP is the non-dependent expression that's been built,
3662 which should be a CALL_EXPR or an INDIRECT_REF to a CALL_EXPR. OVERLOAD is
3663 the overload that NON_DEP is calling. */
3665 tree
3666 build_min_non_dep_op_overload (enum tree_code op,
3667 tree non_dep,
3668 tree overload, ...)
3670 va_list p;
3671 int nargs, expected_nargs;
3672 tree fn, call;
3674 non_dep = extract_call_expr (non_dep);
3676 nargs = call_expr_nargs (non_dep);
3678 expected_nargs = cp_tree_code_length (op);
3679 if (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE)
3680 expected_nargs -= 1;
3681 if ((op == POSTINCREMENT_EXPR
3682 || op == POSTDECREMENT_EXPR)
3683 /* With -fpermissive non_dep could be operator++(). */
3684 && (!flag_permissive || nargs != expected_nargs))
3685 expected_nargs += 1;
3686 gcc_assert (nargs == expected_nargs);
3688 releasing_vec args;
3689 va_start (p, overload);
3691 if (TREE_CODE (TREE_TYPE (overload)) == FUNCTION_TYPE)
3693 fn = overload;
3694 for (int i = 0; i < nargs; i++)
3696 tree arg = va_arg (p, tree);
3697 vec_safe_push (args, arg);
3700 else if (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE)
3702 tree object = va_arg (p, tree);
3703 tree binfo = TYPE_BINFO (TREE_TYPE (object));
3704 tree method = build_baselink (binfo, binfo, overload, NULL_TREE);
3705 fn = build_min (COMPONENT_REF, TREE_TYPE (overload),
3706 object, method, NULL_TREE);
3707 for (int i = 0; i < nargs; i++)
3709 tree arg = va_arg (p, tree);
3710 vec_safe_push (args, arg);
3713 else
3714 gcc_unreachable ();
3716 va_end (p);
3717 call = build_min_non_dep_call_vec (non_dep, fn, args);
3719 tree call_expr = extract_call_expr (call);
3720 KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep);
3721 CALL_EXPR_OPERATOR_SYNTAX (call_expr) = true;
3722 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep);
3723 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep);
3725 return call;
3728 /* Similar to above build_min_non_dep_op_overload, but arguments
3729 are taken from ARGS vector. */
3731 tree
3732 build_min_non_dep_op_overload (tree non_dep, tree overload, tree object,
3733 vec<tree, va_gc> *args)
3735 non_dep = extract_call_expr (non_dep);
3737 unsigned int nargs = call_expr_nargs (non_dep);
3738 gcc_assert (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE);
3739 tree binfo = TYPE_BINFO (TREE_TYPE (object));
3740 tree method = build_baselink (binfo, binfo, overload, NULL_TREE);
3741 tree fn = build_min (COMPONENT_REF, TREE_TYPE (overload),
3742 object, method, NULL_TREE);
3743 gcc_assert (vec_safe_length (args) == nargs);
3745 tree call = build_min_non_dep_call_vec (non_dep, fn, args);
3747 tree call_expr = extract_call_expr (call);
3748 KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep);
3749 CALL_EXPR_OPERATOR_SYNTAX (call_expr) = true;
3750 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep);
3751 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep);
3753 return call;
3756 /* Return a new tree vec copied from VEC, with ELT inserted at index IDX. */
3758 vec<tree, va_gc> *
3759 vec_copy_and_insert (vec<tree, va_gc> *old_vec, tree elt, unsigned idx)
3761 unsigned len = vec_safe_length (old_vec);
3762 gcc_assert (idx <= len);
3764 vec<tree, va_gc> *new_vec = NULL;
3765 vec_alloc (new_vec, len + 1);
3767 unsigned i;
3768 for (i = 0; i < len; ++i)
3770 if (i == idx)
3771 new_vec->quick_push (elt);
3772 new_vec->quick_push ((*old_vec)[i]);
3774 if (i == idx)
3775 new_vec->quick_push (elt);
3777 return new_vec;
3780 tree
3781 get_type_decl (tree t)
3783 if (TREE_CODE (t) == TYPE_DECL)
3784 return t;
3785 if (TYPE_P (t))
3786 return TYPE_STUB_DECL (t);
3787 gcc_assert (t == error_mark_node);
3788 return t;
3791 /* Returns the namespace that contains DECL, whether directly or
3792 indirectly. */
3794 tree
3795 decl_namespace_context (tree decl)
3797 while (1)
3799 if (TREE_CODE (decl) == NAMESPACE_DECL)
3800 return decl;
3801 else if (TYPE_P (decl))
3802 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
3803 else
3804 decl = CP_DECL_CONTEXT (decl);
3808 /* Returns true if decl is within an anonymous namespace, however deeply
3809 nested, or false otherwise. */
3811 bool
3812 decl_anon_ns_mem_p (const_tree decl)
3814 while (TREE_CODE (decl) != NAMESPACE_DECL)
3816 /* Classes inside anonymous namespaces have TREE_PUBLIC == 0. */
3817 if (TYPE_P (decl))
3818 return !TREE_PUBLIC (TYPE_MAIN_DECL (decl));
3820 decl = CP_DECL_CONTEXT (decl);
3822 return !TREE_PUBLIC (decl);
3825 /* Subroutine of cp_tree_equal: t1 and t2 are the CALL_EXPR_FNs of two
3826 CALL_EXPRS. Return whether they are equivalent. */
3828 static bool
3829 called_fns_equal (tree t1, tree t2)
3831 /* Core 1321: dependent names are equivalent even if the overload sets
3832 are different. But do compare explicit template arguments. */
3833 tree name1 = dependent_name (t1);
3834 tree name2 = dependent_name (t2);
3835 if (name1 || name2)
3837 tree targs1 = NULL_TREE, targs2 = NULL_TREE;
3839 if (name1 != name2)
3840 return false;
3842 /* FIXME dependent_name currently returns an unqualified name regardless
3843 of whether the function was named with a qualified- or unqualified-id.
3844 Until that's fixed, check that we aren't looking at overload sets from
3845 different scopes. */
3846 if (is_overloaded_fn (t1) && is_overloaded_fn (t2)
3847 && (DECL_CONTEXT (get_first_fn (t1))
3848 != DECL_CONTEXT (get_first_fn (t2))))
3849 return false;
3851 if (TREE_CODE (t1) == TEMPLATE_ID_EXPR)
3852 targs1 = TREE_OPERAND (t1, 1);
3853 if (TREE_CODE (t2) == TEMPLATE_ID_EXPR)
3854 targs2 = TREE_OPERAND (t2, 1);
3855 return cp_tree_equal (targs1, targs2);
3857 else
3858 return cp_tree_equal (t1, t2);
3861 /* Return truthvalue of whether T1 is the same tree structure as T2.
3862 Return 1 if they are the same. Return 0 if they are different. */
3864 bool
3865 cp_tree_equal (tree t1, tree t2)
3867 enum tree_code code1, code2;
3869 if (t1 == t2)
3870 return true;
3871 if (!t1 || !t2)
3872 return false;
3874 code1 = TREE_CODE (t1);
3875 code2 = TREE_CODE (t2);
3877 if (code1 != code2)
3878 return false;
3880 if (CONSTANT_CLASS_P (t1)
3881 && !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3882 return false;
3884 switch (code1)
3886 case VOID_CST:
3887 /* There's only a single VOID_CST node, so we should never reach
3888 here. */
3889 gcc_unreachable ();
3891 case INTEGER_CST:
3892 return tree_int_cst_equal (t1, t2);
3894 case REAL_CST:
3895 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
3897 case STRING_CST:
3898 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3899 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3900 TREE_STRING_LENGTH (t1));
3902 case FIXED_CST:
3903 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
3904 TREE_FIXED_CST (t2));
3906 case COMPLEX_CST:
3907 return cp_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
3908 && cp_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
3910 case VECTOR_CST:
3911 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
3913 case CONSTRUCTOR:
3914 /* We need to do this when determining whether or not two
3915 non-type pointer to member function template arguments
3916 are the same. */
3917 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
3918 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
3919 return false;
3921 tree field, value;
3922 unsigned int i;
3923 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
3925 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
3926 if (!cp_tree_equal (field, elt2->index)
3927 || !cp_tree_equal (value, elt2->value))
3928 return false;
3931 return true;
3933 case TREE_LIST:
3934 if (!cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
3935 return false;
3936 if (!cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
3937 return false;
3938 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
3940 case SAVE_EXPR:
3941 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3943 case CALL_EXPR:
3945 if (KOENIG_LOOKUP_P (t1) != KOENIG_LOOKUP_P (t2))
3946 return false;
3948 if (!called_fns_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
3949 return false;
3951 call_expr_arg_iterator iter1, iter2;
3952 init_call_expr_arg_iterator (t1, &iter1);
3953 init_call_expr_arg_iterator (t2, &iter2);
3954 if (iter1.n != iter2.n)
3955 return false;
3957 while (more_call_expr_args_p (&iter1))
3959 tree arg1 = next_call_expr_arg (&iter1);
3960 tree arg2 = next_call_expr_arg (&iter2);
3962 gcc_checking_assert (arg1 && arg2);
3963 if (!cp_tree_equal (arg1, arg2))
3964 return false;
3967 return true;
3970 case TARGET_EXPR:
3972 tree o1 = TREE_OPERAND (t1, 0);
3973 tree o2 = TREE_OPERAND (t2, 0);
3975 /* Special case: if either target is an unallocated VAR_DECL,
3976 it means that it's going to be unified with whatever the
3977 TARGET_EXPR is really supposed to initialize, so treat it
3978 as being equivalent to anything. */
3979 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
3980 && !DECL_RTL_SET_P (o1))
3981 /*Nop*/;
3982 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
3983 && !DECL_RTL_SET_P (o2))
3984 /*Nop*/;
3985 else if (!cp_tree_equal (o1, o2))
3986 return false;
3988 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3991 case PARM_DECL:
3992 /* For comparing uses of parameters in late-specified return types
3993 with an out-of-class definition of the function, but can also come
3994 up for expressions that involve 'this' in a member function
3995 template. */
3997 if (comparing_specializations
3998 && DECL_CONTEXT (t1) != DECL_CONTEXT (t2))
3999 /* When comparing hash table entries, only an exact match is
4000 good enough; we don't want to replace 'this' with the
4001 version from another function. But be more flexible
4002 with parameters with identical contexts. */
4003 return false;
4005 if (same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
4007 if (DECL_ARTIFICIAL (t1) ^ DECL_ARTIFICIAL (t2))
4008 return false;
4009 if (CONSTRAINT_VAR_P (t1) ^ CONSTRAINT_VAR_P (t2))
4010 return false;
4011 if (DECL_ARTIFICIAL (t1)
4012 || (DECL_PARM_LEVEL (t1) == DECL_PARM_LEVEL (t2)
4013 && DECL_PARM_INDEX (t1) == DECL_PARM_INDEX (t2)))
4014 return true;
4016 return false;
4018 case VAR_DECL:
4019 case CONST_DECL:
4020 case FIELD_DECL:
4021 case FUNCTION_DECL:
4022 case TEMPLATE_DECL:
4023 case IDENTIFIER_NODE:
4024 case SSA_NAME:
4025 case USING_DECL:
4026 case DEFERRED_PARSE:
4027 return false;
4029 case BASELINK:
4030 return (BASELINK_BINFO (t1) == BASELINK_BINFO (t2)
4031 && BASELINK_ACCESS_BINFO (t1) == BASELINK_ACCESS_BINFO (t2)
4032 && BASELINK_QUALIFIED_P (t1) == BASELINK_QUALIFIED_P (t2)
4033 && cp_tree_equal (BASELINK_FUNCTIONS (t1),
4034 BASELINK_FUNCTIONS (t2)));
4036 case TEMPLATE_PARM_INDEX:
4037 return (TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
4038 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2)
4039 && (TEMPLATE_PARM_PARAMETER_PACK (t1)
4040 == TEMPLATE_PARM_PARAMETER_PACK (t2))
4041 && same_type_p (TREE_TYPE (TEMPLATE_PARM_DECL (t1)),
4042 TREE_TYPE (TEMPLATE_PARM_DECL (t2))));
4044 case TEMPLATE_ID_EXPR:
4045 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
4046 return false;
4047 if (!comp_template_args (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)))
4048 return false;
4049 return true;
4051 case CONSTRAINT_INFO:
4052 return cp_tree_equal (CI_ASSOCIATED_CONSTRAINTS (t1),
4053 CI_ASSOCIATED_CONSTRAINTS (t2));
4055 case CHECK_CONSTR:
4056 return (CHECK_CONSTR_CONCEPT (t1) == CHECK_CONSTR_CONCEPT (t2)
4057 && comp_template_args (CHECK_CONSTR_ARGS (t1),
4058 CHECK_CONSTR_ARGS (t2)));
4060 case TREE_VEC:
4061 /* These are template args. Really we should be getting the
4062 caller to do this as it knows it to be true. */
4063 if (!comp_template_args (t1, t2, NULL, NULL, false))
4064 return false;
4065 return true;
4067 case SIZEOF_EXPR:
4068 case ALIGNOF_EXPR:
4070 tree o1 = TREE_OPERAND (t1, 0);
4071 tree o2 = TREE_OPERAND (t2, 0);
4073 if (code1 == SIZEOF_EXPR)
4075 if (SIZEOF_EXPR_TYPE_P (t1))
4076 o1 = TREE_TYPE (o1);
4077 if (SIZEOF_EXPR_TYPE_P (t2))
4078 o2 = TREE_TYPE (o2);
4080 else if (ALIGNOF_EXPR_STD_P (t1) != ALIGNOF_EXPR_STD_P (t2))
4081 return false;
4083 if (TREE_CODE (o1) != TREE_CODE (o2))
4084 return false;
4086 if (ARGUMENT_PACK_P (o1))
4087 return template_args_equal (o1, o2);
4088 else if (TYPE_P (o1))
4089 return same_type_p (o1, o2);
4090 else
4091 return cp_tree_equal (o1, o2);
4094 case MODOP_EXPR:
4096 tree t1_op1, t2_op1;
4098 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
4099 return false;
4101 t1_op1 = TREE_OPERAND (t1, 1);
4102 t2_op1 = TREE_OPERAND (t2, 1);
4103 if (TREE_CODE (t1_op1) != TREE_CODE (t2_op1))
4104 return false;
4106 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t2, 2));
4109 case PTRMEM_CST:
4110 /* Two pointer-to-members are the same if they point to the same
4111 field or function in the same class. */
4112 if (PTRMEM_CST_MEMBER (t1) != PTRMEM_CST_MEMBER (t2))
4113 return false;
4115 return same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2));
4117 case OVERLOAD:
4119 /* Two overloads. Must be exactly the same set of decls. */
4120 lkp_iterator first (t1);
4121 lkp_iterator second (t2);
4123 for (; first && second; ++first, ++second)
4124 if (*first != *second)
4125 return false;
4126 return !(first || second);
4129 case TRAIT_EXPR:
4130 if (TRAIT_EXPR_KIND (t1) != TRAIT_EXPR_KIND (t2))
4131 return false;
4132 return same_type_p (TRAIT_EXPR_TYPE1 (t1), TRAIT_EXPR_TYPE1 (t2))
4133 && cp_tree_equal (TRAIT_EXPR_TYPE2 (t1), TRAIT_EXPR_TYPE2 (t2));
4135 case NON_LVALUE_EXPR:
4136 case VIEW_CONVERT_EXPR:
4137 /* Used for location wrappers with possibly NULL types. */
4138 if (!TREE_TYPE (t1) || !TREE_TYPE (t2))
4140 if (TREE_TYPE (t1) || TREE_TYPE (t2))
4141 return false;
4142 break;
4144 /* FALLTHROUGH */
4146 case CAST_EXPR:
4147 case STATIC_CAST_EXPR:
4148 case REINTERPRET_CAST_EXPR:
4149 case CONST_CAST_EXPR:
4150 case DYNAMIC_CAST_EXPR:
4151 case IMPLICIT_CONV_EXPR:
4152 case NEW_EXPR:
4153 case BIT_CAST_EXPR:
4154 CASE_CONVERT:
4155 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
4156 return false;
4157 /* Now compare operands as usual. */
4158 break;
4160 case DEFERRED_NOEXCEPT:
4161 return (cp_tree_equal (DEFERRED_NOEXCEPT_PATTERN (t1),
4162 DEFERRED_NOEXCEPT_PATTERN (t2))
4163 && comp_template_args (DEFERRED_NOEXCEPT_ARGS (t1),
4164 DEFERRED_NOEXCEPT_ARGS (t2)));
4166 case LAMBDA_EXPR:
4167 /* Two lambda-expressions are never considered equivalent. */
4168 return false;
4170 case TYPE_ARGUMENT_PACK:
4171 case NONTYPE_ARGUMENT_PACK:
4173 tree p1 = ARGUMENT_PACK_ARGS (t1);
4174 tree p2 = ARGUMENT_PACK_ARGS (t2);
4175 int len = TREE_VEC_LENGTH (p1);
4176 if (TREE_VEC_LENGTH (p2) != len)
4177 return false;
4179 for (int ix = 0; ix != len; ix++)
4180 if (!template_args_equal (TREE_VEC_ELT (p1, ix),
4181 TREE_VEC_ELT (p2, ix)))
4182 return false;
4183 return true;
4186 case EXPR_PACK_EXPANSION:
4187 if (!cp_tree_equal (PACK_EXPANSION_PATTERN (t1),
4188 PACK_EXPANSION_PATTERN (t2)))
4189 return false;
4190 if (!comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
4191 PACK_EXPANSION_EXTRA_ARGS (t2)))
4192 return false;
4193 return true;
4195 default:
4196 break;
4199 switch (TREE_CODE_CLASS (code1))
4201 case tcc_unary:
4202 case tcc_binary:
4203 case tcc_comparison:
4204 case tcc_expression:
4205 case tcc_vl_exp:
4206 case tcc_reference:
4207 case tcc_statement:
4209 int n = cp_tree_operand_length (t1);
4210 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
4211 && n != TREE_OPERAND_LENGTH (t2))
4212 return false;
4214 for (int i = 0; i < n; ++i)
4215 if (!cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
4216 return false;
4218 return true;
4221 case tcc_type:
4222 return same_type_p (t1, t2);
4224 default:
4225 gcc_unreachable ();
4228 /* We can get here with --disable-checking. */
4229 return false;
4232 /* The type of ARG when used as an lvalue. */
4234 tree
4235 lvalue_type (tree arg)
4237 tree type = TREE_TYPE (arg);
4238 return type;
4241 /* The type of ARG for printing error messages; denote lvalues with
4242 reference types. */
4244 tree
4245 error_type (tree arg)
4247 tree type = TREE_TYPE (arg);
4249 if (TREE_CODE (type) == ARRAY_TYPE)
4251 else if (TREE_CODE (type) == ERROR_MARK)
4253 else if (lvalue_p (arg))
4254 type = build_reference_type (lvalue_type (arg));
4255 else if (MAYBE_CLASS_TYPE_P (type))
4256 type = lvalue_type (arg);
4258 return type;
4261 /* Does FUNCTION use a variable-length argument list? */
4264 varargs_function_p (const_tree function)
4266 return stdarg_p (TREE_TYPE (function));
4269 /* Returns 1 if decl is a member of a class. */
4272 member_p (const_tree decl)
4274 const_tree const ctx = DECL_CONTEXT (decl);
4275 return (ctx && TYPE_P (ctx));
4278 /* Create a placeholder for member access where we don't actually have an
4279 object that the access is against. For a general declval<T> equivalent,
4280 use build_stub_object instead. */
4282 tree
4283 build_dummy_object (tree type)
4285 tree decl = build1 (CONVERT_EXPR, build_pointer_type (type), void_node);
4286 return cp_build_fold_indirect_ref (decl);
4289 /* We've gotten a reference to a member of TYPE. Return *this if appropriate,
4290 or a dummy object otherwise. If BINFOP is non-0, it is filled with the
4291 binfo path from current_class_type to TYPE, or 0. */
4293 tree
4294 maybe_dummy_object (tree type, tree* binfop)
4296 tree decl, context;
4297 tree binfo;
4298 tree current = current_nonlambda_class_type ();
4300 if (current
4301 && (binfo = lookup_base (current, type, ba_any, NULL,
4302 tf_warning_or_error)))
4303 context = current;
4304 else
4306 /* Reference from a nested class member function. */
4307 context = type;
4308 binfo = TYPE_BINFO (type);
4311 if (binfop)
4312 *binfop = binfo;
4314 if (current_class_ref
4315 /* current_class_ref might not correspond to current_class_type if
4316 we're in tsubst_default_argument or a lambda-declarator; in either
4317 case, we want to use current_class_ref if it matches CONTEXT. */
4318 && (same_type_ignoring_top_level_qualifiers_p
4319 (TREE_TYPE (current_class_ref), context)))
4320 decl = current_class_ref;
4321 else
4322 decl = build_dummy_object (context);
4324 return decl;
4327 /* Returns 1 if OB is a placeholder object, or a pointer to one. */
4329 bool
4330 is_dummy_object (const_tree ob)
4332 if (INDIRECT_REF_P (ob))
4333 ob = TREE_OPERAND (ob, 0);
4334 return (TREE_CODE (ob) == CONVERT_EXPR
4335 && TREE_OPERAND (ob, 0) == void_node);
4338 /* Returns true if TYPE is char, unsigned char, or std::byte. */
4340 bool
4341 is_byte_access_type (tree type)
4343 type = TYPE_MAIN_VARIANT (type);
4344 if (type == char_type_node
4345 || type == unsigned_char_type_node)
4346 return true;
4348 return (TREE_CODE (type) == ENUMERAL_TYPE
4349 && TYPE_CONTEXT (type) == std_node
4350 && !strcmp ("byte", TYPE_NAME_STRING (type)));
4353 /* Returns true if TYPE is unsigned char or std::byte. */
4355 bool
4356 is_byte_access_type_not_plain_char (tree type)
4358 type = TYPE_MAIN_VARIANT (type);
4359 if (type == char_type_node)
4360 return false;
4362 return is_byte_access_type (type);
4365 /* Returns 1 iff type T is something we want to treat as a scalar type for
4366 the purpose of deciding whether it is trivial/POD/standard-layout. */
4368 bool
4369 scalarish_type_p (const_tree t)
4371 if (t == error_mark_node)
4372 return 1;
4374 return (SCALAR_TYPE_P (t) || VECTOR_TYPE_P (t));
4377 /* Returns true iff T requires non-trivial default initialization. */
4379 bool
4380 type_has_nontrivial_default_init (const_tree t)
4382 t = strip_array_types (CONST_CAST_TREE (t));
4384 if (CLASS_TYPE_P (t))
4385 return TYPE_HAS_COMPLEX_DFLT (t);
4386 else
4387 return 0;
4390 /* Track classes with only deleted copy/move constructors so that we can warn
4391 if they are used in call/return by value. */
4393 static GTY(()) hash_set<tree>* deleted_copy_types;
4394 static void
4395 remember_deleted_copy (const_tree t)
4397 if (!deleted_copy_types)
4398 deleted_copy_types = hash_set<tree>::create_ggc(37);
4399 deleted_copy_types->add (CONST_CAST_TREE (t));
4401 void
4402 maybe_warn_parm_abi (tree t, location_t loc)
4404 if (!deleted_copy_types
4405 || !deleted_copy_types->contains (t))
4406 return;
4408 if ((flag_abi_version == 12 || warn_abi_version == 12)
4409 && classtype_has_non_deleted_move_ctor (t))
4411 bool w;
4412 auto_diagnostic_group d;
4413 if (flag_abi_version > 12)
4414 w = warning_at (loc, OPT_Wabi, "%<-fabi-version=13%> (GCC 8.2) fixes "
4415 "the calling convention for %qT, which was "
4416 "accidentally changed in 8.1", t);
4417 else
4418 w = warning_at (loc, OPT_Wabi, "%<-fabi-version=12%> (GCC 8.1) "
4419 "accidentally changes the calling convention for %qT",
4421 if (w)
4422 inform (location_of (t), " declared here");
4423 return;
4426 auto_diagnostic_group d;
4427 if (warning_at (loc, OPT_Wabi, "the calling convention for %qT changes in "
4428 "%<-fabi-version=13%> (GCC 8.2)", t))
4429 inform (location_of (t), " because all of its copy and move "
4430 "constructors are deleted");
4433 /* Returns true iff copying an object of type T (including via move
4434 constructor) is non-trivial. That is, T has no non-trivial copy
4435 constructors and no non-trivial move constructors, and not all copy/move
4436 constructors are deleted. This function implements the ABI notion of
4437 non-trivial copy, which has diverged from the one in the standard. */
4439 bool
4440 type_has_nontrivial_copy_init (const_tree type)
4442 tree t = strip_array_types (CONST_CAST_TREE (type));
4444 if (CLASS_TYPE_P (t))
4446 gcc_assert (COMPLETE_TYPE_P (t));
4448 if (TYPE_HAS_COMPLEX_COPY_CTOR (t)
4449 || TYPE_HAS_COMPLEX_MOVE_CTOR (t))
4450 /* Nontrivial. */
4451 return true;
4453 if (cxx_dialect < cxx11)
4454 /* No deleted functions before C++11. */
4455 return false;
4457 /* Before ABI v12 we did a bitwise copy of types with only deleted
4458 copy/move constructors. */
4459 if (!abi_version_at_least (12)
4460 && !(warn_abi && abi_version_crosses (12)))
4461 return false;
4463 bool saw_copy = false;
4464 bool saw_non_deleted = false;
4465 bool saw_non_deleted_move = false;
4467 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
4468 saw_copy = saw_non_deleted = true;
4469 else if (CLASSTYPE_LAZY_COPY_CTOR (t))
4471 saw_copy = true;
4472 if (classtype_has_move_assign_or_move_ctor_p (t, true))
4473 /* [class.copy]/8 If the class definition declares a move
4474 constructor or move assignment operator, the implicitly declared
4475 copy constructor is defined as deleted.... */;
4476 else
4477 /* Any other reason the implicitly-declared function would be
4478 deleted would also cause TYPE_HAS_COMPLEX_COPY_CTOR to be
4479 set. */
4480 saw_non_deleted = true;
4483 if (!saw_non_deleted)
4484 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4486 tree fn = *iter;
4487 if (copy_fn_p (fn))
4489 saw_copy = true;
4490 if (!DECL_DELETED_FN (fn))
4492 /* Not deleted, therefore trivial. */
4493 saw_non_deleted = true;
4494 break;
4497 else if (move_fn_p (fn))
4498 if (!DECL_DELETED_FN (fn))
4499 saw_non_deleted_move = true;
4502 gcc_assert (saw_copy);
4504 /* ABI v12 buggily ignored move constructors. */
4505 bool v11nontriv = false;
4506 bool v12nontriv = !saw_non_deleted;
4507 bool v13nontriv = !saw_non_deleted && !saw_non_deleted_move;
4508 bool nontriv = (abi_version_at_least (13) ? v13nontriv
4509 : flag_abi_version == 12 ? v12nontriv
4510 : v11nontriv);
4511 bool warn_nontriv = (warn_abi_version >= 13 ? v13nontriv
4512 : warn_abi_version == 12 ? v12nontriv
4513 : v11nontriv);
4514 if (nontriv != warn_nontriv)
4515 remember_deleted_copy (t);
4517 return nontriv;
4519 else
4520 return 0;
4523 /* Returns 1 iff type T is a trivially copyable type, as defined in
4524 [basic.types] and [class]. */
4526 bool
4527 trivially_copyable_p (const_tree t)
4529 t = strip_array_types (CONST_CAST_TREE (t));
4531 if (CLASS_TYPE_P (t))
4532 return ((!TYPE_HAS_COPY_CTOR (t)
4533 || !TYPE_HAS_COMPLEX_COPY_CTOR (t))
4534 && !TYPE_HAS_COMPLEX_MOVE_CTOR (t)
4535 && (!TYPE_HAS_COPY_ASSIGN (t)
4536 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (t))
4537 && !TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
4538 && TYPE_HAS_TRIVIAL_DESTRUCTOR (t));
4539 else
4540 /* CWG 2094 makes volatile-qualified scalars trivially copyable again. */
4541 return scalarish_type_p (t);
4544 /* Returns 1 iff type T is a trivial type, as defined in [basic.types] and
4545 [class]. */
4547 bool
4548 trivial_type_p (const_tree t)
4550 t = strip_array_types (CONST_CAST_TREE (t));
4552 if (CLASS_TYPE_P (t))
4553 return (TYPE_HAS_TRIVIAL_DFLT (t)
4554 && trivially_copyable_p (t));
4555 else
4556 return scalarish_type_p (t);
4559 /* Returns 1 iff type T is a POD type, as defined in [basic.types]. */
4561 bool
4562 pod_type_p (const_tree t)
4564 /* This CONST_CAST is okay because strip_array_types returns its
4565 argument unmodified and we assign it to a const_tree. */
4566 t = strip_array_types (CONST_CAST_TREE(t));
4568 if (!CLASS_TYPE_P (t))
4569 return scalarish_type_p (t);
4570 else if (cxx_dialect > cxx98)
4571 /* [class]/10: A POD struct is a class that is both a trivial class and a
4572 standard-layout class, and has no non-static data members of type
4573 non-POD struct, non-POD union (or array of such types).
4575 We don't need to check individual members because if a member is
4576 non-std-layout or non-trivial, the class will be too. */
4577 return (std_layout_type_p (t) && trivial_type_p (t));
4578 else
4579 /* The C++98 definition of POD is different. */
4580 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
4583 /* Returns true iff T is POD for the purpose of layout, as defined in the
4584 C++ ABI. */
4586 bool
4587 layout_pod_type_p (const_tree t)
4589 t = strip_array_types (CONST_CAST_TREE (t));
4591 if (CLASS_TYPE_P (t))
4592 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
4593 else
4594 return scalarish_type_p (t);
4597 /* Returns true iff T is a standard-layout type, as defined in
4598 [basic.types]. */
4600 bool
4601 std_layout_type_p (const_tree t)
4603 t = strip_array_types (CONST_CAST_TREE (t));
4605 if (CLASS_TYPE_P (t))
4606 return !CLASSTYPE_NON_STD_LAYOUT (t);
4607 else
4608 return scalarish_type_p (t);
4611 static bool record_has_unique_obj_representations (const_tree, const_tree);
4613 /* Returns true iff T satisfies std::has_unique_object_representations<T>,
4614 as defined in [meta.unary.prop]. */
4616 bool
4617 type_has_unique_obj_representations (const_tree t)
4619 bool ret;
4621 t = strip_array_types (CONST_CAST_TREE (t));
4623 if (!trivially_copyable_p (t))
4624 return false;
4626 if (CLASS_TYPE_P (t) && CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t))
4627 return CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t);
4629 switch (TREE_CODE (t))
4631 case INTEGER_TYPE:
4632 case POINTER_TYPE:
4633 case REFERENCE_TYPE:
4634 /* If some backend has any paddings in these types, we should add
4635 a target hook for this and handle it there. */
4636 return true;
4638 case BOOLEAN_TYPE:
4639 /* For bool values other than 0 and 1 should only appear with
4640 undefined behavior. */
4641 return true;
4643 case ENUMERAL_TYPE:
4644 return type_has_unique_obj_representations (ENUM_UNDERLYING_TYPE (t));
4646 case REAL_TYPE:
4647 /* XFmode certainly contains padding on x86, which the CPU doesn't store
4648 when storing long double values, so for that we have to return false.
4649 Other kinds of floating point values are questionable due to +.0/-.0
4650 and NaNs, let's play safe for now. */
4651 return false;
4653 case FIXED_POINT_TYPE:
4654 return false;
4656 case OFFSET_TYPE:
4657 return true;
4659 case COMPLEX_TYPE:
4660 case VECTOR_TYPE:
4661 return type_has_unique_obj_representations (TREE_TYPE (t));
4663 case RECORD_TYPE:
4664 ret = record_has_unique_obj_representations (t, TYPE_SIZE (t));
4665 if (CLASS_TYPE_P (t))
4667 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
4668 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
4670 return ret;
4672 case UNION_TYPE:
4673 ret = true;
4674 bool any_fields;
4675 any_fields = false;
4676 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4677 if (TREE_CODE (field) == FIELD_DECL)
4679 any_fields = true;
4680 if (!type_has_unique_obj_representations (TREE_TYPE (field))
4681 || simple_cst_equal (DECL_SIZE (field), TYPE_SIZE (t)) != 1)
4683 ret = false;
4684 break;
4687 if (!any_fields && !integer_zerop (TYPE_SIZE (t)))
4688 ret = false;
4689 if (CLASS_TYPE_P (t))
4691 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
4692 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
4694 return ret;
4696 case NULLPTR_TYPE:
4697 return false;
4699 case ERROR_MARK:
4700 return false;
4702 default:
4703 gcc_unreachable ();
4707 /* Helper function for type_has_unique_obj_representations. */
4709 static bool
4710 record_has_unique_obj_representations (const_tree t, const_tree sz)
4712 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4713 if (TREE_CODE (field) != FIELD_DECL)
4715 /* For bases, can't use type_has_unique_obj_representations here, as in
4716 struct S { int i : 24; S (); };
4717 struct T : public S { int j : 8; T (); };
4718 S doesn't have unique obj representations, but T does. */
4719 else if (DECL_FIELD_IS_BASE (field))
4721 if (!record_has_unique_obj_representations (TREE_TYPE (field),
4722 DECL_SIZE (field)))
4723 return false;
4725 else if (DECL_C_BIT_FIELD (field))
4727 tree btype = DECL_BIT_FIELD_TYPE (field);
4728 if (!type_has_unique_obj_representations (btype))
4729 return false;
4731 else if (!type_has_unique_obj_representations (TREE_TYPE (field)))
4732 return false;
4734 offset_int cur = 0;
4735 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4736 if (TREE_CODE (field) == FIELD_DECL)
4738 offset_int fld = wi::to_offset (DECL_FIELD_OFFSET (field));
4739 offset_int bitpos = wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
4740 fld = fld * BITS_PER_UNIT + bitpos;
4741 if (cur != fld)
4742 return false;
4743 if (DECL_SIZE (field))
4745 offset_int size = wi::to_offset (DECL_SIZE (field));
4746 cur += size;
4749 if (cur != wi::to_offset (sz))
4750 return false;
4752 return true;
4755 /* Nonzero iff type T is a class template implicit specialization. */
4757 bool
4758 class_tmpl_impl_spec_p (const_tree t)
4760 return CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INSTANTIATION (t);
4763 /* Returns 1 iff zero initialization of type T means actually storing
4764 zeros in it. */
4767 zero_init_p (const_tree t)
4769 /* This CONST_CAST is okay because strip_array_types returns its
4770 argument unmodified and we assign it to a const_tree. */
4771 t = strip_array_types (CONST_CAST_TREE(t));
4773 if (t == error_mark_node)
4774 return 1;
4776 /* NULL pointers to data members are initialized with -1. */
4777 if (TYPE_PTRDATAMEM_P (t))
4778 return 0;
4780 /* Classes that contain types that can't be zero-initialized, cannot
4781 be zero-initialized themselves. */
4782 if (CLASS_TYPE_P (t) && CLASSTYPE_NON_ZERO_INIT_P (t))
4783 return 0;
4785 return 1;
4788 /* Returns true if the expression or initializer T is the result of
4789 zero-initialization for its type, taking pointers to members
4790 into consideration. */
4792 bool
4793 zero_init_expr_p (tree t)
4795 tree type = TREE_TYPE (t);
4796 if (!type || uses_template_parms (type))
4797 return false;
4798 if (TYPE_PTRMEM_P (type))
4799 return null_member_pointer_value_p (t);
4800 if (TREE_CODE (t) == CONSTRUCTOR)
4802 if (COMPOUND_LITERAL_P (t)
4803 || BRACE_ENCLOSED_INITIALIZER_P (t))
4804 /* Undigested, conversions might change the zeroness. */
4805 return false;
4806 for (constructor_elt &elt : CONSTRUCTOR_ELTS (t))
4808 if (TREE_CODE (type) == UNION_TYPE
4809 && elt.index != first_field (type))
4810 return false;
4811 if (!zero_init_expr_p (elt.value))
4812 return false;
4814 return true;
4816 if (zero_init_p (type))
4817 return initializer_zerop (t);
4818 return false;
4821 /* True IFF T is a C++20 structural type (P1907R1) that can be used as a
4822 non-type template parameter. If EXPLAIN, explain why not. */
4824 bool
4825 structural_type_p (tree t, bool explain)
4827 /* A structural type is one of the following: */
4829 /* a scalar type, or */
4830 if (SCALAR_TYPE_P (t))
4831 return true;
4832 /* an lvalue reference type, or */
4833 if (TYPE_REF_P (t) && !TYPE_REF_IS_RVALUE (t))
4834 return true;
4835 /* a literal class type with the following properties:
4836 - all base classes and non-static data members are public and non-mutable
4838 - the types of all bases classes and non-static data members are
4839 structural types or (possibly multi-dimensional) array thereof. */
4840 if (!CLASS_TYPE_P (t))
4841 return false;
4842 if (!literal_type_p (t))
4844 if (explain)
4845 explain_non_literal_class (t);
4846 return false;
4848 for (tree m = next_initializable_field (TYPE_FIELDS (t)); m;
4849 m = next_initializable_field (DECL_CHAIN (m)))
4851 if (TREE_PRIVATE (m) || TREE_PROTECTED (m))
4853 if (explain)
4855 if (DECL_FIELD_IS_BASE (m))
4856 inform (location_of (m), "base class %qT is not public",
4857 TREE_TYPE (m));
4858 else
4859 inform (location_of (m), "%qD is not public", m);
4861 return false;
4863 if (DECL_MUTABLE_P (m))
4865 if (explain)
4866 inform (location_of (m), "%qD is mutable", m);
4867 return false;
4869 tree mtype = strip_array_types (TREE_TYPE (m));
4870 if (!structural_type_p (mtype))
4872 if (explain)
4874 inform (location_of (m), "%qD has a non-structural type", m);
4875 structural_type_p (mtype, true);
4877 return false;
4880 return true;
4883 /* Handle the C++17 [[nodiscard]] attribute, which is similar to the GNU
4884 warn_unused_result attribute. */
4886 static tree
4887 handle_nodiscard_attribute (tree *node, tree name, tree args,
4888 int /*flags*/, bool *no_add_attrs)
4890 if (args && TREE_CODE (TREE_VALUE (args)) != STRING_CST)
4892 error ("%qE attribute argument must be a string constant", name);
4893 *no_add_attrs = true;
4895 if (TREE_CODE (*node) == FUNCTION_DECL)
4897 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node)))
4898 && !DECL_CONSTRUCTOR_P (*node))
4899 warning_at (DECL_SOURCE_LOCATION (*node),
4900 OPT_Wattributes, "%qE attribute applied to %qD with void "
4901 "return type", name, *node);
4903 else if (OVERLOAD_TYPE_P (*node))
4904 /* OK */;
4905 else
4907 warning (OPT_Wattributes, "%qE attribute can only be applied to "
4908 "functions or to class or enumeration types", name);
4909 *no_add_attrs = true;
4911 return NULL_TREE;
4914 /* Handle a C++20 "no_unique_address" attribute; arguments as in
4915 struct attribute_spec.handler. */
4916 static tree
4917 handle_no_unique_addr_attribute (tree* node,
4918 tree name,
4919 tree /*args*/,
4920 int /*flags*/,
4921 bool* no_add_attrs)
4923 if (TREE_CODE (*node) != FIELD_DECL)
4925 warning (OPT_Wattributes, "%qE attribute can only be applied to "
4926 "non-static data members", name);
4927 *no_add_attrs = true;
4929 else if (DECL_C_BIT_FIELD (*node))
4931 warning (OPT_Wattributes, "%qE attribute cannot be applied to "
4932 "a bit-field", name);
4933 *no_add_attrs = true;
4936 return NULL_TREE;
4939 /* The C++20 [[likely]] and [[unlikely]] attributes on labels map to the GNU
4940 hot/cold attributes. */
4942 static tree
4943 handle_likeliness_attribute (tree *node, tree name, tree args,
4944 int flags, bool *no_add_attrs)
4946 *no_add_attrs = true;
4947 if (TREE_CODE (*node) == LABEL_DECL
4948 || TREE_CODE (*node) == FUNCTION_DECL)
4950 if (args)
4951 warning (OPT_Wattributes, "%qE attribute takes no arguments", name);
4952 tree bname = (is_attribute_p ("likely", name)
4953 ? get_identifier ("hot") : get_identifier ("cold"));
4954 if (TREE_CODE (*node) == FUNCTION_DECL)
4955 warning (OPT_Wattributes, "ISO C++ %qE attribute does not apply to "
4956 "functions; treating as %<[[gnu::%E]]%>", name, bname);
4957 tree battr = build_tree_list (bname, NULL_TREE);
4958 decl_attributes (node, battr, flags);
4959 return NULL_TREE;
4961 else
4962 return error_mark_node;
4965 /* Table of valid C++ attributes. */
4966 const struct attribute_spec cxx_attribute_table[] =
4968 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4969 affects_type_identity, handler, exclude } */
4970 { "init_priority", 1, 1, true, false, false, false,
4971 handle_init_priority_attribute, NULL },
4972 { "abi_tag", 1, -1, false, false, false, true,
4973 handle_abi_tag_attribute, NULL },
4974 { NULL, 0, 0, false, false, false, false, NULL, NULL }
4977 /* Table of C++ standard attributes. */
4978 const struct attribute_spec std_attribute_table[] =
4980 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4981 affects_type_identity, handler, exclude } */
4982 { "maybe_unused", 0, 0, false, false, false, false,
4983 handle_unused_attribute, NULL },
4984 { "nodiscard", 0, 1, false, false, false, false,
4985 handle_nodiscard_attribute, NULL },
4986 { "no_unique_address", 0, 0, true, false, false, false,
4987 handle_no_unique_addr_attribute, NULL },
4988 { "likely", 0, 0, false, false, false, false,
4989 handle_likeliness_attribute, attr_cold_hot_exclusions },
4990 { "unlikely", 0, 0, false, false, false, false,
4991 handle_likeliness_attribute, attr_cold_hot_exclusions },
4992 { "noreturn", 0, 0, true, false, false, false,
4993 handle_noreturn_attribute, attr_noreturn_exclusions },
4994 { NULL, 0, 0, false, false, false, false, NULL, NULL }
4997 /* Handle an "init_priority" attribute; arguments as in
4998 struct attribute_spec.handler. */
4999 static tree
5000 handle_init_priority_attribute (tree* node,
5001 tree name,
5002 tree args,
5003 int /*flags*/,
5004 bool* no_add_attrs)
5006 tree initp_expr = TREE_VALUE (args);
5007 tree decl = *node;
5008 tree type = TREE_TYPE (decl);
5009 int pri;
5011 STRIP_NOPS (initp_expr);
5012 initp_expr = default_conversion (initp_expr);
5013 if (initp_expr)
5014 initp_expr = maybe_constant_value (initp_expr);
5016 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
5018 error ("requested %<init_priority%> is not an integer constant");
5019 cxx_constant_value (initp_expr);
5020 *no_add_attrs = true;
5021 return NULL_TREE;
5024 pri = TREE_INT_CST_LOW (initp_expr);
5026 type = strip_array_types (type);
5028 if (decl == NULL_TREE
5029 || !VAR_P (decl)
5030 || !TREE_STATIC (decl)
5031 || DECL_EXTERNAL (decl)
5032 || (TREE_CODE (type) != RECORD_TYPE
5033 && TREE_CODE (type) != UNION_TYPE)
5034 /* Static objects in functions are initialized the
5035 first time control passes through that
5036 function. This is not precise enough to pin down an
5037 init_priority value, so don't allow it. */
5038 || current_function_decl)
5040 error ("can only use %qE attribute on file-scope definitions "
5041 "of objects of class type", name);
5042 *no_add_attrs = true;
5043 return NULL_TREE;
5046 if (pri > MAX_INIT_PRIORITY || pri <= 0)
5048 error ("requested %<init_priority%> %i is out of range [0, %i]",
5049 pri, MAX_INIT_PRIORITY);
5050 *no_add_attrs = true;
5051 return NULL_TREE;
5054 /* Check for init_priorities that are reserved for
5055 language and runtime support implementations.*/
5056 if (pri <= MAX_RESERVED_INIT_PRIORITY)
5058 warning
5059 (0, "requested %<init_priority%> %i is reserved for internal use",
5060 pri);
5063 if (SUPPORTS_INIT_PRIORITY)
5065 SET_DECL_INIT_PRIORITY (decl, pri);
5066 DECL_HAS_INIT_PRIORITY_P (decl) = 1;
5067 return NULL_TREE;
5069 else
5071 error ("%qE attribute is not supported on this platform", name);
5072 *no_add_attrs = true;
5073 return NULL_TREE;
5077 /* DECL is being redeclared; the old declaration had the abi tags in OLD,
5078 and the new one has the tags in NEW_. Give an error if there are tags
5079 in NEW_ that weren't in OLD. */
5081 bool
5082 check_abi_tag_redeclaration (const_tree decl, const_tree old, const_tree new_)
5084 if (old && TREE_CODE (TREE_VALUE (old)) == TREE_LIST)
5085 old = TREE_VALUE (old);
5086 if (new_ && TREE_CODE (TREE_VALUE (new_)) == TREE_LIST)
5087 new_ = TREE_VALUE (new_);
5088 bool err = false;
5089 for (const_tree t = new_; t; t = TREE_CHAIN (t))
5091 tree str = TREE_VALUE (t);
5092 for (const_tree in = old; in; in = TREE_CHAIN (in))
5094 tree ostr = TREE_VALUE (in);
5095 if (cp_tree_equal (str, ostr))
5096 goto found;
5098 error ("redeclaration of %qD adds abi tag %qE", decl, str);
5099 err = true;
5100 found:;
5102 if (err)
5104 inform (DECL_SOURCE_LOCATION (decl), "previous declaration here");
5105 return false;
5107 return true;
5110 /* The abi_tag attribute with the name NAME was given ARGS. If they are
5111 ill-formed, give an error and return false; otherwise, return true. */
5113 bool
5114 check_abi_tag_args (tree args, tree name)
5116 if (!args)
5118 error ("the %qE attribute requires arguments", name);
5119 return false;
5121 for (tree arg = args; arg; arg = TREE_CHAIN (arg))
5123 tree elt = TREE_VALUE (arg);
5124 if (TREE_CODE (elt) != STRING_CST
5125 || (!same_type_ignoring_top_level_qualifiers_p
5126 (strip_array_types (TREE_TYPE (elt)),
5127 char_type_node)))
5129 error ("arguments to the %qE attribute must be narrow string "
5130 "literals", name);
5131 return false;
5133 const char *begin = TREE_STRING_POINTER (elt);
5134 const char *end = begin + TREE_STRING_LENGTH (elt);
5135 for (const char *p = begin; p != end; ++p)
5137 char c = *p;
5138 if (p == begin)
5140 if (!ISALPHA (c) && c != '_')
5142 error ("arguments to the %qE attribute must contain valid "
5143 "identifiers", name);
5144 inform (input_location, "%<%c%> is not a valid first "
5145 "character for an identifier", c);
5146 return false;
5149 else if (p == end - 1)
5150 gcc_assert (c == 0);
5151 else
5153 if (!ISALNUM (c) && c != '_')
5155 error ("arguments to the %qE attribute must contain valid "
5156 "identifiers", name);
5157 inform (input_location, "%<%c%> is not a valid character "
5158 "in an identifier", c);
5159 return false;
5164 return true;
5167 /* Handle an "abi_tag" attribute; arguments as in
5168 struct attribute_spec.handler. */
5170 static tree
5171 handle_abi_tag_attribute (tree* node, tree name, tree args,
5172 int flags, bool* no_add_attrs)
5174 if (!check_abi_tag_args (args, name))
5175 goto fail;
5177 if (TYPE_P (*node))
5179 if (!OVERLOAD_TYPE_P (*node))
5181 error ("%qE attribute applied to non-class, non-enum type %qT",
5182 name, *node);
5183 goto fail;
5185 else if (!(flags & (int)ATTR_FLAG_TYPE_IN_PLACE))
5187 error ("%qE attribute applied to %qT after its definition",
5188 name, *node);
5189 goto fail;
5191 else if (CLASS_TYPE_P (*node)
5192 && CLASSTYPE_TEMPLATE_INSTANTIATION (*node))
5194 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
5195 "template instantiation %qT", name, *node);
5196 goto fail;
5198 else if (CLASS_TYPE_P (*node)
5199 && CLASSTYPE_TEMPLATE_SPECIALIZATION (*node))
5201 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
5202 "template specialization %qT", name, *node);
5203 goto fail;
5206 tree attributes = TYPE_ATTRIBUTES (*node);
5207 tree decl = TYPE_NAME (*node);
5209 /* Make sure all declarations have the same abi tags. */
5210 if (DECL_SOURCE_LOCATION (decl) != input_location)
5212 if (!check_abi_tag_redeclaration (decl,
5213 lookup_attribute ("abi_tag",
5214 attributes),
5215 args))
5216 goto fail;
5219 else
5221 if (!VAR_OR_FUNCTION_DECL_P (*node))
5223 error ("%qE attribute applied to non-function, non-variable %qD",
5224 name, *node);
5225 goto fail;
5227 else if (DECL_LANGUAGE (*node) == lang_c)
5229 error ("%qE attribute applied to extern \"C\" declaration %qD",
5230 name, *node);
5231 goto fail;
5235 return NULL_TREE;
5237 fail:
5238 *no_add_attrs = true;
5239 return NULL_TREE;
5242 /* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the
5243 thing pointed to by the constant. */
5245 tree
5246 make_ptrmem_cst (tree type, tree member)
5248 tree ptrmem_cst = make_node (PTRMEM_CST);
5249 TREE_TYPE (ptrmem_cst) = type;
5250 PTRMEM_CST_MEMBER (ptrmem_cst) = member;
5251 PTRMEM_CST_LOCATION (ptrmem_cst) = input_location;
5252 return ptrmem_cst;
5255 /* Build a variant of TYPE that has the indicated ATTRIBUTES. May
5256 return an existing type if an appropriate type already exists. */
5258 tree
5259 cp_build_type_attribute_variant (tree type, tree attributes)
5261 tree new_type;
5263 new_type = build_type_attribute_variant (type, attributes);
5264 if (FUNC_OR_METHOD_TYPE_P (new_type))
5265 gcc_checking_assert (cxx_type_hash_eq (type, new_type));
5267 /* Making a new main variant of a class type is broken. */
5268 gcc_assert (!CLASS_TYPE_P (type) || new_type == type);
5270 return new_type;
5273 /* Return TRUE if TYPE1 and TYPE2 are identical for type hashing purposes.
5274 Called only after doing all language independent checks. */
5276 bool
5277 cxx_type_hash_eq (const_tree typea, const_tree typeb)
5279 gcc_assert (FUNC_OR_METHOD_TYPE_P (typea));
5281 if (type_memfn_rqual (typea) != type_memfn_rqual (typeb))
5282 return false;
5283 if (TYPE_HAS_LATE_RETURN_TYPE (typea) != TYPE_HAS_LATE_RETURN_TYPE (typeb))
5284 return false;
5285 return comp_except_specs (TYPE_RAISES_EXCEPTIONS (typea),
5286 TYPE_RAISES_EXCEPTIONS (typeb), ce_exact);
5289 /* Copy the language-specific type variant modifiers from TYPEB to TYPEA. For
5290 C++, these are the exception-specifier and ref-qualifier. */
5292 tree
5293 cxx_copy_lang_qualifiers (const_tree typea, const_tree typeb)
5295 tree type = CONST_CAST_TREE (typea);
5296 if (FUNC_OR_METHOD_TYPE_P (type))
5297 type = build_cp_fntype_variant (type, type_memfn_rqual (typeb),
5298 TYPE_RAISES_EXCEPTIONS (typeb),
5299 TYPE_HAS_LATE_RETURN_TYPE (typeb));
5300 return type;
5303 /* Apply FUNC to all language-specific sub-trees of TP in a pre-order
5304 traversal. Called from walk_tree. */
5306 tree
5307 cp_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func,
5308 void *data, hash_set<tree> *pset)
5310 enum tree_code code = TREE_CODE (*tp);
5311 tree result;
5313 #define WALK_SUBTREE(NODE) \
5314 do \
5316 result = cp_walk_tree (&(NODE), func, data, pset); \
5317 if (result) goto out; \
5319 while (0)
5321 if (TYPE_P (*tp))
5323 /* If *WALK_SUBTREES_P is 1, we're interested in the syntactic form of
5324 the argument, so don't look through typedefs, but do walk into
5325 template arguments for alias templates (and non-typedefed classes).
5327 If *WALK_SUBTREES_P > 1, we're interested in type identity or
5328 equivalence, so look through typedefs, ignoring template arguments for
5329 alias templates, and walk into template args of classes.
5331 See find_abi_tags_r for an example of setting *WALK_SUBTREES_P to 2
5332 when that's the behavior the walk_tree_fn wants. */
5333 if (*walk_subtrees_p == 1 && typedef_variant_p (*tp))
5335 if (tree ti = TYPE_ALIAS_TEMPLATE_INFO (*tp))
5336 WALK_SUBTREE (TI_ARGS (ti));
5337 *walk_subtrees_p = 0;
5338 return NULL_TREE;
5341 if (tree ti = TYPE_TEMPLATE_INFO (*tp))
5342 WALK_SUBTREE (TI_ARGS (ti));
5345 /* Not one of the easy cases. We must explicitly go through the
5346 children. */
5347 result = NULL_TREE;
5348 switch (code)
5350 case TEMPLATE_TYPE_PARM:
5351 if (template_placeholder_p (*tp))
5352 WALK_SUBTREE (CLASS_PLACEHOLDER_TEMPLATE (*tp));
5353 /* Fall through. */
5354 case DEFERRED_PARSE:
5355 case TEMPLATE_TEMPLATE_PARM:
5356 case BOUND_TEMPLATE_TEMPLATE_PARM:
5357 case UNBOUND_CLASS_TEMPLATE:
5358 case TEMPLATE_PARM_INDEX:
5359 case TYPEOF_TYPE:
5360 case UNDERLYING_TYPE:
5361 /* None of these have subtrees other than those already walked
5362 above. */
5363 *walk_subtrees_p = 0;
5364 break;
5366 case TYPENAME_TYPE:
5367 WALK_SUBTREE (TYPE_CONTEXT (*tp));
5368 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (*tp));
5369 *walk_subtrees_p = 0;
5370 break;
5372 case BASELINK:
5373 if (BASELINK_QUALIFIED_P (*tp))
5374 WALK_SUBTREE (BINFO_TYPE (BASELINK_ACCESS_BINFO (*tp)));
5375 WALK_SUBTREE (BASELINK_FUNCTIONS (*tp));
5376 *walk_subtrees_p = 0;
5377 break;
5379 case PTRMEM_CST:
5380 WALK_SUBTREE (TREE_TYPE (*tp));
5381 *walk_subtrees_p = 0;
5382 break;
5384 case TREE_LIST:
5385 WALK_SUBTREE (TREE_PURPOSE (*tp));
5386 break;
5388 case OVERLOAD:
5389 WALK_SUBTREE (OVL_FUNCTION (*tp));
5390 WALK_SUBTREE (OVL_CHAIN (*tp));
5391 *walk_subtrees_p = 0;
5392 break;
5394 case USING_DECL:
5395 WALK_SUBTREE (DECL_NAME (*tp));
5396 WALK_SUBTREE (USING_DECL_SCOPE (*tp));
5397 WALK_SUBTREE (USING_DECL_DECLS (*tp));
5398 *walk_subtrees_p = 0;
5399 break;
5401 case RECORD_TYPE:
5402 if (TYPE_PTRMEMFUNC_P (*tp))
5403 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (*tp));
5404 break;
5406 case TYPE_ARGUMENT_PACK:
5407 case NONTYPE_ARGUMENT_PACK:
5409 tree args = ARGUMENT_PACK_ARGS (*tp);
5410 int i, len = TREE_VEC_LENGTH (args);
5411 for (i = 0; i < len; i++)
5412 WALK_SUBTREE (TREE_VEC_ELT (args, i));
5414 break;
5416 case TYPE_PACK_EXPANSION:
5417 WALK_SUBTREE (TREE_TYPE (*tp));
5418 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp));
5419 *walk_subtrees_p = 0;
5420 break;
5422 case EXPR_PACK_EXPANSION:
5423 WALK_SUBTREE (TREE_OPERAND (*tp, 0));
5424 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp));
5425 *walk_subtrees_p = 0;
5426 break;
5428 case CAST_EXPR:
5429 case REINTERPRET_CAST_EXPR:
5430 case STATIC_CAST_EXPR:
5431 case CONST_CAST_EXPR:
5432 case DYNAMIC_CAST_EXPR:
5433 case IMPLICIT_CONV_EXPR:
5434 case BIT_CAST_EXPR:
5435 if (TREE_TYPE (*tp))
5436 WALK_SUBTREE (TREE_TYPE (*tp));
5437 break;
5439 case CONSTRUCTOR:
5440 if (COMPOUND_LITERAL_P (*tp))
5441 WALK_SUBTREE (TREE_TYPE (*tp));
5442 break;
5444 case TRAIT_EXPR:
5445 WALK_SUBTREE (TRAIT_EXPR_TYPE1 (*tp));
5446 WALK_SUBTREE (TRAIT_EXPR_TYPE2 (*tp));
5447 *walk_subtrees_p = 0;
5448 break;
5450 case DECLTYPE_TYPE:
5451 ++cp_unevaluated_operand;
5452 /* We can't use WALK_SUBTREE here because of the goto. */
5453 result = cp_walk_tree (&DECLTYPE_TYPE_EXPR (*tp), func, data, pset);
5454 --cp_unevaluated_operand;
5455 *walk_subtrees_p = 0;
5456 break;
5458 case ALIGNOF_EXPR:
5459 case SIZEOF_EXPR:
5460 case NOEXCEPT_EXPR:
5461 ++cp_unevaluated_operand;
5462 result = cp_walk_tree (&TREE_OPERAND (*tp, 0), func, data, pset);
5463 --cp_unevaluated_operand;
5464 *walk_subtrees_p = 0;
5465 break;
5467 case REQUIRES_EXPR:
5468 // Only recurse through the nested expression. Do not
5469 // walk the parameter list. Doing so causes false
5470 // positives in the pack expansion checker since the
5471 // requires parameters are introduced as pack expansions.
5472 ++cp_unevaluated_operand;
5473 result = cp_walk_tree (&REQUIRES_EXPR_REQS (*tp), func, data, pset);
5474 --cp_unevaluated_operand;
5475 *walk_subtrees_p = 0;
5476 break;
5478 case DECL_EXPR:
5479 /* User variables should be mentioned in BIND_EXPR_VARS
5480 and their initializers and sizes walked when walking
5481 the containing BIND_EXPR. Compiler temporaries are
5482 handled here. And also normal variables in templates,
5483 since do_poplevel doesn't build a BIND_EXPR then. */
5484 if (VAR_P (TREE_OPERAND (*tp, 0))
5485 && (processing_template_decl
5486 || (DECL_ARTIFICIAL (TREE_OPERAND (*tp, 0))
5487 && !TREE_STATIC (TREE_OPERAND (*tp, 0)))))
5489 tree decl = TREE_OPERAND (*tp, 0);
5490 WALK_SUBTREE (DECL_INITIAL (decl));
5491 WALK_SUBTREE (DECL_SIZE (decl));
5492 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
5494 break;
5496 case LAMBDA_EXPR:
5497 /* Don't walk into the body of the lambda, but the capture initializers
5498 are part of the enclosing context. */
5499 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (*tp); cap;
5500 cap = TREE_CHAIN (cap))
5501 WALK_SUBTREE (TREE_VALUE (cap));
5502 break;
5504 case CO_YIELD_EXPR:
5505 if (TREE_OPERAND (*tp, 1))
5506 /* Operand 1 is the tree for the relevant co_await which has any
5507 interesting sub-trees. */
5508 WALK_SUBTREE (TREE_OPERAND (*tp, 1));
5509 break;
5511 case CO_AWAIT_EXPR:
5512 if (TREE_OPERAND (*tp, 1))
5513 /* Operand 1 is frame variable. */
5514 WALK_SUBTREE (TREE_OPERAND (*tp, 1));
5515 if (TREE_OPERAND (*tp, 2))
5516 /* Operand 2 has the initialiser, and we need to walk any subtrees
5517 there. */
5518 WALK_SUBTREE (TREE_OPERAND (*tp, 2));
5519 break;
5521 case CO_RETURN_EXPR:
5522 if (TREE_OPERAND (*tp, 0))
5524 if (VOID_TYPE_P (TREE_OPERAND (*tp, 0)))
5525 /* For void expressions, operand 1 is a trivial call, and any
5526 interesting subtrees will be part of operand 0. */
5527 WALK_SUBTREE (TREE_OPERAND (*tp, 0));
5528 else if (TREE_OPERAND (*tp, 1))
5529 /* Interesting sub-trees will be in the return_value () call
5530 arguments. */
5531 WALK_SUBTREE (TREE_OPERAND (*tp, 1));
5533 break;
5535 case STATIC_ASSERT:
5536 WALK_SUBTREE (STATIC_ASSERT_CONDITION (*tp));
5537 WALK_SUBTREE (STATIC_ASSERT_MESSAGE (*tp));
5538 break;
5540 default:
5541 return NULL_TREE;
5544 /* We didn't find what we were looking for. */
5545 out:
5546 return result;
5548 #undef WALK_SUBTREE
5551 /* Like save_expr, but for C++. */
5553 tree
5554 cp_save_expr (tree expr)
5556 /* There is no reason to create a SAVE_EXPR within a template; if
5557 needed, we can create the SAVE_EXPR when instantiating the
5558 template. Furthermore, the middle-end cannot handle C++-specific
5559 tree codes. */
5560 if (processing_template_decl)
5561 return expr;
5563 /* TARGET_EXPRs are only expanded once. */
5564 if (TREE_CODE (expr) == TARGET_EXPR)
5565 return expr;
5567 return save_expr (expr);
5570 /* Initialize tree.cc. */
5572 void
5573 init_tree (void)
5575 list_hash_table = hash_table<list_hasher>::create_ggc (61);
5576 register_scoped_attributes (std_attribute_table, NULL);
5579 /* Returns the kind of special function that DECL (a FUNCTION_DECL)
5580 is. Note that sfk_none is zero, so this function can be used as a
5581 predicate to test whether or not DECL is a special function. */
5583 special_function_kind
5584 special_function_p (const_tree decl)
5586 /* Rather than doing all this stuff with magic names, we should
5587 probably have a field of type `special_function_kind' in
5588 DECL_LANG_SPECIFIC. */
5589 if (DECL_INHERITED_CTOR (decl))
5590 return sfk_inheriting_constructor;
5591 if (DECL_COPY_CONSTRUCTOR_P (decl))
5592 return sfk_copy_constructor;
5593 if (DECL_MOVE_CONSTRUCTOR_P (decl))
5594 return sfk_move_constructor;
5595 if (DECL_CONSTRUCTOR_P (decl))
5596 return sfk_constructor;
5597 if (DECL_ASSIGNMENT_OPERATOR_P (decl)
5598 && DECL_OVERLOADED_OPERATOR_IS (decl, NOP_EXPR))
5600 if (copy_fn_p (decl))
5601 return sfk_copy_assignment;
5602 if (move_fn_p (decl))
5603 return sfk_move_assignment;
5605 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
5606 return sfk_destructor;
5607 if (DECL_COMPLETE_DESTRUCTOR_P (decl))
5608 return sfk_complete_destructor;
5609 if (DECL_BASE_DESTRUCTOR_P (decl))
5610 return sfk_base_destructor;
5611 if (DECL_DELETING_DESTRUCTOR_P (decl))
5612 return sfk_deleting_destructor;
5613 if (DECL_CONV_FN_P (decl))
5614 return sfk_conversion;
5615 if (deduction_guide_p (decl))
5616 return sfk_deduction_guide;
5617 if (DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) >= OVL_OP_EQ_EXPR
5618 && DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) <= OVL_OP_SPACESHIP_EXPR)
5619 return sfk_comparison;
5621 return sfk_none;
5624 /* As above, but only if DECL is a special member function as per 11.3.3
5625 [special]: default/copy/move ctor, copy/move assignment, or destructor. */
5627 special_function_kind
5628 special_memfn_p (const_tree decl)
5630 switch (special_function_kind sfk = special_function_p (decl))
5632 case sfk_constructor:
5633 if (!default_ctor_p (decl))
5634 break;
5635 gcc_fallthrough();
5636 case sfk_copy_constructor:
5637 case sfk_copy_assignment:
5638 case sfk_move_assignment:
5639 case sfk_move_constructor:
5640 case sfk_destructor:
5641 return sfk;
5643 default:
5644 break;
5646 return sfk_none;
5649 /* Returns nonzero if TYPE is a character type, including wchar_t. */
5652 char_type_p (tree type)
5654 return (same_type_p (type, char_type_node)
5655 || same_type_p (type, unsigned_char_type_node)
5656 || same_type_p (type, signed_char_type_node)
5657 || same_type_p (type, char8_type_node)
5658 || same_type_p (type, char16_type_node)
5659 || same_type_p (type, char32_type_node)
5660 || same_type_p (type, wchar_type_node));
5663 /* Returns the kind of linkage associated with the indicated DECL. Th
5664 value returned is as specified by the language standard; it is
5665 independent of implementation details regarding template
5666 instantiation, etc. For example, it is possible that a declaration
5667 to which this function assigns external linkage would not show up
5668 as a global symbol when you run `nm' on the resulting object file. */
5670 linkage_kind
5671 decl_linkage (tree decl)
5673 /* This function doesn't attempt to calculate the linkage from first
5674 principles as given in [basic.link]. Instead, it makes use of
5675 the fact that we have already set TREE_PUBLIC appropriately, and
5676 then handles a few special cases. Ideally, we would calculate
5677 linkage first, and then transform that into a concrete
5678 implementation. */
5680 /* Things that don't have names have no linkage. */
5681 if (!DECL_NAME (decl))
5682 return lk_none;
5684 /* Fields have no linkage. */
5685 if (TREE_CODE (decl) == FIELD_DECL)
5686 return lk_none;
5688 /* Things in local scope do not have linkage. */
5689 if (decl_function_context (decl))
5690 return lk_none;
5692 /* Things that are TREE_PUBLIC have external linkage. */
5693 if (TREE_PUBLIC (decl))
5694 return lk_external;
5696 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor variants,
5697 check one of the "clones" for the real linkage. */
5698 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl)
5699 && DECL_CHAIN (decl)
5700 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)))
5701 return decl_linkage (DECL_CHAIN (decl));
5703 if (TREE_CODE (decl) == NAMESPACE_DECL)
5704 return lk_external;
5706 /* Linkage of a CONST_DECL depends on the linkage of the enumeration
5707 type. */
5708 if (TREE_CODE (decl) == CONST_DECL)
5709 return decl_linkage (TYPE_NAME (DECL_CONTEXT (decl)));
5711 /* Members of the anonymous namespace also have TREE_PUBLIC unset, but
5712 are considered to have external linkage for language purposes, as do
5713 template instantiations on targets without weak symbols. DECLs really
5714 meant to have internal linkage have DECL_THIS_STATIC set. */
5715 if (TREE_CODE (decl) == TYPE_DECL)
5716 return lk_external;
5717 if (VAR_OR_FUNCTION_DECL_P (decl))
5719 if (!DECL_THIS_STATIC (decl))
5720 return lk_external;
5722 /* Static data members and static member functions from classes
5723 in anonymous namespace also don't have TREE_PUBLIC set. */
5724 if (DECL_CLASS_CONTEXT (decl))
5725 return lk_external;
5728 /* Everything else has internal linkage. */
5729 return lk_internal;
5732 /* Returns the storage duration of the object or reference associated with
5733 the indicated DECL, which should be a VAR_DECL or PARM_DECL. */
5735 duration_kind
5736 decl_storage_duration (tree decl)
5738 if (TREE_CODE (decl) == PARM_DECL)
5739 return dk_auto;
5740 if (TREE_CODE (decl) == FUNCTION_DECL)
5741 return dk_static;
5742 gcc_assert (VAR_P (decl));
5743 if (!TREE_STATIC (decl)
5744 && !DECL_EXTERNAL (decl))
5745 return dk_auto;
5746 if (CP_DECL_THREAD_LOCAL_P (decl))
5747 return dk_thread;
5748 return dk_static;
5751 /* EXP is an expression that we want to pre-evaluate. Returns (in
5752 *INITP) an expression that will perform the pre-evaluation. The
5753 value returned by this function is a side-effect free expression
5754 equivalent to the pre-evaluated expression. Callers must ensure
5755 that *INITP is evaluated before EXP. */
5757 tree
5758 stabilize_expr (tree exp, tree* initp)
5760 tree init_expr;
5762 if (!TREE_SIDE_EFFECTS (exp))
5763 init_expr = NULL_TREE;
5764 else if (VOID_TYPE_P (TREE_TYPE (exp)))
5766 init_expr = exp;
5767 exp = void_node;
5769 /* There are no expressions with REFERENCE_TYPE, but there can be call
5770 arguments with such a type; just treat it as a pointer. */
5771 else if (TYPE_REF_P (TREE_TYPE (exp))
5772 || SCALAR_TYPE_P (TREE_TYPE (exp))
5773 || !glvalue_p (exp))
5775 init_expr = get_target_expr (exp);
5776 exp = TARGET_EXPR_SLOT (init_expr);
5777 if (CLASS_TYPE_P (TREE_TYPE (exp)))
5778 exp = move (exp);
5779 else
5780 exp = rvalue (exp);
5782 else
5784 bool xval = !lvalue_p (exp);
5785 exp = cp_build_addr_expr (exp, tf_warning_or_error);
5786 init_expr = get_target_expr (exp);
5787 exp = TARGET_EXPR_SLOT (init_expr);
5788 exp = cp_build_fold_indirect_ref (exp);
5789 if (xval)
5790 exp = move (exp);
5792 *initp = init_expr;
5794 gcc_assert (!TREE_SIDE_EFFECTS (exp));
5795 return exp;
5798 /* Add NEW_EXPR, an expression whose value we don't care about, after the
5799 similar expression ORIG. */
5801 tree
5802 add_stmt_to_compound (tree orig, tree new_expr)
5804 if (!new_expr || !TREE_SIDE_EFFECTS (new_expr))
5805 return orig;
5806 if (!orig || !TREE_SIDE_EFFECTS (orig))
5807 return new_expr;
5808 return build2 (COMPOUND_EXPR, void_type_node, orig, new_expr);
5811 /* Like stabilize_expr, but for a call whose arguments we want to
5812 pre-evaluate. CALL is modified in place to use the pre-evaluated
5813 arguments, while, upon return, *INITP contains an expression to
5814 compute the arguments. */
5816 void
5817 stabilize_call (tree call, tree *initp)
5819 tree inits = NULL_TREE;
5820 int i;
5821 int nargs = call_expr_nargs (call);
5823 if (call == error_mark_node || processing_template_decl)
5825 *initp = NULL_TREE;
5826 return;
5829 gcc_assert (TREE_CODE (call) == CALL_EXPR);
5831 for (i = 0; i < nargs; i++)
5833 tree init;
5834 CALL_EXPR_ARG (call, i) =
5835 stabilize_expr (CALL_EXPR_ARG (call, i), &init);
5836 inits = add_stmt_to_compound (inits, init);
5839 *initp = inits;
5842 /* Like stabilize_expr, but for an AGGR_INIT_EXPR whose arguments we want
5843 to pre-evaluate. CALL is modified in place to use the pre-evaluated
5844 arguments, while, upon return, *INITP contains an expression to
5845 compute the arguments. */
5847 static void
5848 stabilize_aggr_init (tree call, tree *initp)
5850 tree inits = NULL_TREE;
5851 int i;
5852 int nargs = aggr_init_expr_nargs (call);
5854 if (call == error_mark_node)
5855 return;
5857 gcc_assert (TREE_CODE (call) == AGGR_INIT_EXPR);
5859 for (i = 0; i < nargs; i++)
5861 tree init;
5862 AGGR_INIT_EXPR_ARG (call, i) =
5863 stabilize_expr (AGGR_INIT_EXPR_ARG (call, i), &init);
5864 inits = add_stmt_to_compound (inits, init);
5867 *initp = inits;
5870 /* Like stabilize_expr, but for an initialization.
5872 If the initialization is for an object of class type, this function
5873 takes care not to introduce additional temporaries.
5875 Returns TRUE iff the expression was successfully pre-evaluated,
5876 i.e., if INIT is now side-effect free, except for, possibly, a
5877 single call to a constructor. */
5879 bool
5880 stabilize_init (tree init, tree *initp)
5882 tree t = init;
5884 *initp = NULL_TREE;
5886 if (t == error_mark_node || processing_template_decl)
5887 return true;
5889 if (TREE_CODE (t) == INIT_EXPR)
5890 t = TREE_OPERAND (t, 1);
5891 if (TREE_CODE (t) == TARGET_EXPR)
5892 t = TARGET_EXPR_INITIAL (t);
5894 /* If the RHS can be stabilized without breaking copy elision, stabilize
5895 it. We specifically don't stabilize class prvalues here because that
5896 would mean an extra copy, but they might be stabilized below. */
5897 if (TREE_CODE (init) == INIT_EXPR
5898 && TREE_CODE (t) != CONSTRUCTOR
5899 && TREE_CODE (t) != AGGR_INIT_EXPR
5900 && (SCALAR_TYPE_P (TREE_TYPE (t))
5901 || glvalue_p (t)))
5903 TREE_OPERAND (init, 1) = stabilize_expr (t, initp);
5904 return true;
5907 if (TREE_CODE (t) == COMPOUND_EXPR
5908 && TREE_CODE (init) == INIT_EXPR)
5910 tree last = expr_last (t);
5911 /* Handle stabilizing the EMPTY_CLASS_EXPR pattern. */
5912 if (!TREE_SIDE_EFFECTS (last))
5914 *initp = t;
5915 TREE_OPERAND (init, 1) = last;
5916 return true;
5920 if (TREE_CODE (t) == CONSTRUCTOR)
5922 /* Aggregate initialization: stabilize each of the field
5923 initializers. */
5924 unsigned i;
5925 constructor_elt *ce;
5926 bool good = true;
5927 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
5928 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
5930 tree type = TREE_TYPE (ce->value);
5931 tree subinit;
5932 if (TYPE_REF_P (type)
5933 || SCALAR_TYPE_P (type))
5934 ce->value = stabilize_expr (ce->value, &subinit);
5935 else if (!stabilize_init (ce->value, &subinit))
5936 good = false;
5937 *initp = add_stmt_to_compound (*initp, subinit);
5939 return good;
5942 if (TREE_CODE (t) == CALL_EXPR)
5944 stabilize_call (t, initp);
5945 return true;
5948 if (TREE_CODE (t) == AGGR_INIT_EXPR)
5950 stabilize_aggr_init (t, initp);
5951 return true;
5954 /* The initialization is being performed via a bitwise copy -- and
5955 the item copied may have side effects. */
5956 return !TREE_SIDE_EFFECTS (init);
5959 /* Returns true if a cast to TYPE may appear in an integral constant
5960 expression. */
5962 bool
5963 cast_valid_in_integral_constant_expression_p (tree type)
5965 return (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5966 || cxx_dialect >= cxx11
5967 || dependent_type_p (type)
5968 || type == error_mark_node);
5971 /* Return true if we need to fix linkage information of DECL. */
5973 static bool
5974 cp_fix_function_decl_p (tree decl)
5976 /* Skip if DECL is not externally visible. */
5977 if (!TREE_PUBLIC (decl))
5978 return false;
5980 /* We need to fix DECL if it a appears to be exported but with no
5981 function body. Thunks do not have CFGs and we may need to
5982 handle them specially later. */
5983 if (!gimple_has_body_p (decl)
5984 && !DECL_THUNK_P (decl)
5985 && !DECL_EXTERNAL (decl))
5987 struct cgraph_node *node = cgraph_node::get (decl);
5989 /* Don't fix same_body aliases. Although they don't have their own
5990 CFG, they share it with what they alias to. */
5991 if (!node || !node->alias || !node->num_references ())
5992 return true;
5995 return false;
5998 /* Clean the C++ specific parts of the tree T. */
6000 void
6001 cp_free_lang_data (tree t)
6003 if (FUNC_OR_METHOD_TYPE_P (t))
6005 /* Default args are not interesting anymore. */
6006 tree argtypes = TYPE_ARG_TYPES (t);
6007 while (argtypes)
6009 TREE_PURPOSE (argtypes) = 0;
6010 argtypes = TREE_CHAIN (argtypes);
6013 else if (TREE_CODE (t) == FUNCTION_DECL
6014 && cp_fix_function_decl_p (t))
6016 /* If T is used in this translation unit at all, the definition
6017 must exist somewhere else since we have decided to not emit it
6018 in this TU. So make it an external reference. */
6019 DECL_EXTERNAL (t) = 1;
6020 TREE_STATIC (t) = 0;
6022 if (TREE_CODE (t) == NAMESPACE_DECL)
6023 /* We do not need the leftover chaining of namespaces from the
6024 binding level. */
6025 DECL_CHAIN (t) = NULL_TREE;
6028 /* Stub for c-common. Please keep in sync with c-decl.cc.
6029 FIXME: If address space support is target specific, then this
6030 should be a C target hook. But currently this is not possible,
6031 because this function is called via REGISTER_TARGET_PRAGMAS. */
6032 void
6033 c_register_addr_space (const char * /*word*/, addr_space_t /*as*/)
6037 /* Return the number of operands in T that we care about for things like
6038 mangling. */
6041 cp_tree_operand_length (const_tree t)
6043 enum tree_code code = TREE_CODE (t);
6045 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
6046 return VL_EXP_OPERAND_LENGTH (t);
6048 return cp_tree_code_length (code);
6051 /* Like cp_tree_operand_length, but takes a tree_code CODE. */
6054 cp_tree_code_length (enum tree_code code)
6056 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
6058 switch (code)
6060 case PREINCREMENT_EXPR:
6061 case PREDECREMENT_EXPR:
6062 case POSTINCREMENT_EXPR:
6063 case POSTDECREMENT_EXPR:
6064 return 1;
6066 case ARRAY_REF:
6067 return 2;
6069 case EXPR_PACK_EXPANSION:
6070 return 1;
6072 default:
6073 return TREE_CODE_LENGTH (code);
6077 /* Like EXPR_LOCATION, but also handle some tcc_exceptional that have
6078 locations. */
6080 location_t
6081 cp_expr_location (const_tree t_)
6083 tree t = CONST_CAST_TREE (t_);
6084 if (t == NULL_TREE)
6085 return UNKNOWN_LOCATION;
6086 switch (TREE_CODE (t))
6088 case LAMBDA_EXPR:
6089 return LAMBDA_EXPR_LOCATION (t);
6090 case STATIC_ASSERT:
6091 return STATIC_ASSERT_SOURCE_LOCATION (t);
6092 case TRAIT_EXPR:
6093 return TRAIT_EXPR_LOCATION (t);
6094 case PTRMEM_CST:
6095 return PTRMEM_CST_LOCATION (t);
6096 default:
6097 return EXPR_LOCATION (t);
6101 /* Implement -Wzero_as_null_pointer_constant. Return true if the
6102 conditions for the warning hold, false otherwise. */
6103 bool
6104 maybe_warn_zero_as_null_pointer_constant (tree expr, location_t loc)
6106 if (c_inhibit_evaluation_warnings == 0
6107 && !null_node_p (expr) && !NULLPTR_TYPE_P (TREE_TYPE (expr)))
6109 warning_at (loc, OPT_Wzero_as_null_pointer_constant,
6110 "zero as null pointer constant");
6111 return true;
6113 return false;
6116 /* Release memory we no longer need after parsing. */
6117 void
6118 cp_tree_c_finish_parsing ()
6120 if (previous_class_level)
6121 invalidate_class_lookup_cache ();
6122 deleted_copy_types = NULL;
6125 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
6126 /* Complain that some language-specific thing hanging off a tree
6127 node has been accessed improperly. */
6129 void
6130 lang_check_failed (const char* file, int line, const char* function)
6132 internal_error ("%<lang_*%> check: failed in %s, at %s:%d",
6133 function, trim_filename (file), line);
6135 #endif /* ENABLE_TREE_CHECKING */
6137 #if CHECKING_P
6139 namespace selftest {
6141 /* Verify that lvalue_kind () works, for various expressions,
6142 and that location wrappers don't affect the results. */
6144 static void
6145 test_lvalue_kind ()
6147 location_t loc = BUILTINS_LOCATION;
6149 /* Verify constants and parameters, without and with
6150 location wrappers. */
6151 tree int_cst = build_int_cst (integer_type_node, 42);
6152 ASSERT_EQ (clk_none, lvalue_kind (int_cst));
6154 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
6155 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
6156 ASSERT_EQ (clk_none, lvalue_kind (wrapped_int_cst));
6158 tree string_lit = build_string (4, "foo");
6159 TREE_TYPE (string_lit) = char_array_type_node;
6160 string_lit = fix_string_type (string_lit);
6161 ASSERT_EQ (clk_ordinary, lvalue_kind (string_lit));
6163 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
6164 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
6165 ASSERT_EQ (clk_ordinary, lvalue_kind (wrapped_string_lit));
6167 tree parm = build_decl (UNKNOWN_LOCATION, PARM_DECL,
6168 get_identifier ("some_parm"),
6169 integer_type_node);
6170 ASSERT_EQ (clk_ordinary, lvalue_kind (parm));
6172 tree wrapped_parm = maybe_wrap_with_location (parm, loc);
6173 ASSERT_TRUE (location_wrapper_p (wrapped_parm));
6174 ASSERT_EQ (clk_ordinary, lvalue_kind (wrapped_parm));
6176 /* Verify that lvalue_kind of std::move on a parm isn't
6177 affected by location wrappers. */
6178 tree rvalue_ref_of_parm = move (parm);
6179 ASSERT_EQ (clk_rvalueref, lvalue_kind (rvalue_ref_of_parm));
6180 tree rvalue_ref_of_wrapped_parm = move (wrapped_parm);
6181 ASSERT_EQ (clk_rvalueref, lvalue_kind (rvalue_ref_of_wrapped_parm));
6183 /* Verify lvalue_p. */
6184 ASSERT_FALSE (lvalue_p (int_cst));
6185 ASSERT_FALSE (lvalue_p (wrapped_int_cst));
6186 ASSERT_TRUE (lvalue_p (parm));
6187 ASSERT_TRUE (lvalue_p (wrapped_parm));
6188 ASSERT_FALSE (lvalue_p (rvalue_ref_of_parm));
6189 ASSERT_FALSE (lvalue_p (rvalue_ref_of_wrapped_parm));
6192 /* Run all of the selftests within this file. */
6194 void
6195 cp_tree_cc_tests ()
6197 test_lvalue_kind ();
6200 } // namespace selftest
6202 #endif /* #if CHECKING_P */
6205 #include "gt-cp-tree.h"