]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/tvec.rs
Fix checking for missing stability annotations
[rust.git] / src / librustc_trans / tvec.rs
1 // Copyright 2012-2014 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 builder::Builder;
13 use llvm::{BasicBlockRef, ValueRef};
14 use common::*;
15 use rustc::ty::Ty;
16
17 pub fn slice_for_each<'a, 'tcx, F>(
18     bcx: &Builder<'a, 'tcx>,
19     data_ptr: ValueRef,
20     unit_ty: Ty<'tcx>,
21     len: ValueRef,
22     f: F
23 ) -> Builder<'a, 'tcx> where F: FnOnce(&Builder<'a, 'tcx>, ValueRef, BasicBlockRef) {
24     // Special-case vectors with elements of size 0  so they don't go out of bounds (#9890)
25     let zst = type_is_zero_size(bcx.ccx, unit_ty);
26     let add = |bcx: &Builder, a, b| if zst {
27         bcx.add(a, b)
28     } else {
29         bcx.inbounds_gep(a, &[b])
30     };
31
32     let body_bcx = bcx.build_sibling_block("slice_loop_body");
33     let next_bcx = bcx.build_sibling_block("slice_loop_next");
34     let header_bcx = bcx.build_sibling_block("slice_loop_header");
35
36     let start = if zst {
37         C_uint(bcx.ccx, 0usize)
38     } else {
39         data_ptr
40     };
41     let end = add(&bcx, start, len);
42
43     bcx.br(header_bcx.llbb());
44     let current = header_bcx.phi(val_ty(start), &[start], &[bcx.llbb()]);
45
46     let keep_going = header_bcx.icmp(llvm::IntNE, current, end);
47     header_bcx.cond_br(keep_going, body_bcx.llbb(), next_bcx.llbb());
48
49     let next = add(&body_bcx, current, C_uint(bcx.ccx, 1usize));
50     f(&body_bcx, if zst { data_ptr } else { current }, header_bcx.llbb());
51     header_bcx.add_incoming_to_phi(current, next, body_bcx.llbb());
52     next_bcx
53 }