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::{Ident, NodeId, DUMMY_NODE_ID};
17 use syntax::codemap::{respan, Span, Spanned};
18 use syntax::owned_slice::OwnedSlice;
20 use syntax::parse::token;
22 use syntax::util::small_vector::SmallVector;
25 // This could have a better place to live.
26 pub trait MoveMap<T> {
27 fn move_map<F>(self, f: F) -> Self where F: FnMut(T) -> T;
30 impl<T> MoveMap<T> for Vec<T> {
31 fn move_map<F>(mut self, mut f: F) -> Vec<T> where F: FnMut(T) -> T {
34 // FIXME(#5016) this shouldn't need to zero to be safe.
35 ptr::write(p, f(ptr::read_and_drop(p)));
42 impl<T> MoveMap<T> for OwnedSlice<T> {
43 fn move_map<F>(self, f: F) -> OwnedSlice<T> where F: FnMut(T) -> T {
44 OwnedSlice::from_vec(self.into_vec().move_map(f))
48 pub trait Folder : Sized {
49 // Any additions to this trait should happen in form
50 // of a call to a public `noop_*` function that only calls
51 // out to the folder again, not other `noop_*` functions.
53 // This is a necessary API workaround to the problem of not
54 // being able to call out to the super default method
55 // in an overridden default method.
57 fn fold_crate(&mut self, c: Crate) -> Crate {
58 noop_fold_crate(c, self)
61 fn fold_meta_items(&mut self, meta_items: Vec<P<MetaItem>>) -> Vec<P<MetaItem>> {
62 noop_fold_meta_items(meta_items, self)
65 fn fold_meta_item(&mut self, meta_item: P<MetaItem>) -> P<MetaItem> {
66 noop_fold_meta_item(meta_item, self)
69 fn fold_view_path(&mut self, view_path: P<ViewPath>) -> P<ViewPath> {
70 noop_fold_view_path(view_path, self)
73 fn fold_foreign_item(&mut self, ni: P<ForeignItem>) -> P<ForeignItem> {
74 noop_fold_foreign_item(ni, self)
77 fn fold_item(&mut self, i: P<Item>) -> SmallVector<P<Item>> {
78 noop_fold_item(i, self)
81 fn fold_item_simple(&mut self, i: Item) -> Item {
82 noop_fold_item_simple(i, self)
85 fn fold_struct_field(&mut self, sf: StructField) -> StructField {
86 noop_fold_struct_field(sf, self)
89 fn fold_item_underscore(&mut self, i: Item_) -> Item_ {
90 noop_fold_item_underscore(i, self)
93 fn fold_trait_item(&mut self, i: P<TraitItem>) -> SmallVector<P<TraitItem>> {
94 noop_fold_trait_item(i, self)
97 fn fold_impl_item(&mut self, i: P<ImplItem>) -> SmallVector<P<ImplItem>> {
98 noop_fold_impl_item(i, self)
101 fn fold_fn_decl(&mut self, d: P<FnDecl>) -> P<FnDecl> {
102 noop_fold_fn_decl(d, self)
105 fn fold_block(&mut self, b: P<Block>) -> P<Block> {
106 noop_fold_block(b, self)
109 fn fold_stmt(&mut self, s: P<Stmt>) -> SmallVector<P<Stmt>> {
110 s.and_then(|s| noop_fold_stmt(s, self))
113 fn fold_arm(&mut self, a: Arm) -> Arm {
114 noop_fold_arm(a, self)
117 fn fold_pat(&mut self, p: P<Pat>) -> P<Pat> {
118 noop_fold_pat(p, self)
121 fn fold_decl(&mut self, d: P<Decl>) -> SmallVector<P<Decl>> {
122 noop_fold_decl(d, self)
125 fn fold_expr(&mut self, e: P<Expr>) -> P<Expr> {
126 e.map(|e| noop_fold_expr(e, self))
129 fn fold_ty(&mut self, t: P<Ty>) -> P<Ty> {
130 noop_fold_ty(t, self)
133 fn fold_ty_binding(&mut self, t: P<TypeBinding>) -> P<TypeBinding> {
134 noop_fold_ty_binding(t, self)
137 fn fold_mod(&mut self, m: Mod) -> Mod {
138 noop_fold_mod(m, self)
141 fn fold_foreign_mod(&mut self, nm: ForeignMod) -> ForeignMod {
142 noop_fold_foreign_mod(nm, self)
145 fn fold_variant(&mut self, v: P<Variant>) -> P<Variant> {
146 noop_fold_variant(v, self)
149 fn fold_ident(&mut self, i: Ident) -> Ident {
150 noop_fold_ident(i, self)
153 fn fold_usize(&mut self, i: usize) -> usize {
154 noop_fold_usize(i, self)
157 fn fold_path(&mut self, p: Path) -> Path {
158 noop_fold_path(p, self)
161 fn fold_path_parameters(&mut self, p: PathParameters) -> PathParameters {
162 noop_fold_path_parameters(p, self)
165 fn fold_angle_bracketed_parameter_data(&mut self, p: AngleBracketedParameterData)
166 -> AngleBracketedParameterData
168 noop_fold_angle_bracketed_parameter_data(p, self)
171 fn fold_parenthesized_parameter_data(&mut self, p: ParenthesizedParameterData)
172 -> ParenthesizedParameterData
174 noop_fold_parenthesized_parameter_data(p, self)
177 fn fold_local(&mut self, l: P<Local>) -> P<Local> {
178 noop_fold_local(l, self)
181 fn fold_explicit_self(&mut self, es: ExplicitSelf) -> ExplicitSelf {
182 noop_fold_explicit_self(es, self)
185 fn fold_explicit_self_underscore(&mut self, es: ExplicitSelf_) -> ExplicitSelf_ {
186 noop_fold_explicit_self_underscore(es, self)
189 fn fold_lifetime(&mut self, l: Lifetime) -> Lifetime {
190 noop_fold_lifetime(l, self)
193 fn fold_lifetime_def(&mut self, l: LifetimeDef) -> LifetimeDef {
194 noop_fold_lifetime_def(l, self)
197 fn fold_attribute(&mut self, at: Attribute) -> Option<Attribute> {
198 noop_fold_attribute(at, self)
201 fn fold_arg(&mut self, a: Arg) -> Arg {
202 noop_fold_arg(a, self)
205 fn fold_generics(&mut self, generics: Generics) -> Generics {
206 noop_fold_generics(generics, self)
209 fn fold_trait_ref(&mut self, p: TraitRef) -> TraitRef {
210 noop_fold_trait_ref(p, self)
213 fn fold_poly_trait_ref(&mut self, p: PolyTraitRef) -> PolyTraitRef {
214 noop_fold_poly_trait_ref(p, self)
217 fn fold_struct_def(&mut self, struct_def: P<StructDef>) -> P<StructDef> {
218 noop_fold_struct_def(struct_def, self)
221 fn fold_lifetimes(&mut self, lts: Vec<Lifetime>) -> Vec<Lifetime> {
222 noop_fold_lifetimes(lts, self)
225 fn fold_lifetime_defs(&mut self, lts: Vec<LifetimeDef>) -> Vec<LifetimeDef> {
226 noop_fold_lifetime_defs(lts, self)
229 fn fold_ty_param(&mut self, tp: TyParam) -> TyParam {
230 noop_fold_ty_param(tp, self)
233 fn fold_ty_params(&mut self, tps: OwnedSlice<TyParam>) -> OwnedSlice<TyParam> {
234 noop_fold_ty_params(tps, self)
237 fn fold_opt_lifetime(&mut self, o_lt: Option<Lifetime>) -> Option<Lifetime> {
238 noop_fold_opt_lifetime(o_lt, self)
241 fn fold_variant_arg(&mut self, va: VariantArg) -> VariantArg {
242 noop_fold_variant_arg(va, self)
245 fn fold_opt_bounds(&mut self, b: Option<OwnedSlice<TyParamBound>>)
246 -> Option<OwnedSlice<TyParamBound>> {
247 noop_fold_opt_bounds(b, self)
250 fn fold_bounds(&mut self, b: OwnedSlice<TyParamBound>)
251 -> OwnedSlice<TyParamBound> {
252 noop_fold_bounds(b, self)
255 fn fold_ty_param_bound(&mut self, tpb: TyParamBound) -> TyParamBound {
256 noop_fold_ty_param_bound(tpb, self)
259 fn fold_mt(&mut self, mt: MutTy) -> MutTy {
260 noop_fold_mt(mt, self)
263 fn fold_field(&mut self, field: Field) -> Field {
264 noop_fold_field(field, self)
267 fn fold_where_clause(&mut self, where_clause: WhereClause)
269 noop_fold_where_clause(where_clause, self)
272 fn fold_where_predicate(&mut self, where_predicate: WherePredicate)
274 noop_fold_where_predicate(where_predicate, self)
277 fn new_id(&mut self, i: NodeId) -> NodeId {
281 fn new_span(&mut self, sp: Span) -> Span {
286 pub fn noop_fold_meta_items<T: Folder>(meta_items: Vec<P<MetaItem>>, fld: &mut T)
287 -> Vec<P<MetaItem>> {
288 meta_items.move_map(|x| fld.fold_meta_item(x))
291 pub fn noop_fold_view_path<T: Folder>(view_path: P<ViewPath>, fld: &mut T) -> P<ViewPath> {
292 view_path.map(|Spanned {node, span}| Spanned {
294 ViewPathSimple(ident, path) => {
295 ViewPathSimple(ident, fld.fold_path(path))
297 ViewPathGlob(path) => {
298 ViewPathGlob(fld.fold_path(path))
300 ViewPathList(path, path_list_idents) => {
301 ViewPathList(fld.fold_path(path),
302 path_list_idents.move_map(|path_list_ident| {
304 node: match path_list_ident.node {
305 PathListIdent { id, name, rename } =>
311 PathListMod { id, rename } =>
317 span: fld.new_span(path_list_ident.span)
322 span: fld.new_span(span)
326 pub fn fold_attrs<T: Folder>(attrs: Vec<Attribute>, fld: &mut T) -> Vec<Attribute> {
327 attrs.into_iter().flat_map(|x| fld.fold_attribute(x)).collect()
330 pub fn noop_fold_arm<T: Folder>(Arm {attrs, pats, guard, body}: Arm, fld: &mut T) -> Arm {
332 attrs: fold_attrs(attrs, fld),
333 pats: pats.move_map(|x| fld.fold_pat(x)),
334 guard: guard.map(|x| fld.fold_expr(x)),
335 body: fld.fold_expr(body),
339 pub fn noop_fold_decl<T: Folder>(d: P<Decl>, fld: &mut T) -> SmallVector<P<Decl>> {
340 d.and_then(|Spanned {node, span}| match node {
341 DeclLocal(l) => SmallVector::one(P(Spanned {
342 node: DeclLocal(fld.fold_local(l)),
343 span: fld.new_span(span)
345 DeclItem(it) => fld.fold_item(it).into_iter().map(|i| P(Spanned {
347 span: fld.new_span(span)
352 pub fn noop_fold_ty_binding<T: Folder>(b: P<TypeBinding>, fld: &mut T) -> P<TypeBinding> {
353 b.map(|TypeBinding { id, ident, ty, span }| TypeBinding {
357 span: fld.new_span(span),
361 pub fn noop_fold_ty<T: Folder>(t: P<Ty>, fld: &mut T) -> P<Ty> {
362 t.map(|Ty {id, node, span}| Ty {
366 TyVec(ty) => TyVec(fld.fold_ty(ty)),
367 TyPtr(mt) => TyPtr(fld.fold_mt(mt)),
368 TyRptr(region, mt) => {
369 TyRptr(fld.fold_opt_lifetime(region), fld.fold_mt(mt))
372 TyBareFn(f.map(|BareFnTy {lifetimes, unsafety, abi, decl}| BareFnTy {
373 lifetimes: fld.fold_lifetime_defs(lifetimes),
376 decl: fld.fold_fn_decl(decl)
379 TyTup(tys) => TyTup(tys.move_map(|ty| fld.fold_ty(ty))),
380 TyParen(ty) => TyParen(fld.fold_ty(ty)),
381 TyPath(qself, path) => {
382 let qself = qself.map(|QSelf { ty, position }| {
388 TyPath(qself, fld.fold_path(path))
390 TyObjectSum(ty, bounds) => {
391 TyObjectSum(fld.fold_ty(ty),
392 fld.fold_bounds(bounds))
394 TyFixedLengthVec(ty, e) => {
395 TyFixedLengthVec(fld.fold_ty(ty), fld.fold_expr(e))
398 TyTypeof(fld.fold_expr(expr))
400 TyPolyTraitRef(bounds) => {
401 TyPolyTraitRef(bounds.move_map(|b| fld.fold_ty_param_bound(b)))
404 span: fld.new_span(span)
408 pub fn noop_fold_foreign_mod<T: Folder>(ForeignMod {abi, items}: ForeignMod,
409 fld: &mut T) -> ForeignMod {
412 items: items.move_map(|x| fld.fold_foreign_item(x)),
416 pub fn noop_fold_variant<T: Folder>(v: P<Variant>, fld: &mut T) -> P<Variant> {
417 v.map(|Spanned {node: Variant_ {id, name, attrs, kind, disr_expr, vis}, span}| Spanned {
421 attrs: fold_attrs(attrs, fld),
423 TupleVariantKind(variant_args) => {
424 TupleVariantKind(variant_args.move_map(|x|
425 fld.fold_variant_arg(x)))
427 StructVariantKind(struct_def) => {
428 StructVariantKind(fld.fold_struct_def(struct_def))
431 disr_expr: disr_expr.map(|e| fld.fold_expr(e)),
434 span: fld.new_span(span),
438 pub fn noop_fold_ident<T: Folder>(i: Ident, _: &mut T) -> Ident {
442 pub fn noop_fold_usize<T: Folder>(i: usize, _: &mut T) -> usize {
446 pub fn noop_fold_path<T: Folder>(Path {global, segments, span}: Path, fld: &mut T) -> Path {
449 segments: segments.move_map(|PathSegment {identifier, parameters}| PathSegment {
450 identifier: fld.fold_ident(identifier),
451 parameters: fld.fold_path_parameters(parameters),
453 span: fld.new_span(span)
457 pub fn noop_fold_path_parameters<T: Folder>(path_parameters: PathParameters, fld: &mut T)
460 match path_parameters {
461 AngleBracketedParameters(data) =>
462 AngleBracketedParameters(fld.fold_angle_bracketed_parameter_data(data)),
463 ParenthesizedParameters(data) =>
464 ParenthesizedParameters(fld.fold_parenthesized_parameter_data(data)),
468 pub fn noop_fold_angle_bracketed_parameter_data<T: Folder>(data: AngleBracketedParameterData,
470 -> AngleBracketedParameterData
472 let AngleBracketedParameterData { lifetimes, types, bindings } = data;
473 AngleBracketedParameterData { lifetimes: fld.fold_lifetimes(lifetimes),
474 types: types.move_map(|ty| fld.fold_ty(ty)),
475 bindings: bindings.move_map(|b| fld.fold_ty_binding(b)) }
478 pub fn noop_fold_parenthesized_parameter_data<T: Folder>(data: ParenthesizedParameterData,
480 -> ParenthesizedParameterData
482 let ParenthesizedParameterData { inputs, output, span } = data;
483 ParenthesizedParameterData { inputs: inputs.move_map(|ty| fld.fold_ty(ty)),
484 output: output.map(|ty| fld.fold_ty(ty)),
485 span: fld.new_span(span) }
488 pub fn noop_fold_local<T: Folder>(l: P<Local>, fld: &mut T) -> P<Local> {
489 l.map(|Local {id, pat, ty, init, span}| Local {
491 ty: ty.map(|t| fld.fold_ty(t)),
492 pat: fld.fold_pat(pat),
493 init: init.map(|e| fld.fold_expr(e)),
494 span: fld.new_span(span)
498 pub fn noop_fold_attribute<T: Folder>(at: Attribute, fld: &mut T) -> Option<Attribute> {
499 let Spanned {node: Attribute_ {id, style, value, is_sugared_doc}, span} = at;
504 value: fld.fold_meta_item(value),
505 is_sugared_doc: is_sugared_doc
507 span: fld.new_span(span)
511 pub fn noop_fold_explicit_self_underscore<T: Folder>(es: ExplicitSelf_, fld: &mut T)
514 SelfStatic | SelfValue(_) => es,
515 SelfRegion(lifetime, m, ident) => {
516 SelfRegion(fld.fold_opt_lifetime(lifetime), m, ident)
518 SelfExplicit(typ, ident) => {
519 SelfExplicit(fld.fold_ty(typ), ident)
524 pub fn noop_fold_explicit_self<T: Folder>(Spanned {span, node}: ExplicitSelf, fld: &mut T)
527 node: fld.fold_explicit_self_underscore(node),
528 span: fld.new_span(span)
532 pub fn noop_fold_meta_item<T: Folder>(mi: P<MetaItem>, fld: &mut T) -> P<MetaItem> {
533 mi.map(|Spanned {node, span}| Spanned {
535 MetaWord(id) => MetaWord(id),
536 MetaList(id, mis) => {
537 MetaList(id, mis.move_map(|e| fld.fold_meta_item(e)))
539 MetaNameValue(id, s) => MetaNameValue(id, s)
541 span: fld.new_span(span)
545 pub fn noop_fold_arg<T: Folder>(Arg {id, pat, ty}: Arg, fld: &mut T) -> Arg {
548 pat: fld.fold_pat(pat),
553 pub fn noop_fold_fn_decl<T: Folder>(decl: P<FnDecl>, fld: &mut T) -> P<FnDecl> {
554 decl.map(|FnDecl {inputs, output, variadic}| FnDecl {
555 inputs: inputs.move_map(|x| fld.fold_arg(x)),
556 output: match output {
557 Return(ty) => Return(fld.fold_ty(ty)),
558 DefaultReturn(span) => DefaultReturn(span),
559 NoReturn(span) => NoReturn(span)
565 pub fn noop_fold_ty_param_bound<T>(tpb: TyParamBound, fld: &mut T)
569 TraitTyParamBound(ty, modifier) => TraitTyParamBound(fld.fold_poly_trait_ref(ty), modifier),
570 RegionTyParamBound(lifetime) => RegionTyParamBound(fld.fold_lifetime(lifetime)),
574 pub fn noop_fold_ty_param<T: Folder>(tp: TyParam, fld: &mut T) -> TyParam {
575 let TyParam {id, ident, bounds, default, span} = tp;
579 bounds: fld.fold_bounds(bounds),
580 default: default.map(|x| fld.fold_ty(x)),
585 pub fn noop_fold_ty_params<T: Folder>(tps: OwnedSlice<TyParam>, fld: &mut T)
586 -> OwnedSlice<TyParam> {
587 tps.move_map(|tp| fld.fold_ty_param(tp))
590 pub fn noop_fold_lifetime<T: Folder>(l: Lifetime, fld: &mut T) -> Lifetime {
592 id: fld.new_id(l.id),
594 span: fld.new_span(l.span)
598 pub fn noop_fold_lifetime_def<T: Folder>(l: LifetimeDef, fld: &mut T)
601 lifetime: fld.fold_lifetime(l.lifetime),
602 bounds: fld.fold_lifetimes(l.bounds),
606 pub fn noop_fold_lifetimes<T: Folder>(lts: Vec<Lifetime>, fld: &mut T) -> Vec<Lifetime> {
607 lts.move_map(|l| fld.fold_lifetime(l))
610 pub fn noop_fold_lifetime_defs<T: Folder>(lts: Vec<LifetimeDef>, fld: &mut T)
611 -> Vec<LifetimeDef> {
612 lts.move_map(|l| fld.fold_lifetime_def(l))
615 pub fn noop_fold_opt_lifetime<T: Folder>(o_lt: Option<Lifetime>, fld: &mut T)
616 -> Option<Lifetime> {
617 o_lt.map(|lt| fld.fold_lifetime(lt))
620 pub fn noop_fold_generics<T: Folder>(Generics {ty_params, lifetimes, where_clause}: Generics,
621 fld: &mut T) -> Generics {
623 ty_params: fld.fold_ty_params(ty_params),
624 lifetimes: fld.fold_lifetime_defs(lifetimes),
625 where_clause: fld.fold_where_clause(where_clause),
629 pub fn noop_fold_where_clause<T: Folder>(
630 WhereClause {id, predicates}: WhereClause,
635 predicates: predicates.move_map(|predicate| {
636 fld.fold_where_predicate(predicate)
641 pub fn noop_fold_where_predicate<T: Folder>(
642 pred: WherePredicate,
646 hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate{bound_lifetimes,
650 hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
651 bound_lifetimes: fld.fold_lifetime_defs(bound_lifetimes),
652 bounded_ty: fld.fold_ty(bounded_ty),
653 bounds: bounds.move_map(|x| fld.fold_ty_param_bound(x)),
654 span: fld.new_span(span)
657 hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate{lifetime,
660 hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
661 span: fld.new_span(span),
662 lifetime: fld.fold_lifetime(lifetime),
663 bounds: bounds.move_map(|bound| fld.fold_lifetime(bound))
666 hir::WherePredicate::EqPredicate(hir::WhereEqPredicate{id,
670 hir::WherePredicate::EqPredicate(hir::WhereEqPredicate{
672 path: fld.fold_path(path),
674 span: fld.new_span(span)
680 pub fn noop_fold_struct_def<T: Folder>(struct_def: P<StructDef>, fld: &mut T) -> P<StructDef> {
681 struct_def.map(|StructDef { fields, ctor_id }| StructDef {
682 fields: fields.move_map(|f| fld.fold_struct_field(f)),
683 ctor_id: ctor_id.map(|cid| fld.new_id(cid)),
687 pub fn noop_fold_trait_ref<T: Folder>(p: TraitRef, fld: &mut T) -> TraitRef {
688 let id = fld.new_id(p.ref_id);
694 path: fld.fold_path(path),
699 pub fn noop_fold_poly_trait_ref<T: Folder>(p: PolyTraitRef, fld: &mut T) -> PolyTraitRef {
701 bound_lifetimes: fld.fold_lifetime_defs(p.bound_lifetimes),
702 trait_ref: fld.fold_trait_ref(p.trait_ref),
703 span: fld.new_span(p.span),
707 pub fn noop_fold_struct_field<T: Folder>(f: StructField, fld: &mut T) -> StructField {
708 let StructField {node: StructField_ {id, kind, ty, attrs}, span} = f;
714 attrs: fold_attrs(attrs, fld),
716 span: fld.new_span(span)
720 pub fn noop_fold_field<T: Folder>(Field {ident, expr, span}: Field, folder: &mut T) -> Field {
722 ident: respan(ident.span, folder.fold_ident(ident.node)),
723 expr: folder.fold_expr(expr),
724 span: folder.new_span(span)
728 pub fn noop_fold_mt<T: Folder>(MutTy {ty, mutbl}: MutTy, folder: &mut T) -> MutTy {
730 ty: folder.fold_ty(ty),
735 pub fn noop_fold_opt_bounds<T: Folder>(b: Option<OwnedSlice<TyParamBound>>, folder: &mut T)
736 -> Option<OwnedSlice<TyParamBound>> {
737 b.map(|bounds| folder.fold_bounds(bounds))
740 fn noop_fold_bounds<T: Folder>(bounds: TyParamBounds, folder: &mut T)
742 bounds.move_map(|bound| folder.fold_ty_param_bound(bound))
745 fn noop_fold_variant_arg<T: Folder>(VariantArg {id, ty}: VariantArg, folder: &mut T)
748 id: folder.new_id(id),
749 ty: folder.fold_ty(ty)
753 pub fn noop_fold_block<T: Folder>(b: P<Block>, folder: &mut T) -> P<Block> {
754 b.map(|Block {id, stmts, expr, rules, span}| Block {
755 id: folder.new_id(id),
756 stmts: stmts.into_iter().flat_map(|s| folder.fold_stmt(s).into_iter()).collect(),
757 expr: expr.map(|x| folder.fold_expr(x)),
759 span: folder.new_span(span),
763 pub fn noop_fold_item_underscore<T: Folder>(i: Item_, folder: &mut T) -> Item_ {
765 ItemExternCrate(string) => ItemExternCrate(string),
766 ItemUse(view_path) => {
767 ItemUse(folder.fold_view_path(view_path))
769 ItemStatic(t, m, e) => {
770 ItemStatic(folder.fold_ty(t), m, folder.fold_expr(e))
773 ItemConst(folder.fold_ty(t), folder.fold_expr(e))
775 ItemFn(decl, unsafety, constness, abi, generics, body) => {
777 folder.fold_fn_decl(decl),
781 folder.fold_generics(generics),
782 folder.fold_block(body)
785 ItemMod(m) => ItemMod(folder.fold_mod(m)),
786 ItemForeignMod(nm) => ItemForeignMod(folder.fold_foreign_mod(nm)),
787 ItemTy(t, generics) => {
788 ItemTy(folder.fold_ty(t), folder.fold_generics(generics))
790 ItemEnum(enum_definition, generics) => {
793 variants: enum_definition.variants.move_map(|x| folder.fold_variant(x)),
795 folder.fold_generics(generics))
797 ItemStruct(struct_def, generics) => {
798 let struct_def = folder.fold_struct_def(struct_def);
799 ItemStruct(struct_def, folder.fold_generics(generics))
801 ItemDefaultImpl(unsafety, ref trait_ref) => {
802 ItemDefaultImpl(unsafety, folder.fold_trait_ref((*trait_ref).clone()))
804 ItemImpl(unsafety, polarity, generics, ifce, ty, impl_items) => {
805 let new_impl_items = impl_items.into_iter().flat_map(|item| {
806 folder.fold_impl_item(item).into_iter()
808 let ifce = match ifce {
810 Some(ref trait_ref) => {
811 Some(folder.fold_trait_ref((*trait_ref).clone()))
816 folder.fold_generics(generics),
821 ItemTrait(unsafety, generics, bounds, items) => {
822 let bounds = folder.fold_bounds(bounds);
823 let items = items.into_iter().flat_map(|item| {
824 folder.fold_trait_item(item).into_iter()
827 folder.fold_generics(generics),
834 pub fn noop_fold_trait_item<T: Folder>(i: P<TraitItem>, folder: &mut T)
835 -> SmallVector<P<TraitItem>> {
836 SmallVector::one(i.map(|TraitItem {id, ident, attrs, node, span}| TraitItem {
837 id: folder.new_id(id),
838 ident: folder.fold_ident(ident),
839 attrs: fold_attrs(attrs, folder),
841 ConstTraitItem(ty, default) => {
842 ConstTraitItem(folder.fold_ty(ty),
843 default.map(|x| folder.fold_expr(x)))
845 MethodTraitItem(sig, body) => {
846 MethodTraitItem(noop_fold_method_sig(sig, folder),
847 body.map(|x| folder.fold_block(x)))
849 TypeTraitItem(bounds, default) => {
850 TypeTraitItem(folder.fold_bounds(bounds),
851 default.map(|x| folder.fold_ty(x)))
854 span: folder.new_span(span)
858 pub fn noop_fold_impl_item<T: Folder>(i: P<ImplItem>, folder: &mut T)
859 -> SmallVector<P<ImplItem>> {
860 SmallVector::one(i.map(|ImplItem {id, ident, attrs, node, vis, span}| ImplItem {
861 id: folder.new_id(id),
862 ident: folder.fold_ident(ident),
863 attrs: fold_attrs(attrs, folder),
866 ConstImplItem(ty, expr) => {
867 ConstImplItem(folder.fold_ty(ty), folder.fold_expr(expr))
869 MethodImplItem(sig, body) => {
870 MethodImplItem(noop_fold_method_sig(sig, folder),
871 folder.fold_block(body))
873 TypeImplItem(ty) => TypeImplItem(folder.fold_ty(ty)),
875 span: folder.new_span(span)
879 pub fn noop_fold_mod<T: Folder>(Mod {inner, items}: Mod, folder: &mut T) -> Mod {
881 inner: folder.new_span(inner),
882 items: items.into_iter().flat_map(|x| folder.fold_item(x).into_iter()).collect(),
886 pub fn noop_fold_crate<T: Folder>(Crate {module, attrs, config, span, exported_macros}: Crate,
887 folder: &mut T) -> Crate {
888 let config = folder.fold_meta_items(config);
890 let mut items = folder.fold_item(P(hir::Item {
891 ident: token::special_idents::invalid,
896 node: hir::ItemMod(module),
899 let (module, attrs, span) = match items.next() {
901 assert!(items.next().is_none(),
902 "a crate cannot expand to more than one item");
903 item.and_then(|hir::Item { attrs, span, node, .. }| {
905 hir::ItemMod(m) => (m, attrs, span),
906 _ => panic!("fold converted a module to not a module"),
921 exported_macros: exported_macros,
925 // fold one item into possibly many items
926 pub fn noop_fold_item<T: Folder>(i: P<Item>, folder: &mut T) -> SmallVector<P<Item>> {
927 SmallVector::one(i.map(|i| folder.fold_item_simple(i)))
930 // fold one item into exactly one item
931 pub fn noop_fold_item_simple<T: Folder>(Item {id, ident, attrs, node, vis, span}: Item,
932 folder: &mut T) -> Item {
933 let id = folder.new_id(id);
934 let node = folder.fold_item_underscore(node);
935 // FIXME: we should update the impl_pretty_name, but it uses pretty printing.
936 // let ident = match node {
937 // // The node may have changed, recompute the "pretty" impl name.
938 // ItemImpl(_, _, _, ref maybe_trait, ref ty, _) => {
939 // impl_pretty_name(maybe_trait, Some(&**ty))
946 ident: folder.fold_ident(ident),
947 attrs: fold_attrs(attrs, folder),
950 span: folder.new_span(span)
954 pub fn noop_fold_foreign_item<T: Folder>(ni: P<ForeignItem>, folder: &mut T) -> P<ForeignItem> {
955 ni.map(|ForeignItem {id, ident, attrs, node, span, vis}| ForeignItem {
956 id: folder.new_id(id),
957 ident: folder.fold_ident(ident),
958 attrs: fold_attrs(attrs, folder),
960 ForeignItemFn(fdec, generics) => {
961 ForeignItemFn(folder.fold_fn_decl(fdec), folder.fold_generics(generics))
963 ForeignItemStatic(t, m) => {
964 ForeignItemStatic(folder.fold_ty(t), m)
968 span: folder.new_span(span)
972 pub fn noop_fold_method_sig<T: Folder>(sig: MethodSig, folder: &mut T) -> MethodSig {
974 generics: folder.fold_generics(sig.generics),
976 explicit_self: folder.fold_explicit_self(sig.explicit_self),
977 unsafety: sig.unsafety,
978 constness: sig.constness,
979 decl: folder.fold_fn_decl(sig.decl)
983 pub fn noop_fold_pat<T: Folder>(p: P<Pat>, folder: &mut T) -> P<Pat> {
984 p.map(|Pat {id, node, span}| Pat {
985 id: folder.new_id(id),
987 PatWild(k) => PatWild(k),
988 PatIdent(binding_mode, pth1, sub) => {
989 PatIdent(binding_mode,
990 Spanned{span: folder.new_span(pth1.span),
991 node: folder.fold_ident(pth1.node)},
992 sub.map(|x| folder.fold_pat(x)))
994 PatLit(e) => PatLit(folder.fold_expr(e)),
995 PatEnum(pth, pats) => {
996 PatEnum(folder.fold_path(pth),
997 pats.map(|pats| pats.move_map(|x| folder.fold_pat(x))))
999 PatQPath(qself, pth) => {
1000 let qself = QSelf {ty: folder.fold_ty(qself.ty), .. qself};
1001 PatQPath(qself, folder.fold_path(pth))
1003 PatStruct(pth, fields, etc) => {
1004 let pth = folder.fold_path(pth);
1005 let fs = fields.move_map(|f| {
1006 Spanned { span: folder.new_span(f.span),
1007 node: hir::FieldPat {
1008 ident: f.node.ident,
1009 pat: folder.fold_pat(f.node.pat),
1010 is_shorthand: f.node.is_shorthand,
1013 PatStruct(pth, fs, etc)
1015 PatTup(elts) => PatTup(elts.move_map(|x| folder.fold_pat(x))),
1016 PatBox(inner) => PatBox(folder.fold_pat(inner)),
1017 PatRegion(inner, mutbl) => PatRegion(folder.fold_pat(inner), mutbl),
1018 PatRange(e1, e2) => {
1019 PatRange(folder.fold_expr(e1), folder.fold_expr(e2))
1021 PatVec(before, slice, after) => {
1022 PatVec(before.move_map(|x| folder.fold_pat(x)),
1023 slice.map(|x| folder.fold_pat(x)),
1024 after.move_map(|x| folder.fold_pat(x)))
1027 span: folder.new_span(span)
1031 pub fn noop_fold_expr<T: Folder>(Expr {id, node, span}: Expr, folder: &mut T) -> Expr {
1033 id: folder.new_id(id),
1036 ExprBox(p.map(|e|folder.fold_expr(e)), folder.fold_expr(e))
1039 ExprVec(exprs.move_map(|x| folder.fold_expr(x)))
1041 ExprRepeat(expr, count) => {
1042 ExprRepeat(folder.fold_expr(expr), folder.fold_expr(count))
1044 ExprTup(elts) => ExprTup(elts.move_map(|x| folder.fold_expr(x))),
1045 ExprCall(f, args) => {
1046 ExprCall(folder.fold_expr(f),
1047 args.move_map(|x| folder.fold_expr(x)))
1049 ExprMethodCall(i, tps, args) => {
1051 respan(folder.new_span(i.span), folder.fold_ident(i.node)),
1052 tps.move_map(|x| folder.fold_ty(x)),
1053 args.move_map(|x| folder.fold_expr(x)))
1055 ExprBinary(binop, lhs, rhs) => {
1057 folder.fold_expr(lhs),
1058 folder.fold_expr(rhs))
1060 ExprUnary(binop, ohs) => {
1061 ExprUnary(binop, folder.fold_expr(ohs))
1063 ExprLit(l) => ExprLit(l),
1064 ExprCast(expr, ty) => {
1065 ExprCast(folder.fold_expr(expr), folder.fold_ty(ty))
1067 ExprAddrOf(m, ohs) => ExprAddrOf(m, folder.fold_expr(ohs)),
1068 ExprIf(cond, tr, fl) => {
1069 ExprIf(folder.fold_expr(cond),
1070 folder.fold_block(tr),
1071 fl.map(|x| folder.fold_expr(x)))
1073 ExprWhile(cond, body, opt_ident) => {
1074 ExprWhile(folder.fold_expr(cond),
1075 folder.fold_block(body),
1076 opt_ident.map(|i| folder.fold_ident(i)))
1078 ExprLoop(body, opt_ident) => {
1079 ExprLoop(folder.fold_block(body),
1080 opt_ident.map(|i| folder.fold_ident(i)))
1082 ExprMatch(expr, arms, source) => {
1083 ExprMatch(folder.fold_expr(expr),
1084 arms.move_map(|x| folder.fold_arm(x)),
1087 ExprClosure(capture_clause, decl, body) => {
1088 ExprClosure(capture_clause,
1089 folder.fold_fn_decl(decl),
1090 folder.fold_block(body))
1092 ExprBlock(blk) => ExprBlock(folder.fold_block(blk)),
1093 ExprAssign(el, er) => {
1094 ExprAssign(folder.fold_expr(el), folder.fold_expr(er))
1096 ExprAssignOp(op, el, er) => {
1098 folder.fold_expr(el),
1099 folder.fold_expr(er))
1101 ExprField(el, ident) => {
1102 ExprField(folder.fold_expr(el),
1103 respan(folder.new_span(ident.span),
1104 folder.fold_ident(ident.node)))
1106 ExprTupField(el, ident) => {
1107 ExprTupField(folder.fold_expr(el),
1108 respan(folder.new_span(ident.span),
1109 folder.fold_usize(ident.node)))
1111 ExprIndex(el, er) => {
1112 ExprIndex(folder.fold_expr(el), folder.fold_expr(er))
1114 ExprRange(e1, e2) => {
1115 ExprRange(e1.map(|x| folder.fold_expr(x)),
1116 e2.map(|x| folder.fold_expr(x)))
1118 ExprPath(qself, path) => {
1119 let qself = qself.map(|QSelf { ty, position }| {
1121 ty: folder.fold_ty(ty),
1125 ExprPath(qself, folder.fold_path(path))
1127 ExprBreak(opt_ident) => ExprBreak(opt_ident.map(|label|
1128 respan(folder.new_span(label.span),
1129 folder.fold_ident(label.node)))
1131 ExprAgain(opt_ident) => ExprAgain(opt_ident.map(|label|
1132 respan(folder.new_span(label.span),
1133 folder.fold_ident(label.node)))
1135 ExprRet(e) => ExprRet(e.map(|x| folder.fold_expr(x))),
1136 ExprInlineAsm(InlineAsm {
1146 }) => ExprInlineAsm(InlineAsm {
1147 inputs: inputs.move_map(|(c, input)| {
1148 (c, folder.fold_expr(input))
1150 outputs: outputs.move_map(|(c, out, is_rw)| {
1151 (c, folder.fold_expr(out), is_rw)
1154 asm_str_style: asm_str_style,
1157 alignstack: alignstack,
1161 ExprStruct(path, fields, maybe_expr) => {
1162 ExprStruct(folder.fold_path(path),
1163 fields.move_map(|x| folder.fold_field(x)),
1164 maybe_expr.map(|x| folder.fold_expr(x)))
1166 ExprParen(ex) => ExprParen(folder.fold_expr(ex))
1168 span: folder.new_span(span)
1172 pub fn noop_fold_stmt<T: Folder>(Spanned {node, span}: Stmt, folder: &mut T)
1173 -> SmallVector<P<Stmt>> {
1174 let span = folder.new_span(span);
1176 StmtDecl(d, id) => {
1177 let id = folder.new_id(id);
1178 folder.fold_decl(d).into_iter().map(|d| P(Spanned {
1179 node: StmtDecl(d, id),
1183 StmtExpr(e, id) => {
1184 let id = folder.new_id(id);
1185 SmallVector::one(P(Spanned {
1186 node: StmtExpr(folder.fold_expr(e), id),
1190 StmtSemi(e, id) => {
1191 let id = folder.new_id(id);
1192 SmallVector::one(P(Spanned {
1193 node: StmtSemi(folder.fold_expr(e), id),