aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/db.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_ty/src/db.rs')
-rw-r--r--crates/hir_ty/src/db.rs158
1 files changed, 158 insertions, 0 deletions
diff --git a/crates/hir_ty/src/db.rs b/crates/hir_ty/src/db.rs
new file mode 100644
index 000000000..25cf9eb7f
--- /dev/null
+++ b/crates/hir_ty/src/db.rs
@@ -0,0 +1,158 @@
1//! FIXME: write short doc here
2
3use std::sync::Arc;
4
5use arena::map::ArenaMap;
6use base_db::{impl_intern_key, salsa, CrateId, Upcast};
7use hir_def::{
8 db::DefDatabase, expr::ExprId, DefWithBodyId, FunctionId, GenericDefId, ImplId, LocalFieldId,
9 TypeParamId, VariantId,
10};
11
12use crate::{
13 method_resolution::{InherentImpls, TraitImpls},
14 traits::chalk,
15 Binders, CallableDefId, GenericPredicate, InferenceResult, OpaqueTyId, PolyFnSig,
16 ReturnTypeImplTraits, TraitRef, Ty, TyDefId, ValueTyDefId,
17};
18use hir_expand::name::Name;
19
20#[salsa::query_group(HirDatabaseStorage)]
21pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
22 #[salsa::invoke(infer_wait)]
23 #[salsa::transparent]
24 fn infer(&self, def: DefWithBodyId) -> Arc<InferenceResult>;
25
26 #[salsa::invoke(crate::infer::infer_query)]
27 fn infer_query(&self, def: DefWithBodyId) -> Arc<InferenceResult>;
28
29 #[salsa::invoke(crate::lower::ty_query)]
30 #[salsa::cycle(crate::lower::ty_recover)]
31 fn ty(&self, def: TyDefId) -> Binders<Ty>;
32
33 #[salsa::invoke(crate::lower::value_ty_query)]
34 fn value_ty(&self, def: ValueTyDefId) -> Binders<Ty>;
35
36 #[salsa::invoke(crate::lower::impl_self_ty_query)]
37 #[salsa::cycle(crate::lower::impl_self_ty_recover)]
38 fn impl_self_ty(&self, def: ImplId) -> Binders<Ty>;
39
40 #[salsa::invoke(crate::lower::impl_trait_query)]
41 fn impl_trait(&self, def: ImplId) -> Option<Binders<TraitRef>>;
42
43 #[salsa::invoke(crate::lower::field_types_query)]
44 fn field_types(&self, var: VariantId) -> Arc<ArenaMap<LocalFieldId, Binders<Ty>>>;
45
46 #[salsa::invoke(crate::callable_item_sig)]
47 fn callable_item_signature(&self, def: CallableDefId) -> PolyFnSig;
48
49 #[salsa::invoke(crate::lower::return_type_impl_traits)]
50 fn return_type_impl_traits(
51 &self,
52 def: FunctionId,
53 ) -> Option<Arc<Binders<ReturnTypeImplTraits>>>;
54
55 #[salsa::invoke(crate::lower::generic_predicates_for_param_query)]
56 #[salsa::cycle(crate::lower::generic_predicates_for_param_recover)]
57 fn generic_predicates_for_param(
58 &self,
59 param_id: TypeParamId,
60 ) -> Arc<[Binders<GenericPredicate>]>;
61
62 #[salsa::invoke(crate::lower::generic_predicates_query)]
63 fn generic_predicates(&self, def: GenericDefId) -> Arc<[Binders<GenericPredicate>]>;
64
65 #[salsa::invoke(crate::lower::generic_defaults_query)]
66 fn generic_defaults(&self, def: GenericDefId) -> Arc<[Binders<Ty>]>;
67
68 #[salsa::invoke(InherentImpls::inherent_impls_in_crate_query)]
69 fn inherent_impls_in_crate(&self, krate: CrateId) -> Arc<InherentImpls>;
70
71 #[salsa::invoke(TraitImpls::trait_impls_in_crate_query)]
72 fn trait_impls_in_crate(&self, krate: CrateId) -> Arc<TraitImpls>;
73
74 #[salsa::invoke(TraitImpls::trait_impls_in_deps_query)]
75 fn trait_impls_in_deps(&self, krate: CrateId) -> Arc<TraitImpls>;
76
77 // Interned IDs for Chalk integration
78 #[salsa::interned]
79 fn intern_callable_def(&self, callable_def: CallableDefId) -> InternedCallableDefId;
80 #[salsa::interned]
81 fn intern_type_param_id(&self, param_id: TypeParamId) -> GlobalTypeParamId;
82 #[salsa::interned]
83 fn intern_impl_trait_id(&self, id: OpaqueTyId) -> InternedOpaqueTyId;
84 #[salsa::interned]
85 fn intern_closure(&self, id: (DefWithBodyId, ExprId)) -> ClosureId;
86
87 #[salsa::invoke(chalk::associated_ty_data_query)]
88 fn associated_ty_data(&self, id: chalk::AssocTypeId) -> Arc<chalk::AssociatedTyDatum>;
89
90 #[salsa::invoke(chalk::trait_datum_query)]
91 fn trait_datum(&self, krate: CrateId, trait_id: chalk::TraitId) -> Arc<chalk::TraitDatum>;
92
93 #[salsa::invoke(chalk::struct_datum_query)]
94 fn struct_datum(&self, krate: CrateId, struct_id: chalk::AdtId) -> Arc<chalk::StructDatum>;
95
96 #[salsa::invoke(crate::traits::chalk::impl_datum_query)]
97 fn impl_datum(&self, krate: CrateId, impl_id: chalk::ImplId) -> Arc<chalk::ImplDatum>;
98
99 #[salsa::invoke(crate::traits::chalk::fn_def_datum_query)]
100 fn fn_def_datum(&self, krate: CrateId, fn_def_id: chalk::FnDefId) -> Arc<chalk::FnDefDatum>;
101
102 #[salsa::invoke(crate::traits::chalk::associated_ty_value_query)]
103 fn associated_ty_value(
104 &self,
105 krate: CrateId,
106 id: chalk::AssociatedTyValueId,
107 ) -> Arc<chalk::AssociatedTyValue>;
108
109 #[salsa::invoke(crate::traits::trait_solve_query)]
110 fn trait_solve(
111 &self,
112 krate: CrateId,
113 goal: crate::Canonical<crate::InEnvironment<crate::Obligation>>,
114 ) -> Option<crate::traits::Solution>;
115
116 #[salsa::invoke(crate::traits::chalk::program_clauses_for_chalk_env_query)]
117 fn program_clauses_for_chalk_env(
118 &self,
119 krate: CrateId,
120 env: chalk_ir::Environment<chalk::Interner>,
121 ) -> chalk_ir::ProgramClauses<chalk::Interner>;
122}
123
124fn infer_wait(db: &impl HirDatabase, def: DefWithBodyId) -> Arc<InferenceResult> {
125 let _p = profile::span("infer:wait").detail(|| match def {
126 DefWithBodyId::FunctionId(it) => db.function_data(it).name.to_string(),
127 DefWithBodyId::StaticId(it) => {
128 db.static_data(it).name.clone().unwrap_or_else(Name::missing).to_string()
129 }
130 DefWithBodyId::ConstId(it) => {
131 db.const_data(it).name.clone().unwrap_or_else(Name::missing).to_string()
132 }
133 });
134 db.infer_query(def)
135}
136
137#[test]
138fn hir_database_is_object_safe() {
139 fn _assert_object_safe(_: &dyn HirDatabase) {}
140}
141
142#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
143pub struct GlobalTypeParamId(salsa::InternId);
144impl_intern_key!(GlobalTypeParamId);
145
146#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
147pub struct InternedOpaqueTyId(salsa::InternId);
148impl_intern_key!(InternedOpaqueTyId);
149
150#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
151pub struct ClosureId(salsa::InternId);
152impl_intern_key!(ClosureId);
153
154/// This exists just for Chalk, because Chalk just has a single `FnDefId` where
155/// we have different IDs for struct and enum variant constructors.
156#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
157pub struct InternedCallableDefId(salsa::InternId);
158impl_intern_key!(InternedCallableDefId);