rust-analyzer/crates/ra_hir/src/resolve.rs

101 lines
2.4 KiB
Rust
Raw Normal View History

2019-01-19 20:23:26 +00:00
#![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!()
}
}