From patchwork Wed Apr 5 14:03:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 79712 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp360680vqo; Wed, 5 Apr 2023 07:44:04 -0700 (PDT) X-Google-Smtp-Source: AKy350ZPYCHjv2KdAclKyUCherxF/jelWCmTA5fcBtJFbR5CuNgTDhVoR/KwM0pg4sCMpcplsfGE X-Received: by 2002:a17:906:f988:b0:931:6b2b:a671 with SMTP id li8-20020a170906f98800b009316b2ba671mr3002750ejb.65.1680705844248; Wed, 05 Apr 2023 07:44:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680705844; cv=none; d=google.com; s=arc-20160816; b=chrjmg1nrxxrilI8592XHYAp6QfuUzGrjdo6Y9m+BD4kxs1a+P5biEYWcCX7QDPIFD UebEsG7ILGQByJ/JgHlxsL4olds6MgdRJ6QN5cdATYrYlWotwXbWdKzWRTEd/ASuXz3Z 4XpGsmTJ7Fm3EEtyoXZGyfGrW4JxnmN6OZ++f+K5NlLHOzCOXw6um/p5S2FCTi23fWg3 2uCCAep8fsxUautadBAISp/PTPps+zG5gXhji8iE47C+9Cdu6n7/btr5IZgD7VqzfyKm ffSyYD2Xj4YwBZrwDkFjfNwQ24qMROHU0P+7+ncThBrfL2EYHhEn6HAsXrU+fPLu4CJy juUw== 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=9UBQ+p1jT9qA5jvv44LEn9QBEyXI/JebO71l2/u1D2U=; b=zpcKN9/oymvblh/FCr/yjsz226JY4u1Aa9O5tBUFW0GNCBmt4ntK3aIeqYDmEcE79V t+UyrF6yJnfgOeGt4+cuHvcjEJnS6xL6IsPLiiHPEPEyBsJ7vbMP9++MM2HY4SJPAmF6 ps7NLwtwipE6bYJ2gPMd+v1OJJT3n3RV0CWvaAMzWXKsGtiGsOQOhiQCnoRpmGulzbRo z5mebFP60WVkkUqReqCwZP1CX/Nx01INbnFIectkwYodk96NKHo9WI22rELvjc2HA34c 2oBI4tgMJDRDsMIvzRsKb7M7JRsUXZC0Yeb+U73q8Q4tA7Y5HReDoHO8d7jhtY6wISCT Wbqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=Hoo+zdcC; 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id bn17-20020a170906c0d100b008b179adfaf5si7883048ejb.466.2023.04.05.07.44.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 07:44:04 -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=Hoo+zdcC; 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 19FC8384F39F for ; Wed, 5 Apr 2023 14:15:44 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by sourceware.org (Postfix) with ESMTPS id 4367C385B53B for ; Wed, 5 Apr 2023 14:06:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4367C385B53B 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-x331.google.com with SMTP id v6-20020a05600c470600b003f034269c96so11822012wmo.4 for ; Wed, 05 Apr 2023 07:06:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1680703568; 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=9UBQ+p1jT9qA5jvv44LEn9QBEyXI/JebO71l2/u1D2U=; b=Hoo+zdcCDnarqncmlDQYN5uaB///zIAMxmplhhem3+OntQdUudkVQkmvBhRJBVlVSL F8ebn7sG1kHoHcYZ42Rt37hfNAGfbMaCbpBeqAsTwNvBfCtoVSRyP0GWmQMB8YxsHFjm IJr7aaEUtPkp8QQnM8DJ4MRihfCXxoFbNIVZa20JyYfgdrh/ildRnXMYDNNXRyghT2vL YEifFBncfLfDAm1Pr+qLO+gYmkDA45wJ87qsU4LbITgLIVo+450lK4ledMR7HXGf8bgH WsSaItDok2nuAG3PYmcLBnY4ylEY+gMpJUzFcIC7DRp2aarhiqxeJHZOfRkjfCl690mk NkPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680703568; 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=9UBQ+p1jT9qA5jvv44LEn9QBEyXI/JebO71l2/u1D2U=; b=hZwi6W4tLwpfSV8XzmNBHjfIW9Tqwl84bmE8oHeKJ6gZ7fgPDvz/Un6Ieq8RPSAUe5 O8r5ZZaZKqxCEtuhziY8E306q/qNCi2WnLrCgjBg5SxFUMhL3NQBds602jMjnnTq4zsf bUhcCYAUBnKbsXYBg4C6oBtP0k93EbJt2nOKIIgcu2p6SccPQFcWyigV53Ex+EmQrOvC XVAbAXX4zR/oLe42PKmDObdLE8xJdzpg5151Zk/e5Lik34qTSKkNjHvpJVmpZV2T2MJR dswOLjWPjc06Aly8SdrtA1utCdPKKB/EoqI8LtowEbFWVygD3WLClkFHwrhkOHTXRkDd VWZQ== X-Gm-Message-State: AAQBX9e+mjar2yB6oLbx/XYC78qZ6xtjTuUjgTPJ5Mh7Lb9XMLTpjC2J Lm1cocIIB2takwJQ2W6D+7LiJgaRGh9g3ADowg== X-Received: by 2002:a05:600c:2189:b0:3ed:a07b:c59d with SMTP id e9-20020a05600c218900b003eda07bc59dmr4969543wme.21.1680703567668; Wed, 05 Apr 2023 07:06:07 -0700 (PDT) Received: from platypus.localdomain ([62.23.166.218]) by smtp.gmail.com with ESMTPSA id ay8-20020a05600c1e0800b003edddae1068sm2330150wmb.9.2023.04.05.07.06.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 07:06:07 -0700 (PDT) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Owen Avery Subject: [committed 56/88] gccrs: Add support for TuplePattern in let statements Date: Wed, 5 Apr 2023 16:03:40 +0200 Message-Id: <20230405140411.3016563-57-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, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=unavailable 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?1762347811473764374?= X-GMAIL-MSGID: =?utf-8?q?1762347811473764374?= From: Owen Avery gcc/rust/ChangeLog: * hir/tree/rust-hir-pattern.h (TuplePatternItemsRanged::get_lower_patterns): Add method. (TuplePatternItemsRanged::get_upper_patterns): Add method. * backend/rust-compile-pattern.cc (CompilePatternLet::visit): Implement TuplePattern visitor. * backend/rust-compile-pattern.h (CompilePatternLet::visit): Move TuplePattern visitor out of header file. gcc/testsuite/ChangeLog: * rust/execute/torture/let-pattern-1.rs: New test. Signed-off-by: Owen Avery --- gcc/rust/backend/rust-compile-pattern.cc | 87 +++++++++++++++++++ gcc/rust/backend/rust-compile-pattern.h | 7 +- gcc/rust/hir/tree/rust-hir-pattern.h | 18 ++++ .../rust/execute/torture/let-pattern-1.rs | 4 + 4 files changed, 110 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/rust/execute/torture/let-pattern-1.rs diff --git a/gcc/rust/backend/rust-compile-pattern.cc b/gcc/rust/backend/rust-compile-pattern.cc index bad98be44d0..cb4b082f9c8 100644 --- a/gcc/rust/backend/rust-compile-pattern.cc +++ b/gcc/rust/backend/rust-compile-pattern.cc @@ -387,5 +387,92 @@ CompilePatternLet::visit (HIR::WildcardPattern &pattern) } } +void +CompilePatternLet::visit (HIR::TuplePattern &pattern) +{ + rust_assert (pattern.has_tuple_pattern_items ()); + + tree tuple_type = TyTyResolveCompile::compile (ctx, ty); + tree init_stmt; + Bvariable *tmp_var + = ctx->get_backend ()->temporary_variable (ctx->peek_fn ().fndecl, + NULL_TREE, tuple_type, init_expr, + false, pattern.get_locus (), + &init_stmt); + tree access_expr + = ctx->get_backend ()->var_expression (tmp_var, pattern.get_locus ()); + ctx->add_statement (init_stmt); + + switch (pattern.get_items ()->get_pattern_type ()) + { + case HIR::TuplePatternItems::TuplePatternItemType::RANGED: { + size_t tuple_idx = 0; + auto &items + = static_cast (*pattern.get_items ()); + + auto &items_lower = items.get_lower_patterns (); + auto &items_upper = items.get_upper_patterns (); + + for (auto &sub : items_lower) + { + TyTy::BaseType *ty_sub = nullptr; + HirId pattern_id = pattern.get_pattern_mappings ().get_hirid (); + bool ok = ctx->get_tyctx ()->lookup_type (pattern_id, &ty_sub); + rust_assert (ok); + + tree sub_init = ctx->get_backend ()->struct_field_expression ( + access_expr, tuple_idx, sub->get_locus ()); + CompilePatternLet::Compile (sub.get (), sub_init, ty_sub, + rval_locus, ctx); + tuple_idx++; + } + + rust_assert (ty->get_kind () == TyTy::TypeKind::TUPLE); + tuple_idx = static_cast (*ty).num_fields () + - items_upper.size (); + + for (auto &sub : items_upper) + { + TyTy::BaseType *ty_sub = nullptr; + HirId pattern_id = pattern.get_pattern_mappings ().get_hirid (); + bool ok = ctx->get_tyctx ()->lookup_type (pattern_id, &ty_sub); + rust_assert (ok); + + tree sub_init = ctx->get_backend ()->struct_field_expression ( + access_expr, tuple_idx, sub->get_locus ()); + CompilePatternLet::Compile (sub.get (), sub_init, ty_sub, + rval_locus, ctx); + tuple_idx++; + } + + return; + } + case HIR::TuplePatternItems::TuplePatternItemType::MULTIPLE: { + size_t tuple_idx = 0; + auto &items = static_cast ( + *pattern.get_items ()); + + for (auto &sub : items.get_patterns ()) + { + TyTy::BaseType *ty_sub = nullptr; + HirId pattern_id = pattern.get_pattern_mappings ().get_hirid (); + bool ok = ctx->get_tyctx ()->lookup_type (pattern_id, &ty_sub); + rust_assert (ok); + + tree sub_init = ctx->get_backend ()->struct_field_expression ( + access_expr, tuple_idx, sub->get_locus ()); + CompilePatternLet::Compile (sub.get (), sub_init, ty_sub, + rval_locus, ctx); + tuple_idx++; + } + + return; + } + default: { + gcc_unreachable (); + } + } +} + } // namespace Compile } // namespace Rust diff --git a/gcc/rust/backend/rust-compile-pattern.h b/gcc/rust/backend/rust-compile-pattern.h index ddf5b14265e..8f44b7b5dbb 100644 --- a/gcc/rust/backend/rust-compile-pattern.h +++ b/gcc/rust/backend/rust-compile-pattern.h @@ -101,6 +101,7 @@ public: void visit (HIR::IdentifierPattern &) override; void visit (HIR::WildcardPattern &) override; + void visit (HIR::TuplePattern &) override; // check for unimplemented Pattern HIR nodes. void visit (HIR::LiteralPattern &pattern) override @@ -146,12 +147,6 @@ public: "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 (), diff --git a/gcc/rust/hir/tree/rust-hir-pattern.h b/gcc/rust/hir/tree/rust-hir-pattern.h index 83b041d2770..1b08ab88a34 100644 --- a/gcc/rust/hir/tree/rust-hir-pattern.h +++ b/gcc/rust/hir/tree/rust-hir-pattern.h @@ -1142,6 +1142,24 @@ public: return TuplePatternItemType::RANGED; } + std::vector > &get_lower_patterns () + { + return lower_patterns; + } + const std::vector > &get_lower_patterns () const + { + return lower_patterns; + } + + std::vector > &get_upper_patterns () + { + return upper_patterns; + } + const std::vector > &get_upper_patterns () const + { + return upper_patterns; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ diff --git a/gcc/testsuite/rust/execute/torture/let-pattern-1.rs b/gcc/testsuite/rust/execute/torture/let-pattern-1.rs new file mode 100644 index 00000000000..107d819a69f --- /dev/null +++ b/gcc/testsuite/rust/execute/torture/let-pattern-1.rs @@ -0,0 +1,4 @@ +fn main() -> i32 { + let (x, y, z) = (2, 3, 6); + x * y - z +}