From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 4E649385735D; Mon, 29 Aug 2022 15:34:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4E649385735D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661787260; bh=70ruQuaj1sQ6E6yfkSAcXZXHmviRV1VW4ObJiFuXwlE=; h=From:To:Subject:Date:From; b=TrF6S176Yl3NmjymEDxzIKoB7OaGUfwqCMSADkWvPMYaWKf4dQjOeosCW0TZFRO62 cRgWAAfp44IR90aC5KKJmMCh1N5Gl/kMXmc0Nd9ntDY3z0l0oQdVwaMNSgNzG6zpUh KW0QX9DDJsk6S3VllYNRXKacuRFVumEwDTTX0zwA= 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 more cases to eval_constant_expression(). Its possible many of them are X-Act-Checkin: gcc X-Git-Author: Faisal Abbas <90.abbasfaisal@gmail.com> X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: e8e58d54db23acc03bba3bd9a89cfe35cf3c2ba3 X-Git-Newrev: c628a66a92f72add62608c2c24cf0ec4c5ae45af Message-Id: <20220829153420.4E649385735D@sourceware.org> Date: Mon, 29 Aug 2022 15:34:20 +0000 (GMT) List-Id: https://gcc.gnu.org/g:c628a66a92f72add62608c2c24cf0ec4c5ae45af commit c628a66a92f72add62608c2c24cf0ec4c5ae45af Author: Faisal Abbas <90.abbasfaisal@gmail.com> Date: Sat Jul 30 20:54:06 2022 +0100 rust-constexpr.cc: port over more cases to eval_constant_expression(). Its possible many of them are not useful for Rust so we can remove them in the clean up expected later. Diff: --- gcc/rust/backend/rust-constexpr.cc | 110 +++++++++++++++++++++++++++++++++++++ gcc/rust/backend/rust-tree.h | 6 ++ 2 files changed, 116 insertions(+) diff --git a/gcc/rust/backend/rust-constexpr.cc b/gcc/rust/backend/rust-constexpr.cc index 357caef6946..3d7b8b0a73d 100644 --- a/gcc/rust/backend/rust-constexpr.cc +++ b/gcc/rust/backend/rust-constexpr.cc @@ -825,6 +825,8 @@ rs_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type, return NULL_TREE; } +// forked from gcc/cp/constexpr.cc cxx_eval_indirect_ref + static tree rs_eval_indirect_ref (const constexpr_ctx *ctx, tree t, bool lval, bool *non_constant_p, bool *overflow_p) @@ -935,6 +937,59 @@ eval_logical_expression (const constexpr_ctx *ctx, tree t, tree bailout_value, return r; } +// forked from gcc/cp/constexp.rcc lookup_placeholder + +/* Find the object of TYPE under initialization in CTX. */ + +static tree +lookup_placeholder (const constexpr_ctx *ctx, bool lval, tree type) +{ + if (!ctx) + return NULL_TREE; + + /* Prefer the outermost matching object, but don't cross + CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */ + if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor)) + if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type)) + return outer_ob; + + /* We could use ctx->object unconditionally, but using ctx->ctor when we + can is a minor optimization. */ + if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type)) + return ctx->ctor; + + if (!ctx->object) + return NULL_TREE; + + /* Since an object cannot have a field of its own type, we can search outward + from ctx->object to find the unique containing object of TYPE. */ + tree ob = ctx->object; + while (ob) + { + if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type)) + break; + if (handled_component_p (ob)) + ob = TREE_OPERAND (ob, 0); + else + ob = NULL_TREE; + } + + return ob; +} + +// forked from gcc/cp/constexp.rcc inline_asm_in_constexpr_error + +/* Complain about an attempt to evaluate inline assembly. */ + +static void +inline_asm_in_constexpr_error (location_t loc) +{ + auto_diagnostic_group d; + error_at (loc, "inline assembly is not a constant expression"); + inform (loc, "only unevaluated inline assembly is allowed in a " + "% function in C++20"); +} + static tree eval_constant_expression (const constexpr_ctx *ctx, tree t, bool lval, bool *non_constant_p, bool *overflow_p, @@ -1096,6 +1151,14 @@ eval_constant_expression (const constexpr_ctx *ctx, tree t, bool lval, r = rs_eval_indirect_ref (ctx, t, lval, non_constant_p, overflow_p); break; + case PAREN_EXPR: + gcc_assert (!REF_PARENTHESIZED_P (t)); + /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in + constant expressions since it's unaffected by -fassociative-math. */ + r = eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, + non_constant_p, overflow_p); + break; + case NOP_EXPR: if (REINTERPRET_CAST_P (t)) { @@ -1151,6 +1214,25 @@ eval_constant_expression (const constexpr_ctx *ctx, tree t, bool lval, } break; + case ADDR_EXPR: { + tree oldop = TREE_OPERAND (t, 0); + tree op = eval_constant_expression (ctx, oldop, + /*lval*/ true, non_constant_p, + overflow_p); + /* Don't VERIFY_CONSTANT here. */ + if (*non_constant_p) + return t; + gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR); + /* This function does more aggressive folding than fold itself. */ + r = build_fold_addr_expr_with_type (op, TREE_TYPE (t)); + if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop) + { + ggc_free (r); + return t; + } + break; + } + case REALPART_EXPR: case IMAGPART_EXPR: if (lval) @@ -1362,6 +1444,34 @@ eval_constant_expression (const constexpr_ctx *ctx, tree t, bool lval, } break; + case PLACEHOLDER_EXPR: + /* Use of the value or address of the current object. */ + if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t))) + { + if (TREE_CODE (ctor) == CONSTRUCTOR) + return ctor; + else + return eval_constant_expression (ctx, ctor, lval, non_constant_p, + overflow_p); + } + /* A placeholder without a referent. We can get here when + checking whether NSDMIs are noexcept, or in massage_init_elt; + just say it's non-constant for now. */ + gcc_assert (ctx->quiet); + *non_constant_p = true; + break; + + case ANNOTATE_EXPR: + r = eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, + non_constant_p, overflow_p, jump_target); + break; + + case ASM_EXPR: + if (!ctx->quiet) + inline_asm_in_constexpr_error (loc); + *non_constant_p = true; + return t; + default: break; } diff --git a/gcc/rust/backend/rust-tree.h b/gcc/rust/backend/rust-tree.h index d4b7d8671fe..966dbebf30c 100644 --- a/gcc/rust/backend/rust-tree.h +++ b/gcc/rust/backend/rust-tree.h @@ -1263,6 +1263,12 @@ extern GTY (()) tree cp_global_trees[CPTI_MAX]; in ascending tree code order. */ #define TYPE_PTROB_P(NODE) (TYPE_PTR_P (NODE) && TYPE_OBJ_P (TREE_TYPE (NODE))) +/* True if this CONSTRUCTOR contains PLACEHOLDER_EXPRs referencing the + CONSTRUCTOR's type not nested inside another CONSTRUCTOR marked with + CONSTRUCTOR_PLACEHOLDER_BOUNDARY. */ +#define CONSTRUCTOR_PLACEHOLDER_BOUNDARY(NODE) \ + (TREE_LANG_FLAG_5 (CONSTRUCTOR_CHECK (NODE))) + #if defined ENABLE_TREE_CHECKING #define LANG_DECL_MIN_CHECK(NODE) \