* config/i386/i386.c (ix86_legitimize_address): Declare
[official-gcc.git] / gcc / cp / constexpr.c
blobafbcf51fec127f00009d6e31114e0ef29d6a6bf3
1 /* Perform -*- C++ -*- constant expression evaluation, including calls to
2 constexpr functions. These routines are used both during actual parsing
3 and during the instantiation of template functions.
5 Copyright (C) 1998-2014 Free Software Foundation, Inc.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tree.h"
27 #include "varasm.h"
28 #include "cp-tree.h"
29 #include "c-family/c-objc.h"
30 #include "tree-iterator.h"
31 #include "gimplify.h"
32 #include "builtins.h"
33 #include "tree-inline.h"
34 #include "ubsan.h"
36 static bool verify_constant (tree, bool, bool *, bool *);
37 #define VERIFY_CONSTANT(X) \
38 do { \
39 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
40 return t; \
41 } while (0)
43 /* Returns true iff FUN is an instantiation of a constexpr function
44 template or a defaulted constexpr function. */
46 bool
47 is_instantiation_of_constexpr (tree fun)
49 return ((DECL_TEMPLOID_INSTANTIATION (fun)
50 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
51 || (DECL_DEFAULTED_FN (fun)
52 && DECL_DECLARED_CONSTEXPR_P (fun)));
55 /* Return true if T is a literal type. */
57 bool
58 literal_type_p (tree t)
60 if (SCALAR_TYPE_P (t)
61 || TREE_CODE (t) == VECTOR_TYPE
62 || TREE_CODE (t) == REFERENCE_TYPE
63 || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
64 return true;
65 if (CLASS_TYPE_P (t))
67 t = complete_type (t);
68 gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
69 return CLASSTYPE_LITERAL_P (t);
71 if (TREE_CODE (t) == ARRAY_TYPE)
72 return literal_type_p (strip_array_types (t));
73 return false;
76 /* If DECL is a variable declared `constexpr', require its type
77 be literal. Return the DECL if OK, otherwise NULL. */
79 tree
80 ensure_literal_type_for_constexpr_object (tree decl)
82 tree type = TREE_TYPE (decl);
83 if (VAR_P (decl)
84 && (DECL_DECLARED_CONSTEXPR_P (decl)
85 || var_in_constexpr_fn (decl))
86 && !processing_template_decl)
88 tree stype = strip_array_types (type);
89 if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype)))
90 /* Don't complain here, we'll complain about incompleteness
91 when we try to initialize the variable. */;
92 else if (!literal_type_p (type))
94 if (DECL_DECLARED_CONSTEXPR_P (decl))
95 error ("the type %qT of constexpr variable %qD is not literal",
96 type, decl);
97 else
99 error ("variable %qD of non-literal type %qT in %<constexpr%> "
100 "function", decl, type);
101 cp_function_chain->invalid_constexpr = true;
103 explain_non_literal_class (type);
104 return NULL;
107 return decl;
110 /* Representation of entries in the constexpr function definition table. */
112 struct GTY((for_user)) constexpr_fundef {
113 tree decl;
114 tree body;
117 struct constexpr_fundef_hasher : ggc_hasher<constexpr_fundef *>
119 static hashval_t hash (constexpr_fundef *);
120 static bool equal (constexpr_fundef *, constexpr_fundef *);
123 /* This table holds all constexpr function definitions seen in
124 the current translation unit. */
126 static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table;
128 /* Utility function used for managing the constexpr function table.
129 Return true if the entries pointed to by P and Q are for the
130 same constexpr function. */
132 inline bool
133 constexpr_fundef_hasher::equal (constexpr_fundef *lhs, constexpr_fundef *rhs)
135 return lhs->decl == rhs->decl;
138 /* Utility function used for managing the constexpr function table.
139 Return a hash value for the entry pointed to by Q. */
141 inline hashval_t
142 constexpr_fundef_hasher::hash (constexpr_fundef *fundef)
144 return DECL_UID (fundef->decl);
147 /* Return a previously saved definition of function FUN. */
149 static constexpr_fundef *
150 retrieve_constexpr_fundef (tree fun)
152 constexpr_fundef fundef = { NULL, NULL };
153 if (constexpr_fundef_table == NULL)
154 return NULL;
156 fundef.decl = fun;
157 return constexpr_fundef_table->find (&fundef);
160 /* Check whether the parameter and return types of FUN are valid for a
161 constexpr function, and complain if COMPLAIN. */
163 static bool
164 is_valid_constexpr_fn (tree fun, bool complain)
166 bool ret = true;
168 if (DECL_INHERITED_CTOR_BASE (fun)
169 && TREE_CODE (fun) == TEMPLATE_DECL)
171 ret = false;
172 if (complain)
173 error ("inherited constructor %qD is not constexpr",
174 get_inherited_ctor (fun));
176 else
178 for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
179 parm != NULL_TREE; parm = TREE_CHAIN (parm))
180 if (!literal_type_p (TREE_TYPE (parm)))
182 ret = false;
183 if (complain)
185 error ("invalid type for parameter %d of constexpr "
186 "function %q+#D", DECL_PARM_INDEX (parm), fun);
187 explain_non_literal_class (TREE_TYPE (parm));
192 if (!DECL_CONSTRUCTOR_P (fun))
194 tree rettype = TREE_TYPE (TREE_TYPE (fun));
195 if (!literal_type_p (rettype))
197 ret = false;
198 if (complain)
200 error ("invalid return type %qT of constexpr function %q+D",
201 rettype, fun);
202 explain_non_literal_class (rettype);
206 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
207 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
209 ret = false;
210 if (complain)
212 error ("enclosing class of constexpr non-static member "
213 "function %q+#D is not a literal type", fun);
214 explain_non_literal_class (DECL_CONTEXT (fun));
218 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
220 ret = false;
221 if (complain)
222 error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
225 return ret;
228 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
229 for a member of an anonymous aggregate, INIT is the initializer for that
230 member, and VEC_OUTER is the vector of constructor elements for the class
231 whose constructor we are processing. Add the initializer to the vector
232 and return true to indicate success. */
234 static bool
235 build_anon_member_initialization (tree member, tree init,
236 vec<constructor_elt, va_gc> **vec_outer)
238 /* MEMBER presents the relevant fields from the inside out, but we need
239 to build up the initializer from the outside in so that we can reuse
240 previously built CONSTRUCTORs if this is, say, the second field in an
241 anonymous struct. So we use a vec as a stack. */
242 auto_vec<tree, 2> fields;
245 fields.safe_push (TREE_OPERAND (member, 1));
246 member = TREE_OPERAND (member, 0);
248 while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
249 && TREE_CODE (member) == COMPONENT_REF);
251 /* VEC has the constructor elements vector for the context of FIELD.
252 If FIELD is an anonymous aggregate, we will push inside it. */
253 vec<constructor_elt, va_gc> **vec = vec_outer;
254 tree field;
255 while (field = fields.pop(),
256 ANON_AGGR_TYPE_P (TREE_TYPE (field)))
258 tree ctor;
259 /* If there is already an outer constructor entry for the anonymous
260 aggregate FIELD, use it; otherwise, insert one. */
261 if (vec_safe_is_empty (*vec)
262 || (*vec)->last().index != field)
264 ctor = build_constructor (TREE_TYPE (field), NULL);
265 CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
267 else
268 ctor = (*vec)->last().value;
269 vec = &CONSTRUCTOR_ELTS (ctor);
272 /* Now we're at the innermost field, the one that isn't an anonymous
273 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
274 gcc_assert (fields.is_empty());
275 CONSTRUCTOR_APPEND_ELT (*vec, field, init);
277 return true;
280 /* Subroutine of build_constexpr_constructor_member_initializers.
281 The expression tree T represents a data member initialization
282 in a (constexpr) constructor definition. Build a pairing of
283 the data member with its initializer, and prepend that pair
284 to the existing initialization pair INITS. */
286 static bool
287 build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
289 tree member, init;
290 if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
291 t = TREE_OPERAND (t, 0);
292 if (TREE_CODE (t) == EXPR_STMT)
293 t = TREE_OPERAND (t, 0);
294 if (t == error_mark_node)
295 return false;
296 if (TREE_CODE (t) == STATEMENT_LIST)
298 tree_stmt_iterator i;
299 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
301 if (! build_data_member_initialization (tsi_stmt (i), vec))
302 return false;
304 return true;
306 if (TREE_CODE (t) == CLEANUP_STMT)
308 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
309 but we can in a constexpr constructor for a non-literal class. Just
310 ignore it; either all the initialization will be constant, in which
311 case the cleanup can't run, or it can't be constexpr.
312 Still recurse into CLEANUP_BODY. */
313 return build_data_member_initialization (CLEANUP_BODY (t), vec);
315 if (TREE_CODE (t) == CONVERT_EXPR)
316 t = TREE_OPERAND (t, 0);
317 if (TREE_CODE (t) == INIT_EXPR
318 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
319 use what this function builds for cx_check_missing_mem_inits, and
320 assignment in the ctor body doesn't count. */
321 || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR))
323 member = TREE_OPERAND (t, 0);
324 init = break_out_target_exprs (TREE_OPERAND (t, 1));
326 else if (TREE_CODE (t) == CALL_EXPR)
328 tree fn = get_callee_fndecl (t);
329 if (!fn || !DECL_CONSTRUCTOR_P (fn))
330 /* We're only interested in calls to subobject constructors. */
331 return true;
332 member = CALL_EXPR_ARG (t, 0);
333 /* We don't use build_cplus_new here because it complains about
334 abstract bases. Leaving the call unwrapped means that it has the
335 wrong type, but cxx_eval_constant_expression doesn't care. */
336 init = break_out_target_exprs (t);
338 else if (TREE_CODE (t) == BIND_EXPR)
339 return build_data_member_initialization (BIND_EXPR_BODY (t), vec);
340 else
341 /* Don't add anything else to the CONSTRUCTOR. */
342 return true;
343 if (INDIRECT_REF_P (member))
344 member = TREE_OPERAND (member, 0);
345 if (TREE_CODE (member) == NOP_EXPR)
347 tree op = member;
348 STRIP_NOPS (op);
349 if (TREE_CODE (op) == ADDR_EXPR)
351 gcc_assert (same_type_ignoring_top_level_qualifiers_p
352 (TREE_TYPE (TREE_TYPE (op)),
353 TREE_TYPE (TREE_TYPE (member))));
354 /* Initializing a cv-qualified member; we need to look through
355 the const_cast. */
356 member = op;
358 else if (op == current_class_ptr
359 && (same_type_ignoring_top_level_qualifiers_p
360 (TREE_TYPE (TREE_TYPE (member)),
361 current_class_type)))
362 /* Delegating constructor. */
363 member = op;
364 else
366 /* This is an initializer for an empty base; keep it for now so
367 we can check it in cxx_eval_bare_aggregate. */
368 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
371 if (TREE_CODE (member) == ADDR_EXPR)
372 member = TREE_OPERAND (member, 0);
373 if (TREE_CODE (member) == COMPONENT_REF)
375 tree aggr = TREE_OPERAND (member, 0);
376 if (TREE_CODE (aggr) != COMPONENT_REF)
377 /* Normal member initialization. */
378 member = TREE_OPERAND (member, 1);
379 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
380 /* Initializing a member of an anonymous union. */
381 return build_anon_member_initialization (member, init, vec);
382 else
383 /* We're initializing a vtable pointer in a base. Leave it as
384 COMPONENT_REF so we remember the path to get to the vfield. */
385 gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
388 CONSTRUCTOR_APPEND_ELT (*vec, member, init);
389 return true;
392 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
393 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
394 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
396 static bool
397 check_constexpr_bind_expr_vars (tree t)
399 gcc_assert (TREE_CODE (t) == BIND_EXPR);
401 for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
402 if (TREE_CODE (var) == TYPE_DECL
403 && DECL_IMPLICIT_TYPEDEF_P (var))
404 return false;
405 return true;
408 /* Subroutine of check_constexpr_ctor_body. */
410 static bool
411 check_constexpr_ctor_body_1 (tree last, tree list)
413 switch (TREE_CODE (list))
415 case DECL_EXPR:
416 if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL)
417 return true;
418 return false;
420 case CLEANUP_POINT_EXPR:
421 return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
422 /*complain=*/false);
424 case BIND_EXPR:
425 if (!check_constexpr_bind_expr_vars (list)
426 || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
427 /*complain=*/false))
428 return false;
429 return true;
431 case USING_STMT:
432 case STATIC_ASSERT:
433 return true;
435 default:
436 return false;
440 /* Make sure that there are no statements after LAST in the constructor
441 body represented by LIST. */
443 bool
444 check_constexpr_ctor_body (tree last, tree list, bool complain)
446 /* C++14 doesn't require a constexpr ctor to have an empty body. */
447 if (cxx_dialect >= cxx14)
448 return true;
450 bool ok = true;
451 if (TREE_CODE (list) == STATEMENT_LIST)
453 tree_stmt_iterator i = tsi_last (list);
454 for (; !tsi_end_p (i); tsi_prev (&i))
456 tree t = tsi_stmt (i);
457 if (t == last)
458 break;
459 if (!check_constexpr_ctor_body_1 (last, t))
461 ok = false;
462 break;
466 else if (list != last
467 && !check_constexpr_ctor_body_1 (last, list))
468 ok = false;
469 if (!ok)
471 if (complain)
472 error ("constexpr constructor does not have empty body");
473 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
475 return ok;
478 /* V is a vector of constructor elements built up for the base and member
479 initializers of a constructor for TYPE. They need to be in increasing
480 offset order, which they might not be yet if TYPE has a primary base
481 which is not first in the base-clause or a vptr and at least one base
482 all of which are non-primary. */
484 static vec<constructor_elt, va_gc> *
485 sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
487 tree pri = CLASSTYPE_PRIMARY_BINFO (type);
488 tree field_type;
489 unsigned i;
490 constructor_elt *ce;
492 if (pri)
493 field_type = BINFO_TYPE (pri);
494 else if (TYPE_CONTAINS_VPTR_P (type))
495 field_type = vtbl_ptr_type_node;
496 else
497 return v;
499 /* Find the element for the primary base or vptr and move it to the
500 beginning of the vec. */
501 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
502 if (TREE_TYPE (ce->index) == field_type)
503 break;
505 if (i > 0 && i < vec_safe_length (v))
507 vec<constructor_elt, va_gc> &vref = *v;
508 constructor_elt elt = vref[i];
509 for (; i > 0; --i)
510 vref[i] = vref[i-1];
511 vref[0] = elt;
514 return v;
517 /* Build compile-time evalable representations of member-initializer list
518 for a constexpr constructor. */
520 static tree
521 build_constexpr_constructor_member_initializers (tree type, tree body)
523 vec<constructor_elt, va_gc> *vec = NULL;
524 bool ok = true;
525 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR
526 || TREE_CODE (body) == EH_SPEC_BLOCK)
527 body = TREE_OPERAND (body, 0);
528 if (TREE_CODE (body) == STATEMENT_LIST)
529 body = STATEMENT_LIST_HEAD (body)->stmt;
530 body = BIND_EXPR_BODY (body);
531 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
533 body = TREE_OPERAND (body, 0);
534 if (TREE_CODE (body) == EXPR_STMT)
535 body = TREE_OPERAND (body, 0);
536 if (TREE_CODE (body) == INIT_EXPR
537 && (same_type_ignoring_top_level_qualifiers_p
538 (TREE_TYPE (TREE_OPERAND (body, 0)),
539 current_class_type)))
541 /* Trivial copy. */
542 return TREE_OPERAND (body, 1);
544 ok = build_data_member_initialization (body, &vec);
546 else if (TREE_CODE (body) == STATEMENT_LIST)
548 tree_stmt_iterator i;
549 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
551 ok = build_data_member_initialization (tsi_stmt (i), &vec);
552 if (!ok)
553 break;
556 else if (TREE_CODE (body) == TRY_BLOCK)
558 error ("body of %<constexpr%> constructor cannot be "
559 "a function-try-block");
560 return error_mark_node;
562 else if (EXPR_P (body))
563 ok = build_data_member_initialization (body, &vec);
564 else
565 gcc_assert (errorcount > 0);
566 if (ok)
568 if (vec_safe_length (vec) > 0)
570 /* In a delegating constructor, return the target. */
571 constructor_elt *ce = &(*vec)[0];
572 if (ce->index == current_class_ptr)
574 body = ce->value;
575 vec_free (vec);
576 return body;
579 vec = sort_constexpr_mem_initializers (type, vec);
580 return build_constructor (type, vec);
582 else
583 return error_mark_node;
586 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
587 declared to be constexpr, or a sub-statement thereof. Returns the
588 return value if suitable, error_mark_node for a statement not allowed in
589 a constexpr function, or NULL_TREE if no return value was found. */
591 static tree
592 constexpr_fn_retval (tree body)
594 switch (TREE_CODE (body))
596 case STATEMENT_LIST:
598 tree_stmt_iterator i;
599 tree expr = NULL_TREE;
600 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
602 tree s = constexpr_fn_retval (tsi_stmt (i));
603 if (s == error_mark_node)
604 return error_mark_node;
605 else if (s == NULL_TREE)
606 /* Keep iterating. */;
607 else if (expr)
608 /* Multiple return statements. */
609 return error_mark_node;
610 else
611 expr = s;
613 return expr;
616 case RETURN_EXPR:
617 return break_out_target_exprs (TREE_OPERAND (body, 0));
619 case DECL_EXPR:
621 tree decl = DECL_EXPR_DECL (body);
622 if (TREE_CODE (decl) == USING_DECL
623 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
624 || DECL_ARTIFICIAL (decl))
625 return NULL_TREE;
626 return error_mark_node;
629 case CLEANUP_POINT_EXPR:
630 return constexpr_fn_retval (TREE_OPERAND (body, 0));
632 case BIND_EXPR:
633 if (!check_constexpr_bind_expr_vars (body))
634 return error_mark_node;
635 return constexpr_fn_retval (BIND_EXPR_BODY (body));
637 case USING_STMT:
638 return NULL_TREE;
640 default:
641 return error_mark_node;
645 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
646 FUN; do the necessary transformations to turn it into a single expression
647 that we can store in the hash table. */
649 static tree
650 massage_constexpr_body (tree fun, tree body)
652 if (DECL_CONSTRUCTOR_P (fun))
653 body = build_constexpr_constructor_member_initializers
654 (DECL_CONTEXT (fun), body);
655 else if (cxx_dialect < cxx14)
657 if (TREE_CODE (body) == EH_SPEC_BLOCK)
658 body = EH_SPEC_STMTS (body);
659 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
660 body = TREE_OPERAND (body, 0);
661 body = constexpr_fn_retval (body);
663 return body;
666 /* FUN is a constexpr constructor with massaged body BODY. Return true
667 if some bases/fields are uninitialized, and complain if COMPLAIN. */
669 static bool
670 cx_check_missing_mem_inits (tree fun, tree body, bool complain)
672 bool bad;
673 tree field;
674 unsigned i, nelts;
675 tree ctype;
677 if (TREE_CODE (body) != CONSTRUCTOR)
678 return false;
680 nelts = CONSTRUCTOR_NELTS (body);
681 ctype = DECL_CONTEXT (fun);
682 field = TYPE_FIELDS (ctype);
684 if (TREE_CODE (ctype) == UNION_TYPE)
686 if (nelts == 0 && next_initializable_field (field))
688 if (complain)
689 error ("%<constexpr%> constructor for union %qT must "
690 "initialize exactly one non-static data member", ctype);
691 return true;
693 return false;
696 bad = false;
697 for (i = 0; i <= nelts; ++i)
699 tree index;
700 if (i == nelts)
701 index = NULL_TREE;
702 else
704 index = CONSTRUCTOR_ELT (body, i)->index;
705 /* Skip base and vtable inits. */
706 if (TREE_CODE (index) != FIELD_DECL
707 || DECL_ARTIFICIAL (index))
708 continue;
710 for (; field != index; field = DECL_CHAIN (field))
712 tree ftype;
713 if (TREE_CODE (field) != FIELD_DECL
714 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
715 || DECL_ARTIFICIAL (field))
716 continue;
717 ftype = strip_array_types (TREE_TYPE (field));
718 if (type_has_constexpr_default_constructor (ftype))
720 /* It's OK to skip a member with a trivial constexpr ctor.
721 A constexpr ctor that isn't trivial should have been
722 added in by now. */
723 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
724 || errorcount != 0);
725 continue;
727 if (!complain)
728 return true;
729 error ("member %qD must be initialized by mem-initializer "
730 "in %<constexpr%> constructor", field);
731 inform (DECL_SOURCE_LOCATION (field), "declared here");
732 bad = true;
734 if (field == NULL_TREE)
735 break;
736 field = DECL_CHAIN (field);
739 return bad;
742 /* We are processing the definition of the constexpr function FUN.
743 Check that its BODY fulfills the propriate requirements and
744 enter it in the constexpr function definition table.
745 For constructor BODY is actually the TREE_LIST of the
746 member-initializer list. */
748 tree
749 register_constexpr_fundef (tree fun, tree body)
751 constexpr_fundef entry;
752 constexpr_fundef **slot;
754 if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun)))
755 return NULL;
757 body = massage_constexpr_body (fun, body);
758 if (body == NULL_TREE || body == error_mark_node)
760 if (!DECL_CONSTRUCTOR_P (fun))
761 error ("body of constexpr function %qD not a return-statement", fun);
762 return NULL;
765 if (!potential_rvalue_constant_expression (body))
767 if (!DECL_GENERATED_P (fun))
768 require_potential_rvalue_constant_expression (body);
769 return NULL;
772 if (DECL_CONSTRUCTOR_P (fun)
773 && cx_check_missing_mem_inits (fun, body, !DECL_GENERATED_P (fun)))
774 return NULL;
776 /* Create the constexpr function table if necessary. */
777 if (constexpr_fundef_table == NULL)
778 constexpr_fundef_table
779 = hash_table<constexpr_fundef_hasher>::create_ggc (101);
781 entry.decl = fun;
782 entry.body = body;
783 slot = constexpr_fundef_table->find_slot (&entry, INSERT);
785 gcc_assert (*slot == NULL);
786 *slot = ggc_alloc<constexpr_fundef> ();
787 **slot = entry;
789 return fun;
792 /* FUN is a non-constexpr function called in a context that requires a
793 constant expression. If it comes from a constexpr template, explain why
794 the instantiation isn't constexpr. */
796 void
797 explain_invalid_constexpr_fn (tree fun)
799 static hash_set<tree> *diagnosed;
800 tree body;
801 location_t save_loc;
802 /* Only diagnose defaulted functions or instantiations. */
803 if (!DECL_DEFAULTED_FN (fun)
804 && !is_instantiation_of_constexpr (fun))
805 return;
806 if (diagnosed == NULL)
807 diagnosed = new hash_set<tree>;
808 if (diagnosed->add (fun))
809 /* Already explained. */
810 return;
812 save_loc = input_location;
813 input_location = DECL_SOURCE_LOCATION (fun);
814 inform (0, "%q+D is not usable as a constexpr function because:", fun);
815 /* First check the declaration. */
816 if (is_valid_constexpr_fn (fun, true))
818 /* Then if it's OK, the body. */
819 if (!DECL_DECLARED_CONSTEXPR_P (fun))
820 explain_implicit_non_constexpr (fun);
821 else
823 body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
824 require_potential_rvalue_constant_expression (body);
825 if (DECL_CONSTRUCTOR_P (fun))
826 cx_check_missing_mem_inits (fun, body, true);
829 input_location = save_loc;
832 /* Objects of this type represent calls to constexpr functions
833 along with the bindings of parameters to their arguments, for
834 the purpose of compile time evaluation. */
836 struct GTY((for_user)) constexpr_call {
837 /* Description of the constexpr function definition. */
838 constexpr_fundef *fundef;
839 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
840 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
841 Note: This arrangement is made to accommodate the use of
842 iterative_hash_template_arg (see pt.c). If you change this
843 representation, also change the hash calculation in
844 cxx_eval_call_expression. */
845 tree bindings;
846 /* Result of the call.
847 NULL means the call is being evaluated.
848 error_mark_node means that the evaluation was erroneous;
849 otherwise, the actuall value of the call. */
850 tree result;
851 /* The hash of this call; we remember it here to avoid having to
852 recalculate it when expanding the hash table. */
853 hashval_t hash;
856 struct constexpr_call_hasher : ggc_hasher<constexpr_call *>
858 static hashval_t hash (constexpr_call *);
859 static bool equal (constexpr_call *, constexpr_call *);
862 /* The constexpr expansion context. CALL is the current function
863 expansion, CTOR is the current aggregate initializer, OBJECT is the
864 object being initialized by CTOR, either a VAR_DECL or a _REF. VALUES
865 is a map of values of variables initialized within the expression. */
867 struct constexpr_ctx {
868 /* The innermost call we're evaluating. */
869 constexpr_call *call;
870 /* Values for any temporaries or local variables within the
871 constant-expression. */
872 hash_map<tree,tree> *values;
873 /* The CONSTRUCTOR we're currently building up for an aggregate
874 initializer. */
875 tree ctor;
876 /* The object we're building the CONSTRUCTOR for. */
877 tree object;
878 /* Whether we should error on a non-constant expression or fail quietly. */
879 bool quiet;
880 /* Whether we are strictly conforming to constant expression rules or
881 trying harder to get a constant value. */
882 bool strict;
885 /* A table of all constexpr calls that have been evaluated by the
886 compiler in this translation unit. */
888 static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
890 static tree cxx_eval_constant_expression (const constexpr_ctx *, tree,
891 bool, bool *, bool *, tree * = NULL);
893 /* Compute a hash value for a constexpr call representation. */
895 inline hashval_t
896 constexpr_call_hasher::hash (constexpr_call *info)
898 return info->hash;
901 /* Return true if the objects pointed to by P and Q represent calls
902 to the same constexpr function with the same arguments.
903 Otherwise, return false. */
905 bool
906 constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
908 tree lhs_bindings;
909 tree rhs_bindings;
910 if (lhs == rhs)
911 return 1;
912 if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
913 return 0;
914 lhs_bindings = lhs->bindings;
915 rhs_bindings = rhs->bindings;
916 while (lhs_bindings != NULL && rhs_bindings != NULL)
918 tree lhs_arg = TREE_VALUE (lhs_bindings);
919 tree rhs_arg = TREE_VALUE (rhs_bindings);
920 gcc_assert (TREE_TYPE (lhs_arg) == TREE_TYPE (rhs_arg));
921 if (!cp_tree_equal (lhs_arg, rhs_arg))
922 return 0;
923 lhs_bindings = TREE_CHAIN (lhs_bindings);
924 rhs_bindings = TREE_CHAIN (rhs_bindings);
926 return lhs_bindings == rhs_bindings;
929 /* Initialize the constexpr call table, if needed. */
931 static void
932 maybe_initialize_constexpr_call_table (void)
934 if (constexpr_call_table == NULL)
935 constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
938 /* We have an expression tree T that represents a call, either CALL_EXPR
939 or AGGR_INIT_EXPR. If the call is lexically to a named function,
940 retrun the _DECL for that function. */
942 static tree
943 get_function_named_in_call (tree t)
945 tree fun = NULL;
946 switch (TREE_CODE (t))
948 case CALL_EXPR:
949 fun = CALL_EXPR_FN (t);
950 break;
952 case AGGR_INIT_EXPR:
953 fun = AGGR_INIT_EXPR_FN (t);
954 break;
956 default:
957 gcc_unreachable();
958 break;
960 if (fun && TREE_CODE (fun) == ADDR_EXPR
961 && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
962 fun = TREE_OPERAND (fun, 0);
963 return fun;
966 /* We have an expression tree T that represents a call, either CALL_EXPR
967 or AGGR_INIT_EXPR. Return the Nth argument. */
969 static inline tree
970 get_nth_callarg (tree t, int n)
972 switch (TREE_CODE (t))
974 case CALL_EXPR:
975 return CALL_EXPR_ARG (t, n);
977 case AGGR_INIT_EXPR:
978 return AGGR_INIT_EXPR_ARG (t, n);
980 default:
981 gcc_unreachable ();
982 return NULL;
986 /* Look up the binding of the function parameter T in a constexpr
987 function call context CALL. */
989 static tree
990 lookup_parameter_binding (const constexpr_call *call, tree t)
992 tree b = purpose_member (t, call->bindings);
993 return TREE_VALUE (b);
996 /* Attempt to evaluate T which represents a call to a builtin function.
997 We assume here that all builtin functions evaluate to scalar types
998 represented by _CST nodes. */
1000 static tree
1001 cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t,
1002 bool lval,
1003 bool *non_constant_p, bool *overflow_p)
1005 const int nargs = call_expr_nargs (t);
1006 tree *args = (tree *) alloca (nargs * sizeof (tree));
1007 tree new_call;
1008 int i;
1009 for (i = 0; i < nargs; ++i)
1011 args[i] = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, i),
1012 lval,
1013 non_constant_p, overflow_p);
1014 if (ctx->quiet && *non_constant_p)
1015 return t;
1017 if (*non_constant_p)
1018 return t;
1019 new_call = fold_build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
1020 CALL_EXPR_FN (t), nargs, args);
1021 VERIFY_CONSTANT (new_call);
1022 return new_call;
1025 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1026 the type of the value to match. */
1028 static tree
1029 adjust_temp_type (tree type, tree temp)
1031 if (TREE_TYPE (temp) == type)
1032 return temp;
1033 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1034 if (TREE_CODE (temp) == CONSTRUCTOR)
1035 return build_constructor (type, CONSTRUCTOR_ELTS (temp));
1036 gcc_assert (scalarish_type_p (type));
1037 return cp_fold_convert (type, temp);
1040 /* True if we want to use the new handling of constexpr calls based on
1041 DECL_SAVED_TREE. */
1042 #define use_new_call true
1044 /* Subroutine of cxx_eval_call_expression.
1045 We are processing a call expression (either CALL_EXPR or
1046 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1047 all arguments and bind their values to correspondings
1048 parameters, making up the NEW_CALL context. */
1050 static void
1051 cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t,
1052 constexpr_call *new_call,
1053 bool *non_constant_p, bool *overflow_p,
1054 bool *non_constant_args)
1056 const int nargs = call_expr_nargs (t);
1057 tree fun = new_call->fundef->decl;
1058 tree parms = DECL_ARGUMENTS (fun);
1059 int i;
1060 tree *p = &new_call->bindings;
1061 for (i = 0; i < nargs; ++i)
1063 tree x, arg;
1064 tree type = parms ? TREE_TYPE (parms) : void_type_node;
1065 x = get_nth_callarg (t, i);
1066 /* For member function, the first argument is a pointer to the implied
1067 object. For a constructor, it might still be a dummy object, in
1068 which case we get the real argument from ctx. */
1069 if (i == 0 && DECL_CONSTRUCTOR_P (fun)
1070 && is_dummy_object (x))
1072 x = ctx->object;
1073 x = cp_build_addr_expr (x, tf_warning_or_error);
1075 bool lval = false;
1076 if (parms && DECL_BY_REFERENCE (parms) && !use_new_call)
1078 /* cp_genericize made this a reference for argument passing, but
1079 we don't want to treat it like one for C++11 constexpr
1080 evaluation. C++14 constexpr evaluation uses the genericized
1081 DECL_SAVED_TREE. */
1082 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
1083 gcc_assert (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE);
1084 type = TREE_TYPE (type);
1085 x = convert_from_reference (x);
1086 lval = true;
1088 arg = cxx_eval_constant_expression (ctx, x, lval,
1089 non_constant_p, overflow_p);
1090 /* Don't VERIFY_CONSTANT here. */
1091 if (*non_constant_p && ctx->quiet)
1092 return;
1093 /* Just discard ellipsis args after checking their constantitude. */
1094 if (!parms)
1095 continue;
1096 if (*non_constant_p)
1097 /* Don't try to adjust the type of non-constant args. */
1098 goto next;
1100 /* Make sure the binding has the same type as the parm. */
1101 if (TREE_CODE (type) != REFERENCE_TYPE)
1102 arg = adjust_temp_type (type, arg);
1103 if (!TREE_CONSTANT (arg))
1104 *non_constant_args = true;
1105 *p = build_tree_list (parms, arg);
1106 p = &TREE_CHAIN (*p);
1107 next:
1108 parms = TREE_CHAIN (parms);
1112 /* Variables and functions to manage constexpr call expansion context.
1113 These do not need to be marked for PCH or GC. */
1115 /* FIXME remember and print actual constant arguments. */
1116 static vec<tree> call_stack = vNULL;
1117 static int call_stack_tick;
1118 static int last_cx_error_tick;
1120 static bool
1121 push_cx_call_context (tree call)
1123 ++call_stack_tick;
1124 if (!EXPR_HAS_LOCATION (call))
1125 SET_EXPR_LOCATION (call, input_location);
1126 call_stack.safe_push (call);
1127 if (call_stack.length () > (unsigned) max_constexpr_depth)
1128 return false;
1129 return true;
1132 static void
1133 pop_cx_call_context (void)
1135 ++call_stack_tick;
1136 call_stack.pop ();
1139 vec<tree>
1140 cx_error_context (void)
1142 vec<tree> r = vNULL;
1143 if (call_stack_tick != last_cx_error_tick
1144 && !call_stack.is_empty ())
1145 r = call_stack;
1146 last_cx_error_tick = call_stack_tick;
1147 return r;
1150 /* Subroutine of cxx_eval_constant_expression.
1151 Evaluate the call expression tree T in the context of OLD_CALL expression
1152 evaluation. */
1154 static tree
1155 cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
1156 bool lval,
1157 bool *non_constant_p, bool *overflow_p)
1159 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
1160 tree fun = get_function_named_in_call (t);
1161 constexpr_call new_call = { NULL, NULL, NULL, 0 };
1162 bool depth_ok;
1164 if (fun == NULL_TREE)
1165 switch (CALL_EXPR_IFN (t))
1167 case IFN_UBSAN_NULL:
1168 case IFN_UBSAN_BOUNDS:
1169 return void_node;
1170 default:
1171 if (!ctx->quiet)
1172 error_at (loc, "call to internal function");
1173 *non_constant_p = true;
1174 return t;
1177 if (TREE_CODE (fun) != FUNCTION_DECL)
1179 /* Might be a constexpr function pointer. */
1180 fun = cxx_eval_constant_expression (ctx, fun,
1181 /*lval*/false, non_constant_p,
1182 overflow_p);
1183 STRIP_NOPS (fun);
1184 if (TREE_CODE (fun) == ADDR_EXPR)
1185 fun = TREE_OPERAND (fun, 0);
1187 if (TREE_CODE (fun) != FUNCTION_DECL)
1189 if (!ctx->quiet && !*non_constant_p)
1190 error_at (loc, "expression %qE does not designate a constexpr "
1191 "function", fun);
1192 *non_constant_p = true;
1193 return t;
1195 if (DECL_CLONED_FUNCTION_P (fun))
1196 fun = DECL_CLONED_FUNCTION (fun);
1198 if (is_ubsan_builtin_p (fun))
1199 return void_node;
1201 if (is_builtin_fn (fun))
1202 return cxx_eval_builtin_function_call (ctx, t,
1203 lval, non_constant_p, overflow_p);
1204 if (!DECL_DECLARED_CONSTEXPR_P (fun))
1206 if (!ctx->quiet)
1208 error_at (loc, "call to non-constexpr function %qD", fun);
1209 explain_invalid_constexpr_fn (fun);
1211 *non_constant_p = true;
1212 return t;
1215 /* Shortcut trivial constructor/op=. */
1216 if (trivial_fn_p (fun))
1218 if (call_expr_nargs (t) == 2)
1220 tree arg = convert_from_reference (get_nth_callarg (t, 1));
1221 return cxx_eval_constant_expression (ctx, arg,
1222 lval, non_constant_p,
1223 overflow_p);
1225 else if (TREE_CODE (t) == AGGR_INIT_EXPR
1226 && AGGR_INIT_ZERO_FIRST (t))
1227 return build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false);
1230 /* If in direct recursive call, optimize definition search. */
1231 if (ctx && ctx->call && ctx->call->fundef->decl == fun)
1232 new_call.fundef = ctx->call->fundef;
1233 else
1235 new_call.fundef = retrieve_constexpr_fundef (fun);
1236 if (new_call.fundef == NULL || new_call.fundef->body == NULL)
1238 if (!ctx->quiet)
1240 if (DECL_INITIAL (fun))
1242 /* The definition of fun was somehow unsuitable. */
1243 error_at (loc, "%qD called in a constant expression", fun);
1244 explain_invalid_constexpr_fn (fun);
1246 else
1247 error_at (loc, "%qD used before its definition", fun);
1249 *non_constant_p = true;
1250 return t;
1254 constexpr_ctx new_ctx = *ctx;
1255 if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
1256 && TREE_CODE (t) == AGGR_INIT_EXPR)
1258 /* We want to have an initialization target for an AGGR_INIT_EXPR.
1259 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
1260 new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
1261 tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
1262 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctor) = true;
1263 ctx->values->put (new_ctx.object, ctor);
1264 ctx = &new_ctx;
1267 bool non_constant_args = false;
1268 cxx_bind_parameters_in_call (ctx, t, &new_call,
1269 non_constant_p, overflow_p, &non_constant_args);
1270 if (*non_constant_p)
1271 return t;
1273 depth_ok = push_cx_call_context (t);
1275 tree result = NULL_TREE;
1277 constexpr_call *entry = NULL;
1278 if (!non_constant_args)
1280 new_call.hash = iterative_hash_template_arg
1281 (new_call.bindings, constexpr_fundef_hasher::hash (new_call.fundef));
1283 /* If we have seen this call before, we are done. */
1284 maybe_initialize_constexpr_call_table ();
1285 constexpr_call **slot
1286 = constexpr_call_table->find_slot (&new_call, INSERT);
1287 entry = *slot;
1288 if (entry == NULL)
1290 /* We need to keep a pointer to the entry, not just the slot, as the
1291 slot can move in the call to cxx_eval_builtin_function_call. */
1292 *slot = entry = ggc_alloc<constexpr_call> ();
1293 *entry = new_call;
1295 /* Calls which are in progress have their result set to NULL
1296 so that we can detect circular dependencies. */
1297 else if (entry->result == NULL)
1299 if (!ctx->quiet)
1300 error ("call has circular dependency");
1301 *non_constant_p = true;
1302 entry->result = result = error_mark_node;
1304 else
1305 result = entry->result;
1308 if (!depth_ok)
1310 if (!ctx->quiet)
1311 error ("constexpr evaluation depth exceeds maximum of %d (use "
1312 "-fconstexpr-depth= to increase the maximum)",
1313 max_constexpr_depth);
1314 *non_constant_p = true;
1315 result = error_mark_node;
1317 else
1319 if (!result || result == error_mark_node)
1321 if (!use_new_call)
1323 new_ctx.call = &new_call;
1324 result = (cxx_eval_constant_expression
1325 (&new_ctx, new_call.fundef->body,
1326 lval,
1327 non_constant_p, overflow_p));
1329 else
1331 if (DECL_SAVED_TREE (fun) == NULL_TREE
1332 && (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun)))
1333 /* The maybe-in-charge 'tor had its DECL_SAVED_TREE
1334 cleared, try the first clone. */
1335 fun = DECL_CHAIN (fun);
1336 gcc_assert (DECL_SAVED_TREE (fun));
1337 tree parms, res;
1339 /* Unshare the whole function body. */
1340 tree body = copy_fn (fun, parms, res);
1342 /* Associate the bindings with the remapped parms. */
1343 tree bound = new_call.bindings;
1344 tree remapped = parms;
1345 while (bound)
1347 tree oparm = TREE_PURPOSE (bound);
1348 tree arg = TREE_VALUE (bound);
1349 gcc_assert (DECL_NAME (remapped) == DECL_NAME (oparm));
1350 ctx->values->put (remapped, arg);
1351 bound = TREE_CHAIN (bound);
1352 remapped = DECL_CHAIN (remapped);
1354 /* Add the RESULT_DECL to the values map, too. */
1355 tree slot = NULL_TREE;
1356 if (DECL_BY_REFERENCE (res))
1358 slot = AGGR_INIT_EXPR_SLOT (t);
1359 tree addr = build_address (slot);
1360 addr = build_nop (TREE_TYPE (res), addr);
1361 ctx->values->put (res, addr);
1362 ctx->values->put (slot, NULL_TREE);
1364 else
1365 ctx->values->put (res, NULL_TREE);
1367 tree jump_target = NULL_TREE;
1368 cxx_eval_constant_expression (ctx, body,
1369 lval, non_constant_p, overflow_p,
1370 &jump_target);
1372 if (DECL_CONSTRUCTOR_P (fun))
1373 /* This can be null for a subobject constructor call, in
1374 which case what we care about is the initialization
1375 side-effects rather than the value. We could get at the
1376 value by evaluating *this, but we don't bother; there's
1377 no need to put such a call in the hash table. */
1378 result = lval ? ctx->object : ctx->ctor;
1379 else
1381 result = *ctx->values->get (slot ? slot : res);
1382 if (result == NULL_TREE && !*non_constant_p)
1384 if (!ctx->quiet)
1385 error ("constexpr call flows off the end "
1386 "of the function");
1387 *non_constant_p = true;
1391 /* Remove the parms/result from the values map. Is it worth
1392 bothering to do this when the map itself is only live for
1393 one constexpr evaluation? If so, maybe also clear out
1394 other vars from call, maybe in BIND_EXPR handling? */
1395 ctx->values->remove (res);
1396 if (slot)
1397 ctx->values->remove (slot);
1398 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
1399 ctx->values->remove (parm);
1403 if (result == error_mark_node)
1404 *non_constant_p = true;
1405 if (*non_constant_p)
1406 result = error_mark_node;
1407 else if (result)
1409 /* If this was a call to initialize an object, set the type of
1410 the CONSTRUCTOR to the type of that object. */
1411 if (DECL_CONSTRUCTOR_P (fun) && !use_new_call)
1413 tree ob_arg = get_nth_callarg (t, 0);
1414 STRIP_NOPS (ob_arg);
1415 gcc_assert (TYPE_PTR_P (TREE_TYPE (ob_arg))
1416 && CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (ob_arg))));
1417 result = adjust_temp_type (TREE_TYPE (TREE_TYPE (ob_arg)),
1418 result);
1421 else
1422 result = void_node;
1423 if (entry)
1424 entry->result = result;
1427 pop_cx_call_context ();
1428 return unshare_expr (result);
1431 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
1433 bool
1434 reduced_constant_expression_p (tree t)
1436 switch (TREE_CODE (t))
1438 case PTRMEM_CST:
1439 /* Even if we can't lower this yet, it's constant. */
1440 return true;
1442 case CONSTRUCTOR:
1443 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
1444 tree elt; unsigned HOST_WIDE_INT idx;
1445 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), idx, elt)
1446 if (!reduced_constant_expression_p (elt))
1447 return false;
1448 return true;
1450 default:
1451 /* FIXME are we calling this too much? */
1452 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
1456 /* Some expressions may have constant operands but are not constant
1457 themselves, such as 1/0. Call this function (or rather, the macro
1458 following it) to check for that condition.
1460 We only call this in places that require an arithmetic constant, not in
1461 places where we might have a non-constant expression that can be a
1462 component of a constant expression, such as the address of a constexpr
1463 variable that might be dereferenced later. */
1465 static bool
1466 verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
1467 bool *overflow_p)
1469 if (!*non_constant_p && !reduced_constant_expression_p (t))
1471 if (!allow_non_constant)
1472 error ("%q+E is not a constant expression", t);
1473 *non_constant_p = true;
1475 if (TREE_OVERFLOW_P (t))
1477 if (!allow_non_constant)
1479 permerror (input_location, "overflow in constant expression");
1480 /* If we're being permissive (and are in an enforcing
1481 context), ignore the overflow. */
1482 if (flag_permissive)
1483 return *non_constant_p;
1485 *overflow_p = true;
1487 return *non_constant_p;
1490 /* Check whether the shift operation with code CODE and type TYPE on LHS
1491 and RHS is undefined. If it is, give an error with an explanation,
1492 and return true; return false otherwise. */
1494 static bool
1495 cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
1496 enum tree_code code, tree type, tree lhs, tree rhs)
1498 if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
1499 || TREE_CODE (lhs) != INTEGER_CST
1500 || TREE_CODE (rhs) != INTEGER_CST)
1501 return false;
1503 tree lhstype = TREE_TYPE (lhs);
1504 unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
1506 /* [expr.shift] The behavior is undefined if the right operand
1507 is negative, or greater than or equal to the length in bits
1508 of the promoted left operand. */
1509 if (tree_int_cst_sgn (rhs) == -1)
1511 if (!ctx->quiet)
1512 error_at (loc, "right operand of shift expression %q+E is negative",
1513 build2_loc (loc, code, type, lhs, rhs));
1514 return true;
1516 if (compare_tree_int (rhs, uprec) >= 0)
1518 if (!ctx->quiet)
1519 error_at (loc, "right operand of shift expression %q+E is >= than "
1520 "the precision of the left operand",
1521 build2_loc (loc, code, type, lhs, rhs));
1522 return true;
1525 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
1526 if E1 has a signed type and non-negative value, and E1x2^E2 is
1527 representable in the corresponding unsigned type of the result type,
1528 then that value, converted to the result type, is the resulting value;
1529 otherwise, the behavior is undefined. */
1530 if (code == LSHIFT_EXPR && !TYPE_UNSIGNED (lhstype)
1531 && (cxx_dialect >= cxx11))
1533 if (tree_int_cst_sgn (lhs) == -1)
1535 if (!ctx->quiet)
1536 error_at (loc, "left operand of shift expression %q+E is negative",
1537 build2_loc (loc, code, type, lhs, rhs));
1538 return true;
1540 /* For signed x << y the following:
1541 (unsigned) x >> ((prec (lhs) - 1) - y)
1542 if > 1, is undefined. The right-hand side of this formula
1543 is the highest bit of the LHS that can be set (starting from 0),
1544 so that the shift doesn't overflow. We then right-shift the LHS
1545 to see whether any other bit is set making the original shift
1546 undefined -- the result is not representable in the corresponding
1547 unsigned type. */
1548 tree t = build_int_cst (unsigned_type_node, uprec - 1);
1549 t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
1550 tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
1551 t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
1552 if (tree_int_cst_lt (integer_one_node, t))
1554 if (!ctx->quiet)
1555 error_at (loc, "shift expression %q+E overflows",
1556 build2_loc (loc, code, type, lhs, rhs));
1557 return true;
1560 return false;
1563 /* Subroutine of cxx_eval_constant_expression.
1564 Attempt to reduce the unary expression tree T to a compile time value.
1565 If successful, return the value. Otherwise issue a diagnostic
1566 and return error_mark_node. */
1568 static tree
1569 cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
1570 bool /*lval*/,
1571 bool *non_constant_p, bool *overflow_p)
1573 tree r;
1574 tree orig_arg = TREE_OPERAND (t, 0);
1575 tree arg = cxx_eval_constant_expression (ctx, orig_arg, /*lval*/false,
1576 non_constant_p, overflow_p);
1577 VERIFY_CONSTANT (arg);
1578 location_t loc = EXPR_LOCATION (t);
1579 enum tree_code code = TREE_CODE (t);
1580 tree type = TREE_TYPE (t);
1581 r = fold_unary_loc (loc, code, type, arg);
1582 if (r == NULL_TREE)
1584 if (arg == orig_arg)
1585 r = t;
1586 else
1587 r = build1_loc (loc, code, type, arg);
1589 VERIFY_CONSTANT (r);
1590 return r;
1593 /* Subroutine of cxx_eval_constant_expression.
1594 Like cxx_eval_unary_expression, except for binary expressions. */
1596 static tree
1597 cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
1598 bool /*lval*/,
1599 bool *non_constant_p, bool *overflow_p)
1601 tree r;
1602 tree orig_lhs = TREE_OPERAND (t, 0);
1603 tree orig_rhs = TREE_OPERAND (t, 1);
1604 tree lhs, rhs;
1605 lhs = cxx_eval_constant_expression (ctx, orig_lhs, /*lval*/false,
1606 non_constant_p, overflow_p);
1607 VERIFY_CONSTANT (lhs);
1608 rhs = cxx_eval_constant_expression (ctx, orig_rhs, /*lval*/false,
1609 non_constant_p, overflow_p);
1610 VERIFY_CONSTANT (rhs);
1612 location_t loc = EXPR_LOCATION (t);
1613 enum tree_code code = TREE_CODE (t);
1614 tree type = TREE_TYPE (t);
1615 r = fold_binary_loc (loc, code, type, lhs, rhs);
1616 if (r == NULL_TREE)
1618 if (lhs == orig_lhs && rhs == orig_rhs)
1619 r = t;
1620 else
1621 r = build2_loc (loc, code, type, lhs, rhs);
1623 else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
1624 *non_constant_p = true;
1625 VERIFY_CONSTANT (r);
1626 return r;
1629 /* Subroutine of cxx_eval_constant_expression.
1630 Attempt to evaluate condition expressions. Dead branches are not
1631 looked into. */
1633 static tree
1634 cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
1635 bool lval,
1636 bool *non_constant_p, bool *overflow_p,
1637 tree *jump_target)
1639 tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
1640 /*lval*/false,
1641 non_constant_p, overflow_p);
1642 VERIFY_CONSTANT (val);
1643 /* Don't VERIFY_CONSTANT the other operands. */
1644 if (integer_zerop (val))
1645 return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
1646 lval,
1647 non_constant_p, overflow_p,
1648 jump_target);
1649 return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
1650 lval,
1651 non_constant_p, overflow_p,
1652 jump_target);
1655 /* Subroutine of cxx_eval_constant_expression.
1656 Attempt to reduce a reference to an array slot. */
1658 static tree
1659 cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
1660 bool lval,
1661 bool *non_constant_p, bool *overflow_p)
1663 tree oldary = TREE_OPERAND (t, 0);
1664 tree ary = cxx_eval_constant_expression (ctx, oldary,
1665 lval,
1666 non_constant_p, overflow_p);
1667 tree index, oldidx;
1668 HOST_WIDE_INT i;
1669 tree elem_type;
1670 unsigned len, elem_nchars = 1;
1671 if (*non_constant_p)
1672 return t;
1673 oldidx = TREE_OPERAND (t, 1);
1674 index = cxx_eval_constant_expression (ctx, oldidx,
1675 false,
1676 non_constant_p, overflow_p);
1677 VERIFY_CONSTANT (index);
1678 if (lval && ary == oldary && index == oldidx)
1679 return t;
1680 else if (lval)
1681 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
1682 elem_type = TREE_TYPE (TREE_TYPE (ary));
1683 if (TREE_CODE (ary) == CONSTRUCTOR)
1684 len = CONSTRUCTOR_NELTS (ary);
1685 else if (TREE_CODE (ary) == STRING_CST)
1687 elem_nchars = (TYPE_PRECISION (elem_type)
1688 / TYPE_PRECISION (char_type_node));
1689 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
1691 else
1693 /* We can't do anything with other tree codes, so use
1694 VERIFY_CONSTANT to complain and fail. */
1695 VERIFY_CONSTANT (ary);
1696 gcc_unreachable ();
1698 if (compare_tree_int (index, len) >= 0)
1700 if (tree_int_cst_lt (index, array_type_nelts_top (TREE_TYPE (ary))))
1702 /* If it's within the array bounds but doesn't have an explicit
1703 initializer, it's value-initialized. */
1704 tree val = build_value_init (elem_type, tf_warning_or_error);
1705 return cxx_eval_constant_expression (ctx, val,
1706 lval,
1707 non_constant_p, overflow_p);
1710 if (!ctx->quiet)
1711 error ("array subscript out of bound");
1712 *non_constant_p = true;
1713 return t;
1715 else if (tree_int_cst_lt (index, integer_zero_node))
1717 if (!ctx->quiet)
1718 error ("negative array subscript");
1719 *non_constant_p = true;
1720 return t;
1722 i = tree_to_shwi (index);
1723 if (TREE_CODE (ary) == CONSTRUCTOR)
1724 return (*CONSTRUCTOR_ELTS (ary))[i].value;
1725 else if (elem_nchars == 1)
1726 return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary))),
1727 TREE_STRING_POINTER (ary)[i]);
1728 else
1730 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (ary)));
1731 return native_interpret_expr (type, (const unsigned char *)
1732 TREE_STRING_POINTER (ary)
1733 + i * elem_nchars, elem_nchars);
1735 /* Don't VERIFY_CONSTANT here. */
1738 /* Subroutine of cxx_eval_constant_expression.
1739 Attempt to reduce a field access of a value of class type. */
1741 static tree
1742 cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
1743 bool lval,
1744 bool *non_constant_p, bool *overflow_p)
1746 unsigned HOST_WIDE_INT i;
1747 tree field;
1748 tree value;
1749 tree part = TREE_OPERAND (t, 1);
1750 tree orig_whole = TREE_OPERAND (t, 0);
1751 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
1752 lval,
1753 non_constant_p, overflow_p);
1754 if (whole == orig_whole)
1755 return t;
1756 if (lval)
1757 return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
1758 whole, part, NULL_TREE);
1759 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
1760 CONSTRUCTOR. */
1761 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
1763 if (!ctx->quiet)
1764 error ("%qE is not a constant expression", orig_whole);
1765 *non_constant_p = true;
1767 if (DECL_MUTABLE_P (part))
1769 if (!ctx->quiet)
1770 error ("mutable %qD is not usable in a constant expression", part);
1771 *non_constant_p = true;
1773 if (*non_constant_p)
1774 return t;
1775 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
1777 if (field == part)
1779 if (value)
1780 return value;
1781 else
1782 /* We're in the middle of initializing it. */
1783 break;
1786 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
1787 && CONSTRUCTOR_NELTS (whole) > 0)
1789 /* DR 1188 says we don't have to deal with this. */
1790 if (!ctx->quiet)
1791 error ("accessing %qD member instead of initialized %qD member in "
1792 "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index);
1793 *non_constant_p = true;
1794 return t;
1797 if (CONSTRUCTOR_NO_IMPLICIT_ZERO (whole))
1799 /* 'whole' is part of the aggregate initializer we're currently
1800 building; if there's no initializer for this member yet, that's an
1801 error. */
1802 if (!ctx->quiet)
1803 error ("accessing uninitialized member %qD", part);
1804 *non_constant_p = true;
1805 return t;
1808 /* If there's no explicit init for this field, it's value-initialized. */
1809 value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
1810 return cxx_eval_constant_expression (ctx, value,
1811 lval,
1812 non_constant_p, overflow_p);
1815 /* Subroutine of cxx_eval_constant_expression.
1816 Attempt to reduce a field access of a value of class type that is
1817 expressed as a BIT_FIELD_REF. */
1819 static tree
1820 cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
1821 bool lval,
1822 bool *non_constant_p, bool *overflow_p)
1824 tree orig_whole = TREE_OPERAND (t, 0);
1825 tree retval, fldval, utype, mask;
1826 bool fld_seen = false;
1827 HOST_WIDE_INT istart, isize;
1828 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
1829 lval,
1830 non_constant_p, overflow_p);
1831 tree start, field, value;
1832 unsigned HOST_WIDE_INT i;
1834 if (whole == orig_whole)
1835 return t;
1836 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
1837 CONSTRUCTOR. */
1838 if (!*non_constant_p
1839 && TREE_CODE (whole) != VECTOR_CST
1840 && TREE_CODE (whole) != CONSTRUCTOR)
1842 if (!ctx->quiet)
1843 error ("%qE is not a constant expression", orig_whole);
1844 *non_constant_p = true;
1846 if (*non_constant_p)
1847 return t;
1849 if (TREE_CODE (whole) == VECTOR_CST)
1850 return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
1851 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
1853 start = TREE_OPERAND (t, 2);
1854 istart = tree_to_shwi (start);
1855 isize = tree_to_shwi (TREE_OPERAND (t, 1));
1856 utype = TREE_TYPE (t);
1857 if (!TYPE_UNSIGNED (utype))
1858 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
1859 retval = build_int_cst (utype, 0);
1860 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
1862 tree bitpos = bit_position (field);
1863 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
1864 return value;
1865 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
1866 && TREE_CODE (value) == INTEGER_CST
1867 && tree_fits_shwi_p (bitpos)
1868 && tree_fits_shwi_p (DECL_SIZE (field)))
1870 HOST_WIDE_INT bit = tree_to_shwi (bitpos);
1871 HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
1872 HOST_WIDE_INT shift;
1873 if (bit >= istart && bit + sz <= istart + isize)
1875 fldval = fold_convert (utype, value);
1876 mask = build_int_cst_type (utype, -1);
1877 mask = fold_build2 (LSHIFT_EXPR, utype, mask,
1878 size_int (TYPE_PRECISION (utype) - sz));
1879 mask = fold_build2 (RSHIFT_EXPR, utype, mask,
1880 size_int (TYPE_PRECISION (utype) - sz));
1881 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
1882 shift = bit - istart;
1883 if (BYTES_BIG_ENDIAN)
1884 shift = TYPE_PRECISION (utype) - shift - sz;
1885 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
1886 size_int (shift));
1887 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
1888 fld_seen = true;
1892 if (fld_seen)
1893 return fold_convert (TREE_TYPE (t), retval);
1894 gcc_unreachable ();
1895 return error_mark_node;
1898 /* Subroutine of cxx_eval_constant_expression.
1899 Evaluate a short-circuited logical expression T in the context
1900 of a given constexpr CALL. BAILOUT_VALUE is the value for
1901 early return. CONTINUE_VALUE is used here purely for
1902 sanity check purposes. */
1904 static tree
1905 cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
1906 tree bailout_value, tree continue_value,
1907 bool lval,
1908 bool *non_constant_p, bool *overflow_p)
1910 tree r;
1911 tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
1912 lval,
1913 non_constant_p, overflow_p);
1914 VERIFY_CONSTANT (lhs);
1915 if (tree_int_cst_equal (lhs, bailout_value))
1916 return lhs;
1917 gcc_assert (tree_int_cst_equal (lhs, continue_value));
1918 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
1919 lval, non_constant_p,
1920 overflow_p);
1921 VERIFY_CONSTANT (r);
1922 return r;
1925 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
1926 CONSTRUCTOR elements to initialize (part of) an object containing that
1927 field. Return a pointer to the constructor_elt corresponding to the
1928 initialization of the field. */
1930 static constructor_elt *
1931 base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
1933 tree aggr = TREE_OPERAND (ref, 0);
1934 tree field = TREE_OPERAND (ref, 1);
1935 HOST_WIDE_INT i;
1936 constructor_elt *ce;
1938 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
1940 if (TREE_CODE (aggr) == COMPONENT_REF)
1942 constructor_elt *base_ce
1943 = base_field_constructor_elt (v, aggr);
1944 v = CONSTRUCTOR_ELTS (base_ce->value);
1947 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
1948 if (ce->index == field)
1949 return ce;
1951 gcc_unreachable ();
1952 return NULL;
1955 /* Some of the expressions fed to the constexpr mechanism are calls to
1956 constructors, which have type void. In that case, return the type being
1957 initialized by the constructor. */
1959 static tree
1960 initialized_type (tree t)
1962 if (TYPE_P (t))
1963 return t;
1964 tree type = cv_unqualified (TREE_TYPE (t));
1965 if (TREE_CODE (t) == CALL_EXPR || TREE_CODE (t) == AGGR_INIT_EXPR)
1967 /* A constructor call has void type, so we need to look deeper. */
1968 tree fn = get_function_named_in_call (t);
1969 if (fn && TREE_CODE (fn) == FUNCTION_DECL
1970 && DECL_CXX_CONSTRUCTOR_P (fn))
1971 type = DECL_CONTEXT (fn);
1973 return type;
1976 /* We're about to initialize element INDEX of an array or class from VALUE.
1977 Set up NEW_CTX appropriately by adjusting .object to refer to the
1978 subobject and creating a new CONSTRUCTOR if the element is itself
1979 a class or array. */
1981 static void
1982 init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
1983 tree index, tree &value)
1985 new_ctx = *ctx;
1987 if (index && TREE_CODE (index) != INTEGER_CST
1988 && TREE_CODE (index) != FIELD_DECL)
1989 /* This won't have an element in the new CONSTRUCTOR. */
1990 return;
1992 tree type = initialized_type (value);
1993 if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
1994 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
1995 return;
1997 /* The sub-aggregate initializer might contain a placeholder;
1998 update object to refer to the subobject and ctor to refer to
1999 the (newly created) sub-initializer. */
2000 if (ctx->object)
2001 new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
2002 tree elt = build_constructor (type, NULL);
2003 CONSTRUCTOR_NO_IMPLICIT_ZERO (elt) = true;
2004 new_ctx.ctor = elt;
2006 if (TREE_CODE (value) == TARGET_EXPR)
2007 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
2008 value = TARGET_EXPR_INITIAL (value);
2011 /* We're about to process an initializer for a class or array TYPE. Make
2012 sure that CTX is set up appropriately. */
2014 static void
2015 verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
2017 /* We don't bother building a ctor for an empty base subobject. */
2018 if (is_empty_class (type))
2019 return;
2021 /* We're in the middle of an initializer that might involve placeholders;
2022 our caller should have created a CONSTRUCTOR for us to put the
2023 initializer into. We will either return that constructor or T. */
2024 gcc_assert (ctx->ctor);
2025 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2026 (type, TREE_TYPE (ctx->ctor)));
2027 gcc_assert (CONSTRUCTOR_NELTS (ctx->ctor) == 0);
2028 if (ctx->object)
2029 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2030 (type, TREE_TYPE (ctx->object)));
2031 gcc_assert (!ctx->object || !DECL_P (ctx->object)
2032 || *(ctx->values->get (ctx->object)) == ctx->ctor);
2035 /* Subroutine of cxx_eval_constant_expression.
2036 The expression tree T denotes a C-style array or a C-style
2037 aggregate. Reduce it to a constant expression. */
2039 static tree
2040 cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
2041 bool lval,
2042 bool *non_constant_p, bool *overflow_p)
2044 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
2045 bool changed = false;
2046 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
2048 verify_ctor_sanity (ctx, TREE_TYPE (t));
2049 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
2050 vec_alloc (*p, vec_safe_length (v));
2052 unsigned i; tree index, value;
2053 FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
2055 constexpr_ctx new_ctx;
2056 init_subob_ctx (ctx, new_ctx, index, value);
2057 if (new_ctx.ctor != ctx->ctor)
2058 /* If we built a new CONSTRUCTOR, attach it now so that other
2059 initializers can refer to it. */
2060 CONSTRUCTOR_APPEND_ELT (*p, index, new_ctx.ctor);
2061 tree elt = cxx_eval_constant_expression (&new_ctx, value,
2062 lval,
2063 non_constant_p, overflow_p);
2064 /* Don't VERIFY_CONSTANT here. */
2065 if (ctx->quiet && *non_constant_p)
2066 break;
2067 if (elt != value)
2068 changed = true;
2069 if (index && TREE_CODE (index) == COMPONENT_REF)
2071 /* This is an initialization of a vfield inside a base
2072 subaggregate that we already initialized; push this
2073 initialization into the previous initialization. */
2074 constructor_elt *inner = base_field_constructor_elt (*p, index);
2075 inner->value = elt;
2076 changed = true;
2078 else if (index
2079 && (TREE_CODE (index) == NOP_EXPR
2080 || TREE_CODE (index) == POINTER_PLUS_EXPR))
2082 /* This is an initializer for an empty base; now that we've
2083 checked that it's constant, we can ignore it. */
2084 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
2085 changed = true;
2087 else if (new_ctx.ctor != ctx->ctor)
2089 /* We appended this element above; update the value. */
2090 gcc_assert ((*p)->last().index == index);
2091 (*p)->last().value = elt;
2093 else
2094 CONSTRUCTOR_APPEND_ELT (*p, index, elt);
2096 if (*non_constant_p || !changed)
2097 return t;
2098 t = ctx->ctor;
2099 /* We're done building this CONSTRUCTOR, so now we can interpret an
2100 element without an explicit initializer as value-initialized. */
2101 CONSTRUCTOR_NO_IMPLICIT_ZERO (t) = false;
2102 if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2103 t = fold (t);
2104 return t;
2107 /* Subroutine of cxx_eval_constant_expression.
2108 The expression tree T is a VEC_INIT_EXPR which denotes the desired
2109 initialization of a non-static data member of array type. Reduce it to a
2110 CONSTRUCTOR.
2112 Note that apart from value-initialization (when VALUE_INIT is true),
2113 this is only intended to support value-initialization and the
2114 initializations done by defaulted constructors for classes with
2115 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
2116 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
2117 for the copy/move constructor. */
2119 static tree
2120 cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
2121 bool value_init, bool lval,
2122 bool *non_constant_p, bool *overflow_p)
2124 tree elttype = TREE_TYPE (atype);
2125 unsigned HOST_WIDE_INT max = tree_to_uhwi (array_type_nelts_top (atype));
2126 verify_ctor_sanity (ctx, atype);
2127 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
2128 vec_alloc (*p, max + 1);
2129 bool pre_init = false;
2130 unsigned HOST_WIDE_INT i;
2132 /* For the default constructor, build up a call to the default
2133 constructor of the element type. We only need to handle class types
2134 here, as for a constructor to be constexpr, all members must be
2135 initialized, which for a defaulted default constructor means they must
2136 be of a class type with a constexpr default constructor. */
2137 if (TREE_CODE (elttype) == ARRAY_TYPE)
2138 /* We only do this at the lowest level. */;
2139 else if (value_init)
2141 init = build_value_init (elttype, tf_warning_or_error);
2142 pre_init = true;
2144 else if (!init)
2146 vec<tree, va_gc> *argvec = make_tree_vector ();
2147 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
2148 &argvec, elttype, LOOKUP_NORMAL,
2149 tf_warning_or_error);
2150 release_tree_vector (argvec);
2151 init = build_aggr_init_expr (TREE_TYPE (init), init);
2152 pre_init = true;
2155 for (i = 0; i < max; ++i)
2157 tree idx = build_int_cst (size_type_node, i);
2158 tree eltinit;
2159 constexpr_ctx new_ctx;
2160 init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
2161 if (new_ctx.ctor != ctx->ctor)
2162 CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
2163 if (TREE_CODE (elttype) == ARRAY_TYPE)
2165 /* A multidimensional array; recurse. */
2166 if (value_init || init == NULL_TREE)
2167 eltinit = NULL_TREE;
2168 else
2169 eltinit = cp_build_array_ref (input_location, init, idx,
2170 tf_warning_or_error);
2171 eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init,
2172 lval,
2173 non_constant_p, overflow_p);
2175 else if (pre_init)
2177 /* Initializing an element using value or default initialization
2178 we just pre-built above. */
2179 eltinit = (cxx_eval_constant_expression
2180 (&new_ctx, init,
2181 lval, non_constant_p, overflow_p));
2183 else
2185 /* Copying an element. */
2186 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2187 (atype, TREE_TYPE (init)));
2188 eltinit = cp_build_array_ref (input_location, init, idx,
2189 tf_warning_or_error);
2190 if (!real_lvalue_p (init))
2191 eltinit = move (eltinit);
2192 eltinit = force_rvalue (eltinit, tf_warning_or_error);
2193 eltinit = (cxx_eval_constant_expression
2194 (&new_ctx, eltinit, lval,
2195 non_constant_p, overflow_p));
2197 if (*non_constant_p && !ctx->quiet)
2198 break;
2199 if (new_ctx.ctor != ctx->ctor)
2201 /* We appended this element above; update the value. */
2202 gcc_assert ((*p)->last().index == idx);
2203 (*p)->last().value = eltinit;
2205 else
2206 CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
2209 if (!*non_constant_p)
2211 init = ctx->ctor;
2212 CONSTRUCTOR_NO_IMPLICIT_ZERO (init) = false;
2214 return init;
2217 static tree
2218 cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
2219 bool lval,
2220 bool *non_constant_p, bool *overflow_p)
2222 tree atype = TREE_TYPE (t);
2223 tree init = VEC_INIT_EXPR_INIT (t);
2224 tree r = cxx_eval_vec_init_1 (ctx, atype, init,
2225 VEC_INIT_EXPR_VALUE_INIT (t),
2226 lval, non_constant_p, overflow_p);
2227 if (*non_constant_p)
2228 return t;
2229 else
2230 return r;
2233 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
2234 match. We want to be less strict for simple *& folding; if we have a
2235 non-const temporary that we access through a const pointer, that should
2236 work. We handle this here rather than change fold_indirect_ref_1
2237 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
2238 don't really make sense outside of constant expression evaluation. Also
2239 we want to allow folding to COMPONENT_REF, which could cause trouble
2240 with TBAA in fold_indirect_ref_1.
2242 Try to keep this function synced with fold_indirect_ref_1. */
2244 static tree
2245 cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
2247 tree sub, subtype;
2249 sub = op0;
2250 STRIP_NOPS (sub);
2251 subtype = TREE_TYPE (sub);
2252 if (!POINTER_TYPE_P (subtype))
2253 return NULL_TREE;
2255 if (TREE_CODE (sub) == ADDR_EXPR)
2257 tree op = TREE_OPERAND (sub, 0);
2258 tree optype = TREE_TYPE (op);
2260 /* *&CONST_DECL -> to the value of the const decl. */
2261 if (TREE_CODE (op) == CONST_DECL)
2262 return DECL_INITIAL (op);
2263 /* *&p => p; make sure to handle *&"str"[cst] here. */
2264 if (same_type_ignoring_top_level_qualifiers_p (optype, type))
2266 tree fop = fold_read_from_constant_string (op);
2267 if (fop)
2268 return fop;
2269 else
2270 return op;
2272 /* *(foo *)&fooarray => fooarray[0] */
2273 else if (TREE_CODE (optype) == ARRAY_TYPE
2274 && (same_type_ignoring_top_level_qualifiers_p
2275 (type, TREE_TYPE (optype))))
2277 tree type_domain = TYPE_DOMAIN (optype);
2278 tree min_val = size_zero_node;
2279 if (type_domain && TYPE_MIN_VALUE (type_domain))
2280 min_val = TYPE_MIN_VALUE (type_domain);
2281 return build4_loc (loc, ARRAY_REF, type, op, min_val,
2282 NULL_TREE, NULL_TREE);
2284 /* *(foo *)&complexfoo => __real__ complexfoo */
2285 else if (TREE_CODE (optype) == COMPLEX_TYPE
2286 && (same_type_ignoring_top_level_qualifiers_p
2287 (type, TREE_TYPE (optype))))
2288 return fold_build1_loc (loc, REALPART_EXPR, type, op);
2289 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
2290 else if (TREE_CODE (optype) == VECTOR_TYPE
2291 && (same_type_ignoring_top_level_qualifiers_p
2292 (type, TREE_TYPE (optype))))
2294 tree part_width = TYPE_SIZE (type);
2295 tree index = bitsize_int (0);
2296 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
2298 /* Also handle conversion to an empty base class, which
2299 is represented with a NOP_EXPR. */
2300 else if (is_empty_class (type)
2301 && CLASS_TYPE_P (optype)
2302 && DERIVED_FROM_P (type, optype))
2304 *empty_base = true;
2305 return op;
2307 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
2308 else if (RECORD_OR_UNION_TYPE_P (optype))
2310 tree field = TYPE_FIELDS (optype);
2311 for (; field; field = DECL_CHAIN (field))
2312 if (TREE_CODE (field) == FIELD_DECL
2313 && integer_zerop (byte_position (field))
2314 && (same_type_ignoring_top_level_qualifiers_p
2315 (TREE_TYPE (field), type)))
2317 return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE);
2318 break;
2322 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
2323 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
2325 tree op00 = TREE_OPERAND (sub, 0);
2326 tree op01 = TREE_OPERAND (sub, 1);
2328 STRIP_NOPS (op00);
2329 if (TREE_CODE (op00) == ADDR_EXPR)
2331 tree op00type;
2332 op00 = TREE_OPERAND (op00, 0);
2333 op00type = TREE_TYPE (op00);
2335 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
2336 if (TREE_CODE (op00type) == VECTOR_TYPE
2337 && (same_type_ignoring_top_level_qualifiers_p
2338 (type, TREE_TYPE (op00type))))
2340 HOST_WIDE_INT offset = tree_to_shwi (op01);
2341 tree part_width = TYPE_SIZE (type);
2342 unsigned HOST_WIDE_INT part_widthi = tree_to_shwi (part_width)/BITS_PER_UNIT;
2343 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
2344 tree index = bitsize_int (indexi);
2346 if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
2347 return fold_build3_loc (loc,
2348 BIT_FIELD_REF, type, op00,
2349 part_width, index);
2352 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
2353 else if (TREE_CODE (op00type) == COMPLEX_TYPE
2354 && (same_type_ignoring_top_level_qualifiers_p
2355 (type, TREE_TYPE (op00type))))
2357 tree size = TYPE_SIZE_UNIT (type);
2358 if (tree_int_cst_equal (size, op01))
2359 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
2361 /* ((foo *)&fooarray)[1] => fooarray[1] */
2362 else if (TREE_CODE (op00type) == ARRAY_TYPE
2363 && (same_type_ignoring_top_level_qualifiers_p
2364 (type, TREE_TYPE (op00type))))
2366 tree type_domain = TYPE_DOMAIN (op00type);
2367 tree min_val = size_zero_node;
2368 if (type_domain && TYPE_MIN_VALUE (type_domain))
2369 min_val = TYPE_MIN_VALUE (type_domain);
2370 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
2371 TYPE_SIZE_UNIT (type));
2372 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
2373 return build4_loc (loc, ARRAY_REF, type, op00, op01,
2374 NULL_TREE, NULL_TREE);
2376 /* Also handle conversion to an empty base class, which
2377 is represented with a NOP_EXPR. */
2378 else if (is_empty_class (type)
2379 && CLASS_TYPE_P (op00type)
2380 && DERIVED_FROM_P (type, op00type))
2382 *empty_base = true;
2383 return op00;
2385 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
2386 else if (RECORD_OR_UNION_TYPE_P (op00type))
2388 tree field = TYPE_FIELDS (op00type);
2389 for (; field; field = DECL_CHAIN (field))
2390 if (TREE_CODE (field) == FIELD_DECL
2391 && tree_int_cst_equal (byte_position (field), op01)
2392 && (same_type_ignoring_top_level_qualifiers_p
2393 (TREE_TYPE (field), type)))
2395 return fold_build3 (COMPONENT_REF, type, op00,
2396 field, NULL_TREE);
2397 break;
2402 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
2403 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
2404 && (same_type_ignoring_top_level_qualifiers_p
2405 (type, TREE_TYPE (TREE_TYPE (subtype)))))
2407 tree type_domain;
2408 tree min_val = size_zero_node;
2409 tree newsub = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL);
2410 if (newsub)
2411 sub = newsub;
2412 else
2413 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
2414 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
2415 if (type_domain && TYPE_MIN_VALUE (type_domain))
2416 min_val = TYPE_MIN_VALUE (type_domain);
2417 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
2418 NULL_TREE);
2421 return NULL_TREE;
2424 static tree
2425 cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
2426 bool lval,
2427 bool *non_constant_p, bool *overflow_p)
2429 tree orig_op0 = TREE_OPERAND (t, 0);
2430 tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
2431 /*lval*/false, non_constant_p,
2432 overflow_p);
2433 bool empty_base = false;
2434 tree r;
2436 /* Don't VERIFY_CONSTANT here. */
2437 if (*non_constant_p)
2438 return t;
2440 r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0,
2441 &empty_base);
2443 if (r)
2444 r = cxx_eval_constant_expression (ctx, r,
2445 lval, non_constant_p, overflow_p);
2446 else
2448 tree sub = op0;
2449 STRIP_NOPS (sub);
2450 if (TREE_CODE (sub) == ADDR_EXPR)
2452 /* We couldn't fold to a constant value. Make sure it's not
2453 something we should have been able to fold. */
2454 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
2455 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
2456 /* DR 1188 says we don't have to deal with this. */
2457 if (!ctx->quiet)
2458 error ("accessing value of %qE through a %qT glvalue in a "
2459 "constant expression", build_fold_indirect_ref (sub),
2460 TREE_TYPE (t));
2461 *non_constant_p = true;
2462 return t;
2466 /* If we're pulling out the value of an empty base, make sure
2467 that the whole object is constant and then return an empty
2468 CONSTRUCTOR. */
2469 if (empty_base && !lval)
2471 VERIFY_CONSTANT (r);
2472 r = build_constructor (TREE_TYPE (t), NULL);
2473 TREE_CONSTANT (r) = true;
2476 if (r == NULL_TREE)
2478 if (lval && op0 != orig_op0)
2479 return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
2480 if (!lval)
2481 VERIFY_CONSTANT (t);
2482 return t;
2484 return r;
2487 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
2488 Shared between potential_constant_expression and
2489 cxx_eval_constant_expression. */
2491 static void
2492 non_const_var_error (tree r)
2494 tree type = TREE_TYPE (r);
2495 error ("the value of %qD is not usable in a constant "
2496 "expression", r);
2497 /* Avoid error cascade. */
2498 if (DECL_INITIAL (r) == error_mark_node)
2499 return;
2500 if (DECL_DECLARED_CONSTEXPR_P (r))
2501 inform (DECL_SOURCE_LOCATION (r),
2502 "%qD used in its own initializer", r);
2503 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
2505 if (!CP_TYPE_CONST_P (type))
2506 inform (DECL_SOURCE_LOCATION (r),
2507 "%q#D is not const", r);
2508 else if (CP_TYPE_VOLATILE_P (type))
2509 inform (DECL_SOURCE_LOCATION (r),
2510 "%q#D is volatile", r);
2511 else if (!DECL_INITIAL (r)
2512 || !TREE_CONSTANT (DECL_INITIAL (r)))
2513 inform (DECL_SOURCE_LOCATION (r),
2514 "%qD was not initialized with a constant "
2515 "expression", r);
2516 else
2517 gcc_unreachable ();
2519 else
2521 if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
2522 inform (DECL_SOURCE_LOCATION (r),
2523 "%qD was not declared %<constexpr%>", r);
2524 else
2525 inform (DECL_SOURCE_LOCATION (r),
2526 "%qD does not have integral or enumeration type",
2531 /* Subroutine of cxx_eval_constant_expression.
2532 Like cxx_eval_unary_expression, except for trinary expressions. */
2534 static tree
2535 cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
2536 bool lval,
2537 bool *non_constant_p, bool *overflow_p)
2539 int i;
2540 tree args[3];
2541 tree val;
2543 for (i = 0; i < 3; i++)
2545 args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
2546 lval,
2547 non_constant_p, overflow_p);
2548 VERIFY_CONSTANT (args[i]);
2551 val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
2552 args[0], args[1], args[2]);
2553 if (val == NULL_TREE)
2554 return t;
2555 VERIFY_CONSTANT (val);
2556 return val;
2559 bool
2560 var_in_constexpr_fn (tree t)
2562 tree ctx = DECL_CONTEXT (t);
2563 return (cxx_dialect >= cxx14 && ctx && TREE_CODE (ctx) == FUNCTION_DECL
2564 && DECL_DECLARED_CONSTEXPR_P (ctx));
2567 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
2569 static tree
2570 cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
2571 bool lval,
2572 bool *non_constant_p, bool *overflow_p)
2574 constexpr_ctx new_ctx = *ctx;
2576 /* First we figure out where we're storing to. */
2577 tree target = TREE_OPERAND (t, 0);
2578 target = cxx_eval_constant_expression (ctx, target,
2579 true,
2580 non_constant_p, overflow_p);
2581 if (*non_constant_p)
2582 return t;
2584 /* And then find the underlying variable. */
2585 vec<tree,va_gc> *refs = make_tree_vector();
2586 tree object = NULL_TREE;
2587 for (tree probe = target; object == NULL_TREE; )
2589 switch (TREE_CODE (probe))
2591 case BIT_FIELD_REF:
2592 case COMPONENT_REF:
2593 case ARRAY_REF:
2594 vec_safe_push (refs, TREE_OPERAND (probe, 1));
2595 vec_safe_push (refs, TREE_TYPE (probe));
2596 probe = TREE_OPERAND (probe, 0);
2597 break;
2599 default:
2600 object = probe;
2604 /* And then find/build up our initializer for the path to the subobject
2605 we're initializing. */
2606 tree *valp;
2607 if (DECL_P (object))
2608 valp = ctx->values->get (object);
2609 else
2610 valp = NULL;
2611 if (!valp)
2613 /* A constant-expression cannot modify objects from outside the
2614 constant-expression. */
2615 if (!ctx->quiet)
2616 error ("modification of %qE is not a constant-expression", object);
2617 *non_constant_p = true;
2618 return t;
2620 tree type = TREE_TYPE (object);
2621 while (!refs->is_empty())
2623 if (*valp == NULL_TREE)
2625 *valp = build_constructor (type, NULL);
2626 CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp) = true;
2629 constructor_elt ce;
2630 type = refs->pop();
2631 ce.index = refs->pop();
2632 ce.value = NULL_TREE;
2634 unsigned HOST_WIDE_INT idx = 0;
2635 constructor_elt *cep = NULL;
2636 for (idx = 0;
2637 vec_safe_iterate (CONSTRUCTOR_ELTS (*valp), idx, &cep);
2638 idx++)
2639 /* ??? slow */
2640 if (cp_tree_equal (ce.index, cep->index))
2641 break;
2642 if (!cep)
2643 cep = vec_safe_push (CONSTRUCTOR_ELTS (*valp), ce);
2644 valp = &cep->value;
2646 release_tree_vector (refs);
2648 if ((AGGREGATE_TYPE_P (TREE_TYPE (t)) || VECTOR_TYPE_P (TREE_TYPE (t))))
2650 /* Create a new CONSTRUCTOR in case evaluation of the initializer
2651 wants to modify it. */
2652 *valp = new_ctx.ctor = build_constructor (TREE_TYPE (t), NULL);
2653 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
2654 new_ctx.object = target;
2657 tree init = cxx_eval_constant_expression (&new_ctx, TREE_OPERAND (t, 1),
2658 false,
2659 non_constant_p, overflow_p);
2660 if (target == object)
2661 /* The hash table might have moved since the get earlier. */
2662 ctx->values->put (object, init);
2663 else
2664 *valp = init;
2666 if (*non_constant_p)
2667 return t;
2668 else if (lval)
2669 return target;
2670 else
2671 return init;
2674 /* Evaluate a ++ or -- expression. */
2676 static tree
2677 cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
2678 bool lval,
2679 bool *non_constant_p, bool *overflow_p)
2681 enum tree_code code = TREE_CODE (t);
2682 tree type = TREE_TYPE (t);
2683 tree op = TREE_OPERAND (t, 0);
2684 tree offset = TREE_OPERAND (t, 1);
2685 gcc_assert (TREE_CONSTANT (offset));
2687 /* The operand as an lvalue. */
2688 op = cxx_eval_constant_expression (ctx, op, true,
2689 non_constant_p, overflow_p);
2691 /* The operand as an rvalue. */
2692 tree val = rvalue (op);
2693 val = cxx_eval_constant_expression (ctx, val, false,
2694 non_constant_p, overflow_p);
2695 VERIFY_CONSTANT (val);
2697 /* The modified value. */
2698 bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
2699 tree mod;
2700 if (POINTER_TYPE_P (type))
2702 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
2703 offset = convert_to_ptrofftype (offset);
2704 if (!inc)
2705 offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
2706 mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
2708 else
2709 mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
2710 VERIFY_CONSTANT (mod);
2712 /* Storing the modified value. */
2713 tree store = build2 (MODIFY_EXPR, type, op, mod);
2714 cxx_eval_constant_expression (ctx, store,
2715 true, non_constant_p, overflow_p);
2717 /* And the value of the expression. */
2718 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2720 /* Prefix ops are lvalues. */
2721 if (lval)
2722 return op;
2723 else
2724 /* But we optimize when the caller wants an rvalue. */
2725 return mod;
2727 else
2728 /* Postfix ops are rvalues. */
2729 return val;
2732 /* Predicates for the meaning of *jump_target. */
2734 static bool
2735 returns (tree *jump_target)
2737 return *jump_target
2738 && TREE_CODE (*jump_target) == RETURN_EXPR;
2741 static bool
2742 breaks (tree *jump_target)
2744 return *jump_target
2745 && TREE_CODE (*jump_target) == LABEL_DECL
2746 && LABEL_DECL_BREAK (*jump_target);
2749 static bool
2750 continues (tree *jump_target)
2752 return *jump_target
2753 && TREE_CODE (*jump_target) == LABEL_DECL
2754 && LABEL_DECL_CONTINUE (*jump_target);
2757 static bool
2758 switches (tree *jump_target)
2760 return *jump_target
2761 && TREE_CODE (*jump_target) == INTEGER_CST;
2764 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
2765 at I matches *jump_target. If we're looking for a case label and we see
2766 the default label, copy I into DEFAULT_LABEL. */
2768 static bool
2769 label_matches (tree *jump_target, tree_stmt_iterator i,
2770 tree_stmt_iterator& default_label)
2772 tree stmt = tsi_stmt (i);
2773 switch (TREE_CODE (*jump_target))
2775 case LABEL_DECL:
2776 if (TREE_CODE (stmt) == LABEL_EXPR
2777 && LABEL_EXPR_LABEL (stmt) == *jump_target)
2778 return true;
2779 break;
2781 case INTEGER_CST:
2782 if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
2784 if (!CASE_LOW (stmt))
2785 default_label = i;
2786 else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
2787 return true;
2789 break;
2791 default:
2792 gcc_unreachable ();
2794 return false;
2797 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
2798 semantics, for switch, break, continue, and return. */
2800 static tree
2801 cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
2802 bool *non_constant_p, bool *overflow_p,
2803 tree *jump_target)
2805 tree_stmt_iterator i;
2806 tree_stmt_iterator default_label = tree_stmt_iterator();
2807 tree local_target;
2808 /* In a statement-expression we want to return the last value. */
2809 tree r = NULL_TREE;
2810 if (!jump_target)
2812 local_target = NULL_TREE;
2813 jump_target = &local_target;
2815 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
2817 reenter:
2818 tree stmt = tsi_stmt (i);
2819 if (*jump_target)
2821 if (TREE_CODE (stmt) == STATEMENT_LIST)
2822 /* The label we want might be inside. */;
2823 else if (label_matches (jump_target, i, default_label))
2824 /* Found it. */
2825 *jump_target = NULL_TREE;
2826 else
2827 continue;
2829 r = cxx_eval_constant_expression (ctx, stmt, false,
2830 non_constant_p, overflow_p,
2831 jump_target);
2832 if (*non_constant_p)
2833 break;
2834 if (returns (jump_target) || breaks (jump_target))
2835 break;
2837 if (switches (jump_target) && !tsi_end_p (default_label))
2839 i = default_label;
2840 *jump_target = NULL_TREE;
2841 goto reenter;
2843 return r;
2846 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
2847 semantics; continue semantics are covered by cxx_eval_statement_list. */
2849 static tree
2850 cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
2851 bool *non_constant_p, bool *overflow_p,
2852 tree *jump_target)
2854 tree body = TREE_OPERAND (t, 0);
2855 while (true)
2857 cxx_eval_statement_list (ctx, body,
2858 non_constant_p, overflow_p, jump_target);
2859 if (returns (jump_target) || breaks (jump_target) || *non_constant_p)
2860 break;
2862 if (breaks (jump_target))
2863 *jump_target = NULL_TREE;
2864 return NULL_TREE;
2867 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
2868 semantics. */
2870 static tree
2871 cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
2872 bool *non_constant_p, bool *overflow_p,
2873 tree *jump_target)
2875 tree cond = TREE_OPERAND (t, 0);
2876 cond = cxx_eval_constant_expression (ctx, cond, false,
2877 non_constant_p, overflow_p);
2878 VERIFY_CONSTANT (cond);
2879 *jump_target = cond;
2881 tree body = TREE_OPERAND (t, 1);
2882 cxx_eval_statement_list (ctx, body,
2883 non_constant_p, overflow_p, jump_target);
2884 if (breaks (jump_target) || switches (jump_target))
2885 *jump_target = NULL_TREE;
2886 return NULL_TREE;
2889 /* Attempt to reduce the expression T to a constant value.
2890 On failure, issue diagnostic and return error_mark_node. */
2891 /* FIXME unify with c_fully_fold */
2892 /* FIXME overflow_p is too global */
2894 static tree
2895 cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
2896 bool lval,
2897 bool *non_constant_p, bool *overflow_p,
2898 tree *jump_target)
2900 constexpr_ctx new_ctx;
2901 tree r = t;
2903 if (t == error_mark_node)
2905 *non_constant_p = true;
2906 return t;
2908 if (CONSTANT_CLASS_P (t))
2910 if (TREE_CODE (t) == PTRMEM_CST)
2911 t = cplus_expand_constant (t);
2912 else if (TREE_OVERFLOW (t) && (!flag_permissive || ctx->quiet))
2913 *overflow_p = true;
2914 return t;
2916 if (TREE_CODE (t) != NOP_EXPR
2917 && reduced_constant_expression_p (t))
2918 return fold (t);
2920 switch (TREE_CODE (t))
2922 case RESULT_DECL:
2923 if (lval)
2924 return t;
2925 /* We ask for an rvalue for the RESULT_DECL when indirecting
2926 through an invisible reference. */
2927 gcc_assert (DECL_BY_REFERENCE (t));
2928 return (*ctx->values->get (t));
2930 case VAR_DECL:
2931 if (lval)
2932 return t;
2933 /* else fall through. */
2934 case CONST_DECL:
2935 if (ctx->strict)
2936 r = decl_really_constant_value (t);
2937 else
2938 r = decl_constant_value (t);
2939 if (TREE_CODE (r) == TARGET_EXPR
2940 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
2941 r = TARGET_EXPR_INITIAL (r);
2942 if (TREE_CODE (r) == VAR_DECL)
2943 if (tree *p = ctx->values->get (r))
2944 r = *p;
2945 if (DECL_P (r))
2947 if (!ctx->quiet)
2948 non_const_var_error (r);
2949 *non_constant_p = true;
2951 break;
2953 case FUNCTION_DECL:
2954 case TEMPLATE_DECL:
2955 case LABEL_DECL:
2956 case LABEL_EXPR:
2957 case CASE_LABEL_EXPR:
2958 return t;
2960 case PARM_DECL:
2961 if (!use_new_call && ctx
2962 && ctx->call && DECL_CONTEXT (t) == ctx->call->fundef->decl)
2963 r = lookup_parameter_binding (ctx->call, t);
2964 else if (lval && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
2965 /* glvalue use. */;
2966 else if (tree *p = ctx->values->get (r))
2967 r = *p;
2968 else if (lval)
2969 /* Defer in case this is only used for its type. */;
2970 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
2971 /* Defer, there's no lvalue->rvalue conversion. */;
2972 else if (is_empty_class (TREE_TYPE (t)))
2974 /* If the class is empty, we aren't actually loading anything. */
2975 r = build_constructor (TREE_TYPE (t), NULL);
2976 TREE_CONSTANT (r) = true;
2978 else
2980 if (!ctx->quiet)
2981 error ("%qE is not a constant expression", t);
2982 *non_constant_p = true;
2984 break;
2986 case CALL_EXPR:
2987 case AGGR_INIT_EXPR:
2988 r = cxx_eval_call_expression (ctx, t, lval,
2989 non_constant_p, overflow_p);
2990 break;
2992 case DECL_EXPR:
2994 r = DECL_EXPR_DECL (t);
2995 if (AGGREGATE_TYPE_P (TREE_TYPE (r))
2996 || VECTOR_TYPE_P (TREE_TYPE (r)))
2998 new_ctx = *ctx;
2999 new_ctx.object = r;
3000 new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
3001 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
3002 new_ctx.values->put (r, new_ctx.ctor);
3003 ctx = &new_ctx;
3006 if (tree init = DECL_INITIAL (r))
3008 init = cxx_eval_constant_expression (ctx, init,
3009 false,
3010 non_constant_p, overflow_p);
3011 ctx->values->put (r, init);
3013 else if (ctx == &new_ctx)
3014 /* We gave it a CONSTRUCTOR above. */;
3015 else
3016 ctx->values->put (r, NULL_TREE);
3018 break;
3020 case TARGET_EXPR:
3021 if (!literal_type_p (TREE_TYPE (t)))
3023 if (!ctx->quiet)
3025 error ("temporary of non-literal type %qT in a "
3026 "constant expression", TREE_TYPE (t));
3027 explain_non_literal_class (TREE_TYPE (t));
3029 *non_constant_p = true;
3030 break;
3032 if ((AGGREGATE_TYPE_P (TREE_TYPE (t)) || VECTOR_TYPE_P (TREE_TYPE (t))))
3034 /* We're being expanded without an explicit target, so start
3035 initializing a new object; expansion with an explicit target
3036 strips the TARGET_EXPR before we get here. */
3037 new_ctx = *ctx;
3038 new_ctx.ctor = build_constructor (TREE_TYPE (t), NULL);
3039 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
3040 new_ctx.object = TARGET_EXPR_SLOT (t);
3041 ctx->values->put (new_ctx.object, new_ctx.ctor);
3042 ctx = &new_ctx;
3044 /* Pass false for 'lval' because this indicates
3045 initialization of a temporary. */
3046 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3047 false,
3048 non_constant_p, overflow_p);
3049 if (!*non_constant_p)
3050 /* Adjust the type of the result to the type of the temporary. */
3051 r = adjust_temp_type (TREE_TYPE (t), r);
3052 if (lval)
3054 tree slot = TARGET_EXPR_SLOT (t);
3055 ctx->values->put (slot, r);
3056 return slot;
3058 break;
3060 case INIT_EXPR:
3061 if (!use_new_call)
3063 /* In C++11 constexpr evaluation we are looking for the value,
3064 not the side-effect of the initialization. */
3065 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3066 false,
3067 non_constant_p, overflow_p);
3068 break;
3070 /* else fall through */
3071 case MODIFY_EXPR:
3072 r = cxx_eval_store_expression (ctx, t, lval,
3073 non_constant_p, overflow_p);
3074 break;
3076 case SCOPE_REF:
3077 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3078 lval,
3079 non_constant_p, overflow_p);
3080 break;
3082 case RETURN_EXPR:
3083 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3084 lval,
3085 non_constant_p, overflow_p);
3086 *jump_target = t;
3087 break;
3089 case SAVE_EXPR:
3090 /* Avoid evaluating a SAVE_EXPR more than once. */
3091 if (tree *p = ctx->values->get (t))
3092 r = *p;
3093 else
3095 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false,
3096 non_constant_p, overflow_p);
3097 ctx->values->put (t, r);
3099 break;
3101 case NON_LVALUE_EXPR:
3102 case TRY_CATCH_EXPR:
3103 case CLEANUP_POINT_EXPR:
3104 case MUST_NOT_THROW_EXPR:
3105 case EXPR_STMT:
3106 case EH_SPEC_BLOCK:
3107 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3108 lval,
3109 non_constant_p, overflow_p,
3110 jump_target);
3111 break;
3113 /* These differ from cxx_eval_unary_expression in that this doesn't
3114 check for a constant operand or result; an address can be
3115 constant without its operand being, and vice versa. */
3116 case INDIRECT_REF:
3117 r = cxx_eval_indirect_ref (ctx, t, lval,
3118 non_constant_p, overflow_p);
3119 break;
3121 case ADDR_EXPR:
3123 tree oldop = TREE_OPERAND (t, 0);
3124 tree op = cxx_eval_constant_expression (ctx, oldop,
3125 /*lval*/true,
3126 non_constant_p, overflow_p);
3127 /* Don't VERIFY_CONSTANT here. */
3128 if (*non_constant_p)
3129 return t;
3130 gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
3131 /* This function does more aggressive folding than fold itself. */
3132 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
3133 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
3134 return t;
3135 break;
3138 case REALPART_EXPR:
3139 case IMAGPART_EXPR:
3140 case CONJ_EXPR:
3141 case FIX_TRUNC_EXPR:
3142 case FLOAT_EXPR:
3143 case NEGATE_EXPR:
3144 case ABS_EXPR:
3145 case BIT_NOT_EXPR:
3146 case TRUTH_NOT_EXPR:
3147 case FIXED_CONVERT_EXPR:
3148 r = cxx_eval_unary_expression (ctx, t, lval,
3149 non_constant_p, overflow_p);
3150 break;
3152 case SIZEOF_EXPR:
3153 if (SIZEOF_EXPR_TYPE_P (t))
3154 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
3155 SIZEOF_EXPR, false);
3156 else if (TYPE_P (TREE_OPERAND (t, 0)))
3157 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
3158 false);
3159 else
3160 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
3161 false);
3162 if (r == error_mark_node)
3163 r = size_one_node;
3164 VERIFY_CONSTANT (r);
3165 break;
3167 case COMPOUND_EXPR:
3169 /* check_return_expr sometimes wraps a TARGET_EXPR in a
3170 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
3171 introduced by build_call_a. */
3172 tree op0 = TREE_OPERAND (t, 0);
3173 tree op1 = TREE_OPERAND (t, 1);
3174 STRIP_NOPS (op1);
3175 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
3176 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
3177 r = cxx_eval_constant_expression (ctx, op0,
3178 lval, non_constant_p, overflow_p,
3179 jump_target);
3180 else
3182 /* Check that the LHS is constant and then discard it. */
3183 cxx_eval_constant_expression (ctx, op0,
3184 true, non_constant_p, overflow_p,
3185 jump_target);
3186 op1 = TREE_OPERAND (t, 1);
3187 r = cxx_eval_constant_expression (ctx, op1,
3188 lval, non_constant_p, overflow_p,
3189 jump_target);
3192 break;
3194 case POINTER_PLUS_EXPR:
3195 case PLUS_EXPR:
3196 case MINUS_EXPR:
3197 case MULT_EXPR:
3198 case TRUNC_DIV_EXPR:
3199 case CEIL_DIV_EXPR:
3200 case FLOOR_DIV_EXPR:
3201 case ROUND_DIV_EXPR:
3202 case TRUNC_MOD_EXPR:
3203 case CEIL_MOD_EXPR:
3204 case ROUND_MOD_EXPR:
3205 case RDIV_EXPR:
3206 case EXACT_DIV_EXPR:
3207 case MIN_EXPR:
3208 case MAX_EXPR:
3209 case LSHIFT_EXPR:
3210 case RSHIFT_EXPR:
3211 case LROTATE_EXPR:
3212 case RROTATE_EXPR:
3213 case BIT_IOR_EXPR:
3214 case BIT_XOR_EXPR:
3215 case BIT_AND_EXPR:
3216 case TRUTH_XOR_EXPR:
3217 case LT_EXPR:
3218 case LE_EXPR:
3219 case GT_EXPR:
3220 case GE_EXPR:
3221 case EQ_EXPR:
3222 case NE_EXPR:
3223 case UNORDERED_EXPR:
3224 case ORDERED_EXPR:
3225 case UNLT_EXPR:
3226 case UNLE_EXPR:
3227 case UNGT_EXPR:
3228 case UNGE_EXPR:
3229 case UNEQ_EXPR:
3230 case LTGT_EXPR:
3231 case RANGE_EXPR:
3232 case COMPLEX_EXPR:
3233 r = cxx_eval_binary_expression (ctx, t, lval,
3234 non_constant_p, overflow_p);
3235 break;
3237 /* fold can introduce non-IF versions of these; still treat them as
3238 short-circuiting. */
3239 case TRUTH_AND_EXPR:
3240 case TRUTH_ANDIF_EXPR:
3241 r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
3242 boolean_true_node,
3243 lval,
3244 non_constant_p, overflow_p);
3245 break;
3247 case TRUTH_OR_EXPR:
3248 case TRUTH_ORIF_EXPR:
3249 r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
3250 boolean_false_node,
3251 lval,
3252 non_constant_p, overflow_p);
3253 break;
3255 case ARRAY_REF:
3256 r = cxx_eval_array_reference (ctx, t, lval,
3257 non_constant_p, overflow_p);
3258 break;
3260 case COMPONENT_REF:
3261 if (is_overloaded_fn (t))
3263 /* We can only get here in checking mode via
3264 build_non_dependent_expr, because any expression that
3265 calls or takes the address of the function will have
3266 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
3267 gcc_checking_assert (ctx->quiet || errorcount);
3268 *non_constant_p = true;
3269 return t;
3271 r = cxx_eval_component_reference (ctx, t, lval,
3272 non_constant_p, overflow_p);
3273 break;
3275 case BIT_FIELD_REF:
3276 r = cxx_eval_bit_field_ref (ctx, t, lval,
3277 non_constant_p, overflow_p);
3278 break;
3280 case COND_EXPR:
3281 case VEC_COND_EXPR:
3282 r = cxx_eval_conditional_expression (ctx, t, lval,
3283 non_constant_p, overflow_p,
3284 jump_target);
3285 break;
3287 case CONSTRUCTOR:
3288 r = cxx_eval_bare_aggregate (ctx, t, lval,
3289 non_constant_p, overflow_p);
3290 break;
3292 case VEC_INIT_EXPR:
3293 /* We can get this in a defaulted constructor for a class with a
3294 non-static data member of array type. Either the initializer will
3295 be NULL, meaning default-initialization, or it will be an lvalue
3296 or xvalue of the same type, meaning direct-initialization from the
3297 corresponding member. */
3298 r = cxx_eval_vec_init (ctx, t, lval,
3299 non_constant_p, overflow_p);
3300 break;
3302 case FMA_EXPR:
3303 case VEC_PERM_EXPR:
3304 r = cxx_eval_trinary_expression (ctx, t, lval,
3305 non_constant_p, overflow_p);
3306 break;
3308 case CONVERT_EXPR:
3309 case VIEW_CONVERT_EXPR:
3310 case NOP_EXPR:
3312 tree oldop = TREE_OPERAND (t, 0);
3313 tree op = cxx_eval_constant_expression (ctx, oldop,
3314 lval,
3315 non_constant_p, overflow_p);
3316 if (*non_constant_p)
3317 return t;
3318 if (POINTER_TYPE_P (TREE_TYPE (t))
3319 && TREE_CODE (op) == INTEGER_CST
3320 && !integer_zerop (op))
3322 if (!ctx->quiet)
3323 error_at (EXPR_LOC_OR_LOC (t, input_location),
3324 "reinterpret_cast from integer to pointer");
3325 *non_constant_p = true;
3326 return t;
3328 if (op == oldop)
3329 /* We didn't fold at the top so we could check for ptr-int
3330 conversion. */
3331 return fold (t);
3332 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), op);
3333 /* Conversion of an out-of-range value has implementation-defined
3334 behavior; the language considers it different from arithmetic
3335 overflow, which is undefined. */
3336 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
3337 TREE_OVERFLOW (r) = false;
3339 break;
3341 case EMPTY_CLASS_EXPR:
3342 /* This is good enough for a function argument that might not get
3343 used, and they can't do anything with it, so just return it. */
3344 return t;
3346 case STATEMENT_LIST:
3347 new_ctx = *ctx;
3348 new_ctx.ctor = new_ctx.object = NULL_TREE;
3349 return cxx_eval_statement_list (&new_ctx, t,
3350 non_constant_p, overflow_p, jump_target);
3352 case BIND_EXPR:
3353 return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
3354 lval,
3355 non_constant_p, overflow_p,
3356 jump_target);
3358 case PREINCREMENT_EXPR:
3359 case POSTINCREMENT_EXPR:
3360 case PREDECREMENT_EXPR:
3361 case POSTDECREMENT_EXPR:
3362 return cxx_eval_increment_expression (ctx, t,
3363 lval, non_constant_p, overflow_p);
3365 case LAMBDA_EXPR:
3366 case NEW_EXPR:
3367 case VEC_NEW_EXPR:
3368 case DELETE_EXPR:
3369 case VEC_DELETE_EXPR:
3370 case THROW_EXPR:
3371 case MODOP_EXPR:
3372 /* GCC internal stuff. */
3373 case VA_ARG_EXPR:
3374 case OBJ_TYPE_REF:
3375 case WITH_CLEANUP_EXPR:
3376 case NON_DEPENDENT_EXPR:
3377 case BASELINK:
3378 case OFFSET_REF:
3379 if (!ctx->quiet)
3380 error_at (EXPR_LOC_OR_LOC (t, input_location),
3381 "expression %qE is not a constant-expression", t);
3382 *non_constant_p = true;
3383 break;
3385 case PLACEHOLDER_EXPR:
3386 if (!ctx || !ctx->ctor || (lval && !ctx->object))
3388 /* A placeholder without a referent. We can get here when
3389 checking whether NSDMIs are noexcept, or in massage_init_elt;
3390 just say it's non-constant for now. */
3391 gcc_assert (ctx->quiet);
3392 *non_constant_p = true;
3393 break;
3395 else
3397 /* Use of the value or address of the current object. We could
3398 use ctx->object unconditionally, but using ctx->ctor when we
3399 can is a minor optimization. */
3400 tree ctor = lval ? ctx->object : ctx->ctor;
3401 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3402 (TREE_TYPE (t), TREE_TYPE (ctor)));
3403 return cxx_eval_constant_expression
3404 (ctx, ctor, lval,
3405 non_constant_p, overflow_p);
3407 break;
3409 case GOTO_EXPR:
3410 *jump_target = TREE_OPERAND (t, 0);
3411 gcc_assert (breaks (jump_target) || continues (jump_target));
3412 break;
3414 case LOOP_EXPR:
3415 cxx_eval_loop_expr (ctx, t,
3416 non_constant_p, overflow_p, jump_target);
3417 break;
3419 case SWITCH_EXPR:
3420 cxx_eval_switch_expr (ctx, t,
3421 non_constant_p, overflow_p, jump_target);
3422 break;
3424 default:
3425 internal_error ("unexpected expression %qE of kind %s", t,
3426 get_tree_code_name (TREE_CODE (t)));
3427 *non_constant_p = true;
3428 break;
3431 if (r == error_mark_node)
3432 *non_constant_p = true;
3434 if (*non_constant_p)
3435 return t;
3436 else
3437 return r;
3440 static tree
3441 cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
3442 bool strict = true, tree object = NULL_TREE)
3444 bool non_constant_p = false;
3445 bool overflow_p = false;
3446 hash_map<tree,tree> map;
3447 constexpr_ctx ctx = { NULL, &map, NULL, NULL, allow_non_constant, strict };
3448 tree type = initialized_type (t);
3449 tree r = t;
3450 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
3452 /* In C++14 an NSDMI can participate in aggregate initialization,
3453 and can refer to the address of the object being initialized, so
3454 we need to pass in the relevant VAR_DECL if we want to do the
3455 evaluation in a single pass. The evaluation will dynamically
3456 update ctx.values for the VAR_DECL. We use the same strategy
3457 for C++11 constexpr constructors that refer to the object being
3458 initialized. */
3459 ctx.ctor = build_constructor (type, NULL);
3460 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctx.ctor) = true;
3461 if (!object)
3463 if (TREE_CODE (t) == TARGET_EXPR)
3464 object = TARGET_EXPR_SLOT (t);
3465 else if (TREE_CODE (t) == AGGR_INIT_EXPR)
3466 object = AGGR_INIT_EXPR_SLOT (t);
3468 ctx.object = object;
3469 if (object)
3470 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3471 (type, TREE_TYPE (object)));
3472 if (object && DECL_P (object))
3473 map.put (object, ctx.ctor);
3474 if (TREE_CODE (r) == TARGET_EXPR)
3475 /* Avoid creating another CONSTRUCTOR when we expand the
3476 TARGET_EXPR. */
3477 r = TARGET_EXPR_INITIAL (r);
3480 r = cxx_eval_constant_expression (&ctx, r,
3481 false, &non_constant_p, &overflow_p);
3483 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
3485 /* Mutable logic is a bit tricky: we want to allow initialization of
3486 constexpr variables with mutable members, but we can't copy those
3487 members to another constexpr variable. */
3488 if (TREE_CODE (r) == CONSTRUCTOR
3489 && CONSTRUCTOR_MUTABLE_POISON (r))
3491 if (!allow_non_constant)
3492 error ("%qE is not a constant expression because it refers to "
3493 "mutable subobjects of %qT", t, type);
3494 non_constant_p = true;
3497 /* Technically we should check this for all subexpressions, but that
3498 runs into problems with our internal representation of pointer
3499 subtraction and the 5.19 rules are still in flux. */
3500 if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
3501 && ARITHMETIC_TYPE_P (TREE_TYPE (r))
3502 && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
3504 if (!allow_non_constant)
3505 error ("conversion from pointer type %qT "
3506 "to arithmetic type %qT in a constant-expression",
3507 TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
3508 non_constant_p = true;
3511 if (!non_constant_p && overflow_p)
3512 non_constant_p = true;
3514 if (non_constant_p && !allow_non_constant)
3515 return error_mark_node;
3516 else if (non_constant_p && TREE_CONSTANT (r))
3518 /* This isn't actually constant, so unset TREE_CONSTANT. */
3519 if (EXPR_P (r))
3520 r = copy_node (r);
3521 else if (TREE_CODE (r) == CONSTRUCTOR)
3522 r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
3523 else
3524 r = build_nop (TREE_TYPE (r), r);
3525 TREE_CONSTANT (r) = false;
3527 else if (non_constant_p || r == t)
3528 return t;
3530 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
3532 if (TREE_CODE (t) == TARGET_EXPR
3533 && TARGET_EXPR_INITIAL (t) == r)
3534 return t;
3535 else
3537 r = get_target_expr (r);
3538 TREE_CONSTANT (r) = true;
3539 return r;
3542 else
3543 return r;
3546 /* Returns true if T is a valid subexpression of a constant expression,
3547 even if it isn't itself a constant expression. */
3549 bool
3550 is_sub_constant_expr (tree t)
3552 bool non_constant_p = false;
3553 bool overflow_p = false;
3554 hash_map <tree, tree> map;
3555 constexpr_ctx ctx = { NULL, &map, NULL, NULL, true, true };
3556 cxx_eval_constant_expression (&ctx, t, false, &non_constant_p,
3557 &overflow_p);
3558 return !non_constant_p && !overflow_p;
3561 /* If T represents a constant expression returns its reduced value.
3562 Otherwise return error_mark_node. If T is dependent, then
3563 return NULL. */
3565 tree
3566 cxx_constant_value (tree t, tree decl)
3568 return cxx_eval_outermost_constant_expr (t, false, true, decl);
3571 /* If T is a constant expression, returns its reduced value.
3572 Otherwise, if T does not have TREE_CONSTANT set, returns T.
3573 Otherwise, returns a version of T without TREE_CONSTANT. */
3575 tree
3576 maybe_constant_value (tree t, tree decl)
3578 tree r;
3580 if (instantiation_dependent_expression_p (t)
3581 || type_unknown_p (t)
3582 || BRACE_ENCLOSED_INITIALIZER_P (t)
3583 || !potential_constant_expression (t))
3585 if (TREE_OVERFLOW_P (t))
3587 t = build_nop (TREE_TYPE (t), t);
3588 TREE_CONSTANT (t) = false;
3590 return t;
3593 r = cxx_eval_outermost_constant_expr (t, true, true, decl);
3594 #ifdef ENABLE_CHECKING
3595 /* cp_tree_equal looks through NOPs, so allow them. */
3596 gcc_assert (r == t
3597 || CONVERT_EXPR_P (t)
3598 || TREE_CODE (t) == VIEW_CONVERT_EXPR
3599 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
3600 || !cp_tree_equal (r, t));
3601 #endif
3602 return r;
3605 /* Like maybe_constant_value but first fully instantiate the argument.
3607 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
3608 (t, tf_none) followed by maybe_constant_value but is more efficient,
3609 because calls instantiation_dependent_expression_p and
3610 potential_constant_expression at most once. */
3612 tree
3613 fold_non_dependent_expr (tree t)
3615 if (t == NULL_TREE)
3616 return NULL_TREE;
3618 /* If we're in a template, but T isn't value dependent, simplify
3619 it. We're supposed to treat:
3621 template <typename T> void f(T[1 + 1]);
3622 template <typename T> void f(T[2]);
3624 as two declarations of the same function, for example. */
3625 if (processing_template_decl)
3627 if (!instantiation_dependent_expression_p (t)
3628 && potential_constant_expression (t))
3630 processing_template_decl_sentinel s;
3631 t = instantiate_non_dependent_expr_internal (t, tf_none);
3633 if (type_unknown_p (t)
3634 || BRACE_ENCLOSED_INITIALIZER_P (t))
3636 if (TREE_OVERFLOW_P (t))
3638 t = build_nop (TREE_TYPE (t), t);
3639 TREE_CONSTANT (t) = false;
3641 return t;
3644 tree r = cxx_eval_outermost_constant_expr (t, true, true, NULL_TREE);
3645 #ifdef ENABLE_CHECKING
3646 /* cp_tree_equal looks through NOPs, so allow them. */
3647 gcc_assert (r == t
3648 || CONVERT_EXPR_P (t)
3649 || TREE_CODE (t) == VIEW_CONVERT_EXPR
3650 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
3651 || !cp_tree_equal (r, t));
3652 #endif
3653 return r;
3655 else if (TREE_OVERFLOW_P (t))
3657 t = build_nop (TREE_TYPE (t), t);
3658 TREE_CONSTANT (t) = false;
3660 return t;
3663 return maybe_constant_value (t);
3666 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
3667 than wrapped in a TARGET_EXPR. */
3669 tree
3670 maybe_constant_init (tree t, tree decl)
3672 if (TREE_CODE (t) == EXPR_STMT)
3673 t = TREE_OPERAND (t, 0);
3674 if (TREE_CODE (t) == CONVERT_EXPR
3675 && VOID_TYPE_P (TREE_TYPE (t)))
3676 t = TREE_OPERAND (t, 0);
3677 if (TREE_CODE (t) == INIT_EXPR)
3678 t = TREE_OPERAND (t, 1);
3679 if (instantiation_dependent_expression_p (t)
3680 || type_unknown_p (t)
3681 || BRACE_ENCLOSED_INITIALIZER_P (t)
3682 || !potential_static_init_expression (t))
3683 /* Don't try to evaluate it. */;
3684 else
3685 t = cxx_eval_outermost_constant_expr (t, true, false, decl);
3686 if (TREE_CODE (t) == TARGET_EXPR)
3688 tree init = TARGET_EXPR_INITIAL (t);
3689 if (TREE_CODE (init) == CONSTRUCTOR)
3690 t = init;
3692 return t;
3695 #if 0
3696 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
3697 /* Return true if the object referred to by REF has automatic or thread
3698 local storage. */
3700 enum { ck_ok, ck_bad, ck_unknown };
3701 static int
3702 check_automatic_or_tls (tree ref)
3704 machine_mode mode;
3705 HOST_WIDE_INT bitsize, bitpos;
3706 tree offset;
3707 int volatilep = 0, unsignedp = 0;
3708 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
3709 &mode, &unsignedp, &volatilep, false);
3710 duration_kind dk;
3712 /* If there isn't a decl in the middle, we don't know the linkage here,
3713 and this isn't a constant expression anyway. */
3714 if (!DECL_P (decl))
3715 return ck_unknown;
3716 dk = decl_storage_duration (decl);
3717 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
3719 #endif
3721 /* Return true if T denotes a potentially constant expression. Issue
3722 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
3723 an lvalue-rvalue conversion is implied.
3725 C++0x [expr.const] used to say
3727 6 An expression is a potential constant expression if it is
3728 a constant expression where all occurrences of function
3729 parameters are replaced by arbitrary constant expressions
3730 of the appropriate type.
3732 2 A conditional expression is a constant expression unless it
3733 involves one of the following as a potentially evaluated
3734 subexpression (3.2), but subexpressions of logical AND (5.14),
3735 logical OR (5.15), and conditional (5.16) operations that are
3736 not evaluated are not considered. */
3738 static bool
3739 potential_constant_expression_1 (tree t, bool want_rval, bool strict,
3740 tsubst_flags_t flags)
3742 #define RECUR(T,RV) potential_constant_expression_1 ((T), (RV), strict, flags)
3743 enum { any = false, rval = true };
3744 int i;
3745 tree tmp;
3747 if (t == error_mark_node)
3748 return false;
3749 if (t == NULL_TREE)
3750 return true;
3751 if (TREE_THIS_VOLATILE (t))
3753 if (flags & tf_error)
3754 error ("expression %qE has side-effects", t);
3755 return false;
3757 if (CONSTANT_CLASS_P (t))
3758 return true;
3760 switch (TREE_CODE (t))
3762 case FUNCTION_DECL:
3763 case BASELINK:
3764 case TEMPLATE_DECL:
3765 case OVERLOAD:
3766 case TEMPLATE_ID_EXPR:
3767 case LABEL_DECL:
3768 case LABEL_EXPR:
3769 case CASE_LABEL_EXPR:
3770 case CONST_DECL:
3771 case SIZEOF_EXPR:
3772 case ALIGNOF_EXPR:
3773 case OFFSETOF_EXPR:
3774 case NOEXCEPT_EXPR:
3775 case TEMPLATE_PARM_INDEX:
3776 case TRAIT_EXPR:
3777 case IDENTIFIER_NODE:
3778 case USERDEF_LITERAL:
3779 /* We can see a FIELD_DECL in a pointer-to-member expression. */
3780 case FIELD_DECL:
3781 case PARM_DECL:
3782 case USING_DECL:
3783 case USING_STMT:
3784 case PLACEHOLDER_EXPR:
3785 case BREAK_STMT:
3786 case CONTINUE_STMT:
3787 return true;
3789 case AGGR_INIT_EXPR:
3790 case CALL_EXPR:
3791 /* -- an invocation of a function other than a constexpr function
3792 or a constexpr constructor. */
3794 tree fun = get_function_named_in_call (t);
3795 const int nargs = call_expr_nargs (t);
3796 i = 0;
3798 if (fun == NULL_TREE)
3800 /* fold_call_expr can't do anything with IFN calls. */
3801 if (flags & tf_error)
3802 error_at (EXPR_LOC_OR_LOC (t, input_location),
3803 "call to internal function");
3804 return false;
3806 if (is_overloaded_fn (fun))
3808 if (TREE_CODE (fun) == FUNCTION_DECL)
3810 if (builtin_valid_in_constant_expr_p (fun))
3811 return true;
3812 if (!DECL_DECLARED_CONSTEXPR_P (fun)
3813 /* Allow any built-in function; if the expansion
3814 isn't constant, we'll deal with that then. */
3815 && !is_builtin_fn (fun))
3817 if (flags & tf_error)
3819 error_at (EXPR_LOC_OR_LOC (t, input_location),
3820 "call to non-constexpr function %qD", fun);
3821 explain_invalid_constexpr_fn (fun);
3823 return false;
3825 /* A call to a non-static member function takes the address
3826 of the object as the first argument. But in a constant
3827 expression the address will be folded away, so look
3828 through it now. */
3829 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
3830 && !DECL_CONSTRUCTOR_P (fun))
3832 tree x = get_nth_callarg (t, 0);
3833 if (is_this_parameter (x))
3834 return true;
3835 else if (!RECUR (x, rval))
3836 return false;
3837 i = 1;
3840 else
3842 if (!RECUR (fun, true))
3843 return false;
3844 fun = get_first_fn (fun);
3846 /* Skip initial arguments to base constructors. */
3847 if (DECL_BASE_CONSTRUCTOR_P (fun))
3848 i = num_artificial_parms_for (fun);
3849 fun = DECL_ORIGIN (fun);
3851 else
3853 if (RECUR (fun, rval))
3854 /* Might end up being a constant function pointer. */;
3855 else
3856 return false;
3858 for (; i < nargs; ++i)
3860 tree x = get_nth_callarg (t, i);
3861 if (!RECUR (x, rval))
3862 return false;
3864 return true;
3867 case NON_LVALUE_EXPR:
3868 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
3869 -- an lvalue of integral type that refers to a non-volatile
3870 const variable or static data member initialized with
3871 constant expressions, or
3873 -- an lvalue of literal type that refers to non-volatile
3874 object defined with constexpr, or that refers to a
3875 sub-object of such an object; */
3876 return RECUR (TREE_OPERAND (t, 0), rval);
3878 case VAR_DECL:
3879 if (want_rval
3880 && !decl_constant_var_p (t)
3881 && (strict
3882 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
3883 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t))
3884 && !var_in_constexpr_fn (t)
3885 && !dependent_type_p (TREE_TYPE (t)))
3887 if (flags & tf_error)
3888 non_const_var_error (t);
3889 return false;
3891 return true;
3893 case NOP_EXPR:
3894 case CONVERT_EXPR:
3895 case VIEW_CONVERT_EXPR:
3896 /* -- a reinterpret_cast. FIXME not implemented, and this rule
3897 may change to something more specific to type-punning (DR 1312). */
3899 tree from = TREE_OPERAND (t, 0);
3900 if (POINTER_TYPE_P (TREE_TYPE (t))
3901 && TREE_CODE (from) == INTEGER_CST
3902 && !integer_zerop (from))
3904 if (flags & tf_error)
3905 error_at (EXPR_LOC_OR_LOC (t, input_location),
3906 "reinterpret_cast from integer to pointer");
3907 return false;
3909 return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
3912 case ADDR_EXPR:
3913 /* -- a unary operator & that is applied to an lvalue that
3914 designates an object with thread or automatic storage
3915 duration; */
3916 t = TREE_OPERAND (t, 0);
3918 if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
3919 /* A pointer-to-member constant. */
3920 return true;
3922 #if 0
3923 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
3924 any checking here, as we might dereference the pointer later. If
3925 we remove this code, also remove check_automatic_or_tls. */
3926 i = check_automatic_or_tls (t);
3927 if (i == ck_ok)
3928 return true;
3929 if (i == ck_bad)
3931 if (flags & tf_error)
3932 error ("address-of an object %qE with thread local or "
3933 "automatic storage is not a constant expression", t);
3934 return false;
3936 #endif
3937 return RECUR (t, any);
3939 case COMPONENT_REF:
3940 case BIT_FIELD_REF:
3941 case ARROW_EXPR:
3942 case OFFSET_REF:
3943 /* -- a class member access unless its postfix-expression is
3944 of literal type or of pointer to literal type. */
3945 /* This test would be redundant, as it follows from the
3946 postfix-expression being a potential constant expression. */
3947 return RECUR (TREE_OPERAND (t, 0), want_rval);
3949 case EXPR_PACK_EXPANSION:
3950 return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
3952 case INDIRECT_REF:
3954 tree x = TREE_OPERAND (t, 0);
3955 STRIP_NOPS (x);
3956 if (is_this_parameter (x))
3958 if (DECL_CONTEXT (x)
3959 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x)))
3961 if (flags & tf_error)
3962 error ("use of %<this%> in a constant expression");
3963 return false;
3965 return true;
3967 return RECUR (x, rval);
3970 case STATEMENT_LIST:
3972 tree_stmt_iterator i;
3973 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
3975 if (!RECUR (tsi_stmt (i), any))
3976 return false;
3978 return true;
3980 break;
3982 case MODIFY_EXPR:
3983 if (cxx_dialect < cxx14)
3984 goto fail;
3985 if (!RECUR (TREE_OPERAND (t, 0), any))
3986 return false;
3987 if (!RECUR (TREE_OPERAND (t, 1), rval))
3988 return false;
3989 return true;
3991 case MODOP_EXPR:
3992 if (cxx_dialect < cxx14)
3993 goto fail;
3994 if (!RECUR (TREE_OPERAND (t, 0), rval))
3995 return false;
3996 if (!RECUR (TREE_OPERAND (t, 2), rval))
3997 return false;
3998 return true;
4000 case IF_STMT:
4001 if (!RECUR (IF_COND (t), rval))
4002 return false;
4003 if (!RECUR (THEN_CLAUSE (t), any))
4004 return false;
4005 if (!RECUR (ELSE_CLAUSE (t), any))
4006 return false;
4007 return true;
4009 case DO_STMT:
4010 if (!RECUR (DO_COND (t), rval))
4011 return false;
4012 if (!RECUR (DO_BODY (t), any))
4013 return false;
4014 return true;
4016 case FOR_STMT:
4017 if (!RECUR (FOR_INIT_STMT (t), any))
4018 return false;
4019 if (!RECUR (FOR_COND (t), rval))
4020 return false;
4021 if (!RECUR (FOR_EXPR (t), any))
4022 return false;
4023 if (!RECUR (FOR_BODY (t), any))
4024 return false;
4025 return true;
4027 case WHILE_STMT:
4028 if (!RECUR (WHILE_COND (t), rval))
4029 return false;
4030 if (!RECUR (WHILE_BODY (t), any))
4031 return false;
4032 return true;
4034 case SWITCH_STMT:
4035 if (!RECUR (SWITCH_STMT_COND (t), rval))
4036 return false;
4037 if (!RECUR (SWITCH_STMT_BODY (t), any))
4038 return false;
4039 return true;
4041 case STMT_EXPR:
4042 return RECUR (STMT_EXPR_STMT (t), rval);
4044 case LAMBDA_EXPR:
4045 case DYNAMIC_CAST_EXPR:
4046 case PSEUDO_DTOR_EXPR:
4047 case NEW_EXPR:
4048 case VEC_NEW_EXPR:
4049 case DELETE_EXPR:
4050 case VEC_DELETE_EXPR:
4051 case THROW_EXPR:
4052 case OMP_ATOMIC:
4053 case OMP_ATOMIC_READ:
4054 case OMP_ATOMIC_CAPTURE_OLD:
4055 case OMP_ATOMIC_CAPTURE_NEW:
4056 /* GCC internal stuff. */
4057 case VA_ARG_EXPR:
4058 case OBJ_TYPE_REF:
4059 case TRANSACTION_EXPR:
4060 case ASM_EXPR:
4061 fail:
4062 if (flags & tf_error)
4063 error ("expression %qE is not a constant-expression", t);
4064 return false;
4066 case TYPEID_EXPR:
4067 /* -- a typeid expression whose operand is of polymorphic
4068 class type; */
4070 tree e = TREE_OPERAND (t, 0);
4071 if (!TYPE_P (e) && !type_dependent_expression_p (e)
4072 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
4074 if (flags & tf_error)
4075 error ("typeid-expression is not a constant expression "
4076 "because %qE is of polymorphic type", e);
4077 return false;
4079 return true;
4082 case MINUS_EXPR:
4083 /* -- a subtraction where both operands are pointers. */
4084 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
4085 && TYPE_PTR_P (TREE_OPERAND (t, 1)))
4087 if (flags & tf_error)
4088 error ("difference of two pointer expressions is not "
4089 "a constant expression");
4090 return false;
4092 want_rval = true;
4093 goto binary;
4095 case LT_EXPR:
4096 case LE_EXPR:
4097 case GT_EXPR:
4098 case GE_EXPR:
4099 case EQ_EXPR:
4100 case NE_EXPR:
4101 /* -- a relational or equality operator where at least
4102 one of the operands is a pointer. */
4103 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
4104 || TYPE_PTR_P (TREE_OPERAND (t, 1)))
4106 if (flags & tf_error)
4107 error ("pointer comparison expression is not a "
4108 "constant expression");
4109 return false;
4111 want_rval = true;
4112 goto binary;
4114 case PREINCREMENT_EXPR:
4115 case POSTINCREMENT_EXPR:
4116 case PREDECREMENT_EXPR:
4117 case POSTDECREMENT_EXPR:
4118 if (cxx_dialect < cxx14)
4119 goto fail;
4120 goto unary;
4122 case BIT_NOT_EXPR:
4123 /* A destructor. */
4124 if (TYPE_P (TREE_OPERAND (t, 0)))
4125 return true;
4126 /* else fall through. */
4128 case REALPART_EXPR:
4129 case IMAGPART_EXPR:
4130 case CONJ_EXPR:
4131 case SAVE_EXPR:
4132 case FIX_TRUNC_EXPR:
4133 case FLOAT_EXPR:
4134 case NEGATE_EXPR:
4135 case ABS_EXPR:
4136 case TRUTH_NOT_EXPR:
4137 case FIXED_CONVERT_EXPR:
4138 case UNARY_PLUS_EXPR:
4139 unary:
4140 return RECUR (TREE_OPERAND (t, 0), rval);
4142 case CAST_EXPR:
4143 case CONST_CAST_EXPR:
4144 case STATIC_CAST_EXPR:
4145 case REINTERPRET_CAST_EXPR:
4146 case IMPLICIT_CONV_EXPR:
4147 if (cxx_dialect < cxx11
4148 && !dependent_type_p (TREE_TYPE (t))
4149 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
4150 /* In C++98, a conversion to non-integral type can't be part of a
4151 constant expression. */
4153 if (flags & tf_error)
4154 error ("cast to non-integral type %qT in a constant expression",
4155 TREE_TYPE (t));
4156 return false;
4159 return (RECUR (TREE_OPERAND (t, 0),
4160 TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE));
4162 case BIND_EXPR:
4163 return RECUR (BIND_EXPR_BODY (t), want_rval);
4165 case WITH_CLEANUP_EXPR:
4166 case CLEANUP_POINT_EXPR:
4167 case MUST_NOT_THROW_EXPR:
4168 case TRY_CATCH_EXPR:
4169 case EH_SPEC_BLOCK:
4170 case EXPR_STMT:
4171 case PAREN_EXPR:
4172 case DECL_EXPR:
4173 case NON_DEPENDENT_EXPR:
4174 /* For convenience. */
4175 case RETURN_EXPR:
4176 return RECUR (TREE_OPERAND (t, 0), want_rval);
4178 case SCOPE_REF:
4179 return RECUR (TREE_OPERAND (t, 1), want_rval);
4181 case TARGET_EXPR:
4182 if (!literal_type_p (TREE_TYPE (t)))
4184 if (flags & tf_error)
4186 error ("temporary of non-literal type %qT in a "
4187 "constant expression", TREE_TYPE (t));
4188 explain_non_literal_class (TREE_TYPE (t));
4190 return false;
4192 case INIT_EXPR:
4193 return RECUR (TREE_OPERAND (t, 1), rval);
4195 case CONSTRUCTOR:
4197 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
4198 constructor_elt *ce;
4199 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
4200 if (!RECUR (ce->value, want_rval))
4201 return false;
4202 return true;
4205 case TREE_LIST:
4207 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
4208 || DECL_P (TREE_PURPOSE (t)));
4209 if (!RECUR (TREE_VALUE (t), want_rval))
4210 return false;
4211 if (TREE_CHAIN (t) == NULL_TREE)
4212 return true;
4213 return RECUR (TREE_CHAIN (t), want_rval);
4216 case TRUNC_DIV_EXPR:
4217 case CEIL_DIV_EXPR:
4218 case FLOOR_DIV_EXPR:
4219 case ROUND_DIV_EXPR:
4220 case TRUNC_MOD_EXPR:
4221 case CEIL_MOD_EXPR:
4222 case ROUND_MOD_EXPR:
4224 tree denom = TREE_OPERAND (t, 1);
4225 if (!RECUR (denom, rval))
4226 return false;
4227 /* We can't call cxx_eval_outermost_constant_expr on an expression
4228 that hasn't been through instantiate_non_dependent_expr yet. */
4229 if (!processing_template_decl)
4230 denom = cxx_eval_outermost_constant_expr (denom, true);
4231 if (integer_zerop (denom))
4233 if (flags & tf_error)
4234 error ("division by zero is not a constant-expression");
4235 return false;
4237 else
4239 want_rval = true;
4240 return RECUR (TREE_OPERAND (t, 0), want_rval);
4244 case COMPOUND_EXPR:
4246 /* check_return_expr sometimes wraps a TARGET_EXPR in a
4247 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
4248 introduced by build_call_a. */
4249 tree op0 = TREE_OPERAND (t, 0);
4250 tree op1 = TREE_OPERAND (t, 1);
4251 STRIP_NOPS (op1);
4252 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
4253 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
4254 return RECUR (op0, want_rval);
4255 else
4256 goto binary;
4259 /* If the first operand is the non-short-circuit constant, look at
4260 the second operand; otherwise we only care about the first one for
4261 potentiality. */
4262 case TRUTH_AND_EXPR:
4263 case TRUTH_ANDIF_EXPR:
4264 tmp = boolean_true_node;
4265 goto truth;
4266 case TRUTH_OR_EXPR:
4267 case TRUTH_ORIF_EXPR:
4268 tmp = boolean_false_node;
4269 truth:
4271 tree op = TREE_OPERAND (t, 0);
4272 if (!RECUR (op, rval))
4273 return false;
4274 if (!processing_template_decl)
4275 op = cxx_eval_outermost_constant_expr (op, true);
4276 if (tree_int_cst_equal (op, tmp))
4277 return RECUR (TREE_OPERAND (t, 1), rval);
4278 else
4279 return true;
4282 case PLUS_EXPR:
4283 case MULT_EXPR:
4284 case POINTER_PLUS_EXPR:
4285 case RDIV_EXPR:
4286 case EXACT_DIV_EXPR:
4287 case MIN_EXPR:
4288 case MAX_EXPR:
4289 case LSHIFT_EXPR:
4290 case RSHIFT_EXPR:
4291 case LROTATE_EXPR:
4292 case RROTATE_EXPR:
4293 case BIT_IOR_EXPR:
4294 case BIT_XOR_EXPR:
4295 case BIT_AND_EXPR:
4296 case TRUTH_XOR_EXPR:
4297 case UNORDERED_EXPR:
4298 case ORDERED_EXPR:
4299 case UNLT_EXPR:
4300 case UNLE_EXPR:
4301 case UNGT_EXPR:
4302 case UNGE_EXPR:
4303 case UNEQ_EXPR:
4304 case LTGT_EXPR:
4305 case RANGE_EXPR:
4306 case COMPLEX_EXPR:
4307 want_rval = true;
4308 /* Fall through. */
4309 case ARRAY_REF:
4310 case ARRAY_RANGE_REF:
4311 case MEMBER_REF:
4312 case DOTSTAR_EXPR:
4313 binary:
4314 for (i = 0; i < 2; ++i)
4315 if (!RECUR (TREE_OPERAND (t, i), want_rval))
4316 return false;
4317 return true;
4319 case CILK_SYNC_STMT:
4320 case CILK_SPAWN_STMT:
4321 case ARRAY_NOTATION_REF:
4322 return false;
4324 case FMA_EXPR:
4325 case VEC_PERM_EXPR:
4326 for (i = 0; i < 3; ++i)
4327 if (!RECUR (TREE_OPERAND (t, i), true))
4328 return false;
4329 return true;
4331 case COND_EXPR:
4332 case VEC_COND_EXPR:
4333 /* If the condition is a known constant, we know which of the legs we
4334 care about; otherwise we only require that the condition and
4335 either of the legs be potentially constant. */
4336 tmp = TREE_OPERAND (t, 0);
4337 if (!RECUR (tmp, rval))
4338 return false;
4339 if (!processing_template_decl)
4340 tmp = cxx_eval_outermost_constant_expr (tmp, true);
4341 if (integer_zerop (tmp))
4342 return RECUR (TREE_OPERAND (t, 2), want_rval);
4343 else if (TREE_CODE (tmp) == INTEGER_CST)
4344 return RECUR (TREE_OPERAND (t, 1), want_rval);
4345 for (i = 1; i < 3; ++i)
4346 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
4347 want_rval, strict, tf_none))
4348 return true;
4349 if (flags & tf_error)
4350 error ("expression %qE is not a constant-expression", t);
4351 return false;
4353 case VEC_INIT_EXPR:
4354 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
4355 return true;
4356 if (flags & tf_error)
4358 error ("non-constant array initialization");
4359 diagnose_non_constexpr_vec_init (t);
4361 return false;
4363 default:
4364 if (objc_is_property_ref (t))
4365 return false;
4367 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
4368 gcc_unreachable();
4369 return false;
4371 #undef RECUR
4374 /* The main entry point to the above. */
4376 bool
4377 potential_constant_expression (tree t)
4379 return potential_constant_expression_1 (t, false, true, tf_none);
4382 bool
4383 potential_static_init_expression (tree t)
4385 return potential_constant_expression_1 (t, false, false, tf_none);
4388 /* As above, but require a constant rvalue. */
4390 bool
4391 potential_rvalue_constant_expression (tree t)
4393 return potential_constant_expression_1 (t, true, true, tf_none);
4396 /* Like above, but complain about non-constant expressions. */
4398 bool
4399 require_potential_constant_expression (tree t)
4401 return potential_constant_expression_1 (t, false, true, tf_warning_or_error);
4404 /* Cross product of the above. */
4406 bool
4407 require_potential_rvalue_constant_expression (tree t)
4409 return potential_constant_expression_1 (t, true, true, tf_warning_or_error);
4412 #include "gt-cp-constexpr.h"