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