From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7905) id 0DB6D385828E; Tue, 16 Jan 2024 17:50:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0DB6D385828E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1705427447; bh=GVHbc4IUqa/H7i1mC1gyvBuabOFvh5bKE1pFQN+Zmtc=; h=From:To:Subject:Date:From; b=bz70wwvZiMJ67TTPMSUMDzrY8Tpok6C8Vc+y4Am84otWG3X6TIDCLJ0JvwxjrG0OA BrWz8hfprYDa3zd8+7sqt4+czEZFHIqccTilNvutQruPoksP+TC1r+hFoRdpcuOWb/ WeRw/+bNdukCO6e9af0q2uSpIUnU/Lz/wPAPFplk= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Arthur Cohen To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-7652] gccrs: expand: Add procmacro loading function X-Act-Checkin: gcc X-Git-Author: Pierre-Emmanuel Patry X-Git-Refname: refs/heads/trunk X-Git-Oldrev: 4ca2f2030048e0a0121a32ad218c0e3f7a327191 X-Git-Newrev: 1f4660d5dba3a6e02aec5f95e8ec7b223ebb9730 Message-Id: <20240116175047.0DB6D385828E@sourceware.org> Date: Tue, 16 Jan 2024 17:50:47 +0000 (GMT) List-Id: https://gcc.gnu.org/g:1f4660d5dba3a6e02aec5f95e8ec7b223ebb9730 commit r14-7652-g1f4660d5dba3a6e02aec5f95e8ec7b223ebb9730 Author: Pierre-Emmanuel Patry Date: Mon May 15 15:23:50 2023 +0200 gccrs: expand: Add procmacro loading function Add a function to load a shared object procmacro and retrieve the list of available procedural macros. gcc/rust/ChangeLog: * Make-lang.in: Add rust-proc-macro object to list. * expand/rust-proc-macro.cc: New file. * expand/rust-proc-macro.h: New file. Signed-off-by: Pierre-Emmanuel Patry Diff: --- gcc/rust/Make-lang.in | 1 + gcc/rust/expand/rust-proc-macro.cc | 64 ++++++++++++++++++++++++++++++++++++++ gcc/rust/expand/rust-proc-macro.h | 34 ++++++++++++++++++++ 3 files changed, 99 insertions(+) diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in index 03a92ac8874..2f1807912fc 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 00000000000..22744cb547d --- /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 +// . + +#include "rust-diagnostics.h" +#include "rust-proc-macro.h" +#ifndef _WIN32 +#include +#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 ( + 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 +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 (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 00000000000..779d3c70fbc --- /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 +// . + +#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 +load_macros (std::string path); + +} // namespace Rust + +#endif /* ! RUST_PROC_MACRO_H */