Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-def/src/item_tree/lower.rs')
-rw-r--r--crates/hir-def/src/item_tree/lower.rs35
1 files changed, 18 insertions, 17 deletions
diff --git a/crates/hir-def/src/item_tree/lower.rs b/crates/hir-def/src/item_tree/lower.rs
index db50e6585d..d8519f7393 100644
--- a/crates/hir-def/src/item_tree/lower.rs
+++ b/crates/hir-def/src/item_tree/lower.rs
@@ -1,8 +1,9 @@
//! AST -> `ItemTree` lowering code.
-use std::{cell::OnceCell, collections::hash_map::Entry};
+use std::cell::OnceCell;
use base_db::FxIndexSet;
+use cfg::CfgOptions;
use hir_expand::{
HirFileId,
mod_path::PathKind,
@@ -22,18 +23,19 @@ use crate::{
item_tree::{
BigModItem, Const, Enum, ExternBlock, ExternCrate, FieldsShape, Function, Impl,
ImportAlias, Interned, ItemTree, ItemTreeAstId, Macro2, MacroCall, MacroRules, Mod,
- ModItemId, ModKind, ModPath, RawAttrs, RawVisibility, RawVisibilityId, SmallModItem,
- Static, Struct, StructKind, Trait, TypeAlias, Union, Use, UseTree, UseTreeKind,
- VisibilityExplicitness,
+ ModItemId, ModKind, ModPath, RawVisibility, RawVisibilityId, SmallModItem, Static, Struct,
+ StructKind, Trait, TypeAlias, Union, Use, UseTree, UseTreeKind, VisibilityExplicitness,
+ attrs::AttrsOrCfg,
},
};
pub(super) struct Ctx<'a> {
- db: &'a dyn DefDatabase,
+ pub(super) db: &'a dyn DefDatabase,
tree: ItemTree,
source_ast_id_map: Arc<AstIdMap>,
span_map: OnceCell<SpanMap>,
file: HirFileId,
+ cfg_options: OnceCell<&'a CfgOptions>,
top_level: Vec<ModItemId>,
visibilities: FxIndexSet<RawVisibility>,
}
@@ -45,12 +47,18 @@ impl<'a> Ctx<'a> {
tree: ItemTree::default(),
source_ast_id_map: db.ast_id_map(file),
file,
+ cfg_options: OnceCell::new(),
span_map: OnceCell::new(),
visibilities: FxIndexSet::default(),
top_level: Vec::new(),
}
}
+ #[inline]
+ pub(super) fn cfg_options(&self) -> &'a CfgOptions {
+ self.cfg_options.get_or_init(|| self.file.krate(self.db).cfg_options(self.db))
+ }
+
pub(super) fn span_map(&self) -> SpanMapRef<'_> {
self.span_map.get_or_init(|| self.db.span_map(self.file)).as_ref()
}
@@ -98,7 +106,7 @@ impl<'a> Ctx<'a> {
}
pub(super) fn lower_block(mut self, block: &ast::BlockExpr) -> ItemTree {
- self.tree.top_attrs = RawAttrs::new(self.db, block, self.span_map());
+ self.tree.top_attrs = self.lower_attrs(block);
self.top_level = block
.statements()
.filter_map(|stmt| match stmt {
@@ -144,22 +152,15 @@ impl<'a> Ctx<'a> {
// FIXME: Handle `global_asm!()`.
ast::Item::AsmExpr(_) => return None,
};
- let attrs = RawAttrs::new(self.db, item, self.span_map());
+ let attrs = self.lower_attrs(item);
self.add_attrs(mod_item.ast_id(), attrs);
Some(mod_item)
}
- fn add_attrs(&mut self, item: FileAstId<ast::Item>, attrs: RawAttrs) {
+ fn add_attrs(&mut self, item: FileAstId<ast::Item>, attrs: AttrsOrCfg) {
if !attrs.is_empty() {
- match self.tree.attrs.entry(item) {
- Entry::Occupied(mut entry) => {
- *entry.get_mut() = entry.get().merge(attrs);
- }
- Entry::Vacant(entry) => {
- entry.insert(attrs);
- }
- }
+ self.tree.attrs.insert(item, attrs);
}
}
@@ -352,7 +353,7 @@ impl<'a> Ctx<'a> {
ast::ExternItem::TypeAlias(ty) => self.lower_type_alias(ty)?.into(),
ast::ExternItem::MacroCall(call) => self.lower_macro_call(call)?.into(),
};
- let attrs = RawAttrs::new(self.db, &item, self.span_map());
+ let attrs = self.lower_attrs(&item);
self.add_attrs(mod_item.ast_id(), attrs);
Some(mod_item)
})