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.
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 // Lowers the AST to the HIR
17 use syntax::codemap::Spanned;
18 use syntax::owned_slice::OwnedSlice;
21 pub fn lower_meta_items(meta_items: &Vec<P<MetaItem>>) -> Vec<P<hir::MetaItem>> {
22 meta_items.iter().map(|x| lower_meta_item(x)).collect()
25 pub fn lower_view_path(view_path: &ViewPath) -> P<hir::ViewPath> {
27 node: match view_path.node {
28 ViewPathSimple(ident, ref path) => {
29 hir::ViewPathSimple(ident, lower_path(path))
31 ViewPathGlob(ref path) => {
32 hir::ViewPathGlob(lower_path(path))
34 ViewPathList(ref path, ref path_list_idents) => {
35 hir::ViewPathList(lower_path(path),
36 path_list_idents.iter().map(|path_list_ident| {
38 node: match path_list_ident.node {
39 PathListIdent { id, name, rename } =>
43 rename: rename.clone(),
45 PathListMod { id, rename } =>
46 hir::PathListMod { id: id, rename: rename.clone() }
48 span: path_list_ident.span
57 pub fn lower_attrs(attrs: &Vec<Attribute>) -> Vec<hir::Attribute> {
58 attrs.iter().map(|x| lower_attribute(x)).collect()
61 pub fn lower_arm(arm: &Arm) -> hir::Arm {
63 attrs: lower_attrs(&arm.attrs),
64 pats: arm.pats.iter().map(|x| lower_pat(x)).collect(),
65 guard: arm.guard.as_ref().map(|ref x| lower_expr(x)),
66 body: lower_expr(&arm.body),
70 pub fn lower_decl(d: &Decl) -> P<hir::Decl> {
72 DeclLocal(ref l) => P(Spanned {
73 node: hir::DeclLocal(lower_local(l)),
76 DeclItem(ref it) => P(Spanned {
77 node: hir::DeclItem(lower_item(it)),
83 pub fn lower_ty_binding(b: &TypeBinding) -> P<hir::TypeBinding> {
84 P(hir::TypeBinding { id: b.id, ident: b.ident, ty: lower_ty(&b.ty), span: b.span })
87 pub fn lower_ty(t: &Ty) -> P<hir::Ty> {
91 TyInfer => hir::TyInfer,
92 TyVec(ref ty) => hir::TyVec(lower_ty(ty)),
93 TyPtr(ref mt) => hir::TyPtr(lower_mt(mt)),
94 TyRptr(ref region, ref mt) => {
95 hir::TyRptr(lower_opt_lifetime(region), lower_mt(mt))
98 hir::TyBareFn(P(hir::BareFnTy {
99 lifetimes: lower_lifetime_defs(&f.lifetimes),
100 unsafety: lower_unsafety(f.unsafety),
102 decl: lower_fn_decl(&f.decl)
105 TyTup(ref tys) => hir::TyTup(tys.iter().map(|ty| lower_ty(ty)).collect()),
106 TyParen(ref ty) => hir::TyParen(lower_ty(ty)),
107 TyPath(ref qself, ref path) => {
108 let qself = qself.as_ref().map(|&QSelf { ref ty, position }| {
114 hir::TyPath(qself, lower_path(path))
116 TyObjectSum(ref ty, ref bounds) => {
117 hir::TyObjectSum(lower_ty(ty),
118 lower_bounds(bounds))
120 TyFixedLengthVec(ref ty, ref e) => {
121 hir::TyFixedLengthVec(lower_ty(ty), lower_expr(e))
123 TyTypeof(ref expr) => {
124 hir::TyTypeof(lower_expr(expr))
126 TyPolyTraitRef(ref bounds) => {
127 hir::TyPolyTraitRef(bounds.iter().map(|b| lower_ty_param_bound(b)).collect())
129 TyMac(_) => panic!("TyMac should have been expanded by now."),
135 pub fn lower_foreign_mod(fm: &ForeignMod) -> hir::ForeignMod {
138 items: fm.items.iter().map(|x| lower_foreign_item(x)).collect(),
142 pub fn lower_variant(v: &Variant) -> P<hir::Variant> {
144 node: hir::Variant_ {
147 attrs: lower_attrs(&v.node.attrs),
148 kind: match v.node.kind {
149 TupleVariantKind(ref variant_args) => {
150 hir::TupleVariantKind(variant_args.iter().map(|ref x|
151 lower_variant_arg(x)).collect())
153 StructVariantKind(ref struct_def) => {
154 hir::StructVariantKind(lower_struct_def(struct_def))
157 disr_expr: v.node.disr_expr.as_ref().map(|e| lower_expr(e)),
158 vis: lower_visibility(v.node.vis),
164 pub fn lower_path(p: &Path) -> hir::Path {
167 segments: p.segments.iter().map(|&PathSegment {identifier, ref parameters}|
169 identifier: identifier,
170 parameters: lower_path_parameters(parameters),
176 pub fn lower_path_parameters(path_parameters: &PathParameters) -> hir::PathParameters {
177 match *path_parameters {
178 AngleBracketedParameters(ref data) =>
179 hir::AngleBracketedParameters(lower_angle_bracketed_parameter_data(data)),
180 ParenthesizedParameters(ref data) =>
181 hir::ParenthesizedParameters(lower_parenthesized_parameter_data(data)),
185 pub fn lower_angle_bracketed_parameter_data(data: &AngleBracketedParameterData)
186 -> hir::AngleBracketedParameterData {
187 let &AngleBracketedParameterData { ref lifetimes, ref types, ref bindings } = data;
188 hir::AngleBracketedParameterData {
189 lifetimes: lower_lifetimes(lifetimes),
190 types: types.iter().map(|ty| lower_ty(ty)).collect(),
191 bindings: bindings.iter().map(|b| lower_ty_binding(b)).collect(),
195 pub fn lower_parenthesized_parameter_data(data: &ParenthesizedParameterData)
196 -> hir::ParenthesizedParameterData {
197 let &ParenthesizedParameterData { ref inputs, ref output, span } = data;
198 hir::ParenthesizedParameterData {
199 inputs: inputs.iter().map(|ty| lower_ty(ty)).collect(),
200 output: output.as_ref().map(|ty| lower_ty(ty)),
205 pub fn lower_local(l: &Local) -> P<hir::Local> {
208 ty: l.ty.as_ref().map(|t| lower_ty(t)),
209 pat: lower_pat(&l.pat),
210 init: l.init.as_ref().map(|e| lower_expr(e)),
215 pub fn lower_attribute(at: &Attribute) -> hir::Attribute {
217 node: hir::Attribute_ {
218 id: hir::AttrId(at.node.id.0),
219 style: lower_attr_style(at.node.style),
220 value: lower_meta_item(&at.node.value),
221 is_sugared_doc: at.node.is_sugared_doc,
227 // FIXME we should probably just unify hir and ast Attributes.
228 pub fn unlower_attribute(at: &hir::Attribute) -> Attribute {
231 id: AttrId(at.node.id.0),
232 style: unlower_attr_style(at.node.style),
233 value: unlower_meta_item(&at.node.value),
234 is_sugared_doc: at.node.is_sugared_doc,
240 pub fn lower_explicit_self_underscore(es: &ExplicitSelf_) -> hir::ExplicitSelf_ {
242 SelfStatic => hir::SelfStatic,
243 SelfValue(v) => hir::SelfValue(v),
244 SelfRegion(ref lifetime, m, ident) => {
245 hir::SelfRegion(lower_opt_lifetime(lifetime), lower_mutability(m), ident)
247 SelfExplicit(ref typ, ident) => {
248 hir::SelfExplicit(lower_ty(typ), ident)
253 pub fn lower_mutability(m: Mutability) -> hir::Mutability {
255 MutMutable => hir::MutMutable,
256 MutImmutable => hir::MutImmutable,
260 pub fn lower_explicit_self(s: &ExplicitSelf) -> hir::ExplicitSelf {
261 Spanned { node: lower_explicit_self_underscore(&s.node), span: s.span }
265 pub fn lower_meta_item(mi: &MetaItem) -> P<hir::MetaItem> {
267 node: match mi.node {
268 MetaWord(ref id) => hir::MetaWord(id.clone()),
269 MetaList(ref id, ref mis) => {
270 hir::MetaList(id.clone(), mis.iter().map(|mi| lower_meta_item(mi)).collect())
272 MetaNameValue(ref id, ref s) => hir::MetaNameValue(id.clone(), lower_lit(s))
278 pub fn unlower_meta_item(mi: &hir::MetaItem) -> P<MetaItem> {
280 node: match mi.node {
281 hir::MetaWord(ref id) => MetaWord(id.clone()),
282 hir::MetaList(ref id, ref mis) => {
283 MetaList(id.clone(), mis.iter().map(|mi| unlower_meta_item(mi)).collect())
285 hir::MetaNameValue(ref id, ref s) => MetaNameValue(id.clone(), unlower_lit(s))
291 pub fn lower_arg(arg: &Arg) -> hir::Arg {
292 hir::Arg { id: arg.id, pat: lower_pat(&arg.pat), ty: lower_ty(&arg.ty) }
295 pub fn lower_fn_decl(decl: &FnDecl) -> P<hir::FnDecl> {
297 inputs: decl.inputs.iter().map(|x| lower_arg(x)).collect(),
298 output: match decl.output {
299 Return(ref ty) => hir::Return(lower_ty(ty)),
300 DefaultReturn(span) => hir::DefaultReturn(span),
301 NoReturn(span) => hir::NoReturn(span)
303 variadic: decl.variadic,
307 pub fn lower_ty_param_bound(tpb: &TyParamBound) -> hir::TyParamBound {
309 TraitTyParamBound(ref ty, modifier) => {
310 hir::TraitTyParamBound(lower_poly_trait_ref(ty), lower_trait_bound_modifier(modifier))
312 RegionTyParamBound(ref lifetime) => hir::RegionTyParamBound(lower_lifetime(lifetime)),
316 pub fn lower_ty_param(tp: &TyParam) -> hir::TyParam {
320 bounds: lower_bounds(&tp.bounds),
321 default: tp.default.as_ref().map(|x| lower_ty(x)),
326 pub fn lower_ty_params(tps: &OwnedSlice<TyParam>) -> OwnedSlice<hir::TyParam> {
327 tps.iter().map(|tp| lower_ty_param(tp)).collect()
330 pub fn lower_lifetime(l: &Lifetime) -> hir::Lifetime {
331 hir::Lifetime { id: l.id, name: l.name, span: l.span }
334 pub fn lower_lifetime_def(l: &LifetimeDef) -> hir::LifetimeDef {
335 hir::LifetimeDef { lifetime: lower_lifetime(&l.lifetime), bounds: lower_lifetimes(&l.bounds) }
338 pub fn lower_lifetimes(lts: &Vec<Lifetime>) -> Vec<hir::Lifetime> {
339 lts.iter().map(|l| lower_lifetime(l)).collect()
342 pub fn lower_lifetime_defs(lts: &Vec<LifetimeDef>) -> Vec<hir::LifetimeDef> {
343 lts.iter().map(|l| lower_lifetime_def(l)).collect()
346 pub fn lower_opt_lifetime(o_lt: &Option<Lifetime>) -> Option<hir::Lifetime> {
347 o_lt.as_ref().map(|lt| lower_lifetime(lt))
350 pub fn lower_generics(g: &Generics) -> hir::Generics {
352 ty_params: lower_ty_params(&g.ty_params),
353 lifetimes: lower_lifetime_defs(&g.lifetimes),
354 where_clause: lower_where_clause(&g.where_clause),
358 pub fn lower_where_clause(wc: &WhereClause) -> hir::WhereClause {
361 predicates: wc.predicates.iter().map(|predicate|
362 lower_where_predicate(predicate)).collect(),
366 pub fn lower_where_predicate(pred: &WherePredicate) -> hir::WherePredicate {
368 WherePredicate::BoundPredicate(WhereBoundPredicate{ ref bound_lifetimes,
372 hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
373 bound_lifetimes: lower_lifetime_defs(bound_lifetimes),
374 bounded_ty: lower_ty(bounded_ty),
375 bounds: bounds.iter().map(|x| lower_ty_param_bound(x)).collect(),
379 WherePredicate::RegionPredicate(WhereRegionPredicate{ ref lifetime,
382 hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
384 lifetime: lower_lifetime(lifetime),
385 bounds: bounds.iter().map(|bound| lower_lifetime(bound)).collect()
388 WherePredicate::EqPredicate(WhereEqPredicate{ id,
392 hir::WherePredicate::EqPredicate(hir::WhereEqPredicate{
394 path: lower_path(path),
402 pub fn lower_struct_def(sd: &StructDef) -> P<hir::StructDef> {
404 fields: sd.fields.iter().map(|f| lower_struct_field(f)).collect(),
409 pub fn lower_trait_ref(p: &TraitRef) -> hir::TraitRef {
410 hir::TraitRef { path: lower_path(&p.path), ref_id: p.ref_id }
413 pub fn lower_poly_trait_ref(p: &PolyTraitRef) -> hir::PolyTraitRef {
415 bound_lifetimes: lower_lifetime_defs(&p.bound_lifetimes),
416 trait_ref: lower_trait_ref(&p.trait_ref),
421 pub fn lower_struct_field(f: &StructField) -> hir::StructField {
423 node: hir::StructField_ {
425 kind: lower_struct_field_kind(&f.node.kind),
426 ty: lower_ty(&f.node.ty),
427 attrs: lower_attrs(&f.node.attrs),
433 pub fn lower_field(f: &Field) -> hir::Field {
434 hir::Field { ident: f.ident, expr: lower_expr(&f.expr), span: f.span }
437 pub fn lower_mt(mt: &MutTy) -> hir::MutTy {
438 hir::MutTy { ty: lower_ty(&mt.ty), mutbl: lower_mutability(mt.mutbl) }
441 pub fn lower_opt_bounds(b: &Option<OwnedSlice<TyParamBound>>)
442 -> Option<OwnedSlice<hir::TyParamBound>> {
443 b.as_ref().map(|ref bounds| lower_bounds(bounds))
446 fn lower_bounds(bounds: &TyParamBounds) -> hir::TyParamBounds {
447 bounds.iter().map(|bound| lower_ty_param_bound(bound)).collect()
450 fn lower_variant_arg(va: &VariantArg) -> hir::VariantArg {
451 hir::VariantArg { id: va.id, ty: lower_ty(&va.ty) }
454 pub fn lower_block(b: &Block) -> P<hir::Block> {
457 stmts: b.stmts.iter().map(|s| lower_stmt(s)).collect(),
458 expr: b.expr.as_ref().map(|ref x| lower_expr(x)),
459 rules: lower_block_check_mode(&b.rules),
464 pub fn lower_item_underscore(i: &Item_) -> hir::Item_ {
466 ItemExternCrate(string) => hir::ItemExternCrate(string),
467 ItemUse(ref view_path) => {
468 hir::ItemUse(lower_view_path(view_path))
470 ItemStatic(ref t, m, ref e) => {
471 hir::ItemStatic(lower_ty(t), lower_mutability(m), lower_expr(e))
473 ItemConst(ref t, ref e) => {
474 hir::ItemConst(lower_ty(t), lower_expr(e))
476 ItemFn(ref decl, unsafety, constness, abi, ref generics, ref body) => {
479 lower_unsafety(unsafety),
480 lower_constness(constness),
482 lower_generics(generics),
486 ItemMod(ref m) => hir::ItemMod(lower_mod(m)),
487 ItemForeignMod(ref nm) => hir::ItemForeignMod(lower_foreign_mod(nm)),
488 ItemTy(ref t, ref generics) => {
489 hir::ItemTy(lower_ty(t), lower_generics(generics))
491 ItemEnum(ref enum_definition, ref generics) => {
494 variants: enum_definition.variants.iter().map(|x| lower_variant(x)).collect(),
496 lower_generics(generics))
498 ItemStruct(ref struct_def, ref generics) => {
499 let struct_def = lower_struct_def(struct_def);
500 hir::ItemStruct(struct_def, lower_generics(generics))
502 ItemDefaultImpl(unsafety, ref trait_ref) => {
503 hir::ItemDefaultImpl(lower_unsafety(unsafety), lower_trait_ref(trait_ref))
505 ItemImpl(unsafety, polarity, ref generics, ref ifce, ref ty, ref impl_items) => {
506 let new_impl_items = impl_items.iter().map(|item| lower_impl_item(item)).collect();
507 let ifce = ifce.as_ref().map(|trait_ref| lower_trait_ref(trait_ref));
508 hir::ItemImpl(lower_unsafety(unsafety),
509 lower_impl_polarity(polarity),
510 lower_generics(generics),
515 ItemTrait(unsafety, ref generics, ref bounds, ref items) => {
516 let bounds = lower_bounds(bounds);
517 let items = items.iter().map(|item| lower_trait_item(item)).collect();
518 hir::ItemTrait(lower_unsafety(unsafety),
519 lower_generics(generics),
523 ItemMac(_) => panic!("Shouldn't still be around"),
527 pub fn lower_trait_item(i: &TraitItem) -> P<hir::TraitItem> {
531 attrs: lower_attrs(&i.attrs),
533 ConstTraitItem(ref ty, ref default) => {
534 hir::ConstTraitItem(lower_ty(ty),
535 default.as_ref().map(|x| lower_expr(x)))
537 MethodTraitItem(ref sig, ref body) => {
538 hir::MethodTraitItem(lower_method_sig(sig),
539 body.as_ref().map(|x| lower_block(x)))
541 TypeTraitItem(ref bounds, ref default) => {
542 hir::TypeTraitItem(lower_bounds(bounds),
543 default.as_ref().map(|x| lower_ty(x)))
550 pub fn lower_impl_item(i: &ImplItem) -> P<hir::ImplItem> {
554 attrs: lower_attrs(&i.attrs),
555 vis: lower_visibility(i.vis),
557 ConstImplItem(ref ty, ref expr) => {
558 hir::ConstImplItem(lower_ty(ty), lower_expr(expr))
560 MethodImplItem(ref sig, ref body) => {
561 hir::MethodImplItem(lower_method_sig(sig),
564 TypeImplItem(ref ty) => hir::TypeImplItem(lower_ty(ty)),
565 MacImplItem(..) => panic!("Shouldn't exist any more"),
571 pub fn lower_mod(m: &Mod) -> hir::Mod {
572 hir::Mod { inner: m.inner, items: m.items.iter().map(|x| lower_item(x)).collect() }
575 pub fn lower_crate(c: &Crate) -> hir::Crate {
576 let config = lower_meta_items(&c.config);
579 module: lower_mod(&c.module),
580 attrs: lower_attrs(&c.attrs),
583 exported_macros: c.exported_macros.iter().map(|m| lower_macro_def(m)).collect(),
587 pub fn lower_macro_def(m: &MacroDef) -> hir::MacroDef {
590 attrs: m.attrs.iter().map(|a| lower_attribute(a)).collect(),
593 imported_from: m.imported_from,
595 use_locally: m.use_locally,
596 allow_internal_unstable: m.allow_internal_unstable,
597 body: m.body.clone(),
601 // fold one item into possibly many items
602 pub fn lower_item(i: &Item) -> P<hir::Item> {
603 P(lower_item_simple(i))
606 // fold one item into exactly one item
607 pub fn lower_item_simple(i: &Item) -> hir::Item {
608 let node = lower_item_underscore(&i.node);
613 attrs: lower_attrs(&i.attrs),
615 vis: lower_visibility(i.vis),
620 pub fn lower_foreign_item(i: &ForeignItem) -> P<hir::ForeignItem> {
624 attrs: lower_attrs(&i.attrs),
626 ForeignItemFn(ref fdec, ref generics) => {
627 hir::ForeignItemFn(lower_fn_decl(fdec), lower_generics(generics))
629 ForeignItemStatic(ref t, m) => {
630 hir::ForeignItemStatic(lower_ty(t), m)
633 vis: lower_visibility(i.vis),
638 pub fn lower_method_sig(sig: &MethodSig) -> hir::MethodSig {
640 generics: lower_generics(&sig.generics),
642 explicit_self: lower_explicit_self(&sig.explicit_self),
643 unsafety: lower_unsafety(sig.unsafety),
644 constness: lower_constness(sig.constness),
645 decl: lower_fn_decl(&sig.decl),
649 pub fn lower_unsafety(u: Unsafety) -> hir::Unsafety {
651 Unsafety::Unsafe => hir::Unsafety::Unsafe,
652 Unsafety::Normal => hir::Unsafety::Normal,
656 pub fn lower_constness(c: Constness) -> hir::Constness {
658 Constness::Const => hir::Constness::Const,
659 Constness::NotConst => hir::Constness::NotConst,
663 pub fn lower_lit(l: &Lit) -> hir::Lit {
666 LitStr(ref i, s) => hir::LitStr(i.clone(), lower_string_style(s)),
667 LitByteStr(ref b) => hir::LitByteStr(b.clone()),
668 LitByte(u) => hir::LitByte(u),
669 LitChar(c) => hir::LitChar(c),
670 LitInt(u, ref t) => hir::LitInt(u, lower_lit_int_type(t)),
671 LitFloat(ref i, t) => hir::LitFloat(i.clone(), lower_float_ty(t)),
672 LitFloatUnsuffixed(ref i) => hir::LitFloatUnsuffixed(i.clone()),
673 LitBool(b) => hir::LitBool(b),
679 pub fn unlower_lit(l: &hir::Lit) -> Lit {
682 hir::LitStr(ref i, s) => LitStr(i.clone(), unlower_string_style(s)),
683 hir::LitByteStr(ref b) => LitByteStr(b.clone()),
684 hir::LitByte(u) => LitByte(u),
685 hir::LitChar(c) => LitChar(c),
686 hir::LitInt(u, ref t) => LitInt(u, unlower_lit_int_type(t)),
687 hir::LitFloat(ref i, t) => LitFloat(i.clone(), unlower_float_ty(t)),
688 hir::LitFloatUnsuffixed(ref i) => LitFloatUnsuffixed(i.clone()),
689 hir::LitBool(b) => LitBool(b),
694 pub fn lower_unop(u: UnOp) -> hir::UnOp {
696 UnUniq => hir::UnUniq,
697 UnDeref => hir::UnDeref,
703 pub fn lower_binop(b: BinOp) -> hir::BinOp {
713 BiBitXor => hir::BiBitXor,
714 BiBitAnd => hir::BiBitAnd,
715 BiBitOr => hir::BiBitOr,
729 pub fn lower_pat(p: &Pat) -> P<hir::Pat> {
733 PatWild(k) => hir::PatWild(lower_pat_wild_kind(k)),
734 PatIdent(ref binding_mode, pth1, ref sub) => {
735 hir::PatIdent(lower_binding_mode(binding_mode),
737 sub.as_ref().map(|x| lower_pat(x)))
739 PatLit(ref e) => hir::PatLit(lower_expr(e)),
740 PatEnum(ref pth, ref pats) => {
741 hir::PatEnum(lower_path(pth),
742 pats.as_ref().map(|pats| pats.iter().map(|x| lower_pat(x)).collect()))
744 PatQPath(ref qself, ref pth) => {
745 let qself = hir::QSelf {
746 ty: lower_ty(&qself.ty),
747 position: qself.position,
749 hir::PatQPath(qself, lower_path(pth))
751 PatStruct(ref pth, ref fields, etc) => {
752 let pth = lower_path(pth);
753 let fs = fields.iter().map(|f| {
754 Spanned { span: f.span,
755 node: hir::FieldPat {
757 pat: lower_pat(&f.node.pat),
758 is_shorthand: f.node.is_shorthand,
761 hir::PatStruct(pth, fs, etc)
763 PatTup(ref elts) => hir::PatTup(elts.iter().map(|x| lower_pat(x)).collect()),
764 PatBox(ref inner) => hir::PatBox(lower_pat(inner)),
765 PatRegion(ref inner, mutbl) => hir::PatRegion(lower_pat(inner),
766 lower_mutability(mutbl)),
767 PatRange(ref e1, ref e2) => {
768 hir::PatRange(lower_expr(e1), lower_expr(e2))
770 PatVec(ref before, ref slice, ref after) => {
771 hir::PatVec(before.iter().map(|x| lower_pat(x)).collect(),
772 slice.as_ref().map(|x| lower_pat(x)),
773 after.iter().map(|x| lower_pat(x)).collect())
775 PatMac(_) => panic!("Shouldn't exist here"),
781 pub fn lower_expr(e: &Expr) -> P<hir::Expr> {
785 ExprBox(ref p, ref e) => {
786 hir::ExprBox(p.as_ref().map(|e| lower_expr(e)), lower_expr(e))
788 ExprVec(ref exprs) => {
789 hir::ExprVec(exprs.iter().map(|x| lower_expr(x)).collect())
791 ExprRepeat(ref expr, ref count) => {
792 hir::ExprRepeat(lower_expr(expr), lower_expr(count))
794 ExprTup(ref elts) => hir::ExprTup(elts.iter().map(|x| lower_expr(x)).collect()),
795 ExprCall(ref f, ref args) => {
796 hir::ExprCall(lower_expr(f),
797 args.iter().map(|x| lower_expr(x)).collect())
799 ExprMethodCall(i, ref tps, ref args) => {
802 tps.iter().map(|x| lower_ty(x)).collect(),
803 args.iter().map(|x| lower_expr(x)).collect())
805 ExprBinary(binop, ref lhs, ref rhs) => {
806 hir::ExprBinary(lower_binop(binop),
810 ExprUnary(op, ref ohs) => {
811 hir::ExprUnary(lower_unop(op), lower_expr(ohs))
813 ExprLit(ref l) => hir::ExprLit(P(lower_lit(l))),
814 ExprCast(ref expr, ref ty) => {
815 hir::ExprCast(lower_expr(expr), lower_ty(ty))
817 ExprAddrOf(m, ref ohs) => hir::ExprAddrOf(lower_mutability(m), lower_expr(ohs)),
818 ExprIf(ref cond, ref tr, ref fl) => {
819 hir::ExprIf(lower_expr(cond),
821 fl.as_ref().map(|x| lower_expr(x)))
823 ExprWhile(ref cond, ref body, opt_ident) => {
824 hir::ExprWhile(lower_expr(cond),
828 ExprLoop(ref body, opt_ident) => {
829 hir::ExprLoop(lower_block(body),
832 ExprMatch(ref expr, ref arms, ref source) => {
833 hir::ExprMatch(lower_expr(expr),
834 arms.iter().map(|x| lower_arm(x)).collect(),
835 lower_match_source(source))
837 ExprClosure(capture_clause, ref decl, ref body) => {
838 hir::ExprClosure(lower_capture_clause(capture_clause),
842 ExprBlock(ref blk) => hir::ExprBlock(lower_block(blk)),
843 ExprAssign(ref el, ref er) => {
844 hir::ExprAssign(lower_expr(el), lower_expr(er))
846 ExprAssignOp(op, ref el, ref er) => {
847 hir::ExprAssignOp(lower_binop(op),
851 ExprField(ref el, ident) => {
852 hir::ExprField(lower_expr(el), ident)
854 ExprTupField(ref el, ident) => {
855 hir::ExprTupField(lower_expr(el), ident)
857 ExprIndex(ref el, ref er) => {
858 hir::ExprIndex(lower_expr(el), lower_expr(er))
860 ExprRange(ref e1, ref e2) => {
861 hir::ExprRange(e1.as_ref().map(|x| lower_expr(x)),
862 e2.as_ref().map(|x| lower_expr(x)))
864 ExprPath(ref qself, ref path) => {
865 let qself = qself.as_ref().map(|&QSelf { ref ty, position }| {
871 hir::ExprPath(qself, lower_path(path))
873 ExprBreak(opt_ident) => hir::ExprBreak(opt_ident),
874 ExprAgain(opt_ident) => hir::ExprAgain(opt_ident),
875 ExprRet(ref e) => hir::ExprRet(e.as_ref().map(|x| lower_expr(x))),
876 ExprInlineAsm(InlineAsm {
886 }) => hir::ExprInlineAsm(hir::InlineAsm {
887 inputs: inputs.iter().map(|&(ref c, ref input)| {
888 (c.clone(), lower_expr(input))
890 outputs: outputs.iter().map(|&(ref c, ref out, ref is_rw)| {
891 (c.clone(), lower_expr(out), *is_rw)
894 asm_str_style: lower_string_style(asm_str_style),
895 clobbers: clobbers.clone(),
897 alignstack: alignstack,
898 dialect: lower_asm_dialect(dialect),
901 ExprStruct(ref path, ref fields, ref maybe_expr) => {
902 hir::ExprStruct(lower_path(path),
903 fields.iter().map(|x| lower_field(x)).collect(),
904 maybe_expr.as_ref().map(|x| lower_expr(x)))
906 ExprParen(ref ex) => hir::ExprParen(lower_expr(ex)),
910 ExprMac(_) => panic!("Shouldn't exist here"),
916 pub fn lower_stmt(s: &Stmt) -> P<hir::Stmt> {
918 StmtDecl(ref d, id) => {
920 node: hir::StmtDecl(lower_decl(d), id),
924 StmtExpr(ref e, id) => {
926 node: hir::StmtExpr(lower_expr(e), id),
930 StmtSemi(ref e, id) => {
932 node: hir::StmtSemi(lower_expr(e), id),
936 StmtMac(..) => panic!("Shouldn't exist here")
940 pub fn lower_string_style(s: StrStyle) -> hir::StrStyle {
942 CookedStr => hir::CookedStr,
943 RawStr(u) => hir::RawStr(u),
947 pub fn unlower_string_style(s: hir::StrStyle) -> StrStyle {
949 hir::CookedStr => CookedStr,
950 hir::RawStr(u) => RawStr(u),
954 pub fn lower_match_source(m: &MatchSource) -> hir::MatchSource {
956 MatchSource::Normal => hir::MatchSource::Normal,
957 MatchSource::IfLetDesugar { contains_else_clause } => {
958 hir::MatchSource::IfLetDesugar { contains_else_clause: contains_else_clause }
960 MatchSource::WhileLetDesugar => hir::MatchSource::WhileLetDesugar,
961 MatchSource::ForLoopDesugar => hir::MatchSource::ForLoopDesugar,
965 pub fn lower_capture_clause(c: CaptureClause) -> hir::CaptureClause {
967 CaptureByValue => hir::CaptureByValue,
968 CaptureByRef => hir::CaptureByRef,
972 pub fn lower_asm_dialect(a: AsmDialect) -> hir::AsmDialect {
974 AsmAtt => hir::AsmAtt,
975 AsmIntel => hir::AsmIntel,
979 pub fn lower_visibility(v: Visibility) -> hir::Visibility {
981 Public => hir::Public,
982 Inherited => hir::Inherited,
986 pub fn lower_block_check_mode(b: &BlockCheckMode) -> hir::BlockCheckMode {
988 DefaultBlock => hir::DefaultBlock,
989 UnsafeBlock(u) => hir::UnsafeBlock(lower_unsafe_source(u)),
990 PushUnsafeBlock(u) => hir::PushUnsafeBlock(lower_unsafe_source(u)),
991 PopUnsafeBlock(u) => hir::PopUnsafeBlock(lower_unsafe_source(u)),
995 pub fn lower_pat_wild_kind(p: PatWildKind) -> hir::PatWildKind {
997 PatWildSingle => hir::PatWildSingle,
998 PatWildMulti => hir::PatWildMulti,
1002 pub fn lower_binding_mode(b: &BindingMode) -> hir::BindingMode {
1004 BindByRef(m) => hir::BindByRef(lower_mutability(m)),
1005 BindByValue(m) => hir::BindByValue(lower_mutability(m)),
1009 pub fn lower_struct_field_kind(s: &StructFieldKind) -> hir::StructFieldKind {
1011 NamedField(ident, vis) => hir::NamedField(ident, lower_visibility(vis)),
1012 UnnamedField(vis) => hir::UnnamedField(lower_visibility(vis)),
1016 pub fn lower_unsafe_source(u: UnsafeSource) -> hir::UnsafeSource {
1018 CompilerGenerated => hir::CompilerGenerated,
1019 UserProvided => hir::UserProvided,
1023 pub fn lower_impl_polarity(i: ImplPolarity) -> hir::ImplPolarity {
1025 ImplPolarity::Positive => hir::ImplPolarity::Positive,
1026 ImplPolarity::Negative => hir::ImplPolarity::Negative,
1030 pub fn lower_float_ty(f: FloatTy) -> hir::FloatTy {
1032 TyF32 => hir::TyF32,
1033 TyF64 => hir::TyF64,
1037 pub fn unlower_float_ty(f: hir::FloatTy) -> FloatTy {
1039 hir::TyF32 => TyF32,
1040 hir::TyF64 => TyF64,
1044 pub fn lower_lit_int_type(i: &LitIntType) -> hir::LitIntType {
1046 SignedIntLit(i, s) => hir::SignedIntLit(lower_int_ty(i), lower_sign(s)),
1047 UnsignedIntLit(u) => hir::UnsignedIntLit(lower_uint_ty(u)),
1048 UnsuffixedIntLit(s) => hir::UnsuffixedIntLit(lower_sign(s)),
1052 pub fn unlower_lit_int_type(i: &hir::LitIntType) -> LitIntType {
1054 hir::SignedIntLit(i, s) => SignedIntLit(unlower_int_ty(i), unlower_sign(s)),
1055 hir::UnsignedIntLit(u) => UnsignedIntLit(unlower_uint_ty(u)),
1056 hir::UnsuffixedIntLit(s) => UnsuffixedIntLit(unlower_sign(s)),
1060 pub fn lower_int_ty(i: IntTy) -> hir::IntTy {
1064 TyI16 => hir::TyI16,
1065 TyI32 => hir::TyI32,
1066 TyI64 => hir::TyI64,
1070 pub fn unlower_int_ty(i: hir::IntTy) -> IntTy {
1074 hir::TyI16 => TyI16,
1075 hir::TyI32 => TyI32,
1076 hir::TyI64 => TyI64,
1080 pub fn lower_uint_ty(u: UintTy) -> hir::UintTy {
1084 TyU16 => hir::TyU16,
1085 TyU32 => hir::TyU32,
1086 TyU64 => hir::TyU64,
1090 pub fn unlower_uint_ty(u: hir::UintTy) -> UintTy {
1094 hir::TyU16 => TyU16,
1095 hir::TyU32 => TyU32,
1096 hir::TyU64 => TyU64,
1100 pub fn lower_sign(f: Sign) -> hir::Sign {
1102 Minus => hir::Minus,
1107 pub fn unlower_sign(f: hir::Sign) -> Sign {
1109 hir::Minus => Minus,
1114 pub fn lower_trait_bound_modifier(f: TraitBoundModifier) -> hir::TraitBoundModifier {
1116 TraitBoundModifier::None => hir::TraitBoundModifier::None,
1117 TraitBoundModifier::Maybe => hir::TraitBoundModifier::Maybe,
1121 pub fn lower_attr_style(f: AttrStyle) -> hir::AttrStyle {
1123 AttrOuter => hir::AttrOuter,
1124 AttrInner => hir::AttrInner,
1128 pub fn unlower_attr_style(f: hir::AttrStyle) -> AttrStyle {
1130 hir::AttrOuter => AttrOuter,
1131 hir::AttrInner => AttrInner,