forked from rust-lang/chalk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.rs
196 lines (169 loc) · 6.07 KB
/
db.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use crate::{
error::ChalkError,
interner::ChalkIr,
lowering::LowerGoal,
program::Program,
query::{Lowering, LoweringDatabase},
tls,
};
use chalk_ir::{
AdtId, AssocTypeId, Binders, Canonical, ClosureId, ConstrainedSubst, Environment, FnDefId,
GenericArg, Goal, ImplId, InEnvironment, OpaqueTyId, ProgramClause, ProgramClauses,
Substitution, TraitId, Ty, UCanonical,
};
use chalk_solve::rust_ir::{
AdtDatum, AssociatedTyDatum, AssociatedTyValue, AssociatedTyValueId, ClosureKind, FnDefDatum,
FnDefInputsAndOutputDatum, ImplDatum, OpaqueTyDatum, TraitDatum, WellKnownTrait,
};
use chalk_solve::{RustIrDatabase, Solution, SolverChoice, SubstitutionResult};
use salsa::Database;
use std::sync::Arc;
#[salsa::database(Lowering)]
#[derive(Debug, Default)]
pub struct ChalkDatabase {
runtime: salsa::Runtime<ChalkDatabase>,
}
impl Database for ChalkDatabase {
fn salsa_runtime(&self) -> &salsa::Runtime<ChalkDatabase> {
&self.runtime
}
}
impl ChalkDatabase {
pub fn with(program_text: &str, solver_choice: SolverChoice) -> Self {
let mut db = ChalkDatabase::default();
db.set_program_text(Arc::new(program_text.to_string()));
db.set_solver_choice(solver_choice);
db
}
pub fn with_program<R>(&self, op: impl FnOnce(&Program) -> R) -> R {
let program = &self.checked_program().unwrap();
tls::set_current_program(&program, || op(&program))
}
pub fn parse_and_lower_goal(&self, text: &str) -> Result<Goal<ChalkIr>, ChalkError> {
let program = self.checked_program()?;
Ok(chalk_parse::parse_goal(text)?.lower(&*program)?)
}
pub fn solve(
&self,
goal: &UCanonical<InEnvironment<Goal<ChalkIr>>>,
) -> Option<Solution<ChalkIr>> {
let solver = self.solver();
let solution = solver.lock().unwrap().solve(self, goal);
solution
}
/// Solves a given goal, producing the solution. This will do only
/// as much work towards `goal` as it has to (and that works is
/// cached for future attempts). Calls provided function `f` to
/// iterate over multiple solutions until the function return `false`.
pub fn solve_multiple(
&self,
goal: &UCanonical<InEnvironment<Goal<ChalkIr>>>,
f: impl FnMut(SubstitutionResult<Canonical<ConstrainedSubst<ChalkIr>>>, bool) -> bool,
) -> bool {
let solver = self.solver();
let solution = solver.lock().unwrap().solve_multiple(self, goal, f);
solution
}
}
impl RustIrDatabase<ChalkIr> for ChalkDatabase {
fn custom_clauses(&self) -> Vec<ProgramClause<ChalkIr>> {
self.program_ir().unwrap().custom_clauses()
}
fn associated_ty_data(&self, ty: AssocTypeId<ChalkIr>) -> Arc<AssociatedTyDatum<ChalkIr>> {
self.program_ir().unwrap().associated_ty_data(ty)
}
fn trait_datum(&self, id: TraitId<ChalkIr>) -> Arc<TraitDatum<ChalkIr>> {
self.program_ir().unwrap().trait_datum(id)
}
fn impl_datum(&self, id: ImplId<ChalkIr>) -> Arc<ImplDatum<ChalkIr>> {
self.program_ir().unwrap().impl_datum(id)
}
fn associated_ty_value(
&self,
id: AssociatedTyValueId<ChalkIr>,
) -> Arc<AssociatedTyValue<ChalkIr>> {
self.program_ir().unwrap().associated_ty_values[&id].clone()
}
fn opaque_ty_data(&self, id: OpaqueTyId<ChalkIr>) -> Arc<OpaqueTyDatum<ChalkIr>> {
self.program_ir().unwrap().opaque_ty_data(id)
}
fn hidden_opaque_type(&self, id: OpaqueTyId<ChalkIr>) -> Ty<ChalkIr> {
self.program_ir().unwrap().hidden_opaque_type(id)
}
fn adt_datum(&self, id: AdtId<ChalkIr>) -> Arc<AdtDatum<ChalkIr>> {
self.program_ir().unwrap().adt_datum(id)
}
fn fn_def_datum(&self, id: FnDefId<ChalkIr>) -> Arc<FnDefDatum<ChalkIr>> {
self.program_ir().unwrap().fn_def_datum(id)
}
fn impls_for_trait(
&self,
trait_id: TraitId<ChalkIr>,
generic_args: &[GenericArg<ChalkIr>],
) -> Vec<ImplId<ChalkIr>> {
self.program_ir()
.unwrap()
.impls_for_trait(trait_id, generic_args)
}
fn local_impls_to_coherence_check(&self, trait_id: TraitId<ChalkIr>) -> Vec<ImplId<ChalkIr>> {
self.program_ir()
.unwrap()
.local_impls_to_coherence_check(trait_id)
}
fn impl_provided_for(&self, auto_trait_id: TraitId<ChalkIr>, adt_id: AdtId<ChalkIr>) -> bool {
self.program_ir()
.unwrap()
.impl_provided_for(auto_trait_id, adt_id)
}
fn well_known_trait_id(&self, well_known_trait: WellKnownTrait) -> Option<TraitId<ChalkIr>> {
self.program_ir()
.unwrap()
.well_known_trait_id(well_known_trait)
}
fn program_clauses_for_env(
&self,
environment: &Environment<ChalkIr>,
) -> ProgramClauses<ChalkIr> {
chalk_solve::program_clauses_for_env(self, environment)
}
fn interner(&self) -> &ChalkIr {
&ChalkIr
}
fn is_object_safe(&self, trait_id: TraitId<ChalkIr>) -> bool {
self.program_ir().unwrap().is_object_safe(trait_id)
}
fn closure_inputs_and_output(
&self,
closure_id: ClosureId<ChalkIr>,
substs: &Substitution<ChalkIr>,
) -> Binders<FnDefInputsAndOutputDatum<ChalkIr>> {
self.program_ir()
.unwrap()
.closure_inputs_and_output(closure_id, substs)
}
fn closure_kind(
&self,
closure_id: ClosureId<ChalkIr>,
substs: &Substitution<ChalkIr>,
) -> ClosureKind {
self.program_ir().unwrap().closure_kind(closure_id, substs)
}
fn closure_upvars(
&self,
closure_id: ClosureId<ChalkIr>,
substs: &Substitution<ChalkIr>,
) -> Binders<Ty<ChalkIr>> {
self.program_ir()
.unwrap()
.closure_upvars(closure_id, substs)
}
fn closure_fn_substitution(
&self,
closure_id: ClosureId<ChalkIr>,
substs: &Substitution<ChalkIr>,
) -> Substitution<ChalkIr> {
self.program_ir()
.unwrap()
.closure_fn_substitution(closure_id, substs)
}
}