]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/constexpr.c
PR c++/88095, CTAD for literal operator templates per P0732
[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
a5544970 5 Copyright (C) 1998-2019 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"
44a845ca 34#include "gimple-fold.h"
8108ea30 35#include "timevar.h"
d8fcab68 36#include "fold-const-call.h"
2d76680f
PC
37
38static bool verify_constant (tree, bool, bool *, bool *);
39#define VERIFY_CONSTANT(X) \
40do { \
2b3ab879 41 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
2d76680f
PC
42 return t; \
43 } while (0)
44
582d2481
JJ
45static HOST_WIDE_INT find_array_ctor_elt (tree ary, tree dindex,
46 bool insert = false);
47
2d76680f
PC
48/* Returns true iff FUN is an instantiation of a constexpr function
49 template or a defaulted constexpr function. */
50
51bool
52is_instantiation_of_constexpr (tree fun)
53{
54 return ((DECL_TEMPLOID_INSTANTIATION (fun)
55 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
56 || (DECL_DEFAULTED_FN (fun)
57 && DECL_DECLARED_CONSTEXPR_P (fun)));
58}
59
60/* Return true if T is a literal type. */
61
62bool
63literal_type_p (tree t)
64{
65 if (SCALAR_TYPE_P (t)
b55b02ea 66 || VECTOR_TYPE_P (t)
9f613f06 67 || TYPE_REF_P (t)
e42c407c 68 || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
2d76680f
PC
69 return true;
70 if (CLASS_TYPE_P (t))
71 {
72 t = complete_type (t);
73 gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
74 return CLASSTYPE_LITERAL_P (t);
75 }
76 if (TREE_CODE (t) == ARRAY_TYPE)
77 return literal_type_p (strip_array_types (t));
78 return false;
79}
80
81/* If DECL is a variable declared `constexpr', require its type
9638f320
PC
82 be literal. Return error_mark_node if we give an error, the
83 DECL otherwise. */
2d76680f
PC
84
85tree
86ensure_literal_type_for_constexpr_object (tree decl)
87{
88 tree type = TREE_TYPE (decl);
89 if (VAR_P (decl)
90 && (DECL_DECLARED_CONSTEXPR_P (decl)
91 || var_in_constexpr_fn (decl))
92 && !processing_template_decl)
93 {
94 tree stype = strip_array_types (type);
95 if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype)))
96 /* Don't complain here, we'll complain about incompleteness
97 when we try to initialize the variable. */;
683be2f7
JM
98 else if (type_uses_auto (type))
99 /* We don't know the actual type yet. */;
2d76680f
PC
100 else if (!literal_type_p (type))
101 {
102 if (DECL_DECLARED_CONSTEXPR_P (decl))
f1eac182 103 {
097f82ec 104 auto_diagnostic_group d;
f2935576
PC
105 error_at (DECL_SOURCE_LOCATION (decl),
106 "the type %qT of %<constexpr%> variable %qD "
107 "is not literal", type, decl);
f1eac182 108 explain_non_literal_class (type);
9638f320 109 decl = error_mark_node;
f1eac182 110 }
2d76680f 111 else
60813a46 112 {
5d4e573b 113 if (!is_instantiation_of_constexpr (current_function_decl))
f1eac182 114 {
097f82ec 115 auto_diagnostic_group d;
f2935576
PC
116 error_at (DECL_SOURCE_LOCATION (decl),
117 "variable %qD of non-literal type %qT in "
118 "%<constexpr%> function", decl, type);
f1eac182 119 explain_non_literal_class (type);
9638f320 120 decl = error_mark_node;
f1eac182 121 }
60813a46
JM
122 cp_function_chain->invalid_constexpr = true;
123 }
2d76680f 124 }
8e9589bd
JM
125 else if (DECL_DECLARED_CONSTEXPR_P (decl)
126 && variably_modified_type_p (type, NULL_TREE))
127 {
f2935576
PC
128 error_at (DECL_SOURCE_LOCATION (decl),
129 "%<constexpr%> variable %qD has variably-modified "
130 "type %qT", decl, type);
8e9589bd
JM
131 decl = error_mark_node;
132 }
2d76680f
PC
133 }
134 return decl;
135}
136
137/* Representation of entries in the constexpr function definition table. */
138
139struct GTY((for_user)) constexpr_fundef {
140 tree decl;
141 tree body;
43574e4f
JJ
142 tree parms;
143 tree result;
2d76680f
PC
144};
145
ca752f39 146struct constexpr_fundef_hasher : ggc_ptr_hash<constexpr_fundef>
2d76680f
PC
147{
148 static hashval_t hash (constexpr_fundef *);
149 static bool equal (constexpr_fundef *, constexpr_fundef *);
150};
151
152/* This table holds all constexpr function definitions seen in
153 the current translation unit. */
154
155static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table;
156
157/* Utility function used for managing the constexpr function table.
158 Return true if the entries pointed to by P and Q are for the
159 same constexpr function. */
160
161inline bool
162constexpr_fundef_hasher::equal (constexpr_fundef *lhs, constexpr_fundef *rhs)
163{
164 return lhs->decl == rhs->decl;
165}
166
167/* Utility function used for managing the constexpr function table.
168 Return a hash value for the entry pointed to by Q. */
169
170inline hashval_t
171constexpr_fundef_hasher::hash (constexpr_fundef *fundef)
172{
173 return DECL_UID (fundef->decl);
174}
175
176/* Return a previously saved definition of function FUN. */
177
178static constexpr_fundef *
179retrieve_constexpr_fundef (tree fun)
180{
2d76680f
PC
181 if (constexpr_fundef_table == NULL)
182 return NULL;
183
43574e4f 184 constexpr_fundef fundef = { fun, NULL, NULL, NULL };
2d76680f
PC
185 return constexpr_fundef_table->find (&fundef);
186}
187
188/* Check whether the parameter and return types of FUN are valid for a
189 constexpr function, and complain if COMPLAIN. */
190
98e5a19a 191bool
2d76680f
PC
192is_valid_constexpr_fn (tree fun, bool complain)
193{
194 bool ret = true;
195
31f7f784 196 if (DECL_INHERITED_CTOR (fun)
2d76680f
PC
197 && TREE_CODE (fun) == TEMPLATE_DECL)
198 {
199 ret = false;
200 if (complain)
84fa214d 201 error ("inherited constructor %qD is not %<constexpr%>",
31f7f784 202 DECL_INHERITED_CTOR (fun));
2d76680f
PC
203 }
204 else
205 {
206 for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
207 parm != NULL_TREE; parm = TREE_CHAIN (parm))
208 if (!literal_type_p (TREE_TYPE (parm)))
209 {
210 ret = false;
211 if (complain)
212 {
097f82ec 213 auto_diagnostic_group d;
84fa214d 214 error ("invalid type for parameter %d of %<constexpr%> "
2d76680f
PC
215 "function %q+#D", DECL_PARM_INDEX (parm), fun);
216 explain_non_literal_class (TREE_TYPE (parm));
217 }
218 }
219 }
220
e1bea341
JM
221 if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) && cxx_dialect < cxx17)
222 {
223 ret = false;
224 if (complain)
225 inform (DECL_SOURCE_LOCATION (fun),
84fa214d 226 "lambdas are implicitly %<constexpr%> only in C++17 and later");
e1bea341
JM
227 }
228 else if (!DECL_CONSTRUCTOR_P (fun))
2d76680f
PC
229 {
230 tree rettype = TREE_TYPE (TREE_TYPE (fun));
231 if (!literal_type_p (rettype))
232 {
233 ret = false;
234 if (complain)
235 {
097f82ec 236 auto_diagnostic_group d;
84fa214d 237 error ("invalid return type %qT of %<constexpr%> function %q+D",
2d76680f
PC
238 rettype, fun);
239 explain_non_literal_class (rettype);
240 }
241 }
242
54069e59
JM
243 /* C++14 DR 1684 removed this restriction. */
244 if (cxx_dialect < cxx14
245 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
2d76680f
PC
246 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
247 {
248 ret = false;
097f82ec
DM
249 if (complain)
250 {
251 auto_diagnostic_group d;
252 if (pedwarn (DECL_SOURCE_LOCATION (fun), OPT_Wpedantic,
253 "enclosing class of %<constexpr%> non-static"
254 " member function %q+#D is not a literal type",
255 fun))
256 explain_non_literal_class (DECL_CONTEXT (fun));
257 }
2d76680f
PC
258 }
259 }
260 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
261 {
262 ret = false;
263 if (complain)
264 error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
265 }
266
267 return ret;
268}
269
270/* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
271 for a member of an anonymous aggregate, INIT is the initializer for that
272 member, and VEC_OUTER is the vector of constructor elements for the class
273 whose constructor we are processing. Add the initializer to the vector
274 and return true to indicate success. */
275
276static bool
277build_anon_member_initialization (tree member, tree init,
278 vec<constructor_elt, va_gc> **vec_outer)
279{
280 /* MEMBER presents the relevant fields from the inside out, but we need
281 to build up the initializer from the outside in so that we can reuse
282 previously built CONSTRUCTORs if this is, say, the second field in an
283 anonymous struct. So we use a vec as a stack. */
284 auto_vec<tree, 2> fields;
285 do
286 {
287 fields.safe_push (TREE_OPERAND (member, 1));
288 member = TREE_OPERAND (member, 0);
289 }
290 while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
291 && TREE_CODE (member) == COMPONENT_REF);
292
293 /* VEC has the constructor elements vector for the context of FIELD.
294 If FIELD is an anonymous aggregate, we will push inside it. */
295 vec<constructor_elt, va_gc> **vec = vec_outer;
296 tree field;
297 while (field = fields.pop(),
298 ANON_AGGR_TYPE_P (TREE_TYPE (field)))
299 {
300 tree ctor;
301 /* If there is already an outer constructor entry for the anonymous
302 aggregate FIELD, use it; otherwise, insert one. */
303 if (vec_safe_is_empty (*vec)
304 || (*vec)->last().index != field)
305 {
306 ctor = build_constructor (TREE_TYPE (field), NULL);
307 CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
308 }
309 else
310 ctor = (*vec)->last().value;
311 vec = &CONSTRUCTOR_ELTS (ctor);
312 }
313
314 /* Now we're at the innermost field, the one that isn't an anonymous
315 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
316 gcc_assert (fields.is_empty());
317 CONSTRUCTOR_APPEND_ELT (*vec, field, init);
318
319 return true;
320}
321
322/* Subroutine of build_constexpr_constructor_member_initializers.
323 The expression tree T represents a data member initialization
324 in a (constexpr) constructor definition. Build a pairing of
325 the data member with its initializer, and prepend that pair
326 to the existing initialization pair INITS. */
327
328static bool
329build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
330{
331 tree member, init;
332 if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
333 t = TREE_OPERAND (t, 0);
334 if (TREE_CODE (t) == EXPR_STMT)
335 t = TREE_OPERAND (t, 0);
336 if (t == error_mark_node)
337 return false;
338 if (TREE_CODE (t) == STATEMENT_LIST)
339 {
340 tree_stmt_iterator i;
341 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
342 {
343 if (! build_data_member_initialization (tsi_stmt (i), vec))
344 return false;
345 }
346 return true;
347 }
348 if (TREE_CODE (t) == CLEANUP_STMT)
349 {
350 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
351 but we can in a constexpr constructor for a non-literal class. Just
352 ignore it; either all the initialization will be constant, in which
353 case the cleanup can't run, or it can't be constexpr.
354 Still recurse into CLEANUP_BODY. */
355 return build_data_member_initialization (CLEANUP_BODY (t), vec);
356 }
357 if (TREE_CODE (t) == CONVERT_EXPR)
358 t = TREE_OPERAND (t, 0);
359 if (TREE_CODE (t) == INIT_EXPR
60813a46
JM
360 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
361 use what this function builds for cx_check_missing_mem_inits, and
362 assignment in the ctor body doesn't count. */
363 || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR))
2d76680f
PC
364 {
365 member = TREE_OPERAND (t, 0);
366 init = break_out_target_exprs (TREE_OPERAND (t, 1));
367 }
368 else if (TREE_CODE (t) == CALL_EXPR)
369 {
60813a46
JM
370 tree fn = get_callee_fndecl (t);
371 if (!fn || !DECL_CONSTRUCTOR_P (fn))
372 /* We're only interested in calls to subobject constructors. */
373 return true;
2d76680f
PC
374 member = CALL_EXPR_ARG (t, 0);
375 /* We don't use build_cplus_new here because it complains about
376 abstract bases. Leaving the call unwrapped means that it has the
377 wrong type, but cxx_eval_constant_expression doesn't care. */
378 init = break_out_target_exprs (t);
379 }
380 else if (TREE_CODE (t) == BIND_EXPR)
381 return build_data_member_initialization (BIND_EXPR_BODY (t), vec);
2d76680f 382 else
60813a46
JM
383 /* Don't add anything else to the CONSTRUCTOR. */
384 return true;
2d76680f
PC
385 if (INDIRECT_REF_P (member))
386 member = TREE_OPERAND (member, 0);
387 if (TREE_CODE (member) == NOP_EXPR)
388 {
389 tree op = member;
390 STRIP_NOPS (op);
391 if (TREE_CODE (op) == ADDR_EXPR)
392 {
393 gcc_assert (same_type_ignoring_top_level_qualifiers_p
394 (TREE_TYPE (TREE_TYPE (op)),
395 TREE_TYPE (TREE_TYPE (member))));
396 /* Initializing a cv-qualified member; we need to look through
397 the const_cast. */
398 member = op;
399 }
400 else if (op == current_class_ptr
401 && (same_type_ignoring_top_level_qualifiers_p
402 (TREE_TYPE (TREE_TYPE (member)),
403 current_class_type)))
404 /* Delegating constructor. */
405 member = op;
406 else
407 {
408 /* This is an initializer for an empty base; keep it for now so
409 we can check it in cxx_eval_bare_aggregate. */
410 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
411 }
412 }
413 if (TREE_CODE (member) == ADDR_EXPR)
414 member = TREE_OPERAND (member, 0);
415 if (TREE_CODE (member) == COMPONENT_REF)
416 {
417 tree aggr = TREE_OPERAND (member, 0);
8cb7aaa1
JM
418 if (TREE_CODE (aggr) == VAR_DECL)
419 /* Initializing a local variable, don't add anything. */
420 return true;
2d76680f
PC
421 if (TREE_CODE (aggr) != COMPONENT_REF)
422 /* Normal member initialization. */
423 member = TREE_OPERAND (member, 1);
424 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
425 /* Initializing a member of an anonymous union. */
426 return build_anon_member_initialization (member, init, vec);
427 else
428 /* We're initializing a vtable pointer in a base. Leave it as
429 COMPONENT_REF so we remember the path to get to the vfield. */
430 gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
431 }
432
49b5925f
JM
433 /* Value-initialization can produce multiple initializers for the
434 same field; use the last one. */
435 if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member)
436 (*vec)->last().value = init;
437 else
438 CONSTRUCTOR_APPEND_ELT (*vec, member, init);
2d76680f
PC
439 return true;
440}
441
442/* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
443 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
444 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
445
446static bool
447check_constexpr_bind_expr_vars (tree t)
448{
449 gcc_assert (TREE_CODE (t) == BIND_EXPR);
450
2d76680f
PC
451 for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
452 if (TREE_CODE (var) == TYPE_DECL
4414e22e
PC
453 && DECL_IMPLICIT_TYPEDEF_P (var)
454 && !LAMBDA_TYPE_P (TREE_TYPE (var)))
2d76680f
PC
455 return false;
456 return true;
457}
458
459/* Subroutine of check_constexpr_ctor_body. */
460
461static bool
462check_constexpr_ctor_body_1 (tree last, tree list)
463{
464 switch (TREE_CODE (list))
465 {
466 case DECL_EXPR:
a0008434
MP
467 if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL
468 || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL)
2d76680f 469 return true;
2d76680f
PC
470 return false;
471
472 case CLEANUP_POINT_EXPR:
473 return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
474 /*complain=*/false);
475
476 case BIND_EXPR:
477 if (!check_constexpr_bind_expr_vars (list)
478 || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
479 /*complain=*/false))
480 return false;
481 return true;
482
483 case USING_STMT:
484 case STATIC_ASSERT:
96a95ac1 485 case DEBUG_BEGIN_STMT:
2d76680f
PC
486 return true;
487
488 default:
489 return false;
490 }
491}
492
493/* Make sure that there are no statements after LAST in the constructor
494 body represented by LIST. */
495
496bool
497check_constexpr_ctor_body (tree last, tree list, bool complain)
498{
60813a46
JM
499 /* C++14 doesn't require a constexpr ctor to have an empty body. */
500 if (cxx_dialect >= cxx14)
501 return true;
502
2d76680f
PC
503 bool ok = true;
504 if (TREE_CODE (list) == STATEMENT_LIST)
505 {
506 tree_stmt_iterator i = tsi_last (list);
507 for (; !tsi_end_p (i); tsi_prev (&i))
508 {
509 tree t = tsi_stmt (i);
510 if (t == last)
511 break;
512 if (!check_constexpr_ctor_body_1 (last, t))
513 {
514 ok = false;
515 break;
516 }
517 }
518 }
519 else if (list != last
520 && !check_constexpr_ctor_body_1 (last, list))
521 ok = false;
522 if (!ok)
523 {
524 if (complain)
84fa214d 525 error ("%<constexpr%> constructor does not have empty body");
2d76680f
PC
526 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
527 }
528 return ok;
529}
530
531/* V is a vector of constructor elements built up for the base and member
532 initializers of a constructor for TYPE. They need to be in increasing
533 offset order, which they might not be yet if TYPE has a primary base
534 which is not first in the base-clause or a vptr and at least one base
535 all of which are non-primary. */
536
537static vec<constructor_elt, va_gc> *
538sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
539{
540 tree pri = CLASSTYPE_PRIMARY_BINFO (type);
541 tree field_type;
542 unsigned i;
543 constructor_elt *ce;
544
545 if (pri)
546 field_type = BINFO_TYPE (pri);
547 else if (TYPE_CONTAINS_VPTR_P (type))
548 field_type = vtbl_ptr_type_node;
549 else
550 return v;
551
552 /* Find the element for the primary base or vptr and move it to the
553 beginning of the vec. */
554 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
555 if (TREE_TYPE (ce->index) == field_type)
556 break;
557
558 if (i > 0 && i < vec_safe_length (v))
559 {
560 vec<constructor_elt, va_gc> &vref = *v;
561 constructor_elt elt = vref[i];
562 for (; i > 0; --i)
563 vref[i] = vref[i-1];
564 vref[0] = elt;
565 }
566
567 return v;
568}
569
570/* Build compile-time evalable representations of member-initializer list
571 for a constexpr constructor. */
572
573static tree
574build_constexpr_constructor_member_initializers (tree type, tree body)
575{
576 vec<constructor_elt, va_gc> *vec = NULL;
577 bool ok = true;
fa837fb6
JM
578 while (true)
579 switch (TREE_CODE (body))
580 {
581 case MUST_NOT_THROW_EXPR:
582 case EH_SPEC_BLOCK:
583 body = TREE_OPERAND (body, 0);
584 break;
585
586 case STATEMENT_LIST:
587 for (tree_stmt_iterator i = tsi_start (body);
588 !tsi_end_p (i); tsi_next (&i))
589 {
590 body = tsi_stmt (i);
591 if (TREE_CODE (body) == BIND_EXPR)
592 break;
593 }
594 break;
595
596 case BIND_EXPR:
597 body = BIND_EXPR_BODY (body);
598 goto found;
599
600 default:
601 gcc_unreachable ();
58cc255c 602 }
fa837fb6 603 found:
1259cb6d
JJ
604 if (TREE_CODE (body) == TRY_BLOCK)
605 {
606 body = TREE_OPERAND (body, 0);
607 if (TREE_CODE (body) == BIND_EXPR)
608 body = BIND_EXPR_BODY (body);
609 }
2d76680f
PC
610 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
611 {
612 body = TREE_OPERAND (body, 0);
613 if (TREE_CODE (body) == EXPR_STMT)
614 body = TREE_OPERAND (body, 0);
615 if (TREE_CODE (body) == INIT_EXPR
616 && (same_type_ignoring_top_level_qualifiers_p
617 (TREE_TYPE (TREE_OPERAND (body, 0)),
618 current_class_type)))
619 {
620 /* Trivial copy. */
621 return TREE_OPERAND (body, 1);
622 }
623 ok = build_data_member_initialization (body, &vec);
624 }
625 else if (TREE_CODE (body) == STATEMENT_LIST)
626 {
627 tree_stmt_iterator i;
628 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
629 {
630 ok = build_data_member_initialization (tsi_stmt (i), &vec);
631 if (!ok)
632 break;
633 }
634 }
2d76680f
PC
635 else if (EXPR_P (body))
636 ok = build_data_member_initialization (body, &vec);
637 else
638 gcc_assert (errorcount > 0);
639 if (ok)
640 {
641 if (vec_safe_length (vec) > 0)
642 {
643 /* In a delegating constructor, return the target. */
644 constructor_elt *ce = &(*vec)[0];
645 if (ce->index == current_class_ptr)
646 {
647 body = ce->value;
648 vec_free (vec);
649 return body;
650 }
651 }
652 vec = sort_constexpr_mem_initializers (type, vec);
653 return build_constructor (type, vec);
654 }
655 else
656 return error_mark_node;
657}
658
1b6fa695
ML
659/* We have an expression tree T that represents a call, either CALL_EXPR
660 or AGGR_INIT_EXPR. If the call is lexically to a named function,
661 retrun the _DECL for that function. */
662
663static tree
664get_function_named_in_call (tree t)
665{
666 tree fun = cp_get_callee (t);
667 if (fun && TREE_CODE (fun) == ADDR_EXPR
668 && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
669 fun = TREE_OPERAND (fun, 0);
670 return fun;
671}
672
2d76680f
PC
673/* Subroutine of register_constexpr_fundef. BODY is the body of a function
674 declared to be constexpr, or a sub-statement thereof. Returns the
675 return value if suitable, error_mark_node for a statement not allowed in
676 a constexpr function, or NULL_TREE if no return value was found. */
677
5afef8b1 678tree
2d76680f
PC
679constexpr_fn_retval (tree body)
680{
681 switch (TREE_CODE (body))
682 {
683 case STATEMENT_LIST:
684 {
685 tree_stmt_iterator i;
686 tree expr = NULL_TREE;
687 for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
688 {
689 tree s = constexpr_fn_retval (tsi_stmt (i));
690 if (s == error_mark_node)
691 return error_mark_node;
692 else if (s == NULL_TREE)
693 /* Keep iterating. */;
694 else if (expr)
695 /* Multiple return statements. */
696 return error_mark_node;
697 else
698 expr = s;
699 }
700 return expr;
701 }
702
703 case RETURN_EXPR:
704 return break_out_target_exprs (TREE_OPERAND (body, 0));
705
706 case DECL_EXPR:
0162cb3b
PC
707 {
708 tree decl = DECL_EXPR_DECL (body);
709 if (TREE_CODE (decl) == USING_DECL
710 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
711 || DECL_ARTIFICIAL (decl))
712 return NULL_TREE;
713 return error_mark_node;
714 }
2d76680f
PC
715
716 case CLEANUP_POINT_EXPR:
717 return constexpr_fn_retval (TREE_OPERAND (body, 0));
718
719 case BIND_EXPR:
720 if (!check_constexpr_bind_expr_vars (body))
721 return error_mark_node;
722 return constexpr_fn_retval (BIND_EXPR_BODY (body));
723
724 case USING_STMT:
96a95ac1 725 case DEBUG_BEGIN_STMT:
2d76680f
PC
726 return NULL_TREE;
727
1b6fa695
ML
728 case CALL_EXPR:
729 {
730 tree fun = get_function_named_in_call (body);
731 if (fun != NULL_TREE
3d78e008 732 && fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE))
1b6fa695
ML
733 return NULL_TREE;
734 }
735 /* Fallthru. */
736
2d76680f
PC
737 default:
738 return error_mark_node;
739 }
740}
741
742/* Subroutine of register_constexpr_fundef. BODY is the DECL_SAVED_TREE of
743 FUN; do the necessary transformations to turn it into a single expression
744 that we can store in the hash table. */
745
746static tree
747massage_constexpr_body (tree fun, tree body)
748{
749 if (DECL_CONSTRUCTOR_P (fun))
750 body = build_constexpr_constructor_member_initializers
751 (DECL_CONTEXT (fun), body);
60813a46 752 else if (cxx_dialect < cxx14)
2d76680f
PC
753 {
754 if (TREE_CODE (body) == EH_SPEC_BLOCK)
755 body = EH_SPEC_STMTS (body);
756 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
757 body = TREE_OPERAND (body, 0);
758 body = constexpr_fn_retval (body);
759 }
760 return body;
761}
762
3e4b91f2
NS
763/* CTYPE is a type constructed from BODY. Return true if some
764 bases/fields are uninitialized, and complain if COMPLAIN. */
2d76680f
PC
765
766static bool
3e4b91f2 767cx_check_missing_mem_inits (tree ctype, tree body, bool complain)
2d76680f 768{
3e4b91f2
NS
769 unsigned nelts = 0;
770
771 if (body)
772 {
773 if (TREE_CODE (body) != CONSTRUCTOR)
774 return false;
775 nelts = CONSTRUCTOR_NELTS (body);
776 }
777 tree field = TYPE_FIELDS (ctype);
2d76680f
PC
778
779 if (TREE_CODE (ctype) == UNION_TYPE)
780 {
781 if (nelts == 0 && next_initializable_field (field))
782 {
783 if (complain)
784 error ("%<constexpr%> constructor for union %qT must "
785 "initialize exactly one non-static data member", ctype);
786 return true;
787 }
788 return false;
789 }
790
3e4b91f2
NS
791 /* Iterate over the CONSTRUCTOR, checking any missing fields don't
792 need an explicit initialization. */
793 bool bad = false;
794 for (unsigned i = 0; i <= nelts; ++i)
2d76680f 795 {
3e4b91f2
NS
796 tree index = NULL_TREE;
797 if (i < nelts)
2d76680f
PC
798 {
799 index = CONSTRUCTOR_ELT (body, i)->index;
800 /* Skip base and vtable inits. */
801 if (TREE_CODE (index) != FIELD_DECL
802 || DECL_ARTIFICIAL (index))
803 continue;
804 }
3e4b91f2 805
2d76680f
PC
806 for (; field != index; field = DECL_CHAIN (field))
807 {
808 tree ftype;
3e4b91f2
NS
809 if (TREE_CODE (field) != FIELD_DECL)
810 continue;
7c30b12a 811 if (DECL_UNNAMED_BIT_FIELD (field))
2d76680f 812 continue;
3e4b91f2
NS
813 if (DECL_ARTIFICIAL (field))
814 continue;
815 if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
816 {
817 /* Recurse to check the anonummous aggregate member. */
818 bad |= cx_check_missing_mem_inits
819 (TREE_TYPE (field), NULL_TREE, complain);
820 if (bad && !complain)
821 return true;
822 continue;
823 }
2d76680f
PC
824 ftype = strip_array_types (TREE_TYPE (field));
825 if (type_has_constexpr_default_constructor (ftype))
826 {
827 /* It's OK to skip a member with a trivial constexpr ctor.
828 A constexpr ctor that isn't trivial should have been
829 added in by now. */
830 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
831 || errorcount != 0);
832 continue;
833 }
834 if (!complain)
835 return true;
097f82ec 836 auto_diagnostic_group d;
b8cd3996
JM
837 error ("member %qD must be initialized by mem-initializer "
838 "in %<constexpr%> constructor", field);
839 inform (DECL_SOURCE_LOCATION (field), "declared here");
2d76680f
PC
840 bad = true;
841 }
842 if (field == NULL_TREE)
843 break;
3e4b91f2
NS
844
845 if (ANON_AGGR_TYPE_P (TREE_TYPE (index)))
846 {
847 /* Check the anonymous aggregate initializer is valid. */
848 bad |= cx_check_missing_mem_inits
849 (TREE_TYPE (index), CONSTRUCTOR_ELT (body, i)->value, complain);
850 if (bad && !complain)
851 return true;
852 }
2d76680f
PC
853 field = DECL_CHAIN (field);
854 }
855
856 return bad;
857}
858
859/* We are processing the definition of the constexpr function FUN.
860 Check that its BODY fulfills the propriate requirements and
861 enter it in the constexpr function definition table.
862 For constructor BODY is actually the TREE_LIST of the
863 member-initializer list. */
864
865tree
866register_constexpr_fundef (tree fun, tree body)
867{
868 constexpr_fundef entry;
869 constexpr_fundef **slot;
870
871 if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun)))
872 return NULL;
873
ca30abcd
JM
874 tree massaged = massage_constexpr_body (fun, body);
875 if (massaged == NULL_TREE || massaged == error_mark_node)
2d76680f
PC
876 {
877 if (!DECL_CONSTRUCTOR_P (fun))
84fa214d
ML
878 error ("body of %<constexpr%> function %qD not a return-statement",
879 fun);
2d76680f
PC
880 return NULL;
881 }
882
ca30abcd 883 if (!potential_rvalue_constant_expression (massaged))
2d76680f
PC
884 {
885 if (!DECL_GENERATED_P (fun))
ca30abcd 886 require_potential_rvalue_constant_expression (massaged);
2d76680f
PC
887 return NULL;
888 }
889
890 if (DECL_CONSTRUCTOR_P (fun)
3e4b91f2
NS
891 && cx_check_missing_mem_inits (DECL_CONTEXT (fun),
892 massaged, !DECL_GENERATED_P (fun)))
2d76680f
PC
893 return NULL;
894
895 /* Create the constexpr function table if necessary. */
896 if (constexpr_fundef_table == NULL)
897 constexpr_fundef_table
898 = hash_table<constexpr_fundef_hasher>::create_ggc (101);
899
900 entry.decl = fun;
43574e4f
JJ
901 tree saved_fn = current_function_decl;
902 bool clear_ctx = false;
903 current_function_decl = fun;
904 if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE)
905 {
906 clear_ctx = true;
907 DECL_CONTEXT (DECL_RESULT (fun)) = fun;
908 }
909 entry.body = copy_fn (fun, entry.parms, entry.result);
910 current_function_decl = saved_fn;
2d76680f 911 slot = constexpr_fundef_table->find_slot (&entry, INSERT);
43574e4f
JJ
912 if (clear_ctx)
913 DECL_CONTEXT (DECL_RESULT (fun)) = NULL_TREE;
2d76680f
PC
914
915 gcc_assert (*slot == NULL);
916 *slot = ggc_alloc<constexpr_fundef> ();
917 **slot = entry;
918
919 return fun;
920}
921
922/* FUN is a non-constexpr function called in a context that requires a
923 constant expression. If it comes from a constexpr template, explain why
924 the instantiation isn't constexpr. */
925
926void
927explain_invalid_constexpr_fn (tree fun)
928{
929 static hash_set<tree> *diagnosed;
930 tree body;
931 location_t save_loc;
98e5a19a 932 /* Only diagnose defaulted functions, lambdas, or instantiations. */
2d76680f 933 if (!DECL_DEFAULTED_FN (fun)
98e5a19a 934 && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun))
2d76680f
PC
935 && !is_instantiation_of_constexpr (fun))
936 return;
937 if (diagnosed == NULL)
938 diagnosed = new hash_set<tree>;
939 if (diagnosed->add (fun))
940 /* Already explained. */
941 return;
942
943 save_loc = input_location;
98e5a19a
JM
944 if (!lambda_static_thunk_p (fun))
945 {
946 /* Diagnostics should completely ignore the static thunk, so leave
947 input_location set to our caller's location. */
948 input_location = DECL_SOURCE_LOCATION (fun);
949 inform (input_location,
84fa214d 950 "%qD is not usable as a %<constexpr%> function because:", fun);
98e5a19a 951 }
2d76680f
PC
952 /* First check the declaration. */
953 if (is_valid_constexpr_fn (fun, true))
954 {
955 /* Then if it's OK, the body. */
98e5a19a
JM
956 if (!DECL_DECLARED_CONSTEXPR_P (fun)
957 && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)))
2d76680f
PC
958 explain_implicit_non_constexpr (fun);
959 else
960 {
961 body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
962 require_potential_rvalue_constant_expression (body);
963 if (DECL_CONSTRUCTOR_P (fun))
3e4b91f2 964 cx_check_missing_mem_inits (DECL_CONTEXT (fun), body, true);
2d76680f
PC
965 }
966 }
967 input_location = save_loc;
968}
969
970/* Objects of this type represent calls to constexpr functions
971 along with the bindings of parameters to their arguments, for
972 the purpose of compile time evaluation. */
973
974struct GTY((for_user)) constexpr_call {
975 /* Description of the constexpr function definition. */
976 constexpr_fundef *fundef;
3c961dc7 977 /* Parameter bindings environment. A TREE_VEC of arguments. */
2d76680f
PC
978 tree bindings;
979 /* Result of the call.
980 NULL means the call is being evaluated.
981 error_mark_node means that the evaluation was erroneous;
982 otherwise, the actuall value of the call. */
983 tree result;
984 /* The hash of this call; we remember it here to avoid having to
985 recalculate it when expanding the hash table. */
986 hashval_t hash;
cce3ae91 987 /* Whether __builtin_is_constant_evaluated() should evaluate to true. */
13de99bc 988 bool manifestly_const_eval;
2d76680f
PC
989};
990
ca752f39 991struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call>
2d76680f
PC
992{
993 static hashval_t hash (constexpr_call *);
994 static bool equal (constexpr_call *, constexpr_call *);
3e605b20
JM
995};
996
4b390698
JJ
997enum constexpr_switch_state {
998 /* Used when processing a switch for the first time by cxx_eval_switch_expr
999 and default: label for that switch has not been seen yet. */
1000 css_default_not_seen,
1001 /* Used when processing a switch for the first time by cxx_eval_switch_expr
1002 and default: label for that switch has been seen already. */
1003 css_default_seen,
1004 /* Used when processing a switch for the second time by
1005 cxx_eval_switch_expr, where default: label should match. */
1006 css_default_processing
1007};
1008
3e605b20
JM
1009/* The constexpr expansion context. CALL is the current function
1010 expansion, CTOR is the current aggregate initializer, OBJECT is the
1011 object being initialized by CTOR, either a VAR_DECL or a _REF. VALUES
1012 is a map of values of variables initialized within the expression. */
1013
1014struct constexpr_ctx {
13f649f6 1015 /* The innermost call we're evaluating. */
3e605b20 1016 constexpr_call *call;
13f649f6
JM
1017 /* Values for any temporaries or local variables within the
1018 constant-expression. */
3e605b20 1019 hash_map<tree,tree> *values;
39dce2b7
JM
1020 /* SAVE_EXPRs that we've seen within the current LOOP_EXPR. NULL if we
1021 aren't inside a loop. */
0ee28590 1022 vec<tree> *save_exprs;
13f649f6
JM
1023 /* The CONSTRUCTOR we're currently building up for an aggregate
1024 initializer. */
3e605b20 1025 tree ctor;
13f649f6 1026 /* The object we're building the CONSTRUCTOR for. */
3e605b20 1027 tree object;
4b390698
JJ
1028 /* If inside SWITCH_EXPR. */
1029 constexpr_switch_state *css_state;
a15ffa22
JJ
1030 /* Number of cxx_eval_constant_expression calls (except skipped ones,
1031 on simple constants or location wrappers) encountered during current
1032 cxx_eval_outermost_constant_expr call. */
1033 HOST_WIDE_INT *constexpr_ops_count;
1034
13f649f6 1035 /* Whether we should error on a non-constant expression or fail quietly. */
2b3ab879 1036 bool quiet;
13f649f6
JM
1037 /* Whether we are strictly conforming to constant expression rules or
1038 trying harder to get a constant value. */
69eb4fde 1039 bool strict;
e4082611 1040 /* Whether __builtin_is_constant_evaluated () should be true. */
13de99bc 1041 bool manifestly_const_eval;
3e605b20 1042};
2d76680f
PC
1043
1044/* A table of all constexpr calls that have been evaluated by the
1045 compiler in this translation unit. */
1046
97f3003f 1047static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
2d76680f 1048
3e605b20 1049static tree cxx_eval_constant_expression (const constexpr_ctx *, tree,
5a804683 1050 bool, bool *, bool *, tree * = NULL);
2d76680f
PC
1051
1052/* Compute a hash value for a constexpr call representation. */
1053
1054inline hashval_t
1055constexpr_call_hasher::hash (constexpr_call *info)
1056{
1057 return info->hash;
1058}
1059
1060/* Return true if the objects pointed to by P and Q represent calls
1061 to the same constexpr function with the same arguments.
1062 Otherwise, return false. */
1063
1064bool
1065constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
1066{
2d76680f 1067 if (lhs == rhs)
46cf7fa1
JJ
1068 return true;
1069 if (lhs->hash != rhs->hash)
1070 return false;
13de99bc 1071 if (lhs->manifestly_const_eval != rhs->manifestly_const_eval)
cce3ae91 1072 return false;
2d76680f 1073 if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
46cf7fa1 1074 return false;
3c961dc7 1075 return cp_tree_equal (lhs->bindings, rhs->bindings);
2d76680f
PC
1076}
1077
1078/* Initialize the constexpr call table, if needed. */
1079
1080static void
1081maybe_initialize_constexpr_call_table (void)
1082{
1083 if (constexpr_call_table == NULL)
1084 constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
1085}
1086
c0daf32d
PP
1087/* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1088 a function happens to get called recursively, we unshare the callee
1089 function's body and evaluate this unshared copy instead of evaluating the
1090 original body.
1091
1092 FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1093 copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
97f3003f
JM
1094 that's keyed off of the original FUNCTION_DECL and whose value is a
1095 TREE_LIST of this function's unused copies awaiting reuse.
c0daf32d 1096
97f3003f 1097 This is not GC-deletable to avoid GC affecting UID generation. */
c0daf32d 1098
97f3003f 1099static GTY(()) hash_map<tree, tree> *fundef_copies_table;
c0daf32d
PP
1100
1101/* Initialize FUNDEF_COPIES_TABLE if it's not initialized. */
1102
1103static void
1104maybe_initialize_fundef_copies_table ()
1105{
97f3003f
JM
1106 if (fundef_copies_table == NULL)
1107 fundef_copies_table = hash_map<tree,tree>::create_ggc (101);
c0daf32d
PP
1108}
1109
1110/* Reuse a copy or create a new unshared copy of the function FUN.
3c98ff9b
NS
1111 Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1112 is parms, TYPE is result. */
c0daf32d 1113
97f3003f 1114static tree
43574e4f 1115get_fundef_copy (constexpr_fundef *fundef)
c0daf32d
PP
1116{
1117 maybe_initialize_fundef_copies_table ();
1118
97f3003f 1119 tree copy;
3c98ff9b 1120 bool existed;
43574e4f 1121 tree *slot = &fundef_copies_table->get_or_insert (fundef->decl, &existed);
3c98ff9b
NS
1122
1123 if (!existed)
c0daf32d 1124 {
3c98ff9b
NS
1125 /* There is no cached function available, or in use. We can use
1126 the function directly. That the slot is now created records
1127 that this function is now in use. */
43574e4f
JJ
1128 copy = build_tree_list (fundef->body, fundef->parms);
1129 TREE_TYPE (copy) = fundef->result;
3c98ff9b
NS
1130 }
1131 else if (*slot == NULL_TREE)
1132 {
1133 /* We've already used the function itself, so make a copy. */
97f3003f 1134 copy = build_tree_list (NULL, NULL);
43574e4f
JJ
1135 tree saved_body = DECL_SAVED_TREE (fundef->decl);
1136 tree saved_parms = DECL_ARGUMENTS (fundef->decl);
1137 tree saved_result = DECL_RESULT (fundef->decl);
1138 tree saved_fn = current_function_decl;
1139 DECL_SAVED_TREE (fundef->decl) = fundef->body;
1140 DECL_ARGUMENTS (fundef->decl) = fundef->parms;
1141 DECL_RESULT (fundef->decl) = fundef->result;
1142 current_function_decl = fundef->decl;
1143 TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy),
1144 TREE_TYPE (copy));
1145 current_function_decl = saved_fn;
1146 DECL_RESULT (fundef->decl) = saved_result;
1147 DECL_ARGUMENTS (fundef->decl) = saved_parms;
1148 DECL_SAVED_TREE (fundef->decl) = saved_body;
c0daf32d
PP
1149 }
1150 else
1151 {
3c98ff9b 1152 /* We have a cached function available. */
c0daf32d 1153 copy = *slot;
97f3003f 1154 *slot = TREE_CHAIN (copy);
c0daf32d
PP
1155 }
1156
1157 return copy;
1158}
1159
3c98ff9b
NS
1160/* Save the copy COPY of function FUN for later reuse by
1161 get_fundef_copy(). By construction, there will always be an entry
1162 to find. */
c0daf32d
PP
1163
1164static void
97f3003f 1165save_fundef_copy (tree fun, tree copy)
c0daf32d 1166{
3c98ff9b 1167 tree *slot = fundef_copies_table->get (fun);
97f3003f 1168 TREE_CHAIN (copy) = *slot;
c0daf32d
PP
1169 *slot = copy;
1170}
1171
2d76680f
PC
1172/* We have an expression tree T that represents a call, either CALL_EXPR
1173 or AGGR_INIT_EXPR. Return the Nth argument. */
1174
1175static inline tree
1176get_nth_callarg (tree t, int n)
1177{
1178 switch (TREE_CODE (t))
1179 {
1180 case CALL_EXPR:
1181 return CALL_EXPR_ARG (t, n);
1182
1183 case AGGR_INIT_EXPR:
1184 return AGGR_INIT_EXPR_ARG (t, n);
1185
1186 default:
1187 gcc_unreachable ();
1188 return NULL;
1189 }
1190}
1191
2d76680f
PC
1192/* Attempt to evaluate T which represents a call to a builtin function.
1193 We assume here that all builtin functions evaluate to scalar types
1194 represented by _CST nodes. */
1195
1196static tree
5756d0f9 1197cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun,
92a596e8 1198 bool lval,
2d76680f
PC
1199 bool *non_constant_p, bool *overflow_p)
1200{
1201 const int nargs = call_expr_nargs (t);
1202 tree *args = (tree *) alloca (nargs * sizeof (tree));
1203 tree new_call;
1204 int i;
5756d0f9
JM
1205
1206 /* Don't fold __builtin_constant_p within a constexpr function. */
e4082611 1207 bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun);
b925d25d 1208
fe736ffd
JM
1209 /* If we aren't requiring a constant expression, defer __builtin_constant_p
1210 in a constexpr function until we have values for the parameters. */
b925d25d 1211 if (bi_const_p
4cd3e7df 1212 && !ctx->manifestly_const_eval
5756d0f9
JM
1213 && current_function_decl
1214 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
2d76680f 1215 {
5756d0f9
JM
1216 *non_constant_p = true;
1217 return t;
2d76680f 1218 }
5756d0f9 1219
e4082611 1220 /* For __builtin_is_constant_evaluated, defer it if not
13de99bc 1221 ctx->manifestly_const_eval, otherwise fold it to true. */
3d78e008 1222 if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
13de99bc 1223 BUILT_IN_FRONTEND))
e4082611 1224 {
13de99bc 1225 if (!ctx->manifestly_const_eval)
e4082611
JJ
1226 {
1227 *non_constant_p = true;
1228 return t;
1229 }
1230 return boolean_true_node;
1231 }
1232
5756d0f9
JM
1233 /* Be permissive for arguments to built-ins; __builtin_constant_p should
1234 return constant false for a non-constant argument. */
1235 constexpr_ctx new_ctx = *ctx;
1236 new_ctx.quiet = true;
5756d0f9 1237 for (i = 0; i < nargs; ++i)
b925d25d 1238 {
2ff7172a
JJ
1239 args[i] = CALL_EXPR_ARG (t, i);
1240 /* If builtin_valid_in_constant_expr_p is true,
1241 potential_constant_expression_1 has not recursed into the arguments
1242 of the builtin, verify it here. */
1243 if (!builtin_valid_in_constant_expr_p (fun)
1244 || potential_constant_expression (args[i]))
4cd3e7df
JJ
1245 {
1246 bool dummy1 = false, dummy2 = false;
1247 args[i] = cxx_eval_constant_expression (&new_ctx, args[i], false,
1248 &dummy1, &dummy2);
1249 }
1250
b925d25d 1251 if (bi_const_p)
4cd3e7df 1252 /* For __builtin_constant_p, fold all expressions with constant values
b925d25d 1253 even if they aren't C++ constant-expressions. */
4cd3e7df 1254 args[i] = cp_fold_rvalue (args[i]);
b925d25d 1255 }
5756d0f9
JM
1256
1257 bool save_ffbcp = force_folding_builtin_constant_p;
1258 force_folding_builtin_constant_p = true;
43574e4f
JJ
1259 tree save_cur_fn = current_function_decl;
1260 /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
1261 if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION)
1262 && ctx->call
1263 && ctx->call->fundef)
1264 current_function_decl = ctx->call->fundef->decl;
109d2197
JJ
1265 new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t),
1266 CALL_EXPR_FN (t), nargs, args);
43574e4f 1267 current_function_decl = save_cur_fn;
5756d0f9 1268 force_folding_builtin_constant_p = save_ffbcp;
109d2197
JJ
1269 if (new_call == NULL)
1270 {
1271 if (!*non_constant_p && !ctx->quiet)
1272 {
1b6fa695
ML
1273 /* Do not allow__builtin_unreachable in constexpr function.
1274 The __builtin_unreachable call with BUILTINS_LOCATION
1275 comes from cp_maybe_instrument_return. */
3d78e008 1276 if (fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE)
1b6fa695 1277 && EXPR_LOCATION (t) == BUILTINS_LOCATION)
84fa214d 1278 error ("%<constexpr%> call flows off the end of the function");
1b6fa695
ML
1279 else
1280 {
1281 new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
1282 CALL_EXPR_FN (t), nargs, args);
1283 error ("%q+E is not a constant expression", new_call);
1284 }
109d2197
JJ
1285 }
1286 *non_constant_p = true;
1287 return t;
1288 }
1289
43574e4f 1290 if (!potential_constant_expression (new_call))
109d2197
JJ
1291 {
1292 if (!*non_constant_p && !ctx->quiet)
1293 error ("%q+E is not a constant expression", new_call);
1294 *non_constant_p = true;
1295 return t;
1296 }
1297
1298 return cxx_eval_constant_expression (&new_ctx, new_call, lval,
1299 non_constant_p, overflow_p);
2d76680f
PC
1300}
1301
1302/* TEMP is the constant value of a temporary object of type TYPE. Adjust
1303 the type of the value to match. */
1304
1305static tree
1306adjust_temp_type (tree type, tree temp)
1307{
47be9509 1308 if (same_type_p (TREE_TYPE (temp), type))
2d76680f
PC
1309 return temp;
1310 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1311 if (TREE_CODE (temp) == CONSTRUCTOR)
b27f74e7
MP
1312 {
1313 /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
1314 tree t = copy_node (temp);
1315 TREE_TYPE (t) = type;
1316 return t;
1317 }
ff8ba86f
JJ
1318 if (TREE_CODE (temp) == EMPTY_CLASS_EXPR)
1319 return build0 (EMPTY_CLASS_EXPR, type);
2d76680f 1320 gcc_assert (scalarish_type_p (type));
72b091f7
MP
1321 /* Now we know we're dealing with a scalar, and a prvalue of non-class
1322 type is cv-unqualified. */
1323 return cp_fold_convert (cv_unqualified (type), temp);
2d76680f
PC
1324}
1325
9b9eb42a
JM
1326/* If T is a CONSTRUCTOR, return an unshared copy of T and any
1327 sub-CONSTRUCTORs. Otherwise return T.
0146e25f 1328
9b9eb42a
JM
1329 We use this whenever we initialize an object as a whole, whether it's a
1330 parameter, a local variable, or a subobject, so that subsequent
1331 modifications don't affect other places where it was used. */
0146e25f 1332
1f6857ba 1333tree
3da7d774 1334unshare_constructor (tree t MEM_STAT_DECL)
0146e25f 1335{
9b9eb42a
JM
1336 if (!t || TREE_CODE (t) != CONSTRUCTOR)
1337 return t;
1338 auto_vec <tree*, 4> ptrs;
1339 ptrs.safe_push (&t);
1340 while (!ptrs.is_empty ())
1341 {
1342 tree *p = ptrs.pop ();
3da7d774
JM
1343 tree n = copy_node (*p PASS_MEM_STAT);
1344 CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT);
9b9eb42a
JM
1345 *p = n;
1346 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n);
1347 constructor_elt *ce;
1348 for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i)
1349 if (TREE_CODE (ce->value) == CONSTRUCTOR)
1350 ptrs.safe_push (&ce->value);
1351 }
0146e25f
PP
1352 return t;
1353}
1354
620adbec
JM
1355/* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
1356
1357static void
1358free_constructor (tree t)
1359{
1360 if (!t || TREE_CODE (t) != CONSTRUCTOR)
1361 return;
1362 releasing_vec ctors;
1363 vec_safe_push (ctors, t);
1364 while (!ctors->is_empty ())
1365 {
1366 tree c = ctors->pop ();
1367 if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c))
1368 {
1369 constructor_elt *ce;
1370 for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
1371 if (TREE_CODE (ce->value) == CONSTRUCTOR)
1372 vec_safe_push (ctors, ce->value);
1373 ggc_free (elts);
1374 }
1375 ggc_free (c);
1376 }
1377}
1378
2d76680f
PC
1379/* Subroutine of cxx_eval_call_expression.
1380 We are processing a call expression (either CALL_EXPR or
3e605b20 1381 AGGR_INIT_EXPR) in the context of CTX. Evaluate
2d76680f
PC
1382 all arguments and bind their values to correspondings
1383 parameters, making up the NEW_CALL context. */
1384
1385static void
3e605b20 1386cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t,
2d76680f 1387 constexpr_call *new_call,
12d9ce19
JM
1388 bool *non_constant_p, bool *overflow_p,
1389 bool *non_constant_args)
2d76680f
PC
1390{
1391 const int nargs = call_expr_nargs (t);
1392 tree fun = new_call->fundef->decl;
43574e4f 1393 tree parms = new_call->fundef->parms;
2d76680f 1394 int i;
3c961dc7
JM
1395 /* We don't record ellipsis args below. */
1396 int nparms = list_length (parms);
1397 int nbinds = nargs < nparms ? nargs : nparms;
1398 tree binds = new_call->bindings = make_tree_vec (nbinds);
2d76680f
PC
1399 for (i = 0; i < nargs; ++i)
1400 {
1401 tree x, arg;
1402 tree type = parms ? TREE_TYPE (parms) : void_type_node;
2d76680f 1403 x = get_nth_callarg (t, i);
3e605b20
JM
1404 /* For member function, the first argument is a pointer to the implied
1405 object. For a constructor, it might still be a dummy object, in
60813a46 1406 which case we get the real argument from ctx. */
3e605b20
JM
1407 if (i == 0 && DECL_CONSTRUCTOR_P (fun)
1408 && is_dummy_object (x))
1409 {
1410 x = ctx->object;
84dd815f 1411 x = build_address (x);
3e605b20 1412 }
43574e4f
JJ
1413 if (TREE_ADDRESSABLE (type))
1414 /* Undo convert_for_arg_passing work here. */
1415 x = convert_from_reference (x);
0e038601 1416 arg = cxx_eval_constant_expression (ctx, x, /*lval=*/false,
5a804683 1417 non_constant_p, overflow_p);
2d76680f 1418 /* Don't VERIFY_CONSTANT here. */
2b3ab879 1419 if (*non_constant_p && ctx->quiet)
2d76680f
PC
1420 return;
1421 /* Just discard ellipsis args after checking their constantitude. */
1422 if (!parms)
1423 continue;
4727d4c6
NS
1424
1425 if (!*non_constant_p)
1426 {
620adbec
JM
1427 /* Unsharing here isn't necessary for correctness, but it
1428 significantly improves memory performance for some reason. */
1dc23505 1429 arg = unshare_constructor (arg);
4727d4c6
NS
1430 /* Make sure the binding has the same type as the parm. But
1431 only for constant args. */
9f613f06 1432 if (!TYPE_REF_P (type))
4727d4c6
NS
1433 arg = adjust_temp_type (type, arg);
1434 if (!TREE_CONSTANT (arg))
1435 *non_constant_args = true;
3c961dc7 1436 TREE_VEC_ELT (binds, i) = arg;
4727d4c6 1437 }
2d76680f
PC
1438 parms = TREE_CHAIN (parms);
1439 }
1440}
1441
1442/* Variables and functions to manage constexpr call expansion context.
1443 These do not need to be marked for PCH or GC. */
1444
1445/* FIXME remember and print actual constant arguments. */
7de76362 1446static vec<tree> call_stack;
2d76680f
PC
1447static int call_stack_tick;
1448static int last_cx_error_tick;
1449
7ffc7de5 1450static int
2d76680f
PC
1451push_cx_call_context (tree call)
1452{
1453 ++call_stack_tick;
1454 if (!EXPR_HAS_LOCATION (call))
1455 SET_EXPR_LOCATION (call, input_location);
1456 call_stack.safe_push (call);
7ffc7de5
JM
1457 int len = call_stack.length ();
1458 if (len > max_constexpr_depth)
2d76680f 1459 return false;
7ffc7de5 1460 return len;
2d76680f
PC
1461}
1462
1463static void
1464pop_cx_call_context (void)
1465{
1466 ++call_stack_tick;
1467 call_stack.pop ();
1468}
1469
1470vec<tree>
1471cx_error_context (void)
1472{
1473 vec<tree> r = vNULL;
1474 if (call_stack_tick != last_cx_error_tick
1475 && !call_stack.is_empty ())
1476 r = call_stack;
1477 last_cx_error_tick = call_stack_tick;
1478 return r;
1479}
1480
44a845ca
MS
1481/* Evaluate a call T to a GCC internal function when possible and return
1482 the evaluated result or, under the control of CTX, give an error, set
1483 NON_CONSTANT_P, and return the unevaluated call T otherwise. */
1484
1485static tree
1486cxx_eval_internal_function (const constexpr_ctx *ctx, tree t,
1487 bool lval,
1488 bool *non_constant_p, bool *overflow_p)
1489{
1490 enum tree_code opcode = ERROR_MARK;
1491
1492 switch (CALL_EXPR_IFN (t))
1493 {
1494 case IFN_UBSAN_NULL:
1495 case IFN_UBSAN_BOUNDS:
1496 case IFN_UBSAN_VPTR:
81fea426 1497 case IFN_FALLTHROUGH:
44a845ca
MS
1498 return void_node;
1499
1500 case IFN_ADD_OVERFLOW:
1501 opcode = PLUS_EXPR;
1502 break;
1503 case IFN_SUB_OVERFLOW:
1504 opcode = MINUS_EXPR;
1505 break;
1506 case IFN_MUL_OVERFLOW:
1507 opcode = MULT_EXPR;
1508 break;
1509
e16f1cc7
JJ
1510 case IFN_LAUNDER:
1511 return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
1512 false, non_constant_p, overflow_p);
1513
d8fcab68
JJ
1514 case IFN_VEC_CONVERT:
1515 {
1516 tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
1517 false, non_constant_p,
1518 overflow_p);
1519 if (TREE_CODE (arg) == VECTOR_CST)
1520 return fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg);
1521 else
1522 {
1523 *non_constant_p = true;
1524 return t;
1525 }
1526 }
1527
44a845ca
MS
1528 default:
1529 if (!ctx->quiet)
6bdfada4 1530 error_at (cp_expr_loc_or_loc (t, input_location),
44a845ca
MS
1531 "call to internal function %qE", t);
1532 *non_constant_p = true;
1533 return t;
1534 }
1535
1536 /* Evaluate constant arguments using OPCODE and return a complex
1537 number containing the result and the overflow bit. */
1538 tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval,
1539 non_constant_p, overflow_p);
1540 tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval,
1541 non_constant_p, overflow_p);
1542
1543 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1544 {
6bdfada4 1545 location_t loc = cp_expr_loc_or_loc (t, input_location);
44a845ca
MS
1546 tree type = TREE_TYPE (TREE_TYPE (t));
1547 tree result = fold_binary_loc (loc, opcode, type,
1548 fold_convert_loc (loc, type, arg0),
1549 fold_convert_loc (loc, type, arg1));
1550 tree ovf
1551 = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1));
1552 /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
1553 if (TREE_OVERFLOW (result))
1554 TREE_OVERFLOW (result) = 0;
1555
1556 return build_complex (TREE_TYPE (t), result, ovf);
1557 }
1558
1559 *non_constant_p = true;
1560 return t;
1561}
1562
e8c48716 1563/* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
ecc57615
JM
1564
1565static void
1566clear_no_implicit_zero (tree ctor)
1567{
e8c48716 1568 if (CONSTRUCTOR_NO_CLEARING (ctor))
ecc57615 1569 {
e8c48716 1570 CONSTRUCTOR_NO_CLEARING (ctor) = false;
ecc57615
JM
1571 tree elt; unsigned HOST_WIDE_INT idx;
1572 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), idx, elt)
1573 if (TREE_CODE (elt) == CONSTRUCTOR)
1574 clear_no_implicit_zero (elt);
1575 }
1576}
1577
2d76680f
PC
1578/* Subroutine of cxx_eval_constant_expression.
1579 Evaluate the call expression tree T in the context of OLD_CALL expression
1580 evaluation. */
1581
1582static tree
3e605b20 1583cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
92a596e8 1584 bool lval,
2d76680f
PC
1585 bool *non_constant_p, bool *overflow_p)
1586{
6bdfada4 1587 location_t loc = cp_expr_loc_or_loc (t, input_location);
2d76680f 1588 tree fun = get_function_named_in_call (t);
cce3ae91 1589 constexpr_call new_call
13de99bc 1590 = { NULL, NULL, NULL, 0, ctx->manifestly_const_eval };
7ffc7de5 1591 int depth_ok;
2d76680f 1592
0b274c17 1593 if (fun == NULL_TREE)
44a845ca
MS
1594 return cxx_eval_internal_function (ctx, t, lval,
1595 non_constant_p, overflow_p);
0b274c17 1596
2d76680f
PC
1597 if (TREE_CODE (fun) != FUNCTION_DECL)
1598 {
1599 /* Might be a constexpr function pointer. */
2b3ab879 1600 fun = cxx_eval_constant_expression (ctx, fun,
92a596e8 1601 /*lval*/false, non_constant_p,
5a804683 1602 overflow_p);
2d76680f
PC
1603 STRIP_NOPS (fun);
1604 if (TREE_CODE (fun) == ADDR_EXPR)
1605 fun = TREE_OPERAND (fun, 0);
582d2481
JJ
1606 /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
1607 indirection, the called expression is a pointer into the
1608 virtual table which should contain FDESC_EXPR. Extract the
1609 FUNCTION_DECL from there. */
1610 else if (TARGET_VTABLE_USES_DESCRIPTORS
1611 && TREE_CODE (fun) == POINTER_PLUS_EXPR
1612 && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR
1613 && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST)
1614 {
1615 tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0);
1616 if (VAR_P (d)
1617 && DECL_VTABLE_OR_VTT_P (d)
1618 && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE
1619 && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type
1620 && DECL_INITIAL (d)
1621 && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR)
1622 {
1623 tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1),
1624 TYPE_SIZE_UNIT (vtable_entry_type));
1625 HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i);
1626 if (idx >= 0)
1627 {
1628 tree fdesc
1629 = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value;
1630 if (TREE_CODE (fdesc) == FDESC_EXPR
1631 && integer_zerop (TREE_OPERAND (fdesc, 1)))
1632 fun = TREE_OPERAND (fdesc, 0);
1633 }
1634 }
1635 }
2d76680f
PC
1636 }
1637 if (TREE_CODE (fun) != FUNCTION_DECL)
1638 {
2b3ab879 1639 if (!ctx->quiet && !*non_constant_p)
84fa214d 1640 error_at (loc, "expression %qE does not designate a %<constexpr%> "
2d76680f
PC
1641 "function", fun);
1642 *non_constant_p = true;
1643 return t;
1644 }
1645 if (DECL_CLONED_FUNCTION_P (fun))
1646 fun = DECL_CLONED_FUNCTION (fun);
0b274c17
MP
1647
1648 if (is_ubsan_builtin_p (fun))
1649 return void_node;
1650
3d78e008 1651 if (fndecl_built_in_p (fun))
5756d0f9 1652 return cxx_eval_builtin_function_call (ctx, t, fun,
92a596e8 1653 lval, non_constant_p, overflow_p);
2d76680f
PC
1654 if (!DECL_DECLARED_CONSTEXPR_P (fun))
1655 {
2b3ab879 1656 if (!ctx->quiet)
2d76680f 1657 {
11399477 1658 if (!lambda_static_thunk_p (fun))
84fa214d 1659 error_at (loc, "call to non-%<constexpr%> function %qD", fun);
2d76680f
PC
1660 explain_invalid_constexpr_fn (fun);
1661 }
1662 *non_constant_p = true;
1663 return t;
1664 }
1665
86461cad
JM
1666 constexpr_ctx new_ctx = *ctx;
1667 if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
1668 && TREE_CODE (t) == AGGR_INIT_EXPR)
1669 {
1670 /* We want to have an initialization target for an AGGR_INIT_EXPR.
1671 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
1672 new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
1673 tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
e8c48716 1674 CONSTRUCTOR_NO_CLEARING (ctor) = true;
86461cad
JM
1675 ctx->values->put (new_ctx.object, ctor);
1676 ctx = &new_ctx;
1677 }
1678
2d76680f
PC
1679 /* Shortcut trivial constructor/op=. */
1680 if (trivial_fn_p (fun))
1681 {
86461cad 1682 tree init = NULL_TREE;
2d76680f 1683 if (call_expr_nargs (t) == 2)
86461cad 1684 init = convert_from_reference (get_nth_callarg (t, 1));
2d76680f
PC
1685 else if (TREE_CODE (t) == AGGR_INIT_EXPR
1686 && AGGR_INIT_ZERO_FIRST (t))
86461cad
JM
1687 init = build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false);
1688 if (init)
1689 {
1690 tree op = get_nth_callarg (t, 0);
1691 if (is_dummy_object (op))
1692 op = ctx->object;
1693 else
1694 op = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (op)), op);
1695 tree set = build2 (MODIFY_EXPR, TREE_TYPE (op), op, init);
c8816908
JM
1696 new_ctx.call = &new_call;
1697 return cxx_eval_constant_expression (&new_ctx, set, lval,
86461cad
JM
1698 non_constant_p, overflow_p);
1699 }
2d76680f
PC
1700 }
1701
81371eff
JM
1702 /* We can't defer instantiating the function any longer. */
1703 if (!DECL_INITIAL (fun)
1704 && DECL_TEMPLOID_INSTANTIATION (fun))
1705 {
f065303f
JM
1706 location_t save_loc = input_location;
1707 input_location = loc;
81371eff
JM
1708 ++function_depth;
1709 instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
1710 --function_depth;
f065303f 1711 input_location = save_loc;
81371eff
JM
1712 }
1713
2d76680f 1714 /* If in direct recursive call, optimize definition search. */
c8816908 1715 if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun)
3e605b20 1716 new_call.fundef = ctx->call->fundef;
2d76680f
PC
1717 else
1718 {
1719 new_call.fundef = retrieve_constexpr_fundef (fun);
4ddcdbf9
JM
1720 if (new_call.fundef == NULL || new_call.fundef->body == NULL
1721 || fun == current_function_decl)
2d76680f 1722 {
2b3ab879 1723 if (!ctx->quiet)
2d76680f 1724 {
4ddcdbf9
JM
1725 /* We need to check for current_function_decl here in case we're
1726 being called during cp_fold_function, because at that point
1727 DECL_INITIAL is set properly and we have a fundef but we
1728 haven't lowered invisirefs yet (c++/70344). */
1729 if (DECL_INITIAL (fun) == error_mark_node
1730 || fun == current_function_decl)
ddd6d421
JM
1731 error_at (loc, "%qD called in a constant expression before its "
1732 "definition is complete", fun);
1733 else if (DECL_INITIAL (fun))
2d76680f 1734 {
98e5a19a
JM
1735 /* The definition of fun was somehow unsuitable. But pretend
1736 that lambda static thunks don't exist. */
1737 if (!lambda_static_thunk_p (fun))
1738 error_at (loc, "%qD called in a constant expression", fun);
2d76680f
PC
1739 explain_invalid_constexpr_fn (fun);
1740 }
1741 else
1742 error_at (loc, "%qD used before its definition", fun);
1743 }
1744 *non_constant_p = true;
1745 return t;
1746 }
1747 }
60813a46 1748
12d9ce19 1749 bool non_constant_args = false;
3e605b20 1750 cxx_bind_parameters_in_call (ctx, t, &new_call,
12d9ce19 1751 non_constant_p, overflow_p, &non_constant_args);
ecdcd560
JM
1752
1753 /* We build up the bindings list before we know whether we already have this
1754 call cached. If we don't end up saving these bindings, ggc_free them when
1755 this function exits. */
6c1dae73 1756 class free_bindings
ecdcd560 1757 {
6c1dae73 1758 public:
ecdcd560
JM
1759 tree &bindings;
1760 bool do_free;
1761 free_bindings (tree &b): bindings (b), do_free(true) { }
1762 void preserve () { do_free = false; }
1763 ~free_bindings () {
1764 if (do_free)
620adbec
JM
1765 {
1766 for (int i = 0; i < TREE_VEC_LENGTH (bindings); ++i)
1767 free_constructor (TREE_VEC_ELT (bindings, i));
1768 ggc_free (bindings);
1769 }
ecdcd560
JM
1770 }
1771 } fb (new_call.bindings);
1772
2d76680f
PC
1773 if (*non_constant_p)
1774 return t;
1775
1776 depth_ok = push_cx_call_context (t);
1777
12d9ce19 1778 tree result = NULL_TREE;
2d76680f 1779
12d9ce19 1780 constexpr_call *entry = NULL;
4a58d2fe 1781 if (depth_ok && !non_constant_args && ctx->strict)
2d76680f 1782 {
cce3ae91
JJ
1783 new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef);
1784 new_call.hash
1785 = iterative_hash_template_arg (new_call.bindings, new_call.hash);
1786 new_call.hash
13de99bc 1787 = iterative_hash_object (ctx->manifestly_const_eval, new_call.hash);
12d9ce19
JM
1788
1789 /* If we have seen this call before, we are done. */
1790 maybe_initialize_constexpr_call_table ();
1791 constexpr_call **slot
1792 = constexpr_call_table->find_slot (&new_call, INSERT);
1793 entry = *slot;
1794 if (entry == NULL)
1795 {
7ffc7de5
JM
1796 /* Only cache up to constexpr_cache_depth to limit memory use. */
1797 if (depth_ok < constexpr_cache_depth)
1798 {
1799 /* We need to keep a pointer to the entry, not just the slot, as
1800 the slot can move during evaluation of the body. */
1801 *slot = entry = ggc_alloc<constexpr_call> ();
1802 *entry = new_call;
1803 fb.preserve ();
1804 }
12d9ce19 1805 }
7ffc7de5
JM
1806 /* Calls that are in progress have their result set to NULL, so that we
1807 can detect circular dependencies. Now that we only cache up to
1808 constexpr_cache_depth this won't catch circular dependencies that
1809 start deeper, but they'll hit the recursion or ops limit. */
12d9ce19
JM
1810 else if (entry->result == NULL)
1811 {
1812 if (!ctx->quiet)
1813 error ("call has circular dependency");
1814 *non_constant_p = true;
1815 entry->result = result = error_mark_node;
1816 }
1817 else
1818 result = entry->result;
2d76680f
PC
1819 }
1820
1821 if (!depth_ok)
1822 {
2b3ab879 1823 if (!ctx->quiet)
84fa214d 1824 error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
7e7a6ed7 1825 "%<-fconstexpr-depth=%> to increase the maximum)",
2d76680f
PC
1826 max_constexpr_depth);
1827 *non_constant_p = true;
12d9ce19 1828 result = error_mark_node;
2d76680f
PC
1829 }
1830 else
1831 {
de54de93
JM
1832 if (result && result != error_mark_node)
1833 /* OK */;
1834 else if (!DECL_SAVED_TREE (fun))
1835 {
1836 /* When at_eof >= 2, cgraph has started throwing away
1837 DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
1838 late code generation for VEC_INIT_EXPR, which needs to be
1839 completely reconsidered. */
1840 gcc_assert (at_eof >= 2 && ctx->quiet);
1841 *non_constant_p = true;
1842 }
1843 else
3e605b20 1844 {
c0daf32d 1845 tree body, parms, res;
620adbec 1846 releasing_vec ctors;
0567dcd2 1847
c0daf32d 1848 /* Reuse or create a new unshared copy of this function's body. */
43574e4f 1849 tree copy = get_fundef_copy (new_call.fundef);
97f3003f
JM
1850 body = TREE_PURPOSE (copy);
1851 parms = TREE_VALUE (copy);
1852 res = TREE_TYPE (copy);
0567dcd2
MP
1853
1854 /* Associate the bindings with the remapped parms. */
1855 tree bound = new_call.bindings;
1856 tree remapped = parms;
3c961dc7 1857 for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
60813a46 1858 {
3c961dc7 1859 tree arg = TREE_VEC_ELT (bound, i);
7f0e23e9 1860 /* Don't share a CONSTRUCTOR that might be changed. */
0146e25f 1861 arg = unshare_constructor (arg);
620adbec
JM
1862 if (TREE_CODE (arg) == CONSTRUCTOR)
1863 vec_safe_push (ctors, arg);
0567dcd2 1864 ctx->values->put (remapped, arg);
0567dcd2 1865 remapped = DECL_CHAIN (remapped);
60813a46 1866 }
0567dcd2
MP
1867 /* Add the RESULT_DECL to the values map, too. */
1868 tree slot = NULL_TREE;
1869 if (DECL_BY_REFERENCE (res))
60813a46 1870 {
0567dcd2
MP
1871 slot = AGGR_INIT_EXPR_SLOT (t);
1872 tree addr = build_address (slot);
1873 addr = build_nop (TREE_TYPE (res), addr);
1874 ctx->values->put (res, addr);
1875 ctx->values->put (slot, NULL_TREE);
1876 }
1877 else
1878 ctx->values->put (res, NULL_TREE);
60813a46 1879
c0daf32d
PP
1880 /* Track the callee's evaluated SAVE_EXPRs so that we can forget
1881 their values after the call. */
1882 constexpr_ctx ctx_with_save_exprs = *ctx;
0ee28590 1883 auto_vec<tree, 10> save_exprs;
c0daf32d 1884 ctx_with_save_exprs.save_exprs = &save_exprs;
1e163090 1885 ctx_with_save_exprs.call = &new_call;
c0daf32d 1886
0567dcd2 1887 tree jump_target = NULL_TREE;
c0daf32d 1888 cxx_eval_constant_expression (&ctx_with_save_exprs, body,
0567dcd2
MP
1889 lval, non_constant_p, overflow_p,
1890 &jump_target);
60813a46 1891
0567dcd2
MP
1892 if (DECL_CONSTRUCTOR_P (fun))
1893 /* This can be null for a subobject constructor call, in
1894 which case what we care about is the initialization
1895 side-effects rather than the value. We could get at the
1896 value by evaluating *this, but we don't bother; there's
1897 no need to put such a call in the hash table. */
1898 result = lval ? ctx->object : ctx->ctor;
1899 else if (VOID_TYPE_P (TREE_TYPE (res)))
1900 result = void_node;
1901 else
1902 {
1903 result = *ctx->values->get (slot ? slot : res);
1904 if (result == NULL_TREE && !*non_constant_p)
60813a46 1905 {
0567dcd2 1906 if (!ctx->quiet)
84fa214d 1907 error ("%<constexpr%> call flows off the end "
0567dcd2
MP
1908 "of the function");
1909 *non_constant_p = true;
60813a46 1910 }
60813a46 1911 }
0567dcd2 1912
c0daf32d 1913 /* Forget the saved values of the callee's SAVE_EXPRs. */
0ee28590
JJ
1914 unsigned int i;
1915 tree save_expr;
1916 FOR_EACH_VEC_ELT (save_exprs, i, save_expr)
1917 ctx_with_save_exprs.values->remove (save_expr);
c0daf32d 1918
0567dcd2
MP
1919 /* Remove the parms/result from the values map. Is it worth
1920 bothering to do this when the map itself is only live for
1921 one constexpr evaluation? If so, maybe also clear out
1922 other vars from call, maybe in BIND_EXPR handling? */
1923 ctx->values->remove (res);
1924 if (slot)
1925 ctx->values->remove (slot);
1926 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
1927 ctx->values->remove (parm);
c0daf32d 1928
620adbec
JM
1929 /* Free any parameter CONSTRUCTORs we aren't returning directly. */
1930 while (!ctors->is_empty ())
1931 {
1932 tree c = ctors->pop ();
1933 if (c != result)
1934 free_constructor (c);
1935 }
1936
c0daf32d
PP
1937 /* Make the unshared function copy we used available for re-use. */
1938 save_fundef_copy (fun, copy);
3e605b20 1939 }
60813a46 1940
2d76680f
PC
1941 if (result == error_mark_node)
1942 *non_constant_p = true;
52228180 1943 if (*non_constant_p || *overflow_p)
12d9ce19 1944 result = error_mark_node;
0567dcd2 1945 else if (!result)
60813a46 1946 result = void_node;
12d9ce19
JM
1947 if (entry)
1948 entry->result = result;
2d76680f
PC
1949 }
1950
f64e0c02
JM
1951 /* The result of a constexpr function must be completely initialized. */
1952 if (TREE_CODE (result) == CONSTRUCTOR)
ecc57615 1953 clear_no_implicit_zero (result);
f64e0c02 1954
2d76680f 1955 pop_cx_call_context ();
9b9eb42a 1956 return result;
2d76680f
PC
1957}
1958
1959/* FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
1960
1961bool
1962reduced_constant_expression_p (tree t)
1963{
4930c53e
MP
1964 if (t == NULL_TREE)
1965 return false;
1966
2d76680f
PC
1967 switch (TREE_CODE (t))
1968 {
1969 case PTRMEM_CST:
1970 /* Even if we can't lower this yet, it's constant. */
1971 return true;
1972
1973 case CONSTRUCTOR:
1974 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
7368cfa4 1975 tree idx, val, field; unsigned HOST_WIDE_INT i;
e8c48716 1976 if (CONSTRUCTOR_NO_CLEARING (t))
6f11ddd8
JM
1977 {
1978 if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1979 /* An initialized vector would have a VECTOR_CST. */
1980 return false;
1981 else
1982 field = next_initializable_field (TYPE_FIELDS (TREE_TYPE (t)));
1983 }
7368cfa4
JM
1984 else
1985 field = NULL_TREE;
1986 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, idx, val)
125db6a1 1987 {
4930c53e
MP
1988 /* If VAL is null, we're in the middle of initializing this
1989 element. */
7368cfa4 1990 if (!reduced_constant_expression_p (val))
125db6a1 1991 return false;
7368cfa4
JM
1992 if (field)
1993 {
1994 if (idx != field)
1995 return false;
1996 field = next_initializable_field (DECL_CHAIN (field));
1997 }
125db6a1 1998 }
7368cfa4
JM
1999 if (field)
2000 return false;
e8c48716 2001 else if (CONSTRUCTOR_NO_CLEARING (t))
7368cfa4 2002 /* All the fields are initialized. */
e8c48716 2003 CONSTRUCTOR_NO_CLEARING (t) = false;
2d76680f
PC
2004 return true;
2005
2006 default:
2007 /* FIXME are we calling this too much? */
2008 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
2009 }
2010}
2011
2012/* Some expressions may have constant operands but are not constant
7d75ea04
JJ
2013 themselves, such as 1/0. Call this function to check for that
2014 condition.
2d76680f
PC
2015
2016 We only call this in places that require an arithmetic constant, not in
2017 places where we might have a non-constant expression that can be a
2018 component of a constant expression, such as the address of a constexpr
2019 variable that might be dereferenced later. */
2020
2021static bool
2022verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
2023 bool *overflow_p)
2024{
2025 if (!*non_constant_p && !reduced_constant_expression_p (t))
2026 {
2027 if (!allow_non_constant)
2028 error ("%q+E is not a constant expression", t);
2029 *non_constant_p = true;
2030 }
2031 if (TREE_OVERFLOW_P (t))
2032 {
2033 if (!allow_non_constant)
2034 {
2035 permerror (input_location, "overflow in constant expression");
2036 /* If we're being permissive (and are in an enforcing
2037 context), ignore the overflow. */
2038 if (flag_permissive)
2039 return *non_constant_p;
2040 }
2041 *overflow_p = true;
2042 }
2043 return *non_constant_p;
2044}
2045
253a921b
MP
2046/* Check whether the shift operation with code CODE and type TYPE on LHS
2047 and RHS is undefined. If it is, give an error with an explanation,
2048 and return true; return false otherwise. */
2049
2050static bool
2051cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
2052 enum tree_code code, tree type, tree lhs, tree rhs)
2053{
2054 if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
2055 || TREE_CODE (lhs) != INTEGER_CST
2056 || TREE_CODE (rhs) != INTEGER_CST)
2057 return false;
2058
2059 tree lhstype = TREE_TYPE (lhs);
2060 unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
2061
2062 /* [expr.shift] The behavior is undefined if the right operand
2063 is negative, or greater than or equal to the length in bits
2064 of the promoted left operand. */
2065 if (tree_int_cst_sgn (rhs) == -1)
2066 {
2067 if (!ctx->quiet)
5342156c
MP
2068 permerror (loc, "right operand of shift expression %q+E is negative",
2069 build2_loc (loc, code, type, lhs, rhs));
2070 return (!flag_permissive || ctx->quiet);
253a921b
MP
2071 }
2072 if (compare_tree_int (rhs, uprec) >= 0)
2073 {
2074 if (!ctx->quiet)
a9c697b8
MS
2075 permerror (loc, "right operand of shift expression %q+E is greater "
2076 "than or equal to the precision %wu of the left operand",
2077 build2_loc (loc, code, type, lhs, rhs), uprec);
5342156c 2078 return (!flag_permissive || ctx->quiet);
253a921b
MP
2079 }
2080
2081 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
2082 if E1 has a signed type and non-negative value, and E1x2^E2 is
2083 representable in the corresponding unsigned type of the result type,
2084 then that value, converted to the result type, is the resulting value;
8ee09943
JJ
2085 otherwise, the behavior is undefined.
2086 For C++2a:
2087 The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
2088 2^N, where N is the range exponent of the type of the result. */
2089 if (code == LSHIFT_EXPR
2090 && !TYPE_UNSIGNED (lhstype)
2091 && cxx_dialect >= cxx11
2092 && cxx_dialect < cxx2a)
253a921b
MP
2093 {
2094 if (tree_int_cst_sgn (lhs) == -1)
2095 {
2096 if (!ctx->quiet)
5342156c
MP
2097 permerror (loc,
2098 "left operand of shift expression %q+E is negative",
2099 build2_loc (loc, code, type, lhs, rhs));
2100 return (!flag_permissive || ctx->quiet);
253a921b
MP
2101 }
2102 /* For signed x << y the following:
2103 (unsigned) x >> ((prec (lhs) - 1) - y)
2104 if > 1, is undefined. The right-hand side of this formula
2105 is the highest bit of the LHS that can be set (starting from 0),
2106 so that the shift doesn't overflow. We then right-shift the LHS
2107 to see whether any other bit is set making the original shift
2108 undefined -- the result is not representable in the corresponding
2109 unsigned type. */
2110 tree t = build_int_cst (unsigned_type_node, uprec - 1);
2111 t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
2112 tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
2113 t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
2114 if (tree_int_cst_lt (integer_one_node, t))
2115 {
2116 if (!ctx->quiet)
5342156c
MP
2117 permerror (loc, "shift expression %q+E overflows",
2118 build2_loc (loc, code, type, lhs, rhs));
2119 return (!flag_permissive || ctx->quiet);
253a921b
MP
2120 }
2121 }
2122 return false;
2123}
2124
2d76680f
PC
2125/* Subroutine of cxx_eval_constant_expression.
2126 Attempt to reduce the unary expression tree T to a compile time value.
2127 If successful, return the value. Otherwise issue a diagnostic
2128 and return error_mark_node. */
2129
2130static tree
3e605b20 2131cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
12d9ce19 2132 bool /*lval*/,
2d76680f
PC
2133 bool *non_constant_p, bool *overflow_p)
2134{
2135 tree r;
2136 tree orig_arg = TREE_OPERAND (t, 0);
12d9ce19
JM
2137 tree arg = cxx_eval_constant_expression (ctx, orig_arg, /*lval*/false,
2138 non_constant_p, overflow_p);
2d76680f 2139 VERIFY_CONSTANT (arg);
f899317e
JM
2140 location_t loc = EXPR_LOCATION (t);
2141 enum tree_code code = TREE_CODE (t);
2142 tree type = TREE_TYPE (t);
2143 r = fold_unary_loc (loc, code, type, arg);
2144 if (r == NULL_TREE)
2145 {
2146 if (arg == orig_arg)
2147 r = t;
2148 else
2149 r = build1_loc (loc, code, type, arg);
2150 }
2d76680f
PC
2151 VERIFY_CONSTANT (r);
2152 return r;
2153}
2154
ea8661cd
JJ
2155/* Helper function for cxx_eval_binary_expression. Try to optimize
2156 original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
2157 generic folding should be used. */
2158
2159static tree
2160cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
2161 tree lhs, tree rhs, bool *non_constant_p,
2162 bool *overflow_p)
2163{
2164 STRIP_NOPS (lhs);
2165 if (TREE_CODE (lhs) != ADDR_EXPR)
2166 return NULL_TREE;
2167
2168 lhs = TREE_OPERAND (lhs, 0);
2169
2170 /* &A[i] p+ j => &A[i + j] */
2171 if (TREE_CODE (lhs) == ARRAY_REF
2172 && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST
2173 && TREE_CODE (rhs) == INTEGER_CST
2174 && TYPE_SIZE_UNIT (TREE_TYPE (lhs))
2175 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST)
2176 {
2177 tree orig_type = TREE_TYPE (t);
2178 location_t loc = EXPR_LOCATION (t);
2179 tree type = TREE_TYPE (lhs);
2180
2181 t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1));
2182 tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0)));
2183 nelts = cxx_eval_constant_expression (ctx, nelts, false, non_constant_p,
2184 overflow_p);
2185 if (*non_constant_p)
2186 return NULL_TREE;
2187 /* Don't fold an out-of-bound access. */
2188 if (!tree_int_cst_le (t, nelts))
2189 return NULL_TREE;
2190 rhs = cp_fold_convert (ssizetype, rhs);
2191 /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
2192 constexpr int A[1]; ... (char *)&A[0] + 1 */
2193 if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
2194 rhs, TYPE_SIZE_UNIT (type))))
2195 return NULL_TREE;
2196 /* Make sure to treat the second operand of POINTER_PLUS_EXPR
2197 as signed. */
2198 rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs,
2199 TYPE_SIZE_UNIT (type));
2200 t = size_binop_loc (loc, PLUS_EXPR, rhs, t);
2201 t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0),
2202 t, NULL_TREE, NULL_TREE);
2203 t = cp_build_addr_expr (t, tf_warning_or_error);
2204 t = cp_fold_convert (orig_type, t);
2205 return cxx_eval_constant_expression (ctx, t, /*lval*/false,
2206 non_constant_p, overflow_p);
2207 }
2208
2209 return NULL_TREE;
2210}
2211
2d76680f
PC
2212/* Subroutine of cxx_eval_constant_expression.
2213 Like cxx_eval_unary_expression, except for binary expressions. */
2214
2215static tree
3e605b20 2216cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
12d9ce19 2217 bool /*lval*/,
2d76680f
PC
2218 bool *non_constant_p, bool *overflow_p)
2219{
618d6c1c 2220 tree r = NULL_TREE;
2d76680f
PC
2221 tree orig_lhs = TREE_OPERAND (t, 0);
2222 tree orig_rhs = TREE_OPERAND (t, 1);
2223 tree lhs, rhs;
12d9ce19 2224 lhs = cxx_eval_constant_expression (ctx, orig_lhs, /*lval*/false,
5a804683 2225 non_constant_p, overflow_p);
c8a66fc9
JM
2226 /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
2227 subtraction. */
2228 if (*non_constant_p)
2229 return t;
12d9ce19 2230 rhs = cxx_eval_constant_expression (ctx, orig_rhs, /*lval*/false,
5a804683 2231 non_constant_p, overflow_p);
c8a66fc9
JM
2232 if (*non_constant_p)
2233 return t;
f899317e
JM
2234
2235 location_t loc = EXPR_LOCATION (t);
2236 enum tree_code code = TREE_CODE (t);
2237 tree type = TREE_TYPE (t);
618d6c1c
PP
2238
2239 if (code == EQ_EXPR || code == NE_EXPR)
2240 {
2241 bool is_code_eq = (code == EQ_EXPR);
2242
2243 if (TREE_CODE (lhs) == PTRMEM_CST
2244 && TREE_CODE (rhs) == PTRMEM_CST)
9b0607de
JM
2245 {
2246 tree lmem = PTRMEM_CST_MEMBER (lhs);
2247 tree rmem = PTRMEM_CST_MEMBER (rhs);
2248 bool eq;
2249 if (TREE_CODE (lmem) == TREE_CODE (rmem)
2250 && TREE_CODE (lmem) == FIELD_DECL
2251 && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE
2252 && same_type_p (DECL_CONTEXT (lmem),
2253 DECL_CONTEXT (rmem)))
2254 /* If both refer to (possibly different) members of the same union
2255 (12.3), they compare equal. */
2256 eq = true;
2257 else
2258 eq = cp_tree_equal (lhs, rhs);
2259 r = constant_boolean_node (eq == is_code_eq, type);
2260 }
618d6c1c
PP
2261 else if ((TREE_CODE (lhs) == PTRMEM_CST
2262 || TREE_CODE (rhs) == PTRMEM_CST)
2263 && (null_member_pointer_value_p (lhs)
2264 || null_member_pointer_value_p (rhs)))
2265 r = constant_boolean_node (!is_code_eq, type);
dd5dda56
JM
2266 else if (TREE_CODE (lhs) == PTRMEM_CST)
2267 lhs = cplus_expand_constant (lhs);
2268 else if (TREE_CODE (rhs) == PTRMEM_CST)
2269 rhs = cplus_expand_constant (rhs);
618d6c1c 2270 }
8bada5cd
MS
2271 if (code == POINTER_PLUS_EXPR && !*non_constant_p
2272 && integer_zerop (lhs) && !integer_zerop (rhs))
2273 {
2274 if (!ctx->quiet)
2275 error ("arithmetic involving a null pointer in %qE", lhs);
bf533db8 2276 *non_constant_p = true;
8bada5cd
MS
2277 return t;
2278 }
ea8661cd
JJ
2279 else if (code == POINTER_PLUS_EXPR)
2280 r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p,
2281 overflow_p);
618d6c1c
PP
2282
2283 if (r == NULL_TREE)
2284 r = fold_binary_loc (loc, code, type, lhs, rhs);
2285
f899317e
JM
2286 if (r == NULL_TREE)
2287 {
2288 if (lhs == orig_lhs && rhs == orig_rhs)
2289 r = t;
2290 else
2291 r = build2_loc (loc, code, type, lhs, rhs);
2292 }
253a921b
MP
2293 else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
2294 *non_constant_p = true;
c8a66fc9
JM
2295 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
2296 a local array in a constexpr function. */
71a93b08 2297 bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs));
caee690e 2298 if (!ptr)
134efa82 2299 VERIFY_CONSTANT (r);
2d76680f
PC
2300 return r;
2301}
2302
2303/* Subroutine of cxx_eval_constant_expression.
2304 Attempt to evaluate condition expressions. Dead branches are not
2305 looked into. */
2306
2307static tree
3e605b20 2308cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
92a596e8 2309 bool lval,
56632b27
JM
2310 bool *non_constant_p, bool *overflow_p,
2311 tree *jump_target)
2d76680f 2312{
3e605b20 2313 tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
12d9ce19 2314 /*lval*/false,
5a804683 2315 non_constant_p, overflow_p);
2d76680f
PC
2316 VERIFY_CONSTANT (val);
2317 /* Don't VERIFY_CONSTANT the other operands. */
2318 if (integer_zerop (val))
43574e4f
JJ
2319 val = TREE_OPERAND (t, 2);
2320 else
2321 val = TREE_OPERAND (t, 1);
2322 if (TREE_CODE (t) == IF_STMT && !val)
2323 val = void_node;
2324 return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
2325 overflow_p, jump_target);
2d76680f
PC
2326}
2327
f370e36d
JJ
2328/* Subroutine of cxx_eval_constant_expression.
2329 Attempt to evaluate vector condition expressions. Unlike
2330 cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
2331 ternary arithmetics operation, where all 3 arguments have to be
2332 evaluated as constants and then folding computes the result from
2333 them. */
2334
2335static tree
2336cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t,
2337 bool *non_constant_p, bool *overflow_p)
2338{
2339 tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
2340 /*lval*/false,
2341 non_constant_p, overflow_p);
2342 VERIFY_CONSTANT (arg1);
2343 tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
2344 /*lval*/false,
2345 non_constant_p, overflow_p);
2346 VERIFY_CONSTANT (arg2);
2347 tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
2348 /*lval*/false,
2349 non_constant_p, overflow_p);
2350 VERIFY_CONSTANT (arg3);
2351 location_t loc = EXPR_LOCATION (t);
2352 tree type = TREE_TYPE (t);
2353 tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
2354 if (r == NULL_TREE)
2355 {
2356 if (arg1 == TREE_OPERAND (t, 0)
2357 && arg2 == TREE_OPERAND (t, 1)
2358 && arg3 == TREE_OPERAND (t, 2))
2359 r = t;
2360 else
2361 r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
2362 }
2363 VERIFY_CONSTANT (r);
2364 return r;
2365}
2366
ceaaf873
JM
2367/* Returns less than, equal to, or greater than zero if KEY is found to be
2368 less than, to match, or to be greater than the constructor_elt's INDEX. */
2369
2370static int
2371array_index_cmp (tree key, tree index)
2372{
2373 gcc_assert (TREE_CODE (key) == INTEGER_CST);
2374
2375 switch (TREE_CODE (index))
2376 {
2377 case INTEGER_CST:
2378 return tree_int_cst_compare (key, index);
2379 case RANGE_EXPR:
2380 {
2381 tree lo = TREE_OPERAND (index, 0);
2382 tree hi = TREE_OPERAND (index, 1);
2383 if (tree_int_cst_lt (key, lo))
2384 return -1;
2385 else if (tree_int_cst_lt (hi, key))
2386 return 1;
2387 else
2388 return 0;
2389 }
2390 default:
2391 gcc_unreachable ();
2392 }
2393}
2394
2395/* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
2396 if none. If INSERT is true, insert a matching element rather than fail. */
2397
2398static HOST_WIDE_INT
582d2481 2399find_array_ctor_elt (tree ary, tree dindex, bool insert)
ceaaf873
JM
2400{
2401 if (tree_int_cst_sgn (dindex) < 0)
2402 return -1;
2403
2404 unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
2405 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
2406 unsigned HOST_WIDE_INT len = vec_safe_length (elts);
2407
2408 unsigned HOST_WIDE_INT end = len;
2409 unsigned HOST_WIDE_INT begin = 0;
2410
2411 /* If the last element of the CONSTRUCTOR has its own index, we can assume
2412 that the same is true of the other elements and index directly. */
2413 if (end > 0)
2414 {
fe217ba0 2415 tree cindex = (*elts)[end - 1].index;
ceaaf873 2416 if (TREE_CODE (cindex) == INTEGER_CST
fe217ba0 2417 && compare_tree_int (cindex, end - 1) == 0)
ceaaf873
JM
2418 {
2419 if (i < end)
2420 return i;
2421 else
2422 begin = end;
2423 }
2424 }
2425
2426 /* Otherwise, find a matching index by means of a binary search. */
2427 while (begin != end)
2428 {
2429 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
a7ccb9e7
JM
2430 constructor_elt &elt = (*elts)[middle];
2431 tree idx = elt.index;
ceaaf873 2432
a7ccb9e7 2433 int cmp = array_index_cmp (dindex, idx);
ceaaf873
JM
2434 if (cmp < 0)
2435 end = middle;
2436 else if (cmp > 0)
2437 begin = middle + 1;
2438 else
a7ccb9e7
JM
2439 {
2440 if (insert && TREE_CODE (idx) == RANGE_EXPR)
2441 {
2442 /* We need to split the range. */
2443 constructor_elt e;
2444 tree lo = TREE_OPERAND (idx, 0);
2445 tree hi = TREE_OPERAND (idx, 1);
fe217ba0
JJ
2446 tree value = elt.value;
2447 dindex = fold_convert (sizetype, dindex);
a7ccb9e7
JM
2448 if (tree_int_cst_lt (lo, dindex))
2449 {
2450 /* There are still some lower elts; shorten the range. */
2451 tree new_hi = int_const_binop (MINUS_EXPR, dindex,
2452 size_one_node);
2453 if (tree_int_cst_equal (lo, new_hi))
2454 /* Only one element left, no longer a range. */
2455 elt.index = lo;
2456 else
2457 TREE_OPERAND (idx, 1) = new_hi;
2458 /* Append the element we want to insert. */
2459 ++middle;
2460 e.index = dindex;
fe217ba0 2461 e.value = unshare_constructor (value);
a7ccb9e7
JM
2462 vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
2463 }
2464 else
2465 /* No lower elts, the range elt is now ours. */
2466 elt.index = dindex;
2467
2468 if (tree_int_cst_lt (dindex, hi))
2469 {
2470 /* There are still some higher elts; append a range. */
2471 tree new_lo = int_const_binop (PLUS_EXPR, dindex,
2472 size_one_node);
2473 if (tree_int_cst_equal (new_lo, hi))
2474 e.index = hi;
2475 else
2476 e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi);
fe217ba0
JJ
2477 e.value = unshare_constructor (value);
2478 vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
a7ccb9e7
JM
2479 }
2480 }
2481 return middle;
2482 }
ceaaf873
JM
2483 }
2484
2485 if (insert)
2486 {
2487 constructor_elt e = { dindex, NULL_TREE };
2488 vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
2489 return end;
2490 }
2491
2492 return -1;
2493}
2494
abdc16c8
MS
2495/* Under the control of CTX, issue a detailed diagnostic for
2496 an out-of-bounds subscript INDEX into the expression ARRAY. */
2497
2498static void
043666e0 2499diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index)
abdc16c8
MS
2500{
2501 if (!ctx->quiet)
2502 {
2503 tree arraytype = TREE_TYPE (array);
2504
2505 /* Convert the unsigned array subscript to a signed integer to avoid
2506 printing huge numbers for small negative values. */
2507 tree sidx = fold_convert (ssizetype, index);
043666e0 2508 STRIP_ANY_LOCATION_WRAPPER (array);
abdc16c8
MS
2509 if (DECL_P (array))
2510 {
08b3748c 2511 if (TYPE_DOMAIN (arraytype))
043666e0
JM
2512 error_at (loc, "array subscript value %qE is outside the bounds "
2513 "of array %qD of type %qT", sidx, array, arraytype);
08b3748c 2514 else
043666e0
JM
2515 error_at (loc, "nonzero array subscript %qE is used with array %qD of "
2516 "type %qT with unknown bounds", sidx, array, arraytype);
abdc16c8
MS
2517 inform (DECL_SOURCE_LOCATION (array), "declared here");
2518 }
08b3748c 2519 else if (TYPE_DOMAIN (arraytype))
043666e0
JM
2520 error_at (loc, "array subscript value %qE is outside the bounds "
2521 "of array type %qT", sidx, arraytype);
08b3748c 2522 else
043666e0
JM
2523 error_at (loc, "nonzero array subscript %qE is used with array of type %qT "
2524 "with unknown bounds", sidx, arraytype);
abdc16c8
MS
2525 }
2526}
ceaaf873 2527
74f8705e
MP
2528/* Return the number of elements for TYPE (which is an ARRAY_TYPE or
2529 a VECTOR_TYPE). */
2530
2531static tree
2532get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type,
2533 bool *non_constant_p, bool *overflow_p)
2534{
2535 tree nelts;
2536 if (TREE_CODE (type) == ARRAY_TYPE)
2537 {
2538 if (TYPE_DOMAIN (type))
2539 nelts = array_type_nelts_top (type);
2540 else
2541 nelts = size_zero_node;
2542 }
2543 else if (VECTOR_TYPE_P (type))
2544 nelts = size_int (TYPE_VECTOR_SUBPARTS (type));
2545 else
2546 gcc_unreachable ();
2547
2548 /* For VLAs, the number of elements won't be an integer constant. */
2549 nelts = cxx_eval_constant_expression (ctx, nelts, false,
2550 non_constant_p, overflow_p);
2551 return nelts;
2552}
2553
d6b46fca
NS
2554/* Extract element INDEX consisting of CHARS_PER_ELT chars from
2555 STRING_CST STRING. */
2556
2557static tree
2558extract_string_elt (tree string, unsigned chars_per_elt, unsigned index)
2559{
2560 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string)));
2561 tree r;
2562
2563 if (chars_per_elt == 1)
2564 r = build_int_cst (type, TREE_STRING_POINTER (string)[index]);
2565 else
2566 {
2567 const unsigned char *ptr
2568 = ((const unsigned char *)TREE_STRING_POINTER (string)
2569 + index * chars_per_elt);
2570 r = native_interpret_expr (type, ptr, chars_per_elt);
2571 }
2572 return r;
2573}
2574
043666e0
JM
2575/* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
2576 subscript, diagnose any problems with it, and return the result. */
2577
2578static tree
2579eval_and_check_array_index (const constexpr_ctx *ctx,
2580 tree t, bool allow_one_past,
2581 bool *non_constant_p, bool *overflow_p)
2582{
2583 location_t loc = cp_expr_loc_or_loc (t, input_location);
2584 tree ary = TREE_OPERAND (t, 0);
2585 t = TREE_OPERAND (t, 1);
2586 tree index = cxx_eval_constant_expression (ctx, t, false,
2587 non_constant_p, overflow_p);
2588 VERIFY_CONSTANT (index);
2589
2590 if (!tree_fits_shwi_p (index)
2591 || tree_int_cst_sgn (index) < 0)
2592 {
2593 diag_array_subscript (loc, ctx, ary, index);
2594 *non_constant_p = true;
2595 return t;
2596 }
2597
2598 tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p,
2599 overflow_p);
2600 VERIFY_CONSTANT (nelts);
2601 if (allow_one_past
2602 ? !tree_int_cst_le (index, nelts)
2603 : !tree_int_cst_lt (index, nelts))
2604 {
2605 diag_array_subscript (loc, ctx, ary, index);
2606 *non_constant_p = true;
2607 return t;
2608 }
2609
2610 return index;
2611}
2612
2d76680f
PC
2613/* Subroutine of cxx_eval_constant_expression.
2614 Attempt to reduce a reference to an array slot. */
2615
2616static tree
3e605b20 2617cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
92a596e8 2618 bool lval,
2d76680f
PC
2619 bool *non_constant_p, bool *overflow_p)
2620{
2621 tree oldary = TREE_OPERAND (t, 0);
3e605b20 2622 tree ary = cxx_eval_constant_expression (ctx, oldary,
92a596e8 2623 lval,
5a804683 2624 non_constant_p, overflow_p);
2d76680f
PC
2625 if (*non_constant_p)
2626 return t;
bc2687dd
JJ
2627 if (!lval
2628 && TREE_CODE (ary) == VIEW_CONVERT_EXPR
043666e0
JM
2629 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0)))
2630 && TREE_TYPE (t) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0))))
2631 ary = TREE_OPERAND (ary, 0);
3b9997bb 2632
043666e0
JM
2633 tree oldidx = TREE_OPERAND (t, 1);
2634 tree index = eval_and_check_array_index (ctx, t, lval,
2635 non_constant_p, overflow_p);
2636 if (*non_constant_p)
2637 return t;
5453bfed 2638
bc2a38df
JJ
2639 if (lval && ary == oldary && index == oldidx)
2640 return t;
2641 else if (lval)
2642 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
2643
043666e0
JM
2644 unsigned len = 0, elem_nchars = 1;
2645 tree elem_type = TREE_TYPE (TREE_TYPE (ary));
2646 if (TREE_CODE (ary) == CONSTRUCTOR)
2647 len = CONSTRUCTOR_NELTS (ary);
2648 else if (TREE_CODE (ary) == STRING_CST)
2649 {
2650 elem_nchars = (TYPE_PRECISION (elem_type)
2651 / TYPE_PRECISION (char_type_node));
2652 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
2653 }
2654 else if (TREE_CODE (ary) == VECTOR_CST)
2655 /* We don't create variable-length VECTOR_CSTs. */
2656 len = VECTOR_CST_NELTS (ary).to_constant ();
2657 else
2658 {
2659 /* We can't do anything with other tree codes, so use
2660 VERIFY_CONSTANT to complain and fail. */
2661 VERIFY_CONSTANT (ary);
2662 gcc_unreachable ();
2663 }
2664
ceaaf873 2665 bool found;
043666e0 2666 HOST_WIDE_INT i = 0;
ceaaf873
JM
2667 if (TREE_CODE (ary) == CONSTRUCTOR)
2668 {
2669 HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
2670 found = (ix >= 0);
2671 if (found)
2672 i = ix;
3b9997bb 2673 }
ceaaf873 2674 else
043666e0
JM
2675 {
2676 i = tree_to_shwi (index);
2677 found = (i < len);
2678 }
3b9997bb 2679
fd2bfee5
JM
2680 if (found)
2681 {
2682 tree r;
2683 if (TREE_CODE (ary) == CONSTRUCTOR)
2684 r = (*CONSTRUCTOR_ELTS (ary))[i].value;
2685 else if (TREE_CODE (ary) == VECTOR_CST)
2686 r = VECTOR_CST_ELT (ary, i);
fd2bfee5 2687 else
d6b46fca
NS
2688 r = extract_string_elt (ary, elem_nchars, i);
2689
fd2bfee5
JM
2690 if (r)
2691 /* Don't VERIFY_CONSTANT here. */
2692 return r;
2d76680f 2693
fd2bfee5 2694 /* Otherwise the element doesn't have a value yet. */
2d76680f 2695 }
3b9997bb 2696
fd2bfee5
JM
2697 /* Not found. */
2698
2699 if (TREE_CODE (ary) == CONSTRUCTOR
e8c48716 2700 && CONSTRUCTOR_NO_CLEARING (ary))
2d76680f 2701 {
fd2bfee5
JM
2702 /* 'ary' is part of the aggregate initializer we're currently
2703 building; if there's no initializer for this element yet,
2704 that's an error. */
2705 if (!ctx->quiet)
2706 error ("accessing uninitialized array element");
2707 *non_constant_p = true;
2708 return t;
2d76680f 2709 }
fd2bfee5
JM
2710
2711 /* If it's within the array bounds but doesn't have an explicit
2712 initializer, it's value-initialized. */
2713 tree val = build_value_init (elem_type, tf_warning_or_error);
2714 return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
2715 overflow_p);
2d76680f
PC
2716}
2717
2718/* Subroutine of cxx_eval_constant_expression.
2719 Attempt to reduce a field access of a value of class type. */
2720
2721static tree
3e605b20 2722cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
92a596e8 2723 bool lval,
2d76680f
PC
2724 bool *non_constant_p, bool *overflow_p)
2725{
2726 unsigned HOST_WIDE_INT i;
2727 tree field;
2728 tree value;
2729 tree part = TREE_OPERAND (t, 1);
2730 tree orig_whole = TREE_OPERAND (t, 0);
3e605b20 2731 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
92a596e8 2732 lval,
5a804683 2733 non_constant_p, overflow_p);
a7f8415c 2734 if (INDIRECT_REF_P (whole)
bf533db8
JJ
2735 && integer_zerop (TREE_OPERAND (whole, 0)))
2736 {
2737 if (!ctx->quiet)
2738 error ("dereferencing a null pointer in %qE", orig_whole);
2739 *non_constant_p = true;
2740 return t;
2741 }
8bada5cd 2742
dcdbc004
JM
2743 if (TREE_CODE (whole) == PTRMEM_CST)
2744 whole = cplus_expand_constant (whole);
2d76680f
PC
2745 if (whole == orig_whole)
2746 return t;
92a596e8 2747 if (lval)
2d76680f
PC
2748 return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
2749 whole, part, NULL_TREE);
2750 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
2751 CONSTRUCTOR. */
2752 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
2753 {
2b3ab879 2754 if (!ctx->quiet)
2d76680f
PC
2755 error ("%qE is not a constant expression", orig_whole);
2756 *non_constant_p = true;
2757 }
2758 if (DECL_MUTABLE_P (part))
2759 {
2b3ab879 2760 if (!ctx->quiet)
2d76680f
PC
2761 error ("mutable %qD is not usable in a constant expression", part);
2762 *non_constant_p = true;
2763 }
2764 if (*non_constant_p)
2765 return t;
2db613e5 2766 bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole));
2d76680f
PC
2767 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
2768 {
2db613e5
JM
2769 /* Use name match for PMF fields, as a variant will have a
2770 different FIELD_DECL with a different type. */
2771 if (pmf ? DECL_NAME (field) == DECL_NAME (part)
2772 : field == part)
60813a46
JM
2773 {
2774 if (value)
2775 return value;
2776 else
2777 /* We're in the middle of initializing it. */
2778 break;
2779 }
2d76680f
PC
2780 }
2781 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
2782 && CONSTRUCTOR_NELTS (whole) > 0)
2783 {
2784 /* DR 1188 says we don't have to deal with this. */
2b3ab879 2785 if (!ctx->quiet)
2d76680f
PC
2786 error ("accessing %qD member instead of initialized %qD member in "
2787 "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index);
2788 *non_constant_p = true;
2789 return t;
2790 }
2791
188e53bd
JM
2792 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
2793 classes never get represented; throw together a value now. */
dbcd32f8 2794 if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
188e53bd
JM
2795 return build_constructor (TREE_TYPE (t), NULL);
2796
2db613e5
JM
2797 gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole)));
2798
e8c48716 2799 if (CONSTRUCTOR_NO_CLEARING (whole))
3e605b20
JM
2800 {
2801 /* 'whole' is part of the aggregate initializer we're currently
2802 building; if there's no initializer for this member yet, that's an
188e53bd 2803 error. */
2b3ab879 2804 if (!ctx->quiet)
3e605b20
JM
2805 error ("accessing uninitialized member %qD", part);
2806 *non_constant_p = true;
2807 return t;
2808 }
2809
2d76680f
PC
2810 /* If there's no explicit init for this field, it's value-initialized. */
2811 value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
3e605b20 2812 return cxx_eval_constant_expression (ctx, value,
92a596e8 2813 lval,
5a804683 2814 non_constant_p, overflow_p);
2d76680f
PC
2815}
2816
2817/* Subroutine of cxx_eval_constant_expression.
2818 Attempt to reduce a field access of a value of class type that is
2819 expressed as a BIT_FIELD_REF. */
2820
2821static tree
3e605b20 2822cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
92a596e8 2823 bool lval,
2d76680f
PC
2824 bool *non_constant_p, bool *overflow_p)
2825{
2826 tree orig_whole = TREE_OPERAND (t, 0);
2827 tree retval, fldval, utype, mask;
2828 bool fld_seen = false;
2829 HOST_WIDE_INT istart, isize;
3e605b20 2830 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
92a596e8 2831 lval,
5a804683 2832 non_constant_p, overflow_p);
2d76680f
PC
2833 tree start, field, value;
2834 unsigned HOST_WIDE_INT i;
2835
2836 if (whole == orig_whole)
2837 return t;
2838 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
2839 CONSTRUCTOR. */
2840 if (!*non_constant_p
2841 && TREE_CODE (whole) != VECTOR_CST
2842 && TREE_CODE (whole) != CONSTRUCTOR)
2843 {
2b3ab879 2844 if (!ctx->quiet)
2d76680f
PC
2845 error ("%qE is not a constant expression", orig_whole);
2846 *non_constant_p = true;
2847 }
2848 if (*non_constant_p)
2849 return t;
2850
2851 if (TREE_CODE (whole) == VECTOR_CST)
2852 return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
2853 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
2854
2855 start = TREE_OPERAND (t, 2);
2856 istart = tree_to_shwi (start);
2857 isize = tree_to_shwi (TREE_OPERAND (t, 1));
2858 utype = TREE_TYPE (t);
2859 if (!TYPE_UNSIGNED (utype))
2860 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
2861 retval = build_int_cst (utype, 0);
2862 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
2863 {
2864 tree bitpos = bit_position (field);
2865 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
2866 return value;
2867 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
2868 && TREE_CODE (value) == INTEGER_CST
2869 && tree_fits_shwi_p (bitpos)
2870 && tree_fits_shwi_p (DECL_SIZE (field)))
2871 {
2872 HOST_WIDE_INT bit = tree_to_shwi (bitpos);
2873 HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
2874 HOST_WIDE_INT shift;
2875 if (bit >= istart && bit + sz <= istart + isize)
2876 {
2877 fldval = fold_convert (utype, value);
2878 mask = build_int_cst_type (utype, -1);
2879 mask = fold_build2 (LSHIFT_EXPR, utype, mask,
2880 size_int (TYPE_PRECISION (utype) - sz));
2881 mask = fold_build2 (RSHIFT_EXPR, utype, mask,
2882 size_int (TYPE_PRECISION (utype) - sz));
2883 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
2884 shift = bit - istart;
2885 if (BYTES_BIG_ENDIAN)
2886 shift = TYPE_PRECISION (utype) - shift - sz;
2887 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
2888 size_int (shift));
2889 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
2890 fld_seen = true;
2891 }
2892 }
2893 }
2894 if (fld_seen)
2895 return fold_convert (TREE_TYPE (t), retval);
2896 gcc_unreachable ();
2897 return error_mark_node;
2898}
2899
2900/* Subroutine of cxx_eval_constant_expression.
2901 Evaluate a short-circuited logical expression T in the context
2902 of a given constexpr CALL. BAILOUT_VALUE is the value for
2903 early return. CONTINUE_VALUE is used here purely for
2904 sanity check purposes. */
2905
2906static tree
3e605b20 2907cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
2d76680f 2908 tree bailout_value, tree continue_value,
92a596e8 2909 bool lval,
2d76680f
PC
2910 bool *non_constant_p, bool *overflow_p)
2911{
2912 tree r;
3e605b20 2913 tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
92a596e8 2914 lval,
5a804683 2915 non_constant_p, overflow_p);
2d76680f
PC
2916 VERIFY_CONSTANT (lhs);
2917 if (tree_int_cst_equal (lhs, bailout_value))
2918 return lhs;
2919 gcc_assert (tree_int_cst_equal (lhs, continue_value));
3e605b20 2920 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
92a596e8 2921 lval, non_constant_p,
5a804683 2922 overflow_p);
2d76680f
PC
2923 VERIFY_CONSTANT (r);
2924 return r;
2925}
2926
2927/* REF is a COMPONENT_REF designating a particular field. V is a vector of
2928 CONSTRUCTOR elements to initialize (part of) an object containing that
2929 field. Return a pointer to the constructor_elt corresponding to the
2930 initialization of the field. */
2931
2932static constructor_elt *
2933base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
2934{
2935 tree aggr = TREE_OPERAND (ref, 0);
2936 tree field = TREE_OPERAND (ref, 1);
2937 HOST_WIDE_INT i;
2938 constructor_elt *ce;
2939
2940 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
2941
2942 if (TREE_CODE (aggr) == COMPONENT_REF)
2943 {
2944 constructor_elt *base_ce
2945 = base_field_constructor_elt (v, aggr);
2946 v = CONSTRUCTOR_ELTS (base_ce->value);
2947 }
2948
2949 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
2950 if (ce->index == field)
2951 return ce;
2952
2953 gcc_unreachable ();
2954 return NULL;
2955}
2956
3e605b20
JM
2957/* Some of the expressions fed to the constexpr mechanism are calls to
2958 constructors, which have type void. In that case, return the type being
2959 initialized by the constructor. */
2960
2961static tree
2962initialized_type (tree t)
2963{
2964 if (TYPE_P (t))
2965 return t;
5dab8b11 2966 tree type = TREE_TYPE (t);
4d0c18c6 2967 if (TREE_CODE (t) == CALL_EXPR)
3e605b20
JM
2968 {
2969 /* A constructor call has void type, so we need to look deeper. */
2970 tree fn = get_function_named_in_call (t);
2971 if (fn && TREE_CODE (fn) == FUNCTION_DECL
2972 && DECL_CXX_CONSTRUCTOR_P (fn))
2973 type = DECL_CONTEXT (fn);
2974 }
4d0c18c6
JM
2975 else if (TREE_CODE (t) == COMPOUND_EXPR)
2976 return initialized_type (TREE_OPERAND (t, 1));
5dab8b11
JM
2977 else if (TREE_CODE (t) == AGGR_INIT_EXPR)
2978 type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t));
2979 return cv_unqualified (type);
3e605b20
JM
2980}
2981
2982/* We're about to initialize element INDEX of an array or class from VALUE.
2983 Set up NEW_CTX appropriately by adjusting .object to refer to the
2984 subobject and creating a new CONSTRUCTOR if the element is itself
2985 a class or array. */
2986
2987static void
2988init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
2989 tree index, tree &value)
2990{
2991 new_ctx = *ctx;
2992
2993 if (index && TREE_CODE (index) != INTEGER_CST
2994 && TREE_CODE (index) != FIELD_DECL)
2995 /* This won't have an element in the new CONSTRUCTOR. */
2996 return;
2997
2998 tree type = initialized_type (value);
2999 if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
3000 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
3001 return;
3002
3003 /* The sub-aggregate initializer might contain a placeholder;
3004 update object to refer to the subobject and ctor to refer to
3005 the (newly created) sub-initializer. */
3006 if (ctx->object)
3007 new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
3008 tree elt = build_constructor (type, NULL);
e8c48716 3009 CONSTRUCTOR_NO_CLEARING (elt) = true;
3e605b20
JM
3010 new_ctx.ctor = elt;
3011
3012 if (TREE_CODE (value) == TARGET_EXPR)
3013 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
3014 value = TARGET_EXPR_INITIAL (value);
3015}
3016
3017/* We're about to process an initializer for a class or array TYPE. Make
3018 sure that CTX is set up appropriately. */
3019
3020static void
3021verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
3022{
3023 /* We don't bother building a ctor for an empty base subobject. */
3024 if (is_empty_class (type))
3025 return;
3026
3027 /* We're in the middle of an initializer that might involve placeholders;
3028 our caller should have created a CONSTRUCTOR for us to put the
3029 initializer into. We will either return that constructor or T. */
3030 gcc_assert (ctx->ctor);
3031 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3032 (type, TREE_TYPE (ctx->ctor)));
fe69277d
JM
3033 /* We used to check that ctx->ctor was empty, but that isn't the case when
3034 the object is zero-initialized before calling the constructor. */
3e605b20 3035 if (ctx->object)
176e79b5
JM
3036 {
3037 tree otype = TREE_TYPE (ctx->object);
3038 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype)
3039 /* Handle flexible array members. */
3040 || (TREE_CODE (otype) == ARRAY_TYPE
3041 && TYPE_DOMAIN (otype) == NULL_TREE
3042 && TREE_CODE (type) == ARRAY_TYPE
3043 && (same_type_ignoring_top_level_qualifiers_p
3044 (TREE_TYPE (type), TREE_TYPE (otype)))));
3045 }
3e605b20
JM
3046 gcc_assert (!ctx->object || !DECL_P (ctx->object)
3047 || *(ctx->values->get (ctx->object)) == ctx->ctor);
3048}
3049
2d76680f
PC
3050/* Subroutine of cxx_eval_constant_expression.
3051 The expression tree T denotes a C-style array or a C-style
3052 aggregate. Reduce it to a constant expression. */
3053
3054static tree
3e605b20 3055cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
92a596e8 3056 bool lval,
2d76680f
PC
3057 bool *non_constant_p, bool *overflow_p)
3058{
3059 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
2d76680f
PC
3060 bool changed = false;
3061 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
8a29084d 3062 tree type = TREE_TYPE (t);
3e605b20 3063
8a29084d 3064 constexpr_ctx new_ctx;
d4619dc1 3065 if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type))
8a29084d 3066 {
d4619dc1
NS
3067 /* We don't really need the ctx->ctor business for a PMF or
3068 vector, but it's simpler to use the same code. */
8a29084d
JM
3069 new_ctx = *ctx;
3070 new_ctx.ctor = build_constructor (type, NULL);
3071 new_ctx.object = NULL_TREE;
3072 ctx = &new_ctx;
3073 };
3074 verify_ctor_sanity (ctx, type);
3e605b20
JM
3075 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
3076 vec_alloc (*p, vec_safe_length (v));
3077
2d63bc39
JM
3078 unsigned i;
3079 tree index, value;
3080 bool constant_p = true;
3081 bool side_effects_p = false;
3e605b20 3082 FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
2d76680f 3083 {
bcb5f3c9 3084 tree orig_value = value;
3e605b20
JM
3085 init_subob_ctx (ctx, new_ctx, index, value);
3086 if (new_ctx.ctor != ctx->ctor)
3087 /* If we built a new CONSTRUCTOR, attach it now so that other
3088 initializers can refer to it. */
3089 CONSTRUCTOR_APPEND_ELT (*p, index, new_ctx.ctor);
3090 tree elt = cxx_eval_constant_expression (&new_ctx, value,
92a596e8 3091 lval,
5a804683 3092 non_constant_p, overflow_p);
2d76680f 3093 /* Don't VERIFY_CONSTANT here. */
2b3ab879 3094 if (ctx->quiet && *non_constant_p)
3e605b20 3095 break;
bcb5f3c9 3096 if (elt != orig_value)
2d76680f 3097 changed = true;
2d63bc39
JM
3098
3099 if (!TREE_CONSTANT (elt))
3100 constant_p = false;
3101 if (TREE_SIDE_EFFECTS (elt))
3102 side_effects_p = true;
3e605b20 3103 if (index && TREE_CODE (index) == COMPONENT_REF)
2d76680f
PC
3104 {
3105 /* This is an initialization of a vfield inside a base
3106 subaggregate that we already initialized; push this
3107 initialization into the previous initialization. */
3e605b20 3108 constructor_elt *inner = base_field_constructor_elt (*p, index);
2d76680f 3109 inner->value = elt;
3e605b20 3110 changed = true;
2d76680f 3111 }
3e605b20
JM
3112 else if (index
3113 && (TREE_CODE (index) == NOP_EXPR
3114 || TREE_CODE (index) == POINTER_PLUS_EXPR))
2d76680f
PC
3115 {
3116 /* This is an initializer for an empty base; now that we've
3117 checked that it's constant, we can ignore it. */
3e605b20
JM
3118 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
3119 changed = true;
3120 }
2d76680f 3121 else
ac9ec198 3122 {
1bdbef09
MP
3123 if (new_ctx.ctor != ctx->ctor)
3124 {
3125 /* We appended this element above; update the value. */
3126 gcc_assert ((*p)->last().index == index);
3127 (*p)->last().value = elt;
3128 }
3129 else
3130 CONSTRUCTOR_APPEND_ELT (*p, index, elt);
3131 /* Adding or replacing an element might change the ctor's flags. */
ac9ec198
MP
3132 TREE_CONSTANT (ctx->ctor) = constant_p;
3133 TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
3134 }
2d76680f
PC
3135 }
3136 if (*non_constant_p || !changed)
3e605b20
JM
3137 return t;
3138 t = ctx->ctor;
3139 /* We're done building this CONSTRUCTOR, so now we can interpret an
3140 element without an explicit initializer as value-initialized. */
e8c48716 3141 CONSTRUCTOR_NO_CLEARING (t) = false;
2d63bc39
JM
3142 TREE_CONSTANT (t) = constant_p;
3143 TREE_SIDE_EFFECTS (t) = side_effects_p;
8a29084d 3144 if (VECTOR_TYPE_P (type))
2d76680f
PC
3145 t = fold (t);
3146 return t;
3147}
3148
3149/* Subroutine of cxx_eval_constant_expression.
3150 The expression tree T is a VEC_INIT_EXPR which denotes the desired
3151 initialization of a non-static data member of array type. Reduce it to a
3152 CONSTRUCTOR.
3153
3154 Note that apart from value-initialization (when VALUE_INIT is true),
3155 this is only intended to support value-initialization and the
3156 initializations done by defaulted constructors for classes with
3157 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
3158 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
3159 for the copy/move constructor. */
3160
3161static tree
3e605b20 3162cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
92a596e8 3163 bool value_init, bool lval,
2d76680f
PC
3164 bool *non_constant_p, bool *overflow_p)
3165{
3166 tree elttype = TREE_TYPE (atype);
3e605b20
JM
3167 verify_ctor_sanity (ctx, atype);
3168 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
2d76680f 3169 bool pre_init = false;
4784470a 3170 unsigned HOST_WIDE_INT i;
dc5ca6c8 3171 tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
2d76680f
PC
3172
3173 /* For the default constructor, build up a call to the default
3174 constructor of the element type. We only need to handle class types
3175 here, as for a constructor to be constexpr, all members must be
3176 initialized, which for a defaulted default constructor means they must
3177 be of a class type with a constexpr default constructor. */
3178 if (TREE_CODE (elttype) == ARRAY_TYPE)
3179 /* We only do this at the lowest level. */;
3180 else if (value_init)
3181 {
dc5ca6c8 3182 init = build_value_init (elttype, complain);
2d76680f
PC
3183 pre_init = true;
3184 }
3185 else if (!init)
3186 {
cd9cf97b 3187 releasing_vec argvec;
2d76680f
PC
3188 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
3189 &argvec, elttype, LOOKUP_NORMAL,
dc5ca6c8 3190 complain);
5dab8b11 3191 init = build_aggr_init_expr (elttype, init);
2d76680f
PC
3192 pre_init = true;
3193 }
3194
74f8705e
MP
3195 tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p,
3196 overflow_p);
3197 unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts);
4784470a 3198 for (i = 0; i < max; ++i)
2d76680f
PC
3199 {
3200 tree idx = build_int_cst (size_type_node, i);
3201 tree eltinit;
928af3bf 3202 bool reuse = false;
3e605b20
JM
3203 constexpr_ctx new_ctx;
3204 init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
3205 if (new_ctx.ctor != ctx->ctor)
3206 CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
2d76680f
PC
3207 if (TREE_CODE (elttype) == ARRAY_TYPE)
3208 {
3209 /* A multidimensional array; recurse. */
3210 if (value_init || init == NULL_TREE)
928af3bf
JJ
3211 {
3212 eltinit = NULL_TREE;
3213 reuse = i == 0;
3214 }
2d76680f 3215 else
dc5ca6c8 3216 eltinit = cp_build_array_ref (input_location, init, idx, complain);
3e605b20 3217 eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init,
92a596e8 3218 lval,
2d76680f
PC
3219 non_constant_p, overflow_p);
3220 }
3221 else if (pre_init)
3222 {
3223 /* Initializing an element using value or default initialization
3224 we just pre-built above. */
3ee378fb
JM
3225 if (init == void_node)
3226 /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
3227 return ctx->ctor;
928af3bf
JJ
3228 eltinit = cxx_eval_constant_expression (&new_ctx, init, lval,
3229 non_constant_p, overflow_p);
3230 reuse = i == 0;
2d76680f
PC
3231 }
3232 else
3233 {
3234 /* Copying an element. */
3235 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3236 (atype, TREE_TYPE (init)));
dc5ca6c8 3237 eltinit = cp_build_array_ref (input_location, init, idx, complain);
72b3e203 3238 if (!lvalue_p (init))
2d76680f 3239 eltinit = move (eltinit);
dc5ca6c8 3240 eltinit = force_rvalue (eltinit, complain);
c2236b9b
JJ
3241 eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
3242 non_constant_p, overflow_p);
2d76680f 3243 }
2b3ab879 3244 if (*non_constant_p && !ctx->quiet)
3e605b20
JM
3245 break;
3246 if (new_ctx.ctor != ctx->ctor)
3247 {
3248 /* We appended this element above; update the value. */
3249 gcc_assert ((*p)->last().index == idx);
3250 (*p)->last().value = eltinit;
3251 }
3252 else
3253 CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
928af3bf 3254 /* Reuse the result of cxx_eval_constant_expression call
c2236b9b
JJ
3255 from the first iteration to all others if it is a constant
3256 initializer that doesn't require relocations. */
928af3bf
JJ
3257 if (reuse
3258 && max > 1
c2236b9b
JJ
3259 && (eltinit == NULL_TREE
3260 || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit))
3261 == null_pointer_node)))
928af3bf
JJ
3262 {
3263 if (new_ctx.ctor != ctx->ctor)
3264 eltinit = new_ctx.ctor;
1e027956
RB
3265 tree range = build2 (RANGE_EXPR, size_type_node,
3266 build_int_cst (size_type_node, 1),
3267 build_int_cst (size_type_node, max - 1));
3268 CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit));
928af3bf
JJ
3269 break;
3270 }
1e027956
RB
3271 else if (i == 0)
3272 vec_safe_reserve (*p, max);
2d76680f
PC
3273 }
3274
3275 if (!*non_constant_p)
3276 {
3e605b20 3277 init = ctx->ctor;
e8c48716 3278 CONSTRUCTOR_NO_CLEARING (init) = false;
2d76680f 3279 }
2d76680f
PC
3280 return init;
3281}
3282
3283static tree
3e605b20 3284cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
92a596e8 3285 bool lval,
2d76680f
PC
3286 bool *non_constant_p, bool *overflow_p)
3287{
3288 tree atype = TREE_TYPE (t);
3289 tree init = VEC_INIT_EXPR_INIT (t);
3e605b20 3290 tree r = cxx_eval_vec_init_1 (ctx, atype, init,
2d76680f 3291 VEC_INIT_EXPR_VALUE_INIT (t),
92a596e8 3292 lval, non_constant_p, overflow_p);
2d76680f
PC
3293 if (*non_constant_p)
3294 return t;
3295 else
3296 return r;
3297}
3298
e4511ca2
JM
3299/* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
3300 where the desired type is an array of unknown bounds because the variable
3301 has had its bounds deduced since the wrapping expression was created. */
3302
3303static bool
3304same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2)
3305{
3306 while (TREE_CODE (type1) == ARRAY_TYPE
3307 && TREE_CODE (type2) == ARRAY_TYPE
3308 && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2)))
3309 {
3310 type1 = TREE_TYPE (type1);
3311 type2 = TREE_TYPE (type2);
3312 }
3313 return same_type_ignoring_top_level_qualifiers_p (type1, type2);
3314}
3315
2d76680f
PC
3316/* A less strict version of fold_indirect_ref_1, which requires cv-quals to
3317 match. We want to be less strict for simple *& folding; if we have a
3318 non-const temporary that we access through a const pointer, that should
3319 work. We handle this here rather than change fold_indirect_ref_1
3320 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
3321 don't really make sense outside of constant expression evaluation. Also
3322 we want to allow folding to COMPONENT_REF, which could cause trouble
3323 with TBAA in fold_indirect_ref_1.
3324
3325 Try to keep this function synced with fold_indirect_ref_1. */
3326
3327static tree
3328cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
3329{
ebe4bf41
MP
3330 tree sub = op0;
3331 tree subtype;
3332 poly_uint64 const_op01;
2d76680f 3333
2d76680f
PC
3334 STRIP_NOPS (sub);
3335 subtype = TREE_TYPE (sub);
71a93b08 3336 if (!INDIRECT_TYPE_P (subtype))
2d76680f
PC
3337 return NULL_TREE;
3338
3339 if (TREE_CODE (sub) == ADDR_EXPR)
3340 {
3341 tree op = TREE_OPERAND (sub, 0);
3342 tree optype = TREE_TYPE (op);
3343
3344 /* *&CONST_DECL -> to the value of the const decl. */
3345 if (TREE_CODE (op) == CONST_DECL)
3346 return DECL_INITIAL (op);
3347 /* *&p => p; make sure to handle *&"str"[cst] here. */
e4511ca2 3348 if (same_type_ignoring_tlq_and_bounds_p (optype, type))
2d76680f
PC
3349 {
3350 tree fop = fold_read_from_constant_string (op);
3351 if (fop)
3352 return fop;
3353 else
3354 return op;
3355 }
3356 /* *(foo *)&fooarray => fooarray[0] */
3357 else if (TREE_CODE (optype) == ARRAY_TYPE
3358 && (same_type_ignoring_top_level_qualifiers_p
3359 (type, TREE_TYPE (optype))))
3360 {
3361 tree type_domain = TYPE_DOMAIN (optype);
3362 tree min_val = size_zero_node;
3363 if (type_domain && TYPE_MIN_VALUE (type_domain))
3364 min_val = TYPE_MIN_VALUE (type_domain);
3365 return build4_loc (loc, ARRAY_REF, type, op, min_val,
3366 NULL_TREE, NULL_TREE);
3367 }
3368 /* *(foo *)&complexfoo => __real__ complexfoo */
3369 else if (TREE_CODE (optype) == COMPLEX_TYPE
3370 && (same_type_ignoring_top_level_qualifiers_p
3371 (type, TREE_TYPE (optype))))
3372 return fold_build1_loc (loc, REALPART_EXPR, type, op);
3373 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
b55b02ea 3374 else if (VECTOR_TYPE_P (optype)
2d76680f
PC
3375 && (same_type_ignoring_top_level_qualifiers_p
3376 (type, TREE_TYPE (optype))))
3377 {
3378 tree part_width = TYPE_SIZE (type);
3379 tree index = bitsize_int (0);
ebe4bf41
MP
3380 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width,
3381 index);
2d76680f
PC
3382 }
3383 /* Also handle conversion to an empty base class, which
3384 is represented with a NOP_EXPR. */
3385 else if (is_empty_class (type)
3386 && CLASS_TYPE_P (optype)
3387 && DERIVED_FROM_P (type, optype))
3388 {
3389 *empty_base = true;
3390 return op;
3391 }
3392 /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
3393 else if (RECORD_OR_UNION_TYPE_P (optype))
3394 {
3395 tree field = TYPE_FIELDS (optype);
3396 for (; field; field = DECL_CHAIN (field))
3397 if (TREE_CODE (field) == FIELD_DECL
b912f962 3398 && TREE_TYPE (field) != error_mark_node
2d76680f
PC
3399 && integer_zerop (byte_position (field))
3400 && (same_type_ignoring_top_level_qualifiers_p
3401 (TREE_TYPE (field), type)))
b912f962 3402 return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE);
2d76680f
PC
3403 }
3404 }
3405 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
ebe4bf41 3406 && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
2d76680f
PC
3407 {
3408 tree op00 = TREE_OPERAND (sub, 0);
3409 tree op01 = TREE_OPERAND (sub, 1);
3410
3411 STRIP_NOPS (op00);
3412 if (TREE_CODE (op00) == ADDR_EXPR)
3413 {
3414 tree op00type;
3415 op00 = TREE_OPERAND (op00, 0);
3416 op00type = TREE_TYPE (op00);
3417
3418 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
b55b02ea 3419 if (VECTOR_TYPE_P (op00type)
ebe4bf41
MP
3420 && same_type_ignoring_top_level_qualifiers_p
3421 (type, TREE_TYPE (op00type))
3422 /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
3423 but we want to treat offsets with MSB set as negative.
3424 For the code below negative offsets are invalid and
3425 TYPE_SIZE of the element is something unsigned, so
3426 check whether op01 fits into poly_int64, which implies
3427 it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
3428 then just use poly_uint64 because we want to treat the
3429 value as unsigned. */
3430 && tree_fits_poly_int64_p (op01))
2d76680f 3431 {
2d76680f 3432 tree part_width = TYPE_SIZE (type);
ebe4bf41
MP
3433 poly_uint64 max_offset
3434 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
3435 * TYPE_VECTOR_SUBPARTS (op00type));
3436 if (known_lt (const_op01, max_offset))
3437 {
3438 tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
3439 return fold_build3_loc (loc,
3440 BIT_FIELD_REF, type, op00,
3441 part_width, index);
3442 }
2d76680f
PC
3443 }
3444 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
3445 else if (TREE_CODE (op00type) == COMPLEX_TYPE
3446 && (same_type_ignoring_top_level_qualifiers_p
3447 (type, TREE_TYPE (op00type))))
3448 {
ebe4bf41
MP
3449 if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
3450 const_op01))
2d76680f
PC
3451 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
3452 }
3453 /* ((foo *)&fooarray)[1] => fooarray[1] */
3454 else if (TREE_CODE (op00type) == ARRAY_TYPE
3455 && (same_type_ignoring_top_level_qualifiers_p
3456 (type, TREE_TYPE (op00type))))
3457 {
3458 tree type_domain = TYPE_DOMAIN (op00type);
3459 tree min_val = size_zero_node;
3460 if (type_domain && TYPE_MIN_VALUE (type_domain))
3461 min_val = TYPE_MIN_VALUE (type_domain);
b07c3651
MP
3462 offset_int off = wi::to_offset (op01);
3463 offset_int el_sz = wi::to_offset (TYPE_SIZE_UNIT (type));
3464 offset_int remainder;
3465 off = wi::divmod_trunc (off, el_sz, SIGNED, &remainder);
3466 if (remainder == 0 && TREE_CODE (min_val) == INTEGER_CST)
3467 {
3468 off = off + wi::to_offset (min_val);
3469 op01 = wide_int_to_tree (sizetype, off);
3470 return build4_loc (loc, ARRAY_REF, type, op00, op01,
3471 NULL_TREE, NULL_TREE);
3472 }
2d76680f
PC
3473 }
3474 /* Also handle conversion to an empty base class, which
3475 is represented with a NOP_EXPR. */
3476 else if (is_empty_class (type)
3477 && CLASS_TYPE_P (op00type)
3478 && DERIVED_FROM_P (type, op00type))
3479 {
3480 *empty_base = true;
3481 return op00;
3482 }
3483 /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
3484 else if (RECORD_OR_UNION_TYPE_P (op00type))
3485 {
3486 tree field = TYPE_FIELDS (op00type);
3487 for (; field; field = DECL_CHAIN (field))
3488 if (TREE_CODE (field) == FIELD_DECL
b912f962 3489 && TREE_TYPE (field) != error_mark_node
2d76680f
PC
3490 && tree_int_cst_equal (byte_position (field), op01)
3491 && (same_type_ignoring_top_level_qualifiers_p
3492 (TREE_TYPE (field), type)))
b912f962
JJ
3493 return fold_build3 (COMPONENT_REF, type, op00,
3494 field, NULL_TREE);
2d76680f
PC
3495 }
3496 }
3497 }
3498 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
3499 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
3500 && (same_type_ignoring_top_level_qualifiers_p
3501 (type, TREE_TYPE (TREE_TYPE (subtype)))))
3502 {
3503 tree type_domain;
3504 tree min_val = size_zero_node;
ebe4bf41
MP
3505 tree newsub
3506 = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL);
2d76680f
PC
3507 if (newsub)
3508 sub = newsub;
3509 else
3510 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
3511 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
3512 if (type_domain && TYPE_MIN_VALUE (type_domain))
3513 min_val = TYPE_MIN_VALUE (type_domain);
3514 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
3515 NULL_TREE);
3516 }
3517
3518 return NULL_TREE;
3519}
3520
3521static tree
3e605b20 3522cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
92a596e8 3523 bool lval,
2d76680f
PC
3524 bool *non_constant_p, bool *overflow_p)
3525{
3526 tree orig_op0 = TREE_OPERAND (t, 0);
2d76680f 3527 bool empty_base = false;
2d76680f 3528
cda0a029
JM
3529 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
3530 operand is an integer-zero. Otherwise reject the MEM_REF for now. */
3531
3532 if (TREE_CODE (t) == MEM_REF
3533 && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1))))
3534 {
3535 gcc_assert (ctx->quiet);
3536 *non_constant_p = true;
3537 return t;
3538 }
3539
255a48d6
JM
3540 /* First try to simplify it directly. */
3541 tree r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), orig_op0,
3542 &empty_base);
3543 if (!r)
2d76680f 3544 {
255a48d6
JM
3545 /* If that didn't work, evaluate the operand first. */
3546 tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
3547 /*lval*/false, non_constant_p,
3548 overflow_p);
3549 /* Don't VERIFY_CONSTANT here. */
3550 if (*non_constant_p)
3551 return t;
3552
8bada5cd
MS
3553 if (!lval && integer_zerop (op0))
3554 {
3555 if (!ctx->quiet)
3556 error ("dereferencing a null pointer");
3557 *non_constant_p = true;
3558 return t;
3559 }
3560
255a48d6
JM
3561 r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0,
3562 &empty_base);
3563 if (r == NULL_TREE)
2d76680f
PC
3564 {
3565 /* We couldn't fold to a constant value. Make sure it's not
3566 something we should have been able to fold. */
255a48d6
JM
3567 tree sub = op0;
3568 STRIP_NOPS (sub);
3569 if (TREE_CODE (sub) == ADDR_EXPR)
3570 {
3571 gcc_assert (!same_type_ignoring_top_level_qualifiers_p
3572 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
3573 /* DR 1188 says we don't have to deal with this. */
3574 if (!ctx->quiet)
3575 error ("accessing value of %qE through a %qT glvalue in a "
3576 "constant expression", build_fold_indirect_ref (sub),
3577 TREE_TYPE (t));
3578 *non_constant_p = true;
3579 return t;
3580 }
3581
3582 if (lval && op0 != orig_op0)
3583 return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
3584 if (!lval)
3585 VERIFY_CONSTANT (t);
2d76680f
PC
3586 return t;
3587 }
3588 }
3589
255a48d6
JM
3590 r = cxx_eval_constant_expression (ctx, r,
3591 lval, non_constant_p, overflow_p);
3592 if (*non_constant_p)
3593 return t;
3594
125db6a1 3595 /* If we're pulling out the value of an empty base, just return an empty
2d76680f 3596 CONSTRUCTOR. */
92a596e8 3597 if (empty_base && !lval)
2d76680f 3598 {
2d76680f
PC
3599 r = build_constructor (TREE_TYPE (t), NULL);
3600 TREE_CONSTANT (r) = true;
3601 }
3602
2d76680f
PC
3603 return r;
3604}
3605
3606/* Complain about R, a VAR_DECL, not being usable in a constant expression.
3607 Shared between potential_constant_expression and
3608 cxx_eval_constant_expression. */
3609
3610static void
3611non_const_var_error (tree r)
3612{
3613 tree type = TREE_TYPE (r);
3614 error ("the value of %qD is not usable in a constant "
3615 "expression", r);
3616 /* Avoid error cascade. */
3617 if (DECL_INITIAL (r) == error_mark_node)
3618 return;
3619 if (DECL_DECLARED_CONSTEXPR_P (r))
3620 inform (DECL_SOURCE_LOCATION (r),
3621 "%qD used in its own initializer", r);
3622 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3623 {
3624 if (!CP_TYPE_CONST_P (type))
3625 inform (DECL_SOURCE_LOCATION (r),
3626 "%q#D is not const", r);
3627 else if (CP_TYPE_VOLATILE_P (type))
3628 inform (DECL_SOURCE_LOCATION (r),
3629 "%q#D is volatile", r);
3630 else if (!DECL_INITIAL (r)
a3e2b438
PP
3631 || !TREE_CONSTANT (DECL_INITIAL (r))
3632 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r))
2d76680f
PC
3633 inform (DECL_SOURCE_LOCATION (r),
3634 "%qD was not initialized with a constant "
3635 "expression", r);
3636 else
3637 gcc_unreachable ();
3638 }
9f613f06 3639 else if (TYPE_REF_P (type))
fd338b13
JM
3640 inform (DECL_SOURCE_LOCATION (r),
3641 "%qD was not initialized with a constant "
3642 "expression", r);
2d76680f
PC
3643 else
3644 {
3645 if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
3646 inform (DECL_SOURCE_LOCATION (r),
3647 "%qD was not declared %<constexpr%>", r);
3648 else
3649 inform (DECL_SOURCE_LOCATION (r),
3650 "%qD does not have integral or enumeration type",
3651 r);
3652 }
3653}
3654
3655/* Subroutine of cxx_eval_constant_expression.
3656 Like cxx_eval_unary_expression, except for trinary expressions. */
3657
3658static tree
3e605b20 3659cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
92a596e8 3660 bool lval,
2d76680f
PC
3661 bool *non_constant_p, bool *overflow_p)
3662{
3663 int i;
3664 tree args[3];
3665 tree val;
3666
3667 for (i = 0; i < 3; i++)
3668 {
3e605b20 3669 args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
92a596e8 3670 lval,
5a804683 3671 non_constant_p, overflow_p);
2d76680f
PC
3672 VERIFY_CONSTANT (args[i]);
3673 }
3674
3675 val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
3676 args[0], args[1], args[2]);
3677 if (val == NULL_TREE)
3678 return t;
3679 VERIFY_CONSTANT (val);
3680 return val;
3681}
3682
98e5a19a
JM
3683/* True if T was declared in a function declared to be constexpr, and
3684 therefore potentially constant in C++14. */
3685
2d76680f
PC
3686bool
3687var_in_constexpr_fn (tree t)
3688{
3689 tree ctx = DECL_CONTEXT (t);
98e5a19a 3690 return (ctx && TREE_CODE (ctx) == FUNCTION_DECL
2d76680f
PC
3691 && DECL_DECLARED_CONSTEXPR_P (ctx));
3692}
3693
98e5a19a
JM
3694/* True if T was declared in a function that might be constexpr: either a
3695 function that was declared constexpr, or a C++17 lambda op(). */
3696
3697bool
3698var_in_maybe_constexpr_fn (tree t)
3699{
7b936140 3700 if (cxx_dialect >= cxx17
98e5a19a
JM
3701 && DECL_FUNCTION_SCOPE_P (t)
3702 && LAMBDA_FUNCTION_P (DECL_CONTEXT (t)))
3703 return true;
3704 return var_in_constexpr_fn (t);
3705}
3706
68d01920
JM
3707/* We're assigning INIT to TARGET. In do_build_copy_constructor and
3708 build_over_call we implement trivial copy of a class with tail padding using
3709 assignment of character arrays, which is valid in normal code, but not in
3710 constexpr evaluation. We don't need to worry about clobbering tail padding
3711 in constexpr evaluation, so strip the type punning. */
3712
3713static void
3714maybe_simplify_trivial_copy (tree &target, tree &init)
3715{
3716 if (TREE_CODE (target) == MEM_REF
3717 && TREE_CODE (init) == MEM_REF
3718 && TREE_TYPE (target) == TREE_TYPE (init)
3719 && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE
3720 && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node)
3721 {
3722 target = build_fold_indirect_ref (TREE_OPERAND (target, 0));
3723 init = build_fold_indirect_ref (TREE_OPERAND (init, 0));
3724 }
3725}
3726
60813a46
JM
3727/* Evaluate an INIT_EXPR or MODIFY_EXPR. */
3728
3729static tree
3730cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
92a596e8 3731 bool lval,
60813a46
JM
3732 bool *non_constant_p, bool *overflow_p)
3733{
3734 constexpr_ctx new_ctx = *ctx;
3735
58cc255c
JM
3736 tree init = TREE_OPERAND (t, 1);
3737 if (TREE_CLOBBER_P (init))
3738 /* Just ignore clobbers. */
3739 return void_node;
3740
60813a46
JM
3741 /* First we figure out where we're storing to. */
3742 tree target = TREE_OPERAND (t, 0);
68d01920
JM
3743
3744 maybe_simplify_trivial_copy (target, init);
3745
3f8e2835 3746 tree type = TREE_TYPE (target);
e8b3c1bc
JM
3747 bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR;
3748 if (preeval)
3749 {
3750 /* Evaluate the value to be stored without knowing what object it will be
3751 stored in, so that any side-effects happen first. */
3752 if (!SCALAR_TYPE_P (type))
3753 new_ctx.ctor = new_ctx.object = NULL_TREE;
3754 init = cxx_eval_constant_expression (&new_ctx, init, false,
3755 non_constant_p, overflow_p);
3756 if (*non_constant_p)
3757 return t;
3758 }
60813a46 3759
d0aa42d2
JM
3760 bool evaluated = false;
3761 if (lval)
3f8e2835 3762 {
d0aa42d2
JM
3763 /* If we want to return a reference to the target, we need to evaluate it
3764 as a whole; otherwise, only evaluate the innermost piece to avoid
3765 building up unnecessary *_REFs. */
3766 target = cxx_eval_constant_expression (ctx, target, true,
3767 non_constant_p, overflow_p);
3768 evaluated = true;
3769 if (*non_constant_p)
3770 return t;
3f8e2835
JM
3771 }
3772
043666e0 3773 /* Find the underlying variable. */
cd9cf97b 3774 releasing_vec refs;
60813a46
JM
3775 tree object = NULL_TREE;
3776 for (tree probe = target; object == NULL_TREE; )
3777 {
3778 switch (TREE_CODE (probe))
3779 {
3780 case BIT_FIELD_REF:
3781 case COMPONENT_REF:
3782 case ARRAY_REF:
043666e0
JM
3783 {
3784 tree ob = TREE_OPERAND (probe, 0);
3785 tree elt = TREE_OPERAND (probe, 1);
3786 if (TREE_CODE (probe) == ARRAY_REF)
3787 {
3788 elt = eval_and_check_array_index (ctx, probe, false,
3789 non_constant_p, overflow_p);
3790 if (*non_constant_p)
3791 return t;
3792 }
3793 vec_safe_push (refs, elt);
3794 vec_safe_push (refs, TREE_TYPE (probe));
3795 probe = ob;
3796 }
60813a46
JM
3797 break;
3798
3799 default:
d0aa42d2
JM
3800 if (evaluated)
3801 object = probe;
3802 else
3803 {
3804 probe = cxx_eval_constant_expression (ctx, probe, true,
3805 non_constant_p, overflow_p);
3806 evaluated = true;
3807 if (*non_constant_p)
3808 return t;
3809 }
3810 break;
60813a46
JM
3811 }
3812 }
3813
3814 /* And then find/build up our initializer for the path to the subobject
3815 we're initializing. */
a1408b7e 3816 tree *valp;
1e163090
JM
3817 if (object == ctx->object && VAR_P (object)
3818 && DECL_NAME (object) && ctx->call == NULL)
3819 /* The variable we're building up an aggregate initializer for is outside
3820 the constant-expression, so don't evaluate the store. We check
3821 DECL_NAME to handle TARGET_EXPR temporaries, which are fair game. */
3822 valp = NULL;
3823 else if (DECL_P (object))
a1408b7e
JM
3824 valp = ctx->values->get (object);
3825 else
3826 valp = NULL;
60813a46
JM
3827 if (!valp)
3828 {
3829 /* A constant-expression cannot modify objects from outside the
3830 constant-expression. */
2b3ab879 3831 if (!ctx->quiet)
64d6d399 3832 error ("modification of %qE is not a constant expression", object);
60813a46
JM
3833 *non_constant_p = true;
3834 return t;
3835 }
3f8e2835 3836 type = TREE_TYPE (object);
c75ce530 3837 bool no_zero_init = true;
2d63bc39 3838
cd9cf97b 3839 releasing_vec ctors;
60813a46
JM
3840 while (!refs->is_empty())
3841 {
3842 if (*valp == NULL_TREE)
3843 {
3844 *valp = build_constructor (type, NULL);
e8c48716 3845 CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
60813a46 3846 }
d6b46fca
NS
3847 else if (TREE_CODE (*valp) == STRING_CST)
3848 {
3849 /* An array was initialized with a string constant, and now
3850 we're writing into one of its elements. Explode the
3851 single initialization into a set of element
3852 initializations. */
3853 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
3854
3855 tree string = *valp;
3856 tree elt_type = TREE_TYPE (type);
3857 unsigned chars_per_elt = (TYPE_PRECISION (elt_type)
3858 / TYPE_PRECISION (char_type_node));
3859 unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt;
3860 tree ary_ctor = build_constructor (type, NULL);
3861
3862 vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts);
3863 for (unsigned ix = 0; ix != num_elts; ix++)
3864 {
3865 constructor_elt elt =
3866 {
3867 build_int_cst (size_type_node, ix),
3868 extract_string_elt (string, chars_per_elt, ix)
3869 };
3870 CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt);
3871 }
3872
3873 *valp = ary_ctor;
3874 }
3875
c75ce530
JM
3876 /* If the value of object is already zero-initialized, any new ctors for
3877 subobjects will also be zero-initialized. */
e8c48716 3878 no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp);
60813a46 3879
2d63bc39
JM
3880 vec_safe_push (ctors, *valp);
3881
ceaaf873 3882 enum tree_code code = TREE_CODE (type);
60813a46 3883 type = refs->pop();
ceaaf873 3884 tree index = refs->pop();
60813a46 3885
60813a46 3886 constructor_elt *cep = NULL;
ceaaf873
JM
3887 if (code == ARRAY_TYPE)
3888 {
3889 HOST_WIDE_INT i
3890 = find_array_ctor_elt (*valp, index, /*insert*/true);
3891 gcc_assert (i >= 0);
3892 cep = CONSTRUCTOR_ELT (*valp, i);
3893 gcc_assert (TREE_CODE (cep->index) != RANGE_EXPR);
3894 }
3895 else
3896 {
3897 gcc_assert (TREE_CODE (index) == FIELD_DECL);
88504f34
NS
3898
3899 /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
3900 Usually we meet initializers in that order, but it is
3901 possible for base types to be placed not in program
3902 order. */
3903 tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
3904 unsigned HOST_WIDE_INT idx;
3905
5c97093b
JM
3906 if (code == UNION_TYPE && CONSTRUCTOR_NELTS (*valp)
3907 && CONSTRUCTOR_ELT (*valp, 0)->index != index)
ed4ec9ce
JJ
3908 {
3909 if (cxx_dialect < cxx2a)
3910 {
3911 if (!ctx->quiet)
3912 error_at (cp_expr_loc_or_loc (t, input_location),
3913 "change of the active member of a union "
3914 "from %qD to %qD",
3915 CONSTRUCTOR_ELT (*valp, 0)->index,
3916 index);
3917 *non_constant_p = true;
3918 }
3919 /* Changing active member. */
3920 vec_safe_truncate (CONSTRUCTOR_ELTS (*valp), 0);
c7f7c313 3921 no_zero_init = true;
ed4ec9ce 3922 }
5c97093b 3923
88504f34 3924 for (idx = 0;
ceaaf873 3925 vec_safe_iterate (CONSTRUCTOR_ELTS (*valp), idx, &cep);
88504f34 3926 idx++, fields = DECL_CHAIN (fields))
ceaaf873 3927 {
88504f34
NS
3928 if (index == cep->index)
3929 goto found;
3930
3931 /* The field we're initializing must be on the field
3932 list. Look to see if it is present before the
3933 field the current ELT initializes. */
3934 for (; fields != cep->index; fields = DECL_CHAIN (fields))
3935 if (index == fields)
3936 goto insert;
ceaaf873 3937 }
88504f34
NS
3938
3939 /* We fell off the end of the CONSTRUCTOR, so insert a new
3940 entry at the end. */
3941 insert:
3942 {
3943 constructor_elt ce = { index, NULL_TREE };
3944
3945 vec_safe_insert (CONSTRUCTOR_ELTS (*valp), idx, ce);
3946 cep = CONSTRUCTOR_ELT (*valp, idx);
3947 }
3948 found:;
ceaaf873 3949 }
60813a46
JM
3950 valp = &cep->value;
3951 }
60813a46 3952
e8b3c1bc 3953 if (!preeval)
60813a46
JM
3954 {
3955 /* Create a new CONSTRUCTOR in case evaluation of the initializer
3956 wants to modify it. */
acb2970c 3957 if (*valp == NULL_TREE)
73a84269 3958 {
664beaf2 3959 *valp = build_constructor (type, NULL);
e8c48716 3960 CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
73a84269 3961 }
664beaf2 3962 new_ctx.ctor = *valp;
60813a46 3963 new_ctx.object = target;
e8b3c1bc
JM
3964 init = cxx_eval_constant_expression (&new_ctx, init, false,
3965 non_constant_p, overflow_p);
d0aa42d2 3966 if (ctors->is_empty())
e8b3c1bc
JM
3967 /* The hash table might have moved since the get earlier. */
3968 valp = ctx->values->get (object);
60813a46
JM
3969 }
3970
7574c916 3971 /* Don't share a CONSTRUCTOR that might be changed later. */
0146e25f 3972 init = unshare_constructor (init);
d96e8407 3973
e8b3c1bc
JM
3974 if (*valp && TREE_CODE (*valp) == CONSTRUCTOR
3975 && TREE_CODE (init) == CONSTRUCTOR)
acb2970c 3976 {
d96e8407
JM
3977 /* An outer ctx->ctor might be pointing to *valp, so replace
3978 its contents. */
d0aa42d2
JM
3979 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init),
3980 TREE_TYPE (*valp)))
3981 {
3982 /* For initialization of an empty base, the original target will be
3983 *(base*)this, evaluation of which resolves to the object
3984 argument, which has the derived type rather than the base type. In
3985 this situation, just evaluate the initializer and return, since
3986 there's no actual data to store. */
3987 gcc_assert (is_empty_class (TREE_TYPE (init)) && !lval);
3988 return init;
3989 }
d96e8407
JM
3990 CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
3991 TREE_CONSTANT (*valp) = TREE_CONSTANT (init);
3992 TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init);
e8c48716
JM
3993 CONSTRUCTOR_NO_CLEARING (*valp)
3994 = CONSTRUCTOR_NO_CLEARING (init);
acb2970c 3995 }
60813a46 3996 else
d96e8407 3997 *valp = init;
2d63bc39 3998
d96e8407
JM
3999 /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
4000 CONSTRUCTORs, if any. */
4001 tree elt;
4002 unsigned i;
4003 bool c = TREE_CONSTANT (init);
4004 bool s = TREE_SIDE_EFFECTS (init);
4005 if (!c || s)
cd9cf97b 4006 FOR_EACH_VEC_ELT (*ctors, i, elt)
d96e8407
JM
4007 {
4008 if (!c)
4009 TREE_CONSTANT (elt) = false;
4010 if (s)
4011 TREE_SIDE_EFFECTS (elt) = true;
4012 }
60813a46
JM
4013
4014 if (*non_constant_p)
4015 return t;
92a596e8 4016 else if (lval)
60813a46
JM
4017 return target;
4018 else
3bdf0a9b 4019 return init;
60813a46
JM
4020}
4021
4022/* Evaluate a ++ or -- expression. */
4023
4024static tree
4025cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
92a596e8 4026 bool lval,
60813a46
JM
4027 bool *non_constant_p, bool *overflow_p)
4028{
4029 enum tree_code code = TREE_CODE (t);
4030 tree type = TREE_TYPE (t);
4031 tree op = TREE_OPERAND (t, 0);
4032 tree offset = TREE_OPERAND (t, 1);
4033 gcc_assert (TREE_CONSTANT (offset));
4034
4035 /* The operand as an lvalue. */
2b3ab879 4036 op = cxx_eval_constant_expression (ctx, op, true,
5a804683 4037 non_constant_p, overflow_p);
60813a46
JM
4038
4039 /* The operand as an rvalue. */
84dd815f
JM
4040 tree val
4041 = cxx_eval_constant_expression (ctx, op, false,
4042 non_constant_p, overflow_p);
caee690e
JM
4043 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
4044 a local array in a constexpr function. */
71a93b08 4045 bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val));
caee690e
JM
4046 if (!ptr)
4047 VERIFY_CONSTANT (val);
60813a46
JM
4048
4049 /* The modified value. */
4050 bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
ac2f8d26 4051 tree mod;
71a93b08 4052 if (INDIRECT_TYPE_P (type))
ac2f8d26
JM
4053 {
4054 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
4055 offset = convert_to_ptrofftype (offset);
4056 if (!inc)
4057 offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
4058 mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
4059 }
4060 else
4061 mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
caee690e
JM
4062 if (!ptr)
4063 VERIFY_CONSTANT (mod);
60813a46
JM
4064
4065 /* Storing the modified value. */
4066 tree store = build2 (MODIFY_EXPR, type, op, mod);
2b3ab879 4067 cxx_eval_constant_expression (ctx, store,
5a804683 4068 true, non_constant_p, overflow_p);
ecdcd560 4069 ggc_free (store);
60813a46
JM
4070
4071 /* And the value of the expression. */
4072 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
4073 {
4074 /* Prefix ops are lvalues. */
92a596e8 4075 if (lval)
60813a46
JM
4076 return op;
4077 else
4078 /* But we optimize when the caller wants an rvalue. */
4079 return mod;
4080 }
4081 else
4082 /* Postfix ops are rvalues. */
4083 return val;
4084}
4085
56632b27
JM
4086/* Predicates for the meaning of *jump_target. */
4087
4088static bool
4089returns (tree *jump_target)
4090{
4091 return *jump_target
d49318d9
PC
4092 && (TREE_CODE (*jump_target) == RETURN_EXPR
4093 || (TREE_CODE (*jump_target) == LABEL_DECL
4094 && LABEL_DECL_CDTOR (*jump_target)));
56632b27
JM
4095}
4096
4097static bool
4098breaks (tree *jump_target)
4099{
4100 return *jump_target
06ec22b7
JM
4101 && ((TREE_CODE (*jump_target) == LABEL_DECL
4102 && LABEL_DECL_BREAK (*jump_target))
3075affd 4103 || TREE_CODE (*jump_target) == BREAK_STMT
06ec22b7 4104 || TREE_CODE (*jump_target) == EXIT_EXPR);
56632b27
JM
4105}
4106
4107static bool
4108continues (tree *jump_target)
4109{
4110 return *jump_target
3075affd
JM
4111 && ((TREE_CODE (*jump_target) == LABEL_DECL
4112 && LABEL_DECL_CONTINUE (*jump_target))
4113 || TREE_CODE (*jump_target) == CONTINUE_STMT);
4114
56632b27
JM
4115}
4116
4117static bool
4118switches (tree *jump_target)
4119{
4120 return *jump_target
4121 && TREE_CODE (*jump_target) == INTEGER_CST;
4122}
4123
4124/* Subroutine of cxx_eval_statement_list. Determine whether the statement
4b390698
JJ
4125 STMT matches *jump_target. If we're looking for a case label and we see
4126 the default label, note it in ctx->css_state. */
56632b27
JM
4127
4128static bool
4b390698 4129label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt)
56632b27 4130{
56632b27
JM
4131 switch (TREE_CODE (*jump_target))
4132 {
4133 case LABEL_DECL:
4134 if (TREE_CODE (stmt) == LABEL_EXPR
4135 && LABEL_EXPR_LABEL (stmt) == *jump_target)
4136 return true;
4137 break;
4138
4139 case INTEGER_CST:
4140 if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
4141 {
4b390698 4142 gcc_assert (ctx->css_state != NULL);
56632b27 4143 if (!CASE_LOW (stmt))
4b390698
JJ
4144 {
4145 /* default: should appear just once in a SWITCH_EXPR
4146 body (excluding nested SWITCH_EXPR). */
4147 gcc_assert (*ctx->css_state != css_default_seen);
4148 /* When evaluating SWITCH_EXPR body for the second time,
4149 return true for the default: label. */
4150 if (*ctx->css_state == css_default_processing)
4151 return true;
4152 *ctx->css_state = css_default_seen;
4153 }
385ed708
JJ
4154 else if (CASE_HIGH (stmt))
4155 {
4156 if (tree_int_cst_le (CASE_LOW (stmt), *jump_target)
4157 && tree_int_cst_le (*jump_target, CASE_HIGH (stmt)))
4158 return true;
4159 }
56632b27
JM
4160 else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
4161 return true;
4162 }
4163 break;
4164
43574e4f
JJ
4165 case BREAK_STMT:
4166 case CONTINUE_STMT:
4167 /* These two are handled directly in cxx_eval_loop_expr by testing
4168 breaks (jump_target) or continues (jump_target). */
4169 break;
4170
56632b27
JM
4171 default:
4172 gcc_unreachable ();
4173 }
4174 return false;
4175}
4176
4177/* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
4178 semantics, for switch, break, continue, and return. */
4179
4180static tree
4181cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
56632b27
JM
4182 bool *non_constant_p, bool *overflow_p,
4183 tree *jump_target)
4184{
4185 tree_stmt_iterator i;
58611fb6 4186 tree local_target;
345edb37
JJ
4187 /* In a statement-expression we want to return the last value.
4188 For empty statement expression return void_node. */
4189 tree r = void_node;
58611fb6
JM
4190 if (!jump_target)
4191 {
4192 local_target = NULL_TREE;
4193 jump_target = &local_target;
4194 }
56632b27
JM
4195 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
4196 {
56632b27 4197 tree stmt = tsi_stmt (i);
0250ba58
MP
4198 /* We've found a continue, so skip everything until we reach
4199 the label its jumping to. */
4200 if (continues (jump_target))
4201 {
4202 if (label_matches (ctx, jump_target, stmt))
4203 /* Found it. */
4204 *jump_target = NULL_TREE;
4205 else
4206 continue;
4207 }
6ef72c36
JJ
4208 if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
4209 continue;
58611fb6
JM
4210 r = cxx_eval_constant_expression (ctx, stmt, false,
4211 non_constant_p, overflow_p,
4212 jump_target);
56632b27
JM
4213 if (*non_constant_p)
4214 break;
4215 if (returns (jump_target) || breaks (jump_target))
4216 break;
4217 }
ce36ba09
JM
4218 if (*jump_target && jump_target == &local_target)
4219 {
4220 /* We aren't communicating the jump to our caller, so give up. We don't
4221 need to support evaluation of jumps out of statement-exprs. */
4222 if (!ctx->quiet)
4223 error_at (cp_expr_loc_or_loc (r, input_location),
4224 "statement is not a constant expression");
4225 *non_constant_p = true;
4226 }
58611fb6 4227 return r;
56632b27
JM
4228}
4229
4230/* Evaluate a LOOP_EXPR for side-effects. Handles break and return
4231 semantics; continue semantics are covered by cxx_eval_statement_list. */
4232
4233static tree
4234cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
56632b27
JM
4235 bool *non_constant_p, bool *overflow_p,
4236 tree *jump_target)
4237{
39dce2b7
JM
4238 constexpr_ctx new_ctx = *ctx;
4239
43574e4f 4240 tree body, cond = NULL_TREE, expr = NULL_TREE;
5ec2cd9f 4241 int count = 0;
43574e4f
JJ
4242 switch (TREE_CODE (t))
4243 {
4244 case LOOP_EXPR:
4245 body = LOOP_EXPR_BODY (t);
4246 break;
4247 case DO_STMT:
4248 body = DO_BODY (t);
4249 cond = DO_COND (t);
4250 break;
4251 case WHILE_STMT:
4252 body = WHILE_BODY (t);
4253 cond = WHILE_COND (t);
4254 count = -1;
4255 break;
4256 case FOR_STMT:
4257 if (FOR_INIT_STMT (t))
4258 cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), /*lval*/false,
4259 non_constant_p, overflow_p, jump_target);
4260 if (*non_constant_p)
4261 return NULL_TREE;
4262 body = FOR_BODY (t);
4263 cond = FOR_COND (t);
4264 expr = FOR_EXPR (t);
4265 count = -1;
4266 break;
4267 default:
4268 gcc_unreachable ();
4269 }
0ee28590 4270 auto_vec<tree, 10> save_exprs;
43574e4f 4271 new_ctx.save_exprs = &save_exprs;
d259b234 4272 do
56632b27 4273 {
43574e4f
JJ
4274 if (count != -1)
4275 {
4276 if (body)
4277 cxx_eval_constant_expression (&new_ctx, body, /*lval*/false,
4278 non_constant_p, overflow_p,
4279 jump_target);
4280 if (breaks (jump_target))
4281 {
4282 *jump_target = NULL_TREE;
4283 break;
4284 }
39dce2b7 4285
43574e4f
JJ
4286 if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target))
4287 *jump_target = NULL_TREE;
4288
4289 if (expr)
4290 cxx_eval_constant_expression (&new_ctx, expr, /*lval*/false,
4291 non_constant_p, overflow_p,
4292 jump_target);
4293 }
4294
4295 if (cond)
4296 {
4297 tree res
4298 = cxx_eval_constant_expression (&new_ctx, cond, /*lval*/false,
4299 non_constant_p, overflow_p,
4300 jump_target);
4301 if (res)
4302 {
4303 if (verify_constant (res, ctx->quiet, non_constant_p,
4304 overflow_p))
4305 break;
4306 if (integer_zerop (res))
4307 break;
4308 }
4309 else
4310 gcc_assert (*jump_target);
4311 }
39dce2b7
JM
4312
4313 /* Forget saved values of SAVE_EXPRs. */
0ee28590
JJ
4314 unsigned int i;
4315 tree save_expr;
4316 FOR_EACH_VEC_ELT (save_exprs, i, save_expr)
4317 new_ctx.values->remove (save_expr);
4318 save_exprs.truncate (0);
43574e4f 4319
5ec2cd9f
JM
4320 if (++count >= constexpr_loop_limit)
4321 {
4322 if (!ctx->quiet)
6bdfada4 4323 error_at (cp_expr_loc_or_loc (t, input_location),
84fa214d 4324 "%<constexpr%> loop iteration count exceeds limit of %d "
a3f9f006 4325 "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
5ec2cd9f
JM
4326 constexpr_loop_limit);
4327 *non_constant_p = true;
4328 break;
4329 }
56632b27 4330 }
4b390698
JJ
4331 while (!returns (jump_target)
4332 && !breaks (jump_target)
43574e4f
JJ
4333 && !continues (jump_target)
4334 && (!switches (jump_target) || count == 0)
4b390698 4335 && !*non_constant_p);
d259b234 4336
43574e4f 4337 /* Forget saved values of SAVE_EXPRs. */
0ee28590
JJ
4338 unsigned int i;
4339 tree save_expr;
4340 FOR_EACH_VEC_ELT (save_exprs, i, save_expr)
4341 new_ctx.values->remove (save_expr);
39dce2b7 4342
56632b27
JM
4343 return NULL_TREE;
4344}
4345
4346/* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
4347 semantics. */
4348
4349static tree
4350cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
56632b27
JM
4351 bool *non_constant_p, bool *overflow_p,
4352 tree *jump_target)
4353{
43574e4f
JJ
4354 tree cond
4355 = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t);
2b3ab879 4356 cond = cxx_eval_constant_expression (ctx, cond, false,
5a804683 4357 non_constant_p, overflow_p);
56632b27
JM
4358 VERIFY_CONSTANT (cond);
4359 *jump_target = cond;
4360
43574e4f
JJ
4361 tree body
4362 = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t);
4b390698
JJ
4363 constexpr_ctx new_ctx = *ctx;
4364 constexpr_switch_state css = css_default_not_seen;
4365 new_ctx.css_state = &css;
4366 cxx_eval_constant_expression (&new_ctx, body, false,
4367 non_constant_p, overflow_p, jump_target);
4368 if (switches (jump_target) && css == css_default_seen)
4369 {
4370 /* If the SWITCH_EXPR body has default: label, process it once again,
4371 this time instructing label_matches to return true for default:
4372 label on switches (jump_target). */
4373 css = css_default_processing;
4374 cxx_eval_constant_expression (&new_ctx, body, false,
4375 non_constant_p, overflow_p, jump_target);
4376 }
56632b27
JM
4377 if (breaks (jump_target) || switches (jump_target))
4378 *jump_target = NULL_TREE;
4379 return NULL_TREE;
4380}
4381
89262ec6
JM
4382/* Find the object of TYPE under initialization in CTX. */
4383
4384static tree
4385lookup_placeholder (const constexpr_ctx *ctx, bool lval, tree type)
4386{
fbd603c4 4387 if (!ctx)
89262ec6
JM
4388 return NULL_TREE;
4389
4390 /* We could use ctx->object unconditionally, but using ctx->ctor when we
4391 can is a minor optimization. */
fbd603c4 4392 if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type))
89262ec6
JM
4393 return ctx->ctor;
4394
fbd603c4
JM
4395 if (!ctx->object)
4396 return NULL_TREE;
4397
89262ec6
JM
4398 /* Since an object cannot have a field of its own type, we can search outward
4399 from ctx->object to find the unique containing object of TYPE. */
4400 tree ob = ctx->object;
4401 while (ob)
4402 {
4403 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type))
4404 break;
4405 if (handled_component_p (ob))
4406 ob = TREE_OPERAND (ob, 0);
4407 else
4408 ob = NULL_TREE;
4409 }
4410
4411 return ob;
4412}
4413
2d76680f
PC
4414/* Attempt to reduce the expression T to a constant value.
4415 On failure, issue diagnostic and return error_mark_node. */
4416/* FIXME unify with c_fully_fold */
60813a46 4417/* FIXME overflow_p is too global */
2d76680f
PC
4418
4419static tree
3e605b20 4420cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
92a596e8 4421 bool lval,
56632b27 4422 bool *non_constant_p, bool *overflow_p,
3075affd 4423 tree *jump_target /* = NULL */)
2d76680f 4424{
4b390698
JJ
4425 if (jump_target && *jump_target)
4426 {
4427 /* If we are jumping, ignore all statements/expressions except those
4428 that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
4429 switch (TREE_CODE (t))
4430 {
4431 case BIND_EXPR:
4432 case STATEMENT_LIST:
4433 case LOOP_EXPR:
4434 case COND_EXPR:
43574e4f
JJ
4435 case IF_STMT:
4436 case DO_STMT:
4437 case WHILE_STMT:
4438 case FOR_STMT:
4b390698
JJ
4439 break;
4440 case LABEL_EXPR:
4441 case CASE_LABEL_EXPR:
4442 if (label_matches (ctx, jump_target, t))
4443 /* Found it. */
4444 *jump_target = NULL_TREE;
4445 return NULL_TREE;
4446 default:
4447 return NULL_TREE;
4448 }
4449 }
3075affd 4450 if (error_operand_p (t))
2d76680f
PC
4451 {
4452 *non_constant_p = true;
4453 return t;
4454 }
7a649ef5
JM
4455
4456 STRIP_ANY_LOCATION_WRAPPER (t);
4457
2d76680f
PC
4458 if (CONSTANT_CLASS_P (t))
4459 {
61637db3
JJ
4460 if (TREE_OVERFLOW (t))
4461 {
4462 if (!ctx->quiet)
4463 permerror (input_location, "overflow in constant expression");
4464 if (!flag_permissive || ctx->quiet)
4465 *overflow_p = true;
4466 }
8bada5cd
MS
4467
4468 if (TREE_CODE (t) == INTEGER_CST
9f613f06 4469 && TYPE_PTR_P (TREE_TYPE (t))
8bada5cd
MS
4470 && !integer_zerop (t))
4471 {
4472 if (!ctx->quiet)
4473 error ("value %qE of type %qT is not a constant expression",
4474 t, TREE_TYPE (t));
4475 *non_constant_p = true;
4476 }
4477
2d76680f
PC
4478 return t;
4479 }
2d76680f 4480
a15ffa22 4481 /* Avoid excessively long constexpr evaluations. */
7a649ef5 4482 if (++*ctx->constexpr_ops_count >= constexpr_ops_limit)
a15ffa22
JJ
4483 {
4484 if (!ctx->quiet)
4485 error_at (cp_expr_loc_or_loc (t, input_location),
4486 "%<constexpr%> evaluation operation count exceeds limit of "
a9c697b8 4487 "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
a15ffa22
JJ
4488 constexpr_ops_limit);
4489 *ctx->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT);
4490 *non_constant_p = true;
4491 return t;
4492 }
4493
7a649ef5
JM
4494 constexpr_ctx new_ctx;
4495 tree r = t;
4496
8bada5cd
MS
4497 tree_code tcode = TREE_CODE (t);
4498 switch (tcode)
2d76680f 4499 {
60813a46 4500 case RESULT_DECL:
92a596e8 4501 if (lval)
60813a46
JM
4502 return t;
4503 /* We ask for an rvalue for the RESULT_DECL when indirecting
bf66b9b4
AH
4504 through an invisible reference, or in named return value
4505 optimization. */
1efb1dc2
MP
4506 if (tree *p = ctx->values->get (t))
4507 return *p;
4508 else
4509 {
4510 if (!ctx->quiet)
4511 error ("%qE is not a constant expression", t);
4512 *non_constant_p = true;
4513 }
4514 break;
60813a46 4515
2d76680f 4516 case VAR_DECL:
70f40fea 4517 if (DECL_HAS_VALUE_EXPR_P (t))
c59fa7ea
JM
4518 {
4519 if (is_normal_capture_proxy (t)
4520 && current_function_decl == DECL_CONTEXT (t))
4521 {
4522 /* Function parms aren't constexpr within the function
4523 definition, so don't try to look at the closure. But if the
4524 captured variable is constant, try to evaluate it directly. */
4525 r = DECL_CAPTURED_VARIABLE (t);
4526 tree type = TREE_TYPE (t);
4527 if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r)))
4528 {
4529 /* Adjust r to match the reference-ness of t. */
4530 if (TYPE_REF_P (type))
4531 r = build_address (r);
4532 else
4533 r = convert_from_reference (r);
4534 }
4535 }
4536 else
4537 r = DECL_VALUE_EXPR (t);
4538 return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
4539 overflow_p);
4540 }
191816a3 4541 /* fall through */
7c83622c
JM
4542 case CONST_DECL:
4543 /* We used to not check lval for CONST_DECL, but darwin.c uses
4544 CONST_DECL for aggregate constants. */
92a596e8 4545 if (lval)
2d76680f 4546 return t;
f4fce183 4547 if (COMPLETE_TYPE_P (TREE_TYPE (t))
dbcd32f8 4548 && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
7dc2b4a2
JM
4549 {
4550 /* If the class is empty, we aren't actually loading anything. */
4551 r = build_constructor (TREE_TYPE (t), NULL);
4552 TREE_CONSTANT (r) = true;
4553 }
4554 else if (ctx->strict)
69eb4fde
JM
4555 r = decl_really_constant_value (t);
4556 else
4557 r = decl_constant_value (t);
2d76680f
PC
4558 if (TREE_CODE (r) == TARGET_EXPR
4559 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
4560 r = TARGET_EXPR_INITIAL (r);
56a6f1d3 4561 if (VAR_P (r))
3e605b20 4562 if (tree *p = ctx->values->get (r))
bd8f5bb2
MP
4563 if (*p != NULL_TREE)
4564 r = *p;
2d76680f
PC
4565 if (DECL_P (r))
4566 {
2b3ab879 4567 if (!ctx->quiet)
2d76680f
PC
4568 non_const_var_error (r);
4569 *non_constant_p = true;
4570 }
4571 break;
4572
96a95ac1
AO
4573 case DEBUG_BEGIN_STMT:
4574 /* ??? It might be nice to retain this information somehow, so
4575 as to be able to step into a constexpr function call. */
4576 /* Fall through. */
4577
2d76680f
PC
4578 case FUNCTION_DECL:
4579 case TEMPLATE_DECL:
4580 case LABEL_DECL:
56632b27
JM
4581 case LABEL_EXPR:
4582 case CASE_LABEL_EXPR:
4b390698 4583 case PREDICT_EXPR:
2d76680f
PC
4584 return t;
4585
4586 case PARM_DECL:
9f613f06 4587 if (lval && !TYPE_REF_P (TREE_TYPE (t)))
60813a46
JM
4588 /* glvalue use. */;
4589 else if (tree *p = ctx->values->get (r))
4590 r = *p;
92a596e8 4591 else if (lval)
2d76680f 4592 /* Defer in case this is only used for its type. */;
9f613f06 4593 else if (TYPE_REF_P (TREE_TYPE (t)))
f2acb8ad 4594 /* Defer, there's no lvalue->rvalue conversion. */;
1c2f613f 4595 else if (COMPLETE_TYPE_P (TREE_TYPE (t))
dbcd32f8 4596 && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
052beba4
JM
4597 {
4598 /* If the class is empty, we aren't actually loading anything. */
4599 r = build_constructor (TREE_TYPE (t), NULL);
4600 TREE_CONSTANT (r) = true;
4601 }
2d76680f
PC
4602 else
4603 {
2b3ab879 4604 if (!ctx->quiet)
2d76680f
PC
4605 error ("%qE is not a constant expression", t);
4606 *non_constant_p = true;
4607 }
4608 break;
4609
4610 case CALL_EXPR:
4611 case AGGR_INIT_EXPR:
92a596e8 4612 r = cxx_eval_call_expression (ctx, t, lval,
2d76680f
PC
4613 non_constant_p, overflow_p);
4614 break;
4615
60813a46
JM
4616 case DECL_EXPR:
4617 {
4618 r = DECL_EXPR_DECL (t);
43574e4f
JJ
4619 if (TREE_CODE (r) == USING_DECL)
4620 {
4621 r = void_node;
4622 break;
4623 }
60813a46
JM
4624 if (AGGREGATE_TYPE_P (TREE_TYPE (r))
4625 || VECTOR_TYPE_P (TREE_TYPE (r)))
4626 {
4627 new_ctx = *ctx;
4628 new_ctx.object = r;
4629 new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
e8c48716 4630 CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
60813a46
JM
4631 new_ctx.values->put (r, new_ctx.ctor);
4632 ctx = &new_ctx;
4633 }
4634
4635 if (tree init = DECL_INITIAL (r))
4636 {
4637 init = cxx_eval_constant_expression (ctx, init,
2b3ab879 4638 false,
5a804683 4639 non_constant_p, overflow_p);
7f0e23e9 4640 /* Don't share a CONSTRUCTOR that might be changed. */
0146e25f 4641 init = unshare_constructor (init);
60813a46
JM
4642 ctx->values->put (r, init);
4643 }
4644 else if (ctx == &new_ctx)
4645 /* We gave it a CONSTRUCTOR above. */;
4646 else
4647 ctx->values->put (r, NULL_TREE);
4648 }
4649 break;
4650
2d76680f
PC
4651 case TARGET_EXPR:
4652 if (!literal_type_p (TREE_TYPE (t)))
4653 {
2b3ab879 4654 if (!ctx->quiet)
2d76680f 4655 {
097f82ec 4656 auto_diagnostic_group d;
2d76680f
PC
4657 error ("temporary of non-literal type %qT in a "
4658 "constant expression", TREE_TYPE (t));
4659 explain_non_literal_class (TREE_TYPE (t));
4660 }
4661 *non_constant_p = true;
4662 break;
4663 }
3e605b20
JM
4664 if ((AGGREGATE_TYPE_P (TREE_TYPE (t)) || VECTOR_TYPE_P (TREE_TYPE (t))))
4665 {
4666 /* We're being expanded without an explicit target, so start
4667 initializing a new object; expansion with an explicit target
4668 strips the TARGET_EXPR before we get here. */
4669 new_ctx = *ctx;
4670 new_ctx.ctor = build_constructor (TREE_TYPE (t), NULL);
e8c48716 4671 CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
3e605b20
JM
4672 new_ctx.object = TARGET_EXPR_SLOT (t);
4673 ctx->values->put (new_ctx.object, new_ctx.ctor);
4674 ctx = &new_ctx;
4675 }
92a596e8 4676 /* Pass false for 'lval' because this indicates
2d76680f 4677 initialization of a temporary. */
3e605b20 4678 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
2b3ab879 4679 false,
5a804683 4680 non_constant_p, overflow_p);
2d76680f
PC
4681 if (!*non_constant_p)
4682 /* Adjust the type of the result to the type of the temporary. */
4683 r = adjust_temp_type (TREE_TYPE (t), r);
92a596e8 4684 if (lval)
f2acb8ad
JM
4685 {
4686 tree slot = TARGET_EXPR_SLOT (t);
0146e25f 4687 r = unshare_constructor (r);
f2acb8ad
JM
4688 ctx->values->put (slot, r);
4689 return slot;
4690 }
2d76680f
PC
4691 break;
4692
60813a46 4693 case INIT_EXPR:
60813a46 4694 case MODIFY_EXPR:
4b390698 4695 gcc_assert (jump_target == NULL || *jump_target == NULL_TREE);
92a596e8 4696 r = cxx_eval_store_expression (ctx, t, lval,
60813a46
JM
4697 non_constant_p, overflow_p);
4698 break;
4699
2d76680f 4700 case SCOPE_REF:
3e605b20 4701 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
92a596e8 4702 lval,
5a804683 4703 non_constant_p, overflow_p);
2d76680f
PC
4704 break;
4705
4706 case RETURN_EXPR:
75e0295b
MP
4707 if (TREE_OPERAND (t, 0) != NULL_TREE)
4708 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
4709 lval,
4710 non_constant_p, overflow_p);
43574e4f
JJ
4711 /* FALLTHRU */
4712 case BREAK_STMT:
4713 case CONTINUE_STMT:
019e0ae8
MP
4714 if (jump_target)
4715 *jump_target = t;
4716 else
4717 {
4718 /* Can happen with ({ return true; }) && false; passed to
4719 maybe_constant_value. There is nothing to jump over in this
4720 case, and the bug will be diagnosed later. */
4721 gcc_assert (ctx->quiet);
4722 *non_constant_p = true;
4723 }
56632b27
JM
4724 break;
4725
cabaf50a
JM
4726 case SAVE_EXPR:
4727 /* Avoid evaluating a SAVE_EXPR more than once. */
4728 if (tree *p = ctx->values->get (t))
4729 r = *p;
4730 else
4731 {
12d9ce19 4732 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false,
cabaf50a
JM
4733 non_constant_p, overflow_p);
4734 ctx->values->put (t, r);
39dce2b7 4735 if (ctx->save_exprs)
0ee28590 4736 ctx->save_exprs->safe_push (t);
cabaf50a
JM
4737 }
4738 break;
4739
2d76680f
PC
4740 case NON_LVALUE_EXPR:
4741 case TRY_CATCH_EXPR:
769430b2 4742 case TRY_BLOCK:
2d76680f
PC
4743 case CLEANUP_POINT_EXPR:
4744 case MUST_NOT_THROW_EXPR:
60813a46
JM
4745 case EXPR_STMT:
4746 case EH_SPEC_BLOCK:
3e605b20 4747 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
92a596e8 4748 lval,
56632b27
JM
4749 non_constant_p, overflow_p,
4750 jump_target);
2d76680f
PC
4751 break;
4752
769430b2
JM
4753 case TRY_FINALLY_EXPR:
4754 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
4755 non_constant_p, overflow_p,
4756 jump_target);
4757 if (!*non_constant_p)
4758 /* Also evaluate the cleanup. */
4759 cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), true,
4760 non_constant_p, overflow_p,
4761 jump_target);
4762 break;
4763
43574e4f
JJ
4764 case CLEANUP_STMT:
4765 r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval,
4766 non_constant_p, overflow_p,
4767 jump_target);
4768 if (!CLEANUP_EH_ONLY (t) && !*non_constant_p)
4769 /* Also evaluate the cleanup. */
4770 cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), true,
4771 non_constant_p, overflow_p,
4772 jump_target);
4773 break;
4774
2d76680f
PC
4775 /* These differ from cxx_eval_unary_expression in that this doesn't
4776 check for a constant operand or result; an address can be
4777 constant without its operand being, and vice versa. */
cda0a029 4778 case MEM_REF:
2d76680f 4779 case INDIRECT_REF:
92a596e8 4780 r = cxx_eval_indirect_ref (ctx, t, lval,
2d76680f
PC
4781 non_constant_p, overflow_p);
4782 break;
4783
4784 case ADDR_EXPR:
4785 {
4786 tree oldop = TREE_OPERAND (t, 0);
3e605b20 4787 tree op = cxx_eval_constant_expression (ctx, oldop,
92a596e8 4788 /*lval*/true,
5a804683 4789 non_constant_p, overflow_p);
2d76680f
PC
4790 /* Don't VERIFY_CONSTANT here. */
4791 if (*non_constant_p)
4792 return t;
f2acb8ad 4793 gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
2d76680f
PC
4794 /* This function does more aggressive folding than fold itself. */
4795 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
4796 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
7a649ef5
JM
4797 {
4798 ggc_free (r);
4799 return t;
4800 }
2d76680f
PC
4801 break;
4802 }
4803
4804 case REALPART_EXPR:
4805 case IMAGPART_EXPR:
87713c6a
JJ
4806 if (lval)
4807 {
4808 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
4809 non_constant_p, overflow_p);
4810 if (r == error_mark_node)
4811 ;
4812 else if (r == TREE_OPERAND (t, 0))
4813 r = t;
4814 else
4815 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r);
4816 break;
4817 }
4818 /* FALLTHRU */
2d76680f
PC
4819 case CONJ_EXPR:
4820 case FIX_TRUNC_EXPR:
4821 case FLOAT_EXPR:
4822 case NEGATE_EXPR:
4823 case ABS_EXPR:
e28fadbc 4824 case ABSU_EXPR:
2d76680f
PC
4825 case BIT_NOT_EXPR:
4826 case TRUTH_NOT_EXPR:
4827 case FIXED_CONVERT_EXPR:
92a596e8 4828 r = cxx_eval_unary_expression (ctx, t, lval,
2d76680f
PC
4829 non_constant_p, overflow_p);
4830 break;
4831
4832 case SIZEOF_EXPR:
99e764a2
MP
4833 r = fold_sizeof_expr (t);
4834 /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
4835 which could lead to an infinite recursion. */
4836 if (TREE_CODE (r) != SIZEOF_EXPR)
4837 r = cxx_eval_constant_expression (ctx, r, lval,
4838 non_constant_p, overflow_p,
4839 jump_target);
4840 else
4841 {
4842 *non_constant_p = true;
4843 gcc_assert (ctx->quiet);
4844 }
4845
2d76680f
PC
4846 break;
4847
4848 case COMPOUND_EXPR:
4849 {
4850 /* check_return_expr sometimes wraps a TARGET_EXPR in a
4851 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
4852 introduced by build_call_a. */
4853 tree op0 = TREE_OPERAND (t, 0);
4854 tree op1 = TREE_OPERAND (t, 1);
4855 STRIP_NOPS (op1);
4856 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
4857 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
2b3ab879 4858 r = cxx_eval_constant_expression (ctx, op0,
92a596e8 4859 lval, non_constant_p, overflow_p,
56632b27 4860 jump_target);
2d76680f
PC
4861 else
4862 {
4863 /* Check that the LHS is constant and then discard it. */
2b3ab879 4864 cxx_eval_constant_expression (ctx, op0,
12d9ce19 4865 true, non_constant_p, overflow_p,
56632b27 4866 jump_target);
06ec22b7
JM
4867 if (*non_constant_p)
4868 return t;
2d76680f 4869 op1 = TREE_OPERAND (t, 1);
2b3ab879 4870 r = cxx_eval_constant_expression (ctx, op1,
92a596e8 4871 lval, non_constant_p, overflow_p,
56632b27 4872 jump_target);
2d76680f
PC
4873 }
4874 }
4875 break;
4876
4877 case POINTER_PLUS_EXPR:
1af4ebf5 4878 case POINTER_DIFF_EXPR:
2d76680f
PC
4879 case PLUS_EXPR:
4880 case MINUS_EXPR:
4881 case MULT_EXPR:
4882 case TRUNC_DIV_EXPR:
4883 case CEIL_DIV_EXPR:
4884 case FLOOR_DIV_EXPR:
4885 case ROUND_DIV_EXPR:
4886 case TRUNC_MOD_EXPR:
4887 case CEIL_MOD_EXPR:
4888 case ROUND_MOD_EXPR:
4889 case RDIV_EXPR:
4890 case EXACT_DIV_EXPR:
4891 case MIN_EXPR:
4892 case MAX_EXPR:
4893 case LSHIFT_EXPR:
4894 case RSHIFT_EXPR:
4895 case LROTATE_EXPR:
4896 case RROTATE_EXPR:
4897 case BIT_IOR_EXPR:
4898 case BIT_XOR_EXPR:
4899 case BIT_AND_EXPR:
4900 case TRUTH_XOR_EXPR:
4901 case LT_EXPR:
4902 case LE_EXPR:
4903 case GT_EXPR:
4904 case GE_EXPR:
4905 case EQ_EXPR:
4906 case NE_EXPR:
4907 case UNORDERED_EXPR:
4908 case ORDERED_EXPR:
4909 case UNLT_EXPR:
4910 case UNLE_EXPR:
4911 case UNGT_EXPR:
4912 case UNGE_EXPR:
4913 case UNEQ_EXPR:
4914 case LTGT_EXPR:
4915 case RANGE_EXPR:
4916 case COMPLEX_EXPR:
92a596e8 4917 r = cxx_eval_binary_expression (ctx, t, lval,
2d76680f
PC
4918 non_constant_p, overflow_p);
4919 break;
4920
4921 /* fold can introduce non-IF versions of these; still treat them as
4922 short-circuiting. */
4923 case TRUTH_AND_EXPR:
4924 case TRUTH_ANDIF_EXPR:
3e605b20 4925 r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
2d76680f 4926 boolean_true_node,
92a596e8 4927 lval,
2d76680f
PC
4928 non_constant_p, overflow_p);
4929 break;
4930
4931 case TRUTH_OR_EXPR:
4932 case TRUTH_ORIF_EXPR:
3e605b20 4933 r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
2d76680f 4934 boolean_false_node,
92a596e8 4935 lval,
2d76680f
PC
4936 non_constant_p, overflow_p);
4937 break;
4938
4939 case ARRAY_REF:
92a596e8 4940 r = cxx_eval_array_reference (ctx, t, lval,
2d76680f
PC
4941 non_constant_p, overflow_p);
4942 break;
4943
4944 case COMPONENT_REF:
4945 if (is_overloaded_fn (t))
4946 {
4947 /* We can only get here in checking mode via
4948 build_non_dependent_expr, because any expression that
4949 calls or takes the address of the function will have
4950 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
2b3ab879 4951 gcc_checking_assert (ctx->quiet || errorcount);
2d76680f
PC
4952 *non_constant_p = true;
4953 return t;
4954 }
92a596e8 4955 r = cxx_eval_component_reference (ctx, t, lval,
2d76680f
PC
4956 non_constant_p, overflow_p);
4957 break;
4958
4959 case BIT_FIELD_REF:
92a596e8 4960 r = cxx_eval_bit_field_ref (ctx, t, lval,
2d76680f
PC
4961 non_constant_p, overflow_p);
4962 break;
4963
4964 case COND_EXPR:
43574e4f 4965 case IF_STMT:
4b390698
JJ
4966 if (jump_target && *jump_target)
4967 {
e9fa2f6d 4968 tree orig_jump = *jump_target;
43574e4f
JJ
4969 tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1))
4970 ? TREE_OPERAND (t, 1) : void_node);
4b390698
JJ
4971 /* When jumping to a label, the label might be either in the
4972 then or else blocks, so process then block first in skipping
4973 mode first, and if we are still in the skipping mode at its end,
4974 process the else block too. */
43574e4f
JJ
4975 r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
4976 overflow_p, jump_target);
e9fa2f6d
MP
4977 /* It's possible that we found the label in the then block. But
4978 it could have been followed by another jumping statement, e.g.
4979 say we're looking for case 1:
4980 if (cond)
4981 {
4982 // skipped statements
4983 case 1:; // clears up *jump_target
4984 return 1; // and sets it to a RETURN_EXPR
4985 }
4986 else { ... }
4987 in which case we need not go looking to the else block.
4988 (goto is not allowed in a constexpr function.) */
4989 if (*jump_target == orig_jump)
43574e4f
JJ
4990 {
4991 arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2))
4992 ? TREE_OPERAND (t, 2) : void_node);
4993 r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
4994 overflow_p, jump_target);
4995 }
4b390698
JJ
4996 break;
4997 }
92a596e8 4998 r = cxx_eval_conditional_expression (ctx, t, lval,
56632b27
JM
4999 non_constant_p, overflow_p,
5000 jump_target);
2d76680f 5001 break;
f370e36d
JJ
5002 case VEC_COND_EXPR:
5003 r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p,
5004 overflow_p);
5005 break;
2d76680f
PC
5006
5007 case CONSTRUCTOR:
35d87f01 5008 if (TREE_CONSTANT (t) && reduced_constant_expression_p (t))
2d63bc39
JM
5009 {
5010 /* Don't re-process a constant CONSTRUCTOR, but do fold it to
5011 VECTOR_CST if applicable. */
ac9ec198 5012 verify_constructor_flags (t);
2d63bc39
JM
5013 if (TREE_CONSTANT (t))
5014 return fold (t);
5015 }
92a596e8 5016 r = cxx_eval_bare_aggregate (ctx, t, lval,
2d76680f
PC
5017 non_constant_p, overflow_p);
5018 break;
5019
5020 case VEC_INIT_EXPR:
5021 /* We can get this in a defaulted constructor for a class with a
5022 non-static data member of array type. Either the initializer will
5023 be NULL, meaning default-initialization, or it will be an lvalue
5024 or xvalue of the same type, meaning direct-initialization from the
5025 corresponding member. */
92a596e8 5026 r = cxx_eval_vec_init (ctx, t, lval,
2d76680f
PC
5027 non_constant_p, overflow_p);
5028 break;
5029
2d76680f 5030 case VEC_PERM_EXPR:
92a596e8 5031 r = cxx_eval_trinary_expression (ctx, t, lval,
2d76680f
PC
5032 non_constant_p, overflow_p);
5033 break;
5034
7d75ea04
JJ
5035 case NOP_EXPR:
5036 if (REINTERPRET_CAST_P (t))
5037 {
5038 if (!ctx->quiet)
6bdfada4 5039 error_at (cp_expr_loc_or_loc (t, input_location),
a9c697b8 5040 "%<reinterpret_cast%> is not a constant expression");
7d75ea04
JJ
5041 *non_constant_p = true;
5042 return t;
5043 }
5044 /* FALLTHROUGH. */
2d76680f
PC
5045 case CONVERT_EXPR:
5046 case VIEW_CONVERT_EXPR:
cda0a029 5047 case UNARY_PLUS_EXPR:
2d76680f
PC
5048 {
5049 tree oldop = TREE_OPERAND (t, 0);
cda0a029 5050
3e605b20 5051 tree op = cxx_eval_constant_expression (ctx, oldop,
92a596e8 5052 lval,
5a804683 5053 non_constant_p, overflow_p);
2d76680f
PC
5054 if (*non_constant_p)
5055 return t;
dcdbc004 5056 tree type = TREE_TYPE (t);
02a8575c
JM
5057
5058 if (VOID_TYPE_P (type))
5059 return void_node;
5060
dcdbc004
JM
5061 if (TREE_CODE (op) == PTRMEM_CST
5062 && !TYPE_PTRMEM_P (type))
5063 op = cplus_expand_constant (op);
7d75ea04 5064
05bf54c3
MP
5065 if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR)
5066 {
7d75ea04
JJ
5067 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op))
5068 && !can_convert_qual (type, op))
5069 op = cplus_expand_constant (op);
5070 return cp_fold_convert (type, op);
05bf54c3 5071 }
8bada5cd 5072
71a93b08 5073 if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST)
2d76680f 5074 {
8bada5cd
MS
5075 if (integer_zerop (op))
5076 {
9f613f06 5077 if (TYPE_REF_P (type))
8bada5cd
MS
5078 {
5079 if (!ctx->quiet)
6bdfada4 5080 error_at (cp_expr_loc_or_loc (t, input_location),
8bada5cd
MS
5081 "dereferencing a null pointer");
5082 *non_constant_p = true;
5083 return t;
5084 }
9f613f06 5085 else if (TYPE_PTR_P (TREE_TYPE (op)))
8bada5cd
MS
5086 {
5087 tree from = TREE_TYPE (op);
5088
5089 if (!can_convert (type, from, tf_none))
5090 {
5091 if (!ctx->quiet)
6bdfada4 5092 error_at (cp_expr_loc_or_loc (t, input_location),
8bada5cd
MS
5093 "conversion of %qT null pointer to %qT "
5094 "is not a constant expression",
5095 from, type);
5096 *non_constant_p = true;
5097 return t;
5098 }
5099 }
5100 }
5101 else
5102 {
5103 /* This detects for example:
5104 reinterpret_cast<void*>(sizeof 0)
5105 */
5106 if (!ctx->quiet)
6bdfada4 5107 error_at (cp_expr_loc_or_loc (t, input_location),
8bada5cd 5108 "%<reinterpret_cast<%T>(%E)%> is not "
64d6d399 5109 "a constant expression",
8bada5cd
MS
5110 type, op);
5111 *non_constant_p = true;
5112 return t;
5113 }
2d76680f 5114 }
7d75ea04 5115
cda0a029 5116 if (op == oldop && tcode != UNARY_PLUS_EXPR)
2d76680f
PC
5117 /* We didn't fold at the top so we could check for ptr-int
5118 conversion. */
5119 return fold (t);
7d75ea04 5120
02a8575c
JM
5121 tree sop;
5122
e4511ca2
JM
5123 /* Handle an array's bounds having been deduced after we built
5124 the wrapping expression. */
5125 if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op)))
5126 r = op;
02a8575c
JM
5127 else if (sop = tree_strip_nop_conversions (op),
5128 sop != op && (same_type_ignoring_tlq_and_bounds_p
5129 (type, TREE_TYPE (sop))))
5130 r = sop;
e4511ca2 5131 else if (tcode == UNARY_PLUS_EXPR)
cda0a029
JM
5132 r = fold_convert (TREE_TYPE (t), op);
5133 else
5134 r = fold_build1 (tcode, type, op);
7d75ea04 5135
2d76680f
PC
5136 /* Conversion of an out-of-range value has implementation-defined
5137 behavior; the language considers it different from arithmetic
5138 overflow, which is undefined. */
5139 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
5140 TREE_OVERFLOW (r) = false;
5141 }
5142 break;
5143
5144 case EMPTY_CLASS_EXPR:
5145 /* This is good enough for a function argument that might not get
5146 used, and they can't do anything with it, so just return it. */
5147 return t;
5148
60813a46 5149 case STATEMENT_LIST:
56632b27
JM
5150 new_ctx = *ctx;
5151 new_ctx.ctor = new_ctx.object = NULL_TREE;
2b3ab879 5152 return cxx_eval_statement_list (&new_ctx, t,
56632b27 5153 non_constant_p, overflow_p, jump_target);
60813a46
JM
5154
5155 case BIND_EXPR:
5156 return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
92a596e8 5157 lval,
56632b27
JM
5158 non_constant_p, overflow_p,
5159 jump_target);
60813a46 5160
2d76680f
PC
5161 case PREINCREMENT_EXPR:
5162 case POSTINCREMENT_EXPR:
5163 case PREDECREMENT_EXPR:
5164 case POSTDECREMENT_EXPR:
2b3ab879 5165 return cxx_eval_increment_expression (ctx, t,
92a596e8 5166 lval, non_constant_p, overflow_p);
60813a46
JM
5167
5168 case LAMBDA_EXPR:
2d76680f
PC
5169 case NEW_EXPR:
5170 case VEC_NEW_EXPR:
5171 case DELETE_EXPR:
5172 case VEC_DELETE_EXPR:
5173 case THROW_EXPR:
2d76680f
PC
5174 case MODOP_EXPR:
5175 /* GCC internal stuff. */
5176 case VA_ARG_EXPR:
2d76680f
PC
5177 case NON_DEPENDENT_EXPR:
5178 case BASELINK:
2d76680f 5179 case OFFSET_REF:
2b3ab879 5180 if (!ctx->quiet)
6bdfada4 5181 error_at (cp_expr_loc_or_loc (t, input_location),
64d6d399 5182 "expression %qE is not a constant expression", t);
2d76680f
PC
5183 *non_constant_p = true;
5184 break;
5185
bf8d8309
MP
5186 case OBJ_TYPE_REF:
5187 {
5188 /* Virtual function call. Let the constexpr machinery figure out
5189 the dynamic type. */
5190 int token = tree_to_shwi (OBJ_TYPE_REF_TOKEN (t));
5191 tree obj = OBJ_TYPE_REF_OBJECT (t);
5192 obj = cxx_eval_constant_expression (ctx, obj, lval, non_constant_p,
5193 overflow_p);
5194 /* We expect something in the form of &x.D.2103.D.2094; get x. */
6a3ebcc6
JJ
5195 if (TREE_CODE (obj) != ADDR_EXPR
5196 || !DECL_P (get_base_address (TREE_OPERAND (obj, 0))))
bf8d8309
MP
5197 {
5198 if (!ctx->quiet)
5199 error_at (cp_expr_loc_or_loc (t, input_location),
5200 "expression %qE is not a constant expression", t);
5201 *non_constant_p = true;
5202 return t;
5203 }
5204 obj = TREE_OPERAND (obj, 0);
69deaf14
JJ
5205 while (TREE_CODE (obj) == COMPONENT_REF
5206 && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1)))
bf8d8309
MP
5207 obj = TREE_OPERAND (obj, 0);
5208 tree objtype = TREE_TYPE (obj);
5209 /* Find the function decl in the virtual functions list. TOKEN is
5210 the DECL_VINDEX that says which function we're looking for. */
5211 tree virtuals = BINFO_VIRTUALS (TYPE_BINFO (objtype));
582d2481
JJ
5212 if (TARGET_VTABLE_USES_DESCRIPTORS)
5213 token /= MAX (TARGET_VTABLE_USES_DESCRIPTORS, 1);
bf8d8309
MP
5214 r = TREE_VALUE (chain_index (token, virtuals));
5215 break;
5216 }
5217
3e605b20 5218 case PLACEHOLDER_EXPR:
89262ec6
JM
5219 /* Use of the value or address of the current object. */
5220 if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t)))
5221 return cxx_eval_constant_expression (ctx, ctor, lval,
5222 non_constant_p, overflow_p);
5223 /* A placeholder without a referent. We can get here when
5224 checking whether NSDMIs are noexcept, or in massage_init_elt;
5225 just say it's non-constant for now. */
5226 gcc_assert (ctx->quiet);
5227 *non_constant_p = true;
3e605b20
JM
5228 break;
5229
06ec22b7
JM
5230 case EXIT_EXPR:
5231 {
5232 tree cond = TREE_OPERAND (t, 0);
5233 cond = cxx_eval_constant_expression (ctx, cond, /*lval*/false,
5234 non_constant_p, overflow_p);
5235 VERIFY_CONSTANT (cond);
5236 if (integer_nonzerop (cond))
5237 *jump_target = t;
5238 }
5239 break;
5240
60813a46 5241 case GOTO_EXPR:
56632b27 5242 *jump_target = TREE_OPERAND (t, 0);
d49318d9
PC
5243 gcc_assert (breaks (jump_target) || continues (jump_target)
5244 /* Allow for jumping to a cdtor_label. */
5245 || returns (jump_target));
56632b27
JM
5246 break;
5247
60813a46 5248 case LOOP_EXPR:
43574e4f
JJ
5249 case DO_STMT:
5250 case WHILE_STMT:
5251 case FOR_STMT:
2b3ab879 5252 cxx_eval_loop_expr (ctx, t,
56632b27
JM
5253 non_constant_p, overflow_p, jump_target);
5254 break;
5255
60813a46 5256 case SWITCH_EXPR:
43574e4f 5257 case SWITCH_STMT:
2b3ab879 5258 cxx_eval_switch_expr (ctx, t,
56632b27 5259 non_constant_p, overflow_p, jump_target);
60813a46
JM
5260 break;
5261
971e17ff
AS
5262 case REQUIRES_EXPR:
5263 /* It's possible to get a requires-expression in a constant
5264 expression. For example:
5265
5266 template<typename T> concept bool C() {
5267 return requires (T t) { t; };
5268 }
5269
5270 template<typename T> requires !C<T>() void f(T);
5271
5272 Normalization leaves f with the associated constraint
5273 '!requires (T t) { ... }' which is not transformed into
5274 a constraint. */
5275 if (!processing_template_decl)
5276 return evaluate_constraint_expression (t, NULL_TREE);
5277 else
5278 *non_constant_p = true;
5279 return t;
5280
98e09245 5281 case ANNOTATE_EXPR:
98e09245
JJ
5282 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
5283 lval,
5284 non_constant_p, overflow_p,
5285 jump_target);
5286 break;
5287
3664e317
JM
5288 case USING_STMT:
5289 r = void_node;
5290 break;
5291
2d76680f 5292 default:
c6e7c499
JM
5293 if (STATEMENT_CODE_P (TREE_CODE (t)))
5294 {
5295 /* This function doesn't know how to deal with pre-genericize
5296 statements; this can only happen with statement-expressions,
5297 so for now just fail. */
5298 if (!ctx->quiet)
5299 error_at (EXPR_LOCATION (t),
64d6d399 5300 "statement is not a constant expression");
c6e7c499
JM
5301 }
5302 else
5303 internal_error ("unexpected expression %qE of kind %s", t,
5304 get_tree_code_name (TREE_CODE (t)));
2d76680f
PC
5305 *non_constant_p = true;
5306 break;
5307 }
5308
5309 if (r == error_mark_node)
5310 *non_constant_p = true;
5311
5312 if (*non_constant_p)
5313 return t;
5314 else
5315 return r;
5316}
5317
e079dced
JM
5318/* P0859: A function is needed for constant evaluation if it is a constexpr
5319 function that is named by an expression ([basic.def.odr]) that is
5320 potentially constant evaluated.
5321
5322 So we need to instantiate any constexpr functions mentioned by the
5323 expression even if the definition isn't needed for evaluating the
5324 expression. */
5325
5326static tree
5327instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/)
5328{
5329 if (TREE_CODE (*tp) == FUNCTION_DECL
5330 && DECL_DECLARED_CONSTEXPR_P (*tp)
5331 && !DECL_INITIAL (*tp)
4fdda3ce 5332 && !trivial_fn_p (*tp)
e079dced
JM
5333 && DECL_TEMPLOID_INSTANTIATION (*tp))
5334 {
5335 ++function_depth;
5336 instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false);
5337 --function_depth;
5338 }
5339 else if (TREE_CODE (*tp) == CALL_EXPR
5340 || TREE_CODE (*tp) == AGGR_INIT_EXPR)
5341 {
5342 if (EXPR_HAS_LOCATION (*tp))
5343 input_location = EXPR_LOCATION (*tp);
5344 }
5345
5346 if (!EXPR_P (*tp))
5347 *walk_subtrees = 0;
5348
5349 return NULL_TREE;
5350}
5351static void
5352instantiate_constexpr_fns (tree t)
5353{
5354 location_t loc = input_location;
5355 cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL);
5356 input_location = loc;
5357}
5358
e4082611 5359/* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
d4accef3
JM
5360 STRICT has the same sense as for constant_value_1: true if we only allow
5361 conforming C++ constant expressions, or false if we want a constant value
5362 even if it doesn't conform.
13de99bc 5363 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
e4082611
JJ
5364 per P0595 even when ALLOW_NON_CONSTANT is true. */
5365
2d76680f 5366static tree
3e605b20 5367cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
e4082611 5368 bool strict = true,
13de99bc 5369 bool manifestly_const_eval = false,
e4082611 5370 tree object = NULL_TREE)
2d76680f 5371{
8108ea30
JM
5372 auto_timevar time (TV_CONSTEXPR);
5373
2d76680f
PC
5374 bool non_constant_p = false;
5375 bool overflow_p = false;
3e605b20 5376 hash_map<tree,tree> map;
a15ffa22 5377 HOST_WIDE_INT constexpr_ctx_count = 0;
39dce2b7 5378
4b390698 5379 constexpr_ctx ctx = { NULL, &map, NULL, NULL, NULL, NULL,
a15ffa22 5380 &constexpr_ctx_count, allow_non_constant, strict,
13de99bc 5381 manifestly_const_eval || !allow_non_constant };
39dce2b7 5382
3e605b20 5383 tree type = initialized_type (t);
3e605b20 5384 tree r = t;
4d0c18c6
JM
5385 if (VOID_TYPE_P (type))
5386 return t;
3e605b20
JM
5387 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
5388 {
5389 /* In C++14 an NSDMI can participate in aggregate initialization,
5390 and can refer to the address of the object being initialized, so
5391 we need to pass in the relevant VAR_DECL if we want to do the
5392 evaluation in a single pass. The evaluation will dynamically
5393 update ctx.values for the VAR_DECL. We use the same strategy
5394 for C++11 constexpr constructors that refer to the object being
5395 initialized. */
5396 ctx.ctor = build_constructor (type, NULL);
e8c48716 5397 CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true;
60813a46
JM
5398 if (!object)
5399 {
5400 if (TREE_CODE (t) == TARGET_EXPR)
5401 object = TARGET_EXPR_SLOT (t);
5402 else if (TREE_CODE (t) == AGGR_INIT_EXPR)
5403 object = AGGR_INIT_EXPR_SLOT (t);
5404 }
3e605b20
JM
5405 ctx.object = object;
5406 if (object)
5407 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5408 (type, TREE_TYPE (object)));
5409 if (object && DECL_P (object))
5410 map.put (object, ctx.ctor);
5411 if (TREE_CODE (r) == TARGET_EXPR)
5412 /* Avoid creating another CONSTRUCTOR when we expand the
5413 TARGET_EXPR. */
5414 r = TARGET_EXPR_INITIAL (r);
5415 }
5416
e079dced 5417 instantiate_constexpr_fns (r);
2b3ab879 5418 r = cxx_eval_constant_expression (&ctx, r,
5a804683 5419 false, &non_constant_p, &overflow_p);
2d76680f
PC
5420
5421 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
5422
023d89c7
JM
5423 /* Mutable logic is a bit tricky: we want to allow initialization of
5424 constexpr variables with mutable members, but we can't copy those
5425 members to another constexpr variable. */
5426 if (TREE_CODE (r) == CONSTRUCTOR
5427 && CONSTRUCTOR_MUTABLE_POISON (r))
2d76680f 5428 {
2d76680f 5429 if (!allow_non_constant)
023d89c7
JM
5430 error ("%qE is not a constant expression because it refers to "
5431 "mutable subobjects of %qT", t, type);
2d76680f
PC
5432 non_constant_p = true;
5433 }
5434
f64e0c02 5435 if (TREE_CODE (r) == CONSTRUCTOR
e8c48716 5436 && CONSTRUCTOR_NO_CLEARING (r))
f64e0c02
JM
5437 {
5438 if (!allow_non_constant)
5439 error ("%qE is not a constant expression because it refers to "
5440 "an incompletely initialized variable", t);
5441 TREE_CONSTANT (r) = false;
5442 non_constant_p = true;
5443 }
5444
2d76680f
PC
5445 /* Technically we should check this for all subexpressions, but that
5446 runs into problems with our internal representation of pointer
5447 subtraction and the 5.19 rules are still in flux. */
5448 if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
5449 && ARITHMETIC_TYPE_P (TREE_TYPE (r))
5450 && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
5451 {
5452 if (!allow_non_constant)
5453 error ("conversion from pointer type %qT "
64d6d399 5454 "to arithmetic type %qT in a constant expression",
2d76680f
PC
5455 TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
5456 non_constant_p = true;
5457 }
5458
5459 if (!non_constant_p && overflow_p)
5460 non_constant_p = true;
5461
56cfb596
PP
5462 /* Unshare the result unless it's a CONSTRUCTOR in which case it's already
5463 unshared. */
5464 bool should_unshare = true;
5465 if (r == t || TREE_CODE (r) == CONSTRUCTOR)
5466 should_unshare = false;
5467
2d76680f
PC
5468 if (non_constant_p && !allow_non_constant)
5469 return error_mark_node;
5470 else if (non_constant_p && TREE_CONSTANT (r))
5471 {
e4082611
JJ
5472 /* If __builtin_is_constant_evaluated () was evaluated to true
5473 and the result is not a valid constant expression, we need to
5474 punt. */
13de99bc 5475 if (manifestly_const_eval)
e4082611
JJ
5476 return cxx_eval_outermost_constant_expr (t, true, strict,
5477 false, object);
eddd715c
JM
5478 /* This isn't actually constant, so unset TREE_CONSTANT.
5479 Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
5480 it to be set if it is invariant address, even when it is not
5481 a valid C++ constant expression. Wrap it with a NOP_EXPR
5482 instead. */
5483 if (EXPR_P (r) && TREE_CODE (r) != ADDR_EXPR)
2d76680f
PC
5484 r = copy_node (r);
5485 else if (TREE_CODE (r) == CONSTRUCTOR)
5486 r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
5487 else
5488 r = build_nop (TREE_TYPE (r), r);
5489 TREE_CONSTANT (r) = false;
5490 }
5dab8b11 5491 else if (non_constant_p)
2d76680f
PC
5492 return t;
5493
56cfb596
PP
5494 if (should_unshare)
5495 r = unshare_expr (r);
5496
2d76680f
PC
5497 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
5498 {
5dab8b11 5499 r = adjust_temp_type (type, r);
2d76680f
PC
5500 if (TREE_CODE (t) == TARGET_EXPR
5501 && TARGET_EXPR_INITIAL (t) == r)
5502 return t;
5dab8b11 5503 else if (TREE_CODE (t) != CONSTRUCTOR)
2d76680f
PC
5504 {
5505 r = get_target_expr (r);
5506 TREE_CONSTANT (r) = true;
2d76680f
PC
5507 }
5508 }
5dab8b11
JM
5509
5510 return r;
2d76680f
PC
5511}
5512
2d76680f
PC
5513/* If T represents a constant expression returns its reduced value.
5514 Otherwise return error_mark_node. If T is dependent, then
5515 return NULL. */
5516
5517tree
3e605b20 5518cxx_constant_value (tree t, tree decl)
2d76680f 5519{
e4082611 5520 return cxx_eval_outermost_constant_expr (t, false, true, true, decl);
2d76680f
PC
5521}
5522
cda0a029
JM
5523/* Helper routine for fold_simple function. Either return simplified
5524 expression T, otherwise NULL_TREE.
5525 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
5526 even if we are within template-declaration. So be careful on call, as in
5527 such case types can be undefined. */
5528
5529static tree
5530fold_simple_1 (tree t)
5531{
5532 tree op1;
5533 enum tree_code code = TREE_CODE (t);
5534
5535 switch (code)
5536 {
5537 case INTEGER_CST:
5538 case REAL_CST:
5539 case VECTOR_CST:
5540 case FIXED_CST:
5541 case COMPLEX_CST:
5542 return t;
5543
5544 case SIZEOF_EXPR:
5545 return fold_sizeof_expr (t);
5546
5547 case ABS_EXPR:
e28fadbc 5548 case ABSU_EXPR:
cda0a029
JM
5549 case CONJ_EXPR:
5550 case REALPART_EXPR:
5551 case IMAGPART_EXPR:
5552 case NEGATE_EXPR:
5553 case BIT_NOT_EXPR:
5554 case TRUTH_NOT_EXPR:
5555 case NOP_EXPR:
5556 case VIEW_CONVERT_EXPR:
5557 case CONVERT_EXPR:
5558 case FLOAT_EXPR:
5559 case FIX_TRUNC_EXPR:
5560 case FIXED_CONVERT_EXPR:
5561 case ADDR_SPACE_CONVERT_EXPR:
5562
5563 op1 = TREE_OPERAND (t, 0);
5564
5565 t = const_unop (code, TREE_TYPE (t), op1);
5566 if (!t)
5567 return NULL_TREE;
5568
5569 if (CONVERT_EXPR_CODE_P (code)
5570 && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1))
5571 TREE_OVERFLOW (t) = false;
5572 return t;
5573
5574 default:
5575 return NULL_TREE;
5576 }
5577}
5578
5579/* If T is a simple constant expression, returns its simplified value.
dba99244 5580 Otherwise returns T. In contrast to maybe_constant_value we
cda0a029
JM
5581 simplify only few operations on constant-expressions, and we don't
5582 try to simplify constexpressions. */
5583
5584tree
5585fold_simple (tree t)
5586{
cda0a029
JM
5587 if (processing_template_decl)
5588 return t;
5589
dba99244
MP
5590 tree r = fold_simple_1 (t);
5591 if (r)
5592 return r;
cda0a029 5593
dba99244 5594 return t;
cda0a029
JM
5595}
5596
2d76680f
PC
5597/* If T is a constant expression, returns its reduced value.
5598 Otherwise, if T does not have TREE_CONSTANT set, returns T.
13de99bc
JJ
5599 Otherwise, returns a version of T without TREE_CONSTANT.
5600 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
5601 as per P0595. */
2d76680f 5602
8a87daca
JM
5603static GTY((deletable)) hash_map<tree, tree> *cv_cache;
5604
5605tree
13de99bc 5606maybe_constant_value (tree t, tree decl, bool manifestly_const_eval)
2d76680f
PC
5607{
5608 tree r;
5609
a0ab7ccd 5610 if (!is_nondependent_constant_expression (t))
2d76680f
PC
5611 {
5612 if (TREE_OVERFLOW_P (t))
5613 {
5614 t = build_nop (TREE_TYPE (t), t);
5615 TREE_CONSTANT (t) = false;
5616 }
5617 return t;
5618 }
8a87daca
JM
5619 else if (CONSTANT_CLASS_P (t))
5620 /* No caching or evaluation needed. */
5621 return t;
5622
13de99bc
JJ
5623 if (manifestly_const_eval)
5624 return cxx_eval_outermost_constant_expr (t, true, true, true, decl);
5625
8a87daca
JM
5626 if (cv_cache == NULL)
5627 cv_cache = hash_map<tree, tree>::create_ggc (101);
5628 if (tree *cached = cv_cache->get (t))
5629 return *cached;
2d76680f 5630
e4082611 5631 r = cxx_eval_outermost_constant_expr (t, true, true, false, decl);
595278be
MM
5632 gcc_checking_assert (r == t
5633 || CONVERT_EXPR_P (t)
5634 || TREE_CODE (t) == VIEW_CONVERT_EXPR
5635 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
5636 || !cp_tree_equal (r, t));
8a87daca 5637 cv_cache->put (t, r);
2d76680f
PC
5638 return r;
5639}
5640
7a7ac32a
PP
5641/* Dispose of the whole CV_CACHE. */
5642
5643static void
5644clear_cv_cache (void)
5645{
5646 if (cv_cache != NULL)
5647 cv_cache->empty ();
5648}
5649
eba9e839 5650/* Dispose of the whole CV_CACHE and FOLD_CACHE. */
1e297006
MP
5651
5652void
eba9e839 5653clear_cv_and_fold_caches (void)
1e297006 5654{
7a7ac32a 5655 clear_cv_cache ();
eba9e839 5656 clear_fold_cache ();
1e297006
MP
5657}
5658
a81c8e8c
MP
5659/* Internal function handling expressions in templates for
5660 fold_non_dependent_expr and fold_non_dependent_init.
5661
5662 If we're in a template, but T isn't value dependent, simplify
5663 it. We're supposed to treat:
5664
5665 template <typename T> void f(T[1 + 1]);
5666 template <typename T> void f(T[2]);
5667
5668 as two declarations of the same function, for example. */
5669
5670static tree
5671fold_non_dependent_expr_template (tree t, tsubst_flags_t complain,
5672 bool manifestly_const_eval)
5673{
5674 gcc_assert (processing_template_decl);
5675
5676 if (is_nondependent_constant_expression (t))
5677 {
5678 processing_template_decl_sentinel s;
5679 t = instantiate_non_dependent_expr_internal (t, complain);
5680
5681 if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t))
5682 {
5683 if (TREE_OVERFLOW_P (t))
5684 {
5685 t = build_nop (TREE_TYPE (t), t);
5686 TREE_CONSTANT (t) = false;
5687 }
5688 return t;
5689 }
5690
5691 tree r = cxx_eval_outermost_constant_expr (t, true, true,
5692 manifestly_const_eval,
5693 NULL_TREE);
5694 /* cp_tree_equal looks through NOPs, so allow them. */
5695 gcc_checking_assert (r == t
5696 || CONVERT_EXPR_P (t)
5697 || TREE_CODE (t) == VIEW_CONVERT_EXPR
5698 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
5699 || !cp_tree_equal (r, t));
5700 return r;
5701 }
5702 else if (TREE_OVERFLOW_P (t))
5703 {
5704 t = build_nop (TREE_TYPE (t), t);
5705 TREE_CONSTANT (t) = false;
5706 }
5707
5708 return t;
5709}
5710
234bef96
PC
5711/* Like maybe_constant_value but first fully instantiate the argument.
5712
5713 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
e56f6629
JM
5714 (t, complain) followed by maybe_constant_value but is more efficient,
5715 because it calls instantiation_dependent_expression_p and
5716 potential_constant_expression at most once.
4cd3e7df 5717 The manifestly_const_eval argument is passed to maybe_constant_value.
e56f6629
JM
5718
5719 Callers should generally pass their active complain, or if they are in a
5720 non-template, diagnosing context, they can use the default of
5721 tf_warning_or_error. Callers that might be within a template context, don't
5722 have a complain parameter, and aren't going to remember the result for long
5723 (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
5724 appropriately. */
234bef96
PC
5725
5726tree
e56f6629 5727fold_non_dependent_expr (tree t,
4cd3e7df
JJ
5728 tsubst_flags_t complain /* = tf_warning_or_error */,
5729 bool manifestly_const_eval /* = false */)
234bef96
PC
5730{
5731 if (t == NULL_TREE)
5732 return NULL_TREE;
5733
a81c8e8c
MP
5734 if (processing_template_decl)
5735 return fold_non_dependent_expr_template (t, complain,
5736 manifestly_const_eval);
234bef96 5737
a81c8e8c
MP
5738 return maybe_constant_value (t, NULL_TREE, manifestly_const_eval);
5739}
234bef96 5740
234bef96 5741
a81c8e8c 5742/* Like maybe_constant_init but first fully instantiate the argument. */
234bef96 5743
a81c8e8c
MP
5744tree
5745fold_non_dependent_init (tree t,
5746 tsubst_flags_t complain /*=tf_warning_or_error*/,
5747 bool manifestly_const_eval /*=false*/)
5748{
5749 if (t == NULL_TREE)
5750 return NULL_TREE;
5751
5752 if (processing_template_decl)
5753 {
5754 t = fold_non_dependent_expr_template (t, complain,
5755 manifestly_const_eval);
5756 /* maybe_constant_init does this stripping, so do it here too. */
5757 if (TREE_CODE (t) == TARGET_EXPR)
234bef96 5758 {
a81c8e8c
MP
5759 tree init = TARGET_EXPR_INITIAL (t);
5760 if (TREE_CODE (init) == CONSTRUCTOR)
5761 t = init;
234bef96
PC
5762 }
5763 return t;
5764 }
5765
a81c8e8c 5766 return maybe_constant_init (t, NULL_TREE, manifestly_const_eval);
234bef96
PC
5767}
5768
2d76680f 5769/* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
e4082611
JJ
5770 than wrapped in a TARGET_EXPR.
5771 ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
13de99bc 5772 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
e4082611 5773 per P0595 even when ALLOW_NON_CONSTANT is true. */
2d76680f 5774
118cd6ba 5775static tree
e4082611 5776maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant,
13de99bc 5777 bool manifestly_const_eval)
2d76680f 5778{
cda0a029
JM
5779 if (!t)
5780 return t;
2d76680f
PC
5781 if (TREE_CODE (t) == EXPR_STMT)
5782 t = TREE_OPERAND (t, 0);
5783 if (TREE_CODE (t) == CONVERT_EXPR
5784 && VOID_TYPE_P (TREE_TYPE (t)))
5785 t = TREE_OPERAND (t, 0);
60813a46
JM
5786 if (TREE_CODE (t) == INIT_EXPR)
5787 t = TREE_OPERAND (t, 1);
f64e0c02
JM
5788 if (TREE_CODE (t) == TARGET_EXPR)
5789 t = TARGET_EXPR_INITIAL (t);
a0ab7ccd 5790 if (!is_nondependent_static_init_expression (t))
69eb4fde 5791 /* Don't try to evaluate it. */;
118cd6ba 5792 else if (CONSTANT_CLASS_P (t) && allow_non_constant)
8a87daca 5793 /* No evaluation needed. */;
69eb4fde 5794 else
e4082611 5795 t = cxx_eval_outermost_constant_expr (t, allow_non_constant,
38295573 5796 /*strict*/false,
13de99bc 5797 manifestly_const_eval, decl);
2d76680f
PC
5798 if (TREE_CODE (t) == TARGET_EXPR)
5799 {
5800 tree init = TARGET_EXPR_INITIAL (t);
5801 if (TREE_CODE (init) == CONSTRUCTOR)
5802 t = init;
5803 }
5804 return t;
5805}
5806
118cd6ba
MP
5807/* Wrapper for maybe_constant_init_1 which permits non constants. */
5808
5809tree
13de99bc 5810maybe_constant_init (tree t, tree decl, bool manifestly_const_eval)
118cd6ba 5811{
13de99bc 5812 return maybe_constant_init_1 (t, decl, true, manifestly_const_eval);
118cd6ba
MP
5813}
5814
5815/* Wrapper for maybe_constant_init_1 which does not permit non constants. */
5816
5817tree
5818cxx_constant_init (tree t, tree decl)
5819{
e4082611 5820 return maybe_constant_init_1 (t, decl, false, true);
118cd6ba
MP
5821}
5822
2d76680f
PC
5823#if 0
5824/* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
5825/* Return true if the object referred to by REF has automatic or thread
5826 local storage. */
5827
5828enum { ck_ok, ck_bad, ck_unknown };
5829static int
5830check_automatic_or_tls (tree ref)
5831{
ef4bddc2 5832 machine_mode mode;
f37fac2b 5833 poly_int64 bitsize, bitpos;
2d76680f
PC
5834 tree offset;
5835 int volatilep = 0, unsignedp = 0;
5836 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
5837 &mode, &unsignedp, &volatilep, false);
5838 duration_kind dk;
5839
5840 /* If there isn't a decl in the middle, we don't know the linkage here,
5841 and this isn't a constant expression anyway. */
5842 if (!DECL_P (decl))
5843 return ck_unknown;
5844 dk = decl_storage_duration (decl);
5845 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
5846}
5847#endif
5848
c7a53bdb
JJ
5849/* Data structure for passing data from potential_constant_expression_1
5850 to check_for_return_continue via cp_walk_tree. */
5851struct check_for_return_continue_data {
5852 hash_set<tree> *pset;
5853 tree continue_stmt;
5854};
5855
5856/* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
5857 called through cp_walk_tree. Return the first RETURN_EXPR found, or note
5858 the first CONTINUE_STMT if RETURN_EXPR is not found. */
5859static tree
5860check_for_return_continue (tree *tp, int *walk_subtrees, void *data)
5861{
5862 tree t = *tp, s;
5863 check_for_return_continue_data *d = (check_for_return_continue_data *) data;
5864 switch (TREE_CODE (t))
5865 {
5866 case RETURN_EXPR:
5867 return t;
5868
5869 case CONTINUE_STMT:
5870 if (d->continue_stmt == NULL_TREE)
5871 d->continue_stmt = t;
5872 break;
5873
5874#define RECUR(x) \
5875 if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
5876 d->pset)) \
5877 return r
5878
5879 /* For loops, walk subtrees manually, so that continue stmts found
5880 inside of the bodies of the loops are ignored. */
5881 case DO_STMT:
5882 *walk_subtrees = 0;
5883 RECUR (DO_COND (t));
5884 s = d->continue_stmt;
5885 RECUR (DO_BODY (t));
5886 d->continue_stmt = s;
5887 break;
5888
5889 case WHILE_STMT:
5890 *walk_subtrees = 0;
5891 RECUR (WHILE_COND (t));
5892 s = d->continue_stmt;
5893 RECUR (WHILE_BODY (t));
5894 d->continue_stmt = s;
5895 break;
5896
5897 case FOR_STMT:
5898 *walk_subtrees = 0;
5899 RECUR (FOR_INIT_STMT (t));
5900 RECUR (FOR_COND (t));
5901 RECUR (FOR_EXPR (t));
5902 s = d->continue_stmt;
5903 RECUR (FOR_BODY (t));
5904 d->continue_stmt = s;
5905 break;
5906
5907 case RANGE_FOR_STMT:
5908 *walk_subtrees = 0;
5909 RECUR (RANGE_FOR_EXPR (t));
5910 s = d->continue_stmt;
5911 RECUR (RANGE_FOR_BODY (t));
5912 d->continue_stmt = s;
5913 break;
5914#undef RECUR
5915
5916 case STATEMENT_LIST:
5917 case CONSTRUCTOR:
5918 break;
5919
5920 default:
5921 if (!EXPR_P (t))
5922 *walk_subtrees = 0;
5923 break;
5924 }
5925
5926 return NULL_TREE;
5927}
5928
2d76680f
PC
5929/* Return true if T denotes a potentially constant expression. Issue
5930 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
a0ab7ccd
JM
5931 an lvalue-rvalue conversion is implied. If NOW is true, we want to
5932 consider the expression in the current context, independent of constexpr
5933 substitution.
2d76680f
PC
5934
5935 C++0x [expr.const] used to say
5936
5937 6 An expression is a potential constant expression if it is
5938 a constant expression where all occurrences of function
5939 parameters are replaced by arbitrary constant expressions
5940 of the appropriate type.
5941
5942 2 A conditional expression is a constant expression unless it
5943 involves one of the following as a potentially evaluated
5944 subexpression (3.2), but subexpressions of logical AND (5.14),
5945 logical OR (5.15), and conditional (5.16) operations that are
5946 not evaluated are not considered. */
5947
5948static bool
a0ab7ccd 5949potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
3075affd 5950 tsubst_flags_t flags, tree *jump_target)
2d76680f 5951{
a0ab7ccd 5952#define RECUR(T,RV) \
3075affd 5953 potential_constant_expression_1 ((T), (RV), strict, now, flags, jump_target)
a0ab7ccd 5954
2d76680f
PC
5955 enum { any = false, rval = true };
5956 int i;
5957 tree tmp;
5958
5959 if (t == error_mark_node)
5960 return false;
5961 if (t == NULL_TREE)
5962 return true;
6bdfada4 5963 location_t loc = cp_expr_loc_or_loc (t, input_location);
3075affd
JM
5964
5965 if (*jump_target)
5966 /* If we are jumping, ignore everything. This is simpler than the
5967 cxx_eval_constant_expression handling because we only need to be
5968 conservatively correct, and we don't necessarily have a constant value
5969 available, so we don't bother with switch tracking. */
5970 return true;
5971
3c393a2c 5972 if (TREE_THIS_VOLATILE (t) && want_rval)
2d76680f
PC
5973 {
5974 if (flags & tf_error)
3c393a2c
MP
5975 error_at (loc, "lvalue-to-rvalue conversion of a volatile lvalue "
5976 "%qE with type %qT", t, TREE_TYPE (t));
2d76680f
PC
5977 return false;
5978 }
5979 if (CONSTANT_CLASS_P (t))
5980 return true;
7dc2b4a2
JM
5981 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED)
5982 && TREE_TYPE (t) == error_mark_node)
5983 return false;
2d76680f
PC
5984
5985 switch (TREE_CODE (t))
5986 {
5987 case FUNCTION_DECL:
5988 case BASELINK:
5989 case TEMPLATE_DECL:
5990 case OVERLOAD:
5991 case TEMPLATE_ID_EXPR:
5992 case LABEL_DECL:
5993 case LABEL_EXPR:
60813a46 5994 case CASE_LABEL_EXPR:
2d76680f
PC
5995 case CONST_DECL:
5996 case SIZEOF_EXPR:
5997 case ALIGNOF_EXPR:
5998 case OFFSETOF_EXPR:
5999 case NOEXCEPT_EXPR:
6000 case TEMPLATE_PARM_INDEX:
6001 case TRAIT_EXPR:
6002 case IDENTIFIER_NODE:
6003 case USERDEF_LITERAL:
6004 /* We can see a FIELD_DECL in a pointer-to-member expression. */
6005 case FIELD_DECL:
cda0a029 6006 case RESULT_DECL:
2d76680f 6007 case USING_DECL:
60813a46 6008 case USING_STMT:
3e605b20 6009 case PLACEHOLDER_EXPR:
971e17ff 6010 case REQUIRES_EXPR:
98e5a19a 6011 case STATIC_ASSERT:
96a95ac1 6012 case DEBUG_BEGIN_STMT:
2d76680f
PC
6013 return true;
6014
3075affd
JM
6015 case RETURN_EXPR:
6016 if (!RECUR (TREE_OPERAND (t, 0), any))
6017 return false;
6018 /* FALLTHROUGH */
6019
6020 case BREAK_STMT:
6021 case CONTINUE_STMT:
6022 *jump_target = t;
6023 return true;
6024
a0ab7ccd
JM
6025 case PARM_DECL:
6026 if (now)
6027 {
6028 if (flags & tf_error)
6029 error ("%qE is not a constant expression", t);
6030 return false;
6031 }
6032 return true;
6033
2d76680f
PC
6034 case AGGR_INIT_EXPR:
6035 case CALL_EXPR:
6036 /* -- an invocation of a function other than a constexpr function
6037 or a constexpr constructor. */
6038 {
6039 tree fun = get_function_named_in_call (t);
6040 const int nargs = call_expr_nargs (t);
6041 i = 0;
6042
60813a46
JM
6043 if (fun == NULL_TREE)
6044 {
44a845ca
MS
6045 /* Reset to allow the function to continue past the end
6046 of the block below. Otherwise return early. */
6047 bool bail = true;
6048
35228ac7
JJ
6049 if (TREE_CODE (t) == CALL_EXPR
6050 && CALL_EXPR_FN (t) == NULL_TREE)
6051 switch (CALL_EXPR_IFN (t))
6052 {
6053 /* These should be ignored, they are optimized away from
6054 constexpr functions. */
6055 case IFN_UBSAN_NULL:
6056 case IFN_UBSAN_BOUNDS:
6057 case IFN_UBSAN_VPTR:
81fea426 6058 case IFN_FALLTHROUGH:
35228ac7 6059 return true;
44a845ca
MS
6060
6061 case IFN_ADD_OVERFLOW:
6062 case IFN_SUB_OVERFLOW:
6063 case IFN_MUL_OVERFLOW:
e16f1cc7 6064 case IFN_LAUNDER:
d8fcab68 6065 case IFN_VEC_CONVERT:
44a845ca 6066 bail = false;
d8fcab68 6067 break;
44a845ca 6068
35228ac7
JJ
6069 default:
6070 break;
6071 }
44a845ca
MS
6072
6073 if (bail)
6074 {
6075 /* fold_call_expr can't do anything with IFN calls. */
6076 if (flags & tf_error)
e40b6fc7 6077 error_at (loc, "call to internal function %qE", t);
44a845ca
MS
6078 return false;
6079 }
60813a46 6080 }
44a845ca
MS
6081
6082 if (fun && is_overloaded_fn (fun))
2d76680f
PC
6083 {
6084 if (TREE_CODE (fun) == FUNCTION_DECL)
6085 {
6086 if (builtin_valid_in_constant_expr_p (fun))
6087 return true;
6088 if (!DECL_DECLARED_CONSTEXPR_P (fun)
6089 /* Allow any built-in function; if the expansion
6090 isn't constant, we'll deal with that then. */
3d78e008 6091 && !fndecl_built_in_p (fun))
2d76680f
PC
6092 {
6093 if (flags & tf_error)
6094 {
84fa214d 6095 error_at (loc, "call to non-%<constexpr%> function %qD",
e40b6fc7 6096 fun);
2d76680f
PC
6097 explain_invalid_constexpr_fn (fun);
6098 }
6099 return false;
6100 }
6101 /* A call to a non-static member function takes the address
6102 of the object as the first argument. But in a constant
6103 expression the address will be folded away, so look
6104 through it now. */
6105 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
6106 && !DECL_CONSTRUCTOR_P (fun))
6107 {
6108 tree x = get_nth_callarg (t, 0);
6109 if (is_this_parameter (x))
3e605b20 6110 return true;
a0ab7ccd
JM
6111 /* Don't require an immediately constant value, as
6112 constexpr substitution might not use the value. */
6113 bool sub_now = false;
6114 if (!potential_constant_expression_1 (x, rval, strict,
3075affd
JM
6115 sub_now, flags,
6116 jump_target))
2d76680f
PC
6117 return false;
6118 i = 1;
6119 }
6120 }
6121 else
6122 {
69eb4fde 6123 if (!RECUR (fun, true))
2d76680f
PC
6124 return false;
6125 fun = get_first_fn (fun);
6126 }
6127 /* Skip initial arguments to base constructors. */
6128 if (DECL_BASE_CONSTRUCTOR_P (fun))
6129 i = num_artificial_parms_for (fun);
6130 fun = DECL_ORIGIN (fun);
6131 }
44a845ca 6132 else if (fun)
2d76680f 6133 {
69eb4fde 6134 if (RECUR (fun, rval))
2d76680f
PC
6135 /* Might end up being a constant function pointer. */;
6136 else
6137 return false;
6138 }
6139 for (; i < nargs; ++i)
6140 {
6141 tree x = get_nth_callarg (t, i);
c3c29ba5
JM
6142 /* In a template, reference arguments haven't been converted to
6143 REFERENCE_TYPE and we might not even know if the parameter
6144 is a reference, so accept lvalue constants too. */
6145 bool rv = processing_template_decl ? any : rval;
a0ab7ccd
JM
6146 /* Don't require an immediately constant value, as constexpr
6147 substitution might not use the value of the argument. */
6148 bool sub_now = false;
6149 if (!potential_constant_expression_1 (x, rv, strict,
3075affd 6150 sub_now, flags, jump_target))
2d76680f
PC
6151 return false;
6152 }
6153 return true;
6154 }
6155
6156 case NON_LVALUE_EXPR:
6157 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
6158 -- an lvalue of integral type that refers to a non-volatile
6159 const variable or static data member initialized with
6160 constant expressions, or
6161
6162 -- an lvalue of literal type that refers to non-volatile
6163 object defined with constexpr, or that refers to a
6164 sub-object of such an object; */
69eb4fde 6165 return RECUR (TREE_OPERAND (t, 0), rval);
2d76680f
PC
6166
6167 case VAR_DECL:
70f40fea 6168 if (DECL_HAS_VALUE_EXPR_P (t))
c1051bf7 6169 {
68ad1bf7 6170 if (now && is_normal_capture_proxy (t))
c1051bf7
JM
6171 {
6172 /* -- in a lambda-expression, a reference to this or to a
6173 variable with automatic storage duration defined outside that
6174 lambda-expression, where the reference would be an
6175 odr-use. */
6176 if (flags & tf_error)
6177 {
6178 tree cap = DECL_CAPTURED_VARIABLE (t);
6179 error ("lambda capture of %qE is not a constant expression",
6180 cap);
6181 if (!want_rval && decl_constant_var_p (cap))
6182 inform (input_location, "because it is used as a glvalue");
6183 }
6184 return false;
6185 }
6186 return RECUR (DECL_VALUE_EXPR (t), rval);
6187 }
69eb4fde 6188 if (want_rval
98e5a19a 6189 && !var_in_maybe_constexpr_fn (t)
7dc2b4a2 6190 && !type_dependent_expression_p (t)
4b691b13 6191 && !decl_maybe_constant_var_p (t)
69eb4fde
JM
6192 && (strict
6193 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
4b691b13
JM
6194 || (DECL_INITIAL (t)
6195 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t)))
7dc2b4a2 6196 && COMPLETE_TYPE_P (TREE_TYPE (t))
dbcd32f8 6197 && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
2d76680f
PC
6198 {
6199 if (flags & tf_error)
6200 non_const_var_error (t);
6201 return false;
6202 }
6203 return true;
6204
6205 case NOP_EXPR:
ed3ea9f2
JJ
6206 if (REINTERPRET_CAST_P (t))
6207 {
6208 if (flags & tf_error)
a9c697b8 6209 error_at (loc, "%<reinterpret_cast%> is not a constant expression");
ed3ea9f2
JJ
6210 return false;
6211 }
6212 /* FALLTHRU */
2d76680f
PC
6213 case CONVERT_EXPR:
6214 case VIEW_CONVERT_EXPR:
6215 /* -- a reinterpret_cast. FIXME not implemented, and this rule
6216 may change to something more specific to type-punning (DR 1312). */
6217 {
6218 tree from = TREE_OPERAND (t, 0);
dfd7fdca
DM
6219 if (location_wrapper_p (t))
6220 return (RECUR (from, want_rval));
6221 if (INDIRECT_TYPE_P (TREE_TYPE (t)))
2d76680f 6222 {
dfd7fdca
DM
6223 STRIP_ANY_LOCATION_WRAPPER (from);
6224 if (TREE_CODE (from) == INTEGER_CST
6225 && !integer_zerop (from))
6226 {
6227 if (flags & tf_error)
a9c697b8
MS
6228 error_at (loc,
6229 "%<reinterpret_cast%> from integer to pointer");
dfd7fdca
DM
6230 return false;
6231 }
2d76680f 6232 }
69eb4fde 6233 return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
2d76680f
PC
6234 }
6235
be845b04
JJ
6236 case ADDRESSOF_EXPR:
6237 /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
6238 t = TREE_OPERAND (t, 0);
6239 goto handle_addr_expr;
6240
2d76680f
PC
6241 case ADDR_EXPR:
6242 /* -- a unary operator & that is applied to an lvalue that
6243 designates an object with thread or automatic storage
6244 duration; */
6245 t = TREE_OPERAND (t, 0);
6246
6247 if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
6248 /* A pointer-to-member constant. */
6249 return true;
6250
be845b04 6251 handle_addr_expr:
2d76680f
PC
6252#if 0
6253 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
6254 any checking here, as we might dereference the pointer later. If
6255 we remove this code, also remove check_automatic_or_tls. */
6256 i = check_automatic_or_tls (t);
6257 if (i == ck_ok)
6258 return true;
6259 if (i == ck_bad)
6260 {
6261 if (flags & tf_error)
6262 error ("address-of an object %qE with thread local or "
6263 "automatic storage is not a constant expression", t);
6264 return false;
6265 }
6266#endif
69eb4fde 6267 return RECUR (t, any);
2d76680f 6268
dc8d2d00
JM
6269 case REALPART_EXPR:
6270 case IMAGPART_EXPR:
2d76680f
PC
6271 case COMPONENT_REF:
6272 case BIT_FIELD_REF:
6273 case ARROW_EXPR:
6274 case OFFSET_REF:
6275 /* -- a class member access unless its postfix-expression is
6276 of literal type or of pointer to literal type. */
6277 /* This test would be redundant, as it follows from the
6278 postfix-expression being a potential constant expression. */
19dedccf
JM
6279 if (type_unknown_p (t))
6280 return true;
69eb4fde 6281 return RECUR (TREE_OPERAND (t, 0), want_rval);
2d76680f
PC
6282
6283 case EXPR_PACK_EXPANSION:
69eb4fde 6284 return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
2d76680f
PC
6285
6286 case INDIRECT_REF:
6287 {
6288 tree x = TREE_OPERAND (t, 0);
6289 STRIP_NOPS (x);
948d0d91 6290 if (is_this_parameter (x) && !is_capture_proxy (x))
2d76680f 6291 {
5d4e573b 6292 if (!var_in_maybe_constexpr_fn (x))
2d76680f
PC
6293 {
6294 if (flags & tf_error)
e40b6fc7 6295 error_at (loc, "use of %<this%> in a constant expression");
2d76680f
PC
6296 return false;
6297 }
2d76680f
PC
6298 return true;
6299 }
69eb4fde 6300 return RECUR (x, rval);
2d76680f
PC
6301 }
6302
60813a46
JM
6303 case STATEMENT_LIST:
6304 {
6305 tree_stmt_iterator i;
6306 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
6307 {
69eb4fde 6308 if (!RECUR (tsi_stmt (i), any))
60813a46
JM
6309 return false;
6310 }
6311 return true;
6312 }
6313 break;
6314
6315 case MODIFY_EXPR:
6316 if (cxx_dialect < cxx14)
6317 goto fail;
69eb4fde 6318 if (!RECUR (TREE_OPERAND (t, 0), any))
60813a46 6319 return false;
69eb4fde 6320 if (!RECUR (TREE_OPERAND (t, 1), rval))
60813a46
JM
6321 return false;
6322 return true;
6323
6324 case MODOP_EXPR:
6325 if (cxx_dialect < cxx14)
6326 goto fail;
69eb4fde 6327 if (!RECUR (TREE_OPERAND (t, 0), rval))
60813a46 6328 return false;
69eb4fde 6329 if (!RECUR (TREE_OPERAND (t, 2), rval))
60813a46
JM
6330 return false;
6331 return true;
6332
60813a46 6333 case DO_STMT:
69eb4fde 6334 if (!RECUR (DO_COND (t), rval))
60813a46 6335 return false;
69eb4fde 6336 if (!RECUR (DO_BODY (t), any))
60813a46 6337 return false;
3075affd
JM
6338 if (breaks (jump_target) || continues (jump_target))
6339 *jump_target = NULL_TREE;
60813a46
JM
6340 return true;
6341
6342 case FOR_STMT:
69eb4fde 6343 if (!RECUR (FOR_INIT_STMT (t), any))
60813a46 6344 return false;
8e9558f0
MP
6345 tmp = FOR_COND (t);
6346 if (!RECUR (tmp, rval))
60813a46 6347 return false;
8e9558f0
MP
6348 if (tmp)
6349 {
6350 if (!processing_template_decl)
6351 tmp = cxx_eval_outermost_constant_expr (tmp, true);
a385474c
MP
6352 /* If we couldn't evaluate the condition, it might not ever be
6353 true. */
6354 if (!integer_onep (tmp))
8e9558f0
MP
6355 return true;
6356 }
69eb4fde 6357 if (!RECUR (FOR_EXPR (t), any))
60813a46 6358 return false;
69eb4fde 6359 if (!RECUR (FOR_BODY (t), any))
60813a46 6360 return false;
3075affd
JM
6361 if (breaks (jump_target) || continues (jump_target))
6362 *jump_target = NULL_TREE;
60813a46
JM
6363 return true;
6364
98e5a19a 6365 case RANGE_FOR_STMT:
8112667c
MP
6366 if (!RECUR (RANGE_FOR_INIT_STMT (t), any))
6367 return false;
98e5a19a
JM
6368 if (!RECUR (RANGE_FOR_EXPR (t), any))
6369 return false;
6370 if (!RECUR (RANGE_FOR_BODY (t), any))
6371 return false;
3075affd
JM
6372 if (breaks (jump_target) || continues (jump_target))
6373 *jump_target = NULL_TREE;
98e5a19a
JM
6374 return true;
6375
60813a46 6376 case WHILE_STMT:
8e9558f0
MP
6377 tmp = WHILE_COND (t);
6378 if (!RECUR (tmp, rval))
60813a46 6379 return false;
8e9558f0
MP
6380 if (!processing_template_decl)
6381 tmp = cxx_eval_outermost_constant_expr (tmp, true);
a385474c
MP
6382 /* If we couldn't evaluate the condition, it might not ever be true. */
6383 if (!integer_onep (tmp))
8e9558f0 6384 return true;
69eb4fde 6385 if (!RECUR (WHILE_BODY (t), any))
60813a46 6386 return false;
3075affd
JM
6387 if (breaks (jump_target) || continues (jump_target))
6388 *jump_target = NULL_TREE;
60813a46
JM
6389 return true;
6390
6391 case SWITCH_STMT:
69eb4fde 6392 if (!RECUR (SWITCH_STMT_COND (t), rval))
60813a46 6393 return false;
ce965730 6394 /* FIXME we don't check SWITCH_STMT_BODY currently, because even
c7a53bdb
JJ
6395 unreachable labels would be checked and it is enough if there is
6396 a single switch cond value for which it is a valid constant
6397 expression. We need to check if there are any RETURN_EXPRs
6398 or CONTINUE_STMTs inside of the body though, as in that case
6399 we need to set *jump_target. */
6400 else
6401 {
6402 hash_set<tree> pset;
6403 check_for_return_continue_data data = { &pset, NULL_TREE };
6404 if (tree ret_expr
6405 = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue,
6406 &data, &pset))
6407 /* The switch might return. */
6408 *jump_target = ret_expr;
6409 else if (data.continue_stmt)
6410 /* The switch can't return, but might continue. */
6411 *jump_target = data.continue_stmt;
6412 }
60813a46
JM
6413 return true;
6414
58611fb6 6415 case STMT_EXPR:
69eb4fde 6416 return RECUR (STMT_EXPR_STMT (t), rval);
58611fb6 6417
2d76680f 6418 case LAMBDA_EXPR:
899ac3b8
JM
6419 if (cxx_dialect >= cxx17)
6420 /* In C++17 lambdas can be constexpr, don't give up yet. */
6421 return true;
6422 else if (flags & tf_error)
6423 error_at (loc, "lambda-expression is not a constant expression "
6424 "before C++17");
6425 return false;
6426
2d76680f
PC
6427 case DYNAMIC_CAST_EXPR:
6428 case PSEUDO_DTOR_EXPR:
2d76680f
PC
6429 case NEW_EXPR:
6430 case VEC_NEW_EXPR:
6431 case DELETE_EXPR:
6432 case VEC_DELETE_EXPR:
6433 case THROW_EXPR:
e40b6fc7
JJ
6434 case OMP_PARALLEL:
6435 case OMP_TASK:
6436 case OMP_FOR:
897064e2 6437 case OMP_SIMD:
e40b6fc7
JJ
6438 case OMP_DISTRIBUTE:
6439 case OMP_TASKLOOP:
6440 case OMP_TEAMS:
6441 case OMP_TARGET_DATA:
6442 case OMP_TARGET:
6443 case OMP_SECTIONS:
6444 case OMP_ORDERED:
6445 case OMP_CRITICAL:
6446 case OMP_SINGLE:
6447 case OMP_SECTION:
6448 case OMP_MASTER:
6449 case OMP_TASKGROUP:
6450 case OMP_TARGET_UPDATE:
6451 case OMP_TARGET_ENTER_DATA:
6452 case OMP_TARGET_EXIT_DATA:
2d76680f
PC
6453 case OMP_ATOMIC:
6454 case OMP_ATOMIC_READ:
6455 case OMP_ATOMIC_CAPTURE_OLD:
6456 case OMP_ATOMIC_CAPTURE_NEW:
28567c40 6457 case OMP_DEPOBJ:
e40b6fc7
JJ
6458 case OACC_PARALLEL:
6459 case OACC_KERNELS:
6460 case OACC_DATA:
6461 case OACC_HOST_DATA:
6462 case OACC_LOOP:
6463 case OACC_CACHE:
6464 case OACC_DECLARE:
6465 case OACC_ENTER_DATA:
6466 case OACC_EXIT_DATA:
6467 case OACC_UPDATE:
2d76680f
PC
6468 /* GCC internal stuff. */
6469 case VA_ARG_EXPR:
2d76680f 6470 case TRANSACTION_EXPR:
60813a46 6471 case ASM_EXPR:
81b6a6c5 6472 case AT_ENCODE_EXPR:
60813a46 6473 fail:
2d76680f 6474 if (flags & tf_error)
e40b6fc7 6475 error_at (loc, "expression %qE is not a constant expression", t);
2d76680f
PC
6476 return false;
6477
bf8d8309
MP
6478 case OBJ_TYPE_REF:
6479 if (cxx_dialect >= cxx2a)
6480 /* In C++2a virtual calls can be constexpr, don't give up yet. */
6481 return true;
6482 else if (flags & tf_error)
a9c697b8
MS
6483 error_at (loc,
6484 "virtual functions cannot be %<constexpr%> before C++2a");
bf8d8309
MP
6485 return false;
6486
2d76680f
PC
6487 case TYPEID_EXPR:
6488 /* -- a typeid expression whose operand is of polymorphic
6489 class type; */
6490 {
6491 tree e = TREE_OPERAND (t, 0);
6492 if (!TYPE_P (e) && !type_dependent_expression_p (e)
6493 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
6494 {
6495 if (flags & tf_error)
a9c697b8 6496 error_at (loc, "%<typeid%> is not a constant expression "
e40b6fc7 6497 "because %qE is of polymorphic type", e);
2d76680f
PC
6498 return false;
6499 }
6500 return true;
6501 }
6502
1af4ebf5 6503 case POINTER_DIFF_EXPR:
2d76680f 6504 case MINUS_EXPR:
2d76680f
PC
6505 want_rval = true;
6506 goto binary;
6507
6508 case LT_EXPR:
6509 case LE_EXPR:
6510 case GT_EXPR:
6511 case GE_EXPR:
6512 case EQ_EXPR:
6513 case NE_EXPR:
2d76680f
PC
6514 want_rval = true;
6515 goto binary;
6516
60813a46
JM
6517 case PREINCREMENT_EXPR:
6518 case POSTINCREMENT_EXPR:
6519 case PREDECREMENT_EXPR:
6520 case POSTDECREMENT_EXPR:
6521 if (cxx_dialect < cxx14)
6522 goto fail;
6523 goto unary;
6524
2d76680f
PC
6525 case BIT_NOT_EXPR:
6526 /* A destructor. */
6527 if (TYPE_P (TREE_OPERAND (t, 0)))
6528 return true;
191816a3 6529 /* fall through. */
2d76680f 6530
2d76680f
PC
6531 case CONJ_EXPR:
6532 case SAVE_EXPR:
6533 case FIX_TRUNC_EXPR:
6534 case FLOAT_EXPR:
6535 case NEGATE_EXPR:
6536 case ABS_EXPR:
e197e64e 6537 case ABSU_EXPR:
2d76680f
PC
6538 case TRUTH_NOT_EXPR:
6539 case FIXED_CONVERT_EXPR:
6540 case UNARY_PLUS_EXPR:
4856a1f0
PC
6541 case UNARY_LEFT_FOLD_EXPR:
6542 case UNARY_RIGHT_FOLD_EXPR:
60813a46 6543 unary:
69eb4fde 6544 return RECUR (TREE_OPERAND (t, 0), rval);
2d76680f
PC
6545
6546 case CAST_EXPR:
6547 case CONST_CAST_EXPR:
6548 case STATIC_CAST_EXPR:
6549 case REINTERPRET_CAST_EXPR:
6550 case IMPLICIT_CONV_EXPR:
6551 if (cxx_dialect < cxx11
6552 && !dependent_type_p (TREE_TYPE (t))
6553 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
6554 /* In C++98, a conversion to non-integral type can't be part of a
6555 constant expression. */
6556 {
6557 if (flags & tf_error)
e40b6fc7
JJ
6558 error_at (loc,
6559 "cast to non-integral type %qT in a constant expression",
6560 TREE_TYPE (t));
2d76680f
PC
6561 return false;
6562 }
fe0604d3
MP
6563 /* This might be a conversion from a class to a (potentially) literal
6564 type. Let's consider it potentially constant since the conversion
6565 might be a constexpr user-defined conversion. */
6566 else if (cxx_dialect >= cxx11
6567 && (dependent_type_p (TREE_TYPE (t))
6568 || !COMPLETE_TYPE_P (TREE_TYPE (t))
6569 || literal_type_p (TREE_TYPE (t)))
6570 && TREE_OPERAND (t, 0))
6571 {
6572 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
6573 /* If this is a dependent type, it could end up being a class
6574 with conversions. */
6575 if (type == NULL_TREE || WILDCARD_TYPE_P (type))
6576 return true;
6577 /* Or a non-dependent class which has conversions. */
6578 else if (CLASS_TYPE_P (type)
6579 && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type)))
6580 return true;
6581 }
2d76680f 6582
69eb4fde 6583 return (RECUR (TREE_OPERAND (t, 0),
9f613f06 6584 !TYPE_REF_P (TREE_TYPE (t))));
2d76680f 6585
60813a46 6586 case BIND_EXPR:
69eb4fde 6587 return RECUR (BIND_EXPR_BODY (t), want_rval);
60813a46 6588
60813a46
JM
6589 case CLEANUP_POINT_EXPR:
6590 case MUST_NOT_THROW_EXPR:
6591 case TRY_CATCH_EXPR:
769430b2 6592 case TRY_BLOCK:
60813a46
JM
6593 case EH_SPEC_BLOCK:
6594 case EXPR_STMT:
2d76680f
PC
6595 case PAREN_EXPR:
6596 case NON_DEPENDENT_EXPR:
6597 /* For convenience. */
f937929e
JM
6598 case LOOP_EXPR:
6599 case EXIT_EXPR:
69eb4fde 6600 return RECUR (TREE_OPERAND (t, 0), want_rval);
2d76680f 6601
98e5a19a
JM
6602 case DECL_EXPR:
6603 tmp = DECL_EXPR_DECL (t);
6604 if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp))
6605 {
6606 if (TREE_STATIC (tmp))
6607 {
6608 if (flags & tf_error)
6609 error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared "
4b691b13 6610 "%<static%> in %<constexpr%> context", tmp);
98e5a19a
JM
6611 return false;
6612 }
6613 else if (CP_DECL_THREAD_LOCAL_P (tmp))
6614 {
6615 if (flags & tf_error)
6616 error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared "
4b691b13 6617 "%<thread_local%> in %<constexpr%> context", tmp);
98e5a19a
JM
6618 return false;
6619 }
3885527d
PC
6620 else if (!check_for_uninitialized_const_var
6621 (tmp, /*constexpr_context_p=*/true, flags))
6622 return false;
98e5a19a
JM
6623 }
6624 return RECUR (tmp, want_rval);
6625
769430b2
JM
6626 case TRY_FINALLY_EXPR:
6627 return (RECUR (TREE_OPERAND (t, 0), want_rval)
6628 && RECUR (TREE_OPERAND (t, 1), any));
6629
2d76680f 6630 case SCOPE_REF:
69eb4fde 6631 return RECUR (TREE_OPERAND (t, 1), want_rval);
2d76680f
PC
6632
6633 case TARGET_EXPR:
d3a9902e
JM
6634 if (!TARGET_EXPR_DIRECT_INIT_P (t)
6635 && !literal_type_p (TREE_TYPE (t)))
2d76680f
PC
6636 {
6637 if (flags & tf_error)
6638 {
097f82ec 6639 auto_diagnostic_group d;
e40b6fc7
JJ
6640 error_at (loc, "temporary of non-literal type %qT in a "
6641 "constant expression", TREE_TYPE (t));
2d76680f
PC
6642 explain_non_literal_class (TREE_TYPE (t));
6643 }
6644 return false;
6645 }
191816a3 6646 /* FALLTHRU */
2d76680f 6647 case INIT_EXPR:
69eb4fde 6648 return RECUR (TREE_OPERAND (t, 1), rval);
2d76680f
PC
6649
6650 case CONSTRUCTOR:
6651 {
6652 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
6653 constructor_elt *ce;
6654 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
69eb4fde 6655 if (!RECUR (ce->value, want_rval))
2d76680f
PC
6656 return false;
6657 return true;
6658 }
6659
6660 case TREE_LIST:
6661 {
6662 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
6663 || DECL_P (TREE_PURPOSE (t)));
69eb4fde 6664 if (!RECUR (TREE_VALUE (t), want_rval))
2d76680f
PC
6665 return false;
6666 if (TREE_CHAIN (t) == NULL_TREE)
6667 return true;
69eb4fde 6668 return RECUR (TREE_CHAIN (t), want_rval);
2d76680f
PC
6669 }
6670
6671 case TRUNC_DIV_EXPR:
6672 case CEIL_DIV_EXPR:
6673 case FLOOR_DIV_EXPR:
6674 case ROUND_DIV_EXPR:
6675 case TRUNC_MOD_EXPR:
6676 case CEIL_MOD_EXPR:
6677 case ROUND_MOD_EXPR:
6678 {
6679 tree denom = TREE_OPERAND (t, 1);
69eb4fde 6680 if (!RECUR (denom, rval))
2d76680f
PC
6681 return false;
6682 /* We can't call cxx_eval_outermost_constant_expr on an expression
234bef96 6683 that hasn't been through instantiate_non_dependent_expr yet. */
2d76680f
PC
6684 if (!processing_template_decl)
6685 denom = cxx_eval_outermost_constant_expr (denom, true);
6686 if (integer_zerop (denom))
6687 {
6688 if (flags & tf_error)
64d6d399 6689 error ("division by zero is not a constant expression");
2d76680f
PC
6690 return false;
6691 }
6692 else
6693 {
6694 want_rval = true;
69eb4fde 6695 return RECUR (TREE_OPERAND (t, 0), want_rval);
2d76680f
PC
6696 }
6697 }
6698
6699 case COMPOUND_EXPR:
6700 {
6701 /* check_return_expr sometimes wraps a TARGET_EXPR in a
6702 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
6703 introduced by build_call_a. */
6704 tree op0 = TREE_OPERAND (t, 0);
6705 tree op1 = TREE_OPERAND (t, 1);
6706 STRIP_NOPS (op1);
6707 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
6708 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
69eb4fde 6709 return RECUR (op0, want_rval);
2d76680f
PC
6710 else
6711 goto binary;
6712 }
6713
6714 /* If the first operand is the non-short-circuit constant, look at
6715 the second operand; otherwise we only care about the first one for
6716 potentiality. */
6717 case TRUTH_AND_EXPR:
6718 case TRUTH_ANDIF_EXPR:
6719 tmp = boolean_true_node;
6720 goto truth;
6721 case TRUTH_OR_EXPR:
6722 case TRUTH_ORIF_EXPR:
6723 tmp = boolean_false_node;
6724 truth:
6725 {
6726 tree op = TREE_OPERAND (t, 0);
69eb4fde 6727 if (!RECUR (op, rval))
2d76680f
PC
6728 return false;
6729 if (!processing_template_decl)
6730 op = cxx_eval_outermost_constant_expr (op, true);
6731 if (tree_int_cst_equal (op, tmp))
69eb4fde 6732 return RECUR (TREE_OPERAND (t, 1), rval);
2d76680f
PC
6733 else
6734 return true;
6735 }
6736
6737 case PLUS_EXPR:
6738 case MULT_EXPR:
6739 case POINTER_PLUS_EXPR:
6740 case RDIV_EXPR:
6741 case EXACT_DIV_EXPR:
6742 case MIN_EXPR:
6743 case MAX_EXPR:
6744 case LSHIFT_EXPR:
6745 case RSHIFT_EXPR:
6746 case LROTATE_EXPR:
6747 case RROTATE_EXPR:
6748 case BIT_IOR_EXPR:
6749 case BIT_XOR_EXPR:
6750 case BIT_AND_EXPR:
6751 case TRUTH_XOR_EXPR:
6752 case UNORDERED_EXPR:
6753 case ORDERED_EXPR:
6754 case UNLT_EXPR:
6755 case UNLE_EXPR:
6756 case UNGT_EXPR:
6757 case UNGE_EXPR:
6758 case UNEQ_EXPR:
6759 case LTGT_EXPR:
6760 case RANGE_EXPR:
6761 case COMPLEX_EXPR:
6762 want_rval = true;
6763 /* Fall through. */
6764 case ARRAY_REF:
6765 case ARRAY_RANGE_REF:
6766 case MEMBER_REF:
6767 case DOTSTAR_EXPR:
41b38772 6768 case MEM_REF:
4856a1f0
PC
6769 case BINARY_LEFT_FOLD_EXPR:
6770 case BINARY_RIGHT_FOLD_EXPR:
2d76680f
PC
6771 binary:
6772 for (i = 0; i < 2; ++i)
69eb4fde 6773 if (!RECUR (TREE_OPERAND (t, i), want_rval))
2d76680f
PC
6774 return false;
6775 return true;
6776
2d76680f
PC
6777 case VEC_PERM_EXPR:
6778 for (i = 0; i < 3; ++i)
69eb4fde 6779 if (!RECUR (TREE_OPERAND (t, i), true))
2d76680f
PC
6780 return false;
6781 return true;
6782
6783 case COND_EXPR:
d5bcd6d4
JM
6784 if (COND_EXPR_IS_VEC_DELETE (t))
6785 {
6786 if (flags & tf_error)
e40b6fc7 6787 error_at (loc, "%<delete[]%> is not a constant expression");
d5bcd6d4
JM
6788 return false;
6789 }
6790 /* Fall through. */
6791 case IF_STMT:
2d76680f
PC
6792 case VEC_COND_EXPR:
6793 /* If the condition is a known constant, we know which of the legs we
6794 care about; otherwise we only require that the condition and
6795 either of the legs be potentially constant. */
6796 tmp = TREE_OPERAND (t, 0);
69eb4fde 6797 if (!RECUR (tmp, rval))
2d76680f
PC
6798 return false;
6799 if (!processing_template_decl)
6800 tmp = cxx_eval_outermost_constant_expr (tmp, true);
6801 if (integer_zerop (tmp))
69eb4fde 6802 return RECUR (TREE_OPERAND (t, 2), want_rval);
2d76680f 6803 else if (TREE_CODE (tmp) == INTEGER_CST)
69eb4fde 6804 return RECUR (TREE_OPERAND (t, 1), want_rval);
2d76680f
PC
6805 for (i = 1; i < 3; ++i)
6806 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
3075affd
JM
6807 want_rval, strict, now,
6808 tf_none, jump_target))
2d76680f
PC
6809 return true;
6810 if (flags & tf_error)
e40b6fc7 6811 error_at (loc, "expression %qE is not a constant expression", t);
2d76680f
PC
6812 return false;
6813
6814 case VEC_INIT_EXPR:
6815 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
6816 return true;
6817 if (flags & tf_error)
6818 {
e40b6fc7 6819 error_at (loc, "non-constant array initialization");
2d76680f
PC
6820 diagnose_non_constexpr_vec_init (t);
6821 }
6822 return false;
6823
133bc698
JM
6824 case TYPE_DECL:
6825 case TAG_DEFN:
6826 /* We can see these in statement-expressions. */
6827 return true;
6828
baf9ebc8 6829 case CLEANUP_STMT:
cda0a029 6830 case EMPTY_CLASS_EXPR:
7fef86d3 6831 case PREDICT_EXPR:
cda0a029
JM
6832 return false;
6833
f937929e
JM
6834 case GOTO_EXPR:
6835 {
6836 tree *target = &TREE_OPERAND (t, 0);
ab3af181
JJ
6837 /* Gotos representing break and continue are OK. */
6838 if (breaks (target) || continues (target))
3075affd
JM
6839 {
6840 *jump_target = *target;
6841 return true;
6842 }
ab3af181 6843 if (flags & tf_error)
e40b6fc7 6844 error_at (loc, "%<goto%> is not a constant expression");
ab3af181 6845 return false;
f937929e
JM
6846 }
6847
98e09245 6848 case ANNOTATE_EXPR:
98e09245
JJ
6849 return RECUR (TREE_OPERAND (t, 0), rval);
6850
2d76680f
PC
6851 default:
6852 if (objc_is_property_ref (t))
6853 return false;
6854
6855 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
ab3af181 6856 gcc_unreachable ();
2d76680f
PC
6857 return false;
6858 }
69eb4fde 6859#undef RECUR
2d76680f
PC
6860}
6861
3075affd
JM
6862bool
6863potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
6864 tsubst_flags_t flags)
6865{
6866 tree target = NULL_TREE;
6867 return potential_constant_expression_1 (t, want_rval, strict, now,
6868 flags, &target);
6869}
6870
2d76680f
PC
6871/* The main entry point to the above. */
6872
6873bool
6874potential_constant_expression (tree t)
6875{
a0ab7ccd 6876 return potential_constant_expression_1 (t, false, true, false, tf_none);
2d76680f
PC
6877}
6878
6879/* As above, but require a constant rvalue. */
6880
6881bool
6882potential_rvalue_constant_expression (tree t)
6883{
a0ab7ccd 6884 return potential_constant_expression_1 (t, true, true, false, tf_none);
2d76680f
PC
6885}
6886
6887/* Like above, but complain about non-constant expressions. */
6888
6889bool
6890require_potential_constant_expression (tree t)
6891{
d8cff23f
MP
6892 return potential_constant_expression_1 (t, false, true, false,
6893 tf_warning_or_error);
2d76680f
PC
6894}
6895
6896/* Cross product of the above. */
6897
6898bool
6899require_potential_rvalue_constant_expression (tree t)
6900{
d8cff23f
MP
6901 return potential_constant_expression_1 (t, true, true, false,
6902 tf_warning_or_error);
6903}
6904
6905/* Like above, but don't consider PARM_DECL a potential_constant_expression. */
6906
6907bool
6908require_rvalue_constant_expression (tree t)
6909{
6910 return potential_constant_expression_1 (t, true, true, true,
6911 tf_warning_or_error);
a0ab7ccd
JM
6912}
6913
6914/* Like potential_constant_expression, but don't consider possible constexpr
6915 substitution of the current function. That is, PARM_DECL qualifies under
6916 potential_constant_expression, but not here.
6917
6918 This is basically what you can check when any actual constant values might
6919 be value-dependent. */
6920
6921bool
6922is_constant_expression (tree t)
6923{
6924 return potential_constant_expression_1 (t, false, true, true, tf_none);
6925}
6926
6927/* Like above, but complain about non-constant expressions. */
6928
6929bool
6930require_constant_expression (tree t)
6931{
6932 return potential_constant_expression_1 (t, false, true, true,
6933 tf_warning_or_error);
6934}
6935
6936/* Like is_constant_expression, but allow const variables that are not allowed
6937 under constexpr rules. */
6938
6939bool
6940is_static_init_expression (tree t)
6941{
6942 return potential_constant_expression_1 (t, false, false, true, tf_none);
2d76680f
PC
6943}
6944
eb07f187
JM
6945/* Returns true if T is a potential constant expression that is not
6946 instantiation-dependent, and therefore a candidate for constant folding even
6947 in a template. */
6948
6949bool
a0ab7ccd 6950is_nondependent_constant_expression (tree t)
eb07f187
JM
6951{
6952 return (!type_unknown_p (t)
6953 && !BRACE_ENCLOSED_INITIALIZER_P (t)
a0ab7ccd 6954 && is_constant_expression (t)
eb07f187
JM
6955 && !instantiation_dependent_expression_p (t));
6956}
6957
6958/* Returns true if T is a potential static initializer expression that is not
6959 instantiation-dependent. */
6960
6961bool
a0ab7ccd 6962is_nondependent_static_init_expression (tree t)
eb07f187
JM
6963{
6964 return (!type_unknown_p (t)
6965 && !BRACE_ENCLOSED_INITIALIZER_P (t)
a0ab7ccd 6966 && is_static_init_expression (t)
eb07f187
JM
6967 && !instantiation_dependent_expression_p (t));
6968}
6969
97f3003f
JM
6970/* Finalize constexpr processing after parsing. */
6971
6972void
6973fini_constexpr (void)
6974{
6975 /* The contexpr call and fundef copies tables are no longer needed. */
6976 constexpr_call_table = NULL;
6977 fundef_copies_table = NULL;
6978}
6979
2d76680f 6980#include "gt-cp-constexpr.h"
This page took 2.773856 seconds and 5 git commands to generate.