From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by sourceware.org (Postfix) with ESMTPS id B64CE3858437 for ; Tue, 30 Jan 2024 12:10:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B64CE3858437 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 B64CE3858437 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::335 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706616653; cv=none; b=ZO6saTPEE5KSk3QJu3HH4xOSUvLjJlEdfTdXeR0HEVDDYHXt74JSSOA5ZlI2HoQz4pDfe5Z8tPmwl9ucnallIfRrvY4WWToEx/uR8hMXd2lp6f0AsgkzqzhppFdZxOJzckwQje9TK8JQVGfc+35QO278nQYtby4ZZmv8WVa1O00= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706616653; c=relaxed/simple; bh=wlhisV3f9BFpORfCt34y1LIMxcELpLgxGD64iBMjUyU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=ZwkEEhwciJB3ghZbF8f9TrYoGz3yhT9W4TXIMpXYawHpBEocQD6ioxJdczNhCSG+qX43P0k7ft7f31NnC/9N1XoQJ+hcoLRevmGXrhJi7fBb2OuA4QVkwrzsfoI+7ZB2AMJyk17yPVmmeN72CD4JRuQksDjfUiF/7QIPCFCLntQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-40fafced243so1215245e9.0 for ; Tue, 30 Jan 2024 04:10:50 -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=dEfwFmgfWiE0V6kvzKYMpUmYBaMFVXGgB1mIav+FY2EjvtsMuOIuhe4Nazrrsi+/DX oOjkchSJxvyZRTUILhB8w4/Y+uX2sMWdqRsJdF+1nDNUz56Nwy7Ybfsk/Xr8GSgj8vt1 kZa4A4Rq7s7OMwJ7EvM79xCNF/pUl8YN5YFIKgyz2DZ0xYc9m0rodb5nKWSwVPwr/rrI fVOEmHjtUtVlxUoB5FK0ufIkj5kwxj0VqqGkzHT/idpG+Urs0Z4UlXY1qncOs95DPeAp D7JN1hL9+myAbG35hlKSaUw/bS1laimh/NaxzTou1g2BYC/RWaz6Ym0cOzi8VGIb/Ydg uTMQ== X-Gm-Message-State: AOJu0YxY/Q6o1DQWjQh24a77nzNVFYyEIyHzz5Bfq/NK4sbDwkVsBK/k F/9tRZrUsxAsZtfR9HprtXEpq1c1KXj3FTLTGlxLN9cMKsk5TAGZStkDKkjaYUwadsv8FSByjTn w2g== X-Google-Smtp-Source: AGHT+IELp0rBFM14V065YjXOgr89Jc66inUUNJdPmAOsO3bpf4m905cHbWYEf+eOVTayxgElV/kyPA== 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> Reply-To: arthur.cohen@embecosm.com MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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=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: 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 (); -- 2.42.1