public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] expand: Add derive expansion stubs
@ 2023-04-06 21:37 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2023-04-06 21:37 UTC (permalink / raw)
  To: gcc-cvs

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

commit b109e685aae985e8feef0bd14728ba580c731d2f
Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Date:   Thu Mar 30 16:28:12 2023 +0200

    expand: Add derive expansion stubs
    
    Add derive macros expansion stub functions.
    
    gcc/rust/ChangeLog:
    
            * expand/rust-expand-visitor.cc (ExpandVisitor::visit): Add call
            to derive expander.
            (ExpandVisitor::expand_derive): Expand a single derive.
            (ExpandVisitor::visit_attrs_with_derive): Visit an item with
            derive attributes.
            (ExpandVisitor::is_derive): Identify a derive attribute.
            * expand/rust-expand-visitor.h: Add function prototypes.
    
    Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>

Diff:
---
 gcc/rust/expand/rust-expand-visitor.cc | 63 ++++++++++++++++++++++++++++++++++
 gcc/rust/expand/rust-expand-visitor.h  | 10 ++++++
 2 files changed, 73 insertions(+)

diff --git a/gcc/rust/expand/rust-expand-visitor.cc b/gcc/rust/expand/rust-expand-visitor.cc
index 2a5dc79f8dd..96619482467 100644
--- a/gcc/rust/expand/rust-expand-visitor.cc
+++ b/gcc/rust/expand/rust-expand-visitor.cc
@@ -812,6 +812,7 @@ ExpandVisitor::visit (AST::TypeAlias &type_alias)
 void
 ExpandVisitor::visit (AST::StructStruct &struct_item)
 {
+  visit_attrs_with_derive (struct_item);
   for (auto &generic : struct_item.get_generic_params ())
     visit (generic);
 
@@ -824,6 +825,7 @@ ExpandVisitor::visit (AST::StructStruct &struct_item)
 void
 ExpandVisitor::visit (AST::TupleStruct &tuple_struct)
 {
+  visit_attrs_with_derive (tuple_struct);
   for (auto &generic : tuple_struct.get_generic_params ())
     visit (generic);
 
@@ -858,6 +860,7 @@ ExpandVisitor::visit (AST::EnumItemDiscriminant &item)
 void
 ExpandVisitor::visit (AST::Enum &enum_item)
 {
+  visit_attrs_with_derive (enum_item);
   for (auto &generic : enum_item.get_generic_params ())
     visit (generic);
 
@@ -868,6 +871,7 @@ ExpandVisitor::visit (AST::Enum &enum_item)
 void
 ExpandVisitor::visit (AST::Union &union_item)
 {
+  visit_attrs_with_derive (union_item);
   for (auto &generic : union_item.get_generic_params ())
     visit (generic);
 
@@ -1337,4 +1341,63 @@ ExpandVisitor::visit (AST::BareFunctionType &type)
     visit (type.get_return_type ());
 }
 
+template <typename T>
+void
+ExpandVisitor::expand_derive (const T &item,
+			      std::unique_ptr<AST::TokenTree> &trait)
+{
+  // FIXME: Implement expansion for that particular trait
+}
+
+template <typename T>
+void
+ExpandVisitor::expand_derive (const T &item, AST::DelimTokenTree &attr)
+{
+  // Item is const because even though the tokenstream might be modified, it
+  // should appear as the same input for every derive proc macro.
+  auto &trees = attr.get_token_trees ();
+  if (trees.size () > 2)
+    {
+      // Skipping begin and end parenthesis
+      for (auto it = trees.begin () + 1; it < trees.end () - 1;
+	   it += 2 /* Increment + skip comma */)
+	{
+	  expand_derive (item, *it);
+	}
+    }
+}
+
+template <typename T>
+void
+ExpandVisitor::visit_attrs_with_derive (T &item)
+{
+  auto &attrs = item.get_outer_attrs ();
+  for (auto it = attrs.begin (); it != attrs.end (); /* erase => No increment*/)
+    {
+      auto current = *it;
+
+      if (is_derive (current))
+	{
+	  it = attrs.erase (it);
+	  // Downcasting checked in is_derive
+	  expand_derive (item, static_cast<AST::DelimTokenTree &> (
+				 current.get_attr_input ()));
+	}
+      else // Skip unknwown
+	{
+	  it++;
+	}
+    }
+}
+
+bool
+ExpandVisitor::is_derive (AST::Attribute &attr)
+{
+  auto &segments = attr.get_path ().get_segments ();
+  return attr.has_attr_input ()
+	 && attr.get_attr_input ().get_attr_input_type ()
+	      == AST::AttrInput::TOKEN_TREE
+	 && !segments.empty () && "derive" == segments[0].get_segment_name ();
+}
+
 } // namespace Rust
diff --git a/gcc/rust/expand/rust-expand-visitor.h b/gcc/rust/expand/rust-expand-visitor.h
index e832d4a06c0..613251dfbbe 100644
--- a/gcc/rust/expand/rust-expand-visitor.h
+++ b/gcc/rust/expand/rust-expand-visitor.h
@@ -313,6 +313,16 @@ public:
   void visit (AST::InferredType &) override;
   void visit (AST::BareFunctionType &type) override;
 
+  bool is_derive (AST::Attribute &attr);
+
+  template <typename T>
+  void expand_derive (const T &item, std::unique_ptr<AST::TokenTree> &trait);
+
+  template <typename T>
+  void expand_derive (const T &item, AST::DelimTokenTree &attr);
+
+  template <typename T> void visit_attrs_with_derive (T &item);
+
 private:
   MacroExpander &expander;
 };

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

only message in thread, other threads:[~2023-04-06 21:37 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-06 21:37 [gcc/devel/rust/master] expand: Add derive expansion stubs 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).