]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: expand: Add derive expansion stubs
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Thu, 30 Mar 2023 14:28:12 +0000 (16:28 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:28:44 +0000 (18:28 +0100)
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>
gcc/rust/expand/rust-expand-visitor.cc
gcc/rust/expand/rust-expand-visitor.h

index 2a5dc79f8dd407ca4a3c0bccc470939ac4ce6f4d..96619482467815855c0c05b1f548aa5bdcc603d1 100644 (file)
@@ -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
index e832d4a06c03d1c74e0fbbef9e067c259635ed96..613251dfbbea806367289c7941ab9b83c873dad2 100644 (file)
@@ -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;
 };