aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2022-02-17 13:23:18 +0000
committerPhilip Herron <philip.herron@embecosm.com>2022-02-17 13:28:25 +0000
commit4c70d7ec770d226bf9ad59b4f03897f6fb10df15 (patch)
tree8b7516086e531504e04ad03420af4668be0b29ee
parent19c5dde80f28c67f657775f770922783faff4b89 (diff)
downloadgcc-4c70d7ec770d226bf9ad59b4f03897f6fb10df15.zip
gcc-4c70d7ec770d226bf9ad59b4f03897f6fb10df15.tar.gz
gcc-4c70d7ec770d226bf9ad59b4f03897f6fb10df15.tar.bz2
Support block expressions within macros
When we parse DelimTokenTree's the delimiter's are synthesised when we ask for the token stream which results in tokens lacking location info. This removes the hack by adding the actual tokens from the lexer into the stream.
-rw-r--r--gcc/rust/ast/rust-ast-full-test.cc15
-rw-r--r--gcc/rust/ast/rust-ast.h30
-rw-r--r--gcc/rust/parse/rust-parse-impl.h6
-rw-r--r--gcc/testsuite/rust/execute/torture/macros5.rs13
4 files changed, 19 insertions, 45 deletions
diff --git a/gcc/rust/ast/rust-ast-full-test.cc b/gcc/rust/ast/rust-ast-full-test.cc
index ee7c407..3a1e295 100644
--- a/gcc/rust/ast/rust-ast-full-test.cc
+++ b/gcc/rust/ast/rust-ast-full-test.cc
@@ -4387,14 +4387,6 @@ std::vector<std::unique_ptr<Token> >
DelimTokenTree::to_token_stream () const
{
std::vector<std::unique_ptr<Token> > tokens;
-
- // simulate presence of delimiters
- const_TokenPtr left_paren
- = Rust::Token::make (left_delim_type_tok_token_id (delim_type),
- Linemap::unknown_location ());
- tokens.push_back (
- std::unique_ptr<Token> (new Token (std::move (left_paren))));
-
for (const auto &tree : token_trees)
{
std::vector<std::unique_ptr<Token> > stream = tree->to_token_stream ();
@@ -4403,14 +4395,7 @@ DelimTokenTree::to_token_stream () const
std::make_move_iterator (stream.end ()));
}
- const_TokenPtr right_paren
- = Rust::Token::make (right_delim_type_tok_token_id (delim_type),
- Linemap::unknown_location ());
- tokens.push_back (
- std::unique_ptr<Token> (new Token (std::move (right_paren))));
-
tokens.shrink_to_fit ();
-
return tokens;
}
diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h
index 8411f65..3e7faf5 100644
--- a/gcc/rust/ast/rust-ast.h
+++ b/gcc/rust/ast/rust-ast.h
@@ -778,36 +778,6 @@ public:
}
DelimType get_delim_type () const { return delim_type; }
-
- static TokenId left_delim_type_tok_token_id (DelimType delim_type)
- {
- switch (delim_type)
- {
- case PARENS:
- return LEFT_PAREN;
- case SQUARE:
- return LEFT_SQUARE;
- case CURLY:
- return LEFT_CURLY;
- default:
- gcc_unreachable ();
- }
- }
-
- static TokenId right_delim_type_tok_token_id (DelimType delim_type)
- {
- switch (delim_type)
- {
- case PARENS:
- return RIGHT_PAREN;
- case SQUARE:
- return RIGHT_SQUARE;
- case CURLY:
- return RIGHT_CURLY;
- default:
- gcc_unreachable ();
- }
- }
};
/* Forward decl - definition moved to rust-expr.h as it requires LiteralExpr to
diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index 7483818..2ea42c7 100644
--- a/gcc/rust/parse/rust-parse-impl.h
+++ b/gcc/rust/parse/rust-parse-impl.h
@@ -905,6 +905,9 @@ Parser<ManagedTokenSource>::parse_delim_token_tree ()
// parse actual token tree vector - 0 or more
std::vector<std::unique_ptr<AST::TokenTree>> token_trees_in_tree;
+ auto delim_open
+ = std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
+ token_trees_in_tree.push_back (std::move (delim_open));
// repeat loop until finding the matching delimiter
t = lexer.peek_token ();
@@ -929,6 +932,9 @@ Parser<ManagedTokenSource>::parse_delim_token_tree ()
// lexer.skip_token();
t = lexer.peek_token ();
}
+ auto delim_close
+ = std::unique_ptr<AST::Token> (new AST::Token (std::move (t)));
+ token_trees_in_tree.push_back (std::move (delim_close));
AST::DelimTokenTree token_tree (delim_type, std::move (token_trees_in_tree),
initial_loc);
diff --git a/gcc/testsuite/rust/execute/torture/macros5.rs b/gcc/testsuite/rust/execute/torture/macros5.rs
new file mode 100644
index 0000000..8226654
--- /dev/null
+++ b/gcc/testsuite/rust/execute/torture/macros5.rs
@@ -0,0 +1,13 @@
+macro_rules! add {
+ ($a:expr,$b:expr) => {{
+ $a + $b
+ }};
+}
+
+fn test() -> i32 {
+ add!(1, 2)
+}
+
+fn main() -> i32 {
+ test() - 3
+}