Skip to content

Replace MacExpr / MacPat / MacItems with MacEager #22875

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

Merged
merged 1 commit into from
Feb 28, 2015
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
8 changes: 4 additions & 4 deletions src/doc/trpl/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ extern crate rustc;
use syntax::codemap::Span;
use syntax::parse::token;
use syntax::ast::{TokenTree, TtToken};
use syntax::ext::base::{ExtCtxt, MacResult, DummyResult, MacExpr};
use syntax::ext::build::AstBuilder; // trait for expr_uint
use syntax::ext::base::{ExtCtxt, MacResult, DummyResult, MacEager};
use syntax::ext::build::AstBuilder; // trait for expr_usize
use rustc::plugin::Registry;

fn expand_rn(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree])
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -183,7 +183,7 @@ with
[`syntax::print::pprust::*_to_string`](http://doc.rust-lang.org/syntax/print/pprust/index.html#functions).

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
[quasiquote macros](../syntax/ext/quote/index.html). They are undocumented and
very rough around the edges. However, the implementation may be a good
Expand Down
11 changes: 6 additions & 5 deletions src/libsyntax/diagnostics/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ use std::collections::BTreeMap;
use ast;
use ast::{Ident, Name, TokenTree};
use codemap::Span;
use ext::base::{ExtCtxt, MacExpr, MacResult, MacItems};
use ext::base::{ExtCtxt, MacEager, MacResult};
use ext::build::AstBuilder;
use parse::token;
use ptr::P;
use util::small_vector::SmallVector;

thread_local! {
static REGISTERED_DIAGNOSTICS: RefCell<BTreeMap<Name, Option<Name>>> = {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -101,7 +102,7 @@ pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt,
let sym = Ident::new(token::gensym(&(
"__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,
Expand All @@ -126,7 +127,7 @@ pub fn expand_build_diagnostic_array<'cx>(ecx: &'cx mut ExtCtxt,
(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;
).unwrap()].into_iter())
).unwrap()]))
}
2 changes: 1 addition & 1 deletion src/libsyntax/ext/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
node: ast::ExprInlineAsm(ast::InlineAsm {
asm: token::intern_and_get_ident(&asm),
Expand Down
116 changes: 69 additions & 47 deletions src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use fold::Folder;

use std::collections::HashMap;
use std::rc::Rc;
use std::default::Default;

pub trait ItemDecorator {
fn expand(&self,
Expand Down Expand Up @@ -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
/// methods are spliced into the AST at the callsite of the macro (or
/// just into the compiler's internal macro table, for `make_def`).
/// methods are spliced into the AST at the callsite of the macro.
pub trait MacResult {
/// Create an expression.
fn make_expr(self: Box<Self>) -> Option<P<ast::Expr>> {
Expand All @@ -254,63 +263,76 @@ pub trait MacResult {
/// By default this attempts to create an expression statement,
/// returning None if that fails.
fn make_stmt(self: Box<Self>) -> Option<P<ast::Stmt>> {
self.make_expr()
.map(|e| P(codemap::respan(e.span, ast::StmtExpr(e, ast::DUMMY_NODE_ID))))
make_stmt_default!(self)
}
}

/// A convenience type for macros that return a single expression.
pub struct MacExpr {
e: P<ast::Expr>
}
impl MacExpr {
pub fn new(e: P<ast::Expr>) -> Box<MacResult+'static> {
box MacExpr { e: e } as Box<MacResult+'static>
}
}
impl MacResult for MacExpr {
fn make_expr(self: Box<MacExpr>) -> Option<P<ast::Expr>> {
Some(self.e)
}
fn make_pat(self: Box<MacExpr>) -> Option<P<ast::Pat>> {
match self.e.node {
ast::ExprLit(_) => Some(P(ast::Pat {
id: ast::DUMMY_NODE_ID,
span: self.e.span,
node: ast::PatLit(self.e)
})),
_ => None
macro_rules! make_MacEager {
( $( $fld:ident: $t:ty, )* ) => {
/// `MacResult` implementation for the common case where you've already
/// built each form of AST that you might return.
#[derive(Default)]
pub struct MacEager {
$(
pub $fld: Option<$t>,
)*
}

impl MacEager {
$(
pub fn $fld(v: $t) -> Box<MacResult> {
box MacEager {
$fld: Some(v),
..Default::default()
} as Box<MacResult>
}
)*
}
}
}
/// A convenience type for macros that return a single pattern.
pub struct MacPat {
p: P<ast::Pat>

make_MacEager! {
expr: P<ast::Expr>,
pat: P<ast::Pat>,
items: SmallVector<P<ast::Item>>,
methods: SmallVector<P<ast::Method>>,
stmt: P<ast::Stmt>,
}
impl MacPat {
pub fn new(p: P<ast::Pat>) -> Box<MacResult+'static> {
box MacPat { p: p } as Box<MacResult+'static>

impl MacResult for MacEager {
fn make_expr(self: Box<Self>) -> Option<P<ast::Expr>> {
self.expr
}
}
impl MacResult for MacPat {
fn make_pat(self: Box<MacPat>) -> Option<P<ast::Pat>> {
Some(self.p)

fn make_items(self: Box<Self>) -> Option<SmallVector<P<ast::Item>>> {
self.items
}
}
/// A type for macros that return multiple items.
pub struct MacItems {
items: SmallVector<P<ast::Item>>
}

impl MacItems {
pub fn new<I: Iterator<Item=P<ast::Item>>>(it: I) -> Box<MacResult+'static> {
box MacItems { items: it.collect() } as Box<MacResult+'static>
fn make_methods(self: Box<Self>) -> Option<SmallVector<P<ast::Method>>> {
self.methods
}
}

impl MacResult for MacItems {
fn make_items(self: Box<MacItems>) -> Option<SmallVector<P<ast::Item>>> {
Some(self.items)
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
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ext/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
MacExpr::new(cx.expr_bool(sp, matches_cfg))
MacEager::expr(cx.expr_bool(sp, matches_cfg))
}
2 changes: 1 addition & 1 deletion src/libsyntax/ext/concat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
token::intern_and_get_ident(&accumulator[..])))
}
2 changes: 1 addition & 1 deletion src/libsyntax/ext/concat_idents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ pub fn expand_syntax_ext<'cx>(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]
),
span: sp,
});
MacExpr::new(e)
MacEager::expr(e)
}
4 changes: 2 additions & 2 deletions src/libsyntax/ext/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenT
&s[..]))))
}
};
MacExpr::new(e)
MacEager::expr(e)
}

pub fn expand_env<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
Expand Down Expand Up @@ -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))
};
MacExpr::new(e)
MacEager::expr(e)
}
2 changes: 1 addition & 1 deletion src/libsyntax/ext/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ pub fn expand_format_args<'cx>(ecx: &'cx mut ExtCtxt, sp: Span,

match parse_args(ecx, sp, tts) {
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))
}
None => DummyResult::expr(sp)
Expand Down
16 changes: 8 additions & 8 deletions src/libsyntax/ext/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,15 +402,15 @@ pub fn expand_quote_tokens<'cx>(cx: &'cx mut ExtCtxt,
-> Box<base::MacResult+'cx> {
let (cx_expr, expr) = expand_tts(cx, sp, tts);
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,
sp: Span,
tts: &[ast::TokenTree])
-> Box<base::MacResult+'cx> {
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,
Expand All @@ -419,31 +419,31 @@ pub fn expand_quote_item<'cx>(cx: &mut ExtCtxt,
-> Box<base::MacResult+'cx> {
let expanded = expand_parse_call(cx, sp, "parse_item_with_outer_attributes",
vec!(), tts);
base::MacExpr::new(expanded)
base::MacEager::expr(expanded)
}

pub fn expand_quote_pat<'cx>(cx: &'cx mut ExtCtxt,
sp: Span,
tts: &[ast::TokenTree])
-> Box<base::MacResult+'cx> {
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,
sp: Span,
tts: &[ast::TokenTree])
-> Box<base::MacResult+'static> {
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,
sp: Span,
tts: &[ast::TokenTree])
-> Box<base::MacResult+'static> {
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,
Expand All @@ -452,7 +452,7 @@ pub fn expand_quote_method(cx: &mut ExtCtxt,
-> Box<base::MacResult+'static> {
let expanded = expand_parse_call(cx, sp, "parse_method_with_outer_attributes",
vec!(), tts);
base::MacExpr::new(expanded)
base::MacEager::expr(expanded)
}

pub fn expand_quote_stmt(cx: &mut ExtCtxt,
Expand All @@ -462,7 +462,7 @@ pub fn expand_quote_stmt(cx: &mut ExtCtxt,
let e_attrs = cx.expr_vec_ng(sp);
let expanded = expand_parse_call(cx, sp, "parse_stmt",
vec!(e_attrs), tts);
base::MacExpr::new(expanded)
base::MacEager::expr(expanded)
}

fn ids_ext(strs: Vec<String> ) -> Vec<ast::Ident> {
Expand Down
Loading