blob: 6eed827415d9562eefe65c370f99a1b5ea81a063 [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;
Alex Crichton2a14e082014-02-20 03:29:5815use collections::HashMap;
Niko Matsakis86b6e6e2013-05-10 17:10:3516use syntax::ast;
17use syntax::ast_util;
18use syntax::opt_vec;
19
20struct CFGBuilder {
21 tcx: ty::ctxt,
Eduard Burtescu7a588ce2014-02-26 14:01:3622 method_map: typeck::MethodMap,
Michael Woerister8a329772013-07-27 08:25:5923 exit_map: HashMap<ast::NodeId, CFGIndex>,
Niko Matsakis86b6e6e2013-05-10 17:10:3524 graph: CFGGraph,
25 loop_scopes: ~[LoopScope],
26}
27
28struct LoopScope {
Michael Woerister8a329772013-07-27 08:25:5929 loop_id: ast::NodeId, // id of loop/while node
Niko Matsakis86b6e6e2013-05-10 17:10:3530 continue_index: CFGIndex, // where to go on a `loop`
31 break_index: CFGIndex, // where to go on a `break
32}
33
34pub fn construct(tcx: ty::ctxt,
Eduard Burtescu7a588ce2014-02-26 14:01:3635 method_map: typeck::MethodMap,
Michael Woerister4bd14242013-07-19 05:38:5536 blk: &ast::Block) -> CFG {
Niko Matsakis86b6e6e2013-05-10 17:10:3537 let mut cfg_builder = CFGBuilder {
38 exit_map: HashMap::new(),
39 graph: graph::Graph::new(),
40 tcx: tcx,
41 method_map: method_map,
42 loop_scopes: ~[]
43 };
44 let entry = cfg_builder.add_node(0, []);
45 let exit = cfg_builder.block(blk, entry);
Alex Crichtonab387a62013-11-28 20:22:5346 let CFGBuilder {exit_map, graph, ..} = cfg_builder;
Niko Matsakis86b6e6e2013-05-10 17:10:3547 CFG {exit_map: exit_map,
48 graph: graph,
49 entry: entry,
50 exit: exit}
51}
52
53impl CFGBuilder {
Michael Woerister4bd14242013-07-19 05:38:5554 fn block(&mut self, blk: &ast::Block, pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:3555 let mut stmts_exit = pred;
Daniel Micay100894552013-08-03 16:45:2356 for &stmt in blk.stmts.iter() {
Niko Matsakis86b6e6e2013-05-10 17:10:3557 stmts_exit = self.stmt(stmt, stmts_exit);
58 }
59
Michael Woerister0cc70742013-07-16 18:08:3560 let expr_exit = self.opt_expr(blk.expr, stmts_exit);
Niko Matsakis86b6e6e2013-05-10 17:10:3561
Michael Woerister0cc70742013-07-16 18:08:3562 self.add_node(blk.id, [expr_exit])
Niko Matsakis86b6e6e2013-05-10 17:10:3563 }
64
Marvin Löbel74190852013-09-02 01:45:3765 fn stmt(&mut self, stmt: @ast::Stmt, pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:3566 match stmt.node {
Marvin Löbel74190852013-09-02 01:45:3767 ast::StmtDecl(decl, _) => {
Niko Matsakis86b6e6e2013-05-10 17:10:3568 self.decl(decl, pred)
69 }
70
Marvin Löbel74190852013-09-02 01:45:3771 ast::StmtExpr(expr, _) | ast::StmtSemi(expr, _) => {
Niko Matsakis86b6e6e2013-05-10 17:10:3572 self.expr(expr, pred)
73 }
74
Alex Crichtonab387a62013-11-28 20:22:5375 ast::StmtMac(..) => {
Niko Matsakis86b6e6e2013-05-10 17:10:3576 self.tcx.sess.span_bug(stmt.span, "unexpanded macro");
77 }
78 }
79 }
80
Marvin Löbel74190852013-09-02 01:45:3781 fn decl(&mut self, decl: @ast::Decl, pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:3582 match decl.node {
Marvin Löbel74190852013-09-02 01:45:3783 ast::DeclLocal(local) => {
Michael Woerister4bd14242013-07-19 05:38:5584 let init_exit = self.opt_expr(local.init, pred);
85 self.pat(local.pat, init_exit)
Niko Matsakis86b6e6e2013-05-10 17:10:3586 }
87
Marvin Löbel74190852013-09-02 01:45:3788 ast::DeclItem(_) => {
Niko Matsakis86b6e6e2013-05-10 17:10:3589 pred
90 }
91 }
92 }
93
Marvin Löbel74190852013-09-02 01:45:3794 fn pat(&mut self, pat: @ast::Pat, pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:3595 match pat.node {
Marvin Löbel74190852013-09-02 01:45:3796 ast::PatIdent(_, _, None) |
97 ast::PatEnum(_, None) |
Alex Crichtonab387a62013-11-28 20:22:5398 ast::PatLit(..) |
99 ast::PatRange(..) |
Brian Anderson85f107d2013-11-08 03:25:39100 ast::PatWild | ast::PatWildMulti => {
Niko Matsakis86b6e6e2013-05-10 17:10:35101 self.add_node(pat.id, [pred])
102 }
103
Marvin Löbel74190852013-09-02 01:45:37104 ast::PatUniq(subpat) |
105 ast::PatRegion(subpat) |
106 ast::PatIdent(_, _, Some(subpat)) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35107 let subpat_exit = self.pat(subpat, pred);
108 self.add_node(pat.id, [subpat_exit])
109 }
110
Marvin Löbel74190852013-09-02 01:45:37111 ast::PatEnum(_, Some(ref subpats)) |
112 ast::PatTup(ref subpats) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35113 let pats_exit =
Erick Tryzelaar68f40d22013-08-10 03:09:47114 self.pats_all(subpats.iter().map(|p| *p), pred);
Niko Matsakis86b6e6e2013-05-10 17:10:35115 self.add_node(pat.id, [pats_exit])
116 }
117
Marvin Löbel74190852013-09-02 01:45:37118 ast::PatStruct(_, ref subpats, _) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35119 let pats_exit =
Erick Tryzelaar68f40d22013-08-10 03:09:47120 self.pats_all(subpats.iter().map(|f| f.pat), pred);
Niko Matsakis86b6e6e2013-05-10 17:10:35121 self.add_node(pat.id, [pats_exit])
122 }
123
Marvin Löbel74190852013-09-02 01:45:37124 ast::PatVec(ref pre, ref vec, ref post) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35125 let pre_exit =
Erick Tryzelaar68f40d22013-08-10 03:09:47126 self.pats_all(pre.iter().map(|p| *p), pred);
Niko Matsakis86b6e6e2013-05-10 17:10:35127 let vec_exit =
Erick Tryzelaar68f40d22013-08-10 03:09:47128 self.pats_all(vec.iter().map(|p| *p), pre_exit);
Niko Matsakis86b6e6e2013-05-10 17:10:35129 let post_exit =
Erick Tryzelaar68f40d22013-08-10 03:09:47130 self.pats_all(post.iter().map(|p| *p), vec_exit);
Niko Matsakis86b6e6e2013-05-10 17:10:35131 self.add_node(pat.id, [post_exit])
132 }
133 }
134 }
135
Marvin Löbel74190852013-09-02 01:45:37136 fn pats_all<I: Iterator<@ast::Pat>>(&mut self,
Niko Matsakis86b6e6e2013-05-10 17:10:35137 pats: I,
138 pred: CFGIndex) -> CFGIndex {
139 //! Handles case where all of the patterns must match.
140 let mut pats = pats;
141 pats.fold(pred, |pred, pat| self.pat(pat, pred))
142 }
143
144 fn pats_any(&mut self,
Marvin Löbel74190852013-09-02 01:45:37145 pats: &[@ast::Pat],
Niko Matsakis86b6e6e2013-05-10 17:10:35146 pred: CFGIndex) -> CFGIndex {
147 //! Handles case where just one of the patterns must match.
148
149 if pats.len() == 1 {
150 self.pat(pats[0], pred)
151 } else {
152 let collect = self.add_dummy_node([]);
Daniel Micay100894552013-08-03 16:45:23153 for &pat in pats.iter() {
Niko Matsakis86b6e6e2013-05-10 17:10:35154 let pat_exit = self.pat(pat, pred);
155 self.add_contained_edge(pat_exit, collect);
156 }
157 collect
158 }
159 }
160
Marvin Löbel74190852013-09-02 01:45:37161 fn expr(&mut self, expr: @ast::Expr, pred: CFGIndex) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35162 match expr.node {
Eduard Burtescua9c4b182013-11-30 22:00:39163 ast::ExprBlock(blk) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35164 let blk_exit = self.block(blk, pred);
165 self.add_node(expr.id, [blk_exit])
166 }
167
Eduard Burtescua9c4b182013-11-30 22:00:39168 ast::ExprIf(cond, then, None) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35169 //
170 // [pred]
171 // |
172 // v 1
173 // [cond]
174 // |
175 // / \
176 // / \
177 // v 2 *
178 // [then] |
179 // | |
180 // v 3 v 4
181 // [..expr..]
182 //
183 let cond_exit = self.expr(cond, pred); // 1
184 let then_exit = self.block(then, cond_exit); // 2
185 self.add_node(expr.id, [cond_exit, then_exit]) // 3,4
186 }
187
Eduard Burtescua9c4b182013-11-30 22:00:39188 ast::ExprIf(cond, then, Some(otherwise)) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35189 //
190 // [pred]
191 // |
192 // v 1
193 // [cond]
194 // |
195 // / \
196 // / \
197 // v 2 v 3
198 // [then][otherwise]
199 // | |
200 // v 4 v 5
201 // [..expr..]
202 //
203 let cond_exit = self.expr(cond, pred); // 1
204 let then_exit = self.block(then, cond_exit); // 2
205 let else_exit = self.expr(otherwise, cond_exit); // 3
206 self.add_node(expr.id, [then_exit, else_exit]) // 4, 5
207 }
208
Eduard Burtescua9c4b182013-11-30 22:00:39209 ast::ExprWhile(cond, body) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35210 //
211 // [pred]
212 // |
213 // v 1
214 // [loopback] <--+ 5
215 // | |
216 // v 2 |
217 // +-----[cond] |
218 // | | |
219 // | v 4 |
220 // | [body] -----+
221 // v 3
222 // [expr]
223 //
224 // Note that `break` and `loop` statements
225 // may cause additional edges.
226
Michael Sullivan7dbc5ae2013-07-30 23:47:22227 // Is the condition considered part of the loop?
Niko Matsakis86b6e6e2013-05-10 17:10:35228 let loopback = self.add_dummy_node([pred]); // 1
229 let cond_exit = self.expr(cond, loopback); // 2
230 let expr_exit = self.add_node(expr.id, [cond_exit]); // 3
231 self.loop_scopes.push(LoopScope {
232 loop_id: expr.id,
233 continue_index: loopback,
234 break_index: expr_exit
235 });
236 let body_exit = self.block(body, cond_exit); // 4
237 self.add_contained_edge(body_exit, loopback); // 5
238 expr_exit
239 }
240
Alex Crichtonab387a62013-11-28 20:22:53241 ast::ExprForLoop(..) => fail!("non-desugared expr_for_loop"),
Graydon Hoarec29e9fb2013-07-30 00:25:00242
Eduard Burtescua9c4b182013-11-30 22:00:39243 ast::ExprLoop(body, _) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35244 //
245 // [pred]
246 // |
247 // v 1
248 // [loopback] <---+
249 // | 4 |
250 // v 3 |
251 // [body] ------+
252 //
253 // [expr] 2
254 //
255 // Note that `break` and `loop` statements
256 // may cause additional edges.
257
258 let loopback = self.add_dummy_node([pred]); // 1
259 let expr_exit = self.add_node(expr.id, []); // 2
260 self.loop_scopes.push(LoopScope {
261 loop_id: expr.id,
262 continue_index: loopback,
263 break_index: expr_exit,
264 });
265 let body_exit = self.block(body, loopback); // 3
266 self.add_contained_edge(body_exit, loopback); // 4
267 self.loop_scopes.pop();
268 expr_exit
269 }
270
Marvin Löbel74190852013-09-02 01:45:37271 ast::ExprMatch(discr, ref arms) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35272 //
273 // [pred]
274 // |
275 // v 1
276 // [discr]
277 // |
278 // v 2
279 // [guard1]
280 // / \
281 // | \
282 // v 3 |
283 // [pat1] |
284 // |
285 // v 4 |
286 // [body1] v
287 // | [guard2]
288 // | / \
289 // | [body2] \
290 // | | ...
291 // | | |
292 // v 5 v v
293 // [....expr....]
294 //
295 let discr_exit = self.expr(discr, pred); // 1
296
297 let expr_exit = self.add_node(expr.id, []);
298 let mut guard_exit = discr_exit;
Daniel Micay100894552013-08-03 16:45:23299 for arm in arms.iter() {
Niko Matsakis86b6e6e2013-05-10 17:10:35300 guard_exit = self.opt_expr(arm.guard, guard_exit); // 2
301 let pats_exit = self.pats_any(arm.pats, guard_exit); // 3
Eduard Burtescua9c4b182013-11-30 22:00:39302 let body_exit = self.block(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
Marvin Löbel74190852013-09-02 01:45:37308 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);
330 let loop_scope = self.loop_scopes[0];
331 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
Marvin Löbel74190852013-09-02 01:45:37350 ast::ExprVec(ref elems, _) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35351 self.straightline(expr, pred, *elems)
352 }
353
Eduard Burtescu6e840232014-02-14 08:28:32354 ast::ExprCall(func, ref args) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35355 self.call(expr, pred, func, *args)
356 }
357
Eduard Burtescu6e840232014-02-14 08:28:32358 ast::ExprMethodCall(_, _, _, ref args) => {
Eduard Burtescu15ba0c32014-01-27 12:18:36359 self.call(expr, pred, args[0], args.slice_from(1))
Niko Matsakis86b6e6e2013-05-10 17:10:35360 }
361
Marvin Löbel74190852013-09-02 01:45:37362 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
Marvin Löbel74190852013-09-02 01:45:37367 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) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35372 self.straightline(expr, pred, *exprs)
373 }
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);
Marvin Löbel74190852013-09-02 01:45:37377 let field_exprs: ~[@ast::Expr] =
Erick Tryzelaar68f40d22013-08-10 03:09:47378 fields.iter().map(|f| f.expr).collect();
Niko Matsakis86b6e6e2013-05-10 17:10:35379 self.straightline(expr, base_exit, field_exprs)
380 }
381
Marvin Löbel74190852013-09-02 01:45:37382 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) |
387 ast::ExprAssignOp(_, _, l, r) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35388 self.straightline(expr, pred, [r, l])
389 }
390
Marvin Löbel74190852013-09-02 01:45:37391 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, _) |
402 ast::ExprUnary(_, _, e) |
403 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 Crichton8a966182013-08-28 06:12:05409 ast::ExprLogLevel |
Alex Crichtonab387a62013-11-28 20:22:53410 ast::ExprMac(..) |
411 ast::ExprInlineAsm(..) |
Alex Crichtonab387a62013-11-28 20:22:53412 ast::ExprFnBlock(..) |
413 ast::ExprProc(..) |
414 ast::ExprLit(..) |
415 ast::ExprPath(..) => {
Niko Matsakis86b6e6e2013-05-10 17:10:35416 self.straightline(expr, pred, [])
417 }
418 }
419 }
420
421 fn call(&mut self,
Marvin Löbel74190852013-09-02 01:45:37422 call_expr: @ast::Expr,
Niko Matsakis86b6e6e2013-05-10 17:10:35423 pred: CFGIndex,
Marvin Löbel74190852013-09-02 01:45:37424 func_or_rcvr: @ast::Expr,
425 args: &[@ast::Expr]) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35426 let func_or_rcvr_exit = self.expr(func_or_rcvr, pred);
427 self.straightline(call_expr, func_or_rcvr_exit, args)
428 }
429
430 fn exprs(&mut self,
Marvin Löbel74190852013-09-02 01:45:37431 exprs: &[@ast::Expr],
Niko Matsakis86b6e6e2013-05-10 17:10:35432 pred: CFGIndex) -> CFGIndex {
433 //! Constructs graph for `exprs` evaluated in order
434
435 exprs.iter().fold(pred, |p, &e| self.expr(e, p))
436 }
437
438 fn opt_expr(&mut self,
Marvin Löbel74190852013-09-02 01:45:37439 opt_expr: Option<@ast::Expr>,
Niko Matsakis86b6e6e2013-05-10 17:10:35440 pred: CFGIndex) -> CFGIndex {
441 //! Constructs graph for `opt_expr` evaluated, if Some
442
443 opt_expr.iter().fold(pred, |p, &e| self.expr(e, p))
444 }
445
446 fn straightline(&mut self,
Marvin Löbel74190852013-09-02 01:45:37447 expr: @ast::Expr,
Niko Matsakis86b6e6e2013-05-10 17:10:35448 pred: CFGIndex,
Marvin Löbel74190852013-09-02 01:45:37449 subexprs: &[@ast::Expr]) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35450 //! Handles case of an expression that evaluates `subexprs` in order
451
452 let subexprs_exit = self.exprs(subexprs, pred);
453 self.add_node(expr.id, [subexprs_exit])
454 }
455
456 fn add_dummy_node(&mut self, preds: &[CFGIndex]) -> CFGIndex {
457 self.add_node(0, preds)
458 }
459
Michael Woerister8a329772013-07-27 08:25:59460 fn add_node(&mut self, id: ast::NodeId, preds: &[CFGIndex]) -> CFGIndex {
Niko Matsakis86b6e6e2013-05-10 17:10:35461 assert!(!self.exit_map.contains_key(&id));
462 let node = self.graph.add_node(CFGNodeData {id: id});
463 self.exit_map.insert(id, node);
Daniel Micay100894552013-08-03 16:45:23464 for &pred in preds.iter() {
Niko Matsakis86b6e6e2013-05-10 17:10:35465 self.add_contained_edge(pred, node);
466 }
467 node
468 }
469
470 fn add_contained_edge(&mut self,
471 source: CFGIndex,
472 target: CFGIndex) {
473 let data = CFGEdgeData {exiting_scopes: opt_vec::Empty};
474 self.graph.add_edge(source, target, data);
475 }
476
477 fn add_exiting_edge(&mut self,
Marvin Löbel74190852013-09-02 01:45:37478 from_expr: @ast::Expr,
Niko Matsakis86b6e6e2013-05-10 17:10:35479 from_index: CFGIndex,
480 to_loop: LoopScope,
481 to_index: CFGIndex) {
482 let mut data = CFGEdgeData {exiting_scopes: opt_vec::Empty};
483 let mut scope_id = from_expr.id;
484 while scope_id != to_loop.loop_id {
485 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(_) => {
Patrick Waltoncc058472013-12-23 19:15:16500 let def_map = self.tcx.def_map.borrow();
501 match def_map.get().find(&expr.id) {
Marvin Löbel74190852013-09-02 01:45:37502 Some(&ast::DefLabel(loop_id)) => {
Daniel Micay100894552013-08-03 16:45:23503 for l in self.loop_scopes.iter() {
Niko Matsakis86b6e6e2013-05-10 17:10:35504 if l.loop_id == loop_id {
505 return *l;
506 }
507 }
508 self.tcx.sess.span_bug(
509 expr.span,
mr.Shuee3fa682014-02-06 09:38:08510 format!("no loop scope for id {:?}", loop_id));
Niko Matsakis86b6e6e2013-05-10 17:10:35511 }
512
513 r => {
514 self.tcx.sess.span_bug(
515 expr.span,
mr.Shuee3fa682014-02-06 09:38:08516 format!("bad entry `{:?}` in def_map for label", r));
Niko Matsakis86b6e6e2013-05-10 17:10:35517 }
518 }
519 }
520 }
521 }
522
Marvin Löbel74190852013-09-02 01:45:37523 fn is_method_call(&self, expr: &ast::Expr) -> bool {
Patrick Waltonf7393d82013-12-22 01:04:42524 let method_map = self.method_map.borrow();
525 method_map.get().contains_key(&expr.id)
Niko Matsakis86b6e6e2013-05-10 17:10:35526 }
Patrick Walton99d44d22013-07-10 02:32:09527}