]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/cast.rs
Auto merge of #58077 - Nemo157:generator-state-debug-info, r=Zoxc
[rust.git] / src / librustc / ty / cast.rs
1 // Helpers for handling cast expressions, used in both
2 // typeck and codegen.
3
4 use crate::ty::{self, Ty};
5
6 use syntax::ast;
7
8 /// Types that are represented as ints.
9 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
10 pub enum IntTy {
11     U(ast::UintTy),
12     I,
13     CEnum,
14     Bool,
15     Char
16 }
17
18 // Valid types for the result of a non-coercion cast
19 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
20 pub enum CastTy<'tcx> {
21     /// Various types that are represented as ints and handled mostly
22     /// in the same way, merged for easier matching.
23     Int(IntTy),
24     /// Floating-Point types
25     Float,
26     /// Function Pointers
27     FnPtr,
28     /// Raw pointers
29     Ptr(ty::TypeAndMut<'tcx>),
30     /// References
31     RPtr(ty::TypeAndMut<'tcx>),
32 }
33
34 /// Cast Kind. See RFC 401 (or librustc_typeck/check/cast.rs)
35 #[derive(Copy, Clone, Debug, RustcEncodable, RustcDecodable)]
36 pub enum CastKind {
37     CoercionCast,
38     PtrPtrCast,
39     PtrAddrCast,
40     AddrPtrCast,
41     NumericCast,
42     EnumCast,
43     PrimIntCast,
44     U8CharCast,
45     ArrayPtrCast,
46     FnPtrPtrCast,
47     FnPtrAddrCast
48 }
49
50 impl<'tcx> CastTy<'tcx> {
51     /// Returns `Some` for integral/pointer casts.
52     /// casts like unsizing casts will return `None`
53     pub fn from_ty(t: Ty<'tcx>) -> Option<CastTy<'tcx>> {
54         match t.sty {
55             ty::Bool => Some(CastTy::Int(IntTy::Bool)),
56             ty::Char => Some(CastTy::Int(IntTy::Char)),
57             ty::Int(_) => Some(CastTy::Int(IntTy::I)),
58             ty::Infer(ty::InferTy::IntVar(_)) => Some(CastTy::Int(IntTy::I)),
59             ty::Infer(ty::InferTy::FloatVar(_)) => Some(CastTy::Float),
60             ty::Uint(u) => Some(CastTy::Int(IntTy::U(u))),
61             ty::Float(_) => Some(CastTy::Float),
62             ty::Adt(d,_) if d.is_enum() && d.is_payloadfree() =>
63                 Some(CastTy::Int(IntTy::CEnum)),
64             ty::RawPtr(mt) => Some(CastTy::Ptr(mt)),
65             ty::Ref(_, ty, mutbl) => Some(CastTy::RPtr(ty::TypeAndMut { ty, mutbl })),
66             ty::FnPtr(..) => Some(CastTy::FnPtr),
67             _ => None,
68         }
69     }
70 }