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