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