]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/constexpr.c
Sync top-level configure.ac with binutils-gdb
[gcc.git] / gcc / cp / constexpr.c
CommitLineData
13f649f6
JM
1/* Perform -*- C++ -*- constant expression evaluation, including calls to
2 constexpr functions. These routines are used both during actual parsing
2d76680f
PC
3 and during the instantiation of template functions.
4
818ab71a 5 Copyright (C) 1998-2016 Free Software Foundation, Inc.
2d76680f
PC
6
7 This file is part of GCC.
8
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.
13
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.
18
19You should have received a copy of the GNU General Public License
20along with GCC; see the file COPYING3. If not see
21<http://www.gnu.org/licenses/>. */
22
23#include "config.h"
24#include "system.h"
25#include "coretypes.h"
2d76680f 26#include "cp-tree.h"
2adfab87 27#include "varasm.h"
2d76680f
PC
28#include "c-family/c-objc.h"
29#include "tree-iterator.h"
30#include "gimplify.h"
31#include "builtins.h"
60813a46 32#include "tree-inline.h"
0b274c17 33#include "ubsan.h"
2d76680f
PC
34
35static bool verify_constant (tree, bool, bool *, bool *);
36#define VERIFY_CONSTANT(X) \
37do { \
2b3ab879 38 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
2d76680f
PC
39 return t; \
40 } while (0)
41
42/* Returns true iff FUN is an instantiation of a constexpr function
43 template or a defaulted constexpr function. */
44
45bool
46is_instantiation_of_constexpr (tree fun)
47{
48 return ((DECL_TEMPLOID_INSTANTIATION (fun)
49 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
50 || (DECL_DEFAULTED_FN (fun)
51 && DECL_DECLARED_CONSTEXPR_P (fun)));
52}
53
54/* Return true if T is a literal type. */
55
56bool
57literal_type_p (tree t)
58{
59 if (SCALAR_TYPE_P (t)
b55b02ea 60 || VECTOR_TYPE_P (t)
e42c407c
MP
61 || TREE_CODE (t) == REFERENCE_TYPE
62 || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
2d76680f
PC
63 return true;
64 if (CLASS_TYPE_P (t))
65 {
66 t = complete_type (t);
67 gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
68 return CLASSTYPE_LITERAL_P (t);
69 }
70 if (TREE_CODE (t) == ARRAY_TYPE)
71 return literal_type_p (strip_array_types (t));
72 return false;
73}
74
75/* If DECL is a variable declared `constexpr', require its type
76 be literal. Return the DECL if OK, otherwise NULL. */
77
78tree
79ensure_literal_type_for_constexpr_object (tree decl)
80{
81 tree type = TREE_TYPE (decl);
82 if (VAR_P (decl)
83 && (DECL_DECLARED_CONSTEXPR_P (decl)
84 || var_in_constexpr_fn (decl))
85 && !processing_template_decl)
86 {
87 tree stype = strip_array_types (type);
88 if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype)))
89 /* Don't complain here, we'll complain about incompleteness
90 when we try to initialize the variable. */;
91 else if (!literal_type_p (type))
92 {
93 if (DECL_DECLARED_CONSTEXPR_P (decl))
f1eac182
JM
94 {
95 error ("the type %qT of constexpr variable %qD is not literal",
96 type, decl);
97 explain_non_literal_class (type);
98 }
2d76680f 99 else
60813a46 100 {
f1eac182
JM
101 if (!DECL_TEMPLATE_INSTANTIATION (current_function_decl))
102 {
103 error ("variable %qD of non-literal type %qT in %<constexpr%> "
104 "function", decl, type);
105 explain_non_literal_class (type);
106 }
60813a46
JM
107 cp_function_chain->invalid_constexpr = true;
108 }
2d76680f
PC
109 return NULL;
110 }
111 }
112 return decl;
113}
114
115/* Representation of entries in the constexpr function definition table. */
116
117struct GTY((for_user)) constexpr_fundef {
118 tree decl;
119 tree body;
120};
121
ca752f39 122struct constexpr_fundef_hasher : ggc_ptr_hash<constexpr_fundef>
2d76680f
PC
123{
124 static hashval_t hash (constexpr_fundef *);
125 static bool equal (constexpr_fundef *, constexpr_fundef *);
126};
127
128/* This table holds all constexpr function definitions seen in
129 the current translation unit. */
130
131static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table;
132
133/* Utility function used for managing the constexpr function table.
134 Return true if the entries pointed to by P and Q are for the
135 same constexpr function. */
136
137inline bool
138constexpr_fundef_hasher::equal (constexpr_fundef *lhs, constexpr_fundef *rhs)
139{
140 return lhs->decl == rhs->decl;
141}
142
143/* Utility function used for managing the constexpr function table.
144 Return a hash value for the entry pointed to by Q. */
145
146inline hashval_t
147constexpr_fundef_hasher::hash (constexpr_fundef *fundef)
148{
149 return DECL_UID (fundef->decl);
150}
151
152/* Return a previously saved definition of function FUN. */
153
154static constexpr_fundef *
155retrieve_constexpr_fundef (tree fun)
156{
157 constexpr_fundef fundef = { NULL, NULL };
158 if (constexpr_fundef_table == NULL)
159 return NULL;
160
161 fundef.decl = fun;
162 return constexpr_fundef_table->find (&fundef);
163}
164
165/* Check whether the parameter and return types of FUN are valid for a
166 constexpr function, and complain if COMPLAIN. */
167
168static bool
169is_valid_constexpr_fn (tree fun, bool complain)
170{
171 bool ret = true;
172
173 if (DECL_INHERITED_CTOR_BASE (fun)
174 && TREE_CODE (fun) == TEMPLATE_DECL)
175 {
176 ret = false;
177 if (complain)
178 error ("inherited constructor %qD is not constexpr",
179 get_inherited_ctor (fun));
180 }
181 else
182 {
183 for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
184 parm != NULL_TREE; parm = TREE_CHAIN (parm))
185 if (!literal_type_p (TREE_TYPE (parm)))
186 {
187 ret = false;
188 if (complain)
189 {
190 error ("invalid type for parameter %d of constexpr "
191 "function %q+#D", DECL_PARM_INDEX (parm), fun);
192 explain_non_literal_class (TREE_TYPE (parm));
193 }
194 }
195 }
196
197 if (!DECL_CONSTRUCTOR_P (fun))
198 {
199 tree rettype = TREE_TYPE (TREE_TYPE (fun));
200 if (!literal_type_p (rettype))
201 {
202 ret = false;
203 if (complain)
204 {
205 error ("invalid return type %qT of constexpr function %q+D",
206 rettype, fun);
207 explain_non_literal_class (rettype);
208 }
209 }
210
211 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
212 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
213 {
214 ret = false;
215 if (complain)
216 {
217 error ("enclosing class of constexpr non-static member "
218 "function %q+#D is not a literal type", fun);
219 explain_non_literal_class (DECL_CONTEXT (fun));
220 }
221 }
222 }
223 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
224 {
225 ret = false;
226 if (complain)
227 error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
228 }
229
230 return ret;
231}
232
233/* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
234 for a member of an anonymous aggregate, INIT is the initializer for that
235 member, and VEC_OUTER is the vector of constructor elements for the class
236 whose constructor we are processing. Add the initializer to the vector
237 and return true to indicate success. */
238
239static bool
240build_anon_member_initialization (tree member, tree init,
241 vec<constructor_elt, va_gc> **vec_outer)
242{
243 /* MEMBER presents the relevant fields from the inside out, but we need
244 to build up the initializer from the outside in so that we can reuse
245 previously built CONSTRUCTORs if this is, say, the second field in an
246 anonymous struct. So we use a vec as a stack. */
247 auto_vec<tree, 2> fields;
248 do
249 {
250 fields.safe_push (TREE_OPERAND (member, 1));
251 member = TREE_OPERAND (member, 0);
252 }
253 while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
254 && TREE_CODE (member) == COMPONENT_REF);
255
256 /* VEC has the constructor elements vector for the context of FIELD.
257 If FIELD is an anonymous aggregate, we will push inside it. */
258 vec<constructor_elt, va_gc> **vec = vec_outer;
259 tree field;
260 while (field = fields.pop(),
261 ANON_AGGR_TYPE_P (TREE_TYPE (field)))
262 {
263 tree ctor;
264 /* If there is already an outer constructor entry for the anonymous
265 aggregate FIELD, use it; otherwise, insert one. */
266 if (vec_safe_is_empty (*vec)
267 || (*vec)->last().index != field)
268 {
269 ctor = build_constructor (TREE_TYPE (field), NULL);
270 CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
271 }
272 else
273 ctor = (*vec)->last().value;
274 vec = &CONSTRUCTOR_ELTS (ctor);
275 }
276
277 /* Now we're at the innermost field, the one that isn't an anonymous
278 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
279 gcc_assert (fields.is_empty());
280 CONSTRUCTOR_APPEND_ELT (*vec, field, init);
281
282 return true;
283}
284
285/* Subroutine of build_constexpr_constructor_member_initializers.
286 The expression tree T represents a data member initialization
287 in a (constexpr) constructor definition. Build a pairing of
288 the data member with its initializer, and prepend that pair
289 to the existing initialization pair INITS. */
290
291static bool
292build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
293{
294 tree member, init;
295 if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
296 t = TREE_OPERAND (t, 0);
297 if (TREE_CODE (t) == EXPR_STMT)
298 t = TREE_OPERAND (t, 0);
299 if (t == error_mark_node)
300 return false;
301 if (TREE_CODE (t) == STATEMENT_LIST)
302 {
303 tree_stmt_iterator i;
304 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
305 {
306 if (! build_data_member_initialization (tsi_stmt (i), vec))
307 return false;
308 }
309 return true;
310 }
311 if (TREE_CODE (t) == CLEANUP_STMT)
312 {
313 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
314 but we can in a constexpr constructor for a non-literal class. Just
315 ignore it; either all the initialization will be constant, in which
316 case the cleanup can't run, or it can't be constexpr.
317 Still recurse into CLEANUP_BODY. */
318 return build_data_member_initialization (CLEANUP_BODY (t), vec);
319 }
320 if (TREE_CODE (t) == CONVERT_EXPR)
321 t = TREE_OPERAND (t, 0);
322 if (TREE_CODE (t) == INIT_EXPR
60813a46
JM
323 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
324 use what this function builds for cx_check_missing_mem_inits, and
325 assignment in the ctor body doesn't count. */
326 || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR))
2d76680f
PC
327 {
328 member = TREE_OPERAND (t, 0);
329 init = break_out_target_exprs (TREE_OPERAND (t, 1));
330 }
331 else if (TREE_CODE (t) == CALL_EXPR)
332 {
60813a46
JM
333 tree fn = get_callee_fndecl (t);
334 if (!fn || !DECL_CONSTRUCTOR_P (fn))
335 /* We're only interested in calls to subobject constructors. */
336 return true;
2d76680f
PC
337 member = CALL_EXPR_ARG (t, 0);
338 /* We don't use build_cplus_new here because it complains about
339 abstract bases. Leaving the call unwrapped means that it has the
340 wrong type, but cxx_eval_constant_expression doesn't care. */
341 init = break_out_target_exprs (t);
342 }
343 else if (TREE_CODE (t) == BIND_EXPR)
344 return build_data_member_initialization (BIND_EXPR_BODY (t), vec);
2d76680f 345 else
60813a46
JM
346 /* Don't add anything else to the CONSTRUCTOR. */
347 return true;
2d76680f
PC
348 if (INDIRECT_REF_P (member))
349 member = TREE_OPERAND (member, 0);
350 if (TREE_CODE (member) == NOP_EXPR)
351 {
352 tree op = member;
353 STRIP_NOPS (op);
354 if (TREE_CODE (op) == ADDR_EXPR)
355 {
356 gcc_assert (same_type_ignoring_top_level_qualifiers_p
357 (TREE_TYPE (TREE_TYPE (op)),
358 TREE_TYPE (TREE_TYPE (member))));
359 /* Initializing a cv-qualified member; we need to look through
360 the const_cast. */
361 member = op;
362 }
363 else if (op == current_class_ptr
364 && (same_type_ignoring_top_level_qualifiers_p
365 (TREE_TYPE (TREE_TYPE (member)),
366 current_class_type)))
367 /* Delegating constructor. */
368 member = op;
369 else
370 {
371 /* This is an initializer for an empty base; keep it for now so
372 we can check it in cxx_eval_bare_aggregate. */
373 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
374 }
375 }
376 if (TREE_CODE (member) == ADDR_EXPR)
377 member = TREE_OPERAND (member, 0);
378 if (TREE_CODE (member) == COMPONENT_REF)
379 {
380 tree aggr = TREE_OPERAND (member, 0);
381 if (TREE_CODE (aggr) != COMPONENT_REF)
382 /* Normal member initialization. */
383 member = TREE_OPERAND (member, 1);
384 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
385 /* Initializing a member of an anonymous union. */
386 return build_anon_member_initialization (member, init, vec);
387 else
388 /* We're initializing a vtable pointer in a base. Leave it as
389 COMPONENT_REF so we remember the path to get to the vfield. */
390 gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
391 }
392
393 CONSTRUCTOR_APPEND_ELT (*vec, member, init);
394 return true;
395}
396
397/* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
398 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
399 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
400
401static bool
402check_constexpr_bind_expr_vars (tree t)
403{
404 gcc_assert (TREE_CODE (t) == BIND_EXPR);
405
2d76680f
PC
406 for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
407 if (TREE_CODE (var) == TYPE_DECL
4414e22e
PC
408 && DECL_IMPLICIT_TYPEDEF_P (var)
409 && !LAMBDA_TYPE_P (TREE_TYPE (var)))
2d76680f
PC
410 return false;
411 return true;
412}
413
414/* Subroutine of check_constexpr_ctor_body. */
415
416static bool
417check_constexpr_ctor_body_1 (tree last, tree list)
418{
419 switch (TREE_CODE (list))
420 {
421 case DECL_EXPR:
422 if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL)
423 return true;
2d76680f
PC
424 return false;
425
426 case CLEANUP_POINT_EXPR:
427 return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
428 /*complain=*/false);
429
430 case BIND_EXPR:
431 if (!check_constexpr_bind_expr_vars (list)
432 || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
433 /*complain=*/false))
434 return false;
435 return true;
436
437 case USING_STMT:
438 case STATIC_ASSERT:
439 return true;
440
441 default:
442 return false;
443 }
444}
445
446/* Make sure that there are no statements after LAST in the constructor
447 body represented by LIST. */
448
449bool
450check_constexpr_ctor_body (tree last, tree list, bool complain)
451{
60813a46
JM
452 /* C++14 doesn't require a constexpr ctor to have an empty body. */
453 if (cxx_dialect >= cxx14)
454 return true;
455
2d76680f
PC
456 bool ok = true;
457 if (TREE_CODE (list) == STATEMENT_LIST)
458 {
459 tree_stmt_iterator i = tsi_last (list);
460 for (; !tsi_end_p (i); tsi_prev (&i))
461 {
462 tree t = tsi_stmt (i);
463 if (t == last)
464 break;
465 if (!check_constexpr_ctor_body_1 (last, t))
466 {
467 ok = false;
468 break;
469 }
470 }
471 }
472 else if (list != last
473 && !check_constexpr_ctor_body_1 (last, list))
474 ok = false;
475 if (!ok)
476 {
477 if (complain)
478 error ("constexpr constructor does not have empty body");
479 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
480 }
481 return ok;
482}
483
484/* V is a vector of constructor elements built up for the base and member
485 initializers of a constructor for TYPE. They need to be in increasing
486 offset order, which they might not be yet if TYPE has a primary base
487 which is not first in the base-clause or a vptr and at least one base
488 all of which are non-primary. */
489
490static vec<constructor_elt, va_gc> *
491sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
492{
493 tree pri = CLASSTYPE_PRIMARY_BINFO (type);
494 tree field_type;
495 unsigned i;
496 constructor_elt *ce;
497
498 if (pri)
499 field_type = BINFO_TYPE (pri);
500 else if (TYPE_CONTAINS_VPTR_P (type))
501 field_type = vtbl_ptr_type_node;
502 else
503 return v;
504
505 /* Find the element for the primary base or vptr and move it to the
506 beginning of the vec. */
507 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
508 if (TREE_TYPE (ce->index) == field_type)
509 break;
510
511 if (i > 0 && i < vec_safe_length (v))
512 {
513 vec<constructor_elt, va_gc> &vref = *v;
514 constructor_elt elt = vref[i];
515 for (; i > 0; --i)
516 vref[i] = vref[i-1];
517 vref[0] = elt;
518 }
519
520 return v;
521}
522
523/* Build compile-time evalable representations of member-initializer list
524 for a constexpr constructor. */
525
526static tree
527build_constexpr_constructor_member_initializers (tree type, tree body)
528{
529 vec<constructor_elt, va_gc> *vec = NULL;
530 bool ok = true;
531 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR
532 || TREE_CODE (body) == EH_SPEC_BLOCK)
533 body = TREE_OPERAND (body, 0);
534 if (TREE_CODE (body) == STATEMENT_LIST)
58cc255c 535 {
30ac6e80
JM
536 for (tree_stmt_iterator i = tsi_start (body);
537 !tsi_end_p (i); tsi_next (&i))
58cc255c
JM
538 {
539 body = tsi_stmt (i);
540 if (TREE_CODE (body) == BIND_EXPR)
541 break;
58cc255c
JM
542 }
543 }
30ac6e80
JM
544 if (TREE_CODE (body) == BIND_EXPR)
545 body = BIND_EXPR_BODY (body);
2d76680f
PC
546 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
547 {
548 body = TREE_OPERAND (body, 0);
549 if (TREE_CODE (body) == EXPR_STMT)
550 body = TREE_OPERAND (body, 0);
551 if (TREE_CODE (body) == INIT_EXPR
552 && (same_type_ignoring_top_level_qualifiers_p
553 (TREE_TYPE (TREE_OPERAND (body, 0)),
554 current_class_type)))
555 {
556 /* Trivial copy. */
557 return TREE_OPERAND (body, 1);
558 }
559 ok = build_data_member_initialization (body, &vec);
560 }
561 else if (TREE_CODE (body) == STATEMENT_LIST)
562 {
563 tree_stmt_iterator i;
564 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
565 {
566 ok = build_data_member_initialization (tsi_stmt (i), &vec);
567 if (!ok)
568 break;
569 }
570 }
571 else if (TREE_CODE (body) == TRY_BLOCK)
572 {
573 error ("body of %<constexpr%> constructor cannot be "
574 "a function-try-block");
575 return error_mark_node;
576 }
577 else if (EXPR_P (body))
578 ok = build_data_member_initialization (body, &vec);
579 else
580 gcc_assert (errorcount > 0);
581 if (ok)
582 {
583 if (vec_safe_length (vec) > 0)
584 {
585 /* In a delegating constructor, return the target. */
586 constructor_elt *ce = &(*vec)[0];
587 if (ce->index == current_class_ptr)
588 {
589 body = ce->value;
590 vec_free (vec);
591 return body;
592 }
593 }
594 vec = sort_constexpr_mem_initializers (type, vec);
595 return build_constructor (type, vec);
596 }
597 else
598 return error_mark_node;
599}
600
601/* Subroutine of register_constexpr_fundef. BODY is the body of a function
602 declared to be constexpr, or a sub-statement thereof. Returns the
603 return value if suitable, error_mark_node for a statement not allowed in
604 a constexpr function, or NULL_TREE if no return value was found. */
605
606static tree
607constexpr_fn_retval (tree body)
608{
609 switch (TREE_CODE (body))
610 {
611 case STATEMENT_LIST:
612 {
613 tree_stmt_iterator i;
614 tree expr = NULL_TREE;
615 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
616 {
617 tree s = constexpr_fn_retval (tsi_stmt (i));
618 if (s == error_mark_node)
619 return error_mark_node;
620 else if (s == NULL_TREE)
621 /* Keep iterating. */;
622 else if (expr)
623 /* Multiple return statements. */
624 return error_mark_node;
625 else
626 expr = s;
627 }
628 return expr;
629 }
630
631 case RETURN_EXPR:
632 return break_out_target_exprs (TREE_OPERAND (body, 0));
633
634 case DECL_EXPR:
0162cb3b
PC
635 {
636 tree decl = DECL_EXPR_DECL (body);
637 if (TREE_CODE (decl) == USING_DECL
638 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
639 || DECL_ARTIFICIAL (decl))
640 return NULL_TREE;
641 return error_mark_node;
642 }
2d76680f
PC
643
644 case CLEANUP_POINT_EXPR:
645 return constexpr_fn_retval (TREE_OPERAND (body, 0));
646
647 case BIND_EXPR:
648 if (!check_constexpr_bind_expr_vars (body))
649 return error_mark_node;
650 return constexpr_fn_retval (BIND_EXPR_BODY (body));
651
652 case USING_STMT:
653 return NULL_TREE;
654
655 default:
656 return error_mark_node;
657 }
658}
659
660/* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
661 FUN; do the necessary transformations to turn it into a single expression
662 that we can store in the hash table. */
663
664static tree
665massage_constexpr_body (tree fun, tree body)
666{
667 if (DECL_CONSTRUCTOR_P (fun))
668 body = build_constexpr_constructor_member_initializers
669 (DECL_CONTEXT (fun), body);
60813a46 670 else if (cxx_dialect < cxx14)
2d76680f
PC
671 {
672 if (TREE_CODE (body) == EH_SPEC_BLOCK)
673 body = EH_SPEC_STMTS (body);
674 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
675 body = TREE_OPERAND (body, 0);
676 body = constexpr_fn_retval (body);
677 }
678 return body;
679}
680
681/* FUN is a constexpr constructor with massaged body BODY. Return true
682 if some bases/fields are uninitialized, and complain if COMPLAIN. */
683
684static bool
685cx_check_missing_mem_inits (tree fun, tree body, bool complain)
686{
687 bool bad;
688 tree field;
689 unsigned i, nelts;
690 tree ctype;
691
692 if (TREE_CODE (body) != CONSTRUCTOR)
693 return false;
694
695 nelts = CONSTRUCTOR_NELTS (body);
696 ctype = DECL_CONTEXT (fun);
697 field = TYPE_FIELDS (ctype);
698
699 if (TREE_CODE (ctype) == UNION_TYPE)
700 {
701 if (nelts == 0 && next_initializable_field (field))
702 {
703 if (complain)
704 error ("%<constexpr%> constructor for union %qT must "
705 "initialize exactly one non-static data member", ctype);
706 return true;
707 }
708 return false;
709 }
710
711 bad = false;
712 for (i = 0; i <= nelts; ++i)
713 {
714 tree index;
715 if (i == nelts)
716 index = NULL_TREE;
717 else
718 {
719 index = CONSTRUCTOR_ELT (body, i)->index;
720 /* Skip base and vtable inits. */
721 if (TREE_CODE (index) != FIELD_DECL
722 || DECL_ARTIFICIAL (index))
723 continue;
724 }
725 for (; field != index; field = DECL_CHAIN (field))
726 {
727 tree ftype;
728 if (TREE_CODE (field) != FIELD_DECL
729 || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
730 || DECL_ARTIFICIAL (field))
731 continue;
732 ftype = strip_array_types (TREE_TYPE (field));
733 if (type_has_constexpr_default_constructor (ftype))
734 {
735 /* It's OK to skip a member with a trivial constexpr ctor.
736 A constexpr ctor that isn't trivial should have been
737 added in by now. */
738 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
739 || errorcount != 0);
740 continue;
741 }
742 if (!complain)
743 return true;
b8cd3996
JM
744 error ("member %qD must be initialized by mem-initializer "
745 "in %<constexpr%> constructor", field);
746 inform (DECL_SOURCE_LOCATION (field), "declared here");
2d76680f
PC
747 bad = true;
748 }
749 if (field == NULL_TREE)
750 break;
751 field = DECL_CHAIN (field);
752 }
753
754 return bad;
755}
756
757/* We are processing the definition of the constexpr function FUN.
758 Check that its BODY fulfills the propriate requirements and
759 enter it in the constexpr function definition table.
760 For constructor BODY is actually the TREE_LIST of the
761 member-initializer list. */
762
763tree
764register_constexpr_fundef (tree fun, tree body)
765{
766 constexpr_fundef entry;
767 constexpr_fundef **slot;
768
769 if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun)))
770 return NULL;
771
772 body = massage_constexpr_body (fun, body);
773 if (body == NULL_TREE || body == error_mark_node)
774 {
775 if (!DECL_CONSTRUCTOR_P (fun))
776 error ("body of constexpr function %qD not a return-statement", fun);
777 return NULL;
778 }
779
780 if (!potential_rvalue_constant_expression (body))
781 {
782 if (!DECL_GENERATED_P (fun))
783 require_potential_rvalue_constant_expression (body);
784 return NULL;
785 }
786
787 if (DECL_CONSTRUCTOR_P (fun)
788 && cx_check_missing_mem_inits (fun, body, !DECL_GENERATED_P (fun)))
789 return NULL;
790
791 /* Create the constexpr function table if necessary. */
792 if (constexpr_fundef_table == NULL)
793 constexpr_fundef_table
794 = hash_table<constexpr_fundef_hasher>::create_ggc (101);
795
796 entry.decl = fun;
797 entry.body = body;
798 slot = constexpr_fundef_table->find_slot (&entry, INSERT);
799
800 gcc_assert (*slot == NULL);
801 *slot = ggc_alloc<constexpr_fundef> ();
802 **slot = entry;
803
804 return fun;
805}
806
807/* FUN is a non-constexpr function called in a context that requires a
808 constant expression. If it comes from a constexpr template, explain why
809 the instantiation isn't constexpr. */
810
811void
812explain_invalid_constexpr_fn (tree fun)
813{
814 static hash_set<tree> *diagnosed;
815 tree body;
816 location_t save_loc;
817 /* Only diagnose defaulted functions or instantiations. */
818 if (!DECL_DEFAULTED_FN (fun)
819 && !is_instantiation_of_constexpr (fun))
820 return;
821 if (diagnosed == NULL)
822 diagnosed = new hash_set<tree>;
823 if (diagnosed->add (fun))
824 /* Already explained. */
825 return;
826
827 save_loc = input_location;
828 input_location = DECL_SOURCE_LOCATION (fun);
4b1cbcee
PC
829 inform (input_location,
830 "%qD is not usable as a constexpr function because:", fun);
2d76680f
PC
831 /* First check the declaration. */
832 if (is_valid_constexpr_fn (fun, true))
833 {
834 /* Then if it's OK, the body. */
835 if (!DECL_DECLARED_CONSTEXPR_P (fun))
836 explain_implicit_non_constexpr (fun);
837 else
838 {
839 body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
840 require_potential_rvalue_constant_expression (body);
841 if (DECL_CONSTRUCTOR_P (fun))
842 cx_check_missing_mem_inits (fun, body, true);
843 }
844 }
845 input_location = save_loc;
846}
847
848/* Objects of this type represent calls to constexpr functions
849 along with the bindings of parameters to their arguments, for
850 the purpose of compile time evaluation. */
851
852struct GTY((for_user)) constexpr_call {
853 /* Description of the constexpr function definition. */
854 constexpr_fundef *fundef;
855 /* Parameter bindings environment. A TREE_LIST where each TREE_PURPOSE
856 is a parameter _DECL and the TREE_VALUE is the value of the parameter.
857 Note: This arrangement is made to accommodate the use of
858 iterative_hash_template_arg (see pt.c). If you change this
859 representation, also change the hash calculation in
860 cxx_eval_call_expression. */
861 tree bindings;
862 /* Result of the call.
863 NULL means the call is being evaluated.
864 error_mark_node means that the evaluation was erroneous;
865 otherwise, the actuall value of the call. */
866 tree result;
867 /* The hash of this call; we remember it here to avoid having to
868 recalculate it when expanding the hash table. */
869 hashval_t hash;
870};
871
ca752f39 872struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call>
2d76680f
PC
873{
874 static hashval_t hash (constexpr_call *);
875 static bool equal (constexpr_call *, constexpr_call *);
3e605b20
JM
876};
877
878/* The constexpr expansion context. CALL is the current function
879 expansion, CTOR is the current aggregate initializer, OBJECT is the
880 object being initialized by CTOR, either a VAR_DECL or a _REF. VALUES
881 is a map of values of variables initialized within the expression. */
882
883struct constexpr_ctx {
13f649f6 884 /* The innermost call we're evaluating. */
3e605b20 885 constexpr_call *call;
13f649f6
JM
886 /* Values for any temporaries or local variables within the
887 constant-expression. */
3e605b20 888 hash_map<tree,tree> *values;
13f649f6
JM
889 /* The CONSTRUCTOR we're currently building up for an aggregate
890 initializer. */
3e605b20 891 tree ctor;
13f649f6 892 /* The object we're building the CONSTRUCTOR for. */
3e605b20 893 tree object;
13f649f6 894 /* Whether we should error on a non-constant expression or fail quietly. */
2b3ab879 895 bool quiet;
13f649f6
JM
896 /* Whether we are strictly conforming to constant expression rules or
897 trying harder to get a constant value. */
69eb4fde 898 bool strict;
3e605b20 899};
2d76680f
PC
900
901/* A table of all constexpr calls that have been evaluated by the
902 compiler in this translation unit. */
903
904static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
905
3e605b20 906static tree cxx_eval_constant_expression (const constexpr_ctx *, tree,
5a804683 907 bool, bool *, bool *, tree * = NULL);
2d76680f
PC
908
909/* Compute a hash value for a constexpr call representation. */
910
911inline hashval_t
912constexpr_call_hasher::hash (constexpr_call *info)
913{
914 return info->hash;
915}
916
917/* Return true if the objects pointed to by P and Q represent calls
918 to the same constexpr function with the same arguments.
919 Otherwise, return false. */
920
921bool
922constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
923{
924 tree lhs_bindings;
925 tree rhs_bindings;
926 if (lhs == rhs)
927 return 1;
928 if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
929 return 0;
930 lhs_bindings = lhs->bindings;
931 rhs_bindings = rhs->bindings;
932 while (lhs_bindings != NULL && rhs_bindings != NULL)
933 {
934 tree lhs_arg = TREE_VALUE (lhs_bindings);
935 tree rhs_arg = TREE_VALUE (rhs_bindings);
936 gcc_assert (TREE_TYPE (lhs_arg) == TREE_TYPE (rhs_arg));
937 if (!cp_tree_equal (lhs_arg, rhs_arg))
938 return 0;
939 lhs_bindings = TREE_CHAIN (lhs_bindings);
940 rhs_bindings = TREE_CHAIN (rhs_bindings);
941 }
942 return lhs_bindings == rhs_bindings;
943}
944
945/* Initialize the constexpr call table, if needed. */
946
947static void
948maybe_initialize_constexpr_call_table (void)
949{
950 if (constexpr_call_table == NULL)
951 constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
952}
953
954/* We have an expression tree T that represents a call, either CALL_EXPR
955 or AGGR_INIT_EXPR. If the call is lexically to a named function,
956 retrun the _DECL for that function. */
957
958static tree
959get_function_named_in_call (tree t)
960{
961 tree fun = NULL;
962 switch (TREE_CODE (t))
963 {
964 case CALL_EXPR:
965 fun = CALL_EXPR_FN (t);
966 break;
967
968 case AGGR_INIT_EXPR:
969 fun = AGGR_INIT_EXPR_FN (t);
970 break;
971
972 default:
973 gcc_unreachable();
974 break;
975 }
60813a46 976 if (fun && TREE_CODE (fun) == ADDR_EXPR
2d76680f
PC
977 && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
978 fun = TREE_OPERAND (fun, 0);
979 return fun;
980}
981
982/* We have an expression tree T that represents a call, either CALL_EXPR
983 or AGGR_INIT_EXPR. Return the Nth argument. */
984
985static inline tree
986get_nth_callarg (tree t, int n)
987{
988 switch (TREE_CODE (t))
989 {
990 case CALL_EXPR:
991 return CALL_EXPR_ARG (t, n);
992
993 case AGGR_INIT_EXPR:
994 return AGGR_INIT_EXPR_ARG (t, n);
995
996 default:
997 gcc_unreachable ();
998 return NULL;
999 }
1000}
1001
2d76680f
PC
1002/* Attempt to evaluate T which represents a call to a builtin function.
1003 We assume here that all builtin functions evaluate to scalar types
1004 represented by _CST nodes. */
1005
1006static tree
5756d0f9 1007cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun,
92a596e8 1008 bool lval,
2d76680f
PC
1009 bool *non_constant_p, bool *overflow_p)
1010{
1011 const int nargs = call_expr_nargs (t);
1012 tree *args = (tree *) alloca (nargs * sizeof (tree));
1013 tree new_call;
1014 int i;
5756d0f9
JM
1015
1016 /* Don't fold __builtin_constant_p within a constexpr function. */
b925d25d
JM
1017 bool bi_const_p = (DECL_FUNCTION_CODE (fun) == BUILT_IN_CONSTANT_P);
1018
1019 if (bi_const_p
5756d0f9
JM
1020 && current_function_decl
1021 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
2d76680f 1022 {
5756d0f9
JM
1023 *non_constant_p = true;
1024 return t;
2d76680f 1025 }
5756d0f9
JM
1026
1027 /* Be permissive for arguments to built-ins; __builtin_constant_p should
1028 return constant false for a non-constant argument. */
1029 constexpr_ctx new_ctx = *ctx;
1030 new_ctx.quiet = true;
1031 bool dummy1 = false, dummy2 = false;
1032 for (i = 0; i < nargs; ++i)
b925d25d
JM
1033 {
1034 args[i] = cxx_eval_constant_expression (&new_ctx, CALL_EXPR_ARG (t, i),
1035 lval, &dummy1, &dummy2);
1036 if (bi_const_p)
1037 /* For __built_in_constant_p, fold all expressions with constant values
1038 even if they aren't C++ constant-expressions. */
1039 args[i] = cp_fully_fold (args[i]);
1040 }
5756d0f9
JM
1041
1042 bool save_ffbcp = force_folding_builtin_constant_p;
1043 force_folding_builtin_constant_p = true;
a6a0570f
RB
1044 new_call = fold_build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
1045 CALL_EXPR_FN (t), nargs, args);
cda0a029
JM
1046 /* Fold away the NOP_EXPR from fold_builtin_n. */
1047 new_call = fold (new_call);
5756d0f9 1048 force_folding_builtin_constant_p = save_ffbcp;
2d76680f
PC
1049 VERIFY_CONSTANT (new_call);
1050 return new_call;
1051}
1052
1053/* TEMP is the constant value of a temporary object of type TYPE. Adjust
1054 the type of the value to match. */
1055
1056static tree
1057adjust_temp_type (tree type, tree temp)
1058{
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);
1066}
1067
1068/* Subroutine of cxx_eval_call_expression.
1069 We are processing a call expression (either CALL_EXPR or
3e605b20 1070 AGGR_INIT_EXPR) in the context of CTX. Evaluate
2d76680f
PC
1071 all arguments and bind their values to correspondings
1072 parameters, making up the NEW_CALL context. */
1073
1074static void
3e605b20 1075cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t,
2d76680f 1076 constexpr_call *new_call,
12d9ce19
JM
1077 bool *non_constant_p, bool *overflow_p,
1078 bool *non_constant_args)
2d76680f
PC
1079{
1080 const int nargs = call_expr_nargs (t);
1081 tree fun = new_call->fundef->decl;
1082 tree parms = DECL_ARGUMENTS (fun);
1083 int i;
60813a46 1084 tree *p = &new_call->bindings;
2d76680f
PC
1085 for (i = 0; i < nargs; ++i)
1086 {
1087 tree x, arg;
1088 tree type = parms ? TREE_TYPE (parms) : void_type_node;
2d76680f 1089 x = get_nth_callarg (t, i);
3e605b20
JM
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
60813a46 1092 which case we get the real argument from ctx. */
3e605b20
JM
1093 if (i == 0 && DECL_CONSTRUCTOR_P (fun)
1094 && is_dummy_object (x))
1095 {
1096 x = ctx->object;
3e605b20
JM
1097 x = cp_build_addr_expr (x, tf_warning_or_error);
1098 }
92a596e8 1099 bool lval = false;
92a596e8 1100 arg = cxx_eval_constant_expression (ctx, x, lval,
5a804683 1101 non_constant_p, overflow_p);
2d76680f 1102 /* Don't VERIFY_CONSTANT here. */
2b3ab879 1103 if (*non_constant_p && ctx->quiet)
2d76680f
PC
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;
1111
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);
12d9ce19
JM
1115 if (!TREE_CONSTANT (arg))
1116 *non_constant_args = true;
60813a46
JM
1117 *p = build_tree_list (parms, arg);
1118 p = &TREE_CHAIN (*p);
2d76680f
PC
1119 next:
1120 parms = TREE_CHAIN (parms);
1121 }
1122}
1123
1124/* Variables and functions to manage constexpr call expansion context.
1125 These do not need to be marked for PCH or GC. */
1126
1127/* FIXME remember and print actual constant arguments. */
1128static vec<tree> call_stack = vNULL;
1129static int call_stack_tick;
1130static int last_cx_error_tick;
1131
1132static bool
1133push_cx_call_context (tree call)
1134{
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;
1142}
1143
1144static void
1145pop_cx_call_context (void)
1146{
1147 ++call_stack_tick;
1148 call_stack.pop ();
1149}
1150
1151vec<tree>
1152cx_error_context (void)
1153{
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;
1160}
1161
1162/* Subroutine of cxx_eval_constant_expression.
1163 Evaluate the call expression tree T in the context of OLD_CALL expression
1164 evaluation. */
1165
1166static tree
3e605b20 1167cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
92a596e8 1168 bool lval,
2d76680f
PC
1169 bool *non_constant_p, bool *overflow_p)
1170{
1171 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
1172 tree fun = get_function_named_in_call (t);
2d76680f 1173 constexpr_call new_call = { NULL, NULL, NULL, 0 };
2d76680f
PC
1174 bool depth_ok;
1175
0b274c17
MP
1176 if (fun == NULL_TREE)
1177 switch (CALL_EXPR_IFN (t))
1178 {
1179 case IFN_UBSAN_NULL:
1180 case IFN_UBSAN_BOUNDS:
35228ac7 1181 case IFN_UBSAN_VPTR:
0b274c17
MP
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;
1188 }
1189
2d76680f
PC
1190 if (TREE_CODE (fun) != FUNCTION_DECL)
1191 {
1192 /* Might be a constexpr function pointer. */
2b3ab879 1193 fun = cxx_eval_constant_expression (ctx, fun,
92a596e8 1194 /*lval*/false, non_constant_p,
5a804683 1195 overflow_p);
2d76680f
PC
1196 STRIP_NOPS (fun);
1197 if (TREE_CODE (fun) == ADDR_EXPR)
1198 fun = TREE_OPERAND (fun, 0);
1199 }
1200 if (TREE_CODE (fun) != FUNCTION_DECL)
1201 {
2b3ab879 1202 if (!ctx->quiet && !*non_constant_p)
2d76680f
PC
1203 error_at (loc, "expression %qE does not designate a constexpr "
1204 "function", fun);
1205 *non_constant_p = true;
1206 return t;
1207 }
1208 if (DECL_CLONED_FUNCTION_P (fun))
1209 fun = DECL_CLONED_FUNCTION (fun);
0b274c17
MP
1210
1211 if (is_ubsan_builtin_p (fun))
1212 return void_node;
1213
2d76680f 1214 if (is_builtin_fn (fun))
5756d0f9 1215 return cxx_eval_builtin_function_call (ctx, t, fun,
92a596e8 1216 lval, non_constant_p, overflow_p);
2d76680f
PC
1217 if (!DECL_DECLARED_CONSTEXPR_P (fun))
1218 {
2b3ab879 1219 if (!ctx->quiet)
2d76680f
PC
1220 {
1221 error_at (loc, "call to non-constexpr function %qD", fun);
1222 explain_invalid_constexpr_fn (fun);
1223 }
1224 *non_constant_p = true;
1225 return t;
1226 }
1227
1228 /* Shortcut trivial constructor/op=. */
1229 if (trivial_fn_p (fun))
1230 {
1231 if (call_expr_nargs (t) == 2)
1232 {
1233 tree arg = convert_from_reference (get_nth_callarg (t, 1));
2b3ab879 1234 return cxx_eval_constant_expression (ctx, arg,
92a596e8 1235 lval, non_constant_p,
5a804683 1236 overflow_p);
2d76680f
PC
1237 }
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);
1241 }
1242
81371eff
JM
1243 /* We can't defer instantiating the function any longer. */
1244 if (!DECL_INITIAL (fun)
1245 && DECL_TEMPLOID_INSTANTIATION (fun))
1246 {
1247 ++function_depth;
1248 instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
1249 --function_depth;
1250 }
1251
2d76680f 1252 /* If in direct recursive call, optimize definition search. */
3e605b20
JM
1253 if (ctx && ctx->call && ctx->call->fundef->decl == fun)
1254 new_call.fundef = ctx->call->fundef;
2d76680f
PC
1255 else
1256 {
1257 new_call.fundef = retrieve_constexpr_fundef (fun);
1258 if (new_call.fundef == NULL || new_call.fundef->body == NULL)
1259 {
2b3ab879 1260 if (!ctx->quiet)
2d76680f
PC
1261 {
1262 if (DECL_INITIAL (fun))
1263 {
1264 /* The definition of fun was somehow unsuitable. */
1265 error_at (loc, "%qD called in a constant expression", fun);
1266 explain_invalid_constexpr_fn (fun);
1267 }
1268 else
1269 error_at (loc, "%qD used before its definition", fun);
1270 }
1271 *non_constant_p = true;
1272 return t;
1273 }
1274 }
60813a46
JM
1275
1276 constexpr_ctx new_ctx = *ctx;
1277 if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
1278 && TREE_CODE (t) == AGGR_INIT_EXPR)
1279 {
1280 /* We want to have an initialization target for an AGGR_INIT_EXPR.
1281 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
1282 new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
1283 tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
1284 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctor) = true;
1285 ctx->values->put (new_ctx.object, ctor);
1286 ctx = &new_ctx;
1287 }
1288
12d9ce19 1289 bool non_constant_args = false;
3e605b20 1290 cxx_bind_parameters_in_call (ctx, t, &new_call,
12d9ce19 1291 non_constant_p, overflow_p, &non_constant_args);
2d76680f
PC
1292 if (*non_constant_p)
1293 return t;
1294
1295 depth_ok = push_cx_call_context (t);
1296
12d9ce19 1297 tree result = NULL_TREE;
2d76680f 1298
12d9ce19 1299 constexpr_call *entry = NULL;
cca444fb 1300 if (depth_ok && !non_constant_args)
2d76680f 1301 {
12d9ce19
JM
1302 new_call.hash = iterative_hash_template_arg
1303 (new_call.bindings, constexpr_fundef_hasher::hash (new_call.fundef));
1304
1305 /* If we have seen this call before, we are done. */
1306 maybe_initialize_constexpr_call_table ();
1307 constexpr_call **slot
1308 = constexpr_call_table->find_slot (&new_call, INSERT);
1309 entry = *slot;
1310 if (entry == NULL)
1311 {
1312 /* We need to keep a pointer to the entry, not just the slot, as the
1313 slot can move in the call to cxx_eval_builtin_function_call. */
1314 *slot = entry = ggc_alloc<constexpr_call> ();
1315 *entry = new_call;
1316 }
1317 /* Calls which are in progress have their result set to NULL
1318 so that we can detect circular dependencies. */
1319 else if (entry->result == NULL)
1320 {
1321 if (!ctx->quiet)
1322 error ("call has circular dependency");
1323 *non_constant_p = true;
1324 entry->result = result = error_mark_node;
1325 }
1326 else
1327 result = entry->result;
2d76680f
PC
1328 }
1329
1330 if (!depth_ok)
1331 {
2b3ab879 1332 if (!ctx->quiet)
2d76680f
PC
1333 error ("constexpr evaluation depth exceeds maximum of %d (use "
1334 "-fconstexpr-depth= to increase the maximum)",
1335 max_constexpr_depth);
1336 *non_constant_p = true;
12d9ce19 1337 result = error_mark_node;
2d76680f
PC
1338 }
1339 else
1340 {
2d76680f 1341 if (!result || result == error_mark_node)
3e605b20 1342 {
0567dcd2
MP
1343 if (DECL_SAVED_TREE (fun) == NULL_TREE
1344 && (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun)))
1345 /* The maybe-in-charge 'tor had its DECL_SAVED_TREE
1346 cleared, try a clone. */
1347 for (fun = DECL_CHAIN (fun);
1348 fun && DECL_CLONED_FUNCTION_P (fun);
1349 fun = DECL_CHAIN (fun))
1350 if (DECL_SAVED_TREE (fun))
1351 break;
1352 gcc_assert (DECL_SAVED_TREE (fun));
1353 tree parms, res;
1354
1355 /* Unshare the whole function body. */
1356 tree body = copy_fn (fun, parms, res);
1357
1358 /* Associate the bindings with the remapped parms. */
1359 tree bound = new_call.bindings;
1360 tree remapped = parms;
1361 while (bound)
60813a46 1362 {
0567dcd2
MP
1363 tree oparm = TREE_PURPOSE (bound);
1364 tree arg = TREE_VALUE (bound);
1365 gcc_assert (DECL_NAME (remapped) == DECL_NAME (oparm));
1366 ctx->values->put (remapped, arg);
1367 bound = TREE_CHAIN (bound);
1368 remapped = DECL_CHAIN (remapped);
60813a46 1369 }
0567dcd2
MP
1370 /* Add the RESULT_DECL to the values map, too. */
1371 tree slot = NULL_TREE;
1372 if (DECL_BY_REFERENCE (res))
60813a46 1373 {
0567dcd2
MP
1374 slot = AGGR_INIT_EXPR_SLOT (t);
1375 tree addr = build_address (slot);
1376 addr = build_nop (TREE_TYPE (res), addr);
1377 ctx->values->put (res, addr);
1378 ctx->values->put (slot, NULL_TREE);
1379 }
1380 else
1381 ctx->values->put (res, NULL_TREE);
60813a46 1382
0567dcd2
MP
1383 tree jump_target = NULL_TREE;
1384 cxx_eval_constant_expression (ctx, body,
1385 lval, non_constant_p, overflow_p,
1386 &jump_target);
60813a46 1387
0567dcd2
MP
1388 if (DECL_CONSTRUCTOR_P (fun))
1389 /* This can be null for a subobject constructor call, in
1390 which case what we care about is the initialization
1391 side-effects rather than the value. We could get at the
1392 value by evaluating *this, but we don't bother; there's
1393 no need to put such a call in the hash table. */
1394 result = lval ? ctx->object : ctx->ctor;
1395 else if (VOID_TYPE_P (TREE_TYPE (res)))
1396 result = void_node;
1397 else
1398 {
1399 result = *ctx->values->get (slot ? slot : res);
1400 if (result == NULL_TREE && !*non_constant_p)
60813a46 1401 {
0567dcd2
MP
1402 if (!ctx->quiet)
1403 error ("constexpr call flows off the end "
1404 "of the function");
1405 *non_constant_p = true;
60813a46 1406 }
60813a46 1407 }
0567dcd2
MP
1408
1409 /* Remove the parms/result from the values map. Is it worth
1410 bothering to do this when the map itself is only live for
1411 one constexpr evaluation? If so, maybe also clear out
1412 other vars from call, maybe in BIND_EXPR handling? */
1413 ctx->values->remove (res);
1414 if (slot)
1415 ctx->values->remove (slot);
1416 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
1417 ctx->values->remove (parm);
3e605b20 1418 }
60813a46 1419
2d76680f
PC
1420 if (result == error_mark_node)
1421 *non_constant_p = true;
1422 if (*non_constant_p)
12d9ce19 1423 result = error_mark_node;
0567dcd2 1424 else if (!result)
60813a46 1425 result = void_node;
12d9ce19
JM
1426 if (entry)
1427 entry->result = result;
2d76680f
PC
1428 }
1429
1430 pop_cx_call_context ();
1431 return unshare_expr (result);
1432}
1433
1434/* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
1435
1436bool
1437reduced_constant_expression_p (tree t)
1438{
1439 switch (TREE_CODE (t))
1440 {
1441 case PTRMEM_CST:
1442 /* Even if we can't lower this yet, it's constant. */
1443 return true;
1444
1445 case CONSTRUCTOR:
1446 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
1447 tree elt; unsigned HOST_WIDE_INT idx;
1448 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), idx, elt)
1449 if (!reduced_constant_expression_p (elt))
1450 return false;
1451 return true;
1452
1453 default:
1454 /* FIXME are we calling this too much? */
1455 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
1456 }
1457}
1458
1459/* Some expressions may have constant operands but are not constant
1460 themselves, such as 1/0. Call this function (or rather, the macro
1461 following it) to check for that condition.
1462
1463 We only call this in places that require an arithmetic constant, not in
1464 places where we might have a non-constant expression that can be a
1465 component of a constant expression, such as the address of a constexpr
1466 variable that might be dereferenced later. */
1467
1468static bool
1469verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
1470 bool *overflow_p)
1471{
1472 if (!*non_constant_p && !reduced_constant_expression_p (t))
1473 {
1474 if (!allow_non_constant)
1475 error ("%q+E is not a constant expression", t);
1476 *non_constant_p = true;
1477 }
1478 if (TREE_OVERFLOW_P (t))
1479 {
1480 if (!allow_non_constant)
1481 {
1482 permerror (input_location, "overflow in constant expression");
1483 /* If we're being permissive (and are in an enforcing
1484 context), ignore the overflow. */
1485 if (flag_permissive)
1486 return *non_constant_p;
1487 }
1488 *overflow_p = true;
1489 }
1490 return *non_constant_p;
1491}
1492
253a921b
MP
1493/* Check whether the shift operation with code CODE and type TYPE on LHS
1494 and RHS is undefined. If it is, give an error with an explanation,
1495 and return true; return false otherwise. */
1496
1497static bool
1498cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
1499 enum tree_code code, tree type, tree lhs, tree rhs)
1500{
1501 if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
1502 || TREE_CODE (lhs) != INTEGER_CST
1503 || TREE_CODE (rhs) != INTEGER_CST)
1504 return false;
1505
1506 tree lhstype = TREE_TYPE (lhs);
1507 unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
1508
1509 /* [expr.shift] The behavior is undefined if the right operand
1510 is negative, or greater than or equal to the length in bits
1511 of the promoted left operand. */
1512 if (tree_int_cst_sgn (rhs) == -1)
1513 {
1514 if (!ctx->quiet)
1515 error_at (loc, "right operand of shift expression %q+E is negative",
1516 build2_loc (loc, code, type, lhs, rhs));
1517 return true;
1518 }
1519 if (compare_tree_int (rhs, uprec) >= 0)
1520 {
1521 if (!ctx->quiet)
1522 error_at (loc, "right operand of shift expression %q+E is >= than "
1523 "the precision of the left operand",
1524 build2_loc (loc, code, type, lhs, rhs));
1525 return true;
1526 }
1527
1528 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
1529 if E1 has a signed type and non-negative value, and E1x2^E2 is
1530 representable in the corresponding unsigned type of the result type,
1531 then that value, converted to the result type, is the resulting value;
1532 otherwise, the behavior is undefined. */
1533 if (code == LSHIFT_EXPR && !TYPE_UNSIGNED (lhstype)
1534 && (cxx_dialect >= cxx11))
1535 {
1536 if (tree_int_cst_sgn (lhs) == -1)
1537 {
1538 if (!ctx->quiet)
1539 error_at (loc, "left operand of shift expression %q+E is negative",
1540 build2_loc (loc, code, type, lhs, rhs));
1541 return true;
1542 }
1543 /* For signed x << y the following:
1544 (unsigned) x >> ((prec (lhs) - 1) - y)
1545 if > 1, is undefined. The right-hand side of this formula
1546 is the highest bit of the LHS that can be set (starting from 0),
1547 so that the shift doesn't overflow. We then right-shift the LHS
1548 to see whether any other bit is set making the original shift
1549 undefined -- the result is not representable in the corresponding
1550 unsigned type. */
1551 tree t = build_int_cst (unsigned_type_node, uprec - 1);
1552 t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
1553 tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
1554 t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
1555 if (tree_int_cst_lt (integer_one_node, t))
1556 {
1557 if (!ctx->quiet)
1558 error_at (loc, "shift expression %q+E overflows",
1559 build2_loc (loc, code, type, lhs, rhs));
1560 return true;
1561 }
1562 }
1563 return false;
1564}
1565
2d76680f
PC
1566/* Subroutine of cxx_eval_constant_expression.
1567 Attempt to reduce the unary expression tree T to a compile time value.
1568 If successful, return the value. Otherwise issue a diagnostic
1569 and return error_mark_node. */
1570
1571static tree
3e605b20 1572cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
12d9ce19 1573 bool /*lval*/,
2d76680f
PC
1574 bool *non_constant_p, bool *overflow_p)
1575{
1576 tree r;
1577 tree orig_arg = TREE_OPERAND (t, 0);
12d9ce19
JM
1578 tree arg = cxx_eval_constant_expression (ctx, orig_arg, /*lval*/false,
1579 non_constant_p, overflow_p);
2d76680f 1580 VERIFY_CONSTANT (arg);
f899317e
JM
1581 location_t loc = EXPR_LOCATION (t);
1582 enum tree_code code = TREE_CODE (t);
1583 tree type = TREE_TYPE (t);
1584 r = fold_unary_loc (loc, code, type, arg);
1585 if (r == NULL_TREE)
1586 {
1587 if (arg == orig_arg)
1588 r = t;
1589 else
1590 r = build1_loc (loc, code, type, arg);
1591 }
2d76680f
PC
1592 VERIFY_CONSTANT (r);
1593 return r;
1594}
1595
1596/* Subroutine of cxx_eval_constant_expression.
1597 Like cxx_eval_unary_expression, except for binary expressions. */
1598
1599static tree
3e605b20 1600cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
12d9ce19 1601 bool /*lval*/,
2d76680f
PC
1602 bool *non_constant_p, bool *overflow_p)
1603{
1604 tree r;
1605 tree orig_lhs = TREE_OPERAND (t, 0);
1606 tree orig_rhs = TREE_OPERAND (t, 1);
1607 tree lhs, rhs;
12d9ce19 1608 lhs = cxx_eval_constant_expression (ctx, orig_lhs, /*lval*/false,
5a804683 1609 non_constant_p, overflow_p);
caee690e
JM
1610 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
1611 a local array in a constexpr function. */
1612 bool ptr = POINTER_TYPE_P (TREE_TYPE (lhs));
1613 if (!ptr)
1614 VERIFY_CONSTANT (lhs);
12d9ce19 1615 rhs = cxx_eval_constant_expression (ctx, orig_rhs, /*lval*/false,
5a804683 1616 non_constant_p, overflow_p);
caee690e 1617 if (!ptr)
134efa82 1618 VERIFY_CONSTANT (rhs);
f899317e
JM
1619
1620 location_t loc = EXPR_LOCATION (t);
1621 enum tree_code code = TREE_CODE (t);
1622 tree type = TREE_TYPE (t);
1623 r = fold_binary_loc (loc, code, type, lhs, rhs);
1624 if (r == NULL_TREE)
1625 {
1626 if (lhs == orig_lhs && rhs == orig_rhs)
1627 r = t;
1628 else
1629 r = build2_loc (loc, code, type, lhs, rhs);
1630 }
253a921b
MP
1631 else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
1632 *non_constant_p = true;
caee690e 1633 if (!ptr)
134efa82 1634 VERIFY_CONSTANT (r);
2d76680f
PC
1635 return r;
1636}
1637
1638/* Subroutine of cxx_eval_constant_expression.
1639 Attempt to evaluate condition expressions. Dead branches are not
1640 looked into. */
1641
1642static tree
3e605b20 1643cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
92a596e8 1644 bool lval,
56632b27
JM
1645 bool *non_constant_p, bool *overflow_p,
1646 tree *jump_target)
2d76680f 1647{
3e605b20 1648 tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
12d9ce19 1649 /*lval*/false,
5a804683 1650 non_constant_p, overflow_p);
2d76680f
PC
1651 VERIFY_CONSTANT (val);
1652 /* Don't VERIFY_CONSTANT the other operands. */
1653 if (integer_zerop (val))
3e605b20 1654 return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
92a596e8 1655 lval,
56632b27
JM
1656 non_constant_p, overflow_p,
1657 jump_target);
3e605b20 1658 return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
92a596e8 1659 lval,
56632b27
JM
1660 non_constant_p, overflow_p,
1661 jump_target);
2d76680f
PC
1662}
1663
ceaaf873
JM
1664/* Returns less than, equal to, or greater than zero if KEY is found to be
1665 less than, to match, or to be greater than the constructor_elt's INDEX. */
1666
1667static int
1668array_index_cmp (tree key, tree index)
1669{
1670 gcc_assert (TREE_CODE (key) == INTEGER_CST);
1671
1672 switch (TREE_CODE (index))
1673 {
1674 case INTEGER_CST:
1675 return tree_int_cst_compare (key, index);
1676 case RANGE_EXPR:
1677 {
1678 tree lo = TREE_OPERAND (index, 0);
1679 tree hi = TREE_OPERAND (index, 1);
1680 if (tree_int_cst_lt (key, lo))
1681 return -1;
1682 else if (tree_int_cst_lt (hi, key))
1683 return 1;
1684 else
1685 return 0;
1686 }
1687 default:
1688 gcc_unreachable ();
1689 }
1690}
1691
1692/* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
1693 if none. If INSERT is true, insert a matching element rather than fail. */
1694
1695static HOST_WIDE_INT
1696find_array_ctor_elt (tree ary, tree dindex, bool insert = false)
1697{
1698 if (tree_int_cst_sgn (dindex) < 0)
1699 return -1;
1700
1701 unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
1702 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
1703 unsigned HOST_WIDE_INT len = vec_safe_length (elts);
1704
1705 unsigned HOST_WIDE_INT end = len;
1706 unsigned HOST_WIDE_INT begin = 0;
1707
1708 /* If the last element of the CONSTRUCTOR has its own index, we can assume
1709 that the same is true of the other elements and index directly. */
1710 if (end > 0)
1711 {
1712 tree cindex = (*elts)[end-1].index;
1713 if (TREE_CODE (cindex) == INTEGER_CST
1714 && compare_tree_int (cindex, end-1) == 0)
1715 {
1716 if (i < end)
1717 return i;
1718 else
1719 begin = end;
1720 }
1721 }
1722
1723 /* Otherwise, find a matching index by means of a binary search. */
1724 while (begin != end)
1725 {
1726 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
1727
1728 int cmp = array_index_cmp (dindex, (*elts)[middle].index);
1729 if (cmp < 0)
1730 end = middle;
1731 else if (cmp > 0)
1732 begin = middle + 1;
1733 else
1734 return middle;
1735 }
1736
1737 if (insert)
1738 {
1739 constructor_elt e = { dindex, NULL_TREE };
1740 vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
1741 return end;
1742 }
1743
1744 return -1;
1745}
1746
1747
2d76680f
PC
1748/* Subroutine of cxx_eval_constant_expression.
1749 Attempt to reduce a reference to an array slot. */
1750
1751static tree
3e605b20 1752cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
92a596e8 1753 bool lval,
2d76680f
PC
1754 bool *non_constant_p, bool *overflow_p)
1755{
1756 tree oldary = TREE_OPERAND (t, 0);
3e605b20 1757 tree ary = cxx_eval_constant_expression (ctx, oldary,
92a596e8 1758 lval,
5a804683 1759 non_constant_p, overflow_p);
2d76680f
PC
1760 tree index, oldidx;
1761 HOST_WIDE_INT i;
1762 tree elem_type;
1763 unsigned len, elem_nchars = 1;
1764 if (*non_constant_p)
1765 return t;
1766 oldidx = TREE_OPERAND (t, 1);
3e605b20 1767 index = cxx_eval_constant_expression (ctx, oldidx,
2b3ab879 1768 false,
5a804683 1769 non_constant_p, overflow_p);
2d76680f 1770 VERIFY_CONSTANT (index);
92a596e8 1771 if (lval && ary == oldary && index == oldidx)
2d76680f 1772 return t;
92a596e8 1773 else if (lval)
2d76680f
PC
1774 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
1775 elem_type = TREE_TYPE (TREE_TYPE (ary));
1776 if (TREE_CODE (ary) == CONSTRUCTOR)
1777 len = CONSTRUCTOR_NELTS (ary);
1778 else if (TREE_CODE (ary) == STRING_CST)
1779 {
1780 elem_nchars = (TYPE_PRECISION (elem_type)
1781 / TYPE_PRECISION (char_type_node));
1782 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
1783 }
1784 else
1785 {
1786 /* We can't do anything with other tree codes, so use
1787 VERIFY_CONSTANT to complain and fail. */
1788 VERIFY_CONSTANT (ary);
1789 gcc_unreachable ();
1790 }
3b9997bb 1791
c2127564
MT
1792 if (!tree_fits_shwi_p (index)
1793 || (i = tree_to_shwi (index)) < 0)
ceaaf873
JM
1794 {
1795 if (!ctx->quiet)
1796 error ("negative array subscript");
1797 *non_constant_p = true;
1798 return t;
1799 }
1800
1801 bool found;
1802 if (TREE_CODE (ary) == CONSTRUCTOR)
1803 {
1804 HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
1805 found = (ix >= 0);
1806 if (found)
1807 i = ix;
3b9997bb 1808 }
ceaaf873
JM
1809 else
1810 found = (i < len);
3b9997bb 1811
ceaaf873 1812 if (!found)
2d76680f
PC
1813 {
1814 if (tree_int_cst_lt (index, array_type_nelts_top (TREE_TYPE (ary))))
1815 {
c75ce530
JM
1816 if (TREE_CODE (ary) == CONSTRUCTOR
1817 && CONSTRUCTOR_NO_IMPLICIT_ZERO (ary))
1818 {
1819 /* 'ary' is part of the aggregate initializer we're currently
1820 building; if there's no initializer for this element yet,
1821 that's an error. */
1822 if (!ctx->quiet)
1823 error ("accessing uninitialized array element");
1824 *non_constant_p = true;
1825 return t;
1826 }
1827
2d76680f
PC
1828 /* If it's within the array bounds but doesn't have an explicit
1829 initializer, it's value-initialized. */
1830 tree val = build_value_init (elem_type, tf_warning_or_error);
3e605b20 1831 return cxx_eval_constant_expression (ctx, val,
92a596e8 1832 lval,
5a804683 1833 non_constant_p, overflow_p);
2d76680f
PC
1834 }
1835
2b3ab879 1836 if (!ctx->quiet)
2d76680f
PC
1837 error ("array subscript out of bound");
1838 *non_constant_p = true;
1839 return t;
1840 }
3b9997bb 1841
2d76680f
PC
1842 if (TREE_CODE (ary) == CONSTRUCTOR)
1843 return (*CONSTRUCTOR_ELTS (ary))[i].value;
1844 else if (elem_nchars == 1)
1845 return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary))),
1846 TREE_STRING_POINTER (ary)[i]);
1847 else
1848 {
1849 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (ary)));
1850 return native_interpret_expr (type, (const unsigned char *)
1851 TREE_STRING_POINTER (ary)
1852 + i * elem_nchars, elem_nchars);
1853 }
1854 /* Don't VERIFY_CONSTANT here. */
1855}
1856
1857/* Subroutine of cxx_eval_constant_expression.
1858 Attempt to reduce a field access of a value of class type. */
1859
1860static tree
3e605b20 1861cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
92a596e8 1862 bool lval,
2d76680f
PC
1863 bool *non_constant_p, bool *overflow_p)
1864{
1865 unsigned HOST_WIDE_INT i;
1866 tree field;
1867 tree value;
1868 tree part = TREE_OPERAND (t, 1);
1869 tree orig_whole = TREE_OPERAND (t, 0);
3e605b20 1870 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
92a596e8 1871 lval,
5a804683 1872 non_constant_p, overflow_p);
dcdbc004
JM
1873 if (TREE_CODE (whole) == PTRMEM_CST)
1874 whole = cplus_expand_constant (whole);
2d76680f
PC
1875 if (whole == orig_whole)
1876 return t;
92a596e8 1877 if (lval)
2d76680f
PC
1878 return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
1879 whole, part, NULL_TREE);
1880 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
1881 CONSTRUCTOR. */
1882 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
1883 {
2b3ab879 1884 if (!ctx->quiet)
2d76680f
PC
1885 error ("%qE is not a constant expression", orig_whole);
1886 *non_constant_p = true;
1887 }
1888 if (DECL_MUTABLE_P (part))
1889 {
2b3ab879 1890 if (!ctx->quiet)
2d76680f
PC
1891 error ("mutable %qD is not usable in a constant expression", part);
1892 *non_constant_p = true;
1893 }
1894 if (*non_constant_p)
1895 return t;
1896 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
1897 {
1898 if (field == part)
60813a46
JM
1899 {
1900 if (value)
1901 return value;
1902 else
1903 /* We're in the middle of initializing it. */
1904 break;
1905 }
2d76680f
PC
1906 }
1907 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
1908 && CONSTRUCTOR_NELTS (whole) > 0)
1909 {
1910 /* DR 1188 says we don't have to deal with this. */
2b3ab879 1911 if (!ctx->quiet)
2d76680f
PC
1912 error ("accessing %qD member instead of initialized %qD member in "
1913 "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index);
1914 *non_constant_p = true;
1915 return t;
1916 }
1917
3e605b20
JM
1918 if (CONSTRUCTOR_NO_IMPLICIT_ZERO (whole))
1919 {
1920 /* 'whole' is part of the aggregate initializer we're currently
1921 building; if there's no initializer for this member yet, that's an
1922 error. */
2b3ab879 1923 if (!ctx->quiet)
3e605b20
JM
1924 error ("accessing uninitialized member %qD", part);
1925 *non_constant_p = true;
1926 return t;
1927 }
1928
2d76680f
PC
1929 /* If there's no explicit init for this field, it's value-initialized. */
1930 value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
3e605b20 1931 return cxx_eval_constant_expression (ctx, value,
92a596e8 1932 lval,
5a804683 1933 non_constant_p, overflow_p);
2d76680f
PC
1934}
1935
1936/* Subroutine of cxx_eval_constant_expression.
1937 Attempt to reduce a field access of a value of class type that is
1938 expressed as a BIT_FIELD_REF. */
1939
1940static tree
3e605b20 1941cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
92a596e8 1942 bool lval,
2d76680f
PC
1943 bool *non_constant_p, bool *overflow_p)
1944{
1945 tree orig_whole = TREE_OPERAND (t, 0);
1946 tree retval, fldval, utype, mask;
1947 bool fld_seen = false;
1948 HOST_WIDE_INT istart, isize;
3e605b20 1949 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
92a596e8 1950 lval,
5a804683 1951 non_constant_p, overflow_p);
2d76680f
PC
1952 tree start, field, value;
1953 unsigned HOST_WIDE_INT i;
1954
1955 if (whole == orig_whole)
1956 return t;
1957 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
1958 CONSTRUCTOR. */
1959 if (!*non_constant_p
1960 && TREE_CODE (whole) != VECTOR_CST
1961 && TREE_CODE (whole) != CONSTRUCTOR)
1962 {
2b3ab879 1963 if (!ctx->quiet)
2d76680f
PC
1964 error ("%qE is not a constant expression", orig_whole);
1965 *non_constant_p = true;
1966 }
1967 if (*non_constant_p)
1968 return t;
1969
1970 if (TREE_CODE (whole) == VECTOR_CST)
1971 return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
1972 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
1973
1974 start = TREE_OPERAND (t, 2);
1975 istart = tree_to_shwi (start);
1976 isize = tree_to_shwi (TREE_OPERAND (t, 1));
1977 utype = TREE_TYPE (t);
1978 if (!TYPE_UNSIGNED (utype))
1979 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
1980 retval = build_int_cst (utype, 0);
1981 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
1982 {
1983 tree bitpos = bit_position (field);
1984 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
1985 return value;
1986 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
1987 && TREE_CODE (value) == INTEGER_CST
1988 && tree_fits_shwi_p (bitpos)
1989 && tree_fits_shwi_p (DECL_SIZE (field)))
1990 {
1991 HOST_WIDE_INT bit = tree_to_shwi (bitpos);
1992 HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
1993 HOST_WIDE_INT shift;
1994 if (bit >= istart && bit + sz <= istart + isize)
1995 {
1996 fldval = fold_convert (utype, value);
1997 mask = build_int_cst_type (utype, -1);
1998 mask = fold_build2 (LSHIFT_EXPR, utype, mask,
1999 size_int (TYPE_PRECISION (utype) - sz));
2000 mask = fold_build2 (RSHIFT_EXPR, utype, mask,
2001 size_int (TYPE_PRECISION (utype) - sz));
2002 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
2003 shift = bit - istart;
2004 if (BYTES_BIG_ENDIAN)
2005 shift = TYPE_PRECISION (utype) - shift - sz;
2006 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
2007 size_int (shift));
2008 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
2009 fld_seen = true;
2010 }
2011 }
2012 }
2013 if (fld_seen)
2014 return fold_convert (TREE_TYPE (t), retval);
2015 gcc_unreachable ();
2016 return error_mark_node;
2017}
2018
2019/* Subroutine of cxx_eval_constant_expression.
2020 Evaluate a short-circuited logical expression T in the context
2021 of a given constexpr CALL. BAILOUT_VALUE is the value for
2022 early return. CONTINUE_VALUE is used here purely for
2023 sanity check purposes. */
2024
2025static tree
3e605b20 2026cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
2d76680f 2027 tree bailout_value, tree continue_value,
92a596e8 2028 bool lval,
2d76680f
PC
2029 bool *non_constant_p, bool *overflow_p)
2030{
2031 tree r;
3e605b20 2032 tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
92a596e8 2033 lval,
5a804683 2034 non_constant_p, overflow_p);
2d76680f
PC
2035 VERIFY_CONSTANT (lhs);
2036 if (tree_int_cst_equal (lhs, bailout_value))
2037 return lhs;
2038 gcc_assert (tree_int_cst_equal (lhs, continue_value));
3e605b20 2039 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
92a596e8 2040 lval, non_constant_p,
5a804683 2041 overflow_p);
2d76680f
PC
2042 VERIFY_CONSTANT (r);
2043 return r;
2044}
2045
2046/* REF is a COMPONENT_REF designating a particular field. V is a vector of
2047 CONSTRUCTOR elements to initialize (part of) an object containing that
2048 field. Return a pointer to the constructor_elt corresponding to the
2049 initialization of the field. */
2050
2051static constructor_elt *
2052base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
2053{
2054 tree aggr = TREE_OPERAND (ref, 0);
2055 tree field = TREE_OPERAND (ref, 1);
2056 HOST_WIDE_INT i;
2057 constructor_elt *ce;
2058
2059 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
2060
2061 if (TREE_CODE (aggr) == COMPONENT_REF)
2062 {
2063 constructor_elt *base_ce
2064 = base_field_constructor_elt (v, aggr);
2065 v = CONSTRUCTOR_ELTS (base_ce->value);
2066 }
2067
2068 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
2069 if (ce->index == field)
2070 return ce;
2071
2072 gcc_unreachable ();
2073 return NULL;
2074}
2075
3e605b20
JM
2076/* Some of the expressions fed to the constexpr mechanism are calls to
2077 constructors, which have type void. In that case, return the type being
2078 initialized by the constructor. */
2079
2080static tree
2081initialized_type (tree t)
2082{
2083 if (TYPE_P (t))
2084 return t;
2085 tree type = cv_unqualified (TREE_TYPE (t));
2086 if (TREE_CODE (t) == CALL_EXPR || TREE_CODE (t) == AGGR_INIT_EXPR)
2087 {
2088 /* A constructor call has void type, so we need to look deeper. */
2089 tree fn = get_function_named_in_call (t);
2090 if (fn && TREE_CODE (fn) == FUNCTION_DECL
2091 && DECL_CXX_CONSTRUCTOR_P (fn))
2092 type = DECL_CONTEXT (fn);
2093 }
2094 return type;
2095}
2096
2097/* We're about to initialize element INDEX of an array or class from VALUE.
2098 Set up NEW_CTX appropriately by adjusting .object to refer to the
2099 subobject and creating a new CONSTRUCTOR if the element is itself
2100 a class or array. */
2101
2102static void
2103init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
2104 tree index, tree &value)
2105{
2106 new_ctx = *ctx;
2107
2108 if (index && TREE_CODE (index) != INTEGER_CST
2109 && TREE_CODE (index) != FIELD_DECL)
2110 /* This won't have an element in the new CONSTRUCTOR. */
2111 return;
2112
2113 tree type = initialized_type (value);
2114 if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
2115 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
2116 return;
2117
2118 /* The sub-aggregate initializer might contain a placeholder;
2119 update object to refer to the subobject and ctor to refer to
2120 the (newly created) sub-initializer. */
2121 if (ctx->object)
2122 new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
2123 tree elt = build_constructor (type, NULL);
2124 CONSTRUCTOR_NO_IMPLICIT_ZERO (elt) = true;
2125 new_ctx.ctor = elt;
2126
2127 if (TREE_CODE (value) == TARGET_EXPR)
2128 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
2129 value = TARGET_EXPR_INITIAL (value);
2130}
2131
2132/* We're about to process an initializer for a class or array TYPE. Make
2133 sure that CTX is set up appropriately. */
2134
2135static void
2136verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
2137{
2138 /* We don't bother building a ctor for an empty base subobject. */
2139 if (is_empty_class (type))
2140 return;
2141
2142 /* We're in the middle of an initializer that might involve placeholders;
2143 our caller should have created a CONSTRUCTOR for us to put the
2144 initializer into. We will either return that constructor or T. */
2145 gcc_assert (ctx->ctor);
2146 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2147 (type, TREE_TYPE (ctx->ctor)));
2148 gcc_assert (CONSTRUCTOR_NELTS (ctx->ctor) == 0);
2149 if (ctx->object)
2150 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2151 (type, TREE_TYPE (ctx->object)));
2152 gcc_assert (!ctx->object || !DECL_P (ctx->object)
2153 || *(ctx->values->get (ctx->object)) == ctx->ctor);
2154}
2155
2d76680f
PC
2156/* Subroutine of cxx_eval_constant_expression.
2157 The expression tree T denotes a C-style array or a C-style
2158 aggregate. Reduce it to a constant expression. */
2159
2160static tree
3e605b20 2161cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
92a596e8 2162 bool lval,
2d76680f
PC
2163 bool *non_constant_p, bool *overflow_p)
2164{
2165 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
2d76680f
PC
2166 bool changed = false;
2167 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
3e605b20
JM
2168
2169 verify_ctor_sanity (ctx, TREE_TYPE (t));
2170 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
2171 vec_alloc (*p, vec_safe_length (v));
2172
2173 unsigned i; tree index, value;
2174 FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
2d76680f 2175 {
3e605b20
JM
2176 constexpr_ctx new_ctx;
2177 init_subob_ctx (ctx, new_ctx, index, value);
2178 if (new_ctx.ctor != ctx->ctor)
2179 /* If we built a new CONSTRUCTOR, attach it now so that other
2180 initializers can refer to it. */
2181 CONSTRUCTOR_APPEND_ELT (*p, index, new_ctx.ctor);
2182 tree elt = cxx_eval_constant_expression (&new_ctx, value,
92a596e8 2183 lval,
5a804683 2184 non_constant_p, overflow_p);
2d76680f 2185 /* Don't VERIFY_CONSTANT here. */
2b3ab879 2186 if (ctx->quiet && *non_constant_p)
3e605b20
JM
2187 break;
2188 if (elt != value)
2d76680f 2189 changed = true;
3e605b20 2190 if (index && TREE_CODE (index) == COMPONENT_REF)
2d76680f
PC
2191 {
2192 /* This is an initialization of a vfield inside a base
2193 subaggregate that we already initialized; push this
2194 initialization into the previous initialization. */
3e605b20 2195 constructor_elt *inner = base_field_constructor_elt (*p, index);
2d76680f 2196 inner->value = elt;
3e605b20 2197 changed = true;
2d76680f 2198 }
3e605b20
JM
2199 else if (index
2200 && (TREE_CODE (index) == NOP_EXPR
2201 || TREE_CODE (index) == POINTER_PLUS_EXPR))
2d76680f
PC
2202 {
2203 /* This is an initializer for an empty base; now that we've
2204 checked that it's constant, we can ignore it. */
3e605b20
JM
2205 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
2206 changed = true;
2207 }
2208 else if (new_ctx.ctor != ctx->ctor)
2209 {
2210 /* We appended this element above; update the value. */
2211 gcc_assert ((*p)->last().index == index);
2212 (*p)->last().value = elt;
2d76680f
PC
2213 }
2214 else
3e605b20 2215 CONSTRUCTOR_APPEND_ELT (*p, index, elt);
2d76680f
PC
2216 }
2217 if (*non_constant_p || !changed)
3e605b20
JM
2218 return t;
2219 t = ctx->ctor;
2220 /* We're done building this CONSTRUCTOR, so now we can interpret an
2221 element without an explicit initializer as value-initialized. */
2222 CONSTRUCTOR_NO_IMPLICIT_ZERO (t) = false;
b55b02ea 2223 if (VECTOR_TYPE_P (TREE_TYPE (t)))
2d76680f
PC
2224 t = fold (t);
2225 return t;
2226}
2227
2228/* Subroutine of cxx_eval_constant_expression.
2229 The expression tree T is a VEC_INIT_EXPR which denotes the desired
2230 initialization of a non-static data member of array type. Reduce it to a
2231 CONSTRUCTOR.
2232
2233 Note that apart from value-initialization (when VALUE_INIT is true),
2234 this is only intended to support value-initialization and the
2235 initializations done by defaulted constructors for classes with
2236 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
2237 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
2238 for the copy/move constructor. */
2239
2240static tree
3e605b20 2241cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
92a596e8 2242 bool value_init, bool lval,
2d76680f
PC
2243 bool *non_constant_p, bool *overflow_p)
2244{
2245 tree elttype = TREE_TYPE (atype);
4784470a 2246 unsigned HOST_WIDE_INT max = tree_to_uhwi (array_type_nelts_top (atype));
3e605b20
JM
2247 verify_ctor_sanity (ctx, atype);
2248 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
2249 vec_alloc (*p, max + 1);
2d76680f 2250 bool pre_init = false;
4784470a 2251 unsigned HOST_WIDE_INT i;
2d76680f
PC
2252
2253 /* For the default constructor, build up a call to the default
2254 constructor of the element type. We only need to handle class types
2255 here, as for a constructor to be constexpr, all members must be
2256 initialized, which for a defaulted default constructor means they must
2257 be of a class type with a constexpr default constructor. */
2258 if (TREE_CODE (elttype) == ARRAY_TYPE)
2259 /* We only do this at the lowest level. */;
2260 else if (value_init)
2261 {
2262 init = build_value_init (elttype, tf_warning_or_error);
2d76680f
PC
2263 pre_init = true;
2264 }
2265 else if (!init)
2266 {
2267 vec<tree, va_gc> *argvec = make_tree_vector ();
2268 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
2269 &argvec, elttype, LOOKUP_NORMAL,
2270 tf_warning_or_error);
2271 release_tree_vector (argvec);
60813a46 2272 init = build_aggr_init_expr (TREE_TYPE (init), init);
2d76680f
PC
2273 pre_init = true;
2274 }
2275
4784470a 2276 for (i = 0; i < max; ++i)
2d76680f
PC
2277 {
2278 tree idx = build_int_cst (size_type_node, i);
2279 tree eltinit;
3e605b20
JM
2280 constexpr_ctx new_ctx;
2281 init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
2282 if (new_ctx.ctor != ctx->ctor)
2283 CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
2d76680f
PC
2284 if (TREE_CODE (elttype) == ARRAY_TYPE)
2285 {
2286 /* A multidimensional array; recurse. */
2287 if (value_init || init == NULL_TREE)
2288 eltinit = NULL_TREE;
2289 else
2290 eltinit = cp_build_array_ref (input_location, init, idx,
2291 tf_warning_or_error);
3e605b20 2292 eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init,
92a596e8 2293 lval,
2d76680f
PC
2294 non_constant_p, overflow_p);
2295 }
2296 else if (pre_init)
2297 {
2298 /* Initializing an element using value or default initialization
2299 we just pre-built above. */
3e605b20 2300 eltinit = (cxx_eval_constant_expression
2b3ab879 2301 (&new_ctx, init,
92a596e8 2302 lval, non_constant_p, overflow_p));
2d76680f
PC
2303 }
2304 else
2305 {
2306 /* Copying an element. */
2307 gcc_assert (same_type_ignoring_top_level_qualifiers_p
2308 (atype, TREE_TYPE (init)));
2309 eltinit = cp_build_array_ref (input_location, init, idx,
2310 tf_warning_or_error);
2311 if (!real_lvalue_p (init))
2312 eltinit = move (eltinit);
2313 eltinit = force_rvalue (eltinit, tf_warning_or_error);
3e605b20 2314 eltinit = (cxx_eval_constant_expression
92a596e8 2315 (&new_ctx, eltinit, lval,
5a804683 2316 non_constant_p, overflow_p));
2d76680f 2317 }
2b3ab879 2318 if (*non_constant_p && !ctx->quiet)
3e605b20
JM
2319 break;
2320 if (new_ctx.ctor != ctx->ctor)
2321 {
2322 /* We appended this element above; update the value. */
2323 gcc_assert ((*p)->last().index == idx);
2324 (*p)->last().value = eltinit;
2325 }
2326 else
2327 CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
2d76680f
PC
2328 }
2329
2330 if (!*non_constant_p)
2331 {
3e605b20
JM
2332 init = ctx->ctor;
2333 CONSTRUCTOR_NO_IMPLICIT_ZERO (init) = false;
2d76680f 2334 }
2d76680f
PC
2335 return init;
2336}
2337
2338static tree
3e605b20 2339cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
92a596e8 2340 bool lval,
2d76680f
PC
2341 bool *non_constant_p, bool *overflow_p)
2342{
2343 tree atype = TREE_TYPE (t);
2344 tree init = VEC_INIT_EXPR_INIT (t);
3e605b20 2345 tree r = cxx_eval_vec_init_1 (ctx, atype, init,
2d76680f 2346 VEC_INIT_EXPR_VALUE_INIT (t),
92a596e8 2347 lval, non_constant_p, overflow_p);
2d76680f
PC
2348 if (*non_constant_p)
2349 return t;
2350 else
2351 return r;
2352}
2353
2354/* A less strict version of fold_indirect_ref_1, which requires cv-quals to
2355 match. We want to be less strict for simple *& folding; if we have a
2356 non-const temporary that we access through a const pointer, that should
2357 work. We handle this here rather than change fold_indirect_ref_1
2358 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
2359 don't really make sense outside of constant expression evaluation. Also
2360 we want to allow folding to COMPONENT_REF, which could cause trouble
2361 with TBAA in fold_indirect_ref_1.
2362
2363 Try to keep this function synced with fold_indirect_ref_1. */
2364
2365static tree
2366cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
2367{
2368 tree sub, subtype;
2369
2370 sub = op0;
2371 STRIP_NOPS (sub);
2372 subtype = TREE_TYPE (sub);
2373 if (!POINTER_TYPE_P (subtype))
2374 return NULL_TREE;
2375
2376 if (TREE_CODE (sub) == ADDR_EXPR)
2377 {
2378 tree op = TREE_OPERAND (sub, 0);
2379 tree optype = TREE_TYPE (op);
2380
2381 /* *&CONST_DECL -> to the value of the const decl. */
2382 if (TREE_CODE (op) == CONST_DECL)
2383 return DECL_INITIAL (op);
2384 /* *&p => p; make sure to handle *&"str"[cst] here. */
fe71aa4e
JM
2385 if (same_type_ignoring_top_level_qualifiers_p (optype, type)
2386 /* Also handle the case where the desired type is an array of unknown
2387 bounds because the variable has had its bounds deduced since the
2388 ADDR_EXPR was created. */
2389 || (TREE_CODE (type) == ARRAY_TYPE
2390 && TREE_CODE (optype) == ARRAY_TYPE
2391 && TYPE_DOMAIN (type) == NULL_TREE
2392 && same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (optype),
2393 TREE_TYPE (type))))
2d76680f
PC
2394 {
2395 tree fop = fold_read_from_constant_string (op);
2396 if (fop)
2397 return fop;
2398 else
2399 return op;
2400 }
2401 /* *(foo *)&fooarray => fooarray[0] */
2402 else if (TREE_CODE (optype) == ARRAY_TYPE
2403 && (same_type_ignoring_top_level_qualifiers_p
2404 (type, TREE_TYPE (optype))))
2405 {
2406 tree type_domain = TYPE_DOMAIN (optype);
2407 tree min_val = size_zero_node;
2408 if (type_domain && TYPE_MIN_VALUE (type_domain))
2409 min_val = TYPE_MIN_VALUE (type_domain);
2410 return build4_loc (loc, ARRAY_REF, type, op, min_val,
2411 NULL_TREE, NULL_TREE);
2412 }
2413 /* *(foo *)&complexfoo => __real__ complexfoo */
2414 else if (TREE_CODE (optype) == COMPLEX_TYPE
2415 && (same_type_ignoring_top_level_qualifiers_p
2416 (type, TREE_TYPE (optype))))
2417 return fold_build1_loc (loc, REALPART_EXPR, type, op);
2418 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
b55b02ea 2419 else if (VECTOR_TYPE_P (optype)
2d76680f
PC
2420 && (same_type_ignoring_top_level_qualifiers_p
2421 (type, TREE_TYPE (optype))))
2422 {
2423 tree part_width = TYPE_SIZE (type);
2424 tree index = bitsize_int (0);
2425 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
2426 }
2427 /* Also handle conversion to an empty base class, which
2428 is represented with a NOP_EXPR. */
2429 else if (is_empty_class (type)
2430 && CLASS_TYPE_P (optype)
2431 && DERIVED_FROM_P (type, optype))
2432 {
2433 *empty_base = true;
2434 return op;
2435 }
2436 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
2437 else if (RECORD_OR_UNION_TYPE_P (optype))
2438 {
2439 tree field = TYPE_FIELDS (optype);
2440 for (; field; field = DECL_CHAIN (field))
2441 if (TREE_CODE (field) == FIELD_DECL
2442 && integer_zerop (byte_position (field))
2443 && (same_type_ignoring_top_level_qualifiers_p
2444 (TREE_TYPE (field), type)))
2445 {
2446 return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE);
2447 break;
2448 }
2449 }
2450 }
2451 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
2452 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
2453 {
2454 tree op00 = TREE_OPERAND (sub, 0);
2455 tree op01 = TREE_OPERAND (sub, 1);
2456
2457 STRIP_NOPS (op00);
2458 if (TREE_CODE (op00) == ADDR_EXPR)
2459 {
2460 tree op00type;
2461 op00 = TREE_OPERAND (op00, 0);
2462 op00type = TREE_TYPE (op00);
2463
2464 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
b55b02ea 2465 if (VECTOR_TYPE_P (op00type)
2d76680f
PC
2466 && (same_type_ignoring_top_level_qualifiers_p
2467 (type, TREE_TYPE (op00type))))
2468 {
2469 HOST_WIDE_INT offset = tree_to_shwi (op01);
2470 tree part_width = TYPE_SIZE (type);
2471 unsigned HOST_WIDE_INT part_widthi = tree_to_shwi (part_width)/BITS_PER_UNIT;
2472 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
2473 tree index = bitsize_int (indexi);
2474
2475 if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
2476 return fold_build3_loc (loc,
2477 BIT_FIELD_REF, type, op00,
2478 part_width, index);
2479
2480 }
2481 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
2482 else if (TREE_CODE (op00type) == COMPLEX_TYPE
2483 && (same_type_ignoring_top_level_qualifiers_p
2484 (type, TREE_TYPE (op00type))))
2485 {
2486 tree size = TYPE_SIZE_UNIT (type);
2487 if (tree_int_cst_equal (size, op01))
2488 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
2489 }
2490 /* ((foo *)&fooarray)[1] => fooarray[1] */
2491 else if (TREE_CODE (op00type) == ARRAY_TYPE
2492 && (same_type_ignoring_top_level_qualifiers_p
2493 (type, TREE_TYPE (op00type))))
2494 {
2495 tree type_domain = TYPE_DOMAIN (op00type);
2496 tree min_val = size_zero_node;
2497 if (type_domain && TYPE_MIN_VALUE (type_domain))
2498 min_val = TYPE_MIN_VALUE (type_domain);
2499 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
2500 TYPE_SIZE_UNIT (type));
2501 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
2502 return build4_loc (loc, ARRAY_REF, type, op00, op01,
2503 NULL_TREE, NULL_TREE);
2504 }
2505 /* Also handle conversion to an empty base class, which
2506 is represented with a NOP_EXPR. */
2507 else if (is_empty_class (type)
2508 && CLASS_TYPE_P (op00type)
2509 && DERIVED_FROM_P (type, op00type))
2510 {
2511 *empty_base = true;
2512 return op00;
2513 }
2514 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
2515 else if (RECORD_OR_UNION_TYPE_P (op00type))
2516 {
2517 tree field = TYPE_FIELDS (op00type);
2518 for (; field; field = DECL_CHAIN (field))
2519 if (TREE_CODE (field) == FIELD_DECL
2520 && tree_int_cst_equal (byte_position (field), op01)
2521 && (same_type_ignoring_top_level_qualifiers_p
2522 (TREE_TYPE (field), type)))
2523 {
2524 return fold_build3 (COMPONENT_REF, type, op00,
afd7acb2 2525 field, NULL_TREE);
2d76680f
PC
2526 break;
2527 }
2528 }
2529 }
2530 }
2531 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
2532 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
2533 && (same_type_ignoring_top_level_qualifiers_p
2534 (type, TREE_TYPE (TREE_TYPE (subtype)))))
2535 {
2536 tree type_domain;
2537 tree min_val = size_zero_node;
2538 tree newsub = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL);
2539 if (newsub)
2540 sub = newsub;
2541 else
2542 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
2543 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
2544 if (type_domain && TYPE_MIN_VALUE (type_domain))
2545 min_val = TYPE_MIN_VALUE (type_domain);
2546 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
2547 NULL_TREE);
2548 }
2549
2550 return NULL_TREE;
2551}
2552
2553static tree
3e605b20 2554cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
92a596e8 2555 bool lval,
2d76680f
PC
2556 bool *non_constant_p, bool *overflow_p)
2557{
2558 tree orig_op0 = TREE_OPERAND (t, 0);
2d76680f 2559 bool empty_base = false;
2d76680f 2560
cda0a029
JM
2561 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
2562 operand is an integer-zero. Otherwise reject the MEM_REF for now. */
2563
2564 if (TREE_CODE (t) == MEM_REF
2565 && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1))))
2566 {
2567 gcc_assert (ctx->quiet);
2568 *non_constant_p = true;
2569 return t;
2570 }
2571
255a48d6
JM
2572 /* First try to simplify it directly. */
2573 tree r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), orig_op0,
2574 &empty_base);
2575 if (!r)
2d76680f 2576 {
255a48d6
JM
2577 /* If that didn't work, evaluate the operand first. */
2578 tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
2579 /*lval*/false, non_constant_p,
2580 overflow_p);
2581 /* Don't VERIFY_CONSTANT here. */
2582 if (*non_constant_p)
2583 return t;
2584
2585 r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0,
2586 &empty_base);
2587 if (r == NULL_TREE)
2d76680f
PC
2588 {
2589 /* We couldn't fold to a constant value. Make sure it's not
2590 something we should have been able to fold. */
255a48d6
JM
2591 tree sub = op0;
2592 STRIP_NOPS (sub);
2593 if (TREE_CODE (sub) == ADDR_EXPR)
2594 {
2595 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
2596 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
2597 /* DR 1188 says we don't have to deal with this. */
2598 if (!ctx->quiet)
2599 error ("accessing value of %qE through a %qT glvalue in a "
2600 "constant expression", build_fold_indirect_ref (sub),
2601 TREE_TYPE (t));
2602 *non_constant_p = true;
2603 return t;
2604 }
2605
2606 if (lval && op0 != orig_op0)
2607 return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
2608 if (!lval)
2609 VERIFY_CONSTANT (t);
2d76680f
PC
2610 return t;
2611 }
2612 }
2613
255a48d6
JM
2614 r = cxx_eval_constant_expression (ctx, r,
2615 lval, non_constant_p, overflow_p);
2616 if (*non_constant_p)
2617 return t;
2618
2d76680f
PC
2619 /* If we're pulling out the value of an empty base, make sure
2620 that the whole object is constant and then return an empty
2621 CONSTRUCTOR. */
92a596e8 2622 if (empty_base && !lval)
2d76680f
PC
2623 {
2624 VERIFY_CONSTANT (r);
2625 r = build_constructor (TREE_TYPE (t), NULL);
2626 TREE_CONSTANT (r) = true;
2627 }
2628
2d76680f
PC
2629 return r;
2630}
2631
2632/* Complain about R, a VAR_DECL, not being usable in a constant expression.
2633 Shared between potential_constant_expression and
2634 cxx_eval_constant_expression. */
2635
2636static void
2637non_const_var_error (tree r)
2638{
2639 tree type = TREE_TYPE (r);
2640 error ("the value of %qD is not usable in a constant "
2641 "expression", r);
2642 /* Avoid error cascade. */
2643 if (DECL_INITIAL (r) == error_mark_node)
2644 return;
2645 if (DECL_DECLARED_CONSTEXPR_P (r))
2646 inform (DECL_SOURCE_LOCATION (r),
2647 "%qD used in its own initializer", r);
2648 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
2649 {
2650 if (!CP_TYPE_CONST_P (type))
2651 inform (DECL_SOURCE_LOCATION (r),
2652 "%q#D is not const", r);
2653 else if (CP_TYPE_VOLATILE_P (type))
2654 inform (DECL_SOURCE_LOCATION (r),
2655 "%q#D is volatile", r);
2656 else if (!DECL_INITIAL (r)
2657 || !TREE_CONSTANT (DECL_INITIAL (r)))
2658 inform (DECL_SOURCE_LOCATION (r),
2659 "%qD was not initialized with a constant "
2660 "expression", r);
2661 else
2662 gcc_unreachable ();
2663 }
2664 else
2665 {
2666 if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
2667 inform (DECL_SOURCE_LOCATION (r),
2668 "%qD was not declared %<constexpr%>", r);
2669 else
2670 inform (DECL_SOURCE_LOCATION (r),
2671 "%qD does not have integral or enumeration type",
2672 r);
2673 }
2674}
2675
2676/* Subroutine of cxx_eval_constant_expression.
2677 Like cxx_eval_unary_expression, except for trinary expressions. */
2678
2679static tree
3e605b20 2680cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
92a596e8 2681 bool lval,
2d76680f
PC
2682 bool *non_constant_p, bool *overflow_p)
2683{
2684 int i;
2685 tree args[3];
2686 tree val;
2687
2688 for (i = 0; i < 3; i++)
2689 {
3e605b20 2690 args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
92a596e8 2691 lval,
5a804683 2692 non_constant_p, overflow_p);
2d76680f
PC
2693 VERIFY_CONSTANT (args[i]);
2694 }
2695
2696 val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
2697 args[0], args[1], args[2]);
2698 if (val == NULL_TREE)
2699 return t;
2700 VERIFY_CONSTANT (val);
2701 return val;
2702}
2703
2704bool
2705var_in_constexpr_fn (tree t)
2706{
2707 tree ctx = DECL_CONTEXT (t);
2708 return (cxx_dialect >= cxx14 && ctx && TREE_CODE (ctx) == FUNCTION_DECL
2709 && DECL_DECLARED_CONSTEXPR_P (ctx));
2710}
2711
60813a46
JM
2712/* Evaluate an INIT_EXPR or MODIFY_EXPR. */
2713
2714static tree
2715cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
92a596e8 2716 bool lval,
60813a46
JM
2717 bool *non_constant_p, bool *overflow_p)
2718{
2719 constexpr_ctx new_ctx = *ctx;
2720
58cc255c
JM
2721 tree init = TREE_OPERAND (t, 1);
2722 if (TREE_CLOBBER_P (init))
2723 /* Just ignore clobbers. */
2724 return void_node;
2725
60813a46
JM
2726 /* First we figure out where we're storing to. */
2727 tree target = TREE_OPERAND (t, 0);
3f8e2835 2728 tree type = TREE_TYPE (target);
60813a46 2729 target = cxx_eval_constant_expression (ctx, target,
2b3ab879 2730 true,
5a804683 2731 non_constant_p, overflow_p);
60813a46
JM
2732 if (*non_constant_p)
2733 return t;
2734
3f8e2835
JM
2735 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (target), type))
2736 {
2737 /* For initialization of an empty base, the original target will be
2738 *(base*)this, which the above evaluation resolves to the object
2739 argument, which has the derived type rather than the base type. In
2740 this situation, just evaluate the initializer and return, since
2741 there's no actual data to store. */
2742 gcc_assert (is_empty_class (type));
2743 return cxx_eval_constant_expression (ctx, init, false,
2744 non_constant_p, overflow_p);
2745 }
2746
60813a46
JM
2747 /* And then find the underlying variable. */
2748 vec<tree,va_gc> *refs = make_tree_vector();
2749 tree object = NULL_TREE;
2750 for (tree probe = target; object == NULL_TREE; )
2751 {
2752 switch (TREE_CODE (probe))
2753 {
2754 case BIT_FIELD_REF:
2755 case COMPONENT_REF:
2756 case ARRAY_REF:
2757 vec_safe_push (refs, TREE_OPERAND (probe, 1));
2758 vec_safe_push (refs, TREE_TYPE (probe));
2759 probe = TREE_OPERAND (probe, 0);
2760 break;
2761
2762 default:
2763 object = probe;
60813a46
JM
2764 }
2765 }
2766
2767 /* And then find/build up our initializer for the path to the subobject
2768 we're initializing. */
a1408b7e
JM
2769 tree *valp;
2770 if (DECL_P (object))
2771 valp = ctx->values->get (object);
2772 else
2773 valp = NULL;
60813a46
JM
2774 if (!valp)
2775 {
2776 /* A constant-expression cannot modify objects from outside the
2777 constant-expression. */
2b3ab879 2778 if (!ctx->quiet)
a1408b7e 2779 error ("modification of %qE is not a constant-expression", object);
60813a46
JM
2780 *non_constant_p = true;
2781 return t;
2782 }
3f8e2835 2783 type = TREE_TYPE (object);
c75ce530 2784 bool no_zero_init = true;
60813a46
JM
2785 while (!refs->is_empty())
2786 {
2787 if (*valp == NULL_TREE)
2788 {
2789 *valp = build_constructor (type, NULL);
c75ce530 2790 CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp) = no_zero_init;
60813a46 2791 }
c75ce530
JM
2792 /* If the value of object is already zero-initialized, any new ctors for
2793 subobjects will also be zero-initialized. */
2794 no_zero_init = CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp);
60813a46 2795
ceaaf873 2796 enum tree_code code = TREE_CODE (type);
60813a46 2797 type = refs->pop();
ceaaf873 2798 tree index = refs->pop();
60813a46 2799
60813a46 2800 constructor_elt *cep = NULL;
ceaaf873
JM
2801 if (code == ARRAY_TYPE)
2802 {
2803 HOST_WIDE_INT i
2804 = find_array_ctor_elt (*valp, index, /*insert*/true);
2805 gcc_assert (i >= 0);
2806 cep = CONSTRUCTOR_ELT (*valp, i);
2807 gcc_assert (TREE_CODE (cep->index) != RANGE_EXPR);
2808 }
2809 else
2810 {
2811 gcc_assert (TREE_CODE (index) == FIELD_DECL);
2812 for (unsigned HOST_WIDE_INT idx = 0;
2813 vec_safe_iterate (CONSTRUCTOR_ELTS (*valp), idx, &cep);
2814 idx++)
2815 if (index == cep->index)
2816 break;
2817 if (!cep)
2818 {
2819 constructor_elt ce = { index, NULL_TREE };
2820 cep = vec_safe_push (CONSTRUCTOR_ELTS (*valp), ce);
2821 }
2822 }
60813a46
JM
2823 valp = &cep->value;
2824 }
2825 release_tree_vector (refs);
2826
acb2970c 2827 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
60813a46
JM
2828 {
2829 /* Create a new CONSTRUCTOR in case evaluation of the initializer
2830 wants to modify it. */
acb2970c 2831 if (*valp == NULL_TREE)
73a84269
JM
2832 {
2833 *valp = new_ctx.ctor = build_constructor (type, NULL);
2834 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = no_zero_init;
2835 }
2836 else
2837 new_ctx.ctor = *valp;
60813a46
JM
2838 new_ctx.object = target;
2839 }
2840
58cc255c
JM
2841 init = cxx_eval_constant_expression (&new_ctx, init, false,
2842 non_constant_p, overflow_p);
60813a46 2843 if (target == object)
acb2970c
JM
2844 {
2845 /* The hash table might have moved since the get earlier. */
2846 valp = ctx->values->get (object);
2847 if (TREE_CODE (init) == CONSTRUCTOR)
2848 /* An outer ctx->ctor might be pointing to *valp, so just replace
2849 its contents. */
2850 CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
2851 else
2852 *valp = init;
2853 }
60813a46
JM
2854 else
2855 *valp = init;
2856
2857 if (*non_constant_p)
2858 return t;
92a596e8 2859 else if (lval)
60813a46
JM
2860 return target;
2861 else
3bdf0a9b 2862 return init;
60813a46
JM
2863}
2864
2865/* Evaluate a ++ or -- expression. */
2866
2867static tree
2868cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
92a596e8 2869 bool lval,
60813a46
JM
2870 bool *non_constant_p, bool *overflow_p)
2871{
2872 enum tree_code code = TREE_CODE (t);
2873 tree type = TREE_TYPE (t);
2874 tree op = TREE_OPERAND (t, 0);
2875 tree offset = TREE_OPERAND (t, 1);
2876 gcc_assert (TREE_CONSTANT (offset));
2877
2878 /* The operand as an lvalue. */
2b3ab879 2879 op = cxx_eval_constant_expression (ctx, op, true,
5a804683 2880 non_constant_p, overflow_p);
60813a46
JM
2881
2882 /* The operand as an rvalue. */
2883 tree val = rvalue (op);
2b3ab879 2884 val = cxx_eval_constant_expression (ctx, val, false,
5a804683 2885 non_constant_p, overflow_p);
caee690e
JM
2886 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
2887 a local array in a constexpr function. */
2888 bool ptr = POINTER_TYPE_P (TREE_TYPE (val));
2889 if (!ptr)
2890 VERIFY_CONSTANT (val);
60813a46
JM
2891
2892 /* The modified value. */
2893 bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
ac2f8d26
JM
2894 tree mod;
2895 if (POINTER_TYPE_P (type))
2896 {
2897 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
2898 offset = convert_to_ptrofftype (offset);
2899 if (!inc)
2900 offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
2901 mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
2902 }
2903 else
2904 mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
caee690e
JM
2905 if (!ptr)
2906 VERIFY_CONSTANT (mod);
60813a46
JM
2907
2908 /* Storing the modified value. */
2909 tree store = build2 (MODIFY_EXPR, type, op, mod);
2b3ab879 2910 cxx_eval_constant_expression (ctx, store,
5a804683 2911 true, non_constant_p, overflow_p);
60813a46
JM
2912
2913 /* And the value of the expression. */
2914 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2915 {
2916 /* Prefix ops are lvalues. */
92a596e8 2917 if (lval)
60813a46
JM
2918 return op;
2919 else
2920 /* But we optimize when the caller wants an rvalue. */
2921 return mod;
2922 }
2923 else
2924 /* Postfix ops are rvalues. */
2925 return val;
2926}
2927
56632b27
JM
2928/* Predicates for the meaning of *jump_target. */
2929
2930static bool
2931returns (tree *jump_target)
2932{
2933 return *jump_target
2934 && TREE_CODE (*jump_target) == RETURN_EXPR;
2935}
2936
2937static bool
2938breaks (tree *jump_target)
2939{
2940 return *jump_target
2941 && TREE_CODE (*jump_target) == LABEL_DECL
2942 && LABEL_DECL_BREAK (*jump_target);
2943}
2944
2945static bool
2946continues (tree *jump_target)
2947{
2948 return *jump_target
2949 && TREE_CODE (*jump_target) == LABEL_DECL
2950 && LABEL_DECL_CONTINUE (*jump_target);
2951}
2952
2953static bool
2954switches (tree *jump_target)
2955{
2956 return *jump_target
2957 && TREE_CODE (*jump_target) == INTEGER_CST;
2958}
2959
2960/* Subroutine of cxx_eval_statement_list. Determine whether the statement
2961 at I matches *jump_target. If we're looking for a case label and we see
2962 the default label, copy I into DEFAULT_LABEL. */
2963
2964static bool
2965label_matches (tree *jump_target, tree_stmt_iterator i,
2966 tree_stmt_iterator& default_label)
2967{
2968 tree stmt = tsi_stmt (i);
2969 switch (TREE_CODE (*jump_target))
2970 {
2971 case LABEL_DECL:
2972 if (TREE_CODE (stmt) == LABEL_EXPR
2973 && LABEL_EXPR_LABEL (stmt) == *jump_target)
2974 return true;
2975 break;
2976
2977 case INTEGER_CST:
2978 if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
2979 {
2980 if (!CASE_LOW (stmt))
2981 default_label = i;
2982 else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
2983 return true;
2984 }
2985 break;
2986
2987 default:
2988 gcc_unreachable ();
2989 }
2990 return false;
2991}
2992
2993/* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
2994 semantics, for switch, break, continue, and return. */
2995
2996static tree
2997cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
56632b27
JM
2998 bool *non_constant_p, bool *overflow_p,
2999 tree *jump_target)
3000{
3001 tree_stmt_iterator i;
3002 tree_stmt_iterator default_label = tree_stmt_iterator();
58611fb6
JM
3003 tree local_target;
3004 /* In a statement-expression we want to return the last value. */
3005 tree r = NULL_TREE;
3006 if (!jump_target)
3007 {
3008 local_target = NULL_TREE;
3009 jump_target = &local_target;
3010 }
56632b27
JM
3011 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
3012 {
3013 reenter:
3014 tree stmt = tsi_stmt (i);
3015 if (*jump_target)
3016 {
3017 if (TREE_CODE (stmt) == STATEMENT_LIST)
3018 /* The label we want might be inside. */;
3019 else if (label_matches (jump_target, i, default_label))
3020 /* Found it. */
3021 *jump_target = NULL_TREE;
3022 else
3023 continue;
3024 }
58611fb6
JM
3025 r = cxx_eval_constant_expression (ctx, stmt, false,
3026 non_constant_p, overflow_p,
3027 jump_target);
56632b27
JM
3028 if (*non_constant_p)
3029 break;
3030 if (returns (jump_target) || breaks (jump_target))
3031 break;
3032 }
3033 if (switches (jump_target) && !tsi_end_p (default_label))
3034 {
3035 i = default_label;
3036 *jump_target = NULL_TREE;
3037 goto reenter;
3038 }
58611fb6 3039 return r;
56632b27
JM
3040}
3041
3042/* Evaluate a LOOP_EXPR for side-effects. Handles break and return
3043 semantics; continue semantics are covered by cxx_eval_statement_list. */
3044
3045static tree
3046cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
56632b27
JM
3047 bool *non_constant_p, bool *overflow_p,
3048 tree *jump_target)
3049{
3050 tree body = TREE_OPERAND (t, 0);
3051 while (true)
3052 {
2b3ab879 3053 cxx_eval_statement_list (ctx, body,
56632b27 3054 non_constant_p, overflow_p, jump_target);
5a5e54cd 3055 if (returns (jump_target) || breaks (jump_target) || *non_constant_p)
56632b27
JM
3056 break;
3057 }
3058 if (breaks (jump_target))
3059 *jump_target = NULL_TREE;
3060 return NULL_TREE;
3061}
3062
3063/* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
3064 semantics. */
3065
3066static tree
3067cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
56632b27
JM
3068 bool *non_constant_p, bool *overflow_p,
3069 tree *jump_target)
3070{
3071 tree cond = TREE_OPERAND (t, 0);
2b3ab879 3072 cond = cxx_eval_constant_expression (ctx, cond, false,
5a804683 3073 non_constant_p, overflow_p);
56632b27
JM
3074 VERIFY_CONSTANT (cond);
3075 *jump_target = cond;
3076
3077 tree body = TREE_OPERAND (t, 1);
2b3ab879 3078 cxx_eval_statement_list (ctx, body,
56632b27
JM
3079 non_constant_p, overflow_p, jump_target);
3080 if (breaks (jump_target) || switches (jump_target))
3081 *jump_target = NULL_TREE;
3082 return NULL_TREE;
3083}
3084
ef4bac78
MP
3085/* Subroutine of cxx_eval_constant_expression.
3086 Attempt to reduce a POINTER_PLUS_EXPR expression T. */
3087
3088static tree
3089cxx_eval_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
3090 bool lval, bool *non_constant_p,
3091 bool *overflow_p)
3092{
6338536c 3093 tree orig_type = TREE_TYPE (t);
ef4bac78
MP
3094 tree op00 = TREE_OPERAND (t, 0);
3095 tree op01 = TREE_OPERAND (t, 1);
3096 location_t loc = EXPR_LOCATION (t);
3097
894bec68
MP
3098 op00 = cxx_eval_constant_expression (ctx, op00, lval,
3099 non_constant_p, overflow_p);
3100
ef4bac78
MP
3101 STRIP_NOPS (op00);
3102 if (TREE_CODE (op00) != ADDR_EXPR)
3103 return NULL_TREE;
3104
cda0a029
JM
3105 op01 = cxx_eval_constant_expression (ctx, op01, lval,
3106 non_constant_p, overflow_p);
ef4bac78
MP
3107 op00 = TREE_OPERAND (op00, 0);
3108
3109 /* &A[i] p+ j => &A[i + j] */
3110 if (TREE_CODE (op00) == ARRAY_REF
3111 && TREE_CODE (TREE_OPERAND (op00, 1)) == INTEGER_CST
6338536c
JJ
3112 && TREE_CODE (op01) == INTEGER_CST
3113 && TYPE_SIZE_UNIT (TREE_TYPE (op00))
3114 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (op00))) == INTEGER_CST)
ef4bac78
MP
3115 {
3116 tree type = TREE_TYPE (op00);
3117 t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (op00, 1));
3118 tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (op00, 0)));
3119 /* Don't fold an out-of-bound access. */
3120 if (!tree_int_cst_le (t, nelts))
3121 return NULL_TREE;
6338536c
JJ
3122 op01 = cp_fold_convert (ssizetype, op01);
3123 /* Don't fold if op01 can't be divided exactly by TYPE_SIZE_UNIT.
3124 constexpr int A[1]; ... (char *)&A[0] + 1 */
3125 if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
3126 op01, TYPE_SIZE_UNIT (type))))
3127 return NULL_TREE;
ef4bac78
MP
3128 /* Make sure to treat the second operand of POINTER_PLUS_EXPR
3129 as signed. */
6338536c 3130 op01 = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, op01,
ef4bac78
MP
3131 TYPE_SIZE_UNIT (type));
3132 t = size_binop_loc (loc, PLUS_EXPR, op01, t);
3133 t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (op00, 0),
3134 t, NULL_TREE, NULL_TREE);
3135 t = cp_build_addr_expr (t, tf_warning_or_error);
6338536c 3136 t = cp_fold_convert (orig_type, t);
ef4bac78
MP
3137 return cxx_eval_constant_expression (ctx, t, lval, non_constant_p,
3138 overflow_p);
3139 }
3140
3141 return NULL_TREE;
3142}
3143
2d76680f
PC
3144/* Attempt to reduce the expression T to a constant value.
3145 On failure, issue diagnostic and return error_mark_node. */
3146/* FIXME unify with c_fully_fold */
60813a46 3147/* FIXME overflow_p is too global */
2d76680f
PC
3148
3149static tree
3e605b20 3150cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
92a596e8 3151 bool lval,
56632b27
JM
3152 bool *non_constant_p, bool *overflow_p,
3153 tree *jump_target)
2d76680f 3154{
3e605b20 3155 constexpr_ctx new_ctx;
2d76680f
PC
3156 tree r = t;
3157
3158 if (t == error_mark_node)
3159 {
3160 *non_constant_p = true;
3161 return t;
3162 }
3163 if (CONSTANT_CLASS_P (t))
3164 {
dcdbc004 3165 if (TREE_OVERFLOW (t) && (!flag_permissive || ctx->quiet))
2d76680f
PC
3166 *overflow_p = true;
3167 return t;
3168 }
2d76680f
PC
3169
3170 switch (TREE_CODE (t))
3171 {
60813a46 3172 case RESULT_DECL:
92a596e8 3173 if (lval)
60813a46
JM
3174 return t;
3175 /* We ask for an rvalue for the RESULT_DECL when indirecting
bf66b9b4
AH
3176 through an invisible reference, or in named return value
3177 optimization. */
60813a46
JM
3178 return (*ctx->values->get (t));
3179
2d76680f 3180 case VAR_DECL:
7c83622c
JM
3181 case CONST_DECL:
3182 /* We used to not check lval for CONST_DECL, but darwin.c uses
3183 CONST_DECL for aggregate constants. */
92a596e8 3184 if (lval)
2d76680f 3185 return t;
69eb4fde
JM
3186 if (ctx->strict)
3187 r = decl_really_constant_value (t);
3188 else
3189 r = decl_constant_value (t);
2d76680f
PC
3190 if (TREE_CODE (r) == TARGET_EXPR
3191 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
3192 r = TARGET_EXPR_INITIAL (r);
56a6f1d3 3193 if (VAR_P (r))
3e605b20 3194 if (tree *p = ctx->values->get (r))
bd8f5bb2
MP
3195 if (*p != NULL_TREE)
3196 r = *p;
2d76680f
PC
3197 if (DECL_P (r))
3198 {
2b3ab879 3199 if (!ctx->quiet)
2d76680f
PC
3200 non_const_var_error (r);
3201 *non_constant_p = true;
3202 }
3203 break;
3204
3205 case FUNCTION_DECL:
3206 case TEMPLATE_DECL:
3207 case LABEL_DECL:
56632b27
JM
3208 case LABEL_EXPR:
3209 case CASE_LABEL_EXPR:
2d76680f
PC
3210 return t;
3211
3212 case PARM_DECL:
0567dcd2 3213 if (lval && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
60813a46
JM
3214 /* glvalue use. */;
3215 else if (tree *p = ctx->values->get (r))
3216 r = *p;
92a596e8 3217 else if (lval)
2d76680f 3218 /* Defer in case this is only used for its type. */;
f2acb8ad
JM
3219 else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
3220 /* Defer, there's no lvalue->rvalue conversion. */;
052beba4
JM
3221 else if (is_empty_class (TREE_TYPE (t)))
3222 {
3223 /* If the class is empty, we aren't actually loading anything. */
3224 r = build_constructor (TREE_TYPE (t), NULL);
3225 TREE_CONSTANT (r) = true;
3226 }
2d76680f
PC
3227 else
3228 {
2b3ab879 3229 if (!ctx->quiet)
2d76680f
PC
3230 error ("%qE is not a constant expression", t);
3231 *non_constant_p = true;
3232 }
3233 break;
3234
3235 case CALL_EXPR:
3236 case AGGR_INIT_EXPR:
92a596e8 3237 r = cxx_eval_call_expression (ctx, t, lval,
2d76680f
PC
3238 non_constant_p, overflow_p);
3239 break;
3240
60813a46
JM
3241 case DECL_EXPR:
3242 {
3243 r = DECL_EXPR_DECL (t);
3244 if (AGGREGATE_TYPE_P (TREE_TYPE (r))
3245 || VECTOR_TYPE_P (TREE_TYPE (r)))
3246 {
3247 new_ctx = *ctx;
3248 new_ctx.object = r;
3249 new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
3250 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
3251 new_ctx.values->put (r, new_ctx.ctor);
3252 ctx = &new_ctx;
3253 }
3254
3255 if (tree init = DECL_INITIAL (r))
3256 {
3257 init = cxx_eval_constant_expression (ctx, init,
2b3ab879 3258 false,
5a804683 3259 non_constant_p, overflow_p);
60813a46
JM
3260 ctx->values->put (r, init);
3261 }
3262 else if (ctx == &new_ctx)
3263 /* We gave it a CONSTRUCTOR above. */;
3264 else
3265 ctx->values->put (r, NULL_TREE);
3266 }
3267 break;
3268
2d76680f
PC
3269 case TARGET_EXPR:
3270 if (!literal_type_p (TREE_TYPE (t)))
3271 {
2b3ab879 3272 if (!ctx->quiet)
2d76680f
PC
3273 {
3274 error ("temporary of non-literal type %qT in a "
3275 "constant expression", TREE_TYPE (t));
3276 explain_non_literal_class (TREE_TYPE (t));
3277 }
3278 *non_constant_p = true;
3279 break;
3280 }
3e605b20
JM
3281 if ((AGGREGATE_TYPE_P (TREE_TYPE (t)) || VECTOR_TYPE_P (TREE_TYPE (t))))
3282 {
3283 /* We're being expanded without an explicit target, so start
3284 initializing a new object; expansion with an explicit target
3285 strips the TARGET_EXPR before we get here. */
3286 new_ctx = *ctx;
3287 new_ctx.ctor = build_constructor (TREE_TYPE (t), NULL);
3288 CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
3289 new_ctx.object = TARGET_EXPR_SLOT (t);
3290 ctx->values->put (new_ctx.object, new_ctx.ctor);
3291 ctx = &new_ctx;
3292 }
92a596e8 3293 /* Pass false for 'lval' because this indicates
2d76680f 3294 initialization of a temporary. */
3e605b20 3295 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
2b3ab879 3296 false,
5a804683 3297 non_constant_p, overflow_p);
2d76680f
PC
3298 if (!*non_constant_p)
3299 /* Adjust the type of the result to the type of the temporary. */
3300 r = adjust_temp_type (TREE_TYPE (t), r);
92a596e8 3301 if (lval)
f2acb8ad
JM
3302 {
3303 tree slot = TARGET_EXPR_SLOT (t);
3304 ctx->values->put (slot, r);
3305 return slot;
3306 }
2d76680f
PC
3307 break;
3308
60813a46 3309 case INIT_EXPR:
60813a46 3310 case MODIFY_EXPR:
92a596e8 3311 r = cxx_eval_store_expression (ctx, t, lval,
60813a46
JM
3312 non_constant_p, overflow_p);
3313 break;
3314
2d76680f 3315 case SCOPE_REF:
3e605b20 3316 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
92a596e8 3317 lval,
5a804683 3318 non_constant_p, overflow_p);
2d76680f
PC
3319 break;
3320
3321 case RETURN_EXPR:
75e0295b
MP
3322 if (TREE_OPERAND (t, 0) != NULL_TREE)
3323 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3324 lval,
3325 non_constant_p, overflow_p);
56632b27
JM
3326 *jump_target = t;
3327 break;
3328
cabaf50a
JM
3329 case SAVE_EXPR:
3330 /* Avoid evaluating a SAVE_EXPR more than once. */
3331 if (tree *p = ctx->values->get (t))
3332 r = *p;
3333 else
3334 {
12d9ce19 3335 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false,
cabaf50a
JM
3336 non_constant_p, overflow_p);
3337 ctx->values->put (t, r);
3338 }
3339 break;
3340
2d76680f
PC
3341 case NON_LVALUE_EXPR:
3342 case TRY_CATCH_EXPR:
769430b2 3343 case TRY_BLOCK:
2d76680f
PC
3344 case CLEANUP_POINT_EXPR:
3345 case MUST_NOT_THROW_EXPR:
60813a46
JM
3346 case EXPR_STMT:
3347 case EH_SPEC_BLOCK:
3e605b20 3348 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
92a596e8 3349 lval,
56632b27
JM
3350 non_constant_p, overflow_p,
3351 jump_target);
2d76680f
PC
3352 break;
3353
769430b2
JM
3354 case TRY_FINALLY_EXPR:
3355 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
3356 non_constant_p, overflow_p,
3357 jump_target);
3358 if (!*non_constant_p)
3359 /* Also evaluate the cleanup. */
3360 cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), true,
3361 non_constant_p, overflow_p,
3362 jump_target);
3363 break;
3364
2d76680f
PC
3365 /* These differ from cxx_eval_unary_expression in that this doesn't
3366 check for a constant operand or result; an address can be
3367 constant without its operand being, and vice versa. */
cda0a029 3368 case MEM_REF:
2d76680f 3369 case INDIRECT_REF:
92a596e8 3370 r = cxx_eval_indirect_ref (ctx, t, lval,
2d76680f
PC
3371 non_constant_p, overflow_p);
3372 break;
3373
3374 case ADDR_EXPR:
3375 {
3376 tree oldop = TREE_OPERAND (t, 0);
3e605b20 3377 tree op = cxx_eval_constant_expression (ctx, oldop,
92a596e8 3378 /*lval*/true,
5a804683 3379 non_constant_p, overflow_p);
2d76680f
PC
3380 /* Don't VERIFY_CONSTANT here. */
3381 if (*non_constant_p)
3382 return t;
f2acb8ad 3383 gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
2d76680f
PC
3384 /* This function does more aggressive folding than fold itself. */
3385 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
3386 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
3387 return t;
3388 break;
3389 }
3390
3391 case REALPART_EXPR:
3392 case IMAGPART_EXPR:
3393 case CONJ_EXPR:
3394 case FIX_TRUNC_EXPR:
3395 case FLOAT_EXPR:
3396 case NEGATE_EXPR:
3397 case ABS_EXPR:
3398 case BIT_NOT_EXPR:
3399 case TRUTH_NOT_EXPR:
3400 case FIXED_CONVERT_EXPR:
92a596e8 3401 r = cxx_eval_unary_expression (ctx, t, lval,
2d76680f
PC
3402 non_constant_p, overflow_p);
3403 break;
3404
3405 case SIZEOF_EXPR:
cda0a029 3406 r = fold_sizeof_expr (t);
2d76680f
PC
3407 VERIFY_CONSTANT (r);
3408 break;
3409
3410 case COMPOUND_EXPR:
3411 {
3412 /* check_return_expr sometimes wraps a TARGET_EXPR in a
3413 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
3414 introduced by build_call_a. */
3415 tree op0 = TREE_OPERAND (t, 0);
3416 tree op1 = TREE_OPERAND (t, 1);
3417 STRIP_NOPS (op1);
3418 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
3419 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
2b3ab879 3420 r = cxx_eval_constant_expression (ctx, op0,
92a596e8 3421 lval, non_constant_p, overflow_p,
56632b27 3422 jump_target);
2d76680f
PC
3423 else
3424 {
3425 /* Check that the LHS is constant and then discard it. */
2b3ab879 3426 cxx_eval_constant_expression (ctx, op0,
12d9ce19 3427 true, non_constant_p, overflow_p,
56632b27 3428 jump_target);
2d76680f 3429 op1 = TREE_OPERAND (t, 1);
2b3ab879 3430 r = cxx_eval_constant_expression (ctx, op1,
92a596e8 3431 lval, non_constant_p, overflow_p,
56632b27 3432 jump_target);
2d76680f
PC
3433 }
3434 }
3435 break;
3436
3437 case POINTER_PLUS_EXPR:
ef4bac78
MP
3438 r = cxx_eval_pointer_plus_expression (ctx, t, lval, non_constant_p,
3439 overflow_p);
3440 if (r)
3441 break;
3442 /* else fall through */
3443
2d76680f
PC
3444 case PLUS_EXPR:
3445 case MINUS_EXPR:
3446 case MULT_EXPR:
3447 case TRUNC_DIV_EXPR:
3448 case CEIL_DIV_EXPR:
3449 case FLOOR_DIV_EXPR:
3450 case ROUND_DIV_EXPR:
3451 case TRUNC_MOD_EXPR:
3452 case CEIL_MOD_EXPR:
3453 case ROUND_MOD_EXPR:
3454 case RDIV_EXPR:
3455 case EXACT_DIV_EXPR:
3456 case MIN_EXPR:
3457 case MAX_EXPR:
3458 case LSHIFT_EXPR:
3459 case RSHIFT_EXPR:
3460 case LROTATE_EXPR:
3461 case RROTATE_EXPR:
3462 case BIT_IOR_EXPR:
3463 case BIT_XOR_EXPR:
3464 case BIT_AND_EXPR:
3465 case TRUTH_XOR_EXPR:
3466 case LT_EXPR:
3467 case LE_EXPR:
3468 case GT_EXPR:
3469 case GE_EXPR:
3470 case EQ_EXPR:
3471 case NE_EXPR:
3472 case UNORDERED_EXPR:
3473 case ORDERED_EXPR:
3474 case UNLT_EXPR:
3475 case UNLE_EXPR:
3476 case UNGT_EXPR:
3477 case UNGE_EXPR:
3478 case UNEQ_EXPR:
3479 case LTGT_EXPR:
3480 case RANGE_EXPR:
3481 case COMPLEX_EXPR:
92a596e8 3482 r = cxx_eval_binary_expression (ctx, t, lval,
2d76680f
PC
3483 non_constant_p, overflow_p);
3484 break;
3485
3486 /* fold can introduce non-IF versions of these; still treat them as
3487 short-circuiting. */
3488 case TRUTH_AND_EXPR:
3489 case TRUTH_ANDIF_EXPR:
3e605b20 3490 r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
2d76680f 3491 boolean_true_node,
92a596e8 3492 lval,
2d76680f
PC
3493 non_constant_p, overflow_p);
3494 break;
3495
3496 case TRUTH_OR_EXPR:
3497 case TRUTH_ORIF_EXPR:
3e605b20 3498 r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
2d76680f 3499 boolean_false_node,
92a596e8 3500 lval,
2d76680f
PC
3501 non_constant_p, overflow_p);
3502 break;
3503
3504 case ARRAY_REF:
92a596e8 3505 r = cxx_eval_array_reference (ctx, t, lval,
2d76680f
PC
3506 non_constant_p, overflow_p);
3507 break;
3508
3509 case COMPONENT_REF:
3510 if (is_overloaded_fn (t))
3511 {
3512 /* We can only get here in checking mode via
3513 build_non_dependent_expr, because any expression that
3514 calls or takes the address of the function will have
3515 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
2b3ab879 3516 gcc_checking_assert (ctx->quiet || errorcount);
2d76680f
PC
3517 *non_constant_p = true;
3518 return t;
3519 }
92a596e8 3520 r = cxx_eval_component_reference (ctx, t, lval,
2d76680f
PC
3521 non_constant_p, overflow_p);
3522 break;
3523
3524 case BIT_FIELD_REF:
92a596e8 3525 r = cxx_eval_bit_field_ref (ctx, t, lval,
2d76680f
PC
3526 non_constant_p, overflow_p);
3527 break;
3528
3529 case COND_EXPR:
3530 case VEC_COND_EXPR:
92a596e8 3531 r = cxx_eval_conditional_expression (ctx, t, lval,
56632b27
JM
3532 non_constant_p, overflow_p,
3533 jump_target);
2d76680f
PC
3534 break;
3535
3536 case CONSTRUCTOR:
b85a3242
JM
3537 if (TREE_CONSTANT (t))
3538 /* Don't re-process a constant CONSTRUCTOR, but do fold it to
3539 VECTOR_CST if applicable. */
3540 return fold (t);
92a596e8 3541 r = cxx_eval_bare_aggregate (ctx, t, lval,
2d76680f
PC
3542 non_constant_p, overflow_p);
3543 break;
3544
3545 case VEC_INIT_EXPR:
3546 /* We can get this in a defaulted constructor for a class with a
3547 non-static data member of array type. Either the initializer will
3548 be NULL, meaning default-initialization, or it will be an lvalue
3549 or xvalue of the same type, meaning direct-initialization from the
3550 corresponding member. */
92a596e8 3551 r = cxx_eval_vec_init (ctx, t, lval,
2d76680f
PC
3552 non_constant_p, overflow_p);
3553 break;
3554
3555 case FMA_EXPR:
3556 case VEC_PERM_EXPR:
92a596e8 3557 r = cxx_eval_trinary_expression (ctx, t, lval,
2d76680f
PC
3558 non_constant_p, overflow_p);
3559 break;
3560
3561 case CONVERT_EXPR:
3562 case VIEW_CONVERT_EXPR:
3563 case NOP_EXPR:
cda0a029 3564 case UNARY_PLUS_EXPR:
2d76680f 3565 {
cda0a029 3566 enum tree_code tcode = TREE_CODE (t);
2d76680f 3567 tree oldop = TREE_OPERAND (t, 0);
cda0a029 3568
3e605b20 3569 tree op = cxx_eval_constant_expression (ctx, oldop,
92a596e8 3570 lval,
5a804683 3571 non_constant_p, overflow_p);
2d76680f
PC
3572 if (*non_constant_p)
3573 return t;
dcdbc004
JM
3574 tree type = TREE_TYPE (t);
3575 if (TREE_CODE (op) == PTRMEM_CST
3576 && !TYPE_PTRMEM_P (type))
3577 op = cplus_expand_constant (op);
3578 if (POINTER_TYPE_P (type)
2d76680f
PC
3579 && TREE_CODE (op) == INTEGER_CST
3580 && !integer_zerop (op))
3581 {
2b3ab879 3582 if (!ctx->quiet)
2d76680f
PC
3583 error_at (EXPR_LOC_OR_LOC (t, input_location),
3584 "reinterpret_cast from integer to pointer");
3585 *non_constant_p = true;
3586 return t;
3587 }
cda0a029 3588 if (op == oldop && tcode != UNARY_PLUS_EXPR)
2d76680f
PC
3589 /* We didn't fold at the top so we could check for ptr-int
3590 conversion. */
3591 return fold (t);
cda0a029
JM
3592 if (tcode == UNARY_PLUS_EXPR)
3593 r = fold_convert (TREE_TYPE (t), op);
3594 else
3595 r = fold_build1 (tcode, type, op);
2d76680f
PC
3596 /* Conversion of an out-of-range value has implementation-defined
3597 behavior; the language considers it different from arithmetic
3598 overflow, which is undefined. */
3599 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
3600 TREE_OVERFLOW (r) = false;
3601 }
3602 break;
3603
3604 case EMPTY_CLASS_EXPR:
3605 /* This is good enough for a function argument that might not get
3606 used, and they can't do anything with it, so just return it. */
3607 return t;
3608
60813a46 3609 case STATEMENT_LIST:
56632b27
JM
3610 new_ctx = *ctx;
3611 new_ctx.ctor = new_ctx.object = NULL_TREE;
2b3ab879 3612 return cxx_eval_statement_list (&new_ctx, t,
56632b27 3613 non_constant_p, overflow_p, jump_target);
60813a46
JM
3614
3615 case BIND_EXPR:
3616 return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
92a596e8 3617 lval,
56632b27
JM
3618 non_constant_p, overflow_p,
3619 jump_target);
60813a46 3620
2d76680f
PC
3621 case PREINCREMENT_EXPR:
3622 case POSTINCREMENT_EXPR:
3623 case PREDECREMENT_EXPR:
3624 case POSTDECREMENT_EXPR:
2b3ab879 3625 return cxx_eval_increment_expression (ctx, t,
92a596e8 3626 lval, non_constant_p, overflow_p);
60813a46
JM
3627
3628 case LAMBDA_EXPR:
2d76680f
PC
3629 case NEW_EXPR:
3630 case VEC_NEW_EXPR:
3631 case DELETE_EXPR:
3632 case VEC_DELETE_EXPR:
3633 case THROW_EXPR:
2d76680f
PC
3634 case MODOP_EXPR:
3635 /* GCC internal stuff. */
3636 case VA_ARG_EXPR:
3637 case OBJ_TYPE_REF:
3638 case WITH_CLEANUP_EXPR:
2d76680f
PC
3639 case NON_DEPENDENT_EXPR:
3640 case BASELINK:
2d76680f 3641 case OFFSET_REF:
2b3ab879 3642 if (!ctx->quiet)
2d76680f
PC
3643 error_at (EXPR_LOC_OR_LOC (t, input_location),
3644 "expression %qE is not a constant-expression", t);
3645 *non_constant_p = true;
3646 break;
3647
3e605b20 3648 case PLACEHOLDER_EXPR:
6ad6af49
JM
3649 if (!ctx || !ctx->ctor || (lval && !ctx->object)
3650 || !(same_type_ignoring_top_level_qualifiers_p
3651 (TREE_TYPE (t), TREE_TYPE (ctx->ctor))))
3e605b20
JM
3652 {
3653 /* A placeholder without a referent. We can get here when
3654 checking whether NSDMIs are noexcept, or in massage_init_elt;
3655 just say it's non-constant for now. */
2b3ab879 3656 gcc_assert (ctx->quiet);
3e605b20
JM
3657 *non_constant_p = true;
3658 break;
3659 }
3660 else
3661 {
3662 /* Use of the value or address of the current object. We could
3663 use ctx->object unconditionally, but using ctx->ctor when we
3664 can is a minor optimization. */
92a596e8 3665 tree ctor = lval ? ctx->object : ctx->ctor;
3e605b20 3666 return cxx_eval_constant_expression
92a596e8 3667 (ctx, ctor, lval,
5a804683 3668 non_constant_p, overflow_p);
3e605b20
JM
3669 }
3670 break;
3671
60813a46 3672 case GOTO_EXPR:
56632b27
JM
3673 *jump_target = TREE_OPERAND (t, 0);
3674 gcc_assert (breaks (jump_target) || continues (jump_target));
3675 break;
3676
60813a46 3677 case LOOP_EXPR:
2b3ab879 3678 cxx_eval_loop_expr (ctx, t,
56632b27
JM
3679 non_constant_p, overflow_p, jump_target);
3680 break;
3681
60813a46 3682 case SWITCH_EXPR:
2b3ab879 3683 cxx_eval_switch_expr (ctx, t,
56632b27 3684 non_constant_p, overflow_p, jump_target);
60813a46
JM
3685 break;
3686
971e17ff
AS
3687 case REQUIRES_EXPR:
3688 /* It's possible to get a requires-expression in a constant
3689 expression. For example:
3690
3691 template<typename T> concept bool C() {
3692 return requires (T t) { t; };
3693 }
3694
3695 template<typename T> requires !C<T>() void f(T);
3696
3697 Normalization leaves f with the associated constraint
3698 '!requires (T t) { ... }' which is not transformed into
3699 a constraint. */
3700 if (!processing_template_decl)
3701 return evaluate_constraint_expression (t, NULL_TREE);
3702 else
3703 *non_constant_p = true;
3704 return t;
3705
2d76680f 3706 default:
c6e7c499
JM
3707 if (STATEMENT_CODE_P (TREE_CODE (t)))
3708 {
3709 /* This function doesn't know how to deal with pre-genericize
3710 statements; this can only happen with statement-expressions,
3711 so for now just fail. */
3712 if (!ctx->quiet)
3713 error_at (EXPR_LOCATION (t),
3714 "statement is not a constant-expression");
3715 }
3716 else
3717 internal_error ("unexpected expression %qE of kind %s", t,
3718 get_tree_code_name (TREE_CODE (t)));
2d76680f
PC
3719 *non_constant_p = true;
3720 break;
3721 }
3722
3723 if (r == error_mark_node)
3724 *non_constant_p = true;
3725
3726 if (*non_constant_p)
3727 return t;
3728 else
3729 return r;
3730}
3731
3732static tree
3e605b20 3733cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
69eb4fde 3734 bool strict = true, tree object = NULL_TREE)
2d76680f
PC
3735{
3736 bool non_constant_p = false;
3737 bool overflow_p = false;
3e605b20 3738 hash_map<tree,tree> map;
13f649f6 3739 constexpr_ctx ctx = { NULL, &map, NULL, NULL, allow_non_constant, strict };
3e605b20 3740 tree type = initialized_type (t);
3e605b20
JM
3741 tree r = t;
3742 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
3743 {
3744 /* In C++14 an NSDMI can participate in aggregate initialization,
3745 and can refer to the address of the object being initialized, so
3746 we need to pass in the relevant VAR_DECL if we want to do the
3747 evaluation in a single pass. The evaluation will dynamically
3748 update ctx.values for the VAR_DECL. We use the same strategy
3749 for C++11 constexpr constructors that refer to the object being
3750 initialized. */
3751 ctx.ctor = build_constructor (type, NULL);
3752 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctx.ctor) = true;
60813a46
JM
3753 if (!object)
3754 {
3755 if (TREE_CODE (t) == TARGET_EXPR)
3756 object = TARGET_EXPR_SLOT (t);
3757 else if (TREE_CODE (t) == AGGR_INIT_EXPR)
3758 object = AGGR_INIT_EXPR_SLOT (t);
3759 }
3e605b20
JM
3760 ctx.object = object;
3761 if (object)
3762 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3763 (type, TREE_TYPE (object)));
3764 if (object && DECL_P (object))
3765 map.put (object, ctx.ctor);
3766 if (TREE_CODE (r) == TARGET_EXPR)
3767 /* Avoid creating another CONSTRUCTOR when we expand the
3768 TARGET_EXPR. */
3769 r = TARGET_EXPR_INITIAL (r);
3770 }
3771
2b3ab879 3772 r = cxx_eval_constant_expression (&ctx, r,
5a804683 3773 false, &non_constant_p, &overflow_p);
2d76680f
PC
3774
3775 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
3776
023d89c7
JM
3777 /* Mutable logic is a bit tricky: we want to allow initialization of
3778 constexpr variables with mutable members, but we can't copy those
3779 members to another constexpr variable. */
3780 if (TREE_CODE (r) == CONSTRUCTOR
3781 && CONSTRUCTOR_MUTABLE_POISON (r))
2d76680f 3782 {
2d76680f 3783 if (!allow_non_constant)
023d89c7
JM
3784 error ("%qE is not a constant expression because it refers to "
3785 "mutable subobjects of %qT", t, type);
2d76680f
PC
3786 non_constant_p = true;
3787 }
3788
3789 /* Technically we should check this for all subexpressions, but that
3790 runs into problems with our internal representation of pointer
3791 subtraction and the 5.19 rules are still in flux. */
3792 if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
3793 && ARITHMETIC_TYPE_P (TREE_TYPE (r))
3794 && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
3795 {
3796 if (!allow_non_constant)
3797 error ("conversion from pointer type %qT "
3798 "to arithmetic type %qT in a constant-expression",
3799 TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
3800 non_constant_p = true;
3801 }
3802
3803 if (!non_constant_p && overflow_p)
3804 non_constant_p = true;
3805
3806 if (non_constant_p && !allow_non_constant)
3807 return error_mark_node;
3808 else if (non_constant_p && TREE_CONSTANT (r))
3809 {
3810 /* This isn't actually constant, so unset TREE_CONSTANT. */
3811 if (EXPR_P (r))
3812 r = copy_node (r);
3813 else if (TREE_CODE (r) == CONSTRUCTOR)
3814 r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
3815 else
3816 r = build_nop (TREE_TYPE (r), r);
3817 TREE_CONSTANT (r) = false;
3818 }
3819 else if (non_constant_p || r == t)
3820 return t;
3821
3822 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
3823 {
3824 if (TREE_CODE (t) == TARGET_EXPR
3825 && TARGET_EXPR_INITIAL (t) == r)
3826 return t;
3827 else
3828 {
3829 r = get_target_expr (r);
3830 TREE_CONSTANT (r) = true;
3831 return r;
3832 }
3833 }
3834 else
3835 return r;
3836}
3837
3838/* Returns true if T is a valid subexpression of a constant expression,
3839 even if it isn't itself a constant expression. */
3840
3841bool
3842is_sub_constant_expr (tree t)
3843{
3844 bool non_constant_p = false;
3845 bool overflow_p = false;
3e605b20 3846 hash_map <tree, tree> map;
13f649f6 3847 constexpr_ctx ctx = { NULL, &map, NULL, NULL, true, true };
2b3ab879 3848 cxx_eval_constant_expression (&ctx, t, false, &non_constant_p,
5a804683 3849 &overflow_p);
2d76680f
PC
3850 return !non_constant_p && !overflow_p;
3851}
3852
3853/* If T represents a constant expression returns its reduced value.
3854 Otherwise return error_mark_node. If T is dependent, then
3855 return NULL. */
3856
3857tree
3e605b20 3858cxx_constant_value (tree t, tree decl)
2d76680f 3859{
69eb4fde 3860 return cxx_eval_outermost_constant_expr (t, false, true, decl);
2d76680f
PC
3861}
3862
cda0a029
JM
3863/* Helper routine for fold_simple function. Either return simplified
3864 expression T, otherwise NULL_TREE.
3865 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
3866 even if we are within template-declaration. So be careful on call, as in
3867 such case types can be undefined. */
3868
3869static tree
3870fold_simple_1 (tree t)
3871{
3872 tree op1;
3873 enum tree_code code = TREE_CODE (t);
3874
3875 switch (code)
3876 {
3877 case INTEGER_CST:
3878 case REAL_CST:
3879 case VECTOR_CST:
3880 case FIXED_CST:
3881 case COMPLEX_CST:
3882 return t;
3883
3884 case SIZEOF_EXPR:
3885 return fold_sizeof_expr (t);
3886
3887 case ABS_EXPR:
3888 case CONJ_EXPR:
3889 case REALPART_EXPR:
3890 case IMAGPART_EXPR:
3891 case NEGATE_EXPR:
3892 case BIT_NOT_EXPR:
3893 case TRUTH_NOT_EXPR:
3894 case NOP_EXPR:
3895 case VIEW_CONVERT_EXPR:
3896 case CONVERT_EXPR:
3897 case FLOAT_EXPR:
3898 case FIX_TRUNC_EXPR:
3899 case FIXED_CONVERT_EXPR:
3900 case ADDR_SPACE_CONVERT_EXPR:
3901
3902 op1 = TREE_OPERAND (t, 0);
3903
3904 t = const_unop (code, TREE_TYPE (t), op1);
3905 if (!t)
3906 return NULL_TREE;
3907
3908 if (CONVERT_EXPR_CODE_P (code)
3909 && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1))
3910 TREE_OVERFLOW (t) = false;
3911 return t;
3912
3913 default:
3914 return NULL_TREE;
3915 }
3916}
3917
3918/* If T is a simple constant expression, returns its simplified value.
3919 Otherwise returns T. In contrast to maybe_constant_value do we
3920 simplify only few operations on constant-expressions, and we don't
3921 try to simplify constexpressions. */
3922
3923tree
3924fold_simple (tree t)
3925{
3926 tree r = NULL_TREE;
3927 if (processing_template_decl)
3928 return t;
3929
3930 r = fold_simple_1 (t);
3931 if (!r)
3932 r = t;
3933
3934 return r;
3935}
3936
2d76680f
PC
3937/* If T is a constant expression, returns its reduced value.
3938 Otherwise, if T does not have TREE_CONSTANT set, returns T.
3939 Otherwise, returns a version of T without TREE_CONSTANT. */
3940
cda0a029
JM
3941static tree
3942maybe_constant_value_1 (tree t, tree decl)
2d76680f
PC
3943{
3944 tree r;
3945
3946 if (instantiation_dependent_expression_p (t)
3947 || type_unknown_p (t)
3948 || BRACE_ENCLOSED_INITIALIZER_P (t)
3949 || !potential_constant_expression (t))
3950 {
3951 if (TREE_OVERFLOW_P (t))
3952 {
3953 t = build_nop (TREE_TYPE (t), t);
3954 TREE_CONSTANT (t) = false;
3955 }
3956 return t;
3957 }
3958
69eb4fde 3959 r = cxx_eval_outermost_constant_expr (t, true, true, decl);
595278be
MM
3960 gcc_checking_assert (r == t
3961 || CONVERT_EXPR_P (t)
3962 || TREE_CODE (t) == VIEW_CONVERT_EXPR
3963 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
3964 || !cp_tree_equal (r, t));
2d76680f
PC
3965 return r;
3966}
3967
cda0a029
JM
3968static GTY((cache, deletable)) cache_map cv_cache;
3969
3970/* If T is a constant expression, returns its reduced value.
3971 Otherwise, if T does not have TREE_CONSTANT set, returns T.
3972 Otherwise, returns a version of T without TREE_CONSTANT. */
3973
3974tree
3975maybe_constant_value (tree t, tree decl)
3976{
3977 tree ret = cv_cache.get (t);
3978 if (!ret)
3979 {
3980 ret = maybe_constant_value_1 (t, decl);
3981 cv_cache.put (t, ret);
3982 }
3983 return ret;
3984}
3985
234bef96
PC
3986/* Like maybe_constant_value but first fully instantiate the argument.
3987
3988 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
3989 (t, tf_none) followed by maybe_constant_value but is more efficient,
3990 because calls instantiation_dependent_expression_p and
3991 potential_constant_expression at most once. */
3992
3993tree
3994fold_non_dependent_expr (tree t)
3995{
3996 if (t == NULL_TREE)
3997 return NULL_TREE;
3998
3999 /* If we're in a template, but T isn't value dependent, simplify
4000 it. We're supposed to treat:
4001
4002 template <typename T> void f(T[1 + 1]);
4003 template <typename T> void f(T[2]);
4004
4005 as two declarations of the same function, for example. */
4006 if (processing_template_decl)
4007 {
4008 if (!instantiation_dependent_expression_p (t)
4009 && potential_constant_expression (t))
4010 {
1b5695e6
JM
4011 processing_template_decl_sentinel s;
4012 t = instantiate_non_dependent_expr_internal (t, tf_none);
234bef96
PC
4013
4014 if (type_unknown_p (t)
4015 || BRACE_ENCLOSED_INITIALIZER_P (t))
4016 {
4017 if (TREE_OVERFLOW_P (t))
4018 {
4019 t = build_nop (TREE_TYPE (t), t);
4020 TREE_CONSTANT (t) = false;
4021 }
4022 return t;
4023 }
4024
69eb4fde 4025 tree r = cxx_eval_outermost_constant_expr (t, true, true, NULL_TREE);
234bef96 4026 /* cp_tree_equal looks through NOPs, so allow them. */
595278be
MM
4027 gcc_checking_assert (r == t
4028 || CONVERT_EXPR_P (t)
4029 || TREE_CODE (t) == VIEW_CONVERT_EXPR
4030 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
4031 || !cp_tree_equal (r, t));
234bef96
PC
4032 return r;
4033 }
4034 else if (TREE_OVERFLOW_P (t))
4035 {
4036 t = build_nop (TREE_TYPE (t), t);
4037 TREE_CONSTANT (t) = false;
4038 }
4039 return t;
4040 }
4041
4042 return maybe_constant_value (t);
4043}
4044
2d76680f
PC
4045/* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
4046 than wrapped in a TARGET_EXPR. */
4047
4048tree
3e605b20 4049maybe_constant_init (tree t, tree decl)
2d76680f 4050{
cda0a029
JM
4051 if (!t)
4052 return t;
2d76680f
PC
4053 if (TREE_CODE (t) == EXPR_STMT)
4054 t = TREE_OPERAND (t, 0);
4055 if (TREE_CODE (t) == CONVERT_EXPR
4056 && VOID_TYPE_P (TREE_TYPE (t)))
4057 t = TREE_OPERAND (t, 0);
60813a46
JM
4058 if (TREE_CODE (t) == INIT_EXPR)
4059 t = TREE_OPERAND (t, 1);
69eb4fde
JM
4060 if (instantiation_dependent_expression_p (t)
4061 || type_unknown_p (t)
4062 || BRACE_ENCLOSED_INITIALIZER_P (t)
4063 || !potential_static_init_expression (t))
4064 /* Don't try to evaluate it. */;
4065 else
4066 t = cxx_eval_outermost_constant_expr (t, true, false, decl);
2d76680f
PC
4067 if (TREE_CODE (t) == TARGET_EXPR)
4068 {
4069 tree init = TARGET_EXPR_INITIAL (t);
4070 if (TREE_CODE (init) == CONSTRUCTOR)
4071 t = init;
4072 }
4073 return t;
4074}
4075
4076#if 0
4077/* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
4078/* Return true if the object referred to by REF has automatic or thread
4079 local storage. */
4080
4081enum { ck_ok, ck_bad, ck_unknown };
4082static int
4083check_automatic_or_tls (tree ref)
4084{
ef4bddc2 4085 machine_mode mode;
2d76680f
PC
4086 HOST_WIDE_INT bitsize, bitpos;
4087 tree offset;
4088 int volatilep = 0, unsignedp = 0;
4089 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
4090 &mode, &unsignedp, &volatilep, false);
4091 duration_kind dk;
4092
4093 /* If there isn't a decl in the middle, we don't know the linkage here,
4094 and this isn't a constant expression anyway. */
4095 if (!DECL_P (decl))
4096 return ck_unknown;
4097 dk = decl_storage_duration (decl);
4098 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
4099}
4100#endif
4101
4102/* Return true if T denotes a potentially constant expression. Issue
4103 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
4104 an lvalue-rvalue conversion is implied.
4105
4106 C++0x [expr.const] used to say
4107
4108 6 An expression is a potential constant expression if it is
4109 a constant expression where all occurrences of function
4110 parameters are replaced by arbitrary constant expressions
4111 of the appropriate type.
4112
4113 2 A conditional expression is a constant expression unless it
4114 involves one of the following as a potentially evaluated
4115 subexpression (3.2), but subexpressions of logical AND (5.14),
4116 logical OR (5.15), and conditional (5.16) operations that are
4117 not evaluated are not considered. */
4118
4119static bool
69eb4fde
JM
4120potential_constant_expression_1 (tree t, bool want_rval, bool strict,
4121 tsubst_flags_t flags)
2d76680f 4122{
69eb4fde 4123#define RECUR(T,RV) potential_constant_expression_1 ((T), (RV), strict, flags)
2d76680f
PC
4124 enum { any = false, rval = true };
4125 int i;
4126 tree tmp;
4127
4128 if (t == error_mark_node)
4129 return false;
4130 if (t == NULL_TREE)
4131 return true;
19dedccf 4132 if (TREE_THIS_VOLATILE (t) && !DECL_P (t))
2d76680f
PC
4133 {
4134 if (flags & tf_error)
4135 error ("expression %qE has side-effects", t);
4136 return false;
4137 }
4138 if (CONSTANT_CLASS_P (t))
4139 return true;
4140
4141 switch (TREE_CODE (t))
4142 {
4143 case FUNCTION_DECL:
4144 case BASELINK:
4145 case TEMPLATE_DECL:
4146 case OVERLOAD:
4147 case TEMPLATE_ID_EXPR:
4148 case LABEL_DECL:
4149 case LABEL_EXPR:
60813a46 4150 case CASE_LABEL_EXPR:
2d76680f
PC
4151 case CONST_DECL:
4152 case SIZEOF_EXPR:
4153 case ALIGNOF_EXPR:
4154 case OFFSETOF_EXPR:
4155 case NOEXCEPT_EXPR:
4156 case TEMPLATE_PARM_INDEX:
4157 case TRAIT_EXPR:
4158 case IDENTIFIER_NODE:
4159 case USERDEF_LITERAL:
4160 /* We can see a FIELD_DECL in a pointer-to-member expression. */
4161 case FIELD_DECL:
4162 case PARM_DECL:
cda0a029 4163 case RESULT_DECL:
2d76680f 4164 case USING_DECL:
60813a46 4165 case USING_STMT:
3e605b20 4166 case PLACEHOLDER_EXPR:
60813a46
JM
4167 case BREAK_STMT:
4168 case CONTINUE_STMT:
971e17ff 4169 case REQUIRES_EXPR:
2d76680f
PC
4170 return true;
4171
4172 case AGGR_INIT_EXPR:
4173 case CALL_EXPR:
4174 /* -- an invocation of a function other than a constexpr function
4175 or a constexpr constructor. */
4176 {
4177 tree fun = get_function_named_in_call (t);
4178 const int nargs = call_expr_nargs (t);
4179 i = 0;
4180
60813a46
JM
4181 if (fun == NULL_TREE)
4182 {
35228ac7
JJ
4183 if (TREE_CODE (t) == CALL_EXPR
4184 && CALL_EXPR_FN (t) == NULL_TREE)
4185 switch (CALL_EXPR_IFN (t))
4186 {
4187 /* These should be ignored, they are optimized away from
4188 constexpr functions. */
4189 case IFN_UBSAN_NULL:
4190 case IFN_UBSAN_BOUNDS:
4191 case IFN_UBSAN_VPTR:
4192 return true;
4193 default:
4194 break;
4195 }
60813a46
JM
4196 /* fold_call_expr can't do anything with IFN calls. */
4197 if (flags & tf_error)
4198 error_at (EXPR_LOC_OR_LOC (t, input_location),
4199 "call to internal function");
4200 return false;
4201 }
2d76680f
PC
4202 if (is_overloaded_fn (fun))
4203 {
4204 if (TREE_CODE (fun) == FUNCTION_DECL)
4205 {
4206 if (builtin_valid_in_constant_expr_p (fun))
4207 return true;
4208 if (!DECL_DECLARED_CONSTEXPR_P (fun)
4209 /* Allow any built-in function; if the expansion
4210 isn't constant, we'll deal with that then. */
4211 && !is_builtin_fn (fun))
4212 {
4213 if (flags & tf_error)
4214 {
4215 error_at (EXPR_LOC_OR_LOC (t, input_location),
4216 "call to non-constexpr function %qD", fun);
4217 explain_invalid_constexpr_fn (fun);
4218 }
4219 return false;
4220 }
4221 /* A call to a non-static member function takes the address
4222 of the object as the first argument. But in a constant
4223 expression the address will be folded away, so look
4224 through it now. */
4225 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
4226 && !DECL_CONSTRUCTOR_P (fun))
4227 {
4228 tree x = get_nth_callarg (t, 0);
4229 if (is_this_parameter (x))
3e605b20 4230 return true;
69eb4fde 4231 else if (!RECUR (x, rval))
2d76680f
PC
4232 return false;
4233 i = 1;
4234 }
4235 }
4236 else
4237 {
69eb4fde 4238 if (!RECUR (fun, true))
2d76680f
PC
4239 return false;
4240 fun = get_first_fn (fun);
4241 }
4242 /* Skip initial arguments to base constructors. */
4243 if (DECL_BASE_CONSTRUCTOR_P (fun))
4244 i = num_artificial_parms_for (fun);
4245 fun = DECL_ORIGIN (fun);
4246 }
4247 else
4248 {
69eb4fde 4249 if (RECUR (fun, rval))
2d76680f
PC
4250 /* Might end up being a constant function pointer. */;
4251 else
4252 return false;
4253 }
4254 for (; i < nargs; ++i)
4255 {
4256 tree x = get_nth_callarg (t, i);
c3c29ba5
JM
4257 /* In a template, reference arguments haven't been converted to
4258 REFERENCE_TYPE and we might not even know if the parameter
4259 is a reference, so accept lvalue constants too. */
4260 bool rv = processing_template_decl ? any : rval;
4261 if (!RECUR (x, rv))
2d76680f
PC
4262 return false;
4263 }
4264 return true;
4265 }
4266
4267 case NON_LVALUE_EXPR:
4268 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
4269 -- an lvalue of integral type that refers to a non-volatile
4270 const variable or static data member initialized with
4271 constant expressions, or
4272
4273 -- an lvalue of literal type that refers to non-volatile
4274 object defined with constexpr, or that refers to a
4275 sub-object of such an object; */
69eb4fde 4276 return RECUR (TREE_OPERAND (t, 0), rval);
2d76680f
PC
4277
4278 case VAR_DECL:
69eb4fde
JM
4279 if (want_rval
4280 && !decl_constant_var_p (t)
4281 && (strict
4282 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
4283 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t))
2d76680f 4284 && !var_in_constexpr_fn (t)
c6a38536 4285 && !type_dependent_expression_p (t))
2d76680f
PC
4286 {
4287 if (flags & tf_error)
4288 non_const_var_error (t);
4289 return false;
4290 }
4291 return true;
4292
4293 case NOP_EXPR:
4294 case CONVERT_EXPR:
4295 case VIEW_CONVERT_EXPR:
4296 /* -- a reinterpret_cast. FIXME not implemented, and this rule
4297 may change to something more specific to type-punning (DR 1312). */
4298 {
4299 tree from = TREE_OPERAND (t, 0);
4300 if (POINTER_TYPE_P (TREE_TYPE (t))
4301 && TREE_CODE (from) == INTEGER_CST
4302 && !integer_zerop (from))
4303 {
4304 if (flags & tf_error)
4305 error_at (EXPR_LOC_OR_LOC (t, input_location),
4306 "reinterpret_cast from integer to pointer");
4307 return false;
4308 }
69eb4fde 4309 return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
2d76680f
PC
4310 }
4311
4312 case ADDR_EXPR:
4313 /* -- a unary operator & that is applied to an lvalue that
4314 designates an object with thread or automatic storage
4315 duration; */
4316 t = TREE_OPERAND (t, 0);
4317
4318 if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
4319 /* A pointer-to-member constant. */
4320 return true;
4321
4322#if 0
4323 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
4324 any checking here, as we might dereference the pointer later. If
4325 we remove this code, also remove check_automatic_or_tls. */
4326 i = check_automatic_or_tls (t);
4327 if (i == ck_ok)
4328 return true;
4329 if (i == ck_bad)
4330 {
4331 if (flags & tf_error)
4332 error ("address-of an object %qE with thread local or "
4333 "automatic storage is not a constant expression", t);
4334 return false;
4335 }
4336#endif
69eb4fde 4337 return RECUR (t, any);
2d76680f
PC
4338
4339 case COMPONENT_REF:
4340 case BIT_FIELD_REF:
4341 case ARROW_EXPR:
4342 case OFFSET_REF:
4343 /* -- a class member access unless its postfix-expression is
4344 of literal type or of pointer to literal type. */
4345 /* This test would be redundant, as it follows from the
4346 postfix-expression being a potential constant expression. */
19dedccf
JM
4347 if (type_unknown_p (t))
4348 return true;
69eb4fde 4349 return RECUR (TREE_OPERAND (t, 0), want_rval);
2d76680f
PC
4350
4351 case EXPR_PACK_EXPANSION:
69eb4fde 4352 return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
2d76680f
PC
4353
4354 case INDIRECT_REF:
4355 {
4356 tree x = TREE_OPERAND (t, 0);
4357 STRIP_NOPS (x);
4358 if (is_this_parameter (x))
4359 {
4360 if (DECL_CONTEXT (x)
4361 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x)))
4362 {
4363 if (flags & tf_error)
4364 error ("use of %<this%> in a constant expression");
4365 return false;
4366 }
2d76680f
PC
4367 return true;
4368 }
69eb4fde 4369 return RECUR (x, rval);
2d76680f
PC
4370 }
4371
60813a46
JM
4372 case STATEMENT_LIST:
4373 {
4374 tree_stmt_iterator i;
4375 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
4376 {
69eb4fde 4377 if (!RECUR (tsi_stmt (i), any))
60813a46
JM
4378 return false;
4379 }
4380 return true;
4381 }
4382 break;
4383
4384 case MODIFY_EXPR:
4385 if (cxx_dialect < cxx14)
4386 goto fail;
69eb4fde 4387 if (!RECUR (TREE_OPERAND (t, 0), any))
60813a46 4388 return false;
69eb4fde 4389 if (!RECUR (TREE_OPERAND (t, 1), rval))
60813a46
JM
4390 return false;
4391 return true;
4392
4393 case MODOP_EXPR:
4394 if (cxx_dialect < cxx14)
4395 goto fail;
69eb4fde 4396 if (!RECUR (TREE_OPERAND (t, 0), rval))
60813a46 4397 return false;
69eb4fde 4398 if (!RECUR (TREE_OPERAND (t, 2), rval))
60813a46
JM
4399 return false;
4400 return true;
4401
60813a46 4402 case DO_STMT:
69eb4fde 4403 if (!RECUR (DO_COND (t), rval))
60813a46 4404 return false;
69eb4fde 4405 if (!RECUR (DO_BODY (t), any))
60813a46
JM
4406 return false;
4407 return true;
4408
4409 case FOR_STMT:
69eb4fde 4410 if (!RECUR (FOR_INIT_STMT (t), any))
60813a46 4411 return false;
69eb4fde 4412 if (!RECUR (FOR_COND (t), rval))
60813a46 4413 return false;
69eb4fde 4414 if (!RECUR (FOR_EXPR (t), any))
60813a46 4415 return false;
69eb4fde 4416 if (!RECUR (FOR_BODY (t), any))
60813a46
JM
4417 return false;
4418 return true;
4419
4420 case WHILE_STMT:
69eb4fde 4421 if (!RECUR (WHILE_COND (t), rval))
60813a46 4422 return false;
69eb4fde 4423 if (!RECUR (WHILE_BODY (t), any))
60813a46
JM
4424 return false;
4425 return true;
4426
4427 case SWITCH_STMT:
69eb4fde 4428 if (!RECUR (SWITCH_STMT_COND (t), rval))
60813a46 4429 return false;
ce965730
MT
4430 /* FIXME we don't check SWITCH_STMT_BODY currently, because even
4431 unreachable labels would be checked. */
60813a46
JM
4432 return true;
4433
58611fb6 4434 case STMT_EXPR:
69eb4fde 4435 return RECUR (STMT_EXPR_STMT (t), rval);
58611fb6 4436
2d76680f
PC
4437 case LAMBDA_EXPR:
4438 case DYNAMIC_CAST_EXPR:
4439 case PSEUDO_DTOR_EXPR:
2d76680f
PC
4440 case NEW_EXPR:
4441 case VEC_NEW_EXPR:
4442 case DELETE_EXPR:
4443 case VEC_DELETE_EXPR:
4444 case THROW_EXPR:
2d76680f
PC
4445 case OMP_ATOMIC:
4446 case OMP_ATOMIC_READ:
4447 case OMP_ATOMIC_CAPTURE_OLD:
4448 case OMP_ATOMIC_CAPTURE_NEW:
4449 /* GCC internal stuff. */
4450 case VA_ARG_EXPR:
4451 case OBJ_TYPE_REF:
2d76680f 4452 case TRANSACTION_EXPR:
60813a46 4453 case ASM_EXPR:
81b6a6c5 4454 case AT_ENCODE_EXPR:
60813a46 4455 fail:
2d76680f
PC
4456 if (flags & tf_error)
4457 error ("expression %qE is not a constant-expression", t);
4458 return false;
4459
4460 case TYPEID_EXPR:
4461 /* -- a typeid expression whose operand is of polymorphic
4462 class type; */
4463 {
4464 tree e = TREE_OPERAND (t, 0);
4465 if (!TYPE_P (e) && !type_dependent_expression_p (e)
4466 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
4467 {
4468 if (flags & tf_error)
4469 error ("typeid-expression is not a constant expression "
4470 "because %qE is of polymorphic type", e);
4471 return false;
4472 }
4473 return true;
4474 }
4475
4476 case MINUS_EXPR:
2d76680f
PC
4477 want_rval = true;
4478 goto binary;
4479
4480 case LT_EXPR:
4481 case LE_EXPR:
4482 case GT_EXPR:
4483 case GE_EXPR:
4484 case EQ_EXPR:
4485 case NE_EXPR:
2d76680f
PC
4486 want_rval = true;
4487 goto binary;
4488
60813a46
JM
4489 case PREINCREMENT_EXPR:
4490 case POSTINCREMENT_EXPR:
4491 case PREDECREMENT_EXPR:
4492 case POSTDECREMENT_EXPR:
4493 if (cxx_dialect < cxx14)
4494 goto fail;
4495 goto unary;
4496
2d76680f
PC
4497 case BIT_NOT_EXPR:
4498 /* A destructor. */
4499 if (TYPE_P (TREE_OPERAND (t, 0)))
4500 return true;
4501 /* else fall through. */
4502
4503 case REALPART_EXPR:
4504 case IMAGPART_EXPR:
4505 case CONJ_EXPR:
4506 case SAVE_EXPR:
4507 case FIX_TRUNC_EXPR:
4508 case FLOAT_EXPR:
4509 case NEGATE_EXPR:
4510 case ABS_EXPR:
4511 case TRUTH_NOT_EXPR:
4512 case FIXED_CONVERT_EXPR:
4513 case UNARY_PLUS_EXPR:
4856a1f0
PC
4514 case UNARY_LEFT_FOLD_EXPR:
4515 case UNARY_RIGHT_FOLD_EXPR:
60813a46 4516 unary:
69eb4fde 4517 return RECUR (TREE_OPERAND (t, 0), rval);
2d76680f
PC
4518
4519 case CAST_EXPR:
4520 case CONST_CAST_EXPR:
4521 case STATIC_CAST_EXPR:
4522 case REINTERPRET_CAST_EXPR:
4523 case IMPLICIT_CONV_EXPR:
4524 if (cxx_dialect < cxx11
4525 && !dependent_type_p (TREE_TYPE (t))
4526 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
4527 /* In C++98, a conversion to non-integral type can't be part of a
4528 constant expression. */
4529 {
4530 if (flags & tf_error)
4531 error ("cast to non-integral type %qT in a constant expression",
4532 TREE_TYPE (t));
4533 return false;
4534 }
4535
69eb4fde
JM
4536 return (RECUR (TREE_OPERAND (t, 0),
4537 TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE));
2d76680f 4538
60813a46 4539 case BIND_EXPR:
69eb4fde 4540 return RECUR (BIND_EXPR_BODY (t), want_rval);
60813a46
JM
4541
4542 case WITH_CLEANUP_EXPR:
4543 case CLEANUP_POINT_EXPR:
4544 case MUST_NOT_THROW_EXPR:
4545 case TRY_CATCH_EXPR:
769430b2 4546 case TRY_BLOCK:
60813a46
JM
4547 case EH_SPEC_BLOCK:
4548 case EXPR_STMT:
2d76680f 4549 case PAREN_EXPR:
60813a46 4550 case DECL_EXPR:
2d76680f
PC
4551 case NON_DEPENDENT_EXPR:
4552 /* For convenience. */
4553 case RETURN_EXPR:
69eb4fde 4554 return RECUR (TREE_OPERAND (t, 0), want_rval);
2d76680f 4555
769430b2
JM
4556 case TRY_FINALLY_EXPR:
4557 return (RECUR (TREE_OPERAND (t, 0), want_rval)
4558 && RECUR (TREE_OPERAND (t, 1), any));
4559
2d76680f 4560 case SCOPE_REF:
69eb4fde 4561 return RECUR (TREE_OPERAND (t, 1), want_rval);
2d76680f
PC
4562
4563 case TARGET_EXPR:
4564 if (!literal_type_p (TREE_TYPE (t)))
4565 {
4566 if (flags & tf_error)
4567 {
4568 error ("temporary of non-literal type %qT in a "
4569 "constant expression", TREE_TYPE (t));
4570 explain_non_literal_class (TREE_TYPE (t));
4571 }
4572 return false;
4573 }
4574 case INIT_EXPR:
69eb4fde 4575 return RECUR (TREE_OPERAND (t, 1), rval);
2d76680f
PC
4576
4577 case CONSTRUCTOR:
4578 {
4579 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
4580 constructor_elt *ce;
4581 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
69eb4fde 4582 if (!RECUR (ce->value, want_rval))
2d76680f
PC
4583 return false;
4584 return true;
4585 }
4586
4587 case TREE_LIST:
4588 {
4589 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
4590 || DECL_P (TREE_PURPOSE (t)));
69eb4fde 4591 if (!RECUR (TREE_VALUE (t), want_rval))
2d76680f
PC
4592 return false;
4593 if (TREE_CHAIN (t) == NULL_TREE)
4594 return true;
69eb4fde 4595 return RECUR (TREE_CHAIN (t), want_rval);
2d76680f
PC
4596 }
4597
4598 case TRUNC_DIV_EXPR:
4599 case CEIL_DIV_EXPR:
4600 case FLOOR_DIV_EXPR:
4601 case ROUND_DIV_EXPR:
4602 case TRUNC_MOD_EXPR:
4603 case CEIL_MOD_EXPR:
4604 case ROUND_MOD_EXPR:
4605 {
4606 tree denom = TREE_OPERAND (t, 1);
69eb4fde 4607 if (!RECUR (denom, rval))
2d76680f
PC
4608 return false;
4609 /* We can't call cxx_eval_outermost_constant_expr on an expression
234bef96 4610 that hasn't been through instantiate_non_dependent_expr yet. */
2d76680f
PC
4611 if (!processing_template_decl)
4612 denom = cxx_eval_outermost_constant_expr (denom, true);
4613 if (integer_zerop (denom))
4614 {
4615 if (flags & tf_error)
4616 error ("division by zero is not a constant-expression");
4617 return false;
4618 }
4619 else
4620 {
4621 want_rval = true;
69eb4fde 4622 return RECUR (TREE_OPERAND (t, 0), want_rval);
2d76680f
PC
4623 }
4624 }
4625
4626 case COMPOUND_EXPR:
4627 {
4628 /* check_return_expr sometimes wraps a TARGET_EXPR in a
4629 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
4630 introduced by build_call_a. */
4631 tree op0 = TREE_OPERAND (t, 0);
4632 tree op1 = TREE_OPERAND (t, 1);
4633 STRIP_NOPS (op1);
4634 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
4635 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
69eb4fde 4636 return RECUR (op0, want_rval);
2d76680f
PC
4637 else
4638 goto binary;
4639 }
4640
4641 /* If the first operand is the non-short-circuit constant, look at
4642 the second operand; otherwise we only care about the first one for
4643 potentiality. */
4644 case TRUTH_AND_EXPR:
4645 case TRUTH_ANDIF_EXPR:
4646 tmp = boolean_true_node;
4647 goto truth;
4648 case TRUTH_OR_EXPR:
4649 case TRUTH_ORIF_EXPR:
4650 tmp = boolean_false_node;
4651 truth:
4652 {
4653 tree op = TREE_OPERAND (t, 0);
69eb4fde 4654 if (!RECUR (op, rval))
2d76680f
PC
4655 return false;
4656 if (!processing_template_decl)
4657 op = cxx_eval_outermost_constant_expr (op, true);
4658 if (tree_int_cst_equal (op, tmp))
69eb4fde 4659 return RECUR (TREE_OPERAND (t, 1), rval);
2d76680f
PC
4660 else
4661 return true;
4662 }
4663
4664 case PLUS_EXPR:
4665 case MULT_EXPR:
4666 case POINTER_PLUS_EXPR:
4667 case RDIV_EXPR:
4668 case EXACT_DIV_EXPR:
4669 case MIN_EXPR:
4670 case MAX_EXPR:
4671 case LSHIFT_EXPR:
4672 case RSHIFT_EXPR:
4673 case LROTATE_EXPR:
4674 case RROTATE_EXPR:
4675 case BIT_IOR_EXPR:
4676 case BIT_XOR_EXPR:
4677 case BIT_AND_EXPR:
4678 case TRUTH_XOR_EXPR:
4679 case UNORDERED_EXPR:
4680 case ORDERED_EXPR:
4681 case UNLT_EXPR:
4682 case UNLE_EXPR:
4683 case UNGT_EXPR:
4684 case UNGE_EXPR:
4685 case UNEQ_EXPR:
4686 case LTGT_EXPR:
4687 case RANGE_EXPR:
4688 case COMPLEX_EXPR:
4689 want_rval = true;
4690 /* Fall through. */
4691 case ARRAY_REF:
4692 case ARRAY_RANGE_REF:
4693 case MEMBER_REF:
4694 case DOTSTAR_EXPR:
41b38772 4695 case MEM_REF:
4856a1f0
PC
4696 case BINARY_LEFT_FOLD_EXPR:
4697 case BINARY_RIGHT_FOLD_EXPR:
2d76680f
PC
4698 binary:
4699 for (i = 0; i < 2; ++i)
69eb4fde 4700 if (!RECUR (TREE_OPERAND (t, i), want_rval))
2d76680f
PC
4701 return false;
4702 return true;
4703
4704 case CILK_SYNC_STMT:
4705 case CILK_SPAWN_STMT:
4706 case ARRAY_NOTATION_REF:
4707 return false;
4708
4709 case FMA_EXPR:
4710 case VEC_PERM_EXPR:
4711 for (i = 0; i < 3; ++i)
69eb4fde 4712 if (!RECUR (TREE_OPERAND (t, i), true))
2d76680f
PC
4713 return false;
4714 return true;
4715
ce965730 4716 case IF_STMT:
2d76680f
PC
4717 case COND_EXPR:
4718 case VEC_COND_EXPR:
4719 /* If the condition is a known constant, we know which of the legs we
4720 care about; otherwise we only require that the condition and
4721 either of the legs be potentially constant. */
4722 tmp = TREE_OPERAND (t, 0);
69eb4fde 4723 if (!RECUR (tmp, rval))
2d76680f
PC
4724 return false;
4725 if (!processing_template_decl)
4726 tmp = cxx_eval_outermost_constant_expr (tmp, true);
4727 if (integer_zerop (tmp))
69eb4fde 4728 return RECUR (TREE_OPERAND (t, 2), want_rval);
2d76680f 4729 else if (TREE_CODE (tmp) == INTEGER_CST)
69eb4fde 4730 return RECUR (TREE_OPERAND (t, 1), want_rval);
2d76680f
PC
4731 for (i = 1; i < 3; ++i)
4732 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
69eb4fde 4733 want_rval, strict, tf_none))
2d76680f
PC
4734 return true;
4735 if (flags & tf_error)
4736 error ("expression %qE is not a constant-expression", t);
4737 return false;
4738
4739 case VEC_INIT_EXPR:
4740 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
4741 return true;
4742 if (flags & tf_error)
4743 {
4744 error ("non-constant array initialization");
4745 diagnose_non_constexpr_vec_init (t);
4746 }
4747 return false;
4748
133bc698
JM
4749 case TYPE_DECL:
4750 case TAG_DEFN:
4751 /* We can see these in statement-expressions. */
4752 return true;
4753
cda0a029
JM
4754 case EMPTY_CLASS_EXPR:
4755 return false;
4756
2d76680f
PC
4757 default:
4758 if (objc_is_property_ref (t))
4759 return false;
4760
4761 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
4762 gcc_unreachable();
4763 return false;
4764 }
69eb4fde 4765#undef RECUR
2d76680f
PC
4766}
4767
4768/* The main entry point to the above. */
4769
4770bool
4771potential_constant_expression (tree t)
4772{
69eb4fde
JM
4773 return potential_constant_expression_1 (t, false, true, tf_none);
4774}
4775
4776bool
4777potential_static_init_expression (tree t)
4778{
4779 return potential_constant_expression_1 (t, false, false, tf_none);
2d76680f
PC
4780}
4781
4782/* As above, but require a constant rvalue. */
4783
4784bool
4785potential_rvalue_constant_expression (tree t)
4786{
69eb4fde 4787 return potential_constant_expression_1 (t, true, true, tf_none);
2d76680f
PC
4788}
4789
4790/* Like above, but complain about non-constant expressions. */
4791
4792bool
4793require_potential_constant_expression (tree t)
4794{
69eb4fde 4795 return potential_constant_expression_1 (t, false, true, tf_warning_or_error);
2d76680f
PC
4796}
4797
4798/* Cross product of the above. */
4799
4800bool
4801require_potential_rvalue_constant_expression (tree t)
4802{
69eb4fde 4803 return potential_constant_expression_1 (t, true, true, tf_warning_or_error);
2d76680f
PC
4804}
4805
4806#include "gt-cp-constexpr.h"
This page took 1.137627 seconds and 5 git commands to generate.