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-2016 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"
35 static bool verify_constant (tree
, bool, bool *, bool *);
36 #define VERIFY_CONSTANT(X) \
38 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
42 /* Returns true iff FUN is an instantiation of a constexpr function
43 template or a defaulted constexpr function. */
46 is_instantiation_of_constexpr (tree fun
)
48 return ((DECL_TEMPLOID_INSTANTIATION (fun
)
49 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun
)))
50 || (DECL_DEFAULTED_FN (fun
)
51 && DECL_DECLARED_CONSTEXPR_P (fun
)));
54 /* Return true if T is a literal type. */
57 literal_type_p (tree t
)
61 || TREE_CODE (t
) == REFERENCE_TYPE
62 || (VOID_TYPE_P (t
) && cxx_dialect
>= cxx14
))
66 t
= complete_type (t
);
67 gcc_assert (COMPLETE_TYPE_P (t
) || errorcount
);
68 return CLASSTYPE_LITERAL_P (t
);
70 if (TREE_CODE (t
) == ARRAY_TYPE
)
71 return literal_type_p (strip_array_types (t
));
75 /* If DECL is a variable declared `constexpr', require its type
76 be literal. Return the DECL if OK, otherwise NULL. */
79 ensure_literal_type_for_constexpr_object (tree decl
)
81 tree type
= TREE_TYPE (decl
);
83 && (DECL_DECLARED_CONSTEXPR_P (decl
)
84 || var_in_constexpr_fn (decl
))
85 && !processing_template_decl
)
87 tree stype
= strip_array_types (type
);
88 if (CLASS_TYPE_P (stype
) && !COMPLETE_TYPE_P (complete_type (stype
)))
89 /* Don't complain here, we'll complain about incompleteness
90 when we try to initialize the variable. */;
91 else if (!literal_type_p (type
))
93 if (DECL_DECLARED_CONSTEXPR_P (decl
))
95 error ("the type %qT of constexpr variable %qD is not literal",
97 explain_non_literal_class (type
);
101 if (!DECL_TEMPLATE_INSTANTIATION (current_function_decl
))
103 error ("variable %qD of non-literal type %qT in %<constexpr%> "
104 "function", decl
, type
);
105 explain_non_literal_class (type
);
107 cp_function_chain
->invalid_constexpr
= true;
115 /* Representation of entries in the constexpr function definition table. */
117 struct GTY((for_user
)) constexpr_fundef
{
122 struct constexpr_fundef_hasher
: ggc_ptr_hash
<constexpr_fundef
>
124 static hashval_t
hash (constexpr_fundef
*);
125 static bool equal (constexpr_fundef
*, constexpr_fundef
*);
128 /* This table holds all constexpr function definitions seen in
129 the current translation unit. */
131 static GTY (()) hash_table
<constexpr_fundef_hasher
> *constexpr_fundef_table
;
133 /* Utility function used for managing the constexpr function table.
134 Return true if the entries pointed to by P and Q are for the
135 same constexpr function. */
138 constexpr_fundef_hasher::equal (constexpr_fundef
*lhs
, constexpr_fundef
*rhs
)
140 return lhs
->decl
== rhs
->decl
;
143 /* Utility function used for managing the constexpr function table.
144 Return a hash value for the entry pointed to by Q. */
147 constexpr_fundef_hasher::hash (constexpr_fundef
*fundef
)
149 return DECL_UID (fundef
->decl
);
152 /* Return a previously saved definition of function FUN. */
154 static constexpr_fundef
*
155 retrieve_constexpr_fundef (tree fun
)
157 constexpr_fundef fundef
= { NULL
, NULL
};
158 if (constexpr_fundef_table
== NULL
)
162 return constexpr_fundef_table
->find (&fundef
);
165 /* Check whether the parameter and return types of FUN are valid for a
166 constexpr function, and complain if COMPLAIN. */
169 is_valid_constexpr_fn (tree fun
, bool complain
)
173 if (DECL_INHERITED_CTOR_BASE (fun
)
174 && TREE_CODE (fun
) == TEMPLATE_DECL
)
178 error ("inherited constructor %qD is not constexpr",
179 get_inherited_ctor (fun
));
183 for (tree parm
= FUNCTION_FIRST_USER_PARM (fun
);
184 parm
!= NULL_TREE
; parm
= TREE_CHAIN (parm
))
185 if (!literal_type_p (TREE_TYPE (parm
)))
190 error ("invalid type for parameter %d of constexpr "
191 "function %q+#D", DECL_PARM_INDEX (parm
), fun
);
192 explain_non_literal_class (TREE_TYPE (parm
));
197 if (!DECL_CONSTRUCTOR_P (fun
))
199 tree rettype
= TREE_TYPE (TREE_TYPE (fun
));
200 if (!literal_type_p (rettype
))
205 error ("invalid return type %qT of constexpr function %q+D",
207 explain_non_literal_class (rettype
);
211 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
212 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun
)))
217 error ("enclosing class of constexpr non-static member "
218 "function %q+#D is not a literal type", fun
);
219 explain_non_literal_class (DECL_CONTEXT (fun
));
223 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun
)))
227 error ("%q#T has virtual base classes", DECL_CONTEXT (fun
));
233 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
234 for a member of an anonymous aggregate, INIT is the initializer for that
235 member, and VEC_OUTER is the vector of constructor elements for the class
236 whose constructor we are processing. Add the initializer to the vector
237 and return true to indicate success. */
240 build_anon_member_initialization (tree member
, tree init
,
241 vec
<constructor_elt
, va_gc
> **vec_outer
)
243 /* MEMBER presents the relevant fields from the inside out, but we need
244 to build up the initializer from the outside in so that we can reuse
245 previously built CONSTRUCTORs if this is, say, the second field in an
246 anonymous struct. So we use a vec as a stack. */
247 auto_vec
<tree
, 2> fields
;
250 fields
.safe_push (TREE_OPERAND (member
, 1));
251 member
= TREE_OPERAND (member
, 0);
253 while (ANON_AGGR_TYPE_P (TREE_TYPE (member
))
254 && TREE_CODE (member
) == COMPONENT_REF
);
256 /* VEC has the constructor elements vector for the context of FIELD.
257 If FIELD is an anonymous aggregate, we will push inside it. */
258 vec
<constructor_elt
, va_gc
> **vec
= vec_outer
;
260 while (field
= fields
.pop(),
261 ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
264 /* If there is already an outer constructor entry for the anonymous
265 aggregate FIELD, use it; otherwise, insert one. */
266 if (vec_safe_is_empty (*vec
)
267 || (*vec
)->last().index
!= field
)
269 ctor
= build_constructor (TREE_TYPE (field
), NULL
);
270 CONSTRUCTOR_APPEND_ELT (*vec
, field
, ctor
);
273 ctor
= (*vec
)->last().value
;
274 vec
= &CONSTRUCTOR_ELTS (ctor
);
277 /* Now we're at the innermost field, the one that isn't an anonymous
278 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
279 gcc_assert (fields
.is_empty());
280 CONSTRUCTOR_APPEND_ELT (*vec
, field
, init
);
285 /* Subroutine of build_constexpr_constructor_member_initializers.
286 The expression tree T represents a data member initialization
287 in a (constexpr) constructor definition. Build a pairing of
288 the data member with its initializer, and prepend that pair
289 to the existing initialization pair INITS. */
292 build_data_member_initialization (tree t
, vec
<constructor_elt
, va_gc
> **vec
)
295 if (TREE_CODE (t
) == CLEANUP_POINT_EXPR
)
296 t
= TREE_OPERAND (t
, 0);
297 if (TREE_CODE (t
) == EXPR_STMT
)
298 t
= TREE_OPERAND (t
, 0);
299 if (t
== error_mark_node
)
301 if (TREE_CODE (t
) == STATEMENT_LIST
)
303 tree_stmt_iterator i
;
304 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
306 if (! build_data_member_initialization (tsi_stmt (i
), vec
))
311 if (TREE_CODE (t
) == CLEANUP_STMT
)
313 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
314 but we can in a constexpr constructor for a non-literal class. Just
315 ignore it; either all the initialization will be constant, in which
316 case the cleanup can't run, or it can't be constexpr.
317 Still recurse into CLEANUP_BODY. */
318 return build_data_member_initialization (CLEANUP_BODY (t
), vec
);
320 if (TREE_CODE (t
) == CONVERT_EXPR
)
321 t
= TREE_OPERAND (t
, 0);
322 if (TREE_CODE (t
) == INIT_EXPR
323 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
324 use what this function builds for cx_check_missing_mem_inits, and
325 assignment in the ctor body doesn't count. */
326 || (cxx_dialect
< cxx14
&& TREE_CODE (t
) == MODIFY_EXPR
))
328 member
= TREE_OPERAND (t
, 0);
329 init
= break_out_target_exprs (TREE_OPERAND (t
, 1));
331 else if (TREE_CODE (t
) == CALL_EXPR
)
333 tree fn
= get_callee_fndecl (t
);
334 if (!fn
|| !DECL_CONSTRUCTOR_P (fn
))
335 /* We're only interested in calls to subobject constructors. */
337 member
= CALL_EXPR_ARG (t
, 0);
338 /* We don't use build_cplus_new here because it complains about
339 abstract bases. Leaving the call unwrapped means that it has the
340 wrong type, but cxx_eval_constant_expression doesn't care. */
341 init
= break_out_target_exprs (t
);
343 else if (TREE_CODE (t
) == BIND_EXPR
)
344 return build_data_member_initialization (BIND_EXPR_BODY (t
), vec
);
346 /* Don't add anything else to the CONSTRUCTOR. */
348 if (INDIRECT_REF_P (member
))
349 member
= TREE_OPERAND (member
, 0);
350 if (TREE_CODE (member
) == NOP_EXPR
)
354 if (TREE_CODE (op
) == ADDR_EXPR
)
356 gcc_assert (same_type_ignoring_top_level_qualifiers_p
357 (TREE_TYPE (TREE_TYPE (op
)),
358 TREE_TYPE (TREE_TYPE (member
))));
359 /* Initializing a cv-qualified member; we need to look through
363 else if (op
== current_class_ptr
364 && (same_type_ignoring_top_level_qualifiers_p
365 (TREE_TYPE (TREE_TYPE (member
)),
366 current_class_type
)))
367 /* Delegating constructor. */
371 /* This is an initializer for an empty base; keep it for now so
372 we can check it in cxx_eval_bare_aggregate. */
373 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member
))));
376 if (TREE_CODE (member
) == ADDR_EXPR
)
377 member
= TREE_OPERAND (member
, 0);
378 if (TREE_CODE (member
) == COMPONENT_REF
)
380 tree aggr
= TREE_OPERAND (member
, 0);
381 if (TREE_CODE (aggr
) != COMPONENT_REF
)
382 /* Normal member initialization. */
383 member
= TREE_OPERAND (member
, 1);
384 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr
)))
385 /* Initializing a member of an anonymous union. */
386 return build_anon_member_initialization (member
, init
, vec
);
388 /* We're initializing a vtable pointer in a base. Leave it as
389 COMPONENT_REF so we remember the path to get to the vfield. */
390 gcc_assert (TREE_TYPE (member
) == vtbl_ptr_type_node
);
393 CONSTRUCTOR_APPEND_ELT (*vec
, member
, init
);
397 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
398 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
399 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
402 check_constexpr_bind_expr_vars (tree t
)
404 gcc_assert (TREE_CODE (t
) == BIND_EXPR
);
406 for (tree var
= BIND_EXPR_VARS (t
); var
; var
= DECL_CHAIN (var
))
407 if (TREE_CODE (var
) == TYPE_DECL
408 && DECL_IMPLICIT_TYPEDEF_P (var
)
409 && !LAMBDA_TYPE_P (TREE_TYPE (var
)))
414 /* Subroutine of check_constexpr_ctor_body. */
417 check_constexpr_ctor_body_1 (tree last
, tree list
)
419 switch (TREE_CODE (list
))
422 if (TREE_CODE (DECL_EXPR_DECL (list
)) == USING_DECL
)
426 case CLEANUP_POINT_EXPR
:
427 return check_constexpr_ctor_body (last
, TREE_OPERAND (list
, 0),
431 if (!check_constexpr_bind_expr_vars (list
)
432 || !check_constexpr_ctor_body (last
, BIND_EXPR_BODY (list
),
446 /* Make sure that there are no statements after LAST in the constructor
447 body represented by LIST. */
450 check_constexpr_ctor_body (tree last
, tree list
, bool complain
)
452 /* C++14 doesn't require a constexpr ctor to have an empty body. */
453 if (cxx_dialect
>= cxx14
)
457 if (TREE_CODE (list
) == STATEMENT_LIST
)
459 tree_stmt_iterator i
= tsi_last (list
);
460 for (; !tsi_end_p (i
); tsi_prev (&i
))
462 tree t
= tsi_stmt (i
);
465 if (!check_constexpr_ctor_body_1 (last
, t
))
472 else if (list
!= last
473 && !check_constexpr_ctor_body_1 (last
, list
))
478 error ("constexpr constructor does not have empty body");
479 DECL_DECLARED_CONSTEXPR_P (current_function_decl
) = false;
484 /* V is a vector of constructor elements built up for the base and member
485 initializers of a constructor for TYPE. They need to be in increasing
486 offset order, which they might not be yet if TYPE has a primary base
487 which is not first in the base-clause or a vptr and at least one base
488 all of which are non-primary. */
490 static vec
<constructor_elt
, va_gc
> *
491 sort_constexpr_mem_initializers (tree type
, vec
<constructor_elt
, va_gc
> *v
)
493 tree pri
= CLASSTYPE_PRIMARY_BINFO (type
);
499 field_type
= BINFO_TYPE (pri
);
500 else if (TYPE_CONTAINS_VPTR_P (type
))
501 field_type
= vtbl_ptr_type_node
;
505 /* Find the element for the primary base or vptr and move it to the
506 beginning of the vec. */
507 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
508 if (TREE_TYPE (ce
->index
) == field_type
)
511 if (i
> 0 && i
< vec_safe_length (v
))
513 vec
<constructor_elt
, va_gc
> &vref
= *v
;
514 constructor_elt elt
= vref
[i
];
523 /* Build compile-time evalable representations of member-initializer list
524 for a constexpr constructor. */
527 build_constexpr_constructor_member_initializers (tree type
, tree body
)
529 vec
<constructor_elt
, va_gc
> *vec
= NULL
;
532 switch (TREE_CODE (body
))
534 case MUST_NOT_THROW_EXPR
:
536 body
= TREE_OPERAND (body
, 0);
540 for (tree_stmt_iterator i
= tsi_start (body
);
541 !tsi_end_p (i
); tsi_next (&i
))
544 if (TREE_CODE (body
) == BIND_EXPR
)
550 body
= BIND_EXPR_BODY (body
);
557 if (TREE_CODE (body
) == CLEANUP_POINT_EXPR
)
559 body
= TREE_OPERAND (body
, 0);
560 if (TREE_CODE (body
) == EXPR_STMT
)
561 body
= TREE_OPERAND (body
, 0);
562 if (TREE_CODE (body
) == INIT_EXPR
563 && (same_type_ignoring_top_level_qualifiers_p
564 (TREE_TYPE (TREE_OPERAND (body
, 0)),
565 current_class_type
)))
568 return TREE_OPERAND (body
, 1);
570 ok
= build_data_member_initialization (body
, &vec
);
572 else if (TREE_CODE (body
) == STATEMENT_LIST
)
574 tree_stmt_iterator i
;
575 for (i
= tsi_start (body
); !tsi_end_p (i
); tsi_next (&i
))
577 ok
= build_data_member_initialization (tsi_stmt (i
), &vec
);
582 else if (TREE_CODE (body
) == TRY_BLOCK
)
584 error ("body of %<constexpr%> constructor cannot be "
585 "a function-try-block");
586 return error_mark_node
;
588 else if (EXPR_P (body
))
589 ok
= build_data_member_initialization (body
, &vec
);
591 gcc_assert (errorcount
> 0);
594 if (vec_safe_length (vec
) > 0)
596 /* In a delegating constructor, return the target. */
597 constructor_elt
*ce
= &(*vec
)[0];
598 if (ce
->index
== current_class_ptr
)
605 vec
= sort_constexpr_mem_initializers (type
, vec
);
606 return build_constructor (type
, vec
);
609 return error_mark_node
;
612 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
613 declared to be constexpr, or a sub-statement thereof. Returns the
614 return value if suitable, error_mark_node for a statement not allowed in
615 a constexpr function, or NULL_TREE if no return value was found. */
618 constexpr_fn_retval (tree body
)
620 switch (TREE_CODE (body
))
624 tree_stmt_iterator i
;
625 tree expr
= NULL_TREE
;
626 for (i
= tsi_start (body
); !tsi_end_p (i
); tsi_next (&i
))
628 tree s
= constexpr_fn_retval (tsi_stmt (i
));
629 if (s
== error_mark_node
)
630 return error_mark_node
;
631 else if (s
== NULL_TREE
)
632 /* Keep iterating. */;
634 /* Multiple return statements. */
635 return error_mark_node
;
643 return break_out_target_exprs (TREE_OPERAND (body
, 0));
647 tree decl
= DECL_EXPR_DECL (body
);
648 if (TREE_CODE (decl
) == USING_DECL
649 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
650 || DECL_ARTIFICIAL (decl
))
652 return error_mark_node
;
655 case CLEANUP_POINT_EXPR
:
656 return constexpr_fn_retval (TREE_OPERAND (body
, 0));
659 if (!check_constexpr_bind_expr_vars (body
))
660 return error_mark_node
;
661 return constexpr_fn_retval (BIND_EXPR_BODY (body
));
667 return error_mark_node
;
671 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
672 FUN; do the necessary transformations to turn it into a single expression
673 that we can store in the hash table. */
676 massage_constexpr_body (tree fun
, tree body
)
678 if (DECL_CONSTRUCTOR_P (fun
))
679 body
= build_constexpr_constructor_member_initializers
680 (DECL_CONTEXT (fun
), body
);
681 else if (cxx_dialect
< cxx14
)
683 if (TREE_CODE (body
) == EH_SPEC_BLOCK
)
684 body
= EH_SPEC_STMTS (body
);
685 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
686 body
= TREE_OPERAND (body
, 0);
687 body
= constexpr_fn_retval (body
);
692 /* FUN is a constexpr constructor with massaged body BODY. Return true
693 if some bases/fields are uninitialized, and complain if COMPLAIN. */
696 cx_check_missing_mem_inits (tree fun
, tree body
, bool complain
)
703 if (TREE_CODE (body
) != CONSTRUCTOR
)
706 nelts
= CONSTRUCTOR_NELTS (body
);
707 ctype
= DECL_CONTEXT (fun
);
708 field
= TYPE_FIELDS (ctype
);
710 if (TREE_CODE (ctype
) == UNION_TYPE
)
712 if (nelts
== 0 && next_initializable_field (field
))
715 error ("%<constexpr%> constructor for union %qT must "
716 "initialize exactly one non-static data member", ctype
);
723 for (i
= 0; i
<= nelts
; ++i
)
730 index
= CONSTRUCTOR_ELT (body
, i
)->index
;
731 /* Skip base and vtable inits. */
732 if (TREE_CODE (index
) != FIELD_DECL
733 || DECL_ARTIFICIAL (index
))
736 for (; field
!= index
; field
= DECL_CHAIN (field
))
739 if (TREE_CODE (field
) != FIELD_DECL
740 || (DECL_C_BIT_FIELD (field
) && !DECL_NAME (field
))
741 || DECL_ARTIFICIAL (field
))
743 ftype
= strip_array_types (TREE_TYPE (field
));
744 if (type_has_constexpr_default_constructor (ftype
))
746 /* It's OK to skip a member with a trivial constexpr ctor.
747 A constexpr ctor that isn't trivial should have been
749 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype
)
755 error ("member %qD must be initialized by mem-initializer "
756 "in %<constexpr%> constructor", field
);
757 inform (DECL_SOURCE_LOCATION (field
), "declared here");
760 if (field
== NULL_TREE
)
762 field
= DECL_CHAIN (field
);
768 /* We are processing the definition of the constexpr function FUN.
769 Check that its BODY fulfills the propriate requirements and
770 enter it in the constexpr function definition table.
771 For constructor BODY is actually the TREE_LIST of the
772 member-initializer list. */
775 register_constexpr_fundef (tree fun
, tree body
)
777 constexpr_fundef entry
;
778 constexpr_fundef
**slot
;
780 if (!is_valid_constexpr_fn (fun
, !DECL_GENERATED_P (fun
)))
783 tree massaged
= massage_constexpr_body (fun
, body
);
784 if (massaged
== NULL_TREE
|| massaged
== error_mark_node
)
786 if (!DECL_CONSTRUCTOR_P (fun
))
787 error ("body of constexpr function %qD not a return-statement", fun
);
791 if (!potential_rvalue_constant_expression (massaged
))
793 if (!DECL_GENERATED_P (fun
))
794 require_potential_rvalue_constant_expression (massaged
);
798 if (DECL_CONSTRUCTOR_P (fun
)
799 && cx_check_missing_mem_inits (fun
, massaged
, !DECL_GENERATED_P (fun
)))
802 /* Create the constexpr function table if necessary. */
803 if (constexpr_fundef_table
== NULL
)
804 constexpr_fundef_table
805 = hash_table
<constexpr_fundef_hasher
>::create_ggc (101);
809 slot
= constexpr_fundef_table
->find_slot (&entry
, INSERT
);
811 gcc_assert (*slot
== NULL
);
812 *slot
= ggc_alloc
<constexpr_fundef
> ();
818 /* FUN is a non-constexpr function called in a context that requires a
819 constant expression. If it comes from a constexpr template, explain why
820 the instantiation isn't constexpr. */
823 explain_invalid_constexpr_fn (tree fun
)
825 static hash_set
<tree
> *diagnosed
;
828 /* Only diagnose defaulted functions or instantiations. */
829 if (!DECL_DEFAULTED_FN (fun
)
830 && !is_instantiation_of_constexpr (fun
))
832 if (diagnosed
== NULL
)
833 diagnosed
= new hash_set
<tree
>;
834 if (diagnosed
->add (fun
))
835 /* Already explained. */
838 save_loc
= input_location
;
839 input_location
= DECL_SOURCE_LOCATION (fun
);
840 inform (input_location
,
841 "%qD is not usable as a constexpr function because:", fun
);
842 /* First check the declaration. */
843 if (is_valid_constexpr_fn (fun
, true))
845 /* Then if it's OK, the body. */
846 if (!DECL_DECLARED_CONSTEXPR_P (fun
))
847 explain_implicit_non_constexpr (fun
);
850 body
= massage_constexpr_body (fun
, DECL_SAVED_TREE (fun
));
851 require_potential_rvalue_constant_expression (body
);
852 if (DECL_CONSTRUCTOR_P (fun
))
853 cx_check_missing_mem_inits (fun
, body
, true);
856 input_location
= save_loc
;
859 /* Objects of this type represent calls to constexpr functions
860 along with the bindings of parameters to their arguments, for
861 the purpose of compile time evaluation. */
863 struct GTY((for_user
)) constexpr_call
{
864 /* Description of the constexpr function definition. */
865 constexpr_fundef
*fundef
;
866 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
867 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
868 Note: This arrangement is made to accommodate the use of
869 iterative_hash_template_arg (see pt.c). If you change this
870 representation, also change the hash calculation in
871 cxx_eval_call_expression. */
873 /* Result of the call.
874 NULL means the call is being evaluated.
875 error_mark_node means that the evaluation was erroneous;
876 otherwise, the actuall value of the call. */
878 /* The hash of this call; we remember it here to avoid having to
879 recalculate it when expanding the hash table. */
883 struct constexpr_call_hasher
: ggc_ptr_hash
<constexpr_call
>
885 static hashval_t
hash (constexpr_call
*);
886 static bool equal (constexpr_call
*, constexpr_call
*);
889 /* The constexpr expansion context. CALL is the current function
890 expansion, CTOR is the current aggregate initializer, OBJECT is the
891 object being initialized by CTOR, either a VAR_DECL or a _REF. VALUES
892 is a map of values of variables initialized within the expression. */
894 struct constexpr_ctx
{
895 /* The innermost call we're evaluating. */
896 constexpr_call
*call
;
897 /* Values for any temporaries or local variables within the
898 constant-expression. */
899 hash_map
<tree
,tree
> *values
;
900 /* SAVE_EXPRs that we've seen within the current LOOP_EXPR. NULL if we
901 aren't inside a loop. */
902 hash_set
<tree
> *save_exprs
;
903 /* The CONSTRUCTOR we're currently building up for an aggregate
906 /* The object we're building the CONSTRUCTOR for. */
908 /* Whether we should error on a non-constant expression or fail quietly. */
910 /* Whether we are strictly conforming to constant expression rules or
911 trying harder to get a constant value. */
915 /* A table of all constexpr calls that have been evaluated by the
916 compiler in this translation unit. */
918 static GTY (()) hash_table
<constexpr_call_hasher
> *constexpr_call_table
;
920 static tree
cxx_eval_constant_expression (const constexpr_ctx
*, tree
,
921 bool, bool *, bool *, tree
* = NULL
);
923 /* Compute a hash value for a constexpr call representation. */
926 constexpr_call_hasher::hash (constexpr_call
*info
)
931 /* Return true if the objects pointed to by P and Q represent calls
932 to the same constexpr function with the same arguments.
933 Otherwise, return false. */
936 constexpr_call_hasher::equal (constexpr_call
*lhs
, constexpr_call
*rhs
)
942 if (!constexpr_fundef_hasher::equal (lhs
->fundef
, rhs
->fundef
))
944 lhs_bindings
= lhs
->bindings
;
945 rhs_bindings
= rhs
->bindings
;
946 while (lhs_bindings
!= NULL
&& rhs_bindings
!= NULL
)
948 tree lhs_arg
= TREE_VALUE (lhs_bindings
);
949 tree rhs_arg
= TREE_VALUE (rhs_bindings
);
950 gcc_assert (TREE_TYPE (lhs_arg
) == TREE_TYPE (rhs_arg
));
951 if (!cp_tree_equal (lhs_arg
, rhs_arg
))
953 lhs_bindings
= TREE_CHAIN (lhs_bindings
);
954 rhs_bindings
= TREE_CHAIN (rhs_bindings
);
956 return lhs_bindings
== rhs_bindings
;
959 /* Initialize the constexpr call table, if needed. */
962 maybe_initialize_constexpr_call_table (void)
964 if (constexpr_call_table
== NULL
)
965 constexpr_call_table
= hash_table
<constexpr_call_hasher
>::create_ggc (101);
968 /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
969 a function happens to get called recursively, we unshare the callee
970 function's body and evaluate this unshared copy instead of evaluating the
973 FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
974 copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
975 that's keyed off of the original FUNCTION_DECL and whose value is a
976 TREE_LIST of this function's unused copies awaiting reuse.
978 This is not GC-deletable to avoid GC affecting UID generation. */
980 static GTY(()) hash_map
<tree
, tree
> *fundef_copies_table
;
982 /* Initialize FUNDEF_COPIES_TABLE if it's not initialized. */
985 maybe_initialize_fundef_copies_table ()
987 if (fundef_copies_table
== NULL
)
988 fundef_copies_table
= hash_map
<tree
,tree
>::create_ggc (101);
991 /* Reuse a copy or create a new unshared copy of the function FUN.
992 Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
993 is parms, TYPE is result. */
996 get_fundef_copy (tree fun
)
998 maybe_initialize_fundef_copies_table ();
1002 tree
*slot
= &fundef_copies_table
->get_or_insert (fun
, &existed
);
1006 /* There is no cached function available, or in use. We can use
1007 the function directly. That the slot is now created records
1008 that this function is now in use. */
1009 copy
= build_tree_list (DECL_SAVED_TREE (fun
), DECL_ARGUMENTS (fun
));
1010 TREE_TYPE (copy
) = DECL_RESULT (fun
);
1012 else if (*slot
== NULL_TREE
)
1014 /* We've already used the function itself, so make a copy. */
1015 copy
= build_tree_list (NULL
, NULL
);
1016 TREE_PURPOSE (copy
) = copy_fn (fun
, TREE_VALUE (copy
), TREE_TYPE (copy
));
1020 /* We have a cached function available. */
1022 *slot
= TREE_CHAIN (copy
);
1028 /* Save the copy COPY of function FUN for later reuse by
1029 get_fundef_copy(). By construction, there will always be an entry
1033 save_fundef_copy (tree fun
, tree copy
)
1035 tree
*slot
= fundef_copies_table
->get (fun
);
1036 TREE_CHAIN (copy
) = *slot
;
1040 /* We have an expression tree T that represents a call, either CALL_EXPR
1041 or AGGR_INIT_EXPR. If the call is lexically to a named function,
1042 retrun the _DECL for that function. */
1045 get_function_named_in_call (tree t
)
1047 tree fun
= cp_get_callee (t
);
1048 if (fun
&& TREE_CODE (fun
) == ADDR_EXPR
1049 && TREE_CODE (TREE_OPERAND (fun
, 0)) == FUNCTION_DECL
)
1050 fun
= TREE_OPERAND (fun
, 0);
1054 /* We have an expression tree T that represents a call, either CALL_EXPR
1055 or AGGR_INIT_EXPR. Return the Nth argument. */
1058 get_nth_callarg (tree t
, int n
)
1060 switch (TREE_CODE (t
))
1063 return CALL_EXPR_ARG (t
, n
);
1065 case AGGR_INIT_EXPR
:
1066 return AGGR_INIT_EXPR_ARG (t
, n
);
1074 /* Attempt to evaluate T which represents a call to a builtin function.
1075 We assume here that all builtin functions evaluate to scalar types
1076 represented by _CST nodes. */
1079 cxx_eval_builtin_function_call (const constexpr_ctx
*ctx
, tree t
, tree fun
,
1081 bool *non_constant_p
, bool *overflow_p
)
1083 const int nargs
= call_expr_nargs (t
);
1084 tree
*args
= (tree
*) alloca (nargs
* sizeof (tree
));
1088 /* Don't fold __builtin_constant_p within a constexpr function. */
1089 bool bi_const_p
= (DECL_FUNCTION_CODE (fun
) == BUILT_IN_CONSTANT_P
);
1092 && current_function_decl
1093 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
1095 *non_constant_p
= true;
1099 /* Be permissive for arguments to built-ins; __builtin_constant_p should
1100 return constant false for a non-constant argument. */
1101 constexpr_ctx new_ctx
= *ctx
;
1102 new_ctx
.quiet
= true;
1103 bool dummy1
= false, dummy2
= false;
1104 for (i
= 0; i
< nargs
; ++i
)
1106 args
[i
] = cxx_eval_constant_expression (&new_ctx
, CALL_EXPR_ARG (t
, i
),
1107 lval
, &dummy1
, &dummy2
);
1109 /* For __built_in_constant_p, fold all expressions with constant values
1110 even if they aren't C++ constant-expressions. */
1111 args
[i
] = cp_fully_fold (args
[i
]);
1114 bool save_ffbcp
= force_folding_builtin_constant_p
;
1115 force_folding_builtin_constant_p
= true;
1116 new_call
= fold_build_call_array_loc (EXPR_LOCATION (t
), TREE_TYPE (t
),
1117 CALL_EXPR_FN (t
), nargs
, args
);
1118 /* Fold away the NOP_EXPR from fold_builtin_n. */
1119 new_call
= fold (new_call
);
1120 force_folding_builtin_constant_p
= save_ffbcp
;
1121 VERIFY_CONSTANT (new_call
);
1125 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1126 the type of the value to match. */
1129 adjust_temp_type (tree type
, tree temp
)
1131 if (TREE_TYPE (temp
) == type
)
1133 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1134 if (TREE_CODE (temp
) == CONSTRUCTOR
)
1135 return build_constructor (type
, CONSTRUCTOR_ELTS (temp
));
1136 gcc_assert (scalarish_type_p (type
));
1137 return cp_fold_convert (type
, temp
);
1140 /* Callback for walk_tree used by unshare_constructor. */
1143 find_constructor (tree
*tp
, int *walk_subtrees
, void *)
1147 if (TREE_CODE (*tp
) == CONSTRUCTOR
)
1152 /* If T is a CONSTRUCTOR or an expression that has a CONSTRUCTOR node as a
1153 subexpression, return an unshared copy of T. Otherwise return T. */
1156 unshare_constructor (tree t
)
1158 tree ctor
= walk_tree (&t
, find_constructor
, NULL
, NULL
);
1159 if (ctor
!= NULL_TREE
)
1160 return unshare_expr (t
);
1164 /* Subroutine of cxx_eval_call_expression.
1165 We are processing a call expression (either CALL_EXPR or
1166 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1167 all arguments and bind their values to correspondings
1168 parameters, making up the NEW_CALL context. */
1171 cxx_bind_parameters_in_call (const constexpr_ctx
*ctx
, tree t
,
1172 constexpr_call
*new_call
,
1173 bool *non_constant_p
, bool *overflow_p
,
1174 bool *non_constant_args
)
1176 const int nargs
= call_expr_nargs (t
);
1177 tree fun
= new_call
->fundef
->decl
;
1178 tree parms
= DECL_ARGUMENTS (fun
);
1180 tree
*p
= &new_call
->bindings
;
1181 for (i
= 0; i
< nargs
; ++i
)
1184 tree type
= parms
? TREE_TYPE (parms
) : void_type_node
;
1185 x
= get_nth_callarg (t
, i
);
1186 /* For member function, the first argument is a pointer to the implied
1187 object. For a constructor, it might still be a dummy object, in
1188 which case we get the real argument from ctx. */
1189 if (i
== 0 && DECL_CONSTRUCTOR_P (fun
)
1190 && is_dummy_object (x
))
1193 x
= cp_build_addr_expr (x
, tf_warning_or_error
);
1196 arg
= cxx_eval_constant_expression (ctx
, x
, lval
,
1197 non_constant_p
, overflow_p
);
1198 /* Don't VERIFY_CONSTANT here. */
1199 if (*non_constant_p
&& ctx
->quiet
)
1201 /* Just discard ellipsis args after checking their constantitude. */
1205 if (!*non_constant_p
)
1207 /* Make sure the binding has the same type as the parm. But
1208 only for constant args. */
1209 if (TREE_CODE (type
) != REFERENCE_TYPE
)
1210 arg
= adjust_temp_type (type
, arg
);
1211 if (!TREE_CONSTANT (arg
))
1212 *non_constant_args
= true;
1213 *p
= build_tree_list (parms
, arg
);
1214 p
= &TREE_CHAIN (*p
);
1216 parms
= TREE_CHAIN (parms
);
1220 /* Variables and functions to manage constexpr call expansion context.
1221 These do not need to be marked for PCH or GC. */
1223 /* FIXME remember and print actual constant arguments. */
1224 static vec
<tree
> call_stack
= vNULL
;
1225 static int call_stack_tick
;
1226 static int last_cx_error_tick
;
1229 push_cx_call_context (tree call
)
1232 if (!EXPR_HAS_LOCATION (call
))
1233 SET_EXPR_LOCATION (call
, input_location
);
1234 call_stack
.safe_push (call
);
1235 if (call_stack
.length () > (unsigned) max_constexpr_depth
)
1241 pop_cx_call_context (void)
1248 cx_error_context (void)
1250 vec
<tree
> r
= vNULL
;
1251 if (call_stack_tick
!= last_cx_error_tick
1252 && !call_stack
.is_empty ())
1254 last_cx_error_tick
= call_stack_tick
;
1258 /* Subroutine of cxx_eval_constant_expression.
1259 Evaluate the call expression tree T in the context of OLD_CALL expression
1263 cxx_eval_call_expression (const constexpr_ctx
*ctx
, tree t
,
1265 bool *non_constant_p
, bool *overflow_p
)
1267 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
1268 tree fun
= get_function_named_in_call (t
);
1269 constexpr_call new_call
= { NULL
, NULL
, NULL
, 0 };
1272 if (fun
== NULL_TREE
)
1273 switch (CALL_EXPR_IFN (t
))
1275 case IFN_UBSAN_NULL
:
1276 case IFN_UBSAN_BOUNDS
:
1277 case IFN_UBSAN_VPTR
:
1281 error_at (loc
, "call to internal function");
1282 *non_constant_p
= true;
1286 if (TREE_CODE (fun
) != FUNCTION_DECL
)
1288 /* Might be a constexpr function pointer. */
1289 fun
= cxx_eval_constant_expression (ctx
, fun
,
1290 /*lval*/false, non_constant_p
,
1293 if (TREE_CODE (fun
) == ADDR_EXPR
)
1294 fun
= TREE_OPERAND (fun
, 0);
1296 if (TREE_CODE (fun
) != FUNCTION_DECL
)
1298 if (!ctx
->quiet
&& !*non_constant_p
)
1299 error_at (loc
, "expression %qE does not designate a constexpr "
1301 *non_constant_p
= true;
1304 if (DECL_CLONED_FUNCTION_P (fun
))
1305 fun
= DECL_CLONED_FUNCTION (fun
);
1307 if (is_ubsan_builtin_p (fun
))
1310 if (is_builtin_fn (fun
))
1311 return cxx_eval_builtin_function_call (ctx
, t
, fun
,
1312 lval
, non_constant_p
, overflow_p
);
1313 if (!DECL_DECLARED_CONSTEXPR_P (fun
))
1317 error_at (loc
, "call to non-constexpr function %qD", fun
);
1318 explain_invalid_constexpr_fn (fun
);
1320 *non_constant_p
= true;
1324 constexpr_ctx new_ctx
= *ctx
;
1325 if (DECL_CONSTRUCTOR_P (fun
) && !ctx
->object
1326 && TREE_CODE (t
) == AGGR_INIT_EXPR
)
1328 /* We want to have an initialization target for an AGGR_INIT_EXPR.
1329 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
1330 new_ctx
.object
= AGGR_INIT_EXPR_SLOT (t
);
1331 tree ctor
= new_ctx
.ctor
= build_constructor (DECL_CONTEXT (fun
), NULL
);
1332 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctor
) = true;
1333 ctx
->values
->put (new_ctx
.object
, ctor
);
1337 /* Shortcut trivial constructor/op=. */
1338 if (trivial_fn_p (fun
))
1340 tree init
= NULL_TREE
;
1341 if (call_expr_nargs (t
) == 2)
1342 init
= convert_from_reference (get_nth_callarg (t
, 1));
1343 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
1344 && AGGR_INIT_ZERO_FIRST (t
))
1345 init
= build_zero_init (DECL_CONTEXT (fun
), NULL_TREE
, false);
1348 tree op
= get_nth_callarg (t
, 0);
1349 if (is_dummy_object (op
))
1352 op
= build1 (INDIRECT_REF
, TREE_TYPE (TREE_TYPE (op
)), op
);
1353 tree set
= build2 (MODIFY_EXPR
, TREE_TYPE (op
), op
, init
);
1354 return cxx_eval_constant_expression (ctx
, set
, lval
,
1355 non_constant_p
, overflow_p
);
1359 /* We can't defer instantiating the function any longer. */
1360 if (!DECL_INITIAL (fun
)
1361 && DECL_TEMPLOID_INSTANTIATION (fun
))
1364 instantiate_decl (fun
, /*defer_ok*/false, /*expl_inst*/false);
1368 /* If in direct recursive call, optimize definition search. */
1369 if (ctx
&& ctx
->call
&& ctx
->call
->fundef
->decl
== fun
)
1370 new_call
.fundef
= ctx
->call
->fundef
;
1373 new_call
.fundef
= retrieve_constexpr_fundef (fun
);
1374 if (new_call
.fundef
== NULL
|| new_call
.fundef
->body
== NULL
1375 || fun
== current_function_decl
)
1379 /* We need to check for current_function_decl here in case we're
1380 being called during cp_fold_function, because at that point
1381 DECL_INITIAL is set properly and we have a fundef but we
1382 haven't lowered invisirefs yet (c++/70344). */
1383 if (DECL_INITIAL (fun
) == error_mark_node
1384 || fun
== current_function_decl
)
1385 error_at (loc
, "%qD called in a constant expression before its "
1386 "definition is complete", fun
);
1387 else if (DECL_INITIAL (fun
))
1389 /* The definition of fun was somehow unsuitable. */
1390 error_at (loc
, "%qD called in a constant expression", fun
);
1391 explain_invalid_constexpr_fn (fun
);
1394 error_at (loc
, "%qD used before its definition", fun
);
1396 *non_constant_p
= true;
1401 bool non_constant_args
= false;
1402 cxx_bind_parameters_in_call (ctx
, t
, &new_call
,
1403 non_constant_p
, overflow_p
, &non_constant_args
);
1404 if (*non_constant_p
)
1407 depth_ok
= push_cx_call_context (t
);
1409 tree result
= NULL_TREE
;
1411 constexpr_call
*entry
= NULL
;
1412 if (depth_ok
&& !non_constant_args
)
1414 new_call
.hash
= iterative_hash_template_arg
1415 (new_call
.bindings
, constexpr_fundef_hasher::hash (new_call
.fundef
));
1417 /* If we have seen this call before, we are done. */
1418 maybe_initialize_constexpr_call_table ();
1419 constexpr_call
**slot
1420 = constexpr_call_table
->find_slot (&new_call
, INSERT
);
1424 /* We need to keep a pointer to the entry, not just the slot, as the
1425 slot can move in the call to cxx_eval_builtin_function_call. */
1426 *slot
= entry
= ggc_alloc
<constexpr_call
> ();
1429 /* Calls that are in progress have their result set to NULL,
1430 so that we can detect circular dependencies. */
1431 else if (entry
->result
== NULL
)
1434 error ("call has circular dependency");
1435 *non_constant_p
= true;
1436 entry
->result
= result
= error_mark_node
;
1439 result
= entry
->result
;
1445 error ("constexpr evaluation depth exceeds maximum of %d (use "
1446 "-fconstexpr-depth= to increase the maximum)",
1447 max_constexpr_depth
);
1448 *non_constant_p
= true;
1449 result
= error_mark_node
;
1453 if (!result
|| result
== error_mark_node
)
1455 gcc_assert (DECL_SAVED_TREE (fun
));
1456 tree body
, parms
, res
;
1458 /* Reuse or create a new unshared copy of this function's body. */
1459 tree copy
= get_fundef_copy (fun
);
1460 body
= TREE_PURPOSE (copy
);
1461 parms
= TREE_VALUE (copy
);
1462 res
= TREE_TYPE (copy
);
1464 /* Associate the bindings with the remapped parms. */
1465 tree bound
= new_call
.bindings
;
1466 tree remapped
= parms
;
1469 tree oparm
= TREE_PURPOSE (bound
);
1470 tree arg
= TREE_VALUE (bound
);
1471 gcc_assert (DECL_NAME (remapped
) == DECL_NAME (oparm
));
1472 /* Don't share a CONSTRUCTOR that might be changed. */
1473 arg
= unshare_constructor (arg
);
1474 ctx
->values
->put (remapped
, arg
);
1475 bound
= TREE_CHAIN (bound
);
1476 remapped
= DECL_CHAIN (remapped
);
1478 /* Add the RESULT_DECL to the values map, too. */
1479 tree slot
= NULL_TREE
;
1480 if (DECL_BY_REFERENCE (res
))
1482 slot
= AGGR_INIT_EXPR_SLOT (t
);
1483 tree addr
= build_address (slot
);
1484 addr
= build_nop (TREE_TYPE (res
), addr
);
1485 ctx
->values
->put (res
, addr
);
1486 ctx
->values
->put (slot
, NULL_TREE
);
1489 ctx
->values
->put (res
, NULL_TREE
);
1491 /* Track the callee's evaluated SAVE_EXPRs so that we can forget
1492 their values after the call. */
1493 constexpr_ctx ctx_with_save_exprs
= *ctx
;
1494 hash_set
<tree
> save_exprs
;
1495 ctx_with_save_exprs
.save_exprs
= &save_exprs
;
1497 tree jump_target
= NULL_TREE
;
1498 cxx_eval_constant_expression (&ctx_with_save_exprs
, body
,
1499 lval
, non_constant_p
, overflow_p
,
1502 if (DECL_CONSTRUCTOR_P (fun
))
1503 /* This can be null for a subobject constructor call, in
1504 which case what we care about is the initialization
1505 side-effects rather than the value. We could get at the
1506 value by evaluating *this, but we don't bother; there's
1507 no need to put such a call in the hash table. */
1508 result
= lval
? ctx
->object
: ctx
->ctor
;
1509 else if (VOID_TYPE_P (TREE_TYPE (res
)))
1513 result
= *ctx
->values
->get (slot
? slot
: res
);
1514 if (result
== NULL_TREE
&& !*non_constant_p
)
1517 error ("constexpr call flows off the end "
1519 *non_constant_p
= true;
1523 /* Forget the saved values of the callee's SAVE_EXPRs. */
1524 for (hash_set
<tree
>::iterator iter
= save_exprs
.begin();
1525 iter
!= save_exprs
.end(); ++iter
)
1526 ctx_with_save_exprs
.values
->remove (*iter
);
1528 /* Remove the parms/result from the values map. Is it worth
1529 bothering to do this when the map itself is only live for
1530 one constexpr evaluation? If so, maybe also clear out
1531 other vars from call, maybe in BIND_EXPR handling? */
1532 ctx
->values
->remove (res
);
1534 ctx
->values
->remove (slot
);
1535 for (tree parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
1536 ctx
->values
->remove (parm
);
1538 /* Make the unshared function copy we used available for re-use. */
1539 save_fundef_copy (fun
, copy
);
1542 if (result
== error_mark_node
)
1543 *non_constant_p
= true;
1544 if (*non_constant_p
|| *overflow_p
)
1545 result
= error_mark_node
;
1549 entry
->result
= result
;
1552 pop_cx_call_context ();
1553 return unshare_constructor (result
);
1556 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
1559 reduced_constant_expression_p (tree t
)
1561 switch (TREE_CODE (t
))
1564 /* Even if we can't lower this yet, it's constant. */
1568 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
1569 tree elt
; unsigned HOST_WIDE_INT idx
;
1570 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), idx
, elt
)
1571 if (!reduced_constant_expression_p (elt
))
1576 /* FIXME are we calling this too much? */
1577 return initializer_constant_valid_p (t
, TREE_TYPE (t
)) != NULL_TREE
;
1581 /* Some expressions may have constant operands but are not constant
1582 themselves, such as 1/0. Call this function (or rather, the macro
1583 following it) to check for that condition.
1585 We only call this in places that require an arithmetic constant, not in
1586 places where we might have a non-constant expression that can be a
1587 component of a constant expression, such as the address of a constexpr
1588 variable that might be dereferenced later. */
1591 verify_constant (tree t
, bool allow_non_constant
, bool *non_constant_p
,
1594 if (!*non_constant_p
&& !reduced_constant_expression_p (t
))
1596 if (!allow_non_constant
)
1597 error ("%q+E is not a constant expression", t
);
1598 *non_constant_p
= true;
1600 if (TREE_OVERFLOW_P (t
))
1602 if (!allow_non_constant
)
1604 permerror (input_location
, "overflow in constant expression");
1605 /* If we're being permissive (and are in an enforcing
1606 context), ignore the overflow. */
1607 if (flag_permissive
)
1608 return *non_constant_p
;
1612 return *non_constant_p
;
1615 /* Check whether the shift operation with code CODE and type TYPE on LHS
1616 and RHS is undefined. If it is, give an error with an explanation,
1617 and return true; return false otherwise. */
1620 cxx_eval_check_shift_p (location_t loc
, const constexpr_ctx
*ctx
,
1621 enum tree_code code
, tree type
, tree lhs
, tree rhs
)
1623 if ((code
!= LSHIFT_EXPR
&& code
!= RSHIFT_EXPR
)
1624 || TREE_CODE (lhs
) != INTEGER_CST
1625 || TREE_CODE (rhs
) != INTEGER_CST
)
1628 tree lhstype
= TREE_TYPE (lhs
);
1629 unsigned HOST_WIDE_INT uprec
= TYPE_PRECISION (TREE_TYPE (lhs
));
1631 /* [expr.shift] The behavior is undefined if the right operand
1632 is negative, or greater than or equal to the length in bits
1633 of the promoted left operand. */
1634 if (tree_int_cst_sgn (rhs
) == -1)
1637 permerror (loc
, "right operand of shift expression %q+E is negative",
1638 build2_loc (loc
, code
, type
, lhs
, rhs
));
1639 return (!flag_permissive
|| ctx
->quiet
);
1641 if (compare_tree_int (rhs
, uprec
) >= 0)
1644 permerror (loc
, "right operand of shift expression %q+E is >= than "
1645 "the precision of the left operand",
1646 build2_loc (loc
, code
, type
, lhs
, rhs
));
1647 return (!flag_permissive
|| ctx
->quiet
);
1650 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
1651 if E1 has a signed type and non-negative value, and E1x2^E2 is
1652 representable in the corresponding unsigned type of the result type,
1653 then that value, converted to the result type, is the resulting value;
1654 otherwise, the behavior is undefined. */
1655 if (code
== LSHIFT_EXPR
&& !TYPE_UNSIGNED (lhstype
)
1656 && (cxx_dialect
>= cxx11
))
1658 if (tree_int_cst_sgn (lhs
) == -1)
1662 "left operand of shift expression %q+E is negative",
1663 build2_loc (loc
, code
, type
, lhs
, rhs
));
1664 return (!flag_permissive
|| ctx
->quiet
);
1666 /* For signed x << y the following:
1667 (unsigned) x >> ((prec (lhs) - 1) - y)
1668 if > 1, is undefined. The right-hand side of this formula
1669 is the highest bit of the LHS that can be set (starting from 0),
1670 so that the shift doesn't overflow. We then right-shift the LHS
1671 to see whether any other bit is set making the original shift
1672 undefined -- the result is not representable in the corresponding
1674 tree t
= build_int_cst (unsigned_type_node
, uprec
- 1);
1675 t
= fold_build2 (MINUS_EXPR
, unsigned_type_node
, t
, rhs
);
1676 tree ulhs
= fold_convert (unsigned_type_for (lhstype
), lhs
);
1677 t
= fold_build2 (RSHIFT_EXPR
, TREE_TYPE (ulhs
), ulhs
, t
);
1678 if (tree_int_cst_lt (integer_one_node
, t
))
1681 permerror (loc
, "shift expression %q+E overflows",
1682 build2_loc (loc
, code
, type
, lhs
, rhs
));
1683 return (!flag_permissive
|| ctx
->quiet
);
1689 /* Subroutine of cxx_eval_constant_expression.
1690 Attempt to reduce the unary expression tree T to a compile time value.
1691 If successful, return the value. Otherwise issue a diagnostic
1692 and return error_mark_node. */
1695 cxx_eval_unary_expression (const constexpr_ctx
*ctx
, tree t
,
1697 bool *non_constant_p
, bool *overflow_p
)
1700 tree orig_arg
= TREE_OPERAND (t
, 0);
1701 tree arg
= cxx_eval_constant_expression (ctx
, orig_arg
, /*lval*/false,
1702 non_constant_p
, overflow_p
);
1703 VERIFY_CONSTANT (arg
);
1704 location_t loc
= EXPR_LOCATION (t
);
1705 enum tree_code code
= TREE_CODE (t
);
1706 tree type
= TREE_TYPE (t
);
1707 r
= fold_unary_loc (loc
, code
, type
, arg
);
1710 if (arg
== orig_arg
)
1713 r
= build1_loc (loc
, code
, type
, arg
);
1715 VERIFY_CONSTANT (r
);
1719 /* Subroutine of cxx_eval_constant_expression.
1720 Like cxx_eval_unary_expression, except for binary expressions. */
1723 cxx_eval_binary_expression (const constexpr_ctx
*ctx
, tree t
,
1725 bool *non_constant_p
, bool *overflow_p
)
1728 tree orig_lhs
= TREE_OPERAND (t
, 0);
1729 tree orig_rhs
= TREE_OPERAND (t
, 1);
1731 lhs
= cxx_eval_constant_expression (ctx
, orig_lhs
, /*lval*/false,
1732 non_constant_p
, overflow_p
);
1733 /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
1735 if (*non_constant_p
)
1737 rhs
= cxx_eval_constant_expression (ctx
, orig_rhs
, /*lval*/false,
1738 non_constant_p
, overflow_p
);
1739 if (*non_constant_p
)
1742 location_t loc
= EXPR_LOCATION (t
);
1743 enum tree_code code
= TREE_CODE (t
);
1744 tree type
= TREE_TYPE (t
);
1746 if (code
== EQ_EXPR
|| code
== NE_EXPR
)
1748 bool is_code_eq
= (code
== EQ_EXPR
);
1750 if (TREE_CODE (lhs
) == PTRMEM_CST
1751 && TREE_CODE (rhs
) == PTRMEM_CST
)
1752 r
= constant_boolean_node (cp_tree_equal (lhs
, rhs
) == is_code_eq
,
1754 else if ((TREE_CODE (lhs
) == PTRMEM_CST
1755 || TREE_CODE (rhs
) == PTRMEM_CST
)
1756 && (null_member_pointer_value_p (lhs
)
1757 || null_member_pointer_value_p (rhs
)))
1758 r
= constant_boolean_node (!is_code_eq
, type
);
1762 r
= fold_binary_loc (loc
, code
, type
, lhs
, rhs
);
1766 if (lhs
== orig_lhs
&& rhs
== orig_rhs
)
1769 r
= build2_loc (loc
, code
, type
, lhs
, rhs
);
1771 else if (cxx_eval_check_shift_p (loc
, ctx
, code
, type
, lhs
, rhs
))
1772 *non_constant_p
= true;
1773 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
1774 a local array in a constexpr function. */
1775 bool ptr
= POINTER_TYPE_P (TREE_TYPE (lhs
));
1777 VERIFY_CONSTANT (r
);
1781 /* Subroutine of cxx_eval_constant_expression.
1782 Attempt to evaluate condition expressions. Dead branches are not
1786 cxx_eval_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
1788 bool *non_constant_p
, bool *overflow_p
,
1791 tree val
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
1793 non_constant_p
, overflow_p
);
1794 VERIFY_CONSTANT (val
);
1795 /* Don't VERIFY_CONSTANT the other operands. */
1796 if (integer_zerop (val
))
1797 return cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 2),
1799 non_constant_p
, overflow_p
,
1801 return cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
1803 non_constant_p
, overflow_p
,
1807 /* Returns less than, equal to, or greater than zero if KEY is found to be
1808 less than, to match, or to be greater than the constructor_elt's INDEX. */
1811 array_index_cmp (tree key
, tree index
)
1813 gcc_assert (TREE_CODE (key
) == INTEGER_CST
);
1815 switch (TREE_CODE (index
))
1818 return tree_int_cst_compare (key
, index
);
1821 tree lo
= TREE_OPERAND (index
, 0);
1822 tree hi
= TREE_OPERAND (index
, 1);
1823 if (tree_int_cst_lt (key
, lo
))
1825 else if (tree_int_cst_lt (hi
, key
))
1835 /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
1836 if none. If INSERT is true, insert a matching element rather than fail. */
1838 static HOST_WIDE_INT
1839 find_array_ctor_elt (tree ary
, tree dindex
, bool insert
= false)
1841 if (tree_int_cst_sgn (dindex
) < 0)
1844 unsigned HOST_WIDE_INT i
= tree_to_uhwi (dindex
);
1845 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (ary
);
1846 unsigned HOST_WIDE_INT len
= vec_safe_length (elts
);
1848 unsigned HOST_WIDE_INT end
= len
;
1849 unsigned HOST_WIDE_INT begin
= 0;
1851 /* If the last element of the CONSTRUCTOR has its own index, we can assume
1852 that the same is true of the other elements and index directly. */
1855 tree cindex
= (*elts
)[end
-1].index
;
1856 if (TREE_CODE (cindex
) == INTEGER_CST
1857 && compare_tree_int (cindex
, end
-1) == 0)
1866 /* Otherwise, find a matching index by means of a binary search. */
1867 while (begin
!= end
)
1869 unsigned HOST_WIDE_INT middle
= (begin
+ end
) / 2;
1870 constructor_elt
&elt
= (*elts
)[middle
];
1871 tree idx
= elt
.index
;
1873 int cmp
= array_index_cmp (dindex
, idx
);
1880 if (insert
&& TREE_CODE (idx
) == RANGE_EXPR
)
1882 /* We need to split the range. */
1884 tree lo
= TREE_OPERAND (idx
, 0);
1885 tree hi
= TREE_OPERAND (idx
, 1);
1886 if (tree_int_cst_lt (lo
, dindex
))
1888 /* There are still some lower elts; shorten the range. */
1889 tree new_hi
= int_const_binop (MINUS_EXPR
, dindex
,
1891 if (tree_int_cst_equal (lo
, new_hi
))
1892 /* Only one element left, no longer a range. */
1895 TREE_OPERAND (idx
, 1) = new_hi
;
1896 /* Append the element we want to insert. */
1899 e
.value
= unshare_constructor (elt
.value
);
1900 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
, e
);
1903 /* No lower elts, the range elt is now ours. */
1906 if (tree_int_cst_lt (dindex
, hi
))
1908 /* There are still some higher elts; append a range. */
1909 tree new_lo
= int_const_binop (PLUS_EXPR
, dindex
,
1911 if (tree_int_cst_equal (new_lo
, hi
))
1914 e
.index
= build2 (RANGE_EXPR
, sizetype
, new_lo
, hi
);
1915 e
.value
= unshare_constructor (elt
.value
);
1916 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), middle
+1, e
);
1925 constructor_elt e
= { dindex
, NULL_TREE
};
1926 vec_safe_insert (CONSTRUCTOR_ELTS (ary
), end
, e
);
1933 /* Under the control of CTX, issue a detailed diagnostic for
1934 an out-of-bounds subscript INDEX into the expression ARRAY. */
1937 diag_array_subscript (const constexpr_ctx
*ctx
, tree array
, tree index
)
1941 tree arraytype
= TREE_TYPE (array
);
1943 /* Convert the unsigned array subscript to a signed integer to avoid
1944 printing huge numbers for small negative values. */
1945 tree sidx
= fold_convert (ssizetype
, index
);
1948 error ("array subscript value %qE is outside the bounds "
1949 "of array %qD of type %qT", sidx
, array
, arraytype
);
1950 inform (DECL_SOURCE_LOCATION (array
), "declared here");
1953 error ("array subscript value %qE is outside the bounds "
1954 "of array type %qT", sidx
, arraytype
);
1958 /* Subroutine of cxx_eval_constant_expression.
1959 Attempt to reduce a reference to an array slot. */
1962 cxx_eval_array_reference (const constexpr_ctx
*ctx
, tree t
,
1964 bool *non_constant_p
, bool *overflow_p
)
1966 tree oldary
= TREE_OPERAND (t
, 0);
1967 tree ary
= cxx_eval_constant_expression (ctx
, oldary
,
1969 non_constant_p
, overflow_p
);
1973 unsigned len
, elem_nchars
= 1;
1974 if (*non_constant_p
)
1976 oldidx
= TREE_OPERAND (t
, 1);
1977 index
= cxx_eval_constant_expression (ctx
, oldidx
,
1979 non_constant_p
, overflow_p
);
1980 VERIFY_CONSTANT (index
);
1981 if (lval
&& ary
== oldary
&& index
== oldidx
)
1984 return build4 (ARRAY_REF
, TREE_TYPE (t
), ary
, index
, NULL
, NULL
);
1985 elem_type
= TREE_TYPE (TREE_TYPE (ary
));
1986 if (TREE_CODE (ary
) == VIEW_CONVERT_EXPR
1987 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary
, 0)))
1988 && TREE_TYPE (t
) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary
, 0))))
1989 ary
= TREE_OPERAND (ary
, 0);
1990 if (TREE_CODE (ary
) == CONSTRUCTOR
)
1991 len
= CONSTRUCTOR_NELTS (ary
);
1992 else if (TREE_CODE (ary
) == STRING_CST
)
1994 elem_nchars
= (TYPE_PRECISION (elem_type
)
1995 / TYPE_PRECISION (char_type_node
));
1996 len
= (unsigned) TREE_STRING_LENGTH (ary
) / elem_nchars
;
1998 else if (TREE_CODE (ary
) == VECTOR_CST
)
1999 len
= VECTOR_CST_NELTS (ary
);
2002 /* We can't do anything with other tree codes, so use
2003 VERIFY_CONSTANT to complain and fail. */
2004 VERIFY_CONSTANT (ary
);
2008 if (!tree_fits_shwi_p (index
)
2009 || (i
= tree_to_shwi (index
)) < 0)
2011 diag_array_subscript (ctx
, ary
, index
);
2012 *non_constant_p
= true;
2017 if (TREE_CODE (TREE_TYPE (ary
)) == ARRAY_TYPE
)
2018 nelts
= array_type_nelts_top (TREE_TYPE (ary
));
2019 else if (VECTOR_TYPE_P (TREE_TYPE (ary
)))
2020 nelts
= size_int (TYPE_VECTOR_SUBPARTS (TREE_TYPE (ary
)));
2024 /* For VLAs, the number of elements won't be an integer constant. */
2025 nelts
= cxx_eval_constant_expression (ctx
, nelts
, false, non_constant_p
,
2027 VERIFY_CONSTANT (nelts
);
2028 if (!tree_int_cst_lt (index
, nelts
))
2030 diag_array_subscript (ctx
, ary
, index
);
2031 *non_constant_p
= true;
2036 if (TREE_CODE (ary
) == CONSTRUCTOR
)
2038 HOST_WIDE_INT ix
= find_array_ctor_elt (ary
, index
);
2048 if (TREE_CODE (ary
) == CONSTRUCTOR
2049 && CONSTRUCTOR_NO_IMPLICIT_ZERO (ary
))
2051 /* 'ary' is part of the aggregate initializer we're currently
2052 building; if there's no initializer for this element yet,
2055 error ("accessing uninitialized array element");
2056 *non_constant_p
= true;
2060 /* If it's within the array bounds but doesn't have an explicit
2061 initializer, it's value-initialized. */
2062 tree val
= build_value_init (elem_type
, tf_warning_or_error
);
2063 return cxx_eval_constant_expression (ctx
, val
, lval
, non_constant_p
,
2067 if (TREE_CODE (ary
) == CONSTRUCTOR
)
2068 return (*CONSTRUCTOR_ELTS (ary
))[i
].value
;
2069 else if (TREE_CODE (ary
) == VECTOR_CST
)
2070 return VECTOR_CST_ELT (ary
, i
);
2071 else if (elem_nchars
== 1)
2072 return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary
))),
2073 TREE_STRING_POINTER (ary
)[i
]);
2076 tree type
= cv_unqualified (TREE_TYPE (TREE_TYPE (ary
)));
2077 return native_interpret_expr (type
, (const unsigned char *)
2078 TREE_STRING_POINTER (ary
)
2079 + i
* elem_nchars
, elem_nchars
);
2081 /* Don't VERIFY_CONSTANT here. */
2084 /* Subroutine of cxx_eval_constant_expression.
2085 Attempt to reduce a field access of a value of class type. */
2088 cxx_eval_component_reference (const constexpr_ctx
*ctx
, tree t
,
2090 bool *non_constant_p
, bool *overflow_p
)
2092 unsigned HOST_WIDE_INT i
;
2095 tree part
= TREE_OPERAND (t
, 1);
2096 tree orig_whole
= TREE_OPERAND (t
, 0);
2097 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
2099 non_constant_p
, overflow_p
);
2100 if (TREE_CODE (whole
) == PTRMEM_CST
)
2101 whole
= cplus_expand_constant (whole
);
2102 if (whole
== orig_whole
)
2105 return fold_build3 (COMPONENT_REF
, TREE_TYPE (t
),
2106 whole
, part
, NULL_TREE
);
2107 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
2109 if (!*non_constant_p
&& TREE_CODE (whole
) != CONSTRUCTOR
)
2112 error ("%qE is not a constant expression", orig_whole
);
2113 *non_constant_p
= true;
2115 if (DECL_MUTABLE_P (part
))
2118 error ("mutable %qD is not usable in a constant expression", part
);
2119 *non_constant_p
= true;
2121 if (*non_constant_p
)
2123 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
2130 /* We're in the middle of initializing it. */
2134 if (TREE_CODE (TREE_TYPE (whole
)) == UNION_TYPE
2135 && CONSTRUCTOR_NELTS (whole
) > 0)
2137 /* DR 1188 says we don't have to deal with this. */
2139 error ("accessing %qD member instead of initialized %qD member in "
2140 "constant expression", part
, CONSTRUCTOR_ELT (whole
, 0)->index
);
2141 *non_constant_p
= true;
2145 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
2146 classes never get represented; throw together a value now. */
2147 if (is_really_empty_class (TREE_TYPE (t
)))
2148 return build_constructor (TREE_TYPE (t
), NULL
);
2150 if (CONSTRUCTOR_NO_IMPLICIT_ZERO (whole
))
2152 /* 'whole' is part of the aggregate initializer we're currently
2153 building; if there's no initializer for this member yet, that's an
2156 error ("accessing uninitialized member %qD", part
);
2157 *non_constant_p
= true;
2161 /* If there's no explicit init for this field, it's value-initialized. */
2162 value
= build_value_init (TREE_TYPE (t
), tf_warning_or_error
);
2163 return cxx_eval_constant_expression (ctx
, value
,
2165 non_constant_p
, overflow_p
);
2168 /* Subroutine of cxx_eval_constant_expression.
2169 Attempt to reduce a field access of a value of class type that is
2170 expressed as a BIT_FIELD_REF. */
2173 cxx_eval_bit_field_ref (const constexpr_ctx
*ctx
, tree t
,
2175 bool *non_constant_p
, bool *overflow_p
)
2177 tree orig_whole
= TREE_OPERAND (t
, 0);
2178 tree retval
, fldval
, utype
, mask
;
2179 bool fld_seen
= false;
2180 HOST_WIDE_INT istart
, isize
;
2181 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
2183 non_constant_p
, overflow_p
);
2184 tree start
, field
, value
;
2185 unsigned HOST_WIDE_INT i
;
2187 if (whole
== orig_whole
)
2189 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
2191 if (!*non_constant_p
2192 && TREE_CODE (whole
) != VECTOR_CST
2193 && TREE_CODE (whole
) != CONSTRUCTOR
)
2196 error ("%qE is not a constant expression", orig_whole
);
2197 *non_constant_p
= true;
2199 if (*non_constant_p
)
2202 if (TREE_CODE (whole
) == VECTOR_CST
)
2203 return fold_ternary (BIT_FIELD_REF
, TREE_TYPE (t
), whole
,
2204 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 2));
2206 start
= TREE_OPERAND (t
, 2);
2207 istart
= tree_to_shwi (start
);
2208 isize
= tree_to_shwi (TREE_OPERAND (t
, 1));
2209 utype
= TREE_TYPE (t
);
2210 if (!TYPE_UNSIGNED (utype
))
2211 utype
= build_nonstandard_integer_type (TYPE_PRECISION (utype
), 1);
2212 retval
= build_int_cst (utype
, 0);
2213 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
2215 tree bitpos
= bit_position (field
);
2216 if (bitpos
== start
&& DECL_SIZE (field
) == TREE_OPERAND (t
, 1))
2218 if (TREE_CODE (TREE_TYPE (field
)) == INTEGER_TYPE
2219 && TREE_CODE (value
) == INTEGER_CST
2220 && tree_fits_shwi_p (bitpos
)
2221 && tree_fits_shwi_p (DECL_SIZE (field
)))
2223 HOST_WIDE_INT bit
= tree_to_shwi (bitpos
);
2224 HOST_WIDE_INT sz
= tree_to_shwi (DECL_SIZE (field
));
2225 HOST_WIDE_INT shift
;
2226 if (bit
>= istart
&& bit
+ sz
<= istart
+ isize
)
2228 fldval
= fold_convert (utype
, value
);
2229 mask
= build_int_cst_type (utype
, -1);
2230 mask
= fold_build2 (LSHIFT_EXPR
, utype
, mask
,
2231 size_int (TYPE_PRECISION (utype
) - sz
));
2232 mask
= fold_build2 (RSHIFT_EXPR
, utype
, mask
,
2233 size_int (TYPE_PRECISION (utype
) - sz
));
2234 fldval
= fold_build2 (BIT_AND_EXPR
, utype
, fldval
, mask
);
2235 shift
= bit
- istart
;
2236 if (BYTES_BIG_ENDIAN
)
2237 shift
= TYPE_PRECISION (utype
) - shift
- sz
;
2238 fldval
= fold_build2 (LSHIFT_EXPR
, utype
, fldval
,
2240 retval
= fold_build2 (BIT_IOR_EXPR
, utype
, retval
, fldval
);
2246 return fold_convert (TREE_TYPE (t
), retval
);
2248 return error_mark_node
;
2251 /* Subroutine of cxx_eval_constant_expression.
2252 Evaluate a short-circuited logical expression T in the context
2253 of a given constexpr CALL. BAILOUT_VALUE is the value for
2254 early return. CONTINUE_VALUE is used here purely for
2255 sanity check purposes. */
2258 cxx_eval_logical_expression (const constexpr_ctx
*ctx
, tree t
,
2259 tree bailout_value
, tree continue_value
,
2261 bool *non_constant_p
, bool *overflow_p
)
2264 tree lhs
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
2266 non_constant_p
, overflow_p
);
2267 VERIFY_CONSTANT (lhs
);
2268 if (tree_int_cst_equal (lhs
, bailout_value
))
2270 gcc_assert (tree_int_cst_equal (lhs
, continue_value
));
2271 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
2272 lval
, non_constant_p
,
2274 VERIFY_CONSTANT (r
);
2278 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
2279 CONSTRUCTOR elements to initialize (part of) an object containing that
2280 field. Return a pointer to the constructor_elt corresponding to the
2281 initialization of the field. */
2283 static constructor_elt
*
2284 base_field_constructor_elt (vec
<constructor_elt
, va_gc
> *v
, tree ref
)
2286 tree aggr
= TREE_OPERAND (ref
, 0);
2287 tree field
= TREE_OPERAND (ref
, 1);
2289 constructor_elt
*ce
;
2291 gcc_assert (TREE_CODE (ref
) == COMPONENT_REF
);
2293 if (TREE_CODE (aggr
) == COMPONENT_REF
)
2295 constructor_elt
*base_ce
2296 = base_field_constructor_elt (v
, aggr
);
2297 v
= CONSTRUCTOR_ELTS (base_ce
->value
);
2300 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
2301 if (ce
->index
== field
)
2308 /* Some of the expressions fed to the constexpr mechanism are calls to
2309 constructors, which have type void. In that case, return the type being
2310 initialized by the constructor. */
2313 initialized_type (tree t
)
2317 tree type
= cv_unqualified (TREE_TYPE (t
));
2318 if (TREE_CODE (t
) == CALL_EXPR
|| TREE_CODE (t
) == AGGR_INIT_EXPR
)
2320 /* A constructor call has void type, so we need to look deeper. */
2321 tree fn
= get_function_named_in_call (t
);
2322 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
2323 && DECL_CXX_CONSTRUCTOR_P (fn
))
2324 type
= DECL_CONTEXT (fn
);
2329 /* We're about to initialize element INDEX of an array or class from VALUE.
2330 Set up NEW_CTX appropriately by adjusting .object to refer to the
2331 subobject and creating a new CONSTRUCTOR if the element is itself
2332 a class or array. */
2335 init_subob_ctx (const constexpr_ctx
*ctx
, constexpr_ctx
&new_ctx
,
2336 tree index
, tree
&value
)
2340 if (index
&& TREE_CODE (index
) != INTEGER_CST
2341 && TREE_CODE (index
) != FIELD_DECL
)
2342 /* This won't have an element in the new CONSTRUCTOR. */
2345 tree type
= initialized_type (value
);
2346 if (!AGGREGATE_TYPE_P (type
) && !VECTOR_TYPE_P (type
))
2347 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
2350 /* The sub-aggregate initializer might contain a placeholder;
2351 update object to refer to the subobject and ctor to refer to
2352 the (newly created) sub-initializer. */
2354 new_ctx
.object
= build_ctor_subob_ref (index
, type
, ctx
->object
);
2355 tree elt
= build_constructor (type
, NULL
);
2356 CONSTRUCTOR_NO_IMPLICIT_ZERO (elt
) = true;
2359 if (TREE_CODE (value
) == TARGET_EXPR
)
2360 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
2361 value
= TARGET_EXPR_INITIAL (value
);
2364 /* We're about to process an initializer for a class or array TYPE. Make
2365 sure that CTX is set up appropriately. */
2368 verify_ctor_sanity (const constexpr_ctx
*ctx
, tree type
)
2370 /* We don't bother building a ctor for an empty base subobject. */
2371 if (is_empty_class (type
))
2374 /* We're in the middle of an initializer that might involve placeholders;
2375 our caller should have created a CONSTRUCTOR for us to put the
2376 initializer into. We will either return that constructor or T. */
2377 gcc_assert (ctx
->ctor
);
2378 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2379 (type
, TREE_TYPE (ctx
->ctor
)));
2380 /* We used to check that ctx->ctor was empty, but that isn't the case when
2381 the object is zero-initialized before calling the constructor. */
2383 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2384 (type
, TREE_TYPE (ctx
->object
)));
2385 gcc_assert (!ctx
->object
|| !DECL_P (ctx
->object
)
2386 || *(ctx
->values
->get (ctx
->object
)) == ctx
->ctor
);
2389 /* Subroutine of cxx_eval_constant_expression.
2390 The expression tree T denotes a C-style array or a C-style
2391 aggregate. Reduce it to a constant expression. */
2394 cxx_eval_bare_aggregate (const constexpr_ctx
*ctx
, tree t
,
2396 bool *non_constant_p
, bool *overflow_p
)
2398 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
2399 bool changed
= false;
2400 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t
));
2401 tree type
= TREE_TYPE (t
);
2403 constexpr_ctx new_ctx
;
2404 if (TYPE_PTRMEMFUNC_P (type
) || VECTOR_TYPE_P (type
))
2406 /* We don't really need the ctx->ctor business for a PMF or
2407 vector, but it's simpler to use the same code. */
2409 new_ctx
.ctor
= build_constructor (type
, NULL
);
2410 new_ctx
.object
= NULL_TREE
;
2413 verify_ctor_sanity (ctx
, type
);
2414 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
2415 vec_alloc (*p
, vec_safe_length (v
));
2419 bool constant_p
= true;
2420 bool side_effects_p
= false;
2421 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, index
, value
)
2423 tree orig_value
= value
;
2424 init_subob_ctx (ctx
, new_ctx
, index
, value
);
2425 if (new_ctx
.ctor
!= ctx
->ctor
)
2426 /* If we built a new CONSTRUCTOR, attach it now so that other
2427 initializers can refer to it. */
2428 CONSTRUCTOR_APPEND_ELT (*p
, index
, new_ctx
.ctor
);
2429 tree elt
= cxx_eval_constant_expression (&new_ctx
, value
,
2431 non_constant_p
, overflow_p
);
2432 /* Don't VERIFY_CONSTANT here. */
2433 if (ctx
->quiet
&& *non_constant_p
)
2435 if (elt
!= orig_value
)
2438 if (!TREE_CONSTANT (elt
))
2440 if (TREE_SIDE_EFFECTS (elt
))
2441 side_effects_p
= true;
2442 if (index
&& TREE_CODE (index
) == COMPONENT_REF
)
2444 /* This is an initialization of a vfield inside a base
2445 subaggregate that we already initialized; push this
2446 initialization into the previous initialization. */
2447 constructor_elt
*inner
= base_field_constructor_elt (*p
, index
);
2452 && (TREE_CODE (index
) == NOP_EXPR
2453 || TREE_CODE (index
) == POINTER_PLUS_EXPR
))
2455 /* This is an initializer for an empty base; now that we've
2456 checked that it's constant, we can ignore it. */
2457 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index
))));
2460 else if (new_ctx
.ctor
!= ctx
->ctor
)
2462 /* We appended this element above; update the value. */
2463 gcc_assert ((*p
)->last().index
== index
);
2464 (*p
)->last().value
= elt
;
2467 CONSTRUCTOR_APPEND_ELT (*p
, index
, elt
);
2469 if (*non_constant_p
|| !changed
)
2472 /* We're done building this CONSTRUCTOR, so now we can interpret an
2473 element without an explicit initializer as value-initialized. */
2474 CONSTRUCTOR_NO_IMPLICIT_ZERO (t
) = false;
2475 TREE_CONSTANT (t
) = constant_p
;
2476 TREE_SIDE_EFFECTS (t
) = side_effects_p
;
2477 if (VECTOR_TYPE_P (type
))
2482 /* Subroutine of cxx_eval_constant_expression.
2483 The expression tree T is a VEC_INIT_EXPR which denotes the desired
2484 initialization of a non-static data member of array type. Reduce it to a
2487 Note that apart from value-initialization (when VALUE_INIT is true),
2488 this is only intended to support value-initialization and the
2489 initializations done by defaulted constructors for classes with
2490 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
2491 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
2492 for the copy/move constructor. */
2495 cxx_eval_vec_init_1 (const constexpr_ctx
*ctx
, tree atype
, tree init
,
2496 bool value_init
, bool lval
,
2497 bool *non_constant_p
, bool *overflow_p
)
2499 tree elttype
= TREE_TYPE (atype
);
2500 unsigned HOST_WIDE_INT max
= tree_to_uhwi (array_type_nelts_top (atype
));
2501 verify_ctor_sanity (ctx
, atype
);
2502 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
2503 vec_alloc (*p
, max
+ 1);
2504 bool pre_init
= false;
2505 unsigned HOST_WIDE_INT i
;
2507 /* For the default constructor, build up a call to the default
2508 constructor of the element type. We only need to handle class types
2509 here, as for a constructor to be constexpr, all members must be
2510 initialized, which for a defaulted default constructor means they must
2511 be of a class type with a constexpr default constructor. */
2512 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
2513 /* We only do this at the lowest level. */;
2514 else if (value_init
)
2516 init
= build_value_init (elttype
, tf_warning_or_error
);
2521 vec
<tree
, va_gc
> *argvec
= make_tree_vector ();
2522 init
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
2523 &argvec
, elttype
, LOOKUP_NORMAL
,
2524 tf_warning_or_error
);
2525 release_tree_vector (argvec
);
2526 init
= build_aggr_init_expr (TREE_TYPE (init
), init
);
2530 for (i
= 0; i
< max
; ++i
)
2532 tree idx
= build_int_cst (size_type_node
, i
);
2535 constexpr_ctx new_ctx
;
2536 init_subob_ctx (ctx
, new_ctx
, idx
, pre_init
? init
: elttype
);
2537 if (new_ctx
.ctor
!= ctx
->ctor
)
2538 CONSTRUCTOR_APPEND_ELT (*p
, idx
, new_ctx
.ctor
);
2539 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
2541 /* A multidimensional array; recurse. */
2542 if (value_init
|| init
== NULL_TREE
)
2544 eltinit
= NULL_TREE
;
2548 eltinit
= cp_build_array_ref (input_location
, init
, idx
,
2549 tf_warning_or_error
);
2550 eltinit
= cxx_eval_vec_init_1 (&new_ctx
, elttype
, eltinit
, value_init
,
2552 non_constant_p
, overflow_p
);
2556 /* Initializing an element using value or default initialization
2557 we just pre-built above. */
2558 eltinit
= cxx_eval_constant_expression (&new_ctx
, init
, lval
,
2559 non_constant_p
, overflow_p
);
2564 /* Copying an element. */
2565 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2566 (atype
, TREE_TYPE (init
)));
2567 eltinit
= cp_build_array_ref (input_location
, init
, idx
,
2568 tf_warning_or_error
);
2569 if (!real_lvalue_p (init
))
2570 eltinit
= move (eltinit
);
2571 eltinit
= force_rvalue (eltinit
, tf_warning_or_error
);
2572 eltinit
= (cxx_eval_constant_expression
2573 (&new_ctx
, eltinit
, lval
,
2574 non_constant_p
, overflow_p
));
2576 if (*non_constant_p
&& !ctx
->quiet
)
2578 if (new_ctx
.ctor
!= ctx
->ctor
)
2580 /* We appended this element above; update the value. */
2581 gcc_assert ((*p
)->last().index
== idx
);
2582 (*p
)->last().value
= eltinit
;
2585 CONSTRUCTOR_APPEND_ELT (*p
, idx
, eltinit
);
2586 /* Reuse the result of cxx_eval_constant_expression call
2587 from the first iteration to all others if it is a constant
2588 initializer that doesn't require relocations. */
2591 && (initializer_constant_valid_p (eltinit
, TREE_TYPE (eltinit
))
2592 == null_pointer_node
))
2594 if (new_ctx
.ctor
!= ctx
->ctor
)
2595 eltinit
= new_ctx
.ctor
;
2596 for (i
= 1; i
< max
; ++i
)
2598 idx
= build_int_cst (size_type_node
, i
);
2599 CONSTRUCTOR_APPEND_ELT (*p
, idx
, unshare_constructor (eltinit
));
2605 if (!*non_constant_p
)
2608 CONSTRUCTOR_NO_IMPLICIT_ZERO (init
) = false;
2614 cxx_eval_vec_init (const constexpr_ctx
*ctx
, tree t
,
2616 bool *non_constant_p
, bool *overflow_p
)
2618 tree atype
= TREE_TYPE (t
);
2619 tree init
= VEC_INIT_EXPR_INIT (t
);
2620 tree r
= cxx_eval_vec_init_1 (ctx
, atype
, init
,
2621 VEC_INIT_EXPR_VALUE_INIT (t
),
2622 lval
, non_constant_p
, overflow_p
);
2623 if (*non_constant_p
)
2629 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
2630 match. We want to be less strict for simple *& folding; if we have a
2631 non-const temporary that we access through a const pointer, that should
2632 work. We handle this here rather than change fold_indirect_ref_1
2633 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
2634 don't really make sense outside of constant expression evaluation. Also
2635 we want to allow folding to COMPONENT_REF, which could cause trouble
2636 with TBAA in fold_indirect_ref_1.
2638 Try to keep this function synced with fold_indirect_ref_1. */
2641 cxx_fold_indirect_ref (location_t loc
, tree type
, tree op0
, bool *empty_base
)
2647 subtype
= TREE_TYPE (sub
);
2648 if (!POINTER_TYPE_P (subtype
))
2651 if (TREE_CODE (sub
) == ADDR_EXPR
)
2653 tree op
= TREE_OPERAND (sub
, 0);
2654 tree optype
= TREE_TYPE (op
);
2656 /* *&CONST_DECL -> to the value of the const decl. */
2657 if (TREE_CODE (op
) == CONST_DECL
)
2658 return DECL_INITIAL (op
);
2659 /* *&p => p; make sure to handle *&"str"[cst] here. */
2660 if (same_type_ignoring_top_level_qualifiers_p (optype
, type
)
2661 /* Also handle the case where the desired type is an array of unknown
2662 bounds because the variable has had its bounds deduced since the
2663 ADDR_EXPR was created. */
2664 || (TREE_CODE (type
) == ARRAY_TYPE
2665 && TREE_CODE (optype
) == ARRAY_TYPE
2666 && TYPE_DOMAIN (type
) == NULL_TREE
2667 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (optype
),
2670 tree fop
= fold_read_from_constant_string (op
);
2676 /* *(foo *)&fooarray => fooarray[0] */
2677 else if (TREE_CODE (optype
) == ARRAY_TYPE
2678 && (same_type_ignoring_top_level_qualifiers_p
2679 (type
, TREE_TYPE (optype
))))
2681 tree type_domain
= TYPE_DOMAIN (optype
);
2682 tree min_val
= size_zero_node
;
2683 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
2684 min_val
= TYPE_MIN_VALUE (type_domain
);
2685 return build4_loc (loc
, ARRAY_REF
, type
, op
, min_val
,
2686 NULL_TREE
, NULL_TREE
);
2688 /* *(foo *)&complexfoo => __real__ complexfoo */
2689 else if (TREE_CODE (optype
) == COMPLEX_TYPE
2690 && (same_type_ignoring_top_level_qualifiers_p
2691 (type
, TREE_TYPE (optype
))))
2692 return fold_build1_loc (loc
, REALPART_EXPR
, type
, op
);
2693 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
2694 else if (VECTOR_TYPE_P (optype
)
2695 && (same_type_ignoring_top_level_qualifiers_p
2696 (type
, TREE_TYPE (optype
))))
2698 tree part_width
= TYPE_SIZE (type
);
2699 tree index
= bitsize_int (0);
2700 return fold_build3_loc (loc
, BIT_FIELD_REF
, type
, op
, part_width
, index
);
2702 /* Also handle conversion to an empty base class, which
2703 is represented with a NOP_EXPR. */
2704 else if (is_empty_class (type
)
2705 && CLASS_TYPE_P (optype
)
2706 && DERIVED_FROM_P (type
, optype
))
2711 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
2712 else if (RECORD_OR_UNION_TYPE_P (optype
))
2714 tree field
= TYPE_FIELDS (optype
);
2715 for (; field
; field
= DECL_CHAIN (field
))
2716 if (TREE_CODE (field
) == FIELD_DECL
2717 && integer_zerop (byte_position (field
))
2718 && (same_type_ignoring_top_level_qualifiers_p
2719 (TREE_TYPE (field
), type
)))
2721 return fold_build3 (COMPONENT_REF
, type
, op
, field
, NULL_TREE
);
2726 else if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
2727 && TREE_CODE (TREE_OPERAND (sub
, 1)) == INTEGER_CST
)
2729 tree op00
= TREE_OPERAND (sub
, 0);
2730 tree op01
= TREE_OPERAND (sub
, 1);
2733 if (TREE_CODE (op00
) == ADDR_EXPR
)
2736 op00
= TREE_OPERAND (op00
, 0);
2737 op00type
= TREE_TYPE (op00
);
2739 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
2740 if (VECTOR_TYPE_P (op00type
)
2741 && (same_type_ignoring_top_level_qualifiers_p
2742 (type
, TREE_TYPE (op00type
))))
2744 HOST_WIDE_INT offset
= tree_to_shwi (op01
);
2745 tree part_width
= TYPE_SIZE (type
);
2746 unsigned HOST_WIDE_INT part_widthi
= tree_to_shwi (part_width
)/BITS_PER_UNIT
;
2747 unsigned HOST_WIDE_INT indexi
= offset
* BITS_PER_UNIT
;
2748 tree index
= bitsize_int (indexi
);
2750 if (offset
/ part_widthi
< TYPE_VECTOR_SUBPARTS (op00type
))
2751 return fold_build3_loc (loc
,
2752 BIT_FIELD_REF
, type
, op00
,
2756 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
2757 else if (TREE_CODE (op00type
) == COMPLEX_TYPE
2758 && (same_type_ignoring_top_level_qualifiers_p
2759 (type
, TREE_TYPE (op00type
))))
2761 tree size
= TYPE_SIZE_UNIT (type
);
2762 if (tree_int_cst_equal (size
, op01
))
2763 return fold_build1_loc (loc
, IMAGPART_EXPR
, type
, op00
);
2765 /* ((foo *)&fooarray)[1] => fooarray[1] */
2766 else if (TREE_CODE (op00type
) == ARRAY_TYPE
2767 && (same_type_ignoring_top_level_qualifiers_p
2768 (type
, TREE_TYPE (op00type
))))
2770 tree type_domain
= TYPE_DOMAIN (op00type
);
2771 tree min_val
= size_zero_node
;
2772 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
2773 min_val
= TYPE_MIN_VALUE (type_domain
);
2774 op01
= size_binop_loc (loc
, EXACT_DIV_EXPR
, op01
,
2775 TYPE_SIZE_UNIT (type
));
2776 op01
= size_binop_loc (loc
, PLUS_EXPR
, op01
, min_val
);
2777 return build4_loc (loc
, ARRAY_REF
, type
, op00
, op01
,
2778 NULL_TREE
, NULL_TREE
);
2780 /* Also handle conversion to an empty base class, which
2781 is represented with a NOP_EXPR. */
2782 else if (is_empty_class (type
)
2783 && CLASS_TYPE_P (op00type
)
2784 && DERIVED_FROM_P (type
, op00type
))
2789 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
2790 else if (RECORD_OR_UNION_TYPE_P (op00type
))
2792 tree field
= TYPE_FIELDS (op00type
);
2793 for (; field
; field
= DECL_CHAIN (field
))
2794 if (TREE_CODE (field
) == FIELD_DECL
2795 && tree_int_cst_equal (byte_position (field
), op01
)
2796 && (same_type_ignoring_top_level_qualifiers_p
2797 (TREE_TYPE (field
), type
)))
2799 return fold_build3 (COMPONENT_REF
, type
, op00
,
2806 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
2807 else if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
2808 && (same_type_ignoring_top_level_qualifiers_p
2809 (type
, TREE_TYPE (TREE_TYPE (subtype
)))))
2812 tree min_val
= size_zero_node
;
2813 tree newsub
= cxx_fold_indirect_ref (loc
, TREE_TYPE (subtype
), sub
, NULL
);
2817 sub
= build1_loc (loc
, INDIRECT_REF
, TREE_TYPE (subtype
), sub
);
2818 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
2819 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
2820 min_val
= TYPE_MIN_VALUE (type_domain
);
2821 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
2829 cxx_eval_indirect_ref (const constexpr_ctx
*ctx
, tree t
,
2831 bool *non_constant_p
, bool *overflow_p
)
2833 tree orig_op0
= TREE_OPERAND (t
, 0);
2834 bool empty_base
= false;
2836 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
2837 operand is an integer-zero. Otherwise reject the MEM_REF for now. */
2839 if (TREE_CODE (t
) == MEM_REF
2840 && (!TREE_OPERAND (t
, 1) || !integer_zerop (TREE_OPERAND (t
, 1))))
2842 gcc_assert (ctx
->quiet
);
2843 *non_constant_p
= true;
2847 /* First try to simplify it directly. */
2848 tree r
= cxx_fold_indirect_ref (EXPR_LOCATION (t
), TREE_TYPE (t
), orig_op0
,
2852 /* If that didn't work, evaluate the operand first. */
2853 tree op0
= cxx_eval_constant_expression (ctx
, orig_op0
,
2854 /*lval*/false, non_constant_p
,
2856 /* Don't VERIFY_CONSTANT here. */
2857 if (*non_constant_p
)
2860 r
= cxx_fold_indirect_ref (EXPR_LOCATION (t
), TREE_TYPE (t
), op0
,
2864 /* We couldn't fold to a constant value. Make sure it's not
2865 something we should have been able to fold. */
2868 if (TREE_CODE (sub
) == ADDR_EXPR
)
2870 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
2871 (TREE_TYPE (TREE_TYPE (sub
)), TREE_TYPE (t
)));
2872 /* DR 1188 says we don't have to deal with this. */
2874 error ("accessing value of %qE through a %qT glvalue in a "
2875 "constant expression", build_fold_indirect_ref (sub
),
2877 *non_constant_p
= true;
2881 if (lval
&& op0
!= orig_op0
)
2882 return build1 (INDIRECT_REF
, TREE_TYPE (t
), op0
);
2884 VERIFY_CONSTANT (t
);
2889 r
= cxx_eval_constant_expression (ctx
, r
,
2890 lval
, non_constant_p
, overflow_p
);
2891 if (*non_constant_p
)
2894 /* If we're pulling out the value of an empty base, make sure
2895 that the whole object is constant and then return an empty
2897 if (empty_base
&& !lval
)
2899 VERIFY_CONSTANT (r
);
2900 r
= build_constructor (TREE_TYPE (t
), NULL
);
2901 TREE_CONSTANT (r
) = true;
2907 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
2908 Shared between potential_constant_expression and
2909 cxx_eval_constant_expression. */
2912 non_const_var_error (tree r
)
2914 tree type
= TREE_TYPE (r
);
2915 error ("the value of %qD is not usable in a constant "
2917 /* Avoid error cascade. */
2918 if (DECL_INITIAL (r
) == error_mark_node
)
2920 if (DECL_DECLARED_CONSTEXPR_P (r
))
2921 inform (DECL_SOURCE_LOCATION (r
),
2922 "%qD used in its own initializer", r
);
2923 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
2925 if (!CP_TYPE_CONST_P (type
))
2926 inform (DECL_SOURCE_LOCATION (r
),
2927 "%q#D is not const", r
);
2928 else if (CP_TYPE_VOLATILE_P (type
))
2929 inform (DECL_SOURCE_LOCATION (r
),
2930 "%q#D is volatile", r
);
2931 else if (!DECL_INITIAL (r
)
2932 || !TREE_CONSTANT (DECL_INITIAL (r
))
2933 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r
))
2934 inform (DECL_SOURCE_LOCATION (r
),
2935 "%qD was not initialized with a constant "
2942 if (cxx_dialect
>= cxx11
&& !DECL_DECLARED_CONSTEXPR_P (r
))
2943 inform (DECL_SOURCE_LOCATION (r
),
2944 "%qD was not declared %<constexpr%>", r
);
2946 inform (DECL_SOURCE_LOCATION (r
),
2947 "%qD does not have integral or enumeration type",
2952 /* Subroutine of cxx_eval_constant_expression.
2953 Like cxx_eval_unary_expression, except for trinary expressions. */
2956 cxx_eval_trinary_expression (const constexpr_ctx
*ctx
, tree t
,
2958 bool *non_constant_p
, bool *overflow_p
)
2964 for (i
= 0; i
< 3; i
++)
2966 args
[i
] = cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, i
),
2968 non_constant_p
, overflow_p
);
2969 VERIFY_CONSTANT (args
[i
]);
2972 val
= fold_ternary_loc (EXPR_LOCATION (t
), TREE_CODE (t
), TREE_TYPE (t
),
2973 args
[0], args
[1], args
[2]);
2974 if (val
== NULL_TREE
)
2976 VERIFY_CONSTANT (val
);
2981 var_in_constexpr_fn (tree t
)
2983 tree ctx
= DECL_CONTEXT (t
);
2984 return (cxx_dialect
>= cxx14
&& ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
2985 && DECL_DECLARED_CONSTEXPR_P (ctx
));
2988 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
2991 cxx_eval_store_expression (const constexpr_ctx
*ctx
, tree t
,
2993 bool *non_constant_p
, bool *overflow_p
)
2995 constexpr_ctx new_ctx
= *ctx
;
2997 tree init
= TREE_OPERAND (t
, 1);
2998 if (TREE_CLOBBER_P (init
))
2999 /* Just ignore clobbers. */
3002 /* First we figure out where we're storing to. */
3003 tree target
= TREE_OPERAND (t
, 0);
3004 tree type
= TREE_TYPE (target
);
3005 target
= cxx_eval_constant_expression (ctx
, target
,
3007 non_constant_p
, overflow_p
);
3008 if (*non_constant_p
)
3011 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (target
), type
))
3013 /* For initialization of an empty base, the original target will be
3014 *(base*)this, which the above evaluation resolves to the object
3015 argument, which has the derived type rather than the base type. In
3016 this situation, just evaluate the initializer and return, since
3017 there's no actual data to store. */
3018 gcc_assert (is_empty_class (type
));
3019 return cxx_eval_constant_expression (ctx
, init
, false,
3020 non_constant_p
, overflow_p
);
3023 /* And then find the underlying variable. */
3024 vec
<tree
,va_gc
> *refs
= make_tree_vector();
3025 tree object
= NULL_TREE
;
3026 for (tree probe
= target
; object
== NULL_TREE
; )
3028 switch (TREE_CODE (probe
))
3033 vec_safe_push (refs
, TREE_OPERAND (probe
, 1));
3034 vec_safe_push (refs
, TREE_TYPE (probe
));
3035 probe
= TREE_OPERAND (probe
, 0);
3043 /* And then find/build up our initializer for the path to the subobject
3044 we're initializing. */
3046 if (DECL_P (object
))
3047 valp
= ctx
->values
->get (object
);
3052 /* A constant-expression cannot modify objects from outside the
3053 constant-expression. */
3055 error ("modification of %qE is not a constant-expression", object
);
3056 *non_constant_p
= true;
3059 type
= TREE_TYPE (object
);
3060 bool no_zero_init
= true;
3062 vec
<tree
,va_gc
> *ctors
= make_tree_vector ();
3063 while (!refs
->is_empty())
3065 if (*valp
== NULL_TREE
)
3067 *valp
= build_constructor (type
, NULL
);
3068 CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp
) = no_zero_init
;
3070 /* If the value of object is already zero-initialized, any new ctors for
3071 subobjects will also be zero-initialized. */
3072 no_zero_init
= CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp
);
3074 vec_safe_push (ctors
, *valp
);
3076 enum tree_code code
= TREE_CODE (type
);
3078 tree index
= refs
->pop();
3080 constructor_elt
*cep
= NULL
;
3081 if (code
== ARRAY_TYPE
)
3084 = find_array_ctor_elt (*valp
, index
, /*insert*/true);
3085 gcc_assert (i
>= 0);
3086 cep
= CONSTRUCTOR_ELT (*valp
, i
);
3087 gcc_assert (TREE_CODE (cep
->index
) != RANGE_EXPR
);
3091 gcc_assert (TREE_CODE (index
) == FIELD_DECL
);
3093 /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
3094 Usually we meet initializers in that order, but it is
3095 possible for base types to be placed not in program
3097 tree fields
= TYPE_FIELDS (DECL_CONTEXT (index
));
3098 unsigned HOST_WIDE_INT idx
;
3101 vec_safe_iterate (CONSTRUCTOR_ELTS (*valp
), idx
, &cep
);
3102 idx
++, fields
= DECL_CHAIN (fields
))
3104 if (index
== cep
->index
)
3107 /* The field we're initializing must be on the field
3108 list. Look to see if it is present before the
3109 field the current ELT initializes. */
3110 for (; fields
!= cep
->index
; fields
= DECL_CHAIN (fields
))
3111 if (index
== fields
)
3115 /* We fell off the end of the CONSTRUCTOR, so insert a new
3116 entry at the end. */
3119 constructor_elt ce
= { index
, NULL_TREE
};
3121 vec_safe_insert (CONSTRUCTOR_ELTS (*valp
), idx
, ce
);
3122 cep
= CONSTRUCTOR_ELT (*valp
, idx
);
3128 release_tree_vector (refs
);
3130 if (AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
))
3132 /* Create a new CONSTRUCTOR in case evaluation of the initializer
3133 wants to modify it. */
3134 if (*valp
== NULL_TREE
)
3136 *valp
= new_ctx
.ctor
= build_constructor (type
, NULL
);
3137 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx
.ctor
) = no_zero_init
;
3140 new_ctx
.ctor
= *valp
;
3141 new_ctx
.object
= target
;
3144 init
= cxx_eval_constant_expression (&new_ctx
, init
, false,
3145 non_constant_p
, overflow_p
);
3146 /* Don't share a CONSTRUCTOR that might be changed later. */
3147 init
= unshare_constructor (init
);
3148 if (target
== object
)
3149 /* The hash table might have moved since the get earlier. */
3150 valp
= ctx
->values
->get (object
);
3152 if (TREE_CODE (init
) == CONSTRUCTOR
)
3154 /* An outer ctx->ctor might be pointing to *valp, so replace
3156 CONSTRUCTOR_ELTS (*valp
) = CONSTRUCTOR_ELTS (init
);
3157 TREE_CONSTANT (*valp
) = TREE_CONSTANT (init
);
3158 TREE_SIDE_EFFECTS (*valp
) = TREE_SIDE_EFFECTS (init
);
3159 CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp
)
3160 = CONSTRUCTOR_NO_IMPLICIT_ZERO (init
);
3165 /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
3166 CONSTRUCTORs, if any. */
3169 bool c
= TREE_CONSTANT (init
);
3170 bool s
= TREE_SIDE_EFFECTS (init
);
3172 FOR_EACH_VEC_SAFE_ELT (ctors
, i
, elt
)
3175 TREE_CONSTANT (elt
) = false;
3177 TREE_SIDE_EFFECTS (elt
) = true;
3179 release_tree_vector (ctors
);
3181 if (*non_constant_p
)
3189 /* Evaluate a ++ or -- expression. */
3192 cxx_eval_increment_expression (const constexpr_ctx
*ctx
, tree t
,
3194 bool *non_constant_p
, bool *overflow_p
)
3196 enum tree_code code
= TREE_CODE (t
);
3197 tree type
= TREE_TYPE (t
);
3198 tree op
= TREE_OPERAND (t
, 0);
3199 tree offset
= TREE_OPERAND (t
, 1);
3200 gcc_assert (TREE_CONSTANT (offset
));
3202 /* The operand as an lvalue. */
3203 op
= cxx_eval_constant_expression (ctx
, op
, true,
3204 non_constant_p
, overflow_p
);
3206 /* The operand as an rvalue. */
3207 tree val
= rvalue (op
);
3208 val
= cxx_eval_constant_expression (ctx
, val
, false,
3209 non_constant_p
, overflow_p
);
3210 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
3211 a local array in a constexpr function. */
3212 bool ptr
= POINTER_TYPE_P (TREE_TYPE (val
));
3214 VERIFY_CONSTANT (val
);
3216 /* The modified value. */
3217 bool inc
= (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
);
3219 if (POINTER_TYPE_P (type
))
3221 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
3222 offset
= convert_to_ptrofftype (offset
);
3224 offset
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (offset
), offset
);
3225 mod
= fold_build2 (POINTER_PLUS_EXPR
, type
, val
, offset
);
3228 mod
= fold_build2 (inc
? PLUS_EXPR
: MINUS_EXPR
, type
, val
, offset
);
3230 VERIFY_CONSTANT (mod
);
3232 /* Storing the modified value. */
3233 tree store
= build2 (MODIFY_EXPR
, type
, op
, mod
);
3234 cxx_eval_constant_expression (ctx
, store
,
3235 true, non_constant_p
, overflow_p
);
3237 /* And the value of the expression. */
3238 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
3240 /* Prefix ops are lvalues. */
3244 /* But we optimize when the caller wants an rvalue. */
3248 /* Postfix ops are rvalues. */
3252 /* Predicates for the meaning of *jump_target. */
3255 returns (tree
*jump_target
)
3258 && TREE_CODE (*jump_target
) == RETURN_EXPR
;
3262 breaks (tree
*jump_target
)
3265 && ((TREE_CODE (*jump_target
) == LABEL_DECL
3266 && LABEL_DECL_BREAK (*jump_target
))
3267 || TREE_CODE (*jump_target
) == EXIT_EXPR
);
3271 continues (tree
*jump_target
)
3274 && TREE_CODE (*jump_target
) == LABEL_DECL
3275 && LABEL_DECL_CONTINUE (*jump_target
);
3279 switches (tree
*jump_target
)
3282 && TREE_CODE (*jump_target
) == INTEGER_CST
;
3285 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
3286 at I matches *jump_target. If we're looking for a case label and we see
3287 the default label, copy I into DEFAULT_LABEL. */
3290 label_matches (tree
*jump_target
, tree_stmt_iterator i
,
3291 tree_stmt_iterator
& default_label
)
3293 tree stmt
= tsi_stmt (i
);
3294 switch (TREE_CODE (*jump_target
))
3297 if (TREE_CODE (stmt
) == LABEL_EXPR
3298 && LABEL_EXPR_LABEL (stmt
) == *jump_target
)
3303 if (TREE_CODE (stmt
) == CASE_LABEL_EXPR
)
3305 if (!CASE_LOW (stmt
))
3307 else if (tree_int_cst_equal (*jump_target
, CASE_LOW (stmt
)))
3318 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
3319 semantics, for switch, break, continue, and return. */
3322 cxx_eval_statement_list (const constexpr_ctx
*ctx
, tree t
,
3323 bool *non_constant_p
, bool *overflow_p
,
3326 tree_stmt_iterator i
;
3327 tree_stmt_iterator default_label
= tree_stmt_iterator();
3329 /* In a statement-expression we want to return the last value. */
3333 local_target
= NULL_TREE
;
3334 jump_target
= &local_target
;
3336 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
3339 tree stmt
= tsi_stmt (i
);
3342 if (TREE_CODE (stmt
) == STATEMENT_LIST
)
3343 /* The label we want might be inside. */;
3344 else if (label_matches (jump_target
, i
, default_label
))
3346 *jump_target
= NULL_TREE
;
3350 r
= cxx_eval_constant_expression (ctx
, stmt
, false,
3351 non_constant_p
, overflow_p
,
3353 if (*non_constant_p
)
3355 if (returns (jump_target
) || breaks (jump_target
))
3358 if (switches (jump_target
) && !tsi_end_p (default_label
))
3361 *jump_target
= NULL_TREE
;
3367 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
3368 semantics; continue semantics are covered by cxx_eval_statement_list. */
3371 cxx_eval_loop_expr (const constexpr_ctx
*ctx
, tree t
,
3372 bool *non_constant_p
, bool *overflow_p
,
3375 constexpr_ctx new_ctx
= *ctx
;
3377 tree body
= TREE_OPERAND (t
, 0);
3380 hash_set
<tree
> save_exprs
;
3381 new_ctx
.save_exprs
= &save_exprs
;
3383 cxx_eval_constant_expression (&new_ctx
, body
, /*lval*/false,
3384 non_constant_p
, overflow_p
, jump_target
);
3386 /* Forget saved values of SAVE_EXPRs. */
3387 for (hash_set
<tree
>::iterator iter
= save_exprs
.begin();
3388 iter
!= save_exprs
.end(); ++iter
)
3389 new_ctx
.values
->remove (*iter
);
3391 while (!returns (jump_target
) && !breaks (jump_target
) && !*non_constant_p
);
3393 if (breaks (jump_target
))
3394 *jump_target
= NULL_TREE
;
3399 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
3403 cxx_eval_switch_expr (const constexpr_ctx
*ctx
, tree t
,
3404 bool *non_constant_p
, bool *overflow_p
,
3407 tree cond
= TREE_OPERAND (t
, 0);
3408 cond
= cxx_eval_constant_expression (ctx
, cond
, false,
3409 non_constant_p
, overflow_p
);
3410 VERIFY_CONSTANT (cond
);
3411 *jump_target
= cond
;
3413 tree body
= TREE_OPERAND (t
, 1);
3414 cxx_eval_statement_list (ctx
, body
,
3415 non_constant_p
, overflow_p
, jump_target
);
3416 if (breaks (jump_target
) || switches (jump_target
))
3417 *jump_target
= NULL_TREE
;
3421 /* Subroutine of cxx_eval_constant_expression.
3422 Attempt to reduce a POINTER_PLUS_EXPR expression T. */
3425 cxx_eval_pointer_plus_expression (const constexpr_ctx
*ctx
, tree t
,
3426 bool lval
, bool *non_constant_p
,
3429 tree orig_type
= TREE_TYPE (t
);
3430 tree op00
= TREE_OPERAND (t
, 0);
3431 tree op01
= TREE_OPERAND (t
, 1);
3432 location_t loc
= EXPR_LOCATION (t
);
3434 op00
= cxx_eval_constant_expression (ctx
, op00
, lval
,
3435 non_constant_p
, overflow_p
);
3438 if (TREE_CODE (op00
) != ADDR_EXPR
)
3441 op01
= cxx_eval_constant_expression (ctx
, op01
, lval
,
3442 non_constant_p
, overflow_p
);
3443 op00
= TREE_OPERAND (op00
, 0);
3445 /* &A[i] p+ j => &A[i + j] */
3446 if (TREE_CODE (op00
) == ARRAY_REF
3447 && TREE_CODE (TREE_OPERAND (op00
, 1)) == INTEGER_CST
3448 && TREE_CODE (op01
) == INTEGER_CST
3449 && TYPE_SIZE_UNIT (TREE_TYPE (op00
))
3450 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (op00
))) == INTEGER_CST
)
3452 tree type
= TREE_TYPE (op00
);
3453 t
= fold_convert_loc (loc
, ssizetype
, TREE_OPERAND (op00
, 1));
3454 tree nelts
= array_type_nelts_top (TREE_TYPE (TREE_OPERAND (op00
, 0)));
3455 /* Don't fold an out-of-bound access. */
3456 if (!tree_int_cst_le (t
, nelts
))
3458 op01
= cp_fold_convert (ssizetype
, op01
);
3459 /* Don't fold if op01 can't be divided exactly by TYPE_SIZE_UNIT.
3460 constexpr int A[1]; ... (char *)&A[0] + 1 */
3461 if (!integer_zerop (fold_build2_loc (loc
, TRUNC_MOD_EXPR
, sizetype
,
3462 op01
, TYPE_SIZE_UNIT (type
))))
3464 /* Make sure to treat the second operand of POINTER_PLUS_EXPR
3466 op01
= fold_build2_loc (loc
, EXACT_DIV_EXPR
, ssizetype
, op01
,
3467 TYPE_SIZE_UNIT (type
));
3468 t
= size_binop_loc (loc
, PLUS_EXPR
, op01
, t
);
3469 t
= build4_loc (loc
, ARRAY_REF
, type
, TREE_OPERAND (op00
, 0),
3470 t
, NULL_TREE
, NULL_TREE
);
3471 t
= cp_build_addr_expr (t
, tf_warning_or_error
);
3472 t
= cp_fold_convert (orig_type
, t
);
3473 return cxx_eval_constant_expression (ctx
, t
, lval
, non_constant_p
,
3480 /* Attempt to reduce the expression T to a constant value.
3481 On failure, issue diagnostic and return error_mark_node. */
3482 /* FIXME unify with c_fully_fold */
3483 /* FIXME overflow_p is too global */
3486 cxx_eval_constant_expression (const constexpr_ctx
*ctx
, tree t
,
3488 bool *non_constant_p
, bool *overflow_p
,
3491 constexpr_ctx new_ctx
;
3494 if (t
== error_mark_node
)
3496 *non_constant_p
= true;
3499 if (CONSTANT_CLASS_P (t
))
3501 if (TREE_OVERFLOW (t
))
3504 permerror (input_location
, "overflow in constant expression");
3505 if (!flag_permissive
|| ctx
->quiet
)
3511 switch (TREE_CODE (t
))
3516 /* We ask for an rvalue for the RESULT_DECL when indirecting
3517 through an invisible reference, or in named return value
3519 return (*ctx
->values
->get (t
));
3523 /* We used to not check lval for CONST_DECL, but darwin.c uses
3524 CONST_DECL for aggregate constants. */
3528 r
= decl_really_constant_value (t
);
3530 r
= decl_constant_value (t
);
3531 if (TREE_CODE (r
) == TARGET_EXPR
3532 && TREE_CODE (TARGET_EXPR_INITIAL (r
)) == CONSTRUCTOR
)
3533 r
= TARGET_EXPR_INITIAL (r
);
3535 if (tree
*p
= ctx
->values
->get (r
))
3536 if (*p
!= NULL_TREE
)
3541 non_const_var_error (r
);
3542 *non_constant_p
= true;
3550 case CASE_LABEL_EXPR
:
3554 if (lval
&& TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
3556 else if (tree
*p
= ctx
->values
->get (r
))
3559 /* Defer in case this is only used for its type. */;
3560 else if (TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
)
3561 /* Defer, there's no lvalue->rvalue conversion. */;
3562 else if (is_empty_class (TREE_TYPE (t
)))
3564 /* If the class is empty, we aren't actually loading anything. */
3565 r
= build_constructor (TREE_TYPE (t
), NULL
);
3566 TREE_CONSTANT (r
) = true;
3571 error ("%qE is not a constant expression", t
);
3572 *non_constant_p
= true;
3577 case AGGR_INIT_EXPR
:
3578 r
= cxx_eval_call_expression (ctx
, t
, lval
,
3579 non_constant_p
, overflow_p
);
3584 r
= DECL_EXPR_DECL (t
);
3585 if (AGGREGATE_TYPE_P (TREE_TYPE (r
))
3586 || VECTOR_TYPE_P (TREE_TYPE (r
)))
3590 new_ctx
.ctor
= build_constructor (TREE_TYPE (r
), NULL
);
3591 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx
.ctor
) = true;
3592 new_ctx
.values
->put (r
, new_ctx
.ctor
);
3596 if (tree init
= DECL_INITIAL (r
))
3598 init
= cxx_eval_constant_expression (ctx
, init
,
3600 non_constant_p
, overflow_p
);
3601 /* Don't share a CONSTRUCTOR that might be changed. */
3602 init
= unshare_constructor (init
);
3603 ctx
->values
->put (r
, init
);
3605 else if (ctx
== &new_ctx
)
3606 /* We gave it a CONSTRUCTOR above. */;
3608 ctx
->values
->put (r
, NULL_TREE
);
3613 if (!literal_type_p (TREE_TYPE (t
)))
3617 error ("temporary of non-literal type %qT in a "
3618 "constant expression", TREE_TYPE (t
));
3619 explain_non_literal_class (TREE_TYPE (t
));
3621 *non_constant_p
= true;
3624 if ((AGGREGATE_TYPE_P (TREE_TYPE (t
)) || VECTOR_TYPE_P (TREE_TYPE (t
))))
3626 /* We're being expanded without an explicit target, so start
3627 initializing a new object; expansion with an explicit target
3628 strips the TARGET_EXPR before we get here. */
3630 new_ctx
.ctor
= build_constructor (TREE_TYPE (t
), NULL
);
3631 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx
.ctor
) = true;
3632 new_ctx
.object
= TARGET_EXPR_SLOT (t
);
3633 ctx
->values
->put (new_ctx
.object
, new_ctx
.ctor
);
3636 /* Pass false for 'lval' because this indicates
3637 initialization of a temporary. */
3638 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
3640 non_constant_p
, overflow_p
);
3641 if (!*non_constant_p
)
3642 /* Adjust the type of the result to the type of the temporary. */
3643 r
= adjust_temp_type (TREE_TYPE (t
), r
);
3646 tree slot
= TARGET_EXPR_SLOT (t
);
3647 r
= unshare_constructor (r
);
3648 ctx
->values
->put (slot
, r
);
3655 r
= cxx_eval_store_expression (ctx
, t
, lval
,
3656 non_constant_p
, overflow_p
);
3660 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
3662 non_constant_p
, overflow_p
);
3666 if (TREE_OPERAND (t
, 0) != NULL_TREE
)
3667 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3669 non_constant_p
, overflow_p
);
3674 /* Avoid evaluating a SAVE_EXPR more than once. */
3675 if (tree
*p
= ctx
->values
->get (t
))
3679 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), false,
3680 non_constant_p
, overflow_p
);
3681 ctx
->values
->put (t
, r
);
3682 if (ctx
->save_exprs
)
3683 ctx
->save_exprs
->add (t
);
3687 case NON_LVALUE_EXPR
:
3688 case TRY_CATCH_EXPR
:
3690 case CLEANUP_POINT_EXPR
:
3691 case MUST_NOT_THROW_EXPR
:
3694 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3696 non_constant_p
, overflow_p
,
3700 case TRY_FINALLY_EXPR
:
3701 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), lval
,
3702 non_constant_p
, overflow_p
,
3704 if (!*non_constant_p
)
3705 /* Also evaluate the cleanup. */
3706 cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1), true,
3707 non_constant_p
, overflow_p
,
3711 /* These differ from cxx_eval_unary_expression in that this doesn't
3712 check for a constant operand or result; an address can be
3713 constant without its operand being, and vice versa. */
3716 r
= cxx_eval_indirect_ref (ctx
, t
, lval
,
3717 non_constant_p
, overflow_p
);
3722 tree oldop
= TREE_OPERAND (t
, 0);
3723 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
3725 non_constant_p
, overflow_p
);
3726 /* Don't VERIFY_CONSTANT here. */
3727 if (*non_constant_p
)
3729 gcc_checking_assert (TREE_CODE (op
) != CONSTRUCTOR
);
3730 /* This function does more aggressive folding than fold itself. */
3731 r
= build_fold_addr_expr_with_type (op
, TREE_TYPE (t
));
3732 if (TREE_CODE (r
) == ADDR_EXPR
&& TREE_OPERAND (r
, 0) == oldop
)
3740 case FIX_TRUNC_EXPR
:
3745 case TRUTH_NOT_EXPR
:
3746 case FIXED_CONVERT_EXPR
:
3747 r
= cxx_eval_unary_expression (ctx
, t
, lval
,
3748 non_constant_p
, overflow_p
);
3752 r
= fold_sizeof_expr (t
);
3753 VERIFY_CONSTANT (r
);
3758 /* check_return_expr sometimes wraps a TARGET_EXPR in a
3759 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
3760 introduced by build_call_a. */
3761 tree op0
= TREE_OPERAND (t
, 0);
3762 tree op1
= TREE_OPERAND (t
, 1);
3764 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
3765 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
3766 r
= cxx_eval_constant_expression (ctx
, op0
,
3767 lval
, non_constant_p
, overflow_p
,
3771 /* Check that the LHS is constant and then discard it. */
3772 cxx_eval_constant_expression (ctx
, op0
,
3773 true, non_constant_p
, overflow_p
,
3775 if (*non_constant_p
)
3777 op1
= TREE_OPERAND (t
, 1);
3778 r
= cxx_eval_constant_expression (ctx
, op1
,
3779 lval
, non_constant_p
, overflow_p
,
3785 case POINTER_PLUS_EXPR
:
3786 r
= cxx_eval_pointer_plus_expression (ctx
, t
, lval
, non_constant_p
,
3790 /* else fall through */
3795 case TRUNC_DIV_EXPR
:
3797 case FLOOR_DIV_EXPR
:
3798 case ROUND_DIV_EXPR
:
3799 case TRUNC_MOD_EXPR
:
3801 case ROUND_MOD_EXPR
:
3803 case EXACT_DIV_EXPR
:
3813 case TRUTH_XOR_EXPR
:
3820 case UNORDERED_EXPR
:
3830 r
= cxx_eval_binary_expression (ctx
, t
, lval
,
3831 non_constant_p
, overflow_p
);
3834 /* fold can introduce non-IF versions of these; still treat them as
3835 short-circuiting. */
3836 case TRUTH_AND_EXPR
:
3837 case TRUTH_ANDIF_EXPR
:
3838 r
= cxx_eval_logical_expression (ctx
, t
, boolean_false_node
,
3841 non_constant_p
, overflow_p
);
3845 case TRUTH_ORIF_EXPR
:
3846 r
= cxx_eval_logical_expression (ctx
, t
, boolean_true_node
,
3849 non_constant_p
, overflow_p
);
3853 r
= cxx_eval_array_reference (ctx
, t
, lval
,
3854 non_constant_p
, overflow_p
);
3858 if (is_overloaded_fn (t
))
3860 /* We can only get here in checking mode via
3861 build_non_dependent_expr, because any expression that
3862 calls or takes the address of the function will have
3863 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
3864 gcc_checking_assert (ctx
->quiet
|| errorcount
);
3865 *non_constant_p
= true;
3868 r
= cxx_eval_component_reference (ctx
, t
, lval
,
3869 non_constant_p
, overflow_p
);
3873 r
= cxx_eval_bit_field_ref (ctx
, t
, lval
,
3874 non_constant_p
, overflow_p
);
3879 r
= cxx_eval_conditional_expression (ctx
, t
, lval
,
3880 non_constant_p
, overflow_p
,
3885 if (TREE_CONSTANT (t
))
3887 /* Don't re-process a constant CONSTRUCTOR, but do fold it to
3888 VECTOR_CST if applicable. */
3889 /* FIXME after GCC 6 branches, make the verify unconditional. */
3891 verify_constructor_flags (t
);
3893 recompute_constructor_flags (t
);
3894 if (TREE_CONSTANT (t
))
3897 r
= cxx_eval_bare_aggregate (ctx
, t
, lval
,
3898 non_constant_p
, overflow_p
);
3902 /* We can get this in a defaulted constructor for a class with a
3903 non-static data member of array type. Either the initializer will
3904 be NULL, meaning default-initialization, or it will be an lvalue
3905 or xvalue of the same type, meaning direct-initialization from the
3906 corresponding member. */
3907 r
= cxx_eval_vec_init (ctx
, t
, lval
,
3908 non_constant_p
, overflow_p
);
3913 r
= cxx_eval_trinary_expression (ctx
, t
, lval
,
3914 non_constant_p
, overflow_p
);
3918 case VIEW_CONVERT_EXPR
:
3920 case UNARY_PLUS_EXPR
:
3922 enum tree_code tcode
= TREE_CODE (t
);
3923 tree oldop
= TREE_OPERAND (t
, 0);
3925 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
3927 non_constant_p
, overflow_p
);
3928 if (*non_constant_p
)
3930 tree type
= TREE_TYPE (t
);
3931 if (TREE_CODE (op
) == PTRMEM_CST
3932 && !TYPE_PTRMEM_P (type
))
3933 op
= cplus_expand_constant (op
);
3934 if (TREE_CODE (op
) == PTRMEM_CST
&& tcode
== NOP_EXPR
)
3936 if (same_type_ignoring_top_level_qualifiers_p (type
,
3942 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3943 "a reinterpret_cast is not a constant-expression");
3944 *non_constant_p
= true;
3948 if (POINTER_TYPE_P (type
)
3949 && TREE_CODE (op
) == INTEGER_CST
3950 && !integer_zerop (op
))
3953 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3954 "reinterpret_cast from integer to pointer");
3955 *non_constant_p
= true;
3958 if (op
== oldop
&& tcode
!= UNARY_PLUS_EXPR
)
3959 /* We didn't fold at the top so we could check for ptr-int
3962 if (tcode
== UNARY_PLUS_EXPR
)
3963 r
= fold_convert (TREE_TYPE (t
), op
);
3965 r
= fold_build1 (tcode
, type
, op
);
3966 /* Conversion of an out-of-range value has implementation-defined
3967 behavior; the language considers it different from arithmetic
3968 overflow, which is undefined. */
3969 if (TREE_OVERFLOW_P (r
) && !TREE_OVERFLOW_P (op
))
3970 TREE_OVERFLOW (r
) = false;
3974 case EMPTY_CLASS_EXPR
:
3975 /* This is good enough for a function argument that might not get
3976 used, and they can't do anything with it, so just return it. */
3979 case STATEMENT_LIST
:
3981 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
3982 return cxx_eval_statement_list (&new_ctx
, t
,
3983 non_constant_p
, overflow_p
, jump_target
);
3986 return cxx_eval_constant_expression (ctx
, BIND_EXPR_BODY (t
),
3988 non_constant_p
, overflow_p
,
3991 case PREINCREMENT_EXPR
:
3992 case POSTINCREMENT_EXPR
:
3993 case PREDECREMENT_EXPR
:
3994 case POSTDECREMENT_EXPR
:
3995 return cxx_eval_increment_expression (ctx
, t
,
3996 lval
, non_constant_p
, overflow_p
);
4002 case VEC_DELETE_EXPR
:
4005 /* GCC internal stuff. */
4008 case WITH_CLEANUP_EXPR
:
4009 case NON_DEPENDENT_EXPR
:
4013 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
4014 "expression %qE is not a constant-expression", t
);
4015 *non_constant_p
= true;
4018 case PLACEHOLDER_EXPR
:
4019 if (!ctx
|| !ctx
->ctor
|| (lval
&& !ctx
->object
)
4020 || !(same_type_ignoring_top_level_qualifiers_p
4021 (TREE_TYPE (t
), TREE_TYPE (ctx
->ctor
))))
4023 /* A placeholder without a referent. We can get here when
4024 checking whether NSDMIs are noexcept, or in massage_init_elt;
4025 just say it's non-constant for now. */
4026 gcc_assert (ctx
->quiet
);
4027 *non_constant_p
= true;
4032 /* Use of the value or address of the current object. We could
4033 use ctx->object unconditionally, but using ctx->ctor when we
4034 can is a minor optimization. */
4035 tree ctor
= lval
? ctx
->object
: ctx
->ctor
;
4036 return cxx_eval_constant_expression
4038 non_constant_p
, overflow_p
);
4044 tree cond
= TREE_OPERAND (t
, 0);
4045 cond
= cxx_eval_constant_expression (ctx
, cond
, /*lval*/false,
4046 non_constant_p
, overflow_p
);
4047 VERIFY_CONSTANT (cond
);
4048 if (integer_nonzerop (cond
))
4054 *jump_target
= TREE_OPERAND (t
, 0);
4055 gcc_assert (breaks (jump_target
) || continues (jump_target
));
4059 cxx_eval_loop_expr (ctx
, t
,
4060 non_constant_p
, overflow_p
, jump_target
);
4064 cxx_eval_switch_expr (ctx
, t
,
4065 non_constant_p
, overflow_p
, jump_target
);
4069 /* It's possible to get a requires-expression in a constant
4070 expression. For example:
4072 template<typename T> concept bool C() {
4073 return requires (T t) { t; };
4076 template<typename T> requires !C<T>() void f(T);
4078 Normalization leaves f with the associated constraint
4079 '!requires (T t) { ... }' which is not transformed into
4081 if (!processing_template_decl
)
4082 return evaluate_constraint_expression (t
, NULL_TREE
);
4084 *non_constant_p
= true;
4088 if (STATEMENT_CODE_P (TREE_CODE (t
)))
4090 /* This function doesn't know how to deal with pre-genericize
4091 statements; this can only happen with statement-expressions,
4092 so for now just fail. */
4094 error_at (EXPR_LOCATION (t
),
4095 "statement is not a constant-expression");
4098 internal_error ("unexpected expression %qE of kind %s", t
,
4099 get_tree_code_name (TREE_CODE (t
)));
4100 *non_constant_p
= true;
4104 if (r
== error_mark_node
)
4105 *non_constant_p
= true;
4107 if (*non_constant_p
)
4114 cxx_eval_outermost_constant_expr (tree t
, bool allow_non_constant
,
4115 bool strict
= true, tree object
= NULL_TREE
)
4117 bool non_constant_p
= false;
4118 bool overflow_p
= false;
4119 hash_map
<tree
,tree
> map
;
4121 constexpr_ctx ctx
= { NULL
, &map
, NULL
, NULL
, NULL
,
4122 allow_non_constant
, strict
};
4124 tree type
= initialized_type (t
);
4126 if (AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
))
4128 /* In C++14 an NSDMI can participate in aggregate initialization,
4129 and can refer to the address of the object being initialized, so
4130 we need to pass in the relevant VAR_DECL if we want to do the
4131 evaluation in a single pass. The evaluation will dynamically
4132 update ctx.values for the VAR_DECL. We use the same strategy
4133 for C++11 constexpr constructors that refer to the object being
4135 ctx
.ctor
= build_constructor (type
, NULL
);
4136 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctx
.ctor
) = true;
4139 if (TREE_CODE (t
) == TARGET_EXPR
)
4140 object
= TARGET_EXPR_SLOT (t
);
4141 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
4142 object
= AGGR_INIT_EXPR_SLOT (t
);
4144 ctx
.object
= object
;
4146 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4147 (type
, TREE_TYPE (object
)));
4148 if (object
&& DECL_P (object
))
4149 map
.put (object
, ctx
.ctor
);
4150 if (TREE_CODE (r
) == TARGET_EXPR
)
4151 /* Avoid creating another CONSTRUCTOR when we expand the
4153 r
= TARGET_EXPR_INITIAL (r
);
4156 r
= cxx_eval_constant_expression (&ctx
, r
,
4157 false, &non_constant_p
, &overflow_p
);
4159 verify_constant (r
, allow_non_constant
, &non_constant_p
, &overflow_p
);
4161 /* Mutable logic is a bit tricky: we want to allow initialization of
4162 constexpr variables with mutable members, but we can't copy those
4163 members to another constexpr variable. */
4164 if (TREE_CODE (r
) == CONSTRUCTOR
4165 && CONSTRUCTOR_MUTABLE_POISON (r
))
4167 if (!allow_non_constant
)
4168 error ("%qE is not a constant expression because it refers to "
4169 "mutable subobjects of %qT", t
, type
);
4170 non_constant_p
= true;
4173 /* Technically we should check this for all subexpressions, but that
4174 runs into problems with our internal representation of pointer
4175 subtraction and the 5.19 rules are still in flux. */
4176 if (CONVERT_EXPR_CODE_P (TREE_CODE (r
))
4177 && ARITHMETIC_TYPE_P (TREE_TYPE (r
))
4178 && TREE_CODE (TREE_OPERAND (r
, 0)) == ADDR_EXPR
)
4180 if (!allow_non_constant
)
4181 error ("conversion from pointer type %qT "
4182 "to arithmetic type %qT in a constant-expression",
4183 TREE_TYPE (TREE_OPERAND (r
, 0)), TREE_TYPE (r
));
4184 non_constant_p
= true;
4187 if (!non_constant_p
&& overflow_p
)
4188 non_constant_p
= true;
4190 /* Unshare the result unless it's a CONSTRUCTOR in which case it's already
4192 bool should_unshare
= true;
4193 if (r
== t
|| TREE_CODE (r
) == CONSTRUCTOR
)
4194 should_unshare
= false;
4196 if (non_constant_p
&& !allow_non_constant
)
4197 return error_mark_node
;
4198 else if (non_constant_p
&& TREE_CONSTANT (r
))
4200 /* This isn't actually constant, so unset TREE_CONSTANT. */
4203 else if (TREE_CODE (r
) == CONSTRUCTOR
)
4204 r
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (r
), r
);
4206 r
= build_nop (TREE_TYPE (r
), r
);
4207 TREE_CONSTANT (r
) = false;
4209 else if (non_constant_p
|| r
== t
)
4213 r
= unshare_expr (r
);
4215 if (TREE_CODE (r
) == CONSTRUCTOR
&& CLASS_TYPE_P (TREE_TYPE (r
)))
4217 if (TREE_CODE (t
) == TARGET_EXPR
4218 && TARGET_EXPR_INITIAL (t
) == r
)
4222 r
= get_target_expr (r
);
4223 TREE_CONSTANT (r
) = true;
4231 /* Returns true if T is a valid subexpression of a constant expression,
4232 even if it isn't itself a constant expression. */
4235 is_sub_constant_expr (tree t
)
4237 bool non_constant_p
= false;
4238 bool overflow_p
= false;
4239 hash_map
<tree
, tree
> map
;
4241 constexpr_ctx ctx
= { NULL
, &map
, NULL
, NULL
, NULL
, true, true };
4243 cxx_eval_constant_expression (&ctx
, t
, false, &non_constant_p
,
4245 return !non_constant_p
&& !overflow_p
;
4248 /* If T represents a constant expression returns its reduced value.
4249 Otherwise return error_mark_node. If T is dependent, then
4253 cxx_constant_value (tree t
, tree decl
)
4255 return cxx_eval_outermost_constant_expr (t
, false, true, decl
);
4258 /* Helper routine for fold_simple function. Either return simplified
4259 expression T, otherwise NULL_TREE.
4260 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
4261 even if we are within template-declaration. So be careful on call, as in
4262 such case types can be undefined. */
4265 fold_simple_1 (tree t
)
4268 enum tree_code code
= TREE_CODE (t
);
4280 return fold_sizeof_expr (t
);
4288 case TRUTH_NOT_EXPR
:
4290 case VIEW_CONVERT_EXPR
:
4293 case FIX_TRUNC_EXPR
:
4294 case FIXED_CONVERT_EXPR
:
4295 case ADDR_SPACE_CONVERT_EXPR
:
4297 op1
= TREE_OPERAND (t
, 0);
4299 t
= const_unop (code
, TREE_TYPE (t
), op1
);
4303 if (CONVERT_EXPR_CODE_P (code
)
4304 && TREE_OVERFLOW_P (t
) && !TREE_OVERFLOW_P (op1
))
4305 TREE_OVERFLOW (t
) = false;
4313 /* If T is a simple constant expression, returns its simplified value.
4314 Otherwise returns T. In contrast to maybe_constant_value do we
4315 simplify only few operations on constant-expressions, and we don't
4316 try to simplify constexpressions. */
4319 fold_simple (tree t
)
4322 if (processing_template_decl
)
4325 r
= fold_simple_1 (t
);
4332 /* If T is a constant expression, returns its reduced value.
4333 Otherwise, if T does not have TREE_CONSTANT set, returns T.
4334 Otherwise, returns a version of T without TREE_CONSTANT. */
4337 maybe_constant_value_1 (tree t
, tree decl
)
4341 if (!potential_nondependent_constant_expression (t
))
4343 if (TREE_OVERFLOW_P (t
))
4345 t
= build_nop (TREE_TYPE (t
), t
);
4346 TREE_CONSTANT (t
) = false;
4351 r
= cxx_eval_outermost_constant_expr (t
, true, true, decl
);
4352 gcc_checking_assert (r
== t
4353 || CONVERT_EXPR_P (t
)
4354 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
4355 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
4356 || !cp_tree_equal (r
, t
));
4360 static GTY((deletable
)) hash_map
<tree
, tree
> *cv_cache
;
4362 /* If T is a constant expression, returns its reduced value.
4363 Otherwise, if T does not have TREE_CONSTANT set, returns T.
4364 Otherwise, returns a version of T without TREE_CONSTANT. */
4367 maybe_constant_value (tree t
, tree decl
)
4369 if (cv_cache
== NULL
)
4370 cv_cache
= hash_map
<tree
, tree
>::create_ggc (101);
4372 if (tree
*cached
= cv_cache
->get (t
))
4375 tree ret
= maybe_constant_value_1 (t
, decl
);
4376 cv_cache
->put (t
, ret
);
4380 /* Dispose of the whole CV_CACHE. */
4383 clear_cv_cache (void)
4385 if (cv_cache
!= NULL
)
4389 /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
4392 clear_cv_and_fold_caches (void)
4395 clear_fold_cache ();
4398 /* Like maybe_constant_value but first fully instantiate the argument.
4400 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
4401 (t, tf_none) followed by maybe_constant_value but is more efficient,
4402 because calls instantiation_dependent_expression_p and
4403 potential_constant_expression at most once. */
4406 fold_non_dependent_expr (tree t
)
4411 /* If we're in a template, but T isn't value dependent, simplify
4412 it. We're supposed to treat:
4414 template <typename T> void f(T[1 + 1]);
4415 template <typename T> void f(T[2]);
4417 as two declarations of the same function, for example. */
4418 if (processing_template_decl
)
4420 if (potential_nondependent_constant_expression (t
))
4422 processing_template_decl_sentinel s
;
4423 t
= instantiate_non_dependent_expr_internal (t
, tf_none
);
4425 if (type_unknown_p (t
)
4426 || BRACE_ENCLOSED_INITIALIZER_P (t
))
4428 if (TREE_OVERFLOW_P (t
))
4430 t
= build_nop (TREE_TYPE (t
), t
);
4431 TREE_CONSTANT (t
) = false;
4436 tree r
= cxx_eval_outermost_constant_expr (t
, true, true, NULL_TREE
);
4437 /* cp_tree_equal looks through NOPs, so allow them. */
4438 gcc_checking_assert (r
== t
4439 || CONVERT_EXPR_P (t
)
4440 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
4441 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
4442 || !cp_tree_equal (r
, t
));
4445 else if (TREE_OVERFLOW_P (t
))
4447 t
= build_nop (TREE_TYPE (t
), t
);
4448 TREE_CONSTANT (t
) = false;
4453 return maybe_constant_value (t
);
4456 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
4457 than wrapped in a TARGET_EXPR. */
4460 maybe_constant_init (tree t
, tree decl
)
4464 if (TREE_CODE (t
) == EXPR_STMT
)
4465 t
= TREE_OPERAND (t
, 0);
4466 if (TREE_CODE (t
) == CONVERT_EXPR
4467 && VOID_TYPE_P (TREE_TYPE (t
)))
4468 t
= TREE_OPERAND (t
, 0);
4469 if (TREE_CODE (t
) == INIT_EXPR
)
4470 t
= TREE_OPERAND (t
, 1);
4471 if (!potential_nondependent_static_init_expression (t
))
4472 /* Don't try to evaluate it. */;
4474 t
= cxx_eval_outermost_constant_expr (t
, true, false, decl
);
4475 if (TREE_CODE (t
) == TARGET_EXPR
)
4477 tree init
= TARGET_EXPR_INITIAL (t
);
4478 if (TREE_CODE (init
) == CONSTRUCTOR
)
4485 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
4486 /* Return true if the object referred to by REF has automatic or thread
4489 enum { ck_ok
, ck_bad
, ck_unknown
};
4491 check_automatic_or_tls (tree ref
)
4494 HOST_WIDE_INT bitsize
, bitpos
;
4496 int volatilep
= 0, unsignedp
= 0;
4497 tree decl
= get_inner_reference (ref
, &bitsize
, &bitpos
, &offset
,
4498 &mode
, &unsignedp
, &volatilep
, false);
4501 /* If there isn't a decl in the middle, we don't know the linkage here,
4502 and this isn't a constant expression anyway. */
4505 dk
= decl_storage_duration (decl
);
4506 return (dk
== dk_auto
|| dk
== dk_thread
) ? ck_bad
: ck_ok
;
4510 /* Return true if T denotes a potentially constant expression. Issue
4511 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
4512 an lvalue-rvalue conversion is implied.
4514 C++0x [expr.const] used to say
4516 6 An expression is a potential constant expression if it is
4517 a constant expression where all occurrences of function
4518 parameters are replaced by arbitrary constant expressions
4519 of the appropriate type.
4521 2 A conditional expression is a constant expression unless it
4522 involves one of the following as a potentially evaluated
4523 subexpression (3.2), but subexpressions of logical AND (5.14),
4524 logical OR (5.15), and conditional (5.16) operations that are
4525 not evaluated are not considered. */
4528 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
,
4529 tsubst_flags_t flags
)
4531 #define RECUR(T,RV) potential_constant_expression_1 ((T), (RV), strict, flags)
4532 enum { any
= false, rval
= true };
4536 if (t
== error_mark_node
)
4540 if (TREE_THIS_VOLATILE (t
) && !DECL_P (t
))
4542 if (flags
& tf_error
)
4543 error ("expression %qE has side-effects", t
);
4546 if (CONSTANT_CLASS_P (t
))
4549 switch (TREE_CODE (t
))
4555 case TEMPLATE_ID_EXPR
:
4558 case CASE_LABEL_EXPR
:
4564 case TEMPLATE_PARM_INDEX
:
4566 case IDENTIFIER_NODE
:
4567 case USERDEF_LITERAL
:
4568 /* We can see a FIELD_DECL in a pointer-to-member expression. */
4574 case PLACEHOLDER_EXPR
:
4580 case AGGR_INIT_EXPR
:
4582 /* -- an invocation of a function other than a constexpr function
4583 or a constexpr constructor. */
4585 tree fun
= get_function_named_in_call (t
);
4586 const int nargs
= call_expr_nargs (t
);
4589 if (fun
== NULL_TREE
)
4591 if (TREE_CODE (t
) == CALL_EXPR
4592 && CALL_EXPR_FN (t
) == NULL_TREE
)
4593 switch (CALL_EXPR_IFN (t
))
4595 /* These should be ignored, they are optimized away from
4596 constexpr functions. */
4597 case IFN_UBSAN_NULL
:
4598 case IFN_UBSAN_BOUNDS
:
4599 case IFN_UBSAN_VPTR
:
4604 /* fold_call_expr can't do anything with IFN calls. */
4605 if (flags
& tf_error
)
4606 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
4607 "call to internal function");
4610 if (is_overloaded_fn (fun
))
4612 if (TREE_CODE (fun
) == FUNCTION_DECL
)
4614 if (builtin_valid_in_constant_expr_p (fun
))
4616 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
4617 /* Allow any built-in function; if the expansion
4618 isn't constant, we'll deal with that then. */
4619 && !is_builtin_fn (fun
))
4621 if (flags
& tf_error
)
4623 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
4624 "call to non-constexpr function %qD", fun
);
4625 explain_invalid_constexpr_fn (fun
);
4629 /* A call to a non-static member function takes the address
4630 of the object as the first argument. But in a constant
4631 expression the address will be folded away, so look
4633 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
4634 && !DECL_CONSTRUCTOR_P (fun
))
4636 tree x
= get_nth_callarg (t
, 0);
4637 if (is_this_parameter (x
))
4639 else if (!RECUR (x
, rval
))
4646 if (!RECUR (fun
, true))
4648 fun
= get_first_fn (fun
);
4650 /* Skip initial arguments to base constructors. */
4651 if (DECL_BASE_CONSTRUCTOR_P (fun
))
4652 i
= num_artificial_parms_for (fun
);
4653 fun
= DECL_ORIGIN (fun
);
4657 if (RECUR (fun
, rval
))
4658 /* Might end up being a constant function pointer. */;
4662 for (; i
< nargs
; ++i
)
4664 tree x
= get_nth_callarg (t
, i
);
4665 /* In a template, reference arguments haven't been converted to
4666 REFERENCE_TYPE and we might not even know if the parameter
4667 is a reference, so accept lvalue constants too. */
4668 bool rv
= processing_template_decl
? any
: rval
;
4675 case NON_LVALUE_EXPR
:
4676 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
4677 -- an lvalue of integral type that refers to a non-volatile
4678 const variable or static data member initialized with
4679 constant expressions, or
4681 -- an lvalue of literal type that refers to non-volatile
4682 object defined with constexpr, or that refers to a
4683 sub-object of such an object; */
4684 return RECUR (TREE_OPERAND (t
, 0), rval
);
4688 && !decl_constant_var_p (t
)
4690 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t
))
4691 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t
))
4692 && !var_in_constexpr_fn (t
)
4693 && !type_dependent_expression_p (t
))
4695 if (flags
& tf_error
)
4696 non_const_var_error (t
);
4703 case VIEW_CONVERT_EXPR
:
4704 /* -- a reinterpret_cast. FIXME not implemented, and this rule
4705 may change to something more specific to type-punning (DR 1312). */
4707 tree from
= TREE_OPERAND (t
, 0);
4708 if (POINTER_TYPE_P (TREE_TYPE (t
))
4709 && TREE_CODE (from
) == INTEGER_CST
4710 && !integer_zerop (from
))
4712 if (flags
& tf_error
)
4713 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
4714 "reinterpret_cast from integer to pointer");
4717 return (RECUR (from
, TREE_CODE (t
) != VIEW_CONVERT_EXPR
));
4721 /* -- a unary operator & that is applied to an lvalue that
4722 designates an object with thread or automatic storage
4724 t
= TREE_OPERAND (t
, 0);
4726 if (TREE_CODE (t
) == OFFSET_REF
&& PTRMEM_OK_P (t
))
4727 /* A pointer-to-member constant. */
4731 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
4732 any checking here, as we might dereference the pointer later. If
4733 we remove this code, also remove check_automatic_or_tls. */
4734 i
= check_automatic_or_tls (t
);
4739 if (flags
& tf_error
)
4740 error ("address-of an object %qE with thread local or "
4741 "automatic storage is not a constant expression", t
);
4745 return RECUR (t
, any
);
4751 /* -- a class member access unless its postfix-expression is
4752 of literal type or of pointer to literal type. */
4753 /* This test would be redundant, as it follows from the
4754 postfix-expression being a potential constant expression. */
4755 if (type_unknown_p (t
))
4757 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
4759 case EXPR_PACK_EXPANSION
:
4760 return RECUR (PACK_EXPANSION_PATTERN (t
), want_rval
);
4764 tree x
= TREE_OPERAND (t
, 0);
4766 if (is_this_parameter (x
))
4768 if (DECL_CONTEXT (x
)
4769 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x
)))
4771 if (flags
& tf_error
)
4772 error ("use of %<this%> in a constant expression");
4777 return RECUR (x
, rval
);
4780 case STATEMENT_LIST
:
4782 tree_stmt_iterator i
;
4783 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
4785 if (!RECUR (tsi_stmt (i
), any
))
4793 if (cxx_dialect
< cxx14
)
4795 if (!RECUR (TREE_OPERAND (t
, 0), any
))
4797 if (!RECUR (TREE_OPERAND (t
, 1), rval
))
4802 if (cxx_dialect
< cxx14
)
4804 if (!RECUR (TREE_OPERAND (t
, 0), rval
))
4806 if (!RECUR (TREE_OPERAND (t
, 2), rval
))
4811 if (!RECUR (DO_COND (t
), rval
))
4813 if (!RECUR (DO_BODY (t
), any
))
4818 if (!RECUR (FOR_INIT_STMT (t
), any
))
4820 if (!RECUR (FOR_COND (t
), rval
))
4822 if (!RECUR (FOR_EXPR (t
), any
))
4824 if (!RECUR (FOR_BODY (t
), any
))
4829 if (!RECUR (WHILE_COND (t
), rval
))
4831 if (!RECUR (WHILE_BODY (t
), any
))
4836 if (!RECUR (SWITCH_STMT_COND (t
), rval
))
4838 /* FIXME we don't check SWITCH_STMT_BODY currently, because even
4839 unreachable labels would be checked. */
4843 return RECUR (STMT_EXPR_STMT (t
), rval
);
4846 case DYNAMIC_CAST_EXPR
:
4847 case PSEUDO_DTOR_EXPR
:
4851 case VEC_DELETE_EXPR
:
4854 case OMP_ATOMIC_READ
:
4855 case OMP_ATOMIC_CAPTURE_OLD
:
4856 case OMP_ATOMIC_CAPTURE_NEW
:
4857 /* GCC internal stuff. */
4860 case TRANSACTION_EXPR
:
4862 case AT_ENCODE_EXPR
:
4864 if (flags
& tf_error
)
4865 error ("expression %qE is not a constant-expression", t
);
4869 /* -- a typeid expression whose operand is of polymorphic
4872 tree e
= TREE_OPERAND (t
, 0);
4873 if (!TYPE_P (e
) && !type_dependent_expression_p (e
)
4874 && TYPE_POLYMORPHIC_P (TREE_TYPE (e
)))
4876 if (flags
& tf_error
)
4877 error ("typeid-expression is not a constant expression "
4878 "because %qE is of polymorphic type", e
);
4897 case PREINCREMENT_EXPR
:
4898 case POSTINCREMENT_EXPR
:
4899 case PREDECREMENT_EXPR
:
4900 case POSTDECREMENT_EXPR
:
4901 if (cxx_dialect
< cxx14
)
4907 if (TYPE_P (TREE_OPERAND (t
, 0)))
4909 /* else fall through. */
4915 case FIX_TRUNC_EXPR
:
4919 case TRUTH_NOT_EXPR
:
4920 case FIXED_CONVERT_EXPR
:
4921 case UNARY_PLUS_EXPR
:
4922 case UNARY_LEFT_FOLD_EXPR
:
4923 case UNARY_RIGHT_FOLD_EXPR
:
4925 return RECUR (TREE_OPERAND (t
, 0), rval
);
4928 case CONST_CAST_EXPR
:
4929 case STATIC_CAST_EXPR
:
4930 case REINTERPRET_CAST_EXPR
:
4931 case IMPLICIT_CONV_EXPR
:
4932 if (cxx_dialect
< cxx11
4933 && !dependent_type_p (TREE_TYPE (t
))
4934 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
)))
4935 /* In C++98, a conversion to non-integral type can't be part of a
4936 constant expression. */
4938 if (flags
& tf_error
)
4939 error ("cast to non-integral type %qT in a constant expression",
4944 return (RECUR (TREE_OPERAND (t
, 0),
4945 TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
));
4948 return RECUR (BIND_EXPR_BODY (t
), want_rval
);
4950 case WITH_CLEANUP_EXPR
:
4951 case CLEANUP_POINT_EXPR
:
4952 case MUST_NOT_THROW_EXPR
:
4953 case TRY_CATCH_EXPR
:
4959 case NON_DEPENDENT_EXPR
:
4960 /* For convenience. */
4964 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
4966 case TRY_FINALLY_EXPR
:
4967 return (RECUR (TREE_OPERAND (t
, 0), want_rval
)
4968 && RECUR (TREE_OPERAND (t
, 1), any
));
4971 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
4974 if (!literal_type_p (TREE_TYPE (t
)))
4976 if (flags
& tf_error
)
4978 error ("temporary of non-literal type %qT in a "
4979 "constant expression", TREE_TYPE (t
));
4980 explain_non_literal_class (TREE_TYPE (t
));
4985 return RECUR (TREE_OPERAND (t
, 1), rval
);
4989 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
4990 constructor_elt
*ce
;
4991 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
4992 if (!RECUR (ce
->value
, want_rval
))
4999 gcc_assert (TREE_PURPOSE (t
) == NULL_TREE
5000 || DECL_P (TREE_PURPOSE (t
)));
5001 if (!RECUR (TREE_VALUE (t
), want_rval
))
5003 if (TREE_CHAIN (t
) == NULL_TREE
)
5005 return RECUR (TREE_CHAIN (t
), want_rval
);
5008 case TRUNC_DIV_EXPR
:
5010 case FLOOR_DIV_EXPR
:
5011 case ROUND_DIV_EXPR
:
5012 case TRUNC_MOD_EXPR
:
5014 case ROUND_MOD_EXPR
:
5016 tree denom
= TREE_OPERAND (t
, 1);
5017 if (!RECUR (denom
, rval
))
5019 /* We can't call cxx_eval_outermost_constant_expr on an expression
5020 that hasn't been through instantiate_non_dependent_expr yet. */
5021 if (!processing_template_decl
)
5022 denom
= cxx_eval_outermost_constant_expr (denom
, true);
5023 if (integer_zerop (denom
))
5025 if (flags
& tf_error
)
5026 error ("division by zero is not a constant-expression");
5032 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
5038 /* check_return_expr sometimes wraps a TARGET_EXPR in a
5039 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
5040 introduced by build_call_a. */
5041 tree op0
= TREE_OPERAND (t
, 0);
5042 tree op1
= TREE_OPERAND (t
, 1);
5044 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
5045 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
5046 return RECUR (op0
, want_rval
);
5051 /* If the first operand is the non-short-circuit constant, look at
5052 the second operand; otherwise we only care about the first one for
5054 case TRUTH_AND_EXPR
:
5055 case TRUTH_ANDIF_EXPR
:
5056 tmp
= boolean_true_node
;
5059 case TRUTH_ORIF_EXPR
:
5060 tmp
= boolean_false_node
;
5063 tree op
= TREE_OPERAND (t
, 0);
5064 if (!RECUR (op
, rval
))
5066 if (!processing_template_decl
)
5067 op
= cxx_eval_outermost_constant_expr (op
, true);
5068 if (tree_int_cst_equal (op
, tmp
))
5069 return RECUR (TREE_OPERAND (t
, 1), rval
);
5076 case POINTER_PLUS_EXPR
:
5078 case EXACT_DIV_EXPR
:
5088 case TRUTH_XOR_EXPR
:
5089 case UNORDERED_EXPR
:
5102 case ARRAY_RANGE_REF
:
5106 case BINARY_LEFT_FOLD_EXPR
:
5107 case BINARY_RIGHT_FOLD_EXPR
:
5109 for (i
= 0; i
< 2; ++i
)
5110 if (!RECUR (TREE_OPERAND (t
, i
), want_rval
))
5114 case CILK_SYNC_STMT
:
5115 case CILK_SPAWN_STMT
:
5116 case ARRAY_NOTATION_REF
:
5121 for (i
= 0; i
< 3; ++i
)
5122 if (!RECUR (TREE_OPERAND (t
, i
), true))
5127 if (COND_EXPR_IS_VEC_DELETE (t
))
5129 if (flags
& tf_error
)
5130 error_at (location_of (t
),
5131 "%<delete[]%> is not a constant-expression");
5137 /* If the condition is a known constant, we know which of the legs we
5138 care about; otherwise we only require that the condition and
5139 either of the legs be potentially constant. */
5140 tmp
= TREE_OPERAND (t
, 0);
5141 if (!RECUR (tmp
, rval
))
5143 if (!processing_template_decl
)
5144 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
5145 if (integer_zerop (tmp
))
5146 return RECUR (TREE_OPERAND (t
, 2), want_rval
);
5147 else if (TREE_CODE (tmp
) == INTEGER_CST
)
5148 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
5149 for (i
= 1; i
< 3; ++i
)
5150 if (potential_constant_expression_1 (TREE_OPERAND (t
, i
),
5151 want_rval
, strict
, tf_none
))
5153 if (flags
& tf_error
)
5154 error ("expression %qE is not a constant-expression", t
);
5158 if (VEC_INIT_EXPR_IS_CONSTEXPR (t
))
5160 if (flags
& tf_error
)
5162 error ("non-constant array initialization");
5163 diagnose_non_constexpr_vec_init (t
);
5169 /* We can see these in statement-expressions. */
5172 case EMPTY_CLASS_EXPR
:
5177 tree
*target
= &TREE_OPERAND (t
, 0);
5178 /* Gotos representing break and continue are OK; we should have
5179 rejected other gotos in parsing. */
5180 gcc_assert (breaks (target
) || continues (target
));
5185 if (objc_is_property_ref (t
))
5188 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t
)));
5195 /* The main entry point to the above. */
5198 potential_constant_expression (tree t
)
5200 return potential_constant_expression_1 (t
, false, true, tf_none
);
5204 potential_static_init_expression (tree t
)
5206 return potential_constant_expression_1 (t
, false, false, tf_none
);
5209 /* As above, but require a constant rvalue. */
5212 potential_rvalue_constant_expression (tree t
)
5214 return potential_constant_expression_1 (t
, true, true, tf_none
);
5217 /* Like above, but complain about non-constant expressions. */
5220 require_potential_constant_expression (tree t
)
5222 return potential_constant_expression_1 (t
, false, true, tf_warning_or_error
);
5225 /* Cross product of the above. */
5228 require_potential_rvalue_constant_expression (tree t
)
5230 return potential_constant_expression_1 (t
, true, true, tf_warning_or_error
);
5233 /* Returns true if T is a potential constant expression that is not
5234 instantiation-dependent, and therefore a candidate for constant folding even
5238 potential_nondependent_constant_expression (tree t
)
5240 return (!type_unknown_p (t
)
5241 && !BRACE_ENCLOSED_INITIALIZER_P (t
)
5242 && potential_constant_expression (t
)
5243 && !instantiation_dependent_expression_p (t
));
5246 /* Returns true if T is a potential static initializer expression that is not
5247 instantiation-dependent. */
5250 potential_nondependent_static_init_expression (tree t
)
5252 return (!type_unknown_p (t
)
5253 && !BRACE_ENCLOSED_INITIALIZER_P (t
)
5254 && potential_static_init_expression (t
)
5255 && !instantiation_dependent_expression_p (t
));
5258 /* Finalize constexpr processing after parsing. */
5261 fini_constexpr (void)
5263 /* The contexpr call and fundef copies tables are no longer needed. */
5264 constexpr_call_table
= NULL
;
5265 fundef_copies_table
= NULL
;
5268 #include "gt-cp-constexpr.h"