1 /* Perform the semantic phase of constexpr parsing, i.e., the process of
2 building tree structure, checking semantic consistency, and
3 building RTL. These routines are used both during actual parsing
4 and during the instantiation of template functions.
6 Copyright (C) 1998-2014 Free Software Foundation, Inc.
8 This file is part of GCC.
10 GCC is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
15 GCC is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3. If not see
22 <http://www.gnu.org/licenses/>. */
26 #include "coretypes.h"
30 #include "c-family/c-objc.h"
31 #include "tree-iterator.h"
34 #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
)
66 t
= complete_type (t
);
67 gcc_assert (COMPLETE_TYPE_P (t
) || errorcount
);
68 return CLASSTYPE_LITERAL_P (t
);
70 if (TREE_CODE (t
) == ARRAY_TYPE
)
71 return literal_type_p (strip_array_types (t
));
75 /* If DECL is a variable declared `constexpr', require its type
76 be literal. Return the DECL if OK, otherwise NULL. */
79 ensure_literal_type_for_constexpr_object (tree decl
)
81 tree type
= TREE_TYPE (decl
);
83 && (DECL_DECLARED_CONSTEXPR_P (decl
)
84 || var_in_constexpr_fn (decl
))
85 && !processing_template_decl
)
87 tree stype
= strip_array_types (type
);
88 if (CLASS_TYPE_P (stype
) && !COMPLETE_TYPE_P (complete_type (stype
)))
89 /* Don't complain here, we'll complain about incompleteness
90 when we try to initialize the variable. */;
91 else if (!literal_type_p (type
))
93 if (DECL_DECLARED_CONSTEXPR_P (decl
))
94 error ("the type %qT of constexpr variable %qD is not literal",
98 error ("variable %qD of non-literal type %qT in %<constexpr%> "
99 "function", decl
, type
);
100 cp_function_chain
->invalid_constexpr
= true;
102 explain_non_literal_class (type
);
109 /* Representation of entries in the constexpr function definition table. */
111 struct GTY((for_user
)) constexpr_fundef
{
116 struct constexpr_fundef_hasher
: ggc_hasher
<constexpr_fundef
*>
118 static hashval_t
hash (constexpr_fundef
*);
119 static bool equal (constexpr_fundef
*, constexpr_fundef
*);
122 /* This table holds all constexpr function definitions seen in
123 the current translation unit. */
125 static GTY (()) hash_table
<constexpr_fundef_hasher
> *constexpr_fundef_table
;
127 /* Utility function used for managing the constexpr function table.
128 Return true if the entries pointed to by P and Q are for the
129 same constexpr function. */
132 constexpr_fundef_hasher::equal (constexpr_fundef
*lhs
, constexpr_fundef
*rhs
)
134 return lhs
->decl
== rhs
->decl
;
137 /* Utility function used for managing the constexpr function table.
138 Return a hash value for the entry pointed to by Q. */
141 constexpr_fundef_hasher::hash (constexpr_fundef
*fundef
)
143 return DECL_UID (fundef
->decl
);
146 /* Return a previously saved definition of function FUN. */
148 static constexpr_fundef
*
149 retrieve_constexpr_fundef (tree fun
)
151 constexpr_fundef fundef
= { NULL
, NULL
};
152 if (constexpr_fundef_table
== NULL
)
156 return constexpr_fundef_table
->find (&fundef
);
159 /* Check whether the parameter and return types of FUN are valid for a
160 constexpr function, and complain if COMPLAIN. */
163 is_valid_constexpr_fn (tree fun
, bool complain
)
167 if (DECL_INHERITED_CTOR_BASE (fun
)
168 && TREE_CODE (fun
) == TEMPLATE_DECL
)
172 error ("inherited constructor %qD is not constexpr",
173 get_inherited_ctor (fun
));
177 for (tree parm
= FUNCTION_FIRST_USER_PARM (fun
);
178 parm
!= NULL_TREE
; parm
= TREE_CHAIN (parm
))
179 if (!literal_type_p (TREE_TYPE (parm
)))
184 error ("invalid type for parameter %d of constexpr "
185 "function %q+#D", DECL_PARM_INDEX (parm
), fun
);
186 explain_non_literal_class (TREE_TYPE (parm
));
191 if (!DECL_CONSTRUCTOR_P (fun
))
193 tree rettype
= TREE_TYPE (TREE_TYPE (fun
));
194 if (!literal_type_p (rettype
))
199 error ("invalid return type %qT of constexpr function %q+D",
201 explain_non_literal_class (rettype
);
205 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
206 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun
)))
211 error ("enclosing class of constexpr non-static member "
212 "function %q+#D is not a literal type", fun
);
213 explain_non_literal_class (DECL_CONTEXT (fun
));
217 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun
)))
221 error ("%q#T has virtual base classes", DECL_CONTEXT (fun
));
227 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
228 for a member of an anonymous aggregate, INIT is the initializer for that
229 member, and VEC_OUTER is the vector of constructor elements for the class
230 whose constructor we are processing. Add the initializer to the vector
231 and return true to indicate success. */
234 build_anon_member_initialization (tree member
, tree init
,
235 vec
<constructor_elt
, va_gc
> **vec_outer
)
237 /* MEMBER presents the relevant fields from the inside out, but we need
238 to build up the initializer from the outside in so that we can reuse
239 previously built CONSTRUCTORs if this is, say, the second field in an
240 anonymous struct. So we use a vec as a stack. */
241 auto_vec
<tree
, 2> fields
;
244 fields
.safe_push (TREE_OPERAND (member
, 1));
245 member
= TREE_OPERAND (member
, 0);
247 while (ANON_AGGR_TYPE_P (TREE_TYPE (member
))
248 && TREE_CODE (member
) == COMPONENT_REF
);
250 /* VEC has the constructor elements vector for the context of FIELD.
251 If FIELD is an anonymous aggregate, we will push inside it. */
252 vec
<constructor_elt
, va_gc
> **vec
= vec_outer
;
254 while (field
= fields
.pop(),
255 ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
258 /* If there is already an outer constructor entry for the anonymous
259 aggregate FIELD, use it; otherwise, insert one. */
260 if (vec_safe_is_empty (*vec
)
261 || (*vec
)->last().index
!= field
)
263 ctor
= build_constructor (TREE_TYPE (field
), NULL
);
264 CONSTRUCTOR_APPEND_ELT (*vec
, field
, ctor
);
267 ctor
= (*vec
)->last().value
;
268 vec
= &CONSTRUCTOR_ELTS (ctor
);
271 /* Now we're at the innermost field, the one that isn't an anonymous
272 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
273 gcc_assert (fields
.is_empty());
274 CONSTRUCTOR_APPEND_ELT (*vec
, field
, init
);
279 /* Subroutine of build_constexpr_constructor_member_initializers.
280 The expression tree T represents a data member initialization
281 in a (constexpr) constructor definition. Build a pairing of
282 the data member with its initializer, and prepend that pair
283 to the existing initialization pair INITS. */
286 build_data_member_initialization (tree t
, vec
<constructor_elt
, va_gc
> **vec
)
289 if (TREE_CODE (t
) == CLEANUP_POINT_EXPR
)
290 t
= TREE_OPERAND (t
, 0);
291 if (TREE_CODE (t
) == EXPR_STMT
)
292 t
= TREE_OPERAND (t
, 0);
293 if (t
== error_mark_node
)
295 if (TREE_CODE (t
) == STATEMENT_LIST
)
297 tree_stmt_iterator i
;
298 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
300 if (! build_data_member_initialization (tsi_stmt (i
), vec
))
305 if (TREE_CODE (t
) == CLEANUP_STMT
)
307 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
308 but we can in a constexpr constructor for a non-literal class. Just
309 ignore it; either all the initialization will be constant, in which
310 case the cleanup can't run, or it can't be constexpr.
311 Still recurse into CLEANUP_BODY. */
312 return build_data_member_initialization (CLEANUP_BODY (t
), vec
);
314 if (TREE_CODE (t
) == CONVERT_EXPR
)
315 t
= TREE_OPERAND (t
, 0);
316 if (TREE_CODE (t
) == INIT_EXPR
317 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
318 use what this function builds for cx_check_missing_mem_inits, and
319 assignment in the ctor body doesn't count. */
320 || (cxx_dialect
< cxx14
&& TREE_CODE (t
) == MODIFY_EXPR
))
322 member
= TREE_OPERAND (t
, 0);
323 init
= break_out_target_exprs (TREE_OPERAND (t
, 1));
325 else if (TREE_CODE (t
) == CALL_EXPR
)
327 tree fn
= get_callee_fndecl (t
);
328 if (!fn
|| !DECL_CONSTRUCTOR_P (fn
))
329 /* We're only interested in calls to subobject constructors. */
331 member
= CALL_EXPR_ARG (t
, 0);
332 /* We don't use build_cplus_new here because it complains about
333 abstract bases. Leaving the call unwrapped means that it has the
334 wrong type, but cxx_eval_constant_expression doesn't care. */
335 init
= break_out_target_exprs (t
);
337 else if (TREE_CODE (t
) == BIND_EXPR
)
338 return build_data_member_initialization (BIND_EXPR_BODY (t
), vec
);
340 /* Don't add anything else to the CONSTRUCTOR. */
342 if (INDIRECT_REF_P (member
))
343 member
= TREE_OPERAND (member
, 0);
344 if (TREE_CODE (member
) == NOP_EXPR
)
348 if (TREE_CODE (op
) == ADDR_EXPR
)
350 gcc_assert (same_type_ignoring_top_level_qualifiers_p
351 (TREE_TYPE (TREE_TYPE (op
)),
352 TREE_TYPE (TREE_TYPE (member
))));
353 /* Initializing a cv-qualified member; we need to look through
357 else if (op
== current_class_ptr
358 && (same_type_ignoring_top_level_qualifiers_p
359 (TREE_TYPE (TREE_TYPE (member
)),
360 current_class_type
)))
361 /* Delegating constructor. */
365 /* This is an initializer for an empty base; keep it for now so
366 we can check it in cxx_eval_bare_aggregate. */
367 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member
))));
370 if (TREE_CODE (member
) == ADDR_EXPR
)
371 member
= TREE_OPERAND (member
, 0);
372 if (TREE_CODE (member
) == COMPONENT_REF
)
374 tree aggr
= TREE_OPERAND (member
, 0);
375 if (TREE_CODE (aggr
) != COMPONENT_REF
)
376 /* Normal member initialization. */
377 member
= TREE_OPERAND (member
, 1);
378 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr
)))
379 /* Initializing a member of an anonymous union. */
380 return build_anon_member_initialization (member
, init
, vec
);
382 /* We're initializing a vtable pointer in a base. Leave it as
383 COMPONENT_REF so we remember the path to get to the vfield. */
384 gcc_assert (TREE_TYPE (member
) == vtbl_ptr_type_node
);
387 CONSTRUCTOR_APPEND_ELT (*vec
, member
, init
);
391 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
392 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
393 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
396 check_constexpr_bind_expr_vars (tree t
)
398 gcc_assert (TREE_CODE (t
) == BIND_EXPR
);
400 for (tree var
= BIND_EXPR_VARS (t
); var
; var
= DECL_CHAIN (var
))
401 if (TREE_CODE (var
) == TYPE_DECL
402 && DECL_IMPLICIT_TYPEDEF_P (var
))
407 /* Subroutine of check_constexpr_ctor_body. */
410 check_constexpr_ctor_body_1 (tree last
, tree list
)
412 switch (TREE_CODE (list
))
415 if (TREE_CODE (DECL_EXPR_DECL (list
)) == USING_DECL
)
419 case CLEANUP_POINT_EXPR
:
420 return check_constexpr_ctor_body (last
, TREE_OPERAND (list
, 0),
424 if (!check_constexpr_bind_expr_vars (list
)
425 || !check_constexpr_ctor_body (last
, BIND_EXPR_BODY (list
),
439 /* Make sure that there are no statements after LAST in the constructor
440 body represented by LIST. */
443 check_constexpr_ctor_body (tree last
, tree list
, bool complain
)
445 /* C++14 doesn't require a constexpr ctor to have an empty body. */
446 if (cxx_dialect
>= cxx14
)
450 if (TREE_CODE (list
) == STATEMENT_LIST
)
452 tree_stmt_iterator i
= tsi_last (list
);
453 for (; !tsi_end_p (i
); tsi_prev (&i
))
455 tree t
= tsi_stmt (i
);
458 if (!check_constexpr_ctor_body_1 (last
, t
))
465 else if (list
!= last
466 && !check_constexpr_ctor_body_1 (last
, list
))
471 error ("constexpr constructor does not have empty body");
472 DECL_DECLARED_CONSTEXPR_P (current_function_decl
) = false;
477 /* V is a vector of constructor elements built up for the base and member
478 initializers of a constructor for TYPE. They need to be in increasing
479 offset order, which they might not be yet if TYPE has a primary base
480 which is not first in the base-clause or a vptr and at least one base
481 all of which are non-primary. */
483 static vec
<constructor_elt
, va_gc
> *
484 sort_constexpr_mem_initializers (tree type
, vec
<constructor_elt
, va_gc
> *v
)
486 tree pri
= CLASSTYPE_PRIMARY_BINFO (type
);
492 field_type
= BINFO_TYPE (pri
);
493 else if (TYPE_CONTAINS_VPTR_P (type
))
494 field_type
= vtbl_ptr_type_node
;
498 /* Find the element for the primary base or vptr and move it to the
499 beginning of the vec. */
500 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
501 if (TREE_TYPE (ce
->index
) == field_type
)
504 if (i
> 0 && i
< vec_safe_length (v
))
506 vec
<constructor_elt
, va_gc
> &vref
= *v
;
507 constructor_elt elt
= vref
[i
];
516 /* Build compile-time evalable representations of member-initializer list
517 for a constexpr constructor. */
520 build_constexpr_constructor_member_initializers (tree type
, tree body
)
522 vec
<constructor_elt
, va_gc
> *vec
= NULL
;
524 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
525 || TREE_CODE (body
) == EH_SPEC_BLOCK
)
526 body
= TREE_OPERAND (body
, 0);
527 if (TREE_CODE (body
) == STATEMENT_LIST
)
528 body
= STATEMENT_LIST_HEAD (body
)->stmt
;
529 body
= BIND_EXPR_BODY (body
);
530 if (TREE_CODE (body
) == CLEANUP_POINT_EXPR
)
532 body
= TREE_OPERAND (body
, 0);
533 if (TREE_CODE (body
) == EXPR_STMT
)
534 body
= TREE_OPERAND (body
, 0);
535 if (TREE_CODE (body
) == INIT_EXPR
536 && (same_type_ignoring_top_level_qualifiers_p
537 (TREE_TYPE (TREE_OPERAND (body
, 0)),
538 current_class_type
)))
541 return TREE_OPERAND (body
, 1);
543 ok
= build_data_member_initialization (body
, &vec
);
545 else if (TREE_CODE (body
) == STATEMENT_LIST
)
547 tree_stmt_iterator i
;
548 for (i
= tsi_start (body
); !tsi_end_p (i
); tsi_next (&i
))
550 ok
= build_data_member_initialization (tsi_stmt (i
), &vec
);
555 else if (TREE_CODE (body
) == TRY_BLOCK
)
557 error ("body of %<constexpr%> constructor cannot be "
558 "a function-try-block");
559 return error_mark_node
;
561 else if (EXPR_P (body
))
562 ok
= build_data_member_initialization (body
, &vec
);
564 gcc_assert (errorcount
> 0);
567 if (vec_safe_length (vec
) > 0)
569 /* In a delegating constructor, return the target. */
570 constructor_elt
*ce
= &(*vec
)[0];
571 if (ce
->index
== current_class_ptr
)
578 vec
= sort_constexpr_mem_initializers (type
, vec
);
579 return build_constructor (type
, vec
);
582 return error_mark_node
;
585 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
586 declared to be constexpr, or a sub-statement thereof. Returns the
587 return value if suitable, error_mark_node for a statement not allowed in
588 a constexpr function, or NULL_TREE if no return value was found. */
591 constexpr_fn_retval (tree body
)
593 switch (TREE_CODE (body
))
597 tree_stmt_iterator i
;
598 tree expr
= NULL_TREE
;
599 for (i
= tsi_start (body
); !tsi_end_p (i
); tsi_next (&i
))
601 tree s
= constexpr_fn_retval (tsi_stmt (i
));
602 if (s
== error_mark_node
)
603 return error_mark_node
;
604 else if (s
== NULL_TREE
)
605 /* Keep iterating. */;
607 /* Multiple return statements. */
608 return error_mark_node
;
616 return break_out_target_exprs (TREE_OPERAND (body
, 0));
620 tree decl
= DECL_EXPR_DECL (body
);
621 if (TREE_CODE (decl
) == USING_DECL
622 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
623 || DECL_ARTIFICIAL (decl
))
625 return error_mark_node
;
628 case CLEANUP_POINT_EXPR
:
629 return constexpr_fn_retval (TREE_OPERAND (body
, 0));
632 if (!check_constexpr_bind_expr_vars (body
))
633 return error_mark_node
;
634 return constexpr_fn_retval (BIND_EXPR_BODY (body
));
640 return error_mark_node
;
644 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
645 FUN; do the necessary transformations to turn it into a single expression
646 that we can store in the hash table. */
649 massage_constexpr_body (tree fun
, tree body
)
651 if (DECL_CONSTRUCTOR_P (fun
))
652 body
= build_constexpr_constructor_member_initializers
653 (DECL_CONTEXT (fun
), body
);
654 else if (cxx_dialect
< cxx14
)
656 if (TREE_CODE (body
) == EH_SPEC_BLOCK
)
657 body
= EH_SPEC_STMTS (body
);
658 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
659 body
= TREE_OPERAND (body
, 0);
660 body
= constexpr_fn_retval (body
);
665 /* FUN is a constexpr constructor with massaged body BODY. Return true
666 if some bases/fields are uninitialized, and complain if COMPLAIN. */
669 cx_check_missing_mem_inits (tree fun
, tree body
, bool complain
)
676 if (TREE_CODE (body
) != CONSTRUCTOR
)
679 nelts
= CONSTRUCTOR_NELTS (body
);
680 ctype
= DECL_CONTEXT (fun
);
681 field
= TYPE_FIELDS (ctype
);
683 if (TREE_CODE (ctype
) == UNION_TYPE
)
685 if (nelts
== 0 && next_initializable_field (field
))
688 error ("%<constexpr%> constructor for union %qT must "
689 "initialize exactly one non-static data member", ctype
);
696 for (i
= 0; i
<= nelts
; ++i
)
703 index
= CONSTRUCTOR_ELT (body
, i
)->index
;
704 /* Skip base and vtable inits. */
705 if (TREE_CODE (index
) != FIELD_DECL
706 || DECL_ARTIFICIAL (index
))
709 for (; field
!= index
; field
= DECL_CHAIN (field
))
712 if (TREE_CODE (field
) != FIELD_DECL
713 || (DECL_C_BIT_FIELD (field
) && !DECL_NAME (field
))
714 || DECL_ARTIFICIAL (field
))
716 ftype
= strip_array_types (TREE_TYPE (field
));
717 if (type_has_constexpr_default_constructor (ftype
))
719 /* It's OK to skip a member with a trivial constexpr ctor.
720 A constexpr ctor that isn't trivial should have been
722 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype
)
728 error ("member %qD must be initialized by mem-initializer "
729 "in %<constexpr%> constructor", field
);
730 inform (DECL_SOURCE_LOCATION (field
), "declared here");
733 if (field
== NULL_TREE
)
735 field
= DECL_CHAIN (field
);
741 /* We are processing the definition of the constexpr function FUN.
742 Check that its BODY fulfills the propriate requirements and
743 enter it in the constexpr function definition table.
744 For constructor BODY is actually the TREE_LIST of the
745 member-initializer list. */
748 register_constexpr_fundef (tree fun
, tree body
)
750 constexpr_fundef entry
;
751 constexpr_fundef
**slot
;
753 if (!is_valid_constexpr_fn (fun
, !DECL_GENERATED_P (fun
)))
756 body
= massage_constexpr_body (fun
, body
);
757 if (body
== NULL_TREE
|| body
== error_mark_node
)
759 if (!DECL_CONSTRUCTOR_P (fun
))
760 error ("body of constexpr function %qD not a return-statement", fun
);
764 if (!potential_rvalue_constant_expression (body
))
766 if (!DECL_GENERATED_P (fun
))
767 require_potential_rvalue_constant_expression (body
);
771 if (DECL_CONSTRUCTOR_P (fun
)
772 && cx_check_missing_mem_inits (fun
, body
, !DECL_GENERATED_P (fun
)))
775 /* Create the constexpr function table if necessary. */
776 if (constexpr_fundef_table
== NULL
)
777 constexpr_fundef_table
778 = hash_table
<constexpr_fundef_hasher
>::create_ggc (101);
782 slot
= constexpr_fundef_table
->find_slot (&entry
, INSERT
);
784 gcc_assert (*slot
== NULL
);
785 *slot
= ggc_alloc
<constexpr_fundef
> ();
791 /* FUN is a non-constexpr function called in a context that requires a
792 constant expression. If it comes from a constexpr template, explain why
793 the instantiation isn't constexpr. */
796 explain_invalid_constexpr_fn (tree fun
)
798 static hash_set
<tree
> *diagnosed
;
801 /* Only diagnose defaulted functions or instantiations. */
802 if (!DECL_DEFAULTED_FN (fun
)
803 && !is_instantiation_of_constexpr (fun
))
805 if (diagnosed
== NULL
)
806 diagnosed
= new hash_set
<tree
>;
807 if (diagnosed
->add (fun
))
808 /* Already explained. */
811 save_loc
= input_location
;
812 input_location
= DECL_SOURCE_LOCATION (fun
);
813 inform (0, "%q+D is not usable as a constexpr function because:", fun
);
814 /* First check the declaration. */
815 if (is_valid_constexpr_fn (fun
, true))
817 /* Then if it's OK, the body. */
818 if (!DECL_DECLARED_CONSTEXPR_P (fun
))
819 explain_implicit_non_constexpr (fun
);
822 body
= massage_constexpr_body (fun
, DECL_SAVED_TREE (fun
));
823 require_potential_rvalue_constant_expression (body
);
824 if (DECL_CONSTRUCTOR_P (fun
))
825 cx_check_missing_mem_inits (fun
, body
, true);
828 input_location
= save_loc
;
831 /* Objects of this type represent calls to constexpr functions
832 along with the bindings of parameters to their arguments, for
833 the purpose of compile time evaluation. */
835 struct GTY((for_user
)) constexpr_call
{
836 /* Description of the constexpr function definition. */
837 constexpr_fundef
*fundef
;
838 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
839 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
840 Note: This arrangement is made to accommodate the use of
841 iterative_hash_template_arg (see pt.c). If you change this
842 representation, also change the hash calculation in
843 cxx_eval_call_expression. */
845 /* Result of the call.
846 NULL means the call is being evaluated.
847 error_mark_node means that the evaluation was erroneous;
848 otherwise, the actuall value of the call. */
850 /* The hash of this call; we remember it here to avoid having to
851 recalculate it when expanding the hash table. */
855 struct constexpr_call_hasher
: ggc_hasher
<constexpr_call
*>
857 static hashval_t
hash (constexpr_call
*);
858 static bool equal (constexpr_call
*, constexpr_call
*);
861 /* The constexpr expansion context. CALL is the current function
862 expansion, CTOR is the current aggregate initializer, OBJECT is the
863 object being initialized by CTOR, either a VAR_DECL or a _REF. VALUES
864 is a map of values of variables initialized within the expression. */
866 struct constexpr_ctx
{
867 constexpr_call
*call
;
868 hash_map
<tree
,tree
> *values
;
875 /* A table of all constexpr calls that have been evaluated by the
876 compiler in this translation unit. */
878 static GTY (()) hash_table
<constexpr_call_hasher
> *constexpr_call_table
;
880 static tree
cxx_eval_constant_expression (const constexpr_ctx
*, tree
,
881 bool, bool *, bool *, tree
* = NULL
);
883 /* Compute a hash value for a constexpr call representation. */
886 constexpr_call_hasher::hash (constexpr_call
*info
)
891 /* Return true if the objects pointed to by P and Q represent calls
892 to the same constexpr function with the same arguments.
893 Otherwise, return false. */
896 constexpr_call_hasher::equal (constexpr_call
*lhs
, constexpr_call
*rhs
)
902 if (!constexpr_fundef_hasher::equal (lhs
->fundef
, rhs
->fundef
))
904 lhs_bindings
= lhs
->bindings
;
905 rhs_bindings
= rhs
->bindings
;
906 while (lhs_bindings
!= NULL
&& rhs_bindings
!= NULL
)
908 tree lhs_arg
= TREE_VALUE (lhs_bindings
);
909 tree rhs_arg
= TREE_VALUE (rhs_bindings
);
910 gcc_assert (TREE_TYPE (lhs_arg
) == TREE_TYPE (rhs_arg
));
911 if (!cp_tree_equal (lhs_arg
, rhs_arg
))
913 lhs_bindings
= TREE_CHAIN (lhs_bindings
);
914 rhs_bindings
= TREE_CHAIN (rhs_bindings
);
916 return lhs_bindings
== rhs_bindings
;
919 /* Initialize the constexpr call table, if needed. */
922 maybe_initialize_constexpr_call_table (void)
924 if (constexpr_call_table
== NULL
)
925 constexpr_call_table
= hash_table
<constexpr_call_hasher
>::create_ggc (101);
928 /* We have an expression tree T that represents a call, either CALL_EXPR
929 or AGGR_INIT_EXPR. If the call is lexically to a named function,
930 retrun the _DECL for that function. */
933 get_function_named_in_call (tree t
)
936 switch (TREE_CODE (t
))
939 fun
= CALL_EXPR_FN (t
);
943 fun
= AGGR_INIT_EXPR_FN (t
);
950 if (fun
&& TREE_CODE (fun
) == ADDR_EXPR
951 && TREE_CODE (TREE_OPERAND (fun
, 0)) == FUNCTION_DECL
)
952 fun
= TREE_OPERAND (fun
, 0);
956 /* We have an expression tree T that represents a call, either CALL_EXPR
957 or AGGR_INIT_EXPR. Return the Nth argument. */
960 get_nth_callarg (tree t
, int n
)
962 switch (TREE_CODE (t
))
965 return CALL_EXPR_ARG (t
, n
);
968 return AGGR_INIT_EXPR_ARG (t
, n
);
976 /* Look up the binding of the function parameter T in a constexpr
977 function call context CALL. */
980 lookup_parameter_binding (const constexpr_call
*call
, tree t
)
982 tree b
= purpose_member (t
, call
->bindings
);
983 return TREE_VALUE (b
);
986 /* Attempt to evaluate T which represents a call to a builtin function.
987 We assume here that all builtin functions evaluate to scalar types
988 represented by _CST nodes. */
991 cxx_eval_builtin_function_call (const constexpr_ctx
*ctx
, tree t
,
993 bool *non_constant_p
, bool *overflow_p
)
995 const int nargs
= call_expr_nargs (t
);
996 tree
*args
= (tree
*) alloca (nargs
* sizeof (tree
));
999 for (i
= 0; i
< nargs
; ++i
)
1001 args
[i
] = cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, i
),
1003 non_constant_p
, overflow_p
);
1004 if (ctx
->quiet
&& *non_constant_p
)
1007 if (*non_constant_p
)
1009 new_call
= fold_builtin_call_array (EXPR_LOCATION (t
), TREE_TYPE (t
),
1010 CALL_EXPR_FN (t
), nargs
, args
);
1011 VERIFY_CONSTANT (new_call
);
1015 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1016 the type of the value to match. */
1019 adjust_temp_type (tree type
, tree temp
)
1021 if (TREE_TYPE (temp
) == type
)
1023 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1024 if (TREE_CODE (temp
) == CONSTRUCTOR
)
1025 return build_constructor (type
, CONSTRUCTOR_ELTS (temp
));
1026 gcc_assert (scalarish_type_p (type
));
1027 return cp_fold_convert (type
, temp
);
1030 /* True if we want to use the new handling of constexpr calls based on
1032 #define use_new_call true
1034 /* Subroutine of cxx_eval_call_expression.
1035 We are processing a call expression (either CALL_EXPR or
1036 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1037 all arguments and bind their values to correspondings
1038 parameters, making up the NEW_CALL context. */
1041 cxx_bind_parameters_in_call (const constexpr_ctx
*ctx
, tree t
,
1042 constexpr_call
*new_call
,
1043 bool *non_constant_p
, bool *overflow_p
)
1045 const int nargs
= call_expr_nargs (t
);
1046 tree fun
= new_call
->fundef
->decl
;
1047 tree parms
= DECL_ARGUMENTS (fun
);
1049 tree
*p
= &new_call
->bindings
;
1050 for (i
= 0; i
< nargs
; ++i
)
1053 tree type
= parms
? TREE_TYPE (parms
) : void_type_node
;
1054 x
= get_nth_callarg (t
, i
);
1055 /* For member function, the first argument is a pointer to the implied
1056 object. For a constructor, it might still be a dummy object, in
1057 which case we get the real argument from ctx. */
1058 if (i
== 0 && DECL_CONSTRUCTOR_P (fun
)
1059 && is_dummy_object (x
))
1062 x
= cp_build_addr_expr (x
, tf_warning_or_error
);
1065 if (parms
&& DECL_BY_REFERENCE (parms
) && !use_new_call
)
1067 /* cp_genericize made this a reference for argument passing, but
1068 we don't want to treat it like one for C++11 constexpr
1069 evaluation. C++14 constexpr evaluation uses the genericized
1071 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
1072 gcc_assert (TREE_CODE (TREE_TYPE (x
)) == REFERENCE_TYPE
);
1073 type
= TREE_TYPE (type
);
1074 x
= convert_from_reference (x
);
1077 arg
= cxx_eval_constant_expression (ctx
, x
, addr
,
1078 non_constant_p
, overflow_p
);
1079 /* Don't VERIFY_CONSTANT here. */
1080 if (*non_constant_p
&& ctx
->quiet
)
1082 /* Just discard ellipsis args after checking their constantitude. */
1085 if (*non_constant_p
)
1086 /* Don't try to adjust the type of non-constant args. */
1089 /* Make sure the binding has the same type as the parm. */
1090 if (TREE_CODE (type
) != REFERENCE_TYPE
)
1091 arg
= adjust_temp_type (type
, arg
);
1092 *p
= build_tree_list (parms
, arg
);
1093 p
= &TREE_CHAIN (*p
);
1095 parms
= TREE_CHAIN (parms
);
1099 /* Variables and functions to manage constexpr call expansion context.
1100 These do not need to be marked for PCH or GC. */
1102 /* FIXME remember and print actual constant arguments. */
1103 static vec
<tree
> call_stack
= vNULL
;
1104 static int call_stack_tick
;
1105 static int last_cx_error_tick
;
1108 push_cx_call_context (tree call
)
1111 if (!EXPR_HAS_LOCATION (call
))
1112 SET_EXPR_LOCATION (call
, input_location
);
1113 call_stack
.safe_push (call
);
1114 if (call_stack
.length () > (unsigned) max_constexpr_depth
)
1120 pop_cx_call_context (void)
1127 cx_error_context (void)
1129 vec
<tree
> r
= vNULL
;
1130 if (call_stack_tick
!= last_cx_error_tick
1131 && !call_stack
.is_empty ())
1133 last_cx_error_tick
= call_stack_tick
;
1137 /* Subroutine of cxx_eval_constant_expression.
1138 Evaluate the call expression tree T in the context of OLD_CALL expression
1142 cxx_eval_call_expression (const constexpr_ctx
*ctx
, tree t
,
1144 bool *non_constant_p
, bool *overflow_p
)
1146 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
1147 tree fun
= get_function_named_in_call (t
);
1149 constexpr_call new_call
= { NULL
, NULL
, NULL
, 0 };
1150 constexpr_call
**slot
;
1151 constexpr_call
*entry
;
1154 if (TREE_CODE (fun
) != FUNCTION_DECL
)
1156 /* Might be a constexpr function pointer. */
1157 fun
= cxx_eval_constant_expression (ctx
, fun
,
1158 /*addr*/false, non_constant_p
,
1161 if (TREE_CODE (fun
) == ADDR_EXPR
)
1162 fun
= TREE_OPERAND (fun
, 0);
1164 if (TREE_CODE (fun
) != FUNCTION_DECL
)
1166 if (!ctx
->quiet
&& !*non_constant_p
)
1167 error_at (loc
, "expression %qE does not designate a constexpr "
1169 *non_constant_p
= true;
1172 if (DECL_CLONED_FUNCTION_P (fun
))
1173 fun
= DECL_CLONED_FUNCTION (fun
);
1174 if (is_builtin_fn (fun
))
1175 return cxx_eval_builtin_function_call (ctx
, t
,
1176 addr
, non_constant_p
, overflow_p
);
1177 if (!DECL_DECLARED_CONSTEXPR_P (fun
))
1181 error_at (loc
, "call to non-constexpr function %qD", fun
);
1182 explain_invalid_constexpr_fn (fun
);
1184 *non_constant_p
= true;
1188 /* Shortcut trivial constructor/op=. */
1189 if (trivial_fn_p (fun
))
1191 if (call_expr_nargs (t
) == 2)
1193 tree arg
= convert_from_reference (get_nth_callarg (t
, 1));
1194 return cxx_eval_constant_expression (ctx
, arg
,
1195 addr
, non_constant_p
,
1198 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
1199 && AGGR_INIT_ZERO_FIRST (t
))
1200 return build_zero_init (DECL_CONTEXT (fun
), NULL_TREE
, false);
1203 /* If in direct recursive call, optimize definition search. */
1204 if (ctx
&& ctx
->call
&& ctx
->call
->fundef
->decl
== fun
)
1205 new_call
.fundef
= ctx
->call
->fundef
;
1208 new_call
.fundef
= retrieve_constexpr_fundef (fun
);
1209 if (new_call
.fundef
== NULL
|| new_call
.fundef
->body
== NULL
)
1213 if (DECL_INITIAL (fun
))
1215 /* The definition of fun was somehow unsuitable. */
1216 error_at (loc
, "%qD called in a constant expression", fun
);
1217 explain_invalid_constexpr_fn (fun
);
1220 error_at (loc
, "%qD used before its definition", fun
);
1222 *non_constant_p
= true;
1227 constexpr_ctx new_ctx
= *ctx
;
1228 if (DECL_CONSTRUCTOR_P (fun
) && !ctx
->object
1229 && TREE_CODE (t
) == AGGR_INIT_EXPR
)
1231 /* We want to have an initialization target for an AGGR_INIT_EXPR.
1232 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
1233 new_ctx
.object
= AGGR_INIT_EXPR_SLOT (t
);
1234 tree ctor
= new_ctx
.ctor
= build_constructor (DECL_CONTEXT (fun
), NULL
);
1235 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctor
) = true;
1236 ctx
->values
->put (new_ctx
.object
, ctor
);
1240 cxx_bind_parameters_in_call (ctx
, t
, &new_call
,
1241 non_constant_p
, overflow_p
);
1242 if (*non_constant_p
)
1245 depth_ok
= push_cx_call_context (t
);
1248 = iterative_hash_template_arg (new_call
.bindings
,
1249 constexpr_fundef_hasher::hash (new_call
.fundef
));
1251 /* If we have seen this call before, we are done. */
1252 maybe_initialize_constexpr_call_table ();
1253 slot
= constexpr_call_table
->find_slot (&new_call
, INSERT
);
1257 /* We need to keep a pointer to the entry, not just the slot, as the
1258 slot can move in the call to cxx_eval_builtin_function_call. */
1259 *slot
= entry
= ggc_alloc
<constexpr_call
> ();
1262 /* Calls which are in progress have their result set to NULL
1263 so that we can detect circular dependencies. */
1264 else if (entry
->result
== NULL
)
1267 error ("call has circular dependency");
1268 *non_constant_p
= true;
1269 entry
->result
= result
= error_mark_node
;
1275 error ("constexpr evaluation depth exceeds maximum of %d (use "
1276 "-fconstexpr-depth= to increase the maximum)",
1277 max_constexpr_depth
);
1278 *non_constant_p
= true;
1279 entry
->result
= result
= error_mark_node
;
1283 result
= entry
->result
;
1284 if (!result
|| result
== error_mark_node
)
1288 new_ctx
.call
= &new_call
;
1289 result
= (cxx_eval_constant_expression
1290 (&new_ctx
, new_call
.fundef
->body
,
1292 non_constant_p
, overflow_p
));
1296 if (DECL_SAVED_TREE (fun
) == NULL_TREE
1297 && (DECL_CONSTRUCTOR_P (fun
) || DECL_DESTRUCTOR_P (fun
)))
1298 /* The maybe-in-charge 'tor had its DECL_SAVED_TREE
1299 cleared, try the first clone. */
1300 fun
= DECL_CHAIN (fun
);
1301 gcc_assert (DECL_SAVED_TREE (fun
));
1304 /* Unshare the whole function body. */
1305 tree body
= copy_fn (fun
, parms
, res
);
1307 /* Associate the bindings with the remapped parms. */
1308 tree bound
= new_call
.bindings
;
1309 tree remapped
= parms
;
1312 tree oparm
= TREE_PURPOSE (bound
);
1313 tree arg
= TREE_VALUE (bound
);
1314 gcc_assert (DECL_NAME (remapped
) == DECL_NAME (oparm
));
1315 ctx
->values
->put (remapped
, arg
);
1316 bound
= TREE_CHAIN (bound
);
1317 remapped
= DECL_CHAIN (remapped
);
1319 /* Add the RESULT_DECL to the values map, too. */
1320 tree slot
= NULL_TREE
;
1321 if (DECL_BY_REFERENCE (res
))
1323 slot
= AGGR_INIT_EXPR_SLOT (t
);
1324 tree addr
= build_address (slot
);
1325 addr
= build_nop (TREE_TYPE (res
), addr
);
1326 ctx
->values
->put (res
, addr
);
1327 ctx
->values
->put (slot
, NULL_TREE
);
1330 ctx
->values
->put (res
, NULL_TREE
);
1332 tree jump_target
= NULL_TREE
;
1333 cxx_eval_constant_expression (ctx
, body
,
1334 addr
, non_constant_p
, overflow_p
,
1337 if (DECL_CONSTRUCTOR_P (fun
))
1338 /* This can be null for a subobject constructor call, in
1339 which case what we care about is the initialization
1340 side-effects rather than the value. We could get at the
1341 value by evaluating *this, but we don't bother; there's
1342 no need to put such a call in the hash table. */
1343 result
= addr
? ctx
->object
: ctx
->ctor
;
1346 result
= *ctx
->values
->get (slot
? slot
: res
);
1347 if (result
== NULL_TREE
)
1350 error ("constexpr call flows off the end "
1352 *non_constant_p
= true;
1356 /* Remove the parms/result from the values map. Is it worth
1357 bothering to do this when the map itself is only live for
1358 one constexpr evaluation? If so, maybe also clear out
1359 other vars from call, maybe in BIND_EXPR handling? */
1360 ctx
->values
->remove (res
);
1362 ctx
->values
->remove (slot
);
1363 for (tree parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
1364 ctx
->values
->remove (parm
);
1368 if (result
== error_mark_node
)
1369 *non_constant_p
= true;
1370 if (*non_constant_p
)
1371 entry
->result
= result
= error_mark_node
;
1374 /* If this was a call to initialize an object, set the type of
1375 the CONSTRUCTOR to the type of that object. */
1376 if (DECL_CONSTRUCTOR_P (fun
) && !use_new_call
)
1378 tree ob_arg
= get_nth_callarg (t
, 0);
1379 STRIP_NOPS (ob_arg
);
1380 gcc_assert (TYPE_PTR_P (TREE_TYPE (ob_arg
))
1381 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (ob_arg
))));
1382 result
= adjust_temp_type (TREE_TYPE (TREE_TYPE (ob_arg
)),
1385 entry
->result
= result
;
1391 pop_cx_call_context ();
1392 return unshare_expr (result
);
1395 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
1398 reduced_constant_expression_p (tree t
)
1400 switch (TREE_CODE (t
))
1403 /* Even if we can't lower this yet, it's constant. */
1407 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
1408 tree elt
; unsigned HOST_WIDE_INT idx
;
1409 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), idx
, elt
)
1410 if (!reduced_constant_expression_p (elt
))
1415 /* FIXME are we calling this too much? */
1416 return initializer_constant_valid_p (t
, TREE_TYPE (t
)) != NULL_TREE
;
1420 /* Some expressions may have constant operands but are not constant
1421 themselves, such as 1/0. Call this function (or rather, the macro
1422 following it) to check for that condition.
1424 We only call this in places that require an arithmetic constant, not in
1425 places where we might have a non-constant expression that can be a
1426 component of a constant expression, such as the address of a constexpr
1427 variable that might be dereferenced later. */
1430 verify_constant (tree t
, bool allow_non_constant
, bool *non_constant_p
,
1433 if (!*non_constant_p
&& !reduced_constant_expression_p (t
))
1435 if (!allow_non_constant
)
1436 error ("%q+E is not a constant expression", t
);
1437 *non_constant_p
= true;
1439 if (TREE_OVERFLOW_P (t
))
1441 if (!allow_non_constant
)
1443 permerror (input_location
, "overflow in constant expression");
1444 /* If we're being permissive (and are in an enforcing
1445 context), ignore the overflow. */
1446 if (flag_permissive
)
1447 return *non_constant_p
;
1451 return *non_constant_p
;
1454 /* Subroutine of cxx_eval_constant_expression.
1455 Attempt to reduce the unary expression tree T to a compile time value.
1456 If successful, return the value. Otherwise issue a diagnostic
1457 and return error_mark_node. */
1460 cxx_eval_unary_expression (const constexpr_ctx
*ctx
, tree t
,
1462 bool *non_constant_p
, bool *overflow_p
)
1465 tree orig_arg
= TREE_OPERAND (t
, 0);
1466 tree arg
= cxx_eval_constant_expression (ctx
, orig_arg
,
1467 addr
, non_constant_p
, overflow_p
);
1468 VERIFY_CONSTANT (arg
);
1469 location_t loc
= EXPR_LOCATION (t
);
1470 enum tree_code code
= TREE_CODE (t
);
1471 tree type
= TREE_TYPE (t
);
1472 r
= fold_unary_loc (loc
, code
, type
, arg
);
1475 if (arg
== orig_arg
)
1478 r
= build1_loc (loc
, code
, type
, arg
);
1480 VERIFY_CONSTANT (r
);
1484 /* Subroutine of cxx_eval_constant_expression.
1485 Like cxx_eval_unary_expression, except for binary expressions. */
1488 cxx_eval_binary_expression (const constexpr_ctx
*ctx
, tree t
,
1490 bool *non_constant_p
, bool *overflow_p
)
1493 tree orig_lhs
= TREE_OPERAND (t
, 0);
1494 tree orig_rhs
= TREE_OPERAND (t
, 1);
1496 lhs
= cxx_eval_constant_expression (ctx
, orig_lhs
,
1498 non_constant_p
, overflow_p
);
1499 VERIFY_CONSTANT (lhs
);
1500 rhs
= cxx_eval_constant_expression (ctx
, orig_rhs
,
1502 non_constant_p
, overflow_p
);
1503 VERIFY_CONSTANT (rhs
);
1505 location_t loc
= EXPR_LOCATION (t
);
1506 enum tree_code code
= TREE_CODE (t
);
1507 tree type
= TREE_TYPE (t
);
1508 r
= fold_binary_loc (loc
, code
, type
, lhs
, rhs
);
1511 if (lhs
== orig_lhs
&& rhs
== orig_rhs
)
1514 r
= build2_loc (loc
, code
, type
, lhs
, rhs
);
1516 VERIFY_CONSTANT (r
);
1520 /* Subroutine of cxx_eval_constant_expression.
1521 Attempt to evaluate condition expressions. Dead branches are not
1525 cxx_eval_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
1527 bool *non_constant_p
, bool *overflow_p
,
1530 tree val
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
1532 non_constant_p
, overflow_p
);
1533 VERIFY_CONSTANT (val
);
1534 /* Don't VERIFY_CONSTANT the other operands. */
1535 if (integer_zerop (val
))
1536 return cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 2),
1538 non_constant_p
, overflow_p
,
1540 return cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
1542 non_constant_p
, overflow_p
,
1546 /* Subroutine of cxx_eval_constant_expression.
1547 Attempt to reduce a reference to an array slot. */
1550 cxx_eval_array_reference (const constexpr_ctx
*ctx
, tree t
,
1552 bool *non_constant_p
, bool *overflow_p
)
1554 tree oldary
= TREE_OPERAND (t
, 0);
1555 tree ary
= cxx_eval_constant_expression (ctx
, oldary
,
1557 non_constant_p
, overflow_p
);
1561 unsigned len
, elem_nchars
= 1;
1562 if (*non_constant_p
)
1564 oldidx
= TREE_OPERAND (t
, 1);
1565 index
= cxx_eval_constant_expression (ctx
, oldidx
,
1567 non_constant_p
, overflow_p
);
1568 VERIFY_CONSTANT (index
);
1569 if (addr
&& ary
== oldary
&& index
== oldidx
)
1572 return build4 (ARRAY_REF
, TREE_TYPE (t
), ary
, index
, NULL
, NULL
);
1573 elem_type
= TREE_TYPE (TREE_TYPE (ary
));
1574 if (TREE_CODE (ary
) == CONSTRUCTOR
)
1575 len
= CONSTRUCTOR_NELTS (ary
);
1576 else if (TREE_CODE (ary
) == STRING_CST
)
1578 elem_nchars
= (TYPE_PRECISION (elem_type
)
1579 / TYPE_PRECISION (char_type_node
));
1580 len
= (unsigned) TREE_STRING_LENGTH (ary
) / elem_nchars
;
1584 /* We can't do anything with other tree codes, so use
1585 VERIFY_CONSTANT to complain and fail. */
1586 VERIFY_CONSTANT (ary
);
1589 if (compare_tree_int (index
, len
) >= 0)
1591 if (tree_int_cst_lt (index
, array_type_nelts_top (TREE_TYPE (ary
))))
1593 /* If it's within the array bounds but doesn't have an explicit
1594 initializer, it's value-initialized. */
1595 tree val
= build_value_init (elem_type
, tf_warning_or_error
);
1596 return cxx_eval_constant_expression (ctx
, val
,
1598 non_constant_p
, overflow_p
);
1602 error ("array subscript out of bound");
1603 *non_constant_p
= true;
1606 else if (tree_int_cst_lt (index
, integer_zero_node
))
1609 error ("negative array subscript");
1610 *non_constant_p
= true;
1613 i
= tree_to_shwi (index
);
1614 if (TREE_CODE (ary
) == CONSTRUCTOR
)
1615 return (*CONSTRUCTOR_ELTS (ary
))[i
].value
;
1616 else if (elem_nchars
== 1)
1617 return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary
))),
1618 TREE_STRING_POINTER (ary
)[i
]);
1621 tree type
= cv_unqualified (TREE_TYPE (TREE_TYPE (ary
)));
1622 return native_interpret_expr (type
, (const unsigned char *)
1623 TREE_STRING_POINTER (ary
)
1624 + i
* elem_nchars
, elem_nchars
);
1626 /* Don't VERIFY_CONSTANT here. */
1629 /* Subroutine of cxx_eval_constant_expression.
1630 Attempt to reduce a field access of a value of class type. */
1633 cxx_eval_component_reference (const constexpr_ctx
*ctx
, tree t
,
1635 bool *non_constant_p
, bool *overflow_p
)
1637 unsigned HOST_WIDE_INT i
;
1640 tree part
= TREE_OPERAND (t
, 1);
1641 tree orig_whole
= TREE_OPERAND (t
, 0);
1642 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
1644 non_constant_p
, overflow_p
);
1645 if (whole
== orig_whole
)
1648 return fold_build3 (COMPONENT_REF
, TREE_TYPE (t
),
1649 whole
, part
, NULL_TREE
);
1650 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
1652 if (!*non_constant_p
&& TREE_CODE (whole
) != CONSTRUCTOR
)
1655 error ("%qE is not a constant expression", orig_whole
);
1656 *non_constant_p
= true;
1658 if (DECL_MUTABLE_P (part
))
1661 error ("mutable %qD is not usable in a constant expression", part
);
1662 *non_constant_p
= true;
1664 if (*non_constant_p
)
1666 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
1673 /* We're in the middle of initializing it. */
1677 if (TREE_CODE (TREE_TYPE (whole
)) == UNION_TYPE
1678 && CONSTRUCTOR_NELTS (whole
) > 0)
1680 /* DR 1188 says we don't have to deal with this. */
1682 error ("accessing %qD member instead of initialized %qD member in "
1683 "constant expression", part
, CONSTRUCTOR_ELT (whole
, 0)->index
);
1684 *non_constant_p
= true;
1688 if (CONSTRUCTOR_NO_IMPLICIT_ZERO (whole
))
1690 /* 'whole' is part of the aggregate initializer we're currently
1691 building; if there's no initializer for this member yet, that's an
1694 error ("accessing uninitialized member %qD", part
);
1695 *non_constant_p
= true;
1699 /* If there's no explicit init for this field, it's value-initialized. */
1700 value
= build_value_init (TREE_TYPE (t
), tf_warning_or_error
);
1701 return cxx_eval_constant_expression (ctx
, value
,
1703 non_constant_p
, overflow_p
);
1706 /* Subroutine of cxx_eval_constant_expression.
1707 Attempt to reduce a field access of a value of class type that is
1708 expressed as a BIT_FIELD_REF. */
1711 cxx_eval_bit_field_ref (const constexpr_ctx
*ctx
, tree t
,
1713 bool *non_constant_p
, bool *overflow_p
)
1715 tree orig_whole
= TREE_OPERAND (t
, 0);
1716 tree retval
, fldval
, utype
, mask
;
1717 bool fld_seen
= false;
1718 HOST_WIDE_INT istart
, isize
;
1719 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
1721 non_constant_p
, overflow_p
);
1722 tree start
, field
, value
;
1723 unsigned HOST_WIDE_INT i
;
1725 if (whole
== orig_whole
)
1727 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
1729 if (!*non_constant_p
1730 && TREE_CODE (whole
) != VECTOR_CST
1731 && TREE_CODE (whole
) != CONSTRUCTOR
)
1734 error ("%qE is not a constant expression", orig_whole
);
1735 *non_constant_p
= true;
1737 if (*non_constant_p
)
1740 if (TREE_CODE (whole
) == VECTOR_CST
)
1741 return fold_ternary (BIT_FIELD_REF
, TREE_TYPE (t
), whole
,
1742 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 2));
1744 start
= TREE_OPERAND (t
, 2);
1745 istart
= tree_to_shwi (start
);
1746 isize
= tree_to_shwi (TREE_OPERAND (t
, 1));
1747 utype
= TREE_TYPE (t
);
1748 if (!TYPE_UNSIGNED (utype
))
1749 utype
= build_nonstandard_integer_type (TYPE_PRECISION (utype
), 1);
1750 retval
= build_int_cst (utype
, 0);
1751 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
1753 tree bitpos
= bit_position (field
);
1754 if (bitpos
== start
&& DECL_SIZE (field
) == TREE_OPERAND (t
, 1))
1756 if (TREE_CODE (TREE_TYPE (field
)) == INTEGER_TYPE
1757 && TREE_CODE (value
) == INTEGER_CST
1758 && tree_fits_shwi_p (bitpos
)
1759 && tree_fits_shwi_p (DECL_SIZE (field
)))
1761 HOST_WIDE_INT bit
= tree_to_shwi (bitpos
);
1762 HOST_WIDE_INT sz
= tree_to_shwi (DECL_SIZE (field
));
1763 HOST_WIDE_INT shift
;
1764 if (bit
>= istart
&& bit
+ sz
<= istart
+ isize
)
1766 fldval
= fold_convert (utype
, value
);
1767 mask
= build_int_cst_type (utype
, -1);
1768 mask
= fold_build2 (LSHIFT_EXPR
, utype
, mask
,
1769 size_int (TYPE_PRECISION (utype
) - sz
));
1770 mask
= fold_build2 (RSHIFT_EXPR
, utype
, mask
,
1771 size_int (TYPE_PRECISION (utype
) - sz
));
1772 fldval
= fold_build2 (BIT_AND_EXPR
, utype
, fldval
, mask
);
1773 shift
= bit
- istart
;
1774 if (BYTES_BIG_ENDIAN
)
1775 shift
= TYPE_PRECISION (utype
) - shift
- sz
;
1776 fldval
= fold_build2 (LSHIFT_EXPR
, utype
, fldval
,
1778 retval
= fold_build2 (BIT_IOR_EXPR
, utype
, retval
, fldval
);
1784 return fold_convert (TREE_TYPE (t
), retval
);
1786 return error_mark_node
;
1789 /* Subroutine of cxx_eval_constant_expression.
1790 Evaluate a short-circuited logical expression T in the context
1791 of a given constexpr CALL. BAILOUT_VALUE is the value for
1792 early return. CONTINUE_VALUE is used here purely for
1793 sanity check purposes. */
1796 cxx_eval_logical_expression (const constexpr_ctx
*ctx
, tree t
,
1797 tree bailout_value
, tree continue_value
,
1799 bool *non_constant_p
, bool *overflow_p
)
1802 tree lhs
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
1804 non_constant_p
, overflow_p
);
1805 VERIFY_CONSTANT (lhs
);
1806 if (tree_int_cst_equal (lhs
, bailout_value
))
1808 gcc_assert (tree_int_cst_equal (lhs
, continue_value
));
1809 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
1810 addr
, non_constant_p
,
1812 VERIFY_CONSTANT (r
);
1816 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
1817 CONSTRUCTOR elements to initialize (part of) an object containing that
1818 field. Return a pointer to the constructor_elt corresponding to the
1819 initialization of the field. */
1821 static constructor_elt
*
1822 base_field_constructor_elt (vec
<constructor_elt
, va_gc
> *v
, tree ref
)
1824 tree aggr
= TREE_OPERAND (ref
, 0);
1825 tree field
= TREE_OPERAND (ref
, 1);
1827 constructor_elt
*ce
;
1829 gcc_assert (TREE_CODE (ref
) == COMPONENT_REF
);
1831 if (TREE_CODE (aggr
) == COMPONENT_REF
)
1833 constructor_elt
*base_ce
1834 = base_field_constructor_elt (v
, aggr
);
1835 v
= CONSTRUCTOR_ELTS (base_ce
->value
);
1838 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
1839 if (ce
->index
== field
)
1846 /* Some of the expressions fed to the constexpr mechanism are calls to
1847 constructors, which have type void. In that case, return the type being
1848 initialized by the constructor. */
1851 initialized_type (tree t
)
1855 tree type
= cv_unqualified (TREE_TYPE (t
));
1856 if (TREE_CODE (t
) == CALL_EXPR
|| TREE_CODE (t
) == AGGR_INIT_EXPR
)
1858 /* A constructor call has void type, so we need to look deeper. */
1859 tree fn
= get_function_named_in_call (t
);
1860 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
1861 && DECL_CXX_CONSTRUCTOR_P (fn
))
1862 type
= DECL_CONTEXT (fn
);
1867 /* We're about to initialize element INDEX of an array or class from VALUE.
1868 Set up NEW_CTX appropriately by adjusting .object to refer to the
1869 subobject and creating a new CONSTRUCTOR if the element is itself
1870 a class or array. */
1873 init_subob_ctx (const constexpr_ctx
*ctx
, constexpr_ctx
&new_ctx
,
1874 tree index
, tree
&value
)
1878 if (index
&& TREE_CODE (index
) != INTEGER_CST
1879 && TREE_CODE (index
) != FIELD_DECL
)
1880 /* This won't have an element in the new CONSTRUCTOR. */
1883 tree type
= initialized_type (value
);
1884 if (!AGGREGATE_TYPE_P (type
) && !VECTOR_TYPE_P (type
))
1885 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
1888 /* The sub-aggregate initializer might contain a placeholder;
1889 update object to refer to the subobject and ctor to refer to
1890 the (newly created) sub-initializer. */
1892 new_ctx
.object
= build_ctor_subob_ref (index
, type
, ctx
->object
);
1893 tree elt
= build_constructor (type
, NULL
);
1894 CONSTRUCTOR_NO_IMPLICIT_ZERO (elt
) = true;
1897 if (TREE_CODE (value
) == TARGET_EXPR
)
1898 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
1899 value
= TARGET_EXPR_INITIAL (value
);
1902 /* We're about to process an initializer for a class or array TYPE. Make
1903 sure that CTX is set up appropriately. */
1906 verify_ctor_sanity (const constexpr_ctx
*ctx
, tree type
)
1908 /* We don't bother building a ctor for an empty base subobject. */
1909 if (is_empty_class (type
))
1912 /* We're in the middle of an initializer that might involve placeholders;
1913 our caller should have created a CONSTRUCTOR for us to put the
1914 initializer into. We will either return that constructor or T. */
1915 gcc_assert (ctx
->ctor
);
1916 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1917 (type
, TREE_TYPE (ctx
->ctor
)));
1918 gcc_assert (CONSTRUCTOR_NELTS (ctx
->ctor
) == 0);
1920 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1921 (type
, TREE_TYPE (ctx
->object
)));
1922 gcc_assert (!ctx
->object
|| !DECL_P (ctx
->object
)
1923 || *(ctx
->values
->get (ctx
->object
)) == ctx
->ctor
);
1926 /* Subroutine of cxx_eval_constant_expression.
1927 The expression tree T denotes a C-style array or a C-style
1928 aggregate. Reduce it to a constant expression. */
1931 cxx_eval_bare_aggregate (const constexpr_ctx
*ctx
, tree t
,
1933 bool *non_constant_p
, bool *overflow_p
)
1935 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
1936 bool changed
= false;
1937 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t
));
1939 verify_ctor_sanity (ctx
, TREE_TYPE (t
));
1940 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
1941 vec_alloc (*p
, vec_safe_length (v
));
1943 unsigned i
; tree index
, value
;
1944 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, index
, value
)
1946 constexpr_ctx new_ctx
;
1947 init_subob_ctx (ctx
, new_ctx
, index
, value
);
1948 if (new_ctx
.ctor
!= ctx
->ctor
)
1949 /* If we built a new CONSTRUCTOR, attach it now so that other
1950 initializers can refer to it. */
1951 CONSTRUCTOR_APPEND_ELT (*p
, index
, new_ctx
.ctor
);
1952 tree elt
= cxx_eval_constant_expression (&new_ctx
, value
,
1954 non_constant_p
, overflow_p
);
1955 /* Don't VERIFY_CONSTANT here. */
1956 if (ctx
->quiet
&& *non_constant_p
)
1960 if (index
&& TREE_CODE (index
) == COMPONENT_REF
)
1962 /* This is an initialization of a vfield inside a base
1963 subaggregate that we already initialized; push this
1964 initialization into the previous initialization. */
1965 constructor_elt
*inner
= base_field_constructor_elt (*p
, index
);
1970 && (TREE_CODE (index
) == NOP_EXPR
1971 || TREE_CODE (index
) == POINTER_PLUS_EXPR
))
1973 /* This is an initializer for an empty base; now that we've
1974 checked that it's constant, we can ignore it. */
1975 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index
))));
1978 else if (new_ctx
.ctor
!= ctx
->ctor
)
1980 /* We appended this element above; update the value. */
1981 gcc_assert ((*p
)->last().index
== index
);
1982 (*p
)->last().value
= elt
;
1985 CONSTRUCTOR_APPEND_ELT (*p
, index
, elt
);
1987 if (*non_constant_p
|| !changed
)
1990 /* We're done building this CONSTRUCTOR, so now we can interpret an
1991 element without an explicit initializer as value-initialized. */
1992 CONSTRUCTOR_NO_IMPLICIT_ZERO (t
) = false;
1993 if (TREE_CODE (TREE_TYPE (t
)) == VECTOR_TYPE
)
1998 /* Subroutine of cxx_eval_constant_expression.
1999 The expression tree T is a VEC_INIT_EXPR which denotes the desired
2000 initialization of a non-static data member of array type. Reduce it to a
2003 Note that apart from value-initialization (when VALUE_INIT is true),
2004 this is only intended to support value-initialization and the
2005 initializations done by defaulted constructors for classes with
2006 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
2007 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
2008 for the copy/move constructor. */
2011 cxx_eval_vec_init_1 (const constexpr_ctx
*ctx
, tree atype
, tree init
,
2012 bool value_init
, bool addr
,
2013 bool *non_constant_p
, bool *overflow_p
)
2015 tree elttype
= TREE_TYPE (atype
);
2016 int max
= tree_to_shwi (array_type_nelts (atype
));
2017 verify_ctor_sanity (ctx
, atype
);
2018 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
2019 vec_alloc (*p
, max
+ 1);
2020 bool pre_init
= false;
2023 /* For the default constructor, build up a call to the default
2024 constructor of the element type. We only need to handle class types
2025 here, as for a constructor to be constexpr, all members must be
2026 initialized, which for a defaulted default constructor means they must
2027 be of a class type with a constexpr default constructor. */
2028 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
2029 /* We only do this at the lowest level. */;
2030 else if (value_init
)
2032 init
= build_value_init (elttype
, tf_warning_or_error
);
2037 vec
<tree
, va_gc
> *argvec
= make_tree_vector ();
2038 init
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
2039 &argvec
, elttype
, LOOKUP_NORMAL
,
2040 tf_warning_or_error
);
2041 release_tree_vector (argvec
);
2042 init
= build_aggr_init_expr (TREE_TYPE (init
), init
);
2046 for (i
= 0; i
<= max
; ++i
)
2048 tree idx
= build_int_cst (size_type_node
, i
);
2050 constexpr_ctx new_ctx
;
2051 init_subob_ctx (ctx
, new_ctx
, idx
, pre_init
? init
: elttype
);
2052 if (new_ctx
.ctor
!= ctx
->ctor
)
2053 CONSTRUCTOR_APPEND_ELT (*p
, idx
, new_ctx
.ctor
);
2054 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
2056 /* A multidimensional array; recurse. */
2057 if (value_init
|| init
== NULL_TREE
)
2058 eltinit
= NULL_TREE
;
2060 eltinit
= cp_build_array_ref (input_location
, init
, idx
,
2061 tf_warning_or_error
);
2062 eltinit
= cxx_eval_vec_init_1 (&new_ctx
, elttype
, eltinit
, value_init
,
2064 non_constant_p
, overflow_p
);
2068 /* Initializing an element using value or default initialization
2069 we just pre-built above. */
2070 eltinit
= (cxx_eval_constant_expression
2072 addr
, non_constant_p
, overflow_p
));
2076 /* Copying an element. */
2077 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2078 (atype
, TREE_TYPE (init
)));
2079 eltinit
= cp_build_array_ref (input_location
, init
, idx
,
2080 tf_warning_or_error
);
2081 if (!real_lvalue_p (init
))
2082 eltinit
= move (eltinit
);
2083 eltinit
= force_rvalue (eltinit
, tf_warning_or_error
);
2084 eltinit
= (cxx_eval_constant_expression
2085 (&new_ctx
, eltinit
, addr
,
2086 non_constant_p
, overflow_p
));
2088 if (*non_constant_p
&& !ctx
->quiet
)
2090 if (new_ctx
.ctor
!= ctx
->ctor
)
2092 /* We appended this element above; update the value. */
2093 gcc_assert ((*p
)->last().index
== idx
);
2094 (*p
)->last().value
= eltinit
;
2097 CONSTRUCTOR_APPEND_ELT (*p
, idx
, eltinit
);
2100 if (!*non_constant_p
)
2103 CONSTRUCTOR_NO_IMPLICIT_ZERO (init
) = false;
2109 cxx_eval_vec_init (const constexpr_ctx
*ctx
, tree t
,
2111 bool *non_constant_p
, bool *overflow_p
)
2113 tree atype
= TREE_TYPE (t
);
2114 tree init
= VEC_INIT_EXPR_INIT (t
);
2115 tree r
= cxx_eval_vec_init_1 (ctx
, atype
, init
,
2116 VEC_INIT_EXPR_VALUE_INIT (t
),
2117 addr
, non_constant_p
, overflow_p
);
2118 if (*non_constant_p
)
2124 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
2125 match. We want to be less strict for simple *& folding; if we have a
2126 non-const temporary that we access through a const pointer, that should
2127 work. We handle this here rather than change fold_indirect_ref_1
2128 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
2129 don't really make sense outside of constant expression evaluation. Also
2130 we want to allow folding to COMPONENT_REF, which could cause trouble
2131 with TBAA in fold_indirect_ref_1.
2133 Try to keep this function synced with fold_indirect_ref_1. */
2136 cxx_fold_indirect_ref (location_t loc
, tree type
, tree op0
, bool *empty_base
)
2142 subtype
= TREE_TYPE (sub
);
2143 if (!POINTER_TYPE_P (subtype
))
2146 if (TREE_CODE (sub
) == ADDR_EXPR
)
2148 tree op
= TREE_OPERAND (sub
, 0);
2149 tree optype
= TREE_TYPE (op
);
2151 /* *&CONST_DECL -> to the value of the const decl. */
2152 if (TREE_CODE (op
) == CONST_DECL
)
2153 return DECL_INITIAL (op
);
2154 /* *&p => p; make sure to handle *&"str"[cst] here. */
2155 if (same_type_ignoring_top_level_qualifiers_p (optype
, type
))
2157 tree fop
= fold_read_from_constant_string (op
);
2163 /* *(foo *)&fooarray => fooarray[0] */
2164 else if (TREE_CODE (optype
) == ARRAY_TYPE
2165 && (same_type_ignoring_top_level_qualifiers_p
2166 (type
, TREE_TYPE (optype
))))
2168 tree type_domain
= TYPE_DOMAIN (optype
);
2169 tree min_val
= size_zero_node
;
2170 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
2171 min_val
= TYPE_MIN_VALUE (type_domain
);
2172 return build4_loc (loc
, ARRAY_REF
, type
, op
, min_val
,
2173 NULL_TREE
, NULL_TREE
);
2175 /* *(foo *)&complexfoo => __real__ complexfoo */
2176 else if (TREE_CODE (optype
) == COMPLEX_TYPE
2177 && (same_type_ignoring_top_level_qualifiers_p
2178 (type
, TREE_TYPE (optype
))))
2179 return fold_build1_loc (loc
, REALPART_EXPR
, type
, op
);
2180 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
2181 else if (TREE_CODE (optype
) == VECTOR_TYPE
2182 && (same_type_ignoring_top_level_qualifiers_p
2183 (type
, TREE_TYPE (optype
))))
2185 tree part_width
= TYPE_SIZE (type
);
2186 tree index
= bitsize_int (0);
2187 return fold_build3_loc (loc
, BIT_FIELD_REF
, type
, op
, part_width
, index
);
2189 /* Also handle conversion to an empty base class, which
2190 is represented with a NOP_EXPR. */
2191 else if (is_empty_class (type
)
2192 && CLASS_TYPE_P (optype
)
2193 && DERIVED_FROM_P (type
, optype
))
2198 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
2199 else if (RECORD_OR_UNION_TYPE_P (optype
))
2201 tree field
= TYPE_FIELDS (optype
);
2202 for (; field
; field
= DECL_CHAIN (field
))
2203 if (TREE_CODE (field
) == FIELD_DECL
2204 && integer_zerop (byte_position (field
))
2205 && (same_type_ignoring_top_level_qualifiers_p
2206 (TREE_TYPE (field
), type
)))
2208 return fold_build3 (COMPONENT_REF
, type
, op
, field
, NULL_TREE
);
2213 else if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
2214 && TREE_CODE (TREE_OPERAND (sub
, 1)) == INTEGER_CST
)
2216 tree op00
= TREE_OPERAND (sub
, 0);
2217 tree op01
= TREE_OPERAND (sub
, 1);
2220 if (TREE_CODE (op00
) == ADDR_EXPR
)
2223 op00
= TREE_OPERAND (op00
, 0);
2224 op00type
= TREE_TYPE (op00
);
2226 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
2227 if (TREE_CODE (op00type
) == VECTOR_TYPE
2228 && (same_type_ignoring_top_level_qualifiers_p
2229 (type
, TREE_TYPE (op00type
))))
2231 HOST_WIDE_INT offset
= tree_to_shwi (op01
);
2232 tree part_width
= TYPE_SIZE (type
);
2233 unsigned HOST_WIDE_INT part_widthi
= tree_to_shwi (part_width
)/BITS_PER_UNIT
;
2234 unsigned HOST_WIDE_INT indexi
= offset
* BITS_PER_UNIT
;
2235 tree index
= bitsize_int (indexi
);
2237 if (offset
/ part_widthi
< TYPE_VECTOR_SUBPARTS (op00type
))
2238 return fold_build3_loc (loc
,
2239 BIT_FIELD_REF
, type
, op00
,
2243 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
2244 else if (TREE_CODE (op00type
) == COMPLEX_TYPE
2245 && (same_type_ignoring_top_level_qualifiers_p
2246 (type
, TREE_TYPE (op00type
))))
2248 tree size
= TYPE_SIZE_UNIT (type
);
2249 if (tree_int_cst_equal (size
, op01
))
2250 return fold_build1_loc (loc
, IMAGPART_EXPR
, type
, op00
);
2252 /* ((foo *)&fooarray)[1] => fooarray[1] */
2253 else if (TREE_CODE (op00type
) == ARRAY_TYPE
2254 && (same_type_ignoring_top_level_qualifiers_p
2255 (type
, TREE_TYPE (op00type
))))
2257 tree type_domain
= TYPE_DOMAIN (op00type
);
2258 tree min_val
= size_zero_node
;
2259 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
2260 min_val
= TYPE_MIN_VALUE (type_domain
);
2261 op01
= size_binop_loc (loc
, EXACT_DIV_EXPR
, op01
,
2262 TYPE_SIZE_UNIT (type
));
2263 op01
= size_binop_loc (loc
, PLUS_EXPR
, op01
, min_val
);
2264 return build4_loc (loc
, ARRAY_REF
, type
, op00
, op01
,
2265 NULL_TREE
, NULL_TREE
);
2267 /* Also handle conversion to an empty base class, which
2268 is represented with a NOP_EXPR. */
2269 else if (is_empty_class (type
)
2270 && CLASS_TYPE_P (op00type
)
2271 && DERIVED_FROM_P (type
, op00type
))
2276 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
2277 else if (RECORD_OR_UNION_TYPE_P (op00type
))
2279 tree field
= TYPE_FIELDS (op00type
);
2280 for (; field
; field
= DECL_CHAIN (field
))
2281 if (TREE_CODE (field
) == FIELD_DECL
2282 && tree_int_cst_equal (byte_position (field
), op01
)
2283 && (same_type_ignoring_top_level_qualifiers_p
2284 (TREE_TYPE (field
), type
)))
2286 return fold_build3 (COMPONENT_REF
, type
, op00
,
2293 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
2294 else if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
2295 && (same_type_ignoring_top_level_qualifiers_p
2296 (type
, TREE_TYPE (TREE_TYPE (subtype
)))))
2299 tree min_val
= size_zero_node
;
2300 tree newsub
= cxx_fold_indirect_ref (loc
, TREE_TYPE (subtype
), sub
, NULL
);
2304 sub
= build1_loc (loc
, INDIRECT_REF
, TREE_TYPE (subtype
), sub
);
2305 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
2306 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
2307 min_val
= TYPE_MIN_VALUE (type_domain
);
2308 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
2316 cxx_eval_indirect_ref (const constexpr_ctx
*ctx
, tree t
,
2318 bool *non_constant_p
, bool *overflow_p
)
2320 tree orig_op0
= TREE_OPERAND (t
, 0);
2321 tree op0
= cxx_eval_constant_expression (ctx
, orig_op0
,
2322 /*addr*/false, non_constant_p
,
2324 bool empty_base
= false;
2327 /* Don't VERIFY_CONSTANT here. */
2328 if (*non_constant_p
)
2331 r
= cxx_fold_indirect_ref (EXPR_LOCATION (t
), TREE_TYPE (t
), op0
,
2335 r
= cxx_eval_constant_expression (ctx
, r
,
2336 addr
, non_constant_p
, overflow_p
);
2341 if (TREE_CODE (sub
) == ADDR_EXPR
)
2343 /* We couldn't fold to a constant value. Make sure it's not
2344 something we should have been able to fold. */
2345 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
2346 (TREE_TYPE (TREE_TYPE (sub
)), TREE_TYPE (t
)));
2347 /* DR 1188 says we don't have to deal with this. */
2349 error ("accessing value of %qE through a %qT glvalue in a "
2350 "constant expression", build_fold_indirect_ref (sub
),
2352 *non_constant_p
= true;
2357 /* If we're pulling out the value of an empty base, make sure
2358 that the whole object is constant and then return an empty
2360 if (empty_base
&& !addr
)
2362 VERIFY_CONSTANT (r
);
2363 r
= build_constructor (TREE_TYPE (t
), NULL
);
2364 TREE_CONSTANT (r
) = true;
2369 if (addr
&& op0
!= orig_op0
)
2370 return build1 (INDIRECT_REF
, TREE_TYPE (t
), op0
);
2372 VERIFY_CONSTANT (t
);
2378 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
2379 Shared between potential_constant_expression and
2380 cxx_eval_constant_expression. */
2383 non_const_var_error (tree r
)
2385 tree type
= TREE_TYPE (r
);
2386 error ("the value of %qD is not usable in a constant "
2388 /* Avoid error cascade. */
2389 if (DECL_INITIAL (r
) == error_mark_node
)
2391 if (DECL_DECLARED_CONSTEXPR_P (r
))
2392 inform (DECL_SOURCE_LOCATION (r
),
2393 "%qD used in its own initializer", r
);
2394 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
2396 if (!CP_TYPE_CONST_P (type
))
2397 inform (DECL_SOURCE_LOCATION (r
),
2398 "%q#D is not const", r
);
2399 else if (CP_TYPE_VOLATILE_P (type
))
2400 inform (DECL_SOURCE_LOCATION (r
),
2401 "%q#D is volatile", r
);
2402 else if (!DECL_INITIAL (r
)
2403 || !TREE_CONSTANT (DECL_INITIAL (r
)))
2404 inform (DECL_SOURCE_LOCATION (r
),
2405 "%qD was not initialized with a constant "
2412 if (cxx_dialect
>= cxx11
&& !DECL_DECLARED_CONSTEXPR_P (r
))
2413 inform (DECL_SOURCE_LOCATION (r
),
2414 "%qD was not declared %<constexpr%>", r
);
2416 inform (DECL_SOURCE_LOCATION (r
),
2417 "%qD does not have integral or enumeration type",
2422 /* Subroutine of cxx_eval_constant_expression.
2423 Like cxx_eval_unary_expression, except for trinary expressions. */
2426 cxx_eval_trinary_expression (const constexpr_ctx
*ctx
, tree t
,
2428 bool *non_constant_p
, bool *overflow_p
)
2434 for (i
= 0; i
< 3; i
++)
2436 args
[i
] = cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, i
),
2438 non_constant_p
, overflow_p
);
2439 VERIFY_CONSTANT (args
[i
]);
2442 val
= fold_ternary_loc (EXPR_LOCATION (t
), TREE_CODE (t
), TREE_TYPE (t
),
2443 args
[0], args
[1], args
[2]);
2444 if (val
== NULL_TREE
)
2446 VERIFY_CONSTANT (val
);
2451 var_in_constexpr_fn (tree t
)
2453 tree ctx
= DECL_CONTEXT (t
);
2454 return (cxx_dialect
>= cxx14
&& ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
2455 && DECL_DECLARED_CONSTEXPR_P (ctx
));
2458 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
2461 cxx_eval_store_expression (const constexpr_ctx
*ctx
, tree t
,
2463 bool *non_constant_p
, bool *overflow_p
)
2465 constexpr_ctx new_ctx
= *ctx
;
2467 /* First we figure out where we're storing to. */
2468 tree target
= TREE_OPERAND (t
, 0);
2469 target
= cxx_eval_constant_expression (ctx
, target
,
2471 non_constant_p
, overflow_p
);
2472 if (*non_constant_p
)
2475 /* And then find the underlying variable. */
2476 vec
<tree
,va_gc
> *refs
= make_tree_vector();
2477 tree object
= NULL_TREE
;
2478 for (tree probe
= target
; object
== NULL_TREE
; )
2480 switch (TREE_CODE (probe
))
2485 vec_safe_push (refs
, TREE_OPERAND (probe
, 1));
2486 vec_safe_push (refs
, TREE_TYPE (probe
));
2487 probe
= TREE_OPERAND (probe
, 0);
2492 gcc_assert (DECL_P (object
));
2496 /* And then find/build up our initializer for the path to the subobject
2497 we're initializing. */
2498 tree
*valp
= ctx
->values
->get (object
);
2501 /* A constant-expression cannot modify objects from outside the
2502 constant-expression. */
2504 error ("modification of %qD is not a constant-expression", object
);
2505 *non_constant_p
= true;
2508 tree type
= TREE_TYPE (object
);
2509 while (!refs
->is_empty())
2511 if (*valp
== NULL_TREE
)
2513 *valp
= build_constructor (type
, NULL
);
2514 CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp
) = true;
2519 ce
.index
= refs
->pop();
2520 ce
.value
= NULL_TREE
;
2522 unsigned HOST_WIDE_INT idx
= 0;
2523 constructor_elt
*cep
= NULL
;
2525 vec_safe_iterate (CONSTRUCTOR_ELTS (*valp
), idx
, &cep
);
2528 if (cp_tree_equal (ce
.index
, cep
->index
))
2531 cep
= vec_safe_push (CONSTRUCTOR_ELTS (*valp
), ce
);
2534 release_tree_vector (refs
);
2536 if ((AGGREGATE_TYPE_P (TREE_TYPE (t
)) || VECTOR_TYPE_P (TREE_TYPE (t
))))
2538 /* Create a new CONSTRUCTOR in case evaluation of the initializer
2539 wants to modify it. */
2540 *valp
= new_ctx
.ctor
= build_constructor (TREE_TYPE (t
), NULL
);
2541 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx
.ctor
) = true;
2542 new_ctx
.object
= target
;
2545 tree init
= cxx_eval_constant_expression (&new_ctx
, TREE_OPERAND (t
, 1),
2547 non_constant_p
, overflow_p
);
2548 if (target
== object
)
2549 /* The hash table might have moved since the get earlier. */
2550 ctx
->values
->put (object
, init
);
2554 if (*non_constant_p
)
2562 /* Evaluate a ++ or -- expression. */
2565 cxx_eval_increment_expression (const constexpr_ctx
*ctx
, tree t
,
2567 bool *non_constant_p
, bool *overflow_p
)
2569 enum tree_code code
= TREE_CODE (t
);
2570 tree type
= TREE_TYPE (t
);
2571 tree op
= TREE_OPERAND (t
, 0);
2572 tree offset
= TREE_OPERAND (t
, 1);
2573 gcc_assert (TREE_CONSTANT (offset
));
2575 /* The operand as an lvalue. */
2576 op
= cxx_eval_constant_expression (ctx
, op
, true,
2577 non_constant_p
, overflow_p
);
2579 /* The operand as an rvalue. */
2580 tree val
= rvalue (op
);
2581 val
= cxx_eval_constant_expression (ctx
, val
, false,
2582 non_constant_p
, overflow_p
);
2583 VERIFY_CONSTANT (val
);
2585 /* The modified value. */
2586 bool inc
= (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
);
2588 if (POINTER_TYPE_P (type
))
2590 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
2591 offset
= convert_to_ptrofftype (offset
);
2593 offset
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (offset
), offset
);
2594 mod
= fold_build2 (POINTER_PLUS_EXPR
, type
, val
, offset
);
2597 mod
= fold_build2 (inc
? PLUS_EXPR
: MINUS_EXPR
, type
, val
, offset
);
2598 VERIFY_CONSTANT (mod
);
2600 /* Storing the modified value. */
2601 tree store
= build2 (MODIFY_EXPR
, type
, op
, mod
);
2602 cxx_eval_constant_expression (ctx
, store
,
2603 true, non_constant_p
, overflow_p
);
2605 /* And the value of the expression. */
2606 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
2608 /* Prefix ops are lvalues. */
2612 /* But we optimize when the caller wants an rvalue. */
2616 /* Postfix ops are rvalues. */
2620 /* Predicates for the meaning of *jump_target. */
2623 returns (tree
*jump_target
)
2626 && TREE_CODE (*jump_target
) == RETURN_EXPR
;
2630 breaks (tree
*jump_target
)
2633 && TREE_CODE (*jump_target
) == LABEL_DECL
2634 && LABEL_DECL_BREAK (*jump_target
);
2638 continues (tree
*jump_target
)
2641 && TREE_CODE (*jump_target
) == LABEL_DECL
2642 && LABEL_DECL_CONTINUE (*jump_target
);
2646 switches (tree
*jump_target
)
2649 && TREE_CODE (*jump_target
) == INTEGER_CST
;
2652 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
2653 at I matches *jump_target. If we're looking for a case label and we see
2654 the default label, copy I into DEFAULT_LABEL. */
2657 label_matches (tree
*jump_target
, tree_stmt_iterator i
,
2658 tree_stmt_iterator
& default_label
)
2660 tree stmt
= tsi_stmt (i
);
2661 switch (TREE_CODE (*jump_target
))
2664 if (TREE_CODE (stmt
) == LABEL_EXPR
2665 && LABEL_EXPR_LABEL (stmt
) == *jump_target
)
2670 if (TREE_CODE (stmt
) == CASE_LABEL_EXPR
)
2672 if (!CASE_LOW (stmt
))
2674 else if (tree_int_cst_equal (*jump_target
, CASE_LOW (stmt
)))
2685 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
2686 semantics, for switch, break, continue, and return. */
2689 cxx_eval_statement_list (const constexpr_ctx
*ctx
, tree t
,
2690 bool *non_constant_p
, bool *overflow_p
,
2693 tree_stmt_iterator i
;
2694 tree_stmt_iterator default_label
= tree_stmt_iterator();
2696 /* In a statement-expression we want to return the last value. */
2700 local_target
= NULL_TREE
;
2701 jump_target
= &local_target
;
2703 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
2706 tree stmt
= tsi_stmt (i
);
2709 if (TREE_CODE (stmt
) == STATEMENT_LIST
)
2710 /* The label we want might be inside. */;
2711 else if (label_matches (jump_target
, i
, default_label
))
2713 *jump_target
= NULL_TREE
;
2717 r
= cxx_eval_constant_expression (ctx
, stmt
, false,
2718 non_constant_p
, overflow_p
,
2720 if (*non_constant_p
)
2722 if (returns (jump_target
) || breaks (jump_target
))
2725 if (switches (jump_target
) && !tsi_end_p (default_label
))
2728 *jump_target
= NULL_TREE
;
2734 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
2735 semantics; continue semantics are covered by cxx_eval_statement_list. */
2738 cxx_eval_loop_expr (const constexpr_ctx
*ctx
, tree t
,
2739 bool *non_constant_p
, bool *overflow_p
,
2742 tree body
= TREE_OPERAND (t
, 0);
2745 cxx_eval_statement_list (ctx
, body
,
2746 non_constant_p
, overflow_p
, jump_target
);
2747 if (returns (jump_target
) || breaks (jump_target
))
2750 if (breaks (jump_target
))
2751 *jump_target
= NULL_TREE
;
2755 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
2759 cxx_eval_switch_expr (const constexpr_ctx
*ctx
, tree t
,
2760 bool *non_constant_p
, bool *overflow_p
,
2763 tree cond
= TREE_OPERAND (t
, 0);
2764 cond
= cxx_eval_constant_expression (ctx
, cond
, false,
2765 non_constant_p
, overflow_p
);
2766 VERIFY_CONSTANT (cond
);
2767 *jump_target
= cond
;
2769 tree body
= TREE_OPERAND (t
, 1);
2770 cxx_eval_statement_list (ctx
, body
,
2771 non_constant_p
, overflow_p
, jump_target
);
2772 if (breaks (jump_target
) || switches (jump_target
))
2773 *jump_target
= NULL_TREE
;
2777 /* Attempt to reduce the expression T to a constant value.
2778 On failure, issue diagnostic and return error_mark_node. */
2779 /* FIXME unify with c_fully_fold */
2780 /* FIXME overflow_p is too global */
2783 cxx_eval_constant_expression (const constexpr_ctx
*ctx
, tree t
,
2785 bool *non_constant_p
, bool *overflow_p
,
2788 constexpr_ctx new_ctx
;
2791 if (t
== error_mark_node
)
2793 *non_constant_p
= true;
2796 if (CONSTANT_CLASS_P (t
))
2798 if (TREE_CODE (t
) == PTRMEM_CST
)
2799 t
= cplus_expand_constant (t
);
2800 else if (TREE_OVERFLOW (t
) && (!flag_permissive
|| ctx
->quiet
))
2804 if (TREE_CODE (t
) != NOP_EXPR
2805 && reduced_constant_expression_p (t
))
2808 switch (TREE_CODE (t
))
2813 /* We ask for an rvalue for the RESULT_DECL when indirecting
2814 through an invisible reference. */
2815 gcc_assert (DECL_BY_REFERENCE (t
));
2816 return (*ctx
->values
->get (t
));
2821 /* else fall through. */
2824 r
= decl_really_constant_value (t
);
2826 r
= decl_constant_value (t
);
2827 if (TREE_CODE (r
) == TARGET_EXPR
2828 && TREE_CODE (TARGET_EXPR_INITIAL (r
)) == CONSTRUCTOR
)
2829 r
= TARGET_EXPR_INITIAL (r
);
2830 if (TREE_CODE (r
) == VAR_DECL
)
2831 if (tree
*p
= ctx
->values
->get (r
))
2836 non_const_var_error (r
);
2837 *non_constant_p
= true;
2845 case CASE_LABEL_EXPR
:
2849 if (!use_new_call
&& ctx
2850 && ctx
->call
&& DECL_CONTEXT (t
) == ctx
->call
->fundef
->decl
)
2851 r
= lookup_parameter_binding (ctx
->call
, t
);
2852 else if (addr
&& TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
2854 else if (tree
*p
= ctx
->values
->get (r
))
2857 /* Defer in case this is only used for its type. */;
2858 else if (TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
)
2859 /* Defer, there's no lvalue->rvalue conversion. */;
2860 else if (is_empty_class (TREE_TYPE (t
)))
2862 /* If the class is empty, we aren't actually loading anything. */
2863 r
= build_constructor (TREE_TYPE (t
), NULL
);
2864 TREE_CONSTANT (r
) = true;
2869 error ("%qE is not a constant expression", t
);
2870 *non_constant_p
= true;
2875 case AGGR_INIT_EXPR
:
2876 r
= cxx_eval_call_expression (ctx
, t
, addr
,
2877 non_constant_p
, overflow_p
);
2882 r
= DECL_EXPR_DECL (t
);
2883 if (AGGREGATE_TYPE_P (TREE_TYPE (r
))
2884 || VECTOR_TYPE_P (TREE_TYPE (r
)))
2888 new_ctx
.ctor
= build_constructor (TREE_TYPE (r
), NULL
);
2889 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx
.ctor
) = true;
2890 new_ctx
.values
->put (r
, new_ctx
.ctor
);
2894 if (tree init
= DECL_INITIAL (r
))
2896 init
= cxx_eval_constant_expression (ctx
, init
,
2898 non_constant_p
, overflow_p
);
2899 ctx
->values
->put (r
, init
);
2901 else if (ctx
== &new_ctx
)
2902 /* We gave it a CONSTRUCTOR above. */;
2904 ctx
->values
->put (r
, NULL_TREE
);
2909 if (!literal_type_p (TREE_TYPE (t
)))
2913 error ("temporary of non-literal type %qT in a "
2914 "constant expression", TREE_TYPE (t
));
2915 explain_non_literal_class (TREE_TYPE (t
));
2917 *non_constant_p
= true;
2920 if ((AGGREGATE_TYPE_P (TREE_TYPE (t
)) || VECTOR_TYPE_P (TREE_TYPE (t
))))
2922 /* We're being expanded without an explicit target, so start
2923 initializing a new object; expansion with an explicit target
2924 strips the TARGET_EXPR before we get here. */
2926 new_ctx
.ctor
= build_constructor (TREE_TYPE (t
), NULL
);
2927 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx
.ctor
) = true;
2928 new_ctx
.object
= TARGET_EXPR_SLOT (t
);
2929 ctx
->values
->put (new_ctx
.object
, new_ctx
.ctor
);
2932 /* Pass false for 'addr' because this indicates
2933 initialization of a temporary. */
2934 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
2936 non_constant_p
, overflow_p
);
2937 if (!*non_constant_p
)
2938 /* Adjust the type of the result to the type of the temporary. */
2939 r
= adjust_temp_type (TREE_TYPE (t
), r
);
2942 tree slot
= TARGET_EXPR_SLOT (t
);
2943 ctx
->values
->put (slot
, r
);
2951 /* In C++11 constexpr evaluation we are looking for the value,
2952 not the side-effect of the initialization. */
2953 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
2955 non_constant_p
, overflow_p
);
2958 /* else fall through */
2960 r
= cxx_eval_store_expression (ctx
, t
, addr
,
2961 non_constant_p
, overflow_p
);
2965 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
2967 non_constant_p
, overflow_p
);
2971 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
2973 non_constant_p
, overflow_p
);
2977 case NON_LVALUE_EXPR
:
2978 case TRY_CATCH_EXPR
:
2979 case CLEANUP_POINT_EXPR
:
2980 case MUST_NOT_THROW_EXPR
:
2984 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
2986 non_constant_p
, overflow_p
,
2990 /* These differ from cxx_eval_unary_expression in that this doesn't
2991 check for a constant operand or result; an address can be
2992 constant without its operand being, and vice versa. */
2994 r
= cxx_eval_indirect_ref (ctx
, t
, addr
,
2995 non_constant_p
, overflow_p
);
3000 tree oldop
= TREE_OPERAND (t
, 0);
3001 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
3003 non_constant_p
, overflow_p
);
3004 /* Don't VERIFY_CONSTANT here. */
3005 if (*non_constant_p
)
3007 gcc_checking_assert (TREE_CODE (op
) != CONSTRUCTOR
);
3008 /* This function does more aggressive folding than fold itself. */
3009 r
= build_fold_addr_expr_with_type (op
, TREE_TYPE (t
));
3010 if (TREE_CODE (r
) == ADDR_EXPR
&& TREE_OPERAND (r
, 0) == oldop
)
3018 case FIX_TRUNC_EXPR
:
3023 case TRUTH_NOT_EXPR
:
3024 case FIXED_CONVERT_EXPR
:
3025 r
= cxx_eval_unary_expression (ctx
, t
, addr
,
3026 non_constant_p
, overflow_p
);
3030 if (SIZEOF_EXPR_TYPE_P (t
))
3031 r
= cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t
, 0)),
3032 SIZEOF_EXPR
, false);
3033 else if (TYPE_P (TREE_OPERAND (t
, 0)))
3034 r
= cxx_sizeof_or_alignof_type (TREE_OPERAND (t
, 0), SIZEOF_EXPR
,
3037 r
= cxx_sizeof_or_alignof_expr (TREE_OPERAND (t
, 0), SIZEOF_EXPR
,
3039 if (r
== error_mark_node
)
3041 VERIFY_CONSTANT (r
);
3046 /* check_return_expr sometimes wraps a TARGET_EXPR in a
3047 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
3048 introduced by build_call_a. */
3049 tree op0
= TREE_OPERAND (t
, 0);
3050 tree op1
= TREE_OPERAND (t
, 1);
3052 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
3053 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
3054 r
= cxx_eval_constant_expression (ctx
, op0
,
3055 addr
, non_constant_p
, overflow_p
,
3059 /* Check that the LHS is constant and then discard it. */
3060 cxx_eval_constant_expression (ctx
, op0
,
3061 false, non_constant_p
, overflow_p
,
3063 op1
= TREE_OPERAND (t
, 1);
3064 r
= cxx_eval_constant_expression (ctx
, op1
,
3065 addr
, non_constant_p
, overflow_p
,
3071 case POINTER_PLUS_EXPR
:
3075 case TRUNC_DIV_EXPR
:
3077 case FLOOR_DIV_EXPR
:
3078 case ROUND_DIV_EXPR
:
3079 case TRUNC_MOD_EXPR
:
3081 case ROUND_MOD_EXPR
:
3083 case EXACT_DIV_EXPR
:
3093 case TRUTH_XOR_EXPR
:
3100 case UNORDERED_EXPR
:
3110 r
= cxx_eval_binary_expression (ctx
, t
, addr
,
3111 non_constant_p
, overflow_p
);
3114 /* fold can introduce non-IF versions of these; still treat them as
3115 short-circuiting. */
3116 case TRUTH_AND_EXPR
:
3117 case TRUTH_ANDIF_EXPR
:
3118 r
= cxx_eval_logical_expression (ctx
, t
, boolean_false_node
,
3121 non_constant_p
, overflow_p
);
3125 case TRUTH_ORIF_EXPR
:
3126 r
= cxx_eval_logical_expression (ctx
, t
, boolean_true_node
,
3129 non_constant_p
, overflow_p
);
3133 r
= cxx_eval_array_reference (ctx
, t
, addr
,
3134 non_constant_p
, overflow_p
);
3138 if (is_overloaded_fn (t
))
3140 /* We can only get here in checking mode via
3141 build_non_dependent_expr, because any expression that
3142 calls or takes the address of the function will have
3143 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
3144 gcc_checking_assert (ctx
->quiet
|| errorcount
);
3145 *non_constant_p
= true;
3148 r
= cxx_eval_component_reference (ctx
, t
, addr
,
3149 non_constant_p
, overflow_p
);
3153 r
= cxx_eval_bit_field_ref (ctx
, t
, addr
,
3154 non_constant_p
, overflow_p
);
3159 r
= cxx_eval_conditional_expression (ctx
, t
, addr
,
3160 non_constant_p
, overflow_p
,
3165 r
= cxx_eval_bare_aggregate (ctx
, t
, addr
,
3166 non_constant_p
, overflow_p
);
3170 /* We can get this in a defaulted constructor for a class with a
3171 non-static data member of array type. Either the initializer will
3172 be NULL, meaning default-initialization, or it will be an lvalue
3173 or xvalue of the same type, meaning direct-initialization from the
3174 corresponding member. */
3175 r
= cxx_eval_vec_init (ctx
, t
, addr
,
3176 non_constant_p
, overflow_p
);
3181 r
= cxx_eval_trinary_expression (ctx
, t
, addr
,
3182 non_constant_p
, overflow_p
);
3186 case VIEW_CONVERT_EXPR
:
3189 tree oldop
= TREE_OPERAND (t
, 0);
3190 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
3192 non_constant_p
, overflow_p
);
3193 if (*non_constant_p
)
3195 if (POINTER_TYPE_P (TREE_TYPE (t
))
3196 && TREE_CODE (op
) == INTEGER_CST
3197 && !integer_zerop (op
))
3200 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3201 "reinterpret_cast from integer to pointer");
3202 *non_constant_p
= true;
3206 /* We didn't fold at the top so we could check for ptr-int
3209 r
= fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), op
);
3210 /* Conversion of an out-of-range value has implementation-defined
3211 behavior; the language considers it different from arithmetic
3212 overflow, which is undefined. */
3213 if (TREE_OVERFLOW_P (r
) && !TREE_OVERFLOW_P (op
))
3214 TREE_OVERFLOW (r
) = false;
3218 case EMPTY_CLASS_EXPR
:
3219 /* This is good enough for a function argument that might not get
3220 used, and they can't do anything with it, so just return it. */
3223 case STATEMENT_LIST
:
3225 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
3226 return cxx_eval_statement_list (&new_ctx
, t
,
3227 non_constant_p
, overflow_p
, jump_target
);
3230 return cxx_eval_constant_expression (ctx
, BIND_EXPR_BODY (t
),
3232 non_constant_p
, overflow_p
,
3235 case PREINCREMENT_EXPR
:
3236 case POSTINCREMENT_EXPR
:
3237 case PREDECREMENT_EXPR
:
3238 case POSTDECREMENT_EXPR
:
3239 return cxx_eval_increment_expression (ctx
, t
,
3240 addr
, non_constant_p
, overflow_p
);
3246 case VEC_DELETE_EXPR
:
3249 /* GCC internal stuff. */
3252 case WITH_CLEANUP_EXPR
:
3253 case NON_DEPENDENT_EXPR
:
3257 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3258 "expression %qE is not a constant-expression", t
);
3259 *non_constant_p
= true;
3262 case PLACEHOLDER_EXPR
:
3263 if (!ctx
|| !ctx
->ctor
|| (addr
&& !ctx
->object
))
3265 /* A placeholder without a referent. We can get here when
3266 checking whether NSDMIs are noexcept, or in massage_init_elt;
3267 just say it's non-constant for now. */
3268 gcc_assert (ctx
->quiet
);
3269 *non_constant_p
= true;
3274 /* Use of the value or address of the current object. We could
3275 use ctx->object unconditionally, but using ctx->ctor when we
3276 can is a minor optimization. */
3277 tree ctor
= addr
? ctx
->object
: ctx
->ctor
;
3278 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3279 (TREE_TYPE (t
), TREE_TYPE (ctor
)));
3280 return cxx_eval_constant_expression
3282 non_constant_p
, overflow_p
);
3287 *jump_target
= TREE_OPERAND (t
, 0);
3288 gcc_assert (breaks (jump_target
) || continues (jump_target
));
3292 cxx_eval_loop_expr (ctx
, t
,
3293 non_constant_p
, overflow_p
, jump_target
);
3297 cxx_eval_switch_expr (ctx
, t
,
3298 non_constant_p
, overflow_p
, jump_target
);
3302 internal_error ("unexpected expression %qE of kind %s", t
,
3303 get_tree_code_name (TREE_CODE (t
)));
3304 *non_constant_p
= true;
3308 if (r
== error_mark_node
)
3309 *non_constant_p
= true;
3311 if (*non_constant_p
)
3318 cxx_eval_outermost_constant_expr (tree t
, bool allow_non_constant
,
3319 bool strict
= true, tree object
= NULL_TREE
)
3321 bool non_constant_p
= false;
3322 bool overflow_p
= false;
3323 constexpr_ctx ctx
= { NULL
, NULL
, NULL
, NULL
, allow_non_constant
, strict
};
3324 hash_map
<tree
,tree
> map
;
3326 tree type
= initialized_type (t
);
3328 if (AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
))
3330 /* In C++14 an NSDMI can participate in aggregate initialization,
3331 and can refer to the address of the object being initialized, so
3332 we need to pass in the relevant VAR_DECL if we want to do the
3333 evaluation in a single pass. The evaluation will dynamically
3334 update ctx.values for the VAR_DECL. We use the same strategy
3335 for C++11 constexpr constructors that refer to the object being
3337 ctx
.ctor
= build_constructor (type
, NULL
);
3338 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctx
.ctor
) = true;
3341 if (TREE_CODE (t
) == TARGET_EXPR
)
3342 object
= TARGET_EXPR_SLOT (t
);
3343 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
3344 object
= AGGR_INIT_EXPR_SLOT (t
);
3346 ctx
.object
= object
;
3348 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3349 (type
, TREE_TYPE (object
)));
3350 if (object
&& DECL_P (object
))
3351 map
.put (object
, ctx
.ctor
);
3352 if (TREE_CODE (r
) == TARGET_EXPR
)
3353 /* Avoid creating another CONSTRUCTOR when we expand the
3355 r
= TARGET_EXPR_INITIAL (r
);
3358 r
= cxx_eval_constant_expression (&ctx
, r
,
3359 false, &non_constant_p
, &overflow_p
);
3361 verify_constant (r
, allow_non_constant
, &non_constant_p
, &overflow_p
);
3363 /* Mutable logic is a bit tricky: we want to allow initialization of
3364 constexpr variables with mutable members, but we can't copy those
3365 members to another constexpr variable. */
3366 if (TREE_CODE (r
) == CONSTRUCTOR
3367 && CONSTRUCTOR_MUTABLE_POISON (r
))
3369 if (!allow_non_constant
)
3370 error ("%qE is not a constant expression because it refers to "
3371 "mutable subobjects of %qT", t
, type
);
3372 non_constant_p
= true;
3375 /* Technically we should check this for all subexpressions, but that
3376 runs into problems with our internal representation of pointer
3377 subtraction and the 5.19 rules are still in flux. */
3378 if (CONVERT_EXPR_CODE_P (TREE_CODE (r
))
3379 && ARITHMETIC_TYPE_P (TREE_TYPE (r
))
3380 && TREE_CODE (TREE_OPERAND (r
, 0)) == ADDR_EXPR
)
3382 if (!allow_non_constant
)
3383 error ("conversion from pointer type %qT "
3384 "to arithmetic type %qT in a constant-expression",
3385 TREE_TYPE (TREE_OPERAND (r
, 0)), TREE_TYPE (r
));
3386 non_constant_p
= true;
3389 if (!non_constant_p
&& overflow_p
)
3390 non_constant_p
= true;
3392 if (non_constant_p
&& !allow_non_constant
)
3393 return error_mark_node
;
3394 else if (non_constant_p
&& TREE_CONSTANT (r
))
3396 /* This isn't actually constant, so unset TREE_CONSTANT. */
3399 else if (TREE_CODE (r
) == CONSTRUCTOR
)
3400 r
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (r
), r
);
3402 r
= build_nop (TREE_TYPE (r
), r
);
3403 TREE_CONSTANT (r
) = false;
3405 else if (non_constant_p
|| r
== t
)
3408 if (TREE_CODE (r
) == CONSTRUCTOR
&& CLASS_TYPE_P (TREE_TYPE (r
)))
3410 if (TREE_CODE (t
) == TARGET_EXPR
3411 && TARGET_EXPR_INITIAL (t
) == r
)
3415 r
= get_target_expr (r
);
3416 TREE_CONSTANT (r
) = true;
3424 /* Returns true if T is a valid subexpression of a constant expression,
3425 even if it isn't itself a constant expression. */
3428 is_sub_constant_expr (tree t
)
3430 bool non_constant_p
= false;
3431 bool overflow_p
= false;
3432 constexpr_ctx ctx
= { NULL
, NULL
, NULL
, NULL
, true, true };
3433 hash_map
<tree
, tree
> map
;
3435 cxx_eval_constant_expression (&ctx
, t
, false, &non_constant_p
,
3437 return !non_constant_p
&& !overflow_p
;
3440 /* If T represents a constant expression returns its reduced value.
3441 Otherwise return error_mark_node. If T is dependent, then
3445 cxx_constant_value (tree t
, tree decl
)
3447 return cxx_eval_outermost_constant_expr (t
, false, true, decl
);
3450 /* If T is a constant expression, returns its reduced value.
3451 Otherwise, if T does not have TREE_CONSTANT set, returns T.
3452 Otherwise, returns a version of T without TREE_CONSTANT. */
3455 maybe_constant_value (tree t
, tree decl
)
3459 if (instantiation_dependent_expression_p (t
)
3460 || type_unknown_p (t
)
3461 || BRACE_ENCLOSED_INITIALIZER_P (t
)
3462 || !potential_constant_expression (t
))
3464 if (TREE_OVERFLOW_P (t
))
3466 t
= build_nop (TREE_TYPE (t
), t
);
3467 TREE_CONSTANT (t
) = false;
3472 r
= cxx_eval_outermost_constant_expr (t
, true, true, decl
);
3473 #ifdef ENABLE_CHECKING
3474 /* cp_tree_equal looks through NOPs, so allow them. */
3476 || CONVERT_EXPR_P (t
)
3477 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
3478 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
3479 || !cp_tree_equal (r
, t
));
3484 /* Like maybe_constant_value but first fully instantiate the argument.
3486 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
3487 (t, tf_none) followed by maybe_constant_value but is more efficient,
3488 because calls instantiation_dependent_expression_p and
3489 potential_constant_expression at most once. */
3492 fold_non_dependent_expr (tree t
)
3497 /* If we're in a template, but T isn't value dependent, simplify
3498 it. We're supposed to treat:
3500 template <typename T> void f(T[1 + 1]);
3501 template <typename T> void f(T[2]);
3503 as two declarations of the same function, for example. */
3504 if (processing_template_decl
)
3506 if (!instantiation_dependent_expression_p (t
)
3507 && potential_constant_expression (t
))
3509 processing_template_decl_sentinel s
;
3510 t
= instantiate_non_dependent_expr_internal (t
, tf_none
);
3512 if (type_unknown_p (t
)
3513 || BRACE_ENCLOSED_INITIALIZER_P (t
))
3515 if (TREE_OVERFLOW_P (t
))
3517 t
= build_nop (TREE_TYPE (t
), t
);
3518 TREE_CONSTANT (t
) = false;
3523 tree r
= cxx_eval_outermost_constant_expr (t
, true, true, NULL_TREE
);
3524 #ifdef ENABLE_CHECKING
3525 /* cp_tree_equal looks through NOPs, so allow them. */
3527 || CONVERT_EXPR_P (t
)
3528 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
3529 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
3530 || !cp_tree_equal (r
, t
));
3534 else if (TREE_OVERFLOW_P (t
))
3536 t
= build_nop (TREE_TYPE (t
), t
);
3537 TREE_CONSTANT (t
) = false;
3542 return maybe_constant_value (t
);
3545 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
3546 than wrapped in a TARGET_EXPR. */
3549 maybe_constant_init (tree t
, tree decl
)
3551 if (TREE_CODE (t
) == EXPR_STMT
)
3552 t
= TREE_OPERAND (t
, 0);
3553 if (TREE_CODE (t
) == CONVERT_EXPR
3554 && VOID_TYPE_P (TREE_TYPE (t
)))
3555 t
= TREE_OPERAND (t
, 0);
3556 if (TREE_CODE (t
) == INIT_EXPR
)
3557 t
= TREE_OPERAND (t
, 1);
3558 if (instantiation_dependent_expression_p (t
)
3559 || type_unknown_p (t
)
3560 || BRACE_ENCLOSED_INITIALIZER_P (t
)
3561 || !potential_static_init_expression (t
))
3562 /* Don't try to evaluate it. */;
3564 t
= cxx_eval_outermost_constant_expr (t
, true, false, decl
);
3565 if (TREE_CODE (t
) == TARGET_EXPR
)
3567 tree init
= TARGET_EXPR_INITIAL (t
);
3568 if (TREE_CODE (init
) == CONSTRUCTOR
)
3575 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
3576 /* Return true if the object referred to by REF has automatic or thread
3579 enum { ck_ok
, ck_bad
, ck_unknown
};
3581 check_automatic_or_tls (tree ref
)
3584 HOST_WIDE_INT bitsize
, bitpos
;
3586 int volatilep
= 0, unsignedp
= 0;
3587 tree decl
= get_inner_reference (ref
, &bitsize
, &bitpos
, &offset
,
3588 &mode
, &unsignedp
, &volatilep
, false);
3591 /* If there isn't a decl in the middle, we don't know the linkage here,
3592 and this isn't a constant expression anyway. */
3595 dk
= decl_storage_duration (decl
);
3596 return (dk
== dk_auto
|| dk
== dk_thread
) ? ck_bad
: ck_ok
;
3600 /* Return true if T denotes a potentially constant expression. Issue
3601 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
3602 an lvalue-rvalue conversion is implied.
3604 C++0x [expr.const] used to say
3606 6 An expression is a potential constant expression if it is
3607 a constant expression where all occurrences of function
3608 parameters are replaced by arbitrary constant expressions
3609 of the appropriate type.
3611 2 A conditional expression is a constant expression unless it
3612 involves one of the following as a potentially evaluated
3613 subexpression (3.2), but subexpressions of logical AND (5.14),
3614 logical OR (5.15), and conditional (5.16) operations that are
3615 not evaluated are not considered. */
3618 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
,
3619 tsubst_flags_t flags
)
3621 #define RECUR(T,RV) potential_constant_expression_1 ((T), (RV), strict, flags)
3622 enum { any
= false, rval
= true };
3626 if (t
== error_mark_node
)
3630 if (TREE_THIS_VOLATILE (t
))
3632 if (flags
& tf_error
)
3633 error ("expression %qE has side-effects", t
);
3636 if (CONSTANT_CLASS_P (t
))
3639 switch (TREE_CODE (t
))
3645 case TEMPLATE_ID_EXPR
:
3648 case CASE_LABEL_EXPR
:
3654 case TEMPLATE_PARM_INDEX
:
3656 case IDENTIFIER_NODE
:
3657 case USERDEF_LITERAL
:
3658 /* We can see a FIELD_DECL in a pointer-to-member expression. */
3663 case PLACEHOLDER_EXPR
:
3668 case AGGR_INIT_EXPR
:
3670 /* -- an invocation of a function other than a constexpr function
3671 or a constexpr constructor. */
3673 tree fun
= get_function_named_in_call (t
);
3674 const int nargs
= call_expr_nargs (t
);
3677 if (fun
== NULL_TREE
)
3679 /* fold_call_expr can't do anything with IFN calls. */
3680 if (flags
& tf_error
)
3681 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3682 "call to internal function");
3685 if (is_overloaded_fn (fun
))
3687 if (TREE_CODE (fun
) == FUNCTION_DECL
)
3689 if (builtin_valid_in_constant_expr_p (fun
))
3691 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
3692 /* Allow any built-in function; if the expansion
3693 isn't constant, we'll deal with that then. */
3694 && !is_builtin_fn (fun
))
3696 if (flags
& tf_error
)
3698 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3699 "call to non-constexpr function %qD", fun
);
3700 explain_invalid_constexpr_fn (fun
);
3704 /* A call to a non-static member function takes the address
3705 of the object as the first argument. But in a constant
3706 expression the address will be folded away, so look
3708 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
3709 && !DECL_CONSTRUCTOR_P (fun
))
3711 tree x
= get_nth_callarg (t
, 0);
3712 if (is_this_parameter (x
))
3714 else if (!RECUR (x
, rval
))
3721 if (!RECUR (fun
, true))
3723 fun
= get_first_fn (fun
);
3725 /* Skip initial arguments to base constructors. */
3726 if (DECL_BASE_CONSTRUCTOR_P (fun
))
3727 i
= num_artificial_parms_for (fun
);
3728 fun
= DECL_ORIGIN (fun
);
3732 if (RECUR (fun
, rval
))
3733 /* Might end up being a constant function pointer. */;
3737 for (; i
< nargs
; ++i
)
3739 tree x
= get_nth_callarg (t
, i
);
3740 if (!RECUR (x
, rval
))
3746 case NON_LVALUE_EXPR
:
3747 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
3748 -- an lvalue of integral type that refers to a non-volatile
3749 const variable or static data member initialized with
3750 constant expressions, or
3752 -- an lvalue of literal type that refers to non-volatile
3753 object defined with constexpr, or that refers to a
3754 sub-object of such an object; */
3755 return RECUR (TREE_OPERAND (t
, 0), rval
);
3759 && !decl_constant_var_p (t
)
3761 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t
))
3762 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t
))
3763 && !var_in_constexpr_fn (t
)
3764 && !dependent_type_p (TREE_TYPE (t
)))
3766 if (flags
& tf_error
)
3767 non_const_var_error (t
);
3774 case VIEW_CONVERT_EXPR
:
3775 /* -- a reinterpret_cast. FIXME not implemented, and this rule
3776 may change to something more specific to type-punning (DR 1312). */
3778 tree from
= TREE_OPERAND (t
, 0);
3779 if (POINTER_TYPE_P (TREE_TYPE (t
))
3780 && TREE_CODE (from
) == INTEGER_CST
3781 && !integer_zerop (from
))
3783 if (flags
& tf_error
)
3784 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3785 "reinterpret_cast from integer to pointer");
3788 return (RECUR (from
, TREE_CODE (t
) != VIEW_CONVERT_EXPR
));
3792 /* -- a unary operator & that is applied to an lvalue that
3793 designates an object with thread or automatic storage
3795 t
= TREE_OPERAND (t
, 0);
3797 if (TREE_CODE (t
) == OFFSET_REF
&& PTRMEM_OK_P (t
))
3798 /* A pointer-to-member constant. */
3802 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
3803 any checking here, as we might dereference the pointer later. If
3804 we remove this code, also remove check_automatic_or_tls. */
3805 i
= check_automatic_or_tls (t
);
3810 if (flags
& tf_error
)
3811 error ("address-of an object %qE with thread local or "
3812 "automatic storage is not a constant expression", t
);
3816 return RECUR (t
, any
);
3822 /* -- a class member access unless its postfix-expression is
3823 of literal type or of pointer to literal type. */
3824 /* This test would be redundant, as it follows from the
3825 postfix-expression being a potential constant expression. */
3826 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
3828 case EXPR_PACK_EXPANSION
:
3829 return RECUR (PACK_EXPANSION_PATTERN (t
), want_rval
);
3833 tree x
= TREE_OPERAND (t
, 0);
3835 if (is_this_parameter (x
))
3837 if (DECL_CONTEXT (x
)
3838 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x
)))
3840 if (flags
& tf_error
)
3841 error ("use of %<this%> in a constant expression");
3846 return RECUR (x
, rval
);
3849 case STATEMENT_LIST
:
3851 tree_stmt_iterator i
;
3852 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
3854 if (!RECUR (tsi_stmt (i
), any
))
3862 if (cxx_dialect
< cxx14
)
3864 if (!RECUR (TREE_OPERAND (t
, 0), any
))
3866 if (!RECUR (TREE_OPERAND (t
, 1), rval
))
3871 if (cxx_dialect
< cxx14
)
3873 if (!RECUR (TREE_OPERAND (t
, 0), rval
))
3875 if (!RECUR (TREE_OPERAND (t
, 2), rval
))
3880 if (!RECUR (IF_COND (t
), rval
))
3882 if (!RECUR (THEN_CLAUSE (t
), any
))
3884 if (!RECUR (ELSE_CLAUSE (t
), any
))
3889 if (!RECUR (DO_COND (t
), rval
))
3891 if (!RECUR (DO_BODY (t
), any
))
3896 if (!RECUR (FOR_INIT_STMT (t
), any
))
3898 if (!RECUR (FOR_COND (t
), rval
))
3900 if (!RECUR (FOR_EXPR (t
), any
))
3902 if (!RECUR (FOR_BODY (t
), any
))
3907 if (!RECUR (WHILE_COND (t
), rval
))
3909 if (!RECUR (WHILE_BODY (t
), any
))
3914 if (!RECUR (SWITCH_STMT_COND (t
), rval
))
3916 if (!RECUR (SWITCH_STMT_BODY (t
), any
))
3921 return RECUR (STMT_EXPR_STMT (t
), rval
);
3924 case DYNAMIC_CAST_EXPR
:
3925 case PSEUDO_DTOR_EXPR
:
3929 case VEC_DELETE_EXPR
:
3932 case OMP_ATOMIC_READ
:
3933 case OMP_ATOMIC_CAPTURE_OLD
:
3934 case OMP_ATOMIC_CAPTURE_NEW
:
3935 /* GCC internal stuff. */
3938 case TRANSACTION_EXPR
:
3941 if (flags
& tf_error
)
3942 error ("expression %qE is not a constant-expression", t
);
3946 /* -- a typeid expression whose operand is of polymorphic
3949 tree e
= TREE_OPERAND (t
, 0);
3950 if (!TYPE_P (e
) && !type_dependent_expression_p (e
)
3951 && TYPE_POLYMORPHIC_P (TREE_TYPE (e
)))
3953 if (flags
& tf_error
)
3954 error ("typeid-expression is not a constant expression "
3955 "because %qE is of polymorphic type", e
);
3962 /* -- a subtraction where both operands are pointers. */
3963 if (TYPE_PTR_P (TREE_OPERAND (t
, 0))
3964 && TYPE_PTR_P (TREE_OPERAND (t
, 1)))
3966 if (flags
& tf_error
)
3967 error ("difference of two pointer expressions is not "
3968 "a constant expression");
3980 /* -- a relational or equality operator where at least
3981 one of the operands is a pointer. */
3982 if (TYPE_PTR_P (TREE_OPERAND (t
, 0))
3983 || TYPE_PTR_P (TREE_OPERAND (t
, 1)))
3985 if (flags
& tf_error
)
3986 error ("pointer comparison expression is not a "
3987 "constant expression");
3993 case PREINCREMENT_EXPR
:
3994 case POSTINCREMENT_EXPR
:
3995 case PREDECREMENT_EXPR
:
3996 case POSTDECREMENT_EXPR
:
3997 if (cxx_dialect
< cxx14
)
4003 if (TYPE_P (TREE_OPERAND (t
, 0)))
4005 /* else fall through. */
4011 case FIX_TRUNC_EXPR
:
4015 case TRUTH_NOT_EXPR
:
4016 case FIXED_CONVERT_EXPR
:
4017 case UNARY_PLUS_EXPR
:
4019 return RECUR (TREE_OPERAND (t
, 0), rval
);
4022 case CONST_CAST_EXPR
:
4023 case STATIC_CAST_EXPR
:
4024 case REINTERPRET_CAST_EXPR
:
4025 case IMPLICIT_CONV_EXPR
:
4026 if (cxx_dialect
< cxx11
4027 && !dependent_type_p (TREE_TYPE (t
))
4028 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
)))
4029 /* In C++98, a conversion to non-integral type can't be part of a
4030 constant expression. */
4032 if (flags
& tf_error
)
4033 error ("cast to non-integral type %qT in a constant expression",
4038 return (RECUR (TREE_OPERAND (t
, 0),
4039 TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
));
4042 return RECUR (BIND_EXPR_BODY (t
), want_rval
);
4044 case WITH_CLEANUP_EXPR
:
4045 case CLEANUP_POINT_EXPR
:
4046 case MUST_NOT_THROW_EXPR
:
4047 case TRY_CATCH_EXPR
:
4052 case NON_DEPENDENT_EXPR
:
4053 /* For convenience. */
4055 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
4058 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
4061 if (!literal_type_p (TREE_TYPE (t
)))
4063 if (flags
& tf_error
)
4065 error ("temporary of non-literal type %qT in a "
4066 "constant expression", TREE_TYPE (t
));
4067 explain_non_literal_class (TREE_TYPE (t
));
4072 return RECUR (TREE_OPERAND (t
, 1), rval
);
4076 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
4077 constructor_elt
*ce
;
4078 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
4079 if (!RECUR (ce
->value
, want_rval
))
4086 gcc_assert (TREE_PURPOSE (t
) == NULL_TREE
4087 || DECL_P (TREE_PURPOSE (t
)));
4088 if (!RECUR (TREE_VALUE (t
), want_rval
))
4090 if (TREE_CHAIN (t
) == NULL_TREE
)
4092 return RECUR (TREE_CHAIN (t
), want_rval
);
4095 case TRUNC_DIV_EXPR
:
4097 case FLOOR_DIV_EXPR
:
4098 case ROUND_DIV_EXPR
:
4099 case TRUNC_MOD_EXPR
:
4101 case ROUND_MOD_EXPR
:
4103 tree denom
= TREE_OPERAND (t
, 1);
4104 if (!RECUR (denom
, rval
))
4106 /* We can't call cxx_eval_outermost_constant_expr on an expression
4107 that hasn't been through instantiate_non_dependent_expr yet. */
4108 if (!processing_template_decl
)
4109 denom
= cxx_eval_outermost_constant_expr (denom
, true);
4110 if (integer_zerop (denom
))
4112 if (flags
& tf_error
)
4113 error ("division by zero is not a constant-expression");
4119 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
4125 /* check_return_expr sometimes wraps a TARGET_EXPR in a
4126 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
4127 introduced by build_call_a. */
4128 tree op0
= TREE_OPERAND (t
, 0);
4129 tree op1
= TREE_OPERAND (t
, 1);
4131 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
4132 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
4133 return RECUR (op0
, want_rval
);
4138 /* If the first operand is the non-short-circuit constant, look at
4139 the second operand; otherwise we only care about the first one for
4141 case TRUTH_AND_EXPR
:
4142 case TRUTH_ANDIF_EXPR
:
4143 tmp
= boolean_true_node
;
4146 case TRUTH_ORIF_EXPR
:
4147 tmp
= boolean_false_node
;
4150 tree op
= TREE_OPERAND (t
, 0);
4151 if (!RECUR (op
, rval
))
4153 if (!processing_template_decl
)
4154 op
= cxx_eval_outermost_constant_expr (op
, true);
4155 if (tree_int_cst_equal (op
, tmp
))
4156 return RECUR (TREE_OPERAND (t
, 1), rval
);
4163 case POINTER_PLUS_EXPR
:
4165 case EXACT_DIV_EXPR
:
4175 case TRUTH_XOR_EXPR
:
4176 case UNORDERED_EXPR
:
4189 case ARRAY_RANGE_REF
:
4193 for (i
= 0; i
< 2; ++i
)
4194 if (!RECUR (TREE_OPERAND (t
, i
), want_rval
))
4198 case CILK_SYNC_STMT
:
4199 case CILK_SPAWN_STMT
:
4200 case ARRAY_NOTATION_REF
:
4205 for (i
= 0; i
< 3; ++i
)
4206 if (!RECUR (TREE_OPERAND (t
, i
), true))
4212 /* If the condition is a known constant, we know which of the legs we
4213 care about; otherwise we only require that the condition and
4214 either of the legs be potentially constant. */
4215 tmp
= TREE_OPERAND (t
, 0);
4216 if (!RECUR (tmp
, rval
))
4218 if (!processing_template_decl
)
4219 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
4220 if (integer_zerop (tmp
))
4221 return RECUR (TREE_OPERAND (t
, 2), want_rval
);
4222 else if (TREE_CODE (tmp
) == INTEGER_CST
)
4223 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
4224 for (i
= 1; i
< 3; ++i
)
4225 if (potential_constant_expression_1 (TREE_OPERAND (t
, i
),
4226 want_rval
, strict
, tf_none
))
4228 if (flags
& tf_error
)
4229 error ("expression %qE is not a constant-expression", t
);
4233 if (VEC_INIT_EXPR_IS_CONSTEXPR (t
))
4235 if (flags
& tf_error
)
4237 error ("non-constant array initialization");
4238 diagnose_non_constexpr_vec_init (t
);
4243 if (objc_is_property_ref (t
))
4246 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t
)));
4253 /* The main entry point to the above. */
4256 potential_constant_expression (tree t
)
4258 return potential_constant_expression_1 (t
, false, true, tf_none
);
4262 potential_static_init_expression (tree t
)
4264 return potential_constant_expression_1 (t
, false, false, tf_none
);
4267 /* As above, but require a constant rvalue. */
4270 potential_rvalue_constant_expression (tree t
)
4272 return potential_constant_expression_1 (t
, true, true, tf_none
);
4275 /* Like above, but complain about non-constant expressions. */
4278 require_potential_constant_expression (tree t
)
4280 return potential_constant_expression_1 (t
, false, true, tf_warning_or_error
);
4283 /* Cross product of the above. */
4286 require_potential_rvalue_constant_expression (tree t
)
4288 return potential_constant_expression_1 (t
, true, true, tf_warning_or_error
);
4291 #include "gt-cp-constexpr.h"