From patchwork Wed Apr 5 14:03:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 79654 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp344910vqo; Wed, 5 Apr 2023 07:21:01 -0700 (PDT) X-Google-Smtp-Source: AKy350aG6oomVTc+dE/KjUcHlWJvkBiIxnlObtFT3a+SKqOaIxgFk5TSMXh7WZ2oPaXmNgrjq/eZ X-Received: by 2002:a17:906:4547:b0:947:a6d8:6ea8 with SMTP id s7-20020a170906454700b00947a6d86ea8mr2772107ejq.21.1680704461791; Wed, 05 Apr 2023 07:21:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680704461; cv=none; d=google.com; s=arc-20160816; b=L/xWJt6twbaInFGqCQSW6lepKBMNuXikdrhgbrUUBwn5+aBmDpALIPXKA5qLfRXKZT dtSAE3PjvNmEgIYfZ0hKSievVL+UyvdIE5jXyUw+KNvNPgvnC9WS5kHR5r2lUM3dv8tp UMC/44ZdsxT0mc9AgoRn/nv2xnp95SQiweGz6wOyDdEOdaxE3oxv46k9mMJD6FN1frlq 9fwh0DHUjMb/R2NEt9iXeZkPjQSAynDaY2ZQtKxJ+4DVEgLbWm9CGWZoeGR5U25Xl047 +VT1pcAl2BCIX9AeNPrbDtXCBPK6+zuY8AUIZZ9DYZUVq88hwsHJiXBz50oCzgF6v8Jm XN5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature:dmarc-filter :delivered-to; bh=HcbuOV94LWC1C2MdNfwgmAGCZC9Vo6kcMiQCQqZU0eM=; b=IkbVtYPQsmtOx/r78hvR37gWEWX6opsq9FIKD8l3KG/MaJ4BmNKDGuJPK+sSrrGKUA wLbwuQBgshnMSfzcD6UgD3tTKoGERglmQdNRTlMbXOg0JJWdXTQAQJgaMd90KVEo1ybi XBZZFGxThhFPeo60I+yD8L9q9cFMhK9gttqAFCCBtAU1EXnuc4qO8dtF5PMaS5bfS/i0 A8Yt2gBbavdsGXz9e5WE+62FXSfL3EgXZQDot9kY6AWgbi6TRBCH12NGpfP3WADtPet3 AfAVjwwDSzS+WpDBj8lJY4ACIO/l6Ag6rNw0eqjMDzLXmRgZ/9zeiTxHgWhTey13WMzZ 1QKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=gK9liulf; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id 9-20020a170906218900b009377c5b6b0asi184595eju.275.2023.04.05.07.21.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 07:21:01 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=gK9liulf; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E523638ABF0B for ; Wed, 5 Apr 2023 14:10:13 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by sourceware.org (Postfix) with ESMTPS id 9367F3856DC7 for ; Wed, 5 Apr 2023 14:05:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9367F3856DC7 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com Received: by mail-wm1-x32f.google.com with SMTP id o32so20949209wms.1 for ; Wed, 05 Apr 2023 07:05:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1680703545; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=HcbuOV94LWC1C2MdNfwgmAGCZC9Vo6kcMiQCQqZU0eM=; b=gK9liulfTOH8LclOBwOVonz9348AH5qrySmAIoAewR2qwnvH1ZAHP2XNKylarFyghU fr7HKkGh9IiRiXeZHDsScpdeY6hsoLF8xXpi1iE+uoqpkSp5qxqcKdeo8UsEztmrW1B7 78gEuU7JZf7HQ3fL64kYxpAdq4Ob02jmcuABYWp64UMGVsSujJ6qQxBcpqnkERkDQj9b pTensMUyDKWeP6xVhnL7SGZGsJbYczVS2AJB+XWw12ynZAXWIJ3jzmBLfHC9HfZ80lWG D3pFwC1USpQOluqdVkP6ikRlgbWCN3MTOBme7QFtI/rpP+UG4iYr+71/WycxB0h/I4Da Tp/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680703545; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=HcbuOV94LWC1C2MdNfwgmAGCZC9Vo6kcMiQCQqZU0eM=; b=N3AY+HV/fzutMZd+fIdZpPo/1wHEVQMdfVnnRB4Ve/8kz5tNJzq9jtnbzcxkhA0S78 UU6XSwbJH7HfVzRVAgiFjg+/kG+cYBPQHgo/v368AwFI8dRtOxpVDdTI6oMCMvql3u+K SsPT9nfFcnBNgz2YLfRwBax2cyy5Pmsa441VmpAXEtxDFDrhsoUSzpWzG4iPUC7cGr3o fvfggfaGt4+WmIDpvdtOYifx3O7kUOxck5tccDLU6MloJc4l5M6Aen2Gb8a5xAeUKpHB P3udKfdU4+Hbt+qRyaAU488UP46JUQOLaxcT669JQ3maPx9273MSY0uA4PjaiVBjJuAO 9heA== X-Gm-Message-State: AAQBX9dMf/Hb9BToCm8HDH7E4Qbff5tKjzFUI9gTd2Whoga7OvG0XAQ+ graJ6EmK++SSCdoQxaOybr03Lb0CwkQLAfWgxw== X-Received: by 2002:a7b:c4c2:0:b0:3eb:38b0:e757 with SMTP id g2-20020a7bc4c2000000b003eb38b0e757mr2013971wmk.10.1680703544739; Wed, 05 Apr 2023 07:05:44 -0700 (PDT) Received: from platypus.localdomain ([62.23.166.218]) by smtp.gmail.com with ESMTPSA id ay8-20020a05600c1e0800b003edddae1068sm2330150wmb.9.2023.04.05.07.05.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 07:05:44 -0700 (PDT) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Owen Avery Subject: [committed 24/88] gccrs: Create and use CompilePatternLet visitor for compiling let statments Date: Wed, 5 Apr 2023 16:03:08 +0200 Message-Id: <20230405140411.3016563-25-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230405140411.3016563-1-arthur.cohen@embecosm.com> References: <20230405140411.3016563-1-arthur.cohen@embecosm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-14.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: arthur.cohen@embecosm.com Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1762346361504816095?= X-GMAIL-MSGID: =?utf-8?q?1762346361504816095?= From: Owen Avery gcc/rust/ChangeLog: * backend/rust-compile-pattern.cc (CompilePatternLet::visit): New function. * backend/rust-compile-stmt.cc (CompileStmt::visit): Likewise. * backend/rust-compile-pattern.h (class CompilePatternLet): New visitor. Signed-off-by: Owen Avery --- gcc/rust/backend/rust-compile-pattern.cc | 58 ++++++++++++++++ gcc/rust/backend/rust-compile-pattern.h | 87 ++++++++++++++++++++++++ gcc/rust/backend/rust-compile-stmt.cc | 31 +-------- 3 files changed, 148 insertions(+), 28 deletions(-) diff --git a/gcc/rust/backend/rust-compile-pattern.cc b/gcc/rust/backend/rust-compile-pattern.cc index 9218b815dd8..e13d6caf7e6 100644 --- a/gcc/rust/backend/rust-compile-pattern.cc +++ b/gcc/rust/backend/rust-compile-pattern.cc @@ -341,5 +341,63 @@ CompilePatternBindings::visit (HIR::GroupedPattern &pattern) pattern.get_item ()->accept_vis (*this); } +void +CompilePatternLet::visit (HIR::IdentifierPattern &pattern) +{ + Bvariable *var = nullptr; + rust_assert ( + ctx->lookup_var_decl (pattern.get_pattern_mappings ().get_hirid (), &var)); + + auto fnctx = ctx->peek_fn (); + if (ty->is_unit ()) + { + ctx->add_statement (init_expr); + + tree stmt_type = TyTyResolveCompile::compile (ctx, ty); + + auto unit_type_init_expr + = ctx->get_backend ()->constructor_expression (stmt_type, false, {}, -1, + rval_locus); + auto s = ctx->get_backend ()->init_statement (fnctx.fndecl, var, + unit_type_init_expr); + ctx->add_statement (s); + } + else + { + auto s + = ctx->get_backend ()->init_statement (fnctx.fndecl, var, init_expr); + ctx->add_statement (s); + } +} + +void +CompilePatternLet::visit (HIR::WildcardPattern &pattern) +{ + Bvariable *var = nullptr; + rust_assert ( + ctx->lookup_var_decl (pattern.get_pattern_mappings ().get_hirid (), &var)); + + auto fnctx = ctx->peek_fn (); + if (ty->is_unit ()) + { + ctx->add_statement (init_expr); + + tree stmt_type = TyTyResolveCompile::compile (ctx, ty); + + auto unit_type_init_expr + = ctx->get_backend ()->constructor_expression (stmt_type, false, {}, -1, + rval_locus); + auto s = ctx->get_backend ()->init_statement (fnctx.fndecl, var, + unit_type_init_expr); + ctx->add_statement (s); + } + else + { + auto s + = ctx->get_backend ()->init_statement (fnctx.fndecl, var, init_expr); + ctx->add_statement (s); + } +} + } // namespace Compile } // namespace Rust diff --git a/gcc/rust/backend/rust-compile-pattern.h b/gcc/rust/backend/rust-compile-pattern.h index 43334affca7..317a3451161 100644 --- a/gcc/rust/backend/rust-compile-pattern.h +++ b/gcc/rust/backend/rust-compile-pattern.h @@ -91,5 +91,92 @@ protected: tree match_scrutinee_expr; }; +class CompilePatternLet : public HIRCompileBase, public HIR::HIRPatternVisitor +{ +public: + static void Compile (HIR::Pattern *pattern, tree init_expr, + TyTy::BaseType *ty, Location rval_locus, Context *ctx) + { + CompilePatternLet compiler (ctx, init_expr, ty, rval_locus); + pattern->accept_vis (compiler); + } + + void visit (HIR::IdentifierPattern &) override; + void visit (HIR::WildcardPattern &) override; + + // check for unimplemented Pattern HIR nodes. + void visit (HIR::GroupedPattern &pattern) override + { + rust_sorry_at (pattern.get_locus (), + "grouped pattern let statements not supported"); + } + + void visit (HIR::LiteralPattern &pattern) override + { + rust_sorry_at (pattern.get_locus (), + "literal pattern let statements not supported"); + } + + void visit (HIR::PathInExpression &pattern) override + { + rust_sorry_at (pattern.get_locus (), + "path-in-expression pattern let statements not supported"); + } + + void visit (HIR::QualifiedPathInExpression &pattern) override + { + rust_sorry_at ( + pattern.get_locus (), + "qualified-path-in-expression pattern let statements not supported"); + } + + void visit (HIR::RangePattern &pattern) override + { + rust_sorry_at (pattern.get_locus (), + "range pattern let statements not supported"); + } + + void visit (HIR::ReferencePattern &pattern) override + { + rust_sorry_at (pattern.get_locus (), + "reference pattern let statements not supported"); + } + + void visit (HIR::SlicePattern &pattern) override + { + rust_sorry_at (pattern.get_locus (), + "slice pattern let statements not supported"); + } + + void visit (HIR::StructPattern &pattern) override + { + rust_sorry_at (pattern.get_locus (), + "struct pattern let statements not supported"); + } + + void visit (HIR::TuplePattern &pattern) override + { + rust_sorry_at (pattern.get_locus (), + "tuple pattern let statements not supported"); + } + + void visit (HIR::TupleStructPattern &pattern) override + { + rust_sorry_at (pattern.get_locus (), + "tuple-struct pattern let statements not supported"); + } + +protected: + CompilePatternLet (Context *ctx, tree init_expr, TyTy::BaseType *ty, + Location rval_locus) + : HIRCompileBase (ctx), init_expr (init_expr), ty (ty), + rval_locus (rval_locus) + {} + + tree init_expr; + TyTy::BaseType *ty; + Location rval_locus; +}; + } // namespace Compile } // namespace Rust diff --git a/gcc/rust/backend/rust-compile-stmt.cc b/gcc/rust/backend/rust-compile-stmt.cc index 3fc2528ff6b..6bb4ead9451 100644 --- a/gcc/rust/backend/rust-compile-stmt.cc +++ b/gcc/rust/backend/rust-compile-stmt.cc @@ -16,6 +16,7 @@ // along with GCC; see the file COPYING3. If not see // . +#include "rust-compile-pattern.h" #include "rust-compile-stmt.h" #include "rust-compile-expr.h" @@ -53,7 +54,7 @@ CompileStmt::visit (HIR::LetStmt &stmt) if (!stmt.has_init_expr ()) return; - const HIR::Pattern &stmt_pattern = *stmt.get_pattern (); + HIR::Pattern &stmt_pattern = *stmt.get_pattern (); HirId stmt_id = stmt_pattern.get_pattern_mappings ().get_hirid (); TyTy::BaseType *ty = nullptr; @@ -65,15 +66,6 @@ CompileStmt::visit (HIR::LetStmt &stmt) return; } - Bvariable *var = nullptr; - if (!ctx->lookup_var_decl (stmt_id, &var)) - { - // FIXME this should be an assertion instead and use error mark node - rust_fatal_error (stmt.get_locus (), - "failed to lookup compiled variable declaration"); - return; - } - tree init = CompileExpr::Compile (stmt.get_init_expr (), ctx); // FIXME use error_mark_node, check that CompileExpr returns error_mark_node // on failure and make this an assertion @@ -84,7 +76,6 @@ CompileStmt::visit (HIR::LetStmt &stmt) bool ok = ctx->get_tyctx ()->lookup_type ( stmt.get_init_expr ()->get_mappings ().get_hirid (), &actual); rust_assert (ok); - tree stmt_type = TyTyResolveCompile::compile (ctx, ty); Location lvalue_locus = stmt.get_pattern ()->get_locus (); Location rvalue_locus = stmt.get_init_expr ()->get_locus (); @@ -92,23 +83,7 @@ CompileStmt::visit (HIR::LetStmt &stmt) init = coercion_site (stmt.get_mappings ().get_hirid (), init, actual, expected, lvalue_locus, rvalue_locus); - auto fnctx = ctx->peek_fn (); - if (ty->is_unit ()) - { - ctx->add_statement (init); - - auto unit_type_init_expr - = ctx->get_backend ()->constructor_expression (stmt_type, false, {}, -1, - rvalue_locus); - auto s = ctx->get_backend ()->init_statement (fnctx.fndecl, var, - unit_type_init_expr); - ctx->add_statement (s); - } - else - { - auto s = ctx->get_backend ()->init_statement (fnctx.fndecl, var, init); - ctx->add_statement (s); - } + CompilePatternLet::Compile (&stmt_pattern, init, ty, rvalue_locus, ctx); } } // namespace Compile