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