From 4c43631829d8bac8b7533c994d8cf1241a95ce70 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 26 Nov 2019 14:35:23 +0300 Subject: Introduce hir_ty --- crates/ra_hir_ty/src/primitive.rs | 190 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 crates/ra_hir_ty/src/primitive.rs (limited to 'crates/ra_hir_ty/src/primitive.rs') diff --git a/crates/ra_hir_ty/src/primitive.rs b/crates/ra_hir_ty/src/primitive.rs new file mode 100644 index 000000000..afa22448d --- /dev/null +++ b/crates/ra_hir_ty/src/primitive.rs @@ -0,0 +1,190 @@ +//! FIXME: write short doc here + +use std::fmt; + +pub use hir_def::builtin_type::{BuiltinFloat, BuiltinInt, FloatBitness, IntBitness, Signedness}; + +#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)] +pub enum Uncertain { + Unknown, + Known(T), +} + +impl From for Uncertain { + fn from(ty: IntTy) -> Self { + Uncertain::Known(ty) + } +} + +impl fmt::Display for Uncertain { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + Uncertain::Unknown => write!(f, "{{integer}}"), + Uncertain::Known(ty) => write!(f, "{}", ty), + } + } +} + +impl From for Uncertain { + fn from(ty: FloatTy) -> Self { + Uncertain::Known(ty) + } +} + +impl fmt::Display for Uncertain { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + Uncertain::Unknown => write!(f, "{{float}}"), + Uncertain::Known(ty) => write!(f, "{}", ty), + } + } +} + +#[derive(Copy, Clone, Eq, PartialEq, Hash)] +pub struct IntTy { + pub signedness: Signedness, + pub bitness: IntBitness, +} + +impl fmt::Debug for IntTy { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Display::fmt(self, f) + } +} + +impl fmt::Display for IntTy { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.ty_to_string()) + } +} + +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", + } + } +} + +#[derive(Copy, Clone, PartialEq, Eq, Hash)] +pub struct FloatTy { + pub bitness: FloatBitness, +} + +impl fmt::Debug for FloatTy { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Display::fmt(self, f) + } +} + +impl fmt::Display for FloatTy { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.ty_to_string()) + } +} + +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", + } + } +} + +impl From for IntTy { + fn from(t: BuiltinInt) -> Self { + IntTy { signedness: t.signedness, bitness: t.bitness } + } +} + +impl From for FloatTy { + fn from(t: BuiltinFloat) -> Self { + FloatTy { bitness: t.bitness } + } +} + +impl From> for Uncertain { + fn from(t: Option) -> Self { + match t { + None => Uncertain::Unknown, + Some(t) => Uncertain::Known(t.into()), + } + } +} + +impl From> for Uncertain { + fn from(t: Option) -> Self { + match t { + None => Uncertain::Unknown, + Some(t) => Uncertain::Known(t.into()), + } + } +} -- cgit v1.2.3