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), allow_non_constant, 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));
619 if (TREE_CODE (DECL_EXPR_DECL (body
)) == USING_DECL
)
621 return error_mark_node
;
623 case CLEANUP_POINT_EXPR
:
624 return constexpr_fn_retval (TREE_OPERAND (body
, 0));
627 if (!check_constexpr_bind_expr_vars (body
))
628 return error_mark_node
;
629 return constexpr_fn_retval (BIND_EXPR_BODY (body
));
635 return error_mark_node
;
639 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
640 FUN; do the necessary transformations to turn it into a single expression
641 that we can store in the hash table. */
644 massage_constexpr_body (tree fun
, tree body
)
646 if (DECL_CONSTRUCTOR_P (fun
))
647 body
= build_constexpr_constructor_member_initializers
648 (DECL_CONTEXT (fun
), body
);
649 else if (cxx_dialect
< cxx14
)
651 if (TREE_CODE (body
) == EH_SPEC_BLOCK
)
652 body
= EH_SPEC_STMTS (body
);
653 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
654 body
= TREE_OPERAND (body
, 0);
655 body
= constexpr_fn_retval (body
);
660 /* FUN is a constexpr constructor with massaged body BODY. Return true
661 if some bases/fields are uninitialized, and complain if COMPLAIN. */
664 cx_check_missing_mem_inits (tree fun
, tree body
, bool complain
)
671 if (TREE_CODE (body
) != CONSTRUCTOR
)
674 nelts
= CONSTRUCTOR_NELTS (body
);
675 ctype
= DECL_CONTEXT (fun
);
676 field
= TYPE_FIELDS (ctype
);
678 if (TREE_CODE (ctype
) == UNION_TYPE
)
680 if (nelts
== 0 && next_initializable_field (field
))
683 error ("%<constexpr%> constructor for union %qT must "
684 "initialize exactly one non-static data member", ctype
);
691 for (i
= 0; i
<= nelts
; ++i
)
698 index
= CONSTRUCTOR_ELT (body
, i
)->index
;
699 /* Skip base and vtable inits. */
700 if (TREE_CODE (index
) != FIELD_DECL
701 || DECL_ARTIFICIAL (index
))
704 for (; field
!= index
; field
= DECL_CHAIN (field
))
707 if (TREE_CODE (field
) != FIELD_DECL
708 || (DECL_C_BIT_FIELD (field
) && !DECL_NAME (field
))
709 || DECL_ARTIFICIAL (field
))
711 ftype
= strip_array_types (TREE_TYPE (field
));
712 if (type_has_constexpr_default_constructor (ftype
))
714 /* It's OK to skip a member with a trivial constexpr ctor.
715 A constexpr ctor that isn't trivial should have been
717 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype
)
723 error ("member %qD must be initialized by mem-initializer "
724 "in %<constexpr%> constructor", field
);
725 inform (DECL_SOURCE_LOCATION (field
), "declared here");
728 if (field
== NULL_TREE
)
730 field
= DECL_CHAIN (field
);
736 /* We are processing the definition of the constexpr function FUN.
737 Check that its BODY fulfills the propriate requirements and
738 enter it in the constexpr function definition table.
739 For constructor BODY is actually the TREE_LIST of the
740 member-initializer list. */
743 register_constexpr_fundef (tree fun
, tree body
)
745 constexpr_fundef entry
;
746 constexpr_fundef
**slot
;
748 if (!is_valid_constexpr_fn (fun
, !DECL_GENERATED_P (fun
)))
751 body
= massage_constexpr_body (fun
, body
);
752 if (body
== NULL_TREE
|| body
== error_mark_node
)
754 if (!DECL_CONSTRUCTOR_P (fun
))
755 error ("body of constexpr function %qD not a return-statement", fun
);
759 if (!potential_rvalue_constant_expression (body
))
761 if (!DECL_GENERATED_P (fun
))
762 require_potential_rvalue_constant_expression (body
);
766 if (DECL_CONSTRUCTOR_P (fun
)
767 && cx_check_missing_mem_inits (fun
, body
, !DECL_GENERATED_P (fun
)))
770 /* Create the constexpr function table if necessary. */
771 if (constexpr_fundef_table
== NULL
)
772 constexpr_fundef_table
773 = hash_table
<constexpr_fundef_hasher
>::create_ggc (101);
777 slot
= constexpr_fundef_table
->find_slot (&entry
, INSERT
);
779 gcc_assert (*slot
== NULL
);
780 *slot
= ggc_alloc
<constexpr_fundef
> ();
786 /* FUN is a non-constexpr function called in a context that requires a
787 constant expression. If it comes from a constexpr template, explain why
788 the instantiation isn't constexpr. */
791 explain_invalid_constexpr_fn (tree fun
)
793 static hash_set
<tree
> *diagnosed
;
796 /* Only diagnose defaulted functions or instantiations. */
797 if (!DECL_DEFAULTED_FN (fun
)
798 && !is_instantiation_of_constexpr (fun
))
800 if (diagnosed
== NULL
)
801 diagnosed
= new hash_set
<tree
>;
802 if (diagnosed
->add (fun
))
803 /* Already explained. */
806 save_loc
= input_location
;
807 input_location
= DECL_SOURCE_LOCATION (fun
);
808 inform (0, "%q+D is not usable as a constexpr function because:", fun
);
809 /* First check the declaration. */
810 if (is_valid_constexpr_fn (fun
, true))
812 /* Then if it's OK, the body. */
813 if (!DECL_DECLARED_CONSTEXPR_P (fun
))
814 explain_implicit_non_constexpr (fun
);
817 body
= massage_constexpr_body (fun
, DECL_SAVED_TREE (fun
));
818 require_potential_rvalue_constant_expression (body
);
819 if (DECL_CONSTRUCTOR_P (fun
))
820 cx_check_missing_mem_inits (fun
, body
, true);
823 input_location
= save_loc
;
826 /* Objects of this type represent calls to constexpr functions
827 along with the bindings of parameters to their arguments, for
828 the purpose of compile time evaluation. */
830 struct GTY((for_user
)) constexpr_call
{
831 /* Description of the constexpr function definition. */
832 constexpr_fundef
*fundef
;
833 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
834 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
835 Note: This arrangement is made to accommodate the use of
836 iterative_hash_template_arg (see pt.c). If you change this
837 representation, also change the hash calculation in
838 cxx_eval_call_expression. */
840 /* Result of the call.
841 NULL means the call is being evaluated.
842 error_mark_node means that the evaluation was erroneous;
843 otherwise, the actuall value of the call. */
845 /* The hash of this call; we remember it here to avoid having to
846 recalculate it when expanding the hash table. */
850 struct constexpr_call_hasher
: ggc_hasher
<constexpr_call
*>
852 static hashval_t
hash (constexpr_call
*);
853 static bool equal (constexpr_call
*, constexpr_call
*);
856 /* The constexpr expansion context. CALL is the current function
857 expansion, CTOR is the current aggregate initializer, OBJECT is the
858 object being initialized by CTOR, either a VAR_DECL or a _REF. VALUES
859 is a map of values of variables initialized within the expression. */
861 struct constexpr_ctx
{
862 constexpr_call
*call
;
863 hash_map
<tree
,tree
> *values
;
868 /* A table of all constexpr calls that have been evaluated by the
869 compiler in this translation unit. */
871 static GTY (()) hash_table
<constexpr_call_hasher
> *constexpr_call_table
;
873 static tree
cxx_eval_constant_expression (const constexpr_ctx
*, tree
,
874 bool, bool, bool *, bool *, tree
*);
876 /* Compute a hash value for a constexpr call representation. */
879 constexpr_call_hasher::hash (constexpr_call
*info
)
884 /* Return true if the objects pointed to by P and Q represent calls
885 to the same constexpr function with the same arguments.
886 Otherwise, return false. */
889 constexpr_call_hasher::equal (constexpr_call
*lhs
, constexpr_call
*rhs
)
895 if (!constexpr_fundef_hasher::equal (lhs
->fundef
, rhs
->fundef
))
897 lhs_bindings
= lhs
->bindings
;
898 rhs_bindings
= rhs
->bindings
;
899 while (lhs_bindings
!= NULL
&& rhs_bindings
!= NULL
)
901 tree lhs_arg
= TREE_VALUE (lhs_bindings
);
902 tree rhs_arg
= TREE_VALUE (rhs_bindings
);
903 gcc_assert (TREE_TYPE (lhs_arg
) == TREE_TYPE (rhs_arg
));
904 if (!cp_tree_equal (lhs_arg
, rhs_arg
))
906 lhs_bindings
= TREE_CHAIN (lhs_bindings
);
907 rhs_bindings
= TREE_CHAIN (rhs_bindings
);
909 return lhs_bindings
== rhs_bindings
;
912 /* Initialize the constexpr call table, if needed. */
915 maybe_initialize_constexpr_call_table (void)
917 if (constexpr_call_table
== NULL
)
918 constexpr_call_table
= hash_table
<constexpr_call_hasher
>::create_ggc (101);
921 /* We have an expression tree T that represents a call, either CALL_EXPR
922 or AGGR_INIT_EXPR. If the call is lexically to a named function,
923 retrun the _DECL for that function. */
926 get_function_named_in_call (tree t
)
929 switch (TREE_CODE (t
))
932 fun
= CALL_EXPR_FN (t
);
936 fun
= AGGR_INIT_EXPR_FN (t
);
943 if (fun
&& TREE_CODE (fun
) == ADDR_EXPR
944 && TREE_CODE (TREE_OPERAND (fun
, 0)) == FUNCTION_DECL
)
945 fun
= TREE_OPERAND (fun
, 0);
949 /* We have an expression tree T that represents a call, either CALL_EXPR
950 or AGGR_INIT_EXPR. Return the Nth argument. */
953 get_nth_callarg (tree t
, int n
)
955 switch (TREE_CODE (t
))
958 return CALL_EXPR_ARG (t
, n
);
961 return AGGR_INIT_EXPR_ARG (t
, n
);
969 /* Look up the binding of the function parameter T in a constexpr
970 function call context CALL. */
973 lookup_parameter_binding (const constexpr_call
*call
, tree t
)
975 tree b
= purpose_member (t
, call
->bindings
);
976 return TREE_VALUE (b
);
979 /* Attempt to evaluate T which represents a call to a builtin function.
980 We assume here that all builtin functions evaluate to scalar types
981 represented by _CST nodes. */
984 cxx_eval_builtin_function_call (const constexpr_ctx
*ctx
, tree t
,
985 bool allow_non_constant
, bool addr
,
986 bool *non_constant_p
, bool *overflow_p
)
988 const int nargs
= call_expr_nargs (t
);
989 tree
*args
= (tree
*) alloca (nargs
* sizeof (tree
));
992 for (i
= 0; i
< nargs
; ++i
)
994 args
[i
] = cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, i
),
995 allow_non_constant
, addr
,
996 non_constant_p
, overflow_p
,
998 if (allow_non_constant
&& *non_constant_p
)
1001 if (*non_constant_p
)
1003 new_call
= fold_builtin_call_array (EXPR_LOCATION (t
), TREE_TYPE (t
),
1004 CALL_EXPR_FN (t
), nargs
, args
);
1005 VERIFY_CONSTANT (new_call
);
1009 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1010 the type of the value to match. */
1013 adjust_temp_type (tree type
, tree temp
)
1015 if (TREE_TYPE (temp
) == type
)
1017 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1018 if (TREE_CODE (temp
) == CONSTRUCTOR
)
1019 return build_constructor (type
, CONSTRUCTOR_ELTS (temp
));
1020 gcc_assert (scalarish_type_p (type
));
1021 return cp_fold_convert (type
, temp
);
1024 /* True if we want to use the new handling of constexpr calls based on
1026 #define use_new_call true
1028 /* Subroutine of cxx_eval_call_expression.
1029 We are processing a call expression (either CALL_EXPR or
1030 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1031 all arguments and bind their values to correspondings
1032 parameters, making up the NEW_CALL context. */
1035 cxx_bind_parameters_in_call (const constexpr_ctx
*ctx
, tree t
,
1036 constexpr_call
*new_call
,
1037 bool allow_non_constant
,
1038 bool *non_constant_p
, bool *overflow_p
)
1040 const int nargs
= call_expr_nargs (t
);
1041 tree fun
= new_call
->fundef
->decl
;
1042 tree parms
= DECL_ARGUMENTS (fun
);
1044 tree
*p
= &new_call
->bindings
;
1045 for (i
= 0; i
< nargs
; ++i
)
1048 tree type
= parms
? TREE_TYPE (parms
) : void_type_node
;
1049 x
= get_nth_callarg (t
, i
);
1050 /* For member function, the first argument is a pointer to the implied
1051 object. For a constructor, it might still be a dummy object, in
1052 which case we get the real argument from ctx. */
1053 if (i
== 0 && DECL_CONSTRUCTOR_P (fun
)
1054 && is_dummy_object (x
))
1057 x
= cp_build_addr_expr (x
, tf_warning_or_error
);
1059 if (parms
&& DECL_BY_REFERENCE (parms
) && !use_new_call
)
1061 /* cp_genericize made this a reference for argument passing, but
1062 we don't want to treat it like one for C++11 constexpr
1063 evaluation. C++14 constexpr evaluation uses the genericized
1065 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
1066 gcc_assert (TREE_CODE (TREE_TYPE (x
)) == REFERENCE_TYPE
);
1067 type
= TREE_TYPE (type
);
1068 x
= convert_from_reference (x
);
1070 arg
= cxx_eval_constant_expression (ctx
, x
, allow_non_constant
,
1071 TREE_CODE (type
) == REFERENCE_TYPE
,
1072 non_constant_p
, overflow_p
, NULL
);
1073 /* Don't VERIFY_CONSTANT here. */
1074 if (*non_constant_p
&& allow_non_constant
)
1076 /* Just discard ellipsis args after checking their constantitude. */
1079 if (*non_constant_p
)
1080 /* Don't try to adjust the type of non-constant args. */
1083 /* Make sure the binding has the same type as the parm. */
1084 if (TREE_CODE (type
) != REFERENCE_TYPE
)
1085 arg
= adjust_temp_type (type
, arg
);
1086 *p
= build_tree_list (parms
, arg
);
1087 p
= &TREE_CHAIN (*p
);
1089 parms
= TREE_CHAIN (parms
);
1093 /* Variables and functions to manage constexpr call expansion context.
1094 These do not need to be marked for PCH or GC. */
1096 /* FIXME remember and print actual constant arguments. */
1097 static vec
<tree
> call_stack
= vNULL
;
1098 static int call_stack_tick
;
1099 static int last_cx_error_tick
;
1102 push_cx_call_context (tree call
)
1105 if (!EXPR_HAS_LOCATION (call
))
1106 SET_EXPR_LOCATION (call
, input_location
);
1107 call_stack
.safe_push (call
);
1108 if (call_stack
.length () > (unsigned) max_constexpr_depth
)
1114 pop_cx_call_context (void)
1121 cx_error_context (void)
1123 vec
<tree
> r
= vNULL
;
1124 if (call_stack_tick
!= last_cx_error_tick
1125 && !call_stack
.is_empty ())
1127 last_cx_error_tick
= call_stack_tick
;
1131 /* Subroutine of cxx_eval_constant_expression.
1132 Evaluate the call expression tree T in the context of OLD_CALL expression
1136 cxx_eval_call_expression (const constexpr_ctx
*ctx
, tree t
,
1137 bool allow_non_constant
, bool addr
,
1138 bool *non_constant_p
, bool *overflow_p
)
1140 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
1141 tree fun
= get_function_named_in_call (t
);
1143 constexpr_call new_call
= { NULL
, NULL
, NULL
, 0 };
1144 constexpr_call
**slot
;
1145 constexpr_call
*entry
;
1148 if (TREE_CODE (fun
) != FUNCTION_DECL
)
1150 /* Might be a constexpr function pointer. */
1151 fun
= cxx_eval_constant_expression (ctx
, fun
, allow_non_constant
,
1152 /*addr*/false, non_constant_p
,
1155 if (TREE_CODE (fun
) == ADDR_EXPR
)
1156 fun
= TREE_OPERAND (fun
, 0);
1158 if (TREE_CODE (fun
) != FUNCTION_DECL
)
1160 if (!allow_non_constant
&& !*non_constant_p
)
1161 error_at (loc
, "expression %qE does not designate a constexpr "
1163 *non_constant_p
= true;
1166 if (DECL_CLONED_FUNCTION_P (fun
))
1167 fun
= DECL_CLONED_FUNCTION (fun
);
1168 if (is_builtin_fn (fun
))
1169 return cxx_eval_builtin_function_call (ctx
, t
, allow_non_constant
,
1170 addr
, non_constant_p
, overflow_p
);
1171 if (!DECL_DECLARED_CONSTEXPR_P (fun
))
1173 if (!allow_non_constant
)
1175 error_at (loc
, "call to non-constexpr function %qD", fun
);
1176 explain_invalid_constexpr_fn (fun
);
1178 *non_constant_p
= true;
1182 /* Shortcut trivial constructor/op=. */
1183 if (trivial_fn_p (fun
))
1185 if (call_expr_nargs (t
) == 2)
1187 tree arg
= convert_from_reference (get_nth_callarg (t
, 1));
1188 return cxx_eval_constant_expression (ctx
, arg
, allow_non_constant
,
1189 addr
, non_constant_p
,
1192 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
1193 && AGGR_INIT_ZERO_FIRST (t
))
1194 return build_zero_init (DECL_CONTEXT (fun
), NULL_TREE
, false);
1197 /* If in direct recursive call, optimize definition search. */
1198 if (ctx
&& ctx
->call
&& ctx
->call
->fundef
->decl
== fun
)
1199 new_call
.fundef
= ctx
->call
->fundef
;
1202 new_call
.fundef
= retrieve_constexpr_fundef (fun
);
1203 if (new_call
.fundef
== NULL
|| new_call
.fundef
->body
== NULL
)
1205 if (!allow_non_constant
)
1207 if (DECL_INITIAL (fun
))
1209 /* The definition of fun was somehow unsuitable. */
1210 error_at (loc
, "%qD called in a constant expression", fun
);
1211 explain_invalid_constexpr_fn (fun
);
1214 error_at (loc
, "%qD used before its definition", fun
);
1216 *non_constant_p
= true;
1221 constexpr_ctx new_ctx
= *ctx
;
1222 if (DECL_CONSTRUCTOR_P (fun
) && !ctx
->object
1223 && TREE_CODE (t
) == AGGR_INIT_EXPR
)
1225 /* We want to have an initialization target for an AGGR_INIT_EXPR.
1226 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
1227 new_ctx
.object
= AGGR_INIT_EXPR_SLOT (t
);
1228 tree ctor
= new_ctx
.ctor
= build_constructor (DECL_CONTEXT (fun
), NULL
);
1229 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctor
) = true;
1230 ctx
->values
->put (new_ctx
.object
, ctor
);
1234 cxx_bind_parameters_in_call (ctx
, t
, &new_call
,
1235 allow_non_constant
, non_constant_p
, overflow_p
);
1236 if (*non_constant_p
)
1239 depth_ok
= push_cx_call_context (t
);
1242 = iterative_hash_template_arg (new_call
.bindings
,
1243 constexpr_fundef_hasher::hash (new_call
.fundef
));
1245 /* If we have seen this call before, we are done. */
1246 maybe_initialize_constexpr_call_table ();
1247 slot
= constexpr_call_table
->find_slot (&new_call
, INSERT
);
1251 /* We need to keep a pointer to the entry, not just the slot, as the
1252 slot can move in the call to cxx_eval_builtin_function_call. */
1253 *slot
= entry
= ggc_alloc
<constexpr_call
> ();
1256 /* Calls which are in progress have their result set to NULL
1257 so that we can detect circular dependencies. */
1258 else if (entry
->result
== NULL
)
1260 if (!allow_non_constant
)
1261 error ("call has circular dependency");
1262 *non_constant_p
= true;
1263 entry
->result
= result
= error_mark_node
;
1268 if (!allow_non_constant
)
1269 error ("constexpr evaluation depth exceeds maximum of %d (use "
1270 "-fconstexpr-depth= to increase the maximum)",
1271 max_constexpr_depth
);
1272 *non_constant_p
= true;
1273 entry
->result
= result
= error_mark_node
;
1277 result
= entry
->result
;
1278 if (!result
|| result
== error_mark_node
)
1282 new_ctx
.call
= &new_call
;
1283 result
= (cxx_eval_constant_expression
1284 (&new_ctx
, new_call
.fundef
->body
,
1285 allow_non_constant
, addr
,
1286 non_constant_p
, overflow_p
, NULL
));
1290 if (DECL_SAVED_TREE (fun
) == NULL_TREE
1291 && (DECL_CONSTRUCTOR_P (fun
) || DECL_DESTRUCTOR_P (fun
)))
1292 /* The maybe-in-charge 'tor had its DECL_SAVED_TREE
1293 cleared, try the first clone. */
1294 fun
= DECL_CHAIN (fun
);
1295 gcc_assert (DECL_SAVED_TREE (fun
));
1298 /* Unshare the whole function body. */
1299 tree body
= copy_fn (fun
, parms
, res
);
1301 /* Associate the bindings with the remapped parms. */
1302 tree bound
= new_call
.bindings
;
1303 tree remapped
= parms
;
1306 tree oparm
= TREE_PURPOSE (bound
);
1307 tree arg
= TREE_VALUE (bound
);
1308 gcc_assert (DECL_NAME (remapped
) == DECL_NAME (oparm
));
1309 ctx
->values
->put (remapped
, arg
);
1310 bound
= TREE_CHAIN (bound
);
1311 remapped
= DECL_CHAIN (remapped
);
1313 /* Add the RESULT_DECL to the values map, too. */
1314 tree slot
= NULL_TREE
;
1315 if (DECL_BY_REFERENCE (res
))
1317 slot
= AGGR_INIT_EXPR_SLOT (t
);
1318 tree addr
= build_address (slot
);
1319 addr
= build_nop (TREE_TYPE (res
), addr
);
1320 ctx
->values
->put (res
, addr
);
1321 ctx
->values
->put (slot
, NULL_TREE
);
1324 ctx
->values
->put (res
, NULL_TREE
);
1326 tree jump_target
= NULL_TREE
;
1327 cxx_eval_constant_expression (ctx
, body
, allow_non_constant
,
1328 addr
, non_constant_p
, overflow_p
,
1331 if (VOID_TYPE_P (TREE_TYPE (res
)))
1332 /* This can be null for a subobject constructor call, in
1333 which case what we care about is the initialization
1334 side-effects rather than the value. We could get at the
1335 value by evaluating *this, but we don't bother; there's
1336 no need to put such a call in the hash table. */
1337 result
= addr
? ctx
->object
: ctx
->ctor
;
1340 result
= *ctx
->values
->get (slot
? slot
: res
);
1341 if (result
== NULL_TREE
)
1343 if (!allow_non_constant
)
1344 error ("constexpr call flows off the end "
1346 *non_constant_p
= true;
1350 /* Remove the parms/result from the values map. Is it worth
1351 bothering to do this when the map itself is only live for
1352 one constexpr evaluation? If so, maybe also clear out
1353 other vars from call, maybe in BIND_EXPR handling? */
1354 ctx
->values
->remove (res
);
1356 ctx
->values
->remove (slot
);
1357 for (tree parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
1358 ctx
->values
->remove (parm
);
1362 if (result
== error_mark_node
)
1363 *non_constant_p
= true;
1364 if (*non_constant_p
)
1365 entry
->result
= result
= error_mark_node
;
1368 /* If this was a call to initialize an object, set the type of
1369 the CONSTRUCTOR to the type of that object. */
1370 if (DECL_CONSTRUCTOR_P (fun
))
1372 tree ob_arg
= get_nth_callarg (t
, 0);
1373 STRIP_NOPS (ob_arg
);
1374 gcc_assert (TYPE_PTR_P (TREE_TYPE (ob_arg
))
1375 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (ob_arg
))));
1376 result
= adjust_temp_type (TREE_TYPE (TREE_TYPE (ob_arg
)),
1379 entry
->result
= result
;
1385 pop_cx_call_context ();
1386 return unshare_expr (result
);
1389 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
1392 reduced_constant_expression_p (tree t
)
1394 switch (TREE_CODE (t
))
1397 /* Even if we can't lower this yet, it's constant. */
1401 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
1402 tree elt
; unsigned HOST_WIDE_INT idx
;
1403 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), idx
, elt
)
1404 if (!reduced_constant_expression_p (elt
))
1409 /* FIXME are we calling this too much? */
1410 return initializer_constant_valid_p (t
, TREE_TYPE (t
)) != NULL_TREE
;
1414 /* Some expressions may have constant operands but are not constant
1415 themselves, such as 1/0. Call this function (or rather, the macro
1416 following it) to check for that condition.
1418 We only call this in places that require an arithmetic constant, not in
1419 places where we might have a non-constant expression that can be a
1420 component of a constant expression, such as the address of a constexpr
1421 variable that might be dereferenced later. */
1424 verify_constant (tree t
, bool allow_non_constant
, bool *non_constant_p
,
1427 if (!*non_constant_p
&& !reduced_constant_expression_p (t
))
1429 if (!allow_non_constant
)
1430 error ("%q+E is not a constant expression", t
);
1431 *non_constant_p
= true;
1433 if (TREE_OVERFLOW_P (t
))
1435 if (!allow_non_constant
)
1437 permerror (input_location
, "overflow in constant expression");
1438 /* If we're being permissive (and are in an enforcing
1439 context), ignore the overflow. */
1440 if (flag_permissive
)
1441 return *non_constant_p
;
1445 return *non_constant_p
;
1448 /* Subroutine of cxx_eval_constant_expression.
1449 Attempt to reduce the unary expression tree T to a compile time value.
1450 If successful, return the value. Otherwise issue a diagnostic
1451 and return error_mark_node. */
1454 cxx_eval_unary_expression (const constexpr_ctx
*ctx
, tree t
,
1455 bool allow_non_constant
, bool addr
,
1456 bool *non_constant_p
, bool *overflow_p
)
1459 tree orig_arg
= TREE_OPERAND (t
, 0);
1460 tree arg
= cxx_eval_constant_expression (ctx
, orig_arg
, allow_non_constant
,
1461 addr
, non_constant_p
, overflow_p
,
1463 VERIFY_CONSTANT (arg
);
1464 if (arg
== orig_arg
)
1466 r
= fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), arg
);
1467 VERIFY_CONSTANT (r
);
1471 /* Subroutine of cxx_eval_constant_expression.
1472 Like cxx_eval_unary_expression, except for binary expressions. */
1475 cxx_eval_binary_expression (const constexpr_ctx
*ctx
, tree t
,
1476 bool allow_non_constant
, bool addr
,
1477 bool *non_constant_p
, bool *overflow_p
)
1480 tree orig_lhs
= TREE_OPERAND (t
, 0);
1481 tree orig_rhs
= TREE_OPERAND (t
, 1);
1483 lhs
= cxx_eval_constant_expression (ctx
, orig_lhs
,
1484 allow_non_constant
, addr
,
1485 non_constant_p
, overflow_p
, NULL
);
1486 VERIFY_CONSTANT (lhs
);
1487 rhs
= cxx_eval_constant_expression (ctx
, orig_rhs
,
1488 allow_non_constant
, addr
,
1489 non_constant_p
, overflow_p
, NULL
);
1490 VERIFY_CONSTANT (rhs
);
1491 if (lhs
== orig_lhs
&& rhs
== orig_rhs
)
1493 r
= fold_build2 (TREE_CODE (t
), TREE_TYPE (t
), lhs
, rhs
);
1494 VERIFY_CONSTANT (r
);
1498 /* Subroutine of cxx_eval_constant_expression.
1499 Attempt to evaluate condition expressions. Dead branches are not
1503 cxx_eval_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
1504 bool allow_non_constant
, bool addr
,
1505 bool *non_constant_p
, bool *overflow_p
,
1508 tree val
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
1509 allow_non_constant
, addr
,
1510 non_constant_p
, overflow_p
,
1512 VERIFY_CONSTANT (val
);
1513 /* Don't VERIFY_CONSTANT the other operands. */
1514 if (integer_zerop (val
))
1515 return cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 2),
1516 allow_non_constant
, addr
,
1517 non_constant_p
, overflow_p
,
1519 return cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
1520 allow_non_constant
, addr
,
1521 non_constant_p
, overflow_p
,
1525 /* Subroutine of cxx_eval_constant_expression.
1526 Attempt to reduce a reference to an array slot. */
1529 cxx_eval_array_reference (const constexpr_ctx
*ctx
, tree t
,
1530 bool allow_non_constant
, bool addr
,
1531 bool *non_constant_p
, bool *overflow_p
)
1533 tree oldary
= TREE_OPERAND (t
, 0);
1534 tree ary
= cxx_eval_constant_expression (ctx
, oldary
,
1535 allow_non_constant
, addr
,
1536 non_constant_p
, overflow_p
, NULL
);
1540 unsigned len
, elem_nchars
= 1;
1541 if (*non_constant_p
)
1543 oldidx
= TREE_OPERAND (t
, 1);
1544 index
= cxx_eval_constant_expression (ctx
, oldidx
,
1545 allow_non_constant
, false,
1546 non_constant_p
, overflow_p
, NULL
);
1547 VERIFY_CONSTANT (index
);
1548 if (addr
&& ary
== oldary
&& index
== oldidx
)
1551 return build4 (ARRAY_REF
, TREE_TYPE (t
), ary
, index
, NULL
, NULL
);
1552 elem_type
= TREE_TYPE (TREE_TYPE (ary
));
1553 if (TREE_CODE (ary
) == CONSTRUCTOR
)
1554 len
= CONSTRUCTOR_NELTS (ary
);
1555 else if (TREE_CODE (ary
) == STRING_CST
)
1557 elem_nchars
= (TYPE_PRECISION (elem_type
)
1558 / TYPE_PRECISION (char_type_node
));
1559 len
= (unsigned) TREE_STRING_LENGTH (ary
) / elem_nchars
;
1563 /* We can't do anything with other tree codes, so use
1564 VERIFY_CONSTANT to complain and fail. */
1565 VERIFY_CONSTANT (ary
);
1568 if (compare_tree_int (index
, len
) >= 0)
1570 if (tree_int_cst_lt (index
, array_type_nelts_top (TREE_TYPE (ary
))))
1572 /* If it's within the array bounds but doesn't have an explicit
1573 initializer, it's value-initialized. */
1574 tree val
= build_value_init (elem_type
, tf_warning_or_error
);
1575 return cxx_eval_constant_expression (ctx
, val
,
1576 allow_non_constant
, addr
,
1577 non_constant_p
, overflow_p
,
1581 if (!allow_non_constant
)
1582 error ("array subscript out of bound");
1583 *non_constant_p
= true;
1586 else if (tree_int_cst_lt (index
, integer_zero_node
))
1588 if (!allow_non_constant
)
1589 error ("negative array subscript");
1590 *non_constant_p
= true;
1593 i
= tree_to_shwi (index
);
1594 if (TREE_CODE (ary
) == CONSTRUCTOR
)
1595 return (*CONSTRUCTOR_ELTS (ary
))[i
].value
;
1596 else if (elem_nchars
== 1)
1597 return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary
))),
1598 TREE_STRING_POINTER (ary
)[i
]);
1601 tree type
= cv_unqualified (TREE_TYPE (TREE_TYPE (ary
)));
1602 return native_interpret_expr (type
, (const unsigned char *)
1603 TREE_STRING_POINTER (ary
)
1604 + i
* elem_nchars
, elem_nchars
);
1606 /* Don't VERIFY_CONSTANT here. */
1609 /* Subroutine of cxx_eval_constant_expression.
1610 Attempt to reduce a field access of a value of class type. */
1613 cxx_eval_component_reference (const constexpr_ctx
*ctx
, tree t
,
1614 bool allow_non_constant
, bool addr
,
1615 bool *non_constant_p
, bool *overflow_p
)
1617 unsigned HOST_WIDE_INT i
;
1620 tree part
= TREE_OPERAND (t
, 1);
1621 tree orig_whole
= TREE_OPERAND (t
, 0);
1622 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
1623 allow_non_constant
, addr
,
1624 non_constant_p
, overflow_p
, NULL
);
1625 if (whole
== orig_whole
)
1628 return fold_build3 (COMPONENT_REF
, TREE_TYPE (t
),
1629 whole
, part
, NULL_TREE
);
1630 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
1632 if (!*non_constant_p
&& TREE_CODE (whole
) != CONSTRUCTOR
)
1634 if (!allow_non_constant
)
1635 error ("%qE is not a constant expression", orig_whole
);
1636 *non_constant_p
= true;
1638 if (DECL_MUTABLE_P (part
))
1640 if (!allow_non_constant
)
1641 error ("mutable %qD is not usable in a constant expression", part
);
1642 *non_constant_p
= true;
1644 if (*non_constant_p
)
1646 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
1653 /* We're in the middle of initializing it. */
1657 if (TREE_CODE (TREE_TYPE (whole
)) == UNION_TYPE
1658 && CONSTRUCTOR_NELTS (whole
) > 0)
1660 /* DR 1188 says we don't have to deal with this. */
1661 if (!allow_non_constant
)
1662 error ("accessing %qD member instead of initialized %qD member in "
1663 "constant expression", part
, CONSTRUCTOR_ELT (whole
, 0)->index
);
1664 *non_constant_p
= true;
1668 if (CONSTRUCTOR_NO_IMPLICIT_ZERO (whole
))
1670 /* 'whole' is part of the aggregate initializer we're currently
1671 building; if there's no initializer for this member yet, that's an
1673 if (!allow_non_constant
)
1674 error ("accessing uninitialized member %qD", part
);
1675 *non_constant_p
= true;
1679 /* If there's no explicit init for this field, it's value-initialized. */
1680 value
= build_value_init (TREE_TYPE (t
), tf_warning_or_error
);
1681 return cxx_eval_constant_expression (ctx
, value
,
1682 allow_non_constant
, addr
,
1683 non_constant_p
, overflow_p
, NULL
);
1686 /* Subroutine of cxx_eval_constant_expression.
1687 Attempt to reduce a field access of a value of class type that is
1688 expressed as a BIT_FIELD_REF. */
1691 cxx_eval_bit_field_ref (const constexpr_ctx
*ctx
, tree t
,
1692 bool allow_non_constant
, bool addr
,
1693 bool *non_constant_p
, bool *overflow_p
)
1695 tree orig_whole
= TREE_OPERAND (t
, 0);
1696 tree retval
, fldval
, utype
, mask
;
1697 bool fld_seen
= false;
1698 HOST_WIDE_INT istart
, isize
;
1699 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
1700 allow_non_constant
, addr
,
1701 non_constant_p
, overflow_p
, NULL
);
1702 tree start
, field
, value
;
1703 unsigned HOST_WIDE_INT i
;
1705 if (whole
== orig_whole
)
1707 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
1709 if (!*non_constant_p
1710 && TREE_CODE (whole
) != VECTOR_CST
1711 && TREE_CODE (whole
) != CONSTRUCTOR
)
1713 if (!allow_non_constant
)
1714 error ("%qE is not a constant expression", orig_whole
);
1715 *non_constant_p
= true;
1717 if (*non_constant_p
)
1720 if (TREE_CODE (whole
) == VECTOR_CST
)
1721 return fold_ternary (BIT_FIELD_REF
, TREE_TYPE (t
), whole
,
1722 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 2));
1724 start
= TREE_OPERAND (t
, 2);
1725 istart
= tree_to_shwi (start
);
1726 isize
= tree_to_shwi (TREE_OPERAND (t
, 1));
1727 utype
= TREE_TYPE (t
);
1728 if (!TYPE_UNSIGNED (utype
))
1729 utype
= build_nonstandard_integer_type (TYPE_PRECISION (utype
), 1);
1730 retval
= build_int_cst (utype
, 0);
1731 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
1733 tree bitpos
= bit_position (field
);
1734 if (bitpos
== start
&& DECL_SIZE (field
) == TREE_OPERAND (t
, 1))
1736 if (TREE_CODE (TREE_TYPE (field
)) == INTEGER_TYPE
1737 && TREE_CODE (value
) == INTEGER_CST
1738 && tree_fits_shwi_p (bitpos
)
1739 && tree_fits_shwi_p (DECL_SIZE (field
)))
1741 HOST_WIDE_INT bit
= tree_to_shwi (bitpos
);
1742 HOST_WIDE_INT sz
= tree_to_shwi (DECL_SIZE (field
));
1743 HOST_WIDE_INT shift
;
1744 if (bit
>= istart
&& bit
+ sz
<= istart
+ isize
)
1746 fldval
= fold_convert (utype
, value
);
1747 mask
= build_int_cst_type (utype
, -1);
1748 mask
= fold_build2 (LSHIFT_EXPR
, utype
, mask
,
1749 size_int (TYPE_PRECISION (utype
) - sz
));
1750 mask
= fold_build2 (RSHIFT_EXPR
, utype
, mask
,
1751 size_int (TYPE_PRECISION (utype
) - sz
));
1752 fldval
= fold_build2 (BIT_AND_EXPR
, utype
, fldval
, mask
);
1753 shift
= bit
- istart
;
1754 if (BYTES_BIG_ENDIAN
)
1755 shift
= TYPE_PRECISION (utype
) - shift
- sz
;
1756 fldval
= fold_build2 (LSHIFT_EXPR
, utype
, fldval
,
1758 retval
= fold_build2 (BIT_IOR_EXPR
, utype
, retval
, fldval
);
1764 return fold_convert (TREE_TYPE (t
), retval
);
1766 return error_mark_node
;
1769 /* Subroutine of cxx_eval_constant_expression.
1770 Evaluate a short-circuited logical expression T in the context
1771 of a given constexpr CALL. BAILOUT_VALUE is the value for
1772 early return. CONTINUE_VALUE is used here purely for
1773 sanity check purposes. */
1776 cxx_eval_logical_expression (const constexpr_ctx
*ctx
, tree t
,
1777 tree bailout_value
, tree continue_value
,
1778 bool allow_non_constant
, bool addr
,
1779 bool *non_constant_p
, bool *overflow_p
)
1782 tree lhs
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
1783 allow_non_constant
, addr
,
1784 non_constant_p
, overflow_p
, NULL
);
1785 VERIFY_CONSTANT (lhs
);
1786 if (tree_int_cst_equal (lhs
, bailout_value
))
1788 gcc_assert (tree_int_cst_equal (lhs
, continue_value
));
1789 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
1790 allow_non_constant
, addr
, non_constant_p
,
1792 VERIFY_CONSTANT (r
);
1796 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
1797 CONSTRUCTOR elements to initialize (part of) an object containing that
1798 field. Return a pointer to the constructor_elt corresponding to the
1799 initialization of the field. */
1801 static constructor_elt
*
1802 base_field_constructor_elt (vec
<constructor_elt
, va_gc
> *v
, tree ref
)
1804 tree aggr
= TREE_OPERAND (ref
, 0);
1805 tree field
= TREE_OPERAND (ref
, 1);
1807 constructor_elt
*ce
;
1809 gcc_assert (TREE_CODE (ref
) == COMPONENT_REF
);
1811 if (TREE_CODE (aggr
) == COMPONENT_REF
)
1813 constructor_elt
*base_ce
1814 = base_field_constructor_elt (v
, aggr
);
1815 v
= CONSTRUCTOR_ELTS (base_ce
->value
);
1818 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
1819 if (ce
->index
== field
)
1826 /* Some of the expressions fed to the constexpr mechanism are calls to
1827 constructors, which have type void. In that case, return the type being
1828 initialized by the constructor. */
1831 initialized_type (tree t
)
1835 tree type
= cv_unqualified (TREE_TYPE (t
));
1836 if (TREE_CODE (t
) == CALL_EXPR
|| TREE_CODE (t
) == AGGR_INIT_EXPR
)
1838 /* A constructor call has void type, so we need to look deeper. */
1839 tree fn
= get_function_named_in_call (t
);
1840 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
1841 && DECL_CXX_CONSTRUCTOR_P (fn
))
1842 type
= DECL_CONTEXT (fn
);
1847 /* We're about to initialize element INDEX of an array or class from VALUE.
1848 Set up NEW_CTX appropriately by adjusting .object to refer to the
1849 subobject and creating a new CONSTRUCTOR if the element is itself
1850 a class or array. */
1853 init_subob_ctx (const constexpr_ctx
*ctx
, constexpr_ctx
&new_ctx
,
1854 tree index
, tree
&value
)
1858 if (index
&& TREE_CODE (index
) != INTEGER_CST
1859 && TREE_CODE (index
) != FIELD_DECL
)
1860 /* This won't have an element in the new CONSTRUCTOR. */
1863 tree type
= initialized_type (value
);
1864 if (!AGGREGATE_TYPE_P (type
) && !VECTOR_TYPE_P (type
))
1865 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
1868 /* The sub-aggregate initializer might contain a placeholder;
1869 update object to refer to the subobject and ctor to refer to
1870 the (newly created) sub-initializer. */
1872 new_ctx
.object
= build_ctor_subob_ref (index
, type
, ctx
->object
);
1873 tree elt
= build_constructor (type
, NULL
);
1874 CONSTRUCTOR_NO_IMPLICIT_ZERO (elt
) = true;
1877 if (TREE_CODE (value
) == TARGET_EXPR
)
1878 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
1879 value
= TARGET_EXPR_INITIAL (value
);
1882 /* We're about to process an initializer for a class or array TYPE. Make
1883 sure that CTX is set up appropriately. */
1886 verify_ctor_sanity (const constexpr_ctx
*ctx
, tree type
)
1888 /* We don't bother building a ctor for an empty base subobject. */
1889 if (is_empty_class (type
))
1892 /* We're in the middle of an initializer that might involve placeholders;
1893 our caller should have created a CONSTRUCTOR for us to put the
1894 initializer into. We will either return that constructor or T. */
1895 gcc_assert (ctx
->ctor
);
1896 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1897 (type
, TREE_TYPE (ctx
->ctor
)));
1898 gcc_assert (CONSTRUCTOR_NELTS (ctx
->ctor
) == 0);
1900 gcc_assert (same_type_ignoring_top_level_qualifiers_p
1901 (type
, TREE_TYPE (ctx
->object
)));
1902 gcc_assert (!ctx
->object
|| !DECL_P (ctx
->object
)
1903 || *(ctx
->values
->get (ctx
->object
)) == ctx
->ctor
);
1906 /* Subroutine of cxx_eval_constant_expression.
1907 The expression tree T denotes a C-style array or a C-style
1908 aggregate. Reduce it to a constant expression. */
1911 cxx_eval_bare_aggregate (const constexpr_ctx
*ctx
, tree t
,
1912 bool allow_non_constant
, bool addr
,
1913 bool *non_constant_p
, bool *overflow_p
)
1915 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
1916 bool changed
= false;
1917 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t
));
1919 verify_ctor_sanity (ctx
, TREE_TYPE (t
));
1920 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
1921 vec_alloc (*p
, vec_safe_length (v
));
1923 unsigned i
; tree index
, value
;
1924 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, index
, value
)
1926 constexpr_ctx new_ctx
;
1927 init_subob_ctx (ctx
, new_ctx
, index
, value
);
1928 if (new_ctx
.ctor
!= ctx
->ctor
)
1929 /* If we built a new CONSTRUCTOR, attach it now so that other
1930 initializers can refer to it. */
1931 CONSTRUCTOR_APPEND_ELT (*p
, index
, new_ctx
.ctor
);
1932 tree elt
= cxx_eval_constant_expression (&new_ctx
, value
,
1933 allow_non_constant
, addr
,
1934 non_constant_p
, overflow_p
,
1936 /* Don't VERIFY_CONSTANT here. */
1937 if (allow_non_constant
&& *non_constant_p
)
1941 if (index
&& TREE_CODE (index
) == COMPONENT_REF
)
1943 /* This is an initialization of a vfield inside a base
1944 subaggregate that we already initialized; push this
1945 initialization into the previous initialization. */
1946 constructor_elt
*inner
= base_field_constructor_elt (*p
, index
);
1951 && (TREE_CODE (index
) == NOP_EXPR
1952 || TREE_CODE (index
) == POINTER_PLUS_EXPR
))
1954 /* This is an initializer for an empty base; now that we've
1955 checked that it's constant, we can ignore it. */
1956 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index
))));
1959 else if (new_ctx
.ctor
!= ctx
->ctor
)
1961 /* We appended this element above; update the value. */
1962 gcc_assert ((*p
)->last().index
== index
);
1963 (*p
)->last().value
= elt
;
1966 CONSTRUCTOR_APPEND_ELT (*p
, index
, elt
);
1968 if (*non_constant_p
|| !changed
)
1971 /* We're done building this CONSTRUCTOR, so now we can interpret an
1972 element without an explicit initializer as value-initialized. */
1973 CONSTRUCTOR_NO_IMPLICIT_ZERO (t
) = false;
1974 if (TREE_CODE (TREE_TYPE (t
)) == VECTOR_TYPE
)
1979 /* Subroutine of cxx_eval_constant_expression.
1980 The expression tree T is a VEC_INIT_EXPR which denotes the desired
1981 initialization of a non-static data member of array type. Reduce it to a
1984 Note that apart from value-initialization (when VALUE_INIT is true),
1985 this is only intended to support value-initialization and the
1986 initializations done by defaulted constructors for classes with
1987 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
1988 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
1989 for the copy/move constructor. */
1992 cxx_eval_vec_init_1 (const constexpr_ctx
*ctx
, tree atype
, tree init
,
1993 bool value_init
, bool allow_non_constant
, bool addr
,
1994 bool *non_constant_p
, bool *overflow_p
)
1996 tree elttype
= TREE_TYPE (atype
);
1997 int max
= tree_to_shwi (array_type_nelts (atype
));
1998 verify_ctor_sanity (ctx
, atype
);
1999 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
2000 vec_alloc (*p
, max
+ 1);
2001 bool pre_init
= false;
2004 /* For the default constructor, build up a call to the default
2005 constructor of the element type. We only need to handle class types
2006 here, as for a constructor to be constexpr, all members must be
2007 initialized, which for a defaulted default constructor means they must
2008 be of a class type with a constexpr default constructor. */
2009 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
2010 /* We only do this at the lowest level. */;
2011 else if (value_init
)
2013 init
= build_value_init (elttype
, tf_warning_or_error
);
2018 vec
<tree
, va_gc
> *argvec
= make_tree_vector ();
2019 init
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
2020 &argvec
, elttype
, LOOKUP_NORMAL
,
2021 tf_warning_or_error
);
2022 release_tree_vector (argvec
);
2023 init
= build_aggr_init_expr (TREE_TYPE (init
), init
);
2027 for (i
= 0; i
<= max
; ++i
)
2029 tree idx
= build_int_cst (size_type_node
, i
);
2031 constexpr_ctx new_ctx
;
2032 init_subob_ctx (ctx
, new_ctx
, idx
, pre_init
? init
: elttype
);
2033 if (new_ctx
.ctor
!= ctx
->ctor
)
2034 CONSTRUCTOR_APPEND_ELT (*p
, idx
, new_ctx
.ctor
);
2035 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
2037 /* A multidimensional array; recurse. */
2038 if (value_init
|| init
== NULL_TREE
)
2039 eltinit
= NULL_TREE
;
2041 eltinit
= cp_build_array_ref (input_location
, init
, idx
,
2042 tf_warning_or_error
);
2043 eltinit
= cxx_eval_vec_init_1 (&new_ctx
, elttype
, eltinit
, value_init
,
2044 allow_non_constant
, addr
,
2045 non_constant_p
, overflow_p
);
2049 /* Initializing an element using value or default initialization
2050 we just pre-built above. */
2051 eltinit
= (cxx_eval_constant_expression
2052 (&new_ctx
, init
, allow_non_constant
,
2053 addr
, non_constant_p
, overflow_p
, NULL
));
2057 /* Copying an element. */
2058 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2059 (atype
, TREE_TYPE (init
)));
2060 eltinit
= cp_build_array_ref (input_location
, init
, idx
,
2061 tf_warning_or_error
);
2062 if (!real_lvalue_p (init
))
2063 eltinit
= move (eltinit
);
2064 eltinit
= force_rvalue (eltinit
, tf_warning_or_error
);
2065 eltinit
= (cxx_eval_constant_expression
2066 (&new_ctx
, eltinit
, allow_non_constant
, addr
,
2067 non_constant_p
, overflow_p
, NULL
));
2069 if (*non_constant_p
&& !allow_non_constant
)
2071 if (new_ctx
.ctor
!= ctx
->ctor
)
2073 /* We appended this element above; update the value. */
2074 gcc_assert ((*p
)->last().index
== idx
);
2075 (*p
)->last().value
= eltinit
;
2078 CONSTRUCTOR_APPEND_ELT (*p
, idx
, eltinit
);
2081 if (!*non_constant_p
)
2084 CONSTRUCTOR_NO_IMPLICIT_ZERO (init
) = false;
2090 cxx_eval_vec_init (const constexpr_ctx
*ctx
, tree t
,
2091 bool allow_non_constant
, bool addr
,
2092 bool *non_constant_p
, bool *overflow_p
)
2094 tree atype
= TREE_TYPE (t
);
2095 tree init
= VEC_INIT_EXPR_INIT (t
);
2096 tree r
= cxx_eval_vec_init_1 (ctx
, atype
, init
,
2097 VEC_INIT_EXPR_VALUE_INIT (t
),
2098 allow_non_constant
, addr
, non_constant_p
, overflow_p
);
2099 if (*non_constant_p
)
2105 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
2106 match. We want to be less strict for simple *& folding; if we have a
2107 non-const temporary that we access through a const pointer, that should
2108 work. We handle this here rather than change fold_indirect_ref_1
2109 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
2110 don't really make sense outside of constant expression evaluation. Also
2111 we want to allow folding to COMPONENT_REF, which could cause trouble
2112 with TBAA in fold_indirect_ref_1.
2114 Try to keep this function synced with fold_indirect_ref_1. */
2117 cxx_fold_indirect_ref (location_t loc
, tree type
, tree op0
, bool *empty_base
)
2123 subtype
= TREE_TYPE (sub
);
2124 if (!POINTER_TYPE_P (subtype
))
2127 if (TREE_CODE (sub
) == ADDR_EXPR
)
2129 tree op
= TREE_OPERAND (sub
, 0);
2130 tree optype
= TREE_TYPE (op
);
2132 /* *&CONST_DECL -> to the value of the const decl. */
2133 if (TREE_CODE (op
) == CONST_DECL
)
2134 return DECL_INITIAL (op
);
2135 /* *&p => p; make sure to handle *&"str"[cst] here. */
2136 if (same_type_ignoring_top_level_qualifiers_p (optype
, type
))
2138 tree fop
= fold_read_from_constant_string (op
);
2144 /* *(foo *)&fooarray => fooarray[0] */
2145 else if (TREE_CODE (optype
) == ARRAY_TYPE
2146 && (same_type_ignoring_top_level_qualifiers_p
2147 (type
, TREE_TYPE (optype
))))
2149 tree type_domain
= TYPE_DOMAIN (optype
);
2150 tree min_val
= size_zero_node
;
2151 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
2152 min_val
= TYPE_MIN_VALUE (type_domain
);
2153 return build4_loc (loc
, ARRAY_REF
, type
, op
, min_val
,
2154 NULL_TREE
, NULL_TREE
);
2156 /* *(foo *)&complexfoo => __real__ complexfoo */
2157 else if (TREE_CODE (optype
) == COMPLEX_TYPE
2158 && (same_type_ignoring_top_level_qualifiers_p
2159 (type
, TREE_TYPE (optype
))))
2160 return fold_build1_loc (loc
, REALPART_EXPR
, type
, op
);
2161 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
2162 else if (TREE_CODE (optype
) == VECTOR_TYPE
2163 && (same_type_ignoring_top_level_qualifiers_p
2164 (type
, TREE_TYPE (optype
))))
2166 tree part_width
= TYPE_SIZE (type
);
2167 tree index
= bitsize_int (0);
2168 return fold_build3_loc (loc
, BIT_FIELD_REF
, type
, op
, part_width
, index
);
2170 /* Also handle conversion to an empty base class, which
2171 is represented with a NOP_EXPR. */
2172 else if (is_empty_class (type
)
2173 && CLASS_TYPE_P (optype
)
2174 && DERIVED_FROM_P (type
, optype
))
2179 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
2180 else if (RECORD_OR_UNION_TYPE_P (optype
))
2182 tree field
= TYPE_FIELDS (optype
);
2183 for (; field
; field
= DECL_CHAIN (field
))
2184 if (TREE_CODE (field
) == FIELD_DECL
2185 && integer_zerop (byte_position (field
))
2186 && (same_type_ignoring_top_level_qualifiers_p
2187 (TREE_TYPE (field
), type
)))
2189 return fold_build3 (COMPONENT_REF
, type
, op
, field
, NULL_TREE
);
2194 else if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
2195 && TREE_CODE (TREE_OPERAND (sub
, 1)) == INTEGER_CST
)
2197 tree op00
= TREE_OPERAND (sub
, 0);
2198 tree op01
= TREE_OPERAND (sub
, 1);
2201 if (TREE_CODE (op00
) == ADDR_EXPR
)
2204 op00
= TREE_OPERAND (op00
, 0);
2205 op00type
= TREE_TYPE (op00
);
2207 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
2208 if (TREE_CODE (op00type
) == VECTOR_TYPE
2209 && (same_type_ignoring_top_level_qualifiers_p
2210 (type
, TREE_TYPE (op00type
))))
2212 HOST_WIDE_INT offset
= tree_to_shwi (op01
);
2213 tree part_width
= TYPE_SIZE (type
);
2214 unsigned HOST_WIDE_INT part_widthi
= tree_to_shwi (part_width
)/BITS_PER_UNIT
;
2215 unsigned HOST_WIDE_INT indexi
= offset
* BITS_PER_UNIT
;
2216 tree index
= bitsize_int (indexi
);
2218 if (offset
/ part_widthi
< TYPE_VECTOR_SUBPARTS (op00type
))
2219 return fold_build3_loc (loc
,
2220 BIT_FIELD_REF
, type
, op00
,
2224 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
2225 else if (TREE_CODE (op00type
) == COMPLEX_TYPE
2226 && (same_type_ignoring_top_level_qualifiers_p
2227 (type
, TREE_TYPE (op00type
))))
2229 tree size
= TYPE_SIZE_UNIT (type
);
2230 if (tree_int_cst_equal (size
, op01
))
2231 return fold_build1_loc (loc
, IMAGPART_EXPR
, type
, op00
);
2233 /* ((foo *)&fooarray)[1] => fooarray[1] */
2234 else if (TREE_CODE (op00type
) == ARRAY_TYPE
2235 && (same_type_ignoring_top_level_qualifiers_p
2236 (type
, TREE_TYPE (op00type
))))
2238 tree type_domain
= TYPE_DOMAIN (op00type
);
2239 tree min_val
= size_zero_node
;
2240 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
2241 min_val
= TYPE_MIN_VALUE (type_domain
);
2242 op01
= size_binop_loc (loc
, EXACT_DIV_EXPR
, op01
,
2243 TYPE_SIZE_UNIT (type
));
2244 op01
= size_binop_loc (loc
, PLUS_EXPR
, op01
, min_val
);
2245 return build4_loc (loc
, ARRAY_REF
, type
, op00
, op01
,
2246 NULL_TREE
, NULL_TREE
);
2248 /* Also handle conversion to an empty base class, which
2249 is represented with a NOP_EXPR. */
2250 else if (is_empty_class (type
)
2251 && CLASS_TYPE_P (op00type
)
2252 && DERIVED_FROM_P (type
, op00type
))
2257 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
2258 else if (RECORD_OR_UNION_TYPE_P (op00type
))
2260 tree field
= TYPE_FIELDS (op00type
);
2261 for (; field
; field
= DECL_CHAIN (field
))
2262 if (TREE_CODE (field
) == FIELD_DECL
2263 && tree_int_cst_equal (byte_position (field
), op01
)
2264 && (same_type_ignoring_top_level_qualifiers_p
2265 (TREE_TYPE (field
), type
)))
2267 return fold_build3 (COMPONENT_REF
, type
, op00
,
2274 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
2275 else if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
2276 && (same_type_ignoring_top_level_qualifiers_p
2277 (type
, TREE_TYPE (TREE_TYPE (subtype
)))))
2280 tree min_val
= size_zero_node
;
2281 tree newsub
= cxx_fold_indirect_ref (loc
, TREE_TYPE (subtype
), sub
, NULL
);
2285 sub
= build1_loc (loc
, INDIRECT_REF
, TREE_TYPE (subtype
), sub
);
2286 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
2287 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
2288 min_val
= TYPE_MIN_VALUE (type_domain
);
2289 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
2297 cxx_eval_indirect_ref (const constexpr_ctx
*ctx
, tree t
,
2298 bool allow_non_constant
, bool addr
,
2299 bool *non_constant_p
, bool *overflow_p
)
2301 tree orig_op0
= TREE_OPERAND (t
, 0);
2302 tree op0
= cxx_eval_constant_expression (ctx
, orig_op0
, allow_non_constant
,
2303 /*addr*/false, non_constant_p
,
2305 bool empty_base
= false;
2308 /* Don't VERIFY_CONSTANT here. */
2309 if (*non_constant_p
)
2312 r
= cxx_fold_indirect_ref (EXPR_LOCATION (t
), TREE_TYPE (t
), op0
,
2316 r
= cxx_eval_constant_expression (ctx
, r
, allow_non_constant
,
2317 addr
, non_constant_p
, overflow_p
, NULL
);
2322 if (TREE_CODE (sub
) == ADDR_EXPR
)
2324 /* We couldn't fold to a constant value. Make sure it's not
2325 something we should have been able to fold. */
2326 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
2327 (TREE_TYPE (TREE_TYPE (sub
)), TREE_TYPE (t
)));
2328 /* DR 1188 says we don't have to deal with this. */
2329 if (!allow_non_constant
)
2330 error ("accessing value of %qE through a %qT glvalue in a "
2331 "constant expression", build_fold_indirect_ref (sub
),
2333 *non_constant_p
= true;
2338 /* If we're pulling out the value of an empty base, make sure
2339 that the whole object is constant and then return an empty
2341 if (empty_base
&& !addr
)
2343 VERIFY_CONSTANT (r
);
2344 r
= build_constructor (TREE_TYPE (t
), NULL
);
2345 TREE_CONSTANT (r
) = true;
2350 if (addr
&& op0
!= orig_op0
)
2351 return build1 (INDIRECT_REF
, TREE_TYPE (t
), op0
);
2353 VERIFY_CONSTANT (t
);
2359 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
2360 Shared between potential_constant_expression and
2361 cxx_eval_constant_expression. */
2364 non_const_var_error (tree r
)
2366 tree type
= TREE_TYPE (r
);
2367 error ("the value of %qD is not usable in a constant "
2369 /* Avoid error cascade. */
2370 if (DECL_INITIAL (r
) == error_mark_node
)
2372 if (DECL_DECLARED_CONSTEXPR_P (r
))
2373 inform (DECL_SOURCE_LOCATION (r
),
2374 "%qD used in its own initializer", r
);
2375 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
2377 if (!CP_TYPE_CONST_P (type
))
2378 inform (DECL_SOURCE_LOCATION (r
),
2379 "%q#D is not const", r
);
2380 else if (CP_TYPE_VOLATILE_P (type
))
2381 inform (DECL_SOURCE_LOCATION (r
),
2382 "%q#D is volatile", r
);
2383 else if (!DECL_INITIAL (r
)
2384 || !TREE_CONSTANT (DECL_INITIAL (r
)))
2385 inform (DECL_SOURCE_LOCATION (r
),
2386 "%qD was not initialized with a constant "
2393 if (cxx_dialect
>= cxx11
&& !DECL_DECLARED_CONSTEXPR_P (r
))
2394 inform (DECL_SOURCE_LOCATION (r
),
2395 "%qD was not declared %<constexpr%>", r
);
2397 inform (DECL_SOURCE_LOCATION (r
),
2398 "%qD does not have integral or enumeration type",
2403 /* Subroutine of cxx_eval_constant_expression.
2404 Like cxx_eval_unary_expression, except for trinary expressions. */
2407 cxx_eval_trinary_expression (const constexpr_ctx
*ctx
, tree t
,
2408 bool allow_non_constant
, bool addr
,
2409 bool *non_constant_p
, bool *overflow_p
)
2415 for (i
= 0; i
< 3; i
++)
2417 args
[i
] = cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, i
),
2418 allow_non_constant
, addr
,
2419 non_constant_p
, overflow_p
,
2421 VERIFY_CONSTANT (args
[i
]);
2424 val
= fold_ternary_loc (EXPR_LOCATION (t
), TREE_CODE (t
), TREE_TYPE (t
),
2425 args
[0], args
[1], args
[2]);
2426 if (val
== NULL_TREE
)
2428 VERIFY_CONSTANT (val
);
2433 var_in_constexpr_fn (tree t
)
2435 tree ctx
= DECL_CONTEXT (t
);
2436 return (cxx_dialect
>= cxx14
&& ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
2437 && DECL_DECLARED_CONSTEXPR_P (ctx
));
2440 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
2443 cxx_eval_store_expression (const constexpr_ctx
*ctx
, tree t
,
2444 bool allow_non_constant
, bool addr
,
2445 bool *non_constant_p
, bool *overflow_p
)
2447 constexpr_ctx new_ctx
= *ctx
;
2449 /* First we figure out where we're storing to. */
2450 tree target
= TREE_OPERAND (t
, 0);
2451 target
= cxx_eval_constant_expression (ctx
, target
,
2452 allow_non_constant
, true,
2453 non_constant_p
, overflow_p
, NULL
);
2454 if (*non_constant_p
)
2457 /* And then find the underlying variable. */
2458 vec
<tree
,va_gc
> *refs
= make_tree_vector();
2459 tree object
= NULL_TREE
;
2460 for (tree probe
= target
; object
== NULL_TREE
; )
2462 switch (TREE_CODE (probe
))
2467 vec_safe_push (refs
, TREE_OPERAND (probe
, 1));
2468 vec_safe_push (refs
, TREE_TYPE (probe
));
2469 probe
= TREE_OPERAND (probe
, 0);
2474 gcc_assert (DECL_P (object
));
2478 /* And then find/build up our initializer for the path to the subobject
2479 we're initializing. */
2480 tree
*valp
= ctx
->values
->get (object
);
2483 /* A constant-expression cannot modify objects from outside the
2484 constant-expression. */
2485 if (!allow_non_constant
)
2486 error ("modification of %qD is not a constant-expression", object
);
2487 *non_constant_p
= true;
2490 tree type
= TREE_TYPE (object
);
2491 while (!refs
->is_empty())
2493 if (*valp
== NULL_TREE
)
2495 *valp
= build_constructor (type
, NULL
);
2496 CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp
) = true;
2501 ce
.index
= refs
->pop();
2502 ce
.value
= NULL_TREE
;
2504 unsigned HOST_WIDE_INT idx
= 0;
2505 constructor_elt
*cep
= NULL
;
2507 vec_safe_iterate (CONSTRUCTOR_ELTS (*valp
), idx
, &cep
);
2510 if (cp_tree_equal (ce
.index
, cep
->index
))
2513 cep
= vec_safe_push (CONSTRUCTOR_ELTS (*valp
), ce
);
2516 release_tree_vector (refs
);
2518 if ((AGGREGATE_TYPE_P (TREE_TYPE (t
)) || VECTOR_TYPE_P (TREE_TYPE (t
))))
2520 /* Create a new CONSTRUCTOR in case evaluation of the initializer
2521 wants to modify it. */
2522 *valp
= new_ctx
.ctor
= build_constructor (TREE_TYPE (t
), NULL
);
2523 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx
.ctor
) = true;
2524 new_ctx
.object
= target
;
2527 tree init
= cxx_eval_constant_expression (&new_ctx
, TREE_OPERAND (t
, 1),
2528 allow_non_constant
, false,
2529 non_constant_p
, overflow_p
, NULL
);
2530 if (target
== object
)
2531 /* The hash table might have moved since the get earlier. */
2532 ctx
->values
->put (object
, init
);
2536 if (*non_constant_p
)
2544 /* Evaluate a ++ or -- expression. */
2547 cxx_eval_increment_expression (const constexpr_ctx
*ctx
, tree t
,
2548 bool allow_non_constant
, bool addr
,
2549 bool *non_constant_p
, bool *overflow_p
)
2551 enum tree_code code
= TREE_CODE (t
);
2552 tree type
= TREE_TYPE (t
);
2553 tree op
= TREE_OPERAND (t
, 0);
2554 tree offset
= TREE_OPERAND (t
, 1);
2555 gcc_assert (TREE_CONSTANT (offset
));
2557 /* The operand as an lvalue. */
2558 op
= cxx_eval_constant_expression (ctx
, op
, allow_non_constant
, true,
2559 non_constant_p
, overflow_p
, NULL
);
2561 /* The operand as an rvalue. */
2562 tree val
= rvalue (op
);
2563 val
= cxx_eval_constant_expression (ctx
, val
, allow_non_constant
, false,
2564 non_constant_p
, overflow_p
, NULL
);
2565 VERIFY_CONSTANT (val
);
2567 /* The modified value. */
2568 bool inc
= (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
);
2569 tree mod
= fold_build2 (inc
? PLUS_EXPR
: MINUS_EXPR
,
2571 VERIFY_CONSTANT (mod
);
2573 /* Storing the modified value. */
2574 tree store
= build2 (MODIFY_EXPR
, type
, op
, mod
);
2575 cxx_eval_constant_expression (ctx
, store
, allow_non_constant
,
2576 true, non_constant_p
, overflow_p
, NULL
);
2578 /* And the value of the expression. */
2579 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
2581 /* Prefix ops are lvalues. */
2585 /* But we optimize when the caller wants an rvalue. */
2589 /* Postfix ops are rvalues. */
2593 /* Predicates for the meaning of *jump_target. */
2596 returns (tree
*jump_target
)
2599 && TREE_CODE (*jump_target
) == RETURN_EXPR
;
2603 breaks (tree
*jump_target
)
2606 && TREE_CODE (*jump_target
) == LABEL_DECL
2607 && LABEL_DECL_BREAK (*jump_target
);
2611 continues (tree
*jump_target
)
2614 && TREE_CODE (*jump_target
) == LABEL_DECL
2615 && LABEL_DECL_CONTINUE (*jump_target
);
2619 switches (tree
*jump_target
)
2622 && TREE_CODE (*jump_target
) == INTEGER_CST
;
2625 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
2626 at I matches *jump_target. If we're looking for a case label and we see
2627 the default label, copy I into DEFAULT_LABEL. */
2630 label_matches (tree
*jump_target
, tree_stmt_iterator i
,
2631 tree_stmt_iterator
& default_label
)
2633 tree stmt
= tsi_stmt (i
);
2634 switch (TREE_CODE (*jump_target
))
2637 if (TREE_CODE (stmt
) == LABEL_EXPR
2638 && LABEL_EXPR_LABEL (stmt
) == *jump_target
)
2643 if (TREE_CODE (stmt
) == CASE_LABEL_EXPR
)
2645 if (!CASE_LOW (stmt
))
2647 else if (tree_int_cst_equal (*jump_target
, CASE_LOW (stmt
)))
2658 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
2659 semantics, for switch, break, continue, and return. */
2662 cxx_eval_statement_list (const constexpr_ctx
*ctx
, tree t
,
2663 bool allow_non_constant
,
2664 bool *non_constant_p
, bool *overflow_p
,
2667 tree_stmt_iterator i
;
2668 tree_stmt_iterator default_label
= tree_stmt_iterator();
2669 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
2672 tree stmt
= tsi_stmt (i
);
2675 if (TREE_CODE (stmt
) == STATEMENT_LIST
)
2676 /* The label we want might be inside. */;
2677 else if (label_matches (jump_target
, i
, default_label
))
2679 *jump_target
= NULL_TREE
;
2683 cxx_eval_constant_expression (ctx
, stmt
,
2684 allow_non_constant
, false,
2685 non_constant_p
, overflow_p
,
2687 if (*non_constant_p
)
2689 if (returns (jump_target
) || breaks (jump_target
))
2692 if (switches (jump_target
) && !tsi_end_p (default_label
))
2695 *jump_target
= NULL_TREE
;
2701 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
2702 semantics; continue semantics are covered by cxx_eval_statement_list. */
2705 cxx_eval_loop_expr (const constexpr_ctx
*ctx
, tree t
,
2706 bool allow_non_constant
,
2707 bool *non_constant_p
, bool *overflow_p
,
2710 tree body
= TREE_OPERAND (t
, 0);
2713 cxx_eval_statement_list (ctx
, body
, allow_non_constant
,
2714 non_constant_p
, overflow_p
, jump_target
);
2715 if (returns (jump_target
) || breaks (jump_target
))
2718 if (breaks (jump_target
))
2719 *jump_target
= NULL_TREE
;
2723 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
2727 cxx_eval_switch_expr (const constexpr_ctx
*ctx
, tree t
,
2728 bool allow_non_constant
,
2729 bool *non_constant_p
, bool *overflow_p
,
2732 tree cond
= TREE_OPERAND (t
, 0);
2733 cond
= cxx_eval_constant_expression (ctx
, cond
, allow_non_constant
, false,
2734 non_constant_p
, overflow_p
, NULL
);
2735 VERIFY_CONSTANT (cond
);
2736 *jump_target
= cond
;
2738 tree body
= TREE_OPERAND (t
, 1);
2739 cxx_eval_statement_list (ctx
, body
, allow_non_constant
,
2740 non_constant_p
, overflow_p
, jump_target
);
2741 if (breaks (jump_target
) || switches (jump_target
))
2742 *jump_target
= NULL_TREE
;
2746 /* Attempt to reduce the expression T to a constant value.
2747 On failure, issue diagnostic and return error_mark_node. */
2748 /* FIXME unify with c_fully_fold */
2749 /* FIXME overflow_p is too global */
2752 cxx_eval_constant_expression (const constexpr_ctx
*ctx
, tree t
,
2753 bool allow_non_constant
, bool addr
,
2754 bool *non_constant_p
, bool *overflow_p
,
2757 constexpr_ctx new_ctx
;
2760 if (t
== error_mark_node
)
2762 *non_constant_p
= true;
2765 if (CONSTANT_CLASS_P (t
))
2767 if (TREE_CODE (t
) == PTRMEM_CST
)
2768 t
= cplus_expand_constant (t
);
2769 else if (TREE_OVERFLOW (t
) && (!flag_permissive
|| allow_non_constant
))
2773 if (TREE_CODE (t
) != NOP_EXPR
2774 && reduced_constant_expression_p (t
))
2777 switch (TREE_CODE (t
))
2782 /* We ask for an rvalue for the RESULT_DECL when indirecting
2783 through an invisible reference. */
2784 gcc_assert (DECL_BY_REFERENCE (t
));
2785 return (*ctx
->values
->get (t
));
2790 /* else fall through. */
2792 r
= integral_constant_value (t
);
2793 if (TREE_CODE (r
) == TARGET_EXPR
2794 && TREE_CODE (TARGET_EXPR_INITIAL (r
)) == CONSTRUCTOR
)
2795 r
= TARGET_EXPR_INITIAL (r
);
2796 if (TREE_CODE (r
) == VAR_DECL
)
2797 if (tree
*p
= ctx
->values
->get (r
))
2801 if (!allow_non_constant
)
2802 non_const_var_error (r
);
2803 *non_constant_p
= true;
2811 case CASE_LABEL_EXPR
:
2815 if (!use_new_call
&& ctx
2816 && ctx
->call
&& DECL_CONTEXT (t
) == ctx
->call
->fundef
->decl
)
2817 r
= lookup_parameter_binding (ctx
->call
, t
);
2818 else if (addr
&& TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
2820 else if (tree
*p
= ctx
->values
->get (r
))
2823 /* Defer in case this is only used for its type. */;
2826 if (!allow_non_constant
)
2827 error ("%qE is not a constant expression", t
);
2828 *non_constant_p
= true;
2833 case AGGR_INIT_EXPR
:
2834 r
= cxx_eval_call_expression (ctx
, t
, allow_non_constant
, addr
,
2835 non_constant_p
, overflow_p
);
2840 r
= DECL_EXPR_DECL (t
);
2841 if (AGGREGATE_TYPE_P (TREE_TYPE (r
))
2842 || VECTOR_TYPE_P (TREE_TYPE (r
)))
2846 new_ctx
.ctor
= build_constructor (TREE_TYPE (r
), NULL
);
2847 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx
.ctor
) = true;
2848 new_ctx
.values
->put (r
, new_ctx
.ctor
);
2852 if (tree init
= DECL_INITIAL (r
))
2854 init
= cxx_eval_constant_expression (ctx
, init
,
2855 allow_non_constant
, false,
2856 non_constant_p
, overflow_p
,
2858 ctx
->values
->put (r
, init
);
2860 else if (ctx
== &new_ctx
)
2861 /* We gave it a CONSTRUCTOR above. */;
2863 ctx
->values
->put (r
, NULL_TREE
);
2868 if (!literal_type_p (TREE_TYPE (t
)))
2870 if (!allow_non_constant
)
2872 error ("temporary of non-literal type %qT in a "
2873 "constant expression", TREE_TYPE (t
));
2874 explain_non_literal_class (TREE_TYPE (t
));
2876 *non_constant_p
= true;
2879 if ((AGGREGATE_TYPE_P (TREE_TYPE (t
)) || VECTOR_TYPE_P (TREE_TYPE (t
))))
2881 /* We're being expanded without an explicit target, so start
2882 initializing a new object; expansion with an explicit target
2883 strips the TARGET_EXPR before we get here. */
2885 new_ctx
.ctor
= build_constructor (TREE_TYPE (t
), NULL
);
2886 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx
.ctor
) = true;
2887 new_ctx
.object
= TARGET_EXPR_SLOT (t
);
2888 ctx
->values
->put (new_ctx
.object
, new_ctx
.ctor
);
2891 /* Pass false for 'addr' because this indicates
2892 initialization of a temporary. */
2893 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
2894 allow_non_constant
, false,
2895 non_constant_p
, overflow_p
, NULL
);
2896 if (!*non_constant_p
)
2897 /* Adjust the type of the result to the type of the temporary. */
2898 r
= adjust_temp_type (TREE_TYPE (t
), r
);
2904 /* In C++11 constexpr evaluation we are looking for the value,
2905 not the side-effect of the initialization. */
2906 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
2907 allow_non_constant
, false,
2908 non_constant_p
, overflow_p
, NULL
);
2911 /* else fall through */
2913 r
= cxx_eval_store_expression (ctx
, t
, allow_non_constant
, addr
,
2914 non_constant_p
, overflow_p
);
2918 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
2919 allow_non_constant
, addr
,
2920 non_constant_p
, overflow_p
, NULL
);
2924 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
2925 allow_non_constant
, addr
,
2926 non_constant_p
, overflow_p
, NULL
);
2930 case NON_LVALUE_EXPR
:
2931 case TRY_CATCH_EXPR
:
2932 case CLEANUP_POINT_EXPR
:
2933 case MUST_NOT_THROW_EXPR
:
2937 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
2938 allow_non_constant
, addr
,
2939 non_constant_p
, overflow_p
,
2943 /* These differ from cxx_eval_unary_expression in that this doesn't
2944 check for a constant operand or result; an address can be
2945 constant without its operand being, and vice versa. */
2947 r
= cxx_eval_indirect_ref (ctx
, t
, allow_non_constant
, addr
,
2948 non_constant_p
, overflow_p
);
2953 tree oldop
= TREE_OPERAND (t
, 0);
2954 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
2957 non_constant_p
, overflow_p
,
2959 /* Don't VERIFY_CONSTANT here. */
2960 if (*non_constant_p
)
2962 /* This function does more aggressive folding than fold itself. */
2963 r
= build_fold_addr_expr_with_type (op
, TREE_TYPE (t
));
2964 if (TREE_CODE (r
) == ADDR_EXPR
&& TREE_OPERAND (r
, 0) == oldop
)
2972 case FIX_TRUNC_EXPR
:
2977 case TRUTH_NOT_EXPR
:
2978 case FIXED_CONVERT_EXPR
:
2979 r
= cxx_eval_unary_expression (ctx
, t
, allow_non_constant
, addr
,
2980 non_constant_p
, overflow_p
);
2984 if (SIZEOF_EXPR_TYPE_P (t
))
2985 r
= cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t
, 0)),
2986 SIZEOF_EXPR
, false);
2987 else if (TYPE_P (TREE_OPERAND (t
, 0)))
2988 r
= cxx_sizeof_or_alignof_type (TREE_OPERAND (t
, 0), SIZEOF_EXPR
,
2991 r
= cxx_sizeof_or_alignof_expr (TREE_OPERAND (t
, 0), SIZEOF_EXPR
,
2993 if (r
== error_mark_node
)
2995 VERIFY_CONSTANT (r
);
3000 /* check_return_expr sometimes wraps a TARGET_EXPR in a
3001 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
3002 introduced by build_call_a. */
3003 tree op0
= TREE_OPERAND (t
, 0);
3004 tree op1
= TREE_OPERAND (t
, 1);
3006 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
3007 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
3008 r
= cxx_eval_constant_expression (ctx
, op0
, allow_non_constant
,
3009 addr
, non_constant_p
, overflow_p
,
3013 /* Check that the LHS is constant and then discard it. */
3014 cxx_eval_constant_expression (ctx
, op0
, allow_non_constant
,
3015 false, non_constant_p
, overflow_p
,
3017 op1
= TREE_OPERAND (t
, 1);
3018 r
= cxx_eval_constant_expression (ctx
, op1
, allow_non_constant
,
3019 addr
, non_constant_p
, overflow_p
,
3025 case POINTER_PLUS_EXPR
:
3029 case TRUNC_DIV_EXPR
:
3031 case FLOOR_DIV_EXPR
:
3032 case ROUND_DIV_EXPR
:
3033 case TRUNC_MOD_EXPR
:
3035 case ROUND_MOD_EXPR
:
3037 case EXACT_DIV_EXPR
:
3047 case TRUTH_XOR_EXPR
:
3054 case UNORDERED_EXPR
:
3064 r
= cxx_eval_binary_expression (ctx
, t
, allow_non_constant
, addr
,
3065 non_constant_p
, overflow_p
);
3068 /* fold can introduce non-IF versions of these; still treat them as
3069 short-circuiting. */
3070 case TRUTH_AND_EXPR
:
3071 case TRUTH_ANDIF_EXPR
:
3072 r
= cxx_eval_logical_expression (ctx
, t
, boolean_false_node
,
3074 allow_non_constant
, addr
,
3075 non_constant_p
, overflow_p
);
3079 case TRUTH_ORIF_EXPR
:
3080 r
= cxx_eval_logical_expression (ctx
, t
, boolean_true_node
,
3082 allow_non_constant
, addr
,
3083 non_constant_p
, overflow_p
);
3087 r
= cxx_eval_array_reference (ctx
, t
, allow_non_constant
, addr
,
3088 non_constant_p
, overflow_p
);
3092 if (is_overloaded_fn (t
))
3094 /* We can only get here in checking mode via
3095 build_non_dependent_expr, because any expression that
3096 calls or takes the address of the function will have
3097 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
3098 gcc_checking_assert (allow_non_constant
|| errorcount
);
3099 *non_constant_p
= true;
3102 r
= cxx_eval_component_reference (ctx
, t
, allow_non_constant
, addr
,
3103 non_constant_p
, overflow_p
);
3107 r
= cxx_eval_bit_field_ref (ctx
, t
, allow_non_constant
, addr
,
3108 non_constant_p
, overflow_p
);
3113 r
= cxx_eval_conditional_expression (ctx
, t
, allow_non_constant
, addr
,
3114 non_constant_p
, overflow_p
,
3119 r
= cxx_eval_bare_aggregate (ctx
, t
, allow_non_constant
, addr
,
3120 non_constant_p
, overflow_p
);
3124 /* We can get this in a defaulted constructor for a class with a
3125 non-static data member of array type. Either the initializer will
3126 be NULL, meaning default-initialization, or it will be an lvalue
3127 or xvalue of the same type, meaning direct-initialization from the
3128 corresponding member. */
3129 r
= cxx_eval_vec_init (ctx
, t
, allow_non_constant
, addr
,
3130 non_constant_p
, overflow_p
);
3135 r
= cxx_eval_trinary_expression (ctx
, t
, allow_non_constant
, addr
,
3136 non_constant_p
, overflow_p
);
3140 case VIEW_CONVERT_EXPR
:
3143 tree oldop
= TREE_OPERAND (t
, 0);
3144 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
3145 allow_non_constant
, addr
,
3146 non_constant_p
, overflow_p
,
3148 if (*non_constant_p
)
3150 if (POINTER_TYPE_P (TREE_TYPE (t
))
3151 && TREE_CODE (op
) == INTEGER_CST
3152 && !integer_zerop (op
))
3154 if (!allow_non_constant
)
3155 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3156 "reinterpret_cast from integer to pointer");
3157 *non_constant_p
= true;
3161 /* We didn't fold at the top so we could check for ptr-int
3164 r
= fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), op
);
3165 /* Conversion of an out-of-range value has implementation-defined
3166 behavior; the language considers it different from arithmetic
3167 overflow, which is undefined. */
3168 if (TREE_OVERFLOW_P (r
) && !TREE_OVERFLOW_P (op
))
3169 TREE_OVERFLOW (r
) = false;
3173 case EMPTY_CLASS_EXPR
:
3174 /* This is good enough for a function argument that might not get
3175 used, and they can't do anything with it, so just return it. */
3178 case STATEMENT_LIST
:
3180 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
3181 return cxx_eval_statement_list (&new_ctx
, t
, allow_non_constant
,
3182 non_constant_p
, overflow_p
, jump_target
);
3185 return cxx_eval_constant_expression (ctx
, BIND_EXPR_BODY (t
),
3186 allow_non_constant
, addr
,
3187 non_constant_p
, overflow_p
,
3190 case PREINCREMENT_EXPR
:
3191 case POSTINCREMENT_EXPR
:
3192 case PREDECREMENT_EXPR
:
3193 case POSTDECREMENT_EXPR
:
3194 return cxx_eval_increment_expression (ctx
, t
, allow_non_constant
,
3195 addr
, non_constant_p
, overflow_p
);
3201 case VEC_DELETE_EXPR
:
3204 /* GCC internal stuff. */
3207 case WITH_CLEANUP_EXPR
:
3208 case NON_DEPENDENT_EXPR
:
3211 if (!allow_non_constant
)
3212 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3213 "expression %qE is not a constant-expression", t
);
3214 *non_constant_p
= true;
3217 case PLACEHOLDER_EXPR
:
3218 if (!ctx
|| !ctx
->ctor
|| (addr
&& !ctx
->object
))
3220 /* A placeholder without a referent. We can get here when
3221 checking whether NSDMIs are noexcept, or in massage_init_elt;
3222 just say it's non-constant for now. */
3223 gcc_assert (allow_non_constant
);
3224 *non_constant_p
= true;
3229 /* Use of the value or address of the current object. We could
3230 use ctx->object unconditionally, but using ctx->ctor when we
3231 can is a minor optimization. */
3232 tree ctor
= addr
? ctx
->object
: ctx
->ctor
;
3233 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3234 (TREE_TYPE (t
), TREE_TYPE (ctor
)));
3235 return cxx_eval_constant_expression
3236 (ctx
, ctor
, allow_non_constant
, addr
,
3237 non_constant_p
, overflow_p
, NULL
);
3242 *jump_target
= TREE_OPERAND (t
, 0);
3243 gcc_assert (breaks (jump_target
) || continues (jump_target
));
3247 cxx_eval_loop_expr (ctx
, t
, allow_non_constant
,
3248 non_constant_p
, overflow_p
, jump_target
);
3252 cxx_eval_switch_expr (ctx
, t
, allow_non_constant
,
3253 non_constant_p
, overflow_p
, jump_target
);
3257 internal_error ("unexpected expression %qE of kind %s", t
,
3258 get_tree_code_name (TREE_CODE (t
)));
3259 *non_constant_p
= true;
3263 if (r
== error_mark_node
)
3264 *non_constant_p
= true;
3266 if (*non_constant_p
)
3273 cxx_eval_outermost_constant_expr (tree t
, bool allow_non_constant
,
3274 tree object
= NULL_TREE
)
3276 bool non_constant_p
= false;
3277 bool overflow_p
= false;
3278 constexpr_ctx ctx
= { NULL
, NULL
, NULL
, NULL
};
3279 hash_map
<tree
,tree
> map
;
3281 tree type
= initialized_type (t
);
3283 if (AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
))
3285 /* In C++14 an NSDMI can participate in aggregate initialization,
3286 and can refer to the address of the object being initialized, so
3287 we need to pass in the relevant VAR_DECL if we want to do the
3288 evaluation in a single pass. The evaluation will dynamically
3289 update ctx.values for the VAR_DECL. We use the same strategy
3290 for C++11 constexpr constructors that refer to the object being
3292 ctx
.ctor
= build_constructor (type
, NULL
);
3293 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctx
.ctor
) = true;
3296 if (TREE_CODE (t
) == TARGET_EXPR
)
3297 object
= TARGET_EXPR_SLOT (t
);
3298 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
3299 object
= AGGR_INIT_EXPR_SLOT (t
);
3301 ctx
.object
= object
;
3303 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3304 (type
, TREE_TYPE (object
)));
3305 if (object
&& DECL_P (object
))
3306 map
.put (object
, ctx
.ctor
);
3307 if (TREE_CODE (r
) == TARGET_EXPR
)
3308 /* Avoid creating another CONSTRUCTOR when we expand the
3310 r
= TARGET_EXPR_INITIAL (r
);
3313 r
= cxx_eval_constant_expression (&ctx
, r
, allow_non_constant
,
3314 false, &non_constant_p
, &overflow_p
, NULL
);
3316 verify_constant (r
, allow_non_constant
, &non_constant_p
, &overflow_p
);
3318 if (TREE_CODE (t
) != CONSTRUCTOR
3319 && cp_has_mutable_p (TREE_TYPE (t
)))
3321 /* We allow a mutable type if the original expression was a
3322 CONSTRUCTOR so that we can do aggregate initialization of
3323 constexpr variables. */
3324 if (!allow_non_constant
)
3325 error ("%qT cannot be the type of a complete constant expression "
3326 "because it has mutable sub-objects", type
);
3327 non_constant_p
= true;
3330 /* Technically we should check this for all subexpressions, but that
3331 runs into problems with our internal representation of pointer
3332 subtraction and the 5.19 rules are still in flux. */
3333 if (CONVERT_EXPR_CODE_P (TREE_CODE (r
))
3334 && ARITHMETIC_TYPE_P (TREE_TYPE (r
))
3335 && TREE_CODE (TREE_OPERAND (r
, 0)) == ADDR_EXPR
)
3337 if (!allow_non_constant
)
3338 error ("conversion from pointer type %qT "
3339 "to arithmetic type %qT in a constant-expression",
3340 TREE_TYPE (TREE_OPERAND (r
, 0)), TREE_TYPE (r
));
3341 non_constant_p
= true;
3344 if (!non_constant_p
&& overflow_p
)
3345 non_constant_p
= true;
3347 if (non_constant_p
&& !allow_non_constant
)
3348 return error_mark_node
;
3349 else if (non_constant_p
&& TREE_CONSTANT (r
))
3351 /* This isn't actually constant, so unset TREE_CONSTANT. */
3354 else if (TREE_CODE (r
) == CONSTRUCTOR
)
3355 r
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (r
), r
);
3357 r
= build_nop (TREE_TYPE (r
), r
);
3358 TREE_CONSTANT (r
) = false;
3360 else if (non_constant_p
|| r
== t
)
3363 if (TREE_CODE (r
) == CONSTRUCTOR
&& CLASS_TYPE_P (TREE_TYPE (r
)))
3365 if (TREE_CODE (t
) == TARGET_EXPR
3366 && TARGET_EXPR_INITIAL (t
) == r
)
3370 r
= get_target_expr (r
);
3371 TREE_CONSTANT (r
) = true;
3379 /* Returns true if T is a valid subexpression of a constant expression,
3380 even if it isn't itself a constant expression. */
3383 is_sub_constant_expr (tree t
)
3385 bool non_constant_p
= false;
3386 bool overflow_p
= false;
3387 constexpr_ctx ctx
= { NULL
, NULL
, NULL
, NULL
};
3388 hash_map
<tree
, tree
> map
;
3390 cxx_eval_constant_expression (&ctx
, t
, true, false, &non_constant_p
,
3392 return !non_constant_p
&& !overflow_p
;
3395 /* If T represents a constant expression returns its reduced value.
3396 Otherwise return error_mark_node. If T is dependent, then
3400 cxx_constant_value (tree t
, tree decl
)
3402 return cxx_eval_outermost_constant_expr (t
, false, decl
);
3405 /* If T is a constant expression, returns its reduced value.
3406 Otherwise, if T does not have TREE_CONSTANT set, returns T.
3407 Otherwise, returns a version of T without TREE_CONSTANT. */
3410 maybe_constant_value (tree t
, tree decl
)
3414 if (instantiation_dependent_expression_p (t
)
3415 || type_unknown_p (t
)
3416 || BRACE_ENCLOSED_INITIALIZER_P (t
)
3417 || !potential_constant_expression (t
))
3419 if (TREE_OVERFLOW_P (t
))
3421 t
= build_nop (TREE_TYPE (t
), t
);
3422 TREE_CONSTANT (t
) = false;
3427 r
= cxx_eval_outermost_constant_expr (t
, true, decl
);
3428 #ifdef ENABLE_CHECKING
3429 /* cp_tree_equal looks through NOPs, so allow them. */
3431 || CONVERT_EXPR_P (t
)
3432 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
3433 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
3434 || !cp_tree_equal (r
, t
));
3439 /* Like maybe_constant_value but first fully instantiate the argument.
3441 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
3442 (t, tf_none) followed by maybe_constant_value but is more efficient,
3443 because calls instantiation_dependent_expression_p and
3444 potential_constant_expression at most once. */
3447 fold_non_dependent_expr (tree t
)
3452 /* If we're in a template, but T isn't value dependent, simplify
3453 it. We're supposed to treat:
3455 template <typename T> void f(T[1 + 1]);
3456 template <typename T> void f(T[2]);
3458 as two declarations of the same function, for example. */
3459 if (processing_template_decl
)
3461 if (!instantiation_dependent_expression_p (t
)
3462 && potential_constant_expression (t
))
3464 HOST_WIDE_INT saved_processing_template_decl
;
3466 saved_processing_template_decl
= processing_template_decl
;
3467 processing_template_decl
= 0;
3468 t
= tsubst_copy_and_build (t
,
3471 /*in_decl=*/NULL_TREE
,
3472 /*function_p=*/false,
3473 /*integral_constant_expression_p=*/true);
3474 processing_template_decl
= saved_processing_template_decl
;
3476 if (type_unknown_p (t
)
3477 || BRACE_ENCLOSED_INITIALIZER_P (t
))
3479 if (TREE_OVERFLOW_P (t
))
3481 t
= build_nop (TREE_TYPE (t
), t
);
3482 TREE_CONSTANT (t
) = false;
3487 tree r
= cxx_eval_outermost_constant_expr (t
, true, NULL_TREE
);
3488 #ifdef ENABLE_CHECKING
3489 /* cp_tree_equal looks through NOPs, so allow them. */
3491 || CONVERT_EXPR_P (t
)
3492 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
3493 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
3494 || !cp_tree_equal (r
, t
));
3498 else if (TREE_OVERFLOW_P (t
))
3500 t
= build_nop (TREE_TYPE (t
), t
);
3501 TREE_CONSTANT (t
) = false;
3506 return maybe_constant_value (t
);
3509 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
3510 than wrapped in a TARGET_EXPR. */
3513 maybe_constant_init (tree t
, tree decl
)
3515 if (TREE_CODE (t
) == EXPR_STMT
)
3516 t
= TREE_OPERAND (t
, 0);
3517 if (TREE_CODE (t
) == CONVERT_EXPR
3518 && VOID_TYPE_P (TREE_TYPE (t
)))
3519 t
= TREE_OPERAND (t
, 0);
3520 if (TREE_CODE (t
) == INIT_EXPR
)
3521 t
= TREE_OPERAND (t
, 1);
3522 t
= maybe_constant_value (t
, decl
);
3523 if (TREE_CODE (t
) == TARGET_EXPR
)
3525 tree init
= TARGET_EXPR_INITIAL (t
);
3526 if (TREE_CODE (init
) == CONSTRUCTOR
)
3533 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
3534 /* Return true if the object referred to by REF has automatic or thread
3537 enum { ck_ok
, ck_bad
, ck_unknown
};
3539 check_automatic_or_tls (tree ref
)
3542 HOST_WIDE_INT bitsize
, bitpos
;
3544 int volatilep
= 0, unsignedp
= 0;
3545 tree decl
= get_inner_reference (ref
, &bitsize
, &bitpos
, &offset
,
3546 &mode
, &unsignedp
, &volatilep
, false);
3549 /* If there isn't a decl in the middle, we don't know the linkage here,
3550 and this isn't a constant expression anyway. */
3553 dk
= decl_storage_duration (decl
);
3554 return (dk
== dk_auto
|| dk
== dk_thread
) ? ck_bad
: ck_ok
;
3558 /* Return true if T denotes a potentially constant expression. Issue
3559 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
3560 an lvalue-rvalue conversion is implied.
3562 C++0x [expr.const] used to say
3564 6 An expression is a potential constant expression if it is
3565 a constant expression where all occurrences of function
3566 parameters are replaced by arbitrary constant expressions
3567 of the appropriate type.
3569 2 A conditional expression is a constant expression unless it
3570 involves one of the following as a potentially evaluated
3571 subexpression (3.2), but subexpressions of logical AND (5.14),
3572 logical OR (5.15), and conditional (5.16) operations that are
3573 not evaluated are not considered. */
3576 potential_constant_expression_1 (tree t
, bool want_rval
, tsubst_flags_t flags
)
3578 enum { any
= false, rval
= true };
3582 if (t
== error_mark_node
)
3586 if (TREE_THIS_VOLATILE (t
))
3588 if (flags
& tf_error
)
3589 error ("expression %qE has side-effects", t
);
3592 if (CONSTANT_CLASS_P (t
))
3595 switch (TREE_CODE (t
))
3601 case TEMPLATE_ID_EXPR
:
3604 case CASE_LABEL_EXPR
:
3610 case TEMPLATE_PARM_INDEX
:
3612 case IDENTIFIER_NODE
:
3613 case USERDEF_LITERAL
:
3614 /* We can see a FIELD_DECL in a pointer-to-member expression. */
3619 case PLACEHOLDER_EXPR
:
3624 case AGGR_INIT_EXPR
:
3626 /* -- an invocation of a function other than a constexpr function
3627 or a constexpr constructor. */
3629 tree fun
= get_function_named_in_call (t
);
3630 const int nargs
= call_expr_nargs (t
);
3633 if (fun
== NULL_TREE
)
3635 /* fold_call_expr can't do anything with IFN calls. */
3636 if (flags
& tf_error
)
3637 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3638 "call to internal function");
3641 if (is_overloaded_fn (fun
))
3643 if (TREE_CODE (fun
) == FUNCTION_DECL
)
3645 if (builtin_valid_in_constant_expr_p (fun
))
3647 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
3648 /* Allow any built-in function; if the expansion
3649 isn't constant, we'll deal with that then. */
3650 && !is_builtin_fn (fun
))
3652 if (flags
& tf_error
)
3654 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3655 "call to non-constexpr function %qD", fun
);
3656 explain_invalid_constexpr_fn (fun
);
3660 /* A call to a non-static member function takes the address
3661 of the object as the first argument. But in a constant
3662 expression the address will be folded away, so look
3664 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
3665 && !DECL_CONSTRUCTOR_P (fun
))
3667 tree x
= get_nth_callarg (t
, 0);
3668 if (is_this_parameter (x
))
3670 else if (!potential_constant_expression_1 (x
, rval
, flags
))
3677 if (!potential_constant_expression_1 (fun
, true, flags
))
3679 fun
= get_first_fn (fun
);
3681 /* Skip initial arguments to base constructors. */
3682 if (DECL_BASE_CONSTRUCTOR_P (fun
))
3683 i
= num_artificial_parms_for (fun
);
3684 fun
= DECL_ORIGIN (fun
);
3688 if (potential_constant_expression_1 (fun
, rval
, flags
))
3689 /* Might end up being a constant function pointer. */;
3693 for (; i
< nargs
; ++i
)
3695 tree x
= get_nth_callarg (t
, i
);
3696 if (!potential_constant_expression_1 (x
, rval
, flags
))
3702 case NON_LVALUE_EXPR
:
3703 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
3704 -- an lvalue of integral type that refers to a non-volatile
3705 const variable or static data member initialized with
3706 constant expressions, or
3708 -- an lvalue of literal type that refers to non-volatile
3709 object defined with constexpr, or that refers to a
3710 sub-object of such an object; */
3711 return potential_constant_expression_1 (TREE_OPERAND (t
, 0), rval
, flags
);
3714 if (want_rval
&& !decl_constant_var_p (t
)
3715 && !var_in_constexpr_fn (t
)
3716 && !dependent_type_p (TREE_TYPE (t
)))
3718 if (flags
& tf_error
)
3719 non_const_var_error (t
);
3726 case VIEW_CONVERT_EXPR
:
3727 /* -- a reinterpret_cast. FIXME not implemented, and this rule
3728 may change to something more specific to type-punning (DR 1312). */
3730 tree from
= TREE_OPERAND (t
, 0);
3731 if (POINTER_TYPE_P (TREE_TYPE (t
))
3732 && TREE_CODE (from
) == INTEGER_CST
3733 && !integer_zerop (from
))
3735 if (flags
& tf_error
)
3736 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3737 "reinterpret_cast from integer to pointer");
3740 return (potential_constant_expression_1
3741 (from
, TREE_CODE (t
) != VIEW_CONVERT_EXPR
, flags
));
3745 /* -- a unary operator & that is applied to an lvalue that
3746 designates an object with thread or automatic storage
3748 t
= TREE_OPERAND (t
, 0);
3750 if (TREE_CODE (t
) == OFFSET_REF
&& PTRMEM_OK_P (t
))
3751 /* A pointer-to-member constant. */
3755 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
3756 any checking here, as we might dereference the pointer later. If
3757 we remove this code, also remove check_automatic_or_tls. */
3758 i
= check_automatic_or_tls (t
);
3763 if (flags
& tf_error
)
3764 error ("address-of an object %qE with thread local or "
3765 "automatic storage is not a constant expression", t
);
3769 return potential_constant_expression_1 (t
, any
, flags
);
3775 /* -- a class member access unless its postfix-expression is
3776 of literal type or of pointer to literal type. */
3777 /* This test would be redundant, as it follows from the
3778 postfix-expression being a potential constant expression. */
3779 return potential_constant_expression_1 (TREE_OPERAND (t
, 0),
3782 case EXPR_PACK_EXPANSION
:
3783 return potential_constant_expression_1 (PACK_EXPANSION_PATTERN (t
),
3788 tree x
= TREE_OPERAND (t
, 0);
3790 if (is_this_parameter (x
))
3792 if (DECL_CONTEXT (x
)
3793 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x
)))
3795 if (flags
& tf_error
)
3796 error ("use of %<this%> in a constant expression");
3801 return potential_constant_expression_1 (x
, rval
, flags
);
3804 case STATEMENT_LIST
:
3806 tree_stmt_iterator i
;
3807 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
3809 if (!potential_constant_expression_1 (tsi_stmt (i
), any
, flags
))
3817 if (cxx_dialect
< cxx14
)
3819 if (!potential_constant_expression_1 (TREE_OPERAND (t
, 0), any
, flags
))
3821 if (!potential_constant_expression_1 (TREE_OPERAND (t
, 1), rval
, flags
))
3826 if (cxx_dialect
< cxx14
)
3828 if (!potential_constant_expression_1 (TREE_OPERAND (t
, 0), rval
, flags
))
3830 if (!potential_constant_expression_1 (TREE_OPERAND (t
, 2), rval
, flags
))
3835 if (!potential_constant_expression_1 (IF_COND (t
), rval
, flags
))
3837 if (!potential_constant_expression_1 (THEN_CLAUSE (t
), any
, flags
))
3839 if (!potential_constant_expression_1 (ELSE_CLAUSE (t
), any
, flags
))
3844 if (!potential_constant_expression_1 (DO_COND (t
), rval
, flags
))
3846 if (!potential_constant_expression_1 (DO_BODY (t
), any
, flags
))
3851 if (!potential_constant_expression_1 (FOR_INIT_STMT (t
), any
, flags
))
3853 if (!potential_constant_expression_1 (FOR_COND (t
), rval
, flags
))
3855 if (!potential_constant_expression_1 (FOR_EXPR (t
), any
, flags
))
3857 if (!potential_constant_expression_1 (FOR_BODY (t
), any
, flags
))
3862 if (!potential_constant_expression_1 (WHILE_COND (t
), rval
, flags
))
3864 if (!potential_constant_expression_1 (WHILE_BODY (t
), any
, flags
))
3869 if (!potential_constant_expression_1 (SWITCH_STMT_COND (t
), rval
, flags
))
3871 if (!potential_constant_expression_1 (SWITCH_STMT_BODY (t
), any
, flags
))
3876 case DYNAMIC_CAST_EXPR
:
3877 case PSEUDO_DTOR_EXPR
:
3881 case VEC_DELETE_EXPR
:
3884 case OMP_ATOMIC_READ
:
3885 case OMP_ATOMIC_CAPTURE_OLD
:
3886 case OMP_ATOMIC_CAPTURE_NEW
:
3887 /* GCC internal stuff. */
3891 case TRANSACTION_EXPR
:
3894 if (flags
& tf_error
)
3895 error ("expression %qE is not a constant-expression", t
);
3899 /* -- a typeid expression whose operand is of polymorphic
3902 tree e
= TREE_OPERAND (t
, 0);
3903 if (!TYPE_P (e
) && !type_dependent_expression_p (e
)
3904 && TYPE_POLYMORPHIC_P (TREE_TYPE (e
)))
3906 if (flags
& tf_error
)
3907 error ("typeid-expression is not a constant expression "
3908 "because %qE is of polymorphic type", e
);
3915 /* -- a subtraction where both operands are pointers. */
3916 if (TYPE_PTR_P (TREE_OPERAND (t
, 0))
3917 && TYPE_PTR_P (TREE_OPERAND (t
, 1)))
3919 if (flags
& tf_error
)
3920 error ("difference of two pointer expressions is not "
3921 "a constant expression");
3933 /* -- a relational or equality operator where at least
3934 one of the operands is a pointer. */
3935 if (TYPE_PTR_P (TREE_OPERAND (t
, 0))
3936 || TYPE_PTR_P (TREE_OPERAND (t
, 1)))
3938 if (flags
& tf_error
)
3939 error ("pointer comparison expression is not a "
3940 "constant expression");
3946 case PREINCREMENT_EXPR
:
3947 case POSTINCREMENT_EXPR
:
3948 case PREDECREMENT_EXPR
:
3949 case POSTDECREMENT_EXPR
:
3950 if (cxx_dialect
< cxx14
)
3956 if (TYPE_P (TREE_OPERAND (t
, 0)))
3958 /* else fall through. */
3964 case FIX_TRUNC_EXPR
:
3968 case TRUTH_NOT_EXPR
:
3969 case FIXED_CONVERT_EXPR
:
3970 case UNARY_PLUS_EXPR
:
3972 return potential_constant_expression_1 (TREE_OPERAND (t
, 0), rval
,
3976 case CONST_CAST_EXPR
:
3977 case STATIC_CAST_EXPR
:
3978 case REINTERPRET_CAST_EXPR
:
3979 case IMPLICIT_CONV_EXPR
:
3980 if (cxx_dialect
< cxx11
3981 && !dependent_type_p (TREE_TYPE (t
))
3982 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
)))
3983 /* In C++98, a conversion to non-integral type can't be part of a
3984 constant expression. */
3986 if (flags
& tf_error
)
3987 error ("cast to non-integral type %qT in a constant expression",
3992 return (potential_constant_expression_1
3993 (TREE_OPERAND (t
, 0),
3994 TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
, flags
));
3997 return potential_constant_expression_1 (BIND_EXPR_BODY (t
),
4000 case WITH_CLEANUP_EXPR
:
4001 case CLEANUP_POINT_EXPR
:
4002 case MUST_NOT_THROW_EXPR
:
4003 case TRY_CATCH_EXPR
:
4008 case NON_DEPENDENT_EXPR
:
4009 /* For convenience. */
4011 return potential_constant_expression_1 (TREE_OPERAND (t
, 0),
4015 return potential_constant_expression_1 (TREE_OPERAND (t
, 1),
4019 if (!literal_type_p (TREE_TYPE (t
)))
4021 if (flags
& tf_error
)
4023 error ("temporary of non-literal type %qT in a "
4024 "constant expression", TREE_TYPE (t
));
4025 explain_non_literal_class (TREE_TYPE (t
));
4030 return potential_constant_expression_1 (TREE_OPERAND (t
, 1),
4035 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
4036 constructor_elt
*ce
;
4037 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
4038 if (!potential_constant_expression_1 (ce
->value
, want_rval
, flags
))
4045 gcc_assert (TREE_PURPOSE (t
) == NULL_TREE
4046 || DECL_P (TREE_PURPOSE (t
)));
4047 if (!potential_constant_expression_1 (TREE_VALUE (t
), want_rval
,
4050 if (TREE_CHAIN (t
) == NULL_TREE
)
4052 return potential_constant_expression_1 (TREE_CHAIN (t
), want_rval
,
4056 case TRUNC_DIV_EXPR
:
4058 case FLOOR_DIV_EXPR
:
4059 case ROUND_DIV_EXPR
:
4060 case TRUNC_MOD_EXPR
:
4062 case ROUND_MOD_EXPR
:
4064 tree denom
= TREE_OPERAND (t
, 1);
4065 if (!potential_constant_expression_1 (denom
, rval
, flags
))
4067 /* We can't call cxx_eval_outermost_constant_expr on an expression
4068 that hasn't been through instantiate_non_dependent_expr yet. */
4069 if (!processing_template_decl
)
4070 denom
= cxx_eval_outermost_constant_expr (denom
, true);
4071 if (integer_zerop (denom
))
4073 if (flags
& tf_error
)
4074 error ("division by zero is not a constant-expression");
4080 return potential_constant_expression_1 (TREE_OPERAND (t
, 0),
4087 /* check_return_expr sometimes wraps a TARGET_EXPR in a
4088 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
4089 introduced by build_call_a. */
4090 tree op0
= TREE_OPERAND (t
, 0);
4091 tree op1
= TREE_OPERAND (t
, 1);
4093 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
4094 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
4095 return potential_constant_expression_1 (op0
, want_rval
, flags
);
4100 /* If the first operand is the non-short-circuit constant, look at
4101 the second operand; otherwise we only care about the first one for
4103 case TRUTH_AND_EXPR
:
4104 case TRUTH_ANDIF_EXPR
:
4105 tmp
= boolean_true_node
;
4108 case TRUTH_ORIF_EXPR
:
4109 tmp
= boolean_false_node
;
4112 tree op
= TREE_OPERAND (t
, 0);
4113 if (!potential_constant_expression_1 (op
, rval
, flags
))
4115 if (!processing_template_decl
)
4116 op
= cxx_eval_outermost_constant_expr (op
, true);
4117 if (tree_int_cst_equal (op
, tmp
))
4118 return potential_constant_expression_1 (TREE_OPERAND (t
, 1), rval
, flags
);
4125 case POINTER_PLUS_EXPR
:
4127 case EXACT_DIV_EXPR
:
4137 case TRUTH_XOR_EXPR
:
4138 case UNORDERED_EXPR
:
4151 case ARRAY_RANGE_REF
:
4155 for (i
= 0; i
< 2; ++i
)
4156 if (!potential_constant_expression_1 (TREE_OPERAND (t
, i
),
4161 case CILK_SYNC_STMT
:
4162 case CILK_SPAWN_STMT
:
4163 case ARRAY_NOTATION_REF
:
4168 for (i
= 0; i
< 3; ++i
)
4169 if (!potential_constant_expression_1 (TREE_OPERAND (t
, i
),
4176 /* If the condition is a known constant, we know which of the legs we
4177 care about; otherwise we only require that the condition and
4178 either of the legs be potentially constant. */
4179 tmp
= TREE_OPERAND (t
, 0);
4180 if (!potential_constant_expression_1 (tmp
, rval
, flags
))
4182 if (!processing_template_decl
)
4183 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
4184 if (integer_zerop (tmp
))
4185 return potential_constant_expression_1 (TREE_OPERAND (t
, 2),
4187 else if (TREE_CODE (tmp
) == INTEGER_CST
)
4188 return potential_constant_expression_1 (TREE_OPERAND (t
, 1),
4190 for (i
= 1; i
< 3; ++i
)
4191 if (potential_constant_expression_1 (TREE_OPERAND (t
, i
),
4192 want_rval
, tf_none
))
4194 if (flags
& tf_error
)
4195 error ("expression %qE is not a constant-expression", t
);
4199 if (VEC_INIT_EXPR_IS_CONSTEXPR (t
))
4201 if (flags
& tf_error
)
4203 error ("non-constant array initialization");
4204 diagnose_non_constexpr_vec_init (t
);
4209 if (objc_is_property_ref (t
))
4212 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t
)));
4218 /* The main entry point to the above. */
4221 potential_constant_expression (tree t
)
4223 return potential_constant_expression_1 (t
, false, tf_none
);
4226 /* As above, but require a constant rvalue. */
4229 potential_rvalue_constant_expression (tree t
)
4231 return potential_constant_expression_1 (t
, true, tf_none
);
4234 /* Like above, but complain about non-constant expressions. */
4237 require_potential_constant_expression (tree t
)
4239 return potential_constant_expression_1 (t
, false, tf_warning_or_error
);
4242 /* Cross product of the above. */
4245 require_potential_rvalue_constant_expression (tree t
)
4247 return potential_constant_expression_1 (t
, true, tf_warning_or_error
);
4250 #include "gt-cp-constexpr.h"