]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: converter: Add from_tokentree function
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Wed, 3 May 2023 08:42:36 +0000 (10:42 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:37:17 +0000 (18:37 +0100)
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>
gcc/rust/util/rust-token-converter.cc

index e40d2135fda255cc7fbdf04278d4572fcf6fda40..54c9cb7c3feeb4cc7453ed0eea7bde045abdf868 100644 (file)
@@ -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);
     }
 }