2019-10-16 10:59:30 +02:00
|
|
|
use crate::base::{self, *};
|
|
|
|
use crate::proc_macro_server;
|
|
|
|
|
2020-11-14 14:47:14 +03:00
|
|
|
use rustc_ast as ast;
|
2020-11-24 14:47:49 -05:00
|
|
|
use rustc_ast::ptr::P;
|
2020-07-01 13:16:49 +03:00
|
|
|
use rustc_ast::token;
|
2020-11-23 01:43:55 -05:00
|
|
|
use rustc_ast::tokenstream::{CanSynthesizeMissingTokens, TokenStream, TokenTree};
|
2020-01-09 11:18:47 +01:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2022-01-23 12:34:26 -06:00
|
|
|
use rustc_errors::ErrorGuaranteed;
|
2020-06-14 14:30:42 +03:00
|
|
|
use rustc_parse::nt_to_tokenstream;
|
2021-01-18 16:47:37 -05:00
|
|
|
use rustc_parse::parser::ForceCollect;
|
2020-01-09 11:18:47 +01:00
|
|
|
use rustc_span::{Span, DUMMY_SP};
|
2019-07-18 21:02:34 +03:00
|
|
|
|
2019-10-16 10:59:30 +02:00
|
|
|
const EXEC_STRATEGY: pm::bridge::server::SameThread = pm::bridge::server::SameThread;
|
2019-07-18 21:02:34 +03:00
|
|
|
|
|
|
|
pub struct BangProcMacro {
|
2019-12-22 17:42:04 -05:00
|
|
|
pub client: pm::bridge::client::Client<fn(pm::TokenStream) -> pm::TokenStream>,
|
2019-07-18 21:02:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl base::ProcMacro for BangProcMacro {
|
2019-12-22 17:42:04 -05:00
|
|
|
fn expand<'cx>(
|
|
|
|
&self,
|
|
|
|
ecx: &'cx mut ExtCtxt<'_>,
|
|
|
|
span: Span,
|
|
|
|
input: TokenStream,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> Result<TokenStream, ErrorGuaranteed> {
|
2021-07-18 15:53:06 -04:00
|
|
|
let proc_macro_backtrace = ecx.ecfg.proc_macro_backtrace;
|
2021-07-10 23:15:30 +03:00
|
|
|
let server = proc_macro_server::Rustc::new(ecx);
|
2021-07-18 15:53:06 -04:00
|
|
|
self.client.run(&EXEC_STRATEGY, server, input, proc_macro_backtrace).map_err(|e| {
|
2020-03-17 10:09:18 +01:00
|
|
|
let mut err = ecx.struct_span_err(span, "proc macro panicked");
|
|
|
|
if let Some(s) = e.as_str() {
|
|
|
|
err.help(&format!("message: {}", s));
|
2019-07-18 21:02:34 +03:00
|
|
|
}
|
2020-03-17 10:09:18 +01:00
|
|
|
err.emit();
|
2022-01-23 12:34:26 -06:00
|
|
|
ErrorGuaranteed
|
2020-03-17 10:09:18 +01:00
|
|
|
})
|
2019-07-18 21:02:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct AttrProcMacro {
|
2019-10-16 10:59:30 +02:00
|
|
|
pub client: pm::bridge::client::Client<fn(pm::TokenStream, pm::TokenStream) -> pm::TokenStream>,
|
2019-07-18 21:02:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl base::AttrProcMacro for AttrProcMacro {
|
2019-12-22 17:42:04 -05:00
|
|
|
fn expand<'cx>(
|
|
|
|
&self,
|
|
|
|
ecx: &'cx mut ExtCtxt<'_>,
|
|
|
|
span: Span,
|
|
|
|
annotation: TokenStream,
|
|
|
|
annotated: TokenStream,
|
2022-01-23 12:34:26 -06:00
|
|
|
) -> Result<TokenStream, ErrorGuaranteed> {
|
2021-07-18 15:53:06 -04:00
|
|
|
let proc_macro_backtrace = ecx.ecfg.proc_macro_backtrace;
|
2021-07-10 23:15:30 +03:00
|
|
|
let server = proc_macro_server::Rustc::new(ecx);
|
2020-08-30 22:17:24 -04:00
|
|
|
self.client
|
2021-07-18 15:53:06 -04:00
|
|
|
.run(&EXEC_STRATEGY, server, annotation, annotated, proc_macro_backtrace)
|
2020-08-30 22:17:24 -04:00
|
|
|
.map_err(|e| {
|
|
|
|
let mut err = ecx.struct_span_err(span, "custom attribute panicked");
|
|
|
|
if let Some(s) = e.as_str() {
|
|
|
|
err.help(&format!("message: {}", s));
|
|
|
|
}
|
|
|
|
err.emit();
|
2022-01-23 12:34:26 -06:00
|
|
|
ErrorGuaranteed
|
2020-08-30 22:17:24 -04:00
|
|
|
})
|
2019-07-18 21:02:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ProcMacroDerive {
|
2019-10-16 10:59:30 +02:00
|
|
|
pub client: pm::bridge::client::Client<fn(pm::TokenStream) -> pm::TokenStream>,
|
2019-07-18 21:02:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MultiItemModifier for ProcMacroDerive {
|
2019-12-22 17:42:04 -05:00
|
|
|
fn expand(
|
|
|
|
&self,
|
|
|
|
ecx: &mut ExtCtxt<'_>,
|
|
|
|
span: Span,
|
|
|
|
_meta_item: &ast::MetaItem,
|
|
|
|
item: Annotatable,
|
2020-03-09 20:50:12 +03:00
|
|
|
) -> ExpandResult<Vec<Annotatable>, Annotatable> {
|
2020-11-24 14:47:49 -05:00
|
|
|
// We need special handling for statement items
|
|
|
|
// (e.g. `fn foo() { #[derive(Debug)] struct Bar; }`)
|
|
|
|
let mut is_stmt = false;
|
2019-07-18 21:02:34 +03:00
|
|
|
let item = match item {
|
2020-11-19 01:55:59 +03:00
|
|
|
Annotatable::Item(item) => token::NtItem(item),
|
2020-11-24 14:47:49 -05:00
|
|
|
Annotatable::Stmt(stmt) => {
|
|
|
|
is_stmt = true;
|
|
|
|
assert!(stmt.is_item());
|
|
|
|
|
|
|
|
// A proc macro can't observe the fact that we're passing
|
|
|
|
// them an `NtStmt` - it can only see the underlying tokens
|
|
|
|
// of the wrapped item
|
|
|
|
token::NtStmt(stmt.into_inner())
|
|
|
|
}
|
2020-11-19 01:55:59 +03:00
|
|
|
_ => unreachable!(),
|
2019-07-18 21:02:34 +03:00
|
|
|
};
|
2021-03-15 15:54:25 -04:00
|
|
|
let input = if crate::base::pretty_printing_compatibility_hack(&item, &ecx.sess.parse_sess)
|
|
|
|
{
|
2020-07-01 13:16:49 +03:00
|
|
|
TokenTree::token(token::Interpolated(Lrc::new(item)), DUMMY_SP).into()
|
2020-06-14 14:30:42 +03:00
|
|
|
} else {
|
2020-11-28 18:33:17 -05:00
|
|
|
nt_to_tokenstream(&item, &ecx.sess.parse_sess, CanSynthesizeMissingTokens::No)
|
2020-06-14 14:30:42 +03:00
|
|
|
};
|
2019-07-18 21:02:34 +03:00
|
|
|
|
2021-07-18 15:53:06 -04:00
|
|
|
let proc_macro_backtrace = ecx.ecfg.proc_macro_backtrace;
|
2021-07-10 23:15:30 +03:00
|
|
|
let server = proc_macro_server::Rustc::new(ecx);
|
2021-07-18 15:53:06 -04:00
|
|
|
let stream = match self.client.run(&EXEC_STRATEGY, server, input, proc_macro_backtrace) {
|
|
|
|
Ok(stream) => stream,
|
|
|
|
Err(e) => {
|
|
|
|
let mut err = ecx.struct_span_err(span, "proc-macro derive panicked");
|
|
|
|
if let Some(s) = e.as_str() {
|
|
|
|
err.help(&format!("message: {}", s));
|
2019-07-18 21:02:34 +03:00
|
|
|
}
|
2021-07-18 15:53:06 -04:00
|
|
|
err.emit();
|
|
|
|
return ExpandResult::Ready(vec![]);
|
|
|
|
}
|
|
|
|
};
|
2019-07-18 21:02:34 +03:00
|
|
|
|
2020-07-30 11:27:50 +10:00
|
|
|
let error_count_before = ecx.sess.parse_sess.span_diagnostic.err_count();
|
2019-12-22 17:42:04 -05:00
|
|
|
let mut parser =
|
2020-07-30 11:27:50 +10:00
|
|
|
rustc_parse::stream_to_parser(&ecx.sess.parse_sess, stream, Some("proc-macro derive"));
|
2019-07-18 21:02:34 +03:00
|
|
|
let mut items = vec![];
|
|
|
|
|
|
|
|
loop {
|
2021-01-18 16:47:37 -05:00
|
|
|
match parser.parse_item(ForceCollect::No) {
|
2019-07-18 21:02:34 +03:00
|
|
|
Ok(None) => break,
|
2020-11-24 14:47:49 -05:00
|
|
|
Ok(Some(item)) => {
|
|
|
|
if is_stmt {
|
|
|
|
items.push(Annotatable::Stmt(P(ecx.stmt_item(span, item))));
|
|
|
|
} else {
|
|
|
|
items.push(Annotatable::Item(item));
|
|
|
|
}
|
|
|
|
}
|
2019-07-18 21:02:34 +03:00
|
|
|
Err(mut err) => {
|
2020-03-17 11:30:53 +01:00
|
|
|
err.emit();
|
|
|
|
break;
|
2019-07-18 21:02:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fail if there have been errors emitted
|
2020-07-30 11:27:50 +10:00
|
|
|
if ecx.sess.parse_sess.span_diagnostic.err_count() > error_count_before {
|
2020-03-17 11:30:53 +01:00
|
|
|
ecx.struct_span_err(span, "proc-macro derive produced unparseable tokens").emit();
|
2019-07-18 21:02:34 +03:00
|
|
|
}
|
|
|
|
|
2020-03-09 20:50:12 +03:00
|
|
|
ExpandResult::Ready(items)
|
2019-07-18 21:02:34 +03:00
|
|
|
}
|
|
|
|
}
|