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