From: Pierre-Emmanuel Patry Date: Wed, 3 May 2023 08:17:16 +0000 (+0200) Subject: gccrs: converter: Add TokenStream conversion function X-Git-Tag: basepoints/gcc-15~2560 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=97612931b1324a2b320d047146f48d26d071681b;p=thirdparty%2Fgcc.git 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 --- diff --git a/gcc/rust/util/rust-token-converter.cc b/gcc/rust/util/rust-token-converter.cc index b0d47de512f7..25abf763e241 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 tokens) +convert (std::vector tokens) { std::vector trees; trees.push_back (ProcMacro::TokenStream::make_tokenstream ()); @@ -297,4 +297,57 @@ to_tokenstream (std::vector tokens) return trees.back (); } +static void +from_tokenstream (ProcMacro::TokenStream ts, std::vector &result); + +static void +from_ident (ProcMacro::Ident ident, std::vector &result) +{} + +static void +from_literal (ProcMacro::Literal literal, std::vector &result) +{} + +static void +from_punct (ProcMacro::Punct punct, std::vector &result) +{} + +static void +from_group (ProcMacro::Group g, std::vector &result) +{} + +static void +from_tokenstream (ProcMacro::TokenStream ts, std::vector &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 +convert (ProcMacro::TokenStream ts) +{ + std::vector 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 5be745cd66c1..ee82d0bc13f3 100644 --- a/gcc/rust/util/rust-token-converter.h +++ b/gcc/rust/util/rust-token-converter.h @@ -19,12 +19,15 @@ #include #include "rust-token.h" -#include "libproc_macro/tokenstream.h" +#include "libproc_macro/proc_macro.h" namespace Rust { ProcMacro::TokenStream -to_tokenstream (std::vector tokens); +convert (std::vector tokens); + +std::vector +convert (ProcMacro::TokenStream ts); } // namespace Rust