public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Thomas Schwinge <tschwinge@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc/devel/rust/master] ast: Dump: Add pretty printing of functions
Date: Wed,  8 Jun 2022 12:51:10 +0000 (GMT)	[thread overview]
Message-ID: <20220608125110.724E5388B5AF@sourceware.org> (raw)

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 &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 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 &param);
+
   // rust-ast.h
   void visit (Token &tok);
   void visit (DelimTokenTree &delim_tok_tree);


                 reply	other threads:[~2022-06-08 12:51 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220608125110.724E5388B5AF@sourceware.org \
    --to=tschwinge@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).