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