Rollup merge of #22875 - kmcallister:maceager, r=sfackler
MacEager is a MacResult implementation for the common case where you've already built each form of AST that you might return. Fixes #17637. Based on #18814. This is a [breaking-change] for syntax extensions: * MacExpr::new becomes MacEager::expr. * MacPat::new becomes MacEager::pat. * MacItems::new becomes MacEager::items. It takes a SmallVector directly, not an iterator. r? @sfackler
This commit is contained in:
commit
37760c1f25
@ -71,8 +71,8 @@ extern crate rustc;
|
|||||||
use syntax::codemap::Span;
|
use syntax::codemap::Span;
|
||||||
use syntax::parse::token;
|
use syntax::parse::token;
|
||||||
use syntax::ast::{TokenTree, TtToken};
|
use syntax::ast::{TokenTree, TtToken};
|
||||||
use syntax::ext::base::{ExtCtxt, MacResult, DummyResult, MacExpr};
|
use syntax::ext::base::{ExtCtxt, MacResult, DummyResult, MacEager};
|
||||||
use syntax::ext::build::AstBuilder; // trait for expr_uint
|
use syntax::ext::build::AstBuilder; // trait for expr_usize
|
||||||
use rustc::plugin::Registry;
|
use rustc::plugin::Registry;
|
||||||
|
|
||||||
fn expand_rn(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree])
|
fn expand_rn(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree])
|
||||||
@ -107,7 +107,7 @@ fn expand_rn(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MacExpr::new(cx.expr_uint(sp, total))
|
MacEager::expr(cx.expr_usize(sp, total))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[plugin_registrar]
|
#[plugin_registrar]
|
||||||
@ -183,7 +183,7 @@ with
|
|||||||
[`syntax::print::pprust::*_to_string`](http://doc.rust-lang.org/syntax/print/pprust/index.html#functions).
|
[`syntax::print::pprust::*_to_string`](http://doc.rust-lang.org/syntax/print/pprust/index.html#functions).
|
||||||
|
|
||||||
The example above produced an integer literal using
|
The example above produced an integer literal using
|
||||||
[`AstBuilder::expr_uint`](../syntax/ext/build/trait.AstBuilder.html#tymethod.expr_uint).
|
[`AstBuilder::expr_usize`](../syntax/ext/build/trait.AstBuilder.html#tymethod.expr_usize).
|
||||||
As an alternative to the `AstBuilder` trait, `libsyntax` provides a set of
|
As an alternative to the `AstBuilder` trait, `libsyntax` provides a set of
|
||||||
[quasiquote macros](../syntax/ext/quote/index.html). They are undocumented and
|
[quasiquote macros](../syntax/ext/quote/index.html). They are undocumented and
|
||||||
very rough around the edges. However, the implementation may be a good
|
very rough around the edges. However, the implementation may be a good
|
||||||
|
@ -14,10 +14,11 @@ use std::collections::BTreeMap;
|
|||||||
use ast;
|
use ast;
|
||||||
use ast::{Ident, Name, TokenTree};
|
use ast::{Ident, Name, TokenTree};
|
||||||
use codemap::Span;
|
use codemap::Span;
|
||||||
use ext::base::{ExtCtxt, MacExpr, MacResult, MacItems};
|
use ext::base::{ExtCtxt, MacEager, MacResult};
|
||||||
use ext::build::AstBuilder;
|
use ext::build::AstBuilder;
|
||||||
use parse::token;
|
use parse::token;
|
||||||
use ptr::P;
|
use ptr::P;
|
||||||
|
use util::small_vector::SmallVector;
|
||||||
|
|
||||||
thread_local! {
|
thread_local! {
|
||||||
static REGISTERED_DIAGNOSTICS: RefCell<BTreeMap<Name, Option<Name>>> = {
|
static REGISTERED_DIAGNOSTICS: RefCell<BTreeMap<Name, Option<Name>>> = {
|
||||||
@ -73,7 +74,7 @@ pub fn expand_diagnostic_used<'cx>(ecx: &'cx mut ExtCtxt,
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
MacExpr::new(quote_expr!(ecx, ()))
|
MacEager::expr(quote_expr!(ecx, ()))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt,
|
pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt,
|
||||||
@ -101,7 +102,7 @@ pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt,
|
|||||||
let sym = Ident::new(token::gensym(&(
|
let sym = Ident::new(token::gensym(&(
|
||||||
"__register_diagnostic_".to_string() + &token::get_ident(*code)
|
"__register_diagnostic_".to_string() + &token::get_ident(*code)
|
||||||
)));
|
)));
|
||||||
MacItems::new(vec![quote_item!(ecx, mod $sym {}).unwrap()].into_iter())
|
MacEager::items(SmallVector::many(vec![quote_item!(ecx, mod $sym {}).unwrap()]))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn expand_build_diagnostic_array<'cx>(ecx: &'cx mut ExtCtxt,
|
pub fn expand_build_diagnostic_array<'cx>(ecx: &'cx mut ExtCtxt,
|
||||||
@ -126,7 +127,7 @@ pub fn expand_build_diagnostic_array<'cx>(ecx: &'cx mut ExtCtxt,
|
|||||||
(descriptions.len(), ecx.expr_vec(span, descriptions))
|
(descriptions.len(), ecx.expr_vec(span, descriptions))
|
||||||
});
|
});
|
||||||
|
|
||||||
MacItems::new(vec![quote_item!(ecx,
|
MacEager::items(SmallVector::many(vec![quote_item!(ecx,
|
||||||
pub static $name: [(&'static str, &'static str); $count] = $expr;
|
pub static $name: [(&'static str, &'static str); $count] = $expr;
|
||||||
).unwrap()].into_iter())
|
).unwrap()]))
|
||||||
}
|
}
|
||||||
|
@ -217,7 +217,7 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
MacExpr::new(P(ast::Expr {
|
MacEager::expr(P(ast::Expr {
|
||||||
id: ast::DUMMY_NODE_ID,
|
id: ast::DUMMY_NODE_ID,
|
||||||
node: ast::ExprInlineAsm(ast::InlineAsm {
|
node: ast::ExprInlineAsm(ast::InlineAsm {
|
||||||
asm: token::intern_and_get_ident(&asm),
|
asm: token::intern_and_get_ident(&asm),
|
||||||
|
@ -28,6 +28,7 @@ use fold::Folder;
|
|||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
use std::default::Default;
|
||||||
|
|
||||||
pub trait ItemDecorator {
|
pub trait ItemDecorator {
|
||||||
fn expand(&self,
|
fn expand(&self,
|
||||||
@ -226,9 +227,17 @@ impl<F> IdentMacroExpander for F
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Use a macro because forwarding to a simple function has type system issues
|
||||||
|
macro_rules! make_stmt_default {
|
||||||
|
($me:expr) => {
|
||||||
|
$me.make_expr().map(|e| {
|
||||||
|
P(codemap::respan(e.span, ast::StmtExpr(e, ast::DUMMY_NODE_ID)))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// The result of a macro expansion. The return values of the various
|
/// The result of a macro expansion. The return values of the various
|
||||||
/// methods are spliced into the AST at the callsite of the macro (or
|
/// methods are spliced into the AST at the callsite of the macro.
|
||||||
/// just into the compiler's internal macro table, for `make_def`).
|
|
||||||
pub trait MacResult {
|
pub trait MacResult {
|
||||||
/// Create an expression.
|
/// Create an expression.
|
||||||
fn make_expr(self: Box<Self>) -> Option<P<ast::Expr>> {
|
fn make_expr(self: Box<Self>) -> Option<P<ast::Expr>> {
|
||||||
@ -254,63 +263,76 @@ pub trait MacResult {
|
|||||||
/// By default this attempts to create an expression statement,
|
/// By default this attempts to create an expression statement,
|
||||||
/// returning None if that fails.
|
/// returning None if that fails.
|
||||||
fn make_stmt(self: Box<Self>) -> Option<P<ast::Stmt>> {
|
fn make_stmt(self: Box<Self>) -> Option<P<ast::Stmt>> {
|
||||||
self.make_expr()
|
make_stmt_default!(self)
|
||||||
.map(|e| P(codemap::respan(e.span, ast::StmtExpr(e, ast::DUMMY_NODE_ID))))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A convenience type for macros that return a single expression.
|
macro_rules! make_MacEager {
|
||||||
pub struct MacExpr {
|
( $( $fld:ident: $t:ty, )* ) => {
|
||||||
e: P<ast::Expr>
|
/// `MacResult` implementation for the common case where you've already
|
||||||
}
|
/// built each form of AST that you might return.
|
||||||
impl MacExpr {
|
#[derive(Default)]
|
||||||
pub fn new(e: P<ast::Expr>) -> Box<MacResult+'static> {
|
pub struct MacEager {
|
||||||
box MacExpr { e: e } as Box<MacResult+'static>
|
$(
|
||||||
}
|
pub $fld: Option<$t>,
|
||||||
}
|
)*
|
||||||
impl MacResult for MacExpr {
|
}
|
||||||
fn make_expr(self: Box<MacExpr>) -> Option<P<ast::Expr>> {
|
|
||||||
Some(self.e)
|
impl MacEager {
|
||||||
}
|
$(
|
||||||
fn make_pat(self: Box<MacExpr>) -> Option<P<ast::Pat>> {
|
pub fn $fld(v: $t) -> Box<MacResult> {
|
||||||
match self.e.node {
|
box MacEager {
|
||||||
ast::ExprLit(_) => Some(P(ast::Pat {
|
$fld: Some(v),
|
||||||
id: ast::DUMMY_NODE_ID,
|
..Default::default()
|
||||||
span: self.e.span,
|
} as Box<MacResult>
|
||||||
node: ast::PatLit(self.e)
|
}
|
||||||
})),
|
)*
|
||||||
_ => None
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// A convenience type for macros that return a single pattern.
|
|
||||||
pub struct MacPat {
|
make_MacEager! {
|
||||||
p: P<ast::Pat>
|
expr: P<ast::Expr>,
|
||||||
}
|
pat: P<ast::Pat>,
|
||||||
impl MacPat {
|
items: SmallVector<P<ast::Item>>,
|
||||||
pub fn new(p: P<ast::Pat>) -> Box<MacResult+'static> {
|
methods: SmallVector<P<ast::Method>>,
|
||||||
box MacPat { p: p } as Box<MacResult+'static>
|
stmt: P<ast::Stmt>,
|
||||||
}
|
|
||||||
}
|
|
||||||
impl MacResult for MacPat {
|
|
||||||
fn make_pat(self: Box<MacPat>) -> Option<P<ast::Pat>> {
|
|
||||||
Some(self.p)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/// A type for macros that return multiple items.
|
|
||||||
pub struct MacItems {
|
|
||||||
items: SmallVector<P<ast::Item>>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MacItems {
|
impl MacResult for MacEager {
|
||||||
pub fn new<I: Iterator<Item=P<ast::Item>>>(it: I) -> Box<MacResult+'static> {
|
fn make_expr(self: Box<Self>) -> Option<P<ast::Expr>> {
|
||||||
box MacItems { items: it.collect() } as Box<MacResult+'static>
|
self.expr
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl MacResult for MacItems {
|
fn make_items(self: Box<Self>) -> Option<SmallVector<P<ast::Item>>> {
|
||||||
fn make_items(self: Box<MacItems>) -> Option<SmallVector<P<ast::Item>>> {
|
self.items
|
||||||
Some(self.items)
|
}
|
||||||
|
|
||||||
|
fn make_methods(self: Box<Self>) -> Option<SmallVector<P<ast::Method>>> {
|
||||||
|
self.methods
|
||||||
|
}
|
||||||
|
|
||||||
|
fn make_stmt(self: Box<Self>) -> Option<P<ast::Stmt>> {
|
||||||
|
match self.stmt {
|
||||||
|
None => make_stmt_default!(self),
|
||||||
|
s => s,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn make_pat(self: Box<Self>) -> Option<P<ast::Pat>> {
|
||||||
|
if let Some(p) = self.pat {
|
||||||
|
return Some(p);
|
||||||
|
}
|
||||||
|
if let Some(e) = self.expr {
|
||||||
|
if let ast::ExprLit(_) = e.node {
|
||||||
|
return Some(P(ast::Pat {
|
||||||
|
id: ast::DUMMY_NODE_ID,
|
||||||
|
span: e.span,
|
||||||
|
node: ast::PatLit(e),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,5 +35,5 @@ pub fn expand_cfg<'cx>(cx: &mut ExtCtxt,
|
|||||||
}
|
}
|
||||||
|
|
||||||
let matches_cfg = attr::cfg_matches(&cx.parse_sess.span_diagnostic, &cx.cfg, &*cfg);
|
let matches_cfg = attr::cfg_matches(&cx.parse_sess.span_diagnostic, &cx.cfg, &*cfg);
|
||||||
MacExpr::new(cx.expr_bool(sp, matches_cfg))
|
MacEager::expr(cx.expr_bool(sp, matches_cfg))
|
||||||
}
|
}
|
||||||
|
@ -60,7 +60,7 @@ pub fn expand_syntax_ext(cx: &mut base::ExtCtxt,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
base::MacExpr::new(cx.expr_str(
|
base::MacEager::expr(cx.expr_str(
|
||||||
sp,
|
sp,
|
||||||
token::intern_and_get_ident(&accumulator[..])))
|
token::intern_and_get_ident(&accumulator[..])))
|
||||||
}
|
}
|
||||||
|
@ -67,5 +67,5 @@ pub fn expand_syntax_ext<'cx>(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]
|
|||||||
),
|
),
|
||||||
span: sp,
|
span: sp,
|
||||||
});
|
});
|
||||||
MacExpr::new(e)
|
MacEager::expr(e)
|
||||||
}
|
}
|
||||||
|
@ -59,7 +59,7 @@ pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenT
|
|||||||
&s[..]))))
|
&s[..]))))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
MacExpr::new(e)
|
MacEager::expr(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn expand_env<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
pub fn expand_env<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
||||||
@ -108,5 +108,5 @@ pub fn expand_env<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
|||||||
}
|
}
|
||||||
Ok(s) => cx.expr_str(sp, token::intern_and_get_ident(&s))
|
Ok(s) => cx.expr_str(sp, token::intern_and_get_ident(&s))
|
||||||
};
|
};
|
||||||
MacExpr::new(e)
|
MacEager::expr(e)
|
||||||
}
|
}
|
||||||
|
@ -633,7 +633,7 @@ pub fn expand_format_args<'cx>(ecx: &'cx mut ExtCtxt, sp: Span,
|
|||||||
|
|
||||||
match parse_args(ecx, sp, tts) {
|
match parse_args(ecx, sp, tts) {
|
||||||
Some((efmt, args, order, names)) => {
|
Some((efmt, args, order, names)) => {
|
||||||
MacExpr::new(expand_preparsed_format_args(ecx, sp, efmt,
|
MacEager::expr(expand_preparsed_format_args(ecx, sp, efmt,
|
||||||
args, order, names))
|
args, order, names))
|
||||||
}
|
}
|
||||||
None => DummyResult::expr(sp)
|
None => DummyResult::expr(sp)
|
||||||
|
@ -402,7 +402,7 @@ pub fn expand_quote_tokens<'cx>(cx: &'cx mut ExtCtxt,
|
|||||||
-> Box<base::MacResult+'cx> {
|
-> Box<base::MacResult+'cx> {
|
||||||
let (cx_expr, expr) = expand_tts(cx, sp, tts);
|
let (cx_expr, expr) = expand_tts(cx, sp, tts);
|
||||||
let expanded = expand_wrapper(cx, sp, cx_expr, expr);
|
let expanded = expand_wrapper(cx, sp, cx_expr, expr);
|
||||||
base::MacExpr::new(expanded)
|
base::MacEager::expr(expanded)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn expand_quote_expr<'cx>(cx: &'cx mut ExtCtxt,
|
pub fn expand_quote_expr<'cx>(cx: &'cx mut ExtCtxt,
|
||||||
@ -410,7 +410,7 @@ pub fn expand_quote_expr<'cx>(cx: &'cx mut ExtCtxt,
|
|||||||
tts: &[ast::TokenTree])
|
tts: &[ast::TokenTree])
|
||||||
-> Box<base::MacResult+'cx> {
|
-> Box<base::MacResult+'cx> {
|
||||||
let expanded = expand_parse_call(cx, sp, "parse_expr", Vec::new(), tts);
|
let expanded = expand_parse_call(cx, sp, "parse_expr", Vec::new(), tts);
|
||||||
base::MacExpr::new(expanded)
|
base::MacEager::expr(expanded)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn expand_quote_item<'cx>(cx: &mut ExtCtxt,
|
pub fn expand_quote_item<'cx>(cx: &mut ExtCtxt,
|
||||||
@ -419,7 +419,7 @@ pub fn expand_quote_item<'cx>(cx: &mut ExtCtxt,
|
|||||||
-> Box<base::MacResult+'cx> {
|
-> Box<base::MacResult+'cx> {
|
||||||
let expanded = expand_parse_call(cx, sp, "parse_item_with_outer_attributes",
|
let expanded = expand_parse_call(cx, sp, "parse_item_with_outer_attributes",
|
||||||
vec!(), tts);
|
vec!(), tts);
|
||||||
base::MacExpr::new(expanded)
|
base::MacEager::expr(expanded)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn expand_quote_pat<'cx>(cx: &'cx mut ExtCtxt,
|
pub fn expand_quote_pat<'cx>(cx: &'cx mut ExtCtxt,
|
||||||
@ -427,7 +427,7 @@ pub fn expand_quote_pat<'cx>(cx: &'cx mut ExtCtxt,
|
|||||||
tts: &[ast::TokenTree])
|
tts: &[ast::TokenTree])
|
||||||
-> Box<base::MacResult+'cx> {
|
-> Box<base::MacResult+'cx> {
|
||||||
let expanded = expand_parse_call(cx, sp, "parse_pat", vec!(), tts);
|
let expanded = expand_parse_call(cx, sp, "parse_pat", vec!(), tts);
|
||||||
base::MacExpr::new(expanded)
|
base::MacEager::expr(expanded)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn expand_quote_arm(cx: &mut ExtCtxt,
|
pub fn expand_quote_arm(cx: &mut ExtCtxt,
|
||||||
@ -435,7 +435,7 @@ pub fn expand_quote_arm(cx: &mut ExtCtxt,
|
|||||||
tts: &[ast::TokenTree])
|
tts: &[ast::TokenTree])
|
||||||
-> Box<base::MacResult+'static> {
|
-> Box<base::MacResult+'static> {
|
||||||
let expanded = expand_parse_call(cx, sp, "parse_arm", vec!(), tts);
|
let expanded = expand_parse_call(cx, sp, "parse_arm", vec!(), tts);
|
||||||
base::MacExpr::new(expanded)
|
base::MacEager::expr(expanded)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn expand_quote_ty(cx: &mut ExtCtxt,
|
pub fn expand_quote_ty(cx: &mut ExtCtxt,
|
||||||
@ -443,7 +443,7 @@ pub fn expand_quote_ty(cx: &mut ExtCtxt,
|
|||||||
tts: &[ast::TokenTree])
|
tts: &[ast::TokenTree])
|
||||||
-> Box<base::MacResult+'static> {
|
-> Box<base::MacResult+'static> {
|
||||||
let expanded = expand_parse_call(cx, sp, "parse_ty", vec!(), tts);
|
let expanded = expand_parse_call(cx, sp, "parse_ty", vec!(), tts);
|
||||||
base::MacExpr::new(expanded)
|
base::MacEager::expr(expanded)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn expand_quote_method(cx: &mut ExtCtxt,
|
pub fn expand_quote_method(cx: &mut ExtCtxt,
|
||||||
@ -452,7 +452,7 @@ pub fn expand_quote_method(cx: &mut ExtCtxt,
|
|||||||
-> Box<base::MacResult+'static> {
|
-> Box<base::MacResult+'static> {
|
||||||
let expanded = expand_parse_call(cx, sp, "parse_method_with_outer_attributes",
|
let expanded = expand_parse_call(cx, sp, "parse_method_with_outer_attributes",
|
||||||
vec!(), tts);
|
vec!(), tts);
|
||||||
base::MacExpr::new(expanded)
|
base::MacEager::expr(expanded)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn expand_quote_stmt(cx: &mut ExtCtxt,
|
pub fn expand_quote_stmt(cx: &mut ExtCtxt,
|
||||||
@ -462,7 +462,7 @@ pub fn expand_quote_stmt(cx: &mut ExtCtxt,
|
|||||||
let e_attrs = cx.expr_vec_ng(sp);
|
let e_attrs = cx.expr_vec_ng(sp);
|
||||||
let expanded = expand_parse_call(cx, sp, "parse_stmt",
|
let expanded = expand_parse_call(cx, sp, "parse_stmt",
|
||||||
vec!(e_attrs), tts);
|
vec!(e_attrs), tts);
|
||||||
base::MacExpr::new(expanded)
|
base::MacEager::expr(expanded)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ids_ext(strs: Vec<String> ) -> Vec<ast::Ident> {
|
fn ids_ext(strs: Vec<String> ) -> Vec<ast::Ident> {
|
||||||
|
@ -35,7 +35,7 @@ pub fn expand_line(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
|||||||
let topmost = cx.original_span_in_file();
|
let topmost = cx.original_span_in_file();
|
||||||
let loc = cx.codemap().lookup_char_pos(topmost.lo);
|
let loc = cx.codemap().lookup_char_pos(topmost.lo);
|
||||||
|
|
||||||
base::MacExpr::new(cx.expr_u32(topmost, loc.line as u32))
|
base::MacEager::expr(cx.expr_u32(topmost, loc.line as u32))
|
||||||
}
|
}
|
||||||
|
|
||||||
/* column!(): expands to the current column number */
|
/* column!(): expands to the current column number */
|
||||||
@ -46,7 +46,7 @@ pub fn expand_column(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
|||||||
let topmost = cx.original_span_in_file();
|
let topmost = cx.original_span_in_file();
|
||||||
let loc = cx.codemap().lookup_char_pos(topmost.lo);
|
let loc = cx.codemap().lookup_char_pos(topmost.lo);
|
||||||
|
|
||||||
base::MacExpr::new(cx.expr_u32(topmost, loc.col.to_usize() as u32))
|
base::MacEager::expr(cx.expr_u32(topmost, loc.col.to_usize() as u32))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// file!(): expands to the current filename */
|
/// file!(): expands to the current filename */
|
||||||
@ -59,13 +59,13 @@ pub fn expand_file(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
|||||||
let topmost = cx.original_span_in_file();
|
let topmost = cx.original_span_in_file();
|
||||||
let loc = cx.codemap().lookup_char_pos(topmost.lo);
|
let loc = cx.codemap().lookup_char_pos(topmost.lo);
|
||||||
let filename = token::intern_and_get_ident(&loc.file.name);
|
let filename = token::intern_and_get_ident(&loc.file.name);
|
||||||
base::MacExpr::new(cx.expr_str(topmost, filename))
|
base::MacEager::expr(cx.expr_str(topmost, filename))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn expand_stringify(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
pub fn expand_stringify(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
||||||
-> Box<base::MacResult+'static> {
|
-> Box<base::MacResult+'static> {
|
||||||
let s = pprust::tts_to_string(tts);
|
let s = pprust::tts_to_string(tts);
|
||||||
base::MacExpr::new(cx.expr_str(sp,
|
base::MacEager::expr(cx.expr_str(sp,
|
||||||
token::intern_and_get_ident(&s[..])))
|
token::intern_and_get_ident(&s[..])))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,7 +77,7 @@ pub fn expand_mod(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
|||||||
.map(|x| token::get_ident(*x).to_string())
|
.map(|x| token::get_ident(*x).to_string())
|
||||||
.collect::<Vec<String>>()
|
.collect::<Vec<String>>()
|
||||||
.connect("::");
|
.connect("::");
|
||||||
base::MacExpr::new(cx.expr_str(
|
base::MacEager::expr(cx.expr_str(
|
||||||
sp,
|
sp,
|
||||||
token::intern_and_get_ident(&string[..])))
|
token::intern_and_get_ident(&string[..])))
|
||||||
}
|
}
|
||||||
@ -155,7 +155,7 @@ pub fn expand_include_str(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
|||||||
let interned = token::intern_and_get_ident(&src[..]);
|
let interned = token::intern_and_get_ident(&src[..]);
|
||||||
cx.codemap().new_filemap(filename, src);
|
cx.codemap().new_filemap(filename, src);
|
||||||
|
|
||||||
base::MacExpr::new(cx.expr_str(sp, interned))
|
base::MacEager::expr(cx.expr_str(sp, interned))
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
cx.span_err(sp,
|
cx.span_err(sp,
|
||||||
@ -181,7 +181,7 @@ pub fn expand_include_bytes(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
|||||||
}
|
}
|
||||||
Ok(bytes) => {
|
Ok(bytes) => {
|
||||||
let bytes = bytes.iter().cloned().collect();
|
let bytes = bytes.iter().cloned().collect();
|
||||||
base::MacExpr::new(cx.expr_lit(sp, ast::LitBinary(Rc::new(bytes))))
|
base::MacEager::expr(cx.expr_lit(sp, ast::LitBinary(Rc::new(bytes))))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,8 @@ extern crate rustc;
|
|||||||
|
|
||||||
use syntax::ast;
|
use syntax::ast;
|
||||||
use syntax::codemap;
|
use syntax::codemap;
|
||||||
use syntax::ext::base::{ExtCtxt, MacResult, MacItems};
|
use syntax::ext::base::{ExtCtxt, MacResult, MacEager};
|
||||||
|
use syntax::util::small_vector::SmallVector;
|
||||||
use rustc::plugin::Registry;
|
use rustc::plugin::Registry;
|
||||||
|
|
||||||
#[plugin_registrar]
|
#[plugin_registrar]
|
||||||
@ -28,8 +29,8 @@ pub fn plugin_registrar(reg: &mut Registry) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn expand(cx: &mut ExtCtxt, _: codemap::Span, _: &[ast::TokenTree]) -> Box<MacResult+'static> {
|
fn expand(cx: &mut ExtCtxt, _: codemap::Span, _: &[ast::TokenTree]) -> Box<MacResult+'static> {
|
||||||
MacItems::new(vec![
|
MacEager::items(SmallVector::many(vec![
|
||||||
quote_item!(cx, struct Struct1;).unwrap(),
|
quote_item!(cx, struct Struct1;).unwrap(),
|
||||||
quote_item!(cx, struct Struct2;).unwrap()
|
quote_item!(cx, struct Struct2;).unwrap()
|
||||||
].into_iter())
|
]))
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ fn expand_make_a_1(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree])
|
|||||||
if !tts.is_empty() {
|
if !tts.is_empty() {
|
||||||
cx.span_fatal(sp, "make_a_1 takes no arguments");
|
cx.span_fatal(sp, "make_a_1 takes no arguments");
|
||||||
}
|
}
|
||||||
MacExpr::new(quote_expr!(cx, 1))
|
MacEager::expr(quote_expr!(cx, 1))
|
||||||
}
|
}
|
||||||
|
|
||||||
// See Issue #15750
|
// See Issue #15750
|
||||||
@ -57,7 +57,7 @@ fn expand_identity(cx: &mut ExtCtxt, _span: Span, tts: &[TokenTree])
|
|||||||
let mut parser = parse::new_parser_from_tts(cx.parse_sess(),
|
let mut parser = parse::new_parser_from_tts(cx.parse_sess(),
|
||||||
cx.cfg(), tts.to_vec());
|
cx.cfg(), tts.to_vec());
|
||||||
let expr = parser.parse_expr();
|
let expr = parser.parse_expr();
|
||||||
MacExpr::new(quote_expr!(&mut *cx, $expr))
|
MacEager::expr(quote_expr!(&mut *cx, $expr))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expand_into_foo(cx: &mut ExtCtxt, sp: Span, attr: &MetaItem, it: P<Item>)
|
fn expand_into_foo(cx: &mut ExtCtxt, sp: Span, attr: &MetaItem, it: P<Item>)
|
||||||
@ -114,7 +114,7 @@ fn expand_forged_ident(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree]) -> Box<Mac
|
|||||||
let mut parser = new_parser_from_tts(parse_sess, cfg, tt);
|
let mut parser = new_parser_from_tts(parse_sess, cfg, tt);
|
||||||
parser.parse_expr()
|
parser.parse_expr()
|
||||||
};
|
};
|
||||||
MacExpr::new(expr)
|
MacEager::expr(expr)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn foo() {}
|
pub fn foo() {}
|
||||||
|
@ -20,7 +20,7 @@ use std::borrow::ToOwned;
|
|||||||
use syntax::ast;
|
use syntax::ast;
|
||||||
use syntax::codemap::Span;
|
use syntax::codemap::Span;
|
||||||
use syntax::ext::build::AstBuilder;
|
use syntax::ext::build::AstBuilder;
|
||||||
use syntax::ext::base::{TTMacroExpander, ExtCtxt, MacResult, MacExpr, NormalTT};
|
use syntax::ext::base::{TTMacroExpander, ExtCtxt, MacResult, MacEager, NormalTT};
|
||||||
use syntax::parse::token;
|
use syntax::parse::token;
|
||||||
use syntax::print::pprust;
|
use syntax::print::pprust;
|
||||||
use syntax::ptr::P;
|
use syntax::ptr::P;
|
||||||
@ -38,7 +38,7 @@ impl TTMacroExpander for Expander {
|
|||||||
let args = self.args.iter().map(|i| pprust::meta_item_to_string(&*i))
|
let args = self.args.iter().map(|i| pprust::meta_item_to_string(&*i))
|
||||||
.collect::<Vec<_>>().connect(", ");
|
.collect::<Vec<_>>().connect(", ");
|
||||||
let interned = token::intern_and_get_ident(&args[..]);
|
let interned = token::intern_and_get_ident(&args[..]);
|
||||||
MacExpr::new(ecx.expr_str(sp, interned))
|
MacEager::expr(ecx.expr_str(sp, interned))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ extern crate rustc;
|
|||||||
use syntax::codemap::Span;
|
use syntax::codemap::Span;
|
||||||
use syntax::parse::token;
|
use syntax::parse::token;
|
||||||
use syntax::ast::{TokenTree, TtToken};
|
use syntax::ast::{TokenTree, TtToken};
|
||||||
use syntax::ext::base::{ExtCtxt, MacResult, DummyResult, MacExpr};
|
use syntax::ext::base::{ExtCtxt, MacResult, DummyResult, MacEager};
|
||||||
use syntax::ext::build::AstBuilder; // trait for expr_usize
|
use syntax::ext::build::AstBuilder; // trait for expr_usize
|
||||||
use rustc::plugin::Registry;
|
use rustc::plugin::Registry;
|
||||||
|
|
||||||
@ -61,7 +61,7 @@ fn expand_rn(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MacExpr::new(cx.expr_usize(sp, total))
|
MacEager::expr(cx.expr_usize(sp, total))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[plugin_registrar]
|
#[plugin_registrar]
|
||||||
|
@ -30,5 +30,5 @@ pub fn plugin_registrar(reg: &mut Registry) {
|
|||||||
fn expand_foo(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree])
|
fn expand_foo(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree])
|
||||||
-> Box<MacResult+'static> {
|
-> Box<MacResult+'static> {
|
||||||
let answer = other::the_answer();
|
let answer = other::the_answer();
|
||||||
MacExpr::new(quote_expr!(cx, $answer))
|
MacEager::expr(quote_expr!(cx, $answer))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user