aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/ast/rust-ast-dump.h
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-06-01 10:44:16 +0000
committerGitHub <noreply@github.com>2022-06-01 10:44:16 +0000
commit0866a4fbc6e7f70cd3708467419c60af8c6104f2 (patch)
treec590b511d30ca79d4babda5575614aa1d67a49ec /gcc/rust/ast/rust-ast-dump.h
parent998f1f73c2626f7ffa9655058e306b7f4e07c584 (diff)
parent573b6298596fae8708982c65db0b66b989299805 (diff)
downloadgcc-0866a4fbc6e7f70cd3708467419c60af8c6104f2.zip
gcc-0866a4fbc6e7f70cd3708467419c60af8c6104f2.tar.gz
gcc-0866a4fbc6e7f70cd3708467419c60af8c6104f2.tar.bz2
Merge #1287
1287: Add AST dump visitor r=CohenArthur a=CohenArthur This adds a base for pretty printing our AST through a new visitor (Addresses #1261). This adds enough code to pretty print "functions" without types in the parameters or return type. Looking for feedback on the implementation before we keep going with more code :) Here's a tiny patch if you want to see the output whenever you're running `-frust-dump-all`: ```diff diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc index 0b0e5af915f..3e39c6a6e17 100644 --- a/gcc/rust/rust-session-manager.cc +++ b/gcc/rust/rust-session-manager.cc `@@` -31,6 +31,7 `@@` #include "rust-cfg-parser.h" #include "rust-lint-scan-deadcode.h" #include "rust-lint-unused-var.h" +#include "rust-ast-dump.h" #include "diagnostic.h" #include "input.h" `@@` -1023,6 +1024,8 `@@` Session::dump_ast (Parser<Lexer> &parser, AST::Crate &crate) const parser.debug_dump_ast_output (crate, out); out.close (); + + AST::Dump (std::cerr).go (crate); } void ``` output: ```rust arthur@platypus ~/G/r/gccrs (add-ast-dump-visitor) [1]> cat test.rs fn foo0(a: i32) { let a = 15; let b: i32 = 15; } fn foo1(a: &i32) {} struct S; fn foo2(a: S) {} fn foo3(a: S) -> i32 {} arthur@platypus ~/G/r/gccrs (add-ast-dump-visitor)> build/gcc/rust1 test.rs -frust-dump-all fn foo0(a: ) { let a = ; let b: = ; } fn foo1(a: ) { } fn foo2(a: ) { } fn foo3(a: ) -> { } test.rs:11:1: error: expected [i32] got [()] 11 | fn foo3(a: S) -> i32 {} | ^ ~ Analyzing compilation unit Time variable usr sys wall GGC phase setup : 0.00 ( 0%) 0.00 ( 0%) 0.01 (100%) 129k ( 94%) TOTAL : 0.00 0.00 0.01 137k Extra diagnostic checks enabled; compiler may run slowly. Configure with --enable-checking=release to disable checks. ``` Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
Diffstat (limited to 'gcc/rust/ast/rust-ast-dump.h')
-rw-r--r--gcc/rust/ast/rust-ast-dump.h239
1 files changed, 239 insertions, 0 deletions
diff --git a/gcc/rust/ast/rust-ast-dump.h b/gcc/rust/ast/rust-ast-dump.h
new file mode 100644
index 0000000..436c2b5
--- /dev/null
+++ b/gcc/rust/ast/rust-ast-dump.h
@@ -0,0 +1,239 @@
+// Copyright (C) 2020-2022 Free Software Foundation, Inc.
+
+// This file is part of GCC.
+
+// GCC is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3, or (at your option) any later
+// version.
+
+// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+// for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with GCC; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+#include "rust-ast-visitor.h"
+#include "rust-ast.h"
+#include "rust-ast-full.h"
+
+#ifndef RUST_AST_DUMP_H
+#define RUST_AST_DUMP_H
+
+namespace Rust {
+namespace AST {
+
+// TODO: We might want to reuse this class somewhere else
+class Indent
+{
+public:
+ Indent ();
+
+ friend std::ostream &operator<< (std::ostream &stream, const Indent &indent);
+
+ void increment ();
+ void decrement ();
+
+private:
+ size_t tabs;
+};
+
+class Dump : public ASTVisitor
+{
+public:
+ Dump (std::ostream &stream);
+
+ /**
+ * Run the visitor on an entire crate and its items
+ */
+ void go (AST::Crate &crate);
+
+private:
+ std::ostream &stream;
+ Indent indentation;
+
+ /**
+ * Format a function's definition parameter
+ */
+ void format_function_param (FunctionParam &param);
+
+ // rust-ast.h
+ void visit (Token &tok);
+ void visit (DelimTokenTree &delim_tok_tree);
+ void visit (AttrInputMetaItemContainer &input);
+ void visit (IdentifierExpr &ident_expr);
+ void visit (Lifetime &lifetime);
+ void visit (LifetimeParam &lifetime_param);
+
+ // rust-path.h
+ void visit (PathInExpression &path);
+ void visit (TypePathSegment &segment);
+ void visit (TypePathSegmentGeneric &segment);
+ void visit (TypePathSegmentFunction &segment);
+ void visit (TypePath &path);
+ void visit (QualifiedPathInExpression &path);
+ void visit (QualifiedPathInType &path);
+
+ // rust-expr.h
+ void visit (LiteralExpr &expr);
+ void visit (AttrInputLiteral &attr_input);
+ void visit (MetaItemLitExpr &meta_item);
+ void visit (MetaItemPathLit &meta_item);
+ void visit (BorrowExpr &expr);
+ void visit (DereferenceExpr &expr);
+ void visit (ErrorPropagationExpr &expr);
+ void visit (NegationExpr &expr);
+ void visit (ArithmeticOrLogicalExpr &expr);
+ void visit (ComparisonExpr &expr);
+ void visit (LazyBooleanExpr &expr);
+ void visit (TypeCastExpr &expr);
+ void visit (AssignmentExpr &expr);
+ void visit (CompoundAssignmentExpr &expr);
+ void visit (GroupedExpr &expr);
+ void visit (ArrayElemsValues &elems);
+ void visit (ArrayElemsCopied &elems);
+ void visit (ArrayExpr &expr);
+ void visit (ArrayIndexExpr &expr);
+ void visit (TupleExpr &expr);
+ void visit (TupleIndexExpr &expr);
+ void visit (StructExprStruct &expr);
+ void visit (StructExprFieldIdentifier &field);
+ void visit (StructExprFieldIdentifierValue &field);
+ void visit (StructExprFieldIndexValue &field);
+ void visit (StructExprStructFields &expr);
+ void visit (StructExprStructBase &expr);
+ void visit (CallExpr &expr);
+ void visit (MethodCallExpr &expr);
+ void visit (FieldAccessExpr &expr);
+ void visit (ClosureExprInner &expr);
+ void visit (BlockExpr &expr);
+ void visit (ClosureExprInnerTyped &expr);
+ void visit (ContinueExpr &expr);
+ void visit (BreakExpr &expr);
+ void visit (RangeFromToExpr &expr);
+ void visit (RangeFromExpr &expr);
+ void visit (RangeToExpr &expr);
+ void visit (RangeFullExpr &expr);
+ void visit (RangeFromToInclExpr &expr);
+ void visit (RangeToInclExpr &expr);
+ void visit (ReturnExpr &expr);
+ void visit (UnsafeBlockExpr &expr);
+ void visit (LoopExpr &expr);
+ void visit (WhileLoopExpr &expr);
+ void visit (WhileLetLoopExpr &expr);
+ void visit (ForLoopExpr &expr);
+ void visit (IfExpr &expr);
+ void visit (IfExprConseqElse &expr);
+ void visit (IfExprConseqIf &expr);
+ void visit (IfExprConseqIfLet &expr);
+ void visit (IfLetExpr &expr);
+ void visit (IfLetExprConseqElse &expr);
+ void visit (IfLetExprConseqIf &expr);
+ void visit (IfLetExprConseqIfLet &expr);
+ void visit (MatchExpr &expr);
+ void visit (AwaitExpr &expr);
+ void visit (AsyncBlockExpr &expr);
+
+ // rust-item.h
+ void visit (TypeParam &param);
+ void visit (LifetimeWhereClauseItem &item);
+ void visit (TypeBoundWhereClauseItem &item);
+ void visit (Method &method);
+ void visit (Module &module);
+ void visit (ExternCrate &crate);
+ void visit (UseTreeGlob &use_tree);
+ void visit (UseTreeList &use_tree);
+ void visit (UseTreeRebind &use_tree);
+ void visit (UseDeclaration &use_decl);
+ void visit (Function &function);
+ void visit (TypeAlias &type_alias);
+ void visit (StructStruct &struct_item);
+ void visit (TupleStruct &tuple_struct);
+ void visit (EnumItem &item);
+ void visit (EnumItemTuple &item);
+ void visit (EnumItemStruct &item);
+ void visit (EnumItemDiscriminant &item);
+ void visit (Enum &enum_item);
+ void visit (Union &union_item);
+ void visit (ConstantItem &const_item);
+ void visit (StaticItem &static_item);
+ void visit (TraitItemFunc &item);
+ void visit (TraitItemMethod &item);
+ void visit (TraitItemConst &item);
+ void visit (TraitItemType &item);
+ void visit (Trait &trait);
+ void visit (InherentImpl &impl);
+ void visit (TraitImpl &impl);
+ void visit (ExternalStaticItem &item);
+ void visit (ExternalFunctionItem &item);
+ void visit (ExternBlock &block);
+
+ // rust-macro.h
+ void visit (MacroMatchFragment &match);
+ void visit (MacroMatchRepetition &match);
+ void visit (MacroMatcher &matcher);
+ void visit (MacroRulesDefinition &rules_def);
+ void visit (MacroInvocation &macro_invoc);
+ void visit (MetaItemPath &meta_item);
+ void visit (MetaItemSeq &meta_item);
+ void visit (MetaWord &meta_item);
+ void visit (MetaNameValueStr &meta_item);
+ void visit (MetaListPaths &meta_item);
+ void visit (MetaListNameValueStr &meta_item);
+
+ // rust-pattern.h
+ void visit (LiteralPattern &pattern);
+ void visit (IdentifierPattern &pattern);
+ void visit (WildcardPattern &pattern);
+ // void visit(RangePatternBound& bound);
+ void visit (RangePatternBoundLiteral &bound);
+ void visit (RangePatternBoundPath &bound);
+ void visit (RangePatternBoundQualPath &bound);
+ void visit (RangePattern &pattern);
+ void visit (ReferencePattern &pattern);
+ // void visit(StructPatternField& field);
+ void visit (StructPatternFieldTuplePat &field);
+ void visit (StructPatternFieldIdentPat &field);
+ void visit (StructPatternFieldIdent &field);
+ void visit (StructPattern &pattern);
+ // void visit(TupleStructItems& tuple_items);
+ void visit (TupleStructItemsNoRange &tuple_items);
+ void visit (TupleStructItemsRange &tuple_items);
+ void visit (TupleStructPattern &pattern);
+ // void visit(TuplePatternItems& tuple_items);
+ void visit (TuplePatternItemsMultiple &tuple_items);
+ void visit (TuplePatternItemsRanged &tuple_items);
+ void visit (TuplePattern &pattern);
+ void visit (GroupedPattern &pattern);
+ void visit (SlicePattern &pattern);
+
+ // rust-stmt.h
+ void visit (EmptyStmt &stmt);
+ void visit (LetStmt &stmt);
+ void visit (ExprStmtWithoutBlock &stmt);
+ void visit (ExprStmtWithBlock &stmt);
+
+ // rust-type.h
+ void visit (TraitBound &bound);
+ void visit (ImplTraitType &type);
+ void visit (TraitObjectType &type);
+ void visit (ParenthesisedType &type);
+ void visit (ImplTraitTypeOneBound &type);
+ void visit (TraitObjectTypeOneBound &type);
+ void visit (TupleType &type);
+ void visit (NeverType &type);
+ void visit (RawPointerType &type);
+ void visit (ReferenceType &type);
+ void visit (ArrayType &type);
+ void visit (SliceType &type);
+ void visit (InferredType &type);
+ void visit (BareFunctionType &type);
+};
+
+} // namespace AST
+} // namespace Rust
+
+#endif // !RUST_AST_DUMP_H