public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] ast: Dump: Add pretty printing of functions
@ 2022-06-08 12:51 Thomas Schwinge
0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:51 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:9a1ae92194805c055c7b5c7b3b7b26c3b2da8365
commit 9a1ae92194805c055c7b5c7b3b7b26c3b2da8365
Author: Arthur Cohen <arthur.cohen@embecosm.com>
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);
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2022-06-08 12:51 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 12:51 [gcc/devel/rust/master] ast: Dump: Add pretty printing of functions Thomas Schwinge
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).