From patchwork Wed Apr 5 14:03:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 79662 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp345860vqo; Wed, 5 Apr 2023 07:22:15 -0700 (PDT) X-Google-Smtp-Source: AKy350a2vTBj/DFg6K2FqcIsZrbS1FdVJKc0voUx8e6V+4gcaZaFzX8tvFR6oZZ+SnrM6yDwFGkJ X-Received: by 2002:a05:6402:442:b0:4fa:d75c:16cd with SMTP id p2-20020a056402044200b004fad75c16cdmr2108746edw.34.1680704535566; Wed, 05 Apr 2023 07:22:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680704535; cv=none; d=google.com; s=arc-20160816; b=kWNodstLW84iU7gDcPyCl4yILFFbFlYHnb2N++E/aZ7c28QPWXJOkdsNa4Z1+aP1Tp tKxckIl2N/SOFldr6wa8SZCawUlxSSCDQ+hchUQMPnnvRIr55/BXZJIX1CWs5kNOH4IG +5E2UtGAps7BOnQRKQEQ9wqPyidQyjcEvctZFmHqMjAWVu0UVSaezeEIIZ9j+iNtKfCN z4iJ3pjKnUH7p8qQk5+oJAwwTq0n2YjZtiteVKFZ9t3UzkB9EuLC9mAqp7NO+hU2fVHW GB7VRtJiyjhbmpDyqiGwEpV0UEpqOzzwoEvVbWKeY45LjGvS5J6iUNtCLrZQrutfpdSt YgvQ== 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=QakCs6H3M9FTFnLP6py8l673boYHcq3ipw/eJQMIuOQ=; b=f4wpaOPR1ln3TEcI84lKQUQ+BviguVcoi3rcxC/FTcugdZ0f8aOuZSGir9KeyYQFTR sTBsVKKtmBLTsbgR9OZD3atxWpuYsKizdi7TPf0UZC7uyE2SvZ/ND2yCQCZH8juWUpAi h9auyp6IjcN0HQlLTr7GDDrKJukaCMoNlhneiLmxluPqgqO+gxmeUDjW20TThwtwC4i+ bpQReE8/eI2vd4vvZvTHcFOjvJUzFBdgX8cuDwonw0+gQ9XgDcHcifd9DsGZELCUiaB3 lNUM7kHhK+sl8IMo0Rn7AWOFJ28kfKBC3cgz02VaFN3Oi2T7v7ywG63KKOGLy1p6GQS/ gPbA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=hE6xnLGz; 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 f8-20020a056402004800b004ab162a8415si1211519edu.283.2023.04.05.07.22.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Apr 2023 07:22:15 -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=hE6xnLGz; 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 D92FB385B53D for ; Wed, 5 Apr 2023 14:10:30 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by sourceware.org (Postfix) with ESMTPS id B1C02385735A for ; Wed, 5 Apr 2023 14:05:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B1C02385735A 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-x334.google.com with SMTP id n10-20020a05600c4f8a00b003ee93d2c914so23482056wmq.2 for ; Wed, 05 Apr 2023 07:05:46 -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=QakCs6H3M9FTFnLP6py8l673boYHcq3ipw/eJQMIuOQ=; b=hE6xnLGzQl6qb6TUxuZwYni+kdQmcnfGdMDxFsdwG7d5A/SSZDvUH6LG7tvAzHF8ZJ 598sFqUMKphX0oP3Fw3ZuRWmGfkdMthNwfzlUDU8LP/r4haAv0ZEG2lqhkXlXQM8T3bx Ww2Iapk4O6qT3Xdzdcl4nt1OXv7+CzBn9nwcJ95Sieh0K16vU8zl+NsVRci4B/FxIy9n tiK+Z8OyToJEnPFZF8xzUg79thl0mXdxlBPnAcsO9aQslMEtlOE5XXyZyodjGUBRSWmm 9HntkcO6iYwT3MRHRSQQB3nQjLj1PZ3Razky06PzEnUjej8xeMhr51sNjOy3HZ908vqc 4w9A== 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=QakCs6H3M9FTFnLP6py8l673boYHcq3ipw/eJQMIuOQ=; b=hTD89U5IWGNYOtJJCkOmswn4QQeX6S90bNFvzlPW1AIcE6FDVjMEWbMOJGxGLLHtf+ o7o04xkGlUGPa2k1CS0HdUiYYmdj0GMuhqugI7HBQrri59oH+8jyX1lUXlIPgFy0FSOM i9PtCGoOi+rkBG246+YEXnta8KpPgPBrGP+VUc2DBqTtqDdNYbnpIGpWEnLjAPxbNC6x d/vCl2H5w3/2JpjrAWypKgEkFjiCfqC9tQHCnpa9FktxagI/1oAYwhGiKC7GHWHGUu8B zYy10bnJNxOe2eS9RBTVTVgZMhlaDctKQvhBPWUHkfpAMk5dMDDfk7HPpjQIeWh5qiUt fbGw== X-Gm-Message-State: AAQBX9eHFw42m3TVhDoUYM8eQXgOUhtwZYbWwoRMMUaAp2/xXS7FZzwe uJIVuZaYytJC41cM43ySNJXo9slbsRWY0L7JOQ== X-Received: by 2002:a7b:cb86:0:b0:3df:e41f:8396 with SMTP id m6-20020a7bcb86000000b003dfe41f8396mr4657384wmi.37.1680703545251; Wed, 05 Apr 2023 07:05:45 -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:45 -0700 (PDT) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Arthur Cohen Subject: [committed 25/88] gccrs: parser: Allow parsing multiple reference types Date: Wed, 5 Apr 2023 16:03:09 +0200 Message-Id: <20230405140411.3016563-26-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.3 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?1762346439305355688?= X-GMAIL-MSGID: =?utf-8?q?1762346439305355688?= From: Arthur Cohen The parser now recursively tries to parse a reference type after seeing a `&` or `&&` token. gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_type): Handle double ampersan properly (Parser::parse_reference_type): Call into `parse_reference_type_inner` and wrap double reference types in another `AST::ReferenceType` node (Parser::parse_reference_type_inner): Add parsing implementation which does not care about the leading token (& or &&) (Parser::parse_type_no_bounds): Handle double ampersand properly * parse/rust-parse.h: Declare `parse_reference_type_inner` gcc/testsuite/ChangeLog: * rust/compile/multi_reference_type.rs: New test. --- gcc/rust/parse/rust-parse-impl.h | 33 +++++++++++++++---- gcc/rust/parse/rust-parse.h | 2 ++ .../rust/compile/multi_reference_type.rs | 12 +++++++ 3 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/rust/compile/multi_reference_type.rs diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index a0bc2e10d2f..23b033fb26e 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -9237,6 +9237,7 @@ Parser::parse_type (bool save_errors) // raw pointer type return parse_raw_pointer_type (); case AMP: // does this also include AMP_AMP? + case LOGICAL_AND: // reference type return parse_reference_type (); case LIFETIME: { @@ -9886,14 +9887,10 @@ Parser::parse_bare_function_type ( std::move (return_type), best_try_locus)); } -// Parses a reference type (mutable or immutable, with given lifetime). template std::unique_ptr -Parser::parse_reference_type () +Parser::parse_reference_type_inner (Location locus) { - Location locus = lexer.peek_token ()->get_locus (); - skip_token (AMP); - // parse optional lifetime AST::Lifetime lifetime = AST::Lifetime::error (); if (lexer.peek_token ()->get_id () == LIFETIME) @@ -9932,6 +9929,29 @@ Parser::parse_reference_type () std::move (lifetime))); } +// Parses a reference type (mutable or immutable, with given lifetime). +template +std::unique_ptr +Parser::parse_reference_type () +{ + auto t = lexer.peek_token (); + auto locus = t->get_locus (); + + switch (t->get_id ()) + { + case AMP: + skip_token (AMP); + return parse_reference_type_inner (locus); + case LOGICAL_AND: + skip_token (LOGICAL_AND); + return std::unique_ptr ( + new AST::ReferenceType (false, parse_reference_type_inner (locus), + locus)); + default: + gcc_unreachable (); + } +} + // Parses a raw (unsafe) pointer type. template std::unique_ptr @@ -10079,7 +10099,8 @@ Parser::parse_type_no_bounds () case ASTERISK: // raw pointer type return parse_raw_pointer_type (); - case AMP: // does this also include AMP_AMP? + case AMP: // does this also include AMP_AMP? Yes! Which is... LOGICAL_AND? + case LOGICAL_AND: // reference type return parse_reference_type (); case LIFETIME: diff --git a/gcc/rust/parse/rust-parse.h b/gcc/rust/parse/rust-parse.h index 5c0fcc3f174..2f767bb2a53 100644 --- a/gcc/rust/parse/rust-parse.h +++ b/gcc/rust/parse/rust-parse.h @@ -604,6 +604,8 @@ private: std::unique_ptr parse_type_no_bounds (); std::unique_ptr parse_slice_or_array_type (); std::unique_ptr parse_raw_pointer_type (); + std::unique_ptr + parse_reference_type_inner (Location locus); std::unique_ptr parse_reference_type (); std::unique_ptr parse_bare_function_type (std::vector for_lifetimes); diff --git a/gcc/testsuite/rust/compile/multi_reference_type.rs b/gcc/testsuite/rust/compile/multi_reference_type.rs new file mode 100644 index 00000000000..5ad7d84adbc --- /dev/null +++ b/gcc/testsuite/rust/compile/multi_reference_type.rs @@ -0,0 +1,12 @@ +fn main() { + let a = 15u8; + let a: &u8 = &a; + let a: &&u8 = &a; + let a: &&&u8 = &a; + let _: &&&&u8 = &a; + + let _: &&u8; + let _: &mut &u8; + let _: &&mut u8; + let _: &mut &mut &u8; +}