#include "rust-diagnostics.h"
#include "rust-proc-macro.h"
+#include "rust-lex.h"
+#include "rust-token-converter.h"
#ifndef _WIN32
#include <dlfcn.h>
#endif
const std::string PROC_MACRO_DECL_PREFIX = "__gccrs_proc_macro_decls_";
+ProcMacro::TokenStream
+tokenstream_from_string (std::string &data, bool &lex_error)
+{
+ // FIXME: Insert location pointing to call site in tokens
+ Lexer lex (data);
+
+ std::vector<const_TokenPtr> tokens;
+ TokenPtr ptr;
+ for (ptr = lex.build_token ();
+ ptr != nullptr && ptr->get_id () != END_OF_FILE;
+ ptr = lex.build_token ())
+ {
+ tokens.emplace_back (ptr);
+ }
+
+ if (ptr == nullptr)
+ {
+ lex_error = true;
+ return ProcMacro::TokenStream::make_tokenstream ();
+ }
+
+ lex_error = false;
+ return convert (tokens);
+}
+
+static_assert (
+ std::is_same<decltype (tokenstream_from_string) *,
+ ProcMacro::from_str_function_t>::value,
+ "Registration callback signature not synced, check proc macro internals.");
+
+template <typename Symbol, typename Callback>
+bool
+register_callback (void *handle, Symbol, std::string symbol_name,
+ Callback callback)
+{
+ void *addr = dlsym (handle, symbol_name.c_str ());
+ if (addr == nullptr)
+ {
+ rust_error_at (Location (),
+ "Callback registration symbol (%s) missing from "
+ "proc macro, wrong version?",
+ symbol_name.c_str ());
+ return false;
+ }
+
+ auto storage = reinterpret_cast<Symbol *> (addr);
+ *storage = callback;
+
+ return true;
+}
+
+#define REGISTER_CALLBACK(HANDLE, SYMBOL, CALLBACK) \
+ register_callback (HANDLE, SYMBOL, #SYMBOL, CALLBACK)
+
const ProcMacro::ProcmacroArray *
load_macros_array (std::string path)
{
return nullptr;
}
+ if (!REGISTER_CALLBACK (handle, __gccrs_pm_callback_from_str_fn,
+ tokenstream_from_string))
+ return nullptr;
+
// FIXME: Add CrateStableId handling, right now all versions may be loaded,
// even incompatible ones.
return *reinterpret_cast<const ProcMacro::ProcmacroArray **> (
#endif
}
+#undef REGISTER_CALLBACK
+
const std::vector<ProcMacro::Procmacro>
load_macros (std::string path)
{
--- /dev/null
+// Copyright (C) 2023 Free Software Foundation, Inc.
+//
+// This file is part of the GNU Proc Macro Library. This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// Under Section 7 of GPL version 3, you are granted additional
+// permissions described in the GCC Runtime Library Exception, version
+// 3.1, as published by the Free Software Foundation.
+
+// You should have received a copy of the GNU General Public License and
+// a copy of the GCC Runtime Library Exception along with this program;
+// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
+// <http://www.gnu.org/licenses/>.
+
+#ifndef REGISTRATION_H
+#define REGISTRATION_H
+
+#include <string>
+#include "tokenstream.h"
+
+namespace ProcMacro {
+
+using from_str_function_t = ProcMacro::TokenStream (*) (std::string &, bool &);
+
+} // namespace ProcMacro
+
+extern "C" ProcMacro::from_str_function_t __gccrs_pm_callback_from_str_fn;
+
+#endif /* !REGISTRATION_H */
#include "tokenstream.h"
#include "tokentree.h"
+#include "registration.h"
#include <cstring>
return {data, 0, capacity};
}
+TokenStream
+TokenStream::make_tokenstream (std::string &source, bool &has_error)
+{
+ return __gccrs_pm_callback_from_str_fn (source, has_error);
+}
+
void
TokenStream::grow (std::uint64_t delta)
{
TokenStream__from_string (unsigned char *str, std::uint64_t len,
TokenStream *ts)
{
- // FIXME: Implement using parser ?
- return false;
+ bool result;
+ auto source = std::string (reinterpret_cast<const char *> (str), len);
+
+ *ts = TokenStream::make_tokenstream (source, result);
+ return result;
}
extern "C" TokenStream