mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-28 12:55:11 +00:00
Merge #2366
2366: Move attrs query to hir_def r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
05939d5043
10 changed files with 202 additions and 123 deletions
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
pub(crate) mod src;
|
pub(crate) mod src;
|
||||||
pub(crate) mod docs;
|
pub(crate) mod docs;
|
||||||
pub(crate) mod attrs;
|
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
@ -13,8 +12,8 @@ use hir_def::{
|
||||||
nameres::per_ns::PerNs,
|
nameres::per_ns::PerNs,
|
||||||
resolver::{HasResolver, TypeNs},
|
resolver::{HasResolver, TypeNs},
|
||||||
type_ref::TypeRef,
|
type_ref::TypeRef,
|
||||||
ContainerId, CrateModuleId, HasModule, ImplId, LocalEnumVariantId, LocalStructFieldId, Lookup,
|
AdtId, ContainerId, CrateModuleId, EnumVariantId, HasModule, ImplId, LocalEnumVariantId,
|
||||||
ModuleId, UnionId,
|
LocalStructFieldId, Lookup, ModuleId, StructFieldId, UnionId,
|
||||||
};
|
};
|
||||||
use hir_expand::{
|
use hir_expand::{
|
||||||
diagnostics::DiagnosticSink,
|
diagnostics::DiagnosticSink,
|
||||||
|
@ -110,7 +109,7 @@ impl_froms!(
|
||||||
BuiltinType
|
BuiltinType
|
||||||
);
|
);
|
||||||
|
|
||||||
pub use hir_def::ModuleSource;
|
pub use hir_def::{attr::Attrs, ModuleSource};
|
||||||
|
|
||||||
impl Module {
|
impl Module {
|
||||||
pub(crate) fn new(krate: Crate, crate_module_id: CrateModuleId) -> Module {
|
pub(crate) fn new(krate: Crate, crate_module_id: CrateModuleId) -> Module {
|
||||||
|
@ -991,3 +990,52 @@ impl From<PerNs> for ScopeDef {
|
||||||
.unwrap_or(ScopeDef::Unknown)
|
.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<T: Into<AttrDef>> 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()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -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<T, D>(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<T: Into<AttrDef> + Copy> HasAttrs for T {
|
|
||||||
fn attrs(&self, db: &impl HirDatabase) -> Attrs {
|
|
||||||
db.attrs((*self).into())
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,13 +1,14 @@
|
||||||
//! FIXME: write short doc here
|
//! FIXME: write short doc here
|
||||||
|
|
||||||
use hir_def::{HasChildSource, HasSource as _, Lookup, VariantId};
|
use hir_def::{HasChildSource, HasSource as _, Lookup, VariantId};
|
||||||
|
use hir_expand::either::Either;
|
||||||
use ra_syntax::ast::{self, AstNode};
|
use ra_syntax::ast::{self, AstNode};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
db::{AstDatabase, DefDatabase, HirDatabase},
|
db::{AstDatabase, DefDatabase, HirDatabase},
|
||||||
ids::AstItemDef,
|
ids::AstItemDef,
|
||||||
Const, Either, Enum, EnumVariant, FieldSource, Function, HasBody, HirFileId, MacroDef, Module,
|
Const, Enum, EnumVariant, FieldSource, Function, HasBody, MacroDef, Module, ModuleSource,
|
||||||
ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union,
|
Static, Struct, StructField, Trait, TypeAlias, Union,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use hir_expand::Source;
|
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.
|
/// 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<ModuleSource> {
|
pub fn definition_source(self, db: &(impl DefDatabase + AstDatabase)) -> Source<ModuleSource> {
|
||||||
let def_map = db.crate_def_map(self.id.krate);
|
let def_map = db.crate_def_map(self.id.krate);
|
||||||
let decl_id = def_map[self.id.module_id].declaration;
|
let src = def_map[self.id.module_id].definition_source(db);
|
||||||
let file_id = def_map[self.id.module_id].definition;
|
src.map(|it| match it {
|
||||||
let value = ModuleSource::new(db, file_id, decl_id);
|
Either::A(it) => ModuleSource::SourceFile(it),
|
||||||
let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id());
|
Either::B(it) => ModuleSource::Module(it),
|
||||||
Source { file_id, value }
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
|
/// 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),
|
db: &(impl DefDatabase + AstDatabase),
|
||||||
) -> Option<Source<ast::Module>> {
|
) -> Option<Source<ast::Module>> {
|
||||||
let def_map = db.crate_def_map(self.id.krate);
|
let def_map = db.crate_def_map(self.id.krate);
|
||||||
let decl = def_map[self.id.module_id].declaration?;
|
def_map[self.id.module_id].declaration_source(db)
|
||||||
let value = decl.to_node(db);
|
|
||||||
Some(Source { file_id: decl.file_id(), value })
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use hir_def::attr::Attrs;
|
|
||||||
use ra_db::salsa;
|
use ra_db::salsa;
|
||||||
use ra_syntax::SmolStr;
|
use ra_syntax::SmolStr;
|
||||||
|
|
||||||
|
@ -46,9 +45,6 @@ pub trait DefDatabase: HirDebugDatabase + DefDatabase2 {
|
||||||
|
|
||||||
#[salsa::invoke(crate::code_model::docs::documentation_query)]
|
#[salsa::invoke(crate::code_model::docs::documentation_query)]
|
||||||
fn documentation(&self, def: crate::DocDef) -> Option<crate::Documentation>;
|
fn documentation(&self, def: crate::DocDef) -> Option<crate::Documentation>;
|
||||||
|
|
||||||
#[salsa::invoke(crate::code_model::attrs::attributes_query)]
|
|
||||||
fn attrs(&self, def: crate::AttrDef) -> Attrs;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[salsa::query_group(HirDatabaseStorage)]
|
#[salsa::query_group(HirDatabaseStorage)]
|
||||||
|
|
|
@ -5,13 +5,13 @@
|
||||||
|
|
||||||
use hir_def::{
|
use hir_def::{
|
||||||
AdtId, AssocItemId, ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId,
|
AdtId, AssocItemId, ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId,
|
||||||
ModuleDefId, StaticId, StructId, TypeAliasId, UnionId, VariantId,
|
ModuleDefId, StaticId, StructFieldId, StructId, TypeAliasId, UnionId, VariantId,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ty::{CallableDef, TypableDef},
|
ty::{CallableDef, TypableDef},
|
||||||
Adt, AssocItem, Const, Crate, DefWithBody, EnumVariant, Function, GenericDef, ModuleDef,
|
Adt, AssocItem, Const, Crate, DefWithBody, EnumVariant, Function, GenericDef, ModuleDef,
|
||||||
Static, TypeAlias, VariantDef,
|
Static, StructField, TypeAlias, VariantDef,
|
||||||
};
|
};
|
||||||
|
|
||||||
impl From<ra_db::CrateId> for Crate {
|
impl From<ra_db::CrateId> for Crate {
|
||||||
|
@ -234,3 +234,9 @@ impl From<VariantDef> for VariantId {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<StructField> for StructFieldId {
|
||||||
|
fn from(def: StructField) -> Self {
|
||||||
|
StructFieldId { parent: def.parent.into(), local_id: def.id }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -51,13 +51,12 @@ mod marks;
|
||||||
|
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
code_model::{
|
code_model::{
|
||||||
attrs::{AttrDef, HasAttrs},
|
|
||||||
docs::{DocDef, Docs, Documentation},
|
docs::{DocDef, Docs, Documentation},
|
||||||
src::{HasBodySource, HasSource},
|
src::{HasBodySource, HasSource},
|
||||||
Adt, AssocItem, Const, Container, Crate, CrateDependency, DefWithBody, Enum, EnumVariant,
|
Adt, AssocItem, AttrDef, Const, Container, Crate, CrateDependency, DefWithBody, Enum,
|
||||||
FieldSource, Function, GenericDef, GenericParam, HasBody, ImplBlock, Local, MacroDef,
|
EnumVariant, FieldSource, Function, GenericDef, GenericParam, HasAttrs, HasBody, ImplBlock,
|
||||||
Module, ModuleDef, ModuleSource, ScopeDef, Static, Struct, StructField, Trait, TypeAlias,
|
Local, MacroDef, Module, ModuleDef, ModuleSource, ScopeDef, Static, Struct, StructField,
|
||||||
Union, VariantDef,
|
Trait, TypeAlias, Union, VariantDef,
|
||||||
},
|
},
|
||||||
expr::ExprScopes,
|
expr::ExprScopes,
|
||||||
from_source::FromSource,
|
from_source::FromSource,
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
use std::{ops, sync::Arc};
|
use std::{ops, sync::Arc};
|
||||||
|
|
||||||
use hir_expand::hygiene::Hygiene;
|
use hir_expand::{either::Either, hygiene::Hygiene, AstId};
|
||||||
use mbe::ast_to_token_tree;
|
use mbe::ast_to_token_tree;
|
||||||
use ra_cfg::CfgOptions;
|
use ra_cfg::CfgOptions;
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
|
@ -11,7 +11,9 @@ use ra_syntax::{
|
||||||
};
|
};
|
||||||
use tt::Subtree;
|
use tt::Subtree;
|
||||||
|
|
||||||
use crate::path::Path;
|
use crate::{
|
||||||
|
db::DefDatabase2, path::Path, AdtId, AstItemDef, AttrDefId, HasChildSource, HasSource, Lookup,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Default, Debug, Clone, PartialEq, Eq)]
|
#[derive(Default, Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct Attrs {
|
pub struct Attrs {
|
||||||
|
@ -30,6 +32,46 @@ impl ops::Deref for Attrs {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Attrs {
|
impl Attrs {
|
||||||
|
pub(crate) fn attrs_query(db: &impl DefDatabase2, def: AttrDefId) -> Attrs {
|
||||||
|
match def {
|
||||||
|
AttrDefId::ModuleId(module) => {
|
||||||
|
let def_map = db.crate_def_map(module.krate);
|
||||||
|
let src = match def_map[module.module_id].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)
|
||||||
|
}
|
||||||
|
AttrDefId::StructFieldId(it) => {
|
||||||
|
let src = it.parent.child_source(db);
|
||||||
|
match &src.value[it.local_id] {
|
||||||
|
Either::A(_tuple) => Attrs::default(),
|
||||||
|
Either::B(record) => {
|
||||||
|
let hygiene = Hygiene::new(db, src.file_id);
|
||||||
|
Attr::from_attrs_owner(record, &hygiene)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
AttrDefId::AdtId(it) => match it {
|
||||||
|
AdtId::StructId(it) => attrs_from_ast(it.0.lookup_intern(db).ast_id, db),
|
||||||
|
AdtId::EnumId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db),
|
||||||
|
AdtId::UnionId(it) => attrs_from_ast(it.0.lookup_intern(db).ast_id, db),
|
||||||
|
},
|
||||||
|
AttrDefId::EnumVariantId(it) => {
|
||||||
|
let src = it.parent.child_source(db);
|
||||||
|
let hygiene = Hygiene::new(db, src.file_id);
|
||||||
|
Attr::from_attrs_owner(&src.value[it.local_id], &hygiene)
|
||||||
|
}
|
||||||
|
AttrDefId::StaticId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db),
|
||||||
|
AttrDefId::ConstId(it) => attrs_from_loc(it.lookup(db), db),
|
||||||
|
AttrDefId::FunctionId(it) => attrs_from_loc(it.lookup(db), db),
|
||||||
|
AttrDefId::TraitId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db),
|
||||||
|
AttrDefId::TypeAliasId(it) => attrs_from_loc(it.lookup(db), db),
|
||||||
|
AttrDefId::MacroDefId(it) => attrs_from_ast(it.ast_id, db),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn has_atom(&self, atom: &str) -> bool {
|
pub fn has_atom(&self, atom: &str) -> bool {
|
||||||
self.iter().any(|it| it.is_simple_atom(atom))
|
self.iter().any(|it| it.is_simple_atom(atom))
|
||||||
}
|
}
|
||||||
|
@ -106,3 +148,23 @@ impl Attr {
|
||||||
cfg_options.is_cfg_enabled(self.as_cfg()?)
|
cfg_options.is_cfg_enabled(self.as_cfg()?)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn attrs_from_ast<D, N>(src: AstId<N>, db: &D) -> Attrs
|
||||||
|
where
|
||||||
|
N: ast::AttrsOwner,
|
||||||
|
D: DefDatabase2,
|
||||||
|
{
|
||||||
|
let hygiene = Hygiene::new(db, src.file_id());
|
||||||
|
Attr::from_attrs_owner(&src.to_node(db), &hygiene)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn attrs_from_loc<T, D>(node: T, db: &D) -> Attrs
|
||||||
|
where
|
||||||
|
T: HasSource,
|
||||||
|
T::Value: ast::AttrsOwner,
|
||||||
|
D: DefDatabase2,
|
||||||
|
{
|
||||||
|
let src = node.source(db);
|
||||||
|
let hygiene = Hygiene::new(db, src.file_id);
|
||||||
|
Attr::from_attrs_owner(&src.value, &hygiene)
|
||||||
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ use ra_syntax::ast;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
adt::{EnumData, StructData},
|
adt::{EnumData, StructData},
|
||||||
|
attr::Attrs,
|
||||||
body::{scope::ExprScopes, Body, BodySourceMap},
|
body::{scope::ExprScopes, Body, BodySourceMap},
|
||||||
data::{ConstData, FunctionData, ImplData, TraitData, TypeAliasData},
|
data::{ConstData, FunctionData, ImplData, TraitData, TypeAliasData},
|
||||||
generics::GenericParams,
|
generics::GenericParams,
|
||||||
|
@ -14,7 +15,7 @@ use crate::{
|
||||||
raw::{ImportSourceMap, RawItems},
|
raw::{ImportSourceMap, RawItems},
|
||||||
CrateDefMap,
|
CrateDefMap,
|
||||||
},
|
},
|
||||||
ConstId, DefWithBodyId, EnumId, FunctionId, GenericDefId, ImplId, ItemLoc, StaticId,
|
AttrDefId, ConstId, DefWithBodyId, EnumId, FunctionId, GenericDefId, ImplId, ItemLoc, StaticId,
|
||||||
StructOrUnionId, TraitId, TypeAliasId,
|
StructOrUnionId, TraitId, TypeAliasId,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -87,4 +88,7 @@ pub trait DefDatabase2: InternDatabase + AstDatabase {
|
||||||
|
|
||||||
#[salsa::invoke(GenericParams::generic_params_query)]
|
#[salsa::invoke(GenericParams::generic_params_query)]
|
||||||
fn generic_params(&self, def: GenericDefId) -> Arc<GenericParams>;
|
fn generic_params(&self, def: GenericDefId) -> Arc<GenericParams>;
|
||||||
|
|
||||||
|
#[salsa::invoke(Attrs::attrs_query)]
|
||||||
|
fn attrs(&self, def: AttrDefId) -> Attrs;
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,7 @@ pub mod nameres;
|
||||||
|
|
||||||
use std::hash::{Hash, Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
|
|
||||||
use hir_expand::{ast_id_map::FileAstId, db::AstDatabase, AstId, HirFileId, Source};
|
use hir_expand::{ast_id_map::FileAstId, db::AstDatabase, AstId, HirFileId, MacroDefId, Source};
|
||||||
use ra_arena::{impl_arena_id, map::ArenaMap, RawId};
|
use ra_arena::{impl_arena_id, map::ArenaMap, RawId};
|
||||||
use ra_db::{salsa, CrateId, FileId};
|
use ra_db::{salsa, CrateId, FileId};
|
||||||
use ra_syntax::{ast, AstNode, SyntaxNode};
|
use ra_syntax::{ast, AstNode, SyntaxNode};
|
||||||
|
@ -280,8 +280,8 @@ pub enum VariantId {
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub struct StructFieldId {
|
pub struct StructFieldId {
|
||||||
parent: VariantId,
|
pub parent: VariantId,
|
||||||
local_id: LocalStructFieldId,
|
pub local_id: LocalStructFieldId,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
@ -477,6 +477,33 @@ impl_froms!(
|
||||||
ConstId
|
ConstId
|
||||||
);
|
);
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||||
|
pub enum AttrDefId {
|
||||||
|
ModuleId(ModuleId),
|
||||||
|
StructFieldId(StructFieldId),
|
||||||
|
AdtId(AdtId),
|
||||||
|
FunctionId(FunctionId),
|
||||||
|
EnumVariantId(EnumVariantId),
|
||||||
|
StaticId(StaticId),
|
||||||
|
ConstId(ConstId),
|
||||||
|
TraitId(TraitId),
|
||||||
|
TypeAliasId(TypeAliasId),
|
||||||
|
MacroDefId(MacroDefId),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl_froms!(
|
||||||
|
AttrDefId: ModuleId,
|
||||||
|
StructFieldId,
|
||||||
|
AdtId(StructId, EnumId, UnionId),
|
||||||
|
EnumVariantId,
|
||||||
|
StaticId,
|
||||||
|
ConstId,
|
||||||
|
FunctionId,
|
||||||
|
TraitId,
|
||||||
|
TypeAliasId,
|
||||||
|
MacroDefId
|
||||||
|
);
|
||||||
|
|
||||||
trait Intern {
|
trait Intern {
|
||||||
type ID;
|
type ID;
|
||||||
fn intern(self, db: &impl db::DefDatabase2) -> Self::ID;
|
fn intern(self, db: &impl db::DefDatabase2) -> Self::ID;
|
||||||
|
|
|
@ -58,7 +58,10 @@ mod tests;
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use hir_expand::{ast_id_map::FileAstId, diagnostics::DiagnosticSink, name::Name, MacroDefId};
|
use hir_expand::{
|
||||||
|
ast_id_map::FileAstId, diagnostics::DiagnosticSink, either::Either, name::Name, MacroDefId,
|
||||||
|
Source,
|
||||||
|
};
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use ra_arena::Arena;
|
use ra_arena::Arena;
|
||||||
use ra_db::{CrateId, Edition, FileId};
|
use ra_db::{CrateId, Edition, FileId};
|
||||||
|
@ -116,12 +119,15 @@ pub struct ModuleData {
|
||||||
pub parent: Option<CrateModuleId>,
|
pub parent: Option<CrateModuleId>,
|
||||||
pub children: FxHashMap<Name, CrateModuleId>,
|
pub children: FxHashMap<Name, CrateModuleId>,
|
||||||
pub scope: ModuleScope,
|
pub scope: ModuleScope,
|
||||||
|
|
||||||
|
// FIXME: these can't be both null, we need a three-state enum here.
|
||||||
/// None for root
|
/// None for root
|
||||||
pub declaration: Option<AstId<ast::Module>>,
|
pub declaration: Option<AstId<ast::Module>>,
|
||||||
/// None for inline modules.
|
/// None for inline modules.
|
||||||
///
|
///
|
||||||
/// Note that non-inline modules, by definition, live inside non-macro file.
|
/// Note that non-inline modules, by definition, live inside non-macro file.
|
||||||
pub definition: Option<FileId>,
|
pub definition: Option<FileId>,
|
||||||
|
|
||||||
pub impls: Vec<ImplId>,
|
pub impls: Vec<ImplId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -285,6 +291,29 @@ impl CrateDefMap {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ModuleData {
|
||||||
|
/// Returns a node which defines this module. That is, a file or a `mod foo {}` with items.
|
||||||
|
pub fn definition_source(
|
||||||
|
&self,
|
||||||
|
db: &impl DefDatabase2,
|
||||||
|
) -> Source<Either<ast::SourceFile, ast::Module>> {
|
||||||
|
if let Some(file_id) = self.definition {
|
||||||
|
let sf = db.parse(file_id).tree();
|
||||||
|
return Source::new(file_id.into(), Either::A(sf));
|
||||||
|
}
|
||||||
|
let decl = self.declaration.unwrap();
|
||||||
|
Source::new(decl.file_id(), Either::B(decl.to_node(db)))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.
|
||||||
|
/// `None` for the crate root.
|
||||||
|
pub fn declaration_source(&self, db: &impl DefDatabase2) -> Option<Source<ast::Module>> {
|
||||||
|
let decl = self.declaration?;
|
||||||
|
let value = decl.to_node(db);
|
||||||
|
Some(Source { file_id: decl.file_id(), value })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
mod diagnostics {
|
mod diagnostics {
|
||||||
use hir_expand::diagnostics::DiagnosticSink;
|
use hir_expand::diagnostics::DiagnosticSink;
|
||||||
use ra_db::RelativePathBuf;
|
use ra_db::RelativePathBuf;
|
||||||
|
|
Loading…
Reference in a new issue