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