[gcc r14-7452] gccrs: expand: Add derive expansion stubs

Arthur Cohen cohenarthur@gcc.gnu.org
Tue Jan 16 17:35:58 GMT 2024


https://gcc.gnu.org/g:36b22e639078d8ca5db5ee0d3002a174a048e813

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

    gccrs: 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;
 };


More information about the Gcc-cvs mailing list