]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/constexpr.c
/cp
[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);
da1fbe63 1347 if (result == NULL_TREE && !*non_constant_p)
9c96033c 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);
3e520b97 2016 unsigned HOST_WIDE_INT max = tree_to_uhwi (array_type_nelts_top (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;
3e520b97 2021 unsigned HOST_WIDE_INT i;
09b42213 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
3e520b97 2046 for (i = 0; i < max; ++i)
09b42213 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
e0e672a0 2977 case SAVE_EXPR:
2978 /* Avoid evaluating a SAVE_EXPR more than once. */
2979 if (tree *p = ctx->values->get (t))
2980 r = *p;
2981 else
2982 {
2983 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), addr,
2984 non_constant_p, overflow_p);
2985 ctx->values->put (t, r);
2986 }
2987 break;
2988
09b42213 2989 case NON_LVALUE_EXPR:
2990 case TRY_CATCH_EXPR:
2991 case CLEANUP_POINT_EXPR:
2992 case MUST_NOT_THROW_EXPR:
9c96033c 2993 case EXPR_STMT:
2994 case EH_SPEC_BLOCK:
cf72f34d 2995 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
f83e6885 2996 addr,
00f21715 2997 non_constant_p, overflow_p,
2998 jump_target);
09b42213 2999 break;
3000
3001 /* These differ from cxx_eval_unary_expression in that this doesn't
3002 check for a constant operand or result; an address can be
3003 constant without its operand being, and vice versa. */
3004 case INDIRECT_REF:
f83e6885 3005 r = cxx_eval_indirect_ref (ctx, t, addr,
09b42213 3006 non_constant_p, overflow_p);
3007 break;
3008
3009 case ADDR_EXPR:
3010 {
3011 tree oldop = TREE_OPERAND (t, 0);
cf72f34d 3012 tree op = cxx_eval_constant_expression (ctx, oldop,
09b42213 3013 /*addr*/true,
b2a43197 3014 non_constant_p, overflow_p);
09b42213 3015 /* Don't VERIFY_CONSTANT here. */
3016 if (*non_constant_p)
3017 return t;
1eb418cc 3018 gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
09b42213 3019 /* This function does more aggressive folding than fold itself. */
3020 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
3021 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
3022 return t;
3023 break;
3024 }
3025
3026 case REALPART_EXPR:
3027 case IMAGPART_EXPR:
3028 case CONJ_EXPR:
3029 case FIX_TRUNC_EXPR:
3030 case FLOAT_EXPR:
3031 case NEGATE_EXPR:
3032 case ABS_EXPR:
3033 case BIT_NOT_EXPR:
3034 case TRUTH_NOT_EXPR:
3035 case FIXED_CONVERT_EXPR:
f83e6885 3036 r = cxx_eval_unary_expression (ctx, t, addr,
09b42213 3037 non_constant_p, overflow_p);
3038 break;
3039
3040 case SIZEOF_EXPR:
3041 if (SIZEOF_EXPR_TYPE_P (t))
3042 r = cxx_sizeof_or_alignof_type (TREE_TYPE (TREE_OPERAND (t, 0)),
3043 SIZEOF_EXPR, false);
3044 else if (TYPE_P (TREE_OPERAND (t, 0)))
3045 r = cxx_sizeof_or_alignof_type (TREE_OPERAND (t, 0), SIZEOF_EXPR,
3046 false);
3047 else
3048 r = cxx_sizeof_or_alignof_expr (TREE_OPERAND (t, 0), SIZEOF_EXPR,
3049 false);
3050 if (r == error_mark_node)
3051 r = size_one_node;
3052 VERIFY_CONSTANT (r);
3053 break;
3054
3055 case COMPOUND_EXPR:
3056 {
3057 /* check_return_expr sometimes wraps a TARGET_EXPR in a
3058 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
3059 introduced by build_call_a. */
3060 tree op0 = TREE_OPERAND (t, 0);
3061 tree op1 = TREE_OPERAND (t, 1);
3062 STRIP_NOPS (op1);
3063 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
3064 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
f83e6885 3065 r = cxx_eval_constant_expression (ctx, op0,
00f21715 3066 addr, non_constant_p, overflow_p,
3067 jump_target);
09b42213 3068 else
3069 {
3070 /* Check that the LHS is constant and then discard it. */
f83e6885 3071 cxx_eval_constant_expression (ctx, op0,
00f21715 3072 false, non_constant_p, overflow_p,
3073 jump_target);
09b42213 3074 op1 = TREE_OPERAND (t, 1);
f83e6885 3075 r = cxx_eval_constant_expression (ctx, op1,
00f21715 3076 addr, non_constant_p, overflow_p,
3077 jump_target);
09b42213 3078 }
3079 }
3080 break;
3081
3082 case POINTER_PLUS_EXPR:
3083 case PLUS_EXPR:
3084 case MINUS_EXPR:
3085 case MULT_EXPR:
3086 case TRUNC_DIV_EXPR:
3087 case CEIL_DIV_EXPR:
3088 case FLOOR_DIV_EXPR:
3089 case ROUND_DIV_EXPR:
3090 case TRUNC_MOD_EXPR:
3091 case CEIL_MOD_EXPR:
3092 case ROUND_MOD_EXPR:
3093 case RDIV_EXPR:
3094 case EXACT_DIV_EXPR:
3095 case MIN_EXPR:
3096 case MAX_EXPR:
3097 case LSHIFT_EXPR:
3098 case RSHIFT_EXPR:
3099 case LROTATE_EXPR:
3100 case RROTATE_EXPR:
3101 case BIT_IOR_EXPR:
3102 case BIT_XOR_EXPR:
3103 case BIT_AND_EXPR:
3104 case TRUTH_XOR_EXPR:
3105 case LT_EXPR:
3106 case LE_EXPR:
3107 case GT_EXPR:
3108 case GE_EXPR:
3109 case EQ_EXPR:
3110 case NE_EXPR:
3111 case UNORDERED_EXPR:
3112 case ORDERED_EXPR:
3113 case UNLT_EXPR:
3114 case UNLE_EXPR:
3115 case UNGT_EXPR:
3116 case UNGE_EXPR:
3117 case UNEQ_EXPR:
3118 case LTGT_EXPR:
3119 case RANGE_EXPR:
3120 case COMPLEX_EXPR:
f83e6885 3121 r = cxx_eval_binary_expression (ctx, t, addr,
09b42213 3122 non_constant_p, overflow_p);
3123 break;
3124
3125 /* fold can introduce non-IF versions of these; still treat them as
3126 short-circuiting. */
3127 case TRUTH_AND_EXPR:
3128 case TRUTH_ANDIF_EXPR:
cf72f34d 3129 r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
09b42213 3130 boolean_true_node,
f83e6885 3131 addr,
09b42213 3132 non_constant_p, overflow_p);
3133 break;
3134
3135 case TRUTH_OR_EXPR:
3136 case TRUTH_ORIF_EXPR:
cf72f34d 3137 r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
09b42213 3138 boolean_false_node,
f83e6885 3139 addr,
09b42213 3140 non_constant_p, overflow_p);
3141 break;
3142
3143 case ARRAY_REF:
f83e6885 3144 r = cxx_eval_array_reference (ctx, t, addr,
09b42213 3145 non_constant_p, overflow_p);
3146 break;
3147
3148 case COMPONENT_REF:
3149 if (is_overloaded_fn (t))
3150 {
3151 /* We can only get here in checking mode via
3152 build_non_dependent_expr, because any expression that
3153 calls or takes the address of the function will have
3154 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
f83e6885 3155 gcc_checking_assert (ctx->quiet || errorcount);
09b42213 3156 *non_constant_p = true;
3157 return t;
3158 }
f83e6885 3159 r = cxx_eval_component_reference (ctx, t, addr,
09b42213 3160 non_constant_p, overflow_p);
3161 break;
3162
3163 case BIT_FIELD_REF:
f83e6885 3164 r = cxx_eval_bit_field_ref (ctx, t, addr,
09b42213 3165 non_constant_p, overflow_p);
3166 break;
3167
3168 case COND_EXPR:
3169 case VEC_COND_EXPR:
f83e6885 3170 r = cxx_eval_conditional_expression (ctx, t, addr,
00f21715 3171 non_constant_p, overflow_p,
3172 jump_target);
09b42213 3173 break;
3174
3175 case CONSTRUCTOR:
f83e6885 3176 r = cxx_eval_bare_aggregate (ctx, t, addr,
09b42213 3177 non_constant_p, overflow_p);
3178 break;
3179
3180 case VEC_INIT_EXPR:
3181 /* We can get this in a defaulted constructor for a class with a
3182 non-static data member of array type. Either the initializer will
3183 be NULL, meaning default-initialization, or it will be an lvalue
3184 or xvalue of the same type, meaning direct-initialization from the
3185 corresponding member. */
f83e6885 3186 r = cxx_eval_vec_init (ctx, t, addr,
09b42213 3187 non_constant_p, overflow_p);
3188 break;
3189
3190 case FMA_EXPR:
3191 case VEC_PERM_EXPR:
f83e6885 3192 r = cxx_eval_trinary_expression (ctx, t, addr,
09b42213 3193 non_constant_p, overflow_p);
3194 break;
3195
3196 case CONVERT_EXPR:
3197 case VIEW_CONVERT_EXPR:
3198 case NOP_EXPR:
3199 {
3200 tree oldop = TREE_OPERAND (t, 0);
cf72f34d 3201 tree op = cxx_eval_constant_expression (ctx, oldop,
f83e6885 3202 addr,
b2a43197 3203 non_constant_p, overflow_p);
09b42213 3204 if (*non_constant_p)
3205 return t;
3206 if (POINTER_TYPE_P (TREE_TYPE (t))
3207 && TREE_CODE (op) == INTEGER_CST
3208 && !integer_zerop (op))
3209 {
f83e6885 3210 if (!ctx->quiet)
09b42213 3211 error_at (EXPR_LOC_OR_LOC (t, input_location),
3212 "reinterpret_cast from integer to pointer");
3213 *non_constant_p = true;
3214 return t;
3215 }
3216 if (op == oldop)
3217 /* We didn't fold at the top so we could check for ptr-int
3218 conversion. */
3219 return fold (t);
3220 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), op);
3221 /* Conversion of an out-of-range value has implementation-defined
3222 behavior; the language considers it different from arithmetic
3223 overflow, which is undefined. */
3224 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
3225 TREE_OVERFLOW (r) = false;
3226 }
3227 break;
3228
3229 case EMPTY_CLASS_EXPR:
3230 /* This is good enough for a function argument that might not get
3231 used, and they can't do anything with it, so just return it. */
3232 return t;
3233
9c96033c 3234 case STATEMENT_LIST:
00f21715 3235 new_ctx = *ctx;
3236 new_ctx.ctor = new_ctx.object = NULL_TREE;
f83e6885 3237 return cxx_eval_statement_list (&new_ctx, t,
00f21715 3238 non_constant_p, overflow_p, jump_target);
9c96033c 3239
3240 case BIND_EXPR:
3241 return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
f83e6885 3242 addr,
00f21715 3243 non_constant_p, overflow_p,
3244 jump_target);
9c96033c 3245
09b42213 3246 case PREINCREMENT_EXPR:
3247 case POSTINCREMENT_EXPR:
3248 case PREDECREMENT_EXPR:
3249 case POSTDECREMENT_EXPR:
f83e6885 3250 return cxx_eval_increment_expression (ctx, t,
9c96033c 3251 addr, non_constant_p, overflow_p);
3252
3253 case LAMBDA_EXPR:
09b42213 3254 case NEW_EXPR:
3255 case VEC_NEW_EXPR:
3256 case DELETE_EXPR:
3257 case VEC_DELETE_EXPR:
3258 case THROW_EXPR:
09b42213 3259 case MODOP_EXPR:
3260 /* GCC internal stuff. */
3261 case VA_ARG_EXPR:
3262 case OBJ_TYPE_REF:
3263 case WITH_CLEANUP_EXPR:
09b42213 3264 case NON_DEPENDENT_EXPR:
3265 case BASELINK:
09b42213 3266 case OFFSET_REF:
f83e6885 3267 if (!ctx->quiet)
09b42213 3268 error_at (EXPR_LOC_OR_LOC (t, input_location),
3269 "expression %qE is not a constant-expression", t);
3270 *non_constant_p = true;
3271 break;
3272
cf72f34d 3273 case PLACEHOLDER_EXPR:
3274 if (!ctx || !ctx->ctor || (addr && !ctx->object))
3275 {
3276 /* A placeholder without a referent. We can get here when
3277 checking whether NSDMIs are noexcept, or in massage_init_elt;
3278 just say it's non-constant for now. */
f83e6885 3279 gcc_assert (ctx->quiet);
cf72f34d 3280 *non_constant_p = true;
3281 break;
3282 }
3283 else
3284 {
3285 /* Use of the value or address of the current object. We could
3286 use ctx->object unconditionally, but using ctx->ctor when we
3287 can is a minor optimization. */
3288 tree ctor = addr ? ctx->object : ctx->ctor;
3289 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3290 (TREE_TYPE (t), TREE_TYPE (ctor)));
3291 return cxx_eval_constant_expression
f83e6885 3292 (ctx, ctor, addr,
b2a43197 3293 non_constant_p, overflow_p);
cf72f34d 3294 }
3295 break;
3296
9c96033c 3297 case GOTO_EXPR:
00f21715 3298 *jump_target = TREE_OPERAND (t, 0);
3299 gcc_assert (breaks (jump_target) || continues (jump_target));
3300 break;
3301
9c96033c 3302 case LOOP_EXPR:
f83e6885 3303 cxx_eval_loop_expr (ctx, t,
00f21715 3304 non_constant_p, overflow_p, jump_target);
3305 break;
3306
9c96033c 3307 case SWITCH_EXPR:
f83e6885 3308 cxx_eval_switch_expr (ctx, t,
00f21715 3309 non_constant_p, overflow_p, jump_target);
9c96033c 3310 break;
3311
09b42213 3312 default:
3313 internal_error ("unexpected expression %qE of kind %s", t,
3314 get_tree_code_name (TREE_CODE (t)));
3315 *non_constant_p = true;
3316 break;
3317 }
3318
3319 if (r == error_mark_node)
3320 *non_constant_p = true;
3321
3322 if (*non_constant_p)
3323 return t;
3324 else
3325 return r;
3326}
3327
3328static tree
cf72f34d 3329cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
2055d27a 3330 bool strict = true, tree object = NULL_TREE)
09b42213 3331{
3332 bool non_constant_p = false;
3333 bool overflow_p = false;
2055d27a 3334 constexpr_ctx ctx = { NULL, NULL, NULL, NULL, allow_non_constant, strict };
cf72f34d 3335 hash_map<tree,tree> map;
3336 ctx.values = &map;
3337 tree type = initialized_type (t);
cf72f34d 3338 tree r = t;
3339 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
3340 {
3341 /* In C++14 an NSDMI can participate in aggregate initialization,
3342 and can refer to the address of the object being initialized, so
3343 we need to pass in the relevant VAR_DECL if we want to do the
3344 evaluation in a single pass. The evaluation will dynamically
3345 update ctx.values for the VAR_DECL. We use the same strategy
3346 for C++11 constexpr constructors that refer to the object being
3347 initialized. */
3348 ctx.ctor = build_constructor (type, NULL);
3349 CONSTRUCTOR_NO_IMPLICIT_ZERO (ctx.ctor) = true;
9c96033c 3350 if (!object)
3351 {
3352 if (TREE_CODE (t) == TARGET_EXPR)
3353 object = TARGET_EXPR_SLOT (t);
3354 else if (TREE_CODE (t) == AGGR_INIT_EXPR)
3355 object = AGGR_INIT_EXPR_SLOT (t);
3356 }
cf72f34d 3357 ctx.object = object;
3358 if (object)
3359 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3360 (type, TREE_TYPE (object)));
3361 if (object && DECL_P (object))
3362 map.put (object, ctx.ctor);
3363 if (TREE_CODE (r) == TARGET_EXPR)
3364 /* Avoid creating another CONSTRUCTOR when we expand the
3365 TARGET_EXPR. */
3366 r = TARGET_EXPR_INITIAL (r);
3367 }
3368
f83e6885 3369 r = cxx_eval_constant_expression (&ctx, r,
b2a43197 3370 false, &non_constant_p, &overflow_p);
09b42213 3371
3372 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
3373
bbcc9042 3374 /* Mutable logic is a bit tricky: we want to allow initialization of
3375 constexpr variables with mutable members, but we can't copy those
3376 members to another constexpr variable. */
3377 if (TREE_CODE (r) == CONSTRUCTOR
3378 && CONSTRUCTOR_MUTABLE_POISON (r))
09b42213 3379 {
09b42213 3380 if (!allow_non_constant)
bbcc9042 3381 error ("%qE is not a constant expression because it refers to "
3382 "mutable subobjects of %qT", t, type);
09b42213 3383 non_constant_p = true;
3384 }
3385
3386 /* Technically we should check this for all subexpressions, but that
3387 runs into problems with our internal representation of pointer
3388 subtraction and the 5.19 rules are still in flux. */
3389 if (CONVERT_EXPR_CODE_P (TREE_CODE (r))
3390 && ARITHMETIC_TYPE_P (TREE_TYPE (r))
3391 && TREE_CODE (TREE_OPERAND (r, 0)) == ADDR_EXPR)
3392 {
3393 if (!allow_non_constant)
3394 error ("conversion from pointer type %qT "
3395 "to arithmetic type %qT in a constant-expression",
3396 TREE_TYPE (TREE_OPERAND (r, 0)), TREE_TYPE (r));
3397 non_constant_p = true;
3398 }
3399
3400 if (!non_constant_p && overflow_p)
3401 non_constant_p = true;
3402
3403 if (non_constant_p && !allow_non_constant)
3404 return error_mark_node;
3405 else if (non_constant_p && TREE_CONSTANT (r))
3406 {
3407 /* This isn't actually constant, so unset TREE_CONSTANT. */
3408 if (EXPR_P (r))
3409 r = copy_node (r);
3410 else if (TREE_CODE (r) == CONSTRUCTOR)
3411 r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
3412 else
3413 r = build_nop (TREE_TYPE (r), r);
3414 TREE_CONSTANT (r) = false;
3415 }
3416 else if (non_constant_p || r == t)
3417 return t;
3418
3419 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
3420 {
3421 if (TREE_CODE (t) == TARGET_EXPR
3422 && TARGET_EXPR_INITIAL (t) == r)
3423 return t;
3424 else
3425 {
3426 r = get_target_expr (r);
3427 TREE_CONSTANT (r) = true;
3428 return r;
3429 }
3430 }
3431 else
3432 return r;
3433}
3434
3435/* Returns true if T is a valid subexpression of a constant expression,
3436 even if it isn't itself a constant expression. */
3437
3438bool
3439is_sub_constant_expr (tree t)
3440{
3441 bool non_constant_p = false;
3442 bool overflow_p = false;
2055d27a 3443 constexpr_ctx ctx = { NULL, NULL, NULL, NULL, true, true };
cf72f34d 3444 hash_map <tree, tree> map;
3445 ctx.values = &map;
f83e6885 3446 cxx_eval_constant_expression (&ctx, t, false, &non_constant_p,
b2a43197 3447 &overflow_p);
09b42213 3448 return !non_constant_p && !overflow_p;
3449}
3450
3451/* If T represents a constant expression returns its reduced value.
3452 Otherwise return error_mark_node. If T is dependent, then
3453 return NULL. */
3454
3455tree
cf72f34d 3456cxx_constant_value (tree t, tree decl)
09b42213 3457{
2055d27a 3458 return cxx_eval_outermost_constant_expr (t, false, true, decl);
09b42213 3459}
3460
3461/* If T is a constant expression, returns its reduced value.
3462 Otherwise, if T does not have TREE_CONSTANT set, returns T.
3463 Otherwise, returns a version of T without TREE_CONSTANT. */
3464
3465tree
cf72f34d 3466maybe_constant_value (tree t, tree decl)
09b42213 3467{
3468 tree r;
3469
3470 if (instantiation_dependent_expression_p (t)
3471 || type_unknown_p (t)
3472 || BRACE_ENCLOSED_INITIALIZER_P (t)
3473 || !potential_constant_expression (t))
3474 {
3475 if (TREE_OVERFLOW_P (t))
3476 {
3477 t = build_nop (TREE_TYPE (t), t);
3478 TREE_CONSTANT (t) = false;
3479 }
3480 return t;
3481 }
3482
2055d27a 3483 r = cxx_eval_outermost_constant_expr (t, true, true, decl);
09b42213 3484#ifdef ENABLE_CHECKING
3485 /* cp_tree_equal looks through NOPs, so allow them. */
3486 gcc_assert (r == t
3487 || CONVERT_EXPR_P (t)
21131a05 3488 || TREE_CODE (t) == VIEW_CONVERT_EXPR
09b42213 3489 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
3490 || !cp_tree_equal (r, t));
3491#endif
3492 return r;
3493}
3494
21131a05 3495/* Like maybe_constant_value but first fully instantiate the argument.
3496
3497 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
3498 (t, tf_none) followed by maybe_constant_value but is more efficient,
3499 because calls instantiation_dependent_expression_p and
3500 potential_constant_expression at most once. */
3501
3502tree
3503fold_non_dependent_expr (tree t)
3504{
3505 if (t == NULL_TREE)
3506 return NULL_TREE;
3507
3508 /* If we're in a template, but T isn't value dependent, simplify
3509 it. We're supposed to treat:
3510
3511 template <typename T> void f(T[1 + 1]);
3512 template <typename T> void f(T[2]);
3513
3514 as two declarations of the same function, for example. */
3515 if (processing_template_decl)
3516 {
3517 if (!instantiation_dependent_expression_p (t)
3518 && potential_constant_expression (t))
3519 {
c8a9f04c 3520 processing_template_decl_sentinel s;
3521 t = instantiate_non_dependent_expr_internal (t, tf_none);
21131a05 3522
3523 if (type_unknown_p (t)
3524 || BRACE_ENCLOSED_INITIALIZER_P (t))
3525 {
3526 if (TREE_OVERFLOW_P (t))
3527 {
3528 t = build_nop (TREE_TYPE (t), t);
3529 TREE_CONSTANT (t) = false;
3530 }
3531 return t;
3532 }
3533
2055d27a 3534 tree r = cxx_eval_outermost_constant_expr (t, true, true, NULL_TREE);
21131a05 3535#ifdef ENABLE_CHECKING
3536 /* cp_tree_equal looks through NOPs, so allow them. */
3537 gcc_assert (r == t
3538 || CONVERT_EXPR_P (t)
3539 || TREE_CODE (t) == VIEW_CONVERT_EXPR
3540 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
3541 || !cp_tree_equal (r, t));
3542#endif
3543 return r;
3544 }
3545 else if (TREE_OVERFLOW_P (t))
3546 {
3547 t = build_nop (TREE_TYPE (t), t);
3548 TREE_CONSTANT (t) = false;
3549 }
3550 return t;
3551 }
3552
3553 return maybe_constant_value (t);
3554}
3555
09b42213 3556/* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
3557 than wrapped in a TARGET_EXPR. */
3558
3559tree
cf72f34d 3560maybe_constant_init (tree t, tree decl)
09b42213 3561{
3562 if (TREE_CODE (t) == EXPR_STMT)
3563 t = TREE_OPERAND (t, 0);
3564 if (TREE_CODE (t) == CONVERT_EXPR
3565 && VOID_TYPE_P (TREE_TYPE (t)))
3566 t = TREE_OPERAND (t, 0);
9c96033c 3567 if (TREE_CODE (t) == INIT_EXPR)
3568 t = TREE_OPERAND (t, 1);
2055d27a 3569 if (instantiation_dependent_expression_p (t)
3570 || type_unknown_p (t)
3571 || BRACE_ENCLOSED_INITIALIZER_P (t)
3572 || !potential_static_init_expression (t))
3573 /* Don't try to evaluate it. */;
3574 else
3575 t = cxx_eval_outermost_constant_expr (t, true, false, decl);
09b42213 3576 if (TREE_CODE (t) == TARGET_EXPR)
3577 {
3578 tree init = TARGET_EXPR_INITIAL (t);
3579 if (TREE_CODE (init) == CONSTRUCTOR)
3580 t = init;
3581 }
3582 return t;
3583}
3584
3585#if 0
3586/* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
3587/* Return true if the object referred to by REF has automatic or thread
3588 local storage. */
3589
3590enum { ck_ok, ck_bad, ck_unknown };
3591static int
3592check_automatic_or_tls (tree ref)
3593{
3754d046 3594 machine_mode mode;
09b42213 3595 HOST_WIDE_INT bitsize, bitpos;
3596 tree offset;
3597 int volatilep = 0, unsignedp = 0;
3598 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
3599 &mode, &unsignedp, &volatilep, false);
3600 duration_kind dk;
3601
3602 /* If there isn't a decl in the middle, we don't know the linkage here,
3603 and this isn't a constant expression anyway. */
3604 if (!DECL_P (decl))
3605 return ck_unknown;
3606 dk = decl_storage_duration (decl);
3607 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
3608}
3609#endif
3610
3611/* Return true if T denotes a potentially constant expression. Issue
3612 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
3613 an lvalue-rvalue conversion is implied.
3614
3615 C++0x [expr.const] used to say
3616
3617 6 An expression is a potential constant expression if it is
3618 a constant expression where all occurrences of function
3619 parameters are replaced by arbitrary constant expressions
3620 of the appropriate type.
3621
3622 2 A conditional expression is a constant expression unless it
3623 involves one of the following as a potentially evaluated
3624 subexpression (3.2), but subexpressions of logical AND (5.14),
3625 logical OR (5.15), and conditional (5.16) operations that are
3626 not evaluated are not considered. */
3627
3628static bool
2055d27a 3629potential_constant_expression_1 (tree t, bool want_rval, bool strict,
3630 tsubst_flags_t flags)
09b42213 3631{
2055d27a 3632#define RECUR(T,RV) potential_constant_expression_1 ((T), (RV), strict, flags)
09b42213 3633 enum { any = false, rval = true };
3634 int i;
3635 tree tmp;
3636
3637 if (t == error_mark_node)
3638 return false;
3639 if (t == NULL_TREE)
3640 return true;
3641 if (TREE_THIS_VOLATILE (t))
3642 {
3643 if (flags & tf_error)
3644 error ("expression %qE has side-effects", t);
3645 return false;
3646 }
3647 if (CONSTANT_CLASS_P (t))
3648 return true;
3649
3650 switch (TREE_CODE (t))
3651 {
3652 case FUNCTION_DECL:
3653 case BASELINK:
3654 case TEMPLATE_DECL:
3655 case OVERLOAD:
3656 case TEMPLATE_ID_EXPR:
3657 case LABEL_DECL:
3658 case LABEL_EXPR:
9c96033c 3659 case CASE_LABEL_EXPR:
09b42213 3660 case CONST_DECL:
3661 case SIZEOF_EXPR:
3662 case ALIGNOF_EXPR:
3663 case OFFSETOF_EXPR:
3664 case NOEXCEPT_EXPR:
3665 case TEMPLATE_PARM_INDEX:
3666 case TRAIT_EXPR:
3667 case IDENTIFIER_NODE:
3668 case USERDEF_LITERAL:
3669 /* We can see a FIELD_DECL in a pointer-to-member expression. */
3670 case FIELD_DECL:
3671 case PARM_DECL:
3672 case USING_DECL:
9c96033c 3673 case USING_STMT:
cf72f34d 3674 case PLACEHOLDER_EXPR:
9c96033c 3675 case BREAK_STMT:
3676 case CONTINUE_STMT:
09b42213 3677 return true;
3678
3679 case AGGR_INIT_EXPR:
3680 case CALL_EXPR:
3681 /* -- an invocation of a function other than a constexpr function
3682 or a constexpr constructor. */
3683 {
3684 tree fun = get_function_named_in_call (t);
3685 const int nargs = call_expr_nargs (t);
3686 i = 0;
3687
9c96033c 3688 if (fun == NULL_TREE)
3689 {
3690 /* fold_call_expr can't do anything with IFN calls. */
3691 if (flags & tf_error)
3692 error_at (EXPR_LOC_OR_LOC (t, input_location),
3693 "call to internal function");
3694 return false;
3695 }
09b42213 3696 if (is_overloaded_fn (fun))
3697 {
3698 if (TREE_CODE (fun) == FUNCTION_DECL)
3699 {
3700 if (builtin_valid_in_constant_expr_p (fun))
3701 return true;
3702 if (!DECL_DECLARED_CONSTEXPR_P (fun)
3703 /* Allow any built-in function; if the expansion
3704 isn't constant, we'll deal with that then. */
3705 && !is_builtin_fn (fun))
3706 {
3707 if (flags & tf_error)
3708 {
3709 error_at (EXPR_LOC_OR_LOC (t, input_location),
3710 "call to non-constexpr function %qD", fun);
3711 explain_invalid_constexpr_fn (fun);
3712 }
3713 return false;
3714 }
3715 /* A call to a non-static member function takes the address
3716 of the object as the first argument. But in a constant
3717 expression the address will be folded away, so look
3718 through it now. */
3719 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
3720 && !DECL_CONSTRUCTOR_P (fun))
3721 {
3722 tree x = get_nth_callarg (t, 0);
3723 if (is_this_parameter (x))
cf72f34d 3724 return true;
2055d27a 3725 else if (!RECUR (x, rval))
09b42213 3726 return false;
3727 i = 1;
3728 }
3729 }
3730 else
3731 {
2055d27a 3732 if (!RECUR (fun, true))
09b42213 3733 return false;
3734 fun = get_first_fn (fun);
3735 }
3736 /* Skip initial arguments to base constructors. */
3737 if (DECL_BASE_CONSTRUCTOR_P (fun))
3738 i = num_artificial_parms_for (fun);
3739 fun = DECL_ORIGIN (fun);
3740 }
3741 else
3742 {
2055d27a 3743 if (RECUR (fun, rval))
09b42213 3744 /* Might end up being a constant function pointer. */;
3745 else
3746 return false;
3747 }
3748 for (; i < nargs; ++i)
3749 {
3750 tree x = get_nth_callarg (t, i);
2055d27a 3751 if (!RECUR (x, rval))
09b42213 3752 return false;
3753 }
3754 return true;
3755 }
3756
3757 case NON_LVALUE_EXPR:
3758 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
3759 -- an lvalue of integral type that refers to a non-volatile
3760 const variable or static data member initialized with
3761 constant expressions, or
3762
3763 -- an lvalue of literal type that refers to non-volatile
3764 object defined with constexpr, or that refers to a
3765 sub-object of such an object; */
2055d27a 3766 return RECUR (TREE_OPERAND (t, 0), rval);
09b42213 3767
3768 case VAR_DECL:
2055d27a 3769 if (want_rval
3770 && !decl_constant_var_p (t)
3771 && (strict
3772 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
3773 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t))
09b42213 3774 && !var_in_constexpr_fn (t)
3775 && !dependent_type_p (TREE_TYPE (t)))
3776 {
3777 if (flags & tf_error)
3778 non_const_var_error (t);
3779 return false;
3780 }
3781 return true;
3782
3783 case NOP_EXPR:
3784 case CONVERT_EXPR:
3785 case VIEW_CONVERT_EXPR:
3786 /* -- a reinterpret_cast. FIXME not implemented, and this rule
3787 may change to something more specific to type-punning (DR 1312). */
3788 {
3789 tree from = TREE_OPERAND (t, 0);
3790 if (POINTER_TYPE_P (TREE_TYPE (t))
3791 && TREE_CODE (from) == INTEGER_CST
3792 && !integer_zerop (from))
3793 {
3794 if (flags & tf_error)
3795 error_at (EXPR_LOC_OR_LOC (t, input_location),
3796 "reinterpret_cast from integer to pointer");
3797 return false;
3798 }
2055d27a 3799 return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
09b42213 3800 }
3801
3802 case ADDR_EXPR:
3803 /* -- a unary operator & that is applied to an lvalue that
3804 designates an object with thread or automatic storage
3805 duration; */
3806 t = TREE_OPERAND (t, 0);
3807
3808 if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
3809 /* A pointer-to-member constant. */
3810 return true;
3811
3812#if 0
3813 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
3814 any checking here, as we might dereference the pointer later. If
3815 we remove this code, also remove check_automatic_or_tls. */
3816 i = check_automatic_or_tls (t);
3817 if (i == ck_ok)
3818 return true;
3819 if (i == ck_bad)
3820 {
3821 if (flags & tf_error)
3822 error ("address-of an object %qE with thread local or "
3823 "automatic storage is not a constant expression", t);
3824 return false;
3825 }
3826#endif
2055d27a 3827 return RECUR (t, any);
09b42213 3828
3829 case COMPONENT_REF:
3830 case BIT_FIELD_REF:
3831 case ARROW_EXPR:
3832 case OFFSET_REF:
3833 /* -- a class member access unless its postfix-expression is
3834 of literal type or of pointer to literal type. */
3835 /* This test would be redundant, as it follows from the
3836 postfix-expression being a potential constant expression. */
2055d27a 3837 return RECUR (TREE_OPERAND (t, 0), want_rval);
09b42213 3838
3839 case EXPR_PACK_EXPANSION:
2055d27a 3840 return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
09b42213 3841
3842 case INDIRECT_REF:
3843 {
3844 tree x = TREE_OPERAND (t, 0);
3845 STRIP_NOPS (x);
3846 if (is_this_parameter (x))
3847 {
3848 if (DECL_CONTEXT (x)
3849 && !DECL_DECLARED_CONSTEXPR_P (DECL_CONTEXT (x)))
3850 {
3851 if (flags & tf_error)
3852 error ("use of %<this%> in a constant expression");
3853 return false;
3854 }
09b42213 3855 return true;
3856 }
2055d27a 3857 return RECUR (x, rval);
09b42213 3858 }
3859
9c96033c 3860 case STATEMENT_LIST:
3861 {
3862 tree_stmt_iterator i;
3863 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
3864 {
2055d27a 3865 if (!RECUR (tsi_stmt (i), any))
9c96033c 3866 return false;
3867 }
3868 return true;
3869 }
3870 break;
3871
3872 case MODIFY_EXPR:
3873 if (cxx_dialect < cxx14)
3874 goto fail;
2055d27a 3875 if (!RECUR (TREE_OPERAND (t, 0), any))
9c96033c 3876 return false;
2055d27a 3877 if (!RECUR (TREE_OPERAND (t, 1), rval))
9c96033c 3878 return false;
3879 return true;
3880
3881 case MODOP_EXPR:
3882 if (cxx_dialect < cxx14)
3883 goto fail;
2055d27a 3884 if (!RECUR (TREE_OPERAND (t, 0), rval))
9c96033c 3885 return false;
2055d27a 3886 if (!RECUR (TREE_OPERAND (t, 2), rval))
9c96033c 3887 return false;
3888 return true;
3889
3890 case IF_STMT:
2055d27a 3891 if (!RECUR (IF_COND (t), rval))
9c96033c 3892 return false;
2055d27a 3893 if (!RECUR (THEN_CLAUSE (t), any))
9c96033c 3894 return false;
2055d27a 3895 if (!RECUR (ELSE_CLAUSE (t), any))
9c96033c 3896 return false;
3897 return true;
3898
3899 case DO_STMT:
2055d27a 3900 if (!RECUR (DO_COND (t), rval))
9c96033c 3901 return false;
2055d27a 3902 if (!RECUR (DO_BODY (t), any))
9c96033c 3903 return false;
3904 return true;
3905
3906 case FOR_STMT:
2055d27a 3907 if (!RECUR (FOR_INIT_STMT (t), any))
9c96033c 3908 return false;
2055d27a 3909 if (!RECUR (FOR_COND (t), rval))
9c96033c 3910 return false;
2055d27a 3911 if (!RECUR (FOR_EXPR (t), any))
9c96033c 3912 return false;
2055d27a 3913 if (!RECUR (FOR_BODY (t), any))
9c96033c 3914 return false;
3915 return true;
3916
3917 case WHILE_STMT:
2055d27a 3918 if (!RECUR (WHILE_COND (t), rval))
9c96033c 3919 return false;
2055d27a 3920 if (!RECUR (WHILE_BODY (t), any))
9c96033c 3921 return false;
3922 return true;
3923
3924 case SWITCH_STMT:
2055d27a 3925 if (!RECUR (SWITCH_STMT_COND (t), rval))
9c96033c 3926 return false;
2055d27a 3927 if (!RECUR (SWITCH_STMT_BODY (t), any))
9c96033c 3928 return false;
3929 return true;
3930
da7981e0 3931 case STMT_EXPR:
2055d27a 3932 return RECUR (STMT_EXPR_STMT (t), rval);
da7981e0 3933
09b42213 3934 case LAMBDA_EXPR:
3935 case DYNAMIC_CAST_EXPR:
3936 case PSEUDO_DTOR_EXPR:
09b42213 3937 case NEW_EXPR:
3938 case VEC_NEW_EXPR:
3939 case DELETE_EXPR:
3940 case VEC_DELETE_EXPR:
3941 case THROW_EXPR:
09b42213 3942 case OMP_ATOMIC:
3943 case OMP_ATOMIC_READ:
3944 case OMP_ATOMIC_CAPTURE_OLD:
3945 case OMP_ATOMIC_CAPTURE_NEW:
3946 /* GCC internal stuff. */
3947 case VA_ARG_EXPR:
3948 case OBJ_TYPE_REF:
09b42213 3949 case TRANSACTION_EXPR:
9c96033c 3950 case ASM_EXPR:
3951 fail:
09b42213 3952 if (flags & tf_error)
3953 error ("expression %qE is not a constant-expression", t);
3954 return false;
3955
3956 case TYPEID_EXPR:
3957 /* -- a typeid expression whose operand is of polymorphic
3958 class type; */
3959 {
3960 tree e = TREE_OPERAND (t, 0);
3961 if (!TYPE_P (e) && !type_dependent_expression_p (e)
3962 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
3963 {
3964 if (flags & tf_error)
3965 error ("typeid-expression is not a constant expression "
3966 "because %qE is of polymorphic type", e);
3967 return false;
3968 }
3969 return true;
3970 }
3971
3972 case MINUS_EXPR:
3973 /* -- a subtraction where both operands are pointers. */
3974 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
3975 && TYPE_PTR_P (TREE_OPERAND (t, 1)))
3976 {
3977 if (flags & tf_error)
3978 error ("difference of two pointer expressions is not "
3979 "a constant expression");
3980 return false;
3981 }
3982 want_rval = true;
3983 goto binary;
3984
3985 case LT_EXPR:
3986 case LE_EXPR:
3987 case GT_EXPR:
3988 case GE_EXPR:
3989 case EQ_EXPR:
3990 case NE_EXPR:
3991 /* -- a relational or equality operator where at least
3992 one of the operands is a pointer. */
3993 if (TYPE_PTR_P (TREE_OPERAND (t, 0))
3994 || TYPE_PTR_P (TREE_OPERAND (t, 1)))
3995 {
3996 if (flags & tf_error)
3997 error ("pointer comparison expression is not a "
3998 "constant expression");
3999 return false;
4000 }
4001 want_rval = true;
4002 goto binary;
4003
9c96033c 4004 case PREINCREMENT_EXPR:
4005 case POSTINCREMENT_EXPR:
4006 case PREDECREMENT_EXPR:
4007 case POSTDECREMENT_EXPR:
4008 if (cxx_dialect < cxx14)
4009 goto fail;
4010 goto unary;
4011
09b42213 4012 case BIT_NOT_EXPR:
4013 /* A destructor. */
4014 if (TYPE_P (TREE_OPERAND (t, 0)))
4015 return true;
4016 /* else fall through. */
4017
4018 case REALPART_EXPR:
4019 case IMAGPART_EXPR:
4020 case CONJ_EXPR:
4021 case SAVE_EXPR:
4022 case FIX_TRUNC_EXPR:
4023 case FLOAT_EXPR:
4024 case NEGATE_EXPR:
4025 case ABS_EXPR:
4026 case TRUTH_NOT_EXPR:
4027 case FIXED_CONVERT_EXPR:
4028 case UNARY_PLUS_EXPR:
9c96033c 4029 unary:
2055d27a 4030 return RECUR (TREE_OPERAND (t, 0), rval);
09b42213 4031
4032 case CAST_EXPR:
4033 case CONST_CAST_EXPR:
4034 case STATIC_CAST_EXPR:
4035 case REINTERPRET_CAST_EXPR:
4036 case IMPLICIT_CONV_EXPR:
4037 if (cxx_dialect < cxx11
4038 && !dependent_type_p (TREE_TYPE (t))
4039 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
4040 /* In C++98, a conversion to non-integral type can't be part of a
4041 constant expression. */
4042 {
4043 if (flags & tf_error)
4044 error ("cast to non-integral type %qT in a constant expression",
4045 TREE_TYPE (t));
4046 return false;
4047 }
4048
2055d27a 4049 return (RECUR (TREE_OPERAND (t, 0),
4050 TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE));
09b42213 4051
9c96033c 4052 case BIND_EXPR:
2055d27a 4053 return RECUR (BIND_EXPR_BODY (t), want_rval);
9c96033c 4054
4055 case WITH_CLEANUP_EXPR:
4056 case CLEANUP_POINT_EXPR:
4057 case MUST_NOT_THROW_EXPR:
4058 case TRY_CATCH_EXPR:
4059 case EH_SPEC_BLOCK:
4060 case EXPR_STMT:
09b42213 4061 case PAREN_EXPR:
9c96033c 4062 case DECL_EXPR:
09b42213 4063 case NON_DEPENDENT_EXPR:
4064 /* For convenience. */
4065 case RETURN_EXPR:
2055d27a 4066 return RECUR (TREE_OPERAND (t, 0), want_rval);
09b42213 4067
4068 case SCOPE_REF:
2055d27a 4069 return RECUR (TREE_OPERAND (t, 1), want_rval);
09b42213 4070
4071 case TARGET_EXPR:
4072 if (!literal_type_p (TREE_TYPE (t)))
4073 {
4074 if (flags & tf_error)
4075 {
4076 error ("temporary of non-literal type %qT in a "
4077 "constant expression", TREE_TYPE (t));
4078 explain_non_literal_class (TREE_TYPE (t));
4079 }
4080 return false;
4081 }
4082 case INIT_EXPR:
2055d27a 4083 return RECUR (TREE_OPERAND (t, 1), rval);
09b42213 4084
4085 case CONSTRUCTOR:
4086 {
4087 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
4088 constructor_elt *ce;
4089 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
2055d27a 4090 if (!RECUR (ce->value, want_rval))
09b42213 4091 return false;
4092 return true;
4093 }
4094
4095 case TREE_LIST:
4096 {
4097 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
4098 || DECL_P (TREE_PURPOSE (t)));
2055d27a 4099 if (!RECUR (TREE_VALUE (t), want_rval))
09b42213 4100 return false;
4101 if (TREE_CHAIN (t) == NULL_TREE)
4102 return true;
2055d27a 4103 return RECUR (TREE_CHAIN (t), want_rval);
09b42213 4104 }
4105
4106 case TRUNC_DIV_EXPR:
4107 case CEIL_DIV_EXPR:
4108 case FLOOR_DIV_EXPR:
4109 case ROUND_DIV_EXPR:
4110 case TRUNC_MOD_EXPR:
4111 case CEIL_MOD_EXPR:
4112 case ROUND_MOD_EXPR:
4113 {
4114 tree denom = TREE_OPERAND (t, 1);
2055d27a 4115 if (!RECUR (denom, rval))
09b42213 4116 return false;
4117 /* We can't call cxx_eval_outermost_constant_expr on an expression
21131a05 4118 that hasn't been through instantiate_non_dependent_expr yet. */
09b42213 4119 if (!processing_template_decl)
4120 denom = cxx_eval_outermost_constant_expr (denom, true);
4121 if (integer_zerop (denom))
4122 {
4123 if (flags & tf_error)
4124 error ("division by zero is not a constant-expression");
4125 return false;
4126 }
4127 else
4128 {
4129 want_rval = true;
2055d27a 4130 return RECUR (TREE_OPERAND (t, 0), want_rval);
09b42213 4131 }
4132 }
4133
4134 case COMPOUND_EXPR:
4135 {
4136 /* check_return_expr sometimes wraps a TARGET_EXPR in a
4137 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
4138 introduced by build_call_a. */
4139 tree op0 = TREE_OPERAND (t, 0);
4140 tree op1 = TREE_OPERAND (t, 1);
4141 STRIP_NOPS (op1);
4142 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
4143 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
2055d27a 4144 return RECUR (op0, want_rval);
09b42213 4145 else
4146 goto binary;
4147 }
4148
4149 /* If the first operand is the non-short-circuit constant, look at
4150 the second operand; otherwise we only care about the first one for
4151 potentiality. */
4152 case TRUTH_AND_EXPR:
4153 case TRUTH_ANDIF_EXPR:
4154 tmp = boolean_true_node;
4155 goto truth;
4156 case TRUTH_OR_EXPR:
4157 case TRUTH_ORIF_EXPR:
4158 tmp = boolean_false_node;
4159 truth:
4160 {
4161 tree op = TREE_OPERAND (t, 0);
2055d27a 4162 if (!RECUR (op, rval))
09b42213 4163 return false;
4164 if (!processing_template_decl)
4165 op = cxx_eval_outermost_constant_expr (op, true);
4166 if (tree_int_cst_equal (op, tmp))
2055d27a 4167 return RECUR (TREE_OPERAND (t, 1), rval);
09b42213 4168 else
4169 return true;
4170 }
4171
4172 case PLUS_EXPR:
4173 case MULT_EXPR:
4174 case POINTER_PLUS_EXPR:
4175 case RDIV_EXPR:
4176 case EXACT_DIV_EXPR:
4177 case MIN_EXPR:
4178 case MAX_EXPR:
4179 case LSHIFT_EXPR:
4180 case RSHIFT_EXPR:
4181 case LROTATE_EXPR:
4182 case RROTATE_EXPR:
4183 case BIT_IOR_EXPR:
4184 case BIT_XOR_EXPR:
4185 case BIT_AND_EXPR:
4186 case TRUTH_XOR_EXPR:
4187 case UNORDERED_EXPR:
4188 case ORDERED_EXPR:
4189 case UNLT_EXPR:
4190 case UNLE_EXPR:
4191 case UNGT_EXPR:
4192 case UNGE_EXPR:
4193 case UNEQ_EXPR:
4194 case LTGT_EXPR:
4195 case RANGE_EXPR:
4196 case COMPLEX_EXPR:
4197 want_rval = true;
4198 /* Fall through. */
4199 case ARRAY_REF:
4200 case ARRAY_RANGE_REF:
4201 case MEMBER_REF:
4202 case DOTSTAR_EXPR:
4203 binary:
4204 for (i = 0; i < 2; ++i)
2055d27a 4205 if (!RECUR (TREE_OPERAND (t, i), want_rval))
09b42213 4206 return false;
4207 return true;
4208
4209 case CILK_SYNC_STMT:
4210 case CILK_SPAWN_STMT:
4211 case ARRAY_NOTATION_REF:
4212 return false;
4213
4214 case FMA_EXPR:
4215 case VEC_PERM_EXPR:
4216 for (i = 0; i < 3; ++i)
2055d27a 4217 if (!RECUR (TREE_OPERAND (t, i), true))
09b42213 4218 return false;
4219 return true;
4220
4221 case COND_EXPR:
4222 case VEC_COND_EXPR:
4223 /* If the condition is a known constant, we know which of the legs we
4224 care about; otherwise we only require that the condition and
4225 either of the legs be potentially constant. */
4226 tmp = TREE_OPERAND (t, 0);
2055d27a 4227 if (!RECUR (tmp, rval))
09b42213 4228 return false;
4229 if (!processing_template_decl)
4230 tmp = cxx_eval_outermost_constant_expr (tmp, true);
4231 if (integer_zerop (tmp))
2055d27a 4232 return RECUR (TREE_OPERAND (t, 2), want_rval);
09b42213 4233 else if (TREE_CODE (tmp) == INTEGER_CST)
2055d27a 4234 return RECUR (TREE_OPERAND (t, 1), want_rval);
09b42213 4235 for (i = 1; i < 3; ++i)
4236 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
2055d27a 4237 want_rval, strict, tf_none))
09b42213 4238 return true;
4239 if (flags & tf_error)
4240 error ("expression %qE is not a constant-expression", t);
4241 return false;
4242
4243 case VEC_INIT_EXPR:
4244 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
4245 return true;
4246 if (flags & tf_error)
4247 {
4248 error ("non-constant array initialization");
4249 diagnose_non_constexpr_vec_init (t);
4250 }
4251 return false;
4252
4253 default:
4254 if (objc_is_property_ref (t))
4255 return false;
4256
4257 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
4258 gcc_unreachable();
4259 return false;
4260 }
2055d27a 4261#undef RECUR
09b42213 4262}
4263
4264/* The main entry point to the above. */
4265
4266bool
4267potential_constant_expression (tree t)
4268{
2055d27a 4269 return potential_constant_expression_1 (t, false, true, tf_none);
4270}
4271
4272bool
4273potential_static_init_expression (tree t)
4274{
4275 return potential_constant_expression_1 (t, false, false, tf_none);
09b42213 4276}
4277
4278/* As above, but require a constant rvalue. */
4279
4280bool
4281potential_rvalue_constant_expression (tree t)
4282{
2055d27a 4283 return potential_constant_expression_1 (t, true, true, tf_none);
09b42213 4284}
4285
4286/* Like above, but complain about non-constant expressions. */
4287
4288bool
4289require_potential_constant_expression (tree t)
4290{
2055d27a 4291 return potential_constant_expression_1 (t, false, true, tf_warning_or_error);
09b42213 4292}
4293
4294/* Cross product of the above. */
4295
4296bool
4297require_potential_rvalue_constant_expression (tree t)
4298{
2055d27a 4299 return potential_constant_expression_1 (t, true, true, tf_warning_or_error);
09b42213 4300}
4301
4302#include "gt-cp-constexpr.h"