From 278556f9fe8240f0c76daaaf8dcf7ee7f516e4af Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 16 Jun 2020 14:52:43 +0200 Subject: Move ItemTree lowering into its own module --- crates/ra_hir_def/src/item_tree/lower.rs | 501 +++++++++++++++++++++++++++++++ 1 file changed, 501 insertions(+) create mode 100644 crates/ra_hir_def/src/item_tree/lower.rs (limited to 'crates/ra_hir_def/src/item_tree/lower.rs') diff --git a/crates/ra_hir_def/src/item_tree/lower.rs b/crates/ra_hir_def/src/item_tree/lower.rs new file mode 100644 index 000000000..d123a7310 --- /dev/null +++ b/crates/ra_hir_def/src/item_tree/lower.rs @@ -0,0 +1,501 @@ +use super::*; +use crate::attr::Attrs; +use hir_expand::{ast_id_map::AstIdMap, hygiene::Hygiene, HirFileId}; +use ra_syntax::ast::{self, ModuleItemOwner}; +use smallvec::SmallVec; +use std::sync::Arc; + +struct ModItems(SmallVec<[ModItem; 1]>); + +impl From for ModItems +where + T: Into, +{ + fn from(t: T) -> Self { + ModItems(SmallVec::from_buf([t.into(); 1])) + } +} + +pub(super) struct Ctx { + pub tree: ItemTree, + pub hygiene: Hygiene, + pub file: HirFileId, + pub source_ast_id_map: Arc, + pub body_ctx: crate::body::LowerCtx, +} + +impl Ctx { + pub(super) fn lower(mut self, item_owner: &dyn ModuleItemOwner) -> ItemTree { + self.tree.top_level = item_owner + .items() + .flat_map(|item| self.lower_mod_item(&item)) + .flat_map(|items| items.0) + .collect(); + self.tree + } + + fn lower_mod_item(&mut self, item: &ast::ModuleItem) -> Option { + let attrs = Attrs::new(item, &self.hygiene); + let items = match item { + ast::ModuleItem::StructDef(ast) => { + self.lower_struct(ast).map(|data| self.tree.structs.alloc(data).into()) + } + ast::ModuleItem::UnionDef(ast) => { + self.lower_union(ast).map(|data| self.tree.unions.alloc(data).into()) + } + ast::ModuleItem::EnumDef(ast) => { + self.lower_enum(ast).map(|data| self.tree.enums.alloc(data).into()) + } + ast::ModuleItem::FnDef(ast) => { + self.lower_function(ast).map(|data| self.tree.functions.alloc(data).into()) + } + ast::ModuleItem::TypeAliasDef(ast) => { + self.lower_type_alias(ast).map(|data| self.tree.type_aliases.alloc(data).into()) + } + ast::ModuleItem::StaticDef(ast) => { + self.lower_static(ast).map(|data| self.tree.statics.alloc(data).into()) + } + ast::ModuleItem::ConstDef(ast) => { + let data = self.lower_const(ast); + Some(self.tree.consts.alloc(data).into()) + } + ast::ModuleItem::Module(ast) => { + self.lower_module(ast).map(|data| self.tree.mods.alloc(data).into()) + } + ast::ModuleItem::TraitDef(ast) => { + self.lower_trait(ast).map(|data| self.tree.traits.alloc(data).into()) + } + ast::ModuleItem::ImplDef(ast) => { + self.lower_impl(ast).map(|data| self.tree.impls.alloc(data).into()) + } + ast::ModuleItem::UseItem(ast) => Some(ModItems( + self.lower_use(ast) + .into_iter() + .map(|data| self.tree.imports.alloc(data).into()) + .collect::>(), + )), + ast::ModuleItem::ExternCrateItem(ast) => { + self.lower_extern_crate(ast).map(|data| self.tree.imports.alloc(data).into()) + } + ast::ModuleItem::MacroCall(ast) => { + self.lower_macro_call(ast).map(|data| self.tree.macro_calls.alloc(data).into()) + } + ast::ModuleItem::ExternBlock(ast) => Some(ModItems( + self.lower_extern_block(ast) + .into_iter() + .map(|item| match item { + Either::Left(func) => self.tree.functions.alloc(func).into(), + Either::Right(statik) => self.tree.statics.alloc(statik).into(), + }) + .collect::>(), + )), + }; + + if !attrs.is_empty() { + for item in items.iter().flat_map(|items| &items.0) { + self.tree.attrs.insert(*item, attrs.clone()); + } + } + + items + } + + fn lower_assoc_item(&mut self, item: &ast::AssocItem) -> Option { + match item { + ast::AssocItem::FnDef(ast) => { + self.lower_function(ast).map(|data| self.tree.functions.alloc(data).into()) + } + ast::AssocItem::TypeAliasDef(ast) => { + self.lower_type_alias(ast).map(|data| self.tree.type_aliases.alloc(data).into()) + } + ast::AssocItem::ConstDef(ast) => { + let data = self.lower_const(ast); + Some(self.tree.consts.alloc(data).into()) + } + } + } + + fn lower_struct(&mut self, strukt: &ast::StructDef) -> Option { + let attrs = self.lower_attrs(strukt); + let visibility = self.lower_visibility(strukt); + let name = strukt.name()?.as_name(); + let generic_params = self.lower_generic_params(strukt); + let fields = self.lower_fields(&strukt.kind()); + let ast_id = self.source_ast_id_map.ast_id(strukt); + let kind = match strukt.kind() { + ast::StructKind::Record(_) => StructDefKind::Record, + ast::StructKind::Tuple(_) => StructDefKind::Tuple, + ast::StructKind::Unit => StructDefKind::Unit, + }; + let res = Struct { name, attrs, visibility, generic_params, fields, ast_id, kind }; + Some(res) + } + + fn lower_fields(&mut self, strukt_kind: &ast::StructKind) -> Fields { + match strukt_kind { + ast::StructKind::Record(it) => { + let range = self.lower_record_fields(it); + Fields::Record(range) + } + ast::StructKind::Tuple(it) => { + let range = self.lower_tuple_fields(it); + Fields::Tuple(range) + } + ast::StructKind::Unit => Fields::Unit, + } + } + + fn lower_record_fields(&mut self, fields: &ast::RecordFieldDefList) -> Range> { + let start = self.next_field_idx(); + for field in fields.fields() { + if let Some(data) = self.lower_record_field(&field) { + self.tree.fields.alloc(data); + } + } + let end = self.next_field_idx(); + start..end + } + + fn lower_record_field(&self, field: &ast::RecordFieldDef) -> Option { + let name = field.name()?.as_name(); + let visibility = self.lower_visibility(field); + let type_ref = self.lower_type_ref(&field.ascribed_type()?); + let res = Field { name, type_ref, visibility }; + Some(res) + } + + fn lower_tuple_fields(&mut self, fields: &ast::TupleFieldDefList) -> Range> { + let start = self.next_field_idx(); + for (i, field) in fields.fields().enumerate() { + if let Some(data) = self.lower_tuple_field(i, &field) { + self.tree.fields.alloc(data); + } + } + let end = self.next_field_idx(); + start..end + } + + fn lower_tuple_field(&self, idx: usize, field: &ast::TupleFieldDef) -> Option { + let name = Name::new_tuple_field(idx); + let visibility = self.lower_visibility(field); + let type_ref = self.lower_type_ref(&field.type_ref()?); + let res = Field { name, type_ref, visibility }; + Some(res) + } + + fn lower_union(&mut self, union: &ast::UnionDef) -> Option { + let attrs = self.lower_attrs(union); + let visibility = self.lower_visibility(union); + let name = union.name()?.as_name(); + let generic_params = self.lower_generic_params(union); + let fields = match union.record_field_def_list() { + Some(record_field_def_list) => { + self.lower_fields(&StructKind::Record(record_field_def_list)) + } + None => Fields::Record(self.next_field_idx()..self.next_field_idx()), + }; + let ast_id = self.source_ast_id_map.ast_id(union); + let res = Union { name, attrs, visibility, generic_params, fields, ast_id }; + Some(res) + } + + fn lower_enum(&mut self, enum_: &ast::EnumDef) -> Option { + let attrs = self.lower_attrs(enum_); + let visibility = self.lower_visibility(enum_); + let name = enum_.name()?.as_name(); + let generic_params = self.lower_generic_params(enum_); + let variants = match &enum_.variant_list() { + Some(variant_list) => self.lower_variants(variant_list), + None => self.next_variant_idx()..self.next_variant_idx(), + }; + let ast_id = self.source_ast_id_map.ast_id(enum_); + let res = Enum { name, attrs, visibility, generic_params, variants, ast_id }; + Some(res) + } + + fn lower_variants(&mut self, variants: &ast::EnumVariantList) -> Range> { + let start = self.next_variant_idx(); + for variant in variants.variants() { + if let Some(data) = self.lower_variant(&variant) { + self.tree.variants.alloc(data); + } + } + let end = self.next_variant_idx(); + start..end + } + + fn lower_variant(&mut self, variant: &ast::EnumVariant) -> Option { + let name = variant.name()?.as_name(); + let fields = self.lower_fields(&variant.kind()); + let res = Variant { name, fields }; + Some(res) + } + + fn lower_function(&mut self, func: &ast::FnDef) -> Option { + let attrs = self.lower_attrs(func); + let visibility = self.lower_visibility(func); + let name = func.name()?.as_name(); + let generic_params = self.lower_generic_params(func); + + let mut params = Vec::new(); + let mut has_self_param = false; + if let Some(param_list) = func.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(&self.body_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(&self.body_ctx, param.ascribed_type()); + params.push(type_ref); + } + } + let ret_type = match func.ret_type().and_then(|rt| rt.type_ref()) { + Some(type_ref) => TypeRef::from_ast(&self.body_ctx, type_ref), + _ => TypeRef::unit(), + }; + + let ret_type = if func.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 ast_id = self.source_ast_id_map.ast_id(func); + let res = Function { + name, + attrs, + visibility, + generic_params, + has_self_param, + params, + ret_type, + ast_id, + }; + Some(res) + } + + fn lower_type_alias(&mut self, type_alias: &ast::TypeAliasDef) -> Option { + let name = type_alias.name()?.as_name(); + let type_ref = type_alias.type_ref().map(|it| self.lower_type_ref(&it)); + let visibility = self.lower_visibility(type_alias); + let generic_params = self.lower_generic_params(type_alias); + let ast_id = self.source_ast_id_map.ast_id(type_alias); + let res = TypeAlias { name, visibility, generic_params, type_ref, ast_id }; + Some(res) + } + + fn lower_static(&mut self, static_: &ast::StaticDef) -> Option { + let name = static_.name()?.as_name(); + let type_ref = self.lower_type_ref_opt(static_.ascribed_type()); + let visibility = self.lower_visibility(static_); + let ast_id = self.source_ast_id_map.ast_id(static_); + let res = Static { name, visibility, type_ref, ast_id }; + Some(res) + } + + fn lower_const(&mut self, konst: &ast::ConstDef) -> Const { + let name = konst.name().map(|it| it.as_name()); + let type_ref = self.lower_type_ref_opt(konst.ascribed_type()); + let visibility = self.lower_visibility(konst); + let ast_id = self.source_ast_id_map.ast_id(konst); + Const { name, visibility, type_ref, ast_id } + } + + fn lower_module(&mut self, module: &ast::Module) -> Option { + let name = module.name()?.as_name(); + let visibility = self.lower_visibility(module); + let kind = if module.semicolon_token().is_some() { + ModKind::Outline {} + } else { + ModKind::Inline { + items: module + .item_list() + .map(|list| { + list.items() + .flat_map(|item| self.lower_mod_item(&item)) + .flat_map(|items| items.0) + .collect() + }) + .unwrap_or_else(|| { + mark::hit!(name_res_works_for_broken_modules); + Vec::new() + }), + } + }; + let ast_id = self.source_ast_id_map.ast_id(module); + Some(Mod { name, visibility, kind, ast_id }) + } + + fn lower_trait(&mut self, trait_def: &ast::TraitDef) -> Option { + let name = trait_def.name()?.as_name(); + let visibility = self.lower_visibility(trait_def); + let generic_params = self.lower_generic_params(trait_def); + let auto = trait_def.auto_token().is_some(); + let items = trait_def.item_list().map(|list| { + // FIXME: Does not handle macros + list.assoc_items().flat_map(|item| self.lower_assoc_item(&item)).collect() + }); + let ast_id = self.source_ast_id_map.ast_id(trait_def); + Some(Trait { + name, + visibility, + generic_params, + auto, + items: items.unwrap_or_default(), + ast_id, + }) + } + + fn lower_impl(&mut self, impl_def: &ast::ImplDef) -> Option { + let generic_params = self.lower_generic_params(impl_def); + let target_trait = impl_def.target_trait().map(|tr| self.lower_type_ref(&tr)); + let target_type = self.lower_type_ref(&impl_def.target_type()?); + let is_negative = impl_def.excl_token().is_some(); + let items = impl_def + .item_list()? + .assoc_items() + .filter_map(|item| self.lower_assoc_item(&item)) + .collect(); + let ast_id = self.source_ast_id_map.ast_id(impl_def); + Some(Impl { generic_params, target_trait, target_type, is_negative, items, ast_id }) + } + + fn lower_use(&mut self, use_item: &ast::UseItem) -> Vec { + // FIXME: cfg_attr + let is_prelude = use_item.has_atom_attr("prelude_import"); + let visibility = self.lower_visibility(use_item); + + // Every use item can expand to many `Import`s. + let mut imports = Vec::new(); + ModPath::expand_use_item( + InFile::new(self.file, use_item.clone()), + &self.hygiene, + |path, _tree, is_glob, alias| { + imports.push(Import { + path, + alias, + visibility: visibility.clone(), + is_glob, + is_prelude, + is_extern_crate: false, + is_macro_use: false, + }); + }, + ); + + imports + } + + fn lower_extern_crate(&mut self, extern_crate: &ast::ExternCrateItem) -> Option { + let path = ModPath::from_name_ref(&extern_crate.name_ref()?); + let alias = extern_crate.alias().map(|a| { + a.name().map(|it| it.as_name()).map_or(ImportAlias::Underscore, ImportAlias::Alias) + }); + let visibility = self.lower_visibility(extern_crate); + // FIXME: cfg_attr + let is_macro_use = extern_crate.has_atom_attr("macro_use"); + + Some(Import { + path, + alias, + visibility, + is_glob: false, + is_prelude: false, + is_extern_crate: true, + is_macro_use, + }) + } + + fn lower_macro_call(&mut self, m: &ast::MacroCall) -> Option { + let name = m.name().map(|it| it.as_name()); + let attrs = Attrs::new(m, &self.hygiene); + let path = ModPath::from_src(m.path()?, &self.hygiene)?; + + let ast_id = self.source_ast_id_map.ast_id(m); + + // FIXME: cfg_attr + let export_attr = attrs.by_key("macro_export"); + + let is_export = export_attr.exists(); + let is_local_inner = if is_export { + export_attr.tt_values().map(|it| &it.token_trees).flatten().any(|it| match it { + tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => { + ident.text.contains("local_inner_macros") + } + _ => false, + }) + } else { + false + }; + + let is_builtin = attrs.by_key("rustc_builtin_macro").exists(); + Some(MacroCall { name, path, is_export, is_builtin, is_local_inner, ast_id }) + } + + fn lower_extern_block(&mut self, block: &ast::ExternBlock) -> Vec> { + block.extern_item_list().map_or(Vec::new(), |list| { + list.extern_items() + .filter_map(|item| match item { + ast::ExternItem::FnDef(ast) => self.lower_function(&ast).map(Either::Left), + ast::ExternItem::StaticDef(ast) => self.lower_static(&ast).map(Either::Right), + }) + .collect() + }) + } + + fn lower_generic_params( + &mut self, + _item: &impl ast::TypeParamsOwner, + ) -> generics::GenericParams { + // TODO + generics::GenericParams { types: Arena::new(), where_predicates: Vec::new() } + } + + fn lower_attrs(&self, item: &impl ast::AttrsOwner) -> Attrs { + Attrs::new(item, &self.hygiene) + } + fn lower_visibility(&self, item: &impl ast::VisibilityOwner) -> RawVisibility { + RawVisibility::from_ast_with_hygiene(item.visibility(), &self.hygiene) + } + fn lower_type_ref(&self, type_ref: &ast::TypeRef) -> TypeRef { + TypeRef::from_ast(&self.body_ctx, type_ref.clone()) + } + fn lower_type_ref_opt(&self, type_ref: Option) -> TypeRef { + TypeRef::from_ast_opt(&self.body_ctx, type_ref) + } + + fn next_field_idx(&self) -> Idx { + Idx::from_raw(RawId::from(self.tree.fields.len() as u32)) + } + fn next_variant_idx(&self) -> Idx { + Idx::from_raw(RawId::from(self.tree.variants.len() as u32)) + } +} + +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(); + let binding = + AssociatedTypeBinding { name: name![Output], type_ref: Some(orig), bounds: Vec::new() }; + last.bindings.push(binding); + generic_args.push(Some(Arc::new(last))); + + Path::from_known_path(path, generic_args) +} -- cgit v1.2.3 From 864b650f92388f4e82d130713b2de9afe637102f Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 16 Jun 2020 19:20:29 +0200 Subject: ItemTree: use a newtyped ID --- crates/ra_hir_def/src/item_tree/lower.rs | 40 ++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 18 deletions(-) (limited to 'crates/ra_hir_def/src/item_tree/lower.rs') diff --git a/crates/ra_hir_def/src/item_tree/lower.rs b/crates/ra_hir_def/src/item_tree/lower.rs index d123a7310..0c9454848 100644 --- a/crates/ra_hir_def/src/item_tree/lower.rs +++ b/crates/ra_hir_def/src/item_tree/lower.rs @@ -5,6 +5,10 @@ use ra_syntax::ast::{self, ModuleItemOwner}; use smallvec::SmallVec; use std::sync::Arc; +fn id(index: Idx) -> FileItemTreeId { + FileItemTreeId { index, _p: PhantomData } +} + struct ModItems(SmallVec<[ModItem; 1]>); impl From for ModItems @@ -38,54 +42,54 @@ impl Ctx { let attrs = Attrs::new(item, &self.hygiene); let items = match item { ast::ModuleItem::StructDef(ast) => { - self.lower_struct(ast).map(|data| self.tree.structs.alloc(data).into()) + self.lower_struct(ast).map(|data| id(self.tree.structs.alloc(data)).into()) } ast::ModuleItem::UnionDef(ast) => { - self.lower_union(ast).map(|data| self.tree.unions.alloc(data).into()) + self.lower_union(ast).map(|data| id(self.tree.unions.alloc(data)).into()) } ast::ModuleItem::EnumDef(ast) => { - self.lower_enum(ast).map(|data| self.tree.enums.alloc(data).into()) + self.lower_enum(ast).map(|data| id(self.tree.enums.alloc(data)).into()) } ast::ModuleItem::FnDef(ast) => { - self.lower_function(ast).map(|data| self.tree.functions.alloc(data).into()) + self.lower_function(ast).map(|data| id(self.tree.functions.alloc(data)).into()) } ast::ModuleItem::TypeAliasDef(ast) => { - self.lower_type_alias(ast).map(|data| self.tree.type_aliases.alloc(data).into()) + self.lower_type_alias(ast).map(|data| id(self.tree.type_aliases.alloc(data)).into()) } ast::ModuleItem::StaticDef(ast) => { - self.lower_static(ast).map(|data| self.tree.statics.alloc(data).into()) + self.lower_static(ast).map(|data| id(self.tree.statics.alloc(data)).into()) } ast::ModuleItem::ConstDef(ast) => { let data = self.lower_const(ast); - Some(self.tree.consts.alloc(data).into()) + Some(id(self.tree.consts.alloc(data)).into()) } ast::ModuleItem::Module(ast) => { - self.lower_module(ast).map(|data| self.tree.mods.alloc(data).into()) + self.lower_module(ast).map(|data| id(self.tree.mods.alloc(data)).into()) } ast::ModuleItem::TraitDef(ast) => { - self.lower_trait(ast).map(|data| self.tree.traits.alloc(data).into()) + self.lower_trait(ast).map(|data| id(self.tree.traits.alloc(data)).into()) } ast::ModuleItem::ImplDef(ast) => { - self.lower_impl(ast).map(|data| self.tree.impls.alloc(data).into()) + self.lower_impl(ast).map(|data| id(self.tree.impls.alloc(data)).into()) } ast::ModuleItem::UseItem(ast) => Some(ModItems( self.lower_use(ast) .into_iter() - .map(|data| self.tree.imports.alloc(data).into()) + .map(|data| id(self.tree.imports.alloc(data)).into()) .collect::>(), )), ast::ModuleItem::ExternCrateItem(ast) => { - self.lower_extern_crate(ast).map(|data| self.tree.imports.alloc(data).into()) + self.lower_extern_crate(ast).map(|data| id(self.tree.imports.alloc(data)).into()) } ast::ModuleItem::MacroCall(ast) => { - self.lower_macro_call(ast).map(|data| self.tree.macro_calls.alloc(data).into()) + self.lower_macro_call(ast).map(|data| id(self.tree.macro_calls.alloc(data)).into()) } ast::ModuleItem::ExternBlock(ast) => Some(ModItems( self.lower_extern_block(ast) .into_iter() .map(|item| match item { - Either::Left(func) => self.tree.functions.alloc(func).into(), - Either::Right(statik) => self.tree.statics.alloc(statik).into(), + Either::Left(func) => id(self.tree.functions.alloc(func)).into(), + Either::Right(statik) => id(self.tree.statics.alloc(statik)).into(), }) .collect::>(), )), @@ -103,14 +107,14 @@ impl Ctx { fn lower_assoc_item(&mut self, item: &ast::AssocItem) -> Option { match item { ast::AssocItem::FnDef(ast) => { - self.lower_function(ast).map(|data| self.tree.functions.alloc(data).into()) + self.lower_function(ast).map(|data| id(self.tree.functions.alloc(data)).into()) } ast::AssocItem::TypeAliasDef(ast) => { - self.lower_type_alias(ast).map(|data| self.tree.type_aliases.alloc(data).into()) + self.lower_type_alias(ast).map(|data| id(self.tree.type_aliases.alloc(data)).into()) } ast::AssocItem::ConstDef(ast) => { let data = self.lower_const(ast); - Some(self.tree.consts.alloc(data).into()) + Some(id(self.tree.consts.alloc(data)).into()) } } } -- cgit v1.2.3 From c12f7b610be49901190cde994dfe4f594150dbf9 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 17 Jun 2020 12:24:05 +0200 Subject: Lower generics --- crates/ra_hir_def/src/item_tree/lower.rs | 80 ++++++++++++++++++++++++++------ 1 file changed, 66 insertions(+), 14 deletions(-) (limited to 'crates/ra_hir_def/src/item_tree/lower.rs') diff --git a/crates/ra_hir_def/src/item_tree/lower.rs b/crates/ra_hir_def/src/item_tree/lower.rs index 0c9454848..737a69c30 100644 --- a/crates/ra_hir_def/src/item_tree/lower.rs +++ b/crates/ra_hir_def/src/item_tree/lower.rs @@ -1,6 +1,12 @@ +//! AST -> `ItemTree` lowering code. + use super::*; -use crate::attr::Attrs; +use crate::{ + attr::Attrs, + generics::{GenericParams, TypeParamData, TypeParamProvenance}, +}; use hir_expand::{ast_id_map::AstIdMap, hygiene::Hygiene, HirFileId}; +use ra_arena::map::ArenaMap; use ra_syntax::ast::{self, ModuleItemOwner}; use smallvec::SmallVec; use std::sync::Arc; @@ -123,7 +129,7 @@ impl Ctx { let attrs = self.lower_attrs(strukt); let visibility = self.lower_visibility(strukt); let name = strukt.name()?.as_name(); - let generic_params = self.lower_generic_params(strukt); + let generic_params = self.lower_generic_params(GenericsOwner::Struct, strukt); let fields = self.lower_fields(&strukt.kind()); let ast_id = self.source_ast_id_map.ast_id(strukt); let kind = match strukt.kind() { @@ -191,7 +197,7 @@ impl Ctx { let attrs = self.lower_attrs(union); let visibility = self.lower_visibility(union); let name = union.name()?.as_name(); - let generic_params = self.lower_generic_params(union); + let generic_params = self.lower_generic_params(GenericsOwner::Union, union); let fields = match union.record_field_def_list() { Some(record_field_def_list) => { self.lower_fields(&StructKind::Record(record_field_def_list)) @@ -207,7 +213,7 @@ impl Ctx { let attrs = self.lower_attrs(enum_); let visibility = self.lower_visibility(enum_); let name = enum_.name()?.as_name(); - let generic_params = self.lower_generic_params(enum_); + let generic_params = self.lower_generic_params(GenericsOwner::Enum, enum_); let variants = match &enum_.variant_list() { Some(variant_list) => self.lower_variants(variant_list), None => self.next_variant_idx()..self.next_variant_idx(), @@ -239,7 +245,6 @@ impl Ctx { let attrs = self.lower_attrs(func); let visibility = self.lower_visibility(func); let name = func.name()?.as_name(); - let generic_params = self.lower_generic_params(func); let mut params = Vec::new(); let mut has_self_param = false; @@ -281,16 +286,17 @@ impl Ctx { }; let ast_id = self.source_ast_id_map.ast_id(func); - let res = Function { + let mut res = Function { name, attrs, visibility, - generic_params, + generic_params: GenericParams::default(), has_self_param, params, ret_type, ast_id, }; + res.generic_params = self.lower_generic_params(GenericsOwner::Function(&res), func); Some(res) } @@ -298,7 +304,7 @@ impl Ctx { let name = type_alias.name()?.as_name(); let type_ref = type_alias.type_ref().map(|it| self.lower_type_ref(&it)); let visibility = self.lower_visibility(type_alias); - let generic_params = self.lower_generic_params(type_alias); + let generic_params = self.lower_generic_params(GenericsOwner::TypeAlias, type_alias); let ast_id = self.source_ast_id_map.ast_id(type_alias); let res = TypeAlias { name, visibility, generic_params, type_ref, ast_id }; Some(res) @@ -349,7 +355,7 @@ impl Ctx { fn lower_trait(&mut self, trait_def: &ast::TraitDef) -> Option { let name = trait_def.name()?.as_name(); let visibility = self.lower_visibility(trait_def); - let generic_params = self.lower_generic_params(trait_def); + let generic_params = self.lower_generic_params(GenericsOwner::Trait(trait_def), trait_def); let auto = trait_def.auto_token().is_some(); let items = trait_def.item_list().map(|list| { // FIXME: Does not handle macros @@ -367,7 +373,7 @@ impl Ctx { } fn lower_impl(&mut self, impl_def: &ast::ImplDef) -> Option { - let generic_params = self.lower_generic_params(impl_def); + let generic_params = self.lower_generic_params(GenericsOwner::Impl, impl_def); let target_trait = impl_def.target_trait().map(|tr| self.lower_type_ref(&tr)); let target_type = self.lower_type_ref(&impl_def.target_type()?); let is_negative = impl_def.excl_token().is_some(); @@ -465,10 +471,43 @@ impl Ctx { fn lower_generic_params( &mut self, - _item: &impl ast::TypeParamsOwner, - ) -> generics::GenericParams { - // TODO - generics::GenericParams { types: Arena::new(), where_predicates: Vec::new() } + owner: GenericsOwner<'_>, + node: &impl ast::TypeParamsOwner, + ) -> GenericParams { + let mut sm = &mut ArenaMap::default(); + let mut generics = GenericParams::default(); + match owner { + GenericsOwner::Function(func) => { + generics.fill(&self.body_ctx, sm, node); + // lower `impl Trait` in arguments + for param in &func.params { + generics.fill_implicit_impl_trait_args(param); + } + } + GenericsOwner::Struct + | GenericsOwner::Enum + | GenericsOwner::Union + | GenericsOwner::TypeAlias => { + generics.fill(&self.body_ctx, sm, node); + } + GenericsOwner::Trait(trait_def) => { + // traits get the Self type as an implicit first type parameter + let self_param_id = generics.types.alloc(TypeParamData { + name: Some(name![Self]), + default: None, + provenance: TypeParamProvenance::TraitSelf, + }); + sm.insert(self_param_id, Either::Left(trait_def.clone())); + // add super traits as bounds on Self + // i.e., trait Foo: Bar is equivalent to trait Foo where Self: Bar + let self_param = TypeRef::Path(name![Self].into()); + generics.fill_bounds(&self.body_ctx, trait_def, self_param); + + generics.fill(&self.body_ctx, &mut sm, node); + } + GenericsOwner::Impl => {} + } + generics } fn lower_attrs(&self, item: &impl ast::AttrsOwner) -> Attrs { @@ -503,3 +542,16 @@ fn desugar_future_path(orig: TypeRef) -> Path { Path::from_known_path(path, generic_args) } + +enum GenericsOwner<'a> { + /// We need access to the partially-lowered `Function` for lowering `impl Trait` in argument + /// position. + Function(&'a Function), + Struct, + Enum, + Union, + /// The `TraitDef` is needed to fill the source map for the implicit `Self` parameter. + Trait(&'a ast::TraitDef), + TypeAlias, + Impl, +} -- cgit v1.2.3 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/item_tree/lower.rs | 271 ++++++++++++++++++------------- 1 file changed, 155 insertions(+), 116 deletions(-) (limited to 'crates/ra_hir_def/src/item_tree/lower.rs') diff --git a/crates/ra_hir_def/src/item_tree/lower.rs b/crates/ra_hir_def/src/item_tree/lower.rs index 737a69c30..f2b8a9418 100644 --- a/crates/ra_hir_def/src/item_tree/lower.rs +++ b/crates/ra_hir_def/src/item_tree/lower.rs @@ -7,9 +7,12 @@ use crate::{ }; use hir_expand::{ast_id_map::AstIdMap, hygiene::Hygiene, HirFileId}; use ra_arena::map::ArenaMap; -use ra_syntax::ast::{self, ModuleItemOwner}; +use ra_syntax::{ + ast::{self, ModuleItemOwner}, + SyntaxNode, +}; use smallvec::SmallVec; -use std::sync::Arc; +use std::{mem, sync::Arc}; fn id(index: Idx) -> FileItemTreeId { FileItemTreeId { index, _p: PhantomData } @@ -27,78 +30,81 @@ where } pub(super) struct Ctx { - pub tree: ItemTree, - pub hygiene: Hygiene, - pub file: HirFileId, - pub source_ast_id_map: Arc, - pub body_ctx: crate::body::LowerCtx, + tree: ItemTree, + hygiene: Hygiene, + file: HirFileId, + source_ast_id_map: Arc, + body_ctx: crate::body::LowerCtx, + inner_items: Vec, } impl Ctx { + pub(super) fn new(db: &dyn DefDatabase, hygiene: Hygiene, file: HirFileId) -> Self { + Self { + tree: ItemTree::empty(file), + hygiene, + file, + source_ast_id_map: db.ast_id_map(file), + body_ctx: crate::body::LowerCtx::new(db, file), + inner_items: Vec::new(), + } + } + pub(super) fn lower(mut self, item_owner: &dyn ModuleItemOwner) -> ItemTree { self.tree.top_level = item_owner .items() - .flat_map(|item| self.lower_mod_item(&item)) + .flat_map(|item| self.lower_mod_item(&item, false)) .flat_map(|items| items.0) .collect(); self.tree } - fn lower_mod_item(&mut self, item: &ast::ModuleItem) -> Option { + fn lower_mod_item(&mut self, item: &ast::ModuleItem, inner: bool) -> Option { + assert!(inner || self.inner_items.is_empty()); + + // Collect inner items for 1-to-1-lowered items. + match item { + ast::ModuleItem::StructDef(_) + | ast::ModuleItem::UnionDef(_) + | ast::ModuleItem::EnumDef(_) + | ast::ModuleItem::FnDef(_) + | ast::ModuleItem::TypeAliasDef(_) + | ast::ModuleItem::ConstDef(_) + | ast::ModuleItem::StaticDef(_) + | ast::ModuleItem::MacroCall(_) => self.collect_inner_items(item.syntax()), + + // These are handled in their respective `lower_X` method (since we can't just blindly + // walk them). + ast::ModuleItem::TraitDef(_) + | ast::ModuleItem::ImplDef(_) + | ast::ModuleItem::ExternBlock(_) => {} + + // These don't have inner items. + ast::ModuleItem::Module(_) + | ast::ModuleItem::ExternCrateItem(_) + | ast::ModuleItem::UseItem(_) => {} + }; + let attrs = Attrs::new(item, &self.hygiene); let items = match item { - ast::ModuleItem::StructDef(ast) => { - self.lower_struct(ast).map(|data| id(self.tree.structs.alloc(data)).into()) - } - ast::ModuleItem::UnionDef(ast) => { - self.lower_union(ast).map(|data| id(self.tree.unions.alloc(data)).into()) - } - ast::ModuleItem::EnumDef(ast) => { - self.lower_enum(ast).map(|data| id(self.tree.enums.alloc(data)).into()) - } - ast::ModuleItem::FnDef(ast) => { - self.lower_function(ast).map(|data| id(self.tree.functions.alloc(data)).into()) - } - ast::ModuleItem::TypeAliasDef(ast) => { - self.lower_type_alias(ast).map(|data| id(self.tree.type_aliases.alloc(data)).into()) - } - ast::ModuleItem::StaticDef(ast) => { - self.lower_static(ast).map(|data| id(self.tree.statics.alloc(data)).into()) - } - ast::ModuleItem::ConstDef(ast) => { - let data = self.lower_const(ast); - Some(id(self.tree.consts.alloc(data)).into()) - } - ast::ModuleItem::Module(ast) => { - self.lower_module(ast).map(|data| id(self.tree.mods.alloc(data)).into()) - } - ast::ModuleItem::TraitDef(ast) => { - self.lower_trait(ast).map(|data| id(self.tree.traits.alloc(data)).into()) - } - ast::ModuleItem::ImplDef(ast) => { - self.lower_impl(ast).map(|data| id(self.tree.impls.alloc(data)).into()) - } + ast::ModuleItem::StructDef(ast) => self.lower_struct(ast).map(Into::into), + ast::ModuleItem::UnionDef(ast) => self.lower_union(ast).map(Into::into), + ast::ModuleItem::EnumDef(ast) => self.lower_enum(ast).map(Into::into), + ast::ModuleItem::FnDef(ast) => self.lower_function(ast).map(Into::into), + ast::ModuleItem::TypeAliasDef(ast) => self.lower_type_alias(ast).map(Into::into), + ast::ModuleItem::StaticDef(ast) => self.lower_static(ast).map(Into::into), + ast::ModuleItem::ConstDef(ast) => Some(self.lower_const(ast).into()), + ast::ModuleItem::Module(ast) => self.lower_module(ast).map(Into::into), + ast::ModuleItem::TraitDef(ast) => self.lower_trait(ast).map(Into::into), + ast::ModuleItem::ImplDef(ast) => self.lower_impl(ast).map(Into::into), ast::ModuleItem::UseItem(ast) => Some(ModItems( - self.lower_use(ast) - .into_iter() - .map(|data| id(self.tree.imports.alloc(data)).into()) - .collect::>(), + self.lower_use(ast).into_iter().map(Into::into).collect::>(), )), - ast::ModuleItem::ExternCrateItem(ast) => { - self.lower_extern_crate(ast).map(|data| id(self.tree.imports.alloc(data)).into()) + ast::ModuleItem::ExternCrateItem(ast) => self.lower_extern_crate(ast).map(Into::into), + ast::ModuleItem::MacroCall(ast) => self.lower_macro_call(ast).map(Into::into), + ast::ModuleItem::ExternBlock(ast) => { + Some(ModItems(self.lower_extern_block(ast).into_iter().collect::>())) } - ast::ModuleItem::MacroCall(ast) => { - self.lower_macro_call(ast).map(|data| id(self.tree.macro_calls.alloc(data)).into()) - } - ast::ModuleItem::ExternBlock(ast) => Some(ModItems( - self.lower_extern_block(ast) - .into_iter() - .map(|item| match item { - Either::Left(func) => id(self.tree.functions.alloc(func)).into(), - Either::Right(statik) => id(self.tree.statics.alloc(statik)).into(), - }) - .collect::>(), - )), }; if !attrs.is_empty() { @@ -110,22 +116,28 @@ impl Ctx { items } - fn lower_assoc_item(&mut self, item: &ast::AssocItem) -> Option { + fn collect_inner_items(&mut self, container: &SyntaxNode) { + let mut inner_items = mem::replace(&mut self.tree.inner_items, FxHashMap::default()); + inner_items.extend( + container.descendants().skip(1).filter_map(ast::ModuleItem::cast).filter_map(|item| { + let ast_id = self.source_ast_id_map.ast_id(&item); + Some((ast_id, self.lower_mod_item(&item, true)?.0)) + }), + ); + self.tree.inner_items = inner_items; + } + + fn lower_assoc_item(&mut self, item: &ast::ModuleItem) -> Option { match item { - ast::AssocItem::FnDef(ast) => { - self.lower_function(ast).map(|data| id(self.tree.functions.alloc(data)).into()) - } - ast::AssocItem::TypeAliasDef(ast) => { - self.lower_type_alias(ast).map(|data| id(self.tree.type_aliases.alloc(data)).into()) - } - ast::AssocItem::ConstDef(ast) => { - let data = self.lower_const(ast); - Some(id(self.tree.consts.alloc(data)).into()) - } + ast::ModuleItem::FnDef(ast) => self.lower_function(ast).map(Into::into), + ast::ModuleItem::TypeAliasDef(ast) => self.lower_type_alias(ast).map(Into::into), + ast::ModuleItem::ConstDef(ast) => Some(self.lower_const(ast).into()), + ast::ModuleItem::MacroCall(ast) => self.lower_macro_call(ast).map(Into::into), + _ => None, } } - fn lower_struct(&mut self, strukt: &ast::StructDef) -> Option { + fn lower_struct(&mut self, strukt: &ast::StructDef) -> Option> { let attrs = self.lower_attrs(strukt); let visibility = self.lower_visibility(strukt); let name = strukt.name()?.as_name(); @@ -138,7 +150,7 @@ impl Ctx { ast::StructKind::Unit => StructDefKind::Unit, }; let res = Struct { name, attrs, visibility, generic_params, fields, ast_id, kind }; - Some(res) + Some(id(self.tree.structs.alloc(res))) } fn lower_fields(&mut self, strukt_kind: &ast::StructKind) -> Fields { @@ -193,7 +205,7 @@ impl Ctx { Some(res) } - fn lower_union(&mut self, union: &ast::UnionDef) -> Option { + fn lower_union(&mut self, union: &ast::UnionDef) -> Option> { let attrs = self.lower_attrs(union); let visibility = self.lower_visibility(union); let name = union.name()?.as_name(); @@ -206,10 +218,10 @@ impl Ctx { }; let ast_id = self.source_ast_id_map.ast_id(union); let res = Union { name, attrs, visibility, generic_params, fields, ast_id }; - Some(res) + Some(id(self.tree.unions.alloc(res))) } - fn lower_enum(&mut self, enum_: &ast::EnumDef) -> Option { + fn lower_enum(&mut self, enum_: &ast::EnumDef) -> Option> { let attrs = self.lower_attrs(enum_); let visibility = self.lower_visibility(enum_); let name = enum_.name()?.as_name(); @@ -220,7 +232,7 @@ impl Ctx { }; let ast_id = self.source_ast_id_map.ast_id(enum_); let res = Enum { name, attrs, visibility, generic_params, variants, ast_id }; - Some(res) + Some(id(self.tree.enums.alloc(res))) } fn lower_variants(&mut self, variants: &ast::EnumVariantList) -> Range> { @@ -241,7 +253,7 @@ impl Ctx { Some(res) } - fn lower_function(&mut self, func: &ast::FnDef) -> Option { + fn lower_function(&mut self, func: &ast::FnDef) -> Option> { let attrs = self.lower_attrs(func); let visibility = self.lower_visibility(func); let name = func.name()?.as_name(); @@ -297,37 +309,42 @@ impl Ctx { ast_id, }; res.generic_params = self.lower_generic_params(GenericsOwner::Function(&res), func); - Some(res) + + Some(id(self.tree.functions.alloc(res))) } - fn lower_type_alias(&mut self, type_alias: &ast::TypeAliasDef) -> Option { + fn lower_type_alias( + &mut self, + type_alias: &ast::TypeAliasDef, + ) -> Option> { let name = type_alias.name()?.as_name(); let type_ref = type_alias.type_ref().map(|it| self.lower_type_ref(&it)); let visibility = self.lower_visibility(type_alias); let generic_params = self.lower_generic_params(GenericsOwner::TypeAlias, type_alias); let ast_id = self.source_ast_id_map.ast_id(type_alias); let res = TypeAlias { name, visibility, generic_params, type_ref, ast_id }; - Some(res) + Some(id(self.tree.type_aliases.alloc(res))) } - fn lower_static(&mut self, static_: &ast::StaticDef) -> Option { + fn lower_static(&mut self, static_: &ast::StaticDef) -> Option> { let name = static_.name()?.as_name(); let type_ref = self.lower_type_ref_opt(static_.ascribed_type()); let visibility = self.lower_visibility(static_); let ast_id = self.source_ast_id_map.ast_id(static_); let res = Static { name, visibility, type_ref, ast_id }; - Some(res) + Some(id(self.tree.statics.alloc(res))) } - fn lower_const(&mut self, konst: &ast::ConstDef) -> Const { + fn lower_const(&mut self, konst: &ast::ConstDef) -> FileItemTreeId { let name = konst.name().map(|it| it.as_name()); let type_ref = self.lower_type_ref_opt(konst.ascribed_type()); let visibility = self.lower_visibility(konst); let ast_id = self.source_ast_id_map.ast_id(konst); - Const { name, visibility, type_ref, ast_id } + let res = Const { name, visibility, type_ref, ast_id }; + id(self.tree.consts.alloc(res)) } - fn lower_module(&mut self, module: &ast::Module) -> Option { + fn lower_module(&mut self, module: &ast::Module) -> Option> { let name = module.name()?.as_name(); let visibility = self.lower_visibility(module); let kind = if module.semicolon_token().is_some() { @@ -338,7 +355,7 @@ impl Ctx { .item_list() .map(|list| { list.items() - .flat_map(|item| self.lower_mod_item(&item)) + .flat_map(|item| self.lower_mod_item(&item, false)) .flat_map(|items| items.0) .collect() }) @@ -349,90 +366,101 @@ impl Ctx { } }; let ast_id = self.source_ast_id_map.ast_id(module); - Some(Mod { name, visibility, kind, ast_id }) + let res = Mod { name, visibility, kind, ast_id }; + Some(id(self.tree.mods.alloc(res))) } - fn lower_trait(&mut self, trait_def: &ast::TraitDef) -> Option { + fn lower_trait(&mut self, trait_def: &ast::TraitDef) -> Option> { let name = trait_def.name()?.as_name(); let visibility = self.lower_visibility(trait_def); let generic_params = self.lower_generic_params(GenericsOwner::Trait(trait_def), trait_def); let auto = trait_def.auto_token().is_some(); let items = trait_def.item_list().map(|list| { - // FIXME: Does not handle macros - list.assoc_items().flat_map(|item| self.lower_assoc_item(&item)).collect() + list.items() + .flat_map(|item| { + self.collect_inner_items(item.syntax()); + self.lower_assoc_item(&item) + }) + .collect() }); let ast_id = self.source_ast_id_map.ast_id(trait_def); - Some(Trait { + let res = Trait { name, visibility, generic_params, auto, items: items.unwrap_or_default(), ast_id, - }) + }; + Some(id(self.tree.traits.alloc(res))) } - fn lower_impl(&mut self, impl_def: &ast::ImplDef) -> Option { + fn lower_impl(&mut self, impl_def: &ast::ImplDef) -> Option> { let generic_params = self.lower_generic_params(GenericsOwner::Impl, impl_def); let target_trait = impl_def.target_trait().map(|tr| self.lower_type_ref(&tr)); let target_type = self.lower_type_ref(&impl_def.target_type()?); let is_negative = impl_def.excl_token().is_some(); + + // We cannot use `assoc_items()` here as that does not include macro calls. let items = impl_def .item_list()? - .assoc_items() - .filter_map(|item| self.lower_assoc_item(&item)) + .items() + .filter_map(|item| { + self.collect_inner_items(item.syntax()); + let assoc = self.lower_assoc_item(&item)?; + Some(assoc) + }) .collect(); let ast_id = self.source_ast_id_map.ast_id(impl_def); - Some(Impl { generic_params, target_trait, target_type, is_negative, items, ast_id }) + let res = Impl { generic_params, target_trait, target_type, is_negative, items, ast_id }; + Some(id(self.tree.impls.alloc(res))) } - fn lower_use(&mut self, use_item: &ast::UseItem) -> Vec { + fn lower_use(&mut self, use_item: &ast::UseItem) -> Vec> { // FIXME: cfg_attr let is_prelude = use_item.has_atom_attr("prelude_import"); let visibility = self.lower_visibility(use_item); + let ast_id = self.source_ast_id_map.ast_id(use_item); // Every use item can expand to many `Import`s. let mut imports = Vec::new(); + let tree = &mut self.tree; ModPath::expand_use_item( InFile::new(self.file, use_item.clone()), &self.hygiene, |path, _tree, is_glob, alias| { - imports.push(Import { + imports.push(id(tree.imports.alloc(Import { path, alias, visibility: visibility.clone(), is_glob, is_prelude, - is_extern_crate: false, - is_macro_use: false, - }); + ast_id, + }))); }, ); imports } - fn lower_extern_crate(&mut self, extern_crate: &ast::ExternCrateItem) -> Option { + fn lower_extern_crate( + &mut self, + extern_crate: &ast::ExternCrateItem, + ) -> Option> { let path = ModPath::from_name_ref(&extern_crate.name_ref()?); let alias = extern_crate.alias().map(|a| { a.name().map(|it| it.as_name()).map_or(ImportAlias::Underscore, ImportAlias::Alias) }); let visibility = self.lower_visibility(extern_crate); + let ast_id = self.source_ast_id_map.ast_id(extern_crate); // FIXME: cfg_attr let is_macro_use = extern_crate.has_atom_attr("macro_use"); - Some(Import { - path, - alias, - visibility, - is_glob: false, - is_prelude: false, - is_extern_crate: true, - is_macro_use, - }) + let res = ExternCrate { path, alias, visibility, is_macro_use, ast_id }; + Some(id(self.tree.extern_crates.alloc(res))) } - fn lower_macro_call(&mut self, m: &ast::MacroCall) -> Option { + fn lower_macro_call(&mut self, m: &ast::MacroCall) -> Option> { let name = m.name().map(|it| it.as_name()); let attrs = Attrs::new(m, &self.hygiene); let path = ModPath::from_src(m.path()?, &self.hygiene)?; @@ -455,15 +483,26 @@ impl Ctx { }; let is_builtin = attrs.by_key("rustc_builtin_macro").exists(); - Some(MacroCall { name, path, is_export, is_builtin, is_local_inner, ast_id }) + let res = MacroCall { name, path, is_export, is_builtin, is_local_inner, ast_id }; + Some(id(self.tree.macro_calls.alloc(res))) } - fn lower_extern_block(&mut self, block: &ast::ExternBlock) -> Vec> { + fn lower_extern_block(&mut self, block: &ast::ExternBlock) -> Vec { block.extern_item_list().map_or(Vec::new(), |list| { list.extern_items() - .filter_map(|item| match item { - ast::ExternItem::FnDef(ast) => self.lower_function(&ast).map(Either::Left), - ast::ExternItem::StaticDef(ast) => self.lower_static(&ast).map(Either::Right), + .filter_map(|item| { + self.collect_inner_items(item.syntax()); + let id = match item { + ast::ExternItem::FnDef(ast) => { + let func = self.lower_function(&ast)?; + func.into() + } + ast::ExternItem::StaticDef(ast) => { + let statik = self.lower_static(&ast)?; + statik.into() + } + }; + Some(id) }) .collect() }) -- 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/item_tree/lower.rs | 51 ++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 9 deletions(-) (limited to 'crates/ra_hir_def/src/item_tree/lower.rs') diff --git a/crates/ra_hir_def/src/item_tree/lower.rs b/crates/ra_hir_def/src/item_tree/lower.rs index f2b8a9418..42af8bb5e 100644 --- a/crates/ra_hir_def/src/item_tree/lower.rs +++ b/crates/ra_hir_def/src/item_tree/lower.rs @@ -36,6 +36,7 @@ pub(super) struct Ctx { source_ast_id_map: Arc, body_ctx: crate::body::LowerCtx, inner_items: Vec, + forced_visibility: Option, } impl Ctx { @@ -47,6 +48,7 @@ impl Ctx { source_ast_id_map: db.ast_id_map(file), body_ctx: crate::body::LowerCtx::new(db, file), inner_items: Vec::new(), + forced_visibility: None, } } @@ -117,6 +119,7 @@ impl Ctx { } fn collect_inner_items(&mut self, container: &SyntaxNode) { + let forced_vis = self.forced_visibility.take(); let mut inner_items = mem::replace(&mut self.tree.inner_items, FxHashMap::default()); inner_items.extend( container.descendants().skip(1).filter_map(ast::ModuleItem::cast).filter_map(|item| { @@ -125,6 +128,7 @@ impl Ctx { }), ); self.tree.inner_items = inner_items; + self.forced_visibility = forced_vis; } fn lower_assoc_item(&mut self, item: &ast::ModuleItem) -> Option { @@ -304,6 +308,7 @@ impl Ctx { visibility, generic_params: GenericParams::default(), has_self_param, + is_unsafe: func.unsafe_token().is_some(), params, ret_type, ast_id, @@ -320,9 +325,10 @@ impl Ctx { let name = type_alias.name()?.as_name(); let type_ref = type_alias.type_ref().map(|it| self.lower_type_ref(&it)); let visibility = self.lower_visibility(type_alias); + let bounds = self.lower_type_bounds(type_alias); let generic_params = self.lower_generic_params(GenericsOwner::TypeAlias, type_alias); let ast_id = self.source_ast_id_map.ast_id(type_alias); - let res = TypeAlias { name, visibility, generic_params, type_ref, ast_id }; + let res = TypeAlias { name, visibility, bounds, generic_params, type_ref, ast_id }; Some(id(self.tree.type_aliases.alloc(res))) } @@ -330,8 +336,9 @@ impl Ctx { let name = static_.name()?.as_name(); let type_ref = self.lower_type_ref_opt(static_.ascribed_type()); let visibility = self.lower_visibility(static_); + let mutable = static_.mut_token().is_some(); let ast_id = self.source_ast_id_map.ast_id(static_); - let res = Static { name, visibility, type_ref, ast_id }; + let res = Static { name, visibility, mutable, type_ref, ast_id }; Some(id(self.tree.statics.alloc(res))) } @@ -376,12 +383,14 @@ impl Ctx { let generic_params = self.lower_generic_params(GenericsOwner::Trait(trait_def), trait_def); let auto = trait_def.auto_token().is_some(); let items = trait_def.item_list().map(|list| { - list.items() - .flat_map(|item| { - self.collect_inner_items(item.syntax()); - self.lower_assoc_item(&item) - }) - .collect() + self.with_inherited_visibility(visibility.clone(), |this| { + list.items() + .flat_map(|item| { + this.collect_inner_items(item.syntax()); + this.lower_assoc_item(&item) + }) + .collect() + }) }); let ast_id = self.source_ast_id_map.ast_id(trait_def); let res = Trait { @@ -549,11 +558,23 @@ impl Ctx { generics } + fn lower_type_bounds(&mut self, node: &impl ast::TypeBoundsOwner) -> Vec { + if let Some(bound_list) = node.type_bound_list() { + bound_list.bounds().map(|it| TypeBound::from_ast(&self.body_ctx, it)).collect() + } else { + Vec::new() + } + } + fn lower_attrs(&self, item: &impl ast::AttrsOwner) -> Attrs { Attrs::new(item, &self.hygiene) } fn lower_visibility(&self, item: &impl ast::VisibilityOwner) -> RawVisibility { - RawVisibility::from_ast_with_hygiene(item.visibility(), &self.hygiene) + if let Some(vis) = self.forced_visibility.as_ref() { + vis.clone() + } else { + RawVisibility::from_ast_with_hygiene(item.visibility(), &self.hygiene) + } } fn lower_type_ref(&self, type_ref: &ast::TypeRef) -> TypeRef { TypeRef::from_ast(&self.body_ctx, type_ref.clone()) @@ -562,6 +583,18 @@ impl Ctx { TypeRef::from_ast_opt(&self.body_ctx, type_ref) } + /// Forces the visibility `vis` to be used for all items lowered during execution of `f`. + fn with_inherited_visibility( + &mut self, + vis: RawVisibility, + f: impl FnOnce(&mut Self) -> R, + ) -> R { + let old = mem::replace(&mut self.forced_visibility, Some(vis)); + let res = f(self); + self.forced_visibility = old; + res + } + fn next_field_idx(&self) -> Idx { Idx::from_raw(RawId::from(self.tree.fields.len() as u32)) } -- cgit v1.2.3 From ffa0435050ae57c5171c19224ca6e9f8a4e3435d Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 22 Jun 2020 19:15:54 +0200 Subject: Make generics and attr queries use ItemTree Now it's fast --- crates/ra_hir_def/src/item_tree/lower.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir_def/src/item_tree/lower.rs') diff --git a/crates/ra_hir_def/src/item_tree/lower.rs b/crates/ra_hir_def/src/item_tree/lower.rs index 42af8bb5e..841c7a852 100644 --- a/crates/ra_hir_def/src/item_tree/lower.rs +++ b/crates/ra_hir_def/src/item_tree/lower.rs @@ -553,7 +553,12 @@ impl Ctx { generics.fill(&self.body_ctx, &mut sm, node); } - GenericsOwner::Impl => {} + GenericsOwner::Impl => { + // Note that we don't add `Self` here: in `impl`s, `Self` is not a + // type-parameter, but rather is a type-alias for impl's target + // type, so this is handled by the resolver. + generics.fill(&self.body_ctx, &mut sm, node); + } } generics } -- cgit v1.2.3 From 689e147c9dc416027fd36e94673431533df545f9 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 23 Jun 2020 13:46:38 +0200 Subject: Collect inner items in expression macros --- crates/ra_hir_def/src/item_tree/lower.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'crates/ra_hir_def/src/item_tree/lower.rs') diff --git a/crates/ra_hir_def/src/item_tree/lower.rs b/crates/ra_hir_def/src/item_tree/lower.rs index 841c7a852..3bb437e81 100644 --- a/crates/ra_hir_def/src/item_tree/lower.rs +++ b/crates/ra_hir_def/src/item_tree/lower.rs @@ -52,7 +52,7 @@ impl Ctx { } } - pub(super) fn lower(mut self, item_owner: &dyn ModuleItemOwner) -> ItemTree { + pub(super) fn lower_module_items(mut self, item_owner: &dyn ModuleItemOwner) -> ItemTree { self.tree.top_level = item_owner .items() .flat_map(|item| self.lower_mod_item(&item, false)) @@ -61,6 +61,11 @@ impl Ctx { self.tree } + pub(super) fn lower_inner_items(mut self, within: &SyntaxNode) -> ItemTree { + self.collect_inner_items(within); + self.tree + } + fn lower_mod_item(&mut self, item: &ast::ModuleItem, inner: bool) -> Option { assert!(inner || self.inner_items.is_empty()); -- 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/item_tree/lower.rs | 41 ++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 15 deletions(-) (limited to 'crates/ra_hir_def/src/item_tree/lower.rs') diff --git a/crates/ra_hir_def/src/item_tree/lower.rs b/crates/ra_hir_def/src/item_tree/lower.rs index 3bb437e81..88530af74 100644 --- a/crates/ra_hir_def/src/item_tree/lower.rs +++ b/crates/ra_hir_def/src/item_tree/lower.rs @@ -12,7 +12,7 @@ use ra_syntax::{ SyntaxNode, }; use smallvec::SmallVec; -use std::{mem, sync::Arc}; +use std::{collections::hash_map::Entry, mem, sync::Arc}; fn id(index: Idx) -> FileItemTreeId { FileItemTreeId { index, _p: PhantomData } @@ -116,13 +116,24 @@ impl Ctx { if !attrs.is_empty() { for item in items.iter().flat_map(|items| &items.0) { - self.tree.attrs.insert(*item, attrs.clone()); + self.add_attrs(*item, attrs.clone()); } } items } + fn add_attrs(&mut self, item: ModItem, attrs: Attrs) { + match self.tree.attrs.entry(item) { + Entry::Occupied(mut entry) => { + *entry.get_mut() = entry.get().merge(attrs); + } + Entry::Vacant(entry) => { + entry.insert(attrs); + } + } + } + fn collect_inner_items(&mut self, container: &SyntaxNode) { let forced_vis = self.forced_visibility.take(); let mut inner_items = mem::replace(&mut self.tree.inner_items, FxHashMap::default()); @@ -147,7 +158,6 @@ impl Ctx { } fn lower_struct(&mut self, strukt: &ast::StructDef) -> Option> { - let attrs = self.lower_attrs(strukt); let visibility = self.lower_visibility(strukt); let name = strukt.name()?.as_name(); let generic_params = self.lower_generic_params(GenericsOwner::Struct, strukt); @@ -158,7 +168,7 @@ impl Ctx { ast::StructKind::Tuple(_) => StructDefKind::Tuple, ast::StructKind::Unit => StructDefKind::Unit, }; - let res = Struct { name, attrs, visibility, generic_params, fields, ast_id, kind }; + let res = Struct { name, visibility, generic_params, fields, ast_id, kind }; Some(id(self.tree.structs.alloc(res))) } @@ -215,7 +225,6 @@ impl Ctx { } fn lower_union(&mut self, union: &ast::UnionDef) -> Option> { - let attrs = self.lower_attrs(union); let visibility = self.lower_visibility(union); let name = union.name()?.as_name(); let generic_params = self.lower_generic_params(GenericsOwner::Union, union); @@ -226,12 +235,11 @@ impl Ctx { None => Fields::Record(self.next_field_idx()..self.next_field_idx()), }; let ast_id = self.source_ast_id_map.ast_id(union); - let res = Union { name, attrs, visibility, generic_params, fields, ast_id }; + let res = Union { name, visibility, generic_params, fields, ast_id }; Some(id(self.tree.unions.alloc(res))) } fn lower_enum(&mut self, enum_: &ast::EnumDef) -> Option> { - let attrs = self.lower_attrs(enum_); let visibility = self.lower_visibility(enum_); let name = enum_.name()?.as_name(); let generic_params = self.lower_generic_params(GenericsOwner::Enum, enum_); @@ -240,7 +248,7 @@ impl Ctx { None => self.next_variant_idx()..self.next_variant_idx(), }; let ast_id = self.source_ast_id_map.ast_id(enum_); - let res = Enum { name, attrs, visibility, generic_params, variants, ast_id }; + let res = Enum { name, visibility, generic_params, variants, ast_id }; Some(id(self.tree.enums.alloc(res))) } @@ -263,7 +271,6 @@ impl Ctx { } fn lower_function(&mut self, func: &ast::FnDef) -> Option> { - let attrs = self.lower_attrs(func); let visibility = self.lower_visibility(func); let name = func.name()?.as_name(); @@ -309,7 +316,6 @@ impl Ctx { let ast_id = self.source_ast_id_map.ast_id(func); let mut res = Function { name, - attrs, visibility, generic_params: GenericParams::default(), has_self_param, @@ -390,9 +396,13 @@ impl Ctx { let items = trait_def.item_list().map(|list| { self.with_inherited_visibility(visibility.clone(), |this| { list.items() - .flat_map(|item| { + .filter_map(|item| { + let attrs = Attrs::new(&item, &this.hygiene); this.collect_inner_items(item.syntax()); - this.lower_assoc_item(&item) + this.lower_assoc_item(&item).map(|item| { + this.add_attrs(item.into(), attrs); + item + }) }) .collect() }) @@ -422,6 +432,8 @@ impl Ctx { .filter_map(|item| { self.collect_inner_items(item.syntax()); let assoc = self.lower_assoc_item(&item)?; + let attrs = Attrs::new(&item, &self.hygiene); + self.add_attrs(assoc.into(), attrs); Some(assoc) }) .collect(); @@ -506,6 +518,7 @@ impl Ctx { list.extern_items() .filter_map(|item| { self.collect_inner_items(item.syntax()); + let attrs = Attrs::new(&item, &self.hygiene); let id = match item { ast::ExternItem::FnDef(ast) => { let func = self.lower_function(&ast)?; @@ -516,6 +529,7 @@ impl Ctx { statik.into() } }; + self.add_attrs(id, attrs); Some(id) }) .collect() @@ -576,9 +590,6 @@ impl Ctx { } } - fn lower_attrs(&self, item: &impl ast::AttrsOwner) -> Attrs { - Attrs::new(item, &self.hygiene) - } fn lower_visibility(&self, item: &impl ast::VisibilityOwner) -> RawVisibility { if let Some(vis) = self.forced_visibility.as_ref() { vis.clone() -- cgit v1.2.3 From a0ad457575bdde24d65c3fbcd4b2819459ad0291 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 23 Jun 2020 18:28:47 +0200 Subject: if let else -> match --- crates/ra_hir_def/src/item_tree/lower.rs | 40 +++++++++++++++++--------------- 1 file changed, 21 insertions(+), 19 deletions(-) (limited to 'crates/ra_hir_def/src/item_tree/lower.rs') diff --git a/crates/ra_hir_def/src/item_tree/lower.rs b/crates/ra_hir_def/src/item_tree/lower.rs index 88530af74..b97927f27 100644 --- a/crates/ra_hir_def/src/item_tree/lower.rs +++ b/crates/ra_hir_def/src/item_tree/lower.rs @@ -278,17 +278,18 @@ impl Ctx { let mut has_self_param = false; if let Some(param_list) = func.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(&self.body_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) + let self_type = match self_param.ascribed_type() { + Some(type_ref) => TypeRef::from_ast(&self.body_ctx, type_ref), + None => { + 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) + } } } }; @@ -583,20 +584,21 @@ impl Ctx { } fn lower_type_bounds(&mut self, node: &impl ast::TypeBoundsOwner) -> Vec { - if let Some(bound_list) = node.type_bound_list() { - bound_list.bounds().map(|it| TypeBound::from_ast(&self.body_ctx, it)).collect() - } else { - Vec::new() + match node.type_bound_list() { + Some(bound_list) => { + bound_list.bounds().map(|it| TypeBound::from_ast(&self.body_ctx, it)).collect() + } + None => Vec::new(), } } fn lower_visibility(&self, item: &impl ast::VisibilityOwner) -> RawVisibility { - if let Some(vis) = self.forced_visibility.as_ref() { - vis.clone() - } else { - RawVisibility::from_ast_with_hygiene(item.visibility(), &self.hygiene) + match &self.forced_visibility { + Some(vis) => vis.clone(), + None => RawVisibility::from_ast_with_hygiene(item.visibility(), &self.hygiene), } } + fn lower_type_ref(&self, type_ref: &ast::TypeRef) -> TypeRef { TypeRef::from_ast(&self.body_ctx, type_ref.clone()) } -- 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/item_tree/lower.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir_def/src/item_tree/lower.rs') diff --git a/crates/ra_hir_def/src/item_tree/lower.rs b/crates/ra_hir_def/src/item_tree/lower.rs index b97927f27..733fcac7a 100644 --- a/crates/ra_hir_def/src/item_tree/lower.rs +++ b/crates/ra_hir_def/src/item_tree/lower.rs @@ -136,7 +136,7 @@ impl Ctx { fn collect_inner_items(&mut self, container: &SyntaxNode) { let forced_vis = self.forced_visibility.take(); - let mut inner_items = mem::replace(&mut self.tree.inner_items, FxHashMap::default()); + let mut inner_items = mem::take(&mut self.tree.inner_items); inner_items.extend( container.descendants().skip(1).filter_map(ast::ModuleItem::cast).filter_map(|item| { let ast_id = self.source_ast_id_map.ast_id(&item); -- cgit v1.2.3 From c019002d17185f4e8be54a978ab5d67bc632f518 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 23 Jun 2020 19:42:19 +0200 Subject: Slightly reduce ItemTree memory footprint --- crates/ra_hir_def/src/item_tree/lower.rs | 46 +++++++++++++++++++------------- 1 file changed, 27 insertions(+), 19 deletions(-) (limited to 'crates/ra_hir_def/src/item_tree/lower.rs') diff --git a/crates/ra_hir_def/src/item_tree/lower.rs b/crates/ra_hir_def/src/item_tree/lower.rs index 733fcac7a..7d28fe7c6 100644 --- a/crates/ra_hir_def/src/item_tree/lower.rs +++ b/crates/ra_hir_def/src/item_tree/lower.rs @@ -66,6 +66,10 @@ impl Ctx { self.tree } + fn data(&mut self) -> &mut ItemTreeData { + self.tree.data_mut() + } + fn lower_mod_item(&mut self, item: &ast::ModuleItem, inner: bool) -> Option { assert!(inner || self.inner_items.is_empty()); @@ -124,7 +128,7 @@ impl Ctx { } fn add_attrs(&mut self, item: ModItem, attrs: Attrs) { - match self.tree.attrs.entry(item) { + match self.tree.attrs.entry(AttrOwner::ModItem(item)) { Entry::Occupied(mut entry) => { *entry.get_mut() = entry.get().merge(attrs); } @@ -169,7 +173,7 @@ impl Ctx { ast::StructKind::Unit => StructDefKind::Unit, }; let res = Struct { name, visibility, generic_params, fields, ast_id, kind }; - Some(id(self.tree.structs.alloc(res))) + Some(id(self.data().structs.alloc(res))) } fn lower_fields(&mut self, strukt_kind: &ast::StructKind) -> Fields { @@ -190,7 +194,7 @@ impl Ctx { let start = self.next_field_idx(); for field in fields.fields() { if let Some(data) = self.lower_record_field(&field) { - self.tree.fields.alloc(data); + self.data().fields.alloc(data); } } let end = self.next_field_idx(); @@ -209,7 +213,7 @@ impl Ctx { let start = self.next_field_idx(); for (i, field) in fields.fields().enumerate() { if let Some(data) = self.lower_tuple_field(i, &field) { - self.tree.fields.alloc(data); + self.data().fields.alloc(data); } } let end = self.next_field_idx(); @@ -236,7 +240,7 @@ impl Ctx { }; let ast_id = self.source_ast_id_map.ast_id(union); let res = Union { name, visibility, generic_params, fields, ast_id }; - Some(id(self.tree.unions.alloc(res))) + Some(id(self.data().unions.alloc(res))) } fn lower_enum(&mut self, enum_: &ast::EnumDef) -> Option> { @@ -249,14 +253,14 @@ impl Ctx { }; let ast_id = self.source_ast_id_map.ast_id(enum_); let res = Enum { name, visibility, generic_params, variants, ast_id }; - Some(id(self.tree.enums.alloc(res))) + Some(id(self.data().enums.alloc(res))) } fn lower_variants(&mut self, variants: &ast::EnumVariantList) -> Range> { let start = self.next_variant_idx(); for variant in variants.variants() { if let Some(data) = self.lower_variant(&variant) { - self.tree.variants.alloc(data); + self.data().variants.alloc(data); } } let end = self.next_variant_idx(); @@ -327,7 +331,7 @@ impl Ctx { }; res.generic_params = self.lower_generic_params(GenericsOwner::Function(&res), func); - Some(id(self.tree.functions.alloc(res))) + Some(id(self.data().functions.alloc(res))) } fn lower_type_alias( @@ -341,7 +345,7 @@ impl Ctx { let generic_params = self.lower_generic_params(GenericsOwner::TypeAlias, type_alias); let ast_id = self.source_ast_id_map.ast_id(type_alias); let res = TypeAlias { name, visibility, bounds, generic_params, type_ref, ast_id }; - Some(id(self.tree.type_aliases.alloc(res))) + Some(id(self.data().type_aliases.alloc(res))) } fn lower_static(&mut self, static_: &ast::StaticDef) -> Option> { @@ -351,7 +355,7 @@ impl Ctx { let mutable = static_.mut_token().is_some(); let ast_id = self.source_ast_id_map.ast_id(static_); let res = Static { name, visibility, mutable, type_ref, ast_id }; - Some(id(self.tree.statics.alloc(res))) + Some(id(self.data().statics.alloc(res))) } fn lower_const(&mut self, konst: &ast::ConstDef) -> FileItemTreeId { @@ -360,7 +364,7 @@ impl Ctx { let visibility = self.lower_visibility(konst); let ast_id = self.source_ast_id_map.ast_id(konst); let res = Const { name, visibility, type_ref, ast_id }; - id(self.tree.consts.alloc(res)) + id(self.data().consts.alloc(res)) } fn lower_module(&mut self, module: &ast::Module) -> Option> { @@ -386,7 +390,7 @@ impl Ctx { }; let ast_id = self.source_ast_id_map.ast_id(module); let res = Mod { name, visibility, kind, ast_id }; - Some(id(self.tree.mods.alloc(res))) + Some(id(self.data().mods.alloc(res))) } fn lower_trait(&mut self, trait_def: &ast::TraitDef) -> Option> { @@ -417,7 +421,7 @@ impl Ctx { items: items.unwrap_or_default(), ast_id, }; - Some(id(self.tree.traits.alloc(res))) + Some(id(self.data().traits.alloc(res))) } fn lower_impl(&mut self, impl_def: &ast::ImplDef) -> Option> { @@ -440,7 +444,7 @@ impl Ctx { .collect(); let ast_id = self.source_ast_id_map.ast_id(impl_def); let res = Impl { generic_params, target_trait, target_type, is_negative, items, ast_id }; - Some(id(self.tree.impls.alloc(res))) + Some(id(self.data().impls.alloc(res))) } fn lower_use(&mut self, use_item: &ast::UseItem) -> Vec> { @@ -451,7 +455,7 @@ impl Ctx { // Every use item can expand to many `Import`s. let mut imports = Vec::new(); - let tree = &mut self.tree; + let tree = self.tree.data_mut(); ModPath::expand_use_item( InFile::new(self.file, use_item.clone()), &self.hygiene, @@ -484,7 +488,7 @@ impl Ctx { let is_macro_use = extern_crate.has_atom_attr("macro_use"); let res = ExternCrate { path, alias, visibility, is_macro_use, ast_id }; - Some(id(self.tree.extern_crates.alloc(res))) + Some(id(self.data().extern_crates.alloc(res))) } fn lower_macro_call(&mut self, m: &ast::MacroCall) -> Option> { @@ -511,7 +515,7 @@ impl Ctx { let is_builtin = attrs.by_key("rustc_builtin_macro").exists(); let res = MacroCall { name, path, is_export, is_builtin, is_local_inner, ast_id }; - Some(id(self.tree.macro_calls.alloc(res))) + Some(id(self.data().macro_calls.alloc(res))) } fn lower_extern_block(&mut self, block: &ast::ExternBlock) -> Vec { @@ -619,10 +623,14 @@ impl Ctx { } fn next_field_idx(&self) -> Idx { - Idx::from_raw(RawId::from(self.tree.fields.len() as u32)) + Idx::from_raw(RawId::from( + self.tree.data.as_ref().map_or(0, |data| data.fields.len() as u32), + )) } fn next_variant_idx(&self) -> Idx { - Idx::from_raw(RawId::from(self.tree.variants.len() as u32)) + Idx::from_raw(RawId::from( + self.tree.data.as_ref().map_or(0, |data| data.variants.len() as u32), + )) } } -- cgit v1.2.3 From 16fd4dabb754b017237127e70ef1e2b409c4f9b6 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 23 Jun 2020 20:20:30 +0200 Subject: Remove file id from item tree It's not needed, and `source` is only used by tests anyways --- crates/ra_hir_def/src/item_tree/lower.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_hir_def/src/item_tree/lower.rs') diff --git a/crates/ra_hir_def/src/item_tree/lower.rs b/crates/ra_hir_def/src/item_tree/lower.rs index 7d28fe7c6..3af22149d 100644 --- a/crates/ra_hir_def/src/item_tree/lower.rs +++ b/crates/ra_hir_def/src/item_tree/lower.rs @@ -42,7 +42,7 @@ pub(super) struct Ctx { impl Ctx { pub(super) fn new(db: &dyn DefDatabase, hygiene: Hygiene, file: HirFileId) -> Self { Self { - tree: ItemTree::empty(file), + tree: ItemTree::empty(), hygiene, file, source_ast_id_map: db.ast_id_map(file), -- 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/item_tree/lower.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) (limited to 'crates/ra_hir_def/src/item_tree/lower.rs') diff --git a/crates/ra_hir_def/src/item_tree/lower.rs b/crates/ra_hir_def/src/item_tree/lower.rs index 3af22149d..73c21b9ec 100644 --- a/crates/ra_hir_def/src/item_tree/lower.rs +++ b/crates/ra_hir_def/src/item_tree/lower.rs @@ -36,7 +36,7 @@ pub(super) struct Ctx { source_ast_id_map: Arc, body_ctx: crate::body::LowerCtx, inner_items: Vec, - forced_visibility: Option, + forced_visibility: Option, } impl Ctx { @@ -201,7 +201,7 @@ impl Ctx { start..end } - fn lower_record_field(&self, field: &ast::RecordFieldDef) -> Option { + fn lower_record_field(&mut self, field: &ast::RecordFieldDef) -> Option { let name = field.name()?.as_name(); let visibility = self.lower_visibility(field); let type_ref = self.lower_type_ref(&field.ascribed_type()?); @@ -220,7 +220,7 @@ impl Ctx { start..end } - fn lower_tuple_field(&self, idx: usize, field: &ast::TupleFieldDef) -> Option { + fn lower_tuple_field(&mut self, idx: usize, field: &ast::TupleFieldDef) -> Option { let name = Name::new_tuple_field(idx); let visibility = self.lower_visibility(field); let type_ref = self.lower_type_ref(&field.type_ref()?); @@ -399,7 +399,7 @@ impl Ctx { let generic_params = self.lower_generic_params(GenericsOwner::Trait(trait_def), trait_def); let auto = trait_def.auto_token().is_some(); let items = trait_def.item_list().map(|list| { - self.with_inherited_visibility(visibility.clone(), |this| { + self.with_inherited_visibility(visibility, |this| { list.items() .filter_map(|item| { let attrs = Attrs::new(&item, &this.hygiene); @@ -463,7 +463,7 @@ impl Ctx { imports.push(id(tree.imports.alloc(Import { path, alias, - visibility: visibility.clone(), + visibility, is_glob, is_prelude, ast_id, @@ -596,11 +596,13 @@ impl Ctx { } } - fn lower_visibility(&self, item: &impl ast::VisibilityOwner) -> RawVisibility { - match &self.forced_visibility { - Some(vis) => vis.clone(), + fn lower_visibility(&mut self, item: &impl ast::VisibilityOwner) -> RawVisibilityId { + let vis = match self.forced_visibility { + Some(vis) => return vis, None => RawVisibility::from_ast_with_hygiene(item.visibility(), &self.hygiene), - } + }; + + self.data().vis.alloc(vis) } fn lower_type_ref(&self, type_ref: &ast::TypeRef) -> TypeRef { @@ -613,7 +615,7 @@ impl Ctx { /// Forces the visibility `vis` to be used for all items lowered during execution of `f`. fn with_inherited_visibility( &mut self, - vis: RawVisibility, + vis: RawVisibilityId, f: impl FnOnce(&mut Self) -> R, ) -> R { let old = mem::replace(&mut self.forced_visibility, Some(vis)); -- cgit v1.2.3 From abdba92334f800d236c65e543377f75327f7307a Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 24 Jun 2020 15:54:35 +0200 Subject: Don't allocate empty generics --- crates/ra_hir_def/src/item_tree/lower.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir_def/src/item_tree/lower.rs') diff --git a/crates/ra_hir_def/src/item_tree/lower.rs b/crates/ra_hir_def/src/item_tree/lower.rs index 73c21b9ec..b1847a6cb 100644 --- a/crates/ra_hir_def/src/item_tree/lower.rs +++ b/crates/ra_hir_def/src/item_tree/lower.rs @@ -322,7 +322,7 @@ impl Ctx { let mut res = Function { name, visibility, - generic_params: GenericParams::default(), + generic_params: GenericParamsId::EMPTY, has_self_param, is_unsafe: func.unsafe_token().is_some(), params, @@ -545,7 +545,7 @@ impl Ctx { &mut self, owner: GenericsOwner<'_>, node: &impl ast::TypeParamsOwner, - ) -> GenericParams { + ) -> GenericParamsId { let mut sm = &mut ArenaMap::default(); let mut generics = GenericParams::default(); match owner { @@ -584,7 +584,8 @@ impl Ctx { generics.fill(&self.body_ctx, &mut sm, node); } } - generics + + self.data().generics.alloc(generics) } fn lower_type_bounds(&mut self, node: &impl ast::TypeBoundsOwner) -> Vec { -- 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/item_tree/lower.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'crates/ra_hir_def/src/item_tree/lower.rs') diff --git a/crates/ra_hir_def/src/item_tree/lower.rs b/crates/ra_hir_def/src/item_tree/lower.rs index b1847a6cb..6e31266a2 100644 --- a/crates/ra_hir_def/src/item_tree/lower.rs +++ b/crates/ra_hir_def/src/item_tree/lower.rs @@ -325,7 +325,7 @@ impl Ctx { generic_params: GenericParamsId::EMPTY, has_self_param, is_unsafe: func.unsafe_token().is_some(), - params, + params: params.into_boxed_slice(), ret_type, ast_id, }; @@ -344,7 +344,14 @@ impl Ctx { let bounds = self.lower_type_bounds(type_alias); let generic_params = self.lower_generic_params(GenericsOwner::TypeAlias, type_alias); let ast_id = self.source_ast_id_map.ast_id(type_alias); - let res = TypeAlias { name, visibility, bounds, generic_params, type_ref, ast_id }; + let res = TypeAlias { + name, + visibility, + bounds: bounds.into_boxed_slice(), + generic_params, + type_ref, + ast_id, + }; Some(id(self.data().type_aliases.alloc(res))) } @@ -384,7 +391,7 @@ impl Ctx { }) .unwrap_or_else(|| { mark::hit!(name_res_works_for_broken_modules); - Vec::new() + Box::new([]) as Box<[_]> }), } }; @@ -552,7 +559,7 @@ impl Ctx { GenericsOwner::Function(func) => { generics.fill(&self.body_ctx, sm, node); // lower `impl Trait` in arguments - for param in &func.params { + for param in &*func.params { generics.fill_implicit_impl_trait_args(param); } } -- cgit v1.2.3 From d6fd7809b016787e8a23f443b1b626840c4ea5c7 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Wed, 24 Jun 2020 16:46:44 +0200 Subject: Clean up and fix inner item collection a bit --- crates/ra_hir_def/src/item_tree/lower.rs | 33 ++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) (limited to 'crates/ra_hir_def/src/item_tree/lower.rs') diff --git a/crates/ra_hir_def/src/item_tree/lower.rs b/crates/ra_hir_def/src/item_tree/lower.rs index 6e31266a2..f10ad25f7 100644 --- a/crates/ra_hir_def/src/item_tree/lower.rs +++ b/crates/ra_hir_def/src/item_tree/lower.rs @@ -82,7 +82,13 @@ impl Ctx { | ast::ModuleItem::TypeAliasDef(_) | ast::ModuleItem::ConstDef(_) | ast::ModuleItem::StaticDef(_) - | ast::ModuleItem::MacroCall(_) => self.collect_inner_items(item.syntax()), + | ast::ModuleItem::MacroCall(_) => { + // Skip this if we're already collecting inner items. We'll descend into all nodes + // already. + if !inner { + self.collect_inner_items(item.syntax()); + } + } // These are handled in their respective `lower_X` method (since we can't just blindly // walk them). @@ -403,7 +409,8 @@ impl Ctx { fn lower_trait(&mut self, trait_def: &ast::TraitDef) -> Option> { let name = trait_def.name()?.as_name(); let visibility = self.lower_visibility(trait_def); - let generic_params = self.lower_generic_params(GenericsOwner::Trait(trait_def), trait_def); + let generic_params = + self.lower_generic_params_and_inner_items(GenericsOwner::Trait(trait_def), trait_def); let auto = trait_def.auto_token().is_some(); let items = trait_def.item_list().map(|list| { self.with_inherited_visibility(visibility, |this| { @@ -432,7 +439,8 @@ impl Ctx { } fn lower_impl(&mut self, impl_def: &ast::ImplDef) -> Option> { - let generic_params = self.lower_generic_params(GenericsOwner::Impl, impl_def); + let generic_params = + self.lower_generic_params_and_inner_items(GenericsOwner::Impl, impl_def); let target_trait = impl_def.target_trait().map(|tr| self.lower_type_ref(&tr)); let target_type = self.lower_type_ref(&impl_def.target_type()?); let is_negative = impl_def.excl_token().is_some(); @@ -548,6 +556,23 @@ impl Ctx { }) } + /// Lowers generics defined on `node` and collects inner items defined within. + fn lower_generic_params_and_inner_items( + &mut self, + owner: GenericsOwner<'_>, + node: &impl ast::TypeParamsOwner, + ) -> GenericParamsId { + // Generics are part of item headers and may contain inner items we need to collect. + if let Some(params) = node.type_param_list() { + self.collect_inner_items(params.syntax()); + } + if let Some(clause) = node.where_clause() { + self.collect_inner_items(clause.syntax()); + } + + self.lower_generic_params(owner, node) + } + fn lower_generic_params( &mut self, owner: GenericsOwner<'_>, @@ -617,7 +642,7 @@ impl Ctx { TypeRef::from_ast(&self.body_ctx, type_ref.clone()) } fn lower_type_ref_opt(&self, type_ref: Option) -> TypeRef { - TypeRef::from_ast_opt(&self.body_ctx, type_ref) + type_ref.map(|ty| self.lower_type_ref(&ty)).unwrap_or(TypeRef::Error) } /// Forces the visibility `vis` to be used for all items lowered during execution of `f`. -- cgit v1.2.3