From b20d37cb49536e19f6af57b00258f86eb8f19325 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 31 Oct 2019 10:51:54 +0300 Subject: move builtin types to hir_def --- crates/ra_hir/src/code_model.rs | 48 +++----------------------- crates/ra_hir/src/lib.rs | 7 ++-- crates/ra_hir/src/nameres.rs | 5 ++- crates/ra_hir/src/resolve.rs | 4 +-- crates/ra_hir/src/ty/lower.rs | 16 ++++++--- crates/ra_hir/src/ty/primitive.rs | 22 +----------- crates/ra_hir_def/src/builtin_type.rs | 63 +++++++++++++++++++++++++++++++++++ crates/ra_hir_def/src/lib.rs | 1 + 8 files changed, 88 insertions(+), 78 deletions(-) create mode 100644 crates/ra_hir_def/src/builtin_type.rs (limited to 'crates') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index a6ce23dd1..e0c6d6340 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -6,13 +6,11 @@ pub(crate) mod docs; use std::sync::Arc; use hir_def::{ + builtin_type::BuiltinType, type_ref::{Mutability, TypeRef}, CrateModuleId, ModuleId, }; -use hir_expand::name::{ - self, AsName, BOOL, CHAR, F32, F64, I128, I16, I32, I64, I8, ISIZE, SELF_TYPE, STR, U128, U16, - U32, U64, U8, USIZE, -}; +use hir_expand::name::{self, AsName}; use ra_db::{CrateId, Edition}; use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; @@ -30,10 +28,7 @@ use crate::{ nameres::{ImportId, ModuleScope, Namespace}, resolve::{Resolver, Scope, TypeNs}, traits::TraitData, - ty::{ - primitive::{FloatBitness, FloatTy, IntBitness, IntTy, Signedness}, - InferenceResult, TraitRef, - }, + ty::{InferenceResult, TraitRef}, Either, HasSource, Name, Ty, }; @@ -87,41 +82,6 @@ pub struct Module { pub(crate) id: ModuleId, } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub enum BuiltinType { - Char, - Bool, - Str, - Int(IntTy), - Float(FloatTy), -} - -impl BuiltinType { - #[rustfmt::skip] - pub(crate) const ALL: &'static [(Name, BuiltinType)] = &[ - (CHAR, BuiltinType::Char), - (BOOL, BuiltinType::Bool), - (STR, BuiltinType::Str), - - (ISIZE, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::Xsize })), - (I8, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X8 })), - (I16, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X16 })), - (I32, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X32 })), - (I64, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X64 })), - (I128, BuiltinType::Int(IntTy { signedness: Signedness::Signed, bitness: IntBitness::X128 })), - - (USIZE, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::Xsize })), - (U8, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X8 })), - (U16, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X16 })), - (U32, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X32 })), - (U64, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X64 })), - (U128, BuiltinType::Int(IntTy { signedness: Signedness::Unsigned, bitness: IntBitness::X128 })), - - (F32, BuiltinType::Float(FloatTy { bitness: FloatBitness::X32 })), - (F64, BuiltinType::Float(FloatTy { bitness: FloatBitness::X64 })), - ]; -} - /// The defs which can be visible in the module. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum ModuleDef { @@ -625,7 +585,7 @@ impl FnData { let self_type = if let Some(type_ref) = self_param.ascribed_type() { TypeRef::from_ast(type_ref) } else { - let self_type = TypeRef::Path(SELF_TYPE.into()); + let self_type = TypeRef::Path(name::SELF_TYPE.into()); match self_param.kind() { ast::SelfParamKind::Owned => self_type, ast::SelfParamKind::Ref => { diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 603b0c3dc..40f5562b4 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -63,9 +63,9 @@ pub use crate::{ code_model::{ docs::{DocDef, Docs, Documentation}, src::{HasBodySource, HasSource, Source}, - Adt, AssocItem, BuiltinType, Const, ConstData, Container, Crate, CrateDependency, - DefWithBody, Enum, EnumVariant, FieldSource, FnData, Function, HasBody, MacroDef, Module, - ModuleDef, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, + Adt, AssocItem, Const, ConstData, Container, Crate, CrateDependency, DefWithBody, Enum, + EnumVariant, FieldSource, FnData, Function, HasBody, MacroDef, Module, ModuleDef, + ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, }, expr::ExprScopes, from_source::FromSource, @@ -81,6 +81,7 @@ pub use crate::{ }; pub use hir_def::{ + builtin_type::BuiltinType, path::{Path, PathKind}, type_ref::Mutability, }; diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index 7c4d07de0..7ba031827 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -54,7 +54,7 @@ mod tests; use std::sync::Arc; -use hir_def::CrateModuleId; +use hir_def::{builtin_type::BuiltinType, CrateModuleId}; use once_cell::sync::Lazy; use ra_arena::Arena; use ra_db::{Edition, FileId}; @@ -68,8 +68,7 @@ use crate::{ diagnostics::DiagnosticSink, ids::MacroDefId, nameres::diagnostics::DefDiagnostic, - Adt, AstId, BuiltinType, Crate, HirFileId, MacroDef, Module, ModuleDef, Name, Path, PathKind, - Trait, + Adt, AstId, Crate, HirFileId, MacroDef, Module, ModuleDef, Name, Path, PathKind, Trait, }; pub use self::per_ns::{Namespace, PerNs}; diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index f77c9df9f..75b24d386 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs @@ -2,6 +2,7 @@ use std::sync::Arc; use hir_def::{ + builtin_type::BuiltinType, path::{Path, PathKind}, CrateModuleId, }; @@ -18,8 +19,7 @@ use crate::{ generics::GenericParams, impl_block::ImplBlock, nameres::{CrateDefMap, PerNs}, - Adt, BuiltinType, Const, Enum, EnumVariant, Function, MacroDef, ModuleDef, Static, Struct, - Trait, TypeAlias, + Adt, Const, Enum, EnumVariant, Function, MacroDef, ModuleDef, Static, Struct, Trait, TypeAlias, }; #[derive(Debug, Clone, Default)] diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index 0f49a0e54..dd7cd979f 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -9,6 +9,7 @@ use std::iter; use std::sync::Arc; use hir_def::{ + builtin_type::BuiltinType, path::{GenericArg, PathSegment}, type_ref::{TypeBound, TypeRef}, }; @@ -24,10 +25,13 @@ use crate::{ generics::{GenericDef, WherePredicate}, nameres::Namespace, resolve::{Resolver, TypeNs}, - ty::Adt, + ty::{ + primitive::{FloatTy, IntTy}, + Adt, + }, util::make_mut_slice, - BuiltinType, Const, Enum, EnumVariant, Function, ModuleDef, Path, Static, Struct, StructField, - Trait, TypeAlias, Union, + Const, Enum, EnumVariant, Function, ModuleDef, Path, Static, Struct, StructField, Trait, + TypeAlias, Union, }; impl Ty { @@ -643,8 +647,10 @@ fn type_for_builtin(def: BuiltinType) -> Ty { BuiltinType::Char => TypeCtor::Char, BuiltinType::Bool => TypeCtor::Bool, BuiltinType::Str => TypeCtor::Str, - BuiltinType::Int(ty) => TypeCtor::Int(ty.into()), - BuiltinType::Float(ty) => TypeCtor::Float(ty.into()), + BuiltinType::Int { signedness, bitness } => { + TypeCtor::Int(IntTy { signedness, bitness }.into()) + } + BuiltinType::Float { bitness } => TypeCtor::Float(FloatTy { bitness }.into()), }) } diff --git a/crates/ra_hir/src/ty/primitive.rs b/crates/ra_hir/src/ty/primitive.rs index 8966f9d1d..1749752f1 100644 --- a/crates/ra_hir/src/ty/primitive.rs +++ b/crates/ra_hir/src/ty/primitive.rs @@ -2,27 +2,7 @@ use std::fmt; -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] -pub enum Signedness { - Signed, - Unsigned, -} - -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] -pub enum IntBitness { - Xsize, - X8, - X16, - X32, - X64, - X128, -} - -#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] -pub enum FloatBitness { - X32, - X64, -} +pub use hir_def::builtin_type::{FloatBitness, IntBitness, Signedness}; #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] pub enum UncertainIntTy { diff --git a/crates/ra_hir_def/src/builtin_type.rs b/crates/ra_hir_def/src/builtin_type.rs new file mode 100644 index 000000000..12929caa9 --- /dev/null +++ b/crates/ra_hir_def/src/builtin_type.rs @@ -0,0 +1,63 @@ +//! This module defines built-in types. +//! +//! A peculiarity of built-in types is that they are always available and are +//! not associated with any particular crate. + +use hir_expand::name::{self, Name}; + +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +pub enum Signedness { + Signed, + Unsigned, +} + +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +pub enum IntBitness { + Xsize, + X8, + X16, + X32, + X64, + X128, +} + +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +pub enum FloatBitness { + X32, + X64, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum BuiltinType { + Char, + Bool, + Str, + Int { signedness: Signedness, bitness: IntBitness }, + Float { bitness: FloatBitness }, +} + +impl BuiltinType { + #[rustfmt::skip] + pub const ALL: &'static [(Name, BuiltinType)] = &[ + (name::CHAR, BuiltinType::Char), + (name::BOOL, BuiltinType::Bool), + (name::STR, BuiltinType::Str ), + + (name::ISIZE, BuiltinType::Int { signedness: Signedness::Signed, bitness: IntBitness::Xsize }), + (name::I8, BuiltinType::Int { signedness: Signedness::Signed, bitness: IntBitness::X8 }), + (name::I16, BuiltinType::Int { signedness: Signedness::Signed, bitness: IntBitness::X16 }), + (name::I32, BuiltinType::Int { signedness: Signedness::Signed, bitness: IntBitness::X32 }), + (name::I64, BuiltinType::Int { signedness: Signedness::Signed, bitness: IntBitness::X64 }), + (name::I128, BuiltinType::Int { signedness: Signedness::Signed, bitness: IntBitness::X128 }), + + (name::USIZE, BuiltinType::Int { signedness: Signedness::Unsigned, bitness: IntBitness::Xsize }), + (name::U8, BuiltinType::Int { signedness: Signedness::Unsigned, bitness: IntBitness::X8 }), + (name::U16, BuiltinType::Int { signedness: Signedness::Unsigned, bitness: IntBitness::X16 }), + (name::U32, BuiltinType::Int { signedness: Signedness::Unsigned, bitness: IntBitness::X32 }), + (name::U64, BuiltinType::Int { signedness: Signedness::Unsigned, bitness: IntBitness::X64 }), + (name::U128, BuiltinType::Int { signedness: Signedness::Unsigned, bitness: IntBitness::X128 }), + + (name::F32, BuiltinType::Float { bitness: FloatBitness::X32 }), + (name::F64, BuiltinType::Float { bitness: FloatBitness::X64 }), + ]; +} diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 7a6c7b301..8cbff673c 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -11,6 +11,7 @@ pub mod db; pub mod attr; pub mod path; pub mod type_ref; +pub mod builtin_type; // FIXME: this should be private pub mod nameres; -- cgit v1.2.3