From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 428CA386DC70; Wed, 8 Jun 2022 12:30:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 428CA386DC70 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] visibility: Add create_private() static function X-Act-Checkin: gcc X-Git-Author: Arthur Cohen X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 7430791e0f71f1882a0f856c496071b76c61a6bc X-Git-Newrev: 03cb435c1905e03da44a6a0723f03c38e7f59b09 Message-Id: <20220608123051.428CA386DC70@sourceware.org> Date: Wed, 8 Jun 2022 12:30:51 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Jun 2022 12:30:51 -0000 https://gcc.gnu.org/g:03cb435c1905e03da44a6a0723f03c38e7f59b09 commit 03cb435c1905e03da44a6a0723f03c38e7f59b09 Author: Arthur Cohen Date: Tue Apr 12 17:43:58 2022 +0200 visibility: Add create_private() static function When parsing a visibility in `parse_visibility`, it is not an error to not have a pub token: It simply means we want to create a private visibility. If we had C++14 or another language, we could instead represent all visibilities as an optional where the Visibility class would not need to change. But I think the best course of action for our case is to instead keep visibilities even when they are private and have a special case in the `VisKind` enumeration. Diff: --- gcc/rust/ast/rust-ast-full-test.cc | 14 ++++++----- gcc/rust/ast/rust-item.h | 51 ++++++++++++++++++++++---------------- gcc/rust/hir/rust-ast-lower.cc | 15 +++++------ gcc/rust/parse/rust-parse-impl.h | 2 +- 4 files changed, 45 insertions(+), 37 deletions(-) diff --git a/gcc/rust/ast/rust-ast-full-test.cc b/gcc/rust/ast/rust-ast-full-test.cc index d58017731ee..e6bad4bcbad 100644 --- a/gcc/rust/ast/rust-ast-full-test.cc +++ b/gcc/rust/ast/rust-ast-full-test.cc @@ -306,17 +306,19 @@ SimplePath::as_string () const std::string Visibility::as_string () const { - switch (public_vis_type) + switch (vis_type) { - case NONE: + case PRIV: + return std::string (""); + case PUB: return std::string ("pub"); - case CRATE: + case PUB_CRATE: return std::string ("pub(crate)"); - case SELF: + case PUB_SELF: return std::string ("pub(self)"); - case SUPER: + case PUB_SUPER: return std::string ("pub(super)"); - case IN_PATH: + case PUB_IN_PATH: return std::string ("pub(in ") + in_path.as_string () + std::string (")"); default: gcc_unreachable (); diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h index f08f2f0c515..1b925c35b97 100644 --- a/gcc/rust/ast/rust-item.h +++ b/gcc/rust/ast/rust-item.h @@ -609,41 +609,44 @@ protected: struct Visibility { public: - enum PublicVisType - { - NONE, - CRATE, - SELF, - SUPER, - IN_PATH + enum VisType + { + PRIV, + PUB, + PUB_CRATE, + PUB_SELF, + PUB_SUPER, + PUB_IN_PATH }; private: - // if vis is public, one of these - PublicVisType public_vis_type; - // Only assigned if public_vis_type is IN_PATH + VisType vis_type; + // Only assigned if vis_type is IN_PATH SimplePath in_path; // should this store location info? public: // Creates a Visibility - TODO make constructor protected or private? - Visibility (PublicVisType public_vis_type, SimplePath in_path) - : public_vis_type (public_vis_type), in_path (std::move (in_path)) + Visibility (VisType vis_type, SimplePath in_path) + : vis_type (vis_type), in_path (std::move (in_path)) {} - PublicVisType get_public_vis_type () const { return public_vis_type; } + VisType get_public_vis_type () const { return vis_type; } // Returns whether visibility is in an error state. bool is_error () const { - return public_vis_type == IN_PATH && in_path.is_empty (); + return vis_type == PUB_IN_PATH && in_path.is_empty (); } + // Returns whether visibility is public or not. + bool is_public () const { return vis_type != PRIV && !is_error (); } + // Creates an error visibility. static Visibility create_error () { - return Visibility (IN_PATH, SimplePath::create_empty ()); + return Visibility (PUB_IN_PATH, SimplePath::create_empty ()); } // Unique pointer custom clone function @@ -657,32 +660,38 @@ public: // Creates a public visibility with no further features/arguments. static Visibility create_public () { - return Visibility (NONE, SimplePath::create_empty ()); + return Visibility (PUB, SimplePath::create_empty ()); } // Creates a public visibility with crate-relative paths or whatever. static Visibility create_crate () { - return Visibility (CRATE, SimplePath::create_empty ()); + return Visibility (PUB_CRATE, SimplePath::create_empty ()); } // Creates a public visibility with self-relative paths or whatever. static Visibility create_self () { - return Visibility (SELF, SimplePath::create_empty ()); + return Visibility (PUB_SELF, SimplePath::create_empty ()); } // Creates a public visibility with parent module-relative paths or // whatever. static Visibility create_super () { - return Visibility (SUPER, SimplePath::create_empty ()); + return Visibility (PUB_SUPER, SimplePath::create_empty ()); + } + + // Creates a private visibility + static Visibility create_private () + { + return Visibility (PRIV, SimplePath::create_empty ()); } // Creates a public visibility with a given path or whatever. static Visibility create_in_path (SimplePath in_path) { - return Visibility (IN_PATH, std::move (in_path)); + return Visibility (PUB_IN_PATH, std::move (in_path)); } std::string as_string () const; @@ -938,7 +947,7 @@ protected: public: /* Does the item have some kind of public visibility (non-default * visibility)? */ - bool has_visibility () const { return !visibility.is_error (); } + bool has_visibility () const { return visibility.is_public (); } std::string as_string () const override; diff --git a/gcc/rust/hir/rust-ast-lower.cc b/gcc/rust/hir/rust-ast-lower.cc index 1da823a8e32..4a344c9f5ab 100644 --- a/gcc/rust/hir/rust-ast-lower.cc +++ b/gcc/rust/hir/rust-ast-lower.cc @@ -36,24 +36,21 @@ translate_visibility (const AST::Visibility &vis) if (vis.is_error ()) return Visibility::create_error (); - // FIXME: ... And then use this? - // if (vis.is_private ()) - // return Visibility::create_private (); - switch (vis.get_public_vis_type ()) { - case AST::Visibility::NONE: + case AST::Visibility::PUB: return Visibility (Visibility::VisType::PUBLIC); - case AST::Visibility::SELF: + case AST::Visibility::PRIV: + case AST::Visibility::PUB_SELF: return Visibility (Visibility::VisType::PRIVATE); // Desugar pub(crate) into pub(in crate) and so on - case AST::Visibility::CRATE: + case AST::Visibility::PUB_CRATE: return Visibility (Visibility::PUBLIC, AST::SimplePath::from_str ("crate")); - case AST::Visibility::SUPER: + case AST::Visibility::PUB_SUPER: return Visibility (Visibility::PUBLIC, AST::SimplePath::from_str ("super")); - case AST::Visibility::IN_PATH: + case AST::Visibility::PUB_IN_PATH: return Visibility (Visibility::VisType::PUBLIC, vis.get_path ()); break; } diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index 48c39de3562..d320f0b1ec5 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -2121,7 +2121,7 @@ Parser::parse_visibility () // check for no visibility if (lexer.peek_token ()->get_id () != PUB) { - return AST::Visibility::create_error (); + return AST::Visibility::create_private (); } lexer.skip_token ();