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