Branch data Line data Source code
1 : : // This file is part of GCC.
2 : :
3 : : // GCC is free software; you can redistribute it and/or modify it under
4 : : // the terms of the GNU General Public License as published by the Free
5 : : // Software Foundation; either version 3, or (at your option) any later
6 : : // version.
7 : :
8 : : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
9 : : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 : : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 : : // for more details.
12 : :
13 : : // You should have received a copy of the GNU General Public License
14 : : // along with GCC; see the file COPYING3. If not see
15 : : // <http://www.gnu.org/licenses/>.
16 : :
17 : : #include "rust-constexpr.h"
18 : : #include "rust-location.h"
19 : : #include "rust-diagnostics.h"
20 : : #include "rust-tree.h"
21 : : #include "fold-const.h"
22 : : #include "realmpfr.h"
23 : : #include "convert.h"
24 : : #include "print-tree.h"
25 : : #include "gimplify.h"
26 : : #include "tree-iterator.h"
27 : : #include "timevar.h"
28 : : #include "varasm.h"
29 : : #include "cgraph.h"
30 : : #include "tree-inline.h"
31 : : #include "vec.h"
32 : : #include "function.h"
33 : : #include "diagnostic.h"
34 : : #include "target.h"
35 : : #include "builtins.h"
36 : :
37 : : #define VERIFY_CONSTANT(X) \
38 : : do \
39 : : { \
40 : : if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
41 : : return t; \
42 : : } \
43 : : while (0)
44 : :
45 : : namespace Rust {
46 : : namespace Compile {
47 : :
48 : : /* Returns true iff FUN is an instantiation of a constexpr function
49 : : template or a defaulted constexpr function. */
50 : :
51 : : bool
52 : 0 : is_instantiation_of_constexpr (tree fun)
53 : : {
54 : 0 : return DECL_DECLARED_CONSTEXPR_P (fun);
55 : : }
56 : :
57 : : /* Return true if T is a literal type. */
58 : :
59 : : bool
60 : 0 : literal_type_p (tree t)
61 : : {
62 : 0 : if (SCALAR_TYPE_P (t) || VECTOR_TYPE_P (t) || TYPE_REF_P (t)
63 : : || (VOID_TYPE_P (t)))
64 : 0 : return true;
65 : :
66 : : if (TREE_CODE (t) == ARRAY_TYPE)
67 : 0 : return literal_type_p (strip_array_types (t));
68 : : return false;
69 : : }
70 : :
71 : : static bool verify_constant (tree, bool, bool *, bool *);
72 : :
73 : : static HOST_WIDE_INT find_array_ctor_elt (tree ary, tree dindex,
74 : : bool insert = false);
75 : : static int array_index_cmp (tree key, tree index);
76 : : static bool potential_constant_expression_1 (tree t, bool want_rval,
77 : : bool strict, bool now,
78 : : tsubst_flags_t flags,
79 : : tree *jump_target);
80 : : bool potential_constant_expression_1 (tree t, bool want_rval, bool strict,
81 : : bool now, tsubst_flags_t flags);
82 : : tree unshare_constructor (tree t MEM_STAT_DECL);
83 : : void maybe_save_constexpr_fundef (tree fun);
84 : :
85 : : static bool returns (tree *jump_target);
86 : : static bool breaks (tree *jump_target);
87 : : static bool continues (tree *jump_target);
88 : : static bool switches (tree *jump_target);
89 : :
90 : 4667 : struct constexpr_global_ctx
91 : : {
92 : : /* Values for any temporaries or local variables within the
93 : : constant-expression. */
94 : : hash_map<tree, tree> values;
95 : : /* Number of cxx_eval_constant_expression calls (except skipped ones,
96 : : on simple constants or location wrappers) encountered during current
97 : : cxx_eval_outermost_constant_expr call. */
98 : : HOST_WIDE_INT constexpr_ops_count;
99 : : /* Heap VAR_DECLs created during the evaluation of the outermost constant
100 : : expression. */
101 : : auto_vec<tree, 16> heap_vars;
102 : : /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */
103 : : vec<tree> *cleanups;
104 : : /* Number of heap VAR_DECL deallocations. */
105 : : unsigned heap_dealloc_count;
106 : : /* Constructor. */
107 : 4667 : constexpr_global_ctx ()
108 : 4667 : : constexpr_ops_count (0), cleanups (NULL), heap_dealloc_count (0)
109 : 4667 : {}
110 : : };
111 : :
112 : : /* In constexpr.cc */
113 : : /* Representation of entries in the constexpr function definition table. */
114 : :
115 : : struct GTY ((for_user)) rust_constexpr_fundef
116 : : {
117 : : tree decl;
118 : : tree body;
119 : : tree parms;
120 : : tree result;
121 : : };
122 : :
123 : : /* Objects of this type represent calls to constexpr functions
124 : : along with the bindings of parameters to their arguments, for
125 : : the purpose of compile time evaluation. */
126 : :
127 : : struct GTY ((for_user)) rust_constexpr_call
128 : : {
129 : : /* Description of the constexpr function definition. */
130 : : rust_constexpr_fundef *fundef;
131 : : /* Parameter bindings environment. A TREE_VEC of arguments. */
132 : : tree bindings;
133 : : /* Result of the call.
134 : : NULL means the call is being evaluated.
135 : : error_mark_node means that the evaluation was erroneous;
136 : : otherwise, the actual value of the call. */
137 : : tree result;
138 : : /* The hash of this call; we remember it here to avoid having to
139 : : recalculate it when expanding the hash table. */
140 : : hashval_t hash;
141 : : /* Whether __builtin_is_constant_evaluated() should evaluate to true. */
142 : : bool manifestly_const_eval;
143 : : };
144 : :
145 : : struct rust_constexpr_call_hasher : ggc_ptr_hash<rust_constexpr_call>
146 : : {
147 : : static hashval_t hash (rust_constexpr_call *);
148 : : static bool equal (rust_constexpr_call *, rust_constexpr_call *);
149 : : };
150 : :
151 : : enum constexpr_switch_state
152 : : {
153 : : /* Used when processing a switch for the first time by cxx_eval_switch_expr
154 : : and default: label for that switch has not been seen yet. */
155 : : css_default_not_seen,
156 : : /* Used when processing a switch for the first time by cxx_eval_switch_expr
157 : : and default: label for that switch has been seen already. */
158 : : css_default_seen,
159 : : /* Used when processing a switch for the second time by
160 : : cxx_eval_switch_expr, where default: label should match. */
161 : : css_default_processing
162 : : };
163 : :
164 : : struct constexpr_ctx
165 : : {
166 : : /* The part of the context that needs to be unique to the whole
167 : : cxx_eval_outermost_constant_expr invocation. */
168 : : constexpr_global_ctx *global;
169 : : /* The innermost call we're evaluating. */
170 : : rust_constexpr_call *call;
171 : : /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen
172 : : within the current LOOP_EXPR. NULL if we aren't inside a loop. */
173 : : vec<tree> *save_exprs;
174 : : /* The CONSTRUCTOR we're currently building up for an aggregate
175 : : initializer. */
176 : : tree ctor;
177 : : /* The object we're building the CONSTRUCTOR for. */
178 : : tree object;
179 : : /* If inside SWITCH_EXPR. */
180 : : constexpr_switch_state *css_state;
181 : : /* The aggregate initialization context inside which this one is nested. This
182 : : is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */
183 : : const constexpr_ctx *parent;
184 : :
185 : : /* Whether we should error on a non-constant expression or fail quietly.
186 : : This flag needs to be here, but some of the others could move to global
187 : : if they get larger than a word. */
188 : : bool quiet;
189 : : /* Whether we are strictly conforming to constant expression rules or
190 : : trying harder to get a constant value. */
191 : : bool strict;
192 : : /* Whether __builtin_is_constant_evaluated () should be true. */
193 : : bool manifestly_const_eval;
194 : : };
195 : :
196 : : struct rust_constexpr_fundef_hasher : ggc_ptr_hash<rust_constexpr_fundef>
197 : : {
198 : : static hashval_t hash (const rust_constexpr_fundef *);
199 : : static bool equal (const rust_constexpr_fundef *,
200 : : const rust_constexpr_fundef *);
201 : : };
202 : :
203 : : /* This table holds all constexpr function definitions seen in
204 : : the current translation unit. */
205 : :
206 : : static GTY (())
207 : : hash_table<rust_constexpr_fundef_hasher> *constexpr_fundef_table;
208 : :
209 : : /* Utility function used for managing the constexpr function table.
210 : : Return true if the entries pointed to by P and Q are for the
211 : : same constexpr function. */
212 : :
213 : : inline bool
214 : 13952 : rust_constexpr_fundef_hasher::equal (const rust_constexpr_fundef *lhs,
215 : : const rust_constexpr_fundef *rhs)
216 : : {
217 : 13952 : return lhs->decl == rhs->decl;
218 : : }
219 : :
220 : : /* Utility function used for managing the constexpr function table.
221 : : Return a hash value for the entry pointed to by Q. */
222 : :
223 : : inline hashval_t
224 : 19061 : rust_constexpr_fundef_hasher::hash (const rust_constexpr_fundef *fundef)
225 : : {
226 : 19061 : return DECL_UID (fundef->decl);
227 : : }
228 : :
229 : : /* Return a previously saved definition of function FUN. */
230 : :
231 : : rust_constexpr_fundef *
232 : 1938 : retrieve_constexpr_fundef (tree fun)
233 : : {
234 : 1938 : if (constexpr_fundef_table == NULL)
235 : : return NULL;
236 : :
237 : 1938 : rust_constexpr_fundef fundef = {fun, NULL_TREE, NULL_TREE, NULL_TREE};
238 : 1938 : return constexpr_fundef_table->find (&fundef);
239 : : }
240 : :
241 : : /* This internal flag controls whether we should avoid doing anything during
242 : : constexpr evaluation that would cause extra DECL_UID generation, such as
243 : : template instantiation and function body copying. */
244 : :
245 : : static bool uid_sensitive_constexpr_evaluation_value;
246 : :
247 : : /* An internal counter that keeps track of the number of times
248 : : uid_sensitive_constexpr_evaluation_p returned true. */
249 : :
250 : : static unsigned uid_sensitive_constexpr_evaluation_true_counter;
251 : :
252 : : /* The accessor for uid_sensitive_constexpr_evaluation_value which also
253 : : increments the corresponding counter. */
254 : :
255 : : static bool
256 : 0 : uid_sensitive_constexpr_evaluation_p ()
257 : : {
258 : 0 : if (uid_sensitive_constexpr_evaluation_value)
259 : : {
260 : 0 : ++uid_sensitive_constexpr_evaluation_true_counter;
261 : 0 : return true;
262 : : }
263 : : else
264 : : return false;
265 : : }
266 : :
267 : : /* RAII sentinel that saves the value of a variable, optionally
268 : : overrides it right away, and restores its value when the sentinel
269 : : id destructed. */
270 : :
271 : : template <typename T> class temp_override
272 : : {
273 : : T &overridden_variable;
274 : : T saved_value;
275 : :
276 : : public:
277 : 0 : temp_override (T &var) : overridden_variable (var), saved_value (var) {}
278 : 0 : temp_override (T &var, T overrider)
279 : 0 : : overridden_variable (var), saved_value (var)
280 : : {
281 : 0 : overridden_variable = overrider;
282 : : }
283 : 0 : ~temp_override () { overridden_variable = saved_value; }
284 : : };
285 : :
286 : : /* An RAII sentinel used to restrict constexpr evaluation so that it
287 : : doesn't do anything that causes extra DECL_UID generation. */
288 : :
289 : : struct uid_sensitive_constexpr_evaluation_sentinel
290 : : {
291 : : temp_override<bool> ovr;
292 : : uid_sensitive_constexpr_evaluation_sentinel ();
293 : : };
294 : :
295 : : /* Used to determine whether uid_sensitive_constexpr_evaluation_p was
296 : : called and returned true, indicating that we've restricted constexpr
297 : : evaluation in order to avoid UID generation. We use this to control
298 : : updates to the fold_cache and cv_cache. */
299 : :
300 : : struct uid_sensitive_constexpr_evaluation_checker
301 : : {
302 : : const unsigned saved_counter;
303 : : uid_sensitive_constexpr_evaluation_checker ();
304 : : bool evaluation_restricted_p () const;
305 : : };
306 : :
307 : : /* The default constructor for uid_sensitive_constexpr_evaluation_sentinel
308 : : enables the internal flag for uid_sensitive_constexpr_evaluation_p
309 : : during the lifetime of the sentinel object. Upon its destruction, the
310 : : previous value of uid_sensitive_constexpr_evaluation_p is restored. */
311 : :
312 : 0 : uid_sensitive_constexpr_evaluation_sentinel ::
313 : 0 : uid_sensitive_constexpr_evaluation_sentinel ()
314 : 0 : : ovr (uid_sensitive_constexpr_evaluation_value, true)
315 : 0 : {}
316 : :
317 : : /* The default constructor for uid_sensitive_constexpr_evaluation_checker
318 : : records the current number of times that uid_sensitive_constexpr_evaluation_p
319 : : has been called and returned true. */
320 : :
321 : 0 : uid_sensitive_constexpr_evaluation_checker ::
322 : 0 : uid_sensitive_constexpr_evaluation_checker ()
323 : 0 : : saved_counter (uid_sensitive_constexpr_evaluation_true_counter)
324 : 0 : {}
325 : :
326 : : /* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and
327 : : some constexpr evaluation was restricted due to u_s_c_e_p being called
328 : : and returning true during the lifetime of this checker object. */
329 : :
330 : : bool
331 : 0 : uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const
332 : : {
333 : 0 : return (uid_sensitive_constexpr_evaluation_value
334 : 0 : && saved_counter != uid_sensitive_constexpr_evaluation_true_counter);
335 : : }
336 : :
337 : : /* A table of all constexpr calls that have been evaluated by the
338 : : compiler in this translation unit. */
339 : :
340 : : static GTY (()) hash_table<rust_constexpr_call_hasher> *constexpr_call_table;
341 : :
342 : : /* Compute a hash value for a constexpr call representation. */
343 : :
344 : : inline hashval_t
345 : 2712 : rust_constexpr_call_hasher::hash (rust_constexpr_call *info)
346 : : {
347 : 2712 : return info->hash;
348 : : }
349 : :
350 : : /* Return true if the objects pointed to by P and Q represent calls
351 : : to the same constexpr function with the same arguments.
352 : : Otherwise, return false. */
353 : :
354 : : bool
355 : 881 : rust_constexpr_call_hasher::equal (rust_constexpr_call *lhs,
356 : : rust_constexpr_call *rhs)
357 : : {
358 : 881 : if (lhs == rhs)
359 : : return true;
360 : 881 : if (lhs->hash != rhs->hash)
361 : : return false;
362 : 0 : if (lhs->manifestly_const_eval != rhs->manifestly_const_eval)
363 : : return false;
364 : 0 : if (!rust_constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
365 : : return false;
366 : 0 : return rs_tree_equal (lhs->bindings, rhs->bindings);
367 : : }
368 : :
369 : : /* Initialize the constexpr call table, if needed. */
370 : :
371 : : static void
372 : 1938 : maybe_initialize_constexpr_call_table (void)
373 : : {
374 : 1938 : if (constexpr_call_table == NULL)
375 : 517 : constexpr_call_table
376 : 517 : = hash_table<rust_constexpr_call_hasher>::create_ggc (101);
377 : 1938 : }
378 : :
379 : : /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
380 : : a function happens to get called recursively, we unshare the callee
381 : : function's body and evaluate this unshared copy instead of evaluating the
382 : : original body.
383 : :
384 : : FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
385 : : copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
386 : : that's keyed off of the original FUNCTION_DECL and whose value is a
387 : : TREE_LIST of this function's unused copies awaiting reuse.
388 : :
389 : : This is not GC-deletable to avoid GC affecting UID generation. */
390 : :
391 : : static GTY (()) decl_tree_map *fundef_copies_table;
392 : :
393 : : /* Reuse a copy or create a new unshared copy of the function FUN.
394 : : Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
395 : : is parms, TYPE is result. */
396 : :
397 : : static tree
398 : 1938 : get_fundef_copy (rust_constexpr_fundef *fundef)
399 : : {
400 : 1938 : tree copy;
401 : 1938 : bool existed;
402 : 1938 : tree *slot
403 : 1938 : = &(hash_map_safe_get_or_insert<hm_ggc> (fundef_copies_table, fundef->decl,
404 : : &existed, 127));
405 : :
406 : 1938 : if (!existed)
407 : : {
408 : : /* There is no cached function available, or in use. We can use
409 : : the function directly. That the slot is now created records
410 : : that this function is now in use. */
411 : 1937 : copy = build_tree_list (fundef->body, fundef->parms);
412 : 1937 : TREE_TYPE (copy) = fundef->result;
413 : : }
414 : 1 : else if (*slot == NULL_TREE)
415 : : {
416 : 0 : if (uid_sensitive_constexpr_evaluation_p ())
417 : 0 : return NULL_TREE;
418 : :
419 : : /* We've already used the function itself, so make a copy. */
420 : 0 : copy = build_tree_list (NULL, NULL);
421 : 0 : tree saved_body = DECL_SAVED_TREE (fundef->decl);
422 : 0 : tree saved_parms = DECL_ARGUMENTS (fundef->decl);
423 : 0 : tree saved_result = DECL_RESULT (fundef->decl);
424 : 0 : tree saved_fn = current_function_decl;
425 : 0 : DECL_SAVED_TREE (fundef->decl) = fundef->body;
426 : 0 : DECL_ARGUMENTS (fundef->decl) = fundef->parms;
427 : 0 : DECL_RESULT (fundef->decl) = fundef->result;
428 : 0 : current_function_decl = fundef->decl;
429 : 0 : TREE_PURPOSE (copy)
430 : 0 : = copy_fn (fundef->decl, TREE_VALUE (copy), TREE_TYPE (copy));
431 : 0 : current_function_decl = saved_fn;
432 : 0 : DECL_RESULT (fundef->decl) = saved_result;
433 : 0 : DECL_ARGUMENTS (fundef->decl) = saved_parms;
434 : 0 : DECL_SAVED_TREE (fundef->decl) = saved_body;
435 : : }
436 : : else
437 : : {
438 : : /* We have a cached function available. */
439 : 1 : copy = *slot;
440 : 1 : *slot = TREE_CHAIN (copy);
441 : : }
442 : :
443 : : return copy;
444 : : }
445 : :
446 : : /* Save the copy COPY of function FUN for later reuse by
447 : : get_fundef_copy(). By construction, there will always be an entry
448 : : to find. */
449 : :
450 : : static void
451 : 1938 : save_fundef_copy (tree fun, tree copy)
452 : : {
453 : 1938 : tree *slot = fundef_copies_table->get (fun);
454 : 1938 : TREE_CHAIN (copy) = *slot;
455 : 1938 : *slot = copy;
456 : 1938 : }
457 : :
458 : : static tree constant_value_1 (tree decl, bool strict_p,
459 : : bool return_aggregate_cst_ok_p, bool unshare_p);
460 : : tree decl_constant_value (tree decl, bool unshare_p);
461 : :
462 : : static void non_const_var_error (location_t loc, tree r);
463 : :
464 : : static tree eval_constant_expression (const constexpr_ctx *ctx, tree, bool,
465 : : bool *, bool *, tree * = NULL);
466 : :
467 : : static tree constexpr_fn_retval (const constexpr_ctx *ctx, tree r);
468 : :
469 : : static tree eval_store_expression (const constexpr_ctx *ctx, tree r, bool,
470 : : bool *, bool *);
471 : :
472 : : static tree eval_call_expression (const constexpr_ctx *ctx, tree r, bool,
473 : : bool *, bool *);
474 : :
475 : : static tree eval_binary_expression (const constexpr_ctx *ctx, tree r, bool,
476 : : bool *, bool *);
477 : :
478 : : static tree get_function_named_in_call (tree t);
479 : :
480 : : static tree eval_statement_list (const constexpr_ctx *ctx, tree t,
481 : : bool *non_constant_p, bool *overflow_p,
482 : : tree *jump_target);
483 : : static tree extract_string_elt (tree string, unsigned chars_per_elt,
484 : : unsigned index);
485 : :
486 : : static tree eval_conditional_expression (const constexpr_ctx *ctx, tree t,
487 : : bool lval, bool *non_constant_p,
488 : : bool *overflow_p, tree *jump_target);
489 : :
490 : : static tree eval_bit_field_ref (const constexpr_ctx *ctx, tree t, bool lval,
491 : : bool *non_constant_p, bool *overflow_p);
492 : :
493 : : static tree eval_loop_expr (const constexpr_ctx *ctx, tree t,
494 : : bool *non_constant_p, bool *overflow_p,
495 : : tree *jump_target);
496 : :
497 : : static tree eval_switch_expr (const constexpr_ctx *ctx, tree t,
498 : : bool *non_constant_p, bool *overflow_p,
499 : : tree *jump_target);
500 : :
501 : : static tree eval_unary_expression (const constexpr_ctx *ctx, tree t,
502 : : bool /*lval*/, bool *non_constant_p,
503 : : bool *overflow_p);
504 : :
505 : : /* Variables and functions to manage constexpr call expansion context.
506 : : These do not need to be marked for PCH or GC. */
507 : :
508 : : /* FIXME remember and print actual constant arguments. */
509 : : static vec<tree> call_stack;
510 : : static int call_stack_tick;
511 : : static int last_cx_error_tick;
512 : :
513 : : static int
514 : 1938 : push_cx_call_context (tree call)
515 : : {
516 : 1938 : ++call_stack_tick;
517 : 1938 : if (!EXPR_HAS_LOCATION (call))
518 : 257 : SET_EXPR_LOCATION (call, input_location);
519 : 1938 : call_stack.safe_push (call);
520 : 1938 : int len = call_stack.length ();
521 : 1938 : if (len > max_constexpr_depth)
522 : 0 : return false;
523 : : return len;
524 : : }
525 : :
526 : : static void
527 : 1938 : pop_cx_call_context (void)
528 : : {
529 : 1938 : ++call_stack_tick;
530 : 1938 : call_stack.pop ();
531 : 1938 : }
532 : :
533 : : vec<tree>
534 : 0 : cx_error_context (void)
535 : : {
536 : 0 : vec<tree> r = vNULL;
537 : 0 : if (call_stack_tick != last_cx_error_tick && !call_stack.is_empty ())
538 : : r = call_stack;
539 : 0 : last_cx_error_tick = call_stack_tick;
540 : 0 : return r;
541 : : }
542 : :
543 : : // this is ported from cxx_eval_outermost_constant_expr
544 : : tree
545 : 4667 : fold_expr (tree expr)
546 : : {
547 : 4667 : bool allow_non_constant = false;
548 : 4667 : bool strict = true;
549 : 4667 : bool manifestly_const_eval = false;
550 : :
551 : 4667 : constexpr_global_ctx global_ctx;
552 : 4667 : constexpr_ctx ctx
553 : : = {&global_ctx, NULL,
554 : : NULL, NULL,
555 : : NULL, NULL,
556 : : NULL, allow_non_constant,
557 : 4667 : strict, manifestly_const_eval || !allow_non_constant};
558 : :
559 : 4667 : auto_vec<tree, 16> cleanups;
560 : 4667 : global_ctx.cleanups = &cleanups;
561 : :
562 : 4667 : bool non_constant_p = false;
563 : 4667 : bool overflow_p = false;
564 : :
565 : 4667 : tree folded = eval_constant_expression (&ctx, expr, false, &non_constant_p,
566 : : &overflow_p);
567 : 4667 : rust_assert (folded != NULL_TREE);
568 : :
569 : : // more logic here to possibly port
570 : 4667 : return folded;
571 : 4667 : }
572 : :
573 : : static bool
574 : 7 : same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2)
575 : : {
576 : 7 : while (TREE_CODE (type1) == ARRAY_TYPE && TREE_CODE (type2) == ARRAY_TYPE
577 : 7 : && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2)))
578 : : {
579 : 0 : type1 = TREE_TYPE (type1);
580 : 0 : type2 = TREE_TYPE (type2);
581 : : }
582 : 7 : return same_type_ignoring_top_level_qualifiers_p (type1, type2);
583 : : }
584 : :
585 : : // forked from gcc/cp/constexpr.cc cxx_union_active_member
586 : :
587 : : /* Try to determine the currently active union member for an expression
588 : : with UNION_TYPE. If it can be determined, return the FIELD_DECL,
589 : : otherwise return NULL_TREE. */
590 : :
591 : : static tree
592 : 0 : union_active_member (const constexpr_ctx *ctx, tree t)
593 : : {
594 : 0 : constexpr_ctx new_ctx = *ctx;
595 : 0 : new_ctx.quiet = true;
596 : 0 : bool non_constant_p = false, overflow_p = false;
597 : 0 : tree ctor = eval_constant_expression (&new_ctx, t, false, &non_constant_p,
598 : : &overflow_p);
599 : 0 : if (TREE_CODE (ctor) == CONSTRUCTOR && CONSTRUCTOR_NELTS (ctor) == 1
600 : 0 : && CONSTRUCTOR_ELT (ctor, 0)->index
601 : 0 : && TREE_CODE (CONSTRUCTOR_ELT (ctor, 0)->index) == FIELD_DECL)
602 : : return CONSTRUCTOR_ELT (ctor, 0)->index;
603 : : return NULL_TREE;
604 : : }
605 : :
606 : : // forked from gcc/cp/constexpr.cc cxx_fold_indirect_ref_1
607 : :
608 : : static tree
609 : 0 : fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type,
610 : : tree op, unsigned HOST_WIDE_INT off, bool *empty_base)
611 : : {
612 : 0 : tree optype = TREE_TYPE (op);
613 : 0 : unsigned HOST_WIDE_INT const_nunits;
614 : 0 : if (off == 0 && similar_type_p (optype, type))
615 : : return op;
616 : 0 : else if (TREE_CODE (optype) == COMPLEX_TYPE
617 : 0 : && similar_type_p (type, TREE_TYPE (optype)))
618 : : {
619 : : /* *(foo *)&complexfoo => __real__ complexfoo */
620 : 0 : if (off == 0)
621 : 0 : return build1_loc (loc, REALPART_EXPR, type, op);
622 : : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
623 : 0 : else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off)
624 : 0 : return build1_loc (loc, IMAGPART_EXPR, type, op);
625 : : }
626 : : /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
627 : 0 : else if (VECTOR_TYPE_P (optype) && similar_type_p (type, TREE_TYPE (optype))
628 : 0 : && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits))
629 : : {
630 : 0 : unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type));
631 : 0 : unsigned HOST_WIDE_INT max_offset = part_width * const_nunits;
632 : 0 : if (off < max_offset && off % part_width == 0)
633 : : {
634 : 0 : tree index = bitsize_int (off * BITS_PER_UNIT);
635 : 0 : return build3_loc (loc, BIT_FIELD_REF, type, op, TYPE_SIZE (type),
636 : 0 : index);
637 : : }
638 : : }
639 : : /* ((foo *)&fooarray)[x] => fooarray[x] */
640 : 0 : else if (TREE_CODE (optype) == ARRAY_TYPE
641 : 0 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype)))
642 : 0 : && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype))))
643 : : {
644 : 0 : tree type_domain = TYPE_DOMAIN (optype);
645 : 0 : tree min_val = size_zero_node;
646 : 0 : if (type_domain && TYPE_MIN_VALUE (type_domain))
647 : 0 : min_val = TYPE_MIN_VALUE (type_domain);
648 : 0 : unsigned HOST_WIDE_INT el_sz
649 : 0 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype)));
650 : 0 : unsigned HOST_WIDE_INT idx = off / el_sz;
651 : 0 : unsigned HOST_WIDE_INT rem = off % el_sz;
652 : 0 : if (tree_fits_uhwi_p (min_val))
653 : : {
654 : 0 : tree index = size_int (idx + tree_to_uhwi (min_val));
655 : 0 : op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index,
656 : : NULL_TREE, NULL_TREE);
657 : 0 : return fold_indirect_ref_1 (ctx, loc, type, op, rem, empty_base);
658 : : }
659 : : }
660 : : /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
661 : 0 : else if (TREE_CODE (optype) == RECORD_TYPE
662 : 0 : || TREE_CODE (optype) == UNION_TYPE)
663 : : {
664 : 0 : if (TREE_CODE (optype) == UNION_TYPE)
665 : : /* For unions prefer the currently active member. */
666 : 0 : if (tree field = union_active_member (ctx, op))
667 : : {
668 : 0 : unsigned HOST_WIDE_INT el_sz
669 : 0 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
670 : 0 : if (off < el_sz)
671 : : {
672 : 0 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field), op, field,
673 : : NULL_TREE);
674 : 0 : if (tree ret = fold_indirect_ref_1 (ctx, loc, type, cop, off,
675 : : empty_base))
676 : : return ret;
677 : : }
678 : : }
679 : 0 : for (tree field = TYPE_FIELDS (optype); field; field = DECL_CHAIN (field))
680 : 0 : if (TREE_CODE (field) == FIELD_DECL
681 : 0 : && TREE_TYPE (field) != error_mark_node
682 : 0 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field))))
683 : : {
684 : 0 : tree pos = byte_position (field);
685 : 0 : if (!tree_fits_uhwi_p (pos))
686 : 0 : continue;
687 : 0 : unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos);
688 : 0 : unsigned HOST_WIDE_INT el_sz
689 : 0 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
690 : 0 : if (upos <= off && off < upos + el_sz)
691 : : {
692 : 0 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field), op, field,
693 : : NULL_TREE);
694 : 0 : if (tree ret = fold_indirect_ref_1 (ctx, loc, type, cop,
695 : : off - upos, empty_base))
696 : : return ret;
697 : : }
698 : : }
699 : : /* Also handle conversion to an empty base class, which
700 : : is represented with a NOP_EXPR. */
701 : 0 : if (is_empty_class (type) && CLASS_TYPE_P (optype))
702 : : {
703 : 0 : *empty_base = true;
704 : 0 : return op;
705 : : }
706 : : }
707 : :
708 : : return NULL_TREE;
709 : : }
710 : :
711 : : // forked from gcc/cp/constexpr.cc cxx_fold_indirect_ref
712 : :
713 : : /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
714 : : match. We want to be less strict for simple *& folding; if we have a
715 : : non-const temporary that we access through a const pointer, that should
716 : : work. We handle this here rather than change fold_indirect_ref_1
717 : : because we're dealing with things like ADDR_EXPR of INTEGER_CST which
718 : : don't really make sense outside of constant expression evaluation. Also
719 : : we want to allow folding to COMPONENT_REF, which could cause trouble
720 : : with TBAA in fold_indirect_ref_1. */
721 : :
722 : : static tree
723 : 0 : rs_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type,
724 : : tree op0, bool *empty_base)
725 : : {
726 : 0 : tree sub = op0;
727 : 0 : tree subtype;
728 : :
729 : : /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
730 : 0 : while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR
731 : 0 : || TREE_CODE (sub) == VIEW_CONVERT_EXPR)
732 : : {
733 : 0 : if (TREE_CODE (sub) == NOP_EXPR && REINTERPRET_CAST_P (sub))
734 : : return NULL_TREE;
735 : 0 : sub = TREE_OPERAND (sub, 0);
736 : : }
737 : :
738 : 0 : subtype = TREE_TYPE (sub);
739 : 0 : if (!INDIRECT_TYPE_P (subtype))
740 : : return NULL_TREE;
741 : :
742 : : /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing
743 : : the innermost component into the offset until it would make the
744 : : offset positive, so that cxx_fold_indirect_ref_1 can identify
745 : : more folding opportunities. */
746 : 0 : auto canonicalize_obj_off = [] (tree &obj, tree &off) {
747 : 0 : while (TREE_CODE (obj) == COMPONENT_REF
748 : 0 : && (tree_int_cst_sign_bit (off) || integer_zerop (off)))
749 : : {
750 : 0 : tree field = TREE_OPERAND (obj, 1);
751 : 0 : tree pos = byte_position (field);
752 : 0 : if (integer_zerop (off) && integer_nonzerop (pos))
753 : : /* If the offset is already 0, keep going as long as the
754 : : component is at position 0. */
755 : : break;
756 : 0 : off = int_const_binop (PLUS_EXPR, off, pos);
757 : 0 : obj = TREE_OPERAND (obj, 0);
758 : : }
759 : 0 : };
760 : :
761 : 0 : if (TREE_CODE (sub) == ADDR_EXPR)
762 : : {
763 : 0 : tree op = TREE_OPERAND (sub, 0);
764 : 0 : tree optype = TREE_TYPE (op);
765 : :
766 : : /* *&CONST_DECL -> to the value of the const decl. */
767 : 0 : if (TREE_CODE (op) == CONST_DECL)
768 : 0 : return DECL_INITIAL (op);
769 : : /* *&p => p; make sure to handle *&"str"[cst] here. */
770 : 0 : if (similar_type_p (optype, type))
771 : : {
772 : 0 : tree fop = fold_read_from_constant_string (op);
773 : 0 : if (fop)
774 : : return fop;
775 : : else
776 : 0 : return op;
777 : : }
778 : : else
779 : : {
780 : 0 : tree off = integer_zero_node;
781 : 0 : canonicalize_obj_off (op, off);
782 : 0 : gcc_assert (integer_zerop (off));
783 : 0 : return fold_indirect_ref_1 (ctx, loc, type, op, 0, empty_base);
784 : : }
785 : : }
786 : 0 : else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
787 : 0 : && tree_fits_uhwi_p (TREE_OPERAND (sub, 1)))
788 : : {
789 : 0 : tree op00 = TREE_OPERAND (sub, 0);
790 : 0 : tree off = TREE_OPERAND (sub, 1);
791 : :
792 : 0 : STRIP_NOPS (op00);
793 : 0 : if (TREE_CODE (op00) == ADDR_EXPR)
794 : : {
795 : 0 : tree obj = TREE_OPERAND (op00, 0);
796 : 0 : canonicalize_obj_off (obj, off);
797 : 0 : return fold_indirect_ref_1 (ctx, loc, type, obj, tree_to_uhwi (off),
798 : : empty_base);
799 : : }
800 : : }
801 : : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
802 : 0 : else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
803 : 0 : && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype))))
804 : : {
805 : 0 : tree type_domain;
806 : 0 : tree min_val = size_zero_node;
807 : 0 : tree newsub
808 : 0 : = rs_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL);
809 : 0 : if (newsub)
810 : : sub = newsub;
811 : : else
812 : 0 : sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
813 : 0 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
814 : 0 : if (type_domain && TYPE_MIN_VALUE (type_domain))
815 : 0 : min_val = TYPE_MIN_VALUE (type_domain);
816 : 0 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
817 : 0 : NULL_TREE);
818 : : }
819 : :
820 : : return NULL_TREE;
821 : : }
822 : :
823 : : // forked from gcc/cp/constexpr.cc cxx_eval_indirect_ref
824 : :
825 : : static tree
826 : 0 : rs_eval_indirect_ref (const constexpr_ctx *ctx, tree t, bool lval,
827 : : bool *non_constant_p, bool *overflow_p)
828 : : {
829 : 0 : tree orig_op0 = TREE_OPERAND (t, 0);
830 : 0 : bool empty_base = false;
831 : :
832 : : /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
833 : : operand is an integer-zero. Otherwise reject the MEM_REF for now. */
834 : :
835 : 0 : if (TREE_CODE (t) == MEM_REF
836 : 0 : && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1))))
837 : : {
838 : 0 : gcc_assert (ctx->quiet);
839 : 0 : *non_constant_p = true;
840 : 0 : return t;
841 : : }
842 : :
843 : : /* First try to simplify it directly. */
844 : 0 : tree r = rs_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
845 : : orig_op0, &empty_base);
846 : 0 : if (!r)
847 : : {
848 : : /* If that didn't work, evaluate the operand first. */
849 : 0 : tree op0
850 : 0 : = eval_constant_expression (ctx, orig_op0,
851 : : /*lval*/ false, non_constant_p, overflow_p);
852 : : /* Don't VERIFY_CONSTANT here. */
853 : 0 : if (*non_constant_p)
854 : : return t;
855 : :
856 : 0 : if (!lval && integer_zerop (op0))
857 : : {
858 : 0 : if (!ctx->quiet)
859 : 0 : error ("dereferencing a null pointer");
860 : 0 : *non_constant_p = true;
861 : 0 : return t;
862 : : }
863 : :
864 : 0 : r = rs_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0,
865 : : &empty_base);
866 : 0 : if (r == NULL_TREE)
867 : : {
868 : : /* We couldn't fold to a constant value. Make sure it's not
869 : : something we should have been able to fold. */
870 : 0 : tree sub = op0;
871 : 0 : STRIP_NOPS (sub);
872 : 0 : if (TREE_CODE (sub) == ADDR_EXPR)
873 : : {
874 : 0 : gcc_assert (
875 : : !similar_type_p (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
876 : : /* DR 1188 says we don't have to deal with this. */
877 : 0 : if (!ctx->quiet)
878 : 0 : error_at (rs_expr_loc_or_input_loc (t),
879 : : "accessing value of %qE through a %qT glvalue in a "
880 : : "constant expression",
881 : 0 : build_fold_indirect_ref (sub), TREE_TYPE (t));
882 : 0 : *non_constant_p = true;
883 : 0 : return t;
884 : : }
885 : :
886 : 0 : if (lval && op0 != orig_op0)
887 : 0 : return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
888 : 0 : if (!lval)
889 : 0 : VERIFY_CONSTANT (t);
890 : 0 : return t;
891 : : }
892 : : }
893 : :
894 : 0 : r = eval_constant_expression (ctx, r, lval, non_constant_p, overflow_p);
895 : 0 : if (*non_constant_p)
896 : : return t;
897 : :
898 : : /* If we're pulling out the value of an empty base, just return an empty
899 : : CONSTRUCTOR. */
900 : 0 : if (empty_base && !lval)
901 : : {
902 : 0 : r = build_constructor (TREE_TYPE (t), NULL);
903 : 0 : TREE_CONSTANT (r) = true;
904 : : }
905 : :
906 : : return r;
907 : : }
908 : :
909 : : // forked from gcc/cp/constexpr.cc cxx_eval_logical_expression
910 : :
911 : : /* Subroutine of cxx_eval_constant_expression.
912 : : Evaluate a short-circuited logical expression T in the context
913 : : of a given constexpr CALL. BAILOUT_VALUE is the value for
914 : : early return. CONTINUE_VALUE is used here purely for
915 : : sanity check purposes. */
916 : :
917 : : static tree
918 : 0 : eval_logical_expression (const constexpr_ctx *ctx, tree t, tree bailout_value,
919 : : tree continue_value, bool lval, bool *non_constant_p,
920 : : bool *overflow_p)
921 : : {
922 : 0 : tree r;
923 : 0 : tree lhs = eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
924 : : non_constant_p, overflow_p);
925 : 0 : VERIFY_CONSTANT (lhs);
926 : 0 : if (tree_int_cst_equal (lhs, bailout_value))
927 : : return lhs;
928 : 0 : gcc_assert (tree_int_cst_equal (lhs, continue_value));
929 : 0 : r = eval_constant_expression (ctx, TREE_OPERAND (t, 1), lval, non_constant_p,
930 : : overflow_p);
931 : 0 : VERIFY_CONSTANT (r);
932 : : return r;
933 : : }
934 : :
935 : : // forked from gcc/cp/constexp.rcc lookup_placeholder
936 : :
937 : : /* Find the object of TYPE under initialization in CTX. */
938 : :
939 : : static tree
940 : 0 : lookup_placeholder (const constexpr_ctx *ctx, bool lval, tree type)
941 : : {
942 : 0 : if (!ctx)
943 : : return NULL_TREE;
944 : :
945 : : /* Prefer the outermost matching object, but don't cross
946 : : CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
947 : 0 : if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor))
948 : 0 : if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type))
949 : : return outer_ob;
950 : :
951 : : /* We could use ctx->object unconditionally, but using ctx->ctor when we
952 : : can is a minor optimization. */
953 : 0 : if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type))
954 : 0 : return ctx->ctor;
955 : :
956 : 0 : if (!ctx->object)
957 : : return NULL_TREE;
958 : :
959 : : /* Since an object cannot have a field of its own type, we can search outward
960 : : from ctx->object to find the unique containing object of TYPE. */
961 : : tree ob = ctx->object;
962 : 0 : while (ob)
963 : : {
964 : 0 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type))
965 : : break;
966 : 0 : if (handled_component_p (ob))
967 : 0 : ob = TREE_OPERAND (ob, 0);
968 : : else
969 : : ob = NULL_TREE;
970 : : }
971 : :
972 : : return ob;
973 : : }
974 : :
975 : : // forked from gcc/cp/constexp.rcc inline_asm_in_constexpr_error
976 : :
977 : : /* Complain about an attempt to evaluate inline assembly. */
978 : :
979 : : static void
980 : 0 : inline_asm_in_constexpr_error (location_t loc)
981 : : {
982 : 0 : auto_diagnostic_group d;
983 : 0 : error_at (loc, "inline assembly is not a constant expression");
984 : 0 : inform (loc, "only unevaluated inline assembly is allowed in a "
985 : : "%<constexpr%> function in C++20");
986 : 0 : }
987 : :
988 : : // forked from gcc/cp/constexpr.cc verify_ctor_sanity
989 : :
990 : : /* We're about to process an initializer for a class or array TYPE. Make
991 : : sure that CTX is set up appropriately. */
992 : :
993 : : static void
994 : 0 : verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
995 : : {
996 : : /* We don't bother building a ctor for an empty base subobject. */
997 : 0 : if (is_empty_class (type))
998 : : return;
999 : :
1000 : : /* We're in the middle of an initializer that might involve placeholders;
1001 : : our caller should have created a CONSTRUCTOR for us to put the
1002 : : initializer into. We will either return that constructor or T. */
1003 : 0 : gcc_assert (ctx->ctor);
1004 : 0 : gcc_assert (
1005 : : same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (ctx->ctor)));
1006 : : /* We used to check that ctx->ctor was empty, but that isn't the case when
1007 : : the object is zero-initialized before calling the constructor. */
1008 : 0 : if (ctx->object)
1009 : : {
1010 : 0 : tree otype = TREE_TYPE (ctx->object);
1011 : 0 : gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype)
1012 : : /* Handle flexible array members. */
1013 : : || (TREE_CODE (otype) == ARRAY_TYPE
1014 : : && TYPE_DOMAIN (otype) == NULL_TREE
1015 : : && TREE_CODE (type) == ARRAY_TYPE
1016 : : && (same_type_ignoring_top_level_qualifiers_p (
1017 : : TREE_TYPE (type), TREE_TYPE (otype)))));
1018 : : }
1019 : 0 : gcc_assert (!ctx->object || !DECL_P (ctx->object)
1020 : : || *(ctx->global->values.get (ctx->object)) == ctx->ctor);
1021 : : }
1022 : :
1023 : : // forked from gcc/cp/constexpr.cc array_index_cmp
1024 : :
1025 : : /* Some of the expressions fed to the constexpr mechanism are calls to
1026 : : constructors, which have type void. In that case, return the type being
1027 : : initialized by the constructor. */
1028 : :
1029 : : static tree
1030 : 0 : initialized_type (tree t)
1031 : : {
1032 : 0 : if (TYPE_P (t))
1033 : : return t;
1034 : 0 : tree type = TREE_TYPE (t);
1035 : 0 : if (TREE_CODE (t) == CALL_EXPR)
1036 : : {
1037 : : /* A constructor call has void type, so we need to look deeper. */
1038 : 0 : tree fn = get_function_named_in_call (t);
1039 : 0 : if (fn && TREE_CODE (fn) == FUNCTION_DECL && DECL_CXX_CONSTRUCTOR_P (fn))
1040 : 0 : type = DECL_CONTEXT (fn);
1041 : : }
1042 : 0 : else if (TREE_CODE (t) == COMPOUND_EXPR)
1043 : 0 : return initialized_type (TREE_OPERAND (t, 1));
1044 : :
1045 : 0 : return cv_unqualified (type);
1046 : : }
1047 : :
1048 : : // forked from gcc/cp/constexpr.cc init_subob_ctx
1049 : :
1050 : : /* We're about to initialize element INDEX of an array or class from VALUE.
1051 : : Set up NEW_CTX appropriately by adjusting .object to refer to the
1052 : : subobject and creating a new CONSTRUCTOR if the element is itself
1053 : : a class or array. */
1054 : :
1055 : : static void
1056 : 0 : init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx, tree index,
1057 : : tree &value)
1058 : : {
1059 : 0 : new_ctx = *ctx;
1060 : :
1061 : 0 : if (index && TREE_CODE (index) != INTEGER_CST
1062 : 0 : && TREE_CODE (index) != FIELD_DECL && TREE_CODE (index) != RANGE_EXPR)
1063 : : /* This won't have an element in the new CONSTRUCTOR. */
1064 : : return;
1065 : :
1066 : 0 : tree type = initialized_type (value);
1067 : 0 : if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
1068 : : /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
1069 : : return;
1070 : :
1071 : : /* The sub-aggregate initializer might contain a placeholder;
1072 : : update object to refer to the subobject and ctor to refer to
1073 : : the (newly created) sub-initializer. */
1074 : 0 : if (ctx->object)
1075 : : {
1076 : 0 : if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR)
1077 : : /* There's no well-defined subobject for this index. */
1078 : 0 : new_ctx.object = NULL_TREE;
1079 : : else
1080 : : {
1081 : : // Faisal: commenting this out as not sure if it's needed and it's
1082 : : // huge new_ctx.object = build_ctor_subob_ref (index, type,
1083 : : // ctx->object);
1084 : : }
1085 : : }
1086 : 0 : tree elt = build_constructor (type, NULL);
1087 : 0 : CONSTRUCTOR_NO_CLEARING (elt) = true;
1088 : 0 : new_ctx.ctor = elt;
1089 : :
1090 : 0 : if (TREE_CODE (value) == TARGET_EXPR)
1091 : : /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
1092 : 0 : value = TARGET_EXPR_INITIAL (value);
1093 : : }
1094 : :
1095 : : // forked from gcc/cp/constexpr.cc base_field_constructor_elt
1096 : :
1097 : : /* REF is a COMPONENT_REF designating a particular field. V is a vector of
1098 : : CONSTRUCTOR elements to initialize (part of) an object containing that
1099 : : field. Return a pointer to the constructor_elt corresponding to the
1100 : : initialization of the field. */
1101 : :
1102 : : static constructor_elt *
1103 : 0 : base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
1104 : : {
1105 : 0 : tree aggr = TREE_OPERAND (ref, 0);
1106 : 0 : tree field = TREE_OPERAND (ref, 1);
1107 : 0 : HOST_WIDE_INT i;
1108 : 0 : constructor_elt *ce;
1109 : :
1110 : 0 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
1111 : :
1112 : 0 : if (TREE_CODE (aggr) == COMPONENT_REF)
1113 : : {
1114 : 0 : constructor_elt *base_ce = base_field_constructor_elt (v, aggr);
1115 : 0 : v = CONSTRUCTOR_ELTS (base_ce->value);
1116 : : }
1117 : :
1118 : 0 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
1119 : 0 : if (ce->index == field)
1120 : 0 : return ce;
1121 : :
1122 : 0 : rust_unreachable ();
1123 : : return NULL;
1124 : : }
1125 : :
1126 : : /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
1127 : : matching constructor_elt exists, then add one to CTOR.
1128 : :
1129 : : As an optimization, if POS_HINT is non-negative then it is used as a guess
1130 : : for the (integer) index of the matching constructor_elt within CTOR. */
1131 : :
1132 : : static constructor_elt *
1133 : 2 : get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1)
1134 : : {
1135 : : /* Check the hint first. */
1136 : 0 : if (pos_hint >= 0 && (unsigned) pos_hint < CONSTRUCTOR_NELTS (ctor)
1137 : 2 : && CONSTRUCTOR_ELT (ctor, pos_hint)->index == index)
1138 : : return CONSTRUCTOR_ELT (ctor, pos_hint);
1139 : :
1140 : 2 : tree type = TREE_TYPE (ctor);
1141 : 2 : if (TREE_CODE (type) == VECTOR_TYPE && index == NULL_TREE)
1142 : : {
1143 : 0 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), index, NULL_TREE);
1144 : 0 : return &CONSTRUCTOR_ELTS (ctor)->last ();
1145 : : }
1146 : 2 : else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
1147 : : {
1148 : 1 : if (TREE_CODE (index) == RANGE_EXPR)
1149 : : {
1150 : : /* Support for RANGE_EXPR index lookups is currently limited to
1151 : : accessing an existing element via POS_HINT, or appending a new
1152 : : element to the end of CTOR. ??? Support for other access
1153 : : patterns may also be needed. */
1154 : 0 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
1155 : 0 : if (vec_safe_length (elts))
1156 : : {
1157 : 0 : tree lo = TREE_OPERAND (index, 0);
1158 : 0 : gcc_assert (array_index_cmp (elts->last ().index, lo) < 0);
1159 : : }
1160 : 0 : CONSTRUCTOR_APPEND_ELT (elts, index, NULL_TREE);
1161 : 0 : return &elts->last ();
1162 : : }
1163 : :
1164 : 1 : HOST_WIDE_INT i = find_array_ctor_elt (ctor, index, /*insert*/ true);
1165 : 1 : gcc_assert (i >= 0);
1166 : 1 : constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i);
1167 : 1 : gcc_assert (cep->index == NULL_TREE
1168 : : || TREE_CODE (cep->index) != RANGE_EXPR);
1169 : : return cep;
1170 : : }
1171 : : else
1172 : : {
1173 : 1 : gcc_assert (
1174 : : TREE_CODE (index) == FIELD_DECL
1175 : : && (same_type_ignoring_top_level_qualifiers_p (DECL_CONTEXT (index),
1176 : : TREE_TYPE (ctor))));
1177 : :
1178 : : /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
1179 : : Usually we meet initializers in that order, but it is
1180 : : possible for base types to be placed not in program
1181 : : order. */
1182 : 1 : tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
1183 : 1 : unsigned HOST_WIDE_INT idx = 0;
1184 : 1 : constructor_elt *cep = NULL;
1185 : :
1186 : : /* Check if we're changing the active member of a union. */
1187 : 0 : if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor)
1188 : 1 : && CONSTRUCTOR_ELT (ctor, 0)->index != index)
1189 : 0 : vec_safe_truncate (CONSTRUCTOR_ELTS (ctor), 0);
1190 : : /* If the bit offset of INDEX is larger than that of the last
1191 : : constructor_elt, then we can just immediately append a new
1192 : : constructor_elt to the end of CTOR. */
1193 : 1 : else if (CONSTRUCTOR_NELTS (ctor)
1194 : 2 : && tree_int_cst_compare (
1195 : 1 : bit_position (index),
1196 : 1 : bit_position (CONSTRUCTOR_ELTS (ctor)->last ().index))
1197 : : > 0)
1198 : : {
1199 : 0 : idx = CONSTRUCTOR_NELTS (ctor);
1200 : 0 : goto insert;
1201 : : }
1202 : :
1203 : : /* Otherwise, we need to iterate over CTOR to find or insert INDEX
1204 : : appropriately. */
1205 : :
1206 : 1 : for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), idx, &cep);
1207 : 0 : idx++, fields = DECL_CHAIN (fields))
1208 : : {
1209 : 1 : if (index == cep->index)
1210 : 1 : goto found;
1211 : :
1212 : : /* The field we're initializing must be on the field
1213 : : list. Look to see if it is present before the
1214 : : field the current ELT initializes. */
1215 : 0 : for (; fields != cep->index; fields = DECL_CHAIN (fields))
1216 : 0 : if (index == fields)
1217 : 0 : goto insert;
1218 : : }
1219 : : /* We fell off the end of the CONSTRUCTOR, so insert a new
1220 : : entry at the end. */
1221 : :
1222 : 0 : insert:
1223 : 0 : {
1224 : 0 : constructor_elt ce = {index, NULL_TREE};
1225 : :
1226 : 0 : vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce);
1227 : 0 : cep = CONSTRUCTOR_ELT (ctor, idx);
1228 : : }
1229 : 1 : found:;
1230 : :
1231 : 1 : return cep;
1232 : : }
1233 : : }
1234 : :
1235 : : // forked from gcc/cp/constexpr.cc cxx_eval_vector_conditional_expression
1236 : :
1237 : : /* Subroutine of cxx_eval_constant_expression.
1238 : : Attempt to evaluate vector condition expressions. Unlike
1239 : : cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
1240 : : ternary arithmetics operation, where all 3 arguments have to be
1241 : : evaluated as constants and then folding computes the result from
1242 : : them. */
1243 : :
1244 : : static tree
1245 : 0 : eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t,
1246 : : bool *non_constant_p, bool *overflow_p)
1247 : : {
1248 : 0 : tree arg1
1249 : 0 : = eval_constant_expression (ctx, TREE_OPERAND (t, 0),
1250 : : /*lval*/ false, non_constant_p, overflow_p);
1251 : 0 : VERIFY_CONSTANT (arg1);
1252 : 0 : tree arg2
1253 : 0 : = eval_constant_expression (ctx, TREE_OPERAND (t, 1),
1254 : : /*lval*/ false, non_constant_p, overflow_p);
1255 : 0 : VERIFY_CONSTANT (arg2);
1256 : 0 : tree arg3
1257 : 0 : = eval_constant_expression (ctx, TREE_OPERAND (t, 2),
1258 : : /*lval*/ false, non_constant_p, overflow_p);
1259 : 0 : VERIFY_CONSTANT (arg3);
1260 : 0 : location_t loc = EXPR_LOCATION (t);
1261 : 0 : tree type = TREE_TYPE (t);
1262 : 0 : tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
1263 : 0 : if (r == NULL_TREE)
1264 : : {
1265 : 0 : if (arg1 == TREE_OPERAND (t, 0) && arg2 == TREE_OPERAND (t, 1)
1266 : 0 : && arg3 == TREE_OPERAND (t, 2))
1267 : : r = t;
1268 : : else
1269 : 0 : r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
1270 : : }
1271 : 0 : VERIFY_CONSTANT (r);
1272 : : return r;
1273 : : }
1274 : :
1275 : : // forked from gcc/cp/constexpr.cc cxx_eval_bare_aggregate
1276 : :
1277 : : /* Subroutine of cxx_eval_constant_expression.
1278 : : The expression tree T denotes a C-style array or a C-style
1279 : : aggregate. Reduce it to a constant expression. */
1280 : :
1281 : : static tree
1282 : 0 : eval_bare_aggregate (const constexpr_ctx *ctx, tree t, bool lval,
1283 : : bool *non_constant_p, bool *overflow_p)
1284 : : {
1285 : 0 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
1286 : 0 : bool changed = false;
1287 : 0 : gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
1288 : 0 : tree type = TREE_TYPE (t);
1289 : :
1290 : 0 : constexpr_ctx new_ctx;
1291 : 0 : if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type))
1292 : : {
1293 : : /* We don't really need the ctx->ctor business for a PMF or
1294 : : vector, but it's simpler to use the same code. */
1295 : 0 : new_ctx = *ctx;
1296 : 0 : new_ctx.ctor = build_constructor (type, NULL);
1297 : 0 : new_ctx.object = NULL_TREE;
1298 : 0 : ctx = &new_ctx;
1299 : 0 : };
1300 : 0 : verify_ctor_sanity (ctx, type);
1301 : 0 : vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
1302 : 0 : vec_alloc (*p, vec_safe_length (v));
1303 : :
1304 : 0 : if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t))
1305 : 0 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1;
1306 : :
1307 : : unsigned i;
1308 : : tree index, value;
1309 : : bool constant_p = true;
1310 : : bool side_effects_p = false;
1311 : 0 : FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
1312 : : {
1313 : 0 : tree orig_value = value;
1314 : : /* Like in cxx_eval_store_expression, omit entries for empty fields. */
1315 : 0 : bool no_slot = TREE_CODE (type) == RECORD_TYPE && is_empty_field (index);
1316 : 0 : if (no_slot)
1317 : 0 : new_ctx = *ctx;
1318 : : else
1319 : 0 : init_subob_ctx (ctx, new_ctx, index, value);
1320 : 0 : int pos_hint = -1;
1321 : 0 : if (new_ctx.ctor != ctx->ctor)
1322 : : {
1323 : : /* If we built a new CONSTRUCTOR, attach it now so that other
1324 : : initializers can refer to it. */
1325 : 0 : constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index);
1326 : 0 : cep->value = new_ctx.ctor;
1327 : 0 : pos_hint = cep - (*p)->begin ();
1328 : : }
1329 : 0 : else if (TREE_CODE (type) == UNION_TYPE)
1330 : : /* Otherwise if we're constructing a non-aggregate union member, set
1331 : : the active union member now so that we can later detect and diagnose
1332 : : if its initializer attempts to activate another member. */
1333 : 0 : get_or_insert_ctor_field (ctx->ctor, index);
1334 : 0 : tree elt = eval_constant_expression (&new_ctx, value, lval,
1335 : : non_constant_p, overflow_p);
1336 : : /* Don't VERIFY_CONSTANT here. */
1337 : 0 : if (ctx->quiet && *non_constant_p)
1338 : : break;
1339 : 0 : if (elt != orig_value)
1340 : 0 : changed = true;
1341 : :
1342 : 0 : if (!TREE_CONSTANT (elt))
1343 : 0 : constant_p = false;
1344 : 0 : if (TREE_SIDE_EFFECTS (elt))
1345 : 0 : side_effects_p = true;
1346 : 0 : if (index && TREE_CODE (index) == COMPONENT_REF)
1347 : : {
1348 : : /* This is an initialization of a vfield inside a base
1349 : : subaggregate that we already initialized; push this
1350 : : initialization into the previous initialization. */
1351 : 0 : constructor_elt *inner = base_field_constructor_elt (*p, index);
1352 : 0 : inner->value = elt;
1353 : 0 : changed = true;
1354 : 0 : }
1355 : 0 : else if (index
1356 : 0 : && (TREE_CODE (index) == NOP_EXPR
1357 : 0 : || TREE_CODE (index) == POINTER_PLUS_EXPR))
1358 : : {
1359 : : /* This is an initializer for an empty base; now that we've
1360 : : checked that it's constant, we can ignore it. */
1361 : 0 : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
1362 : : changed = true;
1363 : : }
1364 : 0 : else if (no_slot)
1365 : : changed = true;
1366 : : else
1367 : : {
1368 : 0 : if (TREE_CODE (type) == UNION_TYPE && (*p)->last ().index != index)
1369 : : /* The initializer erroneously changed the active union member that
1370 : : we're initializing. */
1371 : 0 : gcc_assert (*non_constant_p);
1372 : : else
1373 : : {
1374 : : /* The initializer might have mutated the underlying CONSTRUCTOR,
1375 : : so recompute the location of the target constructer_elt. */
1376 : 0 : constructor_elt *cep
1377 : 0 : = get_or_insert_ctor_field (ctx->ctor, index, pos_hint);
1378 : 0 : cep->value = elt;
1379 : : }
1380 : :
1381 : : /* Adding or replacing an element might change the ctor's flags. */
1382 : 0 : TREE_CONSTANT (ctx->ctor) = constant_p;
1383 : 0 : TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
1384 : : }
1385 : : }
1386 : 0 : if (*non_constant_p || !changed)
1387 : : return t;
1388 : 0 : t = ctx->ctor;
1389 : : /* We're done building this CONSTRUCTOR, so now we can interpret an
1390 : : element without an explicit initializer as value-initialized. */
1391 : 0 : CONSTRUCTOR_NO_CLEARING (t) = false;
1392 : 0 : TREE_CONSTANT (t) = constant_p;
1393 : 0 : TREE_SIDE_EFFECTS (t) = side_effects_p;
1394 : 0 : if (VECTOR_TYPE_P (type))
1395 : 0 : t = fold (t);
1396 : : return t;
1397 : : }
1398 : :
1399 : : /* Subroutine of cxx_eval_constant_expression.
1400 : : Like cxx_eval_unary_expression, except for trinary expressions. */
1401 : :
1402 : : static tree
1403 : 0 : cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t, bool lval,
1404 : : bool *non_constant_p, bool *overflow_p)
1405 : : {
1406 : 0 : int i;
1407 : 0 : tree args[3];
1408 : 0 : tree val;
1409 : :
1410 : 0 : for (i = 0; i < 3; i++)
1411 : : {
1412 : 0 : args[i] = eval_constant_expression (ctx, TREE_OPERAND (t, i), lval,
1413 : : non_constant_p, overflow_p);
1414 : 0 : VERIFY_CONSTANT (args[i]);
1415 : : }
1416 : :
1417 : 0 : val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
1418 : : args[0], args[1], args[2]);
1419 : 0 : if (val == NULL_TREE)
1420 : : return t;
1421 : 0 : VERIFY_CONSTANT (val);
1422 : : return val;
1423 : : }
1424 : :
1425 : : /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
1426 : : initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
1427 : : cleared.
1428 : : FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
1429 : :
1430 : : bool
1431 : 484 : reduced_constant_expression_p (tree t)
1432 : : {
1433 : 484 : if (t == NULL_TREE)
1434 : : return false;
1435 : :
1436 : 484 : switch (TREE_CODE (t))
1437 : : {
1438 : : case PTRMEM_CST:
1439 : : /* Even if we can't lower this yet, it's constant. */
1440 : : return true;
1441 : :
1442 : 63 : case CONSTRUCTOR:
1443 : : /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
1444 : 63 : tree field;
1445 : 63 : if (CONSTRUCTOR_NO_CLEARING (t))
1446 : : {
1447 : 0 : if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
1448 : : /* An initialized vector would have a VECTOR_CST. */
1449 : : return false;
1450 : 0 : else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
1451 : : {
1452 : : /* There must be a valid constant initializer at every array
1453 : : index. */
1454 : 0 : tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
1455 : 0 : tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
1456 : 0 : tree cursor = min;
1457 : 0 : for (auto &e : CONSTRUCTOR_ELTS (t))
1458 : : {
1459 : 0 : if (!reduced_constant_expression_p (e.value))
1460 : : return false;
1461 : 0 : if (array_index_cmp (cursor, e.index) != 0)
1462 : : return false;
1463 : 0 : if (TREE_CODE (e.index) == RANGE_EXPR)
1464 : 0 : cursor = TREE_OPERAND (e.index, 1);
1465 : 0 : cursor = int_const_binop (PLUS_EXPR, cursor, size_one_node);
1466 : : }
1467 : 0 : if (find_array_ctor_elt (t, max) == -1)
1468 : : return false;
1469 : 0 : goto ok;
1470 : : }
1471 : 0 : else if (TREE_CODE (TREE_TYPE (t)) == UNION_TYPE)
1472 : : {
1473 : 0 : if (CONSTRUCTOR_NELTS (t) == 0)
1474 : : /* An initialized union has a constructor element. */
1475 : : return false;
1476 : : /* And it only initializes one member. */
1477 : : field = NULL_TREE;
1478 : : }
1479 : : else
1480 : 0 : field = next_initializable_field (TYPE_FIELDS (TREE_TYPE (t)));
1481 : : }
1482 : : else
1483 : : field = NULL_TREE;
1484 : 550 : for (auto &e : CONSTRUCTOR_ELTS (t))
1485 : : {
1486 : : /* If VAL is null, we're in the middle of initializing this
1487 : : element. */
1488 : 371 : if (!reduced_constant_expression_p (e.value))
1489 : : return false;
1490 : : /* Empty class field may or may not have an initializer. */
1491 : 371 : for (; field && e.index != field;
1492 : 0 : field = next_initializable_field (DECL_CHAIN (field)))
1493 : 0 : if (!is_really_empty_class (TREE_TYPE (field),
1494 : : /*ignore_vptr*/ false))
1495 : : return false;
1496 : 371 : if (field)
1497 : 0 : field = next_initializable_field (DECL_CHAIN (field));
1498 : : }
1499 : : /* There could be a non-empty field at the end. */
1500 : 63 : for (; field; field = next_initializable_field (DECL_CHAIN (field)))
1501 : 0 : if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/ false))
1502 : : return false;
1503 : 63 : ok:
1504 : 63 : if (CONSTRUCTOR_NO_CLEARING (t))
1505 : : /* All the fields are initialized. */
1506 : 0 : CONSTRUCTOR_NO_CLEARING (t) = false;
1507 : : return true;
1508 : :
1509 : 421 : default:
1510 : : /* FIXME are we calling this too much? */
1511 : 421 : return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
1512 : : }
1513 : : }
1514 : :
1515 : : /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1516 : : the type of the value to match. */
1517 : :
1518 : : static tree
1519 : 3 : adjust_temp_type (tree type, tree temp)
1520 : : {
1521 : 3 : if (same_type_p (TREE_TYPE (temp), type))
1522 : : return temp;
1523 : :
1524 : 2 : gcc_assert (scalarish_type_p (type));
1525 : : /* Now we know we're dealing with a scalar, and a prvalue of non-class
1526 : : type is cv-unqualified. */
1527 : 2 : return fold_convert (cv_unqualified (type), temp);
1528 : : }
1529 : :
1530 : : // forked from gcc/cp/constexpr.cc free_constructor
1531 : :
1532 : : /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
1533 : :
1534 : : static void
1535 : 0 : free_constructor (tree t)
1536 : : {
1537 : 0 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
1538 : 0 : return;
1539 : 0 : releasing_vec ctors;
1540 : 0 : vec_safe_push (ctors, t);
1541 : 0 : while (!ctors->is_empty ())
1542 : : {
1543 : 0 : tree c = ctors->pop ();
1544 : 0 : if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c))
1545 : : {
1546 : : constructor_elt *ce;
1547 : 0 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
1548 : 0 : if (TREE_CODE (ce->value) == CONSTRUCTOR)
1549 : 0 : vec_safe_push (ctors, ce->value);
1550 : 0 : ggc_free (elts);
1551 : : }
1552 : 0 : ggc_free (c);
1553 : : }
1554 : 0 : }
1555 : :
1556 : : static tree eval_and_check_array_index (const constexpr_ctx *ctx, tree t,
1557 : : bool allow_one_past,
1558 : : bool *non_constant_p, bool *overflow_p);
1559 : :
1560 : : // forked from gcc/cp/constexpr.cc cxx_eval_array_reference
1561 : :
1562 : : /* Subroutine of cxx_eval_constant_expression.
1563 : : Attempt to reduce a reference to an array slot. */
1564 : :
1565 : : static tree
1566 : 9 : eval_array_reference (const constexpr_ctx *ctx, tree t, bool lval,
1567 : : bool *non_constant_p, bool *overflow_p)
1568 : : {
1569 : 9 : tree oldary = TREE_OPERAND (t, 0);
1570 : 9 : tree ary
1571 : 9 : = eval_constant_expression (ctx, oldary, lval, non_constant_p, overflow_p);
1572 : 9 : if (*non_constant_p)
1573 : : return t;
1574 : 9 : if (!lval && TREE_CODE (ary) == VIEW_CONVERT_EXPR
1575 : 0 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0)))
1576 : 9 : && TREE_TYPE (t) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0))))
1577 : 0 : ary = TREE_OPERAND (ary, 0);
1578 : :
1579 : 9 : tree oldidx = TREE_OPERAND (t, 1);
1580 : 9 : tree index
1581 : 9 : = eval_and_check_array_index (ctx, t, lval, non_constant_p, overflow_p);
1582 : 9 : if (*non_constant_p)
1583 : : return t;
1584 : :
1585 : 8 : if (lval && ary == oldary && index == oldidx)
1586 : : return t;
1587 : 8 : else if (lval)
1588 : 0 : return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
1589 : :
1590 : 8 : unsigned len = 0, elem_nchars = 1;
1591 : 8 : tree elem_type = TREE_TYPE (TREE_TYPE (ary));
1592 : 8 : if (TREE_CODE (ary) == CONSTRUCTOR)
1593 : 8 : len = CONSTRUCTOR_NELTS (ary);
1594 : 0 : else if (TREE_CODE (ary) == STRING_CST)
1595 : : {
1596 : 0 : elem_nchars
1597 : 0 : = (TYPE_PRECISION (elem_type) / TYPE_PRECISION (char_type_node));
1598 : 0 : len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
1599 : : }
1600 : 0 : else if (TREE_CODE (ary) == VECTOR_CST)
1601 : : /* We don't create variable-length VECTOR_CSTs. */
1602 : 0 : len = VECTOR_CST_NELTS (ary).to_constant ();
1603 : : else
1604 : : {
1605 : : /* We can't do anything with other tree codes, so use
1606 : : VERIFY_CONSTANT to complain and fail. */
1607 : 0 : VERIFY_CONSTANT (ary);
1608 : 0 : rust_unreachable ();
1609 : : }
1610 : :
1611 : 8 : bool found;
1612 : 8 : HOST_WIDE_INT i = 0;
1613 : 8 : if (TREE_CODE (ary) == CONSTRUCTOR)
1614 : : {
1615 : 8 : HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
1616 : 8 : found = (ix >= 0);
1617 : 8 : if (found)
1618 : 8 : i = ix;
1619 : : }
1620 : : else
1621 : : {
1622 : 0 : i = tree_to_shwi (index);
1623 : 0 : found = (i < len);
1624 : : }
1625 : :
1626 : 8 : if (found)
1627 : : {
1628 : 8 : tree r;
1629 : 8 : if (TREE_CODE (ary) == CONSTRUCTOR)
1630 : 8 : r = (*CONSTRUCTOR_ELTS (ary))[i].value;
1631 : 0 : else if (TREE_CODE (ary) == VECTOR_CST)
1632 : 0 : r = VECTOR_CST_ELT (ary, i);
1633 : : else
1634 : 0 : r = extract_string_elt (ary, elem_nchars, i);
1635 : :
1636 : 8 : if (r)
1637 : : /* Don't VERIFY_CONSTANT here. */
1638 : : return r;
1639 : :
1640 : : /* Otherwise the element doesn't have a value yet. */
1641 : : }
1642 : :
1643 : : /* Not found. */
1644 : :
1645 : 0 : if (TREE_CODE (ary) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (ary))
1646 : : {
1647 : : /* 'ary' is part of the aggregate initializer we're currently
1648 : : building; if there's no initializer for this element yet,
1649 : : that's an error. */
1650 : 0 : if (!ctx->quiet)
1651 : 0 : error ("accessing uninitialized array element");
1652 : 0 : *non_constant_p = true;
1653 : 0 : return t;
1654 : : }
1655 : :
1656 : : /* If it's within the array bounds but doesn't have an explicit
1657 : : initializer, it's initialized from {}. But use build_value_init
1658 : : directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
1659 : 0 : tree val = NULL_TREE;
1660 : 0 : sorry ("array size expression is not supported yet");
1661 : :
1662 : 0 : constexpr_ctx new_ctx;
1663 : 0 : if (is_really_empty_class (elem_type, /*ignore_vptr*/ false))
1664 : 0 : return build_constructor (elem_type, NULL);
1665 : : // else if (CP_AGGREGATE_TYPE_P (elem_type))
1666 : : // {
1667 : : // tree empty_ctor = build_constructor (init_list_type_node, NULL);
1668 : : // //val = digest_init (elem_type, empty_ctor, tf_warning_or_error);
1669 : : // }
1670 : : // else
1671 : : // val = build_value_init (elem_type, tf_warning_or_error);
1672 : :
1673 : 0 : if (!SCALAR_TYPE_P (elem_type))
1674 : : {
1675 : 0 : new_ctx = *ctx;
1676 : 0 : if (ctx->object)
1677 : : /* If there was no object, don't add one: it could confuse us
1678 : : into thinking we're modifying a const object. */
1679 : 0 : new_ctx.object = t;
1680 : 0 : new_ctx.ctor = build_constructor (elem_type, NULL);
1681 : 0 : ctx = &new_ctx;
1682 : : }
1683 : 0 : t = eval_constant_expression (ctx, val, lval, non_constant_p, overflow_p);
1684 : 0 : if (!SCALAR_TYPE_P (elem_type) && t != ctx->ctor)
1685 : 0 : free_constructor (ctx->ctor);
1686 : : return t;
1687 : : }
1688 : :
1689 : : // forked from gcc/cp/constexpr.cc cxx_eval_component_reference
1690 : :
1691 : : /* Subroutine of cxx_eval_constant_expression.
1692 : : Attempt to reduce a field access of a value of class type. */
1693 : :
1694 : : static tree
1695 : 5 : eval_component_reference (const constexpr_ctx *ctx, tree t, bool lval,
1696 : : bool *non_constant_p, bool *overflow_p)
1697 : : {
1698 : 5 : unsigned HOST_WIDE_INT i;
1699 : 5 : tree field;
1700 : 5 : tree value;
1701 : 5 : tree part = TREE_OPERAND (t, 1);
1702 : 5 : tree orig_whole = TREE_OPERAND (t, 0);
1703 : 5 : tree whole = eval_constant_expression (ctx, orig_whole, lval, non_constant_p,
1704 : : overflow_p);
1705 : 5 : if (INDIRECT_REF_P (whole) && integer_zerop (TREE_OPERAND (whole, 0)))
1706 : : {
1707 : 0 : if (!ctx->quiet)
1708 : 0 : error ("dereferencing a null pointer in %qE", orig_whole);
1709 : 0 : *non_constant_p = true;
1710 : 0 : return t;
1711 : : }
1712 : :
1713 : 5 : if (whole == orig_whole)
1714 : : return t;
1715 : 5 : if (lval)
1716 : 0 : return fold_build3 (COMPONENT_REF, TREE_TYPE (t), whole, part, NULL_TREE);
1717 : : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
1718 : : CONSTRUCTOR. */
1719 : 5 : if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
1720 : : {
1721 : 0 : if (!ctx->quiet)
1722 : 0 : error ("%qE is not a constant expression", orig_whole);
1723 : 0 : *non_constant_p = true;
1724 : : }
1725 : 5 : if (DECL_MUTABLE_P (part))
1726 : : {
1727 : 0 : if (!ctx->quiet)
1728 : 0 : error ("mutable %qD is not usable in a constant expression", part);
1729 : 0 : *non_constant_p = true;
1730 : : }
1731 : 5 : if (*non_constant_p)
1732 : : return t;
1733 : 5 : bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole));
1734 : 6 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
1735 : : {
1736 : : /* Use name match for PMF fields, as a variant will have a
1737 : : different FIELD_DECL with a different type. */
1738 : 6 : if (pmf ? DECL_NAME (field) == DECL_NAME (part) : field == part)
1739 : : {
1740 : 5 : if (value)
1741 : : {
1742 : 5 : STRIP_ANY_LOCATION_WRAPPER (value);
1743 : 5 : return value;
1744 : : }
1745 : : else
1746 : : /* We're in the middle of initializing it. */
1747 : : break;
1748 : : }
1749 : : }
1750 : 0 : if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
1751 : 0 : && CONSTRUCTOR_NELTS (whole) > 0)
1752 : : {
1753 : : /* DR 1188 says we don't have to deal with this. */
1754 : 0 : if (!ctx->quiet)
1755 : : {
1756 : 0 : constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0);
1757 : 0 : if (cep->value == NULL_TREE)
1758 : 0 : error ("accessing uninitialized member %qD", part);
1759 : : else
1760 : 0 : error ("accessing %qD member instead of initialized %qD member in "
1761 : : "constant expression",
1762 : : part, cep->index);
1763 : : }
1764 : 0 : *non_constant_p = true;
1765 : 0 : return t;
1766 : : }
1767 : :
1768 : : /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
1769 : : classes never get represented; throw together a value now. */
1770 : 0 : if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/ false))
1771 : 0 : return build_constructor (TREE_TYPE (t), NULL);
1772 : :
1773 : 0 : gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole)));
1774 : :
1775 : 0 : if (CONSTRUCTOR_NO_CLEARING (whole))
1776 : : {
1777 : : /* 'whole' is part of the aggregate initializer we're currently
1778 : : building; if there's no initializer for this member yet, that's an
1779 : : error. */
1780 : 0 : if (!ctx->quiet)
1781 : 0 : error ("accessing uninitialized member %qD", part);
1782 : 0 : *non_constant_p = true;
1783 : 0 : return t;
1784 : : }
1785 : :
1786 : 0 : value = NULL_TREE;
1787 : 0 : sorry ("constant folding not supported for this tree code");
1788 : : /* If there's no explicit init for this field, it's value-initialized. */
1789 : : // Faisal: commenting this out as not sure if we need this but we need to come
1790 : : // back to handle this to assign suitable value to value before sending it in
1791 : : // eval_constant_expression below
1792 : : // value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
1793 : 0 : return eval_constant_expression (ctx, value, lval, non_constant_p,
1794 : 0 : overflow_p);
1795 : : }
1796 : :
1797 : : /* Subroutine of cxx_eval_statement_list. Determine whether the statement
1798 : : STMT matches *jump_target. If we're looking for a case label and we see
1799 : : the default label, note it in ctx->css_state. */
1800 : :
1801 : : static bool
1802 : 0 : label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt)
1803 : : {
1804 : 0 : switch (TREE_CODE (*jump_target))
1805 : : {
1806 : 0 : case LABEL_DECL:
1807 : 0 : if (TREE_CODE (stmt) == LABEL_EXPR
1808 : 0 : && LABEL_EXPR_LABEL (stmt) == *jump_target)
1809 : : return true;
1810 : : break;
1811 : :
1812 : 0 : case INTEGER_CST:
1813 : 0 : if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
1814 : : {
1815 : 0 : gcc_assert (ctx->css_state != NULL);
1816 : 0 : if (!CASE_LOW (stmt))
1817 : : {
1818 : : /* default: should appear just once in a SWITCH_EXPR
1819 : : body (excluding nested SWITCH_EXPR). */
1820 : 0 : gcc_assert (*ctx->css_state != css_default_seen);
1821 : : /* When evaluating SWITCH_EXPR body for the second time,
1822 : : return true for the default: label. */
1823 : 0 : if (*ctx->css_state == css_default_processing)
1824 : : return true;
1825 : 0 : *ctx->css_state = css_default_seen;
1826 : : }
1827 : 0 : else if (CASE_HIGH (stmt))
1828 : : {
1829 : 0 : if (tree_int_cst_le (CASE_LOW (stmt), *jump_target)
1830 : 0 : && tree_int_cst_le (*jump_target, CASE_HIGH (stmt)))
1831 : : return true;
1832 : : }
1833 : 0 : else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
1834 : : return true;
1835 : : }
1836 : : break;
1837 : :
1838 : : case BREAK_STMT:
1839 : : case CONTINUE_STMT:
1840 : : /* These two are handled directly in cxx_eval_loop_expr by testing
1841 : : breaks (jump_target) or continues (jump_target). */
1842 : : break;
1843 : :
1844 : 0 : default:
1845 : 0 : rust_unreachable ();
1846 : : }
1847 : : return false;
1848 : : }
1849 : :
1850 : : static tree
1851 : 18239 : eval_constant_expression (const constexpr_ctx *ctx, tree t, bool lval,
1852 : : bool *non_constant_p, bool *overflow_p,
1853 : : tree *jump_target /* = NULL */)
1854 : : {
1855 : 18239 : if (jump_target && *jump_target)
1856 : : {
1857 : : /* If we are jumping, ignore all statements/expressions except those
1858 : : that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
1859 : 0 : switch (TREE_CODE (t))
1860 : : {
1861 : : case BIND_EXPR:
1862 : : case STATEMENT_LIST:
1863 : : case LOOP_EXPR:
1864 : : case COND_EXPR:
1865 : : case IF_STMT:
1866 : : case DO_STMT:
1867 : : case WHILE_STMT:
1868 : : case FOR_STMT:
1869 : : break;
1870 : 0 : case LABEL_EXPR:
1871 : 0 : case CASE_LABEL_EXPR:
1872 : 0 : if (label_matches (ctx, jump_target, t))
1873 : : /* Found it. */
1874 : 0 : *jump_target = NULL_TREE;
1875 : 0 : return NULL_TREE;
1876 : : default:
1877 : : return NULL_TREE;
1878 : : }
1879 : : }
1880 : 18239 : if (error_operand_p (t))
1881 : : {
1882 : 3 : *non_constant_p = true;
1883 : 3 : return t;
1884 : : }
1885 : :
1886 : 18236 : location_t loc = EXPR_LOCATION (t);
1887 : :
1888 : 18236 : if (t == NULL_TREE)
1889 : : return NULL_TREE;
1890 : :
1891 : 18236 : if (CONSTANT_CLASS_P (t))
1892 : : {
1893 : 3506 : if (TREE_OVERFLOW (t))
1894 : : {
1895 : 0 : error_at (loc, "overflow in constant expression");
1896 : 0 : return t;
1897 : : }
1898 : :
1899 : : return t;
1900 : : }
1901 : :
1902 : : // Avoid excessively long constexpr evaluations
1903 : 14730 : if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit)
1904 : : {
1905 : 0 : rust_error_at (
1906 : : loc,
1907 : : "%<constexpr%> evaluation operation count exceeds limit of "
1908 : : "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
1909 : : constexpr_ops_limit);
1910 : :
1911 : 0 : return t;
1912 : : }
1913 : :
1914 : 14730 : constexpr_ctx new_ctx;
1915 : 14730 : tree r = t;
1916 : 14730 : tree_code tcode = TREE_CODE (t);
1917 : 14730 : switch (tcode)
1918 : : {
1919 : 46 : case VAR_DECL:
1920 : 46 : if (DECL_HAS_VALUE_EXPR_P (t))
1921 : : {
1922 : 0 : r = DECL_VALUE_EXPR (t);
1923 : 0 : return eval_constant_expression (ctx, r, lval, non_constant_p,
1924 : 0 : overflow_p);
1925 : : }
1926 : : /* fall through */
1927 : 1218 : case CONST_DECL:
1928 : 1218 : {
1929 : : /* We used to not check lval for CONST_DECL, but darwin.cc uses
1930 : : CONST_DECL for aggregate constants. */
1931 : 1218 : if (lval)
1932 : : return t;
1933 : 1205 : else if (t == ctx->object)
1934 : 0 : return ctx->ctor;
1935 : 1205 : if (VAR_P (t))
1936 : 33 : if (tree *p = ctx->global->values.get (t))
1937 : 33 : if (*p != NULL_TREE)
1938 : : {
1939 : 33 : r = *p;
1940 : 33 : break;
1941 : : }
1942 : 1172 : r = decl_constant_value (t, /*unshare_p=*/false);
1943 : 1172 : if (TREE_CODE (r) == TARGET_EXPR
1944 : 1172 : && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
1945 : 0 : r = TARGET_EXPR_INITIAL (r);
1946 : 1172 : if (DECL_P (r))
1947 : : {
1948 : 0 : non_const_var_error (loc, r);
1949 : 0 : return r;
1950 : : }
1951 : : }
1952 : : break;
1953 : :
1954 : 35 : case PARM_DECL:
1955 : 35 : if (lval && !TYPE_REF_P (TREE_TYPE (t)))
1956 : : /* glvalue use. */;
1957 : 25 : else if (tree *p = ctx->global->values.get (r))
1958 : 25 : r = *p;
1959 : 0 : else if (lval)
1960 : : /* Defer in case this is only used for its type. */;
1961 : 0 : else if (COMPLETE_TYPE_P (TREE_TYPE (t))
1962 : 0 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/ false))
1963 : : {
1964 : : /* If the class is empty, we aren't actually loading anything. */
1965 : 0 : r = build_constructor (TREE_TYPE (t), NULL);
1966 : 0 : TREE_CONSTANT (r) = true;
1967 : : }
1968 : : else
1969 : : {
1970 : 0 : if (!ctx->quiet)
1971 : 0 : error ("%qE is not a constant expression", t);
1972 : 0 : *non_constant_p = true;
1973 : : }
1974 : : break;
1975 : :
1976 : 43 : case POINTER_PLUS_EXPR:
1977 : 43 : case POINTER_DIFF_EXPR:
1978 : 43 : case PLUS_EXPR:
1979 : 43 : case MINUS_EXPR:
1980 : 43 : case MULT_EXPR:
1981 : 43 : case TRUNC_DIV_EXPR:
1982 : 43 : case CEIL_DIV_EXPR:
1983 : 43 : case FLOOR_DIV_EXPR:
1984 : 43 : case ROUND_DIV_EXPR:
1985 : 43 : case TRUNC_MOD_EXPR:
1986 : 43 : case CEIL_MOD_EXPR:
1987 : 43 : case ROUND_MOD_EXPR:
1988 : 43 : case RDIV_EXPR:
1989 : 43 : case EXACT_DIV_EXPR:
1990 : 43 : case MIN_EXPR:
1991 : 43 : case MAX_EXPR:
1992 : 43 : case LSHIFT_EXPR:
1993 : 43 : case RSHIFT_EXPR:
1994 : 43 : case LROTATE_EXPR:
1995 : 43 : case RROTATE_EXPR:
1996 : 43 : case BIT_IOR_EXPR:
1997 : 43 : case BIT_XOR_EXPR:
1998 : 43 : case BIT_AND_EXPR:
1999 : 43 : case TRUTH_XOR_EXPR:
2000 : 43 : case LT_EXPR:
2001 : 43 : case LE_EXPR:
2002 : 43 : case GT_EXPR:
2003 : 43 : case GE_EXPR:
2004 : 43 : case EQ_EXPR:
2005 : 43 : case NE_EXPR:
2006 : 43 : case SPACESHIP_EXPR:
2007 : 43 : case UNORDERED_EXPR:
2008 : 43 : case ORDERED_EXPR:
2009 : 43 : case UNLT_EXPR:
2010 : 43 : case UNLE_EXPR:
2011 : 43 : case UNGT_EXPR:
2012 : 43 : case UNGE_EXPR:
2013 : 43 : case UNEQ_EXPR:
2014 : 43 : case LTGT_EXPR:
2015 : 43 : case RANGE_EXPR:
2016 : 43 : case COMPLEX_EXPR:
2017 : 43 : r = eval_binary_expression (ctx, t, lval, non_constant_p, overflow_p);
2018 : 43 : break;
2019 : :
2020 : : /* fold can introduce non-IF versions of these; still treat them as
2021 : : short-circuiting. */
2022 : 0 : case TRUTH_AND_EXPR:
2023 : 0 : case TRUTH_ANDIF_EXPR:
2024 : 0 : r = eval_logical_expression (ctx, t, boolean_false_node,
2025 : : boolean_true_node, lval, non_constant_p,
2026 : : overflow_p);
2027 : 0 : break;
2028 : :
2029 : 0 : case TRUTH_OR_EXPR:
2030 : 0 : case TRUTH_ORIF_EXPR:
2031 : 0 : r = eval_logical_expression (ctx, t, boolean_true_node,
2032 : : boolean_false_node, lval, non_constant_p,
2033 : : overflow_p);
2034 : 0 : break;
2035 : :
2036 : 0 : case TARGET_EXPR:
2037 : 0 : {
2038 : 0 : tree type = TREE_TYPE (t);
2039 : :
2040 : 0 : if (!literal_type_p (type))
2041 : : {
2042 : 0 : if (!ctx->quiet)
2043 : : {
2044 : 0 : auto_diagnostic_group d;
2045 : 0 : error ("temporary of non-literal type %qT in a "
2046 : : "constant expression",
2047 : : type);
2048 : 0 : explain_non_literal_class (type);
2049 : 0 : }
2050 : 0 : *non_constant_p = true;
2051 : 0 : break;
2052 : : }
2053 : 0 : gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t));
2054 : : /* Avoid evaluating a TARGET_EXPR more than once. */
2055 : 0 : tree slot = TARGET_EXPR_SLOT (t);
2056 : 0 : if (tree *p = ctx->global->values.get (slot))
2057 : : {
2058 : 0 : if (lval)
2059 : 0 : return slot;
2060 : 0 : r = *p;
2061 : 0 : break;
2062 : : }
2063 : 0 : if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)))
2064 : : {
2065 : : /* We're being expanded without an explicit target, so start
2066 : : initializing a new object; expansion with an explicit target
2067 : : strips the TARGET_EXPR before we get here. */
2068 : 0 : new_ctx = *ctx;
2069 : : /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
2070 : : any PLACEHOLDER_EXPR within the initializer that refers to the
2071 : : former object under construction. */
2072 : 0 : new_ctx.parent = ctx;
2073 : 0 : new_ctx.ctor = build_constructor (type, NULL);
2074 : 0 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
2075 : 0 : new_ctx.object = slot;
2076 : 0 : ctx->global->values.put (new_ctx.object, new_ctx.ctor);
2077 : 0 : ctx = &new_ctx;
2078 : : }
2079 : : /* Pass false for 'lval' because this indicates
2080 : : initialization of a temporary. */
2081 : 0 : r = eval_constant_expression (ctx, TREE_OPERAND (t, 1), false,
2082 : : non_constant_p, overflow_p);
2083 : 0 : if (*non_constant_p)
2084 : : break;
2085 : : /* Adjust the type of the result to the type of the temporary. */
2086 : 0 : r = adjust_temp_type (type, r);
2087 : 0 : if (TARGET_EXPR_CLEANUP (t) && !CLEANUP_EH_ONLY (t))
2088 : 0 : ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t));
2089 : 0 : r = unshare_constructor (r);
2090 : 0 : ctx->global->values.put (slot, r);
2091 : 0 : if (ctx->save_exprs)
2092 : 0 : ctx->save_exprs->safe_push (slot);
2093 : 0 : if (lval)
2094 : : return slot;
2095 : : }
2096 : 0 : break;
2097 : :
2098 : 1938 : case CALL_EXPR:
2099 : 1938 : r = eval_call_expression (ctx, t, lval, non_constant_p, overflow_p);
2100 : 1938 : break;
2101 : :
2102 : 1930 : case RETURN_EXPR:
2103 : 1930 : if (TREE_OPERAND (t, 0) != NULL_TREE)
2104 : 1930 : r = eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
2105 : : non_constant_p, overflow_p);
2106 : : /* FALLTHRU */
2107 : 1930 : case BREAK_STMT:
2108 : 1930 : case CONTINUE_STMT:
2109 : 1930 : if (jump_target)
2110 : 1930 : *jump_target = t;
2111 : : else
2112 : : {
2113 : : /* Can happen with ({ return true; }) && false; passed to
2114 : : maybe_constant_value. There is nothing to jump over in this
2115 : : case, and the bug will be diagnosed later. */
2116 : 0 : gcc_assert (ctx->quiet);
2117 : 0 : *non_constant_p = true;
2118 : : }
2119 : : break;
2120 : :
2121 : 1644 : case DECL_EXPR:
2122 : 1644 : {
2123 : 1644 : r = DECL_EXPR_DECL (t);
2124 : :
2125 : 1644 : if (AGGREGATE_TYPE_P (TREE_TYPE (r)) || VECTOR_TYPE_P (TREE_TYPE (r)))
2126 : : {
2127 : 68 : new_ctx = *ctx;
2128 : 68 : new_ctx.object = r;
2129 : 68 : new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
2130 : 68 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
2131 : 68 : ctx->global->values.put (r, new_ctx.ctor);
2132 : 68 : ctx = &new_ctx;
2133 : : }
2134 : :
2135 : 1644 : if (tree init = DECL_INITIAL (r))
2136 : : {
2137 : 25 : init = eval_constant_expression (ctx, init, false, non_constant_p,
2138 : : overflow_p);
2139 : : /* Don't share a CONSTRUCTOR that might be changed. */
2140 : 25 : init = unshare_constructor (init);
2141 : : /* Remember that a constant object's constructor has already
2142 : : run. */
2143 : 25 : if (CLASS_TYPE_P (TREE_TYPE (r)) && RS_TYPE_CONST_P (TREE_TYPE (r)))
2144 : 0 : TREE_READONLY (init) = true;
2145 : 25 : ctx->global->values.put (r, init);
2146 : : }
2147 : 1619 : else if (ctx == &new_ctx)
2148 : : /* We gave it a CONSTRUCTOR above. */;
2149 : : else
2150 : 1552 : ctx->global->values.put (r, NULL_TREE);
2151 : : }
2152 : 1644 : break;
2153 : :
2154 : : /* These differ from cxx_eval_unary_expression in that this doesn't
2155 : : check for a constant operand or result; an address can be
2156 : : constant without its operand being, and vice versa. */
2157 : 0 : case MEM_REF:
2158 : 0 : case INDIRECT_REF:
2159 : 0 : r = rs_eval_indirect_ref (ctx, t, lval, non_constant_p, overflow_p);
2160 : 0 : break;
2161 : :
2162 : 0 : case VEC_PERM_EXPR:
2163 : 0 : r = cxx_eval_trinary_expression (ctx, t, lval, non_constant_p,
2164 : : overflow_p);
2165 : 0 : break;
2166 : :
2167 : 0 : case PAREN_EXPR:
2168 : 0 : gcc_assert (!REF_PARENTHESIZED_P (t));
2169 : : /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in
2170 : : constant expressions since it's unaffected by -fassociative-math. */
2171 : 0 : r = eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
2172 : : non_constant_p, overflow_p);
2173 : 0 : break;
2174 : :
2175 : 1953 : case MODIFY_EXPR:
2176 : 1953 : r = eval_store_expression (ctx, t, false, non_constant_p, overflow_p);
2177 : 1953 : break;
2178 : :
2179 : 1965 : case STATEMENT_LIST:
2180 : 1965 : new_ctx = *ctx;
2181 : 1965 : new_ctx.ctor = new_ctx.object = NULL_TREE;
2182 : 1965 : return eval_statement_list (&new_ctx, t, non_constant_p, overflow_p,
2183 : 1965 : jump_target);
2184 : :
2185 : 1965 : case BIND_EXPR:
2186 : 1965 : return eval_constant_expression (ctx, BIND_EXPR_BODY (t), lval,
2187 : 1965 : non_constant_p, overflow_p, jump_target);
2188 : :
2189 : 0 : case OBJ_TYPE_REF:
2190 : : /* Virtual function lookup. We don't need to do anything fancy. */
2191 : 0 : return eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t), lval,
2192 : 0 : non_constant_p, overflow_p);
2193 : :
2194 : 1 : case EXIT_EXPR:
2195 : 1 : {
2196 : 1 : tree cond = TREE_OPERAND (t, 0);
2197 : 1 : cond = eval_constant_expression (ctx, cond, /*lval*/ false,
2198 : : non_constant_p, overflow_p);
2199 : 1 : VERIFY_CONSTANT (cond);
2200 : 1 : if (integer_nonzerop (cond))
2201 : 1 : *jump_target = t;
2202 : : }
2203 : : break;
2204 : :
2205 : 1929 : case RESULT_DECL:
2206 : 1929 : if (lval)
2207 : : return t;
2208 : : /* We ask for an rvalue for the RESULT_DECL when indirecting
2209 : : through an invisible reference, or in named return value
2210 : : optimization. */
2211 : 0 : if (tree *p = ctx->global->values.get (t))
2212 : 0 : return *p;
2213 : : else
2214 : : {
2215 : 0 : if (!ctx->quiet)
2216 : 0 : error ("%qE is not a constant expression", t);
2217 : 0 : *non_constant_p = true;
2218 : : }
2219 : 0 : break;
2220 : :
2221 : 0 : case SAVE_EXPR:
2222 : : /* Avoid evaluating a SAVE_EXPR more than once. */
2223 : 0 : if (tree *p = ctx->global->values.get (t))
2224 : 0 : r = *p;
2225 : : else
2226 : : {
2227 : 0 : r = eval_constant_expression (ctx, TREE_OPERAND (t, 0), false,
2228 : : non_constant_p, overflow_p);
2229 : 0 : if (*non_constant_p)
2230 : : break;
2231 : 0 : ctx->global->values.put (t, r);
2232 : 0 : if (ctx->save_exprs)
2233 : 0 : ctx->save_exprs->safe_push (t);
2234 : : }
2235 : : break;
2236 : :
2237 : 1 : case ADDR_EXPR:
2238 : 1 : {
2239 : 1 : tree oldop = TREE_OPERAND (t, 0);
2240 : 1 : tree op = eval_constant_expression (ctx, oldop,
2241 : : /*lval*/ true, non_constant_p,
2242 : : overflow_p);
2243 : : /* Don't VERIFY_CONSTANT here. */
2244 : 1 : if (*non_constant_p)
2245 : : return t;
2246 : : /* This function does more aggressive folding than fold itself. */
2247 : 1 : r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
2248 : 1 : if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
2249 : : {
2250 : 1 : ggc_free (r);
2251 : 1 : return t;
2252 : : }
2253 : : break;
2254 : : }
2255 : :
2256 : 0 : case COMPOUND_EXPR:
2257 : 0 : {
2258 : : /* check_return_expr sometimes wraps a TARGET_EXPR in a
2259 : : COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
2260 : : introduced by build_call_a. */
2261 : 0 : tree op0 = TREE_OPERAND (t, 0);
2262 : 0 : tree op1 = TREE_OPERAND (t, 1);
2263 : 0 : STRIP_NOPS (op1);
2264 : 0 : if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
2265 : 0 : || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
2266 : 0 : r = eval_constant_expression (ctx, op0, lval, non_constant_p,
2267 : : overflow_p, jump_target);
2268 : : else
2269 : : {
2270 : : /* Check that the LHS is constant and then discard it. */
2271 : 0 : eval_constant_expression (ctx, op0, true, non_constant_p,
2272 : : overflow_p, jump_target);
2273 : 0 : if (*non_constant_p)
2274 : : return t;
2275 : 0 : op1 = TREE_OPERAND (t, 1);
2276 : 0 : r = eval_constant_expression (ctx, op1, lval, non_constant_p,
2277 : : overflow_p, jump_target);
2278 : : }
2279 : : }
2280 : : break;
2281 : :
2282 : 0 : case REALPART_EXPR:
2283 : 0 : case IMAGPART_EXPR:
2284 : 0 : if (lval)
2285 : : {
2286 : 0 : r = eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
2287 : : non_constant_p, overflow_p);
2288 : 0 : if (r == error_mark_node)
2289 : : ;
2290 : 0 : else if (r == TREE_OPERAND (t, 0))
2291 : 0 : r = t;
2292 : : else
2293 : 0 : r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r);
2294 : : break;
2295 : : }
2296 : : /* FALLTHRU */
2297 : 7 : case CONJ_EXPR:
2298 : 7 : case FIX_TRUNC_EXPR:
2299 : 7 : case FLOAT_EXPR:
2300 : 7 : case NEGATE_EXPR:
2301 : 7 : case ABS_EXPR:
2302 : 7 : case ABSU_EXPR:
2303 : 7 : case BIT_NOT_EXPR:
2304 : 7 : case TRUTH_NOT_EXPR:
2305 : 7 : case FIXED_CONVERT_EXPR:
2306 : 7 : r = eval_unary_expression (ctx, t, lval, non_constant_p, overflow_p);
2307 : 7 : break;
2308 : :
2309 : 1 : case LOOP_EXPR:
2310 : 1 : case WHILE_STMT:
2311 : 1 : case FOR_STMT:
2312 : 1 : eval_loop_expr (ctx, t, non_constant_p, overflow_p, jump_target);
2313 : 1 : break;
2314 : :
2315 : 0 : case SWITCH_EXPR:
2316 : 0 : case SWITCH_STMT:
2317 : 0 : eval_switch_expr (ctx, t, non_constant_p, overflow_p, jump_target);
2318 : 0 : break;
2319 : :
2320 : 9 : case ARRAY_REF:
2321 : 9 : r = eval_array_reference (ctx, t, lval, non_constant_p, overflow_p);
2322 : 9 : break;
2323 : :
2324 : 5 : case COMPONENT_REF:
2325 : 5 : if (is_overloaded_fn (t))
2326 : : {
2327 : : /* We can only get here in checking mode via
2328 : : build_non_dependent_expr, because any expression that
2329 : : calls or takes the address of the function will have
2330 : : pulled a FUNCTION_DECL out of the COMPONENT_REF. */
2331 : 0 : gcc_checking_assert (ctx->quiet || errorcount);
2332 : 0 : *non_constant_p = true;
2333 : 0 : return t;
2334 : : }
2335 : 5 : r = eval_component_reference (ctx, t, lval, non_constant_p, overflow_p);
2336 : 5 : break;
2337 : :
2338 : 0 : case BIT_FIELD_REF:
2339 : 0 : r = eval_bit_field_ref (ctx, t, lval, non_constant_p, overflow_p);
2340 : 0 : break;
2341 : :
2342 : 16 : case COND_EXPR:
2343 : 16 : case IF_STMT: // comes from cp-tree.def
2344 : 16 : if (jump_target && *jump_target)
2345 : : {
2346 : 0 : tree orig_jump = *jump_target;
2347 : 0 : tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1))
2348 : 0 : ? TREE_OPERAND (t, 1)
2349 : 0 : : void_node);
2350 : : /* When jumping to a label, the label might be either in the
2351 : : then or else blocks, so process then block first in skipping
2352 : : mode first, and if we are still in the skipping mode at its end,
2353 : : process the else block too. */
2354 : 0 : r = eval_constant_expression (ctx, arg, lval, non_constant_p,
2355 : : overflow_p, jump_target);
2356 : : /* It's possible that we found the label in the then block. But
2357 : : it could have been followed by another jumping statement, e.g.
2358 : : say we're looking for case 1:
2359 : : if (cond)
2360 : : {
2361 : : // skipped statements
2362 : : case 1:; // clears up *jump_target
2363 : : return 1; // and sets it to a RETURN_EXPR
2364 : : }
2365 : : else { ... }
2366 : : in which case we need not go looking to the else block.
2367 : : (goto is not allowed in a constexpr function.) */
2368 : 0 : if (*jump_target == orig_jump)
2369 : : {
2370 : 0 : arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2))
2371 : 0 : ? TREE_OPERAND (t, 2)
2372 : : : void_node);
2373 : 0 : r = eval_constant_expression (ctx, arg, lval, non_constant_p,
2374 : : overflow_p, jump_target);
2375 : : }
2376 : : break;
2377 : : }
2378 : 16 : r = eval_conditional_expression (ctx, t, lval, non_constant_p, overflow_p,
2379 : : jump_target);
2380 : 16 : break;
2381 : :
2382 : 0 : case VEC_COND_EXPR:
2383 : 0 : r = eval_vector_conditional_expression (ctx, t, non_constant_p,
2384 : : overflow_p);
2385 : 0 : break;
2386 : :
2387 : 0 : case TRY_CATCH_EXPR:
2388 : 0 : if (TREE_OPERAND (t, 0) == NULL_TREE)
2389 : : {
2390 : 0 : r = void_node;
2391 : 0 : break;
2392 : : }
2393 : 0 : r = eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
2394 : : non_constant_p, overflow_p, jump_target);
2395 : 0 : break;
2396 : :
2397 : 0 : case CLEANUP_POINT_EXPR:
2398 : 0 : {
2399 : 0 : auto_vec<tree, 2> cleanups;
2400 : 0 : vec<tree> *prev_cleanups = ctx->global->cleanups;
2401 : 0 : ctx->global->cleanups = &cleanups;
2402 : 0 : r = eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
2403 : : non_constant_p, overflow_p, jump_target);
2404 : 0 : ctx->global->cleanups = prev_cleanups;
2405 : 0 : unsigned int i;
2406 : 0 : tree cleanup;
2407 : : /* Evaluate the cleanups. */
2408 : 0 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
2409 : 0 : eval_constant_expression (ctx, cleanup, false, non_constant_p,
2410 : : overflow_p);
2411 : 0 : }
2412 : 0 : break;
2413 : :
2414 : 0 : case TRY_FINALLY_EXPR:
2415 : 0 : r = eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
2416 : : non_constant_p, overflow_p, jump_target);
2417 : 0 : if (!*non_constant_p)
2418 : : /* Also evaluate the cleanup. */
2419 : 0 : eval_constant_expression (ctx, TREE_OPERAND (t, 1), true,
2420 : : non_constant_p, overflow_p);
2421 : : break;
2422 : :
2423 : 62 : case CONSTRUCTOR:
2424 : 62 : if (TREE_CONSTANT (t) && reduced_constant_expression_p (t))
2425 : : {
2426 : : /* Don't re-process a constant CONSTRUCTOR, but do fold it to
2427 : : VECTOR_CST if applicable. */
2428 : 62 : verify_constructor_flags (t);
2429 : 62 : if (TREE_CONSTANT (t))
2430 : 62 : return fold (t);
2431 : : }
2432 : 0 : r = eval_bare_aggregate (ctx, t, lval, non_constant_p, overflow_p);
2433 : 0 : break;
2434 : :
2435 : : /* FALLTHROUGH. */
2436 : 7 : case NOP_EXPR:
2437 : 7 : case CONVERT_EXPR:
2438 : 7 : case VIEW_CONVERT_EXPR:
2439 : 7 : {
2440 : 7 : tree oldop = TREE_OPERAND (t, 0);
2441 : :
2442 : 7 : tree op = eval_constant_expression (ctx, oldop, lval, non_constant_p,
2443 : : overflow_p);
2444 : 7 : if (*non_constant_p)
2445 : : return t;
2446 : 7 : tree type = TREE_TYPE (t);
2447 : :
2448 : 7 : if (VOID_TYPE_P (type))
2449 : 0 : return void_node;
2450 : :
2451 : 0 : if (TREE_CODE (t) == CONVERT_EXPR && ARITHMETIC_TYPE_P (type)
2452 : 7 : && INDIRECT_TYPE_P (TREE_TYPE (op)) && ctx->manifestly_const_eval)
2453 : : {
2454 : 0 : if (!ctx->quiet)
2455 : 0 : error_at (loc,
2456 : : "conversion from pointer type %qT to arithmetic type "
2457 : : "%qT in a constant expression",
2458 : 0 : TREE_TYPE (op), type);
2459 : 0 : *non_constant_p = true;
2460 : 0 : return t;
2461 : : }
2462 : :
2463 : 0 : if (TYPE_PTROB_P (type) && TYPE_PTR_P (TREE_TYPE (op))
2464 : 7 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op))))
2465 : : {
2466 : : /* Likewise, don't error when casting from void* when OP is
2467 : : &heap uninit and similar. */
2468 : 0 : tree sop = tree_strip_nop_conversions (op);
2469 : 0 : if (TREE_CODE (sop) == ADDR_EXPR && VAR_P (TREE_OPERAND (sop, 0))
2470 : 0 : && DECL_ARTIFICIAL (TREE_OPERAND (sop, 0)))
2471 : : /* OK */;
2472 : : else
2473 : : {
2474 : 0 : if (!ctx->quiet)
2475 : 0 : error_at (loc, "cast from %qT is not allowed",
2476 : 0 : TREE_TYPE (op));
2477 : 0 : *non_constant_p = true;
2478 : 0 : return t;
2479 : : }
2480 : : }
2481 : :
2482 : 7 : if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST)
2483 : : {
2484 : 0 : if (integer_zerop (op))
2485 : : {
2486 : 0 : if (TYPE_REF_P (type))
2487 : : {
2488 : 0 : if (!ctx->quiet)
2489 : 0 : error_at (loc, "dereferencing a null pointer");
2490 : 0 : *non_constant_p = true;
2491 : 0 : return t;
2492 : : }
2493 : : }
2494 : : else
2495 : : {
2496 : : /* This detects for example:
2497 : : reinterpret_cast<void*>(sizeof 0)
2498 : : */
2499 : 0 : if (!ctx->quiet)
2500 : 0 : error_at (loc,
2501 : : "%<reinterpret_cast<%T>(%E)%> is not "
2502 : : "a constant expression",
2503 : : type, op);
2504 : 0 : *non_constant_p = true;
2505 : 0 : return t;
2506 : : }
2507 : : }
2508 : :
2509 : 0 : if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == NOP_EXPR
2510 : 0 : && TREE_TYPE (op) == ptr_type_node
2511 : 0 : && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR
2512 : 0 : && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0))
2513 : 7 : && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0), 0))
2514 : 0 : == heap_uninit_identifier
2515 : 0 : || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0), 0))
2516 : 0 : == heap_vec_uninit_identifier))
2517 : : {
2518 : 0 : tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
2519 : 0 : tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var));
2520 : 0 : tree elt_type = TREE_TYPE (type);
2521 : 0 : tree cookie_size = NULL_TREE;
2522 : 0 : if (TREE_CODE (elt_type) == RECORD_TYPE
2523 : 0 : && TYPE_NAME (elt_type) == heap_identifier)
2524 : : {
2525 : 0 : tree fld1 = TYPE_FIELDS (elt_type);
2526 : 0 : tree fld2 = DECL_CHAIN (fld1);
2527 : 0 : elt_type = TREE_TYPE (TREE_TYPE (fld2));
2528 : 0 : cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1));
2529 : : }
2530 : 0 : DECL_NAME (var) = (DECL_NAME (var) == heap_uninit_identifier
2531 : 0 : ? heap_identifier
2532 : : : heap_vec_identifier);
2533 : 0 : TREE_TYPE (var)
2534 : 0 : = build_new_constexpr_heap_type (elt_type, cookie_size, var_size);
2535 : 0 : TREE_TYPE (TREE_OPERAND (op, 0))
2536 : 0 : = build_pointer_type (TREE_TYPE (var));
2537 : : }
2538 : :
2539 : 7 : if (op == oldop)
2540 : : /* We didn't fold at the top so we could check for ptr-int
2541 : : conversion. */
2542 : 0 : return fold (t);
2543 : :
2544 : 7 : tree sop;
2545 : :
2546 : : /* Handle an array's bounds having been deduced after we built
2547 : : the wrapping expression. */
2548 : 7 : if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op)))
2549 : 7 : r = op;
2550 : 0 : else if (sop = tree_strip_nop_conversions (op),
2551 : : sop != op
2552 : 0 : && (same_type_ignoring_tlq_and_bounds_p (type,
2553 : 0 : TREE_TYPE (sop))))
2554 : 0 : r = sop;
2555 : : else
2556 : 0 : r = fold_build1 (tcode, type, op);
2557 : :
2558 : : /* Conversion of an out-of-range value has implementation-defined
2559 : : behavior; the language considers it different from arithmetic
2560 : : overflow, which is undefined. */
2561 : 7 : if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
2562 : 0 : TREE_OVERFLOW (r) = false;
2563 : : }
2564 : : break;
2565 : :
2566 : 0 : case PLACEHOLDER_EXPR:
2567 : : /* Use of the value or address of the current object. */
2568 : 0 : if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t)))
2569 : : {
2570 : 0 : if (TREE_CODE (ctor) == CONSTRUCTOR)
2571 : : return ctor;
2572 : : else
2573 : 0 : return eval_constant_expression (ctx, ctor, lval, non_constant_p,
2574 : 0 : overflow_p);
2575 : : }
2576 : : /* A placeholder without a referent. We can get here when
2577 : : checking whether NSDMIs are noexcept, or in massage_init_elt;
2578 : : just say it's non-constant for now. */
2579 : 0 : gcc_assert (ctx->quiet);
2580 : 0 : *non_constant_p = true;
2581 : 0 : break;
2582 : :
2583 : 0 : case ANNOTATE_EXPR:
2584 : 0 : r = eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
2585 : : non_constant_p, overflow_p, jump_target);
2586 : 0 : break;
2587 : :
2588 : 0 : case ASM_EXPR:
2589 : 0 : if (!ctx->quiet)
2590 : 0 : inline_asm_in_constexpr_error (loc);
2591 : 0 : *non_constant_p = true;
2592 : 0 : return t;
2593 : :
2594 : : default:
2595 : : break;
2596 : : }
2597 : :
2598 : 8795 : return r;
2599 : : }
2600 : :
2601 : : /* Complain about a const object OBJ being modified in a constant expression.
2602 : : EXPR is the MODIFY_EXPR expression performing the modification. */
2603 : :
2604 : : static void
2605 : 0 : modifying_const_object_error (tree expr, tree obj)
2606 : : {
2607 : 0 : location_t loc = EXPR_LOCATION (expr);
2608 : 0 : auto_diagnostic_group d;
2609 : 0 : error_at (loc,
2610 : : "modifying a const object %qE is not allowed in "
2611 : : "a constant expression",
2612 : 0 : TREE_OPERAND (expr, 0));
2613 : 0 : inform (location_of (obj), "originally declared %<const%> here");
2614 : 0 : }
2615 : :
2616 : : /* Return true iff DECL is an empty field, either for an empty base or a
2617 : : [[no_unique_address]] data member. */
2618 : :
2619 : : bool
2620 : 1 : is_empty_field (tree decl)
2621 : : {
2622 : 1 : if (!decl || TREE_CODE (decl) != FIELD_DECL)
2623 : : return false;
2624 : :
2625 : 1 : bool r = is_empty_class (TREE_TYPE (decl));
2626 : :
2627 : : /* Empty fields should have size zero. */
2628 : 1 : gcc_checking_assert (!r || integer_zerop (DECL_SIZE (decl)));
2629 : :
2630 : : return r;
2631 : : }
2632 : :
2633 : : static tree
2634 : 1953 : eval_store_expression (const constexpr_ctx *ctx, tree t, bool lval,
2635 : : bool *non_constant_p, bool *overflow_p)
2636 : : {
2637 : 1953 : constexpr_ctx new_ctx = *ctx;
2638 : :
2639 : 1953 : tree init = TREE_OPERAND (t, 1);
2640 : 1953 : if (TREE_CLOBBER_P (init))
2641 : : /* Just ignore clobbers. */
2642 : 0 : return void_node;
2643 : :
2644 : : /* First we figure out where we're storing to. */
2645 : 1953 : tree target = TREE_OPERAND (t, 0);
2646 : :
2647 : 1953 : tree type = TREE_TYPE (target);
2648 : 1953 : bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR;
2649 : : if (preeval)
2650 : : {
2651 : : /* Evaluate the value to be stored without knowing what object it will be
2652 : : stored in, so that any side-effects happen first. */
2653 : 1953 : if (!SCALAR_TYPE_P (type))
2654 : 63 : new_ctx.ctor = new_ctx.object = NULL_TREE;
2655 : 1953 : init = eval_constant_expression (&new_ctx, init, false, non_constant_p,
2656 : : overflow_p);
2657 : 1953 : if (*non_constant_p)
2658 : : return t;
2659 : : }
2660 : :
2661 : 1952 : bool evaluated = false;
2662 : 1952 : if (lval)
2663 : : {
2664 : : /* If we want to return a reference to the target, we need to evaluate it
2665 : : as a whole; otherwise, only evaluate the innermost piece to avoid
2666 : : building up unnecessary *_REFs. */
2667 : 0 : target = eval_constant_expression (ctx, target, true, non_constant_p,
2668 : : overflow_p);
2669 : 0 : evaluated = true;
2670 : 0 : if (*non_constant_p)
2671 : : return t;
2672 : : }
2673 : :
2674 : : /* Find the underlying variable. */
2675 : 1952 : releasing_vec refs;
2676 : 1952 : tree object = NULL_TREE;
2677 : : /* If we're modifying a const object, save it. */
2678 : 1952 : tree const_object_being_modified = NULL_TREE;
2679 : : // bool mutable_p = false;
2680 : 5858 : for (tree probe = target; object == NULL_TREE;)
2681 : : {
2682 : 3906 : switch (TREE_CODE (probe))
2683 : : {
2684 : 2 : case BIT_FIELD_REF:
2685 : 2 : case COMPONENT_REF:
2686 : 2 : case ARRAY_REF:
2687 : 2 : {
2688 : 2 : tree ob = TREE_OPERAND (probe, 0);
2689 : 2 : tree elt = TREE_OPERAND (probe, 1);
2690 : 2 : if (TREE_CODE (elt) == FIELD_DECL /*&& DECL_MUTABLE_P (elt)*/)
2691 : : {
2692 : : // mutable_p = true;
2693 : : }
2694 : 2 : if (TREE_CODE (probe) == ARRAY_REF)
2695 : : {
2696 : 1 : elt = eval_and_check_array_index (ctx, probe, false,
2697 : : non_constant_p, overflow_p);
2698 : 1 : if (*non_constant_p)
2699 : 0 : return t;
2700 : : }
2701 : : /* We don't check modifying_const_object_p for ARRAY_REFs. Given
2702 : : "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
2703 : : the array isn't const. Instead, check "a" in the next iteration;
2704 : : that will detect modifying "const int a[10]". */
2705 : : // else if (evaluated
2706 : : // && modifying_const_object_p (TREE_CODE (t), probe,
2707 : : // mutable_p)
2708 : : // && const_object_being_modified == NULL_TREE)
2709 : : // const_object_being_modified = probe;
2710 : 2 : vec_safe_push (refs, elt);
2711 : 2 : vec_safe_push (refs, TREE_TYPE (probe));
2712 : 2 : probe = ob;
2713 : : }
2714 : 2 : break;
2715 : :
2716 : 3904 : default:
2717 : 3904 : if (evaluated)
2718 : 1952 : object = probe;
2719 : : else
2720 : : {
2721 : 1952 : probe = eval_constant_expression (ctx, probe, true,
2722 : : non_constant_p, overflow_p);
2723 : 1952 : evaluated = true;
2724 : 1952 : if (*non_constant_p)
2725 : : return t;
2726 : : }
2727 : : break;
2728 : : }
2729 : : }
2730 : :
2731 : : // if (modifying_const_object_p (TREE_CODE (t), object, mutable_p)
2732 : : // && const_object_being_modified == NULL_TREE)
2733 : : // const_object_being_modified = object;
2734 : :
2735 : : /* And then find/build up our initializer for the path to the subobject
2736 : : we're initializing. */
2737 : 1952 : tree *valp;
2738 : 1952 : if (DECL_P (object))
2739 : 1952 : valp = ctx->global->values.get (object);
2740 : : else
2741 : : valp = NULL;
2742 : 1952 : if (!valp)
2743 : : {
2744 : : /* A constant-expression cannot modify objects from outside the
2745 : : constant-expression. */
2746 : 0 : if (!ctx->quiet)
2747 : 0 : error ("modification of %qE is not a constant expression", object);
2748 : 0 : *non_constant_p = true;
2749 : 0 : return t;
2750 : : }
2751 : 1952 : type = TREE_TYPE (object);
2752 : 1952 : bool no_zero_init = true;
2753 : :
2754 : 3904 : releasing_vec ctors, indexes;
2755 : 3904 : auto_vec<int> index_pos_hints;
2756 : 1952 : bool activated_union_member_p = false;
2757 : 1954 : while (!refs->is_empty ())
2758 : : {
2759 : 2 : if (*valp == NULL_TREE)
2760 : : {
2761 : 0 : *valp = build_constructor (type, NULL);
2762 : 0 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
2763 : : }
2764 : 2 : else if (TREE_CODE (*valp) == STRING_CST)
2765 : : {
2766 : : /* An array was initialized with a string constant, and now
2767 : : we're writing into one of its elements. Explode the
2768 : : single initialization into a set of element
2769 : : initializations. */
2770 : 0 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
2771 : :
2772 : 0 : tree string = *valp;
2773 : 0 : tree elt_type = TREE_TYPE (type);
2774 : 0 : unsigned chars_per_elt
2775 : 0 : = (TYPE_PRECISION (elt_type) / TYPE_PRECISION (char_type_node));
2776 : 0 : unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt;
2777 : 0 : tree ary_ctor = build_constructor (type, NULL);
2778 : :
2779 : 0 : vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts);
2780 : 0 : for (unsigned ix = 0; ix != num_elts; ix++)
2781 : : {
2782 : 0 : constructor_elt elt
2783 : 0 : = {build_int_cst (size_type_node, ix),
2784 : 0 : extract_string_elt (string, chars_per_elt, ix)};
2785 : 0 : CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt);
2786 : : }
2787 : :
2788 : 0 : *valp = ary_ctor;
2789 : : }
2790 : :
2791 : : /* If the value of object is already zero-initialized, any new ctors for
2792 : : subobjects will also be zero-initialized. */
2793 : 2 : no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp);
2794 : :
2795 : 2 : enum tree_code code = TREE_CODE (type);
2796 : 2 : type = refs->pop ();
2797 : 2 : tree index = refs->pop ();
2798 : :
2799 : 2 : if (code == RECORD_TYPE && is_empty_field (index))
2800 : : /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they
2801 : : have no data and might have an offset lower than previously declared
2802 : : fields, which confuses the middle-end. The code below will notice
2803 : : that we don't have a CONSTRUCTOR for our inner target and just
2804 : : return init. */
2805 : : break;
2806 : :
2807 : 0 : if (code == UNION_TYPE && CONSTRUCTOR_NELTS (*valp)
2808 : 2 : && CONSTRUCTOR_ELT (*valp, 0)->index != index)
2809 : : {
2810 : 0 : if (TREE_CODE (t) == MODIFY_EXPR && CONSTRUCTOR_NO_CLEARING (*valp))
2811 : : {
2812 : : /* Diagnose changing the active union member while the union
2813 : : is in the process of being initialized. */
2814 : 0 : if (!ctx->quiet)
2815 : 0 : error_at (EXPR_LOCATION (t),
2816 : : "change of the active member of a union "
2817 : : "from %qD to %qD during initialization",
2818 : 0 : CONSTRUCTOR_ELT (*valp, 0)->index, index);
2819 : 0 : *non_constant_p = true;
2820 : : }
2821 : : no_zero_init = true;
2822 : : }
2823 : :
2824 : 2 : vec_safe_push (ctors, *valp);
2825 : 2 : vec_safe_push (indexes, index);
2826 : :
2827 : 2 : constructor_elt *cep = get_or_insert_ctor_field (*valp, index);
2828 : 2 : index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin ());
2829 : :
2830 : 2 : if (code == UNION_TYPE)
2831 : 0 : activated_union_member_p = true;
2832 : :
2833 : 2 : valp = &cep->value;
2834 : : }
2835 : :
2836 : : /* Detect modifying a constant object in constexpr evaluation.
2837 : : We have found a const object that is being modified. Figure out
2838 : : if we need to issue an error. Consider
2839 : :
2840 : : struct A {
2841 : : int n;
2842 : : constexpr A() : n(1) { n = 2; } // #1
2843 : : };
2844 : : struct B {
2845 : : const A a;
2846 : : constexpr B() { a.n = 3; } // #2
2847 : : };
2848 : : constexpr B b{};
2849 : :
2850 : : #1 is OK, since we're modifying an object under construction, but
2851 : : #2 is wrong, since "a" is const and has been fully constructed.
2852 : : To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
2853 : : which means that the object is read-only. For the example above, the
2854 : : *ctors stack at the point of #2 will look like:
2855 : :
2856 : : ctors[0] = {.a={.n=2}} TREE_READONLY = 0
2857 : : ctors[1] = {.n=2} TREE_READONLY = 1
2858 : :
2859 : : and we're modifying "b.a", so we search the stack and see if the
2860 : : constructor for "b.a" has already run. */
2861 : 1952 : if (const_object_being_modified)
2862 : : {
2863 : : bool fail = false;
2864 : : tree const_objtype
2865 : : = strip_array_types (TREE_TYPE (const_object_being_modified));
2866 : : if (!CLASS_TYPE_P (const_objtype))
2867 : : fail = true;
2868 : : else
2869 : : {
2870 : : /* [class.ctor]p5 "A constructor can be invoked for a const,
2871 : : volatile, or const volatile object. const and volatile
2872 : : semantics are not applied on an object under construction.
2873 : : They come into effect when the constructor for the most
2874 : : derived object ends." */
2875 : : for (tree elt : *ctors)
2876 : : if (same_type_ignoring_top_level_qualifiers_p (
2877 : : TREE_TYPE (const_object_being_modified), TREE_TYPE (elt)))
2878 : : {
2879 : : fail = TREE_READONLY (elt);
2880 : : break;
2881 : : }
2882 : : }
2883 : : if (fail)
2884 : : {
2885 : : if (!ctx->quiet)
2886 : : modifying_const_object_error (t, const_object_being_modified);
2887 : : *non_constant_p = true;
2888 : : return t;
2889 : : }
2890 : : }
2891 : :
2892 : 1952 : if (!preeval)
2893 : : {
2894 : : /* We're handling an INIT_EXPR of class type, so the value of the
2895 : : initializer can depend on the object it's initializing. */
2896 : :
2897 : : /* Create a new CONSTRUCTOR in case evaluation of the initializer
2898 : : wants to modify it. */
2899 : 0 : if (*valp == NULL_TREE)
2900 : : {
2901 : 0 : *valp = build_constructor (type, NULL);
2902 : 0 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
2903 : : }
2904 : 0 : new_ctx.ctor = *valp;
2905 : 0 : new_ctx.object = target;
2906 : : /* Avoid temporary materialization when initializing from a TARGET_EXPR.
2907 : : We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
2908 : : expansion of those trees uses ctx instead. */
2909 : 0 : if (TREE_CODE (init) == TARGET_EXPR)
2910 : 0 : if (tree tinit = TARGET_EXPR_INITIAL (init))
2911 : 0 : init = tinit;
2912 : 0 : init = eval_constant_expression (&new_ctx, init, false, non_constant_p,
2913 : : overflow_p);
2914 : : /* The hash table might have moved since the get earlier, and the
2915 : : initializer might have mutated the underlying CONSTRUCTORs, so we must
2916 : : recompute VALP. */
2917 : 0 : valp = ctx->global->values.get (object);
2918 : 0 : for (unsigned i = 0; i < vec_safe_length (indexes); i++)
2919 : : {
2920 : 0 : constructor_elt *cep
2921 : 0 : = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]);
2922 : 0 : valp = &cep->value;
2923 : : }
2924 : : }
2925 : :
2926 : 1952 : if (*non_constant_p)
2927 : : return t;
2928 : :
2929 : : /* Don't share a CONSTRUCTOR that might be changed later. */
2930 : 1952 : init = unshare_constructor (init);
2931 : 1952 : if (init == NULL_TREE)
2932 : : return t;
2933 : :
2934 : 1951 : if (*valp && TREE_CODE (*valp) == CONSTRUCTOR
2935 : 1 : && TREE_CODE (init) == CONSTRUCTOR)
2936 : : {
2937 : : /* An outer ctx->ctor might be pointing to *valp, so replace
2938 : : its contents. */
2939 : 1 : if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init),
2940 : 1 : TREE_TYPE (*valp)))
2941 : : {
2942 : : /* For initialization of an empty base, the original target will be
2943 : : *(base*)this, evaluation of which resolves to the object
2944 : : argument, which has the derived type rather than the base type. In
2945 : : this situation, just evaluate the initializer and return, since
2946 : : there's no actual data to store. */
2947 : 0 : gcc_assert (is_empty_class (TREE_TYPE (init)));
2948 : 0 : return lval ? target : init;
2949 : : }
2950 : 1 : CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
2951 : 1 : TREE_CONSTANT (*valp) = TREE_CONSTANT (init);
2952 : 1 : TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init);
2953 : 1 : CONSTRUCTOR_NO_CLEARING (*valp) = CONSTRUCTOR_NO_CLEARING (init);
2954 : : }
2955 : : // else if (TREE_CODE (init) == CONSTRUCTOR
2956 : : // && !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init),
2957 : : // type))
2958 : : // {
2959 : : // /* See above on initialization of empty bases. */
2960 : : // // gcc_assert (is_empty_class (TREE_TYPE (init)) && !lval);
2961 : : // return init;
2962 : : // }
2963 : : else
2964 : 1950 : *valp = init;
2965 : :
2966 : : /* After initialization, 'const' semantics apply to the value of the
2967 : : object. Make a note of this fact by marking the CONSTRUCTOR
2968 : : TREE_READONLY. */
2969 : 0 : if (TREE_CODE (t) == INIT_EXPR && TREE_CODE (*valp) == CONSTRUCTOR
2970 : 1951 : && TYPE_READONLY (type))
2971 : : {
2972 : : // this vs self? can rust's self be anything other than self or &self in
2973 : : // constexpr mode? if (INDIRECT_REF_P (target)
2974 : : // && (is_this_parameter (
2975 : : // tree_strip_nop_conversions (TREE_OPERAND (target, 0)))))
2976 : : /* We've just initialized '*this' (perhaps via the target
2977 : : constructor of a delegating constructor). Leave it up to the
2978 : : caller that set 'this' to set TREE_READONLY appropriately. */
2979 : : // gcc_checking_assert (
2980 : : // same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (target),
2981 : : // type));
2982 : : // else
2983 : : // TREE_READONLY (*valp) = true;
2984 : : }
2985 : :
2986 : : /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
2987 : : CONSTRUCTORs, if any. */
2988 : 1951 : bool c = TREE_CONSTANT (init);
2989 : 1951 : bool s = TREE_SIDE_EFFECTS (init);
2990 : 1951 : if (!c || s || activated_union_member_p)
2991 : 1 : for (tree elt : *ctors)
2992 : : {
2993 : 0 : if (!c)
2994 : 0 : TREE_CONSTANT (elt) = false;
2995 : 0 : if (s)
2996 : 0 : TREE_SIDE_EFFECTS (elt) = true;
2997 : : /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
2998 : : this union. */
2999 : 0 : if (TREE_CODE (TREE_TYPE (elt)) == UNION_TYPE)
3000 : 0 : CONSTRUCTOR_NO_CLEARING (elt) = false;
3001 : : }
3002 : :
3003 : 1951 : if (*non_constant_p)
3004 : : return t;
3005 : 1951 : else if (lval)
3006 : : return target;
3007 : : else
3008 : 1951 : return init;
3009 : 1952 : }
3010 : :
3011 : : /* Subroutine of cxx_eval_constant_expression.
3012 : : Like cxx_eval_unary_expression, except for binary expressions. */
3013 : : static tree
3014 : 43 : eval_binary_expression (const constexpr_ctx *ctx, tree t, bool lval,
3015 : : bool *non_constant_p, bool *overflow_p)
3016 : : {
3017 : 43 : tree orig_lhs = TREE_OPERAND (t, 0);
3018 : 43 : tree orig_rhs = TREE_OPERAND (t, 1);
3019 : 43 : tree lhs, rhs;
3020 : :
3021 : 43 : lhs = eval_constant_expression (ctx, orig_lhs, lval, non_constant_p,
3022 : : overflow_p);
3023 : 43 : rhs = eval_constant_expression (ctx, orig_rhs, lval, non_constant_p,
3024 : : overflow_p);
3025 : :
3026 : 43 : location_t loc = EXPR_LOCATION (t);
3027 : 43 : enum tree_code code = TREE_CODE (t);
3028 : 43 : tree type = TREE_TYPE (t);
3029 : :
3030 : 43 : return fold_binary_loc (loc, code, type, lhs, rhs);
3031 : : }
3032 : :
3033 : : /* Helper function of cxx_bind_parameters_in_call. Return non-NULL
3034 : : if *TP is address of a static variable (or part of it) currently being
3035 : : constructed or of a heap artificial variable. */
3036 : :
3037 : : static tree
3038 : 3 : addr_of_non_const_var (tree *tp, int *walk_subtrees, void *data)
3039 : : {
3040 : 3 : if (TREE_CODE (*tp) == ADDR_EXPR)
3041 : 0 : if (tree var = get_base_address (TREE_OPERAND (*tp, 0)))
3042 : 0 : if (VAR_P (var) && TREE_STATIC (var))
3043 : : {
3044 : 0 : if (DECL_NAME (var) == heap_uninit_identifier
3045 : 0 : || DECL_NAME (var) == heap_identifier
3046 : 0 : || DECL_NAME (var) == heap_vec_uninit_identifier
3047 : 0 : || DECL_NAME (var) == heap_vec_identifier)
3048 : 0 : return var;
3049 : :
3050 : 0 : constexpr_global_ctx *global = (constexpr_global_ctx *) data;
3051 : 0 : if (global->values.get (var))
3052 : : return var;
3053 : : }
3054 : 3 : if (TYPE_P (*tp))
3055 : 0 : *walk_subtrees = false;
3056 : : return NULL_TREE;
3057 : : }
3058 : :
3059 : : /* Subroutine of cxx_eval_call_expression.
3060 : : We are processing a call expression (either CALL_EXPR or
3061 : : AGGR_INIT_EXPR) in the context of CTX. Evaluate
3062 : : all arguments and bind their values to correspondings
3063 : : parameters, making up the NEW_CALL context. */
3064 : :
3065 : : static tree
3066 : 1938 : rs_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, tree fun,
3067 : : bool *non_constant_p, bool *overflow_p,
3068 : : bool *non_constant_args)
3069 : : {
3070 : 1938 : const int nargs = call_expr_nargs (t);
3071 : 1938 : tree parms = DECL_ARGUMENTS (fun);
3072 : 1938 : int i;
3073 : : /* We don't record ellipsis args below. */
3074 : 1938 : int nparms = list_length (parms);
3075 : 1938 : int nbinds = nargs < nparms ? nargs : nparms;
3076 : 1938 : tree binds = make_tree_vec (nbinds);
3077 : 1941 : for (i = 0; i < nargs; ++i)
3078 : : {
3079 : 3 : tree x, arg;
3080 : 3 : tree type = parms ? TREE_TYPE (parms) : void_type_node;
3081 : 3 : if (parms && DECL_BY_REFERENCE (parms))
3082 : 0 : type = TREE_TYPE (type);
3083 : 3 : x = CALL_EXPR_ARG (t, i);
3084 : :
3085 : 3 : if (TREE_ADDRESSABLE (type))
3086 : : /* Undo convert_for_arg_passing work here. */
3087 : 0 : x = convert_from_reference (x);
3088 : : /* Normally we would strip a TARGET_EXPR in an initialization context
3089 : : such as this, but here we do the elision differently: we keep the
3090 : : TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */
3091 : 3 : arg = eval_constant_expression (ctx, x, /*lval=*/false, non_constant_p,
3092 : : overflow_p);
3093 : : /* Don't VERIFY_CONSTANT here. */
3094 : 3 : if (*non_constant_p && ctx->quiet)
3095 : : break;
3096 : : /* Just discard ellipsis args after checking their constantitude. */
3097 : 3 : if (!parms)
3098 : 0 : continue;
3099 : :
3100 : 3 : if (!*non_constant_p)
3101 : : {
3102 : : /* Make sure the binding has the same type as the parm. But
3103 : : only for constant args. */
3104 : 3 : if (!TYPE_REF_P (type))
3105 : 3 : arg = adjust_temp_type (type, arg);
3106 : 3 : if (!TREE_CONSTANT (arg))
3107 : 0 : *non_constant_args = true;
3108 : 3 : else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3109 : : /* The destructor needs to see any modifications the callee makes
3110 : : to the argument. */
3111 : 0 : *non_constant_args = true;
3112 : : /* If arg is or contains address of a heap artificial variable or
3113 : : of a static variable being constructed, avoid caching the
3114 : : function call, as those variables might be modified by the
3115 : : function, or might be modified by the callers in between
3116 : : the cached function and just read by the function. */
3117 : 3 : else if (!*non_constant_args
3118 : 3 : && rs_walk_tree (&arg, addr_of_non_const_var, ctx->global,
3119 : : NULL))
3120 : 0 : *non_constant_args = true;
3121 : :
3122 : : // /* For virtual calls, adjust the this argument, so that it is
3123 : : // the object on which the method is called, rather than
3124 : : // one of its bases. */
3125 : : // if (i == 0 && DECL_VIRTUAL_P (fun))
3126 : : // {
3127 : : // tree addr = arg;
3128 : : // STRIP_NOPS (addr);
3129 : : // if (TREE_CODE (addr) == ADDR_EXPR)
3130 : : // {
3131 : : // tree obj = TREE_OPERAND (addr, 0);
3132 : : // while (TREE_CODE (obj) == COMPONENT_REF
3133 : : // && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1))
3134 : : // && !same_type_ignoring_top_level_qualifiers_p (
3135 : : // TREE_TYPE (obj), DECL_CONTEXT (fun)))
3136 : : // obj = TREE_OPERAND (obj, 0);
3137 : : // if (obj != TREE_OPERAND (addr, 0))
3138 : : // arg = build_fold_addr_expr_with_type (obj, TREE_TYPE
3139 : : // (arg));
3140 : : // }
3141 : : // }
3142 : 3 : TREE_VEC_ELT (binds, i) = arg;
3143 : : }
3144 : 3 : parms = TREE_CHAIN (parms);
3145 : : }
3146 : :
3147 : 1938 : return binds;
3148 : : }
3149 : :
3150 : : // forked from gcc/cp/constexpr.cc cxx_eval_builtin_function_call
3151 : :
3152 : : /* Attempt to evaluate T which represents a call to a builtin function.
3153 : : We assume here that all builtin functions evaluate to scalar types
3154 : : represented by _CST nodes. */
3155 : :
3156 : : static tree
3157 : 0 : eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun,
3158 : : bool lval, bool *non_constant_p, bool *overflow_p)
3159 : : {
3160 : 0 : const int nargs = call_expr_nargs (t);
3161 : 0 : tree *args = (tree *) alloca (nargs * sizeof (tree));
3162 : 0 : tree new_call;
3163 : 0 : int i;
3164 : :
3165 : : /* Don't fold __builtin_constant_p within a constexpr function. */
3166 : 0 : bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun);
3167 : :
3168 : : /* If we aren't requiring a constant expression, defer __builtin_constant_p
3169 : : in a constexpr function until we have values for the parameters. */
3170 : 0 : if (bi_const_p && !ctx->manifestly_const_eval && current_function_decl
3171 : 0 : && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
3172 : : {
3173 : 0 : *non_constant_p = true;
3174 : 0 : return t;
3175 : : }
3176 : :
3177 : : /* For __builtin_is_constant_evaluated, defer it if not
3178 : : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
3179 : : without manifestly_const_eval even expressions or parts thereof which
3180 : : will later be manifestly const_eval evaluated), otherwise fold it to
3181 : : true. */
3182 : 0 : if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
3183 : : BUILT_IN_FRONTEND))
3184 : : {
3185 : 0 : if (!ctx->manifestly_const_eval)
3186 : : {
3187 : 0 : *non_constant_p = true;
3188 : 0 : return t;
3189 : : }
3190 : 0 : return boolean_true_node;
3191 : : }
3192 : :
3193 : 0 : if (fndecl_built_in_p (fun, CP_BUILT_IN_SOURCE_LOCATION, BUILT_IN_FRONTEND))
3194 : : {
3195 : 0 : temp_override<tree> ovr (current_function_decl);
3196 : 0 : if (ctx->call && ctx->call->fundef)
3197 : 0 : current_function_decl = ctx->call->fundef->decl;
3198 : 0 : return fold_builtin_source_location (EXPR_LOCATION (t));
3199 : 0 : }
3200 : :
3201 : 0 : int strops = 0;
3202 : 0 : int strret = 0;
3203 : 0 : if (fndecl_built_in_p (fun, BUILT_IN_NORMAL))
3204 : 0 : switch (DECL_FUNCTION_CODE (fun))
3205 : : {
3206 : : case BUILT_IN_STRLEN:
3207 : : case BUILT_IN_STRNLEN:
3208 : 0 : strops = 1;
3209 : : break;
3210 : 0 : case BUILT_IN_MEMCHR:
3211 : 0 : case BUILT_IN_STRCHR:
3212 : 0 : case BUILT_IN_STRRCHR:
3213 : 0 : strops = 1;
3214 : 0 : strret = 1;
3215 : 0 : break;
3216 : 0 : case BUILT_IN_MEMCMP:
3217 : 0 : case BUILT_IN_STRCMP:
3218 : 0 : strops = 2;
3219 : 0 : break;
3220 : 0 : case BUILT_IN_STRSTR:
3221 : 0 : strops = 2;
3222 : 0 : strret = 1;
3223 : 0 : break;
3224 : 0 : case BUILT_IN_ASAN_POINTER_COMPARE:
3225 : 0 : case BUILT_IN_ASAN_POINTER_SUBTRACT:
3226 : : /* These builtins shall be ignored during constant expression
3227 : : evaluation. */
3228 : 0 : return void_node;
3229 : : default:
3230 : : break;
3231 : : }
3232 : :
3233 : : /* Be permissive for arguments to built-ins; __builtin_constant_p should
3234 : : return constant false for a non-constant argument. */
3235 : 0 : constexpr_ctx new_ctx = *ctx;
3236 : 0 : new_ctx.quiet = true;
3237 : 0 : for (i = 0; i < nargs; ++i)
3238 : : {
3239 : 0 : tree arg = CALL_EXPR_ARG (t, i);
3240 : 0 : tree oarg = arg;
3241 : :
3242 : : /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
3243 : : expand_builtin doesn't know how to look in the values table. */
3244 : 0 : bool strop = i < strops;
3245 : 0 : if (strop)
3246 : : {
3247 : 0 : STRIP_NOPS (arg);
3248 : 0 : if (TREE_CODE (arg) == ADDR_EXPR)
3249 : 0 : arg = TREE_OPERAND (arg, 0);
3250 : : else
3251 : : strop = false;
3252 : : }
3253 : :
3254 : : /* If builtin_valid_in_constant_expr_p is true,
3255 : : potential_constant_expression_1 has not recursed into the arguments
3256 : : of the builtin, verify it here. */
3257 : 0 : if (!builtin_valid_in_constant_expr_p (fun)
3258 : 0 : || potential_constant_expression (arg))
3259 : : {
3260 : 0 : bool dummy1 = false, dummy2 = false;
3261 : 0 : arg
3262 : 0 : = eval_constant_expression (&new_ctx, arg, false, &dummy1, &dummy2);
3263 : : }
3264 : :
3265 : 0 : if (bi_const_p)
3266 : : /* For __builtin_constant_p, fold all expressions with constant values
3267 : : even if they aren't C++ constant-expressions. */
3268 : 0 : arg = cp_fold_rvalue (arg);
3269 : 0 : else if (strop)
3270 : : {
3271 : 0 : if (TREE_CODE (arg) == CONSTRUCTOR)
3272 : 0 : arg = braced_lists_to_strings (TREE_TYPE (arg), arg);
3273 : 0 : if (TREE_CODE (arg) == STRING_CST)
3274 : 0 : arg = build_address (arg);
3275 : : else
3276 : : arg = oarg;
3277 : : }
3278 : :
3279 : 0 : args[i] = arg;
3280 : : }
3281 : :
3282 : 0 : bool save_ffbcp = force_folding_builtin_constant_p;
3283 : 0 : force_folding_builtin_constant_p |= ctx->manifestly_const_eval;
3284 : 0 : tree save_cur_fn = current_function_decl;
3285 : : /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
3286 : 0 : if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION) && ctx->call
3287 : 0 : && ctx->call->fundef)
3288 : 0 : current_function_decl = ctx->call->fundef->decl;
3289 : 0 : if (fndecl_built_in_p (fun,
3290 : : CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
3291 : : BUILT_IN_FRONTEND))
3292 : : {
3293 : 0 : location_t loc = EXPR_LOCATION (t);
3294 : 0 : if (nargs >= 1)
3295 : 0 : VERIFY_CONSTANT (args[0]);
3296 : 0 : new_call
3297 : 0 : = fold_builtin_is_pointer_inverconvertible_with_class (loc, nargs,
3298 : : args);
3299 : : }
3300 : 0 : else if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
3301 : : BUILT_IN_FRONTEND))
3302 : : {
3303 : 0 : location_t loc = EXPR_LOCATION (t);
3304 : 0 : if (nargs >= 2)
3305 : : {
3306 : 0 : VERIFY_CONSTANT (args[0]);
3307 : 0 : VERIFY_CONSTANT (args[1]);
3308 : : }
3309 : 0 : new_call = fold_builtin_is_corresponding_member (loc, nargs, args);
3310 : : }
3311 : : else
3312 : 0 : new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t),
3313 : 0 : CALL_EXPR_FN (t), nargs, args);
3314 : 0 : current_function_decl = save_cur_fn;
3315 : 0 : force_folding_builtin_constant_p = save_ffbcp;
3316 : 0 : if (new_call == NULL)
3317 : : {
3318 : 0 : if (!*non_constant_p && !ctx->quiet)
3319 : : {
3320 : : /* Do not allow__builtin_unreachable in constexpr function.
3321 : : The __builtin_unreachable call with BUILTINS_LOCATION
3322 : : comes from cp_maybe_instrument_return. */
3323 : 0 : if (fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE)
3324 : 0 : && EXPR_LOCATION (t) == BUILTINS_LOCATION)
3325 : 0 : error ("%<constexpr%> call flows off the end of the function");
3326 : : else
3327 : : {
3328 : 0 : new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
3329 : 0 : CALL_EXPR_FN (t), nargs, args);
3330 : 0 : error ("%q+E is not a constant expression", new_call);
3331 : : }
3332 : : }
3333 : 0 : *non_constant_p = true;
3334 : 0 : return t;
3335 : : }
3336 : :
3337 : 0 : if (!potential_constant_expression (new_call))
3338 : : {
3339 : 0 : if (!*non_constant_p && !ctx->quiet)
3340 : 0 : error ("%q+E is not a constant expression", new_call);
3341 : 0 : *non_constant_p = true;
3342 : 0 : return t;
3343 : : }
3344 : :
3345 : 0 : if (strret)
3346 : : {
3347 : : /* memchr returns a pointer into the first argument, but we replaced the
3348 : : argument above with a STRING_CST; put it back it now. */
3349 : 0 : tree op = CALL_EXPR_ARG (t, strret - 1);
3350 : 0 : STRIP_NOPS (new_call);
3351 : 0 : if (TREE_CODE (new_call) == POINTER_PLUS_EXPR)
3352 : 0 : TREE_OPERAND (new_call, 0) = op;
3353 : 0 : else if (TREE_CODE (new_call) == ADDR_EXPR)
3354 : 0 : new_call = op;
3355 : : }
3356 : :
3357 : 0 : return eval_constant_expression (&new_ctx, new_call, lval, non_constant_p,
3358 : 0 : overflow_p);
3359 : : }
3360 : :
3361 : : // Subroutine of cxx_eval_constant_expression.
3362 : : // Evaluate the call expression tree T in the context of OLD_CALL expression
3363 : : // evaluation.
3364 : : static tree
3365 : 1938 : eval_call_expression (const constexpr_ctx *ctx, tree t, bool lval,
3366 : : bool *non_constant_p, bool *overflow_p)
3367 : : {
3368 : 1938 : location_t loc = EXPR_LOCATION (t);
3369 : 1938 : tree fun = get_function_named_in_call (t);
3370 : 1938 : rust_constexpr_call new_call
3371 : 1938 : = {NULL, NULL, NULL, 0, ctx->manifestly_const_eval};
3372 : 1938 : int depth_ok;
3373 : :
3374 : 1938 : if (fun == NULL_TREE)
3375 : : {
3376 : : // return cxx_eval_internal_function (ctx, t, lval,
3377 : : // non_constant_p, overflow_p);
3378 : 0 : rust_unreachable ();
3379 : : return error_mark_node;
3380 : : }
3381 : :
3382 : 1938 : if (TREE_CODE (fun) != FUNCTION_DECL)
3383 : : {
3384 : 0 : if (!ctx->quiet && !*non_constant_p)
3385 : 0 : error_at (loc,
3386 : : "expression %qE does not designate a %<constexpr%> "
3387 : : "function",
3388 : : fun);
3389 : 0 : *non_constant_p = true;
3390 : 0 : return t;
3391 : : }
3392 : :
3393 : 1938 : if (fndecl_built_in_p (fun))
3394 : 0 : return eval_builtin_function_call (ctx, t, fun, lval, non_constant_p,
3395 : 0 : overflow_p);
3396 : :
3397 : 1938 : bool non_constant_args = false;
3398 : 1938 : new_call.bindings
3399 : 1938 : = rs_bind_parameters_in_call (ctx, t, fun, non_constant_p, overflow_p,
3400 : : &non_constant_args);
3401 : :
3402 : : /* We build up the bindings list before we know whether we already have this
3403 : : call cached. If we don't end up saving these bindings, ggc_free them when
3404 : : this function exits. */
3405 : 1938 : class free_bindings
3406 : : {
3407 : : tree *bindings;
3408 : :
3409 : : public:
3410 : 1938 : free_bindings (tree &b) : bindings (&b) {}
3411 : 1938 : ~free_bindings ()
3412 : : {
3413 : 1938 : if (bindings)
3414 : 0 : ggc_free (*bindings);
3415 : 1938 : }
3416 : 1938 : void preserve () { bindings = NULL; }
3417 : 1938 : } fb (new_call.bindings);
3418 : :
3419 : 1938 : if (*non_constant_p)
3420 : : return t;
3421 : :
3422 : : /* If in direct recursive call, optimize definition search. */
3423 : 1938 : if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun)
3424 : 0 : new_call.fundef = ctx->call->fundef;
3425 : : else
3426 : : {
3427 : 1938 : new_call.fundef = retrieve_constexpr_fundef (fun);
3428 : 1938 : if (new_call.fundef == NULL || new_call.fundef->body == NULL
3429 : 1938 : || new_call.fundef->result == error_mark_node
3430 : 1938 : || fun == current_function_decl)
3431 : : {
3432 : 0 : if (!ctx->quiet)
3433 : : {
3434 : : /* We need to check for current_function_decl here in case we're
3435 : : being called during cp_fold_function, because at that point
3436 : : DECL_INITIAL is set properly and we have a fundef but we
3437 : : haven't lowered invisirefs yet (c++/70344). */
3438 : 0 : if (DECL_INITIAL (fun) == error_mark_node
3439 : 0 : || fun == current_function_decl)
3440 : 0 : error_at (loc,
3441 : : "%qD called in a constant expression before its "
3442 : : "definition is complete",
3443 : : fun);
3444 : 0 : else if (DECL_INITIAL (fun))
3445 : : {
3446 : : // /* The definition of fun was somehow unsuitable. But
3447 : : // pretend
3448 : : // that lambda static thunks don't exist. */
3449 : : // if (!lambda_static_thunk_p (fun))
3450 : : // error_at (loc, "%qD called in a constant expression",
3451 : : // fun);
3452 : 0 : explain_invalid_constexpr_fn (fun);
3453 : : }
3454 : : else
3455 : 0 : error_at (loc, "%qD used before its definition", fun);
3456 : : }
3457 : 0 : *non_constant_p = true;
3458 : 0 : return t;
3459 : : }
3460 : : }
3461 : :
3462 : 1938 : depth_ok = push_cx_call_context (t);
3463 : :
3464 : 1938 : tree result = NULL_TREE;
3465 : 1938 : rust_constexpr_call *entry = NULL;
3466 : 1938 : if (depth_ok && !non_constant_args && ctx->strict)
3467 : : {
3468 : 1938 : new_call.hash = rust_constexpr_fundef_hasher::hash (new_call.fundef);
3469 : 1938 : new_call.hash = iterative_hash_object (new_call.bindings, new_call.hash);
3470 : 1938 : new_call.hash
3471 : 1938 : = iterative_hash_object (ctx->manifestly_const_eval, new_call.hash);
3472 : :
3473 : : /* If we have seen this call before, we are done. */
3474 : 1938 : maybe_initialize_constexpr_call_table ();
3475 : 1938 : rust_constexpr_call **slot
3476 : 1938 : = constexpr_call_table->find_slot (&new_call, INSERT);
3477 : 1938 : entry = *slot;
3478 : 1938 : if (entry == NULL)
3479 : : {
3480 : : /* Only cache up to constexpr_cache_depth to limit memory use. */
3481 : 1938 : if (depth_ok < constexpr_cache_depth)
3482 : : {
3483 : : /* We need to keep a pointer to the entry, not just the slot, as
3484 : : the slot can move during evaluation of the body. */
3485 : 1938 : *slot = entry = ggc_alloc<rust_constexpr_call> ();
3486 : 1938 : *entry = new_call;
3487 : 1938 : fb.preserve ();
3488 : : }
3489 : : }
3490 : : /* Calls that are in progress have their result set to NULL, so that we
3491 : : can detect circular dependencies. Now that we only cache up to
3492 : : constexpr_cache_depth this won't catch circular dependencies that
3493 : : start deeper, but they'll hit the recursion or ops limit. */
3494 : 0 : else if (entry->result == NULL)
3495 : : {
3496 : 0 : if (!ctx->quiet)
3497 : 0 : error ("call has circular dependency");
3498 : 0 : *non_constant_p = true;
3499 : 0 : entry->result = result = error_mark_node;
3500 : : }
3501 : : else
3502 : : result = entry->result;
3503 : : }
3504 : :
3505 : 1938 : if (!depth_ok)
3506 : : {
3507 : 0 : if (!ctx->quiet)
3508 : 0 : error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
3509 : : "%<-fconstexpr-depth=%> to increase the maximum)",
3510 : : max_constexpr_depth);
3511 : 0 : *non_constant_p = true;
3512 : 0 : result = error_mark_node;
3513 : : }
3514 : : else
3515 : : {
3516 : 1938 : bool cacheable = true;
3517 : 1938 : if (result && result != error_mark_node)
3518 : : /* OK */;
3519 : 1938 : else if (!DECL_SAVED_TREE (fun))
3520 : : {
3521 : : /* When at_eof >= 2, cgraph has started throwing away
3522 : : DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
3523 : : late code generation for VEC_INIT_EXPR, which needs to be
3524 : : completely reconsidered. */
3525 : : // gcc_assert (at_eof >= 2 && ctx->quiet);
3526 : 0 : *non_constant_p = true;
3527 : : }
3528 : 1938 : else if (tree copy = get_fundef_copy (new_call.fundef))
3529 : : {
3530 : 1938 : tree body, parms, res;
3531 : 1938 : releasing_vec ctors;
3532 : :
3533 : : /* Reuse or create a new unshared copy of this function's body. */
3534 : 1938 : body = TREE_PURPOSE (copy);
3535 : 1938 : parms = TREE_VALUE (copy);
3536 : 1938 : res = TREE_TYPE (copy);
3537 : :
3538 : : /* Associate the bindings with the remapped parms. */
3539 : 1938 : tree bound = new_call.bindings;
3540 : 1938 : tree remapped = parms;
3541 : 1941 : for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
3542 : : {
3543 : 3 : tree arg = TREE_VEC_ELT (bound, i);
3544 : 3 : if (entry)
3545 : : {
3546 : : /* Unshare args going into the hash table to separate them
3547 : : from the caller's context, for better GC and to avoid
3548 : : problems with verify_gimple. */
3549 : 3 : arg = unshare_expr_without_location (arg);
3550 : 3 : TREE_VEC_ELT (bound, i) = arg;
3551 : :
3552 : : /* And then unshare again so the callee doesn't change the
3553 : : argument values in the hash table. XXX Could we unshare
3554 : : lazily in cxx_eval_store_expression? */
3555 : 3 : arg = unshare_constructor (arg);
3556 : 3 : if (TREE_CODE (arg) == CONSTRUCTOR)
3557 : 0 : vec_safe_push (ctors, arg);
3558 : : }
3559 : :
3560 : 3 : ctx->global->values.put (remapped, arg);
3561 : 3 : remapped = DECL_CHAIN (remapped);
3562 : : }
3563 : : /* Add the RESULT_DECL to the values map, too. */
3564 : 1938 : gcc_assert (!DECL_BY_REFERENCE (res));
3565 : 1938 : ctx->global->values.put (res, NULL_TREE);
3566 : :
3567 : : /* Track the callee's evaluated SAVE_EXPRs and TARGET_EXPRs so that
3568 : : we can forget their values after the call. */
3569 : 1938 : constexpr_ctx ctx_with_save_exprs = *ctx;
3570 : 1938 : auto_vec<tree, 10> save_exprs;
3571 : 1938 : ctx_with_save_exprs.save_exprs = &save_exprs;
3572 : 1938 : ctx_with_save_exprs.call = &new_call;
3573 : 1938 : unsigned save_heap_alloc_count = ctx->global->heap_vars.length ();
3574 : 1938 : unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count;
3575 : :
3576 : 1938 : tree jump_target = NULL_TREE;
3577 : 1938 : eval_constant_expression (&ctx_with_save_exprs, body, lval,
3578 : : non_constant_p, overflow_p, &jump_target);
3579 : :
3580 : 1938 : if (VOID_TYPE_P (TREE_TYPE (res)))
3581 : 0 : result = void_node;
3582 : : else
3583 : : {
3584 : 1938 : result = *ctx->global->values.get (res);
3585 : 1938 : if (result == NULL_TREE && !*non_constant_p)
3586 : : {
3587 : 9 : *non_constant_p = true;
3588 : : }
3589 : : }
3590 : :
3591 : : /* Forget the saved values of the callee's SAVE_EXPRs and
3592 : : TARGET_EXPRs. */
3593 : 5814 : for (tree save_expr : save_exprs)
3594 : 0 : ctx->global->values.remove (save_expr);
3595 : :
3596 : : /* Remove the parms/result from the values map. Is it worth
3597 : : bothering to do this when the map itself is only live for
3598 : : one constexpr evaluation? If so, maybe also clear out
3599 : : other vars from call, maybe in BIND_EXPR handling? */
3600 : 1938 : ctx->global->values.remove (res);
3601 : 1941 : for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
3602 : 3 : ctx->global->values.remove (parm);
3603 : :
3604 : : /* Make the unshared function copy we used available for re-use. */
3605 : 1938 : save_fundef_copy (fun, copy);
3606 : :
3607 : : /* If the call allocated some heap object that hasn't been
3608 : : deallocated during the call, or if it deallocated some heap
3609 : : object it has not allocated, the call isn't really stateless
3610 : : for the constexpr evaluation and should not be cached.
3611 : : It is fine if the call allocates something and deallocates it
3612 : : too. */
3613 : 1938 : if (entry
3614 : 3876 : && (save_heap_alloc_count != ctx->global->heap_vars.length ()
3615 : 1938 : || (save_heap_dealloc_count
3616 : 1938 : != ctx->global->heap_dealloc_count)))
3617 : : {
3618 : 0 : tree heap_var;
3619 : 0 : unsigned int i;
3620 : 0 : if ((ctx->global->heap_vars.length ()
3621 : 0 : - ctx->global->heap_dealloc_count)
3622 : 0 : != save_heap_alloc_count - save_heap_dealloc_count)
3623 : : cacheable = false;
3624 : : else
3625 : 1938 : FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var,
3626 : : save_heap_alloc_count)
3627 : 0 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
3628 : : {
3629 : : cacheable = false;
3630 : : break;
3631 : : }
3632 : : }
3633 : 1938 : }
3634 : : else
3635 : : /* Couldn't get a function copy to evaluate. */
3636 : 0 : *non_constant_p = true;
3637 : :
3638 : 1938 : if (result == error_mark_node)
3639 : 0 : *non_constant_p = true;
3640 : 1938 : if (*non_constant_p || *overflow_p)
3641 : : result = error_mark_node;
3642 : 1928 : else if (!result)
3643 : 0 : result = void_node;
3644 : 1938 : if (entry)
3645 : 1938 : entry->result = cacheable ? result : error_mark_node;
3646 : : }
3647 : :
3648 : 1938 : pop_cx_call_context ();
3649 : 1938 : return result;
3650 : 1938 : }
3651 : :
3652 : : /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
3653 : : for a member of an anonymous aggregate, INIT is the initializer for that
3654 : : member, and VEC_OUTER is the vector of constructor elements for the class
3655 : : whose constructor we are processing. Add the initializer to the vector
3656 : : and return true to indicate success. */
3657 : :
3658 : : // static bool
3659 : : // build_anon_member_initialization (tree member, tree init,
3660 : : // vec<constructor_elt, va_gc> **vec_outer)
3661 : : // {
3662 : : // /* MEMBER presents the relevant fields from the inside out, but we need
3663 : : // to build up the initializer from the outside in so that we can reuse
3664 : : // previously built CONSTRUCTORs if this is, say, the second field in an
3665 : : // anonymous struct. So we use a vec as a stack. */
3666 : : // auto_vec<tree, 2> fields;
3667 : : // do
3668 : : // {
3669 : : // fields.safe_push (TREE_OPERAND (member, 1));
3670 : : // member = TREE_OPERAND (member, 0);
3671 : : // } while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
3672 : : // && TREE_CODE (member) == COMPONENT_REF);
3673 : : //
3674 : : // /* VEC has the constructor elements vector for the context of FIELD.
3675 : : // If FIELD is an anonymous aggregate, we will push inside it. */
3676 : : // vec<constructor_elt, va_gc> **vec = vec_outer;
3677 : : // tree field;
3678 : : // while (field = fields.pop (), ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3679 : : // {
3680 : : // tree ctor;
3681 : : // /* If there is already an outer constructor entry for the anonymous
3682 : : // aggregate FIELD, use it; otherwise, insert one. */
3683 : : // if (vec_safe_is_empty (*vec) || (*vec)->last ().index != field)
3684 : : // {
3685 : : // ctor = build_constructor (TREE_TYPE (field), NULL);
3686 : : // CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
3687 : : // }
3688 : : // else
3689 : : // ctor = (*vec)->last ().value;
3690 : : // vec = &CONSTRUCTOR_ELTS (ctor);
3691 : : // }
3692 : : //
3693 : : // /* Now we're at the innermost field, the one that isn't an anonymous
3694 : : // aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
3695 : : // gcc_assert (fields.is_empty ());
3696 : : // CONSTRUCTOR_APPEND_ELT (*vec, field, init);
3697 : : //
3698 : : // return true;
3699 : : // }
3700 : :
3701 : : ///* V is a vector of constructor elements built up for the base and member
3702 : : // initializers of a constructor for TYPE. They need to be in increasing
3703 : : // offset order, which they might not be yet if TYPE has a primary base
3704 : : // which is not first in the base-clause or a vptr and at least one base
3705 : : // all of which are non-primary. */
3706 : : //
3707 : : // static vec<constructor_elt, va_gc> *
3708 : : // sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
3709 : : //{
3710 : : // tree pri = CLASSTYPE_PRIMARY_BINFO (type);
3711 : : // tree field_type;
3712 : : // unsigned i;
3713 : : // constructor_elt *ce;
3714 : : //
3715 : : // if (pri)
3716 : : // field_type = BINFO_TYPE (pri);
3717 : : // else if (TYPE_CONTAINS_VPTR_P (type))
3718 : : // field_type = vtbl_ptr_type_node;
3719 : : // else
3720 : : // return v;
3721 : : //
3722 : : // /* Find the element for the primary base or vptr and move it to the
3723 : : // beginning of the vec. */
3724 : : // for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
3725 : : // if (TREE_TYPE (ce->index) == field_type)
3726 : : // break;
3727 : : //
3728 : : // if (i > 0 && i < vec_safe_length (v))
3729 : : // {
3730 : : // vec<constructor_elt, va_gc> &vref = *v;
3731 : : // constructor_elt elt = vref[i];
3732 : : // for (; i > 0; --i)
3733 : : // vref[i] = vref[i - 1];
3734 : : // vref[0] = elt;
3735 : : // }
3736 : : //
3737 : : // return v;
3738 : : //}
3739 : :
3740 : : /* Subroutine of build_constexpr_constructor_member_initializers.
3741 : : The expression tree T represents a data member initialization
3742 : : in a (constexpr) constructor definition. Build a pairing of
3743 : : the data member with its initializer, and prepend that pair
3744 : : to the existing initialization pair INITS. */
3745 : :
3746 : : static bool
3747 : 0 : build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
3748 : : {
3749 : 0 : tree member;
3750 : 0 : if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
3751 : 0 : t = TREE_OPERAND (t, 0);
3752 : 0 : if (TREE_CODE (t) == EXPR_STMT)
3753 : 0 : t = TREE_OPERAND (t, 0);
3754 : 0 : if (t == error_mark_node)
3755 : : return false;
3756 : 0 : if (TREE_CODE (t) == STATEMENT_LIST)
3757 : : {
3758 : 0 : for (tree stmt : tsi_range (t))
3759 : 0 : if (!build_data_member_initialization (stmt, vec))
3760 : 0 : return false;
3761 : : return true;
3762 : : }
3763 : 0 : if (TREE_CODE (t) == CONVERT_EXPR)
3764 : 0 : t = TREE_OPERAND (t, 0);
3765 : 0 : if (TREE_CODE (t) == INIT_EXPR
3766 : : /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
3767 : : use what this function builds for cx_check_missing_mem_inits, and
3768 : : assignment in the ctor body doesn't count. */
3769 : 0 : || (TREE_CODE (t) == MODIFY_EXPR))
3770 : : {
3771 : 0 : member = TREE_OPERAND (t, 0);
3772 : : // Faisal: not sure if we need to port over break_out_target_exprs
3773 : : // if not, then not sure how to handle init in this case
3774 : : // init = break_out_target_exprs (TREE_OPERAND (t, 1));
3775 : : }
3776 : 0 : else if (TREE_CODE (t) == CALL_EXPR)
3777 : : {
3778 : 0 : tree fn = get_callee_fndecl (t);
3779 : 0 : if (!fn || !DECL_CONSTRUCTOR_P (fn))
3780 : : /* We're only interested in calls to subobject constructors. */
3781 : : return true;
3782 : 0 : member = CALL_EXPR_ARG (t, 0);
3783 : : /* We don't use build_cplus_new here because it complains about
3784 : : abstract bases. Leaving the call unwrapped means that it has the
3785 : : wrong type, but cxx_eval_constant_expression doesn't care. */
3786 : : // Faisal: not sure if we need to port over break_out_target_exprs
3787 : : // if not, then not sure how to handle init in this case
3788 : : // init = break_out_target_exprs (t);
3789 : : }
3790 : 0 : else if (TREE_CODE (t) == BIND_EXPR)
3791 : 0 : return build_data_member_initialization (BIND_EXPR_BODY (t), vec);
3792 : : else
3793 : : /* Don't add anything else to the CONSTRUCTOR. */
3794 : : return true;
3795 : 0 : if (INDIRECT_REF_P (member))
3796 : 0 : member = TREE_OPERAND (member, 0);
3797 : 0 : if (TREE_CODE (member) == NOP_EXPR)
3798 : : {
3799 : 0 : tree op = member;
3800 : 0 : STRIP_NOPS (op);
3801 : 0 : if (TREE_CODE (op) == ADDR_EXPR)
3802 : : {
3803 : 0 : gcc_assert (same_type_ignoring_top_level_qualifiers_p (
3804 : : TREE_TYPE (TREE_TYPE (op)), TREE_TYPE (TREE_TYPE (member))));
3805 : : /* Initializing a cv-qualified member; we need to look through
3806 : : the const_cast. */
3807 : : member = op;
3808 : : }
3809 : 0 : else if (op == current_class_ptr
3810 : 0 : && (same_type_ignoring_top_level_qualifiers_p (
3811 : 0 : TREE_TYPE (TREE_TYPE (member)), current_class_type)))
3812 : : /* Delegating constructor. */
3813 : : member = op;
3814 : : else
3815 : : {
3816 : : /* This is an initializer for an empty base; keep it for now so
3817 : : we can check it in cxx_eval_bare_aggregate. */
3818 : 0 : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
3819 : : }
3820 : : }
3821 : 0 : if (TREE_CODE (member) == ADDR_EXPR)
3822 : 0 : member = TREE_OPERAND (member, 0);
3823 : 0 : if (TREE_CODE (member) == COMPONENT_REF)
3824 : : {
3825 : 0 : tree aggr = TREE_OPERAND (member, 0);
3826 : 0 : if (TREE_CODE (aggr) == VAR_DECL)
3827 : : /* Initializing a local variable, don't add anything. */
3828 : : return true;
3829 : 0 : if (TREE_CODE (aggr) != COMPONENT_REF)
3830 : : /* Normal member initialization. */
3831 : 0 : member = TREE_OPERAND (member, 1);
3832 : 0 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
3833 : : /* Initializing a member of an anonymous union. */
3834 : 0 : rust_sorry_at (UNDEF_LOCATION,
3835 : : "cannot handle value initialization yet");
3836 : : // return build_anon_member_initialization (member, init, vec);
3837 : : else
3838 : : /* We're initializing a vtable pointer in a base. Leave it as
3839 : : COMPONENT_REF so we remember the path to get to the vfield. */
3840 : 0 : gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
3841 : : }
3842 : :
3843 : : /* Value-initialization can produce multiple initializers for the
3844 : : same field; use the last one. */
3845 : 0 : if (!vec_safe_is_empty (*vec) && (*vec)->last ().index == member)
3846 : 0 : rust_sorry_at (UNDEF_LOCATION, "cannot handle value initialization yet");
3847 : : // (*vec)->last ().value = init;
3848 : : else
3849 : 0 : rust_sorry_at (UNDEF_LOCATION, "cannot handle value initialization yet");
3850 : : // CONSTRUCTOR_APPEND_ELT (*vec, member, init);
3851 : : return true;
3852 : : }
3853 : :
3854 : : ///* Build compile-time evalable representations of member-initializer list
3855 : : // for a constexpr constructor. */
3856 : : //
3857 : : // static tree
3858 : : // build_constexpr_constructor_member_initializers (tree type, tree body)
3859 : : //{
3860 : : // vec<constructor_elt, va_gc> *vec = NULL;
3861 : : // bool ok = true;
3862 : : // while (true)
3863 : : // switch (TREE_CODE (body))
3864 : : // {
3865 : : // case STATEMENT_LIST:
3866 : : // for (tree stmt : tsi_range (body))
3867 : : // {
3868 : : // body = stmt;
3869 : : // if (TREE_CODE (body) == BIND_EXPR)
3870 : : // break;
3871 : : // }
3872 : : // break;
3873 : : //
3874 : : // case BIND_EXPR:
3875 : : // body = BIND_EXPR_BODY (body);
3876 : : // goto found;
3877 : : //
3878 : : // default:
3879 : : // rust_unreachable ();
3880 : : // }
3881 : : // found:
3882 : : //
3883 : : // if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
3884 : : // {
3885 : : // body = TREE_OPERAND (body, 0);
3886 : : // if (TREE_CODE (body) == EXPR_STMT)
3887 : : // body = TREE_OPERAND (body, 0);
3888 : : // if (TREE_CODE (body) == INIT_EXPR
3889 : : // && (same_type_ignoring_top_level_qualifiers_p (
3890 : : // TREE_TYPE (TREE_OPERAND (body, 0)), current_class_type)))
3891 : : // {
3892 : : // /* Trivial copy. */
3893 : : // return TREE_OPERAND (body, 1);
3894 : : // }
3895 : : // ok = build_data_member_initialization (body, &vec);
3896 : : // }
3897 : : // else if (TREE_CODE (body) == STATEMENT_LIST)
3898 : : // {
3899 : : // for (tree stmt : tsi_range (body))
3900 : : // {
3901 : : // ok = build_data_member_initialization (stmt, &vec);
3902 : : // if (!ok)
3903 : : // break;
3904 : : // }
3905 : : // }
3906 : : // else if (EXPR_P (body))
3907 : : // ok = build_data_member_initialization (body, &vec);
3908 : : // else
3909 : : // gcc_assert (errorcount > 0);
3910 : : // if (ok)
3911 : : // {
3912 : : // if (vec_safe_length (vec) > 0)
3913 : : // {
3914 : : // /* In a delegating constructor, return the target. */
3915 : : // constructor_elt *ce = &(*vec)[0];
3916 : : // if (ce->index == current_class_ptr)
3917 : : // {
3918 : : // body = ce->value;
3919 : : // vec_free (vec);
3920 : : // return body;
3921 : : // }
3922 : : // }
3923 : : // vec = sort_constexpr_mem_initializers (type, vec);
3924 : : // return build_constructor (type, vec);
3925 : : // }
3926 : : // else
3927 : : // return error_mark_node;
3928 : : //}
3929 : :
3930 : : // Subroutine of check_constexpr_fundef. BODY is the body of a function
3931 : : // declared to be constexpr, or a sub-statement thereof. Returns the
3932 : : // return value if suitable, error_mark_node for a statement not allowed in
3933 : : // a constexpr function, or NULL_TREE if no return value was found.
3934 : : static tree
3935 : 0 : constexpr_fn_retval (const constexpr_ctx *ctx, tree body)
3936 : : {
3937 : 0 : switch (TREE_CODE (body))
3938 : : {
3939 : 0 : case STATEMENT_LIST:
3940 : 0 : {
3941 : 0 : tree expr = NULL_TREE;
3942 : 0 : for (tree stmt : tsi_range (body))
3943 : : {
3944 : 0 : tree s = constexpr_fn_retval (ctx, stmt);
3945 : 0 : if (s == error_mark_node)
3946 : : return error_mark_node;
3947 : 0 : else if (s == NULL_TREE)
3948 : : /* Keep iterating. */;
3949 : 0 : else if (expr)
3950 : : /* Multiple return statements. */
3951 : : return error_mark_node;
3952 : : else
3953 : : expr = s;
3954 : : }
3955 : : return expr;
3956 : : }
3957 : :
3958 : 0 : case RETURN_EXPR:
3959 : 0 : {
3960 : 0 : bool non_constant_p = false;
3961 : 0 : bool overflow_p = false;
3962 : 0 : return eval_constant_expression (ctx, body, false, &non_constant_p,
3963 : : &overflow_p);
3964 : : }
3965 : 0 : case DECL_EXPR:
3966 : 0 : {
3967 : 0 : tree decl = DECL_EXPR_DECL (body);
3968 : 0 : if (TREE_CODE (decl) == USING_DECL
3969 : : /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
3970 : 0 : || DECL_ARTIFICIAL (decl))
3971 : : return NULL_TREE;
3972 : 0 : return error_mark_node;
3973 : : }
3974 : :
3975 : 0 : case CLEANUP_POINT_EXPR:
3976 : 0 : return constexpr_fn_retval (ctx, TREE_OPERAND (body, 0));
3977 : :
3978 : 0 : case BIND_EXPR:
3979 : 0 : {
3980 : 0 : tree b = BIND_EXPR_BODY (body);
3981 : 0 : return constexpr_fn_retval (ctx, b);
3982 : : }
3983 : 0 : break;
3984 : :
3985 : 0 : default:
3986 : 0 : return error_mark_node;
3987 : : }
3988 : : return error_mark_node;
3989 : : }
3990 : :
3991 : : // Taken from cp/constexpr.cc
3992 : : //
3993 : : // If DECL is a scalar enumeration constant or variable with a
3994 : : // constant initializer, return the initializer (or, its initializers,
3995 : : // recursively); otherwise, return DECL. If STRICT_P, the
3996 : : // initializer is only returned if DECL is a
3997 : : // constant-expression. If RETURN_AGGREGATE_CST_OK_P, it is ok to
3998 : : // return an aggregate constant. If UNSHARE_P, return an unshared
3999 : : // copy of the initializer.
4000 : : static tree
4001 : 1172 : constant_value_1 (tree decl, bool, bool, bool unshare_p)
4002 : : {
4003 : 2344 : while (TREE_CODE (decl) == CONST_DECL)
4004 : : {
4005 : 1172 : tree init;
4006 : : /* If DECL is a static data member in a template
4007 : : specialization, we must instantiate it here. The
4008 : : initializer for the static data member is not processed
4009 : : until needed; we need it now. */
4010 : :
4011 : 1172 : init = DECL_INITIAL (decl);
4012 : 1172 : if (init == error_mark_node)
4013 : : {
4014 : : if (TREE_CODE (decl) == CONST_DECL)
4015 : : /* Treat the error as a constant to avoid cascading errors on
4016 : : excessively recursive template instantiation (c++/9335). */
4017 : : return init;
4018 : : else
4019 : : return decl;
4020 : : }
4021 : :
4022 : : decl = init;
4023 : : }
4024 : 1172 : return unshare_p ? unshare_expr (decl) : decl;
4025 : : }
4026 : :
4027 : : // A more relaxed version of decl_really_constant_value, used by the
4028 : : // common C/C++ code.
4029 : : tree
4030 : 1172 : decl_constant_value (tree decl, bool unshare_p)
4031 : : {
4032 : 1172 : return constant_value_1 (decl, /*strict_p=*/false,
4033 : : /*return_aggregate_cst_ok_p=*/true,
4034 : 1172 : /*unshare_p=*/unshare_p);
4035 : : }
4036 : :
4037 : : static void
4038 : 0 : non_const_var_error (location_t loc, tree r)
4039 : : {
4040 : 0 : error_at (loc,
4041 : : "the value of %qD is not usable in a constant "
4042 : : "expression",
4043 : : r);
4044 : : /* Avoid error cascade. */
4045 : 0 : if (DECL_INITIAL (r) == error_mark_node)
4046 : : return;
4047 : :
4048 : : // more in cp/constexpr.cc
4049 : : }
4050 : :
4051 : : static tree
4052 : 1938 : get_callee (tree call)
4053 : : {
4054 : 1938 : if (call == NULL_TREE)
4055 : : return call;
4056 : 1938 : else if (TREE_CODE (call) == CALL_EXPR)
4057 : 1938 : return CALL_EXPR_FN (call);
4058 : :
4059 : : return NULL_TREE;
4060 : : }
4061 : :
4062 : : // We have an expression tree T that represents a call, either CALL_EXPR
4063 : : // or AGGR_INIT_EXPR. If the call is lexically to a named function,
4064 : : // return the _DECL for that function.
4065 : : static tree
4066 : 1938 : get_function_named_in_call (tree t)
4067 : : {
4068 : 1938 : tree fun = get_callee (t);
4069 : 1938 : if (fun && TREE_CODE (fun) == ADDR_EXPR
4070 : 2291 : && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
4071 : 353 : fun = TREE_OPERAND (fun, 0);
4072 : 1938 : return fun;
4073 : : }
4074 : :
4075 : : // forked from gcc/cp/constexpr.cc maybe_constexpr_fn
4076 : :
4077 : : /* True if a function might be declared constexpr */
4078 : :
4079 : : bool
4080 : 0 : maybe_constexpr_fn (tree t)
4081 : : {
4082 : 0 : return (DECL_DECLARED_CONSTEXPR_P (t));
4083 : : }
4084 : :
4085 : : // forked from gcc/cp/constexpr.cc var_in_maybe_constexpr_fn
4086 : :
4087 : : /* True if T was declared in a function that might be constexpr: either a
4088 : : function that was declared constexpr. */
4089 : :
4090 : : bool
4091 : 0 : var_in_maybe_constexpr_fn (tree t)
4092 : : {
4093 : 0 : return (DECL_FUNCTION_SCOPE_P (t) && maybe_constexpr_fn (DECL_CONTEXT (t)));
4094 : : }
4095 : :
4096 : : /* P0859: A function is needed for constant evaluation if it is a constexpr
4097 : : function that is named by an expression ([basic.def.odr]) that is
4098 : : potentially constant evaluated.
4099 : :
4100 : : So we need to instantiate any constexpr functions mentioned by the
4101 : : expression even if the definition isn't needed for evaluating the
4102 : : expression. */
4103 : :
4104 : : static tree
4105 : 0 : instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void * /*data*/)
4106 : : {
4107 : 0 : if (TREE_CODE (*tp) == CALL_EXPR)
4108 : : {
4109 : 0 : if (EXPR_HAS_LOCATION (*tp))
4110 : 0 : input_location = EXPR_LOCATION (*tp);
4111 : : }
4112 : :
4113 : 0 : if (!EXPR_P (*tp))
4114 : 0 : *walk_subtrees = 0;
4115 : :
4116 : 0 : return NULL_TREE;
4117 : : }
4118 : :
4119 : : static void
4120 : 0 : instantiate_constexpr_fns (tree t)
4121 : : {
4122 : 0 : location_t loc = input_location;
4123 : 0 : rs_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL);
4124 : 0 : input_location = loc;
4125 : 0 : }
4126 : :
4127 : : /* Returns less than, equal to, or greater than zero if KEY is found to be
4128 : : less than, to match, or to be greater than the constructor_elt's INDEX. */
4129 : :
4130 : : static int
4131 : 0 : array_index_cmp (tree key, tree index)
4132 : : {
4133 : 0 : gcc_assert (TREE_CODE (key) == INTEGER_CST);
4134 : :
4135 : 0 : switch (TREE_CODE (index))
4136 : : {
4137 : 0 : case INTEGER_CST:
4138 : 0 : return tree_int_cst_compare (key, index);
4139 : 0 : case RANGE_EXPR:
4140 : 0 : {
4141 : 0 : tree lo = TREE_OPERAND (index, 0);
4142 : 0 : tree hi = TREE_OPERAND (index, 1);
4143 : 0 : if (tree_int_cst_lt (key, lo))
4144 : : return -1;
4145 : 0 : else if (tree_int_cst_lt (hi, key))
4146 : : return 1;
4147 : : else
4148 : 0 : return 0;
4149 : : }
4150 : 0 : default:
4151 : 0 : rust_unreachable ();
4152 : : }
4153 : : }
4154 : :
4155 : : /* If T is a CONSTRUCTOR, return an unshared copy of T and any
4156 : : sub-CONSTRUCTORs. Otherwise return T.
4157 : :
4158 : : We use this whenever we initialize an object as a whole, whether it's a
4159 : : parameter, a local variable, or a subobject, so that subsequent
4160 : : modifications don't affect other places where it was used. */
4161 : :
4162 : : tree
4163 : 1980 : unshare_constructor (tree t MEM_STAT_DECL)
4164 : : {
4165 : 1980 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
4166 : : return t;
4167 : 63 : auto_vec<tree *, 4> ptrs;
4168 : 63 : ptrs.safe_push (&t);
4169 : 63 : while (!ptrs.is_empty ())
4170 : : {
4171 : 66 : tree *p = ptrs.pop ();
4172 : 66 : tree n = copy_node (*p PASS_MEM_STAT);
4173 : 132 : CONSTRUCTOR_ELTS (n)
4174 : 127 : = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT);
4175 : 66 : *p = n;
4176 : 66 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n);
4177 : 66 : constructor_elt *ce;
4178 : 585 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i)
4179 : 390 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
4180 : 3 : ptrs.safe_push (&ce->value);
4181 : : }
4182 : 63 : return t;
4183 : 63 : }
4184 : :
4185 : : /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
4186 : : if none. If INSERT is true, insert a matching element rather than fail. */
4187 : :
4188 : : static HOST_WIDE_INT
4189 : 9 : find_array_ctor_elt (tree ary, tree dindex, bool insert)
4190 : : {
4191 : 9 : if (tree_int_cst_sgn (dindex) < 0)
4192 : : return -1;
4193 : :
4194 : 9 : unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
4195 : 9 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
4196 : 9 : unsigned HOST_WIDE_INT len = vec_safe_length (elts);
4197 : :
4198 : 9 : unsigned HOST_WIDE_INT end = len;
4199 : 9 : unsigned HOST_WIDE_INT begin = 0;
4200 : :
4201 : : /* If the last element of the CONSTRUCTOR has its own index, we can assume
4202 : : that the same is true of the other elements and index directly. */
4203 : 9 : if (end > 0)
4204 : : {
4205 : 9 : tree cindex = (*elts)[end - 1].index;
4206 : 9 : if (cindex == NULL_TREE)
4207 : : {
4208 : : /* Verify that if the last index is missing, all indexes
4209 : : are missing. */
4210 : 0 : if (flag_checking)
4211 : 0 : for (unsigned int j = 0; j < len - 1; ++j)
4212 : 0 : gcc_assert ((*elts)[j].index == NULL_TREE);
4213 : 0 : if (i < end)
4214 : 0 : return i;
4215 : : else
4216 : : {
4217 : 0 : begin = end;
4218 : 0 : if (i == end)
4219 : : /* If the element is to be added right at the end,
4220 : : make sure it is added with cleared index too. */
4221 : 0 : dindex = NULL_TREE;
4222 : 0 : else if (insert)
4223 : : /* Otherwise, in order not to break the assumption
4224 : : that CONSTRUCTOR either has all indexes or none,
4225 : : we need to add indexes to all elements. */
4226 : 0 : for (unsigned int j = 0; j < len; ++j)
4227 : 0 : (*elts)[j].index = build_int_cst (TREE_TYPE (dindex), j);
4228 : : }
4229 : : }
4230 : 9 : else if (TREE_CODE (cindex) == INTEGER_CST
4231 : 9 : && compare_tree_int (cindex, end - 1) == 0)
4232 : : {
4233 : 9 : if (i < end)
4234 : 9 : return i;
4235 : : else
4236 : 0 : begin = end;
4237 : : }
4238 : : }
4239 : :
4240 : : /* Otherwise, find a matching index by means of a binary search. */
4241 : 0 : while (begin != end)
4242 : : {
4243 : 0 : unsigned HOST_WIDE_INT middle = (begin + end) / 2;
4244 : 0 : constructor_elt &elt = (*elts)[middle];
4245 : 0 : tree idx = elt.index;
4246 : :
4247 : 0 : int cmp = array_index_cmp (dindex, idx);
4248 : 0 : if (cmp < 0)
4249 : : end = middle;
4250 : 0 : else if (cmp > 0)
4251 : 0 : begin = middle + 1;
4252 : : else
4253 : : {
4254 : 0 : if (insert && TREE_CODE (idx) == RANGE_EXPR)
4255 : : {
4256 : : /* We need to split the range. */
4257 : 0 : constructor_elt e;
4258 : 0 : tree lo = TREE_OPERAND (idx, 0);
4259 : 0 : tree hi = TREE_OPERAND (idx, 1);
4260 : 0 : tree value = elt.value;
4261 : 0 : dindex = fold_convert (sizetype, dindex);
4262 : 0 : if (tree_int_cst_lt (lo, dindex))
4263 : : {
4264 : : /* There are still some lower elts; shorten the range. */
4265 : 0 : tree new_hi
4266 : 0 : = int_const_binop (MINUS_EXPR, dindex, size_one_node);
4267 : 0 : if (tree_int_cst_equal (lo, new_hi))
4268 : : /* Only one element left, no longer a range. */
4269 : 0 : elt.index = lo;
4270 : : else
4271 : 0 : TREE_OPERAND (idx, 1) = new_hi;
4272 : : /* Append the element we want to insert. */
4273 : 0 : ++middle;
4274 : 0 : e.index = dindex;
4275 : 0 : e.value = unshare_constructor (value);
4276 : 0 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
4277 : : }
4278 : : else
4279 : : /* No lower elts, the range elt is now ours. */
4280 : 0 : elt.index = dindex;
4281 : :
4282 : 0 : if (tree_int_cst_lt (dindex, hi))
4283 : : {
4284 : : /* There are still some higher elts; append a range. */
4285 : 0 : tree new_lo
4286 : 0 : = int_const_binop (PLUS_EXPR, dindex, size_one_node);
4287 : 0 : if (tree_int_cst_equal (new_lo, hi))
4288 : 0 : e.index = hi;
4289 : : else
4290 : 0 : e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi);
4291 : 0 : e.value = unshare_constructor (value);
4292 : 0 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
4293 : : }
4294 : : }
4295 : 0 : return middle;
4296 : : }
4297 : : }
4298 : :
4299 : 0 : if (insert)
4300 : : {
4301 : 0 : constructor_elt e = {dindex, NULL_TREE};
4302 : 0 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
4303 : 0 : return end;
4304 : : }
4305 : :
4306 : : return -1;
4307 : : }
4308 : :
4309 : : /* Some expressions may have constant operands but are not constant
4310 : : themselves, such as 1/0. Call this function to check for that
4311 : : condition.
4312 : :
4313 : : We only call this in places that require an arithmetic constant, not in
4314 : : places where we might have a non-constant expression that can be a
4315 : : component of a constant expression, such as the address of a constexpr
4316 : : variable that might be dereferenced later. */
4317 : :
4318 : : static bool
4319 : 51 : verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
4320 : : bool *overflow_p)
4321 : : {
4322 : 51 : if (!*non_constant_p && !reduced_constant_expression_p (t) && t != void_node)
4323 : : {
4324 : 0 : if (!allow_non_constant)
4325 : 0 : error ("%q+E is not a constant expression", t);
4326 : 0 : *non_constant_p = true;
4327 : : }
4328 : 51 : if (TREE_OVERFLOW_P (t))
4329 : : {
4330 : 0 : if (!allow_non_constant)
4331 : : {
4332 : 0 : permerror (input_location, "overflow in constant expression");
4333 : : /* If we're being permissive (and are in an enforcing
4334 : : context), ignore the overflow. */
4335 : 0 : if (flag_permissive)
4336 : 0 : return *non_constant_p;
4337 : : }
4338 : 0 : *overflow_p = true;
4339 : : }
4340 : 51 : return *non_constant_p;
4341 : : }
4342 : :
4343 : : // forked from gcc/cp/constexpr.cc find_heap_var_refs
4344 : :
4345 : : /* Look for heap variables in the expression *TP. */
4346 : :
4347 : : static tree
4348 : 0 : find_heap_var_refs (tree *tp, int *walk_subtrees, void * /*data*/)
4349 : : {
4350 : 0 : if (VAR_P (*tp)
4351 : 0 : && (DECL_NAME (*tp) == heap_uninit_identifier
4352 : 0 : || DECL_NAME (*tp) == heap_identifier
4353 : 0 : || DECL_NAME (*tp) == heap_vec_uninit_identifier
4354 : 0 : || DECL_NAME (*tp) == heap_vec_identifier
4355 : 0 : || DECL_NAME (*tp) == heap_deleted_identifier))
4356 : : return *tp;
4357 : :
4358 : 0 : if (TYPE_P (*tp))
4359 : 0 : *walk_subtrees = 0;
4360 : : return NULL_TREE;
4361 : : }
4362 : :
4363 : : // forked from gcc/cp/constexpr.cc find_immediate_fndecl
4364 : :
4365 : : /* Find immediate function decls in *TP if any. */
4366 : :
4367 : : static tree
4368 : 0 : find_immediate_fndecl (tree *tp, int * /*walk_subtrees*/, void * /*data*/)
4369 : : {
4370 : 0 : if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp))
4371 : : return *tp;
4372 : 0 : if (TREE_CODE (*tp) == PTRMEM_CST
4373 : 0 : && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL
4374 : 0 : && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp)))
4375 : : return PTRMEM_CST_MEMBER (*tp);
4376 : : return NULL_TREE;
4377 : : }
4378 : :
4379 : : // forked in gcc/cp/constexpr.cc diag_array_subscript
4380 : :
4381 : : /* Under the control of CTX, issue a detailed diagnostic for
4382 : : an out-of-bounds subscript INDEX into the expression ARRAY. */
4383 : :
4384 : : static void
4385 : 1 : diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array,
4386 : : tree index)
4387 : : {
4388 : 1 : if (!ctx->quiet)
4389 : : {
4390 : 1 : tree arraytype = TREE_TYPE (array);
4391 : :
4392 : : /* Convert the unsigned array subscript to a signed integer to avoid
4393 : : printing huge numbers for small negative values. */
4394 : 1 : tree sidx = fold_convert (ssizetype, index);
4395 : 1 : STRIP_ANY_LOCATION_WRAPPER (array);
4396 : 1 : if (DECL_P (array))
4397 : : {
4398 : 1 : if (TYPE_DOMAIN (arraytype))
4399 : 1 : error_at (loc,
4400 : : "array subscript value %qE is outside the bounds "
4401 : : "of array %qD of type %qT",
4402 : : sidx, array, arraytype);
4403 : : else
4404 : 0 : error_at (loc,
4405 : : "nonzero array subscript %qE is used with array %qD of "
4406 : : "type %qT with unknown bounds",
4407 : : sidx, array, arraytype);
4408 : 1 : inform (DECL_SOURCE_LOCATION (array), "declared here");
4409 : : }
4410 : 0 : else if (TYPE_DOMAIN (arraytype))
4411 : 0 : error_at (loc,
4412 : : "array subscript value %qE is outside the bounds "
4413 : : "of array type %qT",
4414 : : sidx, arraytype);
4415 : : else
4416 : 0 : error_at (loc,
4417 : : "nonzero array subscript %qE is used with array of type %qT "
4418 : : "with unknown bounds",
4419 : : sidx, arraytype);
4420 : : }
4421 : 1 : }
4422 : :
4423 : : // forked from gcc/cp/constexpr.cc get_array_or_vector_nelts
4424 : :
4425 : : /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
4426 : : a VECTOR_TYPE). */
4427 : :
4428 : : static tree
4429 : 10 : get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type,
4430 : : bool *non_constant_p, bool *overflow_p)
4431 : : {
4432 : 10 : tree nelts;
4433 : 10 : if (TREE_CODE (type) == ARRAY_TYPE)
4434 : : {
4435 : 10 : if (TYPE_DOMAIN (type))
4436 : 10 : nelts = array_type_nelts_top (type);
4437 : : else
4438 : 0 : nelts = size_zero_node;
4439 : : }
4440 : 0 : else if (VECTOR_TYPE_P (type))
4441 : 0 : nelts = size_int (TYPE_VECTOR_SUBPARTS (type));
4442 : : else
4443 : 0 : rust_unreachable ();
4444 : :
4445 : : /* For VLAs, the number of elements won't be an integer constant. */
4446 : 10 : nelts
4447 : 10 : = eval_constant_expression (ctx, nelts, false, non_constant_p, overflow_p);
4448 : 10 : return nelts;
4449 : : }
4450 : :
4451 : : // forked from gcc/cp/constexpr.cc eval_and_check_array_index
4452 : :
4453 : : /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
4454 : : subscript, diagnose any problems with it, and return the result. */
4455 : :
4456 : : static tree
4457 : 10 : eval_and_check_array_index (const constexpr_ctx *ctx, tree t,
4458 : : bool allow_one_past, bool *non_constant_p,
4459 : : bool *overflow_p)
4460 : : {
4461 : 10 : location_t loc = rs_expr_loc_or_input_loc (t);
4462 : 10 : tree ary = TREE_OPERAND (t, 0);
4463 : 10 : t = TREE_OPERAND (t, 1);
4464 : 10 : tree index = eval_constant_expression (ctx, t, allow_one_past, non_constant_p,
4465 : : overflow_p);
4466 : 10 : VERIFY_CONSTANT (index);
4467 : :
4468 : 10 : if (!tree_fits_shwi_p (index) || tree_int_cst_sgn (index) < 0)
4469 : : {
4470 : 0 : diag_array_subscript (loc, ctx, ary, index);
4471 : 0 : *non_constant_p = true;
4472 : 0 : return t;
4473 : : }
4474 : :
4475 : 10 : tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p,
4476 : : overflow_p);
4477 : 10 : VERIFY_CONSTANT (nelts);
4478 : 20 : if (allow_one_past ? !tree_int_cst_le (index, nelts)
4479 : 10 : : !tree_int_cst_lt (index, nelts))
4480 : : {
4481 : 1 : diag_array_subscript (loc, ctx, ary, index);
4482 : 1 : *non_constant_p = true;
4483 : 1 : return t;
4484 : : }
4485 : :
4486 : : return index;
4487 : : }
4488 : :
4489 : : // forked from gcc/cp/constexpr.cc extract_string_elt
4490 : :
4491 : : /* Extract element INDEX consisting of CHARS_PER_ELT chars from
4492 : : STRING_CST STRING. */
4493 : :
4494 : : static tree
4495 : 0 : extract_string_elt (tree string, unsigned chars_per_elt, unsigned index)
4496 : : {
4497 : 0 : tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string)));
4498 : 0 : tree r;
4499 : :
4500 : 0 : if (chars_per_elt == 1)
4501 : 0 : r = build_int_cst (type, TREE_STRING_POINTER (string)[index]);
4502 : : else
4503 : : {
4504 : 0 : const unsigned char *ptr
4505 : 0 : = ((const unsigned char *) TREE_STRING_POINTER (string)
4506 : 0 : + index * chars_per_elt);
4507 : 0 : r = native_interpret_expr (type, ptr, chars_per_elt);
4508 : : }
4509 : 0 : return r;
4510 : : }
4511 : :
4512 : : /* Check whether the parameter and return types of FUN are valid for a
4513 : : constexpr function, and complain if COMPLAIN. */
4514 : :
4515 : : bool
4516 : 0 : is_valid_constexpr_fn (tree fun, bool complain)
4517 : : {
4518 : 0 : bool ret = true;
4519 : :
4520 : 0 : for (tree parm = FUNCTION_FIRST_USER_PARM (fun); parm != NULL_TREE;
4521 : 0 : parm = TREE_CHAIN (parm))
4522 : 0 : if (!literal_type_p (TREE_TYPE (parm)))
4523 : : {
4524 : 0 : ret = false;
4525 : 0 : if (complain)
4526 : : {
4527 : : // auto_diagnostic_group d;
4528 : : // error ("invalid type for parameter %d of %<constexpr%> "
4529 : : // "function %q+#D",
4530 : : // DECL_PARM_INDEX (parm), fun);
4531 : 0 : location_t locus = DECL_SOURCE_LOCATION (fun);
4532 : 0 : rust_error_at (
4533 : : locus, "invalid type for parameter %d of %<constexpr%> function",
4534 : 0 : DECL_PARM_INDEX (parm));
4535 : : }
4536 : : }
4537 : :
4538 : 0 : return ret;
4539 : : }
4540 : :
4541 : : void
4542 : 0 : explain_invalid_constexpr_fn (tree fun)
4543 : : {
4544 : 0 : static hash_set<tree> *diagnosed;
4545 : : // tree body;
4546 : :
4547 : 0 : if (diagnosed == NULL)
4548 : 0 : diagnosed = new hash_set<tree>;
4549 : 0 : if (diagnosed->add (fun))
4550 : : /* Already explained. */
4551 : 0 : return;
4552 : :
4553 : 0 : iloc_sentinel ils = input_location;
4554 : : // if (!lambda_static_thunk_p (fun))
4555 : : // {
4556 : : // /* Diagnostics should completely ignore the static thunk, so leave
4557 : : // input_location set to our caller's location. */
4558 : : // input_location = DECL_SOURCE_LOCATION (fun);
4559 : : // inform (input_location,
4560 : : // "%qD is not usable as a %<constexpr%> function because:",
4561 : : // fun);
4562 : : // }
4563 : :
4564 : : /* First check the declaration. */
4565 : 0 : if (is_valid_constexpr_fn (fun, true))
4566 : : {
4567 : : // /* Then if it's OK, the body. */
4568 : : // if (!DECL_DECLARED_CONSTEXPR_P (fun))
4569 : : // explain_implicit_non_constexpr (fun);
4570 : : // else
4571 : : // {
4572 : : // if (constexpr_fundef *fd = retrieve_constexpr_fundef (fun))
4573 : : // body = fd->body;
4574 : : // else
4575 : : // body = DECL_SAVED_TREE (fun);
4576 : : // body = massage_constexpr_body (fun, body);
4577 : : // require_potential_rvalue_constant_expression (body);
4578 : : // }
4579 : : }
4580 : 0 : }
4581 : :
4582 : : /* BODY is a validated and massaged definition of a constexpr
4583 : : function. Register it in the hash table. */
4584 : :
4585 : : void
4586 : 4402 : register_constexpr_fundef (const rust_constexpr_fundef &value)
4587 : : {
4588 : : /* Create the constexpr function table if necessary. */
4589 : 4402 : if (constexpr_fundef_table == NULL)
4590 : 677 : constexpr_fundef_table
4591 : 677 : = hash_table<rust_constexpr_fundef_hasher>::create_ggc (101);
4592 : :
4593 : 8804 : rust_constexpr_fundef **slot = constexpr_fundef_table->find_slot (
4594 : 4402 : const_cast<rust_constexpr_fundef *> (&value), INSERT);
4595 : :
4596 : 4402 : gcc_assert (*slot == NULL);
4597 : 4402 : *slot = ggc_alloc<rust_constexpr_fundef> ();
4598 : 4402 : **slot = value;
4599 : 4402 : }
4600 : :
4601 : : /* We are processing the definition of the constexpr function FUN.
4602 : : Check that its body fulfills the apropriate requirements and
4603 : : enter it in the constexpr function definition table. */
4604 : :
4605 : : void
4606 : 4402 : maybe_save_constexpr_fundef (tree fun)
4607 : : {
4608 : : // FIXME
4609 : :
4610 : 4402 : rust_constexpr_fundef entry = {fun, NULL_TREE, NULL_TREE, NULL_TREE};
4611 : 4402 : bool clear_ctx = false;
4612 : 4402 : if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE)
4613 : : {
4614 : 0 : clear_ctx = true;
4615 : 0 : DECL_CONTEXT (DECL_RESULT (fun)) = fun;
4616 : : }
4617 : 4402 : tree saved_fn = current_function_decl;
4618 : 4402 : current_function_decl = fun;
4619 : 4402 : entry.body = copy_fn (entry.decl, entry.parms, entry.result);
4620 : 4402 : current_function_decl = saved_fn;
4621 : 4402 : if (clear_ctx)
4622 : 0 : DECL_CONTEXT (DECL_RESULT (entry.decl)) = NULL_TREE;
4623 : :
4624 : 4402 : register_constexpr_fundef (entry);
4625 : 4402 : }
4626 : :
4627 : : /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
4628 : : semantics, for switch, break, continue, and return. */
4629 : :
4630 : : static tree
4631 : 1965 : eval_statement_list (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
4632 : : bool *overflow_p, tree *jump_target)
4633 : : {
4634 : 1965 : tree local_target;
4635 : : /* In a statement-expression we want to return the last value.
4636 : : For empty statement expression return void_node. */
4637 : 1965 : tree r = void_node;
4638 : 1965 : if (!jump_target)
4639 : : {
4640 : 0 : local_target = NULL_TREE;
4641 : 0 : jump_target = &local_target;
4642 : : }
4643 : 3657 : for (tree stmt : tsi_range (t))
4644 : : {
4645 : : /* We've found a continue, so skip everything until we reach
4646 : : the label its jumping to. */
4647 : 3627 : if (continues (jump_target))
4648 : : {
4649 : 0 : if (label_matches (ctx, jump_target, stmt))
4650 : : /* Found it. */
4651 : 0 : *jump_target = NULL_TREE;
4652 : : else
4653 : 0 : continue;
4654 : : }
4655 : 3627 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
4656 : 0 : continue;
4657 : 3627 : r = eval_constant_expression (ctx, stmt, false, non_constant_p,
4658 : : overflow_p, jump_target);
4659 : 3627 : if (*non_constant_p)
4660 : : break;
4661 : 3626 : if (returns (jump_target) || breaks (jump_target))
4662 : : break;
4663 : : }
4664 : 1965 : if (*jump_target && jump_target == &local_target)
4665 : : {
4666 : : /* We aren't communicating the jump to our caller, so give up. We don't
4667 : : need to support evaluation of jumps out of statement-exprs. */
4668 : 0 : if (!ctx->quiet)
4669 : 0 : error_at (EXPR_LOCATION (r), "statement is not a constant expression");
4670 : 0 : *non_constant_p = true;
4671 : : }
4672 : 1965 : return r;
4673 : : }
4674 : :
4675 : : // forked from gcc/cp/constexpr.cc cxx_eval_conditional_expression
4676 : :
4677 : : /* Subroutine of cxx_eval_constant_expression.
4678 : : Attempt to evaluate condition expressions. Dead branches are not
4679 : : looked into. */
4680 : :
4681 : : static tree
4682 : 16 : eval_conditional_expression (const constexpr_ctx *ctx, tree t, bool lval,
4683 : : bool *non_constant_p, bool *overflow_p,
4684 : : tree *jump_target)
4685 : : {
4686 : 16 : tree val
4687 : 16 : = eval_constant_expression (ctx, TREE_OPERAND (t, 0),
4688 : : /*lval*/ false, non_constant_p, overflow_p);
4689 : 16 : VERIFY_CONSTANT (val);
4690 : 16 : if (TREE_CODE (t) == IF_STMT && IF_STMT_CONSTEVAL_P (t))
4691 : : {
4692 : : /* Evaluate the condition as if it was
4693 : : if (__builtin_is_constant_evaluated ()), i.e. defer it if not
4694 : : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
4695 : : without manifestly_const_eval even expressions or parts thereof which
4696 : : will later be manifestly const_eval evaluated), otherwise fold it to
4697 : : true. */
4698 : 0 : if (ctx->manifestly_const_eval)
4699 : 0 : val = boolean_true_node;
4700 : : else
4701 : : {
4702 : 0 : *non_constant_p = true;
4703 : 0 : return t;
4704 : : }
4705 : : }
4706 : : /* Don't VERIFY_CONSTANT the other operands. */
4707 : 16 : if (integer_zerop (val))
4708 : 12 : val = TREE_OPERAND (t, 2);
4709 : : else
4710 : 4 : val = TREE_OPERAND (t, 1);
4711 : 16 : if (/*TREE_CODE (t) == IF_STMT && */ !val)
4712 : 11 : val = void_node;
4713 : 16 : return eval_constant_expression (ctx, val, lval, non_constant_p, overflow_p,
4714 : 16 : jump_target);
4715 : : }
4716 : :
4717 : : // forked from gcc/cp/constexpr.cc cxx_eval_bit_field_ref
4718 : :
4719 : : /* Subroutine of cxx_eval_constant_expression.
4720 : : Attempt to reduce a field access of a value of class type that is
4721 : : expressed as a BIT_FIELD_REF. */
4722 : :
4723 : : static tree
4724 : 0 : eval_bit_field_ref (const constexpr_ctx *ctx, tree t, bool lval,
4725 : : bool *non_constant_p, bool *overflow_p)
4726 : : {
4727 : 0 : tree orig_whole = TREE_OPERAND (t, 0);
4728 : 0 : tree retval, fldval, utype, mask;
4729 : 0 : bool fld_seen = false;
4730 : 0 : HOST_WIDE_INT istart, isize;
4731 : 0 : tree whole = eval_constant_expression (ctx, orig_whole, lval, non_constant_p,
4732 : : overflow_p);
4733 : 0 : tree start, field, value;
4734 : 0 : unsigned HOST_WIDE_INT i;
4735 : :
4736 : 0 : if (whole == orig_whole)
4737 : : return t;
4738 : : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
4739 : : CONSTRUCTOR. */
4740 : 0 : if (!*non_constant_p && TREE_CODE (whole) != VECTOR_CST
4741 : 0 : && TREE_CODE (whole) != CONSTRUCTOR)
4742 : : {
4743 : 0 : if (!ctx->quiet)
4744 : 0 : error ("%qE is not a constant expression", orig_whole);
4745 : 0 : *non_constant_p = true;
4746 : : }
4747 : 0 : if (*non_constant_p)
4748 : : return t;
4749 : :
4750 : 0 : if (TREE_CODE (whole) == VECTOR_CST)
4751 : 0 : return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
4752 : : TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
4753 : :
4754 : 0 : start = TREE_OPERAND (t, 2);
4755 : 0 : istart = tree_to_shwi (start);
4756 : 0 : isize = tree_to_shwi (TREE_OPERAND (t, 1));
4757 : 0 : utype = TREE_TYPE (t);
4758 : 0 : if (!TYPE_UNSIGNED (utype))
4759 : 0 : utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
4760 : 0 : retval = build_int_cst (utype, 0);
4761 : 0 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
4762 : : {
4763 : 0 : tree bitpos = bit_position (field);
4764 : 0 : STRIP_ANY_LOCATION_WRAPPER (value);
4765 : 0 : if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
4766 : : return value;
4767 : 0 : if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
4768 : 0 : && TREE_CODE (value) == INTEGER_CST && tree_fits_shwi_p (bitpos)
4769 : 0 : && tree_fits_shwi_p (DECL_SIZE (field)))
4770 : : {
4771 : 0 : HOST_WIDE_INT bit = tree_to_shwi (bitpos);
4772 : 0 : HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
4773 : 0 : HOST_WIDE_INT shift;
4774 : 0 : if (bit >= istart && bit + sz <= istart + isize)
4775 : : {
4776 : 0 : fldval = fold_convert (utype, value);
4777 : 0 : mask = build_int_cst_type (utype, -1);
4778 : 0 : mask = fold_build2 (LSHIFT_EXPR, utype, mask,
4779 : : size_int (TYPE_PRECISION (utype) - sz));
4780 : 0 : mask = fold_build2 (RSHIFT_EXPR, utype, mask,
4781 : : size_int (TYPE_PRECISION (utype) - sz));
4782 : 0 : fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
4783 : 0 : shift = bit - istart;
4784 : 0 : if (BYTES_BIG_ENDIAN)
4785 : : shift = TYPE_PRECISION (utype) - shift - sz;
4786 : 0 : fldval
4787 : 0 : = fold_build2 (LSHIFT_EXPR, utype, fldval, size_int (shift));
4788 : 0 : retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
4789 : 0 : fld_seen = true;
4790 : : }
4791 : : }
4792 : : }
4793 : 0 : if (fld_seen)
4794 : 0 : return fold_convert (TREE_TYPE (t), retval);
4795 : 0 : rust_unreachable ();
4796 : : return error_mark_node;
4797 : : }
4798 : :
4799 : : // forked from gcc/cp/constexpr.cc returns
4800 : :
4801 : : /* Predicates for the meaning of *jump_target. */
4802 : :
4803 : : static bool
4804 : 3636 : returns (tree *jump_target)
4805 : : {
4806 : 3636 : return *jump_target
4807 : 3636 : && (TREE_CODE (*jump_target) == RETURN_EXPR
4808 : 2 : || (TREE_CODE (*jump_target) == LABEL_DECL
4809 : 0 : && LABEL_DECL_CDTOR (*jump_target)));
4810 : : }
4811 : :
4812 : : // forked from gcc/cp/constexpr.cc breaks
4813 : :
4814 : : static bool
4815 : 1715 : breaks (tree *jump_target)
4816 : : {
4817 : 1715 : return *jump_target
4818 : 1715 : && ((TREE_CODE (*jump_target) == LABEL_DECL
4819 : 0 : && LABEL_DECL_BREAK (*jump_target))
4820 : 3 : || TREE_CODE (*jump_target) == BREAK_STMT
4821 : 3 : || TREE_CODE (*jump_target) == EXIT_EXPR);
4822 : : }
4823 : :
4824 : : // forked from gcc/cp/constexpr.cc continues
4825 : :
4826 : : static bool
4827 : 3637 : continues (tree *jump_target)
4828 : : {
4829 : 3637 : return *jump_target
4830 : 3637 : && ((TREE_CODE (*jump_target) == LABEL_DECL
4831 : 0 : && LABEL_DECL_CONTINUE (*jump_target))
4832 : 0 : || TREE_CODE (*jump_target) == CONTINUE_STMT);
4833 : : }
4834 : :
4835 : : // forked from gcc/cp/constexpr.cc switches
4836 : :
4837 : : static bool
4838 : 10 : switches (tree *jump_target)
4839 : : {
4840 : 10 : return *jump_target && TREE_CODE (*jump_target) == INTEGER_CST;
4841 : : }
4842 : :
4843 : : // forked from gcc/cp/constexpr.cc cxx_eval_loop_expr
4844 : :
4845 : : /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
4846 : : semantics; continue semantics are covered by cxx_eval_statement_list. */
4847 : :
4848 : : static tree
4849 : 1 : eval_loop_expr (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
4850 : : bool *overflow_p, tree *jump_target)
4851 : : {
4852 : 1 : constexpr_ctx new_ctx = *ctx;
4853 : 1 : tree local_target;
4854 : 1 : if (!jump_target)
4855 : : {
4856 : 0 : local_target = NULL_TREE;
4857 : 0 : jump_target = &local_target;
4858 : : }
4859 : :
4860 : 1 : tree body, cond = NULL_TREE, expr = NULL_TREE;
4861 : 1 : int count = 0;
4862 : 1 : switch (TREE_CODE (t))
4863 : : {
4864 : 1 : case LOOP_EXPR:
4865 : 1 : body = LOOP_EXPR_BODY (t);
4866 : 1 : break;
4867 : 0 : case WHILE_STMT:
4868 : 0 : body = WHILE_BODY (t);
4869 : 0 : cond = WHILE_COND (t);
4870 : 0 : count = -1;
4871 : 0 : break;
4872 : 0 : case FOR_STMT:
4873 : 0 : if (FOR_INIT_STMT (t))
4874 : 0 : eval_constant_expression (ctx, FOR_INIT_STMT (t), /*lval*/ false,
4875 : : non_constant_p, overflow_p, jump_target);
4876 : 0 : if (*non_constant_p)
4877 : : return NULL_TREE;
4878 : 0 : body = FOR_BODY (t);
4879 : 0 : cond = FOR_COND (t);
4880 : 0 : expr = FOR_EXPR (t);
4881 : 0 : count = -1;
4882 : 0 : break;
4883 : 0 : default:
4884 : 0 : rust_unreachable ();
4885 : : }
4886 : 2 : auto_vec<tree, 10> save_exprs;
4887 : 1 : new_ctx.save_exprs = &save_exprs;
4888 : 11 : do
4889 : : {
4890 : 11 : if (count != -1)
4891 : : {
4892 : 11 : if (body)
4893 : 11 : eval_constant_expression (&new_ctx, body, /*lval*/ false,
4894 : : non_constant_p, overflow_p, jump_target);
4895 : 11 : if (breaks (jump_target))
4896 : : {
4897 : 1 : *jump_target = NULL_TREE;
4898 : 1 : break;
4899 : : }
4900 : :
4901 : 10 : if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target))
4902 : 0 : *jump_target = NULL_TREE;
4903 : :
4904 : 10 : if (expr)
4905 : 0 : eval_constant_expression (&new_ctx, expr, /*lval*/ false,
4906 : : non_constant_p, overflow_p, jump_target);
4907 : : }
4908 : :
4909 : 10 : if (cond)
4910 : : {
4911 : 0 : tree res = eval_constant_expression (&new_ctx, cond, /*lval*/ false,
4912 : : non_constant_p, overflow_p,
4913 : : jump_target);
4914 : 0 : if (res)
4915 : : {
4916 : 0 : if (verify_constant (res, ctx->quiet, non_constant_p, overflow_p))
4917 : : break;
4918 : 0 : if (integer_zerop (res))
4919 : : break;
4920 : : }
4921 : : else
4922 : 0 : gcc_assert (*jump_target);
4923 : : }
4924 : :
4925 : : /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
4926 : 30 : for (tree save_expr : save_exprs)
4927 : 0 : ctx->global->values.remove (save_expr);
4928 : 10 : save_exprs.truncate (0);
4929 : :
4930 : 10 : if (++count >= constexpr_loop_limit)
4931 : : {
4932 : 0 : if (!ctx->quiet)
4933 : 0 : error_at (rs_expr_loc_or_input_loc (t),
4934 : : "%<constexpr%> loop iteration count exceeds limit of %d "
4935 : : "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
4936 : : constexpr_loop_limit);
4937 : 0 : *non_constant_p = true;
4938 : 0 : break;
4939 : : }
4940 : : }
4941 : 10 : while (!returns (jump_target) && !breaks (jump_target)
4942 : 10 : && !continues (jump_target) && (!switches (jump_target) || count == 0)
4943 : 20 : && !*non_constant_p);
4944 : :
4945 : : /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
4946 : 3 : for (tree save_expr : save_exprs)
4947 : 0 : ctx->global->values.remove (save_expr);
4948 : :
4949 : 1 : return NULL_TREE;
4950 : : }
4951 : :
4952 : : // forked from gcc/cp/constexpr.cc cxx_eval_switch_expr
4953 : :
4954 : : /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
4955 : : semantics. */
4956 : :
4957 : : static tree
4958 : 0 : eval_switch_expr (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
4959 : : bool *overflow_p, tree *jump_target)
4960 : : {
4961 : 0 : tree cond
4962 : 0 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t);
4963 : 0 : cond
4964 : 0 : = eval_constant_expression (ctx, cond, false, non_constant_p, overflow_p);
4965 : 0 : VERIFY_CONSTANT (cond);
4966 : 0 : *jump_target = cond;
4967 : :
4968 : 0 : tree body
4969 : 0 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t);
4970 : 0 : constexpr_ctx new_ctx = *ctx;
4971 : 0 : constexpr_switch_state css = css_default_not_seen;
4972 : 0 : new_ctx.css_state = &css;
4973 : 0 : eval_constant_expression (&new_ctx, body, false, non_constant_p, overflow_p,
4974 : : jump_target);
4975 : 0 : if (switches (jump_target) && css == css_default_seen)
4976 : : {
4977 : : /* If the SWITCH_EXPR body has default: label, process it once again,
4978 : : this time instructing label_matches to return true for default:
4979 : : label on switches (jump_target). */
4980 : 0 : css = css_default_processing;
4981 : 0 : eval_constant_expression (&new_ctx, body, false, non_constant_p,
4982 : : overflow_p, jump_target);
4983 : : }
4984 : 0 : if (breaks (jump_target) || switches (jump_target))
4985 : 0 : *jump_target = NULL_TREE;
4986 : : return NULL_TREE;
4987 : : }
4988 : :
4989 : : // forked from gcc/cp/constexpr.cc eval_unary_expression
4990 : :
4991 : : /* Subroutine of cxx_eval_constant_expression.
4992 : : Attempt to reduce the unary expression tree T to a compile time value.
4993 : : If successful, return the value. Otherwise issue a diagnostic
4994 : : and return error_mark_node. */
4995 : :
4996 : : static tree
4997 : 7 : eval_unary_expression (const constexpr_ctx *ctx, tree t, bool /*lval*/,
4998 : : bool *non_constant_p, bool *overflow_p)
4999 : : {
5000 : 7 : tree r;
5001 : 7 : tree orig_arg = TREE_OPERAND (t, 0);
5002 : 7 : tree arg = eval_constant_expression (ctx, orig_arg, /*lval*/ false,
5003 : : non_constant_p, overflow_p);
5004 : 7 : VERIFY_CONSTANT (arg);
5005 : 7 : location_t loc = EXPR_LOCATION (t);
5006 : 7 : enum tree_code code = TREE_CODE (t);
5007 : 7 : tree type = TREE_TYPE (t);
5008 : 7 : r = fold_unary_loc (loc, code, type, arg);
5009 : 7 : if (r == NULL_TREE)
5010 : : {
5011 : 0 : if (arg == orig_arg)
5012 : : r = t;
5013 : : else
5014 : 0 : r = build1_loc (loc, code, type, arg);
5015 : : }
5016 : 7 : VERIFY_CONSTANT (r);
5017 : : return r;
5018 : : }
5019 : :
5020 : : // forked from gcc/cp/constexpr.cc cxx_eval_outermost_constant_expr
5021 : :
5022 : : /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
5023 : : STRICT has the same sense as for constant_value_1: true if we only allow
5024 : : conforming C++ constant expressions, or false if we want a constant value
5025 : : even if it doesn't conform.
5026 : : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
5027 : : per P0595 even when ALLOW_NON_CONSTANT is true.
5028 : : CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
5029 : : OBJECT must be non-NULL in that case. */
5030 : :
5031 : : static tree
5032 : 0 : cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
5033 : : bool strict = true,
5034 : : bool manifestly_const_eval = false,
5035 : : bool constexpr_dtor = false,
5036 : : tree object = NULL_TREE)
5037 : : {
5038 : 0 : auto_timevar time (TV_CONSTEXPR);
5039 : :
5040 : 0 : bool non_constant_p = false;
5041 : 0 : bool overflow_p = false;
5042 : :
5043 : 0 : if (BRACE_ENCLOSED_INITIALIZER_P (t))
5044 : : {
5045 : 0 : gcc_checking_assert (allow_non_constant);
5046 : : return t;
5047 : : }
5048 : :
5049 : 0 : constexpr_global_ctx global_ctx;
5050 : 0 : constexpr_ctx ctx
5051 : : = {&global_ctx, NULL,
5052 : : NULL, NULL,
5053 : : NULL, NULL,
5054 : : NULL, allow_non_constant,
5055 : 0 : strict, manifestly_const_eval || !allow_non_constant};
5056 : :
5057 : : /* Turn off -frounding-math for manifestly constant evaluation. */
5058 : 0 : warning_sentinel rm (flag_rounding_math, ctx.manifestly_const_eval);
5059 : 0 : tree type = initialized_type (t);
5060 : 0 : tree r = t;
5061 : 0 : bool is_consteval = false;
5062 : 0 : if (VOID_TYPE_P (type))
5063 : : {
5064 : 0 : if (constexpr_dtor)
5065 : : /* Used for destructors of array elements. */
5066 : 0 : type = TREE_TYPE (object);
5067 : : else
5068 : : {
5069 : 0 : if (TREE_CODE (t) != CALL_EXPR)
5070 : : return t;
5071 : : /* Calls to immediate functions returning void need to be
5072 : : evaluated. */
5073 : 0 : tree fndecl = rs_get_callee_fndecl_nofold (t);
5074 : 0 : if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl))
5075 : : return t;
5076 : : else
5077 : : is_consteval = true;
5078 : : }
5079 : : }
5080 : 0 : else if ((TREE_CODE (t) == CALL_EXPR || TREE_CODE (t) == TARGET_EXPR))
5081 : : {
5082 : : /* For non-concept checks, determine if it is consteval. */
5083 : 0 : tree x = t;
5084 : 0 : if (TREE_CODE (x) == TARGET_EXPR)
5085 : 0 : x = TARGET_EXPR_INITIAL (x);
5086 : 0 : tree fndecl = rs_get_callee_fndecl_nofold (x);
5087 : 0 : if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl))
5088 : : is_consteval = true;
5089 : : }
5090 : 0 : if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
5091 : : {
5092 : : /* In C++14 an NSDMI can participate in aggregate initialization,
5093 : : and can refer to the address of the object being initialized, so
5094 : : we need to pass in the relevant VAR_DECL if we want to do the
5095 : : evaluation in a single pass. The evaluation will dynamically
5096 : : update ctx.values for the VAR_DECL. We use the same strategy
5097 : : for C++11 constexpr constructors that refer to the object being
5098 : : initialized. */
5099 : 0 : if (constexpr_dtor)
5100 : : {
5101 : 0 : gcc_assert (object && VAR_P (object));
5102 : 0 : gcc_assert (DECL_DECLARED_CONSTEXPR_P (object));
5103 : 0 : gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object));
5104 : 0 : if (error_operand_p (DECL_INITIAL (object)))
5105 : : return t;
5106 : 0 : ctx.ctor = unshare_expr (DECL_INITIAL (object));
5107 : 0 : TREE_READONLY (ctx.ctor) = false;
5108 : : /* Temporarily force decl_really_constant_value to return false
5109 : : for it, we want to use ctx.ctor for the current value instead. */
5110 : 0 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false;
5111 : : }
5112 : : else
5113 : : {
5114 : 0 : ctx.ctor = build_constructor (type, NULL);
5115 : 0 : CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true;
5116 : : }
5117 : 0 : if (!object)
5118 : : {
5119 : 0 : if (TREE_CODE (t) == TARGET_EXPR)
5120 : 0 : object = TARGET_EXPR_SLOT (t);
5121 : : }
5122 : 0 : ctx.object = object;
5123 : 0 : if (object)
5124 : 0 : gcc_assert (
5125 : : same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (object)));
5126 : 0 : if (object && DECL_P (object))
5127 : 0 : global_ctx.values.put (object, ctx.ctor);
5128 : 0 : if (TREE_CODE (r) == TARGET_EXPR)
5129 : : /* Avoid creating another CONSTRUCTOR when we expand the
5130 : : TARGET_EXPR. */
5131 : 0 : r = TARGET_EXPR_INITIAL (r);
5132 : : }
5133 : :
5134 : 0 : auto_vec<tree, 16> cleanups;
5135 : 0 : global_ctx.cleanups = &cleanups;
5136 : :
5137 : 0 : if (manifestly_const_eval)
5138 : 0 : instantiate_constexpr_fns (r);
5139 : 0 : r = eval_constant_expression (&ctx, r, false, &non_constant_p, &overflow_p);
5140 : :
5141 : 0 : if (!constexpr_dtor)
5142 : 0 : verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
5143 : : else
5144 : 0 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true;
5145 : :
5146 : 0 : unsigned int i;
5147 : 0 : tree cleanup;
5148 : : /* Evaluate the cleanups. */
5149 : 0 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
5150 : 0 : eval_constant_expression (&ctx, cleanup, false, &non_constant_p,
5151 : : &overflow_p);
5152 : :
5153 : : /* Mutable logic is a bit tricky: we want to allow initialization of
5154 : : constexpr variables with mutable members, but we can't copy those
5155 : : members to another constexpr variable. */
5156 : 0 : if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_MUTABLE_POISON (r))
5157 : : {
5158 : 0 : if (!allow_non_constant)
5159 : 0 : error ("%qE is not a constant expression because it refers to "
5160 : : "mutable subobjects of %qT",
5161 : : t, type);
5162 : 0 : non_constant_p = true;
5163 : : }
5164 : :
5165 : 0 : if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r))
5166 : : {
5167 : 0 : if (!allow_non_constant)
5168 : 0 : error ("%qE is not a constant expression because it refers to "
5169 : : "an incompletely initialized variable",
5170 : : t);
5171 : 0 : TREE_CONSTANT (r) = false;
5172 : 0 : non_constant_p = true;
5173 : : }
5174 : :
5175 : 0 : if (!global_ctx.heap_vars.is_empty ())
5176 : : {
5177 : 0 : tree heap_var
5178 : 0 : = rs_walk_tree_without_duplicates (&r, find_heap_var_refs, NULL);
5179 : 0 : unsigned int i;
5180 : 0 : if (heap_var)
5181 : : {
5182 : 0 : if (!allow_non_constant && !non_constant_p)
5183 : 0 : error_at (DECL_SOURCE_LOCATION (heap_var),
5184 : : "%qE is not a constant expression because it refers to "
5185 : : "a result of %<operator new%>",
5186 : : t);
5187 : 0 : r = t;
5188 : 0 : non_constant_p = true;
5189 : : }
5190 : 0 : FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var)
5191 : : {
5192 : 0 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
5193 : : {
5194 : 0 : if (!allow_non_constant && !non_constant_p)
5195 : 0 : error_at (DECL_SOURCE_LOCATION (heap_var),
5196 : : "%qE is not a constant expression because allocated "
5197 : : "storage has not been deallocated",
5198 : : t);
5199 : 0 : r = t;
5200 : 0 : non_constant_p = true;
5201 : : }
5202 : 0 : varpool_node::get (heap_var)->remove ();
5203 : : }
5204 : : }
5205 : :
5206 : : /* Check that immediate invocation does not return an expression referencing
5207 : : any immediate function decls. */
5208 : 0 : if (is_consteval || in_immediate_context ())
5209 : 0 : if (tree immediate_fndecl
5210 : 0 : = rs_walk_tree_without_duplicates (&r, find_immediate_fndecl, NULL))
5211 : : {
5212 : 0 : if (!allow_non_constant && !non_constant_p)
5213 : 0 : error_at (rs_expr_loc_or_input_loc (t),
5214 : : "immediate evaluation returns address of immediate "
5215 : : "function %qD",
5216 : : immediate_fndecl);
5217 : 0 : r = t;
5218 : 0 : non_constant_p = true;
5219 : : }
5220 : :
5221 : 0 : if (non_constant_p)
5222 : : /* If we saw something bad, go back to our argument. The wrapping below is
5223 : : only for the cases of TREE_CONSTANT argument or overflow. */
5224 : 0 : r = t;
5225 : :
5226 : 0 : if (!non_constant_p && overflow_p)
5227 : 0 : non_constant_p = true;
5228 : :
5229 : : /* Unshare the result. */
5230 : 0 : bool should_unshare = true;
5231 : 0 : if (r == t || (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_INITIAL (t) == r))
5232 : : should_unshare = false;
5233 : :
5234 : 0 : if (non_constant_p && !allow_non_constant)
5235 : 0 : return error_mark_node;
5236 : 0 : else if (constexpr_dtor)
5237 : : return r;
5238 : 0 : else if (non_constant_p && TREE_CONSTANT (r))
5239 : : {
5240 : : /* This isn't actually constant, so unset TREE_CONSTANT.
5241 : : Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
5242 : : it to be set if it is invariant address, even when it is not
5243 : : a valid C++ constant expression. Wrap it with a NOP_EXPR
5244 : : instead. */
5245 : 0 : if (EXPR_P (r) && TREE_CODE (r) != ADDR_EXPR)
5246 : 0 : r = copy_node (r);
5247 : 0 : else if (TREE_CODE (r) == CONSTRUCTOR)
5248 : 0 : r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
5249 : : else
5250 : 0 : r = build_nop (TREE_TYPE (r), r);
5251 : 0 : TREE_CONSTANT (r) = false;
5252 : : }
5253 : 0 : else if (non_constant_p)
5254 : : return t;
5255 : :
5256 : 0 : if (should_unshare)
5257 : 0 : r = unshare_expr (r);
5258 : :
5259 : 0 : if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
5260 : : {
5261 : 0 : r = adjust_temp_type (type, r);
5262 : 0 : if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_INITIAL (t) == r)
5263 : : return t;
5264 : : }
5265 : :
5266 : : /* Remember the original location if that wouldn't need a wrapper. */
5267 : 0 : if (location_t loc = EXPR_LOCATION (t))
5268 : 0 : protected_set_expr_location (r, loc);
5269 : :
5270 : 0 : return r;
5271 : 0 : }
5272 : :
5273 : : /* Like is_constant_expression, but allow const variables that are not allowed
5274 : : under constexpr rules. */
5275 : :
5276 : : bool
5277 : 0 : is_static_init_expression (tree t)
5278 : : {
5279 : 0 : return potential_constant_expression_1 (t, false, false, true, tf_none);
5280 : : }
5281 : :
5282 : : /* Like potential_constant_expression, but don't consider possible constexpr
5283 : : substitution of the current function. That is, PARM_DECL qualifies under
5284 : : potential_constant_expression, but not here.
5285 : :
5286 : : This is basically what you can check when any actual constant values might
5287 : : be value-dependent. */
5288 : :
5289 : : bool
5290 : 0 : is_constant_expression (tree t)
5291 : : {
5292 : 0 : return potential_constant_expression_1 (t, false, true, true, tf_none);
5293 : : }
5294 : :
5295 : : /* Returns true if T is a potential static initializer expression that is not
5296 : : instantiation-dependent. */
5297 : :
5298 : : bool
5299 : 0 : is_nondependent_static_init_expression (tree t)
5300 : : {
5301 : 0 : return (!type_unknown_p (t) && is_static_init_expression (t));
5302 : : }
5303 : :
5304 : : /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
5305 : : than wrapped in a TARGET_EXPR.
5306 : : ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
5307 : : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
5308 : : per P0595 even when ALLOW_NON_CONSTANT is true. */
5309 : :
5310 : : static tree
5311 : 0 : maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant,
5312 : : bool manifestly_const_eval)
5313 : : {
5314 : 0 : if (!t)
5315 : : return t;
5316 : 0 : if (TREE_CODE (t) == EXPR_STMT)
5317 : 0 : t = TREE_OPERAND (t, 0);
5318 : 0 : if (TREE_CODE (t) == CONVERT_EXPR && VOID_TYPE_P (TREE_TYPE (t)))
5319 : 0 : t = TREE_OPERAND (t, 0);
5320 : 0 : if (TREE_CODE (t) == INIT_EXPR)
5321 : 0 : t = TREE_OPERAND (t, 1);
5322 : 0 : if (TREE_CODE (t) == TARGET_EXPR)
5323 : 0 : t = TARGET_EXPR_INITIAL (t);
5324 : 0 : if (!is_nondependent_static_init_expression (t))
5325 : : /* Don't try to evaluate it. */;
5326 : 0 : else if (CONSTANT_CLASS_P (t) && allow_non_constant)
5327 : : /* No evaluation needed. */;
5328 : : else
5329 : 0 : t = cxx_eval_outermost_constant_expr (t, allow_non_constant,
5330 : : /*strict*/ false,
5331 : : manifestly_const_eval, false, decl);
5332 : 0 : if (TREE_CODE (t) == TARGET_EXPR)
5333 : : {
5334 : 0 : tree init = TARGET_EXPR_INITIAL (t);
5335 : 0 : if (TREE_CODE (init) == CONSTRUCTOR)
5336 : 0 : t = init;
5337 : : }
5338 : : return t;
5339 : : }
5340 : :
5341 : : /* Wrapper for maybe_constant_init_1 which permits non constants. */
5342 : :
5343 : : tree
5344 : 0 : maybe_constant_init (tree t, tree decl, bool manifestly_const_eval)
5345 : : {
5346 : 0 : return maybe_constant_init_1 (t, decl, true, manifestly_const_eval);
5347 : : }
5348 : :
5349 : : /* Returns true if T is a potential constant expression that is not
5350 : : instantiation-dependent, and therefore a candidate for constant folding even
5351 : : in a template. */
5352 : :
5353 : : bool
5354 : 0 : is_nondependent_constant_expression (tree t)
5355 : : {
5356 : 0 : return (!type_unknown_p (t) && is_constant_expression (t)
5357 : 0 : && !instantiation_dependent_expression_p (t));
5358 : : }
5359 : :
5360 : : // forked from gcc/cp/parser.cc cp_unevaluated_operand
5361 : :
5362 : : /* Nonzero if we are parsing an unevaluated operand: an operand to
5363 : : sizeof, typeof, or alignof. */
5364 : : int cp_unevaluated_operand;
5365 : :
5366 : : // forked from gcc/cp/constexpr.cc cv_cache
5367 : :
5368 : : /* If T is a constant expression, returns its reduced value.
5369 : : Otherwise, if T does not have TREE_CONSTANT set, returns T.
5370 : : Otherwise, returns a version of T without TREE_CONSTANT.
5371 : : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
5372 : : as per P0595. */
5373 : :
5374 : : static GTY ((deletable)) hash_map<tree, tree> *cv_cache;
5375 : :
5376 : : // forked from gcc/cp/constexpr.cc maybe_constant_value
5377 : :
5378 : : tree
5379 : 0 : maybe_constant_value (tree t, tree decl, bool manifestly_const_eval)
5380 : : {
5381 : 0 : tree r;
5382 : :
5383 : 0 : if (!is_nondependent_constant_expression (t))
5384 : : {
5385 : 0 : if (TREE_OVERFLOW_P (t))
5386 : : {
5387 : 0 : t = build_nop (TREE_TYPE (t), t);
5388 : 0 : TREE_CONSTANT (t) = false;
5389 : : }
5390 : 0 : return t;
5391 : : }
5392 : 0 : else if (CONSTANT_CLASS_P (t))
5393 : : /* No caching or evaluation needed. */
5394 : : return t;
5395 : :
5396 : 0 : if (manifestly_const_eval)
5397 : 0 : return cxx_eval_outermost_constant_expr (t, true, true, true, false, decl);
5398 : :
5399 : 0 : if (cv_cache == NULL)
5400 : 0 : cv_cache = hash_map<tree, tree>::create_ggc (101);
5401 : 0 : if (tree *cached = cv_cache->get (t))
5402 : : {
5403 : 0 : r = *cached;
5404 : 0 : if (r != t)
5405 : : {
5406 : : // Faisal: commenting this out as not sure if it's needed and it's
5407 : : // huge r = break_out_target_exprs (r, /*clear_loc*/true);
5408 : 0 : protected_set_expr_location (r, EXPR_LOCATION (t));
5409 : : }
5410 : 0 : return r;
5411 : : }
5412 : :
5413 : : /* Don't evaluate an unevaluated operand. */
5414 : 0 : if (cp_unevaluated_operand)
5415 : : return t;
5416 : :
5417 : 0 : uid_sensitive_constexpr_evaluation_checker c;
5418 : 0 : r = cxx_eval_outermost_constant_expr (t, true, true, false, false, decl);
5419 : 0 : gcc_checking_assert (
5420 : : r == t || CONVERT_EXPR_P (t) || TREE_CODE (t) == VIEW_CONVERT_EXPR
5421 : : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r)) || !rs_tree_equal (r, t));
5422 : 0 : if (!c.evaluation_restricted_p ())
5423 : 0 : cv_cache->put (t, r);
5424 : : return r;
5425 : : }
5426 : :
5427 : : // forked from gcc/cp/constexpr.cc
5428 : :
5429 : : bool
5430 : 0 : potential_constant_expression (tree t)
5431 : : {
5432 : 0 : return potential_constant_expression_1 (t, false, true, false, tf_none);
5433 : : }
5434 : :
5435 : : /* Data structure for passing data from potential_constant_expression_1
5436 : : to check_for_return_continue via cp_walk_tree. */
5437 : : struct check_for_return_continue_data
5438 : : {
5439 : : hash_set<tree> *pset;
5440 : : tree continue_stmt;
5441 : : tree break_stmt;
5442 : : };
5443 : :
5444 : : /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
5445 : : called through cp_walk_tree. Return the first RETURN_EXPR found, or note
5446 : : the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found. */
5447 : : static tree
5448 : 0 : check_for_return_continue (tree *tp, int *walk_subtrees, void *data)
5449 : : {
5450 : 0 : tree t = *tp, s, b;
5451 : 0 : check_for_return_continue_data *d = (check_for_return_continue_data *) data;
5452 : 0 : switch (TREE_CODE (t))
5453 : : {
5454 : : case RETURN_EXPR:
5455 : : return t;
5456 : :
5457 : 0 : case CONTINUE_STMT:
5458 : 0 : if (d->continue_stmt == NULL_TREE)
5459 : 0 : d->continue_stmt = t;
5460 : : break;
5461 : :
5462 : 0 : case BREAK_STMT:
5463 : 0 : if (d->break_stmt == NULL_TREE)
5464 : 0 : d->break_stmt = t;
5465 : : break;
5466 : :
5467 : : #define RECUR(x) \
5468 : : if (tree r = rs_walk_tree (&x, check_for_return_continue, data, d->pset)) \
5469 : : return r
5470 : :
5471 : : /* For loops, walk subtrees manually, so that continue stmts found
5472 : : inside of the bodies of the loops are ignored. */
5473 : :
5474 : 0 : case WHILE_STMT:
5475 : 0 : *walk_subtrees = 0;
5476 : 0 : RECUR (WHILE_COND (t));
5477 : 0 : s = d->continue_stmt;
5478 : 0 : b = d->break_stmt;
5479 : 0 : RECUR (WHILE_BODY (t));
5480 : 0 : d->continue_stmt = s;
5481 : 0 : d->break_stmt = b;
5482 : 0 : break;
5483 : :
5484 : 0 : case FOR_STMT:
5485 : 0 : *walk_subtrees = 0;
5486 : 0 : RECUR (FOR_INIT_STMT (t));
5487 : 0 : RECUR (FOR_COND (t));
5488 : 0 : RECUR (FOR_EXPR (t));
5489 : 0 : s = d->continue_stmt;
5490 : 0 : b = d->break_stmt;
5491 : 0 : RECUR (FOR_BODY (t));
5492 : 0 : d->continue_stmt = s;
5493 : 0 : d->break_stmt = b;
5494 : 0 : break;
5495 : :
5496 : 0 : case RANGE_FOR_STMT:
5497 : 0 : *walk_subtrees = 0;
5498 : 0 : RECUR (RANGE_FOR_EXPR (t));
5499 : 0 : s = d->continue_stmt;
5500 : 0 : b = d->break_stmt;
5501 : 0 : RECUR (RANGE_FOR_BODY (t));
5502 : 0 : d->continue_stmt = s;
5503 : 0 : d->break_stmt = b;
5504 : 0 : break;
5505 : :
5506 : 0 : case SWITCH_STMT:
5507 : 0 : *walk_subtrees = 0;
5508 : 0 : RECUR (SWITCH_STMT_COND (t));
5509 : 0 : b = d->break_stmt;
5510 : 0 : RECUR (SWITCH_STMT_BODY (t));
5511 : 0 : d->break_stmt = b;
5512 : 0 : break;
5513 : : #undef RECUR
5514 : :
5515 : : case STATEMENT_LIST:
5516 : : case CONSTRUCTOR:
5517 : : break;
5518 : :
5519 : 0 : default:
5520 : 0 : if (!EXPR_P (t))
5521 : 0 : *walk_subtrees = 0;
5522 : : break;
5523 : : }
5524 : :
5525 : : return NULL_TREE;
5526 : : }
5527 : :
5528 : : /* Returns the namespace that contains DECL, whether directly or
5529 : : indirectly. */
5530 : :
5531 : : tree
5532 : 0 : decl_namespace_context (tree decl)
5533 : : {
5534 : 0 : while (1)
5535 : : {
5536 : 0 : if (TREE_CODE (decl) == NAMESPACE_DECL)
5537 : 0 : return decl;
5538 : 0 : else if (TYPE_P (decl))
5539 : 0 : decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
5540 : : else
5541 : 0 : decl = CP_DECL_CONTEXT (decl);
5542 : : }
5543 : : }
5544 : :
5545 : : /* Returns true if DECL is in the std namespace. */
5546 : :
5547 : : bool
5548 : 0 : decl_in_std_namespace_p (tree decl)
5549 : : {
5550 : 0 : while (decl)
5551 : : {
5552 : 0 : decl = decl_namespace_context (decl);
5553 : 0 : if (DECL_NAMESPACE_STD_P (decl))
5554 : : return true;
5555 : : /* Allow inline namespaces inside of std namespace, e.g. with
5556 : : --enable-symvers=gnu-versioned-namespace std::forward would be
5557 : : actually std::_8::forward. */
5558 : 0 : if (!DECL_NAMESPACE_INLINE_P (decl))
5559 : : return false;
5560 : 0 : decl = CP_DECL_CONTEXT (decl);
5561 : : }
5562 : : return false;
5563 : : }
5564 : :
5565 : : /* Return true if FNDECL is std::construct_at. */
5566 : :
5567 : : static inline bool
5568 : : is_std_construct_at (tree fndecl)
5569 : : {
5570 : : if (!decl_in_std_namespace_p (fndecl))
5571 : : return false;
5572 : :
5573 : : tree name = DECL_NAME (fndecl);
5574 : : return name && id_equal (name, "construct_at");
5575 : : }
5576 : :
5577 : : /* Return true if FNDECL is __dynamic_cast. */
5578 : :
5579 : : static inline bool
5580 : : cxx_dynamic_cast_fn_p (tree fndecl)
5581 : : {
5582 : : return (id_equal (DECL_NAME (fndecl), "__dynamic_cast")
5583 : : && CP_DECL_CONTEXT (fndecl) == global_namespace);
5584 : : }
5585 : :
5586 : : /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
5587 : :
5588 : : static inline bool
5589 : : is_std_allocator_allocate (tree fndecl)
5590 : : {
5591 : : tree name = DECL_NAME (fndecl);
5592 : : if (name == NULL_TREE
5593 : : || !(id_equal (name, "allocate") || id_equal (name, "deallocate")))
5594 : : return false;
5595 : :
5596 : : tree ctx = DECL_CONTEXT (fndecl);
5597 : : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
5598 : : return false;
5599 : :
5600 : : tree decl = TYPE_MAIN_DECL (ctx);
5601 : : name = DECL_NAME (decl);
5602 : : if (name == NULL_TREE || !id_equal (name, "allocator"))
5603 : : return false;
5604 : :
5605 : : return decl_in_std_namespace_p (decl);
5606 : : }
5607 : :
5608 : : /* Overload for the above taking rust_constexpr_call*. */
5609 : :
5610 : : static inline bool
5611 : : is_std_allocator_allocate (const rust_constexpr_call *call)
5612 : : {
5613 : : return (call && call->fundef
5614 : : && is_std_allocator_allocate (call->fundef->decl));
5615 : : }
5616 : :
5617 : : /* Return true if T denotes a potentially constant expression. Issue
5618 : : diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
5619 : : an lvalue-rvalue conversion is implied. If NOW is true, we want to
5620 : : consider the expression in the current context, independent of constexpr
5621 : : substitution.
5622 : :
5623 : : C++0x [expr.const] used to say
5624 : :
5625 : : 6 An expression is a potential constant expression if it is
5626 : : a constant expression where all occurrences of function
5627 : : parameters are replaced by arbitrary constant expressions
5628 : : of the appropriate type.
5629 : :
5630 : : 2 A conditional expression is a constant expression unless it
5631 : : involves one of the following as a potentially evaluated
5632 : : subexpression (3.2), but subexpressions of logical AND (5.14),
5633 : : logical OR (5.15), and conditional (5.16) operations that are
5634 : : not evaluated are not considered. */
5635 : :
5636 : : static bool
5637 : 0 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
5638 : : tsubst_flags_t flags, tree *jump_target)
5639 : : {
5640 : : #define RECUR(T, RV) \
5641 : : potential_constant_expression_1 ((T), (RV), strict, now, flags, jump_target)
5642 : :
5643 : 0 : enum
5644 : : {
5645 : : any = false,
5646 : : rval = true
5647 : : };
5648 : 0 : int i;
5649 : 0 : tree tmp;
5650 : :
5651 : 0 : if (t == error_mark_node)
5652 : : return false;
5653 : 0 : if (t == NULL_TREE)
5654 : : return true;
5655 : 0 : location_t loc = rs_expr_loc_or_input_loc (t);
5656 : :
5657 : 0 : if (*jump_target)
5658 : : /* If we are jumping, ignore everything. This is simpler than the
5659 : : cxx_eval_constant_expression handling because we only need to be
5660 : : conservatively correct, and we don't necessarily have a constant value
5661 : : available, so we don't bother with switch tracking. */
5662 : : return true;
5663 : :
5664 : 0 : if (TREE_THIS_VOLATILE (t) && want_rval)
5665 : : {
5666 : 0 : if (flags & tf_error)
5667 : 0 : error_at (loc,
5668 : : "lvalue-to-rvalue conversion of a volatile lvalue "
5669 : : "%qE with type %qT",
5670 : 0 : t, TREE_TYPE (t));
5671 : 0 : return false;
5672 : : }
5673 : 0 : if (CONSTANT_CLASS_P (t))
5674 : : return true;
5675 : 0 : if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED)
5676 : 0 : && TREE_TYPE (t) == error_mark_node)
5677 : : return false;
5678 : :
5679 : 0 : switch (TREE_CODE (t))
5680 : : {
5681 : : case FUNCTION_DECL:
5682 : : case OVERLOAD:
5683 : : case LABEL_DECL:
5684 : : case CASE_LABEL_EXPR:
5685 : : case PREDICT_EXPR:
5686 : : case CONST_DECL:
5687 : : case IDENTIFIER_NODE:
5688 : : /* We can see a FIELD_DECL in a pointer-to-member expression. */
5689 : : case FIELD_DECL:
5690 : : case RESULT_DECL:
5691 : : case PLACEHOLDER_EXPR:
5692 : : case STATIC_ASSERT:
5693 : : return true;
5694 : :
5695 : 0 : case RETURN_EXPR:
5696 : 0 : if (!RECUR (TREE_OPERAND (t, 0), any))
5697 : : return false;
5698 : : /* FALLTHROUGH */
5699 : :
5700 : 0 : case BREAK_STMT:
5701 : 0 : case CONTINUE_STMT:
5702 : 0 : *jump_target = t;
5703 : 0 : return true;
5704 : :
5705 : 0 : case PARM_DECL:
5706 : 0 : if (now && want_rval)
5707 : : {
5708 : 0 : tree type = TREE_TYPE (t);
5709 : 0 : if (is_really_empty_class (type, /*ignore_vptr*/ false))
5710 : : /* An empty class has no data to read. */
5711 : : return true;
5712 : 0 : if (flags & tf_error)
5713 : 0 : error ("%qE is not a constant expression", t);
5714 : 0 : return false;
5715 : : }
5716 : : return true;
5717 : :
5718 : 0 : case CALL_EXPR:
5719 : : /* -- an invocation of a function other than a constexpr function
5720 : : or a constexpr constructor. */
5721 : 0 : {
5722 : 0 : tree fun = get_function_named_in_call (t);
5723 : 0 : const int nargs = call_expr_nargs (t);
5724 : 0 : i = 0;
5725 : :
5726 : 0 : if (fun == NULL_TREE)
5727 : : {
5728 : : /* Reset to allow the function to continue past the end
5729 : : of the block below. Otherwise return early. */
5730 : 0 : bool bail = true;
5731 : :
5732 : 0 : if (TREE_CODE (t) == CALL_EXPR && CALL_EXPR_FN (t) == NULL_TREE)
5733 : 0 : switch (CALL_EXPR_IFN (t))
5734 : : {
5735 : : /* These should be ignored, they are optimized away from
5736 : : constexpr functions. */
5737 : : case IFN_UBSAN_NULL:
5738 : : case IFN_UBSAN_BOUNDS:
5739 : : case IFN_UBSAN_VPTR:
5740 : : case IFN_FALLTHROUGH:
5741 : : return true;
5742 : :
5743 : : case IFN_ADD_OVERFLOW:
5744 : : case IFN_SUB_OVERFLOW:
5745 : : case IFN_MUL_OVERFLOW:
5746 : : case IFN_LAUNDER:
5747 : : case IFN_VEC_CONVERT:
5748 : : bail = false;
5749 : : break;
5750 : :
5751 : : default:
5752 : : break;
5753 : : }
5754 : :
5755 : : if (bail)
5756 : : {
5757 : : /* fold_call_expr can't do anything with IFN calls. */
5758 : 0 : if (flags & tf_error)
5759 : 0 : error_at (loc, "call to internal function %qE", t);
5760 : 0 : return false;
5761 : : }
5762 : : }
5763 : :
5764 : 0 : if (fun && is_overloaded_fn (fun))
5765 : : {
5766 : 0 : if (TREE_CODE (fun) == FUNCTION_DECL)
5767 : : {
5768 : 0 : if (builtin_valid_in_constant_expr_p (fun))
5769 : : return true;
5770 : 0 : if (!maybe_constexpr_fn (fun)
5771 : : /* Allow any built-in function; if the expansion
5772 : : isn't constant, we'll deal with that then. */
5773 : 0 : && !fndecl_built_in_p (fun)
5774 : : /* In C++20, replaceable global allocation functions
5775 : : are constant expressions. */
5776 : 0 : && (/* !cxx_replaceable_global_alloc_fn (fun)
5777 : 0 : ||*/ TREE_CODE (t) != CALL_EXPR
5778 : 0 : || (!CALL_FROM_NEW_OR_DELETE_P (t)
5779 : 0 : && (current_function_decl == NULL_TREE
5780 : : /*|| !is_std_allocator_allocate(current_function_decl)*/)))
5781 : : /* Allow placement new in std::construct_at. */
5782 : 0 : && (/*!cxx_placement_new_fn (fun)
5783 : : ||*/ TREE_CODE (t) != CALL_EXPR
5784 : 0 : || current_function_decl == NULL_TREE
5785 : : /*|| !is_std_construct_at (current_function_decl)*/)
5786 : : /* && !cxx_dynamic_cast_fn_p (fun)*/)
5787 : : {
5788 : 0 : if (flags & tf_error)
5789 : : {
5790 : 0 : error_at (loc, "call to non-%<constexpr%> function %qD",
5791 : : fun);
5792 : 0 : explain_invalid_constexpr_fn (fun);
5793 : : }
5794 : 0 : return false;
5795 : : }
5796 : : /* A call to a non-static member function takes the address
5797 : : of the object as the first argument. But in a constant
5798 : : expression the address will be folded away, so look
5799 : : through it now. */
5800 : 0 : if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
5801 : 0 : && !DECL_CONSTRUCTOR_P (fun))
5802 : : {
5803 : 0 : tree x = CALL_EXPR_ARG (t, 0);
5804 : :
5805 : : /* Don't require an immediately constant value, as
5806 : : constexpr substitution might not use the value. */
5807 : 0 : bool sub_now = false;
5808 : 0 : if (!potential_constant_expression_1 (x, rval, strict,
5809 : : sub_now, flags,
5810 : : jump_target))
5811 : : return false;
5812 : : i = 1;
5813 : : }
5814 : : }
5815 : : else
5816 : : {
5817 : 0 : if (!RECUR (fun, true))
5818 : : return false;
5819 : 0 : fun = get_first_fn (fun);
5820 : : }
5821 : 0 : fun = DECL_ORIGIN (fun);
5822 : : }
5823 : 0 : else if (fun)
5824 : : {
5825 : 0 : if (RECUR (fun, rval))
5826 : : /* Might end up being a constant function pointer. */;
5827 : : else
5828 : : return false;
5829 : : }
5830 : 0 : for (; i < nargs; ++i)
5831 : : {
5832 : 0 : tree x = CALL_EXPR_ARG (t, i);
5833 : : /* In a template, reference arguments haven't been converted to
5834 : : REFERENCE_TYPE and we might not even know if the parameter
5835 : : is a reference, so accept lvalue constants too. */
5836 : 0 : bool rv = rval;
5837 : : /* Don't require an immediately constant value, as constexpr
5838 : : substitution might not use the value of the argument. */
5839 : 0 : bool sub_now = false;
5840 : 0 : if (!potential_constant_expression_1 (x, rv, strict, sub_now, flags,
5841 : : jump_target))
5842 : : return false;
5843 : : }
5844 : : return true;
5845 : : }
5846 : :
5847 : 0 : case NON_LVALUE_EXPR:
5848 : : /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
5849 : : -- an lvalue of integral type that refers to a non-volatile
5850 : : const variable or static data member initialized with
5851 : : constant expressions, or
5852 : :
5853 : : -- an lvalue of literal type that refers to non-volatile
5854 : : object defined with constexpr, or that refers to a
5855 : : sub-object of such an object; */
5856 : 0 : return RECUR (TREE_OPERAND (t, 0), rval);
5857 : :
5858 : 0 : case VAR_DECL:
5859 : 0 : if (DECL_HAS_VALUE_EXPR_P (t))
5860 : : {
5861 : 0 : return RECUR (DECL_VALUE_EXPR (t), rval);
5862 : : }
5863 : 0 : if (want_rval && !var_in_maybe_constexpr_fn (t)
5864 : 0 : && !decl_maybe_constant_var_p (t)
5865 : 0 : && (strict || !RS_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
5866 : 0 : || (DECL_INITIAL (t)
5867 : 0 : && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t)))
5868 : 0 : && COMPLETE_TYPE_P (TREE_TYPE (t))
5869 : 0 : && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/ false))
5870 : : {
5871 : 0 : if (flags & tf_error)
5872 : 0 : non_const_var_error (loc, t);
5873 : 0 : return false;
5874 : : }
5875 : : return true;
5876 : :
5877 : : /* FALLTHRU */
5878 : 0 : case NOP_EXPR:
5879 : 0 : case CONVERT_EXPR:
5880 : 0 : case VIEW_CONVERT_EXPR:
5881 : : /* -- a reinterpret_cast. FIXME not implemented, and this rule
5882 : : may change to something more specific to type-punning (DR 1312). */
5883 : 0 : {
5884 : 0 : tree from = TREE_OPERAND (t, 0);
5885 : 0 : if (location_wrapper_p (t))
5886 : 0 : return (RECUR (from, want_rval));
5887 : 0 : if (INDIRECT_TYPE_P (TREE_TYPE (t)))
5888 : : {
5889 : 0 : STRIP_ANY_LOCATION_WRAPPER (from);
5890 : 0 : if (TREE_CODE (from) == INTEGER_CST && !integer_zerop (from))
5891 : : {
5892 : 0 : if (flags & tf_error)
5893 : 0 : error_at (loc,
5894 : : "%<reinterpret_cast%> from integer to pointer");
5895 : 0 : return false;
5896 : : }
5897 : : }
5898 : 0 : return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
5899 : : }
5900 : :
5901 : 0 : case ADDR_EXPR:
5902 : : /* -- a unary operator & that is applied to an lvalue that
5903 : : designates an object with thread or automatic storage
5904 : : duration; */
5905 : 0 : t = TREE_OPERAND (t, 0);
5906 : :
5907 : 0 : if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
5908 : : /* A pointer-to-member constant. */
5909 : : return true;
5910 : :
5911 : : // handle_addr_expr:
5912 : : #if 0
5913 : : /* FIXME adjust when issue 1197 is fully resolved. For now don't do
5914 : : any checking here, as we might dereference the pointer later. If
5915 : : we remove this code, also remove check_automatic_or_tls. */
5916 : : i = check_automatic_or_tls (t);
5917 : : if (i == ck_ok)
5918 : : return true;
5919 : : if (i == ck_bad)
5920 : : {
5921 : : if (flags & tf_error)
5922 : : error ("address-of an object %qE with thread local or "
5923 : : "automatic storage is not a constant expression", t);
5924 : : return false;
5925 : : }
5926 : : #endif
5927 : 0 : return RECUR (t, any);
5928 : :
5929 : 0 : case COMPONENT_REF:
5930 : : /* -- a class member access unless its postfix-expression is
5931 : : of literal type or of pointer to literal type. */
5932 : : /* This test would be redundant, as it follows from the
5933 : : postfix-expression being a potential constant expression. */
5934 : 0 : if (type_unknown_p (t))
5935 : : return true;
5936 : 0 : if (is_overloaded_fn (t))
5937 : : /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
5938 : : which uses ob as an lvalue. */
5939 : 0 : want_rval = false;
5940 : 0 : gcc_fallthrough ();
5941 : :
5942 : 0 : case REALPART_EXPR:
5943 : 0 : case IMAGPART_EXPR:
5944 : 0 : case BIT_FIELD_REF:
5945 : 0 : return RECUR (TREE_OPERAND (t, 0), want_rval);
5946 : :
5947 : 0 : case INDIRECT_REF:
5948 : 0 : {
5949 : 0 : tree x = TREE_OPERAND (t, 0);
5950 : 0 : STRIP_NOPS (x);
5951 : 0 : return RECUR (x, rval);
5952 : : }
5953 : :
5954 : 0 : case STATEMENT_LIST:
5955 : 0 : for (tree stmt : tsi_range (t))
5956 : 0 : if (!RECUR (stmt, any))
5957 : 0 : return false;
5958 : : return true;
5959 : :
5960 : 0 : case MODIFY_EXPR:
5961 : 0 : if (!RECUR (TREE_OPERAND (t, 0), any))
5962 : : return false;
5963 : : /* Just ignore clobbers. */
5964 : 0 : if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
5965 : : return true;
5966 : 0 : if (!RECUR (TREE_OPERAND (t, 1), rval))
5967 : : return false;
5968 : : return true;
5969 : :
5970 : 0 : case FOR_STMT:
5971 : 0 : if (!RECUR (FOR_INIT_STMT (t), any))
5972 : : return false;
5973 : 0 : tmp = FOR_COND (t);
5974 : 0 : if (!RECUR (tmp, rval))
5975 : : return false;
5976 : 0 : if (tmp)
5977 : : {
5978 : 0 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
5979 : : /* If we couldn't evaluate the condition, it might not ever be
5980 : : true. */
5981 : 0 : if (!integer_onep (tmp))
5982 : : {
5983 : : /* Before returning true, check if the for body can contain
5984 : : a return. */
5985 : 0 : hash_set<tree> pset;
5986 : 0 : check_for_return_continue_data data
5987 : 0 : = {&pset, NULL_TREE, NULL_TREE};
5988 : 0 : if (tree ret_expr
5989 : 0 : = rs_walk_tree (&FOR_BODY (t), check_for_return_continue,
5990 : : &data, &pset))
5991 : 0 : *jump_target = ret_expr;
5992 : 0 : return true;
5993 : 0 : }
5994 : : }
5995 : 0 : if (!RECUR (FOR_EXPR (t), any))
5996 : : return false;
5997 : 0 : if (!RECUR (FOR_BODY (t), any))
5998 : : return false;
5999 : 0 : if (breaks (jump_target) || continues (jump_target))
6000 : 0 : *jump_target = NULL_TREE;
6001 : : return true;
6002 : :
6003 : 0 : case WHILE_STMT:
6004 : 0 : tmp = WHILE_COND (t);
6005 : 0 : if (!RECUR (tmp, rval))
6006 : : return false;
6007 : :
6008 : 0 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
6009 : : /* If we couldn't evaluate the condition, it might not ever be true. */
6010 : 0 : if (!integer_onep (tmp))
6011 : : {
6012 : : /* Before returning true, check if the while body can contain
6013 : : a return. */
6014 : 0 : hash_set<tree> pset;
6015 : 0 : check_for_return_continue_data data = {&pset, NULL_TREE, NULL_TREE};
6016 : 0 : if (tree ret_expr
6017 : 0 : = rs_walk_tree (&WHILE_BODY (t), check_for_return_continue, &data,
6018 : : &pset))
6019 : 0 : *jump_target = ret_expr;
6020 : 0 : return true;
6021 : 0 : }
6022 : 0 : if (!RECUR (WHILE_BODY (t), any))
6023 : : return false;
6024 : 0 : if (breaks (jump_target) || continues (jump_target))
6025 : 0 : *jump_target = NULL_TREE;
6026 : : return true;
6027 : :
6028 : 0 : case SWITCH_STMT:
6029 : 0 : if (!RECUR (SWITCH_STMT_COND (t), rval))
6030 : : return false;
6031 : : /* FIXME we don't check SWITCH_STMT_BODY currently, because even
6032 : : unreachable labels would be checked and it is enough if there is
6033 : : a single switch cond value for which it is a valid constant
6034 : : expression. We need to check if there are any RETURN_EXPRs
6035 : : or CONTINUE_STMTs inside of the body though, as in that case
6036 : : we need to set *jump_target. */
6037 : : else
6038 : : {
6039 : 0 : hash_set<tree> pset;
6040 : 0 : check_for_return_continue_data data = {&pset, NULL_TREE, NULL_TREE};
6041 : 0 : if (tree ret_expr
6042 : 0 : = rs_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue,
6043 : : &data, &pset))
6044 : : /* The switch might return. */
6045 : 0 : *jump_target = ret_expr;
6046 : 0 : else if (data.continue_stmt)
6047 : : /* The switch can't return, but might continue. */
6048 : 0 : *jump_target = data.continue_stmt;
6049 : 0 : }
6050 : 0 : return true;
6051 : :
6052 : 0 : case DYNAMIC_CAST_EXPR:
6053 : 0 : case PSEUDO_DTOR_EXPR:
6054 : 0 : case NEW_EXPR:
6055 : 0 : case VEC_NEW_EXPR:
6056 : 0 : case DELETE_EXPR:
6057 : 0 : case VEC_DELETE_EXPR:
6058 : 0 : case THROW_EXPR:
6059 : 0 : case OMP_PARALLEL:
6060 : 0 : case OMP_TASK:
6061 : 0 : case OMP_FOR:
6062 : 0 : case OMP_SIMD:
6063 : 0 : case OMP_DISTRIBUTE:
6064 : 0 : case OMP_TASKLOOP:
6065 : 0 : case OMP_LOOP:
6066 : 0 : case OMP_TEAMS:
6067 : 0 : case OMP_TARGET_DATA:
6068 : 0 : case OMP_TARGET:
6069 : 0 : case OMP_SECTIONS:
6070 : 0 : case OMP_ORDERED:
6071 : 0 : case OMP_CRITICAL:
6072 : 0 : case OMP_SINGLE:
6073 : 0 : case OMP_SECTION:
6074 : 0 : case OMP_MASTER:
6075 : 0 : case OMP_MASKED:
6076 : 0 : case OMP_TASKGROUP:
6077 : 0 : case OMP_TARGET_UPDATE:
6078 : 0 : case OMP_TARGET_ENTER_DATA:
6079 : 0 : case OMP_TARGET_EXIT_DATA:
6080 : 0 : case OMP_ATOMIC:
6081 : 0 : case OMP_ATOMIC_READ:
6082 : 0 : case OMP_ATOMIC_CAPTURE_OLD:
6083 : 0 : case OMP_ATOMIC_CAPTURE_NEW:
6084 : 0 : case OMP_DEPOBJ:
6085 : 0 : case OACC_PARALLEL:
6086 : 0 : case OACC_KERNELS:
6087 : 0 : case OACC_SERIAL:
6088 : 0 : case OACC_DATA:
6089 : 0 : case OACC_HOST_DATA:
6090 : 0 : case OACC_LOOP:
6091 : 0 : case OACC_CACHE:
6092 : 0 : case OACC_DECLARE:
6093 : 0 : case OACC_ENTER_DATA:
6094 : 0 : case OACC_EXIT_DATA:
6095 : 0 : case OACC_UPDATE:
6096 : : /* GCC internal stuff. */
6097 : 0 : case VA_ARG_EXPR:
6098 : 0 : case TRANSACTION_EXPR:
6099 : 0 : case AT_ENCODE_EXPR:
6100 : :
6101 : 0 : if (flags & tf_error)
6102 : 0 : error_at (loc, "expression %qE is not a constant expression", t);
6103 : : return false;
6104 : :
6105 : 0 : case ASM_EXPR:
6106 : 0 : if (flags & tf_error)
6107 : 0 : inline_asm_in_constexpr_error (loc);
6108 : : return false;
6109 : :
6110 : : case OBJ_TYPE_REF:
6111 : : return true;
6112 : :
6113 : 0 : case POINTER_DIFF_EXPR:
6114 : 0 : case MINUS_EXPR:
6115 : 0 : want_rval = true;
6116 : 0 : goto binary;
6117 : :
6118 : 0 : case LT_EXPR:
6119 : 0 : case LE_EXPR:
6120 : 0 : case GT_EXPR:
6121 : 0 : case GE_EXPR:
6122 : 0 : case EQ_EXPR:
6123 : 0 : case NE_EXPR:
6124 : 0 : case SPACESHIP_EXPR:
6125 : 0 : want_rval = true;
6126 : 0 : goto binary;
6127 : :
6128 : 0 : case PREINCREMENT_EXPR:
6129 : 0 : case POSTINCREMENT_EXPR:
6130 : 0 : case PREDECREMENT_EXPR:
6131 : 0 : case POSTDECREMENT_EXPR:
6132 : 0 : goto unary;
6133 : :
6134 : 0 : case BIT_NOT_EXPR:
6135 : : /* A destructor. */
6136 : 0 : if (TYPE_P (TREE_OPERAND (t, 0)))
6137 : : return true;
6138 : : /* fall through. */
6139 : :
6140 : 0 : case CONJ_EXPR:
6141 : 0 : case SAVE_EXPR:
6142 : 0 : case FIX_TRUNC_EXPR:
6143 : 0 : case FLOAT_EXPR:
6144 : 0 : case NEGATE_EXPR:
6145 : 0 : case ABS_EXPR:
6146 : 0 : case ABSU_EXPR:
6147 : 0 : case TRUTH_NOT_EXPR:
6148 : 0 : case FIXED_CONVERT_EXPR:
6149 : 0 : case UNARY_PLUS_EXPR:
6150 : 0 : case UNARY_LEFT_FOLD_EXPR:
6151 : 0 : case UNARY_RIGHT_FOLD_EXPR:
6152 : 0 : unary:
6153 : 0 : return RECUR (TREE_OPERAND (t, 0), rval);
6154 : :
6155 : 0 : case BIND_EXPR:
6156 : 0 : return RECUR (BIND_EXPR_BODY (t), want_rval);
6157 : :
6158 : 0 : case CLEANUP_POINT_EXPR:
6159 : 0 : case EXPR_STMT:
6160 : 0 : case PAREN_EXPR:
6161 : : /* For convenience. */
6162 : 0 : case LOOP_EXPR:
6163 : 0 : case EXIT_EXPR:
6164 : 0 : return RECUR (TREE_OPERAND (t, 0), want_rval);
6165 : :
6166 : 0 : case DECL_EXPR:
6167 : 0 : tmp = DECL_EXPR_DECL (t);
6168 : 0 : if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp))
6169 : : {
6170 : 0 : if (RS_DECL_THREAD_LOCAL_P (tmp))
6171 : : {
6172 : 0 : if (flags & tf_error)
6173 : 0 : error_at (DECL_SOURCE_LOCATION (tmp),
6174 : : "%qD declared "
6175 : : "%<thread_local%> in %<constexpr%> context",
6176 : : tmp);
6177 : 0 : return false;
6178 : : }
6179 : 0 : else if (TREE_STATIC (tmp))
6180 : : {
6181 : 0 : if (flags & tf_error)
6182 : 0 : error_at (DECL_SOURCE_LOCATION (tmp),
6183 : : "%qD declared "
6184 : : "%<static%> in %<constexpr%> context",
6185 : : tmp);
6186 : 0 : return false;
6187 : : }
6188 : 0 : else if (!check_for_uninitialized_const_var (
6189 : : tmp, /*constexpr_context_p=*/true, flags))
6190 : : return false;
6191 : : }
6192 : 0 : return RECUR (tmp, want_rval);
6193 : :
6194 : 0 : case TRY_FINALLY_EXPR:
6195 : 0 : return (RECUR (TREE_OPERAND (t, 0), want_rval)
6196 : 0 : && RECUR (TREE_OPERAND (t, 1), any));
6197 : :
6198 : 0 : case SCOPE_REF:
6199 : 0 : return RECUR (TREE_OPERAND (t, 1), want_rval);
6200 : :
6201 : 0 : case TARGET_EXPR:
6202 : 0 : if (!TARGET_EXPR_DIRECT_INIT_P (t) && !literal_type_p (TREE_TYPE (t)))
6203 : : {
6204 : 0 : if (flags & tf_error)
6205 : : {
6206 : 0 : auto_diagnostic_group d;
6207 : 0 : error_at (loc,
6208 : : "temporary of non-literal type %qT in a "
6209 : : "constant expression",
6210 : 0 : TREE_TYPE (t));
6211 : 0 : explain_non_literal_class (TREE_TYPE (t));
6212 : 0 : }
6213 : 0 : return false;
6214 : : }
6215 : : /* FALLTHRU */
6216 : 0 : case INIT_EXPR:
6217 : 0 : return RECUR (TREE_OPERAND (t, 1), rval);
6218 : :
6219 : 0 : case CONSTRUCTOR:
6220 : 0 : {
6221 : 0 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
6222 : 0 : constructor_elt *ce;
6223 : 0 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
6224 : 0 : if (!RECUR (ce->value, want_rval))
6225 : : return false;
6226 : : return true;
6227 : : }
6228 : :
6229 : 0 : case TREE_LIST:
6230 : 0 : {
6231 : 0 : gcc_assert (TREE_PURPOSE (t) == NULL_TREE || DECL_P (TREE_PURPOSE (t)));
6232 : 0 : if (!RECUR (TREE_VALUE (t), want_rval))
6233 : : return false;
6234 : 0 : if (TREE_CHAIN (t) == NULL_TREE)
6235 : : return true;
6236 : 0 : return RECUR (TREE_CHAIN (t), want_rval);
6237 : : }
6238 : :
6239 : 0 : case TRUNC_DIV_EXPR:
6240 : 0 : case CEIL_DIV_EXPR:
6241 : 0 : case FLOOR_DIV_EXPR:
6242 : 0 : case ROUND_DIV_EXPR:
6243 : 0 : case TRUNC_MOD_EXPR:
6244 : 0 : case CEIL_MOD_EXPR:
6245 : 0 : case ROUND_MOD_EXPR:
6246 : 0 : {
6247 : 0 : tree denom = TREE_OPERAND (t, 1);
6248 : 0 : if (!RECUR (denom, rval))
6249 : : return false;
6250 : : /* We can't call cxx_eval_outermost_constant_expr on an expression
6251 : : that hasn't been through instantiate_non_dependent_expr yet. */
6252 : 0 : denom = cxx_eval_outermost_constant_expr (denom, true);
6253 : 0 : if (integer_zerop (denom))
6254 : : {
6255 : 0 : if (flags & tf_error)
6256 : 0 : error ("division by zero is not a constant expression");
6257 : 0 : return false;
6258 : : }
6259 : : else
6260 : : {
6261 : 0 : want_rval = true;
6262 : 0 : return RECUR (TREE_OPERAND (t, 0), want_rval);
6263 : : }
6264 : : }
6265 : :
6266 : 0 : case COMPOUND_EXPR:
6267 : 0 : {
6268 : : /* check_return_expr sometimes wraps a TARGET_EXPR in a
6269 : : COMPOUND_EXPR; don't get confused. */
6270 : 0 : tree op0 = TREE_OPERAND (t, 0);
6271 : 0 : tree op1 = TREE_OPERAND (t, 1);
6272 : 0 : STRIP_NOPS (op1);
6273 : 0 : if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
6274 : 0 : return RECUR (op0, want_rval);
6275 : : else
6276 : 0 : goto binary;
6277 : : }
6278 : :
6279 : : /* If the first operand is the non-short-circuit constant, look at
6280 : : the second operand; otherwise we only care about the first one for
6281 : : potentiality. */
6282 : 0 : case TRUTH_AND_EXPR:
6283 : 0 : case TRUTH_ANDIF_EXPR:
6284 : 0 : tmp = boolean_true_node;
6285 : 0 : goto truth;
6286 : 0 : case TRUTH_OR_EXPR:
6287 : 0 : case TRUTH_ORIF_EXPR:
6288 : 0 : tmp = boolean_false_node;
6289 : 0 : truth:
6290 : 0 : {
6291 : 0 : tree op0 = TREE_OPERAND (t, 0);
6292 : 0 : tree op1 = TREE_OPERAND (t, 1);
6293 : 0 : if (!RECUR (op0, rval))
6294 : : return false;
6295 : 0 : if (!(flags & tf_error) && RECUR (op1, rval))
6296 : : /* When quiet, try to avoid expensive trial evaluation by first
6297 : : checking potentiality of the second operand. */
6298 : : return true;
6299 : 0 : op0 = cxx_eval_outermost_constant_expr (op0, true);
6300 : 0 : if (tree_int_cst_equal (op0, tmp))
6301 : 0 : return (flags & tf_error) ? RECUR (op1, rval) : false;
6302 : : else
6303 : : return true;
6304 : : }
6305 : :
6306 : : case PLUS_EXPR:
6307 : : case MULT_EXPR:
6308 : : case POINTER_PLUS_EXPR:
6309 : : case RDIV_EXPR:
6310 : : case EXACT_DIV_EXPR:
6311 : : case MIN_EXPR:
6312 : : case MAX_EXPR:
6313 : : case LSHIFT_EXPR:
6314 : : case RSHIFT_EXPR:
6315 : : case LROTATE_EXPR:
6316 : : case RROTATE_EXPR:
6317 : : case BIT_IOR_EXPR:
6318 : : case BIT_XOR_EXPR:
6319 : : case BIT_AND_EXPR:
6320 : : case TRUTH_XOR_EXPR:
6321 : : case UNORDERED_EXPR:
6322 : : case ORDERED_EXPR:
6323 : : case UNLT_EXPR:
6324 : : case UNLE_EXPR:
6325 : : case UNGT_EXPR:
6326 : : case UNGE_EXPR:
6327 : : case UNEQ_EXPR:
6328 : : case LTGT_EXPR:
6329 : : case RANGE_EXPR:
6330 : : case COMPLEX_EXPR:
6331 : 0 : want_rval = true;
6332 : : /* Fall through. */
6333 : 0 : case ARRAY_REF:
6334 : 0 : case ARRAY_RANGE_REF:
6335 : 0 : case MEMBER_REF:
6336 : 0 : case DOTSTAR_EXPR:
6337 : 0 : case MEM_REF:
6338 : 0 : case BINARY_LEFT_FOLD_EXPR:
6339 : 0 : case BINARY_RIGHT_FOLD_EXPR:
6340 : 0 : binary:
6341 : 0 : for (i = 0; i < 2; ++i)
6342 : 0 : if (!RECUR (TREE_OPERAND (t, i), want_rval))
6343 : : return false;
6344 : : return true;
6345 : :
6346 : : case VEC_PERM_EXPR:
6347 : 0 : for (i = 0; i < 3; ++i)
6348 : 0 : if (!RECUR (TREE_OPERAND (t, i), true))
6349 : : return false;
6350 : : return true;
6351 : :
6352 : 0 : case COND_EXPR:
6353 : 0 : if (COND_EXPR_IS_VEC_DELETE (t))
6354 : : {
6355 : 0 : if (flags & tf_error)
6356 : 0 : error_at (loc, "%<delete[]%> is not a constant expression");
6357 : 0 : return false;
6358 : : }
6359 : : /* Fall through. */
6360 : 0 : case IF_STMT:
6361 : 0 : case VEC_COND_EXPR:
6362 : : /* If the condition is a known constant, we know which of the legs we
6363 : : care about; otherwise we only require that the condition and
6364 : : either of the legs be potentially constant. */
6365 : 0 : tmp = TREE_OPERAND (t, 0);
6366 : 0 : if (!RECUR (tmp, rval))
6367 : : return false;
6368 : :
6369 : 0 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
6370 : : /* potential_constant_expression* isn't told if it is called for
6371 : : manifestly_const_eval or not, so for consteval if always
6372 : : process both branches as if the condition is not a known
6373 : : constant. */
6374 : 0 : if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t))
6375 : : {
6376 : 0 : if (integer_zerop (tmp))
6377 : 0 : return RECUR (TREE_OPERAND (t, 2), want_rval);
6378 : 0 : else if (TREE_CODE (tmp) == INTEGER_CST)
6379 : 0 : return RECUR (TREE_OPERAND (t, 1), want_rval);
6380 : : }
6381 : 0 : tmp = *jump_target;
6382 : 0 : for (i = 1; i < 3; ++i)
6383 : : {
6384 : 0 : tree this_jump_target = tmp;
6385 : 0 : if (potential_constant_expression_1 (TREE_OPERAND (t, i), want_rval,
6386 : : strict, now, tf_none,
6387 : : &this_jump_target))
6388 : : {
6389 : 0 : if (returns (&this_jump_target))
6390 : 0 : *jump_target = this_jump_target;
6391 : 0 : else if (!returns (jump_target))
6392 : : {
6393 : 0 : if (breaks (&this_jump_target)
6394 : 0 : || continues (&this_jump_target))
6395 : 0 : *jump_target = this_jump_target;
6396 : 0 : if (i == 1)
6397 : : {
6398 : : /* If the then branch is potentially constant, but
6399 : : does not return, check if the else branch
6400 : : couldn't return, break or continue. */
6401 : 0 : hash_set<tree> pset;
6402 : 0 : check_for_return_continue_data data
6403 : 0 : = {&pset, NULL_TREE, NULL_TREE};
6404 : 0 : if (tree ret_expr
6405 : 0 : = rs_walk_tree (&TREE_OPERAND (t, 2),
6406 : : check_for_return_continue, &data,
6407 : : &pset))
6408 : 0 : *jump_target = ret_expr;
6409 : 0 : else if (*jump_target == NULL_TREE)
6410 : : {
6411 : 0 : if (data.continue_stmt)
6412 : 0 : *jump_target = data.continue_stmt;
6413 : 0 : else if (data.break_stmt)
6414 : 0 : *jump_target = data.break_stmt;
6415 : : }
6416 : 0 : }
6417 : : }
6418 : 0 : return true;
6419 : : }
6420 : : }
6421 : 0 : if (flags & tf_error)
6422 : 0 : error_at (loc, "expression %qE is not a constant expression", t);
6423 : : return false;
6424 : :
6425 : : case TYPE_DECL:
6426 : : /* We can see these in statement-expressions. */
6427 : : return true;
6428 : :
6429 : 0 : case LABEL_EXPR:
6430 : 0 : t = LABEL_EXPR_LABEL (t);
6431 : 0 : if (DECL_ARTIFICIAL (t))
6432 : : return true;
6433 : 0 : else if (flags & tf_error)
6434 : 0 : error_at (loc, "label definition in %<constexpr%> function only "
6435 : : "available with %<-std=c++2b%> or %<-std=gnu++2b%>");
6436 : : return false;
6437 : :
6438 : 0 : case ANNOTATE_EXPR:
6439 : 0 : return RECUR (TREE_OPERAND (t, 0), rval);
6440 : :
6441 : 0 : case BIT_CAST_EXPR:
6442 : 0 : return RECUR (TREE_OPERAND (t, 0), rval);
6443 : :
6444 : 0 : default:
6445 : 0 : sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
6446 : 0 : rust_unreachable ();
6447 : : return false;
6448 : : }
6449 : : #undef RECUR
6450 : : }
6451 : :
6452 : : bool
6453 : 0 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
6454 : : tsubst_flags_t flags)
6455 : : {
6456 : 0 : if (flags & tf_error)
6457 : : {
6458 : : /* Check potentiality quietly first, as that could be performed more
6459 : : efficiently in some cases (currently only for TRUTH_*_EXPR). If
6460 : : that fails, replay the check noisily to give errors. */
6461 : 0 : flags &= ~tf_error;
6462 : 0 : if (potential_constant_expression_1 (t, want_rval, strict, now, flags))
6463 : : return true;
6464 : 0 : flags |= tf_error;
6465 : : }
6466 : :
6467 : 0 : tree target = NULL_TREE;
6468 : 0 : return potential_constant_expression_1 (t, want_rval, strict, now, flags,
6469 : 0 : &target);
6470 : : }
6471 : :
6472 : : // forked from gcc/cp/constexpr.cc fold_non_dependent_init
6473 : :
6474 : : /* Like maybe_constant_init but first fully instantiate the argument. */
6475 : :
6476 : : tree
6477 : 0 : fold_non_dependent_init (tree t, tsubst_flags_t /*=tf_warning_or_error*/,
6478 : : bool manifestly_const_eval /*=false*/,
6479 : : tree object /* = NULL_TREE */)
6480 : : {
6481 : 0 : if (t == NULL_TREE)
6482 : : return NULL_TREE;
6483 : :
6484 : 0 : return maybe_constant_init (t, object, manifestly_const_eval);
6485 : : }
6486 : :
6487 : : } // namespace Compile
6488 : : } // namespace Rust
6489 : :
6490 : : using namespace Rust::Compile;
6491 : :
6492 : : #include "gt-rust-rust-constexpr.h"
|