Skip to content

Commit 44c6370

Browse files
committed
Move local numbering into ast_map.rs
This further simplifies the alias pass, which is sorely needed.
1 parent 1cda74d commit 44c6370

File tree

2 files changed

+67
-48
lines changed

2 files changed

+67
-48
lines changed

src/comp/middle/alias.rs

+25-31
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,32 @@ tag copied { not_allowed; copied; not_copied; }
1717

1818
type binding = @{node_id: node_id,
1919
span: span,
20-
local_id: uint,
2120
root_var: option::t<node_id>,
21+
local_id: uint,
2222
unsafe_tys: [ty::t],
2323
mutable ok: valid,
2424
mutable copied: copied};
2525
type scope = {bs: [binding], ret_style: ast::ret_style};
2626

2727
fn mk_binding(cx: ctx, id: node_id, span: span, root_var: option::t<node_id>,
2828
unsafe: [ty::t]) -> binding {
29-
ret @{node_id: id, span: span, local_id: cx.next_local,
30-
root_var: root_var, unsafe_tys: unsafe,
31-
mutable ok: valid, mutable copied: not_copied};
29+
ret @{node_id: id, span: span, root_var: root_var,
30+
local_id: local_id_of_node(cx, id),
31+
unsafe_tys: unsafe, mutable ok: valid,
32+
mutable copied: not_copied};
3233
}
3334

3435
tag local_info { local(uint); }
3536

3637
type copy_map = std::map::hashmap<node_id, ()>;
3738

3839
type ctx = {tcx: ty::ctxt,
39-
local_map: std::map::hashmap<node_id, local_info>,
40-
mutable next_local: uint,
4140
copy_map: copy_map};
4241

4342
fn check_crate(tcx: ty::ctxt, crate: @ast::crate) -> copy_map {
4443
// Stores information about object fields and function
4544
// arguments that's otherwise not easily available.
4645
let cx = @{tcx: tcx,
47-
local_map: std::map::new_int_hash(),
48-
mutable next_local: 0u,
4946
copy_map: std::map::new_int_hash()};
5047
let v = @{visit_fn: bind visit_fn(cx, _, _, _, _, _, _, _),
5148
visit_expr: bind visit_expr(cx, _, _, _),
@@ -133,13 +130,6 @@ fn visit_expr(cx: @ctx, ex: @ast::expr, sc: scope, v: vt<scope>) {
133130
if !handled { visit::visit_expr(ex, sc, v); }
134131
}
135132

136-
fn register_locals(cx: ctx, pat: @ast::pat) {
137-
for each pat in ast_util::pat_bindings(pat) {
138-
cx.local_map.insert(pat.id, local(cx.next_local));
139-
cx.next_local += 1u;
140-
}
141-
}
142-
143133
fn visit_decl(cx: @ctx, d: @ast::decl, sc: scope, v: vt<scope>) {
144134
visit::visit_decl(d, sc, v);
145135
alt d.node {
@@ -154,7 +144,6 @@ fn visit_decl(cx: @ctx, d: @ast::decl, sc: scope, v: vt<scope>) {
154144
}
155145
none. { }
156146
}
157-
register_locals(*cx, loc.node.pat);
158147
}
159148
}
160149
_ { }
@@ -200,14 +189,17 @@ fn check_call(cx: ctx, f: @ast::expr, args: [@ast::expr]) -> [binding] {
200189
}
201190
}
202191
let root_var = path_def_id(cx, root.ex);
203-
let new_bnd = mk_binding(cx, arg.id, arg.span, root_var,
204-
inner_mut(root.ds));
205-
new_bnd.copied = alt arg_t.mode {
206-
ast::by_move. { copied }
207-
ast::by_ref. { ret_ref ? not_allowed : not_copied }
208-
ast::by_mut_ref. { not_allowed }
209-
};
210-
bindings += [new_bnd];
192+
bindings += [@{node_id: arg.id,
193+
span: arg.span,
194+
root_var: root_var,
195+
local_id: 0u,
196+
unsafe_tys: inner_mut(root.ds),
197+
mutable ok: valid,
198+
mutable copied: alt arg_t.mode {
199+
ast::by_move. { copied }
200+
ast::by_ref. { ret_ref ? not_allowed : not_copied }
201+
ast::by_mut_ref. { not_allowed }
202+
}}];
211203
i += 1u;
212204
}
213205
let f_may_close =
@@ -283,7 +275,7 @@ fn check_ret_ref(cx: ctx, sc: scope, mut: bool, expr: @ast::expr) {
283275
let cur_node = did.node;
284276
while true {
285277
alt cx.tcx.items.find(cur_node) {
286-
some(ast_map::node_arg(arg)) {
278+
some(ast_map::node_arg(arg, _)) {
287279
if arg.mode == ast::by_move {
288280
bad = some("move-mode parameter");
289281
}
@@ -358,7 +350,6 @@ fn check_alt(cx: ctx, input: @ast::expr, arms: [ast::arm], sc: scope,
358350
new_bs += [mk_binding(cx, info.id, info.span, root_var,
359351
copy info.unsafe)];
360352
}
361-
register_locals(cx, a.pats[0]);
362353
visit::visit_arm(a, {bs: new_bs with sc}, v);
363354
}
364355
}
@@ -373,7 +364,6 @@ fn check_for_each(cx: ctx, local: @ast::local, call: @ast::expr,
373364
new_bs += [mk_binding(cx, proot.id, proot.span, none,
374365
inner_mut(proot.ds))];
375366
}
376-
register_locals(cx, local.node.pat);
377367
visit::visit_block(blk, {bs: new_bs with sc}, v);
378368
}
379369
}
@@ -401,17 +391,15 @@ fn check_for(cx: ctx, local: @ast::local, seq: @ast::expr, blk: ast::blk,
401391
new_bs += [mk_binding(cx, proot.id, proot.span, root_var,
402392
inner_mut(proot.ds))];
403393
}
404-
register_locals(cx, local.node.pat);
405394
visit::visit_block(blk, {bs: new_bs with sc}, v);
406395
}
407396

408397
fn check_var(cx: ctx, ex: @ast::expr, p: ast::path, id: ast::node_id,
409398
assign: bool, sc: scope) {
410399
let def = cx.tcx.def_map.get(id);
411-
if !def_is_local(def, true) { ret; }
400+
if !def_is_local(def, false) { ret; }
412401
let my_defnum = ast_util::def_id_of_def(def).node;
413-
let my_local_id =
414-
alt cx.local_map.find(my_defnum) { some(local(id)) { id } _ { 0u } };
402+
let my_local_id = local_id_of_node(cx, my_defnum);
415403
let var_t = ty::expr_ty(cx.tcx, ex);
416404
for b in sc.bs {
417405
// excludes variables introduced since the alias was made
@@ -557,6 +545,12 @@ fn def_is_local(d: ast::def, objfields_count: bool) -> bool {
557545
};
558546
}
559547

548+
fn local_id_of_node(cx: ctx, id: node_id) -> uint {
549+
alt cx.tcx.items.get(id) {
550+
ast_map::node_arg(_, id) | ast_map::node_local(id) { id }
551+
}
552+
}
553+
560554
// Heuristic, somewhat random way to decide whether to warn when inserting an
561555
// implicit copy.
562556
fn copy_is_expensive(tcx: ty::ctxt, ty: ty::t) -> bool {

src/comp/middle/ast_map.rs

+42-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import std::{smallintmap, option};
22
import syntax::ast::*;
3+
import syntax::ast_util;
34
import syntax::{visit, codemap};
45
import visit::vt;
56

@@ -8,46 +9,70 @@ tag ast_node {
89
node_obj_ctor(@item);
910
node_native_item(@native_item);
1011
node_expr(@expr);
11-
node_arg(arg);
12+
// Locals are numbered, because the alias analysis needs to know in which
13+
// order they are introduced.
14+
node_arg(arg, uint);
15+
node_local(uint);
1216
}
1317

1418
type map = std::map::hashmap<node_id, ast_node>;
19+
type ctx = @{map: map, mutable local_id: uint};
1520

1621
fn map_crate(c: crate) -> map {
1722
// FIXME: This is using an adapter to convert the smallintmap
1823
// interface to the hashmap interface. It would be better to just
1924
// convert everything to use the smallintmap.
20-
let map = new_smallintmap_int_adapter::<ast_node>();
25+
let cx = @{map: new_smallintmap_int_adapter::<ast_node>(),
26+
mutable local_id: 0u};
2127

2228
let v_map = visit::mk_simple_visitor
23-
(@{visit_item: bind map_item(map, _),
24-
visit_native_item: bind map_native_item(map, _),
25-
visit_expr: bind map_expr(map, _),
26-
visit_fn: bind map_fn(map, _, _, _, _, _)
27-
with *visit::default_simple_visitor()});
29+
(@{visit_item: bind map_item(cx, _),
30+
visit_native_item: bind map_native_item(cx, _),
31+
visit_expr: bind map_expr(cx, _),
32+
visit_fn: bind map_fn(cx, _, _, _, _, _),
33+
visit_local: bind map_local(cx, _),
34+
visit_arm: bind map_arm(cx, _)
35+
with *visit::default_simple_visitor()});
2836
visit::visit_crate(c, (), v_map);
29-
ret map;
37+
ret cx.map;
3038
}
3139

32-
fn map_fn(map: map, f: _fn, _tp: [ty_param], _sp: codemap::span,
40+
fn map_fn(cx: ctx, f: _fn, _tp: [ty_param], _sp: codemap::span,
3341
_name: fn_ident, _id: node_id) {
34-
for a in f.decl.inputs { map.insert(a.id, node_arg(a)); }
42+
for a in f.decl.inputs {
43+
cx.map.insert(a.id, node_arg(a, cx.local_id));
44+
cx.local_id += 1u;
45+
}
46+
}
47+
48+
fn map_local(cx: ctx, loc: @local) {
49+
for each p in ast_util::pat_bindings(loc.node.pat) {
50+
cx.map.insert(p.id, node_local(cx.local_id));
51+
cx.local_id += 1u;
52+
}
53+
}
54+
55+
fn map_arm(cx: ctx, arm: arm) {
56+
for each p in ast_util::pat_bindings(arm.pats[0]) {
57+
cx.map.insert(p.id, node_local(cx.local_id));
58+
cx.local_id += 1u;
59+
}
3560
}
3661

37-
fn map_item(map: map, i: @item) {
38-
map.insert(i.id, node_item(i));
62+
fn map_item(cx: ctx, i: @item) {
63+
cx.map.insert(i.id, node_item(i));
3964
alt i.node {
40-
item_obj(_, _, ctor_id) { map.insert(ctor_id, node_obj_ctor(i)); }
65+
item_obj(_, _, ctor_id) { cx.map.insert(ctor_id, node_obj_ctor(i)); }
4166
_ { }
4267
}
4368
}
4469

45-
fn map_native_item(map: map, i: @native_item) {
46-
map.insert(i.id, node_native_item(i));
70+
fn map_native_item(cx: ctx, i: @native_item) {
71+
cx.map.insert(i.id, node_native_item(i));
4772
}
4873

49-
fn map_expr(map: map, ex: @expr) {
50-
map.insert(ex.id, node_expr(ex));
74+
fn map_expr(cx: ctx, ex: @expr) {
75+
cx.map.insert(ex.id, node_expr(ex));
5176
}
5277

5378
fn new_smallintmap_int_adapter<@V>() -> std::map::hashmap<int, V> {

0 commit comments

Comments
 (0)