aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/expand/rust-macro-expand.cc
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-03-31 09:56:35 +0000
committerGitHub <noreply@github.com>2022-03-31 09:56:35 +0000
commit9011184f38a04f81ba3194b826bec3f30a11c07b (patch)
treed85ff3be8c56dfb9d27c64a856b3523e197b41f6 /gcc/rust/expand/rust-macro-expand.cc
parentf9c1a14dab4c47c774f9c7661afc4bb2176eb9bb (diff)
parent73532817fd6f5aa4f59953f4111217a75135a78b (diff)
downloadgcc-9011184f38a04f81ba3194b826bec3f30a11c07b.zip
gcc-9011184f38a04f81ba3194b826bec3f30a11c07b.tar.gz
gcc-9011184f38a04f81ba3194b826bec3f30a11c07b.tar.bz2
Merge #1071
1071: Allow transcribing of zero nodes in certain cases r=CohenArthur a=CohenArthur When expanding AST fragments containing multiple nodes, we must be aware that some cases allow expanding zero or more nodes. Any macro transcription that gets parsed as many nodes (ie any transcriber function that calls `parse_many`) needs to be able to parse zero of those nodes and still get expanded properly (basically, removed). Previously, this would cause a failure to lower the macro invocation which would remain as a child instead of getting stripped/erased. Co-authored-by: Arthur Cohen <arthur.cohen@embecosm.com>
Diffstat (limited to 'gcc/rust/expand/rust-macro-expand.cc')
-rw-r--r--gcc/rust/expand/rust-macro-expand.cc38
1 files changed, 19 insertions, 19 deletions
diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc
index 852e619..20bbbc0 100644
--- a/gcc/rust/expand/rust-macro-expand.cc
+++ b/gcc/rust/expand/rust-macro-expand.cc
@@ -195,7 +195,7 @@ MacroExpander::expand_invoc (AST::MacroInvocation &invoc)
bool ok = mappings->lookup_macro_def (resolved_node, &rules_def);
rust_assert (ok);
- auto fragment = AST::ASTFragment::create_empty ();
+ auto fragment = AST::ASTFragment::create_error ();
if (rules_def->is_builtin ())
fragment
@@ -236,7 +236,7 @@ MacroExpander::expand_invoc_semi (AST::MacroInvocation &invoc)
bool ok = mappings->lookup_macro_def (resolved_node, &rules_def);
rust_assert (ok);
- auto fragment = AST::ASTFragment::create_empty ();
+ auto fragment = AST::ASTFragment::create_error ();
if (rules_def->is_builtin ())
fragment
@@ -781,7 +781,7 @@ MacroExpander::match_repetition (Parser<MacroInvocLexer> &parser,
/**
* Helper function to refactor calling a parsing function 0 or more times
*/
-static std::vector<AST::SingleASTNode>
+static AST::ASTFragment
parse_many (Parser<MacroInvocLexer> &parser, TokenId &delimiter,
std::function<AST::SingleASTNode ()> parse_fn)
{
@@ -795,7 +795,7 @@ parse_many (Parser<MacroInvocLexer> &parser, TokenId &delimiter,
nodes.emplace_back (std::move (node));
}
- return nodes;
+ return AST::ASTFragment (std::move (nodes));
}
/**
@@ -804,7 +804,7 @@ parse_many (Parser<MacroInvocLexer> &parser, TokenId &delimiter,
* @param parser Parser to extract items from
* @param delimiter Id of the token on which parsing should stop
*/
-static std::vector<AST::SingleASTNode>
+static AST::ASTFragment
transcribe_many_items (Parser<MacroInvocLexer> &parser, TokenId &delimiter)
{
return parse_many (parser, delimiter, [&parser] () {
@@ -819,7 +819,7 @@ transcribe_many_items (Parser<MacroInvocLexer> &parser, TokenId &delimiter)
* @param parser Parser to extract items from
* @param delimiter Id of the token on which parsing should stop
*/
-static std::vector<AST::SingleASTNode>
+static AST::ASTFragment
transcribe_many_ext (Parser<MacroInvocLexer> &parser, TokenId &delimiter)
{
return parse_many (parser, delimiter, [&parser] () {
@@ -834,7 +834,7 @@ transcribe_many_ext (Parser<MacroInvocLexer> &parser, TokenId &delimiter)
* @param parser Parser to extract items from
* @param delimiter Id of the token on which parsing should stop
*/
-static std::vector<AST::SingleASTNode>
+static AST::ASTFragment
transcribe_many_trait_items (Parser<MacroInvocLexer> &parser,
TokenId &delimiter)
{
@@ -850,7 +850,7 @@ transcribe_many_trait_items (Parser<MacroInvocLexer> &parser,
* @param parser Parser to extract items from
* @param delimiter Id of the token on which parsing should stop
*/
-static std::vector<AST::SingleASTNode>
+static AST::ASTFragment
transcribe_many_impl_items (Parser<MacroInvocLexer> &parser, TokenId &delimiter)
{
return parse_many (parser, delimiter, [&parser] () {
@@ -865,7 +865,7 @@ transcribe_many_impl_items (Parser<MacroInvocLexer> &parser, TokenId &delimiter)
* @param parser Parser to extract items from
* @param delimiter Id of the token on which parsing should stop
*/
-static std::vector<AST::SingleASTNode>
+static AST::ASTFragment
transcribe_many_trait_impl_items (Parser<MacroInvocLexer> &parser,
TokenId &delimiter)
{
@@ -881,7 +881,7 @@ transcribe_many_trait_impl_items (Parser<MacroInvocLexer> &parser,
* @param parser Parser to extract statements from
* @param delimiter Id of the token on which parsing should stop
*/
-static std::vector<AST::SingleASTNode>
+static AST::ASTFragment
transcribe_many_stmts (Parser<MacroInvocLexer> &parser, TokenId &delimiter)
{
auto restrictions = ParseRestrictions ();
@@ -901,12 +901,12 @@ transcribe_many_stmts (Parser<MacroInvocLexer> &parser, TokenId &delimiter)
*
* @param parser Parser to extract statements from
*/
-static std::vector<AST::SingleASTNode>
+static AST::ASTFragment
transcribe_expression (Parser<MacroInvocLexer> &parser)
{
auto expr = parser.parse_expr ();
- return {AST::SingleASTNode (std::move (expr))};
+ return AST::ASTFragment ({std::move (expr)});
}
/**
@@ -914,15 +914,15 @@ transcribe_expression (Parser<MacroInvocLexer> &parser)
*
* @param parser Parser to extract statements from
*/
-static std::vector<AST::SingleASTNode>
+static AST::ASTFragment
transcribe_type (Parser<MacroInvocLexer> &parser)
{
- auto expr = parser.parse_type ();
+ auto type = parser.parse_type ();
- return {AST::SingleASTNode (std::move (expr))};
+ return AST::ASTFragment ({std::move (type)});
}
-static std::vector<AST::SingleASTNode>
+static AST::ASTFragment
transcribe_on_delimiter (Parser<MacroInvocLexer> &parser, bool semicolon,
AST::DelimType delimiter, TokenId last_token_id)
{
@@ -932,7 +932,7 @@ transcribe_on_delimiter (Parser<MacroInvocLexer> &parser, bool semicolon,
return transcribe_expression (parser);
} // namespace Rust
-static std::vector<AST::SingleASTNode>
+static AST::ASTFragment
transcribe_context (MacroExpander::ContextType ctx,
Parser<MacroInvocLexer> &parser, bool semicolon,
AST::DelimType delimiter, TokenId last_token_id)
@@ -1049,7 +1049,7 @@ MacroExpander::transcribe_rule (
// as a statement (either via ExpressionStatement or
// MacroInvocationWithSemi)
- auto nodes
+ auto fragment
= transcribe_context (ctx, parser, semicolon,
invoc_token_tree.get_delim_type (), last_token_id);
@@ -1072,6 +1072,6 @@ MacroExpander::transcribe_rule (
"tokens here and after are unparsed");
}
- return AST::ASTFragment (std::move (nodes));
+ return fragment;
}
} // namespace Rust