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