Fix g++.dg/torture/Wsizeof-pointer-memaccess2.C with -std=c++11
[official-gcc.git] / gcc / cp / constexpr.c
blob9ebb6406878e0863ff1607370ef457ce2f606f96
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-2015 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 "hash-set.h"
27 #include "machmode.h"
28 #include "vec.h"
29 #include "double-int.h"
30 #include "input.h"
31 #include "alias.h"
32 #include "symtab.h"
33 #include "options.h"
34 #include "wide-int.h"
35 #include "inchash.h"
36 #include "tree.h"
37 #include "varasm.h"
38 #include "cp-tree.h"
39 #include "c-family/c-objc.h"
40 #include "tree-iterator.h"
41 #include "gimplify.h"
42 #include "builtins.h"
43 #include "tree-inline.h"
44 #include "ubsan.h"
46 static bool verify_constant (tree, bool, bool *, bool *);
47 #define VERIFY_CONSTANT(X) \
48 do { \
49 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
50 return t; \
51 } while (0)
53 /* Returns true iff FUN is an instantiation of a constexpr function
54 template or a defaulted constexpr function. */
56 bool
57 is_instantiation_of_constexpr (tree fun)
59 return ((DECL_TEMPLOID_INSTANTIATION (fun)
60 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
61 || (DECL_DEFAULTED_FN (fun)
62 && DECL_DECLARED_CONSTEXPR_P (fun)));
65 /* Return true if T is a literal type. */
67 bool
68 literal_type_p (tree t)
70 if (SCALAR_TYPE_P (t)
71 || TREE_CODE (t) == VECTOR_TYPE
72 || TREE_CODE (t) == REFERENCE_TYPE
73 || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
74 return true;
75 if (CLASS_TYPE_P (t))
77 t = complete_type (t);
78 gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
79 return CLASSTYPE_LITERAL_P (t);
81 if (TREE_CODE (t) == ARRAY_TYPE)
82 return literal_type_p (strip_array_types (t));
83 return false;
86 /* If DECL is a variable declared `constexpr', require its type
87 be literal. Return the DECL if OK, otherwise NULL. */
89 tree
90 ensure_literal_type_for_constexpr_object (tree decl)
92 tree type = TREE_TYPE (decl);
93 if (VAR_P (decl)
94 && (DECL_DECLARED_CONSTEXPR_P (decl)
95 || var_in_constexpr_fn (decl))
96 && !processing_template_decl)
98 tree stype = strip_array_types (type);
99 if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype)))
100 /* Don't complain here, we'll complain about incompleteness
101 when we try to initialize the variable. */;
102 else if (!literal_type_p (type))
104 if (DECL_DECLARED_CONSTEXPR_P (decl))
106 error ("the type %qT of constexpr variable %qD is not literal",
107 type, decl);
108 explain_non_literal_class (type);
110 else
112 if (!DECL_TEMPLATE_INSTANTIATION (current_function_decl))
114 error ("variable %qD of non-literal type %qT in %<constexpr%> "
115 "function", decl, type);
116 explain_non_literal_class (type);
118 cp_function_chain->invalid_constexpr = true;
120 return NULL;
123 return decl;
126 /* Representation of entries in the constexpr function definition table. */
128 struct GTY((for_user)) constexpr_fundef {
129 tree decl;
130 tree body;
133 struct constexpr_fundef_hasher : ggc_hasher<constexpr_fundef *>
135 static hashval_t hash (constexpr_fundef *);
136 static bool equal (constexpr_fundef *, constexpr_fundef *);
139 /* This table holds all constexpr function definitions seen in
140 the current translation unit. */
142 static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table;
144 /* Utility function used for managing the constexpr function table.
145 Return true if the entries pointed to by P and Q are for the
146 same constexpr function. */
148 inline bool
149 constexpr_fundef_hasher::equal (constexpr_fundef *lhs, constexpr_fundef *rhs)
151 return lhs->decl == rhs->decl;
154 /* Utility function used for managing the constexpr function table.
155 Return a hash value for the entry pointed to by Q. */
157 inline hashval_t
158 constexpr_fundef_hasher::hash (constexpr_fundef *fundef)
160 return DECL_UID (fundef->decl);
163 /* Return a previously saved definition of function FUN. */
165 static constexpr_fundef *
166 retrieve_constexpr_fundef (tree fun)
168 constexpr_fundef fundef = { NULL, NULL };
169 if (constexpr_fundef_table == NULL)
170 return NULL;
172 fundef.decl = fun;
173 return constexpr_fundef_table->find (&fundef);
176 /* Check whether the parameter and return types of FUN are valid for a
177 constexpr function, and complain if COMPLAIN. */
179 static bool
180 is_valid_constexpr_fn (tree fun, bool complain)
182 bool ret = true;
184 if (DECL_INHERITED_CTOR_BASE (fun)
185 && TREE_CODE (fun) == TEMPLATE_DECL)
187 ret = false;
188 if (complain)
189 error ("inherited constructor %qD is not constexpr",
190 get_inherited_ctor (fun));
192 else
194 for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
195 parm != NULL_TREE; parm = TREE_CHAIN (parm))
196 if (!literal_type_p (TREE_TYPE (parm)))
198 ret = false;
199 if (complain)
201 error ("invalid type for parameter %d of constexpr "
202 "function %q+#D", DECL_PARM_INDEX (parm), fun);
203 explain_non_literal_class (TREE_TYPE (parm));
208 if (!DECL_CONSTRUCTOR_P (fun))
210 tree rettype = TREE_TYPE (TREE_TYPE (fun));
211 if (!literal_type_p (rettype))
213 ret = false;
214 if (complain)
216 error ("invalid return type %qT of constexpr function %q+D",
217 rettype, fun);
218 explain_non_literal_class (rettype);
222 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
223 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
225 ret = false;
226 if (complain)
228 error ("enclosing class of constexpr non-static member "
229 "function %q+#D is not a literal type", fun);
230 explain_non_literal_class (DECL_CONTEXT (fun));
234 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
236 ret = false;
237 if (complain)
238 error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
241 return ret;
244 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
245 for a member of an anonymous aggregate, INIT is the initializer for that
246 member, and VEC_OUTER is the vector of constructor elements for the class
247 whose constructor we are processing. Add the initializer to the vector
248 and return true to indicate success. */
250 static bool
251 build_anon_member_initialization (tree member, tree init,
252 vec<constructor_elt, va_gc> **vec_outer)
254 /* MEMBER presents the relevant fields from the inside out, but we need
255 to build up the initializer from the outside in so that we can reuse
256 previously built CONSTRUCTORs if this is, say, the second field in an
257 anonymous struct. So we use a vec as a stack. */
258 auto_vec<tree, 2> fields;
261 fields.safe_push (TREE_OPERAND (member, 1));
262 member = TREE_OPERAND (member, 0);
264 while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
265 && TREE_CODE (member) == COMPONENT_REF);
267 /* VEC has the constructor elements vector for the context of FIELD.
268 If FIELD is an anonymous aggregate, we will push inside it. */
269 vec<constructor_elt, va_gc> **vec = vec_outer;
270 tree field;
271 while (field = fields.pop(),
272 ANON_AGGR_TYPE_P (TREE_TYPE (field)))
274 tree ctor;
275 /* If there is already an outer constructor entry for the anonymous
276 aggregate FIELD, use it; otherwise, insert one. */
277 if (vec_safe_is_empty (*vec)
278 || (*vec)->last().index != field)
280 ctor = build_constructor (TREE_TYPE (field), NULL);
281 CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
283 else
284 ctor = (*vec)->last().value;
285 vec = &CONSTRUCTOR_ELTS (ctor);
288 /* Now we're at the innermost field, the one that isn't an anonymous
289 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
290 gcc_assert (fields.is_empty());
291 CONSTRUCTOR_APPEND_ELT (*vec, field, init);
293 return true;
296 /* Subroutine of build_constexpr_constructor_member_initializers.
297 The expression tree T represents a data member initialization
298 in a (constexpr) constructor definition. Build a pairing of
299 the data member with its initializer, and prepend that pair
300 to the existing initialization pair INITS. */
302 static bool
303 build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
305 tree member, init;
306 if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
307 t = TREE_OPERAND (t, 0);
308 if (TREE_CODE (t) == EXPR_STMT)
309 t = TREE_OPERAND (t, 0);
310 if (t == error_mark_node)
311 return false;
312 if (TREE_CODE (t) == STATEMENT_LIST)
314 tree_stmt_iterator i;
315 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
317 if (! build_data_member_initialization (tsi_stmt (i), vec))
318 return false;
320 return true;
322 if (TREE_CODE (t) == CLEANUP_STMT)
324 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
325 but we can in a constexpr constructor for a non-literal class. Just
326 ignore it; either all the initialization will be constant, in which
327 case the cleanup can't run, or it can't be constexpr.
328 Still recurse into CLEANUP_BODY. */
329 return build_data_member_initialization (CLEANUP_BODY (t), vec);
331 if (TREE_CODE (t) == CONVERT_EXPR)
332 t = TREE_OPERAND (t, 0);
333 if (TREE_CODE (t) == INIT_EXPR
334 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
335 use what this function builds for cx_check_missing_mem_inits, and
336 assignment in the ctor body doesn't count. */
337 || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR))
339 member = TREE_OPERAND (t, 0);
340 init = break_out_target_exprs (TREE_OPERAND (t, 1));
342 else if (TREE_CODE (t) == CALL_EXPR)
344 tree fn = get_callee_fndecl (t);
345 if (!fn || !DECL_CONSTRUCTOR_P (fn))
346 /* We're only interested in calls to subobject constructors. */
347 return true;
348 member = CALL_EXPR_ARG (t, 0);
349 /* We don't use build_cplus_new here because it complains about
350 abstract bases. Leaving the call unwrapped means that it has the
351 wrong type, but cxx_eval_constant_expression doesn't care. */
352 init = break_out_target_exprs (t);
354 else if (TREE_CODE (t) == BIND_EXPR)
355 return build_data_member_initialization (BIND_EXPR_BODY (t), vec);
356 else
357 /* Don't add anything else to the CONSTRUCTOR. */
358 return true;
359 if (INDIRECT_REF_P (member))
360 member = TREE_OPERAND (member, 0);
361 if (TREE_CODE (member) == NOP_EXPR)
363 tree op = member;
364 STRIP_NOPS (op);
365 if (TREE_CODE (op) == ADDR_EXPR)
367 gcc_assert (same_type_ignoring_top_level_qualifiers_p
368 (TREE_TYPE (TREE_TYPE (op)),
369 TREE_TYPE (TREE_TYPE (member))));
370 /* Initializing a cv-qualified member; we need to look through
371 the const_cast. */
372 member = op;
374 else if (op == current_class_ptr
375 && (same_type_ignoring_top_level_qualifiers_p
376 (TREE_TYPE (TREE_TYPE (member)),
377 current_class_type)))
378 /* Delegating constructor. */
379 member = op;
380 else
382 /* This is an initializer for an empty base; keep it for now so
383 we can check it in cxx_eval_bare_aggregate. */
384 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
387 if (TREE_CODE (member) == ADDR_EXPR)
388 member = TREE_OPERAND (member, 0);
389 if (TREE_CODE (member) == COMPONENT_REF)
391 tree aggr = TREE_OPERAND (member, 0);
392 if (TREE_CODE (aggr) != COMPONENT_REF)
393 /* Normal member initialization. */
394 member = TREE_OPERAND (member, 1);
395 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
396 /* Initializing a member of an anonymous union. */
397 return build_anon_member_initialization (member, init, vec);
398 else
399 /* We're initializing a vtable pointer in a base. Leave it as
400 COMPONENT_REF so we remember the path to get to the vfield. */
401 gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
404 CONSTRUCTOR_APPEND_ELT (*vec, member, init);
405 return true;
408 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
409 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
410 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
412 static bool
413 check_constexpr_bind_expr_vars (tree t)
415 gcc_assert (TREE_CODE (t) == BIND_EXPR);
417 for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
418 if (TREE_CODE (var) == TYPE_DECL
419 && DECL_IMPLICIT_TYPEDEF_P (var)
420 && !LAMBDA_TYPE_P (TREE_TYPE (var)))
421 return false;
422 return true;
425 /* Subroutine of check_constexpr_ctor_body. */
427 static bool
428 check_constexpr_ctor_body_1 (tree last, tree list)
430 switch (TREE_CODE (list))
432 case DECL_EXPR:
433 if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL)
434 return true;
435 return false;
437 case CLEANUP_POINT_EXPR:
438 return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
439 /*complain=*/false);
441 case BIND_EXPR:
442 if (!check_constexpr_bind_expr_vars (list)
443 || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
444 /*complain=*/false))
445 return false;
446 return true;
448 case USING_STMT:
449 case STATIC_ASSERT:
450 return true;
452 default:
453 return false;
457 /* Make sure that there are no statements after LAST in the constructor
458 body represented by LIST. */
460 bool
461 check_constexpr_ctor_body (tree last, tree list, bool complain)
463 /* C++14 doesn't require a constexpr ctor to have an empty body. */
464 if (cxx_dialect >= cxx14)
465 return true;
467 bool ok = true;
468 if (TREE_CODE (list) == STATEMENT_LIST)
470 tree_stmt_iterator i = tsi_last (list);
471 for (; !tsi_end_p (i); tsi_prev (&i))
473 tree t = tsi_stmt (i);
474 if (t == last)
475 break;
476 if (!check_constexpr_ctor_body_1 (last, t))
478 ok = false;
479 break;
483 else if (list != last
484 && !check_constexpr_ctor_body_1 (last, list))
485 ok = false;
486 if (!ok)
488 if (complain)
489 error ("constexpr constructor does not have empty body");
490 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
492 return ok;
495 /* V is a vector of constructor elements built up for the base and member
496 initializers of a constructor for TYPE. They need to be in increasing
497 offset order, which they might not be yet if TYPE has a primary base
498 which is not first in the base-clause or a vptr and at least one base
499 all of which are non-primary. */
501 static vec<constructor_elt, va_gc> *
502 sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
504 tree pri = CLASSTYPE_PRIMARY_BINFO (type);
505 tree field_type;
506 unsigned i;
507 constructor_elt *ce;
509 if (pri)
510 field_type = BINFO_TYPE (pri);
511 else if (TYPE_CONTAINS_VPTR_P (type))
512 field_type = vtbl_ptr_type_node;
513 else
514 return v;
516 /* Find the element for the primary base or vptr and move it to the
517 beginning of the vec. */
518 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
519 if (TREE_TYPE (ce->index) == field_type)
520 break;
522 if (i > 0 && i < vec_safe_length (v))
524 vec<constructor_elt, va_gc> &vref = *v;
525 constructor_elt elt = vref[i];
526 for (; i > 0; --i)
527 vref[i] = vref[i-1];
528 vref[0] = elt;
531 return v;
534 /* Build compile-time evalable representations of member-initializer list
535 for a constexpr constructor. */
537 static tree
538 build_constexpr_constructor_member_initializers (tree type, tree body)
540 vec<constructor_elt, va_gc> *vec = NULL;
541 bool ok = true;
542 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR
543 || TREE_CODE (body) == EH_SPEC_BLOCK)
544 body = TREE_OPERAND (body, 0);
545 if (TREE_CODE (body) == STATEMENT_LIST)
547 tree_stmt_iterator i = tsi_start (body);
548 while (true)
550 body = tsi_stmt (i);
551 if (TREE_CODE (body) == BIND_EXPR)
552 break;
553 tsi_next (&i);
556 body = BIND_EXPR_BODY (body);
557 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
559 body = TREE_OPERAND (body, 0);
560 if (TREE_CODE (body) == EXPR_STMT)
561 body = TREE_OPERAND (body, 0);
562 if (TREE_CODE (body) == INIT_EXPR
563 && (same_type_ignoring_top_level_qualifiers_p
564 (TREE_TYPE (TREE_OPERAND (body, 0)),
565 current_class_type)))
567 /* Trivial copy. */
568 return TREE_OPERAND (body, 1);
570 ok = build_data_member_initialization (body, &vec);
572 else if (TREE_CODE (body) == STATEMENT_LIST)
574 tree_stmt_iterator i;
575 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
577 ok = build_data_member_initialization (tsi_stmt (i), &vec);
578 if (!ok)
579 break;
582 else if (TREE_CODE (body) == TRY_BLOCK)
584 error ("body of %<constexpr%> constructor cannot be "
585 "a function-try-block");
586 return error_mark_node;
588 else if (EXPR_P (body))
589 ok = build_data_member_initialization (body, &vec);
590 else
591 gcc_assert (errorcount > 0);
592 if (ok)
594 if (vec_safe_length (vec) > 0)
596 /* In a delegating constructor, return the target. */
597 constructor_elt *ce = &(*vec)[0];
598 if (ce->index == current_class_ptr)
600 body = ce->value;
601 vec_free (vec);
602 return body;
605 vec = sort_constexpr_mem_initializers (type, vec);
606 return build_constructor (type, vec);
608 else
609 return error_mark_node;
612 /* Subroutine of register_constexpr_fundef. BODY is the body of a function
613 declared to be constexpr, or a sub-statement thereof. Returns the
614 return value if suitable, error_mark_node for a statement not allowed in
615 a constexpr function, or NULL_TREE if no return value was found. */
617 static tree
618 constexpr_fn_retval (tree body)
620 switch (TREE_CODE (body))
622 case STATEMENT_LIST:
624 tree_stmt_iterator i;
625 tree expr = NULL_TREE;
626 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
628 tree s = constexpr_fn_retval (tsi_stmt (i));
629 if (s == error_mark_node)
630 return error_mark_node;
631 else if (s == NULL_TREE)
632 /* Keep iterating. */;
633 else if (expr)
634 /* Multiple return statements. */
635 return error_mark_node;
636 else
637 expr = s;
639 return expr;
642 case RETURN_EXPR:
643 return break_out_target_exprs (TREE_OPERAND (body, 0));
645 case DECL_EXPR:
647 tree decl = DECL_EXPR_DECL (body);
648 if (TREE_CODE (decl) == USING_DECL
649 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
650 || DECL_ARTIFICIAL (decl))
651 return NULL_TREE;
652 return error_mark_node;
655 case CLEANUP_POINT_EXPR:
656 return constexpr_fn_retval (TREE_OPERAND (body, 0));
658 case BIND_EXPR:
659 if (!check_constexpr_bind_expr_vars (body))
660 return error_mark_node;
661 return constexpr_fn_retval (BIND_EXPR_BODY (body));
663 case USING_STMT:
664 return NULL_TREE;
666 default:
667 return error_mark_node;
671 /* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
672 FUN; do the necessary transformations to turn it into a single expression
673 that we can store in the hash table. */
675 static tree
676 massage_constexpr_body (tree fun, tree body)
678 if (DECL_CONSTRUCTOR_P (fun))
679 body = build_constexpr_constructor_member_initializers
680 (DECL_CONTEXT (fun), body);
681 else if (cxx_dialect < cxx14)
683 if (TREE_CODE (body) == EH_SPEC_BLOCK)
684 body = EH_SPEC_STMTS (body);
685 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
686 body = TREE_OPERAND (body, 0);
687 body = constexpr_fn_retval (body);
689 return body;
692 /* FUN is a constexpr constructor with massaged body BODY. Return true
693 if some bases/fields are uninitialized, and complain if COMPLAIN. */
695 static bool
696 cx_check_missing_mem_inits (tree fun, tree body, bool complain)
698 bool bad;
699 tree field;
700 unsigned i, nelts;
701 tree ctype;
703 if (TREE_CODE (body) != CONSTRUCTOR)
704 return false;
706 nelts = CONSTRUCTOR_NELTS (body);
707 ctype = DECL_CONTEXT (fun);
708 field = TYPE_FIELDS (ctype);
710 if (TREE_CODE (ctype) == UNION_TYPE)
712 if (nelts == 0 && next_initializable_field (field))
714 if (complain)
715 error ("%<constexpr%> constructor for union %qT must "
716 "initialize exactly one non-static data member", ctype);
717 return true;
719 return false;
722 bad = false;
723 for (i = 0; i <= nelts; ++i)
725 tree index;
726 if (i == nelts)
727 index = NULL_TREE;
728 else
730 index = CONSTRUCTOR_ELT (body, i)->index;
731 /* Skip base and vtable inits. */
732 if (TREE_CODE (index) != FIELD_DECL
733 || DECL_ARTIFICIAL (index))
734 continue;
736 for (; field != index; field = DECL_CHAIN (field))
738 tree ftype;
739 if (TREE_CODE (field) != FIELD_DECL
740 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
741 || DECL_ARTIFICIAL (field))
742 continue;
743 ftype = strip_array_types (TREE_TYPE (field));
744 if (type_has_constexpr_default_constructor (ftype))
746 /* It's OK to skip a member with a trivial constexpr ctor.
747 A constexpr ctor that isn't trivial should have been
748 added in by now. */
749 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
750 || errorcount != 0);
751 continue;
753 if (!complain)
754 return true;
755 error ("member %qD must be initialized by mem-initializer "
756 "in %<constexpr%> constructor", field);
757 inform (DECL_SOURCE_LOCATION (field), "declared here");
758 bad = true;
760 if (field == NULL_TREE)
761 break;
762 field = DECL_CHAIN (field);
765 return bad;
768 /* We are processing the definition of the constexpr function FUN.
769 Check that its BODY fulfills the propriate requirements and
770 enter it in the constexpr function definition table.
771 For constructor BODY is actually the TREE_LIST of the
772 member-initializer list. */
774 tree
775 register_constexpr_fundef (tree fun, tree body)
777 constexpr_fundef entry;
778 constexpr_fundef **slot;
780 if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun)))
781 return NULL;
783 body = massage_constexpr_body (fun, body);
784 if (body == NULL_TREE || body == error_mark_node)
786 if (!DECL_CONSTRUCTOR_P (fun))
787 error ("body of constexpr function %qD not a return-statement", fun);
788 return NULL;
791 if (!potential_rvalue_constant_expression (body))
793 if (!DECL_GENERATED_P (fun))
794 require_potential_rvalue_constant_expression (body);
795 return NULL;
798 if (DECL_CONSTRUCTOR_P (fun)
799 && cx_check_missing_mem_inits (fun, body, !DECL_GENERATED_P (fun)))
800 return NULL;
802 /* Create the constexpr function table if necessary. */
803 if (constexpr_fundef_table == NULL)
804 constexpr_fundef_table
805 = hash_table<constexpr_fundef_hasher>::create_ggc (101);
807 entry.decl = fun;
808 entry.body = body;
809 slot = constexpr_fundef_table->find_slot (&entry, INSERT);
811 gcc_assert (*slot == NULL);
812 *slot = ggc_alloc<constexpr_fundef> ();
813 **slot = entry;
815 return fun;
818 /* FUN is a non-constexpr function called in a context that requires a
819 constant expression. If it comes from a constexpr template, explain why
820 the instantiation isn't constexpr. */
822 void
823 explain_invalid_constexpr_fn (tree fun)
825 static hash_set<tree> *diagnosed;
826 tree body;
827 location_t save_loc;
828 /* Only diagnose defaulted functions or instantiations. */
829 if (!DECL_DEFAULTED_FN (fun)
830 && !is_instantiation_of_constexpr (fun))
831 return;
832 if (diagnosed == NULL)
833 diagnosed = new hash_set<tree>;
834 if (diagnosed->add (fun))
835 /* Already explained. */
836 return;
838 save_loc = input_location;
839 input_location = DECL_SOURCE_LOCATION (fun);
840 inform (0, "%q+D is not usable as a constexpr function because:", fun);
841 /* First check the declaration. */
842 if (is_valid_constexpr_fn (fun, true))
844 /* Then if it's OK, the body. */
845 if (!DECL_DECLARED_CONSTEXPR_P (fun))
846 explain_implicit_non_constexpr (fun);
847 else
849 body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
850 require_potential_rvalue_constant_expression (body);
851 if (DECL_CONSTRUCTOR_P (fun))
852 cx_check_missing_mem_inits (fun, body, true);
855 input_location = save_loc;
858 /* Objects of this type represent calls to constexpr functions
859 along with the bindings of parameters to their arguments, for
860 the purpose of compile time evaluation. */
862 struct GTY((for_user)) constexpr_call {
863 /* Description of the constexpr function definition. */
864 constexpr_fundef *fundef;
865 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
866 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
867 Note: This arrangement is made to accommodate the use of
868 iterative_hash_template_arg (see pt.c). If you change this
869 representation, also change the hash calculation in
870 cxx_eval_call_expression. */
871 tree bindings;
872 /* Result of the call.
873 NULL means the call is being evaluated.
874 error_mark_node means that the evaluation was erroneous;
875 otherwise, the actuall value of the call. */
876 tree result;
877 /* The hash of this call; we remember it here to avoid having to
878 recalculate it when expanding the hash table. */
879 hashval_t hash;
882 struct constexpr_call_hasher : ggc_hasher<constexpr_call *>
884 static hashval_t hash (constexpr_call *);
885 static bool equal (constexpr_call *, constexpr_call *);
888 /* The constexpr expansion context. CALL is the current function
889 expansion, CTOR is the current aggregate initializer, OBJECT is the
890 object being initialized by CTOR, either a VAR_DECL or a _REF. VALUES
891 is a map of values of variables initialized within the expression. */
893 struct constexpr_ctx {
894 /* The innermost call we're evaluating. */
895 constexpr_call *call;
896 /* Values for any temporaries or local variables within the
897 constant-expression. */
898 hash_map<tree,tree> *values;
899 /* The CONSTRUCTOR we're currently building up for an aggregate
900 initializer. */
901 tree ctor;
902 /* The object we're building the CONSTRUCTOR for. */
903 tree object;
904 /* Whether we should error on a non-constant expression or fail quietly. */
905 bool quiet;
906 /* Whether we are strictly conforming to constant expression rules or
907 trying harder to get a constant value. */
908 bool strict;
911 /* A table of all constexpr calls that have been evaluated by the
912 compiler in this translation unit. */
914 static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
916 static tree cxx_eval_constant_expression (const constexpr_ctx *, tree,
917 bool, bool *, bool *, tree * = NULL);
919 /* Compute a hash value for a constexpr call representation. */
921 inline hashval_t
922 constexpr_call_hasher::hash (constexpr_call *info)
924 return info->hash;
927 /* Return true if the objects pointed to by P and Q represent calls
928 to the same constexpr function with the same arguments.
929 Otherwise, return false. */
931 bool
932 constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
934 tree lhs_bindings;
935 tree rhs_bindings;
936 if (lhs == rhs)
937 return 1;
938 if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
939 return 0;
940 lhs_bindings = lhs->bindings;
941 rhs_bindings = rhs->bindings;
942 while (lhs_bindings != NULL && rhs_bindings != NULL)
944 tree lhs_arg = TREE_VALUE (lhs_bindings);
945 tree rhs_arg = TREE_VALUE (rhs_bindings);
946 gcc_assert (TREE_TYPE (lhs_arg) == TREE_TYPE (rhs_arg));
947 if (!cp_tree_equal (lhs_arg, rhs_arg))
948 return 0;
949 lhs_bindings = TREE_CHAIN (lhs_bindings);
950 rhs_bindings = TREE_CHAIN (rhs_bindings);
952 return lhs_bindings == rhs_bindings;
955 /* Initialize the constexpr call table, if needed. */
957 static void
958 maybe_initialize_constexpr_call_table (void)
960 if (constexpr_call_table == NULL)
961 constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
964 /* We have an expression tree T that represents a call, either CALL_EXPR
965 or AGGR_INIT_EXPR. If the call is lexically to a named function,
966 retrun the _DECL for that function. */
968 static tree
969 get_function_named_in_call (tree t)
971 tree fun = NULL;
972 switch (TREE_CODE (t))
974 case CALL_EXPR:
975 fun = CALL_EXPR_FN (t);
976 break;
978 case AGGR_INIT_EXPR:
979 fun = AGGR_INIT_EXPR_FN (t);
980 break;
982 default:
983 gcc_unreachable();
984 break;
986 if (fun && TREE_CODE (fun) == ADDR_EXPR
987 && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
988 fun = TREE_OPERAND (fun, 0);
989 return fun;
992 /* We have an expression tree T that represents a call, either CALL_EXPR
993 or AGGR_INIT_EXPR. Return the Nth argument. */
995 static inline tree
996 get_nth_callarg (tree t, int n)
998 switch (TREE_CODE (t))
1000 case CALL_EXPR:
1001 return CALL_EXPR_ARG (t, n);
1003 case AGGR_INIT_EXPR:
1004 return AGGR_INIT_EXPR_ARG (t, n);
1006 default:
1007 gcc_unreachable ();
1008 return NULL;
1012 /* Attempt to evaluate T which represents a call to a builtin function.
1013 We assume here that all builtin functions evaluate to scalar types
1014 represented by _CST nodes. */
1016 static tree
1017 cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun,
1018 bool lval,
1019 bool *non_constant_p, bool *overflow_p)
1021 const int nargs = call_expr_nargs (t);
1022 tree *args = (tree *) alloca (nargs * sizeof (tree));
1023 tree new_call;
1024 int i;
1026 /* Don't fold __builtin_constant_p within a constexpr function. */
1027 if (DECL_FUNCTION_CODE (fun) == BUILT_IN_CONSTANT_P
1028 && current_function_decl
1029 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
1031 *non_constant_p = true;
1032 return t;
1035 /* Be permissive for arguments to built-ins; __builtin_constant_p should
1036 return constant false for a non-constant argument. */
1037 constexpr_ctx new_ctx = *ctx;
1038 new_ctx.quiet = true;
1039 bool dummy1 = false, dummy2 = false;
1040 for (i = 0; i < nargs; ++i)
1041 args[i] = cxx_eval_constant_expression (&new_ctx, CALL_EXPR_ARG (t, i),
1042 lval, &dummy1, &dummy2);
1044 bool save_ffbcp = force_folding_builtin_constant_p;
1045 force_folding_builtin_constant_p = true;
1046 new_call = fold_build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
1047 CALL_EXPR_FN (t), nargs, args);
1048 force_folding_builtin_constant_p = save_ffbcp;
1049 VERIFY_CONSTANT (new_call);
1050 return new_call;
1053 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1054 the type of the value to match. */
1056 static tree
1057 adjust_temp_type (tree type, tree temp)
1059 if (TREE_TYPE (temp) == type)
1060 return temp;
1061 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1062 if (TREE_CODE (temp) == CONSTRUCTOR)
1063 return build_constructor (type, CONSTRUCTOR_ELTS (temp));
1064 gcc_assert (scalarish_type_p (type));
1065 return cp_fold_convert (type, temp);
1068 /* Subroutine of cxx_eval_call_expression.
1069 We are processing a call expression (either CALL_EXPR or
1070 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1071 all arguments and bind their values to correspondings
1072 parameters, making up the NEW_CALL context. */
1074 static void
1075 cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t,
1076 constexpr_call *new_call,
1077 bool *non_constant_p, bool *overflow_p,
1078 bool *non_constant_args)
1080 const int nargs = call_expr_nargs (t);
1081 tree fun = new_call->fundef->decl;
1082 tree parms = DECL_ARGUMENTS (fun);
1083 int i;
1084 tree *p = &new_call->bindings;
1085 for (i = 0; i < nargs; ++i)
1087 tree x, arg;
1088 tree type = parms ? TREE_TYPE (parms) : void_type_node;
1089 x = get_nth_callarg (t, i);
1090 /* For member function, the first argument is a pointer to the implied
1091 object. For a constructor, it might still be a dummy object, in
1092 which case we get the real argument from ctx. */
1093 if (i == 0 && DECL_CONSTRUCTOR_P (fun)
1094 && is_dummy_object (x))
1096 x = ctx->object;
1097 x = cp_build_addr_expr (x, tf_warning_or_error);
1099 bool lval = false;
1100 arg = cxx_eval_constant_expression (ctx, x, lval,
1101 non_constant_p, overflow_p);
1102 /* Don't VERIFY_CONSTANT here. */
1103 if (*non_constant_p && ctx->quiet)
1104 return;
1105 /* Just discard ellipsis args after checking their constantitude. */
1106 if (!parms)
1107 continue;
1108 if (*non_constant_p)
1109 /* Don't try to adjust the type of non-constant args. */
1110 goto next;
1112 /* Make sure the binding has the same type as the parm. */
1113 if (TREE_CODE (type) != REFERENCE_TYPE)
1114 arg = adjust_temp_type (type, arg);
1115 if (!TREE_CONSTANT (arg))
1116 *non_constant_args = true;
1117 *p = build_tree_list (parms, arg);
1118 p = &TREE_CHAIN (*p);
1119 next:
1120 parms = TREE_CHAIN (parms);
1124 /* Variables and functions to manage constexpr call expansion context.
1125 These do not need to be marked for PCH or GC. */
1127 /* FIXME remember and print actual constant arguments. */
1128 static vec<tree> call_stack = vNULL;
1129 static int call_stack_tick;
1130 static int last_cx_error_tick;
1132 static bool
1133 push_cx_call_context (tree call)
1135 ++call_stack_tick;
1136 if (!EXPR_HAS_LOCATION (call))
1137 SET_EXPR_LOCATION (call, input_location);
1138 call_stack.safe_push (call);
1139 if (call_stack.length () > (unsigned) max_constexpr_depth)
1140 return false;
1141 return true;
1144 static void
1145 pop_cx_call_context (void)
1147 ++call_stack_tick;
1148 call_stack.pop ();
1151 vec<tree>
1152 cx_error_context (void)
1154 vec<tree> r = vNULL;
1155 if (call_stack_tick != last_cx_error_tick
1156 && !call_stack.is_empty ())
1157 r = call_stack;
1158 last_cx_error_tick = call_stack_tick;
1159 return r;
1162 /* Subroutine of cxx_eval_constant_expression.
1163 Evaluate the call expression tree T in the context of OLD_CALL expression
1164 evaluation. */
1166 static tree
1167 cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
1168 bool lval,
1169 bool *non_constant_p, bool *overflow_p)
1171 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
1172 tree fun = get_function_named_in_call (t);
1173 constexpr_call new_call = { NULL, NULL, NULL, 0 };
1174 bool depth_ok;
1176 if (fun == NULL_TREE)
1177 switch (CALL_EXPR_IFN (t))
1179 case IFN_UBSAN_NULL:
1180 case IFN_UBSAN_BOUNDS:
1181 case IFN_UBSAN_VPTR:
1182 return void_node;
1183 default:
1184 if (!ctx->quiet)
1185 error_at (loc, "call to internal function");
1186 *non_constant_p = true;
1187 return t;
1190 if (TREE_CODE (fun) != FUNCTION_DECL)
1192 /* Might be a constexpr function pointer. */
1193 fun = cxx_eval_constant_expression (ctx, fun,
1194 /*lval*/false, non_constant_p,
1195 overflow_p);
1196 STRIP_NOPS (fun);
1197 if (TREE_CODE (fun) == ADDR_EXPR)
1198 fun = TREE_OPERAND (fun, 0);
1200 if (TREE_CODE (fun) != FUNCTION_DECL)
1202 if (!ctx->quiet && !*non_constant_p)
1203 error_at (loc, "expression %qE does not designate a constexpr "
1204 "function", fun);
1205 *non_constant_p = true;
1206 return t;
1208 if (DECL_CLONED_FUNCTION_P (fun))
1209 fun = DECL_CLONED_FUNCTION (fun);
1211 if (is_ubsan_builtin_p (fun))
1212 return void_node;
1214 if (is_builtin_fn (fun))
1215 return cxx_eval_builtin_function_call (ctx, t, fun,
1216 lval, non_constant_p, overflow_p);
1217 if (!DECL_DECLARED_CONSTEXPR_P (fun))
1219 if (!ctx->quiet)
1221 error_at (loc, "call to non-constexpr function %qD", fun);
1222 explain_invalid_constexpr_fn (fun);
1224 *non_constant_p = true;
1225 return t;
1228 /* Shortcut trivial constructor/op=. */
1229 if (trivial_fn_p (fun))
1231 if (call_expr_nargs (t) == 2)
1233 tree arg = convert_from_reference (get_nth_callarg (t, 1));
1234 return cxx_eval_constant_expression (ctx, arg,
1235 lval, non_constant_p,
1236 overflow_p);
1238 else if (TREE_CODE (t) == AGGR_INIT_EXPR
1239 && AGGR_INIT_ZERO_FIRST (t))
1240 return build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false);
1243 /* If in direct recursive call, optimize definition search. */
1244 if (ctx && ctx->call && ctx->call->fundef->decl == fun)
1245 new_call.fundef = ctx->call->fundef;
1246 else
1248 new_call.fundef = retrieve_constexpr_fundef (fun);
1249 if (new_call.fundef == NULL || new_call.fundef->body == NULL)
1251 if (!ctx->quiet)
1253 if (DECL_INITIAL (fun))
1255 /* The definition of fun was somehow unsuitable. */
1256 error_at (loc, "%qD called in a constant expression", fun);
1257 explain_invalid_constexpr_fn (fun);
1259 else
1260 error_at (loc, "%qD used before its definition", fun);
1262 *non_constant_p = true;
1263 return t;
1267 constexpr_ctx new_ctx = *ctx;
1268 if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
1269 && TREE_CODE (t) == AGGR_INIT_EXPR)
1271 /* We want to have an initialization target for an AGGR_INIT_EXPR.
1272 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
1273 new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
1274 tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
1275 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctor) = true;
1276 ctx->values->put (new_ctx.object, ctor);
1277 ctx = &new_ctx;
1280 bool non_constant_args = false;
1281 cxx_bind_parameters_in_call (ctx, t, &new_call,
1282 non_constant_p, overflow_p, &non_constant_args);
1283 if (*non_constant_p)
1284 return t;
1286 depth_ok = push_cx_call_context (t);
1288 tree result = NULL_TREE;
1290 constexpr_call *entry = NULL;
1291 if (!non_constant_args)
1293 new_call.hash = iterative_hash_template_arg
1294 (new_call.bindings, constexpr_fundef_hasher::hash (new_call.fundef));
1296 /* If we have seen this call before, we are done. */
1297 maybe_initialize_constexpr_call_table ();
1298 constexpr_call **slot
1299 = constexpr_call_table->find_slot (&new_call, INSERT);
1300 entry = *slot;
1301 if (entry == NULL)
1303 /* We need to keep a pointer to the entry, not just the slot, as the
1304 slot can move in the call to cxx_eval_builtin_function_call. */
1305 *slot = entry = ggc_alloc<constexpr_call> ();
1306 *entry = new_call;
1308 /* Calls which are in progress have their result set to NULL
1309 so that we can detect circular dependencies. */
1310 else if (entry->result == NULL)
1312 if (!ctx->quiet)
1313 error ("call has circular dependency");
1314 *non_constant_p = true;
1315 entry->result = result = error_mark_node;
1317 else
1318 result = entry->result;
1321 if (!depth_ok)
1323 if (!ctx->quiet)
1324 error ("constexpr evaluation depth exceeds maximum of %d (use "
1325 "-fconstexpr-depth= to increase the maximum)",
1326 max_constexpr_depth);
1327 *non_constant_p = true;
1328 result = error_mark_node;
1330 else
1332 if (!result || result == error_mark_node)
1334 if (DECL_SAVED_TREE (fun) == NULL_TREE
1335 && (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun)))
1336 /* The maybe-in-charge 'tor had its DECL_SAVED_TREE
1337 cleared, try a clone. */
1338 for (fun = DECL_CHAIN (fun);
1339 fun && DECL_CLONED_FUNCTION_P (fun);
1340 fun = DECL_CHAIN (fun))
1341 if (DECL_SAVED_TREE (fun))
1342 break;
1343 gcc_assert (DECL_SAVED_TREE (fun));
1344 tree parms, res;
1346 /* Unshare the whole function body. */
1347 tree body = copy_fn (fun, parms, res);
1349 /* Associate the bindings with the remapped parms. */
1350 tree bound = new_call.bindings;
1351 tree remapped = parms;
1352 while (bound)
1354 tree oparm = TREE_PURPOSE (bound);
1355 tree arg = TREE_VALUE (bound);
1356 gcc_assert (DECL_NAME (remapped) == DECL_NAME (oparm));
1357 ctx->values->put (remapped, arg);
1358 bound = TREE_CHAIN (bound);
1359 remapped = DECL_CHAIN (remapped);
1361 /* Add the RESULT_DECL to the values map, too. */
1362 tree slot = NULL_TREE;
1363 if (DECL_BY_REFERENCE (res))
1365 slot = AGGR_INIT_EXPR_SLOT (t);
1366 tree addr = build_address (slot);
1367 addr = build_nop (TREE_TYPE (res), addr);
1368 ctx->values->put (res, addr);
1369 ctx->values->put (slot, NULL_TREE);
1371 else
1372 ctx->values->put (res, NULL_TREE);
1374 tree jump_target = NULL_TREE;
1375 cxx_eval_constant_expression (ctx, body,
1376 lval, non_constant_p, overflow_p,
1377 &jump_target);
1379 if (DECL_CONSTRUCTOR_P (fun))
1380 /* This can be null for a subobject constructor call, in
1381 which case what we care about is the initialization
1382 side-effects rather than the value. We could get at the
1383 value by evaluating *this, but we don't bother; there's
1384 no need to put such a call in the hash table. */
1385 result = lval ? ctx->object : ctx->ctor;
1386 else if (VOID_TYPE_P (TREE_TYPE (res)))
1387 result = void_node;
1388 else
1390 result = *ctx->values->get (slot ? slot : res);
1391 if (result == NULL_TREE && !*non_constant_p)
1393 if (!ctx->quiet)
1394 error ("constexpr call flows off the end "
1395 "of the function");
1396 *non_constant_p = true;
1400 /* Remove the parms/result from the values map. Is it worth
1401 bothering to do this when the map itself is only live for
1402 one constexpr evaluation? If so, maybe also clear out
1403 other vars from call, maybe in BIND_EXPR handling? */
1404 ctx->values->remove (res);
1405 if (slot)
1406 ctx->values->remove (slot);
1407 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
1408 ctx->values->remove (parm);
1411 if (result == error_mark_node)
1412 *non_constant_p = true;
1413 if (*non_constant_p)
1414 result = error_mark_node;
1415 else if (!result)
1416 result = void_node;
1417 if (entry)
1418 entry->result = result;
1421 pop_cx_call_context ();
1422 return unshare_expr (result);
1425 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
1427 bool
1428 reduced_constant_expression_p (tree t)
1430 switch (TREE_CODE (t))
1432 case PTRMEM_CST:
1433 /* Even if we can't lower this yet, it's constant. */
1434 return true;
1436 case CONSTRUCTOR:
1437 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
1438 tree elt; unsigned HOST_WIDE_INT idx;
1439 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), idx, elt)
1440 if (!reduced_constant_expression_p (elt))
1441 return false;
1442 return true;
1444 default:
1445 /* FIXME are we calling this too much? */
1446 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
1450 /* Some expressions may have constant operands but are not constant
1451 themselves, such as 1/0. Call this function (or rather, the macro
1452 following it) to check for that condition.
1454 We only call this in places that require an arithmetic constant, not in
1455 places where we might have a non-constant expression that can be a
1456 component of a constant expression, such as the address of a constexpr
1457 variable that might be dereferenced later. */
1459 static bool
1460 verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
1461 bool *overflow_p)
1463 if (!*non_constant_p && !reduced_constant_expression_p (t))
1465 if (!allow_non_constant)
1466 error ("%q+E is not a constant expression", t);
1467 *non_constant_p = true;
1469 if (TREE_OVERFLOW_P (t))
1471 if (!allow_non_constant)
1473 permerror (input_location, "overflow in constant expression");
1474 /* If we're being permissive (and are in an enforcing
1475 context), ignore the overflow. */
1476 if (flag_permissive)
1477 return *non_constant_p;
1479 *overflow_p = true;
1481 return *non_constant_p;
1484 /* Check whether the shift operation with code CODE and type TYPE on LHS
1485 and RHS is undefined. If it is, give an error with an explanation,
1486 and return true; return false otherwise. */
1488 static bool
1489 cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
1490 enum tree_code code, tree type, tree lhs, tree rhs)
1492 if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
1493 || TREE_CODE (lhs) != INTEGER_CST
1494 || TREE_CODE (rhs) != INTEGER_CST)
1495 return false;
1497 tree lhstype = TREE_TYPE (lhs);
1498 unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
1500 /* [expr.shift] The behavior is undefined if the right operand
1501 is negative, or greater than or equal to the length in bits
1502 of the promoted left operand. */
1503 if (tree_int_cst_sgn (rhs) == -1)
1505 if (!ctx->quiet)
1506 error_at (loc, "right operand of shift expression %q+E is negative",
1507 build2_loc (loc, code, type, lhs, rhs));
1508 return true;
1510 if (compare_tree_int (rhs, uprec) >= 0)
1512 if (!ctx->quiet)
1513 error_at (loc, "right operand of shift expression %q+E is >= than "
1514 "the precision of the left operand",
1515 build2_loc (loc, code, type, lhs, rhs));
1516 return true;
1519 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
1520 if E1 has a signed type and non-negative value, and E1x2^E2 is
1521 representable in the corresponding unsigned type of the result type,
1522 then that value, converted to the result type, is the resulting value;
1523 otherwise, the behavior is undefined. */
1524 if (code == LSHIFT_EXPR && !TYPE_UNSIGNED (lhstype)
1525 && (cxx_dialect >= cxx11))
1527 if (tree_int_cst_sgn (lhs) == -1)
1529 if (!ctx->quiet)
1530 error_at (loc, "left operand of shift expression %q+E is negative",
1531 build2_loc (loc, code, type, lhs, rhs));
1532 return true;
1534 /* For signed x << y the following:
1535 (unsigned) x >> ((prec (lhs) - 1) - y)
1536 if > 1, is undefined. The right-hand side of this formula
1537 is the highest bit of the LHS that can be set (starting from 0),
1538 so that the shift doesn't overflow. We then right-shift the LHS
1539 to see whether any other bit is set making the original shift
1540 undefined -- the result is not representable in the corresponding
1541 unsigned type. */
1542 tree t = build_int_cst (unsigned_type_node, uprec - 1);
1543 t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
1544 tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
1545 t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
1546 if (tree_int_cst_lt (integer_one_node, t))
1548 if (!ctx->quiet)
1549 error_at (loc, "shift expression %q+E overflows",
1550 build2_loc (loc, code, type, lhs, rhs));
1551 return true;
1554 return false;
1557 /* Subroutine of cxx_eval_constant_expression.
1558 Attempt to reduce the unary expression tree T to a compile time value.
1559 If successful, return the value. Otherwise issue a diagnostic
1560 and return error_mark_node. */
1562 static tree
1563 cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
1564 bool /*lval*/,
1565 bool *non_constant_p, bool *overflow_p)
1567 tree r;
1568 tree orig_arg = TREE_OPERAND (t, 0);
1569 tree arg = cxx_eval_constant_expression (ctx, orig_arg, /*lval*/false,
1570 non_constant_p, overflow_p);
1571 VERIFY_CONSTANT (arg);
1572 location_t loc = EXPR_LOCATION (t);
1573 enum tree_code code = TREE_CODE (t);
1574 tree type = TREE_TYPE (t);
1575 r = fold_unary_loc (loc, code, type, arg);
1576 if (r == NULL_TREE)
1578 if (arg == orig_arg)
1579 r = t;
1580 else
1581 r = build1_loc (loc, code, type, arg);
1583 VERIFY_CONSTANT (r);
1584 return r;
1587 /* Subroutine of cxx_eval_constant_expression.
1588 Like cxx_eval_unary_expression, except for binary expressions. */
1590 static tree
1591 cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
1592 bool /*lval*/,
1593 bool *non_constant_p, bool *overflow_p)
1595 tree r;
1596 tree orig_lhs = TREE_OPERAND (t, 0);
1597 tree orig_rhs = TREE_OPERAND (t, 1);
1598 tree lhs, rhs;
1599 lhs = cxx_eval_constant_expression (ctx, orig_lhs, /*lval*/false,
1600 non_constant_p, overflow_p);
1601 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
1602 a local array in a constexpr function. */
1603 bool ptr = POINTER_TYPE_P (TREE_TYPE (lhs));
1604 if (!ptr)
1605 VERIFY_CONSTANT (lhs);
1606 rhs = cxx_eval_constant_expression (ctx, orig_rhs, /*lval*/false,
1607 non_constant_p, overflow_p);
1608 if (!ptr)
1609 VERIFY_CONSTANT (rhs);
1611 location_t loc = EXPR_LOCATION (t);
1612 enum tree_code code = TREE_CODE (t);
1613 tree type = TREE_TYPE (t);
1614 r = fold_binary_loc (loc, code, type, lhs, rhs);
1615 if (r == NULL_TREE)
1617 if (lhs == orig_lhs && rhs == orig_rhs)
1618 r = t;
1619 else
1620 r = build2_loc (loc, code, type, lhs, rhs);
1622 else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
1623 *non_constant_p = true;
1624 if (!ptr)
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 tree init = TREE_OPERAND (t, 1);
2577 if (TREE_CLOBBER_P (init))
2578 /* Just ignore clobbers. */
2579 return void_node;
2581 /* First we figure out where we're storing to. */
2582 tree target = TREE_OPERAND (t, 0);
2583 tree type = TREE_TYPE (target);
2584 target = cxx_eval_constant_expression (ctx, target,
2585 true,
2586 non_constant_p, overflow_p);
2587 if (*non_constant_p)
2588 return t;
2590 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (target), type))
2592 /* For initialization of an empty base, the original target will be
2593 *(base*)this, which the above evaluation resolves to the object
2594 argument, which has the derived type rather than the base type. In
2595 this situation, just evaluate the initializer and return, since
2596 there's no actual data to store. */
2597 gcc_assert (is_empty_class (type));
2598 return cxx_eval_constant_expression (ctx, init, false,
2599 non_constant_p, overflow_p);
2602 /* And then find the underlying variable. */
2603 vec<tree,va_gc> *refs = make_tree_vector();
2604 tree object = NULL_TREE;
2605 for (tree probe = target; object == NULL_TREE; )
2607 switch (TREE_CODE (probe))
2609 case BIT_FIELD_REF:
2610 case COMPONENT_REF:
2611 case ARRAY_REF:
2612 vec_safe_push (refs, TREE_OPERAND (probe, 1));
2613 vec_safe_push (refs, TREE_TYPE (probe));
2614 probe = TREE_OPERAND (probe, 0);
2615 break;
2617 default:
2618 object = probe;
2622 /* And then find/build up our initializer for the path to the subobject
2623 we're initializing. */
2624 tree *valp;
2625 if (DECL_P (object))
2626 valp = ctx->values->get (object);
2627 else
2628 valp = NULL;
2629 if (!valp)
2631 /* A constant-expression cannot modify objects from outside the
2632 constant-expression. */
2633 if (!ctx->quiet)
2634 error ("modification of %qE is not a constant-expression", object);
2635 *non_constant_p = true;
2636 return t;
2638 type = TREE_TYPE (object);
2639 while (!refs->is_empty())
2641 if (*valp == NULL_TREE)
2643 *valp = build_constructor (type, NULL);
2644 CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp) = true;
2647 constructor_elt ce;
2648 type = refs->pop();
2649 ce.index = refs->pop();
2650 ce.value = NULL_TREE;
2652 unsigned HOST_WIDE_INT idx = 0;
2653 constructor_elt *cep = NULL;
2654 for (idx = 0;
2655 vec_safe_iterate (CONSTRUCTOR_ELTS (*valp), idx, &cep);
2656 idx++)
2657 /* ??? slow */
2658 if (cp_tree_equal (ce.index, cep->index))
2659 break;
2660 if (!cep)
2661 cep = vec_safe_push (CONSTRUCTOR_ELTS (*valp), ce);
2662 valp = &cep->value;
2664 release_tree_vector (refs);
2666 if ((AGGREGATE_TYPE_P (TREE_TYPE (t)) || VECTOR_TYPE_P (TREE_TYPE (t))))
2668 /* Create a new CONSTRUCTOR in case evaluation of the initializer
2669 wants to modify it. */
2670 *valp = new_ctx.ctor = build_constructor (TREE_TYPE (t), NULL);
2671 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
2672 new_ctx.object = target;
2675 init = cxx_eval_constant_expression (&new_ctx, init, false,
2676 non_constant_p, overflow_p);
2677 if (target == object)
2678 /* The hash table might have moved since the get earlier. */
2679 ctx->values->put (object, init);
2680 else
2681 *valp = init;
2683 if (*non_constant_p)
2684 return t;
2685 else if (lval)
2686 return target;
2687 else
2688 return init;
2691 /* Evaluate a ++ or -- expression. */
2693 static tree
2694 cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
2695 bool lval,
2696 bool *non_constant_p, bool *overflow_p)
2698 enum tree_code code = TREE_CODE (t);
2699 tree type = TREE_TYPE (t);
2700 tree op = TREE_OPERAND (t, 0);
2701 tree offset = TREE_OPERAND (t, 1);
2702 gcc_assert (TREE_CONSTANT (offset));
2704 /* The operand as an lvalue. */
2705 op = cxx_eval_constant_expression (ctx, op, true,
2706 non_constant_p, overflow_p);
2708 /* The operand as an rvalue. */
2709 tree val = rvalue (op);
2710 val = cxx_eval_constant_expression (ctx, val, false,
2711 non_constant_p, overflow_p);
2712 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
2713 a local array in a constexpr function. */
2714 bool ptr = POINTER_TYPE_P (TREE_TYPE (val));
2715 if (!ptr)
2716 VERIFY_CONSTANT (val);
2718 /* The modified value. */
2719 bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
2720 tree mod;
2721 if (POINTER_TYPE_P (type))
2723 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
2724 offset = convert_to_ptrofftype (offset);
2725 if (!inc)
2726 offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
2727 mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
2729 else
2730 mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
2731 if (!ptr)
2732 VERIFY_CONSTANT (mod);
2734 /* Storing the modified value. */
2735 tree store = build2 (MODIFY_EXPR, type, op, mod);
2736 cxx_eval_constant_expression (ctx, store,
2737 true, non_constant_p, overflow_p);
2739 /* And the value of the expression. */
2740 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2742 /* Prefix ops are lvalues. */
2743 if (lval)
2744 return op;
2745 else
2746 /* But we optimize when the caller wants an rvalue. */
2747 return mod;
2749 else
2750 /* Postfix ops are rvalues. */
2751 return val;
2754 /* Predicates for the meaning of *jump_target. */
2756 static bool
2757 returns (tree *jump_target)
2759 return *jump_target
2760 && TREE_CODE (*jump_target) == RETURN_EXPR;
2763 static bool
2764 breaks (tree *jump_target)
2766 return *jump_target
2767 && TREE_CODE (*jump_target) == LABEL_DECL
2768 && LABEL_DECL_BREAK (*jump_target);
2771 static bool
2772 continues (tree *jump_target)
2774 return *jump_target
2775 && TREE_CODE (*jump_target) == LABEL_DECL
2776 && LABEL_DECL_CONTINUE (*jump_target);
2779 static bool
2780 switches (tree *jump_target)
2782 return *jump_target
2783 && TREE_CODE (*jump_target) == INTEGER_CST;
2786 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
2787 at I matches *jump_target. If we're looking for a case label and we see
2788 the default label, copy I into DEFAULT_LABEL. */
2790 static bool
2791 label_matches (tree *jump_target, tree_stmt_iterator i,
2792 tree_stmt_iterator& default_label)
2794 tree stmt = tsi_stmt (i);
2795 switch (TREE_CODE (*jump_target))
2797 case LABEL_DECL:
2798 if (TREE_CODE (stmt) == LABEL_EXPR
2799 && LABEL_EXPR_LABEL (stmt) == *jump_target)
2800 return true;
2801 break;
2803 case INTEGER_CST:
2804 if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
2806 if (!CASE_LOW (stmt))
2807 default_label = i;
2808 else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
2809 return true;
2811 break;
2813 default:
2814 gcc_unreachable ();
2816 return false;
2819 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
2820 semantics, for switch, break, continue, and return. */
2822 static tree
2823 cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
2824 bool *non_constant_p, bool *overflow_p,
2825 tree *jump_target)
2827 tree_stmt_iterator i;
2828 tree_stmt_iterator default_label = tree_stmt_iterator();
2829 tree local_target;
2830 /* In a statement-expression we want to return the last value. */
2831 tree r = NULL_TREE;
2832 if (!jump_target)
2834 local_target = NULL_TREE;
2835 jump_target = &local_target;
2837 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
2839 reenter:
2840 tree stmt = tsi_stmt (i);
2841 if (*jump_target)
2843 if (TREE_CODE (stmt) == STATEMENT_LIST)
2844 /* The label we want might be inside. */;
2845 else if (label_matches (jump_target, i, default_label))
2846 /* Found it. */
2847 *jump_target = NULL_TREE;
2848 else
2849 continue;
2851 r = cxx_eval_constant_expression (ctx, stmt, false,
2852 non_constant_p, overflow_p,
2853 jump_target);
2854 if (*non_constant_p)
2855 break;
2856 if (returns (jump_target) || breaks (jump_target))
2857 break;
2859 if (switches (jump_target) && !tsi_end_p (default_label))
2861 i = default_label;
2862 *jump_target = NULL_TREE;
2863 goto reenter;
2865 return r;
2868 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
2869 semantics; continue semantics are covered by cxx_eval_statement_list. */
2871 static tree
2872 cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
2873 bool *non_constant_p, bool *overflow_p,
2874 tree *jump_target)
2876 tree body = TREE_OPERAND (t, 0);
2877 while (true)
2879 cxx_eval_statement_list (ctx, body,
2880 non_constant_p, overflow_p, jump_target);
2881 if (returns (jump_target) || breaks (jump_target) || *non_constant_p)
2882 break;
2884 if (breaks (jump_target))
2885 *jump_target = NULL_TREE;
2886 return NULL_TREE;
2889 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
2890 semantics. */
2892 static tree
2893 cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
2894 bool *non_constant_p, bool *overflow_p,
2895 tree *jump_target)
2897 tree cond = TREE_OPERAND (t, 0);
2898 cond = cxx_eval_constant_expression (ctx, cond, false,
2899 non_constant_p, overflow_p);
2900 VERIFY_CONSTANT (cond);
2901 *jump_target = cond;
2903 tree body = TREE_OPERAND (t, 1);
2904 cxx_eval_statement_list (ctx, body,
2905 non_constant_p, overflow_p, jump_target);
2906 if (breaks (jump_target) || switches (jump_target))
2907 *jump_target = NULL_TREE;
2908 return NULL_TREE;
2911 /* Subroutine of cxx_eval_constant_expression.
2912 Attempt to reduce a POINTER_PLUS_EXPR expression T. */
2914 static tree
2915 cxx_eval_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
2916 bool lval, bool *non_constant_p,
2917 bool *overflow_p)
2919 tree orig_type = TREE_TYPE (t);
2920 tree op00 = TREE_OPERAND (t, 0);
2921 tree op01 = TREE_OPERAND (t, 1);
2922 location_t loc = EXPR_LOCATION (t);
2924 op00 = cxx_eval_constant_expression (ctx, op00, lval,
2925 non_constant_p, overflow_p);
2927 STRIP_NOPS (op00);
2928 if (TREE_CODE (op00) != ADDR_EXPR)
2929 return NULL_TREE;
2931 op00 = TREE_OPERAND (op00, 0);
2933 /* &A[i] p+ j => &A[i + j] */
2934 if (TREE_CODE (op00) == ARRAY_REF
2935 && TREE_CODE (TREE_OPERAND (op00, 1)) == INTEGER_CST
2936 && TREE_CODE (op01) == INTEGER_CST
2937 && TYPE_SIZE_UNIT (TREE_TYPE (op00))
2938 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (op00))) == INTEGER_CST)
2940 tree type = TREE_TYPE (op00);
2941 t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (op00, 1));
2942 tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (op00, 0)));
2943 /* Don't fold an out-of-bound access. */
2944 if (!tree_int_cst_le (t, nelts))
2945 return NULL_TREE;
2946 op01 = cp_fold_convert (ssizetype, op01);
2947 /* Don't fold if op01 can't be divided exactly by TYPE_SIZE_UNIT.
2948 constexpr int A[1]; ... (char *)&A[0] + 1 */
2949 if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
2950 op01, TYPE_SIZE_UNIT (type))))
2951 return NULL_TREE;
2952 /* Make sure to treat the second operand of POINTER_PLUS_EXPR
2953 as signed. */
2954 op01 = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, op01,
2955 TYPE_SIZE_UNIT (type));
2956 t = size_binop_loc (loc, PLUS_EXPR, op01, t);
2957 t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (op00, 0),
2958 t, NULL_TREE, NULL_TREE);
2959 t = cp_build_addr_expr (t, tf_warning_or_error);
2960 t = cp_fold_convert (orig_type, t);
2961 return cxx_eval_constant_expression (ctx, t, lval, non_constant_p,
2962 overflow_p);
2965 return NULL_TREE;
2968 /* Attempt to reduce the expression T to a constant value.
2969 On failure, issue diagnostic and return error_mark_node. */
2970 /* FIXME unify with c_fully_fold */
2971 /* FIXME overflow_p is too global */
2973 static tree
2974 cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
2975 bool lval,
2976 bool *non_constant_p, bool *overflow_p,
2977 tree *jump_target)
2979 constexpr_ctx new_ctx;
2980 tree r = t;
2982 if (t == error_mark_node)
2984 *non_constant_p = true;
2985 return t;
2987 if (CONSTANT_CLASS_P (t))
2989 if (TREE_CODE (t) == PTRMEM_CST)
2990 t = cplus_expand_constant (t);
2991 else if (TREE_OVERFLOW (t) && (!flag_permissive || ctx->quiet))
2992 *overflow_p = true;
2993 return t;
2996 switch (TREE_CODE (t))
2998 case RESULT_DECL:
2999 if (lval)
3000 return t;
3001 /* We ask for an rvalue for the RESULT_DECL when indirecting
3002 through an invisible reference, or in named return value
3003 optimization. */
3004 return (*ctx->values->get (t));
3006 case VAR_DECL:
3007 case CONST_DECL:
3008 /* We used to not check lval for CONST_DECL, but darwin.c uses
3009 CONST_DECL for aggregate constants. */
3010 if (lval)
3011 return t;
3012 if (ctx->strict)
3013 r = decl_really_constant_value (t);
3014 else
3015 r = decl_constant_value (t);
3016 if (TREE_CODE (r) == TARGET_EXPR
3017 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
3018 r = TARGET_EXPR_INITIAL (r);
3019 if (TREE_CODE (r) == VAR_DECL)
3020 if (tree *p = ctx->values->get (r))
3021 r = *p;
3022 if (DECL_P (r))
3024 if (!ctx->quiet)
3025 non_const_var_error (r);
3026 *non_constant_p = true;
3028 break;
3030 case FUNCTION_DECL:
3031 case TEMPLATE_DECL:
3032 case LABEL_DECL:
3033 case LABEL_EXPR:
3034 case CASE_LABEL_EXPR:
3035 return t;
3037 case PARM_DECL:
3038 if (lval && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
3039 /* glvalue use. */;
3040 else if (tree *p = ctx->values->get (r))
3041 r = *p;
3042 else if (lval)
3043 /* Defer in case this is only used for its type. */;
3044 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
3045 /* Defer, there's no lvalue->rvalue conversion. */;
3046 else if (is_empty_class (TREE_TYPE (t)))
3048 /* If the class is empty, we aren't actually loading anything. */
3049 r = build_constructor (TREE_TYPE (t), NULL);
3050 TREE_CONSTANT (r) = true;
3052 else
3054 if (!ctx->quiet)
3055 error ("%qE is not a constant expression", t);
3056 *non_constant_p = true;
3058 break;
3060 case CALL_EXPR:
3061 case AGGR_INIT_EXPR:
3062 r = cxx_eval_call_expression (ctx, t, lval,
3063 non_constant_p, overflow_p);
3064 break;
3066 case DECL_EXPR:
3068 r = DECL_EXPR_DECL (t);
3069 if (AGGREGATE_TYPE_P (TREE_TYPE (r))
3070 || VECTOR_TYPE_P (TREE_TYPE (r)))
3072 new_ctx = *ctx;
3073 new_ctx.object = r;
3074 new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
3075 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
3076 new_ctx.values->put (r, new_ctx.ctor);
3077 ctx = &new_ctx;
3080 if (tree init = DECL_INITIAL (r))
3082 init = cxx_eval_constant_expression (ctx, init,
3083 false,
3084 non_constant_p, overflow_p);
3085 ctx->values->put (r, init);
3087 else if (ctx == &new_ctx)
3088 /* We gave it a CONSTRUCTOR above. */;
3089 else
3090 ctx->values->put (r, NULL_TREE);
3092 break;
3094 case TARGET_EXPR:
3095 if (!literal_type_p (TREE_TYPE (t)))
3097 if (!ctx->quiet)
3099 error ("temporary of non-literal type %qT in a "
3100 "constant expression", TREE_TYPE (t));
3101 explain_non_literal_class (TREE_TYPE (t));
3103 *non_constant_p = true;
3104 break;
3106 if ((AGGREGATE_TYPE_P (TREE_TYPE (t)) || VECTOR_TYPE_P (TREE_TYPE (t))))
3108 /* We're being expanded without an explicit target, so start
3109 initializing a new object; expansion with an explicit target
3110 strips the TARGET_EXPR before we get here. */
3111 new_ctx = *ctx;
3112 new_ctx.ctor = build_constructor (TREE_TYPE (t), NULL);
3113 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
3114 new_ctx.object = TARGET_EXPR_SLOT (t);
3115 ctx->values->put (new_ctx.object, new_ctx.ctor);
3116 ctx = &new_ctx;
3118 /* Pass false for 'lval' because this indicates
3119 initialization of a temporary. */
3120 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3121 false,
3122 non_constant_p, overflow_p);
3123 if (!*non_constant_p)
3124 /* Adjust the type of the result to the type of the temporary. */
3125 r = adjust_temp_type (TREE_TYPE (t), r);
3126 if (lval)
3128 tree slot = TARGET_EXPR_SLOT (t);
3129 ctx->values->put (slot, r);
3130 return slot;
3132 break;
3134 case INIT_EXPR:
3135 case MODIFY_EXPR:
3136 r = cxx_eval_store_expression (ctx, t, lval,
3137 non_constant_p, overflow_p);
3138 break;
3140 case SCOPE_REF:
3141 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3142 lval,
3143 non_constant_p, overflow_p);
3144 break;
3146 case RETURN_EXPR:
3147 if (TREE_OPERAND (t, 0) != NULL_TREE)
3148 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3149 lval,
3150 non_constant_p, overflow_p);
3151 *jump_target = t;
3152 break;
3154 case SAVE_EXPR:
3155 /* Avoid evaluating a SAVE_EXPR more than once. */
3156 if (tree *p = ctx->values->get (t))
3157 r = *p;
3158 else
3160 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false,
3161 non_constant_p, overflow_p);
3162 ctx->values->put (t, r);
3164 break;
3166 case NON_LVALUE_EXPR:
3167 case TRY_CATCH_EXPR:
3168 case CLEANUP_POINT_EXPR:
3169 case MUST_NOT_THROW_EXPR:
3170 case EXPR_STMT:
3171 case EH_SPEC_BLOCK:
3172 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3173 lval,
3174 non_constant_p, overflow_p,
3175 jump_target);
3176 break;
3178 /* These differ from cxx_eval_unary_expression in that this doesn't
3179 check for a constant operand or result; an address can be
3180 constant without its operand being, and vice versa. */
3181 case INDIRECT_REF:
3182 r = cxx_eval_indirect_ref (ctx, t, lval,
3183 non_constant_p, overflow_p);
3184 break;
3186 case ADDR_EXPR:
3188 tree oldop = TREE_OPERAND (t, 0);
3189 tree op = cxx_eval_constant_expression (ctx, oldop,
3190 /*lval*/true,
3191 non_constant_p, overflow_p);
3192 /* Don't VERIFY_CONSTANT here. */
3193 if (*non_constant_p)
3194 return t;
3195 gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
3196 /* This function does more aggressive folding than fold itself. */
3197 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
3198 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
3199 return t;
3200 break;
3203 case REALPART_EXPR:
3204 case IMAGPART_EXPR:
3205 case CONJ_EXPR:
3206 case FIX_TRUNC_EXPR:
3207 case FLOAT_EXPR:
3208 case NEGATE_EXPR:
3209 case ABS_EXPR:
3210 case BIT_NOT_EXPR:
3211 case TRUTH_NOT_EXPR:
3212 case FIXED_CONVERT_EXPR:
3213 r = cxx_eval_unary_expression (ctx, t, lval,
3214 non_constant_p, overflow_p);
3215 break;
3217 case SIZEOF_EXPR:
3218 if (SIZEOF_EXPR_TYPE_P (t))
3219 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
3220 SIZEOF_EXPR, false);
3221 else if (TYPE_P (TREE_OPERAND (t, 0)))
3222 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
3223 false);
3224 else
3225 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
3226 false);
3227 if (r == error_mark_node)
3228 r = size_one_node;
3229 VERIFY_CONSTANT (r);
3230 break;
3232 case COMPOUND_EXPR:
3234 /* check_return_expr sometimes wraps a TARGET_EXPR in a
3235 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
3236 introduced by build_call_a. */
3237 tree op0 = TREE_OPERAND (t, 0);
3238 tree op1 = TREE_OPERAND (t, 1);
3239 STRIP_NOPS (op1);
3240 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
3241 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
3242 r = cxx_eval_constant_expression (ctx, op0,
3243 lval, non_constant_p, overflow_p,
3244 jump_target);
3245 else
3247 /* Check that the LHS is constant and then discard it. */
3248 cxx_eval_constant_expression (ctx, op0,
3249 true, non_constant_p, overflow_p,
3250 jump_target);
3251 op1 = TREE_OPERAND (t, 1);
3252 r = cxx_eval_constant_expression (ctx, op1,
3253 lval, non_constant_p, overflow_p,
3254 jump_target);
3257 break;
3259 case POINTER_PLUS_EXPR:
3260 r = cxx_eval_pointer_plus_expression (ctx, t, lval, non_constant_p,
3261 overflow_p);
3262 if (r)
3263 break;
3264 /* else fall through */
3266 case PLUS_EXPR:
3267 case MINUS_EXPR:
3268 case MULT_EXPR:
3269 case TRUNC_DIV_EXPR:
3270 case CEIL_DIV_EXPR:
3271 case FLOOR_DIV_EXPR:
3272 case ROUND_DIV_EXPR:
3273 case TRUNC_MOD_EXPR:
3274 case CEIL_MOD_EXPR:
3275 case ROUND_MOD_EXPR:
3276 case RDIV_EXPR:
3277 case EXACT_DIV_EXPR:
3278 case MIN_EXPR:
3279 case MAX_EXPR:
3280 case LSHIFT_EXPR:
3281 case RSHIFT_EXPR:
3282 case LROTATE_EXPR:
3283 case RROTATE_EXPR:
3284 case BIT_IOR_EXPR:
3285 case BIT_XOR_EXPR:
3286 case BIT_AND_EXPR:
3287 case TRUTH_XOR_EXPR:
3288 case LT_EXPR:
3289 case LE_EXPR:
3290 case GT_EXPR:
3291 case GE_EXPR:
3292 case EQ_EXPR:
3293 case NE_EXPR:
3294 case UNORDERED_EXPR:
3295 case ORDERED_EXPR:
3296 case UNLT_EXPR:
3297 case UNLE_EXPR:
3298 case UNGT_EXPR:
3299 case UNGE_EXPR:
3300 case UNEQ_EXPR:
3301 case LTGT_EXPR:
3302 case RANGE_EXPR:
3303 case COMPLEX_EXPR:
3304 r = cxx_eval_binary_expression (ctx, t, lval,
3305 non_constant_p, overflow_p);
3306 break;
3308 /* fold can introduce non-IF versions of these; still treat them as
3309 short-circuiting. */
3310 case TRUTH_AND_EXPR:
3311 case TRUTH_ANDIF_EXPR:
3312 r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
3313 boolean_true_node,
3314 lval,
3315 non_constant_p, overflow_p);
3316 break;
3318 case TRUTH_OR_EXPR:
3319 case TRUTH_ORIF_EXPR:
3320 r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
3321 boolean_false_node,
3322 lval,
3323 non_constant_p, overflow_p);
3324 break;
3326 case ARRAY_REF:
3327 r = cxx_eval_array_reference (ctx, t, lval,
3328 non_constant_p, overflow_p);
3329 break;
3331 case COMPONENT_REF:
3332 if (is_overloaded_fn (t))
3334 /* We can only get here in checking mode via
3335 build_non_dependent_expr, because any expression that
3336 calls or takes the address of the function will have
3337 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
3338 gcc_checking_assert (ctx->quiet || errorcount);
3339 *non_constant_p = true;
3340 return t;
3342 r = cxx_eval_component_reference (ctx, t, lval,
3343 non_constant_p, overflow_p);
3344 break;
3346 case BIT_FIELD_REF:
3347 r = cxx_eval_bit_field_ref (ctx, t, lval,
3348 non_constant_p, overflow_p);
3349 break;
3351 case COND_EXPR:
3352 case VEC_COND_EXPR:
3353 r = cxx_eval_conditional_expression (ctx, t, lval,
3354 non_constant_p, overflow_p,
3355 jump_target);
3356 break;
3358 case CONSTRUCTOR:
3359 if (TREE_CONSTANT (t))
3360 /* Don't re-process a constant CONSTRUCTOR, but do fold it to
3361 VECTOR_CST if applicable. */
3362 return fold (t);
3363 r = cxx_eval_bare_aggregate (ctx, t, lval,
3364 non_constant_p, overflow_p);
3365 break;
3367 case VEC_INIT_EXPR:
3368 /* We can get this in a defaulted constructor for a class with a
3369 non-static data member of array type. Either the initializer will
3370 be NULL, meaning default-initialization, or it will be an lvalue
3371 or xvalue of the same type, meaning direct-initialization from the
3372 corresponding member. */
3373 r = cxx_eval_vec_init (ctx, t, lval,
3374 non_constant_p, overflow_p);
3375 break;
3377 case FMA_EXPR:
3378 case VEC_PERM_EXPR:
3379 r = cxx_eval_trinary_expression (ctx, t, lval,
3380 non_constant_p, overflow_p);
3381 break;
3383 case CONVERT_EXPR:
3384 case VIEW_CONVERT_EXPR:
3385 case NOP_EXPR:
3387 tree oldop = TREE_OPERAND (t, 0);
3388 tree op = cxx_eval_constant_expression (ctx, oldop,
3389 lval,
3390 non_constant_p, overflow_p);
3391 if (*non_constant_p)
3392 return t;
3393 if (POINTER_TYPE_P (TREE_TYPE (t))
3394 && TREE_CODE (op) == INTEGER_CST
3395 && !integer_zerop (op))
3397 if (!ctx->quiet)
3398 error_at (EXPR_LOC_OR_LOC (t, input_location),
3399 "reinterpret_cast from integer to pointer");
3400 *non_constant_p = true;
3401 return t;
3403 if (op == oldop)
3404 /* We didn't fold at the top so we could check for ptr-int
3405 conversion. */
3406 return fold (t);
3407 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), op);
3408 /* Conversion of an out-of-range value has implementation-defined
3409 behavior; the language considers it different from arithmetic
3410 overflow, which is undefined. */
3411 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
3412 TREE_OVERFLOW (r) = false;
3414 break;
3416 case EMPTY_CLASS_EXPR:
3417 /* This is good enough for a function argument that might not get
3418 used, and they can't do anything with it, so just return it. */
3419 return t;
3421 case STATEMENT_LIST:
3422 new_ctx = *ctx;
3423 new_ctx.ctor = new_ctx.object = NULL_TREE;
3424 return cxx_eval_statement_list (&new_ctx, t,
3425 non_constant_p, overflow_p, jump_target);
3427 case BIND_EXPR:
3428 return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
3429 lval,
3430 non_constant_p, overflow_p,
3431 jump_target);
3433 case PREINCREMENT_EXPR:
3434 case POSTINCREMENT_EXPR:
3435 case PREDECREMENT_EXPR:
3436 case POSTDECREMENT_EXPR:
3437 return cxx_eval_increment_expression (ctx, t,
3438 lval, non_constant_p, overflow_p);
3440 case LAMBDA_EXPR:
3441 case NEW_EXPR:
3442 case VEC_NEW_EXPR:
3443 case DELETE_EXPR:
3444 case VEC_DELETE_EXPR:
3445 case THROW_EXPR:
3446 case MODOP_EXPR:
3447 /* GCC internal stuff. */
3448 case VA_ARG_EXPR:
3449 case OBJ_TYPE_REF:
3450 case WITH_CLEANUP_EXPR:
3451 case NON_DEPENDENT_EXPR:
3452 case BASELINK:
3453 case OFFSET_REF:
3454 if (!ctx->quiet)
3455 error_at (EXPR_LOC_OR_LOC (t, input_location),
3456 "expression %qE is not a constant-expression", t);
3457 *non_constant_p = true;
3458 break;
3460 case PLACEHOLDER_EXPR:
3461 if (!ctx || !ctx->ctor || (lval && !ctx->object))
3463 /* A placeholder without a referent. We can get here when
3464 checking whether NSDMIs are noexcept, or in massage_init_elt;
3465 just say it's non-constant for now. */
3466 gcc_assert (ctx->quiet);
3467 *non_constant_p = true;
3468 break;
3470 else
3472 /* Use of the value or address of the current object. We could
3473 use ctx->object unconditionally, but using ctx->ctor when we
3474 can is a minor optimization. */
3475 tree ctor = lval ? ctx->object : ctx->ctor;
3476 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3477 (TREE_TYPE (t), TREE_TYPE (ctor)));
3478 return cxx_eval_constant_expression
3479 (ctx, ctor, lval,
3480 non_constant_p, overflow_p);
3482 break;
3484 case GOTO_EXPR:
3485 *jump_target = TREE_OPERAND (t, 0);
3486 gcc_assert (breaks (jump_target) || continues (jump_target));
3487 break;
3489 case LOOP_EXPR:
3490 cxx_eval_loop_expr (ctx, t,
3491 non_constant_p, overflow_p, jump_target);
3492 break;
3494 case SWITCH_EXPR:
3495 cxx_eval_switch_expr (ctx, t,
3496 non_constant_p, overflow_p, jump_target);
3497 break;
3499 default:
3500 if (STATEMENT_CODE_P (TREE_CODE (t)))
3502 /* This function doesn't know how to deal with pre-genericize
3503 statements; this can only happen with statement-expressions,
3504 so for now just fail. */
3505 if (!ctx->quiet)
3506 error_at (EXPR_LOCATION (t),
3507 "statement is not a constant-expression");
3509 else
3510 internal_error ("unexpected expression %qE of kind %s", t,
3511 get_tree_code_name (TREE_CODE (t)));
3512 *non_constant_p = true;
3513 break;
3516 if (r == error_mark_node)
3517 *non_constant_p = true;
3519 if (*non_constant_p)
3520 return t;
3521 else
3522 return r;
3525 static tree
3526 cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
3527 bool strict = true, tree object = NULL_TREE)
3529 bool non_constant_p = false;
3530 bool overflow_p = false;
3531 hash_map<tree,tree> map;
3532 constexpr_ctx ctx = { NULL, &map, NULL, NULL, allow_non_constant, strict };
3533 tree type = initialized_type (t);
3534 tree r = t;
3535 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
3537 /* In C++14 an NSDMI can participate in aggregate initialization,
3538 and can refer to the address of the object being initialized, so
3539 we need to pass in the relevant VAR_DECL if we want to do the
3540 evaluation in a single pass. The evaluation will dynamically
3541 update ctx.values for the VAR_DECL. We use the same strategy
3542 for C++11 constexpr constructors that refer to the object being
3543 initialized. */
3544 ctx.ctor = build_constructor (type, NULL);
3545 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctx.ctor) = true;
3546 if (!object)
3548 if (TREE_CODE (t) == TARGET_EXPR)
3549 object = TARGET_EXPR_SLOT (t);
3550 else if (TREE_CODE (t) == AGGR_INIT_EXPR)
3551 object = AGGR_INIT_EXPR_SLOT (t);
3553 ctx.object = object;
3554 if (object)
3555 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3556 (type, TREE_TYPE (object)));
3557 if (object && DECL_P (object))
3558 map.put (object, ctx.ctor);
3559 if (TREE_CODE (r) == TARGET_EXPR)
3560 /* Avoid creating another CONSTRUCTOR when we expand the
3561 TARGET_EXPR. */
3562 r = TARGET_EXPR_INITIAL (r);
3565 r = cxx_eval_constant_expression (&ctx, r,
3566 false, &non_constant_p, &overflow_p);
3568 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
3570 /* Mutable logic is a bit tricky: we want to allow initialization of
3571 constexpr variables with mutable members, but we can't copy those
3572 members to another constexpr variable. */
3573 if (TREE_CODE (r) == CONSTRUCTOR
3574 && CONSTRUCTOR_MUTABLE_POISON (r))
3576 if (!allow_non_constant)
3577 error ("%qE is not a constant expression because it refers to "
3578 "mutable subobjects of %qT", t, type);
3579 non_constant_p = true;
3582 /* Technically we should check this for all subexpressions, but that
3583 runs into problems with our internal representation of pointer
3584 subtraction and the 5.19 rules are still in flux. */
3585 if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
3586 && ARITHMETIC_TYPE_P (TREE_TYPE (r))
3587 && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
3589 if (!allow_non_constant)
3590 error ("conversion from pointer type %qT "
3591 "to arithmetic type %qT in a constant-expression",
3592 TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
3593 non_constant_p = true;
3596 if (!non_constant_p && overflow_p)
3597 non_constant_p = true;
3599 if (non_constant_p && !allow_non_constant)
3600 return error_mark_node;
3601 else if (non_constant_p && TREE_CONSTANT (r))
3603 /* This isn't actually constant, so unset TREE_CONSTANT. */
3604 if (EXPR_P (r))
3605 r = copy_node (r);
3606 else if (TREE_CODE (r) == CONSTRUCTOR)
3607 r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
3608 else
3609 r = build_nop (TREE_TYPE (r), r);
3610 TREE_CONSTANT (r) = false;
3612 else if (non_constant_p || r == t)
3613 return t;
3615 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
3617 if (TREE_CODE (t) == TARGET_EXPR
3618 && TARGET_EXPR_INITIAL (t) == r)
3619 return t;
3620 else
3622 r = get_target_expr (r);
3623 TREE_CONSTANT (r) = true;
3624 return r;
3627 else
3628 return r;
3631 /* Returns true if T is a valid subexpression of a constant expression,
3632 even if it isn't itself a constant expression. */
3634 bool
3635 is_sub_constant_expr (tree t)
3637 bool non_constant_p = false;
3638 bool overflow_p = false;
3639 hash_map <tree, tree> map;
3640 constexpr_ctx ctx = { NULL, &map, NULL, NULL, true, true };
3641 cxx_eval_constant_expression (&ctx, t, false, &non_constant_p,
3642 &overflow_p);
3643 return !non_constant_p && !overflow_p;
3646 /* If T represents a constant expression returns its reduced value.
3647 Otherwise return error_mark_node. If T is dependent, then
3648 return NULL. */
3650 tree
3651 cxx_constant_value (tree t, tree decl)
3653 return cxx_eval_outermost_constant_expr (t, false, true, decl);
3656 /* If T is a constant expression, returns its reduced value.
3657 Otherwise, if T does not have TREE_CONSTANT set, returns T.
3658 Otherwise, returns a version of T without TREE_CONSTANT. */
3660 tree
3661 maybe_constant_value (tree t, tree decl)
3663 tree r;
3665 if (instantiation_dependent_expression_p (t)
3666 || type_unknown_p (t)
3667 || BRACE_ENCLOSED_INITIALIZER_P (t)
3668 || !potential_constant_expression (t))
3670 if (TREE_OVERFLOW_P (t))
3672 t = build_nop (TREE_TYPE (t), t);
3673 TREE_CONSTANT (t) = false;
3675 return t;
3678 r = cxx_eval_outermost_constant_expr (t, true, true, decl);
3679 #ifdef ENABLE_CHECKING
3680 gcc_assert (r == t
3681 || CONVERT_EXPR_P (t)
3682 || TREE_CODE (t) == VIEW_CONVERT_EXPR
3683 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
3684 || !cp_tree_equal (r, t));
3685 #endif
3686 return r;
3689 /* Like maybe_constant_value but first fully instantiate the argument.
3691 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
3692 (t, tf_none) followed by maybe_constant_value but is more efficient,
3693 because calls instantiation_dependent_expression_p and
3694 potential_constant_expression at most once. */
3696 tree
3697 fold_non_dependent_expr (tree t)
3699 if (t == NULL_TREE)
3700 return NULL_TREE;
3702 /* If we're in a template, but T isn't value dependent, simplify
3703 it. We're supposed to treat:
3705 template <typename T> void f(T[1 + 1]);
3706 template <typename T> void f(T[2]);
3708 as two declarations of the same function, for example. */
3709 if (processing_template_decl)
3711 if (!instantiation_dependent_expression_p (t)
3712 && potential_constant_expression (t))
3714 processing_template_decl_sentinel s;
3715 t = instantiate_non_dependent_expr_internal (t, tf_none);
3717 if (type_unknown_p (t)
3718 || BRACE_ENCLOSED_INITIALIZER_P (t))
3720 if (TREE_OVERFLOW_P (t))
3722 t = build_nop (TREE_TYPE (t), t);
3723 TREE_CONSTANT (t) = false;
3725 return t;
3728 tree r = cxx_eval_outermost_constant_expr (t, true, true, NULL_TREE);
3729 #ifdef ENABLE_CHECKING
3730 /* cp_tree_equal looks through NOPs, so allow them. */
3731 gcc_assert (r == t
3732 || CONVERT_EXPR_P (t)
3733 || TREE_CODE (t) == VIEW_CONVERT_EXPR
3734 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
3735 || !cp_tree_equal (r, t));
3736 #endif
3737 return r;
3739 else if (TREE_OVERFLOW_P (t))
3741 t = build_nop (TREE_TYPE (t), t);
3742 TREE_CONSTANT (t) = false;
3744 return t;
3747 return maybe_constant_value (t);
3750 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
3751 than wrapped in a TARGET_EXPR. */
3753 tree
3754 maybe_constant_init (tree t, tree decl)
3756 if (TREE_CODE (t) == EXPR_STMT)
3757 t = TREE_OPERAND (t, 0);
3758 if (TREE_CODE (t) == CONVERT_EXPR
3759 && VOID_TYPE_P (TREE_TYPE (t)))
3760 t = TREE_OPERAND (t, 0);
3761 if (TREE_CODE (t) == INIT_EXPR)
3762 t = TREE_OPERAND (t, 1);
3763 if (instantiation_dependent_expression_p (t)
3764 || type_unknown_p (t)
3765 || BRACE_ENCLOSED_INITIALIZER_P (t)
3766 || !potential_static_init_expression (t))
3767 /* Don't try to evaluate it. */;
3768 else
3769 t = cxx_eval_outermost_constant_expr (t, true, false, decl);
3770 if (TREE_CODE (t) == TARGET_EXPR)
3772 tree init = TARGET_EXPR_INITIAL (t);
3773 if (TREE_CODE (init) == CONSTRUCTOR)
3774 t = init;
3776 return t;
3779 #if 0
3780 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
3781 /* Return true if the object referred to by REF has automatic or thread
3782 local storage. */
3784 enum { ck_ok, ck_bad, ck_unknown };
3785 static int
3786 check_automatic_or_tls (tree ref)
3788 machine_mode mode;
3789 HOST_WIDE_INT bitsize, bitpos;
3790 tree offset;
3791 int volatilep = 0, unsignedp = 0;
3792 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
3793 &mode, &unsignedp, &volatilep, false);
3794 duration_kind dk;
3796 /* If there isn't a decl in the middle, we don't know the linkage here,
3797 and this isn't a constant expression anyway. */
3798 if (!DECL_P (decl))
3799 return ck_unknown;
3800 dk = decl_storage_duration (decl);
3801 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
3803 #endif
3805 /* Return true if T denotes a potentially constant expression. Issue
3806 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
3807 an lvalue-rvalue conversion is implied.
3809 C++0x [expr.const] used to say
3811 6 An expression is a potential constant expression if it is
3812 a constant expression where all occurrences of function
3813 parameters are replaced by arbitrary constant expressions
3814 of the appropriate type.
3816 2 A conditional expression is a constant expression unless it
3817 involves one of the following as a potentially evaluated
3818 subexpression (3.2), but subexpressions of logical AND (5.14),
3819 logical OR (5.15), and conditional (5.16) operations that are
3820 not evaluated are not considered. */
3822 static bool
3823 potential_constant_expression_1 (tree t, bool want_rval, bool strict,
3824 tsubst_flags_t flags)
3826 #define RECUR(T,RV) potential_constant_expression_1 ((T), (RV), strict, flags)
3827 enum { any = false, rval = true };
3828 int i;
3829 tree tmp;
3831 if (t == error_mark_node)
3832 return false;
3833 if (t == NULL_TREE)
3834 return true;
3835 if (TREE_THIS_VOLATILE (t))
3837 if (flags & tf_error)
3838 error ("expression %qE has side-effects", t);
3839 return false;
3841 if (CONSTANT_CLASS_P (t))
3842 return true;
3844 switch (TREE_CODE (t))
3846 case FUNCTION_DECL:
3847 case BASELINK:
3848 case TEMPLATE_DECL:
3849 case OVERLOAD:
3850 case TEMPLATE_ID_EXPR:
3851 case LABEL_DECL:
3852 case LABEL_EXPR:
3853 case CASE_LABEL_EXPR:
3854 case CONST_DECL:
3855 case SIZEOF_EXPR:
3856 case ALIGNOF_EXPR:
3857 case OFFSETOF_EXPR:
3858 case NOEXCEPT_EXPR:
3859 case TEMPLATE_PARM_INDEX:
3860 case TRAIT_EXPR:
3861 case IDENTIFIER_NODE:
3862 case USERDEF_LITERAL:
3863 /* We can see a FIELD_DECL in a pointer-to-member expression. */
3864 case FIELD_DECL:
3865 case PARM_DECL:
3866 case USING_DECL:
3867 case USING_STMT:
3868 case PLACEHOLDER_EXPR:
3869 case BREAK_STMT:
3870 case CONTINUE_STMT:
3871 return true;
3873 case AGGR_INIT_EXPR:
3874 case CALL_EXPR:
3875 /* -- an invocation of a function other than a constexpr function
3876 or a constexpr constructor. */
3878 tree fun = get_function_named_in_call (t);
3879 const int nargs = call_expr_nargs (t);
3880 i = 0;
3882 if (fun == NULL_TREE)
3884 if (TREE_CODE (t) == CALL_EXPR
3885 && CALL_EXPR_FN (t) == NULL_TREE)
3886 switch (CALL_EXPR_IFN (t))
3888 /* These should be ignored, they are optimized away from
3889 constexpr functions. */
3890 case IFN_UBSAN_NULL:
3891 case IFN_UBSAN_BOUNDS:
3892 case IFN_UBSAN_VPTR:
3893 return true;
3894 default:
3895 break;
3897 /* fold_call_expr can't do anything with IFN calls. */
3898 if (flags & tf_error)
3899 error_at (EXPR_LOC_OR_LOC (t, input_location),
3900 "call to internal function");
3901 return false;
3903 if (is_overloaded_fn (fun))
3905 if (TREE_CODE (fun) == FUNCTION_DECL)
3907 if (builtin_valid_in_constant_expr_p (fun))
3908 return true;
3909 if (!DECL_DECLARED_CONSTEXPR_P (fun)
3910 /* Allow any built-in function; if the expansion
3911 isn't constant, we'll deal with that then. */
3912 && !is_builtin_fn (fun))
3914 if (flags & tf_error)
3916 error_at (EXPR_LOC_OR_LOC (t, input_location),
3917 "call to non-constexpr function %qD", fun);
3918 explain_invalid_constexpr_fn (fun);
3920 return false;
3922 /* A call to a non-static member function takes the address
3923 of the object as the first argument. But in a constant
3924 expression the address will be folded away, so look
3925 through it now. */
3926 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
3927 && !DECL_CONSTRUCTOR_P (fun))
3929 tree x = get_nth_callarg (t, 0);
3930 if (is_this_parameter (x))
3931 return true;
3932 else if (!RECUR (x, rval))
3933 return false;
3934 i = 1;
3937 else
3939 if (!RECUR (fun, true))
3940 return false;
3941 fun = get_first_fn (fun);
3943 /* Skip initial arguments to base constructors. */
3944 if (DECL_BASE_CONSTRUCTOR_P (fun))
3945 i = num_artificial_parms_for (fun);
3946 fun = DECL_ORIGIN (fun);
3948 else
3950 if (RECUR (fun, rval))
3951 /* Might end up being a constant function pointer. */;
3952 else
3953 return false;
3955 for (; i < nargs; ++i)
3957 tree x = get_nth_callarg (t, i);
3958 /* In a template, reference arguments haven't been converted to
3959 REFERENCE_TYPE and we might not even know if the parameter
3960 is a reference, so accept lvalue constants too. */
3961 bool rv = processing_template_decl ? any : rval;
3962 if (!RECUR (x, rv))
3963 return false;
3965 return true;
3968 case NON_LVALUE_EXPR:
3969 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
3970 -- an lvalue of integral type that refers to a non-volatile
3971 const variable or static data member initialized with
3972 constant expressions, or
3974 -- an lvalue of literal type that refers to non-volatile
3975 object defined with constexpr, or that refers to a
3976 sub-object of such an object; */
3977 return RECUR (TREE_OPERAND (t, 0), rval);
3979 case VAR_DECL:
3980 if (want_rval
3981 && !decl_constant_var_p (t)
3982 && (strict
3983 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
3984 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t))
3985 && !var_in_constexpr_fn (t)
3986 && !type_dependent_expression_p (t))
3988 if (flags & tf_error)
3989 non_const_var_error (t);
3990 return false;
3992 return true;
3994 case NOP_EXPR:
3995 case CONVERT_EXPR:
3996 case VIEW_CONVERT_EXPR:
3997 /* -- a reinterpret_cast. FIXME not implemented, and this rule
3998 may change to something more specific to type-punning (DR 1312). */
4000 tree from = TREE_OPERAND (t, 0);
4001 if (POINTER_TYPE_P (TREE_TYPE (t))
4002 && TREE_CODE (from) == INTEGER_CST
4003 && !integer_zerop (from))
4005 if (flags & tf_error)
4006 error_at (EXPR_LOC_OR_LOC (t, input_location),
4007 "reinterpret_cast from integer to pointer");
4008 return false;
4010 return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
4013 case ADDR_EXPR:
4014 /* -- a unary operator & that is applied to an lvalue that
4015 designates an object with thread or automatic storage
4016 duration; */
4017 t = TREE_OPERAND (t, 0);
4019 if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
4020 /* A pointer-to-member constant. */
4021 return true;
4023 #if 0
4024 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
4025 any checking here, as we might dereference the pointer later. If
4026 we remove this code, also remove check_automatic_or_tls. */
4027 i = check_automatic_or_tls (t);
4028 if (i == ck_ok)
4029 return true;
4030 if (i == ck_bad)
4032 if (flags & tf_error)
4033 error ("address-of an object %qE with thread local or "
4034 "automatic storage is not a constant expression", t);
4035 return false;
4037 #endif
4038 return RECUR (t, any);
4040 case COMPONENT_REF:
4041 case BIT_FIELD_REF:
4042 case ARROW_EXPR:
4043 case OFFSET_REF:
4044 /* -- a class member access unless its postfix-expression is
4045 of literal type or of pointer to literal type. */
4046 /* This test would be redundant, as it follows from the
4047 postfix-expression being a potential constant expression. */
4048 return RECUR (TREE_OPERAND (t, 0), want_rval);
4050 case EXPR_PACK_EXPANSION:
4051 return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
4053 case INDIRECT_REF:
4055 tree x = TREE_OPERAND (t, 0);
4056 STRIP_NOPS (x);
4057 if (is_this_parameter (x))
4059 if (DECL_CONTEXT (x)
4060 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x)))
4062 if (flags & tf_error)
4063 error ("use of %<this%> in a constant expression");
4064 return false;
4066 return true;
4068 return RECUR (x, rval);
4071 case STATEMENT_LIST:
4073 tree_stmt_iterator i;
4074 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
4076 if (!RECUR (tsi_stmt (i), any))
4077 return false;
4079 return true;
4081 break;
4083 case MODIFY_EXPR:
4084 if (cxx_dialect < cxx14)
4085 goto fail;
4086 if (!RECUR (TREE_OPERAND (t, 0), any))
4087 return false;
4088 if (!RECUR (TREE_OPERAND (t, 1), rval))
4089 return false;
4090 return true;
4092 case MODOP_EXPR:
4093 if (cxx_dialect < cxx14)
4094 goto fail;
4095 if (!RECUR (TREE_OPERAND (t, 0), rval))
4096 return false;
4097 if (!RECUR (TREE_OPERAND (t, 2), rval))
4098 return false;
4099 return true;
4101 case IF_STMT:
4102 if (!RECUR (IF_COND (t), rval))
4103 return false;
4104 if (!RECUR (THEN_CLAUSE (t), any))
4105 return false;
4106 if (!RECUR (ELSE_CLAUSE (t), any))
4107 return false;
4108 return true;
4110 case DO_STMT:
4111 if (!RECUR (DO_COND (t), rval))
4112 return false;
4113 if (!RECUR (DO_BODY (t), any))
4114 return false;
4115 return true;
4117 case FOR_STMT:
4118 if (!RECUR (FOR_INIT_STMT (t), any))
4119 return false;
4120 if (!RECUR (FOR_COND (t), rval))
4121 return false;
4122 if (!RECUR (FOR_EXPR (t), any))
4123 return false;
4124 if (!RECUR (FOR_BODY (t), any))
4125 return false;
4126 return true;
4128 case WHILE_STMT:
4129 if (!RECUR (WHILE_COND (t), rval))
4130 return false;
4131 if (!RECUR (WHILE_BODY (t), any))
4132 return false;
4133 return true;
4135 case SWITCH_STMT:
4136 if (!RECUR (SWITCH_STMT_COND (t), rval))
4137 return false;
4138 if (!RECUR (SWITCH_STMT_BODY (t), any))
4139 return false;
4140 return true;
4142 case STMT_EXPR:
4143 return RECUR (STMT_EXPR_STMT (t), rval);
4145 case LAMBDA_EXPR:
4146 case DYNAMIC_CAST_EXPR:
4147 case PSEUDO_DTOR_EXPR:
4148 case NEW_EXPR:
4149 case VEC_NEW_EXPR:
4150 case DELETE_EXPR:
4151 case VEC_DELETE_EXPR:
4152 case THROW_EXPR:
4153 case OMP_ATOMIC:
4154 case OMP_ATOMIC_READ:
4155 case OMP_ATOMIC_CAPTURE_OLD:
4156 case OMP_ATOMIC_CAPTURE_NEW:
4157 /* GCC internal stuff. */
4158 case VA_ARG_EXPR:
4159 case OBJ_TYPE_REF:
4160 case TRANSACTION_EXPR:
4161 case ASM_EXPR:
4162 fail:
4163 if (flags & tf_error)
4164 error ("expression %qE is not a constant-expression", t);
4165 return false;
4167 case TYPEID_EXPR:
4168 /* -- a typeid expression whose operand is of polymorphic
4169 class type; */
4171 tree e = TREE_OPERAND (t, 0);
4172 if (!TYPE_P (e) && !type_dependent_expression_p (e)
4173 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
4175 if (flags & tf_error)
4176 error ("typeid-expression is not a constant expression "
4177 "because %qE is of polymorphic type", e);
4178 return false;
4180 return true;
4183 case MINUS_EXPR:
4184 want_rval = true;
4185 goto binary;
4187 case LT_EXPR:
4188 case LE_EXPR:
4189 case GT_EXPR:
4190 case GE_EXPR:
4191 case EQ_EXPR:
4192 case NE_EXPR:
4193 want_rval = true;
4194 goto binary;
4196 case PREINCREMENT_EXPR:
4197 case POSTINCREMENT_EXPR:
4198 case PREDECREMENT_EXPR:
4199 case POSTDECREMENT_EXPR:
4200 if (cxx_dialect < cxx14)
4201 goto fail;
4202 goto unary;
4204 case BIT_NOT_EXPR:
4205 /* A destructor. */
4206 if (TYPE_P (TREE_OPERAND (t, 0)))
4207 return true;
4208 /* else fall through. */
4210 case REALPART_EXPR:
4211 case IMAGPART_EXPR:
4212 case CONJ_EXPR:
4213 case SAVE_EXPR:
4214 case FIX_TRUNC_EXPR:
4215 case FLOAT_EXPR:
4216 case NEGATE_EXPR:
4217 case ABS_EXPR:
4218 case TRUTH_NOT_EXPR:
4219 case FIXED_CONVERT_EXPR:
4220 case UNARY_PLUS_EXPR:
4221 unary:
4222 return RECUR (TREE_OPERAND (t, 0), rval);
4224 case CAST_EXPR:
4225 case CONST_CAST_EXPR:
4226 case STATIC_CAST_EXPR:
4227 case REINTERPRET_CAST_EXPR:
4228 case IMPLICIT_CONV_EXPR:
4229 if (cxx_dialect < cxx11
4230 && !dependent_type_p (TREE_TYPE (t))
4231 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
4232 /* In C++98, a conversion to non-integral type can't be part of a
4233 constant expression. */
4235 if (flags & tf_error)
4236 error ("cast to non-integral type %qT in a constant expression",
4237 TREE_TYPE (t));
4238 return false;
4241 return (RECUR (TREE_OPERAND (t, 0),
4242 TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE));
4244 case BIND_EXPR:
4245 return RECUR (BIND_EXPR_BODY (t), want_rval);
4247 case WITH_CLEANUP_EXPR:
4248 case CLEANUP_POINT_EXPR:
4249 case MUST_NOT_THROW_EXPR:
4250 case TRY_CATCH_EXPR:
4251 case EH_SPEC_BLOCK:
4252 case EXPR_STMT:
4253 case PAREN_EXPR:
4254 case DECL_EXPR:
4255 case NON_DEPENDENT_EXPR:
4256 /* For convenience. */
4257 case RETURN_EXPR:
4258 return RECUR (TREE_OPERAND (t, 0), want_rval);
4260 case SCOPE_REF:
4261 return RECUR (TREE_OPERAND (t, 1), want_rval);
4263 case TARGET_EXPR:
4264 if (!literal_type_p (TREE_TYPE (t)))
4266 if (flags & tf_error)
4268 error ("temporary of non-literal type %qT in a "
4269 "constant expression", TREE_TYPE (t));
4270 explain_non_literal_class (TREE_TYPE (t));
4272 return false;
4274 case INIT_EXPR:
4275 return RECUR (TREE_OPERAND (t, 1), rval);
4277 case CONSTRUCTOR:
4279 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
4280 constructor_elt *ce;
4281 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
4282 if (!RECUR (ce->value, want_rval))
4283 return false;
4284 return true;
4287 case TREE_LIST:
4289 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
4290 || DECL_P (TREE_PURPOSE (t)));
4291 if (!RECUR (TREE_VALUE (t), want_rval))
4292 return false;
4293 if (TREE_CHAIN (t) == NULL_TREE)
4294 return true;
4295 return RECUR (TREE_CHAIN (t), want_rval);
4298 case TRUNC_DIV_EXPR:
4299 case CEIL_DIV_EXPR:
4300 case FLOOR_DIV_EXPR:
4301 case ROUND_DIV_EXPR:
4302 case TRUNC_MOD_EXPR:
4303 case CEIL_MOD_EXPR:
4304 case ROUND_MOD_EXPR:
4306 tree denom = TREE_OPERAND (t, 1);
4307 if (!RECUR (denom, rval))
4308 return false;
4309 /* We can't call cxx_eval_outermost_constant_expr on an expression
4310 that hasn't been through instantiate_non_dependent_expr yet. */
4311 if (!processing_template_decl)
4312 denom = cxx_eval_outermost_constant_expr (denom, true);
4313 if (integer_zerop (denom))
4315 if (flags & tf_error)
4316 error ("division by zero is not a constant-expression");
4317 return false;
4319 else
4321 want_rval = true;
4322 return RECUR (TREE_OPERAND (t, 0), want_rval);
4326 case COMPOUND_EXPR:
4328 /* check_return_expr sometimes wraps a TARGET_EXPR in a
4329 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
4330 introduced by build_call_a. */
4331 tree op0 = TREE_OPERAND (t, 0);
4332 tree op1 = TREE_OPERAND (t, 1);
4333 STRIP_NOPS (op1);
4334 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
4335 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
4336 return RECUR (op0, want_rval);
4337 else
4338 goto binary;
4341 /* If the first operand is the non-short-circuit constant, look at
4342 the second operand; otherwise we only care about the first one for
4343 potentiality. */
4344 case TRUTH_AND_EXPR:
4345 case TRUTH_ANDIF_EXPR:
4346 tmp = boolean_true_node;
4347 goto truth;
4348 case TRUTH_OR_EXPR:
4349 case TRUTH_ORIF_EXPR:
4350 tmp = boolean_false_node;
4351 truth:
4353 tree op = TREE_OPERAND (t, 0);
4354 if (!RECUR (op, rval))
4355 return false;
4356 if (!processing_template_decl)
4357 op = cxx_eval_outermost_constant_expr (op, true);
4358 if (tree_int_cst_equal (op, tmp))
4359 return RECUR (TREE_OPERAND (t, 1), rval);
4360 else
4361 return true;
4364 case PLUS_EXPR:
4365 case MULT_EXPR:
4366 case POINTER_PLUS_EXPR:
4367 case RDIV_EXPR:
4368 case EXACT_DIV_EXPR:
4369 case MIN_EXPR:
4370 case MAX_EXPR:
4371 case LSHIFT_EXPR:
4372 case RSHIFT_EXPR:
4373 case LROTATE_EXPR:
4374 case RROTATE_EXPR:
4375 case BIT_IOR_EXPR:
4376 case BIT_XOR_EXPR:
4377 case BIT_AND_EXPR:
4378 case TRUTH_XOR_EXPR:
4379 case UNORDERED_EXPR:
4380 case ORDERED_EXPR:
4381 case UNLT_EXPR:
4382 case UNLE_EXPR:
4383 case UNGT_EXPR:
4384 case UNGE_EXPR:
4385 case UNEQ_EXPR:
4386 case LTGT_EXPR:
4387 case RANGE_EXPR:
4388 case COMPLEX_EXPR:
4389 want_rval = true;
4390 /* Fall through. */
4391 case ARRAY_REF:
4392 case ARRAY_RANGE_REF:
4393 case MEMBER_REF:
4394 case DOTSTAR_EXPR:
4395 case MEM_REF:
4396 binary:
4397 for (i = 0; i < 2; ++i)
4398 if (!RECUR (TREE_OPERAND (t, i), want_rval))
4399 return false;
4400 return true;
4402 case CILK_SYNC_STMT:
4403 case CILK_SPAWN_STMT:
4404 case ARRAY_NOTATION_REF:
4405 return false;
4407 case FMA_EXPR:
4408 case VEC_PERM_EXPR:
4409 for (i = 0; i < 3; ++i)
4410 if (!RECUR (TREE_OPERAND (t, i), true))
4411 return false;
4412 return true;
4414 case COND_EXPR:
4415 case VEC_COND_EXPR:
4416 /* If the condition is a known constant, we know which of the legs we
4417 care about; otherwise we only require that the condition and
4418 either of the legs be potentially constant. */
4419 tmp = TREE_OPERAND (t, 0);
4420 if (!RECUR (tmp, rval))
4421 return false;
4422 if (!processing_template_decl)
4423 tmp = cxx_eval_outermost_constant_expr (tmp, true);
4424 if (integer_zerop (tmp))
4425 return RECUR (TREE_OPERAND (t, 2), want_rval);
4426 else if (TREE_CODE (tmp) == INTEGER_CST)
4427 return RECUR (TREE_OPERAND (t, 1), want_rval);
4428 for (i = 1; i < 3; ++i)
4429 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
4430 want_rval, strict, tf_none))
4431 return true;
4432 if (flags & tf_error)
4433 error ("expression %qE is not a constant-expression", t);
4434 return false;
4436 case VEC_INIT_EXPR:
4437 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
4438 return true;
4439 if (flags & tf_error)
4441 error ("non-constant array initialization");
4442 diagnose_non_constexpr_vec_init (t);
4444 return false;
4446 default:
4447 if (objc_is_property_ref (t))
4448 return false;
4450 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
4451 gcc_unreachable();
4452 return false;
4454 #undef RECUR
4457 /* The main entry point to the above. */
4459 bool
4460 potential_constant_expression (tree t)
4462 return potential_constant_expression_1 (t, false, true, tf_none);
4465 bool
4466 potential_static_init_expression (tree t)
4468 return potential_constant_expression_1 (t, false, false, tf_none);
4471 /* As above, but require a constant rvalue. */
4473 bool
4474 potential_rvalue_constant_expression (tree t)
4476 return potential_constant_expression_1 (t, true, true, tf_none);
4479 /* Like above, but complain about non-constant expressions. */
4481 bool
4482 require_potential_constant_expression (tree t)
4484 return potential_constant_expression_1 (t, false, true, tf_warning_or_error);
4487 /* Cross product of the above. */
4489 bool
4490 require_potential_rvalue_constant_expression (tree t)
4492 return potential_constant_expression_1 (t, true, true, tf_warning_or_error);
4495 #include "gt-cp-constexpr.h"