Skip to content

Commit 77c2c09

Browse files
committed
add renaming and sctable funs
1 parent 38b1d60 commit 77c2c09

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

src/libsyntax/ext/expand.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ use ast::{crate, expr_, expr_mac, mac_invoc_tt};
1515
use ast::{item_mac, stmt_, stmt_mac, stmt_expr, stmt_semi};
1616
use ast::{SCTable, illegal_ctxt};
1717
use ast;
18-
use ast_util::{new_rename, new_mark, resolve};
18+
use ast_util::{new_rename, new_mark, resolve, new_sctable};
1919
use attr;
2020
use codemap;
2121
use codemap::{span, CallInfo, ExpandedFrom, NameAndSpan, spanned};
22+
use core::cast;
23+
use core::local_data;
2224
use ext::base::*;
2325
use fold::*;
2426
use parse;
@@ -395,6 +397,51 @@ pub fn expand_block(extsbox: @mut SyntaxEnv,
395397
}
396398
}
397399

400+
// given a mutable list of renames, return a tree-folder that applies those
401+
// renames.
402+
fn renames_to_fold(renames : @mut ~[(ast::ident,ast::Name)]) -> @ast_fold {
403+
let table = local_sctable_get();
404+
let afp = default_ast_fold();
405+
let f_pre = @AstFoldFns {
406+
fold_ident: |id,_| {
407+
// the individual elements are memoized... it would
408+
// also be possible to memoize on the whole list at once.
409+
let new_ctxt = renames.foldl(id.ctxt,|ctxt,&(from,to)| {
410+
new_rename(from,to,*ctxt,table)
411+
});
412+
ast::ident{repr:id.repr,ctxt:new_ctxt}
413+
},
414+
.. *afp
415+
};
416+
make_fold(f_pre)
417+
}
418+
419+
// perform a bunch of renames
420+
fn apply_pending_renames(folder : @ast_fold, stmt : ast::stmt) -> @ast::stmt {
421+
match folder.fold_stmt(&stmt) {
422+
Some(s) => s,
423+
None => fail!(fmt!("renaming of stmt produced None"))
424+
}
425+
}
426+
427+
// fetch the SCTable from TLS, create one if it doesn't yet exist.
428+
fn local_sctable_get() -> @mut SCTable {
429+
unsafe {
430+
let sctable_key = (cast::transmute::<(uint, uint),
431+
&fn(v: @@mut SCTable)>(
432+
(-4 as uint, 0u)));
433+
match local_data::local_data_get(sctable_key) {
434+
None => {
435+
let new_table = @@mut new_sctable();
436+
local_data::local_data_set(sctable_key,new_table);
437+
*new_table
438+
},
439+
Some(intr) => *intr
440+
}
441+
}
442+
}
443+
444+
398445
pub fn new_span(cx: @ExtCtxt, sp: span) -> span {
399446
/* this discards information in the case of macro-defining macros */
400447
return span {lo: sp.lo, hi: sp.hi, expn_info: cx.backtrace()};

0 commit comments

Comments
 (0)