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/data.rs | 220 ++++++++++++++++++------------------------ 1 file changed, 96 insertions(+), 124 deletions(-) (limited to 'crates/ra_hir_def/src/data.rs') diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs index 53599e74a..697fde3d2 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs @@ -5,24 +5,23 @@ use std::sync::Arc; use hir_expand::{ hygiene::Hygiene, name::{name, AsName, Name}, - AstId, InFile, + InFile, }; use ra_prof::profile; -use ra_syntax::ast::{ - self, AssocItem, AstNode, ModuleItemOwner, NameOwner, TypeAscriptionOwner, TypeBoundsOwner, - VisibilityOwner, -}; +use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner, TypeBoundsOwner, VisibilityOwner}; use crate::{ attr::Attrs, + body::Expander, body::LowerCtx, db::DefDatabase, + item_tree::{AssocItem, ItemTreeId, ModItem}, path::{path, AssociatedTypeBinding, GenericArgs, Path}, src::HasSource, type_ref::{Mutability, TypeBound, TypeRef}, visibility::RawVisibility, - AssocContainerId, AssocItemId, ConstId, ConstLoc, Expander, FunctionId, FunctionLoc, HasModule, - ImplId, Intern, Lookup, StaticId, TraitId, TypeAliasId, TypeAliasLoc, + AssocContainerId, AssocItemId, ConstId, ConstLoc, FunctionId, FunctionLoc, HasModule, ImplId, + Intern, Lookup, ModuleId, StaticId, TraitId, TypeAliasId, TypeAliasLoc, }; #[derive(Debug, Clone, PartialEq, Eq)] @@ -155,30 +154,24 @@ pub struct TraitData { impl TraitData { pub(crate) fn trait_data_query(db: &dyn DefDatabase, tr: TraitId) -> Arc { let tr_loc = tr.lookup(db); - let src = tr_loc.source(db); - let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); - let auto = src.value.auto_token().is_some(); + let item_tree = db.item_tree(tr_loc.id.file_id); + let tr_def = &item_tree[tr_loc.id.value]; + let name = tr_def.name.clone(); + let auto = tr_def.auto; let module_id = tr_loc.container.module(db); - let container = AssocContainerId::TraitId(tr); - let mut items = Vec::new(); - - if let Some(item_list) = src.value.item_list() { - let mut expander = Expander::new(db, tr_loc.ast_id.file_id, module_id); - items.extend(collect_items( - db, - &mut expander, - item_list.assoc_items(), - src.file_id, - container, - )); - items.extend(collect_items_in_macros( - db, - &mut expander, - &src.with_value(item_list), - container, - )); - } + let mut expander = Expander::new(db, tr_loc.id.file_id, module_id); + + let items = collect_items( + db, + module_id, + &mut expander, + tr_def.items.iter().copied(), + tr_loc.id.file_id, + container, + 100, + ); + Arc::new(TraitData { name, items, auto }) } @@ -209,33 +202,28 @@ impl ImplData { pub(crate) fn impl_data_query(db: &dyn DefDatabase, id: ImplId) -> Arc { let _p = profile("impl_data_query"); let impl_loc = id.lookup(db); - let src = impl_loc.source(db); - let lower_ctx = LowerCtx::new(db, src.file_id); - let target_trait = src.value.target_trait().map(|it| TypeRef::from_ast(&lower_ctx, it)); - let target_type = TypeRef::from_ast_opt(&lower_ctx, src.value.target_type()); - let is_negative = src.value.excl_token().is_some(); + let item_tree = db.item_tree(impl_loc.id.file_id); + let impl_def = &item_tree[impl_loc.id.value]; + let target_trait = impl_def.target_trait.clone(); + let target_type = impl_def.target_type.clone(); + let is_negative = impl_def.is_negative; let module_id = impl_loc.container.module(db); let container = AssocContainerId::ImplId(id); + let mut expander = Expander::new(db, impl_loc.id.file_id, module_id); - let mut items: Vec = Vec::new(); - - if let Some(item_list) = src.value.item_list() { - let mut expander = Expander::new(db, impl_loc.ast_id.file_id, module_id); - items.extend( - collect_items(db, &mut expander, item_list.assoc_items(), src.file_id, container) - .into_iter() - .map(|(_, item)| item), - ); - items.extend( - collect_items_in_macros(db, &mut expander, &src.with_value(item_list), container) - .into_iter() - .map(|(_, item)| item), - ); - } + let items = collect_items( + db, + module_id, + &mut expander, + impl_def.items.iter().copied(), + impl_loc.id.file_id, + container, + 100, + ); + let items = items.into_iter().map(|(_, item)| item).collect(); - let res = ImplData { target_trait, target_type, items, is_negative }; - Arc::new(res) + Arc::new(ImplData { target_trait, target_type, items, is_negative }) } } @@ -295,28 +283,12 @@ impl StaticData { } } -fn collect_items_in_macros( - db: &dyn DefDatabase, - expander: &mut Expander, - impl_def: &InFile, - container: AssocContainerId, -) -> Vec<(Name, AssocItemId)> { - let mut res = Vec::new(); - - // We set a limit to protect against infinite recursion - let limit = 100; - - for m in impl_def.value.syntax().children().filter_map(ast::MacroCall::cast) { - res.extend(collect_items_in_macro(db, expander, m, container, limit)) - } - - res -} - -fn collect_items_in_macro( +fn collect_items( db: &dyn DefDatabase, + module: ModuleId, expander: &mut Expander, - m: ast::MacroCall, + assoc_items: impl Iterator, + file_id: crate::HirFileId, container: AssocContainerId, limit: usize, ) -> Vec<(Name, AssocItemId)> { @@ -324,62 +296,62 @@ fn collect_items_in_macro( return Vec::new(); } - if let Some((mark, items)) = expander.enter_expand(db, None, m) { - let items: InFile = expander.to_source(items); - let mut res = collect_items( - db, - expander, - items.value.items().filter_map(|it| AssocItem::cast(it.syntax().clone())), - items.file_id, - container, - ); - - // Recursive collect macros - // Note that ast::ModuleItem do not include ast::MacroCall - // We cannot use ModuleItemOwner::items here - for it in items.value.syntax().children().filter_map(ast::MacroCall::cast) { - res.extend(collect_items_in_macro(db, expander, it, container, limit - 1)) - } - expander.exit(db, mark); - res - } else { - Vec::new() - } -} + let item_tree = db.item_tree(file_id); + let cfg_options = db.crate_graph()[module.krate].cfg_options.clone(); -fn collect_items( - db: &dyn DefDatabase, - expander: &mut Expander, - assoc_items: impl Iterator, - file_id: crate::HirFileId, - container: AssocContainerId, -) -> Vec<(Name, AssocItemId)> { - let items = db.ast_id_map(file_id); - - assoc_items - .filter_map(|item_node| match item_node { - ast::AssocItem::FnDef(it) => { - let name = it.name().map_or_else(Name::missing, |it| it.as_name()); - if !expander.is_cfg_enabled(&it) { - return None; + let mut items = Vec::new(); + for item in assoc_items { + match item { + AssocItem::Function(id) => { + let item = &item_tree[id]; + if !item.attrs.is_cfg_enabled(&cfg_options) { + continue; } - let def = FunctionLoc { container, ast_id: AstId::new(file_id, items.ast_id(&it)) } - .intern(db); - Some((name, def.into())) + let def = FunctionLoc { container, id: ItemTreeId::new(file_id, id) }.intern(db); + items.push((item.name.clone(), def.into())); } - ast::AssocItem::ConstDef(it) => { - let name = it.name().map_or_else(Name::missing, |it| it.as_name()); - let def = ConstLoc { container, ast_id: AstId::new(file_id, items.ast_id(&it)) } - .intern(db); - Some((name, def.into())) + // FIXME: cfg? + AssocItem::Const(id) => { + let item = &item_tree[id]; + let name = if let Some(name) = item.name.clone() { + name + } else { + continue; + }; + let def = ConstLoc { container, id: ItemTreeId::new(file_id, id) }.intern(db); + items.push((name, def.into())); } - ast::AssocItem::TypeAliasDef(it) => { - let name = it.name().map_or_else(Name::missing, |it| it.as_name()); - let def = - TypeAliasLoc { container, ast_id: AstId::new(file_id, items.ast_id(&it)) } - .intern(db); - Some((name, def.into())) + AssocItem::TypeAlias(id) => { + let item = &item_tree[id]; + let def = TypeAliasLoc { container, id: ItemTreeId::new(file_id, id) }.intern(db); + items.push((item.name.clone(), def.into())); } - }) - .collect() + AssocItem::MacroCall(call) => { + let call = &item_tree[call]; + let ast_id_map = db.ast_id_map(file_id); + let root = db.parse_or_expand(file_id).unwrap(); + let call = ast_id_map.get(call.ast_id).to_node(&root); + + if let Some((mark, mac)) = expander.enter_expand(db, None, call) { + let src: InFile = expander.to_source(mac); + let item_tree = db.item_tree(src.file_id); + let iter = + item_tree.top_level_items().iter().filter_map(ModItem::as_assoc_item); + items.extend(collect_items( + db, + module, + expander, + iter, + src.file_id, + container, + limit - 1, + )); + + expander.exit(db, mark); + } + } + } + } + + items } -- cgit v1.2.3