1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
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.
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
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.
24 use codemap
::{respan, Span, Spanned}
;
25 use owned_slice
::OwnedSlice
;
29 use util
::small_vector
::SmallVector
;
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
;
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
{
42 // FIXME(#5016) this shouldn't need to zero to be safe.
43 ptr
::write(p
, f(ptr
::read_and_drop(p
)));
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
))
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.
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.
65 fn fold_crate(&mut self, c
: Crate
) -> Crate
{
66 noop_fold_crate(c
, self)
69 fn fold_meta_items(&mut self, meta_items
: Vec
<P
<MetaItem
>>) -> Vec
<P
<MetaItem
>> {
70 noop_fold_meta_items(meta_items
, self)
73 fn fold_meta_item(&mut self, meta_item
: P
<MetaItem
>) -> P
<MetaItem
> {
74 noop_fold_meta_item(meta_item
, self)
77 fn fold_view_path(&mut self, view_path
: P
<ViewPath
>) -> P
<ViewPath
> {
78 noop_fold_view_path(view_path
, self)
81 fn fold_foreign_item(&mut self, ni
: P
<ForeignItem
>) -> P
<ForeignItem
> {
82 noop_fold_foreign_item(ni
, self)
85 fn fold_item(&mut self, i
: P
<Item
>) -> SmallVector
<P
<Item
>> {
86 noop_fold_item(i
, self)
89 fn fold_item_simple(&mut self, i
: Item
) -> Item
{
90 noop_fold_item_simple(i
, self)
93 fn fold_struct_field(&mut self, sf
: StructField
) -> StructField
{
94 noop_fold_struct_field(sf
, self)
97 fn fold_item_underscore(&mut self, i
: Item_
) -> Item_
{
98 noop_fold_item_underscore(i
, self)
101 fn fold_trait_item(&mut self, i
: P
<TraitItem
>) -> SmallVector
<P
<TraitItem
>> {
102 noop_fold_trait_item(i
, self)
105 fn fold_impl_item(&mut self, i
: P
<ImplItem
>) -> SmallVector
<P
<ImplItem
>> {
106 noop_fold_impl_item(i
, self)
109 fn fold_fn_decl(&mut self, d
: P
<FnDecl
>) -> P
<FnDecl
> {
110 noop_fold_fn_decl(d
, self)
113 fn fold_block(&mut self, b
: P
<Block
>) -> P
<Block
> {
114 noop_fold_block(b
, self)
117 fn fold_stmt(&mut self, s
: P
<Stmt
>) -> SmallVector
<P
<Stmt
>> {
118 s
.and_then(|s
| noop_fold_stmt(s
, self))
121 fn fold_arm(&mut self, a
: Arm
) -> Arm
{
122 noop_fold_arm(a
, self)
125 fn fold_pat(&mut self, p
: P
<Pat
>) -> P
<Pat
> {
126 noop_fold_pat(p
, self)
129 fn fold_decl(&mut self, d
: P
<Decl
>) -> SmallVector
<P
<Decl
>> {
130 noop_fold_decl(d
, self)
133 fn fold_expr(&mut self, e
: P
<Expr
>) -> P
<Expr
> {
134 e
.map(|e
| noop_fold_expr(e
, self))
137 fn fold_ty(&mut self, t
: P
<Ty
>) -> P
<Ty
> {
138 noop_fold_ty(t
, self)
141 fn fold_ty_binding(&mut self, t
: P
<TypeBinding
>) -> P
<TypeBinding
> {
142 noop_fold_ty_binding(t
, self)
145 fn fold_mod(&mut self, m
: Mod
) -> Mod
{
146 noop_fold_mod(m
, self)
149 fn fold_foreign_mod(&mut self, nm
: ForeignMod
) -> ForeignMod
{
150 noop_fold_foreign_mod(nm
, self)
153 fn fold_variant(&mut self, v
: P
<Variant
>) -> P
<Variant
> {
154 noop_fold_variant(v
, self)
157 fn fold_ident(&mut self, i
: Ident
) -> Ident
{
158 noop_fold_ident(i
, self)
161 fn fold_usize(&mut self, i
: usize) -> usize {
162 noop_fold_usize(i
, self)
165 fn fold_path(&mut self, p
: Path
) -> Path
{
166 noop_fold_path(p
, self)
169 fn fold_path_parameters(&mut self, p
: PathParameters
) -> PathParameters
{
170 noop_fold_path_parameters(p
, self)
173 fn fold_angle_bracketed_parameter_data(&mut self, p
: AngleBracketedParameterData
)
174 -> AngleBracketedParameterData
176 noop_fold_angle_bracketed_parameter_data(p
, self)
179 fn fold_parenthesized_parameter_data(&mut self, p
: ParenthesizedParameterData
)
180 -> ParenthesizedParameterData
182 noop_fold_parenthesized_parameter_data(p
, self)
185 fn fold_local(&mut self, l
: P
<Local
>) -> P
<Local
> {
186 noop_fold_local(l
, self)
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)
198 fn fold_explicit_self(&mut self, es
: ExplicitSelf
) -> ExplicitSelf
{
199 noop_fold_explicit_self(es
, self)
202 fn fold_explicit_self_underscore(&mut self, es
: ExplicitSelf_
) -> ExplicitSelf_
{
203 noop_fold_explicit_self_underscore(es
, self)
206 fn fold_lifetime(&mut self, l
: Lifetime
) -> Lifetime
{
207 noop_fold_lifetime(l
, self)
210 fn fold_lifetime_def(&mut self, l
: LifetimeDef
) -> LifetimeDef
{
211 noop_fold_lifetime_def(l
, self)
214 fn fold_attribute(&mut self, at
: Attribute
) -> Option
<Attribute
> {
215 noop_fold_attribute(at
, self)
218 fn fold_arg(&mut self, a
: Arg
) -> Arg
{
219 noop_fold_arg(a
, self)
222 fn fold_generics(&mut self, generics
: Generics
) -> Generics
{
223 noop_fold_generics(generics
, self)
226 fn fold_trait_ref(&mut self, p
: TraitRef
) -> TraitRef
{
227 noop_fold_trait_ref(p
, self)
230 fn fold_poly_trait_ref(&mut self, p
: PolyTraitRef
) -> PolyTraitRef
{
231 noop_fold_poly_trait_ref(p
, self)
234 fn fold_struct_def(&mut self, struct_def
: P
<StructDef
>) -> P
<StructDef
> {
235 noop_fold_struct_def(struct_def
, self)
238 fn fold_lifetimes(&mut self, lts
: Vec
<Lifetime
>) -> Vec
<Lifetime
> {
239 noop_fold_lifetimes(lts
, self)
242 fn fold_lifetime_defs(&mut self, lts
: Vec
<LifetimeDef
>) -> Vec
<LifetimeDef
> {
243 noop_fold_lifetime_defs(lts
, self)
246 fn fold_ty_param(&mut self, tp
: TyParam
) -> TyParam
{
247 noop_fold_ty_param(tp
, self)
250 fn fold_ty_params(&mut self, tps
: OwnedSlice
<TyParam
>) -> OwnedSlice
<TyParam
> {
251 noop_fold_ty_params(tps
, self)
254 fn fold_tt(&mut self, tt
: &TokenTree
) -> TokenTree
{
255 noop_fold_tt(tt
, self)
258 fn fold_tts(&mut self, tts
: &[TokenTree
]) -> Vec
<TokenTree
> {
259 noop_fold_tts(tts
, self)
262 fn fold_token(&mut self, t
: token
::Token
) -> token
::Token
{
263 noop_fold_token(t
, self)
266 fn fold_interpolated(&mut self, nt
: token
::Nonterminal
) -> token
::Nonterminal
{
267 noop_fold_interpolated(nt
, self)
270 fn fold_opt_lifetime(&mut self, o_lt
: Option
<Lifetime
>) -> Option
<Lifetime
> {
271 noop_fold_opt_lifetime(o_lt
, self)
274 fn fold_variant_arg(&mut self, va
: VariantArg
) -> VariantArg
{
275 noop_fold_variant_arg(va
, self)
278 fn fold_opt_bounds(&mut self, b
: Option
<OwnedSlice
<TyParamBound
>>)
279 -> Option
<OwnedSlice
<TyParamBound
>> {
280 noop_fold_opt_bounds(b
, self)
283 fn fold_bounds(&mut self, b
: OwnedSlice
<TyParamBound
>)
284 -> OwnedSlice
<TyParamBound
> {
285 noop_fold_bounds(b
, self)
288 fn fold_ty_param_bound(&mut self, tpb
: TyParamBound
) -> TyParamBound
{
289 noop_fold_ty_param_bound(tpb
, self)
292 fn fold_mt(&mut self, mt
: MutTy
) -> MutTy
{
293 noop_fold_mt(mt
, self)
296 fn fold_field(&mut self, field
: Field
) -> Field
{
297 noop_fold_field(field
, self)
300 fn fold_where_clause(&mut self, where_clause
: WhereClause
)
302 noop_fold_where_clause(where_clause
, self)
305 fn fold_where_predicate(&mut self, where_predicate
: WherePredicate
)
307 noop_fold_where_predicate(where_predicate
, self)
310 fn new_id(&mut self, i
: NodeId
) -> NodeId
{
314 fn new_span(&mut self, sp
: Span
) -> Span
{
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
))
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
{
327 ViewPathSimple(ident
, path
) => {
328 ViewPathSimple(ident
, fld
.fold_path(path
))
330 ViewPathGlob(path
) => {
331 ViewPathGlob(fld
.fold_path(path
))
333 ViewPathList(path
, path_list_idents
) => {
334 ViewPathList(fld
.fold_path(path
),
335 path_list_idents
.move_map(|path_list_ident
| {
337 node
: match path_list_ident
.node
{
338 PathListIdent { id, name }
=>
343 PathListMod { id }
=>
344 PathListMod { id: fld.new_id(id) }
346 span
: fld
.new_span(path_list_ident
.span
)
351 span
: fld
.new_span(span
)
355 pub fn fold_attrs
<T
: Folder
>(attrs
: Vec
<Attribute
>, fld
: &mut T
) -> Vec
<Attribute
> {
356 attrs
.into_iter().flat_map(|x
| fld
.fold_attribute(x
).into_iter()).collect()
359 pub fn noop_fold_arm
<T
: Folder
>(Arm {attrs, pats, guard, body}
: Arm
, fld
: &mut T
) -> Arm
{
361 attrs
: fold_attrs(attrs
, fld
),
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
),
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
)
374 DeclItem(it
) => fld
.fold_item(it
).into_iter().map(|i
| P(Spanned
{
376 span
: fld
.new_span(span
)
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
{
386 span
: fld
.new_span(span
),
390 pub fn noop_fold_ty
<T
: Folder
>(t
: P
<Ty
>, fld
: &mut T
) -> P
<Ty
> {
391 t
.map(|Ty {id, node, span}
| Ty
{
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
))
401 TyBareFn(f
.map(|BareFnTy {lifetimes, unsafety, abi, decl}
| BareFnTy
{
402 lifetimes
: fld
.fold_lifetime_defs(lifetimes
),
405 decl
: fld
.fold_fn_decl(decl
)
408 TyTup(tys
) => TyTup(tys
.move_map(|ty
| fld
.fold_ty(ty
))),
409 TyParen(ty
) => TyParen(fld
.fold_ty(ty
)),
410 TyPath(qself
, path
) => {
411 let qself
= qself
.map(|QSelf { ty, position }
| {
417 TyPath(qself
, fld
.fold_path(path
))
419 TyObjectSum(ty
, bounds
) => {
420 TyObjectSum(fld
.fold_ty(ty
),
421 fld
.fold_bounds(bounds
))
423 TyFixedLengthVec(ty
, e
) => {
424 TyFixedLengthVec(fld
.fold_ty(ty
), fld
.fold_expr(e
))
427 TyTypeof(fld
.fold_expr(expr
))
429 TyPolyTraitRef(bounds
) => {
430 TyPolyTraitRef(bounds
.move_map(|b
| fld
.fold_ty_param_bound(b
)))
433 span
: fld
.new_span(span
)
437 pub fn noop_fold_foreign_mod
<T
: Folder
>(ForeignMod {abi, items}
: ForeignMod
,
438 fld
: &mut T
) -> ForeignMod
{
441 items
: items
.move_map(|x
| fld
.fold_foreign_item(x
)),
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
{
450 attrs
: fold_attrs(attrs
, fld
),
452 TupleVariantKind(variant_args
) => {
453 TupleVariantKind(variant_args
.move_map(|x
|
454 fld
.fold_variant_arg(x
)))
456 StructVariantKind(struct_def
) => {
457 StructVariantKind(fld
.fold_struct_def(struct_def
))
460 disr_expr
: disr_expr
.map(|e
| fld
.fold_expr(e
)),
463 span
: fld
.new_span(span
),
467 pub fn noop_fold_ident
<T
: Folder
>(i
: Ident
, _
: &mut T
) -> Ident
{
471 pub fn noop_fold_usize
<T
: Folder
>(i
: usize, _
: &mut T
) -> usize {
475 pub fn noop_fold_path
<T
: Folder
>(Path {global, segments, span}
: Path
, fld
: &mut T
) -> Path
{
478 segments
: segments
.move_map(|PathSegment {identifier, parameters}
| PathSegment
{
479 identifier
: fld
.fold_ident(identifier
),
480 parameters
: fld
.fold_path_parameters(parameters
),
482 span
: fld
.new_span(span
)
486 pub fn noop_fold_path_parameters
<T
: Folder
>(path_parameters
: PathParameters
, fld
: &mut T
)
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
)),
497 pub fn noop_fold_angle_bracketed_parameter_data
<T
: Folder
>(data
: AngleBracketedParameterData
,
499 -> AngleBracketedParameterData
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
)) }
507 pub fn noop_fold_parenthesized_parameter_data
<T
: Folder
>(data
: ParenthesizedParameterData
,
509 -> ParenthesizedParameterData
511 let ParenthesizedParameterData { inputs, output, span }
= data
;
512 ParenthesizedParameterData
{ inputs
: inputs
.move_map(|ty
| fld
.fold_ty(ty
)),
513 output
: output
.map(|ty
| fld
.fold_ty(ty
)),
514 span
: fld
.new_span(span
) }
517 pub fn noop_fold_local
<T
: Folder
>(l
: P
<Local
>, fld
: &mut T
) -> P
<Local
> {
518 l
.map(|Local {id, pat, ty, init, source, span}
| Local
{
520 ty
: ty
.map(|t
| fld
.fold_ty(t
)),
521 pat
: fld
.fold_pat(pat
),
522 init
: init
.map(|e
| fld
.fold_expr(e
)),
524 span
: fld
.new_span(span
)
528 pub fn noop_fold_attribute
<T
: Folder
>(at
: Attribute
, fld
: &mut T
) -> Option
<Attribute
> {
529 let Spanned {node: Attribute_ {id, style, value, is_sugared_doc}
, span
} = at
;
534 value
: fld
.fold_meta_item(value
),
535 is_sugared_doc
: is_sugared_doc
537 span
: fld
.new_span(span
)
541 pub fn noop_fold_explicit_self_underscore
<T
: Folder
>(es
: ExplicitSelf_
, fld
: &mut T
)
544 SelfStatic
| SelfValue(_
) => es
,
545 SelfRegion(lifetime
, m
, ident
) => {
546 SelfRegion(fld
.fold_opt_lifetime(lifetime
), m
, ident
)
548 SelfExplicit(typ
, ident
) => {
549 SelfExplicit(fld
.fold_ty(typ
), ident
)
554 pub fn noop_fold_explicit_self
<T
: Folder
>(Spanned {span, node}
: ExplicitSelf
, fld
: &mut T
)
557 node
: fld
.fold_explicit_self_underscore(node
),
558 span
: fld
.new_span(span
)
563 pub fn noop_fold_mac
<T
: Folder
>(Spanned {node, span}
: Mac
, fld
: &mut T
) -> Mac
{
566 MacInvocTT(p
, tts
, ctxt
) => {
567 MacInvocTT(fld
.fold_path(p
), fld
.fold_tts(&tts
), ctxt
)
570 span
: fld
.new_span(span
)
574 pub fn noop_fold_meta_item
<T
: Folder
>(mi
: P
<MetaItem
>, fld
: &mut T
) -> P
<MetaItem
> {
575 mi
.map(|Spanned {node, span}
| Spanned
{
577 MetaWord(id
) => MetaWord(id
),
578 MetaList(id
, mis
) => {
579 MetaList(id
, mis
.move_map(|e
| fld
.fold_meta_item(e
)))
581 MetaNameValue(id
, s
) => MetaNameValue(id
, s
)
583 span
: fld
.new_span(span
)
587 pub fn noop_fold_arg
<T
: Folder
>(Arg {id, pat, ty}
: Arg
, fld
: &mut T
) -> Arg
{
590 pat
: fld
.fold_pat(pat
),
595 pub fn noop_fold_tt
<T
: Folder
>(tt
: &TokenTree
, fld
: &mut T
) -> TokenTree
{
597 TtToken(span
, ref tok
) =>
598 TtToken(span
, fld
.fold_token(tok
.clone())),
599 TtDelimited(span
, ref delimed
) => {
600 TtDelimited(span
, Rc
::new(
602 delim
: delimed
.delim
,
603 open_span
: delimed
.open_span
,
604 tts
: fld
.fold_tts(&delimed
.tts
),
605 close_span
: delimed
.close_span
,
609 TtSequence(span
, ref seq
) =>
611 Rc
::new(SequenceRepetition
{
612 tts
: fld
.fold_tts(&seq
.tts
),
613 separator
: seq
.separator
.clone().map(|tok
| fld
.fold_token(tok
)),
619 pub fn noop_fold_tts
<T
: Folder
>(tts
: &[TokenTree
], fld
: &mut T
) -> Vec
<TokenTree
> {
620 tts
.iter().map(|tt
| fld
.fold_tt(tt
)).collect()
623 // apply ident folder if it's an ident, apply other folds to interpolated nodes
624 pub fn noop_fold_token
<T
: Folder
>(t
: token
::Token
, fld
: &mut T
) -> token
::Token
{
626 token
::Ident(id
, followed_by_colons
) => {
627 token
::Ident(fld
.fold_ident(id
), followed_by_colons
)
629 token
::Lifetime(id
) => token
::Lifetime(fld
.fold_ident(id
)),
630 token
::Interpolated(nt
) => token
::Interpolated(fld
.fold_interpolated(nt
)),
631 token
::SubstNt(ident
, namep
) => {
632 token
::SubstNt(fld
.fold_ident(ident
), namep
)
634 token
::MatchNt(name
, kind
, namep
, kindp
) => {
635 token
::MatchNt(fld
.fold_ident(name
), fld
.fold_ident(kind
), namep
, kindp
)
641 /// apply folder to elements of interpolated nodes
643 // NB: this can occur only when applying a fold to partially expanded code, where
644 // parsed pieces have gotten implanted ito *other* macro invocations. This is relevant
645 // for macro hygiene, but possibly not elsewhere.
647 // One problem here occurs because the types for fold_item, fold_stmt, etc. allow the
648 // folder to return *multiple* items; this is a problem for the nodes here, because
649 // they insist on having exactly one piece. One solution would be to mangle the fold
650 // trait to include one-to-many and one-to-one versions of these entry points, but that
651 // would probably confuse a lot of people and help very few. Instead, I'm just going
652 // to put in dynamic checks. I think the performance impact of this will be pretty much
653 // nonexistent. The danger is that someone will apply a fold to a partially expanded
654 // node, and will be confused by the fact that their "fold_item" or "fold_stmt" isn't
655 // getting called on NtItem or NtStmt nodes. Hopefully they'll wind up reading this
656 // comment, and doing something appropriate.
658 // BTW, design choice: I considered just changing the type of, e.g., NtItem to contain
659 // multiple items, but decided against it when I looked at parse_item_or_view_item and
660 // tried to figure out what I would do with multiple items there....
661 pub fn noop_fold_interpolated
<T
: Folder
>(nt
: token
::Nonterminal
, fld
: &mut T
)
662 -> token
::Nonterminal
{
664 token
::NtItem(item
) =>
665 token
::NtItem(fld
.fold_item(item
)
666 // this is probably okay, because the only folds likely
667 // to peek inside interpolated nodes will be renamings/markings,
668 // which map single items to single items
669 .expect_one("expected fold to produce exactly one item")),
670 token
::NtBlock(block
) => token
::NtBlock(fld
.fold_block(block
)),
671 token
::NtStmt(stmt
) =>
672 token
::NtStmt(fld
.fold_stmt(stmt
)
673 // this is probably okay, because the only folds likely
674 // to peek inside interpolated nodes will be renamings/markings,
675 // which map single items to single items
676 .expect_one("expected fold to produce exactly one statement")),
677 token
::NtPat(pat
) => token
::NtPat(fld
.fold_pat(pat
)),
678 token
::NtExpr(expr
) => token
::NtExpr(fld
.fold_expr(expr
)),
679 token
::NtTy(ty
) => token
::NtTy(fld
.fold_ty(ty
)),
680 token
::NtIdent(id
, is_mod_name
) =>
681 token
::NtIdent(Box
::new(fld
.fold_ident(*id
)), is_mod_name
),
682 token
::NtMeta(meta_item
) => token
::NtMeta(fld
.fold_meta_item(meta_item
)),
683 token
::NtPath(path
) => token
::NtPath(Box
::new(fld
.fold_path(*path
))),
684 token
::NtTT(tt
) => token
::NtTT(P(fld
.fold_tt(&*tt
))),
685 token
::NtArm(arm
) => token
::NtArm(fld
.fold_arm(arm
)),
686 token
::NtImplItem(arm
) =>
687 token
::NtImplItem(fld
.fold_impl_item(arm
)
688 .expect_one("expected fold to produce exactly one item")),
689 token
::NtTraitItem(arm
) =>
690 token
::NtTraitItem(fld
.fold_trait_item(arm
)
691 .expect_one("expected fold to produce exactly one item")),
692 token
::NtGenerics(generics
) => token
::NtGenerics(fld
.fold_generics(generics
)),
693 token
::NtWhereClause(where_clause
) =>
694 token
::NtWhereClause(fld
.fold_where_clause(where_clause
)),
698 pub fn noop_fold_fn_decl
<T
: Folder
>(decl
: P
<FnDecl
>, fld
: &mut T
) -> P
<FnDecl
> {
699 decl
.map(|FnDecl {inputs, output, variadic}
| FnDecl
{
700 inputs
: inputs
.move_map(|x
| fld
.fold_arg(x
)),
701 output
: match output
{
702 Return(ty
) => Return(fld
.fold_ty(ty
)),
703 DefaultReturn(span
) => DefaultReturn(span
),
704 NoReturn(span
) => NoReturn(span
)
710 pub fn noop_fold_ty_param_bound
<T
>(tpb
: TyParamBound
, fld
: &mut T
)
714 TraitTyParamBound(ty
, modifier
) => TraitTyParamBound(fld
.fold_poly_trait_ref(ty
), modifier
),
715 RegionTyParamBound(lifetime
) => RegionTyParamBound(fld
.fold_lifetime(lifetime
)),
719 pub fn noop_fold_ty_param
<T
: Folder
>(tp
: TyParam
, fld
: &mut T
) -> TyParam
{
720 let TyParam {id, ident, bounds, default, span}
= tp
;
724 bounds
: fld
.fold_bounds(bounds
),
725 default: default.map(|x
| fld
.fold_ty(x
)),
730 pub fn noop_fold_ty_params
<T
: Folder
>(tps
: OwnedSlice
<TyParam
>, fld
: &mut T
)
731 -> OwnedSlice
<TyParam
> {
732 tps
.move_map(|tp
| fld
.fold_ty_param(tp
))
735 pub fn noop_fold_lifetime
<T
: Folder
>(l
: Lifetime
, fld
: &mut T
) -> Lifetime
{
737 id
: fld
.new_id(l
.id
),
739 span
: fld
.new_span(l
.span
)
743 pub fn noop_fold_lifetime_def
<T
: Folder
>(l
: LifetimeDef
, fld
: &mut T
)
746 lifetime
: fld
.fold_lifetime(l
.lifetime
),
747 bounds
: fld
.fold_lifetimes(l
.bounds
),
751 pub fn noop_fold_lifetimes
<T
: Folder
>(lts
: Vec
<Lifetime
>, fld
: &mut T
) -> Vec
<Lifetime
> {
752 lts
.move_map(|l
| fld
.fold_lifetime(l
))
755 pub fn noop_fold_lifetime_defs
<T
: Folder
>(lts
: Vec
<LifetimeDef
>, fld
: &mut T
)
756 -> Vec
<LifetimeDef
> {
757 lts
.move_map(|l
| fld
.fold_lifetime_def(l
))
760 pub fn noop_fold_opt_lifetime
<T
: Folder
>(o_lt
: Option
<Lifetime
>, fld
: &mut T
)
761 -> Option
<Lifetime
> {
762 o_lt
.map(|lt
| fld
.fold_lifetime(lt
))
765 pub fn noop_fold_generics
<T
: Folder
>(Generics {ty_params, lifetimes, where_clause}
: Generics
,
766 fld
: &mut T
) -> Generics
{
768 ty_params
: fld
.fold_ty_params(ty_params
),
769 lifetimes
: fld
.fold_lifetime_defs(lifetimes
),
770 where_clause
: fld
.fold_where_clause(where_clause
),
774 pub fn noop_fold_where_clause
<T
: Folder
>(
775 WhereClause {id, predicates}
: WhereClause
,
780 predicates
: predicates
.move_map(|predicate
| {
781 fld
.fold_where_predicate(predicate
)
786 pub fn noop_fold_where_predicate
<T
: Folder
>(
787 pred
: WherePredicate
,
791 ast
::WherePredicate
::BoundPredicate(ast
::WhereBoundPredicate
{bound_lifetimes
,
795 ast
::WherePredicate
::BoundPredicate(ast
::WhereBoundPredicate
{
796 bound_lifetimes
: fld
.fold_lifetime_defs(bound_lifetimes
),
797 bounded_ty
: fld
.fold_ty(bounded_ty
),
798 bounds
: bounds
.move_map(|x
| fld
.fold_ty_param_bound(x
)),
799 span
: fld
.new_span(span
)
802 ast
::WherePredicate
::RegionPredicate(ast
::WhereRegionPredicate
{lifetime
,
805 ast
::WherePredicate
::RegionPredicate(ast
::WhereRegionPredicate
{
806 span
: fld
.new_span(span
),
807 lifetime
: fld
.fold_lifetime(lifetime
),
808 bounds
: bounds
.move_map(|bound
| fld
.fold_lifetime(bound
))
811 ast
::WherePredicate
::EqPredicate(ast
::WhereEqPredicate
{id
,
815 ast
::WherePredicate
::EqPredicate(ast
::WhereEqPredicate
{
817 path
: fld
.fold_path(path
),
819 span
: fld
.new_span(span
)
825 pub fn noop_fold_struct_def
<T
: Folder
>(struct_def
: P
<StructDef
>, fld
: &mut T
) -> P
<StructDef
> {
826 struct_def
.map(|StructDef { fields, ctor_id }
| StructDef
{
827 fields
: fields
.move_map(|f
| fld
.fold_struct_field(f
)),
828 ctor_id
: ctor_id
.map(|cid
| fld
.new_id(cid
)),
832 pub fn noop_fold_trait_ref
<T
: Folder
>(p
: TraitRef
, fld
: &mut T
) -> TraitRef
{
833 let id
= fld
.new_id(p
.ref_id
);
839 path
: fld
.fold_path(path
),
844 pub fn noop_fold_poly_trait_ref
<T
: Folder
>(p
: PolyTraitRef
, fld
: &mut T
) -> PolyTraitRef
{
846 bound_lifetimes
: fld
.fold_lifetime_defs(p
.bound_lifetimes
),
847 trait_ref
: fld
.fold_trait_ref(p
.trait_ref
),
848 span
: fld
.new_span(p
.span
),
852 pub fn noop_fold_struct_field
<T
: Folder
>(f
: StructField
, fld
: &mut T
) -> StructField
{
853 let StructField {node: StructField_ {id, kind, ty, attrs}
, span
} = f
;
859 attrs
: fold_attrs(attrs
, fld
),
861 span
: fld
.new_span(span
)
865 pub fn noop_fold_field
<T
: Folder
>(Field {ident, expr, span}
: Field
, folder
: &mut T
) -> Field
{
867 ident
: respan(ident
.span
, folder
.fold_ident(ident
.node
)),
868 expr
: folder
.fold_expr(expr
),
869 span
: folder
.new_span(span
)
873 pub fn noop_fold_mt
<T
: Folder
>(MutTy {ty, mutbl}
: MutTy
, folder
: &mut T
) -> MutTy
{
875 ty
: folder
.fold_ty(ty
),
880 pub fn noop_fold_opt_bounds
<T
: Folder
>(b
: Option
<OwnedSlice
<TyParamBound
>>, folder
: &mut T
)
881 -> Option
<OwnedSlice
<TyParamBound
>> {
882 b
.map(|bounds
| folder
.fold_bounds(bounds
))
885 fn noop_fold_bounds
<T
: Folder
>(bounds
: TyParamBounds
, folder
: &mut T
)
887 bounds
.move_map(|bound
| folder
.fold_ty_param_bound(bound
))
890 fn noop_fold_variant_arg
<T
: Folder
>(VariantArg {id, ty}
: VariantArg
, folder
: &mut T
)
893 id
: folder
.new_id(id
),
894 ty
: folder
.fold_ty(ty
)
898 pub fn noop_fold_block
<T
: Folder
>(b
: P
<Block
>, folder
: &mut T
) -> P
<Block
> {
899 b
.map(|Block {id, stmts, expr, rules, span}
| Block
{
900 id
: folder
.new_id(id
),
901 stmts
: stmts
.into_iter().flat_map(|s
| folder
.fold_stmt(s
).into_iter()).collect(),
902 expr
: expr
.map(|x
| folder
.fold_expr(x
)),
904 span
: folder
.new_span(span
),
908 pub fn noop_fold_item_underscore
<T
: Folder
>(i
: Item_
, folder
: &mut T
) -> Item_
{
910 ItemExternCrate(string
) => ItemExternCrate(string
),
911 ItemUse(view_path
) => {
912 ItemUse(folder
.fold_view_path(view_path
))
914 ItemStatic(t
, m
, e
) => {
915 ItemStatic(folder
.fold_ty(t
), m
, folder
.fold_expr(e
))
918 ItemConst(folder
.fold_ty(t
), folder
.fold_expr(e
))
920 ItemFn(decl
, unsafety
, abi
, generics
, body
) => {
922 folder
.fold_fn_decl(decl
),
925 folder
.fold_generics(generics
),
926 folder
.fold_block(body
)
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
))
934 ItemEnum(enum_definition
, generics
) => {
937 variants
: enum_definition
.variants
.move_map(|x
| folder
.fold_variant(x
)),
939 folder
.fold_generics(generics
))
941 ItemStruct(struct_def
, generics
) => {
942 let struct_def
= folder
.fold_struct_def(struct_def
);
943 ItemStruct(struct_def
, folder
.fold_generics(generics
))
945 ItemDefaultImpl(unsafety
, ref trait_ref
) => {
946 ItemDefaultImpl(unsafety
, folder
.fold_trait_ref((*trait_ref
).clone()))
948 ItemImpl(unsafety
, polarity
, generics
, ifce
, ty
, impl_items
) => {
949 let new_impl_items
= impl_items
.into_iter().flat_map(|item
| {
950 folder
.fold_impl_item(item
).into_iter()
952 let ifce
= match ifce
{
954 Some(ref trait_ref
) => {
955 Some(folder
.fold_trait_ref((*trait_ref
).clone()))
960 folder
.fold_generics(generics
),
965 ItemTrait(unsafety
, generics
, bounds
, items
) => {
966 let bounds
= folder
.fold_bounds(bounds
);
967 let items
= items
.into_iter().flat_map(|item
| {
968 folder
.fold_trait_item(item
).into_iter()
971 folder
.fold_generics(generics
),
975 ItemMac(m
) => ItemMac(folder
.fold_mac(m
)),
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
),
986 ConstTraitItem(ty
, default) => {
987 ConstTraitItem(folder
.fold_ty(ty
),
988 default.map(|x
| folder
.fold_expr(x
)))
990 MethodTraitItem(sig
, body
) => {
991 MethodTraitItem(noop_fold_method_sig(sig
, folder
),
992 body
.map(|x
| folder
.fold_block(x
)))
994 TypeTraitItem(bounds
, default) => {
995 TypeTraitItem(folder
.fold_bounds(bounds
),
996 default.map(|x
| folder
.fold_ty(x
)))
999 span
: folder
.new_span(span
)
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
),
1011 ConstImplItem(ty
, expr
) => {
1012 ConstImplItem(folder
.fold_ty(ty
), folder
.fold_expr(expr
))
1014 MethodImplItem(sig
, body
) => {
1015 MethodImplItem(noop_fold_method_sig(sig
, folder
),
1016 folder
.fold_block(body
))
1018 TypeImplItem(ty
) => TypeImplItem(folder
.fold_ty(ty
)),
1019 MacImplItem(mac
) => MacImplItem(folder
.fold_mac(mac
))
1021 span
: folder
.new_span(span
)
1025 pub fn noop_fold_mod
<T
: Folder
>(Mod {inner, items}
: Mod
, folder
: &mut T
) -> Mod
{
1027 inner
: folder
.new_span(inner
),
1028 items
: items
.into_iter().flat_map(|x
| folder
.fold_item(x
).into_iter()).collect(),
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
);
1036 let mut items
= folder
.fold_item(P(ast
::Item
{
1037 ident
: token
::special_idents
::invalid
,
1039 id
: ast
::DUMMY_NODE_ID
,
1042 node
: ast
::ItemMod(module
),
1045 let (module
, attrs
, span
) = match items
.next() {
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, .. }
| {
1051 ast
::ItemMod(m
) => (m
, attrs
, span
),
1052 _
=> panic
!("fold converted a module to not a module"),
1062 for def
in &mut exported_macros
{
1063 def
.id
= folder
.new_id(def
.id
);
1070 exported_macros
: exported_macros
,
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
)))
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
, _
) => {
1088 ast_util
::impl_pretty_name(maybe_trait
, Some(&**ty
))
1095 ident
: folder
.fold_ident(ident
),
1096 attrs
: fold_attrs(attrs
, folder
),
1099 span
: folder
.new_span(span
)
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
),
1107 attrs
: fold_attrs(attrs
, folder
),
1109 ForeignItemFn(fdec
, generics
) => {
1110 ForeignItemFn(folder
.fold_fn_decl(fdec
), folder
.fold_generics(generics
))
1112 ForeignItemStatic(t
, m
) => {
1113 ForeignItemStatic(folder
.fold_ty(t
), m
)
1117 span
: folder
.new_span(span
)
1121 pub fn noop_fold_method_sig
<T
: Folder
>(sig
: MethodSig
, folder
: &mut T
) -> MethodSig
{
1123 generics
: folder
.fold_generics(sig
.generics
),
1125 explicit_self
: folder
.fold_explicit_self(sig
.explicit_self
),
1126 unsafety
: sig
.unsafety
,
1127 decl
: folder
.fold_fn_decl(sig
.decl
)
1131 pub fn noop_fold_pat
<T
: Folder
>(p
: P
<Pat
>, folder
: &mut T
) -> P
<Pat
> {
1132 p
.map(|Pat {id, node, span}
| Pat
{
1133 id
: folder
.new_id(id
),
1135 PatWild(k
) => PatWild(k
),
1136 PatIdent(binding_mode
, pth1
, sub
) => {
1137 PatIdent(binding_mode
,
1138 Spanned
{span
: folder
.new_span(pth1
.span
),
1139 node
: folder
.fold_ident(pth1
.node
)},
1140 sub
.map(|x
| folder
.fold_pat(x
)))
1142 PatLit(e
) => PatLit(folder
.fold_expr(e
)),
1143 PatEnum(pth
, pats
) => {
1144 PatEnum(folder
.fold_path(pth
),
1145 pats
.map(|pats
| pats
.move_map(|x
| folder
.fold_pat(x
))))
1147 PatQPath(qself
, pth
) => {
1148 let qself
= QSelf {ty: folder.fold_ty(qself.ty), .. qself}
;
1149 PatQPath(qself
, folder
.fold_path(pth
))
1151 PatStruct(pth
, fields
, etc
) => {
1152 let pth
= folder
.fold_path(pth
);
1153 let fs
= fields
.move_map(|f
| {
1154 Spanned
{ span
: folder
.new_span(f
.span
),
1155 node
: ast
::FieldPat
{
1156 ident
: f
.node
.ident
,
1157 pat
: folder
.fold_pat(f
.node
.pat
),
1158 is_shorthand
: f
.node
.is_shorthand
,
1161 PatStruct(pth
, fs
, etc
)
1163 PatTup(elts
) => PatTup(elts
.move_map(|x
| folder
.fold_pat(x
))),
1164 PatBox(inner
) => PatBox(folder
.fold_pat(inner
)),
1165 PatRegion(inner
, mutbl
) => PatRegion(folder
.fold_pat(inner
), mutbl
),
1166 PatRange(e1
, e2
) => {
1167 PatRange(folder
.fold_expr(e1
), folder
.fold_expr(e2
))
1169 PatVec(before
, slice
, after
) => {
1170 PatVec(before
.move_map(|x
| folder
.fold_pat(x
)),
1171 slice
.map(|x
| folder
.fold_pat(x
)),
1172 after
.move_map(|x
| folder
.fold_pat(x
)))
1174 PatMac(mac
) => PatMac(folder
.fold_mac(mac
))
1176 span
: folder
.new_span(span
)
1180 pub fn noop_fold_expr
<T
: Folder
>(Expr {id, node, span}
: Expr
, folder
: &mut T
) -> Expr
{
1182 id
: folder
.new_id(id
),
1185 ExprBox(p
.map(|e
|folder
.fold_expr(e
)), folder
.fold_expr(e
))
1188 ExprVec(exprs
.move_map(|x
| folder
.fold_expr(x
)))
1190 ExprRepeat(expr
, count
) => {
1191 ExprRepeat(folder
.fold_expr(expr
), folder
.fold_expr(count
))
1193 ExprTup(elts
) => ExprTup(elts
.move_map(|x
| folder
.fold_expr(x
))),
1194 ExprCall(f
, args
) => {
1195 ExprCall(folder
.fold_expr(f
),
1196 args
.move_map(|x
| folder
.fold_expr(x
)))
1198 ExprMethodCall(i
, tps
, args
) => {
1200 respan(folder
.new_span(i
.span
), folder
.fold_ident(i
.node
)),
1201 tps
.move_map(|x
| folder
.fold_ty(x
)),
1202 args
.move_map(|x
| folder
.fold_expr(x
)))
1204 ExprBinary(binop
, lhs
, rhs
) => {
1206 folder
.fold_expr(lhs
),
1207 folder
.fold_expr(rhs
))
1209 ExprUnary(binop
, ohs
) => {
1210 ExprUnary(binop
, folder
.fold_expr(ohs
))
1212 ExprLit(l
) => ExprLit(l
),
1213 ExprCast(expr
, ty
) => {
1214 ExprCast(folder
.fold_expr(expr
), folder
.fold_ty(ty
))
1216 ExprAddrOf(m
, ohs
) => ExprAddrOf(m
, folder
.fold_expr(ohs
)),
1217 ExprIf(cond
, tr
, fl
) => {
1218 ExprIf(folder
.fold_expr(cond
),
1219 folder
.fold_block(tr
),
1220 fl
.map(|x
| folder
.fold_expr(x
)))
1222 ExprIfLet(pat
, expr
, tr
, fl
) => {
1223 ExprIfLet(folder
.fold_pat(pat
),
1224 folder
.fold_expr(expr
),
1225 folder
.fold_block(tr
),
1226 fl
.map(|x
| folder
.fold_expr(x
)))
1228 ExprWhile(cond
, body
, opt_ident
) => {
1229 ExprWhile(folder
.fold_expr(cond
),
1230 folder
.fold_block(body
),
1231 opt_ident
.map(|i
| folder
.fold_ident(i
)))
1233 ExprWhileLet(pat
, expr
, body
, opt_ident
) => {
1234 ExprWhileLet(folder
.fold_pat(pat
),
1235 folder
.fold_expr(expr
),
1236 folder
.fold_block(body
),
1237 opt_ident
.map(|i
| folder
.fold_ident(i
)))
1239 ExprForLoop(pat
, iter
, body
, opt_ident
) => {
1240 ExprForLoop(folder
.fold_pat(pat
),
1241 folder
.fold_expr(iter
),
1242 folder
.fold_block(body
),
1243 opt_ident
.map(|i
| folder
.fold_ident(i
)))
1245 ExprLoop(body
, opt_ident
) => {
1246 ExprLoop(folder
.fold_block(body
),
1247 opt_ident
.map(|i
| folder
.fold_ident(i
)))
1249 ExprMatch(expr
, arms
, source
) => {
1250 ExprMatch(folder
.fold_expr(expr
),
1251 arms
.move_map(|x
| folder
.fold_arm(x
)),
1254 ExprClosure(capture_clause
, decl
, body
) => {
1255 ExprClosure(capture_clause
,
1256 folder
.fold_fn_decl(decl
),
1257 folder
.fold_block(body
))
1259 ExprBlock(blk
) => ExprBlock(folder
.fold_block(blk
)),
1260 ExprAssign(el
, er
) => {
1261 ExprAssign(folder
.fold_expr(el
), folder
.fold_expr(er
))
1263 ExprAssignOp(op
, el
, er
) => {
1265 folder
.fold_expr(el
),
1266 folder
.fold_expr(er
))
1268 ExprField(el
, ident
) => {
1269 ExprField(folder
.fold_expr(el
),
1270 respan(folder
.new_span(ident
.span
),
1271 folder
.fold_ident(ident
.node
)))
1273 ExprTupField(el
, ident
) => {
1274 ExprTupField(folder
.fold_expr(el
),
1275 respan(folder
.new_span(ident
.span
),
1276 folder
.fold_usize(ident
.node
)))
1278 ExprIndex(el
, er
) => {
1279 ExprIndex(folder
.fold_expr(el
), folder
.fold_expr(er
))
1281 ExprRange(e1
, e2
) => {
1282 ExprRange(e1
.map(|x
| folder
.fold_expr(x
)),
1283 e2
.map(|x
| folder
.fold_expr(x
)))
1285 ExprPath(qself
, path
) => {
1286 let qself
= qself
.map(|QSelf { ty, position }
| {
1288 ty
: folder
.fold_ty(ty
),
1292 ExprPath(qself
, folder
.fold_path(path
))
1294 ExprBreak(opt_ident
) => ExprBreak(opt_ident
.map(|x
| folder
.fold_ident(x
))),
1295 ExprAgain(opt_ident
) => ExprAgain(opt_ident
.map(|x
| folder
.fold_ident(x
))),
1296 ExprRet(e
) => ExprRet(e
.map(|x
| folder
.fold_expr(x
))),
1297 ExprInlineAsm(InlineAsm
{
1307 }) => ExprInlineAsm(InlineAsm
{
1308 inputs
: inputs
.move_map(|(c
, input
)| {
1309 (c
, folder
.fold_expr(input
))
1311 outputs
: outputs
.move_map(|(c
, out
, is_rw
)| {
1312 (c
, folder
.fold_expr(out
), is_rw
)
1315 asm_str_style
: asm_str_style
,
1318 alignstack
: alignstack
,
1322 ExprMac(mac
) => ExprMac(folder
.fold_mac(mac
)),
1323 ExprStruct(path
, fields
, maybe_expr
) => {
1324 ExprStruct(folder
.fold_path(path
),
1325 fields
.move_map(|x
| folder
.fold_field(x
)),
1326 maybe_expr
.map(|x
| folder
.fold_expr(x
)))
1328 ExprParen(ex
) => ExprParen(folder
.fold_expr(ex
))
1330 span
: folder
.new_span(span
)
1334 pub fn noop_fold_stmt
<T
: Folder
>(Spanned {node, span}
: Stmt
, folder
: &mut T
)
1335 -> SmallVector
<P
<Stmt
>> {
1336 let span
= folder
.new_span(span
);
1338 StmtDecl(d
, id
) => {
1339 let id
= folder
.new_id(id
);
1340 folder
.fold_decl(d
).into_iter().map(|d
| P(Spanned
{
1341 node
: StmtDecl(d
, id
),
1345 StmtExpr(e
, id
) => {
1346 let id
= folder
.new_id(id
);
1347 SmallVector
::one(P(Spanned
{
1348 node
: StmtExpr(folder
.fold_expr(e
), id
),
1352 StmtSemi(e
, id
) => {
1353 let id
= folder
.new_id(id
);
1354 SmallVector
::one(P(Spanned
{
1355 node
: StmtSemi(folder
.fold_expr(e
), id
),
1359 StmtMac(mac
, semi
) => SmallVector
::one(P(Spanned
{
1360 node
: StmtMac(mac
.map(|m
| folder
.fold_mac(m
)), semi
),
1370 use util
::parser_testing
::{string_to_crate, matches_codepattern}
;
1376 // this version doesn't care about getting comments or docstrings in.
1377 fn fake_print_crate(s
: &mut pprust
::State
,
1378 krate
: &ast
::Crate
) -> io
::Result
<()> {
1379 s
.print_mod(&krate
.module
, &krate
.attrs
)
1382 // change every identifier to "zz"
1383 struct ToZzIdentFolder
;
1385 impl Folder
for ToZzIdentFolder
{
1386 fn fold_ident(&mut self, _
: ast
::Ident
) -> ast
::Ident
{
1387 token
::str_to_ident("zz")
1389 fn fold_mac(&mut self, mac
: ast
::Mac
) -> ast
::Mac
{
1390 fold
::noop_fold_mac(mac
, self)
1394 // maybe add to expand.rs...
1395 macro_rules
! assert_pred
{
1396 ($pred
:expr
, $predname
:expr
, $a
:expr
, $b
:expr
) => (
1398 let pred_val
= $pred
;
1401 if !(pred_val(&a_val
, &b_val
)) {
1402 panic
!("expected args satisfying {}, got {} and {}",
1403 $predname
, a_val
, b_val
);
1409 // make sure idents get transformed everywhere
1410 #[test] fn ident_transformation () {
1411 let mut zz_fold
= ToZzIdentFolder
;
1412 let ast
= string_to_crate(
1413 "#[a] mod b {fn c (d : e, f : g) {h!(i,j,k);l;m}}".to_string());
1414 let folded_crate
= zz_fold
.fold_crate(ast
);
1416 matches_codepattern
,
1417 "matches_codepattern",
1418 pprust
::to_string(|s
| fake_print_crate(s
, &folded_crate
)),
1419 "#[a]mod zz{fn zz(zz:zz,zz:zz){zz!(zz,zz,zz);zz;zz}}".to_string());
1422 // even inside macro defs....
1423 #[test] fn ident_transformation_in_defs () {
1424 let mut zz_fold
= ToZzIdentFolder
;
1425 let ast
= string_to_crate(
1426 "macro_rules! a {(b $c:expr $(d $e:token)f+ => \
1427 (g $(d $d $e)+))} ".to_string());
1428 let folded_crate
= zz_fold
.fold_crate(ast
);
1430 matches_codepattern
,
1431 "matches_codepattern",
1432 pprust
::to_string(|s
| fake_print_crate(s
, &folded_crate
)),
1433 "zz!zz((zz$zz:zz$(zz $zz:zz)zz+=>(zz$(zz$zz$zz)+)));".to_string());