]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: converter: Move literal conversion out
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Thu, 27 Jul 2023 13:57:07 +0000 (15:57 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:56:02 +0000 (18:56 +0100)
The literal conversion code could be used for the literal_from_string
callback, this means we should move it out of the function in it's own
function. This involves a new switch, which is quite sad but we're not
yet at a performance profiling phase, there may be lower hanging fruits.

gcc/rust/ChangeLog:

* util/rust-token-converter.cc (handle_suffix): Rework function
to make it work with the new literal conversion function.
(convert_literal): Add a new function to convert to a proc macro
literal from a literal tokenptr. The function will abort if the
pointer does not point to a literal.
(convert): Add call to convert literal for every literal case.
* util/rust-token-converter.h (convert_literal): Add public
prototype.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/util/rust-token-converter.cc
gcc/rust/util/rust-token-converter.h

index 67c1459769a8e68eec781cda5a92bfa65c94991f..e0794f38eefbb23a72a1620edd4c77e8ecbeb08f 100644 (file)
@@ -63,16 +63,41 @@ convert (ProcMacro::Span span)
   return span.start;
 }
 
-static void
-handle_suffix (ProcMacro::TokenStream &ts, const const_TokenPtr &token,
-              ProcMacro::LitKind kind)
+static ProcMacro::Literal
+handle_suffix (const const_TokenPtr &token, ProcMacro::LitKind kind)
 {
   auto str = token->as_string ();
   auto lookup = suffixes.lookup (token->get_type_hint ());
   auto suffix = suffixes.is_iter_ok (lookup) ? lookup->second : "";
-  ts.push (ProcMacro::TokenTree::make_tokentree (
-    ProcMacro::Literal::make_literal (kind, convert (token->get_locus ()), str,
-                                     suffix)));
+  return ProcMacro::Literal::make_literal (kind, convert (token->get_locus ()),
+                                          str, suffix);
+}
+
+ProcMacro::Literal
+convert_literal (const_TokenPtr lit)
+{
+  auto loc = convert (lit->get_locus ());
+  switch (lit->get_id ())
+    {
+    case FLOAT_LITERAL:
+      return handle_suffix (lit, ProcMacro::LitKind::make_float ());
+    case INT_LITERAL:
+      return handle_suffix (lit, ProcMacro::LitKind::make_integer ());
+    case CHAR_LITERAL:
+      return ProcMacro::Literal::make_literal (ProcMacro::LitKind::make_char (),
+                                              loc, lit->as_string ());
+    case STRING_LITERAL:
+      return ProcMacro::Literal::make_literal (ProcMacro::LitKind::make_str (),
+                                              loc, lit->as_string ());
+    case BYTE_CHAR_LITERAL:
+      return ProcMacro::Literal::make_literal (ProcMacro::LitKind::make_byte (),
+                                              loc, lit->as_string ());
+    case BYTE_STRING_LITERAL:
+      return ProcMacro::Literal::make_literal (
+       ProcMacro::LitKind::make_byte_str (), loc, lit->as_string ());
+    default:
+      rust_unreachable ();
+    }
 }
 
 ProcMacro::TokenStream
@@ -87,32 +112,13 @@ convert (const std::vector<const_TokenPtr> &tokens)
        {
        // Literals
        case FLOAT_LITERAL:
-         handle_suffix (trees.back (), token,
-                        ProcMacro::LitKind::make_float ());
-         break;
        case INT_LITERAL:
-         handle_suffix (trees.back (), token,
-                        ProcMacro::LitKind::make_integer ());
-         break;
        case CHAR_LITERAL:
-         trees.back ().push (ProcMacro::TokenTree::make_tokentree (
-           ProcMacro::Literal::make_literal (ProcMacro::LitKind::make_char (),
-                                             loc, token->as_string ())));
-         break;
        case STRING_LITERAL:
-         trees.back ().push (ProcMacro::TokenTree::make_tokentree (
-           ProcMacro::Literal::make_literal (ProcMacro::LitKind::make_str (),
-                                             loc, token->as_string ())));
-         break;
        case BYTE_CHAR_LITERAL:
-         trees.back ().push (ProcMacro::TokenTree::make_tokentree (
-           ProcMacro::Literal::make_literal (ProcMacro::LitKind::make_byte (),
-                                             loc, token->as_string ())));
-         break;
        case BYTE_STRING_LITERAL:
-         trees.back ().push (ProcMacro::TokenTree::make_tokentree (
-           ProcMacro::Literal::make_literal (
-             ProcMacro::LitKind::make_byte_str (), loc, token->as_string ())));
+         trees.back ().push (
+           ProcMacro::TokenTree::make_tokentree (convert_literal (token)));
          break;
        // Ident
        case IDENTIFIER:
index 75b3b38f3726de1a0732c1bd0e47737fb3a5317c..3e9feebd0585401218a06ae4a2e9385a7dd5300e 100644 (file)
@@ -29,6 +29,9 @@ convert (const std::vector<const_TokenPtr> &tokens);
 std::vector<const_TokenPtr>
 convert (const ProcMacro::TokenStream &ts);
 
+ProcMacro::Literal
+convert_literal (const_TokenPtr lit);
+
 } // namespace Rust
 
 #endif /* ! RUST_TOKEN_CONVERTER_H */