From 552ba868afc8f72202ac834d07bbeb330aca007d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 23 Nov 2019 11:14:10 +0300 Subject: Move attrs query to hir_def --- crates/ra_hir/src/code_model.rs | 56 +++++++++++++++++++-- crates/ra_hir/src/code_model/attrs.rs | 91 ----------------------------------- crates/ra_hir/src/code_model/src.rs | 19 ++++---- crates/ra_hir/src/db.rs | 4 -- crates/ra_hir/src/from_id.rs | 10 +++- crates/ra_hir/src/lib.rs | 9 ++-- 6 files changed, 73 insertions(+), 116 deletions(-) delete mode 100644 crates/ra_hir/src/code_model/attrs.rs (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 9f8c6c4a5..496b4ee8a 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -2,7 +2,6 @@ pub(crate) mod src; pub(crate) mod docs; -pub(crate) mod attrs; use std::sync::Arc; @@ -13,8 +12,8 @@ use hir_def::{ nameres::per_ns::PerNs, resolver::{HasResolver, TypeNs}, type_ref::TypeRef, - ContainerId, CrateModuleId, HasModule, ImplId, LocalEnumVariantId, LocalStructFieldId, Lookup, - ModuleId, UnionId, + AdtId, ContainerId, CrateModuleId, EnumVariantId, HasModule, ImplId, LocalEnumVariantId, + LocalStructFieldId, Lookup, ModuleId, StructFieldId, UnionId, }; use hir_expand::{ diagnostics::DiagnosticSink, @@ -110,7 +109,7 @@ impl_froms!( BuiltinType ); -pub use hir_def::ModuleSource; +pub use hir_def::{attr::Attrs, ModuleSource}; impl Module { pub(crate) fn new(krate: Crate, crate_module_id: CrateModuleId) -> Module { @@ -991,3 +990,52 @@ impl From for ScopeDef { .unwrap_or(ScopeDef::Unknown) } } + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum AttrDef { + Module(Module), + StructField(StructField), + Adt(Adt), + Function(Function), + EnumVariant(EnumVariant), + Static(Static), + Const(Const), + Trait(Trait), + TypeAlias(TypeAlias), + MacroDef(MacroDef), +} + +impl_froms!( + AttrDef: Module, + StructField, + Adt(Struct, Enum, Union), + EnumVariant, + Static, + Const, + Function, + Trait, + TypeAlias, + MacroDef +); + +pub trait HasAttrs { + fn attrs(self, db: &impl DefDatabase) -> Attrs; +} + +impl> HasAttrs for T { + fn attrs(self, db: &impl DefDatabase) -> Attrs { + let def = self.into(); + match def { + AttrDef::Module(it) => db.attrs(it.id.into()), + AttrDef::StructField(it) => db.attrs(StructFieldId::from(it).into()), + AttrDef::Adt(it) => db.attrs(AdtId::from(it).into()), + AttrDef::Function(it) => db.attrs(it.id.into()), + AttrDef::EnumVariant(it) => db.attrs(EnumVariantId::from(it).into()), + AttrDef::Static(it) => db.attrs(it.id.into()), + AttrDef::Const(it) => db.attrs(it.id.into()), + AttrDef::Trait(it) => db.attrs(it.id.into()), + AttrDef::TypeAlias(it) => db.attrs(it.id.into()), + AttrDef::MacroDef(it) => db.attrs(it.id.into()), + } + } +} diff --git a/crates/ra_hir/src/code_model/attrs.rs b/crates/ra_hir/src/code_model/attrs.rs deleted file mode 100644 index 96da8c88c..000000000 --- a/crates/ra_hir/src/code_model/attrs.rs +++ /dev/null @@ -1,91 +0,0 @@ -//! FIXME: write short doc here - -use crate::{ - db::{AstDatabase, DefDatabase, HirDatabase}, - Adt, Const, Enum, EnumVariant, FieldSource, Function, HasSource, MacroDef, Module, Static, - Struct, StructField, Trait, TypeAlias, Union, -}; -use hir_def::attr::{Attr, Attrs}; -use hir_expand::hygiene::Hygiene; -use ra_syntax::ast; - -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -pub enum AttrDef { - Module(Module), - StructField(StructField), - Adt(Adt), - Function(Function), - EnumVariant(EnumVariant), - Static(Static), - Const(Const), - Trait(Trait), - TypeAlias(TypeAlias), - MacroDef(MacroDef), -} - -impl_froms!( - AttrDef: Module, - StructField, - Adt(Struct, Enum, Union), - EnumVariant, - Static, - Const, - Function, - Trait, - TypeAlias, - MacroDef -); - -pub trait HasAttrs { - fn attrs(&self, db: &impl HirDatabase) -> Attrs; -} - -pub(crate) fn attributes_query(db: &(impl DefDatabase + AstDatabase), def: AttrDef) -> Attrs { - match def { - AttrDef::Module(it) => { - let src = match it.declaration_source(db) { - Some(it) => it, - None => return Attrs::default(), - }; - let hygiene = Hygiene::new(db, src.file_id); - Attr::from_attrs_owner(&src.value, &hygiene) - } - AttrDef::StructField(it) => match it.source(db).value { - FieldSource::Named(named) => { - let src = it.source(db); - let hygiene = Hygiene::new(db, src.file_id); - Attr::from_attrs_owner(&named, &hygiene) - } - FieldSource::Pos(..) => Attrs::default(), - }, - AttrDef::Adt(it) => match it { - Adt::Struct(it) => attrs_from_ast(it, db), - Adt::Enum(it) => attrs_from_ast(it, db), - Adt::Union(it) => attrs_from_ast(it, db), - }, - AttrDef::EnumVariant(it) => attrs_from_ast(it, db), - AttrDef::Static(it) => attrs_from_ast(it, db), - AttrDef::Const(it) => attrs_from_ast(it, db), - AttrDef::Function(it) => attrs_from_ast(it, db), - AttrDef::Trait(it) => attrs_from_ast(it, db), - AttrDef::TypeAlias(it) => attrs_from_ast(it, db), - AttrDef::MacroDef(it) => attrs_from_ast(it, db), - } -} - -fn attrs_from_ast(node: T, db: &D) -> Attrs -where - T: HasSource, - T::Ast: ast::AttrsOwner, - D: DefDatabase + AstDatabase, -{ - let src = node.source(db); - let hygiene = Hygiene::new(db, src.file_id); - Attr::from_attrs_owner(&src.value, &hygiene) -} - -impl + Copy> HasAttrs for T { - fn attrs(&self, db: &impl HirDatabase) -> Attrs { - db.attrs((*self).into()) - } -} diff --git a/crates/ra_hir/src/code_model/src.rs b/crates/ra_hir/src/code_model/src.rs index a45c062bf..09bacf579 100644 --- a/crates/ra_hir/src/code_model/src.rs +++ b/crates/ra_hir/src/code_model/src.rs @@ -1,13 +1,14 @@ //! FIXME: write short doc here use hir_def::{HasChildSource, HasSource as _, Lookup, VariantId}; +use hir_expand::either::Either; use ra_syntax::ast::{self, AstNode}; use crate::{ db::{AstDatabase, DefDatabase, HirDatabase}, ids::AstItemDef, - Const, Either, Enum, EnumVariant, FieldSource, Function, HasBody, HirFileId, MacroDef, Module, - ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, + Const, Enum, EnumVariant, FieldSource, Function, HasBody, MacroDef, Module, ModuleSource, + Static, Struct, StructField, Trait, TypeAlias, Union, }; pub use hir_expand::Source; @@ -23,11 +24,11 @@ impl Module { /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. pub fn definition_source(self, db: &(impl DefDatabase + AstDatabase)) -> Source { let def_map = db.crate_def_map(self.id.krate); - let decl_id = def_map[self.id.module_id].declaration; - let file_id = def_map[self.id.module_id].definition; - let value = ModuleSource::new(db, file_id, decl_id); - let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id()); - Source { file_id, value } + let src = def_map[self.id.module_id].definition_source(db); + src.map(|it| match it { + Either::A(it) => ModuleSource::SourceFile(it), + Either::B(it) => ModuleSource::Module(it), + }) } /// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`. @@ -37,9 +38,7 @@ impl Module { db: &(impl DefDatabase + AstDatabase), ) -> Option> { let def_map = db.crate_def_map(self.id.krate); - let decl = def_map[self.id.module_id].declaration?; - let value = decl.to_node(db); - Some(Source { file_id: decl.file_id(), value }) + def_map[self.id.module_id].declaration_source(db) } } diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index 85d46b485..c3f698ea0 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -2,7 +2,6 @@ use std::sync::Arc; -use hir_def::attr::Attrs; use ra_db::salsa; use ra_syntax::SmolStr; @@ -46,9 +45,6 @@ pub trait DefDatabase: HirDebugDatabase + DefDatabase2 { #[salsa::invoke(crate::code_model::docs::documentation_query)] fn documentation(&self, def: crate::DocDef) -> Option; - - #[salsa::invoke(crate::code_model::attrs::attributes_query)] - fn attrs(&self, def: crate::AttrDef) -> Attrs; } #[salsa::query_group(HirDatabaseStorage)] diff --git a/crates/ra_hir/src/from_id.rs b/crates/ra_hir/src/from_id.rs index 8900fc1f2..5d7af00ab 100644 --- a/crates/ra_hir/src/from_id.rs +++ b/crates/ra_hir/src/from_id.rs @@ -5,13 +5,13 @@ use hir_def::{ AdtId, AssocItemId, ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, - ModuleDefId, StaticId, StructId, TypeAliasId, UnionId, VariantId, + ModuleDefId, StaticId, StructFieldId, StructId, TypeAliasId, UnionId, VariantId, }; use crate::{ ty::{CallableDef, TypableDef}, Adt, AssocItem, Const, Crate, DefWithBody, EnumVariant, Function, GenericDef, ModuleDef, - Static, TypeAlias, VariantDef, + Static, StructField, TypeAlias, VariantDef, }; impl From for Crate { @@ -234,3 +234,9 @@ impl From for VariantId { } } } + +impl From for StructFieldId { + fn from(def: StructField) -> Self { + StructFieldId { parent: def.parent.into(), local_id: def.id } + } +} diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index e164c9b32..152590cd1 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -51,13 +51,12 @@ mod marks; pub use crate::{ code_model::{ - attrs::{AttrDef, HasAttrs}, docs::{DocDef, Docs, Documentation}, src::{HasBodySource, HasSource}, - Adt, AssocItem, Const, Container, Crate, CrateDependency, DefWithBody, Enum, EnumVariant, - FieldSource, Function, GenericDef, GenericParam, HasBody, ImplBlock, Local, MacroDef, - Module, ModuleDef, ModuleSource, ScopeDef, Static, Struct, StructField, Trait, TypeAlias, - Union, VariantDef, + Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency, DefWithBody, Enum, + EnumVariant, FieldSource, Function, GenericDef, GenericParam, HasAttrs, HasBody, ImplBlock, + Local, MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, Static, Struct, StructField, + Trait, TypeAlias, Union, VariantDef, }, expr::ExprScopes, from_source::FromSource, -- cgit v1.2.3