More #include suggestions (PR c++/84269)
[official-gcc.git] / gcc / cp / tree.c
blobe75c88d6e8f8d8375e5fc214a6d5d2efd69da4e4
1 /* Language-dependent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987-2018 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 *);
46 static tree build_local_temp (tree);
48 static tree handle_init_priority_attribute (tree *, tree, tree, int, bool *);
49 static tree handle_abi_tag_attribute (tree *, tree, tree, int, bool *);
51 /* If REF is an lvalue, returns the kind of lvalue that REF is.
52 Otherwise, returns clk_none. */
54 cp_lvalue_kind
55 lvalue_kind (const_tree ref)
57 cp_lvalue_kind op1_lvalue_kind = clk_none;
58 cp_lvalue_kind op2_lvalue_kind = clk_none;
60 /* Expressions of reference type are sometimes wrapped in
61 INDIRECT_REFs. INDIRECT_REFs are just internal compiler
62 representation, not part of the language, so we have to look
63 through them. */
64 if (REFERENCE_REF_P (ref))
65 return lvalue_kind (TREE_OPERAND (ref, 0));
67 if (TREE_TYPE (ref)
68 && TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
70 /* unnamed rvalue references are rvalues */
71 if (TYPE_REF_IS_RVALUE (TREE_TYPE (ref))
72 && TREE_CODE (ref) != PARM_DECL
73 && !VAR_P (ref)
74 && TREE_CODE (ref) != COMPONENT_REF
75 /* Functions are always lvalues. */
76 && TREE_CODE (TREE_TYPE (TREE_TYPE (ref))) != FUNCTION_TYPE)
77 return clk_rvalueref;
79 /* lvalue references and named rvalue references are lvalues. */
80 return clk_ordinary;
83 if (ref == current_class_ptr)
84 return clk_none;
86 switch (TREE_CODE (ref))
88 case SAVE_EXPR:
89 return clk_none;
90 /* preincrements and predecrements are valid lvals, provided
91 what they refer to are valid lvals. */
92 case PREINCREMENT_EXPR:
93 case PREDECREMENT_EXPR:
94 case TRY_CATCH_EXPR:
95 case REALPART_EXPR:
96 case IMAGPART_EXPR:
97 return lvalue_kind (TREE_OPERAND (ref, 0));
99 case MEMBER_REF:
100 case DOTSTAR_EXPR:
101 if (TREE_CODE (ref) == MEMBER_REF)
102 op1_lvalue_kind = clk_ordinary;
103 else
104 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
105 if (TYPE_PTRMEMFUNC_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
106 op1_lvalue_kind = clk_none;
107 return op1_lvalue_kind;
109 case COMPONENT_REF:
110 if (BASELINK_P (TREE_OPERAND (ref, 1)))
112 tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (ref, 1));
114 /* For static member function recurse on the BASELINK, we can get
115 here e.g. from reference_binding. If BASELINK_FUNCTIONS is
116 OVERLOAD, the overload is resolved first if possible through
117 resolve_address_of_overloaded_function. */
118 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_STATIC_FUNCTION_P (fn))
119 return lvalue_kind (TREE_OPERAND (ref, 1));
121 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
122 /* Look at the member designator. */
123 if (!op1_lvalue_kind)
125 else if (is_overloaded_fn (TREE_OPERAND (ref, 1)))
126 /* The "field" can be a FUNCTION_DECL or an OVERLOAD in some
127 situations. If we're seeing a COMPONENT_REF, it's a non-static
128 member, so it isn't an lvalue. */
129 op1_lvalue_kind = clk_none;
130 else if (TREE_CODE (TREE_OPERAND (ref, 1)) != FIELD_DECL)
131 /* This can be IDENTIFIER_NODE in a template. */;
132 else if (DECL_C_BIT_FIELD (TREE_OPERAND (ref, 1)))
134 /* Clear the ordinary bit. If this object was a class
135 rvalue we want to preserve that information. */
136 op1_lvalue_kind &= ~clk_ordinary;
137 /* The lvalue is for a bitfield. */
138 op1_lvalue_kind |= clk_bitfield;
140 else if (DECL_PACKED (TREE_OPERAND (ref, 1)))
141 op1_lvalue_kind |= clk_packed;
143 return op1_lvalue_kind;
145 case STRING_CST:
146 case COMPOUND_LITERAL_EXPR:
147 return clk_ordinary;
149 case CONST_DECL:
150 /* CONST_DECL without TREE_STATIC are enumeration values and
151 thus not lvalues. With TREE_STATIC they are used by ObjC++
152 in objc_build_string_object and need to be considered as
153 lvalues. */
154 if (! TREE_STATIC (ref))
155 return clk_none;
156 /* FALLTHRU */
157 case VAR_DECL:
158 if (VAR_P (ref) && DECL_HAS_VALUE_EXPR_P (ref))
159 return lvalue_kind (DECL_VALUE_EXPR (CONST_CAST_TREE (ref)));
161 if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
162 && DECL_LANG_SPECIFIC (ref)
163 && DECL_IN_AGGR_P (ref))
164 return clk_none;
165 /* FALLTHRU */
166 case INDIRECT_REF:
167 case ARROW_EXPR:
168 case ARRAY_REF:
169 case PARM_DECL:
170 case RESULT_DECL:
171 case PLACEHOLDER_EXPR:
172 return clk_ordinary;
174 /* A scope ref in a template, left as SCOPE_REF to support later
175 access checking. */
176 case SCOPE_REF:
177 gcc_assert (!type_dependent_expression_p (CONST_CAST_TREE (ref)));
179 tree op = TREE_OPERAND (ref, 1);
180 if (TREE_CODE (op) == FIELD_DECL)
181 return (DECL_C_BIT_FIELD (op) ? clk_bitfield : clk_ordinary);
182 else
183 return lvalue_kind (op);
186 case MAX_EXPR:
187 case MIN_EXPR:
188 /* Disallow <? and >? as lvalues if either argument side-effects. */
189 if (TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 0))
190 || TREE_SIDE_EFFECTS (TREE_OPERAND (ref, 1)))
191 return clk_none;
192 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 0));
193 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1));
194 break;
196 case COND_EXPR:
197 if (processing_template_decl)
199 /* Within templates, a REFERENCE_TYPE will indicate whether
200 the COND_EXPR result is an ordinary lvalue or rvalueref.
201 Since REFERENCE_TYPEs are handled above, if we reach this
202 point, we know we got a plain rvalue. Unless we have a
203 type-dependent expr, that is, but we shouldn't be testing
204 lvalueness if we can't even tell the types yet! */
205 gcc_assert (!type_dependent_expression_p (CONST_CAST_TREE (ref)));
206 if (CLASS_TYPE_P (TREE_TYPE (ref))
207 || TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE)
208 return clk_class;
209 else
210 return clk_none;
212 op1_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 1)
213 ? TREE_OPERAND (ref, 1)
214 : TREE_OPERAND (ref, 0));
215 op2_lvalue_kind = lvalue_kind (TREE_OPERAND (ref, 2));
216 break;
218 case MODOP_EXPR:
219 /* We expect to see unlowered MODOP_EXPRs only during
220 template processing. */
221 gcc_assert (processing_template_decl);
222 return clk_ordinary;
224 case MODIFY_EXPR:
225 case TYPEID_EXPR:
226 return clk_ordinary;
228 case COMPOUND_EXPR:
229 return lvalue_kind (TREE_OPERAND (ref, 1));
231 case TARGET_EXPR:
232 return clk_class;
234 case VA_ARG_EXPR:
235 return (CLASS_TYPE_P (TREE_TYPE (ref)) ? clk_class : clk_none);
237 case CALL_EXPR:
238 /* We can see calls outside of TARGET_EXPR in templates. */
239 if (CLASS_TYPE_P (TREE_TYPE (ref)))
240 return clk_class;
241 return clk_none;
243 case FUNCTION_DECL:
244 /* All functions (except non-static-member functions) are
245 lvalues. */
246 return (DECL_NONSTATIC_MEMBER_FUNCTION_P (ref)
247 ? clk_none : clk_ordinary);
249 case BASELINK:
250 /* We now represent a reference to a single static member function
251 with a BASELINK. */
252 /* This CONST_CAST is okay because BASELINK_FUNCTIONS returns
253 its argument unmodified and we assign it to a const_tree. */
254 return lvalue_kind (BASELINK_FUNCTIONS (CONST_CAST_TREE (ref)));
256 case NON_DEPENDENT_EXPR:
257 case PAREN_EXPR:
258 return lvalue_kind (TREE_OPERAND (ref, 0));
260 case VIEW_CONVERT_EXPR:
261 if (location_wrapper_p (ref))
262 return lvalue_kind (TREE_OPERAND (ref, 0));
263 /* Fallthrough. */
265 default:
266 if (!TREE_TYPE (ref))
267 return clk_none;
268 if (CLASS_TYPE_P (TREE_TYPE (ref))
269 || TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE)
270 return clk_class;
271 break;
274 /* If one operand is not an lvalue at all, then this expression is
275 not an lvalue. */
276 if (!op1_lvalue_kind || !op2_lvalue_kind)
277 return clk_none;
279 /* Otherwise, it's an lvalue, and it has all the odd properties
280 contributed by either operand. */
281 op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind;
282 /* It's not an ordinary lvalue if it involves any other kind. */
283 if ((op1_lvalue_kind & ~clk_ordinary) != clk_none)
284 op1_lvalue_kind &= ~clk_ordinary;
285 /* It can't be both a pseudo-lvalue and a non-addressable lvalue.
286 A COND_EXPR of those should be wrapped in a TARGET_EXPR. */
287 if ((op1_lvalue_kind & (clk_rvalueref|clk_class))
288 && (op1_lvalue_kind & (clk_bitfield|clk_packed)))
289 op1_lvalue_kind = clk_none;
290 return op1_lvalue_kind;
293 /* Returns the kind of lvalue that REF is, in the sense of [basic.lval]. */
295 cp_lvalue_kind
296 real_lvalue_p (const_tree ref)
298 cp_lvalue_kind kind = lvalue_kind (ref);
299 if (kind & (clk_rvalueref|clk_class))
300 return clk_none;
301 else
302 return kind;
305 /* c-common wants us to return bool. */
307 bool
308 lvalue_p (const_tree t)
310 return real_lvalue_p (t);
313 /* This differs from lvalue_p in that xvalues are included. */
315 bool
316 glvalue_p (const_tree ref)
318 cp_lvalue_kind kind = lvalue_kind (ref);
319 if (kind & clk_class)
320 return false;
321 else
322 return (kind != clk_none);
325 /* This differs from glvalue_p in that class prvalues are included. */
327 bool
328 obvalue_p (const_tree ref)
330 return (lvalue_kind (ref) != clk_none);
333 /* Returns true if REF is an xvalue (the result of dereferencing an rvalue
334 reference), false otherwise. */
336 bool
337 xvalue_p (const_tree ref)
339 return (lvalue_kind (ref) == clk_rvalueref);
342 /* True if REF is a bit-field. */
344 bool
345 bitfield_p (const_tree ref)
347 return (lvalue_kind (ref) & clk_bitfield);
350 /* C++-specific version of stabilize_reference. */
352 tree
353 cp_stabilize_reference (tree ref)
355 switch (TREE_CODE (ref))
357 case NON_DEPENDENT_EXPR:
358 /* We aren't actually evaluating this. */
359 return ref;
361 /* We need to treat specially anything stabilize_reference doesn't
362 handle specifically. */
363 case VAR_DECL:
364 case PARM_DECL:
365 case RESULT_DECL:
366 CASE_CONVERT:
367 case FLOAT_EXPR:
368 case FIX_TRUNC_EXPR:
369 case INDIRECT_REF:
370 case COMPONENT_REF:
371 case BIT_FIELD_REF:
372 case ARRAY_REF:
373 case ARRAY_RANGE_REF:
374 case ERROR_MARK:
375 break;
376 default:
377 cp_lvalue_kind kind = lvalue_kind (ref);
378 if ((kind & ~clk_class) != clk_none)
380 tree type = unlowered_expr_type (ref);
381 bool rval = !!(kind & clk_rvalueref);
382 type = cp_build_reference_type (type, rval);
383 /* This inhibits warnings in, eg, cxx_mark_addressable
384 (c++/60955). */
385 warning_sentinel s (extra_warnings);
386 ref = build_static_cast (type, ref, tf_error);
390 return stabilize_reference (ref);
393 /* Test whether DECL is a builtin that may appear in a
394 constant-expression. */
396 bool
397 builtin_valid_in_constant_expr_p (const_tree decl)
399 if (!(TREE_CODE (decl) == FUNCTION_DECL
400 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL))
401 /* Not a built-in. */
402 return false;
403 switch (DECL_FUNCTION_CODE (decl))
405 /* These always have constant results like the corresponding
406 macros/symbol. */
407 case BUILT_IN_FILE:
408 case BUILT_IN_FUNCTION:
409 case BUILT_IN_LINE:
411 /* The following built-ins are valid in constant expressions
412 when their arguments are. */
413 case BUILT_IN_ADD_OVERFLOW_P:
414 case BUILT_IN_SUB_OVERFLOW_P:
415 case BUILT_IN_MUL_OVERFLOW_P:
417 /* These have constant results even if their operands are
418 non-constant. */
419 case BUILT_IN_CONSTANT_P:
420 case BUILT_IN_ATOMIC_ALWAYS_LOCK_FREE:
421 return true;
422 default:
423 return false;
427 /* Build a TARGET_EXPR, initializing the DECL with the VALUE. */
429 static tree
430 build_target_expr (tree decl, tree value, tsubst_flags_t complain)
432 tree t;
433 tree type = TREE_TYPE (decl);
435 value = mark_rvalue_use (value);
437 gcc_checking_assert (VOID_TYPE_P (TREE_TYPE (value))
438 || TREE_TYPE (decl) == TREE_TYPE (value)
439 /* On ARM ctors return 'this'. */
440 || (TYPE_PTR_P (TREE_TYPE (value))
441 && TREE_CODE (value) == CALL_EXPR)
442 || useless_type_conversion_p (TREE_TYPE (decl),
443 TREE_TYPE (value)));
445 if (complain & tf_no_cleanup)
446 /* The caller is building a new-expr and does not need a cleanup. */
447 t = NULL_TREE;
448 else
450 t = cxx_maybe_build_cleanup (decl, complain);
451 if (t == error_mark_node)
452 return error_mark_node;
454 t = build4 (TARGET_EXPR, type, decl, value, t, NULL_TREE);
455 if (EXPR_HAS_LOCATION (value))
456 SET_EXPR_LOCATION (t, EXPR_LOCATION (value));
457 /* We always set TREE_SIDE_EFFECTS so that expand_expr does not
458 ignore the TARGET_EXPR. If there really turn out to be no
459 side-effects, then the optimizer should be able to get rid of
460 whatever code is generated anyhow. */
461 TREE_SIDE_EFFECTS (t) = 1;
463 return t;
466 /* Return an undeclared local temporary of type TYPE for use in building a
467 TARGET_EXPR. */
469 static tree
470 build_local_temp (tree type)
472 tree slot = build_decl (input_location,
473 VAR_DECL, NULL_TREE, type);
474 DECL_ARTIFICIAL (slot) = 1;
475 DECL_IGNORED_P (slot) = 1;
476 DECL_CONTEXT (slot) = current_function_decl;
477 layout_decl (slot, 0);
478 return slot;
481 /* Set various status flags when building an AGGR_INIT_EXPR object T. */
483 static void
484 process_aggr_init_operands (tree t)
486 bool side_effects;
488 side_effects = TREE_SIDE_EFFECTS (t);
489 if (!side_effects)
491 int i, n;
492 n = TREE_OPERAND_LENGTH (t);
493 for (i = 1; i < n; i++)
495 tree op = TREE_OPERAND (t, i);
496 if (op && TREE_SIDE_EFFECTS (op))
498 side_effects = 1;
499 break;
503 TREE_SIDE_EFFECTS (t) = side_effects;
506 /* Build an AGGR_INIT_EXPR of class tcc_vl_exp with the indicated RETURN_TYPE,
507 FN, and SLOT. NARGS is the number of call arguments which are specified
508 as a tree array ARGS. */
510 static tree
511 build_aggr_init_array (tree return_type, tree fn, tree slot, int nargs,
512 tree *args)
514 tree t;
515 int i;
517 t = build_vl_exp (AGGR_INIT_EXPR, nargs + 3);
518 TREE_TYPE (t) = return_type;
519 AGGR_INIT_EXPR_FN (t) = fn;
520 AGGR_INIT_EXPR_SLOT (t) = slot;
521 for (i = 0; i < nargs; i++)
522 AGGR_INIT_EXPR_ARG (t, i) = args[i];
523 process_aggr_init_operands (t);
524 return t;
527 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
528 target. TYPE is the type to be initialized.
530 Build an AGGR_INIT_EXPR to represent the initialization. This function
531 differs from build_cplus_new in that an AGGR_INIT_EXPR can only be used
532 to initialize another object, whereas a TARGET_EXPR can either
533 initialize another object or create its own temporary object, and as a
534 result building up a TARGET_EXPR requires that the type's destructor be
535 callable. */
537 tree
538 build_aggr_init_expr (tree type, tree init)
540 tree fn;
541 tree slot;
542 tree rval;
543 int is_ctor;
545 /* Don't build AGGR_INIT_EXPR in a template. */
546 if (processing_template_decl)
547 return init;
549 fn = cp_get_callee (init);
550 if (fn == NULL_TREE)
551 return convert (type, init);
553 is_ctor = (TREE_CODE (fn) == ADDR_EXPR
554 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
555 && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0)));
557 /* We split the CALL_EXPR into its function and its arguments here.
558 Then, in expand_expr, we put them back together. The reason for
559 this is that this expression might be a default argument
560 expression. In that case, we need a new temporary every time the
561 expression is used. That's what break_out_target_exprs does; it
562 replaces every AGGR_INIT_EXPR with a copy that uses a fresh
563 temporary slot. Then, expand_expr builds up a call-expression
564 using the new slot. */
566 /* If we don't need to use a constructor to create an object of this
567 type, don't mess with AGGR_INIT_EXPR. */
568 if (is_ctor || TREE_ADDRESSABLE (type))
570 slot = build_local_temp (type);
572 if (TREE_CODE (init) == CALL_EXPR)
574 rval = build_aggr_init_array (void_type_node, fn, slot,
575 call_expr_nargs (init),
576 CALL_EXPR_ARGP (init));
577 AGGR_INIT_FROM_THUNK_P (rval)
578 = CALL_FROM_THUNK_P (init);
580 else
582 rval = build_aggr_init_array (void_type_node, fn, slot,
583 aggr_init_expr_nargs (init),
584 AGGR_INIT_EXPR_ARGP (init));
585 AGGR_INIT_FROM_THUNK_P (rval)
586 = AGGR_INIT_FROM_THUNK_P (init);
588 TREE_SIDE_EFFECTS (rval) = 1;
589 AGGR_INIT_VIA_CTOR_P (rval) = is_ctor;
590 TREE_NOTHROW (rval) = TREE_NOTHROW (init);
591 CALL_EXPR_OPERATOR_SYNTAX (rval) = CALL_EXPR_OPERATOR_SYNTAX (init);
592 CALL_EXPR_ORDERED_ARGS (rval) = CALL_EXPR_ORDERED_ARGS (init);
593 CALL_EXPR_REVERSE_ARGS (rval) = CALL_EXPR_REVERSE_ARGS (init);
595 else
596 rval = init;
598 return rval;
601 /* INIT is a CALL_EXPR or AGGR_INIT_EXPR which needs info about its
602 target. TYPE is the type that this initialization should appear to
603 have.
605 Build an encapsulation of the initialization to perform
606 and return it so that it can be processed by language-independent
607 and language-specific expression expanders. */
609 tree
610 build_cplus_new (tree type, tree init, tsubst_flags_t complain)
612 tree rval = build_aggr_init_expr (type, init);
613 tree slot;
615 if (!complete_type_or_maybe_complain (type, init, complain))
616 return error_mark_node;
618 /* Make sure that we're not trying to create an instance of an
619 abstract class. */
620 if (abstract_virtuals_error_sfinae (NULL_TREE, type, complain))
621 return error_mark_node;
623 if (TREE_CODE (rval) == AGGR_INIT_EXPR)
624 slot = AGGR_INIT_EXPR_SLOT (rval);
625 else if (TREE_CODE (rval) == CALL_EXPR
626 || TREE_CODE (rval) == CONSTRUCTOR)
627 slot = build_local_temp (type);
628 else
629 return rval;
631 rval = build_target_expr (slot, rval, complain);
633 if (rval != error_mark_node)
634 TARGET_EXPR_IMPLICIT_P (rval) = 1;
636 return rval;
639 /* Subroutine of build_vec_init_expr: Build up a single element
640 intialization as a proxy for the full array initialization to get things
641 marked as used and any appropriate diagnostics.
643 Since we're deferring building the actual constructor calls until
644 gimplification time, we need to build one now and throw it away so
645 that the relevant constructor gets mark_used before cgraph decides
646 what functions are needed. Here we assume that init is either
647 NULL_TREE, void_type_node (indicating value-initialization), or
648 another array to copy. */
650 static tree
651 build_vec_init_elt (tree type, tree init, tsubst_flags_t complain)
653 tree inner_type = strip_array_types (type);
654 vec<tree, va_gc> *argvec;
656 if (integer_zerop (array_type_nelts_total (type))
657 || !CLASS_TYPE_P (inner_type))
658 /* No interesting initialization to do. */
659 return integer_zero_node;
660 else if (init == void_type_node)
661 return build_value_init (inner_type, complain);
663 gcc_assert (init == NULL_TREE
664 || (same_type_ignoring_top_level_qualifiers_p
665 (type, TREE_TYPE (init))));
667 argvec = make_tree_vector ();
668 if (init)
670 tree init_type = strip_array_types (TREE_TYPE (init));
671 tree dummy = build_dummy_object (init_type);
672 if (!lvalue_p (init))
673 dummy = move (dummy);
674 argvec->quick_push (dummy);
676 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
677 &argvec, inner_type, LOOKUP_NORMAL,
678 complain);
679 release_tree_vector (argvec);
681 /* For a trivial constructor, build_over_call creates a TARGET_EXPR. But
682 we don't want one here because we aren't creating a temporary. */
683 if (TREE_CODE (init) == TARGET_EXPR)
684 init = TARGET_EXPR_INITIAL (init);
686 return init;
689 /* Return a TARGET_EXPR which expresses the initialization of an array to
690 be named later, either default-initialization or copy-initialization
691 from another array of the same type. */
693 tree
694 build_vec_init_expr (tree type, tree init, tsubst_flags_t complain)
696 tree slot;
697 bool value_init = false;
698 tree elt_init = build_vec_init_elt (type, init, complain);
700 if (init == void_type_node)
702 value_init = true;
703 init = NULL_TREE;
706 slot = build_local_temp (type);
707 init = build2 (VEC_INIT_EXPR, type, slot, init);
708 TREE_SIDE_EFFECTS (init) = true;
709 SET_EXPR_LOCATION (init, input_location);
711 if (cxx_dialect >= cxx11
712 && potential_constant_expression (elt_init))
713 VEC_INIT_EXPR_IS_CONSTEXPR (init) = true;
714 VEC_INIT_EXPR_VALUE_INIT (init) = value_init;
716 return init;
719 /* Give a helpful diagnostic for a non-constexpr VEC_INIT_EXPR in a context
720 that requires a constant expression. */
722 void
723 diagnose_non_constexpr_vec_init (tree expr)
725 tree type = TREE_TYPE (VEC_INIT_EXPR_SLOT (expr));
726 tree init, elt_init;
727 if (VEC_INIT_EXPR_VALUE_INIT (expr))
728 init = void_type_node;
729 else
730 init = VEC_INIT_EXPR_INIT (expr);
732 elt_init = build_vec_init_elt (type, init, tf_warning_or_error);
733 require_potential_constant_expression (elt_init);
736 tree
737 build_array_copy (tree init)
739 return build_vec_init_expr (TREE_TYPE (init), init, tf_warning_or_error);
742 /* Build a TARGET_EXPR using INIT to initialize a new temporary of the
743 indicated TYPE. */
745 tree
746 build_target_expr_with_type (tree init, tree type, tsubst_flags_t complain)
748 gcc_assert (!VOID_TYPE_P (type));
750 if (TREE_CODE (init) == TARGET_EXPR
751 || init == error_mark_node)
752 return init;
753 else if (CLASS_TYPE_P (type) && type_has_nontrivial_copy_init (type)
754 && !VOID_TYPE_P (TREE_TYPE (init))
755 && TREE_CODE (init) != COND_EXPR
756 && TREE_CODE (init) != CONSTRUCTOR
757 && TREE_CODE (init) != VA_ARG_EXPR)
758 /* We need to build up a copy constructor call. A void initializer
759 means we're being called from bot_manip. COND_EXPR is a special
760 case because we already have copies on the arms and we don't want
761 another one here. A CONSTRUCTOR is aggregate initialization, which
762 is handled separately. A VA_ARG_EXPR is magic creation of an
763 aggregate; there's no additional work to be done. */
764 return force_rvalue (init, complain);
766 return force_target_expr (type, init, complain);
769 /* Like the above function, but without the checking. This function should
770 only be used by code which is deliberately trying to subvert the type
771 system, such as call_builtin_trap. Or build_over_call, to avoid
772 infinite recursion. */
774 tree
775 force_target_expr (tree type, tree init, tsubst_flags_t complain)
777 tree slot;
779 gcc_assert (!VOID_TYPE_P (type));
781 slot = build_local_temp (type);
782 return build_target_expr (slot, init, complain);
785 /* Like build_target_expr_with_type, but use the type of INIT. */
787 tree
788 get_target_expr_sfinae (tree init, tsubst_flags_t complain)
790 if (TREE_CODE (init) == AGGR_INIT_EXPR)
791 return build_target_expr (AGGR_INIT_EXPR_SLOT (init), init, complain);
792 else if (TREE_CODE (init) == VEC_INIT_EXPR)
793 return build_target_expr (VEC_INIT_EXPR_SLOT (init), init, complain);
794 else
796 init = convert_bitfield_to_declared_type (init);
797 return build_target_expr_with_type (init, TREE_TYPE (init), complain);
801 tree
802 get_target_expr (tree init)
804 return get_target_expr_sfinae (init, tf_warning_or_error);
807 /* If EXPR is a bitfield reference, convert it to the declared type of
808 the bitfield, and return the resulting expression. Otherwise,
809 return EXPR itself. */
811 tree
812 convert_bitfield_to_declared_type (tree expr)
814 tree bitfield_type;
816 bitfield_type = is_bitfield_expr_with_lowered_type (expr);
817 if (bitfield_type)
818 expr = convert_to_integer_nofold (TYPE_MAIN_VARIANT (bitfield_type),
819 expr);
820 return expr;
823 /* EXPR is being used in an rvalue context. Return a version of EXPR
824 that is marked as an rvalue. */
826 tree
827 rvalue (tree expr)
829 tree type;
831 if (error_operand_p (expr))
832 return expr;
834 expr = mark_rvalue_use (expr);
836 /* [basic.lval]
838 Non-class rvalues always have cv-unqualified types. */
839 type = TREE_TYPE (expr);
840 if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
841 type = cv_unqualified (type);
843 /* We need to do this for rvalue refs as well to get the right answer
844 from decltype; see c++/36628. */
845 if (!processing_template_decl && glvalue_p (expr))
846 expr = build1 (NON_LVALUE_EXPR, type, expr);
847 else if (type != TREE_TYPE (expr))
848 expr = build_nop (type, expr);
850 return expr;
854 struct cplus_array_info
856 tree type;
857 tree domain;
860 struct cplus_array_hasher : ggc_ptr_hash<tree_node>
862 typedef cplus_array_info *compare_type;
864 static hashval_t hash (tree t);
865 static bool equal (tree, cplus_array_info *);
868 /* Hash an ARRAY_TYPE. K is really of type `tree'. */
870 hashval_t
871 cplus_array_hasher::hash (tree t)
873 hashval_t hash;
875 hash = TYPE_UID (TREE_TYPE (t));
876 if (TYPE_DOMAIN (t))
877 hash ^= TYPE_UID (TYPE_DOMAIN (t));
878 return hash;
881 /* Compare two ARRAY_TYPEs. K1 is really of type `tree', K2 is really
882 of type `cplus_array_info*'. */
884 bool
885 cplus_array_hasher::equal (tree t1, cplus_array_info *t2)
887 return (TREE_TYPE (t1) == t2->type && TYPE_DOMAIN (t1) == t2->domain);
890 /* Hash table containing dependent array types, which are unsuitable for
891 the language-independent type hash table. */
892 static GTY (()) hash_table<cplus_array_hasher> *cplus_array_htab;
894 /* Build an ARRAY_TYPE without laying it out. */
896 static tree
897 build_min_array_type (tree elt_type, tree index_type)
899 tree t = cxx_make_type (ARRAY_TYPE);
900 TREE_TYPE (t) = elt_type;
901 TYPE_DOMAIN (t) = index_type;
902 return t;
905 /* Set TYPE_CANONICAL like build_array_type_1, but using
906 build_cplus_array_type. */
908 static void
909 set_array_type_canon (tree t, tree elt_type, tree index_type)
911 /* Set the canonical type for this new node. */
912 if (TYPE_STRUCTURAL_EQUALITY_P (elt_type)
913 || (index_type && TYPE_STRUCTURAL_EQUALITY_P (index_type)))
914 SET_TYPE_STRUCTURAL_EQUALITY (t);
915 else if (TYPE_CANONICAL (elt_type) != elt_type
916 || (index_type && TYPE_CANONICAL (index_type) != index_type))
917 TYPE_CANONICAL (t)
918 = build_cplus_array_type (TYPE_CANONICAL (elt_type),
919 index_type
920 ? TYPE_CANONICAL (index_type) : index_type);
921 else
922 TYPE_CANONICAL (t) = t;
925 /* Like build_array_type, but handle special C++ semantics: an array of a
926 variant element type is a variant of the array of the main variant of
927 the element type. */
929 tree
930 build_cplus_array_type (tree elt_type, tree index_type)
932 tree t;
934 if (elt_type == error_mark_node || index_type == error_mark_node)
935 return error_mark_node;
937 bool dependent = (uses_template_parms (elt_type)
938 || (index_type && uses_template_parms (index_type)));
940 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
941 /* Start with an array of the TYPE_MAIN_VARIANT. */
942 t = build_cplus_array_type (TYPE_MAIN_VARIANT (elt_type),
943 index_type);
944 else if (dependent)
946 /* Since type_hash_canon calls layout_type, we need to use our own
947 hash table. */
948 cplus_array_info cai;
949 hashval_t hash;
951 if (cplus_array_htab == NULL)
952 cplus_array_htab = hash_table<cplus_array_hasher>::create_ggc (61);
954 hash = TYPE_UID (elt_type);
955 if (index_type)
956 hash ^= TYPE_UID (index_type);
957 cai.type = elt_type;
958 cai.domain = index_type;
960 tree *e = cplus_array_htab->find_slot_with_hash (&cai, hash, INSERT);
961 if (*e)
962 /* We have found the type: we're done. */
963 return (tree) *e;
964 else
966 /* Build a new array type. */
967 t = build_min_array_type (elt_type, index_type);
969 /* Store it in the hash table. */
970 *e = t;
972 /* Set the canonical type for this new node. */
973 set_array_type_canon (t, elt_type, index_type);
976 else
978 bool typeless_storage
979 = (elt_type == unsigned_char_type_node
980 || elt_type == signed_char_type_node
981 || elt_type == char_type_node
982 || (TREE_CODE (elt_type) == ENUMERAL_TYPE
983 && TYPE_CONTEXT (elt_type) == std_node
984 && !strcmp ("byte", TYPE_NAME_STRING (elt_type))));
985 t = build_array_type (elt_type, index_type, typeless_storage);
988 /* Now check whether we already have this array variant. */
989 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
991 tree m = t;
992 for (t = m; t; t = TYPE_NEXT_VARIANT (t))
993 if (TREE_TYPE (t) == elt_type
994 && TYPE_NAME (t) == NULL_TREE
995 && TYPE_ATTRIBUTES (t) == NULL_TREE)
996 break;
997 if (!t)
999 t = build_min_array_type (elt_type, index_type);
1000 set_array_type_canon (t, elt_type, index_type);
1001 if (!dependent)
1003 layout_type (t);
1004 /* Make sure sizes are shared with the main variant.
1005 layout_type can't be called after setting TYPE_NEXT_VARIANT,
1006 as it will overwrite alignment etc. of all variants. */
1007 TYPE_SIZE (t) = TYPE_SIZE (m);
1008 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (m);
1009 TYPE_TYPELESS_STORAGE (t) = TYPE_TYPELESS_STORAGE (m);
1012 TYPE_MAIN_VARIANT (t) = m;
1013 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
1014 TYPE_NEXT_VARIANT (m) = t;
1018 /* Avoid spurious warnings with VLAs (c++/54583). */
1019 if (TYPE_SIZE (t) && EXPR_P (TYPE_SIZE (t)))
1020 TREE_NO_WARNING (TYPE_SIZE (t)) = 1;
1022 /* Push these needs up to the ARRAY_TYPE so that initialization takes
1023 place more easily. */
1024 bool needs_ctor = (TYPE_NEEDS_CONSTRUCTING (t)
1025 = TYPE_NEEDS_CONSTRUCTING (elt_type));
1026 bool needs_dtor = (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1027 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type));
1029 if (!dependent && t == TYPE_MAIN_VARIANT (t)
1030 && !COMPLETE_TYPE_P (t) && COMPLETE_TYPE_P (elt_type))
1032 /* The element type has been completed since the last time we saw
1033 this array type; update the layout and 'tor flags for any variants
1034 that need it. */
1035 layout_type (t);
1036 for (tree v = TYPE_NEXT_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v))
1038 TYPE_NEEDS_CONSTRUCTING (v) = needs_ctor;
1039 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (v) = needs_dtor;
1043 return t;
1046 /* Return an ARRAY_TYPE with element type ELT and length N. */
1048 tree
1049 build_array_of_n_type (tree elt, int n)
1051 return build_cplus_array_type (elt, build_index_type (size_int (n - 1)));
1054 /* True iff T is an N3639 array of runtime bound (VLA). These were
1055 approved for C++14 but then removed. */
1057 bool
1058 array_of_runtime_bound_p (tree t)
1060 if (!t || TREE_CODE (t) != ARRAY_TYPE)
1061 return false;
1062 if (variably_modified_type_p (TREE_TYPE (t), NULL_TREE))
1063 return false;
1064 tree dom = TYPE_DOMAIN (t);
1065 if (!dom)
1066 return false;
1067 tree max = TYPE_MAX_VALUE (dom);
1068 return (!potential_rvalue_constant_expression (max)
1069 || (!value_dependent_expression_p (max) && !TREE_CONSTANT (max)));
1072 /* Return a reference type node referring to TO_TYPE. If RVAL is
1073 true, return an rvalue reference type, otherwise return an lvalue
1074 reference type. If a type node exists, reuse it, otherwise create
1075 a new one. */
1076 tree
1077 cp_build_reference_type (tree to_type, bool rval)
1079 tree lvalue_ref, t;
1081 if (to_type == error_mark_node)
1082 return error_mark_node;
1084 if (TREE_CODE (to_type) == REFERENCE_TYPE)
1086 rval = rval && TYPE_REF_IS_RVALUE (to_type);
1087 to_type = TREE_TYPE (to_type);
1090 lvalue_ref = build_reference_type (to_type);
1091 if (!rval)
1092 return lvalue_ref;
1094 /* This code to create rvalue reference types is based on and tied
1095 to the code creating lvalue reference types in the middle-end
1096 functions build_reference_type_for_mode and build_reference_type.
1098 It works by putting the rvalue reference type nodes after the
1099 lvalue reference nodes in the TYPE_NEXT_REF_TO linked list, so
1100 they will effectively be ignored by the middle end. */
1102 for (t = lvalue_ref; (t = TYPE_NEXT_REF_TO (t)); )
1103 if (TYPE_REF_IS_RVALUE (t))
1104 return t;
1106 t = build_distinct_type_copy (lvalue_ref);
1108 TYPE_REF_IS_RVALUE (t) = true;
1109 TYPE_NEXT_REF_TO (t) = TYPE_NEXT_REF_TO (lvalue_ref);
1110 TYPE_NEXT_REF_TO (lvalue_ref) = t;
1112 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
1113 SET_TYPE_STRUCTURAL_EQUALITY (t);
1114 else if (TYPE_CANONICAL (to_type) != to_type)
1115 TYPE_CANONICAL (t)
1116 = cp_build_reference_type (TYPE_CANONICAL (to_type), rval);
1117 else
1118 TYPE_CANONICAL (t) = t;
1120 layout_type (t);
1122 return t;
1126 /* Returns EXPR cast to rvalue reference type, like std::move. */
1128 tree
1129 move (tree expr)
1131 tree type = TREE_TYPE (expr);
1132 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
1133 type = cp_build_reference_type (type, /*rval*/true);
1134 return build_static_cast (type, expr, tf_warning_or_error);
1137 /* Used by the C++ front end to build qualified array types. However,
1138 the C version of this function does not properly maintain canonical
1139 types (which are not used in C). */
1140 tree
1141 c_build_qualified_type (tree type, int type_quals, tree /* orig_qual_type */,
1142 size_t /* orig_qual_indirect */)
1144 return cp_build_qualified_type (type, type_quals);
1148 /* Make a variant of TYPE, qualified with the TYPE_QUALS. Handles
1149 arrays correctly. In particular, if TYPE is an array of T's, and
1150 TYPE_QUALS is non-empty, returns an array of qualified T's.
1152 FLAGS determines how to deal with ill-formed qualifications. If
1153 tf_ignore_bad_quals is set, then bad qualifications are dropped
1154 (this is permitted if TYPE was introduced via a typedef or template
1155 type parameter). If bad qualifications are dropped and tf_warning
1156 is set, then a warning is issued for non-const qualifications. If
1157 tf_ignore_bad_quals is not set and tf_error is not set, we
1158 return error_mark_node. Otherwise, we issue an error, and ignore
1159 the qualifications.
1161 Qualification of a reference type is valid when the reference came
1162 via a typedef or template type argument. [dcl.ref] No such
1163 dispensation is provided for qualifying a function type. [dcl.fct]
1164 DR 295 queries this and the proposed resolution brings it into line
1165 with qualifying a reference. We implement the DR. We also behave
1166 in a similar manner for restricting non-pointer types. */
1168 tree
1169 cp_build_qualified_type_real (tree type,
1170 int type_quals,
1171 tsubst_flags_t complain)
1173 tree result;
1174 int bad_quals = TYPE_UNQUALIFIED;
1176 if (type == error_mark_node)
1177 return type;
1179 if (type_quals == cp_type_quals (type))
1180 return type;
1182 if (TREE_CODE (type) == ARRAY_TYPE)
1184 /* In C++, the qualification really applies to the array element
1185 type. Obtain the appropriately qualified element type. */
1186 tree t;
1187 tree element_type
1188 = cp_build_qualified_type_real (TREE_TYPE (type),
1189 type_quals,
1190 complain);
1192 if (element_type == error_mark_node)
1193 return error_mark_node;
1195 /* See if we already have an identically qualified type. Tests
1196 should be equivalent to those in check_qualified_type. */
1197 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1198 if (TREE_TYPE (t) == element_type
1199 && TYPE_NAME (t) == TYPE_NAME (type)
1200 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
1201 && attribute_list_equal (TYPE_ATTRIBUTES (t),
1202 TYPE_ATTRIBUTES (type)))
1203 break;
1205 if (!t)
1207 t = build_cplus_array_type (element_type, TYPE_DOMAIN (type));
1209 /* Keep the typedef name. */
1210 if (TYPE_NAME (t) != TYPE_NAME (type))
1212 t = build_variant_type_copy (t);
1213 TYPE_NAME (t) = TYPE_NAME (type);
1214 SET_TYPE_ALIGN (t, TYPE_ALIGN (type));
1215 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (type);
1219 /* Even if we already had this variant, we update
1220 TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case
1221 they changed since the variant was originally created.
1223 This seems hokey; if there is some way to use a previous
1224 variant *without* coming through here,
1225 TYPE_NEEDS_CONSTRUCTING will never be updated. */
1226 TYPE_NEEDS_CONSTRUCTING (t)
1227 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
1228 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1229 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
1230 return t;
1232 else if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
1234 tree t = PACK_EXPANSION_PATTERN (type);
1236 t = cp_build_qualified_type_real (t, type_quals, complain);
1237 return make_pack_expansion (t, complain);
1240 /* A reference or method type shall not be cv-qualified.
1241 [dcl.ref], [dcl.fct]. This used to be an error, but as of DR 295
1242 (in CD1) we always ignore extra cv-quals on functions. */
1243 if (type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)
1244 && (TREE_CODE (type) == REFERENCE_TYPE
1245 || TREE_CODE (type) == FUNCTION_TYPE
1246 || TREE_CODE (type) == METHOD_TYPE))
1248 if (TREE_CODE (type) == REFERENCE_TYPE)
1249 bad_quals |= type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1250 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1253 /* But preserve any function-cv-quals on a FUNCTION_TYPE. */
1254 if (TREE_CODE (type) == FUNCTION_TYPE)
1255 type_quals |= type_memfn_quals (type);
1257 /* A restrict-qualified type must be a pointer (or reference)
1258 to object or incomplete type. */
1259 if ((type_quals & TYPE_QUAL_RESTRICT)
1260 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1261 && TREE_CODE (type) != TYPENAME_TYPE
1262 && !POINTER_TYPE_P (type))
1264 bad_quals |= TYPE_QUAL_RESTRICT;
1265 type_quals &= ~TYPE_QUAL_RESTRICT;
1268 if (bad_quals == TYPE_UNQUALIFIED
1269 || (complain & tf_ignore_bad_quals))
1270 /*OK*/;
1271 else if (!(complain & tf_error))
1272 return error_mark_node;
1273 else
1275 tree bad_type = build_qualified_type (ptr_type_node, bad_quals);
1276 error ("%qV qualifiers cannot be applied to %qT",
1277 bad_type, type);
1280 /* Retrieve (or create) the appropriately qualified variant. */
1281 result = build_qualified_type (type, type_quals);
1283 /* Preserve exception specs and ref-qualifier since build_qualified_type
1284 doesn't know about them. */
1285 if (TREE_CODE (result) == FUNCTION_TYPE
1286 || TREE_CODE (result) == METHOD_TYPE)
1288 result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type));
1289 result = build_ref_qualified_type (result, type_memfn_rqual (type));
1292 return result;
1295 /* Return TYPE with const and volatile removed. */
1297 tree
1298 cv_unqualified (tree type)
1300 int quals;
1302 if (type == error_mark_node)
1303 return type;
1305 quals = cp_type_quals (type);
1306 quals &= ~(TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE);
1307 return cp_build_qualified_type (type, quals);
1310 /* Subroutine of strip_typedefs. We want to apply to RESULT the attributes
1311 from ATTRIBS that affect type identity, and no others. If any are not
1312 applied, set *remove_attributes to true. */
1314 static tree
1315 apply_identity_attributes (tree result, tree attribs, bool *remove_attributes)
1317 tree first_ident = NULL_TREE;
1318 tree new_attribs = NULL_TREE;
1319 tree *p = &new_attribs;
1321 if (OVERLOAD_TYPE_P (result))
1323 /* On classes and enums all attributes are ingrained. */
1324 gcc_assert (attribs == TYPE_ATTRIBUTES (result));
1325 return result;
1328 for (tree a = attribs; a; a = TREE_CHAIN (a))
1330 const attribute_spec *as
1331 = lookup_attribute_spec (get_attribute_name (a));
1332 if (as && as->affects_type_identity)
1334 if (!first_ident)
1335 first_ident = a;
1336 else if (first_ident == error_mark_node)
1338 *p = tree_cons (TREE_PURPOSE (a), TREE_VALUE (a), NULL_TREE);
1339 p = &TREE_CHAIN (*p);
1342 else if (first_ident)
1344 for (tree a2 = first_ident; a2; a2 = TREE_CHAIN (a2))
1346 *p = tree_cons (TREE_PURPOSE (a2), TREE_VALUE (a2), NULL_TREE);
1347 p = &TREE_CHAIN (*p);
1349 first_ident = error_mark_node;
1352 if (first_ident != error_mark_node)
1353 new_attribs = first_ident;
1355 if (first_ident == attribs)
1356 /* All attributes affected type identity. */;
1357 else
1358 *remove_attributes = true;
1360 return cp_build_type_attribute_variant (result, new_attribs);
1363 /* Builds a qualified variant of T that is not a typedef variant.
1364 E.g. consider the following declarations:
1365 typedef const int ConstInt;
1366 typedef ConstInt* PtrConstInt;
1367 If T is PtrConstInt, this function returns a type representing
1368 const int*.
1369 In other words, if T is a typedef, the function returns the underlying type.
1370 The cv-qualification and attributes of the type returned match the
1371 input type.
1372 They will always be compatible types.
1373 The returned type is built so that all of its subtypes
1374 recursively have their typedefs stripped as well.
1376 This is different from just returning TYPE_CANONICAL (T)
1377 Because of several reasons:
1378 * If T is a type that needs structural equality
1379 its TYPE_CANONICAL (T) will be NULL.
1380 * TYPE_CANONICAL (T) desn't carry type attributes
1381 and loses template parameter names.
1383 If REMOVE_ATTRIBUTES is non-null, also strip attributes that don't
1384 affect type identity, and set the referent to true if any were
1385 stripped. */
1387 tree
1388 strip_typedefs (tree t, bool *remove_attributes)
1390 tree result = NULL, type = NULL, t0 = NULL;
1392 if (!t || t == error_mark_node)
1393 return t;
1395 if (TREE_CODE (t) == TREE_LIST)
1397 bool changed = false;
1398 vec<tree,va_gc> *vec = make_tree_vector ();
1399 tree r = t;
1400 for (; t; t = TREE_CHAIN (t))
1402 gcc_assert (!TREE_PURPOSE (t));
1403 tree elt = strip_typedefs (TREE_VALUE (t), remove_attributes);
1404 if (elt != TREE_VALUE (t))
1405 changed = true;
1406 vec_safe_push (vec, elt);
1408 if (changed)
1409 r = build_tree_list_vec (vec);
1410 release_tree_vector (vec);
1411 return r;
1414 gcc_assert (TYPE_P (t));
1416 if (t == TYPE_CANONICAL (t))
1417 return t;
1419 if (dependent_alias_template_spec_p (t))
1420 /* DR 1558: However, if the template-id is dependent, subsequent
1421 template argument substitution still applies to the template-id. */
1422 return t;
1424 switch (TREE_CODE (t))
1426 case POINTER_TYPE:
1427 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1428 result = build_pointer_type (type);
1429 break;
1430 case REFERENCE_TYPE:
1431 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1432 result = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
1433 break;
1434 case OFFSET_TYPE:
1435 t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t), remove_attributes);
1436 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1437 result = build_offset_type (t0, type);
1438 break;
1439 case RECORD_TYPE:
1440 if (TYPE_PTRMEMFUNC_P (t))
1442 t0 = strip_typedefs (TYPE_PTRMEMFUNC_FN_TYPE (t), remove_attributes);
1443 result = build_ptrmemfunc_type (t0);
1445 break;
1446 case ARRAY_TYPE:
1447 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1448 t0 = strip_typedefs (TYPE_DOMAIN (t), remove_attributes);
1449 result = build_cplus_array_type (type, t0);
1450 break;
1451 case FUNCTION_TYPE:
1452 case METHOD_TYPE:
1454 tree arg_types = NULL, arg_node, arg_node2, arg_type;
1455 bool changed;
1457 /* Because we stomp on TREE_PURPOSE of TYPE_ARG_TYPES in many places
1458 around the compiler (e.g. cp_parser_late_parsing_default_args), we
1459 can't expect that re-hashing a function type will find a previous
1460 equivalent type, so try to reuse the input type if nothing has
1461 changed. If the type is itself a variant, that will change. */
1462 bool is_variant = typedef_variant_p (t);
1463 if (remove_attributes
1464 && (TYPE_ATTRIBUTES (t) || TYPE_USER_ALIGN (t)))
1465 is_variant = true;
1467 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1468 tree canon_spec = (flag_noexcept_type
1469 ? canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (t))
1470 : NULL_TREE);
1471 changed = (type != TREE_TYPE (t) || is_variant
1472 || TYPE_RAISES_EXCEPTIONS (t) != canon_spec);
1474 for (arg_node = TYPE_ARG_TYPES (t);
1475 arg_node;
1476 arg_node = TREE_CHAIN (arg_node))
1478 if (arg_node == void_list_node)
1479 break;
1480 arg_type = strip_typedefs (TREE_VALUE (arg_node),
1481 remove_attributes);
1482 gcc_assert (arg_type);
1483 if (arg_type == TREE_VALUE (arg_node) && !changed)
1484 continue;
1486 if (!changed)
1488 changed = true;
1489 for (arg_node2 = TYPE_ARG_TYPES (t);
1490 arg_node2 != arg_node;
1491 arg_node2 = TREE_CHAIN (arg_node2))
1492 arg_types
1493 = tree_cons (TREE_PURPOSE (arg_node2),
1494 TREE_VALUE (arg_node2), arg_types);
1497 arg_types
1498 = tree_cons (TREE_PURPOSE (arg_node), arg_type, arg_types);
1501 if (!changed)
1502 return t;
1504 if (arg_types)
1505 arg_types = nreverse (arg_types);
1507 /* A list of parameters not ending with an ellipsis
1508 must end with void_list_node. */
1509 if (arg_node)
1510 arg_types = chainon (arg_types, void_list_node);
1512 if (TREE_CODE (t) == METHOD_TYPE)
1514 tree class_type = TREE_TYPE (TREE_VALUE (arg_types));
1515 gcc_assert (class_type);
1516 result =
1517 build_method_type_directly (class_type, type,
1518 TREE_CHAIN (arg_types));
1519 result
1520 = build_ref_qualified_type (result, type_memfn_rqual (t));
1522 else
1524 result = build_function_type (type,
1525 arg_types);
1526 result = apply_memfn_quals (result,
1527 type_memfn_quals (t),
1528 type_memfn_rqual (t));
1531 if (canon_spec)
1532 result = build_exception_variant (result, canon_spec);
1533 if (TYPE_HAS_LATE_RETURN_TYPE (t))
1534 TYPE_HAS_LATE_RETURN_TYPE (result) = 1;
1536 break;
1537 case TYPENAME_TYPE:
1539 bool changed = false;
1540 tree fullname = TYPENAME_TYPE_FULLNAME (t);
1541 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
1542 && TREE_OPERAND (fullname, 1))
1544 tree args = TREE_OPERAND (fullname, 1);
1545 tree new_args = copy_node (args);
1546 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
1548 tree arg = TREE_VEC_ELT (args, i);
1549 tree strip_arg;
1550 if (TYPE_P (arg))
1551 strip_arg = strip_typedefs (arg, remove_attributes);
1552 else
1553 strip_arg = strip_typedefs_expr (arg, remove_attributes);
1554 TREE_VEC_ELT (new_args, i) = strip_arg;
1555 if (strip_arg != arg)
1556 changed = true;
1558 if (changed)
1560 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_args)
1561 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
1562 fullname
1563 = lookup_template_function (TREE_OPERAND (fullname, 0),
1564 new_args);
1566 else
1567 ggc_free (new_args);
1569 tree ctx = strip_typedefs (TYPE_CONTEXT (t), remove_attributes);
1570 if (!changed && ctx == TYPE_CONTEXT (t) && !typedef_variant_p (t))
1571 return t;
1572 tree name = fullname;
1573 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
1574 name = TREE_OPERAND (fullname, 0);
1575 /* Use build_typename_type rather than make_typename_type because we
1576 don't want to resolve it here, just strip typedefs. */
1577 result = build_typename_type (ctx, name, fullname, typename_type);
1579 break;
1580 case DECLTYPE_TYPE:
1581 result = strip_typedefs_expr (DECLTYPE_TYPE_EXPR (t),
1582 remove_attributes);
1583 if (result == DECLTYPE_TYPE_EXPR (t))
1584 result = NULL_TREE;
1585 else
1586 result = (finish_decltype_type
1587 (result,
1588 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t),
1589 tf_none));
1590 break;
1591 case UNDERLYING_TYPE:
1592 type = strip_typedefs (UNDERLYING_TYPE_TYPE (t), remove_attributes);
1593 result = finish_underlying_type (type);
1594 break;
1595 default:
1596 break;
1599 if (!result)
1601 if (typedef_variant_p (t))
1603 /* Explicitly get the underlying type, as TYPE_MAIN_VARIANT doesn't
1604 strip typedefs with attributes. */
1605 result = TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (TYPE_NAME (t)));
1606 result = strip_typedefs (result);
1608 else
1609 result = TYPE_MAIN_VARIANT (t);
1611 gcc_assert (!typedef_variant_p (result));
1613 if (COMPLETE_TYPE_P (result) && !COMPLETE_TYPE_P (t))
1614 /* If RESULT is complete and T isn't, it's likely the case that T
1615 is a variant of RESULT which hasn't been updated yet. Skip the
1616 attribute handling. */;
1617 else
1619 if (TYPE_USER_ALIGN (t) != TYPE_USER_ALIGN (result)
1620 || TYPE_ALIGN (t) != TYPE_ALIGN (result))
1622 gcc_assert (TYPE_USER_ALIGN (t));
1623 if (remove_attributes)
1624 *remove_attributes = true;
1625 else
1627 if (TYPE_ALIGN (t) == TYPE_ALIGN (result))
1628 result = build_variant_type_copy (result);
1629 else
1630 result = build_aligned_type (result, TYPE_ALIGN (t));
1631 TYPE_USER_ALIGN (result) = true;
1635 if (TYPE_ATTRIBUTES (t))
1637 if (remove_attributes)
1638 result = apply_identity_attributes (result, TYPE_ATTRIBUTES (t),
1639 remove_attributes);
1640 else
1641 result = cp_build_type_attribute_variant (result,
1642 TYPE_ATTRIBUTES (t));
1646 return cp_build_qualified_type (result, cp_type_quals (t));
1649 /* Like strip_typedefs above, but works on expressions, so that in
1651 template<class T> struct A
1653 typedef T TT;
1654 B<sizeof(TT)> b;
1657 sizeof(TT) is replaced by sizeof(T). */
1659 tree
1660 strip_typedefs_expr (tree t, bool *remove_attributes)
1662 unsigned i,n;
1663 tree r, type, *ops;
1664 enum tree_code code;
1666 if (t == NULL_TREE || t == error_mark_node)
1667 return t;
1669 if (DECL_P (t) || CONSTANT_CLASS_P (t))
1670 return t;
1672 /* Some expressions have type operands, so let's handle types here rather
1673 than check TYPE_P in multiple places below. */
1674 if (TYPE_P (t))
1675 return strip_typedefs (t, remove_attributes);
1677 code = TREE_CODE (t);
1678 switch (code)
1680 case IDENTIFIER_NODE:
1681 case TEMPLATE_PARM_INDEX:
1682 case OVERLOAD:
1683 case BASELINK:
1684 case ARGUMENT_PACK_SELECT:
1685 return t;
1687 case TRAIT_EXPR:
1689 tree type1 = strip_typedefs (TRAIT_EXPR_TYPE1 (t), remove_attributes);
1690 tree type2 = strip_typedefs (TRAIT_EXPR_TYPE2 (t), remove_attributes);
1691 if (type1 == TRAIT_EXPR_TYPE1 (t)
1692 && type2 == TRAIT_EXPR_TYPE2 (t))
1693 return t;
1694 r = copy_node (t);
1695 TRAIT_EXPR_TYPE1 (r) = type1;
1696 TRAIT_EXPR_TYPE2 (r) = type2;
1697 return r;
1700 case TREE_LIST:
1702 vec<tree, va_gc> *vec = make_tree_vector ();
1703 bool changed = false;
1704 tree it;
1705 for (it = t; it; it = TREE_CHAIN (it))
1707 tree val = strip_typedefs_expr (TREE_VALUE (t), remove_attributes);
1708 vec_safe_push (vec, val);
1709 if (val != TREE_VALUE (t))
1710 changed = true;
1711 gcc_assert (TREE_PURPOSE (it) == NULL_TREE);
1713 if (changed)
1715 r = NULL_TREE;
1716 FOR_EACH_VEC_ELT_REVERSE (*vec, i, it)
1717 r = tree_cons (NULL_TREE, it, r);
1719 else
1720 r = t;
1721 release_tree_vector (vec);
1722 return r;
1725 case TREE_VEC:
1727 bool changed = false;
1728 vec<tree, va_gc> *vec = make_tree_vector ();
1729 n = TREE_VEC_LENGTH (t);
1730 vec_safe_reserve (vec, n);
1731 for (i = 0; i < n; ++i)
1733 tree op = strip_typedefs_expr (TREE_VEC_ELT (t, i),
1734 remove_attributes);
1735 vec->quick_push (op);
1736 if (op != TREE_VEC_ELT (t, i))
1737 changed = true;
1739 if (changed)
1741 r = copy_node (t);
1742 for (i = 0; i < n; ++i)
1743 TREE_VEC_ELT (r, i) = (*vec)[i];
1744 NON_DEFAULT_TEMPLATE_ARGS_COUNT (r)
1745 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
1747 else
1748 r = t;
1749 release_tree_vector (vec);
1750 return r;
1753 case CONSTRUCTOR:
1755 bool changed = false;
1756 vec<constructor_elt, va_gc> *vec
1757 = vec_safe_copy (CONSTRUCTOR_ELTS (t));
1758 n = CONSTRUCTOR_NELTS (t);
1759 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1760 for (i = 0; i < n; ++i)
1762 constructor_elt *e = &(*vec)[i];
1763 tree op = strip_typedefs_expr (e->value, remove_attributes);
1764 if (op != e->value)
1766 changed = true;
1767 e->value = op;
1769 gcc_checking_assert
1770 (e->index == strip_typedefs_expr (e->index, remove_attributes));
1773 if (!changed && type == TREE_TYPE (t))
1775 vec_free (vec);
1776 return t;
1778 else
1780 r = copy_node (t);
1781 TREE_TYPE (r) = type;
1782 CONSTRUCTOR_ELTS (r) = vec;
1783 return r;
1787 case LAMBDA_EXPR:
1788 error ("lambda-expression in a constant expression");
1789 return error_mark_node;
1791 case STATEMENT_LIST:
1792 error ("statement-expression in a constant expression");
1793 return error_mark_node;
1795 default:
1796 break;
1799 gcc_assert (EXPR_P (t));
1801 n = cp_tree_operand_length (t);
1802 ops = XALLOCAVEC (tree, n);
1803 type = TREE_TYPE (t);
1805 switch (code)
1807 CASE_CONVERT:
1808 case IMPLICIT_CONV_EXPR:
1809 case DYNAMIC_CAST_EXPR:
1810 case STATIC_CAST_EXPR:
1811 case CONST_CAST_EXPR:
1812 case REINTERPRET_CAST_EXPR:
1813 case CAST_EXPR:
1814 case NEW_EXPR:
1815 type = strip_typedefs (type, remove_attributes);
1816 /* fallthrough */
1818 default:
1819 for (i = 0; i < n; ++i)
1820 ops[i] = strip_typedefs_expr (TREE_OPERAND (t, i), remove_attributes);
1821 break;
1824 /* If nothing changed, return t. */
1825 for (i = 0; i < n; ++i)
1826 if (ops[i] != TREE_OPERAND (t, i))
1827 break;
1828 if (i == n && type == TREE_TYPE (t))
1829 return t;
1831 r = copy_node (t);
1832 TREE_TYPE (r) = type;
1833 for (i = 0; i < n; ++i)
1834 TREE_OPERAND (r, i) = ops[i];
1835 return r;
1838 /* Makes a copy of BINFO and TYPE, which is to be inherited into a
1839 graph dominated by T. If BINFO is NULL, TYPE is a dependent base,
1840 and we do a shallow copy. If BINFO is non-NULL, we do a deep copy.
1841 VIRT indicates whether TYPE is inherited virtually or not.
1842 IGO_PREV points at the previous binfo of the inheritance graph
1843 order chain. The newly copied binfo's TREE_CHAIN forms this
1844 ordering.
1846 The CLASSTYPE_VBASECLASSES vector of T is constructed in the
1847 correct order. That is in the order the bases themselves should be
1848 constructed in.
1850 The BINFO_INHERITANCE of a virtual base class points to the binfo
1851 of the most derived type. ??? We could probably change this so that
1852 BINFO_INHERITANCE becomes synonymous with BINFO_PRIMARY, and hence
1853 remove a field. They currently can only differ for primary virtual
1854 virtual bases. */
1856 tree
1857 copy_binfo (tree binfo, tree type, tree t, tree *igo_prev, int virt)
1859 tree new_binfo;
1861 if (virt)
1863 /* See if we've already made this virtual base. */
1864 new_binfo = binfo_for_vbase (type, t);
1865 if (new_binfo)
1866 return new_binfo;
1869 new_binfo = make_tree_binfo (binfo ? BINFO_N_BASE_BINFOS (binfo) : 0);
1870 BINFO_TYPE (new_binfo) = type;
1872 /* Chain it into the inheritance graph. */
1873 TREE_CHAIN (*igo_prev) = new_binfo;
1874 *igo_prev = new_binfo;
1876 if (binfo && !BINFO_DEPENDENT_BASE_P (binfo))
1878 int ix;
1879 tree base_binfo;
1881 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), type));
1883 BINFO_OFFSET (new_binfo) = BINFO_OFFSET (binfo);
1884 BINFO_VIRTUALS (new_binfo) = BINFO_VIRTUALS (binfo);
1886 /* We do not need to copy the accesses, as they are read only. */
1887 BINFO_BASE_ACCESSES (new_binfo) = BINFO_BASE_ACCESSES (binfo);
1889 /* Recursively copy base binfos of BINFO. */
1890 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1892 tree new_base_binfo;
1893 new_base_binfo = copy_binfo (base_binfo, BINFO_TYPE (base_binfo),
1894 t, igo_prev,
1895 BINFO_VIRTUAL_P (base_binfo));
1897 if (!BINFO_INHERITANCE_CHAIN (new_base_binfo))
1898 BINFO_INHERITANCE_CHAIN (new_base_binfo) = new_binfo;
1899 BINFO_BASE_APPEND (new_binfo, new_base_binfo);
1902 else
1903 BINFO_DEPENDENT_BASE_P (new_binfo) = 1;
1905 if (virt)
1907 /* Push it onto the list after any virtual bases it contains
1908 will have been pushed. */
1909 CLASSTYPE_VBASECLASSES (t)->quick_push (new_binfo);
1910 BINFO_VIRTUAL_P (new_binfo) = 1;
1911 BINFO_INHERITANCE_CHAIN (new_binfo) = TYPE_BINFO (t);
1914 return new_binfo;
1917 /* Hashing of lists so that we don't make duplicates.
1918 The entry point is `list_hash_canon'. */
1920 struct list_proxy
1922 tree purpose;
1923 tree value;
1924 tree chain;
1927 struct list_hasher : ggc_ptr_hash<tree_node>
1929 typedef list_proxy *compare_type;
1931 static hashval_t hash (tree);
1932 static bool equal (tree, list_proxy *);
1935 /* Now here is the hash table. When recording a list, it is added
1936 to the slot whose index is the hash code mod the table size.
1937 Note that the hash table is used for several kinds of lists.
1938 While all these live in the same table, they are completely independent,
1939 and the hash code is computed differently for each of these. */
1941 static GTY (()) hash_table<list_hasher> *list_hash_table;
1943 /* Compare ENTRY (an entry in the hash table) with DATA (a list_proxy
1944 for a node we are thinking about adding). */
1946 bool
1947 list_hasher::equal (tree t, list_proxy *proxy)
1949 return (TREE_VALUE (t) == proxy->value
1950 && TREE_PURPOSE (t) == proxy->purpose
1951 && TREE_CHAIN (t) == proxy->chain);
1954 /* Compute a hash code for a list (chain of TREE_LIST nodes
1955 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
1956 TREE_COMMON slots), by adding the hash codes of the individual entries. */
1958 static hashval_t
1959 list_hash_pieces (tree purpose, tree value, tree chain)
1961 hashval_t hashcode = 0;
1963 if (chain)
1964 hashcode += TREE_HASH (chain);
1966 if (value)
1967 hashcode += TREE_HASH (value);
1968 else
1969 hashcode += 1007;
1970 if (purpose)
1971 hashcode += TREE_HASH (purpose);
1972 else
1973 hashcode += 1009;
1974 return hashcode;
1977 /* Hash an already existing TREE_LIST. */
1979 hashval_t
1980 list_hasher::hash (tree t)
1982 return list_hash_pieces (TREE_PURPOSE (t),
1983 TREE_VALUE (t),
1984 TREE_CHAIN (t));
1987 /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
1988 object for an identical list if one already exists. Otherwise, build a
1989 new one, and record it as the canonical object. */
1991 tree
1992 hash_tree_cons (tree purpose, tree value, tree chain)
1994 int hashcode = 0;
1995 tree *slot;
1996 struct list_proxy proxy;
1998 /* Hash the list node. */
1999 hashcode = list_hash_pieces (purpose, value, chain);
2000 /* Create a proxy for the TREE_LIST we would like to create. We
2001 don't actually create it so as to avoid creating garbage. */
2002 proxy.purpose = purpose;
2003 proxy.value = value;
2004 proxy.chain = chain;
2005 /* See if it is already in the table. */
2006 slot = list_hash_table->find_slot_with_hash (&proxy, hashcode, INSERT);
2007 /* If not, create a new node. */
2008 if (!*slot)
2009 *slot = tree_cons (purpose, value, chain);
2010 return (tree) *slot;
2013 /* Constructor for hashed lists. */
2015 tree
2016 hash_tree_chain (tree value, tree chain)
2018 return hash_tree_cons (NULL_TREE, value, chain);
2021 void
2022 debug_binfo (tree elem)
2024 HOST_WIDE_INT n;
2025 tree virtuals;
2027 fprintf (stderr, "type \"%s\", offset = " HOST_WIDE_INT_PRINT_DEC
2028 "\nvtable type:\n",
2029 TYPE_NAME_STRING (BINFO_TYPE (elem)),
2030 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
2031 debug_tree (BINFO_TYPE (elem));
2032 if (BINFO_VTABLE (elem))
2033 fprintf (stderr, "vtable decl \"%s\"\n",
2034 IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem))));
2035 else
2036 fprintf (stderr, "no vtable decl yet\n");
2037 fprintf (stderr, "virtuals:\n");
2038 virtuals = BINFO_VIRTUALS (elem);
2039 n = 0;
2041 while (virtuals)
2043 tree fndecl = TREE_VALUE (virtuals);
2044 fprintf (stderr, "%s [%ld =? %ld]\n",
2045 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
2046 (long) n, (long) TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
2047 ++n;
2048 virtuals = TREE_CHAIN (virtuals);
2052 /* Build a representation for the qualified name SCOPE::NAME. TYPE is
2053 the type of the result expression, if known, or NULL_TREE if the
2054 resulting expression is type-dependent. If TEMPLATE_P is true,
2055 NAME is known to be a template because the user explicitly used the
2056 "template" keyword after the "::".
2058 All SCOPE_REFs should be built by use of this function. */
2060 tree
2061 build_qualified_name (tree type, tree scope, tree name, bool template_p)
2063 tree t;
2064 if (type == error_mark_node
2065 || scope == error_mark_node
2066 || name == error_mark_node)
2067 return error_mark_node;
2068 gcc_assert (TREE_CODE (name) != SCOPE_REF);
2069 t = build2 (SCOPE_REF, type, scope, name);
2070 QUALIFIED_NAME_IS_TEMPLATE (t) = template_p;
2071 PTRMEM_OK_P (t) = true;
2072 if (type)
2073 t = convert_from_reference (t);
2074 return t;
2077 /* Like check_qualified_type, but also check ref-qualifier and exception
2078 specification. */
2080 static bool
2081 cp_check_qualified_type (const_tree cand, const_tree base, int type_quals,
2082 cp_ref_qualifier rqual, tree raises)
2084 return (TYPE_QUALS (cand) == type_quals
2085 && check_base_type (cand, base)
2086 && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (cand),
2087 ce_exact)
2088 && type_memfn_rqual (cand) == rqual);
2091 /* Build the FUNCTION_TYPE or METHOD_TYPE with the ref-qualifier RQUAL. */
2093 tree
2094 build_ref_qualified_type (tree type, cp_ref_qualifier rqual)
2096 tree t;
2098 if (rqual == type_memfn_rqual (type))
2099 return type;
2101 int type_quals = TYPE_QUALS (type);
2102 tree raises = TYPE_RAISES_EXCEPTIONS (type);
2103 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2104 if (cp_check_qualified_type (t, type, type_quals, rqual, raises))
2105 return t;
2107 t = build_variant_type_copy (type);
2108 switch (rqual)
2110 case REF_QUAL_RVALUE:
2111 FUNCTION_RVALUE_QUALIFIED (t) = 1;
2112 FUNCTION_REF_QUALIFIED (t) = 1;
2113 break;
2114 case REF_QUAL_LVALUE:
2115 FUNCTION_RVALUE_QUALIFIED (t) = 0;
2116 FUNCTION_REF_QUALIFIED (t) = 1;
2117 break;
2118 default:
2119 FUNCTION_REF_QUALIFIED (t) = 0;
2120 break;
2123 if (TYPE_STRUCTURAL_EQUALITY_P (type))
2124 /* Propagate structural equality. */
2125 SET_TYPE_STRUCTURAL_EQUALITY (t);
2126 else if (TYPE_CANONICAL (type) != type)
2127 /* Build the underlying canonical type, since it is different
2128 from TYPE. */
2129 TYPE_CANONICAL (t) = build_ref_qualified_type (TYPE_CANONICAL (type),
2130 rqual);
2131 else
2132 /* T is its own canonical type. */
2133 TYPE_CANONICAL (t) = t;
2135 return t;
2138 /* Cache of free ovl nodes. Uses OVL_FUNCTION for chaining. */
2139 static GTY((deletable)) tree ovl_cache;
2141 /* Make a raw overload node containing FN. */
2143 tree
2144 ovl_make (tree fn, tree next)
2146 tree result = ovl_cache;
2148 if (result)
2150 ovl_cache = OVL_FUNCTION (result);
2151 /* Zap the flags. */
2152 memset (result, 0, sizeof (tree_base));
2153 TREE_SET_CODE (result, OVERLOAD);
2155 else
2156 result = make_node (OVERLOAD);
2158 if (TREE_CODE (fn) == OVERLOAD)
2159 OVL_NESTED_P (result) = true;
2161 TREE_TYPE (result) = (next || TREE_CODE (fn) == TEMPLATE_DECL
2162 ? unknown_type_node : TREE_TYPE (fn));
2163 OVL_FUNCTION (result) = fn;
2164 OVL_CHAIN (result) = next;
2165 return result;
2168 static tree
2169 ovl_copy (tree ovl)
2171 tree result = ovl_cache;
2173 if (result)
2175 ovl_cache = OVL_FUNCTION (result);
2176 /* Zap the flags. */
2177 memset (result, 0, sizeof (tree_base));
2178 TREE_SET_CODE (result, OVERLOAD);
2180 else
2181 result = make_node (OVERLOAD);
2183 gcc_checking_assert (!OVL_NESTED_P (ovl) && OVL_USED_P (ovl));
2184 TREE_TYPE (result) = TREE_TYPE (ovl);
2185 OVL_FUNCTION (result) = OVL_FUNCTION (ovl);
2186 OVL_CHAIN (result) = OVL_CHAIN (ovl);
2187 OVL_HIDDEN_P (result) = OVL_HIDDEN_P (ovl);
2188 OVL_USING_P (result) = OVL_USING_P (ovl);
2189 OVL_LOOKUP_P (result) = OVL_LOOKUP_P (ovl);
2191 return result;
2194 /* Add FN to the (potentially NULL) overload set OVL. USING_P is
2195 true, if FN is via a using declaration. We also pay attention to
2196 DECL_HIDDEN. Overloads are ordered as hidden, using, regular. */
2198 tree
2199 ovl_insert (tree fn, tree maybe_ovl, bool using_p)
2201 bool copying = false; /* Checking use only. */
2202 bool hidden_p = DECL_HIDDEN_P (fn);
2203 int weight = (hidden_p << 1) | (using_p << 0);
2205 tree result = NULL_TREE;
2206 tree insert_after = NULL_TREE;
2208 /* Find insertion point. */
2209 while (maybe_ovl && TREE_CODE (maybe_ovl) == OVERLOAD
2210 && (weight < ((OVL_HIDDEN_P (maybe_ovl) << 1)
2211 | (OVL_USING_P (maybe_ovl) << 0))))
2213 gcc_checking_assert (!OVL_LOOKUP_P (maybe_ovl)
2214 && (!copying || OVL_USED_P (maybe_ovl)));
2215 if (OVL_USED_P (maybe_ovl))
2217 copying = true;
2218 maybe_ovl = ovl_copy (maybe_ovl);
2219 if (insert_after)
2220 OVL_CHAIN (insert_after) = maybe_ovl;
2222 if (!result)
2223 result = maybe_ovl;
2224 insert_after = maybe_ovl;
2225 maybe_ovl = OVL_CHAIN (maybe_ovl);
2228 tree trail = fn;
2229 if (maybe_ovl || using_p || hidden_p || TREE_CODE (fn) == TEMPLATE_DECL)
2231 trail = ovl_make (fn, maybe_ovl);
2232 if (hidden_p)
2233 OVL_HIDDEN_P (trail) = true;
2234 if (using_p)
2235 OVL_USING_P (trail) = true;
2238 if (insert_after)
2240 OVL_CHAIN (insert_after) = trail;
2241 TREE_TYPE (insert_after) = unknown_type_node;
2243 else
2244 result = trail;
2246 return result;
2249 /* Skip any hidden names at the beginning of OVL. */
2251 tree
2252 ovl_skip_hidden (tree ovl)
2254 for (;
2255 ovl && TREE_CODE (ovl) == OVERLOAD && OVL_HIDDEN_P (ovl);
2256 ovl = OVL_CHAIN (ovl))
2257 gcc_checking_assert (DECL_HIDDEN_P (OVL_FUNCTION (ovl)));
2259 if (ovl && TREE_CODE (ovl) != OVERLOAD && DECL_HIDDEN_P (ovl))
2261 /* Any hidden functions should have been wrapped in an
2262 overload, but injected friend classes will not. */
2263 gcc_checking_assert (!DECL_DECLARES_FUNCTION_P (ovl));
2264 ovl = NULL_TREE;
2267 return ovl;
2270 /* NODE is an OVL_HIDDEN_P node which is now revealed. */
2272 tree
2273 ovl_iterator::reveal_node (tree overload, tree node)
2275 /* We cannot have returned NODE as part of a lookup overload, so it
2276 cannot be USED. */
2277 gcc_checking_assert (!OVL_USED_P (node));
2279 OVL_HIDDEN_P (node) = false;
2280 if (tree chain = OVL_CHAIN (node))
2281 if (TREE_CODE (chain) == OVERLOAD
2282 && (OVL_USING_P (chain) || OVL_HIDDEN_P (chain)))
2284 /* The node needs moving, and the simplest way is to remove it
2285 and reinsert. */
2286 overload = remove_node (overload, node);
2287 overload = ovl_insert (OVL_FUNCTION (node), overload);
2289 return overload;
2292 /* NODE is on the overloads of OVL. Remove it. If a predecessor is
2293 OVL_USED_P we must copy OVL nodes, because those are immutable.
2294 The removed node is unaltered and may continue to be iterated
2295 from (i.e. it is safe to remove a node from an overload one is
2296 currently iterating over). */
2298 tree
2299 ovl_iterator::remove_node (tree overload, tree node)
2301 bool copying = false; /* Checking use only. */
2303 tree *slot = &overload;
2304 while (*slot != node)
2306 tree probe = *slot;
2307 gcc_checking_assert (!OVL_LOOKUP_P (probe)
2308 && (!copying || OVL_USED_P (probe)));
2309 if (OVL_USED_P (probe))
2311 copying = true;
2312 probe = ovl_copy (probe);
2313 *slot = probe;
2316 slot = &OVL_CHAIN (probe);
2319 /* Stitch out NODE. We don't have to worry about now making a
2320 singleton overload (and consequently maybe setting its type),
2321 because all uses of this function will be followed by inserting a
2322 new node that must follow the place we've cut this out from. */
2323 if (TREE_CODE (node) != OVERLOAD)
2324 /* Cloned inherited ctors don't mark themselves as via_using. */
2325 *slot = NULL_TREE;
2326 else
2327 *slot = OVL_CHAIN (node);
2329 return overload;
2332 /* Mark or unmark a lookup set. */
2334 void
2335 lookup_mark (tree ovl, bool val)
2337 for (lkp_iterator iter (ovl); iter; ++iter)
2339 gcc_checking_assert (LOOKUP_SEEN_P (*iter) != val);
2340 LOOKUP_SEEN_P (*iter) = val;
2344 /* Add a set of new FNS into a lookup. */
2346 tree
2347 lookup_add (tree fns, tree lookup)
2349 if (lookup || TREE_CODE (fns) == TEMPLATE_DECL)
2351 lookup = ovl_make (fns, lookup);
2352 OVL_LOOKUP_P (lookup) = true;
2354 else
2355 lookup = fns;
2357 return lookup;
2360 /* FNS is a new overload set, add them to LOOKUP, if they are not
2361 already present there. */
2363 tree
2364 lookup_maybe_add (tree fns, tree lookup, bool deduping)
2366 if (deduping)
2367 for (tree next, probe = fns; probe; probe = next)
2369 tree fn = probe;
2370 next = NULL_TREE;
2372 if (TREE_CODE (probe) == OVERLOAD)
2374 fn = OVL_FUNCTION (probe);
2375 next = OVL_CHAIN (probe);
2378 if (!LOOKUP_SEEN_P (fn))
2379 LOOKUP_SEEN_P (fn) = true;
2380 else
2382 /* This function was already seen. Insert all the
2383 predecessors onto the lookup. */
2384 for (; fns != probe; fns = OVL_CHAIN (fns))
2386 lookup = lookup_add (OVL_FUNCTION (fns), lookup);
2387 /* Propagate OVL_USING, but OVL_HIDDEN doesn't matter. */
2388 if (OVL_USING_P (fns))
2389 OVL_USING_P (lookup) = true;
2392 /* And now skip this function. */
2393 fns = next;
2397 if (fns)
2398 /* We ended in a set of new functions. Add them all in one go. */
2399 lookup = lookup_add (fns, lookup);
2401 return lookup;
2404 /* Regular overload OVL is part of a kept lookup. Mark the nodes on
2405 it as immutable. */
2407 static void
2408 ovl_used (tree ovl)
2410 for (;
2411 ovl && TREE_CODE (ovl) == OVERLOAD
2412 && !OVL_USED_P (ovl);
2413 ovl = OVL_CHAIN (ovl))
2415 gcc_checking_assert (!OVL_LOOKUP_P (ovl));
2416 OVL_USED_P (ovl) = true;
2420 /* If KEEP is true, preserve the contents of a lookup so that it is
2421 available for a later instantiation. Otherwise release the LOOKUP
2422 nodes for reuse. */
2424 void
2425 lookup_keep (tree lookup, bool keep)
2427 for (;
2428 lookup && TREE_CODE (lookup) == OVERLOAD
2429 && OVL_LOOKUP_P (lookup) && !OVL_USED_P (lookup);
2430 lookup = OVL_CHAIN (lookup))
2431 if (keep)
2433 OVL_USED_P (lookup) = true;
2434 ovl_used (OVL_FUNCTION (lookup));
2436 else
2438 OVL_FUNCTION (lookup) = ovl_cache;
2439 ovl_cache = lookup;
2442 if (keep)
2443 ovl_used (lookup);
2446 /* LIST is a TREE_LIST whose TREE_VALUEs may be OVERLOADS that need
2447 keeping, or may be ignored. */
2449 void
2450 lookup_list_keep (tree list, bool keep)
2452 for (; list; list = TREE_CHAIN (list))
2454 tree v = TREE_VALUE (list);
2455 if (TREE_CODE (v) == OVERLOAD)
2456 lookup_keep (v, keep);
2460 /* Returns nonzero if X is an expression for a (possibly overloaded)
2461 function. If "f" is a function or function template, "f", "c->f",
2462 "c.f", "C::f", and "f<int>" will all be considered possibly
2463 overloaded functions. Returns 2 if the function is actually
2464 overloaded, i.e., if it is impossible to know the type of the
2465 function without performing overload resolution. */
2468 is_overloaded_fn (tree x)
2470 /* A baselink is also considered an overloaded function. */
2471 if (TREE_CODE (x) == OFFSET_REF
2472 || TREE_CODE (x) == COMPONENT_REF)
2473 x = TREE_OPERAND (x, 1);
2474 x = MAYBE_BASELINK_FUNCTIONS (x);
2475 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
2476 x = TREE_OPERAND (x, 0);
2478 if (DECL_FUNCTION_TEMPLATE_P (OVL_FIRST (x))
2479 || (TREE_CODE (x) == OVERLOAD && !OVL_SINGLE_P (x)))
2480 return 2;
2482 return (TREE_CODE (x) == FUNCTION_DECL
2483 || TREE_CODE (x) == OVERLOAD);
2486 /* X is the CALL_EXPR_FN of a CALL_EXPR. If X represents a dependent name
2487 (14.6.2), return the IDENTIFIER_NODE for that name. Otherwise, return
2488 NULL_TREE. */
2490 tree
2491 dependent_name (tree x)
2493 if (identifier_p (x))
2494 return x;
2495 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
2496 x = TREE_OPERAND (x, 0);
2497 if (TREE_CODE (x) == OVERLOAD || TREE_CODE (x) == FUNCTION_DECL)
2498 return OVL_NAME (x);
2499 return NULL_TREE;
2502 /* Returns true iff X is an expression for an overloaded function
2503 whose type cannot be known without performing overload
2504 resolution. */
2506 bool
2507 really_overloaded_fn (tree x)
2509 return is_overloaded_fn (x) == 2;
2512 /* Get the overload set FROM refers to. */
2514 tree
2515 get_fns (tree from)
2517 /* A baselink is also considered an overloaded function. */
2518 if (TREE_CODE (from) == OFFSET_REF
2519 || TREE_CODE (from) == COMPONENT_REF)
2520 from = TREE_OPERAND (from, 1);
2521 if (BASELINK_P (from))
2522 from = BASELINK_FUNCTIONS (from);
2523 if (TREE_CODE (from) == TEMPLATE_ID_EXPR)
2524 from = TREE_OPERAND (from, 0);
2525 gcc_assert (TREE_CODE (from) == OVERLOAD
2526 || TREE_CODE (from) == FUNCTION_DECL);
2527 return from;
2530 /* Return the first function of the overload set FROM refers to. */
2532 tree
2533 get_first_fn (tree from)
2535 return OVL_FIRST (get_fns (from));
2538 /* Return the scope where the overloaded functions OVL were found. */
2540 tree
2541 ovl_scope (tree ovl)
2543 if (TREE_CODE (ovl) == OFFSET_REF
2544 || TREE_CODE (ovl) == COMPONENT_REF)
2545 ovl = TREE_OPERAND (ovl, 1);
2546 if (TREE_CODE (ovl) == BASELINK)
2547 return BINFO_TYPE (BASELINK_BINFO (ovl));
2548 if (TREE_CODE (ovl) == TEMPLATE_ID_EXPR)
2549 ovl = TREE_OPERAND (ovl, 0);
2550 /* Skip using-declarations. */
2551 lkp_iterator iter (ovl);
2553 ovl = *iter;
2554 while (iter.using_p () && ++iter);
2556 return CP_DECL_CONTEXT (ovl);
2559 #define PRINT_RING_SIZE 4
2561 static const char *
2562 cxx_printable_name_internal (tree decl, int v, bool translate)
2564 static unsigned int uid_ring[PRINT_RING_SIZE];
2565 static char *print_ring[PRINT_RING_SIZE];
2566 static bool trans_ring[PRINT_RING_SIZE];
2567 static int ring_counter;
2568 int i;
2570 /* Only cache functions. */
2571 if (v < 2
2572 || TREE_CODE (decl) != FUNCTION_DECL
2573 || DECL_LANG_SPECIFIC (decl) == 0)
2574 return lang_decl_name (decl, v, translate);
2576 /* See if this print name is lying around. */
2577 for (i = 0; i < PRINT_RING_SIZE; i++)
2578 if (uid_ring[i] == DECL_UID (decl) && translate == trans_ring[i])
2579 /* yes, so return it. */
2580 return print_ring[i];
2582 if (++ring_counter == PRINT_RING_SIZE)
2583 ring_counter = 0;
2585 if (current_function_decl != NULL_TREE)
2587 /* There may be both translated and untranslated versions of the
2588 name cached. */
2589 for (i = 0; i < 2; i++)
2591 if (uid_ring[ring_counter] == DECL_UID (current_function_decl))
2592 ring_counter += 1;
2593 if (ring_counter == PRINT_RING_SIZE)
2594 ring_counter = 0;
2596 gcc_assert (uid_ring[ring_counter] != DECL_UID (current_function_decl));
2599 free (print_ring[ring_counter]);
2601 print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v, translate));
2602 uid_ring[ring_counter] = DECL_UID (decl);
2603 trans_ring[ring_counter] = translate;
2604 return print_ring[ring_counter];
2607 const char *
2608 cxx_printable_name (tree decl, int v)
2610 return cxx_printable_name_internal (decl, v, false);
2613 const char *
2614 cxx_printable_name_translate (tree decl, int v)
2616 return cxx_printable_name_internal (decl, v, true);
2619 /* Return the canonical version of exception-specification RAISES for a C++17
2620 function type, for use in type comparison and building TYPE_CANONICAL. */
2622 tree
2623 canonical_eh_spec (tree raises)
2625 if (raises == NULL_TREE)
2626 return raises;
2627 else if (DEFERRED_NOEXCEPT_SPEC_P (raises)
2628 || uses_template_parms (raises)
2629 || uses_template_parms (TREE_PURPOSE (raises)))
2630 /* Keep a dependent or deferred exception specification. */
2631 return raises;
2632 else if (nothrow_spec_p (raises))
2633 /* throw() -> noexcept. */
2634 return noexcept_true_spec;
2635 else
2636 /* For C++17 type matching, anything else -> nothing. */
2637 return NULL_TREE;
2640 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
2641 listed in RAISES. */
2643 tree
2644 build_exception_variant (tree type, tree raises)
2646 tree v;
2647 int type_quals;
2649 if (comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (type), ce_exact))
2650 return type;
2652 type_quals = TYPE_QUALS (type);
2653 cp_ref_qualifier rqual = type_memfn_rqual (type);
2654 for (v = TYPE_MAIN_VARIANT (type); v; v = TYPE_NEXT_VARIANT (v))
2655 if (cp_check_qualified_type (v, type, type_quals, rqual, raises))
2656 return v;
2658 /* Need to build a new variant. */
2659 v = build_variant_type_copy (type);
2660 TYPE_RAISES_EXCEPTIONS (v) = raises;
2662 if (!flag_noexcept_type)
2663 /* The exception-specification is not part of the canonical type. */
2664 return v;
2666 /* Canonicalize the exception specification. */
2667 tree cr = canonical_eh_spec (raises);
2669 if (TYPE_STRUCTURAL_EQUALITY_P (type))
2670 /* Propagate structural equality. */
2671 SET_TYPE_STRUCTURAL_EQUALITY (v);
2672 else if (TYPE_CANONICAL (type) != type || cr != raises)
2673 /* Build the underlying canonical type, since it is different
2674 from TYPE. */
2675 TYPE_CANONICAL (v) = build_exception_variant (TYPE_CANONICAL (type), cr);
2676 else
2677 /* T is its own canonical type. */
2678 TYPE_CANONICAL (v) = v;
2680 return v;
2683 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new
2684 BOUND_TEMPLATE_TEMPLATE_PARM bound with NEWARGS as its template
2685 arguments. */
2687 tree
2688 bind_template_template_parm (tree t, tree newargs)
2690 tree decl = TYPE_NAME (t);
2691 tree t2;
2693 t2 = cxx_make_type (BOUND_TEMPLATE_TEMPLATE_PARM);
2694 decl = build_decl (input_location,
2695 TYPE_DECL, DECL_NAME (decl), NULL_TREE);
2697 /* These nodes have to be created to reflect new TYPE_DECL and template
2698 arguments. */
2699 TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t));
2700 TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl;
2701 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
2702 = build_template_info (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t), newargs);
2704 TREE_TYPE (decl) = t2;
2705 TYPE_NAME (t2) = decl;
2706 TYPE_STUB_DECL (t2) = decl;
2707 TYPE_SIZE (t2) = 0;
2708 SET_TYPE_STRUCTURAL_EQUALITY (t2);
2710 return t2;
2713 /* Called from count_trees via walk_tree. */
2715 static tree
2716 count_trees_r (tree *tp, int *walk_subtrees, void *data)
2718 ++*((int *) data);
2720 if (TYPE_P (*tp))
2721 *walk_subtrees = 0;
2723 return NULL_TREE;
2726 /* Debugging function for measuring the rough complexity of a tree
2727 representation. */
2730 count_trees (tree t)
2732 int n_trees = 0;
2733 cp_walk_tree_without_duplicates (&t, count_trees_r, &n_trees);
2734 return n_trees;
2737 /* Called from verify_stmt_tree via walk_tree. */
2739 static tree
2740 verify_stmt_tree_r (tree* tp, int * /*walk_subtrees*/, void* data)
2742 tree t = *tp;
2743 hash_table<nofree_ptr_hash <tree_node> > *statements
2744 = static_cast <hash_table<nofree_ptr_hash <tree_node> > *> (data);
2745 tree_node **slot;
2747 if (!STATEMENT_CODE_P (TREE_CODE (t)))
2748 return NULL_TREE;
2750 /* If this statement is already present in the hash table, then
2751 there is a circularity in the statement tree. */
2752 gcc_assert (!statements->find (t));
2754 slot = statements->find_slot (t, INSERT);
2755 *slot = t;
2757 return NULL_TREE;
2760 /* Debugging function to check that the statement T has not been
2761 corrupted. For now, this function simply checks that T contains no
2762 circularities. */
2764 void
2765 verify_stmt_tree (tree t)
2767 hash_table<nofree_ptr_hash <tree_node> > statements (37);
2768 cp_walk_tree (&t, verify_stmt_tree_r, &statements, NULL);
2771 /* Check if the type T depends on a type with no linkage and if so, return
2772 it. If RELAXED_P then do not consider a class type declared within
2773 a vague-linkage function to have no linkage. */
2775 tree
2776 no_linkage_check (tree t, bool relaxed_p)
2778 tree r;
2780 /* There's no point in checking linkage on template functions; we
2781 can't know their complete types. */
2782 if (processing_template_decl)
2783 return NULL_TREE;
2785 switch (TREE_CODE (t))
2787 case RECORD_TYPE:
2788 if (TYPE_PTRMEMFUNC_P (t))
2789 goto ptrmem;
2790 /* Lambda types that don't have mangling scope have no linkage. We
2791 check CLASSTYPE_LAMBDA_EXPR for error_mark_node because
2792 when we get here from pushtag none of the lambda information is
2793 set up yet, so we want to assume that the lambda has linkage and
2794 fix it up later if not. */
2795 if (CLASSTYPE_LAMBDA_EXPR (t)
2796 && CLASSTYPE_LAMBDA_EXPR (t) != error_mark_node
2797 && LAMBDA_TYPE_EXTRA_SCOPE (t) == NULL_TREE)
2798 return t;
2799 /* Fall through. */
2800 case UNION_TYPE:
2801 if (!CLASS_TYPE_P (t))
2802 return NULL_TREE;
2803 /* Fall through. */
2804 case ENUMERAL_TYPE:
2805 /* Only treat unnamed types as having no linkage if they're at
2806 namespace scope. This is core issue 966. */
2807 if (TYPE_UNNAMED_P (t) && TYPE_NAMESPACE_SCOPE_P (t))
2808 return t;
2810 for (r = CP_TYPE_CONTEXT (t); ; )
2812 /* If we're a nested type of a !TREE_PUBLIC class, we might not
2813 have linkage, or we might just be in an anonymous namespace.
2814 If we're in a TREE_PUBLIC class, we have linkage. */
2815 if (TYPE_P (r) && !TREE_PUBLIC (TYPE_NAME (r)))
2816 return no_linkage_check (TYPE_CONTEXT (t), relaxed_p);
2817 else if (TREE_CODE (r) == FUNCTION_DECL)
2819 if (!relaxed_p || !vague_linkage_p (r))
2820 return t;
2821 else
2822 r = CP_DECL_CONTEXT (r);
2824 else
2825 break;
2828 return NULL_TREE;
2830 case ARRAY_TYPE:
2831 case POINTER_TYPE:
2832 case REFERENCE_TYPE:
2833 case VECTOR_TYPE:
2834 return no_linkage_check (TREE_TYPE (t), relaxed_p);
2836 case OFFSET_TYPE:
2837 ptrmem:
2838 r = no_linkage_check (TYPE_PTRMEM_POINTED_TO_TYPE (t),
2839 relaxed_p);
2840 if (r)
2841 return r;
2842 return no_linkage_check (TYPE_PTRMEM_CLASS_TYPE (t), relaxed_p);
2844 case METHOD_TYPE:
2845 case FUNCTION_TYPE:
2847 tree parm = TYPE_ARG_TYPES (t);
2848 if (TREE_CODE (t) == METHOD_TYPE)
2849 /* The 'this' pointer isn't interesting; a method has the same
2850 linkage (or lack thereof) as its enclosing class. */
2851 parm = TREE_CHAIN (parm);
2852 for (;
2853 parm && parm != void_list_node;
2854 parm = TREE_CHAIN (parm))
2856 r = no_linkage_check (TREE_VALUE (parm), relaxed_p);
2857 if (r)
2858 return r;
2860 return no_linkage_check (TREE_TYPE (t), relaxed_p);
2863 default:
2864 return NULL_TREE;
2868 extern int depth_reached;
2870 void
2871 cxx_print_statistics (void)
2873 print_template_statistics ();
2874 if (GATHER_STATISTICS)
2875 fprintf (stderr, "maximum template instantiation depth reached: %d\n",
2876 depth_reached);
2879 /* Return, as an INTEGER_CST node, the number of elements for TYPE
2880 (which is an ARRAY_TYPE). This counts only elements of the top
2881 array. */
2883 tree
2884 array_type_nelts_top (tree type)
2886 return fold_build2_loc (input_location,
2887 PLUS_EXPR, sizetype,
2888 array_type_nelts (type),
2889 size_one_node);
2892 /* Return, as an INTEGER_CST node, the number of elements for TYPE
2893 (which is an ARRAY_TYPE). This one is a recursive count of all
2894 ARRAY_TYPEs that are clumped together. */
2896 tree
2897 array_type_nelts_total (tree type)
2899 tree sz = array_type_nelts_top (type);
2900 type = TREE_TYPE (type);
2901 while (TREE_CODE (type) == ARRAY_TYPE)
2903 tree n = array_type_nelts_top (type);
2904 sz = fold_build2_loc (input_location,
2905 MULT_EXPR, sizetype, sz, n);
2906 type = TREE_TYPE (type);
2908 return sz;
2911 /* Called from break_out_target_exprs via mapcar. */
2913 static tree
2914 bot_manip (tree* tp, int* walk_subtrees, void* data)
2916 splay_tree target_remap = ((splay_tree) data);
2917 tree t = *tp;
2919 if (!TYPE_P (t) && TREE_CONSTANT (t) && !TREE_SIDE_EFFECTS (t))
2921 /* There can't be any TARGET_EXPRs or their slot variables below this
2922 point. But we must make a copy, in case subsequent processing
2923 alters any part of it. For example, during gimplification a cast
2924 of the form (T) &X::f (where "f" is a member function) will lead
2925 to replacing the PTRMEM_CST for &X::f with a VAR_DECL. */
2926 *walk_subtrees = 0;
2927 *tp = unshare_expr (t);
2928 return NULL_TREE;
2930 if (TREE_CODE (t) == TARGET_EXPR)
2932 tree u;
2934 if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
2936 u = build_cplus_new (TREE_TYPE (t), TREE_OPERAND (t, 1),
2937 tf_warning_or_error);
2938 if (u == error_mark_node)
2939 return u;
2940 if (AGGR_INIT_ZERO_FIRST (TREE_OPERAND (t, 1)))
2941 AGGR_INIT_ZERO_FIRST (TREE_OPERAND (u, 1)) = true;
2943 else
2944 u = build_target_expr_with_type (TREE_OPERAND (t, 1), TREE_TYPE (t),
2945 tf_warning_or_error);
2947 TARGET_EXPR_IMPLICIT_P (u) = TARGET_EXPR_IMPLICIT_P (t);
2948 TARGET_EXPR_LIST_INIT_P (u) = TARGET_EXPR_LIST_INIT_P (t);
2949 TARGET_EXPR_DIRECT_INIT_P (u) = TARGET_EXPR_DIRECT_INIT_P (t);
2951 /* Map the old variable to the new one. */
2952 splay_tree_insert (target_remap,
2953 (splay_tree_key) TREE_OPERAND (t, 0),
2954 (splay_tree_value) TREE_OPERAND (u, 0));
2956 TREE_OPERAND (u, 1) = break_out_target_exprs (TREE_OPERAND (u, 1));
2957 if (TREE_OPERAND (u, 1) == error_mark_node)
2958 return error_mark_node;
2960 /* Replace the old expression with the new version. */
2961 *tp = u;
2962 /* We don't have to go below this point; the recursive call to
2963 break_out_target_exprs will have handled anything below this
2964 point. */
2965 *walk_subtrees = 0;
2966 return NULL_TREE;
2968 if (TREE_CODE (*tp) == SAVE_EXPR)
2970 t = *tp;
2971 splay_tree_node n = splay_tree_lookup (target_remap,
2972 (splay_tree_key) t);
2973 if (n)
2975 *tp = (tree)n->value;
2976 *walk_subtrees = 0;
2978 else
2980 copy_tree_r (tp, walk_subtrees, NULL);
2981 splay_tree_insert (target_remap,
2982 (splay_tree_key)t,
2983 (splay_tree_value)*tp);
2984 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
2985 splay_tree_insert (target_remap,
2986 (splay_tree_key)*tp,
2987 (splay_tree_value)*tp);
2989 return NULL_TREE;
2992 /* Make a copy of this node. */
2993 t = copy_tree_r (tp, walk_subtrees, NULL);
2994 if (TREE_CODE (*tp) == CALL_EXPR)
2996 set_flags_from_callee (*tp);
2998 /* builtin_LINE and builtin_FILE get the location where the default
2999 argument is expanded, not where the call was written. */
3000 tree callee = get_callee_fndecl (*tp);
3001 if (callee && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
3002 switch (DECL_FUNCTION_CODE (callee))
3004 case BUILT_IN_FILE:
3005 case BUILT_IN_LINE:
3006 SET_EXPR_LOCATION (*tp, input_location);
3007 default:
3008 break;
3011 return t;
3014 /* Replace all remapped VAR_DECLs in T with their new equivalents.
3015 DATA is really a splay-tree mapping old variables to new
3016 variables. */
3018 static tree
3019 bot_replace (tree* t, int* /*walk_subtrees*/, void* data)
3021 splay_tree target_remap = ((splay_tree) data);
3023 if (VAR_P (*t))
3025 splay_tree_node n = splay_tree_lookup (target_remap,
3026 (splay_tree_key) *t);
3027 if (n)
3028 *t = (tree) n->value;
3030 else if (TREE_CODE (*t) == PARM_DECL
3031 && DECL_NAME (*t) == this_identifier
3032 && !DECL_CONTEXT (*t))
3034 /* In an NSDMI we need to replace the 'this' parameter we used for
3035 parsing with the real one for this function. */
3036 *t = current_class_ptr;
3038 else if (TREE_CODE (*t) == CONVERT_EXPR
3039 && CONVERT_EXPR_VBASE_PATH (*t))
3041 /* In an NSDMI build_base_path defers building conversions to virtual
3042 bases, and we handle it here. */
3043 tree basetype = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (*t)));
3044 vec<tree, va_gc> *vbases = CLASSTYPE_VBASECLASSES (current_class_type);
3045 int i; tree binfo;
3046 FOR_EACH_VEC_SAFE_ELT (vbases, i, binfo)
3047 if (BINFO_TYPE (binfo) == basetype)
3048 break;
3049 *t = build_base_path (PLUS_EXPR, TREE_OPERAND (*t, 0), binfo, true,
3050 tf_warning_or_error);
3053 return NULL_TREE;
3056 /* When we parse a default argument expression, we may create
3057 temporary variables via TARGET_EXPRs. When we actually use the
3058 default-argument expression, we make a copy of the expression
3059 and replace the temporaries with appropriate local versions. */
3061 tree
3062 break_out_target_exprs (tree t)
3064 static int target_remap_count;
3065 static splay_tree target_remap;
3067 if (!target_remap_count++)
3068 target_remap = splay_tree_new (splay_tree_compare_pointers,
3069 /*splay_tree_delete_key_fn=*/NULL,
3070 /*splay_tree_delete_value_fn=*/NULL);
3071 if (cp_walk_tree (&t, bot_manip, target_remap, NULL) == error_mark_node)
3072 t = error_mark_node;
3073 cp_walk_tree (&t, bot_replace, target_remap, NULL);
3075 if (!--target_remap_count)
3077 splay_tree_delete (target_remap);
3078 target_remap = NULL;
3081 return t;
3084 /* Build an expression for the subobject of OBJ at CONSTRUCTOR index INDEX,
3085 which we expect to have type TYPE. */
3087 tree
3088 build_ctor_subob_ref (tree index, tree type, tree obj)
3090 if (index == NULL_TREE)
3091 /* Can't refer to a particular member of a vector. */
3092 obj = NULL_TREE;
3093 else if (TREE_CODE (index) == INTEGER_CST)
3094 obj = cp_build_array_ref (input_location, obj, index, tf_none);
3095 else
3096 obj = build_class_member_access_expr (obj, index, NULL_TREE,
3097 /*reference*/false, tf_none);
3098 if (obj)
3100 tree objtype = TREE_TYPE (obj);
3101 if (TREE_CODE (objtype) == ARRAY_TYPE && !TYPE_DOMAIN (objtype))
3103 /* When the destination object refers to a flexible array member
3104 verify that it matches the type of the source object except
3105 for its domain and qualifiers. */
3106 gcc_assert (comptypes (TYPE_MAIN_VARIANT (type),
3107 TYPE_MAIN_VARIANT (objtype),
3108 COMPARE_REDECLARATION));
3110 else
3111 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, objtype));
3114 return obj;
3117 struct replace_placeholders_t
3119 tree obj; /* The object to be substituted for a PLACEHOLDER_EXPR. */
3120 tree exp; /* The outermost exp. */
3121 bool seen; /* Whether we've encountered a PLACEHOLDER_EXPR. */
3122 hash_set<tree> *pset; /* To avoid walking same trees multiple times. */
3125 /* Like substitute_placeholder_in_expr, but handle C++ tree codes and
3126 build up subexpressions as we go deeper. */
3128 static tree
3129 replace_placeholders_r (tree* t, int* walk_subtrees, void* data_)
3131 replace_placeholders_t *d = static_cast<replace_placeholders_t*>(data_);
3132 tree obj = d->obj;
3134 if (TYPE_P (*t) || TREE_CONSTANT (*t))
3136 *walk_subtrees = false;
3137 return NULL_TREE;
3140 switch (TREE_CODE (*t))
3142 case PLACEHOLDER_EXPR:
3144 tree x = obj;
3145 for (; !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (*t),
3146 TREE_TYPE (x));
3147 x = TREE_OPERAND (x, 0))
3148 gcc_assert (TREE_CODE (x) == COMPONENT_REF);
3149 *t = unshare_expr (x);
3150 *walk_subtrees = false;
3151 d->seen = true;
3153 break;
3155 case CONSTRUCTOR:
3157 constructor_elt *ce;
3158 vec<constructor_elt,va_gc> *v = CONSTRUCTOR_ELTS (*t);
3159 /* Don't walk into CONSTRUCTOR_PLACEHOLDER_BOUNDARY ctors
3160 other than the d->exp one, those have PLACEHOLDER_EXPRs
3161 related to another object. */
3162 if ((CONSTRUCTOR_PLACEHOLDER_BOUNDARY (*t)
3163 && *t != d->exp)
3164 || d->pset->add (*t))
3166 *walk_subtrees = false;
3167 return NULL_TREE;
3169 for (unsigned i = 0; vec_safe_iterate (v, i, &ce); ++i)
3171 tree *valp = &ce->value;
3172 tree type = TREE_TYPE (*valp);
3173 tree subob = obj;
3175 if (TREE_CODE (*valp) == CONSTRUCTOR
3176 && AGGREGATE_TYPE_P (type))
3178 /* If we're looking at the initializer for OBJ, then build
3179 a sub-object reference. If we're looking at an
3180 initializer for another object, just pass OBJ down. */
3181 if (same_type_ignoring_top_level_qualifiers_p
3182 (TREE_TYPE (*t), TREE_TYPE (obj)))
3183 subob = build_ctor_subob_ref (ce->index, type, obj);
3184 if (TREE_CODE (*valp) == TARGET_EXPR)
3185 valp = &TARGET_EXPR_INITIAL (*valp);
3187 d->obj = subob;
3188 cp_walk_tree (valp, replace_placeholders_r, data_, NULL);
3189 d->obj = obj;
3191 *walk_subtrees = false;
3192 break;
3195 default:
3196 if (d->pset->add (*t))
3197 *walk_subtrees = false;
3198 break;
3201 return NULL_TREE;
3204 /* Replace PLACEHOLDER_EXPRs in EXP with object OBJ. SEEN_P is set if
3205 a PLACEHOLDER_EXPR has been encountered. */
3207 tree
3208 replace_placeholders (tree exp, tree obj, bool *seen_p)
3210 /* This is only relevant for C++14. */
3211 if (cxx_dialect < cxx14)
3212 return exp;
3214 /* If the object isn't a (member of a) class, do nothing. */
3215 tree op0 = obj;
3216 while (TREE_CODE (op0) == COMPONENT_REF)
3217 op0 = TREE_OPERAND (op0, 0);
3218 if (!CLASS_TYPE_P (strip_array_types (TREE_TYPE (op0))))
3219 return exp;
3221 tree *tp = &exp;
3222 if (TREE_CODE (exp) == TARGET_EXPR)
3223 tp = &TARGET_EXPR_INITIAL (exp);
3224 hash_set<tree> pset;
3225 replace_placeholders_t data = { obj, *tp, false, &pset };
3226 cp_walk_tree (tp, replace_placeholders_r, &data, NULL);
3227 if (seen_p)
3228 *seen_p = data.seen;
3229 return exp;
3232 /* Callback function for find_placeholders. */
3234 static tree
3235 find_placeholders_r (tree *t, int *walk_subtrees, void *)
3237 if (TYPE_P (*t) || TREE_CONSTANT (*t))
3239 *walk_subtrees = false;
3240 return NULL_TREE;
3243 switch (TREE_CODE (*t))
3245 case PLACEHOLDER_EXPR:
3246 return *t;
3248 case CONSTRUCTOR:
3249 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (*t))
3250 *walk_subtrees = false;
3251 break;
3253 default:
3254 break;
3257 return NULL_TREE;
3260 /* Return true if EXP contains a PLACEHOLDER_EXPR. Don't walk into
3261 ctors with CONSTRUCTOR_PLACEHOLDER_BOUNDARY flag set. */
3263 bool
3264 find_placeholders (tree exp)
3266 /* This is only relevant for C++14. */
3267 if (cxx_dialect < cxx14)
3268 return false;
3270 return cp_walk_tree_without_duplicates (&exp, find_placeholders_r, NULL);
3273 /* Similar to `build_nt', but for template definitions of dependent
3274 expressions */
3276 tree
3277 build_min_nt_loc (location_t loc, enum tree_code code, ...)
3279 tree t;
3280 int length;
3281 int i;
3282 va_list p;
3284 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3286 va_start (p, code);
3288 t = make_node (code);
3289 SET_EXPR_LOCATION (t, loc);
3290 length = TREE_CODE_LENGTH (code);
3292 for (i = 0; i < length; i++)
3294 tree x = va_arg (p, tree);
3295 TREE_OPERAND (t, i) = x;
3296 if (x && TREE_CODE (x) == OVERLOAD)
3297 lookup_keep (x, true);
3300 va_end (p);
3301 return t;
3304 /* Similar to `build', but for template definitions. */
3306 tree
3307 build_min (enum tree_code code, tree tt, ...)
3309 tree t;
3310 int length;
3311 int i;
3312 va_list p;
3314 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3316 va_start (p, tt);
3318 t = make_node (code);
3319 length = TREE_CODE_LENGTH (code);
3320 TREE_TYPE (t) = tt;
3322 for (i = 0; i < length; i++)
3324 tree x = va_arg (p, tree);
3325 TREE_OPERAND (t, i) = x;
3326 if (x)
3328 if (!TYPE_P (x) && TREE_SIDE_EFFECTS (x))
3329 TREE_SIDE_EFFECTS (t) = 1;
3330 if (TREE_CODE (x) == OVERLOAD)
3331 lookup_keep (x, true);
3335 va_end (p);
3337 if (code == CAST_EXPR)
3338 /* The single operand is a TREE_LIST, which we have to check. */
3339 lookup_list_keep (TREE_OPERAND (t, 0), true);
3341 return t;
3344 /* Similar to `build', but for template definitions of non-dependent
3345 expressions. NON_DEP is the non-dependent expression that has been
3346 built. */
3348 tree
3349 build_min_non_dep (enum tree_code code, tree non_dep, ...)
3351 tree t;
3352 int length;
3353 int i;
3354 va_list p;
3356 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3358 va_start (p, non_dep);
3360 if (REFERENCE_REF_P (non_dep))
3361 non_dep = TREE_OPERAND (non_dep, 0);
3363 t = make_node (code);
3364 length = TREE_CODE_LENGTH (code);
3365 TREE_TYPE (t) = unlowered_expr_type (non_dep);
3366 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
3368 for (i = 0; i < length; i++)
3370 tree x = va_arg (p, tree);
3371 TREE_OPERAND (t, i) = x;
3372 if (x && TREE_CODE (x) == OVERLOAD)
3373 lookup_keep (x, true);
3376 if (code == COMPOUND_EXPR && TREE_CODE (non_dep) != COMPOUND_EXPR)
3377 /* This should not be considered a COMPOUND_EXPR, because it
3378 resolves to an overload. */
3379 COMPOUND_EXPR_OVERLOADED (t) = 1;
3381 va_end (p);
3382 return convert_from_reference (t);
3385 /* Similar to build_min_nt, but call expressions */
3387 tree
3388 build_min_nt_call_vec (tree fn, vec<tree, va_gc> *args)
3390 tree ret, t;
3391 unsigned int ix;
3393 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
3394 CALL_EXPR_FN (ret) = fn;
3395 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
3396 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
3398 CALL_EXPR_ARG (ret, ix) = t;
3399 if (TREE_CODE (t) == OVERLOAD)
3400 lookup_keep (t, true);
3402 return ret;
3405 /* Similar to `build_min_nt_call_vec', but for template definitions of
3406 non-dependent expressions. NON_DEP is the non-dependent expression
3407 that has been built. */
3409 tree
3410 build_min_non_dep_call_vec (tree non_dep, tree fn, vec<tree, va_gc> *argvec)
3412 tree t = build_min_nt_call_vec (fn, argvec);
3413 if (REFERENCE_REF_P (non_dep))
3414 non_dep = TREE_OPERAND (non_dep, 0);
3415 TREE_TYPE (t) = TREE_TYPE (non_dep);
3416 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
3417 return convert_from_reference (t);
3420 /* Similar to build_min_non_dep, but for expressions that have been resolved to
3421 a call to an operator overload. OP is the operator that has been
3422 overloaded. NON_DEP is the non-dependent expression that's been built,
3423 which should be a CALL_EXPR or an INDIRECT_REF to a CALL_EXPR. OVERLOAD is
3424 the overload that NON_DEP is calling. */
3426 tree
3427 build_min_non_dep_op_overload (enum tree_code op,
3428 tree non_dep,
3429 tree overload, ...)
3431 va_list p;
3432 int nargs, expected_nargs;
3433 tree fn, call;
3434 vec<tree, va_gc> *args;
3436 non_dep = extract_call_expr (non_dep);
3438 nargs = call_expr_nargs (non_dep);
3440 expected_nargs = cp_tree_code_length (op);
3441 if ((op == POSTINCREMENT_EXPR
3442 || op == POSTDECREMENT_EXPR)
3443 /* With -fpermissive non_dep could be operator++(). */
3444 && (!flag_permissive || nargs != expected_nargs))
3445 expected_nargs += 1;
3446 gcc_assert (nargs == expected_nargs);
3448 args = make_tree_vector ();
3449 va_start (p, overload);
3451 if (TREE_CODE (TREE_TYPE (overload)) == FUNCTION_TYPE)
3453 fn = overload;
3454 for (int i = 0; i < nargs; i++)
3456 tree arg = va_arg (p, tree);
3457 vec_safe_push (args, arg);
3460 else if (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE)
3462 tree object = va_arg (p, tree);
3463 tree binfo = TYPE_BINFO (TREE_TYPE (object));
3464 tree method = build_baselink (binfo, binfo, overload, NULL_TREE);
3465 fn = build_min (COMPONENT_REF, TREE_TYPE (overload),
3466 object, method, NULL_TREE);
3467 for (int i = 1; i < nargs; i++)
3469 tree arg = va_arg (p, tree);
3470 vec_safe_push (args, arg);
3473 else
3474 gcc_unreachable ();
3476 va_end (p);
3477 call = build_min_non_dep_call_vec (non_dep, fn, args);
3478 release_tree_vector (args);
3480 tree call_expr = extract_call_expr (call);
3481 KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep);
3482 CALL_EXPR_OPERATOR_SYNTAX (call_expr) = true;
3483 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep);
3484 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep);
3486 return call;
3489 /* Return a new tree vec copied from VEC, with ELT inserted at index IDX. */
3491 vec<tree, va_gc> *
3492 vec_copy_and_insert (vec<tree, va_gc> *old_vec, tree elt, unsigned idx)
3494 unsigned len = vec_safe_length (old_vec);
3495 gcc_assert (idx <= len);
3497 vec<tree, va_gc> *new_vec = NULL;
3498 vec_alloc (new_vec, len + 1);
3500 unsigned i;
3501 for (i = 0; i < len; ++i)
3503 if (i == idx)
3504 new_vec->quick_push (elt);
3505 new_vec->quick_push ((*old_vec)[i]);
3507 if (i == idx)
3508 new_vec->quick_push (elt);
3510 return new_vec;
3513 tree
3514 get_type_decl (tree t)
3516 if (TREE_CODE (t) == TYPE_DECL)
3517 return t;
3518 if (TYPE_P (t))
3519 return TYPE_STUB_DECL (t);
3520 gcc_assert (t == error_mark_node);
3521 return t;
3524 /* Returns the namespace that contains DECL, whether directly or
3525 indirectly. */
3527 tree
3528 decl_namespace_context (tree decl)
3530 while (1)
3532 if (TREE_CODE (decl) == NAMESPACE_DECL)
3533 return decl;
3534 else if (TYPE_P (decl))
3535 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
3536 else
3537 decl = CP_DECL_CONTEXT (decl);
3541 /* Returns true if decl is within an anonymous namespace, however deeply
3542 nested, or false otherwise. */
3544 bool
3545 decl_anon_ns_mem_p (const_tree decl)
3547 while (TREE_CODE (decl) != NAMESPACE_DECL)
3549 /* Classes inside anonymous namespaces have TREE_PUBLIC == 0. */
3550 if (TYPE_P (decl))
3551 return !TREE_PUBLIC (TYPE_MAIN_DECL (decl));
3553 decl = CP_DECL_CONTEXT (decl);
3555 return !TREE_PUBLIC (decl);
3558 /* Subroutine of cp_tree_equal: t1 and t2 are the CALL_EXPR_FNs of two
3559 CALL_EXPRS. Return whether they are equivalent. */
3561 static bool
3562 called_fns_equal (tree t1, tree t2)
3564 /* Core 1321: dependent names are equivalent even if the overload sets
3565 are different. But do compare explicit template arguments. */
3566 tree name1 = dependent_name (t1);
3567 tree name2 = dependent_name (t2);
3568 if (name1 || name2)
3570 tree targs1 = NULL_TREE, targs2 = NULL_TREE;
3572 if (name1 != name2)
3573 return false;
3575 if (TREE_CODE (t1) == TEMPLATE_ID_EXPR)
3576 targs1 = TREE_OPERAND (t1, 1);
3577 if (TREE_CODE (t2) == TEMPLATE_ID_EXPR)
3578 targs2 = TREE_OPERAND (t2, 1);
3579 return cp_tree_equal (targs1, targs2);
3581 else
3582 return cp_tree_equal (t1, t2);
3585 /* Return truthvalue of whether T1 is the same tree structure as T2.
3586 Return 1 if they are the same. Return 0 if they are different. */
3588 bool
3589 cp_tree_equal (tree t1, tree t2)
3591 enum tree_code code1, code2;
3593 if (t1 == t2)
3594 return true;
3595 if (!t1 || !t2)
3596 return false;
3598 code1 = TREE_CODE (t1);
3599 code2 = TREE_CODE (t2);
3601 if (code1 != code2)
3602 return false;
3604 if (CONSTANT_CLASS_P (t1)
3605 && !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3606 return false;
3608 switch (code1)
3610 case VOID_CST:
3611 /* There's only a single VOID_CST node, so we should never reach
3612 here. */
3613 gcc_unreachable ();
3615 case INTEGER_CST:
3616 return tree_int_cst_equal (t1, t2);
3618 case REAL_CST:
3619 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
3621 case STRING_CST:
3622 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3623 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3624 TREE_STRING_LENGTH (t1));
3626 case FIXED_CST:
3627 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
3628 TREE_FIXED_CST (t2));
3630 case COMPLEX_CST:
3631 return cp_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
3632 && cp_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
3634 case VECTOR_CST:
3635 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
3637 case CONSTRUCTOR:
3638 /* We need to do this when determining whether or not two
3639 non-type pointer to member function template arguments
3640 are the same. */
3641 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
3642 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
3643 return false;
3645 tree field, value;
3646 unsigned int i;
3647 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
3649 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
3650 if (!cp_tree_equal (field, elt2->index)
3651 || !cp_tree_equal (value, elt2->value))
3652 return false;
3655 return true;
3657 case TREE_LIST:
3658 if (!cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
3659 return false;
3660 if (!cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
3661 return false;
3662 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
3664 case SAVE_EXPR:
3665 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3667 case CALL_EXPR:
3669 tree arg1, arg2;
3670 call_expr_arg_iterator iter1, iter2;
3671 if (!called_fns_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
3672 return false;
3673 for (arg1 = first_call_expr_arg (t1, &iter1),
3674 arg2 = first_call_expr_arg (t2, &iter2);
3675 arg1 && arg2;
3676 arg1 = next_call_expr_arg (&iter1),
3677 arg2 = next_call_expr_arg (&iter2))
3678 if (!cp_tree_equal (arg1, arg2))
3679 return false;
3680 if (arg1 || arg2)
3681 return false;
3682 return true;
3685 case TARGET_EXPR:
3687 tree o1 = TREE_OPERAND (t1, 0);
3688 tree o2 = TREE_OPERAND (t2, 0);
3690 /* Special case: if either target is an unallocated VAR_DECL,
3691 it means that it's going to be unified with whatever the
3692 TARGET_EXPR is really supposed to initialize, so treat it
3693 as being equivalent to anything. */
3694 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
3695 && !DECL_RTL_SET_P (o1))
3696 /*Nop*/;
3697 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
3698 && !DECL_RTL_SET_P (o2))
3699 /*Nop*/;
3700 else if (!cp_tree_equal (o1, o2))
3701 return false;
3703 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3706 case PARM_DECL:
3707 /* For comparing uses of parameters in late-specified return types
3708 with an out-of-class definition of the function, but can also come
3709 up for expressions that involve 'this' in a member function
3710 template. */
3712 if (comparing_specializations && !CONSTRAINT_VAR_P (t1))
3713 /* When comparing hash table entries, only an exact match is
3714 good enough; we don't want to replace 'this' with the
3715 version from another function. But be more flexible
3716 with local parameters in a requires-expression. */
3717 return false;
3719 if (same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3721 if (DECL_ARTIFICIAL (t1) ^ DECL_ARTIFICIAL (t2))
3722 return false;
3723 if (CONSTRAINT_VAR_P (t1) ^ CONSTRAINT_VAR_P (t2))
3724 return false;
3725 if (DECL_ARTIFICIAL (t1)
3726 || (DECL_PARM_LEVEL (t1) == DECL_PARM_LEVEL (t2)
3727 && DECL_PARM_INDEX (t1) == DECL_PARM_INDEX (t2)))
3728 return true;
3730 return false;
3732 case VAR_DECL:
3733 case CONST_DECL:
3734 case FIELD_DECL:
3735 case FUNCTION_DECL:
3736 case TEMPLATE_DECL:
3737 case IDENTIFIER_NODE:
3738 case SSA_NAME:
3739 return false;
3741 case BASELINK:
3742 return (BASELINK_BINFO (t1) == BASELINK_BINFO (t2)
3743 && BASELINK_ACCESS_BINFO (t1) == BASELINK_ACCESS_BINFO (t2)
3744 && BASELINK_QUALIFIED_P (t1) == BASELINK_QUALIFIED_P (t2)
3745 && cp_tree_equal (BASELINK_FUNCTIONS (t1),
3746 BASELINK_FUNCTIONS (t2)));
3748 case TEMPLATE_PARM_INDEX:
3749 return (TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
3750 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2)
3751 && (TEMPLATE_PARM_PARAMETER_PACK (t1)
3752 == TEMPLATE_PARM_PARAMETER_PACK (t2))
3753 && same_type_p (TREE_TYPE (TEMPLATE_PARM_DECL (t1)),
3754 TREE_TYPE (TEMPLATE_PARM_DECL (t2))));
3756 case TEMPLATE_ID_EXPR:
3757 return (cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3758 && cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3760 case CONSTRAINT_INFO:
3761 return cp_tree_equal (CI_ASSOCIATED_CONSTRAINTS (t1),
3762 CI_ASSOCIATED_CONSTRAINTS (t2));
3764 case CHECK_CONSTR:
3765 return (CHECK_CONSTR_CONCEPT (t1) == CHECK_CONSTR_CONCEPT (t2)
3766 && comp_template_args (CHECK_CONSTR_ARGS (t1),
3767 CHECK_CONSTR_ARGS (t2)));
3769 case TREE_VEC:
3771 unsigned ix;
3772 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3773 return false;
3774 for (ix = TREE_VEC_LENGTH (t1); ix--;)
3775 if (!cp_tree_equal (TREE_VEC_ELT (t1, ix),
3776 TREE_VEC_ELT (t2, ix)))
3777 return false;
3778 return true;
3781 case SIZEOF_EXPR:
3782 case ALIGNOF_EXPR:
3784 tree o1 = TREE_OPERAND (t1, 0);
3785 tree o2 = TREE_OPERAND (t2, 0);
3787 if (code1 == SIZEOF_EXPR)
3789 if (SIZEOF_EXPR_TYPE_P (t1))
3790 o1 = TREE_TYPE (o1);
3791 if (SIZEOF_EXPR_TYPE_P (t2))
3792 o2 = TREE_TYPE (o2);
3794 if (TREE_CODE (o1) != TREE_CODE (o2))
3795 return false;
3796 if (TYPE_P (o1))
3797 return same_type_p (o1, o2);
3798 else
3799 return cp_tree_equal (o1, o2);
3802 case MODOP_EXPR:
3804 tree t1_op1, t2_op1;
3806 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
3807 return false;
3809 t1_op1 = TREE_OPERAND (t1, 1);
3810 t2_op1 = TREE_OPERAND (t2, 1);
3811 if (TREE_CODE (t1_op1) != TREE_CODE (t2_op1))
3812 return false;
3814 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t2, 2));
3817 case PTRMEM_CST:
3818 /* Two pointer-to-members are the same if they point to the same
3819 field or function in the same class. */
3820 if (PTRMEM_CST_MEMBER (t1) != PTRMEM_CST_MEMBER (t2))
3821 return false;
3823 return same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2));
3825 case OVERLOAD:
3827 /* Two overloads. Must be exactly the same set of decls. */
3828 lkp_iterator first (t1);
3829 lkp_iterator second (t2);
3831 for (; first && second; ++first, ++second)
3832 if (*first != *second)
3833 return false;
3834 return !(first || second);
3837 case TRAIT_EXPR:
3838 if (TRAIT_EXPR_KIND (t1) != TRAIT_EXPR_KIND (t2))
3839 return false;
3840 return same_type_p (TRAIT_EXPR_TYPE1 (t1), TRAIT_EXPR_TYPE1 (t2))
3841 && cp_tree_equal (TRAIT_EXPR_TYPE2 (t1), TRAIT_EXPR_TYPE2 (t2));
3843 case CAST_EXPR:
3844 case STATIC_CAST_EXPR:
3845 case REINTERPRET_CAST_EXPR:
3846 case CONST_CAST_EXPR:
3847 case DYNAMIC_CAST_EXPR:
3848 case IMPLICIT_CONV_EXPR:
3849 case NEW_EXPR:
3850 CASE_CONVERT:
3851 case NON_LVALUE_EXPR:
3852 case VIEW_CONVERT_EXPR:
3853 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3854 return false;
3855 /* Now compare operands as usual. */
3856 break;
3858 case DEFERRED_NOEXCEPT:
3859 return (cp_tree_equal (DEFERRED_NOEXCEPT_PATTERN (t1),
3860 DEFERRED_NOEXCEPT_PATTERN (t2))
3861 && comp_template_args (DEFERRED_NOEXCEPT_ARGS (t1),
3862 DEFERRED_NOEXCEPT_ARGS (t2)));
3863 break;
3865 default:
3866 break;
3869 switch (TREE_CODE_CLASS (code1))
3871 case tcc_unary:
3872 case tcc_binary:
3873 case tcc_comparison:
3874 case tcc_expression:
3875 case tcc_vl_exp:
3876 case tcc_reference:
3877 case tcc_statement:
3879 int i, n;
3881 n = cp_tree_operand_length (t1);
3882 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
3883 && n != TREE_OPERAND_LENGTH (t2))
3884 return false;
3886 for (i = 0; i < n; ++i)
3887 if (!cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
3888 return false;
3890 return true;
3893 case tcc_type:
3894 return same_type_p (t1, t2);
3895 default:
3896 gcc_unreachable ();
3898 /* We can get here with --disable-checking. */
3899 return false;
3902 /* The type of ARG when used as an lvalue. */
3904 tree
3905 lvalue_type (tree arg)
3907 tree type = TREE_TYPE (arg);
3908 return type;
3911 /* The type of ARG for printing error messages; denote lvalues with
3912 reference types. */
3914 tree
3915 error_type (tree arg)
3917 tree type = TREE_TYPE (arg);
3919 if (TREE_CODE (type) == ARRAY_TYPE)
3921 else if (TREE_CODE (type) == ERROR_MARK)
3923 else if (lvalue_p (arg))
3924 type = build_reference_type (lvalue_type (arg));
3925 else if (MAYBE_CLASS_TYPE_P (type))
3926 type = lvalue_type (arg);
3928 return type;
3931 /* Does FUNCTION use a variable-length argument list? */
3934 varargs_function_p (const_tree function)
3936 return stdarg_p (TREE_TYPE (function));
3939 /* Returns 1 if decl is a member of a class. */
3942 member_p (const_tree decl)
3944 const_tree const ctx = DECL_CONTEXT (decl);
3945 return (ctx && TYPE_P (ctx));
3948 /* Create a placeholder for member access where we don't actually have an
3949 object that the access is against. */
3951 tree
3952 build_dummy_object (tree type)
3954 tree decl = build1 (CONVERT_EXPR, build_pointer_type (type), void_node);
3955 return cp_build_fold_indirect_ref (decl);
3958 /* We've gotten a reference to a member of TYPE. Return *this if appropriate,
3959 or a dummy object otherwise. If BINFOP is non-0, it is filled with the
3960 binfo path from current_class_type to TYPE, or 0. */
3962 tree
3963 maybe_dummy_object (tree type, tree* binfop)
3965 tree decl, context;
3966 tree binfo;
3967 tree current = current_nonlambda_class_type ();
3969 if (current
3970 && (binfo = lookup_base (current, type, ba_any, NULL,
3971 tf_warning_or_error)))
3972 context = current;
3973 else
3975 /* Reference from a nested class member function. */
3976 context = type;
3977 binfo = TYPE_BINFO (type);
3980 if (binfop)
3981 *binfop = binfo;
3983 if (current_class_ref
3984 /* current_class_ref might not correspond to current_class_type if
3985 we're in tsubst_default_argument or a lambda-declarator; in either
3986 case, we want to use current_class_ref if it matches CONTEXT. */
3987 && (same_type_ignoring_top_level_qualifiers_p
3988 (TREE_TYPE (current_class_ref), context)))
3989 decl = current_class_ref;
3990 else
3991 decl = build_dummy_object (context);
3993 return decl;
3996 /* Returns 1 if OB is a placeholder object, or a pointer to one. */
3999 is_dummy_object (const_tree ob)
4001 if (INDIRECT_REF_P (ob))
4002 ob = TREE_OPERAND (ob, 0);
4003 return (TREE_CODE (ob) == CONVERT_EXPR
4004 && TREE_OPERAND (ob, 0) == void_node);
4007 /* Returns 1 iff type T is something we want to treat as a scalar type for
4008 the purpose of deciding whether it is trivial/POD/standard-layout. */
4010 bool
4011 scalarish_type_p (const_tree t)
4013 if (t == error_mark_node)
4014 return 1;
4016 return (SCALAR_TYPE_P (t) || VECTOR_TYPE_P (t));
4019 /* Returns true iff T requires non-trivial default initialization. */
4021 bool
4022 type_has_nontrivial_default_init (const_tree t)
4024 t = strip_array_types (CONST_CAST_TREE (t));
4026 if (CLASS_TYPE_P (t))
4027 return TYPE_HAS_COMPLEX_DFLT (t);
4028 else
4029 return 0;
4032 /* Track classes with only deleted copy/move constructors so that we can warn
4033 if they are used in call/return by value. */
4035 static GTY(()) hash_set<tree>* deleted_copy_types;
4036 static void
4037 remember_deleted_copy (const_tree t)
4039 if (!deleted_copy_types)
4040 deleted_copy_types = hash_set<tree>::create_ggc(37);
4041 deleted_copy_types->add (CONST_CAST_TREE (t));
4043 void
4044 maybe_warn_parm_abi (tree t, location_t loc)
4046 if (!deleted_copy_types
4047 || !deleted_copy_types->contains (t))
4048 return;
4050 warning_at (loc, OPT_Wabi, "the calling convention for %qT changes in "
4051 "-fabi-version=12 (GCC 8)", t);
4052 static bool explained = false;
4053 if (!explained)
4055 inform (loc, " because all of its copy and move constructors "
4056 "are deleted");
4057 explained = true;
4061 /* Returns true iff copying an object of type T (including via move
4062 constructor) is non-trivial. That is, T has no non-trivial copy
4063 constructors and no non-trivial move constructors, and not all copy/move
4064 constructors are deleted. This function implements the ABI notion of
4065 non-trivial copy, which has diverged from the one in the standard. */
4067 bool
4068 type_has_nontrivial_copy_init (const_tree type)
4070 tree t = strip_array_types (CONST_CAST_TREE (type));
4072 if (CLASS_TYPE_P (t))
4074 gcc_assert (COMPLETE_TYPE_P (t));
4076 if (TYPE_HAS_COMPLEX_COPY_CTOR (t)
4077 || TYPE_HAS_COMPLEX_MOVE_CTOR (t))
4078 /* Nontrivial. */
4079 return true;
4081 if (cxx_dialect < cxx11)
4082 /* No deleted functions before C++11. */
4083 return false;
4085 /* Before ABI v12 we did a bitwise copy of types with only deleted
4086 copy/move constructors. */
4087 if (!abi_version_at_least (12)
4088 && !(warn_abi && abi_version_crosses (12)))
4089 return false;
4091 bool saw_copy = false;
4092 bool saw_non_deleted = false;
4094 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
4095 saw_copy = saw_non_deleted = true;
4096 else if (CLASSTYPE_LAZY_COPY_CTOR (t))
4098 saw_copy = true;
4099 if (classtype_has_move_assign_or_move_ctor_p (t, true))
4100 /* [class.copy]/8 If the class definition declares a move
4101 constructor or move assignment operator, the implicitly declared
4102 copy constructor is defined as deleted.... */;
4103 else
4104 /* Any other reason the implicitly-declared function would be
4105 deleted would also cause TYPE_HAS_COMPLEX_COPY_CTOR to be
4106 set. */
4107 saw_non_deleted = true;
4110 if (!saw_non_deleted)
4111 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4113 tree fn = *iter;
4114 if (copy_fn_p (fn))
4116 saw_copy = true;
4117 if (!DECL_DELETED_FN (fn))
4119 /* Not deleted, therefore trivial. */
4120 saw_non_deleted = true;
4121 break;
4126 gcc_assert (saw_copy);
4128 if (saw_copy && !saw_non_deleted)
4130 if (warn_abi && abi_version_crosses (12))
4131 remember_deleted_copy (t);
4132 if (abi_version_at_least (12))
4133 return true;
4136 return false;
4138 else
4139 return 0;
4142 /* Returns 1 iff type T is a trivially copyable type, as defined in
4143 [basic.types] and [class]. */
4145 bool
4146 trivially_copyable_p (const_tree t)
4148 t = strip_array_types (CONST_CAST_TREE (t));
4150 if (CLASS_TYPE_P (t))
4151 return ((!TYPE_HAS_COPY_CTOR (t)
4152 || !TYPE_HAS_COMPLEX_COPY_CTOR (t))
4153 && !TYPE_HAS_COMPLEX_MOVE_CTOR (t)
4154 && (!TYPE_HAS_COPY_ASSIGN (t)
4155 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (t))
4156 && !TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
4157 && TYPE_HAS_TRIVIAL_DESTRUCTOR (t));
4158 else
4159 return !CP_TYPE_VOLATILE_P (t) && scalarish_type_p (t);
4162 /* Returns 1 iff type T is a trivial type, as defined in [basic.types] and
4163 [class]. */
4165 bool
4166 trivial_type_p (const_tree t)
4168 t = strip_array_types (CONST_CAST_TREE (t));
4170 if (CLASS_TYPE_P (t))
4171 return (TYPE_HAS_TRIVIAL_DFLT (t)
4172 && trivially_copyable_p (t));
4173 else
4174 return scalarish_type_p (t);
4177 /* Returns 1 iff type T is a POD type, as defined in [basic.types]. */
4179 bool
4180 pod_type_p (const_tree t)
4182 /* This CONST_CAST is okay because strip_array_types returns its
4183 argument unmodified and we assign it to a const_tree. */
4184 t = strip_array_types (CONST_CAST_TREE(t));
4186 if (!CLASS_TYPE_P (t))
4187 return scalarish_type_p (t);
4188 else if (cxx_dialect > cxx98)
4189 /* [class]/10: A POD struct is a class that is both a trivial class and a
4190 standard-layout class, and has no non-static data members of type
4191 non-POD struct, non-POD union (or array of such types).
4193 We don't need to check individual members because if a member is
4194 non-std-layout or non-trivial, the class will be too. */
4195 return (std_layout_type_p (t) && trivial_type_p (t));
4196 else
4197 /* The C++98 definition of POD is different. */
4198 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
4201 /* Returns true iff T is POD for the purpose of layout, as defined in the
4202 C++ ABI. */
4204 bool
4205 layout_pod_type_p (const_tree t)
4207 t = strip_array_types (CONST_CAST_TREE (t));
4209 if (CLASS_TYPE_P (t))
4210 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
4211 else
4212 return scalarish_type_p (t);
4215 /* Returns true iff T is a standard-layout type, as defined in
4216 [basic.types]. */
4218 bool
4219 std_layout_type_p (const_tree t)
4221 t = strip_array_types (CONST_CAST_TREE (t));
4223 if (CLASS_TYPE_P (t))
4224 return !CLASSTYPE_NON_STD_LAYOUT (t);
4225 else
4226 return scalarish_type_p (t);
4229 static bool record_has_unique_obj_representations (const_tree, const_tree);
4231 /* Returns true iff T satisfies std::has_unique_object_representations<T>,
4232 as defined in [meta.unary.prop]. */
4234 bool
4235 type_has_unique_obj_representations (const_tree t)
4237 bool ret;
4239 t = strip_array_types (CONST_CAST_TREE (t));
4241 if (!trivially_copyable_p (t))
4242 return false;
4244 if (CLASS_TYPE_P (t) && CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t))
4245 return CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t);
4247 switch (TREE_CODE (t))
4249 case INTEGER_TYPE:
4250 case POINTER_TYPE:
4251 case REFERENCE_TYPE:
4252 /* If some backend has any paddings in these types, we should add
4253 a target hook for this and handle it there. */
4254 return true;
4256 case BOOLEAN_TYPE:
4257 /* For bool values other than 0 and 1 should only appear with
4258 undefined behavior. */
4259 return true;
4261 case ENUMERAL_TYPE:
4262 return type_has_unique_obj_representations (ENUM_UNDERLYING_TYPE (t));
4264 case REAL_TYPE:
4265 /* XFmode certainly contains padding on x86, which the CPU doesn't store
4266 when storing long double values, so for that we have to return false.
4267 Other kinds of floating point values are questionable due to +.0/-.0
4268 and NaNs, let's play safe for now. */
4269 return false;
4271 case FIXED_POINT_TYPE:
4272 return false;
4274 case OFFSET_TYPE:
4275 return true;
4277 case COMPLEX_TYPE:
4278 case VECTOR_TYPE:
4279 return type_has_unique_obj_representations (TREE_TYPE (t));
4281 case RECORD_TYPE:
4282 ret = record_has_unique_obj_representations (t, TYPE_SIZE (t));
4283 if (CLASS_TYPE_P (t))
4285 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
4286 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
4288 return ret;
4290 case UNION_TYPE:
4291 ret = true;
4292 bool any_fields;
4293 any_fields = false;
4294 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4295 if (TREE_CODE (field) == FIELD_DECL)
4297 any_fields = true;
4298 if (!type_has_unique_obj_representations (TREE_TYPE (field))
4299 || simple_cst_equal (DECL_SIZE (field), TYPE_SIZE (t)) != 1)
4301 ret = false;
4302 break;
4305 if (!any_fields && !integer_zerop (TYPE_SIZE (t)))
4306 ret = false;
4307 if (CLASS_TYPE_P (t))
4309 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
4310 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
4312 return ret;
4314 case NULLPTR_TYPE:
4315 return false;
4317 case ERROR_MARK:
4318 return false;
4320 default:
4321 gcc_unreachable ();
4325 /* Helper function for type_has_unique_obj_representations. */
4327 static bool
4328 record_has_unique_obj_representations (const_tree t, const_tree sz)
4330 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4331 if (TREE_CODE (field) != FIELD_DECL)
4333 /* For bases, can't use type_has_unique_obj_representations here, as in
4334 struct S { int i : 24; S (); };
4335 struct T : public S { int j : 8; T (); };
4336 S doesn't have unique obj representations, but T does. */
4337 else if (DECL_FIELD_IS_BASE (field))
4339 if (!record_has_unique_obj_representations (TREE_TYPE (field),
4340 DECL_SIZE (field)))
4341 return false;
4343 else if (DECL_C_BIT_FIELD (field))
4345 tree btype = DECL_BIT_FIELD_TYPE (field);
4346 if (!type_has_unique_obj_representations (btype))
4347 return false;
4349 else if (!type_has_unique_obj_representations (TREE_TYPE (field)))
4350 return false;
4352 offset_int cur = 0;
4353 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4354 if (TREE_CODE (field) == FIELD_DECL)
4356 offset_int fld = wi::to_offset (DECL_FIELD_OFFSET (field));
4357 offset_int bitpos = wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
4358 fld = fld * BITS_PER_UNIT + bitpos;
4359 if (cur != fld)
4360 return false;
4361 if (DECL_SIZE (field))
4363 offset_int size = wi::to_offset (DECL_SIZE (field));
4364 cur += size;
4367 if (cur != wi::to_offset (sz))
4368 return false;
4370 return true;
4373 /* Nonzero iff type T is a class template implicit specialization. */
4375 bool
4376 class_tmpl_impl_spec_p (const_tree t)
4378 return CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INSTANTIATION (t);
4381 /* Returns 1 iff zero initialization of type T means actually storing
4382 zeros in it. */
4385 zero_init_p (const_tree t)
4387 /* This CONST_CAST is okay because strip_array_types returns its
4388 argument unmodified and we assign it to a const_tree. */
4389 t = strip_array_types (CONST_CAST_TREE(t));
4391 if (t == error_mark_node)
4392 return 1;
4394 /* NULL pointers to data members are initialized with -1. */
4395 if (TYPE_PTRDATAMEM_P (t))
4396 return 0;
4398 /* Classes that contain types that can't be zero-initialized, cannot
4399 be zero-initialized themselves. */
4400 if (CLASS_TYPE_P (t) && CLASSTYPE_NON_ZERO_INIT_P (t))
4401 return 0;
4403 return 1;
4406 /* Handle the C++17 [[nodiscard]] attribute, which is similar to the GNU
4407 warn_unused_result attribute. */
4409 static tree
4410 handle_nodiscard_attribute (tree *node, tree name, tree /*args*/,
4411 int /*flags*/, bool *no_add_attrs)
4413 if (TREE_CODE (*node) == FUNCTION_DECL)
4415 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
4416 warning (OPT_Wattributes, "%qE attribute applied to %qD with void "
4417 "return type", name, *node);
4419 else if (OVERLOAD_TYPE_P (*node))
4420 /* OK */;
4421 else
4423 warning (OPT_Wattributes, "%qE attribute can only be applied to "
4424 "functions or to class or enumeration types", name);
4425 *no_add_attrs = true;
4427 return NULL_TREE;
4430 /* Table of valid C++ attributes. */
4431 const struct attribute_spec cxx_attribute_table[] =
4433 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4434 affects_type_identity, handler, exclude } */
4435 { "init_priority", 1, 1, true, false, false, false,
4436 handle_init_priority_attribute, NULL },
4437 { "abi_tag", 1, -1, false, false, false, true,
4438 handle_abi_tag_attribute, NULL },
4439 { NULL, 0, 0, false, false, false, false, NULL, NULL }
4442 /* Table of C++ standard attributes. */
4443 const struct attribute_spec std_attribute_table[] =
4445 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4446 affects_type_identity, handler, exclude } */
4447 { "maybe_unused", 0, 0, false, false, false, false,
4448 handle_unused_attribute, NULL },
4449 { "nodiscard", 0, 0, false, false, false, false,
4450 handle_nodiscard_attribute, NULL },
4451 { NULL, 0, 0, false, false, false, false, NULL, NULL }
4454 /* Handle an "init_priority" attribute; arguments as in
4455 struct attribute_spec.handler. */
4456 static tree
4457 handle_init_priority_attribute (tree* node,
4458 tree name,
4459 tree args,
4460 int /*flags*/,
4461 bool* no_add_attrs)
4463 tree initp_expr = TREE_VALUE (args);
4464 tree decl = *node;
4465 tree type = TREE_TYPE (decl);
4466 int pri;
4468 STRIP_NOPS (initp_expr);
4469 initp_expr = default_conversion (initp_expr);
4470 if (initp_expr)
4471 initp_expr = maybe_constant_value (initp_expr);
4473 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
4475 error ("requested init_priority is not an integer constant");
4476 cxx_constant_value (initp_expr);
4477 *no_add_attrs = true;
4478 return NULL_TREE;
4481 pri = TREE_INT_CST_LOW (initp_expr);
4483 type = strip_array_types (type);
4485 if (decl == NULL_TREE
4486 || !VAR_P (decl)
4487 || !TREE_STATIC (decl)
4488 || DECL_EXTERNAL (decl)
4489 || (TREE_CODE (type) != RECORD_TYPE
4490 && TREE_CODE (type) != UNION_TYPE)
4491 /* Static objects in functions are initialized the
4492 first time control passes through that
4493 function. This is not precise enough to pin down an
4494 init_priority value, so don't allow it. */
4495 || current_function_decl)
4497 error ("can only use %qE attribute on file-scope definitions "
4498 "of objects of class type", name);
4499 *no_add_attrs = true;
4500 return NULL_TREE;
4503 if (pri > MAX_INIT_PRIORITY || pri <= 0)
4505 error ("requested init_priority is out of range");
4506 *no_add_attrs = true;
4507 return NULL_TREE;
4510 /* Check for init_priorities that are reserved for
4511 language and runtime support implementations.*/
4512 if (pri <= MAX_RESERVED_INIT_PRIORITY)
4514 warning
4515 (0, "requested init_priority is reserved for internal use");
4518 if (SUPPORTS_INIT_PRIORITY)
4520 SET_DECL_INIT_PRIORITY (decl, pri);
4521 DECL_HAS_INIT_PRIORITY_P (decl) = 1;
4522 return NULL_TREE;
4524 else
4526 error ("%qE attribute is not supported on this platform", name);
4527 *no_add_attrs = true;
4528 return NULL_TREE;
4532 /* DECL is being redeclared; the old declaration had the abi tags in OLD,
4533 and the new one has the tags in NEW_. Give an error if there are tags
4534 in NEW_ that weren't in OLD. */
4536 bool
4537 check_abi_tag_redeclaration (const_tree decl, const_tree old, const_tree new_)
4539 if (old && TREE_CODE (TREE_VALUE (old)) == TREE_LIST)
4540 old = TREE_VALUE (old);
4541 if (new_ && TREE_CODE (TREE_VALUE (new_)) == TREE_LIST)
4542 new_ = TREE_VALUE (new_);
4543 bool err = false;
4544 for (const_tree t = new_; t; t = TREE_CHAIN (t))
4546 tree str = TREE_VALUE (t);
4547 for (const_tree in = old; in; in = TREE_CHAIN (in))
4549 tree ostr = TREE_VALUE (in);
4550 if (cp_tree_equal (str, ostr))
4551 goto found;
4553 error ("redeclaration of %qD adds abi tag %qE", decl, str);
4554 err = true;
4555 found:;
4557 if (err)
4559 inform (DECL_SOURCE_LOCATION (decl), "previous declaration here");
4560 return false;
4562 return true;
4565 /* The abi_tag attribute with the name NAME was given ARGS. If they are
4566 ill-formed, give an error and return false; otherwise, return true. */
4568 bool
4569 check_abi_tag_args (tree args, tree name)
4571 if (!args)
4573 error ("the %qE attribute requires arguments", name);
4574 return false;
4576 for (tree arg = args; arg; arg = TREE_CHAIN (arg))
4578 tree elt = TREE_VALUE (arg);
4579 if (TREE_CODE (elt) != STRING_CST
4580 || (!same_type_ignoring_top_level_qualifiers_p
4581 (strip_array_types (TREE_TYPE (elt)),
4582 char_type_node)))
4584 error ("arguments to the %qE attribute must be narrow string "
4585 "literals", name);
4586 return false;
4588 const char *begin = TREE_STRING_POINTER (elt);
4589 const char *end = begin + TREE_STRING_LENGTH (elt);
4590 for (const char *p = begin; p != end; ++p)
4592 char c = *p;
4593 if (p == begin)
4595 if (!ISALPHA (c) && c != '_')
4597 error ("arguments to the %qE attribute must contain valid "
4598 "identifiers", name);
4599 inform (input_location, "%<%c%> is not a valid first "
4600 "character for an identifier", c);
4601 return false;
4604 else if (p == end - 1)
4605 gcc_assert (c == 0);
4606 else
4608 if (!ISALNUM (c) && c != '_')
4610 error ("arguments to the %qE attribute must contain valid "
4611 "identifiers", name);
4612 inform (input_location, "%<%c%> is not a valid character "
4613 "in an identifier", c);
4614 return false;
4619 return true;
4622 /* Handle an "abi_tag" attribute; arguments as in
4623 struct attribute_spec.handler. */
4625 static tree
4626 handle_abi_tag_attribute (tree* node, tree name, tree args,
4627 int flags, bool* no_add_attrs)
4629 if (!check_abi_tag_args (args, name))
4630 goto fail;
4632 if (TYPE_P (*node))
4634 if (!OVERLOAD_TYPE_P (*node))
4636 error ("%qE attribute applied to non-class, non-enum type %qT",
4637 name, *node);
4638 goto fail;
4640 else if (!(flags & (int)ATTR_FLAG_TYPE_IN_PLACE))
4642 error ("%qE attribute applied to %qT after its definition",
4643 name, *node);
4644 goto fail;
4646 else if (CLASS_TYPE_P (*node)
4647 && CLASSTYPE_TEMPLATE_INSTANTIATION (*node))
4649 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
4650 "template instantiation %qT", name, *node);
4651 goto fail;
4653 else if (CLASS_TYPE_P (*node)
4654 && CLASSTYPE_TEMPLATE_SPECIALIZATION (*node))
4656 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
4657 "template specialization %qT", name, *node);
4658 goto fail;
4661 tree attributes = TYPE_ATTRIBUTES (*node);
4662 tree decl = TYPE_NAME (*node);
4664 /* Make sure all declarations have the same abi tags. */
4665 if (DECL_SOURCE_LOCATION (decl) != input_location)
4667 if (!check_abi_tag_redeclaration (decl,
4668 lookup_attribute ("abi_tag",
4669 attributes),
4670 args))
4671 goto fail;
4674 else
4676 if (!VAR_OR_FUNCTION_DECL_P (*node))
4678 error ("%qE attribute applied to non-function, non-variable %qD",
4679 name, *node);
4680 goto fail;
4682 else if (DECL_LANGUAGE (*node) == lang_c)
4684 error ("%qE attribute applied to extern \"C\" declaration %qD",
4685 name, *node);
4686 goto fail;
4690 return NULL_TREE;
4692 fail:
4693 *no_add_attrs = true;
4694 return NULL_TREE;
4697 /* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the
4698 thing pointed to by the constant. */
4700 tree
4701 make_ptrmem_cst (tree type, tree member)
4703 tree ptrmem_cst = make_node (PTRMEM_CST);
4704 TREE_TYPE (ptrmem_cst) = type;
4705 PTRMEM_CST_MEMBER (ptrmem_cst) = member;
4706 return ptrmem_cst;
4709 /* Build a variant of TYPE that has the indicated ATTRIBUTES. May
4710 return an existing type if an appropriate type already exists. */
4712 tree
4713 cp_build_type_attribute_variant (tree type, tree attributes)
4715 tree new_type;
4717 new_type = build_type_attribute_variant (type, attributes);
4718 if (TREE_CODE (new_type) == FUNCTION_TYPE
4719 || TREE_CODE (new_type) == METHOD_TYPE)
4721 new_type = build_exception_variant (new_type,
4722 TYPE_RAISES_EXCEPTIONS (type));
4723 new_type = build_ref_qualified_type (new_type,
4724 type_memfn_rqual (type));
4727 /* Making a new main variant of a class type is broken. */
4728 gcc_assert (!CLASS_TYPE_P (type) || new_type == type);
4730 return new_type;
4733 /* Return TRUE if TYPE1 and TYPE2 are identical for type hashing purposes.
4734 Called only after doing all language independent checks. */
4736 bool
4737 cxx_type_hash_eq (const_tree typea, const_tree typeb)
4739 gcc_assert (TREE_CODE (typea) == FUNCTION_TYPE
4740 || TREE_CODE (typea) == METHOD_TYPE);
4742 if (type_memfn_rqual (typea) != type_memfn_rqual (typeb))
4743 return false;
4744 return comp_except_specs (TYPE_RAISES_EXCEPTIONS (typea),
4745 TYPE_RAISES_EXCEPTIONS (typeb), ce_exact);
4748 /* Copy the language-specific type variant modifiers from TYPEB to TYPEA. For
4749 C++, these are the exception-specifier and ref-qualifier. */
4751 tree
4752 cxx_copy_lang_qualifiers (const_tree typea, const_tree typeb)
4754 tree type = CONST_CAST_TREE (typea);
4755 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
4757 type = build_exception_variant (type, TYPE_RAISES_EXCEPTIONS (typeb));
4758 type = build_ref_qualified_type (type, type_memfn_rqual (typeb));
4760 return type;
4763 /* Apply FUNC to all language-specific sub-trees of TP in a pre-order
4764 traversal. Called from walk_tree. */
4766 tree
4767 cp_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func,
4768 void *data, hash_set<tree> *pset)
4770 enum tree_code code = TREE_CODE (*tp);
4771 tree result;
4773 #define WALK_SUBTREE(NODE) \
4774 do \
4776 result = cp_walk_tree (&(NODE), func, data, pset); \
4777 if (result) goto out; \
4779 while (0)
4781 /* Not one of the easy cases. We must explicitly go through the
4782 children. */
4783 result = NULL_TREE;
4784 switch (code)
4786 case DEFAULT_ARG:
4787 case TEMPLATE_TEMPLATE_PARM:
4788 case BOUND_TEMPLATE_TEMPLATE_PARM:
4789 case UNBOUND_CLASS_TEMPLATE:
4790 case TEMPLATE_PARM_INDEX:
4791 case TEMPLATE_TYPE_PARM:
4792 case TYPENAME_TYPE:
4793 case TYPEOF_TYPE:
4794 case UNDERLYING_TYPE:
4795 /* None of these have subtrees other than those already walked
4796 above. */
4797 *walk_subtrees_p = 0;
4798 break;
4800 case BASELINK:
4801 if (BASELINK_QUALIFIED_P (*tp))
4802 WALK_SUBTREE (BINFO_TYPE (BASELINK_ACCESS_BINFO (*tp)));
4803 WALK_SUBTREE (BASELINK_FUNCTIONS (*tp));
4804 *walk_subtrees_p = 0;
4805 break;
4807 case PTRMEM_CST:
4808 WALK_SUBTREE (TREE_TYPE (*tp));
4809 *walk_subtrees_p = 0;
4810 break;
4812 case TREE_LIST:
4813 WALK_SUBTREE (TREE_PURPOSE (*tp));
4814 break;
4816 case OVERLOAD:
4817 WALK_SUBTREE (OVL_FUNCTION (*tp));
4818 WALK_SUBTREE (OVL_CHAIN (*tp));
4819 *walk_subtrees_p = 0;
4820 break;
4822 case USING_DECL:
4823 WALK_SUBTREE (DECL_NAME (*tp));
4824 WALK_SUBTREE (USING_DECL_SCOPE (*tp));
4825 WALK_SUBTREE (USING_DECL_DECLS (*tp));
4826 *walk_subtrees_p = 0;
4827 break;
4829 case RECORD_TYPE:
4830 if (TYPE_PTRMEMFUNC_P (*tp))
4831 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (*tp));
4832 break;
4834 case TYPE_ARGUMENT_PACK:
4835 case NONTYPE_ARGUMENT_PACK:
4837 tree args = ARGUMENT_PACK_ARGS (*tp);
4838 int i, len = TREE_VEC_LENGTH (args);
4839 for (i = 0; i < len; i++)
4840 WALK_SUBTREE (TREE_VEC_ELT (args, i));
4842 break;
4844 case TYPE_PACK_EXPANSION:
4845 WALK_SUBTREE (TREE_TYPE (*tp));
4846 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp));
4847 *walk_subtrees_p = 0;
4848 break;
4850 case EXPR_PACK_EXPANSION:
4851 WALK_SUBTREE (TREE_OPERAND (*tp, 0));
4852 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp));
4853 *walk_subtrees_p = 0;
4854 break;
4856 case CAST_EXPR:
4857 case REINTERPRET_CAST_EXPR:
4858 case STATIC_CAST_EXPR:
4859 case CONST_CAST_EXPR:
4860 case DYNAMIC_CAST_EXPR:
4861 case IMPLICIT_CONV_EXPR:
4862 if (TREE_TYPE (*tp))
4863 WALK_SUBTREE (TREE_TYPE (*tp));
4866 int i;
4867 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (*tp)); ++i)
4868 WALK_SUBTREE (TREE_OPERAND (*tp, i));
4870 *walk_subtrees_p = 0;
4871 break;
4873 case TRAIT_EXPR:
4874 WALK_SUBTREE (TRAIT_EXPR_TYPE1 (*tp));
4875 WALK_SUBTREE (TRAIT_EXPR_TYPE2 (*tp));
4876 *walk_subtrees_p = 0;
4877 break;
4879 case DECLTYPE_TYPE:
4880 WALK_SUBTREE (DECLTYPE_TYPE_EXPR (*tp));
4881 *walk_subtrees_p = 0;
4882 break;
4884 case REQUIRES_EXPR:
4885 // Only recurse through the nested expression. Do not
4886 // walk the parameter list. Doing so causes false
4887 // positives in the pack expansion checker since the
4888 // requires parameters are introduced as pack expansions.
4889 WALK_SUBTREE (TREE_OPERAND (*tp, 1));
4890 *walk_subtrees_p = 0;
4891 break;
4893 case DECL_EXPR:
4894 /* User variables should be mentioned in BIND_EXPR_VARS
4895 and their initializers and sizes walked when walking
4896 the containing BIND_EXPR. Compiler temporaries are
4897 handled here. */
4898 if (VAR_P (TREE_OPERAND (*tp, 0))
4899 && DECL_ARTIFICIAL (TREE_OPERAND (*tp, 0))
4900 && !TREE_STATIC (TREE_OPERAND (*tp, 0)))
4902 tree decl = TREE_OPERAND (*tp, 0);
4903 WALK_SUBTREE (DECL_INITIAL (decl));
4904 WALK_SUBTREE (DECL_SIZE (decl));
4905 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
4907 break;
4909 default:
4910 return NULL_TREE;
4913 /* We didn't find what we were looking for. */
4914 out:
4915 return result;
4917 #undef WALK_SUBTREE
4920 /* Like save_expr, but for C++. */
4922 tree
4923 cp_save_expr (tree expr)
4925 /* There is no reason to create a SAVE_EXPR within a template; if
4926 needed, we can create the SAVE_EXPR when instantiating the
4927 template. Furthermore, the middle-end cannot handle C++-specific
4928 tree codes. */
4929 if (processing_template_decl)
4930 return expr;
4931 return save_expr (expr);
4934 /* Initialize tree.c. */
4936 void
4937 init_tree (void)
4939 list_hash_table = hash_table<list_hasher>::create_ggc (61);
4940 register_scoped_attributes (std_attribute_table, NULL);
4943 /* Returns the kind of special function that DECL (a FUNCTION_DECL)
4944 is. Note that sfk_none is zero, so this function can be used as a
4945 predicate to test whether or not DECL is a special function. */
4947 special_function_kind
4948 special_function_p (const_tree decl)
4950 /* Rather than doing all this stuff with magic names, we should
4951 probably have a field of type `special_function_kind' in
4952 DECL_LANG_SPECIFIC. */
4953 if (DECL_INHERITED_CTOR (decl))
4954 return sfk_inheriting_constructor;
4955 if (DECL_COPY_CONSTRUCTOR_P (decl))
4956 return sfk_copy_constructor;
4957 if (DECL_MOVE_CONSTRUCTOR_P (decl))
4958 return sfk_move_constructor;
4959 if (DECL_CONSTRUCTOR_P (decl))
4960 return sfk_constructor;
4961 if (DECL_ASSIGNMENT_OPERATOR_P (decl)
4962 && DECL_OVERLOADED_OPERATOR_IS (decl, NOP_EXPR))
4964 if (copy_fn_p (decl))
4965 return sfk_copy_assignment;
4966 if (move_fn_p (decl))
4967 return sfk_move_assignment;
4969 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
4970 return sfk_destructor;
4971 if (DECL_COMPLETE_DESTRUCTOR_P (decl))
4972 return sfk_complete_destructor;
4973 if (DECL_BASE_DESTRUCTOR_P (decl))
4974 return sfk_base_destructor;
4975 if (DECL_DELETING_DESTRUCTOR_P (decl))
4976 return sfk_deleting_destructor;
4977 if (DECL_CONV_FN_P (decl))
4978 return sfk_conversion;
4979 if (deduction_guide_p (decl))
4980 return sfk_deduction_guide;
4982 return sfk_none;
4985 /* Returns nonzero if TYPE is a character type, including wchar_t. */
4988 char_type_p (tree type)
4990 return (same_type_p (type, char_type_node)
4991 || same_type_p (type, unsigned_char_type_node)
4992 || same_type_p (type, signed_char_type_node)
4993 || same_type_p (type, char16_type_node)
4994 || same_type_p (type, char32_type_node)
4995 || same_type_p (type, wchar_type_node));
4998 /* Returns the kind of linkage associated with the indicated DECL. Th
4999 value returned is as specified by the language standard; it is
5000 independent of implementation details regarding template
5001 instantiation, etc. For example, it is possible that a declaration
5002 to which this function assigns external linkage would not show up
5003 as a global symbol when you run `nm' on the resulting object file. */
5005 linkage_kind
5006 decl_linkage (tree decl)
5008 /* This function doesn't attempt to calculate the linkage from first
5009 principles as given in [basic.link]. Instead, it makes use of
5010 the fact that we have already set TREE_PUBLIC appropriately, and
5011 then handles a few special cases. Ideally, we would calculate
5012 linkage first, and then transform that into a concrete
5013 implementation. */
5015 /* Things that don't have names have no linkage. */
5016 if (!DECL_NAME (decl))
5017 return lk_none;
5019 /* Fields have no linkage. */
5020 if (TREE_CODE (decl) == FIELD_DECL)
5021 return lk_none;
5023 /* Things that are TREE_PUBLIC have external linkage. */
5024 if (TREE_PUBLIC (decl))
5025 return lk_external;
5027 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor variants,
5028 check one of the "clones" for the real linkage. */
5029 if ((DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
5030 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl))
5031 && DECL_CHAIN (decl)
5032 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)))
5033 return decl_linkage (DECL_CHAIN (decl));
5035 if (TREE_CODE (decl) == NAMESPACE_DECL)
5036 return lk_external;
5038 /* Linkage of a CONST_DECL depends on the linkage of the enumeration
5039 type. */
5040 if (TREE_CODE (decl) == CONST_DECL)
5041 return decl_linkage (TYPE_NAME (DECL_CONTEXT (decl)));
5043 /* Things in local scope do not have linkage, if they don't have
5044 TREE_PUBLIC set. */
5045 if (decl_function_context (decl))
5046 return lk_none;
5048 /* Members of the anonymous namespace also have TREE_PUBLIC unset, but
5049 are considered to have external linkage for language purposes, as do
5050 template instantiations on targets without weak symbols. DECLs really
5051 meant to have internal linkage have DECL_THIS_STATIC set. */
5052 if (TREE_CODE (decl) == TYPE_DECL)
5053 return lk_external;
5054 if (VAR_OR_FUNCTION_DECL_P (decl))
5056 if (!DECL_THIS_STATIC (decl))
5057 return lk_external;
5059 /* Static data members and static member functions from classes
5060 in anonymous namespace also don't have TREE_PUBLIC set. */
5061 if (DECL_CLASS_CONTEXT (decl))
5062 return lk_external;
5065 /* Everything else has internal linkage. */
5066 return lk_internal;
5069 /* Returns the storage duration of the object or reference associated with
5070 the indicated DECL, which should be a VAR_DECL or PARM_DECL. */
5072 duration_kind
5073 decl_storage_duration (tree decl)
5075 if (TREE_CODE (decl) == PARM_DECL)
5076 return dk_auto;
5077 if (TREE_CODE (decl) == FUNCTION_DECL)
5078 return dk_static;
5079 gcc_assert (VAR_P (decl));
5080 if (!TREE_STATIC (decl)
5081 && !DECL_EXTERNAL (decl))
5082 return dk_auto;
5083 if (CP_DECL_THREAD_LOCAL_P (decl))
5084 return dk_thread;
5085 return dk_static;
5088 /* EXP is an expression that we want to pre-evaluate. Returns (in
5089 *INITP) an expression that will perform the pre-evaluation. The
5090 value returned by this function is a side-effect free expression
5091 equivalent to the pre-evaluated expression. Callers must ensure
5092 that *INITP is evaluated before EXP. */
5094 tree
5095 stabilize_expr (tree exp, tree* initp)
5097 tree init_expr;
5099 if (!TREE_SIDE_EFFECTS (exp))
5100 init_expr = NULL_TREE;
5101 else if (VOID_TYPE_P (TREE_TYPE (exp)))
5103 init_expr = exp;
5104 exp = void_node;
5106 /* There are no expressions with REFERENCE_TYPE, but there can be call
5107 arguments with such a type; just treat it as a pointer. */
5108 else if (TREE_CODE (TREE_TYPE (exp)) == REFERENCE_TYPE
5109 || SCALAR_TYPE_P (TREE_TYPE (exp))
5110 || !glvalue_p (exp))
5112 init_expr = get_target_expr (exp);
5113 exp = TARGET_EXPR_SLOT (init_expr);
5114 if (CLASS_TYPE_P (TREE_TYPE (exp)))
5115 exp = move (exp);
5116 else
5117 exp = rvalue (exp);
5119 else
5121 bool xval = !lvalue_p (exp);
5122 exp = cp_build_addr_expr (exp, tf_warning_or_error);
5123 init_expr = get_target_expr (exp);
5124 exp = TARGET_EXPR_SLOT (init_expr);
5125 exp = cp_build_fold_indirect_ref (exp);
5126 if (xval)
5127 exp = move (exp);
5129 *initp = init_expr;
5131 gcc_assert (!TREE_SIDE_EFFECTS (exp));
5132 return exp;
5135 /* Add NEW_EXPR, an expression whose value we don't care about, after the
5136 similar expression ORIG. */
5138 tree
5139 add_stmt_to_compound (tree orig, tree new_expr)
5141 if (!new_expr || !TREE_SIDE_EFFECTS (new_expr))
5142 return orig;
5143 if (!orig || !TREE_SIDE_EFFECTS (orig))
5144 return new_expr;
5145 return build2 (COMPOUND_EXPR, void_type_node, orig, new_expr);
5148 /* Like stabilize_expr, but for a call whose arguments we want to
5149 pre-evaluate. CALL is modified in place to use the pre-evaluated
5150 arguments, while, upon return, *INITP contains an expression to
5151 compute the arguments. */
5153 void
5154 stabilize_call (tree call, tree *initp)
5156 tree inits = NULL_TREE;
5157 int i;
5158 int nargs = call_expr_nargs (call);
5160 if (call == error_mark_node || processing_template_decl)
5162 *initp = NULL_TREE;
5163 return;
5166 gcc_assert (TREE_CODE (call) == CALL_EXPR);
5168 for (i = 0; i < nargs; i++)
5170 tree init;
5171 CALL_EXPR_ARG (call, i) =
5172 stabilize_expr (CALL_EXPR_ARG (call, i), &init);
5173 inits = add_stmt_to_compound (inits, init);
5176 *initp = inits;
5179 /* Like stabilize_expr, but for an AGGR_INIT_EXPR whose arguments we want
5180 to pre-evaluate. CALL is modified in place to use the pre-evaluated
5181 arguments, while, upon return, *INITP contains an expression to
5182 compute the arguments. */
5184 static void
5185 stabilize_aggr_init (tree call, tree *initp)
5187 tree inits = NULL_TREE;
5188 int i;
5189 int nargs = aggr_init_expr_nargs (call);
5191 if (call == error_mark_node)
5192 return;
5194 gcc_assert (TREE_CODE (call) == AGGR_INIT_EXPR);
5196 for (i = 0; i < nargs; i++)
5198 tree init;
5199 AGGR_INIT_EXPR_ARG (call, i) =
5200 stabilize_expr (AGGR_INIT_EXPR_ARG (call, i), &init);
5201 inits = add_stmt_to_compound (inits, init);
5204 *initp = inits;
5207 /* Like stabilize_expr, but for an initialization.
5209 If the initialization is for an object of class type, this function
5210 takes care not to introduce additional temporaries.
5212 Returns TRUE iff the expression was successfully pre-evaluated,
5213 i.e., if INIT is now side-effect free, except for, possibly, a
5214 single call to a constructor. */
5216 bool
5217 stabilize_init (tree init, tree *initp)
5219 tree t = init;
5221 *initp = NULL_TREE;
5223 if (t == error_mark_node || processing_template_decl)
5224 return true;
5226 if (TREE_CODE (t) == INIT_EXPR)
5227 t = TREE_OPERAND (t, 1);
5228 if (TREE_CODE (t) == TARGET_EXPR)
5229 t = TARGET_EXPR_INITIAL (t);
5231 /* If the RHS can be stabilized without breaking copy elision, stabilize
5232 it. We specifically don't stabilize class prvalues here because that
5233 would mean an extra copy, but they might be stabilized below. */
5234 if (TREE_CODE (init) == INIT_EXPR
5235 && TREE_CODE (t) != CONSTRUCTOR
5236 && TREE_CODE (t) != AGGR_INIT_EXPR
5237 && (SCALAR_TYPE_P (TREE_TYPE (t))
5238 || glvalue_p (t)))
5240 TREE_OPERAND (init, 1) = stabilize_expr (t, initp);
5241 return true;
5244 if (TREE_CODE (t) == COMPOUND_EXPR
5245 && TREE_CODE (init) == INIT_EXPR)
5247 tree last = expr_last (t);
5248 /* Handle stabilizing the EMPTY_CLASS_EXPR pattern. */
5249 if (!TREE_SIDE_EFFECTS (last))
5251 *initp = t;
5252 TREE_OPERAND (init, 1) = last;
5253 return true;
5257 if (TREE_CODE (t) == CONSTRUCTOR)
5259 /* Aggregate initialization: stabilize each of the field
5260 initializers. */
5261 unsigned i;
5262 constructor_elt *ce;
5263 bool good = true;
5264 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
5265 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
5267 tree type = TREE_TYPE (ce->value);
5268 tree subinit;
5269 if (TREE_CODE (type) == REFERENCE_TYPE
5270 || SCALAR_TYPE_P (type))
5271 ce->value = stabilize_expr (ce->value, &subinit);
5272 else if (!stabilize_init (ce->value, &subinit))
5273 good = false;
5274 *initp = add_stmt_to_compound (*initp, subinit);
5276 return good;
5279 if (TREE_CODE (t) == CALL_EXPR)
5281 stabilize_call (t, initp);
5282 return true;
5285 if (TREE_CODE (t) == AGGR_INIT_EXPR)
5287 stabilize_aggr_init (t, initp);
5288 return true;
5291 /* The initialization is being performed via a bitwise copy -- and
5292 the item copied may have side effects. */
5293 return !TREE_SIDE_EFFECTS (init);
5296 /* Returns true if a cast to TYPE may appear in an integral constant
5297 expression. */
5299 bool
5300 cast_valid_in_integral_constant_expression_p (tree type)
5302 return (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5303 || cxx_dialect >= cxx11
5304 || dependent_type_p (type)
5305 || type == error_mark_node);
5308 /* Return true if we need to fix linkage information of DECL. */
5310 static bool
5311 cp_fix_function_decl_p (tree decl)
5313 /* Skip if DECL is not externally visible. */
5314 if (!TREE_PUBLIC (decl))
5315 return false;
5317 /* We need to fix DECL if it a appears to be exported but with no
5318 function body. Thunks do not have CFGs and we may need to
5319 handle them specially later. */
5320 if (!gimple_has_body_p (decl)
5321 && !DECL_THUNK_P (decl)
5322 && !DECL_EXTERNAL (decl))
5324 struct cgraph_node *node = cgraph_node::get (decl);
5326 /* Don't fix same_body aliases. Although they don't have their own
5327 CFG, they share it with what they alias to. */
5328 if (!node || !node->alias
5329 || !vec_safe_length (node->ref_list.references))
5330 return true;
5333 return false;
5336 /* Clean the C++ specific parts of the tree T. */
5338 void
5339 cp_free_lang_data (tree t)
5341 if (TREE_CODE (t) == METHOD_TYPE
5342 || TREE_CODE (t) == FUNCTION_TYPE)
5344 /* Default args are not interesting anymore. */
5345 tree argtypes = TYPE_ARG_TYPES (t);
5346 while (argtypes)
5348 TREE_PURPOSE (argtypes) = 0;
5349 argtypes = TREE_CHAIN (argtypes);
5352 else if (TREE_CODE (t) == FUNCTION_DECL
5353 && cp_fix_function_decl_p (t))
5355 /* If T is used in this translation unit at all, the definition
5356 must exist somewhere else since we have decided to not emit it
5357 in this TU. So make it an external reference. */
5358 DECL_EXTERNAL (t) = 1;
5359 TREE_STATIC (t) = 0;
5361 if (TREE_CODE (t) == NAMESPACE_DECL)
5362 /* We do not need the leftover chaining of namespaces from the
5363 binding level. */
5364 DECL_CHAIN (t) = NULL_TREE;
5367 /* Stub for c-common. Please keep in sync with c-decl.c.
5368 FIXME: If address space support is target specific, then this
5369 should be a C target hook. But currently this is not possible,
5370 because this function is called via REGISTER_TARGET_PRAGMAS. */
5371 void
5372 c_register_addr_space (const char * /*word*/, addr_space_t /*as*/)
5376 /* Return the number of operands in T that we care about for things like
5377 mangling. */
5380 cp_tree_operand_length (const_tree t)
5382 enum tree_code code = TREE_CODE (t);
5384 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
5385 return VL_EXP_OPERAND_LENGTH (t);
5387 return cp_tree_code_length (code);
5390 /* Like cp_tree_operand_length, but takes a tree_code CODE. */
5393 cp_tree_code_length (enum tree_code code)
5395 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
5397 switch (code)
5399 case PREINCREMENT_EXPR:
5400 case PREDECREMENT_EXPR:
5401 case POSTINCREMENT_EXPR:
5402 case POSTDECREMENT_EXPR:
5403 return 1;
5405 case ARRAY_REF:
5406 return 2;
5408 case EXPR_PACK_EXPANSION:
5409 return 1;
5411 default:
5412 return TREE_CODE_LENGTH (code);
5416 /* Wrapper around warn_deprecated_use that doesn't warn for
5417 current_class_type. */
5419 void
5420 cp_warn_deprecated_use (tree node)
5422 if (TYPE_P (node)
5423 && current_class_type
5424 && TYPE_MAIN_VARIANT (node) == current_class_type)
5425 return;
5426 warn_deprecated_use (node, NULL_TREE);
5429 /* Implement -Wzero_as_null_pointer_constant. Return true if the
5430 conditions for the warning hold, false otherwise. */
5431 bool
5432 maybe_warn_zero_as_null_pointer_constant (tree expr, location_t loc)
5434 if (c_inhibit_evaluation_warnings == 0
5435 && !NULLPTR_TYPE_P (TREE_TYPE (expr)))
5437 warning_at (loc, OPT_Wzero_as_null_pointer_constant,
5438 "zero as null pointer constant");
5439 return true;
5441 return false;
5444 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
5445 /* Complain that some language-specific thing hanging off a tree
5446 node has been accessed improperly. */
5448 void
5449 lang_check_failed (const char* file, int line, const char* function)
5451 internal_error ("lang_* check: failed in %s, at %s:%d",
5452 function, trim_filename (file), line);
5454 #endif /* ENABLE_TREE_CHECKING */
5456 #if CHECKING_P
5458 namespace selftest {
5460 /* Verify that lvalue_kind () works, for various expressions,
5461 and that location wrappers don't affect the results. */
5463 static void
5464 test_lvalue_kind ()
5466 location_t loc = BUILTINS_LOCATION;
5468 /* Verify constants and parameters, without and with
5469 location wrappers. */
5470 tree int_cst = build_int_cst (integer_type_node, 42);
5471 ASSERT_EQ (clk_none, lvalue_kind (int_cst));
5473 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
5474 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
5475 ASSERT_EQ (clk_none, lvalue_kind (wrapped_int_cst));
5477 tree string_lit = build_string (4, "foo");
5478 TREE_TYPE (string_lit) = char_array_type_node;
5479 string_lit = fix_string_type (string_lit);
5480 ASSERT_EQ (clk_ordinary, lvalue_kind (string_lit));
5482 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
5483 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
5484 ASSERT_EQ (clk_ordinary, lvalue_kind (wrapped_string_lit));
5486 tree parm = build_decl (UNKNOWN_LOCATION, PARM_DECL,
5487 get_identifier ("some_parm"),
5488 integer_type_node);
5489 ASSERT_EQ (clk_ordinary, lvalue_kind (parm));
5491 tree wrapped_parm = maybe_wrap_with_location (parm, loc);
5492 ASSERT_TRUE (location_wrapper_p (wrapped_parm));
5493 ASSERT_EQ (clk_ordinary, lvalue_kind (wrapped_parm));
5495 /* Verify that lvalue_kind of std::move on a parm isn't
5496 affected by location wrappers. */
5497 tree rvalue_ref_of_parm = move (parm);
5498 ASSERT_EQ (clk_rvalueref, lvalue_kind (rvalue_ref_of_parm));
5499 tree rvalue_ref_of_wrapped_parm = move (wrapped_parm);
5500 ASSERT_EQ (clk_rvalueref, lvalue_kind (rvalue_ref_of_wrapped_parm));
5503 /* Run all of the selftests within this file. */
5505 void
5506 cp_tree_c_tests ()
5508 test_lvalue_kind ();
5511 } // namespace selftest
5513 #endif /* #if CHECKING_P */
5516 #include "gt-cp-tree.h"