]> git.proxmox.com Git - rustc.git/blame - src/librustc/middle/ty/cast.rs
Imported Upstream version 1.8.0+dfsg1
[rustc.git] / src / librustc / middle / ty / cast.rs
CommitLineData
62682a34
SL
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// Helpers for handling cast expressions, used in both
12// typeck and trans.
13
14use middle::ty::{self, Ty};
15
b039eaaf 16use syntax::ast;
62682a34
SL
17
18/// Types that are represented as ints.
19#[derive(Copy, Clone, Debug, PartialEq, Eq)]
20pub enum IntTy {
21 U(ast::UintTy),
22 I,
23 CEnum,
24 Bool,
25 Char
26}
27
28// Valid types for the result of a non-coercion cast
29#[derive(Copy, Clone, Debug, PartialEq, Eq)]
30pub enum CastTy<'tcx> {
31 /// Various types that are represented as ints and handled mostly
32 /// in the same way, merged for easier matching.
33 Int(IntTy),
34 /// Floating-Point types
35 Float,
36 /// Function Pointers
37 FnPtr,
38 /// Raw pointers
c1a9b12d 39 Ptr(&'tcx ty::TypeAndMut<'tcx>),
62682a34 40 /// References
c1a9b12d 41 RPtr(&'tcx ty::TypeAndMut<'tcx>),
62682a34
SL
42}
43
44/// Cast Kind. See RFC 401 (or librustc_typeck/check/cast.rs)
45#[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
46pub enum CastKind {
47 CoercionCast,
48 PtrPtrCast,
49 PtrAddrCast,
50 AddrPtrCast,
51 NumericCast,
52 EnumCast,
53 PrimIntCast,
54 U8CharCast,
55 ArrayPtrCast,
56 FnPtrPtrCast,
57 FnPtrAddrCast
58}
59
60impl<'tcx> CastTy<'tcx> {
e9174d1e 61 pub fn from_ty(t: Ty<'tcx>) -> Option<CastTy<'tcx>> {
62682a34
SL
62 match t.sty {
63 ty::TyBool => Some(CastTy::Int(IntTy::Bool)),
64 ty::TyChar => Some(CastTy::Int(IntTy::Char)),
65 ty::TyInt(_) => Some(CastTy::Int(IntTy::I)),
66 ty::TyUint(u) => Some(CastTy::Int(IntTy::U(u))),
67 ty::TyFloat(_) => Some(CastTy::Float),
e9174d1e 68 ty::TyEnum(d,_) if d.is_payloadfree() =>
c1a9b12d 69 Some(CastTy::Int(IntTy::CEnum)),
62682a34
SL
70 ty::TyRawPtr(ref mt) => Some(CastTy::Ptr(mt)),
71 ty::TyRef(_, ref mt) => Some(CastTy::RPtr(mt)),
72 ty::TyBareFn(..) => Some(CastTy::FnPtr),
73 _ => None,
74 }
75 }
76}