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