@@ -6,6 +6,23 @@ import std::vec;
6
6
import std:: str;
7
7
import syntax:: ast;
8
8
9
+ export cstore;
10
+ export cnum_map;
11
+ export crate_metadata;
12
+ export mk_cstore;
13
+ export get_crate_data;
14
+ export set_crate_data;
15
+ export have_crate_data;
16
+ export iter_crate_data;
17
+ export add_used_crate_file;
18
+ export get_used_crate_files;
19
+ export add_used_library;
20
+ export get_used_libraries;
21
+ export add_used_link_args;
22
+ export get_used_link_args;
23
+ export add_use_stmt_cnum;
24
+ export get_use_stmt_cnum;
25
+
9
26
// A map from external crate numbers (as decoded from some crate file) to
10
27
// local crate numbers (as generated during this session). Each external
11
28
// crate may refer to types in other external crates, and each has their
@@ -16,73 +33,100 @@ type crate_metadata = rec(str name,
16
33
vec[ u8] data ,
17
34
cnum_map cnum_map) ;
18
35
36
+ // This is a bit of an experiment at encapsulating the data in cstore. By
37
+ // keeping all the data in a non-exported tag variant, it's impossible for
38
+ // other modules to access the cstore's private data. This could also be
39
+ // achieved with an obj, but at the expense of a vtable. Not sure if this is a
40
+ // good pattern or not.
41
+ tag cstore {
42
+ private( cstore_private) ;
43
+ }
44
+
45
+ type cstore_private = @rec ( map:: hashmap[ ast:: crate_num, crate_metadata] metas ,
46
+ use_crate_map use_crate_map,
47
+ mutable vec[ str] used_crate_files ,
48
+ mutable vec[ str] used_libraries ,
49
+ mutable vec[ str] used_link_args ) ;
50
+
19
51
// Map from node_id's of local use statements to crate numbers
20
52
type use_crate_map = map:: hashmap [ ast:: node_id, ast:: crate_num] ;
21
53
22
- type cstore = @rec ( map:: hashmap[ ast:: crate_num, crate_metadata] metas ,
23
- use_crate_map use_crate_map,
24
- mutable vec[ str] used_crate_files ,
25
- mutable vec[ str] used_libraries ,
26
- mutable vec[ str] used_link_args ) ;
54
+ // Internal method to retrieve the data from the cstore
55
+ fn p ( & cstore cstore) -> cstore_private {
56
+ alt ( cstore) {
57
+ case ( private ( ?p) ) { p }
58
+ }
59
+ }
27
60
28
61
fn mk_cstore ( ) -> cstore {
29
62
auto meta_cache = map:: new_int_hash[ crate_metadata] ( ) ;
30
63
auto crate_map = map:: new_int_hash[ ast:: crate_num] ( ) ;
31
- ret @rec( metas = meta_cache,
32
- use_crate_map = crate_map,
33
- mutable used_crate_files = [ ] ,
34
- mutable used_libraries = [ ] ,
35
- mutable used_link_args = [ ] ) ;
64
+ ret private ( @rec ( metas = meta_cache,
65
+ use_crate_map = crate_map,
66
+ mutable used_crate_files = [ ] ,
67
+ mutable used_libraries = [ ] ,
68
+ mutable used_link_args = [ ] ) ) ;
36
69
}
37
70
38
71
fn get_crate_data ( & cstore cstore, ast:: crate_num cnum) -> crate_metadata {
39
- ret cstore. metas . get ( cnum) ;
72
+ ret p ( cstore) . metas . get ( cnum) ;
40
73
}
41
74
42
75
fn set_crate_data ( & cstore cstore, ast:: crate_num cnum, & crate_metadata data) {
43
- cstore. metas . insert ( cnum, data) ;
76
+ p ( cstore) . metas . insert ( cnum, data) ;
44
77
}
45
78
46
79
fn have_crate_data ( & cstore cstore, ast:: crate_num cnum) -> bool {
47
- ret cstore. metas . contains_key ( cnum) ;
80
+ ret p( cstore) . metas . contains_key ( cnum) ;
81
+ }
82
+
83
+ iter iter_crate_data ( & cstore cstore) -> @tup ( ast:: crate_num, crate_metadata) {
84
+ for each ( @tup( ast:: crate_num, crate_metadata) kv
85
+ in p ( cstore) . metas . items ( ) ) {
86
+ put kv;
87
+ }
48
88
}
49
89
50
90
fn add_used_crate_file ( & cstore cstore, & str lib) {
51
- if ( !vec:: member ( lib, cstore. used_crate_files ) ) {
52
- cstore. used_crate_files += [ lib] ;
91
+ if ( !vec:: member ( lib, p ( cstore) . used_crate_files ) ) {
92
+ p ( cstore) . used_crate_files += [ lib] ;
53
93
}
54
94
}
55
95
56
96
fn get_used_crate_files ( & cstore cstore) -> vec[ str ] {
57
- ret cstore. used_crate_files ;
97
+ ret p ( cstore) . used_crate_files ;
58
98
}
59
99
60
100
fn add_used_library ( & cstore cstore, & str lib) -> bool {
61
101
if ( lib == "" ) { ret false ; }
62
102
63
- if ( vec:: member ( lib, cstore. used_libraries ) ) {
103
+ if ( vec:: member ( lib, p ( cstore) . used_libraries ) ) {
64
104
ret false ;
65
105
}
66
106
67
- cstore. used_libraries += [ lib] ;
107
+ p ( cstore) . used_libraries += [ lib] ;
68
108
ret true;
69
109
}
70
110
71
111
fn get_used_libraries ( & cstore cstore) -> vec[ str ] {
72
- ret cstore. used_libraries ;
112
+ ret p ( cstore) . used_libraries ;
73
113
}
74
114
75
115
fn add_used_link_args ( & cstore cstore, & str args ) {
76
- cstore. used_link_args += str:: split ( args, ' ' as u8 ) ;
116
+ p ( cstore) . used_link_args += str:: split ( args, ' ' as u8 ) ;
77
117
}
78
118
79
119
fn get_used_link_args ( & cstore cstore) -> vec[ str ] {
80
- ret cstore. used_link_args ;
120
+ ret p ( cstore) . used_link_args ;
81
121
}
82
122
83
123
fn add_use_stmt_cnum ( & cstore cstore, ast:: node_id use_id,
84
124
ast:: crate_num cnum) {
85
- cstore. use_crate_map . insert ( use_id, cnum) ;
125
+ p ( cstore) . use_crate_map . insert ( use_id, cnum) ;
126
+ }
127
+
128
+ fn get_use_stmt_cnum ( & cstore cstore, ast:: node_id use_id) -> ast:: crate_num {
129
+ ret p( cstore) . use_crate_map . get ( use_id) ;
86
130
}
87
131
88
132
// Local Variables:
0 commit comments