From dd5c2dc5bf9e9dee863bd79105b1782b654221f7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 16:49:05 +0300 Subject: Move ImportId --- crates/ra_hir_def/src/nameres/collector.rs | 14 +++++++------- crates/ra_hir_def/src/nameres/raw.rs | 20 ++++++++------------ 2 files changed, 15 insertions(+), 19 deletions(-) (limited to 'crates/ra_hir_def/src/nameres') diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 1894b072a..4a671b8f3 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -20,8 +20,8 @@ use crate::{ }, path::{Path, PathKind}, AdtId, AstId, AstItemDef, ConstLoc, ContainerId, CrateModuleId, EnumId, EnumVariantId, - FunctionLoc, ImplId, Intern, LocationCtx, ModuleDefId, ModuleId, StaticId, StructId, - StructOrUnionId, TraitId, TypeAliasLoc, UnionId, + FunctionLoc, ImplId, Intern, LocalImportId, LocationCtx, ModuleDefId, ModuleId, StaticId, + StructId, StructOrUnionId, TraitId, TypeAliasLoc, UnionId, }; pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { @@ -94,8 +94,8 @@ impl MacroStackMonitor { struct DefCollector<'a, DB> { db: &'a DB, def_map: CrateDefMap, - glob_imports: FxHashMap>, - unresolved_imports: Vec<(CrateModuleId, raw::ImportId, raw::ImportData)>, + glob_imports: FxHashMap>, + unresolved_imports: Vec<(CrateModuleId, LocalImportId, raw::ImportData)>, unexpanded_macros: Vec<(CrateModuleId, AstId, Path)>, mod_dirs: FxHashMap, @@ -293,7 +293,7 @@ where &mut self, module_id: CrateModuleId, def: PerNs, - import_id: raw::ImportId, + import_id: LocalImportId, import: &raw::ImportData, ) { if import.is_glob { @@ -388,7 +388,7 @@ where fn update( &mut self, module_id: CrateModuleId, - import: Option, + import: Option, resolutions: &[(Name, Resolution)], ) { self.update_recursive(module_id, import, resolutions, 0) @@ -397,7 +397,7 @@ where fn update_recursive( &mut self, module_id: CrateModuleId, - import: Option, + import: Option, resolutions: &[(Name, Resolution)], depth: usize, ) { diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 7618cb059..8ee6f54cd 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -20,7 +20,7 @@ use crate::{ attr::{Attr, Attrs}, db::DefDatabase, path::Path, - FileAstId, HirFileId, ModuleSource, Source, + FileAstId, HirFileId, LocalImportId, ModuleSource, Source, }; /// `RawItems` is a set of top-level items in a file (except for impls). @@ -30,7 +30,7 @@ use crate::{ #[derive(Debug, Default, PartialEq, Eq)] pub struct RawItems { modules: Arena, - imports: Arena, + imports: Arena, defs: Arena, macros: Arena, impls: Arena, @@ -40,7 +40,7 @@ pub struct RawItems { #[derive(Debug, Default, PartialEq, Eq)] pub struct ImportSourceMap { - map: ArenaMap, + map: ArenaMap, } type ImportSourcePtr = Either, AstPtr>; @@ -51,11 +51,11 @@ fn to_node(ptr: ImportSourcePtr, file: &SourceFile) -> ImportSource { } impl ImportSourceMap { - fn insert(&mut self, import: ImportId, ptr: ImportSourcePtr) { + fn insert(&mut self, import: LocalImportId, ptr: ImportSourcePtr) { self.map.insert(import, ptr) } - pub fn get(&self, source: &ModuleSource, import: ImportId) -> ImportSource { + pub fn get(&self, source: &ModuleSource, import: LocalImportId) -> ImportSource { let file = match source { ModuleSource::SourceFile(file) => file.clone(), ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(), @@ -106,9 +106,9 @@ impl Index for RawItems { } } -impl Index for RawItems { +impl Index for RawItems { type Output = ImportData; - fn index(&self, idx: ImportId) -> &ImportData { + fn index(&self, idx: LocalImportId) -> &ImportData { &self.imports[idx] } } @@ -143,7 +143,7 @@ pub(super) struct RawItem { #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub(super) enum RawItemKind { Module(Module), - Import(ImportId), + Import(LocalImportId), Def(Def), Macro(Macro), Impl(Impl), @@ -159,10 +159,6 @@ pub(super) enum ModuleData { Definition { name: Name, ast_id: FileAstId, items: Vec }, } -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct ImportId(RawId); -impl_arena_id!(ImportId); - #[derive(Debug, Clone, PartialEq, Eq)] pub struct ImportData { pub(super) path: Path, -- cgit v1.2.3 From 158b1cb524d8e07aa7a6ec2342bca2ce4667d316 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 16:49:53 +0300 Subject: Rename CrateModuleId --- crates/ra_hir_def/src/nameres/collector.rs | 40 +++++++++++----------- crates/ra_hir_def/src/nameres/path_resolution.rs | 6 ++-- crates/ra_hir_def/src/nameres/tests.rs | 4 +-- .../ra_hir_def/src/nameres/tests/mod_resolution.rs | 2 +- 4 files changed, 26 insertions(+), 26 deletions(-) (limited to 'crates/ra_hir_def/src/nameres') diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 4a671b8f3..5f7697f63 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -19,9 +19,9 @@ use crate::{ per_ns::PerNs, raw, CrateDefMap, ModuleData, Resolution, ResolveMode, }, path::{Path, PathKind}, - AdtId, AstId, AstItemDef, ConstLoc, ContainerId, CrateModuleId, EnumId, EnumVariantId, - FunctionLoc, ImplId, Intern, LocalImportId, LocationCtx, ModuleDefId, ModuleId, StaticId, - StructId, StructOrUnionId, TraitId, TypeAliasLoc, UnionId, + AdtId, AstId, AstItemDef, ConstLoc, ContainerId, EnumId, EnumVariantId, FunctionLoc, ImplId, + Intern, LocalImportId, LocalModuleId, LocationCtx, ModuleDefId, ModuleId, StaticId, StructId, + StructOrUnionId, TraitId, TypeAliasLoc, UnionId, }; pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { @@ -94,10 +94,10 @@ impl MacroStackMonitor { struct DefCollector<'a, DB> { db: &'a DB, def_map: CrateDefMap, - glob_imports: FxHashMap>, - unresolved_imports: Vec<(CrateModuleId, LocalImportId, raw::ImportData)>, - unexpanded_macros: Vec<(CrateModuleId, AstId, Path)>, - mod_dirs: FxHashMap, + glob_imports: FxHashMap>, + unresolved_imports: Vec<(LocalModuleId, LocalImportId, raw::ImportData)>, + unexpanded_macros: Vec<(LocalModuleId, AstId, Path)>, + mod_dirs: FxHashMap, /// Some macro use `$tt:tt which mean we have to handle the macro perfectly /// To prevent stack overflow, we add a deep counter here for prevent that. @@ -173,7 +173,7 @@ where /// ``` fn define_macro( &mut self, - module_id: CrateModuleId, + module_id: LocalModuleId, name: Name, macro_: MacroDefId, export: bool, @@ -200,7 +200,7 @@ where /// the definition of current module. /// And also, `macro_use` on a module will import all legacy macros visable inside to /// current legacy scope, with possible shadowing. - fn define_legacy_macro(&mut self, module_id: CrateModuleId, name: Name, macro_: MacroDefId) { + fn define_legacy_macro(&mut self, module_id: LocalModuleId, name: Name, macro_: MacroDefId) { // Always shadowing self.def_map.modules[module_id].scope.legacy_macros.insert(name, macro_); } @@ -208,7 +208,7 @@ where /// Import macros from `#[macro_use] extern crate`. fn import_macros_from_extern_crate( &mut self, - current_module_id: CrateModuleId, + current_module_id: LocalModuleId, import: &raw::ImportData, ) { log::debug!( @@ -235,7 +235,7 @@ where /// Exported macros are just all macros in the root module scope. /// Note that it contains not only all `#[macro_export]` macros, but also all aliases /// created by `use` in the root module, ignoring the visibility of `use`. - fn import_all_macros_exported(&mut self, current_module_id: CrateModuleId, krate: CrateId) { + fn import_all_macros_exported(&mut self, current_module_id: LocalModuleId, krate: CrateId) { let def_map = self.db.crate_def_map(krate); for (name, def) in def_map[def_map.root].scope.macros() { // `macro_use` only bring things into legacy scope. @@ -265,7 +265,7 @@ where fn resolve_import( &self, - module_id: CrateModuleId, + module_id: LocalModuleId, import: &raw::ImportData, ) -> (PerNs, ReachedFixedPoint) { log::debug!("resolving import: {:?} ({:?})", import, self.def_map.edition); @@ -291,7 +291,7 @@ where fn record_resolved_import( &mut self, - module_id: CrateModuleId, + module_id: LocalModuleId, def: PerNs, import_id: LocalImportId, import: &raw::ImportData, @@ -387,7 +387,7 @@ where fn update( &mut self, - module_id: CrateModuleId, + module_id: LocalModuleId, import: Option, resolutions: &[(Name, Resolution)], ) { @@ -396,7 +396,7 @@ where fn update_recursive( &mut self, - module_id: CrateModuleId, + module_id: LocalModuleId, import: Option, resolutions: &[(Name, Resolution)], depth: usize, @@ -484,7 +484,7 @@ where fn collect_macro_expansion( &mut self, - module_id: CrateModuleId, + module_id: LocalModuleId, macro_call_id: MacroCallId, macro_def_id: MacroDefId, ) { @@ -522,7 +522,7 @@ where /// Walks a single module, populating defs, imports and macros struct ModCollector<'a, D> { def_collector: D, - module_id: CrateModuleId, + module_id: LocalModuleId, file_id: HirFileId, raw_items: &'a raw::RawItems, mod_dir: ModDir, @@ -647,7 +647,7 @@ where name: Name, declaration: AstId, definition: Option, - ) -> CrateModuleId { + ) -> LocalModuleId { let modules = &mut self.def_collector.def_map.modules; let res = modules.alloc(ModuleData::default()); modules[res].parent = Some(self.module_id); @@ -772,7 +772,7 @@ where self.def_collector.unexpanded_macros.push((self.module_id, ast_id, path)); } - fn import_all_legacy_macros(&mut self, module_id: CrateModuleId) { + fn import_all_legacy_macros(&mut self, module_id: LocalModuleId) { let macros = self.def_collector.def_map[module_id].scope.legacy_macros.clone(); for (name, macro_) in macros { self.def_collector.define_legacy_macro(self.module_id, name.clone(), macro_); @@ -827,7 +827,7 @@ mod tests { let def_map = { let edition = db.crate_graph().edition(krate); - let mut modules: Arena = Arena::default(); + let mut modules: Arena = Arena::default(); let root = modules.alloc(ModuleData::default()); CrateDefMap { krate, diff --git a/crates/ra_hir_def/src/nameres/path_resolution.rs b/crates/ra_hir_def/src/nameres/path_resolution.rs index 102009ac7..93b441f96 100644 --- a/crates/ra_hir_def/src/nameres/path_resolution.rs +++ b/crates/ra_hir_def/src/nameres/path_resolution.rs @@ -18,7 +18,7 @@ use crate::{ db::DefDatabase, nameres::{per_ns::PerNs, CrateDefMap}, path::{Path, PathKind}, - AdtId, CrateModuleId, EnumVariantId, ModuleDefId, ModuleId, + AdtId, EnumVariantId, LocalModuleId, ModuleDefId, ModuleId, }; #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -65,7 +65,7 @@ impl CrateDefMap { &self, db: &impl DefDatabase, mode: ResolveMode, - original_module: CrateModuleId, + original_module: LocalModuleId, path: &Path, ) -> ResolvePathResult { let mut segments = path.segments.iter().enumerate(); @@ -217,7 +217,7 @@ impl CrateDefMap { fn resolve_name_in_module( &self, db: &impl DefDatabase, - module: CrateModuleId, + module: LocalModuleId, name: &Name, ) -> PerNs { // Resolve in: diff --git a/crates/ra_hir_def/src/nameres/tests.rs b/crates/ra_hir_def/src/nameres/tests.rs index b5053ba20..f0b86af7c 100644 --- a/crates/ra_hir_def/src/nameres/tests.rs +++ b/crates/ra_hir_def/src/nameres/tests.rs @@ -10,7 +10,7 @@ use insta::assert_snapshot; use ra_db::{fixture::WithFixture, SourceDatabase}; use test_utils::covers; -use crate::{db::DefDatabase, nameres::*, test_db::TestDB, CrateModuleId}; +use crate::{db::DefDatabase, nameres::*, test_db::TestDB, LocalModuleId}; fn def_map(fixtute: &str) -> String { let dm = compute_crate_def_map(fixtute); @@ -28,7 +28,7 @@ fn render_crate_def_map(map: &CrateDefMap) -> String { go(&mut buf, map, "\ncrate", map.root()); return buf.trim().to_string(); - fn go(buf: &mut String, map: &CrateDefMap, path: &str, module: CrateModuleId) { + fn go(buf: &mut String, map: &CrateDefMap, path: &str, module: LocalModuleId) { *buf += path; *buf += "\n"; diff --git a/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs b/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs index eb7b85c07..e11530062 100644 --- a/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs +++ b/crates/ra_hir_def/src/nameres/tests/mod_resolution.rs @@ -665,7 +665,7 @@ fn unresolved_module_diagnostics() { @r###" [ UnresolvedModule { - module: CrateModuleId( + module: LocalModuleId( 0, ), declaration: AstId { -- cgit v1.2.3 From 6bdd5fa461ba0f3f3697339ffb560c577e3b0cc6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 16:53:16 +0300 Subject: Privatise nameres --- crates/ra_hir_def/src/nameres/collector.rs | 3 +- crates/ra_hir_def/src/nameres/path_resolution.rs | 3 +- crates/ra_hir_def/src/nameres/per_ns.rs | 74 ------------------------ 3 files changed, 4 insertions(+), 76 deletions(-) delete mode 100644 crates/ra_hir_def/src/nameres/per_ns.rs (limited to 'crates/ra_hir_def/src/nameres') diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 5f7697f63..b02364e86 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs @@ -16,9 +16,10 @@ use crate::{ db::DefDatabase, nameres::{ diagnostics::DefDiagnostic, mod_resolution::ModDir, path_resolution::ReachedFixedPoint, - per_ns::PerNs, raw, CrateDefMap, ModuleData, Resolution, ResolveMode, + raw, CrateDefMap, ModuleData, Resolution, ResolveMode, }, path::{Path, PathKind}, + per_ns::PerNs, AdtId, AstId, AstItemDef, ConstLoc, ContainerId, EnumId, EnumVariantId, FunctionLoc, ImplId, Intern, LocalImportId, LocalModuleId, LocationCtx, ModuleDefId, ModuleId, StaticId, StructId, StructOrUnionId, TraitId, TypeAliasLoc, UnionId, diff --git a/crates/ra_hir_def/src/nameres/path_resolution.rs b/crates/ra_hir_def/src/nameres/path_resolution.rs index 93b441f96..9455f22bb 100644 --- a/crates/ra_hir_def/src/nameres/path_resolution.rs +++ b/crates/ra_hir_def/src/nameres/path_resolution.rs @@ -16,8 +16,9 @@ use test_utils::tested_by; use crate::{ db::DefDatabase, - nameres::{per_ns::PerNs, CrateDefMap}, + nameres::CrateDefMap, path::{Path, PathKind}, + per_ns::PerNs, AdtId, EnumVariantId, LocalModuleId, ModuleDefId, ModuleId, }; diff --git a/crates/ra_hir_def/src/nameres/per_ns.rs b/crates/ra_hir_def/src/nameres/per_ns.rs deleted file mode 100644 index 717ed1ef9..000000000 --- a/crates/ra_hir_def/src/nameres/per_ns.rs +++ /dev/null @@ -1,74 +0,0 @@ -//! FIXME: write short doc here - -use hir_expand::MacroDefId; - -use crate::ModuleDefId; - -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] -pub struct PerNs { - pub types: Option, - pub values: Option, - /// Since macros has different type, many methods simply ignore it. - /// We can only use special method like `get_macros` to access it. - pub macros: Option, -} - -impl Default for PerNs { - fn default() -> Self { - PerNs { types: None, values: None, macros: None } - } -} - -impl PerNs { - pub fn none() -> PerNs { - PerNs { types: None, values: None, macros: None } - } - - pub fn values(t: ModuleDefId) -> PerNs { - PerNs { types: None, values: Some(t), macros: None } - } - - pub fn types(t: ModuleDefId) -> PerNs { - PerNs { types: Some(t), values: None, macros: None } - } - - pub fn both(types: ModuleDefId, values: ModuleDefId) -> PerNs { - PerNs { types: Some(types), values: Some(values), macros: None } - } - - pub fn macros(macro_: MacroDefId) -> PerNs { - PerNs { types: None, values: None, macros: Some(macro_) } - } - - pub fn is_none(&self) -> bool { - self.types.is_none() && self.values.is_none() && self.macros.is_none() - } - - pub fn is_all(&self) -> bool { - self.types.is_some() && self.values.is_some() && self.macros.is_some() - } - - pub fn take_types(self) -> Option { - self.types - } - - pub fn take_values(self) -> Option { - self.values - } - - pub fn get_macros(&self) -> Option { - self.macros - } - - pub fn only_macros(&self) -> PerNs { - PerNs { types: None, values: None, macros: self.macros } - } - - pub fn or(self, other: PerNs) -> PerNs { - PerNs { - types: self.types.or(other.types), - values: self.values.or(other.values), - macros: self.macros.or(other.macros), - } - } -} -- cgit v1.2.3 From ffc2325d194d2523456484a7dec1f175c729c1b5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 17:06:04 +0300 Subject: Move ModuleSource back to hir --- crates/ra_hir_def/src/nameres/raw.rs | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) (limited to 'crates/ra_hir_def/src/nameres') diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 8ee6f54cd..552cbe544 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -12,7 +12,7 @@ use hir_expand::{ use ra_arena::{impl_arena_id, map::ArenaMap, Arena, RawId}; use ra_syntax::{ ast::{self, AttrsOwner, NameOwner}, - AstNode, AstPtr, SourceFile, + AstNode, AstPtr, }; use test_utils::tested_by; @@ -20,7 +20,7 @@ use crate::{ attr::{Attr, Attrs}, db::DefDatabase, path::Path, - FileAstId, HirFileId, LocalImportId, ModuleSource, Source, + FileAstId, HirFileId, LocalImportId, Source, }; /// `RawItems` is a set of top-level items in a file (except for impls). @@ -44,24 +44,14 @@ pub struct ImportSourceMap { } type ImportSourcePtr = Either, AstPtr>; -type ImportSource = Either; - -fn to_node(ptr: ImportSourcePtr, file: &SourceFile) -> ImportSource { - ptr.map(|ptr| ptr.to_node(file.syntax()), |ptr| ptr.to_node(file.syntax())) -} impl ImportSourceMap { fn insert(&mut self, import: LocalImportId, ptr: ImportSourcePtr) { self.map.insert(import, ptr) } - pub fn get(&self, source: &ModuleSource, import: LocalImportId) -> ImportSource { - let file = match source { - ModuleSource::SourceFile(file) => file.clone(), - ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(), - }; - - to_node(self.map[import], &file) + pub fn get(&self, import: LocalImportId) -> ImportSourcePtr { + self.map[import].clone() } } -- cgit v1.2.3