blob: 2f4ef1cc4b7ab899c587e72a8f4714ea49ab0134 [file] [log] [blame]
gnzlbg9ae88d82017-01-26 20:45:17 +01001// Adapted from libsyntax.
2
3//! A Folder represents an AST->AST fold; it accepts an AST piece,
David Tolnay70925b72017-01-29 11:20:54 -08004//! and returns a piece of the same type.
gnzlbg9ae88d82017-01-26 20:45:17 +01005
6use super::*;
David Tolnay02a8d472017-02-19 12:59:44 -08007#[cfg(not(feature = "full"))]
David Tolnay7a1cda02017-01-29 11:50:31 -08008use constant;
gnzlbg9ae88d82017-01-26 20:45:17 +01009
Alex Crichtonccbb45d2017-05-23 10:58:24 -070010use delimited::{Delimited, Element};
11
gnzlbg9ae88d82017-01-26 20:45:17 +010012/// AST->AST fold.
13///
14/// Each method of the Folder trait is a hook to be potentially overridden. Each
15/// method's default implementation recursively visits the substructure of the
16/// input via the `noop_fold` methods, which perform an "identity fold", that
17/// is, they return the same structure that they are given (for example the
18/// `fold_crate` method by default calls `fold::noop_fold_crate`).
19///
20/// If you want to ensure that your code handles every variant explicitly, you
21/// need to override each method and monitor future changes to `Folder` in case
22/// a new method with a new default implementation gets introduced.
David Tolnay78488252017-01-29 11:22:59 -080023pub trait Folder {
gnzlbg9ae88d82017-01-26 20:45:17 +010024 // Any additions to this trait should happen in form
25 // of a call to a public `noop_*` function that only calls
26 // out to the folder again, not other `noop_*` functions.
27 //
28 // This is a necessary API workaround to the problem of not
29 // being able to call out to the super default method
30 // in an overridden default method.
31
32 fn fold_ident(&mut self, _ident: Ident) -> Ident {
33 noop_fold_ident(self, _ident)
34 }
35 fn fold_derive_input(&mut self, derive_input: DeriveInput) -> DeriveInput {
36 noop_fold_derive_input(self, derive_input)
37 }
38 fn fold_ty(&mut self, ty: Ty) -> Ty {
39 noop_fold_ty(self, ty)
40 }
41 fn fold_generics(&mut self, generics: Generics) -> Generics {
42 noop_fold_generics(self, generics)
43 }
44 fn fold_ty_param_bound(&mut self, bound: TyParamBound) -> TyParamBound {
45 noop_fold_ty_param_bound(self, bound)
46 }
47 fn fold_poly_trait_ref(&mut self, trait_ref: PolyTraitRef) -> PolyTraitRef {
48 noop_fold_poly_trait_ref(self, trait_ref)
49 }
50 fn fold_variant_data(&mut self, data: VariantData) -> VariantData {
51 noop_fold_variant_data(self, data)
52 }
53 fn fold_field(&mut self, field: Field) -> Field {
54 noop_fold_field(self, field)
55 }
56 fn fold_variant(&mut self, variant: Variant) -> Variant {
57 noop_fold_variant(self, variant)
58 }
59 fn fold_lifetime(&mut self, _lifetime: Lifetime) -> Lifetime {
60 noop_fold_lifetime(self, _lifetime)
61 }
62 fn fold_lifetime_def(&mut self, lifetime: LifetimeDef) -> LifetimeDef {
63 noop_fold_lifetime_def(self, lifetime)
64 }
65 fn fold_path(&mut self, path: Path) -> Path {
66 noop_fold_path(self, path)
67 }
68 fn fold_path_segment(&mut self, path_segment: PathSegment) -> PathSegment {
69 noop_fold_path_segment(self, path_segment)
70 }
71 fn fold_path_parameters(&mut self, path_parameters: PathParameters) -> PathParameters {
72 noop_fold_path_parameters(self, path_parameters)
73 }
74 fn fold_assoc_type_binding(&mut self, type_binding: TypeBinding) -> TypeBinding {
75 noop_fold_assoc_type_binding(self, type_binding)
76 }
77 fn fold_attribute(&mut self, _attr: Attribute) -> Attribute {
78 noop_fold_attribute(self, _attr)
79 }
80 fn fold_fn_ret_ty(&mut self, ret_ty: FunctionRetTy) -> FunctionRetTy {
81 noop_fold_fn_ret_ty(self, ret_ty)
82 }
83 fn fold_const_expr(&mut self, expr: ConstExpr) -> ConstExpr {
84 noop_fold_const_expr(self, expr)
85 }
86 fn fold_lit(&mut self, _lit: Lit) -> Lit {
87 noop_fold_lit(self, _lit)
88 }
89
90 fn fold_mac(&mut self, mac: Mac) -> Mac {
91 noop_fold_mac(self, mac)
92 }
93
94 #[cfg(feature = "full")]
95 fn fold_crate(&mut self, _crate: Crate) -> Crate {
96 noop_fold_crate(self, _crate)
97 }
98 #[cfg(feature = "full")]
99 fn fold_item(&mut self, item: Item) -> Item {
100 noop_fold_item(self, item)
101 }
102 #[cfg(feature = "full")]
103 fn fold_expr(&mut self, expr: Expr) -> Expr {
104 noop_fold_expr(self, expr)
105 }
106 #[cfg(feature = "full")]
107 fn fold_foreign_item(&mut self, foreign_item: ForeignItem) -> ForeignItem {
108 noop_fold_foreign_item(self, foreign_item)
109 }
110 #[cfg(feature = "full")]
111 fn fold_pat(&mut self, pat: Pat) -> Pat {
112 noop_fold_pat(self, pat)
113 }
114 #[cfg(feature = "full")]
115 fn fold_fn_decl(&mut self, fn_decl: FnDecl) -> FnDecl {
116 noop_fold_fn_decl(self, fn_decl)
117 }
118 #[cfg(feature = "full")]
119 fn fold_trait_item(&mut self, trait_item: TraitItem) -> TraitItem {
120 noop_fold_trait_item(self, trait_item)
121 }
122 #[cfg(feature = "full")]
123 fn fold_impl_item(&mut self, impl_item: ImplItem) -> ImplItem {
124 noop_fold_impl_item(self, impl_item)
125 }
126 #[cfg(feature = "full")]
127 fn fold_method_sig(&mut self, method_sig: MethodSig) -> MethodSig {
128 noop_fold_method_sig(self, method_sig)
129 }
130 #[cfg(feature = "full")]
131 fn fold_stmt(&mut self, stmt: Stmt) -> Stmt {
132 noop_fold_stmt(self, stmt)
133 }
134 #[cfg(feature = "full")]
135 fn fold_block(&mut self, block: Block) -> Block {
136 noop_fold_block(self, block)
137 }
138 #[cfg(feature = "full")]
139 fn fold_local(&mut self, local: Local) -> Local {
140 noop_fold_local(self, local)
141 }
142 #[cfg(feature = "full")]
143 fn fold_view_path(&mut self, view_path: ViewPath) -> ViewPath {
144 noop_fold_view_path(self, view_path)
145 }
146}
147
148trait LiftOnce<T, U> {
149 type Output;
150 fn lift<F>(self, f: F) -> Self::Output where F: FnOnce(T) -> U;
151}
152
153impl<T, U> LiftOnce<T, U> for Box<T> {
154 type Output = Box<U>;
David Tolnay02a8d472017-02-19 12:59:44 -0800155 // Clippy false positive
156 // https://github.com/Manishearth/rust-clippy/issues/1478
157 #[cfg_attr(feature = "cargo-clippy", allow(boxed_local))]
gnzlbg9ae88d82017-01-26 20:45:17 +0100158 fn lift<F>(self, f: F) -> Box<U>
159 where F: FnOnce(T) -> U
160 {
161 Box::new(f(*self))
162 }
163}
164
165trait LiftMut<T, U> {
166 type Output;
167 fn lift<F>(self, f: F) -> Self::Output where F: FnMut(T) -> U;
168}
169
170impl<T, U> LiftMut<T, U> for Vec<T> {
171 type Output = Vec<U>;
172 fn lift<F>(self, f: F) -> Vec<U>
173 where F: FnMut(T) -> U
174 {
175 self.into_iter().map(f).collect()
176 }
177}
178
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700179impl<T, D, U> LiftMut<T, U> for Delimited<T, D> {
180 type Output = Delimited<U, D>;
181 fn lift<F>(self, mut f: F) -> Self::Output
182 where F: FnMut(T) -> U
183 {
184 self.into_iter().map(|e| {
185 match e {
186 Element::Delimited(t, d) => Element::Delimited(f(t), d),
187 Element::End(t) => Element::End(f(t))
188 }
189 }).collect()
190 }
191}
192
David Tolnay78488252017-01-29 11:22:59 -0800193pub fn noop_fold_ident<F: ?Sized + Folder>(_: &mut F, _ident: Ident) -> Ident {
gnzlbg9ae88d82017-01-26 20:45:17 +0100194 _ident
195}
196
David Tolnay78488252017-01-29 11:22:59 -0800197pub fn noop_fold_derive_input<F: ?Sized + Folder>(folder: &mut F,
gnzlbg9ae88d82017-01-26 20:45:17 +0100198 DeriveInput{ ident,
199 vis,
200 attrs,
201 generics,
David Tolnay05120ef2017-03-12 18:29:26 -0700202body }: DeriveInput) -> DeriveInput{
David Tolnay7a1cda02017-01-29 11:50:31 -0800203 use Body::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100204 DeriveInput {
205 ident: folder.fold_ident(ident),
206 vis: noop_fold_vis(folder, vis),
207 attrs: attrs.lift(|a| folder.fold_attribute(a)),
208 generics: folder.fold_generics(generics),
209 body: match body {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700210 Enum(data) => {
211 Enum(BodyEnum {
212 variants: data.variants.lift(move |v| folder.fold_variant(v)),
213 ..data
214 })
215 }
216 Struct(data) => {
217 Struct(BodyStruct {
218 data: folder.fold_variant_data(data.data),
219 ..data
220 })
221 }
gnzlbg9ae88d82017-01-26 20:45:17 +0100222 },
223 }
224}
225
David Tolnay78488252017-01-29 11:22:59 -0800226pub fn noop_fold_ty<F: ?Sized + Folder>(folder: &mut F, ty: Ty) -> Ty {
Alex Crichton62a0a592017-05-22 13:58:53 -0700227 use ty::*;
David Tolnay7a1cda02017-01-29 11:50:31 -0800228 use Ty::*;
Alex Crichton62a0a592017-05-22 13:58:53 -0700229
gnzlbg9ae88d82017-01-26 20:45:17 +0100230 match ty {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700231 Slice(t) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700232 Slice(TySlice {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700233 ty: t.ty.lift(|v| folder.fold_ty(v)),
234 ..t
Alex Crichton62a0a592017-05-22 13:58:53 -0700235 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100236 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700237 Paren(t) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700238 Paren(TyParen {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700239 ty: t.ty.lift(|v| folder.fold_ty(v)),
240 ..t
Alex Crichton62a0a592017-05-22 13:58:53 -0700241 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100242 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700243 Ptr(t) => {
244 let ty = *t.ty;
Alex Crichton62a0a592017-05-22 13:58:53 -0700245 let MutTy { ty, mutability } = ty;
246 Ptr(TyPtr {
247 ty: Box::new(MutTy {
248 ty: folder.fold_ty(ty),
249 mutability: mutability,
250 }),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700251 ..t
Alex Crichton62a0a592017-05-22 13:58:53 -0700252 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100253 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700254 Rptr(t) => {
255 let ty = *t.ty;
Alex Crichton62a0a592017-05-22 13:58:53 -0700256 let MutTy { ty, mutability } = ty;
257 Rptr(TyRptr {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700258 lifetime: t.lifetime.map(|l| folder.fold_lifetime(l)),
Alex Crichton62a0a592017-05-22 13:58:53 -0700259 ty: Box::new(MutTy {
260 ty: folder.fold_ty(ty),
261 mutability: mutability,
262 }),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700263 ..t
Alex Crichton62a0a592017-05-22 13:58:53 -0700264 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100265 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700266 Never(t) => Never(t),
267 Infer(t) => Infer(t),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700268 Tup(t) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700269 Tup(TyTup {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700270 tys: t.tys.lift(|x| folder.fold_ty(x)),
271 ..t
Alex Crichton62a0a592017-05-22 13:58:53 -0700272 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100273 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700274 BareFn(t) => {
275 let ty = *t.ty;
Alex Crichton62a0a592017-05-22 13:58:53 -0700276 BareFn(TyBareFn {
277 ty: Box::new(BareFnTy {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700278 lifetimes: ty.lifetimes.map(|l| {
279 noop_fold_bound_lifetimes(folder, l)
280 }),
281 inputs: ty.inputs.lift(|v| {
Alex Crichton62a0a592017-05-22 13:58:53 -0700282 BareFnArg {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700283 name: v.name.map(|n| (folder.fold_ident(n.0), n.1)),
Alex Crichton62a0a592017-05-22 13:58:53 -0700284 ty: folder.fold_ty(v.ty),
285 }
286 }),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700287 output: folder.fold_fn_ret_ty(ty.output),
288 ..ty
Alex Crichton62a0a592017-05-22 13:58:53 -0700289 }),
290 })
291 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700292 Path(t) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700293 Path(TyPath {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700294 qself: t.qself.map(|v| noop_fold_qself(folder, v)),
295 path: folder.fold_path(t.path),
Alex Crichton62a0a592017-05-22 13:58:53 -0700296 })
297 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700298 Array(t) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700299 Array(TyArray {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700300 ty: t.ty.lift(|v| folder.fold_ty(v)),
301 amt: folder.fold_const_expr(t.amt),
302 ..t
Alex Crichton62a0a592017-05-22 13:58:53 -0700303 })
304 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700305 TraitObject(t) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700306 TraitObject(TyTraitObject {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700307 bounds: t.bounds.lift(|v| folder.fold_ty_param_bound(v)),
Alex Crichton62a0a592017-05-22 13:58:53 -0700308 })
309 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700310 ImplTrait(t) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700311 ImplTrait(TyImplTrait {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700312 bounds: t.bounds.lift(|v| folder.fold_ty_param_bound(v)),
313 ..t
Alex Crichton62a0a592017-05-22 13:58:53 -0700314 })
315 }
gnzlbg9ae88d82017-01-26 20:45:17 +0100316 Mac(mac) => Mac(folder.fold_mac(mac)),
317 }
318}
319
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700320#[cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))] // clippy lies
321fn noop_fold_qself<F: ?Sized + Folder>(folder: &mut F, qself: QSelf) -> QSelf {
gnzlbg9ae88d82017-01-26 20:45:17 +0100322 QSelf {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700323 ty: Box::new(folder.fold_ty(*(qself.ty))),
324 ..qself
gnzlbg9ae88d82017-01-26 20:45:17 +0100325 }
326}
327
David Tolnay78488252017-01-29 11:22:59 -0800328pub fn noop_fold_generics<F: ?Sized + Folder>(folder: &mut F,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700329 generics: Generics)
David Tolnay05120ef2017-03-12 18:29:26 -0700330-> Generics{
David Tolnay7a1cda02017-01-29 11:50:31 -0800331 use WherePredicate::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100332 Generics {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700333 lifetimes: generics.lifetimes.lift(|l| folder.fold_lifetime_def(l)),
334 ty_params: generics.ty_params.lift(|ty| {
gnzlbg9ae88d82017-01-26 20:45:17 +0100335 TyParam {
336 attrs: ty.attrs.lift(|a| folder.fold_attribute(a)),
337 ident: folder.fold_ident(ty.ident),
David Tolnay05120ef2017-03-12 18:29:26 -0700338 bounds: ty.bounds.lift(|ty_pb| folder.fold_ty_param_bound(ty_pb)),
gnzlbg9ae88d82017-01-26 20:45:17 +0100339 default: ty.default.map(|v| folder.fold_ty(v)),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700340 ..ty
gnzlbg9ae88d82017-01-26 20:45:17 +0100341 }
342 }),
343 where_clause: WhereClause {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700344 predicates: generics.where_clause.predicates.lift(|p| {
345 match p {
346 BoundPredicate(bound_predicate) => {
gnzlbg9ae88d82017-01-26 20:45:17 +0100347 BoundPredicate(WhereBoundPredicate {
348 bound_lifetimes: bound_predicate.bound_lifetimes
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700349 .map(|l| noop_fold_bound_lifetimes(folder, l)),
gnzlbg9ae88d82017-01-26 20:45:17 +0100350 bounded_ty: folder.fold_ty(bound_predicate.bounded_ty),
351 bounds: bound_predicate.bounds
352 .lift(|ty_pb| folder.fold_ty_param_bound(ty_pb)),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700353 ..bound_predicate
gnzlbg9ae88d82017-01-26 20:45:17 +0100354 })
355 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700356 RegionPredicate(region_predicate) => {
gnzlbg9ae88d82017-01-26 20:45:17 +0100357 RegionPredicate(WhereRegionPredicate {
358 lifetime: folder.fold_lifetime(region_predicate.lifetime),
359 bounds: region_predicate.bounds
360 .lift(|b| folder.fold_lifetime(b)),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700361 ..region_predicate
gnzlbg9ae88d82017-01-26 20:45:17 +0100362 })
363 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700364 EqPredicate(eq_predicate) => {
gnzlbg9ae88d82017-01-26 20:45:17 +0100365 EqPredicate(WhereEqPredicate {
366 lhs_ty: folder.fold_ty(eq_predicate.lhs_ty),
367 rhs_ty: folder.fold_ty(eq_predicate.rhs_ty),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700368 ..eq_predicate
gnzlbg9ae88d82017-01-26 20:45:17 +0100369 })
370 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700371 }
372 }),
373 ..generics.where_clause
gnzlbg9ae88d82017-01-26 20:45:17 +0100374 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700375 ..generics
gnzlbg9ae88d82017-01-26 20:45:17 +0100376 }
377}
378
David Tolnay660b03e2017-01-29 11:26:30 -0800379pub fn noop_fold_ty_param_bound<F: ?Sized + Folder>(folder: &mut F,
380 bound: TyParamBound)
381 -> TyParamBound {
David Tolnay7a1cda02017-01-29 11:50:31 -0800382 use TyParamBound::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100383 match bound {
384 Trait(ty, modifier) => Trait(folder.fold_poly_trait_ref(ty), modifier),
385 Region(lifetime) => Region(folder.fold_lifetime(lifetime)),
386 }
387}
388
David Tolnay660b03e2017-01-29 11:26:30 -0800389pub fn noop_fold_poly_trait_ref<F: ?Sized + Folder>(folder: &mut F,
390 trait_ref: PolyTraitRef)
391 -> PolyTraitRef {
gnzlbg9ae88d82017-01-26 20:45:17 +0100392 PolyTraitRef {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700393 bound_lifetimes: trait_ref.bound_lifetimes.map(|bl| {
394 noop_fold_bound_lifetimes(folder, bl)
395 }),
gnzlbg9ae88d82017-01-26 20:45:17 +0100396 trait_ref: folder.fold_path(trait_ref.trait_ref),
397 }
398}
399
David Tolnay78488252017-01-29 11:22:59 -0800400pub fn noop_fold_variant_data<F: ?Sized + Folder>(folder: &mut F,
David Tolnay660b03e2017-01-29 11:26:30 -0800401 data: VariantData)
402 -> VariantData {
David Tolnay7a1cda02017-01-29 11:50:31 -0800403 use VariantData::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100404 match data {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700405 Struct(fields, t) => Struct(fields.lift(|f| folder.fold_field(f)), t),
406 Tuple(fields, t) => Tuple(fields.lift(|f| folder.fold_field(f)), t),
gnzlbg9ae88d82017-01-26 20:45:17 +0100407 Unit => Unit,
408 }
409}
410
David Tolnay78488252017-01-29 11:22:59 -0800411pub fn noop_fold_field<F: ?Sized + Folder>(folder: &mut F, field: Field) -> Field {
gnzlbg9ae88d82017-01-26 20:45:17 +0100412 Field {
413 ident: field.ident.map(|i| folder.fold_ident(i)),
414 vis: noop_fold_vis(folder, field.vis),
415 attrs: field.attrs.lift(|a| folder.fold_attribute(a)),
416 ty: folder.fold_ty(field.ty),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700417 ..field
gnzlbg9ae88d82017-01-26 20:45:17 +0100418 }
419}
420
David Tolnay78488252017-01-29 11:22:59 -0800421pub fn noop_fold_variant<F: ?Sized + Folder>(folder: &mut F,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700422 variant: Variant)
423 -> Variant
424{
gnzlbg9ae88d82017-01-26 20:45:17 +0100425 Variant {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700426 ident: folder.fold_ident(variant.ident),
427 attrs: variant.attrs.lift(|v| folder.fold_attribute(v)),
428 data: folder.fold_variant_data(variant.data),
429 discriminant: variant.discriminant.map(|ce| folder.fold_const_expr(ce)),
430 ..variant
gnzlbg9ae88d82017-01-26 20:45:17 +0100431 }
432}
433
David Tolnay78488252017-01-29 11:22:59 -0800434pub fn noop_fold_lifetime<F: ?Sized + Folder>(folder: &mut F, _lifetime: Lifetime) -> Lifetime {
gnzlbg9ae88d82017-01-26 20:45:17 +0100435 Lifetime { ident: folder.fold_ident(_lifetime.ident) }
436}
437
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700438pub fn noop_fold_bound_lifetimes<F: ?Sized + Folder>(folder: &mut F,
439 b: BoundLifetimes)
440 -> BoundLifetimes
441{
442 BoundLifetimes {
443 lifetimes: b.lifetimes.lift(|l| folder.fold_lifetime_def(l)),
444 ..b
gnzlbg9ae88d82017-01-26 20:45:17 +0100445 }
446}
447
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700448pub fn noop_fold_lifetime_def<F: ?Sized + Folder>(folder: &mut F,
449 def: LifetimeDef)
450 -> LifetimeDef
451{
452 LifetimeDef {
453 attrs: def.attrs.lift(|x| folder.fold_attribute(x)),
454 lifetime: folder.fold_lifetime(def.lifetime),
455 bounds: def.bounds.lift(|l| folder.fold_lifetime(l)),
456 ..def
457 }
458}
459
460pub fn noop_fold_path<F: ?Sized + Folder>(folder: &mut F, path: Path) -> Path {
gnzlbg9ae88d82017-01-26 20:45:17 +0100461 Path {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700462 segments: path.segments.lift(|s| folder.fold_path_segment(s)),
463 ..path
gnzlbg9ae88d82017-01-26 20:45:17 +0100464 }
465}
466
David Tolnay78488252017-01-29 11:22:59 -0800467pub fn noop_fold_path_segment<F: ?Sized + Folder>(folder: &mut F,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700468 seg: PathSegment)
David Tolnay660b03e2017-01-29 11:26:30 -0800469 -> PathSegment {
gnzlbg9ae88d82017-01-26 20:45:17 +0100470 PathSegment {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700471 ident: folder.fold_ident(seg.ident),
472 parameters: folder.fold_path_parameters(seg.parameters),
gnzlbg9ae88d82017-01-26 20:45:17 +0100473 }
474}
475
David Tolnay78488252017-01-29 11:22:59 -0800476pub fn noop_fold_path_parameters<F: ?Sized + Folder>(folder: &mut F,
David Tolnay660b03e2017-01-29 11:26:30 -0800477 path_parameters: PathParameters)
478 -> PathParameters {
David Tolnay7a1cda02017-01-29 11:50:31 -0800479 use PathParameters::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100480 match path_parameters {
481 AngleBracketed(d) => {
gnzlbg9ae88d82017-01-26 20:45:17 +0100482 AngleBracketed(AngleBracketedParameterData {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700483 lifetimes: d.lifetimes.lift(|l| folder.fold_lifetime(l)),
484 types: d.types.lift(|ty| folder.fold_ty(ty)),
485 bindings: d.bindings.lift(|tb| folder.fold_assoc_type_binding(tb)),
486 ..d
487 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100488 }
489 Parenthesized(d) => {
gnzlbg9ae88d82017-01-26 20:45:17 +0100490 Parenthesized(ParenthesizedParameterData {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700491 inputs: d.inputs.lift(|i| folder.fold_ty(i)),
492 output: folder.fold_fn_ret_ty(d.output),
493 ..d
494 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100495 }
496 }
497}
498
David Tolnay78488252017-01-29 11:22:59 -0800499pub fn noop_fold_assoc_type_binding<F: ?Sized + Folder>(folder: &mut F,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700500 binding: TypeBinding)
501 -> TypeBinding
502{
gnzlbg9ae88d82017-01-26 20:45:17 +0100503 TypeBinding {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700504 ident: folder.fold_ident(binding.ident),
505 ty: folder.fold_ty(binding.ty),
506 ..binding
gnzlbg9ae88d82017-01-26 20:45:17 +0100507 }
gnzlbg9ae88d82017-01-26 20:45:17 +0100508}
509
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700510pub fn noop_fold_attribute<F: ?Sized + Folder>(_: &mut F, attr: Attribute) -> Attribute {
511 attr
gnzlbg9ae88d82017-01-26 20:45:17 +0100512}
513
David Tolnay660b03e2017-01-29 11:26:30 -0800514pub fn noop_fold_fn_ret_ty<F: ?Sized + Folder>(folder: &mut F,
515 ret_ty: FunctionRetTy)
516 -> FunctionRetTy {
David Tolnay7a1cda02017-01-29 11:50:31 -0800517 use FunctionRetTy::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100518 match ret_ty {
519 Default => Default,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700520 Ty(ty, t) => Ty(folder.fold_ty(ty), t),
gnzlbg9ae88d82017-01-26 20:45:17 +0100521 }
522}
523
David Tolnay78488252017-01-29 11:22:59 -0800524pub fn noop_fold_const_expr<F: ?Sized + Folder>(folder: &mut F, expr: ConstExpr) -> ConstExpr {
Alex Crichton62a0a592017-05-22 13:58:53 -0700525 use constant::*;
526 use constant::ConstExpr::*;
527
gnzlbg9ae88d82017-01-26 20:45:17 +0100528 match expr {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700529 Call(c) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700530 Call(ConstCall {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700531 func: c.func.lift(|e| folder.fold_const_expr(e)),
532 args: c.args.lift(|v| folder.fold_const_expr(v)),
533 ..c
Alex Crichton62a0a592017-05-22 13:58:53 -0700534 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100535 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700536 Binary(c) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700537 Binary(ConstBinary {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700538 left: c.left.lift(|e| folder.fold_const_expr(e)),
539 right: c.right.lift(|e| folder.fold_const_expr(e)),
540 ..c
Alex Crichton62a0a592017-05-22 13:58:53 -0700541 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100542 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700543 Unary(c) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700544 Unary(ConstUnary {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700545 expr: c.expr.lift(|e| folder.fold_const_expr(e)),
546 ..c
Alex Crichton62a0a592017-05-22 13:58:53 -0700547 })
548 }
gnzlbg9ae88d82017-01-26 20:45:17 +0100549 Lit(l) => Lit(folder.fold_lit(l)),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700550 Cast(c) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700551 Cast(ConstCast {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700552 expr: c.expr.lift(|e| folder.fold_const_expr(e)),
553 ty: c.ty.lift(|v| folder.fold_ty(v)),
554 ..c
Alex Crichton62a0a592017-05-22 13:58:53 -0700555 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100556 }
557 Path(p) => Path(folder.fold_path(p)),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700558 Index(c) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700559 Index(ConstIndex {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700560 expr: c.expr.lift(|e| folder.fold_const_expr(e)),
561 index: c.index.lift(|e| folder.fold_const_expr(e)),
562 ..c
Alex Crichton62a0a592017-05-22 13:58:53 -0700563 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100564 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700565 Paren(c) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700566 Paren(ConstParen {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700567 expr: c.expr.lift(|e| folder.fold_const_expr(e)),
568 ..c
Alex Crichton62a0a592017-05-22 13:58:53 -0700569 })
570 }
David Tolnay4a25fc22017-01-29 11:32:20 -0800571 Other(e) => Other(noop_fold_other_const_expr(folder, e)),
gnzlbg9ae88d82017-01-26 20:45:17 +0100572 }
573}
David Tolnay4a25fc22017-01-29 11:32:20 -0800574
575#[cfg(feature = "full")]
576fn noop_fold_other_const_expr<F: ?Sized + Folder>(folder: &mut F, e: Expr) -> Expr {
577 folder.fold_expr(e)
578}
579
580#[cfg(not(feature = "full"))]
David Tolnay05120ef2017-03-12 18:29:26 -0700581fn noop_fold_other_const_expr<F: ?Sized + Folder>(_: &mut F,
582 e: constant::Other)
583 -> constant::Other {
David Tolnay4a25fc22017-01-29 11:32:20 -0800584 e
585}
586
David Tolnay78488252017-01-29 11:22:59 -0800587pub fn noop_fold_lit<F: ?Sized + Folder>(_: &mut F, _lit: Lit) -> Lit {
gnzlbg9ae88d82017-01-26 20:45:17 +0100588 _lit
589}
590
David Tolnay78488252017-01-29 11:22:59 -0800591pub fn noop_fold_tt<F: ?Sized + Folder>(folder: &mut F, tt: TokenTree) -> TokenTree {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700592 use proc_macro2::{TokenKind, TokenTree as TokenTree2};
593 match tt.0.kind {
594 TokenKind::Word(sym) => {
595 let sym = folder.fold_ident(Ident::new(sym, Span(tt.0.span)));
596 TokenTree(TokenTree2 {
597 span: sym.span.0,
598 kind: TokenKind::Word(sym.sym),
599 })
David Tolnay660b03e2017-01-29 11:26:30 -0800600 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700601 TokenKind::Op(..) => tt,
602 TokenKind::Literal(lit) => {
603 folder.fold_lit(Lit {
604 value: LitKind::Other(lit),
605 span: Span(tt.0.span),
606 }).into_token_tree()
607 }
608 TokenKind::Sequence(delim, stream) => {
609 let stream = stream.into_iter().map(|tt| {
610 noop_fold_tt(folder, TokenTree(tt)).0
611 }).collect();
612 TokenTree(TokenTree2 {
613 span: tt.0.span,
614 kind: TokenKind::Sequence(delim, stream),
615 })
David Tolnay660b03e2017-01-29 11:26:30 -0800616 }
gnzlbg9ae88d82017-01-26 20:45:17 +0100617 }
618}
619
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700620pub fn noop_fold_mac<F: ?Sized + Folder>(folder: &mut F, mac: Mac) -> Mac {
gnzlbg9ae88d82017-01-26 20:45:17 +0100621 Mac {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700622 path: folder.fold_path(mac.path),
623 tokens: mac.tokens.lift(|tt| noop_fold_tt(folder, tt)),
624 ..mac
gnzlbg9ae88d82017-01-26 20:45:17 +0100625 }
626}
627
628#[cfg(feature = "full")]
David Tolnay660b03e2017-01-29 11:26:30 -0800629pub fn noop_fold_crate<F: ?Sized + Folder>(folder: &mut F,
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700630 krate: Crate)
David Tolnay660b03e2017-01-29 11:26:30 -0800631 -> Crate {
gnzlbg9ae88d82017-01-26 20:45:17 +0100632 Crate {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700633 attrs: krate.attrs.lift(|a| folder.fold_attribute(a)),
634 items: krate.items.lift(|i| folder.fold_item(i)),
635 ..krate
gnzlbg9ae88d82017-01-26 20:45:17 +0100636 }
637
638}
639
640#[cfg(feature = "full")]
David Tolnay78488252017-01-29 11:22:59 -0800641pub fn noop_fold_block<F: ?Sized + Folder>(folder: &mut F, block: Block) -> Block {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700642 Block {
643 stmts: block.stmts.lift(|s| folder.fold_stmt(s)),
644 ..block
645 }
gnzlbg9ae88d82017-01-26 20:45:17 +0100646}
647
David Tolnay78488252017-01-29 11:22:59 -0800648fn noop_fold_vis<F: ?Sized + Folder>(folder: &mut F, vis: Visibility) -> Visibility {
David Tolnay7a1cda02017-01-29 11:50:31 -0800649 use Visibility::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100650 match vis {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700651 Crate(t) => Crate(t),
652 Inherited(i) => Inherited(i),
653 Public(p) => Public(p),
654 Restricted(data) => {
655 Restricted(VisRestricted {
656 path: data.path.lift(|p| folder.fold_path(p)),
657 ..data
658 })
659 }
gnzlbg9ae88d82017-01-26 20:45:17 +0100660 }
661}
662
663#[cfg(feature = "full")]
David Tolnay660b03e2017-01-29 11:26:30 -0800664pub fn noop_fold_item<F: ?Sized + Folder>(folder: &mut F,
665 Item { ident, vis, attrs, node }: Item)
666 -> Item {
Alex Crichton62a0a592017-05-22 13:58:53 -0700667 use item::*;
David Tolnay7a1cda02017-01-29 11:50:31 -0800668 use ItemKind::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100669 Item {
670 ident: folder.fold_ident(ident.clone()),
671 vis: noop_fold_vis(folder, vis),
672 attrs: attrs.lift(|a| folder.fold_attribute(a)),
673 node: match node {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700674 ExternCrate(i) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700675 ExternCrate(ItemExternCrate {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700676 original: i.original.map(|i| folder.fold_ident(i)),
677 ..i
Alex Crichton62a0a592017-05-22 13:58:53 -0700678 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100679 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700680 Use(i) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700681 Use(ItemUse {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700682 path: Box::new(folder.fold_view_path(*i.path)),
683 ..i
Alex Crichton62a0a592017-05-22 13:58:53 -0700684 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100685 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700686 Static(i) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700687 Static(ItemStatic {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700688 ty: Box::new(folder.fold_ty(*i.ty)),
689 mutbl: i.mutbl,
690 expr: i.expr.lift(|e| folder.fold_expr(e)),
691 ..i
Alex Crichton62a0a592017-05-22 13:58:53 -0700692 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100693 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700694 Const(i) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700695 Const(ItemConst {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700696 ty: i.ty.lift(|ty| folder.fold_ty(ty)),
697 expr: i.expr.lift(|e| folder.fold_expr(e)),
698 ..i
Alex Crichton62a0a592017-05-22 13:58:53 -0700699 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100700 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700701 Fn(i) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700702 Fn(ItemFn {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700703 decl: i.decl.lift(|v| folder.fold_fn_decl(v)),
704 block: i.block.lift(|v| folder.fold_block(v)),
705 ..i
Alex Crichton62a0a592017-05-22 13:58:53 -0700706 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100707 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700708 Mod(i) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700709 Mod(ItemMod {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700710 items: i.items.map(|items| {
711 (items.0.lift(|i| folder.fold_item(i)), items.1)
712 }),
713 ..i
Alex Crichton62a0a592017-05-22 13:58:53 -0700714 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100715 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700716 ForeignMod(i) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700717 ForeignMod(ItemForeignMod {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700718 items: i.items.lift(|foreign_item| {
Alex Crichton62a0a592017-05-22 13:58:53 -0700719 folder.fold_foreign_item(foreign_item)
720 }),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700721 ..i
Alex Crichton62a0a592017-05-22 13:58:53 -0700722 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100723 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700724 Ty(i) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700725 Ty(ItemTy {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700726 ty: i.ty.lift(|ty| folder.fold_ty(ty)),
727 generics: folder.fold_generics(i.generics),
728 ..i
Alex Crichton62a0a592017-05-22 13:58:53 -0700729 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100730 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700731 Enum(i) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700732 Enum(ItemEnum {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700733 variants: i.variants.lift(|v| folder.fold_variant(v)),
734 generics: folder.fold_generics(i.generics),
735 ..i
Alex Crichton62a0a592017-05-22 13:58:53 -0700736 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100737 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700738 Struct(i) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700739 Struct(ItemStruct {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700740 data: folder.fold_variant_data(i.data),
741 generics: folder.fold_generics(i.generics),
742 ..i
Alex Crichton62a0a592017-05-22 13:58:53 -0700743 })
744 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700745 Union(i) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700746 Union(ItemUnion {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700747 data: folder.fold_variant_data(i.data),
748 generics: folder.fold_generics(i.generics),
749 ..i
Alex Crichton62a0a592017-05-22 13:58:53 -0700750 })
751 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700752 Trait(i) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700753 Trait(ItemTrait {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700754 generics: folder.fold_generics(i.generics),
755 supertraits: i.supertraits.lift(|typb| folder.fold_ty_param_bound(typb)),
756 items: i.items.lift(|ti| folder.fold_trait_item(ti)),
757 ..i
Alex Crichton62a0a592017-05-22 13:58:53 -0700758 })
759 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700760 DefaultImpl(i) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700761 DefaultImpl(ItemDefaultImpl {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700762 path: folder.fold_path(i.path),
763 ..i
Alex Crichton62a0a592017-05-22 13:58:53 -0700764 })
765 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700766 Impl(i) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700767 Impl(ItemImpl {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700768 generics: folder.fold_generics(i.generics),
769 trait_: i.trait_.map(|p| folder.fold_path(p)),
770 self_ty: i.self_ty.lift(|ty| folder.fold_ty(ty)),
771 items: i.items.lift(|i| folder.fold_impl_item(i)),
772 ..i
Alex Crichton62a0a592017-05-22 13:58:53 -0700773 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100774 }
775 Mac(mac) => Mac(folder.fold_mac(mac)),
776 },
777 }
778}
779
gnzlbg9ae88d82017-01-26 20:45:17 +0100780#[cfg(feature = "full")]
David Tolnay78488252017-01-29 11:22:59 -0800781pub fn noop_fold_expr<F: ?Sized + Folder>(folder: &mut F, Expr { node, attrs }: Expr) -> Expr {
Alex Crichton62a0a592017-05-22 13:58:53 -0700782 use expr::*;
783 use expr::ExprKind::*;
784
gnzlbg9ae88d82017-01-26 20:45:17 +0100785 Expr {
786 node: match node {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700787 Box(e) => {
788 Box(ExprBox {
789 expr: e.expr.lift(|e| folder.fold_expr(e)),
790 ..e
791 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100792 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700793 InPlace(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700794 InPlace(ExprInPlace {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700795 place: e.place.lift(|e| folder.fold_expr(e)),
796 value: e.value.lift(|e| folder.fold_expr(e)),
797 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -0700798 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100799 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700800 Array(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700801 Array(ExprArray {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700802 exprs: e.exprs.lift(|e| folder.fold_expr(e)),
803 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -0700804 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100805 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700806 Call(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700807 Call(ExprCall {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700808 func: e.func.lift(|e| folder.fold_expr(e)),
809 args: e.args.lift(|e| folder.fold_expr(e)),
810 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -0700811 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100812 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700813 MethodCall(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700814 MethodCall(ExprMethodCall {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700815 method: folder.fold_ident(e.method),
816 typarams: e.typarams.lift(|t| folder.fold_ty(t)),
817 args: e.args.lift(|e| folder.fold_expr(e)),
818 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -0700819 })
820 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700821 Tup(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700822 Tup(ExprTup {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700823 args: e.args.lift(|e| folder.fold_expr(e)),
824 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -0700825 })
826 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700827 Binary(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700828 Binary(ExprBinary {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700829 left: e.left.lift(|e| folder.fold_expr(e)),
830 right: e.right.lift(|e| folder.fold_expr(e)),
831 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -0700832 })
833 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700834 Unary(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700835 Unary(ExprUnary {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700836 expr: e.expr.lift(|e| folder.fold_expr(e)),
837 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -0700838 })
839 }
gnzlbg9ae88d82017-01-26 20:45:17 +0100840 Lit(lit) => Lit(folder.fold_lit(lit)),
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700841 Cast(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700842 Cast(ExprCast {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700843 expr: e.expr.lift(|e| folder.fold_expr(e)),
844 ty: e.ty.lift(|t| folder.fold_ty(t)),
845 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -0700846 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100847 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700848 Type(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700849 Type(ExprType {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700850 expr: e.expr.lift(|e| folder.fold_expr(e)),
851 ty: e.ty.lift(|t| folder.fold_ty(t)),
852 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -0700853 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100854 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700855 If(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700856 If(ExprIf {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700857 cond: e.cond.lift(|e| folder.fold_expr(e)),
858 if_true: folder.fold_block(e.if_true),
859 if_false: e.if_false.map(|v| v.lift(|e| folder.fold_expr(e))),
860 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -0700861 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100862 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700863 IfLet(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700864 IfLet(ExprIfLet {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700865 pat: e.pat.lift(|p| folder.fold_pat(p)),
866 expr: e.expr.lift(|e| folder.fold_expr(e)),
867 if_true: folder.fold_block(e.if_true),
868 if_false: e.if_false.map(|v| v.lift(|e| folder.fold_expr(e))),
869 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -0700870 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100871 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700872 While(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700873 While(ExprWhile {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700874 cond: e.cond.lift(|e| folder.fold_expr(e)),
875 body: folder.fold_block(e.body),
876 label: e.label.map(|i| folder.fold_ident(i)),
877 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -0700878 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100879 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700880 WhileLet(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700881 WhileLet(ExprWhileLet {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700882 pat: e.pat.lift(|p| folder.fold_pat(p)),
883 expr: e.expr.lift(|e| folder.fold_expr(e)),
884 body: folder.fold_block(e.body),
885 label: e.label.map(|i| folder.fold_ident(i)),
886 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -0700887 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100888 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700889 ForLoop(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700890 ForLoop(ExprForLoop {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700891 pat: e.pat.lift(|p| folder.fold_pat(p)),
892 expr: e.expr.lift(|e| folder.fold_expr(e)),
893 body: folder.fold_block(e.body),
894 label: e.label.map(|i| folder.fold_ident(i)),
895 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -0700896 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100897 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700898 Loop(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700899 Loop(ExprLoop {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700900 body: folder.fold_block(e.body),
901 label: e.label.map(|i| folder.fold_ident(i)),
902 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -0700903 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100904 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700905 Match(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700906 Match(ExprMatch {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700907 expr: e.expr.lift(|e| folder.fold_expr(e)),
908 arms: e.arms.lift(|a: Arm| {
Alex Crichton62a0a592017-05-22 13:58:53 -0700909 Arm {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700910 attrs: a.attrs.lift(|a| folder.fold_attribute(a)),
911 pats: a.pats.lift(|p| folder.fold_pat(p)),
912 guard: a.guard.map(|v| v.lift(|e| folder.fold_expr(e))),
913 body: a.body.lift(|e| folder.fold_expr(e)),
914 ..a
Alex Crichton62a0a592017-05-22 13:58:53 -0700915 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700916 }),
917 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -0700918 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100919 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700920 Catch(e) => {
921 Catch(ExprCatch {
922 block: folder.fold_block(e.block),
923 ..e
924 })
Arnavion02ef13f2017-04-25 00:54:31 -0700925 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700926 Closure(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700927 Closure(ExprClosure {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700928 decl: e.decl.lift(|v| folder.fold_fn_decl(v)),
929 body: e.body.lift(|e| folder.fold_expr(e)),
930 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -0700931 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100932 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700933 Block(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700934 Block(ExprBlock {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700935 block: folder.fold_block(e.block),
936 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -0700937 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100938 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700939 Assign(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700940 Assign(ExprAssign {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700941 left: e.left.lift(|e| folder.fold_expr(e)),
942 right: e.right.lift(|e| folder.fold_expr(e)),
943 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -0700944 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100945 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700946 AssignOp(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700947 AssignOp(ExprAssignOp {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700948 left: e.left.lift(|e| folder.fold_expr(e)),
949 right: e.right.lift(|e| folder.fold_expr(e)),
950 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -0700951 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100952 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700953 Field(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700954 Field(ExprField {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700955 expr: e.expr.lift(|e| folder.fold_expr(e)),
956 field: folder.fold_ident(e.field),
957 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -0700958 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100959 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700960 TupField(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700961 TupField(ExprTupField {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700962 expr: e.expr.lift(|e| folder.fold_expr(e)),
963 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -0700964 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100965 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700966 Index(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700967 Index(ExprIndex {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700968 expr: e.expr.lift(|e| folder.fold_expr(e)),
969 index: e.index.lift(|e| folder.fold_expr(e)),
970 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -0700971 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100972 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700973 Range(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700974 Range(ExprRange {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700975 from: e.from.map(|v| v.lift(|e| folder.fold_expr(e))),
976 to: e.to.map(|v| v.lift(|e| folder.fold_expr(e))),
977 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -0700978 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100979 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700980 Path(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700981 Path(ExprPath {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700982 qself: e.qself.map(|v| noop_fold_qself(folder, v)),
983 path: folder.fold_path(e.path),
Alex Crichton62a0a592017-05-22 13:58:53 -0700984 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100985 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700986 AddrOf(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700987 AddrOf(ExprAddrOf {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700988 expr: e.expr.lift(|e| folder.fold_expr(e)),
989 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -0700990 })
991 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700992 Break(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -0700993 Break(ExprBreak {
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700994 label: e.label.map(|i| folder.fold_ident(i)),
995 expr: e.expr.map(|v| v.lift(|e| folder.fold_expr(e))),
996 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -0700997 })
998 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -0700999 Continue(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001000 Continue(ExprContinue {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001001 label: e.label.map(|i| folder.fold_ident(i)),
1002 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -07001003 })
1004 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001005 Ret(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001006 Ret(ExprRet {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001007 expr: e.expr.map(|v| v.lift(|e| folder.fold_expr(e))),
1008 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -07001009 })
1010 }
1011 Mac(mac) => Mac(folder.fold_mac(mac)),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001012 Struct(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001013 Struct(ExprStruct {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001014 path: folder.fold_path(e.path),
1015 fields: e.fields.lift(|field: FieldValue| {
Alex Crichton62a0a592017-05-22 13:58:53 -07001016 FieldValue {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001017 ident: folder.fold_ident(field.ident),
1018 expr: folder.fold_expr(field.expr),
1019 attrs: field.attrs.lift(|v| folder.fold_attribute(v)),
1020 ..field
Alex Crichton62a0a592017-05-22 13:58:53 -07001021 }
1022 }),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001023 rest: e.rest.map(|v| v.lift(|e| folder.fold_expr(e))),
1024 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -07001025 })
1026 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001027 Repeat(e) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001028 Repeat(ExprRepeat {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001029 expr: e.expr.lift(|e| folder.fold_expr(e)),
1030 amt: e.amt.lift(|e| folder.fold_expr(e)),
1031 ..e
Alex Crichton62a0a592017-05-22 13:58:53 -07001032 })
1033 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001034 Paren(e) => {
1035 Paren(ExprParen {
1036 expr: e.expr.lift(|e| folder.fold_expr(e)),
1037 ..e
1038 })
Alex Crichton62a0a592017-05-22 13:58:53 -07001039 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001040 Try(e) => {
1041 Try(ExprTry {
1042 expr: e.expr.lift(|e| folder.fold_expr(e)),
1043 ..e
1044 })
Alex Crichton62a0a592017-05-22 13:58:53 -07001045 }
gnzlbg9ae88d82017-01-26 20:45:17 +01001046 },
1047 attrs: attrs.into_iter().map(|a| folder.fold_attribute(a)).collect(),
1048 }
1049}
1050
gnzlbg9ae88d82017-01-26 20:45:17 +01001051#[cfg(feature = "full")]
David Tolnay78488252017-01-29 11:22:59 -08001052pub fn noop_fold_foreign_item<F: ?Sized + Folder>(folder: &mut F,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001053 item: ForeignItem)
David Tolnay05120ef2017-03-12 18:29:26 -07001054-> ForeignItem{
Alex Crichton62a0a592017-05-22 13:58:53 -07001055 use item::*;
1056
gnzlbg9ae88d82017-01-26 20:45:17 +01001057 ForeignItem {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001058 ident: folder.fold_ident(item.ident),
1059 attrs: item.attrs.into_iter().map(|a| folder.fold_attribute(a)).collect(),
1060 node: match item.node {
1061 ForeignItemKind::Fn(item) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001062 ForeignItemKind::Fn(ForeignItemFn {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001063 decl: item.decl.lift(|v| folder.fold_fn_decl(v)),
Alex Crichton62a0a592017-05-22 13:58:53 -07001064 })
gnzlbg9ae88d82017-01-26 20:45:17 +01001065 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001066 ForeignItemKind::Static(item) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001067 ForeignItemKind::Static(ForeignItemStatic {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001068 ty: item.ty.lift(|v| folder.fold_ty(v)),
1069 ..item
Alex Crichton62a0a592017-05-22 13:58:53 -07001070 })
gnzlbg9ae88d82017-01-26 20:45:17 +01001071 }
1072 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001073 vis: noop_fold_vis(folder, item.vis),
1074 ..item
gnzlbg9ae88d82017-01-26 20:45:17 +01001075 }
1076}
1077
gnzlbg9ae88d82017-01-26 20:45:17 +01001078#[cfg(feature = "full")]
David Tolnay78488252017-01-29 11:22:59 -08001079pub fn noop_fold_pat<F: ?Sized + Folder>(folder: &mut F, pat: Pat) -> Pat {
David Tolnay7a1cda02017-01-29 11:50:31 -08001080 use Pat::*;
gnzlbg9ae88d82017-01-26 20:45:17 +01001081 match pat {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001082 Wild(b) => Wild(b),
1083 Ident(p) => {
1084 Ident(PatIdent {
1085 ident: folder.fold_ident(p.ident),
1086 subpat: p.subpat.map(|p| p.lift(|p| folder.fold_pat(p))),
1087 ..p
1088 })
gnzlbg9ae88d82017-01-26 20:45:17 +01001089 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001090 Struct(p) => {
1091 Struct(PatStruct {
1092 path: folder.fold_path(p.path),
1093 fields: p.fields.lift(|field: FieldPat| {
gnzlbg9ae88d82017-01-26 20:45:17 +01001094 FieldPat {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001095 ident: folder.fold_ident(field.ident),
1096 pat: field.pat.lift(|p| folder.fold_pat(p)),
1097 attrs: field.attrs.lift(|a| folder.fold_attribute(a)),
1098 ..field
gnzlbg9ae88d82017-01-26 20:45:17 +01001099 }
1100 }),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001101 ..p
1102 })
gnzlbg9ae88d82017-01-26 20:45:17 +01001103 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001104 TupleStruct(p) => {
1105 TupleStruct(PatTupleStruct {
1106 path: folder.fold_path(p.path),
1107 pat: PatTuple {
1108 pats: p.pat.pats.lift(|p| folder.fold_pat(p)),
1109 ..p.pat
1110 },
1111 })
gnzlbg9ae88d82017-01-26 20:45:17 +01001112 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001113 Path(p) => {
1114 Path(PatPath {
1115 qself: p.qself.map(|v| noop_fold_qself(folder, v)),
1116 path: folder.fold_path(p.path),
1117 })
gnzlbg9ae88d82017-01-26 20:45:17 +01001118 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001119 Tuple(p) => {
1120 Tuple(PatTuple {
1121 pats: p.pats.lift(|p| folder.fold_pat(p)),
1122 ..p
1123 })
gnzlbg9ae88d82017-01-26 20:45:17 +01001124 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001125 Box(p) => {
1126 Box(PatBox {
1127 pat: p.pat.lift(|p| folder.fold_pat(p)),
1128 ..p
1129 })
1130 }
1131 Ref(p) => {
1132 Ref(PatRef {
1133 pat: p.pat.lift(|p| folder.fold_pat(p)),
1134 ..p
1135 })
1136 }
1137 Lit(p) => {
1138 Lit(PatLit {
1139 expr: p.expr.lift(|e| folder.fold_expr(e)),
1140 })
1141 }
1142 Range(p) => {
1143 Range(PatRange {
1144 hi: p.hi.lift(|e| folder.fold_expr(e)),
1145 lo: p.lo.lift(|e| folder.fold_expr(e)),
1146 ..p
1147 })
1148 }
1149 Slice(p) => {
1150 Slice(PatSlice {
1151 front: p.front.lift(|p| folder.fold_pat(p)),
1152 middle: p.middle.map(|v| v.lift(|p| folder.fold_pat(p))),
1153 back: p.back.lift(|p| folder.fold_pat(p)),
1154 ..p
1155 })
gnzlbg9ae88d82017-01-26 20:45:17 +01001156 }
1157 Mac(mac) => Mac(folder.fold_mac(mac)),
1158 }
1159}
1160
gnzlbg9ae88d82017-01-26 20:45:17 +01001161#[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001162pub fn noop_fold_fn_decl<F: ?Sized + Folder>(folder: &mut F, decl: FnDecl)
1163 -> FnDecl
1164{
gnzlbg9ae88d82017-01-26 20:45:17 +01001165 FnDecl {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001166 inputs: decl.inputs.lift(|a| {
Alex Crichton62a0a592017-05-22 13:58:53 -07001167 use item::*;
David Tolnay7a1cda02017-01-29 11:50:31 -08001168 use FnArg::*;
gnzlbg9ae88d82017-01-26 20:45:17 +01001169 match a {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001170 SelfRef(a) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001171 SelfRef(ArgSelfRef {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001172 lifetime: a.lifetime.map(|v| folder.fold_lifetime(v)),
1173 ..a
Alex Crichton62a0a592017-05-22 13:58:53 -07001174 })
gnzlbg9ae88d82017-01-26 20:45:17 +01001175 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001176 SelfValue(a) => SelfValue(a),
1177 Captured(a) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001178 Captured(ArgCaptured {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001179 pat: folder.fold_pat(a.pat),
1180 ty: folder.fold_ty(a.ty),
1181 ..a
Alex Crichton62a0a592017-05-22 13:58:53 -07001182 })
1183 }
gnzlbg9ae88d82017-01-26 20:45:17 +01001184 Ignored(ty) => Ignored(folder.fold_ty(ty)),
1185 }
1186 }),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001187 output: folder.fold_fn_ret_ty(decl.output),
1188 generics: folder.fold_generics(decl.generics),
1189 ..decl
gnzlbg9ae88d82017-01-26 20:45:17 +01001190 }
gnzlbg9ae88d82017-01-26 20:45:17 +01001191}
1192
gnzlbg9ae88d82017-01-26 20:45:17 +01001193#[cfg(feature = "full")]
David Tolnay78488252017-01-29 11:22:59 -08001194pub fn noop_fold_trait_item<F: ?Sized + Folder>(folder: &mut F,
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001195 item: TraitItem)
David Tolnay660b03e2017-01-29 11:26:30 -08001196 -> TraitItem {
Alex Crichton62a0a592017-05-22 13:58:53 -07001197 use item::*;
David Tolnay7a1cda02017-01-29 11:50:31 -08001198 use TraitItemKind::*;
gnzlbg9ae88d82017-01-26 20:45:17 +01001199 TraitItem {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001200 ident: folder.fold_ident(item.ident),
1201 attrs: item.attrs.lift(|v| folder.fold_attribute(v)),
1202 node: match item.node {
1203 Const(i) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001204 Const(TraitItemConst {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001205 ty: folder.fold_ty(i.ty),
1206 default: i.default.map(|v| folder.fold_expr(v)),
1207 ..i
Alex Crichton62a0a592017-05-22 13:58:53 -07001208 })
gnzlbg9ae88d82017-01-26 20:45:17 +01001209 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001210 Method(i) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001211 Method(TraitItemMethod {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001212 sig: folder.fold_method_sig(i.sig),
1213 default: i.default.map(|v| folder.fold_block(v)),
1214 ..i
Alex Crichton62a0a592017-05-22 13:58:53 -07001215 })
1216 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001217 Type(i) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001218 Type(TraitItemType {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001219 bounds: i.bounds.lift(|v| folder.fold_ty_param_bound(v)),
1220 default: i.default.map(|v| folder.fold_ty(v)),
1221 ..i
Alex Crichton62a0a592017-05-22 13:58:53 -07001222 })
gnzlbg9ae88d82017-01-26 20:45:17 +01001223 }
David Tolnay70925b72017-01-29 11:20:54 -08001224 Macro(mac) => Macro(folder.fold_mac(mac)),
gnzlbg9ae88d82017-01-26 20:45:17 +01001225 },
1226 }
1227}
1228
gnzlbg9ae88d82017-01-26 20:45:17 +01001229#[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001230pub fn noop_fold_impl_item<F: ?Sized + Folder>(folder: &mut F, item: ImplItem)
1231 -> ImplItem
1232{
Alex Crichton62a0a592017-05-22 13:58:53 -07001233 use item::*;
David Tolnay7a1cda02017-01-29 11:50:31 -08001234 use ImplItemKind::*;
Alex Crichton62a0a592017-05-22 13:58:53 -07001235
gnzlbg9ae88d82017-01-26 20:45:17 +01001236 ImplItem {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001237 ident: folder.fold_ident(item.ident),
1238 vis: noop_fold_vis(folder, item.vis),
1239 attrs: item.attrs.lift(|v| folder.fold_attribute(v)),
1240 node: match item.node {
1241 Const(i) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001242 Const(ImplItemConst {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001243 ty: folder.fold_ty(i.ty),
1244 expr: folder.fold_expr(i.expr),
1245 ..i
Alex Crichton62a0a592017-05-22 13:58:53 -07001246 })
1247 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001248 Method(i) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001249 Method(ImplItemMethod {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001250 sig: folder.fold_method_sig(i.sig),
1251 block: folder.fold_block(i.block),
Alex Crichton62a0a592017-05-22 13:58:53 -07001252 })
1253 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001254 Type(i) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001255 Type(ImplItemType {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001256 ty: folder.fold_ty(i.ty),
1257 ..i
Alex Crichton62a0a592017-05-22 13:58:53 -07001258 })
1259 }
gnzlbg9ae88d82017-01-26 20:45:17 +01001260 Macro(mac) => Macro(folder.fold_mac(mac)),
1261 },
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001262 ..item
gnzlbg9ae88d82017-01-26 20:45:17 +01001263 }
1264}
1265
gnzlbg9ae88d82017-01-26 20:45:17 +01001266#[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001267pub fn noop_fold_method_sig<F: ?Sized + Folder>(folder: &mut F, sig: MethodSig)
1268 -> MethodSig
1269{
gnzlbg9ae88d82017-01-26 20:45:17 +01001270 MethodSig {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001271 decl: folder.fold_fn_decl(sig.decl),
1272 ..sig
gnzlbg9ae88d82017-01-26 20:45:17 +01001273 }
1274
1275}
1276
gnzlbg9ae88d82017-01-26 20:45:17 +01001277#[cfg(feature = "full")]
David Tolnay78488252017-01-29 11:22:59 -08001278pub fn noop_fold_stmt<F: ?Sized + Folder>(folder: &mut F, stmt: Stmt) -> Stmt {
David Tolnay7a1cda02017-01-29 11:50:31 -08001279 use Stmt::*;
gnzlbg9ae88d82017-01-26 20:45:17 +01001280 match stmt {
1281 Local(local) => Local(local.lift(|l| folder.fold_local(l))),
1282 Item(item) => Item(item.lift(|v| folder.fold_item(v))),
1283 Expr(expr) => Expr(expr.lift(|v| folder.fold_expr(v))),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001284 Semi(expr, t) => Semi(expr.lift(|v| folder.fold_expr(v)), t),
gnzlbg9ae88d82017-01-26 20:45:17 +01001285 Mac(mac_stmt) => {
1286 Mac(mac_stmt.lift(|(mac, style, attrs)| {
David Tolnay05120ef2017-03-12 18:29:26 -07001287 (folder.fold_mac(mac),
1288 style,
1289 attrs.lift(|a| folder.fold_attribute(a)))
1290 }))
gnzlbg9ae88d82017-01-26 20:45:17 +01001291 }
1292 }
1293
1294}
1295
gnzlbg9ae88d82017-01-26 20:45:17 +01001296#[cfg(feature = "full")]
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001297pub fn noop_fold_local<F: ?Sized + Folder>(folder: &mut F, local: Local)
1298 -> Local
1299{
gnzlbg9ae88d82017-01-26 20:45:17 +01001300 Local {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001301 pat: local.pat.lift(|v| folder.fold_pat(v)),
1302 ty: local.ty.map(|v| v.lift(|t| folder.fold_ty(t))),
1303 init: local.init.map(|v| v.lift(|e| folder.fold_expr(e))),
1304 attrs: local.attrs.lift(|a| folder.fold_attribute(a)),
1305 ..local
gnzlbg9ae88d82017-01-26 20:45:17 +01001306 }
1307}
1308
1309#[cfg(feature = "full")]
David Tolnay78488252017-01-29 11:22:59 -08001310pub fn noop_fold_view_path<F: ?Sized + Folder>(folder: &mut F, view_path: ViewPath) -> ViewPath {
Alex Crichton62a0a592017-05-22 13:58:53 -07001311 use item::*;
David Tolnay7a1cda02017-01-29 11:50:31 -08001312 use ViewPath::*;
gnzlbg9ae88d82017-01-26 20:45:17 +01001313 match view_path {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001314 Simple(p) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001315 Simple(PathSimple {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001316 path: folder.fold_path(p.path),
1317 rename: p.rename.map(|i| folder.fold_ident(i)),
1318 ..p
Alex Crichton62a0a592017-05-22 13:58:53 -07001319 })
1320 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001321 Glob(p) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001322 Glob(PathGlob {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001323 path: folder.fold_path(p.path),
1324 ..p
Alex Crichton62a0a592017-05-22 13:58:53 -07001325 })
1326 }
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001327 List(p) => {
Alex Crichton62a0a592017-05-22 13:58:53 -07001328 List(PathList {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001329 path: folder.fold_path(p.path),
1330 items: p.items.lift(|item: PathListItem| {
Alex Crichton62a0a592017-05-22 13:58:53 -07001331 PathListItem {
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001332 name: folder.fold_ident(item.name),
1333 rename: item.rename.map(|i| folder.fold_ident(i)),
1334 ..item
Alex Crichton62a0a592017-05-22 13:58:53 -07001335 }
1336 }),
Alex Crichtonccbb45d2017-05-23 10:58:24 -07001337 ..p
Alex Crichton62a0a592017-05-22 13:58:53 -07001338 })
gnzlbg9ae88d82017-01-26 20:45:17 +01001339 }
1340 }
1341}