public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] macros: Allow macro calls in trait implementations
@ 2022-06-08 12:21 Thomas Schwinge
0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:21 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:a7ef6f98be0e25187ad1690428aafc17e19b5751
commit a7ef6f98be0e25187ad1690428aafc17e19b5751
Author: Arthur Cohen <arthur.cohen@embecosm.com>
Date: Wed Mar 16 16:57:17 2022 +0100
macros: Allow macro calls in trait implementations
Just like inherent implementation blocks, trait implementation blocks
(`impl Trait for Type`) can also contain macro invocations.
Diff:
---
gcc/rust/ast/rust-ast.h | 28 ++++++++++++++++++++++++++
gcc/rust/expand/rust-attribute-visitor.cc | 8 ++++++--
gcc/rust/expand/rust-macro-expand.cc | 19 +++++++++++++++++
gcc/rust/expand/rust-macro-expand.h | 3 ++-
gcc/rust/parse/rust-parse.h | 2 +-
gcc/testsuite/rust/execute/torture/macros23.rs | 19 +++++++++++++++++
6 files changed, 75 insertions(+), 4 deletions(-)
diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h
index e33fff2d988..a22c2d1ad1d 100644
--- a/gcc/rust/ast/rust-ast.h
+++ b/gcc/rust/ast/rust-ast.h
@@ -1514,6 +1514,7 @@ public:
EXTERN,
TRAIT,
IMPL,
+ TRAIT_IMPL,
};
private:
@@ -1526,6 +1527,7 @@ private:
std::unique_ptr<ExternalItem> external_item;
std::unique_ptr<TraitItem> trait_item;
std::unique_ptr<InherentImplItem> impl_item;
+ std::unique_ptr<TraitImplItem> trait_impl_item;
public:
SingleASTNode (std::unique_ptr<Expr> expr)
@@ -1552,6 +1554,10 @@ public:
: kind (IMPL), impl_item (std::move (item))
{}
+ SingleASTNode (std::unique_ptr<TraitImplItem> trait_impl_item)
+ : kind (TRAIT_IMPL), trait_impl_item (std::move (trait_impl_item))
+ {}
+
SingleASTNode (SingleASTNode const &other)
{
kind = other.kind;
@@ -1580,6 +1586,10 @@ public:
case IMPL:
impl_item = other.impl_item->clone_inherent_impl_item ();
break;
+
+ case TRAIT_IMPL:
+ trait_impl_item = other.trait_impl_item->clone_trait_impl_item ();
+ break;
}
}
@@ -1611,6 +1621,10 @@ public:
case IMPL:
impl_item = other.impl_item->clone_inherent_impl_item ();
break;
+
+ case TRAIT_IMPL:
+ trait_impl_item = other.trait_impl_item->clone_trait_impl_item ();
+ break;
}
return *this;
}
@@ -1679,6 +1693,12 @@ public:
return std::move (impl_item);
}
+ std::unique_ptr<TraitImplItem> take_trait_impl_item ()
+ {
+ rust_assert (!is_error ());
+ return std::move (trait_impl_item);
+ }
+
void accept_vis (ASTVisitor &vis)
{
switch (kind)
@@ -1706,6 +1726,10 @@ public:
case IMPL:
impl_item->accept_vis (vis);
break;
+
+ case TRAIT_IMPL:
+ trait_impl_item->accept_vis (vis);
+ break;
}
}
@@ -1725,6 +1749,8 @@ public:
return trait_item == nullptr;
case IMPL:
return impl_item == nullptr;
+ case TRAIT_IMPL:
+ return trait_impl_item == nullptr;
}
gcc_unreachable ();
@@ -1747,6 +1773,8 @@ public:
return "Trait Item: " + trait_item->as_string ();
case IMPL:
return "Impl Item: " + impl_item->as_string ();
+ case TRAIT_IMPL:
+ return "Trait Impl Item: " + impl_item->as_string ();
}
gcc_unreachable ();
diff --git a/gcc/rust/expand/rust-attribute-visitor.cc b/gcc/rust/expand/rust-attribute-visitor.cc
index 50821edcfae..3de660897ca 100644
--- a/gcc/rust/expand/rust-attribute-visitor.cc
+++ b/gcc/rust/expand/rust-attribute-visitor.cc
@@ -2549,8 +2549,12 @@ AttrVisitor::visit (AST::TraitImpl &impl)
if (impl.has_where_clause ())
expand_where_clause (impl.get_where_clause ());
- // strip trait impl items if required
- expand_pointer_allow_strip (impl.get_impl_items ());
+ std::function<std::unique_ptr<AST::TraitImplItem> (AST::SingleASTNode)>
+ extractor
+ = [] (AST::SingleASTNode node) { return node.take_trait_impl_item (); };
+
+ expand_macro_children (MacroExpander::TRAIT_IMPL, impl.get_impl_items (),
+ extractor);
}
void
AttrVisitor::visit (AST::ExternalStaticItem &item)
diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc
index 80f822b3e53..3bdb8c685e6 100644
--- a/gcc/rust/expand/rust-macro-expand.cc
+++ b/gcc/rust/expand/rust-macro-expand.cc
@@ -859,6 +859,22 @@ transcribe_many_impl_items (Parser<MacroInvocLexer> &parser, TokenId &delimiter)
});
}
+/**
+ * Transcribe 0 or more trait impl items from a macro invocation
+ *
+ * @param parser Parser to extract items from
+ * @param delimiter Id of the token on which parsing should stop
+ */
+static std::vector<AST::SingleASTNode>
+transcribe_many_trait_impl_items (Parser<MacroInvocLexer> &parser,
+ TokenId &delimiter)
+{
+ return parse_many (parser, delimiter, [&parser] () {
+ auto item = parser.parse_trait_impl_item ();
+ return AST::SingleASTNode (std::move (item));
+ });
+}
+
/**
* Transcribe 0 or more statements from a macro invocation
*
@@ -932,6 +948,9 @@ transcribe_context (MacroExpander::ContextType ctx,
case MacroExpander::ContextType::IMPL:
return transcribe_many_impl_items (parser, last_token_id);
break;
+ case MacroExpander::ContextType::TRAIT_IMPL:
+ return transcribe_many_trait_impl_items (parser, last_token_id);
+ break;
case MacroExpander::ContextType::EXTERN:
return transcribe_many_ext (parser, last_token_id);
break;
diff --git a/gcc/rust/expand/rust-macro-expand.h b/gcc/rust/expand/rust-macro-expand.h
index 61b69e412a4..f08525fd4e8 100644
--- a/gcc/rust/expand/rust-macro-expand.h
+++ b/gcc/rust/expand/rust-macro-expand.h
@@ -187,9 +187,10 @@ struct MacroExpander
{
ITEM,
BLOCK,
+ EXTERN,
TRAIT,
IMPL,
- EXTERN,
+ TRAIT_IMPL,
};
ExpansionCfg cfg;
diff --git a/gcc/rust/parse/rust-parse.h b/gcc/rust/parse/rust-parse.h
index bb5bf3d7072..588061629e9 100644
--- a/gcc/rust/parse/rust-parse.h
+++ b/gcc/rust/parse/rust-parse.h
@@ -138,6 +138,7 @@ public:
std::unique_ptr<AST::ExternalItem> parse_external_item ();
std::unique_ptr<AST::TraitItem> parse_trait_item ();
std::unique_ptr<AST::InherentImplItem> parse_inherent_impl_item ();
+ std::unique_ptr<AST::TraitImplItem> parse_trait_impl_item ();
AST::PathInExpression parse_path_in_expression ();
std::vector<std::unique_ptr<AST::LifetimeParam> > parse_lifetime_params ();
AST::Visibility parse_visibility ();
@@ -298,7 +299,6 @@ private:
std::unique_ptr<AST::InherentImplItem>
parse_inherent_impl_function_or_method (AST::Visibility vis,
AST::AttrVec outer_attrs);
- std::unique_ptr<AST::TraitImplItem> parse_trait_impl_item ();
std::unique_ptr<AST::TraitImplItem>
parse_trait_impl_function_or_method (AST::Visibility vis,
AST::AttrVec outer_attrs);
diff --git a/gcc/testsuite/rust/execute/torture/macros23.rs b/gcc/testsuite/rust/execute/torture/macros23.rs
new file mode 100644
index 00000000000..846352d0487
--- /dev/null
+++ b/gcc/testsuite/rust/execute/torture/macros23.rs
@@ -0,0 +1,19 @@
+trait Valuable {
+ const VALUE: i32;
+}
+
+struct Something;
+
+macro_rules! implement {
+ () => {
+ const VALUE: i32 = 18;
+ };
+}
+
+impl Valuable for Something {
+ implement!();
+}
+
+fn main() -> i32 {
+ Something::VALUE - 18
+}
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2022-06-08 12:21 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 12:21 [gcc/devel/rust/master] macros: Allow macro calls in trait implementations Thomas Schwinge
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).