Skip to content

Commit 91b6526

Browse files
committed
moved TLS of sctable to ast_util, hid parameter in hygiene calls
1 parent fe6baa9 commit 91b6526

File tree

2 files changed

+103
-81
lines changed

2 files changed

+103
-81
lines changed

src/libsyntax/ast_util.rs

Lines changed: 75 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use ast::*;
1414
use ast;
1515
use ast_util;
1616
use codemap::{span, spanned};
17+
use core::cast;
18+
use core::local_data;
1719
use opt_vec;
1820
use parse::token;
1921
use visit;
@@ -623,12 +625,18 @@ pub enum Privacy {
623625
pub fn new_ident(name: Name) -> ident { ident {name: name, ctxt: 0}}
624626

625627
/// Extend a syntax context with a given mark
626-
pub fn new_mark (m:Mrk, tail:SyntaxContext,table:&mut SCTable)
628+
pub fn new_mark(m:Mrk, tail:SyntaxContext) -> SyntaxContext {
629+
new_mark_internal(m,tail,get_sctable())
630+
}
631+
632+
// Extend a syntax context with a given mark and table
633+
// FIXME #4536 : currently pub to allow testing
634+
pub fn new_mark_internal(m:Mrk, tail:SyntaxContext,table:&mut SCTable)
627635
-> SyntaxContext {
628636
let key = (tail,m);
629637
// FIXME #5074 : can't use more natural style because we're missing
630638
// flow-sensitivity. Results in two lookups on a hash table hit.
631-
// also applies to new_rename, below.
639+
// also applies to new_rename_internal, below.
632640
// let try_lookup = table.mark_memo.find(&key);
633641
match table.mark_memo.contains_key(&key) {
634642
false => {
@@ -646,7 +654,13 @@ pub fn new_mark (m:Mrk, tail:SyntaxContext,table:&mut SCTable)
646654
}
647655
648656
/// Extend a syntax context with a given rename
649-
pub fn new_rename (id:ident, to:Name, tail:SyntaxContext, table: &mut SCTable)
657+
pub fn new_rename(id:ident, to:Name, tail:SyntaxContext) -> SyntaxContext {
658+
new_rename_internal(id, to, tail, get_sctable())
659+
}
660+
661+
// Extend a syntax context with a given rename and sctable
662+
// FIXME #4536 : currently pub to allow testing
663+
pub fn new_rename_internal(id:ident, to:Name, tail:SyntaxContext, table: &mut SCTable)
650664
-> SyntaxContext {
651665
let key = (tail,id,to);
652666
// FIXME #5074
@@ -668,34 +682,58 @@ pub fn new_rename (id:ident, to:Name, tail:SyntaxContext, table: &mut SCTable)
668682
669683
/// Make a fresh syntax context table with EmptyCtxt in slot zero
670684
/// and IllegalCtxt in slot one.
671-
pub fn new_sctable() -> SCTable {
685+
// FIXME #4536 : currently pub to allow testing
686+
pub fn new_sctable_internal() -> SCTable {
672687
SCTable {
673688
table: ~[EmptyCtxt,IllegalCtxt],
674689
mark_memo: HashMap::new(),
675690
rename_memo: HashMap::new()
676691
}
677692
}
678693
694+
// fetch the SCTable from TLS, create one if it doesn't yet exist.
695+
pub fn get_sctable() -> @mut SCTable {
696+
unsafe {
697+
let sctable_key = (cast::transmute::<(uint, uint),
698+
&fn(v: @@mut SCTable)>(
699+
(-4 as uint, 0u)));
700+
match local_data::local_data_get(sctable_key) {
701+
None => {
702+
let new_table = @@mut new_sctable_internal();
703+
local_data::local_data_set(sctable_key,new_table);
704+
*new_table
705+
},
706+
Some(intr) => *intr
707+
}
708+
}
709+
}
710+
679711
/// Add a value to the end of a vec, return its index
680712
fn idx_push<T>(vec: &mut ~[T], val: T) -> uint {
681713
vec.push(val);
682714
vec.len() - 1
683715
}
684716
685717
/// Resolve a syntax object to a name, per MTWT.
686-
pub fn resolve (id : ident, table : &mut SCTable) -> Name {
718+
pub fn resolve(id : ident) -> Name {
719+
resolve_internal(id, get_sctable())
720+
}
721+
722+
// Resolve a syntax object to a name, per MTWT.
723+
// FIXME #4536 : currently pub to allow testing
724+
pub fn resolve_internal(id : ident, table : &mut SCTable) -> Name {
687725
match table.table[id.ctxt] {
688726
EmptyCtxt => id.name,
689727
// ignore marks here:
690-
Mark(_,subctxt) => resolve (ident{name:id.name, ctxt: subctxt},table),
728+
Mark(_,subctxt) => resolve_internal(ident{name:id.name, ctxt: subctxt},table),
691729
// do the rename if necessary:
692730
Rename(ident{name,ctxt},toname,subctxt) => {
693731
// this could be cached or computed eagerly:
694-
let resolvedfrom = resolve(ident{name:name,ctxt:ctxt},table);
695-
let resolvedthis = resolve(ident{name:id.name,ctxt:subctxt},table);
732+
let resolvedfrom = resolve_internal(ident{name:name,ctxt:ctxt},table);
733+
let resolvedthis = resolve_internal(ident{name:id.name,ctxt:subctxt},table);
696734
if ((resolvedthis == resolvedfrom)
697-
&& (marksof (ctxt,resolvedthis,table)
698-
== marksof (subctxt,resolvedthis,table))) {
735+
&& (marksof(ctxt,resolvedthis,table)
736+
== marksof(subctxt,resolvedthis,table))) {
699737
toname
700738
} else {
701739
resolvedthis
@@ -797,8 +835,8 @@ mod test {
797835
-> SyntaxContext {
798836
tscs.foldr(tail, |tsc : &TestSC,tail : SyntaxContext|
799837
{match *tsc {
800-
M(mrk) => new_mark(mrk,tail,table),
801-
R(ident,name) => new_rename(ident,name,tail,table)}})
838+
M(mrk) => new_mark_internal(mrk,tail,table),
839+
R(ident,name) => new_rename_internal(ident,name,tail,table)}})
802840
}
803841
804842
// gather a SyntaxContext back into a vector of TestSCs
@@ -823,7 +861,7 @@ mod test {
823861
}
824862
825863
#[test] fn test_unfold_refold(){
826-
let mut t = new_sctable();
864+
let mut t = new_sctable_internal();
827865
828866
let test_sc = ~[M(3),R(id(101,0),14),M(9)];
829867
assert_eq!(unfold_test_sc(copy test_sc,empty_ctxt,&mut t),4);
@@ -837,11 +875,11 @@ mod test {
837875
// in a vector. v[0] will be the outermost mark.
838876
fn unfold_marks(mrks:~[Mrk],tail:SyntaxContext,table: &mut SCTable) -> SyntaxContext {
839877
mrks.foldr(tail, |mrk:&Mrk,tail:SyntaxContext|
840-
{new_mark(*mrk,tail,table)})
878+
{new_mark_internal(*mrk,tail,table)})
841879
}
842880
843881
#[test] fn unfold_marks_test() {
844-
let mut t = new_sctable();
882+
let mut t = new_sctable_internal();
845883
846884
assert_eq!(unfold_marks(~[3,7],empty_ctxt,&mut t),3);
847885
assert_eq!(t.table[2],Mark(7,0));
@@ -851,7 +889,7 @@ mod test {
851889
#[test] fn test_marksof () {
852890
let stopname = 242;
853891
let name1 = 243;
854-
let mut t = new_sctable();
892+
let mut t = new_sctable_internal();
855893
assert_eq!(marksof (empty_ctxt,stopname,&t),~[]);
856894
// FIXME #5074: ANF'd to dodge nested calls
857895
{ let ans = unfold_marks(~[4,98],empty_ctxt,&mut t);
@@ -865,13 +903,13 @@ mod test {
865903
// rename where stop doesn't match:
866904
{ let chain = ~[M(9),
867905
R(id(name1,
868-
new_mark (4, empty_ctxt,&mut t)),
906+
new_mark_internal (4, empty_ctxt,&mut t)),
869907
100101102),
870908
M(14)];
871909
let ans = unfold_test_sc(chain,empty_ctxt,&mut t);
872910
assert_eq! (marksof (ans, stopname, &t), ~[9,14]);}
873911
// rename where stop does match
874-
{ let name1sc = new_mark(4, empty_ctxt, &mut t);
912+
{ let name1sc = new_mark_internal(4, empty_ctxt, &mut t);
875913
let chain = ~[M(9),
876914
R(id(name1, name1sc),
877915
stopname),
@@ -883,59 +921,59 @@ mod test {
883921
884922
#[test] fn resolve_tests () {
885923
let a = 40;
886-
let mut t = new_sctable();
924+
let mut t = new_sctable_internal();
887925
// - ctxt is MT
888-
assert_eq!(resolve(id(a,empty_ctxt),&mut t),a);
926+
assert_eq!(resolve_internal(id(a,empty_ctxt),&mut t),a);
889927
// - simple ignored marks
890928
{ let sc = unfold_marks(~[1,2,3],empty_ctxt,&mut t);
891-
assert_eq!(resolve(id(a,sc),&mut t),a);}
929+
assert_eq!(resolve_internal(id(a,sc),&mut t),a);}
892930
// - orthogonal rename where names don't match
893931
{ let sc = unfold_test_sc(~[R(id(50,empty_ctxt),51),M(12)],empty_ctxt,&mut t);
894-
assert_eq!(resolve(id(a,sc),&mut t),a);}
932+
assert_eq!(resolve_internal(id(a,sc),&mut t),a);}
895933
// - rename where names do match, but marks don't
896-
{ let sc1 = new_mark(1,empty_ctxt,&mut t);
934+
{ let sc1 = new_mark_internal(1,empty_ctxt,&mut t);
897935
let sc = unfold_test_sc(~[R(id(a,sc1),50),
898936
M(1),
899937
M(2)],
900938
empty_ctxt,&mut t);
901-
assert_eq!(resolve(id(a,sc),&mut t), a);}
939+
assert_eq!(resolve_internal(id(a,sc),&mut t), a);}
902940
// - rename where names and marks match
903941
{ let sc1 = unfold_test_sc(~[M(1),M(2)],empty_ctxt,&mut t);
904942
let sc = unfold_test_sc(~[R(id(a,sc1),50),M(1),M(2)],empty_ctxt,&mut t);
905-
assert_eq!(resolve(id(a,sc),&mut t), 50); }
943+
assert_eq!(resolve_internal(id(a,sc),&mut t), 50); }
906944
// - rename where names and marks match by literal sharing
907945
{ let sc1 = unfold_test_sc(~[M(1),M(2)],empty_ctxt,&mut t);
908946
let sc = unfold_test_sc(~[R(id(a,sc1),50)],sc1,&mut t);
909-
assert_eq!(resolve(id(a,sc),&mut t), 50); }
947+
assert_eq!(resolve_internal(id(a,sc),&mut t), 50); }
910948
// - two renames of the same var.. can only happen if you use
911949
// local-expand to prevent the inner binding from being renamed
912950
// during the rename-pass caused by the first:
913951
io::println("about to run bad test");
914952
{ let sc = unfold_test_sc(~[R(id(a,empty_ctxt),50),
915953
R(id(a,empty_ctxt),51)],
916954
empty_ctxt,&mut t);
917-
assert_eq!(resolve(id(a,sc),&mut t), 51); }
955+
assert_eq!(resolve_internal(id(a,sc),&mut t), 51); }
918956
// the simplest double-rename:
919-
{ let a_to_a50 = new_rename(id(a,empty_ctxt),50,empty_ctxt,&mut t);
920-
let a50_to_a51 = new_rename(id(a,a_to_a50),51,a_to_a50,&mut t);
921-
assert_eq!(resolve(id(a,a50_to_a51),&mut t),51);
957+
{ let a_to_a50 = new_rename_internal(id(a,empty_ctxt),50,empty_ctxt,&mut t);
958+
let a50_to_a51 = new_rename_internal(id(a,a_to_a50),51,a_to_a50,&mut t);
959+
assert_eq!(resolve_internal(id(a,a50_to_a51),&mut t),51);
922960
// mark on the outside doesn't stop rename:
923-
let sc = new_mark(9,a50_to_a51,&mut t);
924-
assert_eq!(resolve(id(a,sc),&mut t),51);
961+
let sc = new_mark_internal(9,a50_to_a51,&mut t);
962+
assert_eq!(resolve_internal(id(a,sc),&mut t),51);
925963
// but mark on the inside does:
926964
let a50_to_a51_b = unfold_test_sc(~[R(id(a,a_to_a50),51),
927965
M(9)],
928966
a_to_a50,
929967
&mut t);
930-
assert_eq!(resolve(id(a,a50_to_a51_b),&mut t),50);}
968+
assert_eq!(resolve_internal(id(a,a50_to_a51_b),&mut t),50);}
931969
}
932970

933971
#[test] fn hashing_tests () {
934-
let mut t = new_sctable();
935-
assert_eq!(new_mark(12,empty_ctxt,&mut t),2);
936-
assert_eq!(new_mark(13,empty_ctxt,&mut t),3);
972+
let mut t = new_sctable_internal();
973+
assert_eq!(new_mark_internal(12,empty_ctxt,&mut t),2);
974+
assert_eq!(new_mark_internal(13,empty_ctxt,&mut t),3);
937975
// using the same one again should result in the same index:
938-
assert_eq!(new_mark(12,empty_ctxt,&mut t),2);
976+
assert_eq!(new_mark_internal(12,empty_ctxt,&mut t),2);
939977
// I'm assuming that the rename table will behave the same....
940978
}
941979

0 commit comments

Comments
 (0)