1 /* Perform -*- C++ -*- constant expression evaluation, including calls to
2 constexpr functions. These routines are used both during actual parsing
3 and during the instantiation of template functions.
5 Copyright (C) 1998-2019 Free Software Foundation, Inc.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 GCC is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
25 #include "coretypes.h"
28 #include "c-family/c-objc.h"
29 #include "tree-iterator.h"
32 #include "tree-inline.h"
34 #include "gimple-fold.h"
36 #include "fold-const-call.h"
38 static bool verify_constant (tree
, bool, bool *, bool *);
39 #define VERIFY_CONSTANT(X) \
41 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
45 static HOST_WIDE_INT
find_array_ctor_elt (tree ary
, tree dindex
,
48 /* Returns true iff FUN is an instantiation of a constexpr function
49 template or a defaulted constexpr function. */
52 is_instantiation_of_constexpr (tree fun
)
54 return ((DECL_TEMPLOID_INSTANTIATION (fun
)
55 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun
)))
56 || (DECL_DEFAULTED_FN (fun
)
57 && DECL_DECLARED_CONSTEXPR_P (fun
)));
60 /* Return true if T is a literal type. */
63 literal_type_p (tree t
)
68 || (VOID_TYPE_P (t
) && cxx_dialect
>= cxx14
))
72 t
= complete_type (t
);
73 gcc_assert (COMPLETE_TYPE_P (t
) || errorcount
);
74 return CLASSTYPE_LITERAL_P (t
);
76 if (TREE_CODE (t
) == ARRAY_TYPE
)
77 return literal_type_p (strip_array_types (t
));
81 /* If DECL is a variable declared `constexpr', require its type
82 be literal. Return error_mark_node if we give an error, the
86 ensure_literal_type_for_constexpr_object (tree decl
)
88 tree type
= TREE_TYPE (decl
);
90 && (DECL_DECLARED_CONSTEXPR_P (decl
)
91 || var_in_constexpr_fn (decl
))
92 && !processing_template_decl
)
94 tree stype
= strip_array_types (type
);
95 if (CLASS_TYPE_P (stype
) && !COMPLETE_TYPE_P (complete_type (stype
)))
96 /* Don't complain here, we'll complain about incompleteness
97 when we try to initialize the variable. */;
98 else if (type_uses_auto (type
))
99 /* We don't know the actual type yet. */;
100 else if (!literal_type_p (type
))
102 if (DECL_DECLARED_CONSTEXPR_P (decl
))
104 auto_diagnostic_group d
;
105 error_at (DECL_SOURCE_LOCATION (decl
),
106 "the type %qT of %<constexpr%> variable %qD "
107 "is not literal", type
, decl
);
108 explain_non_literal_class (type
);
109 decl
= error_mark_node
;
113 if (!is_instantiation_of_constexpr (current_function_decl
))
115 auto_diagnostic_group d
;
116 error_at (DECL_SOURCE_LOCATION (decl
),
117 "variable %qD of non-literal type %qT in "
118 "%<constexpr%> function", decl
, type
);
119 explain_non_literal_class (type
);
120 decl
= error_mark_node
;
122 cp_function_chain
->invalid_constexpr
= true;
125 else if (DECL_DECLARED_CONSTEXPR_P (decl
)
126 && variably_modified_type_p (type
, NULL_TREE
))
128 error_at (DECL_SOURCE_LOCATION (decl
),
129 "%<constexpr%> variable %qD has variably-modified "
130 "type %qT", decl
, type
);
131 decl
= error_mark_node
;
137 /* Representation of entries in the constexpr function definition table. */
139 struct GTY((for_user
)) constexpr_fundef
{
146 struct constexpr_fundef_hasher
: ggc_ptr_hash
<constexpr_fundef
>
148 static hashval_t
hash (constexpr_fundef
*);
149 static bool equal (constexpr_fundef
*, constexpr_fundef
*);
152 /* This table holds all constexpr function definitions seen in
153 the current translation unit. */
155 static GTY (()) hash_table
<constexpr_fundef_hasher
> *constexpr_fundef_table
;
157 /* Utility function used for managing the constexpr function table.
158 Return true if the entries pointed to by P and Q are for the
159 same constexpr function. */
162 constexpr_fundef_hasher::equal (constexpr_fundef
*lhs
, constexpr_fundef
*rhs
)
164 return lhs
->decl
== rhs
->decl
;
167 /* Utility function used for managing the constexpr function table.
168 Return a hash value for the entry pointed to by Q. */
171 constexpr_fundef_hasher::hash (constexpr_fundef
*fundef
)
173 return DECL_UID (fundef
->decl
);
176 /* Return a previously saved definition of function FUN. */
178 static constexpr_fundef
*
179 retrieve_constexpr_fundef (tree fun
)
181 if (constexpr_fundef_table
== NULL
)
184 constexpr_fundef fundef
= { fun
, NULL
, NULL
, NULL
};
185 return constexpr_fundef_table
->find (&fundef
);
188 /* Check whether the parameter and return types of FUN are valid for a
189 constexpr function, and complain if COMPLAIN. */
192 is_valid_constexpr_fn (tree fun
, bool complain
)
196 if (DECL_INHERITED_CTOR (fun
)
197 && TREE_CODE (fun
) == TEMPLATE_DECL
)
201 error ("inherited constructor %qD is not %<constexpr%>",
202 DECL_INHERITED_CTOR (fun
));
206 for (tree parm
= FUNCTION_FIRST_USER_PARM (fun
);
207 parm
!= NULL_TREE
; parm
= TREE_CHAIN (parm
))
208 if (!literal_type_p (TREE_TYPE (parm
)))
213 auto_diagnostic_group d
;
214 error ("invalid type for parameter %d of %<constexpr%> "
215 "function %q+#D", DECL_PARM_INDEX (parm
), fun
);
216 explain_non_literal_class (TREE_TYPE (parm
));
221 if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun
)) && cxx_dialect
< cxx17
)
225 inform (DECL_SOURCE_LOCATION (fun
),
226 "lambdas are implicitly %<constexpr%> only in C++17 and later");
228 else if (!DECL_CONSTRUCTOR_P (fun
))
230 tree rettype
= TREE_TYPE (TREE_TYPE (fun
));
231 if (!literal_type_p (rettype
))
236 auto_diagnostic_group d
;
237 error ("invalid return type %qT of %<constexpr%> function %q+D",
239 explain_non_literal_class (rettype
);
243 /* C++14 DR 1684 removed this restriction. */
244 if (cxx_dialect
< cxx14
245 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
246 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun
)))
251 auto_diagnostic_group d
;
252 if (pedwarn (DECL_SOURCE_LOCATION (fun
), OPT_Wpedantic
,
253 "enclosing class of %<constexpr%> non-static"
254 " member function %q+#D is not a literal type",
256 explain_non_literal_class (DECL_CONTEXT (fun
));
260 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun
)))
264 error ("%q#T has virtual base classes", DECL_CONTEXT (fun
));
270 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
271 for a member of an anonymous aggregate, INIT is the initializer for that
272 member, and VEC_OUTER is the vector of constructor elements for the class
273 whose constructor we are processing. Add the initializer to the vector
274 and return true to indicate success. */
277 build_anon_member_initialization (tree member
, tree init
,
278 vec
<constructor_elt
, va_gc
> **vec_outer
)
280 /* MEMBER presents the relevant fields from the inside out, but we need
281 to build up the initializer from the outside in so that we can reuse
282 previously built CONSTRUCTORs if this is, say, the second field in an
283 anonymous struct. So we use a vec as a stack. */
284 auto_vec
<tree
, 2> fields
;
287 fields
.safe_push (TREE_OPERAND (member
, 1));
288 member
= TREE_OPERAND (member
, 0);
290 while (ANON_AGGR_TYPE_P (TREE_TYPE (member
))
291 && TREE_CODE (member
) == COMPONENT_REF
);
293 /* VEC has the constructor elements vector for the context of FIELD.
294 If FIELD is an anonymous aggregate, we will push inside it. */
295 vec
<constructor_elt
, va_gc
> **vec
= vec_outer
;
297 while (field
= fields
.pop(),
298 ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
301 /* If there is already an outer constructor entry for the anonymous
302 aggregate FIELD, use it; otherwise, insert one. */
303 if (vec_safe_is_empty (*vec
)
304 || (*vec
)->last().index
!= field
)
306 ctor
= build_constructor (TREE_TYPE (field
), NULL
);
307 CONSTRUCTOR_APPEND_ELT (*vec
, field
, ctor
);
310 ctor
= (*vec
)->last().value
;
311 vec
= &CONSTRUCTOR_ELTS (ctor
);
314 /* Now we're at the innermost field, the one that isn't an anonymous
315 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
316 gcc_assert (fields
.is_empty());
317 CONSTRUCTOR_APPEND_ELT (*vec
, field
, init
);
322 /* Subroutine of build_constexpr_constructor_member_initializers.
323 The expression tree T represents a data member initialization
324 in a (constexpr) constructor definition. Build a pairing of
325 the data member with its initializer, and prepend that pair
326 to the existing initialization pair INITS. */
329 build_data_member_initialization (tree t
, vec
<constructor_elt
, va_gc
> **vec
)
332 if (TREE_CODE (t
) == CLEANUP_POINT_EXPR
)
333 t
= TREE_OPERAND (t
, 0);
334 if (TREE_CODE (t
) == EXPR_STMT
)
335 t
= TREE_OPERAND (t
, 0);
336 if (t
== error_mark_node
)
338 if (TREE_CODE (t
) == STATEMENT_LIST
)
340 tree_stmt_iterator i
;
341 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
343 if (! build_data_member_initialization (tsi_stmt (i
), vec
))
348 if (TREE_CODE (t
) == CLEANUP_STMT
)
350 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
351 but we can in a constexpr constructor for a non-literal class. Just
352 ignore it; either all the initialization will be constant, in which
353 case the cleanup can't run, or it can't be constexpr.
354 Still recurse into CLEANUP_BODY. */
355 return build_data_member_initialization (CLEANUP_BODY (t
), vec
);
357 if (TREE_CODE (t
) == CONVERT_EXPR
)
358 t
= TREE_OPERAND (t
, 0);
359 if (TREE_CODE (t
) == INIT_EXPR
360 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
361 use what this function builds for cx_check_missing_mem_inits, and
362 assignment in the ctor body doesn't count. */
363 || (cxx_dialect
< cxx14
&& TREE_CODE (t
) == MODIFY_EXPR
))
365 member
= TREE_OPERAND (t
, 0);
366 init
= break_out_target_exprs (TREE_OPERAND (t
, 1));
368 else if (TREE_CODE (t
) == CALL_EXPR
)
370 tree fn
= get_callee_fndecl (t
);
371 if (!fn
|| !DECL_CONSTRUCTOR_P (fn
))
372 /* We're only interested in calls to subobject constructors. */
374 member
= CALL_EXPR_ARG (t
, 0);
375 /* We don't use build_cplus_new here because it complains about
376 abstract bases. Leaving the call unwrapped means that it has the
377 wrong type, but cxx_eval_constant_expression doesn't care. */
378 init
= break_out_target_exprs (t
);
380 else if (TREE_CODE (t
) == BIND_EXPR
)
381 return build_data_member_initialization (BIND_EXPR_BODY (t
), vec
);
383 /* Don't add anything else to the CONSTRUCTOR. */
385 if (INDIRECT_REF_P (member
))
386 member
= TREE_OPERAND (member
, 0);
387 if (TREE_CODE (member
) == NOP_EXPR
)
391 if (TREE_CODE (op
) == ADDR_EXPR
)
393 gcc_assert (same_type_ignoring_top_level_qualifiers_p
394 (TREE_TYPE (TREE_TYPE (op
)),
395 TREE_TYPE (TREE_TYPE (member
))));
396 /* Initializing a cv-qualified member; we need to look through
400 else if (op
== current_class_ptr
401 && (same_type_ignoring_top_level_qualifiers_p
402 (TREE_TYPE (TREE_TYPE (member
)),
403 current_class_type
)))
404 /* Delegating constructor. */
408 /* This is an initializer for an empty base; keep it for now so
409 we can check it in cxx_eval_bare_aggregate. */
410 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member
))));
413 if (TREE_CODE (member
) == ADDR_EXPR
)
414 member
= TREE_OPERAND (member
, 0);
415 if (TREE_CODE (member
) == COMPONENT_REF
)
417 tree aggr
= TREE_OPERAND (member
, 0);
418 if (TREE_CODE (aggr
) == VAR_DECL
)
419 /* Initializing a local variable, don't add anything. */
421 if (TREE_CODE (aggr
) != COMPONENT_REF
)
422 /* Normal member initialization. */
423 member
= TREE_OPERAND (member
, 1);
424 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr
)))
425 /* Initializing a member of an anonymous union. */
426 return build_anon_member_initialization (member
, init
, vec
);
428 /* We're initializing a vtable pointer in a base. Leave it as
429 COMPONENT_REF so we remember the path to get to the vfield. */
430 gcc_assert (TREE_TYPE (member
) == vtbl_ptr_type_node
);
433 /* Value-initialization can produce multiple initializers for the
434 same field; use the last one. */
435 if (!vec_safe_is_empty (*vec
) && (*vec
)->last().index
== member
)
436 (*vec
)->last().value
= init
;
438 CONSTRUCTOR_APPEND_ELT (*vec
, member
, init
);
442 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
443 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
444 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
447 check_constexpr_bind_expr_vars (tree t
)
449 gcc_assert (TREE_CODE (t
) == BIND_EXPR
);
451 for (tree var
= BIND_EXPR_VARS (t
); var
; var
= DECL_CHAIN (var
))
452 if (TREE_CODE (var
) == TYPE_DECL
453 && DECL_IMPLICIT_TYPEDEF_P (var
)
454 && !LAMBDA_TYPE_P (TREE_TYPE (var
)))
459 /* Subroutine of check_constexpr_ctor_body. */
462 check_constexpr_ctor_body_1 (tree last
, tree list
)
464 switch (TREE_CODE (list
))
467 if (TREE_CODE (DECL_EXPR_DECL (list
)) == USING_DECL
468 || TREE_CODE (DECL_EXPR_DECL (list
)) == TYPE_DECL
)
472 case CLEANUP_POINT_EXPR
:
473 return check_constexpr_ctor_body (last
, TREE_OPERAND (list
, 0),
477 if (!check_constexpr_bind_expr_vars (list
)
478 || !check_constexpr_ctor_body (last
, BIND_EXPR_BODY (list
),
485 case DEBUG_BEGIN_STMT
:
493 /* Make sure that there are no statements after LAST in the constructor
494 body represented by LIST. */
497 check_constexpr_ctor_body (tree last
, tree list
, bool complain
)
499 /* C++14 doesn't require a constexpr ctor to have an empty body. */
500 if (cxx_dialect
>= cxx14
)
504 if (TREE_CODE (list
) == STATEMENT_LIST
)
506 tree_stmt_iterator i
= tsi_last (list
);
507 for (; !tsi_end_p (i
); tsi_prev (&i
))
509 tree t
= tsi_stmt (i
);
512 if (!check_constexpr_ctor_body_1 (last
, t
))
519 else if (list
!= last
520 && !check_constexpr_ctor_body_1 (last
, list
))
525 error ("%<constexpr%> constructor does not have empty body");
526 DECL_DECLARED_CONSTEXPR_P (current_function_decl
) = false;
531 /* V is a vector of constructor elements built up for the base and member
532 initializers of a constructor for TYPE. They need to be in increasing
533 offset order, which they might not be yet if TYPE has a primary base
534 which is not first in the base-clause or a vptr and at least one base
535 all of which are non-primary. */
537 static vec
<constructor_elt
, va_gc
> *
538 sort_constexpr_mem_initializers (tree type
, vec
<constructor_elt
, va_gc
> *v
)
540 tree pri
= CLASSTYPE_PRIMARY_BINFO (type
);
546 field_type
= BINFO_TYPE (pri
);
547 else if (TYPE_CONTAINS_VPTR_P (type
))
548 field_type
= vtbl_ptr_type_node
;
552 /* Find the element for the primary base or vptr and move it to the
553 beginning of the vec. */
554 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
555 if (TREE_TYPE (ce
->index
) == field_type
)
558 if (i
> 0 && i
< vec_safe_length (v
))
560 vec
<constructor_elt
, va_gc
> &vref
= *v
;
561 constructor_elt elt
= vref
[i
];
570 /* Build compile-time evalable representations of member-initializer list
571 for a constexpr constructor. */
574 build_constexpr_constructor_member_initializers (tree type
, tree body
)
576 vec
<constructor_elt
, va_gc
> *vec
= NULL
;
579 switch (TREE_CODE (body
))
581 case MUST_NOT_THROW_EXPR
:
583 body
= TREE_OPERAND (body
, 0);
587 for (tree_stmt_iterator i
= tsi_start (body
);
588 !tsi_end_p (i
); tsi_next (&i
))
591 if (TREE_CODE (body
) == BIND_EXPR
)
597 body
= BIND_EXPR_BODY (body
);
604 if (TREE_CODE (body
) == TRY_BLOCK
)
606 body
= TREE_OPERAND (body
, 0);
607 if (TREE_CODE (body
) == BIND_EXPR
)
608 body
= BIND_EXPR_BODY (body
);
610 if (TREE_CODE (body
) == CLEANUP_POINT_EXPR
)
612 body
= TREE_OPERAND (body
, 0);
613 if (TREE_CODE (body
) == EXPR_STMT
)
614 body
= TREE_OPERAND (body
, 0);
615 if (TREE_CODE (body
) == INIT_EXPR
616 && (same_type_ignoring_top_level_qualifiers_p
617 (TREE_TYPE (TREE_OPERAND (body
, 0)),
618 current_class_type
)))
621 return TREE_OPERAND (body
, 1);
623 ok
= build_data_member_initialization (body
, &vec
);
625 else if (TREE_CODE (body
) == STATEMENT_LIST
)
627 tree_stmt_iterator i
;
628 for (i
= tsi_start (body
); !tsi_end_p (i
); tsi_next (&i
))
630 ok
= build_data_member_initialization (tsi_stmt (i
), &vec
);
635 else if (EXPR_P (body
))
636 ok
= build_data_member_initialization (body
, &vec
);
638 gcc_assert (errorcount
> 0);
641 if (vec_safe_length (vec
) > 0)
643 /* In a delegating constructor, return the target. */
644 constructor_elt
*ce
= &(*vec
)[0];
645 if (ce
->index
== current_class_ptr
)
652 vec
= sort_constexpr_mem_initializers (type
, vec
);
653 return build_constructor (type
, vec
);
656 return error_mark_node
;
659 /* We have an expression tree T that represents a call, either CALL_EXPR
660 or AGGR_INIT_EXPR. If the call is lexically to a named function,
661 retrun the _DECL for that function. */
664 get_function_named_in_call (tree t
)
666 tree fun
= cp_get_callee (t
);
667 if (fun
&& TREE_CODE (fun
) == ADDR_EXPR
668 && TREE_CODE (TREE_OPERAND (fun
, 0)) == FUNCTION_DECL
)
669 fun
= TREE_OPERAND (fun
, 0);
673 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
674 declared to be constexpr, or a sub-statement thereof. Returns the
675 return value if suitable, error_mark_node for a statement not allowed in
676 a constexpr function, or NULL_TREE if no return value was found. */
679 constexpr_fn_retval (tree body
)
681 switch (TREE_CODE (body
))
685 tree_stmt_iterator i
;
686 tree expr
= NULL_TREE
;
687 for (i
= tsi_start (body
); !tsi_end_p (i
); tsi_next (&i
))
689 tree s
= constexpr_fn_retval (tsi_stmt (i
));
690 if (s
== error_mark_node
)
691 return error_mark_node
;
692 else if (s
== NULL_TREE
)
693 /* Keep iterating. */;
695 /* Multiple return statements. */
696 return error_mark_node
;
704 return break_out_target_exprs (TREE_OPERAND (body
, 0));
708 tree decl
= DECL_EXPR_DECL (body
);
709 if (TREE_CODE (decl
) == USING_DECL
710 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
711 || DECL_ARTIFICIAL (decl
))
713 return error_mark_node
;
716 case CLEANUP_POINT_EXPR
:
717 return constexpr_fn_retval (TREE_OPERAND (body
, 0));
720 if (!check_constexpr_bind_expr_vars (body
))
721 return error_mark_node
;
722 return constexpr_fn_retval (BIND_EXPR_BODY (body
));
725 case DEBUG_BEGIN_STMT
:
730 tree fun
= get_function_named_in_call (body
);
732 && fndecl_built_in_p (fun
, BUILT_IN_UNREACHABLE
))
738 return error_mark_node
;
742 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
743 FUN; do the necessary transformations to turn it into a single expression
744 that we can store in the hash table. */
747 massage_constexpr_body (tree fun
, tree body
)
749 if (DECL_CONSTRUCTOR_P (fun
))
750 body
= build_constexpr_constructor_member_initializers
751 (DECL_CONTEXT (fun
), body
);
752 else if (cxx_dialect
< cxx14
)
754 if (TREE_CODE (body
) == EH_SPEC_BLOCK
)
755 body
= EH_SPEC_STMTS (body
);
756 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
757 body
= TREE_OPERAND (body
, 0);
758 body
= constexpr_fn_retval (body
);
763 /* CTYPE is a type constructed from BODY. Return true if some
764 bases/fields are uninitialized, and complain if COMPLAIN. */
767 cx_check_missing_mem_inits (tree ctype
, tree body
, bool complain
)
773 if (TREE_CODE (body
) != CONSTRUCTOR
)
775 nelts
= CONSTRUCTOR_NELTS (body
);
777 tree field
= TYPE_FIELDS (ctype
);
779 if (TREE_CODE (ctype
) == UNION_TYPE
)
781 if (nelts
== 0 && next_initializable_field (field
))
784 error ("%<constexpr%> constructor for union %qT must "
785 "initialize exactly one non-static data member", ctype
);
791 /* Iterate over the CONSTRUCTOR, checking any missing fields don't
792 need an explicit initialization. */
794 for (unsigned i
= 0; i
<= nelts
; ++i
)
796 tree index
= NULL_TREE
;
799 index
= CONSTRUCTOR_ELT (body
, i
)->index
;
800 /* Skip base and vtable inits. */
801 if (TREE_CODE (index
) != FIELD_DECL
802 || DECL_ARTIFICIAL (index
))
806 for (; field
!= index
; field
= DECL_CHAIN (field
))
809 if (TREE_CODE (field
) != FIELD_DECL
)
811 if (DECL_UNNAMED_BIT_FIELD (field
))
813 if (DECL_ARTIFICIAL (field
))
815 if (ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
817 /* Recurse to check the anonummous aggregate member. */
818 bad
|= cx_check_missing_mem_inits
819 (TREE_TYPE (field
), NULL_TREE
, complain
);
820 if (bad
&& !complain
)
824 ftype
= strip_array_types (TREE_TYPE (field
));
825 if (type_has_constexpr_default_constructor (ftype
))
827 /* It's OK to skip a member with a trivial constexpr ctor.
828 A constexpr ctor that isn't trivial should have been
830 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype
)
836 auto_diagnostic_group d
;
837 error ("member %qD must be initialized by mem-initializer "
838 "in %<constexpr%> constructor", field
);
839 inform (DECL_SOURCE_LOCATION (field
), "declared here");
842 if (field
== NULL_TREE
)
845 if (ANON_AGGR_TYPE_P (TREE_TYPE (index
)))
847 /* Check the anonymous aggregate initializer is valid. */
848 bad
|= cx_check_missing_mem_inits
849 (TREE_TYPE (index
), CONSTRUCTOR_ELT (body
, i
)->value
, complain
);
850 if (bad
&& !complain
)
853 field
= DECL_CHAIN (field
);
859 /* We are processing the definition of the constexpr function FUN.
860 Check that its BODY fulfills the propriate requirements and
861 enter it in the constexpr function definition table.
862 For constructor BODY is actually the TREE_LIST of the
863 member-initializer list. */
866 register_constexpr_fundef (tree fun
, tree body
)
868 constexpr_fundef entry
;
869 constexpr_fundef
**slot
;
871 if (!is_valid_constexpr_fn (fun
, !DECL_GENERATED_P (fun
)))
874 tree massaged
= massage_constexpr_body (fun
, body
);
875 if (massaged
== NULL_TREE
|| massaged
== error_mark_node
)
877 if (!DECL_CONSTRUCTOR_P (fun
))
878 error ("body of %<constexpr%> function %qD not a return-statement",
883 if (!potential_rvalue_constant_expression (massaged
))
885 if (!DECL_GENERATED_P (fun
))
886 require_potential_rvalue_constant_expression (massaged
);
890 if (DECL_CONSTRUCTOR_P (fun
)
891 && cx_check_missing_mem_inits (DECL_CONTEXT (fun
),
892 massaged
, !DECL_GENERATED_P (fun
)))
895 /* Create the constexpr function table if necessary. */
896 if (constexpr_fundef_table
== NULL
)
897 constexpr_fundef_table
898 = hash_table
<constexpr_fundef_hasher
>::create_ggc (101);
901 tree saved_fn
= current_function_decl
;
902 bool clear_ctx
= false;
903 current_function_decl
= fun
;
904 if (DECL_RESULT (fun
) && DECL_CONTEXT (DECL_RESULT (fun
)) == NULL_TREE
)
907 DECL_CONTEXT (DECL_RESULT (fun
)) = fun
;
909 entry
.body
= copy_fn (fun
, entry
.parms
, entry
.result
);
910 current_function_decl
= saved_fn
;
911 slot
= constexpr_fundef_table
->find_slot (&entry
, INSERT
);
913 DECL_CONTEXT (DECL_RESULT (fun
)) = NULL_TREE
;
915 gcc_assert (*slot
== NULL
);
916 *slot
= ggc_alloc
<constexpr_fundef
> ();
922 /* FUN is a non-constexpr function called in a context that requires a
923 constant expression. If it comes from a constexpr template, explain why
924 the instantiation isn't constexpr. */
927 explain_invalid_constexpr_fn (tree fun
)
929 static hash_set
<tree
> *diagnosed
;
932 /* Only diagnose defaulted functions, lambdas, or instantiations. */
933 if (!DECL_DEFAULTED_FN (fun
)
934 && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun
))
935 && !is_instantiation_of_constexpr (fun
))
937 if (diagnosed
== NULL
)
938 diagnosed
= new hash_set
<tree
>;
939 if (diagnosed
->add (fun
))
940 /* Already explained. */
943 save_loc
= input_location
;
944 if (!lambda_static_thunk_p (fun
))
946 /* Diagnostics should completely ignore the static thunk, so leave
947 input_location set to our caller's location. */
948 input_location
= DECL_SOURCE_LOCATION (fun
);
949 inform (input_location
,
950 "%qD is not usable as a %<constexpr%> function because:", fun
);
952 /* First check the declaration. */
953 if (is_valid_constexpr_fn (fun
, true))
955 /* Then if it's OK, the body. */
956 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
957 && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun
)))
958 explain_implicit_non_constexpr (fun
);
961 body
= massage_constexpr_body (fun
, DECL_SAVED_TREE (fun
));
962 require_potential_rvalue_constant_expression (body
);
963 if (DECL_CONSTRUCTOR_P (fun
))
964 cx_check_missing_mem_inits (DECL_CONTEXT (fun
), body
, true);
967 input_location
= save_loc
;
970 /* Objects of this type represent calls to constexpr functions
971 along with the bindings of parameters to their arguments, for
972 the purpose of compile time evaluation. */
974 struct GTY((for_user
)) constexpr_call
{
975 /* Description of the constexpr function definition. */
976 constexpr_fundef
*fundef
;
977 /* Parameter bindings environment. A TREE_VEC of arguments. */
979 /* Result of the call.
980 NULL means the call is being evaluated.
981 error_mark_node means that the evaluation was erroneous;
982 otherwise, the actuall value of the call. */
984 /* The hash of this call; we remember it here to avoid having to
985 recalculate it when expanding the hash table. */
987 /* Whether __builtin_is_constant_evaluated() should evaluate to true. */
988 bool manifestly_const_eval
;
991 struct constexpr_call_hasher
: ggc_ptr_hash
<constexpr_call
>
993 static hashval_t
hash (constexpr_call
*);
994 static bool equal (constexpr_call
*, constexpr_call
*);
997 enum constexpr_switch_state
{
998 /* Used when processing a switch for the first time by cxx_eval_switch_expr
999 and default: label for that switch has not been seen yet. */
1000 css_default_not_seen
,
1001 /* Used when processing a switch for the first time by cxx_eval_switch_expr
1002 and default: label for that switch has been seen already. */
1004 /* Used when processing a switch for the second time by
1005 cxx_eval_switch_expr, where default: label should match. */
1006 css_default_processing
1009 /* The constexpr expansion context. CALL is the current function
1010 expansion, CTOR is the current aggregate initializer, OBJECT is the
1011 object being initialized by CTOR, either a VAR_DECL or a _REF. VALUES
1012 is a map of values of variables initialized within the expression. */
1014 struct constexpr_ctx
{
1015 /* The innermost call we're evaluating. */
1016 constexpr_call
*call
;
1017 /* Values for any temporaries or local variables within the
1018 constant-expression. */
1019 hash_map
<tree
,tree
> *values
;
1020 /* SAVE_EXPRs that we've seen within the current LOOP_EXPR. NULL if we
1021 aren't inside a loop. */
1022 vec
<tree
> *save_exprs
;
1023 /* The CONSTRUCTOR we're currently building up for an aggregate
1026 /* The object we're building the CONSTRUCTOR for. */
1028 /* If inside SWITCH_EXPR. */
1029 constexpr_switch_state
*css_state
;
1030 /* Number of cxx_eval_constant_expression calls (except skipped ones,
1031 on simple constants or location wrappers) encountered during current
1032 cxx_eval_outermost_constant_expr call. */
1033 HOST_WIDE_INT
*constexpr_ops_count
;
1035 /* Whether we should error on a non-constant expression or fail quietly. */
1037 /* Whether we are strictly conforming to constant expression rules or
1038 trying harder to get a constant value. */
1040 /* Whether __builtin_is_constant_evaluated () should be true. */
1041 bool manifestly_const_eval
;
1044 /* A table of all constexpr calls that have been evaluated by the
1045 compiler in this translation unit. */
1047 static GTY (()) hash_table
<constexpr_call_hasher
> *constexpr_call_table
;
1049 static tree
cxx_eval_constant_expression (const constexpr_ctx
*, tree
,
1050 bool, bool *, bool *, tree
* = NULL
);
1052 /* Compute a hash value for a constexpr call representation. */
1055 constexpr_call_hasher::hash (constexpr_call
*info
)
1060 /* Return true if the objects pointed to by P and Q represent calls
1061 to the same constexpr function with the same arguments.
1062 Otherwise, return false. */
1065 constexpr_call_hasher::equal (constexpr_call
*lhs
, constexpr_call
*rhs
)
1069 if (lhs
->hash
!= rhs
->hash
)
1071 if (lhs
->manifestly_const_eval
!= rhs
->manifestly_const_eval
)
1073 if (!constexpr_fundef_hasher::equal (lhs
->fundef
, rhs
->fundef
))
1075 return cp_tree_equal (lhs
->bindings
, rhs
->bindings
);
1078 /* Initialize the constexpr call table, if needed. */
1081 maybe_initialize_constexpr_call_table (void)
1083 if (constexpr_call_table
== NULL
)
1084 constexpr_call_table
= hash_table
<constexpr_call_hasher
>::create_ggc (101);
1087 /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1088 a function happens to get called recursively, we unshare the callee
1089 function's body and evaluate this unshared copy instead of evaluating the
1092 FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1093 copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
1094 that's keyed off of the original FUNCTION_DECL and whose value is a
1095 TREE_LIST of this function's unused copies awaiting reuse.
1097 This is not GC-deletable to avoid GC affecting UID generation. */
1099 static GTY(()) hash_map
<tree
, tree
> *fundef_copies_table
;
1101 /* Initialize FUNDEF_COPIES_TABLE if it's not initialized. */
1104 maybe_initialize_fundef_copies_table ()
1106 if (fundef_copies_table
== NULL
)
1107 fundef_copies_table
= hash_map
<tree
,tree
>::create_ggc (101);
1110 /* Reuse a copy or create a new unshared copy of the function FUN.
1111 Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1112 is parms, TYPE is result. */
1115 get_fundef_copy (constexpr_fundef
*fundef
)
1117 maybe_initialize_fundef_copies_table ();
1121 tree
*slot
= &fundef_copies_table
->get_or_insert (fundef
->decl
, &existed
);
1125 /* There is no cached function available, or in use. We can use
1126 the function directly. That the slot is now created records
1127 that this function is now in use. */
1128 copy
= build_tree_list (fundef
->body
, fundef
->parms
);
1129 TREE_TYPE (copy
) = fundef
->result
;
1131 else if (*slot
== NULL_TREE
)
1133 /* We've already used the function itself, so make a copy. */
1134 copy
= build_tree_list (NULL
, NULL
);
1135 tree saved_body
= DECL_SAVED_TREE (fundef
->decl
);
1136 tree saved_parms
= DECL_ARGUMENTS (fundef
->decl
);
1137 tree saved_result
= DECL_RESULT (fundef
->decl
);
1138 tree saved_fn
= current_function_decl
;
1139 DECL_SAVED_TREE (fundef
->decl
) = fundef
->body
;
1140 DECL_ARGUMENTS (fundef
->decl
) = fundef
->parms
;
1141 DECL_RESULT (fundef
->decl
) = fundef
->result
;
1142 current_function_decl
= fundef
->decl
;
1143 TREE_PURPOSE (copy
) = copy_fn (fundef
->decl
, TREE_VALUE (copy
),
1145 current_function_decl
= saved_fn
;
1146 DECL_RESULT (fundef
->decl
) = saved_result
;
1147 DECL_ARGUMENTS (fundef
->decl
) = saved_parms
;
1148 DECL_SAVED_TREE (fundef
->decl
) = saved_body
;
1152 /* We have a cached function available. */
1154 *slot
= TREE_CHAIN (copy
);
1160 /* Save the copy COPY of function FUN for later reuse by
1161 get_fundef_copy(). By construction, there will always be an entry
1165 save_fundef_copy (tree fun
, tree copy
)
1167 tree
*slot
= fundef_copies_table
->get (fun
);
1168 TREE_CHAIN (copy
) = *slot
;
1172 /* We have an expression tree T that represents a call, either CALL_EXPR
1173 or AGGR_INIT_EXPR. Return the Nth argument. */
1176 get_nth_callarg (tree t
, int n
)
1178 switch (TREE_CODE (t
))
1181 return CALL_EXPR_ARG (t
, n
);
1183 case AGGR_INIT_EXPR
:
1184 return AGGR_INIT_EXPR_ARG (t
, n
);
1192 /* Attempt to evaluate T which represents a call to a builtin function.
1193 We assume here that all builtin functions evaluate to scalar types
1194 represented by _CST nodes. */
1197 cxx_eval_builtin_function_call (const constexpr_ctx
*ctx
, tree t
, tree fun
,
1199 bool *non_constant_p
, bool *overflow_p
)
1201 const int nargs
= call_expr_nargs (t
);
1202 tree
*args
= (tree
*) alloca (nargs
* sizeof (tree
));
1206 /* Don't fold __builtin_constant_p within a constexpr function. */
1207 bool bi_const_p
= DECL_IS_BUILTIN_CONSTANT_P (fun
);
1209 /* If we aren't requiring a constant expression, defer __builtin_constant_p
1210 in a constexpr function until we have values for the parameters. */
1212 && !ctx
->manifestly_const_eval
1213 && current_function_decl
1214 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
1216 *non_constant_p
= true;
1220 /* For __builtin_is_constant_evaluated, defer it if not
1221 ctx->manifestly_const_eval, otherwise fold it to true. */
1222 if (fndecl_built_in_p (fun
, CP_BUILT_IN_IS_CONSTANT_EVALUATED
,
1225 if (!ctx
->manifestly_const_eval
)
1227 *non_constant_p
= true;
1230 return boolean_true_node
;
1233 /* Be permissive for arguments to built-ins; __builtin_constant_p should
1234 return constant false for a non-constant argument. */
1235 constexpr_ctx new_ctx
= *ctx
;
1236 new_ctx
.quiet
= true;
1237 for (i
= 0; i
< nargs
; ++i
)
1239 args
[i
] = CALL_EXPR_ARG (t
, i
);
1240 /* If builtin_valid_in_constant_expr_p is true,
1241 potential_constant_expression_1 has not recursed into the arguments
1242 of the builtin, verify it here. */
1243 if (!builtin_valid_in_constant_expr_p (fun
)
1244 || potential_constant_expression (args
[i
]))
1246 bool dummy1
= false, dummy2
= false;
1247 args
[i
] = cxx_eval_constant_expression (&new_ctx
, args
[i
], false,
1252 /* For __builtin_constant_p, fold all expressions with constant values
1253 even if they aren't C++ constant-expressions. */
1254 args
[i
] = cp_fold_rvalue (args
[i
]);
1257 bool save_ffbcp
= force_folding_builtin_constant_p
;
1258 force_folding_builtin_constant_p
= true;
1259 tree save_cur_fn
= current_function_decl
;
1260 /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
1261 if (fndecl_built_in_p (fun
, BUILT_IN_FUNCTION
)
1263 && ctx
->call
->fundef
)
1264 current_function_decl
= ctx
->call
->fundef
->decl
;
1265 new_call
= fold_builtin_call_array (EXPR_LOCATION (t
), TREE_TYPE (t
),
1266 CALL_EXPR_FN (t
), nargs
, args
);
1267 current_function_decl
= save_cur_fn
;
1268 force_folding_builtin_constant_p
= save_ffbcp
;
1269 if (new_call
== NULL
)
1271 if (!*non_constant_p
&& !ctx
->quiet
)
1273 /* Do not allow__builtin_unreachable in constexpr function.
1274 The __builtin_unreachable call with BUILTINS_LOCATION
1275 comes from cp_maybe_instrument_return. */
1276 if (fndecl_built_in_p (fun
, BUILT_IN_UNREACHABLE
)
1277 && EXPR_LOCATION (t
) == BUILTINS_LOCATION
)
1278 error ("%<constexpr%> call flows off the end of the function");
1281 new_call
= build_call_array_loc (EXPR_LOCATION (t
), TREE_TYPE (t
),
1282 CALL_EXPR_FN (t
), nargs
, args
);
1283 error ("%q+E is not a constant expression", new_call
);
1286 *non_constant_p
= true;
1290 if (!potential_constant_expression (new_call
))
1292 if (!*non_constant_p
&& !ctx
->quiet
)
1293 error ("%q+E is not a constant expression", new_call
);
1294 *non_constant_p
= true;
1298 return cxx_eval_constant_expression (&new_ctx
, new_call
, lval
,
1299 non_constant_p
, overflow_p
);
1302 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1303 the type of the value to match. */
1306 adjust_temp_type (tree type
, tree temp
)
1308 if (same_type_p (TREE_TYPE (temp
), type
))
1310 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1311 if (TREE_CODE (temp
) == CONSTRUCTOR
)
1313 /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
1314 tree t
= copy_node (temp
);
1315 TREE_TYPE (t
) = type
;
1318 if (TREE_CODE (temp
) == EMPTY_CLASS_EXPR
)
1319 return build0 (EMPTY_CLASS_EXPR
, type
);
1320 gcc_assert (scalarish_type_p (type
));
1321 /* Now we know we're dealing with a scalar, and a prvalue of non-class
1322 type is cv-unqualified. */
1323 return cp_fold_convert (cv_unqualified (type
), temp
);
1326 /* If T is a CONSTRUCTOR, return an unshared copy of T and any
1327 sub-CONSTRUCTORs. Otherwise return T.
1329 We use this whenever we initialize an object as a whole, whether it's a
1330 parameter, a local variable, or a subobject, so that subsequent
1331 modifications don't affect other places where it was used. */
1334 unshare_constructor (tree t MEM_STAT_DECL
)
1336 if (!t
|| TREE_CODE (t
) != CONSTRUCTOR
)
1338 auto_vec
<tree
*, 4> ptrs
;
1339 ptrs
.safe_push (&t
);
1340 while (!ptrs
.is_empty ())
1342 tree
*p
= ptrs
.pop ();
1343 tree n
= copy_node (*p PASS_MEM_STAT
);
1344 CONSTRUCTOR_ELTS (n
) = vec_safe_copy (CONSTRUCTOR_ELTS (*p
) PASS_MEM_STAT
);
1346 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (n
);
1347 constructor_elt
*ce
;
1348 for (HOST_WIDE_INT i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
1349 if (TREE_CODE (ce
->value
) == CONSTRUCTOR
)
1350 ptrs
.safe_push (&ce
->value
);
1355 /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
1358 free_constructor (tree t
)
1360 if (!t
|| TREE_CODE (t
) != CONSTRUCTOR
)
1362 releasing_vec ctors
;
1363 vec_safe_push (ctors
, t
);
1364 while (!ctors
->is_empty ())
1366 tree c
= ctors
->pop ();
1367 if (vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (c
))
1369 constructor_elt
*ce
;
1370 for (HOST_WIDE_INT i
= 0; vec_safe_iterate (elts
, i
, &ce
); ++i
)
1371 if (TREE_CODE (ce
->value
) == CONSTRUCTOR
)
1372 vec_safe_push (ctors
, ce
->value
);
1379 /* Subroutine of cxx_eval_call_expression.
1380 We are processing a call expression (either CALL_EXPR or
1381 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1382 all arguments and bind their values to correspondings
1383 parameters, making up the NEW_CALL context. */
1386 cxx_bind_parameters_in_call (const constexpr_ctx
*ctx
, tree t
,
1387 constexpr_call
*new_call
,
1388 bool *non_constant_p
, bool *overflow_p
,
1389 bool *non_constant_args
)
1391 const int nargs
= call_expr_nargs (t
);
1392 tree fun
= new_call
->fundef
->decl
;
1393 tree parms
= new_call
->fundef
->parms
;
1395 /* We don't record ellipsis args below. */
1396 int nparms
= list_length (parms
);
1397 int nbinds
= nargs
< nparms
? nargs
: nparms
;
1398 tree binds
= new_call
->bindings
= make_tree_vec (nbinds
);
1399 for (i
= 0; i
< nargs
; ++i
)
1402 tree type
= parms
? TREE_TYPE (parms
) : void_type_node
;
1403 x
= get_nth_callarg (t
, i
);
1404 /* For member function, the first argument is a pointer to the implied
1405 object. For a constructor, it might still be a dummy object, in
1406 which case we get the real argument from ctx. */
1407 if (i
== 0 && DECL_CONSTRUCTOR_P (fun
)
1408 && is_dummy_object (x
))
1411 x
= build_address (x
);
1413 if (TREE_ADDRESSABLE (type
))
1414 /* Undo convert_for_arg_passing work here. */
1415 x
= convert_from_reference (x
);
1416 arg
= cxx_eval_constant_expression (ctx
, x
, /*lval=*/false,
1417 non_constant_p
, overflow_p
);
1418 /* Don't VERIFY_CONSTANT here. */
1419 if (*non_constant_p
&& ctx
->quiet
)
1421 /* Just discard ellipsis args after checking their constantitude. */
1425 if (!*non_constant_p
)
1427 /* Unsharing here isn't necessary for correctness, but it
1428 significantly improves memory performance for some reason. */
1429 arg
= unshare_constructor (arg
);
1430 /* Make sure the binding has the same type as the parm. But
1431 only for constant args. */
1432 if (!TYPE_REF_P (type
))
1433 arg
= adjust_temp_type (type
, arg
);
1434 if (!TREE_CONSTANT (arg
))
1435 *non_constant_args
= true;
1436 TREE_VEC_ELT (binds
, i
) = arg
;
1438 parms
= TREE_CHAIN (parms
);
1442 /* Variables and functions to manage constexpr call expansion context.
1443 These do not need to be marked for PCH or GC. */
1445 /* FIXME remember and print actual constant arguments. */
1446 static vec
<tree
> call_stack
;
1447 static int call_stack_tick
;
1448 static int last_cx_error_tick
;
1451 push_cx_call_context (tree call
)
1454 if (!EXPR_HAS_LOCATION (call
))
1455 SET_EXPR_LOCATION (call
, input_location
);
1456 call_stack
.safe_push (call
);
1457 int len
= call_stack
.length ();
1458 if (len
> max_constexpr_depth
)
1464 pop_cx_call_context (void)
1471 cx_error_context (void)
1473 vec
<tree
> r
= vNULL
;
1474 if (call_stack_tick
!= last_cx_error_tick
1475 && !call_stack
.is_empty ())
1477 last_cx_error_tick
= call_stack_tick
;
1481 /* Evaluate a call T to a GCC internal function when possible and return
1482 the evaluated result or, under the control of CTX, give an error, set
1483 NON_CONSTANT_P, and return the unevaluated call T otherwise. */
1486 cxx_eval_internal_function (const constexpr_ctx
*ctx
, tree t
,
1488 bool *non_constant_p
, bool *overflow_p
)
1490 enum tree_code opcode
= ERROR_MARK
;
1492 switch (CALL_EXPR_IFN (t
))
1494 case IFN_UBSAN_NULL
:
1495 case IFN_UBSAN_BOUNDS
:
1496 case IFN_UBSAN_VPTR
:
1497 case IFN_FALLTHROUGH
:
1500 case IFN_ADD_OVERFLOW
:
1503 case IFN_SUB_OVERFLOW
:
1504 opcode
= MINUS_EXPR
;
1506 case IFN_MUL_OVERFLOW
:
1511 return cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0),
1512 false, non_constant_p
, overflow_p
);
1514 case IFN_VEC_CONVERT
:
1516 tree arg
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0),
1517 false, non_constant_p
,
1519 if (TREE_CODE (arg
) == VECTOR_CST
)
1520 return fold_const_call (CFN_VEC_CONVERT
, TREE_TYPE (t
), arg
);
1523 *non_constant_p
= true;
1530 error_at (cp_expr_loc_or_input_loc (t
),
1531 "call to internal function %qE", t
);
1532 *non_constant_p
= true;
1536 /* Evaluate constant arguments using OPCODE and return a complex
1537 number containing the result and the overflow bit. */
1538 tree arg0
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 0), lval
,
1539 non_constant_p
, overflow_p
);
1540 tree arg1
= cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, 1), lval
,
1541 non_constant_p
, overflow_p
);
1543 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
1545 location_t loc
= cp_expr_loc_or_input_loc (t
);
1546 tree type
= TREE_TYPE (TREE_TYPE (t
));
1547 tree result
= fold_binary_loc (loc
, opcode
, type
,
1548 fold_convert_loc (loc
, type
, arg0
),
1549 fold_convert_loc (loc
, type
, arg1
));
1551 = build_int_cst (type
, arith_overflowed_p (opcode
, type
, arg0
, arg1
));
1552 /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
1553 if (TREE_OVERFLOW (result
))
1554 TREE_OVERFLOW (result
) = 0;
1556 return build_complex (TREE_TYPE (t
), result
, ovf
);
1559 *non_constant_p
= true;
1563 /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
1566 clear_no_implicit_zero (tree ctor
)
1568 if (CONSTRUCTOR_NO_CLEARING (ctor
))
1570 CONSTRUCTOR_NO_CLEARING (ctor
) = false;
1571 tree elt
; unsigned HOST_WIDE_INT idx
;
1572 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor
), idx
, elt
)
1573 if (TREE_CODE (elt
) == CONSTRUCTOR
)
1574 clear_no_implicit_zero (elt
);
1578 /* Subroutine of cxx_eval_constant_expression.
1579 Evaluate the call expression tree T in the context of OLD_CALL expression
1583 cxx_eval_call_expression (const constexpr_ctx
*ctx
, tree t
,
1585 bool *non_constant_p
, bool *overflow_p
)
1587 location_t loc
= cp_expr_loc_or_input_loc (t
);
1588 tree fun
= get_function_named_in_call (t
);
1589 constexpr_call new_call
1590 = { NULL
, NULL
, NULL
, 0, ctx
->manifestly_const_eval
};
1593 if (fun
== NULL_TREE
)
1594 return cxx_eval_internal_function (ctx
, t
, lval
,
1595 non_constant_p
, overflow_p
);
1597 if (TREE_CODE (fun
) != FUNCTION_DECL
)
1599 /* Might be a constexpr function pointer. */
1600 fun
= cxx_eval_constant_expression (ctx
, fun
,
1601 /*lval*/false, non_constant_p
,
1604 if (TREE_CODE (fun
) == ADDR_EXPR
)
1605 fun
= TREE_OPERAND (fun
, 0);
1606 /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
1607 indirection, the called expression is a pointer into the
1608 virtual table which should contain FDESC_EXPR. Extract the
1609 FUNCTION_DECL from there. */
1610 else if (TARGET_VTABLE_USES_DESCRIPTORS
1611 && TREE_CODE (fun
) == POINTER_PLUS_EXPR
1612 && TREE_CODE (TREE_OPERAND (fun
, 0)) == ADDR_EXPR
1613 && TREE_CODE (TREE_OPERAND (fun
, 1)) == INTEGER_CST
)
1615 tree d
= TREE_OPERAND (TREE_OPERAND (fun
, 0), 0);
1617 && DECL_VTABLE_OR_VTT_P (d
)
1618 && TREE_CODE (TREE_TYPE (d
)) == ARRAY_TYPE
1619 && TREE_TYPE (TREE_TYPE (d
)) == vtable_entry_type
1621 && TREE_CODE (DECL_INITIAL (d
)) == CONSTRUCTOR
)
1623 tree i
= int_const_binop (TRUNC_DIV_EXPR
, TREE_OPERAND (fun
, 1),
1624 TYPE_SIZE_UNIT (vtable_entry_type
));
1625 HOST_WIDE_INT idx
= find_array_ctor_elt (DECL_INITIAL (d
), i
);
1629 = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d
)))[idx
].value
;
1630 if (TREE_CODE (fdesc
) == FDESC_EXPR
1631 && integer_zerop (TREE_OPERAND (fdesc
, 1)))
1632 fun
= TREE_OPERAND (fdesc
, 0);
1637 if (TREE_CODE (fun
) != FUNCTION_DECL
)
1639 if (!ctx
->quiet
&& !*non_constant_p
)
1640 error_at (loc
, "expression %qE does not designate a %<constexpr%> "
1642 *non_constant_p
= true;
1645 if (DECL_CLONED_FUNCTION_P (fun
))
1646 fun
= DECL_CLONED_FUNCTION (fun
);
1648 if (is_ubsan_builtin_p (fun
))
1651 if (fndecl_built_in_p (fun
))
1652 return cxx_eval_builtin_function_call (ctx
, t
, fun
,
1653 lval
, non_constant_p
, overflow_p
);
1654 if (!DECL_DECLARED_CONSTEXPR_P (fun
))
1658 if (!lambda_static_thunk_p (fun
))
1659 error_at (loc
, "call to non-%<constexpr%> function %qD", fun
);
1660 explain_invalid_constexpr_fn (fun
);
1662 *non_constant_p
= true;
1666 constexpr_ctx new_ctx
= *ctx
;
1667 if (DECL_CONSTRUCTOR_P (fun
) && !ctx
->object
1668 && TREE_CODE (t
) == AGGR_INIT_EXPR
)
1670 /* We want to have an initialization target for an AGGR_INIT_EXPR.
1671 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
1672 new_ctx
.object
= AGGR_INIT_EXPR_SLOT (t
);
1673 tree ctor
= new_ctx
.ctor
= build_constructor (DECL_CONTEXT (fun
), NULL
);
1674 CONSTRUCTOR_NO_CLEARING (ctor
) = true;
1675 ctx
->values
->put (new_ctx
.object
, ctor
);
1679 /* Shortcut trivial constructor/op=. */
1680 if (trivial_fn_p (fun
))
1682 tree init
= NULL_TREE
;
1683 if (call_expr_nargs (t
) == 2)
1684 init
= convert_from_reference (get_nth_callarg (t
, 1));
1685 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
1686 && AGGR_INIT_ZERO_FIRST (t
))
1687 init
= build_zero_init (DECL_CONTEXT (fun
), NULL_TREE
, false);
1690 tree op
= get_nth_callarg (t
, 0);
1691 if (is_dummy_object (op
))
1694 op
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (op
)), op
);
1695 tree set
= build2 (MODIFY_EXPR
, TREE_TYPE (op
), op
, init
);
1696 new_ctx
.call
= &new_call
;
1697 return cxx_eval_constant_expression (&new_ctx
, set
, lval
,
1698 non_constant_p
, overflow_p
);
1702 /* We can't defer instantiating the function any longer. */
1703 if (!DECL_INITIAL (fun
)
1704 && DECL_TEMPLOID_INSTANTIATION (fun
))
1706 location_t save_loc
= input_location
;
1707 input_location
= loc
;
1709 instantiate_decl (fun
, /*defer_ok*/false, /*expl_inst*/false);
1711 input_location
= save_loc
;
1714 /* If in direct recursive call, optimize definition search. */
1715 if (ctx
&& ctx
->call
&& ctx
->call
->fundef
&& ctx
->call
->fundef
->decl
== fun
)
1716 new_call
.fundef
= ctx
->call
->fundef
;
1719 new_call
.fundef
= retrieve_constexpr_fundef (fun
);
1720 if (new_call
.fundef
== NULL
|| new_call
.fundef
->body
== NULL
1721 || fun
== current_function_decl
)
1725 /* We need to check for current_function_decl here in case we're
1726 being called during cp_fold_function, because at that point
1727 DECL_INITIAL is set properly and we have a fundef but we
1728 haven't lowered invisirefs yet (c++/70344). */
1729 if (DECL_INITIAL (fun
) == error_mark_node
1730 || fun
== current_function_decl
)
1731 error_at (loc
, "%qD called in a constant expression before its "
1732 "definition is complete", fun
);
1733 else if (DECL_INITIAL (fun
))
1735 /* The definition of fun was somehow unsuitable. But pretend
1736 that lambda static thunks don't exist. */
1737 if (!lambda_static_thunk_p (fun
))
1738 error_at (loc
, "%qD called in a constant expression", fun
);
1739 explain_invalid_constexpr_fn (fun
);
1742 error_at (loc
, "%qD used before its definition", fun
);
1744 *non_constant_p
= true;
1749 bool non_constant_args
= false;
1750 cxx_bind_parameters_in_call (ctx
, t
, &new_call
,
1751 non_constant_p
, overflow_p
, &non_constant_args
);
1753 /* We build up the bindings list before we know whether we already have this
1754 call cached. If we don't end up saving these bindings, ggc_free them when
1755 this function exits. */
1761 free_bindings (tree
&b
): bindings (b
), do_free(true) { }
1762 void preserve () { do_free
= false; }
1766 for (int i
= 0; i
< TREE_VEC_LENGTH (bindings
); ++i
)
1767 free_constructor (TREE_VEC_ELT (bindings
, i
));
1768 ggc_free (bindings
);
1771 } fb (new_call
.bindings
);
1773 if (*non_constant_p
)
1776 depth_ok
= push_cx_call_context (t
);
1778 tree result
= NULL_TREE
;
1780 constexpr_call
*entry
= NULL
;
1781 if (depth_ok
&& !non_constant_args
&& ctx
->strict
)
1783 new_call
.hash
= constexpr_fundef_hasher::hash (new_call
.fundef
);
1785 = iterative_hash_template_arg (new_call
.bindings
, new_call
.hash
);
1787 = iterative_hash_object (ctx
->manifestly_const_eval
, new_call
.hash
);
1789 /* If we have seen this call before, we are done. */
1790 maybe_initialize_constexpr_call_table ();
1791 constexpr_call
**slot
1792 = constexpr_call_table
->find_slot (&new_call
, INSERT
);
1796 /* Only cache up to constexpr_cache_depth to limit memory use. */
1797 if (depth_ok
< constexpr_cache_depth
)
1799 /* We need to keep a pointer to the entry, not just the slot, as
1800 the slot can move during evaluation of the body. */
1801 *slot
= entry
= ggc_alloc
<constexpr_call
> ();
1806 /* Calls that are in progress have their result set to NULL, so that we
1807 can detect circular dependencies. Now that we only cache up to
1808 constexpr_cache_depth this won't catch circular dependencies that
1809 start deeper, but they'll hit the recursion or ops limit. */
1810 else if (entry
->result
== NULL
)
1813 error ("call has circular dependency");
1814 *non_constant_p
= true;
1815 entry
->result
= result
= error_mark_node
;
1818 result
= entry
->result
;
1824 error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
1825 "%<-fconstexpr-depth=%> to increase the maximum)",
1826 max_constexpr_depth
);
1827 *non_constant_p
= true;
1828 result
= error_mark_node
;
1832 if (result
&& result
!= error_mark_node
)
1834 else if (!DECL_SAVED_TREE (fun
))
1836 /* When at_eof >= 2, cgraph has started throwing away
1837 DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
1838 late code generation for VEC_INIT_EXPR, which needs to be
1839 completely reconsidered. */
1840 gcc_assert (at_eof
>= 2 && ctx
->quiet
);
1841 *non_constant_p
= true;
1845 tree body
, parms
, res
;
1846 releasing_vec ctors
;
1848 /* Reuse or create a new unshared copy of this function's body. */
1849 tree copy
= get_fundef_copy (new_call
.fundef
);
1850 body
= TREE_PURPOSE (copy
);
1851 parms
= TREE_VALUE (copy
);
1852 res
= TREE_TYPE (copy
);
1854 /* Associate the bindings with the remapped parms. */
1855 tree bound
= new_call
.bindings
;
1856 tree remapped
= parms
;
1857 for (int i
= 0; i
< TREE_VEC_LENGTH (bound
); ++i
)
1859 tree arg
= TREE_VEC_ELT (bound
, i
);
1860 /* Don't share a CONSTRUCTOR that might be changed. */
1861 arg
= unshare_constructor (arg
);
1862 if (TREE_CODE (arg
) == CONSTRUCTOR
)
1863 vec_safe_push (ctors
, arg
);
1864 ctx
->values
->put (remapped
, arg
);
1865 remapped
= DECL_CHAIN (remapped
);
1867 /* Add the RESULT_DECL to the values map, too. */
1868 tree slot
= NULL_TREE
;
1869 if (DECL_BY_REFERENCE (res
))
1871 slot
= AGGR_INIT_EXPR_SLOT (t
);
1872 tree addr
= build_address (slot
);
1873 addr
= build_nop (TREE_TYPE (res
), addr
);
1874 ctx
->values
->put (res
, addr
);
1875 ctx
->values
->put (slot
, NULL_TREE
);
1878 ctx
->values
->put (res
, NULL_TREE
);
1880 /* Track the callee's evaluated SAVE_EXPRs so that we can forget
1881 their values after the call. */
1882 constexpr_ctx ctx_with_save_exprs
= *ctx
;
1883 auto_vec
<tree
, 10> save_exprs
;
1884 ctx_with_save_exprs
.save_exprs
= &save_exprs
;
1885 ctx_with_save_exprs
.call
= &new_call
;
1887 tree jump_target
= NULL_TREE
;
1888 cxx_eval_constant_expression (&ctx_with_save_exprs
, body
,
1889 lval
, non_constant_p
, overflow_p
,
1892 if (DECL_CONSTRUCTOR_P (fun
))
1893 /* This can be null for a subobject constructor call, in
1894 which case what we care about is the initialization
1895 side-effects rather than the value. We could get at the
1896 value by evaluating *this, but we don't bother; there's
1897 no need to put such a call in the hash table. */
1898 result
= lval
? ctx
->object
: ctx
->ctor
;
1899 else if (VOID_TYPE_P (TREE_TYPE (res
)))
1903 result
= *ctx
->values
->get (slot
? slot
: res
);
1904 if (result
== NULL_TREE
&& !*non_constant_p
)
1907 error ("%<constexpr%> call flows off the end "
1909 *non_constant_p
= true;
1913 /* Forget the saved values of the callee's SAVE_EXPRs. */
1916 FOR_EACH_VEC_ELT (save_exprs
, i
, save_expr
)
1917 ctx_with_save_exprs
.values
->remove (save_expr
);
1919 /* Remove the parms/result from the values map. Is it worth
1920 bothering to do this when the map itself is only live for
1921 one constexpr evaluation? If so, maybe also clear out
1922 other vars from call, maybe in BIND_EXPR handling? */
1923 ctx
->values
->remove (res
);
1925 ctx
->values
->remove (slot
);
1926 for (tree parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
1927 ctx
->values
->remove (parm
);
1929 /* Free any parameter CONSTRUCTORs we aren't returning directly. */
1930 while (!ctors
->is_empty ())
1932 tree c
= ctors
->pop ();
1934 free_constructor (c
);
1937 /* Make the unshared function copy we used available for re-use. */
1938 save_fundef_copy (fun
, copy
);
1941 if (result
== error_mark_node
)
1942 *non_constant_p
= true;
1943 if (*non_constant_p
|| *overflow_p
)
1944 result
= error_mark_node
;
1948 entry
->result
= result
;
1951 /* The result of a constexpr function must be completely initialized. */
1952 if (TREE_CODE (result
) == CONSTRUCTOR
)
1953 clear_no_implicit_zero (result
);
1955 pop_cx_call_context ();
1959 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
1962 reduced_constant_expression_p (tree t
)
1967 switch (TREE_CODE (t
))
1970 /* Even if we can't lower this yet, it's constant. */
1974 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
1975 tree idx
, val
, field
; unsigned HOST_WIDE_INT i
;
1976 if (CONSTRUCTOR_NO_CLEARING (t
))
1978 if (TREE_CODE (TREE_TYPE (t
)) == VECTOR_TYPE
)
1979 /* An initialized vector would have a VECTOR_CST. */
1982 field
= next_initializable_field (TYPE_FIELDS (TREE_TYPE (t
)));
1986 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t
), i
, idx
, val
)
1988 /* If VAL is null, we're in the middle of initializing this
1990 if (!reduced_constant_expression_p (val
))
1996 field
= next_initializable_field (DECL_CHAIN (field
));
2001 else if (CONSTRUCTOR_NO_CLEARING (t
))
2002 /* All the fields are initialized. */
2003 CONSTRUCTOR_NO_CLEARING (t
) = false;
2007 /* FIXME are we calling this too much? */
2008 return initializer_constant_valid_p (t
, TREE_TYPE (t
)) != NULL_TREE
;
2012 /* Some expressions may have constant operands but are not constant
2013 themselves, such as 1/0. Call this function to check for that
2016 We only call this in places that require an arithmetic constant, not in
2017 places where we might have a non-constant expression that can be a
2018 component of a constant expression, such as the address of a constexpr
2019 variable that might be dereferenced later. */
2022 verify_constant (tree t
, bool allow_non_constant
, bool *non_constant_p
,
2025 if (!*non_constant_p
&& !reduced_constant_expression_p (t
))
2027 if (!allow_non_constant
)
2028 error ("%q+E is not a constant expression", t
);
2029 *non_constant_p
= true;
2031 if (TREE_OVERFLOW_P (t
))
2033 if (!allow_non_constant
)
2035 permerror (input_location
, "overflow in constant expression");
2036 /* If we're being permissive (and are in an enforcing
2037 context), ignore the overflow. */
2038 if (flag_permissive
)
2039 return *non_constant_p
;
2043 return *non_constant_p
;
2046 /* Check whether the shift operation with code CODE and type TYPE on LHS
2047 and RHS is undefined. If it is, give an error with an explanation,
2048 and return true; return false otherwise. */
2051 cxx_eval_check_shift_p (location_t loc
, const constexpr_ctx
*ctx
,
2052 enum tree_code code
, tree type
, tree lhs
, tree rhs
)
2054 if ((code
!= LSHIFT_EXPR
&& code
!= RSHIFT_EXPR
)
2055 || TREE_CODE (lhs
) != INTEGER_CST
2056 || TREE_CODE (rhs
) != INTEGER_CST
)
2059 tree lhstype
= TREE_TYPE (lhs
);
2060 unsigned HOST_WIDE_INT uprec
= TYPE_PRECISION (TREE_TYPE (lhs
));
2062 /* [expr.shift] The behavior is undefined if the right operand
2063 is negative, or greater than or equal to the length in bits
2064 of the promoted left operand. */
2065 if (tree_int_cst_sgn (rhs
) == -1)
2068 permerror (loc
, "right operand of shift expression %q+E is negative",
2069 build2_loc (loc
, code
, type
, lhs
, rhs
));
2070 return (!flag_permissive
|| ctx
->quiet
);
2072 if (compare_tree_int (rhs
, uprec
) >= 0)
2075 permerror (loc
, "right operand of shift expression %q+E is greater "
2076 "than or equal to the precision %wu of the left operand",
2077 build2_loc (loc
, code
, type
, lhs
, rhs
), uprec
);
2078 return (!flag_permissive
|| ctx
->quiet
);
2081 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
2082 if E1 has a signed type and non-negative value, and E1x2^E2 is
2083 representable in the corresponding unsigned type of the result type,
2084 then that value, converted to the result type, is the resulting value;
2085 otherwise, the behavior is undefined.
2087 The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
2088 2^N, where N is the range exponent of the type of the result. */
2089 if (code
== LSHIFT_EXPR
2090 && !TYPE_UNSIGNED (lhstype
)
2091 && cxx_dialect
>= cxx11
2092 && cxx_dialect
< cxx2a
)
2094 if (tree_int_cst_sgn (lhs
) == -1)
2098 "left operand of shift expression %q+E is negative",
2099 build2_loc (loc
, code
, type
, lhs
, rhs
));
2100 return (!flag_permissive
|| ctx
->quiet
);
2102 /* For signed x << y the following:
2103 (unsigned) x >> ((prec (lhs) - 1) - y)
2104 if > 1, is undefined. The right-hand side of this formula
2105 is the highest bit of the LHS that can be set (starting from 0),
2106 so that the shift doesn't overflow. We then right-shift the LHS
2107 to see whether any other bit is set making the original shift
2108 undefined -- the result is not representable in the corresponding
2110 tree t
= build_int_cst (unsigned_type_node
, uprec
- 1);
2111 t
= fold_build2 (MINUS_EXPR
, unsigned_type_node
, t
, rhs
);
2112 tree ulhs
= fold_convert (unsigned_type_for (lhstype
), lhs
);
2113 t
= fold_build2 (RSHIFT_EXPR
, TREE_TYPE (ulhs
), ulhs
, t
);
2114 if (tree_int_cst_lt (integer_one_node
, t
))
2117 permerror (loc
, "shift expression %q+E overflows",
2118 build2_loc (loc
, code
, type
, lhs
, rhs
));
2119 return (!flag_permissive
|| ctx
->quiet
);
2125 /* Subroutine of cxx_eval_constant_expression.
2126 Attempt to reduce the unary expression tree T to a compile time value.
2127 If successful, return the value. Otherwise issue a diagnostic
2128 and return error_mark_node. */
2131 cxx_eval_unary_expression (const constexpr_ctx
*ctx
, tree t
,
2133 bool *non_constant_p
, bool *overflow_p
)
2136 tree orig_arg
= TREE_OPERAND (t
, 0);
2137 tree arg
= cxx_eval_constant_expression (ctx
, orig_arg
, /*lval*/false,
2138 non_constant_p
, overflow_p
);
2139 VERIFY_CONSTANT (arg
);
2140 location_t loc
= EXPR_LOCATION (t
);
2141 enum tree_code code
= TREE_CODE (t
);
2142 tree type
= TREE_TYPE (t
);
2143 r
= fold_unary_loc (loc
, code
, type
, arg
);
2146 if (arg
== orig_arg
)
2149 r
= build1_loc (loc
, code
, type
, arg
);
2151 VERIFY_CONSTANT (r
);
2155 /* Helper function for cxx_eval_binary_expression. Try to optimize
2156 original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
2157 generic folding should be used. */
2160 cxx_fold_pointer_plus_expression (const constexpr_ctx
*ctx
, tree t
,
2161 tree lhs
, tree rhs
, bool *non_constant_p
,
2165 if (TREE_CODE (lhs
) != ADDR_EXPR
)
2168 lhs
= TREE_OPERAND (lhs
, 0);
2170 /* &A[i] p+ j => &A[i + j] */
2171 if (TREE_CODE (lhs
) == ARRAY_REF
2172 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == INTEGER_CST
2173 && TREE_CODE (rhs
) == INTEGER_CST
2174 && TYPE_SIZE_UNIT (TREE_TYPE (lhs
))
2175 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs
))) == INTEGER_CST
)
2177 tree orig_type
= TREE_TYPE (t
);
2178 location_t loc
= EXPR_LOCATION (t
);
2179 tree type
= TREE_TYPE (lhs
);
2181 t
= fold_convert_loc (loc
, ssizetype
, TREE_OPERAND (lhs
, 1));
2182 tree nelts
= array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs
, 0)));
2183 nelts
= cxx_eval_constant_expression (ctx
, nelts
, false, non_constant_p
,
2185 if (*non_constant_p
)
2187 /* Don't fold an out-of-bound access. */
2188 if (!tree_int_cst_le (t
, nelts
))
2190 rhs
= cp_fold_convert (ssizetype
, rhs
);
2191 /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
2192 constexpr int A[1]; ... (char *)&A[0] + 1 */
2193 if (!integer_zerop (fold_build2_loc (loc
, TRUNC_MOD_EXPR
, sizetype
,
2194 rhs
, TYPE_SIZE_UNIT (type
))))
2196 /* Make sure to treat the second operand of POINTER_PLUS_EXPR
2198 rhs
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, ssizetype
, rhs
,
2199 TYPE_SIZE_UNIT (type
));
2200 t
= size_binop_loc (loc
, PLUS_EXPR
, rhs
, t
);
2201 t
= build4_loc (loc
, ARRAY_REF
, type
, TREE_OPERAND (lhs
, 0),
2202 t
, NULL_TREE
, NULL_TREE
);
2203 t
= cp_build_addr_expr (t
, tf_warning_or_error
);
2204 t
= cp_fold_convert (orig_type
, t
);
2205 return cxx_eval_constant_expression (ctx
, t
, /*lval*/false,
2206 non_constant_p
, overflow_p
);
2212 /* Subroutine of cxx_eval_constant_expression.
2213 Like cxx_eval_unary_expression, except for binary expressions. */
2216 cxx_eval_binary_expression (const constexpr_ctx
*ctx
, tree t
,
2218 bool *non_constant_p
, bool *overflow_p
)
2221 tree orig_lhs
= TREE_OPERAND (t
, 0);
2222 tree orig_rhs
= TREE_OPERAND (t
, 1);
2224 lhs
= cxx_eval_constant_expression (ctx
, orig_lhs
, /*lval*/false,
2225 non_constant_p
, overflow_p
);
2226 /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
2228 if (*non_constant_p
)
2230 rhs
= cxx_eval_constant_expression (ctx
, orig_rhs
, /*lval*/false,
2231 non_constant_p
, overflow_p
);
2232 if (*non_constant_p
)
2235 location_t loc
= EXPR_LOCATION (t
);
2236 enum tree_code code
= TREE_CODE (t
);
2237 tree type
= TREE_TYPE (t
);
2239 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
2241 bool is_code_eq
= (code
== EQ_EXPR
);
2243 if (TREE_CODE (lhs
) == PTRMEM_CST
2244 && TREE_CODE (rhs
) == PTRMEM_CST
)
2246 tree lmem
= PTRMEM_CST_MEMBER (lhs
);
2247 tree rmem
= PTRMEM_CST_MEMBER (rhs
);
2249 if (TREE_CODE (lmem
) == TREE_CODE (rmem
)
2250 && TREE_CODE (lmem
) == FIELD_DECL
2251 && TREE_CODE (DECL_CONTEXT (lmem
)) == UNION_TYPE
2252 && same_type_p (DECL_CONTEXT (lmem
),
2253 DECL_CONTEXT (rmem
)))
2254 /* If both refer to (possibly different) members of the same union
2255 (12.3), they compare equal. */
2258 eq
= cp_tree_equal (lhs
, rhs
);
2259 r
= constant_boolean_node (eq
== is_code_eq
, type
);
2261 else if ((TREE_CODE (lhs
) == PTRMEM_CST
2262 || TREE_CODE (rhs
) == PTRMEM_CST
)
2263 && (null_member_pointer_value_p (lhs
)
2264 || null_member_pointer_value_p (rhs
)))
2265 r
= constant_boolean_node (!is_code_eq
, type
);
2266 else if (TREE_CODE (lhs
) == PTRMEM_CST
)
2267 lhs
= cplus_expand_constant (lhs
);
2268 else if (TREE_CODE (rhs
) == PTRMEM_CST
)
2269 rhs
= cplus_expand_constant (rhs
);
2271 if (code
== POINTER_PLUS_EXPR
&& !*non_constant_p
2272 && integer_zerop (lhs
) && !integer_zerop (rhs
))
2275 error ("arithmetic involving a null pointer in %qE", lhs
);
2276 *non_constant_p
= true;
2279 else if (code
== POINTER_PLUS_EXPR
)
2280 r
= cxx_fold_pointer_plus_expression (ctx
, t
, lhs
, rhs
, non_constant_p
,
2284 r
= fold_binary_loc (loc
, code
, type
, lhs
, rhs
);
2288 if (lhs
== orig_lhs
&& rhs
== orig_rhs
)
2291 r
= build2_loc (loc
, code
, type
, lhs
, rhs
);
2293 else if (cxx_eval_check_shift_p (loc
, ctx
, code
, type
, lhs
, rhs
))
2294 *non_constant_p
= true;
2295 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
2296 a local array in a constexpr function. */
2297 bool ptr
= INDIRECT_TYPE_P (TREE_TYPE (lhs
));
2299 VERIFY_CONSTANT (r
);
2303 /* Subroutine of cxx_eval_constant_expression.
2304 Attempt to evaluate condition expressions. Dead branches are not
2308 cxx_eval_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
2310 bool *non_constant_p
, bool *overflow_p
,
2313 tree val
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
2315 non_constant_p
, overflow_p
);
2316 VERIFY_CONSTANT (val
);
2317 /* Don't VERIFY_CONSTANT the other operands. */
2318 if (integer_zerop (val
))
2319 val
= TREE_OPERAND (t
, 2);
2321 val
= TREE_OPERAND (t
, 1);
2322 if (TREE_CODE (t
) == IF_STMT
&& !val
)
2324 return cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
2325 overflow_p
, jump_target
);
2328 /* Subroutine of cxx_eval_constant_expression.
2329 Attempt to evaluate vector condition expressions. Unlike
2330 cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
2331 ternary arithmetics operation, where all 3 arguments have to be
2332 evaluated as constants and then folding computes the result from
2336 cxx_eval_vector_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
2337 bool *non_constant_p
, bool *overflow_p
)
2339 tree arg1
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
2341 non_constant_p
, overflow_p
);
2342 VERIFY_CONSTANT (arg1
);
2343 tree arg2
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
2345 non_constant_p
, overflow_p
);
2346 VERIFY_CONSTANT (arg2
);
2347 tree arg3
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 2),
2349 non_constant_p
, overflow_p
);
2350 VERIFY_CONSTANT (arg3
);
2351 location_t loc
= EXPR_LOCATION (t
);
2352 tree type
= TREE_TYPE (t
);
2353 tree r
= fold_ternary_loc (loc
, VEC_COND_EXPR
, type
, arg1
, arg2
, arg3
);
2356 if (arg1
== TREE_OPERAND (t
, 0)
2357 && arg2
== TREE_OPERAND (t
, 1)
2358 && arg3
== TREE_OPERAND (t
, 2))
2361 r
= build3_loc (loc
, VEC_COND_EXPR
, type
, arg1
, arg2
, arg3
);
2363 VERIFY_CONSTANT (r
);
2367 /* Returns less than, equal to, or greater than zero if KEY is found to be
2368 less than, to match, or to be greater than the constructor_elt's INDEX. */
2371 array_index_cmp (tree key
, tree index
)
2373 gcc_assert (TREE_CODE (key
) == INTEGER_CST
);
2375 switch (TREE_CODE (index
))
2378 return tree_int_cst_compare (key
, index
);
2381 tree lo
= TREE_OPERAND (index
, 0);
2382 tree hi
= TREE_OPERAND (index
, 1);
2383 if (tree_int_cst_lt (key
, lo
))
2385 else if (tree_int_cst_lt (hi
, key
))
2395 /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
2396 if none. If INSERT is true, insert a matching element rather than fail. */
2398 static HOST_WIDE_INT
2399 find_array_ctor_elt (tree ary
, tree dindex
, bool insert
)
2401 if (tree_int_cst_sgn (dindex
) < 0)
2404 unsigned HOST_WIDE_INT i
= tree_to_uhwi (dindex
);
2405 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ary
);
2406 unsigned HOST_WIDE_INT len
= vec_safe_length (elts
);
2408 unsigned HOST_WIDE_INT end
= len
;
2409 unsigned HOST_WIDE_INT begin
= 0;
2411 /* If the last element of the CONSTRUCTOR has its own index, we can assume
2412 that the same is true of the other elements and index directly. */
2415 tree cindex
= (*elts
)[end
- 1].index
;
2416 if (TREE_CODE (cindex
) == INTEGER_CST
2417 && compare_tree_int (cindex
, end
- 1) == 0)
2426 /* Otherwise, find a matching index by means of a binary search. */
2427 while (begin
!= end
)
2429 unsigned HOST_WIDE_INT middle
= (begin
+ end
) / 2;
2430 constructor_elt
&elt
= (*elts
)[middle
];
2431 tree idx
= elt
.index
;
2433 int cmp
= array_index_cmp (dindex
, idx
);
2440 if (insert
&& TREE_CODE (idx
) == RANGE_EXPR
)
2442 /* We need to split the range. */
2444 tree lo
= TREE_OPERAND (idx
, 0);
2445 tree hi
= TREE_OPERAND (idx
, 1);
2446 tree value
= elt
.value
;
2447 dindex
= fold_convert (sizetype
, dindex
);
2448 if (tree_int_cst_lt (lo
, dindex
))
2450 /* There are still some lower elts; shorten the range. */
2451 tree new_hi
= int_const_binop (MINUS_EXPR
, dindex
,
2453 if (tree_int_cst_equal (lo
, new_hi
))
2454 /* Only one element left, no longer a range. */
2457 TREE_OPERAND (idx
, 1) = new_hi
;
2458 /* Append the element we want to insert. */
2461 e
.value
= unshare_constructor (value
);
2462 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
, e
);
2465 /* No lower elts, the range elt is now ours. */
2468 if (tree_int_cst_lt (dindex
, hi
))
2470 /* There are still some higher elts; append a range. */
2471 tree new_lo
= int_const_binop (PLUS_EXPR
, dindex
,
2473 if (tree_int_cst_equal (new_lo
, hi
))
2476 e
.index
= build2 (RANGE_EXPR
, sizetype
, new_lo
, hi
);
2477 e
.value
= unshare_constructor (value
);
2478 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
+ 1, e
);
2487 constructor_elt e
= { dindex
, NULL_TREE
};
2488 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), end
, e
);
2495 /* Under the control of CTX, issue a detailed diagnostic for
2496 an out-of-bounds subscript INDEX into the expression ARRAY. */
2499 diag_array_subscript (location_t loc
, const constexpr_ctx
*ctx
, tree array
, tree index
)
2503 tree arraytype
= TREE_TYPE (array
);
2505 /* Convert the unsigned array subscript to a signed integer to avoid
2506 printing huge numbers for small negative values. */
2507 tree sidx
= fold_convert (ssizetype
, index
);
2508 STRIP_ANY_LOCATION_WRAPPER (array
);
2511 if (TYPE_DOMAIN (arraytype
))
2512 error_at (loc
, "array subscript value %qE is outside the bounds "
2513 "of array %qD of type %qT", sidx
, array
, arraytype
);
2515 error_at (loc
, "nonzero array subscript %qE is used with array %qD of "
2516 "type %qT with unknown bounds", sidx
, array
, arraytype
);
2517 inform (DECL_SOURCE_LOCATION (array
), "declared here");
2519 else if (TYPE_DOMAIN (arraytype
))
2520 error_at (loc
, "array subscript value %qE is outside the bounds "
2521 "of array type %qT", sidx
, arraytype
);
2523 error_at (loc
, "nonzero array subscript %qE is used with array of type %qT "
2524 "with unknown bounds", sidx
, arraytype
);
2528 /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
2532 get_array_or_vector_nelts (const constexpr_ctx
*ctx
, tree type
,
2533 bool *non_constant_p
, bool *overflow_p
)
2536 if (TREE_CODE (type
) == ARRAY_TYPE
)
2538 if (TYPE_DOMAIN (type
))
2539 nelts
= array_type_nelts_top (type
);
2541 nelts
= size_zero_node
;
2543 else if (VECTOR_TYPE_P (type
))
2544 nelts
= size_int (TYPE_VECTOR_SUBPARTS (type
));
2548 /* For VLAs, the number of elements won't be an integer constant. */
2549 nelts
= cxx_eval_constant_expression (ctx
, nelts
, false,
2550 non_constant_p
, overflow_p
);
2554 /* Extract element INDEX consisting of CHARS_PER_ELT chars from
2555 STRING_CST STRING. */
2558 extract_string_elt (tree string
, unsigned chars_per_elt
, unsigned index
)
2560 tree type
= cv_unqualified (TREE_TYPE (TREE_TYPE (string
)));
2563 if (chars_per_elt
== 1)
2564 r
= build_int_cst (type
, TREE_STRING_POINTER (string
)[index
]);
2567 const unsigned char *ptr
2568 = ((const unsigned char *)TREE_STRING_POINTER (string
)
2569 + index
* chars_per_elt
);
2570 r
= native_interpret_expr (type
, ptr
, chars_per_elt
);
2575 /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
2576 subscript, diagnose any problems with it, and return the result. */
2579 eval_and_check_array_index (const constexpr_ctx
*ctx
,
2580 tree t
, bool allow_one_past
,
2581 bool *non_constant_p
, bool *overflow_p
)
2583 location_t loc
= cp_expr_loc_or_input_loc (t
);
2584 tree ary
= TREE_OPERAND (t
, 0);
2585 t
= TREE_OPERAND (t
, 1);
2586 tree index
= cxx_eval_constant_expression (ctx
, t
, false,
2587 non_constant_p
, overflow_p
);
2588 VERIFY_CONSTANT (index
);
2590 if (!tree_fits_shwi_p (index
)
2591 || tree_int_cst_sgn (index
) < 0)
2593 diag_array_subscript (loc
, ctx
, ary
, index
);
2594 *non_constant_p
= true;
2598 tree nelts
= get_array_or_vector_nelts (ctx
, TREE_TYPE (ary
), non_constant_p
,
2600 VERIFY_CONSTANT (nelts
);
2602 ? !tree_int_cst_le (index
, nelts
)
2603 : !tree_int_cst_lt (index
, nelts
))
2605 diag_array_subscript (loc
, ctx
, ary
, index
);
2606 *non_constant_p
= true;
2613 /* Subroutine of cxx_eval_constant_expression.
2614 Attempt to reduce a reference to an array slot. */
2617 cxx_eval_array_reference (const constexpr_ctx
*ctx
, tree t
,
2619 bool *non_constant_p
, bool *overflow_p
)
2621 tree oldary
= TREE_OPERAND (t
, 0);
2622 tree ary
= cxx_eval_constant_expression (ctx
, oldary
,
2624 non_constant_p
, overflow_p
);
2625 if (*non_constant_p
)
2628 && TREE_CODE (ary
) == VIEW_CONVERT_EXPR
2629 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary
, 0)))
2630 && TREE_TYPE (t
) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary
, 0))))
2631 ary
= TREE_OPERAND (ary
, 0);
2633 tree oldidx
= TREE_OPERAND (t
, 1);
2634 tree index
= eval_and_check_array_index (ctx
, t
, lval
,
2635 non_constant_p
, overflow_p
);
2636 if (*non_constant_p
)
2639 if (lval
&& ary
== oldary
&& index
== oldidx
)
2642 return build4 (ARRAY_REF
, TREE_TYPE (t
), ary
, index
, NULL
, NULL
);
2644 unsigned len
= 0, elem_nchars
= 1;
2645 tree elem_type
= TREE_TYPE (TREE_TYPE (ary
));
2646 if (TREE_CODE (ary
) == CONSTRUCTOR
)
2647 len
= CONSTRUCTOR_NELTS (ary
);
2648 else if (TREE_CODE (ary
) == STRING_CST
)
2650 elem_nchars
= (TYPE_PRECISION (elem_type
)
2651 / TYPE_PRECISION (char_type_node
));
2652 len
= (unsigned) TREE_STRING_LENGTH (ary
) / elem_nchars
;
2654 else if (TREE_CODE (ary
) == VECTOR_CST
)
2655 /* We don't create variable-length VECTOR_CSTs. */
2656 len
= VECTOR_CST_NELTS (ary
).to_constant ();
2659 /* We can't do anything with other tree codes, so use
2660 VERIFY_CONSTANT to complain and fail. */
2661 VERIFY_CONSTANT (ary
);
2666 HOST_WIDE_INT i
= 0;
2667 if (TREE_CODE (ary
) == CONSTRUCTOR
)
2669 HOST_WIDE_INT ix
= find_array_ctor_elt (ary
, index
);
2676 i
= tree_to_shwi (index
);
2683 if (TREE_CODE (ary
) == CONSTRUCTOR
)
2684 r
= (*CONSTRUCTOR_ELTS (ary
))[i
].value
;
2685 else if (TREE_CODE (ary
) == VECTOR_CST
)
2686 r
= VECTOR_CST_ELT (ary
, i
);
2688 r
= extract_string_elt (ary
, elem_nchars
, i
);
2691 /* Don't VERIFY_CONSTANT here. */
2694 /* Otherwise the element doesn't have a value yet. */
2699 if (TREE_CODE (ary
) == CONSTRUCTOR
2700 && CONSTRUCTOR_NO_CLEARING (ary
))
2702 /* 'ary' is part of the aggregate initializer we're currently
2703 building; if there's no initializer for this element yet,
2706 error ("accessing uninitialized array element");
2707 *non_constant_p
= true;
2711 /* If it's within the array bounds but doesn't have an explicit
2712 initializer, it's value-initialized. */
2713 tree val
= build_value_init (elem_type
, tf_warning_or_error
);
2714 return cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
2718 /* Subroutine of cxx_eval_constant_expression.
2719 Attempt to reduce a field access of a value of class type. */
2722 cxx_eval_component_reference (const constexpr_ctx
*ctx
, tree t
,
2724 bool *non_constant_p
, bool *overflow_p
)
2726 unsigned HOST_WIDE_INT i
;
2729 tree part
= TREE_OPERAND (t
, 1);
2730 tree orig_whole
= TREE_OPERAND (t
, 0);
2731 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
2733 non_constant_p
, overflow_p
);
2734 if (INDIRECT_REF_P (whole
)
2735 && integer_zerop (TREE_OPERAND (whole
, 0)))
2738 error ("dereferencing a null pointer in %qE", orig_whole
);
2739 *non_constant_p
= true;
2743 if (TREE_CODE (whole
) == PTRMEM_CST
)
2744 whole
= cplus_expand_constant (whole
);
2745 if (whole
== orig_whole
)
2748 return fold_build3 (COMPONENT_REF
, TREE_TYPE (t
),
2749 whole
, part
, NULL_TREE
);
2750 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
2752 if (!*non_constant_p
&& TREE_CODE (whole
) != CONSTRUCTOR
)
2755 error ("%qE is not a constant expression", orig_whole
);
2756 *non_constant_p
= true;
2758 if (DECL_MUTABLE_P (part
))
2761 error ("mutable %qD is not usable in a constant expression", part
);
2762 *non_constant_p
= true;
2764 if (*non_constant_p
)
2766 bool pmf
= TYPE_PTRMEMFUNC_P (TREE_TYPE (whole
));
2767 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
2769 /* Use name match for PMF fields, as a variant will have a
2770 different FIELD_DECL with a different type. */
2771 if (pmf
? DECL_NAME (field
) == DECL_NAME (part
)
2777 /* We're in the middle of initializing it. */
2781 if (TREE_CODE (TREE_TYPE (whole
)) == UNION_TYPE
2782 && CONSTRUCTOR_NELTS (whole
) > 0)
2784 /* DR 1188 says we don't have to deal with this. */
2786 error ("accessing %qD member instead of initialized %qD member in "
2787 "constant expression", part
, CONSTRUCTOR_ELT (whole
, 0)->index
);
2788 *non_constant_p
= true;
2792 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
2793 classes never get represented; throw together a value now. */
2794 if (is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
2795 return build_constructor (TREE_TYPE (t
), NULL
);
2797 gcc_assert (DECL_CONTEXT (part
) == TYPE_MAIN_VARIANT (TREE_TYPE (whole
)));
2799 if (CONSTRUCTOR_NO_CLEARING (whole
))
2801 /* 'whole' is part of the aggregate initializer we're currently
2802 building; if there's no initializer for this member yet, that's an
2805 error ("accessing uninitialized member %qD", part
);
2806 *non_constant_p
= true;
2810 /* If there's no explicit init for this field, it's value-initialized. */
2811 value
= build_value_init (TREE_TYPE (t
), tf_warning_or_error
);
2812 return cxx_eval_constant_expression (ctx
, value
,
2814 non_constant_p
, overflow_p
);
2817 /* Subroutine of cxx_eval_constant_expression.
2818 Attempt to reduce a field access of a value of class type that is
2819 expressed as a BIT_FIELD_REF. */
2822 cxx_eval_bit_field_ref (const constexpr_ctx
*ctx
, tree t
,
2824 bool *non_constant_p
, bool *overflow_p
)
2826 tree orig_whole
= TREE_OPERAND (t
, 0);
2827 tree retval
, fldval
, utype
, mask
;
2828 bool fld_seen
= false;
2829 HOST_WIDE_INT istart
, isize
;
2830 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
2832 non_constant_p
, overflow_p
);
2833 tree start
, field
, value
;
2834 unsigned HOST_WIDE_INT i
;
2836 if (whole
== orig_whole
)
2838 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
2840 if (!*non_constant_p
2841 && TREE_CODE (whole
) != VECTOR_CST
2842 && TREE_CODE (whole
) != CONSTRUCTOR
)
2845 error ("%qE is not a constant expression", orig_whole
);
2846 *non_constant_p
= true;
2848 if (*non_constant_p
)
2851 if (TREE_CODE (whole
) == VECTOR_CST
)
2852 return fold_ternary (BIT_FIELD_REF
, TREE_TYPE (t
), whole
,
2853 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 2));
2855 start
= TREE_OPERAND (t
, 2);
2856 istart
= tree_to_shwi (start
);
2857 isize
= tree_to_shwi (TREE_OPERAND (t
, 1));
2858 utype
= TREE_TYPE (t
);
2859 if (!TYPE_UNSIGNED (utype
))
2860 utype
= build_nonstandard_integer_type (TYPE_PRECISION (utype
), 1);
2861 retval
= build_int_cst (utype
, 0);
2862 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
2864 tree bitpos
= bit_position (field
);
2865 if (bitpos
== start
&& DECL_SIZE (field
) == TREE_OPERAND (t
, 1))
2867 if (TREE_CODE (TREE_TYPE (field
)) == INTEGER_TYPE
2868 && TREE_CODE (value
) == INTEGER_CST
2869 && tree_fits_shwi_p (bitpos
)
2870 && tree_fits_shwi_p (DECL_SIZE (field
)))
2872 HOST_WIDE_INT bit
= tree_to_shwi (bitpos
);
2873 HOST_WIDE_INT sz
= tree_to_shwi (DECL_SIZE (field
));
2874 HOST_WIDE_INT shift
;
2875 if (bit
>= istart
&& bit
+ sz
<= istart
+ isize
)
2877 fldval
= fold_convert (utype
, value
);
2878 mask
= build_int_cst_type (utype
, -1);
2879 mask
= fold_build2 (LSHIFT_EXPR
, utype
, mask
,
2880 size_int (TYPE_PRECISION (utype
) - sz
));
2881 mask
= fold_build2 (RSHIFT_EXPR
, utype
, mask
,
2882 size_int (TYPE_PRECISION (utype
) - sz
));
2883 fldval
= fold_build2 (BIT_AND_EXPR
, utype
, fldval
, mask
);
2884 shift
= bit
- istart
;
2885 if (BYTES_BIG_ENDIAN
)
2886 shift
= TYPE_PRECISION (utype
) - shift
- sz
;
2887 fldval
= fold_build2 (LSHIFT_EXPR
, utype
, fldval
,
2889 retval
= fold_build2 (BIT_IOR_EXPR
, utype
, retval
, fldval
);
2895 return fold_convert (TREE_TYPE (t
), retval
);
2897 return error_mark_node
;
2900 /* Subroutine of cxx_eval_constant_expression.
2901 Evaluate a short-circuited logical expression T in the context
2902 of a given constexpr CALL. BAILOUT_VALUE is the value for
2903 early return. CONTINUE_VALUE is used here purely for
2904 sanity check purposes. */
2907 cxx_eval_logical_expression (const constexpr_ctx
*ctx
, tree t
,
2908 tree bailout_value
, tree continue_value
,
2910 bool *non_constant_p
, bool *overflow_p
)
2913 tree lhs
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
2915 non_constant_p
, overflow_p
);
2916 VERIFY_CONSTANT (lhs
);
2917 if (tree_int_cst_equal (lhs
, bailout_value
))
2919 gcc_assert (tree_int_cst_equal (lhs
, continue_value
));
2920 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
2921 lval
, non_constant_p
,
2923 VERIFY_CONSTANT (r
);
2927 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
2928 CONSTRUCTOR elements to initialize (part of) an object containing that
2929 field. Return a pointer to the constructor_elt corresponding to the
2930 initialization of the field. */
2932 static constructor_elt
*
2933 base_field_constructor_elt (vec
<constructor_elt
, va_gc
> *v
, tree ref
)
2935 tree aggr
= TREE_OPERAND (ref
, 0);
2936 tree field
= TREE_OPERAND (ref
, 1);
2938 constructor_elt
*ce
;
2940 gcc_assert (TREE_CODE (ref
) == COMPONENT_REF
);
2942 if (TREE_CODE (aggr
) == COMPONENT_REF
)
2944 constructor_elt
*base_ce
2945 = base_field_constructor_elt (v
, aggr
);
2946 v
= CONSTRUCTOR_ELTS (base_ce
->value
);
2949 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
2950 if (ce
->index
== field
)
2957 /* Some of the expressions fed to the constexpr mechanism are calls to
2958 constructors, which have type void. In that case, return the type being
2959 initialized by the constructor. */
2962 initialized_type (tree t
)
2966 tree type
= TREE_TYPE (t
);
2967 if (TREE_CODE (t
) == CALL_EXPR
)
2969 /* A constructor call has void type, so we need to look deeper. */
2970 tree fn
= get_function_named_in_call (t
);
2971 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
2972 && DECL_CXX_CONSTRUCTOR_P (fn
))
2973 type
= DECL_CONTEXT (fn
);
2975 else if (TREE_CODE (t
) == COMPOUND_EXPR
)
2976 return initialized_type (TREE_OPERAND (t
, 1));
2977 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
2978 type
= TREE_TYPE (AGGR_INIT_EXPR_SLOT (t
));
2979 return cv_unqualified (type
);
2982 /* We're about to initialize element INDEX of an array or class from VALUE.
2983 Set up NEW_CTX appropriately by adjusting .object to refer to the
2984 subobject and creating a new CONSTRUCTOR if the element is itself
2985 a class or array. */
2988 init_subob_ctx (const constexpr_ctx
*ctx
, constexpr_ctx
&new_ctx
,
2989 tree index
, tree
&value
)
2993 if (index
&& TREE_CODE (index
) != INTEGER_CST
2994 && TREE_CODE (index
) != FIELD_DECL
)
2995 /* This won't have an element in the new CONSTRUCTOR. */
2998 tree type
= initialized_type (value
);
2999 if (!AGGREGATE_TYPE_P (type
) && !VECTOR_TYPE_P (type
))
3000 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
3003 /* The sub-aggregate initializer might contain a placeholder;
3004 update object to refer to the subobject and ctor to refer to
3005 the (newly created) sub-initializer. */
3007 new_ctx
.object
= build_ctor_subob_ref (index
, type
, ctx
->object
);
3008 tree elt
= build_constructor (type
, NULL
);
3009 CONSTRUCTOR_NO_CLEARING (elt
) = true;
3012 if (TREE_CODE (value
) == TARGET_EXPR
)
3013 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
3014 value
= TARGET_EXPR_INITIAL (value
);
3017 /* We're about to process an initializer for a class or array TYPE. Make
3018 sure that CTX is set up appropriately. */
3021 verify_ctor_sanity (const constexpr_ctx
*ctx
, tree type
)
3023 /* We don't bother building a ctor for an empty base subobject. */
3024 if (is_empty_class (type
))
3027 /* We're in the middle of an initializer that might involve placeholders;
3028 our caller should have created a CONSTRUCTOR for us to put the
3029 initializer into. We will either return that constructor or T. */
3030 gcc_assert (ctx
->ctor
);
3031 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3032 (type
, TREE_TYPE (ctx
->ctor
)));
3033 /* We used to check that ctx->ctor was empty, but that isn't the case when
3034 the object is zero-initialized before calling the constructor. */
3037 tree otype
= TREE_TYPE (ctx
->object
);
3038 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type
, otype
)
3039 /* Handle flexible array members. */
3040 || (TREE_CODE (otype
) == ARRAY_TYPE
3041 && TYPE_DOMAIN (otype
) == NULL_TREE
3042 && TREE_CODE (type
) == ARRAY_TYPE
3043 && (same_type_ignoring_top_level_qualifiers_p
3044 (TREE_TYPE (type
), TREE_TYPE (otype
)))));
3046 gcc_assert (!ctx
->object
|| !DECL_P (ctx
->object
)
3047 || *(ctx
->values
->get (ctx
->object
)) == ctx
->ctor
);
3050 /* Subroutine of cxx_eval_constant_expression.
3051 The expression tree T denotes a C-style array or a C-style
3052 aggregate. Reduce it to a constant expression. */
3055 cxx_eval_bare_aggregate (const constexpr_ctx
*ctx
, tree t
,
3057 bool *non_constant_p
, bool *overflow_p
)
3059 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
3060 bool changed
= false;
3061 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t
));
3062 tree type
= TREE_TYPE (t
);
3064 constexpr_ctx new_ctx
;
3065 if (TYPE_PTRMEMFUNC_P (type
) || VECTOR_TYPE_P (type
))
3067 /* We don't really need the ctx->ctor business for a PMF or
3068 vector, but it's simpler to use the same code. */
3070 new_ctx
.ctor
= build_constructor (type
, NULL
);
3071 new_ctx
.object
= NULL_TREE
;
3074 verify_ctor_sanity (ctx
, type
);
3075 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
3076 vec_alloc (*p
, vec_safe_length (v
));
3080 bool constant_p
= true;
3081 bool side_effects_p
= false;
3082 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, index
, value
)
3084 tree orig_value
= value
;
3085 init_subob_ctx (ctx
, new_ctx
, index
, value
);
3086 if (new_ctx
.ctor
!= ctx
->ctor
)
3087 /* If we built a new CONSTRUCTOR, attach it now so that other
3088 initializers can refer to it. */
3089 CONSTRUCTOR_APPEND_ELT (*p
, index
, new_ctx
.ctor
);
3090 tree elt
= cxx_eval_constant_expression (&new_ctx
, value
,
3092 non_constant_p
, overflow_p
);
3093 /* Don't VERIFY_CONSTANT here. */
3094 if (ctx
->quiet
&& *non_constant_p
)
3096 if (elt
!= orig_value
)
3099 if (!TREE_CONSTANT (elt
))
3101 if (TREE_SIDE_EFFECTS (elt
))
3102 side_effects_p
= true;
3103 if (index
&& TREE_CODE (index
) == COMPONENT_REF
)
3105 /* This is an initialization of a vfield inside a base
3106 subaggregate that we already initialized; push this
3107 initialization into the previous initialization. */
3108 constructor_elt
*inner
= base_field_constructor_elt (*p
, index
);
3113 && (TREE_CODE (index
) == NOP_EXPR
3114 || TREE_CODE (index
) == POINTER_PLUS_EXPR
))
3116 /* This is an initializer for an empty base; now that we've
3117 checked that it's constant, we can ignore it. */
3118 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index
))));
3123 if (new_ctx
.ctor
!= ctx
->ctor
)
3125 /* We appended this element above; update the value. */
3126 gcc_assert ((*p
)->last().index
== index
);
3127 (*p
)->last().value
= elt
;
3130 CONSTRUCTOR_APPEND_ELT (*p
, index
, elt
);
3131 /* Adding or replacing an element might change the ctor's flags. */
3132 TREE_CONSTANT (ctx
->ctor
) = constant_p
;
3133 TREE_SIDE_EFFECTS (ctx
->ctor
) = side_effects_p
;
3136 if (*non_constant_p
|| !changed
)
3139 /* We're done building this CONSTRUCTOR, so now we can interpret an
3140 element without an explicit initializer as value-initialized. */
3141 CONSTRUCTOR_NO_CLEARING (t
) = false;
3142 TREE_CONSTANT (t
) = constant_p
;
3143 TREE_SIDE_EFFECTS (t
) = side_effects_p
;
3144 if (VECTOR_TYPE_P (type
))
3149 /* Subroutine of cxx_eval_constant_expression.
3150 The expression tree T is a VEC_INIT_EXPR which denotes the desired
3151 initialization of a non-static data member of array type. Reduce it to a
3154 Note that apart from value-initialization (when VALUE_INIT is true),
3155 this is only intended to support value-initialization and the
3156 initializations done by defaulted constructors for classes with
3157 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
3158 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
3159 for the copy/move constructor. */
3162 cxx_eval_vec_init_1 (const constexpr_ctx
*ctx
, tree atype
, tree init
,
3163 bool value_init
, bool lval
,
3164 bool *non_constant_p
, bool *overflow_p
)
3166 tree elttype
= TREE_TYPE (atype
);
3167 verify_ctor_sanity (ctx
, atype
);
3168 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
3169 bool pre_init
= false;
3170 unsigned HOST_WIDE_INT i
;
3171 tsubst_flags_t complain
= ctx
->quiet
? tf_none
: tf_warning_or_error
;
3173 /* For the default constructor, build up a call to the default
3174 constructor of the element type. We only need to handle class types
3175 here, as for a constructor to be constexpr, all members must be
3176 initialized, which for a defaulted default constructor means they must
3177 be of a class type with a constexpr default constructor. */
3178 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
3179 /* We only do this at the lowest level. */;
3180 else if (value_init
)
3182 init
= build_value_init (elttype
, complain
);
3187 releasing_vec argvec
;
3188 init
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
3189 &argvec
, elttype
, LOOKUP_NORMAL
,
3191 init
= build_aggr_init_expr (elttype
, init
);
3195 tree nelts
= get_array_or_vector_nelts (ctx
, atype
, non_constant_p
,
3197 unsigned HOST_WIDE_INT max
= tree_to_uhwi (nelts
);
3198 for (i
= 0; i
< max
; ++i
)
3200 tree idx
= build_int_cst (size_type_node
, i
);
3203 constexpr_ctx new_ctx
;
3204 init_subob_ctx (ctx
, new_ctx
, idx
, pre_init
? init
: elttype
);
3205 if (new_ctx
.ctor
!= ctx
->ctor
)
3206 CONSTRUCTOR_APPEND_ELT (*p
, idx
, new_ctx
.ctor
);
3207 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
3209 /* A multidimensional array; recurse. */
3210 if (value_init
|| init
== NULL_TREE
)
3212 eltinit
= NULL_TREE
;
3216 eltinit
= cp_build_array_ref (input_location
, init
, idx
, complain
);
3217 eltinit
= cxx_eval_vec_init_1 (&new_ctx
, elttype
, eltinit
, value_init
,
3219 non_constant_p
, overflow_p
);
3223 /* Initializing an element using value or default initialization
3224 we just pre-built above. */
3225 if (init
== void_node
)
3226 /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
3228 eltinit
= cxx_eval_constant_expression (&new_ctx
, init
, lval
,
3229 non_constant_p
, overflow_p
);
3234 /* Copying an element. */
3235 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3236 (atype
, TREE_TYPE (init
)));
3237 eltinit
= cp_build_array_ref (input_location
, init
, idx
, complain
);
3238 if (!lvalue_p (init
))
3239 eltinit
= move (eltinit
);
3240 eltinit
= force_rvalue (eltinit
, complain
);
3241 eltinit
= cxx_eval_constant_expression (&new_ctx
, eltinit
, lval
,
3242 non_constant_p
, overflow_p
);
3244 if (*non_constant_p
&& !ctx
->quiet
)
3246 if (new_ctx
.ctor
!= ctx
->ctor
)
3248 /* We appended this element above; update the value. */
3249 gcc_assert ((*p
)->last().index
== idx
);
3250 (*p
)->last().value
= eltinit
;
3253 CONSTRUCTOR_APPEND_ELT (*p
, idx
, eltinit
);
3254 /* Reuse the result of cxx_eval_constant_expression call
3255 from the first iteration to all others if it is a constant
3256 initializer that doesn't require relocations. */
3259 && (eltinit
== NULL_TREE
3260 || (initializer_constant_valid_p (eltinit
, TREE_TYPE (eltinit
))
3261 == null_pointer_node
)))
3263 if (new_ctx
.ctor
!= ctx
->ctor
)
3264 eltinit
= new_ctx
.ctor
;
3265 tree range
= build2 (RANGE_EXPR
, size_type_node
,
3266 build_int_cst (size_type_node
, 1),
3267 build_int_cst (size_type_node
, max
- 1));
3268 CONSTRUCTOR_APPEND_ELT (*p
, range
, unshare_constructor (eltinit
));
3272 vec_safe_reserve (*p
, max
);
3275 if (!*non_constant_p
)
3278 CONSTRUCTOR_NO_CLEARING (init
) = false;
3284 cxx_eval_vec_init (const constexpr_ctx
*ctx
, tree t
,
3286 bool *non_constant_p
, bool *overflow_p
)
3288 tree atype
= TREE_TYPE (t
);
3289 tree init
= VEC_INIT_EXPR_INIT (t
);
3290 tree r
= cxx_eval_vec_init_1 (ctx
, atype
, init
,
3291 VEC_INIT_EXPR_VALUE_INIT (t
),
3292 lval
, non_constant_p
, overflow_p
);
3293 if (*non_constant_p
)
3299 /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
3300 where the desired type is an array of unknown bounds because the variable
3301 has had its bounds deduced since the wrapping expression was created. */
3304 same_type_ignoring_tlq_and_bounds_p (tree type1
, tree type2
)
3306 while (TREE_CODE (type1
) == ARRAY_TYPE
3307 && TREE_CODE (type2
) == ARRAY_TYPE
3308 && (!TYPE_DOMAIN (type1
) || !TYPE_DOMAIN (type2
)))
3310 type1
= TREE_TYPE (type1
);
3311 type2
= TREE_TYPE (type2
);
3313 return same_type_ignoring_top_level_qualifiers_p (type1
, type2
);
3316 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
3317 match. We want to be less strict for simple *& folding; if we have a
3318 non-const temporary that we access through a const pointer, that should
3319 work. We handle this here rather than change fold_indirect_ref_1
3320 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
3321 don't really make sense outside of constant expression evaluation. Also
3322 we want to allow folding to COMPONENT_REF, which could cause trouble
3323 with TBAA in fold_indirect_ref_1.
3325 Try to keep this function synced with fold_indirect_ref_1. */
3328 cxx_fold_indirect_ref (location_t loc
, tree type
, tree op0
, bool *empty_base
)
3332 poly_uint64 const_op01
;
3335 subtype
= TREE_TYPE (sub
);
3336 if (!INDIRECT_TYPE_P (subtype
))
3339 if (TREE_CODE (sub
) == ADDR_EXPR
)
3341 tree op
= TREE_OPERAND (sub
, 0);
3342 tree optype
= TREE_TYPE (op
);
3344 /* *&CONST_DECL -> to the value of the const decl. */
3345 if (TREE_CODE (op
) == CONST_DECL
)
3346 return DECL_INITIAL (op
);
3347 /* *&p => p; make sure to handle *&"str"[cst] here. */
3348 if (same_type_ignoring_tlq_and_bounds_p (optype
, type
))
3350 tree fop
= fold_read_from_constant_string (op
);
3356 /* *(foo *)&fooarray => fooarray[0] */
3357 else if (TREE_CODE (optype
) == ARRAY_TYPE
3358 && (same_type_ignoring_top_level_qualifiers_p
3359 (type
, TREE_TYPE (optype
))))
3361 tree type_domain
= TYPE_DOMAIN (optype
);
3362 tree min_val
= size_zero_node
;
3363 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
3364 min_val
= TYPE_MIN_VALUE (type_domain
);
3365 return build4_loc (loc
, ARRAY_REF
, type
, op
, min_val
,
3366 NULL_TREE
, NULL_TREE
);
3368 /* *(foo *)&complexfoo => __real__ complexfoo */
3369 else if (TREE_CODE (optype
) == COMPLEX_TYPE
3370 && (same_type_ignoring_top_level_qualifiers_p
3371 (type
, TREE_TYPE (optype
))))
3372 return fold_build1_loc (loc
, REALPART_EXPR
, type
, op
);
3373 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
3374 else if (VECTOR_TYPE_P (optype
)
3375 && (same_type_ignoring_top_level_qualifiers_p
3376 (type
, TREE_TYPE (optype
))))
3378 tree part_width
= TYPE_SIZE (type
);
3379 tree index
= bitsize_int (0);
3380 return fold_build3_loc (loc
, BIT_FIELD_REF
, type
, op
, part_width
,
3383 /* Also handle conversion to an empty base class, which
3384 is represented with a NOP_EXPR. */
3385 else if (is_empty_class (type
)
3386 && CLASS_TYPE_P (optype
)
3387 && DERIVED_FROM_P (type
, optype
))
3392 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
3393 else if (RECORD_OR_UNION_TYPE_P (optype
))
3395 tree field
= TYPE_FIELDS (optype
);
3396 for (; field
; field
= DECL_CHAIN (field
))
3397 if (TREE_CODE (field
) == FIELD_DECL
3398 && TREE_TYPE (field
) != error_mark_node
3399 && integer_zerop (byte_position (field
))
3400 && (same_type_ignoring_top_level_qualifiers_p
3401 (TREE_TYPE (field
), type
)))
3402 return fold_build3 (COMPONENT_REF
, type
, op
, field
, NULL_TREE
);
3405 else if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
3406 && poly_int_tree_p (TREE_OPERAND (sub
, 1), &const_op01
))
3408 tree op00
= TREE_OPERAND (sub
, 0);
3409 tree op01
= TREE_OPERAND (sub
, 1);
3412 if (TREE_CODE (op00
) == ADDR_EXPR
)
3415 op00
= TREE_OPERAND (op00
, 0);
3416 op00type
= TREE_TYPE (op00
);
3418 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
3419 if (VECTOR_TYPE_P (op00type
)
3420 && same_type_ignoring_top_level_qualifiers_p
3421 (type
, TREE_TYPE (op00type
))
3422 /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
3423 but we want to treat offsets with MSB set as negative.
3424 For the code below negative offsets are invalid and
3425 TYPE_SIZE of the element is something unsigned, so
3426 check whether op01 fits into poly_int64, which implies
3427 it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
3428 then just use poly_uint64 because we want to treat the
3429 value as unsigned. */
3430 && tree_fits_poly_int64_p (op01
))
3432 tree part_width
= TYPE_SIZE (type
);
3433 poly_uint64 max_offset
3434 = (tree_to_uhwi (part_width
) / BITS_PER_UNIT
3435 * TYPE_VECTOR_SUBPARTS (op00type
));
3436 if (known_lt (const_op01
, max_offset
))
3438 tree index
= bitsize_int (const_op01
* BITS_PER_UNIT
);
3439 return fold_build3_loc (loc
,
3440 BIT_FIELD_REF
, type
, op00
,
3444 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
3445 else if (TREE_CODE (op00type
) == COMPLEX_TYPE
3446 && (same_type_ignoring_top_level_qualifiers_p
3447 (type
, TREE_TYPE (op00type
))))
3449 if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type
)),
3451 return fold_build1_loc (loc
, IMAGPART_EXPR
, type
, op00
);
3453 /* ((foo *)&fooarray)[1] => fooarray[1] */
3454 else if (TREE_CODE (op00type
) == ARRAY_TYPE
3455 && (same_type_ignoring_top_level_qualifiers_p
3456 (type
, TREE_TYPE (op00type
))))
3458 tree type_domain
= TYPE_DOMAIN (op00type
);
3459 tree min_val
= size_zero_node
;
3460 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
3461 min_val
= TYPE_MIN_VALUE (type_domain
);
3462 offset_int off
= wi::to_offset (op01
);
3463 offset_int el_sz
= wi::to_offset (TYPE_SIZE_UNIT (type
));
3464 offset_int remainder
;
3465 off
= wi::divmod_trunc (off
, el_sz
, SIGNED
, &remainder
);
3466 if (remainder
== 0 && TREE_CODE (min_val
) == INTEGER_CST
)
3468 off
= off
+ wi::to_offset (min_val
);
3469 op01
= wide_int_to_tree (sizetype
, off
);
3470 return build4_loc (loc
, ARRAY_REF
, type
, op00
, op01
,
3471 NULL_TREE
, NULL_TREE
);
3474 /* Also handle conversion to an empty base class, which
3475 is represented with a NOP_EXPR. */
3476 else if (is_empty_class (type
)
3477 && CLASS_TYPE_P (op00type
)
3478 && DERIVED_FROM_P (type
, op00type
))
3483 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
3484 else if (RECORD_OR_UNION_TYPE_P (op00type
))
3486 tree field
= TYPE_FIELDS (op00type
);
3487 for (; field
; field
= DECL_CHAIN (field
))
3488 if (TREE_CODE (field
) == FIELD_DECL
3489 && TREE_TYPE (field
) != error_mark_node
3490 && tree_int_cst_equal (byte_position (field
), op01
)
3491 && (same_type_ignoring_top_level_qualifiers_p
3492 (TREE_TYPE (field
), type
)))
3493 return fold_build3 (COMPONENT_REF
, type
, op00
,
3498 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
3499 else if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
3500 && (same_type_ignoring_top_level_qualifiers_p
3501 (type
, TREE_TYPE (TREE_TYPE (subtype
)))))
3504 tree min_val
= size_zero_node
;
3506 = cxx_fold_indirect_ref (loc
, TREE_TYPE (subtype
), sub
, NULL
);
3510 sub
= build1_loc (loc
, INDIRECT_REF
, TREE_TYPE (subtype
), sub
);
3511 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
3512 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
3513 min_val
= TYPE_MIN_VALUE (type_domain
);
3514 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
3522 cxx_eval_indirect_ref (const constexpr_ctx
*ctx
, tree t
,
3524 bool *non_constant_p
, bool *overflow_p
)
3526 tree orig_op0
= TREE_OPERAND (t
, 0);
3527 bool empty_base
= false;
3529 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
3530 operand is an integer-zero. Otherwise reject the MEM_REF for now. */
3532 if (TREE_CODE (t
) == MEM_REF
3533 && (!TREE_OPERAND (t
, 1) || !integer_zerop (TREE_OPERAND (t
, 1))))
3535 gcc_assert (ctx
->quiet
);
3536 *non_constant_p
= true;
3540 /* First try to simplify it directly. */
3541 tree r
= cxx_fold_indirect_ref (EXPR_LOCATION (t
), TREE_TYPE (t
), orig_op0
,
3545 /* If that didn't work, evaluate the operand first. */
3546 tree op0
= cxx_eval_constant_expression (ctx
, orig_op0
,
3547 /*lval*/false, non_constant_p
,
3549 /* Don't VERIFY_CONSTANT here. */
3550 if (*non_constant_p
)
3553 if (!lval
&& integer_zerop (op0
))
3556 error ("dereferencing a null pointer");
3557 *non_constant_p
= true;
3561 r
= cxx_fold_indirect_ref (EXPR_LOCATION (t
), TREE_TYPE (t
), op0
,
3565 /* We couldn't fold to a constant value. Make sure it's not
3566 something we should have been able to fold. */
3569 if (TREE_CODE (sub
) == ADDR_EXPR
)
3571 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
3572 (TREE_TYPE (TREE_TYPE (sub
)), TREE_TYPE (t
)));
3573 /* DR 1188 says we don't have to deal with this. */
3575 error ("accessing value of %qE through a %qT glvalue in a "
3576 "constant expression", build_fold_indirect_ref (sub
),
3578 *non_constant_p
= true;
3582 if (lval
&& op0
!= orig_op0
)
3583 return build1 (INDIRECT_REF
, TREE_TYPE (t
), op0
);
3585 VERIFY_CONSTANT (t
);
3590 r
= cxx_eval_constant_expression (ctx
, r
,
3591 lval
, non_constant_p
, overflow_p
);
3592 if (*non_constant_p
)
3595 /* If we're pulling out the value of an empty base, just return an empty
3597 if (empty_base
&& !lval
)
3599 r
= build_constructor (TREE_TYPE (t
), NULL
);
3600 TREE_CONSTANT (r
) = true;
3606 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
3607 Shared between potential_constant_expression and
3608 cxx_eval_constant_expression. */
3611 non_const_var_error (tree r
)
3613 tree type
= TREE_TYPE (r
);
3614 error ("the value of %qD is not usable in a constant "
3616 /* Avoid error cascade. */
3617 if (DECL_INITIAL (r
) == error_mark_node
)
3619 if (DECL_DECLARED_CONSTEXPR_P (r
))
3620 inform (DECL_SOURCE_LOCATION (r
),
3621 "%qD used in its own initializer", r
);
3622 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
3624 if (!CP_TYPE_CONST_P (type
))
3625 inform (DECL_SOURCE_LOCATION (r
),
3626 "%q#D is not const", r
);
3627 else if (CP_TYPE_VOLATILE_P (type
))
3628 inform (DECL_SOURCE_LOCATION (r
),
3629 "%q#D is volatile", r
);
3630 else if (!DECL_INITIAL (r
)
3631 || !TREE_CONSTANT (DECL_INITIAL (r
))
3632 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
))
3633 inform (DECL_SOURCE_LOCATION (r
),
3634 "%qD was not initialized with a constant "
3639 else if (TYPE_REF_P (type
))
3640 inform (DECL_SOURCE_LOCATION (r
),
3641 "%qD was not initialized with a constant "
3645 if (cxx_dialect
>= cxx11
&& !DECL_DECLARED_CONSTEXPR_P (r
))
3646 inform (DECL_SOURCE_LOCATION (r
),
3647 "%qD was not declared %<constexpr%>", r
);
3649 inform (DECL_SOURCE_LOCATION (r
),
3650 "%qD does not have integral or enumeration type",
3655 /* Subroutine of cxx_eval_constant_expression.
3656 Like cxx_eval_unary_expression, except for trinary expressions. */
3659 cxx_eval_trinary_expression (const constexpr_ctx
*ctx
, tree t
,
3661 bool *non_constant_p
, bool *overflow_p
)
3667 for (i
= 0; i
< 3; i
++)
3669 args
[i
] = cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, i
),
3671 non_constant_p
, overflow_p
);
3672 VERIFY_CONSTANT (args
[i
]);
3675 val
= fold_ternary_loc (EXPR_LOCATION (t
), TREE_CODE (t
), TREE_TYPE (t
),
3676 args
[0], args
[1], args
[2]);
3677 if (val
== NULL_TREE
)
3679 VERIFY_CONSTANT (val
);
3683 /* True if T was declared in a function declared to be constexpr, and
3684 therefore potentially constant in C++14. */
3687 var_in_constexpr_fn (tree t
)
3689 tree ctx
= DECL_CONTEXT (t
);
3690 return (ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
3691 && DECL_DECLARED_CONSTEXPR_P (ctx
));
3694 /* True if T was declared in a function that might be constexpr: either a
3695 function that was declared constexpr, or a C++17 lambda op(). */
3698 var_in_maybe_constexpr_fn (tree t
)
3700 if (cxx_dialect
>= cxx17
3701 && DECL_FUNCTION_SCOPE_P (t
)
3702 && LAMBDA_FUNCTION_P (DECL_CONTEXT (t
)))
3704 return var_in_constexpr_fn (t
);
3707 /* We're assigning INIT to TARGET. In do_build_copy_constructor and
3708 build_over_call we implement trivial copy of a class with tail padding using
3709 assignment of character arrays, which is valid in normal code, but not in
3710 constexpr evaluation. We don't need to worry about clobbering tail padding
3711 in constexpr evaluation, so strip the type punning. */
3714 maybe_simplify_trivial_copy (tree
&target
, tree
&init
)
3716 if (TREE_CODE (target
) == MEM_REF
3717 && TREE_CODE (init
) == MEM_REF
3718 && TREE_TYPE (target
) == TREE_TYPE (init
)
3719 && TREE_CODE (TREE_TYPE (target
)) == ARRAY_TYPE
3720 && TREE_TYPE (TREE_TYPE (target
)) == unsigned_char_type_node
)
3722 target
= build_fold_indirect_ref (TREE_OPERAND (target
, 0));
3723 init
= build_fold_indirect_ref (TREE_OPERAND (init
, 0));
3727 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
3730 cxx_eval_store_expression (const constexpr_ctx
*ctx
, tree t
,
3732 bool *non_constant_p
, bool *overflow_p
)
3734 constexpr_ctx new_ctx
= *ctx
;
3736 tree init
= TREE_OPERAND (t
, 1);
3737 if (TREE_CLOBBER_P (init
))
3738 /* Just ignore clobbers. */
3741 /* First we figure out where we're storing to. */
3742 tree target
= TREE_OPERAND (t
, 0);
3744 maybe_simplify_trivial_copy (target
, init
);
3746 tree type
= TREE_TYPE (target
);
3747 bool preeval
= SCALAR_TYPE_P (type
) || TREE_CODE (t
) == MODIFY_EXPR
;
3750 /* Evaluate the value to be stored without knowing what object it will be
3751 stored in, so that any side-effects happen first. */
3752 if (!SCALAR_TYPE_P (type
))
3753 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
3754 init
= cxx_eval_constant_expression (&new_ctx
, init
, false,
3755 non_constant_p
, overflow_p
);
3756 if (*non_constant_p
)
3760 bool evaluated
= false;
3763 /* If we want to return a reference to the target, we need to evaluate it
3764 as a whole; otherwise, only evaluate the innermost piece to avoid
3765 building up unnecessary *_REFs. */
3766 target
= cxx_eval_constant_expression (ctx
, target
, true,
3767 non_constant_p
, overflow_p
);
3769 if (*non_constant_p
)
3773 /* Find the underlying variable. */
3775 tree object
= NULL_TREE
;
3776 for (tree probe
= target
; object
== NULL_TREE
; )
3778 switch (TREE_CODE (probe
))
3784 tree ob
= TREE_OPERAND (probe
, 0);
3785 tree elt
= TREE_OPERAND (probe
, 1);
3786 if (TREE_CODE (probe
) == ARRAY_REF
)
3788 elt
= eval_and_check_array_index (ctx
, probe
, false,
3789 non_constant_p
, overflow_p
);
3790 if (*non_constant_p
)
3793 vec_safe_push (refs
, elt
);
3794 vec_safe_push (refs
, TREE_TYPE (probe
));
3804 probe
= cxx_eval_constant_expression (ctx
, probe
, true,
3805 non_constant_p
, overflow_p
);
3807 if (*non_constant_p
)
3814 /* And then find/build up our initializer for the path to the subobject
3815 we're initializing. */
3817 if (object
== ctx
->object
&& VAR_P (object
)
3818 && DECL_NAME (object
) && ctx
->call
== NULL
)
3819 /* The variable we're building up an aggregate initializer for is outside
3820 the constant-expression, so don't evaluate the store. We check
3821 DECL_NAME to handle TARGET_EXPR temporaries, which are fair game. */
3823 else if (DECL_P (object
))
3824 valp
= ctx
->values
->get (object
);
3829 /* A constant-expression cannot modify objects from outside the
3830 constant-expression. */
3832 error ("modification of %qE is not a constant expression", object
);
3833 *non_constant_p
= true;
3836 type
= TREE_TYPE (object
);
3837 bool no_zero_init
= true;
3839 releasing_vec ctors
;
3840 while (!refs
->is_empty())
3842 if (*valp
== NULL_TREE
)
3844 *valp
= build_constructor (type
, NULL
);
3845 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
3847 else if (TREE_CODE (*valp
) == STRING_CST
)
3849 /* An array was initialized with a string constant, and now
3850 we're writing into one of its elements. Explode the
3851 single initialization into a set of element
3853 gcc_assert (TREE_CODE (type
) == ARRAY_TYPE
);
3855 tree string
= *valp
;
3856 tree elt_type
= TREE_TYPE (type
);
3857 unsigned chars_per_elt
= (TYPE_PRECISION (elt_type
)
3858 / TYPE_PRECISION (char_type_node
));
3859 unsigned num_elts
= TREE_STRING_LENGTH (string
) / chars_per_elt
;
3860 tree ary_ctor
= build_constructor (type
, NULL
);
3862 vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor
), num_elts
);
3863 for (unsigned ix
= 0; ix
!= num_elts
; ix
++)
3865 constructor_elt elt
=
3867 build_int_cst (size_type_node
, ix
),
3868 extract_string_elt (string
, chars_per_elt
, ix
)
3870 CONSTRUCTOR_ELTS (ary_ctor
)->quick_push (elt
);
3876 /* If the value of object is already zero-initialized, any new ctors for
3877 subobjects will also be zero-initialized. */
3878 no_zero_init
= CONSTRUCTOR_NO_CLEARING (*valp
);
3880 vec_safe_push (ctors
, *valp
);
3882 enum tree_code code
= TREE_CODE (type
);
3884 tree index
= refs
->pop();
3886 constructor_elt
*cep
= NULL
;
3887 if (code
== ARRAY_TYPE
)
3890 = find_array_ctor_elt (*valp
, index
, /*insert*/true);
3891 gcc_assert (i
>= 0);
3892 cep
= CONSTRUCTOR_ELT (*valp
, i
);
3893 gcc_assert (TREE_CODE (cep
->index
) != RANGE_EXPR
);
3897 gcc_assert (TREE_CODE (index
) == FIELD_DECL
);
3899 /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
3900 Usually we meet initializers in that order, but it is
3901 possible for base types to be placed not in program
3903 tree fields
= TYPE_FIELDS (DECL_CONTEXT (index
));
3904 unsigned HOST_WIDE_INT idx
;
3906 if (code
== UNION_TYPE
&& CONSTRUCTOR_NELTS (*valp
)
3907 && CONSTRUCTOR_ELT (*valp
, 0)->index
!= index
)
3909 if (cxx_dialect
< cxx2a
)
3912 error_at (cp_expr_loc_or_input_loc (t
),
3913 "change of the active member of a union "
3915 CONSTRUCTOR_ELT (*valp
, 0)->index
,
3917 *non_constant_p
= true;
3919 /* Changing active member. */
3920 vec_safe_truncate (CONSTRUCTOR_ELTS (*valp
), 0);
3921 no_zero_init
= true;
3925 vec_safe_iterate (CONSTRUCTOR_ELTS (*valp
), idx
, &cep
);
3926 idx
++, fields
= DECL_CHAIN (fields
))
3928 if (index
== cep
->index
)
3931 /* The field we're initializing must be on the field
3932 list. Look to see if it is present before the
3933 field the current ELT initializes. */
3934 for (; fields
!= cep
->index
; fields
= DECL_CHAIN (fields
))
3935 if (index
== fields
)
3939 /* We fell off the end of the CONSTRUCTOR, so insert a new
3940 entry at the end. */
3943 constructor_elt ce
= { index
, NULL_TREE
};
3945 vec_safe_insert (CONSTRUCTOR_ELTS (*valp
), idx
, ce
);
3946 cep
= CONSTRUCTOR_ELT (*valp
, idx
);
3955 /* Create a new CONSTRUCTOR in case evaluation of the initializer
3956 wants to modify it. */
3957 if (*valp
== NULL_TREE
)
3959 *valp
= build_constructor (type
, NULL
);
3960 CONSTRUCTOR_NO_CLEARING (*valp
) = no_zero_init
;
3962 new_ctx
.ctor
= *valp
;
3963 new_ctx
.object
= target
;
3964 init
= cxx_eval_constant_expression (&new_ctx
, init
, false,
3965 non_constant_p
, overflow_p
);
3966 if (ctors
->is_empty())
3967 /* The hash table might have moved since the get earlier. */
3968 valp
= ctx
->values
->get (object
);
3971 /* Don't share a CONSTRUCTOR that might be changed later. */
3972 init
= unshare_constructor (init
);
3974 if (*valp
&& TREE_CODE (*valp
) == CONSTRUCTOR
3975 && TREE_CODE (init
) == CONSTRUCTOR
)
3977 /* An outer ctx->ctor might be pointing to *valp, so replace
3979 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init
),
3982 /* For initialization of an empty base, the original target will be
3983 *(base*)this, evaluation of which resolves to the object
3984 argument, which has the derived type rather than the base type. In
3985 this situation, just evaluate the initializer and return, since
3986 there's no actual data to store. */
3987 gcc_assert (is_empty_class (TREE_TYPE (init
)) && !lval
);
3990 CONSTRUCTOR_ELTS (*valp
) = CONSTRUCTOR_ELTS (init
);
3991 TREE_CONSTANT (*valp
) = TREE_CONSTANT (init
);
3992 TREE_SIDE_EFFECTS (*valp
) = TREE_SIDE_EFFECTS (init
);
3993 CONSTRUCTOR_NO_CLEARING (*valp
)
3994 = CONSTRUCTOR_NO_CLEARING (init
);
3999 /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
4000 CONSTRUCTORs, if any. */
4003 bool c
= TREE_CONSTANT (init
);
4004 bool s
= TREE_SIDE_EFFECTS (init
);
4006 FOR_EACH_VEC_ELT (*ctors
, i
, elt
)
4009 TREE_CONSTANT (elt
) = false;
4011 TREE_SIDE_EFFECTS (elt
) = true;
4014 if (*non_constant_p
)
4022 /* Evaluate a ++ or -- expression. */
4025 cxx_eval_increment_expression (const constexpr_ctx
*ctx
, tree t
,
4027 bool *non_constant_p
, bool *overflow_p
)
4029 enum tree_code code
= TREE_CODE (t
);
4030 tree type
= TREE_TYPE (t
);
4031 tree op
= TREE_OPERAND (t
, 0);
4032 tree offset
= TREE_OPERAND (t
, 1);
4033 gcc_assert (TREE_CONSTANT (offset
));
4035 /* The operand as an lvalue. */
4036 op
= cxx_eval_constant_expression (ctx
, op
, true,
4037 non_constant_p
, overflow_p
);
4039 /* The operand as an rvalue. */
4041 = cxx_eval_constant_expression (ctx
, op
, false,
4042 non_constant_p
, overflow_p
);
4043 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
4044 a local array in a constexpr function. */
4045 bool ptr
= INDIRECT_TYPE_P (TREE_TYPE (val
));
4047 VERIFY_CONSTANT (val
);
4049 /* The modified value. */
4050 bool inc
= (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
);
4052 if (INDIRECT_TYPE_P (type
))
4054 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
4055 offset
= convert_to_ptrofftype (offset
);
4057 offset
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (offset
), offset
);
4058 mod
= fold_build2 (POINTER_PLUS_EXPR
, type
, val
, offset
);
4061 mod
= fold_build2 (inc
? PLUS_EXPR
: MINUS_EXPR
, type
, val
, offset
);
4063 VERIFY_CONSTANT (mod
);
4065 /* Storing the modified value. */
4066 tree store
= build2 (MODIFY_EXPR
, type
, op
, mod
);
4067 cxx_eval_constant_expression (ctx
, store
,
4068 true, non_constant_p
, overflow_p
);
4071 /* And the value of the expression. */
4072 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
4074 /* Prefix ops are lvalues. */
4078 /* But we optimize when the caller wants an rvalue. */
4082 /* Postfix ops are rvalues. */
4086 /* Predicates for the meaning of *jump_target. */
4089 returns (tree
*jump_target
)
4092 && (TREE_CODE (*jump_target
) == RETURN_EXPR
4093 || (TREE_CODE (*jump_target
) == LABEL_DECL
4094 && LABEL_DECL_CDTOR (*jump_target
)));
4098 breaks (tree
*jump_target
)
4101 && ((TREE_CODE (*jump_target
) == LABEL_DECL
4102 && LABEL_DECL_BREAK (*jump_target
))
4103 || TREE_CODE (*jump_target
) == BREAK_STMT
4104 || TREE_CODE (*jump_target
) == EXIT_EXPR
);
4108 continues (tree
*jump_target
)
4111 && ((TREE_CODE (*jump_target
) == LABEL_DECL
4112 && LABEL_DECL_CONTINUE (*jump_target
))
4113 || TREE_CODE (*jump_target
) == CONTINUE_STMT
);
4118 switches (tree
*jump_target
)
4121 && TREE_CODE (*jump_target
) == INTEGER_CST
;
4124 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
4125 STMT matches *jump_target. If we're looking for a case label and we see
4126 the default label, note it in ctx->css_state. */
4129 label_matches (const constexpr_ctx
*ctx
, tree
*jump_target
, tree stmt
)
4131 switch (TREE_CODE (*jump_target
))
4134 if (TREE_CODE (stmt
) == LABEL_EXPR
4135 && LABEL_EXPR_LABEL (stmt
) == *jump_target
)
4140 if (TREE_CODE (stmt
) == CASE_LABEL_EXPR
)
4142 gcc_assert (ctx
->css_state
!= NULL
);
4143 if (!CASE_LOW (stmt
))
4145 /* default: should appear just once in a SWITCH_EXPR
4146 body (excluding nested SWITCH_EXPR). */
4147 gcc_assert (*ctx
->css_state
!= css_default_seen
);
4148 /* When evaluating SWITCH_EXPR body for the second time,
4149 return true for the default: label. */
4150 if (*ctx
->css_state
== css_default_processing
)
4152 *ctx
->css_state
= css_default_seen
;
4154 else if (CASE_HIGH (stmt
))
4156 if (tree_int_cst_le (CASE_LOW (stmt
), *jump_target
)
4157 && tree_int_cst_le (*jump_target
, CASE_HIGH (stmt
)))
4160 else if (tree_int_cst_equal (*jump_target
, CASE_LOW (stmt
)))
4167 /* These two are handled directly in cxx_eval_loop_expr by testing
4168 breaks (jump_target) or continues (jump_target). */
4177 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
4178 semantics, for switch, break, continue, and return. */
4181 cxx_eval_statement_list (const constexpr_ctx
*ctx
, tree t
,
4182 bool *non_constant_p
, bool *overflow_p
,
4185 tree_stmt_iterator i
;
4187 /* In a statement-expression we want to return the last value.
4188 For empty statement expression return void_node. */
4192 local_target
= NULL_TREE
;
4193 jump_target
= &local_target
;
4195 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
4197 tree stmt
= tsi_stmt (i
);
4198 /* We've found a continue, so skip everything until we reach
4199 the label its jumping to. */
4200 if (continues (jump_target
))
4202 if (label_matches (ctx
, jump_target
, stmt
))
4204 *jump_target
= NULL_TREE
;
4208 if (TREE_CODE (stmt
) == DEBUG_BEGIN_STMT
)
4210 r
= cxx_eval_constant_expression (ctx
, stmt
, false,
4211 non_constant_p
, overflow_p
,
4213 if (*non_constant_p
)
4215 if (returns (jump_target
) || breaks (jump_target
))
4218 if (*jump_target
&& jump_target
== &local_target
)
4220 /* We aren't communicating the jump to our caller, so give up. We don't
4221 need to support evaluation of jumps out of statement-exprs. */
4223 error_at (cp_expr_loc_or_input_loc (r
),
4224 "statement is not a constant expression");
4225 *non_constant_p
= true;
4230 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
4231 semantics; continue semantics are covered by cxx_eval_statement_list. */
4234 cxx_eval_loop_expr (const constexpr_ctx
*ctx
, tree t
,
4235 bool *non_constant_p
, bool *overflow_p
,
4238 constexpr_ctx new_ctx
= *ctx
;
4240 tree body
, cond
= NULL_TREE
, expr
= NULL_TREE
;
4242 switch (TREE_CODE (t
))
4245 body
= LOOP_EXPR_BODY (t
);
4252 body
= WHILE_BODY (t
);
4253 cond
= WHILE_COND (t
);
4257 if (FOR_INIT_STMT (t
))
4258 cxx_eval_constant_expression (ctx
, FOR_INIT_STMT (t
), /*lval*/false,
4259 non_constant_p
, overflow_p
, jump_target
);
4260 if (*non_constant_p
)
4262 body
= FOR_BODY (t
);
4263 cond
= FOR_COND (t
);
4264 expr
= FOR_EXPR (t
);
4270 auto_vec
<tree
, 10> save_exprs
;
4271 new_ctx
.save_exprs
= &save_exprs
;
4277 cxx_eval_constant_expression (&new_ctx
, body
, /*lval*/false,
4278 non_constant_p
, overflow_p
,
4280 if (breaks (jump_target
))
4282 *jump_target
= NULL_TREE
;
4286 if (TREE_CODE (t
) != LOOP_EXPR
&& continues (jump_target
))
4287 *jump_target
= NULL_TREE
;
4290 cxx_eval_constant_expression (&new_ctx
, expr
, /*lval*/false,
4291 non_constant_p
, overflow_p
,
4298 = cxx_eval_constant_expression (&new_ctx
, cond
, /*lval*/false,
4299 non_constant_p
, overflow_p
,
4303 if (verify_constant (res
, ctx
->quiet
, non_constant_p
,
4306 if (integer_zerop (res
))
4310 gcc_assert (*jump_target
);
4313 /* Forget saved values of SAVE_EXPRs. */
4316 FOR_EACH_VEC_ELT (save_exprs
, i
, save_expr
)
4317 new_ctx
.values
->remove (save_expr
);
4318 save_exprs
.truncate (0);
4320 if (++count
>= constexpr_loop_limit
)
4323 error_at (cp_expr_loc_or_input_loc (t
),
4324 "%<constexpr%> loop iteration count exceeds limit of %d "
4325 "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
4326 constexpr_loop_limit
);
4327 *non_constant_p
= true;
4331 while (!returns (jump_target
)
4332 && !breaks (jump_target
)
4333 && !continues (jump_target
)
4334 && (!switches (jump_target
) || count
== 0)
4335 && !*non_constant_p
);
4337 /* Forget saved values of SAVE_EXPRs. */
4340 FOR_EACH_VEC_ELT (save_exprs
, i
, save_expr
)
4341 new_ctx
.values
->remove (save_expr
);
4346 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
4350 cxx_eval_switch_expr (const constexpr_ctx
*ctx
, tree t
,
4351 bool *non_constant_p
, bool *overflow_p
,
4355 = TREE_CODE (t
) == SWITCH_STMT
? SWITCH_STMT_COND (t
) : SWITCH_COND (t
);
4356 cond
= cxx_eval_constant_expression (ctx
, cond
, false,
4357 non_constant_p
, overflow_p
);
4358 VERIFY_CONSTANT (cond
);
4359 *jump_target
= cond
;
4362 = TREE_CODE (t
) == SWITCH_STMT
? SWITCH_STMT_BODY (t
) : SWITCH_BODY (t
);
4363 constexpr_ctx new_ctx
= *ctx
;
4364 constexpr_switch_state css
= css_default_not_seen
;
4365 new_ctx
.css_state
= &css
;
4366 cxx_eval_constant_expression (&new_ctx
, body
, false,
4367 non_constant_p
, overflow_p
, jump_target
);
4368 if (switches (jump_target
) && css
== css_default_seen
)
4370 /* If the SWITCH_EXPR body has default: label, process it once again,
4371 this time instructing label_matches to return true for default:
4372 label on switches (jump_target). */
4373 css
= css_default_processing
;
4374 cxx_eval_constant_expression (&new_ctx
, body
, false,
4375 non_constant_p
, overflow_p
, jump_target
);
4377 if (breaks (jump_target
) || switches (jump_target
))
4378 *jump_target
= NULL_TREE
;
4382 /* Find the object of TYPE under initialization in CTX. */
4385 lookup_placeholder (const constexpr_ctx
*ctx
, bool lval
, tree type
)
4390 /* We could use ctx->object unconditionally, but using ctx->ctor when we
4391 can is a minor optimization. */
4392 if (!lval
&& ctx
->ctor
&& same_type_p (TREE_TYPE (ctx
->ctor
), type
))
4398 /* Since an object cannot have a field of its own type, we can search outward
4399 from ctx->object to find the unique containing object of TYPE. */
4400 tree ob
= ctx
->object
;
4403 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob
), type
))
4405 if (handled_component_p (ob
))
4406 ob
= TREE_OPERAND (ob
, 0);
4414 /* Attempt to reduce the expression T to a constant value.
4415 On failure, issue diagnostic and return error_mark_node. */
4416 /* FIXME unify with c_fully_fold */
4417 /* FIXME overflow_p is too global */
4420 cxx_eval_constant_expression (const constexpr_ctx
*ctx
, tree t
,
4422 bool *non_constant_p
, bool *overflow_p
,
4423 tree
*jump_target
/* = NULL */)
4425 if (jump_target
&& *jump_target
)
4427 /* If we are jumping, ignore all statements/expressions except those
4428 that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
4429 switch (TREE_CODE (t
))
4432 case STATEMENT_LIST
:
4441 case CASE_LABEL_EXPR
:
4442 if (label_matches (ctx
, jump_target
, t
))
4444 *jump_target
= NULL_TREE
;
4450 if (error_operand_p (t
))
4452 *non_constant_p
= true;
4456 STRIP_ANY_LOCATION_WRAPPER (t
);
4458 if (CONSTANT_CLASS_P (t
))
4460 if (TREE_OVERFLOW (t
))
4463 permerror (input_location
, "overflow in constant expression");
4464 if (!flag_permissive
|| ctx
->quiet
)
4468 if (TREE_CODE (t
) == INTEGER_CST
4469 && TYPE_PTR_P (TREE_TYPE (t
))
4470 && !integer_zerop (t
))
4473 error ("value %qE of type %qT is not a constant expression",
4475 *non_constant_p
= true;
4481 /* Avoid excessively long constexpr evaluations. */
4482 if (++*ctx
->constexpr_ops_count
>= constexpr_ops_limit
)
4485 error_at (cp_expr_loc_or_input_loc (t
),
4486 "%<constexpr%> evaluation operation count exceeds limit of "
4487 "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
4488 constexpr_ops_limit
);
4489 *ctx
->constexpr_ops_count
= INTTYPE_MINIMUM (HOST_WIDE_INT
);
4490 *non_constant_p
= true;
4494 constexpr_ctx new_ctx
;
4497 tree_code tcode
= TREE_CODE (t
);
4503 /* We ask for an rvalue for the RESULT_DECL when indirecting
4504 through an invisible reference, or in named return value
4506 if (tree
*p
= ctx
->values
->get (t
))
4511 error ("%qE is not a constant expression", t
);
4512 *non_constant_p
= true;
4517 if (DECL_HAS_VALUE_EXPR_P (t
))
4519 if (is_normal_capture_proxy (t
)
4520 && current_function_decl
== DECL_CONTEXT (t
))
4522 /* Function parms aren't constexpr within the function
4523 definition, so don't try to look at the closure. But if the
4524 captured variable is constant, try to evaluate it directly. */
4525 r
= DECL_CAPTURED_VARIABLE (t
);
4526 tree type
= TREE_TYPE (t
);
4527 if (TYPE_REF_P (type
) != TYPE_REF_P (TREE_TYPE (r
)))
4529 /* Adjust r to match the reference-ness of t. */
4530 if (TYPE_REF_P (type
))
4531 r
= build_address (r
);
4533 r
= convert_from_reference (r
);
4537 r
= DECL_VALUE_EXPR (t
);
4538 return cxx_eval_constant_expression (ctx
, r
, lval
, non_constant_p
,
4543 /* We used to not check lval for CONST_DECL, but darwin.c uses
4544 CONST_DECL for aggregate constants. */
4547 if (COMPLETE_TYPE_P (TREE_TYPE (t
))
4548 && is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
4550 /* If the class is empty, we aren't actually loading anything. */
4551 r
= build_constructor (TREE_TYPE (t
), NULL
);
4552 TREE_CONSTANT (r
) = true;
4554 else if (ctx
->strict
)
4555 r
= decl_really_constant_value (t
);
4557 r
= decl_constant_value (t
);
4558 if (TREE_CODE (r
) == TARGET_EXPR
4559 && TREE_CODE (TARGET_EXPR_INITIAL (r
)) == CONSTRUCTOR
)
4560 r
= TARGET_EXPR_INITIAL (r
);
4562 if (tree
*p
= ctx
->values
->get (r
))
4563 if (*p
!= NULL_TREE
)
4568 non_const_var_error (r
);
4569 *non_constant_p
= true;
4573 case DEBUG_BEGIN_STMT
:
4574 /* ??? It might be nice to retain this information somehow, so
4575 as to be able to step into a constexpr function call. */
4582 case CASE_LABEL_EXPR
:
4587 if (lval
&& !TYPE_REF_P (TREE_TYPE (t
)))
4589 else if (tree
*p
= ctx
->values
->get (r
))
4592 /* Defer in case this is only used for its type. */;
4593 else if (TYPE_REF_P (TREE_TYPE (t
)))
4594 /* Defer, there's no lvalue->rvalue conversion. */;
4595 else if (COMPLETE_TYPE_P (TREE_TYPE (t
))
4596 && is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
4598 /* If the class is empty, we aren't actually loading anything. */
4599 r
= build_constructor (TREE_TYPE (t
), NULL
);
4600 TREE_CONSTANT (r
) = true;
4605 error ("%qE is not a constant expression", t
);
4606 *non_constant_p
= true;
4611 case AGGR_INIT_EXPR
:
4612 r
= cxx_eval_call_expression (ctx
, t
, lval
,
4613 non_constant_p
, overflow_p
);
4618 r
= DECL_EXPR_DECL (t
);
4619 if (TREE_CODE (r
) == USING_DECL
)
4624 if (AGGREGATE_TYPE_P (TREE_TYPE (r
))
4625 || VECTOR_TYPE_P (TREE_TYPE (r
)))
4629 new_ctx
.ctor
= build_constructor (TREE_TYPE (r
), NULL
);
4630 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = true;
4631 new_ctx
.values
->put (r
, new_ctx
.ctor
);
4635 if (tree init
= DECL_INITIAL (r
))
4637 init
= cxx_eval_constant_expression (ctx
, init
,
4639 non_constant_p
, overflow_p
);
4640 /* Don't share a CONSTRUCTOR that might be changed. */
4641 init
= unshare_constructor (init
);
4642 ctx
->values
->put (r
, init
);
4644 else if (ctx
== &new_ctx
)
4645 /* We gave it a CONSTRUCTOR above. */;
4647 ctx
->values
->put (r
, NULL_TREE
);
4652 if (!literal_type_p (TREE_TYPE (t
)))
4656 auto_diagnostic_group d
;
4657 error ("temporary of non-literal type %qT in a "
4658 "constant expression", TREE_TYPE (t
));
4659 explain_non_literal_class (TREE_TYPE (t
));
4661 *non_constant_p
= true;
4664 if ((AGGREGATE_TYPE_P (TREE_TYPE (t
)) || VECTOR_TYPE_P (TREE_TYPE (t
))))
4666 /* We're being expanded without an explicit target, so start
4667 initializing a new object; expansion with an explicit target
4668 strips the TARGET_EXPR before we get here. */
4670 new_ctx
.ctor
= build_constructor (TREE_TYPE (t
), NULL
);
4671 CONSTRUCTOR_NO_CLEARING (new_ctx
.ctor
) = true;
4672 new_ctx
.object
= TARGET_EXPR_SLOT (t
);
4673 ctx
->values
->put (new_ctx
.object
, new_ctx
.ctor
);
4676 /* Pass false for 'lval' because this indicates
4677 initialization of a temporary. */
4678 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
4680 non_constant_p
, overflow_p
);
4681 if (!*non_constant_p
)
4682 /* Adjust the type of the result to the type of the temporary. */
4683 r
= adjust_temp_type (TREE_TYPE (t
), r
);
4686 tree slot
= TARGET_EXPR_SLOT (t
);
4687 r
= unshare_constructor (r
);
4688 ctx
->values
->put (slot
, r
);
4695 gcc_assert (jump_target
== NULL
|| *jump_target
== NULL_TREE
);
4696 r
= cxx_eval_store_expression (ctx
, t
, lval
,
4697 non_constant_p
, overflow_p
);
4701 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
4703 non_constant_p
, overflow_p
);
4707 if (TREE_OPERAND (t
, 0) != NULL_TREE
)
4708 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
4710 non_constant_p
, overflow_p
);
4718 /* Can happen with ({ return true; }) && false; passed to
4719 maybe_constant_value. There is nothing to jump over in this
4720 case, and the bug will be diagnosed later. */
4721 gcc_assert (ctx
->quiet
);
4722 *non_constant_p
= true;
4727 /* Avoid evaluating a SAVE_EXPR more than once. */
4728 if (tree
*p
= ctx
->values
->get (t
))
4732 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), false,
4733 non_constant_p
, overflow_p
);
4734 ctx
->values
->put (t
, r
);
4735 if (ctx
->save_exprs
)
4736 ctx
->save_exprs
->safe_push (t
);
4740 case NON_LVALUE_EXPR
:
4741 case TRY_CATCH_EXPR
:
4743 case CLEANUP_POINT_EXPR
:
4744 case MUST_NOT_THROW_EXPR
:
4747 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
4749 non_constant_p
, overflow_p
,
4753 case TRY_FINALLY_EXPR
:
4754 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
4755 non_constant_p
, overflow_p
,
4757 if (!*non_constant_p
)
4758 /* Also evaluate the cleanup. */
4759 cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1), true,
4760 non_constant_p
, overflow_p
,
4765 r
= cxx_eval_constant_expression (ctx
, CLEANUP_BODY (t
), lval
,
4766 non_constant_p
, overflow_p
,
4768 if (!CLEANUP_EH_ONLY (t
) && !*non_constant_p
)
4769 /* Also evaluate the cleanup. */
4770 cxx_eval_constant_expression (ctx
, CLEANUP_EXPR (t
), true,
4771 non_constant_p
, overflow_p
,
4775 /* These differ from cxx_eval_unary_expression in that this doesn't
4776 check for a constant operand or result; an address can be
4777 constant without its operand being, and vice versa. */
4780 r
= cxx_eval_indirect_ref (ctx
, t
, lval
,
4781 non_constant_p
, overflow_p
);
4786 tree oldop
= TREE_OPERAND (t
, 0);
4787 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
4789 non_constant_p
, overflow_p
);
4790 /* Don't VERIFY_CONSTANT here. */
4791 if (*non_constant_p
)
4793 gcc_checking_assert (TREE_CODE (op
) != CONSTRUCTOR
);
4794 /* This function does more aggressive folding than fold itself. */
4795 r
= build_fold_addr_expr_with_type (op
, TREE_TYPE (t
));
4796 if (TREE_CODE (r
) == ADDR_EXPR
&& TREE_OPERAND (r
, 0) == oldop
)
4808 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
4809 non_constant_p
, overflow_p
);
4810 if (r
== error_mark_node
)
4812 else if (r
== TREE_OPERAND (t
, 0))
4815 r
= fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), r
);
4820 case FIX_TRUNC_EXPR
:
4826 case TRUTH_NOT_EXPR
:
4827 case FIXED_CONVERT_EXPR
:
4828 r
= cxx_eval_unary_expression (ctx
, t
, lval
,
4829 non_constant_p
, overflow_p
);
4833 r
= fold_sizeof_expr (t
);
4834 /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
4835 which could lead to an infinite recursion. */
4836 if (TREE_CODE (r
) != SIZEOF_EXPR
)
4837 r
= cxx_eval_constant_expression (ctx
, r
, lval
,
4838 non_constant_p
, overflow_p
,
4842 *non_constant_p
= true;
4843 gcc_assert (ctx
->quiet
);
4850 /* check_return_expr sometimes wraps a TARGET_EXPR in a
4851 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
4852 introduced by build_call_a. */
4853 tree op0
= TREE_OPERAND (t
, 0);
4854 tree op1
= TREE_OPERAND (t
, 1);
4856 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
4857 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
4858 r
= cxx_eval_constant_expression (ctx
, op0
,
4859 lval
, non_constant_p
, overflow_p
,
4863 /* Check that the LHS is constant and then discard it. */
4864 cxx_eval_constant_expression (ctx
, op0
,
4865 true, non_constant_p
, overflow_p
,
4867 if (*non_constant_p
)
4869 op1
= TREE_OPERAND (t
, 1);
4870 r
= cxx_eval_constant_expression (ctx
, op1
,
4871 lval
, non_constant_p
, overflow_p
,
4877 case POINTER_PLUS_EXPR
:
4878 case POINTER_DIFF_EXPR
:
4882 case TRUNC_DIV_EXPR
:
4884 case FLOOR_DIV_EXPR
:
4885 case ROUND_DIV_EXPR
:
4886 case TRUNC_MOD_EXPR
:
4888 case ROUND_MOD_EXPR
:
4890 case EXACT_DIV_EXPR
:
4900 case TRUTH_XOR_EXPR
:
4907 case UNORDERED_EXPR
:
4917 r
= cxx_eval_binary_expression (ctx
, t
, lval
,
4918 non_constant_p
, overflow_p
);
4921 /* fold can introduce non-IF versions of these; still treat them as
4922 short-circuiting. */
4923 case TRUTH_AND_EXPR
:
4924 case TRUTH_ANDIF_EXPR
:
4925 r
= cxx_eval_logical_expression (ctx
, t
, boolean_false_node
,
4928 non_constant_p
, overflow_p
);
4932 case TRUTH_ORIF_EXPR
:
4933 r
= cxx_eval_logical_expression (ctx
, t
, boolean_true_node
,
4936 non_constant_p
, overflow_p
);
4940 r
= cxx_eval_array_reference (ctx
, t
, lval
,
4941 non_constant_p
, overflow_p
);
4945 if (is_overloaded_fn (t
))
4947 /* We can only get here in checking mode via
4948 build_non_dependent_expr, because any expression that
4949 calls or takes the address of the function will have
4950 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
4951 gcc_checking_assert (ctx
->quiet
|| errorcount
);
4952 *non_constant_p
= true;
4955 r
= cxx_eval_component_reference (ctx
, t
, lval
,
4956 non_constant_p
, overflow_p
);
4960 r
= cxx_eval_bit_field_ref (ctx
, t
, lval
,
4961 non_constant_p
, overflow_p
);
4966 if (jump_target
&& *jump_target
)
4968 tree orig_jump
= *jump_target
;
4969 tree arg
= ((TREE_CODE (t
) != IF_STMT
|| TREE_OPERAND (t
, 1))
4970 ? TREE_OPERAND (t
, 1) : void_node
);
4971 /* When jumping to a label, the label might be either in the
4972 then or else blocks, so process then block first in skipping
4973 mode first, and if we are still in the skipping mode at its end,
4974 process the else block too. */
4975 r
= cxx_eval_constant_expression (ctx
, arg
, lval
, non_constant_p
,
4976 overflow_p
, jump_target
);
4977 /* It's possible that we found the label in the then block. But
4978 it could have been followed by another jumping statement, e.g.
4979 say we're looking for case 1:
4982 // skipped statements
4983 case 1:; // clears up *jump_target
4984 return 1; // and sets it to a RETURN_EXPR
4987 in which case we need not go looking to the else block.
4988 (goto is not allowed in a constexpr function.) */
4989 if (*jump_target
== orig_jump
)
4991 arg
= ((TREE_CODE (t
) != IF_STMT
|| TREE_OPERAND (t
, 2))
4992 ? TREE_OPERAND (t
, 2) : void_node
);
4993 r
= cxx_eval_constant_expression (ctx
, arg
, lval
, non_constant_p
,
4994 overflow_p
, jump_target
);
4998 r
= cxx_eval_conditional_expression (ctx
, t
, lval
,
4999 non_constant_p
, overflow_p
,
5003 r
= cxx_eval_vector_conditional_expression (ctx
, t
, non_constant_p
,
5008 if (TREE_CONSTANT (t
) && reduced_constant_expression_p (t
))
5010 /* Don't re-process a constant CONSTRUCTOR, but do fold it to
5011 VECTOR_CST if applicable. */
5012 verify_constructor_flags (t
);
5013 if (TREE_CONSTANT (t
))
5016 r
= cxx_eval_bare_aggregate (ctx
, t
, lval
,
5017 non_constant_p
, overflow_p
);
5021 /* We can get this in a defaulted constructor for a class with a
5022 non-static data member of array type. Either the initializer will
5023 be NULL, meaning default-initialization, or it will be an lvalue
5024 or xvalue of the same type, meaning direct-initialization from the
5025 corresponding member. */
5026 r
= cxx_eval_vec_init (ctx
, t
, lval
,
5027 non_constant_p
, overflow_p
);
5031 r
= cxx_eval_trinary_expression (ctx
, t
, lval
,
5032 non_constant_p
, overflow_p
);
5036 if (REINTERPRET_CAST_P (t
))
5039 error_at (cp_expr_loc_or_input_loc (t
),
5040 "%<reinterpret_cast%> is not a constant expression");
5041 *non_constant_p
= true;
5046 case VIEW_CONVERT_EXPR
:
5047 case UNARY_PLUS_EXPR
:
5049 tree oldop
= TREE_OPERAND (t
, 0);
5051 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
5053 non_constant_p
, overflow_p
);
5054 if (*non_constant_p
)
5056 tree type
= TREE_TYPE (t
);
5058 if (VOID_TYPE_P (type
))
5061 if (TREE_CODE (op
) == PTRMEM_CST
5062 && !TYPE_PTRMEM_P (type
))
5063 op
= cplus_expand_constant (op
);
5065 if (TREE_CODE (op
) == PTRMEM_CST
&& tcode
== NOP_EXPR
)
5067 if (!same_type_ignoring_top_level_qualifiers_p (type
, TREE_TYPE (op
))
5068 && !can_convert_qual (type
, op
))
5069 op
= cplus_expand_constant (op
);
5070 return cp_fold_convert (type
, op
);
5073 if (INDIRECT_TYPE_P (type
) && TREE_CODE (op
) == INTEGER_CST
)
5075 if (integer_zerop (op
))
5077 if (TYPE_REF_P (type
))
5080 error_at (cp_expr_loc_or_input_loc (t
),
5081 "dereferencing a null pointer");
5082 *non_constant_p
= true;
5085 else if (TYPE_PTR_P (TREE_TYPE (op
)))
5087 tree from
= TREE_TYPE (op
);
5089 if (!can_convert (type
, from
, tf_none
))
5092 error_at (cp_expr_loc_or_input_loc (t
),
5093 "conversion of %qT null pointer to %qT "
5094 "is not a constant expression",
5096 *non_constant_p
= true;
5103 /* This detects for example:
5104 reinterpret_cast<void*>(sizeof 0)
5107 error_at (cp_expr_loc_or_input_loc (t
),
5108 "%<reinterpret_cast<%T>(%E)%> is not "
5109 "a constant expression",
5111 *non_constant_p
= true;
5116 if (op
== oldop
&& tcode
!= UNARY_PLUS_EXPR
)
5117 /* We didn't fold at the top so we could check for ptr-int
5123 /* Handle an array's bounds having been deduced after we built
5124 the wrapping expression. */
5125 if (same_type_ignoring_tlq_and_bounds_p (type
, TREE_TYPE (op
)))
5127 else if (sop
= tree_strip_nop_conversions (op
),
5128 sop
!= op
&& (same_type_ignoring_tlq_and_bounds_p
5129 (type
, TREE_TYPE (sop
))))
5131 else if (tcode
== UNARY_PLUS_EXPR
)
5132 r
= fold_convert (TREE_TYPE (t
), op
);
5134 r
= fold_build1 (tcode
, type
, op
);
5136 /* Conversion of an out-of-range value has implementation-defined
5137 behavior; the language considers it different from arithmetic
5138 overflow, which is undefined. */
5139 if (TREE_OVERFLOW_P (r
) && !TREE_OVERFLOW_P (op
))
5140 TREE_OVERFLOW (r
) = false;
5144 case EMPTY_CLASS_EXPR
:
5145 /* This is good enough for a function argument that might not get
5146 used, and they can't do anything with it, so just return it. */
5149 case STATEMENT_LIST
:
5151 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
5152 return cxx_eval_statement_list (&new_ctx
, t
,
5153 non_constant_p
, overflow_p
, jump_target
);
5156 return cxx_eval_constant_expression (ctx
, BIND_EXPR_BODY (t
),
5158 non_constant_p
, overflow_p
,
5161 case PREINCREMENT_EXPR
:
5162 case POSTINCREMENT_EXPR
:
5163 case PREDECREMENT_EXPR
:
5164 case POSTDECREMENT_EXPR
:
5165 return cxx_eval_increment_expression (ctx
, t
,
5166 lval
, non_constant_p
, overflow_p
);
5172 case VEC_DELETE_EXPR
:
5175 /* GCC internal stuff. */
5177 case NON_DEPENDENT_EXPR
:
5181 error_at (cp_expr_loc_or_input_loc (t
),
5182 "expression %qE is not a constant expression", t
);
5183 *non_constant_p
= true;
5188 /* Virtual function call. Let the constexpr machinery figure out
5189 the dynamic type. */
5190 int token
= tree_to_shwi (OBJ_TYPE_REF_TOKEN (t
));
5191 tree obj
= OBJ_TYPE_REF_OBJECT (t
);
5192 obj
= cxx_eval_constant_expression (ctx
, obj
, lval
, non_constant_p
,
5194 /* We expect something in the form of &x.D.2103.D.2094; get x. */
5195 if (TREE_CODE (obj
) != ADDR_EXPR
5196 || !DECL_P (get_base_address (TREE_OPERAND (obj
, 0))))
5199 error_at (cp_expr_loc_or_input_loc (t
),
5200 "expression %qE is not a constant expression", t
);
5201 *non_constant_p
= true;
5204 obj
= TREE_OPERAND (obj
, 0);
5205 while (TREE_CODE (obj
) == COMPONENT_REF
5206 && DECL_FIELD_IS_BASE (TREE_OPERAND (obj
, 1)))
5207 obj
= TREE_OPERAND (obj
, 0);
5208 tree objtype
= TREE_TYPE (obj
);
5209 /* Find the function decl in the virtual functions list. TOKEN is
5210 the DECL_VINDEX that says which function we're looking for. */
5211 tree virtuals
= BINFO_VIRTUALS (TYPE_BINFO (objtype
));
5212 if (TARGET_VTABLE_USES_DESCRIPTORS
)
5213 token
/= MAX (TARGET_VTABLE_USES_DESCRIPTORS
, 1);
5214 r
= TREE_VALUE (chain_index (token
, virtuals
));
5218 case PLACEHOLDER_EXPR
:
5219 /* Use of the value or address of the current object. */
5220 if (tree ctor
= lookup_placeholder (ctx
, lval
, TREE_TYPE (t
)))
5221 return cxx_eval_constant_expression (ctx
, ctor
, lval
,
5222 non_constant_p
, overflow_p
);
5223 /* A placeholder without a referent. We can get here when
5224 checking whether NSDMIs are noexcept, or in massage_init_elt;
5225 just say it's non-constant for now. */
5226 gcc_assert (ctx
->quiet
);
5227 *non_constant_p
= true;
5232 tree cond
= TREE_OPERAND (t
, 0);
5233 cond
= cxx_eval_constant_expression (ctx
, cond
, /*lval*/false,
5234 non_constant_p
, overflow_p
);
5235 VERIFY_CONSTANT (cond
);
5236 if (integer_nonzerop (cond
))
5242 *jump_target
= TREE_OPERAND (t
, 0);
5243 gcc_assert (breaks (jump_target
) || continues (jump_target
)
5244 /* Allow for jumping to a cdtor_label. */
5245 || returns (jump_target
));
5252 cxx_eval_loop_expr (ctx
, t
,
5253 non_constant_p
, overflow_p
, jump_target
);
5258 cxx_eval_switch_expr (ctx
, t
,
5259 non_constant_p
, overflow_p
, jump_target
);
5263 /* It's possible to get a requires-expression in a constant
5264 expression. For example:
5266 template<typename T> concept bool C() {
5267 return requires (T t) { t; };
5270 template<typename T> requires !C<T>() void f(T);
5272 Normalization leaves f with the associated constraint
5273 '!requires (T t) { ... }' which is not transformed into
5275 if (!processing_template_decl
)
5276 return evaluate_constraint_expression (t
, NULL_TREE
);
5278 *non_constant_p
= true;
5282 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
5284 non_constant_p
, overflow_p
,
5293 if (STATEMENT_CODE_P (TREE_CODE (t
)))
5295 /* This function doesn't know how to deal with pre-genericize
5296 statements; this can only happen with statement-expressions,
5297 so for now just fail. */
5299 error_at (EXPR_LOCATION (t
),
5300 "statement is not a constant expression");
5303 internal_error ("unexpected expression %qE of kind %s", t
,
5304 get_tree_code_name (TREE_CODE (t
)));
5305 *non_constant_p
= true;
5309 if (r
== error_mark_node
)
5310 *non_constant_p
= true;
5312 if (*non_constant_p
)
5318 /* P0859: A function is needed for constant evaluation if it is a constexpr
5319 function that is named by an expression ([basic.def.odr]) that is
5320 potentially constant evaluated.
5322 So we need to instantiate any constexpr functions mentioned by the
5323 expression even if the definition isn't needed for evaluating the
5327 instantiate_cx_fn_r (tree
*tp
, int *walk_subtrees
, void */
*data*/
)
5329 if (TREE_CODE (*tp
) == FUNCTION_DECL
5330 && DECL_DECLARED_CONSTEXPR_P (*tp
)
5331 && !DECL_INITIAL (*tp
)
5332 && !trivial_fn_p (*tp
)
5333 && DECL_TEMPLOID_INSTANTIATION (*tp
))
5336 instantiate_decl (*tp
, /*defer_ok*/false, /*expl_inst*/false);
5339 else if (TREE_CODE (*tp
) == CALL_EXPR
5340 || TREE_CODE (*tp
) == AGGR_INIT_EXPR
)
5342 if (EXPR_HAS_LOCATION (*tp
))
5343 input_location
= EXPR_LOCATION (*tp
);
5352 instantiate_constexpr_fns (tree t
)
5354 location_t loc
= input_location
;
5355 cp_walk_tree_without_duplicates (&t
, instantiate_cx_fn_r
, NULL
);
5356 input_location
= loc
;
5359 /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
5360 STRICT has the same sense as for constant_value_1: true if we only allow
5361 conforming C++ constant expressions, or false if we want a constant value
5362 even if it doesn't conform.
5363 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
5364 per P0595 even when ALLOW_NON_CONSTANT is true. */
5367 cxx_eval_outermost_constant_expr (tree t
, bool allow_non_constant
,
5369 bool manifestly_const_eval
= false,
5370 tree object
= NULL_TREE
)
5372 auto_timevar
time (TV_CONSTEXPR
);
5374 bool non_constant_p
= false;
5375 bool overflow_p
= false;
5376 hash_map
<tree
,tree
> map
;
5377 HOST_WIDE_INT constexpr_ctx_count
= 0;
5379 constexpr_ctx ctx
= { NULL
, &map
, NULL
, NULL
, NULL
, NULL
,
5380 &constexpr_ctx_count
, allow_non_constant
, strict
,
5381 manifestly_const_eval
|| !allow_non_constant
};
5383 tree type
= initialized_type (t
);
5385 if (VOID_TYPE_P (type
))
5387 if (AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
))
5389 /* In C++14 an NSDMI can participate in aggregate initialization,
5390 and can refer to the address of the object being initialized, so
5391 we need to pass in the relevant VAR_DECL if we want to do the
5392 evaluation in a single pass. The evaluation will dynamically
5393 update ctx.values for the VAR_DECL. We use the same strategy
5394 for C++11 constexpr constructors that refer to the object being
5396 ctx
.ctor
= build_constructor (type
, NULL
);
5397 CONSTRUCTOR_NO_CLEARING (ctx
.ctor
) = true;
5400 if (TREE_CODE (t
) == TARGET_EXPR
)
5401 object
= TARGET_EXPR_SLOT (t
);
5402 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
5403 object
= AGGR_INIT_EXPR_SLOT (t
);
5405 ctx
.object
= object
;
5407 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5408 (type
, TREE_TYPE (object
)));
5409 if (object
&& DECL_P (object
))
5410 map
.put (object
, ctx
.ctor
);
5411 if (TREE_CODE (r
) == TARGET_EXPR
)
5412 /* Avoid creating another CONSTRUCTOR when we expand the
5414 r
= TARGET_EXPR_INITIAL (r
);
5417 instantiate_constexpr_fns (r
);
5418 r
= cxx_eval_constant_expression (&ctx
, r
,
5419 false, &non_constant_p
, &overflow_p
);
5421 verify_constant (r
, allow_non_constant
, &non_constant_p
, &overflow_p
);
5423 /* Mutable logic is a bit tricky: we want to allow initialization of
5424 constexpr variables with mutable members, but we can't copy those
5425 members to another constexpr variable. */
5426 if (TREE_CODE (r
) == CONSTRUCTOR
5427 && CONSTRUCTOR_MUTABLE_POISON (r
))
5429 if (!allow_non_constant
)
5430 error ("%qE is not a constant expression because it refers to "
5431 "mutable subobjects of %qT", t
, type
);
5432 non_constant_p
= true;
5435 if (TREE_CODE (r
) == CONSTRUCTOR
5436 && CONSTRUCTOR_NO_CLEARING (r
))
5438 if (!allow_non_constant
)
5439 error ("%qE is not a constant expression because it refers to "
5440 "an incompletely initialized variable", t
);
5441 TREE_CONSTANT (r
) = false;
5442 non_constant_p
= true;
5445 /* Technically we should check this for all subexpressions, but that
5446 runs into problems with our internal representation of pointer
5447 subtraction and the 5.19 rules are still in flux. */
5448 if (CONVERT_EXPR_CODE_P (TREE_CODE (r
))
5449 && ARITHMETIC_TYPE_P (TREE_TYPE (r
))
5450 && TREE_CODE (TREE_OPERAND (r
, 0)) == ADDR_EXPR
)
5452 if (!allow_non_constant
)
5453 error ("conversion from pointer type %qT "
5454 "to arithmetic type %qT in a constant expression",
5455 TREE_TYPE (TREE_OPERAND (r
, 0)), TREE_TYPE (r
));
5456 non_constant_p
= true;
5459 if (!non_constant_p
&& overflow_p
)
5460 non_constant_p
= true;
5462 /* Unshare the result unless it's a CONSTRUCTOR in which case it's already
5464 bool should_unshare
= true;
5465 if (r
== t
|| TREE_CODE (r
) == CONSTRUCTOR
)
5466 should_unshare
= false;
5468 if (non_constant_p
&& !allow_non_constant
)
5469 return error_mark_node
;
5470 else if (non_constant_p
&& TREE_CONSTANT (r
))
5472 /* If __builtin_is_constant_evaluated () was evaluated to true
5473 and the result is not a valid constant expression, we need to
5475 if (manifestly_const_eval
)
5476 return cxx_eval_outermost_constant_expr (t
, true, strict
,
5478 /* This isn't actually constant, so unset TREE_CONSTANT.
5479 Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
5480 it to be set if it is invariant address, even when it is not
5481 a valid C++ constant expression. Wrap it with a NOP_EXPR
5483 if (EXPR_P (r
) && TREE_CODE (r
) != ADDR_EXPR
)
5485 else if (TREE_CODE (r
) == CONSTRUCTOR
)
5486 r
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (r
), r
);
5488 r
= build_nop (TREE_TYPE (r
), r
);
5489 TREE_CONSTANT (r
) = false;
5491 else if (non_constant_p
)
5495 r
= unshare_expr (r
);
5497 if (TREE_CODE (r
) == CONSTRUCTOR
&& CLASS_TYPE_P (TREE_TYPE (r
)))
5499 r
= adjust_temp_type (type
, r
);
5500 if (TREE_CODE (t
) == TARGET_EXPR
5501 && TARGET_EXPR_INITIAL (t
) == r
)
5503 else if (TREE_CODE (t
) != CONSTRUCTOR
)
5505 r
= get_target_expr (r
);
5506 TREE_CONSTANT (r
) = true;
5513 /* If T represents a constant expression returns its reduced value.
5514 Otherwise return error_mark_node. If T is dependent, then
5518 cxx_constant_value (tree t
, tree decl
)
5520 return cxx_eval_outermost_constant_expr (t
, false, true, true, decl
);
5523 /* Helper routine for fold_simple function. Either return simplified
5524 expression T, otherwise NULL_TREE.
5525 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
5526 even if we are within template-declaration. So be careful on call, as in
5527 such case types can be undefined. */
5530 fold_simple_1 (tree t
)
5533 enum tree_code code
= TREE_CODE (t
);
5545 return fold_sizeof_expr (t
);
5554 case TRUTH_NOT_EXPR
:
5556 case VIEW_CONVERT_EXPR
:
5559 case FIX_TRUNC_EXPR
:
5560 case FIXED_CONVERT_EXPR
:
5561 case ADDR_SPACE_CONVERT_EXPR
:
5563 op1
= TREE_OPERAND (t
, 0);
5565 t
= const_unop (code
, TREE_TYPE (t
), op1
);
5569 if (CONVERT_EXPR_CODE_P (code
)
5570 && TREE_OVERFLOW_P (t
) && !TREE_OVERFLOW_P (op1
))
5571 TREE_OVERFLOW (t
) = false;
5579 /* If T is a simple constant expression, returns its simplified value.
5580 Otherwise returns T. In contrast to maybe_constant_value we
5581 simplify only few operations on constant-expressions, and we don't
5582 try to simplify constexpressions. */
5585 fold_simple (tree t
)
5587 if (processing_template_decl
)
5590 tree r
= fold_simple_1 (t
);
5597 /* If T is a constant expression, returns its reduced value.
5598 Otherwise, if T does not have TREE_CONSTANT set, returns T.
5599 Otherwise, returns a version of T without TREE_CONSTANT.
5600 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
5603 static GTY((deletable
)) hash_map
<tree
, tree
> *cv_cache
;
5606 maybe_constant_value (tree t
, tree decl
, bool manifestly_const_eval
)
5610 if (!is_nondependent_constant_expression (t
))
5612 if (TREE_OVERFLOW_P (t
))
5614 t
= build_nop (TREE_TYPE (t
), t
);
5615 TREE_CONSTANT (t
) = false;
5619 else if (CONSTANT_CLASS_P (t
))
5620 /* No caching or evaluation needed. */
5623 if (manifestly_const_eval
)
5624 return cxx_eval_outermost_constant_expr (t
, true, true, true, decl
);
5626 if (cv_cache
== NULL
)
5627 cv_cache
= hash_map
<tree
, tree
>::create_ggc (101);
5628 if (tree
*cached
= cv_cache
->get (t
))
5631 r
= cxx_eval_outermost_constant_expr (t
, true, true, false, decl
);
5632 gcc_checking_assert (r
== t
5633 || CONVERT_EXPR_P (t
)
5634 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
5635 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
5636 || !cp_tree_equal (r
, t
));
5637 cv_cache
->put (t
, r
);
5641 /* Dispose of the whole CV_CACHE. */
5644 clear_cv_cache (void)
5646 if (cv_cache
!= NULL
)
5650 /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
5653 clear_cv_and_fold_caches (void)
5656 clear_fold_cache ();
5659 /* Internal function handling expressions in templates for
5660 fold_non_dependent_expr and fold_non_dependent_init.
5662 If we're in a template, but T isn't value dependent, simplify
5663 it. We're supposed to treat:
5665 template <typename T> void f(T[1 + 1]);
5666 template <typename T> void f(T[2]);
5668 as two declarations of the same function, for example. */
5671 fold_non_dependent_expr_template (tree t
, tsubst_flags_t complain
,
5672 bool manifestly_const_eval
)
5674 gcc_assert (processing_template_decl
);
5676 if (is_nondependent_constant_expression (t
))
5678 processing_template_decl_sentinel s
;
5679 t
= instantiate_non_dependent_expr_internal (t
, complain
);
5681 if (type_unknown_p (t
) || BRACE_ENCLOSED_INITIALIZER_P (t
))
5683 if (TREE_OVERFLOW_P (t
))
5685 t
= build_nop (TREE_TYPE (t
), t
);
5686 TREE_CONSTANT (t
) = false;
5691 tree r
= cxx_eval_outermost_constant_expr (t
, true, true,
5692 manifestly_const_eval
,
5694 /* cp_tree_equal looks through NOPs, so allow them. */
5695 gcc_checking_assert (r
== t
5696 || CONVERT_EXPR_P (t
)
5697 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
5698 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
5699 || !cp_tree_equal (r
, t
));
5702 else if (TREE_OVERFLOW_P (t
))
5704 t
= build_nop (TREE_TYPE (t
), t
);
5705 TREE_CONSTANT (t
) = false;
5711 /* Like maybe_constant_value but first fully instantiate the argument.
5713 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
5714 (t, complain) followed by maybe_constant_value but is more efficient,
5715 because it calls instantiation_dependent_expression_p and
5716 potential_constant_expression at most once.
5717 The manifestly_const_eval argument is passed to maybe_constant_value.
5719 Callers should generally pass their active complain, or if they are in a
5720 non-template, diagnosing context, they can use the default of
5721 tf_warning_or_error. Callers that might be within a template context, don't
5722 have a complain parameter, and aren't going to remember the result for long
5723 (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
5727 fold_non_dependent_expr (tree t
,
5728 tsubst_flags_t complain
/* = tf_warning_or_error */,
5729 bool manifestly_const_eval
/* = false */)
5734 if (processing_template_decl
)
5735 return fold_non_dependent_expr_template (t
, complain
,
5736 manifestly_const_eval
);
5738 return maybe_constant_value (t
, NULL_TREE
, manifestly_const_eval
);
5742 /* Like maybe_constant_init but first fully instantiate the argument. */
5745 fold_non_dependent_init (tree t
,
5746 tsubst_flags_t complain
/*=tf_warning_or_error*/,
5747 bool manifestly_const_eval
/*=false*/)
5752 if (processing_template_decl
)
5754 t
= fold_non_dependent_expr_template (t
, complain
,
5755 manifestly_const_eval
);
5756 /* maybe_constant_init does this stripping, so do it here too. */
5757 if (TREE_CODE (t
) == TARGET_EXPR
)
5759 tree init
= TARGET_EXPR_INITIAL (t
);
5760 if (TREE_CODE (init
) == CONSTRUCTOR
)
5766 return maybe_constant_init (t
, NULL_TREE
, manifestly_const_eval
);
5769 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
5770 than wrapped in a TARGET_EXPR.
5771 ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
5772 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
5773 per P0595 even when ALLOW_NON_CONSTANT is true. */
5776 maybe_constant_init_1 (tree t
, tree decl
, bool allow_non_constant
,
5777 bool manifestly_const_eval
)
5781 if (TREE_CODE (t
) == EXPR_STMT
)
5782 t
= TREE_OPERAND (t
, 0);
5783 if (TREE_CODE (t
) == CONVERT_EXPR
5784 && VOID_TYPE_P (TREE_TYPE (t
)))
5785 t
= TREE_OPERAND (t
, 0);
5786 if (TREE_CODE (t
) == INIT_EXPR
)
5787 t
= TREE_OPERAND (t
, 1);
5788 if (TREE_CODE (t
) == TARGET_EXPR
)
5789 t
= TARGET_EXPR_INITIAL (t
);
5790 if (!is_nondependent_static_init_expression (t
))
5791 /* Don't try to evaluate it. */;
5792 else if (CONSTANT_CLASS_P (t
) && allow_non_constant
)
5793 /* No evaluation needed. */;
5795 t
= cxx_eval_outermost_constant_expr (t
, allow_non_constant
,
5797 manifestly_const_eval
, decl
);
5798 if (TREE_CODE (t
) == TARGET_EXPR
)
5800 tree init
= TARGET_EXPR_INITIAL (t
);
5801 if (TREE_CODE (init
) == CONSTRUCTOR
)
5807 /* Wrapper for maybe_constant_init_1 which permits non constants. */
5810 maybe_constant_init (tree t
, tree decl
, bool manifestly_const_eval
)
5812 return maybe_constant_init_1 (t
, decl
, true, manifestly_const_eval
);
5815 /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
5818 cxx_constant_init (tree t
, tree decl
)
5820 return maybe_constant_init_1 (t
, decl
, false, true);
5824 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
5825 /* Return true if the object referred to by REF has automatic or thread
5828 enum { ck_ok
, ck_bad
, ck_unknown
};
5830 check_automatic_or_tls (tree ref
)
5833 poly_int64 bitsize
, bitpos
;
5835 int volatilep
= 0, unsignedp
= 0;
5836 tree decl
= get_inner_reference (ref
, &bitsize
, &bitpos
, &offset
,
5837 &mode
, &unsignedp
, &volatilep
, false);
5840 /* If there isn't a decl in the middle, we don't know the linkage here,
5841 and this isn't a constant expression anyway. */
5844 dk
= decl_storage_duration (decl
);
5845 return (dk
== dk_auto
|| dk
== dk_thread
) ? ck_bad
: ck_ok
;
5849 /* Data structure for passing data from potential_constant_expression_1
5850 to check_for_return_continue via cp_walk_tree. */
5851 struct check_for_return_continue_data
{
5852 hash_set
<tree
> *pset
;
5856 /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
5857 called through cp_walk_tree. Return the first RETURN_EXPR found, or note
5858 the first CONTINUE_STMT if RETURN_EXPR is not found. */
5860 check_for_return_continue (tree
*tp
, int *walk_subtrees
, void *data
)
5863 check_for_return_continue_data
*d
= (check_for_return_continue_data
*) data
;
5864 switch (TREE_CODE (t
))
5870 if (d
->continue_stmt
== NULL_TREE
)
5871 d
->continue_stmt
= t
;
5875 if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
5879 /* For loops, walk subtrees manually, so that continue stmts found
5880 inside of the bodies of the loops are ignored. */
5883 RECUR (DO_COND (t
));
5884 s
= d
->continue_stmt
;
5885 RECUR (DO_BODY (t
));
5886 d
->continue_stmt
= s
;
5891 RECUR (WHILE_COND (t
));
5892 s
= d
->continue_stmt
;
5893 RECUR (WHILE_BODY (t
));
5894 d
->continue_stmt
= s
;
5899 RECUR (FOR_INIT_STMT (t
));
5900 RECUR (FOR_COND (t
));
5901 RECUR (FOR_EXPR (t
));
5902 s
= d
->continue_stmt
;
5903 RECUR (FOR_BODY (t
));
5904 d
->continue_stmt
= s
;
5907 case RANGE_FOR_STMT
:
5909 RECUR (RANGE_FOR_EXPR (t
));
5910 s
= d
->continue_stmt
;
5911 RECUR (RANGE_FOR_BODY (t
));
5912 d
->continue_stmt
= s
;
5916 case STATEMENT_LIST
:
5929 /* Return true if T denotes a potentially constant expression. Issue
5930 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
5931 an lvalue-rvalue conversion is implied. If NOW is true, we want to
5932 consider the expression in the current context, independent of constexpr
5935 C++0x [expr.const] used to say
5937 6 An expression is a potential constant expression if it is
5938 a constant expression where all occurrences of function
5939 parameters are replaced by arbitrary constant expressions
5940 of the appropriate type.
5942 2 A conditional expression is a constant expression unless it
5943 involves one of the following as a potentially evaluated
5944 subexpression (3.2), but subexpressions of logical AND (5.14),
5945 logical OR (5.15), and conditional (5.16) operations that are
5946 not evaluated are not considered. */
5949 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
, bool now
,
5950 tsubst_flags_t flags
, tree
*jump_target
)
5952 #define RECUR(T,RV) \
5953 potential_constant_expression_1 ((T), (RV), strict, now, flags, jump_target)
5955 enum { any
= false, rval
= true };
5959 if (t
== error_mark_node
)
5963 location_t loc
= cp_expr_loc_or_input_loc (t
);
5966 /* If we are jumping, ignore everything. This is simpler than the
5967 cxx_eval_constant_expression handling because we only need to be
5968 conservatively correct, and we don't necessarily have a constant value
5969 available, so we don't bother with switch tracking. */
5972 if (TREE_THIS_VOLATILE (t
) && want_rval
)
5974 if (flags
& tf_error
)
5975 error_at (loc
, "lvalue-to-rvalue conversion of a volatile lvalue "
5976 "%qE with type %qT", t
, TREE_TYPE (t
));
5979 if (CONSTANT_CLASS_P (t
))
5981 if (CODE_CONTAINS_STRUCT (TREE_CODE (t
), TS_TYPED
)
5982 && TREE_TYPE (t
) == error_mark_node
)
5985 switch (TREE_CODE (t
))
5991 case TEMPLATE_ID_EXPR
:
5994 case CASE_LABEL_EXPR
:
6000 case TEMPLATE_PARM_INDEX
:
6002 case IDENTIFIER_NODE
:
6003 case USERDEF_LITERAL
:
6004 /* We can see a FIELD_DECL in a pointer-to-member expression. */
6009 case PLACEHOLDER_EXPR
:
6012 case DEBUG_BEGIN_STMT
:
6016 if (!RECUR (TREE_OPERAND (t
, 0), any
))
6028 if (flags
& tf_error
)
6029 error ("%qE is not a constant expression", t
);
6034 case AGGR_INIT_EXPR
:
6036 /* -- an invocation of a function other than a constexpr function
6037 or a constexpr constructor. */
6039 tree fun
= get_function_named_in_call (t
);
6040 const int nargs
= call_expr_nargs (t
);
6043 if (fun
== NULL_TREE
)
6045 /* Reset to allow the function to continue past the end
6046 of the block below. Otherwise return early. */
6049 if (TREE_CODE (t
) == CALL_EXPR
6050 && CALL_EXPR_FN (t
) == NULL_TREE
)
6051 switch (CALL_EXPR_IFN (t
))
6053 /* These should be ignored, they are optimized away from
6054 constexpr functions. */
6055 case IFN_UBSAN_NULL
:
6056 case IFN_UBSAN_BOUNDS
:
6057 case IFN_UBSAN_VPTR
:
6058 case IFN_FALLTHROUGH
:
6061 case IFN_ADD_OVERFLOW
:
6062 case IFN_SUB_OVERFLOW
:
6063 case IFN_MUL_OVERFLOW
:
6065 case IFN_VEC_CONVERT
:
6075 /* fold_call_expr can't do anything with IFN calls. */
6076 if (flags
& tf_error
)
6077 error_at (loc
, "call to internal function %qE", t
);
6082 if (fun
&& is_overloaded_fn (fun
))
6084 if (TREE_CODE (fun
) == FUNCTION_DECL
)
6086 if (builtin_valid_in_constant_expr_p (fun
))
6088 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
6089 /* Allow any built-in function; if the expansion
6090 isn't constant, we'll deal with that then. */
6091 && !fndecl_built_in_p (fun
))
6093 if (flags
& tf_error
)
6095 error_at (loc
, "call to non-%<constexpr%> function %qD",
6097 explain_invalid_constexpr_fn (fun
);
6101 /* A call to a non-static member function takes the address
6102 of the object as the first argument. But in a constant
6103 expression the address will be folded away, so look
6105 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
6106 && !DECL_CONSTRUCTOR_P (fun
))
6108 tree x
= get_nth_callarg (t
, 0);
6109 if (is_this_parameter (x
))
6111 /* Don't require an immediately constant value, as
6112 constexpr substitution might not use the value. */
6113 bool sub_now
= false;
6114 if (!potential_constant_expression_1 (x
, rval
, strict
,
6123 if (!RECUR (fun
, true))
6125 fun
= get_first_fn (fun
);
6127 /* Skip initial arguments to base constructors. */
6128 if (DECL_BASE_CONSTRUCTOR_P (fun
))
6129 i
= num_artificial_parms_for (fun
);
6130 fun
= DECL_ORIGIN (fun
);
6134 if (RECUR (fun
, rval
))
6135 /* Might end up being a constant function pointer. */;
6139 for (; i
< nargs
; ++i
)
6141 tree x
= get_nth_callarg (t
, i
);
6142 /* In a template, reference arguments haven't been converted to
6143 REFERENCE_TYPE and we might not even know if the parameter
6144 is a reference, so accept lvalue constants too. */
6145 bool rv
= processing_template_decl
? any
: rval
;
6146 /* Don't require an immediately constant value, as constexpr
6147 substitution might not use the value of the argument. */
6148 bool sub_now
= false;
6149 if (!potential_constant_expression_1 (x
, rv
, strict
,
6150 sub_now
, flags
, jump_target
))
6156 case NON_LVALUE_EXPR
:
6157 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
6158 -- an lvalue of integral type that refers to a non-volatile
6159 const variable or static data member initialized with
6160 constant expressions, or
6162 -- an lvalue of literal type that refers to non-volatile
6163 object defined with constexpr, or that refers to a
6164 sub-object of such an object; */
6165 return RECUR (TREE_OPERAND (t
, 0), rval
);
6168 if (DECL_HAS_VALUE_EXPR_P (t
))
6170 if (now
&& is_normal_capture_proxy (t
))
6172 /* -- in a lambda-expression, a reference to this or to a
6173 variable with automatic storage duration defined outside that
6174 lambda-expression, where the reference would be an
6176 if (flags
& tf_error
)
6178 tree cap
= DECL_CAPTURED_VARIABLE (t
);
6179 error ("lambda capture of %qE is not a constant expression",
6181 if (!want_rval
&& decl_constant_var_p (cap
))
6182 inform (input_location
, "because it is used as a glvalue");
6186 return RECUR (DECL_VALUE_EXPR (t
), rval
);
6189 && !var_in_maybe_constexpr_fn (t
)
6190 && !type_dependent_expression_p (t
)
6191 && !decl_maybe_constant_var_p (t
)
6193 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t
))
6194 || (DECL_INITIAL (t
)
6195 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t
)))
6196 && COMPLETE_TYPE_P (TREE_TYPE (t
))
6197 && !is_really_empty_class (TREE_TYPE (t
), /*ignore_vptr*/false))
6199 if (flags
& tf_error
)
6200 non_const_var_error (t
);
6206 if (REINTERPRET_CAST_P (t
))
6208 if (flags
& tf_error
)
6209 error_at (loc
, "%<reinterpret_cast%> is not a constant expression");
6214 case VIEW_CONVERT_EXPR
:
6215 /* -- a reinterpret_cast. FIXME not implemented, and this rule
6216 may change to something more specific to type-punning (DR 1312). */
6218 tree from
= TREE_OPERAND (t
, 0);
6219 if (location_wrapper_p (t
))
6220 return (RECUR (from
, want_rval
));
6221 if (INDIRECT_TYPE_P (TREE_TYPE (t
)))
6223 STRIP_ANY_LOCATION_WRAPPER (from
);
6224 if (TREE_CODE (from
) == INTEGER_CST
6225 && !integer_zerop (from
))
6227 if (flags
& tf_error
)
6229 "%<reinterpret_cast%> from integer to pointer");
6233 return (RECUR (from
, TREE_CODE (t
) != VIEW_CONVERT_EXPR
));
6236 case ADDRESSOF_EXPR
:
6237 /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
6238 t
= TREE_OPERAND (t
, 0);
6239 goto handle_addr_expr
;
6242 /* -- a unary operator & that is applied to an lvalue that
6243 designates an object with thread or automatic storage
6245 t
= TREE_OPERAND (t
, 0);
6247 if (TREE_CODE (t
) == OFFSET_REF
&& PTRMEM_OK_P (t
))
6248 /* A pointer-to-member constant. */
6253 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
6254 any checking here, as we might dereference the pointer later. If
6255 we remove this code, also remove check_automatic_or_tls. */
6256 i
= check_automatic_or_tls (t
);
6261 if (flags
& tf_error
)
6262 error ("address-of an object %qE with thread local or "
6263 "automatic storage is not a constant expression", t
);
6267 return RECUR (t
, any
);
6275 /* -- a class member access unless its postfix-expression is
6276 of literal type or of pointer to literal type. */
6277 /* This test would be redundant, as it follows from the
6278 postfix-expression being a potential constant expression. */
6279 if (type_unknown_p (t
))
6281 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
6283 case EXPR_PACK_EXPANSION
:
6284 return RECUR (PACK_EXPANSION_PATTERN (t
), want_rval
);
6288 tree x
= TREE_OPERAND (t
, 0);
6290 if (is_this_parameter (x
) && !is_capture_proxy (x
))
6292 if (!var_in_maybe_constexpr_fn (x
))
6294 if (flags
& tf_error
)
6295 error_at (loc
, "use of %<this%> in a constant expression");
6300 return RECUR (x
, rval
);
6303 case STATEMENT_LIST
:
6305 tree_stmt_iterator i
;
6306 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
6308 if (!RECUR (tsi_stmt (i
), any
))
6316 if (cxx_dialect
< cxx14
)
6318 if (!RECUR (TREE_OPERAND (t
, 0), any
))
6320 if (!RECUR (TREE_OPERAND (t
, 1), rval
))
6325 if (cxx_dialect
< cxx14
)
6327 if (!RECUR (TREE_OPERAND (t
, 0), rval
))
6329 if (!RECUR (TREE_OPERAND (t
, 2), rval
))
6334 if (!RECUR (DO_COND (t
), rval
))
6336 if (!RECUR (DO_BODY (t
), any
))
6338 if (breaks (jump_target
) || continues (jump_target
))
6339 *jump_target
= NULL_TREE
;
6343 if (!RECUR (FOR_INIT_STMT (t
), any
))
6346 if (!RECUR (tmp
, rval
))
6350 if (!processing_template_decl
)
6351 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
6352 /* If we couldn't evaluate the condition, it might not ever be
6354 if (!integer_onep (tmp
))
6357 if (!RECUR (FOR_EXPR (t
), any
))
6359 if (!RECUR (FOR_BODY (t
), any
))
6361 if (breaks (jump_target
) || continues (jump_target
))
6362 *jump_target
= NULL_TREE
;
6365 case RANGE_FOR_STMT
:
6366 if (!RECUR (RANGE_FOR_INIT_STMT (t
), any
))
6368 if (!RECUR (RANGE_FOR_EXPR (t
), any
))
6370 if (!RECUR (RANGE_FOR_BODY (t
), any
))
6372 if (breaks (jump_target
) || continues (jump_target
))
6373 *jump_target
= NULL_TREE
;
6377 tmp
= WHILE_COND (t
);
6378 if (!RECUR (tmp
, rval
))
6380 if (!processing_template_decl
)
6381 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
6382 /* If we couldn't evaluate the condition, it might not ever be true. */
6383 if (!integer_onep (tmp
))
6385 if (!RECUR (WHILE_BODY (t
), any
))
6387 if (breaks (jump_target
) || continues (jump_target
))
6388 *jump_target
= NULL_TREE
;
6392 if (!RECUR (SWITCH_STMT_COND (t
), rval
))
6394 /* FIXME we don't check SWITCH_STMT_BODY currently, because even
6395 unreachable labels would be checked and it is enough if there is
6396 a single switch cond value for which it is a valid constant
6397 expression. We need to check if there are any RETURN_EXPRs
6398 or CONTINUE_STMTs inside of the body though, as in that case
6399 we need to set *jump_target. */
6402 hash_set
<tree
> pset
;
6403 check_for_return_continue_data data
= { &pset
, NULL_TREE
};
6405 = cp_walk_tree (&SWITCH_STMT_BODY (t
), check_for_return_continue
,
6407 /* The switch might return. */
6408 *jump_target
= ret_expr
;
6409 else if (data
.continue_stmt
)
6410 /* The switch can't return, but might continue. */
6411 *jump_target
= data
.continue_stmt
;
6416 return RECUR (STMT_EXPR_STMT (t
), rval
);
6419 if (cxx_dialect
>= cxx17
)
6420 /* In C++17 lambdas can be constexpr, don't give up yet. */
6422 else if (flags
& tf_error
)
6423 error_at (loc
, "lambda-expression is not a constant expression "
6427 case DYNAMIC_CAST_EXPR
:
6428 case PSEUDO_DTOR_EXPR
:
6432 case VEC_DELETE_EXPR
:
6438 case OMP_DISTRIBUTE
:
6442 case OMP_TARGET_DATA
:
6451 case OMP_TARGET_UPDATE
:
6452 case OMP_TARGET_ENTER_DATA
:
6453 case OMP_TARGET_EXIT_DATA
:
6455 case OMP_ATOMIC_READ
:
6456 case OMP_ATOMIC_CAPTURE_OLD
:
6457 case OMP_ATOMIC_CAPTURE_NEW
:
6462 case OACC_HOST_DATA
:
6466 case OACC_ENTER_DATA
:
6467 case OACC_EXIT_DATA
:
6469 /* GCC internal stuff. */
6471 case TRANSACTION_EXPR
:
6473 case AT_ENCODE_EXPR
:
6475 if (flags
& tf_error
)
6476 error_at (loc
, "expression %qE is not a constant expression", t
);
6480 if (cxx_dialect
>= cxx2a
)
6481 /* In C++2a virtual calls can be constexpr, don't give up yet. */
6483 else if (flags
& tf_error
)
6485 "virtual functions cannot be %<constexpr%> before C++2a");
6489 /* -- a typeid expression whose operand is of polymorphic
6492 tree e
= TREE_OPERAND (t
, 0);
6493 if (!TYPE_P (e
) && !type_dependent_expression_p (e
)
6494 && TYPE_POLYMORPHIC_P (TREE_TYPE (e
)))
6496 if (flags
& tf_error
)
6497 error_at (loc
, "%<typeid%> is not a constant expression "
6498 "because %qE is of polymorphic type", e
);
6504 case POINTER_DIFF_EXPR
:
6518 case PREINCREMENT_EXPR
:
6519 case POSTINCREMENT_EXPR
:
6520 case PREDECREMENT_EXPR
:
6521 case POSTDECREMENT_EXPR
:
6522 if (cxx_dialect
< cxx14
)
6528 if (TYPE_P (TREE_OPERAND (t
, 0)))
6534 case FIX_TRUNC_EXPR
:
6539 case TRUTH_NOT_EXPR
:
6540 case FIXED_CONVERT_EXPR
:
6541 case UNARY_PLUS_EXPR
:
6542 case UNARY_LEFT_FOLD_EXPR
:
6543 case UNARY_RIGHT_FOLD_EXPR
:
6545 return RECUR (TREE_OPERAND (t
, 0), rval
);
6548 case CONST_CAST_EXPR
:
6549 case STATIC_CAST_EXPR
:
6550 case REINTERPRET_CAST_EXPR
:
6551 case IMPLICIT_CONV_EXPR
:
6552 if (cxx_dialect
< cxx11
6553 && !dependent_type_p (TREE_TYPE (t
))
6554 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
)))
6555 /* In C++98, a conversion to non-integral type can't be part of a
6556 constant expression. */
6558 if (flags
& tf_error
)
6560 "cast to non-integral type %qT in a constant expression",
6564 /* This might be a conversion from a class to a (potentially) literal
6565 type. Let's consider it potentially constant since the conversion
6566 might be a constexpr user-defined conversion. */
6567 else if (cxx_dialect
>= cxx11
6568 && (dependent_type_p (TREE_TYPE (t
))
6569 || !COMPLETE_TYPE_P (TREE_TYPE (t
))
6570 || literal_type_p (TREE_TYPE (t
)))
6571 && TREE_OPERAND (t
, 0))
6573 tree type
= TREE_TYPE (TREE_OPERAND (t
, 0));
6574 /* If this is a dependent type, it could end up being a class
6575 with conversions. */
6576 if (type
== NULL_TREE
|| WILDCARD_TYPE_P (type
))
6578 /* Or a non-dependent class which has conversions. */
6579 else if (CLASS_TYPE_P (type
)
6580 && (TYPE_HAS_CONVERSION (type
) || dependent_scope_p (type
)))
6584 return (RECUR (TREE_OPERAND (t
, 0),
6585 !TYPE_REF_P (TREE_TYPE (t
))));
6588 return RECUR (BIND_EXPR_BODY (t
), want_rval
);
6590 case CLEANUP_POINT_EXPR
:
6591 case MUST_NOT_THROW_EXPR
:
6592 case TRY_CATCH_EXPR
:
6597 case NON_DEPENDENT_EXPR
:
6598 /* For convenience. */
6601 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
6604 tmp
= DECL_EXPR_DECL (t
);
6605 if (VAR_P (tmp
) && !DECL_ARTIFICIAL (tmp
))
6607 if (TREE_STATIC (tmp
))
6609 if (flags
& tf_error
)
6610 error_at (DECL_SOURCE_LOCATION (tmp
), "%qD declared "
6611 "%<static%> in %<constexpr%> context", tmp
);
6614 else if (CP_DECL_THREAD_LOCAL_P (tmp
))
6616 if (flags
& tf_error
)
6617 error_at (DECL_SOURCE_LOCATION (tmp
), "%qD declared "
6618 "%<thread_local%> in %<constexpr%> context", tmp
);
6621 else if (!check_for_uninitialized_const_var
6622 (tmp
, /*constexpr_context_p=*/true, flags
))
6625 return RECUR (tmp
, want_rval
);
6627 case TRY_FINALLY_EXPR
:
6628 return (RECUR (TREE_OPERAND (t
, 0), want_rval
)
6629 && RECUR (TREE_OPERAND (t
, 1), any
));
6632 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
6635 if (!TARGET_EXPR_DIRECT_INIT_P (t
)
6636 && !literal_type_p (TREE_TYPE (t
)))
6638 if (flags
& tf_error
)
6640 auto_diagnostic_group d
;
6641 error_at (loc
, "temporary of non-literal type %qT in a "
6642 "constant expression", TREE_TYPE (t
));
6643 explain_non_literal_class (TREE_TYPE (t
));
6649 return RECUR (TREE_OPERAND (t
, 1), rval
);
6653 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
6654 constructor_elt
*ce
;
6655 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
6656 if (!RECUR (ce
->value
, want_rval
))
6663 gcc_assert (TREE_PURPOSE (t
) == NULL_TREE
6664 || DECL_P (TREE_PURPOSE (t
)));
6665 if (!RECUR (TREE_VALUE (t
), want_rval
))
6667 if (TREE_CHAIN (t
) == NULL_TREE
)
6669 return RECUR (TREE_CHAIN (t
), want_rval
);
6672 case TRUNC_DIV_EXPR
:
6674 case FLOOR_DIV_EXPR
:
6675 case ROUND_DIV_EXPR
:
6676 case TRUNC_MOD_EXPR
:
6678 case ROUND_MOD_EXPR
:
6680 tree denom
= TREE_OPERAND (t
, 1);
6681 if (!RECUR (denom
, rval
))
6683 /* We can't call cxx_eval_outermost_constant_expr on an expression
6684 that hasn't been through instantiate_non_dependent_expr yet. */
6685 if (!processing_template_decl
)
6686 denom
= cxx_eval_outermost_constant_expr (denom
, true);
6687 if (integer_zerop (denom
))
6689 if (flags
& tf_error
)
6690 error ("division by zero is not a constant expression");
6696 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
6702 /* check_return_expr sometimes wraps a TARGET_EXPR in a
6703 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
6704 introduced by build_call_a. */
6705 tree op0
= TREE_OPERAND (t
, 0);
6706 tree op1
= TREE_OPERAND (t
, 1);
6708 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
6709 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
6710 return RECUR (op0
, want_rval
);
6715 /* If the first operand is the non-short-circuit constant, look at
6716 the second operand; otherwise we only care about the first one for
6718 case TRUTH_AND_EXPR
:
6719 case TRUTH_ANDIF_EXPR
:
6720 tmp
= boolean_true_node
;
6723 case TRUTH_ORIF_EXPR
:
6724 tmp
= boolean_false_node
;
6727 tree op
= TREE_OPERAND (t
, 0);
6728 if (!RECUR (op
, rval
))
6730 if (!processing_template_decl
)
6731 op
= cxx_eval_outermost_constant_expr (op
, true);
6732 if (tree_int_cst_equal (op
, tmp
))
6733 return RECUR (TREE_OPERAND (t
, 1), rval
);
6740 case POINTER_PLUS_EXPR
:
6742 case EXACT_DIV_EXPR
:
6752 case TRUTH_XOR_EXPR
:
6753 case UNORDERED_EXPR
:
6766 case ARRAY_RANGE_REF
:
6770 case BINARY_LEFT_FOLD_EXPR
:
6771 case BINARY_RIGHT_FOLD_EXPR
:
6773 for (i
= 0; i
< 2; ++i
)
6774 if (!RECUR (TREE_OPERAND (t
, i
), want_rval
))
6779 for (i
= 0; i
< 3; ++i
)
6780 if (!RECUR (TREE_OPERAND (t
, i
), true))
6785 if (COND_EXPR_IS_VEC_DELETE (t
))
6787 if (flags
& tf_error
)
6788 error_at (loc
, "%<delete[]%> is not a constant expression");
6794 /* If the condition is a known constant, we know which of the legs we
6795 care about; otherwise we only require that the condition and
6796 either of the legs be potentially constant. */
6797 tmp
= TREE_OPERAND (t
, 0);
6798 if (!RECUR (tmp
, rval
))
6800 if (!processing_template_decl
)
6801 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
6802 if (integer_zerop (tmp
))
6803 return RECUR (TREE_OPERAND (t
, 2), want_rval
);
6804 else if (TREE_CODE (tmp
) == INTEGER_CST
)
6805 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
6806 for (i
= 1; i
< 3; ++i
)
6807 if (potential_constant_expression_1 (TREE_OPERAND (t
, i
),
6808 want_rval
, strict
, now
,
6809 tf_none
, jump_target
))
6811 if (flags
& tf_error
)
6812 error_at (loc
, "expression %qE is not a constant expression", t
);
6816 if (VEC_INIT_EXPR_IS_CONSTEXPR (t
))
6818 if (flags
& tf_error
)
6820 error_at (loc
, "non-constant array initialization");
6821 diagnose_non_constexpr_vec_init (t
);
6827 /* We can see these in statement-expressions. */
6831 case EMPTY_CLASS_EXPR
:
6837 tree
*target
= &TREE_OPERAND (t
, 0);
6838 /* Gotos representing break and continue are OK. */
6839 if (breaks (target
) || continues (target
))
6841 *jump_target
= *target
;
6844 if (flags
& tf_error
)
6845 error_at (loc
, "%<goto%> is not a constant expression");
6850 return RECUR (TREE_OPERAND (t
, 0), rval
);
6853 if (objc_is_property_ref (t
))
6856 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t
)));
6864 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
, bool now
,
6865 tsubst_flags_t flags
)
6867 tree target
= NULL_TREE
;
6868 return potential_constant_expression_1 (t
, want_rval
, strict
, now
,
6872 /* The main entry point to the above. */
6875 potential_constant_expression (tree t
)
6877 return potential_constant_expression_1 (t
, false, true, false, tf_none
);
6880 /* As above, but require a constant rvalue. */
6883 potential_rvalue_constant_expression (tree t
)
6885 return potential_constant_expression_1 (t
, true, true, false, tf_none
);
6888 /* Like above, but complain about non-constant expressions. */
6891 require_potential_constant_expression (tree t
)
6893 return potential_constant_expression_1 (t
, false, true, false,
6894 tf_warning_or_error
);
6897 /* Cross product of the above. */
6900 require_potential_rvalue_constant_expression (tree t
)
6902 return potential_constant_expression_1 (t
, true, true, false,
6903 tf_warning_or_error
);
6906 /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
6909 require_rvalue_constant_expression (tree t
)
6911 return potential_constant_expression_1 (t
, true, true, true,
6912 tf_warning_or_error
);
6915 /* Like potential_constant_expression, but don't consider possible constexpr
6916 substitution of the current function. That is, PARM_DECL qualifies under
6917 potential_constant_expression, but not here.
6919 This is basically what you can check when any actual constant values might
6920 be value-dependent. */
6923 is_constant_expression (tree t
)
6925 return potential_constant_expression_1 (t
, false, true, true, tf_none
);
6928 /* Like above, but complain about non-constant expressions. */
6931 require_constant_expression (tree t
)
6933 return potential_constant_expression_1 (t
, false, true, true,
6934 tf_warning_or_error
);
6937 /* Like is_constant_expression, but allow const variables that are not allowed
6938 under constexpr rules. */
6941 is_static_init_expression (tree t
)
6943 return potential_constant_expression_1 (t
, false, false, true, tf_none
);
6946 /* Returns true if T is a potential constant expression that is not
6947 instantiation-dependent, and therefore a candidate for constant folding even
6951 is_nondependent_constant_expression (tree t
)
6953 return (!type_unknown_p (t
)
6954 && !BRACE_ENCLOSED_INITIALIZER_P (t
)
6955 && is_constant_expression (t
)
6956 && !instantiation_dependent_expression_p (t
));
6959 /* Returns true if T is a potential static initializer expression that is not
6960 instantiation-dependent. */
6963 is_nondependent_static_init_expression (tree t
)
6965 return (!type_unknown_p (t
)
6966 && !BRACE_ENCLOSED_INITIALIZER_P (t
)
6967 && is_static_init_expression (t
)
6968 && !instantiation_dependent_expression_p (t
));
6971 /* Finalize constexpr processing after parsing. */
6974 fini_constexpr (void)
6976 /* The contexpr call and fundef copies tables are no longer needed. */
6977 constexpr_call_table
= NULL
;
6978 fundef_copies_table
= NULL
;
6981 #include "gt-cp-constexpr.h"