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