]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/trans/basic_block.rs
rollup merge of #19577: aidancully/master
[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 pub struct BasicBlock(pub BasicBlockRef);
17
18 impl Copy for BasicBlock {}
19
20 pub type Preds<'a> = Map<'a, Value, BasicBlock, Filter<'a, Value, Users>>;
21
22 /// Wrapper for LLVM BasicBlockRef
23 impl BasicBlock {
24     pub fn get(&self) -> BasicBlockRef {
25         let BasicBlock(v) = *self; v
26     }
27
28     pub fn as_value(self) -> Value {
29         unsafe {
30             Value(llvm::LLVMBasicBlockAsValue(self.get()))
31         }
32     }
33
34     pub fn pred_iter(self) -> Preds<'static> {
35         self.as_value().user_iter()
36             .filter(|user| user.is_a_terminator_inst())
37             .map(|user| user.get_parent().unwrap())
38     }
39
40     pub fn get_single_predecessor(self) -> Option<BasicBlock> {
41         let mut iter = self.pred_iter();
42         match (iter.next(), iter.next()) {
43             (Some(first), None) => Some(first),
44             _ => None
45         }
46     }
47 }