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, Name, NodeId, DUMMY_NODE_ID, Attribute, Attribute_, MetaItem};
16 use syntax::ast::{MetaWord, MetaList, MetaNameValue};
18 use syntax::codemap::{respan, Span, Spanned};
19 use syntax::owned_slice::OwnedSlice;
21 use syntax::parse::token;
23 use syntax::util::small_vector::SmallVector;
26 // This could have a better place to live.
27 pub trait MoveMap<T> {
28 fn move_map<F>(self, f: F) -> Self where F: FnMut(T) -> T;
31 impl<T> MoveMap<T> for Vec<T> {
32 fn move_map<F>(mut self, mut f: F) -> Vec<T>
33 where F: FnMut(T) -> T
37 // FIXME(#5016) this shouldn't need to zero to be safe.
38 ptr::write(p, f(ptr::read_and_drop(p)));
45 impl<T> MoveMap<T> for OwnedSlice<T> {
46 fn move_map<F>(self, f: F) -> OwnedSlice<T>
47 where F: FnMut(T) -> T
49 OwnedSlice::from_vec(self.into_vec().move_map(f))
53 pub trait Folder : Sized {
54 // Any additions to this trait should happen in form
55 // of a call to a public `noop_*` function that only calls
56 // out to the folder again, not other `noop_*` functions.
58 // This is a necessary API workaround to the problem of not
59 // being able to call out to the super default method
60 // in an overridden default method.
62 fn fold_crate(&mut self, c: Crate) -> Crate {
63 noop_fold_crate(c, self)
66 fn fold_meta_items(&mut self, meta_items: Vec<P<MetaItem>>) -> Vec<P<MetaItem>> {
67 noop_fold_meta_items(meta_items, self)
70 fn fold_meta_item(&mut self, meta_item: P<MetaItem>) -> P<MetaItem> {
71 noop_fold_meta_item(meta_item, self)
74 fn fold_view_path(&mut self, view_path: P<ViewPath>) -> P<ViewPath> {
75 noop_fold_view_path(view_path, self)
78 fn fold_foreign_item(&mut self, ni: P<ForeignItem>) -> P<ForeignItem> {
79 noop_fold_foreign_item(ni, self)
82 fn fold_item(&mut self, i: P<Item>) -> SmallVector<P<Item>> {
83 noop_fold_item(i, self)
86 fn fold_item_simple(&mut self, i: Item) -> Item {
87 noop_fold_item_simple(i, self)
90 fn fold_struct_field(&mut self, sf: StructField) -> StructField {
91 noop_fold_struct_field(sf, self)
94 fn fold_item_underscore(&mut self, i: Item_) -> Item_ {
95 noop_fold_item_underscore(i, self)
98 fn fold_trait_item(&mut self, i: P<TraitItem>) -> SmallVector<P<TraitItem>> {
99 noop_fold_trait_item(i, self)
102 fn fold_impl_item(&mut self, i: P<ImplItem>) -> SmallVector<P<ImplItem>> {
103 noop_fold_impl_item(i, self)
106 fn fold_fn_decl(&mut self, d: P<FnDecl>) -> P<FnDecl> {
107 noop_fold_fn_decl(d, self)
110 fn fold_block(&mut self, b: P<Block>) -> P<Block> {
111 noop_fold_block(b, self)
114 fn fold_stmt(&mut self, s: P<Stmt>) -> SmallVector<P<Stmt>> {
115 s.and_then(|s| noop_fold_stmt(s, self))
118 fn fold_arm(&mut self, a: Arm) -> Arm {
119 noop_fold_arm(a, self)
122 fn fold_pat(&mut self, p: P<Pat>) -> P<Pat> {
123 noop_fold_pat(p, self)
126 fn fold_decl(&mut self, d: P<Decl>) -> SmallVector<P<Decl>> {
127 noop_fold_decl(d, self)
130 fn fold_expr(&mut self, e: P<Expr>) -> P<Expr> {
131 e.map(|e| noop_fold_expr(e, self))
134 fn fold_ty(&mut self, t: P<Ty>) -> P<Ty> {
135 noop_fold_ty(t, self)
138 fn fold_ty_binding(&mut self, t: P<TypeBinding>) -> P<TypeBinding> {
139 noop_fold_ty_binding(t, self)
142 fn fold_mod(&mut self, m: Mod) -> Mod {
143 noop_fold_mod(m, self)
146 fn fold_foreign_mod(&mut self, nm: ForeignMod) -> ForeignMod {
147 noop_fold_foreign_mod(nm, self)
150 fn fold_variant(&mut self, v: P<Variant>) -> P<Variant> {
151 noop_fold_variant(v, self)
154 fn fold_name(&mut self, n: Name) -> Name {
155 noop_fold_name(n, self)
158 fn fold_ident(&mut self, i: Ident) -> Ident {
159 noop_fold_ident(i, self)
162 fn fold_usize(&mut self, i: usize) -> usize {
163 noop_fold_usize(i, self)
166 fn fold_path(&mut self, p: Path) -> Path {
167 noop_fold_path(p, self)
170 fn fold_path_parameters(&mut self, p: PathParameters) -> PathParameters {
171 noop_fold_path_parameters(p, self)
174 fn fold_angle_bracketed_parameter_data(&mut self,
175 p: AngleBracketedParameterData)
176 -> AngleBracketedParameterData {
177 noop_fold_angle_bracketed_parameter_data(p, self)
180 fn fold_parenthesized_parameter_data(&mut self,
181 p: ParenthesizedParameterData)
182 -> ParenthesizedParameterData {
183 noop_fold_parenthesized_parameter_data(p, self)
186 fn fold_local(&mut self, l: P<Local>) -> P<Local> {
187 noop_fold_local(l, self)
190 fn fold_explicit_self(&mut self, es: ExplicitSelf) -> ExplicitSelf {
191 noop_fold_explicit_self(es, self)
194 fn fold_explicit_self_underscore(&mut self, es: ExplicitSelf_) -> ExplicitSelf_ {
195 noop_fold_explicit_self_underscore(es, self)
198 fn fold_lifetime(&mut self, l: Lifetime) -> Lifetime {
199 noop_fold_lifetime(l, self)
202 fn fold_lifetime_def(&mut self, l: LifetimeDef) -> LifetimeDef {
203 noop_fold_lifetime_def(l, self)
206 fn fold_attribute(&mut self, at: Attribute) -> Option<Attribute> {
207 noop_fold_attribute(at, self)
210 fn fold_arg(&mut self, a: Arg) -> Arg {
211 noop_fold_arg(a, self)
214 fn fold_generics(&mut self, generics: Generics) -> Generics {
215 noop_fold_generics(generics, self)
218 fn fold_trait_ref(&mut self, p: TraitRef) -> TraitRef {
219 noop_fold_trait_ref(p, self)
222 fn fold_poly_trait_ref(&mut self, p: PolyTraitRef) -> PolyTraitRef {
223 noop_fold_poly_trait_ref(p, self)
226 fn fold_variant_data(&mut self, vdata: VariantData) -> VariantData {
227 noop_fold_variant_data(vdata, self)
230 fn fold_lifetimes(&mut self, lts: Vec<Lifetime>) -> Vec<Lifetime> {
231 noop_fold_lifetimes(lts, self)
234 fn fold_lifetime_defs(&mut self, lts: Vec<LifetimeDef>) -> Vec<LifetimeDef> {
235 noop_fold_lifetime_defs(lts, self)
238 fn fold_ty_param(&mut self, tp: TyParam) -> TyParam {
239 noop_fold_ty_param(tp, self)
242 fn fold_ty_params(&mut self, tps: OwnedSlice<TyParam>) -> OwnedSlice<TyParam> {
243 noop_fold_ty_params(tps, self)
246 fn fold_opt_lifetime(&mut self, o_lt: Option<Lifetime>) -> Option<Lifetime> {
247 noop_fold_opt_lifetime(o_lt, self)
250 fn fold_opt_bounds(&mut self,
251 b: Option<OwnedSlice<TyParamBound>>)
252 -> Option<OwnedSlice<TyParamBound>> {
253 noop_fold_opt_bounds(b, self)
256 fn fold_bounds(&mut self, b: OwnedSlice<TyParamBound>) -> OwnedSlice<TyParamBound> {
257 noop_fold_bounds(b, self)
260 fn fold_ty_param_bound(&mut self, tpb: TyParamBound) -> TyParamBound {
261 noop_fold_ty_param_bound(tpb, self)
264 fn fold_mt(&mut self, mt: MutTy) -> MutTy {
265 noop_fold_mt(mt, self)
268 fn fold_field(&mut self, field: Field) -> Field {
269 noop_fold_field(field, self)
272 fn fold_where_clause(&mut self, where_clause: WhereClause) -> WhereClause {
273 noop_fold_where_clause(where_clause, self)
276 fn fold_where_predicate(&mut self, where_predicate: WherePredicate) -> WherePredicate {
277 noop_fold_where_predicate(where_predicate, self)
280 fn new_id(&mut self, i: NodeId) -> NodeId {
284 fn new_span(&mut self, sp: Span) -> Span {
289 pub fn noop_fold_meta_items<T: Folder>(meta_items: Vec<P<MetaItem>>,
291 -> Vec<P<MetaItem>> {
292 meta_items.move_map(|x| fld.fold_meta_item(x))
295 pub fn noop_fold_view_path<T: Folder>(view_path: P<ViewPath>, fld: &mut T) -> P<ViewPath> {
296 view_path.map(|Spanned { node, span }| {
299 ViewPathSimple(name, path) => {
300 ViewPathSimple(name, fld.fold_path(path))
302 ViewPathGlob(path) => {
303 ViewPathGlob(fld.fold_path(path))
305 ViewPathList(path, path_list_idents) => {
306 ViewPathList(fld.fold_path(path),
307 path_list_idents.move_map(|path_list_ident| {
309 node: match path_list_ident.node {
310 PathListIdent { id, name, rename } => PathListIdent {
315 PathListMod { id, rename } => PathListMod {
320 span: fld.new_span(path_list_ident.span),
325 span: fld.new_span(span),
330 pub fn fold_attrs<T: Folder>(attrs: Vec<Attribute>, fld: &mut T) -> Vec<Attribute> {
331 attrs.into_iter().flat_map(|x| fld.fold_attribute(x)).collect()
334 pub fn noop_fold_arm<T: Folder>(Arm { attrs, pats, guard, body }: Arm, fld: &mut T) -> Arm {
336 attrs: fold_attrs(attrs, fld),
337 pats: pats.move_map(|x| fld.fold_pat(x)),
338 guard: guard.map(|x| fld.fold_expr(x)),
339 body: fld.fold_expr(body),
343 pub fn noop_fold_decl<T: Folder>(d: P<Decl>, fld: &mut T) -> SmallVector<P<Decl>> {
344 d.and_then(|Spanned { node, span }| {
346 DeclLocal(l) => SmallVector::one(P(Spanned {
347 node: DeclLocal(fld.fold_local(l)),
348 span: fld.new_span(span),
350 DeclItem(it) => fld.fold_item(it)
355 span: fld.new_span(span),
363 pub fn noop_fold_ty_binding<T: Folder>(b: P<TypeBinding>, fld: &mut T) -> P<TypeBinding> {
364 b.map(|TypeBinding { id, name, ty, span }| {
369 span: fld.new_span(span),
374 pub fn noop_fold_ty<T: Folder>(t: P<Ty>, fld: &mut T) -> P<Ty> {
375 t.map(|Ty { id, node, span }| {
380 TyVec(ty) => TyVec(fld.fold_ty(ty)),
381 TyPtr(mt) => TyPtr(fld.fold_mt(mt)),
382 TyRptr(region, mt) => {
383 TyRptr(fld.fold_opt_lifetime(region), fld.fold_mt(mt))
386 TyBareFn(f.map(|BareFnTy { lifetimes, unsafety, abi, decl }| {
388 lifetimes: fld.fold_lifetime_defs(lifetimes),
391 decl: fld.fold_fn_decl(decl),
395 TyTup(tys) => TyTup(tys.move_map(|ty| fld.fold_ty(ty))),
396 TyParen(ty) => TyParen(fld.fold_ty(ty)),
397 TyPath(qself, path) => {
398 let qself = qself.map(|QSelf { ty, position }| {
404 TyPath(qself, fld.fold_path(path))
406 TyObjectSum(ty, bounds) => {
407 TyObjectSum(fld.fold_ty(ty), fld.fold_bounds(bounds))
409 TyFixedLengthVec(ty, e) => {
410 TyFixedLengthVec(fld.fold_ty(ty), fld.fold_expr(e))
413 TyTypeof(fld.fold_expr(expr))
415 TyPolyTraitRef(bounds) => {
416 TyPolyTraitRef(bounds.move_map(|b| fld.fold_ty_param_bound(b)))
419 span: fld.new_span(span),
424 pub fn noop_fold_foreign_mod<T: Folder>(ForeignMod { abi, items }: ForeignMod,
429 items: items.move_map(|x| fld.fold_foreign_item(x)),
433 pub fn noop_fold_variant<T: Folder>(v: P<Variant>, fld: &mut T) -> P<Variant> {
434 v.map(|Spanned {node: Variant_ {name, attrs, data, disr_expr}, span}| Spanned {
437 attrs: fold_attrs(attrs, fld),
438 data: fld.fold_variant_data(data),
439 disr_expr: disr_expr.map(|e| fld.fold_expr(e)),
441 span: fld.new_span(span),
445 pub fn noop_fold_name<T: Folder>(n: Name, _: &mut T) -> Name {
449 pub fn noop_fold_ident<T: Folder>(i: Ident, _: &mut T) -> Ident {
453 pub fn noop_fold_usize<T: Folder>(i: usize, _: &mut T) -> usize {
457 pub fn noop_fold_path<T: Folder>(Path { global, segments, span }: Path, fld: &mut T) -> Path {
460 segments: segments.move_map(|PathSegment { identifier, parameters }| {
462 identifier: fld.fold_ident(identifier),
463 parameters: fld.fold_path_parameters(parameters),
466 span: fld.new_span(span),
470 pub fn noop_fold_path_parameters<T: Folder>(path_parameters: PathParameters,
473 match path_parameters {
474 AngleBracketedParameters(data) =>
475 AngleBracketedParameters(fld.fold_angle_bracketed_parameter_data(data)),
476 ParenthesizedParameters(data) =>
477 ParenthesizedParameters(fld.fold_parenthesized_parameter_data(data)),
481 pub fn noop_fold_angle_bracketed_parameter_data<T: Folder>(data: AngleBracketedParameterData,
483 -> AngleBracketedParameterData {
484 let AngleBracketedParameterData { lifetimes, types, bindings } = data;
485 AngleBracketedParameterData {
486 lifetimes: fld.fold_lifetimes(lifetimes),
487 types: types.move_map(|ty| fld.fold_ty(ty)),
488 bindings: bindings.move_map(|b| fld.fold_ty_binding(b)),
492 pub fn noop_fold_parenthesized_parameter_data<T: Folder>(data: ParenthesizedParameterData,
494 -> ParenthesizedParameterData {
495 let ParenthesizedParameterData { inputs, output, span } = data;
496 ParenthesizedParameterData {
497 inputs: inputs.move_map(|ty| fld.fold_ty(ty)),
498 output: output.map(|ty| fld.fold_ty(ty)),
499 span: fld.new_span(span),
503 pub fn noop_fold_local<T: Folder>(l: P<Local>, fld: &mut T) -> P<Local> {
504 l.map(|Local { id, pat, ty, init, span }| {
507 ty: ty.map(|t| fld.fold_ty(t)),
508 pat: fld.fold_pat(pat),
509 init: init.map(|e| fld.fold_expr(e)),
510 span: fld.new_span(span),
515 pub fn noop_fold_attribute<T: Folder>(at: Attribute, fld: &mut T) -> Option<Attribute> {
516 let Spanned {node: Attribute_ {id, style, value, is_sugared_doc}, span} = at;
521 value: fld.fold_meta_item(value),
522 is_sugared_doc: is_sugared_doc,
524 span: fld.new_span(span),
528 pub fn noop_fold_explicit_self_underscore<T: Folder>(es: ExplicitSelf_,
532 SelfStatic | SelfValue(_) => es,
533 SelfRegion(lifetime, m, name) => {
534 SelfRegion(fld.fold_opt_lifetime(lifetime), m, name)
536 SelfExplicit(typ, name) => {
537 SelfExplicit(fld.fold_ty(typ), name)
542 pub fn noop_fold_explicit_self<T: Folder>(Spanned { span, node }: ExplicitSelf,
546 node: fld.fold_explicit_self_underscore(node),
547 span: fld.new_span(span),
551 pub fn noop_fold_meta_item<T: Folder>(mi: P<MetaItem>, fld: &mut T) -> P<MetaItem> {
552 mi.map(|Spanned { node, span }| {
555 MetaWord(id) => MetaWord(id),
556 MetaList(id, mis) => {
557 MetaList(id, mis.move_map(|e| fld.fold_meta_item(e)))
559 MetaNameValue(id, s) => MetaNameValue(id, s),
561 span: fld.new_span(span),
566 pub fn noop_fold_arg<T: Folder>(Arg { id, pat, ty }: Arg, fld: &mut T) -> Arg {
569 pat: fld.fold_pat(pat),
574 pub fn noop_fold_fn_decl<T: Folder>(decl: P<FnDecl>, fld: &mut T) -> P<FnDecl> {
575 decl.map(|FnDecl { inputs, output, variadic }| {
577 inputs: inputs.move_map(|x| fld.fold_arg(x)),
578 output: match output {
579 Return(ty) => Return(fld.fold_ty(ty)),
580 DefaultReturn(span) => DefaultReturn(span),
581 NoReturn(span) => NoReturn(span),
588 pub fn noop_fold_ty_param_bound<T>(tpb: TyParamBound, fld: &mut T) -> TyParamBound
592 TraitTyParamBound(ty, modifier) => TraitTyParamBound(fld.fold_poly_trait_ref(ty), modifier),
593 RegionTyParamBound(lifetime) => RegionTyParamBound(fld.fold_lifetime(lifetime)),
597 pub fn noop_fold_ty_param<T: Folder>(tp: TyParam, fld: &mut T) -> TyParam {
598 let TyParam {id, name, bounds, default, span} = tp;
602 bounds: fld.fold_bounds(bounds),
603 default: default.map(|x| fld.fold_ty(x)),
608 pub fn noop_fold_ty_params<T: Folder>(tps: OwnedSlice<TyParam>,
610 -> OwnedSlice<TyParam> {
611 tps.move_map(|tp| fld.fold_ty_param(tp))
614 pub fn noop_fold_lifetime<T: Folder>(l: Lifetime, fld: &mut T) -> Lifetime {
616 id: fld.new_id(l.id),
618 span: fld.new_span(l.span),
622 pub fn noop_fold_lifetime_def<T: Folder>(l: LifetimeDef, fld: &mut T) -> LifetimeDef {
624 lifetime: fld.fold_lifetime(l.lifetime),
625 bounds: fld.fold_lifetimes(l.bounds),
629 pub fn noop_fold_lifetimes<T: Folder>(lts: Vec<Lifetime>, fld: &mut T) -> Vec<Lifetime> {
630 lts.move_map(|l| fld.fold_lifetime(l))
633 pub fn noop_fold_lifetime_defs<T: Folder>(lts: Vec<LifetimeDef>, fld: &mut T) -> Vec<LifetimeDef> {
634 lts.move_map(|l| fld.fold_lifetime_def(l))
637 pub fn noop_fold_opt_lifetime<T: Folder>(o_lt: Option<Lifetime>, fld: &mut T) -> Option<Lifetime> {
638 o_lt.map(|lt| fld.fold_lifetime(lt))
641 pub fn noop_fold_generics<T: Folder>(Generics { ty_params, lifetimes, where_clause }: Generics,
645 ty_params: fld.fold_ty_params(ty_params),
646 lifetimes: fld.fold_lifetime_defs(lifetimes),
647 where_clause: fld.fold_where_clause(where_clause),
651 pub fn noop_fold_where_clause<T: Folder>(WhereClause { id, predicates }: WhereClause,
656 predicates: predicates.move_map(|predicate| fld.fold_where_predicate(predicate)),
660 pub fn noop_fold_where_predicate<T: Folder>(pred: WherePredicate, fld: &mut T) -> WherePredicate {
662 hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate{bound_lifetimes,
666 hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
667 bound_lifetimes: fld.fold_lifetime_defs(bound_lifetimes),
668 bounded_ty: fld.fold_ty(bounded_ty),
669 bounds: bounds.move_map(|x| fld.fold_ty_param_bound(x)),
670 span: fld.new_span(span),
673 hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate{lifetime,
676 hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
677 span: fld.new_span(span),
678 lifetime: fld.fold_lifetime(lifetime),
679 bounds: bounds.move_map(|bound| fld.fold_lifetime(bound)),
682 hir::WherePredicate::EqPredicate(hir::WhereEqPredicate{id,
686 hir::WherePredicate::EqPredicate(hir::WhereEqPredicate {
688 path: fld.fold_path(path),
690 span: fld.new_span(span),
696 pub fn noop_fold_variant_data<T: Folder>(vdata: VariantData, fld: &mut T) -> VariantData {
698 VariantData::Struct(fields, id) => {
699 VariantData::Struct(fields.move_map(|f| fld.fold_struct_field(f)), fld.new_id(id))
701 VariantData::Tuple(fields, id) => {
702 VariantData::Tuple(fields.move_map(|f| fld.fold_struct_field(f)), fld.new_id(id))
704 VariantData::Unit(id) => VariantData::Unit(fld.new_id(id))
708 pub fn noop_fold_trait_ref<T: Folder>(p: TraitRef, fld: &mut T) -> TraitRef {
709 let id = fld.new_id(p.ref_id);
715 path: fld.fold_path(path),
720 pub fn noop_fold_poly_trait_ref<T: Folder>(p: PolyTraitRef, fld: &mut T) -> PolyTraitRef {
722 bound_lifetimes: fld.fold_lifetime_defs(p.bound_lifetimes),
723 trait_ref: fld.fold_trait_ref(p.trait_ref),
724 span: fld.new_span(p.span),
728 pub fn noop_fold_struct_field<T: Folder>(f: StructField, fld: &mut T) -> StructField {
729 let StructField {node: StructField_ {id, kind, ty, attrs}, span} = f;
735 attrs: fold_attrs(attrs, fld),
737 span: fld.new_span(span),
741 pub fn noop_fold_field<T: Folder>(Field { name, expr, span }: Field, folder: &mut T) -> Field {
743 name: respan(folder.new_span(name.span),
744 folder.fold_name(name.node)),
745 expr: folder.fold_expr(expr),
746 span: folder.new_span(span),
750 pub fn noop_fold_mt<T: Folder>(MutTy { ty, mutbl }: MutTy, folder: &mut T) -> MutTy {
752 ty: folder.fold_ty(ty),
757 pub fn noop_fold_opt_bounds<T: Folder>(b: Option<OwnedSlice<TyParamBound>>,
759 -> Option<OwnedSlice<TyParamBound>> {
760 b.map(|bounds| folder.fold_bounds(bounds))
763 fn noop_fold_bounds<T: Folder>(bounds: TyParamBounds, folder: &mut T) -> TyParamBounds {
764 bounds.move_map(|bound| folder.fold_ty_param_bound(bound))
767 pub fn noop_fold_block<T: Folder>(b: P<Block>, folder: &mut T) -> P<Block> {
768 b.map(|Block { id, stmts, expr, rules, span }| {
770 id: folder.new_id(id),
771 stmts: stmts.into_iter().flat_map(|s| folder.fold_stmt(s).into_iter()).collect(),
772 expr: expr.map(|x| folder.fold_expr(x)),
774 span: folder.new_span(span),
779 pub fn noop_fold_item_underscore<T: Folder>(i: Item_, folder: &mut T) -> Item_ {
781 ItemExternCrate(string) => ItemExternCrate(string),
782 ItemUse(view_path) => {
783 ItemUse(folder.fold_view_path(view_path))
785 ItemStatic(t, m, e) => {
786 ItemStatic(folder.fold_ty(t), m, folder.fold_expr(e))
789 ItemConst(folder.fold_ty(t), folder.fold_expr(e))
791 ItemFn(decl, unsafety, constness, abi, generics, body) => {
792 ItemFn(folder.fold_fn_decl(decl),
796 folder.fold_generics(generics),
797 folder.fold_block(body))
799 ItemMod(m) => ItemMod(folder.fold_mod(m)),
800 ItemForeignMod(nm) => ItemForeignMod(folder.fold_foreign_mod(nm)),
801 ItemTy(t, generics) => {
802 ItemTy(folder.fold_ty(t), folder.fold_generics(generics))
804 ItemEnum(enum_definition, generics) => {
805 ItemEnum(hir::EnumDef {
806 variants: enum_definition.variants.move_map(|x| folder.fold_variant(x)),
808 folder.fold_generics(generics))
810 ItemStruct(struct_def, generics) => {
811 let struct_def = folder.fold_variant_data(struct_def);
812 ItemStruct(struct_def, folder.fold_generics(generics))
814 ItemDefaultImpl(unsafety, ref trait_ref) => {
815 ItemDefaultImpl(unsafety,
816 folder.fold_trait_ref((*trait_ref).clone()))
818 ItemImpl(unsafety, polarity, generics, ifce, ty, impl_items) => {
819 let new_impl_items = impl_items.into_iter()
821 folder.fold_impl_item(item).into_iter()
824 let ifce = match ifce {
826 Some(ref trait_ref) => {
827 Some(folder.fold_trait_ref((*trait_ref).clone()))
832 folder.fold_generics(generics),
837 ItemTrait(unsafety, generics, bounds, items) => {
838 let bounds = folder.fold_bounds(bounds);
839 let items = items.into_iter()
840 .flat_map(|item| folder.fold_trait_item(item).into_iter())
842 ItemTrait(unsafety, folder.fold_generics(generics), bounds, items)
847 pub fn noop_fold_trait_item<T: Folder>(i: P<TraitItem>,
849 -> SmallVector<P<TraitItem>> {
850 SmallVector::one(i.map(|TraitItem { id, name, attrs, node, span }| {
852 id: folder.new_id(id),
853 name: folder.fold_name(name),
854 attrs: fold_attrs(attrs, folder),
856 ConstTraitItem(ty, default) => {
857 ConstTraitItem(folder.fold_ty(ty),
858 default.map(|x| folder.fold_expr(x)))
860 MethodTraitItem(sig, body) => {
861 MethodTraitItem(noop_fold_method_sig(sig, folder),
862 body.map(|x| folder.fold_block(x)))
864 TypeTraitItem(bounds, default) => {
865 TypeTraitItem(folder.fold_bounds(bounds),
866 default.map(|x| folder.fold_ty(x)))
869 span: folder.new_span(span),
874 pub fn noop_fold_impl_item<T: Folder>(i: P<ImplItem>, folder: &mut T) -> SmallVector<P<ImplItem>> {
875 SmallVector::one(i.map(|ImplItem { id, name, attrs, node, vis, span }| {
877 id: folder.new_id(id),
878 name: folder.fold_name(name),
879 attrs: fold_attrs(attrs, folder),
882 ConstImplItem(ty, expr) => {
883 ConstImplItem(folder.fold_ty(ty), folder.fold_expr(expr))
885 MethodImplItem(sig, body) => {
886 MethodImplItem(noop_fold_method_sig(sig, folder),
887 folder.fold_block(body))
889 TypeImplItem(ty) => TypeImplItem(folder.fold_ty(ty)),
891 span: folder.new_span(span),
896 pub fn noop_fold_mod<T: Folder>(Mod { inner, items }: Mod, folder: &mut T) -> Mod {
898 inner: folder.new_span(inner),
899 items: items.into_iter().flat_map(|x| folder.fold_item(x).into_iter()).collect(),
903 pub fn noop_fold_crate<T: Folder>(Crate { module, attrs, config, span, exported_macros }: Crate,
906 let config = folder.fold_meta_items(config);
908 let mut items = folder.fold_item(P(hir::Item {
909 name: token::special_idents::invalid.name,
914 node: hir::ItemMod(module),
918 let (module, attrs, span) = match items.next() {
920 assert!(items.next().is_none(),
921 "a crate cannot expand to more than one item");
922 item.and_then(|hir::Item { attrs, span, node, .. }| {
924 hir::ItemMod(m) => (m, attrs, span),
925 _ => panic!("fold converted a module to not a module"),
942 exported_macros: exported_macros,
946 // fold one item into possibly many items
947 pub fn noop_fold_item<T: Folder>(i: P<Item>, folder: &mut T) -> SmallVector<P<Item>> {
948 SmallVector::one(i.map(|i| folder.fold_item_simple(i)))
951 // fold one item into exactly one item
952 pub fn noop_fold_item_simple<T: Folder>(Item { id, name, attrs, node, vis, span }: Item,
955 let id = folder.new_id(id);
956 let node = folder.fold_item_underscore(node);
957 // FIXME: we should update the impl_pretty_name, but it uses pretty printing.
958 // let ident = match node {
959 // // The node may have changed, recompute the "pretty" impl name.
960 // ItemImpl(_, _, _, ref maybe_trait, ref ty, _) => {
961 // impl_pretty_name(maybe_trait, Some(&**ty))
968 name: folder.fold_name(name),
969 attrs: fold_attrs(attrs, folder),
972 span: folder.new_span(span),
976 pub fn noop_fold_foreign_item<T: Folder>(ni: P<ForeignItem>, folder: &mut T) -> P<ForeignItem> {
977 ni.map(|ForeignItem { id, name, attrs, node, span, vis }| {
979 id: folder.new_id(id),
980 name: folder.fold_name(name),
981 attrs: fold_attrs(attrs, folder),
983 ForeignItemFn(fdec, generics) => {
984 ForeignItemFn(folder.fold_fn_decl(fdec),
985 folder.fold_generics(generics))
987 ForeignItemStatic(t, m) => {
988 ForeignItemStatic(folder.fold_ty(t), m)
992 span: folder.new_span(span),
997 pub fn noop_fold_method_sig<T: Folder>(sig: MethodSig, folder: &mut T) -> MethodSig {
999 generics: folder.fold_generics(sig.generics),
1001 explicit_self: folder.fold_explicit_self(sig.explicit_self),
1002 unsafety: sig.unsafety,
1003 constness: sig.constness,
1004 decl: folder.fold_fn_decl(sig.decl),
1008 pub fn noop_fold_pat<T: Folder>(p: P<Pat>, folder: &mut T) -> P<Pat> {
1009 p.map(|Pat { id, node, span }| {
1011 id: folder.new_id(id),
1014 PatIdent(binding_mode, pth1, sub) => {
1015 PatIdent(binding_mode,
1017 span: folder.new_span(pth1.span),
1018 node: folder.fold_ident(pth1.node),
1020 sub.map(|x| folder.fold_pat(x)))
1022 PatLit(e) => PatLit(folder.fold_expr(e)),
1023 PatEnum(pth, pats) => {
1024 PatEnum(folder.fold_path(pth),
1025 pats.map(|pats| pats.move_map(|x| folder.fold_pat(x))))
1027 PatQPath(qself, pth) => {
1028 let qself = QSelf { ty: folder.fold_ty(qself.ty), ..qself };
1029 PatQPath(qself, folder.fold_path(pth))
1031 PatStruct(pth, fields, etc) => {
1032 let pth = folder.fold_path(pth);
1033 let fs = fields.move_map(|f| {
1035 span: folder.new_span(f.span),
1036 node: hir::FieldPat {
1038 pat: folder.fold_pat(f.node.pat),
1039 is_shorthand: f.node.is_shorthand,
1043 PatStruct(pth, fs, etc)
1045 PatTup(elts) => PatTup(elts.move_map(|x| folder.fold_pat(x))),
1046 PatBox(inner) => PatBox(folder.fold_pat(inner)),
1047 PatRegion(inner, mutbl) => PatRegion(folder.fold_pat(inner), mutbl),
1048 PatRange(e1, e2) => {
1049 PatRange(folder.fold_expr(e1), folder.fold_expr(e2))
1051 PatVec(before, slice, after) => {
1052 PatVec(before.move_map(|x| folder.fold_pat(x)),
1053 slice.map(|x| folder.fold_pat(x)),
1054 after.move_map(|x| folder.fold_pat(x)))
1057 span: folder.new_span(span),
1062 pub fn noop_fold_expr<T: Folder>(Expr { id, node, span }: Expr, folder: &mut T) -> Expr {
1064 id: folder.new_id(id),
1067 ExprBox(folder.fold_expr(e))
1070 ExprVec(exprs.move_map(|x| folder.fold_expr(x)))
1072 ExprRepeat(expr, count) => {
1073 ExprRepeat(folder.fold_expr(expr), folder.fold_expr(count))
1075 ExprTup(elts) => ExprTup(elts.move_map(|x| folder.fold_expr(x))),
1076 ExprCall(f, args) => {
1077 ExprCall(folder.fold_expr(f),
1078 args.move_map(|x| folder.fold_expr(x)))
1080 ExprMethodCall(name, tps, args) => {
1081 ExprMethodCall(respan(folder.new_span(name.span),
1082 folder.fold_name(name.node)),
1083 tps.move_map(|x| folder.fold_ty(x)),
1084 args.move_map(|x| folder.fold_expr(x)))
1086 ExprBinary(binop, lhs, rhs) => {
1087 ExprBinary(binop, folder.fold_expr(lhs), folder.fold_expr(rhs))
1089 ExprUnary(binop, ohs) => {
1090 ExprUnary(binop, folder.fold_expr(ohs))
1092 ExprLit(l) => ExprLit(l),
1093 ExprCast(expr, ty) => {
1094 ExprCast(folder.fold_expr(expr), folder.fold_ty(ty))
1096 ExprAddrOf(m, ohs) => ExprAddrOf(m, folder.fold_expr(ohs)),
1097 ExprIf(cond, tr, fl) => {
1098 ExprIf(folder.fold_expr(cond),
1099 folder.fold_block(tr),
1100 fl.map(|x| folder.fold_expr(x)))
1102 ExprWhile(cond, body, opt_ident) => {
1103 ExprWhile(folder.fold_expr(cond),
1104 folder.fold_block(body),
1105 opt_ident.map(|i| folder.fold_ident(i)))
1107 ExprLoop(body, opt_ident) => {
1108 ExprLoop(folder.fold_block(body),
1109 opt_ident.map(|i| folder.fold_ident(i)))
1111 ExprMatch(expr, arms, source) => {
1112 ExprMatch(folder.fold_expr(expr),
1113 arms.move_map(|x| folder.fold_arm(x)),
1116 ExprClosure(capture_clause, decl, body) => {
1117 ExprClosure(capture_clause,
1118 folder.fold_fn_decl(decl),
1119 folder.fold_block(body))
1121 ExprBlock(blk) => ExprBlock(folder.fold_block(blk)),
1122 ExprAssign(el, er) => {
1123 ExprAssign(folder.fold_expr(el), folder.fold_expr(er))
1125 ExprAssignOp(op, el, er) => {
1126 ExprAssignOp(op, folder.fold_expr(el), folder.fold_expr(er))
1128 ExprField(el, name) => {
1129 ExprField(folder.fold_expr(el),
1130 respan(folder.new_span(name.span),
1131 folder.fold_name(name.node)))
1133 ExprTupField(el, index) => {
1134 ExprTupField(folder.fold_expr(el),
1135 respan(folder.new_span(index.span),
1136 folder.fold_usize(index.node)))
1138 ExprIndex(el, er) => {
1139 ExprIndex(folder.fold_expr(el), folder.fold_expr(er))
1141 ExprRange(e1, e2) => {
1142 ExprRange(e1.map(|x| folder.fold_expr(x)),
1143 e2.map(|x| folder.fold_expr(x)))
1145 ExprPath(qself, path) => {
1146 let qself = qself.map(|QSelf { ty, position }| {
1148 ty: folder.fold_ty(ty),
1152 ExprPath(qself, folder.fold_path(path))
1154 ExprBreak(opt_ident) => ExprBreak(opt_ident.map(|label| {
1155 respan(folder.new_span(label.span),
1156 folder.fold_ident(label.node))
1158 ExprAgain(opt_ident) => ExprAgain(opt_ident.map(|label| {
1159 respan(folder.new_span(label.span),
1160 folder.fold_ident(label.node))
1162 ExprRet(e) => ExprRet(e.map(|x| folder.fold_expr(x))),
1163 ExprInlineAsm(InlineAsm {
1173 }) => ExprInlineAsm(InlineAsm {
1174 inputs: inputs.move_map(|(c, input)| (c, folder.fold_expr(input))),
1175 outputs: outputs.move_map(|(c, out, is_rw)| (c, folder.fold_expr(out), is_rw)),
1177 asm_str_style: asm_str_style,
1180 alignstack: alignstack,
1184 ExprStruct(path, fields, maybe_expr) => {
1185 ExprStruct(folder.fold_path(path),
1186 fields.move_map(|x| folder.fold_field(x)),
1187 maybe_expr.map(|x| folder.fold_expr(x)))
1190 span: folder.new_span(span),
1194 pub fn noop_fold_stmt<T: Folder>(Spanned { node, span }: Stmt,
1196 -> SmallVector<P<Stmt>> {
1197 let span = folder.new_span(span);
1199 StmtDecl(d, id) => {
1200 let id = folder.new_id(id);
1205 node: StmtDecl(d, id),
1211 StmtExpr(e, id) => {
1212 let id = folder.new_id(id);
1213 SmallVector::one(P(Spanned {
1214 node: StmtExpr(folder.fold_expr(e), id),
1218 StmtSemi(e, id) => {
1219 let id = folder.new_id(id);
1220 SmallVector::one(P(Spanned {
1221 node: StmtSemi(folder.fold_expr(e), id),