diff options
Diffstat (limited to 'crates/ra_hir/src/module.rs')
-rw-r--r-- | crates/ra_hir/src/module.rs | 376 |
1 files changed, 0 insertions, 376 deletions
diff --git a/crates/ra_hir/src/module.rs b/crates/ra_hir/src/module.rs deleted file mode 100644 index b9821115c..000000000 --- a/crates/ra_hir/src/module.rs +++ /dev/null | |||
@@ -1,376 +0,0 @@ | |||
1 | pub(super) mod imp; | ||
2 | pub(super) mod nameres; | ||
3 | |||
4 | use std::sync::Arc; | ||
5 | use log; | ||
6 | |||
7 | use ra_syntax::{ | ||
8 | algo::generate, | ||
9 | ast::{self, AstNode, NameOwner}, | ||
10 | SyntaxNode, | ||
11 | }; | ||
12 | use ra_arena::{Arena, RawId, impl_arena_id}; | ||
13 | use ra_db::{SourceRootId, FileId, Cancelable}; | ||
14 | use relative_path::RelativePathBuf; | ||
15 | |||
16 | use crate::{ | ||
17 | Def, DefKind, DefLoc, DefId, | ||
18 | Name, Path, PathKind, HirDatabase, SourceItemId, SourceFileItemId, Crate, | ||
19 | HirFileId, | ||
20 | }; | ||
21 | |||
22 | pub use self::nameres::{ModuleScope, Resolution, Namespace, PerNs}; | ||
23 | |||
24 | /// `Module` is API entry point to get all the information | ||
25 | /// about a particular module. | ||
26 | #[derive(Debug, Clone)] | ||
27 | pub struct Module { | ||
28 | tree: Arc<ModuleTree>, | ||
29 | pub(crate) source_root_id: SourceRootId, | ||
30 | pub(crate) module_id: ModuleId, | ||
31 | } | ||
32 | |||
33 | impl Module { | ||
34 | pub(super) fn new( | ||
35 | db: &impl HirDatabase, | ||
36 | source_root_id: SourceRootId, | ||
37 | module_id: ModuleId, | ||
38 | ) -> Cancelable<Module> { | ||
39 | let module_tree = db.module_tree(source_root_id)?; | ||
40 | let res = Module { | ||
41 | tree: module_tree, | ||
42 | source_root_id, | ||
43 | module_id, | ||
44 | }; | ||
45 | Ok(res) | ||
46 | } | ||
47 | |||
48 | /// Returns `mod foo;` or `mod foo {}` node whihc declared this module. | ||
49 | /// Returns `None` for the root module | ||
50 | pub fn parent_link_source(&self, db: &impl HirDatabase) -> Option<(FileId, ast::ModuleNode)> { | ||
51 | let link = self.module_id.parent_link(&self.tree)?; | ||
52 | let file_id = link | ||
53 | .owner(&self.tree) | ||
54 | .source(&self.tree) | ||
55 | .file_id() | ||
56 | .as_original_file(); | ||
57 | let src = link.bind_source(&self.tree, db); | ||
58 | Some((file_id, src)) | ||
59 | } | ||
60 | |||
61 | pub fn file_id(&self) -> FileId { | ||
62 | self.source().file_id().as_original_file() | ||
63 | } | ||
64 | |||
65 | /// Parent module. Returns `None` if this is a root module. | ||
66 | pub fn parent(&self) -> Option<Module> { | ||
67 | let parent_id = self.module_id.parent(&self.tree)?; | ||
68 | Some(Module { | ||
69 | module_id: parent_id, | ||
70 | ..self.clone() | ||
71 | }) | ||
72 | } | ||
73 | |||
74 | /// Returns an iterator of all children of this module. | ||
75 | pub fn children<'a>(&'a self) -> impl Iterator<Item = (Name, Module)> + 'a { | ||
76 | self.module_id | ||
77 | .children(&self.tree) | ||
78 | .map(move |(name, module_id)| { | ||
79 | ( | ||
80 | name, | ||
81 | Module { | ||
82 | module_id, | ||
83 | ..self.clone() | ||
84 | }, | ||
85 | ) | ||
86 | }) | ||
87 | } | ||
88 | |||
89 | /// Returns the crate this module is part of. | ||
90 | pub fn krate(&self, db: &impl HirDatabase) -> Option<Crate> { | ||
91 | let root_id = self.module_id.crate_root(&self.tree); | ||
92 | let file_id = root_id.source(&self.tree).file_id().as_original_file(); | ||
93 | let crate_graph = db.crate_graph(); | ||
94 | let crate_id = crate_graph.crate_id_for_crate_root(file_id)?; | ||
95 | Some(Crate::new(crate_id)) | ||
96 | } | ||
97 | |||
98 | /// Returns the all modules on the way to the root. | ||
99 | pub fn path_to_root(&self) -> Vec<Module> { | ||
100 | generate(Some(self.clone()), move |it| it.parent()).collect::<Vec<Module>>() | ||
101 | } | ||
102 | |||
103 | /// The root of the tree this module is part of | ||
104 | pub fn crate_root(&self) -> Module { | ||
105 | let root_id = self.module_id.crate_root(&self.tree); | ||
106 | Module { | ||
107 | module_id: root_id, | ||
108 | ..self.clone() | ||
109 | } | ||
110 | } | ||
111 | |||
112 | /// `name` is `None` for the crate's root module | ||
113 | pub fn name(&self) -> Option<&Name> { | ||
114 | let link = self.module_id.parent_link(&self.tree)?; | ||
115 | Some(link.name(&self.tree)) | ||
116 | } | ||
117 | |||
118 | pub fn def_id(&self, db: &impl HirDatabase) -> DefId { | ||
119 | let def_loc = DefLoc { | ||
120 | kind: DefKind::Module, | ||
121 | source_root_id: self.source_root_id, | ||
122 | module_id: self.module_id, | ||
123 | source_item_id: self.module_id.source(&self.tree).0, | ||
124 | }; | ||
125 | def_loc.id(db) | ||
126 | } | ||
127 | |||
128 | /// Finds a child module with the specified name. | ||
129 | pub fn child(&self, name: &Name) -> Option<Module> { | ||
130 | let child_id = self.module_id.child(&self.tree, name)?; | ||
131 | Some(Module { | ||
132 | module_id: child_id, | ||
133 | ..self.clone() | ||
134 | }) | ||
135 | } | ||
136 | |||
137 | /// Returns a `ModuleScope`: a set of items, visible in this module. | ||
138 | pub fn scope(&self, db: &impl HirDatabase) -> Cancelable<ModuleScope> { | ||
139 | let item_map = db.item_map(self.source_root_id)?; | ||
140 | let res = item_map.per_module[&self.module_id].clone(); | ||
141 | Ok(res) | ||
142 | } | ||
143 | |||
144 | pub fn resolve_path(&self, db: &impl HirDatabase, path: &Path) -> Cancelable<PerNs<DefId>> { | ||
145 | let mut curr_per_ns = PerNs::types( | ||
146 | match path.kind { | ||
147 | PathKind::Crate => self.crate_root(), | ||
148 | PathKind::Self_ | PathKind::Plain => self.clone(), | ||
149 | PathKind::Super => { | ||
150 | if let Some(p) = self.parent() { | ||
151 | p | ||
152 | } else { | ||
153 | return Ok(PerNs::none()); | ||
154 | } | ||
155 | } | ||
156 | } | ||
157 | .def_id(db), | ||
158 | ); | ||
159 | |||
160 | let segments = &path.segments; | ||
161 | for name in segments.iter() { | ||
162 | let curr = if let Some(r) = curr_per_ns.as_ref().take(Namespace::Types) { | ||
163 | r | ||
164 | } else { | ||
165 | return Ok(PerNs::none()); | ||
166 | }; | ||
167 | let module = match curr.resolve(db)? { | ||
168 | Def::Module(it) => it, | ||
169 | // TODO here would be the place to handle enum variants... | ||
170 | _ => return Ok(PerNs::none()), | ||
171 | }; | ||
172 | let scope = module.scope(db)?; | ||
173 | curr_per_ns = if let Some(r) = scope.get(&name) { | ||
174 | r.def_id | ||
175 | } else { | ||
176 | return Ok(PerNs::none()); | ||
177 | }; | ||
178 | } | ||
179 | Ok(curr_per_ns) | ||
180 | } | ||
181 | |||
182 | pub fn problems(&self, db: &impl HirDatabase) -> Vec<(SyntaxNode, Problem)> { | ||
183 | self.module_id.problems(&self.tree, db) | ||
184 | } | ||
185 | |||
186 | pub(crate) fn source(&self) -> ModuleSource { | ||
187 | self.module_id.source(&self.tree) | ||
188 | } | ||
189 | } | ||
190 | |||
191 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
192 | pub struct ModuleId(RawId); | ||
193 | impl_arena_id!(ModuleId); | ||
194 | |||
195 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
196 | pub struct LinkId(RawId); | ||
197 | impl_arena_id!(LinkId); | ||
198 | |||
199 | /// Physically, rust source is organized as a set of files, but logically it is | ||
200 | /// organized as a tree of modules. Usually, a single file corresponds to a | ||
201 | /// single module, but it is not nessary the case. | ||
202 | /// | ||
203 | /// Module encapsulate the logic of transitioning from the fuzzy world of files | ||
204 | /// (which can have multiple parents) to the precise world of modules (which | ||
205 | /// always have one parent). | ||
206 | #[derive(Default, Debug, PartialEq, Eq)] | ||
207 | pub struct ModuleTree { | ||
208 | mods: Arena<ModuleId, ModuleData>, | ||
209 | links: Arena<LinkId, LinkData>, | ||
210 | } | ||
211 | |||
212 | impl ModuleTree { | ||
213 | pub(crate) fn modules<'a>(&'a self) -> impl Iterator<Item = ModuleId> + 'a { | ||
214 | self.mods.iter().map(|(id, _)| id) | ||
215 | } | ||
216 | |||
217 | pub(crate) fn modules_with_sources<'a>( | ||
218 | &'a self, | ||
219 | ) -> impl Iterator<Item = (ModuleId, ModuleSource)> + 'a { | ||
220 | self.mods.iter().map(|(id, m)| (id, m.source)) | ||
221 | } | ||
222 | } | ||
223 | |||
224 | /// `ModuleSource` is the syntax tree element that produced this module: | ||
225 | /// either a file, or an inlinde module. | ||
226 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | ||
227 | pub struct ModuleSource(SourceItemId); | ||
228 | |||
229 | /// An owned syntax node for a module. Unlike `ModuleSource`, | ||
230 | /// this holds onto the AST for the whole file. | ||
231 | pub(crate) enum ModuleSourceNode { | ||
232 | SourceFile(ast::SourceFileNode), | ||
233 | Module(ast::ModuleNode), | ||
234 | } | ||
235 | |||
236 | #[derive(Clone, Debug, Hash, PartialEq, Eq)] | ||
237 | pub enum Problem { | ||
238 | UnresolvedModule { | ||
239 | candidate: RelativePathBuf, | ||
240 | }, | ||
241 | NotDirOwner { | ||
242 | move_to: RelativePathBuf, | ||
243 | candidate: RelativePathBuf, | ||
244 | }, | ||
245 | } | ||
246 | |||
247 | impl ModuleId { | ||
248 | pub(crate) fn source(self, tree: &ModuleTree) -> ModuleSource { | ||
249 | tree.mods[self].source | ||
250 | } | ||
251 | fn parent_link(self, tree: &ModuleTree) -> Option<LinkId> { | ||
252 | tree.mods[self].parent | ||
253 | } | ||
254 | fn parent(self, tree: &ModuleTree) -> Option<ModuleId> { | ||
255 | let link = self.parent_link(tree)?; | ||
256 | Some(tree.links[link].owner) | ||
257 | } | ||
258 | fn crate_root(self, tree: &ModuleTree) -> ModuleId { | ||
259 | generate(Some(self), move |it| it.parent(tree)) | ||
260 | .last() | ||
261 | .unwrap() | ||
262 | } | ||
263 | fn child(self, tree: &ModuleTree, name: &Name) -> Option<ModuleId> { | ||
264 | let link = tree.mods[self] | ||
265 | .children | ||
266 | .iter() | ||
267 | .map(|&it| &tree.links[it]) | ||
268 | .find(|it| it.name == *name)?; | ||
269 | Some(*link.points_to.first()?) | ||
270 | } | ||
271 | fn children<'a>(self, tree: &'a ModuleTree) -> impl Iterator<Item = (Name, ModuleId)> + 'a { | ||
272 | tree.mods[self].children.iter().filter_map(move |&it| { | ||
273 | let link = &tree.links[it]; | ||
274 | let module = *link.points_to.first()?; | ||
275 | Some((link.name.clone(), module)) | ||
276 | }) | ||
277 | } | ||
278 | fn problems(self, tree: &ModuleTree, db: &impl HirDatabase) -> Vec<(SyntaxNode, Problem)> { | ||
279 | tree.mods[self] | ||
280 | .children | ||
281 | .iter() | ||
282 | .filter_map(|&it| { | ||
283 | let p = tree.links[it].problem.clone()?; | ||
284 | let s = it.bind_source(tree, db); | ||
285 | let s = s.borrowed().name().unwrap().syntax().owned(); | ||
286 | Some((s, p)) | ||
287 | }) | ||
288 | .collect() | ||
289 | } | ||
290 | } | ||
291 | |||
292 | impl LinkId { | ||
293 | fn owner(self, tree: &ModuleTree) -> ModuleId { | ||
294 | tree.links[self].owner | ||
295 | } | ||
296 | fn name(self, tree: &ModuleTree) -> &Name { | ||
297 | &tree.links[self].name | ||
298 | } | ||
299 | fn bind_source<'a>(self, tree: &ModuleTree, db: &impl HirDatabase) -> ast::ModuleNode { | ||
300 | let owner = self.owner(tree); | ||
301 | match owner.source(tree).resolve(db) { | ||
302 | ModuleSourceNode::SourceFile(root) => { | ||
303 | let ast = imp::modules(root.borrowed()) | ||
304 | .find(|(name, _)| name == &tree.links[self].name) | ||
305 | .unwrap() | ||
306 | .1; | ||
307 | ast.owned() | ||
308 | } | ||
309 | ModuleSourceNode::Module(it) => it, | ||
310 | } | ||
311 | } | ||
312 | } | ||
313 | |||
314 | #[derive(Debug, PartialEq, Eq, Hash)] | ||
315 | pub struct ModuleData { | ||
316 | source: ModuleSource, | ||
317 | parent: Option<LinkId>, | ||
318 | children: Vec<LinkId>, | ||
319 | } | ||
320 | |||
321 | impl ModuleSource { | ||
322 | // precondition: item_id **must** point to module | ||
323 | fn new(file_id: HirFileId, item_id: Option<SourceFileItemId>) -> ModuleSource { | ||
324 | let source_item_id = SourceItemId { file_id, item_id }; | ||
325 | ModuleSource(source_item_id) | ||
326 | } | ||
327 | |||
328 | pub(crate) fn new_file(file_id: HirFileId) -> ModuleSource { | ||
329 | ModuleSource::new(file_id, None) | ||
330 | } | ||
331 | |||
332 | pub(crate) fn new_inline( | ||
333 | db: &impl HirDatabase, | ||
334 | file_id: HirFileId, | ||
335 | m: ast::Module, | ||
336 | ) -> ModuleSource { | ||
337 | assert!(!m.has_semi()); | ||
338 | let file_items = db.file_items(file_id); | ||
339 | let item_id = file_items.id_of(file_id, m.syntax()); | ||
340 | ModuleSource::new(file_id, Some(item_id)) | ||
341 | } | ||
342 | |||
343 | pub(crate) fn file_id(self) -> HirFileId { | ||
344 | self.0.file_id | ||
345 | } | ||
346 | |||
347 | pub(crate) fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode { | ||
348 | let syntax_node = db.file_item(self.0); | ||
349 | let syntax_node = syntax_node.borrowed(); | ||
350 | if let Some(file) = ast::SourceFile::cast(syntax_node) { | ||
351 | return ModuleSourceNode::SourceFile(file.owned()); | ||
352 | } | ||
353 | let module = ast::Module::cast(syntax_node).unwrap(); | ||
354 | ModuleSourceNode::Module(module.owned()) | ||
355 | } | ||
356 | } | ||
357 | |||
358 | #[derive(Hash, Debug, PartialEq, Eq)] | ||
359 | struct LinkData { | ||
360 | owner: ModuleId, | ||
361 | name: Name, | ||
362 | points_to: Vec<ModuleId>, | ||
363 | problem: Option<Problem>, | ||
364 | } | ||
365 | |||
366 | impl ModuleTree { | ||
367 | fn push_mod(&mut self, data: ModuleData) -> ModuleId { | ||
368 | self.mods.alloc(data) | ||
369 | } | ||
370 | fn push_link(&mut self, data: LinkData) -> LinkId { | ||
371 | let owner = data.owner; | ||
372 | let id = self.links.alloc(data); | ||
373 | self.mods[owner].children.push(id); | ||
374 | id | ||
375 | } | ||
376 | } | ||