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