From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 7415B3856DDF for ; Wed, 5 Apr 2023 14:05:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7415B3856DDF 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 o32so20949215wms.1 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=Y0uy8lfbIKrIMwZVGdrD4bV8+yoU+2oSOh9FjTm+ESR7Gv2p+ccW8SFMDfgK4RKGVH LVITcySabakalihQo7/ZXtEZSOLNiMZjaOTgK18Jw+4fYRmUGJyUevc1VwQH8TguyqJl ENrGGbrGlJQCQXi/nViiB7WEZeOHUsGQOb1PlCNc39UjgRGJhRMd5Ba/ChaHVCLVHoRv MbOCNsMh0vylcQmf8LrnJXi4z4wiBZDFeEIAaMI+xNvID91T9LDFVRmYZG2QxQwV/Rv3 pNcdV1S2LZQZIyxm7Vz0t/TfEJ+iGxOVn70e0Rz0Hs9SdidPAP21f/0iuOXPF4D+3H1t /+ow== X-Gm-Message-State: AAQBX9d85leHBAr2+WNL5sYsv331TwAhFdqSTrudEnsksuVT3VNQbxes A/pqYr4MapdcLdBzxoduCRyRFX8YAs3PCRhF9w== X-Google-Smtp-Source: AKy350aF0OE1aPiAfEuC1DHHKPRbzowLrwfzmpvCTkUXn2ismera2am5y39vgaEaEV+C98OJ7w5tyA== 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> Reply-To: arthur.cohen@embecosm.com MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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; +} -- 2.40.0