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