diff options
author | Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com> | 2023-05-03 10:42:36 +0200 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2023-05-11 09:38:21 +0200 |
commit | c24227d377fea090cf019a40f14308b52bc5af82 (patch) | |
tree | 3cea17d64864a7d64febeb407c116352e764d3a5 | |
parent | 87dfca85c99477d442d62373bd477b87f95857fe (diff) | |
download | gcc-c24227d377fea090cf019a40f14308b52bc5af82.zip gcc-c24227d377fea090cf019a40f14308b52bc5af82.tar.gz gcc-c24227d377fea090cf019a40f14308b52bc5af82.tar.bz2 |
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 <pierre-emmanuel.patry@embecosm.com>
-rw-r--r-- | gcc/rust/util/rust-token-converter.cc | 61 |
1 files changed, 40 insertions, 21 deletions
diff --git a/gcc/rust/util/rust-token-converter.cc b/gcc/rust/util/rust-token-converter.cc index e40d213..54c9cb7 100644 --- a/gcc/rust/util/rust-token-converter.cc +++ b/gcc/rust/util/rust-token-converter.cc @@ -298,7 +298,8 @@ convert (std::vector<TokenPtr> tokens) } static void -from_tokenstream (ProcMacro::TokenStream ts, std::vector<TokenPtr> &result); +from_tokenstream (const ProcMacro::TokenStream &ts, + std::vector<TokenPtr> &result); static void from_ident (ProcMacro::Ident ident, std::vector<TokenPtr> &result) @@ -313,7 +314,7 @@ from_punct (ProcMacro::Punct punct, std::vector<TokenPtr> &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<TokenPtr> &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<TokenPtr> &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<TokenPtr> &result) +from_tokenstream (const 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 (); - } + from_tokentree (ts.data[i], result); } } |