-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
4,185 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
//! Macro ABI for version 1.58 of rustc | ||
#[allow(dead_code)] | ||
#[doc(hidden)] | ||
mod proc_macro; | ||
|
||
#[allow(dead_code)] | ||
#[doc(hidden)] | ||
mod rustc_server; | ||
|
||
use libloading::Library; | ||
use proc_macro_api::ProcMacroKind; | ||
|
||
use super::PanicMessage; | ||
|
||
pub(crate) struct Abi { | ||
exported_macros: Vec<proc_macro::bridge::client::ProcMacro>, | ||
} | ||
|
||
impl From<proc_macro::bridge::PanicMessage> for PanicMessage { | ||
fn from(p: proc_macro::bridge::PanicMessage) -> Self { | ||
Self { message: p.as_str().map(|s| s.to_string()) } | ||
} | ||
} | ||
|
||
impl Abi { | ||
pub unsafe fn from_lib(lib: &Library, symbol_name: String) -> Result<Abi, libloading::Error> { | ||
let macros: libloading::Symbol<&&[proc_macro::bridge::client::ProcMacro]> = | ||
lib.get(symbol_name.as_bytes())?; | ||
Ok(Self { exported_macros: macros.to_vec() }) | ||
} | ||
|
||
pub fn expand( | ||
&self, | ||
macro_name: &str, | ||
macro_body: &tt::Subtree, | ||
attributes: Option<&tt::Subtree>, | ||
) -> Result<tt::Subtree, PanicMessage> { | ||
let parsed_body = rustc_server::TokenStream::with_subtree(macro_body.clone()); | ||
|
||
let parsed_attributes = attributes.map_or(rustc_server::TokenStream::new(), |attr| { | ||
rustc_server::TokenStream::with_subtree(attr.clone()) | ||
}); | ||
|
||
for proc_macro in &self.exported_macros { | ||
match proc_macro { | ||
proc_macro::bridge::client::ProcMacro::CustomDerive { | ||
trait_name, client, .. | ||
} if *trait_name == macro_name => { | ||
let res = client.run( | ||
&proc_macro::bridge::server::SameThread, | ||
rustc_server::Rustc::default(), | ||
parsed_body, | ||
false, | ||
); | ||
return res.map(|it| it.into_subtree()).map_err(PanicMessage::from); | ||
} | ||
proc_macro::bridge::client::ProcMacro::Bang { name, client } | ||
if *name == macro_name => | ||
{ | ||
let res = client.run( | ||
&proc_macro::bridge::server::SameThread, | ||
rustc_server::Rustc::default(), | ||
parsed_body, | ||
false, | ||
); | ||
return res.map(|it| it.into_subtree()).map_err(PanicMessage::from); | ||
} | ||
proc_macro::bridge::client::ProcMacro::Attr { name, client } | ||
if *name == macro_name => | ||
{ | ||
let res = client.run( | ||
&proc_macro::bridge::server::SameThread, | ||
rustc_server::Rustc::default(), | ||
parsed_attributes, | ||
parsed_body, | ||
false, | ||
); | ||
return res.map(|it| it.into_subtree()).map_err(PanicMessage::from); | ||
} | ||
_ => continue, | ||
} | ||
} | ||
|
||
Err(proc_macro::bridge::PanicMessage::String("Nothing to expand".to_string()).into()) | ||
} | ||
|
||
pub fn list_macros(&self) -> Vec<(String, ProcMacroKind)> { | ||
self.exported_macros | ||
.iter() | ||
.map(|proc_macro| match proc_macro { | ||
proc_macro::bridge::client::ProcMacro::CustomDerive { trait_name, .. } => { | ||
(trait_name.to_string(), ProcMacroKind::CustomDerive) | ||
} | ||
proc_macro::bridge::client::ProcMacro::Bang { name, .. } => { | ||
(name.to_string(), ProcMacroKind::FuncLike) | ||
} | ||
proc_macro::bridge::client::ProcMacro::Attr { name, .. } => { | ||
(name.to_string(), ProcMacroKind::Attr) | ||
} | ||
}) | ||
.collect() | ||
} | ||
} |
143 changes: 143 additions & 0 deletions
143
crates/proc_macro_srv/src/abis/abi_1_58/proc_macro/bridge/buffer.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
//! Buffer management for same-process client<->server communication. | ||
use std::io::{self, Write}; | ||
use std::mem; | ||
use std::ops::{Deref, DerefMut}; | ||
use std::slice; | ||
|
||
#[repr(C)] | ||
pub struct Buffer<T: Copy> { | ||
data: *mut T, | ||
len: usize, | ||
capacity: usize, | ||
reserve: extern "C" fn(Buffer<T>, usize) -> Buffer<T>, | ||
drop: extern "C" fn(Buffer<T>), | ||
} | ||
|
||
unsafe impl<T: Copy + Sync> Sync for Buffer<T> {} | ||
unsafe impl<T: Copy + Send> Send for Buffer<T> {} | ||
|
||
impl<T: Copy> Default for Buffer<T> { | ||
fn default() -> Self { | ||
Self::from(vec![]) | ||
} | ||
} | ||
|
||
impl<T: Copy> Deref for Buffer<T> { | ||
type Target = [T]; | ||
fn deref(&self) -> &[T] { | ||
unsafe { slice::from_raw_parts(self.data as *const T, self.len) } | ||
} | ||
} | ||
|
||
impl<T: Copy> DerefMut for Buffer<T> { | ||
fn deref_mut(&mut self) -> &mut [T] { | ||
unsafe { slice::from_raw_parts_mut(self.data, self.len) } | ||
} | ||
} | ||
|
||
impl<T: Copy> Buffer<T> { | ||
pub(super) fn new() -> Self { | ||
Self::default() | ||
} | ||
|
||
pub(super) fn clear(&mut self) { | ||
self.len = 0; | ||
} | ||
|
||
pub(super) fn take(&mut self) -> Self { | ||
mem::take(self) | ||
} | ||
|
||
// We have the array method separate from extending from a slice. This is | ||
// because in the case of small arrays, codegen can be more efficient | ||
// (avoiding a memmove call). With extend_from_slice, LLVM at least | ||
// currently is not able to make that optimization. | ||
pub(super) fn extend_from_array<const N: usize>(&mut self, xs: &[T; N]) { | ||
if xs.len() > (self.capacity - self.len) { | ||
let b = self.take(); | ||
*self = (b.reserve)(b, xs.len()); | ||
} | ||
unsafe { | ||
xs.as_ptr().copy_to_nonoverlapping(self.data.add(self.len), xs.len()); | ||
self.len += xs.len(); | ||
} | ||
} | ||
|
||
pub(super) fn extend_from_slice(&mut self, xs: &[T]) { | ||
if xs.len() > (self.capacity - self.len) { | ||
let b = self.take(); | ||
*self = (b.reserve)(b, xs.len()); | ||
} | ||
unsafe { | ||
xs.as_ptr().copy_to_nonoverlapping(self.data.add(self.len), xs.len()); | ||
self.len += xs.len(); | ||
} | ||
} | ||
|
||
pub(super) fn push(&mut self, v: T) { | ||
// The code here is taken from Vec::push, and we know that reserve() | ||
// will panic if we're exceeding isize::MAX bytes and so there's no need | ||
// to check for overflow. | ||
if self.len == self.capacity { | ||
let b = self.take(); | ||
*self = (b.reserve)(b, 1); | ||
} | ||
unsafe { | ||
*self.data.add(self.len) = v; | ||
self.len += 1; | ||
} | ||
} | ||
} | ||
|
||
impl Write for Buffer<u8> { | ||
fn write(&mut self, xs: &[u8]) -> io::Result<usize> { | ||
self.extend_from_slice(xs); | ||
Ok(xs.len()) | ||
} | ||
|
||
fn write_all(&mut self, xs: &[u8]) -> io::Result<()> { | ||
self.extend_from_slice(xs); | ||
Ok(()) | ||
} | ||
|
||
fn flush(&mut self) -> io::Result<()> { | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl<T: Copy> Drop for Buffer<T> { | ||
fn drop(&mut self) { | ||
let b = self.take(); | ||
(b.drop)(b); | ||
} | ||
} | ||
|
||
impl<T: Copy> From<Vec<T>> for Buffer<T> { | ||
fn from(mut v: Vec<T>) -> Self { | ||
let (data, len, capacity) = (v.as_mut_ptr(), v.len(), v.capacity()); | ||
mem::forget(v); | ||
|
||
// This utility function is nested in here because it can *only* | ||
// be safely called on `Buffer`s created by *this* `proc_macro`. | ||
fn to_vec<T: Copy>(b: Buffer<T>) -> Vec<T> { | ||
unsafe { | ||
let Buffer { data, len, capacity, .. } = b; | ||
mem::forget(b); | ||
Vec::from_raw_parts(data, len, capacity) | ||
} | ||
} | ||
|
||
extern "C" fn reserve<T: Copy>(b: Buffer<T>, additional: usize) -> Buffer<T> { | ||
let mut v = to_vec(b); | ||
v.reserve(additional); | ||
Buffer::from(v) | ||
} | ||
|
||
extern "C" fn drop<T: Copy>(b: Buffer<T>) { | ||
mem::drop(to_vec(b)); | ||
} | ||
|
||
Buffer { data, len, capacity, reserve, drop } | ||
} | ||
} |
Oops, something went wrong.