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
|
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use ast;
use codemap::{DUMMY_SP, dummy_spanned};
use ext::base::ExtCtxt;
use ext::expand::{Expansion, ExpansionKind};
use fold::*;
use parse::token::{intern, keywords};
use ptr::P;
use util::move_map::MoveMap;
use util::small_vector::SmallVector;
use std::collections::HashMap;
use std::mem;
pub fn placeholder(kind: ExpansionKind, id: ast::NodeId) -> Expansion {
fn mac_placeholder() -> ast::Mac {
dummy_spanned(ast::Mac_ {
path: ast::Path { span: DUMMY_SP, global: false, segments: Vec::new() },
tts: Vec::new(),
})
}
let ident = keywords::Invalid.ident();
let attrs = Vec::new();
let vis = ast::Visibility::Inherited;
let span = DUMMY_SP;
let expr_placeholder = || P(ast::Expr {
id: id, span: span,
attrs: ast::ThinVec::new(),
node: ast::ExprKind::Mac(mac_placeholder()),
});
match kind {
ExpansionKind::Expr => Expansion::Expr(expr_placeholder()),
ExpansionKind::OptExpr => Expansion::OptExpr(Some(expr_placeholder())),
ExpansionKind::Items => Expansion::Items(SmallVector::one(P(ast::Item {
id: id, span: span, ident: ident, vis: vis, attrs: attrs,
node: ast::ItemKind::Mac(mac_placeholder()),
}))),
ExpansionKind::TraitItems => Expansion::TraitItems(SmallVector::one(ast::TraitItem {
id: id, span: span, ident: ident, attrs: attrs,
node: ast::TraitItemKind::Macro(mac_placeholder()),
})),
ExpansionKind::ImplItems => Expansion::ImplItems(SmallVector::one(ast::ImplItem {
id: id, span: span, ident: ident, vis: vis, attrs: attrs,
node: ast::ImplItemKind::Macro(mac_placeholder()),
defaultness: ast::Defaultness::Final,
})),
ExpansionKind::Pat => Expansion::Pat(P(ast::Pat {
id: id, span: span, node: ast::PatKind::Mac(mac_placeholder()),
})),
ExpansionKind::Ty => Expansion::Ty(P(ast::Ty {
id: id, span: span, node: ast::TyKind::Mac(mac_placeholder()),
})),
ExpansionKind::Stmts => Expansion::Stmts(SmallVector::one({
let mac = P((mac_placeholder(), ast::MacStmtStyle::Braces, ast::ThinVec::new()));
ast::Stmt { id: id, span: span, node: ast::StmtKind::Mac(mac) }
})),
}
}
pub fn macro_scope_placeholder() -> Expansion {
placeholder(ExpansionKind::Items, ast::DUMMY_NODE_ID)
}
pub struct PlaceholderExpander<'a, 'b: 'a> {
expansions: HashMap<ast::NodeId, Expansion>,
cx: &'a mut ExtCtxt<'b>,
monotonic: bool,
}
impl<'a, 'b> PlaceholderExpander<'a, 'b> {
pub fn new(cx: &'a mut ExtCtxt<'b>, monotonic: bool) -> Self {
PlaceholderExpander {
cx: cx,
expansions: HashMap::new(),
monotonic: monotonic,
}
}
pub fn add(&mut self, id: ast::NodeId, expansion: Expansion) {
self.expansions.insert(id, expansion);
}
pub fn remove(&mut self, id: ast::NodeId) -> Expansion {
self.expansions.remove(&id).unwrap()
}
}
impl<'a, 'b> Folder for PlaceholderExpander<'a, 'b> {
fn fold_item(&mut self, item: P<ast::Item>) -> SmallVector<P<ast::Item>> {
match item.node {
// Scope placeholder
ast::ItemKind::Mac(_) if item.id == ast::DUMMY_NODE_ID => SmallVector::one(item),
ast::ItemKind::Mac(_) => self.remove(item.id).make_items(),
_ => noop_fold_item(item, self),
}
}
fn fold_trait_item(&mut self, item: ast::TraitItem) -> SmallVector<ast::TraitItem> {
match item.node {
ast::TraitItemKind::Macro(_) => self.remove(item.id).make_trait_items(),
_ => noop_fold_trait_item(item, self),
}
}
fn fold_impl_item(&mut self, item: ast::ImplItem) -> SmallVector<ast::ImplItem> {
match item.node {
ast::ImplItemKind::Macro(_) => self.remove(item.id).make_impl_items(),
_ => noop_fold_impl_item(item, self),
}
}
fn fold_expr(&mut self, expr: P<ast::Expr>) -> P<ast::Expr> {
match expr.node {
ast::ExprKind::Mac(_) => self.remove(expr.id).make_expr(),
_ => expr.map(|expr| noop_fold_expr(expr, self)),
}
}
fn fold_opt_expr(&mut self, expr: P<ast::Expr>) -> Option<P<ast::Expr>> {
match expr.node {
ast::ExprKind::Mac(_) => self.remove(expr.id).make_opt_expr(),
_ => noop_fold_opt_expr(expr, self),
}
}
fn fold_stmt(&mut self, stmt: ast::Stmt) -> SmallVector<ast::Stmt> {
let (style, mut expansion) = match stmt.node {
ast::StmtKind::Mac(mac) => (mac.1, self.remove(stmt.id).make_stmts()),
_ => return noop_fold_stmt(stmt, self),
};
if style == ast::MacStmtStyle::Semicolon {
if let Some(stmt) = expansion.pop() {
expansion.push(stmt.add_trailing_semicolon());
}
}
expansion
}
fn fold_pat(&mut self, pat: P<ast::Pat>) -> P<ast::Pat> {
match pat.node {
ast::PatKind::Mac(_) => self.remove(pat.id).make_pat(),
_ => noop_fold_pat(pat, self),
}
}
fn fold_ty(&mut self, ty: P<ast::Ty>) -> P<ast::Ty> {
match ty.node {
ast::TyKind::Mac(_) => self.remove(ty.id).make_ty(),
_ => noop_fold_ty(ty, self),
}
}
fn fold_block(&mut self, block: P<ast::Block>) -> P<ast::Block> {
noop_fold_block(block, self).map(|mut block| {
let mut macros = Vec::new();
let mut remaining_stmts = block.stmts.len();
block.stmts = block.stmts.move_flat_map(|mut stmt| {
remaining_stmts -= 1;
// Scope placeholder
if let ast::StmtKind::Item(ref item) = stmt.node {
if let ast::ItemKind::Mac(..) = item.node {
macros.push(item.ident.ctxt.data().outer_mark);
return None;
}
}
match stmt.node {
// Avoid wasting a node id on a trailing expression statement,
// which shares a HIR node with the expression itself.
ast::StmtKind::Expr(ref expr) if remaining_stmts == 0 => stmt.id = expr.id,
_ if self.monotonic => {
assert_eq!(stmt.id, ast::DUMMY_NODE_ID);
stmt.id = self.cx.resolver.next_node_id();
}
_ => {}
}
if self.monotonic && !macros.is_empty() {
let macros = mem::replace(&mut macros, Vec::new());
self.cx.resolver.add_expansions_at_stmt(stmt.id, macros);
}
Some(stmt)
});
block
})
}
fn fold_mod(&mut self, module: ast::Mod) -> ast::Mod {
let mut module = noop_fold_mod(module, self);
module.items = module.items.move_flat_map(|item| match item.node {
ast::ItemKind::Mac(_) => None, // remove scope placeholders from modules
_ => Some(item),
});
module
}
}
pub fn reconstructed_macro_rules(def: &ast::MacroDef) -> Expansion {
Expansion::Items(SmallVector::one(P(ast::Item {
ident: def.ident,
attrs: def.attrs.clone(),
id: ast::DUMMY_NODE_ID,
node: ast::ItemKind::Mac(ast::Mac {
span: def.span,
node: ast::Mac_ {
path: ast::Path {
span: DUMMY_SP,
global: false,
segments: vec![ast::PathSegment {
identifier: ast::Ident::with_empty_ctxt(intern("macro_rules")),
parameters: ast::PathParameters::none(),
}],
},
tts: def.body.clone(),
}
}),
vis: ast::Visibility::Inherited,
span: def.span,
})))
}
|