From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id DC99A385695C; Mon, 29 Aug 2022 15:33:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DC99A385695C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661787229; bh=C4TAZ5qRV0mndCOA/zx5XiE0Q/O4/qpk7OWJA4giL+U=; h=From:To:Subject:Date:From; b=wG8O6sp8nFP3oFLq4sMqB4oQB9nlprMJJstre0lk+iyqV9DMcnCU3MWhCKhzGnL9c joGW+DG/qeQvHcsSBE8flAy+ug/drswpygmcNO70uF+J+ID5fWJCuOnq2laycCuKGZ ll74m+Um8aesWoybqhhKBZcVNn1pTR/2mqTMkDDU= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] rust-constexpr.cc: port over cxx_eval_switch_expr X-Act-Checkin: gcc X-Git-Author: Faisal Abbas <90.abbasfaisal@gmail.com> X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: f21f475feca30d0c9fb74dfe3bb65a6d88d5311c X-Git-Newrev: e991065fdbee901d4bfe3af89e0d497e74dcc7d3 Message-Id: <20220829153349.DC99A385695C@sourceware.org> Date: Mon, 29 Aug 2022 15:33:49 +0000 (GMT) List-Id: https://gcc.gnu.org/g:e991065fdbee901d4bfe3af89e0d497e74dcc7d3 commit e991065fdbee901d4bfe3af89e0d497e74dcc7d3 Author: Faisal Abbas <90.abbasfaisal@gmail.com> Date: Fri Jul 29 05:35:34 2022 +0100 rust-constexpr.cc: port over cxx_eval_switch_expr Diff: --- gcc/rust/backend/rust-constexpr.cc | 56 ++++++++++++++++++++++++++++++++++++++ gcc/rust/backend/rust-tree.h | 5 ++++ 2 files changed, 61 insertions(+) diff --git a/gcc/rust/backend/rust-constexpr.cc b/gcc/rust/backend/rust-constexpr.cc index cac28d6870f..127451e5966 100644 --- a/gcc/rust/backend/rust-constexpr.cc +++ b/gcc/rust/backend/rust-constexpr.cc @@ -497,6 +497,10 @@ static tree eval_loop_expr (const constexpr_ctx *ctx, tree t, bool *non_constant_p, bool *overflow_p, tree *jump_target); +static tree +eval_switch_expr (const constexpr_ctx *ctx, tree t, bool *non_constant_p, + bool *overflow_p, tree *jump_target); + /* Variables and functions to manage constexpr call expansion context. These do not need to be marked for PCH or GC. */ @@ -741,6 +745,11 @@ eval_constant_expression (const constexpr_ctx *ctx, tree t, bool lval, eval_loop_expr (ctx, t, non_constant_p, overflow_p, jump_target); break; + case SWITCH_EXPR: + case SWITCH_STMT: + eval_switch_expr (ctx, t, non_constant_p, overflow_p, jump_target); + break; + case BIT_FIELD_REF: r = eval_bit_field_ref (ctx, t, lval, non_constant_p, overflow_p); break; @@ -2771,6 +2780,8 @@ eval_bit_field_ref (const constexpr_ctx *ctx, tree t, bool lval, return error_mark_node; } +// forked from gcc/cp/constexpr.cc returns + /* Predicates for the meaning of *jump_target. */ static bool @@ -2782,6 +2793,8 @@ returns (tree *jump_target) && LABEL_DECL_CDTOR (*jump_target))); } +// forked from gcc/cp/constexpr.cc breaks + static bool breaks (tree *jump_target) { @@ -2792,6 +2805,8 @@ breaks (tree *jump_target) || TREE_CODE (*jump_target) == EXIT_EXPR); } +// forked from gcc/cp/constexpr.cc continues + static bool continues (tree *jump_target) { @@ -2801,12 +2816,16 @@ continues (tree *jump_target) || TREE_CODE (*jump_target) == CONTINUE_STMT); } +// forked from gcc/cp/constexpr.cc switches + static bool switches (tree *jump_target) { return *jump_target && TREE_CODE (*jump_target) == INTEGER_CST; } +// forked from gcc/cp/constexpr.cc cxx_eval_loop_expr + /* Evaluate a LOOP_EXPR for side-effects. Handles break and return semantics; continue semantics are covered by cxx_eval_statement_list. */ @@ -2914,6 +2933,43 @@ eval_loop_expr (const constexpr_ctx *ctx, tree t, bool *non_constant_p, return NULL_TREE; } +// forked from gcc/cp/constexpr.cc cxx_eval_switch_expr + +/* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump + semantics. */ + +static tree +eval_switch_expr (const constexpr_ctx *ctx, tree t, bool *non_constant_p, + bool *overflow_p, tree *jump_target) +{ + tree cond + = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t); + cond + = eval_constant_expression (ctx, cond, false, non_constant_p, overflow_p); + VERIFY_CONSTANT (cond); + *jump_target = cond; + + tree body + = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t); + constexpr_ctx new_ctx = *ctx; + constexpr_switch_state css = css_default_not_seen; + new_ctx.css_state = &css; + eval_constant_expression (&new_ctx, body, false, non_constant_p, overflow_p, + jump_target); + if (switches (jump_target) && css == css_default_seen) + { + /* If the SWITCH_EXPR body has default: label, process it once again, + this time instructing label_matches to return true for default: + label on switches (jump_target). */ + css = css_default_processing; + eval_constant_expression (&new_ctx, body, false, non_constant_p, + overflow_p, jump_target); + } + if (breaks (jump_target) || switches (jump_target)) + *jump_target = NULL_TREE; + return NULL_TREE; +} + // #include "gt-rust-rust-constexpr.h" } // namespace Compile diff --git a/gcc/rust/backend/rust-tree.h b/gcc/rust/backend/rust-tree.h index f9623a2848a..757b721791f 100644 --- a/gcc/rust/backend/rust-tree.h +++ b/gcc/rust/backend/rust-tree.h @@ -1352,6 +1352,11 @@ extern GTY (()) tree cp_global_trees[CPTI_MAX]; #define FOR_BODY(NODE) TREE_OPERAND (FOR_STMT_CHECK (NODE), 3) #define FOR_SCOPE(NODE) TREE_OPERAND (FOR_STMT_CHECK (NODE), 4) +#define SWITCH_STMT_COND(NODE) TREE_OPERAND (SWITCH_STMT_CHECK (NODE), 0) +#define SWITCH_STMT_BODY(NODE) TREE_OPERAND (SWITCH_STMT_CHECK (NODE), 1) +#define SWITCH_STMT_TYPE(NODE) TREE_OPERAND (SWITCH_STMT_CHECK (NODE), 2) +#define SWITCH_STMT_SCOPE(NODE) TREE_OPERAND (SWITCH_STMT_CHECK (NODE), 3) + /* Nonzero if NODE is the target for genericization of 'break' stmts. */ #define LABEL_DECL_BREAK(NODE) DECL_LANG_FLAG_0 (LABEL_DECL_CHECK (NODE))