2015-07-31 07:04:06 +00:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-09-30 05:23:52 +00:00
|
|
|
// Lowers the AST to the HIR.
|
|
|
|
//
|
|
|
|
// Since the AST and HIR are fairly similar, this is mostly a simple procedure,
|
|
|
|
// much like a fold. Where lowering involves a bit more work things get more
|
|
|
|
// interesting and there are some invariants you should know about. These mostly
|
|
|
|
// concern spans and ids.
|
|
|
|
//
|
|
|
|
// Spans are assigned to AST nodes during parsing and then are modified during
|
|
|
|
// expansion to indicate the origin of a node and the process it went through
|
|
|
|
// being expanded. Ids are assigned to AST nodes just before lowering.
|
|
|
|
//
|
|
|
|
// For the simpler lowering steps, ids and spans should be preserved. Unlike
|
|
|
|
// expansion we do not preserve the process of lowering in the spans, so spans
|
|
|
|
// should not be modified here. When creating a new node (as opposed to
|
|
|
|
// 'folding' an existing one), then you create a new id using `next_id()`.
|
|
|
|
//
|
|
|
|
// You must ensure that ids are unique. That means that you should only use the
|
2015-10-06 19:26:22 +00:00
|
|
|
// id from an AST node in a single HIR node (you can assume that AST node ids
|
2015-09-30 05:23:52 +00:00
|
|
|
// are unique). Every new node must have a unique id. Avoid cloning HIR nodes.
|
2015-10-06 19:26:22 +00:00
|
|
|
// If you do, you must then set the new node's id to a fresh one.
|
2015-09-30 05:23:52 +00:00
|
|
|
//
|
|
|
|
// Lowering must be reproducable (the compiler only lowers once, but tools and
|
|
|
|
// custom lints may lower an AST node to a HIR node to interact with the
|
2015-10-06 19:26:22 +00:00
|
|
|
// compiler). The most interesting bit of this is ids - if you lower an AST node
|
2015-09-30 05:23:52 +00:00
|
|
|
// and create new HIR nodes with fresh ids, when re-lowering the same node, you
|
|
|
|
// must ensure you get the same ids! To do this, we keep track of the next id
|
|
|
|
// when we translate a node which requires new ids. By checking this cache and
|
|
|
|
// using node ids starting with the cached id, we ensure ids are reproducible.
|
|
|
|
// To use this system, you just need to hold on to a CachedIdSetter object
|
|
|
|
// whilst lowering. This is an RAII object that takes care of setting and
|
|
|
|
// restoring the cached id, etc.
|
|
|
|
//
|
|
|
|
// This whole system relies on node ids being incremented one at a time and
|
|
|
|
// all increments being for lowering. This means that you should not call any
|
|
|
|
// non-lowering function which will use new node ids.
|
|
|
|
//
|
2015-10-06 19:26:22 +00:00
|
|
|
// We must also cache gensym'ed Idents to ensure that we get the same Ident
|
|
|
|
// every time we lower a node with gensym'ed names. One consequence of this is
|
|
|
|
// that you can only gensym a name once in a lowering (you don't need to worry
|
|
|
|
// about nested lowering though). That's because we cache based on the name and
|
|
|
|
// the currently cached node id, which is unique per lowered node.
|
|
|
|
//
|
2015-09-30 05:23:52 +00:00
|
|
|
// Spans are used for error messages and for tools to map semantics back to
|
|
|
|
// source code. It is therefore not as important with spans as ids to be strict
|
|
|
|
// about use (you can't break the compiler by screwing up a span). Obviously, a
|
|
|
|
// HIR node can only have a single span. But multiple nodes can have the same
|
|
|
|
// span and spans don't need to be kept in order, etc. Where code is preserved
|
|
|
|
// by lowering, it should have the same span as in the AST. Where HIR nodes are
|
|
|
|
// new it is probably best to give a span for the whole AST node being lowered.
|
|
|
|
// All nodes should have real spans, don't use dummy spans. Tools are likely to
|
|
|
|
// get confused if the spans from leaf AST nodes occur in multiple places
|
|
|
|
// in the HIR, especially for multiple identifiers.
|
2015-07-31 07:04:06 +00:00
|
|
|
|
|
|
|
use hir;
|
|
|
|
|
2015-11-18 09:16:25 +00:00
|
|
|
use std::collections::BTreeMap;
|
2015-09-30 03:17:37 +00:00
|
|
|
use std::collections::HashMap;
|
2015-07-31 07:04:06 +00:00
|
|
|
use syntax::ast::*;
|
|
|
|
use syntax::ptr::P;
|
2015-09-28 02:00:15 +00:00
|
|
|
use syntax::codemap::{respan, Spanned, Span};
|
2015-07-31 07:04:06 +00:00
|
|
|
use syntax::owned_slice::OwnedSlice;
|
2015-09-28 02:00:15 +00:00
|
|
|
use syntax::parse::token::{self, str_to_ident};
|
|
|
|
use syntax::std_inject;
|
2015-11-17 22:32:12 +00:00
|
|
|
use syntax::visit::{self, Visitor};
|
2015-07-31 07:04:06 +00:00
|
|
|
|
2015-09-30 03:17:37 +00:00
|
|
|
use std::cell::{Cell, RefCell};
|
|
|
|
|
|
|
|
pub struct LoweringContext<'a> {
|
2015-09-28 02:00:15 +00:00
|
|
|
crate_root: Option<&'static str>,
|
2015-09-30 05:23:52 +00:00
|
|
|
// Map AST ids to ids used for expanded nodes.
|
2015-09-30 03:17:37 +00:00
|
|
|
id_cache: RefCell<HashMap<NodeId, NodeId>>,
|
2015-09-30 05:23:52 +00:00
|
|
|
// Use if there are no cached ids for the current node.
|
2015-09-30 03:17:37 +00:00
|
|
|
id_assigner: &'a NodeIdAssigner,
|
2015-09-30 05:23:52 +00:00
|
|
|
// 0 == no cached id. Must be incremented to align with previous id
|
|
|
|
// incrementing.
|
2015-09-30 03:17:37 +00:00
|
|
|
cached_id: Cell<u32>,
|
2015-10-06 19:26:22 +00:00
|
|
|
// Keep track of gensym'ed idents.
|
|
|
|
gensym_cache: RefCell<HashMap<(NodeId, &'static str), Ident>>,
|
|
|
|
// A copy of cached_id, but is also set to an id while it is being cached.
|
|
|
|
gensym_key: Cell<u32>,
|
2015-09-25 04:03:28 +00:00
|
|
|
}
|
|
|
|
|
2015-09-30 03:17:37 +00:00
|
|
|
impl<'a, 'hir> LoweringContext<'a> {
|
2015-10-06 02:31:43 +00:00
|
|
|
pub fn new(id_assigner: &'a NodeIdAssigner, c: Option<&Crate>) -> LoweringContext<'a> {
|
|
|
|
let crate_root = c.and_then(|c| {
|
|
|
|
if std_inject::no_core(c) {
|
|
|
|
None
|
|
|
|
} else if std_inject::no_std(c) {
|
|
|
|
Some("core")
|
|
|
|
} else {
|
|
|
|
Some("std")
|
|
|
|
}
|
|
|
|
});
|
2015-09-28 02:00:15 +00:00
|
|
|
|
2015-09-25 04:03:28 +00:00
|
|
|
LoweringContext {
|
2015-09-28 02:00:15 +00:00
|
|
|
crate_root: crate_root,
|
2015-09-30 03:17:37 +00:00
|
|
|
id_cache: RefCell::new(HashMap::new()),
|
|
|
|
id_assigner: id_assigner,
|
|
|
|
cached_id: Cell::new(0),
|
2015-10-06 19:26:22 +00:00
|
|
|
gensym_cache: RefCell::new(HashMap::new()),
|
|
|
|
gensym_key: Cell::new(0),
|
2015-09-25 04:03:28 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-28 02:00:15 +00:00
|
|
|
|
|
|
|
fn next_id(&self) -> NodeId {
|
2015-09-30 03:17:37 +00:00
|
|
|
let cached = self.cached_id.get();
|
|
|
|
if cached == 0 {
|
2015-11-05 21:17:59 +00:00
|
|
|
return self.id_assigner.next_node_id();
|
2015-09-30 03:17:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.cached_id.set(cached + 1);
|
|
|
|
cached
|
2015-09-28 02:00:15 +00:00
|
|
|
}
|
2015-10-06 19:26:22 +00:00
|
|
|
|
|
|
|
fn str_to_ident(&self, s: &'static str) -> Ident {
|
|
|
|
let cached_id = self.gensym_key.get();
|
|
|
|
if cached_id == 0 {
|
|
|
|
return token::gensym_ident(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
let cached = self.gensym_cache.borrow().contains_key(&(cached_id, s));
|
|
|
|
if cached {
|
|
|
|
self.gensym_cache.borrow()[&(cached_id, s)]
|
|
|
|
} else {
|
|
|
|
let result = token::gensym_ident(s);
|
|
|
|
self.gensym_cache.borrow_mut().insert((cached_id, s), result);
|
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
2015-09-25 04:03:28 +00:00
|
|
|
}
|
2015-07-31 07:04:06 +00:00
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_view_path(lctx: &LoweringContext, view_path: &ViewPath) -> P<hir::ViewPath> {
|
2015-07-31 07:04:06 +00:00
|
|
|
P(Spanned {
|
|
|
|
node: match view_path.node {
|
|
|
|
ViewPathSimple(ident, ref path) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::ViewPathSimple(ident.name, lower_path(lctx, path))
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
ViewPathGlob(ref path) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::ViewPathGlob(lower_path(lctx, path))
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
ViewPathList(ref path, ref path_list_idents) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::ViewPathList(lower_path(lctx, path),
|
2015-10-06 03:03:56 +00:00
|
|
|
path_list_idents.iter()
|
|
|
|
.map(|path_list_ident| {
|
|
|
|
Spanned {
|
|
|
|
node: match path_list_ident.node {
|
|
|
|
PathListIdent { id, name, rename } =>
|
|
|
|
hir::PathListIdent {
|
|
|
|
id: id,
|
|
|
|
name: name.name,
|
|
|
|
rename: rename.map(|x| x.name),
|
|
|
|
},
|
|
|
|
PathListMod { id, rename } =>
|
|
|
|
hir::PathListMod {
|
|
|
|
id: id,
|
|
|
|
rename: rename.map(|x| x.name),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
span: path_list_ident.span,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect())
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
span: view_path.span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_arm(lctx: &LoweringContext, arm: &Arm) -> hir::Arm {
|
2015-07-31 07:04:06 +00:00
|
|
|
hir::Arm {
|
2015-09-14 09:58:20 +00:00
|
|
|
attrs: arm.attrs.clone(),
|
2015-11-12 17:30:52 +00:00
|
|
|
pats: arm.pats.iter().map(|x| lower_pat(lctx, x)).collect(),
|
|
|
|
guard: arm.guard.as_ref().map(|ref x| lower_expr(lctx, x)),
|
|
|
|
body: lower_expr(lctx, &arm.body),
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_decl(lctx: &LoweringContext, d: &Decl) -> P<hir::Decl> {
|
2015-07-31 07:04:06 +00:00
|
|
|
match d.node {
|
|
|
|
DeclLocal(ref l) => P(Spanned {
|
2015-11-12 17:30:52 +00:00
|
|
|
node: hir::DeclLocal(lower_local(lctx, l)),
|
2015-10-06 03:03:56 +00:00
|
|
|
span: d.span,
|
2015-07-31 07:04:06 +00:00
|
|
|
}),
|
|
|
|
DeclItem(ref it) => P(Spanned {
|
2015-11-17 22:32:12 +00:00
|
|
|
node: hir::DeclItem(lower_item_id(lctx, it)),
|
2015-10-06 03:03:56 +00:00
|
|
|
span: d.span,
|
2015-07-31 07:04:06 +00:00
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_ty_binding(lctx: &LoweringContext, b: &TypeBinding) -> P<hir::TypeBinding> {
|
2015-10-06 03:03:56 +00:00
|
|
|
P(hir::TypeBinding {
|
|
|
|
id: b.id,
|
|
|
|
name: b.ident.name,
|
2015-11-12 17:30:52 +00:00
|
|
|
ty: lower_ty(lctx, &b.ty),
|
2015-10-06 03:03:56 +00:00
|
|
|
span: b.span,
|
|
|
|
})
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_ty(lctx: &LoweringContext, t: &Ty) -> P<hir::Ty> {
|
2015-07-31 07:04:06 +00:00
|
|
|
P(hir::Ty {
|
|
|
|
id: t.id,
|
|
|
|
node: match t.node {
|
|
|
|
TyInfer => hir::TyInfer,
|
2015-11-12 17:30:52 +00:00
|
|
|
TyVec(ref ty) => hir::TyVec(lower_ty(lctx, ty)),
|
|
|
|
TyPtr(ref mt) => hir::TyPtr(lower_mt(lctx, mt)),
|
2015-07-31 07:04:06 +00:00
|
|
|
TyRptr(ref region, ref mt) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::TyRptr(lower_opt_lifetime(lctx, region), lower_mt(lctx, mt))
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
TyBareFn(ref f) => {
|
|
|
|
hir::TyBareFn(P(hir::BareFnTy {
|
2015-11-12 17:30:52 +00:00
|
|
|
lifetimes: lower_lifetime_defs(lctx, &f.lifetimes),
|
|
|
|
unsafety: lower_unsafety(lctx, f.unsafety),
|
2015-07-31 07:04:06 +00:00
|
|
|
abi: f.abi,
|
2015-11-12 17:30:52 +00:00
|
|
|
decl: lower_fn_decl(lctx, &f.decl),
|
2015-07-31 07:04:06 +00:00
|
|
|
}))
|
|
|
|
}
|
2015-11-12 17:30:52 +00:00
|
|
|
TyTup(ref tys) => hir::TyTup(tys.iter().map(|ty| lower_ty(lctx, ty)).collect()),
|
2015-11-16 19:49:47 +00:00
|
|
|
TyParen(ref ty) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
return lower_ty(lctx, ty);
|
2015-11-16 19:49:47 +00:00
|
|
|
}
|
2015-07-31 07:04:06 +00:00
|
|
|
TyPath(ref qself, ref path) => {
|
|
|
|
let qself = qself.as_ref().map(|&QSelf { ref ty, position }| {
|
|
|
|
hir::QSelf {
|
2015-11-12 17:30:52 +00:00
|
|
|
ty: lower_ty(lctx, ty),
|
2015-09-27 19:23:31 +00:00
|
|
|
position: position,
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
});
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::TyPath(qself, lower_path(lctx, path))
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
TyObjectSum(ref ty, ref bounds) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::TyObjectSum(lower_ty(lctx, ty), lower_bounds(lctx, bounds))
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
TyFixedLengthVec(ref ty, ref e) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::TyFixedLengthVec(lower_ty(lctx, ty), lower_expr(lctx, e))
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
TyTypeof(ref expr) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::TyTypeof(lower_expr(lctx, expr))
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
TyPolyTraitRef(ref bounds) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::TyPolyTraitRef(bounds.iter().map(|b| lower_ty_param_bound(lctx, b)).collect())
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
TyMac(_) => panic!("TyMac should have been expanded by now."),
|
|
|
|
},
|
|
|
|
span: t.span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_foreign_mod(lctx: &LoweringContext, fm: &ForeignMod) -> hir::ForeignMod {
|
2015-07-31 07:04:06 +00:00
|
|
|
hir::ForeignMod {
|
|
|
|
abi: fm.abi,
|
2015-11-12 17:30:52 +00:00
|
|
|
items: fm.items.iter().map(|x| lower_foreign_item(lctx, x)).collect(),
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_variant(lctx: &LoweringContext, v: &Variant) -> P<hir::Variant> {
|
2015-07-31 07:04:06 +00:00
|
|
|
P(Spanned {
|
|
|
|
node: hir::Variant_ {
|
2015-09-20 13:47:24 +00:00
|
|
|
name: v.node.name.name,
|
2015-09-14 09:58:20 +00:00
|
|
|
attrs: v.node.attrs.clone(),
|
2015-11-12 17:30:52 +00:00
|
|
|
data: lower_variant_data(lctx, &v.node.data),
|
|
|
|
disr_expr: v.node.disr_expr.as_ref().map(|e| lower_expr(lctx, e)),
|
2015-07-31 07:04:06 +00:00
|
|
|
},
|
|
|
|
span: v.span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_path(lctx: &LoweringContext, p: &Path) -> hir::Path {
|
2015-07-31 07:04:06 +00:00
|
|
|
hir::Path {
|
|
|
|
global: p.global,
|
2015-10-06 03:03:56 +00:00
|
|
|
segments: p.segments
|
|
|
|
.iter()
|
|
|
|
.map(|&PathSegment { identifier, ref parameters }| {
|
|
|
|
hir::PathSegment {
|
|
|
|
identifier: identifier,
|
2015-11-12 17:30:52 +00:00
|
|
|
parameters: lower_path_parameters(lctx, parameters),
|
2015-10-06 03:03:56 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect(),
|
2015-07-31 07:04:06 +00:00
|
|
|
span: p.span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_path_parameters(lctx: &LoweringContext,
|
2015-09-25 04:03:28 +00:00
|
|
|
path_parameters: &PathParameters)
|
|
|
|
-> hir::PathParameters {
|
2015-07-31 07:04:06 +00:00
|
|
|
match *path_parameters {
|
|
|
|
AngleBracketedParameters(ref data) =>
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::AngleBracketedParameters(lower_angle_bracketed_parameter_data(lctx, data)),
|
2015-07-31 07:04:06 +00:00
|
|
|
ParenthesizedParameters(ref data) =>
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::ParenthesizedParameters(lower_parenthesized_parameter_data(lctx, data)),
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_angle_bracketed_parameter_data(lctx: &LoweringContext,
|
2015-09-25 04:03:28 +00:00
|
|
|
data: &AngleBracketedParameterData)
|
2015-07-31 07:04:06 +00:00
|
|
|
-> hir::AngleBracketedParameterData {
|
|
|
|
let &AngleBracketedParameterData { ref lifetimes, ref types, ref bindings } = data;
|
|
|
|
hir::AngleBracketedParameterData {
|
2015-11-12 17:30:52 +00:00
|
|
|
lifetimes: lower_lifetimes(lctx, lifetimes),
|
|
|
|
types: types.iter().map(|ty| lower_ty(lctx, ty)).collect(),
|
|
|
|
bindings: bindings.iter().map(|b| lower_ty_binding(lctx, b)).collect(),
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_parenthesized_parameter_data(lctx: &LoweringContext,
|
2015-09-25 04:03:28 +00:00
|
|
|
data: &ParenthesizedParameterData)
|
2015-07-31 07:04:06 +00:00
|
|
|
-> hir::ParenthesizedParameterData {
|
|
|
|
let &ParenthesizedParameterData { ref inputs, ref output, span } = data;
|
|
|
|
hir::ParenthesizedParameterData {
|
2015-11-12 17:30:52 +00:00
|
|
|
inputs: inputs.iter().map(|ty| lower_ty(lctx, ty)).collect(),
|
|
|
|
output: output.as_ref().map(|ty| lower_ty(lctx, ty)),
|
2015-07-31 07:04:06 +00:00
|
|
|
span: span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_local(lctx: &LoweringContext, l: &Local) -> P<hir::Local> {
|
2015-07-31 07:04:06 +00:00
|
|
|
P(hir::Local {
|
2015-10-06 03:03:56 +00:00
|
|
|
id: l.id,
|
2015-11-12 17:30:52 +00:00
|
|
|
ty: l.ty.as_ref().map(|t| lower_ty(lctx, t)),
|
|
|
|
pat: lower_pat(lctx, &l.pat),
|
|
|
|
init: l.init.as_ref().map(|e| lower_expr(lctx, e)),
|
2015-10-06 03:03:56 +00:00
|
|
|
span: l.span,
|
|
|
|
})
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_explicit_self_underscore(lctx: &LoweringContext,
|
2015-09-25 04:03:28 +00:00
|
|
|
es: &ExplicitSelf_)
|
|
|
|
-> hir::ExplicitSelf_ {
|
2015-07-31 07:04:06 +00:00
|
|
|
match *es {
|
|
|
|
SelfStatic => hir::SelfStatic,
|
2015-09-20 13:47:24 +00:00
|
|
|
SelfValue(v) => hir::SelfValue(v.name),
|
2015-07-31 07:04:06 +00:00
|
|
|
SelfRegion(ref lifetime, m, ident) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::SelfRegion(lower_opt_lifetime(lctx, lifetime),
|
|
|
|
lower_mutability(lctx, m),
|
2015-09-27 19:23:31 +00:00
|
|
|
ident.name)
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
SelfExplicit(ref typ, ident) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::SelfExplicit(lower_ty(lctx, typ), ident.name)
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 04:03:28 +00:00
|
|
|
pub fn lower_mutability(_lctx: &LoweringContext, m: Mutability) -> hir::Mutability {
|
2015-07-31 07:04:06 +00:00
|
|
|
match m {
|
|
|
|
MutMutable => hir::MutMutable,
|
|
|
|
MutImmutable => hir::MutImmutable,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_explicit_self(lctx: &LoweringContext, s: &ExplicitSelf) -> hir::ExplicitSelf {
|
2015-10-06 03:03:56 +00:00
|
|
|
Spanned {
|
2015-11-12 17:30:52 +00:00
|
|
|
node: lower_explicit_self_underscore(lctx, &s.node),
|
2015-10-06 03:03:56 +00:00
|
|
|
span: s.span,
|
|
|
|
}
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_arg(lctx: &LoweringContext, arg: &Arg) -> hir::Arg {
|
2015-10-06 03:03:56 +00:00
|
|
|
hir::Arg {
|
|
|
|
id: arg.id,
|
2015-11-12 17:30:52 +00:00
|
|
|
pat: lower_pat(lctx, &arg.pat),
|
|
|
|
ty: lower_ty(lctx, &arg.ty),
|
2015-10-06 03:03:56 +00:00
|
|
|
}
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_fn_decl(lctx: &LoweringContext, decl: &FnDecl) -> P<hir::FnDecl> {
|
2015-07-31 07:04:06 +00:00
|
|
|
P(hir::FnDecl {
|
2015-11-12 17:30:52 +00:00
|
|
|
inputs: decl.inputs.iter().map(|x| lower_arg(lctx, x)).collect(),
|
2015-07-31 07:04:06 +00:00
|
|
|
output: match decl.output {
|
2015-11-12 17:30:52 +00:00
|
|
|
Return(ref ty) => hir::Return(lower_ty(lctx, ty)),
|
2015-07-31 07:04:06 +00:00
|
|
|
DefaultReturn(span) => hir::DefaultReturn(span),
|
2015-09-27 19:23:31 +00:00
|
|
|
NoReturn(span) => hir::NoReturn(span),
|
2015-07-31 07:04:06 +00:00
|
|
|
},
|
|
|
|
variadic: decl.variadic,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_ty_param_bound(lctx: &LoweringContext, tpb: &TyParamBound) -> hir::TyParamBound {
|
2015-07-31 07:04:06 +00:00
|
|
|
match *tpb {
|
|
|
|
TraitTyParamBound(ref ty, modifier) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::TraitTyParamBound(lower_poly_trait_ref(lctx, ty),
|
|
|
|
lower_trait_bound_modifier(lctx, modifier))
|
2015-09-25 04:03:28 +00:00
|
|
|
}
|
|
|
|
RegionTyParamBound(ref lifetime) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::RegionTyParamBound(lower_lifetime(lctx, lifetime))
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_ty_param(lctx: &LoweringContext, tp: &TyParam) -> hir::TyParam {
|
2015-07-31 07:04:06 +00:00
|
|
|
hir::TyParam {
|
|
|
|
id: tp.id,
|
2015-09-20 13:47:24 +00:00
|
|
|
name: tp.ident.name,
|
2015-11-12 17:30:52 +00:00
|
|
|
bounds: lower_bounds(lctx, &tp.bounds),
|
|
|
|
default: tp.default.as_ref().map(|x| lower_ty(lctx, x)),
|
2015-07-31 07:04:06 +00:00
|
|
|
span: tp.span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_ty_params(lctx: &LoweringContext,
|
2015-09-25 04:03:28 +00:00
|
|
|
tps: &OwnedSlice<TyParam>)
|
|
|
|
-> OwnedSlice<hir::TyParam> {
|
2015-11-12 17:30:52 +00:00
|
|
|
tps.iter().map(|tp| lower_ty_param(lctx, tp)).collect()
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
|
2015-09-25 04:03:28 +00:00
|
|
|
pub fn lower_lifetime(_lctx: &LoweringContext, l: &Lifetime) -> hir::Lifetime {
|
2015-10-06 03:03:56 +00:00
|
|
|
hir::Lifetime {
|
|
|
|
id: l.id,
|
|
|
|
name: l.name,
|
|
|
|
span: l.span,
|
|
|
|
}
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_lifetime_def(lctx: &LoweringContext, l: &LifetimeDef) -> hir::LifetimeDef {
|
2015-09-27 19:23:31 +00:00
|
|
|
hir::LifetimeDef {
|
2015-11-12 17:30:52 +00:00
|
|
|
lifetime: lower_lifetime(lctx, &l.lifetime),
|
|
|
|
bounds: lower_lifetimes(lctx, &l.bounds),
|
2015-09-27 19:23:31 +00:00
|
|
|
}
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_lifetimes(lctx: &LoweringContext, lts: &Vec<Lifetime>) -> Vec<hir::Lifetime> {
|
|
|
|
lts.iter().map(|l| lower_lifetime(lctx, l)).collect()
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_lifetime_defs(lctx: &LoweringContext,
|
2015-09-25 04:03:28 +00:00
|
|
|
lts: &Vec<LifetimeDef>)
|
|
|
|
-> Vec<hir::LifetimeDef> {
|
2015-11-12 17:30:52 +00:00
|
|
|
lts.iter().map(|l| lower_lifetime_def(lctx, l)).collect()
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_opt_lifetime(lctx: &LoweringContext,
|
2015-09-25 04:03:28 +00:00
|
|
|
o_lt: &Option<Lifetime>)
|
|
|
|
-> Option<hir::Lifetime> {
|
2015-11-12 17:30:52 +00:00
|
|
|
o_lt.as_ref().map(|lt| lower_lifetime(lctx, lt))
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_generics(lctx: &LoweringContext, g: &Generics) -> hir::Generics {
|
2015-07-31 07:04:06 +00:00
|
|
|
hir::Generics {
|
2015-11-12 17:30:52 +00:00
|
|
|
ty_params: lower_ty_params(lctx, &g.ty_params),
|
|
|
|
lifetimes: lower_lifetime_defs(lctx, &g.lifetimes),
|
|
|
|
where_clause: lower_where_clause(lctx, &g.where_clause),
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_where_clause(lctx: &LoweringContext, wc: &WhereClause) -> hir::WhereClause {
|
2015-07-31 07:04:06 +00:00
|
|
|
hir::WhereClause {
|
|
|
|
id: wc.id,
|
2015-10-06 03:03:56 +00:00
|
|
|
predicates: wc.predicates
|
|
|
|
.iter()
|
2015-11-12 17:30:52 +00:00
|
|
|
.map(|predicate| lower_where_predicate(lctx, predicate))
|
2015-10-06 03:03:56 +00:00
|
|
|
.collect(),
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_where_predicate(lctx: &LoweringContext,
|
2015-09-25 04:03:28 +00:00
|
|
|
pred: &WherePredicate)
|
|
|
|
-> hir::WherePredicate {
|
2015-07-31 07:04:06 +00:00
|
|
|
match *pred {
|
|
|
|
WherePredicate::BoundPredicate(WhereBoundPredicate{ ref bound_lifetimes,
|
|
|
|
ref bounded_ty,
|
|
|
|
ref bounds,
|
|
|
|
span}) => {
|
|
|
|
hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
|
2015-11-12 17:30:52 +00:00
|
|
|
bound_lifetimes: lower_lifetime_defs(lctx, bound_lifetimes),
|
|
|
|
bounded_ty: lower_ty(lctx, bounded_ty),
|
|
|
|
bounds: bounds.iter().map(|x| lower_ty_param_bound(lctx, x)).collect(),
|
2015-10-06 03:03:56 +00:00
|
|
|
span: span,
|
2015-07-31 07:04:06 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
WherePredicate::RegionPredicate(WhereRegionPredicate{ ref lifetime,
|
|
|
|
ref bounds,
|
|
|
|
span}) => {
|
|
|
|
hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
|
|
|
|
span: span,
|
2015-11-12 17:30:52 +00:00
|
|
|
lifetime: lower_lifetime(lctx, lifetime),
|
|
|
|
bounds: bounds.iter().map(|bound| lower_lifetime(lctx, bound)).collect(),
|
2015-07-31 07:04:06 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
WherePredicate::EqPredicate(WhereEqPredicate{ id,
|
|
|
|
ref path,
|
|
|
|
ref ty,
|
|
|
|
span}) => {
|
2015-09-27 19:23:31 +00:00
|
|
|
hir::WherePredicate::EqPredicate(hir::WhereEqPredicate {
|
2015-07-31 07:04:06 +00:00
|
|
|
id: id,
|
2015-11-12 17:30:52 +00:00
|
|
|
path: lower_path(lctx, path),
|
|
|
|
ty: lower_ty(lctx, ty),
|
2015-10-06 03:03:56 +00:00
|
|
|
span: span,
|
2015-07-31 07:04:06 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_variant_data(lctx: &LoweringContext, vdata: &VariantData) -> hir::VariantData {
|
2015-10-25 15:33:51 +00:00
|
|
|
match *vdata {
|
2015-10-10 00:28:40 +00:00
|
|
|
VariantData::Struct(ref fields, id) => {
|
|
|
|
hir::VariantData::Struct(fields.iter()
|
2015-11-12 17:30:52 +00:00
|
|
|
.map(|f| lower_struct_field(lctx, f))
|
2015-11-05 21:17:59 +00:00
|
|
|
.collect(),
|
|
|
|
id)
|
2015-10-10 00:28:40 +00:00
|
|
|
}
|
|
|
|
VariantData::Tuple(ref fields, id) => {
|
|
|
|
hir::VariantData::Tuple(fields.iter()
|
2015-11-12 17:30:52 +00:00
|
|
|
.map(|f| lower_struct_field(lctx, f))
|
2015-11-05 21:17:59 +00:00
|
|
|
.collect(),
|
|
|
|
id)
|
2015-10-02 00:53:28 +00:00
|
|
|
}
|
2015-11-05 21:17:59 +00:00
|
|
|
VariantData::Unit(id) => hir::VariantData::Unit(id),
|
2015-10-25 15:33:51 +00:00
|
|
|
}
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_trait_ref(lctx: &LoweringContext, p: &TraitRef) -> hir::TraitRef {
|
2015-10-06 03:03:56 +00:00
|
|
|
hir::TraitRef {
|
2015-11-12 17:30:52 +00:00
|
|
|
path: lower_path(lctx, &p.path),
|
2015-10-06 03:03:56 +00:00
|
|
|
ref_id: p.ref_id,
|
|
|
|
}
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_poly_trait_ref(lctx: &LoweringContext, p: &PolyTraitRef) -> hir::PolyTraitRef {
|
2015-07-31 07:04:06 +00:00
|
|
|
hir::PolyTraitRef {
|
2015-11-12 17:30:52 +00:00
|
|
|
bound_lifetimes: lower_lifetime_defs(lctx, &p.bound_lifetimes),
|
|
|
|
trait_ref: lower_trait_ref(lctx, &p.trait_ref),
|
2015-07-31 07:04:06 +00:00
|
|
|
span: p.span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_struct_field(lctx: &LoweringContext, f: &StructField) -> hir::StructField {
|
2015-07-31 07:04:06 +00:00
|
|
|
Spanned {
|
|
|
|
node: hir::StructField_ {
|
|
|
|
id: f.node.id,
|
2015-11-12 17:30:52 +00:00
|
|
|
kind: lower_struct_field_kind(lctx, &f.node.kind),
|
|
|
|
ty: lower_ty(lctx, &f.node.ty),
|
2015-09-14 09:58:20 +00:00
|
|
|
attrs: f.node.attrs.clone(),
|
2015-07-31 07:04:06 +00:00
|
|
|
},
|
|
|
|
span: f.span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_field(lctx: &LoweringContext, f: &Field) -> hir::Field {
|
2015-09-20 11:00:18 +00:00
|
|
|
hir::Field {
|
|
|
|
name: respan(f.ident.span, f.ident.node.name),
|
2015-11-12 17:30:52 +00:00
|
|
|
expr: lower_expr(lctx, &f.expr),
|
2015-10-06 03:03:56 +00:00
|
|
|
span: f.span,
|
2015-09-20 11:00:18 +00:00
|
|
|
}
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_mt(lctx: &LoweringContext, mt: &MutTy) -> hir::MutTy {
|
2015-10-06 03:03:56 +00:00
|
|
|
hir::MutTy {
|
2015-11-12 17:30:52 +00:00
|
|
|
ty: lower_ty(lctx, &mt.ty),
|
|
|
|
mutbl: lower_mutability(lctx, mt.mutbl),
|
2015-10-06 03:03:56 +00:00
|
|
|
}
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_opt_bounds(lctx: &LoweringContext,
|
2015-10-06 03:03:56 +00:00
|
|
|
b: &Option<OwnedSlice<TyParamBound>>)
|
2015-07-31 07:04:06 +00:00
|
|
|
-> Option<OwnedSlice<hir::TyParamBound>> {
|
2015-11-12 17:30:52 +00:00
|
|
|
b.as_ref().map(|ref bounds| lower_bounds(lctx, bounds))
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
fn lower_bounds(lctx: &LoweringContext, bounds: &TyParamBounds) -> hir::TyParamBounds {
|
|
|
|
bounds.iter().map(|bound| lower_ty_param_bound(lctx, bound)).collect()
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_block(lctx: &LoweringContext, b: &Block) -> P<hir::Block> {
|
2015-07-31 07:04:06 +00:00
|
|
|
P(hir::Block {
|
|
|
|
id: b.id,
|
2015-11-12 17:30:52 +00:00
|
|
|
stmts: b.stmts.iter().map(|s| lower_stmt(lctx, s)).collect(),
|
|
|
|
expr: b.expr.as_ref().map(|ref x| lower_expr(lctx, x)),
|
|
|
|
rules: lower_block_check_mode(lctx, &b.rules),
|
2015-07-31 07:04:06 +00:00
|
|
|
span: b.span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_item_underscore(lctx: &LoweringContext, i: &Item_) -> hir::Item_ {
|
2015-07-31 07:04:06 +00:00
|
|
|
match *i {
|
|
|
|
ItemExternCrate(string) => hir::ItemExternCrate(string),
|
|
|
|
ItemUse(ref view_path) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::ItemUse(lower_view_path(lctx, view_path))
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
ItemStatic(ref t, m, ref e) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::ItemStatic(lower_ty(lctx, t),
|
|
|
|
lower_mutability(lctx, m),
|
|
|
|
lower_expr(lctx, e))
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
ItemConst(ref t, ref e) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::ItemConst(lower_ty(lctx, t), lower_expr(lctx, e))
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
ItemFn(ref decl, unsafety, constness, abi, ref generics, ref body) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::ItemFn(lower_fn_decl(lctx, decl),
|
|
|
|
lower_unsafety(lctx, unsafety),
|
|
|
|
lower_constness(lctx, constness),
|
2015-10-06 03:03:56 +00:00
|
|
|
abi,
|
2015-11-12 17:30:52 +00:00
|
|
|
lower_generics(lctx, generics),
|
|
|
|
lower_block(lctx, body))
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
2015-11-12 17:30:52 +00:00
|
|
|
ItemMod(ref m) => hir::ItemMod(lower_mod(lctx, m)),
|
|
|
|
ItemForeignMod(ref nm) => hir::ItemForeignMod(lower_foreign_mod(lctx, nm)),
|
2015-07-31 07:04:06 +00:00
|
|
|
ItemTy(ref t, ref generics) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::ItemTy(lower_ty(lctx, t), lower_generics(lctx, generics))
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
ItemEnum(ref enum_definition, ref generics) => {
|
2015-10-06 03:03:56 +00:00
|
|
|
hir::ItemEnum(hir::EnumDef {
|
|
|
|
variants: enum_definition.variants
|
|
|
|
.iter()
|
2015-11-12 17:30:52 +00:00
|
|
|
.map(|x| lower_variant(lctx, x))
|
2015-10-06 03:03:56 +00:00
|
|
|
.collect(),
|
|
|
|
},
|
2015-11-12 17:30:52 +00:00
|
|
|
lower_generics(lctx, generics))
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
ItemStruct(ref struct_def, ref generics) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
let struct_def = lower_variant_data(lctx, struct_def);
|
|
|
|
hir::ItemStruct(struct_def, lower_generics(lctx, generics))
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
ItemDefaultImpl(unsafety, ref trait_ref) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::ItemDefaultImpl(lower_unsafety(lctx, unsafety),
|
|
|
|
lower_trait_ref(lctx, trait_ref))
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
ItemImpl(unsafety, polarity, ref generics, ref ifce, ref ty, ref impl_items) => {
|
2015-10-06 03:03:56 +00:00
|
|
|
let new_impl_items = impl_items.iter()
|
2015-11-12 17:30:52 +00:00
|
|
|
.map(|item| lower_impl_item(lctx, item))
|
2015-10-06 03:03:56 +00:00
|
|
|
.collect();
|
2015-11-12 17:30:52 +00:00
|
|
|
let ifce = ifce.as_ref().map(|trait_ref| lower_trait_ref(lctx, trait_ref));
|
|
|
|
hir::ItemImpl(lower_unsafety(lctx, unsafety),
|
|
|
|
lower_impl_polarity(lctx, polarity),
|
|
|
|
lower_generics(lctx, generics),
|
2015-07-31 07:04:06 +00:00
|
|
|
ifce,
|
2015-11-12 17:30:52 +00:00
|
|
|
lower_ty(lctx, ty),
|
2015-07-31 07:04:06 +00:00
|
|
|
new_impl_items)
|
|
|
|
}
|
|
|
|
ItemTrait(unsafety, ref generics, ref bounds, ref items) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
let bounds = lower_bounds(lctx, bounds);
|
|
|
|
let items = items.iter().map(|item| lower_trait_item(lctx, item)).collect();
|
|
|
|
hir::ItemTrait(lower_unsafety(lctx, unsafety),
|
|
|
|
lower_generics(lctx, generics),
|
2015-07-31 07:04:06 +00:00
|
|
|
bounds,
|
|
|
|
items)
|
|
|
|
}
|
|
|
|
ItemMac(_) => panic!("Shouldn't still be around"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_trait_item(lctx: &LoweringContext, i: &TraitItem) -> P<hir::TraitItem> {
|
2015-07-31 07:04:06 +00:00
|
|
|
P(hir::TraitItem {
|
2015-09-27 19:23:31 +00:00
|
|
|
id: i.id,
|
|
|
|
name: i.ident.name,
|
|
|
|
attrs: i.attrs.clone(),
|
|
|
|
node: match i.node {
|
2015-07-31 07:04:06 +00:00
|
|
|
ConstTraitItem(ref ty, ref default) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::ConstTraitItem(lower_ty(lctx, ty),
|
|
|
|
default.as_ref().map(|x| lower_expr(lctx, x)))
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
MethodTraitItem(ref sig, ref body) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::MethodTraitItem(lower_method_sig(lctx, sig),
|
|
|
|
body.as_ref().map(|x| lower_block(lctx, x)))
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
TypeTraitItem(ref bounds, ref default) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::TypeTraitItem(lower_bounds(lctx, bounds),
|
|
|
|
default.as_ref().map(|x| lower_ty(lctx, x)))
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
},
|
2015-09-27 19:23:31 +00:00
|
|
|
span: i.span,
|
|
|
|
})
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_impl_item(lctx: &LoweringContext, i: &ImplItem) -> P<hir::ImplItem> {
|
2015-07-31 07:04:06 +00:00
|
|
|
P(hir::ImplItem {
|
2015-10-06 03:03:56 +00:00
|
|
|
id: i.id,
|
|
|
|
name: i.ident.name,
|
|
|
|
attrs: i.attrs.clone(),
|
2015-11-12 17:30:52 +00:00
|
|
|
vis: lower_visibility(lctx, i.vis),
|
2015-10-06 03:03:56 +00:00
|
|
|
node: match i.node {
|
2015-11-13 13:15:04 +00:00
|
|
|
ImplItemKind::Const(ref ty, ref expr) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::ImplItemKind::Const(lower_ty(lctx, ty), lower_expr(lctx, expr))
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
2015-11-13 13:15:04 +00:00
|
|
|
ImplItemKind::Method(ref sig, ref body) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::ImplItemKind::Method(lower_method_sig(lctx, sig), lower_block(lctx, body))
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
2015-11-12 17:30:52 +00:00
|
|
|
ImplItemKind::Type(ref ty) => hir::ImplItemKind::Type(lower_ty(lctx, ty)),
|
2015-11-13 13:15:04 +00:00
|
|
|
ImplItemKind::Macro(..) => panic!("Shouldn't exist any more"),
|
2015-07-31 07:04:06 +00:00
|
|
|
},
|
2015-09-27 19:23:31 +00:00
|
|
|
span: i.span,
|
|
|
|
})
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_mod(lctx: &LoweringContext, m: &Mod) -> hir::Mod {
|
2015-10-06 03:03:56 +00:00
|
|
|
hir::Mod {
|
|
|
|
inner: m.inner,
|
2015-11-17 22:32:12 +00:00
|
|
|
item_ids: m.items.iter().map(|x| lower_item_id(lctx, x)).collect(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ItemLowerer<'lcx, 'interner: 'lcx> {
|
2015-11-18 09:16:25 +00:00
|
|
|
items: BTreeMap<NodeId, hir::Item>,
|
2015-11-17 22:32:12 +00:00
|
|
|
lctx: &'lcx LoweringContext<'interner>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'lcx, 'interner> Visitor<'lcx> for ItemLowerer<'lcx, 'interner> {
|
|
|
|
fn visit_item(&mut self, item: &'lcx Item) {
|
|
|
|
self.items.insert(item.id, lower_item(self.lctx, item));
|
|
|
|
visit::walk_item(self, item);
|
2015-10-06 03:03:56 +00:00
|
|
|
}
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_crate(lctx: &LoweringContext, c: &Crate) -> hir::Crate {
|
2015-11-17 22:32:12 +00:00
|
|
|
let items = {
|
2015-11-18 09:16:25 +00:00
|
|
|
let mut item_lowerer = ItemLowerer { items: BTreeMap::new(), lctx: lctx };
|
2015-11-17 22:32:12 +00:00
|
|
|
visit::walk_crate(&mut item_lowerer, c);
|
|
|
|
item_lowerer.items
|
|
|
|
};
|
|
|
|
|
2015-07-31 07:04:06 +00:00
|
|
|
hir::Crate {
|
2015-11-12 17:30:52 +00:00
|
|
|
module: lower_mod(lctx, &c.module),
|
2015-09-14 09:58:20 +00:00
|
|
|
attrs: c.attrs.clone(),
|
|
|
|
config: c.config.clone(),
|
2015-07-31 07:04:06 +00:00
|
|
|
span: c.span,
|
2015-11-12 17:30:52 +00:00
|
|
|
exported_macros: c.exported_macros.iter().map(|m| lower_macro_def(lctx, m)).collect(),
|
2015-11-17 22:32:12 +00:00
|
|
|
items: items,
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 04:03:28 +00:00
|
|
|
pub fn lower_macro_def(_lctx: &LoweringContext, m: &MacroDef) -> hir::MacroDef {
|
2015-07-31 07:04:06 +00:00
|
|
|
hir::MacroDef {
|
2015-09-20 11:51:40 +00:00
|
|
|
name: m.ident.name,
|
2015-09-14 09:58:20 +00:00
|
|
|
attrs: m.attrs.clone(),
|
2015-07-31 07:04:06 +00:00
|
|
|
id: m.id,
|
|
|
|
span: m.span,
|
2015-09-20 11:51:40 +00:00
|
|
|
imported_from: m.imported_from.map(|x| x.name),
|
2015-07-31 07:04:06 +00:00
|
|
|
export: m.export,
|
|
|
|
use_locally: m.use_locally,
|
|
|
|
allow_internal_unstable: m.allow_internal_unstable,
|
|
|
|
body: m.body.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-17 22:32:12 +00:00
|
|
|
pub fn lower_item_id(_lctx: &LoweringContext, i: &Item) -> hir::ItemId {
|
|
|
|
hir::ItemId { id: i.id }
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-17 22:32:12 +00:00
|
|
|
pub fn lower_item(lctx: &LoweringContext, i: &Item) -> hir::Item {
|
2015-11-12 17:30:52 +00:00
|
|
|
let node = lower_item_underscore(lctx, &i.node);
|
2015-07-31 07:04:06 +00:00
|
|
|
|
|
|
|
hir::Item {
|
|
|
|
id: i.id,
|
2015-09-20 01:50:30 +00:00
|
|
|
name: i.ident.name,
|
2015-09-14 09:58:20 +00:00
|
|
|
attrs: i.attrs.clone(),
|
2015-07-31 07:04:06 +00:00
|
|
|
node: node,
|
2015-11-12 17:30:52 +00:00
|
|
|
vis: lower_visibility(lctx, i.vis),
|
2015-07-31 07:04:06 +00:00
|
|
|
span: i.span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_foreign_item(lctx: &LoweringContext, i: &ForeignItem) -> P<hir::ForeignItem> {
|
2015-07-31 07:04:06 +00:00
|
|
|
P(hir::ForeignItem {
|
2015-09-27 19:23:31 +00:00
|
|
|
id: i.id,
|
|
|
|
name: i.ident.name,
|
|
|
|
attrs: i.attrs.clone(),
|
|
|
|
node: match i.node {
|
2015-07-31 07:04:06 +00:00
|
|
|
ForeignItemFn(ref fdec, ref generics) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::ForeignItemFn(lower_fn_decl(lctx, fdec), lower_generics(lctx, generics))
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
ForeignItemStatic(ref t, m) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::ForeignItemStatic(lower_ty(lctx, t), m)
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
},
|
2015-11-12 17:30:52 +00:00
|
|
|
vis: lower_visibility(lctx, i.vis),
|
2015-10-06 03:03:56 +00:00
|
|
|
span: i.span,
|
|
|
|
})
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_method_sig(lctx: &LoweringContext, sig: &MethodSig) -> hir::MethodSig {
|
2015-07-31 07:04:06 +00:00
|
|
|
hir::MethodSig {
|
2015-11-12 17:30:52 +00:00
|
|
|
generics: lower_generics(lctx, &sig.generics),
|
2015-07-31 07:04:06 +00:00
|
|
|
abi: sig.abi,
|
2015-11-12 17:30:52 +00:00
|
|
|
explicit_self: lower_explicit_self(lctx, &sig.explicit_self),
|
|
|
|
unsafety: lower_unsafety(lctx, sig.unsafety),
|
|
|
|
constness: lower_constness(lctx, sig.constness),
|
|
|
|
decl: lower_fn_decl(lctx, &sig.decl),
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 04:03:28 +00:00
|
|
|
pub fn lower_unsafety(_lctx: &LoweringContext, u: Unsafety) -> hir::Unsafety {
|
2015-07-31 07:04:06 +00:00
|
|
|
match u {
|
|
|
|
Unsafety::Unsafe => hir::Unsafety::Unsafe,
|
|
|
|
Unsafety::Normal => hir::Unsafety::Normal,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 04:03:28 +00:00
|
|
|
pub fn lower_constness(_lctx: &LoweringContext, c: Constness) -> hir::Constness {
|
2015-07-31 07:04:06 +00:00
|
|
|
match c {
|
|
|
|
Constness::Const => hir::Constness::Const,
|
|
|
|
Constness::NotConst => hir::Constness::NotConst,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 04:03:28 +00:00
|
|
|
pub fn lower_unop(_lctx: &LoweringContext, u: UnOp) -> hir::UnOp {
|
2015-07-31 07:04:06 +00:00
|
|
|
match u {
|
|
|
|
UnDeref => hir::UnDeref,
|
|
|
|
UnNot => hir::UnNot,
|
|
|
|
UnNeg => hir::UnNeg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 04:03:28 +00:00
|
|
|
pub fn lower_binop(_lctx: &LoweringContext, b: BinOp) -> hir::BinOp {
|
2015-07-31 07:04:06 +00:00
|
|
|
Spanned {
|
|
|
|
node: match b.node {
|
|
|
|
BiAdd => hir::BiAdd,
|
|
|
|
BiSub => hir::BiSub,
|
|
|
|
BiMul => hir::BiMul,
|
|
|
|
BiDiv => hir::BiDiv,
|
|
|
|
BiRem => hir::BiRem,
|
|
|
|
BiAnd => hir::BiAnd,
|
|
|
|
BiOr => hir::BiOr,
|
|
|
|
BiBitXor => hir::BiBitXor,
|
|
|
|
BiBitAnd => hir::BiBitAnd,
|
|
|
|
BiBitOr => hir::BiBitOr,
|
|
|
|
BiShl => hir::BiShl,
|
|
|
|
BiShr => hir::BiShr,
|
|
|
|
BiEq => hir::BiEq,
|
|
|
|
BiLt => hir::BiLt,
|
|
|
|
BiLe => hir::BiLe,
|
|
|
|
BiNe => hir::BiNe,
|
|
|
|
BiGe => hir::BiGe,
|
|
|
|
BiGt => hir::BiGt,
|
|
|
|
},
|
|
|
|
span: b.span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_pat(lctx: &LoweringContext, p: &Pat) -> P<hir::Pat> {
|
2015-07-31 07:04:06 +00:00
|
|
|
P(hir::Pat {
|
2015-10-06 03:03:56 +00:00
|
|
|
id: p.id,
|
|
|
|
node: match p.node {
|
2015-10-31 00:44:43 +00:00
|
|
|
PatWild => hir::PatWild,
|
2015-07-31 07:04:06 +00:00
|
|
|
PatIdent(ref binding_mode, pth1, ref sub) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::PatIdent(lower_binding_mode(lctx, binding_mode),
|
2015-10-06 03:03:56 +00:00
|
|
|
pth1,
|
2015-11-12 17:30:52 +00:00
|
|
|
sub.as_ref().map(|x| lower_pat(lctx, x)))
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
2015-11-12 17:30:52 +00:00
|
|
|
PatLit(ref e) => hir::PatLit(lower_expr(lctx, e)),
|
2015-07-31 07:04:06 +00:00
|
|
|
PatEnum(ref pth, ref pats) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::PatEnum(lower_path(lctx, pth),
|
2015-09-25 04:03:28 +00:00
|
|
|
pats.as_ref()
|
2015-11-12 17:30:52 +00:00
|
|
|
.map(|pats| pats.iter().map(|x| lower_pat(lctx, x)).collect()))
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
PatQPath(ref qself, ref pth) => {
|
|
|
|
let qself = hir::QSelf {
|
2015-11-12 17:30:52 +00:00
|
|
|
ty: lower_ty(lctx, &qself.ty),
|
2015-07-31 07:04:06 +00:00
|
|
|
position: qself.position,
|
|
|
|
};
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::PatQPath(qself, lower_path(lctx, pth))
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
PatStruct(ref pth, ref fields, etc) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
let pth = lower_path(lctx, pth);
|
2015-10-06 03:03:56 +00:00
|
|
|
let fs = fields.iter()
|
|
|
|
.map(|f| {
|
|
|
|
Spanned {
|
|
|
|
span: f.span,
|
|
|
|
node: hir::FieldPat {
|
|
|
|
name: f.node.ident.name,
|
2015-11-12 17:30:52 +00:00
|
|
|
pat: lower_pat(lctx, &f.node.pat),
|
2015-10-06 03:03:56 +00:00
|
|
|
is_shorthand: f.node.is_shorthand,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2015-07-31 07:04:06 +00:00
|
|
|
hir::PatStruct(pth, fs, etc)
|
|
|
|
}
|
2015-11-12 17:30:52 +00:00
|
|
|
PatTup(ref elts) => hir::PatTup(elts.iter().map(|x| lower_pat(lctx, x)).collect()),
|
|
|
|
PatBox(ref inner) => hir::PatBox(lower_pat(lctx, inner)),
|
2015-11-05 21:17:59 +00:00
|
|
|
PatRegion(ref inner, mutbl) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::PatRegion(lower_pat(lctx, inner), lower_mutability(lctx, mutbl))
|
2015-11-05 21:17:59 +00:00
|
|
|
}
|
2015-07-31 07:04:06 +00:00
|
|
|
PatRange(ref e1, ref e2) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::PatRange(lower_expr(lctx, e1), lower_expr(lctx, e2))
|
2015-10-06 03:03:56 +00:00
|
|
|
}
|
2015-07-31 07:04:06 +00:00
|
|
|
PatVec(ref before, ref slice, ref after) => {
|
2015-11-12 17:30:52 +00:00
|
|
|
hir::PatVec(before.iter().map(|x| lower_pat(lctx, x)).collect(),
|
|
|
|
slice.as_ref().map(|x| lower_pat(lctx, x)),
|
|
|
|
after.iter().map(|x| lower_pat(lctx, x)).collect())
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
PatMac(_) => panic!("Shouldn't exist here"),
|
|
|
|
},
|
2015-09-27 19:23:31 +00:00
|
|
|
span: p.span,
|
|
|
|
})
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
// Utility fn for setting and unsetting the cached id.
|
|
|
|
fn cache_ids<'a, OP, R>(lctx: &LoweringContext, expr_id: NodeId, op: OP) -> R
|
|
|
|
where OP: FnOnce(&LoweringContext) -> R
|
|
|
|
{
|
|
|
|
// Only reset the id if it was previously 0, i.e., was not cached.
|
|
|
|
// If it was cached, we are in a nested node, but our id count will
|
|
|
|
// still count towards the parent's count.
|
|
|
|
let reset_cached_id = lctx.cached_id.get() == 0;
|
|
|
|
|
|
|
|
{
|
2015-09-30 03:17:37 +00:00
|
|
|
let id_cache: &mut HashMap<_, _> = &mut lctx.id_cache.borrow_mut();
|
|
|
|
|
|
|
|
if id_cache.contains_key(&expr_id) {
|
|
|
|
let cached_id = lctx.cached_id.get();
|
|
|
|
if cached_id == 0 {
|
|
|
|
// We're entering a node where we need to track ids, but are not
|
|
|
|
// yet tracking.
|
|
|
|
lctx.cached_id.set(id_cache[&expr_id]);
|
2015-10-06 19:26:22 +00:00
|
|
|
lctx.gensym_key.set(id_cache[&expr_id]);
|
2015-09-30 03:17:37 +00:00
|
|
|
} else {
|
|
|
|
// We're already tracking - check that the tracked id is the same
|
|
|
|
// as the expected id.
|
|
|
|
assert!(cached_id == id_cache[&expr_id], "id mismatch");
|
|
|
|
}
|
|
|
|
} else {
|
2015-10-06 19:26:22 +00:00
|
|
|
let next_id = lctx.id_assigner.peek_node_id();
|
|
|
|
id_cache.insert(expr_id, next_id);
|
|
|
|
lctx.gensym_key.set(next_id);
|
2015-09-30 03:17:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
let result = op(lctx);
|
|
|
|
|
|
|
|
if reset_cached_id {
|
|
|
|
lctx.cached_id.set(0);
|
|
|
|
lctx.gensym_key.set(0);
|
2015-09-30 03:17:37 +00:00
|
|
|
}
|
2015-11-12 17:31:05 +00:00
|
|
|
|
|
|
|
result
|
2015-09-30 03:17:37 +00:00
|
|
|
}
|
|
|
|
|
2015-09-28 02:00:15 +00:00
|
|
|
pub fn lower_expr(lctx: &LoweringContext, e: &Expr) -> P<hir::Expr> {
|
2015-07-31 07:04:06 +00:00
|
|
|
P(hir::Expr {
|
2015-10-06 03:03:56 +00:00
|
|
|
id: e.id,
|
|
|
|
node: match e.node {
|
|
|
|
// Issue #22181:
|
|
|
|
// Eventually a desugaring for `box EXPR`
|
|
|
|
// (similar to the desugaring above for `in PLACE BLOCK`)
|
|
|
|
// should go here, desugaring
|
|
|
|
//
|
|
|
|
// to:
|
|
|
|
//
|
|
|
|
// let mut place = BoxPlace::make_place();
|
|
|
|
// let raw_place = Place::pointer(&mut place);
|
|
|
|
// let value = $value;
|
|
|
|
// unsafe {
|
|
|
|
// ::std::ptr::write(raw_place, value);
|
|
|
|
// Boxed::finalize(place)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// But for now there are type-inference issues doing that.
|
|
|
|
ExprBox(ref e) => {
|
|
|
|
hir::ExprBox(lower_expr(lctx, e))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Desugar ExprBox: `in (PLACE) EXPR`
|
|
|
|
ExprInPlace(ref placer, ref value_expr) => {
|
2015-09-29 00:46:01 +00:00
|
|
|
// to:
|
|
|
|
//
|
2015-10-06 03:03:56 +00:00
|
|
|
// let p = PLACE;
|
|
|
|
// let mut place = Placer::make_place(p);
|
2015-09-29 00:46:01 +00:00
|
|
|
// let raw_place = Place::pointer(&mut place);
|
2015-10-06 03:03:56 +00:00
|
|
|
// push_unsafe!({
|
|
|
|
// std::intrinsics::move_val_init(raw_place, pop_unsafe!( EXPR ));
|
|
|
|
// InPlace::finalize(place)
|
|
|
|
// })
|
2015-11-12 17:31:05 +00:00
|
|
|
return cache_ids(lctx, e.id, |lctx| {
|
|
|
|
let placer_expr = lower_expr(lctx, placer);
|
|
|
|
let value_expr = lower_expr(lctx, value_expr);
|
|
|
|
|
|
|
|
let placer_ident = lctx.str_to_ident("placer");
|
|
|
|
let place_ident = lctx.str_to_ident("place");
|
|
|
|
let p_ptr_ident = lctx.str_to_ident("p_ptr");
|
|
|
|
|
|
|
|
let make_place = ["ops", "Placer", "make_place"];
|
|
|
|
let place_pointer = ["ops", "Place", "pointer"];
|
|
|
|
let move_val_init = ["intrinsics", "move_val_init"];
|
|
|
|
let inplace_finalize = ["ops", "InPlace", "finalize"];
|
|
|
|
|
|
|
|
let make_call = |lctx: &LoweringContext, p, args| {
|
|
|
|
let path = core_path(lctx, e.span, p);
|
|
|
|
let path = expr_path(lctx, path);
|
|
|
|
expr_call(lctx, e.span, path, args)
|
|
|
|
};
|
2015-10-06 03:03:56 +00:00
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
let mk_stmt_let = |lctx: &LoweringContext, bind, expr| {
|
|
|
|
stmt_let(lctx, e.span, false, bind, expr)
|
|
|
|
};
|
2015-10-06 03:03:56 +00:00
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
let mk_stmt_let_mut = |lctx: &LoweringContext, bind, expr| {
|
|
|
|
stmt_let(lctx, e.span, true, bind, expr)
|
|
|
|
};
|
2015-10-06 03:03:56 +00:00
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
// let placer = <placer_expr> ;
|
|
|
|
let s1 = {
|
|
|
|
let placer_expr = signal_block_expr(lctx,
|
|
|
|
vec![],
|
|
|
|
placer_expr,
|
|
|
|
e.span,
|
|
|
|
hir::PopUnstableBlock);
|
|
|
|
mk_stmt_let(lctx, placer_ident, placer_expr)
|
|
|
|
};
|
2015-10-06 03:03:56 +00:00
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
// let mut place = Placer::make_place(placer);
|
|
|
|
let s2 = {
|
|
|
|
let placer = expr_ident(lctx, e.span, placer_ident);
|
|
|
|
let call = make_call(lctx, &make_place, vec![placer]);
|
|
|
|
mk_stmt_let_mut(lctx, place_ident, call)
|
|
|
|
};
|
2015-09-29 00:17:46 +00:00
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
// let p_ptr = Place::pointer(&mut place);
|
|
|
|
let s3 = {
|
|
|
|
let agent = expr_ident(lctx, e.span, place_ident);
|
|
|
|
let args = vec![expr_mut_addr_of(lctx, e.span, agent)];
|
|
|
|
let call = make_call(lctx, &place_pointer, args);
|
|
|
|
mk_stmt_let(lctx, p_ptr_ident, call)
|
|
|
|
};
|
2015-09-29 00:17:46 +00:00
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
// pop_unsafe!(EXPR));
|
|
|
|
let pop_unsafe_expr = {
|
|
|
|
let value_expr = signal_block_expr(lctx,
|
|
|
|
vec![],
|
|
|
|
value_expr,
|
|
|
|
e.span,
|
|
|
|
hir::PopUnstableBlock);
|
|
|
|
signal_block_expr(lctx,
|
|
|
|
vec![],
|
|
|
|
value_expr,
|
|
|
|
e.span,
|
|
|
|
hir::PopUnsafeBlock(hir::CompilerGenerated))
|
|
|
|
};
|
2015-09-29 00:17:46 +00:00
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
// push_unsafe!({
|
|
|
|
// std::intrinsics::move_val_init(raw_place, pop_unsafe!( EXPR ));
|
|
|
|
// InPlace::finalize(place)
|
|
|
|
// })
|
|
|
|
let expr = {
|
|
|
|
let ptr = expr_ident(lctx, e.span, p_ptr_ident);
|
|
|
|
let call_move_val_init =
|
|
|
|
hir::StmtSemi(
|
|
|
|
make_call(lctx, &move_val_init, vec![ptr, pop_unsafe_expr]),
|
|
|
|
lctx.next_id());
|
|
|
|
let call_move_val_init = respan(e.span, call_move_val_init);
|
|
|
|
|
|
|
|
let place = expr_ident(lctx, e.span, place_ident);
|
|
|
|
let call = make_call(lctx, &inplace_finalize, vec![place]);
|
|
|
|
signal_block_expr(lctx,
|
|
|
|
vec![P(call_move_val_init)],
|
|
|
|
call,
|
|
|
|
e.span,
|
|
|
|
hir::PushUnsafeBlock(hir::CompilerGenerated))
|
|
|
|
};
|
2015-10-06 03:03:56 +00:00
|
|
|
|
|
|
|
signal_block_expr(lctx,
|
2015-11-12 17:31:05 +00:00
|
|
|
vec![s1, s2, s3],
|
|
|
|
expr,
|
2015-10-06 03:03:56 +00:00
|
|
|
e.span,
|
2015-11-12 17:31:05 +00:00
|
|
|
hir::PushUnstableBlock)
|
|
|
|
});
|
2015-10-06 03:03:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ExprVec(ref exprs) => {
|
|
|
|
hir::ExprVec(exprs.iter().map(|x| lower_expr(lctx, x)).collect())
|
|
|
|
}
|
|
|
|
ExprRepeat(ref expr, ref count) => {
|
2015-11-12 17:31:05 +00:00
|
|
|
let expr = lower_expr(lctx, expr);
|
|
|
|
let count = lower_expr(lctx, count);
|
|
|
|
hir::ExprRepeat(expr, count)
|
2015-10-06 03:03:56 +00:00
|
|
|
}
|
|
|
|
ExprTup(ref elts) => {
|
|
|
|
hir::ExprTup(elts.iter().map(|x| lower_expr(lctx, x)).collect())
|
|
|
|
}
|
|
|
|
ExprCall(ref f, ref args) => {
|
2015-11-12 17:31:05 +00:00
|
|
|
let f = lower_expr(lctx, f);
|
|
|
|
hir::ExprCall(f, args.iter().map(|x| lower_expr(lctx, x)).collect())
|
2015-10-06 03:03:56 +00:00
|
|
|
}
|
|
|
|
ExprMethodCall(i, ref tps, ref args) => {
|
2015-11-12 17:31:05 +00:00
|
|
|
let tps = tps.iter().map(|x| lower_ty(lctx, x)).collect();
|
|
|
|
let args = args.iter().map(|x| lower_expr(lctx, x)).collect();
|
|
|
|
hir::ExprMethodCall(respan(i.span, i.node.name), tps, args)
|
2015-10-06 03:03:56 +00:00
|
|
|
}
|
|
|
|
ExprBinary(binop, ref lhs, ref rhs) => {
|
2015-11-12 17:31:05 +00:00
|
|
|
let binop = lower_binop(lctx, binop);
|
|
|
|
let lhs = lower_expr(lctx, lhs);
|
|
|
|
let rhs = lower_expr(lctx, rhs);
|
|
|
|
hir::ExprBinary(binop, lhs, rhs)
|
2015-10-06 03:03:56 +00:00
|
|
|
}
|
|
|
|
ExprUnary(op, ref ohs) => {
|
2015-11-12 17:31:05 +00:00
|
|
|
let op = lower_unop(lctx, op);
|
|
|
|
let ohs = lower_expr(lctx, ohs);
|
|
|
|
hir::ExprUnary(op, ohs)
|
2015-10-06 03:03:56 +00:00
|
|
|
}
|
|
|
|
ExprLit(ref l) => hir::ExprLit(P((**l).clone())),
|
|
|
|
ExprCast(ref expr, ref ty) => {
|
2015-11-12 17:31:05 +00:00
|
|
|
let expr = lower_expr(lctx, expr);
|
|
|
|
hir::ExprCast(expr, lower_ty(lctx, ty))
|
2015-10-06 03:03:56 +00:00
|
|
|
}
|
|
|
|
ExprAddrOf(m, ref ohs) => {
|
2015-11-12 17:31:05 +00:00
|
|
|
let m = lower_mutability(lctx, m);
|
|
|
|
let ohs = lower_expr(lctx, ohs);
|
|
|
|
hir::ExprAddrOf(m, ohs)
|
2015-10-06 03:03:56 +00:00
|
|
|
}
|
|
|
|
// More complicated than you might expect because the else branch
|
|
|
|
// might be `if let`.
|
|
|
|
ExprIf(ref cond, ref blk, ref else_opt) => {
|
|
|
|
let else_opt = else_opt.as_ref().map(|els| {
|
|
|
|
match els.node {
|
2015-09-28 04:24:42 +00:00
|
|
|
ExprIfLet(..) => {
|
2015-11-12 17:31:05 +00:00
|
|
|
cache_ids(lctx, e.id, |lctx| {
|
|
|
|
// wrap the if-let expr in a block
|
|
|
|
let span = els.span;
|
|
|
|
let els = lower_expr(lctx, els);
|
|
|
|
let id = lctx.next_id();
|
|
|
|
let blk = P(hir::Block {
|
|
|
|
stmts: vec![],
|
|
|
|
expr: Some(els),
|
|
|
|
id: id,
|
|
|
|
rules: hir::DefaultBlock,
|
|
|
|
span: span,
|
|
|
|
});
|
|
|
|
expr_block(lctx, blk)
|
|
|
|
})
|
2015-09-28 04:24:42 +00:00
|
|
|
}
|
2015-10-06 03:03:56 +00:00
|
|
|
_ => lower_expr(lctx, els),
|
|
|
|
}
|
|
|
|
});
|
2015-09-28 04:24:42 +00:00
|
|
|
|
2015-11-05 21:17:59 +00:00
|
|
|
hir::ExprIf(lower_expr(lctx, cond), lower_block(lctx, blk), else_opt)
|
2015-10-06 03:03:56 +00:00
|
|
|
}
|
|
|
|
ExprWhile(ref cond, ref body, opt_ident) => {
|
2015-11-05 21:17:59 +00:00
|
|
|
hir::ExprWhile(lower_expr(lctx, cond), lower_block(lctx, body), opt_ident)
|
2015-10-06 03:03:56 +00:00
|
|
|
}
|
|
|
|
ExprLoop(ref body, opt_ident) => {
|
|
|
|
hir::ExprLoop(lower_block(lctx, body), opt_ident)
|
|
|
|
}
|
|
|
|
ExprMatch(ref expr, ref arms) => {
|
|
|
|
hir::ExprMatch(lower_expr(lctx, expr),
|
|
|
|
arms.iter().map(|x| lower_arm(lctx, x)).collect(),
|
|
|
|
hir::MatchSource::Normal)
|
|
|
|
}
|
|
|
|
ExprClosure(capture_clause, ref decl, ref body) => {
|
|
|
|
hir::ExprClosure(lower_capture_clause(lctx, capture_clause),
|
|
|
|
lower_fn_decl(lctx, decl),
|
|
|
|
lower_block(lctx, body))
|
|
|
|
}
|
|
|
|
ExprBlock(ref blk) => hir::ExprBlock(lower_block(lctx, blk)),
|
|
|
|
ExprAssign(ref el, ref er) => {
|
|
|
|
hir::ExprAssign(lower_expr(lctx, el), lower_expr(lctx, er))
|
|
|
|
}
|
|
|
|
ExprAssignOp(op, ref el, ref er) => {
|
|
|
|
hir::ExprAssignOp(lower_binop(lctx, op),
|
|
|
|
lower_expr(lctx, el),
|
|
|
|
lower_expr(lctx, er))
|
|
|
|
}
|
|
|
|
ExprField(ref el, ident) => {
|
2015-11-05 21:17:59 +00:00
|
|
|
hir::ExprField(lower_expr(lctx, el), respan(ident.span, ident.node.name))
|
2015-10-06 03:03:56 +00:00
|
|
|
}
|
|
|
|
ExprTupField(ref el, ident) => {
|
|
|
|
hir::ExprTupField(lower_expr(lctx, el), ident)
|
|
|
|
}
|
|
|
|
ExprIndex(ref el, ref er) => {
|
|
|
|
hir::ExprIndex(lower_expr(lctx, el), lower_expr(lctx, er))
|
|
|
|
}
|
|
|
|
ExprRange(ref e1, ref e2) => {
|
|
|
|
hir::ExprRange(e1.as_ref().map(|x| lower_expr(lctx, x)),
|
|
|
|
e2.as_ref().map(|x| lower_expr(lctx, x)))
|
|
|
|
}
|
|
|
|
ExprPath(ref qself, ref path) => {
|
|
|
|
let qself = qself.as_ref().map(|&QSelf { ref ty, position }| {
|
|
|
|
hir::QSelf {
|
|
|
|
ty: lower_ty(lctx, ty),
|
|
|
|
position: position,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
hir::ExprPath(qself, lower_path(lctx, path))
|
|
|
|
}
|
|
|
|
ExprBreak(opt_ident) => hir::ExprBreak(opt_ident),
|
|
|
|
ExprAgain(opt_ident) => hir::ExprAgain(opt_ident),
|
|
|
|
ExprRet(ref e) => hir::ExprRet(e.as_ref().map(|x| lower_expr(lctx, x))),
|
|
|
|
ExprInlineAsm(InlineAsm {
|
2015-07-31 07:04:06 +00:00
|
|
|
ref inputs,
|
|
|
|
ref outputs,
|
|
|
|
ref asm,
|
|
|
|
asm_str_style,
|
|
|
|
ref clobbers,
|
|
|
|
volatile,
|
|
|
|
alignstack,
|
|
|
|
dialect,
|
|
|
|
expn_id,
|
|
|
|
}) => hir::ExprInlineAsm(hir::InlineAsm {
|
2015-10-06 03:03:56 +00:00
|
|
|
inputs: inputs.iter()
|
|
|
|
.map(|&(ref c, ref input)| (c.clone(), lower_expr(lctx, input)))
|
|
|
|
.collect(),
|
|
|
|
outputs: outputs.iter()
|
|
|
|
.map(|&(ref c, ref out, ref is_rw)| {
|
|
|
|
(c.clone(), lower_expr(lctx, out), *is_rw)
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
asm: asm.clone(),
|
|
|
|
asm_str_style: asm_str_style,
|
|
|
|
clobbers: clobbers.clone(),
|
|
|
|
volatile: volatile,
|
|
|
|
alignstack: alignstack,
|
|
|
|
dialect: dialect,
|
|
|
|
expn_id: expn_id,
|
|
|
|
}),
|
|
|
|
ExprStruct(ref path, ref fields, ref maybe_expr) => {
|
|
|
|
hir::ExprStruct(lower_path(lctx, path),
|
|
|
|
fields.iter().map(|x| lower_field(lctx, x)).collect(),
|
|
|
|
maybe_expr.as_ref().map(|x| lower_expr(lctx, x)))
|
|
|
|
}
|
|
|
|
ExprParen(ref ex) => {
|
|
|
|
return lower_expr(lctx, ex);
|
|
|
|
}
|
2015-09-28 04:24:42 +00:00
|
|
|
|
2015-10-06 03:03:56 +00:00
|
|
|
// Desugar ExprIfLet
|
|
|
|
// From: `if let <pat> = <sub_expr> <body> [<else_opt>]`
|
|
|
|
ExprIfLet(ref pat, ref sub_expr, ref body, ref else_opt) => {
|
|
|
|
// to:
|
|
|
|
//
|
|
|
|
// match <sub_expr> {
|
|
|
|
// <pat> => <body>,
|
|
|
|
// [_ if <else_opt_if_cond> => <else_opt_if_body>,]
|
|
|
|
// _ => [<else_opt> | ()]
|
|
|
|
// }
|
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
return cache_ids(lctx, e.id, |lctx| {
|
|
|
|
// `<pat> => <body>`
|
|
|
|
let pat_arm = {
|
|
|
|
let body = lower_block(lctx, body);
|
|
|
|
let body_expr = expr_block(lctx, body);
|
|
|
|
arm(vec![lower_pat(lctx, pat)], body_expr)
|
|
|
|
};
|
2015-09-28 04:24:42 +00:00
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
// `[_ if <else_opt_if_cond> => <else_opt_if_body>,]`
|
|
|
|
let mut else_opt = else_opt.as_ref().map(|e| lower_expr(lctx, e));
|
|
|
|
let else_if_arms = {
|
|
|
|
let mut arms = vec![];
|
|
|
|
loop {
|
|
|
|
let else_opt_continue = else_opt.and_then(|els| {
|
|
|
|
els.and_then(|els| {
|
|
|
|
match els.node {
|
|
|
|
// else if
|
|
|
|
hir::ExprIf(cond, then, else_opt) => {
|
|
|
|
let pat_under = pat_wild(lctx, e.span);
|
|
|
|
arms.push(hir::Arm {
|
|
|
|
attrs: vec![],
|
|
|
|
pats: vec![pat_under],
|
|
|
|
guard: Some(cond),
|
|
|
|
body: expr_block(lctx, then),
|
|
|
|
});
|
|
|
|
else_opt.map(|else_opt| (else_opt, true))
|
|
|
|
}
|
|
|
|
_ => Some((P(els), false)),
|
2015-10-06 03:03:56 +00:00
|
|
|
}
|
2015-11-12 17:31:05 +00:00
|
|
|
})
|
|
|
|
});
|
|
|
|
match else_opt_continue {
|
|
|
|
Some((e, true)) => {
|
|
|
|
else_opt = Some(e);
|
|
|
|
}
|
|
|
|
Some((e, false)) => {
|
|
|
|
else_opt = Some(e);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
else_opt = None;
|
|
|
|
break;
|
2015-09-28 04:24:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-12 17:31:05 +00:00
|
|
|
arms
|
|
|
|
};
|
2015-09-28 04:24:42 +00:00
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
let contains_else_clause = else_opt.is_some();
|
2015-09-28 04:24:42 +00:00
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
// `_ => [<else_opt> | ()]`
|
|
|
|
let else_arm = {
|
|
|
|
let pat_under = pat_wild(lctx, e.span);
|
|
|
|
let else_expr =
|
|
|
|
else_opt.unwrap_or_else(
|
|
|
|
|| expr_tuple(lctx, e.span, vec![]));
|
|
|
|
arm(vec![pat_under], else_expr)
|
|
|
|
};
|
2015-09-28 04:24:42 +00:00
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
let mut arms = Vec::with_capacity(else_if_arms.len() + 2);
|
|
|
|
arms.push(pat_arm);
|
|
|
|
arms.extend(else_if_arms);
|
|
|
|
arms.push(else_arm);
|
2015-10-06 03:03:56 +00:00
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
let sub_expr = lower_expr(lctx, sub_expr);
|
|
|
|
expr(lctx,
|
|
|
|
e.span,
|
|
|
|
hir::ExprMatch(sub_expr,
|
|
|
|
arms,
|
|
|
|
hir::MatchSource::IfLetDesugar {
|
|
|
|
contains_else_clause: contains_else_clause,
|
|
|
|
}))
|
|
|
|
});
|
2015-10-06 03:03:56 +00:00
|
|
|
}
|
2015-09-28 04:24:42 +00:00
|
|
|
|
2015-10-06 03:03:56 +00:00
|
|
|
// Desugar ExprWhileLet
|
|
|
|
// From: `[opt_ident]: while let <pat> = <sub_expr> <body>`
|
|
|
|
ExprWhileLet(ref pat, ref sub_expr, ref body, opt_ident) => {
|
|
|
|
// to:
|
|
|
|
//
|
|
|
|
// [opt_ident]: loop {
|
|
|
|
// match <sub_expr> {
|
|
|
|
// <pat> => <body>,
|
|
|
|
// _ => break
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
return cache_ids(lctx, e.id, |lctx| {
|
|
|
|
// `<pat> => <body>`
|
|
|
|
let pat_arm = {
|
|
|
|
let body = lower_block(lctx, body);
|
|
|
|
let body_expr = expr_block(lctx, body);
|
|
|
|
arm(vec![lower_pat(lctx, pat)], body_expr)
|
|
|
|
};
|
2015-09-28 04:24:42 +00:00
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
// `_ => break`
|
|
|
|
let break_arm = {
|
|
|
|
let pat_under = pat_wild(lctx, e.span);
|
|
|
|
let break_expr = expr_break(lctx, e.span);
|
|
|
|
arm(vec![pat_under], break_expr)
|
|
|
|
};
|
2015-10-06 03:03:56 +00:00
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
// `match <sub_expr> { ... }`
|
|
|
|
let arms = vec![pat_arm, break_arm];
|
|
|
|
let sub_expr = lower_expr(lctx, sub_expr);
|
|
|
|
let match_expr = expr(lctx,
|
|
|
|
e.span,
|
|
|
|
hir::ExprMatch(sub_expr,
|
|
|
|
arms,
|
|
|
|
hir::MatchSource::WhileLetDesugar));
|
|
|
|
|
|
|
|
// `[opt_ident]: loop { ... }`
|
|
|
|
let loop_block = block_expr(lctx, match_expr);
|
|
|
|
expr(lctx, e.span, hir::ExprLoop(loop_block, opt_ident))
|
|
|
|
});
|
2015-10-06 03:03:56 +00:00
|
|
|
}
|
2015-09-28 04:24:42 +00:00
|
|
|
|
2015-10-06 03:03:56 +00:00
|
|
|
// Desugar ExprForLoop
|
|
|
|
// From: `[opt_ident]: for <pat> in <head> <body>`
|
|
|
|
ExprForLoop(ref pat, ref head, ref body, opt_ident) => {
|
|
|
|
// to:
|
|
|
|
//
|
|
|
|
// {
|
|
|
|
// let result = match ::std::iter::IntoIterator::into_iter(<head>) {
|
|
|
|
// mut iter => {
|
|
|
|
// [opt_ident]: loop {
|
|
|
|
// match ::std::iter::Iterator::next(&mut iter) {
|
|
|
|
// ::std::option::Option::Some(<pat>) => <body>,
|
|
|
|
// ::std::option::Option::None => break
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
// result
|
|
|
|
// }
|
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
return cache_ids(lctx, e.id, |lctx| {
|
|
|
|
// expand <head>
|
|
|
|
let head = lower_expr(lctx, head);
|
2015-10-06 03:03:56 +00:00
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
let iter = lctx.str_to_ident("iter");
|
2015-09-28 02:00:15 +00:00
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
// `::std::option::Option::Some(<pat>) => <body>`
|
|
|
|
let pat_arm = {
|
|
|
|
let body_block = lower_block(lctx, body);
|
|
|
|
let body_span = body_block.span;
|
|
|
|
let body_expr = P(hir::Expr {
|
|
|
|
id: lctx.next_id(),
|
|
|
|
node: hir::ExprBlock(body_block),
|
|
|
|
span: body_span,
|
|
|
|
});
|
|
|
|
let pat = lower_pat(lctx, pat);
|
|
|
|
let some_pat = pat_some(lctx, e.span, pat);
|
2015-09-28 02:00:15 +00:00
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
arm(vec![some_pat], body_expr)
|
|
|
|
};
|
2015-09-28 02:00:15 +00:00
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
// `::std::option::Option::None => break`
|
|
|
|
let break_arm = {
|
|
|
|
let break_expr = expr_break(lctx, e.span);
|
2015-09-28 02:00:15 +00:00
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
arm(vec![pat_none(lctx, e.span)], break_expr)
|
|
|
|
};
|
2015-09-28 02:00:15 +00:00
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
// `match ::std::iter::Iterator::next(&mut iter) { ... }`
|
|
|
|
let match_expr = {
|
|
|
|
let next_path = {
|
|
|
|
let strs = std_path(lctx, &["iter", "Iterator", "next"]);
|
|
|
|
|
|
|
|
path_global(e.span, strs)
|
|
|
|
};
|
|
|
|
let iter = expr_ident(lctx, e.span, iter);
|
|
|
|
let ref_mut_iter = expr_mut_addr_of(lctx, e.span, iter);
|
|
|
|
let next_path = expr_path(lctx, next_path);
|
|
|
|
let next_expr = expr_call(lctx, e.span, next_path, vec![ref_mut_iter]);
|
|
|
|
let arms = vec![pat_arm, break_arm];
|
|
|
|
|
|
|
|
expr(lctx,
|
|
|
|
e.span,
|
|
|
|
hir::ExprMatch(next_expr, arms, hir::MatchSource::ForLoopDesugar))
|
2015-09-28 02:00:15 +00:00
|
|
|
};
|
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
// `[opt_ident]: loop { ... }`
|
|
|
|
let loop_block = block_expr(lctx, match_expr);
|
|
|
|
let loop_expr = expr(lctx, e.span, hir::ExprLoop(loop_block, opt_ident));
|
|
|
|
|
|
|
|
// `mut iter => { ... }`
|
|
|
|
let iter_arm = {
|
|
|
|
let iter_pat = pat_ident_binding_mode(lctx,
|
|
|
|
e.span,
|
|
|
|
iter,
|
|
|
|
hir::BindByValue(hir::MutMutable));
|
|
|
|
arm(vec![iter_pat], loop_expr)
|
|
|
|
};
|
2015-09-28 02:00:15 +00:00
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
// `match ::std::iter::IntoIterator::into_iter(<head>) { ... }`
|
|
|
|
let into_iter_expr = {
|
|
|
|
let into_iter_path = {
|
|
|
|
let strs = std_path(lctx, &["iter", "IntoIterator", "into_iter"]);
|
2015-09-28 02:00:15 +00:00
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
path_global(e.span, strs)
|
|
|
|
};
|
2015-09-28 02:00:15 +00:00
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
let into_iter = expr_path(lctx, into_iter_path);
|
|
|
|
expr_call(lctx, e.span, into_iter, vec![head])
|
2015-09-28 02:00:15 +00:00
|
|
|
};
|
|
|
|
|
2015-11-12 17:31:05 +00:00
|
|
|
let match_expr = expr_match(lctx,
|
|
|
|
e.span,
|
|
|
|
into_iter_expr,
|
|
|
|
vec![iter_arm],
|
|
|
|
hir::MatchSource::ForLoopDesugar);
|
|
|
|
|
|
|
|
// `{ let result = ...; result }`
|
|
|
|
let result_ident = lctx.str_to_ident("result");
|
|
|
|
let let_stmt = stmt_let(lctx, e.span, false, result_ident, match_expr);
|
|
|
|
let result = expr_ident(lctx, e.span, result_ident);
|
|
|
|
let block = block_all(lctx, e.span, vec![let_stmt], Some(result));
|
|
|
|
expr_block(lctx, block)
|
|
|
|
});
|
2015-10-06 03:03:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ExprMac(_) => panic!("Shouldn't exist here"),
|
|
|
|
},
|
|
|
|
span: e.span,
|
|
|
|
})
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_stmt(lctx: &LoweringContext, s: &Stmt) -> P<hir::Stmt> {
|
2015-07-31 07:04:06 +00:00
|
|
|
match s.node {
|
|
|
|
StmtDecl(ref d, id) => {
|
|
|
|
P(Spanned {
|
2015-11-12 17:30:52 +00:00
|
|
|
node: hir::StmtDecl(lower_decl(lctx, d), id),
|
2015-10-06 03:03:56 +00:00
|
|
|
span: s.span,
|
2015-07-31 07:04:06 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
StmtExpr(ref e, id) => {
|
|
|
|
P(Spanned {
|
2015-11-12 17:30:52 +00:00
|
|
|
node: hir::StmtExpr(lower_expr(lctx, e), id),
|
2015-10-06 03:03:56 +00:00
|
|
|
span: s.span,
|
2015-07-31 07:04:06 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
StmtSemi(ref e, id) => {
|
|
|
|
P(Spanned {
|
2015-11-12 17:30:52 +00:00
|
|
|
node: hir::StmtSemi(lower_expr(lctx, e), id),
|
2015-10-06 03:03:56 +00:00
|
|
|
span: s.span,
|
2015-07-31 07:04:06 +00:00
|
|
|
})
|
|
|
|
}
|
2015-09-27 19:23:31 +00:00
|
|
|
StmtMac(..) => panic!("Shouldn't exist here"),
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 04:03:28 +00:00
|
|
|
pub fn lower_capture_clause(_lctx: &LoweringContext, c: CaptureClause) -> hir::CaptureClause {
|
2015-07-31 07:04:06 +00:00
|
|
|
match c {
|
|
|
|
CaptureByValue => hir::CaptureByValue,
|
|
|
|
CaptureByRef => hir::CaptureByRef,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 04:03:28 +00:00
|
|
|
pub fn lower_visibility(_lctx: &LoweringContext, v: Visibility) -> hir::Visibility {
|
2015-07-31 07:04:06 +00:00
|
|
|
match v {
|
|
|
|
Public => hir::Public,
|
|
|
|
Inherited => hir::Inherited,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_block_check_mode(lctx: &LoweringContext, b: &BlockCheckMode) -> hir::BlockCheckMode {
|
2015-07-31 07:04:06 +00:00
|
|
|
match *b {
|
|
|
|
DefaultBlock => hir::DefaultBlock,
|
2015-11-12 17:30:52 +00:00
|
|
|
UnsafeBlock(u) => hir::UnsafeBlock(lower_unsafe_source(lctx, u)),
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_binding_mode(lctx: &LoweringContext, b: &BindingMode) -> hir::BindingMode {
|
2015-07-31 07:04:06 +00:00
|
|
|
match *b {
|
2015-11-12 17:30:52 +00:00
|
|
|
BindByRef(m) => hir::BindByRef(lower_mutability(lctx, m)),
|
|
|
|
BindByValue(m) => hir::BindByValue(lower_mutability(lctx, m)),
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-12 17:30:52 +00:00
|
|
|
pub fn lower_struct_field_kind(lctx: &LoweringContext,
|
2015-09-25 04:03:28 +00:00
|
|
|
s: &StructFieldKind)
|
|
|
|
-> hir::StructFieldKind {
|
2015-07-31 07:04:06 +00:00
|
|
|
match *s {
|
2015-11-12 17:30:52 +00:00
|
|
|
NamedField(ident, vis) => hir::NamedField(ident.name, lower_visibility(lctx, vis)),
|
|
|
|
UnnamedField(vis) => hir::UnnamedField(lower_visibility(lctx, vis)),
|
2015-07-31 07:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 04:03:28 +00:00
|
|
|
pub fn lower_unsafe_source(_lctx: &LoweringContext, u: UnsafeSource) -> hir::UnsafeSource {
|
2015-07-31 07:04:06 +00:00
|
|
|
match u {
|
|
|
|
CompilerGenerated => hir::CompilerGenerated,
|
|
|
|
UserProvided => hir::UserProvided,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 04:03:28 +00:00
|
|
|
pub fn lower_impl_polarity(_lctx: &LoweringContext, i: ImplPolarity) -> hir::ImplPolarity {
|
2015-07-31 07:04:06 +00:00
|
|
|
match i {
|
|
|
|
ImplPolarity::Positive => hir::ImplPolarity::Positive,
|
|
|
|
ImplPolarity::Negative => hir::ImplPolarity::Negative,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 04:03:28 +00:00
|
|
|
pub fn lower_trait_bound_modifier(_lctx: &LoweringContext,
|
|
|
|
f: TraitBoundModifier)
|
|
|
|
-> hir::TraitBoundModifier {
|
2015-07-31 07:04:06 +00:00
|
|
|
match f {
|
|
|
|
TraitBoundModifier::None => hir::TraitBoundModifier::None,
|
|
|
|
TraitBoundModifier::Maybe => hir::TraitBoundModifier::Maybe,
|
|
|
|
}
|
|
|
|
}
|
2015-09-28 02:00:15 +00:00
|
|
|
|
|
|
|
// Helper methods for building HIR.
|
|
|
|
|
|
|
|
fn arm(pats: Vec<P<hir::Pat>>, expr: P<hir::Expr>) -> hir::Arm {
|
|
|
|
hir::Arm {
|
2015-11-05 21:17:59 +00:00
|
|
|
attrs: vec![],
|
2015-09-28 02:00:15 +00:00
|
|
|
pats: pats,
|
|
|
|
guard: None,
|
2015-10-06 03:03:56 +00:00
|
|
|
body: expr,
|
2015-09-28 02:00:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expr_break(lctx: &LoweringContext, span: Span) -> P<hir::Expr> {
|
|
|
|
expr(lctx, span, hir::ExprBreak(None))
|
|
|
|
}
|
|
|
|
|
2015-10-06 03:03:56 +00:00
|
|
|
fn expr_call(lctx: &LoweringContext,
|
|
|
|
span: Span,
|
|
|
|
e: P<hir::Expr>,
|
|
|
|
args: Vec<P<hir::Expr>>)
|
|
|
|
-> P<hir::Expr> {
|
2015-09-28 02:00:15 +00:00
|
|
|
expr(lctx, span, hir::ExprCall(e, args))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expr_ident(lctx: &LoweringContext, span: Span, id: Ident) -> P<hir::Expr> {
|
|
|
|
expr_path(lctx, path_ident(span, id))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expr_mut_addr_of(lctx: &LoweringContext, span: Span, e: P<hir::Expr>) -> P<hir::Expr> {
|
|
|
|
expr(lctx, span, hir::ExprAddrOf(hir::MutMutable, e))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expr_path(lctx: &LoweringContext, path: hir::Path) -> P<hir::Expr> {
|
|
|
|
expr(lctx, path.span, hir::ExprPath(None, path))
|
|
|
|
}
|
|
|
|
|
2015-10-06 03:03:56 +00:00
|
|
|
fn expr_match(lctx: &LoweringContext,
|
|
|
|
span: Span,
|
|
|
|
arg: P<hir::Expr>,
|
2015-10-11 20:49:29 +00:00
|
|
|
arms: Vec<hir::Arm>,
|
|
|
|
source: hir::MatchSource)
|
2015-10-06 03:03:56 +00:00
|
|
|
-> P<hir::Expr> {
|
2015-11-05 21:17:59 +00:00
|
|
|
expr(lctx, span, hir::ExprMatch(arg, arms, source))
|
2015-09-28 02:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn expr_block(lctx: &LoweringContext, b: P<hir::Block>) -> P<hir::Expr> {
|
|
|
|
expr(lctx, b.span, hir::ExprBlock(b))
|
|
|
|
}
|
|
|
|
|
2015-09-28 04:24:42 +00:00
|
|
|
fn expr_tuple(lctx: &LoweringContext, sp: Span, exprs: Vec<P<hir::Expr>>) -> P<hir::Expr> {
|
|
|
|
expr(lctx, sp, hir::ExprTup(exprs))
|
|
|
|
}
|
|
|
|
|
2015-09-28 02:00:15 +00:00
|
|
|
fn expr(lctx: &LoweringContext, span: Span, node: hir::Expr_) -> P<hir::Expr> {
|
|
|
|
P(hir::Expr {
|
|
|
|
id: lctx.next_id(),
|
|
|
|
node: node,
|
|
|
|
span: span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-10-06 03:03:56 +00:00
|
|
|
fn stmt_let(lctx: &LoweringContext,
|
|
|
|
sp: Span,
|
|
|
|
mutbl: bool,
|
|
|
|
ident: Ident,
|
|
|
|
ex: P<hir::Expr>)
|
|
|
|
-> P<hir::Stmt> {
|
2015-09-28 02:00:15 +00:00
|
|
|
let pat = if mutbl {
|
|
|
|
pat_ident_binding_mode(lctx, sp, ident, hir::BindByValue(hir::MutMutable))
|
|
|
|
} else {
|
|
|
|
pat_ident(lctx, sp, ident)
|
|
|
|
};
|
|
|
|
let local = P(hir::Local {
|
|
|
|
pat: pat,
|
|
|
|
ty: None,
|
|
|
|
init: Some(ex),
|
|
|
|
id: lctx.next_id(),
|
|
|
|
span: sp,
|
|
|
|
});
|
|
|
|
let decl = respan(sp, hir::DeclLocal(local));
|
|
|
|
P(respan(sp, hir::StmtDecl(P(decl), lctx.next_id())))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn block_expr(lctx: &LoweringContext, expr: P<hir::Expr>) -> P<hir::Block> {
|
|
|
|
block_all(lctx, expr.span, Vec::new(), Some(expr))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn block_all(lctx: &LoweringContext,
|
|
|
|
span: Span,
|
|
|
|
stmts: Vec<P<hir::Stmt>>,
|
2015-10-06 03:03:56 +00:00
|
|
|
expr: Option<P<hir::Expr>>)
|
|
|
|
-> P<hir::Block> {
|
|
|
|
P(hir::Block {
|
|
|
|
stmts: stmts,
|
|
|
|
expr: expr,
|
|
|
|
id: lctx.next_id(),
|
|
|
|
rules: hir::DefaultBlock,
|
|
|
|
span: span,
|
|
|
|
})
|
2015-09-28 02:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn pat_some(lctx: &LoweringContext, span: Span, pat: P<hir::Pat>) -> P<hir::Pat> {
|
|
|
|
let some = std_path(lctx, &["option", "Option", "Some"]);
|
|
|
|
let path = path_global(span, some);
|
2015-11-05 21:17:59 +00:00
|
|
|
pat_enum(lctx, span, path, vec![pat])
|
2015-09-28 02:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn pat_none(lctx: &LoweringContext, span: Span) -> P<hir::Pat> {
|
|
|
|
let none = std_path(lctx, &["option", "Option", "None"]);
|
|
|
|
let path = path_global(span, none);
|
|
|
|
pat_enum(lctx, span, path, vec![])
|
|
|
|
}
|
|
|
|
|
2015-10-06 03:03:56 +00:00
|
|
|
fn pat_enum(lctx: &LoweringContext,
|
|
|
|
span: Span,
|
|
|
|
path: hir::Path,
|
|
|
|
subpats: Vec<P<hir::Pat>>)
|
|
|
|
-> P<hir::Pat> {
|
2015-09-28 02:00:15 +00:00
|
|
|
let pt = hir::PatEnum(path, Some(subpats));
|
|
|
|
pat(lctx, span, pt)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pat_ident(lctx: &LoweringContext, span: Span, ident: Ident) -> P<hir::Pat> {
|
|
|
|
pat_ident_binding_mode(lctx, span, ident, hir::BindByValue(hir::MutImmutable))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pat_ident_binding_mode(lctx: &LoweringContext,
|
|
|
|
span: Span,
|
|
|
|
ident: Ident,
|
2015-10-06 03:03:56 +00:00
|
|
|
bm: hir::BindingMode)
|
|
|
|
-> P<hir::Pat> {
|
|
|
|
let pat_ident = hir::PatIdent(bm,
|
|
|
|
Spanned {
|
|
|
|
span: span,
|
|
|
|
node: ident,
|
|
|
|
},
|
|
|
|
None);
|
2015-09-28 02:00:15 +00:00
|
|
|
pat(lctx, span, pat_ident)
|
|
|
|
}
|
|
|
|
|
2015-09-28 04:24:42 +00:00
|
|
|
fn pat_wild(lctx: &LoweringContext, span: Span) -> P<hir::Pat> {
|
2015-10-31 00:44:43 +00:00
|
|
|
pat(lctx, span, hir::PatWild)
|
2015-09-28 04:24:42 +00:00
|
|
|
}
|
|
|
|
|
2015-09-28 02:00:15 +00:00
|
|
|
fn pat(lctx: &LoweringContext, span: Span, pat: hir::Pat_) -> P<hir::Pat> {
|
2015-10-06 03:03:56 +00:00
|
|
|
P(hir::Pat {
|
|
|
|
id: lctx.next_id(),
|
|
|
|
node: pat,
|
|
|
|
span: span,
|
|
|
|
})
|
2015-09-28 02:00:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn path_ident(span: Span, id: Ident) -> hir::Path {
|
2015-11-05 21:17:59 +00:00
|
|
|
path(span, vec![id])
|
2015-09-28 02:00:15 +00:00
|
|
|
}
|
|
|
|
|
2015-10-06 03:03:56 +00:00
|
|
|
fn path(span: Span, strs: Vec<Ident>) -> hir::Path {
|
2015-09-28 02:00:15 +00:00
|
|
|
path_all(span, false, strs, Vec::new(), Vec::new(), Vec::new())
|
|
|
|
}
|
|
|
|
|
2015-10-06 03:03:56 +00:00
|
|
|
fn path_global(span: Span, strs: Vec<Ident>) -> hir::Path {
|
2015-09-28 02:00:15 +00:00
|
|
|
path_all(span, true, strs, Vec::new(), Vec::new(), Vec::new())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn path_all(sp: Span,
|
|
|
|
global: bool,
|
2015-10-06 03:03:56 +00:00
|
|
|
mut idents: Vec<Ident>,
|
2015-09-28 02:00:15 +00:00
|
|
|
lifetimes: Vec<hir::Lifetime>,
|
|
|
|
types: Vec<P<hir::Ty>>,
|
2015-10-06 03:03:56 +00:00
|
|
|
bindings: Vec<P<hir::TypeBinding>>)
|
2015-09-28 02:00:15 +00:00
|
|
|
-> hir::Path {
|
|
|
|
let last_identifier = idents.pop().unwrap();
|
|
|
|
let mut segments: Vec<hir::PathSegment> = idents.into_iter()
|
|
|
|
.map(|ident| {
|
2015-10-06 03:03:56 +00:00
|
|
|
hir::PathSegment {
|
|
|
|
identifier: ident,
|
|
|
|
parameters: hir::PathParameters::none(),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2015-09-28 02:00:15 +00:00
|
|
|
segments.push(hir::PathSegment {
|
|
|
|
identifier: last_identifier,
|
|
|
|
parameters: hir::AngleBracketedParameters(hir::AngleBracketedParameterData {
|
|
|
|
lifetimes: lifetimes,
|
|
|
|
types: OwnedSlice::from_vec(types),
|
|
|
|
bindings: OwnedSlice::from_vec(bindings),
|
2015-10-06 03:03:56 +00:00
|
|
|
}),
|
2015-09-28 02:00:15 +00:00
|
|
|
});
|
|
|
|
hir::Path {
|
|
|
|
span: sp,
|
|
|
|
global: global,
|
|
|
|
segments: segments,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn std_path(lctx: &LoweringContext, components: &[&str]) -> Vec<Ident> {
|
|
|
|
let mut v = Vec::new();
|
|
|
|
if let Some(s) = lctx.crate_root {
|
|
|
|
v.push(str_to_ident(s));
|
|
|
|
}
|
|
|
|
v.extend(components.iter().map(|s| str_to_ident(s)));
|
2015-11-05 21:17:59 +00:00
|
|
|
return v;
|
2015-09-28 02:00:15 +00:00
|
|
|
}
|
2015-09-29 00:17:46 +00:00
|
|
|
|
|
|
|
// Given suffix ["b","c","d"], returns path `::std::b::c::d` when
|
|
|
|
// `fld.cx.use_std`, and `::core::b::c::d` otherwise.
|
|
|
|
fn core_path(lctx: &LoweringContext, span: Span, components: &[&str]) -> hir::Path {
|
|
|
|
let idents = std_path(lctx, components);
|
|
|
|
path_global(span, idents)
|
|
|
|
}
|
|
|
|
|
2015-10-06 03:03:56 +00:00
|
|
|
fn signal_block_expr(lctx: &LoweringContext,
|
|
|
|
stmts: Vec<P<hir::Stmt>>,
|
|
|
|
expr: P<hir::Expr>,
|
|
|
|
span: Span,
|
|
|
|
rule: hir::BlockCheckMode)
|
|
|
|
-> P<hir::Expr> {
|
2015-11-12 17:30:52 +00:00
|
|
|
let id = lctx.next_id();
|
2015-10-06 03:03:56 +00:00
|
|
|
expr_block(lctx,
|
|
|
|
P(hir::Block {
|
|
|
|
rules: rule,
|
|
|
|
span: span,
|
2015-11-12 17:30:52 +00:00
|
|
|
id: id,
|
2015-10-06 03:03:56 +00:00
|
|
|
stmts: stmts,
|
|
|
|
expr: Some(expr),
|
|
|
|
}))
|
2015-09-29 00:17:46 +00:00
|
|
|
}
|
2015-10-06 19:26:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
use syntax::ast::{self, NodeId, NodeIdAssigner};
|
|
|
|
use syntax::{parse, codemap};
|
|
|
|
use syntax::fold::Folder;
|
|
|
|
use std::cell::Cell;
|
|
|
|
|
|
|
|
struct MockAssigner {
|
|
|
|
next_id: Cell<NodeId>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MockAssigner {
|
|
|
|
fn new() -> MockAssigner {
|
2015-11-05 21:17:59 +00:00
|
|
|
MockAssigner { next_id: Cell::new(0) }
|
2015-10-06 19:26:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
trait FakeExtCtxt {
|
|
|
|
fn call_site(&self) -> codemap::Span;
|
|
|
|
fn cfg(&self) -> ast::CrateConfig;
|
|
|
|
fn ident_of(&self, st: &str) -> ast::Ident;
|
|
|
|
fn name_of(&self, st: &str) -> ast::Name;
|
|
|
|
fn parse_sess(&self) -> &parse::ParseSess;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FakeExtCtxt for parse::ParseSess {
|
|
|
|
fn call_site(&self) -> codemap::Span {
|
|
|
|
codemap::Span {
|
|
|
|
lo: codemap::BytePos(0),
|
|
|
|
hi: codemap::BytePos(0),
|
|
|
|
expn_id: codemap::NO_EXPANSION,
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 21:17:59 +00:00
|
|
|
fn cfg(&self) -> ast::CrateConfig {
|
|
|
|
Vec::new()
|
|
|
|
}
|
2015-10-06 19:26:22 +00:00
|
|
|
fn ident_of(&self, st: &str) -> ast::Ident {
|
|
|
|
parse::token::str_to_ident(st)
|
|
|
|
}
|
|
|
|
fn name_of(&self, st: &str) -> ast::Name {
|
|
|
|
parse::token::intern(st)
|
|
|
|
}
|
2015-11-05 21:17:59 +00:00
|
|
|
fn parse_sess(&self) -> &parse::ParseSess {
|
|
|
|
self
|
|
|
|
}
|
2015-10-06 19:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl NodeIdAssigner for MockAssigner {
|
|
|
|
fn next_node_id(&self) -> NodeId {
|
|
|
|
let result = self.next_id.get();
|
|
|
|
self.next_id.set(result + 1);
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
fn peek_node_id(&self) -> NodeId {
|
|
|
|
self.next_id.get()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Folder for MockAssigner {
|
|
|
|
fn new_id(&mut self, old_id: NodeId) -> NodeId {
|
|
|
|
assert_eq!(old_id, ast::DUMMY_NODE_ID);
|
|
|
|
self.next_node_id()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_preserves_ids() {
|
|
|
|
let cx = parse::ParseSess::new();
|
|
|
|
let mut assigner = MockAssigner::new();
|
|
|
|
|
2015-11-05 21:17:59 +00:00
|
|
|
let ast_if_let = quote_expr!(&cx,
|
|
|
|
if let Some(foo) = baz {
|
|
|
|
bar(foo);
|
|
|
|
});
|
2015-10-06 19:26:22 +00:00
|
|
|
let ast_if_let = assigner.fold_expr(ast_if_let);
|
2015-11-05 21:17:59 +00:00
|
|
|
let ast_while_let = quote_expr!(&cx,
|
|
|
|
while let Some(foo) = baz {
|
|
|
|
bar(foo);
|
|
|
|
});
|
2015-10-06 19:26:22 +00:00
|
|
|
let ast_while_let = assigner.fold_expr(ast_while_let);
|
2015-11-05 21:17:59 +00:00
|
|
|
let ast_for = quote_expr!(&cx,
|
|
|
|
for i in 0..10 {
|
|
|
|
foo(i);
|
|
|
|
});
|
2015-10-06 19:26:22 +00:00
|
|
|
let ast_for = assigner.fold_expr(ast_for);
|
|
|
|
let ast_in = quote_expr!(&cx, in HEAP { foo() });
|
|
|
|
let ast_in = assigner.fold_expr(ast_in);
|
|
|
|
|
|
|
|
let lctx = LoweringContext::new(&assigner, None);
|
|
|
|
let hir1 = lower_expr(&lctx, &ast_if_let);
|
|
|
|
let hir2 = lower_expr(&lctx, &ast_if_let);
|
|
|
|
assert!(hir1 == hir2);
|
|
|
|
|
|
|
|
let hir1 = lower_expr(&lctx, &ast_while_let);
|
|
|
|
let hir2 = lower_expr(&lctx, &ast_while_let);
|
|
|
|
assert!(hir1 == hir2);
|
|
|
|
|
|
|
|
let hir1 = lower_expr(&lctx, &ast_for);
|
|
|
|
let hir2 = lower_expr(&lctx, &ast_for);
|
|
|
|
assert!(hir1 == hir2);
|
|
|
|
|
|
|
|
let hir1 = lower_expr(&lctx, &ast_in);
|
|
|
|
let hir2 = lower_expr(&lctx, &ast_in);
|
|
|
|
assert!(hir1 == hir2);
|
|
|
|
}
|
|
|
|
}
|