public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] macros: Allow parsing :tt fragments
@ 2022-06-08 12:23 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:23 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:5651331236d39ce685e1de3396463fd88c3a83d2

commit 5651331236d39ce685e1de3396463fd88c3a83d2
Author: Arthur Cohen <arthur.cohen@embecosm.com>
Date:   Thu Mar 24 15:03:20 2022 +0100

    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

Diff:
---
 gcc/rust/expand/rust-macro-expand.cc           |  4 +---
 gcc/rust/parse/rust-parse.h                    |  2 +-
 gcc/testsuite/rust/execute/torture/macros25.rs | 13 +++++++++++++
 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 e0dfc502609..62273448677 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 cb77033fe55..88bd311935b 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 00000000000..c2658721bdf
--- /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 00000000000..30f0beef0d9
--- /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
+}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-06-08 12:23 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:23 [gcc/devel/rust/master] macros: Allow parsing :tt fragments 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).