diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in index 03a92ac8874a7fe2a39f47e76ccaf87b4ad1e215..2f1807912fc33012ad09e756b2937d09bad2c88e 100644 --- a/gcc/rust/Make-lang.in +++ b/gcc/rust/Make-lang.in @@ -91,6 +91,7 @@ GRS_OBJS = \ rust/rust-derive.o \ rust/rust-derive-clone.o \ rust/rust-derive-copy.o \ + rust/rust-proc-macro.o \ rust/rust-macro-invoc-lexer.o \ rust/rust-macro-substitute-ctx.o \ rust/rust-macro-builtins.o \ diff --git a/gcc/rust/expand/rust-proc-macro.cc b/gcc/rust/expand/rust-proc-macro.cc new file mode 100644 index 0000000000000000000000000000000000000000..22744cb547d2c913b94e0ee84cbfb3cd18dba98c --- /dev/null +++ b/gcc/rust/expand/rust-proc-macro.cc @@ -0,0 +1,64 @@ +// This file is part of GCC. + +// GCC 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. + +// GCC 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. + +// You should have received a copy of the GNU General Public License +// along with GCC; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#include "rust-diagnostics.h" +#include "rust-proc-macro.h" +#ifndef _WIN32 +#include <dlfcn.h> +#endif + +namespace Rust { + +const std::string PROC_MACRO_DECL_PREFIX = "__gccrs_proc_macro_decls_"; + +const ProcMacro::ProcmacroArray * +load_macros_array (std::string path) +{ +#ifndef _WIN32 + void *handle = dlopen (path.c_str (), RTLD_LAZY | RTLD_LOCAL); + // We're leaking the handle since we can't ever unload it + if (!handle) + { + rust_debug ("Error whilst opening procedural macro: %s", dlerror ()); + return nullptr; + } + + // FIXME: Add CrateStableId handling, right now all versions may be loaded, + // even incompatible ones. + return *reinterpret_cast<const ProcMacro::ProcmacroArray **> ( + dlsym (handle, PROC_MACRO_DECL_PREFIX.c_str ())); +#else + rust_sorry_at (Location (), + "Procedural macros are not yet supported on windows host"); + gcc_unreachable (); +#endif +} + +const std::vector<ProcMacro::Procmacro> +load_macros (std::string path) +{ + const ProcMacro::ProcmacroArray *array = load_macros_array (path); + // Did not load the proc macro + if (array == nullptr) + gcc_unreachable (); + + rust_debug ("Found %lu procedural macros", array->length); + + return std::vector<ProcMacro::Procmacro> (array->macros, + array->macros + array->length); +} + +} // namespace Rust diff --git a/gcc/rust/expand/rust-proc-macro.h b/gcc/rust/expand/rust-proc-macro.h new file mode 100644 index 0000000000000000000000000000000000000000..779d3c70fbc0223efd3ee27a21a6648e7dbdceb2 --- /dev/null +++ b/gcc/rust/expand/rust-proc-macro.h @@ -0,0 +1,34 @@ +// This file is part of GCC. + +// GCC 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. + +// GCC 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. + +// You should have received a copy of the GNU General Public License +// along with GCC; see the file COPYING3. If not see +// <http://www.gnu.org/licenses/>. + +#ifndef RUST_PROC_MACRO_H +#define RUST_PROC_MACRO_H + +#include "libproc_macro/proc_macro.h" + +namespace Rust { + +/** + * Load a procedural macro library and return a pointer to it's entrypoint. + * + * @param The path to the shared object file to load. + */ +const std::vector<ProcMacro::Procmacro> +load_macros (std::string path); + +} // namespace Rust + +#endif /* ! RUST_PROC_MACRO_H */