2019-11-03 21:11:37 +00:00
|
|
|
//! Utility module for converting between hir_def ids and code_model wrappers.
|
|
|
|
//!
|
|
|
|
//! It's unclear if we need this long-term, but it's definitelly useful while we
|
|
|
|
//! are splitting the hir.
|
|
|
|
|
2019-11-21 09:21:46 +00:00
|
|
|
use hir_def::{
|
2020-11-02 15:31:38 +00:00
|
|
|
expr::PatId, item_scope::ItemInNs, AdtId, AssocItemId, DefWithBodyId, EnumVariantId, FieldId,
|
|
|
|
GenericDefId, ModuleDefId, VariantId,
|
2019-11-21 09:21:46 +00:00
|
|
|
};
|
2019-10-31 15:45:10 +00:00
|
|
|
|
2019-11-21 09:21:46 +00:00
|
|
|
use crate::{
|
2020-11-02 15:31:38 +00:00
|
|
|
Adt, AssocItem, DefWithBody, EnumVariant, Field, GenericDef, Local, MacroDef, ModuleDef,
|
|
|
|
VariantDef,
|
2019-11-21 09:21:46 +00:00
|
|
|
};
|
2019-10-31 15:45:10 +00:00
|
|
|
|
|
|
|
macro_rules! from_id {
|
|
|
|
($(($id:path, $ty:path)),*) => {$(
|
|
|
|
impl From<$id> for $ty {
|
|
|
|
fn from(id: $id) -> $ty {
|
|
|
|
$ty { id }
|
|
|
|
}
|
|
|
|
}
|
2019-12-08 11:44:14 +00:00
|
|
|
impl From<$ty> for $id {
|
|
|
|
fn from(ty: $ty) -> $id {
|
|
|
|
ty.id
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 15:45:10 +00:00
|
|
|
)*}
|
|
|
|
}
|
|
|
|
|
|
|
|
from_id![
|
2020-08-13 14:25:38 +00:00
|
|
|
(base_db::CrateId, crate::Crate),
|
2019-10-31 15:45:10 +00:00
|
|
|
(hir_def::ModuleId, crate::Module),
|
|
|
|
(hir_def::StructId, crate::Struct),
|
|
|
|
(hir_def::UnionId, crate::Union),
|
|
|
|
(hir_def::EnumId, crate::Enum),
|
|
|
|
(hir_def::TypeAliasId, crate::TypeAlias),
|
|
|
|
(hir_def::TraitId, crate::Trait),
|
|
|
|
(hir_def::StaticId, crate::Static),
|
|
|
|
(hir_def::ConstId, crate::Const),
|
|
|
|
(hir_def::FunctionId, crate::Function),
|
2020-02-29 20:24:40 +00:00
|
|
|
(hir_def::ImplId, crate::ImplDef),
|
2020-02-26 12:22:46 +00:00
|
|
|
(hir_def::TypeParamId, crate::TypeParam),
|
2020-12-13 21:13:16 +00:00
|
|
|
(hir_def::LifetimeParamId, crate::LifetimeParam),
|
2019-10-31 15:45:10 +00:00
|
|
|
(hir_expand::MacroDefId, crate::MacroDef)
|
|
|
|
];
|
|
|
|
|
|
|
|
impl From<AdtId> for Adt {
|
|
|
|
fn from(id: AdtId) -> Self {
|
|
|
|
match id {
|
|
|
|
AdtId::StructId(it) => Adt::Struct(it.into()),
|
|
|
|
AdtId::UnionId(it) => Adt::Union(it.into()),
|
|
|
|
AdtId::EnumId(it) => Adt::Enum(it.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-20 09:25:02 +00:00
|
|
|
impl From<Adt> for AdtId {
|
|
|
|
fn from(id: Adt) -> Self {
|
|
|
|
match id {
|
|
|
|
Adt::Struct(it) => AdtId::StructId(it.id),
|
|
|
|
Adt::Union(it) => AdtId::UnionId(it.id),
|
|
|
|
Adt::Enum(it) => AdtId::EnumId(it.id),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 15:45:10 +00:00
|
|
|
impl From<EnumVariantId> for EnumVariant {
|
|
|
|
fn from(id: EnumVariantId) -> Self {
|
|
|
|
EnumVariant { parent: id.parent.into(), id: id.local_id }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 18:43:36 +00:00
|
|
|
impl From<EnumVariant> for EnumVariantId {
|
|
|
|
fn from(def: EnumVariant) -> Self {
|
|
|
|
EnumVariantId { parent: def.parent.id, local_id: def.id }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 15:45:10 +00:00
|
|
|
impl From<ModuleDefId> for ModuleDef {
|
|
|
|
fn from(id: ModuleDefId) -> Self {
|
|
|
|
match id {
|
|
|
|
ModuleDefId::ModuleId(it) => ModuleDef::Module(it.into()),
|
|
|
|
ModuleDefId::FunctionId(it) => ModuleDef::Function(it.into()),
|
|
|
|
ModuleDefId::AdtId(it) => ModuleDef::Adt(it.into()),
|
|
|
|
ModuleDefId::EnumVariantId(it) => ModuleDef::EnumVariant(it.into()),
|
|
|
|
ModuleDefId::ConstId(it) => ModuleDef::Const(it.into()),
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-14 14:37:22 +00:00
|
|
|
|
2019-12-31 15:17:08 +00:00
|
|
|
impl From<ModuleDef> for ModuleDefId {
|
|
|
|
fn from(id: ModuleDef) -> Self {
|
|
|
|
match id {
|
|
|
|
ModuleDef::Module(it) => ModuleDefId::ModuleId(it.into()),
|
|
|
|
ModuleDef::Function(it) => ModuleDefId::FunctionId(it.into()),
|
|
|
|
ModuleDef::Adt(it) => ModuleDefId::AdtId(it.into()),
|
|
|
|
ModuleDef::EnumVariant(it) => ModuleDefId::EnumVariantId(it.into()),
|
|
|
|
ModuleDef::Const(it) => ModuleDefId::ConstId(it.into()),
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 14:37:22 +00:00
|
|
|
impl From<DefWithBody> for DefWithBodyId {
|
|
|
|
fn from(def: DefWithBody) -> Self {
|
|
|
|
match def {
|
|
|
|
DefWithBody::Function(it) => DefWithBodyId::FunctionId(it.id),
|
|
|
|
DefWithBody::Static(it) => DefWithBodyId::StaticId(it.id),
|
|
|
|
DefWithBody::Const(it) => DefWithBodyId::ConstId(it.id),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-15 18:28:00 +00:00
|
|
|
|
2019-11-21 09:21:46 +00:00
|
|
|
impl From<DefWithBodyId> for DefWithBody {
|
|
|
|
fn from(def: DefWithBodyId) -> Self {
|
|
|
|
match def {
|
|
|
|
DefWithBodyId::FunctionId(it) => DefWithBody::Function(it.into()),
|
|
|
|
DefWithBodyId::StaticId(it) => DefWithBody::Static(it.into()),
|
|
|
|
DefWithBodyId::ConstId(it) => DefWithBody::Const(it.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 18:28:00 +00:00
|
|
|
impl From<AssocItemId> for AssocItem {
|
|
|
|
fn from(def: AssocItemId) -> Self {
|
|
|
|
match def {
|
|
|
|
AssocItemId::FunctionId(it) => AssocItem::Function(it.into()),
|
|
|
|
AssocItemId::TypeAliasId(it) => AssocItem::TypeAlias(it.into()),
|
|
|
|
AssocItemId::ConstId(it) => AssocItem::Const(it.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-20 09:25:02 +00:00
|
|
|
|
|
|
|
impl From<GenericDef> for GenericDefId {
|
|
|
|
fn from(def: GenericDef) -> Self {
|
|
|
|
match def {
|
|
|
|
GenericDef::Function(it) => GenericDefId::FunctionId(it.id),
|
|
|
|
GenericDef::Adt(it) => GenericDefId::AdtId(it.into()),
|
|
|
|
GenericDef::Trait(it) => GenericDefId::TraitId(it.id),
|
|
|
|
GenericDef::TypeAlias(it) => GenericDefId::TypeAliasId(it.id),
|
2020-02-29 20:24:40 +00:00
|
|
|
GenericDef::ImplDef(it) => GenericDefId::ImplId(it.id),
|
2019-11-20 09:25:02 +00:00
|
|
|
GenericDef::EnumVariant(it) => {
|
|
|
|
GenericDefId::EnumVariantId(EnumVariantId { parent: it.parent.id, local_id: it.id })
|
|
|
|
}
|
|
|
|
GenericDef::Const(it) => GenericDefId::ConstId(it.id),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-13 21:13:16 +00:00
|
|
|
impl From<GenericDefId> for GenericDef {
|
|
|
|
fn from(def: GenericDefId) -> Self {
|
|
|
|
match def {
|
|
|
|
GenericDefId::FunctionId(it) => GenericDef::Function(it.into()),
|
|
|
|
GenericDefId::AdtId(it) => GenericDef::Adt(it.into()),
|
|
|
|
GenericDefId::TraitId(it) => GenericDef::Trait(it.into()),
|
|
|
|
GenericDefId::TypeAliasId(it) => GenericDef::TypeAlias(it.into()),
|
|
|
|
GenericDefId::ImplId(it) => GenericDef::ImplDef(it.into()),
|
|
|
|
GenericDefId::EnumVariantId(it) => {
|
|
|
|
GenericDef::EnumVariant(EnumVariant { parent: it.parent.into(), id: it.local_id })
|
|
|
|
}
|
|
|
|
GenericDefId::ConstId(it) => GenericDef::Const(it.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 09:21:46 +00:00
|
|
|
impl From<Adt> for GenericDefId {
|
|
|
|
fn from(id: Adt) -> Self {
|
|
|
|
match id {
|
|
|
|
Adt::Struct(it) => it.id.into(),
|
|
|
|
Adt::Union(it) => it.id.into(),
|
|
|
|
Adt::Enum(it) => it.id.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-21 13:23:02 +00:00
|
|
|
|
2019-11-26 11:29:12 +00:00
|
|
|
impl From<VariantId> for VariantDef {
|
|
|
|
fn from(def: VariantId) -> Self {
|
|
|
|
match def {
|
|
|
|
VariantId::StructId(it) => VariantDef::Struct(it.into()),
|
|
|
|
VariantId::EnumVariantId(it) => VariantDef::EnumVariant(it.into()),
|
|
|
|
VariantId::UnionId(it) => VariantDef::Union(it.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 18:43:36 +00:00
|
|
|
impl From<VariantDef> for VariantId {
|
|
|
|
fn from(def: VariantDef) -> Self {
|
|
|
|
match def {
|
|
|
|
VariantDef::Struct(it) => VariantId::StructId(it.id),
|
|
|
|
VariantDef::EnumVariant(it) => VariantId::EnumVariantId(it.into()),
|
2019-11-26 11:29:12 +00:00
|
|
|
VariantDef::Union(it) => VariantId::UnionId(it.id),
|
2019-11-22 18:43:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-23 08:14:10 +00:00
|
|
|
|
2020-04-25 12:23:34 +00:00
|
|
|
impl From<Field> for FieldId {
|
|
|
|
fn from(def: Field) -> Self {
|
|
|
|
FieldId { parent: def.parent.into(), local_id: def.id }
|
2019-11-23 08:14:10 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-23 11:43:38 +00:00
|
|
|
|
2020-04-25 12:23:34 +00:00
|
|
|
impl From<FieldId> for Field {
|
|
|
|
fn from(def: FieldId) -> Self {
|
|
|
|
Field { parent: def.parent.into(), id: def.local_id }
|
2019-11-26 11:29:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 12:41:53 +00:00
|
|
|
impl From<AssocItem> for GenericDefId {
|
|
|
|
fn from(item: AssocItem) -> Self {
|
|
|
|
match item {
|
|
|
|
AssocItem::Function(f) => f.id.into(),
|
|
|
|
AssocItem::Const(c) => c.id.into(),
|
|
|
|
AssocItem::TypeAlias(t) => t.id.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-29 17:32:18 +00:00
|
|
|
|
|
|
|
impl From<(DefWithBodyId, PatId)> for Local {
|
|
|
|
fn from((parent, pat_id): (DefWithBodyId, PatId)) -> Self {
|
2020-02-29 17:34:34 +00:00
|
|
|
Local { parent, pat_id }
|
2020-02-29 17:32:18 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-23 11:34:56 +00:00
|
|
|
|
|
|
|
impl From<MacroDef> for ItemInNs {
|
|
|
|
fn from(macro_def: MacroDef) -> Self {
|
|
|
|
ItemInNs::Macros(macro_def.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ModuleDef> for ItemInNs {
|
|
|
|
fn from(module_def: ModuleDef) -> Self {
|
|
|
|
match module_def {
|
|
|
|
ModuleDef::Static(_) | ModuleDef::Const(_) | ModuleDef::Function(_) => {
|
|
|
|
ItemInNs::Values(module_def.into())
|
|
|
|
}
|
|
|
|
_ => ItemInNs::Types(module_def.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|