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