[gcc r14-7579] gccrs: converter: Add TokenStream conversion function

Arthur Cohen cohenarthur@gcc.gnu.org
Tue Jan 16 17:42:18 GMT 2024


https://gcc.gnu.org/g:97612931b1324a2b320d047146f48d26d071681b

commit r14-7579-g97612931b1324a2b320d047146f48d26d071681b
Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Date:   Wed May 3 10:17:16 2023 +0200

    gccrs: converter: Add TokenStream conversion function
    
    Add a tokenstream conversion function dispatching to inner elements
    
    gcc/rust/ChangeLog:
    
            * util/rust-token-converter.cc (to_tokenstream): Change function
            name from to_tokenstream to convert.
            (convert): Likewise.
            (from_tokenstream): Add tokenstream handler.
            (from_ident): Add empty function.
            (from_literal): Likewise.
            (from_punct): Likewise.
            (from_group): Likewise.
            * util/rust-token-converter.h (to_tokenstream): Change function
            name from to_tokenstream to convert.
            (convert): Likewise.
    
    Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>

Diff:
---
 gcc/rust/util/rust-token-converter.cc | 55 ++++++++++++++++++++++++++++++++++-
 gcc/rust/util/rust-token-converter.h  |  7 +++--
 2 files changed, 59 insertions(+), 3 deletions(-)

diff --git a/gcc/rust/util/rust-token-converter.cc b/gcc/rust/util/rust-token-converter.cc
index b0d47de512f..25abf763e24 100644
--- a/gcc/rust/util/rust-token-converter.cc
+++ b/gcc/rust/util/rust-token-converter.cc
@@ -122,7 +122,7 @@ dispatch_integer_literals (ProcMacro::TokenStream &ts, TokenPtr &token)
 }
 
 ProcMacro::TokenStream
-to_tokenstream (std::vector<TokenPtr> tokens)
+convert (std::vector<TokenPtr> tokens)
 {
   std::vector<ProcMacro::TokenStream> trees;
   trees.push_back (ProcMacro::TokenStream::make_tokenstream ());
@@ -297,4 +297,57 @@ to_tokenstream (std::vector<TokenPtr> tokens)
   return trees.back ();
 }
 
+static void
+from_tokenstream (ProcMacro::TokenStream ts, std::vector<TokenPtr> &result);
+
+static void
+from_ident (ProcMacro::Ident ident, std::vector<TokenPtr> &result)
+{}
+
+static void
+from_literal (ProcMacro::Literal literal, std::vector<TokenPtr> &result)
+{}
+
+static void
+from_punct (ProcMacro::Punct punct, std::vector<TokenPtr> &result)
+{}
+
+static void
+from_group (ProcMacro::Group g, std::vector<TokenPtr> &result)
+{}
+
+static void
+from_tokenstream (ProcMacro::TokenStream ts, std::vector<TokenPtr> &result)
+{
+  for (std::uint64_t i = 0; i < ts.size; i++)
+    {
+      ProcMacro::TokenTree &tt = ts.data[i];
+      switch (tt.tag)
+	{
+	case ProcMacro::GROUP:
+	  from_group (tt.payload.group, result);
+	  break;
+	case ProcMacro::IDENT:
+	  from_ident (tt.payload.ident, result);
+	  break;
+	case ProcMacro::PUNCT:
+	  from_punct (tt.payload.punct, result);
+	  break;
+	case ProcMacro::LITERAL:
+	  from_literal (tt.payload.literal, result);
+	  break;
+	default:
+	  gcc_unreachable ();
+	}
+    }
+}
+
+std::vector<TokenPtr>
+convert (ProcMacro::TokenStream ts)
+{
+  std::vector<TokenPtr> result;
+  from_tokenstream (ts, result);
+  return result;
+}
+
 } // namespace Rust
diff --git a/gcc/rust/util/rust-token-converter.h b/gcc/rust/util/rust-token-converter.h
index 5be745cd66c..ee82d0bc13f 100644
--- a/gcc/rust/util/rust-token-converter.h
+++ b/gcc/rust/util/rust-token-converter.h
@@ -19,12 +19,15 @@
 
 #include <vector>
 #include "rust-token.h"
-#include "libproc_macro/tokenstream.h"
+#include "libproc_macro/proc_macro.h"
 
 namespace Rust {
 
 ProcMacro::TokenStream
-to_tokenstream (std::vector<TokenPtr> tokens);
+convert (std::vector<TokenPtr> tokens);
+
+std::vector<TokenPtr>
+convert (ProcMacro::TokenStream ts);
 
 } // namespace Rust


More information about the Gcc-cvs mailing list