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