Skip to content

Commit 5486b70

Browse files
author
Jonas Schievink
committed
Use hir_def to resolve proc macros
1 parent dfa3a3f commit 5486b70

File tree

3 files changed

+79
-16
lines changed

3 files changed

+79
-16
lines changed

crates/hir_def/src/attr.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,19 @@ pub struct AttrQuery<'a> {
171171
}
172172

173173
impl<'a> AttrQuery<'a> {
174+
/// For an attribute like `#[attr(value)]`, returns the `(value)` subtree.
175+
///
176+
/// If the attribute does not have a token tree argument, returns `None`.
174177
pub fn tt_values(self) -> impl Iterator<Item = &'a Subtree> {
175178
self.attrs().filter_map(|attr| match attr.input.as_ref()? {
176179
AttrInput::TokenTree(it) => Some(it),
177180
_ => None,
178181
})
179182
}
180183

184+
/// For an attribute like `#[key = "value"]`, returns `"value"`.
185+
///
186+
/// Returns `None` if the attribute does not have `key = "value"` form.
181187
pub fn string_value(self) -> Option<&'a SmolStr> {
182188
self.attrs().find_map(|attr| match attr.input.as_ref()? {
183189
AttrInput::Literal(it) => Some(it),

crates/hir_def/src/nameres/collector.rs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ use hir_expand::{
1616
proc_macro::ProcMacroExpander,
1717
HirFileId, MacroCallId, MacroDefId, MacroDefKind,
1818
};
19-
use rustc_hash::FxHashMap;
20-
use rustc_hash::FxHashSet;
19+
use rustc_hash::{FxHashMap, FxHashSet};
2120
use syntax::ast;
2221
use test_utils::mark;
22+
use tt::{Leaf, TokenTree};
2323

2424
use crate::{
2525
attr::Attrs,
@@ -281,6 +281,25 @@ impl DefCollector<'_> {
281281
}
282282
}
283283

284+
fn resolve_proc_macro(&mut self, name: &Name) {
285+
let macro_def = match self.proc_macros.iter().find(|(n, _)| n == name) {
286+
Some((_, expander)) => MacroDefId {
287+
ast_id: None,
288+
krate: Some(self.def_map.krate),
289+
kind: MacroDefKind::ProcMacro(*expander),
290+
local_inner: false,
291+
},
292+
None => MacroDefId {
293+
ast_id: None,
294+
krate: Some(self.def_map.krate),
295+
kind: MacroDefKind::ProcMacro(ProcMacroExpander::dummy(self.def_map.krate)),
296+
local_inner: false,
297+
},
298+
};
299+
300+
self.define_proc_macro(name.clone(), macro_def);
301+
}
302+
284303
/// Define a macro with `macro_rules`.
285304
///
286305
/// It will define the macro in legacy textual scope, and if it has `#[macro_export]`,
@@ -917,6 +936,9 @@ impl ModCollector<'_, '_> {
917936
}
918937
ModItem::Function(id) => {
919938
let func = &self.item_tree[id];
939+
940+
self.collect_proc_macro_def(&func.name, attrs);
941+
920942
def = Some(DefData {
921943
id: FunctionLoc {
922944
container: container.into(),
@@ -1177,6 +1199,30 @@ impl ModCollector<'_, '_> {
11771199
}
11781200
}
11791201

1202+
/// If `attrs` registers a procedural macro, collects its definition.
1203+
fn collect_proc_macro_def(&mut self, func_name: &Name, attrs: &Attrs) {
1204+
// FIXME: this should only be done in the root module of `proc-macro` crates, not everywhere
1205+
// FIXME: distinguish the type of macro
1206+
let macro_name = if attrs.by_key("proc_macro").exists()
1207+
|| attrs.by_key("proc_macro_attribute").exists()
1208+
{
1209+
func_name.clone()
1210+
} else {
1211+
let derive = attrs.by_key("proc_macro_derive");
1212+
if let Some(arg) = derive.tt_values().next() {
1213+
if let [TokenTree::Leaf(Leaf::Ident(trait_name))] = &*arg.token_trees {
1214+
trait_name.as_name()
1215+
} else {
1216+
return;
1217+
}
1218+
} else {
1219+
return;
1220+
}
1221+
};
1222+
1223+
self.def_collector.resolve_proc_macro(&macro_name);
1224+
}
1225+
11801226
fn collect_macro(&mut self, mac: &MacroCall) {
11811227
let mut ast_id = AstIdWithPath::new(self.file_id, mac.ast_id, mac.path.clone());
11821228

crates/hir_expand/src/proc_macro.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use tt::buffer::{Cursor, TokenBuffer};
77
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
88
pub struct ProcMacroExpander {
99
krate: CrateId,
10-
proc_macro_id: ProcMacroId,
10+
proc_macro_id: Option<ProcMacroId>,
1111
}
1212

1313
macro_rules! err {
@@ -20,8 +20,14 @@ macro_rules! err {
2020
}
2121

2222
impl ProcMacroExpander {
23-
pub fn new(krate: CrateId, proc_macro_id: ProcMacroId) -> ProcMacroExpander {
24-
ProcMacroExpander { krate, proc_macro_id }
23+
pub fn new(krate: CrateId, proc_macro_id: ProcMacroId) -> Self {
24+
Self { krate, proc_macro_id: Some(proc_macro_id) }
25+
}
26+
27+
pub fn dummy(krate: CrateId) -> Self {
28+
// FIXME: Should store the name for better errors
29+
// FIXME: I think this is the second layer of "dummy" expansion, we should reduce that
30+
Self { krate, proc_macro_id: None }
2531
}
2632

2733
pub fn expand(
@@ -30,17 +36,22 @@ impl ProcMacroExpander {
3036
_id: LazyMacroId,
3137
tt: &tt::Subtree,
3238
) -> Result<tt::Subtree, mbe::ExpandError> {
33-
let krate_graph = db.crate_graph();
34-
let proc_macro = krate_graph[self.krate]
35-
.proc_macro
36-
.get(self.proc_macro_id.0 as usize)
37-
.clone()
38-
.ok_or_else(|| err!("No derive macro found."))?;
39-
40-
let tt = remove_derive_attrs(tt)
41-
.ok_or_else(|| err!("Fail to remove derive for custom derive"))?;
42-
43-
proc_macro.expander.expand(&tt, None).map_err(mbe::ExpandError::from)
39+
match self.proc_macro_id {
40+
Some(id) => {
41+
let krate_graph = db.crate_graph();
42+
let proc_macro = krate_graph[self.krate]
43+
.proc_macro
44+
.get(id.0 as usize)
45+
.clone()
46+
.ok_or_else(|| err!("No derive macro found."))?;
47+
48+
let tt = remove_derive_attrs(tt)
49+
.ok_or_else(|| err!("Fail to remove derive for custom derive"))?;
50+
51+
proc_macro.expander.expand(&tt, None).map_err(mbe::ExpandError::from)
52+
}
53+
None => Err(err!("Unresolved proc macro")),
54+
}
4455
}
4556
}
4657

0 commit comments

Comments
 (0)