1 // A pass that annotates for each loops and functions with the free
2 // variables that they contain.
4 import syntax::print::pprust::path_to_str;
8 import syntax::{ast, ast_util, visit};
9 import middle::resolve;
10 import syntax::codemap::span;
12 export annotate_freevars;
18 // A vector of defs representing the free variables referred to in a function.
19 // (The def_upvar will already have been stripped).
20 type freevar_entry = {
21 def: ast::def, //< The variable being accessed free.
22 span: span //< First span where it is accessed (there can be multiple)
24 type freevar_info = @[@freevar_entry];
25 type freevar_map = hashmap<ast::node_id, freevar_info>;
27 // Searches through part of the AST for all references to locals or
28 // upvars in this frame and returns the list of definition IDs thus found.
29 // Since we want to be able to collect upvars in some arbitrary piece
30 // of the AST, we take a walker function that we invoke with a visitor
31 // in order to start the search.
32 fn collect_freevars(def_map: resolve::def_map, blk: ast::blk)
34 let seen = new_int_hash();
35 let refs = @mutable [];
37 fn ignore_item(_i: @ast::item, &&_depth: int, _v: visit::vt<int>) { }
40 fn@ (expr: @ast::expr, &&depth: int, v: visit::vt<int>) {
42 ast::expr_fn(proto, decl, _, captures) {
43 if proto != ast::proto_bare {
44 visit::visit_expr(expr, depth + 1, v);
47 ast::expr_fn_block(_, _) {
48 visit::visit_expr(expr, depth + 1, v);
50 ast::expr_path(path) {
52 alt def_map.find(expr.id) {
53 none. { fail ("Not found: " + path_to_str(path)) }
58 ast::def_upvar(_, inner, _) { def = *inner; }
63 if i == depth { // Made it to end of loop
64 let dnum = ast_util::def_id_of_def(def).node;
65 if !seen.contains_key(dnum) {
66 *refs += [@{def:def, span:expr.span}];
67 seen.insert(dnum, ());
73 _ { visit::visit_expr(expr, depth, v); }
77 let v = visit::mk_vt(@{visit_item: ignore_item, visit_expr: walk_expr
78 with *visit::default_visitor()});
79 v.visit_block(blk, 1, v);
83 // Build a map from every function and for-each body to a set of the
84 // freevars contained in it. The implementation is not particularly
85 // efficient as it fully recomputes the free variables at every
86 // node of interest rather than building up the free variables in
87 // one pass. This could be improved upon if it turns out to matter.
88 fn annotate_freevars(def_map: resolve::def_map, crate: @ast::crate) ->
90 let freevars = new_int_hash();
92 let walk_fn = fn@ (_fk: visit::fn_kind, _decl: ast::fn_decl,
93 blk: ast::blk, _sp: span, nid: ast::node_id) {
94 let vars = collect_freevars(def_map, blk);
95 freevars.insert(nid, vars);
99 visit::mk_simple_visitor(@{visit_fn: walk_fn
100 with *visit::default_simple_visitor()});
101 visit::visit_crate(*crate, (), visitor);
106 fn get_freevars(tcx: ty::ctxt, fid: ast::node_id) -> freevar_info {
107 alt tcx.freevars.find(fid) {
108 none. { fail "get_freevars: " + int::str(fid) + " has no freevars"; }
112 fn has_freevars(tcx: ty::ctxt, fid: ast::node_id) -> bool {
113 ret vec::len(*get_freevars(tcx, fid)) != 0u;
119 // indent-tabs-mode: nil
121 // buffer-file-coding-system: utf-8-unix