blob: 3a5c763ac9c6960fa7cb0443b3bed50b1d81db9d [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 {
Alex Crichton62a0a592017-05-22 13:58:53 -0700201 use ty::*;
David Tolnay7a1cda02017-01-29 11:50:31 -0800202 use Ty::*;
Alex Crichton62a0a592017-05-22 13:58:53 -0700203
gnzlbg9ae88d82017-01-26 20:45:17 +0100204 match ty {
Alex Crichton62a0a592017-05-22 13:58:53 -0700205 Slice(TySlice { ty }) => {
206 Slice(TySlice {
207 ty: ty.lift(|v| folder.fold_ty(v)),
208 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100209 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700210 Paren(TyParen { ty }) => {
211 Paren(TyParen {
212 ty: ty.lift(|v| folder.fold_ty(v)),
213 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100214 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700215 Ptr(TyPtr { ty }) => {
216 let ty = *ty;
217 let MutTy { ty, mutability } = ty;
218 Ptr(TyPtr {
219 ty: Box::new(MutTy {
220 ty: folder.fold_ty(ty),
221 mutability: mutability,
222 }),
223 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100224 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700225 Rptr(TyRptr { lifetime, ty }) => {
226 let ty = *ty;
227 let MutTy { ty, mutability } = ty;
228 Rptr(TyRptr {
229 lifetime: lifetime.map(|l| folder.fold_lifetime(l)),
230 ty: Box::new(MutTy {
231 ty: folder.fold_ty(ty),
232 mutability: mutability,
233 }),
234 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100235 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700236 Never(t) => Never(t),
237 Infer(t) => Infer(t),
238 Tup(TyTup { tys }) => {
239 Tup(TyTup {
240 tys: tys.lift(|x| folder.fold_ty(x)),
241 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100242 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700243 BareFn(TyBareFn { ty }) => {
244 let ty = *ty;
245 let BareFnTy { unsafety, abi, lifetimes, inputs, output, variadic } = ty;
246 BareFn(TyBareFn {
247 ty: Box::new(BareFnTy {
248 unsafety: unsafety,
249 abi: abi,
250 lifetimes: lifetimes.lift(|l| folder.fold_lifetime_def(l)),
251 inputs: inputs.lift(|v| {
252 BareFnArg {
253 name: v.name.map(|n| folder.fold_ident(n)),
254 ty: folder.fold_ty(v.ty),
255 }
256 }),
257 output: folder.fold_fn_ret_ty(output),
258 variadic: variadic,
259 }),
260 })
261 }
262 Path(TyPath { qself, path }) => {
263 Path(TyPath {
264 qself: qself.map(|v| noop_fold_qself(folder, v)),
265 path: folder.fold_path(path),
266 })
267 }
268 Array(TyArray { ty, amt }) => {
269 Array(TyArray {
270 ty: ty.lift(|v| folder.fold_ty(v)),
271 amt: folder.fold_const_expr(amt),
272 })
273 }
274 TraitObject(TyTraitObject { bounds }) => {
275 TraitObject(TyTraitObject {
276 bounds: bounds.lift(|v| folder.fold_ty_param_bound(v)),
277 })
278 }
279 ImplTrait(TyImplTrait { bounds }) => {
280 ImplTrait(TyImplTrait {
281 bounds: bounds.lift(|v| folder.fold_ty_param_bound(v)),
282 })
283 }
gnzlbg9ae88d82017-01-26 20:45:17 +0100284 Mac(mac) => Mac(folder.fold_mac(mac)),
285 }
286}
287
David Tolnay78488252017-01-29 11:22:59 -0800288fn noop_fold_qself<F: ?Sized + Folder>(folder: &mut F, QSelf { ty, position }: QSelf) -> QSelf {
gnzlbg9ae88d82017-01-26 20:45:17 +0100289 QSelf {
290 ty: Box::new(folder.fold_ty(*(ty))),
291 position: position,
292 }
293}
294
David Tolnay78488252017-01-29 11:22:59 -0800295pub fn noop_fold_generics<F: ?Sized + Folder>(folder: &mut F,
gnzlbg9ae88d82017-01-26 20:45:17 +0100296 Generics { lifetimes, ty_params, where_clause }: Generics)
David Tolnay05120ef2017-03-12 18:29:26 -0700297-> Generics{
David Tolnay7a1cda02017-01-29 11:50:31 -0800298 use WherePredicate::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100299 Generics {
300 lifetimes: lifetimes.lift(|l| folder.fold_lifetime_def(l)),
301 ty_params: ty_params.lift(|ty| {
302 TyParam {
303 attrs: ty.attrs.lift(|a| folder.fold_attribute(a)),
304 ident: folder.fold_ident(ty.ident),
David Tolnay05120ef2017-03-12 18:29:26 -0700305 bounds: ty.bounds.lift(|ty_pb| folder.fold_ty_param_bound(ty_pb)),
gnzlbg9ae88d82017-01-26 20:45:17 +0100306 default: ty.default.map(|v| folder.fold_ty(v)),
307 }
308 }),
309 where_clause: WhereClause {
David Tolnay05120ef2017-03-12 18:29:26 -0700310 predicates: where_clause.predicates.lift(|p| match p {
311 BoundPredicate(bound_predicate) => {
gnzlbg9ae88d82017-01-26 20:45:17 +0100312 BoundPredicate(WhereBoundPredicate {
313 bound_lifetimes: bound_predicate.bound_lifetimes
314 .lift(|l| folder.fold_lifetime_def(l)),
315 bounded_ty: folder.fold_ty(bound_predicate.bounded_ty),
316 bounds: bound_predicate.bounds
317 .lift(|ty_pb| folder.fold_ty_param_bound(ty_pb)),
318 })
319 }
David Tolnay05120ef2017-03-12 18:29:26 -0700320 RegionPredicate(region_predicate) => {
gnzlbg9ae88d82017-01-26 20:45:17 +0100321 RegionPredicate(WhereRegionPredicate {
322 lifetime: folder.fold_lifetime(region_predicate.lifetime),
323 bounds: region_predicate.bounds
324 .lift(|b| folder.fold_lifetime(b)),
325 })
326 }
David Tolnay05120ef2017-03-12 18:29:26 -0700327 EqPredicate(eq_predicate) => {
gnzlbg9ae88d82017-01-26 20:45:17 +0100328 EqPredicate(WhereEqPredicate {
329 lhs_ty: folder.fold_ty(eq_predicate.lhs_ty),
330 rhs_ty: folder.fold_ty(eq_predicate.rhs_ty),
331 })
332 }
David Tolnay05120ef2017-03-12 18:29:26 -0700333 }),
gnzlbg9ae88d82017-01-26 20:45:17 +0100334 },
335 }
336}
337
David Tolnay660b03e2017-01-29 11:26:30 -0800338pub fn noop_fold_ty_param_bound<F: ?Sized + Folder>(folder: &mut F,
339 bound: TyParamBound)
340 -> TyParamBound {
David Tolnay7a1cda02017-01-29 11:50:31 -0800341 use TyParamBound::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100342 match bound {
343 Trait(ty, modifier) => Trait(folder.fold_poly_trait_ref(ty), modifier),
344 Region(lifetime) => Region(folder.fold_lifetime(lifetime)),
345 }
346}
347
David Tolnay660b03e2017-01-29 11:26:30 -0800348pub fn noop_fold_poly_trait_ref<F: ?Sized + Folder>(folder: &mut F,
349 trait_ref: PolyTraitRef)
350 -> PolyTraitRef {
gnzlbg9ae88d82017-01-26 20:45:17 +0100351 PolyTraitRef {
David Tolnay05120ef2017-03-12 18:29:26 -0700352 bound_lifetimes: trait_ref.bound_lifetimes.lift(|bl| folder.fold_lifetime_def(bl)),
gnzlbg9ae88d82017-01-26 20:45:17 +0100353 trait_ref: folder.fold_path(trait_ref.trait_ref),
354 }
355}
356
David Tolnay78488252017-01-29 11:22:59 -0800357pub fn noop_fold_variant_data<F: ?Sized + Folder>(folder: &mut F,
David Tolnay660b03e2017-01-29 11:26:30 -0800358 data: VariantData)
359 -> VariantData {
David Tolnay7a1cda02017-01-29 11:50:31 -0800360 use VariantData::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100361 match data {
362 Struct(fields) => Struct(fields.lift(|f| folder.fold_field(f))),
363 Tuple(fields) => Tuple(fields.lift(|f| folder.fold_field(f))),
364 Unit => Unit,
365 }
366}
367
David Tolnay78488252017-01-29 11:22:59 -0800368pub fn noop_fold_field<F: ?Sized + Folder>(folder: &mut F, field: Field) -> Field {
gnzlbg9ae88d82017-01-26 20:45:17 +0100369 Field {
370 ident: field.ident.map(|i| folder.fold_ident(i)),
371 vis: noop_fold_vis(folder, field.vis),
372 attrs: field.attrs.lift(|a| folder.fold_attribute(a)),
373 ty: folder.fold_ty(field.ty),
374 }
375}
376
David Tolnay78488252017-01-29 11:22:59 -0800377pub fn noop_fold_variant<F: ?Sized + Folder>(folder: &mut F,
gnzlbg9ae88d82017-01-26 20:45:17 +0100378 Variant { ident, attrs, data, discriminant }: Variant)
David Tolnay05120ef2017-03-12 18:29:26 -0700379-> Variant{
gnzlbg9ae88d82017-01-26 20:45:17 +0100380 Variant {
381 ident: folder.fold_ident(ident),
382 attrs: attrs.lift(|v| folder.fold_attribute(v)),
383 data: folder.fold_variant_data(data),
384 discriminant: discriminant.map(|ce| folder.fold_const_expr(ce)),
385 }
386}
387
David Tolnay78488252017-01-29 11:22:59 -0800388pub fn noop_fold_lifetime<F: ?Sized + Folder>(folder: &mut F, _lifetime: Lifetime) -> Lifetime {
gnzlbg9ae88d82017-01-26 20:45:17 +0100389 Lifetime { ident: folder.fold_ident(_lifetime.ident) }
390}
391
David Tolnay78488252017-01-29 11:22:59 -0800392pub fn noop_fold_lifetime_def<F: ?Sized + Folder>(folder: &mut F,
gnzlbg9ae88d82017-01-26 20:45:17 +0100393 LifetimeDef { attrs, lifetime, bounds }: LifetimeDef)
David Tolnay05120ef2017-03-12 18:29:26 -0700394-> LifetimeDef{
gnzlbg9ae88d82017-01-26 20:45:17 +0100395 LifetimeDef {
396 attrs: attrs.lift(|x| folder.fold_attribute(x)),
397 lifetime: folder.fold_lifetime(lifetime),
398 bounds: bounds.lift(|l| folder.fold_lifetime(l)),
399 }
400}
401
David Tolnay78488252017-01-29 11:22:59 -0800402pub fn noop_fold_path<F: ?Sized + Folder>(folder: &mut F, Path { global, segments }: Path) -> Path {
gnzlbg9ae88d82017-01-26 20:45:17 +0100403 Path {
404 global: global,
405 segments: segments.lift(|s| folder.fold_path_segment(s)),
406 }
407}
408
David Tolnay78488252017-01-29 11:22:59 -0800409pub fn noop_fold_path_segment<F: ?Sized + Folder>(folder: &mut F,
David Tolnay660b03e2017-01-29 11:26:30 -0800410 PathSegment { ident, parameters }: PathSegment)
411 -> PathSegment {
gnzlbg9ae88d82017-01-26 20:45:17 +0100412 PathSegment {
413 ident: folder.fold_ident(ident),
414 parameters: folder.fold_path_parameters(parameters),
415 }
416}
417
David Tolnay78488252017-01-29 11:22:59 -0800418pub fn noop_fold_path_parameters<F: ?Sized + Folder>(folder: &mut F,
David Tolnay660b03e2017-01-29 11:26:30 -0800419 path_parameters: PathParameters)
420 -> PathParameters {
David Tolnay7a1cda02017-01-29 11:50:31 -0800421 use PathParameters::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100422 match path_parameters {
423 AngleBracketed(d) => {
424 let AngleBracketedParameterData { lifetimes, types, bindings } = d;
425 AngleBracketed(AngleBracketedParameterData {
David Tolnay05120ef2017-03-12 18:29:26 -0700426 lifetimes: lifetimes.into_iter()
427 .map(|l| folder.fold_lifetime(l))
428 .collect(),
429 types: types.lift(|ty| folder.fold_ty(ty)),
430 bindings: bindings.lift(|tb| folder.fold_assoc_type_binding(tb)),
431 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100432 }
433 Parenthesized(d) => {
434 let ParenthesizedParameterData { inputs, output } = d;
435 Parenthesized(ParenthesizedParameterData {
David Tolnay05120ef2017-03-12 18:29:26 -0700436 inputs: inputs.lift(|i| folder.fold_ty(i)),
437 output: output.map(|v| folder.fold_ty(v)),
438 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100439 }
440 }
441}
442
David Tolnay78488252017-01-29 11:22:59 -0800443pub fn noop_fold_assoc_type_binding<F: ?Sized + Folder>(folder: &mut F,
David Tolnay660b03e2017-01-29 11:26:30 -0800444 TypeBinding { ident, ty }: TypeBinding)
445 -> TypeBinding {
gnzlbg9ae88d82017-01-26 20:45:17 +0100446 TypeBinding {
447 ident: folder.fold_ident(ident),
448 ty: folder.fold_ty(ty),
449 }
450
451}
452
David Tolnay78488252017-01-29 11:22:59 -0800453pub fn noop_fold_attribute<F: ?Sized + Folder>(_: &mut F, _attr: Attribute) -> Attribute {
gnzlbg9ae88d82017-01-26 20:45:17 +0100454 _attr
455}
456
David Tolnay660b03e2017-01-29 11:26:30 -0800457pub fn noop_fold_fn_ret_ty<F: ?Sized + Folder>(folder: &mut F,
458 ret_ty: FunctionRetTy)
459 -> FunctionRetTy {
David Tolnay7a1cda02017-01-29 11:50:31 -0800460 use FunctionRetTy::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100461 match ret_ty {
462 Default => Default,
463 Ty(ty) => Ty(folder.fold_ty(ty)),
464 }
465}
466
David Tolnay78488252017-01-29 11:22:59 -0800467pub fn noop_fold_const_expr<F: ?Sized + Folder>(folder: &mut F, expr: ConstExpr) -> ConstExpr {
Alex Crichton62a0a592017-05-22 13:58:53 -0700468 use constant::*;
469 use constant::ConstExpr::*;
470
gnzlbg9ae88d82017-01-26 20:45:17 +0100471 match expr {
Alex Crichton62a0a592017-05-22 13:58:53 -0700472 Call(ConstCall { func, args }) => {
473 Call(ConstCall {
474 func: func.lift(|e| folder.fold_const_expr(e)),
475 args: args.lift(|v| folder.fold_const_expr(v)),
476 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100477 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700478 Binary(ConstBinary { op, left, right }) => {
479 Binary(ConstBinary {
480 op: op,
481 left: left.lift(|e| folder.fold_const_expr(e)),
482 right: right.lift(|e| folder.fold_const_expr(e)),
483 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100484 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700485 Unary(ConstUnary { op, expr }) => {
486 Unary(ConstUnary {
487 op: op,
488 expr: expr.lift(|e| folder.fold_const_expr(e)),
489 })
490 }
gnzlbg9ae88d82017-01-26 20:45:17 +0100491 Lit(l) => Lit(folder.fold_lit(l)),
Alex Crichton62a0a592017-05-22 13:58:53 -0700492 Cast(ConstCast { expr, ty }) => {
493 Cast(ConstCast {
494 expr: expr.lift(|e| folder.fold_const_expr(e)),
495 ty: ty.lift(|v| folder.fold_ty(v)),
496 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100497 }
498 Path(p) => Path(folder.fold_path(p)),
Alex Crichton62a0a592017-05-22 13:58:53 -0700499 Index(ConstIndex { expr, index }) => {
500 Index(ConstIndex {
501 expr: expr.lift(|e| folder.fold_const_expr(e)),
502 index: index.lift(|e| folder.fold_const_expr(e)),
503 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100504 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700505 Paren(ConstParen { expr }) => {
506 Paren(ConstParen {
507 expr: expr.lift(|e| folder.fold_const_expr(e)),
508 })
509 }
David Tolnay4a25fc22017-01-29 11:32:20 -0800510 Other(e) => Other(noop_fold_other_const_expr(folder, e)),
gnzlbg9ae88d82017-01-26 20:45:17 +0100511 }
512}
David Tolnay4a25fc22017-01-29 11:32:20 -0800513
514#[cfg(feature = "full")]
515fn noop_fold_other_const_expr<F: ?Sized + Folder>(folder: &mut F, e: Expr) -> Expr {
516 folder.fold_expr(e)
517}
518
519#[cfg(not(feature = "full"))]
David Tolnay05120ef2017-03-12 18:29:26 -0700520fn noop_fold_other_const_expr<F: ?Sized + Folder>(_: &mut F,
521 e: constant::Other)
522 -> constant::Other {
David Tolnay4a25fc22017-01-29 11:32:20 -0800523 e
524}
525
David Tolnay78488252017-01-29 11:22:59 -0800526pub fn noop_fold_lit<F: ?Sized + Folder>(_: &mut F, _lit: Lit) -> Lit {
gnzlbg9ae88d82017-01-26 20:45:17 +0100527 _lit
528}
529
David Tolnay78488252017-01-29 11:22:59 -0800530pub fn noop_fold_tt<F: ?Sized + Folder>(folder: &mut F, tt: TokenTree) -> TokenTree {
David Tolnay7a1cda02017-01-29 11:50:31 -0800531 use TokenTree::*;
532 use Token::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100533 match tt {
David Tolnay660b03e2017-01-29 11:26:30 -0800534 Token(token) => {
535 Token(match token {
David Tolnay05120ef2017-03-12 18:29:26 -0700536 Literal(lit) => Literal(folder.fold_lit(lit)),
537 Ident(ident) => Ident(folder.fold_ident(ident)),
538 Lifetime(ident) => Lifetime(folder.fold_ident(ident)),
539 x => x,
540 })
David Tolnay660b03e2017-01-29 11:26:30 -0800541 }
542 Delimited(super::Delimited { delim, tts }) => {
543 Delimited(super::Delimited {
David Tolnay05120ef2017-03-12 18:29:26 -0700544 delim: delim,
545 tts: tts.lift(|v| noop_fold_tt(folder, v)),
546 })
David Tolnay660b03e2017-01-29 11:26:30 -0800547 }
gnzlbg9ae88d82017-01-26 20:45:17 +0100548 }
549}
550
David Tolnay78488252017-01-29 11:22:59 -0800551pub fn noop_fold_mac<F: ?Sized + Folder>(folder: &mut F, Mac { path, tts }: Mac) -> Mac {
gnzlbg9ae88d82017-01-26 20:45:17 +0100552 Mac {
553 path: folder.fold_path(path),
554 tts: tts.lift(|tt| noop_fold_tt(folder, tt)),
555 }
556}
557
558#[cfg(feature = "full")]
David Tolnay660b03e2017-01-29 11:26:30 -0800559pub fn noop_fold_crate<F: ?Sized + Folder>(folder: &mut F,
560 Crate { shebang, attrs, items }: Crate)
561 -> Crate {
gnzlbg9ae88d82017-01-26 20:45:17 +0100562 Crate {
563 shebang: shebang,
564 attrs: attrs.lift(|a| folder.fold_attribute(a)),
565 items: items.lift(|i| folder.fold_item(i)),
566 }
567
568}
569
570#[cfg(feature = "full")]
David Tolnay78488252017-01-29 11:22:59 -0800571pub fn noop_fold_block<F: ?Sized + Folder>(folder: &mut F, block: Block) -> Block {
gnzlbg9ae88d82017-01-26 20:45:17 +0100572 Block { stmts: block.stmts.lift(|s| folder.fold_stmt(s)) }
573}
574
David Tolnay78488252017-01-29 11:22:59 -0800575fn noop_fold_vis<F: ?Sized + Folder>(folder: &mut F, vis: Visibility) -> Visibility {
David Tolnay7a1cda02017-01-29 11:50:31 -0800576 use Visibility::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100577 match vis {
578 Crate => Crate,
579 Inherited => Inherited,
580 Public => Public,
581 Restricted(path) => Restricted(path.lift(|p| folder.fold_path(p))),
582 }
583}
584
585#[cfg(feature = "full")]
David Tolnay660b03e2017-01-29 11:26:30 -0800586pub fn noop_fold_item<F: ?Sized + Folder>(folder: &mut F,
587 Item { ident, vis, attrs, node }: Item)
588 -> Item {
Alex Crichton62a0a592017-05-22 13:58:53 -0700589 use item::*;
David Tolnay7a1cda02017-01-29 11:50:31 -0800590 use ItemKind::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100591 Item {
592 ident: folder.fold_ident(ident.clone()),
593 vis: noop_fold_vis(folder, vis),
594 attrs: attrs.lift(|a| folder.fold_attribute(a)),
595 node: match node {
Alex Crichton62a0a592017-05-22 13:58:53 -0700596 ExternCrate(ItemExternCrate { original }) => {
597 ExternCrate(ItemExternCrate {
598 original: original.map(|i| folder.fold_ident(i)),
599 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100600 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700601 Use(ItemUse { path }) => {
602 Use(ItemUse {
603 path: Box::new(folder.fold_view_path(*path)),
604 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100605 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700606 Static(ItemStatic { ty, mutbl, expr }) => {
607 Static(ItemStatic {
608 ty: Box::new(folder.fold_ty(*ty)),
609 mutbl: mutbl,
610 expr: expr.lift(|e| folder.fold_expr(e)),
611 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100612 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700613 Const(ItemConst { ty, expr }) => {
614 Const(ItemConst {
615 ty: ty.lift(|ty| folder.fold_ty(ty)),
616 expr: expr.lift(|e| folder.fold_expr(e)),
617 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100618 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700619 Fn(ItemFn { decl, unsafety, constness, abi, generics, block }) => {
620 Fn(ItemFn {
621 decl: decl.lift(|v| folder.fold_fn_decl(v)),
622 unsafety: unsafety,
623 constness: constness,
624 abi: abi,
625 generics: folder.fold_generics(generics),
626 block: block.lift(|v| folder.fold_block(v)),
627 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100628 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700629 Mod(ItemMod { items }) => {
630 Mod(ItemMod {
631 items: items.map(|items| items.lift(|i| folder.fold_item(i))),
632 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100633 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700634 ForeignMod(ItemForeignMod { abi, items }) => {
635 ForeignMod(ItemForeignMod {
636 abi: abi,
637 items: items.lift(|foreign_item| {
638 folder.fold_foreign_item(foreign_item)
639 }),
640 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100641 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700642 Ty(ItemTy { ty, generics }) => {
643 Ty(ItemTy {
644 ty: ty.lift(|ty| folder.fold_ty(ty)),
645 generics: folder.fold_generics(generics),
646 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100647 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700648 Enum(ItemEnum { variants, generics }) => {
649 Enum(ItemEnum {
650 variants: variants.lift(|v| folder.fold_variant(v)),
651 generics: folder.fold_generics(generics),
652 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100653 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700654 Struct(ItemStruct { data, generics }) => {
655 Struct(ItemStruct {
656 data: folder.fold_variant_data(data),
657 generics: folder.fold_generics(generics),
658 })
659 }
660 Union(ItemUnion { data, generics }) => {
661 Union(ItemUnion {
662 data: folder.fold_variant_data(data),
663 generics: folder.fold_generics(generics),
664 })
665 }
666 Trait(ItemTrait { unsafety, generics, supertraits, items }) => {
667 Trait(ItemTrait {
668 unsafety: unsafety,
669 generics: folder.fold_generics(generics),
670 supertraits: supertraits.lift(|typb| folder.fold_ty_param_bound(typb)),
671 items: items.lift(|ti| folder.fold_trait_item(ti)),
672 })
673 }
674 DefaultImpl(ItemDefaultImpl { unsafety, path }) => {
675 DefaultImpl(ItemDefaultImpl {
676 unsafety: unsafety,
677 path: folder.fold_path(path),
678 })
679 }
680 Impl(ItemImpl {
681 unsafety,
682 polarity,
683 generics,
684 trait_,
685 self_ty,
686 items,
687 }) => {
688 Impl(ItemImpl {
689 unsafety: unsafety,
690 polarity: polarity,
691 generics: folder.fold_generics(generics),
692 trait_: trait_.map(|p| folder.fold_path(p)),
693 self_ty: self_ty.lift(|ty| folder.fold_ty(ty)),
694 items: items.lift(|i| folder.fold_impl_item(i)),
695 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100696 }
697 Mac(mac) => Mac(folder.fold_mac(mac)),
698 },
699 }
700}
701
gnzlbg9ae88d82017-01-26 20:45:17 +0100702#[cfg(feature = "full")]
David Tolnay78488252017-01-29 11:22:59 -0800703pub fn noop_fold_expr<F: ?Sized + Folder>(folder: &mut F, Expr { node, attrs }: Expr) -> Expr {
Alex Crichton62a0a592017-05-22 13:58:53 -0700704 use expr::*;
705 use expr::ExprKind::*;
706
gnzlbg9ae88d82017-01-26 20:45:17 +0100707 Expr {
708 node: match node {
Alex Crichton62a0a592017-05-22 13:58:53 -0700709 Box(ExprBox { expr }) => {
710 Box(ExprBox { expr: expr.lift(|e| folder.fold_expr(e)) })
gnzlbg9ae88d82017-01-26 20:45:17 +0100711 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700712 InPlace(ExprInPlace { place, value }) => {
713 InPlace(ExprInPlace {
714 place: place.lift(|e| folder.fold_expr(e)),
715 value: value.lift(|e| folder.fold_expr(e)),
716 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100717 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700718 Array(ExprArray { exprs }) => {
719 Array(ExprArray {
720 exprs: exprs.lift(|e| folder.fold_expr(e)),
721 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100722 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700723 Call(ExprCall { func, args }) => {
724 Call(ExprCall {
725 func: func.lift(|e| folder.fold_expr(e)),
726 args: args.lift(|e| folder.fold_expr(e)),
727 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100728 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700729 MethodCall(ExprMethodCall { method, typarams, args }) => {
730 MethodCall(ExprMethodCall {
731 method: folder.fold_ident(method),
732 typarams: typarams.lift(|t| folder.fold_ty(t)),
733 args: args.lift(|e| folder.fold_expr(e)),
734 })
735 }
736 Tup(ExprTup { args }) => {
737 Tup(ExprTup {
738 args: args.lift(|e| folder.fold_expr(e)),
739 })
740 }
741 Binary(ExprBinary { op, left, right }) => {
742 Binary(ExprBinary {
743 op: op,
744 left: left.lift(|e| folder.fold_expr(e)),
745 right: right.lift(|e| folder.fold_expr(e)),
746 })
747 }
748 Unary(ExprUnary { op, expr }) => {
749 Unary(ExprUnary {
750 op: op,
751 expr: expr.lift(|e| folder.fold_expr(e)),
752 })
753 }
gnzlbg9ae88d82017-01-26 20:45:17 +0100754 Lit(lit) => Lit(folder.fold_lit(lit)),
Alex Crichton62a0a592017-05-22 13:58:53 -0700755 Cast(ExprCast { expr, ty }) => {
756 Cast(ExprCast {
757 expr: expr.lift(|e| folder.fold_expr(e)),
758 ty: ty.lift(|t| folder.fold_ty(t)),
759 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100760 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700761 Type(ExprType { expr, ty }) => {
762 Type(ExprType {
763 expr: expr.lift(|e| folder.fold_expr(e)),
764 ty: ty.lift(|t| folder.fold_ty(t)),
765 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100766 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700767 If(ExprIf { cond, if_true, if_false }) => {
768 If(ExprIf {
769 cond: cond.lift(|e| folder.fold_expr(e)),
770 if_true: folder.fold_block(if_true),
771 if_false: if_false.map(|v| v.lift(|e| folder.fold_expr(e))),
772 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100773 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700774 IfLet(ExprIfLet { pat, expr, if_true, if_false }) => {
775 IfLet(ExprIfLet {
776 pat: pat.lift(|p| folder.fold_pat(p)),
777 expr: expr.lift(|e| folder.fold_expr(e)),
778 if_true: folder.fold_block(if_true),
779 if_false: if_false.map(|v| v.lift(|e| folder.fold_expr(e))),
780 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100781 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700782 While(ExprWhile { cond, body, label }) => {
783 While(ExprWhile {
784 cond: cond.lift(|e| folder.fold_expr(e)),
785 body: folder.fold_block(body),
786 label: label.map(|i| folder.fold_ident(i)),
787 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100788 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700789 WhileLet(ExprWhileLet { pat, expr, body, label }) => {
790 WhileLet(ExprWhileLet {
791 pat: pat.lift(|p| folder.fold_pat(p)),
792 expr: expr.lift(|e| folder.fold_expr(e)),
793 body: folder.fold_block(body),
794 label: label.map(|i| folder.fold_ident(i)),
795 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100796 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700797 ForLoop(ExprForLoop { pat, expr, body, label }) => {
798 ForLoop(ExprForLoop {
799 pat: pat.lift(|p| folder.fold_pat(p)),
800 expr: expr.lift(|e| folder.fold_expr(e)),
801 body: folder.fold_block(body),
802 label: label.map(|i| folder.fold_ident(i)),
803 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100804 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700805 Loop(ExprLoop { body, label }) => {
806 Loop(ExprLoop {
807 body: folder.fold_block(body),
808 label: label.map(|i| folder.fold_ident(i)),
809 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100810 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700811 Match(ExprMatch { expr, arms }) => {
812 Match(ExprMatch {
813 expr: expr.lift(|e| folder.fold_expr(e)),
814 arms: arms.lift(|Arm { attrs, pats, guard, body }: Arm| {
815 Arm {
816 attrs: attrs.lift(|a| folder.fold_attribute(a)),
817 pats: pats.lift(|p| folder.fold_pat(p)),
818 guard: guard.map(|v| v.lift(|e| folder.fold_expr(e))),
819 body: body.lift(|e| folder.fold_expr(e)),
820 }
821 })
822 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100823 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700824 Catch(ExprCatch { block }) => {
825 Catch(ExprCatch { block: folder.fold_block(block) })
Arnavion02ef13f2017-04-25 00:54:31 -0700826 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700827 Closure(ExprClosure { capture, decl, body }) => {
828 Closure(ExprClosure {
829 capture: capture,
830 decl: decl.lift(|v| folder.fold_fn_decl(v)),
831 body: body.lift(|e| folder.fold_expr(e)),
832 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100833 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700834 Block(ExprBlock { unsafety, block }) => {
835 Block(ExprBlock {
836 unsafety: unsafety,
837 block: folder.fold_block(block),
838 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100839 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700840 Assign(ExprAssign { left, right }) => {
841 Assign(ExprAssign {
842 left: left.lift(|e| folder.fold_expr(e)),
843 right: right.lift(|e| folder.fold_expr(e)),
844 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100845 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700846 AssignOp(ExprAssignOp { op, left, right }) => {
847 AssignOp(ExprAssignOp {
848 op: op,
849 left: left.lift(|e| folder.fold_expr(e)),
850 right: right.lift(|e| folder.fold_expr(e)),
851 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100852 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700853 Field(ExprField { expr, field }) => {
854 Field(ExprField {
855 expr: expr.lift(|e| folder.fold_expr(e)),
856 field: folder.fold_ident(field),
857 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100858 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700859 TupField(ExprTupField { expr, field }) => {
860 TupField(ExprTupField {
861 expr: expr.lift(|e| folder.fold_expr(e)),
862 field: field,
863 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100864 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700865 Index(ExprIndex { expr, index }) => {
866 Index(ExprIndex {
867 expr: expr.lift(|e| folder.fold_expr(e)),
868 index: index.lift(|e| folder.fold_expr(e)),
869 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100870 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700871 Range(ExprRange { from, to, limits }) => {
872 Range(ExprRange {
873 from: from.map(|v| v.lift(|e| folder.fold_expr(e))),
874 to: to.map(|v| v.lift(|e| folder.fold_expr(e))),
875 limits: limits,
876 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100877 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700878 Path(ExprPath { qself, path }) => {
879 Path(ExprPath {
880 qself: qself.map(|v| noop_fold_qself(folder, v)),
881 path: folder.fold_path(path),
882 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100883 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700884 AddrOf(ExprAddrOf { mutbl, expr }) => {
885 AddrOf(ExprAddrOf {
886 mutbl: mutbl,
887 expr: expr.lift(|e| folder.fold_expr(e)),
888 })
889 }
890 Break(ExprBreak { label, expr }) => {
891 Break(ExprBreak {
892 label: label.map(|i| folder.fold_ident(i)),
893 expr: expr.map(|v| v.lift(|e| folder.fold_expr(e))),
894 })
895 }
896 Continue(ExprContinue { label }) => {
897 Continue(ExprContinue {
898 label: label.map(|i| folder.fold_ident(i)),
899 })
900 }
901 Ret(ExprRet { expr }) => {
902 Ret(ExprRet {
903 expr: expr.map(|v| v.lift(|e| folder.fold_expr(e))),
904 })
905 }
906 Mac(mac) => Mac(folder.fold_mac(mac)),
907 Struct(ExprStruct { path, fields, rest }) => {
908 Struct(ExprStruct {
909 path: folder.fold_path(path),
910 fields: fields.lift(|FieldValue { ident, expr, is_shorthand, attrs }: FieldValue| {
911 FieldValue {
912 ident: folder.fold_ident(ident),
913 expr: folder.fold_expr(expr),
914 is_shorthand: is_shorthand,
915 attrs: attrs.lift(|v| folder.fold_attribute(v)),
916 }
917 }),
918 rest: rest.map(|v| v.lift(|e| folder.fold_expr(e))),
919 })
920 }
921 Repeat(ExprRepeat { expr, amt }) => {
922 Repeat(ExprRepeat {
923 expr: expr.lift(|e| folder.fold_expr(e)),
924 amt: amt.lift(|e| folder.fold_expr(e)),
925 })
926 }
927 Paren(ExprParen { expr }) => {
928 Paren(ExprParen { expr: expr.lift(|e| folder.fold_expr(e)) })
929 }
930 Try(ExprTry { expr }) => {
931 Try(ExprTry { expr: expr.lift(|e| folder.fold_expr(e)) })
932 }
gnzlbg9ae88d82017-01-26 20:45:17 +0100933 },
934 attrs: attrs.into_iter().map(|a| folder.fold_attribute(a)).collect(),
935 }
936}
937
gnzlbg9ae88d82017-01-26 20:45:17 +0100938#[cfg(feature = "full")]
David Tolnay78488252017-01-29 11:22:59 -0800939pub fn noop_fold_foreign_item<F: ?Sized + Folder>(folder: &mut F,
gnzlbg9ae88d82017-01-26 20:45:17 +0100940 ForeignItem { ident, attrs, node, vis }: ForeignItem)
David Tolnay05120ef2017-03-12 18:29:26 -0700941-> ForeignItem{
Alex Crichton62a0a592017-05-22 13:58:53 -0700942 use item::*;
943
gnzlbg9ae88d82017-01-26 20:45:17 +0100944 ForeignItem {
945 ident: folder.fold_ident(ident),
946 attrs: attrs.into_iter().map(|a| folder.fold_attribute(a)).collect(),
947 node: match node {
Alex Crichton62a0a592017-05-22 13:58:53 -0700948 ForeignItemKind::Fn(ForeignItemFn { decl, generics }) => {
949 ForeignItemKind::Fn(ForeignItemFn {
950 decl: decl.lift(|v| folder.fold_fn_decl(v)),
951 generics: folder.fold_generics(generics),
952 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100953 }
Alex Crichton62a0a592017-05-22 13:58:53 -0700954 ForeignItemKind::Static(ForeignItemStatic { ty, mutbl }) => {
955 ForeignItemKind::Static(ForeignItemStatic {
956 ty: ty.lift(|v| folder.fold_ty(v)),
957 mutbl: mutbl,
958 })
gnzlbg9ae88d82017-01-26 20:45:17 +0100959 }
960 },
961 vis: noop_fold_vis(folder, vis),
962 }
963}
964
gnzlbg9ae88d82017-01-26 20:45:17 +0100965#[cfg(feature = "full")]
David Tolnay78488252017-01-29 11:22:59 -0800966pub fn noop_fold_pat<F: ?Sized + Folder>(folder: &mut F, pat: Pat) -> Pat {
David Tolnay7a1cda02017-01-29 11:50:31 -0800967 use Pat::*;
gnzlbg9ae88d82017-01-26 20:45:17 +0100968 match pat {
969 Wild => Wild,
970 Ident(binding_mode, ident, pat) => {
971 Ident(binding_mode,
972 folder.fold_ident(ident),
973 pat.map(|p| p.lift(|p| folder.fold_pat(p))))
974 }
975 Struct(path, field_patterns, dots) => {
976 Struct(folder.fold_path(path),
977 field_patterns.lift(|FieldPat { ident, pat, is_shorthand, attrs }: FieldPat| {
978 FieldPat {
979 ident: folder.fold_ident(ident),
980 pat: pat.lift(|p| folder.fold_pat(p)),
981 is_shorthand: is_shorthand,
982 attrs: attrs.lift(|a| folder.fold_attribute(a)),
983 }
984 }),
985 dots)
986 }
987 TupleStruct(path, pats, len) => {
988 TupleStruct(folder.fold_path(path),
989 pats.lift(|p| folder.fold_pat(p)),
990 len)
991 }
992 Path(qself, path) => {
993 Path(qself.map(|v| noop_fold_qself(folder, v)),
994 folder.fold_path(path))
995 }
996 Tuple(pats, len) => Tuple(pats.lift(|p| folder.fold_pat(p)), len),
997 Box(b) => Box(b.lift(|p| folder.fold_pat(p))),
998 Ref(b, mutability) => Ref(b.lift(|p| folder.fold_pat(p)), mutability),
999 Lit(expr) => Lit(expr.lift(|e| folder.fold_expr(e))),
Arnavion1992e2f2017-04-25 01:47:46 -07001000 Range(l, r, limits) => {
gnzlbg9ae88d82017-01-26 20:45:17 +01001001 Range(l.lift(|e| folder.fold_expr(e)),
Arnavion1992e2f2017-04-25 01:47:46 -07001002 r.lift(|e| folder.fold_expr(e)),
1003 limits)
gnzlbg9ae88d82017-01-26 20:45:17 +01001004 }
1005 Slice(lefts, pat, rights) => {
1006 Slice(lefts.lift(|p| folder.fold_pat(p)),
1007 pat.map(|v| v.lift(|p| folder.fold_pat(p))),
1008 rights.lift(|p| folder.fold_pat(p)))
1009 }
1010 Mac(mac) => Mac(folder.fold_mac(mac)),
1011 }
1012}
1013
gnzlbg9ae88d82017-01-26 20:45:17 +01001014#[cfg(feature = "full")]
David Tolnay78488252017-01-29 11:22:59 -08001015pub fn noop_fold_fn_decl<F: ?Sized + Folder>(folder: &mut F,
David Tolnay660b03e2017-01-29 11:26:30 -08001016 FnDecl { inputs, output, variadic }: FnDecl)
1017 -> FnDecl {
gnzlbg9ae88d82017-01-26 20:45:17 +01001018
1019 FnDecl {
1020 inputs: inputs.lift(|a| {
Alex Crichton62a0a592017-05-22 13:58:53 -07001021 use item::*;
David Tolnay7a1cda02017-01-29 11:50:31 -08001022 use FnArg::*;
gnzlbg9ae88d82017-01-26 20:45:17 +01001023 match a {
Alex Crichton62a0a592017-05-22 13:58:53 -07001024 SelfRef(ArgSelfRef { lifetime, mutbl }) => {
1025 SelfRef(ArgSelfRef {
1026 lifetime: lifetime.map(|v| folder.fold_lifetime(v)),
1027 mutbl: mutbl,
1028 })
gnzlbg9ae88d82017-01-26 20:45:17 +01001029 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001030 SelfValue(ArgSelf { mutbl } ) => {
1031 SelfValue(ArgSelf {
1032 mutbl: mutbl,
1033 })
1034 }
1035 Captured(ArgCaptured { pat, ty }) => {
1036 Captured(ArgCaptured {
1037 pat: folder.fold_pat(pat),
1038 ty: folder.fold_ty(ty),
1039 })
1040 }
gnzlbg9ae88d82017-01-26 20:45:17 +01001041 Ignored(ty) => Ignored(folder.fold_ty(ty)),
1042 }
1043 }),
1044 output: folder.fold_fn_ret_ty(output),
1045 variadic: variadic,
1046 }
1047
1048}
1049
gnzlbg9ae88d82017-01-26 20:45:17 +01001050#[cfg(feature = "full")]
David Tolnay78488252017-01-29 11:22:59 -08001051pub fn noop_fold_trait_item<F: ?Sized + Folder>(folder: &mut F,
David Tolnay660b03e2017-01-29 11:26:30 -08001052 TraitItem { ident, attrs, node }: TraitItem)
1053 -> TraitItem {
Alex Crichton62a0a592017-05-22 13:58:53 -07001054 use item::*;
David Tolnay7a1cda02017-01-29 11:50:31 -08001055 use TraitItemKind::*;
gnzlbg9ae88d82017-01-26 20:45:17 +01001056 TraitItem {
1057 ident: folder.fold_ident(ident),
1058 attrs: attrs.lift(|v| folder.fold_attribute(v)),
1059 node: match node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001060 Const(TraitItemConst { ty, default }) => {
1061 Const(TraitItemConst {
1062 ty: folder.fold_ty(ty),
1063 default: default.map(|v| folder.fold_expr(v)),
1064 })
gnzlbg9ae88d82017-01-26 20:45:17 +01001065 }
Alex Crichton62a0a592017-05-22 13:58:53 -07001066 Method(TraitItemMethod { sig, default }) => {
1067 Method(TraitItemMethod {
1068 sig: folder.fold_method_sig(sig),
1069 default: default.map(|v| folder.fold_block(v)),
1070 })
1071 }
1072 Type(TraitItemType { bounds, default }) => {
1073 Type(TraitItemType {
1074 bounds: bounds.lift(|v| folder.fold_ty_param_bound(v)),
1075 default: default.map(|v| folder.fold_ty(v)),
1076 })
gnzlbg9ae88d82017-01-26 20:45:17 +01001077 }
David Tolnay70925b72017-01-29 11:20:54 -08001078 Macro(mac) => Macro(folder.fold_mac(mac)),
gnzlbg9ae88d82017-01-26 20:45:17 +01001079 },
1080 }
1081}
1082
gnzlbg9ae88d82017-01-26 20:45:17 +01001083#[cfg(feature = "full")]
David Tolnay78488252017-01-29 11:22:59 -08001084pub fn noop_fold_impl_item<F: ?Sized + Folder>(folder: &mut F,
gnzlbg9ae88d82017-01-26 20:45:17 +01001085 ImplItem { ident, vis, defaultness, attrs, node }: ImplItem)
David Tolnay05120ef2017-03-12 18:29:26 -07001086-> ImplItem{
Alex Crichton62a0a592017-05-22 13:58:53 -07001087 use item::*;
David Tolnay7a1cda02017-01-29 11:50:31 -08001088 use ImplItemKind::*;
Alex Crichton62a0a592017-05-22 13:58:53 -07001089
gnzlbg9ae88d82017-01-26 20:45:17 +01001090 ImplItem {
1091 ident: folder.fold_ident(ident),
1092 vis: noop_fold_vis(folder, vis),
1093 defaultness: defaultness,
1094 attrs: attrs.lift(|v| folder.fold_attribute(v)),
1095 node: match node {
Alex Crichton62a0a592017-05-22 13:58:53 -07001096 Const(ImplItemConst { ty, expr }) => {
1097 Const(ImplItemConst {
1098 ty: folder.fold_ty(ty),
1099 expr: folder.fold_expr(expr),
1100 })
1101 }
1102 Method(ImplItemMethod { sig, block }) => {
1103 Method(ImplItemMethod {
1104 sig: folder.fold_method_sig(sig),
1105 block: folder.fold_block(block),
1106 })
1107 }
1108 Type(ImplItemType { ty }) => {
1109 Type(ImplItemType {
1110 ty: folder.fold_ty(ty),
1111 })
1112 }
gnzlbg9ae88d82017-01-26 20:45:17 +01001113 Macro(mac) => Macro(folder.fold_mac(mac)),
1114 },
1115 }
1116}
1117
gnzlbg9ae88d82017-01-26 20:45:17 +01001118#[cfg(feature = "full")]
David Tolnay05120ef2017-03-12 18:29:26 -07001119pub 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 +01001120 MethodSig {
1121 unsafety: unsafety,
1122 constness: constness,
1123 abi: abi,
1124 decl: folder.fold_fn_decl(decl),
1125 generics: folder.fold_generics(generics),
1126 }
1127
1128}
1129
gnzlbg9ae88d82017-01-26 20:45:17 +01001130#[cfg(feature = "full")]
David Tolnay78488252017-01-29 11:22:59 -08001131pub fn noop_fold_stmt<F: ?Sized + Folder>(folder: &mut F, stmt: Stmt) -> Stmt {
David Tolnay7a1cda02017-01-29 11:50:31 -08001132 use Stmt::*;
gnzlbg9ae88d82017-01-26 20:45:17 +01001133 match stmt {
1134 Local(local) => Local(local.lift(|l| folder.fold_local(l))),
1135 Item(item) => Item(item.lift(|v| folder.fold_item(v))),
1136 Expr(expr) => Expr(expr.lift(|v| folder.fold_expr(v))),
1137 Semi(expr) => Semi(expr.lift(|v| folder.fold_expr(v))),
1138 Mac(mac_stmt) => {
1139 Mac(mac_stmt.lift(|(mac, style, attrs)| {
David Tolnay05120ef2017-03-12 18:29:26 -07001140 (folder.fold_mac(mac),
1141 style,
1142 attrs.lift(|a| folder.fold_attribute(a)))
1143 }))
gnzlbg9ae88d82017-01-26 20:45:17 +01001144 }
1145 }
1146
1147}
1148
gnzlbg9ae88d82017-01-26 20:45:17 +01001149#[cfg(feature = "full")]
David Tolnay660b03e2017-01-29 11:26:30 -08001150pub fn noop_fold_local<F: ?Sized + Folder>(folder: &mut F,
1151 Local { pat, ty, init, attrs }: Local)
1152 -> Local {
gnzlbg9ae88d82017-01-26 20:45:17 +01001153 Local {
1154 pat: pat.lift(|v| folder.fold_pat(v)),
1155 ty: ty.map(|v| v.lift(|t| folder.fold_ty(t))),
1156 init: init.map(|v| v.lift(|e| folder.fold_expr(e))),
1157 attrs: attrs.lift(|a| folder.fold_attribute(a)),
1158 }
1159}
1160
1161#[cfg(feature = "full")]
David Tolnay78488252017-01-29 11:22:59 -08001162pub fn noop_fold_view_path<F: ?Sized + Folder>(folder: &mut F, view_path: ViewPath) -> ViewPath {
Alex Crichton62a0a592017-05-22 13:58:53 -07001163 use item::*;
David Tolnay7a1cda02017-01-29 11:50:31 -08001164 use ViewPath::*;
gnzlbg9ae88d82017-01-26 20:45:17 +01001165 match view_path {
Alex Crichton62a0a592017-05-22 13:58:53 -07001166 Simple(PathSimple { path, rename }) => {
1167 Simple(PathSimple {
1168 path: folder.fold_path(path),
1169 rename: rename.map(|i| folder.fold_ident(i)),
1170 })
1171 }
1172 Glob(PathGlob { path }) => {
1173 Glob(PathGlob {
1174 path: folder.fold_path(path),
1175 })
1176 }
1177 List(PathList { path, items }) => {
1178 List(PathList {
1179 path: folder.fold_path(path),
1180 items: items.lift(|PathListItem { name, rename }: PathListItem| {
1181 PathListItem {
1182 name: folder.fold_ident(name),
1183 rename: rename.map(|i| folder.fold_ident(i)),
1184 }
1185 }),
1186 })
gnzlbg9ae88d82017-01-26 20:45:17 +01001187 }
1188 }
1189}