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 From 1fbe21a545104e85aa5f9d0d8a45ec1040396cb9 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 22 Jun 2020 16:41:10 +0200 Subject: Make remaining item data queries use item tree --- crates/ra_hir_def/src/data.rs | 164 +++++++++++------------------------------- 1 file changed, 42 insertions(+), 122 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 697fde3d2..921253c42 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs @@ -2,23 +2,16 @@ use std::sync::Arc; -use hir_expand::{ - hygiene::Hygiene, - name::{name, AsName, Name}, - InFile, -}; +use hir_expand::{name::Name, InFile}; use ra_prof::profile; -use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner, TypeBoundsOwner, VisibilityOwner}; +use ra_syntax::ast; 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}, + type_ref::{TypeBound, TypeRef}, visibility::RawVisibility, AssocContainerId, AssocItemId, ConstId, ConstLoc, FunctionId, FunctionLoc, HasModule, ImplId, Intern, Lookup, ModuleId, StaticId, TraitId, TypeAliasId, TypeAliasLoc, @@ -40,82 +33,27 @@ pub struct FunctionData { impl FunctionData { pub(crate) fn fn_data_query(db: &impl DefDatabase, func: FunctionId) -> Arc { let loc = func.lookup(db); - let src = loc.source(db); - let ctx = LowerCtx::new(db, src.file_id); - let name = src.value.name().map(|n| n.as_name()).unwrap_or_else(Name::missing); - let mut params = Vec::new(); - let mut has_self_param = false; - if let Some(param_list) = src.value.param_list() { - if let Some(self_param) = param_list.self_param() { - let self_type = if let Some(type_ref) = self_param.ascribed_type() { - TypeRef::from_ast(&ctx, type_ref) - } else { - let self_type = TypeRef::Path(name![Self].into()); - match self_param.kind() { - ast::SelfParamKind::Owned => self_type, - ast::SelfParamKind::Ref => { - TypeRef::Reference(Box::new(self_type), Mutability::Shared) - } - ast::SelfParamKind::MutRef => { - TypeRef::Reference(Box::new(self_type), Mutability::Mut) - } - } - }; - params.push(self_type); - has_self_param = true; - } - for param in param_list.params() { - let type_ref = TypeRef::from_ast_opt(&ctx, param.ascribed_type()); - params.push(type_ref); - } - } - let attrs = Attrs::new(&src.value, &Hygiene::new(db.upcast(), src.file_id)); - - let ret_type = if let Some(type_ref) = src.value.ret_type().and_then(|rt| rt.type_ref()) { - TypeRef::from_ast(&ctx, type_ref) - } else { - TypeRef::unit() - }; - - let ret_type = if src.value.async_token().is_some() { - let future_impl = desugar_future_path(ret_type); - let ty_bound = TypeBound::Path(future_impl); - TypeRef::ImplTrait(vec![ty_bound]) - } else { - ret_type - }; - - let is_unsafe = src.value.unsafe_token().is_some(); - - let vis_default = RawVisibility::default_for_container(loc.container); - let visibility = - RawVisibility::from_ast_with_default(db, vis_default, src.map(|s| s.visibility())); - - let sig = - FunctionData { name, params, ret_type, has_self_param, is_unsafe, visibility, attrs }; - Arc::new(sig) + let item_tree = db.item_tree(loc.id.file_id); + let func = &item_tree[loc.id.value]; + + Arc::new(FunctionData { + name: func.name.clone(), + params: func.params.clone(), + ret_type: func.ret_type.clone(), + attrs: func.attrs.clone(), + has_self_param: func.has_self_param, + is_unsafe: func.is_unsafe, + visibility: func.visibility.clone(), + }) } } -fn desugar_future_path(orig: TypeRef) -> Path { - let path = path![core::future::Future]; - let mut generic_args: Vec<_> = std::iter::repeat(None).take(path.segments.len() - 1).collect(); - let mut last = GenericArgs::empty(); - last.bindings.push(AssociatedTypeBinding { - name: name![Output], - type_ref: Some(orig), - bounds: Vec::new(), - }); - generic_args.push(Some(Arc::new(last))); - - Path::from_known_path(path, generic_args) -} - #[derive(Debug, Clone, PartialEq, Eq)] pub struct TypeAliasData { pub name: Name, pub type_ref: Option, pub visibility: RawVisibility, + /// Bounds restricting the type alias itself (eg. `type Ty: Bound;` in a trait or impl). pub bounds: Vec, } @@ -125,22 +63,15 @@ impl TypeAliasData { typ: TypeAliasId, ) -> Arc { let loc = typ.lookup(db); - let node = loc.source(db); - let name = node.value.name().map_or_else(Name::missing, |n| n.as_name()); - let lower_ctx = LowerCtx::new(db, node.file_id); - let type_ref = node.value.type_ref().map(|it| TypeRef::from_ast(&lower_ctx, it)); - let vis_default = RawVisibility::default_for_container(loc.container); - let visibility = RawVisibility::from_ast_with_default( - db, - vis_default, - node.as_ref().map(|n| n.visibility()), - ); - let bounds = if let Some(bound_list) = node.value.type_bound_list() { - bound_list.bounds().map(|it| TypeBound::from_ast(&lower_ctx, it)).collect() - } else { - Vec::new() - }; - Arc::new(TypeAliasData { name, type_ref, visibility, bounds }) + let item_tree = db.item_tree(loc.id.file_id); + let typ = &item_tree[loc.id.value]; + + Arc::new(TypeAliasData { + name: typ.name.clone(), + type_ref: typ.type_ref.clone(), + visibility: typ.visibility.clone(), + bounds: typ.bounds.clone(), + }) } } @@ -238,22 +169,14 @@ pub struct ConstData { impl ConstData { pub(crate) fn const_data_query(db: &dyn DefDatabase, konst: ConstId) -> Arc { let loc = konst.lookup(db); - let node = loc.source(db); - let vis_default = RawVisibility::default_for_container(loc.container); - Arc::new(ConstData::new(db, vis_default, node)) - } + let item_tree = db.item_tree(loc.id.file_id); + let konst = &item_tree[loc.id.value]; - fn new( - db: &dyn DefDatabase, - vis_default: RawVisibility, - node: InFile, - ) -> ConstData { - let ctx = LowerCtx::new(db, node.file_id); - let name = node.value.name().map(|n| n.as_name()); - let type_ref = TypeRef::from_ast_opt(&ctx, node.value.ascribed_type()); - let visibility = - RawVisibility::from_ast_with_default(db, vis_default, node.map(|n| n.visibility())); - ConstData { name, type_ref, visibility } + Arc::new(ConstData { + name: konst.name.clone(), + type_ref: konst.type_ref.clone(), + visibility: konst.visibility.clone(), + }) } } @@ -267,19 +190,16 @@ pub struct StaticData { impl StaticData { pub(crate) fn static_data_query(db: &dyn DefDatabase, konst: StaticId) -> Arc { - let node = konst.lookup(db).source(db); - let ctx = LowerCtx::new(db, node.file_id); - - let name = node.value.name().map(|n| n.as_name()); - let type_ref = TypeRef::from_ast_opt(&ctx, node.value.ascribed_type()); - let mutable = node.value.mut_token().is_some(); - let visibility = RawVisibility::from_ast_with_default( - db, - RawVisibility::private(), - node.map(|n| n.visibility()), - ); - - Arc::new(StaticData { name, type_ref, visibility, mutable }) + let node = konst.lookup(db); + let item_tree = db.item_tree(node.id.file_id); + let statik = &item_tree[node.id.value]; + + Arc::new(StaticData { + name: Some(statik.name.clone()), + type_ref: statik.type_ref.clone(), + visibility: statik.visibility.clone(), + mutable: statik.mutable, + }) } } -- cgit v1.2.3 From ae7a296c85bba6ab279dd17b193f86db22ec3437 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 23 Jun 2020 18:20:51 +0200 Subject: Unify and test attribute handling --- crates/ra_hir_def/src/data.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 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 921253c42..51c97c584 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs @@ -40,7 +40,7 @@ impl FunctionData { name: func.name.clone(), params: func.params.clone(), ret_type: func.ret_type.clone(), - attrs: func.attrs.clone(), + attrs: item_tree.attrs(loc.id.value.into()).clone(), has_self_param: func.has_self_param, is_unsafe: func.is_unsafe, visibility: func.visibility.clone(), @@ -224,7 +224,8 @@ fn collect_items( match item { AssocItem::Function(id) => { let item = &item_tree[id]; - if !item.attrs.is_cfg_enabled(&cfg_options) { + let attrs = item_tree.attrs(id.into()); + if !attrs.is_cfg_enabled(&cfg_options) { continue; } let def = FunctionLoc { container, id: ItemTreeId::new(file_id, id) }.intern(db); -- cgit v1.2.3 From 20ff1cdcfbef92429962569f7e98a169c6a10e50 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 23 Jun 2020 18:31:14 +0200 Subject: Address more comments --- crates/ra_hir_def/src/data.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 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 51c97c584..5f8eb72a0 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs @@ -234,10 +234,9 @@ fn collect_items( // FIXME: cfg? AssocItem::Const(id) => { let item = &item_tree[id]; - let name = if let Some(name) = item.name.clone() { - name - } else { - continue; + let name = match item.name.clone() { + Some(name) => name, + None => continue, }; let def = ConstLoc { container, id: ItemTreeId::new(file_id, id) }.intern(db); items.push((name, def.into())); -- cgit v1.2.3 From 43cad21623bc5de59598a565097be9c7d8642818 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 24 Jun 2020 15:36:18 +0200 Subject: Don't allocate common visibilities --- crates/ra_hir_def/src/data.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 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 5f8eb72a0..5ca331380 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs @@ -43,7 +43,7 @@ impl FunctionData { attrs: item_tree.attrs(loc.id.value.into()).clone(), has_self_param: func.has_self_param, is_unsafe: func.is_unsafe, - visibility: func.visibility.clone(), + visibility: item_tree[func.visibility].clone(), }) } } @@ -69,7 +69,7 @@ impl TypeAliasData { Arc::new(TypeAliasData { name: typ.name.clone(), type_ref: typ.type_ref.clone(), - visibility: typ.visibility.clone(), + visibility: item_tree[typ.visibility].clone(), bounds: typ.bounds.clone(), }) } @@ -175,7 +175,7 @@ impl ConstData { Arc::new(ConstData { name: konst.name.clone(), type_ref: konst.type_ref.clone(), - visibility: konst.visibility.clone(), + visibility: item_tree[konst.visibility].clone(), }) } } @@ -197,7 +197,7 @@ impl StaticData { Arc::new(StaticData { name: Some(statik.name.clone()), type_ref: statik.type_ref.clone(), - visibility: statik.visibility.clone(), + visibility: item_tree[statik.visibility].clone(), mutable: statik.mutable, }) } -- cgit v1.2.3 From 94169ee504bf1a5e59530c1ab1f5f5550ae45914 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 24 Jun 2020 16:07:02 +0200 Subject: ItemTree: Use more boxed slices --- crates/ra_hir_def/src/data.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 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 5ca331380..f9e5701db 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs @@ -38,7 +38,7 @@ impl FunctionData { Arc::new(FunctionData { name: func.name.clone(), - params: func.params.clone(), + params: func.params.to_vec(), ret_type: func.ret_type.clone(), attrs: item_tree.attrs(loc.id.value.into()).clone(), has_self_param: func.has_self_param, @@ -70,7 +70,7 @@ impl TypeAliasData { name: typ.name.clone(), type_ref: typ.type_ref.clone(), visibility: item_tree[typ.visibility].clone(), - bounds: typ.bounds.clone(), + bounds: typ.bounds.to_vec(), }) } } -- cgit v1.2.3