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