diff options
Diffstat (limited to 'crates/ra_hir/src/resolve.rs')
-rw-r--r-- | crates/ra_hir/src/resolve.rs | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs new file mode 100644 index 000000000..41fcb35bc --- /dev/null +++ b/crates/ra_hir/src/resolve.rs | |||
@@ -0,0 +1,100 @@ | |||
1 | #![allow(unused_variables, dead_code)] | ||
2 | //! Name resolution. | ||
3 | use std::sync::Arc; | ||
4 | |||
5 | use rustc_hash::FxHashMap; | ||
6 | |||
7 | use crate::{ | ||
8 | ModuleDef, | ||
9 | name::Name, | ||
10 | nameres::{PerNs, lower::ImportId, ItemMap}, | ||
11 | module_tree::ModuleId, | ||
12 | generics::GenericParams, | ||
13 | expr::{Body, scope::{ExprScopes, ScopeId}, PatId}, | ||
14 | impl_block::ImplBlock, | ||
15 | path::Path, | ||
16 | }; | ||
17 | |||
18 | #[derive(Debug, Clone)] | ||
19 | pub struct Resolver { | ||
20 | scopes: Vec<Scope>, // maybe a 'linked list' of scopes? or allow linking a Resolver to a parent Resolver? that's an optimization that might not be necessary, though | ||
21 | } | ||
22 | |||
23 | // TODO how to store these best | ||
24 | #[derive(Debug, Clone)] | ||
25 | pub(crate) struct ModuleItemMap { | ||
26 | item_map: Arc<ItemMap>, | ||
27 | module_id: ModuleId, | ||
28 | } | ||
29 | |||
30 | #[derive(Debug, Clone)] | ||
31 | pub(crate) struct ExprScope { | ||
32 | expr_scopes: Arc<ExprScopes>, | ||
33 | scope_id: ScopeId, | ||
34 | } | ||
35 | |||
36 | #[derive(Debug, Clone)] | ||
37 | pub(crate) enum Scope { | ||
38 | /// All the items and imported names of a module | ||
39 | ModuleScope(ModuleItemMap), | ||
40 | /// Brings the generic parameters of an item into scope | ||
41 | GenericParams(Arc<GenericParams>), | ||
42 | /// Brings the function parameters into scope | ||
43 | FunctionParams(Arc<Body>), | ||
44 | /// Brings `Self` into scope | ||
45 | ImplBlockScope(ImplBlock), | ||
46 | /// Local bindings | ||
47 | ExprScope(ExprScope), | ||
48 | } | ||
49 | |||
50 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
51 | pub enum Resolution { | ||
52 | /// An item | ||
53 | Def { | ||
54 | def: ModuleDef, | ||
55 | import: Option<ImportId>, | ||
56 | }, | ||
57 | /// A local binding (only value namespace) | ||
58 | LocalBinding { pat: PatId }, | ||
59 | /// A generic parameter | ||
60 | GenericParam { idx: u32 }, | ||
61 | // TODO how does `Self` resolve? | ||
62 | } | ||
63 | |||
64 | impl Resolver { | ||
65 | pub fn resolve_name(&self, name: &Name) -> PerNs<Resolution> { | ||
66 | for scope in self.scopes.iter().rev() { | ||
67 | let resolution = scope.resolve_name(name); | ||
68 | if !resolution.is_none() { | ||
69 | return resolution; | ||
70 | } | ||
71 | } | ||
72 | PerNs::none() | ||
73 | } | ||
74 | |||
75 | pub fn resolve_path(&self, path: &Path) -> PerNs<Resolution> { | ||
76 | unimplemented!() | ||
77 | } | ||
78 | |||
79 | pub fn all_names(&self) -> FxHashMap<Name, Resolution> { | ||
80 | unimplemented!() | ||
81 | } | ||
82 | } | ||
83 | |||
84 | impl Resolver { | ||
85 | pub(crate) fn push_scope(mut self, scope: Scope) -> Resolver { | ||
86 | self.scopes.push(scope); | ||
87 | self | ||
88 | } | ||
89 | |||
90 | pub(crate) fn pop_scope(mut self) -> Resolver { | ||
91 | self.scopes.pop(); | ||
92 | self | ||
93 | } | ||
94 | } | ||
95 | |||
96 | impl Scope { | ||
97 | fn resolve_name(&self, name: &Name) -> PerNs<Resolution> { | ||
98 | unimplemented!() | ||
99 | } | ||
100 | } | ||