2019-11-03 15:11:37 -06:00
|
|
|
//! Utility module for converting between hir_def ids and code_model wrappers.
|
|
|
|
//!
|
2021-01-08 08:46:48 -06:00
|
|
|
//! It's unclear if we need this long-term, but it's definitely useful while we
|
2019-11-03 15:11:37 -06:00
|
|
|
//! are splitting the hir.
|
|
|
|
|
2019-11-21 03:21:46 -06:00
|
|
|
use hir_def::{
|
2023-06-05 04:04:23 -05:00
|
|
|
hir::{BindingId, LabelId},
|
2021-01-01 17:42:07 -06:00
|
|
|
AdtId, AssocItemId, DefWithBodyId, EnumVariantId, FieldId, GenericDefId, GenericParamId,
|
|
|
|
ModuleDefId, VariantId,
|
2019-11-21 03:21:46 -06:00
|
|
|
};
|
2019-10-31 10:45:10 -05:00
|
|
|
|
2019-11-21 03:21:46 -06:00
|
|
|
use crate::{
|
2021-07-28 10:39:04 -05:00
|
|
|
Adt, AssocItem, BuiltinType, DefWithBody, Field, GenericDef, GenericParam, ItemInNs, Label,
|
|
|
|
Local, ModuleDef, Variant, VariantDef,
|
2019-11-21 03:21:46 -06:00
|
|
|
};
|
2019-10-31 10:45:10 -05:00
|
|
|
|
|
|
|
macro_rules! from_id {
|
2023-08-07 04:03:15 -05:00
|
|
|
($(($id:path, $ty:path)),* $(,)?) => {$(
|
2019-10-31 10:45:10 -05:00
|
|
|
impl From<$id> for $ty {
|
|
|
|
fn from(id: $id) -> $ty {
|
|
|
|
$ty { id }
|
|
|
|
}
|
|
|
|
}
|
2019-12-08 05:44:14 -06:00
|
|
|
impl From<$ty> for $id {
|
|
|
|
fn from(ty: $ty) -> $id {
|
|
|
|
ty.id
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 10:45:10 -05:00
|
|
|
)*}
|
|
|
|
}
|
|
|
|
|
|
|
|
from_id![
|
2020-08-13 09:25:38 -05:00
|
|
|
(base_db::CrateId, crate::Crate),
|
2019-10-31 10:45:10 -05: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),
|
2023-03-13 03:42:24 -05:00
|
|
|
(hir_def::TraitAliasId, crate::TraitAlias),
|
2019-10-31 10:45:10 -05:00
|
|
|
(hir_def::StaticId, crate::Static),
|
|
|
|
(hir_def::ConstId, crate::Const),
|
2023-06-19 01:14:04 -05:00
|
|
|
(hir_def::InTypeConstId, crate::InTypeConst),
|
2019-10-31 10:45:10 -05:00
|
|
|
(hir_def::FunctionId, crate::Function),
|
2020-12-17 05:36:15 -06:00
|
|
|
(hir_def::ImplId, crate::Impl),
|
2021-12-29 07:35:59 -06:00
|
|
|
(hir_def::TypeOrConstParamId, crate::TypeOrConstParam),
|
2020-02-26 06:22:46 -06:00
|
|
|
(hir_def::TypeParamId, crate::TypeParam),
|
2021-01-01 03:06:42 -06:00
|
|
|
(hir_def::ConstParamId, crate::ConstParam),
|
2021-12-29 07:35:59 -06:00
|
|
|
(hir_def::LifetimeParamId, crate::LifetimeParam),
|
2023-08-07 04:03:15 -05:00
|
|
|
(hir_def::MacroId, crate::Macro),
|
|
|
|
(hir_def::ExternCrateId, crate::ExternCrateDecl),
|
2019-10-31 10:45:10 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
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 03:25:02 -06: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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 17:42:07 -06:00
|
|
|
impl From<GenericParamId> for GenericParam {
|
|
|
|
fn from(id: GenericParamId) -> Self {
|
|
|
|
match id {
|
|
|
|
GenericParamId::TypeParamId(it) => GenericParam::TypeParam(it.into()),
|
|
|
|
GenericParamId::ConstParamId(it) => GenericParam::ConstParam(it.into()),
|
2021-12-29 07:35:59 -06:00
|
|
|
GenericParamId::LifetimeParamId(it) => GenericParam::LifetimeParam(it.into()),
|
2021-01-01 17:42:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<GenericParam> for GenericParamId {
|
|
|
|
fn from(id: GenericParam) -> Self {
|
|
|
|
match id {
|
|
|
|
GenericParam::LifetimeParam(it) => GenericParamId::LifetimeParamId(it.id),
|
2022-03-12 06:43:53 -06:00
|
|
|
GenericParam::ConstParam(it) => GenericParamId::ConstParamId(it.id),
|
|
|
|
GenericParam::TypeParam(it) => GenericParamId::TypeParamId(it.id),
|
2021-01-01 17:42:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-20 01:05:24 -06:00
|
|
|
impl From<EnumVariantId> for Variant {
|
2019-10-31 10:45:10 -05:00
|
|
|
fn from(id: EnumVariantId) -> Self {
|
2020-12-20 01:05:24 -06:00
|
|
|
Variant { parent: id.parent.into(), id: id.local_id }
|
2019-10-31 10:45:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-20 01:05:24 -06:00
|
|
|
impl From<Variant> for EnumVariantId {
|
|
|
|
fn from(def: Variant) -> Self {
|
2019-11-22 12:43:36 -06:00
|
|
|
EnumVariantId { parent: def.parent.id, local_id: def.id }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 10:45:10 -05: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()),
|
2020-12-20 01:05:24 -06:00
|
|
|
ModuleDefId::EnumVariantId(it) => ModuleDef::Variant(it.into()),
|
2019-10-31 10:45:10 -05:00
|
|
|
ModuleDefId::ConstId(it) => ModuleDef::Const(it.into()),
|
|
|
|
ModuleDefId::StaticId(it) => ModuleDef::Static(it.into()),
|
|
|
|
ModuleDefId::TraitId(it) => ModuleDef::Trait(it.into()),
|
2023-03-13 03:42:24 -05:00
|
|
|
ModuleDefId::TraitAliasId(it) => ModuleDef::TraitAlias(it.into()),
|
2019-10-31 10:45:10 -05:00
|
|
|
ModuleDefId::TypeAliasId(it) => ModuleDef::TypeAlias(it.into()),
|
2021-02-11 12:52:33 -06:00
|
|
|
ModuleDefId::BuiltinType(it) => ModuleDef::BuiltinType(it.into()),
|
2022-03-08 16:51:48 -06:00
|
|
|
ModuleDefId::MacroId(it) => ModuleDef::Macro(it.into()),
|
2019-10-31 10:45:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-14 08:37:22 -06:00
|
|
|
|
2019-12-31 09:17:08 -06: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()),
|
2020-12-20 01:05:24 -06:00
|
|
|
ModuleDef::Variant(it) => ModuleDefId::EnumVariantId(it.into()),
|
2019-12-31 09:17:08 -06:00
|
|
|
ModuleDef::Const(it) => ModuleDefId::ConstId(it.into()),
|
|
|
|
ModuleDef::Static(it) => ModuleDefId::StaticId(it.into()),
|
|
|
|
ModuleDef::Trait(it) => ModuleDefId::TraitId(it.into()),
|
2023-03-13 03:42:24 -05:00
|
|
|
ModuleDef::TraitAlias(it) => ModuleDefId::TraitAliasId(it.into()),
|
2019-12-31 09:17:08 -06:00
|
|
|
ModuleDef::TypeAlias(it) => ModuleDefId::TypeAliasId(it.into()),
|
2021-02-11 12:52:33 -06:00
|
|
|
ModuleDef::BuiltinType(it) => ModuleDefId::BuiltinType(it.into()),
|
2022-03-08 16:51:48 -06:00
|
|
|
ModuleDef::Macro(it) => ModuleDefId::MacroId(it.into()),
|
2019-12-31 09:17:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 08:37:22 -06: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),
|
2022-10-11 02:37:35 -05:00
|
|
|
DefWithBody::Variant(it) => DefWithBodyId::VariantId(it.into()),
|
2023-06-19 01:14:04 -05:00
|
|
|
DefWithBody::InTypeConst(it) => DefWithBodyId::InTypeConstId(it.id),
|
2019-11-14 08:37:22 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-15 12:28:00 -06:00
|
|
|
|
2019-11-21 03:21:46 -06: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()),
|
2022-10-11 02:37:35 -05:00
|
|
|
DefWithBodyId::VariantId(it) => DefWithBody::Variant(it.into()),
|
2023-06-19 01:14:04 -05:00
|
|
|
DefWithBodyId::InTypeConstId(it) => DefWithBody::InTypeConst(it.into()),
|
2019-11-21 03:21:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 12:28:00 -06: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 03:25:02 -06: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),
|
2023-03-13 03:42:24 -05:00
|
|
|
GenericDef::TraitAlias(it) => GenericDefId::TraitAliasId(it.id),
|
2019-11-20 03:25:02 -06:00
|
|
|
GenericDef::TypeAlias(it) => GenericDefId::TypeAliasId(it.id),
|
2020-12-17 05:36:15 -06:00
|
|
|
GenericDef::Impl(it) => GenericDefId::ImplId(it.id),
|
2022-10-11 02:37:35 -05:00
|
|
|
GenericDef::Variant(it) => GenericDefId::EnumVariantId(it.into()),
|
2019-11-20 03:25:02 -06:00
|
|
|
GenericDef::Const(it) => GenericDefId::ConstId(it.id),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-13 15:13:16 -06: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()),
|
2023-03-13 03:42:24 -05:00
|
|
|
GenericDefId::TraitAliasId(it) => GenericDef::TraitAlias(it.into()),
|
2020-12-13 15:13:16 -06:00
|
|
|
GenericDefId::TypeAliasId(it) => GenericDef::TypeAlias(it.into()),
|
2020-12-17 05:36:15 -06:00
|
|
|
GenericDefId::ImplId(it) => GenericDef::Impl(it.into()),
|
2022-10-11 02:37:35 -05:00
|
|
|
GenericDefId::EnumVariantId(it) => GenericDef::Variant(it.into()),
|
2020-12-13 15:13:16 -06:00
|
|
|
GenericDefId::ConstId(it) => GenericDef::Const(it.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 03:21:46 -06: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 07:23:02 -06:00
|
|
|
|
2019-11-26 05:29:12 -06:00
|
|
|
impl From<VariantId> for VariantDef {
|
|
|
|
fn from(def: VariantId) -> Self {
|
|
|
|
match def {
|
|
|
|
VariantId::StructId(it) => VariantDef::Struct(it.into()),
|
2020-12-20 01:05:24 -06:00
|
|
|
VariantId::EnumVariantId(it) => VariantDef::Variant(it.into()),
|
2019-11-26 05:29:12 -06:00
|
|
|
VariantId::UnionId(it) => VariantDef::Union(it.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-22 12:43:36 -06:00
|
|
|
impl From<VariantDef> for VariantId {
|
|
|
|
fn from(def: VariantDef) -> Self {
|
|
|
|
match def {
|
|
|
|
VariantDef::Struct(it) => VariantId::StructId(it.id),
|
2020-12-20 01:05:24 -06:00
|
|
|
VariantDef::Variant(it) => VariantId::EnumVariantId(it.into()),
|
2019-11-26 05:29:12 -06:00
|
|
|
VariantDef::Union(it) => VariantId::UnionId(it.id),
|
2019-11-22 12:43:36 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-23 02:14:10 -06:00
|
|
|
|
2020-04-25 07:23:34 -05:00
|
|
|
impl From<Field> for FieldId {
|
|
|
|
fn from(def: Field) -> Self {
|
|
|
|
FieldId { parent: def.parent.into(), local_id: def.id }
|
2019-11-23 02:14:10 -06:00
|
|
|
}
|
|
|
|
}
|
2019-11-23 05:43:38 -06:00
|
|
|
|
2020-04-25 07:23:34 -05:00
|
|
|
impl From<FieldId> for Field {
|
|
|
|
fn from(def: FieldId) -> Self {
|
|
|
|
Field { parent: def.parent.into(), id: def.local_id }
|
2019-11-26 05:29:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 06:41:53 -06: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 11:32:18 -06:00
|
|
|
|
2023-03-13 03:42:24 -05:00
|
|
|
impl From<(DefWithBodyId, BindingId)> for Local {
|
|
|
|
fn from((parent, binding_id): (DefWithBodyId, BindingId)) -> Self {
|
|
|
|
Local { parent, binding_id }
|
2020-02-29 11:32:18 -06:00
|
|
|
}
|
|
|
|
}
|
2020-03-23 06:34:56 -05:00
|
|
|
|
2020-12-23 09:34:30 -06:00
|
|
|
impl From<(DefWithBodyId, LabelId)> for Label {
|
|
|
|
fn from((parent, label_id): (DefWithBodyId, LabelId)) -> Self {
|
|
|
|
Label { parent, label_id }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-28 10:39:04 -05:00
|
|
|
impl From<hir_def::item_scope::ItemInNs> for ItemInNs {
|
|
|
|
fn from(it: hir_def::item_scope::ItemInNs) -> Self {
|
|
|
|
match it {
|
|
|
|
hir_def::item_scope::ItemInNs::Types(it) => ItemInNs::Types(it.into()),
|
|
|
|
hir_def::item_scope::ItemInNs::Values(it) => ItemInNs::Values(it.into()),
|
|
|
|
hir_def::item_scope::ItemInNs::Macros(it) => ItemInNs::Macros(it.into()),
|
|
|
|
}
|
2020-03-23 06:34:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-28 10:39:04 -05:00
|
|
|
impl From<ItemInNs> for hir_def::item_scope::ItemInNs {
|
|
|
|
fn from(it: ItemInNs) -> Self {
|
|
|
|
match it {
|
|
|
|
ItemInNs::Types(it) => Self::Types(it.into()),
|
|
|
|
ItemInNs::Values(it) => Self::Values(it.into()),
|
|
|
|
ItemInNs::Macros(it) => Self::Macros(it.into()),
|
2020-03-23 06:34:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-11 12:52:33 -06:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|