]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/hair/cx/expr.rs
[MIR] use mir::repr::Constant in ExprKind::Repeat, close #29789
[rust.git] / src / librustc_mir / hair / cx / expr.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use hair::*;
12 use rustc_data_structures::fnv::FnvHashMap;
13 use hair::cx::Cx;
14 use hair::cx::block;
15 use hair::cx::to_ref::ToRef;
16 use rustc::front::map;
17 use rustc::middle::def::Def;
18 use rustc::middle::region::CodeExtent;
19 use rustc::middle::pat_util;
20 use rustc::middle::ty::{self, VariantDef, Ty};
21 use rustc::mir::repr::*;
22 use rustc_front::hir;
23 use rustc_front::util as hir_util;
24 use syntax::parse::token;
25 use syntax::ptr::P;
26
27 impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
28     type Output = Expr<'tcx>;
29
30     fn make_mirror<'a>(self, cx: &mut Cx<'a, 'tcx>) -> Expr<'tcx> {
31         debug!("Expr::make_mirror(): id={}, span={:?}", self.id, self.span);
32
33         let expr_ty = cx.tcx.expr_ty(self); // note: no adjustments (yet)!
34
35         let kind = match self.node {
36             // Here comes the interesting stuff:
37             hir::ExprMethodCall(_, _, ref args) => {
38                 // Rewrite a.b(c) into UFCS form like Trait::b(a, c)
39                 let expr = method_callee(cx, self, ty::MethodCall::expr(self.id));
40                 let args = args.iter()
41                                .map(|e| e.to_ref())
42                                .collect();
43                 ExprKind::Call {
44                     ty: expr.ty,
45                     fun: expr.to_ref(),
46                     args: args,
47                 }
48             }
49
50             hir::ExprCall(ref fun, ref args) => {
51                 if cx.tcx.is_method_call(self.id) {
52                     // The callee is something implementing Fn, FnMut, or FnOnce.
53                     // Find the actual method implementation being called and
54                     // build the appropriate UFCS call expression with the
55                     // callee-object as self parameter.
56                     let method = method_callee(cx, self, ty::MethodCall::expr(self.id));
57                     let mut argrefs = vec![fun.to_ref()];
58                     argrefs.extend(args.iter().map(|a| a.to_ref()));
59
60                     ExprKind::Call {
61                         ty: method.ty,
62                         fun: method.to_ref(),
63                         args: argrefs,
64                     }
65                 } else {
66                     let adt_data = if let hir::ExprPath(..) = fun.node {
67                         // Tuple-like ADTs are represented as ExprCall. We convert them here.
68                         expr_ty.ty_adt_def().and_then(|adt_def|{
69                             match cx.tcx.def_map.borrow()[&fun.id].full_def() {
70                                 Def::Variant(_, variant_id) => {
71                                     Some((adt_def, adt_def.variant_index_with_id(variant_id)))
72                                 },
73                                 Def::Struct(..) => {
74                                     Some((adt_def, 0))
75                                 },
76                                 _ => None
77                             }
78                         })
79                     } else { None };
80                     if let Some((adt_def, index)) = adt_data {
81                         let substs = cx.tcx.mk_substs(cx.tcx.node_id_item_substs(fun.id).substs);
82                         let field_refs = args.iter().enumerate().map(|(idx, e)| FieldExprRef {
83                             name: Field::new(idx),
84                             expr: e.to_ref()
85                         }).collect();
86                         ExprKind::Adt {
87                             adt_def: adt_def,
88                             substs: substs,
89                             variant_index: index,
90                             fields: field_refs,
91                             base: None
92                         }
93                     } else {
94                         ExprKind::Call {
95                             ty: cx.tcx.node_id_to_type(fun.id),
96                             fun: fun.to_ref(),
97                             args: args.to_ref(),
98                         }
99                     }
100                 }
101             }
102
103             hir::ExprAddrOf(mutbl, ref expr) => {
104                 let region = match expr_ty.sty {
105                     ty::TyRef(r, _) => r,
106                     _ => cx.tcx.sess.span_bug(expr.span, "type of & not region"),
107                 };
108                 ExprKind::Borrow {
109                     region: *region,
110                     borrow_kind: to_borrow_kind(mutbl),
111                     arg: expr.to_ref(),
112                 }
113             }
114
115             hir::ExprBlock(ref blk) => {
116                 ExprKind::Block { body: &**blk }
117             }
118
119             hir::ExprAssign(ref lhs, ref rhs) => {
120                 ExprKind::Assign {
121                     lhs: lhs.to_ref(),
122                     rhs: rhs.to_ref(),
123                 }
124             }
125
126             hir::ExprAssignOp(op, ref lhs, ref rhs) => {
127                 let op = bin_op(op.node);
128                 ExprKind::AssignOp {
129                     op: op,
130                     lhs: lhs.to_ref(),
131                     rhs: rhs.to_ref(),
132                 }
133             }
134
135             hir::ExprLit(..) => ExprKind::Literal {
136                 literal: cx.const_eval_literal(self)
137             },
138
139             hir::ExprBinary(op, ref lhs, ref rhs) => {
140                 if cx.tcx.is_method_call(self.id) {
141                     let pass_args = if hir_util::is_by_value_binop(op.node) {
142                         PassArgs::ByValue
143                     } else {
144                         PassArgs::ByRef
145                     };
146                     overloaded_operator(cx, self, ty::MethodCall::expr(self.id),
147                                         pass_args, lhs.to_ref(), vec![rhs])
148                 } else {
149                     // FIXME overflow
150                     match op.node {
151                         hir::BinOp_::BiAnd => {
152                             ExprKind::LogicalOp {
153                                 op: LogicalOp::And,
154                                 lhs: lhs.to_ref(),
155                                 rhs: rhs.to_ref(),
156                             }
157                         }
158                         hir::BinOp_::BiOr => {
159                             ExprKind::LogicalOp {
160                                 op: LogicalOp::Or,
161                                 lhs: lhs.to_ref(),
162                                 rhs: rhs.to_ref(),
163                             }
164                         }
165                         _ => {
166                             let op = bin_op(op.node);
167                             ExprKind::Binary {
168                                 op: op,
169                                 lhs: lhs.to_ref(),
170                                 rhs: rhs.to_ref(),
171                             }
172                         }
173                     }
174                 }
175             }
176
177             hir::ExprIndex(ref lhs, ref index) => {
178                 if cx.tcx.is_method_call(self.id) {
179                     overloaded_lvalue(cx, self, ty::MethodCall::expr(self.id),
180                                       PassArgs::ByValue, lhs.to_ref(), vec![index])
181                 } else {
182                     ExprKind::Index {
183                         lhs: lhs.to_ref(),
184                         index: index.to_ref(),
185                     }
186                 }
187             }
188
189             hir::ExprUnary(hir::UnOp::UnDeref, ref arg) => {
190                 if cx.tcx.is_method_call(self.id) {
191                     overloaded_lvalue(cx, self, ty::MethodCall::expr(self.id),
192                                       PassArgs::ByValue, arg.to_ref(), vec![])
193                 } else {
194                     ExprKind::Deref { arg: arg.to_ref() }
195                 }
196             }
197
198             hir::ExprUnary(op, ref arg) => {
199                 if cx.tcx.is_method_call(self.id) {
200                     overloaded_operator(cx, self, ty::MethodCall::expr(self.id),
201                                         PassArgs::ByValue, arg.to_ref(), vec![])
202                 } else {
203                     // FIXME overflow
204                     let op = match op {
205                         hir::UnOp::UnNot => UnOp::Not,
206                         hir::UnOp::UnNeg => UnOp::Neg,
207                         hir::UnOp::UnDeref => {
208                             cx.tcx.sess.span_bug(
209                                 self.span,
210                                 "UnDeref should have been handled elsewhere");
211                         }
212                     };
213                     ExprKind::Unary {
214                         op: op,
215                         arg: arg.to_ref(),
216                     }
217                 }
218             }
219
220             hir::ExprStruct(_, ref fields, ref base) => {
221                 match expr_ty.sty {
222                     ty::TyStruct(adt, substs) => {
223                         let field_refs = field_refs(&adt.variants[0], fields);
224                         ExprKind::Adt {
225                             adt_def: adt,
226                             variant_index: 0,
227                             substs: substs,
228                             fields: field_refs,
229                             base: base.to_ref(),
230                         }
231                     }
232                     ty::TyEnum(adt, substs) => {
233                         match cx.tcx.def_map.borrow()[&self.id].full_def() {
234                             Def::Variant(enum_id, variant_id) => {
235                                 debug_assert!(adt.did == enum_id);
236                                 let index = adt.variant_index_with_id(variant_id);
237                                 let field_refs = field_refs(&adt.variants[index], fields);
238                                 ExprKind::Adt {
239                                     adt_def: adt,
240                                     variant_index: index,
241                                     substs: substs,
242                                     fields: field_refs,
243                                     base: base.to_ref(),
244                                 }
245                             }
246                             ref def => {
247                                 cx.tcx.sess.span_bug(
248                                     self.span,
249                                     &format!("unexpected def: {:?}", def));
250                             }
251                         }
252                     }
253                     _ => {
254                         cx.tcx.sess.span_bug(
255                             self.span,
256                             &format!("unexpected type for struct literal: {:?}", expr_ty));
257                     }
258                 }
259             }
260
261             hir::ExprClosure(..) => {
262                 let closure_ty = cx.tcx.expr_ty(self);
263                 let (def_id, substs) = match closure_ty.sty {
264                     ty::TyClosure(def_id, ref substs) => (def_id, substs),
265                     _ => {
266                         cx.tcx.sess.span_bug(self.span,
267                                              &format!("closure expr w/o closure type: {:?}",
268                                                       closure_ty));
269                     }
270                 };
271                 let upvars = cx.tcx.with_freevars(self.id, |freevars| {
272                     freevars.iter()
273                             .enumerate()
274                             .map(|(i, fv)| capture_freevar(cx, self, fv, substs.upvar_tys[i]))
275                             .collect()
276                 });
277                 ExprKind::Closure {
278                     closure_id: def_id,
279                     substs: &**substs,
280                     upvars: upvars,
281                 }
282             }
283
284             hir::ExprRange(ref start, ref end) => {
285                 let range_ty = cx.tcx.expr_ty(self);
286                 let (adt_def, substs) = match range_ty.sty {
287                     ty::TyStruct(adt_def, substs) => (adt_def, substs),
288                     _ => {
289                         cx.tcx.sess.span_bug(self.span, "unexpanded ast");
290                     }
291                 };
292
293                 let field_expr_ref = |s: &'tcx P<hir::Expr>, name: &str| {
294                     let name = token::intern(name);
295                     let index = adt_def.variants[0].index_of_field_named(name).unwrap();
296                     FieldExprRef { name: Field::new(index), expr: s.to_ref() }
297                 };
298
299                 let start_field = start.as_ref()
300                                        .into_iter()
301                                        .map(|s| field_expr_ref(s, "start"));
302
303                 let end_field = end.as_ref()
304                                    .into_iter()
305                                    .map(|e| field_expr_ref(e, "end"));
306
307                 ExprKind::Adt {
308                     adt_def: adt_def,
309                     variant_index: 0,
310                     substs: substs,
311                     fields: start_field.chain(end_field).collect(),
312                     base: None,
313                 }
314             }
315
316             hir::ExprPath(..) => {
317                 convert_path_expr(cx, self)
318             }
319
320             hir::ExprInlineAsm(ref asm) => {
321                 ExprKind::InlineAsm { asm: asm }
322             }
323
324             // Now comes the rote stuff:
325
326             hir::ExprRepeat(ref v, ref c) => ExprKind::Repeat {
327                 value: v.to_ref(),
328                 count: Constant {
329                     ty: cx.tcx.expr_ty(c),
330                     span: c.span,
331                     literal: cx.const_eval_literal(c)
332                 }
333             },
334             hir::ExprRet(ref v) =>
335                 ExprKind::Return { value: v.to_ref() },
336             hir::ExprBreak(label) =>
337                 ExprKind::Break { label: label.map(|_| loop_label(cx, self)) },
338             hir::ExprAgain(label) =>
339                 ExprKind::Continue { label: label.map(|_| loop_label(cx, self)) },
340             hir::ExprMatch(ref discr, ref arms, _) =>
341                 ExprKind::Match { discriminant: discr.to_ref(),
342                                   arms: arms.iter().map(|a| convert_arm(cx, a)).collect() },
343             hir::ExprIf(ref cond, ref then, ref otherwise) =>
344                 ExprKind::If { condition: cond.to_ref(),
345                                then: block::to_expr_ref(cx, then),
346                                otherwise: otherwise.to_ref() },
347             hir::ExprWhile(ref cond, ref body, _) =>
348                 ExprKind::Loop { condition: Some(cond.to_ref()),
349                                  body: block::to_expr_ref(cx, body) },
350             hir::ExprLoop(ref body, _) =>
351                 ExprKind::Loop { condition: None,
352                                  body: block::to_expr_ref(cx, body) },
353             hir::ExprField(ref source, name) => {
354                 let index = match cx.tcx.expr_ty_adjusted(source).sty {
355                     ty::TyStruct(adt_def, _) =>
356                         adt_def.variants[0].index_of_field_named(name.node),
357                     ref ty =>
358                         cx.tcx.sess.span_bug(
359                             self.span,
360                             &format!("field of non-struct: {:?}", ty)),
361                 };
362                 let index = index.unwrap_or_else(|| {
363                     cx.tcx.sess.span_bug(
364                         self.span,
365                         &format!("no index found for field `{}`", name.node));
366                 });
367                 ExprKind::Field { lhs: source.to_ref(), name: Field::new(index) }
368             }
369             hir::ExprTupField(ref source, index) =>
370                 ExprKind::Field { lhs: source.to_ref(),
371                                   name: Field::new(index.node as usize) },
372             hir::ExprCast(ref source, _) =>
373                 ExprKind::Cast { source: source.to_ref() },
374             hir::ExprType(ref source, _) =>
375                 return source.make_mirror(cx),
376             hir::ExprBox(ref value) =>
377                 ExprKind::Box { value: value.to_ref() },
378             hir::ExprVec(ref fields) =>
379                 ExprKind::Vec { fields: fields.to_ref() },
380             hir::ExprTup(ref fields) =>
381                 ExprKind::Tuple { fields: fields.to_ref() },
382         };
383
384         let temp_lifetime = cx.tcx.region_maps.temporary_scope(self.id);
385         let expr_extent = cx.tcx.region_maps.node_extent(self.id);
386
387         let mut expr = Expr {
388             temp_lifetime: temp_lifetime,
389             ty: expr_ty,
390             span: self.span,
391             kind: kind,
392         };
393
394         // Now apply adjustments, if any.
395         match cx.tcx.tables.borrow().adjustments.get(&self.id) {
396             None => {}
397             Some(&ty::adjustment::AdjustReifyFnPointer) => {
398                 let adjusted_ty = cx.tcx.expr_ty_adjusted(self);
399                 expr = Expr {
400                     temp_lifetime: temp_lifetime,
401                     ty: adjusted_ty,
402                     span: self.span,
403                     kind: ExprKind::ReifyFnPointer { source: expr.to_ref() },
404                 };
405             }
406             Some(&ty::adjustment::AdjustUnsafeFnPointer) => {
407                 let adjusted_ty = cx.tcx.expr_ty_adjusted(self);
408                 expr = Expr {
409                     temp_lifetime: temp_lifetime,
410                     ty: adjusted_ty,
411                     span: self.span,
412                     kind: ExprKind::UnsafeFnPointer { source: expr.to_ref() },
413                 };
414             }
415             Some(&ty::adjustment::AdjustDerefRef(ref adj)) => {
416                 for i in 0..adj.autoderefs {
417                     let i = i as u32;
418                     let adjusted_ty =
419                         expr.ty.adjust_for_autoderef(
420                             cx.tcx,
421                             self.id,
422                             self.span,
423                             i,
424                             |mc| cx.tcx.tables.borrow().method_map.get(&mc).map(|m| m.ty));
425                     let kind = if cx.tcx.is_overloaded_autoderef(self.id, i) {
426                         overloaded_lvalue(cx, self, ty::MethodCall::autoderef(self.id, i),
427                                           PassArgs::ByValue, expr.to_ref(), vec![])
428                     } else {
429                         ExprKind::Deref { arg: expr.to_ref() }
430                     };
431                     expr = Expr {
432                         temp_lifetime: temp_lifetime,
433                         ty: adjusted_ty,
434                         span: self.span,
435                         kind: kind,
436                     };
437                 }
438
439                 if let Some(autoref) = adj.autoref {
440                     let adjusted_ty = expr.ty.adjust_for_autoref(cx.tcx, Some(autoref));
441                     match autoref {
442                         ty::adjustment::AutoPtr(r, m) => {
443                             expr = Expr {
444                                 temp_lifetime: temp_lifetime,
445                                 ty: adjusted_ty,
446                                 span: self.span,
447                                 kind: ExprKind::Borrow {
448                                     region: *r,
449                                     borrow_kind: to_borrow_kind(m),
450                                     arg: expr.to_ref(),
451                                 },
452                             };
453                         }
454                         ty::adjustment::AutoUnsafe(m) => {
455                             // Convert this to a suitable `&foo` and
456                             // then an unsafe coercion. Limit the region to be just this
457                             // expression.
458                             let region = ty::ReScope(expr_extent);
459                             let region = cx.tcx.mk_region(region);
460                             expr = Expr {
461                                 temp_lifetime: temp_lifetime,
462                                 ty: cx.tcx.mk_ref(region, ty::TypeAndMut { ty: expr.ty, mutbl: m }),
463                                 span: self.span,
464                                 kind: ExprKind::Borrow {
465                                     region: *region,
466                                     borrow_kind: to_borrow_kind(m),
467                                     arg: expr.to_ref(),
468                                 },
469                             };
470                             expr = Expr {
471                                 temp_lifetime: temp_lifetime,
472                                 ty: adjusted_ty,
473                                 span: self.span,
474                                 kind: ExprKind::Cast { source: expr.to_ref() },
475                             };
476                         }
477                     }
478                 }
479
480                 if let Some(target) = adj.unsize {
481                     expr = Expr {
482                         temp_lifetime: temp_lifetime,
483                         ty: target,
484                         span: self.span,
485                         kind: ExprKind::Unsize { source: expr.to_ref() },
486                     };
487                 }
488             }
489         }
490
491         // Next, wrap this up in the expr's scope.
492         expr = Expr {
493             temp_lifetime: temp_lifetime,
494             ty: expr.ty,
495             span: self.span,
496             kind: ExprKind::Scope {
497                 extent: expr_extent,
498                 value: expr.to_ref(),
499             },
500         };
501
502         // Finally, create a destruction scope, if any.
503         if let Some(extent) = cx.tcx.region_maps.opt_destruction_extent(self.id) {
504             expr = Expr {
505                 temp_lifetime: temp_lifetime,
506                 ty: expr.ty,
507                 span: self.span,
508                 kind: ExprKind::Scope {
509                     extent: extent,
510                     value: expr.to_ref(),
511                 },
512             };
513         }
514
515         // OK, all done!
516         expr
517     }
518 }
519
520 fn method_callee<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>,
521                                expr: &hir::Expr,
522                                method_call: ty::MethodCall)
523                                -> Expr<'tcx> {
524     let tables = cx.tcx.tables.borrow();
525     let callee = &tables.method_map[&method_call];
526     let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
527     Expr {
528         temp_lifetime: temp_lifetime,
529         ty: callee.ty,
530         span: expr.span,
531         kind: ExprKind::Literal {
532             literal: Literal::Item {
533                 def_id: callee.def_id,
534                 kind: ItemKind::Method,
535                 substs: callee.substs,
536             },
537         },
538     }
539 }
540
541 fn to_borrow_kind(m: hir::Mutability) -> BorrowKind {
542     match m {
543         hir::MutMutable => BorrowKind::Mut,
544         hir::MutImmutable => BorrowKind::Shared,
545     }
546 }
547
548 fn convert_arm<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>, arm: &'tcx hir::Arm) -> Arm<'tcx> {
549     let mut map;
550     let opt_map = if arm.pats.len() == 1 {
551         None
552     } else {
553         map = FnvHashMap();
554         pat_util::pat_bindings(&cx.tcx.def_map, &arm.pats[0], |_, p_id, _, path| {
555             map.insert(path.node, p_id);
556         });
557         Some(&map)
558     };
559
560     Arm {
561         patterns: arm.pats.iter().map(|p| cx.refutable_pat(opt_map, p)).collect(),
562         guard: arm.guard.to_ref(),
563         body: arm.body.to_ref(),
564     }
565 }
566
567 fn convert_path_expr<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>, expr: &'tcx hir::Expr) -> ExprKind<'tcx> {
568     let substs = cx.tcx.mk_substs(cx.tcx.node_id_item_substs(expr.id).substs);
569     // Otherwise there may be def_map borrow conflicts
570     let def = cx.tcx.def_map.borrow()[&expr.id].full_def();
571     let (def_id, kind) = match def {
572         // A regular function.
573         Def::Fn(def_id) => (def_id, ItemKind::Function),
574         Def::Method(def_id) => (def_id, ItemKind::Method),
575         Def::Struct(def_id) => match cx.tcx.node_id_to_type(expr.id).sty {
576             // A tuple-struct constructor. Should only be reached if not called in the same
577             // expression.
578             ty::TyBareFn(..) => (def_id, ItemKind::Function),
579             // A unit struct which is used as a value. We return a completely different ExprKind
580             // here to account for this special case.
581             ty::TyStruct(adt_def, substs) => return ExprKind::Adt {
582                 adt_def: adt_def,
583                 variant_index: 0,
584                 substs: substs,
585                 fields: vec![],
586                 base: None
587             },
588             ref sty => panic!("unexpected sty: {:?}", sty)
589         },
590         Def::Variant(enum_id, variant_id) => match cx.tcx.node_id_to_type(expr.id).sty {
591             // A variant constructor. Should only be reached if not called in the same
592             // expression.
593             ty::TyBareFn(..) => (variant_id, ItemKind::Function),
594             // A unit variant, similar special case to the struct case above.
595             ty::TyEnum(adt_def, substs) => {
596                 debug_assert!(adt_def.did == enum_id);
597                 let index = adt_def.variant_index_with_id(variant_id);
598                 return ExprKind::Adt {
599                     adt_def: adt_def,
600                     substs: substs,
601                     variant_index: index,
602                     fields: vec![],
603                     base: None
604                 };
605             },
606             ref sty => panic!("unexpected sty: {:?}", sty)
607         },
608         Def::Const(def_id) |
609         Def::AssociatedConst(def_id) => {
610             if let Some(v) = cx.try_const_eval_literal(expr) {
611                 return ExprKind::Literal { literal: v };
612             } else {
613                 (def_id, ItemKind::Constant)
614             }
615         }
616
617         Def::Static(node_id, _) => return ExprKind::StaticRef {
618             id: node_id,
619         },
620
621         def @ Def::Local(..) |
622         def @ Def::Upvar(..) => return convert_var(cx, expr, def),
623
624         def =>
625             cx.tcx.sess.span_bug(
626                 expr.span,
627                 &format!("def `{:?}` not yet implemented", def)),
628     };
629     ExprKind::Literal {
630         literal: Literal::Item { def_id: def_id, kind: kind, substs: substs }
631     }
632 }
633
634 fn convert_var<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>,
635                              expr: &'tcx hir::Expr,
636                              def: Def)
637                              -> ExprKind<'tcx> {
638     let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
639
640     match def {
641         Def::Local(_, node_id) => {
642             ExprKind::VarRef {
643                 id: node_id,
644             }
645         }
646
647         Def::Upvar(_, id_var, index, closure_expr_id) => {
648             debug!("convert_var(upvar({:?}, {:?}, {:?}))", id_var, index, closure_expr_id);
649             let var_ty = cx.tcx.node_id_to_type(id_var);
650
651             let body_id = match cx.tcx.map.find(closure_expr_id) {
652                 Some(map::NodeExpr(expr)) => {
653                     match expr.node {
654                         hir::ExprClosure(_, _, ref body) => body.id,
655                         _ => {
656                             cx.tcx.sess.span_bug(expr.span, "closure expr is not a closure expr");
657                         }
658                     }
659                 }
660                 _ => {
661                     cx.tcx.sess.span_bug(expr.span, "ast-map has garbage for closure expr");
662                 }
663             };
664
665             // FIXME free regions in closures are not right
666             let closure_ty = cx.tcx.node_id_to_type(closure_expr_id);
667
668             // FIXME we're just hard-coding the idea that the
669             // signature will be &self or &mut self and hence will
670             // have a bound region with number 0
671             let region = ty::Region::ReFree(ty::FreeRegion {
672                 scope: cx.tcx.region_maps.node_extent(body_id),
673                 bound_region: ty::BoundRegion::BrAnon(0),
674             });
675             let region = cx.tcx.mk_region(region);
676
677             let self_expr = match cx.tcx.closure_kind(cx.tcx.map.local_def_id(closure_expr_id)) {
678                 ty::ClosureKind::FnClosureKind => {
679                     let ref_closure_ty =
680                         cx.tcx.mk_ref(region,
681                                    ty::TypeAndMut { ty: closure_ty,
682                                                     mutbl: hir::MutImmutable });
683                     Expr {
684                         ty: closure_ty,
685                         temp_lifetime: temp_lifetime,
686                         span: expr.span,
687                         kind: ExprKind::Deref {
688                             arg: Expr {
689                                 ty: ref_closure_ty,
690                                 temp_lifetime: temp_lifetime,
691                                 span: expr.span,
692                                 kind: ExprKind::SelfRef
693                             }.to_ref()
694                         }
695                     }
696                 }
697                 ty::ClosureKind::FnMutClosureKind => {
698                     let ref_closure_ty =
699                         cx.tcx.mk_ref(region,
700                                    ty::TypeAndMut { ty: closure_ty,
701                                                     mutbl: hir::MutMutable });
702                     Expr {
703                         ty: closure_ty,
704                         temp_lifetime: temp_lifetime,
705                         span: expr.span,
706                         kind: ExprKind::Deref {
707                             arg: Expr {
708                                 ty: ref_closure_ty,
709                                 temp_lifetime: temp_lifetime,
710                                 span: expr.span,
711                                 kind: ExprKind::SelfRef
712                             }.to_ref()
713                         }
714                     }
715                 }
716                 ty::ClosureKind::FnOnceClosureKind => {
717                     Expr {
718                         ty: closure_ty,
719                         temp_lifetime: temp_lifetime,
720                         span: expr.span,
721                         kind: ExprKind::SelfRef,
722                     }
723                 }
724             };
725
726             // at this point we have `self.n`, which loads up the upvar
727             let field_kind = ExprKind::Field {
728                 lhs: self_expr.to_ref(),
729                 name: Field::new(index),
730             };
731
732             // ...but the upvar might be an `&T` or `&mut T` capture, at which
733             // point we need an implicit deref
734             let upvar_id = ty::UpvarId {
735                 var_id: id_var,
736                 closure_expr_id: closure_expr_id,
737             };
738             let upvar_capture = match cx.tcx.upvar_capture(upvar_id) {
739                 Some(c) => c,
740                 None => {
741                     cx.tcx.sess.span_bug(
742                         expr.span,
743                         &format!("no upvar_capture for {:?}", upvar_id));
744                 }
745             };
746             match upvar_capture {
747                 ty::UpvarCapture::ByValue => field_kind,
748                 ty::UpvarCapture::ByRef(_) => {
749                     ExprKind::Deref {
750                         arg: Expr {
751                             temp_lifetime: temp_lifetime,
752                             ty: var_ty,
753                             span: expr.span,
754                             kind: field_kind,
755                         }.to_ref()
756                     }
757                 }
758             }
759         }
760
761         _ => cx.tcx.sess.span_bug(expr.span, "type of & not region"),
762     }
763 }
764
765
766 fn bin_op(op: hir::BinOp_) -> BinOp {
767     match op {
768         hir::BinOp_::BiAdd => BinOp::Add,
769         hir::BinOp_::BiSub => BinOp::Sub,
770         hir::BinOp_::BiMul => BinOp::Mul,
771         hir::BinOp_::BiDiv => BinOp::Div,
772         hir::BinOp_::BiRem => BinOp::Rem,
773         hir::BinOp_::BiBitXor => BinOp::BitXor,
774         hir::BinOp_::BiBitAnd => BinOp::BitAnd,
775         hir::BinOp_::BiBitOr => BinOp::BitOr,
776         hir::BinOp_::BiShl => BinOp::Shl,
777         hir::BinOp_::BiShr => BinOp::Shr,
778         hir::BinOp_::BiEq => BinOp::Eq,
779         hir::BinOp_::BiLt => BinOp::Lt,
780         hir::BinOp_::BiLe => BinOp::Le,
781         hir::BinOp_::BiNe => BinOp::Ne,
782         hir::BinOp_::BiGe => BinOp::Ge,
783         hir::BinOp_::BiGt => BinOp::Gt,
784         _ => panic!("no equivalent for ast binop {:?}", op),
785     }
786 }
787
788 enum PassArgs {
789     ByValue,
790     ByRef,
791 }
792
793 fn overloaded_operator<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>,
794                                      expr: &'tcx hir::Expr,
795                                      method_call: ty::MethodCall,
796                                      pass_args: PassArgs,
797                                      receiver: ExprRef<'tcx>,
798                                      args: Vec<&'tcx P<hir::Expr>>)
799                                      -> ExprKind<'tcx> {
800     // the receiver has all the adjustments that are needed, so we can
801     // just push a reference to it
802     let mut argrefs = vec![receiver];
803
804     // the arguments, unfortunately, do not, so if this is a ByRef
805     // operator, we have to gin up the autorefs (but by value is easy)
806     match pass_args {
807         PassArgs::ByValue => {
808             argrefs.extend(args.iter().map(|arg| arg.to_ref()))
809         }
810
811         PassArgs::ByRef => {
812             let scope = cx.tcx.region_maps.node_extent(expr.id);
813             let region = cx.tcx.mk_region(ty::ReScope(scope));
814             let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
815             argrefs.extend(
816                 args.iter()
817                     .map(|arg| {
818                         let arg_ty = cx.tcx.expr_ty_adjusted(arg);
819                         let adjusted_ty =
820                             cx.tcx.mk_ref(region,
821                                        ty::TypeAndMut { ty: arg_ty,
822                                                         mutbl: hir::MutImmutable });
823                         Expr {
824                             temp_lifetime: temp_lifetime,
825                             ty: adjusted_ty,
826                             span: expr.span,
827                             kind: ExprKind::Borrow { region: *region,
828                                                      borrow_kind: BorrowKind::Shared,
829                                                      arg: arg.to_ref() }
830                         }.to_ref()
831                     }))
832         }
833     }
834
835     // now create the call itself
836     let fun = method_callee(cx, expr, method_call);
837     ExprKind::Call {
838         ty: fun.ty,
839         fun: fun.to_ref(),
840         args: argrefs,
841     }
842 }
843
844 fn overloaded_lvalue<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>,
845                                    expr: &'tcx hir::Expr,
846                                    method_call: ty::MethodCall,
847                                    pass_args: PassArgs,
848                                    receiver: ExprRef<'tcx>,
849                                    args: Vec<&'tcx P<hir::Expr>>)
850                                    -> ExprKind<'tcx> {
851     // For an overloaded *x or x[y] expression of type T, the method
852     // call returns an &T and we must add the deref so that the types
853     // line up (this is because `*x` and `x[y]` represent lvalues):
854
855     // to find the type &T of the content returned by the method;
856     let tables = cx.tcx.tables.borrow();
857     let callee = &tables.method_map[&method_call];
858     let ref_ty = callee.ty.fn_ret();
859     let ref_ty = cx.tcx.no_late_bound_regions(&ref_ty).unwrap().unwrap();
860     //                                              1~~~~~   2~~~~~
861     // (1) callees always have all late-bound regions fully instantiated,
862     // (2) overloaded methods don't return `!`
863
864     // construct the complete expression `foo()` for the overloaded call,
865     // which will yield the &T type
866     let temp_lifetime = cx.tcx.region_maps.temporary_scope(expr.id);
867     let ref_kind = overloaded_operator(cx, expr, method_call, pass_args, receiver, args);
868     let ref_expr = Expr {
869         temp_lifetime: temp_lifetime,
870         ty: ref_ty,
871         span: expr.span,
872         kind: ref_kind,
873     };
874
875     // construct and return a deref wrapper `*foo()`
876     ExprKind::Deref { arg: ref_expr.to_ref() }
877 }
878
879 fn capture_freevar<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>,
880                                  closure_expr: &'tcx hir::Expr,
881                                  freevar: &ty::Freevar,
882                                  freevar_ty: Ty<'tcx>)
883                                  -> ExprRef<'tcx> {
884     let id_var = freevar.def.var_id();
885     let upvar_id = ty::UpvarId {
886         var_id: id_var,
887         closure_expr_id: closure_expr.id,
888     };
889     let upvar_capture = cx.tcx.upvar_capture(upvar_id).unwrap();
890     let temp_lifetime = cx.tcx.region_maps.temporary_scope(closure_expr.id);
891     let var_ty = cx.tcx.node_id_to_type(id_var);
892     let captured_var = Expr {
893         temp_lifetime: temp_lifetime,
894         ty: var_ty,
895         span: closure_expr.span,
896         kind: convert_var(cx, closure_expr, freevar.def),
897     };
898     match upvar_capture {
899         ty::UpvarCapture::ByValue => {
900             captured_var.to_ref()
901         }
902         ty::UpvarCapture::ByRef(upvar_borrow) => {
903             let borrow_kind = match upvar_borrow.kind {
904                 ty::BorrowKind::ImmBorrow => BorrowKind::Shared,
905                 ty::BorrowKind::UniqueImmBorrow => BorrowKind::Unique,
906                 ty::BorrowKind::MutBorrow => BorrowKind::Mut,
907             };
908             Expr {
909                 temp_lifetime: temp_lifetime,
910                 ty: freevar_ty,
911                 span: closure_expr.span,
912                 kind: ExprKind::Borrow { region: upvar_borrow.region,
913                                          borrow_kind: borrow_kind,
914                                          arg: captured_var.to_ref() }
915             }.to_ref()
916         }
917     }
918 }
919
920 fn loop_label<'a, 'tcx: 'a>(cx: &mut Cx<'a, 'tcx>, expr: &'tcx hir::Expr) -> CodeExtent {
921     match cx.tcx.def_map.borrow().get(&expr.id).map(|d| d.full_def()) {
922         Some(Def::Label(loop_id)) => cx.tcx.region_maps.node_extent(loop_id),
923         d => {
924             cx.tcx.sess.span_bug(expr.span, &format!("loop scope resolved to {:?}", d));
925         }
926     }
927 }
928
929 /// Converts a list of named fields (i.e. for struct-like struct/enum ADTs) into FieldExprRef.
930 fn field_refs<'tcx>(variant: VariantDef<'tcx>,
931                     fields: &'tcx [hir::Field])
932                     -> Vec<FieldExprRef<'tcx>>
933 {
934     fields.iter()
935           .map(|field| FieldExprRef {
936               name: Field::new(variant.index_of_field_named(field.name.node).unwrap()),
937               expr: field.expr.to_ref(),
938           })
939           .collect()
940 }