Wrap BuiltinType in code model

This commit is contained in:
Jonas Schievink 2021-02-11 19:52:33 +01:00
parent b0f20a795d
commit 216dc856c5
9 changed files with 56 additions and 28 deletions

View File

@ -6,7 +6,6 @@
use either::Either;
use hir_def::{
adt::{ReprKind, StructKind, VariantData},
builtin_type::BuiltinType,
expr::{BindingAnnotation, LabelId, Pat, PatId},
import_map,
item_tree::ItemTreeNode,
@ -245,7 +244,7 @@ pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
ModuleDef::Const(it) => it.name(db),
ModuleDef::Static(it) => it.name(db),
ModuleDef::BuiltinType(it) => Some(it.as_name()),
ModuleDef::BuiltinType(it) => Some(it.name()),
}
}
@ -994,6 +993,23 @@ fn visibility(&self, db: &dyn HirDatabase) -> Visibility {
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct BuiltinType {
pub(crate) inner: hir_def::builtin_type::BuiltinType,
}
impl BuiltinType {
pub fn ty(self, db: &dyn HirDatabase, module: Module) -> Type {
let resolver = module.id.resolver(db.upcast());
Type::new_with_resolver(db, &resolver, Ty::builtin(self.inner))
.expect("crate not present in resolver")
}
pub fn name(self) -> Name {
self.inner.as_name()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MacroDef {
pub(crate) id: MacroDefId,

View File

@ -11,8 +11,9 @@
};
use crate::{
code_model::GenericParam, Adt, AssocItem, DefWithBody, Field, GenericDef, Label, Local,
MacroDef, ModuleDef, Variant, VariantDef,
code_model::{BuiltinType, GenericParam},
Adt, AssocItem, DefWithBody, Field, GenericDef, Label, Local, MacroDef, ModuleDef, Variant,
VariantDef,
};
macro_rules! from_id {
@ -111,7 +112,7 @@ fn from(id: ModuleDefId) -> Self {
ModuleDefId::StaticId(it) => ModuleDef::Static(it.into()),
ModuleDefId::TraitId(it) => ModuleDef::Trait(it.into()),
ModuleDefId::TypeAliasId(it) => ModuleDef::TypeAlias(it.into()),
ModuleDefId::BuiltinType(it) => ModuleDef::BuiltinType(it),
ModuleDefId::BuiltinType(it) => ModuleDef::BuiltinType(it.into()),
}
}
}
@ -127,7 +128,7 @@ fn from(id: ModuleDef) -> Self {
ModuleDef::Static(it) => ModuleDefId::StaticId(it.into()),
ModuleDef::Trait(it) => ModuleDefId::TraitId(it.into()),
ModuleDef::TypeAlias(it) => ModuleDefId::TypeAliasId(it.into()),
ModuleDef::BuiltinType(it) => ModuleDefId::BuiltinType(it),
ModuleDef::BuiltinType(it) => ModuleDefId::BuiltinType(it.into()),
}
}
}
@ -274,3 +275,15 @@ fn from(module_def: ModuleDef) -> Self {
}
}
}
impl From<hir_def::builtin_type::BuiltinType> for BuiltinType {
fn from(inner: hir_def::builtin_type::BuiltinType) -> Self {
Self { inner }
}
}
impl From<BuiltinType> for hir_def::builtin_type::BuiltinType {
fn from(it: BuiltinType) -> Self {
it.inner
}
}

View File

@ -49,7 +49,7 @@ fn in_type_ns(&self) -> Option<TypeNs> {
match self {
PathResolution::Def(ModuleDef::Adt(adt)) => Some(TypeNs::AdtId((*adt).into())),
PathResolution::Def(ModuleDef::BuiltinType(builtin)) => {
Some(TypeNs::BuiltinType(*builtin))
Some(TypeNs::BuiltinType((*builtin).into()))
}
PathResolution::Def(ModuleDef::Const(_))
| PathResolution::Def(ModuleDef::Variant(_))

View File

@ -28,8 +28,9 @@
};
use crate::{
db::HirDatabase, semantics::PathResolution, Adt, Const, Field, Function, Local, MacroDef,
ModuleDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Variant,
code_model::BuiltinType, db::HirDatabase, semantics::PathResolution, Adt, Const, Field,
Function, Local, MacroDef, ModuleDef, Static, Struct, Trait, Type, TypeAlias, TypeParam,
Variant,
};
use base_db::CrateId;
@ -479,7 +480,7 @@ fn resolve_hir_path_(
}
TypeNs::EnumVariantId(it) => PathResolution::Def(Variant::from(it).into()),
TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()),
TypeNs::BuiltinType(it) => PathResolution::Def(it.into()),
TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()),
TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()),
})
};
@ -555,7 +556,7 @@ fn resolve_hir_path_qualifier(
TypeNs::AdtSelfType(it) | TypeNs::AdtId(it) => PathResolution::Def(Adt::from(it).into()),
TypeNs::EnumVariantId(it) => PathResolution::Def(Variant::from(it).into()),
TypeNs::TypeAliasId(it) => PathResolution::Def(TypeAlias::from(it).into()),
TypeNs::BuiltinType(it) => PathResolution::Def(it.into()),
TypeNs::BuiltinType(it) => PathResolution::Def(BuiltinType::from(it).into()),
TypeNs::TraitId(it) => PathResolution::Def(Trait::from(it).into()),
})
}

View File

@ -361,6 +361,7 @@ pub fn module(&self) -> Option<ModuleId> {
}
pub fn krate(&self) -> Option<CrateId> {
// FIXME: can this ever be `None`?
self.module_scope().map(|t| t.0.krate())
}

View File

@ -27,6 +27,7 @@ macro_rules! eprintln {
use base_db::{salsa, CrateId};
use hir_def::{
builtin_type::BuiltinType,
expr::ExprId,
type_ref::{Mutability, Rawness},
AdtId, AssocContainerId, DefWithBodyId, FunctionId, GenericDefId, HasModule, LifetimeParamId,
@ -738,6 +739,15 @@ pub fn fn_ptr(sig: FnSig) -> Self {
Substs(sig.params_and_return),
)
}
pub fn builtin(builtin: BuiltinType) -> Self {
Ty::simple(match builtin {
BuiltinType::Char => TypeCtor::Char,
BuiltinType::Bool => TypeCtor::Bool,
BuiltinType::Str => TypeCtor::Str,
BuiltinType::Int(t) => TypeCtor::Int(IntTy::from(t).into()),
BuiltinType::Float(t) => TypeCtor::Float(FloatTy::from(t).into()),
})
}
pub fn as_reference(&self) -> Option<(&Ty, Mutability)> {
match self {

View File

@ -27,7 +27,6 @@
use crate::{
db::HirDatabase,
primitive::{FloatTy, IntTy},
utils::{
all_super_trait_refs, associated_type_by_name_including_super_traits, generics,
make_mut_slice, variant_data,
@ -1051,17 +1050,6 @@ fn type_for_static(db: &dyn HirDatabase, def: StaticId) -> Binders<Ty> {
Binders::new(0, Ty::from_hir(&ctx, &data.type_ref))
}
/// Build the declared type of a static.
fn type_for_builtin(def: BuiltinType) -> Ty {
Ty::simple(match def {
BuiltinType::Char => TypeCtor::Char,
BuiltinType::Bool => TypeCtor::Bool,
BuiltinType::Str => TypeCtor::Str,
BuiltinType::Int(t) => TypeCtor::Int(IntTy::from(t).into()),
BuiltinType::Float(t) => TypeCtor::Float(FloatTy::from(t).into()),
})
}
fn fn_sig_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> PolyFnSig {
let struct_data = db.struct_data(def);
let fields = struct_data.variant_data.fields();
@ -1186,7 +1174,7 @@ pub enum ValueTyDefId {
/// namespace.
pub(crate) fn ty_query(db: &dyn HirDatabase, def: TyDefId) -> Binders<Ty> {
match def {
TyDefId::BuiltinType(it) => Binders::new(0, type_for_builtin(it)),
TyDefId::BuiltinType(it) => Binders::new(0, Ty::builtin(it)),
TyDefId::AdtId(it) => type_for_adt(db, it),
TyDefId::TypeAliasId(it) => type_for_type_alias(db, it),
}

View File

@ -9,8 +9,7 @@
use hir::{
db::{DefDatabase, HirDatabase},
Adt, AsAssocItem, AsName, AssocItem, AssocItemContainer, Crate, Field, HasAttrs, ItemInNs,
ModuleDef,
Adt, AsAssocItem, AssocItem, AssocItemContainer, Crate, Field, HasAttrs, ItemInNs, ModuleDef,
};
use ide_db::{
defs::{Definition, NameClass, NameRefClass},
@ -429,7 +428,7 @@ fn get_symbol_filename(db: &dyn HirDatabase, definition: &ModuleDef) -> Option<S
ModuleDef::Module(_) => "index.html".to_string(),
ModuleDef::Trait(t) => format!("trait.{}.html", t.name(db)),
ModuleDef::TypeAlias(t) => format!("type.{}.html", t.name(db)),
ModuleDef::BuiltinType(t) => format!("primitive.{}.html", t.as_name()),
ModuleDef::BuiltinType(t) => format!("primitive.{}.html", t.name()),
ModuleDef::Function(f) => format!("fn.{}.html", f.name(db)),
ModuleDef::Variant(ev) => {
format!("enum.{}.html#variant.{}", ev.parent_enum(db).name(db), ev.name(db))

View File

@ -334,7 +334,7 @@ fn hover_for_definition(db: &RootDatabase, def: Definition) -> Option<Markup> {
ModuleDef::Static(it) => from_def_source(db, it, mod_path),
ModuleDef::Trait(it) => from_def_source(db, it, mod_path),
ModuleDef::TypeAlias(it) => from_def_source(db, it, mod_path),
ModuleDef::BuiltinType(it) => Some(Markup::fenced_block(&it)),
ModuleDef::BuiltinType(it) => Some(Markup::fenced_block(&it.name())),
},
Definition::Local(it) => Some(Markup::fenced_block(&it.ty(db).display(db))),
Definition::SelfType(impl_def) => {