Introduce hir_ty

This commit is contained in:
Aleksey Kladov 2019-11-26 14:35:23 +03:00
parent a443b5033c
commit 4c43631829
7 changed files with 252 additions and 190 deletions

23
Cargo.lock generated
View File

@ -1024,6 +1024,7 @@ dependencies = [
"ra_db 0.1.0",
"ra_hir_def 0.1.0",
"ra_hir_expand 0.1.0",
"ra_hir_ty 0.1.0",
"ra_mbe 0.1.0",
"ra_prof 0.1.0",
"ra_syntax 0.1.0",
@ -1065,6 +1066,28 @@ dependencies = [
"ra_tt 0.1.0",
]
[[package]]
name = "ra_hir_ty"
version = "0.1.0"
dependencies = [
"chalk-ir 0.1.0 (git+https://github.com/jackh726/chalk.git?rev=095cd38a4f16337913bba487f2055b9ca0179f30)",
"chalk-rust-ir 0.1.0 (git+https://github.com/jackh726/chalk.git?rev=095cd38a4f16337913bba487f2055b9ca0179f30)",
"chalk-solve 0.1.0 (git+https://github.com/jackh726/chalk.git?rev=095cd38a4f16337913bba487f2055b9ca0179f30)",
"ena 0.13.1 (registry+https://github.com/rust-lang/crates.io-index)",
"insta 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lalrpop-intern 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ra_arena 0.1.0",
"ra_db 0.1.0",
"ra_hir_def 0.1.0",
"ra_hir_expand 0.1.0",
"ra_prof 0.1.0",
"ra_syntax 0.1.0",
"rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"test_utils 0.1.0",
]
[[package]]
name = "ra_ide_api"
version = "0.1.0"

View File

@ -23,6 +23,7 @@ mbe = { path = "../ra_mbe", package = "ra_mbe" }
tt = { path = "../ra_tt", package = "ra_tt" }
hir_expand = { path = "../ra_hir_expand", package = "ra_hir_expand" }
hir_def = { path = "../ra_hir_def", package = "ra_hir_def" }
hir_ty = { path = "../ra_hir_ty", package = "ra_hir_ty" }
test_utils = { path = "../test_utils" }
ra_prof = { path = "../ra_prof" }

View File

@ -9,7 +9,7 @@ use std::iter;
use std::sync::Arc;
use hir_def::{
builtin_type::{BuiltinFloat, BuiltinInt, BuiltinType},
builtin_type::BuiltinType,
generics::WherePredicate,
path::{GenericArg, PathSegment},
resolver::{HasResolver, Resolver, TypeNs},
@ -27,7 +27,7 @@ use super::{
use crate::{
db::HirDatabase,
ty::{
primitive::{FloatTy, IntTy, Uncertain},
primitive::{FloatTy, IntTy},
Adt,
},
util::make_mut_slice,
@ -679,36 +679,6 @@ fn type_for_builtin(def: BuiltinType) -> Ty {
})
}
impl From<BuiltinInt> for IntTy {
fn from(t: BuiltinInt) -> Self {
IntTy { signedness: t.signedness, bitness: t.bitness }
}
}
impl From<BuiltinFloat> for FloatTy {
fn from(t: BuiltinFloat) -> Self {
FloatTy { bitness: t.bitness }
}
}
impl From<Option<BuiltinInt>> for Uncertain<IntTy> {
fn from(t: Option<BuiltinInt>) -> Self {
match t {
None => Uncertain::Unknown,
Some(t) => Uncertain::Known(t.into()),
}
}
}
impl From<Option<BuiltinFloat>> for Uncertain<FloatTy> {
fn from(t: Option<BuiltinFloat>) -> Self {
match t {
None => Uncertain::Unknown,
Some(t) => Uncertain::Known(t.into()),
}
}
}
fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: StructId) -> FnSig {
let struct_data = db.struct_data(def.into());
let fields = struct_data.variant_data.fields();

View File

@ -1,160 +1,3 @@
//! FIXME: write short doc here
use std::fmt;
pub use hir_def::builtin_type::{FloatBitness, IntBitness, Signedness};
#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)]
pub enum Uncertain<T> {
Unknown,
Known(T),
}
impl From<IntTy> for Uncertain<IntTy> {
fn from(ty: IntTy) -> Self {
Uncertain::Known(ty)
}
}
impl fmt::Display for Uncertain<IntTy> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Uncertain::Unknown => write!(f, "{{integer}}"),
Uncertain::Known(ty) => write!(f, "{}", ty),
}
}
}
impl From<FloatTy> for Uncertain<FloatTy> {
fn from(ty: FloatTy) -> Self {
Uncertain::Known(ty)
}
}
impl fmt::Display for Uncertain<FloatTy> {
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(crate) 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(crate) fn ty_to_string(self) -> &'static str {
match self.bitness {
FloatBitness::X32 => "f32",
FloatBitness::X64 => "f64",
}
}
}
pub use hir_ty::primitive::{FloatBitness, IntBitness, Signedness, FloatTy, IntTy, Uncertain};

View File

@ -0,0 +1,32 @@
[package]
edition = "2018"
name = "ra_hir_ty"
version = "0.1.0"
authors = ["rust-analyzer developers"]
[lib]
doctest = false
[dependencies]
log = "0.4.5"
rustc-hash = "1.0"
parking_lot = "0.9.0"
ena = "0.13"
ra_syntax = { path = "../ra_syntax" }
ra_arena = { path = "../ra_arena" }
ra_db = { path = "../ra_db" }
hir_expand = { path = "../ra_hir_expand", package = "ra_hir_expand" }
hir_def = { path = "../ra_hir_def", package = "ra_hir_def" }
test_utils = { path = "../test_utils" }
ra_prof = { path = "../ra_prof" }
# https://github.com/rust-lang/chalk/pull/294
chalk-solve = { git = "https://github.com/jackh726/chalk.git", rev = "095cd38a4f16337913bba487f2055b9ca0179f30" }
chalk-rust-ir = { git = "https://github.com/jackh726/chalk.git", rev = "095cd38a4f16337913bba487f2055b9ca0179f30" }
chalk-ir = { git = "https://github.com/jackh726/chalk.git", rev = "095cd38a4f16337913bba487f2055b9ca0179f30" }
lalrpop-intern = "0.15.1"
[dev-dependencies]
insta = "0.12.0"

View File

@ -0,0 +1,3 @@
//! FIXME: write short doc here
pub mod primitive;

View File

@ -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<T> {
Unknown,
Known(T),
}
impl From<IntTy> for Uncertain<IntTy> {
fn from(ty: IntTy) -> Self {
Uncertain::Known(ty)
}
}
impl fmt::Display for Uncertain<IntTy> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Uncertain::Unknown => write!(f, "{{integer}}"),
Uncertain::Known(ty) => write!(f, "{}", ty),
}
}
}
impl From<FloatTy> for Uncertain<FloatTy> {
fn from(ty: FloatTy) -> Self {
Uncertain::Known(ty)
}
}
impl fmt::Display for Uncertain<FloatTy> {
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<BuiltinInt> for IntTy {
fn from(t: BuiltinInt) -> Self {
IntTy { signedness: t.signedness, bitness: t.bitness }
}
}
impl From<BuiltinFloat> for FloatTy {
fn from(t: BuiltinFloat) -> Self {
FloatTy { bitness: t.bitness }
}
}
impl From<Option<BuiltinInt>> for Uncertain<IntTy> {
fn from(t: Option<BuiltinInt>) -> Self {
match t {
None => Uncertain::Unknown,
Some(t) => Uncertain::Known(t.into()),
}
}
}
impl From<Option<BuiltinFloat>> for Uncertain<FloatTy> {
fn from(t: Option<BuiltinFloat>) -> Self {
match t {
None => Uncertain::Unknown,
Some(t) => Uncertain::Known(t.into()),
}
}
}