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