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