1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
import option;
import std::smallintmap;
import syntax::ast::*;
import syntax::ast_util;
import syntax::{visit, codemap};
tag ast_node {
node_item(@item);
node_obj_ctor(@item);
node_native_item(@native_item);
node_method(@method);
node_expr(@expr);
// Locals are numbered, because the alias analysis needs to know in which
// order they are introduced.
node_arg(arg, uint);
node_local(uint);
}
type map = std::map::hashmap<node_id, ast_node>;
type ctx = @{map: map, mutable local_id: uint};
fn map_crate(c: crate) -> map {
// FIXME: This is using an adapter to convert the smallintmap
// interface to the hashmap interface. It would be better to just
// convert everything to use the smallintmap.
let cx = @{map: new_smallintmap_int_adapter::<ast_node>(),
mutable local_id: 0u};
let v_map = visit::mk_simple_visitor
(@{visit_item: bind map_item(cx, _),
visit_native_item: bind map_native_item(cx, _),
visit_expr: bind map_expr(cx, _),
visit_fn: bind map_fn(cx, _, _, _, _, _),
visit_local: bind map_local(cx, _),
visit_arm: bind map_arm(cx, _)
with *visit::default_simple_visitor()});
visit::visit_crate(c, (), v_map);
ret cx.map;
}
fn map_fn(cx: ctx, f: _fn, _tp: [ty_param], _sp: codemap::span,
_name: fn_ident, _id: node_id) {
for a in f.decl.inputs {
cx.map.insert(a.id, node_arg(a, cx.local_id));
cx.local_id += 1u;
}
}
fn map_local(cx: ctx, loc: @local) {
ast_util::pat_bindings(loc.node.pat) {|p|
cx.map.insert(p.id, node_local(cx.local_id));
cx.local_id += 1u;
};
}
fn map_arm(cx: ctx, arm: arm) {
ast_util::pat_bindings(arm.pats[0]) {|p|
cx.map.insert(p.id, node_local(cx.local_id));
cx.local_id += 1u;
};
}
fn map_item(cx: ctx, i: @item) {
cx.map.insert(i.id, node_item(i));
alt i.node {
item_obj(_, _, ctor_id) { cx.map.insert(ctor_id, node_obj_ctor(i)); }
item_impl(_, _, ms) {
for m in ms { cx.map.insert(m.node.id, node_method(m)); }
}
_ { }
}
}
fn map_native_item(cx: ctx, i: @native_item) {
cx.map.insert(i.id, node_native_item(i));
}
fn map_expr(cx: ctx, ex: @expr) {
cx.map.insert(ex.id, node_expr(ex));
}
fn new_smallintmap_int_adapter<copy V>() -> std::map::hashmap<int, V> {
let key_idx = fn (&&key: int) -> uint { key as uint };
let idx_key = fn (idx: uint) -> int { idx as int };
ret new_smallintmap_adapter(key_idx, idx_key);
}
// This creates an object with the hashmap interface backed
// by the smallintmap type, because I don't want to go through
// the entire codebase adapting all the callsites to the different
// interface.
// FIXME: hashmap and smallintmap should support the same interface.
fn new_smallintmap_adapter<copy K, copy V>(key_idx: fn(K) -> uint,
idx_key: fn(uint) -> K)
-> std::map::hashmap<K, V> {
obj adapter<copy K, copy V>(map: smallintmap::smallintmap<V>,
key_idx: fn(K) -> uint,
idx_key: fn(uint) -> K) {
fn size() -> uint { fail }
fn insert(key: K, value: V) -> bool {
let exists = smallintmap::contains_key(map, key_idx(key));
smallintmap::insert(map, key_idx(key), value);
ret !exists;
}
fn contains_key(key: K) -> bool {
ret smallintmap::contains_key(map, key_idx(key));
}
fn get(key: K) -> V { ret smallintmap::get(map, key_idx(key)); }
fn find(key: K) -> option::t<V> {
ret smallintmap::find(map, key_idx(key));
}
fn remove(_key: K) -> option::t<V> { fail }
fn rehash() { fail }
fn items(it: block(K, V)) {
let idx = 0u;
for item in map.v {
alt item {
option::some(elt) {
it(idx_key(idx), elt);
}
option::none. { }
}
idx += 1u;
}
}
fn keys(it: block(K)) {
let idx = 0u;
for item in map.v {
if item != option::none { it(idx_key(idx)); }
idx += 1u;
}
}
fn values(it: block(V)) {
for item in map.v {
alt item { option::some(elt) { it(elt); } _ {} }
}
}
}
let map = smallintmap::mk::<V>();
ret adapter(map, key_idx, idx_key);
}
fn node_span(node: ast_node) -> codemap::span {
alt node {
node_item(item) { item.span }
node_obj_ctor(item) { item.span }
node_native_item(nitem) { nitem.span }
node_expr(expr) { expr.span }
}
}
#[cfg(test)]
mod test {
import syntax::ast_util;
#[test]
fn test_node_span_item() {
let expected: codemap::span = ast_util::mk_sp(20u, 30u);
let node =
node_item(@{ident: "test",
attrs: [],
id: 0,
node: item_mod({view_items: [], items: []}),
span: expected});
assert (node_span(node) == expected);
}
#[test]
fn test_node_span_obj_ctor() {
let expected: codemap::span = ast_util::mk_sp(20u, 30u);
let node =
node_obj_ctor(@{ident: "test",
attrs: [],
id: 0,
node: item_mod({view_items: [], items: []}),
span: expected});
assert (node_span(node) == expected);
}
#[test]
fn test_node_span_native_item() {
let expected: codemap::span = ast_util::mk_sp(20u, 30u);
let node =
node_native_item(@{ident: "test",
attrs: [],
node: native_item_ty,
id: 0,
span: expected});
assert (node_span(node) == expected);
}
#[test]
fn test_node_span_expr() {
let expected: codemap::span = ast_util::mk_sp(20u, 30u);
let node = node_expr(@{id: 0, node: expr_break, span: expected});
assert (node_span(node) == expected);
}
}
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:
|