public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] visibility: Add create_private() static function
@ 2022-06-08 12:30 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:30 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:03cb435c1905e03da44a6a0723f03c38e7f59b09

commit 03cb435c1905e03da44a6a0723f03c38e7f59b09
Author: Arthur Cohen <arthur.cohen@embecosm.com>
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<AST::Visibility> 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<ManagedTokenSource>::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 ();


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-06-08 12:30 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 12:30 [gcc/devel/rust/master] visibility: Add create_private() static function Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).