Skip to content

Support macros expanding to multiple items #10649

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 3 commits into from
Nov 27, 2013
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
34 changes: 12 additions & 22 deletions src/librustc/front/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,6 @@ pub fn strip_items(crate: ast::Crate,
ctxt.fold_crate(crate)
}

fn filter_item(cx: &Context, item: @ast::item) -> Option<@ast::item> {
if item_in_cfg(cx, item) {
Some(item)
} else {
None
}
}

fn filter_view_item<'r>(cx: &Context, view_item: &'r ast::view_item)
-> Option<&'r ast::view_item> {
if view_item_in_cfg(cx, view_item) {
Expand All @@ -66,9 +58,10 @@ fn filter_view_item<'r>(cx: &Context, view_item: &'r ast::view_item)
}

fn fold_mod(cx: &Context, m: &ast::_mod) -> ast::_mod {
let filtered_items = m.items.iter().filter_map(|a| {
filter_item(cx, *a).and_then(|x| cx.fold_item(x))
}).collect();
let filtered_items = m.items.iter()
.filter(|&a| item_in_cfg(cx, *a))
.flat_map(|&x| cx.fold_item(x).move_iter())
.collect();
let filtered_view_items = m.view_items.iter().filter_map(|a| {
filter_view_item(cx, a).map(|x| cx.fold_view_item(x))
}).collect();
Expand Down Expand Up @@ -122,28 +115,25 @@ fn fold_item_underscore(cx: &Context, item: &ast::item_) -> ast::item_ {
fold::noop_fold_item_underscore(&item, cx)
}

fn filter_stmt(cx: &Context, stmt: @ast::Stmt) -> Option<@ast::Stmt> {
fn retain_stmt(cx: &Context, stmt: @ast::Stmt) -> bool {
match stmt.node {
ast::StmtDecl(decl, _) => {
match decl.node {
ast::DeclItem(item) => {
if item_in_cfg(cx, item) {
Some(stmt)
} else {
None
}
item_in_cfg(cx, item)
}
_ => Some(stmt)
_ => true
}
}
_ => Some(stmt),
_ => true
}
}

fn fold_block(cx: &Context, b: &ast::Block) -> ast::Block {
let resulting_stmts = b.stmts.iter().filter_map(|a| {
filter_stmt(cx, *a).and_then(|stmt| cx.fold_stmt(stmt))
}).collect();
let resulting_stmts = b.stmts.iter()
.filter(|&a| retain_stmt(cx, *a))
.flat_map(|&stmt| cx.fold_stmt(stmt).move_iter())
.collect();
let filtered_view_items = b.view_items.iter().filter_map(|a| {
filter_view_item(cx, a).map(|x| cx.fold_view_item(x))
}).collect();
Expand Down
5 changes: 3 additions & 2 deletions src/librustc/front/std_inject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use syntax::codemap;
use syntax::fold::ast_fold;
use syntax::fold;
use syntax::opt_vec;
use syntax::util::small_vector::SmallVector;

static STD_VERSION: &'static str = "0.9-pre";

Expand Down Expand Up @@ -98,14 +99,14 @@ impl fold::ast_fold for StandardLibraryInjector {
}
}

fn fold_item(&self, item: @ast::item) -> Option<@ast::item> {
fn fold_item(&self, item: @ast::item) -> SmallVector<@ast::item> {
if !no_prelude(item.attrs) {
// only recur if there wasn't `#[no_implicit_prelude];`
// on this item, i.e. this means that the prelude is not
// implicitly imported though the whole subtree
fold::noop_fold_item(item, self)
} else {
Some(item)
SmallVector::one(item)
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/librustc/front/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use syntax::fold;
use syntax::opt_vec;
use syntax::print::pprust;
use syntax::{ast, ast_util};
use syntax::util::small_vector::SmallVector;

struct Test {
span: Span,
Expand Down Expand Up @@ -76,7 +77,7 @@ impl fold::ast_fold for TestHarnessGenerator {
}
}

fn fold_item(&self, i: @ast::item) -> Option<@ast::item> {
fn fold_item(&self, i: @ast::item) -> SmallVector<@ast::item> {
self.cx.path.push(i.ident);
debug!("current path: {}",
ast_util::path_name_i(self.cx.path.clone()));
Expand Down Expand Up @@ -108,7 +109,7 @@ impl fold::ast_fold for TestHarnessGenerator {

let res = fold::noop_fold_item(i, self);
self.cx.path.pop();
return res;
res
}

fn fold_mod(&self, m: &ast::_mod) -> ast::_mod {
Expand Down
6 changes: 4 additions & 2 deletions src/librustc/middle/astencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,8 @@ fn simplify_ast(ii: &ast::inlined_item) -> ast::inlined_item {

match *ii {
//hack: we're not dropping items
ast::ii_item(i) => ast::ii_item(fld.fold_item(i).unwrap()),
ast::ii_item(i) => ast::ii_item(fld.fold_item(i)
.expect_one("expected one item")),
ast::ii_method(d, is_provided, m) =>
ast::ii_method(d, is_provided, fld.fold_method(m)),
ast::ii_foreign(i) => ast::ii_foreign(fld.fold_foreign_item(i))
Expand Down Expand Up @@ -379,7 +380,8 @@ fn renumber_ast(xcx: @ExtendedDecodeContext, ii: ast::inlined_item)
xcx: xcx,
};
match ii {
ast::ii_item(i) => ast::ii_item(fld.fold_item(i).unwrap()),
ast::ii_item(i) => ast::ii_item(fld.fold_item(i)
.expect_one("expected one item")),
ast::ii_method(d, is_provided, m) =>
ast::ii_method(xcx.tr_def_id(d), is_provided, fld.fold_method(m)),
ast::ii_foreign(i) => ast::ii_foreign(fld.fold_foreign_item(i)),
Expand Down
5 changes: 3 additions & 2 deletions src/librustpkg/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use syntax::{ast, attr, codemap, diagnostic, fold, visit};
use syntax::attr::AttrMetaMethods;
use syntax::fold::ast_fold;
use syntax::visit::Visitor;
use syntax::util::small_vector::SmallVector;
use rustc::back::link::output_type_exe;
use rustc::back::link;
use rustc::driver::session::{lib_crate, bin_crate};
Expand Down Expand Up @@ -99,7 +100,7 @@ fn fold_mod(_ctx: @mut ReadyCtx, m: &ast::_mod, fold: &CrateSetup)
}

fn fold_item(ctx: @mut ReadyCtx, item: @ast::item, fold: &CrateSetup)
-> Option<@ast::item> {
-> SmallVector<@ast::item> {
ctx.path.push(item.ident);

let mut cmds = ~[];
Expand Down Expand Up @@ -142,7 +143,7 @@ struct CrateSetup {
}

impl fold::ast_fold for CrateSetup {
fn fold_item(&self, item: @ast::item) -> Option<@ast::item> {
fn fold_item(&self, item: @ast::item) -> SmallVector<@ast::item> {
fold_item(self.ctx, item, self)
}
fn fold_mod(&self, module: &ast::_mod) -> ast::_mod {
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use ext::expand;
use parse;
use parse::token;
use parse::token::{ident_to_str, intern, str_to_ident};
use util::small_vector::SmallVector;

use std::hashmap::HashMap;

Expand Down Expand Up @@ -131,7 +132,7 @@ pub type SyntaxExpanderTTItemFunNoCtxt =

pub trait AnyMacro {
fn make_expr(&self) -> @ast::Expr;
fn make_item(&self) -> Option<@ast::item>;
fn make_items(&self) -> SmallVector<@ast::item>;
fn make_stmt(&self) -> @ast::Stmt;
}

Expand Down
Loading