Introduce TypeCtor::Scalar

This commit is contained in:
Lukas Wirth 2021-02-28 01:20:04 +01:00
parent 2a4076c14d
commit 5183c9f083
13 changed files with 353 additions and 383 deletions

View file

@ -5,18 +5,28 @@
use std::fmt;
pub use hir_def::builtin_type::{BuiltinFloat, BuiltinInt, FloatBitness, IntBitness, Signedness};
pub use hir_def::builtin_type::{BuiltinFloat, BuiltinInt, BuiltinUint};
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub struct IntTy {
pub signedness: Signedness,
pub bitness: IntBitness,
/// Different signed int types.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum IntTy {
Isize,
I8,
I16,
I32,
I64,
I128,
}
impl fmt::Debug for IntTy {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, f)
}
/// Different unsigned int types.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum UintTy {
Usize,
U8,
U16,
U32,
U64,
U128,
}
impl fmt::Display for IntTy {
@ -26,75 +36,41 @@ impl fmt::Display for IntTy {
}
impl IntTy {
pub fn isize() -> IntTy {
IntTy { signedness: Signedness::Signed, bitness: IntBitness::Xsize }
}
pub fn i8() -> IntTy {
IntTy { signedness: Signedness::Signed, bitness: IntBitness::X8 }
}
pub fn i16() -> IntTy {
IntTy { signedness: Signedness::Signed, bitness: IntBitness::X16 }
}
pub fn i32() -> IntTy {
IntTy { signedness: Signedness::Signed, bitness: IntBitness::X32 }
}
pub fn i64() -> IntTy {
IntTy { signedness: Signedness::Signed, bitness: IntBitness::X64 }
}
pub fn i128() -> IntTy {
IntTy { signedness: Signedness::Signed, bitness: IntBitness::X128 }
}
pub fn usize() -> IntTy {
IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::Xsize }
}
pub fn u8() -> IntTy {
IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X8 }
}
pub fn u16() -> IntTy {
IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X16 }
}
pub fn u32() -> IntTy {
IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X32 }
}
pub fn u64() -> IntTy {
IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X64 }
}
pub fn u128() -> IntTy {
IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X128 }
}
pub fn ty_to_string(self) -> &'static str {
match (self.signedness, self.bitness) {
(Signedness::Signed, IntBitness::Xsize) => "isize",
(Signedness::Signed, IntBitness::X8) => "i8",
(Signedness::Signed, IntBitness::X16) => "i16",
(Signedness::Signed, IntBitness::X32) => "i32",
(Signedness::Signed, IntBitness::X64) => "i64",
(Signedness::Signed, IntBitness::X128) => "i128",
(Signedness::Unsigned, IntBitness::Xsize) => "usize",
(Signedness::Unsigned, IntBitness::X8) => "u8",
(Signedness::Unsigned, IntBitness::X16) => "u16",
(Signedness::Unsigned, IntBitness::X32) => "u32",
(Signedness::Unsigned, IntBitness::X64) => "u64",
(Signedness::Unsigned, IntBitness::X128) => "u128",
match self {
IntTy::Isize => "isize",
IntTy::I8 => "i8",
IntTy::I16 => "i16",
IntTy::I32 => "i32",
IntTy::I64 => "i64",
IntTy::I128 => "i128",
}
}
}
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
pub struct FloatTy {
pub bitness: FloatBitness,
impl fmt::Display for UintTy {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.ty_to_string())
}
}
impl UintTy {
pub fn ty_to_string(self) -> &'static str {
match self {
UintTy::Usize => "usize",
UintTy::U8 => "u8",
UintTy::U16 => "u16",
UintTy::U32 => "u32",
UintTy::U64 => "u64",
UintTy::U128 => "u128",
}
}
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum FloatTy {
F32,
F64,
}
impl fmt::Debug for FloatTy {
@ -110,30 +86,45 @@ impl fmt::Display for FloatTy {
}
impl FloatTy {
pub fn f32() -> FloatTy {
FloatTy { bitness: FloatBitness::X32 }
}
pub fn f64() -> FloatTy {
FloatTy { bitness: FloatBitness::X64 }
}
pub fn ty_to_string(self) -> &'static str {
match self.bitness {
FloatBitness::X32 => "f32",
FloatBitness::X64 => "f64",
match self {
FloatTy::F32 => "f32",
FloatTy::F64 => "f64",
}
}
}
impl From<BuiltinInt> for IntTy {
fn from(t: BuiltinInt) -> Self {
IntTy { signedness: t.signedness, bitness: t.bitness }
match t {
BuiltinInt::Isize => Self::Isize,
BuiltinInt::I8 => Self::I8,
BuiltinInt::I16 => Self::I16,
BuiltinInt::I32 => Self::I32,
BuiltinInt::I64 => Self::I64,
BuiltinInt::I128 => Self::I128,
}
}
}
impl From<BuiltinUint> for UintTy {
fn from(t: BuiltinUint) -> Self {
match t {
BuiltinUint::Usize => Self::Usize,
BuiltinUint::U8 => Self::U8,
BuiltinUint::U16 => Self::U16,
BuiltinUint::U32 => Self::U32,
BuiltinUint::U64 => Self::U64,
BuiltinUint::U128 => Self::U128,
}
}
}
impl From<BuiltinFloat> for FloatTy {
fn from(t: BuiltinFloat) -> Self {
FloatTy { bitness: t.bitness }
match t {
BuiltinFloat::F32 => Self::F32,
BuiltinFloat::F64 => Self::F64,
}
}
}