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