]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Introduce AST::Visitable class for AST
authorMarc Poulhiès <dkm@kataplop.net>
Thu, 6 Apr 2023 17:20:55 +0000 (19:20 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:28:46 +0000 (18:28 +0100)
AST::Visitable is an abstract class with a unique accept_vis() method.
Make all abstract AST node class inherit from this class.

Allows for easy definition of operations on nodes that must accept a
visitor.

The static Dump::dump() is an example of such use: the static method
accepts any AST node, creates a Dump visitor and have it visit the AST
starting at the node.

This change also inserts a debug(Visitable&) function in the global
namespace to make it easy to call from the debugger (similar to
debug_tree or debug(rtx*) functions).

gcc/rust/ChangeLog:

* ast/rust-ast-dump.cc (Dump::debug): New.
* ast/rust-ast-dump.h (Dump::debug): Untemplate it.
(debug): New.
* ast/rust-ast.h (class Visitable): New.
(class TokenTree): Inherit from Visitable.
(class MacroMatch): Likewise.
(class AttrInput): Likewise.
(class MetaItemInner): Likewise.
(class Pattern): Likewise.
(classTypeParamBound): Likewise.
(class GenericParam): Likewise.
(class TraitItem): Likewise.
(classInherentImplItem): Likewise.
(class TraitImplItem): Likewise.
(class ExternalItem): Likewise.
(class SingleASTNode): Likewise.

Signed-off-by: Marc Poulhiès <dkm@kataplop.net>
gcc/rust/ast/rust-ast-dump.cc
gcc/rust/ast/rust-ast-dump.h
gcc/rust/ast/rust-ast.h

index 2335ffdddb80a3e2f84c3eb690412d04854f80b3..d25a2a4af1aa5f78684e52c57725348ef075c263 100644 (file)
@@ -17,6 +17,7 @@
 // <http://www.gnu.org/licenses/>.
 
 #include "rust-ast-dump.h"
+#include "rust-expr.h"
 
 namespace Rust {
 namespace AST {
@@ -1916,5 +1917,21 @@ Dump::visit (BareFunctionType &type)
     }
 }
 
+void
+Dump::debug (Visitable &v)
+{
+  auto dump = Dump (std::cerr);
+
+  std::cerr << '\n';
+  v.accept_vis (dump);
+  std::cerr << '\n';
+}
+
 } // namespace AST
 } // namespace Rust
+
+void
+debug (Rust::AST::Visitable &v)
+{
+  Rust::AST::Dump::debug (v);
+}
index 412873382abedc687e04e3a61712ea43be90390b..26f7208ca4e3b45c2687e9e4c99df346accd8c77 100644 (file)
@@ -38,21 +38,8 @@ public:
   void go (AST::Crate &crate);
   void go (AST::Item &item);
 
-  /**
-   * Use the AST Dump as a debugging tool
-   */
-  template <typename T> static void debug (T &instance)
-  {
-    auto dump = Dump (std::cerr);
-
-    std::cerr << '\n';
-    instance.accept_vis (dump);
-    std::cerr << '\n';
-  }
-  template <typename T> static void debug (std::unique_ptr<T> &instance)
-  {
-    debug (*instance);
-  }
+  // Helper method to get a quick debug dump to standard error output
+  static void debug (Visitable &v);
 
 private:
   std::ostream &stream;
@@ -309,4 +296,8 @@ private:
 } // namespace AST
 } // namespace Rust
 
+// In the global namespace to make it easier to call from debugger
+void
+debug (Rust::AST::Visitable &v);
+
 #endif // !RUST_AST_DUMP_H
index 0d2a16b23d8aba902e578bf905c937c5c538473e..7d7de89cc8be1533512780c6cb29067c05cab303 100644 (file)
@@ -46,8 +46,14 @@ enum Kind
   MACRO_INVOCATION,
 };
 
+class Visitable
+{
+public:
+  virtual void accept_vis (ASTVisitor &vis) = 0;
+};
+
 // Abstract base class for all AST elements
-class Node
+class Node : public Visitable
 {
 public:
   /**
@@ -72,7 +78,7 @@ enum DelimType
 class Token;
 
 // A tree of tokens (or a single token) - abstract base class
-class TokenTree
+class TokenTree : public Visitable
 {
 public:
   virtual ~TokenTree () {}
@@ -85,8 +91,6 @@ public:
 
   virtual std::string as_string () const = 0;
 
-  virtual void accept_vis (ASTVisitor &vis) = 0;
-
   /* Converts token tree to a flat token stream. Tokens must be pointer to avoid
    * mutual dependency with Token. */
   virtual std::vector<std::unique_ptr<Token> > to_token_stream () const = 0;
@@ -97,7 +101,7 @@ protected:
 };
 
 // Abstract base class for a macro match
-class MacroMatch
+class MacroMatch : public Visitable
 {
 public:
   enum MacroMatchType
@@ -119,8 +123,6 @@ public:
     return std::unique_ptr<MacroMatch> (clone_macro_match_impl ());
   }
 
-  virtual void accept_vis (ASTVisitor &vis) = 0;
-
   virtual MacroMatchType get_macro_match_type () const = 0;
 
 protected:
@@ -587,7 +589,7 @@ protected:
 };
 
 // Attribute body - abstract base class
-class AttrInput
+class AttrInput : public Visitable
 {
 public:
   enum AttrInputType
@@ -607,8 +609,6 @@ public:
 
   virtual std::string as_string () const = 0;
 
-  virtual void accept_vis (ASTVisitor &vis) = 0;
-
   virtual bool check_cfg_predicate (const Session &session) const = 0;
 
   // Parse attribute input to meta item, if possible
@@ -630,7 +630,7 @@ protected:
 class MetaNameValueStr;
 
 // abstract base meta item inner class
-class MetaItemInner
+class MetaItemInner : public Visitable
 {
 protected:
   // pure virtual as MetaItemInner
@@ -649,8 +649,6 @@ public:
 
   virtual std::string as_string () const = 0;
 
-  virtual void accept_vis (ASTVisitor &vis) = 0;
-
   /* HACK: used to simplify parsing - creates a copy of that type, or returns
    * null */
   virtual std::unique_ptr<MetaNameValueStr> to_meta_name_value_str () const;
@@ -894,8 +892,6 @@ public:
 
   virtual std::string as_string () const = 0;
 
-  virtual void accept_vis (ASTVisitor &vis) = 0;
-
   virtual Location get_locus () const = 0;
 
   virtual void mark_for_strip () = 0;
@@ -976,8 +972,6 @@ public:
   // HACK: strictly not needed, but faster than full downcast clone
   virtual bool is_expr_without_block () const = 0;
 
-  virtual void accept_vis (ASTVisitor &vis) = 0;
-
   virtual void mark_for_strip () = 0;
   virtual bool is_marked_for_strip () const = 0;
 
@@ -1092,7 +1086,7 @@ protected:
 };
 
 // Pattern base AST node
-class Pattern
+class Pattern : public Visitable
 {
 public:
   // Unique pointer custom clone function
@@ -1106,7 +1100,6 @@ public:
   virtual ~Pattern () {}
 
   virtual std::string as_string () const = 0;
-  virtual void accept_vis (ASTVisitor &vis) = 0;
 
   // as only one kind of pattern can be stripped, have default of nothing
   virtual void mark_for_strip () {}
@@ -1144,8 +1137,6 @@ public:
   /* as pointer, shouldn't require definition beforehand, only forward
    * declaration. */
 
-  virtual void accept_vis (ASTVisitor &vis) = 0;
-
   // as only two kinds of types can be stripped, have default of nothing
   virtual void mark_for_strip () {}
   virtual bool is_marked_for_strip () const { return false; }
@@ -1190,7 +1181,7 @@ protected:
 
 /* Abstract base class representing a type param bound - Lifetime and TraitBound
  * extends it */
-class TypeParamBound
+class TypeParamBound : public Visitable
 {
 public:
   virtual ~TypeParamBound () {}
@@ -1203,8 +1194,6 @@ public:
 
   virtual std::string as_string () const = 0;
 
-  virtual void accept_vis (ASTVisitor &vis) = 0;
-
   NodeId get_node_id () const { return node_id; }
 
   virtual Location get_locus () const = 0;
@@ -1279,7 +1268,7 @@ protected:
 
 /* Base generic parameter in AST. Abstract - can be represented by a Lifetime or
  * Type param */
-class GenericParam
+class GenericParam : public Visitable
 {
 public:
   enum class Kind
@@ -1299,8 +1288,6 @@ public:
 
   virtual std::string as_string () const = 0;
 
-  virtual void accept_vis (ASTVisitor &vis) = 0;
-
   virtual Location get_locus () const = 0;
 
   virtual Kind get_kind () const = 0;
@@ -1372,7 +1359,7 @@ protected:
 };
 
 // Item used in trait declarations - abstract base class
-class TraitItem
+class TraitItem : public Visitable
 {
 protected:
   TraitItem (Location locus)
@@ -1396,8 +1383,6 @@ public:
 
   virtual std::string as_string () const = 0;
 
-  virtual void accept_vis (ASTVisitor &vis) = 0;
-
   virtual void mark_for_strip () = 0;
   virtual bool is_marked_for_strip () const = 0;
 
@@ -1407,7 +1392,7 @@ public:
 
 /* Abstract base class for items used within an inherent impl block (the impl
  * name {} one) */
-class InherentImplItem
+class InherentImplItem : public Visitable
 {
 protected:
   // Clone function implementation as pure virtual method
@@ -1424,8 +1409,6 @@ public:
 
   virtual std::string as_string () const = 0;
 
-  virtual void accept_vis (ASTVisitor &vis) = 0;
-
   virtual void mark_for_strip () = 0;
   virtual bool is_marked_for_strip () const = 0;
 
@@ -1433,7 +1416,7 @@ public:
 };
 
 // Abstract base class for items used in a trait impl
-class TraitImplItem
+class TraitImplItem : public Visitable
 {
 protected:
   virtual TraitImplItem *clone_trait_impl_item_impl () const = 0;
@@ -1449,14 +1432,12 @@ public:
 
   virtual std::string as_string () const = 0;
 
-  virtual void accept_vis (ASTVisitor &vis) = 0;
-
   virtual void mark_for_strip () = 0;
   virtual bool is_marked_for_strip () const = 0;
 };
 
 // Abstract base class for an item used inside an extern block
-class ExternalItem
+class ExternalItem : public Visitable
 {
 public:
   ExternalItem () : node_id (Analysis::Mappings::get ()->get_next_node_id ()) {}
@@ -1471,8 +1452,6 @@ public:
 
   virtual std::string as_string () const = 0;
 
-  virtual void accept_vis (ASTVisitor &vis) = 0;
-
   virtual void mark_for_strip () = 0;
   virtual bool is_marked_for_strip () const = 0;
 
@@ -1576,7 +1555,7 @@ public:
   }
 };
 
-class SingleASTNode
+class SingleASTNode : public Visitable
 {
 public:
   enum NodeType
@@ -1792,7 +1771,7 @@ public:
     return std::move (type);
   }
 
-  void accept_vis (ASTVisitor &vis)
+  void accept_vis (ASTVisitor &vis) override
   {
     switch (kind)
       {