blob: d631340fc4e31c974c1ed4bc287b2b5ed3157a7e [file] [log] [blame]
Niko Matsakis86b6e6e2013-05-10 17:10:351// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
11use middle::cfg::*;
12use middle::graph;
13use middle::typeck;
14use middle::ty;
Niko Matsakis86b6e6e2013-05-10 17:10:3515use syntax::ast;
16use syntax::ast_util;
Alex Crichtonbec7b762014-02-28 22:34:2617use util::nodemap::NodeMap;
Niko Matsakis86b6e6e2013-05-10 17:10:3518
Eduard Burtescu9b1fee82014-03-06 03:07:4719struct CFGBuilder<'a> {
20 tcx: &'a ty::ctxt,
Eduard Burtescu7a588ce2014-02-26 14:01:3621 method_map: typeck::MethodMap,
Alex Crichtonbec7b762014-02-28 22:34:2622 exit_map: NodeMap<CFGIndex>,
Niko Matsakis86b6e6e2013-05-10 17:10:3523 graph: CFGGraph,
Patrick Walton3b6e9d42014-03-04 18:02:4924 loop_scopes: Vec<LoopScope> ,
Niko Matsakis86b6e6e2013-05-10 17:10:3525}
26
27struct LoopScope {
Michael Woerister8a329772013-07-27 08:25:5928 loop_id: ast::NodeId, // id of loop/while node
Niko Matsakis86b6e6e2013-05-10 17:10:3529 continue_index: CFGIndex, // where to go on a `loop`
30 break_index: CFGIndex, // where to go on a `break
31}
32
Eduard Burtescu9b1fee82014-03-06 03:07:4733pub fn construct(tcx: &ty::ctxt,
Eduard Burtescu7a588ce2014-02-26 14:01:3634 method_map: typeck::MethodMap,
Michael Woerister4bd14242013-07-19 05:38:5535 blk: &ast::Block) -> CFG {
Niko Matsakis86b6e6e2013-05-10 17:10:3536 let mut cfg_builder = CFGBuilder {
Alex Crichtonbec7b762014-02-28 22:34:2637 exit_map: NodeMap::new(),
Niko Matsakis86b6e6e2013-05-10 17:10:3538 graph: graph::Graph::new(),
39 tcx: tcx,
40 method_map: method_map,
Patrick Walton3b6e9d42014-03-04 18:02:4941 loop_scopes: Vec::new()
Niko Matsakis86b6e6e2013-05-10 17:10:3542 };
43 let entry = cfg_builder.add_node(0, []);
44 let exit = cfg_builder.block(blk, entry);
Alex Crichtonab387a62013-11-28 20:22:5345 let CFGBuilder {exit_map, graph, ..} = cfg_builder;
Niko Matsakis86b6e6e2013-05-10 17:10:3546 CFG {exit_map: exit_map,
47 graph: graph,
48 entry: entry,
49 exit: exit}
50}
51
Eduard Burtescu9b1fee82014-03-06 03:07:4752impl<'a> CFGBuilder<'a> {
Michael Woerister4bd14242013-07-19 05:38:5553 fn block(&mut self, blk: &ast::Block, pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:3554 let mut stmts_exit = pred;
Daniel Micay100894552013-08-03 16:45:2355 for &stmt in blk.stmts.iter() {
Niko Matsakis86b6e6e2013-05-10 17:10:3556 stmts_exit = self.stmt(stmt, stmts_exit);
57 }
58
Michael Woerister0cc70742013-07-16 18:08:3559 let expr_exit = self.opt_expr(blk.expr, stmts_exit);
Niko Matsakis86b6e6e2013-05-10 17:10:3560
Michael Woerister0cc70742013-07-16 18:08:3561 self.add_node(blk.id, [expr_exit])
Niko Matsakis86b6e6e2013-05-10 17:10:3562 }
63
Marvin Löbel74190852013-09-02 01:45:3764 fn stmt(&mut self, stmt: @ast::Stmt, pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:3565 match stmt.node {
Marvin Löbel74190852013-09-02 01:45:3766 ast::StmtDecl(decl, _) => {
Niko Matsakis86b6e6e2013-05-10 17:10:3567 self.decl(decl, pred)
68 }
69
Marvin Löbel74190852013-09-02 01:45:3770 ast::StmtExpr(expr, _) | ast::StmtSemi(expr, _) => {
Niko Matsakis86b6e6e2013-05-10 17:10:3571 self.expr(expr, pred)
72 }
73
Alex Crichtonab387a62013-11-28 20:22:5374 ast::StmtMac(..) => {
Niko Matsakis86b6e6e2013-05-10 17:10:3575 self.tcx.sess.span_bug(stmt.span, "unexpanded macro");
76 }
77 }
78 }
79
Marvin Löbel74190852013-09-02 01:45:3780 fn decl(&mut self, decl: @ast::Decl, pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:3581 match decl.node {
Marvin Löbel74190852013-09-02 01:45:3782 ast::DeclLocal(local) => {
Michael Woerister4bd14242013-07-19 05:38:5583 let init_exit = self.opt_expr(local.init, pred);
84 self.pat(local.pat, init_exit)
Niko Matsakis86b6e6e2013-05-10 17:10:3585 }
86
Marvin Löbel74190852013-09-02 01:45:3787 ast::DeclItem(_) => {
Niko Matsakis86b6e6e2013-05-10 17:10:3588 pred
89 }
90 }
91 }
92
Marvin Löbel74190852013-09-02 01:45:3793 fn pat(&mut self, pat: @ast::Pat, pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:3594 match pat.node {
Marvin Löbel74190852013-09-02 01:45:3795 ast::PatIdent(_, _, None) |
96 ast::PatEnum(_, None) |
Alex Crichtonab387a62013-11-28 20:22:5397 ast::PatLit(..) |
98 ast::PatRange(..) |
Brian Anderson85f107d2013-11-08 03:25:3999 ast::PatWild | ast::PatWildMulti => {
Niko Matsakis86b6e6e2013-05-10 17:10:35100 self.add_node(pat.id, [pred])
101 }
102
Marvin Löbel74190852013-09-02 01:45:37103 ast::PatUniq(subpat) |
104 ast::PatRegion(subpat) |
105 ast::PatIdent(_, _, Some(subpat)) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35106 let subpat_exit = self.pat(subpat, pred);
107 self.add_node(pat.id, [subpat_exit])
108 }
109
Marvin Löbel74190852013-09-02 01:45:37110 ast::PatEnum(_, Some(ref subpats)) |
111 ast::PatTup(ref subpats) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35112 let pats_exit =
Erick Tryzelaar68f40d22013-08-10 03:09:47113 self.pats_all(subpats.iter().map(|p| *p), pred);
Niko Matsakis86b6e6e2013-05-10 17:10:35114 self.add_node(pat.id, [pats_exit])
115 }
116
Marvin Löbel74190852013-09-02 01:45:37117 ast::PatStruct(_, ref subpats, _) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35118 let pats_exit =
Erick Tryzelaar68f40d22013-08-10 03:09:47119 self.pats_all(subpats.iter().map(|f| f.pat), pred);
Niko Matsakis86b6e6e2013-05-10 17:10:35120 self.add_node(pat.id, [pats_exit])
121 }
122
Marvin Löbel74190852013-09-02 01:45:37123 ast::PatVec(ref pre, ref vec, ref post) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35124 let pre_exit =
Erick Tryzelaar68f40d22013-08-10 03:09:47125 self.pats_all(pre.iter().map(|p| *p), pred);
Niko Matsakis86b6e6e2013-05-10 17:10:35126 let vec_exit =
Erick Tryzelaar68f40d22013-08-10 03:09:47127 self.pats_all(vec.iter().map(|p| *p), pre_exit);
Niko Matsakis86b6e6e2013-05-10 17:10:35128 let post_exit =
Erick Tryzelaar68f40d22013-08-10 03:09:47129 self.pats_all(post.iter().map(|p| *p), vec_exit);
Niko Matsakis86b6e6e2013-05-10 17:10:35130 self.add_node(pat.id, [post_exit])
131 }
132 }
133 }
134
Marvin Löbel74190852013-09-02 01:45:37135 fn pats_all<I: Iterator<@ast::Pat>>(&mut self,
Niko Matsakis86b6e6e2013-05-10 17:10:35136 pats: I,
137 pred: CFGIndex) -> CFGIndex {
138 //! Handles case where all of the patterns must match.
139 let mut pats = pats;
140 pats.fold(pred, |pred, pat| self.pat(pat, pred))
141 }
142
143 fn pats_any(&mut self,
Marvin Löbel74190852013-09-02 01:45:37144 pats: &[@ast::Pat],
Niko Matsakis86b6e6e2013-05-10 17:10:35145 pred: CFGIndex) -> CFGIndex {
146 //! Handles case where just one of the patterns must match.
147
148 if pats.len() == 1 {
149 self.pat(pats[0], pred)
150 } else {
151 let collect = self.add_dummy_node([]);
Daniel Micay100894552013-08-03 16:45:23152 for &pat in pats.iter() {
Niko Matsakis86b6e6e2013-05-10 17:10:35153 let pat_exit = self.pat(pat, pred);
154 self.add_contained_edge(pat_exit, collect);
155 }
156 collect
157 }
158 }
159
Marvin Löbel74190852013-09-02 01:45:37160 fn expr(&mut self, expr: @ast::Expr, pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35161 match expr.node {
Eduard Burtescua9c4b182013-11-30 22:00:39162 ast::ExprBlock(blk) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35163 let blk_exit = self.block(blk, pred);
164 self.add_node(expr.id, [blk_exit])
165 }
166
Eduard Burtescua9c4b182013-11-30 22:00:39167 ast::ExprIf(cond, then, None) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35168 //
169 // [pred]
170 // |
171 // v 1
172 // [cond]
173 // |
174 // / \
175 // / \
176 // v 2 *
177 // [then] |
178 // | |
179 // v 3 v 4
180 // [..expr..]
181 //
182 let cond_exit = self.expr(cond, pred); // 1
183 let then_exit = self.block(then, cond_exit); // 2
184 self.add_node(expr.id, [cond_exit, then_exit]) // 3,4
185 }
186
Eduard Burtescua9c4b182013-11-30 22:00:39187 ast::ExprIf(cond, then, Some(otherwise)) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35188 //
189 // [pred]
190 // |
191 // v 1
192 // [cond]
193 // |
194 // / \
195 // / \
196 // v 2 v 3
197 // [then][otherwise]
198 // | |
199 // v 4 v 5
200 // [..expr..]
201 //
202 let cond_exit = self.expr(cond, pred); // 1
203 let then_exit = self.block(then, cond_exit); // 2
204 let else_exit = self.expr(otherwise, cond_exit); // 3
205 self.add_node(expr.id, [then_exit, else_exit]) // 4, 5
206 }
207
Eduard Burtescua9c4b182013-11-30 22:00:39208 ast::ExprWhile(cond, body) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35209 //
210 // [pred]
211 // |
212 // v 1
213 // [loopback] <--+ 5
214 // | |
215 // v 2 |
216 // +-----[cond] |
217 // | | |
218 // | v 4 |
219 // | [body] -----+
220 // v 3
221 // [expr]
222 //
223 // Note that `break` and `loop` statements
224 // may cause additional edges.
225
Michael Sullivan7dbc5ae2013-07-30 23:47:22226 // Is the condition considered part of the loop?
Niko Matsakis86b6e6e2013-05-10 17:10:35227 let loopback = self.add_dummy_node([pred]); // 1
228 let cond_exit = self.expr(cond, loopback); // 2
229 let expr_exit = self.add_node(expr.id, [cond_exit]); // 3
230 self.loop_scopes.push(LoopScope {
231 loop_id: expr.id,
232 continue_index: loopback,
233 break_index: expr_exit
234 });
235 let body_exit = self.block(body, cond_exit); // 4
236 self.add_contained_edge(body_exit, loopback); // 5
237 expr_exit
238 }
239
Alex Crichtonab387a62013-11-28 20:22:53240 ast::ExprForLoop(..) => fail!("non-desugared expr_for_loop"),
Graydon Hoarec29e9fb2013-07-30 00:25:00241
Eduard Burtescua9c4b182013-11-30 22:00:39242 ast::ExprLoop(body, _) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35243 //
244 // [pred]
245 // |
246 // v 1
247 // [loopback] <---+
248 // | 4 |
249 // v 3 |
250 // [body] ------+
251 //
252 // [expr] 2
253 //
254 // Note that `break` and `loop` statements
255 // may cause additional edges.
256
257 let loopback = self.add_dummy_node([pred]); // 1
258 let expr_exit = self.add_node(expr.id, []); // 2
259 self.loop_scopes.push(LoopScope {
260 loop_id: expr.id,
261 continue_index: loopback,
262 break_index: expr_exit,
263 });
264 let body_exit = self.block(body, loopback); // 3
265 self.add_contained_edge(body_exit, loopback); // 4
266 self.loop_scopes.pop();
267 expr_exit
268 }
269
Marvin Löbel74190852013-09-02 01:45:37270 ast::ExprMatch(discr, ref arms) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35271 //
272 // [pred]
273 // |
274 // v 1
275 // [discr]
276 // |
277 // v 2
278 // [guard1]
279 // / \
280 // | \
281 // v 3 |
282 // [pat1] |
283 // |
284 // v 4 |
285 // [body1] v
286 // | [guard2]
287 // | / \
288 // | [body2] \
289 // | | ...
290 // | | |
291 // v 5 v v
292 // [....expr....]
293 //
294 let discr_exit = self.expr(discr, pred); // 1
295
296 let expr_exit = self.add_node(expr.id, []);
297 let mut guard_exit = discr_exit;
Daniel Micay100894552013-08-03 16:45:23298 for arm in arms.iter() {
Niko Matsakis86b6e6e2013-05-10 17:10:35299 guard_exit = self.opt_expr(arm.guard, guard_exit); // 2
Patrick Waltonc1ed4d72014-02-28 23:25:15300 let pats_exit = self.pats_any(arm.pats.as_slice(),
301 guard_exit); // 3
Huon Wilsonc3b90472014-03-03 07:41:47302 let body_exit = self.expr(arm.body, pats_exit); // 4
Niko Matsakis86b6e6e2013-05-10 17:10:35303 self.add_contained_edge(body_exit, expr_exit); // 5
304 }
305 expr_exit
306 }
307
Eduard Burtescu05e4d942014-02-26 14:06:45308 ast::ExprBinary(op, l, r) if ast_util::lazy_binop(op) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35309 //
310 // [pred]
311 // |
312 // v 1
313 // [l]
314 // |
315 // / \
316 // / \
317 // v 2 *
318 // [r] |
319 // | |
320 // v 3 v 4
321 // [..exit..]
322 //
323 let l_exit = self.expr(l, pred); // 1
324 let r_exit = self.expr(r, l_exit); // 2
325 self.add_node(expr.id, [l_exit, r_exit]) // 3,4
326 }
327
Marvin Löbel74190852013-09-02 01:45:37328 ast::ExprRet(v) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35329 let v_exit = self.opt_expr(v, pred);
Felix S. Klock II43c07242014-03-08 20:36:22330 let loop_scope = *self.loop_scopes.get(0);
Niko Matsakis86b6e6e2013-05-10 17:10:35331 self.add_exiting_edge(expr, v_exit,
332 loop_scope, loop_scope.break_index);
333 self.add_node(expr.id, [])
334 }
335
Marvin Löbel74190852013-09-02 01:45:37336 ast::ExprBreak(label) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35337 let loop_scope = self.find_scope(expr, label);
338 self.add_exiting_edge(expr, pred,
339 loop_scope, loop_scope.break_index);
340 self.add_node(expr.id, [])
341 }
342
Marvin Löbel74190852013-09-02 01:45:37343 ast::ExprAgain(label) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35344 let loop_scope = self.find_scope(expr, label);
345 self.add_exiting_edge(expr, pred,
346 loop_scope, loop_scope.continue_index);
347 self.add_node(expr.id, [])
348 }
349
Eduard Burtescu7c48e532014-04-04 10:12:18350 ast::ExprVec(ref elems) => {
Patrick Waltonc1ed4d72014-02-28 23:25:15351 self.straightline(expr, pred, elems.as_slice())
Niko Matsakis86b6e6e2013-05-10 17:10:35352 }
353
Eduard Burtescu6e840232014-02-14 08:28:32354 ast::ExprCall(func, ref args) => {
Patrick Waltonc1ed4d72014-02-28 23:25:15355 self.call(expr, pred, func, args.as_slice())
Niko Matsakis86b6e6e2013-05-10 17:10:35356 }
357
Eduard Burtescu05e4d942014-02-26 14:06:45358 ast::ExprMethodCall(_, _, ref args) => {
Patrick Waltonc1ed4d72014-02-28 23:25:15359 self.call(expr, pred, *args.get(0), args.slice_from(1))
Niko Matsakis86b6e6e2013-05-10 17:10:35360 }
361
Eduard Burtescu05e4d942014-02-26 14:06:45362 ast::ExprIndex(l, r) |
363 ast::ExprBinary(_, l, r) if self.is_method_call(expr) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35364 self.call(expr, pred, l, [r])
365 }
366
Eduard Burtescu05e4d942014-02-26 14:06:45367 ast::ExprUnary(_, e) if self.is_method_call(expr) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35368 self.call(expr, pred, e, [])
369 }
370
Marvin Löbel74190852013-09-02 01:45:37371 ast::ExprTup(ref exprs) => {
Patrick Waltonc1ed4d72014-02-28 23:25:15372 self.straightline(expr, pred, exprs.as_slice())
Niko Matsakis86b6e6e2013-05-10 17:10:35373 }
374
Marvin Löbel74190852013-09-02 01:45:37375 ast::ExprStruct(_, ref fields, base) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35376 let base_exit = self.opt_expr(base, pred);
Patrick Walton3b6e9d42014-03-04 18:02:49377 let field_exprs: Vec<@ast::Expr> =
Erick Tryzelaar68f40d22013-08-10 03:09:47378 fields.iter().map(|f| f.expr).collect();
Felix S. Klock II43c07242014-03-08 20:36:22379 self.straightline(expr, base_exit, field_exprs.as_slice())
Niko Matsakis86b6e6e2013-05-10 17:10:35380 }
381
Eduard Burtescu7c48e532014-04-04 10:12:18382 ast::ExprRepeat(elem, count) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35383 self.straightline(expr, pred, [elem, count])
384 }
385
Marvin Löbel74190852013-09-02 01:45:37386 ast::ExprAssign(l, r) |
Eduard Burtescu05e4d942014-02-26 14:06:45387 ast::ExprAssignOp(_, l, r) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35388 self.straightline(expr, pred, [r, l])
389 }
390
Eduard Burtescu05e4d942014-02-26 14:06:45391 ast::ExprIndex(l, r) |
392 ast::ExprBinary(_, l, r) => { // NB: && and || handled earlier
Niko Matsakis86b6e6e2013-05-10 17:10:35393 self.straightline(expr, pred, [l, r])
394 }
395
Patrick Waltone1271152013-12-18 00:46:18396 ast::ExprBox(p, e) => {
397 self.straightline(expr, pred, [p, e])
398 }
399
Marvin Löbel74190852013-09-02 01:45:37400 ast::ExprAddrOf(_, e) |
Marvin Löbel74190852013-09-02 01:45:37401 ast::ExprCast(e, _) |
Eduard Burtescu05e4d942014-02-26 14:06:45402 ast::ExprUnary(_, e) |
Marvin Löbel74190852013-09-02 01:45:37403 ast::ExprParen(e) |
404 ast::ExprVstore(e, _) |
405 ast::ExprField(e, _, _) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35406 self.straightline(expr, pred, [e])
407 }
408
Alex Crichtonab387a62013-11-28 20:22:53409 ast::ExprMac(..) |
410 ast::ExprInlineAsm(..) |
Alex Crichtonab387a62013-11-28 20:22:53411 ast::ExprFnBlock(..) |
412 ast::ExprProc(..) |
413 ast::ExprLit(..) |
414 ast::ExprPath(..) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35415 self.straightline(expr, pred, [])
416 }
417 }
418 }
419
420 fn call(&mut self,
Marvin Löbel74190852013-09-02 01:45:37421 call_expr: @ast::Expr,
Niko Matsakis86b6e6e2013-05-10 17:10:35422 pred: CFGIndex,
Marvin Löbel74190852013-09-02 01:45:37423 func_or_rcvr: @ast::Expr,
424 args: &[@ast::Expr]) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35425 let func_or_rcvr_exit = self.expr(func_or_rcvr, pred);
426 self.straightline(call_expr, func_or_rcvr_exit, args)
427 }
428
429 fn exprs(&mut self,
Marvin Löbel74190852013-09-02 01:45:37430 exprs: &[@ast::Expr],
Niko Matsakis86b6e6e2013-05-10 17:10:35431 pred: CFGIndex) -> CFGIndex {
432 //! Constructs graph for `exprs` evaluated in order
433
434 exprs.iter().fold(pred, |p, &e| self.expr(e, p))
435 }
436
437 fn opt_expr(&mut self,
Marvin Löbel74190852013-09-02 01:45:37438 opt_expr: Option<@ast::Expr>,
Niko Matsakis86b6e6e2013-05-10 17:10:35439 pred: CFGIndex) -> CFGIndex {
440 //! Constructs graph for `opt_expr` evaluated, if Some
441
442 opt_expr.iter().fold(pred, |p, &e| self.expr(e, p))
443 }
444
445 fn straightline(&mut self,
Marvin Löbel74190852013-09-02 01:45:37446 expr: @ast::Expr,
Niko Matsakis86b6e6e2013-05-10 17:10:35447 pred: CFGIndex,
Marvin Löbel74190852013-09-02 01:45:37448 subexprs: &[@ast::Expr]) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35449 //! Handles case of an expression that evaluates `subexprs` in order
450
451 let subexprs_exit = self.exprs(subexprs, pred);
452 self.add_node(expr.id, [subexprs_exit])
453 }
454
455 fn add_dummy_node(&mut self, preds: &[CFGIndex]) -> CFGIndex {
456 self.add_node(0, preds)
457 }
458
Michael Woerister8a329772013-07-27 08:25:59459 fn add_node(&mut self, id: ast::NodeId, preds: &[CFGIndex]) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35460 assert!(!self.exit_map.contains_key(&id));
461 let node = self.graph.add_node(CFGNodeData {id: id});
462 self.exit_map.insert(id, node);
Daniel Micay100894552013-08-03 16:45:23463 for &pred in preds.iter() {
Niko Matsakis86b6e6e2013-05-10 17:10:35464 self.add_contained_edge(pred, node);
465 }
466 node
467 }
468
469 fn add_contained_edge(&mut self,
470 source: CFGIndex,
471 target: CFGIndex) {
Huon Wilson7785fe12014-03-19 12:16:56472 let data = CFGEdgeData {exiting_scopes: vec!() };
Niko Matsakis86b6e6e2013-05-10 17:10:35473 self.graph.add_edge(source, target, data);
474 }
475
476 fn add_exiting_edge(&mut self,
Marvin Löbel74190852013-09-02 01:45:37477 from_expr: @ast::Expr,
Niko Matsakis86b6e6e2013-05-10 17:10:35478 from_index: CFGIndex,
479 to_loop: LoopScope,
480 to_index: CFGIndex) {
Huon Wilson7785fe12014-03-19 12:16:56481 let mut data = CFGEdgeData {exiting_scopes: vec!() };
Niko Matsakis86b6e6e2013-05-10 17:10:35482 let mut scope_id = from_expr.id;
483 while scope_id != to_loop.loop_id {
Huon Wilson7785fe12014-03-19 12:16:56484
Niko Matsakis86b6e6e2013-05-10 17:10:35485 data.exiting_scopes.push(scope_id);
486 scope_id = self.tcx.region_maps.encl_scope(scope_id);
487 }
488 self.graph.add_edge(from_index, to_index, data);
489 }
490
491 fn find_scope(&self,
Marvin Löbel74190852013-09-02 01:45:37492 expr: @ast::Expr,
Edward Wang386db052014-02-15 08:54:32493 label: Option<ast::Ident>) -> LoopScope {
Niko Matsakis86b6e6e2013-05-10 17:10:35494 match label {
495 None => {
Simon Sapinaa66b912013-12-23 14:08:23496 return *self.loop_scopes.last().unwrap();
Niko Matsakis86b6e6e2013-05-10 17:10:35497 }
498
499 Some(_) => {
Alex Crichton0dbb9092014-03-21 02:49:20500 match self.tcx.def_map.borrow().find(&expr.id) {
Marvin Löbel74190852013-09-02 01:45:37501 Some(&ast::DefLabel(loop_id)) => {
Daniel Micay100894552013-08-03 16:45:23502 for l in self.loop_scopes.iter() {
Niko Matsakis86b6e6e2013-05-10 17:10:35503 if l.loop_id == loop_id {
504 return *l;
505 }
506 }
507 self.tcx.sess.span_bug(
508 expr.span,
mr.Shuee3fa682014-02-06 09:38:08509 format!("no loop scope for id {:?}", loop_id));
Niko Matsakis86b6e6e2013-05-10 17:10:35510 }
511
512 r => {
513 self.tcx.sess.span_bug(
514 expr.span,
mr.Shuee3fa682014-02-06 09:38:08515 format!("bad entry `{:?}` in def_map for label", r));
Niko Matsakis86b6e6e2013-05-10 17:10:35516 }
517 }
518 }
519 }
520 }
521
Marvin Löbel74190852013-09-02 01:45:37522 fn is_method_call(&self, expr: &ast::Expr) -> bool {
Eduard Burtescu20b4e152014-03-06 17:24:11523 let method_call = typeck::MethodCall::expr(expr.id);
Alex Crichton0dbb9092014-03-21 02:49:20524 self.method_map.borrow().contains_key(&method_call)
Niko Matsakis86b6e6e2013-05-10 17:10:35525 }
Patrick Walton99d44d22013-07-10 02:32:09526}