• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! base_db defines basic database traits. The concrete DB is defined by ide.
2 
3 #![warn(rust_2018_idioms, unused_lifetimes, semicolon_in_expressions_from_macros)]
4 
5 mod input;
6 mod change;
7 pub mod fixture;
8 
9 use std::panic;
10 
11 use rustc_hash::FxHashSet;
12 use syntax::{ast, Parse, SourceFile, TextRange, TextSize};
13 use triomphe::Arc;
14 
15 pub use crate::{
16     change::Change,
17     input::{
18         CrateData, CrateDisplayName, CrateGraph, CrateId, CrateName, CrateOrigin, Dependency,
19         Edition, Env, LangCrateOrigin, ProcMacro, ProcMacroExpander, ProcMacroExpansionError,
20         ProcMacroId, ProcMacroKind, ProcMacroLoadResult, ProcMacroPaths, ProcMacros,
21         ReleaseChannel, SourceRoot, SourceRootId, TargetLayoutLoadResult,
22     },
23 };
24 pub use salsa::{self, Cancelled};
25 pub use vfs::{file_set::FileSet, AnchoredPath, AnchoredPathBuf, FileId, VfsPath};
26 
27 #[macro_export]
28 macro_rules! impl_intern_key {
29     ($name:ident) => {
30         impl $crate::salsa::InternKey for $name {
31             fn from_intern_id(v: $crate::salsa::InternId) -> Self {
32                 $name(v)
33             }
34             fn as_intern_id(&self) -> $crate::salsa::InternId {
35                 self.0
36             }
37         }
38     };
39 }
40 
41 pub trait Upcast<T: ?Sized> {
upcast(&self) -> &T42     fn upcast(&self) -> &T;
43 }
44 
45 #[derive(Clone, Copy, Debug)]
46 pub struct FilePosition {
47     pub file_id: FileId,
48     pub offset: TextSize,
49 }
50 
51 #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
52 pub struct FileRange {
53     pub file_id: FileId,
54     pub range: TextRange,
55 }
56 
57 pub const DEFAULT_PARSE_LRU_CAP: usize = 128;
58 
59 pub trait FileLoader {
60     /// Text of the file.
file_text(&self, file_id: FileId) -> Arc<str>61     fn file_text(&self, file_id: FileId) -> Arc<str>;
resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId>62     fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId>;
relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>>63     fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>>;
64 }
65 
66 /// Database which stores all significant input facts: source code and project
67 /// model. Everything else in rust-analyzer is derived from these queries.
68 #[salsa::query_group(SourceDatabaseStorage)]
69 pub trait SourceDatabase: FileLoader + std::fmt::Debug {
70     // Parses the file into the syntax tree.
71     #[salsa::invoke(parse_query)]
parse(&self, file_id: FileId) -> Parse<ast::SourceFile>72     fn parse(&self, file_id: FileId) -> Parse<ast::SourceFile>;
73 
74     /// The crate graph.
75     #[salsa::input]
crate_graph(&self) -> Arc<CrateGraph>76     fn crate_graph(&self) -> Arc<CrateGraph>;
77 
78     /// The crate graph.
79     #[salsa::input]
proc_macros(&self) -> Arc<ProcMacros>80     fn proc_macros(&self) -> Arc<ProcMacros>;
81 }
82 
parse_query(db: &dyn SourceDatabase, file_id: FileId) -> Parse<ast::SourceFile>83 fn parse_query(db: &dyn SourceDatabase, file_id: FileId) -> Parse<ast::SourceFile> {
84     let _p = profile::span("parse_query").detail(|| format!("{file_id:?}"));
85     let text = db.file_text(file_id);
86     SourceFile::parse(&text)
87 }
88 
89 /// We don't want to give HIR knowledge of source roots, hence we extract these
90 /// methods into a separate DB.
91 #[salsa::query_group(SourceDatabaseExtStorage)]
92 pub trait SourceDatabaseExt: SourceDatabase {
93     #[salsa::input]
file_text(&self, file_id: FileId) -> Arc<str>94     fn file_text(&self, file_id: FileId) -> Arc<str>;
95     /// Path to a file, relative to the root of its source root.
96     /// Source root of the file.
97     #[salsa::input]
file_source_root(&self, file_id: FileId) -> SourceRootId98     fn file_source_root(&self, file_id: FileId) -> SourceRootId;
99     /// Contents of the source root.
100     #[salsa::input]
source_root(&self, id: SourceRootId) -> Arc<SourceRoot>101     fn source_root(&self, id: SourceRootId) -> Arc<SourceRoot>;
102 
source_root_crates(&self, id: SourceRootId) -> Arc<FxHashSet<CrateId>>103     fn source_root_crates(&self, id: SourceRootId) -> Arc<FxHashSet<CrateId>>;
104 }
105 
source_root_crates(db: &dyn SourceDatabaseExt, id: SourceRootId) -> Arc<FxHashSet<CrateId>>106 fn source_root_crates(db: &dyn SourceDatabaseExt, id: SourceRootId) -> Arc<FxHashSet<CrateId>> {
107     let graph = db.crate_graph();
108     let res = graph
109         .iter()
110         .filter(|&krate| {
111             let root_file = graph[krate].root_file_id;
112             db.file_source_root(root_file) == id
113         })
114         .collect();
115     Arc::new(res)
116 }
117 
118 /// Silly workaround for cyclic deps between the traits
119 pub struct FileLoaderDelegate<T>(pub T);
120 
121 impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
file_text(&self, file_id: FileId) -> Arc<str>122     fn file_text(&self, file_id: FileId) -> Arc<str> {
123         SourceDatabaseExt::file_text(self.0, file_id)
124     }
resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId>125     fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {
126         // FIXME: this *somehow* should be platform agnostic...
127         let source_root = self.0.file_source_root(path.anchor);
128         let source_root = self.0.source_root(source_root);
129         source_root.resolve_path(path)
130     }
131 
relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>>132     fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
133         let _p = profile::span("relevant_crates");
134         let source_root = self.0.file_source_root(file_id);
135         self.0.source_root_crates(source_root)
136     }
137 }
138