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