]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: proc_macro: Add literal_from_string callback
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Thu, 27 Jul 2023 14:21:25 +0000 (16:21 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:56:02 +0000 (18:56 +0100)
The function to get a literal from a string missed an implementation.
It did require a conversion function to achieve it, now that callback
system has been merged this function can be easily implemented.

gcc/rust/ChangeLog:

* expand/rust-proc-macro.cc (literal_from_string): Add callback
function.
(load_macros_array): Likewise.

libgrust/ChangeLog:

* libproc_macro/literal.cc (Literal__from_string): Add
implementation with call to constructor.
(Literal::make_literal): Add new constructor which calls the
callback.
* libproc_macro/literal.h: Add new constructor's
prototype.
* libproc_macro/proc_macro.cc (bridge_is_available):
Change symbol name to match convention.
* libproc_macro/registration.h: Add lit_from_str
symbol.
* libproc_macro/tokenstream.cc (TokenStream::make_tokenstream):
Change symbol name to disambiguate with literal from string.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/expand/rust-proc-macro.cc
libgrust/libproc_macro/literal.cc
libgrust/libproc_macro/literal.h
libgrust/libproc_macro/proc_macro.cc
libgrust/libproc_macro/registration.h
libgrust/libproc_macro/tokenstream.cc

index c664e20ecbca3f4866c5d2990263a1d1913d7956..69de9894688c5546b54280b558971bfc3014e09e 100644 (file)
@@ -28,6 +28,22 @@ const std::string PROC_MACRO_DECL_PREFIX = "__gccrs_proc_macro_decls_";
 
 namespace {
 
+ProcMacro::Literal
+literal_from_string (const std::string &data, bool &error)
+{
+  Lexer lex (data);
+  const_TokenPtr output = lex.build_token ();
+  if (output == nullptr || !output->is_literal ())
+    {
+      error = true;
+      // We should probably rework this
+      return ProcMacro::Literal::make_usize (0);
+    }
+
+  error = false;
+  return convert_literal (output);
+}
+
 ProcMacro::TokenStream
 tokenstream_from_string (std::string &data, bool &lex_error)
 {
@@ -55,7 +71,12 @@ tokenstream_from_string (std::string &data, bool &lex_error)
 
 static_assert (
   std::is_same<decltype (tokenstream_from_string) *,
-              ProcMacro::from_str_function_t>::value,
+              ProcMacro::ts_from_str_fn_t>::value,
+  "Registration callback signature not synced, check proc macro internals.");
+
+static_assert (
+  std::is_same<decltype (literal_from_string) *,
+              ProcMacro::lit_from_str_fn_t>::value,
   "Registration callback signature not synced, check proc macro internals.");
 
 } // namespace
@@ -96,10 +117,13 @@ load_macros_array (std::string path)
       return nullptr;
     }
 
-  if (!REGISTER_CALLBACK (handle, __gccrs_proc_macro_from_str_fn,
+  if (!REGISTER_CALLBACK (handle, __gccrs_proc_macro_ts_from_str_,
                          tokenstream_from_string))
     return nullptr;
-  if (!REGISTER_CALLBACK (handle, __gccrs_proc_macro_is_available_fn,
+  if (!REGISTER_CALLBACK (handle, __gccrs_proc_macro_lit_from_str_,
+                         literal_from_string))
+    return nullptr;
+  if (!REGISTER_CALLBACK (handle, __gccrs_proc_macro_is_available_,
                          ProcMacro::BridgeState::Available))
     return nullptr;
 
index 8e1e0a4a116c5d29cc8d6c8f4eaacb3d2418d1c1..ea09106cd62643a03cebc8930d6ba179ac974eeb 100644 (file)
 #include <cstring>
 #include <cstdlib>
 
+#include "registration.h"
+
 namespace ProcMacro {
 
 extern "C" {
 bool
 Literal__from_string (FFIString str, Literal *lit)
 {
-  // FIXME: implement this function with lexer
-  std::abort ();
-  return false;
+  bool result;
+  auto source = str.to_string ();
+
+  *lit = Literal::make_literal (source, result);
+  return result;
 }
 }
 
@@ -49,6 +53,12 @@ Literal::clone () const
   return {this->kind, this->text.clone (), this->suffix.clone (), this->span};
 }
 
+Literal
+Literal::make_literal (const std::string &text, bool &has_error)
+{
+  return __gccrs_proc_macro_lit_from_str_ (text, has_error);
+}
+
 Literal
 Literal::make_literal (LitKind kind, Span span, const std::string &text,
                       const std::string &suffix)
index fb8a8b442e29ed0b070e18c68f6804c45d951bdb..37ba0891dbef7838c1623e01c13fc4d5df602434 100644 (file)
@@ -77,6 +77,7 @@ public:
   Literal clone () const;
   bool has_suffix () const { return suffix.len != 0; };
 
+  static Literal make_literal (const std::string &text, bool &has_error);
   static Literal make_literal (const LitKind kind, Span span,
                               const std::string &text,
                               const std::string &suffix = "");
index 293fc957038268808833b66b187e2673b94a3d7d..0490673b6569066d0c987855d0512c90b302330c 100644 (file)
@@ -53,12 +53,12 @@ Procmacro::make_bang (const char *name, BangMacro macro)
 extern "C" bool
 bridge_is_available ()
 {
-  return __gccrs_proc_macro_is_available_fn
-        == ProcMacro::BridgeState::Available;
+  return __gccrs_proc_macro_is_available_ == ProcMacro::BridgeState::Available;
 }
 
 } // namespace ProcMacro
 
-ProcMacro::from_str_function_t __gccrs_proc_macro_from_str_fn = nullptr;
-ProcMacro::BridgeState __gccrs_proc_macro_is_available_fn
+ProcMacro::ts_from_str_fn_t __gccrs_proc_macro_ts_from_str_ = nullptr;
+ProcMacro::lit_from_str_fn_t __gccrs_proc_macro_lit_from_str_ = nullptr;
+ProcMacro::BridgeState __gccrs_proc_macro_is_available_
   = ProcMacro::BridgeState::Unavailable;
index aba7edd81fbab7f4979dec7c89fd4c8e5ed56c5d..98e7c139c272265b7b8a543666e5179755df4f0c 100644 (file)
 #include <string>
 #include "tokenstream.h"
 #include "bridge.h"
+#include "literal.h"
 
 namespace ProcMacro {
 
-using from_str_function_t = ProcMacro::TokenStream (*) (std::string &, bool &);
+using ts_from_str_fn_t = ProcMacro::TokenStream (*) (std::string &, bool &);
+using lit_from_str_fn_t = ProcMacro::Literal (*) (const std::string &, bool &);
 
 } // namespace ProcMacro
 
-extern "C" ProcMacro::from_str_function_t __gccrs_proc_macro_from_str_fn;
-extern "C" ProcMacro::BridgeState __gccrs_proc_macro_is_available_fn;
+extern "C" ProcMacro::ts_from_str_fn_t __gccrs_proc_macro_ts_from_str_;
+extern "C" ProcMacro::lit_from_str_fn_t __gccrs_proc_macro_lit_from_str_;
+extern "C" ProcMacro::BridgeState __gccrs_proc_macro_is_available_;
 
 #endif /* !REGISTRATION_H */
index 0f7f1f2079b6097195e1e7442970f845240dda26..685f28424d24acb609f11eb63b84e6b0d1e0f942 100644 (file)
@@ -49,7 +49,7 @@ TokenStream::make_tokenstream (std::uint64_t capacity)
 TokenStream
 TokenStream::make_tokenstream (std::string &source, bool &has_error)
 {
-  return __gccrs_proc_macro_from_str_fn (source, has_error);
+  return __gccrs_proc_macro_ts_from_str_ (source, has_error);
 }
 
 void