From 5270bca5f72fa65f0515be776e06d3d6a4d1efca Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 26 Mar 2019 13:09:39 +0300 Subject: store macro def inside macro id This solves the problem of "macro expansion can't call into name resolution, because name resolution calls back into macro expansion" Because we store macro def as a part of call id, macro expansion just knows the def! --- crates/ra_hir/src/nameres/collector.rs | 93 ++++++++++++---------------------- 1 file changed, 31 insertions(+), 62 deletions(-) (limited to 'crates/ra_hir/src/nameres/collector.rs') diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs index 8830b4624..89300d2ec 100644 --- a/crates/ra_hir/src/nameres/collector.rs +++ b/crates/ra_hir/src/nameres/collector.rs @@ -6,15 +6,15 @@ use ra_db::FileId; use crate::{ Function, Module, Struct, Enum, Const, Static, Trait, TypeAlias, - DefDatabase, HirFileId, Name, Path, Crate, + DefDatabase, HirFileId, Name, Path, KnownName, nameres::{ Resolution, PerNs, ModuleDef, ReachedFixedPoint, ResolveMode, - CrateDefMap, CrateModuleId, ModuleData, CrateMacroId, + CrateDefMap, CrateModuleId, ModuleData, diagnostics::DefDiagnostic, raw, }, - ids::{AstItemDef, LocationCtx, MacroCallLoc, SourceItemId, MacroCallId}, + ids::{AstItemDef, LocationCtx, MacroCallLoc, SourceItemId, MacroCallId, MacroDefId}, }; pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { @@ -51,8 +51,8 @@ struct DefCollector { def_map: CrateDefMap, glob_imports: FxHashMap>, unresolved_imports: Vec<(CrateModuleId, raw::ImportId, raw::ImportData)>, - unexpanded_macros: Vec<(CrateModuleId, MacroCallId, Path, tt::Subtree)>, - global_macro_scope: FxHashMap, + unexpanded_macros: Vec<(CrateModuleId, SourceItemId, Path)>, + global_macro_scope: FxHashMap, } impl<'a, DB> DefCollector<&'a DB> @@ -62,7 +62,7 @@ where fn collect(&mut self) { let crate_graph = self.db.crate_graph(); let file_id = crate_graph.crate_root(self.def_map.krate.crate_id()); - let raw_items = self.db.raw_items(file_id); + let raw_items = self.db.raw_items(file_id.into()); let module_id = self.def_map.root; self.def_map.modules[module_id].definition = Some(file_id); ModCollector { @@ -93,14 +93,11 @@ where } } - fn define_macro(&mut self, name: Name, tt: &tt::Subtree, export: bool) { - if let Ok(rules) = mbe::MacroRules::parse(tt) { - let macro_id = self.def_map.macros.alloc(rules); - if export { - self.def_map.public_macros.insert(name.clone(), macro_id); - } - self.global_macro_scope.insert(name, macro_id); + fn define_macro(&mut self, name: Name, macro_id: MacroDefId, export: bool) { + if export { + self.def_map.public_macros.insert(name.clone(), macro_id); } + self.global_macro_scope.insert(name, macro_id); } fn resolve_imports(&mut self) -> ReachedFixedPoint { @@ -296,7 +293,7 @@ where let mut macros = std::mem::replace(&mut self.unexpanded_macros, Vec::new()); let mut resolved = Vec::new(); let mut res = ReachedFixedPoint::Yes; - macros.retain(|(module_id, call_id, path, tt)| { + macros.retain(|(module_id, source_item_id, path)| { if path.segments.len() != 2 { return true; } @@ -312,47 +309,24 @@ where res = ReachedFixedPoint::No; let def_map = self.db.crate_def_map(krate); if let Some(macro_id) = def_map.public_macros.get(&path.segments[1].name).cloned() { - resolved.push((*module_id, *call_id, (krate, macro_id), tt.clone())); + let call_id = + MacroCallLoc { def: macro_id, source_item_id: *source_item_id }.id(self.db); + resolved.push((*module_id, call_id)); } false }); - for (module_id, macro_call_id, macro_def_id, arg) in resolved { - self.collect_macro_expansion(module_id, macro_call_id, macro_def_id, arg); + for (module_id, macro_call_id) in resolved { + self.collect_macro_expansion(module_id, macro_call_id); } res } - fn collect_macro_expansion( - &mut self, - module_id: CrateModuleId, - macro_call_id: MacroCallId, - macro_def_id: (Crate, CrateMacroId), - macro_arg: tt::Subtree, - ) { - let (macro_krate, macro_id) = macro_def_id; - let dm; - let rules = if macro_krate == self.def_map.krate { - &self.def_map[macro_id] - } else { - dm = self.db.crate_def_map(macro_krate); - &dm[macro_id] - }; - if let Ok(expansion) = rules.expand(¯o_arg) { - self.def_map.macro_resolutions.insert(macro_call_id, macro_def_id); - // XXX: this **does not** go through a database, because we can't - // identify macro_call without adding the whole state of name resolution - // as a parameter to the query. - // - // So, we run the queries "manually" and we must ensure that - // `db.hir_parse(macro_call_id)` returns the same source_file. - let file_id: HirFileId = macro_call_id.into(); - let source_file = mbe::token_tree_to_ast_item_list(&expansion); - - let raw_items = raw::RawItems::from_source_file(&source_file, file_id); - ModCollector { def_collector: &mut *self, file_id, module_id, raw_items: &raw_items } - .collect(raw_items.items()) - } + fn collect_macro_expansion(&mut self, module_id: CrateModuleId, macro_call_id: MacroCallId) { + let file_id: HirFileId = macro_call_id.into(); + let raw_items = self.db.raw_items(file_id); + ModCollector { def_collector: &mut *self, file_id, module_id, raw_items: &raw_items } + .collect(raw_items.items()) } fn finish(self) -> CrateDefMap { @@ -412,7 +386,7 @@ where Ok(file_id) => { let module_id = self.push_child_module(name.clone(), source_item_id, Some(file_id)); - let raw_items = self.def_collector.db.raw_items(file_id); + let raw_items = self.def_collector.db.raw_items(file_id.into()); ModCollector { def_collector: &mut *self.def_collector, module_id, @@ -484,38 +458,33 @@ where // Case 1: macro rules, define a macro in crate-global mutable scope if is_macro_rules(&mac.path) { if let Some(name) = &mac.name { - self.def_collector.define_macro(name.clone(), &mac.arg, mac.export) + let macro_id = MacroDefId::MacroByExample { + source_item_id: mac.source_item_id.with_file_id(self.file_id), + }; + self.def_collector.define_macro(name.clone(), macro_id, mac.export) } return; } let source_item_id = SourceItemId { file_id: self.file_id, item_id: mac.source_item_id }; - let macro_call_id = MacroCallLoc { - module: Module { krate: self.def_collector.def_map.krate, module_id: self.module_id }, - source_item_id, - } - .id(self.def_collector.db); // Case 2: try to expand macro_rules from this crate, triggering // recursive item collection. if let Some(¯o_id) = mac.path.as_ident().and_then(|name| self.def_collector.global_macro_scope.get(name)) { - self.def_collector.collect_macro_expansion( - self.module_id, - macro_call_id, - (self.def_collector.def_map.krate, macro_id), - mac.arg.clone(), - ); + let macro_call_id = + MacroCallLoc { def: macro_id, source_item_id }.id(self.def_collector.db); + + self.def_collector.collect_macro_expansion(self.module_id, macro_call_id); return; } // Case 3: path to a macro from another crate, expand during name resolution self.def_collector.unexpanded_macros.push(( self.module_id, - macro_call_id, + source_item_id, mac.path.clone(), - mac.arg.clone(), )) } } -- cgit v1.2.3