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