038c6f505cc69e8fcfc058a6b76b978ee7facc77
[platform/upstream/gcc.git] / gcc / cp / constexpr.c
1 /* Perform -*- C++ -*- constant expression evaluation, including calls to
2    constexpr functions.  These routines are used both during actual parsing
3    and during the instantiation of template functions.
4
5    Copyright (C) 1998-2015 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 "tree.h"
27 #include "cp-tree.h"
28 #include "alias.h"
29 #include "varasm.h"
30 #include "c-family/c-objc.h"
31 #include "tree-iterator.h"
32 #include "gimplify.h"
33 #include "builtins.h"
34 #include "tree-inline.h"
35 #include "ubsan.h"
36
37 static bool verify_constant (tree, bool, bool *, bool *);
38 #define VERIFY_CONSTANT(X)                                              \
39 do {                                                                    \
40   if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
41     return t;                                                           \
42  } while (0)
43
44 /* Returns true iff FUN is an instantiation of a constexpr function
45    template or a defaulted constexpr function.  */
46
47 bool
48 is_instantiation_of_constexpr (tree fun)
49 {
50   return ((DECL_TEMPLOID_INSTANTIATION (fun)
51            && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
52           || (DECL_DEFAULTED_FN (fun)
53               && DECL_DECLARED_CONSTEXPR_P (fun)));
54 }
55
56 /* Return true if T is a literal type.   */
57
58 bool
59 literal_type_p (tree t)
60 {
61   if (SCALAR_TYPE_P (t)
62       || VECTOR_TYPE_P (t)
63       || TREE_CODE (t) == REFERENCE_TYPE
64       || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
65     return true;
66   if (CLASS_TYPE_P (t))
67     {
68       t = complete_type (t);
69       gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
70       return CLASSTYPE_LITERAL_P (t);
71     }
72   if (TREE_CODE (t) == ARRAY_TYPE)
73     return literal_type_p (strip_array_types (t));
74   return false;
75 }
76
77 /* If DECL is a variable declared `constexpr', require its type
78    be literal.  Return the DECL if OK, otherwise NULL.  */
79
80 tree
81 ensure_literal_type_for_constexpr_object (tree decl)
82 {
83   tree type = TREE_TYPE (decl);
84   if (VAR_P (decl)
85       && (DECL_DECLARED_CONSTEXPR_P (decl)
86           || var_in_constexpr_fn (decl))
87       && !processing_template_decl)
88     {
89       tree stype = strip_array_types (type);
90       if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype)))
91         /* Don't complain here, we'll complain about incompleteness
92            when we try to initialize the variable.  */;
93       else if (!literal_type_p (type))
94         {
95           if (DECL_DECLARED_CONSTEXPR_P (decl))
96             {
97               error ("the type %qT of constexpr variable %qD is not literal",
98                      type, decl);
99               explain_non_literal_class (type);
100             }
101           else
102             {
103               if (!DECL_TEMPLATE_INSTANTIATION (current_function_decl))
104                 {
105                   error ("variable %qD of non-literal type %qT in %<constexpr%> "
106                          "function", decl, type);
107                   explain_non_literal_class (type);
108                 }
109               cp_function_chain->invalid_constexpr = true;
110             }
111           return NULL;
112         }
113     }
114   return decl;
115 }
116
117 /* Representation of entries in the constexpr function definition table.  */
118
119 struct GTY((for_user)) constexpr_fundef {
120   tree decl;
121   tree body;
122 };
123
124 struct constexpr_fundef_hasher : ggc_ptr_hash<constexpr_fundef>
125 {
126   static hashval_t hash (constexpr_fundef *);
127   static bool equal (constexpr_fundef *, constexpr_fundef *);
128 };
129
130 /* This table holds all constexpr function definitions seen in
131    the current translation unit.  */
132
133 static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table;
134
135 /* Utility function used for managing the constexpr function table.
136    Return true if the entries pointed to by P and Q are for the
137    same constexpr function.  */
138
139 inline bool
140 constexpr_fundef_hasher::equal (constexpr_fundef *lhs, constexpr_fundef *rhs)
141 {
142   return lhs->decl == rhs->decl;
143 }
144
145 /* Utility function used for managing the constexpr function table.
146    Return a hash value for the entry pointed to by Q.  */
147
148 inline hashval_t
149 constexpr_fundef_hasher::hash (constexpr_fundef *fundef)
150 {
151   return DECL_UID (fundef->decl);
152 }
153
154 /* Return a previously saved definition of function FUN.   */
155
156 static constexpr_fundef *
157 retrieve_constexpr_fundef (tree fun)
158 {
159   constexpr_fundef fundef = { NULL, NULL };
160   if (constexpr_fundef_table == NULL)
161     return NULL;
162
163   fundef.decl = fun;
164   return constexpr_fundef_table->find (&fundef);
165 }
166
167 /* Check whether the parameter and return types of FUN are valid for a
168    constexpr function, and complain if COMPLAIN.  */
169
170 static bool
171 is_valid_constexpr_fn (tree fun, bool complain)
172 {
173   bool ret = true;
174
175   if (DECL_INHERITED_CTOR_BASE (fun)
176       && TREE_CODE (fun) == TEMPLATE_DECL)
177     {
178       ret = false;
179       if (complain)
180         error ("inherited constructor %qD is not constexpr",
181                get_inherited_ctor (fun));
182     }
183   else
184     {
185       for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
186            parm != NULL_TREE; parm = TREE_CHAIN (parm))
187         if (!literal_type_p (TREE_TYPE (parm)))
188           {
189             ret = false;
190             if (complain)
191               {
192                 error ("invalid type for parameter %d of constexpr "
193                        "function %q+#D", DECL_PARM_INDEX (parm), fun);
194                 explain_non_literal_class (TREE_TYPE (parm));
195               }
196           }
197     }
198
199   if (!DECL_CONSTRUCTOR_P (fun))
200     {
201       tree rettype = TREE_TYPE (TREE_TYPE (fun));
202       if (!literal_type_p (rettype))
203         {
204           ret = false;
205           if (complain)
206             {
207               error ("invalid return type %qT of constexpr function %q+D",
208                      rettype, fun);
209               explain_non_literal_class (rettype);
210             }
211         }
212
213       if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
214           && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
215         {
216           ret = false;
217           if (complain)
218             {
219               error ("enclosing class of constexpr non-static member "
220                      "function %q+#D is not a literal type", fun);
221               explain_non_literal_class (DECL_CONTEXT (fun));
222             }
223         }
224     }
225   else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
226     {
227       ret = false;
228       if (complain)
229         error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
230     }
231
232   return ret;
233 }
234
235 /* Subroutine of build_data_member_initialization.  MEMBER is a COMPONENT_REF
236    for a member of an anonymous aggregate, INIT is the initializer for that
237    member, and VEC_OUTER is the vector of constructor elements for the class
238    whose constructor we are processing.  Add the initializer to the vector
239    and return true to indicate success.  */
240
241 static bool
242 build_anon_member_initialization (tree member, tree init,
243                                   vec<constructor_elt, va_gc> **vec_outer)
244 {
245   /* MEMBER presents the relevant fields from the inside out, but we need
246      to build up the initializer from the outside in so that we can reuse
247      previously built CONSTRUCTORs if this is, say, the second field in an
248      anonymous struct.  So we use a vec as a stack.  */
249   auto_vec<tree, 2> fields;
250   do
251     {
252       fields.safe_push (TREE_OPERAND (member, 1));
253       member = TREE_OPERAND (member, 0);
254     }
255   while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
256          && TREE_CODE (member) == COMPONENT_REF);
257
258   /* VEC has the constructor elements vector for the context of FIELD.
259      If FIELD is an anonymous aggregate, we will push inside it.  */
260   vec<constructor_elt, va_gc> **vec = vec_outer;
261   tree field;
262   while (field = fields.pop(),
263          ANON_AGGR_TYPE_P (TREE_TYPE (field)))
264     {
265       tree ctor;
266       /* If there is already an outer constructor entry for the anonymous
267          aggregate FIELD, use it; otherwise, insert one.  */
268       if (vec_safe_is_empty (*vec)
269           || (*vec)->last().index != field)
270         {
271           ctor = build_constructor (TREE_TYPE (field), NULL);
272           CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
273         }
274       else
275         ctor = (*vec)->last().value;
276       vec = &CONSTRUCTOR_ELTS (ctor);
277     }
278
279   /* Now we're at the innermost field, the one that isn't an anonymous
280      aggregate.  Add its initializer to the CONSTRUCTOR and we're done.  */
281   gcc_assert (fields.is_empty());
282   CONSTRUCTOR_APPEND_ELT (*vec, field, init);
283
284   return true;
285 }
286
287 /* Subroutine of  build_constexpr_constructor_member_initializers.
288    The expression tree T represents a data member initialization
289    in a (constexpr) constructor definition.  Build a pairing of
290    the data member with its initializer, and prepend that pair
291    to the existing initialization pair INITS.  */
292
293 static bool
294 build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
295 {
296   tree member, init;
297   if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
298     t = TREE_OPERAND (t, 0);
299   if (TREE_CODE (t) == EXPR_STMT)
300     t = TREE_OPERAND (t, 0);
301   if (t == error_mark_node)
302     return false;
303   if (TREE_CODE (t) == STATEMENT_LIST)
304     {
305       tree_stmt_iterator i;
306       for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
307         {
308           if (! build_data_member_initialization (tsi_stmt (i), vec))
309             return false;
310         }
311       return true;
312     }
313   if (TREE_CODE (t) == CLEANUP_STMT)
314     {
315       /* We can't see a CLEANUP_STMT in a constructor for a literal class,
316          but we can in a constexpr constructor for a non-literal class.  Just
317          ignore it; either all the initialization will be constant, in which
318          case the cleanup can't run, or it can't be constexpr.
319          Still recurse into CLEANUP_BODY.  */
320       return build_data_member_initialization (CLEANUP_BODY (t), vec);
321     }
322   if (TREE_CODE (t) == CONVERT_EXPR)
323     t = TREE_OPERAND (t, 0);
324   if (TREE_CODE (t) == INIT_EXPR
325       /* vptr initialization shows up as a MODIFY_EXPR.  In C++14 we only
326          use what this function builds for cx_check_missing_mem_inits, and
327          assignment in the ctor body doesn't count.  */
328       || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR))
329     {
330       member = TREE_OPERAND (t, 0);
331       init = break_out_target_exprs (TREE_OPERAND (t, 1));
332     }
333   else if (TREE_CODE (t) == CALL_EXPR)
334     {
335       tree fn = get_callee_fndecl (t);
336       if (!fn || !DECL_CONSTRUCTOR_P (fn))
337         /* We're only interested in calls to subobject constructors.  */
338         return true;
339       member = CALL_EXPR_ARG (t, 0);
340       /* We don't use build_cplus_new here because it complains about
341          abstract bases.  Leaving the call unwrapped means that it has the
342          wrong type, but cxx_eval_constant_expression doesn't care.  */
343       init = break_out_target_exprs (t);
344     }
345   else if (TREE_CODE (t) == BIND_EXPR)
346     return build_data_member_initialization (BIND_EXPR_BODY (t), vec);
347   else
348     /* Don't add anything else to the CONSTRUCTOR.  */
349     return true;
350   if (INDIRECT_REF_P (member))
351     member = TREE_OPERAND (member, 0);
352   if (TREE_CODE (member) == NOP_EXPR)
353     {
354       tree op = member;
355       STRIP_NOPS (op);
356       if (TREE_CODE (op) == ADDR_EXPR)
357         {
358           gcc_assert (same_type_ignoring_top_level_qualifiers_p
359                       (TREE_TYPE (TREE_TYPE (op)),
360                        TREE_TYPE (TREE_TYPE (member))));
361           /* Initializing a cv-qualified member; we need to look through
362              the const_cast.  */
363           member = op;
364         }
365       else if (op == current_class_ptr
366                && (same_type_ignoring_top_level_qualifiers_p
367                    (TREE_TYPE (TREE_TYPE (member)),
368                     current_class_type)))
369         /* Delegating constructor.  */
370         member = op;
371       else
372         {
373           /* This is an initializer for an empty base; keep it for now so
374              we can check it in cxx_eval_bare_aggregate.  */
375           gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
376         }
377     }
378   if (TREE_CODE (member) == ADDR_EXPR)
379     member = TREE_OPERAND (member, 0);
380   if (TREE_CODE (member) == COMPONENT_REF)
381     {
382       tree aggr = TREE_OPERAND (member, 0);
383       if (TREE_CODE (aggr) != COMPONENT_REF)
384         /* Normal member initialization.  */
385         member = TREE_OPERAND (member, 1);
386       else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
387         /* Initializing a member of an anonymous union.  */
388         return build_anon_member_initialization (member, init, vec);
389       else
390         /* We're initializing a vtable pointer in a base.  Leave it as
391            COMPONENT_REF so we remember the path to get to the vfield.  */
392         gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
393     }
394
395   CONSTRUCTOR_APPEND_ELT (*vec, member, init);
396   return true;
397 }
398
399 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
400    In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a 
401    BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations.  */
402
403 static bool
404 check_constexpr_bind_expr_vars (tree t)
405 {
406   gcc_assert (TREE_CODE (t) == BIND_EXPR);
407
408   for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
409     if (TREE_CODE (var) == TYPE_DECL
410         && DECL_IMPLICIT_TYPEDEF_P (var)
411         && !LAMBDA_TYPE_P (TREE_TYPE (var)))
412       return false;
413   return true;
414 }
415
416 /* Subroutine of check_constexpr_ctor_body.  */
417
418 static bool
419 check_constexpr_ctor_body_1 (tree last, tree list)
420 {
421   switch (TREE_CODE (list))
422     {
423     case DECL_EXPR:
424       if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL)
425         return true;
426       return false;
427
428     case CLEANUP_POINT_EXPR:
429       return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
430                                         /*complain=*/false);
431
432     case BIND_EXPR:
433        if (!check_constexpr_bind_expr_vars (list)
434            || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
435                                           /*complain=*/false))
436          return false;
437        return true;
438
439     case USING_STMT:
440     case STATIC_ASSERT:
441       return true;
442
443     default:
444       return false;
445     }
446 }
447
448 /* Make sure that there are no statements after LAST in the constructor
449    body represented by LIST.  */
450
451 bool
452 check_constexpr_ctor_body (tree last, tree list, bool complain)
453 {
454   /* C++14 doesn't require a constexpr ctor to have an empty body.  */
455   if (cxx_dialect >= cxx14)
456     return true;
457
458   bool ok = true;
459   if (TREE_CODE (list) == STATEMENT_LIST)
460     {
461       tree_stmt_iterator i = tsi_last (list);
462       for (; !tsi_end_p (i); tsi_prev (&i))
463         {
464           tree t = tsi_stmt (i);
465           if (t == last)
466             break;
467           if (!check_constexpr_ctor_body_1 (last, t))
468             {
469               ok = false;
470               break;
471             }
472         }
473     }
474   else if (list != last
475            && !check_constexpr_ctor_body_1 (last, list))
476     ok = false;
477   if (!ok)
478     {
479       if (complain)
480         error ("constexpr constructor does not have empty body");
481       DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
482     }
483   return ok;
484 }
485
486 /* V is a vector of constructor elements built up for the base and member
487    initializers of a constructor for TYPE.  They need to be in increasing
488    offset order, which they might not be yet if TYPE has a primary base
489    which is not first in the base-clause or a vptr and at least one base
490    all of which are non-primary.  */
491
492 static vec<constructor_elt, va_gc> *
493 sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
494 {
495   tree pri = CLASSTYPE_PRIMARY_BINFO (type);
496   tree field_type;
497   unsigned i;
498   constructor_elt *ce;
499
500   if (pri)
501     field_type = BINFO_TYPE (pri);
502   else if (TYPE_CONTAINS_VPTR_P (type))
503     field_type = vtbl_ptr_type_node;
504   else
505     return v;
506
507   /* Find the element for the primary base or vptr and move it to the
508      beginning of the vec.  */
509   for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
510     if (TREE_TYPE (ce->index) == field_type)
511       break;
512
513   if (i > 0 && i < vec_safe_length (v))
514     {
515       vec<constructor_elt, va_gc> &vref = *v;
516       constructor_elt elt = vref[i];
517       for (; i > 0; --i)
518         vref[i] = vref[i-1];
519       vref[0] = elt;
520     }
521
522   return v;
523 }
524
525 /* Build compile-time evalable representations of member-initializer list
526    for a constexpr constructor.  */
527
528 static tree
529 build_constexpr_constructor_member_initializers (tree type, tree body)
530 {
531   vec<constructor_elt, va_gc> *vec = NULL;
532   bool ok = true;
533   if (TREE_CODE (body) == MUST_NOT_THROW_EXPR
534       || TREE_CODE (body) == EH_SPEC_BLOCK)
535     body = TREE_OPERAND (body, 0);
536   if (TREE_CODE (body) == STATEMENT_LIST)
537     {
538       for (tree_stmt_iterator i = tsi_start (body);
539            !tsi_end_p (i); tsi_next (&i))
540         {
541           body = tsi_stmt (i);
542           if (TREE_CODE (body) == BIND_EXPR)
543             break;
544         }
545     }
546   if (TREE_CODE (body) == BIND_EXPR)
547     body = BIND_EXPR_BODY (body);
548   if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
549     {
550       body = TREE_OPERAND (body, 0);
551       if (TREE_CODE (body) == EXPR_STMT)
552         body = TREE_OPERAND (body, 0);
553       if (TREE_CODE (body) == INIT_EXPR
554           && (same_type_ignoring_top_level_qualifiers_p
555               (TREE_TYPE (TREE_OPERAND (body, 0)),
556                current_class_type)))
557         {
558           /* Trivial copy.  */
559           return TREE_OPERAND (body, 1);
560         }
561       ok = build_data_member_initialization (body, &vec);
562     }
563   else if (TREE_CODE (body) == STATEMENT_LIST)
564     {
565       tree_stmt_iterator i;
566       for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
567         {
568           ok = build_data_member_initialization (tsi_stmt (i), &vec);
569           if (!ok)
570             break;
571         }
572     }
573   else if (TREE_CODE (body) == TRY_BLOCK)
574     {
575       error ("body of %<constexpr%> constructor cannot be "
576              "a function-try-block");
577       return error_mark_node;
578     }
579   else if (EXPR_P (body))
580     ok = build_data_member_initialization (body, &vec);
581   else
582     gcc_assert (errorcount > 0);
583   if (ok)
584     {
585       if (vec_safe_length (vec) > 0)
586         {
587           /* In a delegating constructor, return the target.  */
588           constructor_elt *ce = &(*vec)[0];
589           if (ce->index == current_class_ptr)
590             {
591               body = ce->value;
592               vec_free (vec);
593               return body;
594             }
595         }
596       vec = sort_constexpr_mem_initializers (type, vec);
597       return build_constructor (type, vec);
598     }
599   else
600     return error_mark_node;
601 }
602
603 /* Subroutine of register_constexpr_fundef.  BODY is the body of a function
604    declared to be constexpr, or a sub-statement thereof.  Returns the
605    return value if suitable, error_mark_node for a statement not allowed in
606    a constexpr function, or NULL_TREE if no return value was found.  */
607
608 static tree
609 constexpr_fn_retval (tree body)
610 {
611   switch (TREE_CODE (body))
612     {
613     case STATEMENT_LIST:
614       {
615         tree_stmt_iterator i;
616         tree expr = NULL_TREE;
617         for (i = tsi_start (body); !tsi_end_p (i); tsi_next (&i))
618           {
619             tree s = constexpr_fn_retval (tsi_stmt (i));
620             if (s == error_mark_node)
621               return error_mark_node;
622             else if (s == NULL_TREE)
623               /* Keep iterating.  */;
624             else if (expr)
625               /* Multiple return statements.  */
626               return error_mark_node;
627             else
628               expr = s;
629           }
630         return expr;
631       }
632
633     case RETURN_EXPR:
634       return break_out_target_exprs (TREE_OPERAND (body, 0));
635
636     case DECL_EXPR:
637       {
638         tree decl = DECL_EXPR_DECL (body);
639         if (TREE_CODE (decl) == USING_DECL
640             /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__.  */
641             || DECL_ARTIFICIAL (decl))
642           return NULL_TREE;
643         return error_mark_node;
644       }
645
646     case CLEANUP_POINT_EXPR:
647       return constexpr_fn_retval (TREE_OPERAND (body, 0));
648
649     case BIND_EXPR:
650       if (!check_constexpr_bind_expr_vars (body))
651         return error_mark_node;
652       return constexpr_fn_retval (BIND_EXPR_BODY (body));
653
654     case USING_STMT:
655       return NULL_TREE;
656
657     default:
658       return error_mark_node;
659     }
660 }
661
662 /* Subroutine of register_constexpr_fundef.  BODY is the DECL_SAVED_TREE of
663    FUN; do the necessary transformations to turn it into a single expression
664    that we can store in the hash table.  */
665
666 static tree
667 massage_constexpr_body (tree fun, tree body)
668 {
669   if (DECL_CONSTRUCTOR_P (fun))
670     body = build_constexpr_constructor_member_initializers
671       (DECL_CONTEXT (fun), body);
672   else if (cxx_dialect < cxx14)
673     {
674       if (TREE_CODE (body) == EH_SPEC_BLOCK)
675         body = EH_SPEC_STMTS (body);
676       if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
677         body = TREE_OPERAND (body, 0);
678       body = constexpr_fn_retval (body);
679     }
680   return body;
681 }
682
683 /* FUN is a constexpr constructor with massaged body BODY.  Return true
684    if some bases/fields are uninitialized, and complain if COMPLAIN.  */
685
686 static bool
687 cx_check_missing_mem_inits (tree fun, tree body, bool complain)
688 {
689   bool bad;
690   tree field;
691   unsigned i, nelts;
692   tree ctype;
693
694   if (TREE_CODE (body) != CONSTRUCTOR)
695     return false;
696
697   nelts = CONSTRUCTOR_NELTS (body);
698   ctype = DECL_CONTEXT (fun);
699   field = TYPE_FIELDS (ctype);
700
701   if (TREE_CODE (ctype) == UNION_TYPE)
702     {
703       if (nelts == 0 && next_initializable_field (field))
704         {
705           if (complain)
706             error ("%<constexpr%> constructor for union %qT must "
707                    "initialize exactly one non-static data member", ctype);
708           return true;
709         }
710       return false;
711     }
712
713   bad = false;
714   for (i = 0; i <= nelts; ++i)
715     {
716       tree index;
717       if (i == nelts)
718         index = NULL_TREE;
719       else
720         {
721           index = CONSTRUCTOR_ELT (body, i)->index;
722           /* Skip base and vtable inits.  */
723           if (TREE_CODE (index) != FIELD_DECL
724               || DECL_ARTIFICIAL (index))
725             continue;
726         }
727       for (; field != index; field = DECL_CHAIN (field))
728         {
729           tree ftype;
730           if (TREE_CODE (field) != FIELD_DECL
731               || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
732               || DECL_ARTIFICIAL (field))
733             continue;
734           ftype = strip_array_types (TREE_TYPE (field));
735           if (type_has_constexpr_default_constructor (ftype))
736             {
737               /* It's OK to skip a member with a trivial constexpr ctor.
738                  A constexpr ctor that isn't trivial should have been
739                  added in by now.  */
740               gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
741                                    || errorcount != 0);
742               continue;
743             }
744           if (!complain)
745             return true;
746           error ("member %qD must be initialized by mem-initializer "
747                  "in %<constexpr%> constructor", field);
748           inform (DECL_SOURCE_LOCATION (field), "declared here");
749           bad = true;
750         }
751       if (field == NULL_TREE)
752         break;
753       field = DECL_CHAIN (field);
754     }
755
756   return bad;
757 }
758
759 /* We are processing the definition of the constexpr function FUN.
760    Check that its BODY fulfills the propriate requirements and
761    enter it in the constexpr function definition table.
762    For constructor BODY is actually the TREE_LIST of the
763    member-initializer list.  */
764
765 tree
766 register_constexpr_fundef (tree fun, tree body)
767 {
768   constexpr_fundef entry;
769   constexpr_fundef **slot;
770
771   if (!is_valid_constexpr_fn (fun, !DECL_GENERATED_P (fun)))
772     return NULL;
773
774   body = massage_constexpr_body (fun, body);
775   if (body == NULL_TREE || body == error_mark_node)
776     {
777       if (!DECL_CONSTRUCTOR_P (fun))
778         error ("body of constexpr function %qD not a return-statement", fun);
779       return NULL;
780     }
781
782   if (!potential_rvalue_constant_expression (body))
783     {
784       if (!DECL_GENERATED_P (fun))
785         require_potential_rvalue_constant_expression (body);
786       return NULL;
787     }
788
789   if (DECL_CONSTRUCTOR_P (fun)
790       && cx_check_missing_mem_inits (fun, body, !DECL_GENERATED_P (fun)))
791     return NULL;
792
793   /* Create the constexpr function table if necessary.  */
794   if (constexpr_fundef_table == NULL)
795     constexpr_fundef_table
796       = hash_table<constexpr_fundef_hasher>::create_ggc (101);
797
798   entry.decl = fun;
799   entry.body = body;
800   slot = constexpr_fundef_table->find_slot (&entry, INSERT);
801
802   gcc_assert (*slot == NULL);
803   *slot = ggc_alloc<constexpr_fundef> ();
804   **slot = entry;
805
806   return fun;
807 }
808
809 /* FUN is a non-constexpr function called in a context that requires a
810    constant expression.  If it comes from a constexpr template, explain why
811    the instantiation isn't constexpr.  */
812
813 void
814 explain_invalid_constexpr_fn (tree fun)
815 {
816   static hash_set<tree> *diagnosed;
817   tree body;
818   location_t save_loc;
819   /* Only diagnose defaulted functions or instantiations.  */
820   if (!DECL_DEFAULTED_FN (fun)
821       && !is_instantiation_of_constexpr (fun))
822     return;
823   if (diagnosed == NULL)
824     diagnosed = new hash_set<tree>;
825   if (diagnosed->add (fun))
826     /* Already explained.  */
827     return;
828
829   save_loc = input_location;
830   input_location = DECL_SOURCE_LOCATION (fun);
831   inform (input_location,
832           "%qD is not usable as a constexpr function because:", fun);
833   /* First check the declaration.  */
834   if (is_valid_constexpr_fn (fun, true))
835     {
836       /* Then if it's OK, the body.  */
837       if (!DECL_DECLARED_CONSTEXPR_P (fun))
838         explain_implicit_non_constexpr (fun);
839       else
840         {
841           body = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
842           require_potential_rvalue_constant_expression (body);
843           if (DECL_CONSTRUCTOR_P (fun))
844             cx_check_missing_mem_inits (fun, body, true);
845         }
846     }
847   input_location = save_loc;
848 }
849
850 /* Objects of this type represent calls to constexpr functions
851    along with the bindings of parameters to their arguments, for
852    the purpose of compile time evaluation.  */
853
854 struct GTY((for_user)) constexpr_call {
855   /* Description of the constexpr function definition.  */
856   constexpr_fundef *fundef;
857   /* Parameter bindings environment.  A TREE_LIST where each TREE_PURPOSE
858      is a parameter _DECL and the TREE_VALUE is the value of the parameter.
859      Note: This arrangement is made to accommodate the use of
860      iterative_hash_template_arg (see pt.c).  If you change this
861      representation, also change the hash calculation in
862      cxx_eval_call_expression.  */
863   tree bindings;
864   /* Result of the call.
865        NULL means the call is being evaluated.
866        error_mark_node means that the evaluation was erroneous;
867        otherwise, the actuall value of the call.  */
868   tree result;
869   /* The hash of this call; we remember it here to avoid having to
870      recalculate it when expanding the hash table.  */
871   hashval_t hash;
872 };
873
874 struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call>
875 {
876   static hashval_t hash (constexpr_call *);
877   static bool equal (constexpr_call *, constexpr_call *);
878 };
879
880 /* The constexpr expansion context.  CALL is the current function
881    expansion, CTOR is the current aggregate initializer, OBJECT is the
882    object being initialized by CTOR, either a VAR_DECL or a _REF.  VALUES
883    is a map of values of variables initialized within the expression.  */
884
885 struct constexpr_ctx {
886   /* The innermost call we're evaluating.  */
887   constexpr_call *call;
888   /* Values for any temporaries or local variables within the
889      constant-expression. */
890   hash_map<tree,tree> *values;
891   /* The CONSTRUCTOR we're currently building up for an aggregate
892      initializer.  */
893   tree ctor;
894   /* The object we're building the CONSTRUCTOR for.  */
895   tree object;
896   /* Whether we should error on a non-constant expression or fail quietly.  */
897   bool quiet;
898   /* Whether we are strictly conforming to constant expression rules or
899      trying harder to get a constant value.  */
900   bool strict;
901 };
902
903 /* A table of all constexpr calls that have been evaluated by the
904    compiler in this translation unit.  */
905
906 static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
907
908 static tree cxx_eval_constant_expression (const constexpr_ctx *, tree,
909                                           bool, bool *, bool *, tree * = NULL);
910
911 /* Compute a hash value for a constexpr call representation.  */
912
913 inline hashval_t
914 constexpr_call_hasher::hash (constexpr_call *info)
915 {
916   return info->hash;
917 }
918
919 /* Return true if the objects pointed to by P and Q represent calls
920    to the same constexpr function with the same arguments.
921    Otherwise, return false.  */
922
923 bool
924 constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
925 {
926   tree lhs_bindings;
927   tree rhs_bindings;
928   if (lhs == rhs)
929     return 1;
930   if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
931     return 0;
932   lhs_bindings = lhs->bindings;
933   rhs_bindings = rhs->bindings;
934   while (lhs_bindings != NULL && rhs_bindings != NULL)
935     {
936       tree lhs_arg = TREE_VALUE (lhs_bindings);
937       tree rhs_arg = TREE_VALUE (rhs_bindings);
938       gcc_assert (TREE_TYPE (lhs_arg) == TREE_TYPE (rhs_arg));
939       if (!cp_tree_equal (lhs_arg, rhs_arg))
940         return 0;
941       lhs_bindings = TREE_CHAIN (lhs_bindings);
942       rhs_bindings = TREE_CHAIN (rhs_bindings);
943     }
944   return lhs_bindings == rhs_bindings;
945 }
946
947 /* Initialize the constexpr call table, if needed.  */
948
949 static void
950 maybe_initialize_constexpr_call_table (void)
951 {
952   if (constexpr_call_table == NULL)
953     constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
954 }
955
956 /* We have an expression tree T that represents a call, either CALL_EXPR
957    or AGGR_INIT_EXPR.  If the call is lexically to a named function,
958    retrun the _DECL for that function.  */
959
960 static tree
961 get_function_named_in_call (tree t)
962 {
963   tree fun = NULL;
964   switch (TREE_CODE (t))
965     {
966     case CALL_EXPR:
967       fun = CALL_EXPR_FN (t);
968       break;
969
970     case AGGR_INIT_EXPR:
971       fun = AGGR_INIT_EXPR_FN (t);
972       break;
973
974     default:
975       gcc_unreachable();
976       break;
977     }
978   if (fun && TREE_CODE (fun) == ADDR_EXPR
979       && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
980     fun = TREE_OPERAND (fun, 0);
981   return fun;
982 }
983
984 /* We have an expression tree T that represents a call, either CALL_EXPR
985    or AGGR_INIT_EXPR.  Return the Nth argument.  */
986
987 static inline tree
988 get_nth_callarg (tree t, int n)
989 {
990   switch (TREE_CODE (t))
991     {
992     case CALL_EXPR:
993       return CALL_EXPR_ARG (t, n);
994
995     case AGGR_INIT_EXPR:
996       return AGGR_INIT_EXPR_ARG (t, n);
997
998     default:
999       gcc_unreachable ();
1000       return NULL;
1001     }
1002 }
1003
1004 /* Attempt to evaluate T which represents a call to a builtin function.
1005    We assume here that all builtin functions evaluate to scalar types
1006    represented by _CST nodes.  */
1007
1008 static tree
1009 cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun,
1010                                 bool lval,
1011                                 bool *non_constant_p, bool *overflow_p)
1012 {
1013   const int nargs = call_expr_nargs (t);
1014   tree *args = (tree *) alloca (nargs * sizeof (tree));
1015   tree new_call;
1016   int i;
1017
1018   /* Don't fold __builtin_constant_p within a constexpr function.  */
1019   if (DECL_FUNCTION_CODE (fun) == BUILT_IN_CONSTANT_P
1020       && current_function_decl
1021       && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
1022     {
1023       *non_constant_p = true;
1024       return t;
1025     }
1026
1027   /* Be permissive for arguments to built-ins; __builtin_constant_p should
1028      return constant false for a non-constant argument.  */
1029   constexpr_ctx new_ctx = *ctx;
1030   new_ctx.quiet = true;
1031   bool dummy1 = false, dummy2 = false;
1032   for (i = 0; i < nargs; ++i)
1033     args[i] = cxx_eval_constant_expression (&new_ctx, CALL_EXPR_ARG (t, i),
1034                                             lval, &dummy1, &dummy2);
1035
1036   bool save_ffbcp = force_folding_builtin_constant_p;
1037   force_folding_builtin_constant_p = true;
1038   new_call = fold_build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
1039                                         CALL_EXPR_FN (t), nargs, args);
1040   force_folding_builtin_constant_p = save_ffbcp;
1041   VERIFY_CONSTANT (new_call);
1042   return new_call;
1043 }
1044
1045 /* TEMP is the constant value of a temporary object of type TYPE.  Adjust
1046    the type of the value to match.  */
1047
1048 static tree
1049 adjust_temp_type (tree type, tree temp)
1050 {
1051   if (TREE_TYPE (temp) == type)
1052     return temp;
1053   /* Avoid wrapping an aggregate value in a NOP_EXPR.  */
1054   if (TREE_CODE (temp) == CONSTRUCTOR)
1055     return build_constructor (type, CONSTRUCTOR_ELTS (temp));
1056   gcc_assert (scalarish_type_p (type));
1057   return cp_fold_convert (type, temp);
1058 }
1059
1060 /* Subroutine of cxx_eval_call_expression.
1061    We are processing a call expression (either CALL_EXPR or
1062    AGGR_INIT_EXPR) in the context of CTX.  Evaluate
1063    all arguments and bind their values to correspondings
1064    parameters, making up the NEW_CALL context.  */
1065
1066 static void
1067 cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t,
1068                              constexpr_call *new_call,
1069                              bool *non_constant_p, bool *overflow_p,
1070                              bool *non_constant_args)
1071 {
1072   const int nargs = call_expr_nargs (t);
1073   tree fun = new_call->fundef->decl;
1074   tree parms = DECL_ARGUMENTS (fun);
1075   int i;
1076   tree *p = &new_call->bindings;
1077   for (i = 0; i < nargs; ++i)
1078     {
1079       tree x, arg;
1080       tree type = parms ? TREE_TYPE (parms) : void_type_node;
1081       x = get_nth_callarg (t, i);
1082       /* For member function, the first argument is a pointer to the implied
1083          object.  For a constructor, it might still be a dummy object, in
1084          which case we get the real argument from ctx. */
1085       if (i == 0 && DECL_CONSTRUCTOR_P (fun)
1086           && is_dummy_object (x))
1087         {
1088           x = ctx->object;
1089           x = cp_build_addr_expr (x, tf_warning_or_error);
1090         }
1091       bool lval = false;
1092       arg = cxx_eval_constant_expression (ctx, x, lval,
1093                                           non_constant_p, overflow_p);
1094       /* Don't VERIFY_CONSTANT here.  */
1095       if (*non_constant_p && ctx->quiet)
1096         return;
1097       /* Just discard ellipsis args after checking their constantitude.  */
1098       if (!parms)
1099         continue;
1100       if (*non_constant_p)
1101         /* Don't try to adjust the type of non-constant args.  */
1102         goto next;
1103
1104       /* Make sure the binding has the same type as the parm.  */
1105       if (TREE_CODE (type) != REFERENCE_TYPE)
1106         arg = adjust_temp_type (type, arg);
1107       if (!TREE_CONSTANT (arg))
1108         *non_constant_args = true;
1109       *p = build_tree_list (parms, arg);
1110       p = &TREE_CHAIN (*p);
1111     next:
1112       parms = TREE_CHAIN (parms);
1113     }
1114 }
1115
1116 /* Variables and functions to manage constexpr call expansion context.
1117    These do not need to be marked for PCH or GC.  */
1118
1119 /* FIXME remember and print actual constant arguments.  */
1120 static vec<tree> call_stack = vNULL;
1121 static int call_stack_tick;
1122 static int last_cx_error_tick;
1123
1124 static bool
1125 push_cx_call_context (tree call)
1126 {
1127   ++call_stack_tick;
1128   if (!EXPR_HAS_LOCATION (call))
1129     SET_EXPR_LOCATION (call, input_location);
1130   call_stack.safe_push (call);
1131   if (call_stack.length () > (unsigned) max_constexpr_depth)
1132     return false;
1133   return true;
1134 }
1135
1136 static void
1137 pop_cx_call_context (void)
1138 {
1139   ++call_stack_tick;
1140   call_stack.pop ();
1141 }
1142
1143 vec<tree> 
1144 cx_error_context (void)
1145 {
1146   vec<tree> r = vNULL;
1147   if (call_stack_tick != last_cx_error_tick
1148       && !call_stack.is_empty ())
1149     r = call_stack;
1150   last_cx_error_tick = call_stack_tick;
1151   return r;
1152 }
1153
1154 /* Subroutine of cxx_eval_constant_expression.
1155    Evaluate the call expression tree T in the context of OLD_CALL expression
1156    evaluation.  */
1157
1158 static tree
1159 cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
1160                           bool lval,
1161                           bool *non_constant_p, bool *overflow_p)
1162 {
1163   location_t loc = EXPR_LOC_OR_LOC (t, input_location);
1164   tree fun = get_function_named_in_call (t);
1165   constexpr_call new_call = { NULL, NULL, NULL, 0 };
1166   bool depth_ok;
1167
1168   if (fun == NULL_TREE)
1169     switch (CALL_EXPR_IFN (t))
1170       {
1171       case IFN_UBSAN_NULL:
1172       case IFN_UBSAN_BOUNDS:
1173       case IFN_UBSAN_VPTR:
1174         return void_node;
1175       default:
1176         if (!ctx->quiet)
1177           error_at (loc, "call to internal function");
1178         *non_constant_p = true;
1179         return t;
1180       }
1181
1182   if (TREE_CODE (fun) != FUNCTION_DECL)
1183     {
1184       /* Might be a constexpr function pointer.  */
1185       fun = cxx_eval_constant_expression (ctx, fun,
1186                                           /*lval*/false, non_constant_p,
1187                                           overflow_p);
1188       STRIP_NOPS (fun);
1189       if (TREE_CODE (fun) == ADDR_EXPR)
1190         fun = TREE_OPERAND (fun, 0);
1191     }
1192   if (TREE_CODE (fun) != FUNCTION_DECL)
1193     {
1194       if (!ctx->quiet && !*non_constant_p)
1195         error_at (loc, "expression %qE does not designate a constexpr "
1196                   "function", fun);
1197       *non_constant_p = true;
1198       return t;
1199     }
1200   if (DECL_CLONED_FUNCTION_P (fun))
1201     fun = DECL_CLONED_FUNCTION (fun);
1202
1203   if (is_ubsan_builtin_p (fun))
1204     return void_node;
1205
1206   if (is_builtin_fn (fun))
1207     return cxx_eval_builtin_function_call (ctx, t, fun,
1208                                            lval, non_constant_p, overflow_p);
1209   if (!DECL_DECLARED_CONSTEXPR_P (fun))
1210     {
1211       if (!ctx->quiet)
1212         {
1213           error_at (loc, "call to non-constexpr function %qD", fun);
1214           explain_invalid_constexpr_fn (fun);
1215         }
1216       *non_constant_p = true;
1217       return t;
1218     }
1219
1220   /* Shortcut trivial constructor/op=.  */
1221   if (trivial_fn_p (fun))
1222     {
1223       if (call_expr_nargs (t) == 2)
1224         {
1225           tree arg = convert_from_reference (get_nth_callarg (t, 1));
1226           return cxx_eval_constant_expression (ctx, arg,
1227                                                lval, non_constant_p,
1228                                                overflow_p);
1229         }
1230       else if (TREE_CODE (t) == AGGR_INIT_EXPR
1231                && AGGR_INIT_ZERO_FIRST (t))
1232         return build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false);
1233     }
1234
1235   /* We can't defer instantiating the function any longer.  */
1236   if (!DECL_INITIAL (fun)
1237       && DECL_TEMPLOID_INSTANTIATION (fun))
1238     {
1239       ++function_depth;
1240       instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
1241       --function_depth;
1242     }
1243
1244   /* If in direct recursive call, optimize definition search.  */
1245   if (ctx && ctx->call && ctx->call->fundef->decl == fun)
1246     new_call.fundef = ctx->call->fundef;
1247   else
1248     {
1249       new_call.fundef = retrieve_constexpr_fundef (fun);
1250       if (new_call.fundef == NULL || new_call.fundef->body == NULL)
1251         {
1252           if (!ctx->quiet)
1253             {
1254               if (DECL_INITIAL (fun))
1255                 {
1256                   /* The definition of fun was somehow unsuitable.  */
1257                   error_at (loc, "%qD called in a constant expression", fun);
1258                   explain_invalid_constexpr_fn (fun);
1259                 }
1260               else
1261                 error_at (loc, "%qD used before its definition", fun);
1262             }
1263           *non_constant_p = true;
1264           return t;
1265         }
1266     }
1267
1268   constexpr_ctx new_ctx = *ctx;
1269   if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
1270       && TREE_CODE (t) == AGGR_INIT_EXPR)
1271     {
1272       /* We want to have an initialization target for an AGGR_INIT_EXPR.
1273          If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT.  */
1274       new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
1275       tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
1276       CONSTRUCTOR_NO_IMPLICIT_ZERO (ctor) = true;
1277       ctx->values->put (new_ctx.object, ctor);
1278       ctx = &new_ctx;
1279     }
1280
1281   bool non_constant_args = false;
1282   cxx_bind_parameters_in_call (ctx, t, &new_call,
1283                                non_constant_p, overflow_p, &non_constant_args);
1284   if (*non_constant_p)
1285     return t;
1286
1287   depth_ok = push_cx_call_context (t);
1288
1289   tree result = NULL_TREE;
1290
1291   constexpr_call *entry = NULL;
1292   if (depth_ok && !non_constant_args)
1293     {
1294       new_call.hash = iterative_hash_template_arg
1295         (new_call.bindings, constexpr_fundef_hasher::hash (new_call.fundef));
1296
1297       /* If we have seen this call before, we are done.  */
1298       maybe_initialize_constexpr_call_table ();
1299       constexpr_call **slot
1300         = constexpr_call_table->find_slot (&new_call, INSERT);
1301       entry = *slot;
1302       if (entry == NULL)
1303         {
1304           /* We need to keep a pointer to the entry, not just the slot, as the
1305              slot can move in the call to cxx_eval_builtin_function_call.  */
1306           *slot = entry = ggc_alloc<constexpr_call> ();
1307           *entry = new_call;
1308         }
1309       /* Calls which are in progress have their result set to NULL
1310          so that we can detect circular dependencies.  */
1311       else if (entry->result == NULL)
1312         {
1313           if (!ctx->quiet)
1314             error ("call has circular dependency");
1315           *non_constant_p = true;
1316           entry->result = result = error_mark_node;
1317         }
1318       else
1319         result = entry->result;
1320     }
1321
1322   if (!depth_ok)
1323     {
1324       if (!ctx->quiet)
1325         error ("constexpr evaluation depth exceeds maximum of %d (use "
1326                "-fconstexpr-depth= to increase the maximum)",
1327                max_constexpr_depth);
1328       *non_constant_p = true;
1329       result = error_mark_node;
1330     }
1331   else
1332     {
1333       if (!result || result == error_mark_node)
1334         {
1335           if (DECL_SAVED_TREE (fun) == NULL_TREE
1336               && (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun)))
1337             /* The maybe-in-charge 'tor had its DECL_SAVED_TREE
1338                cleared, try a clone.  */
1339             for (fun = DECL_CHAIN (fun);
1340                  fun && DECL_CLONED_FUNCTION_P (fun);
1341                  fun = DECL_CHAIN (fun))
1342               if (DECL_SAVED_TREE (fun))
1343                 break;
1344           gcc_assert (DECL_SAVED_TREE (fun));
1345           tree parms, res;
1346
1347           /* Unshare the whole function body.  */
1348           tree body = copy_fn (fun, parms, res);
1349
1350           /* Associate the bindings with the remapped parms.  */
1351           tree bound = new_call.bindings;
1352           tree remapped = parms;
1353           while (bound)
1354             {
1355               tree oparm = TREE_PURPOSE (bound);
1356               tree arg = TREE_VALUE (bound);
1357               gcc_assert (DECL_NAME (remapped) == DECL_NAME (oparm));
1358               ctx->values->put (remapped, arg);
1359               bound = TREE_CHAIN (bound);
1360               remapped = DECL_CHAIN (remapped);
1361             }
1362           /* Add the RESULT_DECL to the values map, too.  */
1363           tree slot = NULL_TREE;
1364           if (DECL_BY_REFERENCE (res))
1365             {
1366               slot = AGGR_INIT_EXPR_SLOT (t);
1367               tree addr = build_address (slot);
1368               addr = build_nop (TREE_TYPE (res), addr);
1369               ctx->values->put (res, addr);
1370               ctx->values->put (slot, NULL_TREE);
1371             }
1372           else
1373             ctx->values->put (res, NULL_TREE);
1374
1375           tree jump_target = NULL_TREE;
1376           cxx_eval_constant_expression (ctx, body,
1377                                         lval, non_constant_p, overflow_p,
1378                                         &jump_target);
1379
1380           if (DECL_CONSTRUCTOR_P (fun))
1381             /* This can be null for a subobject constructor call, in
1382                which case what we care about is the initialization
1383                side-effects rather than the value.  We could get at the
1384                value by evaluating *this, but we don't bother; there's
1385                no need to put such a call in the hash table.  */
1386             result = lval ? ctx->object : ctx->ctor;
1387           else if (VOID_TYPE_P (TREE_TYPE (res)))
1388             result = void_node;
1389           else
1390             {
1391               result = *ctx->values->get (slot ? slot : res);
1392               if (result == NULL_TREE && !*non_constant_p)
1393                 {
1394                   if (!ctx->quiet)
1395                     error ("constexpr call flows off the end "
1396                            "of the function");
1397                   *non_constant_p = true;
1398                 }
1399             }
1400
1401           /* Remove the parms/result from the values map.  Is it worth
1402              bothering to do this when the map itself is only live for
1403              one constexpr evaluation?  If so, maybe also clear out
1404              other vars from call, maybe in BIND_EXPR handling?  */
1405           ctx->values->remove (res);
1406           if (slot)
1407             ctx->values->remove (slot);
1408           for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
1409             ctx->values->remove (parm);
1410         }
1411
1412       if (result == error_mark_node)
1413         *non_constant_p = true;
1414       if (*non_constant_p)
1415         result = error_mark_node;
1416       else if (!result)
1417         result = void_node;
1418       if (entry)
1419         entry->result = result;
1420     }
1421
1422   pop_cx_call_context ();
1423   return unshare_expr (result);
1424 }
1425
1426 /* FIXME speed this up, it's taking 16% of compile time on sieve testcase.  */
1427
1428 bool
1429 reduced_constant_expression_p (tree t)
1430 {
1431   switch (TREE_CODE (t))
1432     {
1433     case PTRMEM_CST:
1434       /* Even if we can't lower this yet, it's constant.  */
1435       return true;
1436
1437     case CONSTRUCTOR:
1438       /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR.  */
1439       tree elt; unsigned HOST_WIDE_INT idx;
1440       FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), idx, elt)
1441         if (!reduced_constant_expression_p (elt))
1442           return false;
1443       return true;
1444
1445     default:
1446       /* FIXME are we calling this too much?  */
1447       return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
1448     }
1449 }
1450
1451 /* Some expressions may have constant operands but are not constant
1452    themselves, such as 1/0.  Call this function (or rather, the macro
1453    following it) to check for that condition.
1454
1455    We only call this in places that require an arithmetic constant, not in
1456    places where we might have a non-constant expression that can be a
1457    component of a constant expression, such as the address of a constexpr
1458    variable that might be dereferenced later.  */
1459
1460 static bool
1461 verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
1462                  bool *overflow_p)
1463 {
1464   if (!*non_constant_p && !reduced_constant_expression_p (t))
1465     {
1466       if (!allow_non_constant)
1467         error ("%q+E is not a constant expression", t);
1468       *non_constant_p = true;
1469     }
1470   if (TREE_OVERFLOW_P (t))
1471     {
1472       if (!allow_non_constant)
1473         {
1474           permerror (input_location, "overflow in constant expression");
1475           /* If we're being permissive (and are in an enforcing
1476              context), ignore the overflow.  */
1477           if (flag_permissive)
1478             return *non_constant_p;
1479         }
1480       *overflow_p = true;
1481     }
1482   return *non_constant_p;
1483 }
1484
1485 /* Check whether the shift operation with code CODE and type TYPE on LHS
1486    and RHS is undefined.  If it is, give an error with an explanation,
1487    and return true; return false otherwise.  */
1488
1489 static bool
1490 cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
1491                         enum tree_code code, tree type, tree lhs, tree rhs)
1492 {
1493   if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
1494       || TREE_CODE (lhs) != INTEGER_CST
1495       || TREE_CODE (rhs) != INTEGER_CST)
1496     return false;
1497
1498   tree lhstype = TREE_TYPE (lhs);
1499   unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
1500
1501   /* [expr.shift] The behavior is undefined if the right operand
1502      is negative, or greater than or equal to the length in bits
1503      of the promoted left operand.  */
1504   if (tree_int_cst_sgn (rhs) == -1)
1505     {
1506       if (!ctx->quiet)
1507         error_at (loc, "right operand of shift expression %q+E is negative",
1508                   build2_loc (loc, code, type, lhs, rhs));
1509       return true;
1510     }
1511   if (compare_tree_int (rhs, uprec) >= 0)
1512     {
1513       if (!ctx->quiet)
1514         error_at (loc, "right operand of shift expression %q+E is >= than "
1515                   "the precision of the left operand",
1516                   build2_loc (loc, code, type, lhs, rhs));
1517       return true;
1518     }
1519
1520   /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
1521      if E1 has a signed type and non-negative value, and E1x2^E2 is
1522      representable in the corresponding unsigned type of the result type,
1523      then that value, converted to the result type, is the resulting value;
1524      otherwise, the behavior is undefined.  */
1525   if (code == LSHIFT_EXPR && !TYPE_UNSIGNED (lhstype)
1526       && (cxx_dialect >= cxx11))
1527     {
1528       if (tree_int_cst_sgn (lhs) == -1)
1529         {
1530           if (!ctx->quiet)
1531             error_at (loc, "left operand of shift expression %q+E is negative",
1532                       build2_loc (loc, code, type, lhs, rhs));
1533           return true;
1534         }
1535       /* For signed x << y the following:
1536          (unsigned) x >> ((prec (lhs) - 1) - y)
1537          if > 1, is undefined.  The right-hand side of this formula
1538          is the highest bit of the LHS that can be set (starting from 0),
1539          so that the shift doesn't overflow.  We then right-shift the LHS
1540          to see whether any other bit is set making the original shift
1541          undefined -- the result is not representable in the corresponding
1542          unsigned type.  */
1543       tree t = build_int_cst (unsigned_type_node, uprec - 1);
1544       t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
1545       tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
1546       t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
1547       if (tree_int_cst_lt (integer_one_node, t))
1548         {
1549           if (!ctx->quiet)
1550             error_at (loc, "shift expression %q+E overflows",
1551                       build2_loc (loc, code, type, lhs, rhs));
1552           return true;
1553         }
1554     }
1555   return false;
1556 }
1557
1558 /* Subroutine of cxx_eval_constant_expression.
1559    Attempt to reduce the unary expression tree T to a compile time value.
1560    If successful, return the value.  Otherwise issue a diagnostic
1561    and return error_mark_node.  */
1562
1563 static tree
1564 cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
1565                            bool /*lval*/,
1566                            bool *non_constant_p, bool *overflow_p)
1567 {
1568   tree r;
1569   tree orig_arg = TREE_OPERAND (t, 0);
1570   tree arg = cxx_eval_constant_expression (ctx, orig_arg, /*lval*/false,
1571                                            non_constant_p, overflow_p);
1572   VERIFY_CONSTANT (arg);
1573   location_t loc = EXPR_LOCATION (t);
1574   enum tree_code code = TREE_CODE (t);
1575   tree type = TREE_TYPE (t);
1576   r = fold_unary_loc (loc, code, type, arg);
1577   if (r == NULL_TREE)
1578     {
1579       if (arg == orig_arg)
1580         r = t;
1581       else
1582         r = build1_loc (loc, code, type, arg);
1583     }
1584   VERIFY_CONSTANT (r);
1585   return r;
1586 }
1587
1588 /* Subroutine of cxx_eval_constant_expression.
1589    Like cxx_eval_unary_expression, except for binary expressions.  */
1590
1591 static tree
1592 cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
1593                             bool /*lval*/,
1594                             bool *non_constant_p, bool *overflow_p)
1595 {
1596   tree r;
1597   tree orig_lhs = TREE_OPERAND (t, 0);
1598   tree orig_rhs = TREE_OPERAND (t, 1);
1599   tree lhs, rhs;
1600   lhs = cxx_eval_constant_expression (ctx, orig_lhs, /*lval*/false,
1601                                       non_constant_p, overflow_p);
1602   /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
1603      a local array in a constexpr function.  */
1604   bool ptr = POINTER_TYPE_P (TREE_TYPE (lhs));
1605   if (!ptr)
1606     VERIFY_CONSTANT (lhs);
1607   rhs = cxx_eval_constant_expression (ctx, orig_rhs, /*lval*/false,
1608                                       non_constant_p, overflow_p);
1609   if (!ptr)
1610     VERIFY_CONSTANT (rhs);
1611
1612   location_t loc = EXPR_LOCATION (t);
1613   enum tree_code code = TREE_CODE (t);
1614   tree type = TREE_TYPE (t);
1615   r = fold_binary_loc (loc, code, type, lhs, rhs);
1616   if (r == NULL_TREE)
1617     {
1618       if (lhs == orig_lhs && rhs == orig_rhs)
1619         r = t;
1620       else
1621         r = build2_loc (loc, code, type, lhs, rhs);
1622     }
1623   else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
1624     *non_constant_p = true;
1625   if (!ptr)
1626     VERIFY_CONSTANT (r);
1627   return r;
1628 }
1629
1630 /* Subroutine of cxx_eval_constant_expression.
1631    Attempt to evaluate condition expressions.  Dead branches are not
1632    looked into.  */
1633
1634 static tree
1635 cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
1636                                  bool lval,
1637                                  bool *non_constant_p, bool *overflow_p,
1638                                  tree *jump_target)
1639 {
1640   tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
1641                                            /*lval*/false,
1642                                            non_constant_p, overflow_p);
1643   VERIFY_CONSTANT (val);
1644   /* Don't VERIFY_CONSTANT the other operands.  */
1645   if (integer_zerop (val))
1646     return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
1647                                          lval,
1648                                          non_constant_p, overflow_p,
1649                                          jump_target);
1650   return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
1651                                        lval,
1652                                        non_constant_p, overflow_p,
1653                                        jump_target);
1654 }
1655
1656 /* Returns less than, equal to, or greater than zero if KEY is found to be
1657    less than, to match, or to be greater than the constructor_elt's INDEX.  */
1658
1659 static int
1660 array_index_cmp (tree key, tree index)
1661 {
1662   gcc_assert (TREE_CODE (key) == INTEGER_CST);
1663
1664   switch (TREE_CODE (index))
1665     {
1666     case INTEGER_CST:
1667       return tree_int_cst_compare (key, index);
1668     case RANGE_EXPR:
1669       {
1670         tree lo = TREE_OPERAND (index, 0);
1671         tree hi = TREE_OPERAND (index, 1);
1672         if (tree_int_cst_lt (key, lo))
1673           return -1;
1674         else if (tree_int_cst_lt (hi, key))
1675           return 1;
1676         else
1677           return 0;
1678       }
1679     default:
1680       gcc_unreachable ();
1681     }
1682 }
1683
1684 /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
1685    if none.  If INSERT is true, insert a matching element rather than fail.  */
1686
1687 static HOST_WIDE_INT
1688 find_array_ctor_elt (tree ary, tree dindex, bool insert = false)
1689 {
1690   if (tree_int_cst_sgn (dindex) < 0)
1691     return -1;
1692
1693   unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
1694   vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
1695   unsigned HOST_WIDE_INT len = vec_safe_length (elts);
1696
1697   unsigned HOST_WIDE_INT end = len;
1698   unsigned HOST_WIDE_INT begin = 0;
1699
1700   /* If the last element of the CONSTRUCTOR has its own index, we can assume
1701      that the same is true of the other elements and index directly.  */
1702   if (end > 0)
1703     {
1704       tree cindex = (*elts)[end-1].index;
1705       if (TREE_CODE (cindex) == INTEGER_CST
1706           && compare_tree_int (cindex, end-1) == 0)
1707         {
1708           if (i < end)
1709             return i;
1710           else
1711             begin = end;
1712         }
1713     }
1714
1715   /* Otherwise, find a matching index by means of a binary search.  */
1716   while (begin != end)
1717     {
1718       unsigned HOST_WIDE_INT middle = (begin + end) / 2;
1719
1720       int cmp = array_index_cmp (dindex, (*elts)[middle].index);
1721       if (cmp < 0)
1722         end = middle;
1723       else if (cmp > 0)
1724         begin = middle + 1;
1725       else
1726         return middle;
1727     }
1728
1729   if (insert)
1730     {
1731       constructor_elt e = { dindex, NULL_TREE };
1732       vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
1733       return end;
1734     }
1735
1736   return -1;
1737 }
1738
1739
1740 /* Subroutine of cxx_eval_constant_expression.
1741    Attempt to reduce a reference to an array slot.  */
1742
1743 static tree
1744 cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
1745                           bool lval,
1746                           bool *non_constant_p, bool *overflow_p)
1747 {
1748   tree oldary = TREE_OPERAND (t, 0);
1749   tree ary = cxx_eval_constant_expression (ctx, oldary,
1750                                            lval,
1751                                            non_constant_p, overflow_p);
1752   tree index, oldidx;
1753   HOST_WIDE_INT i;
1754   tree elem_type;
1755   unsigned len, elem_nchars = 1;
1756   if (*non_constant_p)
1757     return t;
1758   oldidx = TREE_OPERAND (t, 1);
1759   index = cxx_eval_constant_expression (ctx, oldidx,
1760                                         false,
1761                                         non_constant_p, overflow_p);
1762   VERIFY_CONSTANT (index);
1763   if (lval && ary == oldary && index == oldidx)
1764     return t;
1765   else if (lval)
1766     return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
1767   elem_type = TREE_TYPE (TREE_TYPE (ary));
1768   if (TREE_CODE (ary) == CONSTRUCTOR)
1769     len = CONSTRUCTOR_NELTS (ary);
1770   else if (TREE_CODE (ary) == STRING_CST)
1771     {
1772       elem_nchars = (TYPE_PRECISION (elem_type)
1773                      / TYPE_PRECISION (char_type_node));
1774       len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
1775     }
1776   else
1777     {
1778       /* We can't do anything with other tree codes, so use
1779          VERIFY_CONSTANT to complain and fail.  */
1780       VERIFY_CONSTANT (ary);
1781       gcc_unreachable ();
1782     }
1783
1784   i = tree_to_shwi (index);
1785   if (i < 0)
1786     {
1787       if (!ctx->quiet)
1788         error ("negative array subscript");
1789       *non_constant_p = true;
1790       return t;
1791     }
1792
1793   bool found;
1794   if (TREE_CODE (ary) == CONSTRUCTOR)
1795     {
1796       HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
1797       found = (ix >= 0);
1798       if (found)
1799         i = ix;
1800     }
1801   else
1802     found = (i < len);
1803
1804   if (!found)
1805     {
1806       if (tree_int_cst_lt (index, array_type_nelts_top (TREE_TYPE (ary))))
1807         {
1808           if (TREE_CODE (ary) == CONSTRUCTOR
1809               && CONSTRUCTOR_NO_IMPLICIT_ZERO (ary))
1810             {
1811               /* 'ary' is part of the aggregate initializer we're currently
1812                  building; if there's no initializer for this element yet,
1813                  that's an error. */
1814               if (!ctx->quiet)
1815                 error ("accessing uninitialized array element");
1816               *non_constant_p = true;
1817               return t;
1818             }
1819
1820           /* If it's within the array bounds but doesn't have an explicit
1821              initializer, it's value-initialized.  */
1822           tree val = build_value_init (elem_type, tf_warning_or_error);
1823           return cxx_eval_constant_expression (ctx, val,
1824                                                lval,
1825                                                non_constant_p, overflow_p);
1826         }
1827
1828       if (!ctx->quiet)
1829         error ("array subscript out of bound");
1830       *non_constant_p = true;
1831       return t;
1832     }
1833
1834   if (TREE_CODE (ary) == CONSTRUCTOR)
1835     return (*CONSTRUCTOR_ELTS (ary))[i].value;
1836   else if (elem_nchars == 1)
1837     return build_int_cst (cv_unqualified (TREE_TYPE (TREE_TYPE (ary))),
1838                           TREE_STRING_POINTER (ary)[i]);
1839   else
1840     {
1841       tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (ary)));
1842       return native_interpret_expr (type, (const unsigned char *)
1843                                           TREE_STRING_POINTER (ary)
1844                                           + i * elem_nchars, elem_nchars);
1845     }
1846   /* Don't VERIFY_CONSTANT here.  */
1847 }
1848
1849 /* Subroutine of cxx_eval_constant_expression.
1850    Attempt to reduce a field access of a value of class type.  */
1851
1852 static tree
1853 cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
1854                               bool lval,
1855                               bool *non_constant_p, bool *overflow_p)
1856 {
1857   unsigned HOST_WIDE_INT i;
1858   tree field;
1859   tree value;
1860   tree part = TREE_OPERAND (t, 1);
1861   tree orig_whole = TREE_OPERAND (t, 0);
1862   tree whole = cxx_eval_constant_expression (ctx, orig_whole,
1863                                              lval,
1864                                              non_constant_p, overflow_p);
1865   if (TREE_CODE (whole) == PTRMEM_CST)
1866     whole = cplus_expand_constant (whole);
1867   if (whole == orig_whole)
1868     return t;
1869   if (lval)
1870     return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
1871                         whole, part, NULL_TREE);
1872   /* Don't VERIFY_CONSTANT here; we only want to check that we got a
1873      CONSTRUCTOR.  */
1874   if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
1875     {
1876       if (!ctx->quiet)
1877         error ("%qE is not a constant expression", orig_whole);
1878       *non_constant_p = true;
1879     }
1880   if (DECL_MUTABLE_P (part))
1881     {
1882       if (!ctx->quiet)
1883         error ("mutable %qD is not usable in a constant expression", part);
1884       *non_constant_p = true;
1885     }
1886   if (*non_constant_p)
1887     return t;
1888   FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
1889     {
1890       if (field == part)
1891         {
1892           if (value)
1893             return value;
1894           else
1895             /* We're in the middle of initializing it.  */
1896             break;
1897         }
1898     }
1899   if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
1900       && CONSTRUCTOR_NELTS (whole) > 0)
1901     {
1902       /* DR 1188 says we don't have to deal with this.  */
1903       if (!ctx->quiet)
1904         error ("accessing %qD member instead of initialized %qD member in "
1905                "constant expression", part, CONSTRUCTOR_ELT (whole, 0)->index);
1906       *non_constant_p = true;
1907       return t;
1908     }
1909
1910   if (CONSTRUCTOR_NO_IMPLICIT_ZERO (whole))
1911     {
1912       /* 'whole' is part of the aggregate initializer we're currently
1913          building; if there's no initializer for this member yet, that's an
1914          error. */
1915       if (!ctx->quiet)
1916         error ("accessing uninitialized member %qD", part);
1917       *non_constant_p = true;
1918       return t;
1919     }
1920
1921   /* If there's no explicit init for this field, it's value-initialized.  */
1922   value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
1923   return cxx_eval_constant_expression (ctx, value,
1924                                        lval,
1925                                        non_constant_p, overflow_p);
1926 }
1927
1928 /* Subroutine of cxx_eval_constant_expression.
1929    Attempt to reduce a field access of a value of class type that is
1930    expressed as a BIT_FIELD_REF.  */
1931
1932 static tree
1933 cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
1934                         bool lval,
1935                         bool *non_constant_p, bool *overflow_p)
1936 {
1937   tree orig_whole = TREE_OPERAND (t, 0);
1938   tree retval, fldval, utype, mask;
1939   bool fld_seen = false;
1940   HOST_WIDE_INT istart, isize;
1941   tree whole = cxx_eval_constant_expression (ctx, orig_whole,
1942                                              lval,
1943                                              non_constant_p, overflow_p);
1944   tree start, field, value;
1945   unsigned HOST_WIDE_INT i;
1946
1947   if (whole == orig_whole)
1948     return t;
1949   /* Don't VERIFY_CONSTANT here; we only want to check that we got a
1950      CONSTRUCTOR.  */
1951   if (!*non_constant_p
1952       && TREE_CODE (whole) != VECTOR_CST
1953       && TREE_CODE (whole) != CONSTRUCTOR)
1954     {
1955       if (!ctx->quiet)
1956         error ("%qE is not a constant expression", orig_whole);
1957       *non_constant_p = true;
1958     }
1959   if (*non_constant_p)
1960     return t;
1961
1962   if (TREE_CODE (whole) == VECTOR_CST)
1963     return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
1964                          TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
1965
1966   start = TREE_OPERAND (t, 2);
1967   istart = tree_to_shwi (start);
1968   isize = tree_to_shwi (TREE_OPERAND (t, 1));
1969   utype = TREE_TYPE (t);
1970   if (!TYPE_UNSIGNED (utype))
1971     utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
1972   retval = build_int_cst (utype, 0);
1973   FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
1974     {
1975       tree bitpos = bit_position (field);
1976       if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
1977         return value;
1978       if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
1979           && TREE_CODE (value) == INTEGER_CST
1980           && tree_fits_shwi_p (bitpos)
1981           && tree_fits_shwi_p (DECL_SIZE (field)))
1982         {
1983           HOST_WIDE_INT bit = tree_to_shwi (bitpos);
1984           HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
1985           HOST_WIDE_INT shift;
1986           if (bit >= istart && bit + sz <= istart + isize)
1987             {
1988               fldval = fold_convert (utype, value);
1989               mask = build_int_cst_type (utype, -1);
1990               mask = fold_build2 (LSHIFT_EXPR, utype, mask,
1991                                   size_int (TYPE_PRECISION (utype) - sz));
1992               mask = fold_build2 (RSHIFT_EXPR, utype, mask,
1993                                   size_int (TYPE_PRECISION (utype) - sz));
1994               fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
1995               shift = bit - istart;
1996               if (BYTES_BIG_ENDIAN)
1997                 shift = TYPE_PRECISION (utype) - shift - sz;
1998               fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
1999                                     size_int (shift));
2000               retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
2001               fld_seen = true;
2002             }
2003         }
2004     }
2005   if (fld_seen)
2006     return fold_convert (TREE_TYPE (t), retval);
2007   gcc_unreachable ();
2008   return error_mark_node;
2009 }
2010
2011 /* Subroutine of cxx_eval_constant_expression.
2012    Evaluate a short-circuited logical expression T in the context
2013    of a given constexpr CALL.  BAILOUT_VALUE is the value for
2014    early return.  CONTINUE_VALUE is used here purely for
2015    sanity check purposes.  */
2016
2017 static tree
2018 cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
2019                              tree bailout_value, tree continue_value,
2020                              bool lval,
2021                              bool *non_constant_p, bool *overflow_p)
2022 {
2023   tree r;
2024   tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
2025                                            lval,
2026                                            non_constant_p, overflow_p);
2027   VERIFY_CONSTANT (lhs);
2028   if (tree_int_cst_equal (lhs, bailout_value))
2029     return lhs;
2030   gcc_assert (tree_int_cst_equal (lhs, continue_value));
2031   r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
2032                                     lval, non_constant_p,
2033                                     overflow_p);
2034   VERIFY_CONSTANT (r);
2035   return r;
2036 }
2037
2038 /* REF is a COMPONENT_REF designating a particular field.  V is a vector of
2039    CONSTRUCTOR elements to initialize (part of) an object containing that
2040    field.  Return a pointer to the constructor_elt corresponding to the
2041    initialization of the field.  */
2042
2043 static constructor_elt *
2044 base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
2045 {
2046   tree aggr = TREE_OPERAND (ref, 0);
2047   tree field = TREE_OPERAND (ref, 1);
2048   HOST_WIDE_INT i;
2049   constructor_elt *ce;
2050
2051   gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
2052
2053   if (TREE_CODE (aggr) == COMPONENT_REF)
2054     {
2055       constructor_elt *base_ce
2056         = base_field_constructor_elt (v, aggr);
2057       v = CONSTRUCTOR_ELTS (base_ce->value);
2058     }
2059
2060   for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
2061     if (ce->index == field)
2062       return ce;
2063
2064   gcc_unreachable ();
2065   return NULL;
2066 }
2067
2068 /* Some of the expressions fed to the constexpr mechanism are calls to
2069    constructors, which have type void.  In that case, return the type being
2070    initialized by the constructor.  */
2071
2072 static tree
2073 initialized_type (tree t)
2074 {
2075   if (TYPE_P (t))
2076     return t;
2077   tree type = cv_unqualified (TREE_TYPE (t));
2078   if (TREE_CODE (t) == CALL_EXPR || TREE_CODE (t) == AGGR_INIT_EXPR)
2079     {
2080       /* A constructor call has void type, so we need to look deeper.  */
2081       tree fn = get_function_named_in_call (t);
2082       if (fn && TREE_CODE (fn) == FUNCTION_DECL
2083           && DECL_CXX_CONSTRUCTOR_P (fn))
2084         type = DECL_CONTEXT (fn);
2085     }
2086   return type;
2087 }
2088
2089 /* We're about to initialize element INDEX of an array or class from VALUE.
2090    Set up NEW_CTX appropriately by adjusting .object to refer to the
2091    subobject and creating a new CONSTRUCTOR if the element is itself
2092    a class or array.  */
2093
2094 static void
2095 init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
2096                tree index, tree &value)
2097 {
2098   new_ctx = *ctx;
2099
2100   if (index && TREE_CODE (index) != INTEGER_CST
2101       && TREE_CODE (index) != FIELD_DECL)
2102     /* This won't have an element in the new CONSTRUCTOR.  */
2103     return;
2104
2105   tree type = initialized_type (value);
2106   if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
2107     /* A non-aggregate member doesn't get its own CONSTRUCTOR.  */
2108     return;
2109
2110   /* The sub-aggregate initializer might contain a placeholder;
2111      update object to refer to the subobject and ctor to refer to
2112      the (newly created) sub-initializer.  */
2113   if (ctx->object)
2114     new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
2115   tree elt = build_constructor (type, NULL);
2116   CONSTRUCTOR_NO_IMPLICIT_ZERO (elt) = true;
2117   new_ctx.ctor = elt;
2118
2119   if (TREE_CODE (value) == TARGET_EXPR)
2120     /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR.  */
2121     value = TARGET_EXPR_INITIAL (value);
2122 }
2123
2124 /* We're about to process an initializer for a class or array TYPE.  Make
2125    sure that CTX is set up appropriately.  */
2126
2127 static void
2128 verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
2129 {
2130   /* We don't bother building a ctor for an empty base subobject.  */
2131   if (is_empty_class (type))
2132     return;
2133
2134   /* We're in the middle of an initializer that might involve placeholders;
2135      our caller should have created a CONSTRUCTOR for us to put the
2136      initializer into.  We will either return that constructor or T.  */
2137   gcc_assert (ctx->ctor);
2138   gcc_assert (same_type_ignoring_top_level_qualifiers_p
2139               (type, TREE_TYPE (ctx->ctor)));
2140   gcc_assert (CONSTRUCTOR_NELTS (ctx->ctor) == 0);
2141   if (ctx->object)
2142     gcc_assert (same_type_ignoring_top_level_qualifiers_p
2143                 (type, TREE_TYPE (ctx->object)));
2144   gcc_assert (!ctx->object || !DECL_P (ctx->object)
2145               || *(ctx->values->get (ctx->object)) == ctx->ctor);
2146 }
2147
2148 /* Subroutine of cxx_eval_constant_expression.
2149    The expression tree T denotes a C-style array or a C-style
2150    aggregate.  Reduce it to a constant expression.  */
2151
2152 static tree
2153 cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
2154                          bool lval,
2155                          bool *non_constant_p, bool *overflow_p)
2156 {
2157   vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
2158   bool changed = false;
2159   gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
2160
2161   verify_ctor_sanity (ctx, TREE_TYPE (t));
2162   vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
2163   vec_alloc (*p, vec_safe_length (v));
2164
2165   unsigned i; tree index, value;
2166   FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
2167     {
2168       constexpr_ctx new_ctx;
2169       init_subob_ctx (ctx, new_ctx, index, value);
2170       if (new_ctx.ctor != ctx->ctor)
2171         /* If we built a new CONSTRUCTOR, attach it now so that other
2172            initializers can refer to it.  */
2173         CONSTRUCTOR_APPEND_ELT (*p, index, new_ctx.ctor);
2174       tree elt = cxx_eval_constant_expression (&new_ctx, value,
2175                                                lval,
2176                                                non_constant_p, overflow_p);
2177       /* Don't VERIFY_CONSTANT here.  */
2178       if (ctx->quiet && *non_constant_p)
2179         break;
2180       if (elt != value)
2181         changed = true;
2182       if (index && TREE_CODE (index) == COMPONENT_REF)
2183         {
2184           /* This is an initialization of a vfield inside a base
2185              subaggregate that we already initialized; push this
2186              initialization into the previous initialization.  */
2187           constructor_elt *inner = base_field_constructor_elt (*p, index);
2188           inner->value = elt;
2189           changed = true;
2190         }
2191       else if (index
2192                && (TREE_CODE (index) == NOP_EXPR
2193                    || TREE_CODE (index) == POINTER_PLUS_EXPR))
2194         {
2195           /* This is an initializer for an empty base; now that we've
2196              checked that it's constant, we can ignore it.  */
2197           gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
2198           changed = true;
2199         }
2200       else if (new_ctx.ctor != ctx->ctor)
2201         {
2202           /* We appended this element above; update the value.  */
2203           gcc_assert ((*p)->last().index == index);
2204           (*p)->last().value = elt;
2205         }
2206       else
2207         CONSTRUCTOR_APPEND_ELT (*p, index, elt);
2208     }
2209   if (*non_constant_p || !changed)
2210     return t;
2211   t = ctx->ctor;
2212   /* We're done building this CONSTRUCTOR, so now we can interpret an
2213      element without an explicit initializer as value-initialized.  */
2214   CONSTRUCTOR_NO_IMPLICIT_ZERO (t) = false;
2215   if (VECTOR_TYPE_P (TREE_TYPE (t)))
2216     t = fold (t);
2217   return t;
2218 }
2219
2220 /* Subroutine of cxx_eval_constant_expression.
2221    The expression tree T is a VEC_INIT_EXPR which denotes the desired
2222    initialization of a non-static data member of array type.  Reduce it to a
2223    CONSTRUCTOR.
2224
2225    Note that apart from value-initialization (when VALUE_INIT is true),
2226    this is only intended to support value-initialization and the
2227    initializations done by defaulted constructors for classes with
2228    non-static data members of array type.  In this case, VEC_INIT_EXPR_INIT
2229    will either be NULL_TREE for the default constructor, or a COMPONENT_REF
2230    for the copy/move constructor.  */
2231
2232 static tree
2233 cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
2234                      bool value_init, bool lval,
2235                      bool *non_constant_p, bool *overflow_p)
2236 {
2237   tree elttype = TREE_TYPE (atype);
2238   unsigned HOST_WIDE_INT max = tree_to_uhwi (array_type_nelts_top (atype));
2239   verify_ctor_sanity (ctx, atype);
2240   vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
2241   vec_alloc (*p, max + 1);
2242   bool pre_init = false;
2243   unsigned HOST_WIDE_INT i;
2244
2245   /* For the default constructor, build up a call to the default
2246      constructor of the element type.  We only need to handle class types
2247      here, as for a constructor to be constexpr, all members must be
2248      initialized, which for a defaulted default constructor means they must
2249      be of a class type with a constexpr default constructor.  */
2250   if (TREE_CODE (elttype) == ARRAY_TYPE)
2251     /* We only do this at the lowest level.  */;
2252   else if (value_init)
2253     {
2254       init = build_value_init (elttype, tf_warning_or_error);
2255       pre_init = true;
2256     }
2257   else if (!init)
2258     {
2259       vec<tree, va_gc> *argvec = make_tree_vector ();
2260       init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
2261                                         &argvec, elttype, LOOKUP_NORMAL,
2262                                         tf_warning_or_error);
2263       release_tree_vector (argvec);
2264       init = build_aggr_init_expr (TREE_TYPE (init), init);
2265       pre_init = true;
2266     }
2267
2268   for (i = 0; i < max; ++i)
2269     {
2270       tree idx = build_int_cst (size_type_node, i);
2271       tree eltinit;
2272       constexpr_ctx new_ctx;
2273       init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
2274       if (new_ctx.ctor != ctx->ctor)
2275         CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
2276       if (TREE_CODE (elttype) == ARRAY_TYPE)
2277         {
2278           /* A multidimensional array; recurse.  */
2279           if (value_init || init == NULL_TREE)
2280             eltinit = NULL_TREE;
2281           else
2282             eltinit = cp_build_array_ref (input_location, init, idx,
2283                                           tf_warning_or_error);
2284           eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init,
2285                                          lval,
2286                                          non_constant_p, overflow_p);
2287         }
2288       else if (pre_init)
2289         {
2290           /* Initializing an element using value or default initialization
2291              we just pre-built above.  */
2292           eltinit = (cxx_eval_constant_expression
2293                      (&new_ctx, init,
2294                       lval, non_constant_p, overflow_p));
2295         }
2296       else
2297         {
2298           /* Copying an element.  */
2299           gcc_assert (same_type_ignoring_top_level_qualifiers_p
2300                       (atype, TREE_TYPE (init)));
2301           eltinit = cp_build_array_ref (input_location, init, idx,
2302                                         tf_warning_or_error);
2303           if (!real_lvalue_p (init))
2304             eltinit = move (eltinit);
2305           eltinit = force_rvalue (eltinit, tf_warning_or_error);
2306           eltinit = (cxx_eval_constant_expression
2307                      (&new_ctx, eltinit, lval,
2308                       non_constant_p, overflow_p));
2309         }
2310       if (*non_constant_p && !ctx->quiet)
2311         break;
2312       if (new_ctx.ctor != ctx->ctor)
2313         {
2314           /* We appended this element above; update the value.  */
2315           gcc_assert ((*p)->last().index == idx);
2316           (*p)->last().value = eltinit;
2317         }
2318       else
2319         CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
2320     }
2321
2322   if (!*non_constant_p)
2323     {
2324       init = ctx->ctor;
2325       CONSTRUCTOR_NO_IMPLICIT_ZERO (init) = false;
2326     }
2327   return init;
2328 }
2329
2330 static tree
2331 cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
2332                    bool lval,
2333                    bool *non_constant_p, bool *overflow_p)
2334 {
2335   tree atype = TREE_TYPE (t);
2336   tree init = VEC_INIT_EXPR_INIT (t);
2337   tree r = cxx_eval_vec_init_1 (ctx, atype, init,
2338                                 VEC_INIT_EXPR_VALUE_INIT (t),
2339                                 lval, non_constant_p, overflow_p);
2340   if (*non_constant_p)
2341     return t;
2342   else
2343     return r;
2344 }
2345
2346 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
2347    match.  We want to be less strict for simple *& folding; if we have a
2348    non-const temporary that we access through a const pointer, that should
2349    work.  We handle this here rather than change fold_indirect_ref_1
2350    because we're dealing with things like ADDR_EXPR of INTEGER_CST which
2351    don't really make sense outside of constant expression evaluation.  Also
2352    we want to allow folding to COMPONENT_REF, which could cause trouble
2353    with TBAA in fold_indirect_ref_1.
2354
2355    Try to keep this function synced with fold_indirect_ref_1.  */
2356
2357 static tree
2358 cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
2359 {
2360   tree sub, subtype;
2361
2362   sub = op0;
2363   STRIP_NOPS (sub);
2364   subtype = TREE_TYPE (sub);
2365   if (!POINTER_TYPE_P (subtype))
2366     return NULL_TREE;
2367
2368   if (TREE_CODE (sub) == ADDR_EXPR)
2369     {
2370       tree op = TREE_OPERAND (sub, 0);
2371       tree optype = TREE_TYPE (op);
2372
2373       /* *&CONST_DECL -> to the value of the const decl.  */
2374       if (TREE_CODE (op) == CONST_DECL)
2375         return DECL_INITIAL (op);
2376       /* *&p => p;  make sure to handle *&"str"[cst] here.  */
2377       if (same_type_ignoring_top_level_qualifiers_p (optype, type))
2378         {
2379           tree fop = fold_read_from_constant_string (op);
2380           if (fop)
2381             return fop;
2382           else
2383             return op;
2384         }
2385       /* *(foo *)&fooarray => fooarray[0] */
2386       else if (TREE_CODE (optype) == ARRAY_TYPE
2387                && (same_type_ignoring_top_level_qualifiers_p
2388                    (type, TREE_TYPE (optype))))
2389         {
2390           tree type_domain = TYPE_DOMAIN (optype);
2391           tree min_val = size_zero_node;
2392           if (type_domain && TYPE_MIN_VALUE (type_domain))
2393             min_val = TYPE_MIN_VALUE (type_domain);
2394           return build4_loc (loc, ARRAY_REF, type, op, min_val,
2395                              NULL_TREE, NULL_TREE);
2396         }
2397       /* *(foo *)&complexfoo => __real__ complexfoo */
2398       else if (TREE_CODE (optype) == COMPLEX_TYPE
2399                && (same_type_ignoring_top_level_qualifiers_p
2400                    (type, TREE_TYPE (optype))))
2401         return fold_build1_loc (loc, REALPART_EXPR, type, op);
2402       /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
2403       else if (VECTOR_TYPE_P (optype)
2404                && (same_type_ignoring_top_level_qualifiers_p
2405                    (type, TREE_TYPE (optype))))
2406         {
2407           tree part_width = TYPE_SIZE (type);
2408           tree index = bitsize_int (0);
2409           return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
2410         }
2411       /* Also handle conversion to an empty base class, which
2412          is represented with a NOP_EXPR.  */
2413       else if (is_empty_class (type)
2414                && CLASS_TYPE_P (optype)
2415                && DERIVED_FROM_P (type, optype))
2416         {
2417           *empty_base = true;
2418           return op;
2419         }
2420       /* *(foo *)&struct_with_foo_field => COMPONENT_REF */
2421       else if (RECORD_OR_UNION_TYPE_P (optype))
2422         {
2423           tree field = TYPE_FIELDS (optype);
2424           for (; field; field = DECL_CHAIN (field))
2425             if (TREE_CODE (field) == FIELD_DECL
2426                 && integer_zerop (byte_position (field))
2427                 && (same_type_ignoring_top_level_qualifiers_p
2428                     (TREE_TYPE (field), type)))
2429               {
2430                 return fold_build3 (COMPONENT_REF, type, op, field, NULL_TREE);
2431                 break;
2432               }
2433         }
2434     }
2435   else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
2436            && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
2437     {
2438       tree op00 = TREE_OPERAND (sub, 0);
2439       tree op01 = TREE_OPERAND (sub, 1);
2440
2441       STRIP_NOPS (op00);
2442       if (TREE_CODE (op00) == ADDR_EXPR)
2443         {
2444           tree op00type;
2445           op00 = TREE_OPERAND (op00, 0);
2446           op00type = TREE_TYPE (op00);
2447
2448           /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
2449           if (VECTOR_TYPE_P (op00type)
2450               && (same_type_ignoring_top_level_qualifiers_p
2451                   (type, TREE_TYPE (op00type))))
2452             {
2453               HOST_WIDE_INT offset = tree_to_shwi (op01);
2454               tree part_width = TYPE_SIZE (type);
2455               unsigned HOST_WIDE_INT part_widthi = tree_to_shwi (part_width)/BITS_PER_UNIT;
2456               unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
2457               tree index = bitsize_int (indexi);
2458
2459               if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
2460                 return fold_build3_loc (loc,
2461                                         BIT_FIELD_REF, type, op00,
2462                                         part_width, index);
2463
2464             }
2465           /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
2466           else if (TREE_CODE (op00type) == COMPLEX_TYPE
2467                    && (same_type_ignoring_top_level_qualifiers_p
2468                        (type, TREE_TYPE (op00type))))
2469             {
2470               tree size = TYPE_SIZE_UNIT (type);
2471               if (tree_int_cst_equal (size, op01))
2472                 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
2473             }
2474           /* ((foo *)&fooarray)[1] => fooarray[1] */
2475           else if (TREE_CODE (op00type) == ARRAY_TYPE
2476                    && (same_type_ignoring_top_level_qualifiers_p
2477                        (type, TREE_TYPE (op00type))))
2478             {
2479               tree type_domain = TYPE_DOMAIN (op00type);
2480               tree min_val = size_zero_node;
2481               if (type_domain && TYPE_MIN_VALUE (type_domain))
2482                 min_val = TYPE_MIN_VALUE (type_domain);
2483               op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
2484                                      TYPE_SIZE_UNIT (type));
2485               op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
2486               return build4_loc (loc, ARRAY_REF, type, op00, op01,
2487                                  NULL_TREE, NULL_TREE);
2488             }
2489           /* Also handle conversion to an empty base class, which
2490              is represented with a NOP_EXPR.  */
2491           else if (is_empty_class (type)
2492                    && CLASS_TYPE_P (op00type)
2493                    && DERIVED_FROM_P (type, op00type))
2494             {
2495               *empty_base = true;
2496               return op00;
2497             }
2498           /* ((foo *)&struct_with_foo_field)[1] => COMPONENT_REF */
2499           else if (RECORD_OR_UNION_TYPE_P (op00type))
2500             {
2501               tree field = TYPE_FIELDS (op00type);
2502               for (; field; field = DECL_CHAIN (field))
2503                 if (TREE_CODE (field) == FIELD_DECL
2504                     && tree_int_cst_equal (byte_position (field), op01)
2505                     && (same_type_ignoring_top_level_qualifiers_p
2506                         (TREE_TYPE (field), type)))
2507                   {
2508                     return fold_build3 (COMPONENT_REF, type, op00,
2509                                         field, NULL_TREE);
2510                     break;
2511                   }
2512             }
2513         }
2514     }
2515   /* *(foo *)fooarrptr => (*fooarrptr)[0] */
2516   else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
2517            && (same_type_ignoring_top_level_qualifiers_p
2518                (type, TREE_TYPE (TREE_TYPE (subtype)))))
2519     {
2520       tree type_domain;
2521       tree min_val = size_zero_node;
2522       tree newsub = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL);
2523       if (newsub)
2524         sub = newsub;
2525       else
2526         sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
2527       type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
2528       if (type_domain && TYPE_MIN_VALUE (type_domain))
2529         min_val = TYPE_MIN_VALUE (type_domain);
2530       return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
2531                          NULL_TREE);
2532     }
2533
2534   return NULL_TREE;
2535 }
2536
2537 static tree
2538 cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
2539                        bool lval,
2540                        bool *non_constant_p, bool *overflow_p)
2541 {
2542   tree orig_op0 = TREE_OPERAND (t, 0);
2543   bool empty_base = false;
2544
2545   /* First try to simplify it directly.  */
2546   tree r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), orig_op0,
2547                                   &empty_base);
2548   if (!r)
2549     {
2550       /* If that didn't work, evaluate the operand first.  */
2551       tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
2552                                                /*lval*/false, non_constant_p,
2553                                                overflow_p);
2554       /* Don't VERIFY_CONSTANT here.  */
2555       if (*non_constant_p)
2556         return t;
2557
2558       r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0,
2559                                  &empty_base);
2560       if (r == NULL_TREE)
2561         {
2562           /* We couldn't fold to a constant value.  Make sure it's not
2563              something we should have been able to fold.  */
2564           tree sub = op0;
2565           STRIP_NOPS (sub);
2566           if (TREE_CODE (sub) == ADDR_EXPR)
2567             {
2568               gcc_assert (!same_type_ignoring_top_level_qualifiers_p
2569                           (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
2570               /* DR 1188 says we don't have to deal with this.  */
2571               if (!ctx->quiet)
2572                 error ("accessing value of %qE through a %qT glvalue in a "
2573                        "constant expression", build_fold_indirect_ref (sub),
2574                        TREE_TYPE (t));
2575               *non_constant_p = true;
2576               return t;
2577             }
2578
2579           if (lval && op0 != orig_op0)
2580             return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
2581           if (!lval)
2582             VERIFY_CONSTANT (t);
2583           return t;
2584         }
2585     }
2586
2587   r = cxx_eval_constant_expression (ctx, r,
2588                                     lval, non_constant_p, overflow_p);
2589   if (*non_constant_p)
2590     return t;
2591
2592   /* If we're pulling out the value of an empty base, make sure
2593      that the whole object is constant and then return an empty
2594      CONSTRUCTOR.  */
2595   if (empty_base && !lval)
2596     {
2597       VERIFY_CONSTANT (r);
2598       r = build_constructor (TREE_TYPE (t), NULL);
2599       TREE_CONSTANT (r) = true;
2600     }
2601
2602   return r;
2603 }
2604
2605 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
2606    Shared between potential_constant_expression and
2607    cxx_eval_constant_expression.  */
2608
2609 static void
2610 non_const_var_error (tree r)
2611 {
2612   tree type = TREE_TYPE (r);
2613   error ("the value of %qD is not usable in a constant "
2614          "expression", r);
2615   /* Avoid error cascade.  */
2616   if (DECL_INITIAL (r) == error_mark_node)
2617     return;
2618   if (DECL_DECLARED_CONSTEXPR_P (r))
2619     inform (DECL_SOURCE_LOCATION (r),
2620             "%qD used in its own initializer", r);
2621   else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
2622     {
2623       if (!CP_TYPE_CONST_P (type))
2624         inform (DECL_SOURCE_LOCATION (r),
2625                 "%q#D is not const", r);
2626       else if (CP_TYPE_VOLATILE_P (type))
2627         inform (DECL_SOURCE_LOCATION (r),
2628                 "%q#D is volatile", r);
2629       else if (!DECL_INITIAL (r)
2630                || !TREE_CONSTANT (DECL_INITIAL (r)))
2631         inform (DECL_SOURCE_LOCATION (r),
2632                 "%qD was not initialized with a constant "
2633                 "expression", r);
2634       else
2635         gcc_unreachable ();
2636     }
2637   else
2638     {
2639       if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
2640         inform (DECL_SOURCE_LOCATION (r),
2641                 "%qD was not declared %<constexpr%>", r);
2642       else
2643         inform (DECL_SOURCE_LOCATION (r),
2644                 "%qD does not have integral or enumeration type",
2645                 r);
2646     }
2647 }
2648
2649 /* Subroutine of cxx_eval_constant_expression.
2650    Like cxx_eval_unary_expression, except for trinary expressions.  */
2651
2652 static tree
2653 cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
2654                              bool lval,
2655                              bool *non_constant_p, bool *overflow_p)
2656 {
2657   int i;
2658   tree args[3];
2659   tree val;
2660
2661   for (i = 0; i < 3; i++)
2662     {
2663       args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
2664                                               lval,
2665                                               non_constant_p, overflow_p);
2666       VERIFY_CONSTANT (args[i]);
2667     }
2668
2669   val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
2670                           args[0], args[1], args[2]);
2671   if (val == NULL_TREE)
2672     return t;
2673   VERIFY_CONSTANT (val);
2674   return val;
2675 }
2676
2677 bool
2678 var_in_constexpr_fn (tree t)
2679 {
2680   tree ctx = DECL_CONTEXT (t);
2681   return (cxx_dialect >= cxx14 && ctx && TREE_CODE (ctx) == FUNCTION_DECL
2682           && DECL_DECLARED_CONSTEXPR_P (ctx));
2683 }
2684
2685 /* Evaluate an INIT_EXPR or MODIFY_EXPR.  */
2686
2687 static tree
2688 cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
2689                            bool lval,
2690                            bool *non_constant_p, bool *overflow_p)
2691 {
2692   constexpr_ctx new_ctx = *ctx;
2693
2694   tree init = TREE_OPERAND (t, 1);
2695   if (TREE_CLOBBER_P (init))
2696     /* Just ignore clobbers.  */
2697     return void_node;
2698
2699   /* First we figure out where we're storing to.  */
2700   tree target = TREE_OPERAND (t, 0);
2701   tree type = TREE_TYPE (target);
2702   target = cxx_eval_constant_expression (ctx, target,
2703                                          true,
2704                                          non_constant_p, overflow_p);
2705   if (*non_constant_p)
2706     return t;
2707
2708   if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (target), type))
2709     {
2710       /* For initialization of an empty base, the original target will be
2711          *(base*)this, which the above evaluation resolves to the object
2712          argument, which has the derived type rather than the base type.  In
2713          this situation, just evaluate the initializer and return, since
2714          there's no actual data to store.  */
2715       gcc_assert (is_empty_class (type));
2716       return cxx_eval_constant_expression (ctx, init, false,
2717                                            non_constant_p, overflow_p);
2718     }
2719
2720   /* And then find the underlying variable.  */
2721   vec<tree,va_gc> *refs = make_tree_vector();
2722   tree object = NULL_TREE;
2723   for (tree probe = target; object == NULL_TREE; )
2724     {
2725       switch (TREE_CODE (probe))
2726         {
2727         case BIT_FIELD_REF:
2728         case COMPONENT_REF:
2729         case ARRAY_REF:
2730           vec_safe_push (refs, TREE_OPERAND (probe, 1));
2731           vec_safe_push (refs, TREE_TYPE (probe));
2732           probe = TREE_OPERAND (probe, 0);
2733           break;
2734
2735         default:
2736           object = probe;
2737         }
2738     }
2739
2740   /* And then find/build up our initializer for the path to the subobject
2741      we're initializing.  */
2742   tree *valp;
2743   if (DECL_P (object))
2744     valp = ctx->values->get (object);
2745   else
2746     valp = NULL;
2747   if (!valp)
2748     {
2749       /* A constant-expression cannot modify objects from outside the
2750          constant-expression.  */
2751       if (!ctx->quiet)
2752         error ("modification of %qE is not a constant-expression", object);
2753       *non_constant_p = true;
2754       return t;
2755     }
2756   type = TREE_TYPE (object);
2757   bool no_zero_init = true;
2758   while (!refs->is_empty())
2759     {
2760       if (*valp == NULL_TREE)
2761         {
2762           *valp = build_constructor (type, NULL);
2763           CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp) = no_zero_init;
2764         }
2765       /* If the value of object is already zero-initialized, any new ctors for
2766          subobjects will also be zero-initialized.  */
2767       no_zero_init = CONSTRUCTOR_NO_IMPLICIT_ZERO (*valp);
2768
2769       enum tree_code code = TREE_CODE (type);
2770       type = refs->pop();
2771       tree index = refs->pop();
2772
2773       constructor_elt *cep = NULL;
2774       if (code == ARRAY_TYPE)
2775         {
2776           HOST_WIDE_INT i
2777             = find_array_ctor_elt (*valp, index, /*insert*/true);
2778           gcc_assert (i >= 0);
2779           cep = CONSTRUCTOR_ELT (*valp, i);
2780           gcc_assert (TREE_CODE (cep->index) != RANGE_EXPR);
2781         }
2782       else
2783         {
2784           gcc_assert (TREE_CODE (index) == FIELD_DECL);
2785           for (unsigned HOST_WIDE_INT idx = 0;
2786                vec_safe_iterate (CONSTRUCTOR_ELTS (*valp), idx, &cep);
2787                idx++)
2788             if (index == cep->index)
2789               break;
2790           if (!cep)
2791             {
2792               constructor_elt ce = { index, NULL_TREE };
2793               cep = vec_safe_push (CONSTRUCTOR_ELTS (*valp), ce);
2794             }
2795         }
2796       valp = &cep->value;
2797     }
2798   release_tree_vector (refs);
2799
2800   if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
2801     {
2802       /* Create a new CONSTRUCTOR in case evaluation of the initializer
2803          wants to modify it.  */
2804       if (*valp == NULL_TREE)
2805         {
2806           *valp = new_ctx.ctor = build_constructor (type, NULL);
2807           CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = no_zero_init;
2808         }
2809       else
2810         new_ctx.ctor = *valp;
2811       new_ctx.object = target;
2812     }
2813
2814   init = cxx_eval_constant_expression (&new_ctx, init, false,
2815                                        non_constant_p, overflow_p);
2816   if (target == object)
2817     {
2818       /* The hash table might have moved since the get earlier.  */
2819       valp = ctx->values->get (object);
2820       if (TREE_CODE (init) == CONSTRUCTOR)
2821         /* An outer ctx->ctor might be pointing to *valp, so just replace
2822            its contents.  */
2823         CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
2824       else
2825         *valp = init;
2826     }
2827   else
2828     *valp = init;
2829
2830   if (*non_constant_p)
2831     return t;
2832   else if (lval)
2833     return target;
2834   else
2835     return init;
2836 }
2837
2838 /* Evaluate a ++ or -- expression.  */
2839
2840 static tree
2841 cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
2842                               bool lval,
2843                               bool *non_constant_p, bool *overflow_p)
2844 {
2845   enum tree_code code = TREE_CODE (t);
2846   tree type = TREE_TYPE (t);
2847   tree op = TREE_OPERAND (t, 0);
2848   tree offset = TREE_OPERAND (t, 1);
2849   gcc_assert (TREE_CONSTANT (offset));
2850
2851   /* The operand as an lvalue.  */
2852   op = cxx_eval_constant_expression (ctx, op, true,
2853                                      non_constant_p, overflow_p);
2854
2855   /* The operand as an rvalue.  */
2856   tree val = rvalue (op);
2857   val = cxx_eval_constant_expression (ctx, val, false,
2858                                       non_constant_p, overflow_p);
2859   /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
2860      a local array in a constexpr function.  */
2861   bool ptr = POINTER_TYPE_P (TREE_TYPE (val));
2862   if (!ptr)
2863     VERIFY_CONSTANT (val);
2864
2865   /* The modified value.  */
2866   bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
2867   tree mod;
2868   if (POINTER_TYPE_P (type))
2869     {
2870       /* The middle end requires pointers to use POINTER_PLUS_EXPR.  */
2871       offset = convert_to_ptrofftype (offset);
2872       if (!inc)
2873         offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
2874       mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
2875     }
2876   else
2877     mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
2878   if (!ptr)
2879     VERIFY_CONSTANT (mod);
2880
2881   /* Storing the modified value.  */
2882   tree store = build2 (MODIFY_EXPR, type, op, mod);
2883   cxx_eval_constant_expression (ctx, store,
2884                                 true, non_constant_p, overflow_p);
2885
2886   /* And the value of the expression.  */
2887   if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
2888     {
2889       /* Prefix ops are lvalues.  */
2890       if (lval)
2891         return op;
2892       else
2893         /* But we optimize when the caller wants an rvalue.  */
2894         return mod;
2895     }
2896   else
2897     /* Postfix ops are rvalues.  */
2898     return val;
2899 }
2900
2901 /* Predicates for the meaning of *jump_target.  */
2902
2903 static bool
2904 returns (tree *jump_target)
2905 {
2906   return *jump_target
2907     && TREE_CODE (*jump_target) == RETURN_EXPR;
2908 }
2909
2910 static bool
2911 breaks (tree *jump_target)
2912 {
2913   return *jump_target
2914     && TREE_CODE (*jump_target) == LABEL_DECL
2915     && LABEL_DECL_BREAK (*jump_target);
2916 }
2917
2918 static bool
2919 continues (tree *jump_target)
2920 {
2921   return *jump_target
2922     && TREE_CODE (*jump_target) == LABEL_DECL
2923     && LABEL_DECL_CONTINUE (*jump_target);
2924 }
2925
2926 static bool
2927 switches (tree *jump_target)
2928 {
2929   return *jump_target
2930     && TREE_CODE (*jump_target) == INTEGER_CST;
2931 }
2932
2933 /* Subroutine of cxx_eval_statement_list.  Determine whether the statement
2934    at I matches *jump_target.  If we're looking for a case label and we see
2935    the default label, copy I into DEFAULT_LABEL.  */
2936
2937 static bool
2938 label_matches (tree *jump_target, tree_stmt_iterator i,
2939                tree_stmt_iterator& default_label)
2940 {
2941   tree stmt = tsi_stmt (i);
2942   switch (TREE_CODE (*jump_target))
2943     {
2944     case LABEL_DECL:
2945       if (TREE_CODE (stmt) == LABEL_EXPR
2946           && LABEL_EXPR_LABEL (stmt) == *jump_target)
2947         return true;
2948       break;
2949
2950     case INTEGER_CST:
2951       if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
2952         {
2953           if (!CASE_LOW (stmt))
2954             default_label = i;
2955           else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
2956             return true;
2957         }
2958       break;
2959
2960     default:
2961       gcc_unreachable ();
2962     }
2963   return false;
2964 }
2965
2966 /* Evaluate a STATEMENT_LIST for side-effects.  Handles various jump
2967    semantics, for switch, break, continue, and return.  */
2968
2969 static tree
2970 cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
2971                          bool *non_constant_p, bool *overflow_p,
2972                          tree *jump_target)
2973 {
2974   tree_stmt_iterator i;
2975   tree_stmt_iterator default_label = tree_stmt_iterator();
2976   tree local_target;
2977   /* In a statement-expression we want to return the last value.  */
2978   tree r = NULL_TREE;
2979   if (!jump_target)
2980     {
2981       local_target = NULL_TREE;
2982       jump_target = &local_target;
2983     }
2984   for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
2985     {
2986     reenter:
2987       tree stmt = tsi_stmt (i);
2988       if (*jump_target)
2989         {
2990           if (TREE_CODE (stmt) == STATEMENT_LIST)
2991             /* The label we want might be inside.  */;
2992           else if (label_matches (jump_target, i, default_label))
2993             /* Found it.  */
2994             *jump_target = NULL_TREE;
2995           else
2996             continue;
2997         }
2998       r = cxx_eval_constant_expression (ctx, stmt, false,
2999                                         non_constant_p, overflow_p,
3000                                         jump_target);
3001       if (*non_constant_p)
3002         break;
3003       if (returns (jump_target) || breaks (jump_target))
3004         break;
3005     }
3006   if (switches (jump_target) && !tsi_end_p (default_label))
3007     {
3008       i = default_label;
3009       *jump_target = NULL_TREE;
3010       goto reenter;
3011     }
3012   return r;
3013 }
3014
3015 /* Evaluate a LOOP_EXPR for side-effects.  Handles break and return
3016    semantics; continue semantics are covered by cxx_eval_statement_list.  */
3017
3018 static tree
3019 cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
3020                     bool *non_constant_p, bool *overflow_p,
3021                     tree *jump_target)
3022 {
3023   tree body = TREE_OPERAND (t, 0);
3024   while (true)
3025     {
3026       cxx_eval_statement_list (ctx, body,
3027                                non_constant_p, overflow_p, jump_target);
3028       if (returns (jump_target) || breaks (jump_target) || *non_constant_p)
3029         break;
3030     }
3031   if (breaks (jump_target))
3032     *jump_target = NULL_TREE;
3033   return NULL_TREE;
3034 }
3035
3036 /* Evaluate a SWITCH_EXPR for side-effects.  Handles switch and break jump
3037    semantics.  */
3038
3039 static tree
3040 cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
3041                       bool *non_constant_p, bool *overflow_p,
3042                       tree *jump_target)
3043 {
3044   tree cond = TREE_OPERAND (t, 0);
3045   cond = cxx_eval_constant_expression (ctx, cond, false,
3046                                        non_constant_p, overflow_p);
3047   VERIFY_CONSTANT (cond);
3048   *jump_target = cond;
3049
3050   tree body = TREE_OPERAND (t, 1);
3051   cxx_eval_statement_list (ctx, body,
3052                            non_constant_p, overflow_p, jump_target);
3053   if (breaks (jump_target) || switches (jump_target))
3054     *jump_target = NULL_TREE;
3055   return NULL_TREE;
3056 }
3057
3058 /* Subroutine of cxx_eval_constant_expression.
3059    Attempt to reduce a POINTER_PLUS_EXPR expression T.  */
3060
3061 static tree
3062 cxx_eval_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
3063                                   bool lval, bool *non_constant_p,
3064                                   bool *overflow_p)
3065 {
3066   tree orig_type = TREE_TYPE (t);
3067   tree op00 = TREE_OPERAND (t, 0);
3068   tree op01 = TREE_OPERAND (t, 1);
3069   location_t loc = EXPR_LOCATION (t);
3070
3071   op00 = cxx_eval_constant_expression (ctx, op00, lval,
3072                                        non_constant_p, overflow_p);
3073
3074   STRIP_NOPS (op00);
3075   if (TREE_CODE (op00) != ADDR_EXPR)
3076     return NULL_TREE;
3077
3078   op00 = TREE_OPERAND (op00, 0);
3079
3080   /* &A[i] p+ j => &A[i + j] */
3081   if (TREE_CODE (op00) == ARRAY_REF
3082       && TREE_CODE (TREE_OPERAND (op00, 1)) == INTEGER_CST
3083       && TREE_CODE (op01) == INTEGER_CST
3084       && TYPE_SIZE_UNIT (TREE_TYPE (op00))
3085       && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (op00))) == INTEGER_CST)
3086     {
3087       tree type = TREE_TYPE (op00);
3088       t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (op00, 1));
3089       tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (op00, 0)));
3090       /* Don't fold an out-of-bound access.  */
3091       if (!tree_int_cst_le (t, nelts))
3092         return NULL_TREE;
3093       op01 = cp_fold_convert (ssizetype, op01);
3094       /* Don't fold if op01 can't be divided exactly by TYPE_SIZE_UNIT.
3095          constexpr int A[1]; ... (char *)&A[0] + 1 */
3096       if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
3097                                            op01, TYPE_SIZE_UNIT (type))))
3098         return NULL_TREE;
3099       /* Make sure to treat the second operand of POINTER_PLUS_EXPR
3100          as signed.  */
3101       op01 = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, op01,
3102                               TYPE_SIZE_UNIT (type));
3103       t = size_binop_loc (loc, PLUS_EXPR, op01, t);
3104       t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (op00, 0),
3105                       t, NULL_TREE, NULL_TREE);
3106       t = cp_build_addr_expr (t, tf_warning_or_error);
3107       t = cp_fold_convert (orig_type, t);
3108       return cxx_eval_constant_expression (ctx, t, lval, non_constant_p,
3109                                            overflow_p);
3110     }
3111
3112   return NULL_TREE;
3113 }
3114
3115 /* Attempt to reduce the expression T to a constant value.
3116    On failure, issue diagnostic and return error_mark_node.  */
3117 /* FIXME unify with c_fully_fold */
3118 /* FIXME overflow_p is too global */
3119
3120 static tree
3121 cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
3122                               bool lval,
3123                               bool *non_constant_p, bool *overflow_p,
3124                               tree *jump_target)
3125 {
3126   constexpr_ctx new_ctx;
3127   tree r = t;
3128
3129   if (t == error_mark_node)
3130     {
3131       *non_constant_p = true;
3132       return t;
3133     }
3134   if (CONSTANT_CLASS_P (t))
3135     {
3136       if (TREE_OVERFLOW (t) && (!flag_permissive || ctx->quiet))
3137         *overflow_p = true;
3138       return t;
3139     }
3140
3141   switch (TREE_CODE (t))
3142     {
3143     case RESULT_DECL:
3144       if (lval)
3145         return t;
3146       /* We ask for an rvalue for the RESULT_DECL when indirecting
3147          through an invisible reference, or in named return value
3148          optimization.  */
3149       return (*ctx->values->get (t));
3150
3151     case VAR_DECL:
3152     case CONST_DECL:
3153       /* We used to not check lval for CONST_DECL, but darwin.c uses
3154          CONST_DECL for aggregate constants.  */
3155       if (lval)
3156         return t;
3157       if (ctx->strict)
3158         r = decl_really_constant_value (t);
3159       else
3160         r = decl_constant_value (t);
3161       if (TREE_CODE (r) == TARGET_EXPR
3162           && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
3163         r = TARGET_EXPR_INITIAL (r);
3164       if (VAR_P (r))
3165         if (tree *p = ctx->values->get (r))
3166           r = *p;
3167       if (DECL_P (r))
3168         {
3169           if (!ctx->quiet)
3170             non_const_var_error (r);
3171           *non_constant_p = true;
3172         }
3173       break;
3174
3175     case FUNCTION_DECL:
3176     case TEMPLATE_DECL:
3177     case LABEL_DECL:
3178     case LABEL_EXPR:
3179     case CASE_LABEL_EXPR:
3180       return t;
3181
3182     case PARM_DECL:
3183       if (lval && TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
3184         /* glvalue use.  */;
3185       else if (tree *p = ctx->values->get (r))
3186         r = *p;
3187       else if (lval)
3188         /* Defer in case this is only used for its type.  */;
3189       else if (TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
3190         /* Defer, there's no lvalue->rvalue conversion.  */;
3191       else if (is_empty_class (TREE_TYPE (t)))
3192         {
3193           /* If the class is empty, we aren't actually loading anything.  */
3194           r = build_constructor (TREE_TYPE (t), NULL);
3195           TREE_CONSTANT (r) = true;
3196         }
3197       else
3198         {
3199           if (!ctx->quiet)
3200             error ("%qE is not a constant expression", t);
3201           *non_constant_p = true;
3202         }
3203       break;
3204
3205     case CALL_EXPR:
3206     case AGGR_INIT_EXPR:
3207       r = cxx_eval_call_expression (ctx, t, lval,
3208                                     non_constant_p, overflow_p);
3209       break;
3210
3211     case DECL_EXPR:
3212       {
3213         r = DECL_EXPR_DECL (t);
3214         if (AGGREGATE_TYPE_P (TREE_TYPE (r))
3215             || VECTOR_TYPE_P (TREE_TYPE (r)))
3216           {
3217             new_ctx = *ctx;
3218             new_ctx.object = r;
3219             new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
3220             CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
3221             new_ctx.values->put (r, new_ctx.ctor);
3222             ctx = &new_ctx;
3223           }
3224
3225         if (tree init = DECL_INITIAL (r))
3226           {
3227             init = cxx_eval_constant_expression (ctx, init,
3228                                                  false,
3229                                                  non_constant_p, overflow_p);
3230             ctx->values->put (r, init);
3231           }
3232         else if (ctx == &new_ctx)
3233           /* We gave it a CONSTRUCTOR above.  */;
3234         else
3235           ctx->values->put (r, NULL_TREE);
3236       }
3237       break;
3238
3239     case TARGET_EXPR:
3240       if (!literal_type_p (TREE_TYPE (t)))
3241         {
3242           if (!ctx->quiet)
3243             {
3244               error ("temporary of non-literal type %qT in a "
3245                      "constant expression", TREE_TYPE (t));
3246               explain_non_literal_class (TREE_TYPE (t));
3247             }
3248           *non_constant_p = true;
3249           break;
3250         }
3251       if ((AGGREGATE_TYPE_P (TREE_TYPE (t)) || VECTOR_TYPE_P (TREE_TYPE (t))))
3252         {
3253           /* We're being expanded without an explicit target, so start
3254              initializing a new object; expansion with an explicit target
3255              strips the TARGET_EXPR before we get here.  */
3256           new_ctx = *ctx;
3257           new_ctx.ctor = build_constructor (TREE_TYPE (t), NULL);
3258           CONSTRUCTOR_NO_IMPLICIT_ZERO (new_ctx.ctor) = true;
3259           new_ctx.object = TARGET_EXPR_SLOT (t);
3260           ctx->values->put (new_ctx.object, new_ctx.ctor);
3261           ctx = &new_ctx;
3262         }
3263       /* Pass false for 'lval' because this indicates
3264          initialization of a temporary.  */
3265       r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3266                                         false,
3267                                         non_constant_p, overflow_p);
3268       if (!*non_constant_p)
3269         /* Adjust the type of the result to the type of the temporary.  */
3270         r = adjust_temp_type (TREE_TYPE (t), r);
3271       if (lval)
3272         {
3273           tree slot = TARGET_EXPR_SLOT (t);
3274           ctx->values->put (slot, r);
3275           return slot;
3276         }
3277       break;
3278
3279     case INIT_EXPR:
3280     case MODIFY_EXPR:
3281       r = cxx_eval_store_expression (ctx, t, lval,
3282                                      non_constant_p, overflow_p);
3283       break;
3284
3285     case SCOPE_REF:
3286       r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3287                                         lval,
3288                                         non_constant_p, overflow_p);
3289       break;
3290
3291     case RETURN_EXPR:
3292       if (TREE_OPERAND (t, 0) != NULL_TREE)
3293         r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3294                                           lval,
3295                                           non_constant_p, overflow_p);
3296       *jump_target = t;
3297       break;
3298
3299     case SAVE_EXPR:
3300       /* Avoid evaluating a SAVE_EXPR more than once.  */
3301       if (tree *p = ctx->values->get (t))
3302         r = *p;
3303       else
3304         {
3305           r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false,
3306                                             non_constant_p, overflow_p);
3307           ctx->values->put (t, r);
3308         }
3309       break;
3310
3311     case NON_LVALUE_EXPR:
3312     case TRY_CATCH_EXPR:
3313     case TRY_BLOCK:
3314     case CLEANUP_POINT_EXPR:
3315     case MUST_NOT_THROW_EXPR:
3316     case EXPR_STMT:
3317     case EH_SPEC_BLOCK:
3318       r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3319                                         lval,
3320                                         non_constant_p, overflow_p,
3321                                         jump_target);
3322       break;
3323
3324     case TRY_FINALLY_EXPR:
3325       r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
3326                                         non_constant_p, overflow_p,
3327                                         jump_target);
3328       if (!*non_constant_p)
3329         /* Also evaluate the cleanup.  */
3330         cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), true,
3331                                       non_constant_p, overflow_p,
3332                                       jump_target);
3333       break;
3334
3335       /* These differ from cxx_eval_unary_expression in that this doesn't
3336          check for a constant operand or result; an address can be
3337          constant without its operand being, and vice versa.  */
3338     case INDIRECT_REF:
3339       r = cxx_eval_indirect_ref (ctx, t, lval,
3340                                  non_constant_p, overflow_p);
3341       break;
3342
3343     case ADDR_EXPR:
3344       {
3345         tree oldop = TREE_OPERAND (t, 0);
3346         tree op = cxx_eval_constant_expression (ctx, oldop,
3347                                                 /*lval*/true,
3348                                                 non_constant_p, overflow_p);
3349         /* Don't VERIFY_CONSTANT here.  */
3350         if (*non_constant_p)
3351           return t;
3352         gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
3353         /* This function does more aggressive folding than fold itself.  */
3354         r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
3355         if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
3356           return t;
3357         break;
3358       }
3359
3360     case REALPART_EXPR:
3361     case IMAGPART_EXPR:
3362     case CONJ_EXPR:
3363     case FIX_TRUNC_EXPR:
3364     case FLOAT_EXPR:
3365     case NEGATE_EXPR:
3366     case ABS_EXPR:
3367     case BIT_NOT_EXPR:
3368     case TRUTH_NOT_EXPR:
3369     case FIXED_CONVERT_EXPR:
3370       r = cxx_eval_unary_expression (ctx, t, lval,
3371                                      non_constant_p, overflow_p);
3372       break;
3373
3374     case SIZEOF_EXPR:
3375       if (SIZEOF_EXPR_TYPE_P (t))
3376         r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
3377                                         SIZEOF_EXPR, false);
3378       else if (TYPE_P (TREE_OPERAND (t, 0)))
3379         r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
3380                                         false);
3381       else
3382         r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
3383                                         false);
3384       if (r == error_mark_node)
3385         r = size_one_node;
3386       VERIFY_CONSTANT (r);
3387       break;
3388
3389     case COMPOUND_EXPR:
3390       {
3391         /* check_return_expr sometimes wraps a TARGET_EXPR in a
3392            COMPOUND_EXPR; don't get confused.  Also handle EMPTY_CLASS_EXPR
3393            introduced by build_call_a.  */
3394         tree op0 = TREE_OPERAND (t, 0);
3395         tree op1 = TREE_OPERAND (t, 1);
3396         STRIP_NOPS (op1);
3397         if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
3398             || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
3399           r = cxx_eval_constant_expression (ctx, op0,
3400                                             lval, non_constant_p, overflow_p,
3401                                             jump_target);
3402         else
3403           {
3404             /* Check that the LHS is constant and then discard it.  */
3405             cxx_eval_constant_expression (ctx, op0,
3406                                           true, non_constant_p, overflow_p,
3407                                           jump_target);
3408             op1 = TREE_OPERAND (t, 1);
3409             r = cxx_eval_constant_expression (ctx, op1,
3410                                               lval, non_constant_p, overflow_p,
3411                                               jump_target);
3412           }
3413       }
3414       break;
3415
3416     case POINTER_PLUS_EXPR:
3417       r = cxx_eval_pointer_plus_expression (ctx, t, lval, non_constant_p,
3418                                             overflow_p);
3419       if (r)
3420         break;
3421       /* else fall through */
3422
3423     case PLUS_EXPR:
3424     case MINUS_EXPR:
3425     case MULT_EXPR:
3426     case TRUNC_DIV_EXPR:
3427     case CEIL_DIV_EXPR:
3428     case FLOOR_DIV_EXPR:
3429     case ROUND_DIV_EXPR:
3430     case TRUNC_MOD_EXPR:
3431     case CEIL_MOD_EXPR:
3432     case ROUND_MOD_EXPR:
3433     case RDIV_EXPR:
3434     case EXACT_DIV_EXPR:
3435     case MIN_EXPR:
3436     case MAX_EXPR:
3437     case LSHIFT_EXPR:
3438     case RSHIFT_EXPR:
3439     case LROTATE_EXPR:
3440     case RROTATE_EXPR:
3441     case BIT_IOR_EXPR:
3442     case BIT_XOR_EXPR:
3443     case BIT_AND_EXPR:
3444     case TRUTH_XOR_EXPR:
3445     case LT_EXPR:
3446     case LE_EXPR:
3447     case GT_EXPR:
3448     case GE_EXPR:
3449     case EQ_EXPR:
3450     case NE_EXPR:
3451     case UNORDERED_EXPR:
3452     case ORDERED_EXPR:
3453     case UNLT_EXPR:
3454     case UNLE_EXPR:
3455     case UNGT_EXPR:
3456     case UNGE_EXPR:
3457     case UNEQ_EXPR:
3458     case LTGT_EXPR:
3459     case RANGE_EXPR:
3460     case COMPLEX_EXPR:
3461       r = cxx_eval_binary_expression (ctx, t, lval,
3462                                       non_constant_p, overflow_p);
3463       break;
3464
3465       /* fold can introduce non-IF versions of these; still treat them as
3466          short-circuiting.  */
3467     case TRUTH_AND_EXPR:
3468     case TRUTH_ANDIF_EXPR:
3469       r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
3470                                        boolean_true_node,
3471                                        lval,
3472                                        non_constant_p, overflow_p);
3473       break;
3474
3475     case TRUTH_OR_EXPR:
3476     case TRUTH_ORIF_EXPR:
3477       r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
3478                                        boolean_false_node,
3479                                        lval,
3480                                        non_constant_p, overflow_p);
3481       break;
3482
3483     case ARRAY_REF:
3484       r = cxx_eval_array_reference (ctx, t, lval,
3485                                     non_constant_p, overflow_p);
3486       break;
3487
3488     case COMPONENT_REF:
3489       if (is_overloaded_fn (t))
3490         {
3491           /* We can only get here in checking mode via 
3492              build_non_dependent_expr,  because any expression that
3493              calls or takes the address of the function will have
3494              pulled a FUNCTION_DECL out of the COMPONENT_REF.  */
3495           gcc_checking_assert (ctx->quiet || errorcount);
3496           *non_constant_p = true;
3497           return t;
3498         }
3499       r = cxx_eval_component_reference (ctx, t, lval,
3500                                         non_constant_p, overflow_p);
3501       break;
3502
3503     case BIT_FIELD_REF:
3504       r = cxx_eval_bit_field_ref (ctx, t, lval,
3505                                   non_constant_p, overflow_p);
3506       break;
3507
3508     case COND_EXPR:
3509     case VEC_COND_EXPR:
3510       r = cxx_eval_conditional_expression (ctx, t, lval,
3511                                            non_constant_p, overflow_p,
3512                                            jump_target);
3513       break;
3514
3515     case CONSTRUCTOR:
3516       if (TREE_CONSTANT (t))
3517         /* Don't re-process a constant CONSTRUCTOR, but do fold it to
3518            VECTOR_CST if applicable.  */
3519         return fold (t);
3520       r = cxx_eval_bare_aggregate (ctx, t, lval,
3521                                    non_constant_p, overflow_p);
3522       break;
3523
3524     case VEC_INIT_EXPR:
3525       /* We can get this in a defaulted constructor for a class with a
3526          non-static data member of array type.  Either the initializer will
3527          be NULL, meaning default-initialization, or it will be an lvalue
3528          or xvalue of the same type, meaning direct-initialization from the
3529          corresponding member.  */
3530       r = cxx_eval_vec_init (ctx, t, lval,
3531                              non_constant_p, overflow_p);
3532       break;
3533
3534     case FMA_EXPR:
3535     case VEC_PERM_EXPR:
3536       r = cxx_eval_trinary_expression (ctx, t, lval,
3537                                        non_constant_p, overflow_p);
3538       break;
3539
3540     case CONVERT_EXPR:
3541     case VIEW_CONVERT_EXPR:
3542     case NOP_EXPR:
3543       {
3544         tree oldop = TREE_OPERAND (t, 0);
3545         tree op = cxx_eval_constant_expression (ctx, oldop,
3546                                                 lval,
3547                                                 non_constant_p, overflow_p);
3548         if (*non_constant_p)
3549           return t;
3550         tree type = TREE_TYPE (t);
3551         if (TREE_CODE (op) == PTRMEM_CST
3552             && !TYPE_PTRMEM_P (type))
3553           op = cplus_expand_constant (op);
3554         if (POINTER_TYPE_P (type)
3555             && TREE_CODE (op) == INTEGER_CST
3556             && !integer_zerop (op))
3557           {
3558             if (!ctx->quiet)
3559               error_at (EXPR_LOC_OR_LOC (t, input_location),
3560                         "reinterpret_cast from integer to pointer");
3561             *non_constant_p = true;
3562             return t;
3563           }
3564         if (op == oldop)
3565           /* We didn't fold at the top so we could check for ptr-int
3566              conversion.  */
3567           return fold (t);
3568         r = fold_build1 (TREE_CODE (t), type, op);
3569         /* Conversion of an out-of-range value has implementation-defined
3570            behavior; the language considers it different from arithmetic
3571            overflow, which is undefined.  */
3572         if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
3573           TREE_OVERFLOW (r) = false;
3574       }
3575       break;
3576
3577     case EMPTY_CLASS_EXPR:
3578       /* This is good enough for a function argument that might not get
3579          used, and they can't do anything with it, so just return it.  */
3580       return t;
3581
3582     case STATEMENT_LIST:
3583       new_ctx = *ctx;
3584       new_ctx.ctor = new_ctx.object = NULL_TREE;
3585       return cxx_eval_statement_list (&new_ctx, t,
3586                                       non_constant_p, overflow_p, jump_target);
3587
3588     case BIND_EXPR:
3589       return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
3590                                            lval,
3591                                            non_constant_p, overflow_p,
3592                                            jump_target);
3593
3594     case PREINCREMENT_EXPR:
3595     case POSTINCREMENT_EXPR:
3596     case PREDECREMENT_EXPR:
3597     case POSTDECREMENT_EXPR:
3598       return cxx_eval_increment_expression (ctx, t,
3599                                             lval, non_constant_p, overflow_p);
3600
3601     case LAMBDA_EXPR:
3602     case NEW_EXPR:
3603     case VEC_NEW_EXPR:
3604     case DELETE_EXPR:
3605     case VEC_DELETE_EXPR:
3606     case THROW_EXPR:
3607     case MODOP_EXPR:
3608       /* GCC internal stuff.  */
3609     case VA_ARG_EXPR:
3610     case OBJ_TYPE_REF:
3611     case WITH_CLEANUP_EXPR:
3612     case NON_DEPENDENT_EXPR:
3613     case BASELINK:
3614     case OFFSET_REF:
3615       if (!ctx->quiet)
3616         error_at (EXPR_LOC_OR_LOC (t, input_location),
3617                   "expression %qE is not a constant-expression", t);
3618       *non_constant_p = true;
3619       break;
3620
3621     case PLACEHOLDER_EXPR:
3622       if (!ctx || !ctx->ctor || (lval && !ctx->object)
3623           || !(same_type_ignoring_top_level_qualifiers_p
3624                (TREE_TYPE (t), TREE_TYPE (ctx->ctor))))
3625         {
3626           /* A placeholder without a referent.  We can get here when
3627              checking whether NSDMIs are noexcept, or in massage_init_elt;
3628              just say it's non-constant for now.  */
3629           gcc_assert (ctx->quiet);
3630           *non_constant_p = true;
3631           break;
3632         }
3633       else
3634         {
3635           /* Use of the value or address of the current object.  We could
3636              use ctx->object unconditionally, but using ctx->ctor when we
3637              can is a minor optimization.  */
3638           tree ctor = lval ? ctx->object : ctx->ctor;
3639           return cxx_eval_constant_expression
3640             (ctx, ctor, lval,
3641              non_constant_p, overflow_p);
3642         }
3643       break;
3644
3645     case GOTO_EXPR:
3646       *jump_target = TREE_OPERAND (t, 0);
3647       gcc_assert (breaks (jump_target) || continues (jump_target));
3648       break;
3649
3650     case LOOP_EXPR:
3651       cxx_eval_loop_expr (ctx, t,
3652                           non_constant_p, overflow_p, jump_target);
3653       break;
3654
3655     case SWITCH_EXPR:
3656       cxx_eval_switch_expr (ctx, t,
3657                             non_constant_p, overflow_p, jump_target);
3658       break;
3659
3660     case REQUIRES_EXPR:
3661       /* It's possible to get a requires-expression in a constant
3662          expression. For example:
3663
3664              template<typename T> concept bool C() {
3665                return requires (T t) { t; };
3666              }
3667
3668              template<typename T> requires !C<T>() void f(T);
3669
3670          Normalization leaves f with the associated constraint
3671          '!requires (T t) { ... }' which is not transformed into
3672          a constraint.  */
3673       if (!processing_template_decl)
3674         return evaluate_constraint_expression (t, NULL_TREE);
3675       else
3676         *non_constant_p = true;
3677       return t;
3678
3679     default:
3680       if (STATEMENT_CODE_P (TREE_CODE (t)))
3681         {
3682           /* This function doesn't know how to deal with pre-genericize
3683              statements; this can only happen with statement-expressions,
3684              so for now just fail.  */
3685           if (!ctx->quiet)
3686             error_at (EXPR_LOCATION (t),
3687                       "statement is not a constant-expression");
3688         }
3689       else
3690         internal_error ("unexpected expression %qE of kind %s", t,
3691                         get_tree_code_name (TREE_CODE (t)));
3692       *non_constant_p = true;
3693       break;
3694     }
3695
3696   if (r == error_mark_node)
3697     *non_constant_p = true;
3698
3699   if (*non_constant_p)
3700     return t;
3701   else
3702     return r;
3703 }
3704
3705 static tree
3706 cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
3707                                   bool strict = true, tree object = NULL_TREE)
3708 {
3709   bool non_constant_p = false;
3710   bool overflow_p = false;
3711   hash_map<tree,tree> map;
3712   constexpr_ctx ctx = { NULL, &map, NULL, NULL, allow_non_constant, strict };
3713   tree type = initialized_type (t);
3714   tree r = t;
3715   if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
3716     {
3717       /* In C++14 an NSDMI can participate in aggregate initialization,
3718          and can refer to the address of the object being initialized, so
3719          we need to pass in the relevant VAR_DECL if we want to do the
3720          evaluation in a single pass.  The evaluation will dynamically
3721          update ctx.values for the VAR_DECL.  We use the same strategy
3722          for C++11 constexpr constructors that refer to the object being
3723          initialized.  */
3724       ctx.ctor = build_constructor (type, NULL);
3725       CONSTRUCTOR_NO_IMPLICIT_ZERO (ctx.ctor) = true;
3726       if (!object)
3727         {
3728           if (TREE_CODE (t) == TARGET_EXPR)
3729             object = TARGET_EXPR_SLOT (t);
3730           else if (TREE_CODE (t) == AGGR_INIT_EXPR)
3731             object = AGGR_INIT_EXPR_SLOT (t);
3732         }
3733       ctx.object = object;
3734       if (object)
3735         gcc_assert (same_type_ignoring_top_level_qualifiers_p
3736                     (type, TREE_TYPE (object)));
3737       if (object && DECL_P (object))
3738         map.put (object, ctx.ctor);
3739       if (TREE_CODE (r) == TARGET_EXPR)
3740         /* Avoid creating another CONSTRUCTOR when we expand the
3741            TARGET_EXPR.  */
3742         r = TARGET_EXPR_INITIAL (r);
3743     }
3744
3745   r = cxx_eval_constant_expression (&ctx, r,
3746                                     false, &non_constant_p, &overflow_p);
3747
3748   verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
3749
3750   /* Mutable logic is a bit tricky: we want to allow initialization of
3751      constexpr variables with mutable members, but we can't copy those
3752      members to another constexpr variable.  */
3753   if (TREE_CODE (r) == CONSTRUCTOR
3754       && CONSTRUCTOR_MUTABLE_POISON (r))
3755     {
3756       if (!allow_non_constant)
3757         error ("%qE is not a constant expression because it refers to "
3758                "mutable subobjects of %qT", t, type);
3759       non_constant_p = true;
3760     }
3761
3762   /* Technically we should check this for all subexpressions, but that
3763      runs into problems with our internal representation of pointer
3764      subtraction and the 5.19 rules are still in flux.  */
3765   if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
3766       && ARITHMETIC_TYPE_P (TREE_TYPE (r))
3767       && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
3768     {
3769       if (!allow_non_constant)
3770         error ("conversion from pointer type %qT "
3771                "to arithmetic type %qT in a constant-expression",
3772                TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
3773       non_constant_p = true;
3774     }
3775
3776   if (!non_constant_p && overflow_p)
3777     non_constant_p = true;
3778
3779   if (non_constant_p && !allow_non_constant)
3780     return error_mark_node;
3781   else if (non_constant_p && TREE_CONSTANT (r))
3782     {
3783       /* This isn't actually constant, so unset TREE_CONSTANT.  */
3784       if (EXPR_P (r))
3785         r = copy_node (r);
3786       else if (TREE_CODE (r) == CONSTRUCTOR)
3787         r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
3788       else
3789         r = build_nop (TREE_TYPE (r), r);
3790       TREE_CONSTANT (r) = false;
3791     }
3792   else if (non_constant_p || r == t)
3793     return t;
3794
3795   if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
3796     {
3797       if (TREE_CODE (t) == TARGET_EXPR
3798           && TARGET_EXPR_INITIAL (t) == r)
3799         return t;
3800       else
3801         {
3802           r = get_target_expr (r);
3803           TREE_CONSTANT (r) = true;
3804           return r;
3805         }
3806     }
3807   else
3808     return r;
3809 }
3810
3811 /* Returns true if T is a valid subexpression of a constant expression,
3812    even if it isn't itself a constant expression.  */
3813
3814 bool
3815 is_sub_constant_expr (tree t)
3816 {
3817   bool non_constant_p = false;
3818   bool overflow_p = false;
3819   hash_map <tree, tree> map;
3820   constexpr_ctx ctx = { NULL, &map, NULL, NULL, true, true };
3821   cxx_eval_constant_expression (&ctx, t, false, &non_constant_p,
3822                                 &overflow_p);
3823   return !non_constant_p && !overflow_p;
3824 }
3825
3826 /* If T represents a constant expression returns its reduced value.
3827    Otherwise return error_mark_node.  If T is dependent, then
3828    return NULL.  */
3829
3830 tree
3831 cxx_constant_value (tree t, tree decl)
3832 {
3833   return cxx_eval_outermost_constant_expr (t, false, true, decl);
3834 }
3835
3836 /* If T is a constant expression, returns its reduced value.
3837    Otherwise, if T does not have TREE_CONSTANT set, returns T.
3838    Otherwise, returns a version of T without TREE_CONSTANT.  */
3839
3840 tree
3841 maybe_constant_value (tree t, tree decl)
3842 {
3843   tree r;
3844
3845   if (instantiation_dependent_expression_p (t)
3846       || type_unknown_p (t)
3847       || BRACE_ENCLOSED_INITIALIZER_P (t)
3848       || !potential_constant_expression (t))
3849     {
3850       if (TREE_OVERFLOW_P (t))
3851         {
3852           t = build_nop (TREE_TYPE (t), t);
3853           TREE_CONSTANT (t) = false;
3854         }
3855       return t;
3856     }
3857
3858   r = cxx_eval_outermost_constant_expr (t, true, true, decl);
3859 #ifdef ENABLE_CHECKING
3860   gcc_assert (r == t
3861               || CONVERT_EXPR_P (t)
3862               || TREE_CODE (t) == VIEW_CONVERT_EXPR
3863               || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
3864               || !cp_tree_equal (r, t));
3865 #endif
3866   return r;
3867 }
3868
3869 /* Like maybe_constant_value but first fully instantiate the argument.
3870
3871    Note: this is equivalent to instantiate_non_dependent_expr_sfinae
3872    (t, tf_none) followed by maybe_constant_value but is more efficient,
3873    because calls instantiation_dependent_expression_p and
3874    potential_constant_expression at most once.  */
3875
3876 tree
3877 fold_non_dependent_expr (tree t)
3878 {
3879   if (t == NULL_TREE)
3880     return NULL_TREE;
3881
3882   /* If we're in a template, but T isn't value dependent, simplify
3883      it.  We're supposed to treat:
3884
3885        template <typename T> void f(T[1 + 1]);
3886        template <typename T> void f(T[2]);
3887
3888      as two declarations of the same function, for example.  */
3889   if (processing_template_decl)
3890     {
3891       if (!instantiation_dependent_expression_p (t)
3892           && potential_constant_expression (t))
3893         {
3894           processing_template_decl_sentinel s;
3895           t = instantiate_non_dependent_expr_internal (t, tf_none);
3896
3897           if (type_unknown_p (t)
3898               || BRACE_ENCLOSED_INITIALIZER_P (t))
3899             {
3900               if (TREE_OVERFLOW_P (t))
3901                 {
3902                   t = build_nop (TREE_TYPE (t), t);
3903                   TREE_CONSTANT (t) = false;
3904                 }
3905               return t;
3906             }
3907
3908           tree r = cxx_eval_outermost_constant_expr (t, true, true, NULL_TREE);
3909 #ifdef ENABLE_CHECKING
3910           /* cp_tree_equal looks through NOPs, so allow them.  */
3911           gcc_assert (r == t
3912                       || CONVERT_EXPR_P (t)
3913                       || TREE_CODE (t) == VIEW_CONVERT_EXPR
3914                       || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
3915                       || !cp_tree_equal (r, t));
3916 #endif
3917           return r;
3918         }
3919       else if (TREE_OVERFLOW_P (t))
3920         {
3921           t = build_nop (TREE_TYPE (t), t);
3922           TREE_CONSTANT (t) = false;
3923         }
3924       return t;
3925     }
3926
3927   return maybe_constant_value (t);
3928 }
3929
3930 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
3931    than wrapped in a TARGET_EXPR.  */
3932
3933 tree
3934 maybe_constant_init (tree t, tree decl)
3935 {
3936   if (TREE_CODE (t) == EXPR_STMT)
3937     t = TREE_OPERAND (t, 0);
3938   if (TREE_CODE (t) == CONVERT_EXPR
3939       && VOID_TYPE_P (TREE_TYPE (t)))
3940     t = TREE_OPERAND (t, 0);
3941   if (TREE_CODE (t) == INIT_EXPR)
3942     t = TREE_OPERAND (t, 1);
3943   if (instantiation_dependent_expression_p (t)
3944       || type_unknown_p (t)
3945       || BRACE_ENCLOSED_INITIALIZER_P (t)
3946       || !potential_static_init_expression (t))
3947     /* Don't try to evaluate it.  */;
3948   else
3949     t = cxx_eval_outermost_constant_expr (t, true, false, decl);
3950   if (TREE_CODE (t) == TARGET_EXPR)
3951     {
3952       tree init = TARGET_EXPR_INITIAL (t);
3953       if (TREE_CODE (init) == CONSTRUCTOR)
3954         t = init;
3955     }
3956   return t;
3957 }
3958
3959 #if 0
3960 /* FIXME see ADDR_EXPR section in potential_constant_expression_1.  */
3961 /* Return true if the object referred to by REF has automatic or thread
3962    local storage.  */
3963
3964 enum { ck_ok, ck_bad, ck_unknown };
3965 static int
3966 check_automatic_or_tls (tree ref)
3967 {
3968   machine_mode mode;
3969   HOST_WIDE_INT bitsize, bitpos;
3970   tree offset;
3971   int volatilep = 0, unsignedp = 0;
3972   tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
3973                                    &mode, &unsignedp, &volatilep, false);
3974   duration_kind dk;
3975
3976   /* If there isn't a decl in the middle, we don't know the linkage here,
3977      and this isn't a constant expression anyway.  */
3978   if (!DECL_P (decl))
3979     return ck_unknown;
3980   dk = decl_storage_duration (decl);
3981   return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
3982 }
3983 #endif
3984
3985 /* Return true if T denotes a potentially constant expression.  Issue
3986    diagnostic as appropriate under control of FLAGS.  If WANT_RVAL is true,
3987    an lvalue-rvalue conversion is implied.
3988
3989    C++0x [expr.const] used to say
3990
3991    6 An expression is a potential constant expression if it is
3992      a constant expression where all occurrences of function
3993      parameters are replaced by arbitrary constant expressions
3994      of the appropriate type.
3995
3996    2  A conditional expression is a constant expression unless it
3997       involves one of the following as a potentially evaluated
3998       subexpression (3.2), but subexpressions of logical AND (5.14),
3999       logical OR (5.15), and conditional (5.16) operations that are
4000       not evaluated are not considered.   */
4001
4002 static bool
4003 potential_constant_expression_1 (tree t, bool want_rval, bool strict,
4004                                  tsubst_flags_t flags)
4005 {
4006 #define RECUR(T,RV) potential_constant_expression_1 ((T), (RV), strict, flags)
4007   enum { any = false, rval = true };
4008   int i;
4009   tree tmp;
4010
4011   if (t == error_mark_node)
4012     return false;
4013   if (t == NULL_TREE)
4014     return true;
4015   if (TREE_THIS_VOLATILE (t))
4016     {
4017       if (flags & tf_error)
4018         error ("expression %qE has side-effects", t);
4019       return false;
4020     }
4021   if (CONSTANT_CLASS_P (t))
4022     return true;
4023
4024   switch (TREE_CODE (t))
4025     {
4026     case FUNCTION_DECL:
4027     case BASELINK:
4028     case TEMPLATE_DECL:
4029     case OVERLOAD:
4030     case TEMPLATE_ID_EXPR:
4031     case LABEL_DECL:
4032     case LABEL_EXPR:
4033     case CASE_LABEL_EXPR:
4034     case CONST_DECL:
4035     case SIZEOF_EXPR:
4036     case ALIGNOF_EXPR:
4037     case OFFSETOF_EXPR:
4038     case NOEXCEPT_EXPR:
4039     case TEMPLATE_PARM_INDEX:
4040     case TRAIT_EXPR:
4041     case IDENTIFIER_NODE:
4042     case USERDEF_LITERAL:
4043       /* We can see a FIELD_DECL in a pointer-to-member expression.  */
4044     case FIELD_DECL:
4045     case PARM_DECL:
4046     case USING_DECL:
4047     case USING_STMT:
4048     case PLACEHOLDER_EXPR:
4049     case BREAK_STMT:
4050     case CONTINUE_STMT:
4051     case REQUIRES_EXPR:
4052       return true;
4053
4054     case AGGR_INIT_EXPR:
4055     case CALL_EXPR:
4056       /* -- an invocation of a function other than a constexpr function
4057             or a constexpr constructor.  */
4058       {
4059         tree fun = get_function_named_in_call (t);
4060         const int nargs = call_expr_nargs (t);
4061         i = 0;
4062
4063         if (fun == NULL_TREE)
4064           {
4065             if (TREE_CODE (t) == CALL_EXPR
4066                 && CALL_EXPR_FN (t) == NULL_TREE)
4067               switch (CALL_EXPR_IFN (t))
4068                 {
4069                 /* These should be ignored, they are optimized away from
4070                    constexpr functions.  */
4071                 case IFN_UBSAN_NULL:
4072                 case IFN_UBSAN_BOUNDS:
4073                 case IFN_UBSAN_VPTR:
4074                   return true;
4075                 default:
4076                   break;
4077                 }
4078             /* fold_call_expr can't do anything with IFN calls.  */
4079             if (flags & tf_error)
4080               error_at (EXPR_LOC_OR_LOC (t, input_location),
4081                         "call to internal function");
4082             return false;
4083           }
4084         if (is_overloaded_fn (fun))
4085           {
4086             if (TREE_CODE (fun) == FUNCTION_DECL)
4087               {
4088                 if (builtin_valid_in_constant_expr_p (fun))
4089                   return true;
4090                 if (!DECL_DECLARED_CONSTEXPR_P (fun)
4091                     /* Allow any built-in function; if the expansion
4092                        isn't constant, we'll deal with that then.  */
4093                     && !is_builtin_fn (fun))
4094                   {
4095                     if (flags & tf_error)
4096                       {
4097                         error_at (EXPR_LOC_OR_LOC (t, input_location),
4098                                   "call to non-constexpr function %qD", fun);
4099                         explain_invalid_constexpr_fn (fun);
4100                       }
4101                     return false;
4102                   }
4103                 /* A call to a non-static member function takes the address
4104                    of the object as the first argument.  But in a constant
4105                    expression the address will be folded away, so look
4106                    through it now.  */
4107                 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
4108                     && !DECL_CONSTRUCTOR_P (fun))
4109                   {
4110                     tree x = get_nth_callarg (t, 0);
4111                     if (is_this_parameter (x))
4112                       return true;
4113                     else if (!RECUR (x, rval))
4114                       return false;
4115                     i = 1;
4116                   }
4117               }
4118             else
4119               {
4120                 if (!RECUR (fun, true))
4121                   return false;
4122                 fun = get_first_fn (fun);
4123               }
4124             /* Skip initial arguments to base constructors.  */
4125             if (DECL_BASE_CONSTRUCTOR_P (fun))
4126               i = num_artificial_parms_for (fun);
4127             fun = DECL_ORIGIN (fun);
4128           }
4129         else
4130           {
4131             if (RECUR (fun, rval))
4132               /* Might end up being a constant function pointer.  */;
4133             else
4134               return false;
4135           }
4136         for (; i < nargs; ++i)
4137           {
4138             tree x = get_nth_callarg (t, i);
4139             /* In a template, reference arguments haven't been converted to
4140                REFERENCE_TYPE and we might not even know if the parameter
4141                is a reference, so accept lvalue constants too.  */
4142             bool rv = processing_template_decl ? any : rval;
4143             if (!RECUR (x, rv))
4144               return false;
4145           }
4146         return true;
4147       }
4148
4149     case NON_LVALUE_EXPR:
4150       /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
4151             -- an lvalue of integral type that refers to a non-volatile
4152                const variable or static data member initialized with
4153                constant expressions, or
4154
4155             -- an lvalue of literal type that refers to non-volatile
4156                object defined with constexpr, or that refers to a
4157                sub-object of such an object;  */
4158       return RECUR (TREE_OPERAND (t, 0), rval);
4159
4160     case VAR_DECL:
4161       if (want_rval
4162           && !decl_constant_var_p (t)
4163           && (strict
4164               || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
4165               || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t))
4166           && !var_in_constexpr_fn (t)
4167           && !type_dependent_expression_p (t))
4168         {
4169           if (flags & tf_error)
4170             non_const_var_error (t);
4171           return false;
4172         }
4173       return true;
4174
4175     case NOP_EXPR:
4176     case CONVERT_EXPR:
4177     case VIEW_CONVERT_EXPR:
4178       /* -- a reinterpret_cast.  FIXME not implemented, and this rule
4179          may change to something more specific to type-punning (DR 1312).  */
4180       {
4181         tree from = TREE_OPERAND (t, 0);
4182         if (POINTER_TYPE_P (TREE_TYPE (t))
4183             && TREE_CODE (from) == INTEGER_CST
4184             && !integer_zerop (from))
4185           {
4186             if (flags & tf_error)
4187               error_at (EXPR_LOC_OR_LOC (t, input_location),
4188                         "reinterpret_cast from integer to pointer");
4189             return false;
4190           }
4191         return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
4192       }
4193
4194     case ADDR_EXPR:
4195       /* -- a unary operator & that is applied to an lvalue that
4196             designates an object with thread or automatic storage
4197             duration;  */
4198       t = TREE_OPERAND (t, 0);
4199
4200       if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
4201         /* A pointer-to-member constant.  */
4202         return true;
4203
4204 #if 0
4205       /* FIXME adjust when issue 1197 is fully resolved.  For now don't do
4206          any checking here, as we might dereference the pointer later.  If
4207          we remove this code, also remove check_automatic_or_tls.  */
4208       i = check_automatic_or_tls (t);
4209       if (i == ck_ok)
4210         return true;
4211       if (i == ck_bad)
4212         {
4213           if (flags & tf_error)
4214             error ("address-of an object %qE with thread local or "
4215                    "automatic storage is not a constant expression", t);
4216           return false;
4217         }
4218 #endif
4219       return RECUR (t, any);
4220
4221     case COMPONENT_REF:
4222     case BIT_FIELD_REF:
4223     case ARROW_EXPR:
4224     case OFFSET_REF:
4225       /* -- a class member access unless its postfix-expression is
4226             of literal type or of pointer to literal type.  */
4227       /* This test would be redundant, as it follows from the
4228          postfix-expression being a potential constant expression.  */
4229       return RECUR (TREE_OPERAND (t, 0), want_rval);
4230
4231     case EXPR_PACK_EXPANSION:
4232       return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
4233
4234     case INDIRECT_REF:
4235       {
4236         tree x = TREE_OPERAND (t, 0);
4237         STRIP_NOPS (x);
4238         if (is_this_parameter (x))
4239           {
4240             if (DECL_CONTEXT (x)
4241                 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x)))
4242               {
4243                 if (flags & tf_error)
4244                   error ("use of %<this%> in a constant expression");
4245                 return false;
4246               }
4247             return true;
4248           }
4249         return RECUR (x, rval);
4250       }
4251
4252     case STATEMENT_LIST:
4253       {
4254         tree_stmt_iterator i;
4255         for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
4256           {
4257             if (!RECUR (tsi_stmt (i), any))
4258               return false;
4259           }
4260         return true;
4261       }
4262       break;
4263
4264     case MODIFY_EXPR:
4265       if (cxx_dialect < cxx14)
4266         goto fail;
4267       if (!RECUR (TREE_OPERAND (t, 0), any))
4268         return false;
4269       if (!RECUR (TREE_OPERAND (t, 1), rval))
4270         return false;
4271       return true;
4272
4273     case MODOP_EXPR:
4274       if (cxx_dialect < cxx14)
4275         goto fail;
4276       if (!RECUR (TREE_OPERAND (t, 0), rval))
4277         return false;
4278       if (!RECUR (TREE_OPERAND (t, 2), rval))
4279         return false;
4280       return true;
4281
4282     case DO_STMT:
4283       if (!RECUR (DO_COND (t), rval))
4284         return false;
4285       if (!RECUR (DO_BODY (t), any))
4286         return false;
4287       return true;
4288
4289     case FOR_STMT:
4290       if (!RECUR (FOR_INIT_STMT (t), any))
4291         return false;
4292       if (!RECUR (FOR_COND (t), rval))
4293         return false;
4294       if (!RECUR (FOR_EXPR (t), any))
4295         return false;
4296       if (!RECUR (FOR_BODY (t), any))
4297         return false;
4298       return true;
4299
4300     case WHILE_STMT:
4301       if (!RECUR (WHILE_COND (t), rval))
4302         return false;
4303       if (!RECUR (WHILE_BODY (t), any))
4304         return false;
4305       return true;
4306
4307     case SWITCH_STMT:
4308       if (!RECUR (SWITCH_STMT_COND (t), rval))
4309         return false;
4310       /* FIXME we don't check SWITCH_STMT_BODY currently, because even
4311          unreachable labels would be checked.  */
4312       return true;
4313
4314     case STMT_EXPR:
4315       return RECUR (STMT_EXPR_STMT (t), rval);
4316
4317     case LAMBDA_EXPR:
4318     case DYNAMIC_CAST_EXPR:
4319     case PSEUDO_DTOR_EXPR:
4320     case NEW_EXPR:
4321     case VEC_NEW_EXPR:
4322     case DELETE_EXPR:
4323     case VEC_DELETE_EXPR:
4324     case THROW_EXPR:
4325     case OMP_ATOMIC:
4326     case OMP_ATOMIC_READ:
4327     case OMP_ATOMIC_CAPTURE_OLD:
4328     case OMP_ATOMIC_CAPTURE_NEW:
4329       /* GCC internal stuff.  */
4330     case VA_ARG_EXPR:
4331     case OBJ_TYPE_REF:
4332     case TRANSACTION_EXPR:
4333     case ASM_EXPR:
4334     case AT_ENCODE_EXPR:
4335     fail:
4336       if (flags & tf_error)
4337         error ("expression %qE is not a constant-expression", t);
4338       return false;
4339
4340     case TYPEID_EXPR:
4341       /* -- a typeid expression whose operand is of polymorphic
4342             class type;  */
4343       {
4344         tree e = TREE_OPERAND (t, 0);
4345         if (!TYPE_P (e) && !type_dependent_expression_p (e)
4346             && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
4347           {
4348             if (flags & tf_error)
4349               error ("typeid-expression is not a constant expression "
4350                      "because %qE is of polymorphic type", e);
4351             return false;
4352           }
4353         return true;
4354       }
4355
4356     case MINUS_EXPR:
4357       want_rval = true;
4358       goto binary;
4359
4360     case LT_EXPR:
4361     case LE_EXPR:
4362     case GT_EXPR:
4363     case GE_EXPR:
4364     case EQ_EXPR:
4365     case NE_EXPR:
4366       want_rval = true;
4367       goto binary;
4368
4369     case PREINCREMENT_EXPR:
4370     case POSTINCREMENT_EXPR:
4371     case PREDECREMENT_EXPR:
4372     case POSTDECREMENT_EXPR:
4373       if (cxx_dialect < cxx14)
4374         goto fail;
4375       goto unary;
4376
4377     case BIT_NOT_EXPR:
4378       /* A destructor.  */
4379       if (TYPE_P (TREE_OPERAND (t, 0)))
4380         return true;
4381       /* else fall through.  */
4382
4383     case REALPART_EXPR:
4384     case IMAGPART_EXPR:
4385     case CONJ_EXPR:
4386     case SAVE_EXPR:
4387     case FIX_TRUNC_EXPR:
4388     case FLOAT_EXPR:
4389     case NEGATE_EXPR:
4390     case ABS_EXPR:
4391     case TRUTH_NOT_EXPR:
4392     case FIXED_CONVERT_EXPR:
4393     case UNARY_PLUS_EXPR:
4394     case UNARY_LEFT_FOLD_EXPR:
4395     case UNARY_RIGHT_FOLD_EXPR:
4396     unary:
4397       return RECUR (TREE_OPERAND (t, 0), rval);
4398
4399     case CAST_EXPR:
4400     case CONST_CAST_EXPR:
4401     case STATIC_CAST_EXPR:
4402     case REINTERPRET_CAST_EXPR:
4403     case IMPLICIT_CONV_EXPR:
4404       if (cxx_dialect < cxx11
4405           && !dependent_type_p (TREE_TYPE (t))
4406           && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
4407         /* In C++98, a conversion to non-integral type can't be part of a
4408            constant expression.  */
4409         {
4410           if (flags & tf_error)
4411             error ("cast to non-integral type %qT in a constant expression",
4412                    TREE_TYPE (t));
4413           return false;
4414         }
4415
4416       return (RECUR (TREE_OPERAND (t, 0),
4417                      TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE));
4418
4419     case BIND_EXPR:
4420       return RECUR (BIND_EXPR_BODY (t), want_rval);
4421
4422     case WITH_CLEANUP_EXPR:
4423     case CLEANUP_POINT_EXPR:
4424     case MUST_NOT_THROW_EXPR:
4425     case TRY_CATCH_EXPR:
4426     case TRY_BLOCK:
4427     case EH_SPEC_BLOCK:
4428     case EXPR_STMT:
4429     case PAREN_EXPR:
4430     case DECL_EXPR:
4431     case NON_DEPENDENT_EXPR:
4432       /* For convenience.  */
4433     case RETURN_EXPR:
4434       return RECUR (TREE_OPERAND (t, 0), want_rval);
4435
4436     case TRY_FINALLY_EXPR:
4437       return (RECUR (TREE_OPERAND (t, 0), want_rval)
4438               && RECUR (TREE_OPERAND (t, 1), any));
4439
4440     case SCOPE_REF:
4441       return RECUR (TREE_OPERAND (t, 1), want_rval);
4442
4443     case TARGET_EXPR:
4444       if (!literal_type_p (TREE_TYPE (t)))
4445         {
4446           if (flags & tf_error)
4447             {
4448               error ("temporary of non-literal type %qT in a "
4449                      "constant expression", TREE_TYPE (t));
4450               explain_non_literal_class (TREE_TYPE (t));
4451             }
4452           return false;
4453         }
4454     case INIT_EXPR:
4455       return RECUR (TREE_OPERAND (t, 1), rval);
4456
4457     case CONSTRUCTOR:
4458       {
4459         vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
4460         constructor_elt *ce;
4461         for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
4462           if (!RECUR (ce->value, want_rval))
4463             return false;
4464         return true;
4465       }
4466
4467     case TREE_LIST:
4468       {
4469         gcc_assert (TREE_PURPOSE (t) == NULL_TREE
4470                     || DECL_P (TREE_PURPOSE (t)));
4471         if (!RECUR (TREE_VALUE (t), want_rval))
4472           return false;
4473         if (TREE_CHAIN (t) == NULL_TREE)
4474           return true;
4475         return RECUR (TREE_CHAIN (t), want_rval);
4476       }
4477
4478     case TRUNC_DIV_EXPR:
4479     case CEIL_DIV_EXPR:
4480     case FLOOR_DIV_EXPR:
4481     case ROUND_DIV_EXPR:
4482     case TRUNC_MOD_EXPR:
4483     case CEIL_MOD_EXPR:
4484     case ROUND_MOD_EXPR:
4485       {
4486         tree denom = TREE_OPERAND (t, 1);
4487         if (!RECUR (denom, rval))
4488           return false;
4489         /* We can't call cxx_eval_outermost_constant_expr on an expression
4490            that hasn't been through instantiate_non_dependent_expr yet.  */
4491         if (!processing_template_decl)
4492           denom = cxx_eval_outermost_constant_expr (denom, true);
4493         if (integer_zerop (denom))
4494           {
4495             if (flags & tf_error)
4496               error ("division by zero is not a constant-expression");
4497             return false;
4498           }
4499         else
4500           {
4501             want_rval = true;
4502             return RECUR (TREE_OPERAND (t, 0), want_rval);
4503           }
4504       }
4505
4506     case COMPOUND_EXPR:
4507       {
4508         /* check_return_expr sometimes wraps a TARGET_EXPR in a
4509            COMPOUND_EXPR; don't get confused.  Also handle EMPTY_CLASS_EXPR
4510            introduced by build_call_a.  */
4511         tree op0 = TREE_OPERAND (t, 0);
4512         tree op1 = TREE_OPERAND (t, 1);
4513         STRIP_NOPS (op1);
4514         if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
4515             || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
4516           return RECUR (op0, want_rval);
4517         else
4518           goto binary;
4519       }
4520
4521       /* If the first operand is the non-short-circuit constant, look at
4522          the second operand; otherwise we only care about the first one for
4523          potentiality.  */
4524     case TRUTH_AND_EXPR:
4525     case TRUTH_ANDIF_EXPR:
4526       tmp = boolean_true_node;
4527       goto truth;
4528     case TRUTH_OR_EXPR:
4529     case TRUTH_ORIF_EXPR:
4530       tmp = boolean_false_node;
4531     truth:
4532       {
4533         tree op = TREE_OPERAND (t, 0);
4534         if (!RECUR (op, rval))
4535           return false;
4536         if (!processing_template_decl)
4537           op = cxx_eval_outermost_constant_expr (op, true);
4538         if (tree_int_cst_equal (op, tmp))
4539           return RECUR (TREE_OPERAND (t, 1), rval);
4540         else
4541           return true;
4542       }
4543
4544     case PLUS_EXPR:
4545     case MULT_EXPR:
4546     case POINTER_PLUS_EXPR:
4547     case RDIV_EXPR:
4548     case EXACT_DIV_EXPR:
4549     case MIN_EXPR:
4550     case MAX_EXPR:
4551     case LSHIFT_EXPR:
4552     case RSHIFT_EXPR:
4553     case LROTATE_EXPR:
4554     case RROTATE_EXPR:
4555     case BIT_IOR_EXPR:
4556     case BIT_XOR_EXPR:
4557     case BIT_AND_EXPR:
4558     case TRUTH_XOR_EXPR:
4559     case UNORDERED_EXPR:
4560     case ORDERED_EXPR:
4561     case UNLT_EXPR:
4562     case UNLE_EXPR:
4563     case UNGT_EXPR:
4564     case UNGE_EXPR:
4565     case UNEQ_EXPR:
4566     case LTGT_EXPR:
4567     case RANGE_EXPR:
4568     case COMPLEX_EXPR:
4569       want_rval = true;
4570       /* Fall through.  */
4571     case ARRAY_REF:
4572     case ARRAY_RANGE_REF:
4573     case MEMBER_REF:
4574     case DOTSTAR_EXPR:
4575     case MEM_REF:
4576     case BINARY_LEFT_FOLD_EXPR:
4577     case BINARY_RIGHT_FOLD_EXPR:
4578     binary:
4579       for (i = 0; i < 2; ++i)
4580         if (!RECUR (TREE_OPERAND (t, i), want_rval))
4581           return false;
4582       return true;
4583
4584     case CILK_SYNC_STMT:
4585     case CILK_SPAWN_STMT:
4586     case ARRAY_NOTATION_REF:
4587       return false;
4588
4589     case FMA_EXPR:
4590     case VEC_PERM_EXPR:
4591      for (i = 0; i < 3; ++i)
4592       if (!RECUR (TREE_OPERAND (t, i), true))
4593         return false;
4594      return true;
4595
4596     case IF_STMT:
4597     case COND_EXPR:
4598     case VEC_COND_EXPR:
4599       /* If the condition is a known constant, we know which of the legs we
4600          care about; otherwise we only require that the condition and
4601          either of the legs be potentially constant.  */
4602       tmp = TREE_OPERAND (t, 0);
4603       if (!RECUR (tmp, rval))
4604         return false;
4605       if (!processing_template_decl)
4606         tmp = cxx_eval_outermost_constant_expr (tmp, true);
4607       if (integer_zerop (tmp))
4608         return RECUR (TREE_OPERAND (t, 2), want_rval);
4609       else if (TREE_CODE (tmp) == INTEGER_CST)
4610         return RECUR (TREE_OPERAND (t, 1), want_rval);
4611       for (i = 1; i < 3; ++i)
4612         if (potential_constant_expression_1 (TREE_OPERAND (t, i),
4613                                              want_rval, strict, tf_none))
4614           return true;
4615       if (flags & tf_error)
4616         error ("expression %qE is not a constant-expression", t);
4617       return false;
4618
4619     case VEC_INIT_EXPR:
4620       if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
4621         return true;
4622       if (flags & tf_error)
4623         {
4624           error ("non-constant array initialization");
4625           diagnose_non_constexpr_vec_init (t);
4626         }
4627       return false;
4628
4629     case TYPE_DECL:
4630     case TAG_DEFN:
4631       /* We can see these in statement-expressions.  */
4632       return true;
4633
4634     default:
4635       if (objc_is_property_ref (t))
4636         return false;
4637
4638       sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
4639       gcc_unreachable();
4640       return false;
4641     }
4642 #undef RECUR
4643 }
4644
4645 /* The main entry point to the above.  */
4646
4647 bool
4648 potential_constant_expression (tree t)
4649 {
4650   return potential_constant_expression_1 (t, false, true, tf_none);
4651 }
4652
4653 bool
4654 potential_static_init_expression (tree t)
4655 {
4656   return potential_constant_expression_1 (t, false, false, tf_none);
4657 }
4658
4659 /* As above, but require a constant rvalue.  */
4660
4661 bool
4662 potential_rvalue_constant_expression (tree t)
4663 {
4664   return potential_constant_expression_1 (t, true, true, tf_none);
4665 }
4666
4667 /* Like above, but complain about non-constant expressions.  */
4668
4669 bool
4670 require_potential_constant_expression (tree t)
4671 {
4672   return potential_constant_expression_1 (t, false, true, tf_warning_or_error);
4673 }
4674
4675 /* Cross product of the above.  */
4676
4677 bool
4678 require_potential_rvalue_constant_expression (tree t)
4679 {
4680   return potential_constant_expression_1 (t, true, true, tf_warning_or_error);
4681 }
4682
4683 #include "gt-cp-constexpr.h"