Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Proc macro multi apbi proof of concept #9550

Merged
merged 4 commits into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions crates/proc_macro_srv/src/abis/abi_1_47/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//! Macro ABI for version 1.47 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 use rustc_server::TokenStream;

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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! Copy from <https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/bridge/client.rs>
//! augmented with removing unstable features

use super::super::TokenStream as CrateTokenStream;
use super::*;

macro_rules! define_handles {
Expand Down Expand Up @@ -401,26 +402,26 @@ fn run_client<A: for<'a, 's> DecodeMut<'a, 's, ()>, R: Encode<()>>(
b
}

impl Client<fn(crate::TokenStream) -> crate::TokenStream> {
pub fn expand1(f: fn(crate::TokenStream) -> crate::TokenStream) -> Self {
impl Client<fn(CrateTokenStream) -> CrateTokenStream> {
pub fn expand1(f: fn(CrateTokenStream) -> CrateTokenStream) -> Self {
extern "C" fn run(
bridge: Bridge<'_>,
f: impl FnOnce(crate::TokenStream) -> crate::TokenStream,
f: impl FnOnce(CrateTokenStream) -> CrateTokenStream,
) -> Buffer<u8> {
run_client(bridge, |input| f(crate::TokenStream(input)).0)
run_client(bridge, |input| f(CrateTokenStream(input)).0)
}
Client { get_handle_counters: HandleCounters::get, run, f }
}
}

impl Client<fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream> {
pub fn expand2(f: fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream) -> Self {
impl Client<fn(CrateTokenStream, CrateTokenStream) -> CrateTokenStream> {
pub fn expand2(f: fn(CrateTokenStream, CrateTokenStream) -> CrateTokenStream) -> Self {
extern "C" fn run(
bridge: Bridge<'_>,
f: impl FnOnce(crate::TokenStream, crate::TokenStream) -> crate::TokenStream,
f: impl FnOnce(CrateTokenStream, CrateTokenStream) -> CrateTokenStream,
) -> Buffer<u8> {
run_client(bridge, |(input, input2)| {
f(crate::TokenStream(input), crate::TokenStream(input2)).0
f(CrateTokenStream(input), CrateTokenStream(input2)).0
})
}
Client { get_handle_counters: HandleCounters::get, run, f }
Expand All @@ -433,17 +434,17 @@ pub enum ProcMacro {
CustomDerive {
trait_name: &'static str,
attributes: &'static [&'static str],
client: Client<fn(crate::TokenStream) -> crate::TokenStream>,
client: Client<fn(CrateTokenStream) -> CrateTokenStream>,
},

Attr {
name: &'static str,
client: Client<fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream>,
client: Client<fn(CrateTokenStream, CrateTokenStream) -> CrateTokenStream>,
},

Bang {
name: &'static str,
client: Client<fn(crate::TokenStream) -> crate::TokenStream>,
client: Client<fn(CrateTokenStream) -> CrateTokenStream>,
},
}

Expand All @@ -465,19 +466,19 @@ impl ProcMacro {
pub fn custom_derive(
trait_name: &'static str,
attributes: &'static [&'static str],
expand: fn(crate::TokenStream) -> crate::TokenStream,
expand: fn(CrateTokenStream) -> CrateTokenStream,
) -> Self {
ProcMacro::CustomDerive { trait_name, attributes, client: Client::expand1(expand) }
}

pub fn attr(
name: &'static str,
expand: fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream,
expand: fn(CrateTokenStream, CrateTokenStream) -> CrateTokenStream,
) -> Self {
ProcMacro::Attr { name, client: Client::expand2(expand) }
}

pub fn bang(name: &'static str, expand: fn(crate::TokenStream) -> crate::TokenStream) -> Self {
pub fn bang(name: &'static str, expand: fn(CrateTokenStream) -> CrateTokenStream) -> Self {
ProcMacro::Bang { name, client: Client::expand1(expand) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#![deny(unsafe_code)]

pub use crate::proc_macro::{Delimiter, Level, LineColumn, Spacing};
pub use super::{Delimiter, Level, LineColumn, Spacing};
use std::fmt;
use std::hash::Hash;
use std::marker;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! Copy from <https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/bridge/server.rs>
//! augmented with removing unstable features

use super::super::TokenStream as ProcMacroTokenStream;
use super::*;

// FIXME(eddyb) generate the definition of `HandleStore` in `server.rs`.
Expand Down Expand Up @@ -308,7 +309,7 @@ fn run_server<
Result::decode(&mut &b[..], &mut dispatcher.handle_store)
}

impl client::Client<fn(crate::TokenStream) -> crate::TokenStream> {
impl client::Client<fn(ProcMacroTokenStream) -> ProcMacroTokenStream> {
pub fn run<S: Server>(
&self,
strategy: &impl ExecutionStrategy,
Expand All @@ -330,7 +331,7 @@ impl client::Client<fn(crate::TokenStream) -> crate::TokenStream> {
}
}

impl client::Client<fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream> {
impl client::Client<fn(ProcMacroTokenStream, ProcMacroTokenStream) -> ProcMacroTokenStream> {
pub fn run<S: Server>(
&self,
strategy: &impl ExecutionStrategy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! Copy from <https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/diagnostic.rs>
//! augmented with removing unstable features

use crate::proc_macro::Span;
use super::Span;

/// An enum representing a diagnostic level.
#[derive(Copy, Clone, Debug)]
Expand Down Expand Up @@ -146,15 +146,15 @@ impl Diagnostic {

/// Emit the diagnostic.
pub fn emit(self) {
fn to_internal(spans: Vec<Span>) -> crate::proc_macro::bridge::client::MultiSpan {
let mut multi_span = crate::proc_macro::bridge::client::MultiSpan::new();
fn to_internal(spans: Vec<Span>) -> super::bridge::client::MultiSpan {
let mut multi_span = super::bridge::client::MultiSpan::new();
for span in spans {
multi_span.push(span.0);
}
multi_span
}

let mut diag = crate::proc_macro::bridge::client::Diagnostic::new(
let mut diag = super::bridge::client::Diagnostic::new(
self.level,
&self.message[..],
to_internal(self.spans),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl Extend<TokenStream> for TokenStream {

/// Public implementation details for the `TokenStream` type, such as iterators.
pub mod token_stream {
use crate::proc_macro::{bridge, Group, Ident, Literal, Punct, TokenStream, TokenTree};
use super::{bridge, Group, Ident, Literal, Punct, TokenStream, TokenTree};

/// An iterator over `TokenStream`'s `TokenTree`s.
/// The iteration is "shallow", e.g., the iterator doesn't recurse into delimited groups,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//!
//! FIXME: No span and source file information is implemented yet

use crate::proc_macro::bridge::{self, server};
use super::proc_macro::bridge::{self, server};

use std::collections::HashMap;
use std::hash::Hash;
Expand Down Expand Up @@ -97,9 +97,9 @@ impl Extend<TokenStream> for TokenStream {
}
}

type Level = crate::proc_macro::Level;
type LineColumn = crate::proc_macro::LineColumn;
type SourceFile = crate::proc_macro::SourceFile;
type Level = super::proc_macro::Level;
type LineColumn = super::proc_macro::LineColumn;
type SourceFile = super::proc_macro::SourceFile;

/// A structure representing a diagnostic message and associated children
/// messages.
Expand Down Expand Up @@ -734,8 +734,8 @@ impl server::MultiSpan for Rustc {

#[cfg(test)]
mod tests {
use super::super::proc_macro::bridge::server::Literal;
use super::*;
use crate::proc_macro::bridge::server::Literal;

#[test]
fn test_rustc_server_literals() {
Expand Down
Loading