]> git.proxmox.com Git - rustc.git/blame - src/librustc_trans/trans/basic_block.rs
Imported Upstream version 1.8.0+dfsg1
[rustc.git] / src / librustc_trans / trans / basic_block.rs
CommitLineData
1a4d82fc
JJ
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
11use llvm;
c34b1796 12use llvm::BasicBlockRef;
1a4d82fc
JJ
13use trans::value::{Users, Value};
14use std::iter::{Filter, Map};
15
c34b1796 16#[derive(Copy, Clone)]
1a4d82fc
JJ
17pub struct BasicBlock(pub BasicBlockRef);
18
85aaf69f 19pub type Preds = Map<Filter<Users, fn(&Value) -> bool>, fn(Value) -> BasicBlock>;
1a4d82fc
JJ
20
21/// Wrapper for LLVM BasicBlockRef
22impl 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 fn is_a_terminator_inst(user: &Value) -> bool { user.is_a_terminator_inst() }
35 let is_a_terminator_inst: fn(&Value) -> bool = is_a_terminator_inst;
36
37 fn get_parent(user: Value) -> BasicBlock { user.get_parent().unwrap() }
38 let get_parent: fn(Value) -> BasicBlock = get_parent;
39
40 self.as_value().user_iter()
41 .filter(is_a_terminator_inst)
42 .map(get_parent)
43 }
44
45 pub fn get_single_predecessor(self) -> Option<BasicBlock> {
46 let mut iter = self.pred_iter();
47 match (iter.next(), iter.next()) {
48 (Some(first), None) => Some(first),
49 _ => None
50 }
51 }
52}