aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorArthur Cohen <arthur.cohen@embecosm.com>2022-05-30 11:05:14 +0200
committerArthur Cohen <arthur.cohen@embecosm.com>2022-05-30 11:05:14 +0200
commit9a1ae92194805c055c7b5c7b3b7b26c3b2da8365 (patch)
treeb034c14ab3141ec63c71697e938c2b04641568e1 /gcc
parentc2c26f0499b5faece247c21e3bad7d5d5f5cb31a (diff)
downloadgcc-9a1ae92194805c055c7b5c7b3b7b26c3b2da8365.zip
gcc-9a1ae92194805c055c7b5c7b3b7b26c3b2da8365.tar.gz
gcc-9a1ae92194805c055c7b5c7b3b7b26c3b2da8365.tar.bz2
ast: Dump: Add pretty printing of functions
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/ast/rust-ast-dump.cc88
-rw-r--r--gcc/rust/ast/rust-ast-dump.h12
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 99b6e86..1afb988 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)
@@ -55,6 +55,14 @@ Dump::go (AST::Crate &crate)
}
void
+Dump::format_function_param (FunctionParam &param)
+{
+ 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 &params = 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 99568c6..436c2b5 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 &param);
+
// rust-ast.h
void visit (Token &tok);
void visit (DelimTokenTree &delim_tok_tree);