mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 04:19:13 +00:00
Sketching the resolver API
This commit is contained in:
parent
2b5c226e86
commit
5208c2aa93
8 changed files with 134 additions and 11 deletions
100
crates/ra_hir/src/resolve.rs
Normal file
100
crates/ra_hir/src/resolve.rs
Normal file
|
@ -0,0 +1,100 @@
|
|||
#![allow(unused_variables, dead_code)]
|
||||
//! Name resolution.
|
||||
use std::sync::Arc;
|
||||
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
use crate::{
|
||||
ModuleDef,
|
||||
name::Name,
|
||||
nameres::{PerNs, lower::ImportId, ItemMap},
|
||||
module_tree::ModuleId,
|
||||
generics::GenericParams,
|
||||
expr::{Body, scope::{ExprScopes, ScopeId}, PatId},
|
||||
impl_block::ImplBlock,
|
||||
path::Path,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Resolver {
|
||||
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
|
||||
}
|
||||
|
||||
// TODO how to store these best
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct ModuleItemMap {
|
||||
item_map: Arc<ItemMap>,
|
||||
module_id: ModuleId,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) struct ExprScope {
|
||||
expr_scopes: Arc<ExprScopes>,
|
||||
scope_id: ScopeId,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) enum Scope {
|
||||
/// All the items and imported names of a module
|
||||
ModuleScope(ModuleItemMap),
|
||||
/// Brings the generic parameters of an item into scope
|
||||
GenericParams(Arc<GenericParams>),
|
||||
/// Brings the function parameters into scope
|
||||
FunctionParams(Arc<Body>),
|
||||
/// Brings `Self` into scope
|
||||
ImplBlockScope(ImplBlock),
|
||||
/// Local bindings
|
||||
ExprScope(ExprScope),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Resolution {
|
||||
/// An item
|
||||
Def {
|
||||
def: ModuleDef,
|
||||
import: Option<ImportId>,
|
||||
},
|
||||
/// A local binding (only value namespace)
|
||||
LocalBinding { pat: PatId },
|
||||
/// A generic parameter
|
||||
GenericParam { idx: u32 },
|
||||
// TODO how does `Self` resolve?
|
||||
}
|
||||
|
||||
impl Resolver {
|
||||
pub fn resolve_name(&self, name: &Name) -> PerNs<Resolution> {
|
||||
for scope in self.scopes.iter().rev() {
|
||||
let resolution = scope.resolve_name(name);
|
||||
if !resolution.is_none() {
|
||||
return resolution;
|
||||
}
|
||||
}
|
||||
PerNs::none()
|
||||
}
|
||||
|
||||
pub fn resolve_path(&self, path: &Path) -> PerNs<Resolution> {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
pub fn all_names(&self) -> FxHashMap<Name, Resolution> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
impl Resolver {
|
||||
pub(crate) fn push_scope(mut self, scope: Scope) -> Resolver {
|
||||
self.scopes.push(scope);
|
||||
self
|
||||
}
|
||||
|
||||
pub(crate) fn pop_scope(mut self) -> Resolver {
|
||||
self.scopes.pop();
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Scope {
|
||||
fn resolve_name(&self, name: &Name) -> PerNs<Resolution> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue