From 588b130c7cc9ebdf2bcbf23856f11ebf39625375 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Mon, 5 Jun 2023 11:24:35 +0200 Subject: 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 --- gcc/rust/expand/rust-expand-visitor.cc | 44 ++++++++++++++++++++++++++++++++-- gcc/rust/expand/rust-macro-expand.h | 4 ++-- 2 files changed, 44 insertions(+), 4 deletions(-) (limited to 'gcc') 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 &item, std::string &to_derive, return result; } +static std::vector> +expand_attribute (std::unique_ptr &item, AST::SimplePath &name, + MacroExpander &expander) +{ + std::vector> 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> &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 - 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 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))); } -- cgit v1.1