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