]> git.lizzy.rs Git - rust.git/blob - src/librustc_front/lowering.rs
Rollup merge of #28836 - jackwilsonv:patch-6, r=steveklabnik
[rust.git] / src / librustc_front / lowering.rs
1 // Copyright 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.
4 //
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.
10
11 // Lowers the AST to the HIR
12
13 use hir;
14
15 use syntax::ast::*;
16 use syntax::ptr::P;
17 use syntax::codemap::{respan, Spanned};
18 use syntax::owned_slice::OwnedSlice;
19
20
21 pub fn lower_view_path(view_path: &ViewPath) -> P<hir::ViewPath> {
22     P(Spanned {
23         node: match view_path.node {
24             ViewPathSimple(ident, ref path) => {
25                 hir::ViewPathSimple(ident.name, lower_path(path))
26             }
27             ViewPathGlob(ref path) => {
28                 hir::ViewPathGlob(lower_path(path))
29             }
30             ViewPathList(ref path, ref path_list_idents) => {
31                 hir::ViewPathList(lower_path(path),
32                                   path_list_idents.iter()
33                                                   .map(|path_list_ident| {
34                                                       Spanned {
35                                                           node: match path_list_ident.node {
36                                                               PathListIdent { id, name, rename } =>
37                                                                   hir::PathListIdent {
38                                                                   id: id,
39                                                                   name: name.name,
40                                                                   rename: rename.map(|x| x.name),
41                                                               },
42                                                               PathListMod { id, rename } =>
43                                                                   hir::PathListMod {
44                                                                   id: id,
45                                                                   rename: rename.map(|x| x.name),
46                                                               },
47                                                           },
48                                                           span: path_list_ident.span,
49                                                       }
50                                                   })
51                                                   .collect())
52             }
53         },
54         span: view_path.span,
55     })
56 }
57
58 pub fn lower_arm(arm: &Arm) -> hir::Arm {
59     hir::Arm {
60         attrs: arm.attrs.clone(),
61         pats: arm.pats.iter().map(|x| lower_pat(x)).collect(),
62         guard: arm.guard.as_ref().map(|ref x| lower_expr(x)),
63         body: lower_expr(&arm.body),
64     }
65 }
66
67 pub fn lower_decl(d: &Decl) -> P<hir::Decl> {
68     match d.node {
69         DeclLocal(ref l) => P(Spanned {
70             node: hir::DeclLocal(lower_local(l)),
71             span: d.span,
72         }),
73         DeclItem(ref it) => P(Spanned {
74             node: hir::DeclItem(lower_item(it)),
75             span: d.span,
76         }),
77     }
78 }
79
80 pub fn lower_ty_binding(b: &TypeBinding) -> P<hir::TypeBinding> {
81     P(hir::TypeBinding {
82         id: b.id,
83         name: b.ident.name,
84         ty: lower_ty(&b.ty),
85         span: b.span,
86     })
87 }
88
89 pub fn lower_ty(t: &Ty) -> P<hir::Ty> {
90     P(hir::Ty {
91         id: t.id,
92         node: match t.node {
93             TyInfer => hir::TyInfer,
94             TyVec(ref ty) => hir::TyVec(lower_ty(ty)),
95             TyPtr(ref mt) => hir::TyPtr(lower_mt(mt)),
96             TyRptr(ref region, ref mt) => {
97                 hir::TyRptr(lower_opt_lifetime(region), lower_mt(mt))
98             }
99             TyBareFn(ref f) => {
100                 hir::TyBareFn(P(hir::BareFnTy {
101                     lifetimes: lower_lifetime_defs(&f.lifetimes),
102                     unsafety: lower_unsafety(f.unsafety),
103                     abi: f.abi,
104                     decl: lower_fn_decl(&f.decl),
105                 }))
106             }
107             TyTup(ref tys) => hir::TyTup(tys.iter().map(|ty| lower_ty(ty)).collect()),
108             TyParen(ref ty) => hir::TyParen(lower_ty(ty)),
109             TyPath(ref qself, ref path) => {
110                 let qself = qself.as_ref().map(|&QSelf { ref ty, position }| {
111                     hir::QSelf {
112                         ty: lower_ty(ty),
113                         position: position,
114                     }
115                 });
116                 hir::TyPath(qself, lower_path(path))
117             }
118             TyObjectSum(ref ty, ref bounds) => {
119                 hir::TyObjectSum(lower_ty(ty), lower_bounds(bounds))
120             }
121             TyFixedLengthVec(ref ty, ref e) => {
122                 hir::TyFixedLengthVec(lower_ty(ty), lower_expr(e))
123             }
124             TyTypeof(ref expr) => {
125                 hir::TyTypeof(lower_expr(expr))
126             }
127             TyPolyTraitRef(ref bounds) => {
128                 hir::TyPolyTraitRef(bounds.iter().map(|b| lower_ty_param_bound(b)).collect())
129             }
130             TyMac(_) => panic!("TyMac should have been expanded by now."),
131         },
132         span: t.span,
133     })
134 }
135
136 pub fn lower_foreign_mod(fm: &ForeignMod) -> hir::ForeignMod {
137     hir::ForeignMod {
138         abi: fm.abi,
139         items: fm.items.iter().map(|x| lower_foreign_item(x)).collect(),
140     }
141 }
142
143 pub fn lower_variant(v: &Variant) -> P<hir::Variant> {
144     P(Spanned {
145         node: hir::Variant_ {
146             id: v.node.id,
147             name: v.node.name.name,
148             attrs: v.node.attrs.clone(),
149             kind: match v.node.kind {
150                 TupleVariantKind(ref variant_args) => {
151                     hir::TupleVariantKind(variant_args.iter()
152                                                       .map(|ref x| lower_variant_arg(x))
153                                                       .collect())
154                 }
155                 StructVariantKind(ref struct_def) => {
156                     hir::StructVariantKind(lower_struct_def(struct_def))
157                 }
158             },
159             disr_expr: v.node.disr_expr.as_ref().map(|e| lower_expr(e)),
160         },
161         span: v.span,
162     })
163 }
164
165 pub fn lower_path(p: &Path) -> hir::Path {
166     hir::Path {
167         global: p.global,
168         segments: p.segments
169                    .iter()
170                    .map(|&PathSegment { identifier, ref parameters }| {
171                        hir::PathSegment {
172                            identifier: identifier,
173                            parameters: lower_path_parameters(parameters),
174                        }
175                    })
176                    .collect(),
177         span: p.span,
178     }
179 }
180
181 pub fn lower_path_parameters(path_parameters: &PathParameters) -> hir::PathParameters {
182     match *path_parameters {
183         AngleBracketedParameters(ref data) =>
184             hir::AngleBracketedParameters(lower_angle_bracketed_parameter_data(data)),
185         ParenthesizedParameters(ref data) =>
186             hir::ParenthesizedParameters(lower_parenthesized_parameter_data(data)),
187     }
188 }
189
190 pub fn lower_angle_bracketed_parameter_data(data: &AngleBracketedParameterData)
191                                             -> hir::AngleBracketedParameterData {
192     let &AngleBracketedParameterData { ref lifetimes, ref types, ref bindings } = data;
193     hir::AngleBracketedParameterData {
194         lifetimes: lower_lifetimes(lifetimes),
195         types: types.iter().map(|ty| lower_ty(ty)).collect(),
196         bindings: bindings.iter().map(|b| lower_ty_binding(b)).collect(),
197     }
198 }
199
200 pub fn lower_parenthesized_parameter_data(data: &ParenthesizedParameterData)
201                                           -> hir::ParenthesizedParameterData {
202     let &ParenthesizedParameterData { ref inputs, ref output, span } = data;
203     hir::ParenthesizedParameterData {
204         inputs: inputs.iter().map(|ty| lower_ty(ty)).collect(),
205         output: output.as_ref().map(|ty| lower_ty(ty)),
206         span: span,
207     }
208 }
209
210 pub fn lower_local(l: &Local) -> P<hir::Local> {
211     P(hir::Local {
212         id: l.id,
213         ty: l.ty.as_ref().map(|t| lower_ty(t)),
214         pat: lower_pat(&l.pat),
215         init: l.init.as_ref().map(|e| lower_expr(e)),
216         span: l.span,
217     })
218 }
219
220 pub fn lower_explicit_self_underscore(es: &ExplicitSelf_) -> hir::ExplicitSelf_ {
221     match *es {
222         SelfStatic => hir::SelfStatic,
223         SelfValue(v) => hir::SelfValue(v.name),
224         SelfRegion(ref lifetime, m, ident) => {
225             hir::SelfRegion(lower_opt_lifetime(lifetime),
226                             lower_mutability(m),
227                             ident.name)
228         }
229         SelfExplicit(ref typ, ident) => {
230             hir::SelfExplicit(lower_ty(typ), ident.name)
231         }
232     }
233 }
234
235 pub fn lower_mutability(m: Mutability) -> hir::Mutability {
236     match m {
237         MutMutable => hir::MutMutable,
238         MutImmutable => hir::MutImmutable,
239     }
240 }
241
242 pub fn lower_explicit_self(s: &ExplicitSelf) -> hir::ExplicitSelf {
243     Spanned {
244         node: lower_explicit_self_underscore(&s.node),
245         span: s.span,
246     }
247 }
248
249 pub fn lower_arg(arg: &Arg) -> hir::Arg {
250     hir::Arg {
251         id: arg.id,
252         pat: lower_pat(&arg.pat),
253         ty: lower_ty(&arg.ty),
254     }
255 }
256
257 pub fn lower_fn_decl(decl: &FnDecl) -> P<hir::FnDecl> {
258     P(hir::FnDecl {
259         inputs: decl.inputs.iter().map(|x| lower_arg(x)).collect(),
260         output: match decl.output {
261             Return(ref ty) => hir::Return(lower_ty(ty)),
262             DefaultReturn(span) => hir::DefaultReturn(span),
263             NoReturn(span) => hir::NoReturn(span),
264         },
265         variadic: decl.variadic,
266     })
267 }
268
269 pub fn lower_ty_param_bound(tpb: &TyParamBound) -> hir::TyParamBound {
270     match *tpb {
271         TraitTyParamBound(ref ty, modifier) => {
272             hir::TraitTyParamBound(lower_poly_trait_ref(ty),
273                                    lower_trait_bound_modifier(modifier))
274         }
275         RegionTyParamBound(ref lifetime) => hir::RegionTyParamBound(lower_lifetime(lifetime)),
276     }
277 }
278
279 pub fn lower_ty_param(tp: &TyParam) -> hir::TyParam {
280     hir::TyParam {
281         id: tp.id,
282         name: tp.ident.name,
283         bounds: lower_bounds(&tp.bounds),
284         default: tp.default.as_ref().map(|x| lower_ty(x)),
285         span: tp.span,
286     }
287 }
288
289 pub fn lower_ty_params(tps: &OwnedSlice<TyParam>) -> OwnedSlice<hir::TyParam> {
290     tps.iter().map(|tp| lower_ty_param(tp)).collect()
291 }
292
293 pub fn lower_lifetime(l: &Lifetime) -> hir::Lifetime {
294     hir::Lifetime {
295         id: l.id,
296         name: l.name,
297         span: l.span,
298     }
299 }
300
301 pub fn lower_lifetime_def(l: &LifetimeDef) -> hir::LifetimeDef {
302     hir::LifetimeDef {
303         lifetime: lower_lifetime(&l.lifetime),
304         bounds: lower_lifetimes(&l.bounds),
305     }
306 }
307
308 pub fn lower_lifetimes(lts: &Vec<Lifetime>) -> Vec<hir::Lifetime> {
309     lts.iter().map(|l| lower_lifetime(l)).collect()
310 }
311
312 pub fn lower_lifetime_defs(lts: &Vec<LifetimeDef>) -> Vec<hir::LifetimeDef> {
313     lts.iter().map(|l| lower_lifetime_def(l)).collect()
314 }
315
316 pub fn lower_opt_lifetime(o_lt: &Option<Lifetime>) -> Option<hir::Lifetime> {
317     o_lt.as_ref().map(|lt| lower_lifetime(lt))
318 }
319
320 pub fn lower_generics(g: &Generics) -> hir::Generics {
321     hir::Generics {
322         ty_params: lower_ty_params(&g.ty_params),
323         lifetimes: lower_lifetime_defs(&g.lifetimes),
324         where_clause: lower_where_clause(&g.where_clause),
325     }
326 }
327
328 pub fn lower_where_clause(wc: &WhereClause) -> hir::WhereClause {
329     hir::WhereClause {
330         id: wc.id,
331         predicates: wc.predicates
332                       .iter()
333                       .map(|predicate| lower_where_predicate(predicate))
334                       .collect(),
335     }
336 }
337
338 pub fn lower_where_predicate(pred: &WherePredicate) -> hir::WherePredicate {
339     match *pred {
340         WherePredicate::BoundPredicate(WhereBoundPredicate{ ref bound_lifetimes,
341                                                             ref bounded_ty,
342                                                             ref bounds,
343                                                             span}) => {
344             hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
345                 bound_lifetimes: lower_lifetime_defs(bound_lifetimes),
346                 bounded_ty: lower_ty(bounded_ty),
347                 bounds: bounds.iter().map(|x| lower_ty_param_bound(x)).collect(),
348                 span: span,
349             })
350         }
351         WherePredicate::RegionPredicate(WhereRegionPredicate{ ref lifetime,
352                                                               ref bounds,
353                                                               span}) => {
354             hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
355                 span: span,
356                 lifetime: lower_lifetime(lifetime),
357                 bounds: bounds.iter().map(|bound| lower_lifetime(bound)).collect(),
358             })
359         }
360         WherePredicate::EqPredicate(WhereEqPredicate{ id,
361                                                       ref path,
362                                                       ref ty,
363                                                       span}) => {
364             hir::WherePredicate::EqPredicate(hir::WhereEqPredicate {
365                 id: id,
366                 path: lower_path(path),
367                 ty: lower_ty(ty),
368                 span: span,
369             })
370         }
371     }
372 }
373
374 pub fn lower_struct_def(sd: &StructDef) -> P<hir::StructDef> {
375     P(hir::StructDef {
376         fields: sd.fields.iter().map(|f| lower_struct_field(f)).collect(),
377         ctor_id: sd.ctor_id,
378     })
379 }
380
381 pub fn lower_trait_ref(p: &TraitRef) -> hir::TraitRef {
382     hir::TraitRef {
383         path: lower_path(&p.path),
384         ref_id: p.ref_id,
385     }
386 }
387
388 pub fn lower_poly_trait_ref(p: &PolyTraitRef) -> hir::PolyTraitRef {
389     hir::PolyTraitRef {
390         bound_lifetimes: lower_lifetime_defs(&p.bound_lifetimes),
391         trait_ref: lower_trait_ref(&p.trait_ref),
392         span: p.span,
393     }
394 }
395
396 pub fn lower_struct_field(f: &StructField) -> hir::StructField {
397     Spanned {
398         node: hir::StructField_ {
399             id: f.node.id,
400             kind: lower_struct_field_kind(&f.node.kind),
401             ty: lower_ty(&f.node.ty),
402             attrs: f.node.attrs.clone(),
403         },
404         span: f.span,
405     }
406 }
407
408 pub fn lower_field(f: &Field) -> hir::Field {
409     hir::Field {
410         name: respan(f.ident.span, f.ident.node.name),
411         expr: lower_expr(&f.expr),
412         span: f.span,
413     }
414 }
415
416 pub fn lower_mt(mt: &MutTy) -> hir::MutTy {
417     hir::MutTy {
418         ty: lower_ty(&mt.ty),
419         mutbl: lower_mutability(mt.mutbl),
420     }
421 }
422
423 pub fn lower_opt_bounds(b: &Option<OwnedSlice<TyParamBound>>)
424                         -> Option<OwnedSlice<hir::TyParamBound>> {
425     b.as_ref().map(|ref bounds| lower_bounds(bounds))
426 }
427
428 fn lower_bounds(bounds: &TyParamBounds) -> hir::TyParamBounds {
429     bounds.iter().map(|bound| lower_ty_param_bound(bound)).collect()
430 }
431
432 fn lower_variant_arg(va: &VariantArg) -> hir::VariantArg {
433     hir::VariantArg {
434         id: va.id,
435         ty: lower_ty(&va.ty),
436     }
437 }
438
439 pub fn lower_block(b: &Block) -> P<hir::Block> {
440     P(hir::Block {
441         id: b.id,
442         stmts: b.stmts.iter().map(|s| lower_stmt(s)).collect(),
443         expr: b.expr.as_ref().map(|ref x| lower_expr(x)),
444         rules: lower_block_check_mode(&b.rules),
445         span: b.span,
446     })
447 }
448
449 pub fn lower_item_underscore(i: &Item_) -> hir::Item_ {
450     match *i {
451         ItemExternCrate(string) => hir::ItemExternCrate(string),
452         ItemUse(ref view_path) => {
453             hir::ItemUse(lower_view_path(view_path))
454         }
455         ItemStatic(ref t, m, ref e) => {
456             hir::ItemStatic(lower_ty(t), lower_mutability(m), lower_expr(e))
457         }
458         ItemConst(ref t, ref e) => {
459             hir::ItemConst(lower_ty(t), lower_expr(e))
460         }
461         ItemFn(ref decl, unsafety, constness, abi, ref generics, ref body) => {
462             hir::ItemFn(lower_fn_decl(decl),
463                         lower_unsafety(unsafety),
464                         lower_constness(constness),
465                         abi,
466                         lower_generics(generics),
467                         lower_block(body))
468         }
469         ItemMod(ref m) => hir::ItemMod(lower_mod(m)),
470         ItemForeignMod(ref nm) => hir::ItemForeignMod(lower_foreign_mod(nm)),
471         ItemTy(ref t, ref generics) => {
472             hir::ItemTy(lower_ty(t), lower_generics(generics))
473         }
474         ItemEnum(ref enum_definition, ref generics) => {
475             hir::ItemEnum(hir::EnumDef {
476                               variants: enum_definition.variants
477                                                        .iter()
478                                                        .map(|x| lower_variant(x))
479                                                        .collect(),
480                           },
481                           lower_generics(generics))
482         }
483         ItemStruct(ref struct_def, ref generics) => {
484             let struct_def = lower_struct_def(struct_def);
485             hir::ItemStruct(struct_def, lower_generics(generics))
486         }
487         ItemDefaultImpl(unsafety, ref trait_ref) => {
488             hir::ItemDefaultImpl(lower_unsafety(unsafety), lower_trait_ref(trait_ref))
489         }
490         ItemImpl(unsafety, polarity, ref generics, ref ifce, ref ty, ref impl_items) => {
491             let new_impl_items = impl_items.iter().map(|item| lower_impl_item(item)).collect();
492             let ifce = ifce.as_ref().map(|trait_ref| lower_trait_ref(trait_ref));
493             hir::ItemImpl(lower_unsafety(unsafety),
494                           lower_impl_polarity(polarity),
495                           lower_generics(generics),
496                           ifce,
497                           lower_ty(ty),
498                           new_impl_items)
499         }
500         ItemTrait(unsafety, ref generics, ref bounds, ref items) => {
501             let bounds = lower_bounds(bounds);
502             let items = items.iter().map(|item| lower_trait_item(item)).collect();
503             hir::ItemTrait(lower_unsafety(unsafety),
504                            lower_generics(generics),
505                            bounds,
506                            items)
507         }
508         ItemMac(_) => panic!("Shouldn't still be around"),
509     }
510 }
511
512 pub fn lower_trait_item(i: &TraitItem) -> P<hir::TraitItem> {
513     P(hir::TraitItem {
514         id: i.id,
515         name: i.ident.name,
516         attrs: i.attrs.clone(),
517         node: match i.node {
518             ConstTraitItem(ref ty, ref default) => {
519                 hir::ConstTraitItem(lower_ty(ty),
520                                     default.as_ref().map(|x| lower_expr(x)))
521             }
522             MethodTraitItem(ref sig, ref body) => {
523                 hir::MethodTraitItem(lower_method_sig(sig),
524                                      body.as_ref().map(|x| lower_block(x)))
525             }
526             TypeTraitItem(ref bounds, ref default) => {
527                 hir::TypeTraitItem(lower_bounds(bounds),
528                                    default.as_ref().map(|x| lower_ty(x)))
529             }
530         },
531         span: i.span,
532     })
533 }
534
535 pub fn lower_impl_item(i: &ImplItem) -> P<hir::ImplItem> {
536     P(hir::ImplItem {
537         id: i.id,
538         name: i.ident.name,
539         attrs: i.attrs.clone(),
540         vis: lower_visibility(i.vis),
541         node: match i.node {
542             ConstImplItem(ref ty, ref expr) => {
543                 hir::ConstImplItem(lower_ty(ty), lower_expr(expr))
544             }
545             MethodImplItem(ref sig, ref body) => {
546                 hir::MethodImplItem(lower_method_sig(sig), lower_block(body))
547             }
548             TypeImplItem(ref ty) => hir::TypeImplItem(lower_ty(ty)),
549             MacImplItem(..) => panic!("Shouldn't exist any more"),
550         },
551         span: i.span,
552     })
553 }
554
555 pub fn lower_mod(m: &Mod) -> hir::Mod {
556     hir::Mod {
557         inner: m.inner,
558         items: m.items.iter().map(|x| lower_item(x)).collect(),
559     }
560 }
561
562 pub fn lower_crate(c: &Crate) -> hir::Crate {
563     hir::Crate {
564         module: lower_mod(&c.module),
565         attrs: c.attrs.clone(),
566         config: c.config.clone(),
567         span: c.span,
568         exported_macros: c.exported_macros.iter().map(|m| lower_macro_def(m)).collect(),
569     }
570 }
571
572 pub fn lower_macro_def(m: &MacroDef) -> hir::MacroDef {
573     hir::MacroDef {
574         name: m.ident.name,
575         attrs: m.attrs.clone(),
576         id: m.id,
577         span: m.span,
578         imported_from: m.imported_from.map(|x| x.name),
579         export: m.export,
580         use_locally: m.use_locally,
581         allow_internal_unstable: m.allow_internal_unstable,
582         body: m.body.clone(),
583     }
584 }
585
586 // fold one item into possibly many items
587 pub fn lower_item(i: &Item) -> P<hir::Item> {
588     P(lower_item_simple(i))
589 }
590
591 // fold one item into exactly one item
592 pub fn lower_item_simple(i: &Item) -> hir::Item {
593     let node = lower_item_underscore(&i.node);
594
595     hir::Item {
596         id: i.id,
597         name: i.ident.name,
598         attrs: i.attrs.clone(),
599         node: node,
600         vis: lower_visibility(i.vis),
601         span: i.span,
602     }
603 }
604
605 pub fn lower_foreign_item(i: &ForeignItem) -> P<hir::ForeignItem> {
606     P(hir::ForeignItem {
607         id: i.id,
608         name: i.ident.name,
609         attrs: i.attrs.clone(),
610         node: match i.node {
611             ForeignItemFn(ref fdec, ref generics) => {
612                 hir::ForeignItemFn(lower_fn_decl(fdec), lower_generics(generics))
613             }
614             ForeignItemStatic(ref t, m) => {
615                 hir::ForeignItemStatic(lower_ty(t), m)
616             }
617         },
618         vis: lower_visibility(i.vis),
619         span: i.span,
620     })
621 }
622
623 pub fn lower_method_sig(sig: &MethodSig) -> hir::MethodSig {
624     hir::MethodSig {
625         generics: lower_generics(&sig.generics),
626         abi: sig.abi,
627         explicit_self: lower_explicit_self(&sig.explicit_self),
628         unsafety: lower_unsafety(sig.unsafety),
629         constness: lower_constness(sig.constness),
630         decl: lower_fn_decl(&sig.decl),
631     }
632 }
633
634 pub fn lower_unsafety(u: Unsafety) -> hir::Unsafety {
635     match u {
636         Unsafety::Unsafe => hir::Unsafety::Unsafe,
637         Unsafety::Normal => hir::Unsafety::Normal,
638     }
639 }
640
641 pub fn lower_constness(c: Constness) -> hir::Constness {
642     match c {
643         Constness::Const => hir::Constness::Const,
644         Constness::NotConst => hir::Constness::NotConst,
645     }
646 }
647
648 pub fn lower_unop(u: UnOp) -> hir::UnOp {
649     match u {
650         UnDeref => hir::UnDeref,
651         UnNot => hir::UnNot,
652         UnNeg => hir::UnNeg,
653     }
654 }
655
656 pub fn lower_binop(b: BinOp) -> hir::BinOp {
657     Spanned {
658         node: match b.node {
659             BiAdd => hir::BiAdd,
660             BiSub => hir::BiSub,
661             BiMul => hir::BiMul,
662             BiDiv => hir::BiDiv,
663             BiRem => hir::BiRem,
664             BiAnd => hir::BiAnd,
665             BiOr => hir::BiOr,
666             BiBitXor => hir::BiBitXor,
667             BiBitAnd => hir::BiBitAnd,
668             BiBitOr => hir::BiBitOr,
669             BiShl => hir::BiShl,
670             BiShr => hir::BiShr,
671             BiEq => hir::BiEq,
672             BiLt => hir::BiLt,
673             BiLe => hir::BiLe,
674             BiNe => hir::BiNe,
675             BiGe => hir::BiGe,
676             BiGt => hir::BiGt,
677         },
678         span: b.span,
679     }
680 }
681
682 pub fn lower_pat(p: &Pat) -> P<hir::Pat> {
683     P(hir::Pat {
684         id: p.id,
685         node: match p.node {
686             PatWild(k) => hir::PatWild(lower_pat_wild_kind(k)),
687             PatIdent(ref binding_mode, pth1, ref sub) => {
688                 hir::PatIdent(lower_binding_mode(binding_mode),
689                               pth1,
690                               sub.as_ref().map(|x| lower_pat(x)))
691             }
692             PatLit(ref e) => hir::PatLit(lower_expr(e)),
693             PatEnum(ref pth, ref pats) => {
694                 hir::PatEnum(lower_path(pth),
695                              pats.as_ref().map(|pats| pats.iter().map(|x| lower_pat(x)).collect()))
696             }
697             PatQPath(ref qself, ref pth) => {
698                 let qself = hir::QSelf {
699                     ty: lower_ty(&qself.ty),
700                     position: qself.position,
701                 };
702                 hir::PatQPath(qself, lower_path(pth))
703             }
704             PatStruct(ref pth, ref fields, etc) => {
705                 let pth = lower_path(pth);
706                 let fs = fields.iter()
707                                .map(|f| {
708                                    Spanned {
709                                        span: f.span,
710                                        node: hir::FieldPat {
711                                            name: f.node.ident.name,
712                                            pat: lower_pat(&f.node.pat),
713                                            is_shorthand: f.node.is_shorthand,
714                                        },
715                                    }
716                                })
717                                .collect();
718                 hir::PatStruct(pth, fs, etc)
719             }
720             PatTup(ref elts) => hir::PatTup(elts.iter().map(|x| lower_pat(x)).collect()),
721             PatBox(ref inner) => hir::PatBox(lower_pat(inner)),
722             PatRegion(ref inner, mutbl) =>
723                 hir::PatRegion(lower_pat(inner), lower_mutability(mutbl)),
724             PatRange(ref e1, ref e2) => {
725                 hir::PatRange(lower_expr(e1), lower_expr(e2))
726             }
727             PatVec(ref before, ref slice, ref after) => {
728                 hir::PatVec(before.iter().map(|x| lower_pat(x)).collect(),
729                             slice.as_ref().map(|x| lower_pat(x)),
730                             after.iter().map(|x| lower_pat(x)).collect())
731             }
732             PatMac(_) => panic!("Shouldn't exist here"),
733         },
734         span: p.span,
735     })
736 }
737
738 pub fn lower_expr(e: &Expr) -> P<hir::Expr> {
739     P(hir::Expr {
740         id: e.id,
741         node: match e.node {
742             ExprBox(ref e) => {
743                 hir::ExprBox(lower_expr(e))
744             }
745             ExprVec(ref exprs) => {
746                 hir::ExprVec(exprs.iter().map(|x| lower_expr(x)).collect())
747             }
748             ExprRepeat(ref expr, ref count) => {
749                 hir::ExprRepeat(lower_expr(expr), lower_expr(count))
750             }
751             ExprTup(ref elts) => hir::ExprTup(elts.iter().map(|x| lower_expr(x)).collect()),
752             ExprCall(ref f, ref args) => {
753                 hir::ExprCall(lower_expr(f),
754                               args.iter().map(|x| lower_expr(x)).collect())
755             }
756             ExprMethodCall(i, ref tps, ref args) => {
757                 hir::ExprMethodCall(respan(i.span, i.node.name),
758                                     tps.iter().map(|x| lower_ty(x)).collect(),
759                                     args.iter().map(|x| lower_expr(x)).collect())
760             }
761             ExprBinary(binop, ref lhs, ref rhs) => {
762                 hir::ExprBinary(lower_binop(binop), lower_expr(lhs), lower_expr(rhs))
763             }
764             ExprUnary(op, ref ohs) => {
765                 hir::ExprUnary(lower_unop(op), lower_expr(ohs))
766             }
767             ExprLit(ref l) => hir::ExprLit(P((**l).clone())),
768             ExprCast(ref expr, ref ty) => {
769                 hir::ExprCast(lower_expr(expr), lower_ty(ty))
770             }
771             ExprAddrOf(m, ref ohs) => hir::ExprAddrOf(lower_mutability(m), lower_expr(ohs)),
772             ExprIf(ref cond, ref tr, ref fl) => {
773                 hir::ExprIf(lower_expr(cond),
774                             lower_block(tr),
775                             fl.as_ref().map(|x| lower_expr(x)))
776             }
777             ExprWhile(ref cond, ref body, opt_ident) => {
778                 hir::ExprWhile(lower_expr(cond), lower_block(body), opt_ident)
779             }
780             ExprLoop(ref body, opt_ident) => {
781                 hir::ExprLoop(lower_block(body), opt_ident)
782             }
783             ExprMatch(ref expr, ref arms, ref source) => {
784                 hir::ExprMatch(lower_expr(expr),
785                                arms.iter().map(|x| lower_arm(x)).collect(),
786                                lower_match_source(source))
787             }
788             ExprClosure(capture_clause, ref decl, ref body) => {
789                 hir::ExprClosure(lower_capture_clause(capture_clause),
790                                  lower_fn_decl(decl),
791                                  lower_block(body))
792             }
793             ExprBlock(ref blk) => hir::ExprBlock(lower_block(blk)),
794             ExprAssign(ref el, ref er) => {
795                 hir::ExprAssign(lower_expr(el), lower_expr(er))
796             }
797             ExprAssignOp(op, ref el, ref er) => {
798                 hir::ExprAssignOp(lower_binop(op), lower_expr(el), lower_expr(er))
799             }
800             ExprField(ref el, ident) => {
801                 hir::ExprField(lower_expr(el), respan(ident.span, ident.node.name))
802             }
803             ExprTupField(ref el, ident) => {
804                 hir::ExprTupField(lower_expr(el), ident)
805             }
806             ExprIndex(ref el, ref er) => {
807                 hir::ExprIndex(lower_expr(el), lower_expr(er))
808             }
809             ExprRange(ref e1, ref e2) => {
810                 hir::ExprRange(e1.as_ref().map(|x| lower_expr(x)),
811                                e2.as_ref().map(|x| lower_expr(x)))
812             }
813             ExprPath(ref qself, ref path) => {
814                 let qself = qself.as_ref().map(|&QSelf { ref ty, position }| {
815                     hir::QSelf {
816                         ty: lower_ty(ty),
817                         position: position,
818                     }
819                 });
820                 hir::ExprPath(qself, lower_path(path))
821             }
822             ExprBreak(opt_ident) => hir::ExprBreak(opt_ident),
823             ExprAgain(opt_ident) => hir::ExprAgain(opt_ident),
824             ExprRet(ref e) => hir::ExprRet(e.as_ref().map(|x| lower_expr(x))),
825             ExprInlineAsm(InlineAsm {
826                     ref inputs,
827                     ref outputs,
828                     ref asm,
829                     asm_str_style,
830                     ref clobbers,
831                     volatile,
832                     alignstack,
833                     dialect,
834                     expn_id,
835                 }) => hir::ExprInlineAsm(hir::InlineAsm {
836                 inputs: inputs.iter()
837                               .map(|&(ref c, ref input)| (c.clone(), lower_expr(input)))
838                               .collect(),
839                 outputs: outputs.iter()
840                                 .map(|&(ref c, ref out, ref is_rw)| {
841                                     (c.clone(), lower_expr(out), *is_rw)
842                                 })
843                                 .collect(),
844                 asm: asm.clone(),
845                 asm_str_style: asm_str_style,
846                 clobbers: clobbers.clone(),
847                 volatile: volatile,
848                 alignstack: alignstack,
849                 dialect: dialect,
850                 expn_id: expn_id,
851             }),
852             ExprStruct(ref path, ref fields, ref maybe_expr) => {
853                 hir::ExprStruct(lower_path(path),
854                                 fields.iter().map(|x| lower_field(x)).collect(),
855                                 maybe_expr.as_ref().map(|x| lower_expr(x)))
856             }
857             ExprParen(ref ex) => {
858                 return lower_expr(ex);
859             }
860             ExprInPlace(..) |
861             ExprIfLet(..) |
862             ExprWhileLet(..) |
863             ExprForLoop(..) |
864             ExprMac(_) => panic!("Shouldn't exist here"),
865         },
866         span: e.span,
867     })
868 }
869
870 pub fn lower_stmt(s: &Stmt) -> P<hir::Stmt> {
871     match s.node {
872         StmtDecl(ref d, id) => {
873             P(Spanned {
874                 node: hir::StmtDecl(lower_decl(d), id),
875                 span: s.span,
876             })
877         }
878         StmtExpr(ref e, id) => {
879             P(Spanned {
880                 node: hir::StmtExpr(lower_expr(e), id),
881                 span: s.span,
882             })
883         }
884         StmtSemi(ref e, id) => {
885             P(Spanned {
886                 node: hir::StmtSemi(lower_expr(e), id),
887                 span: s.span,
888             })
889         }
890         StmtMac(..) => panic!("Shouldn't exist here"),
891     }
892 }
893
894 pub fn lower_match_source(m: &MatchSource) -> hir::MatchSource {
895     match *m {
896         MatchSource::Normal => hir::MatchSource::Normal,
897         MatchSource::IfLetDesugar { contains_else_clause } => {
898             hir::MatchSource::IfLetDesugar { contains_else_clause: contains_else_clause }
899         }
900         MatchSource::WhileLetDesugar => hir::MatchSource::WhileLetDesugar,
901         MatchSource::ForLoopDesugar => hir::MatchSource::ForLoopDesugar,
902     }
903 }
904
905 pub fn lower_capture_clause(c: CaptureClause) -> hir::CaptureClause {
906     match c {
907         CaptureByValue => hir::CaptureByValue,
908         CaptureByRef => hir::CaptureByRef,
909     }
910 }
911
912 pub fn lower_visibility(v: Visibility) -> hir::Visibility {
913     match v {
914         Public => hir::Public,
915         Inherited => hir::Inherited,
916     }
917 }
918
919 pub fn lower_block_check_mode(b: &BlockCheckMode) -> hir::BlockCheckMode {
920     match *b {
921         DefaultBlock => hir::DefaultBlock,
922         UnsafeBlock(u) => hir::UnsafeBlock(lower_unsafe_source(u)),
923         PushUnsafeBlock(u) => hir::PushUnsafeBlock(lower_unsafe_source(u)),
924         PopUnsafeBlock(u) => hir::PopUnsafeBlock(lower_unsafe_source(u)),
925     }
926 }
927
928 pub fn lower_pat_wild_kind(p: PatWildKind) -> hir::PatWildKind {
929     match p {
930         PatWildSingle => hir::PatWildSingle,
931         PatWildMulti => hir::PatWildMulti,
932     }
933 }
934
935 pub fn lower_binding_mode(b: &BindingMode) -> hir::BindingMode {
936     match *b {
937         BindByRef(m) => hir::BindByRef(lower_mutability(m)),
938         BindByValue(m) => hir::BindByValue(lower_mutability(m)),
939     }
940 }
941
942 pub fn lower_struct_field_kind(s: &StructFieldKind) -> hir::StructFieldKind {
943     match *s {
944         NamedField(ident, vis) => hir::NamedField(ident.name, lower_visibility(vis)),
945         UnnamedField(vis) => hir::UnnamedField(lower_visibility(vis)),
946     }
947 }
948
949 pub fn lower_unsafe_source(u: UnsafeSource) -> hir::UnsafeSource {
950     match u {
951         CompilerGenerated => hir::CompilerGenerated,
952         UserProvided => hir::UserProvided,
953     }
954 }
955
956 pub fn lower_impl_polarity(i: ImplPolarity) -> hir::ImplPolarity {
957     match i {
958         ImplPolarity::Positive => hir::ImplPolarity::Positive,
959         ImplPolarity::Negative => hir::ImplPolarity::Negative,
960     }
961 }
962
963 pub fn lower_trait_bound_modifier(f: TraitBoundModifier) -> hir::TraitBoundModifier {
964     match f {
965         TraitBoundModifier::None => hir::TraitBoundModifier::None,
966         TraitBoundModifier::Maybe => hir::TraitBoundModifier::Maybe,
967     }
968 }