aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/code_model.rs29
1 files changed, 28 insertions, 1 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 1bdcda069..efc3502d0 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -30,6 +30,7 @@ use ra_syntax::{
30 ast::{self, AttrsOwner}, 30 ast::{self, AttrsOwner},
31 AstNode, 31 AstNode,
32}; 32};
33use rustc_hash::FxHashSet;
33 34
34use crate::{ 35use crate::{
35 db::{DefDatabase, HirDatabase}, 36 db::{DefDatabase, HirDatabase},
@@ -123,10 +124,25 @@ impl_froms!(
123 BuiltinType 124 BuiltinType
124); 125);
125 126
127impl ModuleDef {
128 pub fn module(self, db: &impl HirDatabase) -> Option<Module> {
129 match self {
130 ModuleDef::Module(it) => it.parent(db),
131 ModuleDef::Function(it) => Some(it.module(db)),
132 ModuleDef::Adt(it) => Some(it.module(db)),
133 ModuleDef::EnumVariant(it) => Some(it.module(db)),
134 ModuleDef::Const(it) => Some(it.module(db)),
135 ModuleDef::Static(it) => Some(it.module(db)),
136 ModuleDef::Trait(it) => Some(it.module(db)),
137 ModuleDef::TypeAlias(it) => Some(it.module(db)),
138 ModuleDef::BuiltinType(_) => None,
139 }
140 }
141}
142
126pub use hir_def::{ 143pub use hir_def::{
127 attr::Attrs, item_scope::ItemInNs, visibility::Visibility, AssocItemId, AssocItemLoc, 144 attr::Attrs, item_scope::ItemInNs, visibility::Visibility, AssocItemId, AssocItemLoc,
128}; 145};
129use rustc_hash::FxHashSet;
130 146
131impl Module { 147impl Module {
132 pub(crate) fn new(krate: Crate, crate_module_id: LocalModuleId) -> Module { 148 pub(crate) fn new(krate: Crate, crate_module_id: LocalModuleId) -> Module {
@@ -649,6 +665,17 @@ pub struct MacroDef {
649 pub(crate) id: MacroDefId, 665 pub(crate) id: MacroDefId,
650} 666}
651 667
668impl MacroDef {
669 /// FIXME: right now, this just returns the root module of the crate that
670 /// defines this macro. The reasons for this is that macros are expanded
671 /// early, in `ra_hir_expand`, where modules simply do not exist yet.
672 pub fn module(self, db: &impl HirDatabase) -> Option<Module> {
673 let krate = self.id.krate?;
674 let module_id = db.crate_def_map(krate).root;
675 Some(Module::new(Crate { id: krate }, module_id))
676 }
677}
678
652/// Invariant: `inner.as_assoc_item(db).is_some()` 679/// Invariant: `inner.as_assoc_item(db).is_some()`
653/// We do not actively enforce this invariant. 680/// We do not actively enforce this invariant.
654#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] 681#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]