aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/db.rs
blob: 7fc3fe31badfda1aa96e37b5246bcab4253ef371 (plain)
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
use std::sync::Arc;
#[cfg(test)]
use parking_lot::Mutex;
use salsa::{self, Database};
use ra_db::{LocationIntener, BaseDatabase};
use hir::{self, DefId, DefLoc, FnId, SourceItemId};

use crate::{
    symbol_index,
};

#[derive(Debug)]
pub(crate) struct RootDatabase {
    #[cfg(test)]
    events: Mutex<Option<Vec<salsa::Event<RootDatabase>>>>,
    #[cfg(not(test))]
    events: (),

    runtime: salsa::Runtime<RootDatabase>,
    id_maps: Arc<IdMaps>,
}

#[derive(Debug, Default)]
struct IdMaps {
    fns: LocationIntener<SourceItemId, FnId>,
    defs: LocationIntener<DefLoc, DefId>,
}

impl salsa::Database for RootDatabase {
    fn salsa_runtime(&self) -> &salsa::Runtime<RootDatabase> {
        &self.runtime
    }

    #[allow(unused)]
    fn salsa_event(&self, event: impl Fn() -> salsa::Event<RootDatabase>) {
        #[cfg(test)]
        {
            let mut events = self.events.lock();
            if let Some(events) = &mut *events {
                events.push(event());
            }
        }
    }
}

impl Default for RootDatabase {
    fn default() -> RootDatabase {
        let mut db = RootDatabase {
            events: Default::default(),
            runtime: salsa::Runtime::default(),
            id_maps: Default::default(),
        };
        db.query_mut(ra_db::SourceRootQuery)
            .set(ra_db::WORKSPACE, Default::default());
        db.query_mut(ra_db::CrateGraphQuery)
            .set((), Default::default());
        db.query_mut(ra_db::LibrariesQuery)
            .set((), Default::default());
        db
    }
}

impl salsa::ParallelDatabase for RootDatabase {
    fn snapshot(&self) -> salsa::Snapshot<RootDatabase> {
        salsa::Snapshot::new(RootDatabase {
            events: Default::default(),
            runtime: self.runtime.snapshot(self),
            id_maps: self.id_maps.clone(),
        })
    }
}

impl BaseDatabase for RootDatabase {}

impl AsRef<LocationIntener<DefLoc, DefId>> for RootDatabase {
    fn as_ref(&self) -> &LocationIntener<DefLoc, DefId> {
        &self.id_maps.defs
    }
}

impl AsRef<LocationIntener<hir::SourceItemId, FnId>> for RootDatabase {
    fn as_ref(&self) -> &LocationIntener<hir::SourceItemId, FnId> {
        &self.id_maps.fns
    }
}

#[cfg(test)]
impl RootDatabase {
    pub(crate) fn log(&self, f: impl FnOnce()) -> Vec<salsa::Event<RootDatabase>> {
        *self.events.lock() = Some(Vec::new());
        f();
        let events = self.events.lock().take().unwrap();
        events
    }

    pub(crate) fn log_executed(&self, f: impl FnOnce()) -> Vec<String> {
        let events = self.log(f);
        events
            .into_iter()
            .filter_map(|e| match e.kind {
                // This pretty horrible, but `Debug` is the only way to inspect
                // QueryDescriptor at the moment.
                salsa::EventKind::WillExecute { descriptor } => Some(format!("{:?}", descriptor)),
                _ => None,
            })
            .collect()
    }
}

salsa::database_storage! {
    pub(crate) struct RootDatabaseStorage for RootDatabase {
        impl ra_db::FilesDatabase {
            fn file_text() for ra_db::FileTextQuery;
            fn file_source_root() for ra_db::FileSourceRootQuery;
            fn source_root() for ra_db::SourceRootQuery;
            fn libraries() for ra_db::LibrariesQuery;
            fn crate_graph() for ra_db::CrateGraphQuery;
        }
        impl ra_db::SyntaxDatabase {
            fn source_file() for ra_db::SourceFileQuery;
            fn file_lines() for ra_db::FileLinesQuery;
        }
        impl symbol_index::SymbolsDatabase {
            fn file_symbols() for symbol_index::FileSymbolsQuery;
            fn library_symbols() for symbol_index::LibrarySymbolsQuery;
        }
        impl hir::db::HirDatabase {
            fn module_tree() for hir::db::ModuleTreeQuery;
            fn fn_scopes() for hir::db::FnScopesQuery;
            fn file_items() for hir::db::SourceFileItemsQuery;
            fn file_item() for hir::db::FileItemQuery;
            fn input_module_items() for hir::db::InputModuleItemsQuery;
            fn item_map() for hir::db::ItemMapQuery;
            fn fn_syntax() for hir::db::FnSyntaxQuery;
            fn submodules() for hir::db::SubmodulesQuery;
        }
    }
}