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