public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] ast: Dump Traits properly
@ 2022-06-08 12:52 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:52 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:4d021b8efb6cdddd6fc860738f0b6c7ab2022fe7

commit 4d021b8efb6cdddd6fc860738f0b6c7ab2022fe7
Author: Arthur Cohen <arthur.cohen@embecosm.com>
Date:   Thu Jun 2 17:53:30 2022 +0200

    ast: Dump Traits properly

Diff:
---
 gcc/rust/ast/rust-ast-dump.cc | 76 ++++++++++++++++++++++++++++++++++++++++---
 gcc/rust/ast/rust-ast-dump.h  |  4 +++
 gcc/rust/ast/rust-item.h      | 24 +++-----------
 3 files changed, 79 insertions(+), 25 deletions(-)

diff --git a/gcc/rust/ast/rust-ast-dump.cc b/gcc/rust/ast/rust-ast-dump.cc
index c23720b59d1..cdcb5632cc7 100644
--- a/gcc/rust/ast/rust-ast-dump.cc
+++ b/gcc/rust/ast/rust-ast-dump.cc
@@ -514,25 +514,91 @@ void
 Dump::visit (StaticItem &static_item)
 {}
 
+void
+Dump::format_function_common (std::unique_ptr<Type> &return_type,
+			      std::unique_ptr<BlockExpr> &block)
+{
+  if (return_type)
+    {
+      stream << "-> ";
+      return_type->accept_vis (*this);
+    }
+
+  if (block)
+    {
+      if (return_type)
+	stream << ' ';
+      block->accept_vis (*this);
+    }
+  else
+    stream << ";\n";
+}
+
 void
 Dump::visit (TraitItemFunc &item)
-{}
+{
+  auto func = item.get_trait_function_decl ();
+  stream << indentation << "fn " << func.get_identifier () << '(';
+
+  auto &params = func.get_function_params ();
+  for (auto &param : params)
+    {
+      stream << ", ";
+      format_function_param (param);
+    }
+
+  stream << ") ";
+
+  format_function_common (func.get_return_type (), item.get_definition ());
+}
 
 void
 Dump::visit (TraitItemMethod &item)
-{}
+{
+  auto method = item.get_trait_method_decl ();
+  stream << indentation << "fn " << method.get_identifier () << '(';
+
+  auto &self = method.get_self_param ();
+  stream << self.as_string ();
+
+  auto &params = method.get_function_params ();
+  for (auto &param : params)
+    {
+      stream << ", ";
+      format_function_param (param);
+    }
+
+  stream << ") ";
+
+  format_function_common (method.get_return_type (), item.get_definition ());
+}
 
 void
 Dump::visit (TraitItemConst &item)
-{}
+{
+  stream << indentation << "const " << item.get_identifier () << ": ";
+  item.get_type ()->accept_vis (*this);
+  stream << ";\n";
+}
 
 void
 Dump::visit (TraitItemType &item)
-{}
+{
+  stream << indentation << "type " << item.get_identifier () << ";\n";
+}
 
 void
 Dump::visit (Trait &trait)
-{}
+{
+  stream << "trait " << trait.get_identifier () << " {\n";
+  indentation.increment ();
+
+  for (auto &item : trait.get_trait_items ())
+    item->accept_vis (*this);
+
+  indentation.decrement ();
+  stream << "\n}\n";
+}
 
 void
 Dump::visit (InherentImpl &impl)
diff --git a/gcc/rust/ast/rust-ast-dump.h b/gcc/rust/ast/rust-ast-dump.h
index 436c2b523dd..e6c6ca4bc6f 100644
--- a/gcc/rust/ast/rust-ast-dump.h
+++ b/gcc/rust/ast/rust-ast-dump.h
@@ -55,6 +55,10 @@ private:
   std::ostream &stream;
   Indent indentation;
 
+  // Format together common items of functions: Parameters, return type, block
+  void format_function_common (std::unique_ptr<Type> &return_type,
+			       std::unique_ptr<BlockExpr> &block);
+
   /**
    * Format a function's definition parameter
    */
diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h
index 4bed5af51ee..6d953fb128c 100644
--- a/gcc/rust/ast/rust-item.h
+++ b/gcc/rust/ast/rust-item.h
@@ -2872,11 +2872,7 @@ public:
   }
 
   // TODO: is this better? Or is a "vis_block" better?
-  std::unique_ptr<Type> &get_return_type ()
-  {
-    rust_assert (has_return_type ());
-    return return_type;
-  }
+  std::unique_ptr<Type> &get_return_type () { return return_type; }
 
   // TODO: is this better? Or is a "vis_block" better?
   WhereClause &get_where_clause () { return where_clause; }
@@ -2954,11 +2950,7 @@ public:
   const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
 
   // TODO: is this better? Or is a "vis_block" better?
-  std::unique_ptr<BlockExpr> &get_definition ()
-  {
-    rust_assert (has_definition ());
-    return block_expr;
-  }
+  std::unique_ptr<BlockExpr> &get_definition () { return block_expr; }
 
   // TODO: is this better? Or is a "vis_block" better?
   TraitFunctionDecl &get_trait_function_decl ()
@@ -3097,11 +3089,7 @@ public:
   }
 
   // TODO: is this better? Or is a "vis_block" better?
-  std::unique_ptr<Type> &get_return_type ()
-  {
-    rust_assert (has_return_type ());
-    return return_type;
-  }
+  std::unique_ptr<Type> &get_return_type () { return return_type; }
 
   // TODO: is this better? Or is a "vis_block" better?
   WhereClause &get_where_clause () { return where_clause; }
@@ -3189,11 +3177,7 @@ public:
   }
 
   // TODO: is this better? Or is a "vis_block" better?
-  std::unique_ptr<BlockExpr> &get_definition ()
-  {
-    rust_assert (has_definition ());
-    return block_expr;
-  }
+  std::unique_ptr<BlockExpr> &get_definition () { return block_expr; }
 
 protected:
   // Clone function implementation as (not pure) virtual method


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-06-08 12:52 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:52 [gcc/devel/rust/master] ast: Dump Traits properly 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).