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_builtin_call_array (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);
2586 gcc_assert (DECL_P (object
));
2590 /* And then find/build up our initializer for the path to the subobject
2591 we're initializing. */
2592 tree
*valp
= ctx
->values
->get (object
);
2595 /* A constant-expression cannot modify objects from outside the
2596 constant-expression. */
2598 error ("modification of %qD is not a constant-expression", object
);
2599 *non_constant_p
= true;
2602 tree type
= TREE_TYPE (object
);
2603 while (!refs
->is_empty())
2605 if (*valp
== NULL_TREE
)
2607 *valp
= build_constructor (type
, NULL
);
2608 CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp
) = true;
2613 ce
.index
= refs
->pop();
2614 ce
.value
= NULL_TREE
;
2616 unsigned HOST_WIDE_INT idx
= 0;
2617 constructor_elt
*cep
= NULL
;
2619 vec_safe_iterate (CONSTRUCTOR_ELTS (*valp
), idx
, &cep
);
2622 if (cp_tree_equal (ce
.index
, cep
->index
))
2625 cep
= vec_safe_push (CONSTRUCTOR_ELTS (*valp
), ce
);
2628 release_tree_vector (refs
);
2630 if ((AGGREGATE_TYPE_P (TREE_TYPE (t
)) || VECTOR_TYPE_P (TREE_TYPE (t
))))
2632 /* Create a new CONSTRUCTOR in case evaluation of the initializer
2633 wants to modify it. */
2634 *valp
= new_ctx
.ctor
= build_constructor (TREE_TYPE (t
), NULL
);
2635 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx
.ctor
) = true;
2636 new_ctx
.object
= target
;
2639 tree init
= cxx_eval_constant_expression (&new_ctx
, TREE_OPERAND (t
, 1),
2641 non_constant_p
, overflow_p
);
2642 if (target
== object
)
2643 /* The hash table might have moved since the get earlier. */
2644 ctx
->values
->put (object
, init
);
2648 if (*non_constant_p
)
2656 /* Evaluate a ++ or -- expression. */
2659 cxx_eval_increment_expression (const constexpr_ctx
*ctx
, tree t
,
2661 bool *non_constant_p
, bool *overflow_p
)
2663 enum tree_code code
= TREE_CODE (t
);
2664 tree type
= TREE_TYPE (t
);
2665 tree op
= TREE_OPERAND (t
, 0);
2666 tree offset
= TREE_OPERAND (t
, 1);
2667 gcc_assert (TREE_CONSTANT (offset
));
2669 /* The operand as an lvalue. */
2670 op
= cxx_eval_constant_expression (ctx
, op
, true,
2671 non_constant_p
, overflow_p
);
2673 /* The operand as an rvalue. */
2674 tree val
= rvalue (op
);
2675 val
= cxx_eval_constant_expression (ctx
, val
, false,
2676 non_constant_p
, overflow_p
);
2677 VERIFY_CONSTANT (val
);
2679 /* The modified value. */
2680 bool inc
= (code
== PREINCREMENT_EXPR
|| code
== POSTINCREMENT_EXPR
);
2682 if (POINTER_TYPE_P (type
))
2684 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
2685 offset
= convert_to_ptrofftype (offset
);
2687 offset
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (offset
), offset
);
2688 mod
= fold_build2 (POINTER_PLUS_EXPR
, type
, val
, offset
);
2691 mod
= fold_build2 (inc
? PLUS_EXPR
: MINUS_EXPR
, type
, val
, offset
);
2692 VERIFY_CONSTANT (mod
);
2694 /* Storing the modified value. */
2695 tree store
= build2 (MODIFY_EXPR
, type
, op
, mod
);
2696 cxx_eval_constant_expression (ctx
, store
,
2697 true, non_constant_p
, overflow_p
);
2699 /* And the value of the expression. */
2700 if (code
== PREINCREMENT_EXPR
|| code
== PREDECREMENT_EXPR
)
2702 /* Prefix ops are lvalues. */
2706 /* But we optimize when the caller wants an rvalue. */
2710 /* Postfix ops are rvalues. */
2714 /* Predicates for the meaning of *jump_target. */
2717 returns (tree
*jump_target
)
2720 && TREE_CODE (*jump_target
) == RETURN_EXPR
;
2724 breaks (tree
*jump_target
)
2727 && TREE_CODE (*jump_target
) == LABEL_DECL
2728 && LABEL_DECL_BREAK (*jump_target
);
2732 continues (tree
*jump_target
)
2735 && TREE_CODE (*jump_target
) == LABEL_DECL
2736 && LABEL_DECL_CONTINUE (*jump_target
);
2740 switches (tree
*jump_target
)
2743 && TREE_CODE (*jump_target
) == INTEGER_CST
;
2746 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
2747 at I matches *jump_target. If we're looking for a case label and we see
2748 the default label, copy I into DEFAULT_LABEL. */
2751 label_matches (tree
*jump_target
, tree_stmt_iterator i
,
2752 tree_stmt_iterator
& default_label
)
2754 tree stmt
= tsi_stmt (i
);
2755 switch (TREE_CODE (*jump_target
))
2758 if (TREE_CODE (stmt
) == LABEL_EXPR
2759 && LABEL_EXPR_LABEL (stmt
) == *jump_target
)
2764 if (TREE_CODE (stmt
) == CASE_LABEL_EXPR
)
2766 if (!CASE_LOW (stmt
))
2768 else if (tree_int_cst_equal (*jump_target
, CASE_LOW (stmt
)))
2779 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
2780 semantics, for switch, break, continue, and return. */
2783 cxx_eval_statement_list (const constexpr_ctx
*ctx
, tree t
,
2784 bool *non_constant_p
, bool *overflow_p
,
2787 tree_stmt_iterator i
;
2788 tree_stmt_iterator default_label
= tree_stmt_iterator();
2790 /* In a statement-expression we want to return the last value. */
2794 local_target
= NULL_TREE
;
2795 jump_target
= &local_target
;
2797 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
2800 tree stmt
= tsi_stmt (i
);
2803 if (TREE_CODE (stmt
) == STATEMENT_LIST
)
2804 /* The label we want might be inside. */;
2805 else if (label_matches (jump_target
, i
, default_label
))
2807 *jump_target
= NULL_TREE
;
2811 r
= cxx_eval_constant_expression (ctx
, stmt
, false,
2812 non_constant_p
, overflow_p
,
2814 if (*non_constant_p
)
2816 if (returns (jump_target
) || breaks (jump_target
))
2819 if (switches (jump_target
) && !tsi_end_p (default_label
))
2822 *jump_target
= NULL_TREE
;
2828 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
2829 semantics; continue semantics are covered by cxx_eval_statement_list. */
2832 cxx_eval_loop_expr (const constexpr_ctx
*ctx
, tree t
,
2833 bool *non_constant_p
, bool *overflow_p
,
2836 tree body
= TREE_OPERAND (t
, 0);
2839 cxx_eval_statement_list (ctx
, body
,
2840 non_constant_p
, overflow_p
, jump_target
);
2841 if (returns (jump_target
) || breaks (jump_target
))
2844 if (breaks (jump_target
))
2845 *jump_target
= NULL_TREE
;
2849 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
2853 cxx_eval_switch_expr (const constexpr_ctx
*ctx
, tree t
,
2854 bool *non_constant_p
, bool *overflow_p
,
2857 tree cond
= TREE_OPERAND (t
, 0);
2858 cond
= cxx_eval_constant_expression (ctx
, cond
, false,
2859 non_constant_p
, overflow_p
);
2860 VERIFY_CONSTANT (cond
);
2861 *jump_target
= cond
;
2863 tree body
= TREE_OPERAND (t
, 1);
2864 cxx_eval_statement_list (ctx
, body
,
2865 non_constant_p
, overflow_p
, jump_target
);
2866 if (breaks (jump_target
) || switches (jump_target
))
2867 *jump_target
= NULL_TREE
;
2871 /* Attempt to reduce the expression T to a constant value.
2872 On failure, issue diagnostic and return error_mark_node. */
2873 /* FIXME unify with c_fully_fold */
2874 /* FIXME overflow_p is too global */
2877 cxx_eval_constant_expression (const constexpr_ctx
*ctx
, tree t
,
2879 bool *non_constant_p
, bool *overflow_p
,
2882 constexpr_ctx new_ctx
;
2885 if (t
== error_mark_node
)
2887 *non_constant_p
= true;
2890 if (CONSTANT_CLASS_P (t
))
2892 if (TREE_CODE (t
) == PTRMEM_CST
)
2893 t
= cplus_expand_constant (t
);
2894 else if (TREE_OVERFLOW (t
) && (!flag_permissive
|| ctx
->quiet
))
2898 if (TREE_CODE (t
) != NOP_EXPR
2899 && reduced_constant_expression_p (t
))
2902 switch (TREE_CODE (t
))
2907 /* We ask for an rvalue for the RESULT_DECL when indirecting
2908 through an invisible reference. */
2909 gcc_assert (DECL_BY_REFERENCE (t
));
2910 return (*ctx
->values
->get (t
));
2915 /* else fall through. */
2918 r
= decl_really_constant_value (t
);
2920 r
= decl_constant_value (t
);
2921 if (TREE_CODE (r
) == TARGET_EXPR
2922 && TREE_CODE (TARGET_EXPR_INITIAL (r
)) == CONSTRUCTOR
)
2923 r
= TARGET_EXPR_INITIAL (r
);
2924 if (TREE_CODE (r
) == VAR_DECL
)
2925 if (tree
*p
= ctx
->values
->get (r
))
2930 non_const_var_error (r
);
2931 *non_constant_p
= true;
2939 case CASE_LABEL_EXPR
:
2943 if (!use_new_call
&& ctx
2944 && ctx
->call
&& DECL_CONTEXT (t
) == ctx
->call
->fundef
->decl
)
2945 r
= lookup_parameter_binding (ctx
->call
, t
);
2946 else if (addr
&& TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
)
2948 else if (tree
*p
= ctx
->values
->get (r
))
2951 /* Defer in case this is only used for its type. */;
2952 else if (TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
)
2953 /* Defer, there's no lvalue->rvalue conversion. */;
2954 else if (is_empty_class (TREE_TYPE (t
)))
2956 /* If the class is empty, we aren't actually loading anything. */
2957 r
= build_constructor (TREE_TYPE (t
), NULL
);
2958 TREE_CONSTANT (r
) = true;
2963 error ("%qE is not a constant expression", t
);
2964 *non_constant_p
= true;
2969 case AGGR_INIT_EXPR
:
2970 r
= cxx_eval_call_expression (ctx
, t
, addr
,
2971 non_constant_p
, overflow_p
);
2976 r
= DECL_EXPR_DECL (t
);
2977 if (AGGREGATE_TYPE_P (TREE_TYPE (r
))
2978 || VECTOR_TYPE_P (TREE_TYPE (r
)))
2982 new_ctx
.ctor
= build_constructor (TREE_TYPE (r
), NULL
);
2983 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx
.ctor
) = true;
2984 new_ctx
.values
->put (r
, new_ctx
.ctor
);
2988 if (tree init
= DECL_INITIAL (r
))
2990 init
= cxx_eval_constant_expression (ctx
, init
,
2992 non_constant_p
, overflow_p
);
2993 ctx
->values
->put (r
, init
);
2995 else if (ctx
== &new_ctx
)
2996 /* We gave it a CONSTRUCTOR above. */;
2998 ctx
->values
->put (r
, NULL_TREE
);
3003 if (!literal_type_p (TREE_TYPE (t
)))
3007 error ("temporary of non-literal type %qT in a "
3008 "constant expression", TREE_TYPE (t
));
3009 explain_non_literal_class (TREE_TYPE (t
));
3011 *non_constant_p
= true;
3014 if ((AGGREGATE_TYPE_P (TREE_TYPE (t
)) || VECTOR_TYPE_P (TREE_TYPE (t
))))
3016 /* We're being expanded without an explicit target, so start
3017 initializing a new object; expansion with an explicit target
3018 strips the TARGET_EXPR before we get here. */
3020 new_ctx
.ctor
= build_constructor (TREE_TYPE (t
), NULL
);
3021 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx
.ctor
) = true;
3022 new_ctx
.object
= TARGET_EXPR_SLOT (t
);
3023 ctx
->values
->put (new_ctx
.object
, new_ctx
.ctor
);
3026 /* Pass false for 'addr' because this indicates
3027 initialization of a temporary. */
3028 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
3030 non_constant_p
, overflow_p
);
3031 if (!*non_constant_p
)
3032 /* Adjust the type of the result to the type of the temporary. */
3033 r
= adjust_temp_type (TREE_TYPE (t
), r
);
3036 tree slot
= TARGET_EXPR_SLOT (t
);
3037 ctx
->values
->put (slot
, r
);
3045 /* In C++11 constexpr evaluation we are looking for the value,
3046 not the side-effect of the initialization. */
3047 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
3049 non_constant_p
, overflow_p
);
3052 /* else fall through */
3054 r
= cxx_eval_store_expression (ctx
, t
, addr
,
3055 non_constant_p
, overflow_p
);
3059 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 1),
3061 non_constant_p
, overflow_p
);
3065 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3067 non_constant_p
, overflow_p
);
3072 /* Avoid evaluating a SAVE_EXPR more than once. */
3073 if (tree
*p
= ctx
->values
->get (t
))
3077 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0), addr
,
3078 non_constant_p
, overflow_p
);
3079 ctx
->values
->put (t
, r
);
3083 case NON_LVALUE_EXPR
:
3084 case TRY_CATCH_EXPR
:
3085 case CLEANUP_POINT_EXPR
:
3086 case MUST_NOT_THROW_EXPR
:
3089 r
= cxx_eval_constant_expression (ctx
, TREE_OPERAND (t
, 0),
3091 non_constant_p
, overflow_p
,
3095 /* These differ from cxx_eval_unary_expression in that this doesn't
3096 check for a constant operand or result; an address can be
3097 constant without its operand being, and vice versa. */
3099 r
= cxx_eval_indirect_ref (ctx
, t
, addr
,
3100 non_constant_p
, overflow_p
);
3105 tree oldop
= TREE_OPERAND (t
, 0);
3106 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
3108 non_constant_p
, overflow_p
);
3109 /* Don't VERIFY_CONSTANT here. */
3110 if (*non_constant_p
)
3112 gcc_checking_assert (TREE_CODE (op
) != CONSTRUCTOR
);
3113 /* This function does more aggressive folding than fold itself. */
3114 r
= build_fold_addr_expr_with_type (op
, TREE_TYPE (t
));
3115 if (TREE_CODE (r
) == ADDR_EXPR
&& TREE_OPERAND (r
, 0) == oldop
)
3123 case FIX_TRUNC_EXPR
:
3128 case TRUTH_NOT_EXPR
:
3129 case FIXED_CONVERT_EXPR
:
3130 r
= cxx_eval_unary_expression (ctx
, t
, addr
,
3131 non_constant_p
, overflow_p
);
3135 if (SIZEOF_EXPR_TYPE_P (t
))
3136 r
= cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t
, 0)),
3137 SIZEOF_EXPR
, false);
3138 else if (TYPE_P (TREE_OPERAND (t
, 0)))
3139 r
= cxx_sizeof_or_alignof_type (TREE_OPERAND (t
, 0), SIZEOF_EXPR
,
3142 r
= cxx_sizeof_or_alignof_expr (TREE_OPERAND (t
, 0), SIZEOF_EXPR
,
3144 if (r
== error_mark_node
)
3146 VERIFY_CONSTANT (r
);
3151 /* check_return_expr sometimes wraps a TARGET_EXPR in a
3152 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
3153 introduced by build_call_a. */
3154 tree op0
= TREE_OPERAND (t
, 0);
3155 tree op1
= TREE_OPERAND (t
, 1);
3157 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
3158 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
3159 r
= cxx_eval_constant_expression (ctx
, op0
,
3160 addr
, non_constant_p
, overflow_p
,
3164 /* Check that the LHS is constant and then discard it. */
3165 cxx_eval_constant_expression (ctx
, op0
,
3166 false, non_constant_p
, overflow_p
,
3168 op1
= TREE_OPERAND (t
, 1);
3169 r
= cxx_eval_constant_expression (ctx
, op1
,
3170 addr
, non_constant_p
, overflow_p
,
3176 case POINTER_PLUS_EXPR
:
3180 case TRUNC_DIV_EXPR
:
3182 case FLOOR_DIV_EXPR
:
3183 case ROUND_DIV_EXPR
:
3184 case TRUNC_MOD_EXPR
:
3186 case ROUND_MOD_EXPR
:
3188 case EXACT_DIV_EXPR
:
3198 case TRUTH_XOR_EXPR
:
3205 case UNORDERED_EXPR
:
3215 r
= cxx_eval_binary_expression (ctx
, t
, addr
,
3216 non_constant_p
, overflow_p
);
3219 /* fold can introduce non-IF versions of these; still treat them as
3220 short-circuiting. */
3221 case TRUTH_AND_EXPR
:
3222 case TRUTH_ANDIF_EXPR
:
3223 r
= cxx_eval_logical_expression (ctx
, t
, boolean_false_node
,
3226 non_constant_p
, overflow_p
);
3230 case TRUTH_ORIF_EXPR
:
3231 r
= cxx_eval_logical_expression (ctx
, t
, boolean_true_node
,
3234 non_constant_p
, overflow_p
);
3238 r
= cxx_eval_array_reference (ctx
, t
, addr
,
3239 non_constant_p
, overflow_p
);
3243 if (is_overloaded_fn (t
))
3245 /* We can only get here in checking mode via
3246 build_non_dependent_expr, because any expression that
3247 calls or takes the address of the function will have
3248 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
3249 gcc_checking_assert (ctx
->quiet
|| errorcount
);
3250 *non_constant_p
= true;
3253 r
= cxx_eval_component_reference (ctx
, t
, addr
,
3254 non_constant_p
, overflow_p
);
3258 r
= cxx_eval_bit_field_ref (ctx
, t
, addr
,
3259 non_constant_p
, overflow_p
);
3264 r
= cxx_eval_conditional_expression (ctx
, t
, addr
,
3265 non_constant_p
, overflow_p
,
3270 r
= cxx_eval_bare_aggregate (ctx
, t
, addr
,
3271 non_constant_p
, overflow_p
);
3275 /* We can get this in a defaulted constructor for a class with a
3276 non-static data member of array type. Either the initializer will
3277 be NULL, meaning default-initialization, or it will be an lvalue
3278 or xvalue of the same type, meaning direct-initialization from the
3279 corresponding member. */
3280 r
= cxx_eval_vec_init (ctx
, t
, addr
,
3281 non_constant_p
, overflow_p
);
3286 r
= cxx_eval_trinary_expression (ctx
, t
, addr
,
3287 non_constant_p
, overflow_p
);
3291 case VIEW_CONVERT_EXPR
:
3294 tree oldop
= TREE_OPERAND (t
, 0);
3295 tree op
= cxx_eval_constant_expression (ctx
, oldop
,
3297 non_constant_p
, overflow_p
);
3298 if (*non_constant_p
)
3300 if (POINTER_TYPE_P (TREE_TYPE (t
))
3301 && TREE_CODE (op
) == INTEGER_CST
3302 && !integer_zerop (op
))
3305 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3306 "reinterpret_cast from integer to pointer");
3307 *non_constant_p
= true;
3311 /* We didn't fold at the top so we could check for ptr-int
3314 r
= fold_build1 (TREE_CODE (t
), TREE_TYPE (t
), op
);
3315 /* Conversion of an out-of-range value has implementation-defined
3316 behavior; the language considers it different from arithmetic
3317 overflow, which is undefined. */
3318 if (TREE_OVERFLOW_P (r
) && !TREE_OVERFLOW_P (op
))
3319 TREE_OVERFLOW (r
) = false;
3323 case EMPTY_CLASS_EXPR
:
3324 /* This is good enough for a function argument that might not get
3325 used, and they can't do anything with it, so just return it. */
3328 case STATEMENT_LIST
:
3330 new_ctx
.ctor
= new_ctx
.object
= NULL_TREE
;
3331 return cxx_eval_statement_list (&new_ctx
, t
,
3332 non_constant_p
, overflow_p
, jump_target
);
3335 return cxx_eval_constant_expression (ctx
, BIND_EXPR_BODY (t
),
3337 non_constant_p
, overflow_p
,
3340 case PREINCREMENT_EXPR
:
3341 case POSTINCREMENT_EXPR
:
3342 case PREDECREMENT_EXPR
:
3343 case POSTDECREMENT_EXPR
:
3344 return cxx_eval_increment_expression (ctx
, t
,
3345 addr
, non_constant_p
, overflow_p
);
3351 case VEC_DELETE_EXPR
:
3354 /* GCC internal stuff. */
3357 case WITH_CLEANUP_EXPR
:
3358 case NON_DEPENDENT_EXPR
:
3362 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3363 "expression %qE is not a constant-expression", t
);
3364 *non_constant_p
= true;
3367 case PLACEHOLDER_EXPR
:
3368 if (!ctx
|| !ctx
->ctor
|| (addr
&& !ctx
->object
))
3370 /* A placeholder without a referent. We can get here when
3371 checking whether NSDMIs are noexcept, or in massage_init_elt;
3372 just say it's non-constant for now. */
3373 gcc_assert (ctx
->quiet
);
3374 *non_constant_p
= true;
3379 /* Use of the value or address of the current object. We could
3380 use ctx->object unconditionally, but using ctx->ctor when we
3381 can is a minor optimization. */
3382 tree ctor
= addr
? ctx
->object
: ctx
->ctor
;
3383 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3384 (TREE_TYPE (t
), TREE_TYPE (ctor
)));
3385 return cxx_eval_constant_expression
3387 non_constant_p
, overflow_p
);
3392 *jump_target
= TREE_OPERAND (t
, 0);
3393 gcc_assert (breaks (jump_target
) || continues (jump_target
));
3397 cxx_eval_loop_expr (ctx
, t
,
3398 non_constant_p
, overflow_p
, jump_target
);
3402 cxx_eval_switch_expr (ctx
, t
,
3403 non_constant_p
, overflow_p
, jump_target
);
3407 internal_error ("unexpected expression %qE of kind %s", t
,
3408 get_tree_code_name (TREE_CODE (t
)));
3409 *non_constant_p
= true;
3413 if (r
== error_mark_node
)
3414 *non_constant_p
= true;
3416 if (*non_constant_p
)
3423 cxx_eval_outermost_constant_expr (tree t
, bool allow_non_constant
,
3424 bool strict
= true, tree object
= NULL_TREE
)
3426 bool non_constant_p
= false;
3427 bool overflow_p
= false;
3428 constexpr_ctx ctx
= { NULL
, NULL
, NULL
, NULL
, allow_non_constant
, strict
};
3429 hash_map
<tree
,tree
> map
;
3431 tree type
= initialized_type (t
);
3433 if (AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
))
3435 /* In C++14 an NSDMI can participate in aggregate initialization,
3436 and can refer to the address of the object being initialized, so
3437 we need to pass in the relevant VAR_DECL if we want to do the
3438 evaluation in a single pass. The evaluation will dynamically
3439 update ctx.values for the VAR_DECL. We use the same strategy
3440 for C++11 constexpr constructors that refer to the object being
3442 ctx
.ctor
= build_constructor (type
, NULL
);
3443 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctx
.ctor
) = true;
3446 if (TREE_CODE (t
) == TARGET_EXPR
)
3447 object
= TARGET_EXPR_SLOT (t
);
3448 else if (TREE_CODE (t
) == AGGR_INIT_EXPR
)
3449 object
= AGGR_INIT_EXPR_SLOT (t
);
3451 ctx
.object
= object
;
3453 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3454 (type
, TREE_TYPE (object
)));
3455 if (object
&& DECL_P (object
))
3456 map
.put (object
, ctx
.ctor
);
3457 if (TREE_CODE (r
) == TARGET_EXPR
)
3458 /* Avoid creating another CONSTRUCTOR when we expand the
3460 r
= TARGET_EXPR_INITIAL (r
);
3463 r
= cxx_eval_constant_expression (&ctx
, r
,
3464 false, &non_constant_p
, &overflow_p
);
3466 verify_constant (r
, allow_non_constant
, &non_constant_p
, &overflow_p
);
3468 /* Mutable logic is a bit tricky: we want to allow initialization of
3469 constexpr variables with mutable members, but we can't copy those
3470 members to another constexpr variable. */
3471 if (TREE_CODE (r
) == CONSTRUCTOR
3472 && CONSTRUCTOR_MUTABLE_POISON (r
))
3474 if (!allow_non_constant
)
3475 error ("%qE is not a constant expression because it refers to "
3476 "mutable subobjects of %qT", t
, type
);
3477 non_constant_p
= true;
3480 /* Technically we should check this for all subexpressions, but that
3481 runs into problems with our internal representation of pointer
3482 subtraction and the 5.19 rules are still in flux. */
3483 if (CONVERT_EXPR_CODE_P (TREE_CODE (r
))
3484 && ARITHMETIC_TYPE_P (TREE_TYPE (r
))
3485 && TREE_CODE (TREE_OPERAND (r
, 0)) == ADDR_EXPR
)
3487 if (!allow_non_constant
)
3488 error ("conversion from pointer type %qT "
3489 "to arithmetic type %qT in a constant-expression",
3490 TREE_TYPE (TREE_OPERAND (r
, 0)), TREE_TYPE (r
));
3491 non_constant_p
= true;
3494 if (!non_constant_p
&& overflow_p
)
3495 non_constant_p
= true;
3497 if (non_constant_p
&& !allow_non_constant
)
3498 return error_mark_node
;
3499 else if (non_constant_p
&& TREE_CONSTANT (r
))
3501 /* This isn't actually constant, so unset TREE_CONSTANT. */
3504 else if (TREE_CODE (r
) == CONSTRUCTOR
)
3505 r
= build1 (VIEW_CONVERT_EXPR
, TREE_TYPE (r
), r
);
3507 r
= build_nop (TREE_TYPE (r
), r
);
3508 TREE_CONSTANT (r
) = false;
3510 else if (non_constant_p
|| r
== t
)
3513 if (TREE_CODE (r
) == CONSTRUCTOR
&& CLASS_TYPE_P (TREE_TYPE (r
)))
3515 if (TREE_CODE (t
) == TARGET_EXPR
3516 && TARGET_EXPR_INITIAL (t
) == r
)
3520 r
= get_target_expr (r
);
3521 TREE_CONSTANT (r
) = true;
3529 /* Returns true if T is a valid subexpression of a constant expression,
3530 even if it isn't itself a constant expression. */
3533 is_sub_constant_expr (tree t
)
3535 bool non_constant_p
= false;
3536 bool overflow_p
= false;
3537 constexpr_ctx ctx
= { NULL
, NULL
, NULL
, NULL
, true, true };
3538 hash_map
<tree
, tree
> map
;
3540 cxx_eval_constant_expression (&ctx
, t
, false, &non_constant_p
,
3542 return !non_constant_p
&& !overflow_p
;
3545 /* If T represents a constant expression returns its reduced value.
3546 Otherwise return error_mark_node. If T is dependent, then
3550 cxx_constant_value (tree t
, tree decl
)
3552 return cxx_eval_outermost_constant_expr (t
, false, true, decl
);
3555 /* If T is a constant expression, returns its reduced value.
3556 Otherwise, if T does not have TREE_CONSTANT set, returns T.
3557 Otherwise, returns a version of T without TREE_CONSTANT. */
3560 maybe_constant_value (tree t
, tree decl
)
3564 if (instantiation_dependent_expression_p (t
)
3565 || type_unknown_p (t
)
3566 || BRACE_ENCLOSED_INITIALIZER_P (t
)
3567 || !potential_constant_expression (t
))
3569 if (TREE_OVERFLOW_P (t
))
3571 t
= build_nop (TREE_TYPE (t
), t
);
3572 TREE_CONSTANT (t
) = false;
3577 r
= cxx_eval_outermost_constant_expr (t
, true, true, decl
);
3578 #ifdef ENABLE_CHECKING
3579 /* cp_tree_equal looks through NOPs, so allow them. */
3581 || CONVERT_EXPR_P (t
)
3582 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
3583 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
3584 || !cp_tree_equal (r
, t
));
3589 /* Like maybe_constant_value but first fully instantiate the argument.
3591 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
3592 (t, tf_none) followed by maybe_constant_value but is more efficient,
3593 because calls instantiation_dependent_expression_p and
3594 potential_constant_expression at most once. */
3597 fold_non_dependent_expr (tree t
)
3602 /* If we're in a template, but T isn't value dependent, simplify
3603 it. We're supposed to treat:
3605 template <typename T> void f(T[1 + 1]);
3606 template <typename T> void f(T[2]);
3608 as two declarations of the same function, for example. */
3609 if (processing_template_decl
)
3611 if (!instantiation_dependent_expression_p (t
)
3612 && potential_constant_expression (t
))
3614 processing_template_decl_sentinel s
;
3615 t
= instantiate_non_dependent_expr_internal (t
, tf_none
);
3617 if (type_unknown_p (t
)
3618 || BRACE_ENCLOSED_INITIALIZER_P (t
))
3620 if (TREE_OVERFLOW_P (t
))
3622 t
= build_nop (TREE_TYPE (t
), t
);
3623 TREE_CONSTANT (t
) = false;
3628 tree r
= cxx_eval_outermost_constant_expr (t
, true, true, NULL_TREE
);
3629 #ifdef ENABLE_CHECKING
3630 /* cp_tree_equal looks through NOPs, so allow them. */
3632 || CONVERT_EXPR_P (t
)
3633 || TREE_CODE (t
) == VIEW_CONVERT_EXPR
3634 || (TREE_CONSTANT (t
) && !TREE_CONSTANT (r
))
3635 || !cp_tree_equal (r
, t
));
3639 else if (TREE_OVERFLOW_P (t
))
3641 t
= build_nop (TREE_TYPE (t
), t
);
3642 TREE_CONSTANT (t
) = false;
3647 return maybe_constant_value (t
);
3650 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
3651 than wrapped in a TARGET_EXPR. */
3654 maybe_constant_init (tree t
, tree decl
)
3656 if (TREE_CODE (t
) == EXPR_STMT
)
3657 t
= TREE_OPERAND (t
, 0);
3658 if (TREE_CODE (t
) == CONVERT_EXPR
3659 && VOID_TYPE_P (TREE_TYPE (t
)))
3660 t
= TREE_OPERAND (t
, 0);
3661 if (TREE_CODE (t
) == INIT_EXPR
)
3662 t
= TREE_OPERAND (t
, 1);
3663 if (instantiation_dependent_expression_p (t
)
3664 || type_unknown_p (t
)
3665 || BRACE_ENCLOSED_INITIALIZER_P (t
)
3666 || !potential_static_init_expression (t
))
3667 /* Don't try to evaluate it. */;
3669 t
= cxx_eval_outermost_constant_expr (t
, true, false, decl
);
3670 if (TREE_CODE (t
) == TARGET_EXPR
)
3672 tree init
= TARGET_EXPR_INITIAL (t
);
3673 if (TREE_CODE (init
) == CONSTRUCTOR
)
3680 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
3681 /* Return true if the object referred to by REF has automatic or thread
3684 enum { ck_ok
, ck_bad
, ck_unknown
};
3686 check_automatic_or_tls (tree ref
)
3689 HOST_WIDE_INT bitsize
, bitpos
;
3691 int volatilep
= 0, unsignedp
= 0;
3692 tree decl
= get_inner_reference (ref
, &bitsize
, &bitpos
, &offset
,
3693 &mode
, &unsignedp
, &volatilep
, false);
3696 /* If there isn't a decl in the middle, we don't know the linkage here,
3697 and this isn't a constant expression anyway. */
3700 dk
= decl_storage_duration (decl
);
3701 return (dk
== dk_auto
|| dk
== dk_thread
) ? ck_bad
: ck_ok
;
3705 /* Return true if T denotes a potentially constant expression. Issue
3706 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
3707 an lvalue-rvalue conversion is implied.
3709 C++0x [expr.const] used to say
3711 6 An expression is a potential constant expression if it is
3712 a constant expression where all occurrences of function
3713 parameters are replaced by arbitrary constant expressions
3714 of the appropriate type.
3716 2 A conditional expression is a constant expression unless it
3717 involves one of the following as a potentially evaluated
3718 subexpression (3.2), but subexpressions of logical AND (5.14),
3719 logical OR (5.15), and conditional (5.16) operations that are
3720 not evaluated are not considered. */
3723 potential_constant_expression_1 (tree t
, bool want_rval
, bool strict
,
3724 tsubst_flags_t flags
)
3726 #define RECUR(T,RV) potential_constant_expression_1 ((T), (RV), strict, flags)
3727 enum { any
= false, rval
= true };
3731 if (t
== error_mark_node
)
3735 if (TREE_THIS_VOLATILE (t
))
3737 if (flags
& tf_error
)
3738 error ("expression %qE has side-effects", t
);
3741 if (CONSTANT_CLASS_P (t
))
3744 switch (TREE_CODE (t
))
3750 case TEMPLATE_ID_EXPR
:
3753 case CASE_LABEL_EXPR
:
3759 case TEMPLATE_PARM_INDEX
:
3761 case IDENTIFIER_NODE
:
3762 case USERDEF_LITERAL
:
3763 /* We can see a FIELD_DECL in a pointer-to-member expression. */
3768 case PLACEHOLDER_EXPR
:
3773 case AGGR_INIT_EXPR
:
3775 /* -- an invocation of a function other than a constexpr function
3776 or a constexpr constructor. */
3778 tree fun
= get_function_named_in_call (t
);
3779 const int nargs
= call_expr_nargs (t
);
3782 if (fun
== NULL_TREE
)
3784 /* fold_call_expr can't do anything with IFN calls. */
3785 if (flags
& tf_error
)
3786 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3787 "call to internal function");
3790 if (is_overloaded_fn (fun
))
3792 if (TREE_CODE (fun
) == FUNCTION_DECL
)
3794 if (builtin_valid_in_constant_expr_p (fun
))
3796 if (!DECL_DECLARED_CONSTEXPR_P (fun
)
3797 /* Allow any built-in function; if the expansion
3798 isn't constant, we'll deal with that then. */
3799 && !is_builtin_fn (fun
))
3801 if (flags
& tf_error
)
3803 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3804 "call to non-constexpr function %qD", fun
);
3805 explain_invalid_constexpr_fn (fun
);
3809 /* A call to a non-static member function takes the address
3810 of the object as the first argument. But in a constant
3811 expression the address will be folded away, so look
3813 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun
)
3814 && !DECL_CONSTRUCTOR_P (fun
))
3816 tree x
= get_nth_callarg (t
, 0);
3817 if (is_this_parameter (x
))
3819 else if (!RECUR (x
, rval
))
3826 if (!RECUR (fun
, true))
3828 fun
= get_first_fn (fun
);
3830 /* Skip initial arguments to base constructors. */
3831 if (DECL_BASE_CONSTRUCTOR_P (fun
))
3832 i
= num_artificial_parms_for (fun
);
3833 fun
= DECL_ORIGIN (fun
);
3837 if (RECUR (fun
, rval
))
3838 /* Might end up being a constant function pointer. */;
3842 for (; i
< nargs
; ++i
)
3844 tree x
= get_nth_callarg (t
, i
);
3845 if (!RECUR (x
, rval
))
3851 case NON_LVALUE_EXPR
:
3852 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
3853 -- an lvalue of integral type that refers to a non-volatile
3854 const variable or static data member initialized with
3855 constant expressions, or
3857 -- an lvalue of literal type that refers to non-volatile
3858 object defined with constexpr, or that refers to a
3859 sub-object of such an object; */
3860 return RECUR (TREE_OPERAND (t
, 0), rval
);
3864 && !decl_constant_var_p (t
)
3866 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t
))
3867 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t
))
3868 && !var_in_constexpr_fn (t
)
3869 && !dependent_type_p (TREE_TYPE (t
)))
3871 if (flags
& tf_error
)
3872 non_const_var_error (t
);
3879 case VIEW_CONVERT_EXPR
:
3880 /* -- a reinterpret_cast. FIXME not implemented, and this rule
3881 may change to something more specific to type-punning (DR 1312). */
3883 tree from
= TREE_OPERAND (t
, 0);
3884 if (POINTER_TYPE_P (TREE_TYPE (t
))
3885 && TREE_CODE (from
) == INTEGER_CST
3886 && !integer_zerop (from
))
3888 if (flags
& tf_error
)
3889 error_at (EXPR_LOC_OR_LOC (t
, input_location
),
3890 "reinterpret_cast from integer to pointer");
3893 return (RECUR (from
, TREE_CODE (t
) != VIEW_CONVERT_EXPR
));
3897 /* -- a unary operator & that is applied to an lvalue that
3898 designates an object with thread or automatic storage
3900 t
= TREE_OPERAND (t
, 0);
3902 if (TREE_CODE (t
) == OFFSET_REF
&& PTRMEM_OK_P (t
))
3903 /* A pointer-to-member constant. */
3907 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
3908 any checking here, as we might dereference the pointer later. If
3909 we remove this code, also remove check_automatic_or_tls. */
3910 i
= check_automatic_or_tls (t
);
3915 if (flags
& tf_error
)
3916 error ("address-of an object %qE with thread local or "
3917 "automatic storage is not a constant expression", t
);
3921 return RECUR (t
, any
);
3927 /* -- a class member access unless its postfix-expression is
3928 of literal type or of pointer to literal type. */
3929 /* This test would be redundant, as it follows from the
3930 postfix-expression being a potential constant expression. */
3931 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
3933 case EXPR_PACK_EXPANSION
:
3934 return RECUR (PACK_EXPANSION_PATTERN (t
), want_rval
);
3938 tree x
= TREE_OPERAND (t
, 0);
3940 if (is_this_parameter (x
))
3942 if (DECL_CONTEXT (x
)
3943 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x
)))
3945 if (flags
& tf_error
)
3946 error ("use of %<this%> in a constant expression");
3951 return RECUR (x
, rval
);
3954 case STATEMENT_LIST
:
3956 tree_stmt_iterator i
;
3957 for (i
= tsi_start (t
); !tsi_end_p (i
); tsi_next (&i
))
3959 if (!RECUR (tsi_stmt (i
), any
))
3967 if (cxx_dialect
< cxx14
)
3969 if (!RECUR (TREE_OPERAND (t
, 0), any
))
3971 if (!RECUR (TREE_OPERAND (t
, 1), rval
))
3976 if (cxx_dialect
< cxx14
)
3978 if (!RECUR (TREE_OPERAND (t
, 0), rval
))
3980 if (!RECUR (TREE_OPERAND (t
, 2), rval
))
3985 if (!RECUR (IF_COND (t
), rval
))
3987 if (!RECUR (THEN_CLAUSE (t
), any
))
3989 if (!RECUR (ELSE_CLAUSE (t
), any
))
3994 if (!RECUR (DO_COND (t
), rval
))
3996 if (!RECUR (DO_BODY (t
), any
))
4001 if (!RECUR (FOR_INIT_STMT (t
), any
))
4003 if (!RECUR (FOR_COND (t
), rval
))
4005 if (!RECUR (FOR_EXPR (t
), any
))
4007 if (!RECUR (FOR_BODY (t
), any
))
4012 if (!RECUR (WHILE_COND (t
), rval
))
4014 if (!RECUR (WHILE_BODY (t
), any
))
4019 if (!RECUR (SWITCH_STMT_COND (t
), rval
))
4021 if (!RECUR (SWITCH_STMT_BODY (t
), any
))
4026 return RECUR (STMT_EXPR_STMT (t
), rval
);
4029 case DYNAMIC_CAST_EXPR
:
4030 case PSEUDO_DTOR_EXPR
:
4034 case VEC_DELETE_EXPR
:
4037 case OMP_ATOMIC_READ
:
4038 case OMP_ATOMIC_CAPTURE_OLD
:
4039 case OMP_ATOMIC_CAPTURE_NEW
:
4040 /* GCC internal stuff. */
4043 case TRANSACTION_EXPR
:
4046 if (flags
& tf_error
)
4047 error ("expression %qE is not a constant-expression", t
);
4051 /* -- a typeid expression whose operand is of polymorphic
4054 tree e
= TREE_OPERAND (t
, 0);
4055 if (!TYPE_P (e
) && !type_dependent_expression_p (e
)
4056 && TYPE_POLYMORPHIC_P (TREE_TYPE (e
)))
4058 if (flags
& tf_error
)
4059 error ("typeid-expression is not a constant expression "
4060 "because %qE is of polymorphic type", e
);
4067 /* -- a subtraction where both operands are pointers. */
4068 if (TYPE_PTR_P (TREE_OPERAND (t
, 0))
4069 && TYPE_PTR_P (TREE_OPERAND (t
, 1)))
4071 if (flags
& tf_error
)
4072 error ("difference of two pointer expressions is not "
4073 "a constant expression");
4085 /* -- a relational or equality operator where at least
4086 one of the operands is a pointer. */
4087 if (TYPE_PTR_P (TREE_OPERAND (t
, 0))
4088 || TYPE_PTR_P (TREE_OPERAND (t
, 1)))
4090 if (flags
& tf_error
)
4091 error ("pointer comparison expression is not a "
4092 "constant expression");
4098 case PREINCREMENT_EXPR
:
4099 case POSTINCREMENT_EXPR
:
4100 case PREDECREMENT_EXPR
:
4101 case POSTDECREMENT_EXPR
:
4102 if (cxx_dialect
< cxx14
)
4108 if (TYPE_P (TREE_OPERAND (t
, 0)))
4110 /* else fall through. */
4116 case FIX_TRUNC_EXPR
:
4120 case TRUTH_NOT_EXPR
:
4121 case FIXED_CONVERT_EXPR
:
4122 case UNARY_PLUS_EXPR
:
4124 return RECUR (TREE_OPERAND (t
, 0), rval
);
4127 case CONST_CAST_EXPR
:
4128 case STATIC_CAST_EXPR
:
4129 case REINTERPRET_CAST_EXPR
:
4130 case IMPLICIT_CONV_EXPR
:
4131 if (cxx_dialect
< cxx11
4132 && !dependent_type_p (TREE_TYPE (t
))
4133 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t
)))
4134 /* In C++98, a conversion to non-integral type can't be part of a
4135 constant expression. */
4137 if (flags
& tf_error
)
4138 error ("cast to non-integral type %qT in a constant expression",
4143 return (RECUR (TREE_OPERAND (t
, 0),
4144 TREE_CODE (TREE_TYPE (t
)) != REFERENCE_TYPE
));
4147 return RECUR (BIND_EXPR_BODY (t
), want_rval
);
4149 case WITH_CLEANUP_EXPR
:
4150 case CLEANUP_POINT_EXPR
:
4151 case MUST_NOT_THROW_EXPR
:
4152 case TRY_CATCH_EXPR
:
4157 case NON_DEPENDENT_EXPR
:
4158 /* For convenience. */
4160 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
4163 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
4166 if (!literal_type_p (TREE_TYPE (t
)))
4168 if (flags
& tf_error
)
4170 error ("temporary of non-literal type %qT in a "
4171 "constant expression", TREE_TYPE (t
));
4172 explain_non_literal_class (TREE_TYPE (t
));
4177 return RECUR (TREE_OPERAND (t
, 1), rval
);
4181 vec
<constructor_elt
, va_gc
> *v
= CONSTRUCTOR_ELTS (t
);
4182 constructor_elt
*ce
;
4183 for (i
= 0; vec_safe_iterate (v
, i
, &ce
); ++i
)
4184 if (!RECUR (ce
->value
, want_rval
))
4191 gcc_assert (TREE_PURPOSE (t
) == NULL_TREE
4192 || DECL_P (TREE_PURPOSE (t
)));
4193 if (!RECUR (TREE_VALUE (t
), want_rval
))
4195 if (TREE_CHAIN (t
) == NULL_TREE
)
4197 return RECUR (TREE_CHAIN (t
), want_rval
);
4200 case TRUNC_DIV_EXPR
:
4202 case FLOOR_DIV_EXPR
:
4203 case ROUND_DIV_EXPR
:
4204 case TRUNC_MOD_EXPR
:
4206 case ROUND_MOD_EXPR
:
4208 tree denom
= TREE_OPERAND (t
, 1);
4209 if (!RECUR (denom
, rval
))
4211 /* We can't call cxx_eval_outermost_constant_expr on an expression
4212 that hasn't been through instantiate_non_dependent_expr yet. */
4213 if (!processing_template_decl
)
4214 denom
= cxx_eval_outermost_constant_expr (denom
, true);
4215 if (integer_zerop (denom
))
4217 if (flags
& tf_error
)
4218 error ("division by zero is not a constant-expression");
4224 return RECUR (TREE_OPERAND (t
, 0), want_rval
);
4230 /* check_return_expr sometimes wraps a TARGET_EXPR in a
4231 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
4232 introduced by build_call_a. */
4233 tree op0
= TREE_OPERAND (t
, 0);
4234 tree op1
= TREE_OPERAND (t
, 1);
4236 if ((TREE_CODE (op0
) == TARGET_EXPR
&& op1
== TARGET_EXPR_SLOT (op0
))
4237 || TREE_CODE (op1
) == EMPTY_CLASS_EXPR
)
4238 return RECUR (op0
, want_rval
);
4243 /* If the first operand is the non-short-circuit constant, look at
4244 the second operand; otherwise we only care about the first one for
4246 case TRUTH_AND_EXPR
:
4247 case TRUTH_ANDIF_EXPR
:
4248 tmp
= boolean_true_node
;
4251 case TRUTH_ORIF_EXPR
:
4252 tmp
= boolean_false_node
;
4255 tree op
= TREE_OPERAND (t
, 0);
4256 if (!RECUR (op
, rval
))
4258 if (!processing_template_decl
)
4259 op
= cxx_eval_outermost_constant_expr (op
, true);
4260 if (tree_int_cst_equal (op
, tmp
))
4261 return RECUR (TREE_OPERAND (t
, 1), rval
);
4268 case POINTER_PLUS_EXPR
:
4270 case EXACT_DIV_EXPR
:
4280 case TRUTH_XOR_EXPR
:
4281 case UNORDERED_EXPR
:
4294 case ARRAY_RANGE_REF
:
4298 for (i
= 0; i
< 2; ++i
)
4299 if (!RECUR (TREE_OPERAND (t
, i
), want_rval
))
4303 case CILK_SYNC_STMT
:
4304 case CILK_SPAWN_STMT
:
4305 case ARRAY_NOTATION_REF
:
4310 for (i
= 0; i
< 3; ++i
)
4311 if (!RECUR (TREE_OPERAND (t
, i
), true))
4317 /* If the condition is a known constant, we know which of the legs we
4318 care about; otherwise we only require that the condition and
4319 either of the legs be potentially constant. */
4320 tmp
= TREE_OPERAND (t
, 0);
4321 if (!RECUR (tmp
, rval
))
4323 if (!processing_template_decl
)
4324 tmp
= cxx_eval_outermost_constant_expr (tmp
, true);
4325 if (integer_zerop (tmp
))
4326 return RECUR (TREE_OPERAND (t
, 2), want_rval
);
4327 else if (TREE_CODE (tmp
) == INTEGER_CST
)
4328 return RECUR (TREE_OPERAND (t
, 1), want_rval
);
4329 for (i
= 1; i
< 3; ++i
)
4330 if (potential_constant_expression_1 (TREE_OPERAND (t
, i
),
4331 want_rval
, strict
, tf_none
))
4333 if (flags
& tf_error
)
4334 error ("expression %qE is not a constant-expression", t
);
4338 if (VEC_INIT_EXPR_IS_CONSTEXPR (t
))
4340 if (flags
& tf_error
)
4342 error ("non-constant array initialization");
4343 diagnose_non_constexpr_vec_init (t
);
4348 if (objc_is_property_ref (t
))
4351 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t
)));
4358 /* The main entry point to the above. */
4361 potential_constant_expression (tree t
)
4363 return potential_constant_expression_1 (t
, false, true, tf_none
);
4367 potential_static_init_expression (tree t
)
4369 return potential_constant_expression_1 (t
, false, false, tf_none
);
4372 /* As above, but require a constant rvalue. */
4375 potential_rvalue_constant_expression (tree t
)
4377 return potential_constant_expression_1 (t
, true, true, tf_none
);
4380 /* Like above, but complain about non-constant expressions. */
4383 require_potential_constant_expression (tree t
)
4385 return potential_constant_expression_1 (t
, false, true, tf_warning_or_error
);
4388 /* Cross product of the above. */
4391 require_potential_rvalue_constant_expression (tree t
)
4393 return potential_constant_expression_1 (t
, true, true, tf_warning_or_error
);
4396 #include "gt-cp-constexpr.h"