diff options
author | Marc Poulhiès <dkm@kataplop.net> | 2023-04-06 19:20:55 +0200 |
---|---|---|
committer | CohenArthur <arthur.cohen@embecosm.com> | 2023-04-11 07:42:45 +0000 |
commit | 62d48d5e4a9e10aa31509dcdd3e6d960c40f4134 (patch) | |
tree | 904408b4e3dc158a4f9330e7673cd9fd951f6723 /gcc | |
parent | 2bc9f60374ff5a5910d5a3d746d20903a8f50543 (diff) | |
download | gcc-62d48d5e4a9e10aa31509dcdd3e6d960c40f4134.zip gcc-62d48d5e4a9e10aa31509dcdd3e6d960c40f4134.tar.gz gcc-62d48d5e4a9e10aa31509dcdd3e6d960c40f4134.tar.bz2 |
gccrs: Introduce AST::Visitable class for AST
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>
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/ast/rust-ast-dump.cc | 17 | ||||
-rw-r--r-- | gcc/rust/ast/rust-ast-dump.h | 21 | ||||
-rw-r--r-- | 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 aab8f0e..05de528 100644 --- a/gcc/rust/ast/rust-ast-dump.cc +++ b/gcc/rust/ast/rust-ast-dump.cc @@ -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); +} diff --git a/gcc/rust/ast/rust-ast-dump.h b/gcc/rust/ast/rust-ast-dump.h index 4b76ec1..45722f2 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 <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 diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index be9c722..540a262 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<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) { |