aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2021-05-29 19:32:57 +0100
committerJonas Schievink <[email protected]>2021-05-29 19:32:57 +0100
commit8c639a87bdbd08b10d4eb84d00f25ba7bcd56be0 (patch)
tree127d90ba4f4eac66081f9b78a2d53d2d713a0593 /crates
parentf6da603c7fe56c19a275dc7bab1f30fe1ad39707 (diff)
Thread proc-macro types through the HIR
Diffstat (limited to 'crates')
-rw-r--r--crates/base_db/src/input.rs2
-rw-r--r--crates/hir/src/lib.rs18
-rw-r--r--crates/hir_def/src/nameres/collector.rs9
-rw-r--r--crates/hir_def/src/nameres/proc_macro.rs10
-rw-r--r--crates/hir_expand/src/lib.rs5
5 files changed, 34 insertions, 10 deletions
diff --git a/crates/base_db/src/input.rs b/crates/base_db/src/input.rs
index 64ccd11ee..23cb0c839 100644
--- a/crates/base_db/src/input.rs
+++ b/crates/base_db/src/input.rs
@@ -147,7 +147,7 @@ impl CrateDisplayName {
147#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] 147#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
148pub struct ProcMacroId(pub u32); 148pub struct ProcMacroId(pub u32);
149 149
150#[derive(Copy, Clone, Eq, PartialEq, Debug)] 150#[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)]
151pub enum ProcMacroKind { 151pub enum ProcMacroKind {
152 CustomDerive, 152 CustomDerive,
153 FuncLike, 153 FuncLike,
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index 01b2de515..975ae4869 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -1282,10 +1282,16 @@ impl BuiltinType {
1282 1282
1283#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 1283#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1284pub enum MacroKind { 1284pub enum MacroKind {
1285 /// `macro_rules!` or Macros 2.0 macro.
1285 Declarative, 1286 Declarative,
1286 ProcMacro, 1287 /// A built-in or custom derive.
1287 Derive, 1288 Derive,
1289 /// A built-in function-like macro.
1288 BuiltIn, 1290 BuiltIn,
1291 /// A procedural attribute macro.
1292 Attr,
1293 /// A function-like procedural macro.
1294 ProcMacro,
1289} 1295}
1290 1296
1291#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 1297#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -1315,11 +1321,13 @@ impl MacroDef {
1315 pub fn kind(&self) -> MacroKind { 1321 pub fn kind(&self) -> MacroKind {
1316 match self.id.kind { 1322 match self.id.kind {
1317 MacroDefKind::Declarative(_) => MacroKind::Declarative, 1323 MacroDefKind::Declarative(_) => MacroKind::Declarative,
1318 MacroDefKind::BuiltIn(_, _) => MacroKind::BuiltIn, 1324 MacroDefKind::BuiltIn(_, _) | MacroDefKind::BuiltInEager(_, _) => MacroKind::BuiltIn,
1319 MacroDefKind::BuiltInDerive(_, _) => MacroKind::Derive, 1325 MacroDefKind::BuiltInDerive(_, _) => MacroKind::Derive,
1320 MacroDefKind::BuiltInEager(_, _) => MacroKind::BuiltIn, 1326 MacroDefKind::ProcMacro(_, base_db::ProcMacroKind::CustomDerive, _) => {
1321 // FIXME might be a derive 1327 MacroKind::Derive
1322 MacroDefKind::ProcMacro(_, _) => MacroKind::ProcMacro, 1328 }
1329 MacroDefKind::ProcMacro(_, base_db::ProcMacroKind::Attr, _) => MacroKind::Attr,
1330 MacroDefKind::ProcMacro(_, base_db::ProcMacroKind::FuncLike, _) => MacroKind::ProcMacro,
1323 } 1331 }
1324 } 1332 }
1325} 1333}
diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs
index 4296c6304..d9d6c91a8 100644
--- a/crates/hir_def/src/nameres/collector.rs
+++ b/crates/hir_def/src/nameres/collector.rs
@@ -477,16 +477,21 @@ impl DefCollector<'_> {
477 /// going out of sync with what the build system sees (since we resolve using VFS state, but 477 /// going out of sync with what the build system sees (since we resolve using VFS state, but
478 /// Cargo builds only on-disk files). We could and probably should add diagnostics for that. 478 /// Cargo builds only on-disk files). We could and probably should add diagnostics for that.
479 fn export_proc_macro(&mut self, def: ProcMacroDef, ast_id: AstId<ast::Fn>) { 479 fn export_proc_macro(&mut self, def: ProcMacroDef, ast_id: AstId<ast::Fn>) {
480 let kind = def.kind.to_basedb_kind();
480 self.exports_proc_macros = true; 481 self.exports_proc_macros = true;
481 let macro_def = match self.proc_macros.iter().find(|(n, _)| n == &def.name) { 482 let macro_def = match self.proc_macros.iter().find(|(n, _)| n == &def.name) {
482 Some((_, expander)) => MacroDefId { 483 Some((_, expander)) => MacroDefId {
483 krate: self.def_map.krate, 484 krate: self.def_map.krate,
484 kind: MacroDefKind::ProcMacro(*expander, ast_id), 485 kind: MacroDefKind::ProcMacro(*expander, kind, ast_id),
485 local_inner: false, 486 local_inner: false,
486 }, 487 },
487 None => MacroDefId { 488 None => MacroDefId {
488 krate: self.def_map.krate, 489 krate: self.def_map.krate,
489 kind: MacroDefKind::ProcMacro(ProcMacroExpander::dummy(self.def_map.krate), ast_id), 490 kind: MacroDefKind::ProcMacro(
491 ProcMacroExpander::dummy(self.def_map.krate),
492 kind,
493 ast_id,
494 ),
490 local_inner: false, 495 local_inner: false,
491 }, 496 },
492 }; 497 };
diff --git a/crates/hir_def/src/nameres/proc_macro.rs b/crates/hir_def/src/nameres/proc_macro.rs
index 156598f19..3f095d623 100644
--- a/crates/hir_def/src/nameres/proc_macro.rs
+++ b/crates/hir_def/src/nameres/proc_macro.rs
@@ -18,6 +18,16 @@ pub(super) enum ProcMacroKind {
18 Attr, 18 Attr,
19} 19}
20 20
21impl ProcMacroKind {
22 pub(super) fn to_basedb_kind(&self) -> base_db::ProcMacroKind {
23 match self {
24 ProcMacroKind::CustomDerive { .. } => base_db::ProcMacroKind::CustomDerive,
25 ProcMacroKind::FnLike => base_db::ProcMacroKind::FuncLike,
26 ProcMacroKind::Attr => base_db::ProcMacroKind::Attr,
27 }
28 }
29}
30
21impl Attrs { 31impl Attrs {
22 #[rustfmt::skip] 32 #[rustfmt::skip]
23 pub(super) fn parse_proc_macro_decl(&self, func_name: &Name) -> Option<ProcMacroDef> { 33 pub(super) fn parse_proc_macro_decl(&self, func_name: &Name) -> Option<ProcMacroDef> {
diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs
index 10d37234e..90d8ae240 100644
--- a/crates/hir_expand/src/lib.rs
+++ b/crates/hir_expand/src/lib.rs
@@ -15,6 +15,7 @@ pub mod quote;
15pub mod eager; 15pub mod eager;
16mod input; 16mod input;
17 17
18use base_db::ProcMacroKind;
18use either::Either; 19use either::Either;
19 20
20pub use mbe::{ExpandError, ExpandResult}; 21pub use mbe::{ExpandError, ExpandResult};
@@ -207,7 +208,7 @@ impl MacroDefId {
207 MacroDefKind::BuiltIn(_, id) => id, 208 MacroDefKind::BuiltIn(_, id) => id,
208 MacroDefKind::BuiltInDerive(_, id) => id, 209 MacroDefKind::BuiltInDerive(_, id) => id,
209 MacroDefKind::BuiltInEager(_, id) => id, 210 MacroDefKind::BuiltInEager(_, id) => id,
210 MacroDefKind::ProcMacro(_, id) => return Either::Right(*id), 211 MacroDefKind::ProcMacro(.., id) => return Either::Right(*id),
211 }; 212 };
212 Either::Left(*id) 213 Either::Left(*id)
213 } 214 }
@@ -224,7 +225,7 @@ pub enum MacroDefKind {
224 // FIXME: maybe just Builtin and rename BuiltinFnLikeExpander to BuiltinExpander 225 // FIXME: maybe just Builtin and rename BuiltinFnLikeExpander to BuiltinExpander
225 BuiltInDerive(BuiltinDeriveExpander, AstId<ast::Macro>), 226 BuiltInDerive(BuiltinDeriveExpander, AstId<ast::Macro>),
226 BuiltInEager(EagerExpander, AstId<ast::Macro>), 227 BuiltInEager(EagerExpander, AstId<ast::Macro>),
227 ProcMacro(ProcMacroExpander, AstId<ast::Fn>), 228 ProcMacro(ProcMacroExpander, ProcMacroKind, AstId<ast::Fn>),
228} 229}
229 230
230#[derive(Debug, Clone, PartialEq, Eq, Hash)] 231#[derive(Debug, Clone, PartialEq, Eq, Hash)]