2018-09-15 09:21:47 -05:00
|
|
|
mod imp;
|
2018-09-13 14:58:36 -05:00
|
|
|
|
|
|
|
use std::{
|
|
|
|
sync::Arc,
|
|
|
|
};
|
|
|
|
use im;
|
|
|
|
use salsa;
|
2018-10-15 12:15:53 -05:00
|
|
|
use crate::{FileId, imp::FileResolverImp};
|
2018-09-13 14:58:36 -05:00
|
|
|
|
2018-09-15 09:21:47 -05:00
|
|
|
#[derive(Debug, Default, Clone)]
|
2018-09-13 14:58:36 -05:00
|
|
|
pub(crate) struct State {
|
2018-09-15 09:21:47 -05:00
|
|
|
pub(crate) file_map: im::HashMap<FileId, Arc<String>>,
|
|
|
|
pub(crate) file_resolver: FileResolverImp
|
2018-09-13 14:58:36 -05:00
|
|
|
}
|
|
|
|
|
2018-09-15 09:21:47 -05:00
|
|
|
#[derive(Debug)]
|
2018-09-13 14:58:36 -05:00
|
|
|
pub(crate) struct Db {
|
2018-09-15 09:21:47 -05:00
|
|
|
imp: imp::Db,
|
2018-09-13 14:58:36 -05:00
|
|
|
}
|
|
|
|
|
2018-09-15 09:21:47 -05:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub(crate) struct QueryCtx<'a> {
|
|
|
|
imp: &'a salsa::QueryCtx<State, imp::Data>,
|
2018-09-13 14:58:36 -05:00
|
|
|
}
|
|
|
|
|
2018-09-15 09:21:47 -05:00
|
|
|
pub(crate) struct Query<T, R>(pub(crate) u16, pub(crate) fn(QueryCtx, &T) -> R);
|
|
|
|
|
|
|
|
pub(crate) struct QueryRegistry {
|
|
|
|
imp: imp::QueryRegistry,
|
2018-09-13 14:58:36 -05:00
|
|
|
}
|
|
|
|
|
2018-09-15 15:19:41 -05:00
|
|
|
impl Default for Db {
|
|
|
|
fn default() -> Db {
|
|
|
|
Db::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 14:58:36 -05:00
|
|
|
impl Db {
|
2018-09-15 05:55:34 -05:00
|
|
|
pub(crate) fn new() -> Db {
|
2018-09-15 09:21:47 -05:00
|
|
|
let reg = QueryRegistry::new();
|
|
|
|
Db { imp: imp::Db::new(reg.imp) }
|
2018-09-13 14:58:36 -05:00
|
|
|
}
|
|
|
|
pub(crate) fn state(&self) -> &State {
|
2018-09-15 09:21:47 -05:00
|
|
|
self.imp.imp.ground_data()
|
2018-09-13 14:58:36 -05:00
|
|
|
}
|
2018-09-15 09:21:47 -05:00
|
|
|
pub(crate) fn with_changes(&self, new_state: State, changed_files: &[FileId], resolver_changed: bool) -> Db {
|
|
|
|
Db { imp: self.imp.with_changes(new_state, changed_files, resolver_changed) }
|
2018-09-13 14:58:36 -05:00
|
|
|
}
|
2018-09-15 09:21:47 -05:00
|
|
|
pub(crate) fn make_query<F: FnOnce(QueryCtx) -> R, R>(&self, f: F) -> R {
|
|
|
|
let ctx = QueryCtx { imp: &self.imp.imp.query_ctx() };
|
|
|
|
f(ctx)
|
|
|
|
}
|
2018-09-15 15:19:41 -05:00
|
|
|
#[allow(unused)]
|
2018-09-15 09:21:47 -05:00
|
|
|
pub(crate) fn trace_query<F: FnOnce(QueryCtx) -> R, R>(&self, f: F) -> (R, Vec<&'static str>) {
|
|
|
|
let ctx = QueryCtx { imp: &self.imp.imp.query_ctx() };
|
|
|
|
let res = f(ctx);
|
|
|
|
let trace = self.imp.extract_trace(ctx.imp);
|
|
|
|
(res, trace)
|
2018-09-13 14:58:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> QueryCtx<'a> {
|
2018-09-15 09:21:47 -05:00
|
|
|
pub(crate) fn get<Q: imp::EvalQuery>(&self, q: Q, params: Q::Params) -> Arc<Q::Output> {
|
|
|
|
q.get(self, params)
|
2018-09-13 14:58:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-15 09:21:47 -05:00
|
|
|
pub(crate) fn file_text(ctx: QueryCtx, file_id: FileId) -> Arc<String> {
|
|
|
|
imp::file_text(ctx, file_id)
|
2018-09-13 14:58:36 -05:00
|
|
|
}
|
|
|
|
|
2018-09-15 09:21:47 -05:00
|
|
|
pub(crate) fn file_set(ctx: QueryCtx) -> Arc<(Vec<FileId>, FileResolverImp)> {
|
|
|
|
imp::file_set(ctx)
|
2018-09-13 14:58:36 -05:00
|
|
|
}
|
2018-09-15 09:21:47 -05:00
|
|
|
impl QueryRegistry {
|
|
|
|
fn new() -> QueryRegistry {
|
|
|
|
let mut reg = QueryRegistry { imp: imp::QueryRegistry::new() };
|
2018-10-15 12:15:53 -05:00
|
|
|
crate::queries::register_queries(&mut reg);
|
|
|
|
crate::module_map::register_queries(&mut reg);
|
2018-09-15 09:21:47 -05:00
|
|
|
reg
|
|
|
|
}
|
|
|
|
pub(crate) fn add<Q: imp::EvalQuery>(&mut self, q: Q, name: &'static str) {
|
|
|
|
self.imp.add(q, name)
|
|
|
|
}
|
2018-09-13 14:58:36 -05:00
|
|
|
}
|