Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-def/src/item_tree.rs')
| -rw-r--r-- | crates/hir-def/src/item_tree.rs | 40 |
1 files changed, 26 insertions, 14 deletions
diff --git a/crates/hir-def/src/item_tree.rs b/crates/hir-def/src/item_tree.rs index 2a104fff2b..f35df8d3a7 100644 --- a/crates/hir-def/src/item_tree.rs +++ b/crates/hir-def/src/item_tree.rs @@ -30,7 +30,6 @@ //! In general, any item in the `ItemTree` stores its `AstId`, which allows mapping it back to its //! surface syntax. -mod attrs; mod lower; mod pretty; #[cfg(test)] @@ -44,8 +43,10 @@ use std::{ }; use ast::{AstNode, StructKind}; +use base_db::Crate; use hir_expand::{ ExpandTo, HirFileId, + attrs::RawAttrs, mod_path::{ModPath, PathKind}, name::Name, }; @@ -58,12 +59,9 @@ use syntax::{SyntaxKind, ast, match_ast}; use thin_vec::ThinVec; use triomphe::Arc; -use crate::{BlockId, Lookup, db::DefDatabase}; +use crate::{BlockId, Lookup, attr::Attrs, db::DefDatabase}; -pub(crate) use crate::item_tree::{ - attrs::*, - lower::{lower_use_tree, visibility_from_ast}, -}; +pub(crate) use crate::item_tree::lower::{lower_use_tree, visibility_from_ast}; #[derive(Copy, Clone, Eq, PartialEq)] pub(crate) struct RawVisibilityId(u32); @@ -98,7 +96,7 @@ pub(crate) fn file_item_tree_query(db: &dyn DefDatabase, file_id: HirFileId) -> let mut item_tree = match_ast! { match syntax { ast::SourceFile(file) => { - let top_attrs = ctx.lower_attrs(&file); + let top_attrs = RawAttrs::new(db, &file, ctx.span_map()); let mut item_tree = ctx.lower_module_items(&file); item_tree.top_attrs = top_attrs; item_tree @@ -134,7 +132,7 @@ pub(crate) fn file_item_tree_query(db: &dyn DefDatabase, file_id: HirFileId) -> attrs: FxHashMap::default(), small_data: FxHashMap::default(), big_data: FxHashMap::default(), - top_attrs: AttrsOrCfg::empty(), + top_attrs: RawAttrs::EMPTY, vis: ItemVisibilities { arena: ThinVec::new() }, }) }) @@ -170,7 +168,7 @@ pub(crate) fn block_item_tree_query(db: &dyn DefDatabase, block: BlockId) -> Arc attrs: FxHashMap::default(), small_data: FxHashMap::default(), big_data: FxHashMap::default(), - top_attrs: AttrsOrCfg::empty(), + top_attrs: RawAttrs::EMPTY, vis: ItemVisibilities { arena: ThinVec::new() }, }) }) @@ -184,8 +182,8 @@ pub(crate) fn block_item_tree_query(db: &dyn DefDatabase, block: BlockId) -> Arc #[derive(Debug, Default, Eq, PartialEq)] pub struct ItemTree { top_level: Box<[ModItemId]>, - top_attrs: AttrsOrCfg, - attrs: FxHashMap<FileAstId<ast::Item>, AttrsOrCfg>, + top_attrs: RawAttrs, + attrs: FxHashMap<FileAstId<ast::Item>, RawAttrs>, vis: ItemVisibilities, big_data: FxHashMap<FileAstId<ast::Item>, BigModItem>, small_data: FxHashMap<FileAstId<ast::Item>, SmallModItem>, @@ -199,12 +197,26 @@ impl ItemTree { } /// Returns the inner attributes of the source file. - pub(crate) fn top_level_attrs(&self) -> &AttrsOrCfg { + pub(crate) fn top_level_raw_attrs(&self) -> &RawAttrs { &self.top_attrs } - pub(crate) fn attrs(&self, of: FileAstId<ast::Item>) -> Option<&AttrsOrCfg> { - self.attrs.get(&of) + /// Returns the inner attributes of the source file. + pub(crate) fn top_level_attrs(&self, db: &dyn DefDatabase, krate: Crate) -> Attrs { + Attrs::expand_cfg_attr(db, krate, self.top_attrs.clone()) + } + + pub(crate) fn raw_attrs(&self, of: FileAstId<ast::Item>) -> &RawAttrs { + self.attrs.get(&of).unwrap_or(&RawAttrs::EMPTY) + } + + pub(crate) fn attrs( + &self, + db: &dyn DefDatabase, + krate: Crate, + of: FileAstId<ast::Item>, + ) -> Attrs { + Attrs::expand_cfg_attr(db, krate, self.raw_attrs(of).clone()) } /// Returns a count of a few, expensive items. |