1
Fork 0

Moved syntax::ext::base::SyntaxEnv into syntax::ext::base::ExtCtx

This commit is contained in:
Marvin Löbel 2014-07-19 21:34:24 +02:00
parent df68c6f3c3
commit 94d92e6830
2 changed files with 213 additions and 201 deletions

View file

@ -23,6 +23,7 @@ use ext::mtwt;
use std::collections::HashMap;
use std::gc::{Gc, GC};
use std::rc::Rc;
// new-style macro! tt code:
//
@ -104,9 +105,9 @@ pub type IdentMacroExpanderFn =
/// just into the compiler's internal macro table, for `make_def`).
pub trait MacResult {
/// Define a new macro.
// this particular flavor should go away; the idea that a macro might
// expand into either a macro definition or an expression, depending
// on what the context wants, is kind of silly.
// this should go away; the idea that a macro might expand into
// either a macro definition or an expression, depending on what
// the context wants, is kind of silly.
fn make_def(&self) -> Option<MacroDef> {
None
}
@ -314,7 +315,7 @@ impl BlockInfo {
/// The base map of methods for expanding syntax extension
/// AST nodes into full ASTs
pub fn syntax_expander_table() -> SyntaxEnv {
fn initial_syntax_expander_table() -> SyntaxEnv {
// utility function to simplify creating NormalTT syntax extensions
fn builtin_normal_expander(f: MacroExpanderFn) -> SyntaxExtension {
NormalTT(box BasicMacroExpander {
@ -431,7 +432,9 @@ pub struct ExtCtxt<'a> {
pub mod_path: Vec<ast::Ident> ,
pub trace_mac: bool,
pub exported_macros: Vec<Gc<ast::Item>>
pub exported_macros: Vec<Gc<ast::Item>>,
pub syntax_env: SyntaxEnv,
}
impl<'a> ExtCtxt<'a> {
@ -445,6 +448,7 @@ impl<'a> ExtCtxt<'a> {
ecfg: ecfg,
trace_mac: false,
exported_macros: Vec::new(),
syntax_env: initial_syntax_expander_table(),
}
}
@ -453,7 +457,6 @@ impl<'a> ExtCtxt<'a> {
match e.node {
ast::ExprMac(..) => {
let mut expander = expand::MacroExpander {
extsbox: syntax_expander_table(),
cx: self,
};
e = expand::expand_expr(e, &mut expander);
@ -642,10 +645,13 @@ pub fn get_exprs_from_tts(cx: &mut ExtCtxt,
/// In order to have some notion of scoping for macros,
/// we want to implement the notion of a transformation
/// environment.
///
/// This environment maps Names to SyntaxExtensions.
pub struct SyntaxEnv {
chain: Vec<MapChainFrame> ,
}
//impl question: how to implement it? Initially, the
// impl question: how to implement it? Initially, the
// env will contain only macros, so it might be painful
// to add an empty frame for every context. Let's just
// get it working, first....
@ -657,15 +663,11 @@ pub fn get_exprs_from_tts(cx: &mut ExtCtxt,
struct MapChainFrame {
info: BlockInfo,
map: HashMap<Name, SyntaxExtension>,
}
pub struct SyntaxEnv {
chain: Vec<MapChainFrame> ,
map: HashMap<Name, Rc<SyntaxExtension>>,
}
impl SyntaxEnv {
pub fn new() -> SyntaxEnv {
fn new() -> SyntaxEnv {
let mut map = SyntaxEnv { chain: Vec::new() };
map.push_frame();
map
@ -692,10 +694,10 @@ impl SyntaxEnv {
unreachable!()
}
pub fn find<'a>(&'a self, k: &Name) -> Option<&'a SyntaxExtension> {
pub fn find(&self, k: &Name) -> Option<Rc<SyntaxExtension>> {
for frame in self.chain.iter().rev() {
match frame.map.find(k) {
Some(v) => return Some(v),
Some(v) => return Some(v.clone()),
None => {}
}
}
@ -703,7 +705,7 @@ impl SyntaxEnv {
}
pub fn insert(&mut self, k: Name, v: SyntaxExtension) {
self.find_escape_frame().map.insert(k, v);
self.find_escape_frame().map.insert(k, Rc::new(v));
}
pub fn info<'a>(&'a mut self) -> &'a mut BlockInfo {

View file

@ -208,7 +208,7 @@ fn expand_mac_invoc<T>(mac: &ast::Mac, span: &codemap::Span,
}
let extname = pth.segments.get(0).identifier;
let extnamestr = token::get_ident(extname);
match fld.extsbox.find(&extname.name) {
match fld.cx.syntax_env.find(&extname.name) {
None => {
fld.cx.span_err(
pth.span,
@ -218,7 +218,8 @@ fn expand_mac_invoc<T>(mac: &ast::Mac, span: &codemap::Span,
// let compilation continue
None
}
Some(&NormalTT(ref expandfun, exp_span)) => {
Some(rc) => match *rc {
NormalTT(ref expandfun, exp_span) => {
fld.cx.bt_push(ExpnInfo {
call_site: *span,
callee: NameAndSpan {
@ -262,6 +263,7 @@ fn expand_mac_invoc<T>(mac: &ast::Mac, span: &codemap::Span,
}
}
}
}
}
/// Rename loop label and expand its loop body
@ -288,10 +290,10 @@ fn expand_loop_block(loop_block: P<Block>,
// The rename *must* be added to the enclosed syntax context for
// `break` or `continue` to pick up because by definition they are
// in a block enclosed by loop head.
fld.extsbox.push_frame();
fld.extsbox.info().pending_renames.push(rename);
fld.cx.syntax_env.push_frame();
fld.cx.syntax_env.info().pending_renames.push(rename);
let expanded_block = expand_block_elts(&*loop_block, fld);
fld.extsbox.pop_frame();
fld.cx.syntax_env.pop_frame();
(expanded_block, Some(renamed_ident))
}
@ -321,8 +323,9 @@ fn expand_item(it: Gc<ast::Item>, fld: &mut MacroExpander)
for attr in it.attrs.iter() {
let mname = attr.name();
match fld.extsbox.find(&intern(mname.get())) {
Some(&ItemDecorator(dec_fn)) => {
match fld.cx.syntax_env.find(&intern(mname.get())) {
Some(rc) => match *rc {
ItemDecorator(dec_fn) => {
attr::mark_used(attr);
fld.cx.bt_push(ExpnInfo {
@ -345,6 +348,8 @@ fn expand_item(it: Gc<ast::Item>, fld: &mut MacroExpander)
fld.cx.bt_pop();
}
_ => new_attrs.push((*attr).clone()),
},
_ => new_attrs.push((*attr).clone()),
}
}
@ -353,7 +358,7 @@ fn expand_item(it: Gc<ast::Item>, fld: &mut MacroExpander)
ast::ItemMod(_) | ast::ItemForeignMod(_) => {
fld.cx.mod_push(it.ident);
let macro_escape = contains_macro_escape(new_attrs.as_slice());
let result = with_exts_frame!(fld.extsbox,
let result = with_exts_frame!(fld.cx.syntax_env,
macro_escape,
noop_fold_item(&*it, fld));
fld.cx.mod_pop();
@ -377,8 +382,8 @@ fn expand_item_modifiers(mut it: Gc<ast::Item>, fld: &mut MacroExpander)
-> Gc<ast::Item> {
// partition the attributes into ItemModifiers and others
let (modifiers, other_attrs) = it.attrs.partitioned(|attr| {
match fld.extsbox.find(&intern(attr.name().get())) {
Some(&ItemModifier(_)) => true,
match fld.cx.syntax_env.find(&intern(attr.name().get())) {
Some(rc) => match *rc { ItemModifier(_) => true, _ => false },
_ => false
}
});
@ -395,8 +400,9 @@ fn expand_item_modifiers(mut it: Gc<ast::Item>, fld: &mut MacroExpander)
for attr in modifiers.iter() {
let mname = attr.name();
match fld.extsbox.find(&intern(mname.get())) {
Some(&ItemModifier(dec_fn)) => {
match fld.cx.syntax_env.find(&intern(mname.get())) {
Some(rc) => match *rc {
ItemModifier(dec_fn) => {
attr::mark_used(attr);
fld.cx.bt_push(ExpnInfo {
call_site: attr.span,
@ -410,6 +416,8 @@ fn expand_item_modifiers(mut it: Gc<ast::Item>, fld: &mut MacroExpander)
fld.cx.bt_pop();
}
_ => unreachable!()
},
_ => unreachable!()
}
}
@ -452,7 +460,7 @@ fn expand_item_mac(it: Gc<ast::Item>, fld: &mut MacroExpander)
let extname = pth.segments.get(0).identifier;
let extnamestr = token::get_ident(extname);
let fm = fresh_mark();
let expanded = match fld.extsbox.find(&extname.name) {
let expanded = match fld.cx.syntax_env.find(&extname.name) {
None => {
fld.cx.span_err(pth.span,
format!("macro undefined: '{}!'",
@ -461,7 +469,8 @@ fn expand_item_mac(it: Gc<ast::Item>, fld: &mut MacroExpander)
return SmallVector::zero();
}
Some(&NormalTT(ref expander, span)) => {
Some(rc) => match *rc {
NormalTT(ref expander, span) => {
if it.ident.name != parse::token::special_idents::invalid.name {
fld.cx
.span_err(pth.span,
@ -483,7 +492,7 @@ fn expand_item_mac(it: Gc<ast::Item>, fld: &mut MacroExpander)
let marked_before = mark_tts(tts.as_slice(), fm);
expander.expand(fld.cx, it.span, marked_before.as_slice())
}
Some(&IdentTT(ref expander, span)) => {
IdentTT(ref expander, span) => {
if it.ident.name == parse::token::special_idents::invalid.name {
fld.cx.span_err(pth.span,
format!("macro {}! expects an ident argument",
@ -502,7 +511,7 @@ fn expand_item_mac(it: Gc<ast::Item>, fld: &mut MacroExpander)
let marked_tts = mark_tts(tts.as_slice(), fm);
expander.expand(fld.cx, it.span, it.ident, marked_tts)
}
Some(&LetSyntaxTT(ref expander, span)) => {
LetSyntaxTT(ref expander, span) => {
if it.ident.name == parse::token::special_idents::invalid.name {
fld.cx.span_err(pth.span,
format!("macro {}! expects an ident argument",
@ -526,6 +535,7 @@ fn expand_item_mac(it: Gc<ast::Item>, fld: &mut MacroExpander)
extnamestr.get()).as_slice());
return SmallVector::zero();
}
}
};
let items = match expanded.make_def() {
@ -534,7 +544,7 @@ fn expand_item_mac(it: Gc<ast::Item>, fld: &mut MacroExpander)
// result of expanding a LetSyntaxTT, and thus doesn't
// need to be marked. Not that it could be marked anyway.
// create issue to recommend refactoring here?
fld.extsbox.insert(intern(name.as_slice()), ext);
fld.cx.syntax_env.insert(intern(name.as_slice()), ext);
if attr::contains_name(it.attrs.as_slice(), "macro_export") {
fld.cx.exported_macros.push(it);
}
@ -641,7 +651,7 @@ fn expand_non_macro_stmt(s: &Stmt, fld: &mut MacroExpander)
rename_fld.fold_pat(expanded_pat)
};
// add them to the existing pending renames:
fld.extsbox.info().pending_renames.push_all_move(new_pending_renames);
fld.cx.syntax_env.info().pending_renames.push_all_move(new_pending_renames);
// also, don't forget to expand the init:
let new_init_opt = init.map(|e| fld.fold_expr(e));
let rewritten_local =
@ -742,7 +752,7 @@ fn fn_decl_arg_bindings(fn_decl: &ast::FnDecl) -> Vec<ast::Ident> {
// expand a block. pushes a new exts_frame, then calls expand_block_elts
fn expand_block(blk: &Block, fld: &mut MacroExpander) -> P<Block> {
// see note below about treatment of exts table
with_exts_frame!(fld.extsbox,false,
with_exts_frame!(fld.cx.syntax_env,false,
expand_block_elts(blk, fld))
}
@ -753,7 +763,7 @@ fn expand_block_elts(b: &Block, fld: &mut MacroExpander) -> P<Block> {
b.stmts.iter().flat_map(|x| {
// perform all pending renames
let renamed_stmt = {
let pending_renames = &mut fld.extsbox.info().pending_renames;
let pending_renames = &mut fld.cx.syntax_env.info().pending_renames;
let mut rename_fld = IdentRenamer{renames:pending_renames};
rename_fld.fold_stmt(&**x).expect_one("rename_fold didn't return one value")
};
@ -762,7 +772,7 @@ fn expand_block_elts(b: &Block, fld: &mut MacroExpander) -> P<Block> {
}).collect();
let new_expr = b.expr.map(|x| {
let expr = {
let pending_renames = &mut fld.extsbox.info().pending_renames;
let pending_renames = &mut fld.cx.syntax_env.info().pending_renames;
let mut rename_fld = IdentRenamer{renames:pending_renames};
rename_fld.fold_expr(x)
};
@ -795,7 +805,7 @@ fn expand_pat(p: Gc<ast::Pat>, fld: &mut MacroExpander) -> Gc<ast::Pat> {
}
let extname = pth.segments.get(0).identifier;
let extnamestr = token::get_ident(extname);
let marked_after = match fld.extsbox.find(&extname.name) {
let marked_after = match fld.cx.syntax_env.find(&extname.name) {
None => {
fld.cx.span_err(pth.span,
format!("macro undefined: '{}!'",
@ -804,7 +814,8 @@ fn expand_pat(p: Gc<ast::Pat>, fld: &mut MacroExpander) -> Gc<ast::Pat> {
return DummyResult::raw_pat(p.span);
}
Some(&NormalTT(ref expander, span)) => {
Some(rc) => match *rc {
NormalTT(ref expander, span) => {
fld.cx.bt_push(ExpnInfo {
call_site: p.span,
callee: NameAndSpan {
@ -842,6 +853,7 @@ fn expand_pat(p: Gc<ast::Pat>, fld: &mut MacroExpander) -> Gc<ast::Pat> {
extnamestr.get()).as_slice());
return DummyResult::raw_pat(p.span);
}
}
};
let fully_expanded =
@ -975,7 +987,6 @@ fn expand_and_rename_fn_decl_and_block(fn_decl: &ast::FnDecl, block: Gc<ast::Blo
/// A tree-folder that performs macro expansion
pub struct MacroExpander<'a, 'b> {
pub extsbox: SyntaxEnv,
pub cx: &'a mut ExtCtxt<'b>,
}
@ -1044,7 +1055,6 @@ pub fn expand_crate(parse_sess: &parse::ParseSess,
c: Crate) -> Crate {
let mut cx = ExtCtxt::new(parse_sess, c.config.clone(), cfg);
let mut expander = MacroExpander {
extsbox: syntax_expander_table(),
cx: &mut cx,
};
@ -1063,7 +1073,7 @@ pub fn expand_crate(parse_sess: &parse::ParseSess,
}
for (name, extension) in user_exts.move_iter() {
expander.extsbox.insert(name, extension);
expander.cx.syntax_env.insert(name, extension);
}
let mut ret = expander.fold_crate(c);