aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>2023-05-03 10:17:16 +0200
committerArthur Cohen <arthur.cohen@embecosm.com>2023-05-11 09:38:21 +0200
commit15225362fe308ee1cf71018ac84203574feaa99d (patch)
tree24b891da7d005324c9ea80fd54c341a62bc8aa4e
parentb12481cf8ca462dd49ff101f50bda02418f777d8 (diff)
downloadgcc-15225362fe308ee1cf71018ac84203574feaa99d.zip
gcc-15225362fe308ee1cf71018ac84203574feaa99d.tar.gz
gcc-15225362fe308ee1cf71018ac84203574feaa99d.tar.bz2
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>
-rw-r--r--gcc/rust/util/rust-token-converter.cc55
-rw-r--r--gcc/rust/util/rust-token-converter.h7
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 b0d47de..25abf76 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 5be745c..ee82d0b 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