From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 724E5388B5AF; Wed, 8 Jun 2022 12:51:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 724E5388B5AF Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] ast: Dump: Add pretty printing of functions X-Act-Checkin: gcc X-Git-Author: Arthur Cohen X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: c2c26f0499b5faece247c21e3bad7d5d5f5cb31a X-Git-Newrev: 9a1ae92194805c055c7b5c7b3b7b26c3b2da8365 Message-Id: <20220608125110.724E5388B5AF@sourceware.org> Date: Wed, 8 Jun 2022 12:51:10 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Jun 2022 12:51:10 -0000 https://gcc.gnu.org/g:9a1ae92194805c055c7b5c7b3b7b26c3b2da8365 commit 9a1ae92194805c055c7b5c7b3b7b26c3b2da8365 Author: Arthur Cohen Date: Mon May 30 11:05:14 2022 +0200 ast: Dump: Add pretty printing of functions Diff: --- gcc/rust/ast/rust-ast-dump.cc | 88 +++++++++++++++++++++++++++++++++++++++---- gcc/rust/ast/rust-ast-dump.h | 12 ++++-- 2 files changed, 89 insertions(+), 11 deletions(-) diff --git a/gcc/rust/ast/rust-ast-dump.cc b/gcc/rust/ast/rust-ast-dump.cc index 99b6e86523c..1afb988b674 100644 --- a/gcc/rust/ast/rust-ast-dump.cc +++ b/gcc/rust/ast/rust-ast-dump.cc @@ -23,10 +23,10 @@ namespace AST { Indent::Indent () : tabs (0) {} -std::ofstream & -Indent::operator<< (std::ofstream &stream) +std::ostream & +operator<< (std::ostream &stream, const Indent &indent) { - for (size_t i = 0; i < tabs; i++) + for (size_t i = 0; i < indent.tabs; i++) stream << '\t'; return stream; @@ -45,7 +45,7 @@ Indent::decrement () tabs--; } -Dump::Dump (std::ofstream &stream) : stream (stream), indentation (Indent ()) {} +Dump::Dump (std::ostream &stream) : stream (stream), indentation (Indent ()) {} void Dump::go (AST::Crate &crate) @@ -54,6 +54,14 @@ Dump::go (AST::Crate &crate) item->accept_vis (*this); } +void +Dump::format_function_param (FunctionParam ¶m) +{ + param.get_pattern ()->accept_vis (*this); + stream << ": "; + param.get_type ()->accept_vis (*this); +} + void Dump::visit (Token &tok) {} @@ -234,7 +242,23 @@ Dump::visit (ClosureExprInner &expr) void Dump::visit (BlockExpr &expr) -{} +{ + stream << "{\n"; + indentation.increment (); + + for (auto &stmt : expr.get_statements ()) + { + stream << indentation; + stmt->accept_vis (*this); + stream << ";\n"; + } + + if (expr.has_tail_expr ()) + expr.get_tail_expr ()->accept_vis (*this); + + stream << "\n}\n"; + indentation.increment (); +} void Dump::visit (ClosureExprInnerTyped &expr) @@ -383,7 +407,36 @@ Dump::visit (UseDeclaration &use_decl) void Dump::visit (Function &function) -{} +{ + stream << "fn " << function.get_function_name () << '('; + + auto ¶ms = function.get_function_params (); + if (params.size () >= 1) + { + format_function_param (params[0]); + for (size_t i = 1; i < params.size (); i++) + { + stream << ", "; + format_function_param (params[i]); + } + } + + stream << ") "; + + if (function.has_return_type ()) + { + stream << "-> "; + function.get_return_type ()->accept_vis (*this); + } + + auto &block = function.get_definition (); + if (!block) + stream << ';'; + else + block->accept_vis (*this); + + stream << '\n'; +} void Dump::visit (TypeAlias &type_alias) @@ -521,7 +574,9 @@ Dump::visit (LiteralPattern &pattern) void Dump::visit (IdentifierPattern &pattern) -{} +{ + stream << pattern.get_ident (); +} void Dump::visit (WildcardPattern &pattern) @@ -610,7 +665,24 @@ Dump::visit (EmptyStmt &stmt) void Dump::visit (LetStmt &stmt) -{} +{ + stream << "let "; + auto &pattern = stmt.get_pattern (); + if (pattern) + pattern->accept_vis (*this); + + if (stmt.has_type ()) + { + stream << ": "; + stmt.get_type ()->accept_vis (*this); + } + + if (stmt.has_init_expr ()) + { + stream << " = "; + stmt.get_init_expr ()->accept_vis (*this); + } +} void Dump::visit (ExprStmtWithoutBlock &stmt) diff --git a/gcc/rust/ast/rust-ast-dump.h b/gcc/rust/ast/rust-ast-dump.h index 99568c65e36..436c2b523dd 100644 --- a/gcc/rust/ast/rust-ast-dump.h +++ b/gcc/rust/ast/rust-ast-dump.h @@ -18,6 +18,7 @@ #include "rust-ast-visitor.h" #include "rust-ast.h" +#include "rust-ast-full.h" #ifndef RUST_AST_DUMP_H #define RUST_AST_DUMP_H @@ -31,7 +32,7 @@ class Indent public: Indent (); - std::ofstream &operator<< (std::ofstream &stream); + friend std::ostream &operator<< (std::ostream &stream, const Indent &indent); void increment (); void decrement (); @@ -43,7 +44,7 @@ private: class Dump : public ASTVisitor { public: - Dump (std::ofstream &stream); + Dump (std::ostream &stream); /** * Run the visitor on an entire crate and its items @@ -51,9 +52,14 @@ public: void go (AST::Crate &crate); private: - std::ofstream &stream; + std::ostream &stream; Indent indentation; + /** + * Format a function's definition parameter + */ + void format_function_param (FunctionParam ¶m); + // rust-ast.h void visit (Token &tok); void visit (DelimTokenTree &delim_tok_tree);