]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: expand: Expand item level attribute proc macros
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Mon, 5 Jun 2023 09:24:35 +0000 (11:24 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:46:26 +0000 (18:46 +0100)
Expand custom attributes from procedural macros at item level.

gcc/rust/ChangeLog:

* expand/rust-expand-visitor.cc (expand_attribute): Add function
to expand a given attribute on a given item.
* expand/rust-macro-expand.h (struct MacroExpander): Change
return type to ast fragment.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/expand/rust-expand-visitor.cc
gcc/rust/expand/rust-macro-expand.h

index b63e3ba82c947d37914edb20e883d71ce29c6b17..927d9225049ee45d7e58457359268a03b91778a4 100644 (file)
@@ -126,6 +126,29 @@ derive_item (std::unique_ptr<AST::Item> &item, std::string &to_derive,
   return result;
 }
 
+static std::vector<std::unique_ptr<AST::Item>>
+expand_attribute (std::unique_ptr<AST::Item> &item, AST::SimplePath &name,
+                 MacroExpander &expander)
+{
+  std::vector<std::unique_ptr<AST::Item>> result;
+  auto frag = expander.expand_attribute_proc_macro (item, name);
+  if (!frag.is_error ())
+    {
+      for (auto &node : frag.get_nodes ())
+       {
+         switch (node.get_kind ())
+           {
+           case AST::SingleASTNode::ITEM:
+             result.push_back (node.take_item ());
+             break;
+           default:
+             gcc_unreachable ();
+           }
+       }
+    }
+  return result;
+}
+
 void
 ExpandVisitor::expand_inner_items (
   std::vector<std::unique_ptr<AST::Item>> &items)
@@ -173,8 +196,25 @@ ExpandVisitor::expand_inner_items (
                }
              else /* Attribute */
                {
-                 // Ignore for now
-                 attr_it++;
+                 if (is_builtin (current))
+                   {
+                     attr_it++;
+                   }
+                 else
+                   {
+                     attr_it = attrs.erase (attr_it);
+                     auto new_items
+                       = expand_attribute (item, current.get_path (),
+                                           expander);
+                     it = items.erase (it);
+                     std::move (new_items.begin (), new_items.end (),
+                                std::inserter (items, it));
+                     // TODO: Improve this ?
+                     // item is invalid since it refers to now deleted,
+                     // cancel the loop increment and break.
+                     it--;
+                     break;
+                   }
                }
            }
        }
index 676a9d14790ed714ed9299533517a15651d0369e..0e53913438a79a933594d3115595b285b1c94d9c 100644 (file)
@@ -406,7 +406,7 @@ struct MacroExpander
   }
 
   template <typename T>
-  void expand_attribute_proc_macro (T &item, AST::SimplePath &path)
+  AST::Fragment expand_attribute_proc_macro (T &item, AST::SimplePath &path)
   {
     ProcMacro::Attribute macro;
 
@@ -436,7 +436,7 @@ struct MacroExpander
     std::vector<const_TokenPtr> vec (c.cbegin (), c.cend ());
 
     // FIXME: Handle attributes
-    parse_proc_macro_output (
+    return parse_proc_macro_output (
       macro.macro (ProcMacro::TokenStream::make_tokenstream (), convert (vec)));
   }