]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/trans/basic_block.rs
Merge pull request #20510 from tshepang/patch-6
[rust.git] / src / librustc_trans / trans / basic_block.rs
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 llvm;
12 use llvm::{BasicBlockRef};
13 use trans::value::{Users, Value};
14 use std::iter::{Filter, Map};
15
16 #[derive(Copy)]
17 pub struct BasicBlock(pub BasicBlockRef);
18
19 pub type Preds = Map<
20     Value,
21     BasicBlock,
22     Filter<Value, Users, fn(&Value) -> bool>,
23     fn(Value) -> BasicBlock,
24 >;
25
26 /// Wrapper for LLVM BasicBlockRef
27 impl BasicBlock {
28     pub fn get(&self) -> BasicBlockRef {
29         let BasicBlock(v) = *self; v
30     }
31
32     pub fn as_value(self) -> Value {
33         unsafe {
34             Value(llvm::LLVMBasicBlockAsValue(self.get()))
35         }
36     }
37
38     pub fn pred_iter(self) -> Preds {
39         fn is_a_terminator_inst(user: &Value) -> bool { user.is_a_terminator_inst() }
40         let is_a_terminator_inst: fn(&Value) -> bool = is_a_terminator_inst;
41
42         fn get_parent(user: Value) -> BasicBlock { user.get_parent().unwrap() }
43         let get_parent: fn(Value) -> BasicBlock = get_parent;
44
45         self.as_value().user_iter()
46             .filter(is_a_terminator_inst)
47             .map(get_parent)
48     }
49
50     pub fn get_single_predecessor(self) -> Option<BasicBlock> {
51         let mut iter = self.pred_iter();
52         match (iter.next(), iter.next()) {
53             (Some(first), None) => Some(first),
54             _ => None
55         }
56     }
57 }