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

https://gcc.gnu.org/g:2a264a36932288c2a1fe848b03eab977267761dd

commit 2a264a36932288c2a1fe848b03eab977267761dd
Author: Arthur Cohen <arthur.cohen@embecosm.com>
Date:   Mon Apr 11 14:15:10 2022 +0200

    hir: Cleanup Visibility struct
    
    The HIR::Visibility struct was extremely similar to the AST::Visibility
    one. However, we do not need to keep as much information at the HIR
    level: Syntactic sugar such as pub(crate) can be kept as the desugared
    form, which is pub(in crate). Likewise, pub(self) can be desugared to
    pub(in self) which amounts to having a private item.

Diff:
---
 gcc/rust/backend/rust-compile-base.cc     |  3 +-
 gcc/rust/backend/rust-compile-implitem.cc |  5 +-
 gcc/rust/hir/tree/rust-hir-full-test.cc   | 16 ++-----
 gcc/rust/hir/tree/rust-hir-item.h         | 77 +++++++------------------------
 4 files changed, 24 insertions(+), 77 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc
index a705da729fe..602fc56c353 100644
--- a/gcc/rust/backend/rust-compile-base.cc
+++ b/gcc/rust/backend/rust-compile-base.cc
@@ -37,8 +37,7 @@ HIRCompileBase::setup_attributes_on_fndecl (
 {
   // if its the main fn or pub visibility mark its as DECL_PUBLIC
   // please see https://github.com/Rust-GCC/gccrs/pull/137
-  bool is_pub
-    = visibility.get_vis_type () != HIR::Visibility::PublicVisType::NONE;
+  bool is_pub = visibility.get_vis_type () == HIR::Visibility::VisType::PUBLIC;
   if (is_main_entry_point || is_pub)
     {
       TREE_PUBLIC (fndecl) = 1;
diff --git a/gcc/rust/backend/rust-compile-implitem.cc b/gcc/rust/backend/rust-compile-implitem.cc
index 8dc18d3c5cc..b44cdc984b8 100644
--- a/gcc/rust/backend/rust-compile-implitem.cc
+++ b/gcc/rust/backend/rust-compile-implitem.cc
@@ -84,9 +84,8 @@ CompileTraitItem::visit (HIR::TraitItemFunc &func)
     &canonical_path);
   rust_assert (ok);
 
-  // FIXME
-  HIR::Visibility vis (HIR::Visibility::PublicVisType::NONE,
-		       AST::SimplePath::create_empty ());
+  // FIXME: Get from lowering the item's visibility instead
+  auto vis = HIR::Visibility::create_public ();
   HIR::TraitFunctionDecl &function = func.get_decl ();
   tree fndecl
     = compile_function (ctx, function.get_function_name (),
diff --git a/gcc/rust/hir/tree/rust-hir-full-test.cc b/gcc/rust/hir/tree/rust-hir-full-test.cc
index 051e503d053..9b56086ea55 100644
--- a/gcc/rust/hir/tree/rust-hir-full-test.cc
+++ b/gcc/rust/hir/tree/rust-hir-full-test.cc
@@ -118,18 +118,12 @@ Crate::as_string () const
 std::string
 Visibility::as_string () const
 {
-  switch (public_vis_type)
+  switch (vis_type)
     {
-    case NONE:
-      return std::string ("pub");
-    case CRATE:
-      return std::string ("pub(crate)");
-    case SELF:
-      return std::string ("pub(self)");
-    case SUPER:
-      return std::string ("pub(super)");
-    case IN_PATH:
-      return std::string ("pub(in ") + in_path.as_string () + std::string (")");
+    case PRIVATE:
+      return std::string ("private");
+    case PUBLIC:
+      return std::string ("pub(in ") + path.as_string () + std::string (")");
     default:
       gcc_unreachable ();
     }
diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h
index 6d65837d966..3d761806216 100644
--- a/gcc/rust/hir/tree/rust-hir-item.h
+++ b/gcc/rust/hir/tree/rust-hir-item.h
@@ -553,91 +553,46 @@ public:
 struct Visibility
 {
 public:
-  enum PublicVisType
+  enum VisType
   {
-    NONE,
-    CRATE,
-    SELF,
-    SUPER,
-    IN_PATH
+    PRIVATE,
+    PUBLIC,
+    ERROR,
   };
 
 private:
-  // if vis is public, one of these
-  PublicVisType public_vis_type;
-  // Only assigned if public_vis_type is IN_PATH
-  AST::SimplePath in_path;
+  VisType vis_type;
+  AST::SimplePath path;
 
   // should this store location info?
 
 public:
   // Creates a Visibility - TODO make constructor protected or private?
-  Visibility (PublicVisType public_vis_type, AST::SimplePath in_path)
-    : public_vis_type (public_vis_type), in_path (std::move (in_path))
+  Visibility (VisType vis_type,
+	      AST::SimplePath path = AST::SimplePath::create_empty ())
+    : vis_type (vis_type), path (std::move (path))
   {}
 
   // Returns whether visibility is in an error state.
-  bool is_error () const
-  {
-    return public_vis_type == IN_PATH && in_path.is_empty ();
-  }
+  bool is_error () const { return vis_type == ERROR; }
 
   // Creates an error visibility.
   static Visibility create_error ()
   {
-    return Visibility (IN_PATH, AST::SimplePath::create_empty ());
+    return Visibility (ERROR, AST::SimplePath::create_empty ());
   }
 
-  // Unique pointer custom clone function
-  /*std::unique_ptr<Visibility> clone_visibility() const {
-      return std::unique_ptr<Visibility>(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.
+  // Creates a public visibility.
+  // FIXME: Remove this function: We should not be calling it anymore and
+  // instead we should be using `translate_visibility`
   static Visibility create_public ()
   {
-    return Visibility (NONE, AST::SimplePath::create_empty ());
-  }
-
-  // Creates a public visibility with crate-relative paths or whatever.
-  static Visibility create_crate ()
-  {
-    return Visibility (CRATE, AST::SimplePath::create_empty ());
-  }
-
-  // Creates a public visibility with self-relative paths or whatever.
-  static Visibility create_self ()
-  {
-    return Visibility (SELF, AST::SimplePath::create_empty ());
-  }
-
-  // Creates a public visibility with parent module-relative paths or
-  // whatever.
-  static Visibility create_super ()
-  {
-    return Visibility (SUPER, AST::SimplePath::create_empty ());
-  }
-
-  // Creates a public visibility with a given path or whatever.
-  static Visibility create_in_path (AST::SimplePath in_path)
-  {
-    return Visibility (IN_PATH, std::move (in_path));
+    return Visibility (ERROR, AST::SimplePath::create_empty ());
   }
 
-  PublicVisType get_vis_type () const { return public_vis_type; }
+  VisType get_vis_type () const { return vis_type; }
 
   std::string as_string () const;
-
-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


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

only message in thread, other threads:[~2022-06-08 12:27 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:27 [gcc/devel/rust/master] hir: Cleanup Visibility struct 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).