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.
13 // Since the AST and HIR are fairly similar, this is mostly a simple procedure,
14 // much like a fold. Where lowering involves a bit more work things get more
15 // interesting and there are some invariants you should know about. These mostly
16 // concern spans and ids.
18 // Spans are assigned to AST nodes during parsing and then are modified during
19 // expansion to indicate the origin of a node and the process it went through
20 // being expanded. Ids are assigned to AST nodes just before lowering.
22 // For the simpler lowering steps, ids and spans should be preserved. Unlike
23 // expansion we do not preserve the process of lowering in the spans, so spans
24 // should not be modified here. When creating a new node (as opposed to
25 // 'folding' an existing one), then you create a new id using `next_id()`.
27 // You must ensure that ids are unique. That means that you should only use the
28 // id from an AST node in a single HIR node (you can assume that AST node ids
29 // are unique). Every new node must have a unique id. Avoid cloning HIR nodes.
30 // If you do, you must then set the new node's id to a fresh one.
32 // Lowering must be reproducable (the compiler only lowers once, but tools and
33 // custom lints may lower an AST node to a HIR node to interact with the
34 // compiler). The most interesting bit of this is ids - if you lower an AST node
35 // and create new HIR nodes with fresh ids, when re-lowering the same node, you
36 // must ensure you get the same ids! To do this, we keep track of the next id
37 // when we translate a node which requires new ids. By checking this cache and
38 // using node ids starting with the cached id, we ensure ids are reproducible.
39 // To use this system, you just need to hold on to a CachedIdSetter object
40 // whilst lowering. This is an RAII object that takes care of setting and
41 // restoring the cached id, etc.
43 // This whole system relies on node ids being incremented one at a time and
44 // all increments being for lowering. This means that you should not call any
45 // non-lowering function which will use new node ids.
47 // We must also cache gensym'ed Idents to ensure that we get the same Ident
48 // every time we lower a node with gensym'ed names. One consequence of this is
49 // that you can only gensym a name once in a lowering (you don't need to worry
50 // about nested lowering though). That's because we cache based on the name and
51 // the currently cached node id, which is unique per lowered node.
53 // Spans are used for error messages and for tools to map semantics back to
54 // source code. It is therefore not as important with spans as ids to be strict
55 // about use (you can't break the compiler by screwing up a span). Obviously, a
56 // HIR node can only have a single span. But multiple nodes can have the same
57 // span and spans don't need to be kept in order, etc. Where code is preserved
58 // by lowering, it should have the same span as in the AST. Where HIR nodes are
59 // new it is probably best to give a span for the whole AST node being lowered.
60 // All nodes should have real spans, don't use dummy spans. Tools are likely to
61 // get confused if the spans from leaf AST nodes occur in multiple places
62 // in the HIR, especially for multiple identifiers.
66 use std::collections::BTreeMap;
67 use std::collections::HashMap;
70 use syntax::attr::{ThinAttributes, ThinAttributesExt};
71 use syntax::ext::mtwt;
73 use syntax::codemap::{respan, Spanned, Span};
74 use syntax::parse::token;
75 use syntax::std_inject;
76 use syntax::visit::{self, Visitor};
78 use std::cell::{Cell, RefCell};
80 pub struct LoweringContext<'a> {
81 crate_root: Option<&'static str>,
82 // Map AST ids to ids used for expanded nodes.
83 id_cache: RefCell<HashMap<NodeId, NodeId>>,
84 // Use if there are no cached ids for the current node.
85 id_assigner: &'a NodeIdAssigner,
86 // 0 == no cached id. Must be incremented to align with previous id
89 // Keep track of gensym'ed idents.
90 gensym_cache: RefCell<HashMap<(NodeId, &'static str), hir::Ident>>,
91 // A copy of cached_id, but is also set to an id while a node is lowered for
93 gensym_key: Cell<u32>,
96 impl<'a, 'hir> LoweringContext<'a> {
97 pub fn new(id_assigner: &'a NodeIdAssigner, c: Option<&Crate>) -> LoweringContext<'a> {
98 let crate_root = c.and_then(|c| {
99 if std_inject::no_core(c) {
101 } else if std_inject::no_std(c) {
109 crate_root: crate_root,
110 id_cache: RefCell::new(HashMap::new()),
111 id_assigner: id_assigner,
112 cached_id: Cell::new(0),
113 gensym_cache: RefCell::new(HashMap::new()),
114 gensym_key: Cell::new(0),
118 fn next_id(&self) -> NodeId {
119 let cached_id = self.cached_id.get();
121 return self.id_assigner.next_node_id();
124 self.cached_id.set(cached_id + 1);
128 fn str_to_ident(&self, s: &'static str) -> hir::Ident {
129 let gensym_key = self.gensym_key.get();
131 return hir::Ident::from_name(token::gensym(s));
134 let cached = self.gensym_cache.borrow().contains_key(&(gensym_key, s));
136 self.gensym_cache.borrow()[&(gensym_key, s)]
138 let result = hir::Ident::from_name(token::gensym(s));
139 self.gensym_cache.borrow_mut().insert((gensym_key, s), result);
145 // Utility fn for setting and unsetting the cached id.
146 fn cache_ids<'a, OP, R>(lctx: &LoweringContext, expr_id: NodeId, op: OP) -> R
147 where OP: FnOnce(&LoweringContext) -> R
149 // Only reset the id if it was previously 0, i.e., was not cached.
150 // If it was cached, we are in a nested node, but our id count will
151 // still count towards the parent's count.
152 let reset_cached_id = lctx.cached_id.get() == 0;
153 // We always reset gensym_key so that if we use the same name in a nested
154 // node and after that node, they get different values.
155 let old_gensym_key = lctx.gensym_key.get();
158 let id_cache: &mut HashMap<_, _> = &mut lctx.id_cache.borrow_mut();
160 if id_cache.contains_key(&expr_id) {
161 let cached_id = lctx.cached_id.get();
163 // We're entering a node where we need to track ids, but are not
165 lctx.cached_id.set(id_cache[&expr_id]);
167 // We're already tracking - check that the tracked id is the same
168 // as the expected id.
169 assert!(cached_id == id_cache[&expr_id], "id mismatch");
171 lctx.gensym_key.set(id_cache[&expr_id]);
173 // We've never lowered this node before, remember it for next time.
174 let next_id = lctx.id_assigner.peek_node_id();
175 id_cache.insert(expr_id, next_id);
176 lctx.gensym_key.set(next_id);
177 // self.cached_id is not set when we lower a node for the first time,
178 // only on re-lowering.
182 let result = op(lctx);
185 lctx.cached_id.set(0);
187 lctx.gensym_key.set(old_gensym_key);
192 pub fn lower_ident(_lctx: &LoweringContext, ident: Ident) -> hir::Ident {
194 name: mtwt::resolve(ident),
195 unhygienic_name: ident.name,
199 pub fn lower_attrs(_lctx: &LoweringContext, attrs: &Vec<Attribute>) -> hir::HirVec<Attribute> {
203 pub fn lower_view_path(lctx: &LoweringContext, view_path: &ViewPath) -> P<hir::ViewPath> {
205 node: match view_path.node {
206 ViewPathSimple(ident, ref path) => {
207 hir::ViewPathSimple(ident.name, lower_path(lctx, path))
209 ViewPathGlob(ref path) => {
210 hir::ViewPathGlob(lower_path(lctx, path))
212 ViewPathList(ref path, ref path_list_idents) => {
213 hir::ViewPathList(lower_path(lctx, path),
214 path_list_idents.iter()
215 .map(lower_path_list_item)
219 span: view_path.span,
223 fn lower_path_list_item(path_list_ident: &PathListItem) -> hir::PathListItem {
225 node: match path_list_ident.node {
226 PathListItemKind::Ident { id, name, rename } => hir::PathListIdent {
229 rename: rename.map(|x| x.name),
231 PathListItemKind::Mod { id, rename } => hir::PathListMod {
233 rename: rename.map(|x| x.name),
236 span: path_list_ident.span,
240 pub fn lower_arm(lctx: &LoweringContext, arm: &Arm) -> hir::Arm {
242 attrs: lower_attrs(lctx, &arm.attrs),
243 pats: arm.pats.iter().map(|x| lower_pat(lctx, x)).collect(),
244 guard: arm.guard.as_ref().map(|ref x| lower_expr(lctx, x)),
245 body: lower_expr(lctx, &arm.body),
249 pub fn lower_decl(lctx: &LoweringContext, d: &Decl) -> P<hir::Decl> {
251 DeclKind::Local(ref l) => P(Spanned {
252 node: hir::DeclLocal(lower_local(lctx, l)),
255 DeclKind::Item(ref it) => P(Spanned {
256 node: hir::DeclItem(lower_item_id(lctx, it)),
262 pub fn lower_ty_binding(lctx: &LoweringContext, b: &TypeBinding) -> hir::TypeBinding {
266 ty: lower_ty(lctx, &b.ty),
271 pub fn lower_ty(lctx: &LoweringContext, t: &Ty) -> P<hir::Ty> {
272 use syntax::ast::TyKind::*;
276 Infer => hir::TyInfer,
277 Vec(ref ty) => hir::TyVec(lower_ty(lctx, ty)),
278 Ptr(ref mt) => hir::TyPtr(lower_mt(lctx, mt)),
279 Rptr(ref region, ref mt) => {
280 hir::TyRptr(lower_opt_lifetime(lctx, region), lower_mt(lctx, mt))
283 hir::TyBareFn(P(hir::BareFnTy {
284 lifetimes: lower_lifetime_defs(lctx, &f.lifetimes),
285 unsafety: lower_unsafety(lctx, f.unsafety),
287 decl: lower_fn_decl(lctx, &f.decl),
290 Tup(ref tys) => hir::TyTup(tys.iter().map(|ty| lower_ty(lctx, ty)).collect()),
292 return lower_ty(lctx, ty);
294 Path(ref qself, ref path) => {
295 let qself = qself.as_ref().map(|&QSelf { ref ty, position }| {
297 ty: lower_ty(lctx, ty),
301 hir::TyPath(qself, lower_path(lctx, path))
303 ObjectSum(ref ty, ref bounds) => {
304 hir::TyObjectSum(lower_ty(lctx, ty), lower_bounds(lctx, bounds))
306 FixedLengthVec(ref ty, ref e) => {
307 hir::TyFixedLengthVec(lower_ty(lctx, ty), lower_expr(lctx, e))
309 Typeof(ref expr) => {
310 hir::TyTypeof(lower_expr(lctx, expr))
312 PolyTraitRef(ref bounds) => {
313 hir::TyPolyTraitRef(bounds.iter().map(|b| lower_ty_param_bound(lctx, b)).collect())
315 Mac(_) => panic!("TyMac should have been expanded by now."),
321 pub fn lower_foreign_mod(lctx: &LoweringContext, fm: &ForeignMod) -> hir::ForeignMod {
324 items: fm.items.iter().map(|x| lower_foreign_item(lctx, x)).collect(),
328 pub fn lower_variant(lctx: &LoweringContext, v: &Variant) -> hir::Variant {
330 node: hir::Variant_ {
331 name: v.node.name.name,
332 attrs: lower_attrs(lctx, &v.node.attrs),
333 data: lower_variant_data(lctx, &v.node.data),
334 disr_expr: v.node.disr_expr.as_ref().map(|e| lower_expr(lctx, e)),
340 // Path segments are usually unhygienic, hygienic path segments can occur only in
341 // identifier-like paths originating from `ExprPath`.
342 // Make life simpler for rustc_resolve by renaming only such segments.
343 pub fn lower_path_full(lctx: &LoweringContext, p: &Path, maybe_hygienic: bool) -> hir::Path {
344 let maybe_hygienic = maybe_hygienic && !p.global && p.segments.len() == 1;
349 .map(|&PathSegment { identifier, ref parameters }| {
351 identifier: if maybe_hygienic {
352 lower_ident(lctx, identifier)
354 hir::Ident::from_name(identifier.name)
356 parameters: lower_path_parameters(lctx, parameters),
364 pub fn lower_path(lctx: &LoweringContext, p: &Path) -> hir::Path {
365 lower_path_full(lctx, p, false)
368 pub fn lower_path_parameters(lctx: &LoweringContext,
369 path_parameters: &PathParameters)
370 -> hir::PathParameters {
371 match *path_parameters {
372 PathParameters::AngleBracketed(ref data) =>
373 hir::AngleBracketedParameters(lower_angle_bracketed_parameter_data(lctx, data)),
374 PathParameters::Parenthesized(ref data) =>
375 hir::ParenthesizedParameters(lower_parenthesized_parameter_data(lctx, data)),
379 pub fn lower_angle_bracketed_parameter_data(lctx: &LoweringContext,
380 data: &AngleBracketedParameterData)
381 -> hir::AngleBracketedParameterData {
382 let &AngleBracketedParameterData { ref lifetimes, ref types, ref bindings } = data;
383 hir::AngleBracketedParameterData {
384 lifetimes: lower_lifetimes(lctx, lifetimes),
385 types: types.iter().map(|ty| lower_ty(lctx, ty)).collect(),
386 bindings: bindings.iter().map(|b| lower_ty_binding(lctx, b)).collect(),
390 pub fn lower_parenthesized_parameter_data(lctx: &LoweringContext,
391 data: &ParenthesizedParameterData)
392 -> hir::ParenthesizedParameterData {
393 let &ParenthesizedParameterData { ref inputs, ref output, span } = data;
394 hir::ParenthesizedParameterData {
395 inputs: inputs.iter().map(|ty| lower_ty(lctx, ty)).collect(),
396 output: output.as_ref().map(|ty| lower_ty(lctx, ty)),
401 pub fn lower_local(lctx: &LoweringContext, l: &Local) -> P<hir::Local> {
404 ty: l.ty.as_ref().map(|t| lower_ty(lctx, t)),
405 pat: lower_pat(lctx, &l.pat),
406 init: l.init.as_ref().map(|e| lower_expr(lctx, e)),
408 attrs: l.attrs.clone(),
412 pub fn lower_explicit_self_underscore(lctx: &LoweringContext,
414 -> hir::ExplicitSelf_ {
416 SelfKind::Static => hir::SelfStatic,
417 SelfKind::Value(v) => hir::SelfValue(v.name),
418 SelfKind::Region(ref lifetime, m, ident) => {
419 hir::SelfRegion(lower_opt_lifetime(lctx, lifetime),
420 lower_mutability(lctx, m),
423 SelfKind::Explicit(ref typ, ident) => {
424 hir::SelfExplicit(lower_ty(lctx, typ), ident.name)
429 pub fn lower_mutability(_lctx: &LoweringContext, m: Mutability) -> hir::Mutability {
431 Mutability::Mutable => hir::MutMutable,
432 Mutability::Immutable => hir::MutImmutable,
436 pub fn lower_explicit_self(lctx: &LoweringContext, s: &ExplicitSelf) -> hir::ExplicitSelf {
438 node: lower_explicit_self_underscore(lctx, &s.node),
443 pub fn lower_arg(lctx: &LoweringContext, arg: &Arg) -> hir::Arg {
446 pat: lower_pat(lctx, &arg.pat),
447 ty: lower_ty(lctx, &arg.ty),
451 pub fn lower_fn_decl(lctx: &LoweringContext, decl: &FnDecl) -> P<hir::FnDecl> {
453 inputs: decl.inputs.iter().map(|x| lower_arg(lctx, x)).collect(),
454 output: match decl.output {
455 FunctionRetTy::Ty(ref ty) => hir::Return(lower_ty(lctx, ty)),
456 FunctionRetTy::Default(span) => hir::DefaultReturn(span),
457 FunctionRetTy::None(span) => hir::NoReturn(span),
459 variadic: decl.variadic,
463 pub fn lower_ty_param_bound(lctx: &LoweringContext, tpb: &TyParamBound) -> hir::TyParamBound {
465 TraitTyParamBound(ref ty, modifier) => {
466 hir::TraitTyParamBound(lower_poly_trait_ref(lctx, ty),
467 lower_trait_bound_modifier(lctx, modifier))
469 RegionTyParamBound(ref lifetime) => {
470 hir::RegionTyParamBound(lower_lifetime(lctx, lifetime))
475 pub fn lower_ty_param(lctx: &LoweringContext, tp: &TyParam) -> hir::TyParam {
479 bounds: lower_bounds(lctx, &tp.bounds),
480 default: tp.default.as_ref().map(|x| lower_ty(lctx, x)),
485 pub fn lower_ty_params(lctx: &LoweringContext,
487 -> hir::HirVec<hir::TyParam> {
488 tps.iter().map(|tp| lower_ty_param(lctx, tp)).collect()
491 pub fn lower_lifetime(_lctx: &LoweringContext, l: &Lifetime) -> hir::Lifetime {
499 pub fn lower_lifetime_def(lctx: &LoweringContext, l: &LifetimeDef) -> hir::LifetimeDef {
501 lifetime: lower_lifetime(lctx, &l.lifetime),
502 bounds: lower_lifetimes(lctx, &l.bounds),
506 pub fn lower_lifetimes(lctx: &LoweringContext, lts: &Vec<Lifetime>) -> hir::HirVec<hir::Lifetime> {
507 lts.iter().map(|l| lower_lifetime(lctx, l)).collect()
510 pub fn lower_lifetime_defs(lctx: &LoweringContext,
511 lts: &Vec<LifetimeDef>)
512 -> hir::HirVec<hir::LifetimeDef> {
513 lts.iter().map(|l| lower_lifetime_def(lctx, l)).collect()
516 pub fn lower_opt_lifetime(lctx: &LoweringContext,
517 o_lt: &Option<Lifetime>)
518 -> Option<hir::Lifetime> {
519 o_lt.as_ref().map(|lt| lower_lifetime(lctx, lt))
522 pub fn lower_generics(lctx: &LoweringContext, g: &Generics) -> hir::Generics {
524 ty_params: lower_ty_params(lctx, &g.ty_params),
525 lifetimes: lower_lifetime_defs(lctx, &g.lifetimes),
526 where_clause: lower_where_clause(lctx, &g.where_clause),
530 pub fn lower_where_clause(lctx: &LoweringContext, wc: &WhereClause) -> hir::WhereClause {
533 predicates: wc.predicates
535 .map(|predicate| lower_where_predicate(lctx, predicate))
540 pub fn lower_where_predicate(lctx: &LoweringContext,
541 pred: &WherePredicate)
542 -> hir::WherePredicate {
544 WherePredicate::BoundPredicate(WhereBoundPredicate{ ref bound_lifetimes,
548 hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
549 bound_lifetimes: lower_lifetime_defs(lctx, bound_lifetimes),
550 bounded_ty: lower_ty(lctx, bounded_ty),
551 bounds: bounds.iter().map(|x| lower_ty_param_bound(lctx, x)).collect(),
555 WherePredicate::RegionPredicate(WhereRegionPredicate{ ref lifetime,
558 hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
560 lifetime: lower_lifetime(lctx, lifetime),
561 bounds: bounds.iter().map(|bound| lower_lifetime(lctx, bound)).collect(),
564 WherePredicate::EqPredicate(WhereEqPredicate{ id,
568 hir::WherePredicate::EqPredicate(hir::WhereEqPredicate {
570 path: lower_path(lctx, path),
571 ty: lower_ty(lctx, ty),
578 pub fn lower_variant_data(lctx: &LoweringContext, vdata: &VariantData) -> hir::VariantData {
580 VariantData::Struct(ref fields, id) => {
581 hir::VariantData::Struct(fields.iter()
583 .map(|f| lower_struct_field(lctx, f))
587 VariantData::Tuple(ref fields, id) => {
588 hir::VariantData::Tuple(fields.iter()
590 .map(|f| lower_struct_field(lctx, f))
594 VariantData::Unit(id) => hir::VariantData::Unit(id),
598 pub fn lower_trait_ref(lctx: &LoweringContext, p: &TraitRef) -> hir::TraitRef {
600 path: lower_path(lctx, &p.path),
605 pub fn lower_poly_trait_ref(lctx: &LoweringContext, p: &PolyTraitRef) -> hir::PolyTraitRef {
607 bound_lifetimes: lower_lifetime_defs(lctx, &p.bound_lifetimes),
608 trait_ref: lower_trait_ref(lctx, &p.trait_ref),
613 pub fn lower_struct_field(lctx: &LoweringContext,
614 (index, f): (usize, &StructField))
615 -> hir::StructField {
619 name: f.node.ident().map(|ident| ident.name)
620 .unwrap_or(token::intern(&index.to_string())),
621 vis: lower_visibility(lctx, f.node.kind.visibility()),
622 ty: lower_ty(lctx, &f.node.ty),
623 attrs: lower_attrs(lctx, &f.node.attrs),
627 pub fn lower_field(lctx: &LoweringContext, f: &Field) -> hir::Field {
629 name: respan(f.ident.span, f.ident.node.name),
630 expr: lower_expr(lctx, &f.expr),
635 pub fn lower_mt(lctx: &LoweringContext, mt: &MutTy) -> hir::MutTy {
637 ty: lower_ty(lctx, &mt.ty),
638 mutbl: lower_mutability(lctx, mt.mutbl),
642 pub fn lower_opt_bounds(lctx: &LoweringContext,
643 b: &Option<TyParamBounds>)
644 -> Option<hir::TyParamBounds> {
645 b.as_ref().map(|ref bounds| lower_bounds(lctx, bounds))
648 fn lower_bounds(lctx: &LoweringContext, bounds: &TyParamBounds) -> hir::TyParamBounds {
649 bounds.iter().map(|bound| lower_ty_param_bound(lctx, bound)).collect()
652 pub fn lower_block(lctx: &LoweringContext, b: &Block) -> P<hir::Block> {
655 stmts: b.stmts.iter().map(|s| lower_stmt(lctx, s)).collect(),
656 expr: b.expr.as_ref().map(|ref x| lower_expr(lctx, x)),
657 rules: lower_block_check_mode(lctx, &b.rules),
662 pub fn lower_item_kind(lctx: &LoweringContext, i: &ItemKind) -> hir::Item_ {
664 ItemKind::ExternCrate(string) => hir::ItemExternCrate(string),
665 ItemKind::Use(ref view_path) => {
666 hir::ItemUse(lower_view_path(lctx, view_path))
668 ItemKind::Static(ref t, m, ref e) => {
669 hir::ItemStatic(lower_ty(lctx, t),
670 lower_mutability(lctx, m),
673 ItemKind::Const(ref t, ref e) => {
674 hir::ItemConst(lower_ty(lctx, t), lower_expr(lctx, e))
676 ItemKind::Fn(ref decl, unsafety, constness, abi, ref generics, ref body) => {
677 hir::ItemFn(lower_fn_decl(lctx, decl),
678 lower_unsafety(lctx, unsafety),
679 lower_constness(lctx, constness),
681 lower_generics(lctx, generics),
682 lower_block(lctx, body))
684 ItemKind::Mod(ref m) => hir::ItemMod(lower_mod(lctx, m)),
685 ItemKind::ForeignMod(ref nm) => hir::ItemForeignMod(lower_foreign_mod(lctx, nm)),
686 ItemKind::Ty(ref t, ref generics) => {
687 hir::ItemTy(lower_ty(lctx, t), lower_generics(lctx, generics))
689 ItemKind::Enum(ref enum_definition, ref generics) => {
690 hir::ItemEnum(hir::EnumDef {
691 variants: enum_definition.variants
693 .map(|x| lower_variant(lctx, x))
696 lower_generics(lctx, generics))
698 ItemKind::Struct(ref struct_def, ref generics) => {
699 let struct_def = lower_variant_data(lctx, struct_def);
700 hir::ItemStruct(struct_def, lower_generics(lctx, generics))
702 ItemKind::DefaultImpl(unsafety, ref trait_ref) => {
703 hir::ItemDefaultImpl(lower_unsafety(lctx, unsafety),
704 lower_trait_ref(lctx, trait_ref))
706 ItemKind::Impl(unsafety, polarity, ref generics, ref ifce, ref ty, ref impl_items) => {
707 let new_impl_items = impl_items.iter()
708 .map(|item| lower_impl_item(lctx, item))
710 let ifce = ifce.as_ref().map(|trait_ref| lower_trait_ref(lctx, trait_ref));
711 hir::ItemImpl(lower_unsafety(lctx, unsafety),
712 lower_impl_polarity(lctx, polarity),
713 lower_generics(lctx, generics),
718 ItemKind::Trait(unsafety, ref generics, ref bounds, ref items) => {
719 let bounds = lower_bounds(lctx, bounds);
720 let items = items.iter().map(|item| lower_trait_item(lctx, item)).collect();
721 hir::ItemTrait(lower_unsafety(lctx, unsafety),
722 lower_generics(lctx, generics),
726 ItemKind::Mac(_) => panic!("Shouldn't still be around"),
730 pub fn lower_trait_item(lctx: &LoweringContext, i: &TraitItem) -> hir::TraitItem {
734 attrs: lower_attrs(lctx, &i.attrs),
736 TraitItemKind::Const(ref ty, ref default) => {
737 hir::ConstTraitItem(lower_ty(lctx, ty),
738 default.as_ref().map(|x| lower_expr(lctx, x)))
740 TraitItemKind::Method(ref sig, ref body) => {
741 hir::MethodTraitItem(lower_method_sig(lctx, sig),
742 body.as_ref().map(|x| lower_block(lctx, x)))
744 TraitItemKind::Type(ref bounds, ref default) => {
745 hir::TypeTraitItem(lower_bounds(lctx, bounds),
746 default.as_ref().map(|x| lower_ty(lctx, x)))
753 pub fn lower_impl_item(lctx: &LoweringContext, i: &ImplItem) -> hir::ImplItem {
757 attrs: lower_attrs(lctx, &i.attrs),
758 vis: lower_visibility(lctx, i.vis),
759 defaultness: lower_defaultness(lctx, i.defaultness),
761 ImplItemKind::Const(ref ty, ref expr) => {
762 hir::ImplItemKind::Const(lower_ty(lctx, ty), lower_expr(lctx, expr))
764 ImplItemKind::Method(ref sig, ref body) => {
765 hir::ImplItemKind::Method(lower_method_sig(lctx, sig), lower_block(lctx, body))
767 ImplItemKind::Type(ref ty) => hir::ImplItemKind::Type(lower_ty(lctx, ty)),
768 ImplItemKind::Macro(..) => panic!("Shouldn't exist any more"),
774 pub fn lower_mod(lctx: &LoweringContext, m: &Mod) -> hir::Mod {
777 item_ids: m.items.iter().map(|x| lower_item_id(lctx, x)).collect(),
781 struct ItemLowerer<'lcx, 'interner: 'lcx> {
782 items: BTreeMap<NodeId, hir::Item>,
783 lctx: &'lcx LoweringContext<'interner>,
786 impl<'lcx, 'interner> Visitor<'lcx> for ItemLowerer<'lcx, 'interner> {
787 fn visit_item(&mut self, item: &'lcx Item) {
788 self.items.insert(item.id, lower_item(self.lctx, item));
789 visit::walk_item(self, item);
793 pub fn lower_crate(lctx: &LoweringContext, c: &Crate) -> hir::Crate {
795 let mut item_lowerer = ItemLowerer { items: BTreeMap::new(), lctx: lctx };
796 visit::walk_crate(&mut item_lowerer, c);
801 module: lower_mod(lctx, &c.module),
802 attrs: lower_attrs(lctx, &c.attrs),
803 config: c.config.clone().into(),
805 exported_macros: c.exported_macros.iter().map(|m| lower_macro_def(lctx, m)).collect(),
810 pub fn lower_macro_def(lctx: &LoweringContext, m: &MacroDef) -> hir::MacroDef {
813 attrs: lower_attrs(lctx, &m.attrs),
816 imported_from: m.imported_from.map(|x| x.name),
818 use_locally: m.use_locally,
819 allow_internal_unstable: m.allow_internal_unstable,
820 body: m.body.clone().into(),
824 pub fn lower_item_id(_lctx: &LoweringContext, i: &Item) -> hir::ItemId {
825 hir::ItemId { id: i.id }
828 pub fn lower_item(lctx: &LoweringContext, i: &Item) -> hir::Item {
829 let node = lower_item_kind(lctx, &i.node);
834 attrs: lower_attrs(lctx, &i.attrs),
836 vis: lower_visibility(lctx, i.vis),
841 pub fn lower_foreign_item(lctx: &LoweringContext, i: &ForeignItem) -> hir::ForeignItem {
845 attrs: lower_attrs(lctx, &i.attrs),
847 ForeignItemKind::Fn(ref fdec, ref generics) => {
848 hir::ForeignItemFn(lower_fn_decl(lctx, fdec), lower_generics(lctx, generics))
850 ForeignItemKind::Static(ref t, m) => {
851 hir::ForeignItemStatic(lower_ty(lctx, t), m)
854 vis: lower_visibility(lctx, i.vis),
859 pub fn lower_method_sig(lctx: &LoweringContext, sig: &MethodSig) -> hir::MethodSig {
861 generics: lower_generics(lctx, &sig.generics),
863 explicit_self: lower_explicit_self(lctx, &sig.explicit_self),
864 unsafety: lower_unsafety(lctx, sig.unsafety),
865 constness: lower_constness(lctx, sig.constness),
866 decl: lower_fn_decl(lctx, &sig.decl),
870 pub fn lower_unsafety(_lctx: &LoweringContext, u: Unsafety) -> hir::Unsafety {
872 Unsafety::Unsafe => hir::Unsafety::Unsafe,
873 Unsafety::Normal => hir::Unsafety::Normal,
877 pub fn lower_constness(_lctx: &LoweringContext, c: Constness) -> hir::Constness {
879 Constness::Const => hir::Constness::Const,
880 Constness::NotConst => hir::Constness::NotConst,
884 pub fn lower_unop(_lctx: &LoweringContext, u: UnOp) -> hir::UnOp {
886 UnOp::Deref => hir::UnDeref,
887 UnOp::Not => hir::UnNot,
888 UnOp::Neg => hir::UnNeg,
892 pub fn lower_binop(_lctx: &LoweringContext, b: BinOp) -> hir::BinOp {
895 BinOpKind::Add => hir::BiAdd,
896 BinOpKind::Sub => hir::BiSub,
897 BinOpKind::Mul => hir::BiMul,
898 BinOpKind::Div => hir::BiDiv,
899 BinOpKind::Rem => hir::BiRem,
900 BinOpKind::And => hir::BiAnd,
901 BinOpKind::Or => hir::BiOr,
902 BinOpKind::BitXor => hir::BiBitXor,
903 BinOpKind::BitAnd => hir::BiBitAnd,
904 BinOpKind::BitOr => hir::BiBitOr,
905 BinOpKind::Shl => hir::BiShl,
906 BinOpKind::Shr => hir::BiShr,
907 BinOpKind::Eq => hir::BiEq,
908 BinOpKind::Lt => hir::BiLt,
909 BinOpKind::Le => hir::BiLe,
910 BinOpKind::Ne => hir::BiNe,
911 BinOpKind::Ge => hir::BiGe,
912 BinOpKind::Gt => hir::BiGt,
918 pub fn lower_pat(lctx: &LoweringContext, p: &Pat) -> P<hir::Pat> {
922 PatKind::Wild => hir::PatKind::Wild,
923 PatKind::Ident(ref binding_mode, pth1, ref sub) => {
924 hir::PatKind::Ident(lower_binding_mode(lctx, binding_mode),
925 respan(pth1.span, lower_ident(lctx, pth1.node)),
926 sub.as_ref().map(|x| lower_pat(lctx, x)))
928 PatKind::Lit(ref e) => hir::PatKind::Lit(lower_expr(lctx, e)),
929 PatKind::TupleStruct(ref pth, ref pats) => {
930 hir::PatKind::TupleStruct(lower_path(lctx, pth),
932 .map(|pats| pats.iter().map(|x| lower_pat(lctx, x)).collect()))
934 PatKind::Path(ref pth) => {
935 hir::PatKind::Path(lower_path(lctx, pth))
937 PatKind::QPath(ref qself, ref pth) => {
938 let qself = hir::QSelf {
939 ty: lower_ty(lctx, &qself.ty),
940 position: qself.position,
942 hir::PatKind::QPath(qself, lower_path(lctx, pth))
944 PatKind::Struct(ref pth, ref fields, etc) => {
945 let pth = lower_path(lctx, pth);
946 let fs = fields.iter()
950 node: hir::FieldPat {
951 name: f.node.ident.name,
952 pat: lower_pat(lctx, &f.node.pat),
953 is_shorthand: f.node.is_shorthand,
958 hir::PatKind::Struct(pth, fs, etc)
960 PatKind::Tup(ref elts) => {
961 hir::PatKind::Tup(elts.iter().map(|x| lower_pat(lctx, x)).collect())
963 PatKind::Box(ref inner) => hir::PatKind::Box(lower_pat(lctx, inner)),
964 PatKind::Ref(ref inner, mutbl) => {
965 hir::PatKind::Ref(lower_pat(lctx, inner), lower_mutability(lctx, mutbl))
967 PatKind::Range(ref e1, ref e2) => {
968 hir::PatKind::Range(lower_expr(lctx, e1), lower_expr(lctx, e2))
970 PatKind::Vec(ref before, ref slice, ref after) => {
971 hir::PatKind::Vec(before.iter().map(|x| lower_pat(lctx, x)).collect(),
972 slice.as_ref().map(|x| lower_pat(lctx, x)),
973 after.iter().map(|x| lower_pat(lctx, x)).collect())
975 PatKind::Mac(_) => panic!("Shouldn't exist here"),
981 pub fn lower_expr(lctx: &LoweringContext, e: &Expr) -> P<hir::Expr> {
986 // Eventually a desugaring for `box EXPR`
987 // (similar to the desugaring above for `in PLACE BLOCK`)
988 // should go here, desugaring
992 // let mut place = BoxPlace::make_place();
993 // let raw_place = Place::pointer(&mut place);
994 // let value = $value;
996 // ::std::ptr::write(raw_place, value);
997 // Boxed::finalize(place)
1000 // But for now there are type-inference issues doing that.
1001 ExprKind::Box(ref e) => {
1002 hir::ExprBox(lower_expr(lctx, e))
1005 // Desugar ExprBox: `in (PLACE) EXPR`
1006 ExprKind::InPlace(ref placer, ref value_expr) => {
1010 // let mut place = Placer::make_place(p);
1011 // let raw_place = Place::pointer(&mut place);
1013 // std::intrinsics::move_val_init(raw_place, pop_unsafe!( EXPR ));
1014 // InPlace::finalize(place)
1016 return cache_ids(lctx, e.id, |lctx| {
1017 let placer_expr = lower_expr(lctx, placer);
1018 let value_expr = lower_expr(lctx, value_expr);
1020 let placer_ident = lctx.str_to_ident("placer");
1021 let place_ident = lctx.str_to_ident("place");
1022 let p_ptr_ident = lctx.str_to_ident("p_ptr");
1024 let make_place = ["ops", "Placer", "make_place"];
1025 let place_pointer = ["ops", "Place", "pointer"];
1026 let move_val_init = ["intrinsics", "move_val_init"];
1027 let inplace_finalize = ["ops", "InPlace", "finalize"];
1029 let make_call = |lctx: &LoweringContext, p, args| {
1030 let path = core_path(lctx, e.span, p);
1031 let path = expr_path(lctx, path, None);
1032 expr_call(lctx, e.span, path, args, None)
1035 let mk_stmt_let = |lctx: &LoweringContext, bind, expr| {
1036 stmt_let(lctx, e.span, false, bind, expr, None)
1039 let mk_stmt_let_mut = |lctx: &LoweringContext, bind, expr| {
1040 stmt_let(lctx, e.span, true, bind, expr, None)
1043 // let placer = <placer_expr> ;
1045 let placer_expr = signal_block_expr(lctx,
1049 hir::PopUnstableBlock,
1051 mk_stmt_let(lctx, placer_ident, placer_expr)
1054 // let mut place = Placer::make_place(placer);
1056 let placer = expr_ident(lctx, e.span, placer_ident, None);
1057 let call = make_call(lctx, &make_place, hir_vec![placer]);
1058 mk_stmt_let_mut(lctx, place_ident, call)
1061 // let p_ptr = Place::pointer(&mut place);
1063 let agent = expr_ident(lctx, e.span, place_ident, None);
1064 let args = hir_vec![expr_mut_addr_of(lctx, e.span, agent, None)];
1065 let call = make_call(lctx, &place_pointer, args);
1066 mk_stmt_let(lctx, p_ptr_ident, call)
1069 // pop_unsafe!(EXPR));
1070 let pop_unsafe_expr = {
1071 let value_expr = signal_block_expr(lctx,
1075 hir::PopUnstableBlock,
1077 signal_block_expr(lctx,
1081 hir::PopUnsafeBlock(hir::CompilerGenerated), None)
1085 // std::intrinsics::move_val_init(raw_place, pop_unsafe!( EXPR ));
1086 // InPlace::finalize(place)
1089 let ptr = expr_ident(lctx, e.span, p_ptr_ident, None);
1090 let call_move_val_init =
1092 make_call(lctx, &move_val_init, hir_vec![ptr, pop_unsafe_expr]),
1094 let call_move_val_init = respan(e.span, call_move_val_init);
1096 let place = expr_ident(lctx, e.span, place_ident, None);
1097 let call = make_call(lctx, &inplace_finalize, hir_vec![place]);
1098 signal_block_expr(lctx,
1099 hir_vec![call_move_val_init],
1102 hir::PushUnsafeBlock(hir::CompilerGenerated), None)
1105 signal_block_expr(lctx,
1106 hir_vec![s1, s2, s3],
1109 hir::PushUnstableBlock,
1114 ExprKind::Vec(ref exprs) => {
1115 hir::ExprVec(exprs.iter().map(|x| lower_expr(lctx, x)).collect())
1117 ExprKind::Repeat(ref expr, ref count) => {
1118 let expr = lower_expr(lctx, expr);
1119 let count = lower_expr(lctx, count);
1120 hir::ExprRepeat(expr, count)
1122 ExprKind::Tup(ref elts) => {
1123 hir::ExprTup(elts.iter().map(|x| lower_expr(lctx, x)).collect())
1125 ExprKind::Call(ref f, ref args) => {
1126 let f = lower_expr(lctx, f);
1127 hir::ExprCall(f, args.iter().map(|x| lower_expr(lctx, x)).collect())
1129 ExprKind::MethodCall(i, ref tps, ref args) => {
1130 let tps = tps.iter().map(|x| lower_ty(lctx, x)).collect();
1131 let args = args.iter().map(|x| lower_expr(lctx, x)).collect();
1132 hir::ExprMethodCall(respan(i.span, i.node.name), tps, args)
1134 ExprKind::Binary(binop, ref lhs, ref rhs) => {
1135 let binop = lower_binop(lctx, binop);
1136 let lhs = lower_expr(lctx, lhs);
1137 let rhs = lower_expr(lctx, rhs);
1138 hir::ExprBinary(binop, lhs, rhs)
1140 ExprKind::Unary(op, ref ohs) => {
1141 let op = lower_unop(lctx, op);
1142 let ohs = lower_expr(lctx, ohs);
1143 hir::ExprUnary(op, ohs)
1145 ExprKind::Lit(ref l) => hir::ExprLit(P((**l).clone())),
1146 ExprKind::Cast(ref expr, ref ty) => {
1147 let expr = lower_expr(lctx, expr);
1148 hir::ExprCast(expr, lower_ty(lctx, ty))
1150 ExprKind::Type(ref expr, ref ty) => {
1151 let expr = lower_expr(lctx, expr);
1152 hir::ExprType(expr, lower_ty(lctx, ty))
1154 ExprKind::AddrOf(m, ref ohs) => {
1155 let m = lower_mutability(lctx, m);
1156 let ohs = lower_expr(lctx, ohs);
1157 hir::ExprAddrOf(m, ohs)
1159 // More complicated than you might expect because the else branch
1160 // might be `if let`.
1161 ExprKind::If(ref cond, ref blk, ref else_opt) => {
1162 let else_opt = else_opt.as_ref().map(|els| {
1164 ExprKind::IfLet(..) => {
1165 cache_ids(lctx, e.id, |lctx| {
1166 // wrap the if-let expr in a block
1167 let span = els.span;
1168 let els = lower_expr(lctx, els);
1169 let id = lctx.next_id();
1170 let blk = P(hir::Block {
1174 rules: hir::DefaultBlock,
1177 expr_block(lctx, blk, None)
1180 _ => lower_expr(lctx, els),
1184 hir::ExprIf(lower_expr(lctx, cond), lower_block(lctx, blk), else_opt)
1186 ExprKind::While(ref cond, ref body, opt_ident) => {
1187 hir::ExprWhile(lower_expr(lctx, cond), lower_block(lctx, body),
1188 opt_ident.map(|ident| lower_ident(lctx, ident)))
1190 ExprKind::Loop(ref body, opt_ident) => {
1191 hir::ExprLoop(lower_block(lctx, body),
1192 opt_ident.map(|ident| lower_ident(lctx, ident)))
1194 ExprKind::Match(ref expr, ref arms) => {
1195 hir::ExprMatch(lower_expr(lctx, expr),
1196 arms.iter().map(|x| lower_arm(lctx, x)).collect(),
1197 hir::MatchSource::Normal)
1199 ExprKind::Closure(capture_clause, ref decl, ref body) => {
1200 hir::ExprClosure(lower_capture_clause(lctx, capture_clause),
1201 lower_fn_decl(lctx, decl),
1202 lower_block(lctx, body))
1204 ExprKind::Block(ref blk) => hir::ExprBlock(lower_block(lctx, blk)),
1205 ExprKind::Assign(ref el, ref er) => {
1206 hir::ExprAssign(lower_expr(lctx, el), lower_expr(lctx, er))
1208 ExprKind::AssignOp(op, ref el, ref er) => {
1209 hir::ExprAssignOp(lower_binop(lctx, op),
1210 lower_expr(lctx, el),
1211 lower_expr(lctx, er))
1213 ExprKind::Field(ref el, ident) => {
1214 hir::ExprField(lower_expr(lctx, el), respan(ident.span, ident.node.name))
1216 ExprKind::TupField(ref el, ident) => {
1217 hir::ExprTupField(lower_expr(lctx, el), ident)
1219 ExprKind::Index(ref el, ref er) => {
1220 hir::ExprIndex(lower_expr(lctx, el), lower_expr(lctx, er))
1222 ExprKind::Range(ref e1, ref e2, lims) => {
1223 fn make_struct(lctx: &LoweringContext,
1226 fields: &[(&str, &P<Expr>)]) -> P<hir::Expr> {
1227 let strs = std_path(lctx, &iter::once(&"ops")
1230 .collect::<Vec<_>>());
1232 let structpath = path_global(ast_expr.span, strs);
1234 let hir_expr = if fields.len() == 0 {
1237 ast_expr.attrs.clone())
1242 fields.into_iter().map(|&(s, e)| {
1243 field(token::intern(s),
1244 signal_block_expr(lctx,
1246 lower_expr(lctx, &**e),
1248 hir::PopUnstableBlock,
1253 ast_expr.attrs.clone())
1256 signal_block_expr(lctx,
1260 hir::PushUnstableBlock,
1264 return cache_ids(lctx, e.id, |lctx| {
1265 use syntax::ast::RangeLimits::*;
1267 match (e1, e2, lims) {
1268 (&None, &None, HalfOpen) =>
1269 make_struct(lctx, e, &["RangeFull"],
1272 (&Some(ref e1), &None, HalfOpen) =>
1273 make_struct(lctx, e, &["RangeFrom"],
1276 (&None, &Some(ref e2), HalfOpen) =>
1277 make_struct(lctx, e, &["RangeTo"],
1280 (&Some(ref e1), &Some(ref e2), HalfOpen) =>
1281 make_struct(lctx, e, &["Range"],
1282 &[("start", e1), ("end", e2)]),
1284 (&None, &Some(ref e2), Closed) =>
1285 make_struct(lctx, e, &["RangeToInclusive"],
1288 (&Some(ref e1), &Some(ref e2), Closed) =>
1289 make_struct(lctx, e, &["RangeInclusive", "NonEmpty"],
1290 &[("start", e1), ("end", e2)]),
1292 _ => panic!("impossible range in AST"),
1296 ExprKind::Path(ref qself, ref path) => {
1297 let hir_qself = qself.as_ref().map(|&QSelf { ref ty, position }| {
1299 ty: lower_ty(lctx, ty),
1303 hir::ExprPath(hir_qself, lower_path_full(lctx, path, qself.is_none()))
1305 ExprKind::Break(opt_ident) => hir::ExprBreak(opt_ident.map(|sp_ident| {
1306 respan(sp_ident.span, lower_ident(lctx, sp_ident.node))
1308 ExprKind::Again(opt_ident) => hir::ExprAgain(opt_ident.map(|sp_ident| {
1309 respan(sp_ident.span, lower_ident(lctx, sp_ident.node))
1311 ExprKind::Ret(ref e) => hir::ExprRet(e.as_ref().map(|x| lower_expr(lctx, x))),
1312 ExprKind::InlineAsm(InlineAsm {
1322 }) => hir::ExprInlineAsm(hir::InlineAsm {
1323 inputs: inputs.iter().map(|&(ref c, _)| c.clone()).collect(),
1324 outputs: outputs.iter()
1326 hir::InlineAsmOutput {
1327 constraint: out.constraint.clone(),
1329 is_indirect: out.is_indirect,
1334 asm_str_style: asm_str_style,
1335 clobbers: clobbers.clone().into(),
1337 alignstack: alignstack,
1340 }, outputs.iter().map(|out| lower_expr(lctx, &out.expr)).collect(),
1341 inputs.iter().map(|&(_, ref input)| lower_expr(lctx, input)).collect()),
1342 ExprKind::Struct(ref path, ref fields, ref maybe_expr) => {
1343 hir::ExprStruct(lower_path(lctx, path),
1344 fields.iter().map(|x| lower_field(lctx, x)).collect(),
1345 maybe_expr.as_ref().map(|x| lower_expr(lctx, x)))
1347 ExprKind::Paren(ref ex) => {
1348 // merge attributes into the inner expression.
1349 return lower_expr(lctx, ex).map(|mut ex| {
1350 ex.attrs.update(|attrs| {
1351 attrs.prepend(e.attrs.clone())
1357 // Desugar ExprIfLet
1358 // From: `if let <pat> = <sub_expr> <body> [<else_opt>]`
1359 ExprKind::IfLet(ref pat, ref sub_expr, ref body, ref else_opt) => {
1362 // match <sub_expr> {
1364 // [_ if <else_opt_if_cond> => <else_opt_if_body>,]
1365 // _ => [<else_opt> | ()]
1368 return cache_ids(lctx, e.id, |lctx| {
1369 // `<pat> => <body>`
1371 let body = lower_block(lctx, body);
1372 let body_expr = expr_block(lctx, body, None);
1373 arm(hir_vec![lower_pat(lctx, pat)], body_expr)
1376 // `[_ if <else_opt_if_cond> => <else_opt_if_body>,]`
1377 let mut else_opt = else_opt.as_ref().map(|e| lower_expr(lctx, e));
1378 let else_if_arms = {
1379 let mut arms = vec![];
1381 let else_opt_continue = else_opt.and_then(|els| {
1382 els.and_then(|els| {
1385 hir::ExprIf(cond, then, else_opt) => {
1386 let pat_under = pat_wild(lctx, e.span);
1387 arms.push(hir::Arm {
1389 pats: hir_vec![pat_under],
1391 body: expr_block(lctx, then, None),
1393 else_opt.map(|else_opt| (else_opt, true))
1395 _ => Some((P(els), false)),
1399 match else_opt_continue {
1400 Some((e, true)) => {
1403 Some((e, false)) => {
1416 let contains_else_clause = else_opt.is_some();
1418 // `_ => [<else_opt> | ()]`
1420 let pat_under = pat_wild(lctx, e.span);
1422 else_opt.unwrap_or_else(
1423 || expr_tuple(lctx, e.span, hir_vec![], None));
1424 arm(hir_vec![pat_under], else_expr)
1427 let mut arms = Vec::with_capacity(else_if_arms.len() + 2);
1429 arms.extend(else_if_arms);
1430 arms.push(else_arm);
1432 let sub_expr = lower_expr(lctx, sub_expr);
1433 // add attributes to the outer returned expr node
1436 hir::ExprMatch(sub_expr,
1438 hir::MatchSource::IfLetDesugar {
1439 contains_else_clause: contains_else_clause,
1445 // Desugar ExprWhileLet
1446 // From: `[opt_ident]: while let <pat> = <sub_expr> <body>`
1447 ExprKind::WhileLet(ref pat, ref sub_expr, ref body, opt_ident) => {
1450 // [opt_ident]: loop {
1451 // match <sub_expr> {
1457 return cache_ids(lctx, e.id, |lctx| {
1458 // `<pat> => <body>`
1460 let body = lower_block(lctx, body);
1461 let body_expr = expr_block(lctx, body, None);
1462 arm(hir_vec![lower_pat(lctx, pat)], body_expr)
1467 let pat_under = pat_wild(lctx, e.span);
1468 let break_expr = expr_break(lctx, e.span, None);
1469 arm(hir_vec![pat_under], break_expr)
1472 // `match <sub_expr> { ... }`
1473 let arms = hir_vec![pat_arm, break_arm];
1474 let sub_expr = lower_expr(lctx, sub_expr);
1475 let match_expr = expr(lctx,
1477 hir::ExprMatch(sub_expr,
1479 hir::MatchSource::WhileLetDesugar),
1482 // `[opt_ident]: loop { ... }`
1483 let loop_block = block_expr(lctx, match_expr);
1484 let loop_expr = hir::ExprLoop(loop_block,
1485 opt_ident.map(|ident| lower_ident(lctx, ident)));
1486 // add attributes to the outer returned expr node
1487 expr(lctx, e.span, loop_expr, e.attrs.clone())
1491 // Desugar ExprForLoop
1492 // From: `[opt_ident]: for <pat> in <head> <body>`
1493 ExprKind::ForLoop(ref pat, ref head, ref body, opt_ident) => {
1497 // let result = match ::std::iter::IntoIterator::into_iter(<head>) {
1499 // [opt_ident]: loop {
1500 // match ::std::iter::Iterator::next(&mut iter) {
1501 // ::std::option::Option::Some(<pat>) => <body>,
1502 // ::std::option::Option::None => break
1510 return cache_ids(lctx, e.id, |lctx| {
1512 let head = lower_expr(lctx, head);
1514 let iter = lctx.str_to_ident("iter");
1516 // `::std::option::Option::Some(<pat>) => <body>`
1518 let body_block = lower_block(lctx, body);
1519 let body_span = body_block.span;
1520 let body_expr = P(hir::Expr {
1522 node: hir::ExprBlock(body_block),
1526 let pat = lower_pat(lctx, pat);
1527 let some_pat = pat_some(lctx, e.span, pat);
1529 arm(hir_vec![some_pat], body_expr)
1532 // `::std::option::Option::None => break`
1534 let break_expr = expr_break(lctx, e.span, None);
1536 arm(hir_vec![pat_none(lctx, e.span)], break_expr)
1539 // `match ::std::iter::Iterator::next(&mut iter) { ... }`
1542 let strs = std_path(lctx, &["iter", "Iterator", "next"]);
1544 path_global(e.span, strs)
1546 let iter = expr_ident(lctx, e.span, iter, None);
1547 let ref_mut_iter = expr_mut_addr_of(lctx, e.span, iter, None);
1548 let next_path = expr_path(lctx, next_path, None);
1549 let next_expr = expr_call(lctx,
1552 hir_vec![ref_mut_iter],
1554 let arms = hir_vec![pat_arm, break_arm];
1558 hir::ExprMatch(next_expr, arms, hir::MatchSource::ForLoopDesugar),
1562 // `[opt_ident]: loop { ... }`
1563 let loop_block = block_expr(lctx, match_expr);
1564 let loop_expr = hir::ExprLoop(loop_block,
1565 opt_ident.map(|ident| lower_ident(lctx, ident)));
1566 let loop_expr = expr(lctx, e.span, loop_expr, None);
1568 // `mut iter => { ... }`
1570 let iter_pat = pat_ident_binding_mode(lctx,
1573 hir::BindByValue(hir::MutMutable));
1574 arm(hir_vec![iter_pat], loop_expr)
1577 // `match ::std::iter::IntoIterator::into_iter(<head>) { ... }`
1578 let into_iter_expr = {
1579 let into_iter_path = {
1580 let strs = std_path(lctx, &["iter", "IntoIterator", "into_iter"]);
1582 path_global(e.span, strs)
1585 let into_iter = expr_path(lctx, into_iter_path, None);
1586 expr_call(lctx, e.span, into_iter, hir_vec![head], None)
1589 let match_expr = expr_match(lctx,
1593 hir::MatchSource::ForLoopDesugar,
1596 // `{ let _result = ...; _result }`
1597 // underscore prevents an unused_variables lint if the head diverges
1598 let result_ident = lctx.str_to_ident("_result");
1599 let let_stmt = stmt_let(lctx, e.span, false, result_ident, match_expr, None);
1600 let result = expr_ident(lctx, e.span, result_ident, None);
1601 let block = block_all(lctx, e.span, hir_vec![let_stmt], Some(result));
1602 // add the attributes to the outer returned expr node
1603 expr_block(lctx, block, e.attrs.clone())
1607 // Desugar ExprKind::Try
1609 ExprKind::Try(ref sub_expr) => {
1616 // return Err(From::from(err))
1621 return cache_ids(lctx, e.id, |lctx| {
1623 let sub_expr = lower_expr(lctx, sub_expr);
1627 let val_ident = lctx.str_to_ident("val");
1628 let val_pat = pat_ident(lctx, e.span, val_ident);
1629 let val_expr = expr_ident(lctx, e.span, val_ident, None);
1630 let ok_pat = pat_ok(lctx, e.span, val_pat);
1632 arm(hir_vec![ok_pat], val_expr)
1635 // Err(err) => return Err(From::from(err))
1637 let err_ident = lctx.str_to_ident("err");
1639 let path = std_path(lctx, &["convert", "From", "from"]);
1640 let path = path_global(e.span, path);
1641 let from = expr_path(lctx, path, None);
1642 let err_expr = expr_ident(lctx, e.span, err_ident, None);
1644 expr_call(lctx, e.span, from, hir_vec![err_expr], None)
1647 let path = std_path(lctx, &["result", "Result", "Err"]);
1648 let path = path_global(e.span, path);
1649 let err_ctor = expr_path(lctx, path, None);
1650 expr_call(lctx, e.span, err_ctor, hir_vec![from_expr], None)
1652 let err_pat = pat_err(lctx, e.span, pat_ident(lctx, e.span, err_ident));
1653 let ret_expr = expr(lctx, e.span,
1654 hir::Expr_::ExprRet(Some(err_expr)), None);
1656 arm(hir_vec![err_pat], ret_expr)
1659 expr_match(lctx, e.span, sub_expr, hir_vec![err_arm, ok_arm],
1660 hir::MatchSource::TryDesugar, None)
1664 ExprKind::Mac(_) => panic!("Shouldn't exist here"),
1667 attrs: e.attrs.clone(),
1671 pub fn lower_stmt(lctx: &LoweringContext, s: &Stmt) -> hir::Stmt {
1673 StmtKind::Decl(ref d, id) => {
1675 node: hir::StmtDecl(lower_decl(lctx, d), id),
1679 StmtKind::Expr(ref e, id) => {
1681 node: hir::StmtExpr(lower_expr(lctx, e), id),
1685 StmtKind::Semi(ref e, id) => {
1687 node: hir::StmtSemi(lower_expr(lctx, e), id),
1691 StmtKind::Mac(..) => panic!("Shouldn't exist here"),
1695 pub fn lower_capture_clause(_lctx: &LoweringContext, c: CaptureBy) -> hir::CaptureClause {
1697 CaptureBy::Value => hir::CaptureByValue,
1698 CaptureBy::Ref => hir::CaptureByRef,
1702 pub fn lower_visibility(_lctx: &LoweringContext, v: Visibility) -> hir::Visibility {
1704 Visibility::Public => hir::Public,
1705 Visibility::Inherited => hir::Inherited,
1709 pub fn lower_defaultness(_lctx: &LoweringContext, d: Defaultness) -> hir::Defaultness {
1711 Defaultness::Default => hir::Defaultness::Default,
1712 Defaultness::Final => hir::Defaultness::Final,
1716 pub fn lower_block_check_mode(lctx: &LoweringContext, b: &BlockCheckMode) -> hir::BlockCheckMode {
1718 BlockCheckMode::Default => hir::DefaultBlock,
1719 BlockCheckMode::Unsafe(u) => hir::UnsafeBlock(lower_unsafe_source(lctx, u)),
1723 pub fn lower_binding_mode(lctx: &LoweringContext, b: &BindingMode) -> hir::BindingMode {
1725 BindingMode::ByRef(m) => hir::BindByRef(lower_mutability(lctx, m)),
1726 BindingMode::ByValue(m) => hir::BindByValue(lower_mutability(lctx, m)),
1730 pub fn lower_unsafe_source(_lctx: &LoweringContext, u: UnsafeSource) -> hir::UnsafeSource {
1732 CompilerGenerated => hir::CompilerGenerated,
1733 UserProvided => hir::UserProvided,
1737 pub fn lower_impl_polarity(_lctx: &LoweringContext, i: ImplPolarity) -> hir::ImplPolarity {
1739 ImplPolarity::Positive => hir::ImplPolarity::Positive,
1740 ImplPolarity::Negative => hir::ImplPolarity::Negative,
1744 pub fn lower_trait_bound_modifier(_lctx: &LoweringContext,
1745 f: TraitBoundModifier)
1746 -> hir::TraitBoundModifier {
1748 TraitBoundModifier::None => hir::TraitBoundModifier::None,
1749 TraitBoundModifier::Maybe => hir::TraitBoundModifier::Maybe,
1753 // Helper methods for building HIR.
1755 fn arm(pats: hir::HirVec<P<hir::Pat>>, expr: P<hir::Expr>) -> hir::Arm {
1764 fn field(name: Name, expr: P<hir::Expr>, span: Span) -> hir::Field {
1775 fn expr_break(lctx: &LoweringContext, span: Span,
1776 attrs: ThinAttributes) -> P<hir::Expr> {
1777 expr(lctx, span, hir::ExprBreak(None), attrs)
1780 fn expr_call(lctx: &LoweringContext,
1783 args: hir::HirVec<P<hir::Expr>>,
1784 attrs: ThinAttributes)
1786 expr(lctx, span, hir::ExprCall(e, args), attrs)
1789 fn expr_ident(lctx: &LoweringContext, span: Span, id: hir::Ident,
1790 attrs: ThinAttributes) -> P<hir::Expr> {
1791 expr_path(lctx, path_ident(span, id), attrs)
1794 fn expr_mut_addr_of(lctx: &LoweringContext, span: Span, e: P<hir::Expr>,
1795 attrs: ThinAttributes) -> P<hir::Expr> {
1796 expr(lctx, span, hir::ExprAddrOf(hir::MutMutable, e), attrs)
1799 fn expr_path(lctx: &LoweringContext, path: hir::Path,
1800 attrs: ThinAttributes) -> P<hir::Expr> {
1801 expr(lctx, path.span, hir::ExprPath(None, path), attrs)
1804 fn expr_match(lctx: &LoweringContext,
1807 arms: hir::HirVec<hir::Arm>,
1808 source: hir::MatchSource,
1809 attrs: ThinAttributes)
1811 expr(lctx, span, hir::ExprMatch(arg, arms, source), attrs)
1814 fn expr_block(lctx: &LoweringContext, b: P<hir::Block>,
1815 attrs: ThinAttributes) -> P<hir::Expr> {
1816 expr(lctx, b.span, hir::ExprBlock(b), attrs)
1819 fn expr_tuple(lctx: &LoweringContext, sp: Span, exprs: hir::HirVec<P<hir::Expr>>,
1820 attrs: ThinAttributes) -> P<hir::Expr> {
1821 expr(lctx, sp, hir::ExprTup(exprs), attrs)
1824 fn expr_struct(lctx: &LoweringContext,
1827 fields: hir::HirVec<hir::Field>,
1828 e: Option<P<hir::Expr>>,
1829 attrs: ThinAttributes) -> P<hir::Expr> {
1830 expr(lctx, sp, hir::ExprStruct(path, fields, e), attrs)
1833 fn expr(lctx: &LoweringContext, span: Span, node: hir::Expr_,
1834 attrs: ThinAttributes) -> P<hir::Expr> {
1843 fn stmt_let(lctx: &LoweringContext,
1848 attrs: ThinAttributes)
1850 let pat = if mutbl {
1851 pat_ident_binding_mode(lctx, sp, ident, hir::BindByValue(hir::MutMutable))
1853 pat_ident(lctx, sp, ident)
1855 let local = P(hir::Local {
1863 let decl = respan(sp, hir::DeclLocal(local));
1864 respan(sp, hir::StmtDecl(P(decl), lctx.next_id()))
1867 fn block_expr(lctx: &LoweringContext, expr: P<hir::Expr>) -> P<hir::Block> {
1868 block_all(lctx, expr.span, hir::HirVec::new(), Some(expr))
1871 fn block_all(lctx: &LoweringContext,
1873 stmts: hir::HirVec<hir::Stmt>,
1874 expr: Option<P<hir::Expr>>)
1880 rules: hir::DefaultBlock,
1885 fn pat_ok(lctx: &LoweringContext, span: Span, pat: P<hir::Pat>) -> P<hir::Pat> {
1886 let ok = std_path(lctx, &["result", "Result", "Ok"]);
1887 let path = path_global(span, ok);
1888 pat_enum(lctx, span, path, hir_vec![pat])
1891 fn pat_err(lctx: &LoweringContext, span: Span, pat: P<hir::Pat>) -> P<hir::Pat> {
1892 let err = std_path(lctx, &["result", "Result", "Err"]);
1893 let path = path_global(span, err);
1894 pat_enum(lctx, span, path, hir_vec![pat])
1897 fn pat_some(lctx: &LoweringContext, span: Span, pat: P<hir::Pat>) -> P<hir::Pat> {
1898 let some = std_path(lctx, &["option", "Option", "Some"]);
1899 let path = path_global(span, some);
1900 pat_enum(lctx, span, path, hir_vec![pat])
1903 fn pat_none(lctx: &LoweringContext, span: Span) -> P<hir::Pat> {
1904 let none = std_path(lctx, &["option", "Option", "None"]);
1905 let path = path_global(span, none);
1906 pat_enum(lctx, span, path, hir_vec![])
1909 fn pat_enum(lctx: &LoweringContext,
1912 subpats: hir::HirVec<P<hir::Pat>>)
1914 let pt = if subpats.is_empty() {
1915 hir::PatKind::Path(path)
1917 hir::PatKind::TupleStruct(path, Some(subpats))
1922 fn pat_ident(lctx: &LoweringContext, span: Span, ident: hir::Ident) -> P<hir::Pat> {
1923 pat_ident_binding_mode(lctx, span, ident, hir::BindByValue(hir::MutImmutable))
1926 fn pat_ident_binding_mode(lctx: &LoweringContext,
1929 bm: hir::BindingMode)
1931 let pat_ident = hir::PatKind::Ident(bm,
1937 pat(lctx, span, pat_ident)
1940 fn pat_wild(lctx: &LoweringContext, span: Span) -> P<hir::Pat> {
1941 pat(lctx, span, hir::PatKind::Wild)
1944 fn pat(lctx: &LoweringContext, span: Span, pat: hir::PatKind) -> P<hir::Pat> {
1952 fn path_ident(span: Span, id: hir::Ident) -> hir::Path {
1953 path(span, vec![id])
1956 fn path(span: Span, strs: Vec<hir::Ident>) -> hir::Path {
1957 path_all(span, false, strs, hir::HirVec::new(), hir::HirVec::new(), hir::HirVec::new())
1960 fn path_global(span: Span, strs: Vec<hir::Ident>) -> hir::Path {
1961 path_all(span, true, strs, hir::HirVec::new(), hir::HirVec::new(), hir::HirVec::new())
1964 fn path_all(sp: Span,
1966 mut idents: Vec<hir::Ident>,
1967 lifetimes: hir::HirVec<hir::Lifetime>,
1968 types: hir::HirVec<P<hir::Ty>>,
1969 bindings: hir::HirVec<hir::TypeBinding>)
1971 let last_identifier = idents.pop().unwrap();
1972 let mut segments: Vec<hir::PathSegment> = idents.into_iter()
1976 parameters: hir::PathParameters::none(),
1980 segments.push(hir::PathSegment {
1981 identifier: last_identifier,
1982 parameters: hir::AngleBracketedParameters(hir::AngleBracketedParameterData {
1983 lifetimes: lifetimes,
1991 segments: segments.into(),
1995 fn std_path(lctx: &LoweringContext, components: &[&str]) -> Vec<hir::Ident> {
1996 let mut v = Vec::new();
1997 if let Some(s) = lctx.crate_root {
1998 v.push(hir::Ident::from_name(token::intern(s)));
2000 v.extend(components.iter().map(|s| hir::Ident::from_name(token::intern(s))));
2004 // Given suffix ["b","c","d"], returns path `::std::b::c::d` when
2005 // `fld.cx.use_std`, and `::core::b::c::d` otherwise.
2006 fn core_path(lctx: &LoweringContext, span: Span, components: &[&str]) -> hir::Path {
2007 let idents = std_path(lctx, components);
2008 path_global(span, idents)
2011 fn signal_block_expr(lctx: &LoweringContext,
2012 stmts: hir::HirVec<hir::Stmt>,
2015 rule: hir::BlockCheckMode,
2016 attrs: ThinAttributes)
2018 let id = lctx.next_id();
2035 use syntax::ast::{self, NodeId, NodeIdAssigner};
2036 use syntax::{parse, codemap};
2037 use syntax::fold::Folder;
2038 use std::cell::Cell;
2040 struct MockAssigner {
2041 next_id: Cell<NodeId>,
2045 fn new() -> MockAssigner {
2046 MockAssigner { next_id: Cell::new(0) }
2051 fn call_site(&self) -> codemap::Span;
2052 fn cfg(&self) -> ast::CrateConfig;
2053 fn ident_of(&self, st: &str) -> ast::Ident;
2054 fn name_of(&self, st: &str) -> ast::Name;
2055 fn parse_sess(&self) -> &parse::ParseSess;
2058 impl FakeExtCtxt for parse::ParseSess {
2059 fn call_site(&self) -> codemap::Span {
2061 lo: codemap::BytePos(0),
2062 hi: codemap::BytePos(0),
2063 expn_id: codemap::NO_EXPANSION,
2066 fn cfg(&self) -> ast::CrateConfig {
2069 fn ident_of(&self, st: &str) -> ast::Ident {
2070 parse::token::str_to_ident(st)
2072 fn name_of(&self, st: &str) -> ast::Name {
2073 parse::token::intern(st)
2075 fn parse_sess(&self) -> &parse::ParseSess {
2080 impl NodeIdAssigner for MockAssigner {
2081 fn next_node_id(&self) -> NodeId {
2082 let result = self.next_id.get();
2083 self.next_id.set(result + 1);
2087 fn peek_node_id(&self) -> NodeId {
2092 impl Folder for MockAssigner {
2093 fn new_id(&mut self, old_id: NodeId) -> NodeId {
2094 assert_eq!(old_id, ast::DUMMY_NODE_ID);
2100 fn test_preserves_ids() {
2101 let cx = parse::ParseSess::new();
2102 let mut assigner = MockAssigner::new();
2104 let ast_if_let = quote_expr!(&cx,
2105 if let Some(foo) = baz {
2108 let ast_if_let = assigner.fold_expr(ast_if_let);
2109 let ast_while_let = quote_expr!(&cx,
2110 while let Some(foo) = baz {
2113 let ast_while_let = assigner.fold_expr(ast_while_let);
2114 let ast_for = quote_expr!(&cx,
2120 let ast_for = assigner.fold_expr(ast_for);
2121 let ast_in = quote_expr!(&cx, in HEAP { foo() });
2122 let ast_in = assigner.fold_expr(ast_in);
2124 let lctx = LoweringContext::new(&assigner, None);
2125 let hir1 = lower_expr(&lctx, &ast_if_let);
2126 let hir2 = lower_expr(&lctx, &ast_if_let);
2127 assert!(hir1 == hir2);
2129 let hir1 = lower_expr(&lctx, &ast_while_let);
2130 let hir2 = lower_expr(&lctx, &ast_while_let);
2131 assert!(hir1 == hir2);
2133 let hir1 = lower_expr(&lctx, &ast_for);
2134 let hir2 = lower_expr(&lctx, &ast_for);
2135 assert!(hir1 == hir2);
2137 let hir1 = lower_expr(&lctx, &ast_in);
2138 let hir2 = lower_expr(&lctx, &ast_in);
2139 assert!(hir1 == hir2);