blob: 9afda635971d1b0e95ee86552bccf8a1498241a0 [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
10/// AST->AST fold.
11///
12/// Each method of the Folder trait is a hook to be potentially overridden. Each
13/// method's default implementation recursively visits the substructure of the
14/// input via the `noop_fold` methods, which perform an "identity fold", that
15/// is, they return the same structure that they are given (for example the
16/// `fold_crate` method by default calls `fold::noop_fold_crate`).
17///
18/// If you want to ensure that your code handles every variant explicitly, you
19/// need to override each method and monitor future changes to `Folder` in case
20/// a new method with a new default implementation gets introduced.
David Tolnay78488252017-01-29 11:22:59 -080021pub trait Folder {
gnzlbg9ae88d82017-01-26 20:45:17 +010022 // Any additions to this trait should happen in form
23 // of a call to a public `noop_*` function that only calls
24 // out to the folder again, not other `noop_*` functions.
25 //
26 // This is a necessary API workaround to the problem of not
27 // being able to call out to the super default method
28 // in an overridden default method.
29
30 fn fold_ident(&mut self, _ident: Ident) -> Ident {
31 noop_fold_ident(self, _ident)
32 }
33 fn fold_derive_input(&mut self, derive_input: DeriveInput) -> DeriveInput {
34 noop_fold_derive_input(self, derive_input)
35 }
36 fn fold_ty(&mut self, ty: Ty) -> Ty {
37 noop_fold_ty(self, ty)
38 }
39 fn fold_generics(&mut self, generics: Generics) -> Generics {
40 noop_fold_generics(self, generics)
41 }
42 fn fold_ty_param_bound(&mut self, bound: TyParamBound) -> TyParamBound {
43 noop_fold_ty_param_bound(self, bound)
44 }
45 fn fold_poly_trait_ref(&mut self, trait_ref: PolyTraitRef) -> PolyTraitRef {
46 noop_fold_poly_trait_ref(self, trait_ref)
47 }
48 fn fold_variant_data(&mut self, data: VariantData) -> VariantData {
49 noop_fold_variant_data(self, data)
50 }
51 fn fold_field(&mut self, field: Field) -> Field {
52 noop_fold_field(self, field)
53 }
54 fn fold_variant(&mut self, variant: Variant) -> Variant {
55 noop_fold_variant(self, variant)
56 }
57 fn fold_lifetime(&mut self, _lifetime: Lifetime) -> Lifetime {
58 noop_fold_lifetime(self, _lifetime)
59 }
60 fn fold_lifetime_def(&mut self, lifetime: LifetimeDef) -> LifetimeDef {
61 noop_fold_lifetime_def(self, lifetime)
62 }
63 fn fold_path(&mut self, path: Path) -> Path {
64 noop_fold_path(self, path)
65 }
66 fn fold_path_segment(&mut self, path_segment: PathSegment) -> PathSegment {
67 noop_fold_path_segment(self, path_segment)
68 }
69 fn fold_path_parameters(&mut self, path_parameters: PathParameters) -> PathParameters {
70 noop_fold_path_parameters(self, path_parameters)
71 }
72 fn fold_assoc_type_binding(&mut self, type_binding: TypeBinding) -> TypeBinding {
73 noop_fold_assoc_type_binding(self, type_binding)
74 }
75 fn fold_attribute(&mut self, _attr: Attribute) -> Attribute {
76 noop_fold_attribute(self, _attr)
77 }
78 fn fold_fn_ret_ty(&mut self, ret_ty: FunctionRetTy) -> FunctionRetTy {
79 noop_fold_fn_ret_ty(self, ret_ty)
80 }
81 fn fold_const_expr(&mut self, expr: ConstExpr) -> ConstExpr {
82 noop_fold_const_expr(self, expr)
83 }
84 fn fold_lit(&mut self, _lit: Lit) -> Lit {
85 noop_fold_lit(self, _lit)
86 }
87
88 fn fold_mac(&mut self, mac: Mac) -> Mac {
89 noop_fold_mac(self, mac)
90 }
91
92 #[cfg(feature = "full")]
93 fn fold_crate(&mut self, _crate: Crate) -> Crate {
94 noop_fold_crate(self, _crate)
95 }
96 #[cfg(feature = "full")]
97 fn fold_item(&mut self, item: Item) -> Item {
98 noop_fold_item(self, item)
99 }
100 #[cfg(feature = "full")]
101 fn fold_expr(&mut self, expr: Expr) -> Expr {
102 noop_fold_expr(self, expr)
103 }
104 #[cfg(feature = "full")]
105 fn fold_foreign_item(&mut self, foreign_item: ForeignItem) -> ForeignItem {
106 noop_fold_foreign_item(self, foreign_item)
107 }
108 #[cfg(feature = "full")]
109 fn fold_pat(&mut self, pat: Pat) -> Pat {
110 noop_fold_pat(self, pat)
111 }
112 #[cfg(feature = "full")]
113 fn fold_fn_decl(&mut self, fn_decl: FnDecl) -> FnDecl {
114 noop_fold_fn_decl(self, fn_decl)
115 }
116 #[cfg(feature = "full")]
117 fn fold_trait_item(&mut self, trait_item: TraitItem) -> TraitItem {
118 noop_fold_trait_item(self, trait_item)
119 }
120 #[cfg(feature = "full")]
121 fn fold_impl_item(&mut self, impl_item: ImplItem) -> ImplItem {
122 noop_fold_impl_item(self, impl_item)
123 }
124 #[cfg(feature = "full")]
125 fn fold_method_sig(&mut self, method_sig: MethodSig) -> MethodSig {
126 noop_fold_method_sig(self, method_sig)
127 }
128 #[cfg(feature = "full")]
129 fn fold_stmt(&mut self, stmt: Stmt) -> Stmt {
130 noop_fold_stmt(self, stmt)
131 }
132 #[cfg(feature = "full")]
133 fn fold_block(&mut self, block: Block) -> Block {
134 noop_fold_block(self, block)
135 }
136 #[cfg(feature = "full")]
137 fn fold_local(&mut self, local: Local) -> Local {
138 noop_fold_local(self, local)
139 }
140 #[cfg(feature = "full")]
141 fn fold_view_path(&mut self, view_path: ViewPath) -> ViewPath {
142 noop_fold_view_path(self, view_path)
143 }
144}
145
146trait LiftOnce<T, U> {
147 type Output;
148 fn lift<F>(self, f: F) -> Self::Output where F: FnOnce(T) -> U;
149}
150
151impl<T, U> LiftOnce<T, U> for Box<T> {
152 type Output = Box<U>;
David Tolnay02a8d472017-02-19 12:59:44 -0800153 // Clippy false positive
154 // https://github.com/Manishearth/rust-clippy/issues/1478
155 #[cfg_attr(feature = "cargo-clippy", allow(boxed_local))]
gnzlbg9ae88d82017-01-26 20:45:17 +0100156 fn lift<F>(self, f: F) -> Box<U>
157 where F: FnOnce(T) -> U
158 {
159 Box::new(f(*self))
160 }
161}
162
163trait LiftMut<T, U> {
164 type Output;
165 fn lift<F>(self, f: F) -> Self::Output where F: FnMut(T) -> U;
166}
167
168impl<T, U> LiftMut<T, U> for Vec<T> {
169 type Output = Vec<U>;
170 fn lift<F>(self, f: F) -> Vec<U>
171 where F: FnMut(T) -> U
172 {
173 self.into_iter().map(f).collect()
174 }
175}
176
David Tolnay78488252017-01-29 11:22:59 -0800177pub fn noop_fold_ident<F: ?Sized + Folder>(_: &mut F, _ident: Ident) -> Ident {
gnzlbg9ae88d82017-01-26 20:45:17 +0100178 _ident
179}
180
David Tolnay78488252017-01-29 11:22:59 -0800181pub fn noop_fold_derive_input<F: ?Sized + Folder>(folder: &mut F,
gnzlbg9ae88d82017-01-26 20:45:17 +0100182 DeriveInput{ ident,
183 vis,
184 attrs,
185 generics,
David Tolnay05120ef2017-03-12 18:29:26 -0700186body }: DeriveInput) -> DeriveInput{
David Tolnay7a1cda02017-01-29 11:50:31 -0800187 use Body::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100188 DeriveInput {
189 ident: folder.fold_ident(ident),
190 vis: noop_fold_vis(folder, vis),
191 attrs: attrs.lift(|a| folder.fold_attribute(a)),
192 generics: folder.fold_generics(generics),
193 body: match body {
David Tolnay660b03e2017-01-29 11:26:30 -0800194 Enum(variants) => Enum(variants.lift(move |v| folder.fold_variant(v))),
gnzlbg9ae88d82017-01-26 20:45:17 +0100195 Struct(variant_data) => Struct(folder.fold_variant_data(variant_data)),
196 },
197 }
198}
199
David Tolnay78488252017-01-29 11:22:59 -0800200pub fn noop_fold_ty<F: ?Sized + Folder>(folder: &mut F, ty: Ty) -> Ty {
David Tolnay7a1cda02017-01-29 11:50:31 -0800201 use Ty::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100202 match ty {
203 Slice(inner) => Slice(inner.lift(|v| folder.fold_ty(v))),
204 Paren(inner) => Paren(inner.lift(|v| folder.fold_ty(v))),
205 Ptr(mutable_type) => {
206 let mutable_type_ = *mutable_type;
207 let MutTy { ty, mutability }: MutTy = mutable_type_;
208 Ptr(Box::new(MutTy {
David Tolnay05120ef2017-03-12 18:29:26 -0700209 ty: folder.fold_ty(ty),
210 mutability: mutability,
211 }))
gnzlbg9ae88d82017-01-26 20:45:17 +0100212 }
213 Rptr(opt_lifetime, mutable_type) => {
214 let mutable_type_ = *mutable_type;
215 let MutTy { ty, mutability }: MutTy = mutable_type_;
216 Rptr(opt_lifetime.map(|l| folder.fold_lifetime(l)),
217 Box::new(MutTy {
David Tolnay05120ef2017-03-12 18:29:26 -0700218 ty: folder.fold_ty(ty),
219 mutability: mutability,
220 }))
gnzlbg9ae88d82017-01-26 20:45:17 +0100221 }
222 Never => Never,
223 Infer => Infer,
224 Tup(tuple_element_types) => Tup(tuple_element_types.lift(|x| folder.fold_ty(x))),
225 BareFn(bare_fn) => {
226 let bf_ = *bare_fn;
227 let BareFnTy { unsafety, abi, lifetimes, inputs, output, variadic } = bf_;
228 BareFn(Box::new(BareFnTy {
David Tolnay05120ef2017-03-12 18:29:26 -0700229 unsafety: unsafety,
230 abi: abi,
231 lifetimes: lifetimes.lift(|l| folder.fold_lifetime_def(l)),
232 inputs: inputs.lift(|v| {
233 BareFnArg {
234 name: v.name.map(|n| folder.fold_ident(n)),
235 ty: folder.fold_ty(v.ty),
236 }
237 }),
238 output: folder.fold_fn_ret_ty(output),
239 variadic: variadic,
240 }))
gnzlbg9ae88d82017-01-26 20:45:17 +0100241 }
242 Path(maybe_qself, path) => {
243 Path(maybe_qself.map(|v| noop_fold_qself(folder, v)),
244 folder.fold_path(path))
245 }
246 Array(inner, len) => {
247 Array({
248 inner.lift(|v| folder.fold_ty(v))
249 },
250 folder.fold_const_expr(len))
251 }
252 TraitObject(bounds) => TraitObject(bounds.lift(|v| folder.fold_ty_param_bound(v))),
253 ImplTrait(bounds) => ImplTrait(bounds.lift(|v| folder.fold_ty_param_bound(v))),
254 Mac(mac) => Mac(folder.fold_mac(mac)),
255 }
256}
257
David Tolnay78488252017-01-29 11:22:59 -0800258fn noop_fold_qself<F: ?Sized + Folder>(folder: &mut F, QSelf { ty, position }: QSelf) -> QSelf {
gnzlbg9ae88d82017-01-26 20:45:17 +0100259 QSelf {
260 ty: Box::new(folder.fold_ty(*(ty))),
261 position: position,
262 }
263}
264
David Tolnay78488252017-01-29 11:22:59 -0800265pub fn noop_fold_generics<F: ?Sized + Folder>(folder: &mut F,
gnzlbg9ae88d82017-01-26 20:45:17 +0100266 Generics { lifetimes, ty_params, where_clause }: Generics)
David Tolnay05120ef2017-03-12 18:29:26 -0700267-> Generics{
David Tolnay7a1cda02017-01-29 11:50:31 -0800268 use WherePredicate::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100269 Generics {
270 lifetimes: lifetimes.lift(|l| folder.fold_lifetime_def(l)),
271 ty_params: ty_params.lift(|ty| {
272 TyParam {
273 attrs: ty.attrs.lift(|a| folder.fold_attribute(a)),
274 ident: folder.fold_ident(ty.ident),
David Tolnay05120ef2017-03-12 18:29:26 -0700275 bounds: ty.bounds.lift(|ty_pb| folder.fold_ty_param_bound(ty_pb)),
gnzlbg9ae88d82017-01-26 20:45:17 +0100276 default: ty.default.map(|v| folder.fold_ty(v)),
277 }
278 }),
279 where_clause: WhereClause {
David Tolnay05120ef2017-03-12 18:29:26 -0700280 predicates: where_clause.predicates.lift(|p| match p {
281 BoundPredicate(bound_predicate) => {
gnzlbg9ae88d82017-01-26 20:45:17 +0100282 BoundPredicate(WhereBoundPredicate {
283 bound_lifetimes: bound_predicate.bound_lifetimes
284 .lift(|l| folder.fold_lifetime_def(l)),
285 bounded_ty: folder.fold_ty(bound_predicate.bounded_ty),
286 bounds: bound_predicate.bounds
287 .lift(|ty_pb| folder.fold_ty_param_bound(ty_pb)),
288 })
289 }
David Tolnay05120ef2017-03-12 18:29:26 -0700290 RegionPredicate(region_predicate) => {
gnzlbg9ae88d82017-01-26 20:45:17 +0100291 RegionPredicate(WhereRegionPredicate {
292 lifetime: folder.fold_lifetime(region_predicate.lifetime),
293 bounds: region_predicate.bounds
294 .lift(|b| folder.fold_lifetime(b)),
295 })
296 }
David Tolnay05120ef2017-03-12 18:29:26 -0700297 EqPredicate(eq_predicate) => {
gnzlbg9ae88d82017-01-26 20:45:17 +0100298 EqPredicate(WhereEqPredicate {
299 lhs_ty: folder.fold_ty(eq_predicate.lhs_ty),
300 rhs_ty: folder.fold_ty(eq_predicate.rhs_ty),
301 })
302 }
David Tolnay05120ef2017-03-12 18:29:26 -0700303 }),
gnzlbg9ae88d82017-01-26 20:45:17 +0100304 },
305 }
306}
307
David Tolnay660b03e2017-01-29 11:26:30 -0800308pub fn noop_fold_ty_param_bound<F: ?Sized + Folder>(folder: &mut F,
309 bound: TyParamBound)
310 -> TyParamBound {
David Tolnay7a1cda02017-01-29 11:50:31 -0800311 use TyParamBound::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100312 match bound {
313 Trait(ty, modifier) => Trait(folder.fold_poly_trait_ref(ty), modifier),
314 Region(lifetime) => Region(folder.fold_lifetime(lifetime)),
315 }
316}
317
David Tolnay660b03e2017-01-29 11:26:30 -0800318pub fn noop_fold_poly_trait_ref<F: ?Sized + Folder>(folder: &mut F,
319 trait_ref: PolyTraitRef)
320 -> PolyTraitRef {
gnzlbg9ae88d82017-01-26 20:45:17 +0100321 PolyTraitRef {
David Tolnay05120ef2017-03-12 18:29:26 -0700322 bound_lifetimes: trait_ref.bound_lifetimes.lift(|bl| folder.fold_lifetime_def(bl)),
gnzlbg9ae88d82017-01-26 20:45:17 +0100323 trait_ref: folder.fold_path(trait_ref.trait_ref),
324 }
325}
326
David Tolnay78488252017-01-29 11:22:59 -0800327pub fn noop_fold_variant_data<F: ?Sized + Folder>(folder: &mut F,
David Tolnay660b03e2017-01-29 11:26:30 -0800328 data: VariantData)
329 -> VariantData {
David Tolnay7a1cda02017-01-29 11:50:31 -0800330 use VariantData::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100331 match data {
332 Struct(fields) => Struct(fields.lift(|f| folder.fold_field(f))),
333 Tuple(fields) => Tuple(fields.lift(|f| folder.fold_field(f))),
334 Unit => Unit,
335 }
336}
337
David Tolnay78488252017-01-29 11:22:59 -0800338pub fn noop_fold_field<F: ?Sized + Folder>(folder: &mut F, field: Field) -> Field {
gnzlbg9ae88d82017-01-26 20:45:17 +0100339 Field {
340 ident: field.ident.map(|i| folder.fold_ident(i)),
341 vis: noop_fold_vis(folder, field.vis),
342 attrs: field.attrs.lift(|a| folder.fold_attribute(a)),
343 ty: folder.fold_ty(field.ty),
344 }
345}
346
David Tolnay78488252017-01-29 11:22:59 -0800347pub fn noop_fold_variant<F: ?Sized + Folder>(folder: &mut F,
gnzlbg9ae88d82017-01-26 20:45:17 +0100348 Variant { ident, attrs, data, discriminant }: Variant)
David Tolnay05120ef2017-03-12 18:29:26 -0700349-> Variant{
gnzlbg9ae88d82017-01-26 20:45:17 +0100350 Variant {
351 ident: folder.fold_ident(ident),
352 attrs: attrs.lift(|v| folder.fold_attribute(v)),
353 data: folder.fold_variant_data(data),
354 discriminant: discriminant.map(|ce| folder.fold_const_expr(ce)),
355 }
356}
357
David Tolnay78488252017-01-29 11:22:59 -0800358pub fn noop_fold_lifetime<F: ?Sized + Folder>(folder: &mut F, _lifetime: Lifetime) -> Lifetime {
gnzlbg9ae88d82017-01-26 20:45:17 +0100359 Lifetime { ident: folder.fold_ident(_lifetime.ident) }
360}
361
David Tolnay78488252017-01-29 11:22:59 -0800362pub fn noop_fold_lifetime_def<F: ?Sized + Folder>(folder: &mut F,
gnzlbg9ae88d82017-01-26 20:45:17 +0100363 LifetimeDef { attrs, lifetime, bounds }: LifetimeDef)
David Tolnay05120ef2017-03-12 18:29:26 -0700364-> LifetimeDef{
gnzlbg9ae88d82017-01-26 20:45:17 +0100365 LifetimeDef {
366 attrs: attrs.lift(|x| folder.fold_attribute(x)),
367 lifetime: folder.fold_lifetime(lifetime),
368 bounds: bounds.lift(|l| folder.fold_lifetime(l)),
369 }
370}
371
David Tolnay78488252017-01-29 11:22:59 -0800372pub fn noop_fold_path<F: ?Sized + Folder>(folder: &mut F, Path { global, segments }: Path) -> Path {
gnzlbg9ae88d82017-01-26 20:45:17 +0100373 Path {
374 global: global,
375 segments: segments.lift(|s| folder.fold_path_segment(s)),
376 }
377}
378
David Tolnay78488252017-01-29 11:22:59 -0800379pub fn noop_fold_path_segment<F: ?Sized + Folder>(folder: &mut F,
David Tolnay660b03e2017-01-29 11:26:30 -0800380 PathSegment { ident, parameters }: PathSegment)
381 -> PathSegment {
gnzlbg9ae88d82017-01-26 20:45:17 +0100382 PathSegment {
383 ident: folder.fold_ident(ident),
384 parameters: folder.fold_path_parameters(parameters),
385 }
386}
387
David Tolnay78488252017-01-29 11:22:59 -0800388pub fn noop_fold_path_parameters<F: ?Sized + Folder>(folder: &mut F,
David Tolnay660b03e2017-01-29 11:26:30 -0800389 path_parameters: PathParameters)
390 -> PathParameters {
David Tolnay7a1cda02017-01-29 11:50:31 -0800391 use PathParameters::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100392 match path_parameters {
393 AngleBracketed(d) => {
394 let AngleBracketedParameterData { lifetimes, types, bindings } = d;
395 AngleBracketed(AngleBracketedParameterData {
David Tolnay05120ef2017-03-12 18:29:26 -0700396 lifetimes: lifetimes.into_iter()
397 .map(|l| folder.fold_lifetime(l))
398 .collect(),
399 types: types.lift(|ty| folder.fold_ty(ty)),
400 bindings: bindings.lift(|tb| folder.fold_assoc_type_binding(tb)),
401 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100402 }
403 Parenthesized(d) => {
404 let ParenthesizedParameterData { inputs, output } = d;
405 Parenthesized(ParenthesizedParameterData {
David Tolnay05120ef2017-03-12 18:29:26 -0700406 inputs: inputs.lift(|i| folder.fold_ty(i)),
407 output: output.map(|v| folder.fold_ty(v)),
408 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100409 }
410 }
411}
412
David Tolnay78488252017-01-29 11:22:59 -0800413pub fn noop_fold_assoc_type_binding<F: ?Sized + Folder>(folder: &mut F,
David Tolnay660b03e2017-01-29 11:26:30 -0800414 TypeBinding { ident, ty }: TypeBinding)
415 -> TypeBinding {
gnzlbg9ae88d82017-01-26 20:45:17 +0100416 TypeBinding {
417 ident: folder.fold_ident(ident),
418 ty: folder.fold_ty(ty),
419 }
420
421}
422
David Tolnay78488252017-01-29 11:22:59 -0800423pub fn noop_fold_attribute<F: ?Sized + Folder>(_: &mut F, _attr: Attribute) -> Attribute {
gnzlbg9ae88d82017-01-26 20:45:17 +0100424 _attr
425}
426
David Tolnay660b03e2017-01-29 11:26:30 -0800427pub fn noop_fold_fn_ret_ty<F: ?Sized + Folder>(folder: &mut F,
428 ret_ty: FunctionRetTy)
429 -> FunctionRetTy {
David Tolnay7a1cda02017-01-29 11:50:31 -0800430 use FunctionRetTy::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100431 match ret_ty {
432 Default => Default,
433 Ty(ty) => Ty(folder.fold_ty(ty)),
434 }
435}
436
David Tolnay78488252017-01-29 11:22:59 -0800437pub fn noop_fold_const_expr<F: ?Sized + Folder>(folder: &mut F, expr: ConstExpr) -> ConstExpr {
David Tolnay7a1cda02017-01-29 11:50:31 -0800438 use ConstExpr::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100439 match expr {
440 Call(f, args) => {
441 Call(f.lift(|e| folder.fold_const_expr(e)),
442 args.lift(|v| folder.fold_const_expr(v)))
443 }
444 Binary(op, lhs, rhs) => {
445 Binary(op,
446 lhs.lift(|e| folder.fold_const_expr(e)),
447 rhs.lift(|e| folder.fold_const_expr(e)))
448 }
449 Unary(op, e) => Unary(op, e.lift(|e| folder.fold_const_expr(e))),
450 Lit(l) => Lit(folder.fold_lit(l)),
451 Cast(e, ty) => {
452 Cast(e.lift(|e| folder.fold_const_expr(e)),
453 ty.lift(|v| folder.fold_ty(v)))
454 }
455 Path(p) => Path(folder.fold_path(p)),
456 Index(o, i) => {
457 Index(o.lift(|e| folder.fold_const_expr(e)),
458 i.lift(|e| folder.fold_const_expr(e)))
459 }
460 Paren(no_op) => Paren(no_op.lift(|e| folder.fold_const_expr(e))),
David Tolnay4a25fc22017-01-29 11:32:20 -0800461 Other(e) => Other(noop_fold_other_const_expr(folder, e)),
gnzlbg9ae88d82017-01-26 20:45:17 +0100462 }
463}
David Tolnay4a25fc22017-01-29 11:32:20 -0800464
465#[cfg(feature = "full")]
466fn noop_fold_other_const_expr<F: ?Sized + Folder>(folder: &mut F, e: Expr) -> Expr {
467 folder.fold_expr(e)
468}
469
470#[cfg(not(feature = "full"))]
David Tolnay05120ef2017-03-12 18:29:26 -0700471fn noop_fold_other_const_expr<F: ?Sized + Folder>(_: &mut F,
472 e: constant::Other)
473 -> constant::Other {
David Tolnay4a25fc22017-01-29 11:32:20 -0800474 e
475}
476
David Tolnay78488252017-01-29 11:22:59 -0800477pub fn noop_fold_lit<F: ?Sized + Folder>(_: &mut F, _lit: Lit) -> Lit {
gnzlbg9ae88d82017-01-26 20:45:17 +0100478 _lit
479}
480
David Tolnay78488252017-01-29 11:22:59 -0800481pub fn noop_fold_tt<F: ?Sized + Folder>(folder: &mut F, tt: TokenTree) -> TokenTree {
David Tolnay7a1cda02017-01-29 11:50:31 -0800482 use TokenTree::*;
483 use Token::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100484 match tt {
David Tolnay660b03e2017-01-29 11:26:30 -0800485 Token(token) => {
486 Token(match token {
David Tolnay05120ef2017-03-12 18:29:26 -0700487 Literal(lit) => Literal(folder.fold_lit(lit)),
488 Ident(ident) => Ident(folder.fold_ident(ident)),
489 Lifetime(ident) => Lifetime(folder.fold_ident(ident)),
490 x => x,
491 })
David Tolnay660b03e2017-01-29 11:26:30 -0800492 }
493 Delimited(super::Delimited { delim, tts }) => {
494 Delimited(super::Delimited {
David Tolnay05120ef2017-03-12 18:29:26 -0700495 delim: delim,
496 tts: tts.lift(|v| noop_fold_tt(folder, v)),
497 })
David Tolnay660b03e2017-01-29 11:26:30 -0800498 }
gnzlbg9ae88d82017-01-26 20:45:17 +0100499 }
500}
501
David Tolnay78488252017-01-29 11:22:59 -0800502pub fn noop_fold_mac<F: ?Sized + Folder>(folder: &mut F, Mac { path, tts }: Mac) -> Mac {
gnzlbg9ae88d82017-01-26 20:45:17 +0100503 Mac {
504 path: folder.fold_path(path),
505 tts: tts.lift(|tt| noop_fold_tt(folder, tt)),
506 }
507}
508
509#[cfg(feature = "full")]
David Tolnay660b03e2017-01-29 11:26:30 -0800510pub fn noop_fold_crate<F: ?Sized + Folder>(folder: &mut F,
511 Crate { shebang, attrs, items }: Crate)
512 -> Crate {
gnzlbg9ae88d82017-01-26 20:45:17 +0100513 Crate {
514 shebang: shebang,
515 attrs: attrs.lift(|a| folder.fold_attribute(a)),
516 items: items.lift(|i| folder.fold_item(i)),
517 }
518
519}
520
521#[cfg(feature = "full")]
David Tolnay78488252017-01-29 11:22:59 -0800522pub fn noop_fold_block<F: ?Sized + Folder>(folder: &mut F, block: Block) -> Block {
gnzlbg9ae88d82017-01-26 20:45:17 +0100523 Block { stmts: block.stmts.lift(|s| folder.fold_stmt(s)) }
524}
525
David Tolnay78488252017-01-29 11:22:59 -0800526fn noop_fold_vis<F: ?Sized + Folder>(folder: &mut F, vis: Visibility) -> Visibility {
David Tolnay7a1cda02017-01-29 11:50:31 -0800527 use Visibility::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100528 match vis {
529 Crate => Crate,
530 Inherited => Inherited,
531 Public => Public,
532 Restricted(path) => Restricted(path.lift(|p| folder.fold_path(p))),
533 }
534}
535
536#[cfg(feature = "full")]
David Tolnay660b03e2017-01-29 11:26:30 -0800537pub fn noop_fold_item<F: ?Sized + Folder>(folder: &mut F,
538 Item { ident, vis, attrs, node }: Item)
539 -> Item {
David Tolnay7a1cda02017-01-29 11:50:31 -0800540 use ItemKind::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100541 Item {
542 ident: folder.fold_ident(ident.clone()),
543 vis: noop_fold_vis(folder, vis),
544 attrs: attrs.lift(|a| folder.fold_attribute(a)),
545 node: match node {
546 ExternCrate(name) => ExternCrate(name.map(|i| folder.fold_ident(i))),
547 Use(view_path) => Use(Box::new(folder.fold_view_path(*view_path))),
548 Static(ty, mutability, expr) => {
549 Static(Box::new(folder.fold_ty(*ty)),
550 mutability,
551 expr.lift(|e| folder.fold_expr(e)))
552 }
553 Const(ty, expr) => {
554 Const(ty.lift(|ty| folder.fold_ty(ty)),
555 expr.lift(|e| folder.fold_expr(e)))
556 }
557 Fn(fn_decl, unsafety, constness, abi, generics, block) => {
558 Fn(fn_decl.lift(|v| folder.fold_fn_decl(v)),
559 unsafety,
560 constness,
561 abi,
562 folder.fold_generics(generics),
563 block.lift(|v| folder.fold_block(v)))
564 }
565 Mod(items) => Mod(items.map(|items| items.lift(|i| folder.fold_item(i)))),
566 ForeignMod(super::ForeignMod { abi, items }) => {
567 ForeignMod(super::ForeignMod {
David Tolnay05120ef2017-03-12 18:29:26 -0700568 abi: abi,
569 items: items.lift(|foreign_item| {
570 folder.fold_foreign_item(foreign_item)
571 }),
572 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100573 }
574 Ty(ty, generics) => {
575 Ty(ty.lift(|ty| folder.fold_ty(ty)),
576 folder.fold_generics(generics))
577 }
578 Enum(variants, generics) => {
David Tolnaya6aa8f12017-01-29 11:23:54 -0800579 Enum(variants.lift(|v| folder.fold_variant(v)),
gnzlbg9ae88d82017-01-26 20:45:17 +0100580 folder.fold_generics(generics))
581 }
582 Struct(variant_data, generics) => {
David Tolnaya6aa8f12017-01-29 11:23:54 -0800583 Struct(folder.fold_variant_data(variant_data),
gnzlbg9ae88d82017-01-26 20:45:17 +0100584 folder.fold_generics(generics))
585 }
586 Union(variant_data, generics) => {
David Tolnaya6aa8f12017-01-29 11:23:54 -0800587 Union(folder.fold_variant_data(variant_data),
gnzlbg9ae88d82017-01-26 20:45:17 +0100588 folder.fold_generics(generics))
589 }
590 Trait(unsafety, generics, typbs, trait_items) => {
591 Trait(unsafety,
592 folder.fold_generics(generics),
593 typbs.lift(|typb| folder.fold_ty_param_bound(typb)),
594 trait_items.lift(|ti| folder.fold_trait_item(ti)))
595 }
596 DefaultImpl(unsafety, path) => DefaultImpl(unsafety, folder.fold_path(path)),
597 Impl(unsafety, impl_polarity, generics, path, ty, impl_items) => {
598 Impl(unsafety,
599 impl_polarity,
600 folder.fold_generics(generics),
601 path.map(|p| folder.fold_path(p)),
602 ty.lift(|ty| folder.fold_ty(ty)),
603 impl_items.lift(|i| folder.fold_impl_item(i)))
604 }
605 Mac(mac) => Mac(folder.fold_mac(mac)),
606 },
607 }
608}
609
gnzlbg9ae88d82017-01-26 20:45:17 +0100610#[cfg(feature = "full")]
David Tolnay78488252017-01-29 11:22:59 -0800611pub fn noop_fold_expr<F: ?Sized + Folder>(folder: &mut F, Expr { node, attrs }: Expr) -> Expr {
David Tolnay7a1cda02017-01-29 11:50:31 -0800612 use ExprKind::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100613 Expr {
614 node: match node {
615 ExprKind::Box(e) => ExprKind::Box(e.lift(|e| folder.fold_expr(e))),
616 InPlace(place, value) => {
617 InPlace(place.lift(|e| folder.fold_expr(e)),
618 value.lift(|e| folder.fold_expr(e)))
619 }
620 Array(array) => Array(array.lift(|e| folder.fold_expr(e))),
621 Call(function, args) => {
622 Call(function.lift(|e| folder.fold_expr(e)),
623 args.lift(|e| folder.fold_expr(e)))
624 }
625 MethodCall(method, tys, args) => {
626 MethodCall(folder.fold_ident(method),
627 tys.lift(|t| folder.fold_ty(t)),
628 args.lift(|e| folder.fold_expr(e)))
629 }
630 Tup(args) => Tup(args.lift(|e| folder.fold_expr(e))),
631 Binary(bop, lhs, rhs) => {
632 Binary(bop,
633 lhs.lift(|e| folder.fold_expr(e)),
634 rhs.lift(|e| folder.fold_expr(e)))
635 }
636 Unary(uop, e) => Unary(uop, e.lift(|e| folder.fold_expr(e))),
637 Lit(lit) => Lit(folder.fold_lit(lit)),
638 Cast(e, ty) => {
639 Cast(e.lift(|e| folder.fold_expr(e)),
640 ty.lift(|t| folder.fold_ty(t)))
641 }
642 Type(e, ty) => {
643 Type(e.lift(|e| folder.fold_expr(e)),
644 ty.lift(|t| folder.fold_ty(t)))
645 }
646 If(e, if_block, else_block) => {
647 If(e.lift(|e| folder.fold_expr(e)),
648 folder.fold_block(if_block),
649 else_block.map(|v| v.lift(|e| folder.fold_expr(e))))
650 }
651 IfLet(pat, expr, block, else_block) => {
652 IfLet(pat.lift(|p| folder.fold_pat(p)),
653 expr.lift(|e| folder.fold_expr(e)),
654 folder.fold_block(block),
655 else_block.map(|v| v.lift(|e| folder.fold_expr(e))))
656 }
657 While(e, block, label) => {
658 While(e.lift(|e| folder.fold_expr(e)),
659 folder.fold_block(block),
660 label.map(|i| folder.fold_ident(i)))
661 }
662 WhileLet(pat, expr, block, label) => {
663 WhileLet(pat.lift(|p| folder.fold_pat(p)),
664 expr.lift(|e| folder.fold_expr(e)),
665 folder.fold_block(block),
666 label.map(|i| folder.fold_ident(i)))
667 }
668 ForLoop(pat, expr, block, label) => {
669 ForLoop(pat.lift(|p| folder.fold_pat(p)),
670 expr.lift(|e| folder.fold_expr(e)),
671 folder.fold_block(block),
672 label.map(|i| folder.fold_ident(i)))
673 }
674 Loop(block, label) => {
675 Loop(folder.fold_block(block),
676 label.map(|i| folder.fold_ident(i)))
677 }
678 Match(e, arms) => {
679 Match(e.lift(|e| folder.fold_expr(e)),
680 arms.lift(|Arm { attrs, pats, guard, body }: Arm| {
681 Arm {
682 attrs: attrs.lift(|a| folder.fold_attribute(a)),
683 pats: pats.lift(|p| folder.fold_pat(p)),
684 guard: guard.map(|v| v.lift(|e| folder.fold_expr(e))),
685 body: body.lift(|e| folder.fold_expr(e)),
686 }
687 }))
688 }
Arnavion02ef13f2017-04-25 00:54:31 -0700689 Catch(block) => {
690 Catch(folder.fold_block(block))
691 }
gnzlbg9ae88d82017-01-26 20:45:17 +0100692 Closure(capture_by, fn_decl, expr) => {
693 Closure(capture_by,
694 fn_decl.lift(|v| folder.fold_fn_decl(v)),
695 expr.lift(|e| folder.fold_expr(e)))
696 }
697 Block(unsafety, block) => Block(unsafety, folder.fold_block(block)),
698 Assign(lhs, rhs) => {
699 Assign(lhs.lift(|e| folder.fold_expr(e)),
700 rhs.lift(|e| folder.fold_expr(e)))
701 }
702 AssignOp(bop, lhs, rhs) => {
703 AssignOp(bop,
704 lhs.lift(|e| folder.fold_expr(e)),
705 rhs.lift(|e| folder.fold_expr(e)))
706 }
707 Field(expr, name) => Field(expr.lift(|e| folder.fold_expr(e)), folder.fold_ident(name)),
708 TupField(expr, index) => TupField(expr.lift(|e| folder.fold_expr(e)), index),
709 Index(expr, index) => {
710 Index(expr.lift(|e| folder.fold_expr(e)),
711 index.lift(|e| folder.fold_expr(e)))
712 }
713 Range(lhs, rhs, limits) => {
714 Range(lhs.map(|v| v.lift(|e| folder.fold_expr(e))),
715 rhs.map(|v| v.lift(|e| folder.fold_expr(e))),
716 limits)
717 }
718 Path(qself, path) => {
719 Path(qself.map(|v| noop_fold_qself(folder, v)),
720 folder.fold_path(path))
721 }
722 AddrOf(mutability, expr) => AddrOf(mutability, expr.lift(|e| folder.fold_expr(e))),
723 Break(label, expr) => {
724 Break(label.map(|i| folder.fold_ident(i)),
725 expr.map(|v| v.lift(|e| folder.fold_expr(e))))
726 }
727 Continue(label) => Continue(label.map(|i| folder.fold_ident(i))),
728 Ret(expr) => Ret(expr.map(|v| v.lift(|e| folder.fold_expr(e)))),
729 ExprKind::Mac(mac) => ExprKind::Mac(folder.fold_mac(mac)),
730 Struct(path, fields, expr) => {
731 Struct(folder.fold_path(path),
732 fields.lift(|FieldValue { ident, expr, is_shorthand, attrs }: FieldValue| {
733 FieldValue {
734 ident: folder.fold_ident(ident),
735 expr: folder.fold_expr(expr),
736 is_shorthand: is_shorthand,
737 attrs: attrs.lift(|v| folder.fold_attribute(v)),
738 }
739 }),
740 expr.map(|v| v.lift(|e| folder.fold_expr(e))))
741 }
742 Repeat(element, number) => {
743 Repeat(element.lift(|e| folder.fold_expr(e)),
744 number.lift(|e| folder.fold_expr(e)))
745 }
746 Paren(expr) => Paren(expr.lift(|e| folder.fold_expr(e))),
747 Try(expr) => Try(expr.lift(|e| folder.fold_expr(e))),
748 },
749 attrs: attrs.into_iter().map(|a| folder.fold_attribute(a)).collect(),
750 }
751}
752
gnzlbg9ae88d82017-01-26 20:45:17 +0100753#[cfg(feature = "full")]
David Tolnay78488252017-01-29 11:22:59 -0800754pub fn noop_fold_foreign_item<F: ?Sized + Folder>(folder: &mut F,
gnzlbg9ae88d82017-01-26 20:45:17 +0100755 ForeignItem { ident, attrs, node, vis }: ForeignItem)
David Tolnay05120ef2017-03-12 18:29:26 -0700756-> ForeignItem{
gnzlbg9ae88d82017-01-26 20:45:17 +0100757 ForeignItem {
758 ident: folder.fold_ident(ident),
759 attrs: attrs.into_iter().map(|a| folder.fold_attribute(a)).collect(),
760 node: match node {
761 ForeignItemKind::Fn(fn_dcl, generics) => {
762 ForeignItemKind::Fn(fn_dcl.lift(|v| folder.fold_fn_decl(v)),
763 folder.fold_generics(generics))
764 }
765 ForeignItemKind::Static(ty, mutability) => {
766 ForeignItemKind::Static(ty.lift(|v| folder.fold_ty(v)), mutability)
767 }
768 },
769 vis: noop_fold_vis(folder, vis),
770 }
771}
772
gnzlbg9ae88d82017-01-26 20:45:17 +0100773#[cfg(feature = "full")]
David Tolnay78488252017-01-29 11:22:59 -0800774pub fn noop_fold_pat<F: ?Sized + Folder>(folder: &mut F, pat: Pat) -> Pat {
David Tolnay7a1cda02017-01-29 11:50:31 -0800775 use Pat::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100776 match pat {
777 Wild => Wild,
778 Ident(binding_mode, ident, pat) => {
779 Ident(binding_mode,
780 folder.fold_ident(ident),
781 pat.map(|p| p.lift(|p| folder.fold_pat(p))))
782 }
783 Struct(path, field_patterns, dots) => {
784 Struct(folder.fold_path(path),
785 field_patterns.lift(|FieldPat { ident, pat, is_shorthand, attrs }: FieldPat| {
786 FieldPat {
787 ident: folder.fold_ident(ident),
788 pat: pat.lift(|p| folder.fold_pat(p)),
789 is_shorthand: is_shorthand,
790 attrs: attrs.lift(|a| folder.fold_attribute(a)),
791 }
792 }),
793 dots)
794 }
795 TupleStruct(path, pats, len) => {
796 TupleStruct(folder.fold_path(path),
797 pats.lift(|p| folder.fold_pat(p)),
798 len)
799 }
800 Path(qself, path) => {
801 Path(qself.map(|v| noop_fold_qself(folder, v)),
802 folder.fold_path(path))
803 }
804 Tuple(pats, len) => Tuple(pats.lift(|p| folder.fold_pat(p)), len),
805 Box(b) => Box(b.lift(|p| folder.fold_pat(p))),
806 Ref(b, mutability) => Ref(b.lift(|p| folder.fold_pat(p)), mutability),
807 Lit(expr) => Lit(expr.lift(|e| folder.fold_expr(e))),
808 Range(l, r) => {
809 Range(l.lift(|e| folder.fold_expr(e)),
810 r.lift(|e| folder.fold_expr(e)))
811 }
812 Slice(lefts, pat, rights) => {
813 Slice(lefts.lift(|p| folder.fold_pat(p)),
814 pat.map(|v| v.lift(|p| folder.fold_pat(p))),
815 rights.lift(|p| folder.fold_pat(p)))
816 }
817 Mac(mac) => Mac(folder.fold_mac(mac)),
818 }
819}
820
gnzlbg9ae88d82017-01-26 20:45:17 +0100821#[cfg(feature = "full")]
David Tolnay78488252017-01-29 11:22:59 -0800822pub fn noop_fold_fn_decl<F: ?Sized + Folder>(folder: &mut F,
David Tolnay660b03e2017-01-29 11:26:30 -0800823 FnDecl { inputs, output, variadic }: FnDecl)
824 -> FnDecl {
gnzlbg9ae88d82017-01-26 20:45:17 +0100825
826 FnDecl {
827 inputs: inputs.lift(|a| {
David Tolnay7a1cda02017-01-29 11:50:31 -0800828 use FnArg::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100829 match a {
830 SelfRef(lifetime, mutability) => {
831 SelfRef(lifetime.map(|v| folder.fold_lifetime(v)), mutability)
832 }
833 SelfValue(mutability) => SelfValue(mutability),
834 Captured(pat, ty) => Captured(folder.fold_pat(pat), folder.fold_ty(ty)),
835 Ignored(ty) => Ignored(folder.fold_ty(ty)),
836 }
837 }),
838 output: folder.fold_fn_ret_ty(output),
839 variadic: variadic,
840 }
841
842}
843
gnzlbg9ae88d82017-01-26 20:45:17 +0100844#[cfg(feature = "full")]
David Tolnay78488252017-01-29 11:22:59 -0800845pub fn noop_fold_trait_item<F: ?Sized + Folder>(folder: &mut F,
David Tolnay660b03e2017-01-29 11:26:30 -0800846 TraitItem { ident, attrs, node }: TraitItem)
847 -> TraitItem {
David Tolnay7a1cda02017-01-29 11:50:31 -0800848 use TraitItemKind::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100849 TraitItem {
850 ident: folder.fold_ident(ident),
851 attrs: attrs.lift(|v| folder.fold_attribute(v)),
852 node: match node {
853 Const(ty, expr) => Const(folder.fold_ty(ty), expr.map(|v| folder.fold_expr(v))),
854 Method(sig, block) => {
855 Method(folder.fold_method_sig(sig),
856 block.map(|v| folder.fold_block(v)))
857 }
858 Type(ty_pbs, ty) => {
859 Type(ty_pbs.lift(|v| folder.fold_ty_param_bound(v)),
860 ty.map(|v| folder.fold_ty(v)))
861 }
David Tolnay70925b72017-01-29 11:20:54 -0800862 Macro(mac) => Macro(folder.fold_mac(mac)),
gnzlbg9ae88d82017-01-26 20:45:17 +0100863 },
864 }
865}
866
gnzlbg9ae88d82017-01-26 20:45:17 +0100867#[cfg(feature = "full")]
David Tolnay78488252017-01-29 11:22:59 -0800868pub fn noop_fold_impl_item<F: ?Sized + Folder>(folder: &mut F,
gnzlbg9ae88d82017-01-26 20:45:17 +0100869 ImplItem { ident, vis, defaultness, attrs, node }: ImplItem)
David Tolnay05120ef2017-03-12 18:29:26 -0700870-> ImplItem{
David Tolnay7a1cda02017-01-29 11:50:31 -0800871 use ImplItemKind::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100872 ImplItem {
873 ident: folder.fold_ident(ident),
874 vis: noop_fold_vis(folder, vis),
875 defaultness: defaultness,
876 attrs: attrs.lift(|v| folder.fold_attribute(v)),
877 node: match node {
878 Const(ty, expr) => Const(folder.fold_ty(ty), folder.fold_expr(expr)),
David Tolnay660b03e2017-01-29 11:26:30 -0800879 Method(sig, block) => Method(folder.fold_method_sig(sig), folder.fold_block(block)),
gnzlbg9ae88d82017-01-26 20:45:17 +0100880 Type(ty) => Type(folder.fold_ty(ty)),
881 Macro(mac) => Macro(folder.fold_mac(mac)),
882 },
883 }
884}
885
gnzlbg9ae88d82017-01-26 20:45:17 +0100886#[cfg(feature = "full")]
David Tolnay05120ef2017-03-12 18:29:26 -0700887pub fn noop_fold_method_sig<F: ?Sized + Folder>(folder: &mut F, MethodSig{unsafety, constness, abi, decl, generics}:MethodSig) -> MethodSig{
gnzlbg9ae88d82017-01-26 20:45:17 +0100888 MethodSig {
889 unsafety: unsafety,
890 constness: constness,
891 abi: abi,
892 decl: folder.fold_fn_decl(decl),
893 generics: folder.fold_generics(generics),
894 }
895
896}
897
gnzlbg9ae88d82017-01-26 20:45:17 +0100898#[cfg(feature = "full")]
David Tolnay78488252017-01-29 11:22:59 -0800899pub fn noop_fold_stmt<F: ?Sized + Folder>(folder: &mut F, stmt: Stmt) -> Stmt {
David Tolnay7a1cda02017-01-29 11:50:31 -0800900 use Stmt::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100901 match stmt {
902 Local(local) => Local(local.lift(|l| folder.fold_local(l))),
903 Item(item) => Item(item.lift(|v| folder.fold_item(v))),
904 Expr(expr) => Expr(expr.lift(|v| folder.fold_expr(v))),
905 Semi(expr) => Semi(expr.lift(|v| folder.fold_expr(v))),
906 Mac(mac_stmt) => {
907 Mac(mac_stmt.lift(|(mac, style, attrs)| {
David Tolnay05120ef2017-03-12 18:29:26 -0700908 (folder.fold_mac(mac),
909 style,
910 attrs.lift(|a| folder.fold_attribute(a)))
911 }))
gnzlbg9ae88d82017-01-26 20:45:17 +0100912 }
913 }
914
915}
916
gnzlbg9ae88d82017-01-26 20:45:17 +0100917#[cfg(feature = "full")]
David Tolnay660b03e2017-01-29 11:26:30 -0800918pub fn noop_fold_local<F: ?Sized + Folder>(folder: &mut F,
919 Local { pat, ty, init, attrs }: Local)
920 -> Local {
gnzlbg9ae88d82017-01-26 20:45:17 +0100921 Local {
922 pat: pat.lift(|v| folder.fold_pat(v)),
923 ty: ty.map(|v| v.lift(|t| folder.fold_ty(t))),
924 init: init.map(|v| v.lift(|e| folder.fold_expr(e))),
925 attrs: attrs.lift(|a| folder.fold_attribute(a)),
926 }
927}
928
929#[cfg(feature = "full")]
David Tolnay78488252017-01-29 11:22:59 -0800930pub fn noop_fold_view_path<F: ?Sized + Folder>(folder: &mut F, view_path: ViewPath) -> ViewPath {
David Tolnay7a1cda02017-01-29 11:50:31 -0800931 use ViewPath::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100932 match view_path {
933 Simple(path, ident) => Simple(folder.fold_path(path), ident.map(|i| folder.fold_ident(i))),
934 Glob(path) => Glob(folder.fold_path(path)),
935 List(path, items) => {
936 List(folder.fold_path(path),
937 items.lift(|PathListItem { name, rename }: PathListItem| {
David Tolnay05120ef2017-03-12 18:29:26 -0700938 PathListItem {
939 name: folder.fold_ident(name),
940 rename: rename.map(|i| folder.fold_ident(i)),
941 }
942 }))
gnzlbg9ae88d82017-01-26 20:45:17 +0100943 }
944 }
945}