1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! A Folder represents an HIR->HIR fold; it accepts a HIR piece,
12 //! and returns a piece of the same type.
15 use syntax::ast::{Name, NodeId, DUMMY_NODE_ID, Attribute, Attribute_, MetaItem};
16 use syntax::ast::MetaItemKind;
19 use syntax::codemap::{respan, Spanned};
21 use syntax::parse::token::keywords;
22 use syntax::util::move_map::MoveMap;
24 pub trait Folder : Sized {
25 // Any additions to this trait should happen in form
26 // of a call to a public `noop_*` function that only calls
27 // out to the folder again, not other `noop_*` functions.
29 // This is a necessary API workaround to the problem of not
30 // being able to call out to the super default method
31 // in an overridden default method.
33 fn fold_crate(&mut self, c: Crate) -> Crate {
34 noop_fold_crate(c, self)
37 fn fold_meta_items(&mut self, meta_items: HirVec<P<MetaItem>>) -> HirVec<P<MetaItem>> {
38 noop_fold_meta_items(meta_items, self)
41 fn fold_meta_item(&mut self, meta_item: P<MetaItem>) -> P<MetaItem> {
42 noop_fold_meta_item(meta_item, self)
45 fn fold_view_path(&mut self, view_path: P<ViewPath>) -> P<ViewPath> {
46 noop_fold_view_path(view_path, self)
49 fn fold_foreign_item(&mut self, ni: ForeignItem) -> ForeignItem {
50 noop_fold_foreign_item(ni, self)
53 fn fold_item(&mut self, i: Item) -> Item {
54 noop_fold_item(i, self)
57 fn fold_item_id(&mut self, i: ItemId) -> ItemId {
58 noop_fold_item_id(i, self)
61 fn fold_struct_field(&mut self, sf: StructField) -> StructField {
62 noop_fold_struct_field(sf, self)
65 fn fold_item_underscore(&mut self, i: Item_) -> Item_ {
66 noop_fold_item_underscore(i, self)
69 fn fold_trait_item(&mut self, i: TraitItem) -> TraitItem {
70 noop_fold_trait_item(i, self)
73 fn fold_impl_item(&mut self, i: ImplItem) -> ImplItem {
74 noop_fold_impl_item(i, self)
77 fn fold_fn_decl(&mut self, d: P<FnDecl>) -> P<FnDecl> {
78 noop_fold_fn_decl(d, self)
81 fn fold_block(&mut self, b: P<Block>) -> P<Block> {
82 noop_fold_block(b, self)
85 fn fold_stmt(&mut self, s: Stmt) -> Stmt {
86 noop_fold_stmt(s, self)
89 fn fold_arm(&mut self, a: Arm) -> Arm {
90 noop_fold_arm(a, self)
93 fn fold_pat(&mut self, p: P<Pat>) -> P<Pat> {
94 noop_fold_pat(p, self)
97 fn fold_decl(&mut self, d: P<Decl>) -> P<Decl> {
98 noop_fold_decl(d, self)
101 fn fold_expr(&mut self, e: P<Expr>) -> P<Expr> {
102 e.map(|e| noop_fold_expr(e, self))
105 fn fold_ty(&mut self, t: P<Ty>) -> P<Ty> {
106 noop_fold_ty(t, self)
109 fn fold_ty_binding(&mut self, t: TypeBinding) -> TypeBinding {
110 noop_fold_ty_binding(t, self)
113 fn fold_mod(&mut self, m: Mod) -> Mod {
114 noop_fold_mod(m, self)
117 fn fold_foreign_mod(&mut self, nm: ForeignMod) -> ForeignMod {
118 noop_fold_foreign_mod(nm, self)
121 fn fold_variant(&mut self, v: Variant) -> Variant {
122 noop_fold_variant(v, self)
125 fn fold_name(&mut self, n: Name) -> Name {
126 noop_fold_name(n, self)
129 fn fold_usize(&mut self, i: usize) -> usize {
130 noop_fold_usize(i, self)
133 fn fold_path(&mut self, p: Path) -> Path {
134 noop_fold_path(p, self)
137 fn fold_path_parameters(&mut self, p: PathParameters) -> PathParameters {
138 noop_fold_path_parameters(p, self)
141 fn fold_angle_bracketed_parameter_data(&mut self,
142 p: AngleBracketedParameterData)
143 -> AngleBracketedParameterData {
144 noop_fold_angle_bracketed_parameter_data(p, self)
147 fn fold_parenthesized_parameter_data(&mut self,
148 p: ParenthesizedParameterData)
149 -> ParenthesizedParameterData {
150 noop_fold_parenthesized_parameter_data(p, self)
153 fn fold_local(&mut self, l: P<Local>) -> P<Local> {
154 noop_fold_local(l, self)
157 fn fold_lifetime(&mut self, l: Lifetime) -> Lifetime {
158 noop_fold_lifetime(l, self)
161 fn fold_lifetime_def(&mut self, l: LifetimeDef) -> LifetimeDef {
162 noop_fold_lifetime_def(l, self)
165 fn fold_attribute(&mut self, at: Attribute) -> Option<Attribute> {
166 noop_fold_attribute(at, self)
169 fn fold_arg(&mut self, a: Arg) -> Arg {
170 noop_fold_arg(a, self)
173 fn fold_generics(&mut self, generics: Generics) -> Generics {
174 noop_fold_generics(generics, self)
177 fn fold_trait_ref(&mut self, p: TraitRef) -> TraitRef {
178 noop_fold_trait_ref(p, self)
181 fn fold_poly_trait_ref(&mut self, p: PolyTraitRef) -> PolyTraitRef {
182 noop_fold_poly_trait_ref(p, self)
185 fn fold_variant_data(&mut self, vdata: VariantData) -> VariantData {
186 noop_fold_variant_data(vdata, self)
189 fn fold_lifetimes(&mut self, lts: HirVec<Lifetime>) -> HirVec<Lifetime> {
190 noop_fold_lifetimes(lts, self)
193 fn fold_lifetime_defs(&mut self, lts: HirVec<LifetimeDef>) -> HirVec<LifetimeDef> {
194 noop_fold_lifetime_defs(lts, self)
197 fn fold_ty_param(&mut self, tp: TyParam) -> TyParam {
198 noop_fold_ty_param(tp, self)
201 fn fold_ty_params(&mut self, tps: HirVec<TyParam>) -> HirVec<TyParam> {
202 noop_fold_ty_params(tps, self)
205 fn fold_opt_lifetime(&mut self, o_lt: Option<Lifetime>) -> Option<Lifetime> {
206 noop_fold_opt_lifetime(o_lt, self)
209 fn fold_opt_bounds(&mut self,
210 b: Option<TyParamBounds>)
211 -> Option<TyParamBounds> {
212 noop_fold_opt_bounds(b, self)
215 fn fold_bounds(&mut self, b: TyParamBounds) -> TyParamBounds {
216 noop_fold_bounds(b, self)
219 fn fold_ty_param_bound(&mut self, tpb: TyParamBound) -> TyParamBound {
220 noop_fold_ty_param_bound(tpb, self)
223 fn fold_mt(&mut self, mt: MutTy) -> MutTy {
224 noop_fold_mt(mt, self)
227 fn fold_field(&mut self, field: Field) -> Field {
228 noop_fold_field(field, self)
231 fn fold_where_clause(&mut self, where_clause: WhereClause) -> WhereClause {
232 noop_fold_where_clause(where_clause, self)
235 fn fold_where_predicate(&mut self, where_predicate: WherePredicate) -> WherePredicate {
236 noop_fold_where_predicate(where_predicate, self)
239 /// called for the `id` on each declaration
240 fn new_id(&mut self, i: NodeId) -> NodeId {
244 /// called for ids that are references (e.g., ItemDef)
245 fn map_id(&mut self, i: NodeId) -> NodeId {
249 fn new_span(&mut self, sp: Span) -> Span {
254 pub fn noop_fold_meta_items<T: Folder>(meta_items: HirVec<P<MetaItem>>,
256 -> HirVec<P<MetaItem>> {
257 meta_items.move_map(|x| fld.fold_meta_item(x))
260 pub fn noop_fold_view_path<T: Folder>(view_path: P<ViewPath>, fld: &mut T) -> P<ViewPath> {
261 view_path.map(|Spanned { node, span }| {
264 ViewPathSimple(name, path) => {
265 ViewPathSimple(name, fld.fold_path(path))
267 ViewPathGlob(path) => {
268 ViewPathGlob(fld.fold_path(path))
270 ViewPathList(path, path_list_idents) => {
271 ViewPathList(fld.fold_path(path),
272 path_list_idents.move_map(|path_list_ident| {
274 node: match path_list_ident.node {
275 PathListIdent { id, name, rename } => PathListIdent {
280 PathListMod { id, rename } => PathListMod {
285 span: fld.new_span(path_list_ident.span),
290 span: fld.new_span(span),
295 pub fn fold_attrs<T, F>(attrs: T, fld: &mut F) -> T
296 where T: Into<Vec<Attribute>> + From<Vec<Attribute>>,
299 attrs.into().move_flat_map(|x| fld.fold_attribute(x)).into()
302 pub fn noop_fold_arm<T: Folder>(Arm { attrs, pats, guard, body }: Arm, fld: &mut T) -> Arm {
304 attrs: fold_attrs(attrs, fld),
305 pats: pats.move_map(|x| fld.fold_pat(x)),
306 guard: guard.map(|x| fld.fold_expr(x)),
307 body: fld.fold_expr(body),
311 pub fn noop_fold_decl<T: Folder>(d: P<Decl>, fld: &mut T) -> P<Decl> {
312 d.map(|Spanned { node, span }| {
314 DeclLocal(l) => Spanned {
315 node: DeclLocal(fld.fold_local(l)),
316 span: fld.new_span(span),
318 DeclItem(it) => Spanned {
319 node: DeclItem(fld.fold_item_id(it)),
320 span: fld.new_span(span),
326 pub fn noop_fold_ty_binding<T: Folder>(b: TypeBinding, fld: &mut T) -> TypeBinding {
328 id: fld.new_id(b.id),
330 ty: fld.fold_ty(b.ty),
331 span: fld.new_span(b.span),
335 pub fn noop_fold_ty<T: Folder>(t: P<Ty>, fld: &mut T) -> P<Ty> {
336 t.map(|Ty { id, node, span }| {
341 TyVec(ty) => TyVec(fld.fold_ty(ty)),
342 TyPtr(mt) => TyPtr(fld.fold_mt(mt)),
343 TyRptr(region, mt) => {
344 TyRptr(fld.fold_opt_lifetime(region), fld.fold_mt(mt))
347 TyBareFn(f.map(|BareFnTy { lifetimes, unsafety, abi, decl }| {
349 lifetimes: fld.fold_lifetime_defs(lifetimes),
352 decl: fld.fold_fn_decl(decl),
356 TyTup(tys) => TyTup(tys.move_map(|ty| fld.fold_ty(ty))),
357 TyPath(qself, path) => {
358 let qself = qself.map(|QSelf { ty, position }| {
364 TyPath(qself, fld.fold_path(path))
366 TyObjectSum(ty, bounds) => {
367 TyObjectSum(fld.fold_ty(ty), fld.fold_bounds(bounds))
369 TyFixedLengthVec(ty, e) => {
370 TyFixedLengthVec(fld.fold_ty(ty), fld.fold_expr(e))
373 TyTypeof(fld.fold_expr(expr))
375 TyPolyTraitRef(bounds) => {
376 TyPolyTraitRef(bounds.move_map(|b| fld.fold_ty_param_bound(b)))
379 span: fld.new_span(span),
384 pub fn noop_fold_foreign_mod<T: Folder>(ForeignMod { abi, items }: ForeignMod,
389 items: items.move_map(|x| fld.fold_foreign_item(x)),
393 pub fn noop_fold_variant<T: Folder>(v: Variant, fld: &mut T) -> Variant {
397 attrs: fold_attrs(v.node.attrs, fld),
398 data: fld.fold_variant_data(v.node.data),
399 disr_expr: v.node.disr_expr.map(|e| fld.fold_expr(e)),
401 span: fld.new_span(v.span),
405 pub fn noop_fold_name<T: Folder>(n: Name, _: &mut T) -> Name {
409 pub fn noop_fold_usize<T: Folder>(i: usize, _: &mut T) -> usize {
413 pub fn noop_fold_path<T: Folder>(Path { global, segments, span }: Path, fld: &mut T) -> Path {
416 segments: segments.move_map(|PathSegment { name, parameters }| {
418 name: fld.fold_name(name),
419 parameters: fld.fold_path_parameters(parameters),
422 span: fld.new_span(span),
426 pub fn noop_fold_path_parameters<T: Folder>(path_parameters: PathParameters,
429 match path_parameters {
430 AngleBracketedParameters(data) =>
431 AngleBracketedParameters(fld.fold_angle_bracketed_parameter_data(data)),
432 ParenthesizedParameters(data) =>
433 ParenthesizedParameters(fld.fold_parenthesized_parameter_data(data)),
437 pub fn noop_fold_angle_bracketed_parameter_data<T: Folder>(data: AngleBracketedParameterData,
439 -> AngleBracketedParameterData {
440 let AngleBracketedParameterData { lifetimes, types, bindings } = data;
441 AngleBracketedParameterData {
442 lifetimes: fld.fold_lifetimes(lifetimes),
443 types: types.move_map(|ty| fld.fold_ty(ty)),
444 bindings: bindings.move_map(|b| fld.fold_ty_binding(b)),
448 pub fn noop_fold_parenthesized_parameter_data<T: Folder>(data: ParenthesizedParameterData,
450 -> ParenthesizedParameterData {
451 let ParenthesizedParameterData { inputs, output, span } = data;
452 ParenthesizedParameterData {
453 inputs: inputs.move_map(|ty| fld.fold_ty(ty)),
454 output: output.map(|ty| fld.fold_ty(ty)),
455 span: fld.new_span(span),
459 pub fn noop_fold_local<T: Folder>(l: P<Local>, fld: &mut T) -> P<Local> {
460 l.map(|Local { id, pat, ty, init, span, attrs }| {
463 ty: ty.map(|t| fld.fold_ty(t)),
464 pat: fld.fold_pat(pat),
465 init: init.map(|e| fld.fold_expr(e)),
466 span: fld.new_span(span),
467 attrs: fold_attrs(attrs, fld),
472 pub fn noop_fold_attribute<T: Folder>(at: Attribute, fld: &mut T) -> Option<Attribute> {
473 let Spanned {node: Attribute_ {id, style, value, is_sugared_doc}, span} = at;
478 value: fld.fold_meta_item(value),
479 is_sugared_doc: is_sugared_doc,
481 span: fld.new_span(span),
485 pub fn noop_fold_meta_item<T: Folder>(mi: P<MetaItem>, fld: &mut T) -> P<MetaItem> {
486 mi.map(|Spanned { node, span }| {
489 MetaItemKind::Word(id) => MetaItemKind::Word(id),
490 MetaItemKind::List(id, mis) => {
491 MetaItemKind::List(id, mis.move_map(|e| fld.fold_meta_item(e)))
493 MetaItemKind::NameValue(id, s) => MetaItemKind::NameValue(id, s),
495 span: fld.new_span(span),
500 pub fn noop_fold_arg<T: Folder>(Arg { id, pat, ty }: Arg, fld: &mut T) -> Arg {
503 pat: fld.fold_pat(pat),
508 pub fn noop_fold_fn_decl<T: Folder>(decl: P<FnDecl>, fld: &mut T) -> P<FnDecl> {
509 decl.map(|FnDecl { inputs, output, variadic }| {
511 inputs: inputs.move_map(|x| fld.fold_arg(x)),
512 output: match output {
513 Return(ty) => Return(fld.fold_ty(ty)),
514 DefaultReturn(span) => DefaultReturn(span),
515 NoReturn(span) => NoReturn(span),
522 pub fn noop_fold_ty_param_bound<T>(tpb: TyParamBound, fld: &mut T) -> TyParamBound
526 TraitTyParamBound(ty, modifier) => TraitTyParamBound(fld.fold_poly_trait_ref(ty), modifier),
527 RegionTyParamBound(lifetime) => RegionTyParamBound(fld.fold_lifetime(lifetime)),
531 pub fn noop_fold_ty_param<T: Folder>(tp: TyParam, fld: &mut T) -> TyParam {
532 let TyParam {id, name, bounds, default, span} = tp;
536 bounds: fld.fold_bounds(bounds),
537 default: default.map(|x| fld.fold_ty(x)),
542 pub fn noop_fold_ty_params<T: Folder>(tps: HirVec<TyParam>,
545 tps.move_map(|tp| fld.fold_ty_param(tp))
548 pub fn noop_fold_lifetime<T: Folder>(l: Lifetime, fld: &mut T) -> Lifetime {
550 id: fld.new_id(l.id),
552 span: fld.new_span(l.span),
556 pub fn noop_fold_lifetime_def<T: Folder>(l: LifetimeDef, fld: &mut T) -> LifetimeDef {
558 lifetime: fld.fold_lifetime(l.lifetime),
559 bounds: fld.fold_lifetimes(l.bounds),
563 pub fn noop_fold_lifetimes<T: Folder>(lts: HirVec<Lifetime>, fld: &mut T) -> HirVec<Lifetime> {
564 lts.move_map(|l| fld.fold_lifetime(l))
567 pub fn noop_fold_lifetime_defs<T: Folder>(lts: HirVec<LifetimeDef>,
569 -> HirVec<LifetimeDef> {
570 lts.move_map(|l| fld.fold_lifetime_def(l))
573 pub fn noop_fold_opt_lifetime<T: Folder>(o_lt: Option<Lifetime>, fld: &mut T) -> Option<Lifetime> {
574 o_lt.map(|lt| fld.fold_lifetime(lt))
577 pub fn noop_fold_generics<T: Folder>(Generics { ty_params, lifetimes, where_clause }: Generics,
581 ty_params: fld.fold_ty_params(ty_params),
582 lifetimes: fld.fold_lifetime_defs(lifetimes),
583 where_clause: fld.fold_where_clause(where_clause),
587 pub fn noop_fold_where_clause<T: Folder>(WhereClause { id, predicates }: WhereClause,
592 predicates: predicates.move_map(|predicate| fld.fold_where_predicate(predicate)),
596 pub fn noop_fold_where_predicate<T: Folder>(pred: WherePredicate, fld: &mut T) -> WherePredicate {
598 hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate{bound_lifetimes,
602 hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
603 bound_lifetimes: fld.fold_lifetime_defs(bound_lifetimes),
604 bounded_ty: fld.fold_ty(bounded_ty),
605 bounds: bounds.move_map(|x| fld.fold_ty_param_bound(x)),
606 span: fld.new_span(span),
609 hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate{lifetime,
612 hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
613 span: fld.new_span(span),
614 lifetime: fld.fold_lifetime(lifetime),
615 bounds: bounds.move_map(|bound| fld.fold_lifetime(bound)),
618 hir::WherePredicate::EqPredicate(hir::WhereEqPredicate{id,
622 hir::WherePredicate::EqPredicate(hir::WhereEqPredicate {
624 path: fld.fold_path(path),
626 span: fld.new_span(span),
632 pub fn noop_fold_variant_data<T: Folder>(vdata: VariantData, fld: &mut T) -> VariantData {
634 VariantData::Struct(fields, id) => {
635 VariantData::Struct(fields.move_map(|f| fld.fold_struct_field(f)),
638 VariantData::Tuple(fields, id) => {
639 VariantData::Tuple(fields.move_map(|f| fld.fold_struct_field(f)),
642 VariantData::Unit(id) => VariantData::Unit(fld.new_id(id)),
646 pub fn noop_fold_trait_ref<T: Folder>(p: TraitRef, fld: &mut T) -> TraitRef {
647 let id = fld.new_id(p.ref_id);
653 path: fld.fold_path(path),
658 pub fn noop_fold_poly_trait_ref<T: Folder>(p: PolyTraitRef, fld: &mut T) -> PolyTraitRef {
660 bound_lifetimes: fld.fold_lifetime_defs(p.bound_lifetimes),
661 trait_ref: fld.fold_trait_ref(p.trait_ref),
662 span: fld.new_span(p.span),
666 pub fn noop_fold_struct_field<T: Folder>(f: StructField, fld: &mut T) -> StructField {
668 span: fld.new_span(f.span),
669 id: fld.new_id(f.id),
672 ty: fld.fold_ty(f.ty),
673 attrs: fold_attrs(f.attrs, fld),
677 pub fn noop_fold_field<T: Folder>(Field { name, expr, span }: Field, folder: &mut T) -> Field {
679 name: respan(folder.new_span(name.span), folder.fold_name(name.node)),
680 expr: folder.fold_expr(expr),
681 span: folder.new_span(span),
685 pub fn noop_fold_mt<T: Folder>(MutTy { ty, mutbl }: MutTy, folder: &mut T) -> MutTy {
687 ty: folder.fold_ty(ty),
692 pub fn noop_fold_opt_bounds<T: Folder>(b: Option<TyParamBounds>,
694 -> Option<TyParamBounds> {
695 b.map(|bounds| folder.fold_bounds(bounds))
698 fn noop_fold_bounds<T: Folder>(bounds: TyParamBounds, folder: &mut T) -> TyParamBounds {
699 bounds.move_map(|bound| folder.fold_ty_param_bound(bound))
702 pub fn noop_fold_block<T: Folder>(b: P<Block>, folder: &mut T) -> P<Block> {
703 b.map(|Block { id, stmts, expr, rules, span }| {
705 id: folder.new_id(id),
706 stmts: stmts.move_map(|s| folder.fold_stmt(s)),
707 expr: expr.map(|x| folder.fold_expr(x)),
709 span: folder.new_span(span),
714 pub fn noop_fold_item_underscore<T: Folder>(i: Item_, folder: &mut T) -> Item_ {
716 ItemExternCrate(string) => ItemExternCrate(string),
717 ItemUse(view_path) => {
718 ItemUse(folder.fold_view_path(view_path))
720 ItemStatic(t, m, e) => {
721 ItemStatic(folder.fold_ty(t), m, folder.fold_expr(e))
724 ItemConst(folder.fold_ty(t), folder.fold_expr(e))
726 ItemFn(decl, unsafety, constness, abi, generics, body) => {
727 ItemFn(folder.fold_fn_decl(decl),
731 folder.fold_generics(generics),
732 folder.fold_block(body))
734 ItemMod(m) => ItemMod(folder.fold_mod(m)),
735 ItemForeignMod(nm) => ItemForeignMod(folder.fold_foreign_mod(nm)),
736 ItemTy(t, generics) => {
737 ItemTy(folder.fold_ty(t), folder.fold_generics(generics))
739 ItemEnum(enum_definition, generics) => {
740 ItemEnum(hir::EnumDef {
741 variants: enum_definition.variants.move_map(|x| folder.fold_variant(x)),
743 folder.fold_generics(generics))
745 ItemStruct(struct_def, generics) => {
746 let struct_def = folder.fold_variant_data(struct_def);
747 ItemStruct(struct_def, folder.fold_generics(generics))
749 ItemDefaultImpl(unsafety, ref trait_ref) => {
750 ItemDefaultImpl(unsafety, folder.fold_trait_ref((*trait_ref).clone()))
752 ItemImpl(unsafety, polarity, generics, ifce, ty, impl_items) => {
753 let new_impl_items = impl_items
754 .move_map(|item| folder.fold_impl_item(item));
755 let ifce = match ifce {
757 Some(ref trait_ref) => {
758 Some(folder.fold_trait_ref((*trait_ref).clone()))
763 folder.fold_generics(generics),
768 ItemTrait(unsafety, generics, bounds, items) => {
769 let bounds = folder.fold_bounds(bounds);
770 let items = items.move_map(|item| folder.fold_trait_item(item));
771 ItemTrait(unsafety, folder.fold_generics(generics), bounds, items)
776 pub fn noop_fold_trait_item<T: Folder>(i: TraitItem,
780 id: folder.new_id(i.id),
781 name: folder.fold_name(i.name),
782 attrs: fold_attrs(i.attrs, folder),
784 ConstTraitItem(ty, default) => {
785 ConstTraitItem(folder.fold_ty(ty), default.map(|x| folder.fold_expr(x)))
787 MethodTraitItem(sig, body) => {
788 MethodTraitItem(noop_fold_method_sig(sig, folder),
789 body.map(|x| folder.fold_block(x)))
791 TypeTraitItem(bounds, default) => {
792 TypeTraitItem(folder.fold_bounds(bounds),
793 default.map(|x| folder.fold_ty(x)))
796 span: folder.new_span(i.span),
800 pub fn noop_fold_impl_item<T: Folder>(i: ImplItem, folder: &mut T) -> ImplItem {
802 id: folder.new_id(i.id),
803 name: folder.fold_name(i.name),
804 attrs: fold_attrs(i.attrs, folder),
806 defaultness: i.defaultness,
808 ImplItemKind::Const(ty, expr) => {
809 ImplItemKind::Const(folder.fold_ty(ty), folder.fold_expr(expr))
811 ImplItemKind::Method(sig, body) => {
812 ImplItemKind::Method(noop_fold_method_sig(sig, folder), folder.fold_block(body))
814 ImplItemKind::Type(ty) => ImplItemKind::Type(folder.fold_ty(ty)),
816 span: folder.new_span(i.span),
820 pub fn noop_fold_mod<T: Folder>(Mod { inner, item_ids }: Mod, folder: &mut T) -> Mod {
822 inner: folder.new_span(inner),
823 item_ids: item_ids.move_map(|x| folder.fold_item_id(x)),
827 pub fn noop_fold_crate<T: Folder>(Crate { module, attrs, config, span,
828 exported_macros, items }: Crate,
831 let config = folder.fold_meta_items(config);
833 let crate_mod = folder.fold_item(hir::Item {
834 name: keywords::Invalid.name(),
839 node: hir::ItemMod(module),
842 let (module, attrs, span) = match crate_mod {
843 hir::Item { attrs, span, node, .. } => {
845 hir::ItemMod(m) => (m, attrs, span),
846 _ => panic!("fold converted a module to not a module"),
851 let items = items.into_iter()
852 .map(|(id, item)| (id, folder.fold_item(item)))
860 exported_macros: exported_macros,
865 pub fn noop_fold_item_id<T: Folder>(i: ItemId, folder: &mut T) -> ItemId {
866 let id = folder.map_id(i.id);
870 // fold one item into one item
871 pub fn noop_fold_item<T: Folder>(item: Item, folder: &mut T) -> Item {
872 let Item { id, name, attrs, node, vis, span } = item;
873 let id = folder.new_id(id);
874 let node = folder.fold_item_underscore(node);
878 name: folder.fold_name(name),
879 attrs: fold_attrs(attrs, folder),
882 span: folder.new_span(span),
886 pub fn noop_fold_foreign_item<T: Folder>(ni: ForeignItem, folder: &mut T) -> ForeignItem {
888 id: folder.new_id(ni.id),
889 name: folder.fold_name(ni.name),
890 attrs: fold_attrs(ni.attrs, folder),
891 node: match ni.node {
892 ForeignItemFn(fdec, generics) => {
893 ForeignItemFn(folder.fold_fn_decl(fdec), folder.fold_generics(generics))
895 ForeignItemStatic(t, m) => {
896 ForeignItemStatic(folder.fold_ty(t), m)
900 span: folder.new_span(ni.span),
904 pub fn noop_fold_method_sig<T: Folder>(sig: MethodSig, folder: &mut T) -> MethodSig {
906 generics: folder.fold_generics(sig.generics),
908 unsafety: sig.unsafety,
909 constness: sig.constness,
910 decl: folder.fold_fn_decl(sig.decl),
914 pub fn noop_fold_pat<T: Folder>(p: P<Pat>, folder: &mut T) -> P<Pat> {
915 p.map(|Pat { id, node, span }| {
917 id: folder.new_id(id),
919 PatKind::Wild => PatKind::Wild,
920 PatKind::Binding(binding_mode, pth1, sub) => {
921 PatKind::Binding(binding_mode,
923 span: folder.new_span(pth1.span),
924 node: folder.fold_name(pth1.node),
926 sub.map(|x| folder.fold_pat(x)))
928 PatKind::Lit(e) => PatKind::Lit(folder.fold_expr(e)),
929 PatKind::TupleStruct(pth, pats, ddpos) => {
930 PatKind::TupleStruct(folder.fold_path(pth),
931 pats.move_map(|x| folder.fold_pat(x)), ddpos)
933 PatKind::Path(opt_qself, pth) => {
934 let opt_qself = opt_qself.map(|qself| {
935 QSelf { ty: folder.fold_ty(qself.ty), position: qself.position }
937 PatKind::Path(opt_qself, folder.fold_path(pth))
939 PatKind::Struct(pth, fields, etc) => {
940 let pth = folder.fold_path(pth);
941 let fs = fields.move_map(|f| {
943 span: folder.new_span(f.span),
944 node: hir::FieldPat {
946 pat: folder.fold_pat(f.node.pat),
947 is_shorthand: f.node.is_shorthand,
951 PatKind::Struct(pth, fs, etc)
953 PatKind::Tuple(elts, ddpos) => {
954 PatKind::Tuple(elts.move_map(|x| folder.fold_pat(x)), ddpos)
956 PatKind::Box(inner) => PatKind::Box(folder.fold_pat(inner)),
957 PatKind::Ref(inner, mutbl) => PatKind::Ref(folder.fold_pat(inner), mutbl),
958 PatKind::Range(e1, e2) => {
959 PatKind::Range(folder.fold_expr(e1), folder.fold_expr(e2))
961 PatKind::Vec(before, slice, after) => {
962 PatKind::Vec(before.move_map(|x| folder.fold_pat(x)),
963 slice.map(|x| folder.fold_pat(x)),
964 after.move_map(|x| folder.fold_pat(x)))
967 span: folder.new_span(span),
972 pub fn noop_fold_expr<T: Folder>(Expr { id, node, span, attrs }: Expr, folder: &mut T) -> Expr {
974 id: folder.new_id(id),
977 ExprBox(folder.fold_expr(e))
980 ExprVec(exprs.move_map(|x| folder.fold_expr(x)))
982 ExprRepeat(expr, count) => {
983 ExprRepeat(folder.fold_expr(expr), folder.fold_expr(count))
985 ExprTup(elts) => ExprTup(elts.move_map(|x| folder.fold_expr(x))),
986 ExprCall(f, args) => {
987 ExprCall(folder.fold_expr(f), args.move_map(|x| folder.fold_expr(x)))
989 ExprMethodCall(name, tps, args) => {
990 ExprMethodCall(respan(folder.new_span(name.span), folder.fold_name(name.node)),
991 tps.move_map(|x| folder.fold_ty(x)),
992 args.move_map(|x| folder.fold_expr(x)))
994 ExprBinary(binop, lhs, rhs) => {
995 ExprBinary(binop, folder.fold_expr(lhs), folder.fold_expr(rhs))
997 ExprUnary(binop, ohs) => {
998 ExprUnary(binop, folder.fold_expr(ohs))
1000 ExprLit(l) => ExprLit(l),
1001 ExprCast(expr, ty) => {
1002 ExprCast(folder.fold_expr(expr), folder.fold_ty(ty))
1004 ExprType(expr, ty) => {
1005 ExprType(folder.fold_expr(expr), folder.fold_ty(ty))
1007 ExprAddrOf(m, ohs) => ExprAddrOf(m, folder.fold_expr(ohs)),
1008 ExprIf(cond, tr, fl) => {
1009 ExprIf(folder.fold_expr(cond),
1010 folder.fold_block(tr),
1011 fl.map(|x| folder.fold_expr(x)))
1013 ExprWhile(cond, body, opt_name) => {
1014 ExprWhile(folder.fold_expr(cond),
1015 folder.fold_block(body),
1016 opt_name.map(|label| {
1017 respan(folder.new_span(label.span), folder.fold_name(label.node))
1020 ExprLoop(body, opt_name) => {
1021 ExprLoop(folder.fold_block(body),
1022 opt_name.map(|label| {
1023 respan(folder.new_span(label.span), folder.fold_name(label.node))
1026 ExprMatch(expr, arms, source) => {
1027 ExprMatch(folder.fold_expr(expr),
1028 arms.move_map(|x| folder.fold_arm(x)),
1031 ExprClosure(capture_clause, decl, body, fn_decl_span) => {
1032 ExprClosure(capture_clause,
1033 folder.fold_fn_decl(decl),
1034 folder.fold_block(body),
1035 folder.new_span(fn_decl_span))
1037 ExprBlock(blk) => ExprBlock(folder.fold_block(blk)),
1038 ExprAssign(el, er) => {
1039 ExprAssign(folder.fold_expr(el), folder.fold_expr(er))
1041 ExprAssignOp(op, el, er) => {
1042 ExprAssignOp(op, folder.fold_expr(el), folder.fold_expr(er))
1044 ExprField(el, name) => {
1045 ExprField(folder.fold_expr(el),
1046 respan(folder.new_span(name.span), folder.fold_name(name.node)))
1048 ExprTupField(el, index) => {
1049 ExprTupField(folder.fold_expr(el),
1050 respan(folder.new_span(index.span), folder.fold_usize(index.node)))
1052 ExprIndex(el, er) => {
1053 ExprIndex(folder.fold_expr(el), folder.fold_expr(er))
1055 ExprPath(qself, path) => {
1056 let qself = qself.map(|QSelf { ty, position }| {
1058 ty: folder.fold_ty(ty),
1062 ExprPath(qself, folder.fold_path(path))
1064 ExprBreak(opt_name) => ExprBreak(opt_name.map(|label| {
1065 respan(folder.new_span(label.span), folder.fold_name(label.node))
1067 ExprAgain(opt_name) => ExprAgain(opt_name.map(|label| {
1068 respan(folder.new_span(label.span), folder.fold_name(label.node))
1070 ExprRet(e) => ExprRet(e.map(|x| folder.fold_expr(x))),
1071 ExprInlineAsm(asm, outputs, inputs) => {
1073 outputs.move_map(|x| folder.fold_expr(x)),
1074 inputs.move_map(|x| folder.fold_expr(x)))
1076 ExprStruct(path, fields, maybe_expr) => {
1077 ExprStruct(folder.fold_path(path),
1078 fields.move_map(|x| folder.fold_field(x)),
1079 maybe_expr.map(|x| folder.fold_expr(x)))
1082 span: folder.new_span(span),
1083 attrs: fold_attrs(attrs, folder),
1087 pub fn noop_fold_stmt<T: Folder>(stmt: Stmt, folder: &mut T) -> Stmt {
1088 let span = folder.new_span(stmt.span);
1090 StmtDecl(d, id) => {
1091 let id = folder.new_id(id);
1093 node: StmtDecl(folder.fold_decl(d), id),
1097 StmtExpr(e, id) => {
1098 let id = folder.new_id(id);
1100 node: StmtExpr(folder.fold_expr(e), id),
1104 StmtSemi(e, id) => {
1105 let id = folder.new_id(id);
1107 node: StmtSemi(folder.fold_expr(e), id),