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