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