aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>2023-06-05 11:24:35 +0200
committerArthur Cohen <arthur.cohen@embecosm.com>2023-06-08 14:32:35 +0200
commit588b130c7cc9ebdf2bcbf23856f11ebf39625375 (patch)
tree0679f3c9bc88669be674a6a664a41c983eb60c31
parentb21aa448a4595208415e269aa1080846064029c8 (diff)
downloadgcc-588b130c7cc9ebdf2bcbf23856f11ebf39625375.zip
gcc-588b130c7cc9ebdf2bcbf23856f11ebf39625375.tar.gz
gcc-588b130c7cc9ebdf2bcbf23856f11ebf39625375.tar.bz2
expand: Expand item level attribute proc macros
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>
-rw-r--r--gcc/rust/expand/rust-expand-visitor.cc44
-rw-r--r--gcc/rust/expand/rust-macro-expand.h4
2 files changed, 44 insertions, 4 deletions
diff --git a/gcc/rust/expand/rust-expand-visitor.cc b/gcc/rust/expand/rust-expand-visitor.cc
index b63e3ba..927d922 100644
--- a/gcc/rust/expand/rust-expand-visitor.cc
+++ b/gcc/rust/expand/rust-expand-visitor.cc
@@ -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;
+ }
}
}
}
diff --git a/gcc/rust/expand/rust-macro-expand.h b/gcc/rust/expand/rust-macro-expand.h
index 524e9b6..7c79c46 100644
--- a/gcc/rust/expand/rust-macro-expand.h
+++ b/gcc/rust/expand/rust-macro-expand.h
@@ -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)));
}