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},
15 ConstData, FunctionData, ImplData, Macro2Data, MacroRulesData, ProcMacroData, StaticData,
16 TraitData, TypeAliasData,
18 generics::GenericParams,
19 import_map::ImportMap,
21 item_tree::{AttrOwner, ItemTree},
22 lang_item::{LangItemTarget, LangItems},
23 nameres::{diagnostics::DefDiagnostic, DefMap},
24 visibility::{self, Visibility},
25 AttrDefId, BlockId, BlockLoc, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, ExternBlockId,
26 ExternBlockLoc, FunctionId, FunctionLoc, GenericDefId, ImplId, ImplLoc, LocalEnumVariantId,
27 LocalFieldId, Macro2Id, Macro2Loc, MacroRulesId, MacroRulesLoc, ProcMacroId, ProcMacroLoc,
28 StaticId, StaticLoc, StructId, StructLoc, TraitId, TraitLoc, TypeAliasId, TypeAliasLoc,
29 UnionId, UnionLoc, VariantId,
32 #[salsa::query_group(InternDatabaseStorage)]
33 pub trait InternDatabase: SourceDatabase {
35 fn intern_function(&self, loc: FunctionLoc) -> FunctionId;
37 fn intern_struct(&self, loc: StructLoc) -> StructId;
39 fn intern_union(&self, loc: UnionLoc) -> UnionId;
41 fn intern_enum(&self, loc: EnumLoc) -> EnumId;
43 fn intern_const(&self, loc: ConstLoc) -> ConstId;
45 fn intern_static(&self, loc: StaticLoc) -> StaticId;
47 fn intern_trait(&self, loc: TraitLoc) -> TraitId;
49 fn intern_type_alias(&self, loc: TypeAliasLoc) -> TypeAliasId;
51 fn intern_impl(&self, loc: ImplLoc) -> ImplId;
53 fn intern_extern_block(&self, loc: ExternBlockLoc) -> ExternBlockId;
55 fn intern_block(&self, loc: BlockLoc) -> BlockId;
57 fn intern_macro2(&self, loc: Macro2Loc) -> Macro2Id;
59 fn intern_proc_macro(&self, loc: ProcMacroLoc) -> ProcMacroId;
61 fn intern_macro_rules(&self, loc: MacroRulesLoc) -> MacroRulesId;
64 #[salsa::query_group(DefDatabaseStorage)]
65 pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> {
67 fn enable_proc_attr_macros(&self) -> bool;
69 #[salsa::invoke(ItemTree::file_item_tree_query)]
70 fn file_item_tree(&self, file_id: HirFileId) -> Arc<ItemTree>;
72 #[salsa::invoke(crate_def_map_wait)]
74 fn crate_def_map(&self, krate: CrateId) -> Arc<DefMap>;
76 #[salsa::invoke(DefMap::crate_def_map_query)]
77 fn crate_def_map_query(&self, krate: CrateId) -> Arc<DefMap>;
79 /// Computes the block-level `DefMap`, returning `None` when `block` doesn't contain any inner
92 /// The `block_def_map` for block 0 would return `None`, while `block_def_map` of block 1 would
93 /// return a `DefMap` containing `inner`.
94 #[salsa::invoke(DefMap::block_def_map_query)]
95 fn block_def_map(&self, block: BlockId) -> Option<Arc<DefMap>>;
97 #[salsa::invoke(StructData::struct_data_query)]
98 fn struct_data(&self, id: StructId) -> Arc<StructData>;
100 #[salsa::invoke(StructData::union_data_query)]
101 fn union_data(&self, id: UnionId) -> Arc<StructData>;
103 #[salsa::invoke(EnumData::enum_data_query)]
104 fn enum_data(&self, e: EnumId) -> Arc<EnumData>;
106 #[salsa::invoke(ImplData::impl_data_query)]
107 fn impl_data(&self, e: ImplId) -> Arc<ImplData>;
109 #[salsa::invoke(ImplData::impl_data_with_diagnostics_query)]
110 fn impl_data_with_diagnostics(&self, e: ImplId) -> (Arc<ImplData>, Arc<Vec<DefDiagnostic>>);
112 #[salsa::invoke(TraitData::trait_data_query)]
113 fn trait_data(&self, e: TraitId) -> Arc<TraitData>;
115 #[salsa::invoke(TraitData::trait_data_with_diagnostics_query)]
116 fn trait_data_with_diagnostics(&self, tr: TraitId)
117 -> (Arc<TraitData>, Arc<Vec<DefDiagnostic>>);
119 #[salsa::invoke(TypeAliasData::type_alias_data_query)]
120 fn type_alias_data(&self, e: TypeAliasId) -> Arc<TypeAliasData>;
122 #[salsa::invoke(FunctionData::fn_data_query)]
123 fn function_data(&self, func: FunctionId) -> Arc<FunctionData>;
125 #[salsa::invoke(ConstData::const_data_query)]
126 fn const_data(&self, konst: ConstId) -> Arc<ConstData>;
128 #[salsa::invoke(StaticData::static_data_query)]
129 fn static_data(&self, konst: StaticId) -> Arc<StaticData>;
131 #[salsa::invoke(Macro2Data::macro2_data_query)]
132 fn macro2_data(&self, makro: Macro2Id) -> Arc<Macro2Data>;
134 #[salsa::invoke(MacroRulesData::macro_rules_data_query)]
135 fn macro_rules_data(&self, makro: MacroRulesId) -> Arc<MacroRulesData>;
137 #[salsa::invoke(ProcMacroData::proc_macro_data_query)]
138 fn proc_macro_data(&self, makro: ProcMacroId) -> Arc<ProcMacroData>;
140 #[salsa::invoke(Body::body_with_source_map_query)]
141 fn body_with_source_map(&self, def: DefWithBodyId) -> (Arc<Body>, Arc<BodySourceMap>);
143 #[salsa::invoke(Body::body_query)]
144 fn body(&self, def: DefWithBodyId) -> Arc<Body>;
146 #[salsa::invoke(ExprScopes::expr_scopes_query)]
147 fn expr_scopes(&self, def: DefWithBodyId) -> Arc<ExprScopes>;
149 #[salsa::invoke(GenericParams::generic_params_query)]
150 fn generic_params(&self, def: GenericDefId) -> Interned<GenericParams>;
152 #[salsa::invoke(Attrs::variants_attrs_query)]
153 fn variants_attrs(&self, def: EnumId) -> Arc<ArenaMap<LocalEnumVariantId, Attrs>>;
155 #[salsa::invoke(Attrs::fields_attrs_query)]
156 fn fields_attrs(&self, def: VariantId) -> Arc<ArenaMap<LocalFieldId, Attrs>>;
158 #[salsa::invoke(crate::attr::variants_attrs_source_map)]
159 fn variants_attrs_source_map(
162 ) -> Arc<ArenaMap<LocalEnumVariantId, AstPtr<ast::Variant>>>;
164 #[salsa::invoke(crate::attr::fields_attrs_source_map)]
165 fn fields_attrs_source_map(
168 ) -> Arc<ArenaMap<LocalFieldId, Either<AstPtr<ast::TupleField>, AstPtr<ast::RecordField>>>>;
170 #[salsa::invoke(AttrsWithOwner::attrs_query)]
171 fn attrs(&self, def: AttrDefId) -> AttrsWithOwner;
173 #[salsa::invoke(LangItems::crate_lang_items_query)]
174 fn crate_lang_items(&self, krate: CrateId) -> Arc<LangItems>;
176 #[salsa::invoke(LangItems::lang_item_query)]
177 fn lang_item(&self, start_crate: CrateId, item: SmolStr) -> Option<LangItemTarget>;
179 #[salsa::invoke(ImportMap::import_map_query)]
180 fn import_map(&self, krate: CrateId) -> Arc<ImportMap>;
182 #[salsa::invoke(visibility::field_visibilities_query)]
183 fn field_visibilities(&self, var: VariantId) -> Arc<ArenaMap<LocalFieldId, Visibility>>;
185 // FIXME: unify function_visibility and const_visibility?
186 #[salsa::invoke(visibility::function_visibility_query)]
187 fn function_visibility(&self, def: FunctionId) -> Visibility;
189 #[salsa::invoke(visibility::const_visibility_query)]
190 fn const_visibility(&self, def: ConstId) -> Visibility;
192 #[salsa::transparent]
193 fn crate_limits(&self, crate_id: CrateId) -> CrateLimits;
195 fn crate_supports_no_std(&self, crate_id: CrateId) -> bool;
198 fn crate_def_map_wait(db: &dyn DefDatabase, krate: CrateId) -> Arc<DefMap> {
199 let _p = profile::span("crate_def_map:wait");
200 db.crate_def_map_query(krate)
203 pub struct CrateLimits {
204 /// The maximum depth for potentially infinitely-recursive compile-time operations like macro expansion or auto-dereference.
205 pub recursion_limit: u32,
208 fn crate_limits(db: &dyn DefDatabase, crate_id: CrateId) -> CrateLimits {
209 let def_map = db.crate_def_map(crate_id);
212 // 128 is the default in rustc.
213 recursion_limit: def_map.recursion_limit().unwrap_or(128),
217 fn crate_supports_no_std(db: &dyn DefDatabase, crate_id: CrateId) -> bool {
218 let file = db.crate_graph()[crate_id].root_file_id;
219 let item_tree = db.file_item_tree(file.into());
220 let attrs = item_tree.raw_attrs(AttrOwner::TopLevel);
221 for attr in &**attrs {
222 match attr.path().as_ident().and_then(|id| id.as_text()) {
223 Some(ident) if ident == "no_std" => return true,
224 Some(ident) if ident == "cfg_attr" => {}
228 // This is a `cfg_attr`; check if it could possibly expand to `no_std`.
229 // Syntax is: `#[cfg_attr(condition(cfg, style), attr0, attr1, <...>)]`
230 let tt = match attr.token_tree_value() {
231 Some(tt) => &tt.token_trees,
235 let segments = tt.split(|tt| match tt {
236 tt::TokenTree::Leaf(tt::Leaf::Punct(p)) if p.char == ',' => true,
239 for output in segments.skip(1) {
241 [tt::TokenTree::Leaf(tt::Leaf::Ident(ident))] if ident.text == "no_std" => {