From cd773704975f399a8c736a84509dfe2e106412a7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marc=20Poulhi=C3=A8s?= Date: Thu, 6 Apr 2023 19:20:55 +0200 Subject: [PATCH] gccrs: Introduce AST::Visitable class for AST MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 --- gcc/rust/ast/rust-ast-dump.cc | 17 ++++++++++ gcc/rust/ast/rust-ast-dump.h | 21 ++++-------- gcc/rust/ast/rust-ast.h | 61 ++++++++++++----------------------- 3 files changed, 43 insertions(+), 56 deletions(-) diff --git a/gcc/rust/ast/rust-ast-dump.cc b/gcc/rust/ast/rust-ast-dump.cc index 2335ffdddb80..d25a2a4af1aa 100644 --- a/gcc/rust/ast/rust-ast-dump.cc +++ b/gcc/rust/ast/rust-ast-dump.cc @@ -17,6 +17,7 @@ // . #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); +} diff --git a/gcc/rust/ast/rust-ast-dump.h b/gcc/rust/ast/rust-ast-dump.h index 412873382abe..26f7208ca4e3 100644 --- a/gcc/rust/ast/rust-ast-dump.h +++ b/gcc/rust/ast/rust-ast-dump.h @@ -38,21 +38,8 @@ public: void go (AST::Crate &crate); void go (AST::Item &item); - /** - * Use the AST Dump as a debugging tool - */ - template static void debug (T &instance) - { - auto dump = Dump (std::cerr); - - std::cerr << '\n'; - instance.accept_vis (dump); - std::cerr << '\n'; - } - template static void debug (std::unique_ptr &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 diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index 0d2a16b23d8a..7d7de89cc8be 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -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 > 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 (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 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) { -- 2.47.2