|
| 1 | +import syntax::{visit, ast_util}; |
| 2 | +import syntax::ast::*; |
| 3 | +import std::list::{list, nil, cons, tail}; |
| 4 | +import std::{vec, list, option}; |
| 5 | + |
| 6 | +// Marks expr_paths that are last uses. |
| 7 | +type last_uses = std::map::hashmap<node_id, ()>; |
| 8 | + |
| 9 | +tag seen { unset; seen(node_id); } |
| 10 | +tag block_type { func; loop; } |
| 11 | + |
| 12 | +type set = [{def: node_id, exprs: list<node_id>}]; |
| 13 | +type bl = @{type: block_type, mutable second: bool, mutable exits: [set]}; |
| 14 | + |
| 15 | +type ctx = {last_uses: std::map::hashmap<node_id, bool>, |
| 16 | + def_map: resolve::def_map, |
| 17 | + tcx: ty::ctxt, |
| 18 | + // The current set of local last uses |
| 19 | + mutable current: set, |
| 20 | + mutable blocks: list<bl>}; |
| 21 | + |
| 22 | +fn find_last_uses(c: @crate, def_map: resolve::def_map, tcx: ty::ctxt) |
| 23 | + -> last_uses { |
| 24 | + let v = visit::mk_vt(@{visit_expr: visit_expr, |
| 25 | + visit_fn: visit_fn |
| 26 | + with *visit::default_visitor()}); |
| 27 | + let cx = {last_uses: std::map::new_int_hash(), |
| 28 | + def_map: def_map, |
| 29 | + tcx: tcx, |
| 30 | + mutable current: [], |
| 31 | + mutable blocks: nil}; |
| 32 | + visit::visit_crate(*c, cx, v); |
| 33 | + let mini_table = std::map::new_int_hash(); |
| 34 | + cx.last_uses.items {|key, val| if val { mini_table.insert(key, ()); }} |
| 35 | + ret mini_table; |
| 36 | +} |
| 37 | + |
| 38 | +fn visit_expr(ex: @expr, cx: ctx, v: visit::vt<ctx>) { |
| 39 | + alt ex.node { |
| 40 | + expr_ret(oexpr) { |
| 41 | + visit::visit_expr_opt(oexpr, cx, v); |
| 42 | + if !add_block_exit(cx, func) { leave_fn(cx); } |
| 43 | + } |
| 44 | + expr_fail(oexpr) { |
| 45 | + visit::visit_expr_opt(oexpr, cx, v); |
| 46 | + leave_fn(cx); |
| 47 | + } |
| 48 | + expr_break. { add_block_exit(cx, loop); } |
| 49 | + expr_while(_, _) | expr_do_while(_, _) { |
| 50 | + visit_block(loop, cx) {|| visit::visit_expr(ex, cx, v);} |
| 51 | + } |
| 52 | + expr_for(_, coll, blk) { |
| 53 | + v.visit_expr(coll, cx, v); |
| 54 | + visit_block(loop, cx) {|| visit::visit_block(blk, cx, v);} |
| 55 | + } |
| 56 | + expr_ternary(_, _, _) { |
| 57 | + v.visit_expr(ast_util::ternary_to_if(ex), cx, v); |
| 58 | + } |
| 59 | + expr_alt(input, arms) { |
| 60 | + v.visit_expr(input, cx, v); |
| 61 | + let before = cx.current, sets = []; |
| 62 | + for arm in arms { |
| 63 | + cx.current = before; |
| 64 | + v.visit_arm(arm, cx, v); |
| 65 | + sets += [cx.current]; |
| 66 | + } |
| 67 | + cx.current = join_branches(sets); |
| 68 | + } |
| 69 | + expr_if(cond, then, els) { |
| 70 | + v.visit_expr(cond, cx, v); |
| 71 | + let cur = cx.current; |
| 72 | + visit::visit_block(then, cx, v); |
| 73 | + cx.current <-> cur; |
| 74 | + visit::visit_expr_opt(els, cx, v); |
| 75 | + cx.current = join_branches([cur, cx.current]); |
| 76 | + } |
| 77 | + expr_path(_) { |
| 78 | + alt clear_if_path(cx, ex, v, false) { |
| 79 | + option::some(my_def) { |
| 80 | + cx.current += [{def: my_def, exprs: cons(ex.id, @nil)}]; |
| 81 | + } |
| 82 | + _ {} |
| 83 | + } |
| 84 | + } |
| 85 | + expr_swap(lhs, rhs) { |
| 86 | + clear_if_path(cx, lhs, v, false); |
| 87 | + clear_if_path(cx, rhs, v, false); |
| 88 | + } |
| 89 | + expr_move(dest, src) | expr_assign(dest, src) { |
| 90 | + v.visit_expr(src, cx, v); |
| 91 | + clear_if_path(cx, dest, v, true); |
| 92 | + } |
| 93 | + expr_assign_op(_, dest, src) { |
| 94 | + v.visit_expr(src, cx, v); |
| 95 | + v.visit_expr(dest, cx, v); |
| 96 | + clear_if_path(cx, dest, v, true); |
| 97 | + } |
| 98 | + expr_call(f, args, _) { |
| 99 | + v.visit_expr(f, cx, v); |
| 100 | + let i = 0u; |
| 101 | + for arg_t in ty::ty_fn_args(cx.tcx, ty::expr_ty(cx.tcx, f)) { |
| 102 | + alt arg_t.mode { |
| 103 | + by_mut_ref. { clear_if_path(cx, args[i], v, false); } |
| 104 | + _ { v.visit_expr(args[i], cx, v); } |
| 105 | + } |
| 106 | + i += 1u; |
| 107 | + } |
| 108 | + } |
| 109 | + _ { visit::visit_expr(ex, cx, v); } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +fn visit_fn(f: _fn, tps: [ty_param], sp: syntax::codemap::span, |
| 114 | + ident: fn_ident, id: node_id, cx: ctx, v: visit::vt<ctx>) { |
| 115 | + if f.proto == proto_block { |
| 116 | + visit_block(func, cx, {|| |
| 117 | + visit::visit_fn(f, tps, sp, ident, id, cx, v); |
| 118 | + }); |
| 119 | + } else { |
| 120 | + let old = nil; |
| 121 | + cx.blocks <-> old; |
| 122 | + visit::visit_fn(f, tps, sp, ident, id, cx, v); |
| 123 | + cx.blocks <-> old; |
| 124 | + leave_fn(cx); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +fn visit_block(tp: block_type, cx: ctx, visit: block()) { |
| 129 | + let local = @{type: tp, mutable second: false, mutable exits: []}; |
| 130 | + cx.blocks = cons(local, @cx.blocks); |
| 131 | + visit(); |
| 132 | + local.second = true; |
| 133 | + visit(); |
| 134 | + cx.blocks = tail(cx.blocks); |
| 135 | + cx.current = join_branches(local.exits); |
| 136 | +} |
| 137 | + |
| 138 | +fn add_block_exit(cx: ctx, tp: block_type) -> bool { |
| 139 | + let cur = cx.blocks; |
| 140 | + while cur != nil { |
| 141 | + alt cur { |
| 142 | + cons(b, tail) { |
| 143 | + if (b.type == tp) { |
| 144 | + if !b.second { b.exits += [cx.current]; } |
| 145 | + ret true; |
| 146 | + } |
| 147 | + cur = *tail; |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + ret false; |
| 152 | +} |
| 153 | + |
| 154 | +fn join_branches(branches: [set]) -> set { |
| 155 | + let found: set = [], i = 0u, l = vec::len(branches); |
| 156 | + for set in branches { |
| 157 | + i += 1u; |
| 158 | + for {def, exprs} in set { |
| 159 | + if !vec::any({|v| v.def == def}, found) { |
| 160 | + let j = i, ne = exprs; |
| 161 | + while j < l { |
| 162 | + for {def: d2, exprs} in branches[j] { |
| 163 | + if d2 == def { |
| 164 | + list::iter(exprs) {|e| |
| 165 | + if !list::has(ne, e) { ne = cons(e, @ne); } |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + j += 1u; |
| 170 | + } |
| 171 | + found += [{def: def, exprs: ne}]; |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + ret found; |
| 176 | +} |
| 177 | + |
| 178 | +fn leave_fn(cx: ctx) { |
| 179 | + for {def, exprs} in cx.current { |
| 180 | + list::iter(exprs) {|ex_id| |
| 181 | + if !cx.last_uses.contains_key(ex_id) { |
| 182 | + cx.last_uses.insert(ex_id, true); |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +fn clear_in_current(cx: ctx, my_def: node_id, to: bool) { |
| 189 | + for {def, exprs} in cx.current { |
| 190 | + if def == my_def { |
| 191 | + list::iter(exprs) {|expr| |
| 192 | + if !to || !cx.last_uses.contains_key(expr) { |
| 193 | + cx.last_uses.insert(expr, to); |
| 194 | + } |
| 195 | + } |
| 196 | + cx.current = vec::filter({|x| x.def != my_def}, |
| 197 | + copy cx.current); |
| 198 | + break; |
| 199 | + } |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +fn clear_if_path(cx: ctx, ex: @expr, v: visit::vt<ctx>, to: bool) |
| 204 | + -> option::t<node_id> { |
| 205 | + alt ex.node { |
| 206 | + expr_path(_) { |
| 207 | + alt cx.def_map.get(ex.id) { |
| 208 | + def_local(def_id, let_copy.) | def_arg(def_id, by_copy.) | |
| 209 | + def_arg(def_id, by_move.) { |
| 210 | + clear_in_current(cx, def_id.node, to); |
| 211 | + ret option::some(def_id.node); |
| 212 | + } |
| 213 | + _ {} |
| 214 | + } |
| 215 | + } |
| 216 | + _ { v.visit_expr(ex, cx, v); } |
| 217 | + } |
| 218 | + ret option::none; |
| 219 | +} |
0 commit comments