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