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-2022 Free Software Foundation, Inc.
6 
7    This file is part of GCC.
8 
9    GCC is free software; you can redistribute it and/or modify it
10    under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3, or (at your option)
12    any later version.
13 
14    GCC is distributed in the hope that it will be useful, but
15    WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17    General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22 
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "cp-tree.h"
27 #include "varasm.h"
28 #include "c-family/c-objc.h"
29 #include "tree-iterator.h"
30 #include "gimplify.h"
31 #include "builtins.h"
32 #include "tree-inline.h"
33 #include "ubsan.h"
34 #include "gimple-fold.h"
35 #include "timevar.h"
36 #include "fold-const-call.h"
37 #include "stor-layout.h"
38 #include "cgraph.h"
39 #include "opts.h"
40 #include "stringpool.h"
41 #include "attribs.h"
42 
43 static bool verify_constant (tree, bool, bool *, bool *);
44 #define VERIFY_CONSTANT(X)                                                      \
45 do {                                                                                      \
46   if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
47     return t;                                                                             \
48  } while (0)
49 
50 static HOST_WIDE_INT find_array_ctor_elt (tree ary, tree dindex,
51                                                     bool insert = false);
52 static int array_index_cmp (tree key, tree index);
53 
54 /* Returns true iff FUN is an instantiation of a constexpr function
55    template or a defaulted constexpr function.  */
56 
57 bool
is_instantiation_of_constexpr(tree fun)58 is_instantiation_of_constexpr (tree fun)
59 {
60   return ((DECL_TEMPLOID_INSTANTIATION (fun)
61              && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
62             || (DECL_DEFAULTED_FN (fun)
63                 && DECL_DECLARED_CONSTEXPR_P (fun)));
64 }
65 
66 /* Return true if T is a literal type.   */
67 
68 bool
literal_type_p(tree t)69 literal_type_p (tree t)
70 {
71   if (SCALAR_TYPE_P (t)
72       || VECTOR_TYPE_P (t)
73       || TYPE_REF_P (t)
74       || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
75     return true;
76   if (CLASS_TYPE_P (t))
77     {
78       t = complete_type (t);
79       gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
80       return CLASSTYPE_LITERAL_P (t);
81     }
82   if (TREE_CODE (t) == ARRAY_TYPE)
83     return literal_type_p (strip_array_types (t));
84   return false;
85 }
86 
87 /* If DECL is a variable declared `constexpr', require its type
88    be literal.  Return error_mark_node if we give an error, the
89    DECL otherwise.  */
90 
91 tree
ensure_literal_type_for_constexpr_object(tree decl)92 ensure_literal_type_for_constexpr_object (tree decl)
93 {
94   tree type = TREE_TYPE (decl);
95   if (VAR_P (decl)
96       && (DECL_DECLARED_CONSTEXPR_P (decl)
97             || var_in_constexpr_fn (decl))
98       && !processing_template_decl)
99     {
100       tree stype = strip_array_types (type);
101       if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype)))
102           /* Don't complain here, we'll complain about incompleteness
103              when we try to initialize the variable.  */;
104       else if (!literal_type_p (type))
105           {
106             if (DECL_DECLARED_CONSTEXPR_P (decl))
107               {
108                 auto_diagnostic_group d;
109                 error_at (DECL_SOURCE_LOCATION (decl),
110                               "the type %qT of %<constexpr%> variable %qD "
111                               "is not literal", type, decl);
112                 explain_non_literal_class (type);
113                 decl = error_mark_node;
114               }
115             else if (cxx_dialect < cxx23)
116               {
117                 if (!is_instantiation_of_constexpr (current_function_decl))
118                     {
119                       auto_diagnostic_group d;
120                       error_at (DECL_SOURCE_LOCATION (decl),
121                                   "variable %qD of non-literal type %qT in "
122                                   "%<constexpr%> function only available with "
123                                   "%<-std=c++2b%> or %<-std=gnu++2b%>", decl, type);
124                       explain_non_literal_class (type);
125                       decl = error_mark_node;
126                     }
127                 cp_function_chain->invalid_constexpr = true;
128               }
129           }
130       else if (DECL_DECLARED_CONSTEXPR_P (decl)
131                  && variably_modified_type_p (type, NULL_TREE))
132           {
133             error_at (DECL_SOURCE_LOCATION (decl),
134                         "%<constexpr%> variable %qD has variably-modified "
135                         "type %qT", decl, type);
136             decl = error_mark_node;
137           }
138     }
139   return decl;
140 }
141 
142 struct constexpr_fundef_hasher : ggc_ptr_hash<constexpr_fundef>
143 {
144   static hashval_t hash (const constexpr_fundef *);
145   static bool equal (const constexpr_fundef *, const constexpr_fundef *);
146 };
147 
148 /* This table holds all constexpr function definitions seen in
149    the current translation unit.  */
150 
151 static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table;
152 
153 /* Utility function used for managing the constexpr function table.
154    Return true if the entries pointed to by P and Q are for the
155    same constexpr function.  */
156 
157 inline bool
equal(const constexpr_fundef * lhs,const constexpr_fundef * rhs)158 constexpr_fundef_hasher::equal (const constexpr_fundef *lhs,
159                                         const constexpr_fundef *rhs)
160 {
161   return lhs->decl == rhs->decl;
162 }
163 
164 /* Utility function used for managing the constexpr function table.
165    Return a hash value for the entry pointed to by Q.  */
166 
167 inline hashval_t
hash(const constexpr_fundef * fundef)168 constexpr_fundef_hasher::hash (const constexpr_fundef *fundef)
169 {
170   return DECL_UID (fundef->decl);
171 }
172 
173 /* Return a previously saved definition of function FUN.   */
174 
175 constexpr_fundef *
retrieve_constexpr_fundef(tree fun)176 retrieve_constexpr_fundef (tree fun)
177 {
178   if (constexpr_fundef_table == NULL)
179     return NULL;
180 
181   constexpr_fundef fundef = { fun, NULL_TREE, NULL_TREE, NULL_TREE };
182   return constexpr_fundef_table->find (&fundef);
183 }
184 
185 /* Check whether the parameter and return types of FUN are valid for a
186    constexpr function, and complain if COMPLAIN.  */
187 
188 bool
is_valid_constexpr_fn(tree fun,bool complain)189 is_valid_constexpr_fn (tree fun, bool complain)
190 {
191   bool ret = true;
192 
193   if (DECL_INHERITED_CTOR (fun)
194       && TREE_CODE (fun) == TEMPLATE_DECL)
195     {
196       ret = false;
197       if (complain)
198           error ("inherited constructor %qD is not %<constexpr%>",
199                  DECL_INHERITED_CTOR (fun));
200     }
201   else
202     {
203       for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
204              parm != NULL_TREE; parm = TREE_CHAIN (parm))
205           if (!literal_type_p (TREE_TYPE (parm)))
206             {
207               ret = false;
208               if (complain)
209                 {
210                     auto_diagnostic_group d;
211                     error ("invalid type for parameter %d of %<constexpr%> "
212                            "function %q+#D", DECL_PARM_INDEX (parm), fun);
213                     explain_non_literal_class (TREE_TYPE (parm));
214                 }
215             }
216     }
217 
218   if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) && cxx_dialect < cxx17)
219     {
220       ret = false;
221       if (complain)
222           inform (DECL_SOURCE_LOCATION (fun),
223                     "lambdas are implicitly %<constexpr%> only in C++17 and later");
224     }
225   else if (DECL_DESTRUCTOR_P (fun))
226     {
227       if (cxx_dialect < cxx20)
228           {
229             ret = false;
230             if (complain)
231               error_at (DECL_SOURCE_LOCATION (fun),
232                           "%<constexpr%> destructors only available"
233                           " with %<-std=c++20%> or %<-std=gnu++20%>");
234           }
235     }
236   else if (!DECL_CONSTRUCTOR_P (fun))
237     {
238       tree rettype = TREE_TYPE (TREE_TYPE (fun));
239       if (!literal_type_p (rettype))
240           {
241             ret = false;
242             if (complain)
243               {
244                 auto_diagnostic_group d;
245                 error ("invalid return type %qT of %<constexpr%> function %q+D",
246                          rettype, fun);
247                 explain_non_literal_class (rettype);
248               }
249           }
250 
251       /* C++14 DR 1684 removed this restriction.  */
252       if (cxx_dialect < cxx14
253             && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
254             && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
255           {
256             ret = false;
257             if (complain)
258               {
259                 auto_diagnostic_group d;
260                 if (pedwarn (DECL_SOURCE_LOCATION (fun), OPT_Wpedantic,
261                                    "enclosing class of %<constexpr%> non-static"
262                                    " member function %q+#D is not a literal type",
263                                    fun))
264                     explain_non_literal_class (DECL_CONTEXT (fun));
265               }
266           }
267     }
268   else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
269     {
270       ret = false;
271       if (complain)
272           error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
273     }
274 
275   return ret;
276 }
277 
278 /* Subroutine of build_data_member_initialization.  MEMBER is a COMPONENT_REF
279    for a member of an anonymous aggregate, INIT is the initializer for that
280    member, and VEC_OUTER is the vector of constructor elements for the class
281    whose constructor we are processing.  Add the initializer to the vector
282    and return true to indicate success.  */
283 
284 static bool
build_anon_member_initialization(tree member,tree init,vec<constructor_elt,va_gc> ** vec_outer)285 build_anon_member_initialization (tree member, tree init,
286                                           vec<constructor_elt, va_gc> **vec_outer)
287 {
288   /* MEMBER presents the relevant fields from the inside out, but we need
289      to build up the initializer from the outside in so that we can reuse
290      previously built CONSTRUCTORs if this is, say, the second field in an
291      anonymous struct.  So we use a vec as a stack.  */
292   auto_vec<tree, 2> fields;
293   do
294     {
295       fields.safe_push (TREE_OPERAND (member, 1));
296       member = TREE_OPERAND (member, 0);
297     }
298   while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
299            && TREE_CODE (member) == COMPONENT_REF);
300 
301   /* VEC has the constructor elements vector for the context of FIELD.
302      If FIELD is an anonymous aggregate, we will push inside it.  */
303   vec<constructor_elt, va_gc> **vec = vec_outer;
304   tree field;
305   while (field = fields.pop(),
306            ANON_AGGR_TYPE_P (TREE_TYPE (field)))
307     {
308       tree ctor;
309       /* If there is already an outer constructor entry for the anonymous
310            aggregate FIELD, use it; otherwise, insert one.  */
311       if (vec_safe_is_empty (*vec)
312             || (*vec)->last().index != field)
313           {
314             ctor = build_constructor (TREE_TYPE (field), NULL);
315             CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
316           }
317       else
318           ctor = (*vec)->last().value;
319       vec = &CONSTRUCTOR_ELTS (ctor);
320     }
321 
322   /* Now we're at the innermost field, the one that isn't an anonymous
323      aggregate.  Add its initializer to the CONSTRUCTOR and we're done.  */
324   gcc_assert (fields.is_empty());
325   CONSTRUCTOR_APPEND_ELT (*vec, field, init);
326 
327   return true;
328 }
329 
330 /* Subroutine of  build_constexpr_constructor_member_initializers.
331    The expression tree T represents a data member initialization
332    in a (constexpr) constructor definition.  Build a pairing of
333    the data member with its initializer, and prepend that pair
334    to the existing initialization pair INITS.  */
335 
336 static bool
build_data_member_initialization(tree t,vec<constructor_elt,va_gc> ** vec)337 build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
338 {
339   tree member, init;
340   if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
341     t = TREE_OPERAND (t, 0);
342   if (TREE_CODE (t) == EXPR_STMT)
343     t = TREE_OPERAND (t, 0);
344   if (t == error_mark_node)
345     return false;
346   if (TREE_CODE (t) == STATEMENT_LIST)
347     {
348       for (tree stmt : tsi_range (t))
349           if (! build_data_member_initialization (stmt, vec))
350             return false;
351       return true;
352     }
353   if (TREE_CODE (t) == CLEANUP_STMT)
354     {
355       /* We can't see a CLEANUP_STMT in a constructor for a literal class,
356            but we can in a constexpr constructor for a non-literal class.  Just
357            ignore it; either all the initialization will be constant, in which
358            case the cleanup can't run, or it can't be constexpr.
359            Still recurse into CLEANUP_BODY.  */
360       return build_data_member_initialization (CLEANUP_BODY (t), vec);
361     }
362   if (TREE_CODE (t) == CONVERT_EXPR)
363     t = TREE_OPERAND (t, 0);
364   if (TREE_CODE (t) == INIT_EXPR
365       /* vptr initialization shows up as a MODIFY_EXPR.  In C++14 we only
366            use what this function builds for cx_check_missing_mem_inits, and
367            assignment in the ctor body doesn't count.  */
368       || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR))
369     {
370       member = TREE_OPERAND (t, 0);
371       init = break_out_target_exprs (TREE_OPERAND (t, 1));
372     }
373   else if (TREE_CODE (t) == CALL_EXPR)
374     {
375       tree fn = get_callee_fndecl (t);
376       if (!fn || !DECL_CONSTRUCTOR_P (fn))
377           /* We're only interested in calls to subobject constructors.  */
378           return true;
379       member = CALL_EXPR_ARG (t, 0);
380       /* We don't use build_cplus_new here because it complains about
381            abstract bases.  Leaving the call unwrapped means that it has the
382            wrong type, but cxx_eval_constant_expression doesn't care.  */
383       init = break_out_target_exprs (t);
384     }
385   else if (TREE_CODE (t) == BIND_EXPR)
386     return build_data_member_initialization (BIND_EXPR_BODY (t), vec);
387   else
388     /* Don't add anything else to the CONSTRUCTOR.  */
389     return true;
390   if (INDIRECT_REF_P (member))
391     member = TREE_OPERAND (member, 0);
392   if (TREE_CODE (member) == NOP_EXPR)
393     {
394       tree op = member;
395       STRIP_NOPS (op);
396       if (TREE_CODE (op) == ADDR_EXPR)
397           {
398             gcc_assert (same_type_ignoring_top_level_qualifiers_p
399                           (TREE_TYPE (TREE_TYPE (op)),
400                            TREE_TYPE (TREE_TYPE (member))));
401             /* Initializing a cv-qualified member; we need to look through
402                the const_cast.  */
403             member = op;
404           }
405       else if (op == current_class_ptr
406                  && (same_type_ignoring_top_level_qualifiers_p
407                        (TREE_TYPE (TREE_TYPE (member)),
408                         current_class_type)))
409           /* Delegating constructor.  */
410           member = op;
411       else
412           {
413             /* This is an initializer for an empty base; keep it for now so
414                we can check it in cxx_eval_bare_aggregate.  */
415             gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
416           }
417     }
418   if (TREE_CODE (member) == ADDR_EXPR)
419     member = TREE_OPERAND (member, 0);
420   if (TREE_CODE (member) == COMPONENT_REF)
421     {
422       tree aggr = TREE_OPERAND (member, 0);
423       if (TREE_CODE (aggr) == VAR_DECL)
424           /* Initializing a local variable, don't add anything.  */
425           return true;
426       if (TREE_CODE (aggr) != COMPONENT_REF)
427           /* Normal member initialization.  */
428           member = TREE_OPERAND (member, 1);
429       else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
430           /* Initializing a member of an anonymous union.  */
431           return build_anon_member_initialization (member, init, vec);
432       else
433           /* We're initializing a vtable pointer in a base.  Leave it as
434              COMPONENT_REF so we remember the path to get to the vfield.  */
435           gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
436     }
437 
438   /* Value-initialization can produce multiple initializers for the
439      same field; use the last one.  */
440   if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member)
441     (*vec)->last().value = init;
442   else
443     CONSTRUCTOR_APPEND_ELT (*vec, member, init);
444   return true;
445 }
446 
447 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
448    In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
449    BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations.  */
450 
451 static bool
check_constexpr_bind_expr_vars(tree t)452 check_constexpr_bind_expr_vars (tree t)
453 {
454   gcc_assert (TREE_CODE (t) == BIND_EXPR);
455 
456   for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
457     if (TREE_CODE (var) == TYPE_DECL
458           && DECL_IMPLICIT_TYPEDEF_P (var)
459           && !LAMBDA_TYPE_P (TREE_TYPE (var)))
460       return false;
461   return true;
462 }
463 
464 /* Subroutine of check_constexpr_ctor_body.  */
465 
466 static bool
check_constexpr_ctor_body_1(tree last,tree list)467 check_constexpr_ctor_body_1 (tree last, tree list)
468 {
469   switch (TREE_CODE (list))
470     {
471     case DECL_EXPR:
472       if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL
473             || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL)
474           return true;
475       return false;
476 
477     case CLEANUP_POINT_EXPR:
478       return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
479                                                   /*complain=*/false);
480 
481     case BIND_EXPR:
482        if (!check_constexpr_bind_expr_vars (list)
483              || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
484                                                     /*complain=*/false))
485            return false;
486        return true;
487 
488     case USING_STMT:
489     case STATIC_ASSERT:
490     case DEBUG_BEGIN_STMT:
491       return true;
492 
493     default:
494       return false;
495     }
496 }
497 
498 /* Make sure that there are no statements after LAST in the constructor
499    body represented by LIST.  */
500 
501 bool
check_constexpr_ctor_body(tree last,tree list,bool complain)502 check_constexpr_ctor_body (tree last, tree list, bool complain)
503 {
504   /* C++14 doesn't require a constexpr ctor to have an empty body.  */
505   if (cxx_dialect >= cxx14)
506     return true;
507 
508   bool ok = true;
509   if (TREE_CODE (list) == STATEMENT_LIST)
510     {
511       tree_stmt_iterator i = tsi_last (list);
512       for (; !tsi_end_p (i); tsi_prev (&i))
513           {
514             tree t = tsi_stmt (i);
515             if (t == last)
516               break;
517             if (!check_constexpr_ctor_body_1 (last, t))
518               {
519                 ok = false;
520                 break;
521               }
522           }
523     }
524   else if (list != last
525              && !check_constexpr_ctor_body_1 (last, list))
526     ok = false;
527   if (!ok)
528     {
529       if (complain)
530           error ("%<constexpr%> constructor does not have empty body");
531       DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
532     }
533   return ok;
534 }
535 
536 /* V is a vector of constructor elements built up for the base and member
537    initializers of a constructor for TYPE.  They need to be in increasing
538    offset order, which they might not be yet if TYPE has a primary base
539    which is not first in the base-clause or a vptr and at least one base
540    all of which are non-primary.  */
541 
542 static vec<constructor_elt, va_gc> *
sort_constexpr_mem_initializers(tree type,vec<constructor_elt,va_gc> * v)543 sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
544 {
545   tree pri = CLASSTYPE_PRIMARY_BINFO (type);
546   tree field_type;
547   unsigned i;
548   constructor_elt *ce;
549 
550   if (pri)
551     field_type = BINFO_TYPE (pri);
552   else if (TYPE_CONTAINS_VPTR_P (type))
553     field_type = vtbl_ptr_type_node;
554   else
555     return v;
556 
557   /* Find the element for the primary base or vptr and move it to the
558      beginning of the vec.  */
559   for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
560     if (TREE_TYPE (ce->index) == field_type)
561       break;
562 
563   if (i > 0 && i < vec_safe_length (v))
564     {
565       vec<constructor_elt, va_gc> &vref = *v;
566       constructor_elt elt = vref[i];
567       for (; i > 0; --i)
568           vref[i] = vref[i-1];
569       vref[0] = elt;
570     }
571 
572   return v;
573 }
574 
575 /* Build compile-time evalable representations of member-initializer list
576    for a constexpr constructor.  */
577 
578 static tree
build_constexpr_constructor_member_initializers(tree type,tree body)579 build_constexpr_constructor_member_initializers (tree type, tree body)
580 {
581   vec<constructor_elt, va_gc> *vec = NULL;
582   bool ok = true;
583   while (true)
584     switch (TREE_CODE (body))
585       {
586       case MUST_NOT_THROW_EXPR:
587       case EH_SPEC_BLOCK:
588           body = TREE_OPERAND (body, 0);
589           break;
590 
591       case STATEMENT_LIST:
592           for (tree stmt : tsi_range (body))
593             {
594               body = stmt;
595               if (TREE_CODE (body) == BIND_EXPR)
596                 break;
597             }
598           break;
599 
600       case BIND_EXPR:
601           body = BIND_EXPR_BODY (body);
602           goto found;
603 
604       default:
605           gcc_unreachable ();
606     }
607  found:
608   if (TREE_CODE (body) == TRY_BLOCK)
609     {
610       body = TREE_OPERAND (body, 0);
611       if (TREE_CODE (body) == BIND_EXPR)
612           body = BIND_EXPR_BODY (body);
613     }
614   if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
615     {
616       body = TREE_OPERAND (body, 0);
617       if (TREE_CODE (body) == EXPR_STMT)
618           body = TREE_OPERAND (body, 0);
619       if (TREE_CODE (body) == INIT_EXPR
620             && (same_type_ignoring_top_level_qualifiers_p
621                 (TREE_TYPE (TREE_OPERAND (body, 0)),
622                  current_class_type)))
623           {
624             /* Trivial copy.  */
625             return TREE_OPERAND (body, 1);
626           }
627       ok = build_data_member_initialization (body, &vec);
628     }
629   else if (TREE_CODE (body) == STATEMENT_LIST)
630     {
631       for (tree stmt : tsi_range (body))
632           {
633             ok = build_data_member_initialization (stmt, &vec);
634             if (!ok)
635               break;
636           }
637     }
638   else if (EXPR_P (body))
639     ok = build_data_member_initialization (body, &vec);
640   else
641     gcc_assert (errorcount > 0);
642   if (ok)
643     {
644       if (vec_safe_length (vec) > 0)
645           {
646             /* In a delegating constructor, return the target.  */
647             constructor_elt *ce = &(*vec)[0];
648             if (ce->index == current_class_ptr)
649               {
650                 body = ce->value;
651                 vec_free (vec);
652                 return body;
653               }
654           }
655       vec = sort_constexpr_mem_initializers (type, vec);
656       return build_constructor (type, vec);
657     }
658   else
659     return error_mark_node;
660 }
661 
662 /* We have an expression tree T that represents a call, either CALL_EXPR
663    or AGGR_INIT_EXPR.  If the call is lexically to a named function,
664    retrun the _DECL for that function.  */
665 
666 static tree
get_function_named_in_call(tree t)667 get_function_named_in_call (tree t)
668 {
669   tree fun = cp_get_callee (t);
670   if (fun && TREE_CODE (fun) == ADDR_EXPR
671       && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
672     fun = TREE_OPERAND (fun, 0);
673   return fun;
674 }
675 
676 /* Subroutine of check_constexpr_fundef.  BODY is the body of a function
677    declared to be constexpr, or a sub-statement thereof.  Returns the
678    return value if suitable, error_mark_node for a statement not allowed in
679    a constexpr function, or NULL_TREE if no return value was found.  */
680 
681 tree
constexpr_fn_retval(tree body)682 constexpr_fn_retval (tree body)
683 {
684   switch (TREE_CODE (body))
685     {
686     case STATEMENT_LIST:
687       {
688           tree expr = NULL_TREE;
689           for (tree stmt : tsi_range (body))
690             {
691               tree s = constexpr_fn_retval (stmt);
692               if (s == error_mark_node)
693                 return error_mark_node;
694               else if (s == NULL_TREE)
695                 /* Keep iterating.  */;
696               else if (expr)
697                 /* Multiple return statements.  */
698                 return error_mark_node;
699               else
700                 expr = s;
701             }
702           return expr;
703       }
704 
705     case RETURN_EXPR:
706       return break_out_target_exprs (TREE_OPERAND (body, 0));
707 
708     case DECL_EXPR:
709       {
710           tree decl = DECL_EXPR_DECL (body);
711           if (TREE_CODE (decl) == USING_DECL
712               /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__.  */
713               || DECL_ARTIFICIAL (decl))
714             return NULL_TREE;
715           return error_mark_node;
716       }
717 
718     case CLEANUP_POINT_EXPR:
719       return constexpr_fn_retval (TREE_OPERAND (body, 0));
720 
721     case BIND_EXPR:
722       if (!check_constexpr_bind_expr_vars (body))
723           return error_mark_node;
724       return constexpr_fn_retval (BIND_EXPR_BODY (body));
725 
726     case USING_STMT:
727     case DEBUG_BEGIN_STMT:
728       return NULL_TREE;
729 
730     case CALL_EXPR:
731           {
732             tree fun = get_function_named_in_call (body);
733             if (fun != NULL_TREE
734                 && fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE))
735               return NULL_TREE;
736           }
737       /* Fallthru.  */
738 
739     default:
740       return error_mark_node;
741     }
742 }
743 
744 /* Subroutine of check_constexpr_fundef.  BODY is the DECL_SAVED_TREE of
745    FUN; do the necessary transformations to turn it into a single expression
746    that we can store in the hash table.  */
747 
748 static tree
massage_constexpr_body(tree fun,tree body)749 massage_constexpr_body (tree fun, tree body)
750 {
751   if (DECL_CONSTRUCTOR_P (fun))
752     body = build_constexpr_constructor_member_initializers
753       (DECL_CONTEXT (fun), body);
754   else if (cxx_dialect < cxx14)
755     {
756       if (TREE_CODE (body) == EH_SPEC_BLOCK)
757         body = EH_SPEC_STMTS (body);
758       if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
759           body = TREE_OPERAND (body, 0);
760       body = constexpr_fn_retval (body);
761     }
762   return body;
763 }
764 
765 /* CTYPE is a type constructed from BODY.  Return true if some
766    bases/fields are uninitialized, and complain if COMPLAIN.  */
767 
768 static bool
cx_check_missing_mem_inits(tree ctype,tree body,bool complain)769 cx_check_missing_mem_inits (tree ctype, tree body, bool complain)
770 {
771   /* We allow uninitialized bases/fields in C++20.  */
772   if (cxx_dialect >= cxx20)
773     return false;
774 
775   unsigned nelts = 0;
776 
777   if (body)
778     {
779       if (TREE_CODE (body) != CONSTRUCTOR)
780           return false;
781       nelts = CONSTRUCTOR_NELTS (body);
782     }
783   tree field = TYPE_FIELDS (ctype);
784 
785   if (TREE_CODE (ctype) == UNION_TYPE)
786     {
787       if (nelts == 0 && next_initializable_field (field))
788           {
789             if (complain)
790               error ("%<constexpr%> constructor for union %qT must "
791                        "initialize exactly one non-static data member", ctype);
792             return true;
793           }
794       return false;
795     }
796 
797   /* Iterate over the CONSTRUCTOR, checking any missing fields don't
798      need an explicit initialization.  */
799   bool bad = false;
800   for (unsigned i = 0; i <= nelts; ++i)
801     {
802       tree index = NULL_TREE;
803       if (i < nelts)
804           {
805             index = CONSTRUCTOR_ELT (body, i)->index;
806             /* Skip base and vtable inits.  */
807             if (TREE_CODE (index) != FIELD_DECL
808                 || DECL_ARTIFICIAL (index))
809               continue;
810           }
811 
812       for (; field != index; field = DECL_CHAIN (field))
813           {
814             tree ftype;
815             if (TREE_CODE (field) != FIELD_DECL)
816               continue;
817             if (DECL_UNNAMED_BIT_FIELD (field))
818               continue;
819             if (DECL_ARTIFICIAL (field))
820               continue;
821             if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
822               {
823                 /* Recurse to check the anonymous aggregate member.  */
824                 bad |= cx_check_missing_mem_inits
825                     (TREE_TYPE (field), NULL_TREE, complain);
826                 if (bad && !complain)
827                     return true;
828                 continue;
829               }
830             ftype = TREE_TYPE (field);
831             if (!ftype || !TYPE_P (ftype) || !COMPLETE_TYPE_P (ftype))
832               /* A flexible array can't be intialized here, so don't complain
833                  that it isn't.  */
834               continue;
835             if (is_empty_field (field))
836               /* An empty field doesn't need an initializer.  */
837               continue;
838             ftype = strip_array_types (ftype);
839             if (type_has_constexpr_default_constructor (ftype))
840               {
841                 /* It's OK to skip a member with a trivial constexpr ctor.
842                    A constexpr ctor that isn't trivial should have been
843                    added in by now.  */
844                 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
845                                            || errorcount != 0);
846                 continue;
847               }
848             if (!complain)
849               return true;
850             auto_diagnostic_group d;
851             error ("member %qD must be initialized by mem-initializer "
852                      "in %<constexpr%> constructor", field);
853             inform (DECL_SOURCE_LOCATION (field), "declared here");
854             bad = true;
855           }
856       if (field == NULL_TREE)
857           break;
858 
859       if (ANON_AGGR_TYPE_P (TREE_TYPE (index)))
860           {
861             /* Check the anonymous aggregate initializer is valid.  */
862             bad |= cx_check_missing_mem_inits
863               (TREE_TYPE (index), CONSTRUCTOR_ELT (body, i)->value, complain);
864             if (bad && !complain)
865               return true;
866           }
867       field = DECL_CHAIN (field);
868     }
869 
870   return bad;
871 }
872 
873 /* We are processing the definition of the constexpr function FUN.
874    Check that its body fulfills the apropriate requirements and
875    enter it in the constexpr function definition table.  */
876 
877 void
maybe_save_constexpr_fundef(tree fun)878 maybe_save_constexpr_fundef (tree fun)
879 {
880   if (processing_template_decl
881       || cp_function_chain->invalid_constexpr
882       || (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun)))
883     return;
884 
885   /* With -fimplicit-constexpr, try to make inlines constexpr.  We'll
886      actually set DECL_DECLARED_CONSTEXPR_P below if the checks pass.  */
887   bool implicit = false;
888   if (flag_implicit_constexpr)
889     {
890       if (DECL_DELETING_DESTRUCTOR_P (fun)
891             && decl_implicit_constexpr_p (DECL_CLONED_FUNCTION (fun)))
892           /* Don't inherit implicit constexpr from the non-deleting
893              destructor.  */
894           DECL_DECLARED_CONSTEXPR_P (fun) = false;
895 
896       if (!DECL_DECLARED_CONSTEXPR_P (fun)
897             && DECL_DECLARED_INLINE_P (fun)
898             && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fun)))
899           implicit = true;
900     }
901 
902   if (!DECL_DECLARED_CONSTEXPR_P (fun) && !implicit)
903     return;
904 
905   bool complain = !DECL_GENERATED_P (fun) && !implicit;
906 
907   if (!is_valid_constexpr_fn (fun, complain))
908     return;
909 
910   tree massaged = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
911   if (massaged == NULL_TREE || massaged == error_mark_node)
912     {
913       if (!DECL_CONSTRUCTOR_P (fun) && complain)
914           error ("body of %<constexpr%> function %qD not a return-statement",
915                  fun);
916       return;
917     }
918 
919   bool potential = potential_rvalue_constant_expression (massaged);
920   if (!potential && complain)
921     require_potential_rvalue_constant_expression (massaged);
922 
923   if (DECL_CONSTRUCTOR_P (fun) && potential
924       && !DECL_DEFAULTED_FN (fun))
925     {
926       if (cx_check_missing_mem_inits (DECL_CONTEXT (fun),
927                                               massaged, complain))
928           potential = false;
929       else if (cxx_dialect > cxx11)
930           {
931             /* What we got from massage_constexpr_body is pretty much just the
932                ctor-initializer, also check the body.  */
933             massaged = DECL_SAVED_TREE (fun);
934             potential = potential_rvalue_constant_expression (massaged);
935             if (!potential && complain)
936               require_potential_rvalue_constant_expression (massaged);
937           }
938     }
939 
940   if (!potential && complain)
941     return;
942 
943   if (implicit)
944     {
945       if (potential)
946           {
947             DECL_DECLARED_CONSTEXPR_P (fun) = true;
948             DECL_LANG_SPECIFIC (fun)->u.fn.implicit_constexpr = true;
949             if (DECL_CONSTRUCTOR_P (fun))
950               TYPE_HAS_CONSTEXPR_CTOR (DECL_CONTEXT (fun)) = true;
951           }
952       else
953           /* Don't bother keeping the pre-generic body of unsuitable functions
954              not explicitly declared constexpr.  */
955           return;
956     }
957 
958   constexpr_fundef entry = {fun, NULL_TREE, NULL_TREE, NULL_TREE};
959   bool clear_ctx = false;
960   if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE)
961     {
962       clear_ctx = true;
963       DECL_CONTEXT (DECL_RESULT (fun)) = fun;
964     }
965   tree saved_fn = current_function_decl;
966   current_function_decl = fun;
967   entry.body = copy_fn (entry.decl, entry.parms, entry.result);
968   current_function_decl = saved_fn;
969   if (clear_ctx)
970     DECL_CONTEXT (DECL_RESULT (entry.decl)) = NULL_TREE;
971   if (!potential)
972     /* For a template instantiation, we want to remember the pre-generic body
973        for explain_invalid_constexpr_fn, but do tell cxx_eval_call_expression
974        that it doesn't need to bother trying to expand the function.  */
975     entry.result = error_mark_node;
976 
977   register_constexpr_fundef (entry);
978 }
979 
980 /* BODY is a validated and massaged definition of a constexpr
981    function.  Register it in the hash table.  */
982 
983 void
register_constexpr_fundef(const constexpr_fundef & value)984 register_constexpr_fundef (const constexpr_fundef &value)
985 {
986   /* Create the constexpr function table if necessary.  */
987   if (constexpr_fundef_table == NULL)
988     constexpr_fundef_table
989       = hash_table<constexpr_fundef_hasher>::create_ggc (101);
990 
991   constexpr_fundef **slot = constexpr_fundef_table->find_slot
992     (const_cast<constexpr_fundef *> (&value), INSERT);
993 
994   gcc_assert (*slot == NULL);
995   *slot = ggc_alloc<constexpr_fundef> ();
996   **slot = value;
997 }
998 
999 /* FUN is a non-constexpr function called in a context that requires a
1000    constant expression.  If it comes from a constexpr template, explain why
1001    the instantiation isn't constexpr.  */
1002 
1003 void
explain_invalid_constexpr_fn(tree fun)1004 explain_invalid_constexpr_fn (tree fun)
1005 {
1006   static hash_set<tree> *diagnosed;
1007   tree body;
1008   /* Only diagnose defaulted functions, lambdas, or instantiations.  */
1009   if (!DECL_DEFAULTED_FN (fun)
1010       && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun))
1011       && !is_instantiation_of_constexpr (fun))
1012     {
1013       inform (DECL_SOURCE_LOCATION (fun), "%qD declared here", fun);
1014       return;
1015     }
1016   if (diagnosed == NULL)
1017     diagnosed = new hash_set<tree>;
1018   if (diagnosed->add (fun))
1019     /* Already explained.  */
1020     return;
1021 
1022   iloc_sentinel ils = input_location;
1023   if (!lambda_static_thunk_p (fun))
1024     {
1025       /* Diagnostics should completely ignore the static thunk, so leave
1026            input_location set to our caller's location.  */
1027       input_location = DECL_SOURCE_LOCATION (fun);
1028       inform (input_location,
1029                 "%qD is not usable as a %<constexpr%> function because:", fun);
1030     }
1031   /* First check the declaration.  */
1032   if (is_valid_constexpr_fn (fun, true))
1033     {
1034       /* Then if it's OK, the body.  */
1035       if (!DECL_DECLARED_CONSTEXPR_P (fun)
1036             && DECL_DEFAULTED_FN (fun))
1037           explain_implicit_non_constexpr (fun);
1038       else
1039           {
1040             if (constexpr_fundef *fd = retrieve_constexpr_fundef (fun))
1041               body = fd->body;
1042             else
1043               body = DECL_SAVED_TREE (fun);
1044             body = massage_constexpr_body (fun, body);
1045             require_potential_rvalue_constant_expression (body);
1046             if (DECL_CONSTRUCTOR_P (fun))
1047               cx_check_missing_mem_inits (DECL_CONTEXT (fun), body, true);
1048           }
1049     }
1050 }
1051 
1052 /* Objects of this type represent calls to constexpr functions
1053    along with the bindings of parameters to their arguments, for
1054    the purpose of compile time evaluation.  */
1055 
1056 struct GTY((for_user)) constexpr_call {
1057   /* Description of the constexpr function definition.  */
1058   constexpr_fundef *fundef;
1059   /* Parameter bindings environment.  A TREE_VEC of arguments.  */
1060   tree bindings;
1061   /* Result of the call.
1062        NULL means the call is being evaluated.
1063        error_mark_node means that the evaluation was erroneous;
1064        otherwise, the actuall value of the call.  */
1065   tree result;
1066   /* The hash of this call; we remember it here to avoid having to
1067      recalculate it when expanding the hash table.  */
1068   hashval_t hash;
1069   /* Whether __builtin_is_constant_evaluated() should evaluate to true.  */
1070   bool manifestly_const_eval;
1071 };
1072 
1073 struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call>
1074 {
1075   static hashval_t hash (constexpr_call *);
1076   static bool equal (constexpr_call *, constexpr_call *);
1077 };
1078 
1079 enum constexpr_switch_state {
1080   /* Used when processing a switch for the first time by cxx_eval_switch_expr
1081      and default: label for that switch has not been seen yet.  */
1082   css_default_not_seen,
1083   /* Used when processing a switch for the first time by cxx_eval_switch_expr
1084      and default: label for that switch has been seen already.  */
1085   css_default_seen,
1086   /* Used when processing a switch for the second time by
1087      cxx_eval_switch_expr, where default: label should match.  */
1088   css_default_processing
1089 };
1090 
1091 /* The constexpr expansion context part which needs one instance per
1092    cxx_eval_outermost_constant_expr invocation.  VALUES is a map of values of
1093    variables initialized within the expression.  */
1094 
1095 struct constexpr_global_ctx {
1096   /* Values for any temporaries or local variables within the
1097      constant-expression. */
1098   hash_map<tree,tree> values;
1099   /* Number of cxx_eval_constant_expression calls (except skipped ones,
1100      on simple constants or location wrappers) encountered during current
1101      cxx_eval_outermost_constant_expr call.  */
1102   HOST_WIDE_INT constexpr_ops_count;
1103   /* Heap VAR_DECLs created during the evaluation of the outermost constant
1104      expression.  */
1105   auto_vec<tree, 16> heap_vars;
1106   /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR.  */
1107   vec<tree> *cleanups;
1108   /* Number of heap VAR_DECL deallocations.  */
1109   unsigned heap_dealloc_count;
1110   /* Constructor.  */
constexpr_global_ctxconstexpr_global_ctx1111   constexpr_global_ctx ()
1112     : constexpr_ops_count (0), cleanups (NULL), heap_dealloc_count (0) {}
1113 };
1114 
1115 /* The constexpr expansion context.  CALL is the current function
1116    expansion, CTOR is the current aggregate initializer, OBJECT is the
1117    object being initialized by CTOR, either a VAR_DECL or a _REF.    */
1118 
1119 struct constexpr_ctx {
1120   /* The part of the context that needs to be unique to the whole
1121      cxx_eval_outermost_constant_expr invocation.  */
1122   constexpr_global_ctx *global;
1123   /* The innermost call we're evaluating.  */
1124   constexpr_call *call;
1125   /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen
1126      within the current LOOP_EXPR.  NULL if we aren't inside a loop.  */
1127   vec<tree> *save_exprs;
1128   /* The CONSTRUCTOR we're currently building up for an aggregate
1129      initializer.  */
1130   tree ctor;
1131   /* The object we're building the CONSTRUCTOR for.  */
1132   tree object;
1133   /* If inside SWITCH_EXPR.  */
1134   constexpr_switch_state *css_state;
1135   /* The aggregate initialization context inside which this one is nested.  This
1136      is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs.  */
1137   const constexpr_ctx *parent;
1138 
1139   /* Whether we should error on a non-constant expression or fail quietly.
1140      This flag needs to be here, but some of the others could move to global
1141      if they get larger than a word.  */
1142   bool quiet;
1143   /* Whether we are strictly conforming to constant expression rules or
1144      trying harder to get a constant value.  */
1145   bool strict;
1146   /* Whether __builtin_is_constant_evaluated () should be true.  */
1147   bool manifestly_const_eval;
1148 };
1149 
1150 /* This internal flag controls whether we should avoid doing anything during
1151    constexpr evaluation that would cause extra DECL_UID generation, such as
1152    template instantiation and function body copying.  */
1153 
1154 static bool uid_sensitive_constexpr_evaluation_value;
1155 
1156 /* An internal counter that keeps track of the number of times
1157    uid_sensitive_constexpr_evaluation_p returned true.  */
1158 
1159 static unsigned uid_sensitive_constexpr_evaluation_true_counter;
1160 
1161 /* The accessor for uid_sensitive_constexpr_evaluation_value which also
1162    increments the corresponding counter.  */
1163 
1164 static bool
uid_sensitive_constexpr_evaluation_p()1165 uid_sensitive_constexpr_evaluation_p ()
1166 {
1167   if (uid_sensitive_constexpr_evaluation_value)
1168     {
1169       ++uid_sensitive_constexpr_evaluation_true_counter;
1170       return true;
1171     }
1172   else
1173     return false;
1174 }
1175 
1176 /* The default constructor for uid_sensitive_constexpr_evaluation_sentinel
1177    enables the internal flag for uid_sensitive_constexpr_evaluation_p
1178    during the lifetime of the sentinel object.  Upon its destruction, the
1179    previous value of uid_sensitive_constexpr_evaluation_p is restored.  */
1180 
1181 uid_sensitive_constexpr_evaluation_sentinel
uid_sensitive_constexpr_evaluation_sentinel()1182 ::uid_sensitive_constexpr_evaluation_sentinel ()
1183   : ovr (uid_sensitive_constexpr_evaluation_value, true)
1184 {
1185 }
1186 
1187 /* The default constructor for uid_sensitive_constexpr_evaluation_checker
1188    records the current number of times that uid_sensitive_constexpr_evaluation_p
1189    has been called and returned true.  */
1190 
1191 uid_sensitive_constexpr_evaluation_checker
uid_sensitive_constexpr_evaluation_checker()1192 ::uid_sensitive_constexpr_evaluation_checker ()
1193   : saved_counter (uid_sensitive_constexpr_evaluation_true_counter)
1194 {
1195 }
1196 
1197 /* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and
1198    some constexpr evaluation was restricted due to u_s_c_e_p being called
1199    and returning true during the lifetime of this checker object.  */
1200 
1201 bool
evaluation_restricted_p() const1202 uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const
1203 {
1204   return (uid_sensitive_constexpr_evaluation_value
1205             && saved_counter != uid_sensitive_constexpr_evaluation_true_counter);
1206 }
1207 
1208 
1209 /* A table of all constexpr calls that have been evaluated by the
1210    compiler in this translation unit.  */
1211 
1212 static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
1213 
1214 static tree cxx_eval_constant_expression (const constexpr_ctx *, tree,
1215                                                     bool, bool *, bool *, tree * = NULL);
1216 
1217 /* Compute a hash value for a constexpr call representation.  */
1218 
1219 inline hashval_t
hash(constexpr_call * info)1220 constexpr_call_hasher::hash (constexpr_call *info)
1221 {
1222   return info->hash;
1223 }
1224 
1225 /* Return true if the objects pointed to by P and Q represent calls
1226    to the same constexpr function with the same arguments.
1227    Otherwise, return false.  */
1228 
1229 bool
equal(constexpr_call * lhs,constexpr_call * rhs)1230 constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
1231 {
1232   if (lhs == rhs)
1233     return true;
1234   if (lhs->hash != rhs->hash)
1235     return false;
1236   if (lhs->manifestly_const_eval != rhs->manifestly_const_eval)
1237     return false;
1238   if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
1239     return false;
1240   return cp_tree_equal (lhs->bindings, rhs->bindings);
1241 }
1242 
1243 /* Initialize the constexpr call table, if needed.  */
1244 
1245 static void
maybe_initialize_constexpr_call_table(void)1246 maybe_initialize_constexpr_call_table (void)
1247 {
1248   if (constexpr_call_table == NULL)
1249     constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
1250 }
1251 
1252 /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1253    a function happens to get called recursively, we unshare the callee
1254    function's body and evaluate this unshared copy instead of evaluating the
1255    original body.
1256 
1257    FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1258    copies.  The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
1259    that's keyed off of the original FUNCTION_DECL and whose value is a
1260    TREE_LIST of this function's unused copies awaiting reuse.
1261 
1262    This is not GC-deletable to avoid GC affecting UID generation.  */
1263 
1264 static GTY(()) decl_tree_map *fundef_copies_table;
1265 
1266 /* Reuse a copy or create a new unshared copy of the function FUN.
1267    Return this copy.  We use a TREE_LIST whose PURPOSE is body, VALUE
1268    is parms, TYPE is result.  */
1269 
1270 static tree
get_fundef_copy(constexpr_fundef * fundef)1271 get_fundef_copy (constexpr_fundef *fundef)
1272 {
1273   tree copy;
1274   bool existed;
1275   tree *slot = &(hash_map_safe_get_or_insert<hm_ggc>
1276                      (fundef_copies_table, fundef->decl, &existed, 127));
1277 
1278   if (!existed)
1279     {
1280       /* There is no cached function available, or in use.  We can use
1281            the function directly.  That the slot is now created records
1282            that this function is now in use.  */
1283       copy = build_tree_list (fundef->body, fundef->parms);
1284       TREE_TYPE (copy) = fundef->result;
1285     }
1286   else if (*slot == NULL_TREE)
1287     {
1288       if (uid_sensitive_constexpr_evaluation_p ())
1289           return NULL_TREE;
1290 
1291       /* We've already used the function itself, so make a copy.  */
1292       copy = build_tree_list (NULL, NULL);
1293       tree saved_body = DECL_SAVED_TREE (fundef->decl);
1294       tree saved_parms = DECL_ARGUMENTS (fundef->decl);
1295       tree saved_result = DECL_RESULT (fundef->decl);
1296       tree saved_fn = current_function_decl;
1297       DECL_SAVED_TREE (fundef->decl) = fundef->body;
1298       DECL_ARGUMENTS (fundef->decl) = fundef->parms;
1299       DECL_RESULT (fundef->decl) = fundef->result;
1300       current_function_decl = fundef->decl;
1301       TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy),
1302                                              TREE_TYPE (copy));
1303       current_function_decl = saved_fn;
1304       DECL_RESULT (fundef->decl) = saved_result;
1305       DECL_ARGUMENTS (fundef->decl) = saved_parms;
1306       DECL_SAVED_TREE (fundef->decl) = saved_body;
1307     }
1308   else
1309     {
1310       /* We have a cached function available.  */
1311       copy = *slot;
1312       *slot = TREE_CHAIN (copy);
1313     }
1314 
1315   return copy;
1316 }
1317 
1318 /* Save the copy COPY of function FUN for later reuse by
1319    get_fundef_copy().  By construction, there will always be an entry
1320    to find.  */
1321 
1322 static void
save_fundef_copy(tree fun,tree copy)1323 save_fundef_copy (tree fun, tree copy)
1324 {
1325   tree *slot = fundef_copies_table->get (fun);
1326   TREE_CHAIN (copy) = *slot;
1327   *slot = copy;
1328 }
1329 
1330 /* We have an expression tree T that represents a call, either CALL_EXPR
1331    or AGGR_INIT_EXPR.  Return the Nth argument.  */
1332 
1333 static inline tree
get_nth_callarg(tree t,int n)1334 get_nth_callarg (tree t, int n)
1335 {
1336   switch (TREE_CODE (t))
1337     {
1338     case CALL_EXPR:
1339       return CALL_EXPR_ARG (t, n);
1340 
1341     case AGGR_INIT_EXPR:
1342       return AGGR_INIT_EXPR_ARG (t, n);
1343 
1344     default:
1345       gcc_unreachable ();
1346       return NULL;
1347     }
1348 }
1349 
1350 /* Attempt to evaluate T which represents a call to a builtin function.
1351    We assume here that all builtin functions evaluate to scalar types
1352    represented by _CST nodes.  */
1353 
1354 static tree
cxx_eval_builtin_function_call(const constexpr_ctx * ctx,tree t,tree fun,bool lval,bool * non_constant_p,bool * overflow_p)1355 cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun,
1356                                         bool lval,
1357                                         bool *non_constant_p, bool *overflow_p)
1358 {
1359   const int nargs = call_expr_nargs (t);
1360   tree *args = (tree *) alloca (nargs * sizeof (tree));
1361   tree new_call;
1362   int i;
1363 
1364   /* Don't fold __builtin_constant_p within a constexpr function.  */
1365   bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun);
1366 
1367   /* If we aren't requiring a constant expression, defer __builtin_constant_p
1368      in a constexpr function until we have values for the parameters.  */
1369   if (bi_const_p
1370       && !ctx->manifestly_const_eval
1371       && current_function_decl
1372       && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
1373     {
1374       *non_constant_p = true;
1375       return t;
1376     }
1377 
1378   /* For __builtin_is_constant_evaluated, defer it if not
1379      ctx->manifestly_const_eval (as sometimes we try to constant evaluate
1380      without manifestly_const_eval even expressions or parts thereof which
1381      will later be manifestly const_eval evaluated), otherwise fold it to
1382      true.  */
1383   if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
1384                                BUILT_IN_FRONTEND))
1385     {
1386       if (!ctx->manifestly_const_eval)
1387           {
1388             *non_constant_p = true;
1389             return t;
1390           }
1391       return boolean_true_node;
1392     }
1393 
1394   if (fndecl_built_in_p (fun, CP_BUILT_IN_SOURCE_LOCATION, BUILT_IN_FRONTEND))
1395     {
1396       temp_override<tree> ovr (current_function_decl);
1397       if (ctx->call && ctx->call->fundef)
1398           current_function_decl = ctx->call->fundef->decl;
1399       return fold_builtin_source_location (EXPR_LOCATION (t));
1400     }
1401 
1402   int strops = 0;
1403   int strret = 0;
1404   if (fndecl_built_in_p (fun, BUILT_IN_NORMAL))
1405     switch (DECL_FUNCTION_CODE (fun))
1406       {
1407       case BUILT_IN_STRLEN:
1408       case BUILT_IN_STRNLEN:
1409           strops = 1;
1410           break;
1411       case BUILT_IN_MEMCHR:
1412       case BUILT_IN_STRCHR:
1413       case BUILT_IN_STRRCHR:
1414           strops = 1;
1415           strret = 1;
1416           break;
1417       case BUILT_IN_MEMCMP:
1418       case BUILT_IN_STRCMP:
1419           strops = 2;
1420           break;
1421       case BUILT_IN_STRSTR:
1422           strops = 2;
1423           strret = 1;
1424           break;
1425       case BUILT_IN_ASAN_POINTER_COMPARE:
1426       case BUILT_IN_ASAN_POINTER_SUBTRACT:
1427           /* These builtins shall be ignored during constant expression
1428              evaluation.  */
1429           return void_node;
1430       default:
1431           break;
1432       }
1433 
1434   /* Be permissive for arguments to built-ins; __builtin_constant_p should
1435      return constant false for a non-constant argument.  */
1436   constexpr_ctx new_ctx = *ctx;
1437   new_ctx.quiet = true;
1438   for (i = 0; i < nargs; ++i)
1439     {
1440       tree arg = CALL_EXPR_ARG (t, i);
1441       tree oarg = arg;
1442 
1443       /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
1444            expand_builtin doesn't know how to look in the values table.  */
1445       bool strop = i < strops;
1446       if (strop)
1447           {
1448             STRIP_NOPS (arg);
1449             if (TREE_CODE (arg) == ADDR_EXPR)
1450               arg = TREE_OPERAND (arg, 0);
1451             else
1452               strop = false;
1453           }
1454 
1455       /* If builtin_valid_in_constant_expr_p is true,
1456            potential_constant_expression_1 has not recursed into the arguments
1457            of the builtin, verify it here.  */
1458       if (!builtin_valid_in_constant_expr_p (fun)
1459             || potential_constant_expression (arg))
1460           {
1461             bool dummy1 = false, dummy2 = false;
1462             arg = cxx_eval_constant_expression (&new_ctx, arg, false,
1463                                                         &dummy1, &dummy2);
1464           }
1465 
1466       if (bi_const_p)
1467           /* For __builtin_constant_p, fold all expressions with constant values
1468              even if they aren't C++ constant-expressions.  */
1469           arg = cp_fold_rvalue (arg);
1470       else if (strop)
1471           {
1472             if (TREE_CODE (arg) == CONSTRUCTOR)
1473               arg = braced_lists_to_strings (TREE_TYPE (arg), arg);
1474             if (TREE_CODE (arg) == STRING_CST)
1475               arg = build_address (arg);
1476             else
1477               arg = oarg;
1478           }
1479 
1480       args[i] = arg;
1481     }
1482 
1483   bool save_ffbcp = force_folding_builtin_constant_p;
1484   force_folding_builtin_constant_p |= ctx->manifestly_const_eval;
1485   tree save_cur_fn = current_function_decl;
1486   /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION ().  */
1487   if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION)
1488       && ctx->call
1489       && ctx->call->fundef)
1490     current_function_decl = ctx->call->fundef->decl;
1491   if (fndecl_built_in_p (fun,
1492                                CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
1493                                BUILT_IN_FRONTEND))
1494     {
1495       location_t loc = EXPR_LOCATION (t);
1496       if (nargs >= 1)
1497           VERIFY_CONSTANT (args[0]);
1498       new_call
1499           = fold_builtin_is_pointer_inverconvertible_with_class (loc, nargs,
1500                                                                              args);
1501     }
1502   else if (fndecl_built_in_p (fun,
1503                                     CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
1504                                     BUILT_IN_FRONTEND))
1505     {
1506       location_t loc = EXPR_LOCATION (t);
1507       if (nargs >= 2)
1508           {
1509             VERIFY_CONSTANT (args[0]);
1510             VERIFY_CONSTANT (args[1]);
1511           }
1512       new_call = fold_builtin_is_corresponding_member (loc, nargs, args);
1513     }
1514   else
1515     new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t),
1516                                                   CALL_EXPR_FN (t), nargs, args);
1517   current_function_decl = save_cur_fn;
1518   force_folding_builtin_constant_p = save_ffbcp;
1519   if (new_call == NULL)
1520     {
1521       if (!*non_constant_p && !ctx->quiet)
1522           {
1523             /* Do not allow__builtin_unreachable in constexpr function.
1524                The __builtin_unreachable call with BUILTINS_LOCATION
1525                comes from cp_maybe_instrument_return.  */
1526             if (fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE)
1527                 && EXPR_LOCATION (t) == BUILTINS_LOCATION)
1528               error ("%<constexpr%> call flows off the end of the function");
1529             else
1530               {
1531                 new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
1532                                                          CALL_EXPR_FN (t), nargs, args);
1533                 error ("%q+E is not a constant expression", new_call);
1534               }
1535           }
1536       *non_constant_p = true;
1537       return t;
1538     }
1539 
1540   if (!potential_constant_expression (new_call))
1541     {
1542       if (!*non_constant_p && !ctx->quiet)
1543           error ("%q+E is not a constant expression", new_call);
1544       *non_constant_p = true;
1545       return t;
1546     }
1547 
1548   if (strret)
1549     {
1550       /* memchr returns a pointer into the first argument, but we replaced the
1551            argument above with a STRING_CST; put it back it now.  */
1552       tree op = CALL_EXPR_ARG (t, strret-1);
1553       STRIP_NOPS (new_call);
1554       if (TREE_CODE (new_call) == POINTER_PLUS_EXPR)
1555           TREE_OPERAND (new_call, 0) = op;
1556       else if (TREE_CODE (new_call) == ADDR_EXPR)
1557           new_call = op;
1558     }
1559 
1560   return cxx_eval_constant_expression (&new_ctx, new_call, lval,
1561                                                non_constant_p, overflow_p);
1562 }
1563 
1564 /* TEMP is the constant value of a temporary object of type TYPE.  Adjust
1565    the type of the value to match.  */
1566 
1567 static tree
adjust_temp_type(tree type,tree temp)1568 adjust_temp_type (tree type, tree temp)
1569 {
1570   if (same_type_p (TREE_TYPE (temp), type))
1571     return temp;
1572   /* Avoid wrapping an aggregate value in a NOP_EXPR.  */
1573   if (TREE_CODE (temp) == CONSTRUCTOR)
1574     {
1575       /* build_constructor wouldn't retain various CONSTRUCTOR flags.  */
1576       tree t = copy_node (temp);
1577       TREE_TYPE (t) = type;
1578       return t;
1579     }
1580   if (TREE_CODE (temp) == EMPTY_CLASS_EXPR)
1581     return build0 (EMPTY_CLASS_EXPR, type);
1582   gcc_assert (scalarish_type_p (type));
1583   /* Now we know we're dealing with a scalar, and a prvalue of non-class
1584      type is cv-unqualified.  */
1585   return cp_fold_convert (cv_unqualified (type), temp);
1586 }
1587 
1588 /* If T is a CONSTRUCTOR, return an unshared copy of T and any
1589    sub-CONSTRUCTORs.  Otherwise return T.
1590 
1591    We use this whenever we initialize an object as a whole, whether it's a
1592    parameter, a local variable, or a subobject, so that subsequent
1593    modifications don't affect other places where it was used.  */
1594 
1595 tree
unshare_constructor(tree t MEM_STAT_DECL)1596 unshare_constructor (tree t MEM_STAT_DECL)
1597 {
1598   if (!t || TREE_CODE (t) != CONSTRUCTOR)
1599     return t;
1600   auto_vec <tree*, 4> ptrs;
1601   ptrs.safe_push (&t);
1602   while (!ptrs.is_empty ())
1603     {
1604       tree *p = ptrs.pop ();
1605       tree n = copy_node (*p PASS_MEM_STAT);
1606       CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT);
1607       *p = n;
1608       vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n);
1609       constructor_elt *ce;
1610       for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i)
1611           if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
1612             ptrs.safe_push (&ce->value);
1613     }
1614   return t;
1615 }
1616 
1617 /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs.  */
1618 
1619 static void
free_constructor(tree t)1620 free_constructor (tree t)
1621 {
1622   if (!t || TREE_CODE (t) != CONSTRUCTOR)
1623     return;
1624   releasing_vec ctors;
1625   vec_safe_push (ctors, t);
1626   while (!ctors->is_empty ())
1627     {
1628       tree c = ctors->pop ();
1629       if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c))
1630           {
1631             constructor_elt *ce;
1632             for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
1633               if (TREE_CODE (ce->value) == CONSTRUCTOR)
1634                 vec_safe_push (ctors, ce->value);
1635             ggc_free (elts);
1636           }
1637       ggc_free (c);
1638     }
1639 }
1640 
1641 /* Helper function of cxx_bind_parameters_in_call.  Return non-NULL
1642    if *TP is address of a static variable (or part of it) currently being
1643    constructed or of a heap artificial variable.  */
1644 
1645 static tree
addr_of_non_const_var(tree * tp,int * walk_subtrees,void * data)1646 addr_of_non_const_var (tree *tp, int *walk_subtrees, void *data)
1647 {
1648   if (TREE_CODE (*tp) == ADDR_EXPR)
1649     if (tree var = get_base_address (TREE_OPERAND (*tp, 0)))
1650       if (VAR_P (var) && TREE_STATIC (var))
1651           {
1652             if (DECL_NAME (var) == heap_uninit_identifier
1653                 || DECL_NAME (var) == heap_identifier
1654                 || DECL_NAME (var) == heap_vec_uninit_identifier
1655                 || DECL_NAME (var) == heap_vec_identifier)
1656               return var;
1657 
1658             constexpr_global_ctx *global = (constexpr_global_ctx *) data;
1659             if (global->values.get (var))
1660               return var;
1661           }
1662   if (TYPE_P (*tp))
1663     *walk_subtrees = false;
1664   return NULL_TREE;
1665 }
1666 
1667 /* Subroutine of cxx_eval_call_expression.
1668    We are processing a call expression (either CALL_EXPR or
1669    AGGR_INIT_EXPR) in the context of CTX.  Evaluate
1670    all arguments and bind their values to correspondings
1671    parameters, making up the NEW_CALL context.  */
1672 
1673 static tree
cxx_bind_parameters_in_call(const constexpr_ctx * ctx,tree t,tree fun,bool * non_constant_p,bool * overflow_p,bool * non_constant_args)1674 cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, tree fun,
1675                                    bool *non_constant_p, bool *overflow_p,
1676                                    bool *non_constant_args)
1677 {
1678   const int nargs = call_expr_nargs (t);
1679   tree parms = DECL_ARGUMENTS (fun);
1680   int i;
1681   /* We don't record ellipsis args below.  */
1682   int nparms = list_length (parms);
1683   int nbinds = nargs < nparms ? nargs : nparms;
1684   tree binds = make_tree_vec (nbinds);
1685   for (i = 0; i < nargs; ++i)
1686     {
1687       tree x, arg;
1688       tree type = parms ? TREE_TYPE (parms) : void_type_node;
1689       if (parms && DECL_BY_REFERENCE (parms))
1690           type = TREE_TYPE (type);
1691       x = get_nth_callarg (t, i);
1692       /* For member function, the first argument is a pointer to the implied
1693          object.  For a constructor, it might still be a dummy object, in
1694          which case we get the real argument from ctx. */
1695       if (i == 0 && DECL_CONSTRUCTOR_P (fun)
1696             && is_dummy_object (x))
1697           {
1698             x = ctx->object;
1699             x = build_address (x);
1700           }
1701       if (TREE_ADDRESSABLE (type))
1702           /* Undo convert_for_arg_passing work here.  */
1703           x = convert_from_reference (x);
1704       /* Normally we would strip a TARGET_EXPR in an initialization context
1705            such as this, but here we do the elision differently: we keep the
1706            TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm.  */
1707       arg = cxx_eval_constant_expression (ctx, x, /*lval=*/false,
1708                                                     non_constant_p, overflow_p);
1709       /* Don't VERIFY_CONSTANT here.  */
1710       if (*non_constant_p && ctx->quiet)
1711           break;
1712       /* Just discard ellipsis args after checking their constantitude.  */
1713       if (!parms)
1714           continue;
1715 
1716       if (!*non_constant_p)
1717           {
1718             /* Make sure the binding has the same type as the parm.  But
1719                only for constant args.  */
1720             if (!TYPE_REF_P (type))
1721               arg = adjust_temp_type (type, arg);
1722             if (!TREE_CONSTANT (arg))
1723               *non_constant_args = true;
1724             else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1725               /* The destructor needs to see any modifications the callee makes
1726                  to the argument.  */
1727               *non_constant_args = true;
1728               /* If arg is or contains address of a heap artificial variable or
1729                  of a static variable being constructed, avoid caching the
1730                  function call, as those variables might be modified by the
1731                  function, or might be modified by the callers in between
1732                  the cached function and just read by the function.  */
1733             else if (!*non_constant_args
1734                        && cp_walk_tree (&arg, addr_of_non_const_var, ctx->global,
1735                                             NULL))
1736               *non_constant_args = true;
1737 
1738             /* For virtual calls, adjust the this argument, so that it is
1739                the object on which the method is called, rather than
1740                one of its bases.  */
1741             if (i == 0 && DECL_VIRTUAL_P (fun))
1742               {
1743                 tree addr = arg;
1744                 STRIP_NOPS (addr);
1745                 if (TREE_CODE (addr) == ADDR_EXPR)
1746                     {
1747                       tree obj = TREE_OPERAND (addr, 0);
1748                       while (TREE_CODE (obj) == COMPONENT_REF
1749                                && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1))
1750                                && !same_type_ignoring_top_level_qualifiers_p
1751                                                   (TREE_TYPE (obj), DECL_CONTEXT (fun)))
1752                         obj = TREE_OPERAND (obj, 0);
1753                       if (obj != TREE_OPERAND (addr, 0))
1754                         arg = build_fold_addr_expr_with_type (obj,
1755                                                                         TREE_TYPE (arg));
1756                     }
1757               }
1758             TREE_VEC_ELT (binds, i) = arg;
1759           }
1760       parms = TREE_CHAIN (parms);
1761     }
1762 
1763   return binds;
1764 }
1765 
1766 /* Variables and functions to manage constexpr call expansion context.
1767    These do not need to be marked for PCH or GC.  */
1768 
1769 /* FIXME remember and print actual constant arguments.  */
1770 static vec<tree> call_stack;
1771 static int call_stack_tick;
1772 static int last_cx_error_tick;
1773 
1774 static int
push_cx_call_context(tree call)1775 push_cx_call_context (tree call)
1776 {
1777   ++call_stack_tick;
1778   if (!EXPR_HAS_LOCATION (call))
1779     SET_EXPR_LOCATION (call, input_location);
1780   call_stack.safe_push (call);
1781   int len = call_stack.length ();
1782   if (len > max_constexpr_depth)
1783     return false;
1784   return len;
1785 }
1786 
1787 static void
pop_cx_call_context(void)1788 pop_cx_call_context (void)
1789 {
1790   ++call_stack_tick;
1791   call_stack.pop ();
1792 }
1793 
1794 vec<tree>
cx_error_context(void)1795 cx_error_context (void)
1796 {
1797   vec<tree> r = vNULL;
1798   if (call_stack_tick != last_cx_error_tick
1799       && !call_stack.is_empty ())
1800     r = call_stack;
1801   last_cx_error_tick = call_stack_tick;
1802   return r;
1803 }
1804 
1805 /* Evaluate a call T to a GCC internal function when possible and return
1806    the evaluated result or, under the control of CTX, give an error, set
1807    NON_CONSTANT_P, and return the unevaluated call T otherwise.  */
1808 
1809 static tree
cxx_eval_internal_function(const constexpr_ctx * ctx,tree t,bool lval,bool * non_constant_p,bool * overflow_p)1810 cxx_eval_internal_function (const constexpr_ctx *ctx, tree t,
1811                                   bool lval,
1812                                   bool *non_constant_p, bool *overflow_p)
1813 {
1814   enum tree_code opcode = ERROR_MARK;
1815 
1816   switch (CALL_EXPR_IFN (t))
1817     {
1818     case IFN_UBSAN_NULL:
1819     case IFN_UBSAN_BOUNDS:
1820     case IFN_UBSAN_VPTR:
1821     case IFN_FALLTHROUGH:
1822       return void_node;
1823 
1824     case IFN_ADD_OVERFLOW:
1825       opcode = PLUS_EXPR;
1826       break;
1827     case IFN_SUB_OVERFLOW:
1828       opcode = MINUS_EXPR;
1829       break;
1830     case IFN_MUL_OVERFLOW:
1831       opcode = MULT_EXPR;
1832       break;
1833 
1834     case IFN_LAUNDER:
1835       return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
1836                                                      false, non_constant_p, overflow_p);
1837 
1838     case IFN_VEC_CONVERT:
1839       {
1840           tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
1841                                                              false, non_constant_p,
1842                                                              overflow_p);
1843           if (TREE_CODE (arg) == VECTOR_CST)
1844             if (tree r = fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg))
1845               return r;
1846       }
1847       /* FALLTHRU */
1848 
1849     default:
1850       if (!ctx->quiet)
1851           error_at (cp_expr_loc_or_input_loc (t),
1852                       "call to internal function %qE", t);
1853       *non_constant_p = true;
1854       return t;
1855     }
1856 
1857   /* Evaluate constant arguments using OPCODE and return a complex
1858      number containing the result and the overflow bit.  */
1859   tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval,
1860                                                       non_constant_p, overflow_p);
1861   tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval,
1862                                                       non_constant_p, overflow_p);
1863 
1864   if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1865     {
1866       location_t loc = cp_expr_loc_or_input_loc (t);
1867       tree type = TREE_TYPE (TREE_TYPE (t));
1868       tree result = fold_binary_loc (loc, opcode, type,
1869                                              fold_convert_loc (loc, type, arg0),
1870                                              fold_convert_loc (loc, type, arg1));
1871       tree ovf
1872           = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1));
1873       /* Reset TREE_OVERFLOW to avoid warnings for the overflow.  */
1874       if (TREE_OVERFLOW (result))
1875           TREE_OVERFLOW (result) = 0;
1876 
1877       return build_complex (TREE_TYPE (t), result, ovf);
1878     }
1879 
1880   *non_constant_p = true;
1881   return t;
1882 }
1883 
1884 /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates.  */
1885 
1886 static void
clear_no_implicit_zero(tree ctor)1887 clear_no_implicit_zero (tree ctor)
1888 {
1889   if (CONSTRUCTOR_NO_CLEARING (ctor))
1890     {
1891       CONSTRUCTOR_NO_CLEARING (ctor) = false;
1892       for (auto &e: CONSTRUCTOR_ELTS (ctor))
1893           if (TREE_CODE (e.value) == CONSTRUCTOR)
1894             clear_no_implicit_zero (e.value);
1895     }
1896 }
1897 
1898 /* Complain about a const object OBJ being modified in a constant expression.
1899    EXPR is the MODIFY_EXPR expression performing the modification.  */
1900 
1901 static void
modifying_const_object_error(tree expr,tree obj)1902 modifying_const_object_error (tree expr, tree obj)
1903 {
1904   location_t loc = cp_expr_loc_or_input_loc (expr);
1905   auto_diagnostic_group d;
1906   error_at (loc, "modifying a const object %qE is not allowed in "
1907               "a constant expression", TREE_OPERAND (expr, 0));
1908   inform (location_of (obj), "originally declared %<const%> here");
1909 }
1910 
1911 /* Return true if FNDECL is a replaceable global allocation function that
1912    should be useable during constant expression evaluation.  */
1913 
1914 static inline bool
cxx_replaceable_global_alloc_fn(tree fndecl)1915 cxx_replaceable_global_alloc_fn (tree fndecl)
1916 {
1917   return (cxx_dialect >= cxx20
1918             && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl))
1919             && CP_DECL_CONTEXT (fndecl) == global_namespace
1920             && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
1921                 || DECL_IS_OPERATOR_DELETE_P (fndecl)));
1922 }
1923 
1924 /* Return true if FNDECL is a placement new function that should be
1925    useable during constant expression evaluation of std::construct_at.  */
1926 
1927 static inline bool
cxx_placement_new_fn(tree fndecl)1928 cxx_placement_new_fn (tree fndecl)
1929 {
1930   if (cxx_dialect >= cxx20
1931       && IDENTIFIER_NEW_OP_P (DECL_NAME (fndecl))
1932       && CP_DECL_CONTEXT (fndecl) == global_namespace
1933       && !DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
1934       && TREE_CODE (TREE_TYPE (fndecl)) == FUNCTION_TYPE)
1935     {
1936       tree first_arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
1937       if (TREE_VALUE (first_arg) == ptr_type_node
1938             && TREE_CHAIN (first_arg) == void_list_node)
1939           return true;
1940     }
1941   return false;
1942 }
1943 
1944 /* Return true if FNDECL is std::construct_at.  */
1945 
1946 static inline bool
is_std_construct_at(tree fndecl)1947 is_std_construct_at (tree fndecl)
1948 {
1949   if (!decl_in_std_namespace_p (fndecl))
1950     return false;
1951 
1952   tree name = DECL_NAME (fndecl);
1953   return name && id_equal (name, "construct_at");
1954 }
1955 
1956 /* Overload for the above taking constexpr_call*.  */
1957 
1958 static inline bool
is_std_construct_at(const constexpr_call * call)1959 is_std_construct_at (const constexpr_call *call)
1960 {
1961   return (call
1962             && call->fundef
1963             && is_std_construct_at (call->fundef->decl));
1964 }
1965 
1966 /* Return true if FNDECL is std::allocator<T>::{,de}allocate.  */
1967 
1968 static inline bool
is_std_allocator_allocate(tree fndecl)1969 is_std_allocator_allocate (tree fndecl)
1970 {
1971   tree name = DECL_NAME (fndecl);
1972   if (name == NULL_TREE
1973       || !(id_equal (name, "allocate") || id_equal (name, "deallocate")))
1974     return false;
1975 
1976   tree ctx = DECL_CONTEXT (fndecl);
1977   if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
1978     return false;
1979 
1980   tree decl = TYPE_MAIN_DECL (ctx);
1981   name = DECL_NAME (decl);
1982   if (name == NULL_TREE || !id_equal (name, "allocator"))
1983     return false;
1984 
1985   return decl_in_std_namespace_p (decl);
1986 }
1987 
1988 /* Overload for the above taking constexpr_call*.  */
1989 
1990 static inline bool
is_std_allocator_allocate(const constexpr_call * call)1991 is_std_allocator_allocate (const constexpr_call *call)
1992 {
1993   return (call
1994             && call->fundef
1995             && is_std_allocator_allocate (call->fundef->decl));
1996 }
1997 
1998 /* Return true if FNDECL is __dynamic_cast.  */
1999 
2000 static inline bool
cxx_dynamic_cast_fn_p(tree fndecl)2001 cxx_dynamic_cast_fn_p (tree fndecl)
2002 {
2003   return (cxx_dialect >= cxx20
2004             && id_equal (DECL_NAME (fndecl), "__dynamic_cast")
2005             && CP_DECL_CONTEXT (fndecl) == global_namespace);
2006 }
2007 
2008 /* Often, we have an expression in the form of address + offset, e.g.
2009    "&_ZTV1A + 16".  Extract the object from it, i.e. "_ZTV1A".  */
2010 
2011 static tree
extract_obj_from_addr_offset(tree expr)2012 extract_obj_from_addr_offset (tree expr)
2013 {
2014   if (TREE_CODE (expr) == POINTER_PLUS_EXPR)
2015     expr = TREE_OPERAND (expr, 0);
2016   STRIP_NOPS (expr);
2017   if (TREE_CODE (expr) == ADDR_EXPR)
2018     expr = TREE_OPERAND (expr, 0);
2019   return expr;
2020 }
2021 
2022 /* Given a PATH like
2023 
2024      g.D.2181.D.2154.D.2102.D.2093
2025 
2026    find a component with type TYPE.  Return NULL_TREE if not found, and
2027    error_mark_node if the component is not accessible.  If STOP is non-null,
2028    this function will return NULL_TREE if STOP is found before TYPE.  */
2029 
2030 static tree
get_component_with_type(tree path,tree type,tree stop)2031 get_component_with_type (tree path, tree type, tree stop)
2032 {
2033   while (true)
2034     {
2035       if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), type))
2036           /* Found it.  */
2037           return path;
2038       else if (stop
2039                  && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path),
2040                                                                             stop)))
2041           return NULL_TREE;
2042       else if (TREE_CODE (path) == COMPONENT_REF
2043                  && DECL_FIELD_IS_BASE (TREE_OPERAND (path, 1)))
2044           {
2045             /* We need to check that the component we're accessing is in fact
2046                accessible.  */
2047             if (TREE_PRIVATE (TREE_OPERAND (path, 1))
2048                 || TREE_PROTECTED (TREE_OPERAND (path, 1)))
2049               return error_mark_node;
2050             path = TREE_OPERAND (path, 0);
2051           }
2052       else
2053           return NULL_TREE;
2054     }
2055 }
2056 
2057 /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
2058 
2059    The declaration of __dynamic_cast is:
2060 
2061    void* __dynamic_cast (const void* __src_ptr,
2062                                const __class_type_info* __src_type,
2063                                const __class_type_info* __dst_type,
2064                                ptrdiff_t __src2dst);
2065 
2066    where src2dst has the following possible values
2067 
2068    >-1: src_type is a unique public non-virtual base of dst_type
2069           dst_ptr + src2dst == src_ptr
2070    -1: unspecified relationship
2071    -2: src_type is not a public base of dst_type
2072    -3: src_type is a multiple public non-virtual base of dst_type
2073 
2074   Since literal types can't have virtual bases, we only expect hint >=0,
2075   -2, or -3.  */
2076 
2077 static tree
cxx_eval_dynamic_cast_fn(const constexpr_ctx * ctx,tree call,bool * non_constant_p,bool * overflow_p)2078 cxx_eval_dynamic_cast_fn (const constexpr_ctx *ctx, tree call,
2079                                 bool *non_constant_p, bool *overflow_p)
2080 {
2081   /* T will be something like
2082       __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
2083      dismantle it.  */
2084   gcc_assert (call_expr_nargs (call) == 4);
2085   tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
2086   tree obj = CALL_EXPR_ARG (call, 0);
2087   tree type = CALL_EXPR_ARG (call, 2);
2088   HOST_WIDE_INT hint = int_cst_value (CALL_EXPR_ARG (call, 3));
2089   location_t loc = cp_expr_loc_or_input_loc (call);
2090 
2091   /* Get the target type of the dynamic_cast.  */
2092   gcc_assert (TREE_CODE (type) == ADDR_EXPR);
2093   type = TREE_OPERAND (type, 0);
2094   type = TREE_TYPE (DECL_NAME (type));
2095 
2096   /* TYPE can only be either T* or T&.  We can't know which of these it
2097      is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
2098      and something like "(T*)(T&)(T*) x" in the second case.  */
2099   bool reference_p = false;
2100   while (CONVERT_EXPR_P (obj) || TREE_CODE (obj) == SAVE_EXPR)
2101     {
2102       reference_p |= TYPE_REF_P (TREE_TYPE (obj));
2103       obj = TREE_OPERAND (obj, 0);
2104     }
2105 
2106   /* Evaluate the object so that we know its dynamic type.  */
2107   obj = cxx_eval_constant_expression (ctx, obj, /*lval*/false, non_constant_p,
2108                                               overflow_p);
2109   if (*non_constant_p)
2110     return call;
2111 
2112   /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
2113      but when HINT is > 0, it can also be something like
2114      &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET.  */
2115   obj = extract_obj_from_addr_offset (obj);
2116   const tree objtype = TREE_TYPE (obj);
2117   /* If OBJ doesn't refer to a base field, we're done.  */
2118   if (tree t = (TREE_CODE (obj) == COMPONENT_REF
2119                     ? TREE_OPERAND (obj, 1) : obj))
2120     if (TREE_CODE (t) != FIELD_DECL || !DECL_FIELD_IS_BASE (t))
2121       {
2122           if (reference_p)
2123             {
2124               if (!ctx->quiet)
2125                 {
2126                     error_at (loc, "reference %<dynamic_cast%> failed");
2127                     inform (loc, "dynamic type %qT of its operand does "
2128                               "not have a base class of type %qT",
2129                               objtype, type);
2130                 }
2131               *non_constant_p = true;
2132             }
2133           return integer_zero_node;
2134       }
2135 
2136   /* [class.cdtor] When a dynamic_cast is used in a constructor ...
2137      or in a destructor ... if the operand of the dynamic_cast refers
2138      to the object under construction or destruction, this object is
2139      considered to be a most derived object that has the type of the
2140      constructor or destructor's class.  */
2141   tree vtable = build_vfield_ref (obj, objtype);
2142   vtable = cxx_eval_constant_expression (ctx, vtable, /*lval*/false,
2143                                                    non_constant_p, overflow_p);
2144   if (*non_constant_p)
2145     return call;
2146   /* With -fsanitize=vptr, we initialize all vtable pointers to null,
2147      so it's possible that we got a null pointer now.  */
2148   if (integer_zerop (vtable))
2149     {
2150       if (!ctx->quiet)
2151           error_at (loc, "virtual table pointer is used uninitialized");
2152       *non_constant_p = true;
2153       return integer_zero_node;
2154     }
2155   /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A.  */
2156   vtable = extract_obj_from_addr_offset (vtable);
2157   const tree mdtype = DECL_CONTEXT (vtable);
2158 
2159   /* Given dynamic_cast<T>(v),
2160 
2161      [expr.dynamic.cast] If C is the class type to which T points or refers,
2162      the runtime check logically executes as follows:
2163 
2164      If, in the most derived object pointed (referred) to by v, v points
2165      (refers) to a public base class subobject of a C object, and if only
2166      one object of type C is derived from the subobject pointed (referred)
2167      to by v the result points (refers) to that C object.
2168 
2169      In this case, HINT >= 0 or -3.  */
2170   if (hint >= 0 || hint == -3)
2171     {
2172       /* Look for a component with type TYPE.  */
2173       tree t = get_component_with_type (obj, type, mdtype);
2174       /* If not accessible, give an error.  */
2175       if (t == error_mark_node)
2176           {
2177             if (reference_p)
2178               {
2179                 if (!ctx->quiet)
2180                     {
2181                       error_at (loc, "reference %<dynamic_cast%> failed");
2182                       inform (loc, "static type %qT of its operand is a "
2183                                 "non-public base class of dynamic type %qT",
2184                                 objtype, type);
2185 
2186                     }
2187                 *non_constant_p = true;
2188               }
2189             return integer_zero_node;
2190           }
2191       else if (t)
2192           /* The result points to the TYPE object.  */
2193           return cp_build_addr_expr (t, complain);
2194       /* Else, TYPE was not found, because the HINT turned out to be wrong.
2195            Fall through to the normal processing.  */
2196     }
2197 
2198   /* Otherwise, if v points (refers) to a public base class subobject of the
2199      most derived object, and the type of the most derived object has a base
2200      class, of type C, that is unambiguous and public, the result points
2201      (refers) to the C subobject of the most derived object.
2202 
2203      But it can also be an invalid case.  */
2204 
2205   /* Get the most derived object.  */
2206   obj = get_component_with_type (obj, mdtype, NULL_TREE);
2207   if (obj == error_mark_node)
2208     {
2209       if (reference_p)
2210           {
2211             if (!ctx->quiet)
2212               {
2213                 error_at (loc, "reference %<dynamic_cast%> failed");
2214                 inform (loc, "static type %qT of its operand is a non-public"
2215                           " base class of dynamic type %qT", objtype, mdtype);
2216               }
2217             *non_constant_p = true;
2218           }
2219       return integer_zero_node;
2220     }
2221   else
2222     gcc_assert (obj);
2223 
2224   /* Check that the type of the most derived object has a base class
2225      of type TYPE that is unambiguous and public.  */
2226   base_kind b_kind;
2227   tree binfo = lookup_base (mdtype, type, ba_check, &b_kind, tf_none);
2228   if (!binfo || binfo == error_mark_node)
2229     {
2230       if (reference_p)
2231           {
2232             if (!ctx->quiet)
2233               {
2234                 error_at (loc, "reference %<dynamic_cast%> failed");
2235                 if (b_kind == bk_ambig)
2236                     inform (loc, "%qT is an ambiguous base class of dynamic "
2237                               "type %qT of its operand", type, mdtype);
2238                 else
2239                     inform (loc, "dynamic type %qT of its operand does not "
2240                               "have an unambiguous public base class %qT",
2241                               mdtype, type);
2242               }
2243             *non_constant_p = true;
2244           }
2245       return integer_zero_node;
2246     }
2247   /* If so, return the TYPE subobject of the most derived object.  */
2248   obj = convert_to_base_statically (obj, binfo);
2249   return cp_build_addr_expr (obj, complain);
2250 }
2251 
2252 /* Data structure used by replace_decl and replace_decl_r.  */
2253 
2254 struct replace_decl_data
2255 {
2256   /* The _DECL we want to replace.  */
2257   tree decl;
2258   /* The replacement for DECL.  */
2259   tree replacement;
2260   /* Trees we've visited.  */
2261   hash_set<tree> *pset;
2262   /* Whether we've performed any replacements.  */
2263   bool changed;
2264 };
2265 
2266 /* Helper function for replace_decl, called through cp_walk_tree.  */
2267 
2268 static tree
replace_decl_r(tree * tp,int * walk_subtrees,void * data)2269 replace_decl_r (tree *tp, int *walk_subtrees, void *data)
2270 {
2271   replace_decl_data *d = (replace_decl_data *) data;
2272 
2273   if (*tp == d->decl)
2274     {
2275       *tp = unshare_expr (d->replacement);
2276       d->changed = true;
2277       *walk_subtrees = 0;
2278     }
2279   else if (TYPE_P (*tp)
2280              || d->pset->add (*tp))
2281     *walk_subtrees = 0;
2282 
2283   return NULL_TREE;
2284 }
2285 
2286 /* Replace every occurrence of DECL with (an unshared copy of)
2287    REPLACEMENT within the expression *TP.  Returns true iff a
2288    replacement was performed.  */
2289 
2290 bool
replace_decl(tree * tp,tree decl,tree replacement)2291 replace_decl (tree *tp, tree decl, tree replacement)
2292 {
2293   gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
2294                            (TREE_TYPE (decl), TREE_TYPE (replacement)));
2295   hash_set<tree> pset;
2296   replace_decl_data data = { decl, replacement, &pset, false };
2297   cp_walk_tree (tp, replace_decl_r, &data, NULL);
2298   return data.changed;
2299 }
2300 
2301 /* Evaluate the call T to virtual function thunk THUNK_FNDECL.  */
2302 
2303 static tree
cxx_eval_thunk_call(const constexpr_ctx * ctx,tree t,tree thunk_fndecl,bool lval,bool * non_constant_p,bool * overflow_p)2304 cxx_eval_thunk_call (const constexpr_ctx *ctx, tree t, tree thunk_fndecl,
2305                          bool lval,
2306                          bool *non_constant_p, bool *overflow_p)
2307 {
2308   tree function = THUNK_TARGET (thunk_fndecl);
2309 
2310   if (THUNK_VIRTUAL_OFFSET (thunk_fndecl))
2311     {
2312       if (!ctx->quiet)
2313           {
2314             if (!DECL_DECLARED_CONSTEXPR_P (function))
2315               {
2316                 error ("call to non-%<constexpr%> function %qD", function);
2317                 explain_invalid_constexpr_fn (function);
2318               }
2319             else
2320               /* virtual_offset is only set for virtual bases, which make the
2321                  class non-literal, so we don't need to handle it here.  */
2322               error ("calling constexpr member function %qD through virtual "
2323                        "base subobject", function);
2324           }
2325       *non_constant_p = true;
2326       return t;
2327     }
2328 
2329   tree new_call = copy_node (t);
2330   CALL_EXPR_FN (new_call) = function;
2331   TREE_TYPE (new_call) = TREE_TYPE (TREE_TYPE (function));
2332 
2333   tree offset = size_int (THUNK_FIXED_OFFSET (thunk_fndecl));
2334 
2335   if (DECL_THIS_THUNK_P (thunk_fndecl))
2336     {
2337       /* 'this'-adjusting thunk.  */
2338       tree this_arg = CALL_EXPR_ARG (t, 0);
2339       this_arg = build2 (POINTER_PLUS_EXPR, TREE_TYPE (this_arg),
2340                                this_arg, offset);
2341       CALL_EXPR_ARG (new_call, 0) = this_arg;
2342     }
2343   else
2344     /* Return-adjusting thunk.  */
2345     new_call = build2 (POINTER_PLUS_EXPR, TREE_TYPE (new_call),
2346                            new_call, offset);
2347 
2348   return cxx_eval_constant_expression (ctx, new_call, lval,
2349                                                non_constant_p, overflow_p);
2350 }
2351 
2352 /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set
2353    its TREE_READONLY flag according to READONLY_P.  Used for constexpr
2354    'tors to detect modifying const objects in a constexpr context.  */
2355 
2356 static void
cxx_set_object_constness(const constexpr_ctx * ctx,tree object,bool readonly_p,bool * non_constant_p,bool * overflow_p)2357 cxx_set_object_constness (const constexpr_ctx *ctx, tree object,
2358                                 bool readonly_p, bool *non_constant_p,
2359                                 bool *overflow_p)
2360 {
2361   if (CLASS_TYPE_P (TREE_TYPE (object))
2362       && CP_TYPE_CONST_P (TREE_TYPE (object)))
2363     {
2364       /* Subobjects might not be stored in ctx->global->values but we
2365            can get its CONSTRUCTOR by evaluating *this.  */
2366       tree e = cxx_eval_constant_expression (ctx, object, /*lval*/false,
2367                                                        non_constant_p, overflow_p);
2368       if (TREE_CODE (e) == CONSTRUCTOR && !*non_constant_p)
2369           TREE_READONLY (e) = readonly_p;
2370     }
2371 }
2372 
2373 /* Subroutine of cxx_eval_constant_expression.
2374    Evaluate the call expression tree T in the context of OLD_CALL expression
2375    evaluation.  */
2376 
2377 static tree
cxx_eval_call_expression(const constexpr_ctx * ctx,tree t,bool lval,bool * non_constant_p,bool * overflow_p)2378 cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
2379                                 bool lval,
2380                                 bool *non_constant_p, bool *overflow_p)
2381 {
2382   /* Handle concept checks separately.  */
2383   if (concept_check_p (t))
2384     return evaluate_concept_check (t);
2385 
2386   location_t loc = cp_expr_loc_or_input_loc (t);
2387   tree fun = get_function_named_in_call (t);
2388   constexpr_call new_call
2389     = { NULL, NULL, NULL, 0, ctx->manifestly_const_eval };
2390   int depth_ok;
2391 
2392   if (fun == NULL_TREE)
2393     return cxx_eval_internal_function (ctx, t, lval,
2394                                                non_constant_p, overflow_p);
2395 
2396   if (TREE_CODE (fun) != FUNCTION_DECL)
2397     {
2398       /* Might be a constexpr function pointer.  */
2399       fun = cxx_eval_constant_expression (ctx, fun,
2400                                                     /*lval*/false, non_constant_p,
2401                                                     overflow_p);
2402       STRIP_NOPS (fun);
2403       if (TREE_CODE (fun) == ADDR_EXPR)
2404           fun = TREE_OPERAND (fun, 0);
2405       /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
2406            indirection, the called expression is a pointer into the
2407            virtual table which should contain FDESC_EXPR.  Extract the
2408            FUNCTION_DECL from there.  */
2409       else if (TARGET_VTABLE_USES_DESCRIPTORS
2410                  && TREE_CODE (fun) == POINTER_PLUS_EXPR
2411                  && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR
2412                  && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST)
2413           {
2414             tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0);
2415             if (VAR_P (d)
2416                 && DECL_VTABLE_OR_VTT_P (d)
2417                 && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE
2418                 && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type
2419                 && DECL_INITIAL (d)
2420                 && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR)
2421               {
2422                 tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1),
2423                                                   TYPE_SIZE_UNIT (vtable_entry_type));
2424                 HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i);
2425                 if (idx >= 0)
2426                     {
2427                       tree fdesc
2428                         = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value;
2429                       if (TREE_CODE (fdesc) == FDESC_EXPR
2430                           && integer_zerop (TREE_OPERAND (fdesc, 1)))
2431                         fun = TREE_OPERAND (fdesc, 0);
2432                     }
2433               }
2434           }
2435     }
2436   if (TREE_CODE (fun) != FUNCTION_DECL)
2437     {
2438       if (!ctx->quiet && !*non_constant_p)
2439           error_at (loc, "expression %qE does not designate a %<constexpr%> "
2440                       "function", fun);
2441       *non_constant_p = true;
2442       return t;
2443     }
2444   if (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun))
2445     fun = DECL_CLONED_FUNCTION (fun);
2446 
2447   if (is_ubsan_builtin_p (fun))
2448     return void_node;
2449 
2450   if (fndecl_built_in_p (fun))
2451     return cxx_eval_builtin_function_call (ctx, t, fun,
2452                                                      lval, non_constant_p, overflow_p);
2453   if (DECL_THUNK_P (fun))
2454     return cxx_eval_thunk_call (ctx, t, fun, lval, non_constant_p, overflow_p);
2455   if (!maybe_constexpr_fn (fun))
2456     {
2457       if (TREE_CODE (t) == CALL_EXPR
2458             && cxx_replaceable_global_alloc_fn (fun)
2459             && (CALL_FROM_NEW_OR_DELETE_P (t)
2460                 || is_std_allocator_allocate (ctx->call)))
2461           {
2462             const int nargs = call_expr_nargs (t);
2463             tree arg0 = NULL_TREE;
2464             for (int i = 0; i < nargs; ++i)
2465               {
2466                 tree arg = CALL_EXPR_ARG (t, i);
2467                 arg = cxx_eval_constant_expression (ctx, arg, false,
2468                                                               non_constant_p, overflow_p);
2469                 VERIFY_CONSTANT (arg);
2470                 if (i == 0)
2471                     arg0 = arg;
2472               }
2473             gcc_assert (arg0);
2474             if (IDENTIFIER_NEW_OP_P (DECL_NAME (fun)))
2475               {
2476                 tree type = build_array_type_nelts (char_type_node,
2477                                                               tree_to_uhwi (arg0));
2478                 tree var = build_decl (loc, VAR_DECL,
2479                                              (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
2480                                               & OVL_OP_FLAG_VEC)
2481                                              ? heap_vec_uninit_identifier
2482                                              : heap_uninit_identifier,
2483                                              type);
2484                 DECL_ARTIFICIAL (var) = 1;
2485                 TREE_STATIC (var) = 1;
2486                 // Temporarily register the artificial var in varpool,
2487                 // so that comparisons of its address against NULL are folded
2488                 // through nonzero_address even with
2489                 // -fno-delete-null-pointer-checks or that comparison of
2490                 // addresses of different heap artificial vars is folded too.
2491                 // See PR98988 and PR99031.
2492                 varpool_node::finalize_decl (var);
2493                 ctx->global->heap_vars.safe_push (var);
2494                 ctx->global->values.put (var, NULL_TREE);
2495                 return fold_convert (ptr_type_node, build_address (var));
2496               }
2497             else
2498               {
2499                 STRIP_NOPS (arg0);
2500                 if (TREE_CODE (arg0) == ADDR_EXPR
2501                       && VAR_P (TREE_OPERAND (arg0, 0)))
2502                     {
2503                       tree var = TREE_OPERAND (arg0, 0);
2504                       if (DECL_NAME (var) == heap_uninit_identifier
2505                           || DECL_NAME (var) == heap_identifier)
2506                         {
2507                           if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
2508                                 & OVL_OP_FLAG_VEC)
2509                               {
2510                                 if (!ctx->quiet)
2511                                   {
2512                                     error_at (loc, "array deallocation of object "
2513                                                        "allocated with non-array "
2514                                                        "allocation");
2515                                     inform (DECL_SOURCE_LOCATION (var),
2516                                               "allocation performed here");
2517                                   }
2518                                 *non_constant_p = true;
2519                                 return t;
2520                               }
2521                           DECL_NAME (var) = heap_deleted_identifier;
2522                           ctx->global->values.remove (var);
2523                           ctx->global->heap_dealloc_count++;
2524                           return void_node;
2525                         }
2526                       else if (DECL_NAME (var) == heap_vec_uninit_identifier
2527                                  || DECL_NAME (var) == heap_vec_identifier)
2528                         {
2529                           if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
2530                                  & OVL_OP_FLAG_VEC) == 0)
2531                               {
2532                                 if (!ctx->quiet)
2533                                   {
2534                                     error_at (loc, "non-array deallocation of "
2535                                                        "object allocated with array "
2536                                                        "allocation");
2537                                     inform (DECL_SOURCE_LOCATION (var),
2538                                               "allocation performed here");
2539                                   }
2540                                 *non_constant_p = true;
2541                                 return t;
2542                               }
2543                           DECL_NAME (var) = heap_deleted_identifier;
2544                           ctx->global->values.remove (var);
2545                           ctx->global->heap_dealloc_count++;
2546                           return void_node;
2547                         }
2548                       else if (DECL_NAME (var) == heap_deleted_identifier)
2549                         {
2550                           if (!ctx->quiet)
2551                               error_at (loc, "deallocation of already deallocated "
2552                                                "storage");
2553                           *non_constant_p = true;
2554                           return t;
2555                         }
2556                     }
2557                 if (!ctx->quiet)
2558                     error_at (loc, "deallocation of storage that was "
2559                                      "not previously allocated");
2560                 *non_constant_p = true;
2561                 return t;
2562               }
2563           }
2564       /* Allow placement new in std::construct_at, just return the second
2565            argument.  */
2566       if (TREE_CODE (t) == CALL_EXPR
2567             && cxx_placement_new_fn (fun)
2568             && is_std_construct_at (ctx->call))
2569           {
2570             const int nargs = call_expr_nargs (t);
2571             tree arg1 = NULL_TREE;
2572             for (int i = 0; i < nargs; ++i)
2573               {
2574                 tree arg = CALL_EXPR_ARG (t, i);
2575                 arg = cxx_eval_constant_expression (ctx, arg, false,
2576                                                               non_constant_p, overflow_p);
2577                 if (i == 1)
2578                     arg1 = arg;
2579                 else
2580                     VERIFY_CONSTANT (arg);
2581               }
2582             gcc_assert (arg1);
2583             return arg1;
2584           }
2585       else if (cxx_dynamic_cast_fn_p (fun))
2586           return cxx_eval_dynamic_cast_fn (ctx, t, non_constant_p, overflow_p);
2587 
2588       if (!ctx->quiet)
2589           {
2590             if (!lambda_static_thunk_p (fun))
2591               error_at (loc, "call to non-%<constexpr%> function %qD", fun);
2592             explain_invalid_constexpr_fn (fun);
2593           }
2594       *non_constant_p = true;
2595       return t;
2596     }
2597 
2598   constexpr_ctx new_ctx = *ctx;
2599   if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
2600       && TREE_CODE (t) == AGGR_INIT_EXPR)
2601     {
2602       /* We want to have an initialization target for an AGGR_INIT_EXPR.
2603            If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT.  */
2604       new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
2605       tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
2606       CONSTRUCTOR_NO_CLEARING (ctor) = true;
2607       ctx->global->values.put (new_ctx.object, ctor);
2608       ctx = &new_ctx;
2609     }
2610 
2611   /* Shortcut trivial constructor/op=.  */
2612   if (trivial_fn_p (fun))
2613     {
2614       tree init = NULL_TREE;
2615       if (call_expr_nargs (t) == 2)
2616           init = convert_from_reference (get_nth_callarg (t, 1));
2617       else if (TREE_CODE (t) == AGGR_INIT_EXPR
2618                  && AGGR_INIT_ZERO_FIRST (t))
2619           init = build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false);
2620       if (init)
2621           {
2622             tree op = get_nth_callarg (t, 0);
2623             if (is_dummy_object (op))
2624               op = ctx->object;
2625             else
2626               op = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (op)), op);
2627             tree set = build2 (MODIFY_EXPR, TREE_TYPE (op), op, init);
2628             new_ctx.call = &new_call;
2629             return cxx_eval_constant_expression (&new_ctx, set, lval,
2630                                                          non_constant_p, overflow_p);
2631           }
2632     }
2633 
2634   bool non_constant_args = false;
2635   new_call.bindings
2636     = cxx_bind_parameters_in_call (ctx, t, fun, non_constant_p,
2637                                            overflow_p, &non_constant_args);
2638 
2639   /* We build up the bindings list before we know whether we already have this
2640      call cached.  If we don't end up saving these bindings, ggc_free them when
2641      this function exits.  */
2642   class free_bindings
2643   {
2644     tree *bindings;
2645   public:
2646     free_bindings (tree &b): bindings (&b) { }
2647     ~free_bindings () { if (bindings) ggc_free (*bindings); }
2648     void preserve () { bindings = NULL; }
2649   } fb (new_call.bindings);
2650 
2651   if (*non_constant_p)
2652     return t;
2653 
2654   /* We can't defer instantiating the function any longer.  */
2655   if (!DECL_INITIAL (fun)
2656       && DECL_TEMPLOID_INSTANTIATION (fun)
2657       && !uid_sensitive_constexpr_evaluation_p ())
2658     {
2659       location_t save_loc = input_location;
2660       input_location = loc;
2661       ++function_depth;
2662       if (ctx->manifestly_const_eval)
2663           FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true;
2664       instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
2665       --function_depth;
2666       input_location = save_loc;
2667     }
2668 
2669   /* If in direct recursive call, optimize definition search.  */
2670   if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun)
2671     new_call.fundef = ctx->call->fundef;
2672   else
2673     {
2674       new_call.fundef = retrieve_constexpr_fundef (fun);
2675       if (new_call.fundef == NULL || new_call.fundef->body == NULL
2676             || new_call.fundef->result == error_mark_node
2677             || fun == current_function_decl)
2678         {
2679             if (!ctx->quiet)
2680               {
2681                 /* We need to check for current_function_decl here in case we're
2682                      being called during cp_fold_function, because at that point
2683                      DECL_INITIAL is set properly and we have a fundef but we
2684                      haven't lowered invisirefs yet (c++/70344).  */
2685                 if (DECL_INITIAL (fun) == error_mark_node
2686                       || fun == current_function_decl)
2687                     error_at (loc, "%qD called in a constant expression before its "
2688                                 "definition is complete", fun);
2689                 else if (DECL_INITIAL (fun))
2690                     {
2691                       /* The definition of fun was somehow unsuitable.  But pretend
2692                          that lambda static thunks don't exist.  */
2693                       if (!lambda_static_thunk_p (fun))
2694                         error_at (loc, "%qD called in a constant expression", fun);
2695                       explain_invalid_constexpr_fn (fun);
2696                     }
2697                 else
2698                     error_at (loc, "%qD used before its definition", fun);
2699               }
2700             *non_constant_p = true;
2701           return t;
2702         }
2703     }
2704 
2705   depth_ok = push_cx_call_context (t);
2706 
2707   /* Remember the object we are constructing or destructing.  */
2708   tree new_obj = NULL_TREE;
2709   if (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun))
2710     {
2711       /* In a cdtor, it should be the first `this' argument.
2712            At this point it has already been evaluated in the call
2713            to cxx_bind_parameters_in_call.  */
2714       new_obj = TREE_VEC_ELT (new_call.bindings, 0);
2715       STRIP_NOPS (new_obj);
2716       if (TREE_CODE (new_obj) == ADDR_EXPR)
2717           new_obj = TREE_OPERAND (new_obj, 0);
2718 
2719       if (ctx->call && ctx->call->fundef
2720             && DECL_CONSTRUCTOR_P (ctx->call->fundef->decl))
2721           {
2722             tree cur_obj = TREE_VEC_ELT (ctx->call->bindings, 0);
2723             STRIP_NOPS (cur_obj);
2724             if (TREE_CODE (cur_obj) == ADDR_EXPR)
2725               cur_obj = TREE_OPERAND (cur_obj, 0);
2726             if (new_obj == cur_obj)
2727               /* We're calling the target constructor of a delegating
2728                  constructor, or accessing a base subobject through a
2729                  NOP_EXPR as part of a call to a base constructor, so
2730                  there is no new (sub)object.  */
2731               new_obj = NULL_TREE;
2732           }
2733     }
2734 
2735   tree result = NULL_TREE;
2736 
2737   constexpr_call *entry = NULL;
2738   if (depth_ok && !non_constant_args && ctx->strict)
2739     {
2740       new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef);
2741       new_call.hash
2742           = iterative_hash_template_arg (new_call.bindings, new_call.hash);
2743       new_call.hash
2744           = iterative_hash_object (ctx->manifestly_const_eval, new_call.hash);
2745 
2746       /* If we have seen this call before, we are done.  */
2747       maybe_initialize_constexpr_call_table ();
2748       constexpr_call **slot
2749           = constexpr_call_table->find_slot (&new_call, INSERT);
2750       entry = *slot;
2751       if (entry == NULL)
2752           {
2753             /* Only cache up to constexpr_cache_depth to limit memory use.  */
2754             if (depth_ok < constexpr_cache_depth)
2755               {
2756                 /* We need to keep a pointer to the entry, not just the slot, as
2757                      the slot can move during evaluation of the body.  */
2758                 *slot = entry = ggc_alloc<constexpr_call> ();
2759                 *entry = new_call;
2760                 fb.preserve ();
2761               }
2762           }
2763       /* Calls that are in progress have their result set to NULL, so that we
2764            can detect circular dependencies.  Now that we only cache up to
2765            constexpr_cache_depth this won't catch circular dependencies that
2766            start deeper, but they'll hit the recursion or ops limit.  */
2767       else if (entry->result == NULL)
2768           {
2769             if (!ctx->quiet)
2770               error ("call has circular dependency");
2771             *non_constant_p = true;
2772             entry->result = result = error_mark_node;
2773           }
2774       else
2775           result = entry->result;
2776     }
2777 
2778   if (!depth_ok)
2779     {
2780       if (!ctx->quiet)
2781           error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
2782                  "%<-fconstexpr-depth=%> to increase the maximum)",
2783                  max_constexpr_depth);
2784       *non_constant_p = true;
2785       result = error_mark_node;
2786     }
2787   else
2788     {
2789       bool cacheable = true;
2790       if (result && result != error_mark_node)
2791           /* OK */;
2792       else if (!DECL_SAVED_TREE (fun))
2793           {
2794             /* When at_eof >= 2, cgraph has started throwing away
2795                DECL_SAVED_TREE, so fail quietly.  FIXME we get here because of
2796                late code generation for VEC_INIT_EXPR, which needs to be
2797                completely reconsidered.  */
2798             gcc_assert (at_eof >= 2 && ctx->quiet);
2799             *non_constant_p = true;
2800           }
2801       else if (tree copy = get_fundef_copy (new_call.fundef))
2802           {
2803             tree body, parms, res;
2804             releasing_vec ctors;
2805 
2806             /* Reuse or create a new unshared copy of this function's body.  */
2807             body = TREE_PURPOSE (copy);
2808             parms = TREE_VALUE (copy);
2809             res = TREE_TYPE (copy);
2810 
2811             /* Associate the bindings with the remapped parms.  */
2812             tree bound = new_call.bindings;
2813             tree remapped = parms;
2814             for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
2815               {
2816                 tree arg = TREE_VEC_ELT (bound, i);
2817                 if (entry)
2818                     {
2819                       /* Unshare args going into the hash table to separate them
2820                          from the caller's context, for better GC and to avoid
2821                          problems with verify_gimple.  */
2822                       arg = unshare_expr_without_location (arg);
2823                       TREE_VEC_ELT (bound, i) = arg;
2824 
2825                       /* And then unshare again so the callee doesn't change the
2826                          argument values in the hash table. XXX Could we unshare
2827                          lazily in cxx_eval_store_expression?  */
2828                       arg = unshare_constructor (arg);
2829                       if (TREE_CODE (arg) == CONSTRUCTOR)
2830                         vec_safe_push (ctors, arg);
2831                     }
2832                 ctx->global->values.put (remapped, arg);
2833                 remapped = DECL_CHAIN (remapped);
2834               }
2835             /* Add the RESULT_DECL to the values map, too.  */
2836             gcc_assert (!DECL_BY_REFERENCE (res));
2837             ctx->global->values.put (res, NULL_TREE);
2838 
2839             /* Track the callee's evaluated SAVE_EXPRs and TARGET_EXPRs so that
2840                we can forget their values after the call.  */
2841             constexpr_ctx ctx_with_save_exprs = *ctx;
2842             auto_vec<tree, 10> save_exprs;
2843             ctx_with_save_exprs.save_exprs = &save_exprs;
2844             ctx_with_save_exprs.call = &new_call;
2845             unsigned save_heap_alloc_count = ctx->global->heap_vars.length ();
2846             unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count;
2847 
2848             /* If this is a constexpr destructor, the object's const and volatile
2849                semantics are no longer in effect; see [class.dtor]p5.  */
2850             if (new_obj && DECL_DESTRUCTOR_P (fun))
2851               cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/false,
2852                                               non_constant_p, overflow_p);
2853 
2854             tree jump_target = NULL_TREE;
2855             cxx_eval_constant_expression (&ctx_with_save_exprs, body,
2856                                                   lval, non_constant_p, overflow_p,
2857                                                   &jump_target);
2858 
2859             if (DECL_CONSTRUCTOR_P (fun))
2860               {
2861                 /* This can be null for a subobject constructor call, in
2862                      which case what we care about is the initialization
2863                      side-effects rather than the value.  We could get at the
2864                      value by evaluating *this, but we don't bother; there's
2865                      no need to put such a call in the hash table.  */
2866                 result = lval ? ctx->object : ctx->ctor;
2867 
2868                 /* If we've just evaluated a subobject constructor call for an
2869                      empty union member, it might not have produced a side effect
2870                      that actually activated the union member.  So produce such a
2871                      side effect now to ensure the union appears initialized.  */
2872                 if (!result && new_obj
2873                       && TREE_CODE (new_obj) == COMPONENT_REF
2874                       && TREE_CODE (TREE_TYPE
2875                                         (TREE_OPERAND (new_obj, 0))) == UNION_TYPE
2876                       && is_really_empty_class (TREE_TYPE (new_obj),
2877                                                       /*ignore_vptr*/false))
2878                     {
2879                       tree activate = build2 (MODIFY_EXPR, TREE_TYPE (new_obj),
2880                                                     new_obj,
2881                                                     build_constructor (TREE_TYPE (new_obj),
2882                                                                            NULL));
2883                       cxx_eval_constant_expression (ctx, activate, lval,
2884                                                             non_constant_p, overflow_p);
2885                       ggc_free (activate);
2886                     }
2887               }
2888             else if (VOID_TYPE_P (TREE_TYPE (res)))
2889               result = void_node;
2890             else
2891               {
2892                 result = *ctx->global->values.get (res);
2893                 if (result == NULL_TREE && !*non_constant_p
2894                       && !DECL_DESTRUCTOR_P (fun))
2895                     {
2896                       if (!ctx->quiet)
2897                         error ("%<constexpr%> call flows off the end "
2898                                  "of the function");
2899                       *non_constant_p = true;
2900                     }
2901               }
2902 
2903             /* At this point, the object's constructor will have run, so
2904                the object is no longer under construction, and its possible
2905                'const' semantics now apply.  Make a note of this fact by
2906                marking the CONSTRUCTOR TREE_READONLY.  */
2907             if (new_obj && DECL_CONSTRUCTOR_P (fun))
2908               cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/true,
2909                                               non_constant_p, overflow_p);
2910 
2911             /* Forget the saved values of the callee's SAVE_EXPRs and
2912                TARGET_EXPRs.  */
2913             for (tree save_expr : save_exprs)
2914               ctx->global->values.remove (save_expr);
2915 
2916             /* Remove the parms/result from the values map.  Is it worth
2917                bothering to do this when the map itself is only live for
2918                one constexpr evaluation?  If so, maybe also clear out
2919                other vars from call, maybe in BIND_EXPR handling?  */
2920             ctx->global->values.remove (res);
2921             for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
2922               ctx->global->values.remove (parm);
2923 
2924             /* Free any parameter CONSTRUCTORs we aren't returning directly.  */
2925             while (!ctors->is_empty ())
2926               {
2927                 tree c = ctors->pop ();
2928                 if (c != result)
2929                     free_constructor (c);
2930               }
2931 
2932             /* Make the unshared function copy we used available for re-use.  */
2933             save_fundef_copy (fun, copy);
2934 
2935             /* If the call allocated some heap object that hasn't been
2936                deallocated during the call, or if it deallocated some heap
2937                object it has not allocated, the call isn't really stateless
2938                for the constexpr evaluation and should not be cached.
2939                It is fine if the call allocates something and deallocates it
2940                too.  */
2941             if (entry
2942                 && (save_heap_alloc_count != ctx->global->heap_vars.length ()
2943                       || (save_heap_dealloc_count
2944                           != ctx->global->heap_dealloc_count)))
2945               {
2946                 tree heap_var;
2947                 unsigned int i;
2948                 if ((ctx->global->heap_vars.length ()
2949                        - ctx->global->heap_dealloc_count)
2950                       != save_heap_alloc_count - save_heap_dealloc_count)
2951                     cacheable = false;
2952                 else
2953                     FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var,
2954                                                save_heap_alloc_count)
2955                       if (DECL_NAME (heap_var) != heap_deleted_identifier)
2956                         {
2957                           cacheable = false;
2958                           break;
2959                         }
2960               }
2961 
2962               /* Rewrite all occurrences of the function's RESULT_DECL with the
2963                  current object under construction.  */
2964               if (!*non_constant_p && ctx->object
2965                     && CLASS_TYPE_P (TREE_TYPE (res))
2966                     && !is_empty_class (TREE_TYPE (res)))
2967                 if (replace_decl (&result, res, ctx->object))
2968                     cacheable = false;
2969           }
2970       else
2971           /* Couldn't get a function copy to evaluate.  */
2972           *non_constant_p = true;
2973 
2974       if (result == error_mark_node)
2975           *non_constant_p = true;
2976       if (*non_constant_p || *overflow_p)
2977           result = error_mark_node;
2978       else if (!result)
2979           result = void_node;
2980       if (entry)
2981           entry->result = cacheable ? result : error_mark_node;
2982     }
2983 
2984   /* The result of a constexpr function must be completely initialized.
2985 
2986      However, in C++20, a constexpr constructor doesn't necessarily have
2987      to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
2988      in order to detect reading an unitialized object in constexpr instead
2989      of value-initializing it.  (reduced_constant_expression_p is expected to
2990      take care of clearing the flag.)  */
2991   if (TREE_CODE (result) == CONSTRUCTOR
2992       && (cxx_dialect < cxx20
2993             || !DECL_CONSTRUCTOR_P (fun)))
2994     clear_no_implicit_zero (result);
2995 
2996   pop_cx_call_context ();
2997   return result;
2998 }
2999 
3000 /* Return true if T is a valid constant initializer.  If a CONSTRUCTOR
3001    initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
3002    cleared.
3003    FIXME speed this up, it's taking 16% of compile time on sieve testcase.  */
3004 
3005 bool
reduced_constant_expression_p(tree t)3006 reduced_constant_expression_p (tree t)
3007 {
3008   if (t == NULL_TREE)
3009     return false;
3010 
3011   switch (TREE_CODE (t))
3012     {
3013     case PTRMEM_CST:
3014       /* Even if we can't lower this yet, it's constant.  */
3015       return true;
3016 
3017     case CONSTRUCTOR:
3018       /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR.  */
3019       tree field;
3020       if (CONSTRUCTOR_NO_CLEARING (t))
3021           {
3022             if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
3023               /* An initialized vector would have a VECTOR_CST.  */
3024               return false;
3025             else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
3026               {
3027                 /* There must be a valid constant initializer at every array
3028                      index.  */
3029                 tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
3030                 tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
3031                 tree cursor = min;
3032                 for (auto &e: CONSTRUCTOR_ELTS (t))
3033                     {
3034                       if (!reduced_constant_expression_p (e.value))
3035                         return false;
3036                       if (array_index_cmp (cursor, e.index) != 0)
3037                         return false;
3038                       if (TREE_CODE (e.index) == RANGE_EXPR)
3039                         cursor = TREE_OPERAND (e.index, 1);
3040                       cursor = int_const_binop (PLUS_EXPR, cursor, size_one_node);
3041                     }
3042                 if (find_array_ctor_elt (t, max) == -1)
3043                     return false;
3044                 goto ok;
3045               }
3046             else if (cxx_dialect >= cxx20
3047                        && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE)
3048               {
3049                 if (CONSTRUCTOR_NELTS (t) == 0)
3050                     /* An initialized union has a constructor element.  */
3051                     return false;
3052                 /* And it only initializes one member.  */
3053                 field = NULL_TREE;
3054               }
3055             else
3056               field = next_subobject_field (TYPE_FIELDS (TREE_TYPE (t)));
3057           }
3058       else
3059           field = NULL_TREE;
3060       for (auto &e: CONSTRUCTOR_ELTS (t))
3061           {
3062             /* If VAL is null, we're in the middle of initializing this
3063                element.  */
3064             if (!reduced_constant_expression_p (e.value))
3065               return false;
3066             /* We want to remove initializers for empty fields in a struct to
3067                avoid confusing output_constructor.  */
3068             if (is_empty_field (e.index)
3069                 && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
3070               return false;
3071             /* Check for non-empty fields between initialized fields when
3072                CONSTRUCTOR_NO_CLEARING.  */
3073             for (; field && e.index != field;
3074                  field = next_subobject_field (DECL_CHAIN (field)))
3075               if (!is_really_empty_class (TREE_TYPE (field),
3076                                                   /*ignore_vptr*/false))
3077                 return false;
3078             if (field)
3079               field = next_subobject_field (DECL_CHAIN (field));
3080           }
3081       /* There could be a non-empty field at the end.  */
3082       for (; field; field = next_subobject_field (DECL_CHAIN (field)))
3083           if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false))
3084             return false;
3085 ok:
3086       if (CONSTRUCTOR_NO_CLEARING (t))
3087           /* All the fields are initialized.  */
3088           CONSTRUCTOR_NO_CLEARING (t) = false;
3089       return true;
3090 
3091     default:
3092       /* FIXME are we calling this too much?  */
3093       return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
3094     }
3095 }
3096 
3097 /* Some expressions may have constant operands but are not constant
3098    themselves, such as 1/0.  Call this function to check for that
3099    condition.
3100 
3101    We only call this in places that require an arithmetic constant, not in
3102    places where we might have a non-constant expression that can be a
3103    component of a constant expression, such as the address of a constexpr
3104    variable that might be dereferenced later.  */
3105 
3106 static bool
verify_constant(tree t,bool allow_non_constant,bool * non_constant_p,bool * overflow_p)3107 verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
3108                      bool *overflow_p)
3109 {
3110   if (!*non_constant_p && !reduced_constant_expression_p (t)
3111       && t != void_node)
3112     {
3113       if (!allow_non_constant)
3114           error ("%q+E is not a constant expression", t);
3115       *non_constant_p = true;
3116     }
3117   if (TREE_OVERFLOW_P (t))
3118     {
3119       if (!allow_non_constant)
3120           {
3121             permerror (input_location, "overflow in constant expression");
3122             /* If we're being permissive (and are in an enforcing
3123                context), ignore the overflow.  */
3124             if (flag_permissive)
3125               return *non_constant_p;
3126           }
3127       *overflow_p = true;
3128     }
3129   return *non_constant_p;
3130 }
3131 
3132 /* Check whether the shift operation with code CODE and type TYPE on LHS
3133    and RHS is undefined.  If it is, give an error with an explanation,
3134    and return true; return false otherwise.  */
3135 
3136 static bool
cxx_eval_check_shift_p(location_t loc,const constexpr_ctx * ctx,enum tree_code code,tree type,tree lhs,tree rhs)3137 cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
3138                               enum tree_code code, tree type, tree lhs, tree rhs)
3139 {
3140   if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
3141       || TREE_CODE (lhs) != INTEGER_CST
3142       || TREE_CODE (rhs) != INTEGER_CST)
3143     return false;
3144 
3145   tree lhstype = TREE_TYPE (lhs);
3146   unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
3147 
3148   /* [expr.shift] The behavior is undefined if the right operand
3149      is negative, or greater than or equal to the length in bits
3150      of the promoted left operand.  */
3151   if (tree_int_cst_sgn (rhs) == -1)
3152     {
3153       if (!ctx->quiet)
3154           permerror (loc, "right operand of shift expression %q+E is negative",
3155                        build2_loc (loc, code, type, lhs, rhs));
3156       return (!flag_permissive || ctx->quiet);
3157     }
3158   if (compare_tree_int (rhs, uprec) >= 0)
3159     {
3160       if (!ctx->quiet)
3161           permerror (loc, "right operand of shift expression %q+E is greater "
3162                        "than or equal to the precision %wu of the left operand",
3163                        build2_loc (loc, code, type, lhs, rhs), uprec);
3164       return (!flag_permissive || ctx->quiet);
3165     }
3166 
3167   /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
3168      if E1 has a signed type and non-negative value, and E1x2^E2 is
3169      representable in the corresponding unsigned type of the result type,
3170      then that value, converted to the result type, is the resulting value;
3171      otherwise, the behavior is undefined.
3172      For C++20:
3173      The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
3174      2^N, where N is the range exponent of the type of the result.  */
3175   if (code == LSHIFT_EXPR
3176       && !TYPE_OVERFLOW_WRAPS (lhstype)
3177       && cxx_dialect >= cxx11
3178       && cxx_dialect < cxx20)
3179     {
3180       if (tree_int_cst_sgn (lhs) == -1)
3181           {
3182             if (!ctx->quiet)
3183               permerror (loc,
3184                            "left operand of shift expression %q+E is negative",
3185                            build2_loc (loc, code, type, lhs, rhs));
3186             return (!flag_permissive || ctx->quiet);
3187           }
3188       /* For signed x << y the following:
3189            (unsigned) x >> ((prec (lhs) - 1) - y)
3190            if > 1, is undefined.  The right-hand side of this formula
3191            is the highest bit of the LHS that can be set (starting from 0),
3192            so that the shift doesn't overflow.  We then right-shift the LHS
3193            to see whether any other bit is set making the original shift
3194            undefined -- the result is not representable in the corresponding
3195            unsigned type.  */
3196       tree t = build_int_cst (unsigned_type_node, uprec - 1);
3197       t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
3198       tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
3199       t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
3200       if (tree_int_cst_lt (integer_one_node, t))
3201           {
3202             if (!ctx->quiet)
3203               permerror (loc, "shift expression %q+E overflows",
3204                            build2_loc (loc, code, type, lhs, rhs));
3205             return (!flag_permissive || ctx->quiet);
3206           }
3207     }
3208   return false;
3209 }
3210 
3211 /* Subroutine of cxx_eval_constant_expression.
3212    Attempt to reduce the unary expression tree T to a compile time value.
3213    If successful, return the value.  Otherwise issue a diagnostic
3214    and return error_mark_node.  */
3215 
3216 static tree
cxx_eval_unary_expression(const constexpr_ctx * ctx,tree t,bool,bool * non_constant_p,bool * overflow_p)3217 cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
3218                                  bool /*lval*/,
3219                                  bool *non_constant_p, bool *overflow_p)
3220 {
3221   tree r;
3222   tree orig_arg = TREE_OPERAND (t, 0);
3223   tree arg = cxx_eval_constant_expression (ctx, orig_arg, /*lval*/false,
3224                                                      non_constant_p, overflow_p);
3225   VERIFY_CONSTANT (arg);
3226   location_t loc = EXPR_LOCATION (t);
3227   enum tree_code code = TREE_CODE (t);
3228   tree type = TREE_TYPE (t);
3229   r = fold_unary_loc (loc, code, type, arg);
3230   if (r == NULL_TREE)
3231     {
3232       if (arg == orig_arg)
3233           r = t;
3234       else
3235           r = build1_loc (loc, code, type, arg);
3236     }
3237   VERIFY_CONSTANT (r);
3238   return r;
3239 }
3240 
3241 /* Helper function for cxx_eval_binary_expression.  Try to optimize
3242    original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
3243    generic folding should be used.  */
3244 
3245 static tree
cxx_fold_pointer_plus_expression(const constexpr_ctx * ctx,tree t,tree lhs,tree rhs,bool * non_constant_p,bool * overflow_p)3246 cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
3247                                           tree lhs, tree rhs, bool *non_constant_p,
3248                                           bool *overflow_p)
3249 {
3250   STRIP_NOPS (lhs);
3251   if (TREE_CODE (lhs) != ADDR_EXPR)
3252     return NULL_TREE;
3253 
3254   lhs = TREE_OPERAND (lhs, 0);
3255 
3256   /* &A[i] p+ j => &A[i + j] */
3257   if (TREE_CODE (lhs) == ARRAY_REF
3258       && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST
3259       && TREE_CODE (rhs) == INTEGER_CST
3260       && TYPE_SIZE_UNIT (TREE_TYPE (lhs))
3261       && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST)
3262     {
3263       tree orig_type = TREE_TYPE (t);
3264       location_t loc = EXPR_LOCATION (t);
3265       tree type = TREE_TYPE (lhs);
3266 
3267       t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1));
3268       tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0)));
3269       nelts = cxx_eval_constant_expression (ctx, nelts, false, non_constant_p,
3270                                                       overflow_p);
3271       if (*non_constant_p)
3272           return NULL_TREE;
3273       /* Don't fold an out-of-bound access.  */
3274       if (!tree_int_cst_le (t, nelts))
3275           return NULL_TREE;
3276       rhs = cp_fold_convert (ssizetype, rhs);
3277       /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
3278            constexpr int A[1]; ... (char *)&A[0] + 1 */
3279       if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
3280                                                      rhs, TYPE_SIZE_UNIT (type))))
3281           return NULL_TREE;
3282       /* Make sure to treat the second operand of POINTER_PLUS_EXPR
3283            as signed.  */
3284       rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs,
3285                                    TYPE_SIZE_UNIT (type));
3286       t = size_binop_loc (loc, PLUS_EXPR, rhs, t);
3287       t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0),
3288                           t, NULL_TREE, NULL_TREE);
3289       t = cp_build_addr_expr (t, tf_warning_or_error);
3290       t = cp_fold_convert (orig_type, t);
3291       return cxx_eval_constant_expression (ctx, t, /*lval*/false,
3292                                                      non_constant_p, overflow_p);
3293     }
3294 
3295   return NULL_TREE;
3296 }
3297 
3298 /* Try to fold expressions like
3299    (struct S *) (&a[0].D.2378 + 12)
3300    into
3301    &MEM <struct T> [(void *)&a + 12B]
3302    This is something normally done by gimple_fold_stmt_to_constant_1
3303    on GIMPLE, but is undesirable on GENERIC if we are e.g. going to
3304    dereference the address because some details are lost.
3305    For pointer comparisons we want such folding though so that
3306    match.pd address_compare optimization works.  */
3307 
3308 static tree
cxx_maybe_fold_addr_pointer_plus(tree t)3309 cxx_maybe_fold_addr_pointer_plus (tree t)
3310 {
3311   while (CONVERT_EXPR_P (t)
3312            && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
3313     t = TREE_OPERAND (t, 0);
3314   if (TREE_CODE (t) != POINTER_PLUS_EXPR)
3315     return NULL_TREE;
3316   tree op0 = TREE_OPERAND (t, 0);
3317   tree op1 = TREE_OPERAND (t, 1);
3318   if (TREE_CODE (op1) != INTEGER_CST)
3319     return NULL_TREE;
3320   while (CONVERT_EXPR_P (op0)
3321            && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
3322     op0 = TREE_OPERAND (op0, 0);
3323   if (TREE_CODE (op0) != ADDR_EXPR)
3324     return NULL_TREE;
3325   op1 = fold_convert (ptr_type_node, op1);
3326   tree r = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)), op0, op1);
3327   return build1_loc (EXPR_LOCATION (t), ADDR_EXPR, TREE_TYPE (op0), r);
3328 }
3329 
3330 /* Subroutine of cxx_eval_constant_expression.
3331    Like cxx_eval_unary_expression, except for binary expressions.  */
3332 
3333 static tree
cxx_eval_binary_expression(const constexpr_ctx * ctx,tree t,bool lval,bool * non_constant_p,bool * overflow_p)3334 cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
3335                                   bool lval,
3336                                   bool *non_constant_p, bool *overflow_p)
3337 {
3338   tree r = NULL_TREE;
3339   tree orig_lhs = TREE_OPERAND (t, 0);
3340   tree orig_rhs = TREE_OPERAND (t, 1);
3341   tree lhs, rhs;
3342   lhs = cxx_eval_constant_expression (ctx, orig_lhs, /*lval*/false,
3343                                               non_constant_p, overflow_p);
3344   /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
3345      subtraction.  */
3346   if (*non_constant_p)
3347     return t;
3348   rhs = cxx_eval_constant_expression (ctx, orig_rhs, /*lval*/false,
3349                                               non_constant_p, overflow_p);
3350   if (*non_constant_p)
3351     return t;
3352 
3353   location_t loc = EXPR_LOCATION (t);
3354   enum tree_code code = TREE_CODE (t);
3355   tree type = TREE_TYPE (t);
3356 
3357   if (code == EQ_EXPR || code == NE_EXPR)
3358     {
3359       bool is_code_eq = (code == EQ_EXPR);
3360 
3361       if (TREE_CODE (lhs) == PTRMEM_CST
3362             && TREE_CODE (rhs) == PTRMEM_CST)
3363           {
3364             tree lmem = PTRMEM_CST_MEMBER (lhs);
3365             tree rmem = PTRMEM_CST_MEMBER (rhs);
3366             bool eq;
3367             if (TREE_CODE (lmem) == TREE_CODE (rmem)
3368                 && TREE_CODE (lmem) == FIELD_DECL
3369                 && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE
3370                 && same_type_p (DECL_CONTEXT (lmem),
3371                                     DECL_CONTEXT (rmem)))
3372               /* If both refer to (possibly different) members of the same union
3373                  (12.3), they compare equal. */
3374               eq = true;
3375             else
3376               eq = cp_tree_equal (lhs, rhs);
3377             r = constant_boolean_node (eq == is_code_eq, type);
3378           }
3379       else if ((TREE_CODE (lhs) == PTRMEM_CST
3380                     || TREE_CODE (rhs) == PTRMEM_CST)
3381                  && (null_member_pointer_value_p (lhs)
3382                        || null_member_pointer_value_p (rhs)))
3383           r = constant_boolean_node (!is_code_eq, type);
3384       else if (TREE_CODE (lhs) == PTRMEM_CST)
3385           lhs = cplus_expand_constant (lhs);
3386       else if (TREE_CODE (rhs) == PTRMEM_CST)
3387           rhs = cplus_expand_constant (rhs);
3388     }
3389   if (r == NULL_TREE
3390       && TREE_CODE_CLASS (code) == tcc_comparison
3391       && POINTER_TYPE_P (TREE_TYPE (lhs)))
3392     {
3393       if (tree lhso = cxx_maybe_fold_addr_pointer_plus (lhs))
3394           lhs = fold_convert (TREE_TYPE (lhs), lhso);
3395       if (tree rhso = cxx_maybe_fold_addr_pointer_plus (rhs))
3396           rhs = fold_convert (TREE_TYPE (rhs), rhso);
3397     }
3398   if (code == POINTER_PLUS_EXPR && !*non_constant_p
3399       && integer_zerop (lhs) && !integer_zerop (rhs))
3400     {
3401       if (!ctx->quiet)
3402           error ("arithmetic involving a null pointer in %qE", lhs);
3403       *non_constant_p = true;
3404       return t;
3405     }
3406   else if (code == POINTER_PLUS_EXPR)
3407     r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p,
3408                                                     overflow_p);
3409   else if (code == SPACESHIP_EXPR)
3410     {
3411       r = genericize_spaceship (loc, type, lhs, rhs);
3412       return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
3413                                                      overflow_p);
3414     }
3415 
3416   if (r == NULL_TREE)
3417     {
3418       if (ctx->manifestly_const_eval
3419             && (flag_constexpr_fp_except
3420                 || TREE_CODE (type) != REAL_TYPE))
3421           {
3422             auto ofcc = make_temp_override (folding_cxx_constexpr, true);
3423             r = fold_binary_initializer_loc (loc, code, type, lhs, rhs);
3424           }
3425       else
3426           r = fold_binary_loc (loc, code, type, lhs, rhs);
3427     }
3428 
3429   if (r == NULL_TREE
3430       && (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
3431       && TREE_CODE (lhs) == INTEGER_CST
3432       && TREE_CODE (rhs) == INTEGER_CST
3433       && wi::neg_p (wi::to_wide (rhs)))
3434     {
3435       /* For diagnostics and -fpermissive emulate previous behavior of
3436            handling shifts by negative amount.  */
3437       tree nrhs = const_unop (NEGATE_EXPR, TREE_TYPE (rhs), rhs);
3438       if (nrhs)
3439           r = fold_binary_loc (loc,
3440                                    code == LSHIFT_EXPR ? RSHIFT_EXPR : LSHIFT_EXPR,
3441                                    type, lhs, nrhs);
3442     }
3443 
3444   if (r == NULL_TREE)
3445     {
3446       if (lhs == orig_lhs && rhs == orig_rhs)
3447           r = t;
3448       else
3449           r = build2_loc (loc, code, type, lhs, rhs);
3450     }
3451   else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
3452     *non_constant_p = true;
3453   /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
3454      a local array in a constexpr function.  */
3455   bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs));
3456   if (!ptr)
3457     VERIFY_CONSTANT (r);
3458   return r;
3459 }
3460 
3461 /* Subroutine of cxx_eval_constant_expression.
3462    Attempt to evaluate condition expressions.  Dead branches are not
3463    looked into.  */
3464 
3465 static tree
cxx_eval_conditional_expression(const constexpr_ctx * ctx,tree t,bool lval,bool * non_constant_p,bool * overflow_p,tree * jump_target)3466 cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
3467                                          bool lval,
3468                                          bool *non_constant_p, bool *overflow_p,
3469                                          tree *jump_target)
3470 {
3471   tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3472                                                      /*lval*/false,
3473                                                      non_constant_p, overflow_p);
3474   VERIFY_CONSTANT (val);
3475   if (TREE_CODE (t) == IF_STMT && IF_STMT_CONSTEVAL_P (t))
3476     {
3477       /* Evaluate the condition as if it was
3478            if (__builtin_is_constant_evaluated ()), i.e. defer it if not
3479            ctx->manifestly_const_eval (as sometimes we try to constant evaluate
3480            without manifestly_const_eval even expressions or parts thereof which
3481            will later be manifestly const_eval evaluated), otherwise fold it to
3482            true.  */
3483       if (ctx->manifestly_const_eval)
3484           val = boolean_true_node;
3485       else
3486           {
3487             *non_constant_p = true;
3488             return t;
3489           }
3490     }
3491   /* Don't VERIFY_CONSTANT the other operands.  */
3492   if (integer_zerop (val))
3493     val = TREE_OPERAND (t, 2);
3494   else
3495     val = TREE_OPERAND (t, 1);
3496   if (TREE_CODE (t) == IF_STMT && !val)
3497     val = void_node;
3498   return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
3499                                                overflow_p, jump_target);
3500 }
3501 
3502 /* Subroutine of cxx_eval_constant_expression.
3503    Attempt to evaluate vector condition expressions.  Unlike
3504    cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
3505    ternary arithmetics operation, where all 3 arguments have to be
3506    evaluated as constants and then folding computes the result from
3507    them.  */
3508 
3509 static tree
cxx_eval_vector_conditional_expression(const constexpr_ctx * ctx,tree t,bool * non_constant_p,bool * overflow_p)3510 cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t,
3511                                                   bool *non_constant_p, bool *overflow_p)
3512 {
3513   tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3514                                                       /*lval*/false,
3515                                                       non_constant_p, overflow_p);
3516   VERIFY_CONSTANT (arg1);
3517   tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3518                                                       /*lval*/false,
3519                                                       non_constant_p, overflow_p);
3520   VERIFY_CONSTANT (arg2);
3521   tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
3522                                                       /*lval*/false,
3523                                                       non_constant_p, overflow_p);
3524   VERIFY_CONSTANT (arg3);
3525   location_t loc = EXPR_LOCATION (t);
3526   tree type = TREE_TYPE (t);
3527   tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
3528   if (r == NULL_TREE)
3529     {
3530       if (arg1 == TREE_OPERAND (t, 0)
3531             && arg2 == TREE_OPERAND (t, 1)
3532             && arg3 == TREE_OPERAND (t, 2))
3533           r = t;
3534       else
3535           r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
3536     }
3537   VERIFY_CONSTANT (r);
3538   return r;
3539 }
3540 
3541 /* Returns less than, equal to, or greater than zero if KEY is found to be
3542    less than, to match, or to be greater than the constructor_elt's INDEX.  */
3543 
3544 static int
array_index_cmp(tree key,tree index)3545 array_index_cmp (tree key, tree index)
3546 {
3547   gcc_assert (TREE_CODE (key) == INTEGER_CST);
3548 
3549   switch (TREE_CODE (index))
3550     {
3551     case INTEGER_CST:
3552       return tree_int_cst_compare (key, index);
3553     case RANGE_EXPR:
3554       {
3555           tree lo = TREE_OPERAND (index, 0);
3556           tree hi = TREE_OPERAND (index, 1);
3557           if (tree_int_cst_lt (key, lo))
3558             return -1;
3559           else if (tree_int_cst_lt (hi, key))
3560             return 1;
3561           else
3562             return 0;
3563       }
3564     default:
3565       gcc_unreachable ();
3566     }
3567 }
3568 
3569 /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
3570    if none.  If INSERT is true, insert a matching element rather than fail.  */
3571 
3572 static HOST_WIDE_INT
find_array_ctor_elt(tree ary,tree dindex,bool insert)3573 find_array_ctor_elt (tree ary, tree dindex, bool insert)
3574 {
3575   if (tree_int_cst_sgn (dindex) < 0)
3576     return -1;
3577 
3578   unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
3579   vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
3580   unsigned HOST_WIDE_INT len = vec_safe_length (elts);
3581 
3582   unsigned HOST_WIDE_INT end = len;
3583   unsigned HOST_WIDE_INT begin = 0;
3584 
3585   /* If the last element of the CONSTRUCTOR has its own index, we can assume
3586      that the same is true of the other elements and index directly.  */
3587   if (end > 0)
3588     {
3589       tree cindex = (*elts)[end - 1].index;
3590       if (cindex == NULL_TREE)
3591           {
3592             /* Verify that if the last index is missing, all indexes
3593                are missing.  */
3594             if (flag_checking)
3595               for (unsigned int j = 0; j < len - 1; ++j)
3596                 gcc_assert ((*elts)[j].index == NULL_TREE);
3597             if (i < end)
3598               return i;
3599             else
3600               {
3601                 begin = end;
3602                 if (i == end)
3603                     /* If the element is to be added right at the end,
3604                        make sure it is added with cleared index too.  */
3605                     dindex = NULL_TREE;
3606                 else if (insert)
3607                     /* Otherwise, in order not to break the assumption
3608                        that CONSTRUCTOR either has all indexes or none,
3609                        we need to add indexes to all elements.  */
3610                     for (unsigned int j = 0; j < len; ++j)
3611                       (*elts)[j].index = build_int_cst (TREE_TYPE (dindex), j);
3612               }
3613           }
3614       else if (TREE_CODE (cindex) == INTEGER_CST
3615                  && compare_tree_int (cindex, end - 1) == 0)
3616           {
3617             if (i < end)
3618               return i;
3619             else
3620               begin = end;
3621           }
3622     }
3623 
3624   /* Otherwise, find a matching index by means of a binary search.  */
3625   while (begin != end)
3626     {
3627       unsigned HOST_WIDE_INT middle = (begin + end) / 2;
3628       constructor_elt &elt = (*elts)[middle];
3629       tree idx = elt.index;
3630 
3631       int cmp = array_index_cmp (dindex, idx);
3632       if (cmp < 0)
3633           end = middle;
3634       else if (cmp > 0)
3635           begin = middle + 1;
3636       else
3637           {
3638             if (insert && TREE_CODE (idx) == RANGE_EXPR)
3639               {
3640                 /* We need to split the range.  */
3641                 constructor_elt e;
3642                 tree lo = TREE_OPERAND (idx, 0);
3643                 tree hi = TREE_OPERAND (idx, 1);
3644                 tree value = elt.value;
3645                 dindex = fold_convert (sizetype, dindex);
3646                 if (tree_int_cst_lt (lo, dindex))
3647                     {
3648                       /* There are still some lower elts; shorten the range.  */
3649                       tree new_hi = int_const_binop (MINUS_EXPR, dindex,
3650                                                              size_one_node);
3651                       if (tree_int_cst_equal (lo, new_hi))
3652                         /* Only one element left, no longer a range.  */
3653                         elt.index = lo;
3654                       else
3655                         TREE_OPERAND (idx, 1) = new_hi;
3656                       /* Append the element we want to insert.  */
3657                       ++middle;
3658                       e.index = dindex;
3659                       e.value = unshare_constructor (value);
3660                       vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
3661                     }
3662                 else
3663                     /* No lower elts, the range elt is now ours.  */
3664                     elt.index = dindex;
3665 
3666                 if (tree_int_cst_lt (dindex, hi))
3667                     {
3668                       /* There are still some higher elts; append a range.  */
3669                       tree new_lo = int_const_binop (PLUS_EXPR, dindex,
3670                                                              size_one_node);
3671                       if (tree_int_cst_equal (new_lo, hi))
3672                         e.index = hi;
3673                       else
3674                         e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi);
3675                       e.value = unshare_constructor (value);
3676                       vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
3677                     }
3678               }
3679             return middle;
3680           }
3681     }
3682 
3683   if (insert)
3684     {
3685       constructor_elt e = { dindex, NULL_TREE };
3686       vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
3687       return end;
3688     }
3689 
3690   return -1;
3691 }
3692 
3693 /* Return a pointer to the constructor_elt of CTOR which matches INDEX.  If no
3694    matching constructor_elt exists, then add one to CTOR.
3695 
3696    As an optimization, if POS_HINT is non-negative then it is used as a guess
3697    for the (integer) index of the matching constructor_elt within CTOR.  */
3698 
3699 static constructor_elt *
get_or_insert_ctor_field(tree ctor,tree index,int pos_hint=-1)3700 get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1)
3701 {
3702   /* Check the hint first.  */
3703   if (pos_hint >= 0 && (unsigned)pos_hint < CONSTRUCTOR_NELTS (ctor)
3704       && CONSTRUCTOR_ELT (ctor, pos_hint)->index == index)
3705     return CONSTRUCTOR_ELT (ctor, pos_hint);
3706 
3707   tree type = TREE_TYPE (ctor);
3708   if (TREE_CODE (type) == VECTOR_TYPE && index == NULL_TREE)
3709     {
3710       CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), index, NULL_TREE);
3711       return &CONSTRUCTOR_ELTS (ctor)->last();
3712     }
3713   else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
3714     {
3715       if (TREE_CODE (index) == RANGE_EXPR)
3716           {
3717             /* Support for RANGE_EXPR index lookups is currently limited to
3718                accessing an existing element via POS_HINT, or appending a new
3719                element to the end of CTOR.  ??? Support for other access
3720                patterns may also be needed.  */
3721             vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3722             if (vec_safe_length (elts))
3723               {
3724                 tree lo = TREE_OPERAND (index, 0);
3725                 gcc_assert (array_index_cmp (elts->last().index, lo) < 0);
3726               }
3727             CONSTRUCTOR_APPEND_ELT (elts, index, NULL_TREE);
3728             return &elts->last();
3729           }
3730 
3731       HOST_WIDE_INT i = find_array_ctor_elt (ctor, index, /*insert*/true);
3732       gcc_assert (i >= 0);
3733       constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i);
3734       gcc_assert (cep->index == NULL_TREE
3735                       || TREE_CODE (cep->index) != RANGE_EXPR);
3736       return cep;
3737     }
3738   else
3739     {
3740       gcc_assert (TREE_CODE (index) == FIELD_DECL
3741                       && (same_type_ignoring_top_level_qualifiers_p
3742                           (DECL_CONTEXT (index), TREE_TYPE (ctor))));
3743 
3744       /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
3745            Usually we meet initializers in that order, but it is
3746            possible for base types to be placed not in program
3747            order.  */
3748       tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
3749       unsigned HOST_WIDE_INT idx = 0;
3750       constructor_elt *cep = NULL;
3751 
3752       /* Check if we're changing the active member of a union.  */
3753       if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor)
3754             && CONSTRUCTOR_ELT (ctor, 0)->index != index)
3755           vec_safe_truncate (CONSTRUCTOR_ELTS (ctor), 0);
3756       /* If the bit offset of INDEX is larger than that of the last
3757            constructor_elt, then we can just immediately append a new
3758            constructor_elt to the end of CTOR.  */
3759       else if (CONSTRUCTOR_NELTS (ctor)
3760                  && tree_int_cst_compare (bit_position (index),
3761                                                   bit_position (CONSTRUCTOR_ELTS (ctor)
3762                                                                   ->last().index)) > 0)
3763           {
3764             idx = CONSTRUCTOR_NELTS (ctor);
3765             goto insert;
3766           }
3767 
3768       /* Otherwise, we need to iterate over CTOR to find or insert INDEX
3769            appropriately.  */
3770 
3771       for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), idx, &cep);
3772              idx++, fields = DECL_CHAIN (fields))
3773           {
3774             if (index == cep->index)
3775               goto found;
3776 
3777             /* The field we're initializing must be on the field
3778                list.  Look to see if it is present before the
3779                field the current ELT initializes.  */
3780             for (; fields != cep->index; fields = DECL_CHAIN (fields))
3781               if (index == fields)
3782                 goto insert;
3783           }
3784       /* We fell off the end of the CONSTRUCTOR, so insert a new
3785            entry at the end.  */
3786 
3787     insert:
3788       {
3789           constructor_elt ce = { index, NULL_TREE };
3790 
3791           vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce);
3792           cep = CONSTRUCTOR_ELT (ctor, idx);
3793       }
3794     found:;
3795 
3796       return cep;
3797     }
3798 }
3799 
3800 /* Under the control of CTX, issue a detailed diagnostic for
3801    an out-of-bounds subscript INDEX into the expression ARRAY.  */
3802 
3803 static void
diag_array_subscript(location_t loc,const constexpr_ctx * ctx,tree array,tree index)3804 diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index)
3805 {
3806   if (!ctx->quiet)
3807     {
3808       tree arraytype = TREE_TYPE (array);
3809 
3810       /* Convert the unsigned array subscript to a signed integer to avoid
3811            printing huge numbers for small negative values.  */
3812       tree sidx = fold_convert (ssizetype, index);
3813       STRIP_ANY_LOCATION_WRAPPER (array);
3814       if (DECL_P (array))
3815           {
3816             if (TYPE_DOMAIN (arraytype))
3817               error_at (loc, "array subscript value %qE is outside the bounds "
3818                           "of array %qD of type %qT", sidx, array, arraytype);
3819             else
3820               error_at (loc, "nonzero array subscript %qE is used with array %qD of "
3821                           "type %qT with unknown bounds", sidx, array, arraytype);
3822             inform (DECL_SOURCE_LOCATION (array), "declared here");
3823           }
3824       else if (TYPE_DOMAIN (arraytype))
3825           error_at (loc, "array subscript value %qE is outside the bounds "
3826                       "of array type %qT", sidx, arraytype);
3827       else
3828           error_at (loc, "nonzero array subscript %qE is used with array of type %qT "
3829                       "with unknown bounds", sidx, arraytype);
3830     }
3831 }
3832 
3833 /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
3834    a VECTOR_TYPE).  */
3835 
3836 static tree
get_array_or_vector_nelts(const constexpr_ctx * ctx,tree type,bool * non_constant_p,bool * overflow_p)3837 get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type,
3838                                  bool *non_constant_p, bool *overflow_p)
3839 {
3840   tree nelts;
3841   if (TREE_CODE (type) == ARRAY_TYPE)
3842     {
3843       if (TYPE_DOMAIN (type))
3844           nelts = array_type_nelts_top (type);
3845       else
3846           nelts = size_zero_node;
3847     }
3848   else if (VECTOR_TYPE_P (type))
3849     nelts = size_int (TYPE_VECTOR_SUBPARTS (type));
3850   else
3851     gcc_unreachable ();
3852 
3853   /* For VLAs, the number of elements won't be an integer constant.  */
3854   nelts = cxx_eval_constant_expression (ctx, nelts, false,
3855                                                   non_constant_p, overflow_p);
3856   return nelts;
3857 }
3858 
3859 /* Extract element INDEX consisting of CHARS_PER_ELT chars from
3860    STRING_CST STRING.  */
3861 
3862 static tree
extract_string_elt(tree string,unsigned chars_per_elt,unsigned index)3863 extract_string_elt (tree string, unsigned chars_per_elt, unsigned index)
3864 {
3865   tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string)));
3866   tree r;
3867 
3868   if (chars_per_elt == 1)
3869     r = build_int_cst (type, TREE_STRING_POINTER (string)[index]);
3870   else
3871     {
3872       const unsigned char *ptr
3873           = ((const unsigned char *)TREE_STRING_POINTER (string)
3874              + index * chars_per_elt);
3875       r = native_interpret_expr (type, ptr, chars_per_elt);
3876     }
3877   return r;
3878 }
3879 
3880 /* Subroutine of cxx_eval_array_reference.  T is an ARRAY_REF; evaluate the
3881    subscript, diagnose any problems with it, and return the result.  */
3882 
3883 static tree
eval_and_check_array_index(const constexpr_ctx * ctx,tree t,bool allow_one_past,bool * non_constant_p,bool * overflow_p)3884 eval_and_check_array_index (const constexpr_ctx *ctx,
3885                                   tree t, bool allow_one_past,
3886                                   bool *non_constant_p, bool *overflow_p)
3887 {
3888   location_t loc = cp_expr_loc_or_input_loc (t);
3889   tree ary = TREE_OPERAND (t, 0);
3890   t = TREE_OPERAND (t, 1);
3891   tree index = cxx_eval_constant_expression (ctx, t, false,
3892                                                        non_constant_p, overflow_p);
3893   VERIFY_CONSTANT (index);
3894 
3895   if (!tree_fits_shwi_p (index)
3896       || tree_int_cst_sgn (index) < 0)
3897     {
3898       diag_array_subscript (loc, ctx, ary, index);
3899       *non_constant_p = true;
3900       return t;
3901     }
3902 
3903   tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p,
3904                                                     overflow_p);
3905   VERIFY_CONSTANT (nelts);
3906   if (allow_one_past
3907       ? !tree_int_cst_le (index, nelts)
3908       : !tree_int_cst_lt (index, nelts))
3909     {
3910       diag_array_subscript (loc, ctx, ary, index);
3911       *non_constant_p = true;
3912       return t;
3913     }
3914 
3915   return index;
3916 }
3917 
3918 /* Subroutine of cxx_eval_constant_expression.
3919    Attempt to reduce a reference to an array slot.  */
3920 
3921 static tree
cxx_eval_array_reference(const constexpr_ctx * ctx,tree t,bool lval,bool * non_constant_p,bool * overflow_p)3922 cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
3923                                 bool lval,
3924                                 bool *non_constant_p, bool *overflow_p)
3925 {
3926   tree oldary = TREE_OPERAND (t, 0);
3927   tree ary = cxx_eval_constant_expression (ctx, oldary,
3928                                                      lval,
3929                                                      non_constant_p, overflow_p);
3930   if (*non_constant_p)
3931     return t;
3932   if (!lval
3933       && TREE_CODE (ary) == VIEW_CONVERT_EXPR
3934       && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0)))
3935       && (TYPE_MAIN_VARIANT (TREE_TYPE (t))
3936             == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0))))))
3937     ary = TREE_OPERAND (ary, 0);
3938 
3939   tree oldidx = TREE_OPERAND (t, 1);
3940   tree index = eval_and_check_array_index (ctx, t, lval,
3941                                                      non_constant_p, overflow_p);
3942   if (*non_constant_p)
3943     return t;
3944 
3945   if (lval && ary == oldary && index == oldidx)
3946     return t;
3947   else if (lval)
3948     return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
3949 
3950   unsigned len = 0, elem_nchars = 1;
3951   tree elem_type = TREE_TYPE (TREE_TYPE (ary));
3952   if (TREE_CODE (ary) == CONSTRUCTOR)
3953     len = CONSTRUCTOR_NELTS (ary);
3954   else if (TREE_CODE (ary) == STRING_CST)
3955     {
3956       elem_nchars = (TYPE_PRECISION (elem_type)
3957                          / TYPE_PRECISION (char_type_node));
3958       len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
3959     }
3960   else if (TREE_CODE (ary) == VECTOR_CST)
3961     /* We don't create variable-length VECTOR_CSTs.  */
3962     len = VECTOR_CST_NELTS (ary).to_constant ();
3963   else
3964     {
3965       /* We can't do anything with other tree codes, so use
3966            VERIFY_CONSTANT to complain and fail.  */
3967       VERIFY_CONSTANT (ary);
3968       gcc_unreachable ();
3969     }
3970 
3971   bool found;
3972   HOST_WIDE_INT i = 0;
3973   if (TREE_CODE (ary) == CONSTRUCTOR)
3974     {
3975       HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
3976       found = (ix >= 0);
3977       if (found)
3978           i = ix;
3979     }
3980   else
3981     {
3982       i = tree_to_shwi (index);
3983       found = (i < len);
3984     }
3985 
3986   if (found)
3987     {
3988       tree r;
3989       if (TREE_CODE (ary) == CONSTRUCTOR)
3990           r = (*CONSTRUCTOR_ELTS (ary))[i].value;
3991       else if (TREE_CODE (ary) == VECTOR_CST)
3992           r = VECTOR_CST_ELT (ary, i);
3993       else
3994           r = extract_string_elt (ary, elem_nchars, i);
3995 
3996       if (r)
3997           /* Don't VERIFY_CONSTANT here.  */
3998           return r;
3999 
4000       /* Otherwise the element doesn't have a value yet.  */
4001     }
4002 
4003   /* Not found.  */
4004 
4005   if (TREE_CODE (ary) == CONSTRUCTOR
4006       && CONSTRUCTOR_NO_CLEARING (ary))
4007     {
4008       /* 'ary' is part of the aggregate initializer we're currently
4009            building; if there's no initializer for this element yet,
4010            that's an error.  */
4011       if (!ctx->quiet)
4012           error ("accessing uninitialized array element");
4013       *non_constant_p = true;
4014       return t;
4015     }
4016 
4017   /* If it's within the array bounds but doesn't have an explicit
4018      initializer, it's initialized from {}.  But use build_value_init
4019      directly for non-aggregates to avoid creating a garbage CONSTRUCTOR.  */
4020   tree val;
4021   constexpr_ctx new_ctx;
4022   if (is_really_empty_class (elem_type, /*ignore_vptr*/false))
4023     return build_constructor (elem_type, NULL);
4024   else if (CP_AGGREGATE_TYPE_P (elem_type))
4025     {
4026       tree empty_ctor = build_constructor (init_list_type_node, NULL);
4027       val = digest_init (elem_type, empty_ctor, tf_warning_or_error);
4028     }
4029   else
4030     val = build_value_init (elem_type, tf_warning_or_error);
4031 
4032   if (!SCALAR_TYPE_P (elem_type))
4033     {
4034       new_ctx = *ctx;
4035       if (ctx->object)
4036           /* If there was no object, don't add one: it could confuse us
4037              into thinking we're modifying a const object.  */
4038           new_ctx.object = t;
4039       new_ctx.ctor = build_constructor (elem_type, NULL);
4040       ctx = &new_ctx;
4041     }
4042   t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
4043                                             overflow_p);
4044   if (!SCALAR_TYPE_P (elem_type) && t != ctx->ctor)
4045     free_constructor (ctx->ctor);
4046   return t;
4047 }
4048 
4049 /* Subroutine of cxx_eval_constant_expression.
4050    Attempt to reduce a field access of a value of class type.  */
4051 
4052 static tree
cxx_eval_component_reference(const constexpr_ctx * ctx,tree t,bool lval,bool * non_constant_p,bool * overflow_p)4053 cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
4054                                     bool lval,
4055                                     bool *non_constant_p, bool *overflow_p)
4056 {
4057   unsigned HOST_WIDE_INT i;
4058   tree field;
4059   tree value;
4060   tree part = TREE_OPERAND (t, 1);
4061   tree orig_whole = TREE_OPERAND (t, 0);
4062   tree whole = cxx_eval_constant_expression (ctx, orig_whole,
4063                                                        lval,
4064                                                        non_constant_p, overflow_p);
4065   if (INDIRECT_REF_P (whole)
4066       && integer_zerop (TREE_OPERAND (whole, 0)))
4067     {
4068       if (!ctx->quiet)
4069           error ("dereferencing a null pointer in %qE", orig_whole);
4070       *non_constant_p = true;
4071       return t;
4072     }
4073 
4074   if (TREE_CODE (whole) == PTRMEM_CST)
4075     whole = cplus_expand_constant (whole);
4076   if (whole == orig_whole)
4077     return t;
4078   if (lval)
4079     return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
4080                               whole, part, NULL_TREE);
4081   /* Don't VERIFY_CONSTANT here; we only want to check that we got a
4082      CONSTRUCTOR.  */
4083   if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
4084     {
4085       if (!ctx->quiet)
4086           error ("%qE is not a constant expression", orig_whole);
4087       *non_constant_p = true;
4088     }
4089   if (DECL_MUTABLE_P (part))
4090     {
4091       if (!ctx->quiet)
4092           error ("mutable %qD is not usable in a constant expression", part);
4093       *non_constant_p = true;
4094     }
4095   if (*non_constant_p)
4096     return t;
4097   bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole));
4098   FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
4099     {
4100       /* Use name match for PMF fields, as a variant will have a
4101            different FIELD_DECL with a different type.  */
4102       if (pmf ? DECL_NAME (field) == DECL_NAME (part)
4103             : field == part)
4104           {
4105             if (value)
4106               {
4107                 STRIP_ANY_LOCATION_WRAPPER (value);
4108                 return value;
4109               }
4110             else
4111               /* We're in the middle of initializing it.  */
4112               break;
4113           }
4114     }
4115   if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
4116       && CONSTRUCTOR_NELTS (whole) > 0)
4117     {
4118       /* DR 1188 says we don't have to deal with this.  */
4119       if (!ctx->quiet)
4120           {
4121             constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0);
4122             if (cep->value == NULL_TREE)
4123               error ("accessing uninitialized member %qD", part);
4124             else
4125               error ("accessing %qD member instead of initialized %qD member in "
4126                        "constant expression", part, cep->index);
4127           }
4128       *non_constant_p = true;
4129       return t;
4130     }
4131 
4132   /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
4133      classes never get represented; throw together a value now.  */
4134   if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
4135     return build_constructor (TREE_TYPE (t), NULL);
4136 
4137   gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole)));
4138 
4139   if (CONSTRUCTOR_NO_CLEARING (whole))
4140     {
4141       /* 'whole' is part of the aggregate initializer we're currently
4142            building; if there's no initializer for this member yet, that's an
4143            error.  */
4144       if (!ctx->quiet)
4145           error ("accessing uninitialized member %qD", part);
4146       *non_constant_p = true;
4147       return t;
4148     }
4149 
4150   /* If there's no explicit init for this field, it's value-initialized.  */
4151   value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
4152   return cxx_eval_constant_expression (ctx, value,
4153                                                lval,
4154                                                non_constant_p, overflow_p);
4155 }
4156 
4157 /* Subroutine of cxx_eval_constant_expression.
4158    Attempt to reduce a field access of a value of class type that is
4159    expressed as a BIT_FIELD_REF.  */
4160 
4161 static tree
cxx_eval_bit_field_ref(const constexpr_ctx * ctx,tree t,bool lval,bool * non_constant_p,bool * overflow_p)4162 cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
4163                               bool lval,
4164                               bool *non_constant_p, bool *overflow_p)
4165 {
4166   tree orig_whole = TREE_OPERAND (t, 0);
4167   tree retval, fldval, utype, mask;
4168   bool fld_seen = false;
4169   HOST_WIDE_INT istart, isize;
4170   tree whole = cxx_eval_constant_expression (ctx, orig_whole,
4171                                                        lval,
4172                                                        non_constant_p, overflow_p);
4173   tree start, field, value;
4174   unsigned HOST_WIDE_INT i;
4175 
4176   if (whole == orig_whole)
4177     return t;
4178   /* Don't VERIFY_CONSTANT here; we only want to check that we got a
4179      CONSTRUCTOR.  */
4180   if (!*non_constant_p
4181       && TREE_CODE (whole) != VECTOR_CST
4182       && TREE_CODE (whole) != CONSTRUCTOR)
4183     {
4184       if (!ctx->quiet)
4185           error ("%qE is not a constant expression", orig_whole);
4186       *non_constant_p = true;
4187     }
4188   if (*non_constant_p)
4189     return t;
4190 
4191   if (TREE_CODE (whole) == VECTOR_CST || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
4192     {
4193       if (tree r = fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
4194                                          TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)))
4195           return r;
4196       if (!ctx->quiet)
4197           error ("%qE is not a constant expression", orig_whole);
4198       *non_constant_p = true;
4199       return t;
4200     }
4201 
4202   start = TREE_OPERAND (t, 2);
4203   istart = tree_to_shwi (start);
4204   isize = tree_to_shwi (TREE_OPERAND (t, 1));
4205   utype = TREE_TYPE (t);
4206   if (!TYPE_UNSIGNED (utype))
4207     utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
4208   retval = build_int_cst (utype, 0);
4209   FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
4210     {
4211       tree bitpos = bit_position (field);
4212       STRIP_ANY_LOCATION_WRAPPER (value);
4213       if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
4214           return value;
4215       if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
4216             && TREE_CODE (value) == INTEGER_CST
4217             && tree_fits_shwi_p (bitpos)
4218             && tree_fits_shwi_p (DECL_SIZE (field)))
4219           {
4220             HOST_WIDE_INT bit = tree_to_shwi (bitpos);
4221             HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
4222             HOST_WIDE_INT shift;
4223             if (bit >= istart && bit + sz <= istart + isize)
4224               {
4225                 fldval = fold_convert (utype, value);
4226                 mask = build_int_cst_type (utype, -1);
4227                 mask = fold_build2 (LSHIFT_EXPR, utype, mask,
4228                                           size_int (TYPE_PRECISION (utype) - sz));
4229                 mask = fold_build2 (RSHIFT_EXPR, utype, mask,
4230                                           size_int (TYPE_PRECISION (utype) - sz));
4231                 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
4232                 shift = bit - istart;
4233                 if (BYTES_BIG_ENDIAN)
4234                     shift = TYPE_PRECISION (utype) - shift - sz;
4235                 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
4236                                             size_int (shift));
4237                 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
4238                 fld_seen = true;
4239               }
4240           }
4241     }
4242   if (fld_seen)
4243     return fold_convert (TREE_TYPE (t), retval);
4244   gcc_unreachable ();
4245   return error_mark_node;
4246 }
4247 
4248 /* Helper for cxx_eval_bit_cast.
4249    Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From
4250    types and types of all subobjects have is_union_v<T>, is_pointer_v<T>,
4251    is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static
4252    data members of reference type.  */
4253 
4254 static bool
check_bit_cast_type(const constexpr_ctx * ctx,location_t loc,tree type,tree orig_type)4255 check_bit_cast_type (const constexpr_ctx *ctx, location_t loc, tree type,
4256                          tree orig_type)
4257 {
4258   if (TREE_CODE (type) == UNION_TYPE)
4259     {
4260       if (!ctx->quiet)
4261           {
4262             if (type == orig_type)
4263               error_at (loc, "%qs is not a constant expression because %qT is "
4264                                  "a union type", "__builtin_bit_cast", type);
4265             else
4266               error_at (loc, "%qs is not a constant expression because %qT "
4267                                  "contains a union type", "__builtin_bit_cast",
4268                           orig_type);
4269           }
4270       return true;
4271     }
4272   if (TREE_CODE (type) == POINTER_TYPE)
4273     {
4274       if (!ctx->quiet)
4275           {
4276             if (type == orig_type)
4277               error_at (loc, "%qs is not a constant expression because %qT is "
4278                                  "a pointer type", "__builtin_bit_cast", type);
4279             else
4280               error_at (loc, "%qs is not a constant expression because %qT "
4281                                  "contains a pointer type", "__builtin_bit_cast",
4282                           orig_type);
4283           }
4284       return true;
4285     }
4286   if (TREE_CODE (type) == REFERENCE_TYPE)
4287     {
4288       if (!ctx->quiet)
4289           {
4290             if (type == orig_type)
4291               error_at (loc, "%qs is not a constant expression because %qT is "
4292                                  "a reference type", "__builtin_bit_cast", type);
4293             else
4294               error_at (loc, "%qs is not a constant expression because %qT "
4295                                  "contains a reference type", "__builtin_bit_cast",
4296                           orig_type);
4297           }
4298       return true;
4299     }
4300   if (TYPE_PTRMEM_P (type))
4301     {
4302       if (!ctx->quiet)
4303           {
4304             if (type == orig_type)
4305               error_at (loc, "%qs is not a constant expression because %qT is "
4306                                  "a pointer to member type", "__builtin_bit_cast",
4307                           type);
4308             else
4309               error_at (loc, "%qs is not a constant expression because %qT "
4310                                  "contains a pointer to member type",
4311                           "__builtin_bit_cast", orig_type);
4312           }
4313       return true;
4314     }
4315   if (TYPE_VOLATILE (type))
4316     {
4317       if (!ctx->quiet)
4318           {
4319             if (type == orig_type)
4320               error_at (loc, "%qs is not a constant expression because %qT is "
4321                                  "volatile", "__builtin_bit_cast", type);
4322             else
4323               error_at (loc, "%qs is not a constant expression because %qT "
4324                                  "contains a volatile subobject",
4325                           "__builtin_bit_cast", orig_type);
4326           }
4327       return true;
4328     }
4329   if (TREE_CODE (type) == RECORD_TYPE)
4330     for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4331       if (TREE_CODE (field) == FIELD_DECL
4332             && check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type))
4333           return true;
4334   return false;
4335 }
4336 
4337 /* Helper function for cxx_eval_bit_cast.  For unsigned char or
4338    std::byte members of CONSTRUCTOR (recursively) if they contain
4339    some indeterminate bits (as set in MASK), remove the ctor elts,
4340    mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the
4341    bits in MASK.  */
4342 
4343 static void
clear_uchar_or_std_byte_in_mask(location_t loc,tree t,unsigned char * mask)4344 clear_uchar_or_std_byte_in_mask (location_t loc, tree t, unsigned char *mask)
4345 {
4346   if (TREE_CODE (t) != CONSTRUCTOR)
4347     return;
4348 
4349   unsigned i, j = 0;
4350   tree index, value;
4351   FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value)
4352     {
4353       tree type = TREE_TYPE (value);
4354       if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
4355             && DECL_BIT_FIELD_TYPE (index) != NULL_TREE)
4356           {
4357             if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index)))
4358               {
4359                 HOST_WIDE_INT fldsz = TYPE_PRECISION (TREE_TYPE (index));
4360                 gcc_assert (fldsz != 0);
4361                 HOST_WIDE_INT pos = int_byte_position (index);
4362                 HOST_WIDE_INT bpos
4363                     = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index));
4364                 bpos %= BITS_PER_UNIT;
4365                 HOST_WIDE_INT end
4366                     = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT;
4367                 gcc_assert (end == 1 || end == 2);
4368                 unsigned char *p = mask + pos;
4369                 unsigned char mask_save[2];
4370                 mask_save[0] = mask[pos];
4371                 mask_save[1] = end == 2 ? mask[pos + 1] : 0;
4372                 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
4373                     sorry_at (loc, "PDP11 bit-field handling unsupported"
4374                                      " in %qs", "__builtin_bit_cast");
4375                 else if (BYTES_BIG_ENDIAN)
4376                     {
4377                       /* Big endian.  */
4378                       if (bpos + fldsz <= BITS_PER_UNIT)
4379                         *p &= ~(((1 << fldsz) - 1)
4380                                   << (BITS_PER_UNIT - bpos - fldsz));
4381                       else
4382                         {
4383                           gcc_assert (bpos);
4384                           *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos);
4385                           p++;
4386                           fldsz -= BITS_PER_UNIT - bpos;
4387                           gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
4388                           *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz;
4389                         }
4390                     }
4391                 else
4392                     {
4393                       /* Little endian.  */
4394                       if (bpos + fldsz <= BITS_PER_UNIT)
4395                         *p &= ~(((1 << fldsz) - 1) << bpos);
4396                       else
4397                         {
4398                           gcc_assert (bpos);
4399                           *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos);
4400                           p++;
4401                           fldsz -= BITS_PER_UNIT - bpos;
4402                           gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
4403                           *p &= ~((1 << fldsz) - 1);
4404                         }
4405                     }
4406                 if (mask_save[0] != mask[pos]
4407                       || (end == 2 && mask_save[1] != mask[pos + 1]))
4408                     {
4409                       CONSTRUCTOR_NO_CLEARING (t) = 1;
4410                       continue;
4411                     }
4412               }
4413           }
4414       else if (is_byte_access_type_not_plain_char (type))
4415           {
4416             HOST_WIDE_INT pos;
4417             if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4418               pos = tree_to_shwi (index);
4419             else
4420               pos = int_byte_position (index);
4421             if (mask[pos])
4422               {
4423                 CONSTRUCTOR_NO_CLEARING (t) = 1;
4424                 mask[pos] = 0;
4425                 continue;
4426               }
4427           }
4428       if (TREE_CODE (value) == CONSTRUCTOR)
4429           {
4430             HOST_WIDE_INT pos;
4431             if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4432               pos = tree_to_shwi (index)
4433                       * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))));
4434             else
4435               pos = int_byte_position (index);
4436             clear_uchar_or_std_byte_in_mask (loc, value, mask + pos);
4437           }
4438       if (i != j)
4439           {
4440             CONSTRUCTOR_ELT (t, j)->index = index;
4441             CONSTRUCTOR_ELT (t, j)->value = value;
4442           }
4443       ++j;
4444     }
4445   if (CONSTRUCTOR_NELTS (t) != j)
4446     vec_safe_truncate (CONSTRUCTOR_ELTS (t), j);
4447 }
4448 
4449 /* Subroutine of cxx_eval_constant_expression.
4450    Attempt to evaluate a BIT_CAST_EXPR.  */
4451 
4452 static tree
cxx_eval_bit_cast(const constexpr_ctx * ctx,tree t,bool * non_constant_p,bool * overflow_p)4453 cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
4454                        bool *overflow_p)
4455 {
4456   if (check_bit_cast_type (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
4457                                  TREE_TYPE (t))
4458       || check_bit_cast_type (ctx, cp_expr_loc_or_loc (TREE_OPERAND (t, 0),
4459                                                                    EXPR_LOCATION (t)),
4460                                     TREE_TYPE (TREE_OPERAND (t, 0)),
4461                                     TREE_TYPE (TREE_OPERAND (t, 0))))
4462     {
4463       *non_constant_p = true;
4464       return t;
4465     }
4466 
4467   tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false,
4468                                                     non_constant_p, overflow_p);
4469   if (*non_constant_p)
4470     return t;
4471 
4472   location_t loc = EXPR_LOCATION (t);
4473   if (BITS_PER_UNIT != 8 || CHAR_BIT != 8)
4474     {
4475       if (!ctx->quiet)
4476           sorry_at (loc, "%qs cannot be constant evaluated on the target",
4477                            "__builtin_bit_cast");
4478       *non_constant_p = true;
4479       return t;
4480     }
4481 
4482   if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t))))
4483     {
4484       if (!ctx->quiet)
4485           sorry_at (loc, "%qs cannot be constant evaluated because the "
4486                            "type is too large", "__builtin_bit_cast");
4487       *non_constant_p = true;
4488       return t;
4489     }
4490 
4491   HOST_WIDE_INT len = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t)));
4492   if (len < 0 || (int) len != len)
4493     {
4494       if (!ctx->quiet)
4495           sorry_at (loc, "%qs cannot be constant evaluated because the "
4496                            "type is too large", "__builtin_bit_cast");
4497       *non_constant_p = true;
4498       return t;
4499     }
4500 
4501   unsigned char buf[64];
4502   unsigned char *ptr, *mask;
4503   size_t alen = (size_t) len * 2;
4504   if (alen <= sizeof (buf))
4505     ptr = buf;
4506   else
4507     ptr = XNEWVEC (unsigned char, alen);
4508   mask = ptr + (size_t) len;
4509   /* At the beginning consider everything indeterminate.  */
4510   memset (mask, ~0, (size_t) len);
4511 
4512   if (native_encode_initializer (op, ptr, len, 0, mask) != len)
4513     {
4514       if (!ctx->quiet)
4515           sorry_at (loc, "%qs cannot be constant evaluated because the "
4516                            "argument cannot be encoded", "__builtin_bit_cast");
4517       *non_constant_p = true;
4518       if (ptr != buf)
4519           XDELETE (ptr);
4520       return t;
4521     }
4522 
4523   tree r = NULL_TREE;
4524   if (can_native_interpret_type_p (TREE_TYPE (t)))
4525     {
4526       r = native_interpret_expr (TREE_TYPE (t), ptr, len);
4527       if (is_byte_access_type_not_plain_char (TREE_TYPE (t)))
4528           {
4529             gcc_assert (len == 1);
4530             if (mask[0])
4531               {
4532                 memset (mask, 0, len);
4533                 r = build_constructor (TREE_TYPE (r), NULL);
4534                 CONSTRUCTOR_NO_CLEARING (r) = 1;
4535               }
4536           }
4537     }
4538   else if (TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
4539     {
4540       r = native_interpret_aggregate (TREE_TYPE (t), ptr, 0, len);
4541       if (r != NULL_TREE)
4542           {
4543             clear_type_padding_in_mask (TREE_TYPE (t), mask);
4544             clear_uchar_or_std_byte_in_mask (loc, r, mask);
4545           }
4546     }
4547 
4548   if (r != NULL_TREE)
4549     {
4550       for (int i = 0; i < len; i++)
4551           if (mask[i])
4552             {
4553               if (!ctx->quiet)
4554                 error_at (loc, "%qs accessing uninitialized byte at offset %d",
4555                                    "__builtin_bit_cast", i);
4556               *non_constant_p = true;
4557               r = t;
4558               break;
4559             }
4560       if (ptr != buf)
4561           XDELETE (ptr);
4562       return r;
4563     }
4564 
4565   if (!ctx->quiet)
4566     sorry_at (loc, "%qs cannot be constant evaluated because the "
4567                        "argument cannot be interpreted", "__builtin_bit_cast");
4568   *non_constant_p = true;
4569   if (ptr != buf)
4570     XDELETE (ptr);
4571   return t;
4572 }
4573 
4574 /* Subroutine of cxx_eval_constant_expression.
4575    Evaluate a short-circuited logical expression T in the context
4576    of a given constexpr CALL.  BAILOUT_VALUE is the value for
4577    early return.  CONTINUE_VALUE is used here purely for
4578    sanity check purposes.  */
4579 
4580 static tree
cxx_eval_logical_expression(const constexpr_ctx * ctx,tree t,tree bailout_value,tree continue_value,bool * non_constant_p,bool * overflow_p)4581 cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
4582                              tree bailout_value, tree continue_value,
4583                                    bool *non_constant_p, bool *overflow_p)
4584 {
4585   tree r;
4586   tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
4587                                                      /*lval*/false, non_constant_p,
4588                                                      overflow_p);
4589   VERIFY_CONSTANT (lhs);
4590   if (tree_int_cst_equal (lhs, bailout_value))
4591     return lhs;
4592   gcc_assert (tree_int_cst_equal (lhs, continue_value));
4593   r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
4594                                             /*lval*/false, non_constant_p,
4595                                             overflow_p);
4596   VERIFY_CONSTANT (r);
4597   return r;
4598 }
4599 
4600 /* REF is a COMPONENT_REF designating a particular field.  V is a vector of
4601    CONSTRUCTOR elements to initialize (part of) an object containing that
4602    field.  Return a pointer to the constructor_elt corresponding to the
4603    initialization of the field.  */
4604 
4605 static constructor_elt *
base_field_constructor_elt(vec<constructor_elt,va_gc> * v,tree ref)4606 base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
4607 {
4608   tree aggr = TREE_OPERAND (ref, 0);
4609   tree field = TREE_OPERAND (ref, 1);
4610   HOST_WIDE_INT i;
4611   constructor_elt *ce;
4612 
4613   gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
4614 
4615   if (TREE_CODE (aggr) == COMPONENT_REF)
4616     {
4617       constructor_elt *base_ce
4618           = base_field_constructor_elt (v, aggr);
4619       v = CONSTRUCTOR_ELTS (base_ce->value);
4620     }
4621 
4622   for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
4623     if (ce->index == field)
4624       return ce;
4625 
4626   gcc_unreachable ();
4627   return NULL;
4628 }
4629 
4630 /* Some of the expressions fed to the constexpr mechanism are calls to
4631    constructors, which have type void.  In that case, return the type being
4632    initialized by the constructor.  */
4633 
4634 static tree
initialized_type(tree t)4635 initialized_type (tree t)
4636 {
4637   if (TYPE_P (t))
4638     return t;
4639   tree type = TREE_TYPE (t);
4640   if (TREE_CODE (t) == CALL_EXPR)
4641     {
4642       /* A constructor call has void type, so we need to look deeper.  */
4643       tree fn = get_function_named_in_call (t);
4644       if (fn && TREE_CODE (fn) == FUNCTION_DECL
4645             && DECL_CXX_CONSTRUCTOR_P (fn))
4646           type = DECL_CONTEXT (fn);
4647     }
4648   else if (TREE_CODE (t) == COMPOUND_EXPR)
4649     return initialized_type (TREE_OPERAND (t, 1));
4650   else if (TREE_CODE (t) == AGGR_INIT_EXPR)
4651     type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t));
4652   return cv_unqualified (type);
4653 }
4654 
4655 /* We're about to initialize element INDEX of an array or class from VALUE.
4656    Set up NEW_CTX appropriately by adjusting .object to refer to the
4657    subobject and creating a new CONSTRUCTOR if the element is itself
4658    a class or array.  */
4659 
4660 static void
init_subob_ctx(const constexpr_ctx * ctx,constexpr_ctx & new_ctx,tree index,tree & value)4661 init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
4662                  tree index, tree &value)
4663 {
4664   new_ctx = *ctx;
4665 
4666   if (index && TREE_CODE (index) != INTEGER_CST
4667       && TREE_CODE (index) != FIELD_DECL
4668       && TREE_CODE (index) != RANGE_EXPR)
4669     /* This won't have an element in the new CONSTRUCTOR.  */
4670     return;
4671 
4672   tree type = initialized_type (value);
4673   if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
4674     /* A non-aggregate member doesn't get its own CONSTRUCTOR.  */
4675     return;
4676   if (VECTOR_TYPE_P (type)
4677       && VECTOR_TYPE_P (TREE_TYPE (ctx->ctor))
4678       && index == NULL_TREE)
4679     /* A vector inside of a vector CONSTRUCTOR, e.g. when a larger
4680        vector is constructed from smaller vectors, doesn't get its own
4681        CONSTRUCTOR either.  */
4682     return;
4683 
4684   /* The sub-aggregate initializer might contain a placeholder;
4685      update object to refer to the subobject and ctor to refer to
4686      the (newly created) sub-initializer.  */
4687   if (ctx->object)
4688     {
4689       if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR)
4690           /* There's no well-defined subobject for this index.  */
4691           new_ctx.object = NULL_TREE;
4692       else
4693           new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
4694     }
4695   tree elt = build_constructor (type, NULL);
4696   CONSTRUCTOR_NO_CLEARING (elt) = true;
4697   new_ctx.ctor = elt;
4698 
4699   if (TREE_CODE (value) == TARGET_EXPR)
4700     /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR.  */
4701     value = TARGET_EXPR_INITIAL (value);
4702 }
4703 
4704 /* We're about to process an initializer for a class or array TYPE.  Make
4705    sure that CTX is set up appropriately.  */
4706 
4707 static void
verify_ctor_sanity(const constexpr_ctx * ctx,tree type)4708 verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
4709 {
4710   /* We don't bother building a ctor for an empty base subobject.  */
4711   if (is_empty_class (type))
4712     return;
4713 
4714   /* We're in the middle of an initializer that might involve placeholders;
4715      our caller should have created a CONSTRUCTOR for us to put the
4716      initializer into.  We will either return that constructor or T.  */
4717   gcc_assert (ctx->ctor);
4718   gcc_assert (same_type_ignoring_top_level_qualifiers_p
4719                 (type, TREE_TYPE (ctx->ctor)));
4720   /* We used to check that ctx->ctor was empty, but that isn't the case when
4721      the object is zero-initialized before calling the constructor.  */
4722   if (ctx->object)
4723     {
4724       tree otype = TREE_TYPE (ctx->object);
4725       gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype)
4726                       /* Handle flexible array members.  */
4727                       || (TREE_CODE (otype) == ARRAY_TYPE
4728                           && TYPE_DOMAIN (otype) == NULL_TREE
4729                           && TREE_CODE (type) == ARRAY_TYPE
4730                           && (same_type_ignoring_top_level_qualifiers_p
4731                                 (TREE_TYPE (type), TREE_TYPE (otype)))));
4732     }
4733   gcc_assert (!ctx->object || !DECL_P (ctx->object)
4734                 || *(ctx->global->values.get (ctx->object)) == ctx->ctor);
4735 }
4736 
4737 /* Subroutine of cxx_eval_constant_expression.
4738    The expression tree T denotes a C-style array or a C-style
4739    aggregate.  Reduce it to a constant expression.  */
4740 
4741 static tree
cxx_eval_bare_aggregate(const constexpr_ctx * ctx,tree t,bool lval,bool * non_constant_p,bool * overflow_p)4742 cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
4743                                bool lval,
4744                                bool *non_constant_p, bool *overflow_p)
4745 {
4746   vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
4747   bool changed = false;
4748   gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
4749   tree type = TREE_TYPE (t);
4750 
4751   constexpr_ctx new_ctx;
4752   if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type))
4753     {
4754       /* We don't really need the ctx->ctor business for a PMF or
4755            vector, but it's simpler to use the same code.  */
4756       new_ctx = *ctx;
4757       new_ctx.ctor = build_constructor (type, NULL);
4758       new_ctx.object = NULL_TREE;
4759       ctx = &new_ctx;
4760     };
4761   verify_ctor_sanity (ctx, type);
4762   vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
4763   vec_alloc (*p, vec_safe_length (v));
4764 
4765   if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t))
4766     CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1;
4767 
4768   unsigned i;
4769   tree index, value;
4770   bool constant_p = true;
4771   bool side_effects_p = false;
4772   FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
4773     {
4774       tree orig_value = value;
4775       /* Like in cxx_eval_store_expression, omit entries for empty fields.  */
4776       bool no_slot = TREE_CODE (type) == RECORD_TYPE && is_empty_field (index);
4777       init_subob_ctx (ctx, new_ctx, index, value);
4778       int pos_hint = -1;
4779       if (new_ctx.ctor != ctx->ctor && !no_slot)
4780           {
4781             /* If we built a new CONSTRUCTOR, attach it now so that other
4782                initializers can refer to it.  */
4783             constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index);
4784             cep->value = new_ctx.ctor;
4785             pos_hint = cep - (*p)->begin();
4786           }
4787       else if (TREE_CODE (type) == UNION_TYPE)
4788           /* Otherwise if we're constructing a non-aggregate union member, set
4789              the active union member now so that we can later detect and diagnose
4790              if its initializer attempts to activate another member.  */
4791           get_or_insert_ctor_field (ctx->ctor, index);
4792       tree elt = cxx_eval_constant_expression (&new_ctx, value,
4793                                                          lval,
4794                                                          non_constant_p, overflow_p);
4795       /* Don't VERIFY_CONSTANT here.  */
4796       if (ctx->quiet && *non_constant_p)
4797           break;
4798       if (elt != orig_value)
4799           changed = true;
4800 
4801       if (!TREE_CONSTANT (elt))
4802           constant_p = false;
4803       if (TREE_SIDE_EFFECTS (elt))
4804           side_effects_p = true;
4805       if (index && TREE_CODE (index) == COMPONENT_REF)
4806           {
4807             /* This is an initialization of a vfield inside a base
4808                subaggregate that we already initialized; push this
4809                initialization into the previous initialization.  */
4810             constructor_elt *inner = base_field_constructor_elt (*p, index);
4811             inner->value = elt;
4812             changed = true;
4813           }
4814       else if (index
4815                  && (TREE_CODE (index) == NOP_EXPR
4816                        || TREE_CODE (index) == POINTER_PLUS_EXPR))
4817           {
4818             /* This is an initializer for an empty base; now that we've
4819                checked that it's constant, we can ignore it.  */
4820             gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
4821             changed = true;
4822           }
4823       else if (no_slot)
4824           changed = true;
4825       else
4826           {
4827             if (TREE_CODE (type) == UNION_TYPE
4828                 && (*p)->last().index != index)
4829               /* The initializer erroneously changed the active union member that
4830                  we're initializing.  */
4831               gcc_assert (*non_constant_p);
4832             else
4833               {
4834                 /* The initializer might have mutated the underlying CONSTRUCTOR,
4835                      so recompute the location of the target constructer_elt.  */
4836                 constructor_elt *cep
4837                     = get_or_insert_ctor_field (ctx->ctor, index, pos_hint);
4838                 cep->value = elt;
4839               }
4840 
4841             /* Adding or replacing an element might change the ctor's flags.  */
4842             TREE_CONSTANT (ctx->ctor) = constant_p;
4843             TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
4844           }
4845     }
4846   if (*non_constant_p || !changed)
4847     return t;
4848   t = ctx->ctor;
4849   /* We're done building this CONSTRUCTOR, so now we can interpret an
4850      element without an explicit initializer as value-initialized.  */
4851   CONSTRUCTOR_NO_CLEARING (t) = false;
4852   TREE_CONSTANT (t) = constant_p;
4853   TREE_SIDE_EFFECTS (t) = side_effects_p;
4854   if (VECTOR_TYPE_P (type))
4855     t = fold (t);
4856   return t;
4857 }
4858 
4859 /* Subroutine of cxx_eval_constant_expression.
4860    The expression tree T is a VEC_INIT_EXPR which denotes the desired
4861    initialization of a non-static data member of array type.  Reduce it to a
4862    CONSTRUCTOR.
4863 
4864    Note that apart from value-initialization (when VALUE_INIT is true),
4865    this is only intended to support value-initialization and the
4866    initializations done by defaulted constructors for classes with
4867    non-static data members of array type.  In this case, VEC_INIT_EXPR_INIT
4868    will either be NULL_TREE for the default constructor, or a COMPONENT_REF
4869    for the copy/move constructor.  */
4870 
4871 static tree
cxx_eval_vec_init_1(const constexpr_ctx * ctx,tree atype,tree init,bool value_init,bool lval,bool * non_constant_p,bool * overflow_p)4872 cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
4873                          bool value_init, bool lval,
4874                          bool *non_constant_p, bool *overflow_p)
4875 {
4876   tree elttype = TREE_TYPE (atype);
4877   verify_ctor_sanity (ctx, atype);
4878   vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
4879   bool pre_init = false;
4880   unsigned HOST_WIDE_INT i;
4881   tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
4882 
4883   if (init && TREE_CODE (init) == CONSTRUCTOR)
4884     return cxx_eval_bare_aggregate (ctx, init, lval,
4885                                             non_constant_p, overflow_p);
4886 
4887   /* For the default constructor, build up a call to the default
4888      constructor of the element type.  We only need to handle class types
4889      here, as for a constructor to be constexpr, all members must be
4890      initialized, which for a defaulted default constructor means they must
4891      be of a class type with a constexpr default constructor.  */
4892   if (TREE_CODE (elttype) == ARRAY_TYPE)
4893     /* We only do this at the lowest level.  */;
4894   else if (value_init)
4895     {
4896       init = build_value_init (elttype, complain);
4897       pre_init = true;
4898     }
4899   else if (!init)
4900     {
4901       releasing_vec argvec;
4902       init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
4903                                                   &argvec, elttype, LOOKUP_NORMAL,
4904                                                   complain);
4905       init = build_aggr_init_expr (elttype, init);
4906       pre_init = true;
4907     }
4908 
4909   bool zeroed_out = false;
4910   if (!CONSTRUCTOR_NO_CLEARING (ctx->ctor))
4911     {
4912       /* We're initializing an array object that had been zero-initialized
4913            earlier.  Truncate ctx->ctor, and propagate its zeroed state by
4914            clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
4915            initializers we append to it.  */
4916       gcc_checking_assert (initializer_zerop (ctx->ctor));
4917       zeroed_out = true;
4918       vec_safe_truncate (*p, 0);
4919     }
4920 
4921   tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p,
4922                                                     overflow_p);
4923   unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts);
4924   for (i = 0; i < max; ++i)
4925     {
4926       tree idx = build_int_cst (size_type_node, i);
4927       tree eltinit;
4928       bool reuse = false;
4929       constexpr_ctx new_ctx;
4930       init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
4931       if (new_ctx.ctor != ctx->ctor)
4932           {
4933             if (zeroed_out)
4934               CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false;
4935             CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
4936           }
4937       if (TREE_CODE (elttype) == ARRAY_TYPE)
4938           {
4939             /* A multidimensional array; recurse.  */
4940             if (value_init || init == NULL_TREE)
4941               {
4942                 eltinit = NULL_TREE;
4943                 reuse = i == 0;
4944               }
4945             else
4946               eltinit = cp_build_array_ref (input_location, init, idx, complain);
4947             eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init,
4948                                                    lval,
4949                                                    non_constant_p, overflow_p);
4950           }
4951       else if (pre_init)
4952           {
4953             /* Initializing an element using value or default initialization
4954                we just pre-built above.  */
4955             if (init == void_node)
4956               /* Trivial default-init, don't do anything to the CONSTRUCTOR.  */
4957               return ctx->ctor;
4958             eltinit = cxx_eval_constant_expression (&new_ctx, init, lval,
4959                                                               non_constant_p, overflow_p);
4960             reuse = i == 0;
4961           }
4962       else
4963           {
4964             /* Copying an element.  */
4965             gcc_assert (same_type_ignoring_top_level_qualifiers_p
4966                           (atype, TREE_TYPE (init)));
4967             eltinit = cp_build_array_ref (input_location, init, idx, complain);
4968             if (!lvalue_p (init))
4969               eltinit = move (eltinit);
4970             eltinit = force_rvalue (eltinit, complain);
4971             eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
4972                                                               non_constant_p, overflow_p);
4973           }
4974       if (*non_constant_p)
4975           break;
4976       if (new_ctx.ctor != ctx->ctor)
4977           {
4978             /* We appended this element above; update the value.  */
4979             gcc_assert ((*p)->last().index == idx);
4980             (*p)->last().value = eltinit;
4981           }
4982       else
4983           CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
4984       /* Reuse the result of cxx_eval_constant_expression call
4985            from the first iteration to all others if it is a constant
4986            initializer that doesn't require relocations.  */
4987       if (reuse
4988             && max > 1
4989             && (eltinit == NULL_TREE
4990                 || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit))
4991                       == null_pointer_node)))
4992           {
4993             if (new_ctx.ctor != ctx->ctor)
4994               eltinit = new_ctx.ctor;
4995             tree range = build2 (RANGE_EXPR, size_type_node,
4996                                      build_int_cst (size_type_node, 1),
4997                                      build_int_cst (size_type_node, max - 1));
4998             CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit));
4999             break;
5000           }
5001       else if (i == 0)
5002           vec_safe_reserve (*p, max);
5003     }
5004 
5005   if (!*non_constant_p)
5006     {
5007       init = ctx->ctor;
5008       CONSTRUCTOR_NO_CLEARING (init) = false;
5009     }
5010   return init;
5011 }
5012 
5013 static tree
cxx_eval_vec_init(const constexpr_ctx * ctx,tree t,bool lval,bool * non_constant_p,bool * overflow_p)5014 cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
5015                        bool lval,
5016                        bool *non_constant_p, bool *overflow_p)
5017 {
5018   tree atype = TREE_TYPE (t);
5019   tree init = VEC_INIT_EXPR_INIT (t);
5020   bool value_init = VEC_INIT_EXPR_VALUE_INIT (t);
5021   if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init))
5022     ;
5023   else if (CONSTRUCTOR_NELTS (init) == 0
5024              && !CP_AGGREGATE_TYPE_P (strip_array_types (atype)))
5025     {
5026       /* Handle {} as value-init.  */
5027       init = NULL_TREE;
5028       value_init = true;
5029     }
5030   else
5031     {
5032       /* This is a more complicated case, like needing to loop over trailing
5033            elements; call build_vec_init and evaluate the result.  */
5034       tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
5035       constexpr_ctx new_ctx = *ctx;
5036       if (!ctx->object)
5037           {
5038             /* We want to have an initialization target for an VEC_INIT_EXPR.
5039                If we don't already have one in CTX, use the VEC_INIT_EXPR_SLOT.  */
5040             new_ctx.object = VEC_INIT_EXPR_SLOT (t);
5041             tree ctor = new_ctx.ctor = build_constructor (atype, NULL);
5042             CONSTRUCTOR_NO_CLEARING (ctor) = true;
5043             ctx->global->values.put (new_ctx.object, ctor);
5044             ctx = &new_ctx;
5045           }
5046       init = expand_vec_init_expr (ctx->object, t, complain);
5047       return cxx_eval_constant_expression (ctx, init, lval, non_constant_p,
5048                                                      overflow_p);
5049     }
5050   tree r = cxx_eval_vec_init_1 (ctx, atype, init, value_init,
5051                                         lval, non_constant_p, overflow_p);
5052   if (*non_constant_p)
5053     return t;
5054   else
5055     return r;
5056 }
5057 
5058 /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
5059    where the desired type is an array of unknown bounds because the variable
5060    has had its bounds deduced since the wrapping expression was created.  */
5061 
5062 static bool
same_type_ignoring_tlq_and_bounds_p(tree type1,tree type2)5063 same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2)
5064 {
5065   while (TREE_CODE (type1) == ARRAY_TYPE
5066            && TREE_CODE (type2) == ARRAY_TYPE
5067            && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2)))
5068     {
5069       type1 = TREE_TYPE (type1);
5070       type2 = TREE_TYPE (type2);
5071     }
5072   return same_type_ignoring_top_level_qualifiers_p (type1, type2);
5073 }
5074 
5075 /* Try to determine the currently active union member for an expression
5076    with UNION_TYPE.  If it can be determined, return the FIELD_DECL,
5077    otherwise return NULL_TREE.  */
5078 
5079 static tree
cxx_union_active_member(const constexpr_ctx * ctx,tree t)5080 cxx_union_active_member (const constexpr_ctx *ctx, tree t)
5081 {
5082   constexpr_ctx new_ctx = *ctx;
5083   new_ctx.quiet = true;
5084   bool non_constant_p = false, overflow_p = false;
5085   tree ctor = cxx_eval_constant_expression (&new_ctx, t, false,
5086                                                       &non_constant_p,
5087                                                       &overflow_p);
5088   if (TREE_CODE (ctor) == CONSTRUCTOR
5089       && CONSTRUCTOR_NELTS (ctor) == 1
5090       && CONSTRUCTOR_ELT (ctor, 0)->index
5091       && TREE_CODE (CONSTRUCTOR_ELT (ctor, 0)->index) == FIELD_DECL)
5092     return CONSTRUCTOR_ELT (ctor, 0)->index;
5093   return NULL_TREE;
5094 }
5095 
5096 /* Helper function for cxx_fold_indirect_ref_1, called recursively.  */
5097 
5098 static tree
cxx_fold_indirect_ref_1(const constexpr_ctx * ctx,location_t loc,tree type,tree op,unsigned HOST_WIDE_INT off,bool * empty_base)5099 cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type,
5100                                tree op, unsigned HOST_WIDE_INT off, bool *empty_base)
5101 {
5102   tree optype = TREE_TYPE (op);
5103   unsigned HOST_WIDE_INT const_nunits;
5104   if (off == 0 && similar_type_p (optype, type))
5105     return op;
5106   else if (TREE_CODE (optype) == COMPLEX_TYPE
5107              && similar_type_p (type, TREE_TYPE (optype)))
5108     {
5109       /* *(foo *)&complexfoo => __real__ complexfoo */
5110       if (off == 0)
5111           return build1_loc (loc, REALPART_EXPR, type, op);
5112       /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
5113       else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off)
5114           return build1_loc (loc, IMAGPART_EXPR, type, op);
5115     }
5116   /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
5117   else if (VECTOR_TYPE_P (optype)
5118              && similar_type_p (type, TREE_TYPE (optype))
5119              && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits))
5120     {
5121       unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type));
5122       unsigned HOST_WIDE_INT max_offset = part_width * const_nunits;
5123       if (off < max_offset && off % part_width == 0)
5124           {
5125             tree index = bitsize_int (off * BITS_PER_UNIT);
5126             return build3_loc (loc, BIT_FIELD_REF, type, op,
5127                                    TYPE_SIZE (type), index);
5128           }
5129     }
5130   /* ((foo *)&fooarray)[x] => fooarray[x] */
5131   else if (TREE_CODE (optype) == ARRAY_TYPE
5132              && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype)))
5133              && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype))))
5134     {
5135       tree type_domain = TYPE_DOMAIN (optype);
5136       tree min_val = size_zero_node;
5137       if (type_domain && TYPE_MIN_VALUE (type_domain))
5138           min_val = TYPE_MIN_VALUE (type_domain);
5139       unsigned HOST_WIDE_INT el_sz
5140           = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype)));
5141       unsigned HOST_WIDE_INT idx = off / el_sz;
5142       unsigned HOST_WIDE_INT rem = off % el_sz;
5143       if (tree_fits_uhwi_p (min_val))
5144           {
5145             tree index = size_int (idx + tree_to_uhwi (min_val));
5146             op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index,
5147                                  NULL_TREE, NULL_TREE);
5148             return cxx_fold_indirect_ref_1 (ctx, loc, type, op, rem,
5149                                                     empty_base);
5150           }
5151     }
5152   /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
5153   else if (TREE_CODE (optype) == RECORD_TYPE
5154              || TREE_CODE (optype) == UNION_TYPE)
5155     {
5156       if (TREE_CODE (optype) == UNION_TYPE)
5157           /* For unions prefer the currently active member.  */
5158           if (tree field = cxx_union_active_member (ctx, op))
5159             {
5160               unsigned HOST_WIDE_INT el_sz
5161                 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
5162               if (off < el_sz)
5163                 {
5164                     tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
5165                                            op, field, NULL_TREE);
5166                     if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
5167                                                                       off, empty_base))
5168                       return ret;
5169                 }
5170             }
5171       for (tree field = TYPE_FIELDS (optype);
5172              field; field = DECL_CHAIN (field))
5173           if (TREE_CODE (field) == FIELD_DECL
5174               && TREE_TYPE (field) != error_mark_node
5175               && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field))))
5176             {
5177               tree pos = byte_position (field);
5178               if (!tree_fits_uhwi_p (pos))
5179                 continue;
5180               unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos);
5181               unsigned HOST_WIDE_INT el_sz
5182                 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
5183               if (upos <= off && off < upos + el_sz)
5184                 {
5185                     tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
5186                                            op, field, NULL_TREE);
5187                     if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
5188                                                                       off - upos,
5189                                                                       empty_base))
5190                       return ret;
5191                 }
5192             }
5193       /* Also handle conversion to an empty base class, which
5194            is represented with a NOP_EXPR.  */
5195       if (is_empty_class (type)
5196             && CLASS_TYPE_P (optype)
5197             && DERIVED_FROM_P (type, optype))
5198           {
5199             *empty_base = true;
5200             return op;
5201           }
5202     }
5203 
5204   return NULL_TREE;
5205 }
5206 
5207 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
5208    match.  We want to be less strict for simple *& folding; if we have a
5209    non-const temporary that we access through a const pointer, that should
5210    work.  We handle this here rather than change fold_indirect_ref_1
5211    because we're dealing with things like ADDR_EXPR of INTEGER_CST which
5212    don't really make sense outside of constant expression evaluation.  Also
5213    we want to allow folding to COMPONENT_REF, which could cause trouble
5214    with TBAA in fold_indirect_ref_1.  */
5215 
5216 static tree
cxx_fold_indirect_ref(const constexpr_ctx * ctx,location_t loc,tree type,tree op0,bool * empty_base)5217 cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type,
5218                            tree op0, bool *empty_base)
5219 {
5220   tree sub = op0;
5221   tree subtype;
5222   poly_uint64 const_op01;
5223 
5224   /* STRIP_NOPS, but stop if REINTERPRET_CAST_P.  */
5225   while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR
5226            || TREE_CODE (sub) == VIEW_CONVERT_EXPR)
5227     {
5228       if (TREE_CODE (sub) == NOP_EXPR
5229             && REINTERPRET_CAST_P (sub))
5230           return NULL_TREE;
5231       sub = TREE_OPERAND (sub, 0);
5232     }
5233 
5234   subtype = TREE_TYPE (sub);
5235   if (!INDIRECT_TYPE_P (subtype))
5236     return NULL_TREE;
5237 
5238   /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing
5239      the innermost component into the offset until it would make the
5240      offset positive, so that cxx_fold_indirect_ref_1 can identify
5241      more folding opportunities.  */
5242   auto canonicalize_obj_off = [] (tree& obj, tree& off) {
5243     while (TREE_CODE (obj) == COMPONENT_REF
5244              && (tree_int_cst_sign_bit (off) || integer_zerop (off)))
5245       {
5246           tree field = TREE_OPERAND (obj, 1);
5247           tree pos = byte_position (field);
5248           if (integer_zerop (off) && integer_nonzerop (pos))
5249             /* If the offset is already 0, keep going as long as the
5250                component is at position 0.  */
5251             break;
5252           off = int_const_binop (PLUS_EXPR, off, pos);
5253           obj = TREE_OPERAND (obj, 0);
5254       }
5255   };
5256 
5257   if (TREE_CODE (sub) == ADDR_EXPR)
5258     {
5259       tree op = TREE_OPERAND (sub, 0);
5260       tree optype = TREE_TYPE (op);
5261 
5262       /* *&CONST_DECL -> to the value of the const decl.  */
5263       if (TREE_CODE (op) == CONST_DECL)
5264           return DECL_INITIAL (op);
5265       /* *&p => p;  make sure to handle *&"str"[cst] here.  */
5266       if (similar_type_p (optype, type))
5267           {
5268             tree fop = fold_read_from_constant_string (op);
5269             if (fop)
5270               return fop;
5271             else
5272               return op;
5273           }
5274       else
5275           {
5276             tree off = integer_zero_node;
5277             canonicalize_obj_off (op, off);
5278             gcc_assert (integer_zerop (off));
5279             return cxx_fold_indirect_ref_1 (ctx, loc, type, op, 0, empty_base);
5280           }
5281     }
5282   else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
5283              && tree_fits_uhwi_p (TREE_OPERAND (sub, 1)))
5284     {
5285       tree op00 = TREE_OPERAND (sub, 0);
5286       tree off = TREE_OPERAND (sub, 1);
5287 
5288       STRIP_NOPS (op00);
5289       if (TREE_CODE (op00) == ADDR_EXPR)
5290           {
5291             tree obj = TREE_OPERAND (op00, 0);
5292             canonicalize_obj_off (obj, off);
5293             return cxx_fold_indirect_ref_1 (ctx, loc, type, obj,
5294                                                     tree_to_uhwi (off), empty_base);
5295           }
5296     }
5297   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
5298   else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
5299              && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype))))
5300     {
5301       tree type_domain;
5302       tree min_val = size_zero_node;
5303       tree newsub
5304           = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL);
5305       if (newsub)
5306           sub = newsub;
5307       else
5308           sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
5309       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
5310       if (type_domain && TYPE_MIN_VALUE (type_domain))
5311           min_val = TYPE_MIN_VALUE (type_domain);
5312       return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
5313                                NULL_TREE);
5314     }
5315 
5316   return NULL_TREE;
5317 }
5318 
5319 static tree
cxx_eval_indirect_ref(const constexpr_ctx * ctx,tree t,bool lval,bool * non_constant_p,bool * overflow_p)5320 cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
5321                            bool lval,
5322                            bool *non_constant_p, bool *overflow_p)
5323 {
5324   tree orig_op0 = TREE_OPERAND (t, 0);
5325   bool empty_base = false;
5326 
5327   /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
5328      operand is an integer-zero.  Otherwise reject the MEM_REF for now.  */
5329 
5330   if (TREE_CODE (t) == MEM_REF
5331       && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1))))
5332     {
5333       gcc_assert (ctx->quiet);
5334       *non_constant_p = true;
5335       return t;
5336     }
5337 
5338   /* First try to simplify it directly.  */
5339   tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
5340                                           orig_op0, &empty_base);
5341   if (!r)
5342     {
5343       /* If that didn't work, evaluate the operand first.  */
5344       tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
5345                                                          /*lval*/false, non_constant_p,
5346                                                          overflow_p);
5347       /* Don't VERIFY_CONSTANT here.  */
5348       if (*non_constant_p)
5349           return t;
5350 
5351       if (!lval && integer_zerop (op0))
5352           {
5353             if (!ctx->quiet)
5354               error ("dereferencing a null pointer");
5355             *non_constant_p = true;
5356             return t;
5357           }
5358 
5359       r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0,
5360                                          &empty_base);
5361       if (r == NULL_TREE)
5362           {
5363             /* We couldn't fold to a constant value.  Make sure it's not
5364                something we should have been able to fold.  */
5365             tree sub = op0;
5366             STRIP_NOPS (sub);
5367             if (TREE_CODE (sub) == ADDR_EXPR)
5368               {
5369                 gcc_assert (!similar_type_p
5370                                 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
5371                 /* DR 1188 says we don't have to deal with this.  */
5372                 if (!ctx->quiet)
5373                     error_at (cp_expr_loc_or_input_loc (t),
5374                                 "accessing value of %qE through a %qT glvalue in a "
5375                                 "constant expression", build_fold_indirect_ref (sub),
5376                                 TREE_TYPE (t));
5377                 *non_constant_p = true;
5378                 return t;
5379               }
5380 
5381             if (lval && op0 != orig_op0)
5382               return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
5383             if (!lval)
5384               VERIFY_CONSTANT (t);
5385             return t;
5386           }
5387     }
5388 
5389   r = cxx_eval_constant_expression (ctx, r,
5390                                             lval, non_constant_p, overflow_p);
5391   if (*non_constant_p)
5392     return t;
5393 
5394   /* If we're pulling out the value of an empty base, just return an empty
5395      CONSTRUCTOR.  */
5396   if (empty_base && !lval)
5397     {
5398       r = build_constructor (TREE_TYPE (t), NULL);
5399       TREE_CONSTANT (r) = true;
5400     }
5401 
5402   return r;
5403 }
5404 
5405 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
5406    Shared between potential_constant_expression and
5407    cxx_eval_constant_expression.  */
5408 
5409 static void
non_const_var_error(location_t loc,tree r)5410 non_const_var_error (location_t loc, tree r)
5411 {
5412   auto_diagnostic_group d;
5413   tree type = TREE_TYPE (r);
5414   if (DECL_NAME (r) == heap_uninit_identifier
5415       || DECL_NAME (r) == heap_identifier
5416       || DECL_NAME (r) == heap_vec_uninit_identifier
5417       || DECL_NAME (r) == heap_vec_identifier)
5418     {
5419       error_at (loc, "the content of uninitialized storage is not usable "
5420                     "in a constant expression");
5421       inform (DECL_SOURCE_LOCATION (r), "allocated here");
5422       return;
5423     }
5424   if (DECL_NAME (r) == heap_deleted_identifier)
5425     {
5426       error_at (loc, "use of allocated storage after deallocation in a "
5427                     "constant expression");
5428       inform (DECL_SOURCE_LOCATION (r), "allocated here");
5429       return;
5430     }
5431   error_at (loc, "the value of %qD is not usable in a constant "
5432               "expression", r);
5433   /* Avoid error cascade.  */
5434   if (DECL_INITIAL (r) == error_mark_node)
5435     return;
5436   if (DECL_DECLARED_CONSTEXPR_P (r))
5437     inform (DECL_SOURCE_LOCATION (r),
5438               "%qD used in its own initializer", r);
5439   else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5440     {
5441       if (!CP_TYPE_CONST_P (type))
5442           inform (DECL_SOURCE_LOCATION (r),
5443                     "%q#D is not const", r);
5444       else if (CP_TYPE_VOLATILE_P (type))
5445           inform (DECL_SOURCE_LOCATION (r),
5446                     "%q#D is volatile", r);
5447       else if (!DECL_INITIAL (r)
5448                  || !TREE_CONSTANT (DECL_INITIAL (r))
5449                  || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r))
5450           inform (DECL_SOURCE_LOCATION (r),
5451                     "%qD was not initialized with a constant "
5452                     "expression", r);
5453       else
5454           gcc_unreachable ();
5455     }
5456   else if (TYPE_REF_P (type))
5457     inform (DECL_SOURCE_LOCATION (r),
5458               "%qD was not initialized with a constant "
5459               "expression", r);
5460   else
5461     {
5462       if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
5463           inform (DECL_SOURCE_LOCATION (r),
5464                     "%qD was not declared %<constexpr%>", r);
5465       else
5466           inform (DECL_SOURCE_LOCATION (r),
5467                     "%qD does not have integral or enumeration type",
5468                     r);
5469     }
5470 }
5471 
5472 /* Subroutine of cxx_eval_constant_expression.
5473    Like cxx_eval_unary_expression, except for trinary expressions.  */
5474 
5475 static tree
cxx_eval_trinary_expression(const constexpr_ctx * ctx,tree t,bool lval,bool * non_constant_p,bool * overflow_p)5476 cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
5477                                    bool lval,
5478                                    bool *non_constant_p, bool *overflow_p)
5479 {
5480   int i;
5481   tree args[3];
5482   tree val;
5483 
5484   for (i = 0; i < 3; i++)
5485     {
5486       args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
5487                                                         lval,
5488                                                         non_constant_p, overflow_p);
5489       VERIFY_CONSTANT (args[i]);
5490     }
5491 
5492   val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
5493                                 args[0], args[1], args[2]);
5494   if (val == NULL_TREE)
5495     return t;
5496   VERIFY_CONSTANT (val);
5497   return val;
5498 }
5499 
5500 /* True if T was declared in a function declared to be constexpr, and
5501    therefore potentially constant in C++14.  */
5502 
5503 bool
var_in_constexpr_fn(tree t)5504 var_in_constexpr_fn (tree t)
5505 {
5506   tree ctx = DECL_CONTEXT (t);
5507   return (ctx && TREE_CODE (ctx) == FUNCTION_DECL
5508             && DECL_DECLARED_CONSTEXPR_P (ctx));
5509 }
5510 
5511 /* True if a function might be constexpr: either a function that was
5512    declared constexpr, or a C++17 lambda op().  */
5513 
5514 bool
maybe_constexpr_fn(tree t)5515 maybe_constexpr_fn (tree t)
5516 {
5517   return (DECL_DECLARED_CONSTEXPR_P (t)
5518             || (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t))
5519             || (flag_implicit_constexpr
5520                 && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t))));
5521 }
5522 
5523 /* True if T was declared in a function that might be constexpr: either a
5524    function that was declared constexpr, or a C++17 lambda op().  */
5525 
5526 bool
var_in_maybe_constexpr_fn(tree t)5527 var_in_maybe_constexpr_fn (tree t)
5528 {
5529   return (DECL_FUNCTION_SCOPE_P (t)
5530             && maybe_constexpr_fn (DECL_CONTEXT (t)));
5531 }
5532 
5533 /* We're assigning INIT to TARGET.  In do_build_copy_constructor and
5534    build_over_call we implement trivial copy of a class with tail padding using
5535    assignment of character arrays, which is valid in normal code, but not in
5536    constexpr evaluation.  We don't need to worry about clobbering tail padding
5537    in constexpr evaluation, so strip the type punning.  */
5538 
5539 static void
maybe_simplify_trivial_copy(tree & target,tree & init)5540 maybe_simplify_trivial_copy (tree &target, tree &init)
5541 {
5542   if (TREE_CODE (target) == MEM_REF
5543       && TREE_CODE (init) == MEM_REF
5544       && TREE_TYPE (target) == TREE_TYPE (init)
5545       && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE
5546       && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node)
5547     {
5548       target = build_fold_indirect_ref (TREE_OPERAND (target, 0));
5549       init = build_fold_indirect_ref (TREE_OPERAND (init, 0));
5550     }
5551 }
5552 
5553 /* Returns true if REF, which is a COMPONENT_REF, has any fields
5554    of constant type.  This does not check for 'mutable', so the
5555    caller is expected to be mindful of that.  */
5556 
5557 static bool
cref_has_const_field(tree ref)5558 cref_has_const_field (tree ref)
5559 {
5560   while (TREE_CODE (ref) == COMPONENT_REF)
5561     {
5562       if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
5563        return true;
5564       ref = TREE_OPERAND (ref, 0);
5565     }
5566   return false;
5567 }
5568 
5569 /* Return true if we are modifying something that is const during constant
5570    expression evaluation.  CODE is the code of the statement, OBJ is the
5571    object in question, MUTABLE_P is true if one of the subobjects were
5572    declared mutable.  */
5573 
5574 static bool
modifying_const_object_p(tree_code code,tree obj,bool mutable_p)5575 modifying_const_object_p (tree_code code, tree obj, bool mutable_p)
5576 {
5577   /* If this is initialization, there's no problem.  */
5578   if (code != MODIFY_EXPR)
5579     return false;
5580 
5581   /* [basic.type.qualifier] "A const object is an object of type
5582      const T or a non-mutable subobject of a const object."  */
5583   if (mutable_p)
5584     return false;
5585 
5586   if (TREE_READONLY (obj))
5587     return true;
5588 
5589   if (CP_TYPE_CONST_P (TREE_TYPE (obj)))
5590     {
5591       /* Although a COMPONENT_REF may have a const type, we should
5592            only consider it modifying a const object when any of the
5593            field components is const.  This can happen when using
5594            constructs such as const_cast<const T &>(m), making something
5595            const even though it wasn't declared const.  */
5596       if (TREE_CODE (obj) == COMPONENT_REF)
5597           return cref_has_const_field (obj);
5598       else
5599           return true;
5600     }
5601 
5602   return false;
5603 }
5604 
5605 /* Evaluate an INIT_EXPR or MODIFY_EXPR.  */
5606 
5607 static tree
cxx_eval_store_expression(const constexpr_ctx * ctx,tree t,bool lval,bool * non_constant_p,bool * overflow_p)5608 cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
5609                                  bool lval,
5610                                  bool *non_constant_p, bool *overflow_p)
5611 {
5612   constexpr_ctx new_ctx = *ctx;
5613 
5614   tree init = TREE_OPERAND (t, 1);
5615   if (TREE_CLOBBER_P (init))
5616     /* Just ignore clobbers.  */
5617     return void_node;
5618 
5619   /* First we figure out where we're storing to.  */
5620   tree target = TREE_OPERAND (t, 0);
5621 
5622   maybe_simplify_trivial_copy (target, init);
5623 
5624   tree type = TREE_TYPE (target);
5625   bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR;
5626   if (preeval)
5627     {
5628       /* Evaluate the value to be stored without knowing what object it will be
5629            stored in, so that any side-effects happen first.  */
5630       if (!SCALAR_TYPE_P (type))
5631           new_ctx.ctor = new_ctx.object = NULL_TREE;
5632       init = cxx_eval_constant_expression (&new_ctx, init, false,
5633                                                      non_constant_p, overflow_p);
5634       if (*non_constant_p)
5635           return t;
5636     }
5637 
5638   bool evaluated = false;
5639   if (lval)
5640     {
5641       /* If we want to return a reference to the target, we need to evaluate it
5642            as a whole; otherwise, only evaluate the innermost piece to avoid
5643            building up unnecessary *_REFs.  */
5644       target = cxx_eval_constant_expression (ctx, target, true,
5645                                                        non_constant_p, overflow_p);
5646       evaluated = true;
5647       if (*non_constant_p)
5648           return t;
5649     }
5650 
5651   /* Find the underlying variable.  */
5652   releasing_vec refs;
5653   tree object = NULL_TREE;
5654   /* If we're modifying a const object, save it.  */
5655   tree const_object_being_modified = NULL_TREE;
5656   bool mutable_p = false;
5657   for (tree probe = target; object == NULL_TREE; )
5658     {
5659       switch (TREE_CODE (probe))
5660           {
5661           case BIT_FIELD_REF:
5662           case COMPONENT_REF:
5663           case ARRAY_REF:
5664             {
5665               tree ob = TREE_OPERAND (probe, 0);
5666               tree elt = TREE_OPERAND (probe, 1);
5667               if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt))
5668                 mutable_p = true;
5669               if (TREE_CODE (probe) == ARRAY_REF)
5670                 {
5671                     elt = eval_and_check_array_index (ctx, probe, false,
5672                                                               non_constant_p, overflow_p);
5673                     if (*non_constant_p)
5674                       return t;
5675                 }
5676               /* We don't check modifying_const_object_p for ARRAY_REFs.  Given
5677                  "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
5678                  the array isn't const.  Instead, check "a" in the next iteration;
5679                  that will detect modifying "const int a[10]".  */
5680               else if (evaluated
5681                          && modifying_const_object_p (TREE_CODE (t), probe,
5682                                                               mutable_p)
5683                          && const_object_being_modified == NULL_TREE)
5684                 const_object_being_modified = probe;
5685               vec_safe_push (refs, elt);
5686               vec_safe_push (refs, TREE_TYPE (probe));
5687               probe = ob;
5688             }
5689             break;
5690 
5691           default:
5692             if (evaluated)
5693               object = probe;
5694             else
5695               {
5696                 probe = cxx_eval_constant_expression (ctx, probe, true,
5697                                                                 non_constant_p, overflow_p);
5698                 evaluated = true;
5699                 if (*non_constant_p)
5700                     return t;
5701               }
5702             break;
5703           }
5704     }
5705 
5706   if (modifying_const_object_p (TREE_CODE (t), object, mutable_p)
5707       && const_object_being_modified == NULL_TREE)
5708     const_object_being_modified = object;
5709 
5710   /* And then find/build up our initializer for the path to the subobject
5711      we're initializing.  */
5712   tree *valp;
5713   if (DECL_P (object))
5714     valp = ctx->global->values.get (object);
5715   else
5716     valp = NULL;
5717   if (!valp)
5718     {
5719       /* A constant-expression cannot modify objects from outside the
5720            constant-expression.  */
5721       if (!ctx->quiet)
5722           error ("modification of %qE is not a constant expression", object);
5723       *non_constant_p = true;
5724       return t;
5725     }
5726   type = TREE_TYPE (object);
5727   bool no_zero_init = true;
5728 
5729   releasing_vec ctors, indexes;
5730   auto_vec<int> index_pos_hints;
5731   bool activated_union_member_p = false;
5732   while (!refs->is_empty ())
5733     {
5734       if (*valp == NULL_TREE)
5735           {
5736             *valp = build_constructor (type, NULL);
5737             CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
5738           }
5739       else if (STRIP_ANY_LOCATION_WRAPPER (*valp),
5740                  TREE_CODE (*valp) == STRING_CST)
5741           {
5742             /* An array was initialized with a string constant, and now
5743                we're writing into one of its elements.  Explode the
5744                single initialization into a set of element
5745                initializations.  */
5746             gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
5747 
5748             tree string = *valp;
5749             tree elt_type = TREE_TYPE (type);
5750             unsigned chars_per_elt = (TYPE_PRECISION (elt_type)
5751                                             / TYPE_PRECISION (char_type_node));
5752             unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt;
5753             tree ary_ctor = build_constructor (type, NULL);
5754 
5755             vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts);
5756             for (unsigned ix = 0; ix != num_elts; ix++)
5757               {
5758                 constructor_elt elt =
5759                     {
5760                       build_int_cst (size_type_node, ix),
5761                       extract_string_elt (string, chars_per_elt, ix)
5762                     };
5763                 CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt);
5764               }
5765 
5766             *valp = ary_ctor;
5767           }
5768 
5769       /* If the value of object is already zero-initialized, any new ctors for
5770            subobjects will also be zero-initialized.  */
5771       no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp);
5772 
5773       enum tree_code code = TREE_CODE (type);
5774       type = refs->pop();
5775       tree index = refs->pop();
5776 
5777       if (code == RECORD_TYPE && is_empty_field (index))
5778           /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they
5779              have no data and might have an offset lower than previously declared
5780              fields, which confuses the middle-end.  The code below will notice
5781              that we don't have a CONSTRUCTOR for our inner target and just
5782              return init.  */
5783           break;
5784 
5785       if (code == UNION_TYPE && CONSTRUCTOR_NELTS (*valp)
5786             && CONSTRUCTOR_ELT (*valp, 0)->index != index)
5787           {
5788             if (cxx_dialect < cxx20)
5789               {
5790                 if (!ctx->quiet)
5791                     error_at (cp_expr_loc_or_input_loc (t),
5792                                 "change of the active member of a union "
5793                                 "from %qD to %qD",
5794                                 CONSTRUCTOR_ELT (*valp, 0)->index,
5795                                 index);
5796                 *non_constant_p = true;
5797               }
5798             else if (TREE_CODE (t) == MODIFY_EXPR
5799                        && CONSTRUCTOR_NO_CLEARING (*valp))
5800               {
5801                 /* Diagnose changing the active union member while the union
5802                      is in the process of being initialized.  */
5803                 if (!ctx->quiet)
5804                     error_at (cp_expr_loc_or_input_loc (t),
5805                                 "change of the active member of a union "
5806                                 "from %qD to %qD during initialization",
5807                                 CONSTRUCTOR_ELT (*valp, 0)->index,
5808                                 index);
5809                 *non_constant_p = true;
5810               }
5811             no_zero_init = true;
5812           }
5813 
5814       vec_safe_push (ctors, *valp);
5815       vec_safe_push (indexes, index);
5816 
5817       constructor_elt *cep
5818           = get_or_insert_ctor_field (*valp, index);
5819       index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin());
5820 
5821       if (code == UNION_TYPE)
5822           activated_union_member_p = true;
5823 
5824       valp = &cep->value;
5825     }
5826 
5827   /* Detect modifying a constant object in constexpr evaluation.
5828      We have found a const object that is being modified.  Figure out
5829      if we need to issue an error.  Consider
5830 
5831      struct A {
5832        int n;
5833        constexpr A() : n(1) { n = 2; } // #1
5834      };
5835      struct B {
5836        const A a;
5837        constexpr B() { a.n = 3; } // #2
5838      };
5839     constexpr B b{};
5840 
5841     #1 is OK, since we're modifying an object under construction, but
5842     #2 is wrong, since "a" is const and has been fully constructed.
5843     To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
5844     which means that the object is read-only.  For the example above, the
5845     *ctors stack at the point of #2 will look like:
5846 
5847       ctors[0] = {.a={.n=2}}  TREE_READONLY = 0
5848       ctors[1] = {.n=2}       TREE_READONLY = 1
5849 
5850     and we're modifying "b.a", so we search the stack and see if the
5851     constructor for "b.a" has already run.  */
5852   if (const_object_being_modified)
5853     {
5854       bool fail = false;
5855       tree const_objtype
5856           = strip_array_types (TREE_TYPE (const_object_being_modified));
5857       if (!CLASS_TYPE_P (const_objtype))
5858           fail = true;
5859       else
5860           {
5861             /* [class.ctor]p5 "A constructor can be invoked for a const,
5862                volatile, or const volatile object.  const and volatile
5863                semantics are not applied on an object under construction.
5864                They come into effect when the constructor for the most
5865                derived object ends."  */
5866             for (tree elt : *ctors)
5867               if (same_type_ignoring_top_level_qualifiers_p
5868                     (TREE_TYPE (const_object_being_modified), TREE_TYPE (elt)))
5869                 {
5870                     fail = TREE_READONLY (elt);
5871                     break;
5872                 }
5873           }
5874       if (fail)
5875           {
5876             if (!ctx->quiet)
5877               modifying_const_object_error (t, const_object_being_modified);
5878             *non_constant_p = true;
5879             return t;
5880           }
5881     }
5882 
5883   if (!preeval)
5884     {
5885       /* We're handling an INIT_EXPR of class type, so the value of the
5886            initializer can depend on the object it's initializing.  */
5887 
5888       /* Create a new CONSTRUCTOR in case evaluation of the initializer
5889            wants to modify it.  */
5890       if (*valp == NULL_TREE)
5891           {
5892             *valp = build_constructor (type, NULL);
5893             CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
5894           }
5895       new_ctx.ctor = *valp;
5896       new_ctx.object = target;
5897       /* Avoid temporary materialization when initializing from a TARGET_EXPR.
5898            We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
5899            expansion of those trees uses ctx instead.  */
5900       if (TREE_CODE (init) == TARGET_EXPR)
5901           if (tree tinit = TARGET_EXPR_INITIAL (init))
5902             init = tinit;
5903       init = cxx_eval_constant_expression (&new_ctx, init, false,
5904                                                      non_constant_p, overflow_p);
5905       /* The hash table might have moved since the get earlier, and the
5906            initializer might have mutated the underlying CONSTRUCTORs, so we must
5907            recompute VALP. */
5908       valp = ctx->global->values.get (object);
5909       for (unsigned i = 0; i < vec_safe_length (indexes); i++)
5910           {
5911             constructor_elt *cep
5912               = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]);
5913             valp = &cep->value;
5914           }
5915     }
5916 
5917   /* Don't share a CONSTRUCTOR that might be changed later.  */
5918   init = unshare_constructor (init);
5919 
5920   if (*valp && TREE_CODE (*valp) == CONSTRUCTOR
5921       && TREE_CODE (init) == CONSTRUCTOR)
5922     {
5923       /* An outer ctx->ctor might be pointing to *valp, so replace
5924            its contents.  */
5925       if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init),
5926                                                                   TREE_TYPE (*valp)))
5927           {
5928             /* For initialization of an empty base, the original target will be
5929              *(base*)this, evaluation of which resolves to the object
5930              argument, which has the derived type rather than the base type.  In
5931              this situation, just evaluate the initializer and return, since
5932              there's no actual data to store.  */
5933             gcc_assert (is_empty_class (TREE_TYPE (init)));
5934             return lval ? target : init;
5935           }
5936       CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
5937       TREE_CONSTANT (*valp) = TREE_CONSTANT (init);
5938       TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init);
5939       CONSTRUCTOR_NO_CLEARING (*valp)
5940           = CONSTRUCTOR_NO_CLEARING (init);
5941     }
5942   else if (TREE_CODE (init) == CONSTRUCTOR
5943              && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init),
5944                                                                         type))
5945     {
5946       /* See above on initialization of empty bases.  */
5947       gcc_assert (is_empty_class (TREE_TYPE (init)) && !lval);
5948       if (!*valp)
5949           {
5950             /* But do make sure we have something in *valp.  */
5951             *valp = build_constructor (type, nullptr);
5952             CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
5953           }
5954       return init;
5955     }
5956   else
5957     *valp = init;
5958 
5959   /* After initialization, 'const' semantics apply to the value of the
5960      object.  Make a note of this fact by marking the CONSTRUCTOR
5961      TREE_READONLY.  */
5962   if (TREE_CODE (t) == INIT_EXPR
5963       && TREE_CODE (*valp) == CONSTRUCTOR
5964       && TYPE_READONLY (type))
5965     {
5966       if (INDIRECT_REF_P (target)
5967             && (is_this_parameter
5968                 (tree_strip_nop_conversions (TREE_OPERAND (target, 0)))))
5969           /* We've just initialized '*this' (perhaps via the target
5970              constructor of a delegating constructor).  Leave it up to the
5971              caller that set 'this' to set TREE_READONLY appropriately.  */
5972           gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
5973                                    (TREE_TYPE (target), type));
5974       else
5975           TREE_READONLY (*valp) = true;
5976     }
5977 
5978   /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
5979      CONSTRUCTORs, if any.  */
5980   bool c = TREE_CONSTANT (init);
5981   bool s = TREE_SIDE_EFFECTS (init);
5982   if (!c || s || activated_union_member_p)
5983     for (tree elt : *ctors)
5984       {
5985           if (!c)
5986             TREE_CONSTANT (elt) = false;
5987           if (s)
5988             TREE_SIDE_EFFECTS (elt) = true;
5989           /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
5990              this union.  */
5991           if (TREE_CODE (TREE_TYPE (elt)) == UNION_TYPE)
5992             CONSTRUCTOR_NO_CLEARING (elt) = false;
5993       }
5994 
5995   if (*non_constant_p)
5996     return t;
5997   else if (lval)
5998     return target;
5999   else
6000     return init;
6001 }
6002 
6003 /* Evaluate a ++ or -- expression.  */
6004 
6005 static tree
cxx_eval_increment_expression(const constexpr_ctx * ctx,tree t,bool lval,bool * non_constant_p,bool * overflow_p)6006 cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
6007                                     bool lval,
6008                                     bool *non_constant_p, bool *overflow_p)
6009 {
6010   enum tree_code code = TREE_CODE (t);
6011   tree type = TREE_TYPE (t);
6012   tree op = TREE_OPERAND (t, 0);
6013   tree offset = TREE_OPERAND (t, 1);
6014   gcc_assert (TREE_CONSTANT (offset));
6015 
6016   /* OFFSET is constant, but perhaps not constant enough.  We need to
6017      e.g. bash FLOAT_EXPRs to REAL_CSTs.  */
6018   offset = fold_simple (offset);
6019 
6020   /* The operand as an lvalue.  */
6021   op = cxx_eval_constant_expression (ctx, op, true,
6022                                              non_constant_p, overflow_p);
6023 
6024   /* The operand as an rvalue.  */
6025   tree val
6026     = cxx_eval_constant_expression (ctx, op, false,
6027                                             non_constant_p, overflow_p);
6028   /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
6029      a local array in a constexpr function.  */
6030   bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val));
6031   if (!ptr)
6032     VERIFY_CONSTANT (val);
6033 
6034   /* The modified value.  */
6035   bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
6036   tree mod;
6037   if (INDIRECT_TYPE_P (type))
6038     {
6039       /* The middle end requires pointers to use POINTER_PLUS_EXPR.  */
6040       offset = convert_to_ptrofftype (offset);
6041       if (!inc)
6042           offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
6043       mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
6044     }
6045   else if (c_promoting_integer_type_p (type)
6046              && !TYPE_UNSIGNED (type)
6047              && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6048     {
6049       offset = fold_convert (integer_type_node, offset);
6050       mod = fold_convert (integer_type_node, val);
6051       tree t = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, integer_type_node,
6052                                   mod, offset);
6053       mod = fold_convert (type, t);
6054       if (TREE_OVERFLOW_P (mod) && !TREE_OVERFLOW_P (t))
6055           TREE_OVERFLOW (mod) = false;
6056     }
6057   else
6058     mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
6059   if (!ptr)
6060     VERIFY_CONSTANT (mod);
6061 
6062   /* Storing the modified value.  */
6063   tree store = build2_loc (cp_expr_loc_or_loc (t, input_location),
6064                                  MODIFY_EXPR, type, op, mod);
6065   mod = cxx_eval_constant_expression (ctx, store, lval,
6066                                               non_constant_p, overflow_p);
6067   ggc_free (store);
6068   if (*non_constant_p)
6069     return t;
6070 
6071   /* And the value of the expression.  */
6072   if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
6073     /* Prefix ops are lvalues, but the caller might want an rvalue;
6074        lval has already been taken into account in the store above.  */
6075     return mod;
6076   else
6077     /* Postfix ops are rvalues.  */
6078     return val;
6079 }
6080 
6081 /* Predicates for the meaning of *jump_target.  */
6082 
6083 static bool
returns(tree * jump_target)6084 returns (tree *jump_target)
6085 {
6086   return *jump_target
6087     && (TREE_CODE (*jump_target) == RETURN_EXPR
6088           || (TREE_CODE (*jump_target) == LABEL_DECL
6089               && LABEL_DECL_CDTOR (*jump_target)));
6090 }
6091 
6092 static bool
breaks(tree * jump_target)6093 breaks (tree *jump_target)
6094 {
6095   return *jump_target
6096     && ((TREE_CODE (*jump_target) == LABEL_DECL
6097            && LABEL_DECL_BREAK (*jump_target))
6098           || TREE_CODE (*jump_target) == BREAK_STMT
6099           || TREE_CODE (*jump_target) == EXIT_EXPR);
6100 }
6101 
6102 static bool
continues(tree * jump_target)6103 continues (tree *jump_target)
6104 {
6105   return *jump_target
6106     && ((TREE_CODE (*jump_target) == LABEL_DECL
6107            && LABEL_DECL_CONTINUE (*jump_target))
6108           || TREE_CODE (*jump_target) == CONTINUE_STMT);
6109 
6110 }
6111 
6112 static bool
switches(tree * jump_target)6113 switches (tree *jump_target)
6114 {
6115   return *jump_target
6116     && TREE_CODE (*jump_target) == INTEGER_CST;
6117 }
6118 
6119 /* Subroutine of cxx_eval_statement_list.  Determine whether the statement
6120    STMT matches *jump_target.  If we're looking for a case label and we see
6121    the default label, note it in ctx->css_state.  */
6122 
6123 static bool
label_matches(const constexpr_ctx * ctx,tree * jump_target,tree stmt)6124 label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt)
6125 {
6126   switch (TREE_CODE (*jump_target))
6127     {
6128     case LABEL_DECL:
6129       if (TREE_CODE (stmt) == LABEL_EXPR
6130             && LABEL_EXPR_LABEL (stmt) == *jump_target)
6131           return true;
6132       break;
6133 
6134     case INTEGER_CST:
6135       if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
6136           {
6137             gcc_assert (ctx->css_state != NULL);
6138             if (!CASE_LOW (stmt))
6139               {
6140                 /* default: should appear just once in a SWITCH_EXPR
6141                      body (excluding nested SWITCH_EXPR).  */
6142                 gcc_assert (*ctx->css_state != css_default_seen);
6143                 /* When evaluating SWITCH_EXPR body for the second time,
6144                      return true for the default: label.  */
6145                 if (*ctx->css_state == css_default_processing)
6146                     return true;
6147                 *ctx->css_state = css_default_seen;
6148               }
6149             else if (CASE_HIGH (stmt))
6150               {
6151                 if (tree_int_cst_le (CASE_LOW (stmt), *jump_target)
6152                       && tree_int_cst_le (*jump_target, CASE_HIGH (stmt)))
6153                     return true;
6154               }
6155             else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
6156               return true;
6157           }
6158       break;
6159 
6160     case BREAK_STMT:
6161     case CONTINUE_STMT:
6162       /* These two are handled directly in cxx_eval_loop_expr by testing
6163            breaks (jump_target) or continues (jump_target).  */
6164       break;
6165 
6166     default:
6167       gcc_unreachable ();
6168     }
6169   return false;
6170 }
6171 
6172 /* Evaluate a STATEMENT_LIST for side-effects.  Handles various jump
6173    semantics, for switch, break, continue, and return.  */
6174 
6175 static tree
cxx_eval_statement_list(const constexpr_ctx * ctx,tree t,bool * non_constant_p,bool * overflow_p,tree * jump_target)6176 cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
6177                                bool *non_constant_p, bool *overflow_p,
6178                                tree *jump_target)
6179 {
6180   tree local_target;
6181   /* In a statement-expression we want to return the last value.
6182      For empty statement expression return void_node.  */
6183   tree r = void_node;
6184   if (!jump_target)
6185     {
6186       local_target = NULL_TREE;
6187       jump_target = &local_target;
6188     }
6189   for (tree stmt : tsi_range (t))
6190     {
6191       /* We've found a continue, so skip everything until we reach
6192            the label its jumping to.  */
6193       if (continues (jump_target))
6194           {
6195             if (label_matches (ctx, jump_target, stmt))
6196               /* Found it.  */
6197               *jump_target = NULL_TREE;
6198             else
6199               continue;
6200           }
6201       if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
6202           continue;
6203       r = cxx_eval_constant_expression (ctx, stmt, false,
6204                                                   non_constant_p, overflow_p,
6205                                                   jump_target);
6206       if (*non_constant_p)
6207           break;
6208       if (returns (jump_target) || breaks (jump_target))
6209           break;
6210     }
6211   if (*jump_target && jump_target == &local_target)
6212     {
6213       /* We aren't communicating the jump to our caller, so give up.  We don't
6214            need to support evaluation of jumps out of statement-exprs.  */
6215       if (!ctx->quiet)
6216           error_at (cp_expr_loc_or_input_loc (r),
6217                       "statement is not a constant expression");
6218       *non_constant_p = true;
6219     }
6220   return r;
6221 }
6222 
6223 /* Evaluate a LOOP_EXPR for side-effects.  Handles break and return
6224    semantics; continue semantics are covered by cxx_eval_statement_list.  */
6225 
6226 static tree
cxx_eval_loop_expr(const constexpr_ctx * ctx,tree t,bool * non_constant_p,bool * overflow_p,tree * jump_target)6227 cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
6228                         bool *non_constant_p, bool *overflow_p,
6229                         tree *jump_target)
6230 {
6231   constexpr_ctx new_ctx = *ctx;
6232   tree local_target;
6233   if (!jump_target)
6234     {
6235       local_target = NULL_TREE;
6236       jump_target = &local_target;
6237     }
6238 
6239   tree body, cond = NULL_TREE, expr = NULL_TREE;
6240   int count = 0;
6241   switch (TREE_CODE (t))
6242     {
6243     case LOOP_EXPR:
6244       body = LOOP_EXPR_BODY (t);
6245       break;
6246     case DO_STMT:
6247       body = DO_BODY (t);
6248       cond = DO_COND (t);
6249       break;
6250     case WHILE_STMT:
6251       body = WHILE_BODY (t);
6252       cond = WHILE_COND (t);
6253       count = -1;
6254       break;
6255     case FOR_STMT:
6256       if (FOR_INIT_STMT (t))
6257           cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), /*lval*/false,
6258                                               non_constant_p, overflow_p, jump_target);
6259       if (*non_constant_p)
6260           return NULL_TREE;
6261       body = FOR_BODY (t);
6262       cond = FOR_COND (t);
6263       expr = FOR_EXPR (t);
6264       count = -1;
6265       break;
6266     default:
6267       gcc_unreachable ();
6268     }
6269   auto_vec<tree, 10> save_exprs;
6270   new_ctx.save_exprs = &save_exprs;
6271   do
6272     {
6273       if (count != -1)
6274           {
6275             if (body)
6276               cxx_eval_constant_expression (&new_ctx, body, /*lval*/false,
6277                                                     non_constant_p, overflow_p,
6278                                                     jump_target);
6279             if (breaks (jump_target))
6280               {
6281                 *jump_target = NULL_TREE;
6282                 break;
6283               }
6284 
6285             if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target))
6286               *jump_target = NULL_TREE;
6287 
6288             if (expr)
6289               cxx_eval_constant_expression (&new_ctx, expr, /*lval*/false,
6290                                                     non_constant_p, overflow_p,
6291                                                     jump_target);
6292           }
6293 
6294       if (cond)
6295           {
6296             tree res
6297               = cxx_eval_constant_expression (&new_ctx, cond, /*lval*/false,
6298                                                       non_constant_p, overflow_p,
6299                                                       jump_target);
6300             if (res)
6301               {
6302                 if (verify_constant (res, ctx->quiet, non_constant_p,
6303                                            overflow_p))
6304                     break;
6305                 if (integer_zerop (res))
6306                     break;
6307               }
6308             else
6309               gcc_assert (*jump_target);
6310           }
6311 
6312       /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs.  */
6313       for (tree save_expr : save_exprs)
6314           ctx->global->values.remove (save_expr);
6315       save_exprs.truncate (0);
6316 
6317       if (++count >= constexpr_loop_limit)
6318           {
6319             if (!ctx->quiet)
6320               error_at (cp_expr_loc_or_input_loc (t),
6321                           "%<constexpr%> loop iteration count exceeds limit of %d "
6322                           "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
6323                           constexpr_loop_limit);
6324             *non_constant_p = true;
6325             break;
6326           }
6327     }
6328   while (!returns (jump_target)
6329            && !breaks (jump_target)
6330            && !continues (jump_target)
6331            && (!switches (jump_target) || count == 0)
6332            && !*non_constant_p);
6333 
6334   /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs.  */
6335   for (tree save_expr : save_exprs)
6336     ctx->global->values.remove (save_expr);
6337 
6338   return NULL_TREE;
6339 }
6340 
6341 /* Evaluate a SWITCH_EXPR for side-effects.  Handles switch and break jump
6342    semantics.  */
6343 
6344 static tree
cxx_eval_switch_expr(const constexpr_ctx * ctx,tree t,bool * non_constant_p,bool * overflow_p,tree * jump_target)6345 cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
6346                           bool *non_constant_p, bool *overflow_p,
6347                           tree *jump_target)
6348 {
6349   tree cond
6350     = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t);
6351   cond = cxx_eval_constant_expression (ctx, cond, false,
6352                                                non_constant_p, overflow_p);
6353   VERIFY_CONSTANT (cond);
6354   *jump_target = cond;
6355 
6356   tree body
6357     = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t);
6358   constexpr_ctx new_ctx = *ctx;
6359   constexpr_switch_state css = css_default_not_seen;
6360   new_ctx.css_state = &css;
6361   cxx_eval_constant_expression (&new_ctx, body, false,
6362                                         non_constant_p, overflow_p, jump_target);
6363   if (switches (jump_target) && css == css_default_seen)
6364     {
6365       /* If the SWITCH_EXPR body has default: label, process it once again,
6366            this time instructing label_matches to return true for default:
6367            label on switches (jump_target).  */
6368       css = css_default_processing;
6369       cxx_eval_constant_expression (&new_ctx, body, false,
6370                                             non_constant_p, overflow_p, jump_target);
6371     }
6372   if (breaks (jump_target) || switches (jump_target))
6373     *jump_target = NULL_TREE;
6374   return NULL_TREE;
6375 }
6376 
6377 /* Find the object of TYPE under initialization in CTX.  */
6378 
6379 static tree
lookup_placeholder(const constexpr_ctx * ctx,bool lval,tree type)6380 lookup_placeholder (const constexpr_ctx *ctx, bool lval, tree type)
6381 {
6382   if (!ctx)
6383     return NULL_TREE;
6384 
6385   /* Prefer the outermost matching object, but don't cross
6386      CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors.  */
6387   if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor))
6388     if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type))
6389       return outer_ob;
6390 
6391   /* We could use ctx->object unconditionally, but using ctx->ctor when we
6392      can is a minor optimization.  */
6393   if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type))
6394     return ctx->ctor;
6395 
6396   if (!ctx->object)
6397     return NULL_TREE;
6398 
6399   /* Since an object cannot have a field of its own type, we can search outward
6400      from ctx->object to find the unique containing object of TYPE.  */
6401   tree ob = ctx->object;
6402   while (ob)
6403     {
6404       if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type))
6405           break;
6406       if (handled_component_p (ob))
6407           ob = TREE_OPERAND (ob, 0);
6408       else
6409           ob = NULL_TREE;
6410     }
6411 
6412   return ob;
6413 }
6414 
6415 /* Complain about an attempt to evaluate inline assembly.  */
6416 
6417 static void
inline_asm_in_constexpr_error(location_t loc)6418 inline_asm_in_constexpr_error (location_t loc)
6419 {
6420   auto_diagnostic_group d;
6421   error_at (loc, "inline assembly is not a constant expression");
6422   inform (loc, "only unevaluated inline assembly is allowed in a "
6423             "%<constexpr%> function in C++20");
6424 }
6425 
6426 /* We're getting the constant value of DECL in a manifestly constant-evaluated
6427    context; maybe complain about that.  */
6428 
6429 static void
maybe_warn_about_constant_value(location_t loc,tree decl)6430 maybe_warn_about_constant_value (location_t loc, tree decl)
6431 {
6432   static bool explained = false;
6433   if (cxx_dialect >= cxx17
6434       && warn_interference_size
6435       && !OPTION_SET_P (param_destruct_interfere_size)
6436       && DECL_CONTEXT (decl) == std_node
6437       && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size")
6438       && (LOCATION_FILE (input_location) != main_input_filename
6439             || module_exporting_p ())
6440       && warning_at (loc, OPT_Winterference_size, "use of %qD", decl)
6441       && !explained)
6442     {
6443       explained = true;
6444       inform (loc, "its value can vary between compiler versions or "
6445                 "with different %<-mtune%> or %<-mcpu%> flags");
6446       inform (loc, "if this use is part of a public ABI, change it to "
6447                 "instead use a constant variable you define");
6448       inform (loc, "the default value for the current CPU tuning "
6449                 "is %d bytes", param_destruct_interfere_size);
6450       inform (loc, "you can stabilize this value with %<--param "
6451                 "hardware_destructive_interference_size=%d%>, or disable "
6452                 "this warning with %<-Wno-interference-size%>",
6453                 param_destruct_interfere_size);
6454     }
6455 }
6456 
6457 /* For element type ELT_TYPE, return the appropriate type of the heap object
6458    containing such element(s).  COOKIE_SIZE is NULL or the size of cookie
6459    in bytes.  If COOKIE_SIZE is NULL, return array type
6460    ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return
6461    struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
6462    where N is is computed such that the size of the struct fits into FULL_SIZE.
6463    If ARG_SIZE is non-NULL, it is the first argument to the new operator.
6464    It should be passed if ELT_TYPE is zero sized type in which case FULL_SIZE
6465    will be also 0 and so it is not possible to determine the actual array
6466    size.  CTX, NON_CONSTANT_P and OVERFLOW_P are used during constant
6467    expression evaluation of subexpressions of ARG_SIZE.  */
6468 
6469 static tree
build_new_constexpr_heap_type(const constexpr_ctx * ctx,tree elt_type,tree cookie_size,tree full_size,tree arg_size,bool * non_constant_p,bool * overflow_p)6470 build_new_constexpr_heap_type (const constexpr_ctx *ctx, tree elt_type,
6471                                      tree cookie_size, tree full_size, tree arg_size,
6472                                      bool *non_constant_p, bool *overflow_p)
6473 {
6474   gcc_assert (cookie_size == NULL_TREE || tree_fits_uhwi_p (cookie_size));
6475   gcc_assert (tree_fits_uhwi_p (full_size));
6476   unsigned HOST_WIDE_INT csz = cookie_size ? tree_to_uhwi (cookie_size) : 0;
6477   if (arg_size)
6478     {
6479       STRIP_NOPS (arg_size);
6480       if (cookie_size)
6481           {
6482             if (TREE_CODE (arg_size) != PLUS_EXPR)
6483               arg_size = NULL_TREE;
6484             else if (TREE_CODE (TREE_OPERAND (arg_size, 0)) == INTEGER_CST
6485                        && tree_int_cst_equal (cookie_size,
6486                                                     TREE_OPERAND (arg_size, 0)))
6487               {
6488                 arg_size = TREE_OPERAND (arg_size, 1);
6489                 STRIP_NOPS (arg_size);
6490               }
6491             else if (TREE_CODE (TREE_OPERAND (arg_size, 1)) == INTEGER_CST
6492                        && tree_int_cst_equal (cookie_size,
6493                                                     TREE_OPERAND (arg_size, 1)))
6494               {
6495                 arg_size = TREE_OPERAND (arg_size, 0);
6496                 STRIP_NOPS (arg_size);
6497               }
6498             else
6499               arg_size = NULL_TREE;
6500           }
6501       if (arg_size && TREE_CODE (arg_size) == MULT_EXPR)
6502           {
6503             tree op0 = TREE_OPERAND (arg_size, 0);
6504             tree op1 = TREE_OPERAND (arg_size, 1);
6505             if (integer_zerop (op0))
6506               arg_size
6507                 = cxx_eval_constant_expression (ctx, op1, false, non_constant_p,
6508                                                         overflow_p);
6509             else if (integer_zerop (op1))
6510               arg_size
6511                 = cxx_eval_constant_expression (ctx, op0, false, non_constant_p,
6512                                                         overflow_p);
6513             else
6514               arg_size = NULL_TREE;
6515           }
6516       else
6517           arg_size = NULL_TREE;
6518     }
6519 
6520   unsigned HOST_WIDE_INT fsz = tree_to_uhwi (arg_size ? arg_size : full_size);
6521   if (!arg_size)
6522     {
6523       unsigned HOST_WIDE_INT esz = int_size_in_bytes (elt_type);
6524       gcc_assert (fsz >= csz);
6525       fsz -= csz;
6526       if (esz)
6527           fsz /= esz;
6528     }
6529   tree itype2 = build_index_type (size_int (fsz - 1));
6530   if (!cookie_size)
6531     return build_cplus_array_type (elt_type, itype2);
6532   return build_new_constexpr_heap_type (elt_type, cookie_size, itype2);
6533 }
6534 
6535 /* Attempt to reduce the expression T to a constant value.
6536    On failure, issue diagnostic and return error_mark_node.  */
6537 /* FIXME unify with c_fully_fold */
6538 /* FIXME overflow_p is too global */
6539 
6540 static tree
cxx_eval_constant_expression(const constexpr_ctx * ctx,tree t,bool lval,bool * non_constant_p,bool * overflow_p,tree * jump_target)6541 cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
6542                                     bool lval,
6543                                     bool *non_constant_p, bool *overflow_p,
6544                                     tree *jump_target /* = NULL */)
6545 {
6546   if (jump_target && *jump_target)
6547     {
6548       /* If we are jumping, ignore all statements/expressions except those
6549            that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies.  */
6550       switch (TREE_CODE (t))
6551           {
6552           case BIND_EXPR:
6553           case STATEMENT_LIST:
6554           case LOOP_EXPR:
6555           case COND_EXPR:
6556           case IF_STMT:
6557           case DO_STMT:
6558           case WHILE_STMT:
6559           case FOR_STMT:
6560             break;
6561           case LABEL_EXPR:
6562           case CASE_LABEL_EXPR:
6563             if (label_matches (ctx, jump_target, t))
6564               /* Found it.  */
6565               *jump_target = NULL_TREE;
6566             return NULL_TREE;
6567           default:
6568             return NULL_TREE;
6569           }
6570     }
6571   if (error_operand_p (t))
6572     {
6573       *non_constant_p = true;
6574       return t;
6575     }
6576 
6577   location_t loc = cp_expr_loc_or_input_loc (t);
6578 
6579   STRIP_ANY_LOCATION_WRAPPER (t);
6580 
6581   if (CONSTANT_CLASS_P (t))
6582     {
6583       if (TREE_OVERFLOW (t))
6584           {
6585             if (!ctx->quiet)
6586               permerror (input_location, "overflow in constant expression");
6587             if (!flag_permissive || ctx->quiet)
6588               *overflow_p = true;
6589           }
6590 
6591       if (TREE_CODE (t) == INTEGER_CST
6592             && TYPE_PTR_P (TREE_TYPE (t))
6593             /* INTEGER_CST with pointer-to-method type is only used
6594                for a virtual method in a pointer to member function.
6595                Don't reject those.  */
6596             && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE
6597             && !integer_zerop (t))
6598           {
6599             if (!ctx->quiet)
6600               error ("value %qE of type %qT is not a constant expression",
6601                        t, TREE_TYPE (t));
6602             *non_constant_p = true;
6603           }
6604 
6605       return t;
6606     }
6607 
6608   /* Avoid excessively long constexpr evaluations.  */
6609   if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit)
6610     {
6611       if (!ctx->quiet)
6612           error_at (loc,
6613                       "%<constexpr%> evaluation operation count exceeds limit of "
6614                       "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
6615                       constexpr_ops_limit);
6616       ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT);
6617       *non_constant_p = true;
6618       return t;
6619     }
6620 
6621   constexpr_ctx new_ctx;
6622   tree r = t;
6623 
6624   tree_code tcode = TREE_CODE (t);
6625   switch (tcode)
6626     {
6627     case RESULT_DECL:
6628       if (lval)
6629           return t;
6630       /* We ask for an rvalue for the RESULT_DECL when indirecting
6631            through an invisible reference, or in named return value
6632            optimization.  */
6633       if (tree *p = ctx->global->values.get (t))
6634           return *p;
6635       else
6636           {
6637             if (!ctx->quiet)
6638               error ("%qE is not a constant expression", t);
6639             *non_constant_p = true;
6640           }
6641       break;
6642 
6643     case VAR_DECL:
6644       if (DECL_HAS_VALUE_EXPR_P (t))
6645           {
6646             if (is_normal_capture_proxy (t)
6647                 && current_function_decl == DECL_CONTEXT (t))
6648               {
6649                 /* Function parms aren't constexpr within the function
6650                      definition, so don't try to look at the closure.  But if the
6651                      captured variable is constant, try to evaluate it directly. */
6652                 r = DECL_CAPTURED_VARIABLE (t);
6653                 tree type = TREE_TYPE (t);
6654                 if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r)))
6655                     {
6656                       /* Adjust r to match the reference-ness of t.  */
6657                       if (TYPE_REF_P (type))
6658                         r = build_address (r);
6659                       else
6660                         r = convert_from_reference (r);
6661                     }
6662               }
6663             else
6664               r = DECL_VALUE_EXPR (t);
6665             return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
6666                                                          overflow_p);
6667           }
6668       /* fall through */
6669     case CONST_DECL:
6670       /* We used to not check lval for CONST_DECL, but darwin.cc uses
6671            CONST_DECL for aggregate constants.  */
6672       if (lval)
6673           return t;
6674       else if (t == ctx->object)
6675           return ctx->ctor;
6676       if (VAR_P (t))
6677           if (tree *p = ctx->global->values.get (t))
6678             if (*p != NULL_TREE)
6679               {
6680                 r = *p;
6681                 break;
6682               }
6683       if (ctx->manifestly_const_eval)
6684           maybe_warn_about_constant_value (loc, t);
6685       if (COMPLETE_TYPE_P (TREE_TYPE (t))
6686             && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
6687           {
6688             /* If the class is empty, we aren't actually loading anything.  */
6689             r = build_constructor (TREE_TYPE (t), NULL);
6690             TREE_CONSTANT (r) = true;
6691           }
6692       else if (ctx->strict)
6693           r = decl_really_constant_value (t, /*unshare_p=*/false);
6694       else
6695           r = decl_constant_value (t, /*unshare_p=*/false);
6696       if (TREE_CODE (r) == TARGET_EXPR
6697             && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
6698           r = TARGET_EXPR_INITIAL (r);
6699       if (DECL_P (r))
6700           {
6701             if (!ctx->quiet)
6702               non_const_var_error (loc, r);
6703             *non_constant_p = true;
6704           }
6705       break;
6706 
6707     case DEBUG_BEGIN_STMT:
6708       /* ??? It might be nice to retain this information somehow, so
6709            as to be able to step into a constexpr function call.  */
6710       /* Fall through.  */
6711 
6712     case FUNCTION_DECL:
6713     case TEMPLATE_DECL:
6714     case LABEL_DECL:
6715     case LABEL_EXPR:
6716     case CASE_LABEL_EXPR:
6717     case PREDICT_EXPR:
6718       return t;
6719 
6720     case PARM_DECL:
6721       if (lval && !TYPE_REF_P (TREE_TYPE (t)))
6722           /* glvalue use.  */;
6723       else if (tree *p = ctx->global->values.get (r))
6724           r = *p;
6725       else if (lval)
6726           /* Defer in case this is only used for its type.  */;
6727       else if (COMPLETE_TYPE_P (TREE_TYPE (t))
6728                  && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
6729           {
6730             /* If the class is empty, we aren't actually loading anything.  */
6731             r = build_constructor (TREE_TYPE (t), NULL);
6732             TREE_CONSTANT (r) = true;
6733           }
6734       else
6735           {
6736             if (!ctx->quiet)
6737               error ("%qE is not a constant expression", t);
6738             *non_constant_p = true;
6739           }
6740       break;
6741 
6742     case CALL_EXPR:
6743     case AGGR_INIT_EXPR:
6744       r = cxx_eval_call_expression (ctx, t, lval,
6745                                             non_constant_p, overflow_p);
6746       break;
6747 
6748     case DECL_EXPR:
6749       {
6750           r = DECL_EXPR_DECL (t);
6751           if (TREE_CODE (r) == USING_DECL)
6752             {
6753               r = void_node;
6754               break;
6755             }
6756 
6757           if (VAR_P (r)
6758               && (TREE_STATIC (r)
6759                     || (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r)))
6760               /* Allow __FUNCTION__ etc.  */
6761               && !DECL_ARTIFICIAL (r))
6762             {
6763               if (!ctx->quiet)
6764                 {
6765                     if (CP_DECL_THREAD_LOCAL_P (r))
6766                       error_at (loc, "control passes through definition of %qD "
6767                                          "with thread storage duration", r);
6768                     else
6769                       error_at (loc, "control passes through definition of %qD "
6770                                          "with static storage duration", r);
6771                 }
6772               *non_constant_p = true;
6773               break;
6774             }
6775 
6776           if (AGGREGATE_TYPE_P (TREE_TYPE (r))
6777               || VECTOR_TYPE_P (TREE_TYPE (r)))
6778             {
6779               new_ctx = *ctx;
6780               new_ctx.object = r;
6781               new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
6782               CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
6783               ctx->global->values.put (r, new_ctx.ctor);
6784               ctx = &new_ctx;
6785             }
6786 
6787           if (tree init = DECL_INITIAL (r))
6788             {
6789               init = cxx_eval_constant_expression (ctx, init,
6790                                                              false,
6791                                                              non_constant_p, overflow_p);
6792               /* Don't share a CONSTRUCTOR that might be changed.  */
6793               init = unshare_constructor (init);
6794               /* Remember that a constant object's constructor has already
6795                  run.  */
6796               if (CLASS_TYPE_P (TREE_TYPE (r))
6797                     && CP_TYPE_CONST_P (TREE_TYPE (r)))
6798                 TREE_READONLY (init) = true;
6799               ctx->global->values.put (r, init);
6800             }
6801           else if (ctx == &new_ctx)
6802             /* We gave it a CONSTRUCTOR above.  */;
6803           else
6804             ctx->global->values.put (r, NULL_TREE);
6805       }
6806       break;
6807 
6808     case TARGET_EXPR:
6809       {
6810           tree type = TREE_TYPE (t);
6811 
6812           if (!literal_type_p (type))
6813             {
6814               if (!ctx->quiet)
6815                 {
6816                     auto_diagnostic_group d;
6817                     error ("temporary of non-literal type %qT in a "
6818                            "constant expression", type);
6819                     explain_non_literal_class (type);
6820                 }
6821               *non_constant_p = true;
6822               break;
6823             }
6824           gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t));
6825           /* Avoid evaluating a TARGET_EXPR more than once.  */
6826           tree slot = TARGET_EXPR_SLOT (t);
6827           if (tree *p = ctx->global->values.get (slot))
6828             {
6829               if (lval)
6830                 return slot;
6831               r = *p;
6832               break;
6833             }
6834           if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)))
6835             {
6836               /* We're being expanded without an explicit target, so start
6837                  initializing a new object; expansion with an explicit target
6838                  strips the TARGET_EXPR before we get here.  */
6839               new_ctx = *ctx;
6840               /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
6841                  any PLACEHOLDER_EXPR within the initializer that refers to the
6842                  former object under construction.  */
6843               new_ctx.parent = ctx;
6844               new_ctx.ctor = build_constructor (type, NULL);
6845               CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
6846               new_ctx.object = slot;
6847               ctx->global->values.put (new_ctx.object, new_ctx.ctor);
6848               ctx = &new_ctx;
6849             }
6850           /* Pass false for 'lval' because this indicates
6851              initialization of a temporary.  */
6852           r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
6853                                                     false,
6854                                                     non_constant_p, overflow_p);
6855           if (*non_constant_p)
6856             break;
6857           /* If the initializer is complex, evaluate it to initialize slot.  */
6858           bool is_complex = target_expr_needs_replace (t);
6859           if (!is_complex)
6860             {
6861               r = unshare_constructor (r);
6862               /* Adjust the type of the result to the type of the temporary.  */
6863               r = adjust_temp_type (type, r);
6864               ctx->global->values.put (slot, r);
6865             }
6866           if (TARGET_EXPR_CLEANUP (t) && !CLEANUP_EH_ONLY (t))
6867             ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t));
6868           if (ctx->save_exprs)
6869             ctx->save_exprs->safe_push (slot);
6870           if (lval)
6871             return slot;
6872           if (is_complex)
6873             r = *ctx->global->values.get (slot);
6874       }
6875       break;
6876 
6877     case INIT_EXPR:
6878     case MODIFY_EXPR:
6879       gcc_assert (jump_target == NULL || *jump_target == NULL_TREE);
6880       r = cxx_eval_store_expression (ctx, t, lval,
6881                                              non_constant_p, overflow_p);
6882       break;
6883 
6884     case SCOPE_REF:
6885       r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
6886                                                   lval,
6887                                                   non_constant_p, overflow_p);
6888       break;
6889 
6890     case RETURN_EXPR:
6891       if (TREE_OPERAND (t, 0) != NULL_TREE)
6892           r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
6893                                                     lval,
6894                                                     non_constant_p, overflow_p);
6895       /* FALLTHRU */
6896     case BREAK_STMT:
6897     case CONTINUE_STMT:
6898       if (jump_target)
6899           *jump_target = t;
6900       else
6901           {
6902             /* Can happen with ({ return true; }) && false; passed to
6903                maybe_constant_value.  There is nothing to jump over in this
6904                case, and the bug will be diagnosed later.  */
6905             gcc_assert (ctx->quiet);
6906             *non_constant_p = true;
6907           }
6908       break;
6909 
6910     case SAVE_EXPR:
6911       /* Avoid evaluating a SAVE_EXPR more than once.  */
6912       if (tree *p = ctx->global->values.get (t))
6913           r = *p;
6914       else
6915           {
6916             r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false,
6917                                                       non_constant_p, overflow_p);
6918             if (*non_constant_p)
6919               break;
6920             ctx->global->values.put (t, r);
6921             if (ctx->save_exprs)
6922               ctx->save_exprs->safe_push (t);
6923           }
6924       break;
6925 
6926     case TRY_CATCH_EXPR:
6927       if (TREE_OPERAND (t, 0) == NULL_TREE)
6928           {
6929             r = void_node;
6930             break;
6931           }
6932       /* FALLTHRU */
6933     case NON_LVALUE_EXPR:
6934     case TRY_BLOCK:
6935     case MUST_NOT_THROW_EXPR:
6936     case EXPR_STMT:
6937     case EH_SPEC_BLOCK:
6938       r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
6939                                                   lval,
6940                                                   non_constant_p, overflow_p,
6941                                                   jump_target);
6942       break;
6943 
6944     case CLEANUP_POINT_EXPR:
6945       {
6946           auto_vec<tree, 2> cleanups;
6947           vec<tree> *prev_cleanups = ctx->global->cleanups;
6948           ctx->global->cleanups = &cleanups;
6949           r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
6950                                                     lval,
6951                                                     non_constant_p, overflow_p,
6952                                                     jump_target);
6953           ctx->global->cleanups = prev_cleanups;
6954           unsigned int i;
6955           tree cleanup;
6956           /* Evaluate the cleanups.  */
6957           FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
6958             cxx_eval_constant_expression (ctx, cleanup, false,
6959                                                   non_constant_p, overflow_p);
6960       }
6961       break;
6962 
6963     case TRY_FINALLY_EXPR:
6964       r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
6965                                                   non_constant_p, overflow_p,
6966                                                   jump_target);
6967       if (!*non_constant_p)
6968           /* Also evaluate the cleanup.  */
6969           cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), true,
6970                                               non_constant_p, overflow_p);
6971       break;
6972 
6973     case CLEANUP_STMT:
6974       r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval,
6975                                                   non_constant_p, overflow_p,
6976                                                   jump_target);
6977       if (!CLEANUP_EH_ONLY (t) && !*non_constant_p)
6978           {
6979             iloc_sentinel ils (loc);
6980             /* Also evaluate the cleanup.  */
6981             cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), true,
6982                                                   non_constant_p, overflow_p);
6983           }
6984       break;
6985 
6986       /* These differ from cxx_eval_unary_expression in that this doesn't
6987            check for a constant operand or result; an address can be
6988            constant without its operand being, and vice versa.  */
6989     case MEM_REF:
6990     case INDIRECT_REF:
6991       r = cxx_eval_indirect_ref (ctx, t, lval,
6992                                          non_constant_p, overflow_p);
6993       break;
6994 
6995     case ADDR_EXPR:
6996       {
6997           tree oldop = TREE_OPERAND (t, 0);
6998           tree op = cxx_eval_constant_expression (ctx, oldop,
6999                                                             /*lval*/true,
7000                                                             non_constant_p, overflow_p);
7001           /* Don't VERIFY_CONSTANT here.  */
7002           if (*non_constant_p)
7003             return t;
7004           gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
7005           /* This function does more aggressive folding than fold itself.  */
7006           r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
7007           if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
7008             {
7009               ggc_free (r);
7010               return t;
7011             }
7012           break;
7013       }
7014 
7015     case REALPART_EXPR:
7016     case IMAGPART_EXPR:
7017       if (lval)
7018           {
7019             r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
7020                                                       non_constant_p, overflow_p);
7021             if (r == error_mark_node)
7022               ;
7023             else if (r == TREE_OPERAND (t, 0))
7024               r = t;
7025             else
7026               r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r);
7027             break;
7028           }
7029       /* FALLTHRU */
7030     case CONJ_EXPR:
7031     case FIX_TRUNC_EXPR:
7032     case FLOAT_EXPR:
7033     case NEGATE_EXPR:
7034     case ABS_EXPR:
7035     case ABSU_EXPR:
7036     case BIT_NOT_EXPR:
7037     case TRUTH_NOT_EXPR:
7038     case FIXED_CONVERT_EXPR:
7039       r = cxx_eval_unary_expression (ctx, t, lval,
7040                                              non_constant_p, overflow_p);
7041       break;
7042 
7043     case SIZEOF_EXPR:
7044       r = fold_sizeof_expr (t);
7045       /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
7046            which could lead to an infinite recursion.  */
7047       if (TREE_CODE (r) != SIZEOF_EXPR)
7048           r = cxx_eval_constant_expression (ctx, r, lval,
7049                                                     non_constant_p, overflow_p,
7050                                                     jump_target);
7051       else
7052           {
7053             *non_constant_p = true;
7054             gcc_assert (ctx->quiet);
7055           }
7056 
7057       break;
7058 
7059     case COMPOUND_EXPR:
7060       {
7061           /* check_return_expr sometimes wraps a TARGET_EXPR in a
7062              COMPOUND_EXPR; don't get confused.  Also handle EMPTY_CLASS_EXPR
7063              introduced by build_call_a.  */
7064           tree op0 = TREE_OPERAND (t, 0);
7065           tree op1 = TREE_OPERAND (t, 1);
7066           STRIP_NOPS (op1);
7067           if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
7068               || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
7069             r = cxx_eval_constant_expression (ctx, op0,
7070                                                       lval, non_constant_p, overflow_p,
7071                                                       jump_target);
7072           else
7073             {
7074               /* Check that the LHS is constant and then discard it.  */
7075               cxx_eval_constant_expression (ctx, op0,
7076                                                     true, non_constant_p, overflow_p,
7077                                                     jump_target);
7078               if (*non_constant_p)
7079                 return t;
7080               op1 = TREE_OPERAND (t, 1);
7081               r = cxx_eval_constant_expression (ctx, op1,
7082                                                         lval, non_constant_p, overflow_p,
7083                                                         jump_target);
7084             }
7085       }
7086       break;
7087 
7088     case POINTER_PLUS_EXPR:
7089     case POINTER_DIFF_EXPR:
7090     case PLUS_EXPR:
7091     case MINUS_EXPR:
7092     case MULT_EXPR:
7093     case TRUNC_DIV_EXPR:
7094     case CEIL_DIV_EXPR:
7095     case FLOOR_DIV_EXPR:
7096     case ROUND_DIV_EXPR:
7097     case TRUNC_MOD_EXPR:
7098     case CEIL_MOD_EXPR:
7099     case ROUND_MOD_EXPR:
7100     case RDIV_EXPR:
7101     case EXACT_DIV_EXPR:
7102     case MIN_EXPR:
7103     case MAX_EXPR:
7104     case LSHIFT_EXPR:
7105     case RSHIFT_EXPR:
7106     case LROTATE_EXPR:
7107     case RROTATE_EXPR:
7108     case BIT_IOR_EXPR:
7109     case BIT_XOR_EXPR:
7110     case BIT_AND_EXPR:
7111     case TRUTH_XOR_EXPR:
7112     case LT_EXPR:
7113     case LE_EXPR:
7114     case GT_EXPR:
7115     case GE_EXPR:
7116     case EQ_EXPR:
7117     case NE_EXPR:
7118     case SPACESHIP_EXPR:
7119     case UNORDERED_EXPR:
7120     case ORDERED_EXPR:
7121     case UNLT_EXPR:
7122     case UNLE_EXPR:
7123     case UNGT_EXPR:
7124     case UNGE_EXPR:
7125     case UNEQ_EXPR:
7126     case LTGT_EXPR:
7127     case RANGE_EXPR:
7128     case COMPLEX_EXPR:
7129       r = cxx_eval_binary_expression (ctx, t, lval,
7130                                               non_constant_p, overflow_p);
7131       break;
7132 
7133       /* fold can introduce non-IF versions of these; still treat them as
7134            short-circuiting.  */
7135     case TRUTH_AND_EXPR:
7136     case TRUTH_ANDIF_EXPR:
7137       r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
7138                                                boolean_true_node,
7139                                                non_constant_p, overflow_p);
7140       break;
7141 
7142     case TRUTH_OR_EXPR:
7143     case TRUTH_ORIF_EXPR:
7144       r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
7145                                                boolean_false_node,
7146                                                non_constant_p, overflow_p);
7147       break;
7148 
7149     case ARRAY_REF:
7150       r = cxx_eval_array_reference (ctx, t, lval,
7151                                             non_constant_p, overflow_p);
7152       break;
7153 
7154     case COMPONENT_REF:
7155       if (is_overloaded_fn (t))
7156           {
7157             /* We can only get here in checking mode via
7158                build_non_dependent_expr,  because any expression that
7159                calls or takes the address of the function will have
7160                pulled a FUNCTION_DECL out of the COMPONENT_REF.  */
7161             gcc_checking_assert (ctx->quiet || errorcount);
7162             *non_constant_p = true;
7163             return t;
7164           }
7165       r = cxx_eval_component_reference (ctx, t, lval,
7166                                                   non_constant_p, overflow_p);
7167       break;
7168 
7169     case BIT_FIELD_REF:
7170       r = cxx_eval_bit_field_ref (ctx, t, lval,
7171                                           non_constant_p, overflow_p);
7172       break;
7173 
7174     case COND_EXPR:
7175     case IF_STMT:
7176       if (jump_target && *jump_target)
7177           {
7178             tree orig_jump = *jump_target;
7179             tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1))
7180                           ? TREE_OPERAND (t, 1) : void_node);
7181             /* When jumping to a label, the label might be either in the
7182                then or else blocks, so process then block first in skipping
7183                mode first, and if we are still in the skipping mode at its end,
7184                process the else block too.  */
7185             r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
7186                                                       overflow_p, jump_target);
7187             /* It's possible that we found the label in the then block.  But
7188                it could have been followed by another jumping statement, e.g.
7189                say we're looking for case 1:
7190                 if (cond)
7191                     {
7192                       // skipped statements
7193                       case 1:; // clears up *jump_target
7194                       return 1; // and sets it to a RETURN_EXPR
7195                     }
7196                 else { ... }
7197                in which case we need not go looking to the else block.
7198                (goto is not allowed in a constexpr function.)  */
7199             if (*jump_target == orig_jump)
7200               {
7201                 arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2))
7202                          ? TREE_OPERAND (t, 2) : void_node);
7203                 r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
7204                                                             overflow_p, jump_target);
7205               }
7206             break;
7207           }
7208       r = cxx_eval_conditional_expression (ctx, t, lval,
7209                                                      non_constant_p, overflow_p,
7210                                                      jump_target);
7211       break;
7212     case VEC_COND_EXPR:
7213       r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p,
7214                                                               overflow_p);
7215       break;
7216 
7217     case CONSTRUCTOR:
7218       if (TREE_CONSTANT (t) && reduced_constant_expression_p (t))
7219           {
7220             /* Don't re-process a constant CONSTRUCTOR, but do fold it to
7221                VECTOR_CST if applicable.  */
7222             verify_constructor_flags (t);
7223             if (TREE_CONSTANT (t))
7224               return fold (t);
7225           }
7226       r = cxx_eval_bare_aggregate (ctx, t, lval,
7227                                            non_constant_p, overflow_p);
7228       break;
7229 
7230     case VEC_INIT_EXPR:
7231       /* We can get this in a defaulted constructor for a class with a
7232            non-static data member of array type.  Either the initializer will
7233            be NULL, meaning default-initialization, or it will be an lvalue
7234            or xvalue of the same type, meaning direct-initialization from the
7235            corresponding member.  */
7236       r = cxx_eval_vec_init (ctx, t, lval,
7237                                    non_constant_p, overflow_p);
7238       break;
7239 
7240     case VEC_PERM_EXPR:
7241       r = cxx_eval_trinary_expression (ctx, t, lval,
7242                                                non_constant_p, overflow_p);
7243       break;
7244 
7245     case PAREN_EXPR:
7246       gcc_assert (!REF_PARENTHESIZED_P (t));
7247       /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in
7248          constant expressions since it's unaffected by -fassociative-math.  */
7249       r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
7250                                                   non_constant_p, overflow_p);
7251       break;
7252 
7253     case NOP_EXPR:
7254       if (REINTERPRET_CAST_P (t))
7255           {
7256             if (!ctx->quiet)
7257               error_at (loc,
7258                           "%<reinterpret_cast%> is not a constant expression");
7259             *non_constant_p = true;
7260             return t;
7261           }
7262       /* FALLTHROUGH.  */
7263     case CONVERT_EXPR:
7264     case VIEW_CONVERT_EXPR:
7265     case UNARY_PLUS_EXPR:
7266       {
7267           tree oldop = TREE_OPERAND (t, 0);
7268 
7269           tree op = cxx_eval_constant_expression (ctx, oldop,
7270                                                             lval,
7271                                                             non_constant_p, overflow_p);
7272           if (*non_constant_p)
7273             return t;
7274           tree type = TREE_TYPE (t);
7275 
7276           if (VOID_TYPE_P (type))
7277             return void_node;
7278 
7279           if (TREE_CODE (t) == CONVERT_EXPR
7280               && ARITHMETIC_TYPE_P (type)
7281               && INDIRECT_TYPE_P (TREE_TYPE (op))
7282               && ctx->manifestly_const_eval)
7283             {
7284               if (!ctx->quiet)
7285                 error_at (loc,
7286                               "conversion from pointer type %qT to arithmetic type "
7287                               "%qT in a constant expression", TREE_TYPE (op), type);
7288               *non_constant_p = true;
7289               return t;
7290             }
7291 
7292           /* [expr.const]: a conversion from type cv void* to a pointer-to-object
7293              type cannot be part of a core constant expression as a resolution to
7294              DR 1312.  */
7295           if (TYPE_PTROB_P (type)
7296               && TYPE_PTR_P (TREE_TYPE (op))
7297               && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op)))
7298               /* Inside a call to std::construct_at or to
7299                  std::allocator<T>::{,de}allocate, we permit casting from void*
7300                  because that is compiler-generated code.  */
7301               && !is_std_construct_at (ctx->call)
7302               && !is_std_allocator_allocate (ctx->call))
7303             {
7304               /* Likewise, don't error when casting from void* when OP is
7305                  &heap uninit and similar.  */
7306               tree sop = tree_strip_nop_conversions (op);
7307               if (TREE_CODE (sop) == ADDR_EXPR
7308                     && VAR_P (TREE_OPERAND (sop, 0))
7309                     && DECL_ARTIFICIAL (TREE_OPERAND (sop, 0)))
7310                 /* OK */;
7311               else
7312                 {
7313                     if (!ctx->quiet)
7314                       error_at (loc, "cast from %qT is not allowed",
7315                                   TREE_TYPE (op));
7316                     *non_constant_p = true;
7317                     return t;
7318                 }
7319             }
7320 
7321           if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
7322             {
7323               op = cplus_expand_constant (op);
7324               if (TREE_CODE (op) == PTRMEM_CST)
7325                 {
7326                     if (!ctx->quiet)
7327                       error_at (loc, "%qE is not a constant expression when the "
7328                                   "class %qT is still incomplete", op,
7329                                   PTRMEM_CST_CLASS (op));
7330                     *non_constant_p = true;
7331                     return t;
7332                 }
7333             }
7334 
7335           if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR)
7336             {
7337               if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op))
7338                     && !can_convert_qual (type, op))
7339                 op = cplus_expand_constant (op);
7340               return cp_fold_convert (type, op);
7341             }
7342 
7343           if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST)
7344             {
7345               if (integer_zerop (op))
7346                 {
7347                     if (TYPE_REF_P (type))
7348                       {
7349                         if (!ctx->quiet)
7350                           error_at (loc, "dereferencing a null pointer");
7351                         *non_constant_p = true;
7352                         return t;
7353                       }
7354                 }
7355               else
7356                 {
7357                     /* This detects for example:
7358                          reinterpret_cast<void*>(sizeof 0)
7359                     */
7360                     if (!ctx->quiet)
7361                       error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not "
7362                                   "a constant expression",
7363                                   type, op);
7364                     *non_constant_p = true;
7365                     return t;
7366                 }
7367             }
7368 
7369           if (INDIRECT_TYPE_P (type)
7370               && TREE_CODE (op) == NOP_EXPR
7371               && TREE_TYPE (op) == ptr_type_node
7372               && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR
7373               && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0))
7374               && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
7375                                                    0)) == heap_uninit_identifier
7376                     || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
7377                                                       0)) == heap_vec_uninit_identifier))
7378             {
7379               tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
7380               tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var));
7381               tree elt_type = TREE_TYPE (type);
7382               tree cookie_size = NULL_TREE;
7383               tree arg_size = NULL_TREE;
7384               if (TREE_CODE (elt_type) == RECORD_TYPE
7385                     && TYPE_NAME (elt_type) == heap_identifier)
7386                 {
7387                     tree fld1 = TYPE_FIELDS (elt_type);
7388                     tree fld2 = DECL_CHAIN (fld1);
7389                     elt_type = TREE_TYPE (TREE_TYPE (fld2));
7390                     cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1));
7391                 }
7392               DECL_NAME (var)
7393                 = (DECL_NAME (var) == heap_uninit_identifier
7394                      ? heap_identifier : heap_vec_identifier);
7395               /* For zero sized elt_type, try to recover how many outer_nelts
7396                  it should have.  */
7397               if ((cookie_size ? tree_int_cst_equal (var_size, cookie_size)
7398                                    : integer_zerop (var_size))
7399                     && !int_size_in_bytes (elt_type)
7400                     && TREE_CODE (oldop) == CALL_EXPR
7401                     && call_expr_nargs (oldop) >= 1)
7402                 if (tree fun = get_function_named_in_call (oldop))
7403                     if (cxx_replaceable_global_alloc_fn (fun)
7404                         && IDENTIFIER_NEW_OP_P (DECL_NAME (fun)))
7405                       arg_size = CALL_EXPR_ARG (oldop, 0);
7406               TREE_TYPE (var)
7407                 = build_new_constexpr_heap_type (ctx, elt_type, cookie_size,
7408                                                          var_size, arg_size,
7409                                                          non_constant_p, overflow_p);
7410               TREE_TYPE (TREE_OPERAND (op, 0))
7411                 = build_pointer_type (TREE_TYPE (var));
7412             }
7413 
7414           if (op == oldop && tcode != UNARY_PLUS_EXPR)
7415             /* We didn't fold at the top so we could check for ptr-int
7416                conversion.  */
7417             return fold (t);
7418 
7419           tree sop;
7420 
7421           /* Handle an array's bounds having been deduced after we built
7422              the wrapping expression.  */
7423           if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op)))
7424             r = op;
7425           else if (sop = tree_strip_nop_conversions (op),
7426                      sop != op && (same_type_ignoring_tlq_and_bounds_p
7427                                      (type, TREE_TYPE (sop))))
7428             r = sop;
7429           else if (tcode == UNARY_PLUS_EXPR)
7430             r = fold_convert (TREE_TYPE (t), op);
7431           else
7432             r = fold_build1 (tcode, type, op);
7433 
7434           /* Conversion of an out-of-range value has implementation-defined
7435              behavior; the language considers it different from arithmetic
7436              overflow, which is undefined.  */
7437           if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
7438             TREE_OVERFLOW (r) = false;
7439       }
7440       break;
7441 
7442     case EMPTY_CLASS_EXPR:
7443       /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering
7444            it to an appropriate CONSTRUCTOR.  */
7445       return build_constructor (TREE_TYPE (t), NULL);
7446 
7447     case STATEMENT_LIST:
7448       new_ctx = *ctx;
7449       new_ctx.ctor = new_ctx.object = NULL_TREE;
7450       return cxx_eval_statement_list (&new_ctx, t,
7451                                               non_constant_p, overflow_p, jump_target);
7452 
7453     case BIND_EXPR:
7454       return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
7455                                                      lval,
7456                                                      non_constant_p, overflow_p,
7457                                                      jump_target);
7458 
7459     case PREINCREMENT_EXPR:
7460     case POSTINCREMENT_EXPR:
7461     case PREDECREMENT_EXPR:
7462     case POSTDECREMENT_EXPR:
7463       return cxx_eval_increment_expression (ctx, t,
7464                                                       lval, non_constant_p, overflow_p);
7465 
7466     case LAMBDA_EXPR:
7467     case NEW_EXPR:
7468     case VEC_NEW_EXPR:
7469     case DELETE_EXPR:
7470     case VEC_DELETE_EXPR:
7471     case THROW_EXPR:
7472     case MODOP_EXPR:
7473       /* GCC internal stuff.  */
7474     case VA_ARG_EXPR:
7475     case NON_DEPENDENT_EXPR:
7476     case BASELINK:
7477     case OFFSET_REF:
7478       if (!ctx->quiet)
7479           error_at (loc, "expression %qE is not a constant expression", t);
7480       *non_constant_p = true;
7481       break;
7482 
7483     case OBJ_TYPE_REF:
7484       /* Virtual function lookup.  We don't need to do anything fancy.  */
7485       return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t),
7486                                                      lval, non_constant_p, overflow_p);
7487 
7488     case PLACEHOLDER_EXPR:
7489       /* Use of the value or address of the current object.  */
7490       if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t)))
7491           {
7492             if (TREE_CODE (ctor) == CONSTRUCTOR)
7493               return ctor;
7494             else
7495               return cxx_eval_constant_expression (ctx, ctor, lval,
7496                                                              non_constant_p, overflow_p);
7497           }
7498       /* A placeholder without a referent.  We can get here when
7499            checking whether NSDMIs are noexcept, or in massage_init_elt;
7500            just say it's non-constant for now.  */
7501       gcc_assert (ctx->quiet);
7502       *non_constant_p = true;
7503       break;
7504 
7505     case EXIT_EXPR:
7506       {
7507           tree cond = TREE_OPERAND (t, 0);
7508           cond = cxx_eval_constant_expression (ctx, cond, /*lval*/false,
7509                                                        non_constant_p, overflow_p);
7510           VERIFY_CONSTANT (cond);
7511           if (integer_nonzerop (cond))
7512             *jump_target = t;
7513       }
7514       break;
7515 
7516     case GOTO_EXPR:
7517       if (breaks (&TREE_OPERAND (t, 0))
7518             || continues (&TREE_OPERAND (t, 0))
7519             /* Allow for jumping to a cdtor_label.  */
7520             || returns (&TREE_OPERAND (t, 0)))
7521           *jump_target = TREE_OPERAND (t, 0);
7522       else
7523           {
7524             gcc_assert (cxx_dialect >= cxx23);
7525             if (!ctx->quiet)
7526               error_at (loc, "%<goto%> is not a constant expression");
7527             *non_constant_p = true;
7528           }
7529       break;
7530 
7531     case LOOP_EXPR:
7532     case DO_STMT:
7533     case WHILE_STMT:
7534     case FOR_STMT:
7535       cxx_eval_loop_expr (ctx, t,
7536                                 non_constant_p, overflow_p, jump_target);
7537       break;
7538 
7539     case SWITCH_EXPR:
7540     case SWITCH_STMT:
7541       cxx_eval_switch_expr (ctx, t,
7542                                   non_constant_p, overflow_p, jump_target);
7543       break;
7544 
7545     case REQUIRES_EXPR:
7546       /* It's possible to get a requires-expression in a constant
7547          expression. For example:
7548 
7549              template<typename T> concept bool C() {
7550                return requires (T t) { t; };
7551              }
7552 
7553              template<typename T> requires !C<T>() void f(T);
7554 
7555          Normalization leaves f with the associated constraint
7556          '!requires (T t) { ... }' which is not transformed into
7557          a constraint.  */
7558       if (!processing_template_decl)
7559           return evaluate_requires_expr (t);
7560       else
7561         *non_constant_p = true;
7562       return t;
7563 
7564     case ANNOTATE_EXPR:
7565       r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
7566                                                   lval,
7567                                                   non_constant_p, overflow_p,
7568                                                   jump_target);
7569       break;
7570 
7571     case USING_STMT:
7572       r = void_node;
7573       break;
7574 
7575     case TEMPLATE_ID_EXPR:
7576       {
7577         /* We can evaluate template-id that refers to a concept only if
7578              the template arguments are non-dependent.  */
7579           tree id = unpack_concept_check (t);
7580           tree tmpl = TREE_OPERAND (id, 0);
7581           if (!concept_definition_p (tmpl))
7582             internal_error ("unexpected template-id %qE", t);
7583 
7584           if (function_concept_p (tmpl))
7585             {
7586               if (!ctx->quiet)
7587                 error_at (cp_expr_loc_or_input_loc (t),
7588                               "function concept must be called");
7589               r = error_mark_node;
7590               break;
7591             }
7592 
7593           if (!value_dependent_expression_p (t)
7594               && !uid_sensitive_constexpr_evaluation_p ())
7595             r = evaluate_concept_check (t);
7596           else
7597             *non_constant_p = true;
7598 
7599           break;
7600       }
7601 
7602     case ASM_EXPR:
7603       if (!ctx->quiet)
7604           inline_asm_in_constexpr_error (loc);
7605       *non_constant_p = true;
7606       return t;
7607 
7608     case BIT_CAST_EXPR:
7609       if (lval)
7610           {
7611             if (!ctx->quiet)
7612               error_at (EXPR_LOCATION (t),
7613                           "address of a call to %qs is not a constant expression",
7614                           "__builtin_bit_cast");
7615             *non_constant_p = true;
7616             return t;
7617           }
7618       r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p);
7619       break;
7620 
7621     case OMP_PARALLEL:
7622     case OMP_TASK:
7623     case OMP_FOR:
7624     case OMP_SIMD:
7625     case OMP_DISTRIBUTE:
7626     case OMP_TASKLOOP:
7627     case OMP_LOOP:
7628     case OMP_TEAMS:
7629     case OMP_TARGET_DATA:
7630     case OMP_TARGET:
7631     case OMP_SECTIONS:
7632     case OMP_ORDERED:
7633     case OMP_CRITICAL:
7634     case OMP_SINGLE:
7635     case OMP_SCAN:
7636     case OMP_SCOPE:
7637     case OMP_SECTION:
7638     case OMP_MASTER:
7639     case OMP_MASKED:
7640     case OMP_TASKGROUP:
7641     case OMP_TARGET_UPDATE:
7642     case OMP_TARGET_ENTER_DATA:
7643     case OMP_TARGET_EXIT_DATA:
7644     case OMP_ATOMIC:
7645     case OMP_ATOMIC_READ:
7646     case OMP_ATOMIC_CAPTURE_OLD:
7647     case OMP_ATOMIC_CAPTURE_NEW:
7648     case OMP_DEPOBJ:
7649     case OACC_PARALLEL:
7650     case OACC_KERNELS:
7651     case OACC_SERIAL:
7652     case OACC_DATA:
7653     case OACC_HOST_DATA:
7654     case OACC_LOOP:
7655     case OACC_CACHE:
7656     case OACC_DECLARE:
7657     case OACC_ENTER_DATA:
7658     case OACC_EXIT_DATA:
7659     case OACC_UPDATE:
7660       if (!ctx->quiet)
7661           error_at (EXPR_LOCATION (t),
7662                       "statement is not a constant expression");
7663       *non_constant_p = true;
7664       break;
7665 
7666     default:
7667       if (STATEMENT_CODE_P (TREE_CODE (t)))
7668           {
7669             /* This function doesn't know how to deal with pre-genericize
7670                statements; this can only happen with statement-expressions,
7671                so for now just fail.  */
7672             if (!ctx->quiet)
7673               error_at (EXPR_LOCATION (t),
7674                           "statement is not a constant expression");
7675           }
7676       else
7677           internal_error ("unexpected expression %qE of kind %s", t,
7678                               get_tree_code_name (TREE_CODE (t)));
7679       *non_constant_p = true;
7680       break;
7681     }
7682 
7683   if (r == error_mark_node)
7684     *non_constant_p = true;
7685 
7686   if (*non_constant_p)
7687     return t;
7688   else
7689     return r;
7690 }
7691 
7692 /* P0859: A function is needed for constant evaluation if it is a constexpr
7693    function that is named by an expression ([basic.def.odr]) that is
7694    potentially constant evaluated.
7695 
7696    So we need to instantiate any constexpr functions mentioned by the
7697    expression even if the definition isn't needed for evaluating the
7698    expression.  */
7699 
7700 static tree
instantiate_cx_fn_r(tree * tp,int * walk_subtrees,void *)7701 instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/)
7702 {
7703   if (TREE_CODE (*tp) == FUNCTION_DECL
7704       && DECL_DECLARED_CONSTEXPR_P (*tp)
7705       && !DECL_INITIAL (*tp)
7706       && !trivial_fn_p (*tp)
7707       && DECL_TEMPLOID_INSTANTIATION (*tp)
7708       && !uid_sensitive_constexpr_evaluation_p ())
7709     {
7710       ++function_depth;
7711       instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false);
7712       --function_depth;
7713     }
7714   else if (TREE_CODE (*tp) == CALL_EXPR
7715              || TREE_CODE (*tp) == AGGR_INIT_EXPR)
7716     {
7717       if (EXPR_HAS_LOCATION (*tp))
7718           input_location = EXPR_LOCATION (*tp);
7719     }
7720 
7721   if (!EXPR_P (*tp))
7722     *walk_subtrees = 0;
7723 
7724   return NULL_TREE;
7725 }
7726 
7727 static void
instantiate_constexpr_fns(tree t)7728 instantiate_constexpr_fns (tree t)
7729 {
7730   location_t loc = input_location;
7731   cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL);
7732   input_location = loc;
7733 }
7734 
7735 /* Look for heap variables in the expression *TP.  */
7736 
7737 static tree
find_heap_var_refs(tree * tp,int * walk_subtrees,void *)7738 find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/)
7739 {
7740   if (VAR_P (*tp)
7741       && (DECL_NAME (*tp) == heap_uninit_identifier
7742             || DECL_NAME (*tp) == heap_identifier
7743             || DECL_NAME (*tp) == heap_vec_uninit_identifier
7744             || DECL_NAME (*tp) == heap_vec_identifier
7745             || DECL_NAME (*tp) == heap_deleted_identifier))
7746     return *tp;
7747 
7748   if (TYPE_P (*tp))
7749     *walk_subtrees = 0;
7750   return NULL_TREE;
7751 }
7752 
7753 /* Find immediate function decls in *TP if any.  */
7754 
7755 static tree
find_immediate_fndecl(tree * tp,int *,void *)7756 find_immediate_fndecl (tree *tp, int */*walk_subtrees*/, void */*data*/)
7757 {
7758   if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp))
7759     return *tp;
7760   if (TREE_CODE (*tp) == PTRMEM_CST
7761       && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL
7762       && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp)))
7763     return PTRMEM_CST_MEMBER (*tp);
7764   return NULL_TREE;
7765 }
7766 
7767 /* T has TREE_CONSTANT set but has been deemed not a valid C++ constant
7768    expression.  Return a version of T that has TREE_CONSTANT cleared.  */
7769 
7770 static tree
mark_non_constant(tree t)7771 mark_non_constant (tree t)
7772 {
7773   gcc_checking_assert (TREE_CONSTANT (t));
7774 
7775   /* This isn't actually constant, so unset TREE_CONSTANT.
7776      Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
7777      it to be set if it is invariant address, even when it is not
7778      a valid C++ constant expression.  Wrap it with a NOP_EXPR
7779      instead.  */
7780   if (EXPR_P (t) && TREE_CODE (t) != ADDR_EXPR)
7781     t = copy_node (t);
7782   else if (TREE_CODE (t) == CONSTRUCTOR)
7783     t = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (t), t);
7784   else
7785     t = build_nop (TREE_TYPE (t), t);
7786   TREE_CONSTANT (t) = false;
7787   return t;
7788 }
7789 
7790 /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
7791    STRICT has the same sense as for constant_value_1: true if we only allow
7792    conforming C++ constant expressions, or false if we want a constant value
7793    even if it doesn't conform.
7794    MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
7795    per P0595 even when ALLOW_NON_CONSTANT is true.
7796    CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
7797    OBJECT must be non-NULL in that case.  */
7798 
7799 static tree
cxx_eval_outermost_constant_expr(tree t,bool allow_non_constant,bool strict=true,bool manifestly_const_eval=false,bool constexpr_dtor=false,tree object=NULL_TREE)7800 cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
7801                                           bool strict = true,
7802                                           bool manifestly_const_eval = false,
7803                                           bool constexpr_dtor = false,
7804                                           tree object = NULL_TREE)
7805 {
7806   auto_timevar time (TV_CONSTEXPR);
7807 
7808   bool non_constant_p = false;
7809   bool overflow_p = false;
7810 
7811   if (BRACE_ENCLOSED_INITIALIZER_P (t))
7812     {
7813       gcc_checking_assert (allow_non_constant);
7814       return t;
7815     }
7816 
7817   constexpr_global_ctx global_ctx;
7818   constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL,
7819                               allow_non_constant, strict,
7820                               manifestly_const_eval || !allow_non_constant };
7821 
7822   /* Turn off -frounding-math for manifestly constant evaluation.  */
7823   warning_sentinel rm (flag_rounding_math, ctx.manifestly_const_eval);
7824   tree type = initialized_type (t);
7825   tree r = t;
7826   bool is_consteval = false;
7827   if (VOID_TYPE_P (type))
7828     {
7829       if (constexpr_dtor)
7830           /* Used for destructors of array elements.  */
7831           type = TREE_TYPE (object);
7832       else
7833           {
7834             if (cxx_dialect < cxx20)
7835               return t;
7836             if (TREE_CODE (t) != CALL_EXPR && TREE_CODE (t) != AGGR_INIT_EXPR)
7837               return t;
7838             /* Calls to immediate functions returning void need to be
7839                evaluated.  */
7840             tree fndecl = cp_get_callee_fndecl_nofold (t);
7841             if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl))
7842               return t;
7843             else
7844               is_consteval = true;
7845           }
7846     }
7847   else if (cxx_dialect >= cxx20
7848              && (TREE_CODE (t) == CALL_EXPR
7849                  || TREE_CODE (t) == AGGR_INIT_EXPR
7850                  || TREE_CODE (t) == TARGET_EXPR))
7851     {
7852       /* For non-concept checks, determine if it is consteval.  */
7853       if (!concept_check_p (t))
7854           {
7855             tree x = t;
7856             if (TREE_CODE (x) == TARGET_EXPR)
7857               x = TARGET_EXPR_INITIAL (x);
7858             tree fndecl = cp_get_callee_fndecl_nofold (x);
7859             if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl))
7860               is_consteval = true;
7861           }
7862     }
7863   if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
7864     {
7865       /* In C++14 an NSDMI can participate in aggregate initialization,
7866            and can refer to the address of the object being initialized, so
7867            we need to pass in the relevant VAR_DECL if we want to do the
7868            evaluation in a single pass.  The evaluation will dynamically
7869            update ctx.values for the VAR_DECL.  We use the same strategy
7870            for C++11 constexpr constructors that refer to the object being
7871            initialized.  */
7872       if (constexpr_dtor)
7873           {
7874             gcc_assert (object && VAR_P (object));
7875             gcc_assert (DECL_DECLARED_CONSTEXPR_P (object));
7876             gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object));
7877             if (error_operand_p (DECL_INITIAL (object)))
7878               return t;
7879             ctx.ctor = unshare_expr (DECL_INITIAL (object));
7880             TREE_READONLY (ctx.ctor) = false;
7881             /* Temporarily force decl_really_constant_value to return false
7882                for it, we want to use ctx.ctor for the current value instead.  */
7883             DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false;
7884           }
7885       else
7886           {
7887             ctx.ctor = build_constructor (type, NULL);
7888             CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true;
7889           }
7890       if (!object)
7891           {
7892             if (TREE_CODE (t) == TARGET_EXPR)
7893               object = TARGET_EXPR_SLOT (t);
7894             else if (TREE_CODE (t) == AGGR_INIT_EXPR)
7895               object = AGGR_INIT_EXPR_SLOT (t);
7896           }
7897       ctx.object = object;
7898       if (object)
7899           gcc_assert (same_type_ignoring_top_level_qualifiers_p
7900                         (type, TREE_TYPE (object)));
7901       if (object && DECL_P (object))
7902           global_ctx.values.put (object, ctx.ctor);
7903       if (TREE_CODE (r) == TARGET_EXPR)
7904           /* Avoid creating another CONSTRUCTOR when we expand the
7905              TARGET_EXPR.  */
7906           r = TARGET_EXPR_INITIAL (r);
7907     }
7908 
7909   auto_vec<tree, 16> cleanups;
7910   global_ctx.cleanups = &cleanups;
7911 
7912   if (manifestly_const_eval)
7913     instantiate_constexpr_fns (r);
7914   r = cxx_eval_constant_expression (&ctx, r,
7915                                             false, &non_constant_p, &overflow_p);
7916 
7917   if (!constexpr_dtor)
7918     verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
7919   else
7920     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true;
7921 
7922   unsigned int i;
7923   tree cleanup;
7924   /* Evaluate the cleanups.  */
7925   FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
7926     cxx_eval_constant_expression (&ctx, cleanup, false,
7927                                           &non_constant_p, &overflow_p);
7928 
7929   /* Mutable logic is a bit tricky: we want to allow initialization of
7930      constexpr variables with mutable members, but we can't copy those
7931      members to another constexpr variable.  */
7932   if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_MUTABLE_POISON (r))
7933     {
7934       if (!allow_non_constant)
7935           error ("%qE is not a constant expression because it refers to "
7936                  "mutable subobjects of %qT", t, type);
7937       non_constant_p = true;
7938     }
7939 
7940   if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r))
7941     {
7942       if (!allow_non_constant)
7943           error ("%qE is not a constant expression because it refers to "
7944                  "an incompletely initialized variable", t);
7945       TREE_CONSTANT (r) = false;
7946       non_constant_p = true;
7947     }
7948 
7949   if (!global_ctx.heap_vars.is_empty ())
7950     {
7951       tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs,
7952                                                                    NULL);
7953       unsigned int i;
7954       if (heap_var)
7955           {
7956             if (!allow_non_constant && !non_constant_p)
7957               error_at (DECL_SOURCE_LOCATION (heap_var),
7958                           "%qE is not a constant expression because it refers to "
7959                           "a result of %<operator new%>", t);
7960             r = t;
7961             non_constant_p = true;
7962           }
7963       FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var)
7964           {
7965             if (DECL_NAME (heap_var) != heap_deleted_identifier)
7966               {
7967                 if (!allow_non_constant && !non_constant_p)
7968                     error_at (DECL_SOURCE_LOCATION (heap_var),
7969                                 "%qE is not a constant expression because allocated "
7970                                 "storage has not been deallocated", t);
7971                 r = t;
7972                 non_constant_p = true;
7973               }
7974             varpool_node::get (heap_var)->remove ();
7975           }
7976     }
7977 
7978   /* Check that immediate invocation does not return an expression referencing
7979      any immediate function decls.  */
7980   if (is_consteval || in_immediate_context ())
7981     if (tree immediate_fndecl
7982           = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl,
7983                                                      NULL))
7984     {
7985       if (!allow_non_constant && !non_constant_p)
7986           error_at (cp_expr_loc_or_input_loc (t),
7987                       "immediate evaluation returns address of immediate "
7988                       "function %qD", immediate_fndecl);
7989       r = t;
7990       non_constant_p = true;
7991     }
7992 
7993   if (non_constant_p)
7994     /* If we saw something bad, go back to our argument.  The wrapping below is
7995        only for the cases of TREE_CONSTANT argument or overflow.  */
7996     r = t;
7997 
7998   if (!non_constant_p && overflow_p)
7999     non_constant_p = true;
8000 
8001   /* Unshare the result.  */
8002   bool should_unshare = true;
8003   if (r == t || (TREE_CODE (t) == TARGET_EXPR
8004                      && TARGET_EXPR_INITIAL (t) == r))
8005     should_unshare = false;
8006 
8007   if (non_constant_p && !allow_non_constant)
8008     return error_mark_node;
8009   else if (constexpr_dtor)
8010     return r;
8011   else if (non_constant_p && TREE_CONSTANT (r))
8012     r = mark_non_constant (r);
8013   else if (non_constant_p)
8014     return t;
8015 
8016   if (should_unshare)
8017     r = unshare_expr (r);
8018 
8019   if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
8020     {
8021       r = adjust_temp_type (type, r);
8022       if (TREE_CODE (t) == TARGET_EXPR
8023             && TARGET_EXPR_INITIAL (t) == r)
8024           return t;
8025       else if (TREE_CODE (t) == CONSTRUCTOR)
8026           ;
8027       else if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_CLEANUP (t))
8028           r = get_target_expr (r);
8029       else
8030           {
8031             r = get_target_expr_sfinae (r, tf_warning_or_error | tf_no_cleanup);
8032             TREE_CONSTANT (r) = true;
8033           }
8034     }
8035 
8036   /* Remember the original location if that wouldn't need a wrapper.  */
8037   if (location_t loc = EXPR_LOCATION (t))
8038     protected_set_expr_location (r, loc);
8039 
8040   return r;
8041 }
8042 
8043 /* If T represents a constant expression returns its reduced value.
8044    Otherwise return error_mark_node.  If T is dependent, then
8045    return NULL.  */
8046 
8047 tree
cxx_constant_value(tree t,tree decl)8048 cxx_constant_value (tree t, tree decl)
8049 {
8050   return cxx_eval_outermost_constant_expr (t, false, true, true, false, decl);
8051 }
8052 
8053 /* As above, but respect SFINAE.  */
8054 
8055 tree
cxx_constant_value_sfinae(tree t,tree decl,tsubst_flags_t complain)8056 cxx_constant_value_sfinae (tree t, tree decl, tsubst_flags_t complain)
8057 {
8058   bool sfinae = !(complain & tf_error);
8059   tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, true, false, decl);
8060   if (sfinae && !TREE_CONSTANT (r))
8061     r = error_mark_node;
8062   return r;
8063 }
8064 
8065 /* Like cxx_constant_value, but used for evaluation of constexpr destructors
8066    of constexpr variables.  The actual initializer of DECL is not modified.  */
8067 
8068 void
cxx_constant_dtor(tree t,tree decl)8069 cxx_constant_dtor (tree t, tree decl)
8070 {
8071   cxx_eval_outermost_constant_expr (t, false, true, true, true, decl);
8072 }
8073 
8074 /* Helper routine for fold_simple function.  Either return simplified
8075    expression T, otherwise NULL_TREE.
8076    In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
8077    even if we are within template-declaration.  So be careful on call, as in
8078    such case types can be undefined.  */
8079 
8080 static tree
fold_simple_1(tree t)8081 fold_simple_1 (tree t)
8082 {
8083   tree op1;
8084   enum tree_code code = TREE_CODE (t);
8085 
8086   switch (code)
8087     {
8088     case INTEGER_CST:
8089     case REAL_CST:
8090     case VECTOR_CST:
8091     case FIXED_CST:
8092     case COMPLEX_CST:
8093       return t;
8094 
8095     case SIZEOF_EXPR:
8096       return fold_sizeof_expr (t);
8097 
8098     case ABS_EXPR:
8099     case ABSU_EXPR:
8100     case CONJ_EXPR:
8101     case REALPART_EXPR:
8102     case IMAGPART_EXPR:
8103     case NEGATE_EXPR:
8104     case BIT_NOT_EXPR:
8105     case TRUTH_NOT_EXPR:
8106     case NOP_EXPR:
8107     case VIEW_CONVERT_EXPR:
8108     case CONVERT_EXPR:
8109     case FLOAT_EXPR:
8110     case FIX_TRUNC_EXPR:
8111     case FIXED_CONVERT_EXPR:
8112     case ADDR_SPACE_CONVERT_EXPR:
8113 
8114       op1 = TREE_OPERAND (t, 0);
8115 
8116       t = const_unop (code, TREE_TYPE (t), op1);
8117       if (!t)
8118           return NULL_TREE;
8119 
8120       if (CONVERT_EXPR_CODE_P (code)
8121             && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1))
8122           TREE_OVERFLOW (t) = false;
8123       return t;
8124 
8125     default:
8126       return NULL_TREE;
8127     }
8128 }
8129 
8130 /* If T is a simple constant expression, returns its simplified value.
8131    Otherwise returns T.  In contrast to maybe_constant_value we
8132    simplify only few operations on constant-expressions, and we don't
8133    try to simplify constexpressions.  */
8134 
8135 tree
fold_simple(tree t)8136 fold_simple (tree t)
8137 {
8138   if (processing_template_decl)
8139     return t;
8140 
8141   tree r = fold_simple_1 (t);
8142   if (r)
8143     return r;
8144 
8145   return t;
8146 }
8147 
8148 /* Try folding the expression T to a simple constant.
8149    Returns that constant, otherwise returns T.  */
8150 
8151 tree
fold_to_constant(tree t)8152 fold_to_constant (tree t)
8153 {
8154   tree r = fold (t);
8155   if (CONSTANT_CLASS_P (r) && !TREE_OVERFLOW (r))
8156     return r;
8157   else
8158     return t;
8159 }
8160 
8161 /* If T is a constant expression, returns its reduced value.
8162    Otherwise, if T does not have TREE_CONSTANT set, returns T.
8163    Otherwise, returns a version of T without TREE_CONSTANT.
8164    MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
8165    as per P0595.  */
8166 
8167 static GTY((deletable)) hash_map<tree, tree> *cv_cache;
8168 
8169 tree
maybe_constant_value(tree t,tree decl,bool manifestly_const_eval)8170 maybe_constant_value (tree t, tree decl, bool manifestly_const_eval)
8171 {
8172   tree r;
8173 
8174   if (!is_nondependent_constant_expression (t))
8175     {
8176       if (TREE_OVERFLOW_P (t)
8177             || (!processing_template_decl && TREE_CONSTANT (t)))
8178           t = mark_non_constant (t);
8179       return t;
8180     }
8181   else if (CONSTANT_CLASS_P (t))
8182     /* No caching or evaluation needed.  */
8183     return t;
8184 
8185   if (manifestly_const_eval)
8186     return cxx_eval_outermost_constant_expr (t, true, true, true, false, decl);
8187 
8188   if (cv_cache == NULL)
8189     cv_cache = hash_map<tree, tree>::create_ggc (101);
8190   if (tree *cached = cv_cache->get (t))
8191     {
8192       r = *cached;
8193       if (r != t)
8194           {
8195             /* Clear processing_template_decl for sake of break_out_target_exprs;
8196                entries in the cv_cache are non-templated.  */
8197             processing_template_decl_sentinel ptds;
8198 
8199             r = break_out_target_exprs (r, /*clear_loc*/true);
8200             protected_set_expr_location (r, EXPR_LOCATION (t));
8201           }
8202       return r;
8203     }
8204 
8205   /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
8206      but at least try folding it to a simple constant.  */
8207   if (cp_unevaluated_operand)
8208     return fold_to_constant (t);
8209 
8210   uid_sensitive_constexpr_evaluation_checker c;
8211   r = cxx_eval_outermost_constant_expr (t, true, true, false, false, decl);
8212   gcc_checking_assert (r == t
8213                            || CONVERT_EXPR_P (t)
8214                            || TREE_CODE (t) == VIEW_CONVERT_EXPR
8215                            || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
8216                            || !cp_tree_equal (r, t));
8217   if (!c.evaluation_restricted_p ())
8218     cv_cache->put (t, r);
8219   return r;
8220 }
8221 
8222 /* Dispose of the whole CV_CACHE.  */
8223 
8224 static void
clear_cv_cache(void)8225 clear_cv_cache (void)
8226 {
8227   if (cv_cache != NULL)
8228     cv_cache->empty ();
8229 }
8230 
8231 /* Dispose of the whole CV_CACHE and FOLD_CACHE.  */
8232 
8233 void
clear_cv_and_fold_caches()8234 clear_cv_and_fold_caches ()
8235 {
8236   clear_cv_cache ();
8237   clear_fold_cache ();
8238 }
8239 
8240 /* Internal function handling expressions in templates for
8241    fold_non_dependent_expr and fold_non_dependent_init.
8242 
8243    If we're in a template, but T isn't value dependent, simplify
8244    it.  We're supposed to treat:
8245 
8246      template <typename T> void f(T[1 + 1]);
8247      template <typename T> void f(T[2]);
8248 
8249    as two declarations of the same function, for example.  */
8250 
8251 static tree
fold_non_dependent_expr_template(tree t,tsubst_flags_t complain,bool manifestly_const_eval,tree object)8252 fold_non_dependent_expr_template (tree t, tsubst_flags_t complain,
8253                                           bool manifestly_const_eval,
8254                                           tree object)
8255 {
8256   gcc_assert (processing_template_decl);
8257 
8258   if (is_nondependent_constant_expression (t))
8259     {
8260       processing_template_decl_sentinel s;
8261       t = instantiate_non_dependent_expr_internal (t, complain);
8262 
8263       if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t))
8264           {
8265             if (TREE_OVERFLOW_P (t))
8266               {
8267                 t = build_nop (TREE_TYPE (t), t);
8268                 TREE_CONSTANT (t) = false;
8269               }
8270             return t;
8271           }
8272       else if (CONSTANT_CLASS_P (t))
8273           /* No evaluation needed.  */
8274           return t;
8275 
8276       /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
8277            but at least try folding it to a simple constant.  */
8278       if (cp_unevaluated_operand && !manifestly_const_eval)
8279           return fold_to_constant (t);
8280 
8281       tree r = cxx_eval_outermost_constant_expr (t, true, true,
8282                                                              manifestly_const_eval,
8283                                                              false, object);
8284       /* cp_tree_equal looks through NOPs, so allow them.  */
8285       gcc_checking_assert (r == t
8286                                  || CONVERT_EXPR_P (t)
8287                                  || TREE_CODE (t) == VIEW_CONVERT_EXPR
8288                                  || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
8289                                  || !cp_tree_equal (r, t));
8290       return r;
8291     }
8292   else if (TREE_OVERFLOW_P (t))
8293     {
8294       t = build_nop (TREE_TYPE (t), t);
8295       TREE_CONSTANT (t) = false;
8296     }
8297 
8298   return t;
8299 }
8300 
8301 /* Like maybe_constant_value but first fully instantiate the argument.
8302 
8303    Note: this is equivalent to instantiate_non_dependent_expr_sfinae
8304    (t, complain) followed by maybe_constant_value but is more efficient,
8305    because it calls instantiation_dependent_expression_p and
8306    potential_constant_expression at most once.
8307    The manifestly_const_eval argument is passed to maybe_constant_value.
8308 
8309    Callers should generally pass their active complain, or if they are in a
8310    non-template, diagnosing context, they can use the default of
8311    tf_warning_or_error.  Callers that might be within a template context, don't
8312    have a complain parameter, and aren't going to remember the result for long
8313    (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
8314    appropriately.  */
8315 
8316 tree
fold_non_dependent_expr(tree t,tsubst_flags_t complain,bool manifestly_const_eval,tree object)8317 fold_non_dependent_expr (tree t,
8318                                tsubst_flags_t complain /* = tf_warning_or_error */,
8319                                bool manifestly_const_eval /* = false */,
8320                                tree object /* = NULL_TREE */)
8321 {
8322   if (t == NULL_TREE)
8323     return NULL_TREE;
8324 
8325   if (processing_template_decl)
8326     return fold_non_dependent_expr_template (t, complain,
8327                                                        manifestly_const_eval, object);
8328 
8329   return maybe_constant_value (t, object, manifestly_const_eval);
8330 }
8331 
8332 /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
8333    return the original expression.  */
8334 
8335 tree
maybe_fold_non_dependent_expr(tree expr,tsubst_flags_t complain)8336 maybe_fold_non_dependent_expr (tree expr,
8337                                      tsubst_flags_t complain/*=tf_warning_or_error*/)
8338 {
8339   tree t = fold_non_dependent_expr (expr, complain);
8340   if (t && TREE_CONSTANT (t))
8341     return t;
8342 
8343   return expr;
8344 }
8345 
8346 /* Like maybe_constant_init but first fully instantiate the argument.  */
8347 
8348 tree
fold_non_dependent_init(tree t,tsubst_flags_t complain,bool manifestly_const_eval,tree object)8349 fold_non_dependent_init (tree t,
8350                                tsubst_flags_t complain /*=tf_warning_or_error*/,
8351                                bool manifestly_const_eval /*=false*/,
8352                                tree object /* = NULL_TREE */)
8353 {
8354   if (t == NULL_TREE)
8355     return NULL_TREE;
8356 
8357   if (processing_template_decl)
8358     {
8359       t = fold_non_dependent_expr_template (t, complain,
8360                                                       manifestly_const_eval, object);
8361       /* maybe_constant_init does this stripping, so do it here too.  */
8362       if (TREE_CODE (t) == TARGET_EXPR)
8363           {
8364             tree init = TARGET_EXPR_INITIAL (t);
8365             if (TREE_CODE (init) == CONSTRUCTOR)
8366               t = init;
8367           }
8368       return t;
8369     }
8370 
8371   return maybe_constant_init (t, object, manifestly_const_eval);
8372 }
8373 
8374 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
8375    than wrapped in a TARGET_EXPR.
8376    ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
8377    MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
8378    per P0595 even when ALLOW_NON_CONSTANT is true.  */
8379 
8380 static tree
maybe_constant_init_1(tree t,tree decl,bool allow_non_constant,bool manifestly_const_eval)8381 maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant,
8382                            bool manifestly_const_eval)
8383 {
8384   if (!t)
8385     return t;
8386   if (TREE_CODE (t) == EXPR_STMT)
8387     t = TREE_OPERAND (t, 0);
8388   if (TREE_CODE (t) == CONVERT_EXPR
8389       && VOID_TYPE_P (TREE_TYPE (t)))
8390     t = TREE_OPERAND (t, 0);
8391   if (TREE_CODE (t) == INIT_EXPR)
8392     t = TREE_OPERAND (t, 1);
8393   if (TREE_CODE (t) == TARGET_EXPR)
8394     t = TARGET_EXPR_INITIAL (t);
8395   if (!is_nondependent_static_init_expression (t))
8396     /* Don't try to evaluate it.  */;
8397   else if (CONSTANT_CLASS_P (t) && allow_non_constant)
8398     /* No evaluation needed.  */;
8399   else
8400     t = cxx_eval_outermost_constant_expr (t, allow_non_constant,
8401                                                     /*strict*/false,
8402                                                     manifestly_const_eval, false, decl);
8403   if (TREE_CODE (t) == TARGET_EXPR)
8404     {
8405       tree init = TARGET_EXPR_INITIAL (t);
8406       if (TREE_CODE (init) == CONSTRUCTOR)
8407           t = init;
8408     }
8409   return t;
8410 }
8411 
8412 /* Wrapper for maybe_constant_init_1 which permits non constants.  */
8413 
8414 tree
maybe_constant_init(tree t,tree decl,bool manifestly_const_eval)8415 maybe_constant_init (tree t, tree decl, bool manifestly_const_eval)
8416 {
8417   return maybe_constant_init_1 (t, decl, true, manifestly_const_eval);
8418 }
8419 
8420 /* Wrapper for maybe_constant_init_1 which does not permit non constants.  */
8421 
8422 tree
cxx_constant_init(tree t,tree decl)8423 cxx_constant_init (tree t, tree decl)
8424 {
8425   return maybe_constant_init_1 (t, decl, false, true);
8426 }
8427 
8428 #if 0
8429 /* FIXME see ADDR_EXPR section in potential_constant_expression_1.  */
8430 /* Return true if the object referred to by REF has automatic or thread
8431    local storage.  */
8432 
8433 enum { ck_ok, ck_bad, ck_unknown };
8434 static int
8435 check_automatic_or_tls (tree ref)
8436 {
8437   machine_mode mode;
8438   poly_int64 bitsize, bitpos;
8439   tree offset;
8440   int volatilep = 0, unsignedp = 0;
8441   tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
8442                                            &mode, &unsignedp, &volatilep, false);
8443   duration_kind dk;
8444 
8445   /* If there isn't a decl in the middle, we don't know the linkage here,
8446      and this isn't a constant expression anyway.  */
8447   if (!DECL_P (decl))
8448     return ck_unknown;
8449   dk = decl_storage_duration (decl);
8450   return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
8451 }
8452 #endif
8453 
8454 /* Data structure for passing data from potential_constant_expression_1
8455    to check_for_return_continue via cp_walk_tree.  */
8456 struct check_for_return_continue_data {
8457   hash_set<tree> *pset;
8458   tree continue_stmt;
8459   tree break_stmt;
8460 };
8461 
8462 /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
8463    called through cp_walk_tree.  Return the first RETURN_EXPR found, or note
8464    the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found.  */
8465 static tree
check_for_return_continue(tree * tp,int * walk_subtrees,void * data)8466 check_for_return_continue (tree *tp, int *walk_subtrees, void *data)
8467 {
8468   tree t = *tp, s, b;
8469   check_for_return_continue_data *d = (check_for_return_continue_data *) data;
8470   switch (TREE_CODE (t))
8471     {
8472     case RETURN_EXPR:
8473       return t;
8474 
8475     case CONTINUE_STMT:
8476       if (d->continue_stmt == NULL_TREE)
8477           d->continue_stmt = t;
8478       break;
8479 
8480     case BREAK_STMT:
8481       if (d->break_stmt == NULL_TREE)
8482           d->break_stmt = t;
8483       break;
8484 
8485 #define RECUR(x) \
8486       if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
8487                                          d->pset))                                        \
8488           return r
8489 
8490       /* For loops, walk subtrees manually, so that continue stmts found
8491            inside of the bodies of the loops are ignored.  */
8492     case DO_STMT:
8493       *walk_subtrees = 0;
8494       RECUR (DO_COND (t));
8495       s = d->continue_stmt;
8496       b = d->break_stmt;
8497       RECUR (DO_BODY (t));
8498       d->continue_stmt = s;
8499       d->break_stmt = b;
8500       break;
8501 
8502     case WHILE_STMT:
8503       *walk_subtrees = 0;
8504       RECUR (WHILE_COND (t));
8505       s = d->continue_stmt;
8506       b = d->break_stmt;
8507       RECUR (WHILE_BODY (t));
8508       d->continue_stmt = s;
8509       d->break_stmt = b;
8510       break;
8511 
8512     case FOR_STMT:
8513       *walk_subtrees = 0;
8514       RECUR (FOR_INIT_STMT (t));
8515       RECUR (FOR_COND (t));
8516       RECUR (FOR_EXPR (t));
8517       s = d->continue_stmt;
8518       b = d->break_stmt;
8519       RECUR (FOR_BODY (t));
8520       d->continue_stmt = s;
8521       d->break_stmt = b;
8522       break;
8523 
8524     case RANGE_FOR_STMT:
8525       *walk_subtrees = 0;
8526       RECUR (RANGE_FOR_EXPR (t));
8527       s = d->continue_stmt;
8528       b = d->break_stmt;
8529       RECUR (RANGE_FOR_BODY (t));
8530       d->continue_stmt = s;
8531       d->break_stmt = b;
8532       break;
8533 
8534     case SWITCH_STMT:
8535       *walk_subtrees = 0;
8536       RECUR (SWITCH_STMT_COND (t));
8537       b = d->break_stmt;
8538       RECUR (SWITCH_STMT_BODY (t));
8539       d->break_stmt = b;
8540       break;
8541 #undef RECUR
8542 
8543     case STATEMENT_LIST:
8544     case CONSTRUCTOR:
8545       break;
8546 
8547     default:
8548       if (!EXPR_P (t))
8549           *walk_subtrees = 0;
8550       break;
8551     }
8552 
8553   return NULL_TREE;
8554 }
8555 
8556 /* Return true if T denotes a potentially constant expression.  Issue
8557    diagnostic as appropriate under control of FLAGS.  If WANT_RVAL is true,
8558    an lvalue-rvalue conversion is implied.  If NOW is true, we want to
8559    consider the expression in the current context, independent of constexpr
8560    substitution.
8561 
8562    C++0x [expr.const] used to say
8563 
8564    6 An expression is a potential constant expression if it is
8565      a constant expression where all occurrences of function
8566      parameters are replaced by arbitrary constant expressions
8567      of the appropriate type.
8568 
8569    2  A conditional expression is a constant expression unless it
8570       involves one of the following as a potentially evaluated
8571       subexpression (3.2), but subexpressions of logical AND (5.14),
8572       logical OR (5.15), and conditional (5.16) operations that are
8573       not evaluated are not considered.   */
8574 
8575 static bool
potential_constant_expression_1(tree t,bool want_rval,bool strict,bool now,tsubst_flags_t flags,tree * jump_target)8576 potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
8577                                          tsubst_flags_t flags, tree *jump_target)
8578 {
8579 #define RECUR(T,RV) \
8580   potential_constant_expression_1 ((T), (RV), strict, now, flags, jump_target)
8581 
8582   enum { any = false, rval = true };
8583   int i;
8584   tree tmp;
8585 
8586   if (t == error_mark_node)
8587     return false;
8588   if (t == NULL_TREE)
8589     return true;
8590   location_t loc = cp_expr_loc_or_input_loc (t);
8591 
8592   if (*jump_target)
8593     /* If we are jumping, ignore everything.  This is simpler than the
8594        cxx_eval_constant_expression handling because we only need to be
8595        conservatively correct, and we don't necessarily have a constant value
8596        available, so we don't bother with switch tracking.  */
8597     return true;
8598 
8599   if (TREE_THIS_VOLATILE (t) && want_rval)
8600     {
8601       if (flags & tf_error)
8602           error_at (loc, "lvalue-to-rvalue conversion of a volatile lvalue "
8603                       "%qE with type %qT", t, TREE_TYPE (t));
8604       return false;
8605     }
8606   if (CONSTANT_CLASS_P (t))
8607     return true;
8608   if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED)
8609       && TREE_TYPE (t) == error_mark_node)
8610     return false;
8611 
8612   switch (TREE_CODE (t))
8613     {
8614     case FUNCTION_DECL:
8615     case BASELINK:
8616     case TEMPLATE_DECL:
8617     case OVERLOAD:
8618     case TEMPLATE_ID_EXPR:
8619     case LABEL_DECL:
8620     case CASE_LABEL_EXPR:
8621     case PREDICT_EXPR:
8622     case CONST_DECL:
8623     case SIZEOF_EXPR:
8624     case ALIGNOF_EXPR:
8625     case OFFSETOF_EXPR:
8626     case NOEXCEPT_EXPR:
8627     case TEMPLATE_PARM_INDEX:
8628     case TRAIT_EXPR:
8629     case IDENTIFIER_NODE:
8630     case USERDEF_LITERAL:
8631       /* We can see a FIELD_DECL in a pointer-to-member expression.  */
8632     case FIELD_DECL:
8633     case RESULT_DECL:
8634     case USING_DECL:
8635     case USING_STMT:
8636     case PLACEHOLDER_EXPR:
8637     case REQUIRES_EXPR:
8638     case STATIC_ASSERT:
8639     case DEBUG_BEGIN_STMT:
8640       return true;
8641 
8642     case RETURN_EXPR:
8643       if (!RECUR (TREE_OPERAND (t, 0), any))
8644           return false;
8645       /* FALLTHROUGH */
8646 
8647     case BREAK_STMT:
8648     case CONTINUE_STMT:
8649       *jump_target = t;
8650       return true;
8651 
8652     case PARM_DECL:
8653       if (now && want_rval)
8654           {
8655             tree type = TREE_TYPE (t);
8656             if ((processing_template_decl && !COMPLETE_TYPE_P (type))
8657                 || dependent_type_p (type)
8658                 || is_really_empty_class (type, /*ignore_vptr*/false))
8659               /* An empty class has no data to read.  */
8660               return true;
8661             if (flags & tf_error)
8662               error ("%qE is not a constant expression", t);
8663             return false;
8664           }
8665       return true;
8666 
8667     case AGGR_INIT_EXPR:
8668     case CALL_EXPR:
8669       /* -- an invocation of a function other than a constexpr function
8670             or a constexpr constructor.  */
8671       {
8672         tree fun = get_function_named_in_call (t);
8673         const int nargs = call_expr_nargs (t);
8674           i = 0;
8675 
8676           if (fun == NULL_TREE)
8677             {
8678               /* Reset to allow the function to continue past the end
8679                  of the block below.  Otherwise return early.  */
8680               bool bail = true;
8681 
8682               if (TREE_CODE (t) == CALL_EXPR
8683                     && CALL_EXPR_FN (t) == NULL_TREE)
8684                 switch (CALL_EXPR_IFN (t))
8685                     {
8686                     /* These should be ignored, they are optimized away from
8687                        constexpr functions.  */
8688                     case IFN_UBSAN_NULL:
8689                     case IFN_UBSAN_BOUNDS:
8690                     case IFN_UBSAN_VPTR:
8691                     case IFN_FALLTHROUGH:
8692                       return true;
8693 
8694                     case IFN_ADD_OVERFLOW:
8695                     case IFN_SUB_OVERFLOW:
8696                     case IFN_MUL_OVERFLOW:
8697                     case IFN_LAUNDER:
8698                     case IFN_VEC_CONVERT:
8699                       bail = false;
8700                       break;
8701 
8702                     default:
8703                       break;
8704                     }
8705 
8706               if (bail)
8707                 {
8708                     /* fold_call_expr can't do anything with IFN calls.  */
8709                     if (flags & tf_error)
8710                       error_at (loc, "call to internal function %qE", t);
8711                     return false;
8712                 }
8713             }
8714 
8715           if (fun && is_overloaded_fn (fun))
8716             {
8717               if (TREE_CODE (fun) == FUNCTION_DECL)
8718                 {
8719                     if (builtin_valid_in_constant_expr_p (fun))
8720                       return true;
8721                     if (!maybe_constexpr_fn (fun)
8722                         /* Allow any built-in function; if the expansion
8723                            isn't constant, we'll deal with that then.  */
8724                         && !fndecl_built_in_p (fun)
8725                         /* In C++20, replaceable global allocation functions
8726                            are constant expressions.  */
8727                         && (!cxx_replaceable_global_alloc_fn (fun)
8728                               || TREE_CODE (t) != CALL_EXPR
8729                               || (!CALL_FROM_NEW_OR_DELETE_P (t)
8730                                   && (current_function_decl == NULL_TREE
8731                                         || !is_std_allocator_allocate
8732                                                             (current_function_decl))))
8733                         /* Allow placement new in std::construct_at.  */
8734                         && (!cxx_placement_new_fn (fun)
8735                               || TREE_CODE (t) != CALL_EXPR
8736                               || current_function_decl == NULL_TREE
8737                               || !is_std_construct_at (current_function_decl))
8738                         && !cxx_dynamic_cast_fn_p (fun))
8739                       {
8740                         if (flags & tf_error)
8741                           {
8742                               error_at (loc, "call to non-%<constexpr%> function %qD",
8743                                           fun);
8744                               explain_invalid_constexpr_fn (fun);
8745                           }
8746                         return false;
8747                       }
8748                     /* A call to a non-static member function takes the address
8749                        of the object as the first argument.  But in a constant
8750                        expression the address will be folded away, so look
8751                        through it now.  */
8752                     if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
8753                         && !DECL_CONSTRUCTOR_P (fun))
8754                       {
8755                         tree x = get_nth_callarg (t, 0);
8756                         if (is_this_parameter (x))
8757                           return true;
8758                         /* Don't require an immediately constant value, as
8759                            constexpr substitution might not use the value.  */
8760                         bool sub_now = false;
8761                         if (!potential_constant_expression_1 (x, rval, strict,
8762                                                                         sub_now, flags,
8763                                                                         jump_target))
8764                           return false;
8765                         i = 1;
8766                       }
8767                 }
8768               else
8769                 {
8770                     if (!RECUR (fun, true))
8771                       return false;
8772                     fun = get_first_fn (fun);
8773                 }
8774               /* Skip initial arguments to base constructors.  */
8775               if (DECL_BASE_CONSTRUCTOR_P (fun))
8776                 i = num_artificial_parms_for (fun);
8777               fun = DECL_ORIGIN (fun);
8778             }
8779           else if (fun)
8780           {
8781               if (TREE_TYPE (fun)
8782                     && FUNCTION_POINTER_TYPE_P (TREE_TYPE (fun)))
8783                 want_rval = rval;
8784               else
8785                 want_rval = any;
8786               if (RECUR (fun, want_rval))
8787                 /* Might end up being a constant function pointer.  But it
8788                      could also be a function object with constexpr op(), so
8789                      we pass 'any' so that the underlying VAR_DECL is deemed
8790                      as potentially-constant even though it wasn't declared
8791                      constexpr.  */;
8792               else
8793                 return false;
8794           }
8795         for (; i < nargs; ++i)
8796           {
8797             tree x = get_nth_callarg (t, i);
8798               /* In a template, reference arguments haven't been converted to
8799                  REFERENCE_TYPE and we might not even know if the parameter
8800                  is a reference, so accept lvalue constants too.  */
8801               bool rv = processing_template_decl ? any : rval;
8802               /* Don't require an immediately constant value, as constexpr
8803                  substitution might not use the value of the argument.  */
8804               bool sub_now = false;
8805               if (!potential_constant_expression_1 (x, rv, strict,
8806                                                               sub_now, flags, jump_target))
8807                 return false;
8808           }
8809         return true;
8810       }
8811 
8812     case NON_LVALUE_EXPR:
8813       /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
8814             -- an lvalue of integral type that refers to a non-volatile
8815                const variable or static data member initialized with
8816                constant expressions, or
8817 
8818             -- an lvalue of literal type that refers to non-volatile
8819                object defined with constexpr, or that refers to a
8820                sub-object of such an object;  */
8821       return RECUR (TREE_OPERAND (t, 0), rval);
8822 
8823     case VAR_DECL:
8824       if (DECL_HAS_VALUE_EXPR_P (t))
8825           {
8826             if (now && is_normal_capture_proxy (t))
8827               {
8828                 /* -- in a lambda-expression, a reference to this or to a
8829                      variable with automatic storage duration defined outside that
8830                      lambda-expression, where the reference would be an
8831                      odr-use.  */
8832 
8833                 if (want_rval)
8834                     /* Since we're doing an lvalue-rvalue conversion, this might
8835                        not be an odr-use, so evaluate the variable directly. */
8836                     return RECUR (DECL_CAPTURED_VARIABLE (t), rval);
8837 
8838                 if (flags & tf_error)
8839                     {
8840                       tree cap = DECL_CAPTURED_VARIABLE (t);
8841                       error ("lambda capture of %qE is not a constant expression",
8842                                cap);
8843                       if (decl_constant_var_p (cap))
8844                         inform (input_location, "because it is used as a glvalue");
8845                     }
8846                 return false;
8847               }
8848             /* Treat __PRETTY_FUNCTION__ inside a template function as
8849                potentially-constant.  */
8850             else if (DECL_PRETTY_FUNCTION_P (t)
8851                        && DECL_VALUE_EXPR (t) == error_mark_node)
8852               return true;
8853             return RECUR (DECL_VALUE_EXPR (t), rval);
8854           }
8855       if (want_rval
8856             && (now || !var_in_maybe_constexpr_fn (t))
8857             && !type_dependent_expression_p (t)
8858             && !decl_maybe_constant_var_p (t)
8859             && (strict
8860                 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
8861                 || (DECL_INITIAL (t)
8862                       && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t)))
8863             && COMPLETE_TYPE_P (TREE_TYPE (t))
8864             && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
8865         {
8866           if (flags & tf_error)
8867               non_const_var_error (loc, t);
8868           return false;
8869         }
8870       return true;
8871 
8872     case NOP_EXPR:
8873       if (REINTERPRET_CAST_P (t))
8874           {
8875             if (flags & tf_error)
8876               error_at (loc, "%<reinterpret_cast%> is not a constant expression");
8877             return false;
8878           }
8879       /* FALLTHRU */
8880     case CONVERT_EXPR:
8881     case VIEW_CONVERT_EXPR:
8882       /* -- a reinterpret_cast.  FIXME not implemented, and this rule
8883            may change to something more specific to type-punning (DR 1312).  */
8884       {
8885         tree from = TREE_OPERAND (t, 0);
8886           if (location_wrapper_p (t))
8887             return (RECUR (from, want_rval));
8888           if (INDIRECT_TYPE_P (TREE_TYPE (t)))
8889             {
8890               STRIP_ANY_LOCATION_WRAPPER (from);
8891               if (TREE_CODE (from) == INTEGER_CST
8892                     && !integer_zerop (from))
8893                 {
8894                     if (flags & tf_error)
8895                       error_at (loc,
8896                                   "%<reinterpret_cast%> from integer to pointer");
8897                     return false;
8898                 }
8899             }
8900         return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
8901       }
8902 
8903     case ADDRESSOF_EXPR:
8904       /* This is like ADDR_EXPR, except it won't form pointer-to-member.  */
8905       t = TREE_OPERAND (t, 0);
8906       goto handle_addr_expr;
8907 
8908     case ADDR_EXPR:
8909       /* -- a unary operator & that is applied to an lvalue that
8910             designates an object with thread or automatic storage
8911             duration;  */
8912       t = TREE_OPERAND (t, 0);
8913 
8914       if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
8915           /* A pointer-to-member constant.  */
8916           return true;
8917 
8918     handle_addr_expr:
8919 #if 0
8920       /* FIXME adjust when issue 1197 is fully resolved.  For now don't do
8921          any checking here, as we might dereference the pointer later.  If
8922          we remove this code, also remove check_automatic_or_tls.  */
8923       i = check_automatic_or_tls (t);
8924       if (i == ck_ok)
8925           return true;
8926       if (i == ck_bad)
8927         {
8928           if (flags & tf_error)
8929             error ("address-of an object %qE with thread local or "
8930                    "automatic storage is not a constant expression", t);
8931           return false;
8932         }
8933 #endif
8934       return RECUR (t, any);
8935 
8936     case COMPONENT_REF:
8937     case ARROW_EXPR:
8938     case OFFSET_REF:
8939       /* -- a class member access unless its postfix-expression is
8940             of literal type or of pointer to literal type.  */
8941       /* This test would be redundant, as it follows from the
8942            postfix-expression being a potential constant expression.  */
8943       if (type_unknown_p (t))
8944           return true;
8945       if (is_overloaded_fn (t))
8946           /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
8947              which uses ob as an lvalue.  */
8948           want_rval = false;
8949       gcc_fallthrough ();
8950 
8951     case REALPART_EXPR:
8952     case IMAGPART_EXPR:
8953     case BIT_FIELD_REF:
8954       return RECUR (TREE_OPERAND (t, 0), want_rval);
8955 
8956     case EXPR_PACK_EXPANSION:
8957       return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
8958 
8959     case INDIRECT_REF:
8960       {
8961         tree x = TREE_OPERAND (t, 0);
8962         STRIP_NOPS (x);
8963         if (is_this_parameter (x) && !is_capture_proxy (x))
8964             {
8965               if (now || !var_in_maybe_constexpr_fn (x))
8966                 {
8967                     if (flags & tf_error)
8968                       error_at (loc, "use of %<this%> in a constant expression");
8969                     return false;
8970                 }
8971               return true;
8972             }
8973           return RECUR (x, rval);
8974       }
8975 
8976     case STATEMENT_LIST:
8977       for (tree stmt : tsi_range (t))
8978           if (!RECUR (stmt, any))
8979             return false;
8980       return true;
8981 
8982     case MODIFY_EXPR:
8983       if (cxx_dialect < cxx14)
8984           goto fail;
8985       if (!RECUR (TREE_OPERAND (t, 0), any))
8986           return false;
8987       /* Just ignore clobbers.  */
8988       if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
8989           return true;
8990       if (!RECUR (TREE_OPERAND (t, 1), rval))
8991           return false;
8992       return true;
8993 
8994     case MODOP_EXPR:
8995       if (cxx_dialect < cxx14)
8996           goto fail;
8997       if (!RECUR (TREE_OPERAND (t, 0), rval))
8998           return false;
8999       if (!RECUR (TREE_OPERAND (t, 2), rval))
9000           return false;
9001       return true;
9002 
9003     case DO_STMT:
9004       if (!RECUR (DO_COND (t), rval))
9005           return false;
9006       if (!RECUR (DO_BODY (t), any))
9007           return false;
9008       if (breaks (jump_target) || continues (jump_target))
9009           *jump_target = NULL_TREE;
9010       return true;
9011 
9012     case FOR_STMT:
9013       if (!RECUR (FOR_INIT_STMT (t), any))
9014           return false;
9015       tmp = FOR_COND (t);
9016       if (!RECUR (tmp, rval))
9017           return false;
9018       if (tmp)
9019           {
9020             if (!processing_template_decl)
9021               tmp = cxx_eval_outermost_constant_expr (tmp, true);
9022             /* If we couldn't evaluate the condition, it might not ever be
9023                true.  */
9024             if (!integer_onep (tmp))
9025               {
9026                 /* Before returning true, check if the for body can contain
9027                      a return.  */
9028                 hash_set<tree> pset;
9029                 check_for_return_continue_data data = { &pset, NULL_TREE,
9030                                                                   NULL_TREE };
9031                 if (tree ret_expr
9032                       = cp_walk_tree (&FOR_BODY (t), check_for_return_continue,
9033                                           &data, &pset))
9034                     *jump_target = ret_expr;
9035                 return true;
9036               }
9037           }
9038       if (!RECUR (FOR_EXPR (t), any))
9039           return false;
9040       if (!RECUR (FOR_BODY (t), any))
9041           return false;
9042       if (breaks (jump_target) || continues (jump_target))
9043           *jump_target = NULL_TREE;
9044       return true;
9045 
9046     case RANGE_FOR_STMT:
9047       if (!RECUR (RANGE_FOR_INIT_STMT (t), any))
9048           return false;
9049       if (!RECUR (RANGE_FOR_EXPR (t), any))
9050           return false;
9051       if (!RECUR (RANGE_FOR_BODY (t), any))
9052           return false;
9053       if (breaks (jump_target) || continues (jump_target))
9054           *jump_target = NULL_TREE;
9055       return true;
9056 
9057     case WHILE_STMT:
9058       tmp = WHILE_COND (t);
9059       if (!RECUR (tmp, rval))
9060           return false;
9061       if (!processing_template_decl)
9062           tmp = cxx_eval_outermost_constant_expr (tmp, true);
9063       /* If we couldn't evaluate the condition, it might not ever be true.  */
9064       if (!integer_onep (tmp))
9065           {
9066             /* Before returning true, check if the while body can contain
9067                a return.  */
9068             hash_set<tree> pset;
9069             check_for_return_continue_data data = { &pset, NULL_TREE,
9070                                                               NULL_TREE  };
9071             if (tree ret_expr
9072                 = cp_walk_tree (&WHILE_BODY (t), check_for_return_continue,
9073                                     &data, &pset))
9074               *jump_target = ret_expr;
9075             return true;
9076           }
9077       if (!RECUR (WHILE_BODY (t), any))
9078           return false;
9079       if (breaks (jump_target) || continues (jump_target))
9080           *jump_target = NULL_TREE;
9081       return true;
9082 
9083     case SWITCH_STMT:
9084       if (!RECUR (SWITCH_STMT_COND (t), rval))
9085           return false;
9086       /* FIXME we don't check SWITCH_STMT_BODY currently, because even
9087            unreachable labels would be checked and it is enough if there is
9088            a single switch cond value for which it is a valid constant
9089            expression.  We need to check if there are any RETURN_EXPRs
9090            or CONTINUE_STMTs inside of the body though, as in that case
9091            we need to set *jump_target.  */
9092       else
9093           {
9094             hash_set<tree> pset;
9095             check_for_return_continue_data data = { &pset, NULL_TREE,
9096                                                               NULL_TREE };
9097             if (tree ret_expr
9098                 = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue,
9099                                     &data, &pset))
9100               /* The switch might return.  */
9101               *jump_target = ret_expr;
9102             else if (data.continue_stmt)
9103               /* The switch can't return, but might continue.  */
9104               *jump_target = data.continue_stmt;
9105           }
9106       return true;
9107 
9108     case STMT_EXPR:
9109       return RECUR (STMT_EXPR_STMT (t), rval);
9110 
9111     case LAMBDA_EXPR:
9112       if (cxx_dialect >= cxx17)
9113           /* In C++17 lambdas can be constexpr, don't give up yet.  */
9114           return true;
9115       else if (flags & tf_error)
9116           error_at (loc, "lambda-expression is not a constant expression "
9117                       "before C++17");
9118       return false;
9119 
9120     case DYNAMIC_CAST_EXPR:
9121     case PSEUDO_DTOR_EXPR:
9122     case NEW_EXPR:
9123     case VEC_NEW_EXPR:
9124     case DELETE_EXPR:
9125     case VEC_DELETE_EXPR:
9126     case THROW_EXPR:
9127     case OMP_PARALLEL:
9128     case OMP_TASK:
9129     case OMP_FOR:
9130     case OMP_SIMD:
9131     case OMP_DISTRIBUTE:
9132     case OMP_TASKLOOP:
9133     case OMP_LOOP:
9134     case OMP_TEAMS:
9135     case OMP_TARGET_DATA:
9136     case OMP_TARGET:
9137     case OMP_SECTIONS:
9138     case OMP_ORDERED:
9139     case OMP_CRITICAL:
9140     case OMP_SINGLE:
9141     case OMP_SCAN:
9142     case OMP_SCOPE:
9143     case OMP_SECTION:
9144     case OMP_MASTER:
9145     case OMP_MASKED:
9146     case OMP_TASKGROUP:
9147     case OMP_TARGET_UPDATE:
9148     case OMP_TARGET_ENTER_DATA:
9149     case OMP_TARGET_EXIT_DATA:
9150     case OMP_ATOMIC:
9151     case OMP_ATOMIC_READ:
9152     case OMP_ATOMIC_CAPTURE_OLD:
9153     case OMP_ATOMIC_CAPTURE_NEW:
9154     case OMP_DEPOBJ:
9155     case OACC_PARALLEL:
9156     case OACC_KERNELS:
9157     case OACC_SERIAL:
9158     case OACC_DATA:
9159     case OACC_HOST_DATA:
9160     case OACC_LOOP:
9161     case OACC_CACHE:
9162     case OACC_DECLARE:
9163     case OACC_ENTER_DATA:
9164     case OACC_EXIT_DATA:
9165     case OACC_UPDATE:
9166       /* GCC internal stuff.  */
9167     case VA_ARG_EXPR:
9168     case TRANSACTION_EXPR:
9169     case AT_ENCODE_EXPR:
9170     fail:
9171       if (flags & tf_error)
9172           error_at (loc, "expression %qE is not a constant expression", t);
9173       return false;
9174 
9175     case ASM_EXPR:
9176       if (flags & tf_error)
9177           inline_asm_in_constexpr_error (loc);
9178       return false;
9179 
9180     case OBJ_TYPE_REF:
9181       if (cxx_dialect >= cxx20)
9182           /* In C++20 virtual calls can be constexpr, don't give up yet.  */
9183           return true;
9184       else if (flags & tf_error)
9185           error_at (loc,
9186                       "virtual functions cannot be %<constexpr%> before C++20");
9187       return false;
9188 
9189     case TYPEID_EXPR:
9190       /* In C++20, a typeid expression whose operand is of polymorphic
9191            class type can be constexpr.  */
9192       {
9193         tree e = TREE_OPERAND (t, 0);
9194           if (cxx_dialect < cxx20
9195               && strict
9196               && !TYPE_P (e)
9197               && !type_dependent_expression_p (e)
9198               && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
9199           {
9200             if (flags & tf_error)
9201                 error_at (loc, "%<typeid%> is not a constant expression "
9202                               "because %qE is of polymorphic type", e);
9203             return false;
9204           }
9205         return true;
9206       }
9207 
9208     case POINTER_DIFF_EXPR:
9209     case MINUS_EXPR:
9210       want_rval = true;
9211       goto binary;
9212 
9213     case LT_EXPR:
9214     case LE_EXPR:
9215     case GT_EXPR:
9216     case GE_EXPR:
9217     case EQ_EXPR:
9218     case NE_EXPR:
9219     case SPACESHIP_EXPR:
9220       want_rval = true;
9221       goto binary;
9222 
9223     case PREINCREMENT_EXPR:
9224     case POSTINCREMENT_EXPR:
9225     case PREDECREMENT_EXPR:
9226     case POSTDECREMENT_EXPR:
9227       if (cxx_dialect < cxx14)
9228           goto fail;
9229       goto unary;
9230 
9231     case BIT_NOT_EXPR:
9232       /* A destructor.  */
9233       if (TYPE_P (TREE_OPERAND (t, 0)))
9234           return true;
9235       /* fall through.  */
9236 
9237     case CONJ_EXPR:
9238     case SAVE_EXPR:
9239     case FIX_TRUNC_EXPR:
9240     case FLOAT_EXPR:
9241     case NEGATE_EXPR:
9242     case ABS_EXPR:
9243     case ABSU_EXPR:
9244     case TRUTH_NOT_EXPR:
9245     case FIXED_CONVERT_EXPR:
9246     case UNARY_PLUS_EXPR:
9247     case UNARY_LEFT_FOLD_EXPR:
9248     case UNARY_RIGHT_FOLD_EXPR:
9249     unary:
9250       return RECUR (TREE_OPERAND (t, 0), rval);
9251 
9252     case CAST_EXPR:
9253     case CONST_CAST_EXPR:
9254     case STATIC_CAST_EXPR:
9255     case REINTERPRET_CAST_EXPR:
9256     case IMPLICIT_CONV_EXPR:
9257       if (cxx_dialect < cxx11
9258             && !dependent_type_p (TREE_TYPE (t))
9259             && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
9260           /* In C++98, a conversion to non-integral type can't be part of a
9261              constant expression.  */
9262           {
9263             if (flags & tf_error)
9264               error_at (loc,
9265                           "cast to non-integral type %qT in a constant expression",
9266                           TREE_TYPE (t));
9267             return false;
9268           }
9269       /* This might be a conversion from a class to a (potentially) literal
9270            type.  Let's consider it potentially constant since the conversion
9271            might be a constexpr user-defined conversion.  */
9272       else if (cxx_dialect >= cxx11
9273                  && (dependent_type_p (TREE_TYPE (t))
9274                        || !COMPLETE_TYPE_P (TREE_TYPE (t))
9275                        || literal_type_p (TREE_TYPE (t)))
9276                  && TREE_OPERAND (t, 0))
9277           {
9278             tree type = TREE_TYPE (TREE_OPERAND (t, 0));
9279             /* If this is a dependent type, it could end up being a class
9280                with conversions.  */
9281             if (type == NULL_TREE || WILDCARD_TYPE_P (type))
9282               return true;
9283             /* Or a non-dependent class which has conversions.  */
9284             else if (CLASS_TYPE_P (type)
9285                        && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type)))
9286               return true;
9287           }
9288 
9289       return (RECUR (TREE_OPERAND (t, 0),
9290                          !TYPE_REF_P (TREE_TYPE (t))));
9291 
9292     case BIND_EXPR:
9293       return RECUR (BIND_EXPR_BODY (t), want_rval);
9294 
9295     case NON_DEPENDENT_EXPR:
9296       /* Treat NON_DEPENDENT_EXPR as non-constant: it's not handled by
9297            constexpr evaluation or tsubst, so fold_non_dependent_expr can't
9298            do anything useful with it.  And we shouldn't see it in a context
9299            where a constant expression is strictly required, hence the assert.  */
9300       gcc_checking_assert (!(flags & tf_error));
9301       return false;
9302 
9303     case CLEANUP_POINT_EXPR:
9304     case MUST_NOT_THROW_EXPR:
9305     case TRY_CATCH_EXPR:
9306     case TRY_BLOCK:
9307     case EH_SPEC_BLOCK:
9308     case EXPR_STMT:
9309     case PAREN_EXPR:
9310       /* For convenience.  */
9311     case LOOP_EXPR:
9312     case EXIT_EXPR:
9313       return RECUR (TREE_OPERAND (t, 0), want_rval);
9314 
9315     case DECL_EXPR:
9316       tmp = DECL_EXPR_DECL (t);
9317       if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp))
9318           {
9319             if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp))
9320               {
9321                 if (flags & tf_error)
9322                     error_at (DECL_SOURCE_LOCATION (tmp), "%qD defined "
9323                                 "%<thread_local%> in %<constexpr%> context", tmp);
9324                 return false;
9325               }
9326             else if (TREE_STATIC (tmp))
9327               {
9328                 if (flags & tf_error)
9329                     error_at (DECL_SOURCE_LOCATION (tmp), "%qD defined "
9330                                 "%<static%> in %<constexpr%> context", tmp);
9331                 return false;
9332               }
9333             else if (!check_for_uninitialized_const_var
9334                        (tmp, /*constexpr_context_p=*/true, flags))
9335               return false;
9336           }
9337       return RECUR (tmp, want_rval);
9338 
9339     case TRY_FINALLY_EXPR:
9340       return (RECUR (TREE_OPERAND (t, 0), want_rval)
9341                 && RECUR (TREE_OPERAND (t, 1), any));
9342 
9343     case SCOPE_REF:
9344       return RECUR (TREE_OPERAND (t, 1), want_rval);
9345 
9346     case TARGET_EXPR:
9347       if (!TARGET_EXPR_DIRECT_INIT_P (t)
9348             && !literal_type_p (TREE_TYPE (t)))
9349           {
9350             if (flags & tf_error)
9351               {
9352                 auto_diagnostic_group d;
9353                 error_at (loc, "temporary of non-literal type %qT in a "
9354                               "constant expression", TREE_TYPE (t));
9355                 explain_non_literal_class (TREE_TYPE (t));
9356               }
9357             return false;
9358           }
9359       /* FALLTHRU */
9360     case INIT_EXPR:
9361       return RECUR (TREE_OPERAND (t, 1), rval);
9362 
9363     case CONSTRUCTOR:
9364       {
9365         vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
9366         constructor_elt *ce;
9367         for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
9368             if (!RECUR (ce->value, want_rval))
9369               return false;
9370           return true;
9371       }
9372 
9373     case TREE_LIST:
9374       {
9375           gcc_assert (TREE_PURPOSE (t) == NULL_TREE
9376                         || DECL_P (TREE_PURPOSE (t)));
9377           if (!RECUR (TREE_VALUE (t), want_rval))
9378             return false;
9379           if (TREE_CHAIN (t) == NULL_TREE)
9380             return true;
9381           return RECUR (TREE_CHAIN (t), want_rval);
9382       }
9383 
9384     case TRUNC_DIV_EXPR:
9385     case CEIL_DIV_EXPR:
9386     case FLOOR_DIV_EXPR:
9387     case ROUND_DIV_EXPR:
9388     case TRUNC_MOD_EXPR:
9389     case CEIL_MOD_EXPR:
9390     case ROUND_MOD_EXPR:
9391       {
9392           tree denom = TREE_OPERAND (t, 1);
9393           if (!RECUR (denom, rval))
9394             return false;
9395           /* We can't call cxx_eval_outermost_constant_expr on an expression
9396              that hasn't been through instantiate_non_dependent_expr yet.  */
9397           if (!processing_template_decl)
9398             denom = cxx_eval_outermost_constant_expr (denom, true);
9399           if (integer_zerop (denom))
9400             {
9401               if (flags & tf_error)
9402                 error ("division by zero is not a constant expression");
9403               return false;
9404             }
9405           else
9406             {
9407               want_rval = true;
9408               return RECUR (TREE_OPERAND (t, 0), want_rval);
9409             }
9410       }
9411 
9412     case COMPOUND_EXPR:
9413       {
9414           /* check_return_expr sometimes wraps a TARGET_EXPR in a
9415              COMPOUND_EXPR; don't get confused.  */
9416           tree op0 = TREE_OPERAND (t, 0);
9417           tree op1 = TREE_OPERAND (t, 1);
9418           STRIP_NOPS (op1);
9419           if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
9420             return RECUR (op0, want_rval);
9421           else
9422             goto binary;
9423       }
9424 
9425       /* If the first operand is the non-short-circuit constant, look at
9426            the second operand; otherwise we only care about the first one for
9427            potentiality.  */
9428     case TRUTH_AND_EXPR:
9429     case TRUTH_ANDIF_EXPR:
9430       tmp = boolean_true_node;
9431       goto truth;
9432     case TRUTH_OR_EXPR:
9433     case TRUTH_ORIF_EXPR:
9434       tmp = boolean_false_node;
9435     truth:
9436       {
9437           tree op0 = TREE_OPERAND (t, 0);
9438           tree op1 = TREE_OPERAND (t, 1);
9439           if (!RECUR (op0, rval))
9440             return false;
9441           if (!(flags & tf_error) && RECUR (op1, rval))
9442             /* When quiet, try to avoid expensive trial evaluation by first
9443                checking potentiality of the second operand.  */
9444             return true;
9445           if (!processing_template_decl)
9446             op0 = cxx_eval_outermost_constant_expr (op0, true);
9447           if (tree_int_cst_equal (op0, tmp))
9448             return (flags & tf_error) ? RECUR (op1, rval) : false;
9449           else
9450             return true;
9451       }
9452 
9453     case PLUS_EXPR:
9454     case MULT_EXPR:
9455     case POINTER_PLUS_EXPR:
9456     case RDIV_EXPR:
9457     case EXACT_DIV_EXPR:
9458     case MIN_EXPR:
9459     case MAX_EXPR:
9460     case LSHIFT_EXPR:
9461     case RSHIFT_EXPR:
9462     case LROTATE_EXPR:
9463     case RROTATE_EXPR:
9464     case BIT_IOR_EXPR:
9465     case BIT_XOR_EXPR:
9466     case BIT_AND_EXPR:
9467     case TRUTH_XOR_EXPR:
9468     case UNORDERED_EXPR:
9469     case ORDERED_EXPR:
9470     case UNLT_EXPR:
9471     case UNLE_EXPR:
9472     case UNGT_EXPR:
9473     case UNGE_EXPR:
9474     case UNEQ_EXPR:
9475     case LTGT_EXPR:
9476     case RANGE_EXPR:
9477     case COMPLEX_EXPR:
9478       want_rval = true;
9479       /* Fall through.  */
9480     case ARRAY_REF:
9481     case ARRAY_RANGE_REF:
9482     case MEMBER_REF:
9483     case DOTSTAR_EXPR:
9484     case MEM_REF:
9485     case BINARY_LEFT_FOLD_EXPR:
9486     case BINARY_RIGHT_FOLD_EXPR:
9487     binary:
9488       for (i = 0; i < 2; ++i)
9489           if (!RECUR (TREE_OPERAND (t, i), want_rval))
9490             return false;
9491       return true;
9492 
9493     case VEC_PERM_EXPR:
9494      for (i = 0; i < 3; ++i)
9495       if (!RECUR (TREE_OPERAND (t, i), true))
9496           return false;
9497      return true;
9498 
9499     case COND_EXPR:
9500       if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20)
9501           {
9502             if (flags & tf_error)
9503               error_at (loc, "%<delete[]%> is not a constant expression");
9504             return false;
9505           }
9506       /* Fall through.  */
9507     case IF_STMT:
9508     case VEC_COND_EXPR:
9509       /* If the condition is a known constant, we know which of the legs we
9510            care about; otherwise we only require that the condition and
9511            either of the legs be potentially constant.  */
9512       tmp = TREE_OPERAND (t, 0);
9513       if (!RECUR (tmp, rval))
9514           return false;
9515       if (!processing_template_decl)
9516           tmp = cxx_eval_outermost_constant_expr (tmp, true);
9517       /* potential_constant_expression* isn't told if it is called for
9518            manifestly_const_eval or not, so for consteval if always
9519            process both branches as if the condition is not a known
9520            constant.  */
9521       if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t))
9522           {
9523             if (integer_zerop (tmp))
9524               return RECUR (TREE_OPERAND (t, 2), want_rval);
9525             else if (TREE_CODE (tmp) == INTEGER_CST)
9526               return RECUR (TREE_OPERAND (t, 1), want_rval);
9527           }
9528       tmp = *jump_target;
9529       for (i = 1; i < 3; ++i)
9530           {
9531             tree this_jump_target = tmp;
9532             if (potential_constant_expression_1 (TREE_OPERAND (t, i),
9533                                                          want_rval, strict, now,
9534                                                          tf_none, &this_jump_target))
9535               {
9536                 if (returns (&this_jump_target))
9537                     *jump_target = this_jump_target;
9538                 else if (!returns (jump_target))
9539                     {
9540                       if (breaks (&this_jump_target)
9541                           || continues (&this_jump_target))
9542                         *jump_target = this_jump_target;
9543                       if (i == 1)
9544                         {
9545                           /* If the then branch is potentially constant, but
9546                                does not return, check if the else branch
9547                                couldn't return, break or continue.  */
9548                           hash_set<tree> pset;
9549                           check_for_return_continue_data data = { &pset, NULL_TREE,
9550                                                                             NULL_TREE };
9551                           if (tree ret_expr
9552                               = cp_walk_tree (&TREE_OPERAND (t, 2),
9553                                                   check_for_return_continue, &data,
9554                                                   &pset))
9555                               *jump_target = ret_expr;
9556                           else if (*jump_target == NULL_TREE)
9557                               {
9558                                 if (data.continue_stmt)
9559                                   *jump_target = data.continue_stmt;
9560                                 else if (data.break_stmt)
9561                                   *jump_target = data.break_stmt;
9562                               }
9563                         }
9564                     }
9565                 return true;
9566               }
9567           }
9568       if (flags & tf_error)
9569           {
9570             if (TREE_CODE (t) == IF_STMT)
9571               error_at (loc, "neither branch of %<if%> is a constant expression");
9572             else
9573               error_at (loc, "expression %qE is not a constant expression", t);
9574           }
9575       return false;
9576 
9577     case VEC_INIT_EXPR:
9578       if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
9579           return true;
9580       if (flags & tf_error)
9581           {
9582             error_at (loc, "non-constant array initialization");
9583             diagnose_non_constexpr_vec_init (t);
9584           }
9585       return false;
9586 
9587     case TYPE_DECL:
9588     case TAG_DEFN:
9589       /* We can see these in statement-expressions.  */
9590       return true;
9591 
9592     case CLEANUP_STMT:
9593       if (!RECUR (CLEANUP_BODY (t), any))
9594           return false;
9595       if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any))
9596           return false;
9597       return true;
9598 
9599     case EMPTY_CLASS_EXPR:
9600       return true;
9601 
9602     case GOTO_EXPR:
9603       {
9604           tree *target = &TREE_OPERAND (t, 0);
9605           /* Gotos representing break, continue and cdtor return are OK.  */
9606           if (breaks (target) || continues (target) || returns (target))
9607             {
9608               *jump_target = *target;
9609               return true;
9610             }
9611           if (flags & tf_error)
9612             error_at (loc, "%<goto%> is not a constant expression");
9613           return false;
9614       }
9615 
9616     case LABEL_EXPR:
9617       t = LABEL_EXPR_LABEL (t);
9618       if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23)
9619           return true;
9620       else if (flags & tf_error)
9621           error_at (loc, "label definition in %<constexpr%> function only "
9622                            "available with %<-std=c++2b%> or %<-std=gnu++2b%>");
9623       return false;
9624 
9625     case ANNOTATE_EXPR:
9626       return RECUR (TREE_OPERAND (t, 0), rval);
9627 
9628     case BIT_CAST_EXPR:
9629       return RECUR (TREE_OPERAND (t, 0), rval);
9630 
9631     /* Coroutine await, yield and return expressions are not.  */
9632     case CO_AWAIT_EXPR:
9633     case CO_YIELD_EXPR:
9634     case CO_RETURN_EXPR:
9635       return false;
9636 
9637     case NONTYPE_ARGUMENT_PACK:
9638       {
9639           tree args = ARGUMENT_PACK_ARGS (t);
9640           int len = TREE_VEC_LENGTH (args);
9641           for (int i = 0; i < len; ++i)
9642             if (!RECUR (TREE_VEC_ELT (args, i), any))
9643               return false;
9644           return true;
9645       }
9646 
9647     default:
9648       if (objc_non_constant_expr_p (t))
9649           return false;
9650 
9651       sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
9652       gcc_unreachable ();
9653       return false;
9654     }
9655 #undef RECUR
9656 }
9657 
9658 bool
potential_constant_expression_1(tree t,bool want_rval,bool strict,bool now,tsubst_flags_t flags)9659 potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
9660                                          tsubst_flags_t flags)
9661 {
9662   if (flags & tf_error)
9663     {
9664       /* Check potentiality quietly first, as that could be performed more
9665            efficiently in some cases (currently only for TRUTH_*_EXPR).  If
9666            that fails, replay the check noisily to give errors.  */
9667       flags &= ~tf_error;
9668       if (potential_constant_expression_1 (t, want_rval, strict, now, flags))
9669           return true;
9670       flags |= tf_error;
9671     }
9672 
9673   tree target = NULL_TREE;
9674   return potential_constant_expression_1 (t, want_rval, strict, now,
9675                                                     flags, &target);
9676 }
9677 
9678 /* The main entry point to the above.  */
9679 
9680 bool
potential_constant_expression(tree t)9681 potential_constant_expression (tree t)
9682 {
9683   return potential_constant_expression_1 (t, false, true, false, tf_none);
9684 }
9685 
9686 /* As above, but require a constant rvalue.  */
9687 
9688 bool
potential_rvalue_constant_expression(tree t)9689 potential_rvalue_constant_expression (tree t)
9690 {
9691   return potential_constant_expression_1 (t, true, true, false, tf_none);
9692 }
9693 
9694 /* Like above, but complain about non-constant expressions.  */
9695 
9696 bool
require_potential_constant_expression(tree t)9697 require_potential_constant_expression (tree t)
9698 {
9699   return potential_constant_expression_1 (t, false, true, false,
9700                                                     tf_warning_or_error);
9701 }
9702 
9703 /* Cross product of the above.  */
9704 
9705 bool
require_potential_rvalue_constant_expression(tree t)9706 require_potential_rvalue_constant_expression (tree t)
9707 {
9708   return potential_constant_expression_1 (t, true, true, false,
9709                                                     tf_warning_or_error);
9710 }
9711 
9712 /* Like above, but don't consider PARM_DECL a potential_constant_expression.  */
9713 
9714 bool
require_rvalue_constant_expression(tree t)9715 require_rvalue_constant_expression (tree t)
9716 {
9717   return potential_constant_expression_1 (t, true, true, true,
9718                                                     tf_warning_or_error);
9719 }
9720 
9721 /* Like potential_constant_expression, but don't consider possible constexpr
9722    substitution of the current function.  That is, PARM_DECL qualifies under
9723    potential_constant_expression, but not here.
9724 
9725    This is basically what you can check when any actual constant values might
9726    be value-dependent.  */
9727 
9728 bool
is_constant_expression(tree t)9729 is_constant_expression (tree t)
9730 {
9731   return potential_constant_expression_1 (t, false, true, true, tf_none);
9732 }
9733 
9734 /* As above, but expect an rvalue.  */
9735 
9736 bool
is_rvalue_constant_expression(tree t)9737 is_rvalue_constant_expression (tree t)
9738 {
9739   return potential_constant_expression_1 (t, true, true, true, tf_none);
9740 }
9741 
9742 /* Like above, but complain about non-constant expressions.  */
9743 
9744 bool
require_constant_expression(tree t)9745 require_constant_expression (tree t)
9746 {
9747   return potential_constant_expression_1 (t, false, true, true,
9748                                                     tf_warning_or_error);
9749 }
9750 
9751 /* Like is_constant_expression, but allow const variables that are not allowed
9752    under constexpr rules.  */
9753 
9754 bool
is_static_init_expression(tree t)9755 is_static_init_expression (tree t)
9756 {
9757   return potential_constant_expression_1 (t, false, false, true, tf_none);
9758 }
9759 
9760 /* Returns true if T is a potential constant expression that is not
9761    instantiation-dependent, and therefore a candidate for constant folding even
9762    in a template.  */
9763 
9764 bool
is_nondependent_constant_expression(tree t)9765 is_nondependent_constant_expression (tree t)
9766 {
9767   return (!type_unknown_p (t)
9768             && is_constant_expression (t)
9769             && !instantiation_dependent_expression_p (t));
9770 }
9771 
9772 /* Returns true if T is a potential static initializer expression that is not
9773    instantiation-dependent.  */
9774 
9775 bool
is_nondependent_static_init_expression(tree t)9776 is_nondependent_static_init_expression (tree t)
9777 {
9778   return (!type_unknown_p (t)
9779             && is_static_init_expression (t)
9780             && !instantiation_dependent_expression_p (t));
9781 }
9782 
9783 /* True iff FN is an implicitly constexpr function.  */
9784 
9785 bool
decl_implicit_constexpr_p(tree fn)9786 decl_implicit_constexpr_p (tree fn)
9787 {
9788   if (!(flag_implicit_constexpr
9789           && TREE_CODE (fn) == FUNCTION_DECL
9790           && DECL_DECLARED_CONSTEXPR_P (fn)))
9791     return false;
9792 
9793   if (DECL_CLONED_FUNCTION_P (fn))
9794     fn = DECL_CLONED_FUNCTION (fn);
9795 
9796   return (DECL_LANG_SPECIFIC (fn)
9797             && DECL_LANG_SPECIFIC (fn)->u.fn.implicit_constexpr);
9798 }
9799 
9800 /* Finalize constexpr processing after parsing.  */
9801 
9802 void
fini_constexpr(void)9803 fini_constexpr (void)
9804 {
9805   /* The contexpr call and fundef copies tables are no longer needed.  */
9806   constexpr_call_table = NULL;
9807   fundef_copies_table = NULL;
9808 }
9809 
9810 #include "gt-cp-constexpr.h"
9811