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