PR inline-asm/84742
[official-gcc.git] / gcc / cp / tree.c
blob4cf2126608f0b7fedd008583bf5e09f395118a79
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 (TREE_CODE (to_type) == REFERENCE_TYPE)
1083 rval = rval && TYPE_REF_IS_RVALUE (to_type);
1084 to_type = TREE_TYPE (to_type);
1087 lvalue_ref = build_reference_type (to_type);
1088 if (!rval)
1089 return lvalue_ref;
1091 /* This code to create rvalue reference types is based on and tied
1092 to the code creating lvalue reference types in the middle-end
1093 functions build_reference_type_for_mode and build_reference_type.
1095 It works by putting the rvalue reference type nodes after the
1096 lvalue reference nodes in the TYPE_NEXT_REF_TO linked list, so
1097 they will effectively be ignored by the middle end. */
1099 for (t = lvalue_ref; (t = TYPE_NEXT_REF_TO (t)); )
1100 if (TYPE_REF_IS_RVALUE (t))
1101 return t;
1103 t = build_distinct_type_copy (lvalue_ref);
1105 TYPE_REF_IS_RVALUE (t) = true;
1106 TYPE_NEXT_REF_TO (t) = TYPE_NEXT_REF_TO (lvalue_ref);
1107 TYPE_NEXT_REF_TO (lvalue_ref) = t;
1109 if (TYPE_STRUCTURAL_EQUALITY_P (to_type))
1110 SET_TYPE_STRUCTURAL_EQUALITY (t);
1111 else if (TYPE_CANONICAL (to_type) != to_type)
1112 TYPE_CANONICAL (t)
1113 = cp_build_reference_type (TYPE_CANONICAL (to_type), rval);
1114 else
1115 TYPE_CANONICAL (t) = t;
1117 layout_type (t);
1119 return t;
1123 /* Returns EXPR cast to rvalue reference type, like std::move. */
1125 tree
1126 move (tree expr)
1128 tree type = TREE_TYPE (expr);
1129 gcc_assert (TREE_CODE (type) != REFERENCE_TYPE);
1130 type = cp_build_reference_type (type, /*rval*/true);
1131 return build_static_cast (type, expr, tf_warning_or_error);
1134 /* Used by the C++ front end to build qualified array types. However,
1135 the C version of this function does not properly maintain canonical
1136 types (which are not used in C). */
1137 tree
1138 c_build_qualified_type (tree type, int type_quals, tree /* orig_qual_type */,
1139 size_t /* orig_qual_indirect */)
1141 return cp_build_qualified_type (type, type_quals);
1145 /* Make a variant of TYPE, qualified with the TYPE_QUALS. Handles
1146 arrays correctly. In particular, if TYPE is an array of T's, and
1147 TYPE_QUALS is non-empty, returns an array of qualified T's.
1149 FLAGS determines how to deal with ill-formed qualifications. If
1150 tf_ignore_bad_quals is set, then bad qualifications are dropped
1151 (this is permitted if TYPE was introduced via a typedef or template
1152 type parameter). If bad qualifications are dropped and tf_warning
1153 is set, then a warning is issued for non-const qualifications. If
1154 tf_ignore_bad_quals is not set and tf_error is not set, we
1155 return error_mark_node. Otherwise, we issue an error, and ignore
1156 the qualifications.
1158 Qualification of a reference type is valid when the reference came
1159 via a typedef or template type argument. [dcl.ref] No such
1160 dispensation is provided for qualifying a function type. [dcl.fct]
1161 DR 295 queries this and the proposed resolution brings it into line
1162 with qualifying a reference. We implement the DR. We also behave
1163 in a similar manner for restricting non-pointer types. */
1165 tree
1166 cp_build_qualified_type_real (tree type,
1167 int type_quals,
1168 tsubst_flags_t complain)
1170 tree result;
1171 int bad_quals = TYPE_UNQUALIFIED;
1173 if (type == error_mark_node)
1174 return type;
1176 if (type_quals == cp_type_quals (type))
1177 return type;
1179 if (TREE_CODE (type) == ARRAY_TYPE)
1181 /* In C++, the qualification really applies to the array element
1182 type. Obtain the appropriately qualified element type. */
1183 tree t;
1184 tree element_type
1185 = cp_build_qualified_type_real (TREE_TYPE (type),
1186 type_quals,
1187 complain);
1189 if (element_type == error_mark_node)
1190 return error_mark_node;
1192 /* See if we already have an identically qualified type. Tests
1193 should be equivalent to those in check_qualified_type. */
1194 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
1195 if (TREE_TYPE (t) == element_type
1196 && TYPE_NAME (t) == TYPE_NAME (type)
1197 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
1198 && attribute_list_equal (TYPE_ATTRIBUTES (t),
1199 TYPE_ATTRIBUTES (type)))
1200 break;
1202 if (!t)
1204 t = build_cplus_array_type (element_type, TYPE_DOMAIN (type));
1206 /* Keep the typedef name. */
1207 if (TYPE_NAME (t) != TYPE_NAME (type))
1209 t = build_variant_type_copy (t);
1210 TYPE_NAME (t) = TYPE_NAME (type);
1211 SET_TYPE_ALIGN (t, TYPE_ALIGN (type));
1212 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (type);
1216 /* Even if we already had this variant, we update
1217 TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case
1218 they changed since the variant was originally created.
1220 This seems hokey; if there is some way to use a previous
1221 variant *without* coming through here,
1222 TYPE_NEEDS_CONSTRUCTING will never be updated. */
1223 TYPE_NEEDS_CONSTRUCTING (t)
1224 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
1225 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1226 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
1227 return t;
1229 else if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
1231 tree t = PACK_EXPANSION_PATTERN (type);
1233 t = cp_build_qualified_type_real (t, type_quals, complain);
1234 return make_pack_expansion (t, complain);
1237 /* A reference or method type shall not be cv-qualified.
1238 [dcl.ref], [dcl.fct]. This used to be an error, but as of DR 295
1239 (in CD1) we always ignore extra cv-quals on functions. */
1240 if (type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)
1241 && (TREE_CODE (type) == REFERENCE_TYPE
1242 || TREE_CODE (type) == FUNCTION_TYPE
1243 || TREE_CODE (type) == METHOD_TYPE))
1245 if (TREE_CODE (type) == REFERENCE_TYPE)
1246 bad_quals |= type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1247 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
1250 /* But preserve any function-cv-quals on a FUNCTION_TYPE. */
1251 if (TREE_CODE (type) == FUNCTION_TYPE)
1252 type_quals |= type_memfn_quals (type);
1254 /* A restrict-qualified type must be a pointer (or reference)
1255 to object or incomplete type. */
1256 if ((type_quals & TYPE_QUAL_RESTRICT)
1257 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1258 && TREE_CODE (type) != TYPENAME_TYPE
1259 && !POINTER_TYPE_P (type))
1261 bad_quals |= TYPE_QUAL_RESTRICT;
1262 type_quals &= ~TYPE_QUAL_RESTRICT;
1265 if (bad_quals == TYPE_UNQUALIFIED
1266 || (complain & tf_ignore_bad_quals))
1267 /*OK*/;
1268 else if (!(complain & tf_error))
1269 return error_mark_node;
1270 else
1272 tree bad_type = build_qualified_type (ptr_type_node, bad_quals);
1273 error ("%qV qualifiers cannot be applied to %qT",
1274 bad_type, type);
1277 /* Retrieve (or create) the appropriately qualified variant. */
1278 result = build_qualified_type (type, type_quals);
1280 /* Preserve exception specs and ref-qualifier since build_qualified_type
1281 doesn't know about them. */
1282 if (TREE_CODE (result) == FUNCTION_TYPE
1283 || TREE_CODE (result) == METHOD_TYPE)
1285 result = build_exception_variant (result, TYPE_RAISES_EXCEPTIONS (type));
1286 result = build_ref_qualified_type (result, type_memfn_rqual (type));
1289 return result;
1292 /* Return TYPE with const and volatile removed. */
1294 tree
1295 cv_unqualified (tree type)
1297 int quals;
1299 if (type == error_mark_node)
1300 return type;
1302 quals = cp_type_quals (type);
1303 quals &= ~(TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE);
1304 return cp_build_qualified_type (type, quals);
1307 /* Subroutine of strip_typedefs. We want to apply to RESULT the attributes
1308 from ATTRIBS that affect type identity, and no others. If any are not
1309 applied, set *remove_attributes to true. */
1311 static tree
1312 apply_identity_attributes (tree result, tree attribs, bool *remove_attributes)
1314 tree first_ident = NULL_TREE;
1315 tree new_attribs = NULL_TREE;
1316 tree *p = &new_attribs;
1318 if (OVERLOAD_TYPE_P (result))
1320 /* On classes and enums all attributes are ingrained. */
1321 gcc_assert (attribs == TYPE_ATTRIBUTES (result));
1322 return result;
1325 for (tree a = attribs; a; a = TREE_CHAIN (a))
1327 const attribute_spec *as
1328 = lookup_attribute_spec (get_attribute_name (a));
1329 if (as && as->affects_type_identity)
1331 if (!first_ident)
1332 first_ident = a;
1333 else if (first_ident == error_mark_node)
1335 *p = tree_cons (TREE_PURPOSE (a), TREE_VALUE (a), NULL_TREE);
1336 p = &TREE_CHAIN (*p);
1339 else if (first_ident)
1341 for (tree a2 = first_ident; a2; a2 = TREE_CHAIN (a2))
1343 *p = tree_cons (TREE_PURPOSE (a2), TREE_VALUE (a2), NULL_TREE);
1344 p = &TREE_CHAIN (*p);
1346 first_ident = error_mark_node;
1349 if (first_ident != error_mark_node)
1350 new_attribs = first_ident;
1352 if (first_ident == attribs)
1353 /* All attributes affected type identity. */;
1354 else
1355 *remove_attributes = true;
1357 return cp_build_type_attribute_variant (result, new_attribs);
1360 /* Builds a qualified variant of T that is not a typedef variant.
1361 E.g. consider the following declarations:
1362 typedef const int ConstInt;
1363 typedef ConstInt* PtrConstInt;
1364 If T is PtrConstInt, this function returns a type representing
1365 const int*.
1366 In other words, if T is a typedef, the function returns the underlying type.
1367 The cv-qualification and attributes of the type returned match the
1368 input type.
1369 They will always be compatible types.
1370 The returned type is built so that all of its subtypes
1371 recursively have their typedefs stripped as well.
1373 This is different from just returning TYPE_CANONICAL (T)
1374 Because of several reasons:
1375 * If T is a type that needs structural equality
1376 its TYPE_CANONICAL (T) will be NULL.
1377 * TYPE_CANONICAL (T) desn't carry type attributes
1378 and loses template parameter names.
1380 If REMOVE_ATTRIBUTES is non-null, also strip attributes that don't
1381 affect type identity, and set the referent to true if any were
1382 stripped. */
1384 tree
1385 strip_typedefs (tree t, bool *remove_attributes)
1387 tree result = NULL, type = NULL, t0 = NULL;
1389 if (!t || t == error_mark_node)
1390 return t;
1392 if (TREE_CODE (t) == TREE_LIST)
1394 bool changed = false;
1395 vec<tree,va_gc> *vec = make_tree_vector ();
1396 tree r = t;
1397 for (; t; t = TREE_CHAIN (t))
1399 gcc_assert (!TREE_PURPOSE (t));
1400 tree elt = strip_typedefs (TREE_VALUE (t), remove_attributes);
1401 if (elt != TREE_VALUE (t))
1402 changed = true;
1403 vec_safe_push (vec, elt);
1405 if (changed)
1406 r = build_tree_list_vec (vec);
1407 release_tree_vector (vec);
1408 return r;
1411 gcc_assert (TYPE_P (t));
1413 if (t == TYPE_CANONICAL (t))
1414 return t;
1416 if (dependent_alias_template_spec_p (t))
1417 /* DR 1558: However, if the template-id is dependent, subsequent
1418 template argument substitution still applies to the template-id. */
1419 return t;
1421 switch (TREE_CODE (t))
1423 case POINTER_TYPE:
1424 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1425 result = build_pointer_type (type);
1426 break;
1427 case REFERENCE_TYPE:
1428 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1429 result = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
1430 break;
1431 case OFFSET_TYPE:
1432 t0 = strip_typedefs (TYPE_OFFSET_BASETYPE (t), remove_attributes);
1433 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1434 result = build_offset_type (t0, type);
1435 break;
1436 case RECORD_TYPE:
1437 if (TYPE_PTRMEMFUNC_P (t))
1439 t0 = strip_typedefs (TYPE_PTRMEMFUNC_FN_TYPE (t), remove_attributes);
1440 result = build_ptrmemfunc_type (t0);
1442 break;
1443 case ARRAY_TYPE:
1444 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1445 t0 = strip_typedefs (TYPE_DOMAIN (t), remove_attributes);
1446 result = build_cplus_array_type (type, t0);
1447 break;
1448 case FUNCTION_TYPE:
1449 case METHOD_TYPE:
1451 tree arg_types = NULL, arg_node, arg_node2, arg_type;
1452 bool changed;
1454 /* Because we stomp on TREE_PURPOSE of TYPE_ARG_TYPES in many places
1455 around the compiler (e.g. cp_parser_late_parsing_default_args), we
1456 can't expect that re-hashing a function type will find a previous
1457 equivalent type, so try to reuse the input type if nothing has
1458 changed. If the type is itself a variant, that will change. */
1459 bool is_variant = typedef_variant_p (t);
1460 if (remove_attributes
1461 && (TYPE_ATTRIBUTES (t) || TYPE_USER_ALIGN (t)))
1462 is_variant = true;
1464 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1465 tree canon_spec = (flag_noexcept_type
1466 ? canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (t))
1467 : NULL_TREE);
1468 changed = (type != TREE_TYPE (t) || is_variant
1469 || TYPE_RAISES_EXCEPTIONS (t) != canon_spec);
1471 for (arg_node = TYPE_ARG_TYPES (t);
1472 arg_node;
1473 arg_node = TREE_CHAIN (arg_node))
1475 if (arg_node == void_list_node)
1476 break;
1477 arg_type = strip_typedefs (TREE_VALUE (arg_node),
1478 remove_attributes);
1479 gcc_assert (arg_type);
1480 if (arg_type == TREE_VALUE (arg_node) && !changed)
1481 continue;
1483 if (!changed)
1485 changed = true;
1486 for (arg_node2 = TYPE_ARG_TYPES (t);
1487 arg_node2 != arg_node;
1488 arg_node2 = TREE_CHAIN (arg_node2))
1489 arg_types
1490 = tree_cons (TREE_PURPOSE (arg_node2),
1491 TREE_VALUE (arg_node2), arg_types);
1494 arg_types
1495 = tree_cons (TREE_PURPOSE (arg_node), arg_type, arg_types);
1498 if (!changed)
1499 return t;
1501 if (arg_types)
1502 arg_types = nreverse (arg_types);
1504 /* A list of parameters not ending with an ellipsis
1505 must end with void_list_node. */
1506 if (arg_node)
1507 arg_types = chainon (arg_types, void_list_node);
1509 if (TREE_CODE (t) == METHOD_TYPE)
1511 tree class_type = TREE_TYPE (TREE_VALUE (arg_types));
1512 gcc_assert (class_type);
1513 result =
1514 build_method_type_directly (class_type, type,
1515 TREE_CHAIN (arg_types));
1516 result
1517 = build_ref_qualified_type (result, type_memfn_rqual (t));
1519 else
1521 result = build_function_type (type,
1522 arg_types);
1523 result = apply_memfn_quals (result,
1524 type_memfn_quals (t),
1525 type_memfn_rqual (t));
1528 if (canon_spec)
1529 result = build_exception_variant (result, canon_spec);
1530 if (TYPE_HAS_LATE_RETURN_TYPE (t))
1531 TYPE_HAS_LATE_RETURN_TYPE (result) = 1;
1533 break;
1534 case TYPENAME_TYPE:
1536 bool changed = false;
1537 tree fullname = TYPENAME_TYPE_FULLNAME (t);
1538 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
1539 && TREE_OPERAND (fullname, 1))
1541 tree args = TREE_OPERAND (fullname, 1);
1542 tree new_args = copy_node (args);
1543 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
1545 tree arg = TREE_VEC_ELT (args, i);
1546 tree strip_arg;
1547 if (TYPE_P (arg))
1548 strip_arg = strip_typedefs (arg, remove_attributes);
1549 else
1550 strip_arg = strip_typedefs_expr (arg, remove_attributes);
1551 TREE_VEC_ELT (new_args, i) = strip_arg;
1552 if (strip_arg != arg)
1553 changed = true;
1555 if (changed)
1557 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_args)
1558 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
1559 fullname
1560 = lookup_template_function (TREE_OPERAND (fullname, 0),
1561 new_args);
1563 else
1564 ggc_free (new_args);
1566 tree ctx = strip_typedefs (TYPE_CONTEXT (t), remove_attributes);
1567 if (!changed && ctx == TYPE_CONTEXT (t) && !typedef_variant_p (t))
1568 return t;
1569 tree name = fullname;
1570 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
1571 name = TREE_OPERAND (fullname, 0);
1572 /* Use build_typename_type rather than make_typename_type because we
1573 don't want to resolve it here, just strip typedefs. */
1574 result = build_typename_type (ctx, name, fullname, typename_type);
1576 break;
1577 case DECLTYPE_TYPE:
1578 result = strip_typedefs_expr (DECLTYPE_TYPE_EXPR (t),
1579 remove_attributes);
1580 if (result == DECLTYPE_TYPE_EXPR (t))
1581 result = NULL_TREE;
1582 else
1583 result = (finish_decltype_type
1584 (result,
1585 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t),
1586 tf_none));
1587 break;
1588 case UNDERLYING_TYPE:
1589 type = strip_typedefs (UNDERLYING_TYPE_TYPE (t), remove_attributes);
1590 result = finish_underlying_type (type);
1591 break;
1592 default:
1593 break;
1596 if (!result)
1598 if (typedef_variant_p (t))
1600 /* Explicitly get the underlying type, as TYPE_MAIN_VARIANT doesn't
1601 strip typedefs with attributes. */
1602 result = TYPE_MAIN_VARIANT (DECL_ORIGINAL_TYPE (TYPE_NAME (t)));
1603 result = strip_typedefs (result);
1605 else
1606 result = TYPE_MAIN_VARIANT (t);
1608 gcc_assert (!typedef_variant_p (result));
1610 if (COMPLETE_TYPE_P (result) && !COMPLETE_TYPE_P (t))
1611 /* If RESULT is complete and T isn't, it's likely the case that T
1612 is a variant of RESULT which hasn't been updated yet. Skip the
1613 attribute handling. */;
1614 else
1616 if (TYPE_USER_ALIGN (t) != TYPE_USER_ALIGN (result)
1617 || TYPE_ALIGN (t) != TYPE_ALIGN (result))
1619 gcc_assert (TYPE_USER_ALIGN (t));
1620 if (remove_attributes)
1621 *remove_attributes = true;
1622 else
1624 if (TYPE_ALIGN (t) == TYPE_ALIGN (result))
1625 result = build_variant_type_copy (result);
1626 else
1627 result = build_aligned_type (result, TYPE_ALIGN (t));
1628 TYPE_USER_ALIGN (result) = true;
1632 if (TYPE_ATTRIBUTES (t))
1634 if (remove_attributes)
1635 result = apply_identity_attributes (result, TYPE_ATTRIBUTES (t),
1636 remove_attributes);
1637 else
1638 result = cp_build_type_attribute_variant (result,
1639 TYPE_ATTRIBUTES (t));
1643 return cp_build_qualified_type (result, cp_type_quals (t));
1646 /* Like strip_typedefs above, but works on expressions, so that in
1648 template<class T> struct A
1650 typedef T TT;
1651 B<sizeof(TT)> b;
1654 sizeof(TT) is replaced by sizeof(T). */
1656 tree
1657 strip_typedefs_expr (tree t, bool *remove_attributes)
1659 unsigned i,n;
1660 tree r, type, *ops;
1661 enum tree_code code;
1663 if (t == NULL_TREE || t == error_mark_node)
1664 return t;
1666 if (DECL_P (t) || CONSTANT_CLASS_P (t))
1667 return t;
1669 /* Some expressions have type operands, so let's handle types here rather
1670 than check TYPE_P in multiple places below. */
1671 if (TYPE_P (t))
1672 return strip_typedefs (t, remove_attributes);
1674 code = TREE_CODE (t);
1675 switch (code)
1677 case IDENTIFIER_NODE:
1678 case TEMPLATE_PARM_INDEX:
1679 case OVERLOAD:
1680 case BASELINK:
1681 case ARGUMENT_PACK_SELECT:
1682 return t;
1684 case TRAIT_EXPR:
1686 tree type1 = strip_typedefs (TRAIT_EXPR_TYPE1 (t), remove_attributes);
1687 tree type2 = strip_typedefs (TRAIT_EXPR_TYPE2 (t), remove_attributes);
1688 if (type1 == TRAIT_EXPR_TYPE1 (t)
1689 && type2 == TRAIT_EXPR_TYPE2 (t))
1690 return t;
1691 r = copy_node (t);
1692 TRAIT_EXPR_TYPE1 (r) = type1;
1693 TRAIT_EXPR_TYPE2 (r) = type2;
1694 return r;
1697 case TREE_LIST:
1699 vec<tree, va_gc> *vec = make_tree_vector ();
1700 bool changed = false;
1701 tree it;
1702 for (it = t; it; it = TREE_CHAIN (it))
1704 tree val = strip_typedefs_expr (TREE_VALUE (t), remove_attributes);
1705 vec_safe_push (vec, val);
1706 if (val != TREE_VALUE (t))
1707 changed = true;
1708 gcc_assert (TREE_PURPOSE (it) == NULL_TREE);
1710 if (changed)
1712 r = NULL_TREE;
1713 FOR_EACH_VEC_ELT_REVERSE (*vec, i, it)
1714 r = tree_cons (NULL_TREE, it, r);
1716 else
1717 r = t;
1718 release_tree_vector (vec);
1719 return r;
1722 case TREE_VEC:
1724 bool changed = false;
1725 vec<tree, va_gc> *vec = make_tree_vector ();
1726 n = TREE_VEC_LENGTH (t);
1727 vec_safe_reserve (vec, n);
1728 for (i = 0; i < n; ++i)
1730 tree op = strip_typedefs_expr (TREE_VEC_ELT (t, i),
1731 remove_attributes);
1732 vec->quick_push (op);
1733 if (op != TREE_VEC_ELT (t, i))
1734 changed = true;
1736 if (changed)
1738 r = copy_node (t);
1739 for (i = 0; i < n; ++i)
1740 TREE_VEC_ELT (r, i) = (*vec)[i];
1741 NON_DEFAULT_TEMPLATE_ARGS_COUNT (r)
1742 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
1744 else
1745 r = t;
1746 release_tree_vector (vec);
1747 return r;
1750 case CONSTRUCTOR:
1752 bool changed = false;
1753 vec<constructor_elt, va_gc> *vec
1754 = vec_safe_copy (CONSTRUCTOR_ELTS (t));
1755 n = CONSTRUCTOR_NELTS (t);
1756 type = strip_typedefs (TREE_TYPE (t), remove_attributes);
1757 for (i = 0; i < n; ++i)
1759 constructor_elt *e = &(*vec)[i];
1760 tree op = strip_typedefs_expr (e->value, remove_attributes);
1761 if (op != e->value)
1763 changed = true;
1764 e->value = op;
1766 gcc_checking_assert
1767 (e->index == strip_typedefs_expr (e->index, remove_attributes));
1770 if (!changed && type == TREE_TYPE (t))
1772 vec_free (vec);
1773 return t;
1775 else
1777 r = copy_node (t);
1778 TREE_TYPE (r) = type;
1779 CONSTRUCTOR_ELTS (r) = vec;
1780 return r;
1784 case LAMBDA_EXPR:
1785 error ("lambda-expression in a constant expression");
1786 return error_mark_node;
1788 default:
1789 break;
1792 gcc_assert (EXPR_P (t));
1794 n = cp_tree_operand_length (t);
1795 ops = XALLOCAVEC (tree, n);
1796 type = TREE_TYPE (t);
1798 switch (code)
1800 CASE_CONVERT:
1801 case IMPLICIT_CONV_EXPR:
1802 case DYNAMIC_CAST_EXPR:
1803 case STATIC_CAST_EXPR:
1804 case CONST_CAST_EXPR:
1805 case REINTERPRET_CAST_EXPR:
1806 case CAST_EXPR:
1807 case NEW_EXPR:
1808 type = strip_typedefs (type, remove_attributes);
1809 /* fallthrough */
1811 default:
1812 for (i = 0; i < n; ++i)
1813 ops[i] = strip_typedefs_expr (TREE_OPERAND (t, i), remove_attributes);
1814 break;
1817 /* If nothing changed, return t. */
1818 for (i = 0; i < n; ++i)
1819 if (ops[i] != TREE_OPERAND (t, i))
1820 break;
1821 if (i == n && type == TREE_TYPE (t))
1822 return t;
1824 r = copy_node (t);
1825 TREE_TYPE (r) = type;
1826 for (i = 0; i < n; ++i)
1827 TREE_OPERAND (r, i) = ops[i];
1828 return r;
1831 /* Makes a copy of BINFO and TYPE, which is to be inherited into a
1832 graph dominated by T. If BINFO is NULL, TYPE is a dependent base,
1833 and we do a shallow copy. If BINFO is non-NULL, we do a deep copy.
1834 VIRT indicates whether TYPE is inherited virtually or not.
1835 IGO_PREV points at the previous binfo of the inheritance graph
1836 order chain. The newly copied binfo's TREE_CHAIN forms this
1837 ordering.
1839 The CLASSTYPE_VBASECLASSES vector of T is constructed in the
1840 correct order. That is in the order the bases themselves should be
1841 constructed in.
1843 The BINFO_INHERITANCE of a virtual base class points to the binfo
1844 of the most derived type. ??? We could probably change this so that
1845 BINFO_INHERITANCE becomes synonymous with BINFO_PRIMARY, and hence
1846 remove a field. They currently can only differ for primary virtual
1847 virtual bases. */
1849 tree
1850 copy_binfo (tree binfo, tree type, tree t, tree *igo_prev, int virt)
1852 tree new_binfo;
1854 if (virt)
1856 /* See if we've already made this virtual base. */
1857 new_binfo = binfo_for_vbase (type, t);
1858 if (new_binfo)
1859 return new_binfo;
1862 new_binfo = make_tree_binfo (binfo ? BINFO_N_BASE_BINFOS (binfo) : 0);
1863 BINFO_TYPE (new_binfo) = type;
1865 /* Chain it into the inheritance graph. */
1866 TREE_CHAIN (*igo_prev) = new_binfo;
1867 *igo_prev = new_binfo;
1869 if (binfo && !BINFO_DEPENDENT_BASE_P (binfo))
1871 int ix;
1872 tree base_binfo;
1874 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), type));
1876 BINFO_OFFSET (new_binfo) = BINFO_OFFSET (binfo);
1877 BINFO_VIRTUALS (new_binfo) = BINFO_VIRTUALS (binfo);
1879 /* We do not need to copy the accesses, as they are read only. */
1880 BINFO_BASE_ACCESSES (new_binfo) = BINFO_BASE_ACCESSES (binfo);
1882 /* Recursively copy base binfos of BINFO. */
1883 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
1885 tree new_base_binfo;
1886 new_base_binfo = copy_binfo (base_binfo, BINFO_TYPE (base_binfo),
1887 t, igo_prev,
1888 BINFO_VIRTUAL_P (base_binfo));
1890 if (!BINFO_INHERITANCE_CHAIN (new_base_binfo))
1891 BINFO_INHERITANCE_CHAIN (new_base_binfo) = new_binfo;
1892 BINFO_BASE_APPEND (new_binfo, new_base_binfo);
1895 else
1896 BINFO_DEPENDENT_BASE_P (new_binfo) = 1;
1898 if (virt)
1900 /* Push it onto the list after any virtual bases it contains
1901 will have been pushed. */
1902 CLASSTYPE_VBASECLASSES (t)->quick_push (new_binfo);
1903 BINFO_VIRTUAL_P (new_binfo) = 1;
1904 BINFO_INHERITANCE_CHAIN (new_binfo) = TYPE_BINFO (t);
1907 return new_binfo;
1910 /* Hashing of lists so that we don't make duplicates.
1911 The entry point is `list_hash_canon'. */
1913 struct list_proxy
1915 tree purpose;
1916 tree value;
1917 tree chain;
1920 struct list_hasher : ggc_ptr_hash<tree_node>
1922 typedef list_proxy *compare_type;
1924 static hashval_t hash (tree);
1925 static bool equal (tree, list_proxy *);
1928 /* Now here is the hash table. When recording a list, it is added
1929 to the slot whose index is the hash code mod the table size.
1930 Note that the hash table is used for several kinds of lists.
1931 While all these live in the same table, they are completely independent,
1932 and the hash code is computed differently for each of these. */
1934 static GTY (()) hash_table<list_hasher> *list_hash_table;
1936 /* Compare ENTRY (an entry in the hash table) with DATA (a list_proxy
1937 for a node we are thinking about adding). */
1939 bool
1940 list_hasher::equal (tree t, list_proxy *proxy)
1942 return (TREE_VALUE (t) == proxy->value
1943 && TREE_PURPOSE (t) == proxy->purpose
1944 && TREE_CHAIN (t) == proxy->chain);
1947 /* Compute a hash code for a list (chain of TREE_LIST nodes
1948 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
1949 TREE_COMMON slots), by adding the hash codes of the individual entries. */
1951 static hashval_t
1952 list_hash_pieces (tree purpose, tree value, tree chain)
1954 hashval_t hashcode = 0;
1956 if (chain)
1957 hashcode += TREE_HASH (chain);
1959 if (value)
1960 hashcode += TREE_HASH (value);
1961 else
1962 hashcode += 1007;
1963 if (purpose)
1964 hashcode += TREE_HASH (purpose);
1965 else
1966 hashcode += 1009;
1967 return hashcode;
1970 /* Hash an already existing TREE_LIST. */
1972 hashval_t
1973 list_hasher::hash (tree t)
1975 return list_hash_pieces (TREE_PURPOSE (t),
1976 TREE_VALUE (t),
1977 TREE_CHAIN (t));
1980 /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
1981 object for an identical list if one already exists. Otherwise, build a
1982 new one, and record it as the canonical object. */
1984 tree
1985 hash_tree_cons (tree purpose, tree value, tree chain)
1987 int hashcode = 0;
1988 tree *slot;
1989 struct list_proxy proxy;
1991 /* Hash the list node. */
1992 hashcode = list_hash_pieces (purpose, value, chain);
1993 /* Create a proxy for the TREE_LIST we would like to create. We
1994 don't actually create it so as to avoid creating garbage. */
1995 proxy.purpose = purpose;
1996 proxy.value = value;
1997 proxy.chain = chain;
1998 /* See if it is already in the table. */
1999 slot = list_hash_table->find_slot_with_hash (&proxy, hashcode, INSERT);
2000 /* If not, create a new node. */
2001 if (!*slot)
2002 *slot = tree_cons (purpose, value, chain);
2003 return (tree) *slot;
2006 /* Constructor for hashed lists. */
2008 tree
2009 hash_tree_chain (tree value, tree chain)
2011 return hash_tree_cons (NULL_TREE, value, chain);
2014 void
2015 debug_binfo (tree elem)
2017 HOST_WIDE_INT n;
2018 tree virtuals;
2020 fprintf (stderr, "type \"%s\", offset = " HOST_WIDE_INT_PRINT_DEC
2021 "\nvtable type:\n",
2022 TYPE_NAME_STRING (BINFO_TYPE (elem)),
2023 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
2024 debug_tree (BINFO_TYPE (elem));
2025 if (BINFO_VTABLE (elem))
2026 fprintf (stderr, "vtable decl \"%s\"\n",
2027 IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem))));
2028 else
2029 fprintf (stderr, "no vtable decl yet\n");
2030 fprintf (stderr, "virtuals:\n");
2031 virtuals = BINFO_VIRTUALS (elem);
2032 n = 0;
2034 while (virtuals)
2036 tree fndecl = TREE_VALUE (virtuals);
2037 fprintf (stderr, "%s [%ld =? %ld]\n",
2038 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
2039 (long) n, (long) TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
2040 ++n;
2041 virtuals = TREE_CHAIN (virtuals);
2045 /* Build a representation for the qualified name SCOPE::NAME. TYPE is
2046 the type of the result expression, if known, or NULL_TREE if the
2047 resulting expression is type-dependent. If TEMPLATE_P is true,
2048 NAME is known to be a template because the user explicitly used the
2049 "template" keyword after the "::".
2051 All SCOPE_REFs should be built by use of this function. */
2053 tree
2054 build_qualified_name (tree type, tree scope, tree name, bool template_p)
2056 tree t;
2057 if (type == error_mark_node
2058 || scope == error_mark_node
2059 || name == error_mark_node)
2060 return error_mark_node;
2061 gcc_assert (TREE_CODE (name) != SCOPE_REF);
2062 t = build2 (SCOPE_REF, type, scope, name);
2063 QUALIFIED_NAME_IS_TEMPLATE (t) = template_p;
2064 PTRMEM_OK_P (t) = true;
2065 if (type)
2066 t = convert_from_reference (t);
2067 return t;
2070 /* Like check_qualified_type, but also check ref-qualifier and exception
2071 specification. */
2073 static bool
2074 cp_check_qualified_type (const_tree cand, const_tree base, int type_quals,
2075 cp_ref_qualifier rqual, tree raises)
2077 return (TYPE_QUALS (cand) == type_quals
2078 && check_base_type (cand, base)
2079 && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (cand),
2080 ce_exact)
2081 && type_memfn_rqual (cand) == rqual);
2084 /* Build the FUNCTION_TYPE or METHOD_TYPE with the ref-qualifier RQUAL. */
2086 tree
2087 build_ref_qualified_type (tree type, cp_ref_qualifier rqual)
2089 tree t;
2091 if (rqual == type_memfn_rqual (type))
2092 return type;
2094 int type_quals = TYPE_QUALS (type);
2095 tree raises = TYPE_RAISES_EXCEPTIONS (type);
2096 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2097 if (cp_check_qualified_type (t, type, type_quals, rqual, raises))
2098 return t;
2100 t = build_variant_type_copy (type);
2101 switch (rqual)
2103 case REF_QUAL_RVALUE:
2104 FUNCTION_RVALUE_QUALIFIED (t) = 1;
2105 FUNCTION_REF_QUALIFIED (t) = 1;
2106 break;
2107 case REF_QUAL_LVALUE:
2108 FUNCTION_RVALUE_QUALIFIED (t) = 0;
2109 FUNCTION_REF_QUALIFIED (t) = 1;
2110 break;
2111 default:
2112 FUNCTION_REF_QUALIFIED (t) = 0;
2113 break;
2116 if (TYPE_STRUCTURAL_EQUALITY_P (type))
2117 /* Propagate structural equality. */
2118 SET_TYPE_STRUCTURAL_EQUALITY (t);
2119 else if (TYPE_CANONICAL (type) != type)
2120 /* Build the underlying canonical type, since it is different
2121 from TYPE. */
2122 TYPE_CANONICAL (t) = build_ref_qualified_type (TYPE_CANONICAL (type),
2123 rqual);
2124 else
2125 /* T is its own canonical type. */
2126 TYPE_CANONICAL (t) = t;
2128 return t;
2131 /* Cache of free ovl nodes. Uses OVL_FUNCTION for chaining. */
2132 static GTY((deletable)) tree ovl_cache;
2134 /* Make a raw overload node containing FN. */
2136 tree
2137 ovl_make (tree fn, tree next)
2139 tree result = ovl_cache;
2141 if (result)
2143 ovl_cache = OVL_FUNCTION (result);
2144 /* Zap the flags. */
2145 memset (result, 0, sizeof (tree_base));
2146 TREE_SET_CODE (result, OVERLOAD);
2148 else
2149 result = make_node (OVERLOAD);
2151 if (TREE_CODE (fn) == OVERLOAD)
2152 OVL_NESTED_P (result) = true;
2154 TREE_TYPE (result) = (next || TREE_CODE (fn) == TEMPLATE_DECL
2155 ? unknown_type_node : TREE_TYPE (fn));
2156 OVL_FUNCTION (result) = fn;
2157 OVL_CHAIN (result) = next;
2158 return result;
2161 static tree
2162 ovl_copy (tree ovl)
2164 tree result = ovl_cache;
2166 if (result)
2168 ovl_cache = OVL_FUNCTION (result);
2169 /* Zap the flags. */
2170 memset (result, 0, sizeof (tree_base));
2171 TREE_SET_CODE (result, OVERLOAD);
2173 else
2174 result = make_node (OVERLOAD);
2176 gcc_checking_assert (!OVL_NESTED_P (ovl) && OVL_USED_P (ovl));
2177 TREE_TYPE (result) = TREE_TYPE (ovl);
2178 OVL_FUNCTION (result) = OVL_FUNCTION (ovl);
2179 OVL_CHAIN (result) = OVL_CHAIN (ovl);
2180 OVL_HIDDEN_P (result) = OVL_HIDDEN_P (ovl);
2181 OVL_USING_P (result) = OVL_USING_P (ovl);
2182 OVL_LOOKUP_P (result) = OVL_LOOKUP_P (ovl);
2184 return result;
2187 /* Add FN to the (potentially NULL) overload set OVL. USING_P is
2188 true, if FN is via a using declaration. We also pay attention to
2189 DECL_HIDDEN. Overloads are ordered as hidden, using, regular. */
2191 tree
2192 ovl_insert (tree fn, tree maybe_ovl, bool using_p)
2194 bool copying = false; /* Checking use only. */
2195 bool hidden_p = DECL_HIDDEN_P (fn);
2196 int weight = (hidden_p << 1) | (using_p << 0);
2198 tree result = NULL_TREE;
2199 tree insert_after = NULL_TREE;
2201 /* Find insertion point. */
2202 while (maybe_ovl && TREE_CODE (maybe_ovl) == OVERLOAD
2203 && (weight < ((OVL_HIDDEN_P (maybe_ovl) << 1)
2204 | (OVL_USING_P (maybe_ovl) << 0))))
2206 gcc_checking_assert (!OVL_LOOKUP_P (maybe_ovl)
2207 && (!copying || OVL_USED_P (maybe_ovl)));
2208 if (OVL_USED_P (maybe_ovl))
2210 copying = true;
2211 maybe_ovl = ovl_copy (maybe_ovl);
2212 if (insert_after)
2213 OVL_CHAIN (insert_after) = maybe_ovl;
2215 if (!result)
2216 result = maybe_ovl;
2217 insert_after = maybe_ovl;
2218 maybe_ovl = OVL_CHAIN (maybe_ovl);
2221 tree trail = fn;
2222 if (maybe_ovl || using_p || hidden_p || TREE_CODE (fn) == TEMPLATE_DECL)
2224 trail = ovl_make (fn, maybe_ovl);
2225 if (hidden_p)
2226 OVL_HIDDEN_P (trail) = true;
2227 if (using_p)
2228 OVL_USING_P (trail) = true;
2231 if (insert_after)
2233 OVL_CHAIN (insert_after) = trail;
2234 TREE_TYPE (insert_after) = unknown_type_node;
2236 else
2237 result = trail;
2239 return result;
2242 /* Skip any hidden names at the beginning of OVL. */
2244 tree
2245 ovl_skip_hidden (tree ovl)
2247 for (;
2248 ovl && TREE_CODE (ovl) == OVERLOAD && OVL_HIDDEN_P (ovl);
2249 ovl = OVL_CHAIN (ovl))
2250 gcc_checking_assert (DECL_HIDDEN_P (OVL_FUNCTION (ovl)));
2252 if (ovl && TREE_CODE (ovl) != OVERLOAD && DECL_HIDDEN_P (ovl))
2254 /* Any hidden functions should have been wrapped in an
2255 overload, but injected friend classes will not. */
2256 gcc_checking_assert (!DECL_DECLARES_FUNCTION_P (ovl));
2257 ovl = NULL_TREE;
2260 return ovl;
2263 /* NODE is an OVL_HIDDEN_P node which is now revealed. */
2265 tree
2266 ovl_iterator::reveal_node (tree overload, tree node)
2268 /* We cannot have returned NODE as part of a lookup overload, so it
2269 cannot be USED. */
2270 gcc_checking_assert (!OVL_USED_P (node));
2272 OVL_HIDDEN_P (node) = false;
2273 if (tree chain = OVL_CHAIN (node))
2274 if (TREE_CODE (chain) == OVERLOAD
2275 && (OVL_USING_P (chain) || OVL_HIDDEN_P (chain)))
2277 /* The node needs moving, and the simplest way is to remove it
2278 and reinsert. */
2279 overload = remove_node (overload, node);
2280 overload = ovl_insert (OVL_FUNCTION (node), overload);
2282 return overload;
2285 /* NODE is on the overloads of OVL. Remove it. If a predecessor is
2286 OVL_USED_P we must copy OVL nodes, because those are immutable.
2287 The removed node is unaltered and may continue to be iterated
2288 from (i.e. it is safe to remove a node from an overload one is
2289 currently iterating over). */
2291 tree
2292 ovl_iterator::remove_node (tree overload, tree node)
2294 bool copying = false; /* Checking use only. */
2296 tree *slot = &overload;
2297 while (*slot != node)
2299 tree probe = *slot;
2300 gcc_checking_assert (!OVL_LOOKUP_P (probe)
2301 && (!copying || OVL_USED_P (probe)));
2302 if (OVL_USED_P (probe))
2304 copying = true;
2305 probe = ovl_copy (probe);
2306 *slot = probe;
2309 slot = &OVL_CHAIN (probe);
2312 /* Stitch out NODE. We don't have to worry about now making a
2313 singleton overload (and consequently maybe setting its type),
2314 because all uses of this function will be followed by inserting a
2315 new node that must follow the place we've cut this out from. */
2316 if (TREE_CODE (node) != OVERLOAD)
2317 /* Cloned inherited ctors don't mark themselves as via_using. */
2318 *slot = NULL_TREE;
2319 else
2320 *slot = OVL_CHAIN (node);
2322 return overload;
2325 /* Mark or unmark a lookup set. */
2327 void
2328 lookup_mark (tree ovl, bool val)
2330 for (lkp_iterator iter (ovl); iter; ++iter)
2332 gcc_checking_assert (LOOKUP_SEEN_P (*iter) != val);
2333 LOOKUP_SEEN_P (*iter) = val;
2337 /* Add a set of new FNS into a lookup. */
2339 tree
2340 lookup_add (tree fns, tree lookup)
2342 if (lookup || TREE_CODE (fns) == TEMPLATE_DECL)
2344 lookup = ovl_make (fns, lookup);
2345 OVL_LOOKUP_P (lookup) = true;
2347 else
2348 lookup = fns;
2350 return lookup;
2353 /* FNS is a new overload set, add them to LOOKUP, if they are not
2354 already present there. */
2356 tree
2357 lookup_maybe_add (tree fns, tree lookup, bool deduping)
2359 if (deduping)
2360 for (tree next, probe = fns; probe; probe = next)
2362 tree fn = probe;
2363 next = NULL_TREE;
2365 if (TREE_CODE (probe) == OVERLOAD)
2367 fn = OVL_FUNCTION (probe);
2368 next = OVL_CHAIN (probe);
2371 if (!LOOKUP_SEEN_P (fn))
2372 LOOKUP_SEEN_P (fn) = true;
2373 else
2375 /* This function was already seen. Insert all the
2376 predecessors onto the lookup. */
2377 for (; fns != probe; fns = OVL_CHAIN (fns))
2379 lookup = lookup_add (OVL_FUNCTION (fns), lookup);
2380 /* Propagate OVL_USING, but OVL_HIDDEN doesn't matter. */
2381 if (OVL_USING_P (fns))
2382 OVL_USING_P (lookup) = true;
2385 /* And now skip this function. */
2386 fns = next;
2390 if (fns)
2391 /* We ended in a set of new functions. Add them all in one go. */
2392 lookup = lookup_add (fns, lookup);
2394 return lookup;
2397 /* Regular overload OVL is part of a kept lookup. Mark the nodes on
2398 it as immutable. */
2400 static void
2401 ovl_used (tree ovl)
2403 for (;
2404 ovl && TREE_CODE (ovl) == OVERLOAD
2405 && !OVL_USED_P (ovl);
2406 ovl = OVL_CHAIN (ovl))
2408 gcc_checking_assert (!OVL_LOOKUP_P (ovl));
2409 OVL_USED_P (ovl) = true;
2413 /* If KEEP is true, preserve the contents of a lookup so that it is
2414 available for a later instantiation. Otherwise release the LOOKUP
2415 nodes for reuse. */
2417 void
2418 lookup_keep (tree lookup, bool keep)
2420 for (;
2421 lookup && TREE_CODE (lookup) == OVERLOAD
2422 && OVL_LOOKUP_P (lookup) && !OVL_USED_P (lookup);
2423 lookup = OVL_CHAIN (lookup))
2424 if (keep)
2426 OVL_USED_P (lookup) = true;
2427 ovl_used (OVL_FUNCTION (lookup));
2429 else
2431 OVL_FUNCTION (lookup) = ovl_cache;
2432 ovl_cache = lookup;
2435 if (keep)
2436 ovl_used (lookup);
2439 /* Returns nonzero if X is an expression for a (possibly overloaded)
2440 function. If "f" is a function or function template, "f", "c->f",
2441 "c.f", "C::f", and "f<int>" will all be considered possibly
2442 overloaded functions. Returns 2 if the function is actually
2443 overloaded, i.e., if it is impossible to know the type of the
2444 function without performing overload resolution. */
2447 is_overloaded_fn (tree x)
2449 /* A baselink is also considered an overloaded function. */
2450 if (TREE_CODE (x) == OFFSET_REF
2451 || TREE_CODE (x) == COMPONENT_REF)
2452 x = TREE_OPERAND (x, 1);
2453 x = MAYBE_BASELINK_FUNCTIONS (x);
2454 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
2455 x = TREE_OPERAND (x, 0);
2457 if (DECL_FUNCTION_TEMPLATE_P (OVL_FIRST (x))
2458 || (TREE_CODE (x) == OVERLOAD && !OVL_SINGLE_P (x)))
2459 return 2;
2461 return (TREE_CODE (x) == FUNCTION_DECL
2462 || TREE_CODE (x) == OVERLOAD);
2465 /* X is the CALL_EXPR_FN of a CALL_EXPR. If X represents a dependent name
2466 (14.6.2), return the IDENTIFIER_NODE for that name. Otherwise, return
2467 NULL_TREE. */
2469 tree
2470 dependent_name (tree x)
2472 if (identifier_p (x))
2473 return x;
2474 if (TREE_CODE (x) == TEMPLATE_ID_EXPR)
2475 x = TREE_OPERAND (x, 0);
2476 if (TREE_CODE (x) == OVERLOAD || TREE_CODE (x) == FUNCTION_DECL)
2477 return OVL_NAME (x);
2478 return NULL_TREE;
2481 /* Returns true iff X is an expression for an overloaded function
2482 whose type cannot be known without performing overload
2483 resolution. */
2485 bool
2486 really_overloaded_fn (tree x)
2488 return is_overloaded_fn (x) == 2;
2491 /* Get the overload set FROM refers to. */
2493 tree
2494 get_fns (tree from)
2496 /* A baselink is also considered an overloaded function. */
2497 if (TREE_CODE (from) == OFFSET_REF
2498 || TREE_CODE (from) == COMPONENT_REF)
2499 from = TREE_OPERAND (from, 1);
2500 if (BASELINK_P (from))
2501 from = BASELINK_FUNCTIONS (from);
2502 if (TREE_CODE (from) == TEMPLATE_ID_EXPR)
2503 from = TREE_OPERAND (from, 0);
2504 gcc_assert (TREE_CODE (from) == OVERLOAD
2505 || TREE_CODE (from) == FUNCTION_DECL);
2506 return from;
2509 /* Return the first function of the overload set FROM refers to. */
2511 tree
2512 get_first_fn (tree from)
2514 return OVL_FIRST (get_fns (from));
2517 /* Return the scope where the overloaded functions OVL were found. */
2519 tree
2520 ovl_scope (tree ovl)
2522 if (TREE_CODE (ovl) == OFFSET_REF
2523 || TREE_CODE (ovl) == COMPONENT_REF)
2524 ovl = TREE_OPERAND (ovl, 1);
2525 if (TREE_CODE (ovl) == BASELINK)
2526 return BINFO_TYPE (BASELINK_BINFO (ovl));
2527 if (TREE_CODE (ovl) == TEMPLATE_ID_EXPR)
2528 ovl = TREE_OPERAND (ovl, 0);
2529 /* Skip using-declarations. */
2530 lkp_iterator iter (ovl);
2532 ovl = *iter;
2533 while (iter.using_p () && ++iter);
2535 return CP_DECL_CONTEXT (ovl);
2538 #define PRINT_RING_SIZE 4
2540 static const char *
2541 cxx_printable_name_internal (tree decl, int v, bool translate)
2543 static unsigned int uid_ring[PRINT_RING_SIZE];
2544 static char *print_ring[PRINT_RING_SIZE];
2545 static bool trans_ring[PRINT_RING_SIZE];
2546 static int ring_counter;
2547 int i;
2549 /* Only cache functions. */
2550 if (v < 2
2551 || TREE_CODE (decl) != FUNCTION_DECL
2552 || DECL_LANG_SPECIFIC (decl) == 0)
2553 return lang_decl_name (decl, v, translate);
2555 /* See if this print name is lying around. */
2556 for (i = 0; i < PRINT_RING_SIZE; i++)
2557 if (uid_ring[i] == DECL_UID (decl) && translate == trans_ring[i])
2558 /* yes, so return it. */
2559 return print_ring[i];
2561 if (++ring_counter == PRINT_RING_SIZE)
2562 ring_counter = 0;
2564 if (current_function_decl != NULL_TREE)
2566 /* There may be both translated and untranslated versions of the
2567 name cached. */
2568 for (i = 0; i < 2; i++)
2570 if (uid_ring[ring_counter] == DECL_UID (current_function_decl))
2571 ring_counter += 1;
2572 if (ring_counter == PRINT_RING_SIZE)
2573 ring_counter = 0;
2575 gcc_assert (uid_ring[ring_counter] != DECL_UID (current_function_decl));
2578 free (print_ring[ring_counter]);
2580 print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v, translate));
2581 uid_ring[ring_counter] = DECL_UID (decl);
2582 trans_ring[ring_counter] = translate;
2583 return print_ring[ring_counter];
2586 const char *
2587 cxx_printable_name (tree decl, int v)
2589 return cxx_printable_name_internal (decl, v, false);
2592 const char *
2593 cxx_printable_name_translate (tree decl, int v)
2595 return cxx_printable_name_internal (decl, v, true);
2598 /* Return the canonical version of exception-specification RAISES for a C++17
2599 function type, for use in type comparison and building TYPE_CANONICAL. */
2601 tree
2602 canonical_eh_spec (tree raises)
2604 if (raises == NULL_TREE)
2605 return raises;
2606 else if (DEFERRED_NOEXCEPT_SPEC_P (raises)
2607 || uses_template_parms (raises)
2608 || uses_template_parms (TREE_PURPOSE (raises)))
2609 /* Keep a dependent or deferred exception specification. */
2610 return raises;
2611 else if (nothrow_spec_p (raises))
2612 /* throw() -> noexcept. */
2613 return noexcept_true_spec;
2614 else
2615 /* For C++17 type matching, anything else -> nothing. */
2616 return NULL_TREE;
2619 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
2620 listed in RAISES. */
2622 tree
2623 build_exception_variant (tree type, tree raises)
2625 tree v;
2626 int type_quals;
2628 if (comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (type), ce_exact))
2629 return type;
2631 type_quals = TYPE_QUALS (type);
2632 cp_ref_qualifier rqual = type_memfn_rqual (type);
2633 for (v = TYPE_MAIN_VARIANT (type); v; v = TYPE_NEXT_VARIANT (v))
2634 if (cp_check_qualified_type (v, type, type_quals, rqual, raises))
2635 return v;
2637 /* Need to build a new variant. */
2638 v = build_variant_type_copy (type);
2639 TYPE_RAISES_EXCEPTIONS (v) = raises;
2641 if (!flag_noexcept_type)
2642 /* The exception-specification is not part of the canonical type. */
2643 return v;
2645 /* Canonicalize the exception specification. */
2646 tree cr = canonical_eh_spec (raises);
2648 if (TYPE_STRUCTURAL_EQUALITY_P (type))
2649 /* Propagate structural equality. */
2650 SET_TYPE_STRUCTURAL_EQUALITY (v);
2651 else if (TYPE_CANONICAL (type) != type || cr != raises)
2652 /* Build the underlying canonical type, since it is different
2653 from TYPE. */
2654 TYPE_CANONICAL (v) = build_exception_variant (TYPE_CANONICAL (type), cr);
2655 else
2656 /* T is its own canonical type. */
2657 TYPE_CANONICAL (v) = v;
2659 return v;
2662 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new
2663 BOUND_TEMPLATE_TEMPLATE_PARM bound with NEWARGS as its template
2664 arguments. */
2666 tree
2667 bind_template_template_parm (tree t, tree newargs)
2669 tree decl = TYPE_NAME (t);
2670 tree t2;
2672 t2 = cxx_make_type (BOUND_TEMPLATE_TEMPLATE_PARM);
2673 decl = build_decl (input_location,
2674 TYPE_DECL, DECL_NAME (decl), NULL_TREE);
2676 /* These nodes have to be created to reflect new TYPE_DECL and template
2677 arguments. */
2678 TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t));
2679 TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl;
2680 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
2681 = build_template_info (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t), newargs);
2683 TREE_TYPE (decl) = t2;
2684 TYPE_NAME (t2) = decl;
2685 TYPE_STUB_DECL (t2) = decl;
2686 TYPE_SIZE (t2) = 0;
2687 SET_TYPE_STRUCTURAL_EQUALITY (t2);
2689 return t2;
2692 /* Called from count_trees via walk_tree. */
2694 static tree
2695 count_trees_r (tree *tp, int *walk_subtrees, void *data)
2697 ++*((int *) data);
2699 if (TYPE_P (*tp))
2700 *walk_subtrees = 0;
2702 return NULL_TREE;
2705 /* Debugging function for measuring the rough complexity of a tree
2706 representation. */
2709 count_trees (tree t)
2711 int n_trees = 0;
2712 cp_walk_tree_without_duplicates (&t, count_trees_r, &n_trees);
2713 return n_trees;
2716 /* Called from verify_stmt_tree via walk_tree. */
2718 static tree
2719 verify_stmt_tree_r (tree* tp, int * /*walk_subtrees*/, void* data)
2721 tree t = *tp;
2722 hash_table<nofree_ptr_hash <tree_node> > *statements
2723 = static_cast <hash_table<nofree_ptr_hash <tree_node> > *> (data);
2724 tree_node **slot;
2726 if (!STATEMENT_CODE_P (TREE_CODE (t)))
2727 return NULL_TREE;
2729 /* If this statement is already present in the hash table, then
2730 there is a circularity in the statement tree. */
2731 gcc_assert (!statements->find (t));
2733 slot = statements->find_slot (t, INSERT);
2734 *slot = t;
2736 return NULL_TREE;
2739 /* Debugging function to check that the statement T has not been
2740 corrupted. For now, this function simply checks that T contains no
2741 circularities. */
2743 void
2744 verify_stmt_tree (tree t)
2746 hash_table<nofree_ptr_hash <tree_node> > statements (37);
2747 cp_walk_tree (&t, verify_stmt_tree_r, &statements, NULL);
2750 /* Check if the type T depends on a type with no linkage and if so, return
2751 it. If RELAXED_P then do not consider a class type declared within
2752 a vague-linkage function to have no linkage. */
2754 tree
2755 no_linkage_check (tree t, bool relaxed_p)
2757 tree r;
2759 /* There's no point in checking linkage on template functions; we
2760 can't know their complete types. */
2761 if (processing_template_decl)
2762 return NULL_TREE;
2764 switch (TREE_CODE (t))
2766 case RECORD_TYPE:
2767 if (TYPE_PTRMEMFUNC_P (t))
2768 goto ptrmem;
2769 /* Lambda types that don't have mangling scope have no linkage. We
2770 check CLASSTYPE_LAMBDA_EXPR for error_mark_node because
2771 when we get here from pushtag none of the lambda information is
2772 set up yet, so we want to assume that the lambda has linkage and
2773 fix it up later if not. */
2774 if (CLASSTYPE_LAMBDA_EXPR (t)
2775 && CLASSTYPE_LAMBDA_EXPR (t) != error_mark_node
2776 && LAMBDA_TYPE_EXTRA_SCOPE (t) == NULL_TREE)
2777 return t;
2778 /* Fall through. */
2779 case UNION_TYPE:
2780 if (!CLASS_TYPE_P (t))
2781 return NULL_TREE;
2782 /* Fall through. */
2783 case ENUMERAL_TYPE:
2784 /* Only treat unnamed types as having no linkage if they're at
2785 namespace scope. This is core issue 966. */
2786 if (TYPE_UNNAMED_P (t) && TYPE_NAMESPACE_SCOPE_P (t))
2787 return t;
2789 for (r = CP_TYPE_CONTEXT (t); ; )
2791 /* If we're a nested type of a !TREE_PUBLIC class, we might not
2792 have linkage, or we might just be in an anonymous namespace.
2793 If we're in a TREE_PUBLIC class, we have linkage. */
2794 if (TYPE_P (r) && !TREE_PUBLIC (TYPE_NAME (r)))
2795 return no_linkage_check (TYPE_CONTEXT (t), relaxed_p);
2796 else if (TREE_CODE (r) == FUNCTION_DECL)
2798 if (!relaxed_p || !vague_linkage_p (r))
2799 return t;
2800 else
2801 r = CP_DECL_CONTEXT (r);
2803 else
2804 break;
2807 return NULL_TREE;
2809 case ARRAY_TYPE:
2810 case POINTER_TYPE:
2811 case REFERENCE_TYPE:
2812 case VECTOR_TYPE:
2813 return no_linkage_check (TREE_TYPE (t), relaxed_p);
2815 case OFFSET_TYPE:
2816 ptrmem:
2817 r = no_linkage_check (TYPE_PTRMEM_POINTED_TO_TYPE (t),
2818 relaxed_p);
2819 if (r)
2820 return r;
2821 return no_linkage_check (TYPE_PTRMEM_CLASS_TYPE (t), relaxed_p);
2823 case METHOD_TYPE:
2824 case FUNCTION_TYPE:
2826 tree parm = TYPE_ARG_TYPES (t);
2827 if (TREE_CODE (t) == METHOD_TYPE)
2828 /* The 'this' pointer isn't interesting; a method has the same
2829 linkage (or lack thereof) as its enclosing class. */
2830 parm = TREE_CHAIN (parm);
2831 for (;
2832 parm && parm != void_list_node;
2833 parm = TREE_CHAIN (parm))
2835 r = no_linkage_check (TREE_VALUE (parm), relaxed_p);
2836 if (r)
2837 return r;
2839 return no_linkage_check (TREE_TYPE (t), relaxed_p);
2842 default:
2843 return NULL_TREE;
2847 extern int depth_reached;
2849 void
2850 cxx_print_statistics (void)
2852 print_template_statistics ();
2853 if (GATHER_STATISTICS)
2854 fprintf (stderr, "maximum template instantiation depth reached: %d\n",
2855 depth_reached);
2858 /* Return, as an INTEGER_CST node, the number of elements for TYPE
2859 (which is an ARRAY_TYPE). This counts only elements of the top
2860 array. */
2862 tree
2863 array_type_nelts_top (tree type)
2865 return fold_build2_loc (input_location,
2866 PLUS_EXPR, sizetype,
2867 array_type_nelts (type),
2868 size_one_node);
2871 /* Return, as an INTEGER_CST node, the number of elements for TYPE
2872 (which is an ARRAY_TYPE). This one is a recursive count of all
2873 ARRAY_TYPEs that are clumped together. */
2875 tree
2876 array_type_nelts_total (tree type)
2878 tree sz = array_type_nelts_top (type);
2879 type = TREE_TYPE (type);
2880 while (TREE_CODE (type) == ARRAY_TYPE)
2882 tree n = array_type_nelts_top (type);
2883 sz = fold_build2_loc (input_location,
2884 MULT_EXPR, sizetype, sz, n);
2885 type = TREE_TYPE (type);
2887 return sz;
2890 /* Called from break_out_target_exprs via mapcar. */
2892 static tree
2893 bot_manip (tree* tp, int* walk_subtrees, void* data)
2895 splay_tree target_remap = ((splay_tree) data);
2896 tree t = *tp;
2898 if (!TYPE_P (t) && TREE_CONSTANT (t) && !TREE_SIDE_EFFECTS (t))
2900 /* There can't be any TARGET_EXPRs or their slot variables below this
2901 point. But we must make a copy, in case subsequent processing
2902 alters any part of it. For example, during gimplification a cast
2903 of the form (T) &X::f (where "f" is a member function) will lead
2904 to replacing the PTRMEM_CST for &X::f with a VAR_DECL. */
2905 *walk_subtrees = 0;
2906 *tp = unshare_expr (t);
2907 return NULL_TREE;
2909 if (TREE_CODE (t) == TARGET_EXPR)
2911 tree u;
2913 if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
2915 u = build_cplus_new (TREE_TYPE (t), TREE_OPERAND (t, 1),
2916 tf_warning_or_error);
2917 if (u == error_mark_node)
2918 return u;
2919 if (AGGR_INIT_ZERO_FIRST (TREE_OPERAND (t, 1)))
2920 AGGR_INIT_ZERO_FIRST (TREE_OPERAND (u, 1)) = true;
2922 else
2923 u = build_target_expr_with_type (TREE_OPERAND (t, 1), TREE_TYPE (t),
2924 tf_warning_or_error);
2926 TARGET_EXPR_IMPLICIT_P (u) = TARGET_EXPR_IMPLICIT_P (t);
2927 TARGET_EXPR_LIST_INIT_P (u) = TARGET_EXPR_LIST_INIT_P (t);
2928 TARGET_EXPR_DIRECT_INIT_P (u) = TARGET_EXPR_DIRECT_INIT_P (t);
2930 /* Map the old variable to the new one. */
2931 splay_tree_insert (target_remap,
2932 (splay_tree_key) TREE_OPERAND (t, 0),
2933 (splay_tree_value) TREE_OPERAND (u, 0));
2935 TREE_OPERAND (u, 1) = break_out_target_exprs (TREE_OPERAND (u, 1));
2936 if (TREE_OPERAND (u, 1) == error_mark_node)
2937 return error_mark_node;
2939 /* Replace the old expression with the new version. */
2940 *tp = u;
2941 /* We don't have to go below this point; the recursive call to
2942 break_out_target_exprs will have handled anything below this
2943 point. */
2944 *walk_subtrees = 0;
2945 return NULL_TREE;
2947 if (TREE_CODE (*tp) == SAVE_EXPR)
2949 t = *tp;
2950 splay_tree_node n = splay_tree_lookup (target_remap,
2951 (splay_tree_key) t);
2952 if (n)
2954 *tp = (tree)n->value;
2955 *walk_subtrees = 0;
2957 else
2959 copy_tree_r (tp, walk_subtrees, NULL);
2960 splay_tree_insert (target_remap,
2961 (splay_tree_key)t,
2962 (splay_tree_value)*tp);
2963 /* Make sure we don't remap an already-remapped SAVE_EXPR. */
2964 splay_tree_insert (target_remap,
2965 (splay_tree_key)*tp,
2966 (splay_tree_value)*tp);
2968 return NULL_TREE;
2971 /* Make a copy of this node. */
2972 t = copy_tree_r (tp, walk_subtrees, NULL);
2973 if (TREE_CODE (*tp) == CALL_EXPR)
2975 set_flags_from_callee (*tp);
2977 /* builtin_LINE and builtin_FILE get the location where the default
2978 argument is expanded, not where the call was written. */
2979 tree callee = get_callee_fndecl (*tp);
2980 if (callee && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
2981 switch (DECL_FUNCTION_CODE (callee))
2983 case BUILT_IN_FILE:
2984 case BUILT_IN_LINE:
2985 SET_EXPR_LOCATION (*tp, input_location);
2986 default:
2987 break;
2990 return t;
2993 /* Replace all remapped VAR_DECLs in T with their new equivalents.
2994 DATA is really a splay-tree mapping old variables to new
2995 variables. */
2997 static tree
2998 bot_replace (tree* t, int* /*walk_subtrees*/, void* data)
3000 splay_tree target_remap = ((splay_tree) data);
3002 if (VAR_P (*t))
3004 splay_tree_node n = splay_tree_lookup (target_remap,
3005 (splay_tree_key) *t);
3006 if (n)
3007 *t = (tree) n->value;
3009 else if (TREE_CODE (*t) == PARM_DECL
3010 && DECL_NAME (*t) == this_identifier
3011 && !DECL_CONTEXT (*t))
3013 /* In an NSDMI we need to replace the 'this' parameter we used for
3014 parsing with the real one for this function. */
3015 *t = current_class_ptr;
3017 else if (TREE_CODE (*t) == CONVERT_EXPR
3018 && CONVERT_EXPR_VBASE_PATH (*t))
3020 /* In an NSDMI build_base_path defers building conversions to virtual
3021 bases, and we handle it here. */
3022 tree basetype = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (*t)));
3023 vec<tree, va_gc> *vbases = CLASSTYPE_VBASECLASSES (current_class_type);
3024 int i; tree binfo;
3025 FOR_EACH_VEC_SAFE_ELT (vbases, i, binfo)
3026 if (BINFO_TYPE (binfo) == basetype)
3027 break;
3028 *t = build_base_path (PLUS_EXPR, TREE_OPERAND (*t, 0), binfo, true,
3029 tf_warning_or_error);
3032 return NULL_TREE;
3035 /* When we parse a default argument expression, we may create
3036 temporary variables via TARGET_EXPRs. When we actually use the
3037 default-argument expression, we make a copy of the expression
3038 and replace the temporaries with appropriate local versions. */
3040 tree
3041 break_out_target_exprs (tree t)
3043 static int target_remap_count;
3044 static splay_tree target_remap;
3046 if (!target_remap_count++)
3047 target_remap = splay_tree_new (splay_tree_compare_pointers,
3048 /*splay_tree_delete_key_fn=*/NULL,
3049 /*splay_tree_delete_value_fn=*/NULL);
3050 if (cp_walk_tree (&t, bot_manip, target_remap, NULL) == error_mark_node)
3051 t = error_mark_node;
3052 cp_walk_tree (&t, bot_replace, target_remap, NULL);
3054 if (!--target_remap_count)
3056 splay_tree_delete (target_remap);
3057 target_remap = NULL;
3060 return t;
3063 /* Build an expression for the subobject of OBJ at CONSTRUCTOR index INDEX,
3064 which we expect to have type TYPE. */
3066 tree
3067 build_ctor_subob_ref (tree index, tree type, tree obj)
3069 if (index == NULL_TREE)
3070 /* Can't refer to a particular member of a vector. */
3071 obj = NULL_TREE;
3072 else if (TREE_CODE (index) == INTEGER_CST)
3073 obj = cp_build_array_ref (input_location, obj, index, tf_none);
3074 else
3075 obj = build_class_member_access_expr (obj, index, NULL_TREE,
3076 /*reference*/false, tf_none);
3077 if (obj)
3079 tree objtype = TREE_TYPE (obj);
3080 if (TREE_CODE (objtype) == ARRAY_TYPE && !TYPE_DOMAIN (objtype))
3082 /* When the destination object refers to a flexible array member
3083 verify that it matches the type of the source object except
3084 for its domain and qualifiers. */
3085 gcc_assert (comptypes (TYPE_MAIN_VARIANT (type),
3086 TYPE_MAIN_VARIANT (objtype),
3087 COMPARE_REDECLARATION));
3089 else
3090 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, objtype));
3093 return obj;
3096 struct replace_placeholders_t
3098 tree obj; /* The object to be substituted for a PLACEHOLDER_EXPR. */
3099 bool seen; /* Whether we've encountered a PLACEHOLDER_EXPR. */
3100 hash_set<tree> *pset; /* To avoid walking same trees multiple times. */
3103 /* Like substitute_placeholder_in_expr, but handle C++ tree codes and
3104 build up subexpressions as we go deeper. */
3106 static tree
3107 replace_placeholders_r (tree* t, int* walk_subtrees, void* data_)
3109 replace_placeholders_t *d = static_cast<replace_placeholders_t*>(data_);
3110 tree obj = d->obj;
3112 if (TYPE_P (*t) || TREE_CONSTANT (*t))
3114 *walk_subtrees = false;
3115 return NULL_TREE;
3118 switch (TREE_CODE (*t))
3120 case PLACEHOLDER_EXPR:
3122 tree x = obj;
3123 for (; !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (*t),
3124 TREE_TYPE (x));
3125 x = TREE_OPERAND (x, 0))
3126 gcc_assert (TREE_CODE (x) == COMPONENT_REF);
3127 *t = x;
3128 *walk_subtrees = false;
3129 d->seen = true;
3131 break;
3133 case CONSTRUCTOR:
3135 constructor_elt *ce;
3136 vec<constructor_elt,va_gc> *v = CONSTRUCTOR_ELTS (*t);
3137 if (d->pset->add (*t))
3139 *walk_subtrees = false;
3140 return NULL_TREE;
3142 for (unsigned i = 0; vec_safe_iterate (v, i, &ce); ++i)
3144 tree *valp = &ce->value;
3145 tree type = TREE_TYPE (*valp);
3146 tree subob = obj;
3148 if (TREE_CODE (*valp) == CONSTRUCTOR
3149 && AGGREGATE_TYPE_P (type))
3151 /* If we're looking at the initializer for OBJ, then build
3152 a sub-object reference. If we're looking at an
3153 initializer for another object, just pass OBJ down. */
3154 if (same_type_ignoring_top_level_qualifiers_p
3155 (TREE_TYPE (*t), TREE_TYPE (obj)))
3156 subob = build_ctor_subob_ref (ce->index, type, obj);
3157 if (TREE_CODE (*valp) == TARGET_EXPR)
3158 valp = &TARGET_EXPR_INITIAL (*valp);
3160 d->obj = subob;
3161 cp_walk_tree (valp, replace_placeholders_r, data_, NULL);
3162 d->obj = obj;
3164 *walk_subtrees = false;
3165 break;
3168 default:
3169 if (d->pset->add (*t))
3170 *walk_subtrees = false;
3171 break;
3174 return NULL_TREE;
3177 /* Replace PLACEHOLDER_EXPRs in EXP with object OBJ. SEEN_P is set if
3178 a PLACEHOLDER_EXPR has been encountered. */
3180 tree
3181 replace_placeholders (tree exp, tree obj, bool *seen_p)
3183 /* This is only relevant for C++14. */
3184 if (cxx_dialect < cxx14)
3185 return exp;
3187 /* If the object isn't a (member of a) class, do nothing. */
3188 tree op0 = obj;
3189 while (TREE_CODE (op0) == COMPONENT_REF)
3190 op0 = TREE_OPERAND (op0, 0);
3191 if (!CLASS_TYPE_P (strip_array_types (TREE_TYPE (op0))))
3192 return exp;
3194 tree *tp = &exp;
3195 hash_set<tree> pset;
3196 replace_placeholders_t data = { obj, false, &pset };
3197 if (TREE_CODE (exp) == TARGET_EXPR)
3198 tp = &TARGET_EXPR_INITIAL (exp);
3199 cp_walk_tree (tp, replace_placeholders_r, &data, NULL);
3200 if (seen_p)
3201 *seen_p = data.seen;
3202 return exp;
3205 /* Similar to `build_nt', but for template definitions of dependent
3206 expressions */
3208 tree
3209 build_min_nt_loc (location_t loc, enum tree_code code, ...)
3211 tree t;
3212 int length;
3213 int i;
3214 va_list p;
3216 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3218 va_start (p, code);
3220 t = make_node (code);
3221 SET_EXPR_LOCATION (t, loc);
3222 length = TREE_CODE_LENGTH (code);
3224 for (i = 0; i < length; i++)
3226 tree x = va_arg (p, tree);
3227 TREE_OPERAND (t, i) = x;
3228 if (x && TREE_CODE (x) == OVERLOAD)
3229 lookup_keep (x, true);
3232 va_end (p);
3233 return t;
3236 /* Similar to `build', but for template definitions. */
3238 tree
3239 build_min (enum tree_code code, tree tt, ...)
3241 tree t;
3242 int length;
3243 int i;
3244 va_list p;
3246 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3248 va_start (p, tt);
3250 t = make_node (code);
3251 length = TREE_CODE_LENGTH (code);
3252 TREE_TYPE (t) = tt;
3254 for (i = 0; i < length; i++)
3256 tree x = va_arg (p, tree);
3257 TREE_OPERAND (t, i) = x;
3258 if (x)
3260 if (!TYPE_P (x) && TREE_SIDE_EFFECTS (x))
3261 TREE_SIDE_EFFECTS (t) = 1;
3262 if (TREE_CODE (x) == OVERLOAD)
3263 lookup_keep (x, true);
3267 va_end (p);
3269 if (code == CAST_EXPR)
3270 /* The single operand is a TREE_LIST, which we have to check. */
3271 for (tree v = TREE_OPERAND (t, 0); v; v = TREE_CHAIN (v))
3272 if (TREE_CODE (TREE_VALUE (v)) == OVERLOAD)
3273 lookup_keep (TREE_VALUE (v), true);
3275 return t;
3278 /* Similar to `build', but for template definitions of non-dependent
3279 expressions. NON_DEP is the non-dependent expression that has been
3280 built. */
3282 tree
3283 build_min_non_dep (enum tree_code code, tree non_dep, ...)
3285 tree t;
3286 int length;
3287 int i;
3288 va_list p;
3290 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
3292 va_start (p, non_dep);
3294 if (REFERENCE_REF_P (non_dep))
3295 non_dep = TREE_OPERAND (non_dep, 0);
3297 t = make_node (code);
3298 length = TREE_CODE_LENGTH (code);
3299 TREE_TYPE (t) = unlowered_expr_type (non_dep);
3300 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
3302 for (i = 0; i < length; i++)
3304 tree x = va_arg (p, tree);
3305 TREE_OPERAND (t, i) = x;
3306 if (x && TREE_CODE (x) == OVERLOAD)
3307 lookup_keep (x, true);
3310 if (code == COMPOUND_EXPR && TREE_CODE (non_dep) != COMPOUND_EXPR)
3311 /* This should not be considered a COMPOUND_EXPR, because it
3312 resolves to an overload. */
3313 COMPOUND_EXPR_OVERLOADED (t) = 1;
3315 va_end (p);
3316 return convert_from_reference (t);
3319 /* Similar to build_min_nt, but call expressions */
3321 tree
3322 build_min_nt_call_vec (tree fn, vec<tree, va_gc> *args)
3324 tree ret, t;
3325 unsigned int ix;
3327 ret = build_vl_exp (CALL_EXPR, vec_safe_length (args) + 3);
3328 CALL_EXPR_FN (ret) = fn;
3329 CALL_EXPR_STATIC_CHAIN (ret) = NULL_TREE;
3330 FOR_EACH_VEC_SAFE_ELT (args, ix, t)
3332 CALL_EXPR_ARG (ret, ix) = t;
3333 if (TREE_CODE (t) == OVERLOAD)
3334 lookup_keep (t, true);
3336 return ret;
3339 /* Similar to `build_min_nt_call_vec', but for template definitions of
3340 non-dependent expressions. NON_DEP is the non-dependent expression
3341 that has been built. */
3343 tree
3344 build_min_non_dep_call_vec (tree non_dep, tree fn, vec<tree, va_gc> *argvec)
3346 tree t = build_min_nt_call_vec (fn, argvec);
3347 if (REFERENCE_REF_P (non_dep))
3348 non_dep = TREE_OPERAND (non_dep, 0);
3349 TREE_TYPE (t) = TREE_TYPE (non_dep);
3350 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
3351 return convert_from_reference (t);
3354 /* Similar to build_min_non_dep, but for expressions that have been resolved to
3355 a call to an operator overload. OP is the operator that has been
3356 overloaded. NON_DEP is the non-dependent expression that's been built,
3357 which should be a CALL_EXPR or an INDIRECT_REF to a CALL_EXPR. OVERLOAD is
3358 the overload that NON_DEP is calling. */
3360 tree
3361 build_min_non_dep_op_overload (enum tree_code op,
3362 tree non_dep,
3363 tree overload, ...)
3365 va_list p;
3366 int nargs, expected_nargs;
3367 tree fn, call;
3368 vec<tree, va_gc> *args;
3370 non_dep = extract_call_expr (non_dep);
3372 nargs = call_expr_nargs (non_dep);
3374 expected_nargs = cp_tree_code_length (op);
3375 if ((op == POSTINCREMENT_EXPR
3376 || op == POSTDECREMENT_EXPR)
3377 /* With -fpermissive non_dep could be operator++(). */
3378 && (!flag_permissive || nargs != expected_nargs))
3379 expected_nargs += 1;
3380 gcc_assert (nargs == expected_nargs);
3382 args = make_tree_vector ();
3383 va_start (p, overload);
3385 if (TREE_CODE (TREE_TYPE (overload)) == FUNCTION_TYPE)
3387 fn = overload;
3388 for (int i = 0; i < nargs; i++)
3390 tree arg = va_arg (p, tree);
3391 vec_safe_push (args, arg);
3394 else if (TREE_CODE (TREE_TYPE (overload)) == METHOD_TYPE)
3396 tree object = va_arg (p, tree);
3397 tree binfo = TYPE_BINFO (TREE_TYPE (object));
3398 tree method = build_baselink (binfo, binfo, overload, NULL_TREE);
3399 fn = build_min (COMPONENT_REF, TREE_TYPE (overload),
3400 object, method, NULL_TREE);
3401 for (int i = 1; i < nargs; i++)
3403 tree arg = va_arg (p, tree);
3404 vec_safe_push (args, arg);
3407 else
3408 gcc_unreachable ();
3410 va_end (p);
3411 call = build_min_non_dep_call_vec (non_dep, fn, args);
3412 release_tree_vector (args);
3414 tree call_expr = extract_call_expr (call);
3415 KOENIG_LOOKUP_P (call_expr) = KOENIG_LOOKUP_P (non_dep);
3416 CALL_EXPR_OPERATOR_SYNTAX (call_expr) = true;
3417 CALL_EXPR_ORDERED_ARGS (call_expr) = CALL_EXPR_ORDERED_ARGS (non_dep);
3418 CALL_EXPR_REVERSE_ARGS (call_expr) = CALL_EXPR_REVERSE_ARGS (non_dep);
3420 return call;
3423 /* Return a new tree vec copied from VEC, with ELT inserted at index IDX. */
3425 vec<tree, va_gc> *
3426 vec_copy_and_insert (vec<tree, va_gc> *old_vec, tree elt, unsigned idx)
3428 unsigned len = vec_safe_length (old_vec);
3429 gcc_assert (idx <= len);
3431 vec<tree, va_gc> *new_vec = NULL;
3432 vec_alloc (new_vec, len + 1);
3434 unsigned i;
3435 for (i = 0; i < len; ++i)
3437 if (i == idx)
3438 new_vec->quick_push (elt);
3439 new_vec->quick_push ((*old_vec)[i]);
3441 if (i == idx)
3442 new_vec->quick_push (elt);
3444 return new_vec;
3447 tree
3448 get_type_decl (tree t)
3450 if (TREE_CODE (t) == TYPE_DECL)
3451 return t;
3452 if (TYPE_P (t))
3453 return TYPE_STUB_DECL (t);
3454 gcc_assert (t == error_mark_node);
3455 return t;
3458 /* Returns the namespace that contains DECL, whether directly or
3459 indirectly. */
3461 tree
3462 decl_namespace_context (tree decl)
3464 while (1)
3466 if (TREE_CODE (decl) == NAMESPACE_DECL)
3467 return decl;
3468 else if (TYPE_P (decl))
3469 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
3470 else
3471 decl = CP_DECL_CONTEXT (decl);
3475 /* Returns true if decl is within an anonymous namespace, however deeply
3476 nested, or false otherwise. */
3478 bool
3479 decl_anon_ns_mem_p (const_tree decl)
3481 while (TREE_CODE (decl) != NAMESPACE_DECL)
3483 /* Classes inside anonymous namespaces have TREE_PUBLIC == 0. */
3484 if (TYPE_P (decl))
3485 return !TREE_PUBLIC (TYPE_MAIN_DECL (decl));
3487 decl = CP_DECL_CONTEXT (decl);
3489 return !TREE_PUBLIC (decl);
3492 /* Subroutine of cp_tree_equal: t1 and t2 are the CALL_EXPR_FNs of two
3493 CALL_EXPRS. Return whether they are equivalent. */
3495 static bool
3496 called_fns_equal (tree t1, tree t2)
3498 /* Core 1321: dependent names are equivalent even if the overload sets
3499 are different. But do compare explicit template arguments. */
3500 tree name1 = dependent_name (t1);
3501 tree name2 = dependent_name (t2);
3502 if (name1 || name2)
3504 tree targs1 = NULL_TREE, targs2 = NULL_TREE;
3506 if (name1 != name2)
3507 return false;
3509 if (TREE_CODE (t1) == TEMPLATE_ID_EXPR)
3510 targs1 = TREE_OPERAND (t1, 1);
3511 if (TREE_CODE (t2) == TEMPLATE_ID_EXPR)
3512 targs2 = TREE_OPERAND (t2, 1);
3513 return cp_tree_equal (targs1, targs2);
3515 else
3516 return cp_tree_equal (t1, t2);
3519 /* Return truthvalue of whether T1 is the same tree structure as T2.
3520 Return 1 if they are the same. Return 0 if they are different. */
3522 bool
3523 cp_tree_equal (tree t1, tree t2)
3525 enum tree_code code1, code2;
3527 if (t1 == t2)
3528 return true;
3529 if (!t1 || !t2)
3530 return false;
3532 code1 = TREE_CODE (t1);
3533 code2 = TREE_CODE (t2);
3535 if (code1 != code2)
3536 return false;
3538 if (CONSTANT_CLASS_P (t1)
3539 && !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3540 return false;
3542 switch (code1)
3544 case VOID_CST:
3545 /* There's only a single VOID_CST node, so we should never reach
3546 here. */
3547 gcc_unreachable ();
3549 case INTEGER_CST:
3550 return tree_int_cst_equal (t1, t2);
3552 case REAL_CST:
3553 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
3555 case STRING_CST:
3556 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3557 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3558 TREE_STRING_LENGTH (t1));
3560 case FIXED_CST:
3561 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
3562 TREE_FIXED_CST (t2));
3564 case COMPLEX_CST:
3565 return cp_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
3566 && cp_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
3568 case VECTOR_CST:
3569 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
3571 case CONSTRUCTOR:
3572 /* We need to do this when determining whether or not two
3573 non-type pointer to member function template arguments
3574 are the same. */
3575 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
3576 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
3577 return false;
3579 tree field, value;
3580 unsigned int i;
3581 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
3583 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
3584 if (!cp_tree_equal (field, elt2->index)
3585 || !cp_tree_equal (value, elt2->value))
3586 return false;
3589 return true;
3591 case TREE_LIST:
3592 if (!cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
3593 return false;
3594 if (!cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
3595 return false;
3596 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
3598 case SAVE_EXPR:
3599 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3601 case CALL_EXPR:
3603 tree arg1, arg2;
3604 call_expr_arg_iterator iter1, iter2;
3605 if (!called_fns_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
3606 return false;
3607 for (arg1 = first_call_expr_arg (t1, &iter1),
3608 arg2 = first_call_expr_arg (t2, &iter2);
3609 arg1 && arg2;
3610 arg1 = next_call_expr_arg (&iter1),
3611 arg2 = next_call_expr_arg (&iter2))
3612 if (!cp_tree_equal (arg1, arg2))
3613 return false;
3614 if (arg1 || arg2)
3615 return false;
3616 return true;
3619 case TARGET_EXPR:
3621 tree o1 = TREE_OPERAND (t1, 0);
3622 tree o2 = TREE_OPERAND (t2, 0);
3624 /* Special case: if either target is an unallocated VAR_DECL,
3625 it means that it's going to be unified with whatever the
3626 TARGET_EXPR is really supposed to initialize, so treat it
3627 as being equivalent to anything. */
3628 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
3629 && !DECL_RTL_SET_P (o1))
3630 /*Nop*/;
3631 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
3632 && !DECL_RTL_SET_P (o2))
3633 /*Nop*/;
3634 else if (!cp_tree_equal (o1, o2))
3635 return false;
3637 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3640 case PARM_DECL:
3641 /* For comparing uses of parameters in late-specified return types
3642 with an out-of-class definition of the function, but can also come
3643 up for expressions that involve 'this' in a member function
3644 template. */
3646 if (comparing_specializations && !CONSTRAINT_VAR_P (t1))
3647 /* When comparing hash table entries, only an exact match is
3648 good enough; we don't want to replace 'this' with the
3649 version from another function. But be more flexible
3650 with local parameters in a requires-expression. */
3651 return false;
3653 if (same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3655 if (DECL_ARTIFICIAL (t1) ^ DECL_ARTIFICIAL (t2))
3656 return false;
3657 if (CONSTRAINT_VAR_P (t1) ^ CONSTRAINT_VAR_P (t2))
3658 return false;
3659 if (DECL_ARTIFICIAL (t1)
3660 || (DECL_PARM_LEVEL (t1) == DECL_PARM_LEVEL (t2)
3661 && DECL_PARM_INDEX (t1) == DECL_PARM_INDEX (t2)))
3662 return true;
3664 return false;
3666 case VAR_DECL:
3667 case CONST_DECL:
3668 case FIELD_DECL:
3669 case FUNCTION_DECL:
3670 case TEMPLATE_DECL:
3671 case IDENTIFIER_NODE:
3672 case SSA_NAME:
3673 return false;
3675 case BASELINK:
3676 return (BASELINK_BINFO (t1) == BASELINK_BINFO (t2)
3677 && BASELINK_ACCESS_BINFO (t1) == BASELINK_ACCESS_BINFO (t2)
3678 && BASELINK_QUALIFIED_P (t1) == BASELINK_QUALIFIED_P (t2)
3679 && cp_tree_equal (BASELINK_FUNCTIONS (t1),
3680 BASELINK_FUNCTIONS (t2)));
3682 case TEMPLATE_PARM_INDEX:
3683 return (TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
3684 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2)
3685 && (TEMPLATE_PARM_PARAMETER_PACK (t1)
3686 == TEMPLATE_PARM_PARAMETER_PACK (t2))
3687 && same_type_p (TREE_TYPE (TEMPLATE_PARM_DECL (t1)),
3688 TREE_TYPE (TEMPLATE_PARM_DECL (t2))));
3690 case TEMPLATE_ID_EXPR:
3691 return (cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3692 && cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3694 case CONSTRAINT_INFO:
3695 return cp_tree_equal (CI_ASSOCIATED_CONSTRAINTS (t1),
3696 CI_ASSOCIATED_CONSTRAINTS (t2));
3698 case CHECK_CONSTR:
3699 return (CHECK_CONSTR_CONCEPT (t1) == CHECK_CONSTR_CONCEPT (t2)
3700 && comp_template_args (CHECK_CONSTR_ARGS (t1),
3701 CHECK_CONSTR_ARGS (t2)));
3703 case TREE_VEC:
3705 unsigned ix;
3706 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3707 return false;
3708 for (ix = TREE_VEC_LENGTH (t1); ix--;)
3709 if (!cp_tree_equal (TREE_VEC_ELT (t1, ix),
3710 TREE_VEC_ELT (t2, ix)))
3711 return false;
3712 return true;
3715 case SIZEOF_EXPR:
3716 case ALIGNOF_EXPR:
3718 tree o1 = TREE_OPERAND (t1, 0);
3719 tree o2 = TREE_OPERAND (t2, 0);
3721 if (code1 == SIZEOF_EXPR)
3723 if (SIZEOF_EXPR_TYPE_P (t1))
3724 o1 = TREE_TYPE (o1);
3725 if (SIZEOF_EXPR_TYPE_P (t2))
3726 o2 = TREE_TYPE (o2);
3728 if (TREE_CODE (o1) != TREE_CODE (o2))
3729 return false;
3730 if (TYPE_P (o1))
3731 return same_type_p (o1, o2);
3732 else
3733 return cp_tree_equal (o1, o2);
3736 case MODOP_EXPR:
3738 tree t1_op1, t2_op1;
3740 if (!cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0)))
3741 return false;
3743 t1_op1 = TREE_OPERAND (t1, 1);
3744 t2_op1 = TREE_OPERAND (t2, 1);
3745 if (TREE_CODE (t1_op1) != TREE_CODE (t2_op1))
3746 return false;
3748 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t2, 2));
3751 case PTRMEM_CST:
3752 /* Two pointer-to-members are the same if they point to the same
3753 field or function in the same class. */
3754 if (PTRMEM_CST_MEMBER (t1) != PTRMEM_CST_MEMBER (t2))
3755 return false;
3757 return same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2));
3759 case OVERLOAD:
3761 /* Two overloads. Must be exactly the same set of decls. */
3762 lkp_iterator first (t1);
3763 lkp_iterator second (t2);
3765 for (; first && second; ++first, ++second)
3766 if (*first != *second)
3767 return false;
3768 return !(first || second);
3771 case TRAIT_EXPR:
3772 if (TRAIT_EXPR_KIND (t1) != TRAIT_EXPR_KIND (t2))
3773 return false;
3774 return same_type_p (TRAIT_EXPR_TYPE1 (t1), TRAIT_EXPR_TYPE1 (t2))
3775 && cp_tree_equal (TRAIT_EXPR_TYPE2 (t1), TRAIT_EXPR_TYPE2 (t2));
3777 case CAST_EXPR:
3778 case STATIC_CAST_EXPR:
3779 case REINTERPRET_CAST_EXPR:
3780 case CONST_CAST_EXPR:
3781 case DYNAMIC_CAST_EXPR:
3782 case IMPLICIT_CONV_EXPR:
3783 case NEW_EXPR:
3784 CASE_CONVERT:
3785 case NON_LVALUE_EXPR:
3786 case VIEW_CONVERT_EXPR:
3787 if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
3788 return false;
3789 /* Now compare operands as usual. */
3790 break;
3792 case DEFERRED_NOEXCEPT:
3793 return (cp_tree_equal (DEFERRED_NOEXCEPT_PATTERN (t1),
3794 DEFERRED_NOEXCEPT_PATTERN (t2))
3795 && comp_template_args (DEFERRED_NOEXCEPT_ARGS (t1),
3796 DEFERRED_NOEXCEPT_ARGS (t2)));
3797 break;
3799 default:
3800 break;
3803 switch (TREE_CODE_CLASS (code1))
3805 case tcc_unary:
3806 case tcc_binary:
3807 case tcc_comparison:
3808 case tcc_expression:
3809 case tcc_vl_exp:
3810 case tcc_reference:
3811 case tcc_statement:
3813 int i, n;
3815 n = cp_tree_operand_length (t1);
3816 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
3817 && n != TREE_OPERAND_LENGTH (t2))
3818 return false;
3820 for (i = 0; i < n; ++i)
3821 if (!cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
3822 return false;
3824 return true;
3827 case tcc_type:
3828 return same_type_p (t1, t2);
3829 default:
3830 gcc_unreachable ();
3832 /* We can get here with --disable-checking. */
3833 return false;
3836 /* The type of ARG when used as an lvalue. */
3838 tree
3839 lvalue_type (tree arg)
3841 tree type = TREE_TYPE (arg);
3842 return type;
3845 /* The type of ARG for printing error messages; denote lvalues with
3846 reference types. */
3848 tree
3849 error_type (tree arg)
3851 tree type = TREE_TYPE (arg);
3853 if (TREE_CODE (type) == ARRAY_TYPE)
3855 else if (TREE_CODE (type) == ERROR_MARK)
3857 else if (lvalue_p (arg))
3858 type = build_reference_type (lvalue_type (arg));
3859 else if (MAYBE_CLASS_TYPE_P (type))
3860 type = lvalue_type (arg);
3862 return type;
3865 /* Does FUNCTION use a variable-length argument list? */
3868 varargs_function_p (const_tree function)
3870 return stdarg_p (TREE_TYPE (function));
3873 /* Returns 1 if decl is a member of a class. */
3876 member_p (const_tree decl)
3878 const_tree const ctx = DECL_CONTEXT (decl);
3879 return (ctx && TYPE_P (ctx));
3882 /* Create a placeholder for member access where we don't actually have an
3883 object that the access is against. */
3885 tree
3886 build_dummy_object (tree type)
3888 tree decl = build1 (CONVERT_EXPR, build_pointer_type (type), void_node);
3889 return cp_build_fold_indirect_ref (decl);
3892 /* We've gotten a reference to a member of TYPE. Return *this if appropriate,
3893 or a dummy object otherwise. If BINFOP is non-0, it is filled with the
3894 binfo path from current_class_type to TYPE, or 0. */
3896 tree
3897 maybe_dummy_object (tree type, tree* binfop)
3899 tree decl, context;
3900 tree binfo;
3901 tree current = current_nonlambda_class_type ();
3903 if (current
3904 && (binfo = lookup_base (current, type, ba_any, NULL,
3905 tf_warning_or_error)))
3906 context = current;
3907 else
3909 /* Reference from a nested class member function. */
3910 context = type;
3911 binfo = TYPE_BINFO (type);
3914 if (binfop)
3915 *binfop = binfo;
3917 if (current_class_ref
3918 /* current_class_ref might not correspond to current_class_type if
3919 we're in tsubst_default_argument or a lambda-declarator; in either
3920 case, we want to use current_class_ref if it matches CONTEXT. */
3921 && (same_type_ignoring_top_level_qualifiers_p
3922 (TREE_TYPE (current_class_ref), context)))
3923 decl = current_class_ref;
3924 else
3925 decl = build_dummy_object (context);
3927 return decl;
3930 /* Returns 1 if OB is a placeholder object, or a pointer to one. */
3933 is_dummy_object (const_tree ob)
3935 if (INDIRECT_REF_P (ob))
3936 ob = TREE_OPERAND (ob, 0);
3937 return (TREE_CODE (ob) == CONVERT_EXPR
3938 && TREE_OPERAND (ob, 0) == void_node);
3941 /* Returns 1 iff type T is something we want to treat as a scalar type for
3942 the purpose of deciding whether it is trivial/POD/standard-layout. */
3944 bool
3945 scalarish_type_p (const_tree t)
3947 if (t == error_mark_node)
3948 return 1;
3950 return (SCALAR_TYPE_P (t) || VECTOR_TYPE_P (t));
3953 /* Returns true iff T requires non-trivial default initialization. */
3955 bool
3956 type_has_nontrivial_default_init (const_tree t)
3958 t = strip_array_types (CONST_CAST_TREE (t));
3960 if (CLASS_TYPE_P (t))
3961 return TYPE_HAS_COMPLEX_DFLT (t);
3962 else
3963 return 0;
3966 /* Track classes with only deleted copy/move constructors so that we can warn
3967 if they are used in call/return by value. */
3969 static GTY(()) hash_set<tree>* deleted_copy_types;
3970 static void
3971 remember_deleted_copy (const_tree t)
3973 if (!deleted_copy_types)
3974 deleted_copy_types = hash_set<tree>::create_ggc(37);
3975 deleted_copy_types->add (CONST_CAST_TREE (t));
3977 void
3978 maybe_warn_parm_abi (tree t, location_t loc)
3980 if (!deleted_copy_types
3981 || !deleted_copy_types->contains (t))
3982 return;
3984 warning_at (loc, OPT_Wabi, "the calling convention for %qT changes in "
3985 "-fabi-version=12 (GCC 8)", t);
3986 static bool explained = false;
3987 if (!explained)
3989 inform (loc, " because all of its copy and move constructors "
3990 "are deleted");
3991 explained = true;
3995 /* Returns true iff copying an object of type T (including via move
3996 constructor) is non-trivial. That is, T has no non-trivial copy
3997 constructors and no non-trivial move constructors, and not all copy/move
3998 constructors are deleted. This function implements the ABI notion of
3999 non-trivial copy, which has diverged from the one in the standard. */
4001 bool
4002 type_has_nontrivial_copy_init (const_tree type)
4004 tree t = strip_array_types (CONST_CAST_TREE (type));
4006 if (CLASS_TYPE_P (t))
4008 gcc_assert (COMPLETE_TYPE_P (t));
4010 if (TYPE_HAS_COMPLEX_COPY_CTOR (t)
4011 || TYPE_HAS_COMPLEX_MOVE_CTOR (t))
4012 /* Nontrivial. */
4013 return true;
4015 if (cxx_dialect < cxx11)
4016 /* No deleted functions before C++11. */
4017 return false;
4019 /* Before ABI v12 we did a bitwise copy of types with only deleted
4020 copy/move constructors. */
4021 if (!abi_version_at_least (12)
4022 && !(warn_abi && abi_version_crosses (12)))
4023 return false;
4025 bool saw_copy = false;
4026 bool saw_non_deleted = false;
4028 if (CLASSTYPE_LAZY_MOVE_CTOR (t))
4029 saw_copy = saw_non_deleted = true;
4030 else if (CLASSTYPE_LAZY_COPY_CTOR (t))
4032 saw_copy = true;
4033 if (classtype_has_move_assign_or_move_ctor_p (t, true))
4034 /* [class.copy]/8 If the class definition declares a move
4035 constructor or move assignment operator, the implicitly declared
4036 copy constructor is defined as deleted.... */;
4037 else
4038 /* Any other reason the implicitly-declared function would be
4039 deleted would also cause TYPE_HAS_COMPLEX_COPY_CTOR to be
4040 set. */
4041 saw_non_deleted = true;
4044 if (!saw_non_deleted)
4045 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (t)); iter; ++iter)
4047 tree fn = *iter;
4048 if (copy_fn_p (fn))
4050 saw_copy = true;
4051 if (!DECL_DELETED_FN (fn))
4053 /* Not deleted, therefore trivial. */
4054 saw_non_deleted = true;
4055 break;
4060 gcc_assert (saw_copy);
4062 if (saw_copy && !saw_non_deleted)
4064 if (warn_abi && abi_version_crosses (12))
4065 remember_deleted_copy (t);
4066 if (abi_version_at_least (12))
4067 return true;
4070 return false;
4072 else
4073 return 0;
4076 /* Returns 1 iff type T is a trivially copyable type, as defined in
4077 [basic.types] and [class]. */
4079 bool
4080 trivially_copyable_p (const_tree t)
4082 t = strip_array_types (CONST_CAST_TREE (t));
4084 if (CLASS_TYPE_P (t))
4085 return ((!TYPE_HAS_COPY_CTOR (t)
4086 || !TYPE_HAS_COMPLEX_COPY_CTOR (t))
4087 && !TYPE_HAS_COMPLEX_MOVE_CTOR (t)
4088 && (!TYPE_HAS_COPY_ASSIGN (t)
4089 || !TYPE_HAS_COMPLEX_COPY_ASSIGN (t))
4090 && !TYPE_HAS_COMPLEX_MOVE_ASSIGN (t)
4091 && TYPE_HAS_TRIVIAL_DESTRUCTOR (t));
4092 else
4093 return !CP_TYPE_VOLATILE_P (t) && scalarish_type_p (t);
4096 /* Returns 1 iff type T is a trivial type, as defined in [basic.types] and
4097 [class]. */
4099 bool
4100 trivial_type_p (const_tree t)
4102 t = strip_array_types (CONST_CAST_TREE (t));
4104 if (CLASS_TYPE_P (t))
4105 return (TYPE_HAS_TRIVIAL_DFLT (t)
4106 && trivially_copyable_p (t));
4107 else
4108 return scalarish_type_p (t);
4111 /* Returns 1 iff type T is a POD type, as defined in [basic.types]. */
4113 bool
4114 pod_type_p (const_tree t)
4116 /* This CONST_CAST is okay because strip_array_types returns its
4117 argument unmodified and we assign it to a const_tree. */
4118 t = strip_array_types (CONST_CAST_TREE(t));
4120 if (!CLASS_TYPE_P (t))
4121 return scalarish_type_p (t);
4122 else if (cxx_dialect > cxx98)
4123 /* [class]/10: A POD struct is a class that is both a trivial class and a
4124 standard-layout class, and has no non-static data members of type
4125 non-POD struct, non-POD union (or array of such types).
4127 We don't need to check individual members because if a member is
4128 non-std-layout or non-trivial, the class will be too. */
4129 return (std_layout_type_p (t) && trivial_type_p (t));
4130 else
4131 /* The C++98 definition of POD is different. */
4132 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
4135 /* Returns true iff T is POD for the purpose of layout, as defined in the
4136 C++ ABI. */
4138 bool
4139 layout_pod_type_p (const_tree t)
4141 t = strip_array_types (CONST_CAST_TREE (t));
4143 if (CLASS_TYPE_P (t))
4144 return !CLASSTYPE_NON_LAYOUT_POD_P (t);
4145 else
4146 return scalarish_type_p (t);
4149 /* Returns true iff T is a standard-layout type, as defined in
4150 [basic.types]. */
4152 bool
4153 std_layout_type_p (const_tree t)
4155 t = strip_array_types (CONST_CAST_TREE (t));
4157 if (CLASS_TYPE_P (t))
4158 return !CLASSTYPE_NON_STD_LAYOUT (t);
4159 else
4160 return scalarish_type_p (t);
4163 static bool record_has_unique_obj_representations (const_tree, const_tree);
4165 /* Returns true iff T satisfies std::has_unique_object_representations<T>,
4166 as defined in [meta.unary.prop]. */
4168 bool
4169 type_has_unique_obj_representations (const_tree t)
4171 bool ret;
4173 t = strip_array_types (CONST_CAST_TREE (t));
4175 if (!trivially_copyable_p (t))
4176 return false;
4178 if (CLASS_TYPE_P (t) && CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t))
4179 return CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t);
4181 switch (TREE_CODE (t))
4183 case INTEGER_TYPE:
4184 case POINTER_TYPE:
4185 case REFERENCE_TYPE:
4186 /* If some backend has any paddings in these types, we should add
4187 a target hook for this and handle it there. */
4188 return true;
4190 case BOOLEAN_TYPE:
4191 /* For bool values other than 0 and 1 should only appear with
4192 undefined behavior. */
4193 return true;
4195 case ENUMERAL_TYPE:
4196 return type_has_unique_obj_representations (ENUM_UNDERLYING_TYPE (t));
4198 case REAL_TYPE:
4199 /* XFmode certainly contains padding on x86, which the CPU doesn't store
4200 when storing long double values, so for that we have to return false.
4201 Other kinds of floating point values are questionable due to +.0/-.0
4202 and NaNs, let's play safe for now. */
4203 return false;
4205 case FIXED_POINT_TYPE:
4206 return false;
4208 case OFFSET_TYPE:
4209 return true;
4211 case COMPLEX_TYPE:
4212 case VECTOR_TYPE:
4213 return type_has_unique_obj_representations (TREE_TYPE (t));
4215 case RECORD_TYPE:
4216 ret = record_has_unique_obj_representations (t, TYPE_SIZE (t));
4217 if (CLASS_TYPE_P (t))
4219 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
4220 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
4222 return ret;
4224 case UNION_TYPE:
4225 ret = true;
4226 bool any_fields;
4227 any_fields = false;
4228 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4229 if (TREE_CODE (field) == FIELD_DECL)
4231 any_fields = true;
4232 if (!type_has_unique_obj_representations (TREE_TYPE (field))
4233 || simple_cst_equal (DECL_SIZE (field), TYPE_SIZE (t)) != 1)
4235 ret = false;
4236 break;
4239 if (!any_fields && !integer_zerop (TYPE_SIZE (t)))
4240 ret = false;
4241 if (CLASS_TYPE_P (t))
4243 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS_SET (t) = 1;
4244 CLASSTYPE_UNIQUE_OBJ_REPRESENTATIONS (t) = ret;
4246 return ret;
4248 case NULLPTR_TYPE:
4249 return false;
4251 case ERROR_MARK:
4252 return false;
4254 default:
4255 gcc_unreachable ();
4259 /* Helper function for type_has_unique_obj_representations. */
4261 static bool
4262 record_has_unique_obj_representations (const_tree t, const_tree sz)
4264 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4265 if (TREE_CODE (field) != FIELD_DECL)
4267 /* For bases, can't use type_has_unique_obj_representations here, as in
4268 struct S { int i : 24; S (); };
4269 struct T : public S { int j : 8; T (); };
4270 S doesn't have unique obj representations, but T does. */
4271 else if (DECL_FIELD_IS_BASE (field))
4273 if (!record_has_unique_obj_representations (TREE_TYPE (field),
4274 DECL_SIZE (field)))
4275 return false;
4277 else if (DECL_C_BIT_FIELD (field))
4279 tree btype = DECL_BIT_FIELD_TYPE (field);
4280 if (!type_has_unique_obj_representations (btype))
4281 return false;
4283 else if (!type_has_unique_obj_representations (TREE_TYPE (field)))
4284 return false;
4286 offset_int cur = 0;
4287 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
4288 if (TREE_CODE (field) == FIELD_DECL)
4290 offset_int fld = wi::to_offset (DECL_FIELD_OFFSET (field));
4291 offset_int bitpos = wi::to_offset (DECL_FIELD_BIT_OFFSET (field));
4292 fld = fld * BITS_PER_UNIT + bitpos;
4293 if (cur != fld)
4294 return false;
4295 if (DECL_SIZE (field))
4297 offset_int size = wi::to_offset (DECL_SIZE (field));
4298 cur += size;
4301 if (cur != wi::to_offset (sz))
4302 return false;
4304 return true;
4307 /* Nonzero iff type T is a class template implicit specialization. */
4309 bool
4310 class_tmpl_impl_spec_p (const_tree t)
4312 return CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INSTANTIATION (t);
4315 /* Returns 1 iff zero initialization of type T means actually storing
4316 zeros in it. */
4319 zero_init_p (const_tree t)
4321 /* This CONST_CAST is okay because strip_array_types returns its
4322 argument unmodified and we assign it to a const_tree. */
4323 t = strip_array_types (CONST_CAST_TREE(t));
4325 if (t == error_mark_node)
4326 return 1;
4328 /* NULL pointers to data members are initialized with -1. */
4329 if (TYPE_PTRDATAMEM_P (t))
4330 return 0;
4332 /* Classes that contain types that can't be zero-initialized, cannot
4333 be zero-initialized themselves. */
4334 if (CLASS_TYPE_P (t) && CLASSTYPE_NON_ZERO_INIT_P (t))
4335 return 0;
4337 return 1;
4340 /* Handle the C++17 [[nodiscard]] attribute, which is similar to the GNU
4341 warn_unused_result attribute. */
4343 static tree
4344 handle_nodiscard_attribute (tree *node, tree name, tree /*args*/,
4345 int /*flags*/, bool *no_add_attrs)
4347 if (TREE_CODE (*node) == FUNCTION_DECL)
4349 if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
4350 warning (OPT_Wattributes, "%qE attribute applied to %qD with void "
4351 "return type", name, *node);
4353 else if (OVERLOAD_TYPE_P (*node))
4354 /* OK */;
4355 else
4357 warning (OPT_Wattributes, "%qE attribute can only be applied to "
4358 "functions or to class or enumeration types", name);
4359 *no_add_attrs = true;
4361 return NULL_TREE;
4364 /* Table of valid C++ attributes. */
4365 const struct attribute_spec cxx_attribute_table[] =
4367 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4368 affects_type_identity, handler, exclude } */
4369 { "init_priority", 1, 1, true, false, false, false,
4370 handle_init_priority_attribute, NULL },
4371 { "abi_tag", 1, -1, false, false, false, true,
4372 handle_abi_tag_attribute, NULL },
4373 { NULL, 0, 0, false, false, false, false, NULL, NULL }
4376 /* Table of C++ standard attributes. */
4377 const struct attribute_spec std_attribute_table[] =
4379 /* { name, min_len, max_len, decl_req, type_req, fn_type_req,
4380 affects_type_identity, handler, exclude } */
4381 { "maybe_unused", 0, 0, false, false, false, false,
4382 handle_unused_attribute, NULL },
4383 { "nodiscard", 0, 0, false, false, false, false,
4384 handle_nodiscard_attribute, NULL },
4385 { NULL, 0, 0, false, false, false, false, NULL, NULL }
4388 /* Handle an "init_priority" attribute; arguments as in
4389 struct attribute_spec.handler. */
4390 static tree
4391 handle_init_priority_attribute (tree* node,
4392 tree name,
4393 tree args,
4394 int /*flags*/,
4395 bool* no_add_attrs)
4397 tree initp_expr = TREE_VALUE (args);
4398 tree decl = *node;
4399 tree type = TREE_TYPE (decl);
4400 int pri;
4402 STRIP_NOPS (initp_expr);
4403 initp_expr = default_conversion (initp_expr);
4404 if (initp_expr)
4405 initp_expr = maybe_constant_value (initp_expr);
4407 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
4409 error ("requested init_priority is not an integer constant");
4410 cxx_constant_value (initp_expr);
4411 *no_add_attrs = true;
4412 return NULL_TREE;
4415 pri = TREE_INT_CST_LOW (initp_expr);
4417 type = strip_array_types (type);
4419 if (decl == NULL_TREE
4420 || !VAR_P (decl)
4421 || !TREE_STATIC (decl)
4422 || DECL_EXTERNAL (decl)
4423 || (TREE_CODE (type) != RECORD_TYPE
4424 && TREE_CODE (type) != UNION_TYPE)
4425 /* Static objects in functions are initialized the
4426 first time control passes through that
4427 function. This is not precise enough to pin down an
4428 init_priority value, so don't allow it. */
4429 || current_function_decl)
4431 error ("can only use %qE attribute on file-scope definitions "
4432 "of objects of class type", name);
4433 *no_add_attrs = true;
4434 return NULL_TREE;
4437 if (pri > MAX_INIT_PRIORITY || pri <= 0)
4439 error ("requested init_priority is out of range");
4440 *no_add_attrs = true;
4441 return NULL_TREE;
4444 /* Check for init_priorities that are reserved for
4445 language and runtime support implementations.*/
4446 if (pri <= MAX_RESERVED_INIT_PRIORITY)
4448 warning
4449 (0, "requested init_priority is reserved for internal use");
4452 if (SUPPORTS_INIT_PRIORITY)
4454 SET_DECL_INIT_PRIORITY (decl, pri);
4455 DECL_HAS_INIT_PRIORITY_P (decl) = 1;
4456 return NULL_TREE;
4458 else
4460 error ("%qE attribute is not supported on this platform", name);
4461 *no_add_attrs = true;
4462 return NULL_TREE;
4466 /* DECL is being redeclared; the old declaration had the abi tags in OLD,
4467 and the new one has the tags in NEW_. Give an error if there are tags
4468 in NEW_ that weren't in OLD. */
4470 bool
4471 check_abi_tag_redeclaration (const_tree decl, const_tree old, const_tree new_)
4473 if (old && TREE_CODE (TREE_VALUE (old)) == TREE_LIST)
4474 old = TREE_VALUE (old);
4475 if (new_ && TREE_CODE (TREE_VALUE (new_)) == TREE_LIST)
4476 new_ = TREE_VALUE (new_);
4477 bool err = false;
4478 for (const_tree t = new_; t; t = TREE_CHAIN (t))
4480 tree str = TREE_VALUE (t);
4481 for (const_tree in = old; in; in = TREE_CHAIN (in))
4483 tree ostr = TREE_VALUE (in);
4484 if (cp_tree_equal (str, ostr))
4485 goto found;
4487 error ("redeclaration of %qD adds abi tag %qE", decl, str);
4488 err = true;
4489 found:;
4491 if (err)
4493 inform (DECL_SOURCE_LOCATION (decl), "previous declaration here");
4494 return false;
4496 return true;
4499 /* The abi_tag attribute with the name NAME was given ARGS. If they are
4500 ill-formed, give an error and return false; otherwise, return true. */
4502 bool
4503 check_abi_tag_args (tree args, tree name)
4505 if (!args)
4507 error ("the %qE attribute requires arguments", name);
4508 return false;
4510 for (tree arg = args; arg; arg = TREE_CHAIN (arg))
4512 tree elt = TREE_VALUE (arg);
4513 if (TREE_CODE (elt) != STRING_CST
4514 || (!same_type_ignoring_top_level_qualifiers_p
4515 (strip_array_types (TREE_TYPE (elt)),
4516 char_type_node)))
4518 error ("arguments to the %qE attribute must be narrow string "
4519 "literals", name);
4520 return false;
4522 const char *begin = TREE_STRING_POINTER (elt);
4523 const char *end = begin + TREE_STRING_LENGTH (elt);
4524 for (const char *p = begin; p != end; ++p)
4526 char c = *p;
4527 if (p == begin)
4529 if (!ISALPHA (c) && c != '_')
4531 error ("arguments to the %qE attribute must contain valid "
4532 "identifiers", name);
4533 inform (input_location, "%<%c%> is not a valid first "
4534 "character for an identifier", c);
4535 return false;
4538 else if (p == end - 1)
4539 gcc_assert (c == 0);
4540 else
4542 if (!ISALNUM (c) && c != '_')
4544 error ("arguments to the %qE attribute must contain valid "
4545 "identifiers", name);
4546 inform (input_location, "%<%c%> is not a valid character "
4547 "in an identifier", c);
4548 return false;
4553 return true;
4556 /* Handle an "abi_tag" attribute; arguments as in
4557 struct attribute_spec.handler. */
4559 static tree
4560 handle_abi_tag_attribute (tree* node, tree name, tree args,
4561 int flags, bool* no_add_attrs)
4563 if (!check_abi_tag_args (args, name))
4564 goto fail;
4566 if (TYPE_P (*node))
4568 if (!OVERLOAD_TYPE_P (*node))
4570 error ("%qE attribute applied to non-class, non-enum type %qT",
4571 name, *node);
4572 goto fail;
4574 else if (!(flags & (int)ATTR_FLAG_TYPE_IN_PLACE))
4576 error ("%qE attribute applied to %qT after its definition",
4577 name, *node);
4578 goto fail;
4580 else if (CLASS_TYPE_P (*node)
4581 && CLASSTYPE_TEMPLATE_INSTANTIATION (*node))
4583 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
4584 "template instantiation %qT", name, *node);
4585 goto fail;
4587 else if (CLASS_TYPE_P (*node)
4588 && CLASSTYPE_TEMPLATE_SPECIALIZATION (*node))
4590 warning (OPT_Wattributes, "ignoring %qE attribute applied to "
4591 "template specialization %qT", name, *node);
4592 goto fail;
4595 tree attributes = TYPE_ATTRIBUTES (*node);
4596 tree decl = TYPE_NAME (*node);
4598 /* Make sure all declarations have the same abi tags. */
4599 if (DECL_SOURCE_LOCATION (decl) != input_location)
4601 if (!check_abi_tag_redeclaration (decl,
4602 lookup_attribute ("abi_tag",
4603 attributes),
4604 args))
4605 goto fail;
4608 else
4610 if (!VAR_OR_FUNCTION_DECL_P (*node))
4612 error ("%qE attribute applied to non-function, non-variable %qD",
4613 name, *node);
4614 goto fail;
4616 else if (DECL_LANGUAGE (*node) == lang_c)
4618 error ("%qE attribute applied to extern \"C\" declaration %qD",
4619 name, *node);
4620 goto fail;
4624 return NULL_TREE;
4626 fail:
4627 *no_add_attrs = true;
4628 return NULL_TREE;
4631 /* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the
4632 thing pointed to by the constant. */
4634 tree
4635 make_ptrmem_cst (tree type, tree member)
4637 tree ptrmem_cst = make_node (PTRMEM_CST);
4638 TREE_TYPE (ptrmem_cst) = type;
4639 PTRMEM_CST_MEMBER (ptrmem_cst) = member;
4640 return ptrmem_cst;
4643 /* Build a variant of TYPE that has the indicated ATTRIBUTES. May
4644 return an existing type if an appropriate type already exists. */
4646 tree
4647 cp_build_type_attribute_variant (tree type, tree attributes)
4649 tree new_type;
4651 new_type = build_type_attribute_variant (type, attributes);
4652 if (TREE_CODE (new_type) == FUNCTION_TYPE
4653 || TREE_CODE (new_type) == METHOD_TYPE)
4655 new_type = build_exception_variant (new_type,
4656 TYPE_RAISES_EXCEPTIONS (type));
4657 new_type = build_ref_qualified_type (new_type,
4658 type_memfn_rqual (type));
4661 /* Making a new main variant of a class type is broken. */
4662 gcc_assert (!CLASS_TYPE_P (type) || new_type == type);
4664 return new_type;
4667 /* Return TRUE if TYPE1 and TYPE2 are identical for type hashing purposes.
4668 Called only after doing all language independent checks. */
4670 bool
4671 cxx_type_hash_eq (const_tree typea, const_tree typeb)
4673 gcc_assert (TREE_CODE (typea) == FUNCTION_TYPE
4674 || TREE_CODE (typea) == METHOD_TYPE);
4676 if (type_memfn_rqual (typea) != type_memfn_rqual (typeb))
4677 return false;
4678 return comp_except_specs (TYPE_RAISES_EXCEPTIONS (typea),
4679 TYPE_RAISES_EXCEPTIONS (typeb), ce_exact);
4682 /* Copy the language-specific type variant modifiers from TYPEB to TYPEA. For
4683 C++, these are the exception-specifier and ref-qualifier. */
4685 tree
4686 cxx_copy_lang_qualifiers (const_tree typea, const_tree typeb)
4688 tree type = CONST_CAST_TREE (typea);
4689 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
4691 type = build_exception_variant (type, TYPE_RAISES_EXCEPTIONS (typeb));
4692 type = build_ref_qualified_type (type, type_memfn_rqual (typeb));
4694 return type;
4697 /* Apply FUNC to all language-specific sub-trees of TP in a pre-order
4698 traversal. Called from walk_tree. */
4700 tree
4701 cp_walk_subtrees (tree *tp, int *walk_subtrees_p, walk_tree_fn func,
4702 void *data, hash_set<tree> *pset)
4704 enum tree_code code = TREE_CODE (*tp);
4705 tree result;
4707 #define WALK_SUBTREE(NODE) \
4708 do \
4710 result = cp_walk_tree (&(NODE), func, data, pset); \
4711 if (result) goto out; \
4713 while (0)
4715 /* Not one of the easy cases. We must explicitly go through the
4716 children. */
4717 result = NULL_TREE;
4718 switch (code)
4720 case DEFAULT_ARG:
4721 case TEMPLATE_TEMPLATE_PARM:
4722 case BOUND_TEMPLATE_TEMPLATE_PARM:
4723 case UNBOUND_CLASS_TEMPLATE:
4724 case TEMPLATE_PARM_INDEX:
4725 case TEMPLATE_TYPE_PARM:
4726 case TYPENAME_TYPE:
4727 case TYPEOF_TYPE:
4728 case UNDERLYING_TYPE:
4729 /* None of these have subtrees other than those already walked
4730 above. */
4731 *walk_subtrees_p = 0;
4732 break;
4734 case BASELINK:
4735 if (BASELINK_QUALIFIED_P (*tp))
4736 WALK_SUBTREE (BINFO_TYPE (BASELINK_ACCESS_BINFO (*tp)));
4737 WALK_SUBTREE (BASELINK_FUNCTIONS (*tp));
4738 *walk_subtrees_p = 0;
4739 break;
4741 case PTRMEM_CST:
4742 WALK_SUBTREE (TREE_TYPE (*tp));
4743 *walk_subtrees_p = 0;
4744 break;
4746 case TREE_LIST:
4747 WALK_SUBTREE (TREE_PURPOSE (*tp));
4748 break;
4750 case OVERLOAD:
4751 WALK_SUBTREE (OVL_FUNCTION (*tp));
4752 WALK_SUBTREE (OVL_CHAIN (*tp));
4753 *walk_subtrees_p = 0;
4754 break;
4756 case USING_DECL:
4757 WALK_SUBTREE (DECL_NAME (*tp));
4758 WALK_SUBTREE (USING_DECL_SCOPE (*tp));
4759 WALK_SUBTREE (USING_DECL_DECLS (*tp));
4760 *walk_subtrees_p = 0;
4761 break;
4763 case RECORD_TYPE:
4764 if (TYPE_PTRMEMFUNC_P (*tp))
4765 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (*tp));
4766 break;
4768 case TYPE_ARGUMENT_PACK:
4769 case NONTYPE_ARGUMENT_PACK:
4771 tree args = ARGUMENT_PACK_ARGS (*tp);
4772 int i, len = TREE_VEC_LENGTH (args);
4773 for (i = 0; i < len; i++)
4774 WALK_SUBTREE (TREE_VEC_ELT (args, i));
4776 break;
4778 case TYPE_PACK_EXPANSION:
4779 WALK_SUBTREE (TREE_TYPE (*tp));
4780 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp));
4781 *walk_subtrees_p = 0;
4782 break;
4784 case EXPR_PACK_EXPANSION:
4785 WALK_SUBTREE (TREE_OPERAND (*tp, 0));
4786 WALK_SUBTREE (PACK_EXPANSION_EXTRA_ARGS (*tp));
4787 *walk_subtrees_p = 0;
4788 break;
4790 case CAST_EXPR:
4791 case REINTERPRET_CAST_EXPR:
4792 case STATIC_CAST_EXPR:
4793 case CONST_CAST_EXPR:
4794 case DYNAMIC_CAST_EXPR:
4795 case IMPLICIT_CONV_EXPR:
4796 if (TREE_TYPE (*tp))
4797 WALK_SUBTREE (TREE_TYPE (*tp));
4800 int i;
4801 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (*tp)); ++i)
4802 WALK_SUBTREE (TREE_OPERAND (*tp, i));
4804 *walk_subtrees_p = 0;
4805 break;
4807 case TRAIT_EXPR:
4808 WALK_SUBTREE (TRAIT_EXPR_TYPE1 (*tp));
4809 WALK_SUBTREE (TRAIT_EXPR_TYPE2 (*tp));
4810 *walk_subtrees_p = 0;
4811 break;
4813 case DECLTYPE_TYPE:
4814 WALK_SUBTREE (DECLTYPE_TYPE_EXPR (*tp));
4815 *walk_subtrees_p = 0;
4816 break;
4818 case REQUIRES_EXPR:
4819 // Only recurse through the nested expression. Do not
4820 // walk the parameter list. Doing so causes false
4821 // positives in the pack expansion checker since the
4822 // requires parameters are introduced as pack expansions.
4823 WALK_SUBTREE (TREE_OPERAND (*tp, 1));
4824 *walk_subtrees_p = 0;
4825 break;
4827 case DECL_EXPR:
4828 /* User variables should be mentioned in BIND_EXPR_VARS
4829 and their initializers and sizes walked when walking
4830 the containing BIND_EXPR. Compiler temporaries are
4831 handled here. */
4832 if (VAR_P (TREE_OPERAND (*tp, 0))
4833 && DECL_ARTIFICIAL (TREE_OPERAND (*tp, 0))
4834 && !TREE_STATIC (TREE_OPERAND (*tp, 0)))
4836 tree decl = TREE_OPERAND (*tp, 0);
4837 WALK_SUBTREE (DECL_INITIAL (decl));
4838 WALK_SUBTREE (DECL_SIZE (decl));
4839 WALK_SUBTREE (DECL_SIZE_UNIT (decl));
4841 break;
4843 default:
4844 return NULL_TREE;
4847 /* We didn't find what we were looking for. */
4848 out:
4849 return result;
4851 #undef WALK_SUBTREE
4854 /* Like save_expr, but for C++. */
4856 tree
4857 cp_save_expr (tree expr)
4859 /* There is no reason to create a SAVE_EXPR within a template; if
4860 needed, we can create the SAVE_EXPR when instantiating the
4861 template. Furthermore, the middle-end cannot handle C++-specific
4862 tree codes. */
4863 if (processing_template_decl)
4864 return expr;
4865 return save_expr (expr);
4868 /* Initialize tree.c. */
4870 void
4871 init_tree (void)
4873 list_hash_table = hash_table<list_hasher>::create_ggc (61);
4874 register_scoped_attributes (std_attribute_table, NULL);
4877 /* Returns the kind of special function that DECL (a FUNCTION_DECL)
4878 is. Note that sfk_none is zero, so this function can be used as a
4879 predicate to test whether or not DECL is a special function. */
4881 special_function_kind
4882 special_function_p (const_tree decl)
4884 /* Rather than doing all this stuff with magic names, we should
4885 probably have a field of type `special_function_kind' in
4886 DECL_LANG_SPECIFIC. */
4887 if (DECL_INHERITED_CTOR (decl))
4888 return sfk_inheriting_constructor;
4889 if (DECL_COPY_CONSTRUCTOR_P (decl))
4890 return sfk_copy_constructor;
4891 if (DECL_MOVE_CONSTRUCTOR_P (decl))
4892 return sfk_move_constructor;
4893 if (DECL_CONSTRUCTOR_P (decl))
4894 return sfk_constructor;
4895 if (DECL_ASSIGNMENT_OPERATOR_P (decl)
4896 && DECL_OVERLOADED_OPERATOR_IS (decl, NOP_EXPR))
4898 if (copy_fn_p (decl))
4899 return sfk_copy_assignment;
4900 if (move_fn_p (decl))
4901 return sfk_move_assignment;
4903 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
4904 return sfk_destructor;
4905 if (DECL_COMPLETE_DESTRUCTOR_P (decl))
4906 return sfk_complete_destructor;
4907 if (DECL_BASE_DESTRUCTOR_P (decl))
4908 return sfk_base_destructor;
4909 if (DECL_DELETING_DESTRUCTOR_P (decl))
4910 return sfk_deleting_destructor;
4911 if (DECL_CONV_FN_P (decl))
4912 return sfk_conversion;
4913 if (deduction_guide_p (decl))
4914 return sfk_deduction_guide;
4916 return sfk_none;
4919 /* Returns nonzero if TYPE is a character type, including wchar_t. */
4922 char_type_p (tree type)
4924 return (same_type_p (type, char_type_node)
4925 || same_type_p (type, unsigned_char_type_node)
4926 || same_type_p (type, signed_char_type_node)
4927 || same_type_p (type, char16_type_node)
4928 || same_type_p (type, char32_type_node)
4929 || same_type_p (type, wchar_type_node));
4932 /* Returns the kind of linkage associated with the indicated DECL. Th
4933 value returned is as specified by the language standard; it is
4934 independent of implementation details regarding template
4935 instantiation, etc. For example, it is possible that a declaration
4936 to which this function assigns external linkage would not show up
4937 as a global symbol when you run `nm' on the resulting object file. */
4939 linkage_kind
4940 decl_linkage (tree decl)
4942 /* This function doesn't attempt to calculate the linkage from first
4943 principles as given in [basic.link]. Instead, it makes use of
4944 the fact that we have already set TREE_PUBLIC appropriately, and
4945 then handles a few special cases. Ideally, we would calculate
4946 linkage first, and then transform that into a concrete
4947 implementation. */
4949 /* Things that don't have names have no linkage. */
4950 if (!DECL_NAME (decl))
4951 return lk_none;
4953 /* Fields have no linkage. */
4954 if (TREE_CODE (decl) == FIELD_DECL)
4955 return lk_none;
4957 /* Things that are TREE_PUBLIC have external linkage. */
4958 if (TREE_PUBLIC (decl))
4959 return lk_external;
4961 /* maybe_thunk_body clears TREE_PUBLIC on the maybe-in-charge 'tor variants,
4962 check one of the "clones" for the real linkage. */
4963 if ((DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl)
4964 || DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl))
4965 && DECL_CHAIN (decl)
4966 && DECL_CLONED_FUNCTION (DECL_CHAIN (decl)))
4967 return decl_linkage (DECL_CHAIN (decl));
4969 if (TREE_CODE (decl) == NAMESPACE_DECL)
4970 return lk_external;
4972 /* Linkage of a CONST_DECL depends on the linkage of the enumeration
4973 type. */
4974 if (TREE_CODE (decl) == CONST_DECL)
4975 return decl_linkage (TYPE_NAME (DECL_CONTEXT (decl)));
4977 /* Things in local scope do not have linkage, if they don't have
4978 TREE_PUBLIC set. */
4979 if (decl_function_context (decl))
4980 return lk_none;
4982 /* Members of the anonymous namespace also have TREE_PUBLIC unset, but
4983 are considered to have external linkage for language purposes, as do
4984 template instantiations on targets without weak symbols. DECLs really
4985 meant to have internal linkage have DECL_THIS_STATIC set. */
4986 if (TREE_CODE (decl) == TYPE_DECL)
4987 return lk_external;
4988 if (VAR_OR_FUNCTION_DECL_P (decl))
4990 if (!DECL_THIS_STATIC (decl))
4991 return lk_external;
4993 /* Static data members and static member functions from classes
4994 in anonymous namespace also don't have TREE_PUBLIC set. */
4995 if (DECL_CLASS_CONTEXT (decl))
4996 return lk_external;
4999 /* Everything else has internal linkage. */
5000 return lk_internal;
5003 /* Returns the storage duration of the object or reference associated with
5004 the indicated DECL, which should be a VAR_DECL or PARM_DECL. */
5006 duration_kind
5007 decl_storage_duration (tree decl)
5009 if (TREE_CODE (decl) == PARM_DECL)
5010 return dk_auto;
5011 if (TREE_CODE (decl) == FUNCTION_DECL)
5012 return dk_static;
5013 gcc_assert (VAR_P (decl));
5014 if (!TREE_STATIC (decl)
5015 && !DECL_EXTERNAL (decl))
5016 return dk_auto;
5017 if (CP_DECL_THREAD_LOCAL_P (decl))
5018 return dk_thread;
5019 return dk_static;
5022 /* EXP is an expression that we want to pre-evaluate. Returns (in
5023 *INITP) an expression that will perform the pre-evaluation. The
5024 value returned by this function is a side-effect free expression
5025 equivalent to the pre-evaluated expression. Callers must ensure
5026 that *INITP is evaluated before EXP. */
5028 tree
5029 stabilize_expr (tree exp, tree* initp)
5031 tree init_expr;
5033 if (!TREE_SIDE_EFFECTS (exp))
5034 init_expr = NULL_TREE;
5035 else if (VOID_TYPE_P (TREE_TYPE (exp)))
5037 init_expr = exp;
5038 exp = void_node;
5040 /* There are no expressions with REFERENCE_TYPE, but there can be call
5041 arguments with such a type; just treat it as a pointer. */
5042 else if (TREE_CODE (TREE_TYPE (exp)) == REFERENCE_TYPE
5043 || SCALAR_TYPE_P (TREE_TYPE (exp))
5044 || !glvalue_p (exp))
5046 init_expr = get_target_expr (exp);
5047 exp = TARGET_EXPR_SLOT (init_expr);
5048 if (CLASS_TYPE_P (TREE_TYPE (exp)))
5049 exp = move (exp);
5050 else
5051 exp = rvalue (exp);
5053 else
5055 bool xval = !lvalue_p (exp);
5056 exp = cp_build_addr_expr (exp, tf_warning_or_error);
5057 init_expr = get_target_expr (exp);
5058 exp = TARGET_EXPR_SLOT (init_expr);
5059 exp = cp_build_fold_indirect_ref (exp);
5060 if (xval)
5061 exp = move (exp);
5063 *initp = init_expr;
5065 gcc_assert (!TREE_SIDE_EFFECTS (exp));
5066 return exp;
5069 /* Add NEW_EXPR, an expression whose value we don't care about, after the
5070 similar expression ORIG. */
5072 tree
5073 add_stmt_to_compound (tree orig, tree new_expr)
5075 if (!new_expr || !TREE_SIDE_EFFECTS (new_expr))
5076 return orig;
5077 if (!orig || !TREE_SIDE_EFFECTS (orig))
5078 return new_expr;
5079 return build2 (COMPOUND_EXPR, void_type_node, orig, new_expr);
5082 /* Like stabilize_expr, but for a call whose arguments we want to
5083 pre-evaluate. CALL is modified in place to use the pre-evaluated
5084 arguments, while, upon return, *INITP contains an expression to
5085 compute the arguments. */
5087 void
5088 stabilize_call (tree call, tree *initp)
5090 tree inits = NULL_TREE;
5091 int i;
5092 int nargs = call_expr_nargs (call);
5094 if (call == error_mark_node || processing_template_decl)
5096 *initp = NULL_TREE;
5097 return;
5100 gcc_assert (TREE_CODE (call) == CALL_EXPR);
5102 for (i = 0; i < nargs; i++)
5104 tree init;
5105 CALL_EXPR_ARG (call, i) =
5106 stabilize_expr (CALL_EXPR_ARG (call, i), &init);
5107 inits = add_stmt_to_compound (inits, init);
5110 *initp = inits;
5113 /* Like stabilize_expr, but for an AGGR_INIT_EXPR whose arguments we want
5114 to pre-evaluate. CALL is modified in place to use the pre-evaluated
5115 arguments, while, upon return, *INITP contains an expression to
5116 compute the arguments. */
5118 static void
5119 stabilize_aggr_init (tree call, tree *initp)
5121 tree inits = NULL_TREE;
5122 int i;
5123 int nargs = aggr_init_expr_nargs (call);
5125 if (call == error_mark_node)
5126 return;
5128 gcc_assert (TREE_CODE (call) == AGGR_INIT_EXPR);
5130 for (i = 0; i < nargs; i++)
5132 tree init;
5133 AGGR_INIT_EXPR_ARG (call, i) =
5134 stabilize_expr (AGGR_INIT_EXPR_ARG (call, i), &init);
5135 inits = add_stmt_to_compound (inits, init);
5138 *initp = inits;
5141 /* Like stabilize_expr, but for an initialization.
5143 If the initialization is for an object of class type, this function
5144 takes care not to introduce additional temporaries.
5146 Returns TRUE iff the expression was successfully pre-evaluated,
5147 i.e., if INIT is now side-effect free, except for, possibly, a
5148 single call to a constructor. */
5150 bool
5151 stabilize_init (tree init, tree *initp)
5153 tree t = init;
5155 *initp = NULL_TREE;
5157 if (t == error_mark_node || processing_template_decl)
5158 return true;
5160 if (TREE_CODE (t) == INIT_EXPR)
5161 t = TREE_OPERAND (t, 1);
5162 if (TREE_CODE (t) == TARGET_EXPR)
5163 t = TARGET_EXPR_INITIAL (t);
5165 /* If the RHS can be stabilized without breaking copy elision, stabilize
5166 it. We specifically don't stabilize class prvalues here because that
5167 would mean an extra copy, but they might be stabilized below. */
5168 if (TREE_CODE (init) == INIT_EXPR
5169 && TREE_CODE (t) != CONSTRUCTOR
5170 && TREE_CODE (t) != AGGR_INIT_EXPR
5171 && (SCALAR_TYPE_P (TREE_TYPE (t))
5172 || glvalue_p (t)))
5174 TREE_OPERAND (init, 1) = stabilize_expr (t, initp);
5175 return true;
5178 if (TREE_CODE (t) == COMPOUND_EXPR
5179 && TREE_CODE (init) == INIT_EXPR)
5181 tree last = expr_last (t);
5182 /* Handle stabilizing the EMPTY_CLASS_EXPR pattern. */
5183 if (!TREE_SIDE_EFFECTS (last))
5185 *initp = t;
5186 TREE_OPERAND (init, 1) = last;
5187 return true;
5191 if (TREE_CODE (t) == CONSTRUCTOR)
5193 /* Aggregate initialization: stabilize each of the field
5194 initializers. */
5195 unsigned i;
5196 constructor_elt *ce;
5197 bool good = true;
5198 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
5199 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
5201 tree type = TREE_TYPE (ce->value);
5202 tree subinit;
5203 if (TREE_CODE (type) == REFERENCE_TYPE
5204 || SCALAR_TYPE_P (type))
5205 ce->value = stabilize_expr (ce->value, &subinit);
5206 else if (!stabilize_init (ce->value, &subinit))
5207 good = false;
5208 *initp = add_stmt_to_compound (*initp, subinit);
5210 return good;
5213 if (TREE_CODE (t) == CALL_EXPR)
5215 stabilize_call (t, initp);
5216 return true;
5219 if (TREE_CODE (t) == AGGR_INIT_EXPR)
5221 stabilize_aggr_init (t, initp);
5222 return true;
5225 /* The initialization is being performed via a bitwise copy -- and
5226 the item copied may have side effects. */
5227 return !TREE_SIDE_EFFECTS (init);
5230 /* Returns true if a cast to TYPE may appear in an integral constant
5231 expression. */
5233 bool
5234 cast_valid_in_integral_constant_expression_p (tree type)
5236 return (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5237 || cxx_dialect >= cxx11
5238 || dependent_type_p (type)
5239 || type == error_mark_node);
5242 /* Return true if we need to fix linkage information of DECL. */
5244 static bool
5245 cp_fix_function_decl_p (tree decl)
5247 /* Skip if DECL is not externally visible. */
5248 if (!TREE_PUBLIC (decl))
5249 return false;
5251 /* We need to fix DECL if it a appears to be exported but with no
5252 function body. Thunks do not have CFGs and we may need to
5253 handle them specially later. */
5254 if (!gimple_has_body_p (decl)
5255 && !DECL_THUNK_P (decl)
5256 && !DECL_EXTERNAL (decl))
5258 struct cgraph_node *node = cgraph_node::get (decl);
5260 /* Don't fix same_body aliases. Although they don't have their own
5261 CFG, they share it with what they alias to. */
5262 if (!node || !node->alias
5263 || !vec_safe_length (node->ref_list.references))
5264 return true;
5267 return false;
5270 /* Clean the C++ specific parts of the tree T. */
5272 void
5273 cp_free_lang_data (tree t)
5275 if (TREE_CODE (t) == METHOD_TYPE
5276 || TREE_CODE (t) == FUNCTION_TYPE)
5278 /* Default args are not interesting anymore. */
5279 tree argtypes = TYPE_ARG_TYPES (t);
5280 while (argtypes)
5282 TREE_PURPOSE (argtypes) = 0;
5283 argtypes = TREE_CHAIN (argtypes);
5286 else if (TREE_CODE (t) == FUNCTION_DECL
5287 && cp_fix_function_decl_p (t))
5289 /* If T is used in this translation unit at all, the definition
5290 must exist somewhere else since we have decided to not emit it
5291 in this TU. So make it an external reference. */
5292 DECL_EXTERNAL (t) = 1;
5293 TREE_STATIC (t) = 0;
5295 if (TREE_CODE (t) == NAMESPACE_DECL)
5296 /* We do not need the leftover chaining of namespaces from the
5297 binding level. */
5298 DECL_CHAIN (t) = NULL_TREE;
5301 /* Stub for c-common. Please keep in sync with c-decl.c.
5302 FIXME: If address space support is target specific, then this
5303 should be a C target hook. But currently this is not possible,
5304 because this function is called via REGISTER_TARGET_PRAGMAS. */
5305 void
5306 c_register_addr_space (const char * /*word*/, addr_space_t /*as*/)
5310 /* Return the number of operands in T that we care about for things like
5311 mangling. */
5314 cp_tree_operand_length (const_tree t)
5316 enum tree_code code = TREE_CODE (t);
5318 if (TREE_CODE_CLASS (code) == tcc_vl_exp)
5319 return VL_EXP_OPERAND_LENGTH (t);
5321 return cp_tree_code_length (code);
5324 /* Like cp_tree_operand_length, but takes a tree_code CODE. */
5327 cp_tree_code_length (enum tree_code code)
5329 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
5331 switch (code)
5333 case PREINCREMENT_EXPR:
5334 case PREDECREMENT_EXPR:
5335 case POSTINCREMENT_EXPR:
5336 case POSTDECREMENT_EXPR:
5337 return 1;
5339 case ARRAY_REF:
5340 return 2;
5342 case EXPR_PACK_EXPANSION:
5343 return 1;
5345 default:
5346 return TREE_CODE_LENGTH (code);
5350 /* Implement -Wzero_as_null_pointer_constant. Return true if the
5351 conditions for the warning hold, false otherwise. */
5352 bool
5353 maybe_warn_zero_as_null_pointer_constant (tree expr, location_t loc)
5355 if (c_inhibit_evaluation_warnings == 0
5356 && !NULLPTR_TYPE_P (TREE_TYPE (expr)))
5358 warning_at (loc, OPT_Wzero_as_null_pointer_constant,
5359 "zero as null pointer constant");
5360 return true;
5362 return false;
5365 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
5366 /* Complain that some language-specific thing hanging off a tree
5367 node has been accessed improperly. */
5369 void
5370 lang_check_failed (const char* file, int line, const char* function)
5372 internal_error ("lang_* check: failed in %s, at %s:%d",
5373 function, trim_filename (file), line);
5375 #endif /* ENABLE_TREE_CHECKING */
5377 #if CHECKING_P
5379 namespace selftest {
5381 /* Verify that lvalue_kind () works, for various expressions,
5382 and that location wrappers don't affect the results. */
5384 static void
5385 test_lvalue_kind ()
5387 location_t loc = BUILTINS_LOCATION;
5389 /* Verify constants and parameters, without and with
5390 location wrappers. */
5391 tree int_cst = build_int_cst (integer_type_node, 42);
5392 ASSERT_EQ (clk_none, lvalue_kind (int_cst));
5394 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
5395 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
5396 ASSERT_EQ (clk_none, lvalue_kind (wrapped_int_cst));
5398 tree string_lit = build_string (4, "foo");
5399 TREE_TYPE (string_lit) = char_array_type_node;
5400 string_lit = fix_string_type (string_lit);
5401 ASSERT_EQ (clk_ordinary, lvalue_kind (string_lit));
5403 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
5404 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
5405 ASSERT_EQ (clk_ordinary, lvalue_kind (wrapped_string_lit));
5407 tree parm = build_decl (UNKNOWN_LOCATION, PARM_DECL,
5408 get_identifier ("some_parm"),
5409 integer_type_node);
5410 ASSERT_EQ (clk_ordinary, lvalue_kind (parm));
5412 tree wrapped_parm = maybe_wrap_with_location (parm, loc);
5413 ASSERT_TRUE (location_wrapper_p (wrapped_parm));
5414 ASSERT_EQ (clk_ordinary, lvalue_kind (wrapped_parm));
5416 /* Verify that lvalue_kind of std::move on a parm isn't
5417 affected by location wrappers. */
5418 tree rvalue_ref_of_parm = move (parm);
5419 ASSERT_EQ (clk_rvalueref, lvalue_kind (rvalue_ref_of_parm));
5420 tree rvalue_ref_of_wrapped_parm = move (wrapped_parm);
5421 ASSERT_EQ (clk_rvalueref, lvalue_kind (rvalue_ref_of_wrapped_parm));
5424 /* Run all of the selftests within this file. */
5426 void
5427 cp_tree_c_tests ()
5429 test_lvalue_kind ();
5432 } // namespace selftest
5434 #endif /* #if CHECKING_P */
5437 #include "gt-cp-tree.h"