Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir/src/lib.rs')
-rw-r--r--crates/hir/src/lib.rs158
1 files changed, 87 insertions, 71 deletions
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index 60e3548d49..76bbe80e1f 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -49,10 +49,10 @@ use hir_def::{
src::HasSource as _,
AdtId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId, DefWithBodyId, EnumId,
FunctionId, GenericDefId, HasModule, ImplId, ItemContainerId, LifetimeParamId,
- LocalEnumVariantId, LocalFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId,
- TypeOrConstParamId, TypeParamId, UnionId,
+ LocalEnumVariantId, LocalFieldId, Lookup, MacroId, ModuleId, StaticId, StructId, TraitId,
+ TypeAliasId, TypeOrConstParamId, TypeParamId, UnionId,
};
-use hir_expand::{name::name, MacroCallKind, MacroDefId, MacroDefKind};
+use hir_expand::{name::name, MacroCallKind};
use hir_ty::{
autoderef,
consteval::{eval_const, ComputedExpr, ConstEvalCtx, ConstEvalError, ConstExt},
@@ -207,7 +207,7 @@ impl Crate {
self,
db: &dyn DefDatabase,
query: import_map::Query,
- ) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> {
+ ) -> impl Iterator<Item = Either<ModuleDef, Macro>> {
let _p = profile::span("query_external_importables");
import_map::search_dependencies(db, self.into(), query).into_iter().map(|item| {
match ItemInNs::from(item) {
@@ -272,6 +272,7 @@ pub enum ModuleDef {
Trait(Trait),
TypeAlias(TypeAlias),
BuiltinType(BuiltinType),
+ Macro(Macro),
}
impl_from!(
Module,
@@ -282,7 +283,8 @@ impl_from!(
Static,
Trait,
TypeAlias,
- BuiltinType
+ BuiltinType,
+ Macro
for ModuleDef
);
@@ -307,6 +309,7 @@ impl ModuleDef {
ModuleDef::Static(it) => Some(it.module(db)),
ModuleDef::Trait(it) => Some(it.module(db)),
ModuleDef::TypeAlias(it) => Some(it.module(db)),
+ ModuleDef::Macro(it) => Some(it.module(db)),
ModuleDef::BuiltinType(_) => None,
}
}
@@ -337,6 +340,7 @@ impl ModuleDef {
ModuleDef::Variant(it) => it.name(db),
ModuleDef::TypeAlias(it) => it.name(db),
ModuleDef::Static(it) => it.name(db),
+ ModuleDef::Macro(it) => it.name(db)?,
ModuleDef::BuiltinType(it) => it.name(),
};
Some(name)
@@ -390,6 +394,7 @@ impl ModuleDef {
| ModuleDef::Variant(_)
| ModuleDef::Trait(_)
| ModuleDef::TypeAlias(_)
+ | ModuleDef::Macro(_)
| ModuleDef::BuiltinType(_) => None,
}
}
@@ -404,6 +409,7 @@ impl ModuleDef {
ModuleDef::Static(it) => it.attrs(db),
ModuleDef::Trait(it) => it.attrs(db),
ModuleDef::TypeAlias(it) => it.attrs(db),
+ ModuleDef::Macro(it) => it.attrs(db),
ModuleDef::BuiltinType(_) => return None,
})
}
@@ -420,6 +426,8 @@ impl HasVisibility for ModuleDef {
ModuleDef::Trait(it) => it.visibility(db),
ModuleDef::TypeAlias(it) => it.visibility(db),
ModuleDef::Variant(it) => it.visibility(db),
+ // FIXME
+ ModuleDef::Macro(_) => Visibility::Public,
ModuleDef::BuiltinType(_) => Visibility::Public,
}
}
@@ -1376,25 +1384,27 @@ impl Function {
db.function_data(self.id).has_body()
}
- pub fn as_proc_macro(self, db: &dyn HirDatabase) -> Option<MacroDef> {
- let function_data = db.function_data(self.id);
- let attrs = &function_data.attrs;
- if !(attrs.is_proc_macro()
- || attrs.is_proc_macro_attribute()
- || attrs.is_proc_macro_derive())
- {
- return None;
- }
- let loc = self.id.lookup(db.upcast());
- let krate = loc.krate(db);
- let def_map = db.crate_def_map(krate.into());
- let ast_id =
- InFile::new(loc.id.file_id(), loc.id.item_tree(db.upcast())[loc.id.value].ast_id);
-
- let mut exported_proc_macros = def_map.exported_proc_macros();
- exported_proc_macros
- .find(|&(id, _)| matches!(id.kind, MacroDefKind::ProcMacro(_, _, id) if id == ast_id))
- .map(|(id, _)| MacroDef { id })
+ pub fn as_proc_macro(self, _db: &dyn HirDatabase) -> Option<Macro> {
+ // let function_data = db.function_data(self.id);
+ // let attrs = &function_data.attrs;
+ // if !(attrs.is_proc_macro()
+ // || attrs.is_proc_macro_attribute()
+ // || attrs.is_proc_macro_derive())
+ // {
+ // return None;
+ // }
+ // let loc = self.id.lookup(db.upcast());
+ // let krate = loc.krate(db);
+ // let def_map = db.crate_def_map(krate.into());
+ // let ast_id =
+ // InFile::new(loc.id.file_id(), loc.id.item_tree(db.upcast())[loc.id.value].ast_id);
+
+ // let mut exported_proc_macros = def_map.exported_proc_macros();
+ // exported_proc_macros
+ // .find(|&(id, _)| matches!(id.kind, MacroDefKind::ProcMacro(_, _, id) if id == ast_id))
+ // .map(|(id, _)| Macro { id })
+ // FIXME
+ None
}
/// A textual representation of the HIR of this function for debugging purposes.
@@ -1747,61 +1757,70 @@ pub enum MacroKind {
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
-pub struct MacroDef {
- pub(crate) id: MacroDefId,
+pub struct Macro {
+ pub(crate) id: MacroId,
}
-impl MacroDef {
- /// FIXME: right now, this just returns the root module of the crate that
- /// defines this macro. The reasons for this is that macros are expanded
- /// early, in `hir_expand`, where modules simply do not exist yet.
- pub fn module(self, db: &dyn HirDatabase) -> Option<Module> {
- let krate = self.id.krate;
- let def_map = db.crate_def_map(krate);
- let module_id = def_map.root();
- Some(Module { id: def_map.module_id(module_id) })
+impl Macro {
+ pub fn module(self, db: &dyn HirDatabase) -> Module {
+ Module { id: self.id.module(db.upcast()) }
}
- /// XXX: this parses the file
- pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
- match self.source(db)?.value {
- Either::Left(it) => it.name().map(|it| it.as_name()),
- Either::Right(_) => {
- let krate = self.id.krate;
- let def_map = db.crate_def_map(krate);
- let (_, name) = def_map.exported_proc_macros().find(|&(id, _)| id == self.id)?;
- Some(name)
- }
+ pub fn name(self, _db: &dyn HirDatabase) -> Option<Name> {
+ match self.id {
+ MacroId::Macro2Id(_id) => todo!(),
+ MacroId::MacroRulesId(_id) => todo!(),
+ MacroId::ProcMacroId(_id) => todo!(),
}
}
- pub fn kind(&self) -> MacroKind {
- match self.id.kind {
- MacroDefKind::Declarative(_) => MacroKind::Declarative,
- MacroDefKind::BuiltIn(_, _) | MacroDefKind::BuiltInEager(_, _) => MacroKind::BuiltIn,
- MacroDefKind::BuiltInDerive(_, _) => MacroKind::Derive,
- MacroDefKind::BuiltInAttr(_, _) => MacroKind::Attr,
- MacroDefKind::ProcMacro(_, base_db::ProcMacroKind::CustomDerive, _) => {
- MacroKind::Derive
- }
- MacroDefKind::ProcMacro(_, base_db::ProcMacroKind::Attr, _) => MacroKind::Attr,
- MacroDefKind::ProcMacro(_, base_db::ProcMacroKind::FuncLike, _) => MacroKind::ProcMacro,
+ pub fn kind(&self, db: &dyn HirDatabase) -> MacroKind {
+ match self.id {
+ MacroId::Macro2Id(it) => match it.lookup(db.upcast()).expander {
+ hir_def::MacroExpander::Declarative => MacroKind::Declarative,
+ hir_def::MacroExpander::BuiltIn(_) => MacroKind::BuiltIn,
+ hir_def::MacroExpander::BuiltInAttr(_) => MacroKind::Attr,
+ hir_def::MacroExpander::BuiltInDerive(_) => MacroKind::Derive,
+ hir_def::MacroExpander::BuiltInEager(_) => MacroKind::BuiltIn,
+ },
+ MacroId::MacroRulesId(it) => match it.lookup(db.upcast()).expander {
+ hir_def::MacroExpander::Declarative => MacroKind::Declarative,
+ hir_def::MacroExpander::BuiltIn(_) => MacroKind::BuiltIn,
+ hir_def::MacroExpander::BuiltInAttr(_) => MacroKind::Attr,
+ hir_def::MacroExpander::BuiltInDerive(_) => MacroKind::Derive,
+ hir_def::MacroExpander::BuiltInEager(_) => MacroKind::BuiltIn,
+ },
+ MacroId::ProcMacroId(it) => match it.lookup(db.upcast()).kind {
+ base_db::ProcMacroKind::CustomDerive => MacroKind::Derive,
+ base_db::ProcMacroKind::FuncLike => MacroKind::ProcMacro,
+ base_db::ProcMacroKind::Attr => MacroKind::Attr,
+ },
}
}
- pub fn is_fn_like(&self) -> bool {
- match self.kind() {
+ pub fn is_fn_like(&self, db: &dyn HirDatabase) -> bool {
+ match self.kind(db) {
MacroKind::Declarative | MacroKind::BuiltIn | MacroKind::ProcMacro => true,
MacroKind::Attr | MacroKind::Derive => false,
}
}
- pub fn is_builtin_derive(&self) -> bool {
- matches!(self.id.kind, MacroDefKind::BuiltInAttr(exp, _) if exp.is_derive())
+ pub fn is_builtin_derive(&self, db: &dyn HirDatabase) -> bool {
+ match self.id {
+ MacroId::Macro2Id(it) => match it.lookup(db.upcast()).expander {
+ hir_def::MacroExpander::BuiltInDerive(_) => true,
+ _ => false,
+ },
+ MacroId::MacroRulesId(it) => match it.lookup(db.upcast()).expander {
+ hir_def::MacroExpander::BuiltInDerive(_) => true,
+ _ => false,
+ },
+ MacroId::ProcMacroId(_) => false,
+ }
}
- pub fn is_attr(&self) -> bool {
- matches!(self.kind(), MacroKind::Attr)
+ pub fn is_attr(&self, db: &dyn HirDatabase) -> bool {
+ matches!(self.kind(db), MacroKind::Attr)
}
}
@@ -1809,11 +1828,11 @@ impl MacroDef {
pub enum ItemInNs {
Types(ModuleDef),
Values(ModuleDef),
- Macros(MacroDef),
+ Macros(Macro),
}
-impl From<MacroDef> for ItemInNs {
- fn from(it: MacroDef) -> Self {
+impl From<Macro> for ItemInNs {
+ fn from(it: Macro) -> Self {
Self::Macros(it)
}
}
@@ -1841,7 +1860,7 @@ impl ItemInNs {
pub fn krate(&self, db: &dyn HirDatabase) -> Option<Crate> {
match self {
ItemInNs::Types(did) | ItemInNs::Values(did) => did.module(db).map(|m| m.krate()),
- ItemInNs::Macros(id) => id.module(db).map(|m| m.krate()),
+ ItemInNs::Macros(id) => Some(id.module(db).krate()),
}
}
@@ -3224,7 +3243,6 @@ impl Callable {
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum ScopeDef {
ModuleDef(ModuleDef),
- MacroDef(MacroDef),
GenericParam(GenericParam),
ImplSelfType(Impl),
AdtSelfType(Adt),
@@ -3255,7 +3273,7 @@ impl ScopeDef {
};
if let Some(macro_def_id) = def.take_macros() {
- items.push(ScopeDef::MacroDef(macro_def_id.into()));
+ items.push(ScopeDef::ModuleDef(ModuleDef::Macro(macro_def_id.into())));
}
if items.is_empty() {
@@ -3268,7 +3286,6 @@ impl ScopeDef {
pub fn attrs(&self, db: &dyn HirDatabase) -> Option<AttrsWithOwner> {
match self {
ScopeDef::ModuleDef(it) => it.attrs(db),
- ScopeDef::MacroDef(it) => Some(it.attrs(db)),
ScopeDef::GenericParam(it) => Some(it.attrs(db)),
ScopeDef::ImplSelfType(_)
| ScopeDef::AdtSelfType(_)
@@ -3281,7 +3298,6 @@ impl ScopeDef {
pub fn krate(&self, db: &dyn HirDatabase) -> Option<Crate> {
match self {
ScopeDef::ModuleDef(it) => it.module(db).map(|m| m.krate()),
- ScopeDef::MacroDef(it) => it.module(db).map(|m| m.krate()),
ScopeDef::GenericParam(it) => Some(it.module(db).krate()),
ScopeDef::ImplSelfType(_) => None,
ScopeDef::AdtSelfType(it) => Some(it.module(db).krate()),
@@ -3297,7 +3313,7 @@ impl From<ItemInNs> for ScopeDef {
match item {
ItemInNs::Types(id) => ScopeDef::ModuleDef(id),
ItemInNs::Values(id) => ScopeDef::ModuleDef(id),
- ItemInNs::Macros(id) => ScopeDef::MacroDef(id),
+ ItemInNs::Macros(id) => ScopeDef::ModuleDef(ModuleDef::Macro(id)),
}
}
}