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"
37 static bool verify_constant (tree
, bool, bool *, bool *);
38 #define VERIFY_CONSTANT(X) \
40 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
44 /* Returns true iff FUN is an instantiation of a constexpr function
45 template or a defaulted constexpr function. */
48 is_instantiation_of_constexpr (tree fun
)
50 return ((DECL_TEMPLOID_INSTANTIATION (fun
)
51 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun
)))
52 || (DECL_DEFAULTED_FN (fun
)
53 && DECL_DECLARED_CONSTEXPR_P (fun
)));
56 /* Return true if T is a literal type. */
59 literal_type_p (tree t
)
62 || TREE_CODE (t
) == VECTOR_TYPE
63 || TREE_CODE (t
) == REFERENCE_TYPE
64 || (VOID_TYPE_P (t
) && cxx_dialect
>= cxx14
))
68 t
= complete_type (t
);
69 gcc_assert (COMPLETE_TYPE_P (t
) || errorcount
);
70 return CLASSTYPE_LITERAL_P (t
);
72 if (TREE_CODE (t
) == ARRAY_TYPE
)
73 return literal_type_p (strip_array_types (t
));
77 /* If DECL is a variable declared `constexpr', require its type
78 be literal. Return the DECL if OK, otherwise NULL. */
81 ensure_literal_type_for_constexpr_object (tree decl
)
83 tree type
= TREE_TYPE (decl
);
85 && (DECL_DECLARED_CONSTEXPR_P (decl
)
86 || var_in_constexpr_fn (decl
))
87 && !processing_template_decl
)
89 tree stype
= strip_array_types (type
);
90 if (CLASS_TYPE_P (stype
) && !COMPLETE_TYPE_P (complete_type (stype
)))
91 /* Don't complain here, we'll complain about incompleteness
92 when we try to initialize the variable. */;
93 else if (!literal_type_p (type
))
95 if (DECL_DECLARED_CONSTEXPR_P (decl
))
96 error ("the type %qT of constexpr variable %qD is not literal",
100 error ("variable %qD of non-literal type %qT in %<constexpr%> "
101 "function", decl
, type
);
102 cp_function_chain
->invalid_constexpr
= true;
104 explain_non_literal_class (type
);
111 /* Representation of entries in the constexpr function definition table. */
113 struct GTY((for_user
)) constexpr_fundef
{
118 struct constexpr_fundef_hasher
: ggc_hasher
<constexpr_fundef
*>
120 static hashval_t
hash (constexpr_fundef
*);
121 static bool equal (constexpr_fundef
*, constexpr_fundef
*);
124 /* This table holds all constexpr function definitions seen in
125 the current translation unit. */
127 static GTY (()) hash_table
<constexpr_fundef_hasher
> *constexpr_fundef_table
;
129 /* Utility function used for managing the constexpr function table.
130 Return true if the entries pointed to by P and Q are for the
131 same constexpr function. */
134 constexpr_fundef_hasher::equal (constexpr_fundef
*lhs
, constexpr_fundef
*rhs
)
136 return lhs
->decl
== rhs
->decl
;
139 /* Utility function used for managing the constexpr function table.
140 Return a hash value for the entry pointed to by Q. */
143 constexpr_fundef_hasher::hash (constexpr_fundef
*fundef
)
145 return DECL_UID (fundef
->decl
);
148 /* Return a previously saved definition of function FUN. */
150 static constexpr_fundef
*
151 retrieve_constexpr_fundef (tree fun
)
153 constexpr_fundef fundef
= { NULL
, NULL
};
154 if (constexpr_fundef_table
== NULL
)
158 return constexpr_fundef_table
->find (&fundef
);
161 /* Check whether the parameter and return types of FUN are valid for a
162 constexpr function, and complain if COMPLAIN. */
165 is_valid_constexpr_fn (tree fun
, bool complain
)
169 if (DECL_INHERITED_CTOR_BASE (fun
)
170 && TREE_CODE (fun
) == TEMPLATE_DECL
)
174 error ("inherited constructor %qD is not constexpr",
175 get_inherited_ctor (fun
));
179 for (tree parm
= FUNCTION_FIRST_USER_PARM (fun
);
180 parm
!= NULL_TREE
; parm
= TREE_CHAIN (parm
))
181 if (!literal_type_p (TREE_TYPE (parm
)))
186 error ("invalid type for parameter %d of constexpr "
187 "function %q+#D", DECL_PARM_INDEX (parm
), fun
);
188 explain_non_literal_class (TREE_TYPE (parm
));
193 if (!DECL_CONSTRUCTOR_P (fun
))
195 tree rettype
= TREE_TYPE (TREE_TYPE (fun
));
196 if (!literal_type_p (rettype
))
201 error ("invalid return type %qT of constexpr function %q+D",
203 explain_non_literal_class (rettype
);
207 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
208 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun
)))
213 error ("enclosing class of constexpr non-static member "
214 "function %q+#D is not a literal type", fun
);
215 explain_non_literal_class (DECL_CONTEXT (fun
));
219 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun
)))
223 error ("%q#T has virtual base classes", DECL_CONTEXT (fun
));
229 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
230 for a member of an anonymous aggregate, INIT is the initializer for that
231 member, and VEC_OUTER is the vector of constructor elements for the class
232 whose constructor we are processing. Add the initializer to the vector
233 and return true to indicate success. */
236 build_anon_member_initialization (tree member
, tree init
,
237 vec
<constructor_elt
, va_gc
> **vec_outer
)
239 /* MEMBER presents the relevant fields from the inside out, but we need
240 to build up the initializer from the outside in so that we can reuse
241 previously built CONSTRUCTORs if this is, say, the second field in an
242 anonymous struct. So we use a vec as a stack. */
243 auto_vec
<tree
, 2> fields
;
246 fields
.safe_push (TREE_OPERAND (member
, 1));
247 member
= TREE_OPERAND (member
, 0);
249 while (ANON_AGGR_TYPE_P (TREE_TYPE (member
))
250 && TREE_CODE (member
) == COMPONENT_REF
);
252 /* VEC has the constructor elements vector for the context of FIELD.
253 If FIELD is an anonymous aggregate, we will push inside it. */
254 vec
<constructor_elt
, va_gc
> **vec
= vec_outer
;
256 while (field
= fields
.pop(),
257 ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
260 /* If there is already an outer constructor entry for the anonymous
261 aggregate FIELD, use it; otherwise, insert one. */
262 if (vec_safe_is_empty (*vec
)
263 || (*vec
)->last().index
!= field
)
265 ctor
= build_constructor (TREE_TYPE (field
), NULL
);
266 CONSTRUCTOR_APPEND_ELT (*vec
, field
, ctor
);
269 ctor
= (*vec
)->last().value
;
270 vec
= &CONSTRUCTOR_ELTS (ctor
);
273 /* Now we're at the innermost field, the one that isn't an anonymous
274 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
275 gcc_assert (fields
.is_empty());
276 CONSTRUCTOR_APPEND_ELT (*vec
, field
, init
);
281 /* Subroutine of build_constexpr_constructor_member_initializers.
282 The expression tree T represents a data member initialization
283 in a (constexpr) constructor definition. Build a pairing of
284 the data member with its initializer, and prepend that pair
285 to the existing initialization pair INITS. */
288 build_data_member_initialization (tree t
, vec
<constructor_elt
, va_gc
> **vec
)
291 if (TREE_CODE (t
) == CLEANUP_POINT_EXPR
)
292 t
= TREE_OPERAND (t
, 0);
293 if (TREE_CODE (t
) == EXPR_STMT
)
294 t
= TREE_OPERAND (t
, 0);
295 if (t
== error_mark_node
)
297 if (TREE_CODE (t
) == STATEMENT_LIST
)
299 tree_stmt_iterator i
;
300 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
302 if (! build_data_member_initialization (tsi_stmt (i
), vec
))
307 if (TREE_CODE (t
) == CLEANUP_STMT
)
309 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
310 but we can in a constexpr constructor for a non-literal class. Just
311 ignore it; either all the initialization will be constant, in which
312 case the cleanup can't run, or it can't be constexpr.
313 Still recurse into CLEANUP_BODY. */
314 return build_data_member_initialization (CLEANUP_BODY (t
), vec
);
316 if (TREE_CODE (t
) == CONVERT_EXPR
)
317 t
= TREE_OPERAND (t
, 0);
318 if (TREE_CODE (t
) == INIT_EXPR
319 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
320 use what this function builds for cx_check_missing_mem_inits, and
321 assignment in the ctor body doesn't count. */
322 || (cxx_dialect
< cxx14
&& TREE_CODE (t
) == MODIFY_EXPR
))
324 member
= TREE_OPERAND (t
, 0);
325 init
= break_out_target_exprs (TREE_OPERAND (t
, 1));
327 else if (TREE_CODE (t
) == CALL_EXPR
)
329 tree fn
= get_callee_fndecl (t
);
330 if (!fn
|| !DECL_CONSTRUCTOR_P (fn
))
331 /* We're only interested in calls to subobject constructors. */
333 member
= CALL_EXPR_ARG (t
, 0);
334 /* We don't use build_cplus_new here because it complains about
335 abstract bases. Leaving the call unwrapped means that it has the
336 wrong type, but cxx_eval_constant_expression doesn't care. */
337 init
= break_out_target_exprs (t
);
339 else if (TREE_CODE (t
) == BIND_EXPR
)
340 return build_data_member_initialization (BIND_EXPR_BODY (t
), vec
);
342 /* Don't add anything else to the CONSTRUCTOR. */
344 if (INDIRECT_REF_P (member
))
345 member
= TREE_OPERAND (member
, 0);
346 if (TREE_CODE (member
) == NOP_EXPR
)
350 if (TREE_CODE (op
) == ADDR_EXPR
)
352 gcc_assert (same_type_ignoring_top_level_qualifiers_p
353 (TREE_TYPE (TREE_TYPE (op
)),
354 TREE_TYPE (TREE_TYPE (member
))));
355 /* Initializing a cv-qualified member; we need to look through
359 else if (op
== current_class_ptr
360 && (same_type_ignoring_top_level_qualifiers_p
361 (TREE_TYPE (TREE_TYPE (member
)),
362 current_class_type
)))
363 /* Delegating constructor. */
367 /* This is an initializer for an empty base; keep it for now so
368 we can check it in cxx_eval_bare_aggregate. */
369 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member
))));
372 if (TREE_CODE (member
) == ADDR_EXPR
)
373 member
= TREE_OPERAND (member
, 0);
374 if (TREE_CODE (member
) == COMPONENT_REF
)
376 tree aggr
= TREE_OPERAND (member
, 0);
377 if (TREE_CODE (aggr
) != COMPONENT_REF
)
378 /* Normal member initialization. */
379 member
= TREE_OPERAND (member
, 1);
380 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr
)))
381 /* Initializing a member of an anonymous union. */
382 return build_anon_member_initialization (member
, init
, vec
);
384 /* We're initializing a vtable pointer in a base. Leave it as
385 COMPONENT_REF so we remember the path to get to the vfield. */
386 gcc_assert (TREE_TYPE (member
) == vtbl_ptr_type_node
);
389 CONSTRUCTOR_APPEND_ELT (*vec
, member
, init
);
393 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
394 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
395 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
398 check_constexpr_bind_expr_vars (tree t
)
400 gcc_assert (TREE_CODE (t
) == BIND_EXPR
);
402 for (tree var
= BIND_EXPR_VARS (t
); var
; var
= DECL_CHAIN (var
))
403 if (TREE_CODE (var
) == TYPE_DECL
404 && DECL_IMPLICIT_TYPEDEF_P (var
))
409 /* Subroutine of check_constexpr_ctor_body. */
412 check_constexpr_ctor_body_1 (tree last
, tree list
)
414 switch (TREE_CODE (list
))
417 if (TREE_CODE (DECL_EXPR_DECL (list
)) == USING_DECL
)
421 case CLEANUP_POINT_EXPR
:
422 return check_constexpr_ctor_body (last
, TREE_OPERAND (list
, 0),
426 if (!check_constexpr_bind_expr_vars (list
)
427 || !check_constexpr_ctor_body (last
, BIND_EXPR_BODY (list
),
441 /* Make sure that there are no statements after LAST in the constructor
442 body represented by LIST. */
445 check_constexpr_ctor_body (tree last
, tree list
, bool complain
)
447 /* C++14 doesn't require a constexpr ctor to have an empty body. */
448 if (cxx_dialect
>= cxx14
)
452 if (TREE_CODE (list
) == STATEMENT_LIST
)
454 tree_stmt_iterator i
= tsi_last (list
);
455 for (; !tsi_end_p (i
); tsi_prev (&i
))
457 tree t
= tsi_stmt (i
);
460 if (!check_constexpr_ctor_body_1 (last
, t
))
467 else if (list
!= last
468 && !check_constexpr_ctor_body_1 (last
, list
))
473 error ("constexpr constructor does not have empty body");
474 DECL_DECLARED_CONSTEXPR_P (current_function_decl
) = false;
479 /* V is a vector of constructor elements built up for the base and member
480 initializers of a constructor for TYPE. They need to be in increasing
481 offset order, which they might not be yet if TYPE has a primary base
482 which is not first in the base-clause or a vptr and at least one base
483 all of which are non-primary. */
485 static vec
<constructor_elt
, va_gc
> *
486 sort_constexpr_mem_initializers (tree type
, vec
<constructor_elt
, va_gc
> *v
)
488 tree pri
= CLASSTYPE_PRIMARY_BINFO (type
);
494 field_type
= BINFO_TYPE (pri
);
495 else if (TYPE_CONTAINS_VPTR_P (type
))
496 field_type
= vtbl_ptr_type_node
;
500 /* Find the element for the primary base or vptr and move it to the
501 beginning of the vec. */
502 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
503 if (TREE_TYPE (ce
->index
) == field_type
)
506 if (i
> 0 && i
< vec_safe_length (v
))
508 vec
<constructor_elt
, va_gc
> &vref
= *v
;
509 constructor_elt elt
= vref
[i
];
518 /* Build compile-time evalable representations of member-initializer list
519 for a constexpr constructor. */
522 build_constexpr_constructor_member_initializers (tree type
, tree body
)
524 vec
<constructor_elt
, va_gc
> *vec
= NULL
;
526 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
527 || TREE_CODE (body
) == EH_SPEC_BLOCK
)
528 body
= TREE_OPERAND (body
, 0);
529 if (TREE_CODE (body
) == STATEMENT_LIST
)
530 body
= STATEMENT_LIST_HEAD (body
)->stmt
;
531 body
= BIND_EXPR_BODY (body
);
532 if (TREE_CODE (body
) == CLEANUP_POINT_EXPR
)
534 body
= TREE_OPERAND (body
, 0);
535 if (TREE_CODE (body
) == EXPR_STMT
)
536 body
= TREE_OPERAND (body
, 0);
537 if (TREE_CODE (body
) == INIT_EXPR
538 && (same_type_ignoring_top_level_qualifiers_p
539 (TREE_TYPE (TREE_OPERAND (body
, 0)),
540 current_class_type
)))
543 return TREE_OPERAND (body
, 1);
545 ok
= build_data_member_initialization (body
, &vec
);
547 else if (TREE_CODE (body
) == STATEMENT_LIST
)
549 tree_stmt_iterator i
;
550 for (i
= tsi_start (body
); !tsi_end_p (i
); tsi_next (&i
))
552 ok
= build_data_member_initialization (tsi_stmt (i
), &vec
);
557 else if (TREE_CODE (body
) == TRY_BLOCK
)
559 error ("body of %<constexpr%> constructor cannot be "
560 "a function-try-block");
561 return error_mark_node
;
563 else if (EXPR_P (body
))
564 ok
= build_data_member_initialization (body
, &vec
);
566 gcc_assert (errorcount
> 0);
569 if (vec_safe_length (vec
) > 0)
571 /* In a delegating constructor, return the target. */
572 constructor_elt
*ce
= &(*vec
)[0];
573 if (ce
->index
== current_class_ptr
)
580 vec
= sort_constexpr_mem_initializers (type
, vec
);
581 return build_constructor (type
, vec
);
584 return error_mark_node
;
587 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
588 declared to be constexpr, or a sub-statement thereof. Returns the
589 return value if suitable, error_mark_node for a statement not allowed in
590 a constexpr function, or NULL_TREE if no return value was found. */
593 constexpr_fn_retval (tree body
)
595 switch (TREE_CODE (body
))
599 tree_stmt_iterator i
;
600 tree expr
= NULL_TREE
;
601 for (i
= tsi_start (body
); !tsi_end_p (i
); tsi_next (&i
))
603 tree s
= constexpr_fn_retval (tsi_stmt (i
));
604 if (s
== error_mark_node
)
605 return error_mark_node
;
606 else if (s
== NULL_TREE
)
607 /* Keep iterating. */;
609 /* Multiple return statements. */
610 return error_mark_node
;
618 return break_out_target_exprs (TREE_OPERAND (body
, 0));
622 tree decl
= DECL_EXPR_DECL (body
);
623 if (TREE_CODE (decl
) == USING_DECL
624 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
625 || DECL_ARTIFICIAL (decl
))
627 return error_mark_node
;
630 case CLEANUP_POINT_EXPR
:
631 return constexpr_fn_retval (TREE_OPERAND (body
, 0));
634 if (!check_constexpr_bind_expr_vars (body
))
635 return error_mark_node
;
636 return constexpr_fn_retval (BIND_EXPR_BODY (body
));
642 return error_mark_node
;
646 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
647 FUN; do the necessary transformations to turn it into a single expression
648 that we can store in the hash table. */
651 massage_constexpr_body (tree fun
, tree body
)
653 if (DECL_CONSTRUCTOR_P (fun
))
654 body
= build_constexpr_constructor_member_initializers
655 (DECL_CONTEXT (fun
), body
);
656 else if (cxx_dialect
< cxx14
)
658 if (TREE_CODE (body
) == EH_SPEC_BLOCK
)
659 body
= EH_SPEC_STMTS (body
);
660 if (TREE_CODE (body
) == MUST_NOT_THROW_EXPR
)
661 body
= TREE_OPERAND (body
, 0);
662 body
= constexpr_fn_retval (body
);
667 /* FUN is a constexpr constructor with massaged body BODY. Return true
668 if some bases/fields are uninitialized, and complain if COMPLAIN. */
671 cx_check_missing_mem_inits (tree fun
, tree body
, bool complain
)
678 if (TREE_CODE (body
) != CONSTRUCTOR
)
681 nelts
= CONSTRUCTOR_NELTS (body
);
682 ctype
= DECL_CONTEXT (fun
);
683 field
= TYPE_FIELDS (ctype
);
685 if (TREE_CODE (ctype
) == UNION_TYPE
)
687 if (nelts
== 0 && next_initializable_field (field
))
690 error ("%<constexpr%> constructor for union %qT must "
691 "initialize exactly one non-static data member", ctype
);
698 for (i
= 0; i
<= nelts
; ++i
)
705 index
= CONSTRUCTOR_ELT (body
, i
)->index
;
706 /* Skip base and vtable inits. */
707 if (TREE_CODE (index
) != FIELD_DECL
708 || DECL_ARTIFICIAL (index
))
711 for (; field
!= index
; field
= DECL_CHAIN (field
))
714 if (TREE_CODE (field
) != FIELD_DECL
715 || (DECL_C_BIT_FIELD (field
) && !DECL_NAME (field
))
716 || DECL_ARTIFICIAL (field
))
718 ftype
= strip_array_types (TREE_TYPE (field
));
719 if (type_has_constexpr_default_constructor (ftype
))
721 /* It's OK to skip a member with a trivial constexpr ctor.
722 A constexpr ctor that isn't trivial should have been
724 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype
)
730 error ("member %qD must be initialized by mem-initializer "
731 "in %<constexpr%> constructor", field
);
732 inform (DECL_SOURCE_LOCATION (field
), "declared here");
735 if (field
== NULL_TREE
)
737 field
= DECL_CHAIN (field
);
743 /* We are processing the definition of the constexpr function FUN.
744 Check that its BODY fulfills the propriate requirements and
745 enter it in the constexpr function definition table.
746 For constructor BODY is actually the TREE_LIST of the
747 member-initializer list. */
750 register_constexpr_fundef (tree fun
, tree body
)
752 constexpr_fundef entry
;
753 constexpr_fundef
**slot
;
755 if (!is_valid_constexpr_fn (fun
, !DECL_GENERATED_P (fun
)))
758 body
= massage_constexpr_body (fun
, body
);
759 if (body
== NULL_TREE
|| body
== error_mark_node
)
761 if (!DECL_CONSTRUCTOR_P (fun
))
762 error ("body of constexpr function %qD not a return-statement", fun
);
766 if (!potential_rvalue_constant_expression (body
))
768 if (!DECL_GENERATED_P (fun
))
769 require_potential_rvalue_constant_expression (body
);
773 if (DECL_CONSTRUCTOR_P (fun
)
774 && cx_check_missing_mem_inits (fun
, body
, !DECL_GENERATED_P (fun
)))
777 /* Create the constexpr function table if necessary. */
778 if (constexpr_fundef_table
== NULL
)
779 constexpr_fundef_table
780 = hash_table
<constexpr_fundef_hasher
>::create_ggc (101);
784 slot
= constexpr_fundef_table
->find_slot (&entry
, INSERT
);
786 gcc_assert (*slot
== NULL
);
787 *slot
= ggc_alloc
<constexpr_fundef
> ();
793 /* FUN is a non-constexpr function called in a context that requires a
794 constant expression. If it comes from a constexpr template, explain why
795 the instantiation isn't constexpr. */
798 explain_invalid_constexpr_fn (tree fun
)
800 static hash_set
<tree
> *diagnosed
;
803 /* Only diagnose defaulted functions or instantiations. */
804 if (!DECL_DEFAULTED_FN (fun
)
805 && !is_instantiation_of_constexpr (fun
))
807 if (diagnosed
== NULL
)
808 diagnosed
= new hash_set
<tree
>;
809 if (diagnosed
->add (fun
))
810 /* Already explained. */
813 save_loc
= input_location
;
814 input_location
= DECL_SOURCE_LOCATION (fun
);
815 inform (0, "%q+D is not usable as a constexpr function because:", fun
);
816 /* First check the declaration. */
817 if (is_valid_constexpr_fn (fun
, true))
819 /* Then if it's OK, the body. */
820 if (!DECL_DECLARED_CONSTEXPR_P (fun
))
821 explain_implicit_non_constexpr (fun
);
824 body
= massage_constexpr_body (fun
, DECL_SAVED_TREE (fun
));
825 require_potential_rvalue_constant_expression (body
);
826 if (DECL_CONSTRUCTOR_P (fun
))
827 cx_check_missing_mem_inits (fun
, body
, true);
830 input_location
= save_loc
;
833 /* Objects of this type represent calls to constexpr functions
834 along with the bindings of parameters to their arguments, for
835 the purpose of compile time evaluation. */
837 struct GTY((for_user
)) constexpr_call
{
838 /* Description of the constexpr function definition. */
839 constexpr_fundef
*fundef
;
840 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
841 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
842 Note: This arrangement is made to accommodate the use of
843 iterative_hash_template_arg (see pt.c). If you change this
844 representation, also change the hash calculation in
845 cxx_eval_call_expression. */
847 /* Result of the call.
848 NULL means the call is being evaluated.
849 error_mark_node means that the evaluation was erroneous;
850 otherwise, the actuall value of the call. */
852 /* The hash of this call; we remember it here to avoid having to
853 recalculate it when expanding the hash table. */
857 struct constexpr_call_hasher
: ggc_hasher
<constexpr_call
*>
859 static hashval_t
hash (constexpr_call
*);
860 static bool equal (constexpr_call
*, constexpr_call
*);
863 /* The constexpr expansion context. CALL is the current function
864 expansion, CTOR is the current aggregate initializer, OBJECT is the
865 object being initialized by CTOR, either a VAR_DECL or a _REF. VALUES
866 is a map of values of variables initialized within the expression. */
868 struct constexpr_ctx
{
869 constexpr_call
*call
;
870 hash_map
<tree
,tree
> *values
;
877 /* A table of all constexpr calls that have been evaluated by the
878 compiler in this translation unit. */
880 static GTY (()) hash_table
<constexpr_call_hasher
> *constexpr_call_table
;
882 static tree
cxx_eval_constant_expression (const constexpr_ctx
*, tree
,
883 bool, bool *, bool *, tree
* = NULL
);
885 /* Compute a hash value for a constexpr call representation. */
888 constexpr_call_hasher::hash (constexpr_call
*info
)
893 /* Return true if the objects pointed to by P and Q represent calls
894 to the same constexpr function with the same arguments.
895 Otherwise, return false. */
898 constexpr_call_hasher::equal (constexpr_call
*lhs
, constexpr_call
*rhs
)
904 if (!constexpr_fundef_hasher::equal (lhs
->fundef
, rhs
->fundef
))
906 lhs_bindings
= lhs
->bindings
;
907 rhs_bindings
= rhs
->bindings
;
908 while (lhs_bindings
!= NULL
&& rhs_bindings
!= NULL
)
910 tree lhs_arg
= TREE_VALUE (lhs_bindings
);
911 tree rhs_arg
= TREE_VALUE (rhs_bindings
);
912 gcc_assert (TREE_TYPE (lhs_arg
) == TREE_TYPE (rhs_arg
));
913 if (!cp_tree_equal (lhs_arg
, rhs_arg
))
915 lhs_bindings
= TREE_CHAIN (lhs_bindings
);
916 rhs_bindings
= TREE_CHAIN (rhs_bindings
);
918 return lhs_bindings
== rhs_bindings
;
921 /* Initialize the constexpr call table, if needed. */
924 maybe_initialize_constexpr_call_table (void)
926 if (constexpr_call_table
== NULL
)
927 constexpr_call_table
= hash_table
<constexpr_call_hasher
>::create_ggc (101);
930 /* We have an expression tree T that represents a call, either CALL_EXPR
931 or AGGR_INIT_EXPR. If the call is lexically to a named function,
932 retrun the _DECL for that function. */
935 get_function_named_in_call (tree t
)
938 switch (TREE_CODE (t
))
941 fun
= CALL_EXPR_FN (t
);
945 fun
= AGGR_INIT_EXPR_FN (t
);
952 if (fun
&& TREE_CODE (fun
) == ADDR_EXPR
953 && TREE_CODE (TREE_OPERAND (fun
, 0)) == FUNCTION_DECL
)
954 fun
= TREE_OPERAND (fun
, 0);
958 /* We have an expression tree T that represents a call, either CALL_EXPR
959 or AGGR_INIT_EXPR. Return the Nth argument. */
962 get_nth_callarg (tree t
, int n
)
964 switch (TREE_CODE (t
))
967 return CALL_EXPR_ARG (t
, n
);
970 return AGGR_INIT_EXPR_ARG (t
, n
);
978 /* Look up the binding of the function parameter T in a constexpr
979 function call context CALL. */
982 lookup_parameter_binding (const constexpr_call
*call
, tree t
)
984 tree b
= purpose_member (t
, call
->bindings
);
985 return TREE_VALUE (b
);
988 /* Attempt to evaluate T which represents a call to a builtin function.
989 We assume here that all builtin functions evaluate to scalar types
990 represented by _CST nodes. */
993 cxx_eval_builtin_function_call (const constexpr_ctx
*ctx
, tree t
,
995 bool *non_constant_p
, bool *overflow_p
)
997 const int nargs
= call_expr_nargs (t
);
998 tree
*args
= (tree
*) alloca (nargs
* sizeof (tree
));
1001 for (i
= 0; i
< nargs
; ++i
)
1003 args
[i
] = cxx_eval_constant_expression (ctx
, CALL_EXPR_ARG (t
, i
),
1005 non_constant_p
, overflow_p
);
1006 if (ctx
->quiet
&& *non_constant_p
)
1009 if (*non_constant_p
)
1011 new_call
= fold_build_call_array_loc (EXPR_LOCATION (t
), TREE_TYPE (t
),
1012 CALL_EXPR_FN (t
), nargs
, args
);
1013 VERIFY_CONSTANT (new_call
);
1017 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1018 the type of the value to match. */
1021 adjust_temp_type (tree type
, tree temp
)
1023 if (TREE_TYPE (temp
) == type
)
1025 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1026 if (TREE_CODE (temp
) == CONSTRUCTOR
)
1027 return build_constructor (type
, CONSTRUCTOR_ELTS (temp
));
1028 gcc_assert (scalarish_type_p (type
));
1029 return cp_fold_convert (type
, temp
);
1032 /* True if we want to use the new handling of constexpr calls based on
1034 #define use_new_call true
1036 /* Subroutine of cxx_eval_call_expression.
1037 We are processing a call expression (either CALL_EXPR or
1038 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1039 all arguments and bind their values to correspondings
1040 parameters, making up the NEW_CALL context. */
1043 cxx_bind_parameters_in_call (const constexpr_ctx
*ctx
, tree t
,
1044 constexpr_call
*new_call
,
1045 bool *non_constant_p
, bool *overflow_p
)
1047 const int nargs
= call_expr_nargs (t
);
1048 tree fun
= new_call
->fundef
->decl
;
1049 tree parms
= DECL_ARGUMENTS (fun
);
1051 tree
*p
= &new_call
->bindings
;
1052 for (i
= 0; i
< nargs
; ++i
)
1055 tree type
= parms
? TREE_TYPE (parms
) : void_type_node
;
1056 x
= get_nth_callarg (t
, i
);
1057 /* For member function, the first argument is a pointer to the implied
1058 object. For a constructor, it might still be a dummy object, in
1059 which case we get the real argument from ctx. */
1060 if (i
== 0 && DECL_CONSTRUCTOR_P (fun
)
1061 && is_dummy_object (x
))
1064 x
= cp_build_addr_expr (x
, tf_warning_or_error
);
1067 if (parms
&& DECL_BY_REFERENCE (parms
) && !use_new_call
)
1069 /* cp_genericize made this a reference for argument passing, but
1070 we don't want to treat it like one for C++11 constexpr
1071 evaluation. C++14 constexpr evaluation uses the genericized
1073 gcc_assert (TREE_CODE (type
) == REFERENCE_TYPE
);
1074 gcc_assert (TREE_CODE (TREE_TYPE (x
)) == REFERENCE_TYPE
);
1075 type
= TREE_TYPE (type
);
1076 x
= convert_from_reference (x
);
1079 arg
= cxx_eval_constant_expression (ctx
, x
, addr
,
1080 non_constant_p
, overflow_p
);
1081 /* Don't VERIFY_CONSTANT here. */
1082 if (*non_constant_p
&& ctx
->quiet
)
1084 /* Just discard ellipsis args after checking their constantitude. */
1087 if (*non_constant_p
)
1088 /* Don't try to adjust the type of non-constant args. */
1091 /* Make sure the binding has the same type as the parm. */
1092 if (TREE_CODE (type
) != REFERENCE_TYPE
)
1093 arg
= adjust_temp_type (type
, arg
);
1094 *p
= build_tree_list (parms
, arg
);
1095 p
= &TREE_CHAIN (*p
);
1097 parms
= TREE_CHAIN (parms
);
1101 /* Variables and functions to manage constexpr call expansion context.
1102 These do not need to be marked for PCH or GC. */
1104 /* FIXME remember and print actual constant arguments. */
1105 static vec
<tree
> call_stack
= vNULL
;
1106 static int call_stack_tick
;
1107 static int last_cx_error_tick
;
1110 push_cx_call_context (tree call
)
1113 if (!EXPR_HAS_LOCATION (call
))
1114 SET_EXPR_LOCATION (call
, input_location
);
1115 call_stack
.safe_push (call
);
1116 if (call_stack
.length () > (unsigned) max_constexpr_depth
)
1122 pop_cx_call_context (void)
1129 cx_error_context (void)
1131 vec
<tree
> r
= vNULL
;
1132 if (call_stack_tick
!= last_cx_error_tick
1133 && !call_stack
.is_empty ())
1135 last_cx_error_tick
= call_stack_tick
;
1139 /* Subroutine of cxx_eval_constant_expression.
1140 Evaluate the call expression tree T in the context of OLD_CALL expression
1144 cxx_eval_call_expression (const constexpr_ctx
*ctx
, tree t
,
1146 bool *non_constant_p
, bool *overflow_p
)
1148 location_t loc
= EXPR_LOC_OR_LOC (t
, input_location
);
1149 tree fun
= get_function_named_in_call (t
);
1151 constexpr_call new_call
= { NULL
, NULL
, NULL
, 0 };
1152 constexpr_call
**slot
;
1153 constexpr_call
*entry
;
1156 if (fun
== NULL_TREE
)
1157 switch (CALL_EXPR_IFN (t
))
1159 case IFN_UBSAN_NULL
:
1160 case IFN_UBSAN_BOUNDS
:
1164 error_at (loc
, "call to internal function");
1165 *non_constant_p
= true;
1169 if (TREE_CODE (fun
) != FUNCTION_DECL
)
1171 /* Might be a constexpr function pointer. */
1172 fun
= cxx_eval_constant_expression (ctx
, fun
,
1173 /*addr*/false, non_constant_p
,
1176 if (TREE_CODE (fun
) == ADDR_EXPR
)
1177 fun
= TREE_OPERAND (fun
, 0);
1179 if (TREE_CODE (fun
) != FUNCTION_DECL
)
1181 if (!ctx
->quiet
&& !*non_constant_p
)
1182 error_at (loc
, "expression %qE does not designate a constexpr "
1184 *non_constant_p
= true;
1187 if (DECL_CLONED_FUNCTION_P (fun
))
1188 fun
= DECL_CLONED_FUNCTION (fun
);
1190 if (is_ubsan_builtin_p (fun
))
1193 if (is_builtin_fn (fun
))
1194 return cxx_eval_builtin_function_call (ctx
, t
,
1195 addr
, non_constant_p
, overflow_p
);
1196 if (!DECL_DECLARED_CONSTEXPR_P (fun
))
1200 error_at (loc
, "call to non-constexpr function %qD", fun
);
1201 explain_invalid_constexpr_fn (fun
);
1203 *non_constant_p
= true;
1207 /* Shortcut trivial constructor/op=. */
1208 if (trivial_fn_p (fun
))
1210 if (call_expr_nargs (t
) == 2)
1212 tree arg
= convert_from_reference (get_nth_callarg (t
, 1));
1213 return cxx_eval_constant_expression (ctx
, arg
,
1214 addr
, non_constant_p
,
1217 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
1218 && AGGR_INIT_ZERO_FIRST (t
))
1219 return build_zero_init (DECL_CONTEXT (fun
), NULL_TREE
, false);
1222 /* If in direct recursive call, optimize definition search. */
1223 if (ctx
&& ctx
->call
&& ctx
->call
->fundef
->decl
== fun
)
1224 new_call
.fundef
= ctx
->call
->fundef
;
1227 new_call
.fundef
= retrieve_constexpr_fundef (fun
);
1228 if (new_call
.fundef
== NULL
|| new_call
.fundef
->body
== NULL
)
1232 if (DECL_INITIAL (fun
))
1234 /* The definition of fun was somehow unsuitable. */
1235 error_at (loc
, "%qD called in a constant expression", fun
);
1236 explain_invalid_constexpr_fn (fun
);
1239 error_at (loc
, "%qD used before its definition", fun
);
1241 *non_constant_p
= true;
1246 constexpr_ctx new_ctx
= *ctx
;
1247 if (DECL_CONSTRUCTOR_P (fun
) && !ctx
->object
1248 && TREE_CODE (t
) == AGGR_INIT_EXPR
)
1250 /* We want to have an initialization target for an AGGR_INIT_EXPR.
1251 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
1252 new_ctx
.object
= AGGR_INIT_EXPR_SLOT (t
);
1253 tree ctor
= new_ctx
.ctor
= build_constructor (DECL_CONTEXT (fun
), NULL
);
1254 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctor
) = true;
1255 ctx
->values
->put (new_ctx
.object
, ctor
);
1259 cxx_bind_parameters_in_call (ctx
, t
, &new_call
,
1260 non_constant_p
, overflow_p
);
1261 if (*non_constant_p
)
1264 depth_ok
= push_cx_call_context (t
);
1267 = iterative_hash_template_arg (new_call
.bindings
,
1268 constexpr_fundef_hasher::hash (new_call
.fundef
));
1270 /* If we have seen this call before, we are done. */
1271 maybe_initialize_constexpr_call_table ();
1272 slot
= constexpr_call_table
->find_slot (&new_call
, INSERT
);
1276 /* We need to keep a pointer to the entry, not just the slot, as the
1277 slot can move in the call to cxx_eval_builtin_function_call. */
1278 *slot
= entry
= ggc_alloc
<constexpr_call
> ();
1281 /* Calls which are in progress have their result set to NULL
1282 so that we can detect circular dependencies. */
1283 else if (entry
->result
== NULL
)
1286 error ("call has circular dependency");
1287 *non_constant_p
= true;
1288 entry
->result
= result
= error_mark_node
;
1294 error ("constexpr evaluation depth exceeds maximum of %d (use "
1295 "-fconstexpr-depth= to increase the maximum)",
1296 max_constexpr_depth
);
1297 *non_constant_p
= true;
1298 entry
->result
= result
= error_mark_node
;
1302 result
= entry
->result
;
1303 if (!result
|| result
== error_mark_node
)
1307 new_ctx
.call
= &new_call
;
1308 result
= (cxx_eval_constant_expression
1309 (&new_ctx
, new_call
.fundef
->body
,
1311 non_constant_p
, overflow_p
));
1315 if (DECL_SAVED_TREE (fun
) == NULL_TREE
1316 && (DECL_CONSTRUCTOR_P (fun
) || DECL_DESTRUCTOR_P (fun
)))
1317 /* The maybe-in-charge 'tor had its DECL_SAVED_TREE
1318 cleared, try the first clone. */
1319 fun
= DECL_CHAIN (fun
);
1320 gcc_assert (DECL_SAVED_TREE (fun
));
1323 /* Unshare the whole function body. */
1324 tree body
= copy_fn (fun
, parms
, res
);
1326 /* Associate the bindings with the remapped parms. */
1327 tree bound
= new_call
.bindings
;
1328 tree remapped
= parms
;
1331 tree oparm
= TREE_PURPOSE (bound
);
1332 tree arg
= TREE_VALUE (bound
);
1333 gcc_assert (DECL_NAME (remapped
) == DECL_NAME (oparm
));
1334 ctx
->values
->put (remapped
, arg
);
1335 bound
= TREE_CHAIN (bound
);
1336 remapped
= DECL_CHAIN (remapped
);
1338 /* Add the RESULT_DECL to the values map, too. */
1339 tree slot
= NULL_TREE
;
1340 if (DECL_BY_REFERENCE (res
))
1342 slot
= AGGR_INIT_EXPR_SLOT (t
);
1343 tree addr
= build_address (slot
);
1344 addr
= build_nop (TREE_TYPE (res
), addr
);
1345 ctx
->values
->put (res
, addr
);
1346 ctx
->values
->put (slot
, NULL_TREE
);
1349 ctx
->values
->put (res
, NULL_TREE
);
1351 tree jump_target
= NULL_TREE
;
1352 cxx_eval_constant_expression (ctx
, body
,
1353 addr
, non_constant_p
, overflow_p
,
1356 if (DECL_CONSTRUCTOR_P (fun
))
1357 /* This can be null for a subobject constructor call, in
1358 which case what we care about is the initialization
1359 side-effects rather than the value. We could get at the
1360 value by evaluating *this, but we don't bother; there's
1361 no need to put such a call in the hash table. */
1362 result
= addr
? ctx
->object
: ctx
->ctor
;
1365 result
= *ctx
->values
->get (slot
? slot
: res
);
1366 if (result
== NULL_TREE
&& !*non_constant_p
)
1369 error ("constexpr call flows off the end "
1371 *non_constant_p
= true;
1375 /* Remove the parms/result from the values map. Is it worth
1376 bothering to do this when the map itself is only live for
1377 one constexpr evaluation? If so, maybe also clear out
1378 other vars from call, maybe in BIND_EXPR handling? */
1379 ctx
->values
->remove (res
);
1381 ctx
->values
->remove (slot
);
1382 for (tree parm
= parms
; parm
; parm
= TREE_CHAIN (parm
))
1383 ctx
->values
->remove (parm
);
1387 if (result
== error_mark_node
)
1388 *non_constant_p
= true;
1389 if (*non_constant_p
)
1390 entry
->result
= result
= error_mark_node
;
1393 /* If this was a call to initialize an object, set the type of
1394 the CONSTRUCTOR to the type of that object. */
1395 if (DECL_CONSTRUCTOR_P (fun
) && !use_new_call
)
1397 tree ob_arg
= get_nth_callarg (t
, 0);
1398 STRIP_NOPS (ob_arg
);
1399 gcc_assert (TYPE_PTR_P (TREE_TYPE (ob_arg
))
1400 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (ob_arg
))));
1401 result
= adjust_temp_type (TREE_TYPE (TREE_TYPE (ob_arg
)),
1404 entry
->result
= result
;
1410 pop_cx_call_context ();
1411 return unshare_expr (result
);
1414 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
1417 reduced_constant_expression_p (tree t
)
1419 switch (TREE_CODE (t
))
1422 /* Even if we can't lower this yet, it's constant. */
1426 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
1427 tree elt
; unsigned HOST_WIDE_INT idx
;
1428 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), idx
, elt
)
1429 if (!reduced_constant_expression_p (elt
))
1434 /* FIXME are we calling this too much? */
1435 return initializer_constant_valid_p (t
, TREE_TYPE (t
)) != NULL_TREE
;
1439 /* Some expressions may have constant operands but are not constant
1440 themselves, such as 1/0. Call this function (or rather, the macro
1441 following it) to check for that condition.
1443 We only call this in places that require an arithmetic constant, not in
1444 places where we might have a non-constant expression that can be a
1445 component of a constant expression, such as the address of a constexpr
1446 variable that might be dereferenced later. */
1449 verify_constant (tree t
, bool allow_non_constant
, bool *non_constant_p
,
1452 if (!*non_constant_p
&& !reduced_constant_expression_p (t
))
1454 if (!allow_non_constant
)
1455 error ("%q+E is not a constant expression", t
);
1456 *non_constant_p
= true;
1458 if (TREE_OVERFLOW_P (t
))
1460 if (!allow_non_constant
)
1462 permerror (input_location
, "overflow in constant expression");
1463 /* If we're being permissive (and are in an enforcing
1464 context), ignore the overflow. */
1465 if (flag_permissive
)
1466 return *non_constant_p
;
1470 return *non_constant_p
;
1473 /* Check whether the shift operation with code CODE and type TYPE on LHS
1474 and RHS is undefined. If it is, give an error with an explanation,
1475 and return true; return false otherwise. */
1478 cxx_eval_check_shift_p (location_t loc
, const constexpr_ctx
*ctx
,
1479 enum tree_code code
, tree type
, tree lhs
, tree rhs
)
1481 if ((code
!= LSHIFT_EXPR
&& code
!= RSHIFT_EXPR
)
1482 || TREE_CODE (lhs
) != INTEGER_CST
1483 || TREE_CODE (rhs
) != INTEGER_CST
)
1486 tree lhstype
= TREE_TYPE (lhs
);
1487 unsigned HOST_WIDE_INT uprec
= TYPE_PRECISION (TREE_TYPE (lhs
));
1489 /* [expr.shift] The behavior is undefined if the right operand
1490 is negative, or greater than or equal to the length in bits
1491 of the promoted left operand. */
1492 if (tree_int_cst_sgn (rhs
) == -1)
1495 error_at (loc
, "right operand of shift expression %q+E is negative",
1496 build2_loc (loc
, code
, type
, lhs
, rhs
));
1499 if (compare_tree_int (rhs
, uprec
) >= 0)
1502 error_at (loc
, "right operand of shift expression %q+E is >= than "
1503 "the precision of the left operand",
1504 build2_loc (loc
, code
, type
, lhs
, rhs
));
1508 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
1509 if E1 has a signed type and non-negative value, and E1x2^E2 is
1510 representable in the corresponding unsigned type of the result type,
1511 then that value, converted to the result type, is the resulting value;
1512 otherwise, the behavior is undefined. */
1513 if (code
== LSHIFT_EXPR
&& !TYPE_UNSIGNED (lhstype
)
1514 && (cxx_dialect
>= cxx11
))
1516 if (tree_int_cst_sgn (lhs
) == -1)
1519 error_at (loc
, "left operand of shift expression %q+E is negative",
1520 build2_loc (loc
, code
, type
, lhs
, rhs
));
1523 /* For signed x << y the following:
1524 (unsigned) x >> ((prec (lhs) - 1) - y)
1525 if > 1, is undefined. The right-hand side of this formula
1526 is the highest bit of the LHS that can be set (starting from 0),
1527 so that the shift doesn't overflow. We then right-shift the LHS
1528 to see whether any other bit is set making the original shift
1529 undefined -- the result is not representable in the corresponding
1531 tree t
= build_int_cst (unsigned_type_node
, uprec
- 1);
1532 t
= fold_build2 (MINUS_EXPR
, unsigned_type_node
, t
, rhs
);
1533 tree ulhs
= fold_convert (unsigned_type_for (lhstype
), lhs
);
1534 t
= fold_build2 (RSHIFT_EXPR
, TREE_TYPE (ulhs
), ulhs
, t
);
1535 if (tree_int_cst_lt (integer_one_node
, t
))
1538 error_at (loc
, "shift expression %q+E overflows",
1539 build2_loc (loc
, code
, type
, lhs
, rhs
));
1546 /* Subroutine of cxx_eval_constant_expression.
1547 Attempt to reduce the unary expression tree T to a compile time value.
1548 If successful, return the value. Otherwise issue a diagnostic
1549 and return error_mark_node. */
1552 cxx_eval_unary_expression (const constexpr_ctx
*ctx
, tree t
,
1554 bool *non_constant_p
, bool *overflow_p
)
1557 tree orig_arg
= TREE_OPERAND (t
, 0);
1558 tree arg
= cxx_eval_constant_expression (ctx
, orig_arg
,
1559 addr
, non_constant_p
, overflow_p
);
1560 VERIFY_CONSTANT (arg
);
1561 location_t loc
= EXPR_LOCATION (t
);
1562 enum tree_code code
= TREE_CODE (t
);
1563 tree type
= TREE_TYPE (t
);
1564 r
= fold_unary_loc (loc
, code
, type
, arg
);
1567 if (arg
== orig_arg
)
1570 r
= build1_loc (loc
, code
, type
, arg
);
1572 VERIFY_CONSTANT (r
);
1576 /* Subroutine of cxx_eval_constant_expression.
1577 Like cxx_eval_unary_expression, except for binary expressions. */
1580 cxx_eval_binary_expression (const constexpr_ctx
*ctx
, tree t
,
1582 bool *non_constant_p
, bool *overflow_p
)
1585 tree orig_lhs
= TREE_OPERAND (t
, 0);
1586 tree orig_rhs
= TREE_OPERAND (t
, 1);
1588 lhs
= cxx_eval_constant_expression (ctx
, orig_lhs
,
1590 non_constant_p
, overflow_p
);
1591 VERIFY_CONSTANT (lhs
);
1592 rhs
= cxx_eval_constant_expression (ctx
, orig_rhs
,
1594 non_constant_p
, overflow_p
);
1595 VERIFY_CONSTANT (rhs
);
1597 location_t loc
= EXPR_LOCATION (t
);
1598 enum tree_code code
= TREE_CODE (t
);
1599 tree type
= TREE_TYPE (t
);
1600 r
= fold_binary_loc (loc
, code
, type
, lhs
, rhs
);
1603 if (lhs
== orig_lhs
&& rhs
== orig_rhs
)
1606 r
= build2_loc (loc
, code
, type
, lhs
, rhs
);
1608 else if (cxx_eval_check_shift_p (loc
, ctx
, code
, type
, lhs
, rhs
))
1609 *non_constant_p
= true;
1610 VERIFY_CONSTANT (r
);
1614 /* Subroutine of cxx_eval_constant_expression.
1615 Attempt to evaluate condition expressions. Dead branches are not
1619 cxx_eval_conditional_expression (const constexpr_ctx
*ctx
, tree t
,
1621 bool *non_constant_p
, bool *overflow_p
,
1624 tree val
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
1626 non_constant_p
, overflow_p
);
1627 VERIFY_CONSTANT (val
);
1628 /* Don't VERIFY_CONSTANT the other operands. */
1629 if (integer_zerop (val
))
1630 return cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 2),
1632 non_constant_p
, overflow_p
,
1634 return cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
1636 non_constant_p
, overflow_p
,
1640 /* Subroutine of cxx_eval_constant_expression.
1641 Attempt to reduce a reference to an array slot. */
1644 cxx_eval_array_reference (const constexpr_ctx
*ctx
, tree t
,
1646 bool *non_constant_p
, bool *overflow_p
)
1648 tree oldary
= TREE_OPERAND (t
, 0);
1649 tree ary
= cxx_eval_constant_expression (ctx
, oldary
,
1651 non_constant_p
, overflow_p
);
1655 unsigned len
, elem_nchars
= 1;
1656 if (*non_constant_p
)
1658 oldidx
= TREE_OPERAND (t
, 1);
1659 index
= cxx_eval_constant_expression (ctx
, oldidx
,
1661 non_constant_p
, overflow_p
);
1662 VERIFY_CONSTANT (index
);
1663 if (addr
&& ary
== oldary
&& index
== oldidx
)
1666 return build4 (ARRAY_REF
, TREE_TYPE (t
), ary
, index
, NULL
, NULL
);
1667 elem_type
= TREE_TYPE (TREE_TYPE (ary
));
1668 if (TREE_CODE (ary
) == CONSTRUCTOR
)
1669 len
= CONSTRUCTOR_NELTS (ary
);
1670 else if (TREE_CODE (ary
) == STRING_CST
)
1672 elem_nchars
= (TYPE_PRECISION (elem_type
)
1673 / TYPE_PRECISION (char_type_node
));
1674 len
= (unsigned) TREE_STRING_LENGTH (ary
) / elem_nchars
;
1678 /* We can't do anything with other tree codes, so use
1679 VERIFY_CONSTANT to complain and fail. */
1680 VERIFY_CONSTANT (ary
);
1683 if (compare_tree_int (index
, len
) >= 0)
1685 if (tree_int_cst_lt (index
, array_type_nelts_top (TREE_TYPE (ary
))))
1687 /* If it's within the array bounds but doesn't have an explicit
1688 initializer, it's value-initialized. */
1689 tree val
= build_value_init (elem_type
, tf_warning_or_error
);
1690 return cxx_eval_constant_expression (ctx
, val
,
1692 non_constant_p
, overflow_p
);
1696 error ("array subscript out of bound");
1697 *non_constant_p
= true;
1700 else if (tree_int_cst_lt (index
, integer_zero_node
))
1703 error ("negative array subscript");
1704 *non_constant_p
= true;
1707 i
= tree_to_shwi (index
);
1708 if (TREE_CODE (ary
) == CONSTRUCTOR
)
1709 return (*CONSTRUCTOR_ELTS (ary
))[i
].value
;
1710 else if (elem_nchars
== 1)
1711 return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary
))),
1712 TREE_STRING_POINTER (ary
)[i
]);
1715 tree type
= cv_unqualified (TREE_TYPE (TREE_TYPE (ary
)));
1716 return native_interpret_expr (type
, (const unsigned char *)
1717 TREE_STRING_POINTER (ary
)
1718 + i
* elem_nchars
, elem_nchars
);
1720 /* Don't VERIFY_CONSTANT here. */
1723 /* Subroutine of cxx_eval_constant_expression.
1724 Attempt to reduce a field access of a value of class type. */
1727 cxx_eval_component_reference (const constexpr_ctx
*ctx
, tree t
,
1729 bool *non_constant_p
, bool *overflow_p
)
1731 unsigned HOST_WIDE_INT i
;
1734 tree part
= TREE_OPERAND (t
, 1);
1735 tree orig_whole
= TREE_OPERAND (t
, 0);
1736 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
1738 non_constant_p
, overflow_p
);
1739 if (whole
== orig_whole
)
1742 return fold_build3 (COMPONENT_REF
, TREE_TYPE (t
),
1743 whole
, part
, NULL_TREE
);
1744 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
1746 if (!*non_constant_p
&& TREE_CODE (whole
) != CONSTRUCTOR
)
1749 error ("%qE is not a constant expression", orig_whole
);
1750 *non_constant_p
= true;
1752 if (DECL_MUTABLE_P (part
))
1755 error ("mutable %qD is not usable in a constant expression", part
);
1756 *non_constant_p
= true;
1758 if (*non_constant_p
)
1760 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
1767 /* We're in the middle of initializing it. */
1771 if (TREE_CODE (TREE_TYPE (whole
)) == UNION_TYPE
1772 && CONSTRUCTOR_NELTS (whole
) > 0)
1774 /* DR 1188 says we don't have to deal with this. */
1776 error ("accessing %qD member instead of initialized %qD member in "
1777 "constant expression", part
, CONSTRUCTOR_ELT (whole
, 0)->index
);
1778 *non_constant_p
= true;
1782 if (CONSTRUCTOR_NO_IMPLICIT_ZERO (whole
))
1784 /* 'whole' is part of the aggregate initializer we're currently
1785 building; if there's no initializer for this member yet, that's an
1788 error ("accessing uninitialized member %qD", part
);
1789 *non_constant_p
= true;
1793 /* If there's no explicit init for this field, it's value-initialized. */
1794 value
= build_value_init (TREE_TYPE (t
), tf_warning_or_error
);
1795 return cxx_eval_constant_expression (ctx
, value
,
1797 non_constant_p
, overflow_p
);
1800 /* Subroutine of cxx_eval_constant_expression.
1801 Attempt to reduce a field access of a value of class type that is
1802 expressed as a BIT_FIELD_REF. */
1805 cxx_eval_bit_field_ref (const constexpr_ctx
*ctx
, tree t
,
1807 bool *non_constant_p
, bool *overflow_p
)
1809 tree orig_whole
= TREE_OPERAND (t
, 0);
1810 tree retval
, fldval
, utype
, mask
;
1811 bool fld_seen
= false;
1812 HOST_WIDE_INT istart
, isize
;
1813 tree whole
= cxx_eval_constant_expression (ctx
, orig_whole
,
1815 non_constant_p
, overflow_p
);
1816 tree start
, field
, value
;
1817 unsigned HOST_WIDE_INT i
;
1819 if (whole
== orig_whole
)
1821 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
1823 if (!*non_constant_p
1824 && TREE_CODE (whole
) != VECTOR_CST
1825 && TREE_CODE (whole
) != CONSTRUCTOR
)
1828 error ("%qE is not a constant expression", orig_whole
);
1829 *non_constant_p
= true;
1831 if (*non_constant_p
)
1834 if (TREE_CODE (whole
) == VECTOR_CST
)
1835 return fold_ternary (BIT_FIELD_REF
, TREE_TYPE (t
), whole
,
1836 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 2));
1838 start
= TREE_OPERAND (t
, 2);
1839 istart
= tree_to_shwi (start
);
1840 isize
= tree_to_shwi (TREE_OPERAND (t
, 1));
1841 utype
= TREE_TYPE (t
);
1842 if (!TYPE_UNSIGNED (utype
))
1843 utype
= build_nonstandard_integer_type (TYPE_PRECISION (utype
), 1);
1844 retval
= build_int_cst (utype
, 0);
1845 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole
), i
, field
, value
)
1847 tree bitpos
= bit_position (field
);
1848 if (bitpos
== start
&& DECL_SIZE (field
) == TREE_OPERAND (t
, 1))
1850 if (TREE_CODE (TREE_TYPE (field
)) == INTEGER_TYPE
1851 && TREE_CODE (value
) == INTEGER_CST
1852 && tree_fits_shwi_p (bitpos
)
1853 && tree_fits_shwi_p (DECL_SIZE (field
)))
1855 HOST_WIDE_INT bit
= tree_to_shwi (bitpos
);
1856 HOST_WIDE_INT sz
= tree_to_shwi (DECL_SIZE (field
));
1857 HOST_WIDE_INT shift
;
1858 if (bit
>= istart
&& bit
+ sz
<= istart
+ isize
)
1860 fldval
= fold_convert (utype
, value
);
1861 mask
= build_int_cst_type (utype
, -1);
1862 mask
= fold_build2 (LSHIFT_EXPR
, utype
, mask
,
1863 size_int (TYPE_PRECISION (utype
) - sz
));
1864 mask
= fold_build2 (RSHIFT_EXPR
, utype
, mask
,
1865 size_int (TYPE_PRECISION (utype
) - sz
));
1866 fldval
= fold_build2 (BIT_AND_EXPR
, utype
, fldval
, mask
);
1867 shift
= bit
- istart
;
1868 if (BYTES_BIG_ENDIAN
)
1869 shift
= TYPE_PRECISION (utype
) - shift
- sz
;
1870 fldval
= fold_build2 (LSHIFT_EXPR
, utype
, fldval
,
1872 retval
= fold_build2 (BIT_IOR_EXPR
, utype
, retval
, fldval
);
1878 return fold_convert (TREE_TYPE (t
), retval
);
1880 return error_mark_node
;
1883 /* Subroutine of cxx_eval_constant_expression.
1884 Evaluate a short-circuited logical expression T in the context
1885 of a given constexpr CALL. BAILOUT_VALUE is the value for
1886 early return. CONTINUE_VALUE is used here purely for
1887 sanity check purposes. */
1890 cxx_eval_logical_expression (const constexpr_ctx
*ctx
, tree t
,
1891 tree bailout_value
, tree continue_value
,
1893 bool *non_constant_p
, bool *overflow_p
)
1896 tree lhs
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
1898 non_constant_p
, overflow_p
);
1899 VERIFY_CONSTANT (lhs
);
1900 if (tree_int_cst_equal (lhs
, bailout_value
))
1902 gcc_assert (tree_int_cst_equal (lhs
, continue_value
));
1903 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
1904 addr
, non_constant_p
,
1906 VERIFY_CONSTANT (r
);
1910 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
1911 CONSTRUCTOR elements to initialize (part of) an object containing that
1912 field. Return a pointer to the constructor_elt corresponding to the
1913 initialization of the field. */
1915 static constructor_elt
*
1916 base_field_constructor_elt (vec
<constructor_elt
, va_gc
> *v
, tree ref
)
1918 tree aggr
= TREE_OPERAND (ref
, 0);
1919 tree field
= TREE_OPERAND (ref
, 1);
1921 constructor_elt
*ce
;
1923 gcc_assert (TREE_CODE (ref
) == COMPONENT_REF
);
1925 if (TREE_CODE (aggr
) == COMPONENT_REF
)
1927 constructor_elt
*base_ce
1928 = base_field_constructor_elt (v
, aggr
);
1929 v
= CONSTRUCTOR_ELTS (base_ce
->value
);
1932 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
1933 if (ce
->index
== field
)
1940 /* Some of the expressions fed to the constexpr mechanism are calls to
1941 constructors, which have type void. In that case, return the type being
1942 initialized by the constructor. */
1945 initialized_type (tree t
)
1949 tree type
= cv_unqualified (TREE_TYPE (t
));
1950 if (TREE_CODE (t
) == CALL_EXPR
|| TREE_CODE (t
) == AGGR_INIT_EXPR
)
1952 /* A constructor call has void type, so we need to look deeper. */
1953 tree fn
= get_function_named_in_call (t
);
1954 if (fn
&& TREE_CODE (fn
) == FUNCTION_DECL
1955 && DECL_CXX_CONSTRUCTOR_P (fn
))
1956 type
= DECL_CONTEXT (fn
);
1961 /* We're about to initialize element INDEX of an array or class from VALUE.
1962 Set up NEW_CTX appropriately by adjusting .object to refer to the
1963 subobject and creating a new CONSTRUCTOR if the element is itself
1964 a class or array. */
1967 init_subob_ctx (const constexpr_ctx
*ctx
, constexpr_ctx
&new_ctx
,
1968 tree index
, tree
&value
)
1972 if (index
&& TREE_CODE (index
) != INTEGER_CST
1973 && TREE_CODE (index
) != FIELD_DECL
)
1974 /* This won't have an element in the new CONSTRUCTOR. */
1977 tree type
= initialized_type (value
);
1978 if (!AGGREGATE_TYPE_P (type
) && !VECTOR_TYPE_P (type
))
1979 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
1982 /* The sub-aggregate initializer might contain a placeholder;
1983 update object to refer to the subobject and ctor to refer to
1984 the (newly created) sub-initializer. */
1986 new_ctx
.object
= build_ctor_subob_ref (index
, type
, ctx
->object
);
1987 tree elt
= build_constructor (type
, NULL
);
1988 CONSTRUCTOR_NO_IMPLICIT_ZERO (elt
) = true;
1991 if (TREE_CODE (value
) == TARGET_EXPR
)
1992 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
1993 value
= TARGET_EXPR_INITIAL (value
);
1996 /* We're about to process an initializer for a class or array TYPE. Make
1997 sure that CTX is set up appropriately. */
2000 verify_ctor_sanity (const constexpr_ctx
*ctx
, tree type
)
2002 /* We don't bother building a ctor for an empty base subobject. */
2003 if (is_empty_class (type
))
2006 /* We're in the middle of an initializer that might involve placeholders;
2007 our caller should have created a CONSTRUCTOR for us to put the
2008 initializer into. We will either return that constructor or T. */
2009 gcc_assert (ctx
->ctor
);
2010 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2011 (type
, TREE_TYPE (ctx
->ctor
)));
2012 gcc_assert (CONSTRUCTOR_NELTS (ctx
->ctor
) == 0);
2014 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2015 (type
, TREE_TYPE (ctx
->object
)));
2016 gcc_assert (!ctx
->object
|| !DECL_P (ctx
->object
)
2017 || *(ctx
->values
->get (ctx
->object
)) == ctx
->ctor
);
2020 /* Subroutine of cxx_eval_constant_expression.
2021 The expression tree T denotes a C-style array or a C-style
2022 aggregate. Reduce it to a constant expression. */
2025 cxx_eval_bare_aggregate (const constexpr_ctx
*ctx
, tree t
,
2027 bool *non_constant_p
, bool *overflow_p
)
2029 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
2030 bool changed
= false;
2031 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t
));
2033 verify_ctor_sanity (ctx
, TREE_TYPE (t
));
2034 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
2035 vec_alloc (*p
, vec_safe_length (v
));
2037 unsigned i
; tree index
, value
;
2038 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, index
, value
)
2040 constexpr_ctx new_ctx
;
2041 init_subob_ctx (ctx
, new_ctx
, index
, value
);
2042 if (new_ctx
.ctor
!= ctx
->ctor
)
2043 /* If we built a new CONSTRUCTOR, attach it now so that other
2044 initializers can refer to it. */
2045 CONSTRUCTOR_APPEND_ELT (*p
, index
, new_ctx
.ctor
);
2046 tree elt
= cxx_eval_constant_expression (&new_ctx
, value
,
2048 non_constant_p
, overflow_p
);
2049 /* Don't VERIFY_CONSTANT here. */
2050 if (ctx
->quiet
&& *non_constant_p
)
2054 if (index
&& TREE_CODE (index
) == COMPONENT_REF
)
2056 /* This is an initialization of a vfield inside a base
2057 subaggregate that we already initialized; push this
2058 initialization into the previous initialization. */
2059 constructor_elt
*inner
= base_field_constructor_elt (*p
, index
);
2064 && (TREE_CODE (index
) == NOP_EXPR
2065 || TREE_CODE (index
) == POINTER_PLUS_EXPR
))
2067 /* This is an initializer for an empty base; now that we've
2068 checked that it's constant, we can ignore it. */
2069 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index
))));
2072 else if (new_ctx
.ctor
!= ctx
->ctor
)
2074 /* We appended this element above; update the value. */
2075 gcc_assert ((*p
)->last().index
== index
);
2076 (*p
)->last().value
= elt
;
2079 CONSTRUCTOR_APPEND_ELT (*p
, index
, elt
);
2081 if (*non_constant_p
|| !changed
)
2084 /* We're done building this CONSTRUCTOR, so now we can interpret an
2085 element without an explicit initializer as value-initialized. */
2086 CONSTRUCTOR_NO_IMPLICIT_ZERO (t
) = false;
2087 if (TREE_CODE (TREE_TYPE (t
)) == VECTOR_TYPE
)
2092 /* Subroutine of cxx_eval_constant_expression.
2093 The expression tree T is a VEC_INIT_EXPR which denotes the desired
2094 initialization of a non-static data member of array type. Reduce it to a
2097 Note that apart from value-initialization (when VALUE_INIT is true),
2098 this is only intended to support value-initialization and the
2099 initializations done by defaulted constructors for classes with
2100 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
2101 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
2102 for the copy/move constructor. */
2105 cxx_eval_vec_init_1 (const constexpr_ctx
*ctx
, tree atype
, tree init
,
2106 bool value_init
, bool addr
,
2107 bool *non_constant_p
, bool *overflow_p
)
2109 tree elttype
= TREE_TYPE (atype
);
2110 unsigned HOST_WIDE_INT max
= tree_to_uhwi (array_type_nelts_top (atype
));
2111 verify_ctor_sanity (ctx
, atype
);
2112 vec
<constructor_elt
, va_gc
> **p
= &CONSTRUCTOR_ELTS (ctx
->ctor
);
2113 vec_alloc (*p
, max
+ 1);
2114 bool pre_init
= false;
2115 unsigned HOST_WIDE_INT i
;
2117 /* For the default constructor, build up a call to the default
2118 constructor of the element type. We only need to handle class types
2119 here, as for a constructor to be constexpr, all members must be
2120 initialized, which for a defaulted default constructor means they must
2121 be of a class type with a constexpr default constructor. */
2122 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
2123 /* We only do this at the lowest level. */;
2124 else if (value_init
)
2126 init
= build_value_init (elttype
, tf_warning_or_error
);
2131 vec
<tree
, va_gc
> *argvec
= make_tree_vector ();
2132 init
= build_special_member_call (NULL_TREE
, complete_ctor_identifier
,
2133 &argvec
, elttype
, LOOKUP_NORMAL
,
2134 tf_warning_or_error
);
2135 release_tree_vector (argvec
);
2136 init
= build_aggr_init_expr (TREE_TYPE (init
), init
);
2140 for (i
= 0; i
< max
; ++i
)
2142 tree idx
= build_int_cst (size_type_node
, i
);
2144 constexpr_ctx new_ctx
;
2145 init_subob_ctx (ctx
, new_ctx
, idx
, pre_init
? init
: elttype
);
2146 if (new_ctx
.ctor
!= ctx
->ctor
)
2147 CONSTRUCTOR_APPEND_ELT (*p
, idx
, new_ctx
.ctor
);
2148 if (TREE_CODE (elttype
) == ARRAY_TYPE
)
2150 /* A multidimensional array; recurse. */
2151 if (value_init
|| init
== NULL_TREE
)
2152 eltinit
= NULL_TREE
;
2154 eltinit
= cp_build_array_ref (input_location
, init
, idx
,
2155 tf_warning_or_error
);
2156 eltinit
= cxx_eval_vec_init_1 (&new_ctx
, elttype
, eltinit
, value_init
,
2158 non_constant_p
, overflow_p
);
2162 /* Initializing an element using value or default initialization
2163 we just pre-built above. */
2164 eltinit
= (cxx_eval_constant_expression
2166 addr
, non_constant_p
, overflow_p
));
2170 /* Copying an element. */
2171 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2172 (atype
, TREE_TYPE (init
)));
2173 eltinit
= cp_build_array_ref (input_location
, init
, idx
,
2174 tf_warning_or_error
);
2175 if (!real_lvalue_p (init
))
2176 eltinit
= move (eltinit
);
2177 eltinit
= force_rvalue (eltinit
, tf_warning_or_error
);
2178 eltinit
= (cxx_eval_constant_expression
2179 (&new_ctx
, eltinit
, addr
,
2180 non_constant_p
, overflow_p
));
2182 if (*non_constant_p
&& !ctx
->quiet
)
2184 if (new_ctx
.ctor
!= ctx
->ctor
)
2186 /* We appended this element above; update the value. */
2187 gcc_assert ((*p
)->last().index
== idx
);
2188 (*p
)->last().value
= eltinit
;
2191 CONSTRUCTOR_APPEND_ELT (*p
, idx
, eltinit
);
2194 if (!*non_constant_p
)
2197 CONSTRUCTOR_NO_IMPLICIT_ZERO (init
) = false;
2203 cxx_eval_vec_init (const constexpr_ctx
*ctx
, tree t
,
2205 bool *non_constant_p
, bool *overflow_p
)
2207 tree atype
= TREE_TYPE (t
);
2208 tree init
= VEC_INIT_EXPR_INIT (t
);
2209 tree r
= cxx_eval_vec_init_1 (ctx
, atype
, init
,
2210 VEC_INIT_EXPR_VALUE_INIT (t
),
2211 addr
, non_constant_p
, overflow_p
);
2212 if (*non_constant_p
)
2218 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
2219 match. We want to be less strict for simple *& folding; if we have a
2220 non-const temporary that we access through a const pointer, that should
2221 work. We handle this here rather than change fold_indirect_ref_1
2222 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
2223 don't really make sense outside of constant expression evaluation. Also
2224 we want to allow folding to COMPONENT_REF, which could cause trouble
2225 with TBAA in fold_indirect_ref_1.
2227 Try to keep this function synced with fold_indirect_ref_1. */
2230 cxx_fold_indirect_ref (location_t loc
, tree type
, tree op0
, bool *empty_base
)
2236 subtype
= TREE_TYPE (sub
);
2237 if (!POINTER_TYPE_P (subtype
))
2240 if (TREE_CODE (sub
) == ADDR_EXPR
)
2242 tree op
= TREE_OPERAND (sub
, 0);
2243 tree optype
= TREE_TYPE (op
);
2245 /* *&CONST_DECL -> to the value of the const decl. */
2246 if (TREE_CODE (op
) == CONST_DECL
)
2247 return DECL_INITIAL (op
);
2248 /* *&p => p; make sure to handle *&"str"[cst] here. */
2249 if (same_type_ignoring_top_level_qualifiers_p (optype
, type
))
2251 tree fop
= fold_read_from_constant_string (op
);
2257 /* *(foo *)&fooarray => fooarray[0] */
2258 else if (TREE_CODE (optype
) == ARRAY_TYPE
2259 && (same_type_ignoring_top_level_qualifiers_p
2260 (type
, TREE_TYPE (optype
))))
2262 tree type_domain
= TYPE_DOMAIN (optype
);
2263 tree min_val
= size_zero_node
;
2264 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
2265 min_val
= TYPE_MIN_VALUE (type_domain
);
2266 return build4_loc (loc
, ARRAY_REF
, type
, op
, min_val
,
2267 NULL_TREE
, NULL_TREE
);
2269 /* *(foo *)&complexfoo => __real__ complexfoo */
2270 else if (TREE_CODE (optype
) == COMPLEX_TYPE
2271 && (same_type_ignoring_top_level_qualifiers_p
2272 (type
, TREE_TYPE (optype
))))
2273 return fold_build1_loc (loc
, REALPART_EXPR
, type
, op
);
2274 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
2275 else if (TREE_CODE (optype
) == VECTOR_TYPE
2276 && (same_type_ignoring_top_level_qualifiers_p
2277 (type
, TREE_TYPE (optype
))))
2279 tree part_width
= TYPE_SIZE (type
);
2280 tree index
= bitsize_int (0);
2281 return fold_build3_loc (loc
, BIT_FIELD_REF
, type
, op
, part_width
, index
);
2283 /* Also handle conversion to an empty base class, which
2284 is represented with a NOP_EXPR. */
2285 else if (is_empty_class (type
)
2286 && CLASS_TYPE_P (optype
)
2287 && DERIVED_FROM_P (type
, optype
))
2292 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
2293 else if (RECORD_OR_UNION_TYPE_P (optype
))
2295 tree field
= TYPE_FIELDS (optype
);
2296 for (; field
; field
= DECL_CHAIN (field
))
2297 if (TREE_CODE (field
) == FIELD_DECL
2298 && integer_zerop (byte_position (field
))
2299 && (same_type_ignoring_top_level_qualifiers_p
2300 (TREE_TYPE (field
), type
)))
2302 return fold_build3 (COMPONENT_REF
, type
, op
, field
, NULL_TREE
);
2307 else if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
2308 && TREE_CODE (TREE_OPERAND (sub
, 1)) == INTEGER_CST
)
2310 tree op00
= TREE_OPERAND (sub
, 0);
2311 tree op01
= TREE_OPERAND (sub
, 1);
2314 if (TREE_CODE (op00
) == ADDR_EXPR
)
2317 op00
= TREE_OPERAND (op00
, 0);
2318 op00type
= TREE_TYPE (op00
);
2320 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
2321 if (TREE_CODE (op00type
) == VECTOR_TYPE
2322 && (same_type_ignoring_top_level_qualifiers_p
2323 (type
, TREE_TYPE (op00type
))))
2325 HOST_WIDE_INT offset
= tree_to_shwi (op01
);
2326 tree part_width
= TYPE_SIZE (type
);
2327 unsigned HOST_WIDE_INT part_widthi
= tree_to_shwi (part_width
)/BITS_PER_UNIT
;
2328 unsigned HOST_WIDE_INT indexi
= offset
* BITS_PER_UNIT
;
2329 tree index
= bitsize_int (indexi
);
2331 if (offset
/ part_widthi
< TYPE_VECTOR_SUBPARTS (op00type
))
2332 return fold_build3_loc (loc
,
2333 BIT_FIELD_REF
, type
, op00
,
2337 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
2338 else if (TREE_CODE (op00type
) == COMPLEX_TYPE
2339 && (same_type_ignoring_top_level_qualifiers_p
2340 (type
, TREE_TYPE (op00type
))))
2342 tree size
= TYPE_SIZE_UNIT (type
);
2343 if (tree_int_cst_equal (size
, op01
))
2344 return fold_build1_loc (loc
, IMAGPART_EXPR
, type
, op00
);
2346 /* ((foo *)&fooarray)[1] => fooarray[1] */
2347 else if (TREE_CODE (op00type
) == ARRAY_TYPE
2348 && (same_type_ignoring_top_level_qualifiers_p
2349 (type
, TREE_TYPE (op00type
))))
2351 tree type_domain
= TYPE_DOMAIN (op00type
);
2352 tree min_val
= size_zero_node
;
2353 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
2354 min_val
= TYPE_MIN_VALUE (type_domain
);
2355 op01
= size_binop_loc (loc
, EXACT_DIV_EXPR
, op01
,
2356 TYPE_SIZE_UNIT (type
));
2357 op01
= size_binop_loc (loc
, PLUS_EXPR
, op01
, min_val
);
2358 return build4_loc (loc
, ARRAY_REF
, type
, op00
, op01
,
2359 NULL_TREE
, NULL_TREE
);
2361 /* Also handle conversion to an empty base class, which
2362 is represented with a NOP_EXPR. */
2363 else if (is_empty_class (type
)
2364 && CLASS_TYPE_P (op00type
)
2365 && DERIVED_FROM_P (type
, op00type
))
2370 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
2371 else if (RECORD_OR_UNION_TYPE_P (op00type
))
2373 tree field
= TYPE_FIELDS (op00type
);
2374 for (; field
; field
= DECL_CHAIN (field
))
2375 if (TREE_CODE (field
) == FIELD_DECL
2376 && tree_int_cst_equal (byte_position (field
), op01
)
2377 && (same_type_ignoring_top_level_qualifiers_p
2378 (TREE_TYPE (field
), type
)))
2380 return fold_build3 (COMPONENT_REF
, type
, op00
,
2387 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
2388 else if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
2389 && (same_type_ignoring_top_level_qualifiers_p
2390 (type
, TREE_TYPE (TREE_TYPE (subtype
)))))
2393 tree min_val
= size_zero_node
;
2394 tree newsub
= cxx_fold_indirect_ref (loc
, TREE_TYPE (subtype
), sub
, NULL
);
2398 sub
= build1_loc (loc
, INDIRECT_REF
, TREE_TYPE (subtype
), sub
);
2399 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
2400 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
2401 min_val
= TYPE_MIN_VALUE (type_domain
);
2402 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
2410 cxx_eval_indirect_ref (const constexpr_ctx
*ctx
, tree t
,
2412 bool *non_constant_p
, bool *overflow_p
)
2414 tree orig_op0
= TREE_OPERAND (t
, 0);
2415 tree op0
= cxx_eval_constant_expression (ctx
, orig_op0
,
2416 /*addr*/false, non_constant_p
,
2418 bool empty_base
= false;
2421 /* Don't VERIFY_CONSTANT here. */
2422 if (*non_constant_p
)
2425 r
= cxx_fold_indirect_ref (EXPR_LOCATION (t
), TREE_TYPE (t
), op0
,
2429 r
= cxx_eval_constant_expression (ctx
, r
,
2430 addr
, non_constant_p
, overflow_p
);
2435 if (TREE_CODE (sub
) == ADDR_EXPR
)
2437 /* We couldn't fold to a constant value. Make sure it's not
2438 something we should have been able to fold. */
2439 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
2440 (TREE_TYPE (TREE_TYPE (sub
)), TREE_TYPE (t
)));
2441 /* DR 1188 says we don't have to deal with this. */
2443 error ("accessing value of %qE through a %qT glvalue in a "
2444 "constant expression", build_fold_indirect_ref (sub
),
2446 *non_constant_p
= true;
2451 /* If we're pulling out the value of an empty base, make sure
2452 that the whole object is constant and then return an empty
2454 if (empty_base
&& !addr
)
2456 VERIFY_CONSTANT (r
);
2457 r
= build_constructor (TREE_TYPE (t
), NULL
);
2458 TREE_CONSTANT (r
) = true;
2463 if (addr
&& op0
!= orig_op0
)
2464 return build1 (INDIRECT_REF
, TREE_TYPE (t
), op0
);
2466 VERIFY_CONSTANT (t
);
2472 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
2473 Shared between potential_constant_expression and
2474 cxx_eval_constant_expression. */
2477 non_const_var_error (tree r
)
2479 tree type
= TREE_TYPE (r
);
2480 error ("the value of %qD is not usable in a constant "
2482 /* Avoid error cascade. */
2483 if (DECL_INITIAL (r
) == error_mark_node
)
2485 if (DECL_DECLARED_CONSTEXPR_P (r
))
2486 inform (DECL_SOURCE_LOCATION (r
),
2487 "%qD used in its own initializer", r
);
2488 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type
))
2490 if (!CP_TYPE_CONST_P (type
))
2491 inform (DECL_SOURCE_LOCATION (r
),
2492 "%q#D is not const", r
);
2493 else if (CP_TYPE_VOLATILE_P (type
))
2494 inform (DECL_SOURCE_LOCATION (r
),
2495 "%q#D is volatile", r
);
2496 else if (!DECL_INITIAL (r
)
2497 || !TREE_CONSTANT (DECL_INITIAL (r
)))
2498 inform (DECL_SOURCE_LOCATION (r
),
2499 "%qD was not initialized with a constant "
2506 if (cxx_dialect
>= cxx11
&& !DECL_DECLARED_CONSTEXPR_P (r
))
2507 inform (DECL_SOURCE_LOCATION (r
),
2508 "%qD was not declared %<constexpr%>", r
);
2510 inform (DECL_SOURCE_LOCATION (r
),
2511 "%qD does not have integral or enumeration type",
2516 /* Subroutine of cxx_eval_constant_expression.
2517 Like cxx_eval_unary_expression, except for trinary expressions. */
2520 cxx_eval_trinary_expression (const constexpr_ctx
*ctx
, tree t
,
2522 bool *non_constant_p
, bool *overflow_p
)
2528 for (i
= 0; i
< 3; i
++)
2530 args
[i
] = cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, i
),
2532 non_constant_p
, overflow_p
);
2533 VERIFY_CONSTANT (args
[i
]);
2536 val
= fold_ternary_loc (EXPR_LOCATION (t
), TREE_CODE (t
), TREE_TYPE (t
),
2537 args
[0], args
[1], args
[2]);
2538 if (val
== NULL_TREE
)
2540 VERIFY_CONSTANT (val
);
2545 var_in_constexpr_fn (tree t
)
2547 tree ctx
= DECL_CONTEXT (t
);
2548 return (cxx_dialect
>= cxx14
&& ctx
&& TREE_CODE (ctx
) == FUNCTION_DECL
2549 && DECL_DECLARED_CONSTEXPR_P (ctx
));
2552 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
2555 cxx_eval_store_expression (const constexpr_ctx
*ctx
, tree t
,
2557 bool *non_constant_p
, bool *overflow_p
)
2559 constexpr_ctx new_ctx
= *ctx
;
2561 /* First we figure out where we're storing to. */
2562 tree target
= TREE_OPERAND (t
, 0);
2563 target
= cxx_eval_constant_expression (ctx
, target
,
2565 non_constant_p
, overflow_p
);
2566 if (*non_constant_p
)
2569 /* And then find the underlying variable. */
2570 vec
<tree
,va_gc
> *refs
= make_tree_vector();
2571 tree object
= NULL_TREE
;
2572 for (tree probe
= target
; object
== NULL_TREE
; )
2574 switch (TREE_CODE (probe
))
2579 vec_safe_push (refs
, TREE_OPERAND (probe
, 1));
2580 vec_safe_push (refs
, TREE_TYPE (probe
));
2581 probe
= TREE_OPERAND (probe
, 0);
2589 /* And then find/build up our initializer for the path to the subobject
2590 we're initializing. */
2592 if (DECL_P (object
))
2593 valp
= ctx
->values
->get (object
);
2598 /* A constant-expression cannot modify objects from outside the
2599 constant-expression. */
2601 error ("modification of %qE is not a constant-expression", object
);
2602 *non_constant_p
= true;
2605 tree type
= TREE_TYPE (object
);
2606 while (!refs
->is_empty())
2608 if (*valp
== NULL_TREE
)
2610 *valp
= build_constructor (type
, NULL
);
2611 CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp
) = true;
2616 ce
.index
= refs
->pop();
2617 ce
.value
= NULL_TREE
;
2619 unsigned HOST_WIDE_INT idx
= 0;
2620 constructor_elt
*cep
= NULL
;
2622 vec_safe_iterate (CONSTRUCTOR_ELTS (*valp
), idx
, &cep
);
2625 if (cp_tree_equal (ce
.index
, cep
->index
))
2628 cep
= vec_safe_push (CONSTRUCTOR_ELTS (*valp
), ce
);
2631 release_tree_vector (refs
);
2633 if ((AGGREGATE_TYPE_P (TREE_TYPE (t
)) || VECTOR_TYPE_P (TREE_TYPE (t
))))
2635 /* Create a new CONSTRUCTOR in case evaluation of the initializer
2636 wants to modify it. */
2637 *valp
= new_ctx
.ctor
= build_constructor (TREE_TYPE (t
), NULL
);
2638 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx
.ctor
) = true;
2639 new_ctx
.object
= target
;
2642 tree init
= cxx_eval_constant_expression (&new_ctx
, TREE_OPERAND (t
, 1),
2644 non_constant_p
, overflow_p
);
2645 if (target
== object
)
2646 /* The hash table might have moved since the get earlier. */
2647 ctx
->values
->put (object
, init
);
2651 if (*non_constant_p
)
2659 /* Evaluate a ++ or -- expression. */
2662 cxx_eval_increment_expression (const constexpr_ctx
*ctx
, tree t
,
2664 bool *non_constant_p
, bool *overflow_p
)
2666 enum tree_code code
= TREE_CODE (t
);
2667 tree type
= TREE_TYPE (t
);
2668 tree op
= TREE_OPERAND (t
, 0);
2669 tree offset
= TREE_OPERAND (t
, 1);
2670 gcc_assert (TREE_CONSTANT (offset
));
2672 /* The operand as an lvalue. */
2673 op
= cxx_eval_constant_expression (ctx
, op
, true,
2674 non_constant_p
, overflow_p
);
2676 /* The operand as an rvalue. */
2677 tree val
= rvalue (op
);
2678 val
= cxx_eval_constant_expression (ctx
, val
, false,
2679 non_constant_p
, overflow_p
);
2680 VERIFY_CONSTANT (val
);
2682 /* The modified value. */
2683 bool inc
= (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
);
2685 if (POINTER_TYPE_P (type
))
2687 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
2688 offset
= convert_to_ptrofftype (offset
);
2690 offset
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (offset
), offset
);
2691 mod
= fold_build2 (POINTER_PLUS_EXPR
, type
, val
, offset
);
2694 mod
= fold_build2 (inc
? PLUS_EXPR
: MINUS_EXPR
, type
, val
, offset
);
2695 VERIFY_CONSTANT (mod
);
2697 /* Storing the modified value. */
2698 tree store
= build2 (MODIFY_EXPR
, type
, op
, mod
);
2699 cxx_eval_constant_expression (ctx
, store
,
2700 true, non_constant_p
, overflow_p
);
2702 /* And the value of the expression. */
2703 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
2705 /* Prefix ops are lvalues. */
2709 /* But we optimize when the caller wants an rvalue. */
2713 /* Postfix ops are rvalues. */
2717 /* Predicates for the meaning of *jump_target. */
2720 returns (tree
*jump_target
)
2723 && TREE_CODE (*jump_target
) == RETURN_EXPR
;
2727 breaks (tree
*jump_target
)
2730 && TREE_CODE (*jump_target
) == LABEL_DECL
2731 && LABEL_DECL_BREAK (*jump_target
);
2735 continues (tree
*jump_target
)
2738 && TREE_CODE (*jump_target
) == LABEL_DECL
2739 && LABEL_DECL_CONTINUE (*jump_target
);
2743 switches (tree
*jump_target
)
2746 && TREE_CODE (*jump_target
) == INTEGER_CST
;
2749 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
2750 at I matches *jump_target. If we're looking for a case label and we see
2751 the default label, copy I into DEFAULT_LABEL. */
2754 label_matches (tree
*jump_target
, tree_stmt_iterator i
,
2755 tree_stmt_iterator
& default_label
)
2757 tree stmt
= tsi_stmt (i
);
2758 switch (TREE_CODE (*jump_target
))
2761 if (TREE_CODE (stmt
) == LABEL_EXPR
2762 && LABEL_EXPR_LABEL (stmt
) == *jump_target
)
2767 if (TREE_CODE (stmt
) == CASE_LABEL_EXPR
)
2769 if (!CASE_LOW (stmt
))
2771 else if (tree_int_cst_equal (*jump_target
, CASE_LOW (stmt
)))
2782 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
2783 semantics, for switch, break, continue, and return. */
2786 cxx_eval_statement_list (const constexpr_ctx
*ctx
, tree t
,
2787 bool *non_constant_p
, bool *overflow_p
,
2790 tree_stmt_iterator i
;
2791 tree_stmt_iterator default_label
= tree_stmt_iterator();
2793 /* In a statement-expression we want to return the last value. */
2797 local_target
= NULL_TREE
;
2798 jump_target
= &local_target
;
2800 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
2803 tree stmt
= tsi_stmt (i
);
2806 if (TREE_CODE (stmt
) == STATEMENT_LIST
)
2807 /* The label we want might be inside. */;
2808 else if (label_matches (jump_target
, i
, default_label
))
2810 *jump_target
= NULL_TREE
;
2814 r
= cxx_eval_constant_expression (ctx
, stmt
, false,
2815 non_constant_p
, overflow_p
,
2817 if (*non_constant_p
)
2819 if (returns (jump_target
) || breaks (jump_target
))
2822 if (switches (jump_target
) && !tsi_end_p (default_label
))
2825 *jump_target
= NULL_TREE
;
2831 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
2832 semantics; continue semantics are covered by cxx_eval_statement_list. */
2835 cxx_eval_loop_expr (const constexpr_ctx
*ctx
, tree t
,
2836 bool *non_constant_p
, bool *overflow_p
,
2839 tree body
= TREE_OPERAND (t
, 0);
2842 cxx_eval_statement_list (ctx
, body
,
2843 non_constant_p
, overflow_p
, jump_target
);
2844 if (returns (jump_target
) || breaks (jump_target
))
2847 if (breaks (jump_target
))
2848 *jump_target
= NULL_TREE
;
2852 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
2856 cxx_eval_switch_expr (const constexpr_ctx
*ctx
, tree t
,
2857 bool *non_constant_p
, bool *overflow_p
,
2860 tree cond
= TREE_OPERAND (t
, 0);
2861 cond
= cxx_eval_constant_expression (ctx
, cond
, false,
2862 non_constant_p
, overflow_p
);
2863 VERIFY_CONSTANT (cond
);
2864 *jump_target
= cond
;
2866 tree body
= TREE_OPERAND (t
, 1);
2867 cxx_eval_statement_list (ctx
, body
,
2868 non_constant_p
, overflow_p
, jump_target
);
2869 if (breaks (jump_target
) || switches (jump_target
))
2870 *jump_target
= NULL_TREE
;
2874 /* Attempt to reduce the expression T to a constant value.
2875 On failure, issue diagnostic and return error_mark_node. */
2876 /* FIXME unify with c_fully_fold */
2877 /* FIXME overflow_p is too global */
2880 cxx_eval_constant_expression (const constexpr_ctx
*ctx
, tree t
,
2882 bool *non_constant_p
, bool *overflow_p
,
2885 constexpr_ctx new_ctx
;
2888 if (t
== error_mark_node
)
2890 *non_constant_p
= true;
2893 if (CONSTANT_CLASS_P (t
))
2895 if (TREE_CODE (t
) == PTRMEM_CST
)
2896 t
= cplus_expand_constant (t
);
2897 else if (TREE_OVERFLOW (t
) && (!flag_permissive
|| ctx
->quiet
))
2901 if (TREE_CODE (t
) != NOP_EXPR
2902 && reduced_constant_expression_p (t
))
2905 switch (TREE_CODE (t
))
2910 /* We ask for an rvalue for the RESULT_DECL when indirecting
2911 through an invisible reference. */
2912 gcc_assert (DECL_BY_REFERENCE (t
));
2913 return (*ctx
->values
->get (t
));
2918 /* else fall through. */
2921 r
= decl_really_constant_value (t
);
2923 r
= decl_constant_value (t
);
2924 if (TREE_CODE (r
) == TARGET_EXPR
2925 && TREE_CODE (TARGET_EXPR_INITIAL (r
)) == CONSTRUCTOR
)
2926 r
= TARGET_EXPR_INITIAL (r
);
2927 if (TREE_CODE (r
) == VAR_DECL
)
2928 if (tree
*p
= ctx
->values
->get (r
))
2933 non_const_var_error (r
);
2934 *non_constant_p
= true;
2942 case CASE_LABEL_EXPR
:
2946 if (!use_new_call
&& ctx
2947 && ctx
->call
&& DECL_CONTEXT (t
) == ctx
->call
->fundef
->decl
)
2948 r
= lookup_parameter_binding (ctx
->call
, t
);
2949 else if (addr
&& TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
2951 else if (tree
*p
= ctx
->values
->get (r
))
2954 /* Defer in case this is only used for its type. */;
2955 else if (TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
)
2956 /* Defer, there's no lvalue->rvalue conversion. */;
2957 else if (is_empty_class (TREE_TYPE (t
)))
2959 /* If the class is empty, we aren't actually loading anything. */
2960 r
= build_constructor (TREE_TYPE (t
), NULL
);
2961 TREE_CONSTANT (r
) = true;
2966 error ("%qE is not a constant expression", t
);
2967 *non_constant_p
= true;
2972 case AGGR_INIT_EXPR
:
2973 r
= cxx_eval_call_expression (ctx
, t
, addr
,
2974 non_constant_p
, overflow_p
);
2979 r
= DECL_EXPR_DECL (t
);
2980 if (AGGREGATE_TYPE_P (TREE_TYPE (r
))
2981 || VECTOR_TYPE_P (TREE_TYPE (r
)))
2985 new_ctx
.ctor
= build_constructor (TREE_TYPE (r
), NULL
);
2986 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx
.ctor
) = true;
2987 new_ctx
.values
->put (r
, new_ctx
.ctor
);
2991 if (tree init
= DECL_INITIAL (r
))
2993 init
= cxx_eval_constant_expression (ctx
, init
,
2995 non_constant_p
, overflow_p
);
2996 ctx
->values
->put (r
, init
);
2998 else if (ctx
== &new_ctx
)
2999 /* We gave it a CONSTRUCTOR above. */;
3001 ctx
->values
->put (r
, NULL_TREE
);
3006 if (!literal_type_p (TREE_TYPE (t
)))
3010 error ("temporary of non-literal type %qT in a "
3011 "constant expression", TREE_TYPE (t
));
3012 explain_non_literal_class (TREE_TYPE (t
));
3014 *non_constant_p
= true;
3017 if ((AGGREGATE_TYPE_P (TREE_TYPE (t
)) || VECTOR_TYPE_P (TREE_TYPE (t
))))
3019 /* We're being expanded without an explicit target, so start
3020 initializing a new object; expansion with an explicit target
3021 strips the TARGET_EXPR before we get here. */
3023 new_ctx
.ctor
= build_constructor (TREE_TYPE (t
), NULL
);
3024 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx
.ctor
) = true;
3025 new_ctx
.object
= TARGET_EXPR_SLOT (t
);
3026 ctx
->values
->put (new_ctx
.object
, new_ctx
.ctor
);
3029 /* Pass false for 'addr' because this indicates
3030 initialization of a temporary. */
3031 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
3033 non_constant_p
, overflow_p
);
3034 if (!*non_constant_p
)
3035 /* Adjust the type of the result to the type of the temporary. */
3036 r
= adjust_temp_type (TREE_TYPE (t
), r
);
3039 tree slot
= TARGET_EXPR_SLOT (t
);
3040 ctx
->values
->put (slot
, r
);
3048 /* In C++11 constexpr evaluation we are looking for the value,
3049 not the side-effect of the initialization. */
3050 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
3052 non_constant_p
, overflow_p
);
3055 /* else fall through */
3057 r
= cxx_eval_store_expression (ctx
, t
, addr
,
3058 non_constant_p
, overflow_p
);
3062 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
3064 non_constant_p
, overflow_p
);
3068 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3070 non_constant_p
, overflow_p
);
3075 /* Avoid evaluating a SAVE_EXPR more than once. */
3076 if (tree
*p
= ctx
->values
->get (t
))
3080 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), addr
,
3081 non_constant_p
, overflow_p
);
3082 ctx
->values
->put (t
, r
);
3086 case NON_LVALUE_EXPR
:
3087 case TRY_CATCH_EXPR
:
3088 case CLEANUP_POINT_EXPR
:
3089 case MUST_NOT_THROW_EXPR
:
3092 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3094 non_constant_p
, overflow_p
,
3098 /* These differ from cxx_eval_unary_expression in that this doesn't
3099 check for a constant operand or result; an address can be
3100 constant without its operand being, and vice versa. */
3102 r
= cxx_eval_indirect_ref (ctx
, t
, addr
,
3103 non_constant_p
, overflow_p
);
3108 tree oldop
= TREE_OPERAND (t
, 0);
3109 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
3111 non_constant_p
, overflow_p
);
3112 /* Don't VERIFY_CONSTANT here. */
3113 if (*non_constant_p
)
3115 gcc_checking_assert (TREE_CODE (op
) != CONSTRUCTOR
);
3116 /* This function does more aggressive folding than fold itself. */
3117 r
= build_fold_addr_expr_with_type (op
, TREE_TYPE (t
));
3118 if (TREE_CODE (r
) == ADDR_EXPR
&& TREE_OPERAND (r
, 0) == oldop
)
3126 case FIX_TRUNC_EXPR
:
3131 case TRUTH_NOT_EXPR
:
3132 case FIXED_CONVERT_EXPR
:
3133 r
= cxx_eval_unary_expression (ctx
, t
, addr
,
3134 non_constant_p
, overflow_p
);
3138 if (SIZEOF_EXPR_TYPE_P (t
))
3139 r
= cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t
, 0)),
3140 SIZEOF_EXPR
, false);
3141 else if (TYPE_P (TREE_OPERAND (t
, 0)))
3142 r
= cxx_sizeof_or_alignof_type (TREE_OPERAND (t
, 0), SIZEOF_EXPR
,
3145 r
= cxx_sizeof_or_alignof_expr (TREE_OPERAND (t
, 0), SIZEOF_EXPR
,
3147 if (r
== error_mark_node
)
3149 VERIFY_CONSTANT (r
);
3154 /* check_return_expr sometimes wraps a TARGET_EXPR in a
3155 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
3156 introduced by build_call_a. */
3157 tree op0
= TREE_OPERAND (t
, 0);
3158 tree op1
= TREE_OPERAND (t
, 1);
3160 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
3161 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
3162 r
= cxx_eval_constant_expression (ctx
, op0
,
3163 addr
, non_constant_p
, overflow_p
,
3167 /* Check that the LHS is constant and then discard it. */
3168 cxx_eval_constant_expression (ctx
, op0
,
3169 false, non_constant_p
, overflow_p
,
3171 op1
= TREE_OPERAND (t
, 1);
3172 r
= cxx_eval_constant_expression (ctx
, op1
,
3173 addr
, non_constant_p
, overflow_p
,
3179 case POINTER_PLUS_EXPR
:
3183 case TRUNC_DIV_EXPR
:
3185 case FLOOR_DIV_EXPR
:
3186 case ROUND_DIV_EXPR
:
3187 case TRUNC_MOD_EXPR
:
3189 case ROUND_MOD_EXPR
:
3191 case EXACT_DIV_EXPR
:
3201 case TRUTH_XOR_EXPR
:
3208 case UNORDERED_EXPR
:
3218 r
= cxx_eval_binary_expression (ctx
, t
, addr
,
3219 non_constant_p
, overflow_p
);
3222 /* fold can introduce non-IF versions of these; still treat them as
3223 short-circuiting. */
3224 case TRUTH_AND_EXPR
:
3225 case TRUTH_ANDIF_EXPR
:
3226 r
= cxx_eval_logical_expression (ctx
, t
, boolean_false_node
,
3229 non_constant_p
, overflow_p
);
3233 case TRUTH_ORIF_EXPR
:
3234 r
= cxx_eval_logical_expression (ctx
, t
, boolean_true_node
,
3237 non_constant_p
, overflow_p
);
3241 r
= cxx_eval_array_reference (ctx
, t
, addr
,
3242 non_constant_p
, overflow_p
);
3246 if (is_overloaded_fn (t
))
3248 /* We can only get here in checking mode via
3249 build_non_dependent_expr, because any expression that
3250 calls or takes the address of the function will have
3251 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
3252 gcc_checking_assert (ctx
->quiet
|| errorcount
);
3253 *non_constant_p
= true;
3256 r
= cxx_eval_component_reference (ctx
, t
, addr
,
3257 non_constant_p
, overflow_p
);
3261 r
= cxx_eval_bit_field_ref (ctx
, t
, addr
,
3262 non_constant_p
, overflow_p
);
3267 r
= cxx_eval_conditional_expression (ctx
, t
, addr
,
3268 non_constant_p
, overflow_p
,
3273 r
= cxx_eval_bare_aggregate (ctx
, t
, addr
,
3274 non_constant_p
, overflow_p
);
3278 /* We can get this in a defaulted constructor for a class with a
3279 non-static data member of array type. Either the initializer will
3280 be NULL, meaning default-initialization, or it will be an lvalue
3281 or xvalue of the same type, meaning direct-initialization from the
3282 corresponding member. */
3283 r
= cxx_eval_vec_init (ctx
, t
, addr
,
3284 non_constant_p
, overflow_p
);
3289 r
= cxx_eval_trinary_expression (ctx
, t
, addr
,
3290 non_constant_p
, overflow_p
);
3294 case VIEW_CONVERT_EXPR
:
3297 tree oldop
= TREE_OPERAND (t
, 0);
3298 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
3300 non_constant_p
, overflow_p
);
3301 if (*non_constant_p
)
3303 if (POINTER_TYPE_P (TREE_TYPE (t
))
3304 && TREE_CODE (op
) == INTEGER_CST
3305 && !integer_zerop (op
))
3308 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3309 "reinterpret_cast from integer to pointer");
3310 *non_constant_p
= true;
3314 /* We didn't fold at the top so we could check for ptr-int
3317 r
= fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), op
);
3318 /* Conversion of an out-of-range value has implementation-defined
3319 behavior; the language considers it different from arithmetic
3320 overflow, which is undefined. */
3321 if (TREE_OVERFLOW_P (r
) && !TREE_OVERFLOW_P (op
))
3322 TREE_OVERFLOW (r
) = false;
3326 case EMPTY_CLASS_EXPR
:
3327 /* This is good enough for a function argument that might not get
3328 used, and they can't do anything with it, so just return it. */
3331 case STATEMENT_LIST
:
3333 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
3334 return cxx_eval_statement_list (&new_ctx
, t
,
3335 non_constant_p
, overflow_p
, jump_target
);
3338 return cxx_eval_constant_expression (ctx
, BIND_EXPR_BODY (t
),
3340 non_constant_p
, overflow_p
,
3343 case PREINCREMENT_EXPR
:
3344 case POSTINCREMENT_EXPR
:
3345 case PREDECREMENT_EXPR
:
3346 case POSTDECREMENT_EXPR
:
3347 return cxx_eval_increment_expression (ctx
, t
,
3348 addr
, non_constant_p
, overflow_p
);
3354 case VEC_DELETE_EXPR
:
3357 /* GCC internal stuff. */
3360 case WITH_CLEANUP_EXPR
:
3361 case NON_DEPENDENT_EXPR
:
3365 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3366 "expression %qE is not a constant-expression", t
);
3367 *non_constant_p
= true;
3370 case PLACEHOLDER_EXPR
:
3371 if (!ctx
|| !ctx
->ctor
|| (addr
&& !ctx
->object
))
3373 /* A placeholder without a referent. We can get here when
3374 checking whether NSDMIs are noexcept, or in massage_init_elt;
3375 just say it's non-constant for now. */
3376 gcc_assert (ctx
->quiet
);
3377 *non_constant_p
= true;
3382 /* Use of the value or address of the current object. We could
3383 use ctx->object unconditionally, but using ctx->ctor when we
3384 can is a minor optimization. */
3385 tree ctor
= addr
? ctx
->object
: ctx
->ctor
;
3386 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3387 (TREE_TYPE (t
), TREE_TYPE (ctor
)));
3388 return cxx_eval_constant_expression
3390 non_constant_p
, overflow_p
);
3395 *jump_target
= TREE_OPERAND (t
, 0);
3396 gcc_assert (breaks (jump_target
) || continues (jump_target
));
3400 cxx_eval_loop_expr (ctx
, t
,
3401 non_constant_p
, overflow_p
, jump_target
);
3405 cxx_eval_switch_expr (ctx
, t
,
3406 non_constant_p
, overflow_p
, jump_target
);
3410 internal_error ("unexpected expression %qE of kind %s", t
,
3411 get_tree_code_name (TREE_CODE (t
)));
3412 *non_constant_p
= true;
3416 if (r
== error_mark_node
)
3417 *non_constant_p
= true;
3419 if (*non_constant_p
)
3426 cxx_eval_outermost_constant_expr (tree t
, bool allow_non_constant
,
3427 bool strict
= true, tree object
= NULL_TREE
)
3429 bool non_constant_p
= false;
3430 bool overflow_p
= false;
3431 constexpr_ctx ctx
= { NULL
, NULL
, NULL
, NULL
, allow_non_constant
, strict
};
3432 hash_map
<tree
,tree
> map
;
3434 tree type
= initialized_type (t
);
3436 if (AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
))
3438 /* In C++14 an NSDMI can participate in aggregate initialization,
3439 and can refer to the address of the object being initialized, so
3440 we need to pass in the relevant VAR_DECL if we want to do the
3441 evaluation in a single pass. The evaluation will dynamically
3442 update ctx.values for the VAR_DECL. We use the same strategy
3443 for C++11 constexpr constructors that refer to the object being
3445 ctx
.ctor
= build_constructor (type
, NULL
);
3446 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctx
.ctor
) = true;
3449 if (TREE_CODE (t
) == TARGET_EXPR
)
3450 object
= TARGET_EXPR_SLOT (t
);
3451 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
3452 object
= AGGR_INIT_EXPR_SLOT (t
);
3454 ctx
.object
= object
;
3456 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3457 (type
, TREE_TYPE (object
)));
3458 if (object
&& DECL_P (object
))
3459 map
.put (object
, ctx
.ctor
);
3460 if (TREE_CODE (r
) == TARGET_EXPR
)
3461 /* Avoid creating another CONSTRUCTOR when we expand the
3463 r
= TARGET_EXPR_INITIAL (r
);
3466 r
= cxx_eval_constant_expression (&ctx
, r
,
3467 false, &non_constant_p
, &overflow_p
);
3469 verify_constant (r
, allow_non_constant
, &non_constant_p
, &overflow_p
);
3471 /* Mutable logic is a bit tricky: we want to allow initialization of
3472 constexpr variables with mutable members, but we can't copy those
3473 members to another constexpr variable. */
3474 if (TREE_CODE (r
) == CONSTRUCTOR
3475 && CONSTRUCTOR_MUTABLE_POISON (r
))
3477 if (!allow_non_constant
)
3478 error ("%qE is not a constant expression because it refers to "
3479 "mutable subobjects of %qT", t
, type
);
3480 non_constant_p
= true;
3483 /* Technically we should check this for all subexpressions, but that
3484 runs into problems with our internal representation of pointer
3485 subtraction and the 5.19 rules are still in flux. */
3486 if (CONVERT_EXPR_CODE_P (TREE_CODE (r
))
3487 && ARITHMETIC_TYPE_P (TREE_TYPE (r
))
3488 && TREE_CODE (TREE_OPERAND (r
, 0)) == ADDR_EXPR
)
3490 if (!allow_non_constant
)
3491 error ("conversion from pointer type %qT "
3492 "to arithmetic type %qT in a constant-expression",
3493 TREE_TYPE (TREE_OPERAND (r
, 0)), TREE_TYPE (r
));
3494 non_constant_p
= true;
3497 if (!non_constant_p
&& overflow_p
)
3498 non_constant_p
= true;
3500 if (non_constant_p
&& !allow_non_constant
)
3501 return error_mark_node
;
3502 else if (non_constant_p
&& TREE_CONSTANT (r
))
3504 /* This isn't actually constant, so unset TREE_CONSTANT. */
3507 else if (TREE_CODE (r
) == CONSTRUCTOR
)
3508 r
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (r
), r
);
3510 r
= build_nop (TREE_TYPE (r
), r
);
3511 TREE_CONSTANT (r
) = false;
3513 else if (non_constant_p
|| r
== t
)
3516 if (TREE_CODE (r
) == CONSTRUCTOR
&& CLASS_TYPE_P (TREE_TYPE (r
)))
3518 if (TREE_CODE (t
) == TARGET_EXPR
3519 && TARGET_EXPR_INITIAL (t
) == r
)
3523 r
= get_target_expr (r
);
3524 TREE_CONSTANT (r
) = true;
3532 /* Returns true if T is a valid subexpression of a constant expression,
3533 even if it isn't itself a constant expression. */
3536 is_sub_constant_expr (tree t
)
3538 bool non_constant_p
= false;
3539 bool overflow_p
= false;
3540 constexpr_ctx ctx
= { NULL
, NULL
, NULL
, NULL
, true, true };
3541 hash_map
<tree
, tree
> map
;
3543 cxx_eval_constant_expression (&ctx
, t
, false, &non_constant_p
,
3545 return !non_constant_p
&& !overflow_p
;
3548 /* If T represents a constant expression returns its reduced value.
3549 Otherwise return error_mark_node. If T is dependent, then
3553 cxx_constant_value (tree t
, tree decl
)
3555 return cxx_eval_outermost_constant_expr (t
, false, true, decl
);
3558 /* If T is a constant expression, returns its reduced value.
3559 Otherwise, if T does not have TREE_CONSTANT set, returns T.
3560 Otherwise, returns a version of T without TREE_CONSTANT. */
3563 maybe_constant_value (tree t
, tree decl
)
3567 if (instantiation_dependent_expression_p (t
)
3568 || type_unknown_p (t
)
3569 || BRACE_ENCLOSED_INITIALIZER_P (t
)
3570 || !potential_constant_expression (t
))
3572 if (TREE_OVERFLOW_P (t
))
3574 t
= build_nop (TREE_TYPE (t
), t
);
3575 TREE_CONSTANT (t
) = false;
3580 r
= cxx_eval_outermost_constant_expr (t
, true, true, decl
);
3581 #ifdef ENABLE_CHECKING
3582 /* cp_tree_equal looks through NOPs, so allow them. */
3584 || CONVERT_EXPR_P (t
)
3585 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
3586 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
3587 || !cp_tree_equal (r
, t
));
3592 /* Like maybe_constant_value but first fully instantiate the argument.
3594 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
3595 (t, tf_none) followed by maybe_constant_value but is more efficient,
3596 because calls instantiation_dependent_expression_p and
3597 potential_constant_expression at most once. */
3600 fold_non_dependent_expr (tree t
)
3605 /* If we're in a template, but T isn't value dependent, simplify
3606 it. We're supposed to treat:
3608 template <typename T> void f(T[1 + 1]);
3609 template <typename T> void f(T[2]);
3611 as two declarations of the same function, for example. */
3612 if (processing_template_decl
)
3614 if (!instantiation_dependent_expression_p (t
)
3615 && potential_constant_expression (t
))
3617 processing_template_decl_sentinel s
;
3618 t
= instantiate_non_dependent_expr_internal (t
, tf_none
);
3620 if (type_unknown_p (t
)
3621 || BRACE_ENCLOSED_INITIALIZER_P (t
))
3623 if (TREE_OVERFLOW_P (t
))
3625 t
= build_nop (TREE_TYPE (t
), t
);
3626 TREE_CONSTANT (t
) = false;
3631 tree r
= cxx_eval_outermost_constant_expr (t
, true, true, NULL_TREE
);
3632 #ifdef ENABLE_CHECKING
3633 /* cp_tree_equal looks through NOPs, so allow them. */
3635 || CONVERT_EXPR_P (t
)
3636 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
3637 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
3638 || !cp_tree_equal (r
, t
));
3642 else if (TREE_OVERFLOW_P (t
))
3644 t
= build_nop (TREE_TYPE (t
), t
);
3645 TREE_CONSTANT (t
) = false;
3650 return maybe_constant_value (t
);
3653 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
3654 than wrapped in a TARGET_EXPR. */
3657 maybe_constant_init (tree t
, tree decl
)
3659 if (TREE_CODE (t
) == EXPR_STMT
)
3660 t
= TREE_OPERAND (t
, 0);
3661 if (TREE_CODE (t
) == CONVERT_EXPR
3662 && VOID_TYPE_P (TREE_TYPE (t
)))
3663 t
= TREE_OPERAND (t
, 0);
3664 if (TREE_CODE (t
) == INIT_EXPR
)
3665 t
= TREE_OPERAND (t
, 1);
3666 if (instantiation_dependent_expression_p (t
)
3667 || type_unknown_p (t
)
3668 || BRACE_ENCLOSED_INITIALIZER_P (t
)
3669 || !potential_static_init_expression (t
))
3670 /* Don't try to evaluate it. */;
3672 t
= cxx_eval_outermost_constant_expr (t
, true, false, decl
);
3673 if (TREE_CODE (t
) == TARGET_EXPR
)
3675 tree init
= TARGET_EXPR_INITIAL (t
);
3676 if (TREE_CODE (init
) == CONSTRUCTOR
)
3683 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
3684 /* Return true if the object referred to by REF has automatic or thread
3687 enum { ck_ok
, ck_bad
, ck_unknown
};
3689 check_automatic_or_tls (tree ref
)
3692 HOST_WIDE_INT bitsize
, bitpos
;
3694 int volatilep
= 0, unsignedp
= 0;
3695 tree decl
= get_inner_reference (ref
, &bitsize
, &bitpos
, &offset
,
3696 &mode
, &unsignedp
, &volatilep
, false);
3699 /* If there isn't a decl in the middle, we don't know the linkage here,
3700 and this isn't a constant expression anyway. */
3703 dk
= decl_storage_duration (decl
);
3704 return (dk
== dk_auto
|| dk
== dk_thread
) ? ck_bad
: ck_ok
;
3708 /* Return true if T denotes a potentially constant expression. Issue
3709 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
3710 an lvalue-rvalue conversion is implied.
3712 C++0x [expr.const] used to say
3714 6 An expression is a potential constant expression if it is
3715 a constant expression where all occurrences of function
3716 parameters are replaced by arbitrary constant expressions
3717 of the appropriate type.
3719 2 A conditional expression is a constant expression unless it
3720 involves one of the following as a potentially evaluated
3721 subexpression (3.2), but subexpressions of logical AND (5.14),
3722 logical OR (5.15), and conditional (5.16) operations that are
3723 not evaluated are not considered. */
3726 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
,
3727 tsubst_flags_t flags
)
3729 #define RECUR(T,RV) potential_constant_expression_1 ((T), (RV), strict, flags)
3730 enum { any
= false, rval
= true };
3734 if (t
== error_mark_node
)
3738 if (TREE_THIS_VOLATILE (t
))
3740 if (flags
& tf_error
)
3741 error ("expression %qE has side-effects", t
);
3744 if (CONSTANT_CLASS_P (t
))
3747 switch (TREE_CODE (t
))
3753 case TEMPLATE_ID_EXPR
:
3756 case CASE_LABEL_EXPR
:
3762 case TEMPLATE_PARM_INDEX
:
3764 case IDENTIFIER_NODE
:
3765 case USERDEF_LITERAL
:
3766 /* We can see a FIELD_DECL in a pointer-to-member expression. */
3771 case PLACEHOLDER_EXPR
:
3776 case AGGR_INIT_EXPR
:
3778 /* -- an invocation of a function other than a constexpr function
3779 or a constexpr constructor. */
3781 tree fun
= get_function_named_in_call (t
);
3782 const int nargs
= call_expr_nargs (t
);
3785 if (fun
== NULL_TREE
)
3787 /* fold_call_expr can't do anything with IFN calls. */
3788 if (flags
& tf_error
)
3789 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3790 "call to internal function");
3793 if (is_overloaded_fn (fun
))
3795 if (TREE_CODE (fun
) == FUNCTION_DECL
)
3797 if (builtin_valid_in_constant_expr_p (fun
))
3799 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
3800 /* Allow any built-in function; if the expansion
3801 isn't constant, we'll deal with that then. */
3802 && !is_builtin_fn (fun
))
3804 if (flags
& tf_error
)
3806 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3807 "call to non-constexpr function %qD", fun
);
3808 explain_invalid_constexpr_fn (fun
);
3812 /* A call to a non-static member function takes the address
3813 of the object as the first argument. But in a constant
3814 expression the address will be folded away, so look
3816 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
3817 && !DECL_CONSTRUCTOR_P (fun
))
3819 tree x
= get_nth_callarg (t
, 0);
3820 if (is_this_parameter (x
))
3822 else if (!RECUR (x
, rval
))
3829 if (!RECUR (fun
, true))
3831 fun
= get_first_fn (fun
);
3833 /* Skip initial arguments to base constructors. */
3834 if (DECL_BASE_CONSTRUCTOR_P (fun
))
3835 i
= num_artificial_parms_for (fun
);
3836 fun
= DECL_ORIGIN (fun
);
3840 if (RECUR (fun
, rval
))
3841 /* Might end up being a constant function pointer. */;
3845 for (; i
< nargs
; ++i
)
3847 tree x
= get_nth_callarg (t
, i
);
3848 if (!RECUR (x
, rval
))
3854 case NON_LVALUE_EXPR
:
3855 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
3856 -- an lvalue of integral type that refers to a non-volatile
3857 const variable or static data member initialized with
3858 constant expressions, or
3860 -- an lvalue of literal type that refers to non-volatile
3861 object defined with constexpr, or that refers to a
3862 sub-object of such an object; */
3863 return RECUR (TREE_OPERAND (t
, 0), rval
);
3867 && !decl_constant_var_p (t
)
3869 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t
))
3870 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t
))
3871 && !var_in_constexpr_fn (t
)
3872 && !dependent_type_p (TREE_TYPE (t
)))
3874 if (flags
& tf_error
)
3875 non_const_var_error (t
);
3882 case VIEW_CONVERT_EXPR
:
3883 /* -- a reinterpret_cast. FIXME not implemented, and this rule
3884 may change to something more specific to type-punning (DR 1312). */
3886 tree from
= TREE_OPERAND (t
, 0);
3887 if (POINTER_TYPE_P (TREE_TYPE (t
))
3888 && TREE_CODE (from
) == INTEGER_CST
3889 && !integer_zerop (from
))
3891 if (flags
& tf_error
)
3892 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3893 "reinterpret_cast from integer to pointer");
3896 return (RECUR (from
, TREE_CODE (t
) != VIEW_CONVERT_EXPR
));
3900 /* -- a unary operator & that is applied to an lvalue that
3901 designates an object with thread or automatic storage
3903 t
= TREE_OPERAND (t
, 0);
3905 if (TREE_CODE (t
) == OFFSET_REF
&& PTRMEM_OK_P (t
))
3906 /* A pointer-to-member constant. */
3910 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
3911 any checking here, as we might dereference the pointer later. If
3912 we remove this code, also remove check_automatic_or_tls. */
3913 i
= check_automatic_or_tls (t
);
3918 if (flags
& tf_error
)
3919 error ("address-of an object %qE with thread local or "
3920 "automatic storage is not a constant expression", t
);
3924 return RECUR (t
, any
);
3930 /* -- a class member access unless its postfix-expression is
3931 of literal type or of pointer to literal type. */
3932 /* This test would be redundant, as it follows from the
3933 postfix-expression being a potential constant expression. */
3934 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
3936 case EXPR_PACK_EXPANSION
:
3937 return RECUR (PACK_EXPANSION_PATTERN (t
), want_rval
);
3941 tree x
= TREE_OPERAND (t
, 0);
3943 if (is_this_parameter (x
))
3945 if (DECL_CONTEXT (x
)
3946 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x
)))
3948 if (flags
& tf_error
)
3949 error ("use of %<this%> in a constant expression");
3954 return RECUR (x
, rval
);
3957 case STATEMENT_LIST
:
3959 tree_stmt_iterator i
;
3960 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
3962 if (!RECUR (tsi_stmt (i
), any
))
3970 if (cxx_dialect
< cxx14
)
3972 if (!RECUR (TREE_OPERAND (t
, 0), any
))
3974 if (!RECUR (TREE_OPERAND (t
, 1), rval
))
3979 if (cxx_dialect
< cxx14
)
3981 if (!RECUR (TREE_OPERAND (t
, 0), rval
))
3983 if (!RECUR (TREE_OPERAND (t
, 2), rval
))
3988 if (!RECUR (IF_COND (t
), rval
))
3990 if (!RECUR (THEN_CLAUSE (t
), any
))
3992 if (!RECUR (ELSE_CLAUSE (t
), any
))
3997 if (!RECUR (DO_COND (t
), rval
))
3999 if (!RECUR (DO_BODY (t
), any
))
4004 if (!RECUR (FOR_INIT_STMT (t
), any
))
4006 if (!RECUR (FOR_COND (t
), rval
))
4008 if (!RECUR (FOR_EXPR (t
), any
))
4010 if (!RECUR (FOR_BODY (t
), any
))
4015 if (!RECUR (WHILE_COND (t
), rval
))
4017 if (!RECUR (WHILE_BODY (t
), any
))
4022 if (!RECUR (SWITCH_STMT_COND (t
), rval
))
4024 if (!RECUR (SWITCH_STMT_BODY (t
), any
))
4029 return RECUR (STMT_EXPR_STMT (t
), rval
);
4032 case DYNAMIC_CAST_EXPR
:
4033 case PSEUDO_DTOR_EXPR
:
4037 case VEC_DELETE_EXPR
:
4040 case OMP_ATOMIC_READ
:
4041 case OMP_ATOMIC_CAPTURE_OLD
:
4042 case OMP_ATOMIC_CAPTURE_NEW
:
4043 /* GCC internal stuff. */
4046 case TRANSACTION_EXPR
:
4049 if (flags
& tf_error
)
4050 error ("expression %qE is not a constant-expression", t
);
4054 /* -- a typeid expression whose operand is of polymorphic
4057 tree e
= TREE_OPERAND (t
, 0);
4058 if (!TYPE_P (e
) && !type_dependent_expression_p (e
)
4059 && TYPE_POLYMORPHIC_P (TREE_TYPE (e
)))
4061 if (flags
& tf_error
)
4062 error ("typeid-expression is not a constant expression "
4063 "because %qE is of polymorphic type", e
);
4070 /* -- a subtraction where both operands are pointers. */
4071 if (TYPE_PTR_P (TREE_OPERAND (t
, 0))
4072 && TYPE_PTR_P (TREE_OPERAND (t
, 1)))
4074 if (flags
& tf_error
)
4075 error ("difference of two pointer expressions is not "
4076 "a constant expression");
4088 /* -- a relational or equality operator where at least
4089 one of the operands is a pointer. */
4090 if (TYPE_PTR_P (TREE_OPERAND (t
, 0))
4091 || TYPE_PTR_P (TREE_OPERAND (t
, 1)))
4093 if (flags
& tf_error
)
4094 error ("pointer comparison expression is not a "
4095 "constant expression");
4101 case PREINCREMENT_EXPR
:
4102 case POSTINCREMENT_EXPR
:
4103 case PREDECREMENT_EXPR
:
4104 case POSTDECREMENT_EXPR
:
4105 if (cxx_dialect
< cxx14
)
4111 if (TYPE_P (TREE_OPERAND (t
, 0)))
4113 /* else fall through. */
4119 case FIX_TRUNC_EXPR
:
4123 case TRUTH_NOT_EXPR
:
4124 case FIXED_CONVERT_EXPR
:
4125 case UNARY_PLUS_EXPR
:
4127 return RECUR (TREE_OPERAND (t
, 0), rval
);
4130 case CONST_CAST_EXPR
:
4131 case STATIC_CAST_EXPR
:
4132 case REINTERPRET_CAST_EXPR
:
4133 case IMPLICIT_CONV_EXPR
:
4134 if (cxx_dialect
< cxx11
4135 && !dependent_type_p (TREE_TYPE (t
))
4136 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
)))
4137 /* In C++98, a conversion to non-integral type can't be part of a
4138 constant expression. */
4140 if (flags
& tf_error
)
4141 error ("cast to non-integral type %qT in a constant expression",
4146 return (RECUR (TREE_OPERAND (t
, 0),
4147 TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
));
4150 return RECUR (BIND_EXPR_BODY (t
), want_rval
);
4152 case WITH_CLEANUP_EXPR
:
4153 case CLEANUP_POINT_EXPR
:
4154 case MUST_NOT_THROW_EXPR
:
4155 case TRY_CATCH_EXPR
:
4160 case NON_DEPENDENT_EXPR
:
4161 /* For convenience. */
4163 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
4166 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
4169 if (!literal_type_p (TREE_TYPE (t
)))
4171 if (flags
& tf_error
)
4173 error ("temporary of non-literal type %qT in a "
4174 "constant expression", TREE_TYPE (t
));
4175 explain_non_literal_class (TREE_TYPE (t
));
4180 return RECUR (TREE_OPERAND (t
, 1), rval
);
4184 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
4185 constructor_elt
*ce
;
4186 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
4187 if (!RECUR (ce
->value
, want_rval
))
4194 gcc_assert (TREE_PURPOSE (t
) == NULL_TREE
4195 || DECL_P (TREE_PURPOSE (t
)));
4196 if (!RECUR (TREE_VALUE (t
), want_rval
))
4198 if (TREE_CHAIN (t
) == NULL_TREE
)
4200 return RECUR (TREE_CHAIN (t
), want_rval
);
4203 case TRUNC_DIV_EXPR
:
4205 case FLOOR_DIV_EXPR
:
4206 case ROUND_DIV_EXPR
:
4207 case TRUNC_MOD_EXPR
:
4209 case ROUND_MOD_EXPR
:
4211 tree denom
= TREE_OPERAND (t
, 1);
4212 if (!RECUR (denom
, rval
))
4214 /* We can't call cxx_eval_outermost_constant_expr on an expression
4215 that hasn't been through instantiate_non_dependent_expr yet. */
4216 if (!processing_template_decl
)
4217 denom
= cxx_eval_outermost_constant_expr (denom
, true);
4218 if (integer_zerop (denom
))
4220 if (flags
& tf_error
)
4221 error ("division by zero is not a constant-expression");
4227 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
4233 /* check_return_expr sometimes wraps a TARGET_EXPR in a
4234 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
4235 introduced by build_call_a. */
4236 tree op0
= TREE_OPERAND (t
, 0);
4237 tree op1
= TREE_OPERAND (t
, 1);
4239 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
4240 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
4241 return RECUR (op0
, want_rval
);
4246 /* If the first operand is the non-short-circuit constant, look at
4247 the second operand; otherwise we only care about the first one for
4249 case TRUTH_AND_EXPR
:
4250 case TRUTH_ANDIF_EXPR
:
4251 tmp
= boolean_true_node
;
4254 case TRUTH_ORIF_EXPR
:
4255 tmp
= boolean_false_node
;
4258 tree op
= TREE_OPERAND (t
, 0);
4259 if (!RECUR (op
, rval
))
4261 if (!processing_template_decl
)
4262 op
= cxx_eval_outermost_constant_expr (op
, true);
4263 if (tree_int_cst_equal (op
, tmp
))
4264 return RECUR (TREE_OPERAND (t
, 1), rval
);
4271 case POINTER_PLUS_EXPR
:
4273 case EXACT_DIV_EXPR
:
4283 case TRUTH_XOR_EXPR
:
4284 case UNORDERED_EXPR
:
4297 case ARRAY_RANGE_REF
:
4301 for (i
= 0; i
< 2; ++i
)
4302 if (!RECUR (TREE_OPERAND (t
, i
), want_rval
))
4306 case CILK_SYNC_STMT
:
4307 case CILK_SPAWN_STMT
:
4308 case ARRAY_NOTATION_REF
:
4313 for (i
= 0; i
< 3; ++i
)
4314 if (!RECUR (TREE_OPERAND (t
, i
), true))
4320 /* If the condition is a known constant, we know which of the legs we
4321 care about; otherwise we only require that the condition and
4322 either of the legs be potentially constant. */
4323 tmp
= TREE_OPERAND (t
, 0);
4324 if (!RECUR (tmp
, rval
))
4326 if (!processing_template_decl
)
4327 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
4328 if (integer_zerop (tmp
))
4329 return RECUR (TREE_OPERAND (t
, 2), want_rval
);
4330 else if (TREE_CODE (tmp
) == INTEGER_CST
)
4331 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
4332 for (i
= 1; i
< 3; ++i
)
4333 if (potential_constant_expression_1 (TREE_OPERAND (t
, i
),
4334 want_rval
, strict
, tf_none
))
4336 if (flags
& tf_error
)
4337 error ("expression %qE is not a constant-expression", t
);
4341 if (VEC_INIT_EXPR_IS_CONSTEXPR (t
))
4343 if (flags
& tf_error
)
4345 error ("non-constant array initialization");
4346 diagnose_non_constexpr_vec_init (t
);
4351 if (objc_is_property_ref (t
))
4354 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t
)));
4361 /* The main entry point to the above. */
4364 potential_constant_expression (tree t
)
4366 return potential_constant_expression_1 (t
, false, true, tf_none
);
4370 potential_static_init_expression (tree t
)
4372 return potential_constant_expression_1 (t
, false, false, tf_none
);
4375 /* As above, but require a constant rvalue. */
4378 potential_rvalue_constant_expression (tree t
)
4380 return potential_constant_expression_1 (t
, true, true, tf_none
);
4383 /* Like above, but complain about non-constant expressions. */
4386 require_potential_constant_expression (tree t
)
4388 return potential_constant_expression_1 (t
, false, true, tf_warning_or_error
);
4391 /* Cross product of the above. */
4394 require_potential_rvalue_constant_expression (tree t
)
4396 return potential_constant_expression_1 (t
, true, true, tf_warning_or_error
);
4399 #include "gt-cp-constexpr.h"