diff options
author | Arthur Cohen <arthur.cohen@embecosm.com> | 2022-03-24 15:03:20 +0100 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2022-03-24 15:03:20 +0100 |
commit | 5651331236d39ce685e1de3396463fd88c3a83d2 (patch) | |
tree | 3729e3e925c99ac127e593918544c43fc4a2d855 /gcc | |
parent | 8283724bc24efc0c11960947f2d4e99bc20b3765 (diff) | |
download | gcc-5651331236d39ce685e1de3396463fd88c3a83d2.zip gcc-5651331236d39ce685e1de3396463fd88c3a83d2.tar.gz gcc-5651331236d39ce685e1de3396463fd88c3a83d2.tar.bz2 |
macros: Allow parsing :tt fragments
:tt fragments stand for token trees, and are composed of either a token,
or a delimited token tree, which is a token tree surrounded by
delimiters (parentheses, curly brackets or square brackets).
This should allow us to handle a lot more macros, including extremely
powerful macro patterns such as TT munchers
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/expand/rust-macro-expand.cc | 4 | ||||
-rw-r--r-- | gcc/rust/parse/rust-parse.h | 2 | ||||
-rw-r--r-- | gcc/testsuite/rust/execute/torture/macros25.rs | 13 | ||||
-rw-r--r-- | gcc/testsuite/rust/execute/torture/macros26.rs | 12 |
4 files changed, 27 insertions, 4 deletions
diff --git a/gcc/rust/expand/rust-macro-expand.cc b/gcc/rust/expand/rust-macro-expand.cc index e0dfc50..6227344 100644 --- a/gcc/rust/expand/rust-macro-expand.cc +++ b/gcc/rust/expand/rust-macro-expand.cc @@ -497,10 +497,8 @@ MacroExpander::match_fragment (Parser<MacroInvocLexer> &parser, gcc_unreachable (); break; - // what is TT? case AST::MacroFragSpec::TT: - // parser.parse_token_tree() ? - gcc_unreachable (); + parser.parse_token_tree (); break; // i guess we just ignore invalid and just error out diff --git a/gcc/rust/parse/rust-parse.h b/gcc/rust/parse/rust-parse.h index cb77033..88bd311 100644 --- a/gcc/rust/parse/rust-parse.h +++ b/gcc/rust/parse/rust-parse.h @@ -142,6 +142,7 @@ public: std::vector<std::unique_ptr<AST::LifetimeParam> > parse_lifetime_params (); AST::Visibility parse_visibility (); std::unique_ptr<AST::IdentifierPattern> parse_identifier_pattern (); + std::unique_ptr<AST::TokenTree> parse_token_tree (); private: void skip_after_semicolon (); @@ -188,7 +189,6 @@ private: // Token tree or macro related AST::DelimTokenTree parse_delim_token_tree (); - std::unique_ptr<AST::TokenTree> parse_token_tree (); std::unique_ptr<AST::MacroRulesDefinition> parse_macro_rules_def (AST::AttrVec outer_attrs); std::unique_ptr<AST::MacroInvocation> diff --git a/gcc/testsuite/rust/execute/torture/macros25.rs b/gcc/testsuite/rust/execute/torture/macros25.rs new file mode 100644 index 0000000..c265872 --- /dev/null +++ b/gcc/testsuite/rust/execute/torture/macros25.rs @@ -0,0 +1,13 @@ +macro_rules! t { + ($t:tt) => { + $t + }; +} + +fn frob() -> i32 { + t!(15) + t!((14)) +} + +fn main() -> i32 { + frob() - 29 +} diff --git a/gcc/testsuite/rust/execute/torture/macros26.rs b/gcc/testsuite/rust/execute/torture/macros26.rs new file mode 100644 index 0000000..30f0bee --- /dev/null +++ b/gcc/testsuite/rust/execute/torture/macros26.rs @@ -0,0 +1,12 @@ +macro_rules! count_tt { + ($t:tt) => { 1 }; + ($t:tt $($ts:tt)*) => { 1 + count_tt!($($ts)*) }; +} + +fn main() -> i32 { + let count = count_tt!(1 2 let a = 15) + count_tt!(1 2 (let a = 15)); + // ^ ^ ^^^ ^ ^ ^^ ^ ^ ^^^^^^^^^^^^ + // 6 token-trees 3 token-trees + + count - 9 +} |