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