From 4b03b39d5b4b00daffb120a4d2d9ea4a55a9a7ac Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 22 Jun 2020 15:07:06 +0200 Subject: draw the rest of the owl --- crates/ra_hir_def/src/body/lower.rs | 173 +++++++++++++++++++++++++----------- 1 file changed, 122 insertions(+), 51 deletions(-) (limited to 'crates/ra_hir_def/src/body') diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index f159f80af..e7cf80676 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs @@ -5,7 +5,7 @@ use either::Either; use hir_expand::{ hygiene::Hygiene, name::{name, AsName, Name}, - HirFileId, MacroDefId, MacroDefKind, + AstId, HirFileId, MacroDefId, MacroDefKind, }; use ra_arena::Arena; use ra_syntax::{ @@ -27,6 +27,7 @@ use crate::{ LogicOp, MatchArm, Ordering, Pat, PatId, RecordFieldPat, RecordLitField, Statement, }, item_scope::BuiltinShadowMode, + item_tree::{FileItemTreeId, ItemTree, ItemTreeSource}, path::{GenericArgs, Path}, type_ref::{Mutability, Rawness, TypeRef}, AdtId, ConstLoc, ContainerId, DefWithBodyId, EnumLoc, FunctionLoc, Intern, ModuleDefId, @@ -35,6 +36,7 @@ use crate::{ use super::{ExprSource, PatSource}; use ast::AstChildren; +use std::sync::Arc; pub(crate) struct LowerCtx { hygiene: Hygiene, @@ -55,11 +57,13 @@ impl LowerCtx { pub(super) fn lower( db: &dyn DefDatabase, + file_id: HirFileId, def: DefWithBodyId, expander: Expander, params: Option, body: Option, ) -> (Body, BodySourceMap) { + let item_tree = db.item_tree(file_id); ExprCollector { db, def, @@ -72,6 +76,7 @@ pub(super) fn lower( body_expr: dummy_expr_id(), item_scope: Default::default(), }, + item_trees: vec![(file_id, item_tree)], } .collect(params, body) } @@ -82,6 +87,8 @@ struct ExprCollector<'a> { expander: Expander, body: Body, source_map: BodySourceMap, + + item_trees: Vec<(HirFileId, Arc)>, } impl ExprCollector<'_> { @@ -533,6 +540,9 @@ impl ExprCollector<'_> { self.source_map .expansions .insert(macro_call, self.expander.current_file_id); + + let item_tree = self.db.item_tree(self.expander.current_file_id); + self.item_trees.push((self.expander.current_file_id, item_tree)); let id = self.collect_expr(expansion); self.expander.exit(self.db, mark); id @@ -547,6 +557,21 @@ impl ExprCollector<'_> { } } + fn find_inner_item(&self, id: AstId) -> FileItemTreeId { + let index = + self.item_trees.iter().position(|(file, _)| *file == id.file_id).unwrap_or_else(|| { + panic!("couldn't find item tree for file {:?}", id.file_id); + }); + let tree = &self.item_trees[index].1; + + // Root file (non-macro). + tree.all_inner_items() + .chain(tree.top_level_items().iter().copied()) + .filter_map(|mod_item| mod_item.downcast::()) + .find(|tree_id| tree[*tree_id].ast_id().upcast() == id.value) + .unwrap() + } + fn collect_expr_opt(&mut self, expr: Option) -> ExprId { if let Some(expr) = expr { self.collect_expr(expr) @@ -578,56 +603,102 @@ impl ExprCollector<'_> { fn collect_block_items(&mut self, block: &ast::BlockExpr) { let container = ContainerId::DefWithBodyId(self.def); - for item in block.items() { - let (def, name): (ModuleDefId, Option) = match item { - ast::ModuleItem::FnDef(def) => { - let ast_id = self.expander.ast_id(&def); - ( - FunctionLoc { container: container.into(), ast_id }.intern(self.db).into(), - def.name(), - ) - } - ast::ModuleItem::TypeAliasDef(def) => { - let ast_id = self.expander.ast_id(&def); - ( - TypeAliasLoc { container: container.into(), ast_id }.intern(self.db).into(), - def.name(), - ) - } - ast::ModuleItem::ConstDef(def) => { - let ast_id = self.expander.ast_id(&def); - ( - ConstLoc { container: container.into(), ast_id }.intern(self.db).into(), - def.name(), - ) - } - ast::ModuleItem::StaticDef(def) => { - let ast_id = self.expander.ast_id(&def); - (StaticLoc { container, ast_id }.intern(self.db).into(), def.name()) - } - ast::ModuleItem::StructDef(def) => { - let ast_id = self.expander.ast_id(&def); - (StructLoc { container, ast_id }.intern(self.db).into(), def.name()) - } - ast::ModuleItem::EnumDef(def) => { - let ast_id = self.expander.ast_id(&def); - (EnumLoc { container, ast_id }.intern(self.db).into(), def.name()) - } - ast::ModuleItem::UnionDef(def) => { - let ast_id = self.expander.ast_id(&def); - (UnionLoc { container, ast_id }.intern(self.db).into(), def.name()) - } - ast::ModuleItem::TraitDef(def) => { - let ast_id = self.expander.ast_id(&def); - (TraitLoc { container, ast_id }.intern(self.db).into(), def.name()) - } - ast::ModuleItem::ExternBlock(_) => continue, // FIXME: collect from extern blocks - ast::ModuleItem::ImplDef(_) - | ast::ModuleItem::UseItem(_) - | ast::ModuleItem::ExternCrateItem(_) - | ast::ModuleItem::Module(_) - | ast::ModuleItem::MacroCall(_) => continue, - }; + + let items = block + .items() + .filter_map(|item| { + let (def, name): (ModuleDefId, Option) = match item { + ast::ModuleItem::FnDef(def) => { + let ast_id = self.expander.ast_id(&def); + let id = self.find_inner_item(ast_id.map(|id| id.upcast())); + ( + FunctionLoc { container: container.into(), id: ast_id.with_value(id) } + .intern(self.db) + .into(), + def.name(), + ) + } + ast::ModuleItem::TypeAliasDef(def) => { + let ast_id = self.expander.ast_id(&def); + let id = self.find_inner_item(ast_id.map(|id| id.upcast())); + ( + TypeAliasLoc { container: container.into(), id: ast_id.with_value(id) } + .intern(self.db) + .into(), + def.name(), + ) + } + ast::ModuleItem::ConstDef(def) => { + let ast_id = self.expander.ast_id(&def); + let id = self.find_inner_item(ast_id.map(|id| id.upcast())); + ( + ConstLoc { container: container.into(), id: ast_id.with_value(id) } + .intern(self.db) + .into(), + def.name(), + ) + } + ast::ModuleItem::StaticDef(def) => { + let ast_id = self.expander.ast_id(&def); + let id = self.find_inner_item(ast_id.map(|id| id.upcast())); + ( + StaticLoc { container, id: ast_id.with_value(id) } + .intern(self.db) + .into(), + def.name(), + ) + } + ast::ModuleItem::StructDef(def) => { + let ast_id = self.expander.ast_id(&def); + let id = self.find_inner_item(ast_id.map(|id| id.upcast())); + ( + StructLoc { container, id: ast_id.with_value(id) } + .intern(self.db) + .into(), + def.name(), + ) + } + ast::ModuleItem::EnumDef(def) => { + let ast_id = self.expander.ast_id(&def); + let id = self.find_inner_item(ast_id.map(|id| id.upcast())); + ( + EnumLoc { container, id: ast_id.with_value(id) }.intern(self.db).into(), + def.name(), + ) + } + ast::ModuleItem::UnionDef(def) => { + let ast_id = self.expander.ast_id(&def); + let id = self.find_inner_item(ast_id.map(|id| id.upcast())); + ( + UnionLoc { container, id: ast_id.with_value(id) } + .intern(self.db) + .into(), + def.name(), + ) + } + ast::ModuleItem::TraitDef(def) => { + let ast_id = self.expander.ast_id(&def); + let id = self.find_inner_item(ast_id.map(|id| id.upcast())); + ( + TraitLoc { container, id: ast_id.with_value(id) } + .intern(self.db) + .into(), + def.name(), + ) + } + ast::ModuleItem::ExternBlock(_) => return None, // FIXME: collect from extern blocks + ast::ModuleItem::ImplDef(_) + | ast::ModuleItem::UseItem(_) + | ast::ModuleItem::ExternCrateItem(_) + | ast::ModuleItem::Module(_) + | ast::ModuleItem::MacroCall(_) => return None, + }; + + Some((def, name)) + }) + .collect::>(); + + for (def, name) in items { self.body.item_scope.define_def(def); if let Some(name) = name { let vis = crate::visibility::Visibility::Public; // FIXME determine correctly -- cgit v1.2.3