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