aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/expand/rust-proc-macro.cc
blob: 6ba87b6dd1b35c7e89fc6c5b9fcaab7b11aeb4c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// 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"
#include "rust-lex.h"
#include "rust-token-converter.h"
#ifndef _WIN32
#include <dlfcn.h>
#endif

namespace Rust {

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)
{
#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;
    }

  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 **> (
    dlsym (handle, PROC_MACRO_DECL_PREFIX.c_str ()));
#else
  rust_sorry_at (UNDEF_LOCATION,
		 "Procedural macros are not yet supported on windows host");
  rust_unreachable ();
#endif
}

#undef REGISTER_CALLBACK

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)
    rust_unreachable ();

  rust_debug ("Found %lu procedural macros", array->length);

  return std::vector<ProcMacro::Procmacro> (array->macros,
					    array->macros + array->length);
}

} // namespace Rust