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