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