make PerNs non-generic

This commit is contained in:
Aleksey Kladov 2019-09-13 16:38:59 +03:00
parent 51e2d76b98
commit 2fbe79ed9a
4 changed files with 30 additions and 34 deletions

View File

@ -200,7 +200,7 @@ fn get_legacy_macro(&self, name: &Name) -> Option<MacroDef> {
#[derive(Debug, Clone, PartialEq, Eq, Default)] #[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Resolution { pub struct Resolution {
/// None for unresolved /// None for unresolved
pub def: PerNs<ModuleDef>, pub def: PerNs,
/// ident by which this is imported into local scope. /// ident by which this is imported into local scope.
pub import: Option<ImportId>, pub import: Option<ImportId>,
} }
@ -213,7 +213,7 @@ pub(crate) fn from_macro(macro_: MacroDef) -> Self {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
struct ResolvePathResult { struct ResolvePathResult {
resolved_def: PerNs<ModuleDef>, resolved_def: PerNs,
segment_index: Option<usize>, segment_index: Option<usize>,
reached_fixedpoint: ReachedFixedPoint, reached_fixedpoint: ReachedFixedPoint,
} }
@ -224,7 +224,7 @@ fn empty(reached_fixedpoint: ReachedFixedPoint) -> ResolvePathResult {
} }
fn with( fn with(
resolved_def: PerNs<ModuleDef>, resolved_def: PerNs,
reached_fixedpoint: ReachedFixedPoint, reached_fixedpoint: ReachedFixedPoint,
segment_index: Option<usize>, segment_index: Option<usize>,
) -> ResolvePathResult { ) -> ResolvePathResult {
@ -316,7 +316,7 @@ pub(crate) fn resolve_path(
db: &impl DefDatabase, db: &impl DefDatabase,
original_module: CrateModuleId, original_module: CrateModuleId,
path: &Path, path: &Path,
) -> (PerNs<ModuleDef>, Option<usize>) { ) -> (PerNs, Option<usize>) {
let res = self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path); let res = self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path);
(res.resolved_def, res.segment_index) (res.resolved_def, res.segment_index)
} }
@ -331,7 +331,7 @@ fn resolve_path_fp_with_macro(
path: &Path, path: &Path,
) -> ResolvePathResult { ) -> ResolvePathResult {
let mut segments = path.segments.iter().enumerate(); let mut segments = path.segments.iter().enumerate();
let mut curr_per_ns: PerNs<ModuleDef> = match path.kind { let mut curr_per_ns: PerNs = match path.kind {
PathKind::Crate => { PathKind::Crate => {
PerNs::types(Module { krate: self.krate, module_id: self.root }.into()) PerNs::types(Module { krate: self.krate, module_id: self.root }.into())
} }
@ -456,7 +456,7 @@ fn resolve_path_fp_with_macro(
ResolvePathResult::with(curr_per_ns, ReachedFixedPoint::Yes, None) ResolvePathResult::with(curr_per_ns, ReachedFixedPoint::Yes, None)
} }
fn resolve_name_in_crate_root_or_extern_prelude(&self, name: &Name) -> PerNs<ModuleDef> { fn resolve_name_in_crate_root_or_extern_prelude(&self, name: &Name) -> PerNs {
let from_crate_root = let from_crate_root =
self[self.root].scope.get(name).map_or_else(PerNs::none, |res| res.def); self[self.root].scope.get(name).map_or_else(PerNs::none, |res| res.def);
let from_extern_prelude = self.resolve_name_in_extern_prelude(name); let from_extern_prelude = self.resolve_name_in_extern_prelude(name);
@ -469,7 +469,7 @@ pub(crate) fn resolve_name_in_module(
db: &impl DefDatabase, db: &impl DefDatabase,
module: CrateModuleId, module: CrateModuleId,
name: &Name, name: &Name,
) -> PerNs<ModuleDef> { ) -> PerNs {
// Resolve in: // Resolve in:
// - legacy scope of macro // - legacy scope of macro
// - current module / scope // - current module / scope
@ -485,11 +485,11 @@ pub(crate) fn resolve_name_in_module(
from_legacy_macro.or(from_scope).or(from_extern_prelude).or(from_prelude) from_legacy_macro.or(from_scope).or(from_extern_prelude).or(from_prelude)
} }
fn resolve_name_in_extern_prelude(&self, name: &Name) -> PerNs<ModuleDef> { fn resolve_name_in_extern_prelude(&self, name: &Name) -> PerNs {
self.extern_prelude.get(name).map_or(PerNs::none(), |&it| PerNs::types(it)) self.extern_prelude.get(name).map_or(PerNs::none(), |&it| PerNs::types(it))
} }
fn resolve_in_prelude(&self, db: &impl DefDatabase, name: &Name) -> PerNs<ModuleDef> { fn resolve_in_prelude(&self, db: &impl DefDatabase, name: &Name) -> PerNs {
if let Some(prelude) = self.prelude { if let Some(prelude) = self.prelude {
let keep; let keep;
let def_map = if prelude.krate == self.krate { let def_map = if prelude.krate == self.krate {

View File

@ -243,7 +243,7 @@ fn resolve_import(
&self, &self,
module_id: CrateModuleId, module_id: CrateModuleId,
import: &raw::ImportData, import: &raw::ImportData,
) -> (PerNs<ModuleDef>, ReachedFixedPoint) { ) -> (PerNs, ReachedFixedPoint) {
log::debug!("resolving import: {:?} ({:?})", import, self.def_map.edition); log::debug!("resolving import: {:?} ({:?})", import, self.def_map.edition);
if import.is_extern_crate { if import.is_extern_crate {
let res = self.def_map.resolve_name_in_extern_prelude( let res = self.def_map.resolve_name_in_extern_prelude(
@ -268,7 +268,7 @@ fn resolve_import(
fn record_resolved_import( fn record_resolved_import(
&mut self, &mut self,
module_id: CrateModuleId, module_id: CrateModuleId,
def: PerNs<ModuleDef>, def: PerNs,
import_id: raw::ImportId, import_id: raw::ImportId,
import: &raw::ImportData, import: &raw::ImportData,
) { ) {
@ -662,7 +662,7 @@ macro_rules! def {
}; };
} }
let name = def.name.clone(); let name = def.name.clone();
let def: PerNs<ModuleDef> = match def.kind { let def: PerNs = match def.kind {
raw::DefKind::Function(ast_id) => PerNs::values(def!(Function, ast_id)), raw::DefKind::Function(ast_id) => PerNs::values(def!(Function, ast_id)),
raw::DefKind::Struct(ast_id) => { raw::DefKind::Struct(ast_id) => {
let s = def!(Struct, ast_id); let s = def!(Struct, ast_id);

View File

@ -1,4 +1,4 @@
use crate::MacroDef; use crate::{MacroDef, ModuleDef};
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Namespace { pub enum Namespace {
@ -9,38 +9,38 @@ pub enum Namespace {
} }
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct PerNs<T> { pub struct PerNs {
pub types: Option<T>, pub types: Option<ModuleDef>,
pub values: Option<T>, pub values: Option<ModuleDef>,
/// Since macros has different type, many methods simply ignore it. /// Since macros has different type, many methods simply ignore it.
/// We can only use special method like `get_macros` to access it. /// We can only use special method like `get_macros` to access it.
pub macros: Option<MacroDef>, pub macros: Option<MacroDef>,
} }
impl<T> Default for PerNs<T> { impl Default for PerNs {
fn default() -> Self { fn default() -> Self {
PerNs { types: None, values: None, macros: None } PerNs { types: None, values: None, macros: None }
} }
} }
impl<T> PerNs<T> { impl PerNs {
pub fn none() -> PerNs<T> { pub fn none() -> PerNs {
PerNs { types: None, values: None, macros: None } PerNs { types: None, values: None, macros: None }
} }
pub fn values(t: T) -> PerNs<T> { pub fn values(t: ModuleDef) -> PerNs {
PerNs { types: None, values: Some(t), macros: None } PerNs { types: None, values: Some(t), macros: None }
} }
pub fn types(t: T) -> PerNs<T> { pub fn types(t: ModuleDef) -> PerNs {
PerNs { types: Some(t), values: None, macros: None } PerNs { types: Some(t), values: None, macros: None }
} }
pub fn both(types: T, values: T) -> PerNs<T> { pub fn both(types: ModuleDef, values: ModuleDef) -> PerNs {
PerNs { types: Some(types), values: Some(values), macros: None } PerNs { types: Some(types), values: Some(values), macros: None }
} }
pub fn macros(macro_: MacroDef) -> PerNs<T> { pub fn macros(macro_: MacroDef) -> PerNs {
PerNs { types: None, values: None, macros: Some(macro_) } PerNs { types: None, values: None, macros: Some(macro_) }
} }
@ -52,11 +52,11 @@ pub fn is_all(&self) -> bool {
self.types.is_some() && self.values.is_some() && self.macros.is_some() self.types.is_some() && self.values.is_some() && self.macros.is_some()
} }
pub fn take_types(self) -> Option<T> { pub fn take_types(self) -> Option<ModuleDef> {
self.types self.types
} }
pub fn take_values(self) -> Option<T> { pub fn take_values(self) -> Option<ModuleDef> {
self.values self.values
} }
@ -64,11 +64,11 @@ pub fn get_macros(&self) -> Option<MacroDef> {
self.macros self.macros
} }
pub fn only_macros(&self) -> PerNs<T> { pub fn only_macros(&self) -> PerNs {
PerNs { types: None, values: None, macros: self.macros } PerNs { types: None, values: None, macros: self.macros }
} }
pub fn or(self, other: PerNs<T>) -> PerNs<T> { pub fn or(self, other: PerNs) -> PerNs {
PerNs { PerNs {
types: self.types.or(other.types), types: self.types.or(other.types),
values: self.values.or(other.values), values: self.values.or(other.values),

View File

@ -112,11 +112,7 @@ pub(crate) fn resolve_known_enum(&self, db: &impl HirDatabase, path: &Path) -> O
} }
/// pub only for source-binder /// pub only for source-binder
pub(crate) fn resolve_module_path( pub(crate) fn resolve_module_path(&self, db: &impl HirDatabase, path: &Path) -> PerNs {
&self,
db: &impl HirDatabase,
path: &Path,
) -> PerNs<ModuleDef> {
let (item_map, module) = match self.module() { let (item_map, module) = match self.module() {
Some(it) => it, Some(it) => it,
None => return PerNs::none(), None => return PerNs::none(),
@ -385,8 +381,8 @@ pub enum ScopeDef {
Unknown, Unknown,
} }
impl From<PerNs<ModuleDef>> for ScopeDef { impl From<PerNs> for ScopeDef {
fn from(def: PerNs<ModuleDef>) -> Self { fn from(def: PerNs) -> Self {
def.take_types() def.take_types()
.or_else(|| def.take_values()) .or_else(|| def.take_values())
.map(ScopeDef::ModuleDef) .map(ScopeDef::ModuleDef)