aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_def/src/item_tree.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_def/src/item_tree.rs')
-rw-r--r--crates/hir_def/src/item_tree.rs43
1 files changed, 28 insertions, 15 deletions
diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs
index 1c9babf37..5eb7cae7f 100644
--- a/crates/hir_def/src/item_tree.rs
+++ b/crates/hir_def/src/item_tree.rs
@@ -13,6 +13,7 @@ use std::{
13 13
14use arena::{Arena, Idx, RawId}; 14use arena::{Arena, Idx, RawId};
15use ast::{AstNode, AttrsOwner, NameOwner, StructKind}; 15use ast::{AstNode, AttrsOwner, NameOwner, StructKind};
16use base_db::CrateId;
16use either::Either; 17use either::Either;
17use hir_expand::{ 18use hir_expand::{
18 ast_id_map::FileAstId, 19 ast_id_map::FileAstId,
@@ -26,7 +27,7 @@ use syntax::{ast, match_ast};
26use test_utils::mark; 27use test_utils::mark;
27 28
28use crate::{ 29use crate::{
29 attr::Attrs, 30 attr::{Attrs, RawAttrs},
30 db::DefDatabase, 31 db::DefDatabase,
31 generics::GenericParams, 32 generics::GenericParams,
32 path::{path, AssociatedTypeBinding, GenericArgs, ImportAlias, ModPath, Path, PathKind}, 33 path::{path, AssociatedTypeBinding, GenericArgs, ImportAlias, ModPath, Path, PathKind},
@@ -67,7 +68,7 @@ impl GenericParamsId {
67#[derive(Debug, Eq, PartialEq)] 68#[derive(Debug, Eq, PartialEq)]
68pub struct ItemTree { 69pub struct ItemTree {
69 top_level: SmallVec<[ModItem; 1]>, 70 top_level: SmallVec<[ModItem; 1]>,
70 attrs: FxHashMap<AttrOwner, Attrs>, 71 attrs: FxHashMap<AttrOwner, RawAttrs>,
71 inner_items: FxHashMap<FileAstId<ast::Item>, SmallVec<[ModItem; 1]>>, 72 inner_items: FxHashMap<FileAstId<ast::Item>, SmallVec<[ModItem; 1]>>,
72 73
73 data: Option<Box<ItemTreeData>>, 74 data: Option<Box<ItemTreeData>>,
@@ -88,7 +89,7 @@ impl ItemTree {
88 let mut item_tree = match_ast! { 89 let mut item_tree = match_ast! {
89 match syntax { 90 match syntax {
90 ast::SourceFile(file) => { 91 ast::SourceFile(file) => {
91 top_attrs = Some(Attrs::new(&file, &hygiene)); 92 top_attrs = Some(RawAttrs::new(&file, &hygiene));
92 ctx.lower_module_items(&file) 93 ctx.lower_module_items(&file)
93 }, 94 },
94 ast::MacroItems(items) => { 95 ast::MacroItems(items) => {
@@ -143,6 +144,7 @@ impl ItemTree {
143 mods, 144 mods,
144 macro_calls, 145 macro_calls,
145 macro_rules, 146 macro_rules,
147 macro_defs,
146 exprs, 148 exprs,
147 vis, 149 vis,
148 generics, 150 generics,
@@ -164,6 +166,7 @@ impl ItemTree {
164 mods.shrink_to_fit(); 166 mods.shrink_to_fit();
165 macro_calls.shrink_to_fit(); 167 macro_calls.shrink_to_fit();
166 macro_rules.shrink_to_fit(); 168 macro_rules.shrink_to_fit();
169 macro_defs.shrink_to_fit();
167 exprs.shrink_to_fit(); 170 exprs.shrink_to_fit();
168 171
169 vis.arena.shrink_to_fit(); 172 vis.arena.shrink_to_fit();
@@ -178,12 +181,16 @@ impl ItemTree {
178 } 181 }
179 182
180 /// Returns the inner attributes of the source file. 183 /// Returns the inner attributes of the source file.
181 pub fn top_level_attrs(&self) -> &Attrs { 184 pub fn top_level_attrs(&self, db: &dyn DefDatabase, krate: CrateId) -> Attrs {
182 self.attrs.get(&AttrOwner::TopLevel).unwrap_or(&Attrs::EMPTY) 185 self.attrs.get(&AttrOwner::TopLevel).unwrap_or(&RawAttrs::EMPTY).clone().filter(db, krate)
183 } 186 }
184 187
185 pub fn attrs(&self, of: AttrOwner) -> &Attrs { 188 pub(crate) fn raw_attrs(&self, of: AttrOwner) -> &RawAttrs {
186 self.attrs.get(&of).unwrap_or(&Attrs::EMPTY) 189 self.attrs.get(&of).unwrap_or(&RawAttrs::EMPTY)
190 }
191
192 pub fn attrs(&self, db: &dyn DefDatabase, krate: CrateId, of: AttrOwner) -> Attrs {
193 self.raw_attrs(of).clone().filter(db, krate)
187 } 194 }
188 195
189 /// Returns the lowered inner items that `ast` corresponds to. 196 /// Returns the lowered inner items that `ast` corresponds to.
@@ -283,6 +290,7 @@ struct ItemTreeData {
283 mods: Arena<Mod>, 290 mods: Arena<Mod>,
284 macro_calls: Arena<MacroCall>, 291 macro_calls: Arena<MacroCall>,
285 macro_rules: Arena<MacroRules>, 292 macro_rules: Arena<MacroRules>,
293 macro_defs: Arena<MacroDef>,
286 exprs: Arena<Expr>, 294 exprs: Arena<Expr>,
287 295
288 vis: ItemVisibilities, 296 vis: ItemVisibilities,
@@ -431,6 +439,7 @@ mod_items! {
431 Mod in mods -> ast::Module, 439 Mod in mods -> ast::Module,
432 MacroCall in macro_calls -> ast::MacroCall, 440 MacroCall in macro_calls -> ast::MacroCall,
433 MacroRules in macro_rules -> ast::MacroRules, 441 MacroRules in macro_rules -> ast::MacroRules,
442 MacroDef in macro_defs -> ast::MacroDef,
434} 443}
435 444
436macro_rules! impl_index { 445macro_rules! impl_index {
@@ -640,17 +649,19 @@ pub struct MacroCall {
640 649
641#[derive(Debug, Clone, Eq, PartialEq)] 650#[derive(Debug, Clone, Eq, PartialEq)]
642pub struct MacroRules { 651pub struct MacroRules {
643 /// For `macro_rules!` declarations, this is the name of the declared macro. 652 /// The name of the declared macro.
644 pub name: Name, 653 pub name: Name,
645 /// Has `#[macro_export]`.
646 pub is_export: bool,
647 /// Has `#[macro_export(local_inner_macros)]`.
648 pub is_local_inner: bool,
649 /// Has `#[rustc_builtin_macro]`.
650 pub is_builtin: bool,
651 pub ast_id: FileAstId<ast::MacroRules>, 654 pub ast_id: FileAstId<ast::MacroRules>,
652} 655}
653 656
657/// "Macros 2.0" macro definition.
658#[derive(Debug, Clone, Eq, PartialEq)]
659pub struct MacroDef {
660 pub name: Name,
661 pub visibility: RawVisibilityId,
662 pub ast_id: FileAstId<ast::MacroDef>,
663}
664
654// NB: There's no `FileAstId` for `Expr`. The only case where this would be useful is for array 665// NB: There's no `FileAstId` for `Expr`. The only case where this would be useful is for array
655// lengths, but we don't do much with them yet. 666// lengths, but we don't do much with them yet.
656#[derive(Debug, Clone, Eq, PartialEq)] 667#[derive(Debug, Clone, Eq, PartialEq)]
@@ -680,7 +691,8 @@ impl ModItem {
680 | ModItem::Trait(_) 691 | ModItem::Trait(_)
681 | ModItem::Impl(_) 692 | ModItem::Impl(_)
682 | ModItem::Mod(_) 693 | ModItem::Mod(_)
683 | ModItem::MacroRules(_) => None, 694 | ModItem::MacroRules(_)
695 | ModItem::MacroDef(_) => None,
684 ModItem::MacroCall(call) => Some(AssocItem::MacroCall(*call)), 696 ModItem::MacroCall(call) => Some(AssocItem::MacroCall(*call)),
685 ModItem::Const(konst) => Some(AssocItem::Const(*konst)), 697 ModItem::Const(konst) => Some(AssocItem::Const(*konst)),
686 ModItem::TypeAlias(alias) => Some(AssocItem::TypeAlias(*alias)), 698 ModItem::TypeAlias(alias) => Some(AssocItem::TypeAlias(*alias)),
@@ -708,6 +720,7 @@ impl ModItem {
708 ModItem::Mod(it) => tree[it.index].ast_id().upcast(), 720 ModItem::Mod(it) => tree[it.index].ast_id().upcast(),
709 ModItem::MacroCall(it) => tree[it.index].ast_id().upcast(), 721 ModItem::MacroCall(it) => tree[it.index].ast_id().upcast(),
710 ModItem::MacroRules(it) => tree[it.index].ast_id().upcast(), 722 ModItem::MacroRules(it) => tree[it.index].ast_id().upcast(),
723 ModItem::MacroDef(it) => tree[it.index].ast_id().upcast(),
711 } 724 }
712 } 725 }
713} 726}