1 // Copyright 2013 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
11 use lib::llvm::{llvm, BasicBlockRef};
12 use middle::trans::value::{Users, Value};
13 use std::iter::{Filter, Map};
14
15 pub struct BasicBlock(pub BasicBlockRef);
16
17 pub type Preds<'a> = Map<'a, Value, BasicBlock, Filter<'a, Value, Users>>;
18
19 /**
20 * Wrapper for LLVM BasicBlockRef
21 */
22 impl BasicBlock {
23 pub fn get(&self) -> BasicBlockRef {
24 let BasicBlock(v) = *self; v
25 }
26
27 pub fn as_value(self) -> Value {
28 unsafe {
29 Value(llvm::LLVMBasicBlockAsValue(self.get()))
30 }
31 }
32
33 pub fn pred_iter(self) -> Preds {
34 self.as_value().user_iter()
35 .filter(|user| user.is_a_terminator_inst())
36 .map(|user| user.get_parent().unwrap())
37 }
38
39 pub fn get_single_predecessor(self) -> Option<BasicBlock> {
40 let mut iter = self.pred_iter();
41 match (iter.next(), iter.next()) {
42 (Some(first), None) => Some(first),
43 _ => None
44 }
45 }
46 }