From patchwork Tue Jan 30 12:06:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 194045 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2087:b0:106:209c:c626 with SMTP id gs7csp1174574dyb; Tue, 30 Jan 2024 04:13:13 -0800 (PST) X-Google-Smtp-Source: AGHT+IEPJgGIZUMuKA0WxiTQBs89oHfUoPwwAyHjpTx9QSJPM+MJqR3okLpzJU+ixL8Jjgyxdg4T X-Received: by 2002:a0c:e710:0:b0:68c:3b70:5884 with SMTP id d16-20020a0ce710000000b0068c3b705884mr6343135qvn.98.1706616792852; Tue, 30 Jan 2024 04:13:12 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706616792; cv=pass; d=google.com; s=arc-20160816; b=W26NYfrXWsHmZ1xddTYw543LKDcWzm4tHP/+6FGGA9Fz4kJeI8VBxMg+F+Bqy6FcNe G77BIeaPXgG8P866pxw97svVoRBl3ywFBwbchRWuvCZab7410jenYQ2qooQxkppLAlgv h3prib8JJu6PmB2Nd69InMwM+zu+jiQ1kvuGgLhidHNRSW1L3o2eqo05zzZDItsDF0ev z7Fuk3S8Re8zDoGIp3EnCLoBYmBgy5pFjkDqmS9ulGzhgV7zDg3vJUPQdOWqhO4lEw9o hLb0ymdrBg279JpO8+9yl04bZPdTW+ewZjanCaeNfAhCOPfPvPJ0NdUt0AyNCxPIocQJ uAnA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=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:arc-filter:dmarc-filter:delivered-to; bh=2FiiYG5wITwXf4jrhe2S+b2TsGW5AjR58EQEudQaO4A=; fh=s1GzlUPo1djGkCl3rkRJI30hDOSCVwGp0RAbVwQ8dxQ=; b=0ah0bf+mlvgcM2XJJEev86tRLHKU+TZVYkpOChxmqMx0RA0tdpV6ogFzuuNV4GGLIG WPXivJdCPVq2v2T3eU+cdXoTTAdxrdBpNThrS2gWIgyJXGXEkQ0oKdu9uzfTAcq2lMXF 0RB1KY2vVn/dh62UBQmRif5AS+Q4bCFJOmvtISVast421x6GOVo/swDpFbzqxuMyfFvB /CQZk2AlGw6W4AGIG8IsZsuqXpVC/gbmPtfxrgWUUzc9XtcjwECjnCUpUuxJ3cugW4t6 rE+oQo/YyUcn1Fd7sg/VE0zmWTXzrgt/cpy32txD60ttAOwDhSN5N66yCI+yKRSlXiZO MjDg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=PzzcCqnH; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id m13-20020a0562141bcd00b0068c5582a919si3151893qvc.32.2024.01.30.04.13.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 04:13:12 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=PzzcCqnH; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 64A073857C51 for ; Tue, 30 Jan 2024 12:13:12 +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 765FF3858407 for ; Tue, 30 Jan 2024 12:10:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 765FF3858407 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 765FF3858407 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::334 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706616656; cv=none; b=TtlgryhQowQ5q3NXbXqHkC3H5PEVGqUFkvHh9s5JtIJh6drpOZPY5LqtTRO1h6CcISoan39WPo4H+M07HkGMTlTEjeD6zDWCvLVV/NIVX1RgiDxHFzdu6QbK/ar6Txm3U6m8eL728td53ZMs/41l8jsAf3TmD5v12Un5E2tWxqI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706616656; c=relaxed/simple; bh=wlhisV3f9BFpORfCt34y1LIMxcELpLgxGD64iBMjUyU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=v+GTMiEbBlsgrJkpz7aMpqFWp4u7wSEigO0AMdPAx9wduFGFxp54YVIKfHm1Avm4MV6XjAGAMzXllM51FQshixcXkFYmY20hitAkfWR0QDPR9/ACt986ZThi63s8U6zVmStPCzMd+JMjeEbhUMeGNLsCoJGxlH+K/UQNphhNdNQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-40efcb37373so8956875e9.2 for ; Tue, 30 Jan 2024 04:10:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1706616649; x=1707221449; darn=gcc.gnu.org; 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=2FiiYG5wITwXf4jrhe2S+b2TsGW5AjR58EQEudQaO4A=; b=PzzcCqnHQtwk+aK8ZWO5VzU03yDwp42wY+1cw9r23b0FQXlUzsatlFPnR8X7zBMg8+ 9HwDmrJ+DXbeXjUVu5Q9Fu9JM3GQLWw9Pl3BTqvZ9bdRudDyi5Y4ssCT0fEf7f5gNufE gRBwEdGnZPemavq2e3+mzKispLiuRqlj++wjisZ5jOQvBW2v1zPnjSceqORo5UlLPRnk LJ0arzV1OvOdVVDNr4ObcwV1yuwzC6wbJpyDd10RJGGbvHFQO3ylR5RsZXmceKRRbJis bhuJU0SYg9nYws+R+ZnAgXmev+HrYDtoX7smwcWBadgfggB+y8SziITA2dCU5IV0e4xh XppA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706616649; x=1707221449; 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=2FiiYG5wITwXf4jrhe2S+b2TsGW5AjR58EQEudQaO4A=; b=RQz6sojFhTyfA9v+1nau+2+wy0p8iCWJgNYDvuIPr2MpW9lmyJ8LvnqvUwB8kCjq2w qfjN1XXNF/SwD70ND6d1uIkON3Qgz19U/XiT1qC0xKl8Dni6ls2ueNRZcCZ5zbcD5ieR Jh0JCptywdaQmDlM3BvPBC7eA0+gqHeOk5uYcny3wbXZlLiXyseuW1lL5wH4hSo6eWDQ PuA9VxSNlzU2rzJAp6rYIaKI7F1OiNHg5Z1YHpmHRZbtikoTrA3PNogqjbjNqOi58ve9 64VIYCR3e+1anParJN/7fyh31dUyRTFDCYn8SmDTkhHvuY4ni5LMT2ruv4TjjuWnQLQZ H2vQ== X-Gm-Message-State: AOJu0YyWP4m0pwgVs0mwmxLEaEAWmBhxuGvSL5zf0Ozlu4dygfOY3rAR LjbG3LlVTWzdfGVZxTpFCGpnpeaV34GT91O6z94uUGKLkHbECNMF538bi2A9odNut9qn2hnqYdk jUA== X-Received: by 2002:adf:e311:0:b0:337:c4c1:a3af with SMTP id b17-20020adfe311000000b00337c4c1a3afmr6110068wrj.35.1706616649358; Tue, 30 Jan 2024 04:10:49 -0800 (PST) Received: from platypus.localdomain ([62.23.166.218]) by smtp.gmail.com with ESMTPSA id f9-20020a056000036900b00339307d9d31sm10569894wrf.112.2024.01.30.04.10.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 04:10:49 -0800 (PST) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Pierre-Emmanuel Patry Subject: [COMMITTED 001/101] gccrs: Add visibility to trait item Date: Tue, 30 Jan 2024 13:06:17 +0100 Message-ID: <20240130121026.807464-4-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20240130121026.807464-2-arthur.cohen@embecosm.com> References: <20240130121026.807464-2-arthur.cohen@embecosm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-14.1 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, T_SCC_BODY_TEXT_LINE 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.30 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789517410228756387 X-GMAIL-MSGID: 1789517410228756387 From: Pierre-Emmanuel Patry The compiler shall parse visibility modifiers on trait items and reject those at a later stage (ast validation). gcc/rust/ChangeLog: * ast/rust-item.h (struct Visibility): Move Visibility from here... * ast/rust-ast.h (struct Visibility): ...to here. * parse/rust-parse-impl.h (Parser::parse_trait_item): Parse visibility before giving it back to the item parsing function. (Parser::parse_trait_type): Add visibility modifier. * parse/rust-parse.h (RUST_PARSE_H): Change function prototype. Signed-off-by: Pierre-Emmanuel Patry --- gcc/rust/ast/rust-ast.h | 248 ++++++++++++++++--------------- gcc/rust/ast/rust-item.h | 5 +- gcc/rust/parse/rust-parse-impl.h | 10 +- gcc/rust/parse/rust-parse.h | 3 +- 4 files changed, 140 insertions(+), 126 deletions(-) diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index 67ae92fb42d..4dc7f9710f3 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -490,6 +490,126 @@ operator!= (const SimplePath &lhs, const std::string &rhs) // forward decl for Attribute class AttrInput; +// Visibility of item - if the item has it, then it is some form of public +struct Visibility +{ +public: + enum VisType + { + PRIV, + PUB, + PUB_CRATE, + PUB_SELF, + PUB_SUPER, + PUB_IN_PATH + }; + +private: + VisType vis_type; + // Only assigned if vis_type is IN_PATH + SimplePath in_path; + location_t locus; + + // should this store location info? + +public: + // Creates a Visibility - TODO make constructor protected or private? + Visibility (VisType vis_type, SimplePath in_path, location_t locus) + : vis_type (vis_type), in_path (std::move (in_path)), locus (locus) + {} + + VisType get_vis_type () const { return vis_type; } + + // Returns whether visibility is in an error state. + bool is_error () const + { + return vis_type == PUB_IN_PATH && in_path.is_empty (); + } + + // Returns whether a visibility has a path + bool has_path () const { return !is_error () && vis_type >= PUB_CRATE; } + + // Returns whether visibility is public or not. + bool is_public () const { return vis_type != PRIV && !is_error (); } + + location_t get_locus () const { return locus; } + + // empty? + // Creates an error visibility. + static Visibility create_error () + { + return Visibility (PUB_IN_PATH, SimplePath::create_empty (), + UNDEF_LOCATION); + } + + // Unique pointer custom clone function + /*std::unique_ptr clone_visibility() const { + return std::unique_ptr(clone_visibility_impl()); + }*/ + + /* TODO: think of a way to only allow valid Visibility states - polymorphism + * is one idea but may be too resource-intensive. */ + + // Creates a public visibility with no further features/arguments. + // empty? + static Visibility create_public (location_t pub_vis_location) + { + return Visibility (PUB, SimplePath::create_empty (), pub_vis_location); + } + + // Creates a public visibility with crate-relative paths + static Visibility create_crate (location_t crate_tok_location, + location_t crate_vis_location) + { + return Visibility (PUB_CRATE, + SimplePath::from_str ("crate", crate_tok_location), + crate_vis_location); + } + + // Creates a public visibility with self-relative paths + static Visibility create_self (location_t self_tok_location, + location_t self_vis_location) + { + return Visibility (PUB_SELF, + SimplePath::from_str ("self", self_tok_location), + self_vis_location); + } + + // Creates a public visibility with parent module-relative paths + static Visibility create_super (location_t super_tok_location, + location_t super_vis_location) + { + return Visibility (PUB_SUPER, + SimplePath::from_str ("super", super_tok_location), + super_vis_location); + } + + // Creates a private visibility + static Visibility create_private () + { + return Visibility (PRIV, SimplePath::create_empty (), UNDEF_LOCATION); + } + + // Creates a public visibility with a given path or whatever. + static Visibility create_in_path (SimplePath in_path, + location_t in_path_vis_location) + { + return Visibility (PUB_IN_PATH, std::move (in_path), in_path_vis_location); + } + + std::string as_string () const; + const SimplePath &get_path () const { return in_path; } + SimplePath &get_path () { return in_path; } + +protected: + // Clone function implementation - not currently virtual but may be if + // polymorphism used + /*virtual*/ Visibility *clone_visibility_impl () const + { + return new Visibility (*this); + } +}; + // aka Attr // Attribute AST representation struct Attribute @@ -1042,125 +1162,6 @@ protected: Item *clone_stmt_impl () const final override { return clone_item_impl (); } }; -// Visibility of item - if the item has it, then it is some form of public -struct Visibility -{ -public: - enum VisType - { - PRIV, - PUB, - PUB_CRATE, - PUB_SELF, - PUB_SUPER, - PUB_IN_PATH - }; - -private: - VisType vis_type; - // Only assigned if vis_type is IN_PATH - SimplePath in_path; - location_t locus; - - // should this store location info? - -public: - // Creates a Visibility - TODO make constructor protected or private? - Visibility (VisType vis_type, SimplePath in_path, location_t locus) - : vis_type (vis_type), in_path (std::move (in_path)), locus (locus) - {} - - VisType get_vis_type () const { return vis_type; } - - // Returns whether visibility is in an error state. - bool is_error () const - { - return vis_type == PUB_IN_PATH && in_path.is_empty (); - } - - // Returns whether a visibility has a path - bool has_path () const { return !is_error () && vis_type >= PUB_CRATE; } - - // Returns whether visibility is public or not. - bool is_public () const { return vis_type != PRIV && !is_error (); } - - location_t get_locus () const { return locus; } - - // empty? - // Creates an error visibility. - static Visibility create_error () - { - return Visibility (PUB_IN_PATH, SimplePath::create_empty (), - UNDEF_LOCATION); - } - - // Unique pointer custom clone function - /*std::unique_ptr clone_visibility() const { - return std::unique_ptr(clone_visibility_impl()); - }*/ - - /* TODO: think of a way to only allow valid Visibility states - polymorphism - * is one idea but may be too resource-intensive. */ - - // Creates a public visibility with no further features/arguments. - // empty? - static Visibility create_public (location_t pub_vis_location) - { - return Visibility (PUB, SimplePath::create_empty (), pub_vis_location); - } - - // Creates a public visibility with crate-relative paths - static Visibility create_crate (location_t crate_tok_location, - location_t crate_vis_location) - { - return Visibility (PUB_CRATE, - SimplePath::from_str ("crate", crate_tok_location), - crate_vis_location); - } - - // Creates a public visibility with self-relative paths - static Visibility create_self (location_t self_tok_location, - location_t self_vis_location) - { - return Visibility (PUB_SELF, - SimplePath::from_str ("self", self_tok_location), - self_vis_location); - } - - // Creates a public visibility with parent module-relative paths - static Visibility create_super (location_t super_tok_location, - location_t super_vis_location) - { - return Visibility (PUB_SUPER, - SimplePath::from_str ("super", super_tok_location), - super_vis_location); - } - - // Creates a private visibility - static Visibility create_private () - { - return Visibility (PRIV, SimplePath::create_empty (), UNDEF_LOCATION); - } - - // Creates a public visibility with a given path or whatever. - static Visibility create_in_path (SimplePath in_path, - location_t in_path_vis_location) - { - return Visibility (PUB_IN_PATH, std::move (in_path), in_path_vis_location); - } - - std::string as_string () const; - const SimplePath &get_path () const { return in_path; } - SimplePath &get_path () { return in_path; } - -protected: - // Clone function implementation - not currently virtual but may be if - // polymorphism used - /*virtual*/ Visibility *clone_visibility_impl () const - { - return new Visibility (*this); - } -}; // Item that supports visibility - abstract base class class VisItem : public Item { @@ -1649,13 +1650,20 @@ class TraitItem : virtual public AssociatedItem { protected: TraitItem (location_t locus) - : node_id (Analysis::Mappings::get ()->get_next_node_id ()), locus (locus) + : node_id (Analysis::Mappings::get ()->get_next_node_id ()), + vis (Visibility::create_private ()), locus (locus) + {} + + TraitItem (Visibility vis, location_t locus) + : node_id (Analysis::Mappings::get ()->get_next_node_id ()), vis (vis), + locus (locus) {} // Clone function implementation as pure virtual method virtual TraitItem *clone_associated_item_impl () const override = 0; NodeId node_id; + Visibility vis; location_t locus; public: diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h index 8b53bc63a83..2c0e45962ce 100644 --- a/gcc/rust/ast/rust-item.h +++ b/gcc/rust/ast/rust-item.h @@ -3048,8 +3048,9 @@ public: TraitItemType (Identifier name, std::vector> type_param_bounds, - std::vector outer_attrs, location_t locus) - : TraitItem (locus), outer_attrs (std::move (outer_attrs)), + std::vector outer_attrs, Visibility vis, + location_t locus) + : TraitItem (vis, locus), outer_attrs (std::move (outer_attrs)), name (std::move (name)), type_param_bounds (std::move (type_param_bounds)) {} diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index fd648c53e14..0ae89dc3907 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -22,6 +22,7 @@ /* DO NOT INCLUDE ANYWHERE - this is automatically included with rust-parse.h * This is also the reason why there are no include guards. */ +#include "rust-item.h" #include "rust-token.h" #define INCLUDE_ALGORITHM #include "rust-diagnostics.h" @@ -5012,12 +5013,14 @@ Parser::parse_trait_item () // parse outer attributes (if they exist) AST::AttrVec outer_attrs = parse_outer_attributes (); + AST::Visibility vis = parse_visibility (); + // lookahead to determine what type of trait item to parse const_TokenPtr tok = lexer.peek_token (); switch (tok->get_id ()) { case TYPE: - return parse_trait_type (std::move (outer_attrs)); + return parse_trait_type (std::move (outer_attrs), vis); case CONST: // disambiguate with function qualifier if (lexer.peek_token (1)->get_id () == IDENTIFIER) @@ -5176,7 +5179,8 @@ Parser::parse_trait_item () // Parse a typedef trait item. template std::unique_ptr -Parser::parse_trait_type (AST::AttrVec outer_attrs) +Parser::parse_trait_type (AST::AttrVec outer_attrs, + AST::Visibility vis) { location_t locus = lexer.peek_token ()->get_locus (); skip_token (TYPE); @@ -5208,7 +5212,7 @@ Parser::parse_trait_type (AST::AttrVec outer_attrs) return std::unique_ptr ( new AST::TraitItemType (std::move (ident), std::move (bounds), - std::move (outer_attrs), locus)); + std::move (outer_attrs), vis, locus)); } // Parses a constant trait item. diff --git a/gcc/rust/parse/rust-parse.h b/gcc/rust/parse/rust-parse.h index 9e924e0015c..08e6ce000f4 100644 --- a/gcc/rust/parse/rust-parse.h +++ b/gcc/rust/parse/rust-parse.h @@ -17,6 +17,7 @@ along with GCC; see the file COPYING3. If not see #ifndef RUST_PARSE_H #define RUST_PARSE_H +#include "rust-item.h" #include "rust-lex.h" #include "rust-ast-full.h" #include "rust-diagnostics.h" @@ -331,7 +332,7 @@ private: std::unique_ptr parse_trait (AST::Visibility vis, AST::AttrVec outer_attrs); std::unique_ptr - parse_trait_type (AST::AttrVec outer_attrs); + parse_trait_type (AST::AttrVec outer_attrs, AST::Visibility); std::unique_ptr parse_trait_const (AST::AttrVec outer_attrs); std::unique_ptr parse_self_param ();