1 //! Defines database & queries for name resolution.
4 use base_db::{salsa, CrateId, SourceDatabase, Upcast};
5 use hir_expand::{db::AstDatabase, HirFileId};
6 use la_arena::ArenaMap;
10 adt::{EnumData, StructData},
11 attr::{Attrs, AttrsWithOwner},
12 body::{scope::ExprScopes, Body, BodySourceMap},
13 data::{ConstData, FunctionData, ImplData, StaticData, TraitData, TypeAliasData},
14 generics::GenericParams,
15 import_map::ImportMap,
18 lang_item::{LangItemTarget, LangItems},
20 visibility::{self, Visibility},
21 AttrDefId, BlockId, BlockLoc, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, FunctionId,
22 FunctionLoc, GenericDefId, ImplId, ImplLoc, LocalEnumVariantId, LocalFieldId, StaticId,
23 StaticLoc, StructId, StructLoc, TraitId, TraitLoc, TypeAliasId, TypeAliasLoc, UnionId,
27 #[salsa::query_group(InternDatabaseStorage)]
28 pub trait InternDatabase: SourceDatabase {
30 fn intern_function(&self, loc: FunctionLoc) -> FunctionId;
32 fn intern_struct(&self, loc: StructLoc) -> StructId;
34 fn intern_union(&self, loc: UnionLoc) -> UnionId;
36 fn intern_enum(&self, loc: EnumLoc) -> EnumId;
38 fn intern_const(&self, loc: ConstLoc) -> ConstId;
40 fn intern_static(&self, loc: StaticLoc) -> StaticId;
42 fn intern_trait(&self, loc: TraitLoc) -> TraitId;
44 fn intern_type_alias(&self, loc: TypeAliasLoc) -> TypeAliasId;
46 fn intern_impl(&self, loc: ImplLoc) -> ImplId;
48 fn intern_block(&self, loc: BlockLoc) -> BlockId;
51 #[salsa::query_group(DefDatabaseStorage)]
52 pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> {
53 #[salsa::invoke(ItemTree::file_item_tree_query)]
54 fn file_item_tree(&self, file_id: HirFileId) -> Arc<ItemTree>;
56 #[salsa::invoke(crate_def_map_wait)]
58 fn crate_def_map(&self, krate: CrateId) -> Arc<DefMap>;
60 #[salsa::invoke(DefMap::crate_def_map_query)]
61 fn crate_def_map_query(&self, krate: CrateId) -> Arc<DefMap>;
63 /// Computes the block-level `DefMap`, returning `None` when `block` doesn't contain any inner
76 /// The `block_def_map` for block 0 would return `None`, while `block_def_map` of block 1 would
77 /// return a `DefMap` containing `inner`.
78 #[salsa::invoke(DefMap::block_def_map_query)]
79 fn block_def_map(&self, block: BlockId) -> Option<Arc<DefMap>>;
81 #[salsa::invoke(StructData::struct_data_query)]
82 fn struct_data(&self, id: StructId) -> Arc<StructData>;
83 #[salsa::invoke(StructData::union_data_query)]
84 fn union_data(&self, id: UnionId) -> Arc<StructData>;
86 #[salsa::invoke(EnumData::enum_data_query)]
87 fn enum_data(&self, e: EnumId) -> Arc<EnumData>;
89 #[salsa::invoke(ImplData::impl_data_query)]
90 fn impl_data(&self, e: ImplId) -> Arc<ImplData>;
92 #[salsa::invoke(TraitData::trait_data_query)]
93 fn trait_data(&self, e: TraitId) -> Arc<TraitData>;
95 #[salsa::invoke(TypeAliasData::type_alias_data_query)]
96 fn type_alias_data(&self, e: TypeAliasId) -> Arc<TypeAliasData>;
98 #[salsa::invoke(FunctionData::fn_data_query)]
99 fn function_data(&self, func: FunctionId) -> Arc<FunctionData>;
101 #[salsa::invoke(ConstData::const_data_query)]
102 fn const_data(&self, konst: ConstId) -> Arc<ConstData>;
104 #[salsa::invoke(StaticData::static_data_query)]
105 fn static_data(&self, konst: StaticId) -> Arc<StaticData>;
107 #[salsa::invoke(Body::body_with_source_map_query)]
108 fn body_with_source_map(&self, def: DefWithBodyId) -> (Arc<Body>, Arc<BodySourceMap>);
110 #[salsa::invoke(Body::body_query)]
111 fn body(&self, def: DefWithBodyId) -> Arc<Body>;
113 #[salsa::invoke(ExprScopes::expr_scopes_query)]
114 fn expr_scopes(&self, def: DefWithBodyId) -> Arc<ExprScopes>;
116 #[salsa::invoke(GenericParams::generic_params_query)]
117 fn generic_params(&self, def: GenericDefId) -> Interned<GenericParams>;
119 #[salsa::invoke(Attrs::variants_attrs_query)]
120 fn variants_attrs(&self, def: EnumId) -> Arc<ArenaMap<LocalEnumVariantId, Attrs>>;
122 #[salsa::invoke(Attrs::fields_attrs_query)]
123 fn fields_attrs(&self, def: VariantId) -> Arc<ArenaMap<LocalFieldId, Attrs>>;
125 #[salsa::invoke(AttrsWithOwner::attrs_query)]
126 fn attrs(&self, def: AttrDefId) -> AttrsWithOwner;
128 #[salsa::invoke(LangItems::crate_lang_items_query)]
129 fn crate_lang_items(&self, krate: CrateId) -> Arc<LangItems>;
131 #[salsa::invoke(LangItems::lang_item_query)]
132 fn lang_item(&self, start_crate: CrateId, item: SmolStr) -> Option<LangItemTarget>;
134 #[salsa::invoke(ImportMap::import_map_query)]
135 fn import_map(&self, krate: CrateId) -> Arc<ImportMap>;
137 #[salsa::invoke(visibility::field_visibilities_query)]
138 fn field_visibilities(&self, var: VariantId) -> Arc<ArenaMap<LocalFieldId, Visibility>>;
140 #[salsa::invoke(visibility::function_visibility_query)]
141 fn function_visibility(&self, def: FunctionId) -> Visibility;
144 fn crate_def_map_wait(db: &dyn DefDatabase, krate: CrateId) -> Arc<DefMap> {
145 let _p = profile::span("crate_def_map:wait");
146 db.crate_def_map_query(krate)