From 2bf4f17d1ff0dbfd5c21776f45094d518f01cd49 Mon Sep 17 00:00:00 2001 From: Arthur Cohen Date: Mon, 29 Aug 2022 13:37:00 +0200 Subject: dump: Add AST debugging using the AST::Dump class --- gcc/rust/ast/rust-ast-dump.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'gcc/rust/ast') diff --git a/gcc/rust/ast/rust-ast-dump.h b/gcc/rust/ast/rust-ast-dump.h index 7dd38fe..a72bd4d 100644 --- a/gcc/rust/ast/rust-ast-dump.h +++ b/gcc/rust/ast/rust-ast-dump.h @@ -52,6 +52,22 @@ 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); + } + private: std::ostream &stream; Indent indentation; -- cgit v1.1 From 842829a591c39c24332c3605606f5d89ff89f7d6 Mon Sep 17 00:00:00 2001 From: Arthur Cohen Date: Mon, 29 Aug 2022 13:37:13 +0200 Subject: ast: Only expand expressions and types if the kind is right --- gcc/rust/ast/rust-ast.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'gcc/rust/ast') diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index 461a246..0b032a6 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -24,6 +24,7 @@ #include "rust-hir-map.h" #include "rust-token.h" #include "rust-location.h" +#include "rust-diagnostics.h" namespace Rust { // TODO: remove typedefs and make actual types for these @@ -1868,6 +1869,10 @@ private: */ bool is_single_fragment () const { return nodes.size () == 1; } + bool is_single_fragment (SingleASTNode::NodeType expected) const + { + return is_single_fragment () && nodes[0].get_kind () == expected; + } bool is_single_fragment_kind (SingleASTNode::NodeType kind) const { @@ -1913,6 +1918,16 @@ public: bool should_expand () const { return !is_error (); } + bool is_expression_fragment () const + { + return is_single_fragment (SingleASTNode::NodeType::EXPRESSION); + } + + bool is_type_fragment () const + { + return is_single_fragment (SingleASTNode::NodeType::TYPE); + } + std::unique_ptr take_expression_fragment () { rust_assert (is_single_fragment_kind (SingleASTNode::NodeType::EXPRESSION)); -- cgit v1.1 From fcf6fea382f65930cd4f32f7661bc5d64d1559b4 Mon Sep 17 00:00:00 2001 From: Arthur Cohen Date: Mon, 29 Aug 2022 14:59:18 +0200 Subject: ast: Add better assertion on AST fragments Co-authored-by: philberty --- gcc/rust/ast/rust-ast.h | 47 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 8 deletions(-) (limited to 'gcc/rust/ast') diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index 0b032a6..e9e16e7 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -1819,7 +1819,7 @@ public: return true; } - std::string as_string () + std::string as_string () const { switch (kind) { @@ -1869,14 +1869,45 @@ private: */ bool is_single_fragment () const { return nodes.size () == 1; } - bool is_single_fragment (SingleASTNode::NodeType expected) const + + bool is_single_fragment_of_kind (SingleASTNode::NodeType expected) const { return is_single_fragment () && nodes[0].get_kind () == expected; } - bool is_single_fragment_kind (SingleASTNode::NodeType kind) const + void assert_single_fragment (SingleASTNode::NodeType expected) const { - return is_single_fragment () && nodes[0].get_kind () == kind; + static const std::map str_map = { + {SingleASTNode::NodeType::IMPL, "impl"}, + {SingleASTNode::NodeType::ITEM, "item"}, + {SingleASTNode::NodeType::TYPE, "type"}, + {SingleASTNode::NodeType::EXPRESSION, "expr"}, + {SingleASTNode::NodeType::STMT, "stmt"}, + {SingleASTNode::NodeType::EXTERN, "extern"}, + {SingleASTNode::NodeType::TRAIT, "trait"}, + {SingleASTNode::NodeType::TRAIT_IMPL, "trait impl"}, + }; + + auto actual = nodes[0].get_kind (); + auto fail = false; + + if (!is_single_fragment ()) + { + rust_error_at (Location (), "fragment is not single"); + fail = true; + } + + if (actual != expected) + { + rust_error_at ( + Location (), + "invalid fragment operation: expected %qs node, got %qs node", + str_map.find (expected)->second, + str_map.find (nodes[0].get_kind ())->second); + fail = true; + } + + rust_assert (!fail); } public: @@ -1920,23 +1951,23 @@ public: bool is_expression_fragment () const { - return is_single_fragment (SingleASTNode::NodeType::EXPRESSION); + return is_single_fragment_of_kind (SingleASTNode::NodeType::EXPRESSION); } bool is_type_fragment () const { - return is_single_fragment (SingleASTNode::NodeType::TYPE); + return is_single_fragment_of_kind (SingleASTNode::NodeType::TYPE); } std::unique_ptr take_expression_fragment () { - rust_assert (is_single_fragment_kind (SingleASTNode::NodeType::EXPRESSION)); + assert_single_fragment (SingleASTNode::NodeType::EXPRESSION); return nodes[0].take_expr (); } std::unique_ptr take_type_fragment () { - rust_assert (is_single_fragment_kind (SingleASTNode::NodeType::TYPE)); + assert_single_fragment (SingleASTNode::NodeType::TYPE); return nodes[0].take_type (); } -- cgit v1.1