1 /* Perform the semantic phase of constexpr parsing, i.e., the process of
2 building tree structure, checking semantic consistency, and
3 building RTL. These routines are used both during actual parsing
4 and during the instantiation of template functions.
6 Copyright (C) 1998-2014 Free Software Foundation, Inc.
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
15 GCC is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
26 #include "coretypes.h"
30 #include "c-family/c-objc.h"
31 #include "tree-iterator.h"
35 static bool verify_constant (tree
, bool, bool *, bool *);
36 #define VERIFY_CONSTANT(X) \
38 if (verify_constant ((X), allow_non_constant, 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
)
60 || TREE_CODE (t
) == VECTOR_TYPE
61 || TREE_CODE (t
) == REFERENCE_TYPE
)
65 t
= complete_type (t
);
66 gcc_assert (COMPLETE_TYPE_P (t
) || errorcount
);
67 return CLASSTYPE_LITERAL_P (t
);
69 if (TREE_CODE (t
) == ARRAY_TYPE
)
70 return literal_type_p (strip_array_types (t
));
74 /* If DECL is a variable declared `constexpr', require its type
75 be literal. Return the DECL if OK, otherwise NULL. */
78 ensure_literal_type_for_constexpr_object (tree decl
)
80 tree type
= TREE_TYPE (decl
);
82 && (DECL_DECLARED_CONSTEXPR_P (decl
)
83 || var_in_constexpr_fn (decl
))
84 && !processing_template_decl
)
86 tree stype
= strip_array_types (type
);
87 if (CLASS_TYPE_P (stype
) && !COMPLETE_TYPE_P (complete_type (stype
)))
88 /* Don't complain here, we'll complain about incompleteness
89 when we try to initialize the variable. */;
90 else if (!literal_type_p (type
))
92 if (DECL_DECLARED_CONSTEXPR_P (decl
))
93 error ("the type %qT of constexpr variable %qD is not literal",
96 error ("variable %qD of non-literal type %qT in %<constexpr%> "
97 "function", decl
, type
);
98 explain_non_literal_class (type
);
105 /* Representation of entries in the constexpr function definition table. */
107 struct GTY((for_user
)) constexpr_fundef
{
112 struct constexpr_fundef_hasher
: ggc_hasher
<constexpr_fundef
*>
114 static hashval_t
hash (constexpr_fundef
*);
115 static bool equal (constexpr_fundef
*, constexpr_fundef
*);
118 /* This table holds all constexpr function definitions seen in
119 the current translation unit. */
121 static GTY (()) hash_table
<constexpr_fundef_hasher
> *constexpr_fundef_table
;
123 /* Utility function used for managing the constexpr function table.
124 Return true if the entries pointed to by P and Q are for the
125 same constexpr function. */
128 constexpr_fundef_hasher::equal (constexpr_fundef
*lhs
, constexpr_fundef
*rhs
)
130 return lhs
->decl
== rhs
->decl
;
133 /* Utility function used for managing the constexpr function table.
134 Return a hash value for the entry pointed to by Q. */
137 constexpr_fundef_hasher::hash (constexpr_fundef
*fundef
)
139 return DECL_UID (fundef
->decl
);
142 /* Return a previously saved definition of function FUN. */
144 static constexpr_fundef
*
145 retrieve_constexpr_fundef (tree fun
)
147 constexpr_fundef fundef
= { NULL
, NULL
};
148 if (constexpr_fundef_table
== NULL
)
152 return constexpr_fundef_table
->find (&fundef
);
155 /* Check whether the parameter and return types of FUN are valid for a
156 constexpr function, and complain if COMPLAIN. */
159 is_valid_constexpr_fn (tree fun
, bool complain
)
163 if (DECL_INHERITED_CTOR_BASE (fun
)
164 && TREE_CODE (fun
) == TEMPLATE_DECL
)
168 error ("inherited constructor %qD is not constexpr",
169 get_inherited_ctor (fun
));
173 for (tree parm
= FUNCTION_FIRST_USER_PARM (fun
);
174 parm
!= NULL_TREE
; parm
= TREE_CHAIN (parm
))
175 if (!literal_type_p (TREE_TYPE (parm
)))
180 error ("invalid type for parameter %d of constexpr "
181 "function %q+#D", DECL_PARM_INDEX (parm
), fun
);
182 explain_non_literal_class (TREE_TYPE (parm
));
187 if (!DECL_CONSTRUCTOR_P (fun
))
189 tree rettype
= TREE_TYPE (TREE_TYPE (fun
));
190 if (!literal_type_p (rettype
))
195 error ("invalid return type %qT of constexpr function %q+D",
197 explain_non_literal_class (rettype
);
201 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
202 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun
)))
207 error ("enclosing class of constexpr non-static member "
208 "function %q+#D is not a literal type", fun
);
209 explain_non_literal_class (DECL_CONTEXT (fun
));
213 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun
)))
217 error ("%q#T has virtual base classes", DECL_CONTEXT (fun
));
223 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
224 for a member of an anonymous aggregate, INIT is the initializer for that
225 member, and VEC_OUTER is the vector of constructor elements for the class
226 whose constructor we are processing. Add the initializer to the vector
227 and return true to indicate success. */
230 build_anon_member_initialization (tree member
, tree init
,
231 vec
<constructor_elt
, va_gc
> **vec_outer
)
233 /* MEMBER presents the relevant fields from the inside out, but we need
234 to build up the initializer from the outside in so that we can reuse
235 previously built CONSTRUCTORs if this is, say, the second field in an
236 anonymous struct. So we use a vec as a stack. */
237 auto_vec
<tree
, 2> fields
;
240 fields
.safe_push (TREE_OPERAND (member
, 1));
241 member
= TREE_OPERAND (member
, 0);
243 while (ANON_AGGR_TYPE_P (TREE_TYPE (member
))
244 && TREE_CODE (member
) == COMPONENT_REF
);
246 /* VEC has the constructor elements vector for the context of FIELD.
247 If FIELD is an anonymous aggregate, we will push inside it. */
248 vec
<constructor_elt
, va_gc
> **vec
= vec_outer
;
250 while (field
= fields
.pop(),
251 ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
254 /* If there is already an outer constructor entry for the anonymous
255 aggregate FIELD, use it; otherwise, insert one. */
256 if (vec_safe_is_empty (*vec
)
257 || (*vec
)->last().index
!= field
)
259 ctor
= build_constructor (TREE_TYPE (field
), NULL
);
260 CONSTRUCTOR_APPEND_ELT (*vec
, field
, ctor
);
263 ctor
= (*vec
)->last().value
;
264 vec
= &CONSTRUCTOR_ELTS (ctor
);
267 /* Now we're at the innermost field, the one that isn't an anonymous
268 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
269 gcc_assert (fields
.is_empty());
270 CONSTRUCTOR_APPEND_ELT (*vec
, field
, init
);
275 /* Subroutine of build_constexpr_constructor_member_initializers.
276 The expression tree T represents a data member initialization
277 in a (constexpr) constructor definition. Build a pairing of
278 the data member with its initializer, and prepend that pair
279 to the existing initialization pair INITS. */
282 build_data_member_initialization (tree t
, vec
<constructor_elt
, va_gc
> **vec
)
285 if (TREE_CODE (t
) == CLEANUP_POINT_EXPR
)
286 t
= TREE_OPERAND (t
, 0);
287 if (TREE_CODE (t
) == EXPR_STMT
)
288 t
= TREE_OPERAND (t
, 0);
289 if (t
== error_mark_node
)
291 if (TREE_CODE (t
) == STATEMENT_LIST
)
293 tree_stmt_iterator i
;
294 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
296 if (! build_data_member_initialization (tsi_stmt (i
), vec
))
301 if (TREE_CODE (t
) == CLEANUP_STMT
)
303 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
304 but we can in a constexpr constructor for a non-literal class. Just
305 ignore it; either all the initialization will be constant, in which
306 case the cleanup can't run, or it can't be constexpr.
307 Still recurse into CLEANUP_BODY. */
308 return build_data_member_initialization (CLEANUP_BODY (t
), vec
);
310 if (TREE_CODE (t
) == CONVERT_EXPR
)
311 t
= TREE_OPERAND (t
, 0);
312 if (TREE_CODE (t
) == INIT_EXPR
313 || TREE_CODE (t
) == MODIFY_EXPR
)
315 member
= TREE_OPERAND (t
, 0);
316 init
= break_out_target_exprs (TREE_OPERAND (t
, 1));
318 else if (TREE_CODE (t
) == CALL_EXPR
)
320 member
= CALL_EXPR_ARG (t
, 0);
321 /* We don't use build_cplus_new here because it complains about
322 abstract bases. Leaving the call unwrapped means that it has the
323 wrong type, but cxx_eval_constant_expression doesn't care. */
324 init
= break_out_target_exprs (t
);
326 else if (TREE_CODE (t
) == BIND_EXPR
)
327 return build_data_member_initialization (BIND_EXPR_BODY (t
), vec
);
328 else if (TREE_CODE (t
) == DECL_EXPR
329 || TREE_CODE (t
) == USING_STMT
)
330 /* Declaring a temporary, don't add it to the CONSTRUCTOR.
331 Likewise for using directives. */
335 if (INDIRECT_REF_P (member
))
336 member
= TREE_OPERAND (member
, 0);
337 if (TREE_CODE (member
) == NOP_EXPR
)
341 if (TREE_CODE (op
) == ADDR_EXPR
)
343 gcc_assert (same_type_ignoring_top_level_qualifiers_p
344 (TREE_TYPE (TREE_TYPE (op
)),
345 TREE_TYPE (TREE_TYPE (member
))));
346 /* Initializing a cv-qualified member; we need to look through
350 else if (op
== current_class_ptr
351 && (same_type_ignoring_top_level_qualifiers_p
352 (TREE_TYPE (TREE_TYPE (member
)),
353 current_class_type
)))
354 /* Delegating constructor. */
358 /* This is an initializer for an empty base; keep it for now so
359 we can check it in cxx_eval_bare_aggregate. */
360 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member
))));
363 if (TREE_CODE (member
) == ADDR_EXPR
)
364 member
= TREE_OPERAND (member
, 0);
365 if (TREE_CODE (member
) == COMPONENT_REF
)
367 tree aggr
= TREE_OPERAND (member
, 0);
368 if (TREE_CODE (aggr
) != COMPONENT_REF
)
369 /* Normal member initialization. */
370 member
= TREE_OPERAND (member
, 1);
371 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr
)))
372 /* Initializing a member of an anonymous union. */
373 return build_anon_member_initialization (member
, init
, vec
);
375 /* We're initializing a vtable pointer in a base. Leave it as
376 COMPONENT_REF so we remember the path to get to the vfield. */
377 gcc_assert (TREE_TYPE (member
) == vtbl_ptr_type_node
);
380 CONSTRUCTOR_APPEND_ELT (*vec
, member
, init
);
384 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
385 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
386 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
389 check_constexpr_bind_expr_vars (tree t
)
391 gcc_assert (TREE_CODE (t
) == BIND_EXPR
);
393 if (cxx_dialect
>= cxx14
)
396 for (tree var
= BIND_EXPR_VARS (t
); var
; var
= DECL_CHAIN (var
))
397 if (TREE_CODE (var
) == TYPE_DECL
398 && DECL_IMPLICIT_TYPEDEF_P (var
))
403 /* Subroutine of check_constexpr_ctor_body. */
406 check_constexpr_ctor_body_1 (tree last
, tree list
)
408 switch (TREE_CODE (list
))
411 if (TREE_CODE (DECL_EXPR_DECL (list
)) == USING_DECL
)
413 if (cxx_dialect
>= cxx14
)
417 case CLEANUP_POINT_EXPR
:
418 return check_constexpr_ctor_body (last
, TREE_OPERAND (list
, 0),
422 if (!check_constexpr_bind_expr_vars (list
)
423 || !check_constexpr_ctor_body (last
, BIND_EXPR_BODY (list
),
437 /* Make sure that there are no statements after LAST in the constructor
438 body represented by LIST. */
441 check_constexpr_ctor_body (tree last
, tree list
, bool complain
)
444 if (TREE_CODE (list
) == STATEMENT_LIST
)
446 tree_stmt_iterator i
= tsi_last (list
);
447 for (; !tsi_end_p (i
); tsi_prev (&i
))
449 tree t
= tsi_stmt (i
);
452 if (!check_constexpr_ctor_body_1 (last
, t
))
459 else if (list
!= last
460 && !check_constexpr_ctor_body_1 (last
, list
))
465 error ("constexpr constructor does not have empty body");
466 DECL_DECLARED_CONSTEXPR_P (current_function_decl
) = false;
471 /* V is a vector of constructor elements built up for the base and member
472 initializers of a constructor for TYPE. They need to be in increasing
473 offset order, which they might not be yet if TYPE has a primary base
474 which is not first in the base-clause or a vptr and at least one base
475 all of which are non-primary. */
477 static vec
<constructor_elt
, va_gc
> *
478 sort_constexpr_mem_initializers (tree type
, vec
<constructor_elt
, va_gc
> *v
)
480 tree pri
= CLASSTYPE_PRIMARY_BINFO (type
);
486 field_type
= BINFO_TYPE (pri
);
487 else if (TYPE_CONTAINS_VPTR_P (type
))
488 field_type
= vtbl_ptr_type_node
;
492 /* Find the element for the primary base or vptr and move it to the
493 beginning of the vec. */
494 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
495 if (TREE_TYPE (ce
->index
) == field_type
)
498 if (i
> 0 && i
< vec_safe_length (v
))
500 vec
<constructor_elt
, va_gc
> &vref
= *v
;
501 constructor_elt elt
= vref
[i
];
510 /* Build compile-time evalable representations of member-initializer list
511 for a constexpr constructor. */
514 build_constexpr_constructor_member_initializers (tree type
, tree body
)
516 vec
<constructor_elt
, va_gc
> *vec
= NULL
;
518 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
519 || TREE_CODE (body
) == EH_SPEC_BLOCK
)
520 body
= TREE_OPERAND (body
, 0);
521 if (TREE_CODE (body
) == STATEMENT_LIST
)
522 body
= STATEMENT_LIST_HEAD (body
)->stmt
;
523 body
= BIND_EXPR_BODY (body
);
524 if (TREE_CODE (body
) == CLEANUP_POINT_EXPR
)
526 body
= TREE_OPERAND (body
, 0);
527 if (TREE_CODE (body
) == EXPR_STMT
)
528 body
= TREE_OPERAND (body
, 0);
529 if (TREE_CODE (body
) == INIT_EXPR
530 && (same_type_ignoring_top_level_qualifiers_p
531 (TREE_TYPE (TREE_OPERAND (body
, 0)),
532 current_class_type
)))
535 return TREE_OPERAND (body
, 1);
537 ok
= build_data_member_initialization (body
, &vec
);
539 else if (TREE_CODE (body
) == STATEMENT_LIST
)
541 tree_stmt_iterator i
;
542 for (i
= tsi_start (body
); !tsi_end_p (i
); tsi_next (&i
))
544 ok
= build_data_member_initialization (tsi_stmt (i
), &vec
);
549 else if (TREE_CODE (body
) == TRY_BLOCK
)
551 error ("body of %<constexpr%> constructor cannot be "
552 "a function-try-block");
553 return error_mark_node
;
555 else if (EXPR_P (body
))
556 ok
= build_data_member_initialization (body
, &vec
);
558 gcc_assert (errorcount
> 0);
561 if (vec_safe_length (vec
) > 0)
563 /* In a delegating constructor, return the target. */
564 constructor_elt
*ce
= &(*vec
)[0];
565 if (ce
->index
== current_class_ptr
)
572 vec
= sort_constexpr_mem_initializers (type
, vec
);
573 return build_constructor (type
, vec
);
576 return error_mark_node
;
579 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
580 declared to be constexpr, or a sub-statement thereof. Returns the
581 return value if suitable, error_mark_node for a statement not allowed in
582 a constexpr function, or NULL_TREE if no return value was found. */
585 constexpr_fn_retval (tree body
)
587 switch (TREE_CODE (body
))
591 tree_stmt_iterator i
;
592 tree expr
= NULL_TREE
;
593 for (i
= tsi_start (body
); !tsi_end_p (i
); tsi_next (&i
))
595 tree s
= constexpr_fn_retval (tsi_stmt (i
));
596 if (s
== error_mark_node
)
597 return error_mark_node
;
598 else if (s
== NULL_TREE
)
599 /* Keep iterating. */;
601 /* Multiple return statements. */
602 return error_mark_node
;
610 return break_out_target_exprs (TREE_OPERAND (body
, 0));
613 if (TREE_CODE (DECL_EXPR_DECL (body
)) == USING_DECL
)
615 if (cxx_dialect
>= cxx14
)
617 return error_mark_node
;
619 case CLEANUP_POINT_EXPR
:
620 return constexpr_fn_retval (TREE_OPERAND (body
, 0));
623 if (!check_constexpr_bind_expr_vars (body
))
624 return error_mark_node
;
625 return constexpr_fn_retval (BIND_EXPR_BODY (body
));
631 return error_mark_node
;
635 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
636 FUN; do the necessary transformations to turn it into a single expression
637 that we can store in the hash table. */
640 massage_constexpr_body (tree fun
, tree body
)
642 if (DECL_CONSTRUCTOR_P (fun
))
643 body
= build_constexpr_constructor_member_initializers
644 (DECL_CONTEXT (fun
), body
);
647 if (TREE_CODE (body
) == EH_SPEC_BLOCK
)
648 body
= EH_SPEC_STMTS (body
);
649 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
650 body
= TREE_OPERAND (body
, 0);
651 body
= constexpr_fn_retval (body
);
656 /* FUN is a constexpr constructor with massaged body BODY. Return true
657 if some bases/fields are uninitialized, and complain if COMPLAIN. */
660 cx_check_missing_mem_inits (tree fun
, tree body
, bool complain
)
667 if (TREE_CODE (body
) != CONSTRUCTOR
)
670 nelts
= CONSTRUCTOR_NELTS (body
);
671 ctype
= DECL_CONTEXT (fun
);
672 field
= TYPE_FIELDS (ctype
);
674 if (TREE_CODE (ctype
) == UNION_TYPE
)
676 if (nelts
== 0 && next_initializable_field (field
))
679 error ("%<constexpr%> constructor for union %qT must "
680 "initialize exactly one non-static data member", ctype
);
687 for (i
= 0; i
<= nelts
; ++i
)
694 index
= CONSTRUCTOR_ELT (body
, i
)->index
;
695 /* Skip base and vtable inits. */
696 if (TREE_CODE (index
) != FIELD_DECL
697 || DECL_ARTIFICIAL (index
))
700 for (; field
!= index
; field
= DECL_CHAIN (field
))
703 if (TREE_CODE (field
) != FIELD_DECL
704 || (DECL_C_BIT_FIELD (field
) && !DECL_NAME (field
))
705 || DECL_ARTIFICIAL (field
))
707 ftype
= strip_array_types (TREE_TYPE (field
));
708 if (type_has_constexpr_default_constructor (ftype
))
710 /* It's OK to skip a member with a trivial constexpr ctor.
711 A constexpr ctor that isn't trivial should have been
713 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype
)
719 error ("uninitialized member %qD in %<constexpr%> constructor",
723 if (field
== NULL_TREE
)
725 field
= DECL_CHAIN (field
);
731 /* We are processing the definition of the constexpr function FUN.
732 Check that its BODY fulfills the propriate requirements and
733 enter it in the constexpr function definition table.
734 For constructor BODY is actually the TREE_LIST of the
735 member-initializer list. */
738 register_constexpr_fundef (tree fun
, tree body
)
740 constexpr_fundef entry
;
741 constexpr_fundef
**slot
;
743 if (!is_valid_constexpr_fn (fun
, !DECL_GENERATED_P (fun
)))
746 body
= massage_constexpr_body (fun
, body
);
747 if (body
== NULL_TREE
|| body
== error_mark_node
)
749 if (!DECL_CONSTRUCTOR_P (fun
))
750 error ("body of constexpr function %qD not a return-statement", fun
);
754 if (!potential_rvalue_constant_expression (body
))
756 if (!DECL_GENERATED_P (fun
))
757 require_potential_rvalue_constant_expression (body
);
761 if (DECL_CONSTRUCTOR_P (fun
)
762 && cx_check_missing_mem_inits (fun
, body
, !DECL_GENERATED_P (fun
)))
765 /* Create the constexpr function table if necessary. */
766 if (constexpr_fundef_table
== NULL
)
767 constexpr_fundef_table
768 = hash_table
<constexpr_fundef_hasher
>::create_ggc (101);
772 slot
= constexpr_fundef_table
->find_slot (&entry
, INSERT
);
774 gcc_assert (*slot
== NULL
);
775 *slot
= ggc_alloc
<constexpr_fundef
> ();
781 /* FUN is a non-constexpr function called in a context that requires a
782 constant expression. If it comes from a constexpr template, explain why
783 the instantiation isn't constexpr. */
786 explain_invalid_constexpr_fn (tree fun
)
788 static hash_set
<tree
> *diagnosed
;
791 /* Only diagnose defaulted functions or instantiations. */
792 if (!DECL_DEFAULTED_FN (fun
)
793 && !is_instantiation_of_constexpr (fun
))
795 if (diagnosed
== NULL
)
796 diagnosed
= new hash_set
<tree
>;
797 if (diagnosed
->add (fun
))
798 /* Already explained. */
801 save_loc
= input_location
;
802 input_location
= DECL_SOURCE_LOCATION (fun
);
803 inform (0, "%q+D is not usable as a constexpr function because:", fun
);
804 /* First check the declaration. */
805 if (is_valid_constexpr_fn (fun
, true))
807 /* Then if it's OK, the body. */
808 if (!DECL_DECLARED_CONSTEXPR_P (fun
))
809 explain_implicit_non_constexpr (fun
);
812 body
= massage_constexpr_body (fun
, DECL_SAVED_TREE (fun
));
813 require_potential_rvalue_constant_expression (body
);
814 if (DECL_CONSTRUCTOR_P (fun
))
815 cx_check_missing_mem_inits (fun
, body
, true);
818 input_location
= save_loc
;
821 /* Objects of this type represent calls to constexpr functions
822 along with the bindings of parameters to their arguments, for
823 the purpose of compile time evaluation. */
825 struct GTY((for_user
)) constexpr_call
{
826 /* Description of the constexpr function definition. */
827 constexpr_fundef
*fundef
;
828 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
829 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
830 Note: This arrangement is made to accommodate the use of
831 iterative_hash_template_arg (see pt.c). If you change this
832 representation, also change the hash calculation in
833 cxx_eval_call_expression. */
835 /* Result of the call.
836 NULL means the call is being evaluated.
837 error_mark_node means that the evaluation was erroneous;
838 otherwise, the actuall value of the call. */
840 /* The hash of this call; we remember it here to avoid having to
841 recalculate it when expanding the hash table. */
845 struct constexpr_call_hasher
: ggc_hasher
<constexpr_call
*>
847 static hashval_t
hash (constexpr_call
*);
848 static bool equal (constexpr_call
*, constexpr_call
*);
851 /* A table of all constexpr calls that have been evaluated by the
852 compiler in this translation unit. */
854 static GTY (()) hash_table
<constexpr_call_hasher
> *constexpr_call_table
;
856 static tree
cxx_eval_constant_expression (const constexpr_call
*, tree
,
857 bool, bool, bool *, bool *);
859 /* Compute a hash value for a constexpr call representation. */
862 constexpr_call_hasher::hash (constexpr_call
*info
)
867 /* Return true if the objects pointed to by P and Q represent calls
868 to the same constexpr function with the same arguments.
869 Otherwise, return false. */
872 constexpr_call_hasher::equal (constexpr_call
*lhs
, constexpr_call
*rhs
)
878 if (!constexpr_fundef_hasher::equal (lhs
->fundef
, rhs
->fundef
))
880 lhs_bindings
= lhs
->bindings
;
881 rhs_bindings
= rhs
->bindings
;
882 while (lhs_bindings
!= NULL
&& rhs_bindings
!= NULL
)
884 tree lhs_arg
= TREE_VALUE (lhs_bindings
);
885 tree rhs_arg
= TREE_VALUE (rhs_bindings
);
886 gcc_assert (TREE_TYPE (lhs_arg
) == TREE_TYPE (rhs_arg
));
887 if (!cp_tree_equal (lhs_arg
, rhs_arg
))
889 lhs_bindings
= TREE_CHAIN (lhs_bindings
);
890 rhs_bindings
= TREE_CHAIN (rhs_bindings
);
892 return lhs_bindings
== rhs_bindings
;
895 /* Initialize the constexpr call table, if needed. */
898 maybe_initialize_constexpr_call_table (void)
900 if (constexpr_call_table
== NULL
)
901 constexpr_call_table
= hash_table
<constexpr_call_hasher
>::create_ggc (101);
904 /* We have an expression tree T that represents a call, either CALL_EXPR
905 or AGGR_INIT_EXPR. If the call is lexically to a named function,
906 retrun the _DECL for that function. */
909 get_function_named_in_call (tree t
)
912 switch (TREE_CODE (t
))
915 fun
= CALL_EXPR_FN (t
);
919 fun
= AGGR_INIT_EXPR_FN (t
);
926 if (TREE_CODE (fun
) == ADDR_EXPR
927 && TREE_CODE (TREE_OPERAND (fun
, 0)) == FUNCTION_DECL
)
928 fun
= TREE_OPERAND (fun
, 0);
932 /* We have an expression tree T that represents a call, either CALL_EXPR
933 or AGGR_INIT_EXPR. Return the Nth argument. */
936 get_nth_callarg (tree t
, int n
)
938 switch (TREE_CODE (t
))
941 return CALL_EXPR_ARG (t
, n
);
944 return AGGR_INIT_EXPR_ARG (t
, n
);
952 /* Look up the binding of the function parameter T in a constexpr
953 function call context CALL. */
956 lookup_parameter_binding (const constexpr_call
*call
, tree t
)
958 tree b
= purpose_member (t
, call
->bindings
);
959 return TREE_VALUE (b
);
962 /* Attempt to evaluate T which represents a call to a builtin function.
963 We assume here that all builtin functions evaluate to scalar types
964 represented by _CST nodes. */
967 cxx_eval_builtin_function_call (const constexpr_call
*call
, tree t
,
968 bool allow_non_constant
, bool addr
,
969 bool *non_constant_p
, bool *overflow_p
)
971 const int nargs
= call_expr_nargs (t
);
972 tree
*args
= (tree
*) alloca (nargs
* sizeof (tree
));
975 for (i
= 0; i
< nargs
; ++i
)
977 args
[i
] = cxx_eval_constant_expression (call
, CALL_EXPR_ARG (t
, i
),
978 allow_non_constant
, addr
,
979 non_constant_p
, overflow_p
);
980 if (allow_non_constant
&& *non_constant_p
)
985 new_call
= build_call_array_loc (EXPR_LOCATION (t
), TREE_TYPE (t
),
986 CALL_EXPR_FN (t
), nargs
, args
);
987 new_call
= fold (new_call
);
988 VERIFY_CONSTANT (new_call
);
992 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
993 the type of the value to match. */
996 adjust_temp_type (tree type
, tree temp
)
998 if (TREE_TYPE (temp
) == type
)
1000 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1001 if (TREE_CODE (temp
) == CONSTRUCTOR
)
1002 return build_constructor (type
, CONSTRUCTOR_ELTS (temp
));
1003 gcc_assert (scalarish_type_p (type
));
1004 return cp_fold_convert (type
, temp
);
1007 /* Subroutine of cxx_eval_call_expression.
1008 We are processing a call expression (either CALL_EXPR or
1009 AGGR_INIT_EXPR) in the call context of OLD_CALL. Evaluate
1010 all arguments and bind their values to correspondings
1011 parameters, making up the NEW_CALL context. */
1014 cxx_bind_parameters_in_call (const constexpr_call
*old_call
, tree t
,
1015 constexpr_call
*new_call
,
1016 bool allow_non_constant
,
1017 bool *non_constant_p
, bool *overflow_p
)
1019 const int nargs
= call_expr_nargs (t
);
1020 tree fun
= new_call
->fundef
->decl
;
1021 tree parms
= DECL_ARGUMENTS (fun
);
1023 for (i
= 0; i
< nargs
; ++i
)
1026 tree type
= parms
? TREE_TYPE (parms
) : void_type_node
;
1027 /* For member function, the first argument is a pointer to the implied
1028 object. And for an object construction, don't bind `this' before
1029 it is fully constructed. */
1030 if (i
== 0 && DECL_CONSTRUCTOR_P (fun
))
1032 x
= get_nth_callarg (t
, i
);
1033 if (parms
&& DECL_BY_REFERENCE (parms
))
1035 /* cp_genericize made this a reference for argument passing, but
1036 we don't want to treat it like one for constexpr evaluation. */
1037 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
1038 gcc_assert (TREE_CODE (TREE_TYPE (x
)) == REFERENCE_TYPE
);
1039 type
= TREE_TYPE (type
);
1040 x
= convert_from_reference (x
);
1042 arg
= cxx_eval_constant_expression (old_call
, x
, allow_non_constant
,
1043 TREE_CODE (type
) == REFERENCE_TYPE
,
1044 non_constant_p
, overflow_p
);
1045 /* Don't VERIFY_CONSTANT here. */
1046 if (*non_constant_p
&& allow_non_constant
)
1048 /* Just discard ellipsis args after checking their constantitude. */
1051 if (*non_constant_p
)
1052 /* Don't try to adjust the type of non-constant args. */
1055 /* Make sure the binding has the same type as the parm. */
1056 if (TREE_CODE (type
) != REFERENCE_TYPE
)
1057 arg
= adjust_temp_type (type
, arg
);
1058 new_call
->bindings
= tree_cons (parms
, arg
, new_call
->bindings
);
1060 parms
= TREE_CHAIN (parms
);
1064 /* Variables and functions to manage constexpr call expansion context.
1065 These do not need to be marked for PCH or GC. */
1067 /* FIXME remember and print actual constant arguments. */
1068 static vec
<tree
> call_stack
= vNULL
;
1069 static int call_stack_tick
;
1070 static int last_cx_error_tick
;
1073 push_cx_call_context (tree call
)
1076 if (!EXPR_HAS_LOCATION (call
))
1077 SET_EXPR_LOCATION (call
, input_location
);
1078 call_stack
.safe_push (call
);
1079 if (call_stack
.length () > (unsigned) max_constexpr_depth
)
1085 pop_cx_call_context (void)
1092 cx_error_context (void)
1094 vec
<tree
> r
= vNULL
;
1095 if (call_stack_tick
!= last_cx_error_tick
1096 && !call_stack
.is_empty ())
1098 last_cx_error_tick
= call_stack_tick
;
1102 /* Subroutine of cxx_eval_constant_expression.
1103 Evaluate the call expression tree T in the context of OLD_CALL expression
1107 cxx_eval_call_expression (const constexpr_call
*old_call
, tree t
,
1108 bool allow_non_constant
, bool addr
,
1109 bool *non_constant_p
, bool *overflow_p
)
1111 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
1112 tree fun
= get_function_named_in_call (t
);
1114 constexpr_call new_call
= { NULL
, NULL
, NULL
, 0 };
1115 constexpr_call
**slot
;
1116 constexpr_call
*entry
;
1119 if (TREE_CODE (fun
) != FUNCTION_DECL
)
1121 /* Might be a constexpr function pointer. */
1122 fun
= cxx_eval_constant_expression (old_call
, fun
, allow_non_constant
,
1123 /*addr*/false, non_constant_p
,
1126 if (TREE_CODE (fun
) == ADDR_EXPR
)
1127 fun
= TREE_OPERAND (fun
, 0);
1129 if (TREE_CODE (fun
) != FUNCTION_DECL
)
1131 if (!allow_non_constant
&& !*non_constant_p
)
1132 error_at (loc
, "expression %qE does not designate a constexpr "
1134 *non_constant_p
= true;
1137 if (DECL_CLONED_FUNCTION_P (fun
))
1138 fun
= DECL_CLONED_FUNCTION (fun
);
1139 if (is_builtin_fn (fun
))
1140 return cxx_eval_builtin_function_call (old_call
, t
, allow_non_constant
,
1141 addr
, non_constant_p
, overflow_p
);
1142 if (!DECL_DECLARED_CONSTEXPR_P (fun
))
1144 if (!allow_non_constant
)
1146 error_at (loc
, "call to non-constexpr function %qD", fun
);
1147 explain_invalid_constexpr_fn (fun
);
1149 *non_constant_p
= true;
1153 /* Shortcut trivial constructor/op=. */
1154 if (trivial_fn_p (fun
))
1156 if (call_expr_nargs (t
) == 2)
1158 tree arg
= convert_from_reference (get_nth_callarg (t
, 1));
1159 return cxx_eval_constant_expression (old_call
, arg
, allow_non_constant
,
1160 addr
, non_constant_p
, overflow_p
);
1162 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
1163 && AGGR_INIT_ZERO_FIRST (t
))
1164 return build_zero_init (DECL_CONTEXT (fun
), NULL_TREE
, false);
1167 /* If in direct recursive call, optimize definition search. */
1168 if (old_call
!= NULL
&& old_call
->fundef
->decl
== fun
)
1169 new_call
.fundef
= old_call
->fundef
;
1172 new_call
.fundef
= retrieve_constexpr_fundef (fun
);
1173 if (new_call
.fundef
== NULL
|| new_call
.fundef
->body
== NULL
)
1175 if (!allow_non_constant
)
1177 if (DECL_INITIAL (fun
))
1179 /* The definition of fun was somehow unsuitable. */
1180 error_at (loc
, "%qD called in a constant expression", fun
);
1181 explain_invalid_constexpr_fn (fun
);
1184 error_at (loc
, "%qD used before its definition", fun
);
1186 *non_constant_p
= true;
1190 cxx_bind_parameters_in_call (old_call
, t
, &new_call
,
1191 allow_non_constant
, non_constant_p
, overflow_p
);
1192 if (*non_constant_p
)
1195 depth_ok
= push_cx_call_context (t
);
1198 = iterative_hash_template_arg (new_call
.bindings
,
1199 constexpr_fundef_hasher::hash (new_call
.fundef
));
1201 /* If we have seen this call before, we are done. */
1202 maybe_initialize_constexpr_call_table ();
1203 slot
= constexpr_call_table
->find_slot (&new_call
, INSERT
);
1207 /* We need to keep a pointer to the entry, not just the slot, as the
1208 slot can move in the call to cxx_eval_builtin_function_call. */
1209 *slot
= entry
= ggc_alloc
<constexpr_call
> ();
1212 /* Calls which are in progress have their result set to NULL
1213 so that we can detect circular dependencies. */
1214 else if (entry
->result
== NULL
)
1216 if (!allow_non_constant
)
1217 error ("call has circular dependency");
1218 *non_constant_p
= true;
1219 entry
->result
= result
= error_mark_node
;
1224 if (!allow_non_constant
)
1225 error ("constexpr evaluation depth exceeds maximum of %d (use "
1226 "-fconstexpr-depth= to increase the maximum)",
1227 max_constexpr_depth
);
1228 *non_constant_p
= true;
1229 entry
->result
= result
= error_mark_node
;
1233 result
= entry
->result
;
1234 if (!result
|| result
== error_mark_node
)
1235 result
= (cxx_eval_constant_expression
1236 (&new_call
, new_call
.fundef
->body
,
1237 allow_non_constant
, addr
,
1238 non_constant_p
, overflow_p
));
1239 if (result
== error_mark_node
)
1240 *non_constant_p
= true;
1241 if (*non_constant_p
)
1242 entry
->result
= result
= error_mark_node
;
1245 /* If this was a call to initialize an object, set the type of
1246 the CONSTRUCTOR to the type of that object. */
1247 if (DECL_CONSTRUCTOR_P (fun
))
1249 tree ob_arg
= get_nth_callarg (t
, 0);
1250 STRIP_NOPS (ob_arg
);
1251 gcc_assert (TYPE_PTR_P (TREE_TYPE (ob_arg
))
1252 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (ob_arg
))));
1253 result
= adjust_temp_type (TREE_TYPE (TREE_TYPE (ob_arg
)),
1256 entry
->result
= result
;
1260 pop_cx_call_context ();
1261 return unshare_expr (result
);
1264 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
1267 reduced_constant_expression_p (tree t
)
1269 switch (TREE_CODE (t
))
1272 /* Even if we can't lower this yet, it's constant. */
1276 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
1277 tree elt
; unsigned HOST_WIDE_INT idx
;
1278 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), idx
, elt
)
1279 if (!reduced_constant_expression_p (elt
))
1284 /* FIXME are we calling this too much? */
1285 return initializer_constant_valid_p (t
, TREE_TYPE (t
)) != NULL_TREE
;
1289 /* Some expressions may have constant operands but are not constant
1290 themselves, such as 1/0. Call this function (or rather, the macro
1291 following it) to check for that condition.
1293 We only call this in places that require an arithmetic constant, not in
1294 places where we might have a non-constant expression that can be a
1295 component of a constant expression, such as the address of a constexpr
1296 variable that might be dereferenced later. */
1299 verify_constant (tree t
, bool allow_non_constant
, bool *non_constant_p
,
1302 if (!*non_constant_p
&& !reduced_constant_expression_p (t
))
1304 if (!allow_non_constant
)
1305 error ("%q+E is not a constant expression", t
);
1306 *non_constant_p
= true;
1308 if (TREE_OVERFLOW_P (t
))
1310 if (!allow_non_constant
)
1312 permerror (input_location
, "overflow in constant expression");
1313 /* If we're being permissive (and are in an enforcing
1314 context), ignore the overflow. */
1315 if (flag_permissive
)
1316 return *non_constant_p
;
1320 return *non_constant_p
;
1323 /* Subroutine of cxx_eval_constant_expression.
1324 Attempt to reduce the unary expression tree T to a compile time value.
1325 If successful, return the value. Otherwise issue a diagnostic
1326 and return error_mark_node. */
1329 cxx_eval_unary_expression (const constexpr_call
*call
, tree t
,
1330 bool allow_non_constant
, bool addr
,
1331 bool *non_constant_p
, bool *overflow_p
)
1334 tree orig_arg
= TREE_OPERAND (t
, 0);
1335 tree arg
= cxx_eval_constant_expression (call
, orig_arg
, allow_non_constant
,
1336 addr
, non_constant_p
, overflow_p
);
1337 VERIFY_CONSTANT (arg
);
1338 if (arg
== orig_arg
)
1340 r
= fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), arg
);
1341 VERIFY_CONSTANT (r
);
1345 /* Subroutine of cxx_eval_constant_expression.
1346 Like cxx_eval_unary_expression, except for binary expressions. */
1349 cxx_eval_binary_expression (const constexpr_call
*call
, tree t
,
1350 bool allow_non_constant
, bool addr
,
1351 bool *non_constant_p
, bool *overflow_p
)
1354 tree orig_lhs
= TREE_OPERAND (t
, 0);
1355 tree orig_rhs
= TREE_OPERAND (t
, 1);
1357 lhs
= cxx_eval_constant_expression (call
, orig_lhs
,
1358 allow_non_constant
, addr
,
1359 non_constant_p
, overflow_p
);
1360 VERIFY_CONSTANT (lhs
);
1361 rhs
= cxx_eval_constant_expression (call
, orig_rhs
,
1362 allow_non_constant
, addr
,
1363 non_constant_p
, overflow_p
);
1364 VERIFY_CONSTANT (rhs
);
1365 if (lhs
== orig_lhs
&& rhs
== orig_rhs
)
1367 r
= fold_build2 (TREE_CODE (t
), TREE_TYPE (t
), lhs
, rhs
);
1368 VERIFY_CONSTANT (r
);
1372 /* Subroutine of cxx_eval_constant_expression.
1373 Attempt to evaluate condition expressions. Dead branches are not
1377 cxx_eval_conditional_expression (const constexpr_call
*call
, tree t
,
1378 bool allow_non_constant
, bool addr
,
1379 bool *non_constant_p
, bool *overflow_p
)
1381 tree val
= cxx_eval_constant_expression (call
, TREE_OPERAND (t
, 0),
1382 allow_non_constant
, addr
,
1383 non_constant_p
, overflow_p
);
1384 VERIFY_CONSTANT (val
);
1385 /* Don't VERIFY_CONSTANT the other operands. */
1386 if (integer_zerop (val
))
1387 return cxx_eval_constant_expression (call
, TREE_OPERAND (t
, 2),
1388 allow_non_constant
, addr
,
1389 non_constant_p
, overflow_p
);
1390 return cxx_eval_constant_expression (call
, TREE_OPERAND (t
, 1),
1391 allow_non_constant
, addr
,
1392 non_constant_p
, overflow_p
);
1395 /* Subroutine of cxx_eval_constant_expression.
1396 Attempt to reduce a reference to an array slot. */
1399 cxx_eval_array_reference (const constexpr_call
*call
, tree t
,
1400 bool allow_non_constant
, bool addr
,
1401 bool *non_constant_p
, bool *overflow_p
)
1403 tree oldary
= TREE_OPERAND (t
, 0);
1404 tree ary
= cxx_eval_constant_expression (call
, oldary
,
1405 allow_non_constant
, addr
,
1406 non_constant_p
, overflow_p
);
1410 unsigned len
, elem_nchars
= 1;
1411 if (*non_constant_p
)
1413 oldidx
= TREE_OPERAND (t
, 1);
1414 index
= cxx_eval_constant_expression (call
, oldidx
,
1415 allow_non_constant
, false,
1416 non_constant_p
, overflow_p
);
1417 VERIFY_CONSTANT (index
);
1418 if (addr
&& ary
== oldary
&& index
== oldidx
)
1421 return build4 (ARRAY_REF
, TREE_TYPE (t
), ary
, index
, NULL
, NULL
);
1422 elem_type
= TREE_TYPE (TREE_TYPE (ary
));
1423 if (TREE_CODE (ary
) == CONSTRUCTOR
)
1424 len
= CONSTRUCTOR_NELTS (ary
);
1425 else if (TREE_CODE (ary
) == STRING_CST
)
1427 elem_nchars
= (TYPE_PRECISION (elem_type
)
1428 / TYPE_PRECISION (char_type_node
));
1429 len
= (unsigned) TREE_STRING_LENGTH (ary
) / elem_nchars
;
1433 /* We can't do anything with other tree codes, so use
1434 VERIFY_CONSTANT to complain and fail. */
1435 VERIFY_CONSTANT (ary
);
1438 if (compare_tree_int (index
, len
) >= 0)
1440 if (tree_int_cst_lt (index
, array_type_nelts_top (TREE_TYPE (ary
))))
1442 /* If it's within the array bounds but doesn't have an explicit
1443 initializer, it's value-initialized. */
1444 tree val
= build_value_init (elem_type
, tf_warning_or_error
);
1445 return cxx_eval_constant_expression (call
, val
,
1446 allow_non_constant
, addr
,
1447 non_constant_p
, overflow_p
);
1450 if (!allow_non_constant
)
1451 error ("array subscript out of bound");
1452 *non_constant_p
= true;
1455 else if (tree_int_cst_lt (index
, integer_zero_node
))
1457 if (!allow_non_constant
)
1458 error ("negative array subscript");
1459 *non_constant_p
= true;
1462 i
= tree_to_shwi (index
);
1463 if (TREE_CODE (ary
) == CONSTRUCTOR
)
1464 return (*CONSTRUCTOR_ELTS (ary
))[i
].value
;
1465 else if (elem_nchars
== 1)
1466 return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary
))),
1467 TREE_STRING_POINTER (ary
)[i
]);
1470 tree type
= cv_unqualified (TREE_TYPE (TREE_TYPE (ary
)));
1471 return native_interpret_expr (type
, (const unsigned char *)
1472 TREE_STRING_POINTER (ary
)
1473 + i
* elem_nchars
, elem_nchars
);
1475 /* Don't VERIFY_CONSTANT here. */
1478 /* Subroutine of cxx_eval_constant_expression.
1479 Attempt to reduce a field access of a value of class type. */
1482 cxx_eval_component_reference (const constexpr_call
*call
, tree t
,
1483 bool allow_non_constant
, bool addr
,
1484 bool *non_constant_p
, bool *overflow_p
)
1486 unsigned HOST_WIDE_INT i
;
1489 tree part
= TREE_OPERAND (t
, 1);
1490 tree orig_whole
= TREE_OPERAND (t
, 0);
1491 tree whole
= cxx_eval_constant_expression (call
, orig_whole
,
1492 allow_non_constant
, addr
,
1493 non_constant_p
, overflow_p
);
1494 if (whole
== orig_whole
)
1497 return fold_build3 (COMPONENT_REF
, TREE_TYPE (t
),
1498 whole
, part
, NULL_TREE
);
1499 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
1501 if (!*non_constant_p
&& TREE_CODE (whole
) != CONSTRUCTOR
)
1503 if (!allow_non_constant
)
1504 error ("%qE is not a constant expression", orig_whole
);
1505 *non_constant_p
= true;
1507 if (DECL_MUTABLE_P (part
))
1509 if (!allow_non_constant
)
1510 error ("mutable %qD is not usable in a constant expression", part
);
1511 *non_constant_p
= true;
1513 if (*non_constant_p
)
1515 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
1520 if (TREE_CODE (TREE_TYPE (whole
)) == UNION_TYPE
1521 && CONSTRUCTOR_NELTS (whole
) > 0)
1523 /* DR 1188 says we don't have to deal with this. */
1524 if (!allow_non_constant
)
1525 error ("accessing %qD member instead of initialized %qD member in "
1526 "constant expression", part
, CONSTRUCTOR_ELT (whole
, 0)->index
);
1527 *non_constant_p
= true;
1531 /* If there's no explicit init for this field, it's value-initialized. */
1532 value
= build_value_init (TREE_TYPE (t
), tf_warning_or_error
);
1533 return cxx_eval_constant_expression (call
, value
,
1534 allow_non_constant
, addr
,
1535 non_constant_p
, overflow_p
);
1538 /* Subroutine of cxx_eval_constant_expression.
1539 Attempt to reduce a field access of a value of class type that is
1540 expressed as a BIT_FIELD_REF. */
1543 cxx_eval_bit_field_ref (const constexpr_call
*call
, tree t
,
1544 bool allow_non_constant
, bool addr
,
1545 bool *non_constant_p
, bool *overflow_p
)
1547 tree orig_whole
= TREE_OPERAND (t
, 0);
1548 tree retval
, fldval
, utype
, mask
;
1549 bool fld_seen
= false;
1550 HOST_WIDE_INT istart
, isize
;
1551 tree whole
= cxx_eval_constant_expression (call
, orig_whole
,
1552 allow_non_constant
, addr
,
1553 non_constant_p
, overflow_p
);
1554 tree start
, field
, value
;
1555 unsigned HOST_WIDE_INT i
;
1557 if (whole
== orig_whole
)
1559 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
1561 if (!*non_constant_p
1562 && TREE_CODE (whole
) != VECTOR_CST
1563 && TREE_CODE (whole
) != CONSTRUCTOR
)
1565 if (!allow_non_constant
)
1566 error ("%qE is not a constant expression", orig_whole
);
1567 *non_constant_p
= true;
1569 if (*non_constant_p
)
1572 if (TREE_CODE (whole
) == VECTOR_CST
)
1573 return fold_ternary (BIT_FIELD_REF
, TREE_TYPE (t
), whole
,
1574 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 2));
1576 start
= TREE_OPERAND (t
, 2);
1577 istart
= tree_to_shwi (start
);
1578 isize
= tree_to_shwi (TREE_OPERAND (t
, 1));
1579 utype
= TREE_TYPE (t
);
1580 if (!TYPE_UNSIGNED (utype
))
1581 utype
= build_nonstandard_integer_type (TYPE_PRECISION (utype
), 1);
1582 retval
= build_int_cst (utype
, 0);
1583 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
1585 tree bitpos
= bit_position (field
);
1586 if (bitpos
== start
&& DECL_SIZE (field
) == TREE_OPERAND (t
, 1))
1588 if (TREE_CODE (TREE_TYPE (field
)) == INTEGER_TYPE
1589 && TREE_CODE (value
) == INTEGER_CST
1590 && tree_fits_shwi_p (bitpos
)
1591 && tree_fits_shwi_p (DECL_SIZE (field
)))
1593 HOST_WIDE_INT bit
= tree_to_shwi (bitpos
);
1594 HOST_WIDE_INT sz
= tree_to_shwi (DECL_SIZE (field
));
1595 HOST_WIDE_INT shift
;
1596 if (bit
>= istart
&& bit
+ sz
<= istart
+ isize
)
1598 fldval
= fold_convert (utype
, value
);
1599 mask
= build_int_cst_type (utype
, -1);
1600 mask
= fold_build2 (LSHIFT_EXPR
, utype
, mask
,
1601 size_int (TYPE_PRECISION (utype
) - sz
));
1602 mask
= fold_build2 (RSHIFT_EXPR
, utype
, mask
,
1603 size_int (TYPE_PRECISION (utype
) - sz
));
1604 fldval
= fold_build2 (BIT_AND_EXPR
, utype
, fldval
, mask
);
1605 shift
= bit
- istart
;
1606 if (BYTES_BIG_ENDIAN
)
1607 shift
= TYPE_PRECISION (utype
) - shift
- sz
;
1608 fldval
= fold_build2 (LSHIFT_EXPR
, utype
, fldval
,
1610 retval
= fold_build2 (BIT_IOR_EXPR
, utype
, retval
, fldval
);
1616 return fold_convert (TREE_TYPE (t
), retval
);
1618 return error_mark_node
;
1621 /* Subroutine of cxx_eval_constant_expression.
1622 Evaluate a short-circuited logical expression T in the context
1623 of a given constexpr CALL. BAILOUT_VALUE is the value for
1624 early return. CONTINUE_VALUE is used here purely for
1625 sanity check purposes. */
1628 cxx_eval_logical_expression (const constexpr_call
*call
, tree t
,
1629 tree bailout_value
, tree continue_value
,
1630 bool allow_non_constant
, bool addr
,
1631 bool *non_constant_p
, bool *overflow_p
)
1634 tree lhs
= cxx_eval_constant_expression (call
, TREE_OPERAND (t
, 0),
1635 allow_non_constant
, addr
,
1636 non_constant_p
, overflow_p
);
1637 VERIFY_CONSTANT (lhs
);
1638 if (tree_int_cst_equal (lhs
, bailout_value
))
1640 gcc_assert (tree_int_cst_equal (lhs
, continue_value
));
1641 r
= cxx_eval_constant_expression (call
, TREE_OPERAND (t
, 1),
1642 allow_non_constant
, addr
, non_constant_p
, overflow_p
);
1643 VERIFY_CONSTANT (r
);
1647 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
1648 CONSTRUCTOR elements to initialize (part of) an object containing that
1649 field. Return a pointer to the constructor_elt corresponding to the
1650 initialization of the field. */
1652 static constructor_elt
*
1653 base_field_constructor_elt (vec
<constructor_elt
, va_gc
> *v
, tree ref
)
1655 tree aggr
= TREE_OPERAND (ref
, 0);
1656 tree field
= TREE_OPERAND (ref
, 1);
1658 constructor_elt
*ce
;
1660 gcc_assert (TREE_CODE (ref
) == COMPONENT_REF
);
1662 if (TREE_CODE (aggr
) == COMPONENT_REF
)
1664 constructor_elt
*base_ce
1665 = base_field_constructor_elt (v
, aggr
);
1666 v
= CONSTRUCTOR_ELTS (base_ce
->value
);
1669 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
1670 if (ce
->index
== field
)
1677 /* Subroutine of cxx_eval_constant_expression.
1678 The expression tree T denotes a C-style array or a C-style
1679 aggregate. Reduce it to a constant expression. */
1682 cxx_eval_bare_aggregate (const constexpr_call
*call
, tree t
,
1683 bool allow_non_constant
, bool addr
,
1684 bool *non_constant_p
, bool *overflow_p
)
1686 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
1687 vec
<constructor_elt
, va_gc
> *n
;
1688 vec_alloc (n
, vec_safe_length (v
));
1689 constructor_elt
*ce
;
1691 bool changed
= false;
1692 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t
));
1693 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
1695 tree elt
= cxx_eval_constant_expression (call
, ce
->value
,
1696 allow_non_constant
, addr
,
1697 non_constant_p
, overflow_p
);
1698 /* Don't VERIFY_CONSTANT here. */
1699 if (allow_non_constant
&& *non_constant_p
)
1701 if (elt
!= ce
->value
)
1703 if (ce
->index
&& TREE_CODE (ce
->index
) == COMPONENT_REF
)
1705 /* This is an initialization of a vfield inside a base
1706 subaggregate that we already initialized; push this
1707 initialization into the previous initialization. */
1708 constructor_elt
*inner
= base_field_constructor_elt (n
, ce
->index
);
1712 && (TREE_CODE (ce
->index
) == NOP_EXPR
1713 || TREE_CODE (ce
->index
) == POINTER_PLUS_EXPR
))
1715 /* This is an initializer for an empty base; now that we've
1716 checked that it's constant, we can ignore it. */
1717 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (ce
->index
))));
1720 CONSTRUCTOR_APPEND_ELT (n
, ce
->index
, elt
);
1722 if (*non_constant_p
|| !changed
)
1728 t
= build_constructor (TREE_TYPE (t
), n
);
1729 TREE_CONSTANT (t
) = true;
1730 if (TREE_CODE (TREE_TYPE (t
)) == VECTOR_TYPE
)
1735 /* Subroutine of cxx_eval_constant_expression.
1736 The expression tree T is a VEC_INIT_EXPR which denotes the desired
1737 initialization of a non-static data member of array type. Reduce it to a
1740 Note that apart from value-initialization (when VALUE_INIT is true),
1741 this is only intended to support value-initialization and the
1742 initializations done by defaulted constructors for classes with
1743 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
1744 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
1745 for the copy/move constructor. */
1748 cxx_eval_vec_init_1 (const constexpr_call
*call
, tree atype
, tree init
,
1749 bool value_init
, bool allow_non_constant
, bool addr
,
1750 bool *non_constant_p
, bool *overflow_p
)
1752 tree elttype
= TREE_TYPE (atype
);
1753 int max
= tree_to_shwi (array_type_nelts (atype
));
1754 vec
<constructor_elt
, va_gc
> *n
;
1755 vec_alloc (n
, max
+ 1);
1756 bool pre_init
= false;
1759 /* For the default constructor, build up a call to the default
1760 constructor of the element type. We only need to handle class types
1761 here, as for a constructor to be constexpr, all members must be
1762 initialized, which for a defaulted default constructor means they must
1763 be of a class type with a constexpr default constructor. */
1764 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
1765 /* We only do this at the lowest level. */;
1766 else if (value_init
)
1768 init
= build_value_init (elttype
, tf_warning_or_error
);
1769 init
= cxx_eval_constant_expression
1770 (call
, init
, allow_non_constant
, addr
, non_constant_p
, overflow_p
);
1775 vec
<tree
, va_gc
> *argvec
= make_tree_vector ();
1776 init
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
1777 &argvec
, elttype
, LOOKUP_NORMAL
,
1778 tf_warning_or_error
);
1779 release_tree_vector (argvec
);
1780 init
= cxx_eval_constant_expression (call
, init
, allow_non_constant
,
1781 addr
, non_constant_p
, overflow_p
);
1785 if (*non_constant_p
&& !allow_non_constant
)
1788 for (i
= 0; i
<= max
; ++i
)
1790 tree idx
= build_int_cst (size_type_node
, i
);
1792 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
1794 /* A multidimensional array; recurse. */
1795 if (value_init
|| init
== NULL_TREE
)
1796 eltinit
= NULL_TREE
;
1798 eltinit
= cp_build_array_ref (input_location
, init
, idx
,
1799 tf_warning_or_error
);
1800 eltinit
= cxx_eval_vec_init_1 (call
, elttype
, eltinit
, value_init
,
1801 allow_non_constant
, addr
,
1802 non_constant_p
, overflow_p
);
1806 /* Initializing an element using value or default initialization
1807 we just pre-built above. */
1811 eltinit
= unshare_expr (init
);
1815 /* Copying an element. */
1816 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1817 (atype
, TREE_TYPE (init
)));
1818 eltinit
= cp_build_array_ref (input_location
, init
, idx
,
1819 tf_warning_or_error
);
1820 if (!real_lvalue_p (init
))
1821 eltinit
= move (eltinit
);
1822 eltinit
= force_rvalue (eltinit
, tf_warning_or_error
);
1823 eltinit
= cxx_eval_constant_expression
1824 (call
, eltinit
, allow_non_constant
, addr
, non_constant_p
, overflow_p
);
1826 if (*non_constant_p
&& !allow_non_constant
)
1828 CONSTRUCTOR_APPEND_ELT (n
, idx
, eltinit
);
1831 if (!*non_constant_p
)
1833 init
= build_constructor (atype
, n
);
1834 TREE_CONSTANT (init
) = true;
1844 cxx_eval_vec_init (const constexpr_call
*call
, tree t
,
1845 bool allow_non_constant
, bool addr
,
1846 bool *non_constant_p
, bool *overflow_p
)
1848 tree atype
= TREE_TYPE (t
);
1849 tree init
= VEC_INIT_EXPR_INIT (t
);
1850 tree r
= cxx_eval_vec_init_1 (call
, atype
, init
,
1851 VEC_INIT_EXPR_VALUE_INIT (t
),
1852 allow_non_constant
, addr
, non_constant_p
, overflow_p
);
1853 if (*non_constant_p
)
1859 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
1860 match. We want to be less strict for simple *& folding; if we have a
1861 non-const temporary that we access through a const pointer, that should
1862 work. We handle this here rather than change fold_indirect_ref_1
1863 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
1864 don't really make sense outside of constant expression evaluation. Also
1865 we want to allow folding to COMPONENT_REF, which could cause trouble
1866 with TBAA in fold_indirect_ref_1.
1868 Try to keep this function synced with fold_indirect_ref_1. */
1871 cxx_fold_indirect_ref (location_t loc
, tree type
, tree op0
, bool *empty_base
)
1877 subtype
= TREE_TYPE (sub
);
1878 if (!POINTER_TYPE_P (subtype
))
1881 if (TREE_CODE (sub
) == ADDR_EXPR
)
1883 tree op
= TREE_OPERAND (sub
, 0);
1884 tree optype
= TREE_TYPE (op
);
1886 /* *&CONST_DECL -> to the value of the const decl. */
1887 if (TREE_CODE (op
) == CONST_DECL
)
1888 return DECL_INITIAL (op
);
1889 /* *&p => p; make sure to handle *&"str"[cst] here. */
1890 if (same_type_ignoring_top_level_qualifiers_p (optype
, type
))
1892 tree fop
= fold_read_from_constant_string (op
);
1898 /* *(foo *)&fooarray => fooarray[0] */
1899 else if (TREE_CODE (optype
) == ARRAY_TYPE
1900 && (same_type_ignoring_top_level_qualifiers_p
1901 (type
, TREE_TYPE (optype
))))
1903 tree type_domain
= TYPE_DOMAIN (optype
);
1904 tree min_val
= size_zero_node
;
1905 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
1906 min_val
= TYPE_MIN_VALUE (type_domain
);
1907 return build4_loc (loc
, ARRAY_REF
, type
, op
, min_val
,
1908 NULL_TREE
, NULL_TREE
);
1910 /* *(foo *)&complexfoo => __real__ complexfoo */
1911 else if (TREE_CODE (optype
) == COMPLEX_TYPE
1912 && (same_type_ignoring_top_level_qualifiers_p
1913 (type
, TREE_TYPE (optype
))))
1914 return fold_build1_loc (loc
, REALPART_EXPR
, type
, op
);
1915 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
1916 else if (TREE_CODE (optype
) == VECTOR_TYPE
1917 && (same_type_ignoring_top_level_qualifiers_p
1918 (type
, TREE_TYPE (optype
))))
1920 tree part_width
= TYPE_SIZE (type
);
1921 tree index
= bitsize_int (0);
1922 return fold_build3_loc (loc
, BIT_FIELD_REF
, type
, op
, part_width
, index
);
1924 /* Also handle conversion to an empty base class, which
1925 is represented with a NOP_EXPR. */
1926 else if (is_empty_class (type
)
1927 && CLASS_TYPE_P (optype
)
1928 && DERIVED_FROM_P (type
, optype
))
1933 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
1934 else if (RECORD_OR_UNION_TYPE_P (optype
))
1936 tree field
= TYPE_FIELDS (optype
);
1937 for (; field
; field
= DECL_CHAIN (field
))
1938 if (TREE_CODE (field
) == FIELD_DECL
1939 && integer_zerop (byte_position (field
))
1940 && (same_type_ignoring_top_level_qualifiers_p
1941 (TREE_TYPE (field
), type
)))
1943 return fold_build3 (COMPONENT_REF
, type
, op
, field
, NULL_TREE
);
1948 else if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
1949 && TREE_CODE (TREE_OPERAND (sub
, 1)) == INTEGER_CST
)
1951 tree op00
= TREE_OPERAND (sub
, 0);
1952 tree op01
= TREE_OPERAND (sub
, 1);
1955 if (TREE_CODE (op00
) == ADDR_EXPR
)
1958 op00
= TREE_OPERAND (op00
, 0);
1959 op00type
= TREE_TYPE (op00
);
1961 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
1962 if (TREE_CODE (op00type
) == VECTOR_TYPE
1963 && (same_type_ignoring_top_level_qualifiers_p
1964 (type
, TREE_TYPE (op00type
))))
1966 HOST_WIDE_INT offset
= tree_to_shwi (op01
);
1967 tree part_width
= TYPE_SIZE (type
);
1968 unsigned HOST_WIDE_INT part_widthi
= tree_to_shwi (part_width
)/BITS_PER_UNIT
;
1969 unsigned HOST_WIDE_INT indexi
= offset
* BITS_PER_UNIT
;
1970 tree index
= bitsize_int (indexi
);
1972 if (offset
/ part_widthi
< TYPE_VECTOR_SUBPARTS (op00type
))
1973 return fold_build3_loc (loc
,
1974 BIT_FIELD_REF
, type
, op00
,
1978 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
1979 else if (TREE_CODE (op00type
) == COMPLEX_TYPE
1980 && (same_type_ignoring_top_level_qualifiers_p
1981 (type
, TREE_TYPE (op00type
))))
1983 tree size
= TYPE_SIZE_UNIT (type
);
1984 if (tree_int_cst_equal (size
, op01
))
1985 return fold_build1_loc (loc
, IMAGPART_EXPR
, type
, op00
);
1987 /* ((foo *)&fooarray)[1] => fooarray[1] */
1988 else if (TREE_CODE (op00type
) == ARRAY_TYPE
1989 && (same_type_ignoring_top_level_qualifiers_p
1990 (type
, TREE_TYPE (op00type
))))
1992 tree type_domain
= TYPE_DOMAIN (op00type
);
1993 tree min_val
= size_zero_node
;
1994 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
1995 min_val
= TYPE_MIN_VALUE (type_domain
);
1996 op01
= size_binop_loc (loc
, EXACT_DIV_EXPR
, op01
,
1997 TYPE_SIZE_UNIT (type
));
1998 op01
= size_binop_loc (loc
, PLUS_EXPR
, op01
, min_val
);
1999 return build4_loc (loc
, ARRAY_REF
, type
, op00
, op01
,
2000 NULL_TREE
, NULL_TREE
);
2002 /* Also handle conversion to an empty base class, which
2003 is represented with a NOP_EXPR. */
2004 else if (is_empty_class (type
)
2005 && CLASS_TYPE_P (op00type
)
2006 && DERIVED_FROM_P (type
, op00type
))
2011 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
2012 else if (RECORD_OR_UNION_TYPE_P (op00type
))
2014 tree field
= TYPE_FIELDS (op00type
);
2015 for (; field
; field
= DECL_CHAIN (field
))
2016 if (TREE_CODE (field
) == FIELD_DECL
2017 && tree_int_cst_equal (byte_position (field
), op01
)
2018 && (same_type_ignoring_top_level_qualifiers_p
2019 (TREE_TYPE (field
), type
)))
2021 return fold_build3 (COMPONENT_REF
, type
, op00
,
2028 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
2029 else if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
2030 && (same_type_ignoring_top_level_qualifiers_p
2031 (type
, TREE_TYPE (TREE_TYPE (subtype
)))))
2034 tree min_val
= size_zero_node
;
2035 tree newsub
= cxx_fold_indirect_ref (loc
, TREE_TYPE (subtype
), sub
, NULL
);
2039 sub
= build1_loc (loc
, INDIRECT_REF
, TREE_TYPE (subtype
), sub
);
2040 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
2041 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
2042 min_val
= TYPE_MIN_VALUE (type_domain
);
2043 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
2051 cxx_eval_indirect_ref (const constexpr_call
*call
, tree t
,
2052 bool allow_non_constant
, bool addr
,
2053 bool *non_constant_p
, bool *overflow_p
)
2055 tree orig_op0
= TREE_OPERAND (t
, 0);
2056 tree op0
= cxx_eval_constant_expression (call
, orig_op0
, allow_non_constant
,
2057 /*addr*/false, non_constant_p
, overflow_p
);
2058 bool empty_base
= false;
2061 /* Don't VERIFY_CONSTANT here. */
2062 if (*non_constant_p
)
2065 r
= cxx_fold_indirect_ref (EXPR_LOCATION (t
), TREE_TYPE (t
), op0
,
2069 r
= cxx_eval_constant_expression (call
, r
, allow_non_constant
,
2070 addr
, non_constant_p
, overflow_p
);
2075 if (TREE_CODE (sub
) == ADDR_EXPR
)
2077 /* We couldn't fold to a constant value. Make sure it's not
2078 something we should have been able to fold. */
2079 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
2080 (TREE_TYPE (TREE_TYPE (sub
)), TREE_TYPE (t
)));
2081 /* DR 1188 says we don't have to deal with this. */
2082 if (!allow_non_constant
)
2083 error ("accessing value of %qE through a %qT glvalue in a "
2084 "constant expression", build_fold_indirect_ref (sub
),
2086 *non_constant_p
= true;
2091 /* If we're pulling out the value of an empty base, make sure
2092 that the whole object is constant and then return an empty
2096 VERIFY_CONSTANT (r
);
2097 r
= build_constructor (TREE_TYPE (t
), NULL
);
2098 TREE_CONSTANT (r
) = true;
2103 if (addr
&& op0
!= orig_op0
)
2104 return build1 (INDIRECT_REF
, TREE_TYPE (t
), op0
);
2106 VERIFY_CONSTANT (t
);
2112 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
2113 Shared between potential_constant_expression and
2114 cxx_eval_constant_expression. */
2117 non_const_var_error (tree r
)
2119 tree type
= TREE_TYPE (r
);
2120 error ("the value of %qD is not usable in a constant "
2122 /* Avoid error cascade. */
2123 if (DECL_INITIAL (r
) == error_mark_node
)
2125 if (DECL_DECLARED_CONSTEXPR_P (r
))
2126 inform (DECL_SOURCE_LOCATION (r
),
2127 "%qD used in its own initializer", r
);
2128 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
2130 if (!CP_TYPE_CONST_P (type
))
2131 inform (DECL_SOURCE_LOCATION (r
),
2132 "%q#D is not const", r
);
2133 else if (CP_TYPE_VOLATILE_P (type
))
2134 inform (DECL_SOURCE_LOCATION (r
),
2135 "%q#D is volatile", r
);
2136 else if (!DECL_INITIAL (r
)
2137 || !TREE_CONSTANT (DECL_INITIAL (r
)))
2138 inform (DECL_SOURCE_LOCATION (r
),
2139 "%qD was not initialized with a constant "
2146 if (cxx_dialect
>= cxx11
&& !DECL_DECLARED_CONSTEXPR_P (r
))
2147 inform (DECL_SOURCE_LOCATION (r
),
2148 "%qD was not declared %<constexpr%>", r
);
2150 inform (DECL_SOURCE_LOCATION (r
),
2151 "%qD does not have integral or enumeration type",
2156 /* Subroutine of cxx_eval_constant_expression.
2157 Like cxx_eval_unary_expression, except for trinary expressions. */
2160 cxx_eval_trinary_expression (const constexpr_call
*call
, tree t
,
2161 bool allow_non_constant
, bool addr
,
2162 bool *non_constant_p
, bool *overflow_p
)
2168 for (i
= 0; i
< 3; i
++)
2170 args
[i
] = cxx_eval_constant_expression (call
, TREE_OPERAND (t
, i
),
2171 allow_non_constant
, addr
,
2172 non_constant_p
, overflow_p
);
2173 VERIFY_CONSTANT (args
[i
]);
2176 val
= fold_ternary_loc (EXPR_LOCATION (t
), TREE_CODE (t
), TREE_TYPE (t
),
2177 args
[0], args
[1], args
[2]);
2178 if (val
== NULL_TREE
)
2180 VERIFY_CONSTANT (val
);
2185 var_in_constexpr_fn (tree t
)
2187 tree ctx
= DECL_CONTEXT (t
);
2188 return (cxx_dialect
>= cxx14
&& ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
2189 && DECL_DECLARED_CONSTEXPR_P (ctx
));
2192 /* Attempt to reduce the expression T to a constant value.
2193 On failure, issue diagnostic and return error_mark_node. */
2194 /* FIXME unify with c_fully_fold */
2197 cxx_eval_constant_expression (const constexpr_call
*call
, tree t
,
2198 bool allow_non_constant
, bool addr
,
2199 bool *non_constant_p
, bool *overflow_p
)
2203 if (t
== error_mark_node
)
2205 *non_constant_p
= true;
2208 if (CONSTANT_CLASS_P (t
))
2210 if (TREE_CODE (t
) == PTRMEM_CST
)
2211 t
= cplus_expand_constant (t
);
2212 else if (TREE_OVERFLOW (t
) && (!flag_permissive
|| allow_non_constant
))
2216 if (TREE_CODE (t
) != NOP_EXPR
2217 && reduced_constant_expression_p (t
))
2220 switch (TREE_CODE (t
))
2225 /* else fall through. */
2227 r
= integral_constant_value (t
);
2228 if (TREE_CODE (r
) == TARGET_EXPR
2229 && TREE_CODE (TARGET_EXPR_INITIAL (r
)) == CONSTRUCTOR
)
2230 r
= TARGET_EXPR_INITIAL (r
);
2231 if (DECL_P (r
) && var_in_constexpr_fn (r
)
2232 && DECL_INITIAL (r
))
2233 r
= cxx_eval_constant_expression (call
, DECL_INITIAL (r
),
2234 allow_non_constant
, false,
2235 non_constant_p
, overflow_p
);
2238 if (!allow_non_constant
)
2239 non_const_var_error (r
);
2240 *non_constant_p
= true;
2250 if (call
&& DECL_CONTEXT (t
) == call
->fundef
->decl
)
2252 if (DECL_ARTIFICIAL (t
) && DECL_CONSTRUCTOR_P (DECL_CONTEXT (t
)))
2254 if (!allow_non_constant
)
2255 sorry ("use of the value of the object being constructed "
2256 "in a constant expression");
2257 *non_constant_p
= true;
2260 r
= lookup_parameter_binding (call
, t
);
2263 /* Defer in case this is only used for its type. */;
2266 if (!allow_non_constant
)
2267 error ("%qE is not a constant expression", t
);
2268 *non_constant_p
= true;
2273 case AGGR_INIT_EXPR
:
2274 r
= cxx_eval_call_expression (call
, t
, allow_non_constant
, addr
,
2275 non_constant_p
, overflow_p
);
2279 if (!literal_type_p (TREE_TYPE (t
)))
2281 if (!allow_non_constant
)
2283 error ("temporary of non-literal type %qT in a "
2284 "constant expression", TREE_TYPE (t
));
2285 explain_non_literal_class (TREE_TYPE (t
));
2287 *non_constant_p
= true;
2290 /* else fall through. */
2292 /* Pass false for 'addr' because these codes indicate
2293 initialization of a temporary. */
2294 r
= cxx_eval_constant_expression (call
, TREE_OPERAND (t
, 1),
2295 allow_non_constant
, false,
2296 non_constant_p
, overflow_p
);
2297 if (!*non_constant_p
)
2298 /* Adjust the type of the result to the type of the temporary. */
2299 r
= adjust_temp_type (TREE_TYPE (t
), r
);
2303 r
= cxx_eval_constant_expression (call
, TREE_OPERAND (t
, 1),
2304 allow_non_constant
, addr
,
2305 non_constant_p
, overflow_p
);
2309 case NON_LVALUE_EXPR
:
2310 case TRY_CATCH_EXPR
:
2311 case CLEANUP_POINT_EXPR
:
2312 case MUST_NOT_THROW_EXPR
:
2314 r
= cxx_eval_constant_expression (call
, TREE_OPERAND (t
, 0),
2315 allow_non_constant
, addr
,
2316 non_constant_p
, overflow_p
);
2319 /* These differ from cxx_eval_unary_expression in that this doesn't
2320 check for a constant operand or result; an address can be
2321 constant without its operand being, and vice versa. */
2323 r
= cxx_eval_indirect_ref (call
, t
, allow_non_constant
, addr
,
2324 non_constant_p
, overflow_p
);
2329 tree oldop
= TREE_OPERAND (t
, 0);
2330 tree op
= cxx_eval_constant_expression (call
, oldop
,
2333 non_constant_p
, overflow_p
);
2334 /* Don't VERIFY_CONSTANT here. */
2335 if (*non_constant_p
)
2337 /* This function does more aggressive folding than fold itself. */
2338 r
= build_fold_addr_expr_with_type (op
, TREE_TYPE (t
));
2339 if (TREE_CODE (r
) == ADDR_EXPR
&& TREE_OPERAND (r
, 0) == oldop
)
2347 case FIX_TRUNC_EXPR
:
2352 case TRUTH_NOT_EXPR
:
2353 case FIXED_CONVERT_EXPR
:
2354 r
= cxx_eval_unary_expression (call
, t
, allow_non_constant
, addr
,
2355 non_constant_p
, overflow_p
);
2359 if (SIZEOF_EXPR_TYPE_P (t
))
2360 r
= cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t
, 0)),
2361 SIZEOF_EXPR
, false);
2362 else if (TYPE_P (TREE_OPERAND (t
, 0)))
2363 r
= cxx_sizeof_or_alignof_type (TREE_OPERAND (t
, 0), SIZEOF_EXPR
,
2366 r
= cxx_sizeof_or_alignof_expr (TREE_OPERAND (t
, 0), SIZEOF_EXPR
,
2368 if (r
== error_mark_node
)
2370 VERIFY_CONSTANT (r
);
2375 /* check_return_expr sometimes wraps a TARGET_EXPR in a
2376 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
2377 introduced by build_call_a. */
2378 tree op0
= TREE_OPERAND (t
, 0);
2379 tree op1
= TREE_OPERAND (t
, 1);
2381 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
2382 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
2383 r
= cxx_eval_constant_expression (call
, op0
, allow_non_constant
,
2384 addr
, non_constant_p
, overflow_p
);
2387 /* Check that the LHS is constant and then discard it. */
2388 cxx_eval_constant_expression (call
, op0
, allow_non_constant
,
2389 false, non_constant_p
, overflow_p
);
2390 op1
= TREE_OPERAND (t
, 1);
2391 r
= cxx_eval_constant_expression (call
, op1
, allow_non_constant
,
2392 addr
, non_constant_p
, overflow_p
);
2397 case POINTER_PLUS_EXPR
:
2401 case TRUNC_DIV_EXPR
:
2403 case FLOOR_DIV_EXPR
:
2404 case ROUND_DIV_EXPR
:
2405 case TRUNC_MOD_EXPR
:
2407 case ROUND_MOD_EXPR
:
2409 case EXACT_DIV_EXPR
:
2419 case TRUTH_XOR_EXPR
:
2426 case UNORDERED_EXPR
:
2436 r
= cxx_eval_binary_expression (call
, t
, allow_non_constant
, addr
,
2437 non_constant_p
, overflow_p
);
2440 /* fold can introduce non-IF versions of these; still treat them as
2441 short-circuiting. */
2442 case TRUTH_AND_EXPR
:
2443 case TRUTH_ANDIF_EXPR
:
2444 r
= cxx_eval_logical_expression (call
, t
, boolean_false_node
,
2446 allow_non_constant
, addr
,
2447 non_constant_p
, overflow_p
);
2451 case TRUTH_ORIF_EXPR
:
2452 r
= cxx_eval_logical_expression (call
, t
, boolean_true_node
,
2454 allow_non_constant
, addr
,
2455 non_constant_p
, overflow_p
);
2459 r
= cxx_eval_array_reference (call
, t
, allow_non_constant
, addr
,
2460 non_constant_p
, overflow_p
);
2464 if (is_overloaded_fn (t
))
2466 /* We can only get here in checking mode via
2467 build_non_dependent_expr, because any expression that
2468 calls or takes the address of the function will have
2469 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
2470 gcc_checking_assert (allow_non_constant
|| errorcount
);
2471 *non_constant_p
= true;
2474 r
= cxx_eval_component_reference (call
, t
, allow_non_constant
, addr
,
2475 non_constant_p
, overflow_p
);
2479 r
= cxx_eval_bit_field_ref (call
, t
, allow_non_constant
, addr
,
2480 non_constant_p
, overflow_p
);
2485 r
= cxx_eval_conditional_expression (call
, t
, allow_non_constant
, addr
,
2486 non_constant_p
, overflow_p
);
2490 r
= cxx_eval_bare_aggregate (call
, t
, allow_non_constant
, addr
,
2491 non_constant_p
, overflow_p
);
2495 /* We can get this in a defaulted constructor for a class with a
2496 non-static data member of array type. Either the initializer will
2497 be NULL, meaning default-initialization, or it will be an lvalue
2498 or xvalue of the same type, meaning direct-initialization from the
2499 corresponding member. */
2500 r
= cxx_eval_vec_init (call
, t
, allow_non_constant
, addr
,
2501 non_constant_p
, overflow_p
);
2506 r
= cxx_eval_trinary_expression (call
, t
, allow_non_constant
, addr
,
2507 non_constant_p
, overflow_p
);
2511 case VIEW_CONVERT_EXPR
:
2514 tree oldop
= TREE_OPERAND (t
, 0);
2515 tree op
= cxx_eval_constant_expression (call
, oldop
,
2516 allow_non_constant
, addr
,
2517 non_constant_p
, overflow_p
);
2518 if (*non_constant_p
)
2520 if (POINTER_TYPE_P (TREE_TYPE (t
))
2521 && TREE_CODE (op
) == INTEGER_CST
2522 && !integer_zerop (op
))
2524 if (!allow_non_constant
)
2525 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
2526 "reinterpret_cast from integer to pointer");
2527 *non_constant_p
= true;
2531 /* We didn't fold at the top so we could check for ptr-int
2534 r
= fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), op
);
2535 /* Conversion of an out-of-range value has implementation-defined
2536 behavior; the language considers it different from arithmetic
2537 overflow, which is undefined. */
2538 if (TREE_OVERFLOW_P (r
) && !TREE_OVERFLOW_P (op
))
2539 TREE_OVERFLOW (r
) = false;
2543 case EMPTY_CLASS_EXPR
:
2544 /* This is good enough for a function argument that might not get
2545 used, and they can't do anything with it, so just return it. */
2549 case PREINCREMENT_EXPR
:
2550 case POSTINCREMENT_EXPR
:
2551 case PREDECREMENT_EXPR
:
2552 case POSTDECREMENT_EXPR
:
2556 case VEC_DELETE_EXPR
:
2560 /* GCC internal stuff. */
2563 case WITH_CLEANUP_EXPR
:
2564 case STATEMENT_LIST
:
2566 case NON_DEPENDENT_EXPR
:
2570 if (!allow_non_constant
)
2571 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
2572 "expression %qE is not a constant-expression", t
);
2573 *non_constant_p
= true;
2577 internal_error ("unexpected expression %qE of kind %s", t
,
2578 get_tree_code_name (TREE_CODE (t
)));
2579 *non_constant_p
= true;
2583 if (r
== error_mark_node
)
2584 *non_constant_p
= true;
2586 if (*non_constant_p
)
2593 cxx_eval_outermost_constant_expr (tree t
, bool allow_non_constant
)
2595 bool non_constant_p
= false;
2596 bool overflow_p
= false;
2597 tree r
= cxx_eval_constant_expression (NULL
, t
, allow_non_constant
,
2598 false, &non_constant_p
, &overflow_p
);
2600 verify_constant (r
, allow_non_constant
, &non_constant_p
, &overflow_p
);
2602 if (TREE_CODE (t
) != CONSTRUCTOR
2603 && cp_has_mutable_p (TREE_TYPE (t
)))
2605 /* We allow a mutable type if the original expression was a
2606 CONSTRUCTOR so that we can do aggregate initialization of
2607 constexpr variables. */
2608 if (!allow_non_constant
)
2609 error ("%qT cannot be the type of a complete constant expression "
2610 "because it has mutable sub-objects", TREE_TYPE (t
));
2611 non_constant_p
= true;
2614 /* Technically we should check this for all subexpressions, but that
2615 runs into problems with our internal representation of pointer
2616 subtraction and the 5.19 rules are still in flux. */
2617 if (CONVERT_EXPR_CODE_P (TREE_CODE (r
))
2618 && ARITHMETIC_TYPE_P (TREE_TYPE (r
))
2619 && TREE_CODE (TREE_OPERAND (r
, 0)) == ADDR_EXPR
)
2621 if (!allow_non_constant
)
2622 error ("conversion from pointer type %qT "
2623 "to arithmetic type %qT in a constant-expression",
2624 TREE_TYPE (TREE_OPERAND (r
, 0)), TREE_TYPE (r
));
2625 non_constant_p
= true;
2628 if (!non_constant_p
&& overflow_p
)
2629 non_constant_p
= true;
2631 if (non_constant_p
&& !allow_non_constant
)
2632 return error_mark_node
;
2633 else if (non_constant_p
&& TREE_CONSTANT (r
))
2635 /* This isn't actually constant, so unset TREE_CONSTANT. */
2638 else if (TREE_CODE (r
) == CONSTRUCTOR
)
2639 r
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (r
), r
);
2641 r
= build_nop (TREE_TYPE (r
), r
);
2642 TREE_CONSTANT (r
) = false;
2644 else if (non_constant_p
|| r
== t
)
2647 if (TREE_CODE (r
) == CONSTRUCTOR
&& CLASS_TYPE_P (TREE_TYPE (r
)))
2649 if (TREE_CODE (t
) == TARGET_EXPR
2650 && TARGET_EXPR_INITIAL (t
) == r
)
2654 r
= get_target_expr (r
);
2655 TREE_CONSTANT (r
) = true;
2663 /* Returns true if T is a valid subexpression of a constant expression,
2664 even if it isn't itself a constant expression. */
2667 is_sub_constant_expr (tree t
)
2669 bool non_constant_p
= false;
2670 bool overflow_p
= false;
2671 cxx_eval_constant_expression (NULL
, t
, true, false, &non_constant_p
,
2673 return !non_constant_p
&& !overflow_p
;
2676 /* If T represents a constant expression returns its reduced value.
2677 Otherwise return error_mark_node. If T is dependent, then
2681 cxx_constant_value (tree t
)
2683 return cxx_eval_outermost_constant_expr (t
, false);
2686 /* If T is a constant expression, returns its reduced value.
2687 Otherwise, if T does not have TREE_CONSTANT set, returns T.
2688 Otherwise, returns a version of T without TREE_CONSTANT. */
2691 maybe_constant_value (tree t
)
2695 if (instantiation_dependent_expression_p (t
)
2696 || type_unknown_p (t
)
2697 || BRACE_ENCLOSED_INITIALIZER_P (t
)
2698 || !potential_constant_expression (t
))
2700 if (TREE_OVERFLOW_P (t
))
2702 t
= build_nop (TREE_TYPE (t
), t
);
2703 TREE_CONSTANT (t
) = false;
2708 r
= cxx_eval_outermost_constant_expr (t
, true);
2709 #ifdef ENABLE_CHECKING
2710 /* cp_tree_equal looks through NOPs, so allow them. */
2712 || CONVERT_EXPR_P (t
)
2713 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
2714 || !cp_tree_equal (r
, t
));
2719 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
2720 than wrapped in a TARGET_EXPR. */
2723 maybe_constant_init (tree t
)
2725 if (TREE_CODE (t
) == EXPR_STMT
)
2726 t
= TREE_OPERAND (t
, 0);
2727 if (TREE_CODE (t
) == CONVERT_EXPR
2728 && VOID_TYPE_P (TREE_TYPE (t
)))
2729 t
= TREE_OPERAND (t
, 0);
2730 t
= maybe_constant_value (t
);
2731 if (TREE_CODE (t
) == TARGET_EXPR
)
2733 tree init
= TARGET_EXPR_INITIAL (t
);
2734 if (TREE_CODE (init
) == CONSTRUCTOR
)
2741 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
2742 /* Return true if the object referred to by REF has automatic or thread
2745 enum { ck_ok
, ck_bad
, ck_unknown
};
2747 check_automatic_or_tls (tree ref
)
2749 enum machine_mode mode
;
2750 HOST_WIDE_INT bitsize
, bitpos
;
2752 int volatilep
= 0, unsignedp
= 0;
2753 tree decl
= get_inner_reference (ref
, &bitsize
, &bitpos
, &offset
,
2754 &mode
, &unsignedp
, &volatilep
, false);
2757 /* If there isn't a decl in the middle, we don't know the linkage here,
2758 and this isn't a constant expression anyway. */
2761 dk
= decl_storage_duration (decl
);
2762 return (dk
== dk_auto
|| dk
== dk_thread
) ? ck_bad
: ck_ok
;
2766 /* Return true if T denotes a potentially constant expression. Issue
2767 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
2768 an lvalue-rvalue conversion is implied.
2770 C++0x [expr.const] used to say
2772 6 An expression is a potential constant expression if it is
2773 a constant expression where all occurrences of function
2774 parameters are replaced by arbitrary constant expressions
2775 of the appropriate type.
2777 2 A conditional expression is a constant expression unless it
2778 involves one of the following as a potentially evaluated
2779 subexpression (3.2), but subexpressions of logical AND (5.14),
2780 logical OR (5.15), and conditional (5.16) operations that are
2781 not evaluated are not considered. */
2784 potential_constant_expression_1 (tree t
, bool want_rval
, tsubst_flags_t flags
)
2786 enum { any
= false, rval
= true };
2790 if (t
== error_mark_node
)
2794 if (TREE_THIS_VOLATILE (t
))
2796 if (flags
& tf_error
)
2797 error ("expression %qE has side-effects", t
);
2800 if (CONSTANT_CLASS_P (t
))
2803 switch (TREE_CODE (t
))
2809 case TEMPLATE_ID_EXPR
:
2817 case TEMPLATE_PARM_INDEX
:
2819 case IDENTIFIER_NODE
:
2820 case USERDEF_LITERAL
:
2821 /* We can see a FIELD_DECL in a pointer-to-member expression. */
2827 case AGGR_INIT_EXPR
:
2829 /* -- an invocation of a function other than a constexpr function
2830 or a constexpr constructor. */
2832 tree fun
= get_function_named_in_call (t
);
2833 const int nargs
= call_expr_nargs (t
);
2836 if (is_overloaded_fn (fun
))
2838 if (TREE_CODE (fun
) == FUNCTION_DECL
)
2840 if (builtin_valid_in_constant_expr_p (fun
))
2842 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
2843 /* Allow any built-in function; if the expansion
2844 isn't constant, we'll deal with that then. */
2845 && !is_builtin_fn (fun
))
2847 if (flags
& tf_error
)
2849 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
2850 "call to non-constexpr function %qD", fun
);
2851 explain_invalid_constexpr_fn (fun
);
2855 /* A call to a non-static member function takes the address
2856 of the object as the first argument. But in a constant
2857 expression the address will be folded away, so look
2859 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
2860 && !DECL_CONSTRUCTOR_P (fun
))
2862 tree x
= get_nth_callarg (t
, 0);
2863 if (is_this_parameter (x
))
2865 if (DECL_CONTEXT (x
) == NULL_TREE
2866 || DECL_CONSTRUCTOR_P (DECL_CONTEXT (x
)))
2868 if (flags
& tf_error
)
2869 sorry ("calling a member function of the "
2870 "object being constructed in a constant "
2874 /* Otherwise OK. */;
2876 else if (!potential_constant_expression_1 (x
, rval
, flags
))
2883 if (!potential_constant_expression_1 (fun
, true, flags
))
2885 fun
= get_first_fn (fun
);
2887 /* Skip initial arguments to base constructors. */
2888 if (DECL_BASE_CONSTRUCTOR_P (fun
))
2889 i
= num_artificial_parms_for (fun
);
2890 fun
= DECL_ORIGIN (fun
);
2894 if (potential_constant_expression_1 (fun
, rval
, flags
))
2895 /* Might end up being a constant function pointer. */;
2899 for (; i
< nargs
; ++i
)
2901 tree x
= get_nth_callarg (t
, i
);
2902 if (!potential_constant_expression_1 (x
, rval
, flags
))
2908 case NON_LVALUE_EXPR
:
2909 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
2910 -- an lvalue of integral type that refers to a non-volatile
2911 const variable or static data member initialized with
2912 constant expressions, or
2914 -- an lvalue of literal type that refers to non-volatile
2915 object defined with constexpr, or that refers to a
2916 sub-object of such an object; */
2917 return potential_constant_expression_1 (TREE_OPERAND (t
, 0), rval
, flags
);
2920 if (want_rval
&& !decl_constant_var_p (t
)
2921 && !var_in_constexpr_fn (t
)
2922 && !dependent_type_p (TREE_TYPE (t
)))
2924 if (flags
& tf_error
)
2925 non_const_var_error (t
);
2932 case VIEW_CONVERT_EXPR
:
2933 /* -- a reinterpret_cast. FIXME not implemented, and this rule
2934 may change to something more specific to type-punning (DR 1312). */
2936 tree from
= TREE_OPERAND (t
, 0);
2937 if (POINTER_TYPE_P (TREE_TYPE (t
))
2938 && TREE_CODE (from
) == INTEGER_CST
2939 && !integer_zerop (from
))
2941 if (flags
& tf_error
)
2942 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
2943 "reinterpret_cast from integer to pointer");
2946 return (potential_constant_expression_1
2947 (from
, TREE_CODE (t
) != VIEW_CONVERT_EXPR
, flags
));
2951 /* -- a unary operator & that is applied to an lvalue that
2952 designates an object with thread or automatic storage
2954 t
= TREE_OPERAND (t
, 0);
2956 if (TREE_CODE (t
) == OFFSET_REF
&& PTRMEM_OK_P (t
))
2957 /* A pointer-to-member constant. */
2961 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
2962 any checking here, as we might dereference the pointer later. If
2963 we remove this code, also remove check_automatic_or_tls. */
2964 i
= check_automatic_or_tls (t
);
2969 if (flags
& tf_error
)
2970 error ("address-of an object %qE with thread local or "
2971 "automatic storage is not a constant expression", t
);
2975 return potential_constant_expression_1 (t
, any
, flags
);
2981 /* -- a class member access unless its postfix-expression is
2982 of literal type or of pointer to literal type. */
2983 /* This test would be redundant, as it follows from the
2984 postfix-expression being a potential constant expression. */
2985 return potential_constant_expression_1 (TREE_OPERAND (t
, 0),
2988 case EXPR_PACK_EXPANSION
:
2989 return potential_constant_expression_1 (PACK_EXPANSION_PATTERN (t
),
2994 tree x
= TREE_OPERAND (t
, 0);
2996 if (is_this_parameter (x
))
2998 if (DECL_CONTEXT (x
)
2999 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x
)))
3001 if (flags
& tf_error
)
3002 error ("use of %<this%> in a constant expression");
3005 if (want_rval
&& DECL_CONTEXT (x
)
3006 && DECL_CONSTRUCTOR_P (DECL_CONTEXT (x
)))
3008 if (flags
& tf_error
)
3009 sorry ("use of the value of the object being constructed "
3010 "in a constant expression");
3015 return potential_constant_expression_1 (x
, rval
, flags
);
3019 case DYNAMIC_CAST_EXPR
:
3020 case PSEUDO_DTOR_EXPR
:
3021 case PREINCREMENT_EXPR
:
3022 case POSTINCREMENT_EXPR
:
3023 case PREDECREMENT_EXPR
:
3024 case POSTDECREMENT_EXPR
:
3028 case VEC_DELETE_EXPR
:
3033 case OMP_ATOMIC_READ
:
3034 case OMP_ATOMIC_CAPTURE_OLD
:
3035 case OMP_ATOMIC_CAPTURE_NEW
:
3036 /* GCC internal stuff. */
3039 case WITH_CLEANUP_EXPR
:
3040 case CLEANUP_POINT_EXPR
:
3041 case MUST_NOT_THROW_EXPR
:
3042 case TRY_CATCH_EXPR
:
3043 case STATEMENT_LIST
:
3044 /* Don't bother trying to define a subset of statement-expressions to
3045 be constant-expressions, at least for now. */
3049 case TRANSACTION_EXPR
:
3055 if (flags
& tf_error
)
3056 error ("expression %qE is not a constant-expression", t
);
3060 /* -- a typeid expression whose operand is of polymorphic
3063 tree e
= TREE_OPERAND (t
, 0);
3064 if (!TYPE_P (e
) && !type_dependent_expression_p (e
)
3065 && TYPE_POLYMORPHIC_P (TREE_TYPE (e
)))
3067 if (flags
& tf_error
)
3068 error ("typeid-expression is not a constant expression "
3069 "because %qE is of polymorphic type", e
);
3076 /* -- a subtraction where both operands are pointers. */
3077 if (TYPE_PTR_P (TREE_OPERAND (t
, 0))
3078 && TYPE_PTR_P (TREE_OPERAND (t
, 1)))
3080 if (flags
& tf_error
)
3081 error ("difference of two pointer expressions is not "
3082 "a constant expression");
3094 /* -- a relational or equality operator where at least
3095 one of the operands is a pointer. */
3096 if (TYPE_PTR_P (TREE_OPERAND (t
, 0))
3097 || TYPE_PTR_P (TREE_OPERAND (t
, 1)))
3099 if (flags
& tf_error
)
3100 error ("pointer comparison expression is not a "
3101 "constant expression");
3109 if (TYPE_P (TREE_OPERAND (t
, 0)))
3111 /* else fall through. */
3117 case FIX_TRUNC_EXPR
:
3121 case TRUTH_NOT_EXPR
:
3122 case FIXED_CONVERT_EXPR
:
3123 case UNARY_PLUS_EXPR
:
3124 return potential_constant_expression_1 (TREE_OPERAND (t
, 0), rval
,
3128 case CONST_CAST_EXPR
:
3129 case STATIC_CAST_EXPR
:
3130 case REINTERPRET_CAST_EXPR
:
3131 case IMPLICIT_CONV_EXPR
:
3132 if (cxx_dialect
< cxx11
3133 && !dependent_type_p (TREE_TYPE (t
))
3134 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
)))
3135 /* In C++98, a conversion to non-integral type can't be part of a
3136 constant expression. */
3138 if (flags
& tf_error
)
3139 error ("cast to non-integral type %qT in a constant expression",
3144 return (potential_constant_expression_1
3145 (TREE_OPERAND (t
, 0),
3146 TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
, flags
));
3149 case NON_DEPENDENT_EXPR
:
3150 /* For convenience. */
3152 return potential_constant_expression_1 (TREE_OPERAND (t
, 0),
3156 return potential_constant_expression_1 (TREE_OPERAND (t
, 1),
3160 if (!literal_type_p (TREE_TYPE (t
)))
3162 if (flags
& tf_error
)
3164 error ("temporary of non-literal type %qT in a "
3165 "constant expression", TREE_TYPE (t
));
3166 explain_non_literal_class (TREE_TYPE (t
));
3171 return potential_constant_expression_1 (TREE_OPERAND (t
, 1),
3176 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
3177 constructor_elt
*ce
;
3178 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
3179 if (!potential_constant_expression_1 (ce
->value
, want_rval
, flags
))
3186 gcc_assert (TREE_PURPOSE (t
) == NULL_TREE
3187 || DECL_P (TREE_PURPOSE (t
)));
3188 if (!potential_constant_expression_1 (TREE_VALUE (t
), want_rval
,
3191 if (TREE_CHAIN (t
) == NULL_TREE
)
3193 return potential_constant_expression_1 (TREE_CHAIN (t
), want_rval
,
3197 case TRUNC_DIV_EXPR
:
3199 case FLOOR_DIV_EXPR
:
3200 case ROUND_DIV_EXPR
:
3201 case TRUNC_MOD_EXPR
:
3203 case ROUND_MOD_EXPR
:
3205 tree denom
= TREE_OPERAND (t
, 1);
3206 if (!potential_constant_expression_1 (denom
, rval
, flags
))
3208 /* We can't call cxx_eval_outermost_constant_expr on an expression
3209 that hasn't been through fold_non_dependent_expr yet. */
3210 if (!processing_template_decl
)
3211 denom
= cxx_eval_outermost_constant_expr (denom
, true);
3212 if (integer_zerop (denom
))
3214 if (flags
& tf_error
)
3215 error ("division by zero is not a constant-expression");
3221 return potential_constant_expression_1 (TREE_OPERAND (t
, 0),
3228 /* check_return_expr sometimes wraps a TARGET_EXPR in a
3229 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
3230 introduced by build_call_a. */
3231 tree op0
= TREE_OPERAND (t
, 0);
3232 tree op1
= TREE_OPERAND (t
, 1);
3234 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
3235 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
3236 return potential_constant_expression_1 (op0
, want_rval
, flags
);
3241 /* If the first operand is the non-short-circuit constant, look at
3242 the second operand; otherwise we only care about the first one for
3244 case TRUTH_AND_EXPR
:
3245 case TRUTH_ANDIF_EXPR
:
3246 tmp
= boolean_true_node
;
3249 case TRUTH_ORIF_EXPR
:
3250 tmp
= boolean_false_node
;
3253 tree op
= TREE_OPERAND (t
, 0);
3254 if (!potential_constant_expression_1 (op
, rval
, flags
))
3256 if (!processing_template_decl
)
3257 op
= cxx_eval_outermost_constant_expr (op
, true);
3258 if (tree_int_cst_equal (op
, tmp
))
3259 return potential_constant_expression_1 (TREE_OPERAND (t
, 1), rval
, flags
);
3266 case POINTER_PLUS_EXPR
:
3268 case EXACT_DIV_EXPR
:
3278 case TRUTH_XOR_EXPR
:
3279 case UNORDERED_EXPR
:
3292 case ARRAY_RANGE_REF
:
3296 for (i
= 0; i
< 2; ++i
)
3297 if (!potential_constant_expression_1 (TREE_OPERAND (t
, i
),
3302 case CILK_SYNC_STMT
:
3303 case CILK_SPAWN_STMT
:
3304 case ARRAY_NOTATION_REF
:
3309 for (i
= 0; i
< 3; ++i
)
3310 if (!potential_constant_expression_1 (TREE_OPERAND (t
, i
),
3317 /* If the condition is a known constant, we know which of the legs we
3318 care about; otherwise we only require that the condition and
3319 either of the legs be potentially constant. */
3320 tmp
= TREE_OPERAND (t
, 0);
3321 if (!potential_constant_expression_1 (tmp
, rval
, flags
))
3323 if (!processing_template_decl
)
3324 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
3325 if (integer_zerop (tmp
))
3326 return potential_constant_expression_1 (TREE_OPERAND (t
, 2),
3328 else if (TREE_CODE (tmp
) == INTEGER_CST
)
3329 return potential_constant_expression_1 (TREE_OPERAND (t
, 1),
3331 for (i
= 1; i
< 3; ++i
)
3332 if (potential_constant_expression_1 (TREE_OPERAND (t
, i
),
3333 want_rval
, tf_none
))
3335 if (flags
& tf_error
)
3336 error ("expression %qE is not a constant-expression", t
);
3340 if (VEC_INIT_EXPR_IS_CONSTEXPR (t
))
3342 if (flags
& tf_error
)
3344 error ("non-constant array initialization");
3345 diagnose_non_constexpr_vec_init (t
);
3350 if (objc_is_property_ref (t
))
3353 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t
)));
3359 /* The main entry point to the above. */
3362 potential_constant_expression (tree t
)
3364 return potential_constant_expression_1 (t
, false, tf_none
);
3367 /* As above, but require a constant rvalue. */
3370 potential_rvalue_constant_expression (tree t
)
3372 return potential_constant_expression_1 (t
, true, tf_none
);
3375 /* Like above, but complain about non-constant expressions. */
3378 require_potential_constant_expression (tree t
)
3380 return potential_constant_expression_1 (t
, false, tf_warning_or_error
);
3383 /* Cross product of the above. */
3386 require_potential_rvalue_constant_expression (tree t
)
3388 return potential_constant_expression_1 (t
, true, tf_warning_or_error
);
3391 #include "gt-cp-constexpr.h"