Skip to content

Commit abdb2ac

Browse files
committed
Add proc macro ABI for rustc 1.58
1 parent 7366833 commit abdb2ac

File tree

14 files changed

+4185
-1
lines changed

14 files changed

+4185
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
//! Macro ABI for version 1.58 of rustc
2+
3+
#[allow(dead_code)]
4+
#[doc(hidden)]
5+
mod proc_macro;
6+
7+
#[allow(dead_code)]
8+
#[doc(hidden)]
9+
mod rustc_server;
10+
11+
use libloading::Library;
12+
use proc_macro_api::ProcMacroKind;
13+
14+
use super::PanicMessage;
15+
16+
pub(crate) struct Abi {
17+
exported_macros: Vec<proc_macro::bridge::client::ProcMacro>,
18+
}
19+
20+
impl From<proc_macro::bridge::PanicMessage> for PanicMessage {
21+
fn from(p: proc_macro::bridge::PanicMessage) -> Self {
22+
Self { message: p.as_str().map(|s| s.to_string()) }
23+
}
24+
}
25+
26+
impl Abi {
27+
pub unsafe fn from_lib(lib: &Library, symbol_name: String) -> Result<Abi, libloading::Error> {
28+
let macros: libloading::Symbol<&&[proc_macro::bridge::client::ProcMacro]> =
29+
lib.get(symbol_name.as_bytes())?;
30+
Ok(Self { exported_macros: macros.to_vec() })
31+
}
32+
33+
pub fn expand(
34+
&self,
35+
macro_name: &str,
36+
macro_body: &tt::Subtree,
37+
attributes: Option<&tt::Subtree>,
38+
) -> Result<tt::Subtree, PanicMessage> {
39+
let parsed_body = rustc_server::TokenStream::with_subtree(macro_body.clone());
40+
41+
let parsed_attributes = attributes.map_or(rustc_server::TokenStream::new(), |attr| {
42+
rustc_server::TokenStream::with_subtree(attr.clone())
43+
});
44+
45+
for proc_macro in &self.exported_macros {
46+
match proc_macro {
47+
proc_macro::bridge::client::ProcMacro::CustomDerive {
48+
trait_name, client, ..
49+
} if *trait_name == macro_name => {
50+
let res = client.run(
51+
&proc_macro::bridge::server::SameThread,
52+
rustc_server::Rustc::default(),
53+
parsed_body,
54+
false,
55+
);
56+
return res.map(|it| it.into_subtree()).map_err(PanicMessage::from);
57+
}
58+
proc_macro::bridge::client::ProcMacro::Bang { name, client }
59+
if *name == macro_name =>
60+
{
61+
let res = client.run(
62+
&proc_macro::bridge::server::SameThread,
63+
rustc_server::Rustc::default(),
64+
parsed_body,
65+
false,
66+
);
67+
return res.map(|it| it.into_subtree()).map_err(PanicMessage::from);
68+
}
69+
proc_macro::bridge::client::ProcMacro::Attr { name, client }
70+
if *name == macro_name =>
71+
{
72+
let res = client.run(
73+
&proc_macro::bridge::server::SameThread,
74+
rustc_server::Rustc::default(),
75+
parsed_attributes,
76+
parsed_body,
77+
false,
78+
);
79+
return res.map(|it| it.into_subtree()).map_err(PanicMessage::from);
80+
}
81+
_ => continue,
82+
}
83+
}
84+
85+
Err(proc_macro::bridge::PanicMessage::String("Nothing to expand".to_string()).into())
86+
}
87+
88+
pub fn list_macros(&self) -> Vec<(String, ProcMacroKind)> {
89+
self.exported_macros
90+
.iter()
91+
.map(|proc_macro| match proc_macro {
92+
proc_macro::bridge::client::ProcMacro::CustomDerive { trait_name, .. } => {
93+
(trait_name.to_string(), ProcMacroKind::CustomDerive)
94+
}
95+
proc_macro::bridge::client::ProcMacro::Bang { name, .. } => {
96+
(name.to_string(), ProcMacroKind::FuncLike)
97+
}
98+
proc_macro::bridge::client::ProcMacro::Attr { name, .. } => {
99+
(name.to_string(), ProcMacroKind::Attr)
100+
}
101+
})
102+
.collect()
103+
}
104+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
//! Buffer management for same-process client<->server communication.
2+
3+
use std::io::{self, Write};
4+
use std::mem;
5+
use std::ops::{Deref, DerefMut};
6+
use std::slice;
7+
8+
#[repr(C)]
9+
pub struct Buffer<T: Copy> {
10+
data: *mut T,
11+
len: usize,
12+
capacity: usize,
13+
reserve: extern "C" fn(Buffer<T>, usize) -> Buffer<T>,
14+
drop: extern "C" fn(Buffer<T>),
15+
}
16+
17+
unsafe impl<T: Copy + Sync> Sync for Buffer<T> {}
18+
unsafe impl<T: Copy + Send> Send for Buffer<T> {}
19+
20+
impl<T: Copy> Default for Buffer<T> {
21+
fn default() -> Self {
22+
Self::from(vec![])
23+
}
24+
}
25+
26+
impl<T: Copy> Deref for Buffer<T> {
27+
type Target = [T];
28+
fn deref(&self) -> &[T] {
29+
unsafe { slice::from_raw_parts(self.data as *const T, self.len) }
30+
}
31+
}
32+
33+
impl<T: Copy> DerefMut for Buffer<T> {
34+
fn deref_mut(&mut self) -> &mut [T] {
35+
unsafe { slice::from_raw_parts_mut(self.data, self.len) }
36+
}
37+
}
38+
39+
impl<T: Copy> Buffer<T> {
40+
pub(super) fn new() -> Self {
41+
Self::default()
42+
}
43+
44+
pub(super) fn clear(&mut self) {
45+
self.len = 0;
46+
}
47+
48+
pub(super) fn take(&mut self) -> Self {
49+
mem::take(self)
50+
}
51+
52+
// We have the array method separate from extending from a slice. This is
53+
// because in the case of small arrays, codegen can be more efficient
54+
// (avoiding a memmove call). With extend_from_slice, LLVM at least
55+
// currently is not able to make that optimization.
56+
pub(super) fn extend_from_array<const N: usize>(&mut self, xs: &[T; N]) {
57+
if xs.len() > (self.capacity - self.len) {
58+
let b = self.take();
59+
*self = (b.reserve)(b, xs.len());
60+
}
61+
unsafe {
62+
xs.as_ptr().copy_to_nonoverlapping(self.data.add(self.len), xs.len());
63+
self.len += xs.len();
64+
}
65+
}
66+
67+
pub(super) fn extend_from_slice(&mut self, xs: &[T]) {
68+
if xs.len() > (self.capacity - self.len) {
69+
let b = self.take();
70+
*self = (b.reserve)(b, xs.len());
71+
}
72+
unsafe {
73+
xs.as_ptr().copy_to_nonoverlapping(self.data.add(self.len), xs.len());
74+
self.len += xs.len();
75+
}
76+
}
77+
78+
pub(super) fn push(&mut self, v: T) {
79+
// The code here is taken from Vec::push, and we know that reserve()
80+
// will panic if we're exceeding isize::MAX bytes and so there's no need
81+
// to check for overflow.
82+
if self.len == self.capacity {
83+
let b = self.take();
84+
*self = (b.reserve)(b, 1);
85+
}
86+
unsafe {
87+
*self.data.add(self.len) = v;
88+
self.len += 1;
89+
}
90+
}
91+
}
92+
93+
impl Write for Buffer<u8> {
94+
fn write(&mut self, xs: &[u8]) -> io::Result<usize> {
95+
self.extend_from_slice(xs);
96+
Ok(xs.len())
97+
}
98+
99+
fn write_all(&mut self, xs: &[u8]) -> io::Result<()> {
100+
self.extend_from_slice(xs);
101+
Ok(())
102+
}
103+
104+
fn flush(&mut self) -> io::Result<()> {
105+
Ok(())
106+
}
107+
}
108+
109+
impl<T: Copy> Drop for Buffer<T> {
110+
fn drop(&mut self) {
111+
let b = self.take();
112+
(b.drop)(b);
113+
}
114+
}
115+
116+
impl<T: Copy> From<Vec<T>> for Buffer<T> {
117+
fn from(mut v: Vec<T>) -> Self {
118+
let (data, len, capacity) = (v.as_mut_ptr(), v.len(), v.capacity());
119+
mem::forget(v);
120+
121+
// This utility function is nested in here because it can *only*
122+
// be safely called on `Buffer`s created by *this* `proc_macro`.
123+
fn to_vec<T: Copy>(b: Buffer<T>) -> Vec<T> {
124+
unsafe {
125+
let Buffer { data, len, capacity, .. } = b;
126+
mem::forget(b);
127+
Vec::from_raw_parts(data, len, capacity)
128+
}
129+
}
130+
131+
extern "C" fn reserve<T: Copy>(b: Buffer<T>, additional: usize) -> Buffer<T> {
132+
let mut v = to_vec(b);
133+
v.reserve(additional);
134+
Buffer::from(v)
135+
}
136+
137+
extern "C" fn drop<T: Copy>(b: Buffer<T>) {
138+
mem::drop(to_vec(b));
139+
}
140+
141+
Buffer { data, len, capacity, reserve, drop }
142+
}
143+
}

0 commit comments

Comments
 (0)