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