From ab0e27412e3ae4de957c33a267f27a87ea41b4e9 Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Wed, 3 May 2023 10:42:36 +0200 Subject: [PATCH] gccrs: converter: Add from_tokentree function Add the from_tokentree function which converts a tokentree to it's token representation. This function was previously inlined in the from_tokenstream function but I wanted to keep things clear and coherent. gcc/rust/ChangeLog: * util/rust-token-converter.cc (from_tokenstream): Add call to from_tokentree. (from_tokentree): Add implementation, from the from_tokenstream function. Signed-off-by: Pierre-Emmanuel Patry --- gcc/rust/util/rust-token-converter.cc | 61 ++++++++++++++++++--------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/gcc/rust/util/rust-token-converter.cc b/gcc/rust/util/rust-token-converter.cc index e40d2135fda2..54c9cb7c3fee 100644 --- a/gcc/rust/util/rust-token-converter.cc +++ b/gcc/rust/util/rust-token-converter.cc @@ -298,7 +298,8 @@ convert (std::vector tokens) } static void -from_tokenstream (ProcMacro::TokenStream ts, std::vector &result); +from_tokenstream (const ProcMacro::TokenStream &ts, + std::vector &result); static void from_ident (ProcMacro::Ident ident, std::vector &result) @@ -313,7 +314,7 @@ from_punct (ProcMacro::Punct punct, std::vector &result) {} /** - * Iterate over a Group and append all inner tokens to a vector enclosed by it's + * Iterate over a Group and append all inner tokens to a vector enclosed by its * delimiters. * * @param g Reference to the Group to convert. @@ -347,29 +348,47 @@ from_group (const ProcMacro::Group &g, std::vector &result) } } +/** + * Dispatch TokenTree's conversion to its inner type depending on its tag. + * + * @param tt Reference to the TokenTree. + * @param result Reference to the vector tokens should be appended to. + */ +static void +from_tokentree (const ProcMacro::TokenTree &tt, std::vector &result) +{ + 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 (); + } +} + +/** + * Iterate over a TokenStream and append all inner tokens to a vector. + * + * @param ts Reference to the TokenStream. + * @param result Reference to the vector tokens should be appended to. + */ static void -from_tokenstream (ProcMacro::TokenStream ts, std::vector &result) +from_tokenstream (const 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 (); - } + from_tokentree (ts.data[i], result); } } -- 2.47.2