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