1 /* Perform -*- C++ -*- constant expression evaluation, including calls to
2 constexpr functions. These routines are used both during actual parsing
3 and during the instantiation of template functions.
5 Copyright (C) 1998-2014 Free Software Foundation, Inc.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 GCC is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
25 #include "coretypes.h"
29 #include "c-family/c-objc.h"
30 #include "tree-iterator.h"
33 #include "tree-inline.h"
36 static bool verify_constant (tree
, bool, bool *, bool *);
37 #define VERIFY_CONSTANT(X) \
39 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
43 /* Returns true iff FUN is an instantiation of a constexpr function
44 template or a defaulted constexpr function. */
47 is_instantiation_of_constexpr (tree fun
)
49 return ((DECL_TEMPLOID_INSTANTIATION (fun
)
50 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun
)))
51 || (DECL_DEFAULTED_FN (fun
)
52 && DECL_DECLARED_CONSTEXPR_P (fun
)));
55 /* Return true if T is a literal type. */
58 literal_type_p (tree t
)
61 || TREE_CODE (t
) == VECTOR_TYPE
62 || TREE_CODE (t
) == REFERENCE_TYPE
63 || (VOID_TYPE_P (t
) && cxx_dialect
>= cxx14
))
67 t
= complete_type (t
);
68 gcc_assert (COMPLETE_TYPE_P (t
) || errorcount
);
69 return CLASSTYPE_LITERAL_P (t
);
71 if (TREE_CODE (t
) == ARRAY_TYPE
)
72 return literal_type_p (strip_array_types (t
));
76 /* If DECL is a variable declared `constexpr', require its type
77 be literal. Return the DECL if OK, otherwise NULL. */
80 ensure_literal_type_for_constexpr_object (tree decl
)
82 tree type
= TREE_TYPE (decl
);
84 && (DECL_DECLARED_CONSTEXPR_P (decl
)
85 || var_in_constexpr_fn (decl
))
86 && !processing_template_decl
)
88 tree stype
= strip_array_types (type
);
89 if (CLASS_TYPE_P (stype
) && !COMPLETE_TYPE_P (complete_type (stype
)))
90 /* Don't complain here, we'll complain about incompleteness
91 when we try to initialize the variable. */;
92 else if (!literal_type_p (type
))
94 if (DECL_DECLARED_CONSTEXPR_P (decl
))
95 error ("the type %qT of constexpr variable %qD is not literal",
99 error ("variable %qD of non-literal type %qT in %<constexpr%> "
100 "function", decl
, type
);
101 cp_function_chain
->invalid_constexpr
= true;
103 explain_non_literal_class (type
);
110 /* Representation of entries in the constexpr function definition table. */
112 struct GTY((for_user
)) constexpr_fundef
{
117 struct constexpr_fundef_hasher
: ggc_hasher
<constexpr_fundef
*>
119 static hashval_t
hash (constexpr_fundef
*);
120 static bool equal (constexpr_fundef
*, constexpr_fundef
*);
123 /* This table holds all constexpr function definitions seen in
124 the current translation unit. */
126 static GTY (()) hash_table
<constexpr_fundef_hasher
> *constexpr_fundef_table
;
128 /* Utility function used for managing the constexpr function table.
129 Return true if the entries pointed to by P and Q are for the
130 same constexpr function. */
133 constexpr_fundef_hasher::equal (constexpr_fundef
*lhs
, constexpr_fundef
*rhs
)
135 return lhs
->decl
== rhs
->decl
;
138 /* Utility function used for managing the constexpr function table.
139 Return a hash value for the entry pointed to by Q. */
142 constexpr_fundef_hasher::hash (constexpr_fundef
*fundef
)
144 return DECL_UID (fundef
->decl
);
147 /* Return a previously saved definition of function FUN. */
149 static constexpr_fundef
*
150 retrieve_constexpr_fundef (tree fun
)
152 constexpr_fundef fundef
= { NULL
, NULL
};
153 if (constexpr_fundef_table
== NULL
)
157 return constexpr_fundef_table
->find (&fundef
);
160 /* Check whether the parameter and return types of FUN are valid for a
161 constexpr function, and complain if COMPLAIN. */
164 is_valid_constexpr_fn (tree fun
, bool complain
)
168 if (DECL_INHERITED_CTOR_BASE (fun
)
169 && TREE_CODE (fun
) == TEMPLATE_DECL
)
173 error ("inherited constructor %qD is not constexpr",
174 get_inherited_ctor (fun
));
178 for (tree parm
= FUNCTION_FIRST_USER_PARM (fun
);
179 parm
!= NULL_TREE
; parm
= TREE_CHAIN (parm
))
180 if (!literal_type_p (TREE_TYPE (parm
)))
185 error ("invalid type for parameter %d of constexpr "
186 "function %q+#D", DECL_PARM_INDEX (parm
), fun
);
187 explain_non_literal_class (TREE_TYPE (parm
));
192 if (!DECL_CONSTRUCTOR_P (fun
))
194 tree rettype
= TREE_TYPE (TREE_TYPE (fun
));
195 if (!literal_type_p (rettype
))
200 error ("invalid return type %qT of constexpr function %q+D",
202 explain_non_literal_class (rettype
);
206 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
207 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun
)))
212 error ("enclosing class of constexpr non-static member "
213 "function %q+#D is not a literal type", fun
);
214 explain_non_literal_class (DECL_CONTEXT (fun
));
218 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun
)))
222 error ("%q#T has virtual base classes", DECL_CONTEXT (fun
));
228 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
229 for a member of an anonymous aggregate, INIT is the initializer for that
230 member, and VEC_OUTER is the vector of constructor elements for the class
231 whose constructor we are processing. Add the initializer to the vector
232 and return true to indicate success. */
235 build_anon_member_initialization (tree member
, tree init
,
236 vec
<constructor_elt
, va_gc
> **vec_outer
)
238 /* MEMBER presents the relevant fields from the inside out, but we need
239 to build up the initializer from the outside in so that we can reuse
240 previously built CONSTRUCTORs if this is, say, the second field in an
241 anonymous struct. So we use a vec as a stack. */
242 auto_vec
<tree
, 2> fields
;
245 fields
.safe_push (TREE_OPERAND (member
, 1));
246 member
= TREE_OPERAND (member
, 0);
248 while (ANON_AGGR_TYPE_P (TREE_TYPE (member
))
249 && TREE_CODE (member
) == COMPONENT_REF
);
251 /* VEC has the constructor elements vector for the context of FIELD.
252 If FIELD is an anonymous aggregate, we will push inside it. */
253 vec
<constructor_elt
, va_gc
> **vec
= vec_outer
;
255 while (field
= fields
.pop(),
256 ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
259 /* If there is already an outer constructor entry for the anonymous
260 aggregate FIELD, use it; otherwise, insert one. */
261 if (vec_safe_is_empty (*vec
)
262 || (*vec
)->last().index
!= field
)
264 ctor
= build_constructor (TREE_TYPE (field
), NULL
);
265 CONSTRUCTOR_APPEND_ELT (*vec
, field
, ctor
);
268 ctor
= (*vec
)->last().value
;
269 vec
= &CONSTRUCTOR_ELTS (ctor
);
272 /* Now we're at the innermost field, the one that isn't an anonymous
273 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
274 gcc_assert (fields
.is_empty());
275 CONSTRUCTOR_APPEND_ELT (*vec
, field
, init
);
280 /* Subroutine of build_constexpr_constructor_member_initializers.
281 The expression tree T represents a data member initialization
282 in a (constexpr) constructor definition. Build a pairing of
283 the data member with its initializer, and prepend that pair
284 to the existing initialization pair INITS. */
287 build_data_member_initialization (tree t
, vec
<constructor_elt
, va_gc
> **vec
)
290 if (TREE_CODE (t
) == CLEANUP_POINT_EXPR
)
291 t
= TREE_OPERAND (t
, 0);
292 if (TREE_CODE (t
) == EXPR_STMT
)
293 t
= TREE_OPERAND (t
, 0);
294 if (t
== error_mark_node
)
296 if (TREE_CODE (t
) == STATEMENT_LIST
)
298 tree_stmt_iterator i
;
299 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
301 if (! build_data_member_initialization (tsi_stmt (i
), vec
))
306 if (TREE_CODE (t
) == CLEANUP_STMT
)
308 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
309 but we can in a constexpr constructor for a non-literal class. Just
310 ignore it; either all the initialization will be constant, in which
311 case the cleanup can't run, or it can't be constexpr.
312 Still recurse into CLEANUP_BODY. */
313 return build_data_member_initialization (CLEANUP_BODY (t
), vec
);
315 if (TREE_CODE (t
) == CONVERT_EXPR
)
316 t
= TREE_OPERAND (t
, 0);
317 if (TREE_CODE (t
) == INIT_EXPR
318 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
319 use what this function builds for cx_check_missing_mem_inits, and
320 assignment in the ctor body doesn't count. */
321 || (cxx_dialect
< cxx14
&& TREE_CODE (t
) == MODIFY_EXPR
))
323 member
= TREE_OPERAND (t
, 0);
324 init
= break_out_target_exprs (TREE_OPERAND (t
, 1));
326 else if (TREE_CODE (t
) == CALL_EXPR
)
328 tree fn
= get_callee_fndecl (t
);
329 if (!fn
|| !DECL_CONSTRUCTOR_P (fn
))
330 /* We're only interested in calls to subobject constructors. */
332 member
= CALL_EXPR_ARG (t
, 0);
333 /* We don't use build_cplus_new here because it complains about
334 abstract bases. Leaving the call unwrapped means that it has the
335 wrong type, but cxx_eval_constant_expression doesn't care. */
336 init
= break_out_target_exprs (t
);
338 else if (TREE_CODE (t
) == BIND_EXPR
)
339 return build_data_member_initialization (BIND_EXPR_BODY (t
), vec
);
341 /* Don't add anything else to the CONSTRUCTOR. */
343 if (INDIRECT_REF_P (member
))
344 member
= TREE_OPERAND (member
, 0);
345 if (TREE_CODE (member
) == NOP_EXPR
)
349 if (TREE_CODE (op
) == ADDR_EXPR
)
351 gcc_assert (same_type_ignoring_top_level_qualifiers_p
352 (TREE_TYPE (TREE_TYPE (op
)),
353 TREE_TYPE (TREE_TYPE (member
))));
354 /* Initializing a cv-qualified member; we need to look through
358 else if (op
== current_class_ptr
359 && (same_type_ignoring_top_level_qualifiers_p
360 (TREE_TYPE (TREE_TYPE (member
)),
361 current_class_type
)))
362 /* Delegating constructor. */
366 /* This is an initializer for an empty base; keep it for now so
367 we can check it in cxx_eval_bare_aggregate. */
368 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member
))));
371 if (TREE_CODE (member
) == ADDR_EXPR
)
372 member
= TREE_OPERAND (member
, 0);
373 if (TREE_CODE (member
) == COMPONENT_REF
)
375 tree aggr
= TREE_OPERAND (member
, 0);
376 if (TREE_CODE (aggr
) != COMPONENT_REF
)
377 /* Normal member initialization. */
378 member
= TREE_OPERAND (member
, 1);
379 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr
)))
380 /* Initializing a member of an anonymous union. */
381 return build_anon_member_initialization (member
, init
, vec
);
383 /* We're initializing a vtable pointer in a base. Leave it as
384 COMPONENT_REF so we remember the path to get to the vfield. */
385 gcc_assert (TREE_TYPE (member
) == vtbl_ptr_type_node
);
388 CONSTRUCTOR_APPEND_ELT (*vec
, member
, init
);
392 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
393 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
394 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
397 check_constexpr_bind_expr_vars (tree t
)
399 gcc_assert (TREE_CODE (t
) == BIND_EXPR
);
401 for (tree var
= BIND_EXPR_VARS (t
); var
; var
= DECL_CHAIN (var
))
402 if (TREE_CODE (var
) == TYPE_DECL
403 && DECL_IMPLICIT_TYPEDEF_P (var
))
408 /* Subroutine of check_constexpr_ctor_body. */
411 check_constexpr_ctor_body_1 (tree last
, tree list
)
413 switch (TREE_CODE (list
))
416 if (TREE_CODE (DECL_EXPR_DECL (list
)) == USING_DECL
)
420 case CLEANUP_POINT_EXPR
:
421 return check_constexpr_ctor_body (last
, TREE_OPERAND (list
, 0),
425 if (!check_constexpr_bind_expr_vars (list
)
426 || !check_constexpr_ctor_body (last
, BIND_EXPR_BODY (list
),
440 /* Make sure that there are no statements after LAST in the constructor
441 body represented by LIST. */
444 check_constexpr_ctor_body (tree last
, tree list
, bool complain
)
446 /* C++14 doesn't require a constexpr ctor to have an empty body. */
447 if (cxx_dialect
>= cxx14
)
451 if (TREE_CODE (list
) == STATEMENT_LIST
)
453 tree_stmt_iterator i
= tsi_last (list
);
454 for (; !tsi_end_p (i
); tsi_prev (&i
))
456 tree t
= tsi_stmt (i
);
459 if (!check_constexpr_ctor_body_1 (last
, t
))
466 else if (list
!= last
467 && !check_constexpr_ctor_body_1 (last
, list
))
472 error ("constexpr constructor does not have empty body");
473 DECL_DECLARED_CONSTEXPR_P (current_function_decl
) = false;
478 /* V is a vector of constructor elements built up for the base and member
479 initializers of a constructor for TYPE. They need to be in increasing
480 offset order, which they might not be yet if TYPE has a primary base
481 which is not first in the base-clause or a vptr and at least one base
482 all of which are non-primary. */
484 static vec
<constructor_elt
, va_gc
> *
485 sort_constexpr_mem_initializers (tree type
, vec
<constructor_elt
, va_gc
> *v
)
487 tree pri
= CLASSTYPE_PRIMARY_BINFO (type
);
493 field_type
= BINFO_TYPE (pri
);
494 else if (TYPE_CONTAINS_VPTR_P (type
))
495 field_type
= vtbl_ptr_type_node
;
499 /* Find the element for the primary base or vptr and move it to the
500 beginning of the vec. */
501 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
502 if (TREE_TYPE (ce
->index
) == field_type
)
505 if (i
> 0 && i
< vec_safe_length (v
))
507 vec
<constructor_elt
, va_gc
> &vref
= *v
;
508 constructor_elt elt
= vref
[i
];
517 /* Build compile-time evalable representations of member-initializer list
518 for a constexpr constructor. */
521 build_constexpr_constructor_member_initializers (tree type
, tree body
)
523 vec
<constructor_elt
, va_gc
> *vec
= NULL
;
525 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
526 || TREE_CODE (body
) == EH_SPEC_BLOCK
)
527 body
= TREE_OPERAND (body
, 0);
528 if (TREE_CODE (body
) == STATEMENT_LIST
)
529 body
= STATEMENT_LIST_HEAD (body
)->stmt
;
530 body
= BIND_EXPR_BODY (body
);
531 if (TREE_CODE (body
) == CLEANUP_POINT_EXPR
)
533 body
= TREE_OPERAND (body
, 0);
534 if (TREE_CODE (body
) == EXPR_STMT
)
535 body
= TREE_OPERAND (body
, 0);
536 if (TREE_CODE (body
) == INIT_EXPR
537 && (same_type_ignoring_top_level_qualifiers_p
538 (TREE_TYPE (TREE_OPERAND (body
, 0)),
539 current_class_type
)))
542 return TREE_OPERAND (body
, 1);
544 ok
= build_data_member_initialization (body
, &vec
);
546 else if (TREE_CODE (body
) == STATEMENT_LIST
)
548 tree_stmt_iterator i
;
549 for (i
= tsi_start (body
); !tsi_end_p (i
); tsi_next (&i
))
551 ok
= build_data_member_initialization (tsi_stmt (i
), &vec
);
556 else if (TREE_CODE (body
) == TRY_BLOCK
)
558 error ("body of %<constexpr%> constructor cannot be "
559 "a function-try-block");
560 return error_mark_node
;
562 else if (EXPR_P (body
))
563 ok
= build_data_member_initialization (body
, &vec
);
565 gcc_assert (errorcount
> 0);
568 if (vec_safe_length (vec
) > 0)
570 /* In a delegating constructor, return the target. */
571 constructor_elt
*ce
= &(*vec
)[0];
572 if (ce
->index
== current_class_ptr
)
579 vec
= sort_constexpr_mem_initializers (type
, vec
);
580 return build_constructor (type
, vec
);
583 return error_mark_node
;
586 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
587 declared to be constexpr, or a sub-statement thereof. Returns the
588 return value if suitable, error_mark_node for a statement not allowed in
589 a constexpr function, or NULL_TREE if no return value was found. */
592 constexpr_fn_retval (tree body
)
594 switch (TREE_CODE (body
))
598 tree_stmt_iterator i
;
599 tree expr
= NULL_TREE
;
600 for (i
= tsi_start (body
); !tsi_end_p (i
); tsi_next (&i
))
602 tree s
= constexpr_fn_retval (tsi_stmt (i
));
603 if (s
== error_mark_node
)
604 return error_mark_node
;
605 else if (s
== NULL_TREE
)
606 /* Keep iterating. */;
608 /* Multiple return statements. */
609 return error_mark_node
;
617 return break_out_target_exprs (TREE_OPERAND (body
, 0));
621 tree decl
= DECL_EXPR_DECL (body
);
622 if (TREE_CODE (decl
) == USING_DECL
623 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
624 || DECL_ARTIFICIAL (decl
))
626 return error_mark_node
;
629 case CLEANUP_POINT_EXPR
:
630 return constexpr_fn_retval (TREE_OPERAND (body
, 0));
633 if (!check_constexpr_bind_expr_vars (body
))
634 return error_mark_node
;
635 return constexpr_fn_retval (BIND_EXPR_BODY (body
));
641 return error_mark_node
;
645 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
646 FUN; do the necessary transformations to turn it into a single expression
647 that we can store in the hash table. */
650 massage_constexpr_body (tree fun
, tree body
)
652 if (DECL_CONSTRUCTOR_P (fun
))
653 body
= build_constexpr_constructor_member_initializers
654 (DECL_CONTEXT (fun
), body
);
655 else if (cxx_dialect
< cxx14
)
657 if (TREE_CODE (body
) == EH_SPEC_BLOCK
)
658 body
= EH_SPEC_STMTS (body
);
659 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
660 body
= TREE_OPERAND (body
, 0);
661 body
= constexpr_fn_retval (body
);
666 /* FUN is a constexpr constructor with massaged body BODY. Return true
667 if some bases/fields are uninitialized, and complain if COMPLAIN. */
670 cx_check_missing_mem_inits (tree fun
, tree body
, bool complain
)
677 if (TREE_CODE (body
) != CONSTRUCTOR
)
680 nelts
= CONSTRUCTOR_NELTS (body
);
681 ctype
= DECL_CONTEXT (fun
);
682 field
= TYPE_FIELDS (ctype
);
684 if (TREE_CODE (ctype
) == UNION_TYPE
)
686 if (nelts
== 0 && next_initializable_field (field
))
689 error ("%<constexpr%> constructor for union %qT must "
690 "initialize exactly one non-static data member", ctype
);
697 for (i
= 0; i
<= nelts
; ++i
)
704 index
= CONSTRUCTOR_ELT (body
, i
)->index
;
705 /* Skip base and vtable inits. */
706 if (TREE_CODE (index
) != FIELD_DECL
707 || DECL_ARTIFICIAL (index
))
710 for (; field
!= index
; field
= DECL_CHAIN (field
))
713 if (TREE_CODE (field
) != FIELD_DECL
714 || (DECL_C_BIT_FIELD (field
) && !DECL_NAME (field
))
715 || DECL_ARTIFICIAL (field
))
717 ftype
= strip_array_types (TREE_TYPE (field
));
718 if (type_has_constexpr_default_constructor (ftype
))
720 /* It's OK to skip a member with a trivial constexpr ctor.
721 A constexpr ctor that isn't trivial should have been
723 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype
)
729 error ("member %qD must be initialized by mem-initializer "
730 "in %<constexpr%> constructor", field
);
731 inform (DECL_SOURCE_LOCATION (field
), "declared here");
734 if (field
== NULL_TREE
)
736 field
= DECL_CHAIN (field
);
742 /* We are processing the definition of the constexpr function FUN.
743 Check that its BODY fulfills the propriate requirements and
744 enter it in the constexpr function definition table.
745 For constructor BODY is actually the TREE_LIST of the
746 member-initializer list. */
749 register_constexpr_fundef (tree fun
, tree body
)
751 constexpr_fundef entry
;
752 constexpr_fundef
**slot
;
754 if (!is_valid_constexpr_fn (fun
, !DECL_GENERATED_P (fun
)))
757 body
= massage_constexpr_body (fun
, body
);
758 if (body
== NULL_TREE
|| body
== error_mark_node
)
760 if (!DECL_CONSTRUCTOR_P (fun
))
761 error ("body of constexpr function %qD not a return-statement", fun
);
765 if (!potential_rvalue_constant_expression (body
))
767 if (!DECL_GENERATED_P (fun
))
768 require_potential_rvalue_constant_expression (body
);
772 if (DECL_CONSTRUCTOR_P (fun
)
773 && cx_check_missing_mem_inits (fun
, body
, !DECL_GENERATED_P (fun
)))
776 /* Create the constexpr function table if necessary. */
777 if (constexpr_fundef_table
== NULL
)
778 constexpr_fundef_table
779 = hash_table
<constexpr_fundef_hasher
>::create_ggc (101);
783 slot
= constexpr_fundef_table
->find_slot (&entry
, INSERT
);
785 gcc_assert (*slot
== NULL
);
786 *slot
= ggc_alloc
<constexpr_fundef
> ();
792 /* FUN is a non-constexpr function called in a context that requires a
793 constant expression. If it comes from a constexpr template, explain why
794 the instantiation isn't constexpr. */
797 explain_invalid_constexpr_fn (tree fun
)
799 static hash_set
<tree
> *diagnosed
;
802 /* Only diagnose defaulted functions or instantiations. */
803 if (!DECL_DEFAULTED_FN (fun
)
804 && !is_instantiation_of_constexpr (fun
))
806 if (diagnosed
== NULL
)
807 diagnosed
= new hash_set
<tree
>;
808 if (diagnosed
->add (fun
))
809 /* Already explained. */
812 save_loc
= input_location
;
813 input_location
= DECL_SOURCE_LOCATION (fun
);
814 inform (0, "%q+D is not usable as a constexpr function because:", fun
);
815 /* First check the declaration. */
816 if (is_valid_constexpr_fn (fun
, true))
818 /* Then if it's OK, the body. */
819 if (!DECL_DECLARED_CONSTEXPR_P (fun
))
820 explain_implicit_non_constexpr (fun
);
823 body
= massage_constexpr_body (fun
, DECL_SAVED_TREE (fun
));
824 require_potential_rvalue_constant_expression (body
);
825 if (DECL_CONSTRUCTOR_P (fun
))
826 cx_check_missing_mem_inits (fun
, body
, true);
829 input_location
= save_loc
;
832 /* Objects of this type represent calls to constexpr functions
833 along with the bindings of parameters to their arguments, for
834 the purpose of compile time evaluation. */
836 struct GTY((for_user
)) constexpr_call
{
837 /* Description of the constexpr function definition. */
838 constexpr_fundef
*fundef
;
839 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
840 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
841 Note: This arrangement is made to accommodate the use of
842 iterative_hash_template_arg (see pt.c). If you change this
843 representation, also change the hash calculation in
844 cxx_eval_call_expression. */
846 /* Result of the call.
847 NULL means the call is being evaluated.
848 error_mark_node means that the evaluation was erroneous;
849 otherwise, the actuall value of the call. */
851 /* The hash of this call; we remember it here to avoid having to
852 recalculate it when expanding the hash table. */
856 struct constexpr_call_hasher
: ggc_hasher
<constexpr_call
*>
858 static hashval_t
hash (constexpr_call
*);
859 static bool equal (constexpr_call
*, constexpr_call
*);
862 /* The constexpr expansion context. CALL is the current function
863 expansion, CTOR is the current aggregate initializer, OBJECT is the
864 object being initialized by CTOR, either a VAR_DECL or a _REF. VALUES
865 is a map of values of variables initialized within the expression. */
867 struct constexpr_ctx
{
868 /* The innermost call we're evaluating. */
869 constexpr_call
*call
;
870 /* Values for any temporaries or local variables within the
871 constant-expression. */
872 hash_map
<tree
,tree
> *values
;
873 /* The CONSTRUCTOR we're currently building up for an aggregate
876 /* The object we're building the CONSTRUCTOR for. */
878 /* Whether we should error on a non-constant expression or fail quietly. */
880 /* Whether we are strictly conforming to constant expression rules or
881 trying harder to get a constant value. */
885 /* A table of all constexpr calls that have been evaluated by the
886 compiler in this translation unit. */
888 static GTY (()) hash_table
<constexpr_call_hasher
> *constexpr_call_table
;
890 static tree
cxx_eval_constant_expression (const constexpr_ctx
*, tree
,
891 bool, bool *, bool *, tree
* = NULL
);
893 /* Compute a hash value for a constexpr call representation. */
896 constexpr_call_hasher::hash (constexpr_call
*info
)
901 /* Return true if the objects pointed to by P and Q represent calls
902 to the same constexpr function with the same arguments.
903 Otherwise, return false. */
906 constexpr_call_hasher::equal (constexpr_call
*lhs
, constexpr_call
*rhs
)
912 if (!constexpr_fundef_hasher::equal (lhs
->fundef
, rhs
->fundef
))
914 lhs_bindings
= lhs
->bindings
;
915 rhs_bindings
= rhs
->bindings
;
916 while (lhs_bindings
!= NULL
&& rhs_bindings
!= NULL
)
918 tree lhs_arg
= TREE_VALUE (lhs_bindings
);
919 tree rhs_arg
= TREE_VALUE (rhs_bindings
);
920 gcc_assert (TREE_TYPE (lhs_arg
) == TREE_TYPE (rhs_arg
));
921 if (!cp_tree_equal (lhs_arg
, rhs_arg
))
923 lhs_bindings
= TREE_CHAIN (lhs_bindings
);
924 rhs_bindings
= TREE_CHAIN (rhs_bindings
);
926 return lhs_bindings
== rhs_bindings
;
929 /* Initialize the constexpr call table, if needed. */
932 maybe_initialize_constexpr_call_table (void)
934 if (constexpr_call_table
== NULL
)
935 constexpr_call_table
= hash_table
<constexpr_call_hasher
>::create_ggc (101);
938 /* We have an expression tree T that represents a call, either CALL_EXPR
939 or AGGR_INIT_EXPR. If the call is lexically to a named function,
940 retrun the _DECL for that function. */
943 get_function_named_in_call (tree t
)
946 switch (TREE_CODE (t
))
949 fun
= CALL_EXPR_FN (t
);
953 fun
= AGGR_INIT_EXPR_FN (t
);
960 if (fun
&& TREE_CODE (fun
) == ADDR_EXPR
961 && TREE_CODE (TREE_OPERAND (fun
, 0)) == FUNCTION_DECL
)
962 fun
= TREE_OPERAND (fun
, 0);
966 /* We have an expression tree T that represents a call, either CALL_EXPR
967 or AGGR_INIT_EXPR. Return the Nth argument. */
970 get_nth_callarg (tree t
, int n
)
972 switch (TREE_CODE (t
))
975 return CALL_EXPR_ARG (t
, n
);
978 return AGGR_INIT_EXPR_ARG (t
, n
);
986 /* Look up the binding of the function parameter T in a constexpr
987 function call context CALL. */
990 lookup_parameter_binding (const constexpr_call
*call
, tree t
)
992 tree b
= purpose_member (t
, call
->bindings
);
993 return TREE_VALUE (b
);
996 /* Attempt to evaluate T which represents a call to a builtin function.
997 We assume here that all builtin functions evaluate to scalar types
998 represented by _CST nodes. */
1001 cxx_eval_builtin_function_call (const constexpr_ctx
*ctx
, tree t
,
1003 bool *non_constant_p
, bool *overflow_p
)
1005 const int nargs
= call_expr_nargs (t
);
1006 tree
*args
= (tree
*) alloca (nargs
* sizeof (tree
));
1009 for (i
= 0; i
< nargs
; ++i
)
1011 args
[i
] = cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, i
),
1013 non_constant_p
, overflow_p
);
1014 if (ctx
->quiet
&& *non_constant_p
)
1017 if (*non_constant_p
)
1019 new_call
= fold_build_call_array_loc (EXPR_LOCATION (t
), TREE_TYPE (t
),
1020 CALL_EXPR_FN (t
), nargs
, args
);
1021 VERIFY_CONSTANT (new_call
);
1025 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1026 the type of the value to match. */
1029 adjust_temp_type (tree type
, tree temp
)
1031 if (TREE_TYPE (temp
) == type
)
1033 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1034 if (TREE_CODE (temp
) == CONSTRUCTOR
)
1035 return build_constructor (type
, CONSTRUCTOR_ELTS (temp
));
1036 gcc_assert (scalarish_type_p (type
));
1037 return cp_fold_convert (type
, temp
);
1040 /* True if we want to use the new handling of constexpr calls based on
1042 #define use_new_call true
1044 /* Subroutine of cxx_eval_call_expression.
1045 We are processing a call expression (either CALL_EXPR or
1046 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1047 all arguments and bind their values to correspondings
1048 parameters, making up the NEW_CALL context. */
1051 cxx_bind_parameters_in_call (const constexpr_ctx
*ctx
, tree t
,
1052 constexpr_call
*new_call
,
1053 bool *non_constant_p
, bool *overflow_p
,
1054 bool *non_constant_args
)
1056 const int nargs
= call_expr_nargs (t
);
1057 tree fun
= new_call
->fundef
->decl
;
1058 tree parms
= DECL_ARGUMENTS (fun
);
1060 tree
*p
= &new_call
->bindings
;
1061 for (i
= 0; i
< nargs
; ++i
)
1064 tree type
= parms
? TREE_TYPE (parms
) : void_type_node
;
1065 x
= get_nth_callarg (t
, i
);
1066 /* For member function, the first argument is a pointer to the implied
1067 object. For a constructor, it might still be a dummy object, in
1068 which case we get the real argument from ctx. */
1069 if (i
== 0 && DECL_CONSTRUCTOR_P (fun
)
1070 && is_dummy_object (x
))
1073 x
= cp_build_addr_expr (x
, tf_warning_or_error
);
1076 if (parms
&& DECL_BY_REFERENCE (parms
) && !use_new_call
)
1078 /* cp_genericize made this a reference for argument passing, but
1079 we don't want to treat it like one for C++11 constexpr
1080 evaluation. C++14 constexpr evaluation uses the genericized
1082 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
1083 gcc_assert (TREE_CODE (TREE_TYPE (x
)) == REFERENCE_TYPE
);
1084 type
= TREE_TYPE (type
);
1085 x
= convert_from_reference (x
);
1088 arg
= cxx_eval_constant_expression (ctx
, x
, lval
,
1089 non_constant_p
, overflow_p
);
1090 /* Don't VERIFY_CONSTANT here. */
1091 if (*non_constant_p
&& ctx
->quiet
)
1093 /* Just discard ellipsis args after checking their constantitude. */
1096 if (*non_constant_p
)
1097 /* Don't try to adjust the type of non-constant args. */
1100 /* Make sure the binding has the same type as the parm. */
1101 if (TREE_CODE (type
) != REFERENCE_TYPE
)
1102 arg
= adjust_temp_type (type
, arg
);
1103 if (!TREE_CONSTANT (arg
))
1104 *non_constant_args
= true;
1105 *p
= build_tree_list (parms
, arg
);
1106 p
= &TREE_CHAIN (*p
);
1108 parms
= TREE_CHAIN (parms
);
1112 /* Variables and functions to manage constexpr call expansion context.
1113 These do not need to be marked for PCH or GC. */
1115 /* FIXME remember and print actual constant arguments. */
1116 static vec
<tree
> call_stack
= vNULL
;
1117 static int call_stack_tick
;
1118 static int last_cx_error_tick
;
1121 push_cx_call_context (tree call
)
1124 if (!EXPR_HAS_LOCATION (call
))
1125 SET_EXPR_LOCATION (call
, input_location
);
1126 call_stack
.safe_push (call
);
1127 if (call_stack
.length () > (unsigned) max_constexpr_depth
)
1133 pop_cx_call_context (void)
1140 cx_error_context (void)
1142 vec
<tree
> r
= vNULL
;
1143 if (call_stack_tick
!= last_cx_error_tick
1144 && !call_stack
.is_empty ())
1146 last_cx_error_tick
= call_stack_tick
;
1150 /* Subroutine of cxx_eval_constant_expression.
1151 Evaluate the call expression tree T in the context of OLD_CALL expression
1155 cxx_eval_call_expression (const constexpr_ctx
*ctx
, tree t
,
1157 bool *non_constant_p
, bool *overflow_p
)
1159 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
1160 tree fun
= get_function_named_in_call (t
);
1161 constexpr_call new_call
= { NULL
, NULL
, NULL
, 0 };
1164 if (fun
== NULL_TREE
)
1165 switch (CALL_EXPR_IFN (t
))
1167 case IFN_UBSAN_NULL
:
1168 case IFN_UBSAN_BOUNDS
:
1172 error_at (loc
, "call to internal function");
1173 *non_constant_p
= true;
1177 if (TREE_CODE (fun
) != FUNCTION_DECL
)
1179 /* Might be a constexpr function pointer. */
1180 fun
= cxx_eval_constant_expression (ctx
, fun
,
1181 /*lval*/false, non_constant_p
,
1184 if (TREE_CODE (fun
) == ADDR_EXPR
)
1185 fun
= TREE_OPERAND (fun
, 0);
1187 if (TREE_CODE (fun
) != FUNCTION_DECL
)
1189 if (!ctx
->quiet
&& !*non_constant_p
)
1190 error_at (loc
, "expression %qE does not designate a constexpr "
1192 *non_constant_p
= true;
1195 if (DECL_CLONED_FUNCTION_P (fun
))
1196 fun
= DECL_CLONED_FUNCTION (fun
);
1198 if (is_ubsan_builtin_p (fun
))
1201 if (is_builtin_fn (fun
))
1202 return cxx_eval_builtin_function_call (ctx
, t
,
1203 lval
, non_constant_p
, overflow_p
);
1204 if (!DECL_DECLARED_CONSTEXPR_P (fun
))
1208 error_at (loc
, "call to non-constexpr function %qD", fun
);
1209 explain_invalid_constexpr_fn (fun
);
1211 *non_constant_p
= true;
1215 /* Shortcut trivial constructor/op=. */
1216 if (trivial_fn_p (fun
))
1218 if (call_expr_nargs (t
) == 2)
1220 tree arg
= convert_from_reference (get_nth_callarg (t
, 1));
1221 return cxx_eval_constant_expression (ctx
, arg
,
1222 lval
, non_constant_p
,
1225 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
1226 && AGGR_INIT_ZERO_FIRST (t
))
1227 return build_zero_init (DECL_CONTEXT (fun
), NULL_TREE
, false);
1230 /* If in direct recursive call, optimize definition search. */
1231 if (ctx
&& ctx
->call
&& ctx
->call
->fundef
->decl
== fun
)
1232 new_call
.fundef
= ctx
->call
->fundef
;
1235 new_call
.fundef
= retrieve_constexpr_fundef (fun
);
1236 if (new_call
.fundef
== NULL
|| new_call
.fundef
->body
== NULL
)
1240 if (DECL_INITIAL (fun
))
1242 /* The definition of fun was somehow unsuitable. */
1243 error_at (loc
, "%qD called in a constant expression", fun
);
1244 explain_invalid_constexpr_fn (fun
);
1247 error_at (loc
, "%qD used before its definition", fun
);
1249 *non_constant_p
= true;
1254 constexpr_ctx new_ctx
= *ctx
;
1255 if (DECL_CONSTRUCTOR_P (fun
) && !ctx
->object
1256 && TREE_CODE (t
) == AGGR_INIT_EXPR
)
1258 /* We want to have an initialization target for an AGGR_INIT_EXPR.
1259 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
1260 new_ctx
.object
= AGGR_INIT_EXPR_SLOT (t
);
1261 tree ctor
= new_ctx
.ctor
= build_constructor (DECL_CONTEXT (fun
), NULL
);
1262 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctor
) = true;
1263 ctx
->values
->put (new_ctx
.object
, ctor
);
1267 bool non_constant_args
= false;
1268 cxx_bind_parameters_in_call (ctx
, t
, &new_call
,
1269 non_constant_p
, overflow_p
, &non_constant_args
);
1270 if (*non_constant_p
)
1273 depth_ok
= push_cx_call_context (t
);
1275 tree result
= NULL_TREE
;
1277 constexpr_call
*entry
= NULL
;
1278 if (!non_constant_args
)
1280 new_call
.hash
= iterative_hash_template_arg
1281 (new_call
.bindings
, constexpr_fundef_hasher::hash (new_call
.fundef
));
1283 /* If we have seen this call before, we are done. */
1284 maybe_initialize_constexpr_call_table ();
1285 constexpr_call
**slot
1286 = constexpr_call_table
->find_slot (&new_call
, INSERT
);
1290 /* We need to keep a pointer to the entry, not just the slot, as the
1291 slot can move in the call to cxx_eval_builtin_function_call. */
1292 *slot
= entry
= ggc_alloc
<constexpr_call
> ();
1295 /* Calls which are in progress have their result set to NULL
1296 so that we can detect circular dependencies. */
1297 else if (entry
->result
== NULL
)
1300 error ("call has circular dependency");
1301 *non_constant_p
= true;
1302 entry
->result
= result
= error_mark_node
;
1305 result
= entry
->result
;
1311 error ("constexpr evaluation depth exceeds maximum of %d (use "
1312 "-fconstexpr-depth= to increase the maximum)",
1313 max_constexpr_depth
);
1314 *non_constant_p
= true;
1315 result
= error_mark_node
;
1319 if (!result
|| result
== error_mark_node
)
1323 new_ctx
.call
= &new_call
;
1324 result
= (cxx_eval_constant_expression
1325 (&new_ctx
, new_call
.fundef
->body
,
1327 non_constant_p
, overflow_p
));
1331 if (DECL_SAVED_TREE (fun
) == NULL_TREE
1332 && (DECL_CONSTRUCTOR_P (fun
) || DECL_DESTRUCTOR_P (fun
)))
1333 /* The maybe-in-charge 'tor had its DECL_SAVED_TREE
1334 cleared, try the first clone. */
1335 fun
= DECL_CHAIN (fun
);
1336 gcc_assert (DECL_SAVED_TREE (fun
));
1339 /* Unshare the whole function body. */
1340 tree body
= copy_fn (fun
, parms
, res
);
1342 /* Associate the bindings with the remapped parms. */
1343 tree bound
= new_call
.bindings
;
1344 tree remapped
= parms
;
1347 tree oparm
= TREE_PURPOSE (bound
);
1348 tree arg
= TREE_VALUE (bound
);
1349 gcc_assert (DECL_NAME (remapped
) == DECL_NAME (oparm
));
1350 ctx
->values
->put (remapped
, arg
);
1351 bound
= TREE_CHAIN (bound
);
1352 remapped
= DECL_CHAIN (remapped
);
1354 /* Add the RESULT_DECL to the values map, too. */
1355 tree slot
= NULL_TREE
;
1356 if (DECL_BY_REFERENCE (res
))
1358 slot
= AGGR_INIT_EXPR_SLOT (t
);
1359 tree addr
= build_address (slot
);
1360 addr
= build_nop (TREE_TYPE (res
), addr
);
1361 ctx
->values
->put (res
, addr
);
1362 ctx
->values
->put (slot
, NULL_TREE
);
1365 ctx
->values
->put (res
, NULL_TREE
);
1367 tree jump_target
= NULL_TREE
;
1368 cxx_eval_constant_expression (ctx
, body
,
1369 lval
, non_constant_p
, overflow_p
,
1372 if (DECL_CONSTRUCTOR_P (fun
))
1373 /* This can be null for a subobject constructor call, in
1374 which case what we care about is the initialization
1375 side-effects rather than the value. We could get at the
1376 value by evaluating *this, but we don't bother; there's
1377 no need to put such a call in the hash table. */
1378 result
= lval
? ctx
->object
: ctx
->ctor
;
1381 result
= *ctx
->values
->get (slot
? slot
: res
);
1382 if (result
== NULL_TREE
&& !*non_constant_p
)
1385 error ("constexpr call flows off the end "
1387 *non_constant_p
= true;
1391 /* Remove the parms/result from the values map. Is it worth
1392 bothering to do this when the map itself is only live for
1393 one constexpr evaluation? If so, maybe also clear out
1394 other vars from call, maybe in BIND_EXPR handling? */
1395 ctx
->values
->remove (res
);
1397 ctx
->values
->remove (slot
);
1398 for (tree parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
1399 ctx
->values
->remove (parm
);
1403 if (result
== error_mark_node
)
1404 *non_constant_p
= true;
1405 if (*non_constant_p
)
1406 result
= error_mark_node
;
1409 /* If this was a call to initialize an object, set the type of
1410 the CONSTRUCTOR to the type of that object. */
1411 if (DECL_CONSTRUCTOR_P (fun
) && !use_new_call
)
1413 tree ob_arg
= get_nth_callarg (t
, 0);
1414 STRIP_NOPS (ob_arg
);
1415 gcc_assert (TYPE_PTR_P (TREE_TYPE (ob_arg
))
1416 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (ob_arg
))));
1417 result
= adjust_temp_type (TREE_TYPE (TREE_TYPE (ob_arg
)),
1424 entry
->result
= result
;
1427 pop_cx_call_context ();
1428 return unshare_expr (result
);
1431 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
1434 reduced_constant_expression_p (tree t
)
1436 switch (TREE_CODE (t
))
1439 /* Even if we can't lower this yet, it's constant. */
1443 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
1444 tree elt
; unsigned HOST_WIDE_INT idx
;
1445 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), idx
, elt
)
1446 if (!reduced_constant_expression_p (elt
))
1451 /* FIXME are we calling this too much? */
1452 return initializer_constant_valid_p (t
, TREE_TYPE (t
)) != NULL_TREE
;
1456 /* Some expressions may have constant operands but are not constant
1457 themselves, such as 1/0. Call this function (or rather, the macro
1458 following it) to check for that condition.
1460 We only call this in places that require an arithmetic constant, not in
1461 places where we might have a non-constant expression that can be a
1462 component of a constant expression, such as the address of a constexpr
1463 variable that might be dereferenced later. */
1466 verify_constant (tree t
, bool allow_non_constant
, bool *non_constant_p
,
1469 if (!*non_constant_p
&& !reduced_constant_expression_p (t
))
1471 if (!allow_non_constant
)
1472 error ("%q+E is not a constant expression", t
);
1473 *non_constant_p
= true;
1475 if (TREE_OVERFLOW_P (t
))
1477 if (!allow_non_constant
)
1479 permerror (input_location
, "overflow in constant expression");
1480 /* If we're being permissive (and are in an enforcing
1481 context), ignore the overflow. */
1482 if (flag_permissive
)
1483 return *non_constant_p
;
1487 return *non_constant_p
;
1490 /* Check whether the shift operation with code CODE and type TYPE on LHS
1491 and RHS is undefined. If it is, give an error with an explanation,
1492 and return true; return false otherwise. */
1495 cxx_eval_check_shift_p (location_t loc
, const constexpr_ctx
*ctx
,
1496 enum tree_code code
, tree type
, tree lhs
, tree rhs
)
1498 if ((code
!= LSHIFT_EXPR
&& code
!= RSHIFT_EXPR
)
1499 || TREE_CODE (lhs
) != INTEGER_CST
1500 || TREE_CODE (rhs
) != INTEGER_CST
)
1503 tree lhstype
= TREE_TYPE (lhs
);
1504 unsigned HOST_WIDE_INT uprec
= TYPE_PRECISION (TREE_TYPE (lhs
));
1506 /* [expr.shift] The behavior is undefined if the right operand
1507 is negative, or greater than or equal to the length in bits
1508 of the promoted left operand. */
1509 if (tree_int_cst_sgn (rhs
) == -1)
1512 error_at (loc
, "right operand of shift expression %q+E is negative",
1513 build2_loc (loc
, code
, type
, lhs
, rhs
));
1516 if (compare_tree_int (rhs
, uprec
) >= 0)
1519 error_at (loc
, "right operand of shift expression %q+E is >= than "
1520 "the precision of the left operand",
1521 build2_loc (loc
, code
, type
, lhs
, rhs
));
1525 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
1526 if E1 has a signed type and non-negative value, and E1x2^E2 is
1527 representable in the corresponding unsigned type of the result type,
1528 then that value, converted to the result type, is the resulting value;
1529 otherwise, the behavior is undefined. */
1530 if (code
== LSHIFT_EXPR
&& !TYPE_UNSIGNED (lhstype
)
1531 && (cxx_dialect
>= cxx11
))
1533 if (tree_int_cst_sgn (lhs
) == -1)
1536 error_at (loc
, "left operand of shift expression %q+E is negative",
1537 build2_loc (loc
, code
, type
, lhs
, rhs
));
1540 /* For signed x << y the following:
1541 (unsigned) x >> ((prec (lhs) - 1) - y)
1542 if > 1, is undefined. The right-hand side of this formula
1543 is the highest bit of the LHS that can be set (starting from 0),
1544 so that the shift doesn't overflow. We then right-shift the LHS
1545 to see whether any other bit is set making the original shift
1546 undefined -- the result is not representable in the corresponding
1548 tree t
= build_int_cst (unsigned_type_node
, uprec
- 1);
1549 t
= fold_build2 (MINUS_EXPR
, unsigned_type_node
, t
, rhs
);
1550 tree ulhs
= fold_convert (unsigned_type_for (lhstype
), lhs
);
1551 t
= fold_build2 (RSHIFT_EXPR
, TREE_TYPE (ulhs
), ulhs
, t
);
1552 if (tree_int_cst_lt (integer_one_node
, t
))
1555 error_at (loc
, "shift expression %q+E overflows",
1556 build2_loc (loc
, code
, type
, lhs
, rhs
));
1563 /* Subroutine of cxx_eval_constant_expression.
1564 Attempt to reduce the unary expression tree T to a compile time value.
1565 If successful, return the value. Otherwise issue a diagnostic
1566 and return error_mark_node. */
1569 cxx_eval_unary_expression (const constexpr_ctx
*ctx
, tree t
,
1571 bool *non_constant_p
, bool *overflow_p
)
1574 tree orig_arg
= TREE_OPERAND (t
, 0);
1575 tree arg
= cxx_eval_constant_expression (ctx
, orig_arg
, /*lval*/false,
1576 non_constant_p
, overflow_p
);
1577 VERIFY_CONSTANT (arg
);
1578 location_t loc
= EXPR_LOCATION (t
);
1579 enum tree_code code
= TREE_CODE (t
);
1580 tree type
= TREE_TYPE (t
);
1581 r
= fold_unary_loc (loc
, code
, type
, arg
);
1584 if (arg
== orig_arg
)
1587 r
= build1_loc (loc
, code
, type
, arg
);
1589 VERIFY_CONSTANT (r
);
1593 /* Subroutine of cxx_eval_constant_expression.
1594 Like cxx_eval_unary_expression, except for binary expressions. */
1597 cxx_eval_binary_expression (const constexpr_ctx
*ctx
, tree t
,
1599 bool *non_constant_p
, bool *overflow_p
)
1602 tree orig_lhs
= TREE_OPERAND (t
, 0);
1603 tree orig_rhs
= TREE_OPERAND (t
, 1);
1605 lhs
= cxx_eval_constant_expression (ctx
, orig_lhs
, /*lval*/false,
1606 non_constant_p
, overflow_p
);
1607 VERIFY_CONSTANT (lhs
);
1608 rhs
= cxx_eval_constant_expression (ctx
, orig_rhs
, /*lval*/false,
1609 non_constant_p
, overflow_p
);
1610 VERIFY_CONSTANT (rhs
);
1612 location_t loc
= EXPR_LOCATION (t
);
1613 enum tree_code code
= TREE_CODE (t
);
1614 tree type
= TREE_TYPE (t
);
1615 r
= fold_binary_loc (loc
, code
, type
, lhs
, rhs
);
1618 if (lhs
== orig_lhs
&& rhs
== orig_rhs
)
1621 r
= build2_loc (loc
, code
, type
, lhs
, rhs
);
1623 else if (cxx_eval_check_shift_p (loc
, ctx
, code
, type
, lhs
, rhs
))
1624 *non_constant_p
= true;
1625 VERIFY_CONSTANT (r
);
1629 /* Subroutine of cxx_eval_constant_expression.
1630 Attempt to evaluate condition expressions. Dead branches are not
1634 cxx_eval_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
1636 bool *non_constant_p
, bool *overflow_p
,
1639 tree val
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
1641 non_constant_p
, overflow_p
);
1642 VERIFY_CONSTANT (val
);
1643 /* Don't VERIFY_CONSTANT the other operands. */
1644 if (integer_zerop (val
))
1645 return cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 2),
1647 non_constant_p
, overflow_p
,
1649 return cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
1651 non_constant_p
, overflow_p
,
1655 /* Subroutine of cxx_eval_constant_expression.
1656 Attempt to reduce a reference to an array slot. */
1659 cxx_eval_array_reference (const constexpr_ctx
*ctx
, tree t
,
1661 bool *non_constant_p
, bool *overflow_p
)
1663 tree oldary
= TREE_OPERAND (t
, 0);
1664 tree ary
= cxx_eval_constant_expression (ctx
, oldary
,
1666 non_constant_p
, overflow_p
);
1670 unsigned len
, elem_nchars
= 1;
1671 if (*non_constant_p
)
1673 oldidx
= TREE_OPERAND (t
, 1);
1674 index
= cxx_eval_constant_expression (ctx
, oldidx
,
1676 non_constant_p
, overflow_p
);
1677 VERIFY_CONSTANT (index
);
1678 if (lval
&& ary
== oldary
&& index
== oldidx
)
1681 return build4 (ARRAY_REF
, TREE_TYPE (t
), ary
, index
, NULL
, NULL
);
1682 elem_type
= TREE_TYPE (TREE_TYPE (ary
));
1683 if (TREE_CODE (ary
) == CONSTRUCTOR
)
1684 len
= CONSTRUCTOR_NELTS (ary
);
1685 else if (TREE_CODE (ary
) == STRING_CST
)
1687 elem_nchars
= (TYPE_PRECISION (elem_type
)
1688 / TYPE_PRECISION (char_type_node
));
1689 len
= (unsigned) TREE_STRING_LENGTH (ary
) / elem_nchars
;
1693 /* We can't do anything with other tree codes, so use
1694 VERIFY_CONSTANT to complain and fail. */
1695 VERIFY_CONSTANT (ary
);
1698 if (compare_tree_int (index
, len
) >= 0)
1700 if (tree_int_cst_lt (index
, array_type_nelts_top (TREE_TYPE (ary
))))
1702 /* If it's within the array bounds but doesn't have an explicit
1703 initializer, it's value-initialized. */
1704 tree val
= build_value_init (elem_type
, tf_warning_or_error
);
1705 return cxx_eval_constant_expression (ctx
, val
,
1707 non_constant_p
, overflow_p
);
1711 error ("array subscript out of bound");
1712 *non_constant_p
= true;
1715 else if (tree_int_cst_lt (index
, integer_zero_node
))
1718 error ("negative array subscript");
1719 *non_constant_p
= true;
1722 i
= tree_to_shwi (index
);
1723 if (TREE_CODE (ary
) == CONSTRUCTOR
)
1724 return (*CONSTRUCTOR_ELTS (ary
))[i
].value
;
1725 else if (elem_nchars
== 1)
1726 return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary
))),
1727 TREE_STRING_POINTER (ary
)[i
]);
1730 tree type
= cv_unqualified (TREE_TYPE (TREE_TYPE (ary
)));
1731 return native_interpret_expr (type
, (const unsigned char *)
1732 TREE_STRING_POINTER (ary
)
1733 + i
* elem_nchars
, elem_nchars
);
1735 /* Don't VERIFY_CONSTANT here. */
1738 /* Subroutine of cxx_eval_constant_expression.
1739 Attempt to reduce a field access of a value of class type. */
1742 cxx_eval_component_reference (const constexpr_ctx
*ctx
, tree t
,
1744 bool *non_constant_p
, bool *overflow_p
)
1746 unsigned HOST_WIDE_INT i
;
1749 tree part
= TREE_OPERAND (t
, 1);
1750 tree orig_whole
= TREE_OPERAND (t
, 0);
1751 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
1753 non_constant_p
, overflow_p
);
1754 if (whole
== orig_whole
)
1757 return fold_build3 (COMPONENT_REF
, TREE_TYPE (t
),
1758 whole
, part
, NULL_TREE
);
1759 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
1761 if (!*non_constant_p
&& TREE_CODE (whole
) != CONSTRUCTOR
)
1764 error ("%qE is not a constant expression", orig_whole
);
1765 *non_constant_p
= true;
1767 if (DECL_MUTABLE_P (part
))
1770 error ("mutable %qD is not usable in a constant expression", part
);
1771 *non_constant_p
= true;
1773 if (*non_constant_p
)
1775 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
1782 /* We're in the middle of initializing it. */
1786 if (TREE_CODE (TREE_TYPE (whole
)) == UNION_TYPE
1787 && CONSTRUCTOR_NELTS (whole
) > 0)
1789 /* DR 1188 says we don't have to deal with this. */
1791 error ("accessing %qD member instead of initialized %qD member in "
1792 "constant expression", part
, CONSTRUCTOR_ELT (whole
, 0)->index
);
1793 *non_constant_p
= true;
1797 if (CONSTRUCTOR_NO_IMPLICIT_ZERO (whole
))
1799 /* 'whole' is part of the aggregate initializer we're currently
1800 building; if there's no initializer for this member yet, that's an
1803 error ("accessing uninitialized member %qD", part
);
1804 *non_constant_p
= true;
1808 /* If there's no explicit init for this field, it's value-initialized. */
1809 value
= build_value_init (TREE_TYPE (t
), tf_warning_or_error
);
1810 return cxx_eval_constant_expression (ctx
, value
,
1812 non_constant_p
, overflow_p
);
1815 /* Subroutine of cxx_eval_constant_expression.
1816 Attempt to reduce a field access of a value of class type that is
1817 expressed as a BIT_FIELD_REF. */
1820 cxx_eval_bit_field_ref (const constexpr_ctx
*ctx
, tree t
,
1822 bool *non_constant_p
, bool *overflow_p
)
1824 tree orig_whole
= TREE_OPERAND (t
, 0);
1825 tree retval
, fldval
, utype
, mask
;
1826 bool fld_seen
= false;
1827 HOST_WIDE_INT istart
, isize
;
1828 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
1830 non_constant_p
, overflow_p
);
1831 tree start
, field
, value
;
1832 unsigned HOST_WIDE_INT i
;
1834 if (whole
== orig_whole
)
1836 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
1838 if (!*non_constant_p
1839 && TREE_CODE (whole
) != VECTOR_CST
1840 && TREE_CODE (whole
) != CONSTRUCTOR
)
1843 error ("%qE is not a constant expression", orig_whole
);
1844 *non_constant_p
= true;
1846 if (*non_constant_p
)
1849 if (TREE_CODE (whole
) == VECTOR_CST
)
1850 return fold_ternary (BIT_FIELD_REF
, TREE_TYPE (t
), whole
,
1851 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 2));
1853 start
= TREE_OPERAND (t
, 2);
1854 istart
= tree_to_shwi (start
);
1855 isize
= tree_to_shwi (TREE_OPERAND (t
, 1));
1856 utype
= TREE_TYPE (t
);
1857 if (!TYPE_UNSIGNED (utype
))
1858 utype
= build_nonstandard_integer_type (TYPE_PRECISION (utype
), 1);
1859 retval
= build_int_cst (utype
, 0);
1860 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
1862 tree bitpos
= bit_position (field
);
1863 if (bitpos
== start
&& DECL_SIZE (field
) == TREE_OPERAND (t
, 1))
1865 if (TREE_CODE (TREE_TYPE (field
)) == INTEGER_TYPE
1866 && TREE_CODE (value
) == INTEGER_CST
1867 && tree_fits_shwi_p (bitpos
)
1868 && tree_fits_shwi_p (DECL_SIZE (field
)))
1870 HOST_WIDE_INT bit
= tree_to_shwi (bitpos
);
1871 HOST_WIDE_INT sz
= tree_to_shwi (DECL_SIZE (field
));
1872 HOST_WIDE_INT shift
;
1873 if (bit
>= istart
&& bit
+ sz
<= istart
+ isize
)
1875 fldval
= fold_convert (utype
, value
);
1876 mask
= build_int_cst_type (utype
, -1);
1877 mask
= fold_build2 (LSHIFT_EXPR
, utype
, mask
,
1878 size_int (TYPE_PRECISION (utype
) - sz
));
1879 mask
= fold_build2 (RSHIFT_EXPR
, utype
, mask
,
1880 size_int (TYPE_PRECISION (utype
) - sz
));
1881 fldval
= fold_build2 (BIT_AND_EXPR
, utype
, fldval
, mask
);
1882 shift
= bit
- istart
;
1883 if (BYTES_BIG_ENDIAN
)
1884 shift
= TYPE_PRECISION (utype
) - shift
- sz
;
1885 fldval
= fold_build2 (LSHIFT_EXPR
, utype
, fldval
,
1887 retval
= fold_build2 (BIT_IOR_EXPR
, utype
, retval
, fldval
);
1893 return fold_convert (TREE_TYPE (t
), retval
);
1895 return error_mark_node
;
1898 /* Subroutine of cxx_eval_constant_expression.
1899 Evaluate a short-circuited logical expression T in the context
1900 of a given constexpr CALL. BAILOUT_VALUE is the value for
1901 early return. CONTINUE_VALUE is used here purely for
1902 sanity check purposes. */
1905 cxx_eval_logical_expression (const constexpr_ctx
*ctx
, tree t
,
1906 tree bailout_value
, tree continue_value
,
1908 bool *non_constant_p
, bool *overflow_p
)
1911 tree lhs
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
1913 non_constant_p
, overflow_p
);
1914 VERIFY_CONSTANT (lhs
);
1915 if (tree_int_cst_equal (lhs
, bailout_value
))
1917 gcc_assert (tree_int_cst_equal (lhs
, continue_value
));
1918 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
1919 lval
, non_constant_p
,
1921 VERIFY_CONSTANT (r
);
1925 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
1926 CONSTRUCTOR elements to initialize (part of) an object containing that
1927 field. Return a pointer to the constructor_elt corresponding to the
1928 initialization of the field. */
1930 static constructor_elt
*
1931 base_field_constructor_elt (vec
<constructor_elt
, va_gc
> *v
, tree ref
)
1933 tree aggr
= TREE_OPERAND (ref
, 0);
1934 tree field
= TREE_OPERAND (ref
, 1);
1936 constructor_elt
*ce
;
1938 gcc_assert (TREE_CODE (ref
) == COMPONENT_REF
);
1940 if (TREE_CODE (aggr
) == COMPONENT_REF
)
1942 constructor_elt
*base_ce
1943 = base_field_constructor_elt (v
, aggr
);
1944 v
= CONSTRUCTOR_ELTS (base_ce
->value
);
1947 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
1948 if (ce
->index
== field
)
1955 /* Some of the expressions fed to the constexpr mechanism are calls to
1956 constructors, which have type void. In that case, return the type being
1957 initialized by the constructor. */
1960 initialized_type (tree t
)
1964 tree type
= cv_unqualified (TREE_TYPE (t
));
1965 if (TREE_CODE (t
) == CALL_EXPR
|| TREE_CODE (t
) == AGGR_INIT_EXPR
)
1967 /* A constructor call has void type, so we need to look deeper. */
1968 tree fn
= get_function_named_in_call (t
);
1969 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
1970 && DECL_CXX_CONSTRUCTOR_P (fn
))
1971 type
= DECL_CONTEXT (fn
);
1976 /* We're about to initialize element INDEX of an array or class from VALUE.
1977 Set up NEW_CTX appropriately by adjusting .object to refer to the
1978 subobject and creating a new CONSTRUCTOR if the element is itself
1979 a class or array. */
1982 init_subob_ctx (const constexpr_ctx
*ctx
, constexpr_ctx
&new_ctx
,
1983 tree index
, tree
&value
)
1987 if (index
&& TREE_CODE (index
) != INTEGER_CST
1988 && TREE_CODE (index
) != FIELD_DECL
)
1989 /* This won't have an element in the new CONSTRUCTOR. */
1992 tree type
= initialized_type (value
);
1993 if (!AGGREGATE_TYPE_P (type
) && !VECTOR_TYPE_P (type
))
1994 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
1997 /* The sub-aggregate initializer might contain a placeholder;
1998 update object to refer to the subobject and ctor to refer to
1999 the (newly created) sub-initializer. */
2001 new_ctx
.object
= build_ctor_subob_ref (index
, type
, ctx
->object
);
2002 tree elt
= build_constructor (type
, NULL
);
2003 CONSTRUCTOR_NO_IMPLICIT_ZERO (elt
) = true;
2006 if (TREE_CODE (value
) == TARGET_EXPR
)
2007 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
2008 value
= TARGET_EXPR_INITIAL (value
);
2011 /* We're about to process an initializer for a class or array TYPE. Make
2012 sure that CTX is set up appropriately. */
2015 verify_ctor_sanity (const constexpr_ctx
*ctx
, tree type
)
2017 /* We don't bother building a ctor for an empty base subobject. */
2018 if (is_empty_class (type
))
2021 /* We're in the middle of an initializer that might involve placeholders;
2022 our caller should have created a CONSTRUCTOR for us to put the
2023 initializer into. We will either return that constructor or T. */
2024 gcc_assert (ctx
->ctor
);
2025 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2026 (type
, TREE_TYPE (ctx
->ctor
)));
2027 gcc_assert (CONSTRUCTOR_NELTS (ctx
->ctor
) == 0);
2029 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2030 (type
, TREE_TYPE (ctx
->object
)));
2031 gcc_assert (!ctx
->object
|| !DECL_P (ctx
->object
)
2032 || *(ctx
->values
->get (ctx
->object
)) == ctx
->ctor
);
2035 /* Subroutine of cxx_eval_constant_expression.
2036 The expression tree T denotes a C-style array or a C-style
2037 aggregate. Reduce it to a constant expression. */
2040 cxx_eval_bare_aggregate (const constexpr_ctx
*ctx
, tree t
,
2042 bool *non_constant_p
, bool *overflow_p
)
2044 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
2045 bool changed
= false;
2046 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t
));
2048 verify_ctor_sanity (ctx
, TREE_TYPE (t
));
2049 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
2050 vec_alloc (*p
, vec_safe_length (v
));
2052 unsigned i
; tree index
, value
;
2053 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, index
, value
)
2055 constexpr_ctx new_ctx
;
2056 init_subob_ctx (ctx
, new_ctx
, index
, value
);
2057 if (new_ctx
.ctor
!= ctx
->ctor
)
2058 /* If we built a new CONSTRUCTOR, attach it now so that other
2059 initializers can refer to it. */
2060 CONSTRUCTOR_APPEND_ELT (*p
, index
, new_ctx
.ctor
);
2061 tree elt
= cxx_eval_constant_expression (&new_ctx
, value
,
2063 non_constant_p
, overflow_p
);
2064 /* Don't VERIFY_CONSTANT here. */
2065 if (ctx
->quiet
&& *non_constant_p
)
2069 if (index
&& TREE_CODE (index
) == COMPONENT_REF
)
2071 /* This is an initialization of a vfield inside a base
2072 subaggregate that we already initialized; push this
2073 initialization into the previous initialization. */
2074 constructor_elt
*inner
= base_field_constructor_elt (*p
, index
);
2079 && (TREE_CODE (index
) == NOP_EXPR
2080 || TREE_CODE (index
) == POINTER_PLUS_EXPR
))
2082 /* This is an initializer for an empty base; now that we've
2083 checked that it's constant, we can ignore it. */
2084 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index
))));
2087 else if (new_ctx
.ctor
!= ctx
->ctor
)
2089 /* We appended this element above; update the value. */
2090 gcc_assert ((*p
)->last().index
== index
);
2091 (*p
)->last().value
= elt
;
2094 CONSTRUCTOR_APPEND_ELT (*p
, index
, elt
);
2096 if (*non_constant_p
|| !changed
)
2099 /* We're done building this CONSTRUCTOR, so now we can interpret an
2100 element without an explicit initializer as value-initialized. */
2101 CONSTRUCTOR_NO_IMPLICIT_ZERO (t
) = false;
2102 if (TREE_CODE (TREE_TYPE (t
)) == VECTOR_TYPE
)
2107 /* Subroutine of cxx_eval_constant_expression.
2108 The expression tree T is a VEC_INIT_EXPR which denotes the desired
2109 initialization of a non-static data member of array type. Reduce it to a
2112 Note that apart from value-initialization (when VALUE_INIT is true),
2113 this is only intended to support value-initialization and the
2114 initializations done by defaulted constructors for classes with
2115 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
2116 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
2117 for the copy/move constructor. */
2120 cxx_eval_vec_init_1 (const constexpr_ctx
*ctx
, tree atype
, tree init
,
2121 bool value_init
, bool lval
,
2122 bool *non_constant_p
, bool *overflow_p
)
2124 tree elttype
= TREE_TYPE (atype
);
2125 unsigned HOST_WIDE_INT max
= tree_to_uhwi (array_type_nelts_top (atype
));
2126 verify_ctor_sanity (ctx
, atype
);
2127 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
2128 vec_alloc (*p
, max
+ 1);
2129 bool pre_init
= false;
2130 unsigned HOST_WIDE_INT i
;
2132 /* For the default constructor, build up a call to the default
2133 constructor of the element type. We only need to handle class types
2134 here, as for a constructor to be constexpr, all members must be
2135 initialized, which for a defaulted default constructor means they must
2136 be of a class type with a constexpr default constructor. */
2137 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
2138 /* We only do this at the lowest level. */;
2139 else if (value_init
)
2141 init
= build_value_init (elttype
, tf_warning_or_error
);
2146 vec
<tree
, va_gc
> *argvec
= make_tree_vector ();
2147 init
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
2148 &argvec
, elttype
, LOOKUP_NORMAL
,
2149 tf_warning_or_error
);
2150 release_tree_vector (argvec
);
2151 init
= build_aggr_init_expr (TREE_TYPE (init
), init
);
2155 for (i
= 0; i
< max
; ++i
)
2157 tree idx
= build_int_cst (size_type_node
, i
);
2159 constexpr_ctx new_ctx
;
2160 init_subob_ctx (ctx
, new_ctx
, idx
, pre_init
? init
: elttype
);
2161 if (new_ctx
.ctor
!= ctx
->ctor
)
2162 CONSTRUCTOR_APPEND_ELT (*p
, idx
, new_ctx
.ctor
);
2163 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
2165 /* A multidimensional array; recurse. */
2166 if (value_init
|| init
== NULL_TREE
)
2167 eltinit
= NULL_TREE
;
2169 eltinit
= cp_build_array_ref (input_location
, init
, idx
,
2170 tf_warning_or_error
);
2171 eltinit
= cxx_eval_vec_init_1 (&new_ctx
, elttype
, eltinit
, value_init
,
2173 non_constant_p
, overflow_p
);
2177 /* Initializing an element using value or default initialization
2178 we just pre-built above. */
2179 eltinit
= (cxx_eval_constant_expression
2181 lval
, non_constant_p
, overflow_p
));
2185 /* Copying an element. */
2186 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2187 (atype
, TREE_TYPE (init
)));
2188 eltinit
= cp_build_array_ref (input_location
, init
, idx
,
2189 tf_warning_or_error
);
2190 if (!real_lvalue_p (init
))
2191 eltinit
= move (eltinit
);
2192 eltinit
= force_rvalue (eltinit
, tf_warning_or_error
);
2193 eltinit
= (cxx_eval_constant_expression
2194 (&new_ctx
, eltinit
, lval
,
2195 non_constant_p
, overflow_p
));
2197 if (*non_constant_p
&& !ctx
->quiet
)
2199 if (new_ctx
.ctor
!= ctx
->ctor
)
2201 /* We appended this element above; update the value. */
2202 gcc_assert ((*p
)->last().index
== idx
);
2203 (*p
)->last().value
= eltinit
;
2206 CONSTRUCTOR_APPEND_ELT (*p
, idx
, eltinit
);
2209 if (!*non_constant_p
)
2212 CONSTRUCTOR_NO_IMPLICIT_ZERO (init
) = false;
2218 cxx_eval_vec_init (const constexpr_ctx
*ctx
, tree t
,
2220 bool *non_constant_p
, bool *overflow_p
)
2222 tree atype
= TREE_TYPE (t
);
2223 tree init
= VEC_INIT_EXPR_INIT (t
);
2224 tree r
= cxx_eval_vec_init_1 (ctx
, atype
, init
,
2225 VEC_INIT_EXPR_VALUE_INIT (t
),
2226 lval
, non_constant_p
, overflow_p
);
2227 if (*non_constant_p
)
2233 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
2234 match. We want to be less strict for simple *& folding; if we have a
2235 non-const temporary that we access through a const pointer, that should
2236 work. We handle this here rather than change fold_indirect_ref_1
2237 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
2238 don't really make sense outside of constant expression evaluation. Also
2239 we want to allow folding to COMPONENT_REF, which could cause trouble
2240 with TBAA in fold_indirect_ref_1.
2242 Try to keep this function synced with fold_indirect_ref_1. */
2245 cxx_fold_indirect_ref (location_t loc
, tree type
, tree op0
, bool *empty_base
)
2251 subtype
= TREE_TYPE (sub
);
2252 if (!POINTER_TYPE_P (subtype
))
2255 if (TREE_CODE (sub
) == ADDR_EXPR
)
2257 tree op
= TREE_OPERAND (sub
, 0);
2258 tree optype
= TREE_TYPE (op
);
2260 /* *&CONST_DECL -> to the value of the const decl. */
2261 if (TREE_CODE (op
) == CONST_DECL
)
2262 return DECL_INITIAL (op
);
2263 /* *&p => p; make sure to handle *&"str"[cst] here. */
2264 if (same_type_ignoring_top_level_qualifiers_p (optype
, type
))
2266 tree fop
= fold_read_from_constant_string (op
);
2272 /* *(foo *)&fooarray => fooarray[0] */
2273 else if (TREE_CODE (optype
) == ARRAY_TYPE
2274 && (same_type_ignoring_top_level_qualifiers_p
2275 (type
, TREE_TYPE (optype
))))
2277 tree type_domain
= TYPE_DOMAIN (optype
);
2278 tree min_val
= size_zero_node
;
2279 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
2280 min_val
= TYPE_MIN_VALUE (type_domain
);
2281 return build4_loc (loc
, ARRAY_REF
, type
, op
, min_val
,
2282 NULL_TREE
, NULL_TREE
);
2284 /* *(foo *)&complexfoo => __real__ complexfoo */
2285 else if (TREE_CODE (optype
) == COMPLEX_TYPE
2286 && (same_type_ignoring_top_level_qualifiers_p
2287 (type
, TREE_TYPE (optype
))))
2288 return fold_build1_loc (loc
, REALPART_EXPR
, type
, op
);
2289 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
2290 else if (TREE_CODE (optype
) == VECTOR_TYPE
2291 && (same_type_ignoring_top_level_qualifiers_p
2292 (type
, TREE_TYPE (optype
))))
2294 tree part_width
= TYPE_SIZE (type
);
2295 tree index
= bitsize_int (0);
2296 return fold_build3_loc (loc
, BIT_FIELD_REF
, type
, op
, part_width
, index
);
2298 /* Also handle conversion to an empty base class, which
2299 is represented with a NOP_EXPR. */
2300 else if (is_empty_class (type
)
2301 && CLASS_TYPE_P (optype
)
2302 && DERIVED_FROM_P (type
, optype
))
2307 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
2308 else if (RECORD_OR_UNION_TYPE_P (optype
))
2310 tree field
= TYPE_FIELDS (optype
);
2311 for (; field
; field
= DECL_CHAIN (field
))
2312 if (TREE_CODE (field
) == FIELD_DECL
2313 && integer_zerop (byte_position (field
))
2314 && (same_type_ignoring_top_level_qualifiers_p
2315 (TREE_TYPE (field
), type
)))
2317 return fold_build3 (COMPONENT_REF
, type
, op
, field
, NULL_TREE
);
2322 else if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
2323 && TREE_CODE (TREE_OPERAND (sub
, 1)) == INTEGER_CST
)
2325 tree op00
= TREE_OPERAND (sub
, 0);
2326 tree op01
= TREE_OPERAND (sub
, 1);
2329 if (TREE_CODE (op00
) == ADDR_EXPR
)
2332 op00
= TREE_OPERAND (op00
, 0);
2333 op00type
= TREE_TYPE (op00
);
2335 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
2336 if (TREE_CODE (op00type
) == VECTOR_TYPE
2337 && (same_type_ignoring_top_level_qualifiers_p
2338 (type
, TREE_TYPE (op00type
))))
2340 HOST_WIDE_INT offset
= tree_to_shwi (op01
);
2341 tree part_width
= TYPE_SIZE (type
);
2342 unsigned HOST_WIDE_INT part_widthi
= tree_to_shwi (part_width
)/BITS_PER_UNIT
;
2343 unsigned HOST_WIDE_INT indexi
= offset
* BITS_PER_UNIT
;
2344 tree index
= bitsize_int (indexi
);
2346 if (offset
/ part_widthi
< TYPE_VECTOR_SUBPARTS (op00type
))
2347 return fold_build3_loc (loc
,
2348 BIT_FIELD_REF
, type
, op00
,
2352 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
2353 else if (TREE_CODE (op00type
) == COMPLEX_TYPE
2354 && (same_type_ignoring_top_level_qualifiers_p
2355 (type
, TREE_TYPE (op00type
))))
2357 tree size
= TYPE_SIZE_UNIT (type
);
2358 if (tree_int_cst_equal (size
, op01
))
2359 return fold_build1_loc (loc
, IMAGPART_EXPR
, type
, op00
);
2361 /* ((foo *)&fooarray)[1] => fooarray[1] */
2362 else if (TREE_CODE (op00type
) == ARRAY_TYPE
2363 && (same_type_ignoring_top_level_qualifiers_p
2364 (type
, TREE_TYPE (op00type
))))
2366 tree type_domain
= TYPE_DOMAIN (op00type
);
2367 tree min_val
= size_zero_node
;
2368 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
2369 min_val
= TYPE_MIN_VALUE (type_domain
);
2370 op01
= size_binop_loc (loc
, EXACT_DIV_EXPR
, op01
,
2371 TYPE_SIZE_UNIT (type
));
2372 op01
= size_binop_loc (loc
, PLUS_EXPR
, op01
, min_val
);
2373 return build4_loc (loc
, ARRAY_REF
, type
, op00
, op01
,
2374 NULL_TREE
, NULL_TREE
);
2376 /* Also handle conversion to an empty base class, which
2377 is represented with a NOP_EXPR. */
2378 else if (is_empty_class (type
)
2379 && CLASS_TYPE_P (op00type
)
2380 && DERIVED_FROM_P (type
, op00type
))
2385 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
2386 else if (RECORD_OR_UNION_TYPE_P (op00type
))
2388 tree field
= TYPE_FIELDS (op00type
);
2389 for (; field
; field
= DECL_CHAIN (field
))
2390 if (TREE_CODE (field
) == FIELD_DECL
2391 && tree_int_cst_equal (byte_position (field
), op01
)
2392 && (same_type_ignoring_top_level_qualifiers_p
2393 (TREE_TYPE (field
), type
)))
2395 return fold_build3 (COMPONENT_REF
, type
, op00
,
2402 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
2403 else if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
2404 && (same_type_ignoring_top_level_qualifiers_p
2405 (type
, TREE_TYPE (TREE_TYPE (subtype
)))))
2408 tree min_val
= size_zero_node
;
2409 tree newsub
= cxx_fold_indirect_ref (loc
, TREE_TYPE (subtype
), sub
, NULL
);
2413 sub
= build1_loc (loc
, INDIRECT_REF
, TREE_TYPE (subtype
), sub
);
2414 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
2415 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
2416 min_val
= TYPE_MIN_VALUE (type_domain
);
2417 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
2425 cxx_eval_indirect_ref (const constexpr_ctx
*ctx
, tree t
,
2427 bool *non_constant_p
, bool *overflow_p
)
2429 tree orig_op0
= TREE_OPERAND (t
, 0);
2430 tree op0
= cxx_eval_constant_expression (ctx
, orig_op0
,
2431 /*lval*/false, non_constant_p
,
2433 bool empty_base
= false;
2436 /* Don't VERIFY_CONSTANT here. */
2437 if (*non_constant_p
)
2440 r
= cxx_fold_indirect_ref (EXPR_LOCATION (t
), TREE_TYPE (t
), op0
,
2444 r
= cxx_eval_constant_expression (ctx
, r
,
2445 lval
, non_constant_p
, overflow_p
);
2450 if (TREE_CODE (sub
) == ADDR_EXPR
)
2452 /* We couldn't fold to a constant value. Make sure it's not
2453 something we should have been able to fold. */
2454 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
2455 (TREE_TYPE (TREE_TYPE (sub
)), TREE_TYPE (t
)));
2456 /* DR 1188 says we don't have to deal with this. */
2458 error ("accessing value of %qE through a %qT glvalue in a "
2459 "constant expression", build_fold_indirect_ref (sub
),
2461 *non_constant_p
= true;
2466 /* If we're pulling out the value of an empty base, make sure
2467 that the whole object is constant and then return an empty
2469 if (empty_base
&& !lval
)
2471 VERIFY_CONSTANT (r
);
2472 r
= build_constructor (TREE_TYPE (t
), NULL
);
2473 TREE_CONSTANT (r
) = true;
2478 if (lval
&& op0
!= orig_op0
)
2479 return build1 (INDIRECT_REF
, TREE_TYPE (t
), op0
);
2481 VERIFY_CONSTANT (t
);
2487 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
2488 Shared between potential_constant_expression and
2489 cxx_eval_constant_expression. */
2492 non_const_var_error (tree r
)
2494 tree type
= TREE_TYPE (r
);
2495 error ("the value of %qD is not usable in a constant "
2497 /* Avoid error cascade. */
2498 if (DECL_INITIAL (r
) == error_mark_node
)
2500 if (DECL_DECLARED_CONSTEXPR_P (r
))
2501 inform (DECL_SOURCE_LOCATION (r
),
2502 "%qD used in its own initializer", r
);
2503 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
2505 if (!CP_TYPE_CONST_P (type
))
2506 inform (DECL_SOURCE_LOCATION (r
),
2507 "%q#D is not const", r
);
2508 else if (CP_TYPE_VOLATILE_P (type
))
2509 inform (DECL_SOURCE_LOCATION (r
),
2510 "%q#D is volatile", r
);
2511 else if (!DECL_INITIAL (r
)
2512 || !TREE_CONSTANT (DECL_INITIAL (r
)))
2513 inform (DECL_SOURCE_LOCATION (r
),
2514 "%qD was not initialized with a constant "
2521 if (cxx_dialect
>= cxx11
&& !DECL_DECLARED_CONSTEXPR_P (r
))
2522 inform (DECL_SOURCE_LOCATION (r
),
2523 "%qD was not declared %<constexpr%>", r
);
2525 inform (DECL_SOURCE_LOCATION (r
),
2526 "%qD does not have integral or enumeration type",
2531 /* Subroutine of cxx_eval_constant_expression.
2532 Like cxx_eval_unary_expression, except for trinary expressions. */
2535 cxx_eval_trinary_expression (const constexpr_ctx
*ctx
, tree t
,
2537 bool *non_constant_p
, bool *overflow_p
)
2543 for (i
= 0; i
< 3; i
++)
2545 args
[i
] = cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, i
),
2547 non_constant_p
, overflow_p
);
2548 VERIFY_CONSTANT (args
[i
]);
2551 val
= fold_ternary_loc (EXPR_LOCATION (t
), TREE_CODE (t
), TREE_TYPE (t
),
2552 args
[0], args
[1], args
[2]);
2553 if (val
== NULL_TREE
)
2555 VERIFY_CONSTANT (val
);
2560 var_in_constexpr_fn (tree t
)
2562 tree ctx
= DECL_CONTEXT (t
);
2563 return (cxx_dialect
>= cxx14
&& ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
2564 && DECL_DECLARED_CONSTEXPR_P (ctx
));
2567 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
2570 cxx_eval_store_expression (const constexpr_ctx
*ctx
, tree t
,
2572 bool *non_constant_p
, bool *overflow_p
)
2574 constexpr_ctx new_ctx
= *ctx
;
2576 /* First we figure out where we're storing to. */
2577 tree target
= TREE_OPERAND (t
, 0);
2578 target
= cxx_eval_constant_expression (ctx
, target
,
2580 non_constant_p
, overflow_p
);
2581 if (*non_constant_p
)
2584 /* And then find the underlying variable. */
2585 vec
<tree
,va_gc
> *refs
= make_tree_vector();
2586 tree object
= NULL_TREE
;
2587 for (tree probe
= target
; object
== NULL_TREE
; )
2589 switch (TREE_CODE (probe
))
2594 vec_safe_push (refs
, TREE_OPERAND (probe
, 1));
2595 vec_safe_push (refs
, TREE_TYPE (probe
));
2596 probe
= TREE_OPERAND (probe
, 0);
2604 /* And then find/build up our initializer for the path to the subobject
2605 we're initializing. */
2607 if (DECL_P (object
))
2608 valp
= ctx
->values
->get (object
);
2613 /* A constant-expression cannot modify objects from outside the
2614 constant-expression. */
2616 error ("modification of %qE is not a constant-expression", object
);
2617 *non_constant_p
= true;
2620 tree type
= TREE_TYPE (object
);
2621 while (!refs
->is_empty())
2623 if (*valp
== NULL_TREE
)
2625 *valp
= build_constructor (type
, NULL
);
2626 CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp
) = true;
2631 ce
.index
= refs
->pop();
2632 ce
.value
= NULL_TREE
;
2634 unsigned HOST_WIDE_INT idx
= 0;
2635 constructor_elt
*cep
= NULL
;
2637 vec_safe_iterate (CONSTRUCTOR_ELTS (*valp
), idx
, &cep
);
2640 if (cp_tree_equal (ce
.index
, cep
->index
))
2643 cep
= vec_safe_push (CONSTRUCTOR_ELTS (*valp
), ce
);
2646 release_tree_vector (refs
);
2648 if ((AGGREGATE_TYPE_P (TREE_TYPE (t
)) || VECTOR_TYPE_P (TREE_TYPE (t
))))
2650 /* Create a new CONSTRUCTOR in case evaluation of the initializer
2651 wants to modify it. */
2652 *valp
= new_ctx
.ctor
= build_constructor (TREE_TYPE (t
), NULL
);
2653 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx
.ctor
) = true;
2654 new_ctx
.object
= target
;
2657 tree init
= cxx_eval_constant_expression (&new_ctx
, TREE_OPERAND (t
, 1),
2659 non_constant_p
, overflow_p
);
2660 if (target
== object
)
2661 /* The hash table might have moved since the get earlier. */
2662 ctx
->values
->put (object
, init
);
2666 if (*non_constant_p
)
2674 /* Evaluate a ++ or -- expression. */
2677 cxx_eval_increment_expression (const constexpr_ctx
*ctx
, tree t
,
2679 bool *non_constant_p
, bool *overflow_p
)
2681 enum tree_code code
= TREE_CODE (t
);
2682 tree type
= TREE_TYPE (t
);
2683 tree op
= TREE_OPERAND (t
, 0);
2684 tree offset
= TREE_OPERAND (t
, 1);
2685 gcc_assert (TREE_CONSTANT (offset
));
2687 /* The operand as an lvalue. */
2688 op
= cxx_eval_constant_expression (ctx
, op
, true,
2689 non_constant_p
, overflow_p
);
2691 /* The operand as an rvalue. */
2692 tree val
= rvalue (op
);
2693 val
= cxx_eval_constant_expression (ctx
, val
, false,
2694 non_constant_p
, overflow_p
);
2695 VERIFY_CONSTANT (val
);
2697 /* The modified value. */
2698 bool inc
= (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
);
2700 if (POINTER_TYPE_P (type
))
2702 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
2703 offset
= convert_to_ptrofftype (offset
);
2705 offset
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (offset
), offset
);
2706 mod
= fold_build2 (POINTER_PLUS_EXPR
, type
, val
, offset
);
2709 mod
= fold_build2 (inc
? PLUS_EXPR
: MINUS_EXPR
, type
, val
, offset
);
2710 VERIFY_CONSTANT (mod
);
2712 /* Storing the modified value. */
2713 tree store
= build2 (MODIFY_EXPR
, type
, op
, mod
);
2714 cxx_eval_constant_expression (ctx
, store
,
2715 true, non_constant_p
, overflow_p
);
2717 /* And the value of the expression. */
2718 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
2720 /* Prefix ops are lvalues. */
2724 /* But we optimize when the caller wants an rvalue. */
2728 /* Postfix ops are rvalues. */
2732 /* Predicates for the meaning of *jump_target. */
2735 returns (tree
*jump_target
)
2738 && TREE_CODE (*jump_target
) == RETURN_EXPR
;
2742 breaks (tree
*jump_target
)
2745 && TREE_CODE (*jump_target
) == LABEL_DECL
2746 && LABEL_DECL_BREAK (*jump_target
);
2750 continues (tree
*jump_target
)
2753 && TREE_CODE (*jump_target
) == LABEL_DECL
2754 && LABEL_DECL_CONTINUE (*jump_target
);
2758 switches (tree
*jump_target
)
2761 && TREE_CODE (*jump_target
) == INTEGER_CST
;
2764 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
2765 at I matches *jump_target. If we're looking for a case label and we see
2766 the default label, copy I into DEFAULT_LABEL. */
2769 label_matches (tree
*jump_target
, tree_stmt_iterator i
,
2770 tree_stmt_iterator
& default_label
)
2772 tree stmt
= tsi_stmt (i
);
2773 switch (TREE_CODE (*jump_target
))
2776 if (TREE_CODE (stmt
) == LABEL_EXPR
2777 && LABEL_EXPR_LABEL (stmt
) == *jump_target
)
2782 if (TREE_CODE (stmt
) == CASE_LABEL_EXPR
)
2784 if (!CASE_LOW (stmt
))
2786 else if (tree_int_cst_equal (*jump_target
, CASE_LOW (stmt
)))
2797 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
2798 semantics, for switch, break, continue, and return. */
2801 cxx_eval_statement_list (const constexpr_ctx
*ctx
, tree t
,
2802 bool *non_constant_p
, bool *overflow_p
,
2805 tree_stmt_iterator i
;
2806 tree_stmt_iterator default_label
= tree_stmt_iterator();
2808 /* In a statement-expression we want to return the last value. */
2812 local_target
= NULL_TREE
;
2813 jump_target
= &local_target
;
2815 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
2818 tree stmt
= tsi_stmt (i
);
2821 if (TREE_CODE (stmt
) == STATEMENT_LIST
)
2822 /* The label we want might be inside. */;
2823 else if (label_matches (jump_target
, i
, default_label
))
2825 *jump_target
= NULL_TREE
;
2829 r
= cxx_eval_constant_expression (ctx
, stmt
, false,
2830 non_constant_p
, overflow_p
,
2832 if (*non_constant_p
)
2834 if (returns (jump_target
) || breaks (jump_target
))
2837 if (switches (jump_target
) && !tsi_end_p (default_label
))
2840 *jump_target
= NULL_TREE
;
2846 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
2847 semantics; continue semantics are covered by cxx_eval_statement_list. */
2850 cxx_eval_loop_expr (const constexpr_ctx
*ctx
, tree t
,
2851 bool *non_constant_p
, bool *overflow_p
,
2854 tree body
= TREE_OPERAND (t
, 0);
2857 cxx_eval_statement_list (ctx
, body
,
2858 non_constant_p
, overflow_p
, jump_target
);
2859 if (returns (jump_target
) || breaks (jump_target
) || *non_constant_p
)
2862 if (breaks (jump_target
))
2863 *jump_target
= NULL_TREE
;
2867 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
2871 cxx_eval_switch_expr (const constexpr_ctx
*ctx
, tree t
,
2872 bool *non_constant_p
, bool *overflow_p
,
2875 tree cond
= TREE_OPERAND (t
, 0);
2876 cond
= cxx_eval_constant_expression (ctx
, cond
, false,
2877 non_constant_p
, overflow_p
);
2878 VERIFY_CONSTANT (cond
);
2879 *jump_target
= cond
;
2881 tree body
= TREE_OPERAND (t
, 1);
2882 cxx_eval_statement_list (ctx
, body
,
2883 non_constant_p
, overflow_p
, jump_target
);
2884 if (breaks (jump_target
) || switches (jump_target
))
2885 *jump_target
= NULL_TREE
;
2889 /* Attempt to reduce the expression T to a constant value.
2890 On failure, issue diagnostic and return error_mark_node. */
2891 /* FIXME unify with c_fully_fold */
2892 /* FIXME overflow_p is too global */
2895 cxx_eval_constant_expression (const constexpr_ctx
*ctx
, tree t
,
2897 bool *non_constant_p
, bool *overflow_p
,
2900 constexpr_ctx new_ctx
;
2903 if (t
== error_mark_node
)
2905 *non_constant_p
= true;
2908 if (CONSTANT_CLASS_P (t
))
2910 if (TREE_CODE (t
) == PTRMEM_CST
)
2911 t
= cplus_expand_constant (t
);
2912 else if (TREE_OVERFLOW (t
) && (!flag_permissive
|| ctx
->quiet
))
2916 if (TREE_CODE (t
) != NOP_EXPR
2917 && reduced_constant_expression_p (t
))
2920 switch (TREE_CODE (t
))
2925 /* We ask for an rvalue for the RESULT_DECL when indirecting
2926 through an invisible reference. */
2927 gcc_assert (DECL_BY_REFERENCE (t
));
2928 return (*ctx
->values
->get (t
));
2933 /* else fall through. */
2936 r
= decl_really_constant_value (t
);
2938 r
= decl_constant_value (t
);
2939 if (TREE_CODE (r
) == TARGET_EXPR
2940 && TREE_CODE (TARGET_EXPR_INITIAL (r
)) == CONSTRUCTOR
)
2941 r
= TARGET_EXPR_INITIAL (r
);
2942 if (TREE_CODE (r
) == VAR_DECL
)
2943 if (tree
*p
= ctx
->values
->get (r
))
2948 non_const_var_error (r
);
2949 *non_constant_p
= true;
2957 case CASE_LABEL_EXPR
:
2961 if (!use_new_call
&& ctx
2962 && ctx
->call
&& DECL_CONTEXT (t
) == ctx
->call
->fundef
->decl
)
2963 r
= lookup_parameter_binding (ctx
->call
, t
);
2964 else if (lval
&& TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
2966 else if (tree
*p
= ctx
->values
->get (r
))
2969 /* Defer in case this is only used for its type. */;
2970 else if (TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
)
2971 /* Defer, there's no lvalue->rvalue conversion. */;
2972 else if (is_empty_class (TREE_TYPE (t
)))
2974 /* If the class is empty, we aren't actually loading anything. */
2975 r
= build_constructor (TREE_TYPE (t
), NULL
);
2976 TREE_CONSTANT (r
) = true;
2981 error ("%qE is not a constant expression", t
);
2982 *non_constant_p
= true;
2987 case AGGR_INIT_EXPR
:
2988 r
= cxx_eval_call_expression (ctx
, t
, lval
,
2989 non_constant_p
, overflow_p
);
2994 r
= DECL_EXPR_DECL (t
);
2995 if (AGGREGATE_TYPE_P (TREE_TYPE (r
))
2996 || VECTOR_TYPE_P (TREE_TYPE (r
)))
3000 new_ctx
.ctor
= build_constructor (TREE_TYPE (r
), NULL
);
3001 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx
.ctor
) = true;
3002 new_ctx
.values
->put (r
, new_ctx
.ctor
);
3006 if (tree init
= DECL_INITIAL (r
))
3008 init
= cxx_eval_constant_expression (ctx
, init
,
3010 non_constant_p
, overflow_p
);
3011 ctx
->values
->put (r
, init
);
3013 else if (ctx
== &new_ctx
)
3014 /* We gave it a CONSTRUCTOR above. */;
3016 ctx
->values
->put (r
, NULL_TREE
);
3021 if (!literal_type_p (TREE_TYPE (t
)))
3025 error ("temporary of non-literal type %qT in a "
3026 "constant expression", TREE_TYPE (t
));
3027 explain_non_literal_class (TREE_TYPE (t
));
3029 *non_constant_p
= true;
3032 if ((AGGREGATE_TYPE_P (TREE_TYPE (t
)) || VECTOR_TYPE_P (TREE_TYPE (t
))))
3034 /* We're being expanded without an explicit target, so start
3035 initializing a new object; expansion with an explicit target
3036 strips the TARGET_EXPR before we get here. */
3038 new_ctx
.ctor
= build_constructor (TREE_TYPE (t
), NULL
);
3039 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx
.ctor
) = true;
3040 new_ctx
.object
= TARGET_EXPR_SLOT (t
);
3041 ctx
->values
->put (new_ctx
.object
, new_ctx
.ctor
);
3044 /* Pass false for 'lval' because this indicates
3045 initialization of a temporary. */
3046 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
3048 non_constant_p
, overflow_p
);
3049 if (!*non_constant_p
)
3050 /* Adjust the type of the result to the type of the temporary. */
3051 r
= adjust_temp_type (TREE_TYPE (t
), r
);
3054 tree slot
= TARGET_EXPR_SLOT (t
);
3055 ctx
->values
->put (slot
, r
);
3063 /* In C++11 constexpr evaluation we are looking for the value,
3064 not the side-effect of the initialization. */
3065 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
3067 non_constant_p
, overflow_p
);
3070 /* else fall through */
3072 r
= cxx_eval_store_expression (ctx
, t
, lval
,
3073 non_constant_p
, overflow_p
);
3077 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
3079 non_constant_p
, overflow_p
);
3083 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3085 non_constant_p
, overflow_p
);
3090 /* Avoid evaluating a SAVE_EXPR more than once. */
3091 if (tree
*p
= ctx
->values
->get (t
))
3095 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), false,
3096 non_constant_p
, overflow_p
);
3097 ctx
->values
->put (t
, r
);
3101 case NON_LVALUE_EXPR
:
3102 case TRY_CATCH_EXPR
:
3103 case CLEANUP_POINT_EXPR
:
3104 case MUST_NOT_THROW_EXPR
:
3107 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3109 non_constant_p
, overflow_p
,
3113 /* These differ from cxx_eval_unary_expression in that this doesn't
3114 check for a constant operand or result; an address can be
3115 constant without its operand being, and vice versa. */
3117 r
= cxx_eval_indirect_ref (ctx
, t
, lval
,
3118 non_constant_p
, overflow_p
);
3123 tree oldop
= TREE_OPERAND (t
, 0);
3124 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
3126 non_constant_p
, overflow_p
);
3127 /* Don't VERIFY_CONSTANT here. */
3128 if (*non_constant_p
)
3130 gcc_checking_assert (TREE_CODE (op
) != CONSTRUCTOR
);
3131 /* This function does more aggressive folding than fold itself. */
3132 r
= build_fold_addr_expr_with_type (op
, TREE_TYPE (t
));
3133 if (TREE_CODE (r
) == ADDR_EXPR
&& TREE_OPERAND (r
, 0) == oldop
)
3141 case FIX_TRUNC_EXPR
:
3146 case TRUTH_NOT_EXPR
:
3147 case FIXED_CONVERT_EXPR
:
3148 r
= cxx_eval_unary_expression (ctx
, t
, lval
,
3149 non_constant_p
, overflow_p
);
3153 if (SIZEOF_EXPR_TYPE_P (t
))
3154 r
= cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t
, 0)),
3155 SIZEOF_EXPR
, false);
3156 else if (TYPE_P (TREE_OPERAND (t
, 0)))
3157 r
= cxx_sizeof_or_alignof_type (TREE_OPERAND (t
, 0), SIZEOF_EXPR
,
3160 r
= cxx_sizeof_or_alignof_expr (TREE_OPERAND (t
, 0), SIZEOF_EXPR
,
3162 if (r
== error_mark_node
)
3164 VERIFY_CONSTANT (r
);
3169 /* check_return_expr sometimes wraps a TARGET_EXPR in a
3170 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
3171 introduced by build_call_a. */
3172 tree op0
= TREE_OPERAND (t
, 0);
3173 tree op1
= TREE_OPERAND (t
, 1);
3175 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
3176 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
3177 r
= cxx_eval_constant_expression (ctx
, op0
,
3178 lval
, non_constant_p
, overflow_p
,
3182 /* Check that the LHS is constant and then discard it. */
3183 cxx_eval_constant_expression (ctx
, op0
,
3184 true, non_constant_p
, overflow_p
,
3186 op1
= TREE_OPERAND (t
, 1);
3187 r
= cxx_eval_constant_expression (ctx
, op1
,
3188 lval
, non_constant_p
, overflow_p
,
3194 case POINTER_PLUS_EXPR
:
3198 case TRUNC_DIV_EXPR
:
3200 case FLOOR_DIV_EXPR
:
3201 case ROUND_DIV_EXPR
:
3202 case TRUNC_MOD_EXPR
:
3204 case ROUND_MOD_EXPR
:
3206 case EXACT_DIV_EXPR
:
3216 case TRUTH_XOR_EXPR
:
3223 case UNORDERED_EXPR
:
3233 r
= cxx_eval_binary_expression (ctx
, t
, lval
,
3234 non_constant_p
, overflow_p
);
3237 /* fold can introduce non-IF versions of these; still treat them as
3238 short-circuiting. */
3239 case TRUTH_AND_EXPR
:
3240 case TRUTH_ANDIF_EXPR
:
3241 r
= cxx_eval_logical_expression (ctx
, t
, boolean_false_node
,
3244 non_constant_p
, overflow_p
);
3248 case TRUTH_ORIF_EXPR
:
3249 r
= cxx_eval_logical_expression (ctx
, t
, boolean_true_node
,
3252 non_constant_p
, overflow_p
);
3256 r
= cxx_eval_array_reference (ctx
, t
, lval
,
3257 non_constant_p
, overflow_p
);
3261 if (is_overloaded_fn (t
))
3263 /* We can only get here in checking mode via
3264 build_non_dependent_expr, because any expression that
3265 calls or takes the address of the function will have
3266 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
3267 gcc_checking_assert (ctx
->quiet
|| errorcount
);
3268 *non_constant_p
= true;
3271 r
= cxx_eval_component_reference (ctx
, t
, lval
,
3272 non_constant_p
, overflow_p
);
3276 r
= cxx_eval_bit_field_ref (ctx
, t
, lval
,
3277 non_constant_p
, overflow_p
);
3282 r
= cxx_eval_conditional_expression (ctx
, t
, lval
,
3283 non_constant_p
, overflow_p
,
3288 r
= cxx_eval_bare_aggregate (ctx
, t
, lval
,
3289 non_constant_p
, overflow_p
);
3293 /* We can get this in a defaulted constructor for a class with a
3294 non-static data member of array type. Either the initializer will
3295 be NULL, meaning default-initialization, or it will be an lvalue
3296 or xvalue of the same type, meaning direct-initialization from the
3297 corresponding member. */
3298 r
= cxx_eval_vec_init (ctx
, t
, lval
,
3299 non_constant_p
, overflow_p
);
3304 r
= cxx_eval_trinary_expression (ctx
, t
, lval
,
3305 non_constant_p
, overflow_p
);
3309 case VIEW_CONVERT_EXPR
:
3312 tree oldop
= TREE_OPERAND (t
, 0);
3313 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
3315 non_constant_p
, overflow_p
);
3316 if (*non_constant_p
)
3318 if (POINTER_TYPE_P (TREE_TYPE (t
))
3319 && TREE_CODE (op
) == INTEGER_CST
3320 && !integer_zerop (op
))
3323 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3324 "reinterpret_cast from integer to pointer");
3325 *non_constant_p
= true;
3329 /* We didn't fold at the top so we could check for ptr-int
3332 r
= fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), op
);
3333 /* Conversion of an out-of-range value has implementation-defined
3334 behavior; the language considers it different from arithmetic
3335 overflow, which is undefined. */
3336 if (TREE_OVERFLOW_P (r
) && !TREE_OVERFLOW_P (op
))
3337 TREE_OVERFLOW (r
) = false;
3341 case EMPTY_CLASS_EXPR
:
3342 /* This is good enough for a function argument that might not get
3343 used, and they can't do anything with it, so just return it. */
3346 case STATEMENT_LIST
:
3348 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
3349 return cxx_eval_statement_list (&new_ctx
, t
,
3350 non_constant_p
, overflow_p
, jump_target
);
3353 return cxx_eval_constant_expression (ctx
, BIND_EXPR_BODY (t
),
3355 non_constant_p
, overflow_p
,
3358 case PREINCREMENT_EXPR
:
3359 case POSTINCREMENT_EXPR
:
3360 case PREDECREMENT_EXPR
:
3361 case POSTDECREMENT_EXPR
:
3362 return cxx_eval_increment_expression (ctx
, t
,
3363 lval
, non_constant_p
, overflow_p
);
3369 case VEC_DELETE_EXPR
:
3372 /* GCC internal stuff. */
3375 case WITH_CLEANUP_EXPR
:
3376 case NON_DEPENDENT_EXPR
:
3380 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3381 "expression %qE is not a constant-expression", t
);
3382 *non_constant_p
= true;
3385 case PLACEHOLDER_EXPR
:
3386 if (!ctx
|| !ctx
->ctor
|| (lval
&& !ctx
->object
))
3388 /* A placeholder without a referent. We can get here when
3389 checking whether NSDMIs are noexcept, or in massage_init_elt;
3390 just say it's non-constant for now. */
3391 gcc_assert (ctx
->quiet
);
3392 *non_constant_p
= true;
3397 /* Use of the value or address of the current object. We could
3398 use ctx->object unconditionally, but using ctx->ctor when we
3399 can is a minor optimization. */
3400 tree ctor
= lval
? ctx
->object
: ctx
->ctor
;
3401 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3402 (TREE_TYPE (t
), TREE_TYPE (ctor
)));
3403 return cxx_eval_constant_expression
3405 non_constant_p
, overflow_p
);
3410 *jump_target
= TREE_OPERAND (t
, 0);
3411 gcc_assert (breaks (jump_target
) || continues (jump_target
));
3415 cxx_eval_loop_expr (ctx
, t
,
3416 non_constant_p
, overflow_p
, jump_target
);
3420 cxx_eval_switch_expr (ctx
, t
,
3421 non_constant_p
, overflow_p
, jump_target
);
3425 internal_error ("unexpected expression %qE of kind %s", t
,
3426 get_tree_code_name (TREE_CODE (t
)));
3427 *non_constant_p
= true;
3431 if (r
== error_mark_node
)
3432 *non_constant_p
= true;
3434 if (*non_constant_p
)
3441 cxx_eval_outermost_constant_expr (tree t
, bool allow_non_constant
,
3442 bool strict
= true, tree object
= NULL_TREE
)
3444 bool non_constant_p
= false;
3445 bool overflow_p
= false;
3446 hash_map
<tree
,tree
> map
;
3447 constexpr_ctx ctx
= { NULL
, &map
, NULL
, NULL
, allow_non_constant
, strict
};
3448 tree type
= initialized_type (t
);
3450 if (AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
))
3452 /* In C++14 an NSDMI can participate in aggregate initialization,
3453 and can refer to the address of the object being initialized, so
3454 we need to pass in the relevant VAR_DECL if we want to do the
3455 evaluation in a single pass. The evaluation will dynamically
3456 update ctx.values for the VAR_DECL. We use the same strategy
3457 for C++11 constexpr constructors that refer to the object being
3459 ctx
.ctor
= build_constructor (type
, NULL
);
3460 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctx
.ctor
) = true;
3463 if (TREE_CODE (t
) == TARGET_EXPR
)
3464 object
= TARGET_EXPR_SLOT (t
);
3465 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
3466 object
= AGGR_INIT_EXPR_SLOT (t
);
3468 ctx
.object
= object
;
3470 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3471 (type
, TREE_TYPE (object
)));
3472 if (object
&& DECL_P (object
))
3473 map
.put (object
, ctx
.ctor
);
3474 if (TREE_CODE (r
) == TARGET_EXPR
)
3475 /* Avoid creating another CONSTRUCTOR when we expand the
3477 r
= TARGET_EXPR_INITIAL (r
);
3480 r
= cxx_eval_constant_expression (&ctx
, r
,
3481 false, &non_constant_p
, &overflow_p
);
3483 verify_constant (r
, allow_non_constant
, &non_constant_p
, &overflow_p
);
3485 /* Mutable logic is a bit tricky: we want to allow initialization of
3486 constexpr variables with mutable members, but we can't copy those
3487 members to another constexpr variable. */
3488 if (TREE_CODE (r
) == CONSTRUCTOR
3489 && CONSTRUCTOR_MUTABLE_POISON (r
))
3491 if (!allow_non_constant
)
3492 error ("%qE is not a constant expression because it refers to "
3493 "mutable subobjects of %qT", t
, type
);
3494 non_constant_p
= true;
3497 /* Technically we should check this for all subexpressions, but that
3498 runs into problems with our internal representation of pointer
3499 subtraction and the 5.19 rules are still in flux. */
3500 if (CONVERT_EXPR_CODE_P (TREE_CODE (r
))
3501 && ARITHMETIC_TYPE_P (TREE_TYPE (r
))
3502 && TREE_CODE (TREE_OPERAND (r
, 0)) == ADDR_EXPR
)
3504 if (!allow_non_constant
)
3505 error ("conversion from pointer type %qT "
3506 "to arithmetic type %qT in a constant-expression",
3507 TREE_TYPE (TREE_OPERAND (r
, 0)), TREE_TYPE (r
));
3508 non_constant_p
= true;
3511 if (!non_constant_p
&& overflow_p
)
3512 non_constant_p
= true;
3514 if (non_constant_p
&& !allow_non_constant
)
3515 return error_mark_node
;
3516 else if (non_constant_p
&& TREE_CONSTANT (r
))
3518 /* This isn't actually constant, so unset TREE_CONSTANT. */
3521 else if (TREE_CODE (r
) == CONSTRUCTOR
)
3522 r
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (r
), r
);
3524 r
= build_nop (TREE_TYPE (r
), r
);
3525 TREE_CONSTANT (r
) = false;
3527 else if (non_constant_p
|| r
== t
)
3530 if (TREE_CODE (r
) == CONSTRUCTOR
&& CLASS_TYPE_P (TREE_TYPE (r
)))
3532 if (TREE_CODE (t
) == TARGET_EXPR
3533 && TARGET_EXPR_INITIAL (t
) == r
)
3537 r
= get_target_expr (r
);
3538 TREE_CONSTANT (r
) = true;
3546 /* Returns true if T is a valid subexpression of a constant expression,
3547 even if it isn't itself a constant expression. */
3550 is_sub_constant_expr (tree t
)
3552 bool non_constant_p
= false;
3553 bool overflow_p
= false;
3554 hash_map
<tree
, tree
> map
;
3555 constexpr_ctx ctx
= { NULL
, &map
, NULL
, NULL
, true, true };
3556 cxx_eval_constant_expression (&ctx
, t
, false, &non_constant_p
,
3558 return !non_constant_p
&& !overflow_p
;
3561 /* If T represents a constant expression returns its reduced value.
3562 Otherwise return error_mark_node. If T is dependent, then
3566 cxx_constant_value (tree t
, tree decl
)
3568 return cxx_eval_outermost_constant_expr (t
, false, true, decl
);
3571 /* If T is a constant expression, returns its reduced value.
3572 Otherwise, if T does not have TREE_CONSTANT set, returns T.
3573 Otherwise, returns a version of T without TREE_CONSTANT. */
3576 maybe_constant_value (tree t
, tree decl
)
3580 if (instantiation_dependent_expression_p (t
)
3581 || type_unknown_p (t
)
3582 || BRACE_ENCLOSED_INITIALIZER_P (t
)
3583 || !potential_constant_expression (t
))
3585 if (TREE_OVERFLOW_P (t
))
3587 t
= build_nop (TREE_TYPE (t
), t
);
3588 TREE_CONSTANT (t
) = false;
3593 r
= cxx_eval_outermost_constant_expr (t
, true, true, decl
);
3594 #ifdef ENABLE_CHECKING
3595 /* cp_tree_equal looks through NOPs, so allow them. */
3597 || CONVERT_EXPR_P (t
)
3598 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
3599 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
3600 || !cp_tree_equal (r
, t
));
3605 /* Like maybe_constant_value but first fully instantiate the argument.
3607 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
3608 (t, tf_none) followed by maybe_constant_value but is more efficient,
3609 because calls instantiation_dependent_expression_p and
3610 potential_constant_expression at most once. */
3613 fold_non_dependent_expr (tree t
)
3618 /* If we're in a template, but T isn't value dependent, simplify
3619 it. We're supposed to treat:
3621 template <typename T> void f(T[1 + 1]);
3622 template <typename T> void f(T[2]);
3624 as two declarations of the same function, for example. */
3625 if (processing_template_decl
)
3627 if (!instantiation_dependent_expression_p (t
)
3628 && potential_constant_expression (t
))
3630 processing_template_decl_sentinel s
;
3631 t
= instantiate_non_dependent_expr_internal (t
, tf_none
);
3633 if (type_unknown_p (t
)
3634 || BRACE_ENCLOSED_INITIALIZER_P (t
))
3636 if (TREE_OVERFLOW_P (t
))
3638 t
= build_nop (TREE_TYPE (t
), t
);
3639 TREE_CONSTANT (t
) = false;
3644 tree r
= cxx_eval_outermost_constant_expr (t
, true, true, NULL_TREE
);
3645 #ifdef ENABLE_CHECKING
3646 /* cp_tree_equal looks through NOPs, so allow them. */
3648 || CONVERT_EXPR_P (t
)
3649 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
3650 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
3651 || !cp_tree_equal (r
, t
));
3655 else if (TREE_OVERFLOW_P (t
))
3657 t
= build_nop (TREE_TYPE (t
), t
);
3658 TREE_CONSTANT (t
) = false;
3663 return maybe_constant_value (t
);
3666 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
3667 than wrapped in a TARGET_EXPR. */
3670 maybe_constant_init (tree t
, tree decl
)
3672 if (TREE_CODE (t
) == EXPR_STMT
)
3673 t
= TREE_OPERAND (t
, 0);
3674 if (TREE_CODE (t
) == CONVERT_EXPR
3675 && VOID_TYPE_P (TREE_TYPE (t
)))
3676 t
= TREE_OPERAND (t
, 0);
3677 if (TREE_CODE (t
) == INIT_EXPR
)
3678 t
= TREE_OPERAND (t
, 1);
3679 if (instantiation_dependent_expression_p (t
)
3680 || type_unknown_p (t
)
3681 || BRACE_ENCLOSED_INITIALIZER_P (t
)
3682 || !potential_static_init_expression (t
))
3683 /* Don't try to evaluate it. */;
3685 t
= cxx_eval_outermost_constant_expr (t
, true, false, decl
);
3686 if (TREE_CODE (t
) == TARGET_EXPR
)
3688 tree init
= TARGET_EXPR_INITIAL (t
);
3689 if (TREE_CODE (init
) == CONSTRUCTOR
)
3696 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
3697 /* Return true if the object referred to by REF has automatic or thread
3700 enum { ck_ok
, ck_bad
, ck_unknown
};
3702 check_automatic_or_tls (tree ref
)
3705 HOST_WIDE_INT bitsize
, bitpos
;
3707 int volatilep
= 0, unsignedp
= 0;
3708 tree decl
= get_inner_reference (ref
, &bitsize
, &bitpos
, &offset
,
3709 &mode
, &unsignedp
, &volatilep
, false);
3712 /* If there isn't a decl in the middle, we don't know the linkage here,
3713 and this isn't a constant expression anyway. */
3716 dk
= decl_storage_duration (decl
);
3717 return (dk
== dk_auto
|| dk
== dk_thread
) ? ck_bad
: ck_ok
;
3721 /* Return true if T denotes a potentially constant expression. Issue
3722 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
3723 an lvalue-rvalue conversion is implied.
3725 C++0x [expr.const] used to say
3727 6 An expression is a potential constant expression if it is
3728 a constant expression where all occurrences of function
3729 parameters are replaced by arbitrary constant expressions
3730 of the appropriate type.
3732 2 A conditional expression is a constant expression unless it
3733 involves one of the following as a potentially evaluated
3734 subexpression (3.2), but subexpressions of logical AND (5.14),
3735 logical OR (5.15), and conditional (5.16) operations that are
3736 not evaluated are not considered. */
3739 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
,
3740 tsubst_flags_t flags
)
3742 #define RECUR(T,RV) potential_constant_expression_1 ((T), (RV), strict, flags)
3743 enum { any
= false, rval
= true };
3747 if (t
== error_mark_node
)
3751 if (TREE_THIS_VOLATILE (t
))
3753 if (flags
& tf_error
)
3754 error ("expression %qE has side-effects", t
);
3757 if (CONSTANT_CLASS_P (t
))
3760 switch (TREE_CODE (t
))
3766 case TEMPLATE_ID_EXPR
:
3769 case CASE_LABEL_EXPR
:
3775 case TEMPLATE_PARM_INDEX
:
3777 case IDENTIFIER_NODE
:
3778 case USERDEF_LITERAL
:
3779 /* We can see a FIELD_DECL in a pointer-to-member expression. */
3784 case PLACEHOLDER_EXPR
:
3789 case AGGR_INIT_EXPR
:
3791 /* -- an invocation of a function other than a constexpr function
3792 or a constexpr constructor. */
3794 tree fun
= get_function_named_in_call (t
);
3795 const int nargs
= call_expr_nargs (t
);
3798 if (fun
== NULL_TREE
)
3800 /* fold_call_expr can't do anything with IFN calls. */
3801 if (flags
& tf_error
)
3802 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3803 "call to internal function");
3806 if (is_overloaded_fn (fun
))
3808 if (TREE_CODE (fun
) == FUNCTION_DECL
)
3810 if (builtin_valid_in_constant_expr_p (fun
))
3812 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
3813 /* Allow any built-in function; if the expansion
3814 isn't constant, we'll deal with that then. */
3815 && !is_builtin_fn (fun
))
3817 if (flags
& tf_error
)
3819 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3820 "call to non-constexpr function %qD", fun
);
3821 explain_invalid_constexpr_fn (fun
);
3825 /* A call to a non-static member function takes the address
3826 of the object as the first argument. But in a constant
3827 expression the address will be folded away, so look
3829 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
3830 && !DECL_CONSTRUCTOR_P (fun
))
3832 tree x
= get_nth_callarg (t
, 0);
3833 if (is_this_parameter (x
))
3835 else if (!RECUR (x
, rval
))
3842 if (!RECUR (fun
, true))
3844 fun
= get_first_fn (fun
);
3846 /* Skip initial arguments to base constructors. */
3847 if (DECL_BASE_CONSTRUCTOR_P (fun
))
3848 i
= num_artificial_parms_for (fun
);
3849 fun
= DECL_ORIGIN (fun
);
3853 if (RECUR (fun
, rval
))
3854 /* Might end up being a constant function pointer. */;
3858 for (; i
< nargs
; ++i
)
3860 tree x
= get_nth_callarg (t
, i
);
3861 if (!RECUR (x
, rval
))
3867 case NON_LVALUE_EXPR
:
3868 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
3869 -- an lvalue of integral type that refers to a non-volatile
3870 const variable or static data member initialized with
3871 constant expressions, or
3873 -- an lvalue of literal type that refers to non-volatile
3874 object defined with constexpr, or that refers to a
3875 sub-object of such an object; */
3876 return RECUR (TREE_OPERAND (t
, 0), rval
);
3880 && !decl_constant_var_p (t
)
3882 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t
))
3883 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t
))
3884 && !var_in_constexpr_fn (t
)
3885 && !dependent_type_p (TREE_TYPE (t
)))
3887 if (flags
& tf_error
)
3888 non_const_var_error (t
);
3895 case VIEW_CONVERT_EXPR
:
3896 /* -- a reinterpret_cast. FIXME not implemented, and this rule
3897 may change to something more specific to type-punning (DR 1312). */
3899 tree from
= TREE_OPERAND (t
, 0);
3900 if (POINTER_TYPE_P (TREE_TYPE (t
))
3901 && TREE_CODE (from
) == INTEGER_CST
3902 && !integer_zerop (from
))
3904 if (flags
& tf_error
)
3905 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3906 "reinterpret_cast from integer to pointer");
3909 return (RECUR (from
, TREE_CODE (t
) != VIEW_CONVERT_EXPR
));
3913 /* -- a unary operator & that is applied to an lvalue that
3914 designates an object with thread or automatic storage
3916 t
= TREE_OPERAND (t
, 0);
3918 if (TREE_CODE (t
) == OFFSET_REF
&& PTRMEM_OK_P (t
))
3919 /* A pointer-to-member constant. */
3923 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
3924 any checking here, as we might dereference the pointer later. If
3925 we remove this code, also remove check_automatic_or_tls. */
3926 i
= check_automatic_or_tls (t
);
3931 if (flags
& tf_error
)
3932 error ("address-of an object %qE with thread local or "
3933 "automatic storage is not a constant expression", t
);
3937 return RECUR (t
, any
);
3943 /* -- a class member access unless its postfix-expression is
3944 of literal type or of pointer to literal type. */
3945 /* This test would be redundant, as it follows from the
3946 postfix-expression being a potential constant expression. */
3947 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
3949 case EXPR_PACK_EXPANSION
:
3950 return RECUR (PACK_EXPANSION_PATTERN (t
), want_rval
);
3954 tree x
= TREE_OPERAND (t
, 0);
3956 if (is_this_parameter (x
))
3958 if (DECL_CONTEXT (x
)
3959 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x
)))
3961 if (flags
& tf_error
)
3962 error ("use of %<this%> in a constant expression");
3967 return RECUR (x
, rval
);
3970 case STATEMENT_LIST
:
3972 tree_stmt_iterator i
;
3973 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
3975 if (!RECUR (tsi_stmt (i
), any
))
3983 if (cxx_dialect
< cxx14
)
3985 if (!RECUR (TREE_OPERAND (t
, 0), any
))
3987 if (!RECUR (TREE_OPERAND (t
, 1), rval
))
3992 if (cxx_dialect
< cxx14
)
3994 if (!RECUR (TREE_OPERAND (t
, 0), rval
))
3996 if (!RECUR (TREE_OPERAND (t
, 2), rval
))
4001 if (!RECUR (IF_COND (t
), rval
))
4003 if (!RECUR (THEN_CLAUSE (t
), any
))
4005 if (!RECUR (ELSE_CLAUSE (t
), any
))
4010 if (!RECUR (DO_COND (t
), rval
))
4012 if (!RECUR (DO_BODY (t
), any
))
4017 if (!RECUR (FOR_INIT_STMT (t
), any
))
4019 if (!RECUR (FOR_COND (t
), rval
))
4021 if (!RECUR (FOR_EXPR (t
), any
))
4023 if (!RECUR (FOR_BODY (t
), any
))
4028 if (!RECUR (WHILE_COND (t
), rval
))
4030 if (!RECUR (WHILE_BODY (t
), any
))
4035 if (!RECUR (SWITCH_STMT_COND (t
), rval
))
4037 if (!RECUR (SWITCH_STMT_BODY (t
), any
))
4042 return RECUR (STMT_EXPR_STMT (t
), rval
);
4045 case DYNAMIC_CAST_EXPR
:
4046 case PSEUDO_DTOR_EXPR
:
4050 case VEC_DELETE_EXPR
:
4053 case OMP_ATOMIC_READ
:
4054 case OMP_ATOMIC_CAPTURE_OLD
:
4055 case OMP_ATOMIC_CAPTURE_NEW
:
4056 /* GCC internal stuff. */
4059 case TRANSACTION_EXPR
:
4062 if (flags
& tf_error
)
4063 error ("expression %qE is not a constant-expression", t
);
4067 /* -- a typeid expression whose operand is of polymorphic
4070 tree e
= TREE_OPERAND (t
, 0);
4071 if (!TYPE_P (e
) && !type_dependent_expression_p (e
)
4072 && TYPE_POLYMORPHIC_P (TREE_TYPE (e
)))
4074 if (flags
& tf_error
)
4075 error ("typeid-expression is not a constant expression "
4076 "because %qE is of polymorphic type", e
);
4083 /* -- a subtraction where both operands are pointers. */
4084 if (TYPE_PTR_P (TREE_OPERAND (t
, 0))
4085 && TYPE_PTR_P (TREE_OPERAND (t
, 1)))
4087 if (flags
& tf_error
)
4088 error ("difference of two pointer expressions is not "
4089 "a constant expression");
4101 /* -- a relational or equality operator where at least
4102 one of the operands is a pointer. */
4103 if (TYPE_PTR_P (TREE_OPERAND (t
, 0))
4104 || TYPE_PTR_P (TREE_OPERAND (t
, 1)))
4106 if (flags
& tf_error
)
4107 error ("pointer comparison expression is not a "
4108 "constant expression");
4114 case PREINCREMENT_EXPR
:
4115 case POSTINCREMENT_EXPR
:
4116 case PREDECREMENT_EXPR
:
4117 case POSTDECREMENT_EXPR
:
4118 if (cxx_dialect
< cxx14
)
4124 if (TYPE_P (TREE_OPERAND (t
, 0)))
4126 /* else fall through. */
4132 case FIX_TRUNC_EXPR
:
4136 case TRUTH_NOT_EXPR
:
4137 case FIXED_CONVERT_EXPR
:
4138 case UNARY_PLUS_EXPR
:
4140 return RECUR (TREE_OPERAND (t
, 0), rval
);
4143 case CONST_CAST_EXPR
:
4144 case STATIC_CAST_EXPR
:
4145 case REINTERPRET_CAST_EXPR
:
4146 case IMPLICIT_CONV_EXPR
:
4147 if (cxx_dialect
< cxx11
4148 && !dependent_type_p (TREE_TYPE (t
))
4149 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
)))
4150 /* In C++98, a conversion to non-integral type can't be part of a
4151 constant expression. */
4153 if (flags
& tf_error
)
4154 error ("cast to non-integral type %qT in a constant expression",
4159 return (RECUR (TREE_OPERAND (t
, 0),
4160 TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
));
4163 return RECUR (BIND_EXPR_BODY (t
), want_rval
);
4165 case WITH_CLEANUP_EXPR
:
4166 case CLEANUP_POINT_EXPR
:
4167 case MUST_NOT_THROW_EXPR
:
4168 case TRY_CATCH_EXPR
:
4173 case NON_DEPENDENT_EXPR
:
4174 /* For convenience. */
4176 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
4179 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
4182 if (!literal_type_p (TREE_TYPE (t
)))
4184 if (flags
& tf_error
)
4186 error ("temporary of non-literal type %qT in a "
4187 "constant expression", TREE_TYPE (t
));
4188 explain_non_literal_class (TREE_TYPE (t
));
4193 return RECUR (TREE_OPERAND (t
, 1), rval
);
4197 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
4198 constructor_elt
*ce
;
4199 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
4200 if (!RECUR (ce
->value
, want_rval
))
4207 gcc_assert (TREE_PURPOSE (t
) == NULL_TREE
4208 || DECL_P (TREE_PURPOSE (t
)));
4209 if (!RECUR (TREE_VALUE (t
), want_rval
))
4211 if (TREE_CHAIN (t
) == NULL_TREE
)
4213 return RECUR (TREE_CHAIN (t
), want_rval
);
4216 case TRUNC_DIV_EXPR
:
4218 case FLOOR_DIV_EXPR
:
4219 case ROUND_DIV_EXPR
:
4220 case TRUNC_MOD_EXPR
:
4222 case ROUND_MOD_EXPR
:
4224 tree denom
= TREE_OPERAND (t
, 1);
4225 if (!RECUR (denom
, rval
))
4227 /* We can't call cxx_eval_outermost_constant_expr on an expression
4228 that hasn't been through instantiate_non_dependent_expr yet. */
4229 if (!processing_template_decl
)
4230 denom
= cxx_eval_outermost_constant_expr (denom
, true);
4231 if (integer_zerop (denom
))
4233 if (flags
& tf_error
)
4234 error ("division by zero is not a constant-expression");
4240 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
4246 /* check_return_expr sometimes wraps a TARGET_EXPR in a
4247 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
4248 introduced by build_call_a. */
4249 tree op0
= TREE_OPERAND (t
, 0);
4250 tree op1
= TREE_OPERAND (t
, 1);
4252 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
4253 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
4254 return RECUR (op0
, want_rval
);
4259 /* If the first operand is the non-short-circuit constant, look at
4260 the second operand; otherwise we only care about the first one for
4262 case TRUTH_AND_EXPR
:
4263 case TRUTH_ANDIF_EXPR
:
4264 tmp
= boolean_true_node
;
4267 case TRUTH_ORIF_EXPR
:
4268 tmp
= boolean_false_node
;
4271 tree op
= TREE_OPERAND (t
, 0);
4272 if (!RECUR (op
, rval
))
4274 if (!processing_template_decl
)
4275 op
= cxx_eval_outermost_constant_expr (op
, true);
4276 if (tree_int_cst_equal (op
, tmp
))
4277 return RECUR (TREE_OPERAND (t
, 1), rval
);
4284 case POINTER_PLUS_EXPR
:
4286 case EXACT_DIV_EXPR
:
4296 case TRUTH_XOR_EXPR
:
4297 case UNORDERED_EXPR
:
4310 case ARRAY_RANGE_REF
:
4314 for (i
= 0; i
< 2; ++i
)
4315 if (!RECUR (TREE_OPERAND (t
, i
), want_rval
))
4319 case CILK_SYNC_STMT
:
4320 case CILK_SPAWN_STMT
:
4321 case ARRAY_NOTATION_REF
:
4326 for (i
= 0; i
< 3; ++i
)
4327 if (!RECUR (TREE_OPERAND (t
, i
), true))
4333 /* If the condition is a known constant, we know which of the legs we
4334 care about; otherwise we only require that the condition and
4335 either of the legs be potentially constant. */
4336 tmp
= TREE_OPERAND (t
, 0);
4337 if (!RECUR (tmp
, rval
))
4339 if (!processing_template_decl
)
4340 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
4341 if (integer_zerop (tmp
))
4342 return RECUR (TREE_OPERAND (t
, 2), want_rval
);
4343 else if (TREE_CODE (tmp
) == INTEGER_CST
)
4344 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
4345 for (i
= 1; i
< 3; ++i
)
4346 if (potential_constant_expression_1 (TREE_OPERAND (t
, i
),
4347 want_rval
, strict
, tf_none
))
4349 if (flags
& tf_error
)
4350 error ("expression %qE is not a constant-expression", t
);
4354 if (VEC_INIT_EXPR_IS_CONSTEXPR (t
))
4356 if (flags
& tf_error
)
4358 error ("non-constant array initialization");
4359 diagnose_non_constexpr_vec_init (t
);
4364 if (objc_is_property_ref (t
))
4367 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t
)));
4374 /* The main entry point to the above. */
4377 potential_constant_expression (tree t
)
4379 return potential_constant_expression_1 (t
, false, true, tf_none
);
4383 potential_static_init_expression (tree t
)
4385 return potential_constant_expression_1 (t
, false, false, tf_none
);
4388 /* As above, but require a constant rvalue. */
4391 potential_rvalue_constant_expression (tree t
)
4393 return potential_constant_expression_1 (t
, true, true, tf_none
);
4396 /* Like above, but complain about non-constant expressions. */
4399 require_potential_constant_expression (tree t
)
4401 return potential_constant_expression_1 (t
, false, true, tf_warning_or_error
);
4404 /* Cross product of the above. */
4407 require_potential_rvalue_constant_expression (tree t
)
4409 return potential_constant_expression_1 (t
, true, true, tf_warning_or_error
);
4412 #include "gt-cp-constexpr.h"