]>
Commit | Line | Data |
---|---|---|
1a4d82fc | 1 | // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT |
223e47cc LB |
2 | // file at the top-level directory of this distribution and at |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
1a4d82fc JJ |
11 | //! A Folder represents an AST->AST fold; it accepts an AST piece, |
12 | //! and returns a piece of the same type. So, for instance, macro | |
13 | //! expansion is a Folder that walks over an AST and produces another | |
14 | //! AST. | |
15 | //! | |
16 | //! Note: using a Folder (other than the MacroExpander Folder) on | |
17 | //! an AST before macro expansion is probably a bad idea. For instance, | |
18 | //! a folder renaming item names in a module will miss all of those | |
19 | //! that are created by the expansion of a macro. | |
20 | ||
223e47cc LB |
21 | use ast::*; |
22 | use ast; | |
1a4d82fc JJ |
23 | use ast_util; |
24 | use codemap::{respan, Span, Spanned}; | |
25 | use owned_slice::OwnedSlice; | |
970d7e83 | 26 | use parse::token; |
1a4d82fc JJ |
27 | use ptr::P; |
28 | use std::ptr; | |
29 | use util::small_vector::SmallVector; | |
30 | ||
31 | use std::rc::Rc; | |
32 | ||
33 | // This could have a better place to live. | |
34 | pub trait MoveMap<T> { | |
35 | fn move_map<F>(self, f: F) -> Self where F: FnMut(T) -> T; | |
223e47cc | 36 | } |
970d7e83 | 37 | |
1a4d82fc JJ |
38 | impl<T> MoveMap<T> for Vec<T> { |
39 | fn move_map<F>(mut self, mut f: F) -> Vec<T> where F: FnMut(T) -> T { | |
85aaf69f | 40 | for p in &mut self { |
1a4d82fc JJ |
41 | unsafe { |
42 | // FIXME(#5016) this shouldn't need to zero to be safe. | |
c34b1796 | 43 | ptr::write(p, f(ptr::read_and_drop(p))); |
1a4d82fc | 44 | } |
970d7e83 | 45 | } |
1a4d82fc | 46 | self |
970d7e83 LB |
47 | } |
48 | } | |
49 | ||
1a4d82fc JJ |
50 | impl<T> MoveMap<T> for OwnedSlice<T> { |
51 | fn move_map<F>(self, f: F) -> OwnedSlice<T> where F: FnMut(T) -> T { | |
52 | OwnedSlice::from_vec(self.into_vec().move_map(f)) | |
223e47cc LB |
53 | } |
54 | } | |
55 | ||
1a4d82fc JJ |
56 | pub trait Folder : Sized { |
57 | // Any additions to this trait should happen in form | |
58 | // of a call to a public `noop_*` function that only calls | |
59 | // out to the folder again, not other `noop_*` functions. | |
60 | // | |
61 | // This is a necessary API workaround to the problem of not | |
62 | // being able to call out to the super default method | |
63 | // in an overridden default method. | |
64 | ||
65 | fn fold_crate(&mut self, c: Crate) -> Crate { | |
66 | noop_fold_crate(c, self) | |
223e47cc | 67 | } |
223e47cc | 68 | |
1a4d82fc JJ |
69 | fn fold_meta_items(&mut self, meta_items: Vec<P<MetaItem>>) -> Vec<P<MetaItem>> { |
70 | noop_fold_meta_items(meta_items, self) | |
223e47cc | 71 | } |
223e47cc | 72 | |
1a4d82fc JJ |
73 | fn fold_meta_item(&mut self, meta_item: P<MetaItem>) -> P<MetaItem> { |
74 | noop_fold_meta_item(meta_item, self) | |
75 | } | |
76 | ||
77 | fn fold_view_path(&mut self, view_path: P<ViewPath>) -> P<ViewPath> { | |
78 | noop_fold_view_path(view_path, self) | |
79 | } | |
80 | ||
1a4d82fc JJ |
81 | fn fold_foreign_item(&mut self, ni: P<ForeignItem>) -> P<ForeignItem> { |
82 | noop_fold_foreign_item(ni, self) | |
83 | } | |
84 | ||
85 | fn fold_item(&mut self, i: P<Item>) -> SmallVector<P<Item>> { | |
86 | noop_fold_item(i, self) | |
87 | } | |
88 | ||
89 | fn fold_item_simple(&mut self, i: Item) -> Item { | |
90 | noop_fold_item_simple(i, self) | |
91 | } | |
92 | ||
93 | fn fold_struct_field(&mut self, sf: StructField) -> StructField { | |
94 | noop_fold_struct_field(sf, self) | |
95 | } | |
96 | ||
97 | fn fold_item_underscore(&mut self, i: Item_) -> Item_ { | |
98 | noop_fold_item_underscore(i, self) | |
99 | } | |
100 | ||
c34b1796 | 101 | fn fold_trait_item(&mut self, i: P<TraitItem>) -> SmallVector<P<TraitItem>> { |
85aaf69f SL |
102 | noop_fold_trait_item(i, self) |
103 | } | |
104 | ||
c34b1796 | 105 | fn fold_impl_item(&mut self, i: P<ImplItem>) -> SmallVector<P<ImplItem>> { |
85aaf69f SL |
106 | noop_fold_impl_item(i, self) |
107 | } | |
108 | ||
1a4d82fc JJ |
109 | fn fold_fn_decl(&mut self, d: P<FnDecl>) -> P<FnDecl> { |
110 | noop_fold_fn_decl(d, self) | |
111 | } | |
112 | ||
1a4d82fc JJ |
113 | fn fold_block(&mut self, b: P<Block>) -> P<Block> { |
114 | noop_fold_block(b, self) | |
115 | } | |
116 | ||
117 | fn fold_stmt(&mut self, s: P<Stmt>) -> SmallVector<P<Stmt>> { | |
118 | s.and_then(|s| noop_fold_stmt(s, self)) | |
119 | } | |
120 | ||
121 | fn fold_arm(&mut self, a: Arm) -> Arm { | |
122 | noop_fold_arm(a, self) | |
123 | } | |
124 | ||
125 | fn fold_pat(&mut self, p: P<Pat>) -> P<Pat> { | |
126 | noop_fold_pat(p, self) | |
127 | } | |
128 | ||
129 | fn fold_decl(&mut self, d: P<Decl>) -> SmallVector<P<Decl>> { | |
130 | noop_fold_decl(d, self) | |
131 | } | |
132 | ||
133 | fn fold_expr(&mut self, e: P<Expr>) -> P<Expr> { | |
134 | e.map(|e| noop_fold_expr(e, self)) | |
135 | } | |
136 | ||
137 | fn fold_ty(&mut self, t: P<Ty>) -> P<Ty> { | |
138 | noop_fold_ty(t, self) | |
139 | } | |
140 | ||
1a4d82fc JJ |
141 | fn fold_ty_binding(&mut self, t: P<TypeBinding>) -> P<TypeBinding> { |
142 | noop_fold_ty_binding(t, self) | |
143 | } | |
144 | ||
145 | fn fold_mod(&mut self, m: Mod) -> Mod { | |
146 | noop_fold_mod(m, self) | |
147 | } | |
148 | ||
149 | fn fold_foreign_mod(&mut self, nm: ForeignMod) -> ForeignMod { | |
150 | noop_fold_foreign_mod(nm, self) | |
151 | } | |
152 | ||
153 | fn fold_variant(&mut self, v: P<Variant>) -> P<Variant> { | |
154 | noop_fold_variant(v, self) | |
155 | } | |
156 | ||
157 | fn fold_ident(&mut self, i: Ident) -> Ident { | |
158 | noop_fold_ident(i, self) | |
159 | } | |
160 | ||
85aaf69f SL |
161 | fn fold_usize(&mut self, i: usize) -> usize { |
162 | noop_fold_usize(i, self) | |
1a4d82fc JJ |
163 | } |
164 | ||
165 | fn fold_path(&mut self, p: Path) -> Path { | |
166 | noop_fold_path(p, self) | |
167 | } | |
168 | ||
169 | fn fold_path_parameters(&mut self, p: PathParameters) -> PathParameters { | |
170 | noop_fold_path_parameters(p, self) | |
171 | } | |
172 | ||
173 | fn fold_angle_bracketed_parameter_data(&mut self, p: AngleBracketedParameterData) | |
174 | -> AngleBracketedParameterData | |
175 | { | |
176 | noop_fold_angle_bracketed_parameter_data(p, self) | |
177 | } | |
178 | ||
179 | fn fold_parenthesized_parameter_data(&mut self, p: ParenthesizedParameterData) | |
180 | -> ParenthesizedParameterData | |
181 | { | |
182 | noop_fold_parenthesized_parameter_data(p, self) | |
183 | } | |
184 | ||
185 | fn fold_local(&mut self, l: P<Local>) -> P<Local> { | |
186 | noop_fold_local(l, self) | |
187 | } | |
188 | ||
189 | fn fold_mac(&mut self, _mac: Mac) -> Mac { | |
190 | panic!("fold_mac disabled by default"); | |
191 | // NB: see note about macros above. | |
192 | // if you really want a folder that | |
193 | // works on macros, use this | |
194 | // definition in your trait impl: | |
195 | // fold::noop_fold_mac(_mac, self) | |
196 | } | |
197 | ||
198 | fn fold_explicit_self(&mut self, es: ExplicitSelf) -> ExplicitSelf { | |
199 | noop_fold_explicit_self(es, self) | |
200 | } | |
201 | ||
202 | fn fold_explicit_self_underscore(&mut self, es: ExplicitSelf_) -> ExplicitSelf_ { | |
203 | noop_fold_explicit_self_underscore(es, self) | |
204 | } | |
205 | ||
206 | fn fold_lifetime(&mut self, l: Lifetime) -> Lifetime { | |
207 | noop_fold_lifetime(l, self) | |
208 | } | |
209 | ||
210 | fn fold_lifetime_def(&mut self, l: LifetimeDef) -> LifetimeDef { | |
211 | noop_fold_lifetime_def(l, self) | |
212 | } | |
213 | ||
85aaf69f | 214 | fn fold_attribute(&mut self, at: Attribute) -> Option<Attribute> { |
1a4d82fc JJ |
215 | noop_fold_attribute(at, self) |
216 | } | |
217 | ||
218 | fn fold_arg(&mut self, a: Arg) -> Arg { | |
219 | noop_fold_arg(a, self) | |
220 | } | |
221 | ||
222 | fn fold_generics(&mut self, generics: Generics) -> Generics { | |
223 | noop_fold_generics(generics, self) | |
224 | } | |
225 | ||
226 | fn fold_trait_ref(&mut self, p: TraitRef) -> TraitRef { | |
227 | noop_fold_trait_ref(p, self) | |
228 | } | |
229 | ||
230 | fn fold_poly_trait_ref(&mut self, p: PolyTraitRef) -> PolyTraitRef { | |
231 | noop_fold_poly_trait_ref(p, self) | |
232 | } | |
233 | ||
234 | fn fold_struct_def(&mut self, struct_def: P<StructDef>) -> P<StructDef> { | |
235 | noop_fold_struct_def(struct_def, self) | |
236 | } | |
237 | ||
238 | fn fold_lifetimes(&mut self, lts: Vec<Lifetime>) -> Vec<Lifetime> { | |
239 | noop_fold_lifetimes(lts, self) | |
240 | } | |
241 | ||
242 | fn fold_lifetime_defs(&mut self, lts: Vec<LifetimeDef>) -> Vec<LifetimeDef> { | |
243 | noop_fold_lifetime_defs(lts, self) | |
244 | } | |
245 | ||
246 | fn fold_ty_param(&mut self, tp: TyParam) -> TyParam { | |
247 | noop_fold_ty_param(tp, self) | |
248 | } | |
249 | ||
250 | fn fold_ty_params(&mut self, tps: OwnedSlice<TyParam>) -> OwnedSlice<TyParam> { | |
251 | noop_fold_ty_params(tps, self) | |
252 | } | |
253 | ||
254 | fn fold_tt(&mut self, tt: &TokenTree) -> TokenTree { | |
255 | noop_fold_tt(tt, self) | |
256 | } | |
257 | ||
258 | fn fold_tts(&mut self, tts: &[TokenTree]) -> Vec<TokenTree> { | |
259 | noop_fold_tts(tts, self) | |
260 | } | |
261 | ||
262 | fn fold_token(&mut self, t: token::Token) -> token::Token { | |
263 | noop_fold_token(t, self) | |
264 | } | |
265 | ||
266 | fn fold_interpolated(&mut self, nt: token::Nonterminal) -> token::Nonterminal { | |
267 | noop_fold_interpolated(nt, self) | |
268 | } | |
269 | ||
270 | fn fold_opt_lifetime(&mut self, o_lt: Option<Lifetime>) -> Option<Lifetime> { | |
271 | noop_fold_opt_lifetime(o_lt, self) | |
272 | } | |
273 | ||
274 | fn fold_variant_arg(&mut self, va: VariantArg) -> VariantArg { | |
275 | noop_fold_variant_arg(va, self) | |
276 | } | |
277 | ||
278 | fn fold_opt_bounds(&mut self, b: Option<OwnedSlice<TyParamBound>>) | |
279 | -> Option<OwnedSlice<TyParamBound>> { | |
280 | noop_fold_opt_bounds(b, self) | |
281 | } | |
282 | ||
283 | fn fold_bounds(&mut self, b: OwnedSlice<TyParamBound>) | |
284 | -> OwnedSlice<TyParamBound> { | |
285 | noop_fold_bounds(b, self) | |
286 | } | |
287 | ||
288 | fn fold_ty_param_bound(&mut self, tpb: TyParamBound) -> TyParamBound { | |
289 | noop_fold_ty_param_bound(tpb, self) | |
290 | } | |
291 | ||
292 | fn fold_mt(&mut self, mt: MutTy) -> MutTy { | |
293 | noop_fold_mt(mt, self) | |
294 | } | |
223e47cc | 295 | |
1a4d82fc JJ |
296 | fn fold_field(&mut self, field: Field) -> Field { |
297 | noop_fold_field(field, self) | |
298 | } | |
299 | ||
300 | fn fold_where_clause(&mut self, where_clause: WhereClause) | |
301 | -> WhereClause { | |
302 | noop_fold_where_clause(where_clause, self) | |
303 | } | |
304 | ||
305 | fn fold_where_predicate(&mut self, where_predicate: WherePredicate) | |
306 | -> WherePredicate { | |
307 | noop_fold_where_predicate(where_predicate, self) | |
308 | } | |
309 | ||
1a4d82fc JJ |
310 | fn new_id(&mut self, i: NodeId) -> NodeId { |
311 | i | |
312 | } | |
313 | ||
314 | fn new_span(&mut self, sp: Span) -> Span { | |
315 | sp | |
316 | } | |
223e47cc LB |
317 | } |
318 | ||
1a4d82fc JJ |
319 | pub fn noop_fold_meta_items<T: Folder>(meta_items: Vec<P<MetaItem>>, fld: &mut T) |
320 | -> Vec<P<MetaItem>> { | |
321 | meta_items.move_map(|x| fld.fold_meta_item(x)) | |
223e47cc LB |
322 | } |
323 | ||
1a4d82fc JJ |
324 | pub fn noop_fold_view_path<T: Folder>(view_path: P<ViewPath>, fld: &mut T) -> P<ViewPath> { |
325 | view_path.map(|Spanned {node, span}| Spanned { | |
326 | node: match node { | |
85aaf69f SL |
327 | ViewPathSimple(ident, path) => { |
328 | ViewPathSimple(ident, fld.fold_path(path)) | |
1a4d82fc | 329 | } |
85aaf69f SL |
330 | ViewPathGlob(path) => { |
331 | ViewPathGlob(fld.fold_path(path)) | |
1a4d82fc | 332 | } |
85aaf69f | 333 | ViewPathList(path, path_list_idents) => { |
1a4d82fc JJ |
334 | ViewPathList(fld.fold_path(path), |
335 | path_list_idents.move_map(|path_list_ident| { | |
336 | Spanned { | |
337 | node: match path_list_ident.node { | |
338 | PathListIdent { id, name } => | |
339 | PathListIdent { | |
340 | id: fld.new_id(id), | |
341 | name: name | |
342 | }, | |
343 | PathListMod { id } => | |
344 | PathListMod { id: fld.new_id(id) } | |
345 | }, | |
346 | span: fld.new_span(path_list_ident.span) | |
347 | } | |
85aaf69f | 348 | })) |
1a4d82fc JJ |
349 | } |
350 | }, | |
351 | span: fld.new_span(span) | |
352 | }) | |
223e47cc LB |
353 | } |
354 | ||
85aaf69f | 355 | pub fn fold_attrs<T: Folder>(attrs: Vec<Attribute>, fld: &mut T) -> Vec<Attribute> { |
62682a34 | 356 | attrs.into_iter().flat_map(|x| fld.fold_attribute(x)).collect() |
85aaf69f SL |
357 | } |
358 | ||
1a4d82fc JJ |
359 | pub fn noop_fold_arm<T: Folder>(Arm {attrs, pats, guard, body}: Arm, fld: &mut T) -> Arm { |
360 | Arm { | |
85aaf69f | 361 | attrs: fold_attrs(attrs, fld), |
1a4d82fc JJ |
362 | pats: pats.move_map(|x| fld.fold_pat(x)), |
363 | guard: guard.map(|x| fld.fold_expr(x)), | |
364 | body: fld.fold_expr(body), | |
365 | } | |
223e47cc LB |
366 | } |
367 | ||
1a4d82fc JJ |
368 | pub fn noop_fold_decl<T: Folder>(d: P<Decl>, fld: &mut T) -> SmallVector<P<Decl>> { |
369 | d.and_then(|Spanned {node, span}| match node { | |
370 | DeclLocal(l) => SmallVector::one(P(Spanned { | |
371 | node: DeclLocal(fld.fold_local(l)), | |
372 | span: fld.new_span(span) | |
373 | })), | |
374 | DeclItem(it) => fld.fold_item(it).into_iter().map(|i| P(Spanned { | |
375 | node: DeclItem(i), | |
376 | span: fld.new_span(span) | |
377 | })).collect() | |
378 | }) | |
379 | } | |
223e47cc | 380 | |
1a4d82fc JJ |
381 | pub fn noop_fold_ty_binding<T: Folder>(b: P<TypeBinding>, fld: &mut T) -> P<TypeBinding> { |
382 | b.map(|TypeBinding { id, ident, ty, span }| TypeBinding { | |
383 | id: fld.new_id(id), | |
384 | ident: ident, | |
385 | ty: fld.fold_ty(ty), | |
386 | span: fld.new_span(span), | |
387 | }) | |
223e47cc LB |
388 | } |
389 | ||
1a4d82fc JJ |
390 | pub fn noop_fold_ty<T: Folder>(t: P<Ty>, fld: &mut T) -> P<Ty> { |
391 | t.map(|Ty {id, node, span}| Ty { | |
392 | id: fld.new_id(id), | |
393 | node: match node { | |
394 | TyInfer => node, | |
395 | TyVec(ty) => TyVec(fld.fold_ty(ty)), | |
396 | TyPtr(mt) => TyPtr(fld.fold_mt(mt)), | |
397 | TyRptr(region, mt) => { | |
398 | TyRptr(fld.fold_opt_lifetime(region), fld.fold_mt(mt)) | |
399 | } | |
400 | TyBareFn(f) => { | |
401 | TyBareFn(f.map(|BareFnTy {lifetimes, unsafety, abi, decl}| BareFnTy { | |
402 | lifetimes: fld.fold_lifetime_defs(lifetimes), | |
403 | unsafety: unsafety, | |
404 | abi: abi, | |
405 | decl: fld.fold_fn_decl(decl) | |
406 | })) | |
407 | } | |
408 | TyTup(tys) => TyTup(tys.move_map(|ty| fld.fold_ty(ty))), | |
409 | TyParen(ty) => TyParen(fld.fold_ty(ty)), | |
c34b1796 AL |
410 | TyPath(qself, path) => { |
411 | let qself = qself.map(|QSelf { ty, position }| { | |
412 | QSelf { | |
413 | ty: fld.fold_ty(ty), | |
414 | position: position | |
415 | } | |
416 | }); | |
417 | TyPath(qself, fld.fold_path(path)) | |
1a4d82fc JJ |
418 | } |
419 | TyObjectSum(ty, bounds) => { | |
420 | TyObjectSum(fld.fold_ty(ty), | |
421 | fld.fold_bounds(bounds)) | |
422 | } | |
1a4d82fc JJ |
423 | TyFixedLengthVec(ty, e) => { |
424 | TyFixedLengthVec(fld.fold_ty(ty), fld.fold_expr(e)) | |
425 | } | |
426 | TyTypeof(expr) => { | |
427 | TyTypeof(fld.fold_expr(expr)) | |
428 | } | |
429 | TyPolyTraitRef(bounds) => { | |
430 | TyPolyTraitRef(bounds.move_map(|b| fld.fold_ty_param_bound(b))) | |
431 | } | |
432 | }, | |
433 | span: fld.new_span(span) | |
434 | }) | |
223e47cc LB |
435 | } |
436 | ||
85aaf69f | 437 | pub fn noop_fold_foreign_mod<T: Folder>(ForeignMod {abi, items}: ForeignMod, |
1a4d82fc JJ |
438 | fld: &mut T) -> ForeignMod { |
439 | ForeignMod { | |
440 | abi: abi, | |
1a4d82fc JJ |
441 | items: items.move_map(|x| fld.fold_foreign_item(x)), |
442 | } | |
443 | } | |
223e47cc | 444 | |
1a4d82fc JJ |
445 | pub fn noop_fold_variant<T: Folder>(v: P<Variant>, fld: &mut T) -> P<Variant> { |
446 | v.map(|Spanned {node: Variant_ {id, name, attrs, kind, disr_expr, vis}, span}| Spanned { | |
447 | node: Variant_ { | |
448 | id: fld.new_id(id), | |
449 | name: name, | |
85aaf69f | 450 | attrs: fold_attrs(attrs, fld), |
1a4d82fc JJ |
451 | kind: match kind { |
452 | TupleVariantKind(variant_args) => { | |
453 | TupleVariantKind(variant_args.move_map(|x| | |
454 | fld.fold_variant_arg(x))) | |
223e47cc | 455 | } |
1a4d82fc JJ |
456 | StructVariantKind(struct_def) => { |
457 | StructVariantKind(fld.fold_struct_def(struct_def)) | |
223e47cc LB |
458 | } |
459 | }, | |
1a4d82fc JJ |
460 | disr_expr: disr_expr.map(|e| fld.fold_expr(e)), |
461 | vis: vis, | |
462 | }, | |
463 | span: fld.new_span(span), | |
464 | }) | |
465 | } | |
466 | ||
467 | pub fn noop_fold_ident<T: Folder>(i: Ident, _: &mut T) -> Ident { | |
468 | i | |
469 | } | |
470 | ||
85aaf69f | 471 | pub fn noop_fold_usize<T: Folder>(i: usize, _: &mut T) -> usize { |
1a4d82fc JJ |
472 | i |
473 | } | |
474 | ||
475 | pub fn noop_fold_path<T: Folder>(Path {global, segments, span}: Path, fld: &mut T) -> Path { | |
476 | Path { | |
477 | global: global, | |
478 | segments: segments.move_map(|PathSegment {identifier, parameters}| PathSegment { | |
479 | identifier: fld.fold_ident(identifier), | |
480 | parameters: fld.fold_path_parameters(parameters), | |
481 | }), | |
482 | span: fld.new_span(span) | |
223e47cc LB |
483 | } |
484 | } | |
485 | ||
1a4d82fc JJ |
486 | pub fn noop_fold_path_parameters<T: Folder>(path_parameters: PathParameters, fld: &mut T) |
487 | -> PathParameters | |
488 | { | |
489 | match path_parameters { | |
490 | AngleBracketedParameters(data) => | |
491 | AngleBracketedParameters(fld.fold_angle_bracketed_parameter_data(data)), | |
492 | ParenthesizedParameters(data) => | |
493 | ParenthesizedParameters(fld.fold_parenthesized_parameter_data(data)), | |
494 | } | |
495 | } | |
223e47cc | 496 | |
1a4d82fc JJ |
497 | pub fn noop_fold_angle_bracketed_parameter_data<T: Folder>(data: AngleBracketedParameterData, |
498 | fld: &mut T) | |
499 | -> AngleBracketedParameterData | |
500 | { | |
501 | let AngleBracketedParameterData { lifetimes, types, bindings } = data; | |
502 | AngleBracketedParameterData { lifetimes: fld.fold_lifetimes(lifetimes), | |
503 | types: types.move_map(|ty| fld.fold_ty(ty)), | |
504 | bindings: bindings.move_map(|b| fld.fold_ty_binding(b)) } | |
223e47cc LB |
505 | } |
506 | ||
1a4d82fc JJ |
507 | pub fn noop_fold_parenthesized_parameter_data<T: Folder>(data: ParenthesizedParameterData, |
508 | fld: &mut T) | |
509 | -> ParenthesizedParameterData | |
510 | { | |
85aaf69f | 511 | let ParenthesizedParameterData { inputs, output, span } = data; |
1a4d82fc | 512 | ParenthesizedParameterData { inputs: inputs.move_map(|ty| fld.fold_ty(ty)), |
85aaf69f SL |
513 | output: output.map(|ty| fld.fold_ty(ty)), |
514 | span: fld.new_span(span) } | |
1a4d82fc | 515 | } |
970d7e83 | 516 | |
1a4d82fc | 517 | pub fn noop_fold_local<T: Folder>(l: P<Local>, fld: &mut T) -> P<Local> { |
c1a9b12d | 518 | l.map(|Local {id, pat, ty, init, span}| Local { |
1a4d82fc JJ |
519 | id: fld.new_id(id), |
520 | ty: ty.map(|t| fld.fold_ty(t)), | |
521 | pat: fld.fold_pat(pat), | |
522 | init: init.map(|e| fld.fold_expr(e)), | |
1a4d82fc JJ |
523 | span: fld.new_span(span) |
524 | }) | |
223e47cc LB |
525 | } |
526 | ||
85aaf69f | 527 | pub fn noop_fold_attribute<T: Folder>(at: Attribute, fld: &mut T) -> Option<Attribute> { |
1a4d82fc | 528 | let Spanned {node: Attribute_ {id, style, value, is_sugared_doc}, span} = at; |
85aaf69f | 529 | Some(Spanned { |
1a4d82fc JJ |
530 | node: Attribute_ { |
531 | id: id, | |
532 | style: style, | |
533 | value: fld.fold_meta_item(value), | |
534 | is_sugared_doc: is_sugared_doc | |
535 | }, | |
536 | span: fld.new_span(span) | |
85aaf69f | 537 | }) |
1a4d82fc JJ |
538 | } |
539 | ||
540 | pub fn noop_fold_explicit_self_underscore<T: Folder>(es: ExplicitSelf_, fld: &mut T) | |
541 | -> ExplicitSelf_ { | |
542 | match es { | |
543 | SelfStatic | SelfValue(_) => es, | |
544 | SelfRegion(lifetime, m, ident) => { | |
545 | SelfRegion(fld.fold_opt_lifetime(lifetime), m, ident) | |
223e47cc | 546 | } |
1a4d82fc JJ |
547 | SelfExplicit(typ, ident) => { |
548 | SelfExplicit(fld.fold_ty(typ), ident) | |
223e47cc LB |
549 | } |
550 | } | |
551 | } | |
552 | ||
1a4d82fc JJ |
553 | pub fn noop_fold_explicit_self<T: Folder>(Spanned {span, node}: ExplicitSelf, fld: &mut T) |
554 | -> ExplicitSelf { | |
555 | Spanned { | |
556 | node: fld.fold_explicit_self_underscore(node), | |
557 | span: fld.new_span(span) | |
223e47cc LB |
558 | } |
559 | } | |
560 | ||
1a4d82fc JJ |
561 | |
562 | pub fn noop_fold_mac<T: Folder>(Spanned {node, span}: Mac, fld: &mut T) -> Mac { | |
563 | Spanned { | |
564 | node: match node { | |
565 | MacInvocTT(p, tts, ctxt) => { | |
85aaf69f | 566 | MacInvocTT(fld.fold_path(p), fld.fold_tts(&tts), ctxt) |
1a4d82fc JJ |
567 | } |
568 | }, | |
569 | span: fld.new_span(span) | |
223e47cc LB |
570 | } |
571 | } | |
572 | ||
1a4d82fc JJ |
573 | pub fn noop_fold_meta_item<T: Folder>(mi: P<MetaItem>, fld: &mut T) -> P<MetaItem> { |
574 | mi.map(|Spanned {node, span}| Spanned { | |
575 | node: match node { | |
576 | MetaWord(id) => MetaWord(id), | |
577 | MetaList(id, mis) => { | |
578 | MetaList(id, mis.move_map(|e| fld.fold_meta_item(e))) | |
579 | } | |
580 | MetaNameValue(id, s) => MetaNameValue(id, s) | |
223e47cc | 581 | }, |
1a4d82fc JJ |
582 | span: fld.new_span(span) |
583 | }) | |
223e47cc LB |
584 | } |
585 | ||
1a4d82fc JJ |
586 | pub fn noop_fold_arg<T: Folder>(Arg {id, pat, ty}: Arg, fld: &mut T) -> Arg { |
587 | Arg { | |
588 | id: fld.new_id(id), | |
589 | pat: fld.fold_pat(pat), | |
590 | ty: fld.fold_ty(ty) | |
223e47cc LB |
591 | } |
592 | } | |
593 | ||
1a4d82fc JJ |
594 | pub fn noop_fold_tt<T: Folder>(tt: &TokenTree, fld: &mut T) -> TokenTree { |
595 | match *tt { | |
596 | TtToken(span, ref tok) => | |
597 | TtToken(span, fld.fold_token(tok.clone())), | |
598 | TtDelimited(span, ref delimed) => { | |
599 | TtDelimited(span, Rc::new( | |
600 | Delimited { | |
601 | delim: delimed.delim, | |
602 | open_span: delimed.open_span, | |
85aaf69f | 603 | tts: fld.fold_tts(&delimed.tts), |
1a4d82fc JJ |
604 | close_span: delimed.close_span, |
605 | } | |
606 | )) | |
607 | }, | |
608 | TtSequence(span, ref seq) => | |
609 | TtSequence(span, | |
610 | Rc::new(SequenceRepetition { | |
85aaf69f | 611 | tts: fld.fold_tts(&seq.tts), |
1a4d82fc JJ |
612 | separator: seq.separator.clone().map(|tok| fld.fold_token(tok)), |
613 | ..**seq | |
614 | })), | |
223e47cc LB |
615 | } |
616 | } | |
617 | ||
1a4d82fc JJ |
618 | pub fn noop_fold_tts<T: Folder>(tts: &[TokenTree], fld: &mut T) -> Vec<TokenTree> { |
619 | tts.iter().map(|tt| fld.fold_tt(tt)).collect() | |
620 | } | |
621 | ||
622 | // apply ident folder if it's an ident, apply other folds to interpolated nodes | |
623 | pub fn noop_fold_token<T: Folder>(t: token::Token, fld: &mut T) -> token::Token { | |
624 | match t { | |
625 | token::Ident(id, followed_by_colons) => { | |
626 | token::Ident(fld.fold_ident(id), followed_by_colons) | |
970d7e83 | 627 | } |
1a4d82fc JJ |
628 | token::Lifetime(id) => token::Lifetime(fld.fold_ident(id)), |
629 | token::Interpolated(nt) => token::Interpolated(fld.fold_interpolated(nt)), | |
630 | token::SubstNt(ident, namep) => { | |
631 | token::SubstNt(fld.fold_ident(ident), namep) | |
970d7e83 | 632 | } |
1a4d82fc JJ |
633 | token::MatchNt(name, kind, namep, kindp) => { |
634 | token::MatchNt(fld.fold_ident(name), fld.fold_ident(kind), namep, kindp) | |
970d7e83 | 635 | } |
1a4d82fc | 636 | _ => t |
223e47cc LB |
637 | } |
638 | } | |
639 | ||
1a4d82fc JJ |
640 | /// apply folder to elements of interpolated nodes |
641 | // | |
642 | // NB: this can occur only when applying a fold to partially expanded code, where | |
643 | // parsed pieces have gotten implanted ito *other* macro invocations. This is relevant | |
644 | // for macro hygiene, but possibly not elsewhere. | |
645 | // | |
646 | // One problem here occurs because the types for fold_item, fold_stmt, etc. allow the | |
647 | // folder to return *multiple* items; this is a problem for the nodes here, because | |
648 | // they insist on having exactly one piece. One solution would be to mangle the fold | |
649 | // trait to include one-to-many and one-to-one versions of these entry points, but that | |
650 | // would probably confuse a lot of people and help very few. Instead, I'm just going | |
651 | // to put in dynamic checks. I think the performance impact of this will be pretty much | |
652 | // nonexistent. The danger is that someone will apply a fold to a partially expanded | |
653 | // node, and will be confused by the fact that their "fold_item" or "fold_stmt" isn't | |
654 | // getting called on NtItem or NtStmt nodes. Hopefully they'll wind up reading this | |
655 | // comment, and doing something appropriate. | |
656 | // | |
657 | // BTW, design choice: I considered just changing the type of, e.g., NtItem to contain | |
658 | // multiple items, but decided against it when I looked at parse_item_or_view_item and | |
659 | // tried to figure out what I would do with multiple items there.... | |
660 | pub fn noop_fold_interpolated<T: Folder>(nt: token::Nonterminal, fld: &mut T) | |
661 | -> token::Nonterminal { | |
662 | match nt { | |
663 | token::NtItem(item) => | |
664 | token::NtItem(fld.fold_item(item) | |
665 | // this is probably okay, because the only folds likely | |
666 | // to peek inside interpolated nodes will be renamings/markings, | |
667 | // which map single items to single items | |
668 | .expect_one("expected fold to produce exactly one item")), | |
669 | token::NtBlock(block) => token::NtBlock(fld.fold_block(block)), | |
670 | token::NtStmt(stmt) => | |
671 | token::NtStmt(fld.fold_stmt(stmt) | |
672 | // this is probably okay, because the only folds likely | |
673 | // to peek inside interpolated nodes will be renamings/markings, | |
674 | // which map single items to single items | |
675 | .expect_one("expected fold to produce exactly one statement")), | |
676 | token::NtPat(pat) => token::NtPat(fld.fold_pat(pat)), | |
677 | token::NtExpr(expr) => token::NtExpr(fld.fold_expr(expr)), | |
678 | token::NtTy(ty) => token::NtTy(fld.fold_ty(ty)), | |
d9579d0f AL |
679 | token::NtIdent(id, is_mod_name) => |
680 | token::NtIdent(Box::new(fld.fold_ident(*id)), is_mod_name), | |
1a4d82fc | 681 | token::NtMeta(meta_item) => token::NtMeta(fld.fold_meta_item(meta_item)), |
d9579d0f | 682 | token::NtPath(path) => token::NtPath(Box::new(fld.fold_path(*path))), |
1a4d82fc | 683 | token::NtTT(tt) => token::NtTT(P(fld.fold_tt(&*tt))), |
d9579d0f AL |
684 | token::NtArm(arm) => token::NtArm(fld.fold_arm(arm)), |
685 | token::NtImplItem(arm) => | |
686 | token::NtImplItem(fld.fold_impl_item(arm) | |
687 | .expect_one("expected fold to produce exactly one item")), | |
688 | token::NtTraitItem(arm) => | |
689 | token::NtTraitItem(fld.fold_trait_item(arm) | |
690 | .expect_one("expected fold to produce exactly one item")), | |
691 | token::NtGenerics(generics) => token::NtGenerics(fld.fold_generics(generics)), | |
692 | token::NtWhereClause(where_clause) => | |
693 | token::NtWhereClause(fld.fold_where_clause(where_clause)), | |
223e47cc LB |
694 | } |
695 | } | |
696 | ||
1a4d82fc JJ |
697 | pub fn noop_fold_fn_decl<T: Folder>(decl: P<FnDecl>, fld: &mut T) -> P<FnDecl> { |
698 | decl.map(|FnDecl {inputs, output, variadic}| FnDecl { | |
699 | inputs: inputs.move_map(|x| fld.fold_arg(x)), | |
700 | output: match output { | |
701 | Return(ty) => Return(fld.fold_ty(ty)), | |
85aaf69f | 702 | DefaultReturn(span) => DefaultReturn(span), |
1a4d82fc | 703 | NoReturn(span) => NoReturn(span) |
223e47cc | 704 | }, |
1a4d82fc JJ |
705 | variadic: variadic |
706 | }) | |
707 | } | |
708 | ||
709 | pub fn noop_fold_ty_param_bound<T>(tpb: TyParamBound, fld: &mut T) | |
710 | -> TyParamBound | |
711 | where T: Folder { | |
712 | match tpb { | |
713 | TraitTyParamBound(ty, modifier) => TraitTyParamBound(fld.fold_poly_trait_ref(ty), modifier), | |
714 | RegionTyParamBound(lifetime) => RegionTyParamBound(fld.fold_lifetime(lifetime)), | |
223e47cc LB |
715 | } |
716 | } | |
717 | ||
1a4d82fc JJ |
718 | pub fn noop_fold_ty_param<T: Folder>(tp: TyParam, fld: &mut T) -> TyParam { |
719 | let TyParam {id, ident, bounds, default, span} = tp; | |
720 | TyParam { | |
721 | id: fld.new_id(id), | |
722 | ident: ident, | |
723 | bounds: fld.fold_bounds(bounds), | |
724 | default: default.map(|x| fld.fold_ty(x)), | |
725 | span: span | |
223e47cc LB |
726 | } |
727 | } | |
728 | ||
1a4d82fc JJ |
729 | pub fn noop_fold_ty_params<T: Folder>(tps: OwnedSlice<TyParam>, fld: &mut T) |
730 | -> OwnedSlice<TyParam> { | |
731 | tps.move_map(|tp| fld.fold_ty_param(tp)) | |
223e47cc LB |
732 | } |
733 | ||
1a4d82fc JJ |
734 | pub fn noop_fold_lifetime<T: Folder>(l: Lifetime, fld: &mut T) -> Lifetime { |
735 | Lifetime { | |
736 | id: fld.new_id(l.id), | |
737 | name: l.name, | |
738 | span: fld.new_span(l.span) | |
223e47cc | 739 | } |
1a4d82fc | 740 | } |
223e47cc | 741 | |
1a4d82fc JJ |
742 | pub fn noop_fold_lifetime_def<T: Folder>(l: LifetimeDef, fld: &mut T) |
743 | -> LifetimeDef { | |
744 | LifetimeDef { | |
745 | lifetime: fld.fold_lifetime(l.lifetime), | |
746 | bounds: fld.fold_lifetimes(l.bounds), | |
223e47cc LB |
747 | } |
748 | } | |
749 | ||
1a4d82fc JJ |
750 | pub fn noop_fold_lifetimes<T: Folder>(lts: Vec<Lifetime>, fld: &mut T) -> Vec<Lifetime> { |
751 | lts.move_map(|l| fld.fold_lifetime(l)) | |
752 | } | |
753 | ||
754 | pub fn noop_fold_lifetime_defs<T: Folder>(lts: Vec<LifetimeDef>, fld: &mut T) | |
755 | -> Vec<LifetimeDef> { | |
756 | lts.move_map(|l| fld.fold_lifetime_def(l)) | |
757 | } | |
758 | ||
759 | pub fn noop_fold_opt_lifetime<T: Folder>(o_lt: Option<Lifetime>, fld: &mut T) | |
760 | -> Option<Lifetime> { | |
761 | o_lt.map(|lt| fld.fold_lifetime(lt)) | |
762 | } | |
763 | ||
764 | pub fn noop_fold_generics<T: Folder>(Generics {ty_params, lifetimes, where_clause}: Generics, | |
765 | fld: &mut T) -> Generics { | |
766 | Generics { | |
767 | ty_params: fld.fold_ty_params(ty_params), | |
768 | lifetimes: fld.fold_lifetime_defs(lifetimes), | |
769 | where_clause: fld.fold_where_clause(where_clause), | |
223e47cc | 770 | } |
1a4d82fc JJ |
771 | } |
772 | ||
773 | pub fn noop_fold_where_clause<T: Folder>( | |
774 | WhereClause {id, predicates}: WhereClause, | |
775 | fld: &mut T) | |
776 | -> WhereClause { | |
777 | WhereClause { | |
778 | id: fld.new_id(id), | |
779 | predicates: predicates.move_map(|predicate| { | |
780 | fld.fold_where_predicate(predicate) | |
781 | }) | |
970d7e83 | 782 | } |
1a4d82fc JJ |
783 | } |
784 | ||
785 | pub fn noop_fold_where_predicate<T: Folder>( | |
786 | pred: WherePredicate, | |
787 | fld: &mut T) | |
788 | -> WherePredicate { | |
789 | match pred { | |
85aaf69f SL |
790 | ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate{bound_lifetimes, |
791 | bounded_ty, | |
1a4d82fc JJ |
792 | bounds, |
793 | span}) => { | |
794 | ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate { | |
85aaf69f | 795 | bound_lifetimes: fld.fold_lifetime_defs(bound_lifetimes), |
1a4d82fc JJ |
796 | bounded_ty: fld.fold_ty(bounded_ty), |
797 | bounds: bounds.move_map(|x| fld.fold_ty_param_bound(x)), | |
798 | span: fld.new_span(span) | |
223e47cc LB |
799 | }) |
800 | } | |
1a4d82fc JJ |
801 | ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate{lifetime, |
802 | bounds, | |
803 | span}) => { | |
804 | ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate { | |
805 | span: fld.new_span(span), | |
806 | lifetime: fld.fold_lifetime(lifetime), | |
807 | bounds: bounds.move_map(|bound| fld.fold_lifetime(bound)) | |
223e47cc LB |
808 | }) |
809 | } | |
1a4d82fc JJ |
810 | ast::WherePredicate::EqPredicate(ast::WhereEqPredicate{id, |
811 | path, | |
812 | ty, | |
813 | span}) => { | |
814 | ast::WherePredicate::EqPredicate(ast::WhereEqPredicate{ | |
815 | id: fld.new_id(id), | |
816 | path: fld.fold_path(path), | |
817 | ty:fld.fold_ty(ty), | |
818 | span: fld.new_span(span) | |
819 | }) | |
223e47cc | 820 | } |
223e47cc LB |
821 | } |
822 | } | |
823 | ||
1a4d82fc JJ |
824 | pub fn noop_fold_struct_def<T: Folder>(struct_def: P<StructDef>, fld: &mut T) -> P<StructDef> { |
825 | struct_def.map(|StructDef { fields, ctor_id }| StructDef { | |
826 | fields: fields.move_map(|f| fld.fold_struct_field(f)), | |
827 | ctor_id: ctor_id.map(|cid| fld.new_id(cid)), | |
828 | }) | |
829 | } | |
830 | ||
831 | pub fn noop_fold_trait_ref<T: Folder>(p: TraitRef, fld: &mut T) -> TraitRef { | |
832 | let id = fld.new_id(p.ref_id); | |
833 | let TraitRef { | |
834 | path, | |
835 | ref_id: _, | |
836 | } = p; | |
837 | ast::TraitRef { | |
838 | path: fld.fold_path(path), | |
839 | ref_id: id, | |
223e47cc | 840 | } |
1a4d82fc | 841 | } |
223e47cc | 842 | |
1a4d82fc JJ |
843 | pub fn noop_fold_poly_trait_ref<T: Folder>(p: PolyTraitRef, fld: &mut T) -> PolyTraitRef { |
844 | ast::PolyTraitRef { | |
845 | bound_lifetimes: fld.fold_lifetime_defs(p.bound_lifetimes), | |
85aaf69f SL |
846 | trait_ref: fld.fold_trait_ref(p.trait_ref), |
847 | span: fld.new_span(p.span), | |
1a4d82fc JJ |
848 | } |
849 | } | |
850 | ||
851 | pub fn noop_fold_struct_field<T: Folder>(f: StructField, fld: &mut T) -> StructField { | |
852 | let StructField {node: StructField_ {id, kind, ty, attrs}, span} = f; | |
853 | Spanned { | |
854 | node: StructField_ { | |
855 | id: fld.new_id(id), | |
856 | kind: kind, | |
857 | ty: fld.fold_ty(ty), | |
85aaf69f | 858 | attrs: fold_attrs(attrs, fld), |
1a4d82fc JJ |
859 | }, |
860 | span: fld.new_span(span) | |
223e47cc | 861 | } |
1a4d82fc | 862 | } |
223e47cc | 863 | |
1a4d82fc JJ |
864 | pub fn noop_fold_field<T: Folder>(Field {ident, expr, span}: Field, folder: &mut T) -> Field { |
865 | Field { | |
866 | ident: respan(ident.span, folder.fold_ident(ident.node)), | |
867 | expr: folder.fold_expr(expr), | |
868 | span: folder.new_span(span) | |
869 | } | |
870 | } | |
223e47cc | 871 | |
1a4d82fc JJ |
872 | pub fn noop_fold_mt<T: Folder>(MutTy {ty, mutbl}: MutTy, folder: &mut T) -> MutTy { |
873 | MutTy { | |
874 | ty: folder.fold_ty(ty), | |
875 | mutbl: mutbl, | |
223e47cc LB |
876 | } |
877 | } | |
878 | ||
1a4d82fc JJ |
879 | pub fn noop_fold_opt_bounds<T: Folder>(b: Option<OwnedSlice<TyParamBound>>, folder: &mut T) |
880 | -> Option<OwnedSlice<TyParamBound>> { | |
881 | b.map(|bounds| folder.fold_bounds(bounds)) | |
882 | } | |
883 | ||
884 | fn noop_fold_bounds<T: Folder>(bounds: TyParamBounds, folder: &mut T) | |
885 | -> TyParamBounds { | |
886 | bounds.move_map(|bound| folder.fold_ty_param_bound(bound)) | |
223e47cc LB |
887 | } |
888 | ||
1a4d82fc JJ |
889 | fn noop_fold_variant_arg<T: Folder>(VariantArg {id, ty}: VariantArg, folder: &mut T) |
890 | -> VariantArg { | |
891 | VariantArg { | |
892 | id: folder.new_id(id), | |
893 | ty: folder.fold_ty(ty) | |
223e47cc LB |
894 | } |
895 | } | |
896 | ||
1a4d82fc | 897 | pub fn noop_fold_block<T: Folder>(b: P<Block>, folder: &mut T) -> P<Block> { |
85aaf69f | 898 | b.map(|Block {id, stmts, expr, rules, span}| Block { |
1a4d82fc | 899 | id: folder.new_id(id), |
1a4d82fc JJ |
900 | stmts: stmts.into_iter().flat_map(|s| folder.fold_stmt(s).into_iter()).collect(), |
901 | expr: expr.map(|x| folder.fold_expr(x)), | |
902 | rules: rules, | |
903 | span: folder.new_span(span), | |
904 | }) | |
905 | } | |
906 | ||
907 | pub fn noop_fold_item_underscore<T: Folder>(i: Item_, folder: &mut T) -> Item_ { | |
908 | match i { | |
85aaf69f SL |
909 | ItemExternCrate(string) => ItemExternCrate(string), |
910 | ItemUse(view_path) => { | |
911 | ItemUse(folder.fold_view_path(view_path)) | |
912 | } | |
1a4d82fc JJ |
913 | ItemStatic(t, m, e) => { |
914 | ItemStatic(folder.fold_ty(t), m, folder.fold_expr(e)) | |
223e47cc | 915 | } |
1a4d82fc JJ |
916 | ItemConst(t, e) => { |
917 | ItemConst(folder.fold_ty(t), folder.fold_expr(e)) | |
223e47cc | 918 | } |
62682a34 | 919 | ItemFn(decl, unsafety, constness, abi, generics, body) => { |
1a4d82fc JJ |
920 | ItemFn( |
921 | folder.fold_fn_decl(decl), | |
922 | unsafety, | |
62682a34 | 923 | constness, |
1a4d82fc JJ |
924 | abi, |
925 | folder.fold_generics(generics), | |
926 | folder.fold_block(body) | |
927 | ) | |
970d7e83 | 928 | } |
1a4d82fc JJ |
929 | ItemMod(m) => ItemMod(folder.fold_mod(m)), |
930 | ItemForeignMod(nm) => ItemForeignMod(folder.fold_foreign_mod(nm)), | |
931 | ItemTy(t, generics) => { | |
932 | ItemTy(folder.fold_ty(t), folder.fold_generics(generics)) | |
223e47cc | 933 | } |
1a4d82fc JJ |
934 | ItemEnum(enum_definition, generics) => { |
935 | ItemEnum( | |
936 | ast::EnumDef { | |
937 | variants: enum_definition.variants.move_map(|x| folder.fold_variant(x)), | |
938 | }, | |
939 | folder.fold_generics(generics)) | |
970d7e83 | 940 | } |
1a4d82fc JJ |
941 | ItemStruct(struct_def, generics) => { |
942 | let struct_def = folder.fold_struct_def(struct_def); | |
943 | ItemStruct(struct_def, folder.fold_generics(generics)) | |
223e47cc | 944 | } |
c34b1796 AL |
945 | ItemDefaultImpl(unsafety, ref trait_ref) => { |
946 | ItemDefaultImpl(unsafety, folder.fold_trait_ref((*trait_ref).clone())) | |
947 | } | |
1a4d82fc | 948 | ItemImpl(unsafety, polarity, generics, ifce, ty, impl_items) => { |
85aaf69f SL |
949 | let new_impl_items = impl_items.into_iter().flat_map(|item| { |
950 | folder.fold_impl_item(item).into_iter() | |
951 | }).collect(); | |
1a4d82fc JJ |
952 | let ifce = match ifce { |
953 | None => None, | |
954 | Some(ref trait_ref) => { | |
955 | Some(folder.fold_trait_ref((*trait_ref).clone())) | |
956 | } | |
957 | }; | |
958 | ItemImpl(unsafety, | |
959 | polarity, | |
960 | folder.fold_generics(generics), | |
961 | ifce, | |
962 | folder.fold_ty(ty), | |
963 | new_impl_items) | |
223e47cc | 964 | } |
85aaf69f | 965 | ItemTrait(unsafety, generics, bounds, items) => { |
1a4d82fc | 966 | let bounds = folder.fold_bounds(bounds); |
85aaf69f SL |
967 | let items = items.into_iter().flat_map(|item| { |
968 | folder.fold_trait_item(item).into_iter() | |
1a4d82fc JJ |
969 | }).collect(); |
970 | ItemTrait(unsafety, | |
971 | folder.fold_generics(generics), | |
972 | bounds, | |
85aaf69f | 973 | items) |
1a4d82fc JJ |
974 | } |
975 | ItemMac(m) => ItemMac(folder.fold_mac(m)), | |
223e47cc | 976 | } |
1a4d82fc JJ |
977 | } |
978 | ||
c34b1796 AL |
979 | pub fn noop_fold_trait_item<T: Folder>(i: P<TraitItem>, folder: &mut T) |
980 | -> SmallVector<P<TraitItem>> { | |
981 | SmallVector::one(i.map(|TraitItem {id, ident, attrs, node, span}| TraitItem { | |
982 | id: folder.new_id(id), | |
983 | ident: folder.fold_ident(ident), | |
984 | attrs: fold_attrs(attrs, folder), | |
985 | node: match node { | |
d9579d0f AL |
986 | ConstTraitItem(ty, default) => { |
987 | ConstTraitItem(folder.fold_ty(ty), | |
988 | default.map(|x| folder.fold_expr(x))) | |
989 | } | |
c34b1796 AL |
990 | MethodTraitItem(sig, body) => { |
991 | MethodTraitItem(noop_fold_method_sig(sig, folder), | |
992 | body.map(|x| folder.fold_block(x))) | |
993 | } | |
994 | TypeTraitItem(bounds, default) => { | |
995 | TypeTraitItem(folder.fold_bounds(bounds), | |
996 | default.map(|x| folder.fold_ty(x))) | |
997 | } | |
998 | }, | |
999 | span: folder.new_span(span) | |
1000 | })) | |
85aaf69f SL |
1001 | } |
1002 | ||
c34b1796 AL |
1003 | pub fn noop_fold_impl_item<T: Folder>(i: P<ImplItem>, folder: &mut T) |
1004 | -> SmallVector<P<ImplItem>> { | |
1005 | SmallVector::one(i.map(|ImplItem {id, ident, attrs, node, vis, span}| ImplItem { | |
1006 | id: folder.new_id(id), | |
1007 | ident: folder.fold_ident(ident), | |
1008 | attrs: fold_attrs(attrs, folder), | |
1a4d82fc | 1009 | vis: vis, |
c34b1796 | 1010 | node: match node { |
d9579d0f AL |
1011 | ConstImplItem(ty, expr) => { |
1012 | ConstImplItem(folder.fold_ty(ty), folder.fold_expr(expr)) | |
1013 | } | |
c34b1796 AL |
1014 | MethodImplItem(sig, body) => { |
1015 | MethodImplItem(noop_fold_method_sig(sig, folder), | |
1016 | folder.fold_block(body)) | |
1017 | } | |
1018 | TypeImplItem(ty) => TypeImplItem(folder.fold_ty(ty)), | |
1019 | MacImplItem(mac) => MacImplItem(folder.fold_mac(mac)) | |
1020 | }, | |
1021 | span: folder.new_span(span) | |
1022 | })) | |
1a4d82fc JJ |
1023 | } |
1024 | ||
85aaf69f | 1025 | pub fn noop_fold_mod<T: Folder>(Mod {inner, items}: Mod, folder: &mut T) -> Mod { |
1a4d82fc JJ |
1026 | Mod { |
1027 | inner: folder.new_span(inner), | |
1a4d82fc | 1028 | items: items.into_iter().flat_map(|x| folder.fold_item(x).into_iter()).collect(), |
223e47cc | 1029 | } |
1a4d82fc JJ |
1030 | } |
1031 | ||
1032 | pub fn noop_fold_crate<T: Folder>(Crate {module, attrs, config, mut exported_macros, span}: Crate, | |
1033 | folder: &mut T) -> Crate { | |
1034 | let config = folder.fold_meta_items(config); | |
1035 | ||
1036 | let mut items = folder.fold_item(P(ast::Item { | |
1037 | ident: token::special_idents::invalid, | |
1038 | attrs: attrs, | |
1039 | id: ast::DUMMY_NODE_ID, | |
1040 | vis: ast::Public, | |
1041 | span: span, | |
1042 | node: ast::ItemMod(module), | |
1043 | })).into_iter(); | |
1044 | ||
1045 | let (module, attrs, span) = match items.next() { | |
1046 | Some(item) => { | |
1047 | assert!(items.next().is_none(), | |
1048 | "a crate cannot expand to more than one item"); | |
1049 | item.and_then(|ast::Item { attrs, span, node, .. }| { | |
1050 | match node { | |
1051 | ast::ItemMod(m) => (m, attrs, span), | |
1052 | _ => panic!("fold converted a module to not a module"), | |
1053 | } | |
1054 | }) | |
1055 | } | |
1056 | None => (ast::Mod { | |
1057 | inner: span, | |
85aaf69f SL |
1058 | items: vec![], |
1059 | }, vec![], span) | |
1a4d82fc JJ |
1060 | }; |
1061 | ||
85aaf69f | 1062 | for def in &mut exported_macros { |
1a4d82fc | 1063 | def.id = folder.new_id(def.id); |
223e47cc | 1064 | } |
1a4d82fc JJ |
1065 | |
1066 | Crate { | |
1067 | module: module, | |
1068 | attrs: attrs, | |
1069 | config: config, | |
1070 | exported_macros: exported_macros, | |
1071 | span: span, | |
223e47cc | 1072 | } |
1a4d82fc JJ |
1073 | } |
1074 | ||
1075 | // fold one item into possibly many items | |
1076 | pub fn noop_fold_item<T: Folder>(i: P<Item>, folder: &mut T) -> SmallVector<P<Item>> { | |
1077 | SmallVector::one(i.map(|i| folder.fold_item_simple(i))) | |
1078 | } | |
1079 | ||
1080 | // fold one item into exactly one item | |
1081 | pub fn noop_fold_item_simple<T: Folder>(Item {id, ident, attrs, node, vis, span}: Item, | |
1082 | folder: &mut T) -> Item { | |
1083 | let id = folder.new_id(id); | |
1084 | let node = folder.fold_item_underscore(node); | |
1085 | let ident = match node { | |
1086 | // The node may have changed, recompute the "pretty" impl name. | |
1087 | ItemImpl(_, _, _, ref maybe_trait, ref ty, _) => { | |
c34b1796 | 1088 | ast_util::impl_pretty_name(maybe_trait, Some(&**ty)) |
1a4d82fc JJ |
1089 | } |
1090 | _ => ident | |
1091 | }; | |
1092 | ||
1093 | Item { | |
1094 | id: id, | |
1095 | ident: folder.fold_ident(ident), | |
85aaf69f | 1096 | attrs: fold_attrs(attrs, folder), |
1a4d82fc JJ |
1097 | node: node, |
1098 | vis: vis, | |
1099 | span: folder.new_span(span) | |
223e47cc LB |
1100 | } |
1101 | } | |
1102 | ||
1a4d82fc JJ |
1103 | pub fn noop_fold_foreign_item<T: Folder>(ni: P<ForeignItem>, folder: &mut T) -> P<ForeignItem> { |
1104 | ni.map(|ForeignItem {id, ident, attrs, node, span, vis}| ForeignItem { | |
1105 | id: folder.new_id(id), | |
1106 | ident: folder.fold_ident(ident), | |
85aaf69f | 1107 | attrs: fold_attrs(attrs, folder), |
1a4d82fc JJ |
1108 | node: match node { |
1109 | ForeignItemFn(fdec, generics) => { | |
85aaf69f | 1110 | ForeignItemFn(folder.fold_fn_decl(fdec), folder.fold_generics(generics)) |
1a4d82fc JJ |
1111 | } |
1112 | ForeignItemStatic(t, m) => { | |
1113 | ForeignItemStatic(folder.fold_ty(t), m) | |
1114 | } | |
1115 | }, | |
1116 | vis: vis, | |
1117 | span: folder.new_span(span) | |
1118 | }) | |
1119 | } | |
1120 | ||
c34b1796 AL |
1121 | pub fn noop_fold_method_sig<T: Folder>(sig: MethodSig, folder: &mut T) -> MethodSig { |
1122 | MethodSig { | |
1123 | generics: folder.fold_generics(sig.generics), | |
1124 | abi: sig.abi, | |
1125 | explicit_self: folder.fold_explicit_self(sig.explicit_self), | |
1126 | unsafety: sig.unsafety, | |
62682a34 | 1127 | constness: sig.constness, |
c34b1796 AL |
1128 | decl: folder.fold_fn_decl(sig.decl) |
1129 | } | |
970d7e83 LB |
1130 | } |
1131 | ||
1a4d82fc JJ |
1132 | pub fn noop_fold_pat<T: Folder>(p: P<Pat>, folder: &mut T) -> P<Pat> { |
1133 | p.map(|Pat {id, node, span}| Pat { | |
1134 | id: folder.new_id(id), | |
1135 | node: match node { | |
1136 | PatWild(k) => PatWild(k), | |
1137 | PatIdent(binding_mode, pth1, sub) => { | |
1138 | PatIdent(binding_mode, | |
1139 | Spanned{span: folder.new_span(pth1.span), | |
1140 | node: folder.fold_ident(pth1.node)}, | |
1141 | sub.map(|x| folder.fold_pat(x))) | |
1142 | } | |
1143 | PatLit(e) => PatLit(folder.fold_expr(e)), | |
1144 | PatEnum(pth, pats) => { | |
1145 | PatEnum(folder.fold_path(pth), | |
1146 | pats.map(|pats| pats.move_map(|x| folder.fold_pat(x)))) | |
1147 | } | |
d9579d0f AL |
1148 | PatQPath(qself, pth) => { |
1149 | let qself = QSelf {ty: folder.fold_ty(qself.ty), .. qself}; | |
1150 | PatQPath(qself, folder.fold_path(pth)) | |
1151 | } | |
1a4d82fc JJ |
1152 | PatStruct(pth, fields, etc) => { |
1153 | let pth = folder.fold_path(pth); | |
1154 | let fs = fields.move_map(|f| { | |
1155 | Spanned { span: folder.new_span(f.span), | |
1156 | node: ast::FieldPat { | |
1157 | ident: f.node.ident, | |
1158 | pat: folder.fold_pat(f.node.pat), | |
1159 | is_shorthand: f.node.is_shorthand, | |
1160 | }} | |
1161 | }); | |
1162 | PatStruct(pth, fs, etc) | |
1163 | } | |
1164 | PatTup(elts) => PatTup(elts.move_map(|x| folder.fold_pat(x))), | |
1165 | PatBox(inner) => PatBox(folder.fold_pat(inner)), | |
1166 | PatRegion(inner, mutbl) => PatRegion(folder.fold_pat(inner), mutbl), | |
1167 | PatRange(e1, e2) => { | |
1168 | PatRange(folder.fold_expr(e1), folder.fold_expr(e2)) | |
1169 | }, | |
1170 | PatVec(before, slice, after) => { | |
1171 | PatVec(before.move_map(|x| folder.fold_pat(x)), | |
1172 | slice.map(|x| folder.fold_pat(x)), | |
1173 | after.move_map(|x| folder.fold_pat(x))) | |
1174 | } | |
1175 | PatMac(mac) => PatMac(folder.fold_mac(mac)) | |
1176 | }, | |
1177 | span: folder.new_span(span) | |
1178 | }) | |
1179 | } | |
1180 | ||
1181 | pub fn noop_fold_expr<T: Folder>(Expr {id, node, span}: Expr, folder: &mut T) -> Expr { | |
1182 | Expr { | |
1183 | id: folder.new_id(id), | |
1184 | node: match node { | |
1185 | ExprBox(p, e) => { | |
1186 | ExprBox(p.map(|e|folder.fold_expr(e)), folder.fold_expr(e)) | |
1187 | } | |
1188 | ExprVec(exprs) => { | |
1189 | ExprVec(exprs.move_map(|x| folder.fold_expr(x))) | |
1190 | } | |
1191 | ExprRepeat(expr, count) => { | |
1192 | ExprRepeat(folder.fold_expr(expr), folder.fold_expr(count)) | |
1193 | } | |
1194 | ExprTup(elts) => ExprTup(elts.move_map(|x| folder.fold_expr(x))), | |
1195 | ExprCall(f, args) => { | |
1196 | ExprCall(folder.fold_expr(f), | |
1197 | args.move_map(|x| folder.fold_expr(x))) | |
1198 | } | |
1199 | ExprMethodCall(i, tps, args) => { | |
1200 | ExprMethodCall( | |
9346a6ac | 1201 | respan(folder.new_span(i.span), folder.fold_ident(i.node)), |
1a4d82fc JJ |
1202 | tps.move_map(|x| folder.fold_ty(x)), |
1203 | args.move_map(|x| folder.fold_expr(x))) | |
1204 | } | |
1205 | ExprBinary(binop, lhs, rhs) => { | |
1206 | ExprBinary(binop, | |
1207 | folder.fold_expr(lhs), | |
1208 | folder.fold_expr(rhs)) | |
1209 | } | |
1210 | ExprUnary(binop, ohs) => { | |
1211 | ExprUnary(binop, folder.fold_expr(ohs)) | |
1212 | } | |
1213 | ExprLit(l) => ExprLit(l), | |
1214 | ExprCast(expr, ty) => { | |
1215 | ExprCast(folder.fold_expr(expr), folder.fold_ty(ty)) | |
1216 | } | |
1217 | ExprAddrOf(m, ohs) => ExprAddrOf(m, folder.fold_expr(ohs)), | |
1218 | ExprIf(cond, tr, fl) => { | |
1219 | ExprIf(folder.fold_expr(cond), | |
1220 | folder.fold_block(tr), | |
1221 | fl.map(|x| folder.fold_expr(x))) | |
1222 | } | |
1223 | ExprIfLet(pat, expr, tr, fl) => { | |
1224 | ExprIfLet(folder.fold_pat(pat), | |
1225 | folder.fold_expr(expr), | |
1226 | folder.fold_block(tr), | |
1227 | fl.map(|x| folder.fold_expr(x))) | |
1228 | } | |
1229 | ExprWhile(cond, body, opt_ident) => { | |
1230 | ExprWhile(folder.fold_expr(cond), | |
1231 | folder.fold_block(body), | |
1232 | opt_ident.map(|i| folder.fold_ident(i))) | |
1233 | } | |
1234 | ExprWhileLet(pat, expr, body, opt_ident) => { | |
1235 | ExprWhileLet(folder.fold_pat(pat), | |
1236 | folder.fold_expr(expr), | |
1237 | folder.fold_block(body), | |
1238 | opt_ident.map(|i| folder.fold_ident(i))) | |
1239 | } | |
1240 | ExprForLoop(pat, iter, body, opt_ident) => { | |
1241 | ExprForLoop(folder.fold_pat(pat), | |
1242 | folder.fold_expr(iter), | |
1243 | folder.fold_block(body), | |
1244 | opt_ident.map(|i| folder.fold_ident(i))) | |
1245 | } | |
1246 | ExprLoop(body, opt_ident) => { | |
1247 | ExprLoop(folder.fold_block(body), | |
1248 | opt_ident.map(|i| folder.fold_ident(i))) | |
1249 | } | |
1250 | ExprMatch(expr, arms, source) => { | |
1251 | ExprMatch(folder.fold_expr(expr), | |
1252 | arms.move_map(|x| folder.fold_arm(x)), | |
1253 | source) | |
1254 | } | |
85aaf69f | 1255 | ExprClosure(capture_clause, decl, body) => { |
1a4d82fc | 1256 | ExprClosure(capture_clause, |
1a4d82fc JJ |
1257 | folder.fold_fn_decl(decl), |
1258 | folder.fold_block(body)) | |
1259 | } | |
1260 | ExprBlock(blk) => ExprBlock(folder.fold_block(blk)), | |
1261 | ExprAssign(el, er) => { | |
1262 | ExprAssign(folder.fold_expr(el), folder.fold_expr(er)) | |
1263 | } | |
1264 | ExprAssignOp(op, el, er) => { | |
1265 | ExprAssignOp(op, | |
1266 | folder.fold_expr(el), | |
1267 | folder.fold_expr(er)) | |
1268 | } | |
1269 | ExprField(el, ident) => { | |
1270 | ExprField(folder.fold_expr(el), | |
9346a6ac AL |
1271 | respan(folder.new_span(ident.span), |
1272 | folder.fold_ident(ident.node))) | |
1a4d82fc JJ |
1273 | } |
1274 | ExprTupField(el, ident) => { | |
1275 | ExprTupField(folder.fold_expr(el), | |
9346a6ac AL |
1276 | respan(folder.new_span(ident.span), |
1277 | folder.fold_usize(ident.node))) | |
1a4d82fc JJ |
1278 | } |
1279 | ExprIndex(el, er) => { | |
1280 | ExprIndex(folder.fold_expr(el), folder.fold_expr(er)) | |
1281 | } | |
1282 | ExprRange(e1, e2) => { | |
1283 | ExprRange(e1.map(|x| folder.fold_expr(x)), | |
1284 | e2.map(|x| folder.fold_expr(x))) | |
1285 | } | |
c34b1796 AL |
1286 | ExprPath(qself, path) => { |
1287 | let qself = qself.map(|QSelf { ty, position }| { | |
1288 | QSelf { | |
1289 | ty: folder.fold_ty(ty), | |
1290 | position: position | |
1291 | } | |
1292 | }); | |
1293 | ExprPath(qself, folder.fold_path(path)) | |
1294 | } | |
1a4d82fc JJ |
1295 | ExprBreak(opt_ident) => ExprBreak(opt_ident.map(|x| folder.fold_ident(x))), |
1296 | ExprAgain(opt_ident) => ExprAgain(opt_ident.map(|x| folder.fold_ident(x))), | |
1297 | ExprRet(e) => ExprRet(e.map(|x| folder.fold_expr(x))), | |
1298 | ExprInlineAsm(InlineAsm { | |
1299 | inputs, | |
1300 | outputs, | |
1301 | asm, | |
1302 | asm_str_style, | |
1303 | clobbers, | |
1304 | volatile, | |
1305 | alignstack, | |
1306 | dialect, | |
1307 | expn_id, | |
1308 | }) => ExprInlineAsm(InlineAsm { | |
1309 | inputs: inputs.move_map(|(c, input)| { | |
1310 | (c, folder.fold_expr(input)) | |
1311 | }), | |
1312 | outputs: outputs.move_map(|(c, out, is_rw)| { | |
1313 | (c, folder.fold_expr(out), is_rw) | |
1314 | }), | |
1315 | asm: asm, | |
1316 | asm_str_style: asm_str_style, | |
1317 | clobbers: clobbers, | |
1318 | volatile: volatile, | |
1319 | alignstack: alignstack, | |
1320 | dialect: dialect, | |
1321 | expn_id: expn_id, | |
1322 | }), | |
1323 | ExprMac(mac) => ExprMac(folder.fold_mac(mac)), | |
1324 | ExprStruct(path, fields, maybe_expr) => { | |
1325 | ExprStruct(folder.fold_path(path), | |
1326 | fields.move_map(|x| folder.fold_field(x)), | |
1327 | maybe_expr.map(|x| folder.fold_expr(x))) | |
1328 | }, | |
1329 | ExprParen(ex) => ExprParen(folder.fold_expr(ex)) | |
1330 | }, | |
1331 | span: folder.new_span(span) | |
223e47cc LB |
1332 | } |
1333 | } | |
1334 | ||
1a4d82fc JJ |
1335 | pub fn noop_fold_stmt<T: Folder>(Spanned {node, span}: Stmt, folder: &mut T) |
1336 | -> SmallVector<P<Stmt>> { | |
1337 | let span = folder.new_span(span); | |
1338 | match node { | |
1339 | StmtDecl(d, id) => { | |
1340 | let id = folder.new_id(id); | |
1341 | folder.fold_decl(d).into_iter().map(|d| P(Spanned { | |
1342 | node: StmtDecl(d, id), | |
1343 | span: span | |
1344 | })).collect() | |
1345 | } | |
1346 | StmtExpr(e, id) => { | |
1347 | let id = folder.new_id(id); | |
1348 | SmallVector::one(P(Spanned { | |
1349 | node: StmtExpr(folder.fold_expr(e), id), | |
1350 | span: span | |
1351 | })) | |
1352 | } | |
1353 | StmtSemi(e, id) => { | |
1354 | let id = folder.new_id(id); | |
1355 | SmallVector::one(P(Spanned { | |
1356 | node: StmtSemi(folder.fold_expr(e), id), | |
1357 | span: span | |
1358 | })) | |
1359 | } | |
1360 | StmtMac(mac, semi) => SmallVector::one(P(Spanned { | |
1361 | node: StmtMac(mac.map(|m| folder.fold_mac(m)), semi), | |
1362 | span: span | |
1363 | })) | |
1364 | } | |
223e47cc LB |
1365 | } |
1366 | ||
970d7e83 | 1367 | #[cfg(test)] |
d9579d0f | 1368 | mod tests { |
c34b1796 | 1369 | use std::io; |
970d7e83 LB |
1370 | use ast; |
1371 | use util::parser_testing::{string_to_crate, matches_codepattern}; | |
1372 | use parse::token; | |
1373 | use print::pprust; | |
1a4d82fc | 1374 | use fold; |
970d7e83 LB |
1375 | use super::*; |
1376 | ||
970d7e83 | 1377 | // this version doesn't care about getting comments or docstrings in. |
1a4d82fc | 1378 | fn fake_print_crate(s: &mut pprust::State, |
c34b1796 | 1379 | krate: &ast::Crate) -> io::Result<()> { |
85aaf69f | 1380 | s.print_mod(&krate.module, &krate.attrs) |
970d7e83 LB |
1381 | } |
1382 | ||
1383 | // change every identifier to "zz" | |
1a4d82fc JJ |
1384 | struct ToZzIdentFolder; |
1385 | ||
1386 | impl Folder for ToZzIdentFolder { | |
1387 | fn fold_ident(&mut self, _: ast::Ident) -> ast::Ident { | |
1388 | token::str_to_ident("zz") | |
1389 | } | |
1390 | fn fold_mac(&mut self, mac: ast::Mac) -> ast::Mac { | |
1391 | fold::noop_fold_mac(mac, self) | |
1392 | } | |
970d7e83 LB |
1393 | } |
1394 | ||
1395 | // maybe add to expand.rs... | |
1a4d82fc | 1396 | macro_rules! assert_pred { |
970d7e83 LB |
1397 | ($pred:expr, $predname:expr, $a:expr , $b:expr) => ( |
1398 | { | |
1399 | let pred_val = $pred; | |
1400 | let a_val = $a; | |
1401 | let b_val = $b; | |
85aaf69f | 1402 | if !(pred_val(&a_val, &b_val)) { |
1a4d82fc | 1403 | panic!("expected args satisfying {}, got {} and {}", |
970d7e83 LB |
1404 | $predname, a_val, b_val); |
1405 | } | |
1406 | } | |
1407 | ) | |
1a4d82fc | 1408 | } |
970d7e83 LB |
1409 | |
1410 | // make sure idents get transformed everywhere | |
1411 | #[test] fn ident_transformation () { | |
1a4d82fc JJ |
1412 | let mut zz_fold = ToZzIdentFolder; |
1413 | let ast = string_to_crate( | |
1414 | "#[a] mod b {fn c (d : e, f : g) {h!(i,j,k);l;m}}".to_string()); | |
1415 | let folded_crate = zz_fold.fold_crate(ast); | |
1416 | assert_pred!( | |
1417 | matches_codepattern, | |
1418 | "matches_codepattern", | |
1419 | pprust::to_string(|s| fake_print_crate(s, &folded_crate)), | |
1420 | "#[a]mod zz{fn zz(zz:zz,zz:zz){zz!(zz,zz,zz);zz;zz}}".to_string()); | |
970d7e83 LB |
1421 | } |
1422 | ||
1423 | // even inside macro defs.... | |
1424 | #[test] fn ident_transformation_in_defs () { | |
1a4d82fc JJ |
1425 | let mut zz_fold = ToZzIdentFolder; |
1426 | let ast = string_to_crate( | |
1427 | "macro_rules! a {(b $c:expr $(d $e:token)f+ => \ | |
1428 | (g $(d $d $e)+))} ".to_string()); | |
1429 | let folded_crate = zz_fold.fold_crate(ast); | |
1430 | assert_pred!( | |
1431 | matches_codepattern, | |
1432 | "matches_codepattern", | |
1433 | pprust::to_string(|s| fake_print_crate(s, &folded_crate)), | |
1434 | "zz!zz((zz$zz:zz$(zz $zz:zz)zz+=>(zz$(zz$zz$zz)+)));".to_string()); | |
970d7e83 LB |
1435 | } |
1436 | } |