public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] Add support for ast with generic traits
@ 2022-07-15 21:38 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-07-15 21:38 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:ecd1908fc20a18631725087abe3c48383bdc992a

commit ecd1908fc20a18631725087abe3c48383bdc992a
Author: Philip Herron <philip.herron@embecosm.com>
Date:   Fri Jul 15 15:57:03 2022 +0100

    Add support for ast with generic traits
    
    This allows AST dump of attributes on Traits this code needs to be applied
    for all attribute dumps. Traits also have an implicit Self generic param
    this ensure we ignore this during an AST dump.

Diff:
---
 gcc/rust/ast/rust-ast-dump.cc | 69 +++++++++++++++++++++++++++++++++++++++++--
 gcc/rust/ast/rust-ast-dump.h  |  1 +
 2 files changed, 68 insertions(+), 2 deletions(-)

diff --git a/gcc/rust/ast/rust-ast-dump.cc b/gcc/rust/ast/rust-ast-dump.cc
index 3b02d84c362..ad9ad0b7de7 100644
--- a/gcc/rust/ast/rust-ast-dump.cc
+++ b/gcc/rust/ast/rust-ast-dump.cc
@@ -69,6 +69,45 @@ Dump::format_function_param (FunctionParam &param)
   param.get_type ()->accept_vis (*this);
 }
 
+void
+Dump::emit_attrib (const Attribute &attrib)
+{
+  stream << "#";
+  stream << "[";
+
+  for (size_t i = 0; i < attrib.get_path ().get_segments ().size (); i++)
+    {
+      const auto &seg = attrib.get_path ().get_segments ().at (i);
+      bool has_next = (i + 1) < attrib.get_path ().get_segments ().size ();
+
+      stream << seg.get_segment_name ();
+      if (has_next)
+	stream << "::";
+    }
+
+  if (attrib.has_attr_input ())
+    {
+      stream << " = ";
+
+      bool is_literal = attrib.get_attr_input ().get_attr_input_type ()
+			== AST::AttrInput::AttrInputType::LITERAL;
+      if (is_literal)
+	{
+	  auto &literal
+	    = static_cast<AST::AttrInputLiteral &> (attrib.get_attr_input ());
+	  const auto &value = literal.get_literal ().as_string ();
+
+	  stream << "\"" << value << "\"";
+	}
+      else
+	{
+	  stream << "FIXME";
+	}
+    }
+
+  stream << "]";
+}
+
 void
 Dump::visit (Token &tok)
 {}
@@ -440,7 +479,7 @@ Dump::visit (TypeParam &param)
   stream << param.get_type_representation ();
   if (param.has_type ())
     {
-      stream << ": ";
+      stream << " = ";
       param.get_type ()->accept_vis (*this);
     }
 }
@@ -680,7 +719,33 @@ Dump::visit (TraitItemType &item)
 void
 Dump::visit (Trait &trait)
 {
-  stream << "trait " << trait.get_identifier () << " {\n";
+  for (const auto &attr : trait.get_outer_attrs ())
+    {
+      emit_attrib (attr);
+      stream << "\n" << indentation;
+    }
+
+  stream << "trait " << trait.get_identifier ();
+
+  // Traits actually have an implicit Self thrown at the start so we must expect
+  // the number of generic params to be > 1
+  if (trait.get_generic_params ().size () > 1)
+    {
+      stream << "<";
+      for (size_t i = 1; i < trait.get_generic_params ().size (); i++)
+	{
+	  auto &param = trait.get_generic_params ().at (i);
+	  param->accept_vis (*this);
+
+	  bool has_next = (i + 1) < trait.get_generic_params ().size ();
+	  if (has_next)
+	    stream << ", ";
+	}
+      stream << ">";
+    }
+
+  stream << " {\n";
+
   indentation.increment ();
 
   for (auto &item : trait.get_trait_items ())
diff --git a/gcc/rust/ast/rust-ast-dump.h b/gcc/rust/ast/rust-ast-dump.h
index d74d88759b0..c3854e8287d 100644
--- a/gcc/rust/ast/rust-ast-dump.h
+++ b/gcc/rust/ast/rust-ast-dump.h
@@ -64,6 +64,7 @@ private:
    * Format a function's definition parameter
    */
   void format_function_param (FunctionParam &param);
+  void emit_attrib (const Attribute &attrib);
 
   // rust-ast.h
   void visit (Token &tok);


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

only message in thread, other threads:[~2022-07-15 21:38 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-15 21:38 [gcc/devel/rust/master] Add support for ast with generic traits 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).