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