]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/trans/expr.rs
Auto merge of #28001 - arielb1:dtor-fixes, r=pnkfelix
[rust.git] / src / librustc_trans / trans / expr.rs
1 // Copyright 2012-2014 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 //! # Translation of Expressions
12 //!
13 //! The expr module handles translation of expressions. The most general
14 //! translation routine is `trans()`, which will translate an expression
15 //! into a datum. `trans_into()` is also available, which will translate
16 //! an expression and write the result directly into memory, sometimes
17 //! avoiding the need for a temporary stack slot. Finally,
18 //! `trans_to_lvalue()` is available if you'd like to ensure that the
19 //! result has cleanup scheduled.
20 //!
21 //! Internally, each of these functions dispatches to various other
22 //! expression functions depending on the kind of expression. We divide
23 //! up expressions into:
24 //!
25 //! - **Datum expressions:** Those that most naturally yield values.
26 //!   Examples would be `22`, `box x`, or `a + b` (when not overloaded).
27 //! - **DPS expressions:** Those that most naturally write into a location
28 //!   in memory. Examples would be `foo()` or `Point { x: 3, y: 4 }`.
29 //! - **Statement expressions:** That that do not generate a meaningful
30 //!   result. Examples would be `while { ... }` or `return 44`.
31 //!
32 //! Public entry points:
33 //!
34 //! - `trans_into(bcx, expr, dest) -> bcx`: evaluates an expression,
35 //!   storing the result into `dest`. This is the preferred form, if you
36 //!   can manage it.
37 //!
38 //! - `trans(bcx, expr) -> DatumBlock`: evaluates an expression, yielding
39 //!   `Datum` with the result. You can then store the datum, inspect
40 //!   the value, etc. This may introduce temporaries if the datum is a
41 //!   structural type.
42 //!
43 //! - `trans_to_lvalue(bcx, expr, "...") -> DatumBlock`: evaluates an
44 //!   expression and ensures that the result has a cleanup associated with it,
45 //!   creating a temporary stack slot if necessary.
46 //!
47 //! - `trans_local_var -> Datum`: looks up a local variable or upvar.
48
49 #![allow(non_camel_case_types)]
50
51 pub use self::Dest::*;
52 use self::lazy_binop_ty::*;
53
54 use back::abi;
55 use llvm::{self, ValueRef, TypeKind};
56 use middle::check_const;
57 use middle::def;
58 use middle::lang_items::CoerceUnsizedTraitLangItem;
59 use middle::subst::{Substs, VecPerParamSpace};
60 use middle::traits;
61 use trans::{_match, adt, asm, base, callee, closure, consts, controlflow};
62 use trans::base::*;
63 use trans::build::*;
64 use trans::cleanup::{self, CleanupMethods, DropHintMethods};
65 use trans::common::*;
66 use trans::datum::*;
67 use trans::debuginfo::{self, DebugLoc, ToDebugLoc};
68 use trans::declare;
69 use trans::glue;
70 use trans::machine;
71 use trans::meth;
72 use trans::tvec;
73 use trans::type_of;
74 use middle::cast::{CastKind, CastTy};
75 use middle::ty::{AdjustDerefRef, AdjustReifyFnPointer, AdjustUnsafeFnPointer};
76 use middle::ty::{self, Ty};
77 use middle::ty::MethodCall;
78 use util::common::indenter;
79 use trans::machine::{llsize_of, llsize_of_alloc};
80 use trans::type_::Type;
81
82 use syntax::{ast, ast_util, codemap};
83 use syntax::parse::token::InternedString;
84 use syntax::ptr::P;
85 use syntax::parse::token;
86 use std::mem;
87
88 // Destinations
89
90 // These are passed around by the code generating functions to track the
91 // destination of a computation's value.
92
93 #[derive(Copy, Clone, PartialEq)]
94 pub enum Dest {
95     SaveIn(ValueRef),
96     Ignore,
97 }
98
99 impl Dest {
100     pub fn to_string(&self, ccx: &CrateContext) -> String {
101         match *self {
102             SaveIn(v) => format!("SaveIn({})", ccx.tn().val_to_string(v)),
103             Ignore => "Ignore".to_string()
104         }
105     }
106 }
107
108 /// This function is equivalent to `trans(bcx, expr).store_to_dest(dest)` but it may generate
109 /// better optimized LLVM code.
110 pub fn trans_into<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
111                               expr: &ast::Expr,
112                               dest: Dest)
113                               -> Block<'blk, 'tcx> {
114     let mut bcx = bcx;
115
116     debuginfo::set_source_location(bcx.fcx, expr.id, expr.span);
117
118     if bcx.tcx().tables.borrow().adjustments.contains_key(&expr.id) {
119         // use trans, which may be less efficient but
120         // which will perform the adjustments:
121         let datum = unpack_datum!(bcx, trans(bcx, expr));
122         return datum.store_to_dest(bcx, dest, expr.id);
123     }
124
125     let qualif = *bcx.tcx().const_qualif_map.borrow().get(&expr.id).unwrap();
126     if !qualif.intersects(
127         check_const::ConstQualif::NOT_CONST |
128         check_const::ConstQualif::NEEDS_DROP
129     ) {
130         if !qualif.intersects(check_const::ConstQualif::PREFER_IN_PLACE) {
131             if let SaveIn(lldest) = dest {
132                 let global = consts::get_const_expr_as_global(bcx.ccx(), expr, qualif,
133                                                             bcx.fcx.param_substs);
134                 // Cast pointer to destination, because constants
135                 // have different types.
136                 let lldest = PointerCast(bcx, lldest, val_ty(global));
137                 memcpy_ty(bcx, lldest, global, expr_ty_adjusted(bcx, expr));
138                 return bcx;
139             }
140             // Even if we don't have a value to emit, and the expression
141             // doesn't have any side-effects, we still have to translate the
142             // body of any closures.
143             // FIXME: Find a better way of handling this case.
144         } else {
145             // The only way we're going to see a `const` at this point is if
146             // it prefers in-place instantiation, likely because it contains
147             // `[x; N]` somewhere within.
148             match expr.node {
149                 ast::ExprPath(..) => {
150                     match bcx.def(expr.id) {
151                         def::DefConst(did) => {
152                             let const_expr = consts::get_const_expr(bcx.ccx(), did, expr);
153                             // Temporarily get cleanup scopes out of the way,
154                             // as they require sub-expressions to be contained
155                             // inside the current AST scope.
156                             // These should record no cleanups anyways, `const`
157                             // can't have destructors.
158                             let scopes = mem::replace(&mut *bcx.fcx.scopes.borrow_mut(),
159                                                       vec![]);
160                             // Lock emitted debug locations to the location of
161                             // the constant reference expression.
162                             debuginfo::with_source_location_override(bcx.fcx,
163                                                                      expr.debug_loc(),
164                                                                      || {
165                                 bcx = trans_into(bcx, const_expr, dest)
166                             });
167                             let scopes = mem::replace(&mut *bcx.fcx.scopes.borrow_mut(),
168                                                       scopes);
169                             assert!(scopes.is_empty());
170                             return bcx;
171                         }
172                         _ => {}
173                     }
174                 }
175                 _ => {}
176             }
177         }
178     }
179
180     debug!("trans_into() expr={:?}", expr);
181
182     let cleanup_debug_loc = debuginfo::get_cleanup_debug_loc_for_ast_node(bcx.ccx(),
183                                                                           expr.id,
184                                                                           expr.span,
185                                                                           false);
186     bcx.fcx.push_ast_cleanup_scope(cleanup_debug_loc);
187
188     let kind = expr_kind(bcx.tcx(), expr);
189     bcx = match kind {
190         ExprKind::Lvalue | ExprKind::RvalueDatum => {
191             trans_unadjusted(bcx, expr).store_to_dest(dest, expr.id)
192         }
193         ExprKind::RvalueDps => {
194             trans_rvalue_dps_unadjusted(bcx, expr, dest)
195         }
196         ExprKind::RvalueStmt => {
197             trans_rvalue_stmt_unadjusted(bcx, expr)
198         }
199     };
200
201     bcx.fcx.pop_and_trans_ast_cleanup_scope(bcx, expr.id)
202 }
203
204 /// Translates an expression, returning a datum (and new block) encapsulating the result. When
205 /// possible, it is preferred to use `trans_into`, as that may avoid creating a temporary on the
206 /// stack.
207 pub fn trans<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
208                          expr: &ast::Expr)
209                          -> DatumBlock<'blk, 'tcx, Expr> {
210     debug!("trans(expr={:?})", expr);
211
212     let mut bcx = bcx;
213     let fcx = bcx.fcx;
214     let qualif = *bcx.tcx().const_qualif_map.borrow().get(&expr.id).unwrap();
215     let adjusted_global = !qualif.intersects(check_const::ConstQualif::NON_STATIC_BORROWS);
216     let global = if !qualif.intersects(
217         check_const::ConstQualif::NOT_CONST |
218         check_const::ConstQualif::NEEDS_DROP
219     ) {
220         let global = consts::get_const_expr_as_global(bcx.ccx(), expr, qualif,
221                                                       bcx.fcx.param_substs);
222
223         if qualif.intersects(check_const::ConstQualif::HAS_STATIC_BORROWS) {
224             // Is borrowed as 'static, must return lvalue.
225
226             // Cast pointer to global, because constants have different types.
227             let const_ty = expr_ty_adjusted(bcx, expr);
228             let llty = type_of::type_of(bcx.ccx(), const_ty);
229             let global = PointerCast(bcx, global, llty.ptr_to());
230             let datum = Datum::new(global, const_ty, Lvalue::new("expr::trans"));
231             return DatumBlock::new(bcx, datum.to_expr_datum());
232         }
233
234         // Otherwise, keep around and perform adjustments, if needed.
235         let const_ty = if adjusted_global {
236             expr_ty_adjusted(bcx, expr)
237         } else {
238             expr_ty(bcx, expr)
239         };
240
241         // This could use a better heuristic.
242         Some(if type_is_immediate(bcx.ccx(), const_ty) {
243             // Cast pointer to global, because constants have different types.
244             let llty = type_of::type_of(bcx.ccx(), const_ty);
245             let global = PointerCast(bcx, global, llty.ptr_to());
246             // Maybe just get the value directly, instead of loading it?
247             immediate_rvalue(load_ty(bcx, global, const_ty), const_ty)
248         } else {
249             let llty = type_of::type_of(bcx.ccx(), const_ty);
250             // HACK(eddyb) get around issues with lifetime intrinsics.
251             let scratch = alloca_no_lifetime(bcx, llty, "const");
252             let lldest = if !const_ty.is_structural() {
253                 // Cast pointer to slot, because constants have different types.
254                 PointerCast(bcx, scratch, val_ty(global))
255             } else {
256                 // In this case, memcpy_ty calls llvm.memcpy after casting both
257                 // source and destination to i8*, so we don't need any casts.
258                 scratch
259             };
260             memcpy_ty(bcx, lldest, global, const_ty);
261             Datum::new(scratch, const_ty, Rvalue::new(ByRef))
262         })
263     } else {
264         None
265     };
266
267     let cleanup_debug_loc = debuginfo::get_cleanup_debug_loc_for_ast_node(bcx.ccx(),
268                                                                           expr.id,
269                                                                           expr.span,
270                                                                           false);
271     fcx.push_ast_cleanup_scope(cleanup_debug_loc);
272     let datum = match global {
273         Some(rvalue) => rvalue.to_expr_datum(),
274         None => unpack_datum!(bcx, trans_unadjusted(bcx, expr))
275     };
276     let datum = if adjusted_global {
277         datum // trans::consts already performed adjustments.
278     } else {
279         unpack_datum!(bcx, apply_adjustments(bcx, expr, datum))
280     };
281     bcx = fcx.pop_and_trans_ast_cleanup_scope(bcx, expr.id);
282     return DatumBlock::new(bcx, datum);
283 }
284
285 pub fn get_meta(bcx: Block, fat_ptr: ValueRef) -> ValueRef {
286     StructGEP(bcx, fat_ptr, abi::FAT_PTR_EXTRA)
287 }
288
289 pub fn get_dataptr(bcx: Block, fat_ptr: ValueRef) -> ValueRef {
290     StructGEP(bcx, fat_ptr, abi::FAT_PTR_ADDR)
291 }
292
293 pub fn copy_fat_ptr(bcx: Block, src_ptr: ValueRef, dst_ptr: ValueRef) {
294     Store(bcx, Load(bcx, get_dataptr(bcx, src_ptr)), get_dataptr(bcx, dst_ptr));
295     Store(bcx, Load(bcx, get_meta(bcx, src_ptr)), get_meta(bcx, dst_ptr));
296 }
297
298 /// Retrieve the information we are losing (making dynamic) in an unsizing
299 /// adjustment.
300 ///
301 /// The `old_info` argument is a bit funny. It is intended for use
302 /// in an upcast, where the new vtable for an object will be drived
303 /// from the old one.
304 pub fn unsized_info<'ccx, 'tcx>(ccx: &CrateContext<'ccx, 'tcx>,
305                                 source: Ty<'tcx>,
306                                 target: Ty<'tcx>,
307                                 old_info: Option<ValueRef>,
308                                 param_substs: &'tcx Substs<'tcx>)
309                                 -> ValueRef {
310     let (source, target) = ccx.tcx().struct_lockstep_tails(source, target);
311     match (&source.sty, &target.sty) {
312         (&ty::TyArray(_, len), &ty::TySlice(_)) => C_uint(ccx, len),
313         (&ty::TyTrait(_), &ty::TyTrait(_)) => {
314             // For now, upcasts are limited to changes in marker
315             // traits, and hence never actually require an actual
316             // change to the vtable.
317             old_info.expect("unsized_info: missing old info for trait upcast")
318         }
319         (_, &ty::TyTrait(box ty::TraitTy { ref principal, .. })) => {
320             // Note that we preserve binding levels here:
321             let substs = principal.0.substs.with_self_ty(source).erase_regions();
322             let substs = ccx.tcx().mk_substs(substs);
323             let trait_ref = ty::Binder(ty::TraitRef { def_id: principal.def_id(),
324                                                       substs: substs });
325             consts::ptrcast(meth::get_vtable(ccx, trait_ref, param_substs),
326                             Type::vtable_ptr(ccx))
327         }
328         _ => ccx.sess().bug(&format!("unsized_info: invalid unsizing {:?} -> {:?}",
329                                      source,
330                                      target))
331     }
332 }
333
334 /// Helper for trans that apply adjustments from `expr` to `datum`, which should be the unadjusted
335 /// translation of `expr`.
336 fn apply_adjustments<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
337                                  expr: &ast::Expr,
338                                  datum: Datum<'tcx, Expr>)
339                                  -> DatumBlock<'blk, 'tcx, Expr>
340 {
341     let mut bcx = bcx;
342     let mut datum = datum;
343     let adjustment = match bcx.tcx().tables.borrow().adjustments.get(&expr.id).cloned() {
344         None => {
345             return DatumBlock::new(bcx, datum);
346         }
347         Some(adj) => { adj }
348     };
349     debug!("unadjusted datum for expr {:?}: {} adjustment={:?}",
350            expr,
351            datum.to_string(bcx.ccx()),
352            adjustment);
353     match adjustment {
354         AdjustReifyFnPointer => {
355             // FIXME(#19925) once fn item types are
356             // zero-sized, we'll need to do something here
357         }
358         AdjustUnsafeFnPointer => {
359             // purely a type-level thing
360         }
361         AdjustDerefRef(ref adj) => {
362             let skip_reborrows = if adj.autoderefs == 1 && adj.autoref.is_some() {
363                 // We are a bit paranoid about adjustments and thus might have a re-
364                 // borrow here which merely derefs and then refs again (it might have
365                 // a different region or mutability, but we don't care here).
366                 match datum.ty.sty {
367                     // Don't skip a conversion from Box<T> to &T, etc.
368                     ty::TyRef(..) => {
369                         if bcx.tcx().is_overloaded_autoderef(expr.id, 0) {
370                             // Don't skip an overloaded deref.
371                             0
372                         } else {
373                             1
374                         }
375                     }
376                     _ => 0
377                 }
378             } else {
379                 0
380             };
381
382             if adj.autoderefs > skip_reborrows {
383                 // Schedule cleanup.
384                 let lval = unpack_datum!(bcx, datum.to_lvalue_datum(bcx, "auto_deref", expr.id));
385                 datum = unpack_datum!(bcx, deref_multiple(bcx, expr,
386                                                           lval.to_expr_datum(),
387                                                           adj.autoderefs - skip_reborrows));
388             }
389
390             // (You might think there is a more elegant way to do this than a
391             // skip_reborrows bool, but then you remember that the borrow checker exists).
392             if skip_reborrows == 0 && adj.autoref.is_some() {
393                 if !type_is_sized(bcx.tcx(), datum.ty) {
394                     // Arrange cleanup
395                     let lval = unpack_datum!(bcx,
396                         datum.to_lvalue_datum(bcx, "ref_fat_ptr", expr.id));
397                     datum = unpack_datum!(bcx, ref_fat_ptr(bcx, lval));
398                 } else {
399                     datum = unpack_datum!(bcx, auto_ref(bcx, datum, expr));
400                 }
401             }
402
403             if let Some(target) = adj.unsize {
404                 // We do not arrange cleanup ourselves; if we already are an
405                 // L-value, then cleanup will have already been scheduled (and
406                 // the `datum.to_rvalue_datum` call below will emit code to zero
407                 // the drop flag when moving out of the L-value). If we are an
408                 // R-value, then we do not need to schedule cleanup.
409                 let source_datum = unpack_datum!(bcx,
410                     datum.to_rvalue_datum(bcx, "__coerce_source"));
411
412                 let target = bcx.monomorphize(&target);
413                 let llty = type_of::type_of(bcx.ccx(), target);
414
415                 // HACK(eddyb) get around issues with lifetime intrinsics.
416                 let scratch = alloca_no_lifetime(bcx, llty, "__coerce_target");
417                 let target_datum = Datum::new(scratch, target,
418                                               Rvalue::new(ByRef));
419                 bcx = coerce_unsized(bcx, expr.span, source_datum, target_datum);
420                 datum = Datum::new(scratch, target,
421                                    RvalueExpr(Rvalue::new(ByRef)));
422             }
423         }
424     }
425     debug!("after adjustments, datum={}", datum.to_string(bcx.ccx()));
426     DatumBlock::new(bcx, datum)
427 }
428
429 fn coerce_unsized<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
430                               span: codemap::Span,
431                               source: Datum<'tcx, Rvalue>,
432                               target: Datum<'tcx, Rvalue>)
433                               -> Block<'blk, 'tcx> {
434     let mut bcx = bcx;
435     debug!("coerce_unsized({} -> {})",
436            source.to_string(bcx.ccx()),
437            target.to_string(bcx.ccx()));
438
439     match (&source.ty.sty, &target.ty.sty) {
440         (&ty::TyBox(a), &ty::TyBox(b)) |
441         (&ty::TyRef(_, ty::TypeAndMut { ty: a, .. }),
442          &ty::TyRef(_, ty::TypeAndMut { ty: b, .. })) |
443         (&ty::TyRef(_, ty::TypeAndMut { ty: a, .. }),
444          &ty::TyRawPtr(ty::TypeAndMut { ty: b, .. })) |
445         (&ty::TyRawPtr(ty::TypeAndMut { ty: a, .. }),
446          &ty::TyRawPtr(ty::TypeAndMut { ty: b, .. })) => {
447             let (inner_source, inner_target) = (a, b);
448
449             let (base, old_info) = if !type_is_sized(bcx.tcx(), inner_source) {
450                 // Normally, the source is a thin pointer and we are
451                 // adding extra info to make a fat pointer. The exception
452                 // is when we are upcasting an existing object fat pointer
453                 // to use a different vtable. In that case, we want to
454                 // load out the original data pointer so we can repackage
455                 // it.
456                 (Load(bcx, get_dataptr(bcx, source.val)),
457                 Some(Load(bcx, get_meta(bcx, source.val))))
458             } else {
459                 let val = if source.kind.is_by_ref() {
460                     load_ty(bcx, source.val, source.ty)
461                 } else {
462                     source.val
463                 };
464                 (val, None)
465             };
466
467             let info = unsized_info(bcx.ccx(), inner_source, inner_target,
468                                     old_info, bcx.fcx.param_substs);
469
470             // Compute the base pointer. This doesn't change the pointer value,
471             // but merely its type.
472             let ptr_ty = type_of::in_memory_type_of(bcx.ccx(), inner_target).ptr_to();
473             let base = PointerCast(bcx, base, ptr_ty);
474
475             Store(bcx, base, get_dataptr(bcx, target.val));
476             Store(bcx, info, get_meta(bcx, target.val));
477         }
478
479         // This can be extended to enums and tuples in the future.
480         // (&ty::TyEnum(def_id_a, _), &ty::TyEnum(def_id_b, _)) |
481         (&ty::TyStruct(def_id_a, _), &ty::TyStruct(def_id_b, _)) => {
482             assert_eq!(def_id_a, def_id_b);
483
484             // The target is already by-ref because it's to be written to.
485             let source = unpack_datum!(bcx, source.to_ref_datum(bcx));
486             assert!(target.kind.is_by_ref());
487
488             let trait_substs = Substs::erased(VecPerParamSpace::new(vec![target.ty],
489                                                                     vec![source.ty],
490                                                                     Vec::new()));
491             let trait_ref = ty::Binder(ty::TraitRef {
492                 def_id: langcall(bcx, Some(span), "coercion",
493                                  CoerceUnsizedTraitLangItem),
494                 substs: bcx.tcx().mk_substs(trait_substs)
495             });
496
497             let kind = match fulfill_obligation(bcx.ccx(), span, trait_ref) {
498                 traits::VtableImpl(traits::VtableImplData { impl_def_id, .. }) => {
499                     bcx.tcx().custom_coerce_unsized_kind(impl_def_id)
500                 }
501                 vtable => {
502                     bcx.sess().span_bug(span, &format!("invalid CoerceUnsized vtable: {:?}",
503                                                        vtable));
504                 }
505             };
506
507             let repr_source = adt::represent_type(bcx.ccx(), source.ty);
508             let src_fields = match &*repr_source {
509                 &adt::Repr::Univariant(ref s, _) => &s.fields,
510                 _ => bcx.sess().span_bug(span,
511                                          &format!("Non univariant struct? (repr_source: {:?})",
512                                                   repr_source)),
513             };
514             let repr_target = adt::represent_type(bcx.ccx(), target.ty);
515             let target_fields = match &*repr_target {
516                 &adt::Repr::Univariant(ref s, _) => &s.fields,
517                 _ => bcx.sess().span_bug(span,
518                                          &format!("Non univariant struct? (repr_target: {:?})",
519                                                   repr_target)),
520             };
521
522             let coerce_index = match kind {
523                 ty::CustomCoerceUnsized::Struct(i) => i
524             };
525             assert!(coerce_index < src_fields.len() && src_fields.len() == target_fields.len());
526
527             let iter = src_fields.iter().zip(target_fields).enumerate();
528             for (i, (src_ty, target_ty)) in iter {
529                 let ll_source = adt::trans_field_ptr(bcx, &repr_source, source.val, 0, i);
530                 let ll_target = adt::trans_field_ptr(bcx, &repr_target, target.val, 0, i);
531
532                 // If this is the field we need to coerce, recurse on it.
533                 if i == coerce_index {
534                     coerce_unsized(bcx, span,
535                                    Datum::new(ll_source, src_ty,
536                                               Rvalue::new(ByRef)),
537                                    Datum::new(ll_target, target_ty,
538                                               Rvalue::new(ByRef)));
539                 } else {
540                     // Otherwise, simply copy the data from the source.
541                     assert_eq!(src_ty, target_ty);
542                     memcpy_ty(bcx, ll_target, ll_source, src_ty);
543                 }
544             }
545         }
546         _ => bcx.sess().bug(&format!("coerce_unsized: invalid coercion {:?} -> {:?}",
547                                      source.ty,
548                                      target.ty))
549     }
550     bcx
551 }
552
553 /// Translates an expression in "lvalue" mode -- meaning that it returns a reference to the memory
554 /// that the expr represents.
555 ///
556 /// If this expression is an rvalue, this implies introducing a temporary.  In other words,
557 /// something like `x().f` is translated into roughly the equivalent of
558 ///
559 ///   { tmp = x(); tmp.f }
560 pub fn trans_to_lvalue<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
561                                    expr: &ast::Expr,
562                                    name: &str)
563                                    -> DatumBlock<'blk, 'tcx, Lvalue> {
564     let mut bcx = bcx;
565     let datum = unpack_datum!(bcx, trans(bcx, expr));
566     return datum.to_lvalue_datum(bcx, name, expr.id);
567 }
568
569 /// A version of `trans` that ignores adjustments. You almost certainly do not want to call this
570 /// directly.
571 fn trans_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
572                                 expr: &ast::Expr)
573                                 -> DatumBlock<'blk, 'tcx, Expr> {
574     let mut bcx = bcx;
575
576     debug!("trans_unadjusted(expr={:?})", expr);
577     let _indenter = indenter();
578
579     debuginfo::set_source_location(bcx.fcx, expr.id, expr.span);
580
581     return match expr_kind(bcx.tcx(), expr) {
582         ExprKind::Lvalue | ExprKind::RvalueDatum => {
583             let datum = unpack_datum!(bcx, {
584                 trans_datum_unadjusted(bcx, expr)
585             });
586
587             DatumBlock {bcx: bcx, datum: datum}
588         }
589
590         ExprKind::RvalueStmt => {
591             bcx = trans_rvalue_stmt_unadjusted(bcx, expr);
592             nil(bcx, expr_ty(bcx, expr))
593         }
594
595         ExprKind::RvalueDps => {
596             let ty = expr_ty(bcx, expr);
597             if type_is_zero_size(bcx.ccx(), ty) {
598                 bcx = trans_rvalue_dps_unadjusted(bcx, expr, Ignore);
599                 nil(bcx, ty)
600             } else {
601                 let scratch = rvalue_scratch_datum(bcx, ty, "");
602                 bcx = trans_rvalue_dps_unadjusted(
603                     bcx, expr, SaveIn(scratch.val));
604
605                 // Note: this is not obviously a good idea.  It causes
606                 // immediate values to be loaded immediately after a
607                 // return from a call or other similar expression,
608                 // which in turn leads to alloca's having shorter
609                 // lifetimes and hence larger stack frames.  However,
610                 // in turn it can lead to more register pressure.
611                 // Still, in practice it seems to increase
612                 // performance, since we have fewer problems with
613                 // morestack churn.
614                 let scratch = unpack_datum!(
615                     bcx, scratch.to_appropriate_datum(bcx));
616
617                 DatumBlock::new(bcx, scratch.to_expr_datum())
618             }
619         }
620     };
621
622     fn nil<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, ty: Ty<'tcx>)
623                        -> DatumBlock<'blk, 'tcx, Expr> {
624         let llval = C_undef(type_of::type_of(bcx.ccx(), ty));
625         let datum = immediate_rvalue(llval, ty);
626         DatumBlock::new(bcx, datum.to_expr_datum())
627     }
628 }
629
630 fn trans_datum_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
631                                       expr: &ast::Expr)
632                                       -> DatumBlock<'blk, 'tcx, Expr> {
633     let mut bcx = bcx;
634     let fcx = bcx.fcx;
635     let _icx = push_ctxt("trans_datum_unadjusted");
636
637     match expr.node {
638         ast::ExprParen(ref e) => {
639             trans(bcx, &**e)
640         }
641         ast::ExprPath(..) => {
642             trans_def(bcx, expr, bcx.def(expr.id))
643         }
644         ast::ExprField(ref base, ident) => {
645             trans_rec_field(bcx, &**base, ident.node.name)
646         }
647         ast::ExprTupField(ref base, idx) => {
648             trans_rec_tup_field(bcx, &**base, idx.node)
649         }
650         ast::ExprIndex(ref base, ref idx) => {
651             trans_index(bcx, expr, &**base, &**idx, MethodCall::expr(expr.id))
652         }
653         ast::ExprBox(_, ref contents) => {
654             // Special case for `Box<T>`
655             let box_ty = expr_ty(bcx, expr);
656             let contents_ty = expr_ty(bcx, &**contents);
657             match box_ty.sty {
658                 ty::TyBox(..) => {
659                     trans_uniq_expr(bcx, expr, box_ty, &**contents, contents_ty)
660                 }
661                 _ => bcx.sess().span_bug(expr.span,
662                                          "expected unique box")
663             }
664
665         }
666         ast::ExprLit(ref lit) => trans_immediate_lit(bcx, expr, &**lit),
667         ast::ExprBinary(op, ref lhs, ref rhs) => {
668             trans_binary(bcx, expr, op, &**lhs, &**rhs)
669         }
670         ast::ExprUnary(op, ref x) => {
671             trans_unary(bcx, expr, op, &**x)
672         }
673         ast::ExprAddrOf(_, ref x) => {
674             match x.node {
675                 ast::ExprRepeat(..) | ast::ExprVec(..) => {
676                     // Special case for slices.
677                     let cleanup_debug_loc =
678                         debuginfo::get_cleanup_debug_loc_for_ast_node(bcx.ccx(),
679                                                                       x.id,
680                                                                       x.span,
681                                                                       false);
682                     fcx.push_ast_cleanup_scope(cleanup_debug_loc);
683                     let datum = unpack_datum!(
684                         bcx, tvec::trans_slice_vec(bcx, expr, &**x));
685                     bcx = fcx.pop_and_trans_ast_cleanup_scope(bcx, x.id);
686                     DatumBlock::new(bcx, datum)
687                 }
688                 _ => {
689                     trans_addr_of(bcx, expr, &**x)
690                 }
691             }
692         }
693         ast::ExprCast(ref val, _) => {
694             // Datum output mode means this is a scalar cast:
695             trans_imm_cast(bcx, &**val, expr.id)
696         }
697         _ => {
698             bcx.tcx().sess.span_bug(
699                 expr.span,
700                 &format!("trans_rvalue_datum_unadjusted reached \
701                          fall-through case: {:?}",
702                         expr.node));
703         }
704     }
705 }
706
707 fn trans_field<'blk, 'tcx, F>(bcx: Block<'blk, 'tcx>,
708                               base: &ast::Expr,
709                               get_idx: F)
710                               -> DatumBlock<'blk, 'tcx, Expr> where
711     F: FnOnce(&'blk ty::ctxt<'tcx>, &VariantInfo<'tcx>) -> usize,
712 {
713     let mut bcx = bcx;
714     let _icx = push_ctxt("trans_rec_field");
715
716     let base_datum = unpack_datum!(bcx, trans_to_lvalue(bcx, base, "field"));
717     let bare_ty = base_datum.ty;
718     let repr = adt::represent_type(bcx.ccx(), bare_ty);
719     let vinfo = VariantInfo::from_ty(bcx.tcx(), bare_ty, None);
720
721     let ix = get_idx(bcx.tcx(), &vinfo);
722     let d = base_datum.get_element(
723         bcx,
724         vinfo.fields[ix].1,
725         |srcval| adt::trans_field_ptr(bcx, &*repr, srcval, vinfo.discr, ix));
726
727     if type_is_sized(bcx.tcx(), d.ty) {
728         DatumBlock { datum: d.to_expr_datum(), bcx: bcx }
729     } else {
730         let scratch = rvalue_scratch_datum(bcx, d.ty, "");
731         Store(bcx, d.val, get_dataptr(bcx, scratch.val));
732         let info = Load(bcx, get_meta(bcx, base_datum.val));
733         Store(bcx, info, get_meta(bcx, scratch.val));
734
735         // Always generate an lvalue datum, because this pointer doesn't own
736         // the data and cleanup is scheduled elsewhere.
737         DatumBlock::new(bcx, Datum::new(scratch.val, scratch.ty, LvalueExpr(d.kind)))
738     }
739 }
740
741 /// Translates `base.field`.
742 fn trans_rec_field<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
743                                base: &ast::Expr,
744                                field: ast::Name)
745                                -> DatumBlock<'blk, 'tcx, Expr> {
746     trans_field(bcx, base, |_, vinfo| vinfo.field_index(field))
747 }
748
749 /// Translates `base.<idx>`.
750 fn trans_rec_tup_field<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
751                                    base: &ast::Expr,
752                                    idx: usize)
753                                    -> DatumBlock<'blk, 'tcx, Expr> {
754     trans_field(bcx, base, |_, _| idx)
755 }
756
757 fn trans_index<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
758                            index_expr: &ast::Expr,
759                            base: &ast::Expr,
760                            idx: &ast::Expr,
761                            method_call: MethodCall)
762                            -> DatumBlock<'blk, 'tcx, Expr> {
763     //! Translates `base[idx]`.
764
765     let _icx = push_ctxt("trans_index");
766     let ccx = bcx.ccx();
767     let mut bcx = bcx;
768
769     let index_expr_debug_loc = index_expr.debug_loc();
770
771     // Check for overloaded index.
772     let method_ty = ccx.tcx()
773                        .tables
774                        .borrow()
775                        .method_map
776                        .get(&method_call)
777                        .map(|method| method.ty);
778     let elt_datum = match method_ty {
779         Some(method_ty) => {
780             let method_ty = monomorphize_type(bcx, method_ty);
781
782             let base_datum = unpack_datum!(bcx, trans(bcx, base));
783
784             // Translate index expression.
785             let ix_datum = unpack_datum!(bcx, trans(bcx, idx));
786
787             let ref_ty = // invoked methods have LB regions instantiated:
788                 bcx.tcx().no_late_bound_regions(&method_ty.fn_ret()).unwrap().unwrap();
789             let elt_ty = match ref_ty.builtin_deref(true) {
790                 None => {
791                     bcx.tcx().sess.span_bug(index_expr.span,
792                                             "index method didn't return a \
793                                              dereferenceable type?!")
794                 }
795                 Some(elt_tm) => elt_tm.ty,
796             };
797
798             // Overloaded. Evaluate `trans_overloaded_op`, which will
799             // invoke the user's index() method, which basically yields
800             // a `&T` pointer.  We can then proceed down the normal
801             // path (below) to dereference that `&T`.
802             let scratch = rvalue_scratch_datum(bcx, ref_ty, "overloaded_index_elt");
803             unpack_result!(bcx,
804                            trans_overloaded_op(bcx,
805                                                index_expr,
806                                                method_call,
807                                                base_datum,
808                                                Some((ix_datum, idx.id)),
809                                                Some(SaveIn(scratch.val)),
810                                                false));
811             let datum = scratch.to_expr_datum();
812             let lval = Lvalue::new("expr::trans_index overload");
813             if type_is_sized(bcx.tcx(), elt_ty) {
814                 Datum::new(datum.to_llscalarish(bcx), elt_ty, LvalueExpr(lval))
815             } else {
816                 Datum::new(datum.val, elt_ty, LvalueExpr(lval))
817             }
818         }
819         None => {
820             let base_datum = unpack_datum!(bcx, trans_to_lvalue(bcx,
821                                                                 base,
822                                                                 "index"));
823
824             // Translate index expression and cast to a suitable LLVM integer.
825             // Rust is less strict than LLVM in this regard.
826             let ix_datum = unpack_datum!(bcx, trans(bcx, idx));
827             let ix_val = ix_datum.to_llscalarish(bcx);
828             let ix_size = machine::llbitsize_of_real(bcx.ccx(),
829                                                      val_ty(ix_val));
830             let int_size = machine::llbitsize_of_real(bcx.ccx(),
831                                                       ccx.int_type());
832             let ix_val = {
833                 if ix_size < int_size {
834                     if expr_ty(bcx, idx).is_signed() {
835                         SExt(bcx, ix_val, ccx.int_type())
836                     } else { ZExt(bcx, ix_val, ccx.int_type()) }
837                 } else if ix_size > int_size {
838                     Trunc(bcx, ix_val, ccx.int_type())
839                 } else {
840                     ix_val
841                 }
842             };
843
844             let unit_ty = base_datum.ty.sequence_element_type(bcx.tcx());
845
846             let (base, len) = base_datum.get_vec_base_and_len(bcx);
847
848             debug!("trans_index: base {}", bcx.val_to_string(base));
849             debug!("trans_index: len {}", bcx.val_to_string(len));
850
851             let bounds_check = ICmp(bcx,
852                                     llvm::IntUGE,
853                                     ix_val,
854                                     len,
855                                     index_expr_debug_loc);
856             let expect = ccx.get_intrinsic(&("llvm.expect.i1"));
857             let expected = Call(bcx,
858                                 expect,
859                                 &[bounds_check, C_bool(ccx, false)],
860                                 None,
861                                 index_expr_debug_loc);
862             bcx = with_cond(bcx, expected, |bcx| {
863                 controlflow::trans_fail_bounds_check(bcx,
864                                                      expr_info(index_expr),
865                                                      ix_val,
866                                                      len)
867             });
868             let elt = InBoundsGEP(bcx, base, &[ix_val]);
869             let elt = PointerCast(bcx, elt, type_of::type_of(ccx, unit_ty).ptr_to());
870             let lval = Lvalue::new("expr::trans_index fallback");
871             Datum::new(elt, unit_ty, LvalueExpr(lval))
872         }
873     };
874
875     DatumBlock::new(bcx, elt_datum)
876 }
877
878 fn trans_def<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
879                          ref_expr: &ast::Expr,
880                          def: def::Def)
881                          -> DatumBlock<'blk, 'tcx, Expr> {
882     //! Translates a reference to a path.
883
884     let _icx = push_ctxt("trans_def_lvalue");
885     match def {
886         def::DefFn(..) | def::DefMethod(..) |
887         def::DefStruct(_) | def::DefVariant(..) => {
888             let datum = trans_def_fn_unadjusted(bcx.ccx(), ref_expr, def,
889                                                 bcx.fcx.param_substs);
890             DatumBlock::new(bcx, datum.to_expr_datum())
891         }
892         def::DefStatic(did, _) => {
893             // There are two things that may happen here:
894             //  1) If the static item is defined in this crate, it will be
895             //     translated using `get_item_val`, and we return a pointer to
896             //     the result.
897             //  2) If the static item is defined in another crate then we add
898             //     (or reuse) a declaration of an external global, and return a
899             //     pointer to that.
900             let const_ty = expr_ty(bcx, ref_expr);
901
902             // For external constants, we don't inline.
903             let val = if did.is_local() {
904                 // Case 1.
905
906                 // The LLVM global has the type of its initializer,
907                 // which may not be equal to the enum's type for
908                 // non-C-like enums.
909                 let val = base::get_item_val(bcx.ccx(), did.node);
910                 let pty = type_of::type_of(bcx.ccx(), const_ty).ptr_to();
911                 PointerCast(bcx, val, pty)
912             } else {
913                 // Case 2.
914                 base::get_extern_const(bcx.ccx(), did, const_ty)
915             };
916             let lval = Lvalue::new("expr::trans_def");
917             DatumBlock::new(bcx, Datum::new(val, const_ty, LvalueExpr(lval)))
918         }
919         def::DefConst(_) => {
920             bcx.sess().span_bug(ref_expr.span,
921                 "constant expression should not reach expr::trans_def")
922         }
923         _ => {
924             DatumBlock::new(bcx, trans_local_var(bcx, def).to_expr_datum())
925         }
926     }
927 }
928
929 fn trans_rvalue_stmt_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
930                                             expr: &ast::Expr)
931                                             -> Block<'blk, 'tcx> {
932     let mut bcx = bcx;
933     let _icx = push_ctxt("trans_rvalue_stmt");
934
935     if bcx.unreachable.get() {
936         return bcx;
937     }
938
939     debuginfo::set_source_location(bcx.fcx, expr.id, expr.span);
940
941     match expr.node {
942         ast::ExprParen(ref e) => {
943             trans_into(bcx, &**e, Ignore)
944         }
945         ast::ExprBreak(label_opt) => {
946             controlflow::trans_break(bcx, expr, label_opt)
947         }
948         ast::ExprAgain(label_opt) => {
949             controlflow::trans_cont(bcx, expr, label_opt)
950         }
951         ast::ExprRet(ref ex) => {
952             // Check to see if the return expression itself is reachable.
953             // This can occur when the inner expression contains a return
954             let reachable = if let Some(ref cfg) = bcx.fcx.cfg {
955                 cfg.node_is_reachable(expr.id)
956             } else {
957                 true
958             };
959
960             if reachable {
961                 controlflow::trans_ret(bcx, expr, ex.as_ref().map(|e| &**e))
962             } else {
963                 // If it's not reachable, just translate the inner expression
964                 // directly. This avoids having to manage a return slot when
965                 // it won't actually be used anyway.
966                 if let &Some(ref x) = ex {
967                     bcx = trans_into(bcx, &**x, Ignore);
968                 }
969                 // Mark the end of the block as unreachable. Once we get to
970                 // a return expression, there's no more we should be doing
971                 // after this.
972                 Unreachable(bcx);
973                 bcx
974             }
975         }
976         ast::ExprWhile(ref cond, ref body, _) => {
977             controlflow::trans_while(bcx, expr, &**cond, &**body)
978         }
979         ast::ExprLoop(ref body, _) => {
980             controlflow::trans_loop(bcx, expr, &**body)
981         }
982         ast::ExprAssign(ref dst, ref src) => {
983             let src_datum = unpack_datum!(bcx, trans(bcx, &**src));
984             let dst_datum = unpack_datum!(bcx, trans_to_lvalue(bcx, &**dst, "assign"));
985
986             if bcx.fcx.type_needs_drop(dst_datum.ty) {
987                 // If there are destructors involved, make sure we
988                 // are copying from an rvalue, since that cannot possible
989                 // alias an lvalue. We are concerned about code like:
990                 //
991                 //   a = a
992                 //
993                 // but also
994                 //
995                 //   a = a.b
996                 //
997                 // where e.g. a : Option<Foo> and a.b :
998                 // Option<Foo>. In that case, freeing `a` before the
999                 // assignment may also free `a.b`!
1000                 //
1001                 // We could avoid this intermediary with some analysis
1002                 // to determine whether `dst` may possibly own `src`.
1003                 debuginfo::set_source_location(bcx.fcx, expr.id, expr.span);
1004                 let src_datum = unpack_datum!(
1005                     bcx, src_datum.to_rvalue_datum(bcx, "ExprAssign"));
1006                 let opt_hint_datum = dst_datum.kind.drop_flag_info.hint_datum(bcx);
1007                 let opt_hint_val = opt_hint_datum.map(|d|d.to_value());
1008
1009                 // 1. Drop the data at the destination, passing the
1010                 //    drop-hint in case the lvalue has already been
1011                 //    dropped or moved.
1012                 bcx = glue::drop_ty_core(bcx,
1013                                          dst_datum.val,
1014                                          dst_datum.ty,
1015                                          expr.debug_loc(),
1016                                          false,
1017                                          opt_hint_val);
1018
1019                 // 2. We are overwriting the destination; ensure that
1020                 //    its drop-hint (if any) says "initialized."
1021                 if let Some(hint_val) = opt_hint_val {
1022                     let hint_llval = hint_val.value();
1023                     let drop_needed = C_u8(bcx.fcx.ccx, adt::DTOR_NEEDED_HINT);
1024                     Store(bcx, drop_needed, hint_llval);
1025                 }
1026                 src_datum.store_to(bcx, dst_datum.val)
1027             } else {
1028                 src_datum.store_to(bcx, dst_datum.val)
1029             }
1030         }
1031         ast::ExprAssignOp(op, ref dst, ref src) => {
1032             trans_assign_op(bcx, expr, op, &**dst, &**src)
1033         }
1034         ast::ExprInlineAsm(ref a) => {
1035             asm::trans_inline_asm(bcx, a)
1036         }
1037         _ => {
1038             bcx.tcx().sess.span_bug(
1039                 expr.span,
1040                 &format!("trans_rvalue_stmt_unadjusted reached \
1041                          fall-through case: {:?}",
1042                         expr.node));
1043         }
1044     }
1045 }
1046
1047 fn trans_rvalue_dps_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
1048                                            expr: &ast::Expr,
1049                                            dest: Dest)
1050                                            -> Block<'blk, 'tcx> {
1051     let _icx = push_ctxt("trans_rvalue_dps_unadjusted");
1052     let mut bcx = bcx;
1053     let tcx = bcx.tcx();
1054
1055     debuginfo::set_source_location(bcx.fcx, expr.id, expr.span);
1056
1057     match expr.node {
1058         ast::ExprParen(ref e) => {
1059             trans_into(bcx, &**e, dest)
1060         }
1061         ast::ExprPath(..) => {
1062             trans_def_dps_unadjusted(bcx, expr, bcx.def(expr.id), dest)
1063         }
1064         ast::ExprIf(ref cond, ref thn, ref els) => {
1065             controlflow::trans_if(bcx, expr.id, &**cond, &**thn, els.as_ref().map(|e| &**e), dest)
1066         }
1067         ast::ExprMatch(ref discr, ref arms, _) => {
1068             _match::trans_match(bcx, expr, &**discr, &arms[..], dest)
1069         }
1070         ast::ExprBlock(ref blk) => {
1071             controlflow::trans_block(bcx, &**blk, dest)
1072         }
1073         ast::ExprStruct(_, ref fields, ref base) => {
1074             trans_struct(bcx,
1075                          &fields[..],
1076                          base.as_ref().map(|e| &**e),
1077                          expr.span,
1078                          expr.id,
1079                          node_id_type(bcx, expr.id),
1080                          dest)
1081         }
1082         ast::ExprRange(ref start, ref end) => {
1083             // FIXME it is just not right that we are synthesising ast nodes in
1084             // trans. Shudder.
1085             fn make_field(field_name: &str, expr: P<ast::Expr>) -> ast::Field {
1086                 ast::Field {
1087                     ident: codemap::dummy_spanned(token::str_to_ident(field_name)),
1088                     expr: expr,
1089                     span: codemap::DUMMY_SP,
1090                 }
1091             }
1092
1093             // A range just desugars into a struct.
1094             // Note that the type of the start and end may not be the same, but
1095             // they should only differ in their lifetime, which should not matter
1096             // in trans.
1097             let (did, fields, ty_params) = match (start, end) {
1098                 (&Some(ref start), &Some(ref end)) => {
1099                     // Desugar to Range
1100                     let fields = vec![make_field("start", start.clone()),
1101                                       make_field("end", end.clone())];
1102                     (tcx.lang_items.range_struct(), fields, vec![node_id_type(bcx, start.id)])
1103                 }
1104                 (&Some(ref start), &None) => {
1105                     // Desugar to RangeFrom
1106                     let fields = vec![make_field("start", start.clone())];
1107                     (tcx.lang_items.range_from_struct(), fields, vec![node_id_type(bcx, start.id)])
1108                 }
1109                 (&None, &Some(ref end)) => {
1110                     // Desugar to RangeTo
1111                     let fields = vec![make_field("end", end.clone())];
1112                     (tcx.lang_items.range_to_struct(), fields, vec![node_id_type(bcx, end.id)])
1113                 }
1114                 _ => {
1115                     // Desugar to RangeFull
1116                     (tcx.lang_items.range_full_struct(), vec![], vec![])
1117                 }
1118             };
1119
1120             if let Some(did) = did {
1121                 let substs = Substs::new_type(ty_params, vec![]);
1122                 trans_struct(bcx,
1123                              &fields,
1124                              None,
1125                              expr.span,
1126                              expr.id,
1127                              tcx.mk_struct(tcx.lookup_adt_def(did),
1128                                            tcx.mk_substs(substs)),
1129                              dest)
1130             } else {
1131                 tcx.sess.span_bug(expr.span,
1132                                   "No lang item for ranges (how did we get this far?)")
1133             }
1134         }
1135         ast::ExprTup(ref args) => {
1136             let numbered_fields: Vec<(usize, &ast::Expr)> =
1137                 args.iter().enumerate().map(|(i, arg)| (i, &**arg)).collect();
1138             trans_adt(bcx,
1139                       expr_ty(bcx, expr),
1140                       0,
1141                       &numbered_fields[..],
1142                       None,
1143                       dest,
1144                       expr.debug_loc())
1145         }
1146         ast::ExprLit(ref lit) => {
1147             match lit.node {
1148                 ast::LitStr(ref s, _) => {
1149                     tvec::trans_lit_str(bcx, expr, (*s).clone(), dest)
1150                 }
1151                 _ => {
1152                     bcx.tcx()
1153                        .sess
1154                        .span_bug(expr.span,
1155                                  "trans_rvalue_dps_unadjusted shouldn't be \
1156                                   translating this type of literal")
1157                 }
1158             }
1159         }
1160         ast::ExprVec(..) | ast::ExprRepeat(..) => {
1161             tvec::trans_fixed_vstore(bcx, expr, dest)
1162         }
1163         ast::ExprClosure(_, ref decl, ref body) => {
1164             let dest = match dest {
1165                 SaveIn(lldest) => closure::Dest::SaveIn(bcx, lldest),
1166                 Ignore => closure::Dest::Ignore(bcx.ccx())
1167             };
1168             let substs = match expr_ty(bcx, expr).sty {
1169                 ty::TyClosure(_, ref substs) => substs,
1170                 ref t =>
1171                     bcx.tcx().sess.span_bug(
1172                         expr.span,
1173                         &format!("closure expr without closure type: {:?}", t)),
1174             };
1175             closure::trans_closure_expr(dest, decl, body, expr.id, substs).unwrap_or(bcx)
1176         }
1177         ast::ExprCall(ref f, ref args) => {
1178             if bcx.tcx().is_method_call(expr.id) {
1179                 trans_overloaded_call(bcx,
1180                                       expr,
1181                                       &**f,
1182                                       &args[..],
1183                                       Some(dest))
1184             } else {
1185                 callee::trans_call(bcx,
1186                                    expr,
1187                                    &**f,
1188                                    callee::ArgExprs(&args[..]),
1189                                    dest)
1190             }
1191         }
1192         ast::ExprMethodCall(_, _, ref args) => {
1193             callee::trans_method_call(bcx,
1194                                       expr,
1195                                       &*args[0],
1196                                       callee::ArgExprs(&args[..]),
1197                                       dest)
1198         }
1199         ast::ExprBinary(op, ref lhs, ref rhs) => {
1200             // if not overloaded, would be RvalueDatumExpr
1201             let lhs = unpack_datum!(bcx, trans(bcx, &**lhs));
1202             let rhs_datum = unpack_datum!(bcx, trans(bcx, &**rhs));
1203             trans_overloaded_op(bcx, expr, MethodCall::expr(expr.id), lhs,
1204                                 Some((rhs_datum, rhs.id)), Some(dest),
1205                                 !ast_util::is_by_value_binop(op.node)).bcx
1206         }
1207         ast::ExprUnary(op, ref subexpr) => {
1208             // if not overloaded, would be RvalueDatumExpr
1209             let arg = unpack_datum!(bcx, trans(bcx, &**subexpr));
1210             trans_overloaded_op(bcx, expr, MethodCall::expr(expr.id),
1211                                 arg, None, Some(dest), !ast_util::is_by_value_unop(op)).bcx
1212         }
1213         ast::ExprIndex(ref base, ref idx) => {
1214             // if not overloaded, would be RvalueDatumExpr
1215             let base = unpack_datum!(bcx, trans(bcx, &**base));
1216             let idx_datum = unpack_datum!(bcx, trans(bcx, &**idx));
1217             trans_overloaded_op(bcx, expr, MethodCall::expr(expr.id), base,
1218                                 Some((idx_datum, idx.id)), Some(dest), true).bcx
1219         }
1220         ast::ExprCast(..) => {
1221             // Trait casts used to come this way, now they should be coercions.
1222             bcx.tcx().sess.span_bug(expr.span, "DPS expr_cast (residual trait cast?)")
1223         }
1224         ast::ExprAssignOp(op, ref dst, ref src) => {
1225             trans_assign_op(bcx, expr, op, &**dst, &**src)
1226         }
1227         _ => {
1228             bcx.tcx().sess.span_bug(
1229                 expr.span,
1230                 &format!("trans_rvalue_dps_unadjusted reached fall-through \
1231                          case: {:?}",
1232                         expr.node));
1233         }
1234     }
1235 }
1236
1237 fn trans_def_dps_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
1238                                         ref_expr: &ast::Expr,
1239                                         def: def::Def,
1240                                         dest: Dest)
1241                                         -> Block<'blk, 'tcx> {
1242     let _icx = push_ctxt("trans_def_dps_unadjusted");
1243
1244     let lldest = match dest {
1245         SaveIn(lldest) => lldest,
1246         Ignore => { return bcx; }
1247     };
1248
1249     match def {
1250         def::DefVariant(tid, vid, _) => {
1251             let variant = bcx.tcx().lookup_adt_def(tid).variant_with_id(vid);
1252             if let ty::VariantKind::Tuple = variant.kind() {
1253                 // N-ary variant.
1254                 let llfn = callee::trans_fn_ref(bcx.ccx(), vid,
1255                                                 ExprId(ref_expr.id),
1256                                                 bcx.fcx.param_substs).val;
1257                 Store(bcx, llfn, lldest);
1258                 return bcx;
1259             } else {
1260                 // Nullary variant.
1261                 let ty = expr_ty(bcx, ref_expr);
1262                 let repr = adt::represent_type(bcx.ccx(), ty);
1263                 adt::trans_set_discr(bcx, &*repr, lldest, variant.disr_val);
1264                 return bcx;
1265             }
1266         }
1267         def::DefStruct(_) => {
1268             let ty = expr_ty(bcx, ref_expr);
1269             match ty.sty {
1270                 ty::TyStruct(def, _) if def.has_dtor() => {
1271                     let repr = adt::represent_type(bcx.ccx(), ty);
1272                     adt::trans_set_discr(bcx, &*repr, lldest, 0);
1273                 }
1274                 _ => {}
1275             }
1276             bcx
1277         }
1278         _ => {
1279             bcx.tcx().sess.span_bug(ref_expr.span, &format!(
1280                 "Non-DPS def {:?} referened by {}",
1281                 def, bcx.node_id_to_string(ref_expr.id)));
1282         }
1283     }
1284 }
1285
1286 pub fn trans_def_fn_unadjusted<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
1287                                          ref_expr: &ast::Expr,
1288                                          def: def::Def,
1289                                          param_substs: &'tcx Substs<'tcx>)
1290                                          -> Datum<'tcx, Rvalue> {
1291     let _icx = push_ctxt("trans_def_datum_unadjusted");
1292
1293     match def {
1294         def::DefFn(did, _) |
1295         def::DefStruct(did) | def::DefVariant(_, did, _) => {
1296             callee::trans_fn_ref(ccx, did, ExprId(ref_expr.id), param_substs)
1297         }
1298         def::DefMethod(method_did) => {
1299             match ccx.tcx().impl_or_trait_item(method_did).container() {
1300                 ty::ImplContainer(_) => {
1301                     callee::trans_fn_ref(ccx, method_did,
1302                                          ExprId(ref_expr.id),
1303                                          param_substs)
1304                 }
1305                 ty::TraitContainer(trait_did) => {
1306                     meth::trans_static_method_callee(ccx, method_did,
1307                                                      trait_did, ref_expr.id,
1308                                                      param_substs)
1309                 }
1310             }
1311         }
1312         _ => {
1313             ccx.tcx().sess.span_bug(ref_expr.span, &format!(
1314                     "trans_def_fn_unadjusted invoked on: {:?} for {:?}",
1315                     def,
1316                     ref_expr));
1317         }
1318     }
1319 }
1320
1321 /// Translates a reference to a local variable or argument. This always results in an lvalue datum.
1322 pub fn trans_local_var<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
1323                                    def: def::Def)
1324                                    -> Datum<'tcx, Lvalue> {
1325     let _icx = push_ctxt("trans_local_var");
1326
1327     match def {
1328         def::DefUpvar(nid, _) => {
1329             // Can't move upvars, so this is never a ZeroMemLastUse.
1330             let local_ty = node_id_type(bcx, nid);
1331             let lval = Lvalue::new_with_hint("expr::trans_local_var (upvar)",
1332                                              bcx, nid, HintKind::ZeroAndMaintain);
1333             match bcx.fcx.llupvars.borrow().get(&nid) {
1334                 Some(&val) => Datum::new(val, local_ty, lval),
1335                 None => {
1336                     bcx.sess().bug(&format!(
1337                         "trans_local_var: no llval for upvar {} found",
1338                         nid));
1339                 }
1340             }
1341         }
1342         def::DefLocal(nid) => {
1343             let datum = match bcx.fcx.lllocals.borrow().get(&nid) {
1344                 Some(&v) => v,
1345                 None => {
1346                     bcx.sess().bug(&format!(
1347                         "trans_local_var: no datum for local/arg {} found",
1348                         nid));
1349                 }
1350             };
1351             debug!("take_local(nid={}, v={}, ty={})",
1352                    nid, bcx.val_to_string(datum.val), datum.ty);
1353             datum
1354         }
1355         _ => {
1356             bcx.sess().unimpl(&format!(
1357                 "unsupported def type in trans_local_var: {:?}",
1358                 def));
1359         }
1360     }
1361 }
1362
1363 fn trans_struct<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
1364                             fields: &[ast::Field],
1365                             base: Option<&ast::Expr>,
1366                             expr_span: codemap::Span,
1367                             expr_id: ast::NodeId,
1368                             ty: Ty<'tcx>,
1369                             dest: Dest) -> Block<'blk, 'tcx> {
1370     let _icx = push_ctxt("trans_rec");
1371
1372     let tcx = bcx.tcx();
1373     let vinfo = VariantInfo::of_node(tcx, ty, expr_id);
1374
1375     let mut need_base = vec![true; vinfo.fields.len()];
1376
1377     let numbered_fields = fields.iter().map(|field| {
1378         let pos = vinfo.field_index(field.ident.node.name);
1379         need_base[pos] = false;
1380         (pos, &*field.expr)
1381     }).collect::<Vec<_>>();
1382
1383     let optbase = match base {
1384         Some(base_expr) => {
1385             let mut leftovers = Vec::new();
1386             for (i, b) in need_base.iter().enumerate() {
1387                 if *b {
1388                     leftovers.push((i, vinfo.fields[i].1));
1389                 }
1390             }
1391             Some(StructBaseInfo {expr: base_expr,
1392                                  fields: leftovers })
1393         }
1394         None => {
1395             if need_base.iter().any(|b| *b) {
1396                 tcx.sess.span_bug(expr_span, "missing fields and no base expr")
1397             }
1398             None
1399         }
1400     };
1401
1402     trans_adt(bcx,
1403               ty,
1404               vinfo.discr,
1405               &numbered_fields,
1406               optbase,
1407               dest,
1408               DebugLoc::At(expr_id, expr_span))
1409 }
1410
1411 /// Information that `trans_adt` needs in order to fill in the fields
1412 /// of a struct copied from a base struct (e.g., from an expression
1413 /// like `Foo { a: b, ..base }`.
1414 ///
1415 /// Note that `fields` may be empty; the base expression must always be
1416 /// evaluated for side-effects.
1417 pub struct StructBaseInfo<'a, 'tcx> {
1418     /// The base expression; will be evaluated after all explicit fields.
1419     expr: &'a ast::Expr,
1420     /// The indices of fields to copy paired with their types.
1421     fields: Vec<(usize, Ty<'tcx>)>
1422 }
1423
1424 /// Constructs an ADT instance:
1425 ///
1426 /// - `fields` should be a list of field indices paired with the
1427 /// expression to store into that field.  The initializers will be
1428 /// evaluated in the order specified by `fields`.
1429 ///
1430 /// - `optbase` contains information on the base struct (if any) from
1431 /// which remaining fields are copied; see comments on `StructBaseInfo`.
1432 pub fn trans_adt<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
1433                                  ty: Ty<'tcx>,
1434                                  discr: ty::Disr,
1435                                  fields: &[(usize, &ast::Expr)],
1436                                  optbase: Option<StructBaseInfo<'a, 'tcx>>,
1437                                  dest: Dest,
1438                                  debug_location: DebugLoc)
1439                                  -> Block<'blk, 'tcx> {
1440     let _icx = push_ctxt("trans_adt");
1441     let fcx = bcx.fcx;
1442     let repr = adt::represent_type(bcx.ccx(), ty);
1443
1444     debug_location.apply(bcx.fcx);
1445
1446     // If we don't care about the result, just make a
1447     // temporary stack slot
1448     let addr = match dest {
1449         SaveIn(pos) => pos,
1450         Ignore => alloc_ty(bcx, ty, "temp"),
1451     };
1452
1453     // This scope holds intermediates that must be cleaned should
1454     // panic occur before the ADT as a whole is ready.
1455     let custom_cleanup_scope = fcx.push_custom_cleanup_scope();
1456
1457     if ty.is_simd() {
1458         // Issue 23112: The original logic appeared vulnerable to same
1459         // order-of-eval bug. But, SIMD values are tuple-structs;
1460         // i.e. functional record update (FRU) syntax is unavailable.
1461         //
1462         // To be safe, double-check that we did not get here via FRU.
1463         assert!(optbase.is_none());
1464
1465         // This is the constructor of a SIMD type, such types are
1466         // always primitive machine types and so do not have a
1467         // destructor or require any clean-up.
1468         let llty = type_of::type_of(bcx.ccx(), ty);
1469
1470         // keep a vector as a register, and running through the field
1471         // `insertelement`ing them directly into that register
1472         // (i.e. avoid GEPi and `store`s to an alloca) .
1473         let mut vec_val = C_undef(llty);
1474
1475         for &(i, ref e) in fields {
1476             let block_datum = trans(bcx, &**e);
1477             bcx = block_datum.bcx;
1478             let position = C_uint(bcx.ccx(), i);
1479             let value = block_datum.datum.to_llscalarish(bcx);
1480             vec_val = InsertElement(bcx, vec_val, value, position);
1481         }
1482         Store(bcx, vec_val, addr);
1483     } else if let Some(base) = optbase {
1484         // Issue 23112: If there is a base, then order-of-eval
1485         // requires field expressions eval'ed before base expression.
1486
1487         // First, trans field expressions to temporary scratch values.
1488         let scratch_vals: Vec<_> = fields.iter().map(|&(i, ref e)| {
1489             let datum = unpack_datum!(bcx, trans(bcx, &**e));
1490             (i, datum)
1491         }).collect();
1492
1493         debug_location.apply(bcx.fcx);
1494
1495         // Second, trans the base to the dest.
1496         assert_eq!(discr, 0);
1497
1498         match expr_kind(bcx.tcx(), &*base.expr) {
1499             ExprKind::RvalueDps | ExprKind::RvalueDatum if !bcx.fcx.type_needs_drop(ty) => {
1500                 bcx = trans_into(bcx, &*base.expr, SaveIn(addr));
1501             },
1502             ExprKind::RvalueStmt => {
1503                 bcx.tcx().sess.bug("unexpected expr kind for struct base expr")
1504             }
1505             _ => {
1506                 let base_datum = unpack_datum!(bcx, trans_to_lvalue(bcx, &*base.expr, "base"));
1507                 for &(i, t) in &base.fields {
1508                     let datum = base_datum.get_element(
1509                             bcx, t, |srcval| adt::trans_field_ptr(bcx, &*repr, srcval, discr, i));
1510                     assert!(type_is_sized(bcx.tcx(), datum.ty));
1511                     let dest = adt::trans_field_ptr(bcx, &*repr, addr, discr, i);
1512                     bcx = datum.store_to(bcx, dest);
1513                 }
1514             }
1515         }
1516
1517         // Finally, move scratch field values into actual field locations
1518         for (i, datum) in scratch_vals {
1519             let dest = adt::trans_field_ptr(bcx, &*repr, addr, discr, i);
1520             bcx = datum.store_to(bcx, dest);
1521         }
1522     } else {
1523         // No base means we can write all fields directly in place.
1524         for &(i, ref e) in fields {
1525             let dest = adt::trans_field_ptr(bcx, &*repr, addr, discr, i);
1526             let e_ty = expr_ty_adjusted(bcx, &**e);
1527             bcx = trans_into(bcx, &**e, SaveIn(dest));
1528             let scope = cleanup::CustomScope(custom_cleanup_scope);
1529             fcx.schedule_lifetime_end(scope, dest);
1530             // FIXME: nonzeroing move should generalize to fields
1531             fcx.schedule_drop_mem(scope, dest, e_ty, None);
1532         }
1533     }
1534
1535     adt::trans_set_discr(bcx, &*repr, addr, discr);
1536
1537     fcx.pop_custom_cleanup_scope(custom_cleanup_scope);
1538
1539     // If we don't care about the result drop the temporary we made
1540     match dest {
1541         SaveIn(_) => bcx,
1542         Ignore => {
1543             bcx = glue::drop_ty(bcx, addr, ty, debug_location);
1544             base::call_lifetime_end(bcx, addr);
1545             bcx
1546         }
1547     }
1548 }
1549
1550
1551 fn trans_immediate_lit<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
1552                                    expr: &ast::Expr,
1553                                    lit: &ast::Lit)
1554                                    -> DatumBlock<'blk, 'tcx, Expr> {
1555     // must not be a string constant, that is a RvalueDpsExpr
1556     let _icx = push_ctxt("trans_immediate_lit");
1557     let ty = expr_ty(bcx, expr);
1558     let v = consts::const_lit(bcx.ccx(), expr, lit);
1559     immediate_rvalue_bcx(bcx, v, ty).to_expr_datumblock()
1560 }
1561
1562 fn trans_unary<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
1563                            expr: &ast::Expr,
1564                            op: ast::UnOp,
1565                            sub_expr: &ast::Expr)
1566                            -> DatumBlock<'blk, 'tcx, Expr> {
1567     let ccx = bcx.ccx();
1568     let mut bcx = bcx;
1569     let _icx = push_ctxt("trans_unary_datum");
1570
1571     let method_call = MethodCall::expr(expr.id);
1572
1573     // The only overloaded operator that is translated to a datum
1574     // is an overloaded deref, since it is always yields a `&T`.
1575     // Otherwise, we should be in the RvalueDpsExpr path.
1576     assert!(op == ast::UnDeref || !ccx.tcx().is_method_call(expr.id));
1577
1578     let un_ty = expr_ty(bcx, expr);
1579
1580     let debug_loc = expr.debug_loc();
1581
1582     match op {
1583         ast::UnNot => {
1584             let datum = unpack_datum!(bcx, trans(bcx, sub_expr));
1585             let llresult = Not(bcx, datum.to_llscalarish(bcx), debug_loc);
1586             immediate_rvalue_bcx(bcx, llresult, un_ty).to_expr_datumblock()
1587         }
1588         ast::UnNeg => {
1589             let datum = unpack_datum!(bcx, trans(bcx, sub_expr));
1590             let val = datum.to_llscalarish(bcx);
1591             let (bcx, llneg) = {
1592                 if un_ty.is_fp() {
1593                     let result = FNeg(bcx, val, debug_loc);
1594                     (bcx, result)
1595                 } else {
1596                     let is_signed = un_ty.is_signed();
1597                     let result = Neg(bcx, val, debug_loc);
1598                     let bcx = if bcx.ccx().check_overflow() && is_signed {
1599                         let (llty, min) = base::llty_and_min_for_signed_ty(bcx, un_ty);
1600                         let is_min = ICmp(bcx, llvm::IntEQ, val,
1601                                           C_integral(llty, min, true), debug_loc);
1602                         with_cond(bcx, is_min, |bcx| {
1603                             let msg = InternedString::new(
1604                                 "attempted to negate with overflow");
1605                             controlflow::trans_fail(bcx, expr_info(expr), msg)
1606                         })
1607                     } else {
1608                         bcx
1609                     };
1610                     (bcx, result)
1611                 }
1612             };
1613             immediate_rvalue_bcx(bcx, llneg, un_ty).to_expr_datumblock()
1614         }
1615         ast::UnUniq => {
1616             trans_uniq_expr(bcx, expr, un_ty, sub_expr, expr_ty(bcx, sub_expr))
1617         }
1618         ast::UnDeref => {
1619             let datum = unpack_datum!(bcx, trans(bcx, sub_expr));
1620             deref_once(bcx, expr, datum, method_call)
1621         }
1622     }
1623 }
1624
1625 fn trans_uniq_expr<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
1626                                box_expr: &ast::Expr,
1627                                box_ty: Ty<'tcx>,
1628                                contents: &ast::Expr,
1629                                contents_ty: Ty<'tcx>)
1630                                -> DatumBlock<'blk, 'tcx, Expr> {
1631     let _icx = push_ctxt("trans_uniq_expr");
1632     let fcx = bcx.fcx;
1633     assert!(type_is_sized(bcx.tcx(), contents_ty));
1634     let llty = type_of::type_of(bcx.ccx(), contents_ty);
1635     let size = llsize_of(bcx.ccx(), llty);
1636     let align = C_uint(bcx.ccx(), type_of::align_of(bcx.ccx(), contents_ty));
1637     let llty_ptr = llty.ptr_to();
1638     let Result { bcx, val } = malloc_raw_dyn(bcx,
1639                                              llty_ptr,
1640                                              box_ty,
1641                                              size,
1642                                              align,
1643                                              box_expr.debug_loc());
1644     // Unique boxes do not allocate for zero-size types. The standard library
1645     // may assume that `free` is never called on the pointer returned for
1646     // `Box<ZeroSizeType>`.
1647     let bcx = if llsize_of_alloc(bcx.ccx(), llty) == 0 {
1648         trans_into(bcx, contents, SaveIn(val))
1649     } else {
1650         let custom_cleanup_scope = fcx.push_custom_cleanup_scope();
1651         fcx.schedule_free_value(cleanup::CustomScope(custom_cleanup_scope),
1652                                 val, cleanup::HeapExchange, contents_ty);
1653         let bcx = trans_into(bcx, contents, SaveIn(val));
1654         fcx.pop_custom_cleanup_scope(custom_cleanup_scope);
1655         bcx
1656     };
1657     immediate_rvalue_bcx(bcx, val, box_ty).to_expr_datumblock()
1658 }
1659
1660 fn ref_fat_ptr<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
1661                            lval: Datum<'tcx, Lvalue>)
1662                            -> DatumBlock<'blk, 'tcx, Expr> {
1663     let dest_ty = bcx.tcx().mk_imm_ref(bcx.tcx().mk_region(ty::ReStatic), lval.ty);
1664     let scratch = rvalue_scratch_datum(bcx, dest_ty, "__fat_ptr");
1665     memcpy_ty(bcx, scratch.val, lval.val, scratch.ty);
1666
1667     DatumBlock::new(bcx, scratch.to_expr_datum())
1668 }
1669
1670 fn trans_addr_of<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
1671                              expr: &ast::Expr,
1672                              subexpr: &ast::Expr)
1673                              -> DatumBlock<'blk, 'tcx, Expr> {
1674     let _icx = push_ctxt("trans_addr_of");
1675     let mut bcx = bcx;
1676     let sub_datum = unpack_datum!(bcx, trans_to_lvalue(bcx, subexpr, "addr_of"));
1677     if !type_is_sized(bcx.tcx(), sub_datum.ty) {
1678         // DST lvalue, close to a fat pointer
1679         ref_fat_ptr(bcx, sub_datum)
1680     } else {
1681         // Sized value, ref to a thin pointer
1682         let ty = expr_ty(bcx, expr);
1683         immediate_rvalue_bcx(bcx, sub_datum.val, ty).to_expr_datumblock()
1684     }
1685 }
1686
1687 // Important to get types for both lhs and rhs, because one might be _|_
1688 // and the other not.
1689 fn trans_eager_binop<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
1690                                  binop_expr: &ast::Expr,
1691                                  binop_ty: Ty<'tcx>,
1692                                  op: ast::BinOp,
1693                                  lhs_t: Ty<'tcx>,
1694                                  lhs: ValueRef,
1695                                  rhs_t: Ty<'tcx>,
1696                                  rhs: ValueRef)
1697                                  -> DatumBlock<'blk, 'tcx, Expr> {
1698     let _icx = push_ctxt("trans_eager_binop");
1699
1700     let tcx = bcx.tcx();
1701     let is_simd = lhs_t.is_simd();
1702     let intype = if is_simd {
1703         lhs_t.simd_type(tcx)
1704     } else {
1705         lhs_t
1706     };
1707     let is_float = intype.is_fp();
1708     let is_signed = intype.is_signed();
1709     let info = expr_info(binop_expr);
1710
1711     let binop_debug_loc = binop_expr.debug_loc();
1712
1713     let mut bcx = bcx;
1714     let val = match op.node {
1715       ast::BiAdd => {
1716         if is_float {
1717             FAdd(bcx, lhs, rhs, binop_debug_loc)
1718         } else if is_simd {
1719             Add(bcx, lhs, rhs, binop_debug_loc)
1720         } else {
1721             let (newbcx, res) = with_overflow_check(
1722                 bcx, OverflowOp::Add, info, lhs_t, lhs, rhs, binop_debug_loc);
1723             bcx = newbcx;
1724             res
1725         }
1726       }
1727       ast::BiSub => {
1728         if is_float {
1729             FSub(bcx, lhs, rhs, binop_debug_loc)
1730         } else if is_simd {
1731             Sub(bcx, lhs, rhs, binop_debug_loc)
1732         } else {
1733             let (newbcx, res) = with_overflow_check(
1734                 bcx, OverflowOp::Sub, info, lhs_t, lhs, rhs, binop_debug_loc);
1735             bcx = newbcx;
1736             res
1737         }
1738       }
1739       ast::BiMul => {
1740         if is_float {
1741             FMul(bcx, lhs, rhs, binop_debug_loc)
1742         } else if is_simd {
1743             Mul(bcx, lhs, rhs, binop_debug_loc)
1744         } else {
1745             let (newbcx, res) = with_overflow_check(
1746                 bcx, OverflowOp::Mul, info, lhs_t, lhs, rhs, binop_debug_loc);
1747             bcx = newbcx;
1748             res
1749         }
1750       }
1751       ast::BiDiv => {
1752         if is_float {
1753             FDiv(bcx, lhs, rhs, binop_debug_loc)
1754         } else {
1755             // Only zero-check integers; fp /0 is NaN
1756             bcx = base::fail_if_zero_or_overflows(bcx,
1757                                                   expr_info(binop_expr),
1758                                                   op,
1759                                                   lhs,
1760                                                   rhs,
1761                                                   rhs_t);
1762             if is_signed {
1763                 SDiv(bcx, lhs, rhs, binop_debug_loc)
1764             } else {
1765                 UDiv(bcx, lhs, rhs, binop_debug_loc)
1766             }
1767         }
1768       }
1769       ast::BiRem => {
1770         if is_float {
1771             // LLVM currently always lowers the `frem` instructions appropriate
1772             // library calls typically found in libm. Notably f64 gets wired up
1773             // to `fmod` and f32 gets wired up to `fmodf`. Inconveniently for
1774             // us, 32-bit MSVC does not actually have a `fmodf` symbol, it's
1775             // instead just an inline function in a header that goes up to a
1776             // f64, uses `fmod`, and then comes back down to a f32.
1777             //
1778             // Although LLVM knows that `fmodf` doesn't exist on MSVC, it will
1779             // still unconditionally lower frem instructions over 32-bit floats
1780             // to a call to `fmodf`. To work around this we special case MSVC
1781             // 32-bit float rem instructions and instead do the call out to
1782             // `fmod` ourselves.
1783             //
1784             // Note that this is currently duplicated with src/libcore/ops.rs
1785             // which does the same thing, and it would be nice to perhaps unify
1786             // these two implementations on day! Also note that we call `fmod`
1787             // for both 32 and 64-bit floats because if we emit any FRem
1788             // instruction at all then LLVM is capable of optimizing it into a
1789             // 32-bit FRem (which we're trying to avoid).
1790             let use_fmod = tcx.sess.target.target.options.is_like_msvc &&
1791                            tcx.sess.target.target.arch == "x86";
1792             if use_fmod {
1793                 let f64t = Type::f64(bcx.ccx());
1794                 let fty = Type::func(&[f64t, f64t], &f64t);
1795                 let llfn = declare::declare_cfn(bcx.ccx(), "fmod", fty,
1796                                                 tcx.types.f64);
1797                 if lhs_t == tcx.types.f32 {
1798                     let lhs = FPExt(bcx, lhs, f64t);
1799                     let rhs = FPExt(bcx, rhs, f64t);
1800                     let res = Call(bcx, llfn, &[lhs, rhs], None, binop_debug_loc);
1801                     FPTrunc(bcx, res, Type::f32(bcx.ccx()))
1802                 } else {
1803                     Call(bcx, llfn, &[lhs, rhs], None, binop_debug_loc)
1804                 }
1805             } else {
1806                 FRem(bcx, lhs, rhs, binop_debug_loc)
1807             }
1808         } else {
1809             // Only zero-check integers; fp %0 is NaN
1810             bcx = base::fail_if_zero_or_overflows(bcx,
1811                                                   expr_info(binop_expr),
1812                                                   op, lhs, rhs, rhs_t);
1813             if is_signed {
1814                 SRem(bcx, lhs, rhs, binop_debug_loc)
1815             } else {
1816                 URem(bcx, lhs, rhs, binop_debug_loc)
1817             }
1818         }
1819       }
1820       ast::BiBitOr => Or(bcx, lhs, rhs, binop_debug_loc),
1821       ast::BiBitAnd => And(bcx, lhs, rhs, binop_debug_loc),
1822       ast::BiBitXor => Xor(bcx, lhs, rhs, binop_debug_loc),
1823       ast::BiShl => {
1824           let (newbcx, res) = with_overflow_check(
1825               bcx, OverflowOp::Shl, info, lhs_t, lhs, rhs, binop_debug_loc);
1826           bcx = newbcx;
1827           res
1828       }
1829       ast::BiShr => {
1830           let (newbcx, res) = with_overflow_check(
1831               bcx, OverflowOp::Shr, info, lhs_t, lhs, rhs, binop_debug_loc);
1832           bcx = newbcx;
1833           res
1834       }
1835       ast::BiEq | ast::BiNe | ast::BiLt | ast::BiGe | ast::BiLe | ast::BiGt => {
1836         if is_simd {
1837             base::compare_simd_types(bcx, lhs, rhs, intype, val_ty(lhs), op.node, binop_debug_loc)
1838         } else {
1839             base::compare_scalar_types(bcx, lhs, rhs, intype, op.node, binop_debug_loc)
1840         }
1841       }
1842       _ => {
1843         bcx.tcx().sess.span_bug(binop_expr.span, "unexpected binop");
1844       }
1845     };
1846
1847     immediate_rvalue_bcx(bcx, val, binop_ty).to_expr_datumblock()
1848 }
1849
1850 // refinement types would obviate the need for this
1851 enum lazy_binop_ty {
1852     lazy_and,
1853     lazy_or,
1854 }
1855
1856 fn trans_lazy_binop<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
1857                                 binop_expr: &ast::Expr,
1858                                 op: lazy_binop_ty,
1859                                 a: &ast::Expr,
1860                                 b: &ast::Expr)
1861                                 -> DatumBlock<'blk, 'tcx, Expr> {
1862     let _icx = push_ctxt("trans_lazy_binop");
1863     let binop_ty = expr_ty(bcx, binop_expr);
1864     let fcx = bcx.fcx;
1865
1866     let DatumBlock {bcx: past_lhs, datum: lhs} = trans(bcx, a);
1867     let lhs = lhs.to_llscalarish(past_lhs);
1868
1869     if past_lhs.unreachable.get() {
1870         return immediate_rvalue_bcx(past_lhs, lhs, binop_ty).to_expr_datumblock();
1871     }
1872
1873     let join = fcx.new_id_block("join", binop_expr.id);
1874     let before_rhs = fcx.new_id_block("before_rhs", b.id);
1875
1876     match op {
1877       lazy_and => CondBr(past_lhs, lhs, before_rhs.llbb, join.llbb, DebugLoc::None),
1878       lazy_or => CondBr(past_lhs, lhs, join.llbb, before_rhs.llbb, DebugLoc::None)
1879     }
1880
1881     let DatumBlock {bcx: past_rhs, datum: rhs} = trans(before_rhs, b);
1882     let rhs = rhs.to_llscalarish(past_rhs);
1883
1884     if past_rhs.unreachable.get() {
1885         return immediate_rvalue_bcx(join, lhs, binop_ty).to_expr_datumblock();
1886     }
1887
1888     Br(past_rhs, join.llbb, DebugLoc::None);
1889     let phi = Phi(join, Type::i1(bcx.ccx()), &[lhs, rhs],
1890                   &[past_lhs.llbb, past_rhs.llbb]);
1891
1892     return immediate_rvalue_bcx(join, phi, binop_ty).to_expr_datumblock();
1893 }
1894
1895 fn trans_binary<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
1896                             expr: &ast::Expr,
1897                             op: ast::BinOp,
1898                             lhs: &ast::Expr,
1899                             rhs: &ast::Expr)
1900                             -> DatumBlock<'blk, 'tcx, Expr> {
1901     let _icx = push_ctxt("trans_binary");
1902     let ccx = bcx.ccx();
1903
1904     // if overloaded, would be RvalueDpsExpr
1905     assert!(!ccx.tcx().is_method_call(expr.id));
1906
1907     match op.node {
1908         ast::BiAnd => {
1909             trans_lazy_binop(bcx, expr, lazy_and, lhs, rhs)
1910         }
1911         ast::BiOr => {
1912             trans_lazy_binop(bcx, expr, lazy_or, lhs, rhs)
1913         }
1914         _ => {
1915             let mut bcx = bcx;
1916             let lhs_datum = unpack_datum!(bcx, trans(bcx, lhs));
1917             let rhs_datum = unpack_datum!(bcx, trans(bcx, rhs));
1918             let binop_ty = expr_ty(bcx, expr);
1919
1920             debug!("trans_binary (expr {}): lhs_datum={}",
1921                    expr.id,
1922                    lhs_datum.to_string(ccx));
1923             let lhs_ty = lhs_datum.ty;
1924             let lhs = lhs_datum.to_llscalarish(bcx);
1925
1926             debug!("trans_binary (expr {}): rhs_datum={}",
1927                    expr.id,
1928                    rhs_datum.to_string(ccx));
1929             let rhs_ty = rhs_datum.ty;
1930             let rhs = rhs_datum.to_llscalarish(bcx);
1931             trans_eager_binop(bcx, expr, binop_ty, op,
1932                               lhs_ty, lhs, rhs_ty, rhs)
1933         }
1934     }
1935 }
1936
1937 fn trans_overloaded_op<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
1938                                    expr: &ast::Expr,
1939                                    method_call: MethodCall,
1940                                    lhs: Datum<'tcx, Expr>,
1941                                    rhs: Option<(Datum<'tcx, Expr>, ast::NodeId)>,
1942                                    dest: Option<Dest>,
1943                                    autoref: bool)
1944                                    -> Result<'blk, 'tcx> {
1945     callee::trans_call_inner(bcx,
1946                              expr.debug_loc(),
1947                              |bcx, arg_cleanup_scope| {
1948                                 meth::trans_method_callee(bcx,
1949                                                           method_call,
1950                                                           None,
1951                                                           arg_cleanup_scope)
1952                              },
1953                              callee::ArgOverloadedOp(lhs, rhs, autoref),
1954                              dest)
1955 }
1956
1957 fn trans_overloaded_call<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
1958                                          expr: &ast::Expr,
1959                                          callee: &'a ast::Expr,
1960                                          args: &'a [P<ast::Expr>],
1961                                          dest: Option<Dest>)
1962                                          -> Block<'blk, 'tcx> {
1963     debug!("trans_overloaded_call {}", expr.id);
1964     let method_call = MethodCall::expr(expr.id);
1965     let mut all_args = vec!(callee);
1966     all_args.extend(args.iter().map(|e| &**e));
1967     unpack_result!(bcx,
1968                    callee::trans_call_inner(bcx,
1969                                             expr.debug_loc(),
1970                                             |bcx, arg_cleanup_scope| {
1971                                                 meth::trans_method_callee(
1972                                                     bcx,
1973                                                     method_call,
1974                                                     None,
1975                                                     arg_cleanup_scope)
1976                                             },
1977                                             callee::ArgOverloadedCall(all_args),
1978                                             dest));
1979     bcx
1980 }
1981
1982 pub fn cast_is_noop<'tcx>(tcx: &ty::ctxt<'tcx>,
1983                           expr: &ast::Expr,
1984                           t_in: Ty<'tcx>,
1985                           t_out: Ty<'tcx>)
1986                           -> bool {
1987     if let Some(&CastKind::CoercionCast) = tcx.cast_kinds.borrow().get(&expr.id) {
1988         return true;
1989     }
1990
1991     match (t_in.builtin_deref(true), t_out.builtin_deref(true)) {
1992         (Some(ty::TypeAndMut{ ty: t_in, .. }), Some(ty::TypeAndMut{ ty: t_out, .. })) => {
1993             t_in == t_out
1994         }
1995         _ => {
1996             // This condition isn't redundant with the check for CoercionCast:
1997             // different types can be substituted into the same type, and
1998             // == equality can be overconservative if there are regions.
1999             t_in == t_out
2000         }
2001     }
2002 }
2003
2004 fn trans_imm_cast<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
2005                               expr: &ast::Expr,
2006                               id: ast::NodeId)
2007                               -> DatumBlock<'blk, 'tcx, Expr>
2008 {
2009     use middle::cast::CastTy::*;
2010     use middle::cast::IntTy::*;
2011
2012     fn int_cast(bcx: Block,
2013                 lldsttype: Type,
2014                 llsrctype: Type,
2015                 llsrc: ValueRef,
2016                 signed: bool)
2017                 -> ValueRef
2018     {
2019         let _icx = push_ctxt("int_cast");
2020         let srcsz = llsrctype.int_width();
2021         let dstsz = lldsttype.int_width();
2022         return if dstsz == srcsz {
2023             BitCast(bcx, llsrc, lldsttype)
2024         } else if srcsz > dstsz {
2025             TruncOrBitCast(bcx, llsrc, lldsttype)
2026         } else if signed {
2027             SExtOrBitCast(bcx, llsrc, lldsttype)
2028         } else {
2029             ZExtOrBitCast(bcx, llsrc, lldsttype)
2030         }
2031     }
2032
2033     fn float_cast(bcx: Block,
2034                   lldsttype: Type,
2035                   llsrctype: Type,
2036                   llsrc: ValueRef)
2037                   -> ValueRef
2038     {
2039         let _icx = push_ctxt("float_cast");
2040         let srcsz = llsrctype.float_width();
2041         let dstsz = lldsttype.float_width();
2042         return if dstsz > srcsz {
2043             FPExt(bcx, llsrc, lldsttype)
2044         } else if srcsz > dstsz {
2045             FPTrunc(bcx, llsrc, lldsttype)
2046         } else { llsrc };
2047     }
2048
2049     let _icx = push_ctxt("trans_cast");
2050     let mut bcx = bcx;
2051     let ccx = bcx.ccx();
2052
2053     let t_in = expr_ty_adjusted(bcx, expr);
2054     let t_out = node_id_type(bcx, id);
2055
2056     debug!("trans_cast({:?} as {:?})", t_in, t_out);
2057     let mut ll_t_in = type_of::arg_type_of(ccx, t_in);
2058     let ll_t_out = type_of::arg_type_of(ccx, t_out);
2059     // Convert the value to be cast into a ValueRef, either by-ref or
2060     // by-value as appropriate given its type:
2061     let mut datum = unpack_datum!(bcx, trans(bcx, expr));
2062
2063     let datum_ty = monomorphize_type(bcx, datum.ty);
2064
2065     if cast_is_noop(bcx.tcx(), expr, datum_ty, t_out) {
2066         datum.ty = t_out;
2067         return DatumBlock::new(bcx, datum);
2068     }
2069
2070     if type_is_fat_ptr(bcx.tcx(), t_in) {
2071         assert!(datum.kind.is_by_ref());
2072         if type_is_fat_ptr(bcx.tcx(), t_out) {
2073             return DatumBlock::new(bcx, Datum::new(
2074                 PointerCast(bcx, datum.val, ll_t_out.ptr_to()),
2075                 t_out,
2076                 Rvalue::new(ByRef)
2077             )).to_expr_datumblock();
2078         } else {
2079             // Return the address
2080             return immediate_rvalue_bcx(bcx,
2081                                         PointerCast(bcx,
2082                                                     Load(bcx, get_dataptr(bcx, datum.val)),
2083                                                     ll_t_out),
2084                                         t_out).to_expr_datumblock();
2085         }
2086     }
2087
2088     let r_t_in = CastTy::from_ty(t_in).expect("bad input type for cast");
2089     let r_t_out = CastTy::from_ty(t_out).expect("bad output type for cast");
2090
2091     let (llexpr, signed) = if let Int(CEnum) = r_t_in {
2092         let repr = adt::represent_type(ccx, t_in);
2093         let datum = unpack_datum!(
2094             bcx, datum.to_lvalue_datum(bcx, "trans_imm_cast", expr.id));
2095         let llexpr_ptr = datum.to_llref();
2096         let discr = adt::trans_get_discr(bcx, &*repr, llexpr_ptr, Some(Type::i64(ccx)));
2097         ll_t_in = val_ty(discr);
2098         (discr, adt::is_discr_signed(&*repr))
2099     } else {
2100         (datum.to_llscalarish(bcx), t_in.is_signed())
2101     };
2102
2103     let newval = match (r_t_in, r_t_out) {
2104         (Ptr(_), Ptr(_)) | (FnPtr, Ptr(_)) | (RPtr(_), Ptr(_)) => {
2105             PointerCast(bcx, llexpr, ll_t_out)
2106         }
2107         (Ptr(_), Int(_)) | (FnPtr, Int(_)) => PtrToInt(bcx, llexpr, ll_t_out),
2108         (Int(_), Ptr(_)) => IntToPtr(bcx, llexpr, ll_t_out),
2109
2110         (Int(_), Int(_)) => int_cast(bcx, ll_t_out, ll_t_in, llexpr, signed),
2111         (Float, Float) => float_cast(bcx, ll_t_out, ll_t_in, llexpr),
2112         (Int(_), Float) if signed => SIToFP(bcx, llexpr, ll_t_out),
2113         (Int(_), Float) => UIToFP(bcx, llexpr, ll_t_out),
2114         (Float, Int(I)) => FPToSI(bcx, llexpr, ll_t_out),
2115         (Float, Int(_)) => FPToUI(bcx, llexpr, ll_t_out),
2116
2117         _ => ccx.sess().span_bug(expr.span,
2118                                   &format!("translating unsupported cast: \
2119                                             {:?} -> {:?}",
2120                                            t_in,
2121                                            t_out)
2122                                  )
2123     };
2124     return immediate_rvalue_bcx(bcx, newval, t_out).to_expr_datumblock();
2125 }
2126
2127 fn trans_assign_op<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
2128                                expr: &ast::Expr,
2129                                op: ast::BinOp,
2130                                dst: &ast::Expr,
2131                                src: &ast::Expr)
2132                                -> Block<'blk, 'tcx> {
2133     let _icx = push_ctxt("trans_assign_op");
2134     let mut bcx = bcx;
2135
2136     debug!("trans_assign_op(expr={:?})", expr);
2137
2138     // User-defined operator methods cannot be used with `+=` etc right now
2139     assert!(!bcx.tcx().is_method_call(expr.id));
2140
2141     // Evaluate LHS (destination), which should be an lvalue
2142     let dst_datum = unpack_datum!(bcx, trans_to_lvalue(bcx, dst, "assign_op"));
2143     assert!(!bcx.fcx.type_needs_drop(dst_datum.ty));
2144     let dst_ty = dst_datum.ty;
2145     let dst = load_ty(bcx, dst_datum.val, dst_datum.ty);
2146
2147     // Evaluate RHS
2148     let rhs_datum = unpack_datum!(bcx, trans(bcx, &*src));
2149     let rhs_ty = rhs_datum.ty;
2150     let rhs = rhs_datum.to_llscalarish(bcx);
2151
2152     // Perform computation and store the result
2153     let result_datum = unpack_datum!(
2154         bcx, trans_eager_binop(bcx, expr, dst_datum.ty, op,
2155                                dst_ty, dst, rhs_ty, rhs));
2156     return result_datum.store_to(bcx, dst_datum.val);
2157 }
2158
2159 fn auto_ref<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
2160                         datum: Datum<'tcx, Expr>,
2161                         expr: &ast::Expr)
2162                         -> DatumBlock<'blk, 'tcx, Expr> {
2163     let mut bcx = bcx;
2164
2165     // Ensure cleanup of `datum` if not already scheduled and obtain
2166     // a "by ref" pointer.
2167     let lv_datum = unpack_datum!(bcx, datum.to_lvalue_datum(bcx, "autoref", expr.id));
2168
2169     // Compute final type. Note that we are loose with the region and
2170     // mutability, since those things don't matter in trans.
2171     let referent_ty = lv_datum.ty;
2172     let ptr_ty = bcx.tcx().mk_imm_ref(bcx.tcx().mk_region(ty::ReStatic), referent_ty);
2173
2174     // Get the pointer.
2175     let llref = lv_datum.to_llref();
2176
2177     // Construct the resulting datum, using what was the "by ref"
2178     // ValueRef of type `referent_ty` to be the "by value" ValueRef
2179     // of type `&referent_ty`.
2180     // Pointers to DST types are non-immediate, and therefore still use ByRef.
2181     let kind  = if type_is_sized(bcx.tcx(), referent_ty) { ByValue } else { ByRef };
2182     DatumBlock::new(bcx, Datum::new(llref, ptr_ty, RvalueExpr(Rvalue::new(kind))))
2183 }
2184
2185 fn deref_multiple<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
2186                               expr: &ast::Expr,
2187                               datum: Datum<'tcx, Expr>,
2188                               times: usize)
2189                               -> DatumBlock<'blk, 'tcx, Expr> {
2190     let mut bcx = bcx;
2191     let mut datum = datum;
2192     for i in 0..times {
2193         let method_call = MethodCall::autoderef(expr.id, i as u32);
2194         datum = unpack_datum!(bcx, deref_once(bcx, expr, datum, method_call));
2195     }
2196     DatumBlock { bcx: bcx, datum: datum }
2197 }
2198
2199 fn deref_once<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
2200                           expr: &ast::Expr,
2201                           datum: Datum<'tcx, Expr>,
2202                           method_call: MethodCall)
2203                           -> DatumBlock<'blk, 'tcx, Expr> {
2204     let ccx = bcx.ccx();
2205
2206     debug!("deref_once(expr={:?}, datum={}, method_call={:?})",
2207            expr,
2208            datum.to_string(ccx),
2209            method_call);
2210
2211     let mut bcx = bcx;
2212
2213     // Check for overloaded deref.
2214     let method_ty = ccx.tcx()
2215                        .tables
2216                        .borrow()
2217                        .method_map
2218                        .get(&method_call).map(|method| method.ty);
2219
2220     let datum = match method_ty {
2221         Some(method_ty) => {
2222             let method_ty = monomorphize_type(bcx, method_ty);
2223
2224             // Overloaded. Evaluate `trans_overloaded_op`, which will
2225             // invoke the user's deref() method, which basically
2226             // converts from the `Smaht<T>` pointer that we have into
2227             // a `&T` pointer.  We can then proceed down the normal
2228             // path (below) to dereference that `&T`.
2229             let datum = if method_call.autoderef == 0 {
2230                 datum
2231             } else {
2232                 // Always perform an AutoPtr when applying an overloaded auto-deref
2233                 unpack_datum!(bcx, auto_ref(bcx, datum, expr))
2234             };
2235
2236             let ref_ty = // invoked methods have their LB regions instantiated
2237                 ccx.tcx().no_late_bound_regions(&method_ty.fn_ret()).unwrap().unwrap();
2238             let scratch = rvalue_scratch_datum(bcx, ref_ty, "overloaded_deref");
2239
2240             unpack_result!(bcx, trans_overloaded_op(bcx, expr, method_call,
2241                                                     datum, None, Some(SaveIn(scratch.val)),
2242                                                     false));
2243             scratch.to_expr_datum()
2244         }
2245         None => {
2246             // Not overloaded. We already have a pointer we know how to deref.
2247             datum
2248         }
2249     };
2250
2251     let r = match datum.ty.sty {
2252         ty::TyBox(content_ty) => {
2253             // Make sure we have an lvalue datum here to get the
2254             // proper cleanups scheduled
2255             let datum = unpack_datum!(
2256                 bcx, datum.to_lvalue_datum(bcx, "deref", expr.id));
2257
2258             if type_is_sized(bcx.tcx(), content_ty) {
2259                 let ptr = load_ty(bcx, datum.val, datum.ty);
2260                 DatumBlock::new(bcx, Datum::new(ptr, content_ty, LvalueExpr(datum.kind)))
2261             } else {
2262                 // A fat pointer and a DST lvalue have the same representation
2263                 // just different types. Since there is no temporary for `*e`
2264                 // here (because it is unsized), we cannot emulate the sized
2265                 // object code path for running drop glue and free. Instead,
2266                 // we schedule cleanup for `e`, turning it into an lvalue.
2267
2268                 let lval = Lvalue::new("expr::deref_once ty_uniq");
2269                 let datum = Datum::new(datum.val, content_ty, LvalueExpr(lval));
2270                 DatumBlock::new(bcx, datum)
2271             }
2272         }
2273
2274         ty::TyRawPtr(ty::TypeAndMut { ty: content_ty, .. }) |
2275         ty::TyRef(_, ty::TypeAndMut { ty: content_ty, .. }) => {
2276             let lval = Lvalue::new("expr::deref_once ptr");
2277             if type_is_sized(bcx.tcx(), content_ty) {
2278                 let ptr = datum.to_llscalarish(bcx);
2279
2280                 // Always generate an lvalue datum, even if datum.mode is
2281                 // an rvalue.  This is because datum.mode is only an
2282                 // rvalue for non-owning pointers like &T or *T, in which
2283                 // case cleanup *is* scheduled elsewhere, by the true
2284                 // owner (or, in the case of *T, by the user).
2285                 DatumBlock::new(bcx, Datum::new(ptr, content_ty, LvalueExpr(lval)))
2286             } else {
2287                 // A fat pointer and a DST lvalue have the same representation
2288                 // just different types.
2289                 DatumBlock::new(bcx, Datum::new(datum.val, content_ty, LvalueExpr(lval)))
2290             }
2291         }
2292
2293         _ => {
2294             bcx.tcx().sess.span_bug(
2295                 expr.span,
2296                 &format!("deref invoked on expr of invalid type {:?}",
2297                         datum.ty));
2298         }
2299     };
2300
2301     debug!("deref_once(expr={}, method_call={:?}, result={})",
2302            expr.id, method_call, r.datum.to_string(ccx));
2303
2304     return r;
2305 }
2306
2307 #[derive(Debug)]
2308 enum OverflowOp {
2309     Add,
2310     Sub,
2311     Mul,
2312     Shl,
2313     Shr,
2314 }
2315
2316 impl OverflowOp {
2317     fn codegen_strategy(&self) -> OverflowCodegen {
2318         use self::OverflowCodegen::{ViaIntrinsic, ViaInputCheck};
2319         match *self {
2320             OverflowOp::Add => ViaIntrinsic(OverflowOpViaIntrinsic::Add),
2321             OverflowOp::Sub => ViaIntrinsic(OverflowOpViaIntrinsic::Sub),
2322             OverflowOp::Mul => ViaIntrinsic(OverflowOpViaIntrinsic::Mul),
2323
2324             OverflowOp::Shl => ViaInputCheck(OverflowOpViaInputCheck::Shl),
2325             OverflowOp::Shr => ViaInputCheck(OverflowOpViaInputCheck::Shr),
2326         }
2327     }
2328 }
2329
2330 enum OverflowCodegen {
2331     ViaIntrinsic(OverflowOpViaIntrinsic),
2332     ViaInputCheck(OverflowOpViaInputCheck),
2333 }
2334
2335 enum OverflowOpViaInputCheck { Shl, Shr, }
2336
2337 #[derive(Debug)]
2338 enum OverflowOpViaIntrinsic { Add, Sub, Mul, }
2339
2340 impl OverflowOpViaIntrinsic {
2341     fn to_intrinsic<'blk, 'tcx>(&self, bcx: Block<'blk, 'tcx>, lhs_ty: Ty) -> ValueRef {
2342         let name = self.to_intrinsic_name(bcx.tcx(), lhs_ty);
2343         bcx.ccx().get_intrinsic(&name)
2344     }
2345     fn to_intrinsic_name(&self, tcx: &ty::ctxt, ty: Ty) -> &'static str {
2346         use syntax::ast::IntTy::*;
2347         use syntax::ast::UintTy::*;
2348         use middle::ty::{TyInt, TyUint};
2349
2350         let new_sty = match ty.sty {
2351             TyInt(TyIs) => match &tcx.sess.target.target.target_pointer_width[..] {
2352                 "32" => TyInt(TyI32),
2353                 "64" => TyInt(TyI64),
2354                 _ => panic!("unsupported target word size")
2355             },
2356             TyUint(TyUs) => match &tcx.sess.target.target.target_pointer_width[..] {
2357                 "32" => TyUint(TyU32),
2358                 "64" => TyUint(TyU64),
2359                 _ => panic!("unsupported target word size")
2360             },
2361             ref t @ TyUint(_) | ref t @ TyInt(_) => t.clone(),
2362             _ => panic!("tried to get overflow intrinsic for {:?} applied to non-int type",
2363                         *self)
2364         };
2365
2366         match *self {
2367             OverflowOpViaIntrinsic::Add => match new_sty {
2368                 TyInt(TyI8) => "llvm.sadd.with.overflow.i8",
2369                 TyInt(TyI16) => "llvm.sadd.with.overflow.i16",
2370                 TyInt(TyI32) => "llvm.sadd.with.overflow.i32",
2371                 TyInt(TyI64) => "llvm.sadd.with.overflow.i64",
2372
2373                 TyUint(TyU8) => "llvm.uadd.with.overflow.i8",
2374                 TyUint(TyU16) => "llvm.uadd.with.overflow.i16",
2375                 TyUint(TyU32) => "llvm.uadd.with.overflow.i32",
2376                 TyUint(TyU64) => "llvm.uadd.with.overflow.i64",
2377
2378                 _ => unreachable!(),
2379             },
2380             OverflowOpViaIntrinsic::Sub => match new_sty {
2381                 TyInt(TyI8) => "llvm.ssub.with.overflow.i8",
2382                 TyInt(TyI16) => "llvm.ssub.with.overflow.i16",
2383                 TyInt(TyI32) => "llvm.ssub.with.overflow.i32",
2384                 TyInt(TyI64) => "llvm.ssub.with.overflow.i64",
2385
2386                 TyUint(TyU8) => "llvm.usub.with.overflow.i8",
2387                 TyUint(TyU16) => "llvm.usub.with.overflow.i16",
2388                 TyUint(TyU32) => "llvm.usub.with.overflow.i32",
2389                 TyUint(TyU64) => "llvm.usub.with.overflow.i64",
2390
2391                 _ => unreachable!(),
2392             },
2393             OverflowOpViaIntrinsic::Mul => match new_sty {
2394                 TyInt(TyI8) => "llvm.smul.with.overflow.i8",
2395                 TyInt(TyI16) => "llvm.smul.with.overflow.i16",
2396                 TyInt(TyI32) => "llvm.smul.with.overflow.i32",
2397                 TyInt(TyI64) => "llvm.smul.with.overflow.i64",
2398
2399                 TyUint(TyU8) => "llvm.umul.with.overflow.i8",
2400                 TyUint(TyU16) => "llvm.umul.with.overflow.i16",
2401                 TyUint(TyU32) => "llvm.umul.with.overflow.i32",
2402                 TyUint(TyU64) => "llvm.umul.with.overflow.i64",
2403
2404                 _ => unreachable!(),
2405             },
2406         }
2407     }
2408
2409     fn build_intrinsic_call<'blk, 'tcx>(&self, bcx: Block<'blk, 'tcx>,
2410                                         info: NodeIdAndSpan,
2411                                         lhs_t: Ty<'tcx>, lhs: ValueRef,
2412                                         rhs: ValueRef,
2413                                         binop_debug_loc: DebugLoc)
2414                                         -> (Block<'blk, 'tcx>, ValueRef) {
2415         let llfn = self.to_intrinsic(bcx, lhs_t);
2416
2417         let val = Call(bcx, llfn, &[lhs, rhs], None, binop_debug_loc);
2418         let result = ExtractValue(bcx, val, 0); // iN operation result
2419         let overflow = ExtractValue(bcx, val, 1); // i1 "did it overflow?"
2420
2421         let cond = ICmp(bcx, llvm::IntEQ, overflow, C_integral(Type::i1(bcx.ccx()), 1, false),
2422                         binop_debug_loc);
2423
2424         let expect = bcx.ccx().get_intrinsic(&"llvm.expect.i1");
2425         Call(bcx, expect, &[cond, C_integral(Type::i1(bcx.ccx()), 0, false)],
2426              None, binop_debug_loc);
2427
2428         let bcx =
2429             base::with_cond(bcx, cond, |bcx|
2430                 controlflow::trans_fail(bcx, info,
2431                     InternedString::new("arithmetic operation overflowed")));
2432
2433         (bcx, result)
2434     }
2435 }
2436
2437 impl OverflowOpViaInputCheck {
2438     fn build_with_input_check<'blk, 'tcx>(&self,
2439                                           bcx: Block<'blk, 'tcx>,
2440                                           info: NodeIdAndSpan,
2441                                           lhs_t: Ty<'tcx>,
2442                                           lhs: ValueRef,
2443                                           rhs: ValueRef,
2444                                           binop_debug_loc: DebugLoc)
2445                                           -> (Block<'blk, 'tcx>, ValueRef)
2446     {
2447         let lhs_llty = val_ty(lhs);
2448         let rhs_llty = val_ty(rhs);
2449
2450         // Panic if any bits are set outside of bits that we always
2451         // mask in.
2452         //
2453         // Note that the mask's value is derived from the LHS type
2454         // (since that is where the 32/64 distinction is relevant) but
2455         // the mask's type must match the RHS type (since they will
2456         // both be fed into a and-binop)
2457         let invert_mask = shift_mask_val(bcx, lhs_llty, rhs_llty, true);
2458
2459         let outer_bits = And(bcx, rhs, invert_mask, binop_debug_loc);
2460         let cond = build_nonzero_check(bcx, outer_bits, binop_debug_loc);
2461         let result = match *self {
2462             OverflowOpViaInputCheck::Shl =>
2463                 build_unchecked_lshift(bcx, lhs, rhs, binop_debug_loc),
2464             OverflowOpViaInputCheck::Shr =>
2465                 build_unchecked_rshift(bcx, lhs_t, lhs, rhs, binop_debug_loc),
2466         };
2467         let bcx =
2468             base::with_cond(bcx, cond, |bcx|
2469                 controlflow::trans_fail(bcx, info,
2470                     InternedString::new("shift operation overflowed")));
2471
2472         (bcx, result)
2473     }
2474 }
2475
2476 fn shift_mask_val<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
2477                               llty: Type,
2478                               mask_llty: Type,
2479                               invert: bool) -> ValueRef {
2480     let kind = llty.kind();
2481     match kind {
2482         TypeKind::Integer => {
2483             // i8/u8 can shift by at most 7, i16/u16 by at most 15, etc.
2484             let val = llty.int_width() - 1;
2485             if invert {
2486                 C_integral(mask_llty, !val, true)
2487             } else {
2488                 C_integral(mask_llty, val, false)
2489             }
2490         },
2491         TypeKind::Vector => {
2492             let mask = shift_mask_val(bcx, llty.element_type(), mask_llty.element_type(), invert);
2493             VectorSplat(bcx, mask_llty.vector_length(), mask)
2494         },
2495         _ => panic!("shift_mask_val: expected Integer or Vector, found {:?}", kind),
2496     }
2497 }
2498
2499 // Check if an integer or vector contains a nonzero element.
2500 fn build_nonzero_check<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
2501                                    value: ValueRef,
2502                                    binop_debug_loc: DebugLoc) -> ValueRef {
2503     let llty = val_ty(value);
2504     let kind = llty.kind();
2505     match kind {
2506         TypeKind::Integer => ICmp(bcx, llvm::IntNE, value, C_null(llty), binop_debug_loc),
2507         TypeKind::Vector => {
2508             // Check if any elements of the vector are nonzero by treating
2509             // it as a wide integer and checking if the integer is nonzero.
2510             let width = llty.vector_length() as u64 * llty.element_type().int_width();
2511             let int_value = BitCast(bcx, value, Type::ix(bcx.ccx(), width));
2512             build_nonzero_check(bcx, int_value, binop_debug_loc)
2513         },
2514         _ => panic!("build_nonzero_check: expected Integer or Vector, found {:?}", kind),
2515     }
2516 }
2517
2518 // To avoid UB from LLVM, these two functions mask RHS with an
2519 // appropriate mask unconditionally (i.e. the fallback behavior for
2520 // all shifts). For 32- and 64-bit types, this matches the semantics
2521 // of Java. (See related discussion on #1877 and #10183.)
2522
2523 fn build_unchecked_lshift<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
2524                                       lhs: ValueRef,
2525                                       rhs: ValueRef,
2526                                       binop_debug_loc: DebugLoc) -> ValueRef {
2527     let rhs = base::cast_shift_expr_rhs(bcx, ast::BinOp_::BiShl, lhs, rhs);
2528     // #1877, #10183: Ensure that input is always valid
2529     let rhs = shift_mask_rhs(bcx, rhs, binop_debug_loc);
2530     Shl(bcx, lhs, rhs, binop_debug_loc)
2531 }
2532
2533 fn build_unchecked_rshift<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
2534                                       lhs_t: Ty<'tcx>,
2535                                       lhs: ValueRef,
2536                                       rhs: ValueRef,
2537                                       binop_debug_loc: DebugLoc) -> ValueRef {
2538     let rhs = base::cast_shift_expr_rhs(bcx, ast::BinOp_::BiShr, lhs, rhs);
2539     // #1877, #10183: Ensure that input is always valid
2540     let rhs = shift_mask_rhs(bcx, rhs, binop_debug_loc);
2541     let tcx = bcx.tcx();
2542     let is_simd = lhs_t.is_simd();
2543     let intype = if is_simd {
2544         lhs_t.simd_type(tcx)
2545     } else {
2546         lhs_t
2547     };
2548     let is_signed = intype.is_signed();
2549     if is_signed {
2550         AShr(bcx, lhs, rhs, binop_debug_loc)
2551     } else {
2552         LShr(bcx, lhs, rhs, binop_debug_loc)
2553     }
2554 }
2555
2556 fn shift_mask_rhs<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
2557                               rhs: ValueRef,
2558                               debug_loc: DebugLoc) -> ValueRef {
2559     let rhs_llty = val_ty(rhs);
2560     And(bcx, rhs, shift_mask_val(bcx, rhs_llty, rhs_llty, false), debug_loc)
2561 }
2562
2563 fn with_overflow_check<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, oop: OverflowOp, info: NodeIdAndSpan,
2564                                    lhs_t: Ty<'tcx>, lhs: ValueRef,
2565                                    rhs: ValueRef,
2566                                    binop_debug_loc: DebugLoc)
2567                                    -> (Block<'blk, 'tcx>, ValueRef) {
2568     if bcx.unreachable.get() { return (bcx, _Undef(lhs)); }
2569     if bcx.ccx().check_overflow() {
2570
2571         match oop.codegen_strategy() {
2572             OverflowCodegen::ViaIntrinsic(oop) =>
2573                 oop.build_intrinsic_call(bcx, info, lhs_t, lhs, rhs, binop_debug_loc),
2574             OverflowCodegen::ViaInputCheck(oop) =>
2575                 oop.build_with_input_check(bcx, info, lhs_t, lhs, rhs, binop_debug_loc),
2576         }
2577     } else {
2578         let res = match oop {
2579             OverflowOp::Add => Add(bcx, lhs, rhs, binop_debug_loc),
2580             OverflowOp::Sub => Sub(bcx, lhs, rhs, binop_debug_loc),
2581             OverflowOp::Mul => Mul(bcx, lhs, rhs, binop_debug_loc),
2582
2583             OverflowOp::Shl =>
2584                 build_unchecked_lshift(bcx, lhs, rhs, binop_debug_loc),
2585             OverflowOp::Shr =>
2586                 build_unchecked_rshift(bcx, lhs_t, lhs, rhs, binop_debug_loc),
2587         };
2588         (bcx, res)
2589     }
2590 }
2591
2592 /// We categorize expressions into three kinds.  The distinction between
2593 /// lvalue/rvalue is fundamental to the language.  The distinction between the
2594 /// two kinds of rvalues is an artifact of trans which reflects how we will
2595 /// generate code for that kind of expression.  See trans/expr.rs for more
2596 /// information.
2597 #[derive(Copy, Clone)]
2598 enum ExprKind {
2599     Lvalue,
2600     RvalueDps,
2601     RvalueDatum,
2602     RvalueStmt
2603 }
2604
2605 fn expr_kind(tcx: &ty::ctxt, expr: &ast::Expr) -> ExprKind {
2606     if tcx.is_method_call(expr.id) {
2607         // Overloaded operations are generally calls, and hence they are
2608         // generated via DPS, but there are a few exceptions:
2609         return match expr.node {
2610             // `a += b` has a unit result.
2611             ast::ExprAssignOp(..) => ExprKind::RvalueStmt,
2612
2613             // the deref method invoked for `*a` always yields an `&T`
2614             ast::ExprUnary(ast::UnDeref, _) => ExprKind::Lvalue,
2615
2616             // the index method invoked for `a[i]` always yields an `&T`
2617             ast::ExprIndex(..) => ExprKind::Lvalue,
2618
2619             // in the general case, result could be any type, use DPS
2620             _ => ExprKind::RvalueDps
2621         };
2622     }
2623
2624     match expr.node {
2625         ast::ExprPath(..) => {
2626             match tcx.resolve_expr(expr) {
2627                 def::DefStruct(_) | def::DefVariant(..) => {
2628                     if let ty::TyBareFn(..) = tcx.node_id_to_type(expr.id).sty {
2629                         // ctor function
2630                         ExprKind::RvalueDatum
2631                     } else {
2632                         ExprKind::RvalueDps
2633                     }
2634                 }
2635
2636                 // Special case: A unit like struct's constructor must be called without () at the
2637                 // end (like `UnitStruct`) which means this is an ExprPath to a DefFn. But in case
2638                 // of unit structs this is should not be interpreted as function pointer but as
2639                 // call to the constructor.
2640                 def::DefFn(_, true) => ExprKind::RvalueDps,
2641
2642                 // Fn pointers are just scalar values.
2643                 def::DefFn(..) | def::DefMethod(..) => ExprKind::RvalueDatum,
2644
2645                 // Note: there is actually a good case to be made that
2646                 // DefArg's, particularly those of immediate type, ought to
2647                 // considered rvalues.
2648                 def::DefStatic(..) |
2649                 def::DefUpvar(..) |
2650                 def::DefLocal(..) => ExprKind::Lvalue,
2651
2652                 def::DefConst(..) |
2653                 def::DefAssociatedConst(..) => ExprKind::RvalueDatum,
2654
2655                 def => {
2656                     tcx.sess.span_bug(
2657                         expr.span,
2658                         &format!("uncategorized def for expr {}: {:?}",
2659                                 expr.id,
2660                                 def));
2661                 }
2662             }
2663         }
2664
2665         ast::ExprUnary(ast::UnDeref, _) |
2666         ast::ExprField(..) |
2667         ast::ExprTupField(..) |
2668         ast::ExprIndex(..) => {
2669             ExprKind::Lvalue
2670         }
2671
2672         ast::ExprCall(..) |
2673         ast::ExprMethodCall(..) |
2674         ast::ExprStruct(..) |
2675         ast::ExprRange(..) |
2676         ast::ExprTup(..) |
2677         ast::ExprIf(..) |
2678         ast::ExprMatch(..) |
2679         ast::ExprClosure(..) |
2680         ast::ExprBlock(..) |
2681         ast::ExprRepeat(..) |
2682         ast::ExprVec(..) => {
2683             ExprKind::RvalueDps
2684         }
2685
2686         ast::ExprIfLet(..) => {
2687             tcx.sess.span_bug(expr.span, "non-desugared ExprIfLet");
2688         }
2689         ast::ExprWhileLet(..) => {
2690             tcx.sess.span_bug(expr.span, "non-desugared ExprWhileLet");
2691         }
2692
2693         ast::ExprForLoop(..) => {
2694             tcx.sess.span_bug(expr.span, "non-desugared ExprForLoop");
2695         }
2696
2697         ast::ExprLit(ref lit) if ast_util::lit_is_str(&**lit) => {
2698             ExprKind::RvalueDps
2699         }
2700
2701         ast::ExprBreak(..) |
2702         ast::ExprAgain(..) |
2703         ast::ExprRet(..) |
2704         ast::ExprWhile(..) |
2705         ast::ExprLoop(..) |
2706         ast::ExprAssign(..) |
2707         ast::ExprInlineAsm(..) |
2708         ast::ExprAssignOp(..) => {
2709             ExprKind::RvalueStmt
2710         }
2711
2712         ast::ExprLit(_) | // Note: LitStr is carved out above
2713         ast::ExprUnary(..) |
2714         ast::ExprBox(None, _) |
2715         ast::ExprAddrOf(..) |
2716         ast::ExprBinary(..) |
2717         ast::ExprCast(..) => {
2718             ExprKind::RvalueDatum
2719         }
2720
2721         ast::ExprBox(Some(ref place), _) => {
2722             // Special case `Box<T>` for now:
2723             let def_id = match tcx.def_map.borrow().get(&place.id) {
2724                 Some(def) => def.def_id(),
2725                 None => panic!("no def for place"),
2726             };
2727             if tcx.lang_items.exchange_heap() == Some(def_id) {
2728                 ExprKind::RvalueDatum
2729             } else {
2730                 ExprKind::RvalueDps
2731             }
2732         }
2733
2734         ast::ExprParen(ref e) => expr_kind(tcx, &**e),
2735
2736         ast::ExprMac(..) => {
2737             tcx.sess.span_bug(
2738                 expr.span,
2739                 "macro expression remains after expansion");
2740         }
2741     }
2742 }