From cd773704975f399a8c736a84509dfe2e106412a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Poulhi=C3=A8s?= Date: Thu, 6 Apr 2023 19:20:55 +0200 Subject: gccrs: Introduce AST::Visitable class for AST MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 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.h | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) (limited to 'gcc/rust/ast/rust-ast-dump.h') diff --git a/gcc/rust/ast/rust-ast-dump.h b/gcc/rust/ast/rust-ast-dump.h index 4128733..26f7208 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 -- cgit v1.1