1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
#[allow(unused)]
mod tt;
#[allow(unused)]
mod mbe;
/// Machinery for macro expansion.
///
/// One of the more complicated things about macros is managing the source code
/// that is produced after expansion. See `HirFileId` and `MacroCallId` for how
/// do we do that.
///
/// When the file-management question is resolved, all that is left is a
/// token-tree-to-token-tree transformation plus hygiene. We don't have either of
/// those yet, so all macros are string based at the moment!
use std::sync::Arc;
use ra_syntax::{
TextRange, TextUnit, SourceFile, AstNode, SyntaxNode, TreeArc, SyntaxNodePtr,
SyntaxKind::*,
ast::{self, NameOwner},
};
use crate::{HirDatabase, MacroCallId};
// Hard-coded defs for now :-(
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum MacroDef {
CTry,
Vec,
QueryGroup,
}
impl MacroDef {
/// Expands macro call, returning the expansion and offset to be used to
/// convert ranges between expansion and original source.
pub fn ast_expand(macro_call: &ast::MacroCall) -> Option<(TextUnit, MacroExpansion)> {
let (def, input) = MacroDef::from_call(macro_call)?;
let exp = def.expand(input)?;
let off = macro_call.token_tree()?.syntax().range().start();
Some((off, exp))
}
fn from_call(macro_call: &ast::MacroCall) -> Option<(MacroDef, MacroInput)> {
let def = {
let path = macro_call.path()?;
let name_ref = path.segment()?.name_ref()?;
if name_ref.text() == "ctry" {
MacroDef::CTry
} else if name_ref.text() == "vec" {
MacroDef::Vec
} else if name_ref.text() == "query_group" {
MacroDef::QueryGroup
} else {
return None;
}
};
let input = {
let arg = macro_call.token_tree()?.syntax();
MacroInput {
text: arg.text().to_string(),
}
};
Some((def, input))
}
fn expand(self, input: MacroInput) -> Option<MacroExpansion> {
match self {
MacroDef::CTry => self.expand_ctry(input),
MacroDef::Vec => self.expand_vec(input),
MacroDef::QueryGroup => self.expand_query_group(input),
}
}
fn expand_ctry(self, input: MacroInput) -> Option<MacroExpansion> {
let text = format!(
r"
fn dummy() {{
match {} {{
None => return Ok(None),
Some(it) => it,
}}
}}",
input.text
);
let file = SourceFile::parse(&text);
let match_expr = file.syntax().descendants().find_map(ast::MatchExpr::cast)?;
let match_arg = match_expr.expr()?;
let ptr = SyntaxNodePtr::new(match_arg.syntax());
let src_range = TextRange::offset_len(0.into(), TextUnit::of_str(&input.text));
let ranges_map = vec![(src_range, match_arg.syntax().range())];
let res = MacroExpansion {
text,
ranges_map,
ptr,
};
Some(res)
}
fn expand_vec(self, input: MacroInput) -> Option<MacroExpansion> {
let text = format!(r"fn dummy() {{ {}; }}", input.text);
let file = SourceFile::parse(&text);
let array_expr = file.syntax().descendants().find_map(ast::ArrayExpr::cast)?;
let ptr = SyntaxNodePtr::new(array_expr.syntax());
let src_range = TextRange::offset_len(0.into(), TextUnit::of_str(&input.text));
let ranges_map = vec![(src_range, array_expr.syntax().range())];
let res = MacroExpansion {
text,
ranges_map,
ptr,
};
Some(res)
}
fn expand_query_group(self, input: MacroInput) -> Option<MacroExpansion> {
let anchor = "trait ";
let pos = input.text.find(anchor)? + anchor.len();
let trait_name = input.text[pos..]
.chars()
.take_while(|c| c.is_alphabetic())
.collect::<String>();
if trait_name.is_empty() {
return None;
}
let src_range = TextRange::offset_len((pos as u32).into(), TextUnit::of_str(&trait_name));
let text = format!(r"trait {} {{ }}", trait_name);
let file = SourceFile::parse(&text);
let trait_def = file.syntax().descendants().find_map(ast::TraitDef::cast)?;
let name = trait_def.name()?;
let ptr = SyntaxNodePtr::new(trait_def.syntax());
let ranges_map = vec![(src_range, name.syntax().range())];
let res = MacroExpansion {
text,
ranges_map,
ptr,
};
Some(res)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct MacroInput {
// Should be token trees
pub text: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MacroExpansion {
/// The result of macro expansion. Should be token tree as well.
text: String,
/// Correspondence between ranges in the original source code and ranges in
/// the macro.
ranges_map: Vec<(TextRange, TextRange)>,
/// Implementation detail: internally, a macro is expanded to the whole file,
/// even if it is an expression. This `ptr` selects the actual expansion from
/// the expanded file.
ptr: SyntaxNodePtr,
}
impl MacroExpansion {
// FIXME: does not really make sense, macro expansion is not neccessary a
// whole file. See `MacroExpansion::ptr` as well.
pub(crate) fn file(&self) -> TreeArc<SourceFile> {
SourceFile::parse(&self.text)
}
pub fn syntax(&self) -> TreeArc<SyntaxNode> {
self.ptr.to_node(&self.file()).to_owned()
}
/// Maps range in the source code to the range in the expanded code.
pub fn map_range_forward(&self, src_range: TextRange) -> Option<TextRange> {
for (s_range, t_range) in self.ranges_map.iter() {
if src_range.is_subrange(&s_range) {
let src_at_zero_range = src_range - src_range.start();
let src_range_offset = src_range.start() - s_range.start();
let src_range = src_at_zero_range + src_range_offset + t_range.start();
return Some(src_range);
}
}
None
}
/// Maps range in the expanded code to the range in the source code.
pub fn map_range_back(&self, tgt_range: TextRange) -> Option<TextRange> {
for (s_range, t_range) in self.ranges_map.iter() {
if tgt_range.is_subrange(&t_range) {
let tgt_at_zero_range = tgt_range - tgt_range.start();
let tgt_range_offset = tgt_range.start() - t_range.start();
let src_range = tgt_at_zero_range + tgt_range_offset + s_range.start();
return Some(src_range);
}
}
None
}
}
pub(crate) fn expand_macro_invocation(
db: &impl HirDatabase,
invoc: MacroCallId,
) -> Option<Arc<MacroExpansion>> {
let loc = invoc.loc(db);
let syntax = db.file_item(loc.source_item_id);
let macro_call = ast::MacroCall::cast(&syntax).unwrap();
let (def, input) = MacroDef::from_call(macro_call)?;
def.expand(input).map(Arc::new)
}
fn macro_call_to_tt(call: &ast::MacroCall) -> Option<tt::Subtree> {
let tt = call.token_tree()?;
convert_tt(tt.syntax())
}
fn convert_tt(tt: &SyntaxNode) -> Option<tt::Subtree> {
let first_child = tt.first_child()?;
let last_child = tt.last_child()?;
let delimiter = match (first_child.kind(), last_child.kind()) {
(L_PAREN, R_PAREN) => tt::Delimiter::Parenthesis,
(L_CURLY, R_CURLY) => tt::Delimiter::Brace,
(L_BRACK, R_BRACK) => tt::Delimiter::Bracket,
_ => return None,
};
let mut token_trees = Vec::new();
for child in tt.children().skip(1) {
if child == first_child || child == last_child || child.kind().is_trivia() {
continue;
}
if child.kind().is_punct() {
let leaves = child
.leaf_text()
.unwrap()
.chars()
.map(|char| tt::Punct { char })
.map(tt::Leaf::from)
.map(tt::TokenTree::from);
token_trees.extend(leaves);
} else {
let child: tt::TokenTree = if child.kind() == TOKEN_TREE {
convert_tt(child)?.into()
} else if child.kind().is_keyword() || child.kind() == IDENT {
let text = child.leaf_text().unwrap().clone();
tt::Leaf::from(tt::Ident { text }).into()
} else if child.kind().is_literal() {
tt::Leaf::from(tt::Literal {
text: child.leaf_text().unwrap().clone(),
})
.into()
} else {
log::error!("unknown kind: {:?}", child);
return None;
};
token_trees.push(child)
}
}
let res = tt::Subtree {
delimiter,
token_trees,
};
Some(res)
}
#[test]
fn test_convert_tt() {
let text = r#"
macro_rules! impl_froms {
($e:ident: $($v:ident),*) => {
$(
impl From<$v> for $e {
fn from(it: $v) -> $e {
$e::$v(it)
}
}
)*
}
}
"#;
let source_file = ast::SourceFile::parse(text);
let maco_call = source_file
.syntax()
.descendants()
.find_map(ast::MacroCall::cast)
.unwrap();
let tt = macro_call_to_tt(maco_call).unwrap();
let tt = mbe::parse(&tt);
assert!(tt.is_some());
}
|