]> git.proxmox.com Git - rustc.git/blob - src/librustc_const_math/is.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / librustc_const_math / is.rs
1 // Copyright 2015 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 syntax::ast;
12 use super::err::*;
13
14 /// Depending on the target only one variant is ever used in a compilation.
15 /// Anything else is an error. This invariant is checked at several locations
16 #[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable, Hash, Eq, PartialEq)]
17 pub enum ConstIsize {
18 Is32(i32),
19 Is64(i64),
20 }
21 pub use self::ConstIsize::*;
22
23 impl ConstIsize {
24 pub fn as_i64(self, target_int_ty: ast::IntTy) -> i64 {
25 match (self, target_int_ty) {
26 (Is32(i), ast::IntTy::I32) => i as i64,
27 (Is64(i), ast::IntTy::I64) => i,
28 _ => panic!("got invalid isize size for target"),
29 }
30 }
31 pub fn new(i: i64, target_int_ty: ast::IntTy) -> Result<Self, ConstMathErr> {
32 match target_int_ty {
33 ast::IntTy::I32 if i as i32 as i64 == i => Ok(Is32(i as i32)),
34 ast::IntTy::I32 => Err(LitOutOfRange(ast::IntTy::Is)),
35 ast::IntTy::I64 => Ok(Is64(i)),
36 _ => unreachable!(),
37 }
38 }
39 }