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 /* The constexpr expansion context. CALL is the current function
852 expansion, CTOR is the current aggregate initializer, OBJECT is the
853 object being initialized by CTOR, either a VAR_DECL or a _REF. VALUES
854 is a map of values of variables initialized within the expression. */
856 struct constexpr_ctx
{
857 constexpr_call
*call
;
858 hash_map
<tree
,tree
> *values
;
863 /* A table of all constexpr calls that have been evaluated by the
864 compiler in this translation unit. */
866 static GTY (()) hash_table
<constexpr_call_hasher
> *constexpr_call_table
;
868 static tree
cxx_eval_constant_expression (const constexpr_ctx
*, tree
,
869 bool, bool, bool *, bool *);
871 /* Compute a hash value for a constexpr call representation. */
874 constexpr_call_hasher::hash (constexpr_call
*info
)
879 /* Return true if the objects pointed to by P and Q represent calls
880 to the same constexpr function with the same arguments.
881 Otherwise, return false. */
884 constexpr_call_hasher::equal (constexpr_call
*lhs
, constexpr_call
*rhs
)
890 if (!constexpr_fundef_hasher::equal (lhs
->fundef
, rhs
->fundef
))
892 lhs_bindings
= lhs
->bindings
;
893 rhs_bindings
= rhs
->bindings
;
894 while (lhs_bindings
!= NULL
&& rhs_bindings
!= NULL
)
896 tree lhs_arg
= TREE_VALUE (lhs_bindings
);
897 tree rhs_arg
= TREE_VALUE (rhs_bindings
);
898 gcc_assert (TREE_TYPE (lhs_arg
) == TREE_TYPE (rhs_arg
));
899 if (!cp_tree_equal (lhs_arg
, rhs_arg
))
901 lhs_bindings
= TREE_CHAIN (lhs_bindings
);
902 rhs_bindings
= TREE_CHAIN (rhs_bindings
);
904 return lhs_bindings
== rhs_bindings
;
907 /* Initialize the constexpr call table, if needed. */
910 maybe_initialize_constexpr_call_table (void)
912 if (constexpr_call_table
== NULL
)
913 constexpr_call_table
= hash_table
<constexpr_call_hasher
>::create_ggc (101);
916 /* We have an expression tree T that represents a call, either CALL_EXPR
917 or AGGR_INIT_EXPR. If the call is lexically to a named function,
918 retrun the _DECL for that function. */
921 get_function_named_in_call (tree t
)
924 switch (TREE_CODE (t
))
927 fun
= CALL_EXPR_FN (t
);
931 fun
= AGGR_INIT_EXPR_FN (t
);
938 if (TREE_CODE (fun
) == ADDR_EXPR
939 && TREE_CODE (TREE_OPERAND (fun
, 0)) == FUNCTION_DECL
)
940 fun
= TREE_OPERAND (fun
, 0);
944 /* We have an expression tree T that represents a call, either CALL_EXPR
945 or AGGR_INIT_EXPR. Return the Nth argument. */
948 get_nth_callarg (tree t
, int n
)
950 switch (TREE_CODE (t
))
953 return CALL_EXPR_ARG (t
, n
);
956 return AGGR_INIT_EXPR_ARG (t
, n
);
964 /* Look up the binding of the function parameter T in a constexpr
965 function call context CALL. */
968 lookup_parameter_binding (const constexpr_call
*call
, tree t
)
970 tree b
= purpose_member (t
, call
->bindings
);
971 return TREE_VALUE (b
);
974 /* Attempt to evaluate T which represents a call to a builtin function.
975 We assume here that all builtin functions evaluate to scalar types
976 represented by _CST nodes. */
979 cxx_eval_builtin_function_call (const constexpr_ctx
*ctx
, tree t
,
980 bool allow_non_constant
, bool addr
,
981 bool *non_constant_p
, bool *overflow_p
)
983 const int nargs
= call_expr_nargs (t
);
984 tree
*args
= (tree
*) alloca (nargs
* sizeof (tree
));
987 for (i
= 0; i
< nargs
; ++i
)
989 args
[i
] = cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, i
),
990 allow_non_constant
, addr
,
991 non_constant_p
, overflow_p
);
992 if (allow_non_constant
&& *non_constant_p
)
997 new_call
= build_call_array_loc (EXPR_LOCATION (t
), TREE_TYPE (t
),
998 CALL_EXPR_FN (t
), nargs
, args
);
999 new_call
= fold (new_call
);
1000 VERIFY_CONSTANT (new_call
);
1004 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1005 the type of the value to match. */
1008 adjust_temp_type (tree type
, tree temp
)
1010 if (TREE_TYPE (temp
) == type
)
1012 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1013 if (TREE_CODE (temp
) == CONSTRUCTOR
)
1014 return build_constructor (type
, CONSTRUCTOR_ELTS (temp
));
1015 gcc_assert (scalarish_type_p (type
));
1016 return cp_fold_convert (type
, temp
);
1019 /* Subroutine of cxx_eval_call_expression.
1020 We are processing a call expression (either CALL_EXPR or
1021 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1022 all arguments and bind their values to correspondings
1023 parameters, making up the NEW_CALL context. */
1026 cxx_bind_parameters_in_call (const constexpr_ctx
*ctx
, tree t
,
1027 constexpr_call
*new_call
,
1028 bool allow_non_constant
,
1029 bool *non_constant_p
, bool *overflow_p
)
1031 const int nargs
= call_expr_nargs (t
);
1032 tree fun
= new_call
->fundef
->decl
;
1033 tree parms
= DECL_ARGUMENTS (fun
);
1035 for (i
= 0; i
< nargs
; ++i
)
1038 tree type
= parms
? TREE_TYPE (parms
) : void_type_node
;
1039 x
= get_nth_callarg (t
, i
);
1040 /* For member function, the first argument is a pointer to the implied
1041 object. For a constructor, it might still be a dummy object, in
1042 which case we get the real argument from ctx or the AGGR_INIT_EXPR. */
1043 if (i
== 0 && DECL_CONSTRUCTOR_P (fun
)
1044 && is_dummy_object (x
))
1048 x
= AGGR_INIT_EXPR_SLOT (t
);
1049 x
= cp_build_addr_expr (x
, tf_warning_or_error
);
1051 if (parms
&& DECL_BY_REFERENCE (parms
))
1053 /* cp_genericize made this a reference for argument passing, but
1054 we don't want to treat it like one for constexpr evaluation. */
1055 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
1056 gcc_assert (TREE_CODE (TREE_TYPE (x
)) == REFERENCE_TYPE
);
1057 type
= TREE_TYPE (type
);
1058 x
= convert_from_reference (x
);
1060 arg
= cxx_eval_constant_expression (ctx
, x
, allow_non_constant
,
1061 TREE_CODE (type
) == REFERENCE_TYPE
,
1062 non_constant_p
, overflow_p
);
1063 /* Don't VERIFY_CONSTANT here. */
1064 if (*non_constant_p
&& allow_non_constant
)
1066 /* Just discard ellipsis args after checking their constantitude. */
1069 if (*non_constant_p
)
1070 /* Don't try to adjust the type of non-constant args. */
1073 /* Make sure the binding has the same type as the parm. */
1074 if (TREE_CODE (type
) != REFERENCE_TYPE
)
1075 arg
= adjust_temp_type (type
, arg
);
1076 new_call
->bindings
= tree_cons (parms
, arg
, new_call
->bindings
);
1078 parms
= TREE_CHAIN (parms
);
1082 /* Variables and functions to manage constexpr call expansion context.
1083 These do not need to be marked for PCH or GC. */
1085 /* FIXME remember and print actual constant arguments. */
1086 static vec
<tree
> call_stack
= vNULL
;
1087 static int call_stack_tick
;
1088 static int last_cx_error_tick
;
1091 push_cx_call_context (tree call
)
1094 if (!EXPR_HAS_LOCATION (call
))
1095 SET_EXPR_LOCATION (call
, input_location
);
1096 call_stack
.safe_push (call
);
1097 if (call_stack
.length () > (unsigned) max_constexpr_depth
)
1103 pop_cx_call_context (void)
1110 cx_error_context (void)
1112 vec
<tree
> r
= vNULL
;
1113 if (call_stack_tick
!= last_cx_error_tick
1114 && !call_stack
.is_empty ())
1116 last_cx_error_tick
= call_stack_tick
;
1120 /* Subroutine of cxx_eval_constant_expression.
1121 Evaluate the call expression tree T in the context of OLD_CALL expression
1125 cxx_eval_call_expression (const constexpr_ctx
*ctx
, tree t
,
1126 bool allow_non_constant
, bool addr
,
1127 bool *non_constant_p
, bool *overflow_p
)
1129 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
1130 tree fun
= get_function_named_in_call (t
);
1132 constexpr_call new_call
= { NULL
, NULL
, NULL
, 0 };
1133 constexpr_call
**slot
;
1134 constexpr_call
*entry
;
1137 if (TREE_CODE (fun
) != FUNCTION_DECL
)
1139 /* Might be a constexpr function pointer. */
1140 fun
= cxx_eval_constant_expression (ctx
, fun
, allow_non_constant
,
1141 /*addr*/false, non_constant_p
,
1144 if (TREE_CODE (fun
) == ADDR_EXPR
)
1145 fun
= TREE_OPERAND (fun
, 0);
1147 if (TREE_CODE (fun
) != FUNCTION_DECL
)
1149 if (!allow_non_constant
&& !*non_constant_p
)
1150 error_at (loc
, "expression %qE does not designate a constexpr "
1152 *non_constant_p
= true;
1155 if (DECL_CLONED_FUNCTION_P (fun
))
1156 fun
= DECL_CLONED_FUNCTION (fun
);
1157 if (is_builtin_fn (fun
))
1158 return cxx_eval_builtin_function_call (ctx
, t
, allow_non_constant
,
1159 addr
, non_constant_p
, overflow_p
);
1160 if (!DECL_DECLARED_CONSTEXPR_P (fun
))
1162 if (!allow_non_constant
)
1164 error_at (loc
, "call to non-constexpr function %qD", fun
);
1165 explain_invalid_constexpr_fn (fun
);
1167 *non_constant_p
= true;
1171 /* Shortcut trivial constructor/op=. */
1172 if (trivial_fn_p (fun
))
1174 if (call_expr_nargs (t
) == 2)
1176 tree arg
= convert_from_reference (get_nth_callarg (t
, 1));
1177 return cxx_eval_constant_expression (ctx
, arg
, allow_non_constant
,
1178 addr
, non_constant_p
, overflow_p
);
1180 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
1181 && AGGR_INIT_ZERO_FIRST (t
))
1182 return build_zero_init (DECL_CONTEXT (fun
), NULL_TREE
, false);
1185 /* If in direct recursive call, optimize definition search. */
1186 if (ctx
&& ctx
->call
&& ctx
->call
->fundef
->decl
== fun
)
1187 new_call
.fundef
= ctx
->call
->fundef
;
1190 new_call
.fundef
= retrieve_constexpr_fundef (fun
);
1191 if (new_call
.fundef
== NULL
|| new_call
.fundef
->body
== NULL
)
1193 if (!allow_non_constant
)
1195 if (DECL_INITIAL (fun
))
1197 /* The definition of fun was somehow unsuitable. */
1198 error_at (loc
, "%qD called in a constant expression", fun
);
1199 explain_invalid_constexpr_fn (fun
);
1202 error_at (loc
, "%qD used before its definition", fun
);
1204 *non_constant_p
= true;
1208 cxx_bind_parameters_in_call (ctx
, t
, &new_call
,
1209 allow_non_constant
, non_constant_p
, overflow_p
);
1210 if (*non_constant_p
)
1213 depth_ok
= push_cx_call_context (t
);
1216 = iterative_hash_template_arg (new_call
.bindings
,
1217 constexpr_fundef_hasher::hash (new_call
.fundef
));
1219 /* If we have seen this call before, we are done. */
1220 maybe_initialize_constexpr_call_table ();
1221 slot
= constexpr_call_table
->find_slot (&new_call
, INSERT
);
1225 /* We need to keep a pointer to the entry, not just the slot, as the
1226 slot can move in the call to cxx_eval_builtin_function_call. */
1227 *slot
= entry
= ggc_alloc
<constexpr_call
> ();
1230 /* Calls which are in progress have their result set to NULL
1231 so that we can detect circular dependencies. */
1232 else if (entry
->result
== NULL
)
1234 if (!allow_non_constant
)
1235 error ("call has circular dependency");
1236 *non_constant_p
= true;
1237 entry
->result
= result
= error_mark_node
;
1242 if (!allow_non_constant
)
1243 error ("constexpr evaluation depth exceeds maximum of %d (use "
1244 "-fconstexpr-depth= to increase the maximum)",
1245 max_constexpr_depth
);
1246 *non_constant_p
= true;
1247 entry
->result
= result
= error_mark_node
;
1251 result
= entry
->result
;
1252 if (!result
|| result
== error_mark_node
)
1254 constexpr_ctx new_ctx
= *ctx
;
1255 new_ctx
.call
= &new_call
;
1256 result
= (cxx_eval_constant_expression
1257 (&new_ctx
, new_call
.fundef
->body
,
1258 allow_non_constant
, addr
,
1259 non_constant_p
, overflow_p
));
1261 if (result
== error_mark_node
)
1262 *non_constant_p
= true;
1263 if (*non_constant_p
)
1264 entry
->result
= result
= error_mark_node
;
1267 /* If this was a call to initialize an object, set the type of
1268 the CONSTRUCTOR to the type of that object. */
1269 if (DECL_CONSTRUCTOR_P (fun
))
1271 tree ob_arg
= get_nth_callarg (t
, 0);
1272 STRIP_NOPS (ob_arg
);
1273 gcc_assert (TYPE_PTR_P (TREE_TYPE (ob_arg
))
1274 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (ob_arg
))));
1275 result
= adjust_temp_type (TREE_TYPE (TREE_TYPE (ob_arg
)),
1278 entry
->result
= result
;
1282 pop_cx_call_context ();
1283 return unshare_expr (result
);
1286 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
1289 reduced_constant_expression_p (tree t
)
1291 switch (TREE_CODE (t
))
1294 /* Even if we can't lower this yet, it's constant. */
1298 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
1299 tree elt
; unsigned HOST_WIDE_INT idx
;
1300 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), idx
, elt
)
1301 if (!reduced_constant_expression_p (elt
))
1306 /* FIXME are we calling this too much? */
1307 return initializer_constant_valid_p (t
, TREE_TYPE (t
)) != NULL_TREE
;
1311 /* Some expressions may have constant operands but are not constant
1312 themselves, such as 1/0. Call this function (or rather, the macro
1313 following it) to check for that condition.
1315 We only call this in places that require an arithmetic constant, not in
1316 places where we might have a non-constant expression that can be a
1317 component of a constant expression, such as the address of a constexpr
1318 variable that might be dereferenced later. */
1321 verify_constant (tree t
, bool allow_non_constant
, bool *non_constant_p
,
1324 if (!*non_constant_p
&& !reduced_constant_expression_p (t
))
1326 if (!allow_non_constant
)
1327 error ("%q+E is not a constant expression", t
);
1328 *non_constant_p
= true;
1330 if (TREE_OVERFLOW_P (t
))
1332 if (!allow_non_constant
)
1334 permerror (input_location
, "overflow in constant expression");
1335 /* If we're being permissive (and are in an enforcing
1336 context), ignore the overflow. */
1337 if (flag_permissive
)
1338 return *non_constant_p
;
1342 return *non_constant_p
;
1345 /* Subroutine of cxx_eval_constant_expression.
1346 Attempt to reduce the unary expression tree T to a compile time value.
1347 If successful, return the value. Otherwise issue a diagnostic
1348 and return error_mark_node. */
1351 cxx_eval_unary_expression (const constexpr_ctx
*ctx
, tree t
,
1352 bool allow_non_constant
, bool addr
,
1353 bool *non_constant_p
, bool *overflow_p
)
1356 tree orig_arg
= TREE_OPERAND (t
, 0);
1357 tree arg
= cxx_eval_constant_expression (ctx
, orig_arg
, allow_non_constant
,
1358 addr
, non_constant_p
, overflow_p
);
1359 VERIFY_CONSTANT (arg
);
1360 if (arg
== orig_arg
)
1362 r
= fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), arg
);
1363 VERIFY_CONSTANT (r
);
1367 /* Subroutine of cxx_eval_constant_expression.
1368 Like cxx_eval_unary_expression, except for binary expressions. */
1371 cxx_eval_binary_expression (const constexpr_ctx
*ctx
, tree t
,
1372 bool allow_non_constant
, bool addr
,
1373 bool *non_constant_p
, bool *overflow_p
)
1376 tree orig_lhs
= TREE_OPERAND (t
, 0);
1377 tree orig_rhs
= TREE_OPERAND (t
, 1);
1379 lhs
= cxx_eval_constant_expression (ctx
, orig_lhs
,
1380 allow_non_constant
, addr
,
1381 non_constant_p
, overflow_p
);
1382 VERIFY_CONSTANT (lhs
);
1383 rhs
= cxx_eval_constant_expression (ctx
, orig_rhs
,
1384 allow_non_constant
, addr
,
1385 non_constant_p
, overflow_p
);
1386 VERIFY_CONSTANT (rhs
);
1387 if (lhs
== orig_lhs
&& rhs
== orig_rhs
)
1389 r
= fold_build2 (TREE_CODE (t
), TREE_TYPE (t
), lhs
, rhs
);
1390 VERIFY_CONSTANT (r
);
1394 /* Subroutine of cxx_eval_constant_expression.
1395 Attempt to evaluate condition expressions. Dead branches are not
1399 cxx_eval_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
1400 bool allow_non_constant
, bool addr
,
1401 bool *non_constant_p
, bool *overflow_p
)
1403 tree val
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
1404 allow_non_constant
, addr
,
1405 non_constant_p
, overflow_p
);
1406 VERIFY_CONSTANT (val
);
1407 /* Don't VERIFY_CONSTANT the other operands. */
1408 if (integer_zerop (val
))
1409 return cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 2),
1410 allow_non_constant
, addr
,
1411 non_constant_p
, overflow_p
);
1412 return cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
1413 allow_non_constant
, addr
,
1414 non_constant_p
, overflow_p
);
1417 /* Subroutine of cxx_eval_constant_expression.
1418 Attempt to reduce a reference to an array slot. */
1421 cxx_eval_array_reference (const constexpr_ctx
*ctx
, tree t
,
1422 bool allow_non_constant
, bool addr
,
1423 bool *non_constant_p
, bool *overflow_p
)
1425 tree oldary
= TREE_OPERAND (t
, 0);
1426 tree ary
= cxx_eval_constant_expression (ctx
, oldary
,
1427 allow_non_constant
, addr
,
1428 non_constant_p
, overflow_p
);
1432 unsigned len
, elem_nchars
= 1;
1433 if (*non_constant_p
)
1435 oldidx
= TREE_OPERAND (t
, 1);
1436 index
= cxx_eval_constant_expression (ctx
, oldidx
,
1437 allow_non_constant
, false,
1438 non_constant_p
, overflow_p
);
1439 VERIFY_CONSTANT (index
);
1440 if (addr
&& ary
== oldary
&& index
== oldidx
)
1443 return build4 (ARRAY_REF
, TREE_TYPE (t
), ary
, index
, NULL
, NULL
);
1444 elem_type
= TREE_TYPE (TREE_TYPE (ary
));
1445 if (TREE_CODE (ary
) == CONSTRUCTOR
)
1446 len
= CONSTRUCTOR_NELTS (ary
);
1447 else if (TREE_CODE (ary
) == STRING_CST
)
1449 elem_nchars
= (TYPE_PRECISION (elem_type
)
1450 / TYPE_PRECISION (char_type_node
));
1451 len
= (unsigned) TREE_STRING_LENGTH (ary
) / elem_nchars
;
1455 /* We can't do anything with other tree codes, so use
1456 VERIFY_CONSTANT to complain and fail. */
1457 VERIFY_CONSTANT (ary
);
1460 if (compare_tree_int (index
, len
) >= 0)
1462 if (tree_int_cst_lt (index
, array_type_nelts_top (TREE_TYPE (ary
))))
1464 /* If it's within the array bounds but doesn't have an explicit
1465 initializer, it's value-initialized. */
1466 tree val
= build_value_init (elem_type
, tf_warning_or_error
);
1467 return cxx_eval_constant_expression (ctx
, val
,
1468 allow_non_constant
, addr
,
1469 non_constant_p
, overflow_p
);
1472 if (!allow_non_constant
)
1473 error ("array subscript out of bound");
1474 *non_constant_p
= true;
1477 else if (tree_int_cst_lt (index
, integer_zero_node
))
1479 if (!allow_non_constant
)
1480 error ("negative array subscript");
1481 *non_constant_p
= true;
1484 i
= tree_to_shwi (index
);
1485 if (TREE_CODE (ary
) == CONSTRUCTOR
)
1486 return (*CONSTRUCTOR_ELTS (ary
))[i
].value
;
1487 else if (elem_nchars
== 1)
1488 return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary
))),
1489 TREE_STRING_POINTER (ary
)[i
]);
1492 tree type
= cv_unqualified (TREE_TYPE (TREE_TYPE (ary
)));
1493 return native_interpret_expr (type
, (const unsigned char *)
1494 TREE_STRING_POINTER (ary
)
1495 + i
* elem_nchars
, elem_nchars
);
1497 /* Don't VERIFY_CONSTANT here. */
1500 /* Subroutine of cxx_eval_constant_expression.
1501 Attempt to reduce a field access of a value of class type. */
1504 cxx_eval_component_reference (const constexpr_ctx
*ctx
, tree t
,
1505 bool allow_non_constant
, bool addr
,
1506 bool *non_constant_p
, bool *overflow_p
)
1508 unsigned HOST_WIDE_INT i
;
1511 tree part
= TREE_OPERAND (t
, 1);
1512 tree orig_whole
= TREE_OPERAND (t
, 0);
1513 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
1514 allow_non_constant
, addr
,
1515 non_constant_p
, overflow_p
);
1516 if (whole
== orig_whole
)
1519 return fold_build3 (COMPONENT_REF
, TREE_TYPE (t
),
1520 whole
, part
, NULL_TREE
);
1521 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
1523 if (!*non_constant_p
&& TREE_CODE (whole
) != CONSTRUCTOR
)
1525 if (!allow_non_constant
)
1526 error ("%qE is not a constant expression", orig_whole
);
1527 *non_constant_p
= true;
1529 if (DECL_MUTABLE_P (part
))
1531 if (!allow_non_constant
)
1532 error ("mutable %qD is not usable in a constant expression", part
);
1533 *non_constant_p
= true;
1535 if (*non_constant_p
)
1537 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
1542 if (TREE_CODE (TREE_TYPE (whole
)) == UNION_TYPE
1543 && CONSTRUCTOR_NELTS (whole
) > 0)
1545 /* DR 1188 says we don't have to deal with this. */
1546 if (!allow_non_constant
)
1547 error ("accessing %qD member instead of initialized %qD member in "
1548 "constant expression", part
, CONSTRUCTOR_ELT (whole
, 0)->index
);
1549 *non_constant_p
= true;
1553 if (CONSTRUCTOR_NO_IMPLICIT_ZERO (whole
))
1555 /* 'whole' is part of the aggregate initializer we're currently
1556 building; if there's no initializer for this member yet, that's an
1558 if (!allow_non_constant
)
1559 error ("accessing uninitialized member %qD", part
);
1560 *non_constant_p
= true;
1564 /* If there's no explicit init for this field, it's value-initialized. */
1565 value
= build_value_init (TREE_TYPE (t
), tf_warning_or_error
);
1566 return cxx_eval_constant_expression (ctx
, value
,
1567 allow_non_constant
, addr
,
1568 non_constant_p
, overflow_p
);
1571 /* Subroutine of cxx_eval_constant_expression.
1572 Attempt to reduce a field access of a value of class type that is
1573 expressed as a BIT_FIELD_REF. */
1576 cxx_eval_bit_field_ref (const constexpr_ctx
*ctx
, tree t
,
1577 bool allow_non_constant
, bool addr
,
1578 bool *non_constant_p
, bool *overflow_p
)
1580 tree orig_whole
= TREE_OPERAND (t
, 0);
1581 tree retval
, fldval
, utype
, mask
;
1582 bool fld_seen
= false;
1583 HOST_WIDE_INT istart
, isize
;
1584 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
1585 allow_non_constant
, addr
,
1586 non_constant_p
, overflow_p
);
1587 tree start
, field
, value
;
1588 unsigned HOST_WIDE_INT i
;
1590 if (whole
== orig_whole
)
1592 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
1594 if (!*non_constant_p
1595 && TREE_CODE (whole
) != VECTOR_CST
1596 && TREE_CODE (whole
) != CONSTRUCTOR
)
1598 if (!allow_non_constant
)
1599 error ("%qE is not a constant expression", orig_whole
);
1600 *non_constant_p
= true;
1602 if (*non_constant_p
)
1605 if (TREE_CODE (whole
) == VECTOR_CST
)
1606 return fold_ternary (BIT_FIELD_REF
, TREE_TYPE (t
), whole
,
1607 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 2));
1609 start
= TREE_OPERAND (t
, 2);
1610 istart
= tree_to_shwi (start
);
1611 isize
= tree_to_shwi (TREE_OPERAND (t
, 1));
1612 utype
= TREE_TYPE (t
);
1613 if (!TYPE_UNSIGNED (utype
))
1614 utype
= build_nonstandard_integer_type (TYPE_PRECISION (utype
), 1);
1615 retval
= build_int_cst (utype
, 0);
1616 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
1618 tree bitpos
= bit_position (field
);
1619 if (bitpos
== start
&& DECL_SIZE (field
) == TREE_OPERAND (t
, 1))
1621 if (TREE_CODE (TREE_TYPE (field
)) == INTEGER_TYPE
1622 && TREE_CODE (value
) == INTEGER_CST
1623 && tree_fits_shwi_p (bitpos
)
1624 && tree_fits_shwi_p (DECL_SIZE (field
)))
1626 HOST_WIDE_INT bit
= tree_to_shwi (bitpos
);
1627 HOST_WIDE_INT sz
= tree_to_shwi (DECL_SIZE (field
));
1628 HOST_WIDE_INT shift
;
1629 if (bit
>= istart
&& bit
+ sz
<= istart
+ isize
)
1631 fldval
= fold_convert (utype
, value
);
1632 mask
= build_int_cst_type (utype
, -1);
1633 mask
= fold_build2 (LSHIFT_EXPR
, utype
, mask
,
1634 size_int (TYPE_PRECISION (utype
) - sz
));
1635 mask
= fold_build2 (RSHIFT_EXPR
, utype
, mask
,
1636 size_int (TYPE_PRECISION (utype
) - sz
));
1637 fldval
= fold_build2 (BIT_AND_EXPR
, utype
, fldval
, mask
);
1638 shift
= bit
- istart
;
1639 if (BYTES_BIG_ENDIAN
)
1640 shift
= TYPE_PRECISION (utype
) - shift
- sz
;
1641 fldval
= fold_build2 (LSHIFT_EXPR
, utype
, fldval
,
1643 retval
= fold_build2 (BIT_IOR_EXPR
, utype
, retval
, fldval
);
1649 return fold_convert (TREE_TYPE (t
), retval
);
1651 return error_mark_node
;
1654 /* Subroutine of cxx_eval_constant_expression.
1655 Evaluate a short-circuited logical expression T in the context
1656 of a given constexpr CALL. BAILOUT_VALUE is the value for
1657 early return. CONTINUE_VALUE is used here purely for
1658 sanity check purposes. */
1661 cxx_eval_logical_expression (const constexpr_ctx
*ctx
, tree t
,
1662 tree bailout_value
, tree continue_value
,
1663 bool allow_non_constant
, bool addr
,
1664 bool *non_constant_p
, bool *overflow_p
)
1667 tree lhs
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
1668 allow_non_constant
, addr
,
1669 non_constant_p
, overflow_p
);
1670 VERIFY_CONSTANT (lhs
);
1671 if (tree_int_cst_equal (lhs
, bailout_value
))
1673 gcc_assert (tree_int_cst_equal (lhs
, continue_value
));
1674 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
1675 allow_non_constant
, addr
, non_constant_p
, overflow_p
);
1676 VERIFY_CONSTANT (r
);
1680 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
1681 CONSTRUCTOR elements to initialize (part of) an object containing that
1682 field. Return a pointer to the constructor_elt corresponding to the
1683 initialization of the field. */
1685 static constructor_elt
*
1686 base_field_constructor_elt (vec
<constructor_elt
, va_gc
> *v
, tree ref
)
1688 tree aggr
= TREE_OPERAND (ref
, 0);
1689 tree field
= TREE_OPERAND (ref
, 1);
1691 constructor_elt
*ce
;
1693 gcc_assert (TREE_CODE (ref
) == COMPONENT_REF
);
1695 if (TREE_CODE (aggr
) == COMPONENT_REF
)
1697 constructor_elt
*base_ce
1698 = base_field_constructor_elt (v
, aggr
);
1699 v
= CONSTRUCTOR_ELTS (base_ce
->value
);
1702 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
1703 if (ce
->index
== field
)
1710 /* Some of the expressions fed to the constexpr mechanism are calls to
1711 constructors, which have type void. In that case, return the type being
1712 initialized by the constructor. */
1715 initialized_type (tree t
)
1719 tree type
= cv_unqualified (TREE_TYPE (t
));
1720 if (TREE_CODE (t
) == CALL_EXPR
|| TREE_CODE (t
) == AGGR_INIT_EXPR
)
1722 /* A constructor call has void type, so we need to look deeper. */
1723 tree fn
= get_function_named_in_call (t
);
1724 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
1725 && DECL_CXX_CONSTRUCTOR_P (fn
))
1726 type
= DECL_CONTEXT (fn
);
1731 /* We're about to initialize element INDEX of an array or class from VALUE.
1732 Set up NEW_CTX appropriately by adjusting .object to refer to the
1733 subobject and creating a new CONSTRUCTOR if the element is itself
1734 a class or array. */
1737 init_subob_ctx (const constexpr_ctx
*ctx
, constexpr_ctx
&new_ctx
,
1738 tree index
, tree
&value
)
1742 if (index
&& TREE_CODE (index
) != INTEGER_CST
1743 && TREE_CODE (index
) != FIELD_DECL
)
1744 /* This won't have an element in the new CONSTRUCTOR. */
1747 tree type
= initialized_type (value
);
1748 if (!AGGREGATE_TYPE_P (type
) && !VECTOR_TYPE_P (type
))
1749 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
1752 /* The sub-aggregate initializer might contain a placeholder;
1753 update object to refer to the subobject and ctor to refer to
1754 the (newly created) sub-initializer. */
1756 new_ctx
.object
= build_ctor_subob_ref (index
, type
, ctx
->object
);
1757 tree elt
= build_constructor (type
, NULL
);
1758 CONSTRUCTOR_NO_IMPLICIT_ZERO (elt
) = true;
1761 if (TREE_CODE (value
) == TARGET_EXPR
)
1762 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
1763 value
= TARGET_EXPR_INITIAL (value
);
1766 /* We're about to process an initializer for a class or array TYPE. Make
1767 sure that CTX is set up appropriately. */
1770 verify_ctor_sanity (const constexpr_ctx
*ctx
, tree type
)
1772 /* We don't bother building a ctor for an empty base subobject. */
1773 if (is_empty_class (type
))
1776 /* We're in the middle of an initializer that might involve placeholders;
1777 our caller should have created a CONSTRUCTOR for us to put the
1778 initializer into. We will either return that constructor or T. */
1779 gcc_assert (ctx
->ctor
);
1780 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1781 (type
, TREE_TYPE (ctx
->ctor
)));
1782 gcc_assert (CONSTRUCTOR_NELTS (ctx
->ctor
) == 0);
1784 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1785 (type
, TREE_TYPE (ctx
->object
)));
1786 gcc_assert (!ctx
->object
|| !DECL_P (ctx
->object
)
1787 || *(ctx
->values
->get (ctx
->object
)) == ctx
->ctor
);
1790 /* Subroutine of cxx_eval_constant_expression.
1791 The expression tree T denotes a C-style array or a C-style
1792 aggregate. Reduce it to a constant expression. */
1795 cxx_eval_bare_aggregate (const constexpr_ctx
*ctx
, tree t
,
1796 bool allow_non_constant
, bool addr
,
1797 bool *non_constant_p
, bool *overflow_p
)
1799 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
1800 bool changed
= false;
1801 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t
));
1803 verify_ctor_sanity (ctx
, TREE_TYPE (t
));
1804 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
1805 vec_alloc (*p
, vec_safe_length (v
));
1807 unsigned i
; tree index
, value
;
1808 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, index
, value
)
1810 constexpr_ctx new_ctx
;
1811 init_subob_ctx (ctx
, new_ctx
, index
, value
);
1812 if (new_ctx
.ctor
!= ctx
->ctor
)
1813 /* If we built a new CONSTRUCTOR, attach it now so that other
1814 initializers can refer to it. */
1815 CONSTRUCTOR_APPEND_ELT (*p
, index
, new_ctx
.ctor
);
1816 tree elt
= cxx_eval_constant_expression (&new_ctx
, value
,
1817 allow_non_constant
, addr
,
1818 non_constant_p
, overflow_p
);
1819 /* Don't VERIFY_CONSTANT here. */
1820 if (allow_non_constant
&& *non_constant_p
)
1824 if (index
&& TREE_CODE (index
) == COMPONENT_REF
)
1826 /* This is an initialization of a vfield inside a base
1827 subaggregate that we already initialized; push this
1828 initialization into the previous initialization. */
1829 constructor_elt
*inner
= base_field_constructor_elt (*p
, index
);
1834 && (TREE_CODE (index
) == NOP_EXPR
1835 || TREE_CODE (index
) == POINTER_PLUS_EXPR
))
1837 /* This is an initializer for an empty base; now that we've
1838 checked that it's constant, we can ignore it. */
1839 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index
))));
1842 else if (new_ctx
.ctor
!= ctx
->ctor
)
1844 /* We appended this element above; update the value. */
1845 gcc_assert ((*p
)->last().index
== index
);
1846 (*p
)->last().value
= elt
;
1849 CONSTRUCTOR_APPEND_ELT (*p
, index
, elt
);
1851 if (*non_constant_p
|| !changed
)
1854 /* We're done building this CONSTRUCTOR, so now we can interpret an
1855 element without an explicit initializer as value-initialized. */
1856 CONSTRUCTOR_NO_IMPLICIT_ZERO (t
) = false;
1857 if (TREE_CODE (TREE_TYPE (t
)) == VECTOR_TYPE
)
1862 /* Subroutine of cxx_eval_constant_expression.
1863 The expression tree T is a VEC_INIT_EXPR which denotes the desired
1864 initialization of a non-static data member of array type. Reduce it to a
1867 Note that apart from value-initialization (when VALUE_INIT is true),
1868 this is only intended to support value-initialization and the
1869 initializations done by defaulted constructors for classes with
1870 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
1871 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
1872 for the copy/move constructor. */
1875 cxx_eval_vec_init_1 (const constexpr_ctx
*ctx
, tree atype
, tree init
,
1876 bool value_init
, bool allow_non_constant
, bool addr
,
1877 bool *non_constant_p
, bool *overflow_p
)
1879 tree elttype
= TREE_TYPE (atype
);
1880 int max
= tree_to_shwi (array_type_nelts (atype
));
1881 verify_ctor_sanity (ctx
, atype
);
1882 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
1883 vec_alloc (*p
, max
+ 1);
1884 bool pre_init
= false;
1887 /* For the default constructor, build up a call to the default
1888 constructor of the element type. We only need to handle class types
1889 here, as for a constructor to be constexpr, all members must be
1890 initialized, which for a defaulted default constructor means they must
1891 be of a class type with a constexpr default constructor. */
1892 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
1893 /* We only do this at the lowest level. */;
1894 else if (value_init
)
1896 init
= build_value_init (elttype
, tf_warning_or_error
);
1901 vec
<tree
, va_gc
> *argvec
= make_tree_vector ();
1902 init
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
1903 &argvec
, elttype
, LOOKUP_NORMAL
,
1904 tf_warning_or_error
);
1905 release_tree_vector (argvec
);
1909 for (i
= 0; i
<= max
; ++i
)
1911 tree idx
= build_int_cst (size_type_node
, i
);
1913 constexpr_ctx new_ctx
;
1914 init_subob_ctx (ctx
, new_ctx
, idx
, pre_init
? init
: elttype
);
1915 if (new_ctx
.ctor
!= ctx
->ctor
)
1916 CONSTRUCTOR_APPEND_ELT (*p
, idx
, new_ctx
.ctor
);
1917 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
1919 /* A multidimensional array; recurse. */
1920 if (value_init
|| init
== NULL_TREE
)
1921 eltinit
= NULL_TREE
;
1923 eltinit
= cp_build_array_ref (input_location
, init
, idx
,
1924 tf_warning_or_error
);
1925 eltinit
= cxx_eval_vec_init_1 (&new_ctx
, elttype
, eltinit
, value_init
,
1926 allow_non_constant
, addr
,
1927 non_constant_p
, overflow_p
);
1931 /* Initializing an element using value or default initialization
1932 we just pre-built above. */
1933 eltinit
= (cxx_eval_constant_expression
1934 (&new_ctx
, init
, allow_non_constant
,
1935 addr
, non_constant_p
, overflow_p
));
1939 /* Copying an element. */
1940 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1941 (atype
, TREE_TYPE (init
)));
1942 eltinit
= cp_build_array_ref (input_location
, init
, idx
,
1943 tf_warning_or_error
);
1944 if (!real_lvalue_p (init
))
1945 eltinit
= move (eltinit
);
1946 eltinit
= force_rvalue (eltinit
, tf_warning_or_error
);
1947 eltinit
= (cxx_eval_constant_expression
1948 (&new_ctx
, eltinit
, allow_non_constant
, addr
,
1949 non_constant_p
, overflow_p
));
1951 if (*non_constant_p
&& !allow_non_constant
)
1953 if (new_ctx
.ctor
!= ctx
->ctor
)
1955 /* We appended this element above; update the value. */
1956 gcc_assert ((*p
)->last().index
== idx
);
1957 (*p
)->last().value
= eltinit
;
1960 CONSTRUCTOR_APPEND_ELT (*p
, idx
, eltinit
);
1963 if (!*non_constant_p
)
1966 CONSTRUCTOR_NO_IMPLICIT_ZERO (init
) = false;
1972 cxx_eval_vec_init (const constexpr_ctx
*ctx
, tree t
,
1973 bool allow_non_constant
, bool addr
,
1974 bool *non_constant_p
, bool *overflow_p
)
1976 tree atype
= TREE_TYPE (t
);
1977 tree init
= VEC_INIT_EXPR_INIT (t
);
1978 tree r
= cxx_eval_vec_init_1 (ctx
, atype
, init
,
1979 VEC_INIT_EXPR_VALUE_INIT (t
),
1980 allow_non_constant
, addr
, non_constant_p
, overflow_p
);
1981 if (*non_constant_p
)
1987 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
1988 match. We want to be less strict for simple *& folding; if we have a
1989 non-const temporary that we access through a const pointer, that should
1990 work. We handle this here rather than change fold_indirect_ref_1
1991 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
1992 don't really make sense outside of constant expression evaluation. Also
1993 we want to allow folding to COMPONENT_REF, which could cause trouble
1994 with TBAA in fold_indirect_ref_1.
1996 Try to keep this function synced with fold_indirect_ref_1. */
1999 cxx_fold_indirect_ref (location_t loc
, tree type
, tree op0
, bool *empty_base
)
2005 subtype
= TREE_TYPE (sub
);
2006 if (!POINTER_TYPE_P (subtype
))
2009 if (TREE_CODE (sub
) == ADDR_EXPR
)
2011 tree op
= TREE_OPERAND (sub
, 0);
2012 tree optype
= TREE_TYPE (op
);
2014 /* *&CONST_DECL -> to the value of the const decl. */
2015 if (TREE_CODE (op
) == CONST_DECL
)
2016 return DECL_INITIAL (op
);
2017 /* *&p => p; make sure to handle *&"str"[cst] here. */
2018 if (same_type_ignoring_top_level_qualifiers_p (optype
, type
))
2020 tree fop
= fold_read_from_constant_string (op
);
2026 /* *(foo *)&fooarray => fooarray[0] */
2027 else if (TREE_CODE (optype
) == ARRAY_TYPE
2028 && (same_type_ignoring_top_level_qualifiers_p
2029 (type
, TREE_TYPE (optype
))))
2031 tree type_domain
= TYPE_DOMAIN (optype
);
2032 tree min_val
= size_zero_node
;
2033 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
2034 min_val
= TYPE_MIN_VALUE (type_domain
);
2035 return build4_loc (loc
, ARRAY_REF
, type
, op
, min_val
,
2036 NULL_TREE
, NULL_TREE
);
2038 /* *(foo *)&complexfoo => __real__ complexfoo */
2039 else if (TREE_CODE (optype
) == COMPLEX_TYPE
2040 && (same_type_ignoring_top_level_qualifiers_p
2041 (type
, TREE_TYPE (optype
))))
2042 return fold_build1_loc (loc
, REALPART_EXPR
, type
, op
);
2043 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
2044 else if (TREE_CODE (optype
) == VECTOR_TYPE
2045 && (same_type_ignoring_top_level_qualifiers_p
2046 (type
, TREE_TYPE (optype
))))
2048 tree part_width
= TYPE_SIZE (type
);
2049 tree index
= bitsize_int (0);
2050 return fold_build3_loc (loc
, BIT_FIELD_REF
, type
, op
, part_width
, index
);
2052 /* Also handle conversion to an empty base class, which
2053 is represented with a NOP_EXPR. */
2054 else if (is_empty_class (type
)
2055 && CLASS_TYPE_P (optype
)
2056 && DERIVED_FROM_P (type
, optype
))
2061 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
2062 else if (RECORD_OR_UNION_TYPE_P (optype
))
2064 tree field
= TYPE_FIELDS (optype
);
2065 for (; field
; field
= DECL_CHAIN (field
))
2066 if (TREE_CODE (field
) == FIELD_DECL
2067 && integer_zerop (byte_position (field
))
2068 && (same_type_ignoring_top_level_qualifiers_p
2069 (TREE_TYPE (field
), type
)))
2071 return fold_build3 (COMPONENT_REF
, type
, op
, field
, NULL_TREE
);
2076 else if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
2077 && TREE_CODE (TREE_OPERAND (sub
, 1)) == INTEGER_CST
)
2079 tree op00
= TREE_OPERAND (sub
, 0);
2080 tree op01
= TREE_OPERAND (sub
, 1);
2083 if (TREE_CODE (op00
) == ADDR_EXPR
)
2086 op00
= TREE_OPERAND (op00
, 0);
2087 op00type
= TREE_TYPE (op00
);
2089 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
2090 if (TREE_CODE (op00type
) == VECTOR_TYPE
2091 && (same_type_ignoring_top_level_qualifiers_p
2092 (type
, TREE_TYPE (op00type
))))
2094 HOST_WIDE_INT offset
= tree_to_shwi (op01
);
2095 tree part_width
= TYPE_SIZE (type
);
2096 unsigned HOST_WIDE_INT part_widthi
= tree_to_shwi (part_width
)/BITS_PER_UNIT
;
2097 unsigned HOST_WIDE_INT indexi
= offset
* BITS_PER_UNIT
;
2098 tree index
= bitsize_int (indexi
);
2100 if (offset
/ part_widthi
< TYPE_VECTOR_SUBPARTS (op00type
))
2101 return fold_build3_loc (loc
,
2102 BIT_FIELD_REF
, type
, op00
,
2106 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
2107 else if (TREE_CODE (op00type
) == COMPLEX_TYPE
2108 && (same_type_ignoring_top_level_qualifiers_p
2109 (type
, TREE_TYPE (op00type
))))
2111 tree size
= TYPE_SIZE_UNIT (type
);
2112 if (tree_int_cst_equal (size
, op01
))
2113 return fold_build1_loc (loc
, IMAGPART_EXPR
, type
, op00
);
2115 /* ((foo *)&fooarray)[1] => fooarray[1] */
2116 else if (TREE_CODE (op00type
) == ARRAY_TYPE
2117 && (same_type_ignoring_top_level_qualifiers_p
2118 (type
, TREE_TYPE (op00type
))))
2120 tree type_domain
= TYPE_DOMAIN (op00type
);
2121 tree min_val
= size_zero_node
;
2122 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
2123 min_val
= TYPE_MIN_VALUE (type_domain
);
2124 op01
= size_binop_loc (loc
, EXACT_DIV_EXPR
, op01
,
2125 TYPE_SIZE_UNIT (type
));
2126 op01
= size_binop_loc (loc
, PLUS_EXPR
, op01
, min_val
);
2127 return build4_loc (loc
, ARRAY_REF
, type
, op00
, op01
,
2128 NULL_TREE
, NULL_TREE
);
2130 /* Also handle conversion to an empty base class, which
2131 is represented with a NOP_EXPR. */
2132 else if (is_empty_class (type
)
2133 && CLASS_TYPE_P (op00type
)
2134 && DERIVED_FROM_P (type
, op00type
))
2139 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
2140 else if (RECORD_OR_UNION_TYPE_P (op00type
))
2142 tree field
= TYPE_FIELDS (op00type
);
2143 for (; field
; field
= DECL_CHAIN (field
))
2144 if (TREE_CODE (field
) == FIELD_DECL
2145 && tree_int_cst_equal (byte_position (field
), op01
)
2146 && (same_type_ignoring_top_level_qualifiers_p
2147 (TREE_TYPE (field
), type
)))
2149 return fold_build3 (COMPONENT_REF
, type
, op00
,
2156 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
2157 else if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
2158 && (same_type_ignoring_top_level_qualifiers_p
2159 (type
, TREE_TYPE (TREE_TYPE (subtype
)))))
2162 tree min_val
= size_zero_node
;
2163 tree newsub
= cxx_fold_indirect_ref (loc
, TREE_TYPE (subtype
), sub
, NULL
);
2167 sub
= build1_loc (loc
, INDIRECT_REF
, TREE_TYPE (subtype
), sub
);
2168 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
2169 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
2170 min_val
= TYPE_MIN_VALUE (type_domain
);
2171 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
2179 cxx_eval_indirect_ref (const constexpr_ctx
*ctx
, tree t
,
2180 bool allow_non_constant
, bool addr
,
2181 bool *non_constant_p
, bool *overflow_p
)
2183 tree orig_op0
= TREE_OPERAND (t
, 0);
2184 tree op0
= cxx_eval_constant_expression (ctx
, orig_op0
, allow_non_constant
,
2185 /*addr*/false, non_constant_p
, overflow_p
);
2186 bool empty_base
= false;
2189 /* Don't VERIFY_CONSTANT here. */
2190 if (*non_constant_p
)
2193 r
= cxx_fold_indirect_ref (EXPR_LOCATION (t
), TREE_TYPE (t
), op0
,
2197 r
= cxx_eval_constant_expression (ctx
, r
, allow_non_constant
,
2198 addr
, non_constant_p
, overflow_p
);
2203 if (TREE_CODE (sub
) == ADDR_EXPR
)
2205 /* We couldn't fold to a constant value. Make sure it's not
2206 something we should have been able to fold. */
2207 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
2208 (TREE_TYPE (TREE_TYPE (sub
)), TREE_TYPE (t
)));
2209 /* DR 1188 says we don't have to deal with this. */
2210 if (!allow_non_constant
)
2211 error ("accessing value of %qE through a %qT glvalue in a "
2212 "constant expression", build_fold_indirect_ref (sub
),
2214 *non_constant_p
= true;
2219 /* If we're pulling out the value of an empty base, make sure
2220 that the whole object is constant and then return an empty
2224 VERIFY_CONSTANT (r
);
2225 r
= build_constructor (TREE_TYPE (t
), NULL
);
2226 TREE_CONSTANT (r
) = true;
2231 if (addr
&& op0
!= orig_op0
)
2232 return build1 (INDIRECT_REF
, TREE_TYPE (t
), op0
);
2234 VERIFY_CONSTANT (t
);
2240 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
2241 Shared between potential_constant_expression and
2242 cxx_eval_constant_expression. */
2245 non_const_var_error (tree r
)
2247 tree type
= TREE_TYPE (r
);
2248 error ("the value of %qD is not usable in a constant "
2250 /* Avoid error cascade. */
2251 if (DECL_INITIAL (r
) == error_mark_node
)
2253 if (DECL_DECLARED_CONSTEXPR_P (r
))
2254 inform (DECL_SOURCE_LOCATION (r
),
2255 "%qD used in its own initializer", r
);
2256 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
2258 if (!CP_TYPE_CONST_P (type
))
2259 inform (DECL_SOURCE_LOCATION (r
),
2260 "%q#D is not const", r
);
2261 else if (CP_TYPE_VOLATILE_P (type
))
2262 inform (DECL_SOURCE_LOCATION (r
),
2263 "%q#D is volatile", r
);
2264 else if (!DECL_INITIAL (r
)
2265 || !TREE_CONSTANT (DECL_INITIAL (r
)))
2266 inform (DECL_SOURCE_LOCATION (r
),
2267 "%qD was not initialized with a constant "
2274 if (cxx_dialect
>= cxx11
&& !DECL_DECLARED_CONSTEXPR_P (r
))
2275 inform (DECL_SOURCE_LOCATION (r
),
2276 "%qD was not declared %<constexpr%>", r
);
2278 inform (DECL_SOURCE_LOCATION (r
),
2279 "%qD does not have integral or enumeration type",
2284 /* Subroutine of cxx_eval_constant_expression.
2285 Like cxx_eval_unary_expression, except for trinary expressions. */
2288 cxx_eval_trinary_expression (const constexpr_ctx
*ctx
, tree t
,
2289 bool allow_non_constant
, bool addr
,
2290 bool *non_constant_p
, bool *overflow_p
)
2296 for (i
= 0; i
< 3; i
++)
2298 args
[i
] = cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, i
),
2299 allow_non_constant
, addr
,
2300 non_constant_p
, overflow_p
);
2301 VERIFY_CONSTANT (args
[i
]);
2304 val
= fold_ternary_loc (EXPR_LOCATION (t
), TREE_CODE (t
), TREE_TYPE (t
),
2305 args
[0], args
[1], args
[2]);
2306 if (val
== NULL_TREE
)
2308 VERIFY_CONSTANT (val
);
2313 var_in_constexpr_fn (tree t
)
2315 tree ctx
= DECL_CONTEXT (t
);
2316 return (cxx_dialect
>= cxx14
&& ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
2317 && DECL_DECLARED_CONSTEXPR_P (ctx
));
2320 /* Attempt to reduce the expression T to a constant value.
2321 On failure, issue diagnostic and return error_mark_node. */
2322 /* FIXME unify with c_fully_fold */
2325 cxx_eval_constant_expression (const constexpr_ctx
*ctx
, tree t
,
2326 bool allow_non_constant
, bool addr
,
2327 bool *non_constant_p
, bool *overflow_p
)
2329 constexpr_ctx new_ctx
;
2332 if (t
== error_mark_node
)
2334 *non_constant_p
= true;
2337 if (CONSTANT_CLASS_P (t
))
2339 if (TREE_CODE (t
) == PTRMEM_CST
)
2340 t
= cplus_expand_constant (t
);
2341 else if (TREE_OVERFLOW (t
) && (!flag_permissive
|| allow_non_constant
))
2345 if (TREE_CODE (t
) != NOP_EXPR
2346 && reduced_constant_expression_p (t
))
2349 switch (TREE_CODE (t
))
2354 /* else fall through. */
2356 r
= integral_constant_value (t
);
2357 if (TREE_CODE (r
) == TARGET_EXPR
2358 && TREE_CODE (TARGET_EXPR_INITIAL (r
)) == CONSTRUCTOR
)
2359 r
= TARGET_EXPR_INITIAL (r
);
2360 if (TREE_CODE (r
) == VAR_DECL
&& var_in_constexpr_fn (r
)
2361 && DECL_INITIAL (r
))
2362 r
= cxx_eval_constant_expression (ctx
, DECL_INITIAL (r
),
2363 allow_non_constant
, false,
2364 non_constant_p
, overflow_p
);
2365 if (TREE_CODE (r
) == VAR_DECL
)
2366 if (tree
*p
= ctx
->values
->get (r
))
2370 if (!allow_non_constant
)
2371 non_const_var_error (r
);
2372 *non_constant_p
= true;
2382 if (ctx
&& ctx
->call
&& DECL_CONTEXT (t
) == ctx
->call
->fundef
->decl
)
2383 r
= lookup_parameter_binding (ctx
->call
, t
);
2385 /* Defer in case this is only used for its type. */;
2388 if (!allow_non_constant
)
2389 error ("%qE is not a constant expression", t
);
2390 *non_constant_p
= true;
2395 case AGGR_INIT_EXPR
:
2396 r
= cxx_eval_call_expression (ctx
, t
, allow_non_constant
, addr
,
2397 non_constant_p
, overflow_p
);
2401 if (!literal_type_p (TREE_TYPE (t
)))
2403 if (!allow_non_constant
)
2405 error ("temporary of non-literal type %qT in a "
2406 "constant expression", TREE_TYPE (t
));
2407 explain_non_literal_class (TREE_TYPE (t
));
2409 *non_constant_p
= true;
2412 if ((AGGREGATE_TYPE_P (TREE_TYPE (t
)) || VECTOR_TYPE_P (TREE_TYPE (t
))))
2414 /* We're being expanded without an explicit target, so start
2415 initializing a new object; expansion with an explicit target
2416 strips the TARGET_EXPR before we get here. */
2418 new_ctx
.ctor
= build_constructor (TREE_TYPE (t
), NULL
);
2419 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx
.ctor
) = true;
2420 new_ctx
.object
= TARGET_EXPR_SLOT (t
);
2421 ctx
->values
->put (new_ctx
.object
, new_ctx
.ctor
);
2424 /* else fall through. */
2426 /* Pass false for 'addr' because these codes indicate
2427 initialization of a temporary. */
2428 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
2429 allow_non_constant
, false,
2430 non_constant_p
, overflow_p
);
2431 if (!*non_constant_p
)
2432 /* Adjust the type of the result to the type of the temporary. */
2433 r
= adjust_temp_type (TREE_TYPE (t
), r
);
2437 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
2438 allow_non_constant
, addr
,
2439 non_constant_p
, overflow_p
);
2443 case NON_LVALUE_EXPR
:
2444 case TRY_CATCH_EXPR
:
2445 case CLEANUP_POINT_EXPR
:
2446 case MUST_NOT_THROW_EXPR
:
2448 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
2449 allow_non_constant
, addr
,
2450 non_constant_p
, overflow_p
);
2453 /* These differ from cxx_eval_unary_expression in that this doesn't
2454 check for a constant operand or result; an address can be
2455 constant without its operand being, and vice versa. */
2457 r
= cxx_eval_indirect_ref (ctx
, t
, allow_non_constant
, addr
,
2458 non_constant_p
, overflow_p
);
2463 tree oldop
= TREE_OPERAND (t
, 0);
2464 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
2467 non_constant_p
, overflow_p
);
2468 /* Don't VERIFY_CONSTANT here. */
2469 if (*non_constant_p
)
2471 /* This function does more aggressive folding than fold itself. */
2472 r
= build_fold_addr_expr_with_type (op
, TREE_TYPE (t
));
2473 if (TREE_CODE (r
) == ADDR_EXPR
&& TREE_OPERAND (r
, 0) == oldop
)
2481 case FIX_TRUNC_EXPR
:
2486 case TRUTH_NOT_EXPR
:
2487 case FIXED_CONVERT_EXPR
:
2488 r
= cxx_eval_unary_expression (ctx
, t
, allow_non_constant
, addr
,
2489 non_constant_p
, overflow_p
);
2493 if (SIZEOF_EXPR_TYPE_P (t
))
2494 r
= cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t
, 0)),
2495 SIZEOF_EXPR
, false);
2496 else if (TYPE_P (TREE_OPERAND (t
, 0)))
2497 r
= cxx_sizeof_or_alignof_type (TREE_OPERAND (t
, 0), SIZEOF_EXPR
,
2500 r
= cxx_sizeof_or_alignof_expr (TREE_OPERAND (t
, 0), SIZEOF_EXPR
,
2502 if (r
== error_mark_node
)
2504 VERIFY_CONSTANT (r
);
2509 /* check_return_expr sometimes wraps a TARGET_EXPR in a
2510 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
2511 introduced by build_call_a. */
2512 tree op0
= TREE_OPERAND (t
, 0);
2513 tree op1
= TREE_OPERAND (t
, 1);
2515 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
2516 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
2517 r
= cxx_eval_constant_expression (ctx
, op0
, allow_non_constant
,
2518 addr
, non_constant_p
, overflow_p
);
2521 /* Check that the LHS is constant and then discard it. */
2522 cxx_eval_constant_expression (ctx
, op0
, allow_non_constant
,
2523 false, non_constant_p
, overflow_p
);
2524 op1
= TREE_OPERAND (t
, 1);
2525 r
= cxx_eval_constant_expression (ctx
, op1
, allow_non_constant
,
2526 addr
, non_constant_p
, overflow_p
);
2531 case POINTER_PLUS_EXPR
:
2535 case TRUNC_DIV_EXPR
:
2537 case FLOOR_DIV_EXPR
:
2538 case ROUND_DIV_EXPR
:
2539 case TRUNC_MOD_EXPR
:
2541 case ROUND_MOD_EXPR
:
2543 case EXACT_DIV_EXPR
:
2553 case TRUTH_XOR_EXPR
:
2560 case UNORDERED_EXPR
:
2570 r
= cxx_eval_binary_expression (ctx
, t
, allow_non_constant
, addr
,
2571 non_constant_p
, overflow_p
);
2574 /* fold can introduce non-IF versions of these; still treat them as
2575 short-circuiting. */
2576 case TRUTH_AND_EXPR
:
2577 case TRUTH_ANDIF_EXPR
:
2578 r
= cxx_eval_logical_expression (ctx
, t
, boolean_false_node
,
2580 allow_non_constant
, addr
,
2581 non_constant_p
, overflow_p
);
2585 case TRUTH_ORIF_EXPR
:
2586 r
= cxx_eval_logical_expression (ctx
, t
, boolean_true_node
,
2588 allow_non_constant
, addr
,
2589 non_constant_p
, overflow_p
);
2593 r
= cxx_eval_array_reference (ctx
, t
, allow_non_constant
, addr
,
2594 non_constant_p
, overflow_p
);
2598 if (is_overloaded_fn (t
))
2600 /* We can only get here in checking mode via
2601 build_non_dependent_expr, because any expression that
2602 calls or takes the address of the function will have
2603 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
2604 gcc_checking_assert (allow_non_constant
|| errorcount
);
2605 *non_constant_p
= true;
2608 r
= cxx_eval_component_reference (ctx
, t
, allow_non_constant
, addr
,
2609 non_constant_p
, overflow_p
);
2613 r
= cxx_eval_bit_field_ref (ctx
, t
, allow_non_constant
, addr
,
2614 non_constant_p
, overflow_p
);
2619 r
= cxx_eval_conditional_expression (ctx
, t
, allow_non_constant
, addr
,
2620 non_constant_p
, overflow_p
);
2624 r
= cxx_eval_bare_aggregate (ctx
, t
, allow_non_constant
, addr
,
2625 non_constant_p
, overflow_p
);
2629 /* We can get this in a defaulted constructor for a class with a
2630 non-static data member of array type. Either the initializer will
2631 be NULL, meaning default-initialization, or it will be an lvalue
2632 or xvalue of the same type, meaning direct-initialization from the
2633 corresponding member. */
2634 r
= cxx_eval_vec_init (ctx
, t
, allow_non_constant
, addr
,
2635 non_constant_p
, overflow_p
);
2640 r
= cxx_eval_trinary_expression (ctx
, t
, allow_non_constant
, addr
,
2641 non_constant_p
, overflow_p
);
2645 case VIEW_CONVERT_EXPR
:
2648 tree oldop
= TREE_OPERAND (t
, 0);
2649 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
2650 allow_non_constant
, addr
,
2651 non_constant_p
, overflow_p
);
2652 if (*non_constant_p
)
2654 if (POINTER_TYPE_P (TREE_TYPE (t
))
2655 && TREE_CODE (op
) == INTEGER_CST
2656 && !integer_zerop (op
))
2658 if (!allow_non_constant
)
2659 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
2660 "reinterpret_cast from integer to pointer");
2661 *non_constant_p
= true;
2665 /* We didn't fold at the top so we could check for ptr-int
2668 r
= fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), op
);
2669 /* Conversion of an out-of-range value has implementation-defined
2670 behavior; the language considers it different from arithmetic
2671 overflow, which is undefined. */
2672 if (TREE_OVERFLOW_P (r
) && !TREE_OVERFLOW_P (op
))
2673 TREE_OVERFLOW (r
) = false;
2677 case EMPTY_CLASS_EXPR
:
2678 /* This is good enough for a function argument that might not get
2679 used, and they can't do anything with it, so just return it. */
2683 case PREINCREMENT_EXPR
:
2684 case POSTINCREMENT_EXPR
:
2685 case PREDECREMENT_EXPR
:
2686 case POSTDECREMENT_EXPR
:
2690 case VEC_DELETE_EXPR
:
2694 /* GCC internal stuff. */
2697 case WITH_CLEANUP_EXPR
:
2698 case STATEMENT_LIST
:
2700 case NON_DEPENDENT_EXPR
:
2704 if (!allow_non_constant
)
2705 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
2706 "expression %qE is not a constant-expression", t
);
2707 *non_constant_p
= true;
2710 case PLACEHOLDER_EXPR
:
2711 if (!ctx
|| !ctx
->ctor
|| (addr
&& !ctx
->object
))
2713 /* A placeholder without a referent. We can get here when
2714 checking whether NSDMIs are noexcept, or in massage_init_elt;
2715 just say it's non-constant for now. */
2716 gcc_assert (allow_non_constant
);
2717 *non_constant_p
= true;
2722 /* Use of the value or address of the current object. We could
2723 use ctx->object unconditionally, but using ctx->ctor when we
2724 can is a minor optimization. */
2725 tree ctor
= addr
? ctx
->object
: ctx
->ctor
;
2726 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2727 (TREE_TYPE (t
), TREE_TYPE (ctor
)));
2728 return cxx_eval_constant_expression
2729 (ctx
, ctor
, allow_non_constant
, addr
, non_constant_p
, overflow_p
);
2734 internal_error ("unexpected expression %qE of kind %s", t
,
2735 get_tree_code_name (TREE_CODE (t
)));
2736 *non_constant_p
= true;
2740 if (r
== error_mark_node
)
2741 *non_constant_p
= true;
2743 if (*non_constant_p
)
2750 cxx_eval_outermost_constant_expr (tree t
, bool allow_non_constant
,
2751 tree object
= NULL_TREE
)
2753 bool non_constant_p
= false;
2754 bool overflow_p
= false;
2755 constexpr_ctx ctx
= { NULL
, NULL
, NULL
, NULL
};
2756 hash_map
<tree
,tree
> map
;
2758 tree type
= initialized_type (t
);
2760 if (AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
))
2762 /* In C++14 an NSDMI can participate in aggregate initialization,
2763 and can refer to the address of the object being initialized, so
2764 we need to pass in the relevant VAR_DECL if we want to do the
2765 evaluation in a single pass. The evaluation will dynamically
2766 update ctx.values for the VAR_DECL. We use the same strategy
2767 for C++11 constexpr constructors that refer to the object being
2769 ctx
.ctor
= build_constructor (type
, NULL
);
2770 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctx
.ctor
) = true;
2771 if (!object
&& TREE_CODE (t
) == TARGET_EXPR
)
2772 object
= TARGET_EXPR_SLOT (t
);
2773 ctx
.object
= object
;
2775 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2776 (type
, TREE_TYPE (object
)));
2777 if (object
&& DECL_P (object
))
2778 map
.put (object
, ctx
.ctor
);
2779 if (TREE_CODE (r
) == TARGET_EXPR
)
2780 /* Avoid creating another CONSTRUCTOR when we expand the
2782 r
= TARGET_EXPR_INITIAL (r
);
2785 r
= cxx_eval_constant_expression (&ctx
, r
, allow_non_constant
,
2786 false, &non_constant_p
, &overflow_p
);
2788 verify_constant (r
, allow_non_constant
, &non_constant_p
, &overflow_p
);
2790 if (TREE_CODE (t
) != CONSTRUCTOR
2791 && cp_has_mutable_p (TREE_TYPE (t
)))
2793 /* We allow a mutable type if the original expression was a
2794 CONSTRUCTOR so that we can do aggregate initialization of
2795 constexpr variables. */
2796 if (!allow_non_constant
)
2797 error ("%qT cannot be the type of a complete constant expression "
2798 "because it has mutable sub-objects", type
);
2799 non_constant_p
= true;
2802 /* Technically we should check this for all subexpressions, but that
2803 runs into problems with our internal representation of pointer
2804 subtraction and the 5.19 rules are still in flux. */
2805 if (CONVERT_EXPR_CODE_P (TREE_CODE (r
))
2806 && ARITHMETIC_TYPE_P (TREE_TYPE (r
))
2807 && TREE_CODE (TREE_OPERAND (r
, 0)) == ADDR_EXPR
)
2809 if (!allow_non_constant
)
2810 error ("conversion from pointer type %qT "
2811 "to arithmetic type %qT in a constant-expression",
2812 TREE_TYPE (TREE_OPERAND (r
, 0)), TREE_TYPE (r
));
2813 non_constant_p
= true;
2816 if (!non_constant_p
&& overflow_p
)
2817 non_constant_p
= true;
2819 if (non_constant_p
&& !allow_non_constant
)
2820 return error_mark_node
;
2821 else if (non_constant_p
&& TREE_CONSTANT (r
))
2823 /* This isn't actually constant, so unset TREE_CONSTANT. */
2826 else if (TREE_CODE (r
) == CONSTRUCTOR
)
2827 r
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (r
), r
);
2829 r
= build_nop (TREE_TYPE (r
), r
);
2830 TREE_CONSTANT (r
) = false;
2832 else if (non_constant_p
|| r
== t
)
2835 if (TREE_CODE (r
) == CONSTRUCTOR
&& CLASS_TYPE_P (TREE_TYPE (r
)))
2837 if (TREE_CODE (t
) == TARGET_EXPR
2838 && TARGET_EXPR_INITIAL (t
) == r
)
2842 r
= get_target_expr (r
);
2843 TREE_CONSTANT (r
) = true;
2851 /* Returns true if T is a valid subexpression of a constant expression,
2852 even if it isn't itself a constant expression. */
2855 is_sub_constant_expr (tree t
)
2857 bool non_constant_p
= false;
2858 bool overflow_p
= false;
2859 constexpr_ctx ctx
= { NULL
, NULL
, NULL
, NULL
};
2860 hash_map
<tree
, tree
> map
;
2862 tree type
= initialized_type (t
);
2863 if ((AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
))
2864 && TREE_CODE (t
) != TARGET_EXPR
)
2866 ctx
.ctor
= build_constructor (type
, NULL
);
2867 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctx
.ctor
) = true;
2869 cxx_eval_constant_expression (&ctx
, t
, true, false, &non_constant_p
,
2871 return !non_constant_p
&& !overflow_p
;
2874 /* If T represents a constant expression returns its reduced value.
2875 Otherwise return error_mark_node. If T is dependent, then
2879 cxx_constant_value (tree t
, tree decl
)
2881 return cxx_eval_outermost_constant_expr (t
, false, decl
);
2884 /* If T is a constant expression, returns its reduced value.
2885 Otherwise, if T does not have TREE_CONSTANT set, returns T.
2886 Otherwise, returns a version of T without TREE_CONSTANT. */
2889 maybe_constant_value (tree t
, tree decl
)
2893 if (instantiation_dependent_expression_p (t
)
2894 || type_unknown_p (t
)
2895 || BRACE_ENCLOSED_INITIALIZER_P (t
)
2896 || !potential_constant_expression (t
))
2898 if (TREE_OVERFLOW_P (t
))
2900 t
= build_nop (TREE_TYPE (t
), t
);
2901 TREE_CONSTANT (t
) = false;
2906 r
= cxx_eval_outermost_constant_expr (t
, true, decl
);
2907 #ifdef ENABLE_CHECKING
2908 /* cp_tree_equal looks through NOPs, so allow them. */
2910 || CONVERT_EXPR_P (t
)
2911 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
2912 || !cp_tree_equal (r
, t
));
2917 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
2918 than wrapped in a TARGET_EXPR. */
2921 maybe_constant_init (tree t
, tree decl
)
2923 if (TREE_CODE (t
) == EXPR_STMT
)
2924 t
= TREE_OPERAND (t
, 0);
2925 if (TREE_CODE (t
) == CONVERT_EXPR
2926 && VOID_TYPE_P (TREE_TYPE (t
)))
2927 t
= TREE_OPERAND (t
, 0);
2928 t
= maybe_constant_value (t
, decl
);
2929 if (TREE_CODE (t
) == TARGET_EXPR
)
2931 tree init
= TARGET_EXPR_INITIAL (t
);
2932 if (TREE_CODE (init
) == CONSTRUCTOR
)
2939 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
2940 /* Return true if the object referred to by REF has automatic or thread
2943 enum { ck_ok
, ck_bad
, ck_unknown
};
2945 check_automatic_or_tls (tree ref
)
2948 HOST_WIDE_INT bitsize
, bitpos
;
2950 int volatilep
= 0, unsignedp
= 0;
2951 tree decl
= get_inner_reference (ref
, &bitsize
, &bitpos
, &offset
,
2952 &mode
, &unsignedp
, &volatilep
, false);
2955 /* If there isn't a decl in the middle, we don't know the linkage here,
2956 and this isn't a constant expression anyway. */
2959 dk
= decl_storage_duration (decl
);
2960 return (dk
== dk_auto
|| dk
== dk_thread
) ? ck_bad
: ck_ok
;
2964 /* Return true if T denotes a potentially constant expression. Issue
2965 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
2966 an lvalue-rvalue conversion is implied.
2968 C++0x [expr.const] used to say
2970 6 An expression is a potential constant expression if it is
2971 a constant expression where all occurrences of function
2972 parameters are replaced by arbitrary constant expressions
2973 of the appropriate type.
2975 2 A conditional expression is a constant expression unless it
2976 involves one of the following as a potentially evaluated
2977 subexpression (3.2), but subexpressions of logical AND (5.14),
2978 logical OR (5.15), and conditional (5.16) operations that are
2979 not evaluated are not considered. */
2982 potential_constant_expression_1 (tree t
, bool want_rval
, tsubst_flags_t flags
)
2984 enum { any
= false, rval
= true };
2988 if (t
== error_mark_node
)
2992 if (TREE_THIS_VOLATILE (t
))
2994 if (flags
& tf_error
)
2995 error ("expression %qE has side-effects", t
);
2998 if (CONSTANT_CLASS_P (t
))
3001 switch (TREE_CODE (t
))
3007 case TEMPLATE_ID_EXPR
:
3015 case TEMPLATE_PARM_INDEX
:
3017 case IDENTIFIER_NODE
:
3018 case USERDEF_LITERAL
:
3019 /* We can see a FIELD_DECL in a pointer-to-member expression. */
3023 case PLACEHOLDER_EXPR
:
3026 case AGGR_INIT_EXPR
:
3028 /* -- an invocation of a function other than a constexpr function
3029 or a constexpr constructor. */
3031 tree fun
= get_function_named_in_call (t
);
3032 const int nargs
= call_expr_nargs (t
);
3035 if (is_overloaded_fn (fun
))
3037 if (TREE_CODE (fun
) == FUNCTION_DECL
)
3039 if (builtin_valid_in_constant_expr_p (fun
))
3041 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
3042 /* Allow any built-in function; if the expansion
3043 isn't constant, we'll deal with that then. */
3044 && !is_builtin_fn (fun
))
3046 if (flags
& tf_error
)
3048 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3049 "call to non-constexpr function %qD", fun
);
3050 explain_invalid_constexpr_fn (fun
);
3054 /* A call to a non-static member function takes the address
3055 of the object as the first argument. But in a constant
3056 expression the address will be folded away, so look
3058 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
3059 && !DECL_CONSTRUCTOR_P (fun
))
3061 tree x
= get_nth_callarg (t
, 0);
3062 if (is_this_parameter (x
))
3064 else if (!potential_constant_expression_1 (x
, rval
, flags
))
3071 if (!potential_constant_expression_1 (fun
, true, flags
))
3073 fun
= get_first_fn (fun
);
3075 /* Skip initial arguments to base constructors. */
3076 if (DECL_BASE_CONSTRUCTOR_P (fun
))
3077 i
= num_artificial_parms_for (fun
);
3078 fun
= DECL_ORIGIN (fun
);
3082 if (potential_constant_expression_1 (fun
, rval
, flags
))
3083 /* Might end up being a constant function pointer. */;
3087 for (; i
< nargs
; ++i
)
3089 tree x
= get_nth_callarg (t
, i
);
3090 if (!potential_constant_expression_1 (x
, rval
, flags
))
3096 case NON_LVALUE_EXPR
:
3097 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
3098 -- an lvalue of integral type that refers to a non-volatile
3099 const variable or static data member initialized with
3100 constant expressions, or
3102 -- an lvalue of literal type that refers to non-volatile
3103 object defined with constexpr, or that refers to a
3104 sub-object of such an object; */
3105 return potential_constant_expression_1 (TREE_OPERAND (t
, 0), rval
, flags
);
3108 if (want_rval
&& !decl_constant_var_p (t
)
3109 && !var_in_constexpr_fn (t
)
3110 && !dependent_type_p (TREE_TYPE (t
)))
3112 if (flags
& tf_error
)
3113 non_const_var_error (t
);
3120 case VIEW_CONVERT_EXPR
:
3121 /* -- a reinterpret_cast. FIXME not implemented, and this rule
3122 may change to something more specific to type-punning (DR 1312). */
3124 tree from
= TREE_OPERAND (t
, 0);
3125 if (POINTER_TYPE_P (TREE_TYPE (t
))
3126 && TREE_CODE (from
) == INTEGER_CST
3127 && !integer_zerop (from
))
3129 if (flags
& tf_error
)
3130 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3131 "reinterpret_cast from integer to pointer");
3134 return (potential_constant_expression_1
3135 (from
, TREE_CODE (t
) != VIEW_CONVERT_EXPR
, flags
));
3139 /* -- a unary operator & that is applied to an lvalue that
3140 designates an object with thread or automatic storage
3142 t
= TREE_OPERAND (t
, 0);
3144 if (TREE_CODE (t
) == OFFSET_REF
&& PTRMEM_OK_P (t
))
3145 /* A pointer-to-member constant. */
3149 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
3150 any checking here, as we might dereference the pointer later. If
3151 we remove this code, also remove check_automatic_or_tls. */
3152 i
= check_automatic_or_tls (t
);
3157 if (flags
& tf_error
)
3158 error ("address-of an object %qE with thread local or "
3159 "automatic storage is not a constant expression", t
);
3163 return potential_constant_expression_1 (t
, any
, flags
);
3169 /* -- a class member access unless its postfix-expression is
3170 of literal type or of pointer to literal type. */
3171 /* This test would be redundant, as it follows from the
3172 postfix-expression being a potential constant expression. */
3173 return potential_constant_expression_1 (TREE_OPERAND (t
, 0),
3176 case EXPR_PACK_EXPANSION
:
3177 return potential_constant_expression_1 (PACK_EXPANSION_PATTERN (t
),
3182 tree x
= TREE_OPERAND (t
, 0);
3184 if (is_this_parameter (x
))
3186 if (DECL_CONTEXT (x
)
3187 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x
)))
3189 if (flags
& tf_error
)
3190 error ("use of %<this%> in a constant expression");
3195 return potential_constant_expression_1 (x
, rval
, flags
);
3199 case DYNAMIC_CAST_EXPR
:
3200 case PSEUDO_DTOR_EXPR
:
3201 case PREINCREMENT_EXPR
:
3202 case POSTINCREMENT_EXPR
:
3203 case PREDECREMENT_EXPR
:
3204 case POSTDECREMENT_EXPR
:
3208 case VEC_DELETE_EXPR
:
3213 case OMP_ATOMIC_READ
:
3214 case OMP_ATOMIC_CAPTURE_OLD
:
3215 case OMP_ATOMIC_CAPTURE_NEW
:
3216 /* GCC internal stuff. */
3219 case WITH_CLEANUP_EXPR
:
3220 case CLEANUP_POINT_EXPR
:
3221 case MUST_NOT_THROW_EXPR
:
3222 case TRY_CATCH_EXPR
:
3223 case STATEMENT_LIST
:
3224 /* Don't bother trying to define a subset of statement-expressions to
3225 be constant-expressions, at least for now. */
3229 case TRANSACTION_EXPR
:
3235 if (flags
& tf_error
)
3236 error ("expression %qE is not a constant-expression", t
);
3240 /* -- a typeid expression whose operand is of polymorphic
3243 tree e
= TREE_OPERAND (t
, 0);
3244 if (!TYPE_P (e
) && !type_dependent_expression_p (e
)
3245 && TYPE_POLYMORPHIC_P (TREE_TYPE (e
)))
3247 if (flags
& tf_error
)
3248 error ("typeid-expression is not a constant expression "
3249 "because %qE is of polymorphic type", e
);
3256 /* -- a subtraction where both operands are pointers. */
3257 if (TYPE_PTR_P (TREE_OPERAND (t
, 0))
3258 && TYPE_PTR_P (TREE_OPERAND (t
, 1)))
3260 if (flags
& tf_error
)
3261 error ("difference of two pointer expressions is not "
3262 "a constant expression");
3274 /* -- a relational or equality operator where at least
3275 one of the operands is a pointer. */
3276 if (TYPE_PTR_P (TREE_OPERAND (t
, 0))
3277 || TYPE_PTR_P (TREE_OPERAND (t
, 1)))
3279 if (flags
& tf_error
)
3280 error ("pointer comparison expression is not a "
3281 "constant expression");
3289 if (TYPE_P (TREE_OPERAND (t
, 0)))
3291 /* else fall through. */
3297 case FIX_TRUNC_EXPR
:
3301 case TRUTH_NOT_EXPR
:
3302 case FIXED_CONVERT_EXPR
:
3303 case UNARY_PLUS_EXPR
:
3304 return potential_constant_expression_1 (TREE_OPERAND (t
, 0), rval
,
3308 case CONST_CAST_EXPR
:
3309 case STATIC_CAST_EXPR
:
3310 case REINTERPRET_CAST_EXPR
:
3311 case IMPLICIT_CONV_EXPR
:
3312 if (cxx_dialect
< cxx11
3313 && !dependent_type_p (TREE_TYPE (t
))
3314 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
)))
3315 /* In C++98, a conversion to non-integral type can't be part of a
3316 constant expression. */
3318 if (flags
& tf_error
)
3319 error ("cast to non-integral type %qT in a constant expression",
3324 return (potential_constant_expression_1
3325 (TREE_OPERAND (t
, 0),
3326 TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
, flags
));
3329 case NON_DEPENDENT_EXPR
:
3330 /* For convenience. */
3332 return potential_constant_expression_1 (TREE_OPERAND (t
, 0),
3336 return potential_constant_expression_1 (TREE_OPERAND (t
, 1),
3340 if (!literal_type_p (TREE_TYPE (t
)))
3342 if (flags
& tf_error
)
3344 error ("temporary of non-literal type %qT in a "
3345 "constant expression", TREE_TYPE (t
));
3346 explain_non_literal_class (TREE_TYPE (t
));
3351 return potential_constant_expression_1 (TREE_OPERAND (t
, 1),
3356 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
3357 constructor_elt
*ce
;
3358 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
3359 if (!potential_constant_expression_1 (ce
->value
, want_rval
, flags
))
3366 gcc_assert (TREE_PURPOSE (t
) == NULL_TREE
3367 || DECL_P (TREE_PURPOSE (t
)));
3368 if (!potential_constant_expression_1 (TREE_VALUE (t
), want_rval
,
3371 if (TREE_CHAIN (t
) == NULL_TREE
)
3373 return potential_constant_expression_1 (TREE_CHAIN (t
), want_rval
,
3377 case TRUNC_DIV_EXPR
:
3379 case FLOOR_DIV_EXPR
:
3380 case ROUND_DIV_EXPR
:
3381 case TRUNC_MOD_EXPR
:
3383 case ROUND_MOD_EXPR
:
3385 tree denom
= TREE_OPERAND (t
, 1);
3386 if (!potential_constant_expression_1 (denom
, rval
, flags
))
3388 /* We can't call cxx_eval_outermost_constant_expr on an expression
3389 that hasn't been through fold_non_dependent_expr yet. */
3390 if (!processing_template_decl
)
3391 denom
= cxx_eval_outermost_constant_expr (denom
, true);
3392 if (integer_zerop (denom
))
3394 if (flags
& tf_error
)
3395 error ("division by zero is not a constant-expression");
3401 return potential_constant_expression_1 (TREE_OPERAND (t
, 0),
3408 /* check_return_expr sometimes wraps a TARGET_EXPR in a
3409 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
3410 introduced by build_call_a. */
3411 tree op0
= TREE_OPERAND (t
, 0);
3412 tree op1
= TREE_OPERAND (t
, 1);
3414 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
3415 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
3416 return potential_constant_expression_1 (op0
, want_rval
, flags
);
3421 /* If the first operand is the non-short-circuit constant, look at
3422 the second operand; otherwise we only care about the first one for
3424 case TRUTH_AND_EXPR
:
3425 case TRUTH_ANDIF_EXPR
:
3426 tmp
= boolean_true_node
;
3429 case TRUTH_ORIF_EXPR
:
3430 tmp
= boolean_false_node
;
3433 tree op
= TREE_OPERAND (t
, 0);
3434 if (!potential_constant_expression_1 (op
, rval
, flags
))
3436 if (!processing_template_decl
)
3437 op
= cxx_eval_outermost_constant_expr (op
, true);
3438 if (tree_int_cst_equal (op
, tmp
))
3439 return potential_constant_expression_1 (TREE_OPERAND (t
, 1), rval
, flags
);
3446 case POINTER_PLUS_EXPR
:
3448 case EXACT_DIV_EXPR
:
3458 case TRUTH_XOR_EXPR
:
3459 case UNORDERED_EXPR
:
3472 case ARRAY_RANGE_REF
:
3476 for (i
= 0; i
< 2; ++i
)
3477 if (!potential_constant_expression_1 (TREE_OPERAND (t
, i
),
3482 case CILK_SYNC_STMT
:
3483 case CILK_SPAWN_STMT
:
3484 case ARRAY_NOTATION_REF
:
3489 for (i
= 0; i
< 3; ++i
)
3490 if (!potential_constant_expression_1 (TREE_OPERAND (t
, i
),
3497 /* If the condition is a known constant, we know which of the legs we
3498 care about; otherwise we only require that the condition and
3499 either of the legs be potentially constant. */
3500 tmp
= TREE_OPERAND (t
, 0);
3501 if (!potential_constant_expression_1 (tmp
, rval
, flags
))
3503 if (!processing_template_decl
)
3504 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
3505 if (integer_zerop (tmp
))
3506 return potential_constant_expression_1 (TREE_OPERAND (t
, 2),
3508 else if (TREE_CODE (tmp
) == INTEGER_CST
)
3509 return potential_constant_expression_1 (TREE_OPERAND (t
, 1),
3511 for (i
= 1; i
< 3; ++i
)
3512 if (potential_constant_expression_1 (TREE_OPERAND (t
, i
),
3513 want_rval
, tf_none
))
3515 if (flags
& tf_error
)
3516 error ("expression %qE is not a constant-expression", t
);
3520 if (VEC_INIT_EXPR_IS_CONSTEXPR (t
))
3522 if (flags
& tf_error
)
3524 error ("non-constant array initialization");
3525 diagnose_non_constexpr_vec_init (t
);
3530 if (objc_is_property_ref (t
))
3533 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t
)));
3539 /* The main entry point to the above. */
3542 potential_constant_expression (tree t
)
3544 return potential_constant_expression_1 (t
, false, tf_none
);
3547 /* As above, but require a constant rvalue. */
3550 potential_rvalue_constant_expression (tree t
)
3552 return potential_constant_expression_1 (t
, true, tf_none
);
3555 /* Like above, but complain about non-constant expressions. */
3558 require_potential_constant_expression (tree t
)
3560 return potential_constant_expression_1 (t
, false, tf_warning_or_error
);
3563 /* Cross product of the above. */
3566 require_potential_rvalue_constant_expression (tree t
)
3568 return potential_constant_expression_1 (t
, true, tf_warning_or_error
);
3571 #include "gt-cp-constexpr.h"