1 //! Defines database & queries for name resolution.
4 use base_db::{salsa, CrateId, SourceDatabase, Upcast};
6 use hir_expand::{db::AstDatabase, HirFileId};
7 use la_arena::ArenaMap;
8 use syntax::{ast, AstPtr, SmolStr};
11 adt::{EnumData, StructData},
12 attr::{Attrs, AttrsWithOwner},
13 body::{scope::ExprScopes, Body, BodySourceMap},
14 data::{ConstData, FunctionData, ImplData, StaticData, TraitData, TypeAliasData},
15 generics::GenericParams,
16 import_map::ImportMap,
19 lang_item::{LangItemTarget, LangItems},
21 visibility::{self, Visibility},
22 AttrDefId, BlockId, BlockLoc, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, FunctionId,
23 FunctionLoc, GenericDefId, ImplId, ImplLoc, LocalEnumVariantId, LocalFieldId, StaticId,
24 StaticLoc, StructId, StructLoc, TraitId, TraitLoc, TypeAliasId, TypeAliasLoc, UnionId,
28 #[salsa::query_group(InternDatabaseStorage)]
29 pub trait InternDatabase: SourceDatabase {
31 fn intern_function(&self, loc: FunctionLoc) -> FunctionId;
33 fn intern_struct(&self, loc: StructLoc) -> StructId;
35 fn intern_union(&self, loc: UnionLoc) -> UnionId;
37 fn intern_enum(&self, loc: EnumLoc) -> EnumId;
39 fn intern_const(&self, loc: ConstLoc) -> ConstId;
41 fn intern_static(&self, loc: StaticLoc) -> StaticId;
43 fn intern_trait(&self, loc: TraitLoc) -> TraitId;
45 fn intern_type_alias(&self, loc: TypeAliasLoc) -> TypeAliasId;
47 fn intern_impl(&self, loc: ImplLoc) -> ImplId;
49 fn intern_block(&self, loc: BlockLoc) -> BlockId;
52 #[salsa::query_group(DefDatabaseStorage)]
53 pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> {
54 #[salsa::invoke(ItemTree::file_item_tree_query)]
55 fn file_item_tree(&self, file_id: HirFileId) -> Arc<ItemTree>;
57 #[salsa::invoke(crate_def_map_wait)]
59 fn crate_def_map(&self, krate: CrateId) -> Arc<DefMap>;
61 #[salsa::invoke(DefMap::crate_def_map_query)]
62 fn crate_def_map_query(&self, krate: CrateId) -> Arc<DefMap>;
64 /// Computes the block-level `DefMap`, returning `None` when `block` doesn't contain any inner
77 /// The `block_def_map` for block 0 would return `None`, while `block_def_map` of block 1 would
78 /// return a `DefMap` containing `inner`.
79 #[salsa::invoke(DefMap::block_def_map_query)]
80 fn block_def_map(&self, block: BlockId) -> Option<Arc<DefMap>>;
82 #[salsa::invoke(StructData::struct_data_query)]
83 fn struct_data(&self, id: StructId) -> Arc<StructData>;
84 #[salsa::invoke(StructData::union_data_query)]
85 fn union_data(&self, id: UnionId) -> Arc<StructData>;
87 #[salsa::invoke(EnumData::enum_data_query)]
88 fn enum_data(&self, e: EnumId) -> Arc<EnumData>;
90 #[salsa::invoke(ImplData::impl_data_query)]
91 fn impl_data(&self, e: ImplId) -> Arc<ImplData>;
93 #[salsa::invoke(TraitData::trait_data_query)]
94 fn trait_data(&self, e: TraitId) -> Arc<TraitData>;
96 #[salsa::invoke(TypeAliasData::type_alias_data_query)]
97 fn type_alias_data(&self, e: TypeAliasId) -> Arc<TypeAliasData>;
99 #[salsa::invoke(FunctionData::fn_data_query)]
100 fn function_data(&self, func: FunctionId) -> Arc<FunctionData>;
102 #[salsa::invoke(ConstData::const_data_query)]
103 fn const_data(&self, konst: ConstId) -> Arc<ConstData>;
105 #[salsa::invoke(StaticData::static_data_query)]
106 fn static_data(&self, konst: StaticId) -> Arc<StaticData>;
108 #[salsa::invoke(Body::body_with_source_map_query)]
109 fn body_with_source_map(&self, def: DefWithBodyId) -> (Arc<Body>, Arc<BodySourceMap>);
111 #[salsa::invoke(Body::body_query)]
112 fn body(&self, def: DefWithBodyId) -> Arc<Body>;
114 #[salsa::invoke(ExprScopes::expr_scopes_query)]
115 fn expr_scopes(&self, def: DefWithBodyId) -> Arc<ExprScopes>;
117 #[salsa::invoke(GenericParams::generic_params_query)]
118 fn generic_params(&self, def: GenericDefId) -> Interned<GenericParams>;
120 #[salsa::invoke(Attrs::variants_attrs_query)]
121 fn variants_attrs(&self, def: EnumId) -> Arc<ArenaMap<LocalEnumVariantId, Attrs>>;
123 #[salsa::invoke(Attrs::fields_attrs_query)]
124 fn fields_attrs(&self, def: VariantId) -> Arc<ArenaMap<LocalFieldId, Attrs>>;
126 #[salsa::invoke(crate::attr::variants_attrs_source_map)]
127 fn variants_attrs_source_map(
130 ) -> Arc<ArenaMap<LocalEnumVariantId, AstPtr<ast::Variant>>>;
132 #[salsa::invoke(crate::attr::fields_attrs_source_map)]
133 fn fields_attrs_source_map(
136 ) -> Arc<ArenaMap<LocalFieldId, Either<AstPtr<ast::TupleField>, AstPtr<ast::RecordField>>>>;
138 #[salsa::invoke(AttrsWithOwner::attrs_query)]
139 fn attrs(&self, def: AttrDefId) -> AttrsWithOwner;
141 #[salsa::invoke(LangItems::crate_lang_items_query)]
142 fn crate_lang_items(&self, krate: CrateId) -> Arc<LangItems>;
144 #[salsa::invoke(LangItems::lang_item_query)]
145 fn lang_item(&self, start_crate: CrateId, item: SmolStr) -> Option<LangItemTarget>;
147 #[salsa::invoke(ImportMap::import_map_query)]
148 fn import_map(&self, krate: CrateId) -> Arc<ImportMap>;
150 #[salsa::invoke(visibility::field_visibilities_query)]
151 fn field_visibilities(&self, var: VariantId) -> Arc<ArenaMap<LocalFieldId, Visibility>>;
153 #[salsa::invoke(visibility::function_visibility_query)]
154 fn function_visibility(&self, def: FunctionId) -> Visibility;
157 fn crate_def_map_wait(db: &dyn DefDatabase, krate: CrateId) -> Arc<DefMap> {
158 let _p = profile::span("crate_def_map:wait");
159 db.crate_def_map_query(krate)