public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] hir: Add `Kind` enum to downcast safely
@ 2022-06-08 12:28 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:28 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:544958743884a85b1dba6d67a92901077767d99b

commit 544958743884a85b1dba6d67a92901077767d99b
Author: Arthur Cohen <arthur.cohen@embecosm.com>
Date:   Tue Apr 5 11:33:43 2022 +0200

    hir: Add `Kind` enum to downcast safely

Diff:
---
 gcc/rust/hir/tree/rust-hir-item.h |  6 +++-
 gcc/rust/hir/tree/rust-hir.h      | 61 +++++++++++++++++++++++++++++++++++----
 2 files changed, 60 insertions(+), 7 deletions(-)

diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h
index 0c361e00ecd..2e35aea4abc 100644
--- a/gcc/rust/hir/tree/rust-hir-item.h
+++ b/gcc/rust/hir/tree/rust-hir-item.h
@@ -621,6 +621,8 @@ protected:
 public:
   using HIR::Stmt::accept_vis;
 
+  BaseKind get_hir_kind () override final { return VIS_ITEM; }
+
   /* Does the item have some kind of public visibility (non-default
    * visibility)? */
   bool has_visibility () const { return !visibility.is_error (); }
@@ -2744,7 +2746,7 @@ protected:
 };
 
 // Abstract base class for an item used inside an extern block
-class ExternalItem
+class ExternalItem : public Node
 {
   Analysis::NodeMapping mappings;
   AST::AttrVec outer_attrs;
@@ -2755,6 +2757,8 @@ class ExternalItem
 public:
   virtual ~ExternalItem () {}
 
+  BaseKind get_hir_kind () override final { return EXTERNAL; }
+
   // Returns whether item has outer attributes.
   bool has_outer_attrs () const { return !outer_attrs.empty (); }
 
diff --git a/gcc/rust/hir/tree/rust-hir.h b/gcc/rust/hir/tree/rust-hir.h
index fda7e1d5d9d..e2c930e1374 100644
--- a/gcc/rust/hir/tree/rust-hir.h
+++ b/gcc/rust/hir/tree/rust-hir.h
@@ -45,6 +45,41 @@ class HIRTypeVisitor;
 // forward decl for use in token tree method
 class Token;
 
+// Kind for downcasting various HIR nodes to other base classes when visiting
+// them
+enum BaseKind
+{
+  /* class ExternalItem */
+  EXTERNAL,
+  /* class TraitItem */
+  TRAIT_ITEM,
+  /* class VisItem */
+  VIS_ITEM,
+  /* class Item */
+  ITEM,
+  /* class ImplItem */
+  IMPL,
+  /* class Type */
+  TYPE,
+  /* class Stmt */
+  STMT,
+  /* class Expr */
+  EXPR,
+  /* class Pattern */
+  PATTERN,
+};
+
+class Node
+{
+public:
+  /**
+   * Get the kind of HIR node we are dealing with. This is useful for
+   * downcasting to more precise types when necessary, i.e going from an `Item*`
+   * to a `VisItem*`
+   */
+  virtual BaseKind get_hir_kind () = 0;
+};
+
 // A literal - value with a type. Used in LiteralExpr and LiteralPattern.
 struct Literal
 {
@@ -91,7 +126,7 @@ public:
 
 /* Base statement abstract class. Note that most "statements" are not allowed in
  * top-level module scope - only a subclass of statements called "items" are. */
-class Stmt
+class Stmt : public Node
 {
 public:
   // Unique pointer custom clone function
@@ -100,6 +135,8 @@ public:
     return std::unique_ptr<Stmt> (clone_stmt_impl ());
   }
 
+  BaseKind get_hir_kind () override { return STMT; }
+
   virtual ~Stmt () {}
 
   virtual std::string as_string () const = 0;
@@ -138,6 +175,8 @@ public:
     return std::unique_ptr<Item> (clone_item_impl ());
   }
 
+  BaseKind get_hir_kind () override { return ITEM; }
+
   std::string as_string () const override;
 
   /* Adds crate names to the vector passed by reference, if it can
@@ -171,7 +210,7 @@ protected:
 class ExprWithoutBlock;
 
 // Base expression HIR node - abstract
-class Expr
+class Expr : public Node
 {
   AST::AttrVec outer_attrs;
   Analysis::NodeMapping mappings;
@@ -213,6 +252,8 @@ public:
     Path,
   };
 
+  BaseKind get_hir_kind () override final { return EXPR; }
+
   const AST::AttrVec &get_outer_attrs () const { return outer_attrs; }
 
   // Unique pointer custom clone function
@@ -358,7 +399,7 @@ protected:
 };
 
 // Pattern base HIR node
-class Pattern
+class Pattern : public Node
 {
 public:
   enum PatternType
@@ -376,6 +417,8 @@ public:
     SLICE,
   };
 
+  BaseKind get_hir_kind () override final { return PATTERN; }
+
   // Unique pointer custom clone function
   std::unique_ptr<Pattern> clone_pattern () const
   {
@@ -406,7 +449,7 @@ protected:
 class TraitBound;
 
 // Base class for types as represented in HIR - abstract
-class Type
+class Type : public Node
 {
 public:
   // Unique pointer custom clone function
@@ -418,6 +461,8 @@ public:
   // virtual destructor
   virtual ~Type () {}
 
+  BaseKind get_hir_kind () override final { return TYPE; }
+
   virtual std::string as_string () const = 0;
 
   /* HACK: convert to trait bound. Virtual method overriden by classes that
@@ -686,7 +731,7 @@ protected:
 };
 
 // Item used in trait declarations - abstract base class
-class TraitItem
+class TraitItem : public Node
 {
 public:
   enum TraitItemKind
@@ -696,6 +741,8 @@ public:
     TYPE
   };
 
+  BaseKind get_hir_kind () override final { return TRAIT_ITEM; }
+
 protected:
   // Constructor
   TraitItem (Analysis::NodeMapping mappings) : mappings (mappings) {}
@@ -728,7 +775,7 @@ public:
   virtual const AST::AttrVec &get_outer_attrs () const = 0;
 };
 
-class ImplItem
+class ImplItem : public Node
 {
 public:
   enum ImplItemType
@@ -740,6 +787,8 @@ public:
 
   virtual ~ImplItem () {}
 
+  BaseKind get_hir_kind () override final { return IMPL; }
+
   // Unique pointer custom clone function
   std::unique_ptr<ImplItem> clone_inherent_impl_item () const
   {


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

only message in thread, other threads:[~2022-06-08 12:28 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:28 [gcc/devel/rust/master] hir: Add `Kind` enum to downcast safely 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).