2020-05-22 10:14:53 -05:00
|
|
|
//! This module contains the implementations of the `ToChalk` trait, which
|
|
|
|
//! handles conversion between our data types and their corresponding types in
|
|
|
|
//! Chalk (in both directions); plus some helper functions for more specialized
|
|
|
|
//! conversions.
|
|
|
|
|
2020-05-27 14:05:21 -05:00
|
|
|
use chalk_solve::rust_ir;
|
2020-05-22 10:14:53 -05:00
|
|
|
|
2021-04-09 07:28:04 -05:00
|
|
|
use base_db::salsa::{self, InternKey};
|
2021-12-29 07:35:59 -06:00
|
|
|
use hir_def::{LifetimeParamId, TraitId, TypeAliasId, TypeOrConstParamId};
|
2020-05-22 10:14:53 -05:00
|
|
|
|
2021-04-09 07:28:04 -05:00
|
|
|
use crate::{
|
|
|
|
chalk_db, db::HirDatabase, AssocTypeId, CallableDefId, ChalkTraitId, FnDefId, ForeignDefId,
|
|
|
|
Interner, OpaqueTyId, PlaceholderIndex,
|
|
|
|
};
|
2021-04-09 07:15:26 -05:00
|
|
|
|
|
|
|
pub(crate) trait ToChalk {
|
|
|
|
type Chalk;
|
|
|
|
fn to_chalk(self, db: &dyn HirDatabase) -> Self::Chalk;
|
|
|
|
fn from_chalk(db: &dyn HirDatabase, chalk: Self::Chalk) -> Self;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn from_chalk<T, ChalkT>(db: &dyn HirDatabase, chalk: ChalkT) -> T
|
|
|
|
where
|
|
|
|
T: ToChalk<Chalk = ChalkT>,
|
|
|
|
{
|
|
|
|
T::from_chalk(db, chalk)
|
|
|
|
}
|
2020-05-22 10:14:53 -05:00
|
|
|
|
2020-07-12 08:26:02 -05:00
|
|
|
impl ToChalk for hir_def::ImplId {
|
2021-04-09 07:11:37 -05:00
|
|
|
type Chalk = chalk_db::ImplId;
|
2020-05-22 10:14:53 -05:00
|
|
|
|
2021-04-09 07:11:37 -05:00
|
|
|
fn to_chalk(self, _db: &dyn HirDatabase) -> chalk_db::ImplId {
|
2020-07-12 08:26:02 -05:00
|
|
|
chalk_ir::ImplId(self.as_intern_id())
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
|
|
|
|
2021-04-09 07:11:37 -05:00
|
|
|
fn from_chalk(_db: &dyn HirDatabase, impl_id: chalk_db::ImplId) -> hir_def::ImplId {
|
2020-07-12 08:26:02 -05:00
|
|
|
InternKey::from_intern_id(impl_id.0)
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 06:15:00 -05:00
|
|
|
impl ToChalk for CallableDefId {
|
2020-05-22 11:15:53 -05:00
|
|
|
type Chalk = FnDefId;
|
|
|
|
|
|
|
|
fn to_chalk(self, db: &dyn HirDatabase) -> FnDefId {
|
|
|
|
db.intern_callable_def(self).into()
|
|
|
|
}
|
|
|
|
|
2020-07-16 06:15:00 -05:00
|
|
|
fn from_chalk(db: &dyn HirDatabase, fn_def_id: FnDefId) -> CallableDefId {
|
2020-05-22 11:15:53 -05:00
|
|
|
db.lookup_intern_callable_def(fn_def_id.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 09:31:38 -06:00
|
|
|
pub(crate) struct TypeAliasAsValue(pub(crate) TypeAliasId);
|
2020-07-12 08:26:02 -05:00
|
|
|
|
|
|
|
impl ToChalk for TypeAliasAsValue {
|
2021-04-09 07:11:37 -05:00
|
|
|
type Chalk = chalk_db::AssociatedTyValueId;
|
2020-05-22 10:14:53 -05:00
|
|
|
|
2021-04-09 07:11:37 -05:00
|
|
|
fn to_chalk(self, _db: &dyn HirDatabase) -> chalk_db::AssociatedTyValueId {
|
2020-07-12 08:26:02 -05:00
|
|
|
rust_ir::AssociatedTyValueId(self.0.as_intern_id())
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
|
|
|
|
2020-07-12 08:26:02 -05:00
|
|
|
fn from_chalk(
|
|
|
|
_db: &dyn HirDatabase,
|
2021-04-09 07:11:37 -05:00
|
|
|
assoc_ty_value_id: chalk_db::AssociatedTyValueId,
|
2020-07-12 08:26:02 -05:00
|
|
|
) -> TypeAliasAsValue {
|
|
|
|
TypeAliasAsValue(TypeAliasId::from_intern_id(assoc_ty_value_id.0))
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 07:18:58 -05:00
|
|
|
impl From<FnDefId> for crate::db::InternedCallableDefId {
|
|
|
|
fn from(fn_def_id: FnDefId) -> Self {
|
|
|
|
InternKey::from_intern_id(fn_def_id.0)
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 07:18:58 -05:00
|
|
|
impl From<crate::db::InternedCallableDefId> for FnDefId {
|
|
|
|
fn from(callable_def_id: crate::db::InternedCallableDefId) -> Self {
|
|
|
|
chalk_ir::FnDefId(callable_def_id.as_intern_id())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<OpaqueTyId> for crate::db::InternedOpaqueTyId {
|
|
|
|
fn from(id: OpaqueTyId) -> Self {
|
|
|
|
InternKey::from_intern_id(id.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<crate::db::InternedOpaqueTyId> for OpaqueTyId {
|
|
|
|
fn from(id: crate::db::InternedOpaqueTyId) -> Self {
|
|
|
|
chalk_ir::OpaqueTyId(id.as_intern_id())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<chalk_ir::ClosureId<Interner>> for crate::db::InternedClosureId {
|
|
|
|
fn from(id: chalk_ir::ClosureId<Interner>) -> Self {
|
|
|
|
Self::from_intern_id(id.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<crate::db::InternedClosureId> for chalk_ir::ClosureId<Interner> {
|
|
|
|
fn from(id: crate::db::InternedClosureId) -> Self {
|
|
|
|
chalk_ir::ClosureId(id.as_intern_id())
|
2020-05-22 10:14:53 -05:00
|
|
|
}
|
|
|
|
}
|
2021-04-09 07:28:04 -05:00
|
|
|
|
2022-09-06 03:48:06 -05:00
|
|
|
impl From<chalk_ir::GeneratorId<Interner>> for crate::db::InternedGeneratorId {
|
|
|
|
fn from(id: chalk_ir::GeneratorId<Interner>) -> Self {
|
|
|
|
Self::from_intern_id(id.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<crate::db::InternedGeneratorId> for chalk_ir::GeneratorId<Interner> {
|
|
|
|
fn from(id: crate::db::InternedGeneratorId) -> Self {
|
|
|
|
chalk_ir::GeneratorId(id.as_intern_id())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-09 07:28:04 -05:00
|
|
|
pub fn to_foreign_def_id(id: TypeAliasId) -> ForeignDefId {
|
|
|
|
chalk_ir::ForeignDefId(salsa::InternKey::as_intern_id(&id))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_foreign_def_id(id: ForeignDefId) -> TypeAliasId {
|
|
|
|
salsa::InternKey::from_intern_id(id.0)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_assoc_type_id(id: TypeAliasId) -> AssocTypeId {
|
|
|
|
chalk_ir::AssocTypeId(salsa::InternKey::as_intern_id(&id))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_assoc_type_id(id: AssocTypeId) -> TypeAliasId {
|
|
|
|
salsa::InternKey::from_intern_id(id.0)
|
|
|
|
}
|
|
|
|
|
2021-12-29 07:35:59 -06:00
|
|
|
pub fn from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> TypeOrConstParamId {
|
2021-04-09 07:28:04 -05:00
|
|
|
assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT);
|
|
|
|
let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx));
|
2021-12-29 07:35:59 -06:00
|
|
|
db.lookup_intern_type_or_const_param_id(interned_id)
|
2021-04-09 07:28:04 -05:00
|
|
|
}
|
|
|
|
|
2021-12-29 07:35:59 -06:00
|
|
|
pub fn to_placeholder_idx(db: &dyn HirDatabase, id: TypeOrConstParamId) -> PlaceholderIndex {
|
|
|
|
let interned_id = db.intern_type_or_const_param_id(id);
|
2021-04-09 07:28:04 -05:00
|
|
|
PlaceholderIndex {
|
|
|
|
ui: chalk_ir::UniverseIndex::ROOT,
|
|
|
|
idx: salsa::InternKey::as_intern_id(&interned_id).as_usize(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn lt_from_placeholder_idx(db: &dyn HirDatabase, idx: PlaceholderIndex) -> LifetimeParamId {
|
|
|
|
assert_eq!(idx.ui, chalk_ir::UniverseIndex::ROOT);
|
|
|
|
let interned_id = salsa::InternKey::from_intern_id(salsa::InternId::from(idx.idx));
|
|
|
|
db.lookup_intern_lifetime_param_id(interned_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_chalk_trait_id(id: TraitId) -> ChalkTraitId {
|
|
|
|
chalk_ir::TraitId(salsa::InternKey::as_intern_id(&id))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_chalk_trait_id(id: ChalkTraitId) -> TraitId {
|
|
|
|
salsa::InternKey::from_intern_id(id.0)
|
|
|
|
}
|