From b20d37cb49536e19f6af57b00258f86eb8f19325 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 31 Oct 2019 10:51:54 +0300 Subject: [PATCH] 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 diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index a6ce23dd10e..e0c6d634029 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -6,13 +6,11 @@ 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 @@ 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 @@ pub(crate) fn fn_data_query( 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 603b0c3dc3f..40f5562b406 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -63,9 +63,9 @@ fn from(it: $sv) -> $e { 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 @@ fn from(it: $sv) -> $e { }; 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 7c4d07de0da..7ba03182718 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -54,7 +54,7 @@ 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 @@ 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 f77c9df9f02..75b24d386f1 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 @@ 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 0f49a0e541d..dd7cd979ff4 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs @@ -9,6 +9,7 @@ use std::sync::Arc; use hir_def::{ + builtin_type::BuiltinType, path::{GenericArg, PathSegment}, type_ref::{TypeBound, TypeRef}, }; @@ -24,10 +25,13 @@ 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 8966f9d1d50..1749752f10e 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 00000000000..12929caa900 --- /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 7a6c7b30125..8cbff673cf7 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -11,6 +11,7 @@ pub mod attr; pub mod path; pub mod type_ref; +pub mod builtin_type; // FIXME: this should be private pub mod nameres;