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