]> git.lizzy.rs Git - rust.git/blob - src/comp/middle/capture.rs
Remove proto_sugar and 'lambda' as keyword, commit to fn@.
[rust.git] / src / comp / middle / capture.rs
1 import syntax::{ast, ast_util};
2 import std::map;
3
4 export capture_mode;
5 export capture_var;
6 export capture_map;
7 export check_capture_clause;
8 export compute_capture_vars;
9 export cap_copy;
10 export cap_move;
11 export cap_drop;
12 export cap_ref;
13
14 tag capture_mode {
15     cap_copy; //< Copy the value into the closure.
16     cap_move; //< Move the value into the closure.
17     cap_drop; //< Drop value after creating closure.
18     cap_ref;  //< Reference directly from parent stack frame (block fn).
19 }
20
21 type capture_var = {
22     def: ast::def,     //< The variable being accessed free.
23     mode: capture_mode //< How is the variable being accessed.
24 };
25
26 type capture_map = map::hashmap<ast::def_id, capture_var>;
27
28 // checks the capture clause for a fn_expr() and issues warnings or
29 // errors for any irregularities which we identify.
30 fn check_capture_clause(tcx: ty::ctxt,
31                         fn_expr_id: ast::node_id,
32                         fn_proto: ast::proto,
33                         cap_clause: ast::capture_clause) {
34     let freevars = freevars::get_freevars(tcx, fn_expr_id);
35     let seen_defs = map::new_int_hash();
36
37     let check_capture_item = fn@(&&cap_item: @ast::capture_item) {
38         let cap_def = tcx.def_map.get(cap_item.id);
39         if !vec::any(*freevars, {|fv| fv.def == cap_def}) {
40             tcx.sess.span_warn(
41                 cap_item.span,
42                 #fmt("Captured variable '%s' not used in closure",
43                      cap_item.name));
44         }
45
46         let cap_def_id = ast_util::def_id_of_def(cap_def).node;
47         if !seen_defs.insert(cap_def_id, ()) {
48             tcx.sess.span_err(
49                 cap_item.span,
50                 #fmt("Variable '%s' captured more than once",
51                      cap_item.name));
52         }
53     };
54
55     let check_not_upvar = fn@(&&cap_item: @ast::capture_item) {
56         alt tcx.def_map.get(cap_item.id) {
57           ast::def_upvar(_, _, _) {
58             tcx.sess.span_err(
59                 cap_item.span,
60                 #fmt("Upvars (like '%s') cannot be moved into a closure",
61                      cap_item.name));
62           }
63           _ {}
64         }
65     };
66
67     let check_block_captures = fn@(v: [@ast::capture_item]) {
68         if check vec::is_not_empty(v) {
69             let cap_item0 = vec::head(v);
70             tcx.sess.span_err(
71                 cap_item0.span,
72                 "Cannot capture values explicitly with a block closure");
73         }
74     };
75
76     alt fn_proto {
77       ast::proto_block. {
78         check_block_captures(cap_clause.copies);
79         check_block_captures(cap_clause.moves);
80       }
81       ast::proto_bare. | ast::proto_shared. | ast::proto_send. {
82         vec::iter(cap_clause.copies, check_capture_item);
83         vec::iter(cap_clause.moves, check_capture_item);
84         vec::iter(cap_clause.moves, check_not_upvar);
85       }
86     }
87 }
88
89 fn compute_capture_vars(tcx: ty::ctxt,
90                         fn_expr_id: ast::node_id,
91                         fn_proto: ast::proto,
92                         cap_clause: ast::capture_clause) -> [capture_var] {
93     let freevars = freevars::get_freevars(tcx, fn_expr_id);
94     let cap_map = map::new_int_hash();
95
96     vec::iter(cap_clause.copies) { |cap_item|
97         let cap_def = tcx.def_map.get(cap_item.id);
98         let cap_def_id = ast_util::def_id_of_def(cap_def).node;
99         if vec::any(*freevars, {|fv| fv.def == cap_def}) {
100             cap_map.insert(cap_def_id, { def:cap_def, mode:cap_copy });
101         }
102     }
103
104     vec::iter(cap_clause.moves) { |cap_item|
105         let cap_def = tcx.def_map.get(cap_item.id);
106         let cap_def_id = ast_util::def_id_of_def(cap_def).node;
107         if vec::any(*freevars, {|fv| fv.def == cap_def}) {
108             cap_map.insert(cap_def_id, { def:cap_def, mode:cap_move });
109         } else {
110             cap_map.insert(cap_def_id, { def:cap_def, mode:cap_drop });
111         }
112     }
113
114     let implicit_mode = alt fn_proto {
115       ast::proto_block. { cap_ref }
116       ast::proto_bare. | ast::proto_shared. | ast::proto_send. { cap_copy }
117     };
118
119     vec::iter(*freevars) { |fvar|
120         let fvar_def_id = ast_util::def_id_of_def(fvar.def).node;
121         alt cap_map.find(fvar_def_id) {
122           option::some(_) { /* was explicitly named, do nothing */ }
123           option::none. {
124             cap_map.insert(fvar_def_id, {def:fvar.def, mode:implicit_mode});
125           }
126         }
127     }
128
129     let result = [];
130     cap_map.values { |cap_var| result += [cap_var]; }
131     ret result;
132 }