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