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.
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.
11 //! # Translation of Expressions
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.
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:
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`.
32 //! Public entry points:
34 //! - `trans_into(bcx, expr, dest) -> bcx`: evaluates an expression,
35 //! storing the result into `dest`. This is the preferred form, if you
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
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.
47 //! - `trans_local_var -> Datum`: looks up a local variable or upvar.
49 #![allow(non_camel_case_types)]
51 pub use self::Dest::*;
52 use self::lazy_binop_ty::*;
55 use llvm::{self, ValueRef, TypeKind};
56 use middle::check_const;
58 use middle::lang_items::CoerceUnsizedTraitLangItem;
59 use middle::subst::{Substs, VecPerParamSpace};
61 use trans::{_match, adt, asm, base, callee, closure, consts, controlflow};
64 use trans::cleanup::{self, CleanupMethods, DropHintMethods};
67 use trans::debuginfo::{self, DebugLoc, ToDebugLoc};
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;
82 use syntax::{ast, ast_util, codemap};
83 use syntax::parse::token::InternedString;
85 use syntax::parse::token;
90 // These are passed around by the code generating functions to track the
91 // destination of a computation's value.
93 #[derive(Copy, Clone, PartialEq)]
100 pub fn to_string(&self, ccx: &CrateContext) -> String {
102 SaveIn(v) => format!("SaveIn({})", ccx.tn().val_to_string(v)),
103 Ignore => "Ignore".to_string()
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>,
113 -> Block<'blk, 'tcx> {
116 debuginfo::set_source_location(bcx.fcx, expr.id, expr.span);
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);
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
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));
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.
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.
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(),
160 // Lock emitted debug locations to the location of
161 // the constant reference expression.
162 debuginfo::with_source_location_override(bcx.fcx,
165 bcx = trans_into(bcx, const_expr, dest)
167 let scopes = mem::replace(&mut *bcx.fcx.scopes.borrow_mut(),
169 assert!(scopes.is_empty());
180 debug!("trans_into() expr={:?}", expr);
182 let cleanup_debug_loc = debuginfo::get_cleanup_debug_loc_for_ast_node(bcx.ccx(),
186 bcx.fcx.push_ast_cleanup_scope(cleanup_debug_loc);
188 let kind = expr_kind(bcx.tcx(), expr);
190 ExprKind::Lvalue | ExprKind::RvalueDatum => {
191 trans_unadjusted(bcx, expr).store_to_dest(dest, expr.id)
193 ExprKind::RvalueDps => {
194 trans_rvalue_dps_unadjusted(bcx, expr, dest)
196 ExprKind::RvalueStmt => {
197 trans_rvalue_stmt_unadjusted(bcx, expr)
201 bcx.fcx.pop_and_trans_ast_cleanup_scope(bcx, expr.id)
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
207 pub fn trans<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
209 -> DatumBlock<'blk, 'tcx, Expr> {
210 debug!("trans(expr={:?})", expr);
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
220 let global = consts::get_const_expr_as_global(bcx.ccx(), expr, qualif,
221 bcx.fcx.param_substs);
223 if qualif.intersects(check_const::ConstQualif::HAS_STATIC_BORROWS) {
224 // Is borrowed as 'static, must return lvalue.
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());
234 // Otherwise, keep around and perform adjustments, if needed.
235 let const_ty = if adjusted_global {
236 expr_ty_adjusted(bcx, expr)
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)
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))
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.
260 memcpy_ty(bcx, lldest, global, const_ty);
261 Datum::new(scratch, const_ty, Rvalue::new(ByRef))
267 let cleanup_debug_loc = debuginfo::get_cleanup_debug_loc_for_ast_node(bcx.ccx(),
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))
276 let datum = if adjusted_global {
277 datum // trans::consts already performed adjustments.
279 unpack_datum!(bcx, apply_adjustments(bcx, expr, datum))
281 bcx = fcx.pop_and_trans_ast_cleanup_scope(bcx, expr.id);
282 return DatumBlock::new(bcx, datum);
285 pub fn get_meta(bcx: Block, fat_ptr: ValueRef) -> ValueRef {
286 StructGEP(bcx, fat_ptr, abi::FAT_PTR_EXTRA)
289 pub fn get_dataptr(bcx: Block, fat_ptr: ValueRef) -> ValueRef {
290 StructGEP(bcx, fat_ptr, abi::FAT_PTR_ADDR)
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));
298 /// Retrieve the information we are losing (making dynamic) in an unsizing
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>,
307 old_info: Option<ValueRef>,
308 param_substs: &'tcx Substs<'tcx>)
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")
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(),
325 consts::ptrcast(meth::get_vtable(ccx, trait_ref, param_substs),
326 Type::vtable_ptr(ccx))
328 _ => ccx.sess().bug(&format!("unsized_info: invalid unsizing {:?} -> {:?}",
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>,
338 datum: Datum<'tcx, Expr>)
339 -> DatumBlock<'blk, 'tcx, Expr>
342 let mut datum = datum;
343 let adjustment = match bcx.tcx().tables.borrow().adjustments.get(&expr.id).cloned() {
345 return DatumBlock::new(bcx, datum);
349 debug!("unadjusted datum for expr {:?}: {} adjustment={:?}",
351 datum.to_string(bcx.ccx()),
354 AdjustReifyFnPointer => {
355 // FIXME(#19925) once fn item types are
356 // zero-sized, we'll need to do something here
358 AdjustUnsafeFnPointer => {
359 // purely a type-level thing
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).
367 // Don't skip a conversion from Box<T> to &T, etc.
369 if bcx.tcx().is_overloaded_autoderef(expr.id, 0) {
370 // Don't skip an overloaded deref.
382 if adj.autoderefs > skip_reborrows {
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));
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) {
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));
399 datum = unpack_datum!(bcx, auto_ref(bcx, datum, expr));
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"));
412 let target = bcx.monomorphize(&target);
413 let llty = type_of::type_of(bcx.ccx(), target);
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,
419 bcx = coerce_unsized(bcx, expr.span, source_datum, target_datum);
420 datum = Datum::new(scratch, target,
421 RvalueExpr(Rvalue::new(ByRef)));
425 debug!("after adjustments, datum={}", datum.to_string(bcx.ccx()));
426 DatumBlock::new(bcx, datum)
429 fn coerce_unsized<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
431 source: Datum<'tcx, Rvalue>,
432 target: Datum<'tcx, Rvalue>)
433 -> Block<'blk, 'tcx> {
435 debug!("coerce_unsized({} -> {})",
436 source.to_string(bcx.ccx()),
437 target.to_string(bcx.ccx()));
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);
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
456 (Load(bcx, get_dataptr(bcx, source.val)),
457 Some(Load(bcx, get_meta(bcx, source.val))))
459 let val = if source.kind.is_by_ref() {
460 load_ty(bcx, source.val, source.ty)
467 let info = unsized_info(bcx.ccx(), inner_source, inner_target,
468 old_info, bcx.fcx.param_substs);
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);
475 Store(bcx, base, get_dataptr(bcx, target.val));
476 Store(bcx, info, get_meta(bcx, target.val));
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);
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());
488 let trait_substs = Substs::erased(VecPerParamSpace::new(vec![target.ty],
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)
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)
502 bcx.sess().span_bug(span, &format!("invalid CoerceUnsized vtable: {:?}",
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: {:?})",
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: {:?})",
522 let coerce_index = match kind {
523 ty::CustomCoerceUnsized::Struct(i) => i
525 assert!(coerce_index < src_fields.len() && src_fields.len() == target_fields.len());
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);
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,
537 Datum::new(ll_target, target_ty,
538 Rvalue::new(ByRef)));
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);
546 _ => bcx.sess().bug(&format!("coerce_unsized: invalid coercion {:?} -> {:?}",
553 /// Translates an expression in "lvalue" mode -- meaning that it returns a reference to the memory
554 /// that the expr represents.
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
559 /// { tmp = x(); tmp.f }
560 pub fn trans_to_lvalue<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
563 -> DatumBlock<'blk, 'tcx, Lvalue> {
565 let datum = unpack_datum!(bcx, trans(bcx, expr));
566 return datum.to_lvalue_datum(bcx, name, expr.id);
569 /// A version of `trans` that ignores adjustments. You almost certainly do not want to call this
571 fn trans_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
573 -> DatumBlock<'blk, 'tcx, Expr> {
576 debug!("trans_unadjusted(expr={:?})", expr);
577 let _indenter = indenter();
579 debuginfo::set_source_location(bcx.fcx, expr.id, expr.span);
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)
587 DatumBlock {bcx: bcx, datum: datum}
590 ExprKind::RvalueStmt => {
591 bcx = trans_rvalue_stmt_unadjusted(bcx, expr);
592 nil(bcx, expr_ty(bcx, expr))
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);
601 let scratch = rvalue_scratch_datum(bcx, ty, "");
602 bcx = trans_rvalue_dps_unadjusted(
603 bcx, expr, SaveIn(scratch.val));
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
614 let scratch = unpack_datum!(
615 bcx, scratch.to_appropriate_datum(bcx));
617 DatumBlock::new(bcx, scratch.to_expr_datum())
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())
630 fn trans_datum_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
632 -> DatumBlock<'blk, 'tcx, Expr> {
635 let _icx = push_ctxt("trans_datum_unadjusted");
638 ast::ExprParen(ref e) => {
641 ast::ExprPath(..) => {
642 trans_def(bcx, expr, bcx.def(expr.id))
644 ast::ExprField(ref base, ident) => {
645 trans_rec_field(bcx, &**base, ident.node.name)
647 ast::ExprTupField(ref base, idx) => {
648 trans_rec_tup_field(bcx, &**base, idx.node)
650 ast::ExprIndex(ref base, ref idx) => {
651 trans_index(bcx, expr, &**base, &**idx, MethodCall::expr(expr.id))
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);
659 trans_uniq_expr(bcx, expr, box_ty, &**contents, contents_ty)
661 _ => bcx.sess().span_bug(expr.span,
662 "expected unique box")
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)
670 ast::ExprUnary(op, ref x) => {
671 trans_unary(bcx, expr, op, &**x)
673 ast::ExprAddrOf(_, ref x) => {
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(),
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)
689 trans_addr_of(bcx, expr, &**x)
693 ast::ExprCast(ref val, _) => {
694 // Datum output mode means this is a scalar cast:
695 trans_imm_cast(bcx, &**val, expr.id)
698 bcx.tcx().sess.span_bug(
700 &format!("trans_rvalue_datum_unadjusted reached \
701 fall-through case: {:?}",
707 fn trans_field<'blk, 'tcx, F>(bcx: Block<'blk, 'tcx>,
710 -> DatumBlock<'blk, 'tcx, Expr> where
711 F: FnOnce(&'blk ty::ctxt<'tcx>, &VariantInfo<'tcx>) -> usize,
714 let _icx = push_ctxt("trans_rec_field");
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);
721 let ix = get_idx(bcx.tcx(), &vinfo);
722 let d = base_datum.get_element(
725 |srcval| adt::trans_field_ptr(bcx, &*repr, srcval, vinfo.discr, ix));
727 if type_is_sized(bcx.tcx(), d.ty) {
728 DatumBlock { datum: d.to_expr_datum(), bcx: bcx }
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));
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)))
741 /// Translates `base.field`.
742 fn trans_rec_field<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
745 -> DatumBlock<'blk, 'tcx, Expr> {
746 trans_field(bcx, base, |_, vinfo| vinfo.field_index(field))
749 /// Translates `base.<idx>`.
750 fn trans_rec_tup_field<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
753 -> DatumBlock<'blk, 'tcx, Expr> {
754 trans_field(bcx, base, |_, _| idx)
757 fn trans_index<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
758 index_expr: &ast::Expr,
761 method_call: MethodCall)
762 -> DatumBlock<'blk, 'tcx, Expr> {
763 //! Translates `base[idx]`.
765 let _icx = push_ctxt("trans_index");
769 let index_expr_debug_loc = index_expr.debug_loc();
771 // Check for overloaded index.
772 let method_ty = ccx.tcx()
777 .map(|method| method.ty);
778 let elt_datum = match method_ty {
780 let method_ty = monomorphize_type(bcx, method_ty);
782 let base_datum = unpack_datum!(bcx, trans(bcx, base));
784 // Translate index expression.
785 let ix_datum = unpack_datum!(bcx, trans(bcx, idx));
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) {
791 bcx.tcx().sess.span_bug(index_expr.span,
792 "index method didn't return a \
793 dereferenceable type?!")
795 Some(elt_tm) => elt_tm.ty,
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");
804 trans_overloaded_op(bcx,
808 Some((ix_datum, idx.id)),
809 Some(SaveIn(scratch.val)),
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))
816 Datum::new(datum.val, elt_ty, LvalueExpr(lval))
820 let base_datum = unpack_datum!(bcx, trans_to_lvalue(bcx,
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(),
830 let int_size = machine::llbitsize_of_real(bcx.ccx(),
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())
844 let unit_ty = base_datum.ty.sequence_element_type(bcx.tcx());
846 let (base, len) = base_datum.get_vec_base_and_len(bcx);
848 debug!("trans_index: base {}", bcx.val_to_string(base));
849 debug!("trans_index: len {}", bcx.val_to_string(len));
851 let bounds_check = ICmp(bcx,
855 index_expr_debug_loc);
856 let expect = ccx.get_intrinsic(&("llvm.expect.i1"));
857 let expected = Call(bcx,
859 &[bounds_check, C_bool(ccx, false)],
861 index_expr_debug_loc);
862 bcx = with_cond(bcx, expected, |bcx| {
863 controlflow::trans_fail_bounds_check(bcx,
864 expr_info(index_expr),
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))
875 DatumBlock::new(bcx, elt_datum)
878 fn trans_def<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
879 ref_expr: &ast::Expr,
881 -> DatumBlock<'blk, 'tcx, Expr> {
882 //! Translates a reference to a path.
884 let _icx = push_ctxt("trans_def_lvalue");
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())
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
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
900 let const_ty = expr_ty(bcx, ref_expr);
902 // For external constants, we don't inline.
903 let val = if did.is_local() {
906 // The LLVM global has the type of its initializer,
907 // which may not be equal to the enum's type for
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)
914 base::get_extern_const(bcx.ccx(), did, const_ty)
916 let lval = Lvalue::new("expr::trans_def");
917 DatumBlock::new(bcx, Datum::new(val, const_ty, LvalueExpr(lval)))
919 def::DefConst(_) => {
920 bcx.sess().span_bug(ref_expr.span,
921 "constant expression should not reach expr::trans_def")
924 DatumBlock::new(bcx, trans_local_var(bcx, def).to_expr_datum())
929 fn trans_rvalue_stmt_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
931 -> Block<'blk, 'tcx> {
933 let _icx = push_ctxt("trans_rvalue_stmt");
935 if bcx.unreachable.get() {
939 debuginfo::set_source_location(bcx.fcx, expr.id, expr.span);
942 ast::ExprParen(ref e) => {
943 trans_into(bcx, &**e, Ignore)
945 ast::ExprBreak(label_opt) => {
946 controlflow::trans_break(bcx, expr, label_opt)
948 ast::ExprAgain(label_opt) => {
949 controlflow::trans_cont(bcx, expr, label_opt)
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)
961 controlflow::trans_ret(bcx, expr, ex.as_ref().map(|e| &**e))
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);
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
976 ast::ExprWhile(ref cond, ref body, _) => {
977 controlflow::trans_while(bcx, expr, &**cond, &**body)
979 ast::ExprLoop(ref body, _) => {
980 controlflow::trans_loop(bcx, expr, &**body)
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"));
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:
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`!
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());
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,
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);
1026 src_datum.store_to(bcx, dst_datum.val)
1028 src_datum.store_to(bcx, dst_datum.val)
1031 ast::ExprAssignOp(op, ref dst, ref src) => {
1032 trans_assign_op(bcx, expr, op, &**dst, &**src)
1034 ast::ExprInlineAsm(ref a) => {
1035 asm::trans_inline_asm(bcx, a)
1038 bcx.tcx().sess.span_bug(
1040 &format!("trans_rvalue_stmt_unadjusted reached \
1041 fall-through case: {:?}",
1047 fn trans_rvalue_dps_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
1050 -> Block<'blk, 'tcx> {
1051 let _icx = push_ctxt("trans_rvalue_dps_unadjusted");
1053 let tcx = bcx.tcx();
1055 debuginfo::set_source_location(bcx.fcx, expr.id, expr.span);
1058 ast::ExprParen(ref e) => {
1059 trans_into(bcx, &**e, dest)
1061 ast::ExprPath(..) => {
1062 trans_def_dps_unadjusted(bcx, expr, bcx.def(expr.id), dest)
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)
1067 ast::ExprMatch(ref discr, ref arms, _) => {
1068 _match::trans_match(bcx, expr, &**discr, &arms[..], dest)
1070 ast::ExprBlock(ref blk) => {
1071 controlflow::trans_block(bcx, &**blk, dest)
1073 ast::ExprStruct(_, ref fields, ref base) => {
1076 base.as_ref().map(|e| &**e),
1079 node_id_type(bcx, expr.id),
1082 ast::ExprRange(ref start, ref end) => {
1083 // FIXME it is just not right that we are synthesising ast nodes in
1085 fn make_field(field_name: &str, expr: P<ast::Expr>) -> ast::Field {
1087 ident: codemap::dummy_spanned(token::str_to_ident(field_name)),
1089 span: codemap::DUMMY_SP,
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
1097 let (did, fields, ty_params) = match (start, end) {
1098 (&Some(ref start), &Some(ref end)) => {
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)])
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)])
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)])
1115 // Desugar to RangeFull
1116 (tcx.lang_items.range_full_struct(), vec![], vec![])
1120 if let Some(did) = did {
1121 let substs = Substs::new_type(ty_params, vec![]);
1127 tcx.mk_struct(tcx.lookup_adt_def(did),
1128 tcx.mk_substs(substs)),
1131 tcx.sess.span_bug(expr.span,
1132 "No lang item for ranges (how did we get this far?)")
1135 ast::ExprTup(ref args) => {
1136 let numbered_fields: Vec<(usize, &ast::Expr)> =
1137 args.iter().enumerate().map(|(i, arg)| (i, &**arg)).collect();
1141 &numbered_fields[..],
1146 ast::ExprLit(ref lit) => {
1148 ast::LitStr(ref s, _) => {
1149 tvec::trans_lit_str(bcx, expr, (*s).clone(), dest)
1154 .span_bug(expr.span,
1155 "trans_rvalue_dps_unadjusted shouldn't be \
1156 translating this type of literal")
1160 ast::ExprVec(..) | ast::ExprRepeat(..) => {
1161 tvec::trans_fixed_vstore(bcx, expr, dest)
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())
1168 let substs = match expr_ty(bcx, expr).sty {
1169 ty::TyClosure(_, ref substs) => substs,
1171 bcx.tcx().sess.span_bug(
1173 &format!("closure expr without closure type: {:?}", t)),
1175 closure::trans_closure_expr(dest, decl, body, expr.id, substs).unwrap_or(bcx)
1177 ast::ExprCall(ref f, ref args) => {
1178 if bcx.tcx().is_method_call(expr.id) {
1179 trans_overloaded_call(bcx,
1185 callee::trans_call(bcx,
1188 callee::ArgExprs(&args[..]),
1192 ast::ExprMethodCall(_, _, ref args) => {
1193 callee::trans_method_call(bcx,
1196 callee::ArgExprs(&args[..]),
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
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
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
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?)")
1224 ast::ExprAssignOp(op, ref dst, ref src) => {
1225 trans_assign_op(bcx, expr, op, &**dst, &**src)
1228 bcx.tcx().sess.span_bug(
1230 &format!("trans_rvalue_dps_unadjusted reached fall-through \
1237 fn trans_def_dps_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
1238 ref_expr: &ast::Expr,
1241 -> Block<'blk, 'tcx> {
1242 let _icx = push_ctxt("trans_def_dps_unadjusted");
1244 let lldest = match dest {
1245 SaveIn(lldest) => lldest,
1246 Ignore => { return bcx; }
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() {
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);
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);
1267 def::DefStruct(_) => {
1268 let ty = expr_ty(bcx, ref_expr);
1270 ty::TyStruct(def, _) if def.has_dtor(bcx.tcx()) => {
1271 let repr = adt::represent_type(bcx.ccx(), ty);
1272 adt::trans_set_discr(bcx, &*repr, lldest, 0);
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)));
1286 pub fn trans_def_fn_unadjusted<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
1287 ref_expr: &ast::Expr,
1289 param_substs: &'tcx Substs<'tcx>)
1290 -> Datum<'tcx, Rvalue> {
1291 let _icx = push_ctxt("trans_def_datum_unadjusted");
1294 def::DefFn(did, _) |
1295 def::DefStruct(did) | def::DefVariant(_, did, _) => {
1296 callee::trans_fn_ref(ccx, did, ExprId(ref_expr.id), param_substs)
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),
1305 ty::TraitContainer(trait_did) => {
1306 meth::trans_static_method_callee(ccx, method_did,
1307 trait_did, ref_expr.id,
1313 ccx.tcx().sess.span_bug(ref_expr.span, &format!(
1314 "trans_def_fn_unadjusted invoked on: {:?} for {:?}",
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>,
1324 -> Datum<'tcx, Lvalue> {
1325 let _icx = push_ctxt("trans_local_var");
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),
1336 bcx.sess().bug(&format!(
1337 "trans_local_var: no llval for upvar {} found",
1342 def::DefLocal(nid) => {
1343 let datum = match bcx.fcx.lllocals.borrow().get(&nid) {
1346 bcx.sess().bug(&format!(
1347 "trans_local_var: no datum for local/arg {} found",
1351 debug!("take_local(nid={}, v={}, ty={})",
1352 nid, bcx.val_to_string(datum.val), datum.ty);
1356 bcx.sess().unimpl(&format!(
1357 "unsupported def type in trans_local_var: {:?}",
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,
1369 dest: Dest) -> Block<'blk, 'tcx> {
1370 let _icx = push_ctxt("trans_rec");
1372 let tcx = bcx.tcx();
1373 let vinfo = VariantInfo::of_node(tcx, ty, expr_id);
1375 let mut need_base = vec![true; vinfo.fields.len()];
1377 let numbered_fields = fields.iter().map(|field| {
1378 let pos = vinfo.field_index(field.ident.node.name);
1379 need_base[pos] = false;
1381 }).collect::<Vec<_>>();
1383 let optbase = match base {
1384 Some(base_expr) => {
1385 let mut leftovers = Vec::new();
1386 for (i, b) in need_base.iter().enumerate() {
1388 leftovers.push((i, vinfo.fields[i].1));
1391 Some(StructBaseInfo {expr: base_expr,
1392 fields: leftovers })
1395 if need_base.iter().any(|b| *b) {
1396 tcx.sess.span_bug(expr_span, "missing fields and no base expr")
1408 DebugLoc::At(expr_id, expr_span))
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 }`.
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>)>
1424 /// Constructs an ADT instance:
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`.
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>,
1435 fields: &[(usize, &ast::Expr)],
1436 optbase: Option<StructBaseInfo<'a, 'tcx>>,
1438 debug_location: DebugLoc)
1439 -> Block<'blk, 'tcx> {
1440 let _icx = push_ctxt("trans_adt");
1442 let repr = adt::represent_type(bcx.ccx(), ty);
1444 debug_location.apply(bcx.fcx);
1446 // If we don't care about the result, just make a
1447 // temporary stack slot
1448 let addr = match dest {
1450 Ignore => alloc_ty(bcx, ty, "temp"),
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();
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.
1462 // To be safe, double-check that we did not get here via FRU.
1463 assert!(optbase.is_none());
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);
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);
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);
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.
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));
1493 debug_location.apply(bcx.fcx);
1495 // Second, trans the base to the dest.
1496 assert_eq!(discr, 0);
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));
1502 ExprKind::RvalueStmt => {
1503 bcx.tcx().sess.bug("unexpected expr kind for struct base expr")
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);
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);
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);
1535 adt::trans_set_discr(bcx, &*repr, addr, discr);
1537 fcx.pop_custom_cleanup_scope(custom_cleanup_scope);
1539 // If we don't care about the result drop the temporary we made
1543 bcx = glue::drop_ty(bcx, addr, ty, debug_location);
1544 base::call_lifetime_end(bcx, addr);
1551 fn trans_immediate_lit<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
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()
1562 fn trans_unary<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
1565 sub_expr: &ast::Expr)
1566 -> DatumBlock<'blk, 'tcx, Expr> {
1567 let ccx = bcx.ccx();
1569 let _icx = push_ctxt("trans_unary_datum");
1571 let method_call = MethodCall::expr(expr.id);
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));
1578 let un_ty = expr_ty(bcx, expr);
1580 let debug_loc = expr.debug_loc();
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()
1589 let datum = unpack_datum!(bcx, trans(bcx, sub_expr));
1590 let val = datum.to_llscalarish(bcx);
1591 let (bcx, llneg) = {
1593 let result = FNeg(bcx, val, debug_loc);
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)
1613 immediate_rvalue_bcx(bcx, llneg, un_ty).to_expr_datumblock()
1616 trans_uniq_expr(bcx, expr, un_ty, sub_expr, expr_ty(bcx, sub_expr))
1619 let datum = unpack_datum!(bcx, trans(bcx, sub_expr));
1620 deref_once(bcx, expr, datum, method_call)
1625 fn trans_uniq_expr<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
1626 box_expr: &ast::Expr,
1628 contents: &ast::Expr,
1629 contents_ty: Ty<'tcx>)
1630 -> DatumBlock<'blk, 'tcx, Expr> {
1631 let _icx = push_ctxt("trans_uniq_expr");
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,
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))
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);
1657 immediate_rvalue_bcx(bcx, val, box_ty).to_expr_datumblock()
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);
1667 DatumBlock::new(bcx, scratch.to_expr_datum())
1670 fn trans_addr_of<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
1672 subexpr: &ast::Expr)
1673 -> DatumBlock<'blk, 'tcx, Expr> {
1674 let _icx = push_ctxt("trans_addr_of");
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)
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()
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,
1697 -> DatumBlock<'blk, 'tcx, Expr> {
1698 let _icx = push_ctxt("trans_eager_binop");
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)
1707 let is_float = intype.is_fp();
1708 let is_signed = intype.is_signed();
1709 let info = expr_info(binop_expr);
1711 let binop_debug_loc = binop_expr.debug_loc();
1714 let val = match op.node {
1717 FAdd(bcx, lhs, rhs, binop_debug_loc)
1719 Add(bcx, lhs, rhs, binop_debug_loc)
1721 let (newbcx, res) = with_overflow_check(
1722 bcx, OverflowOp::Add, info, lhs_t, lhs, rhs, binop_debug_loc);
1729 FSub(bcx, lhs, rhs, binop_debug_loc)
1731 Sub(bcx, lhs, rhs, binop_debug_loc)
1733 let (newbcx, res) = with_overflow_check(
1734 bcx, OverflowOp::Sub, info, lhs_t, lhs, rhs, binop_debug_loc);
1741 FMul(bcx, lhs, rhs, binop_debug_loc)
1743 Mul(bcx, lhs, rhs, binop_debug_loc)
1745 let (newbcx, res) = with_overflow_check(
1746 bcx, OverflowOp::Mul, info, lhs_t, lhs, rhs, binop_debug_loc);
1753 FDiv(bcx, lhs, rhs, binop_debug_loc)
1755 // Only zero-check integers; fp /0 is NaN
1756 bcx = base::fail_if_zero_or_overflows(bcx,
1757 expr_info(binop_expr),
1763 SDiv(bcx, lhs, rhs, binop_debug_loc)
1765 UDiv(bcx, lhs, rhs, binop_debug_loc)
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.
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.
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";
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,
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()))
1803 Call(bcx, llfn, &[lhs, rhs], None, binop_debug_loc)
1806 FRem(bcx, lhs, rhs, binop_debug_loc)
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);
1814 SRem(bcx, lhs, rhs, binop_debug_loc)
1816 URem(bcx, lhs, rhs, binop_debug_loc)
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),
1824 let (newbcx, res) = with_overflow_check(
1825 bcx, OverflowOp::Shl, info, lhs_t, lhs, rhs, binop_debug_loc);
1830 let (newbcx, res) = with_overflow_check(
1831 bcx, OverflowOp::Shr, info, lhs_t, lhs, rhs, binop_debug_loc);
1835 ast::BiEq | ast::BiNe | ast::BiLt | ast::BiGe | ast::BiLe | ast::BiGt => {
1837 base::compare_simd_types(bcx, lhs, rhs, intype, val_ty(lhs), op.node, binop_debug_loc)
1839 base::compare_scalar_types(bcx, lhs, rhs, intype, op.node, binop_debug_loc)
1843 bcx.tcx().sess.span_bug(binop_expr.span, "unexpected binop");
1847 immediate_rvalue_bcx(bcx, val, binop_ty).to_expr_datumblock()
1850 // refinement types would obviate the need for this
1851 enum lazy_binop_ty {
1856 fn trans_lazy_binop<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
1857 binop_expr: &ast::Expr,
1861 -> DatumBlock<'blk, 'tcx, Expr> {
1862 let _icx = push_ctxt("trans_lazy_binop");
1863 let binop_ty = expr_ty(bcx, binop_expr);
1866 let DatumBlock {bcx: past_lhs, datum: lhs} = trans(bcx, a);
1867 let lhs = lhs.to_llscalarish(past_lhs);
1869 if past_lhs.unreachable.get() {
1870 return immediate_rvalue_bcx(past_lhs, lhs, binop_ty).to_expr_datumblock();
1873 let join = fcx.new_id_block("join", binop_expr.id);
1874 let before_rhs = fcx.new_id_block("before_rhs", b.id);
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)
1881 let DatumBlock {bcx: past_rhs, datum: rhs} = trans(before_rhs, b);
1882 let rhs = rhs.to_llscalarish(past_rhs);
1884 if past_rhs.unreachable.get() {
1885 return immediate_rvalue_bcx(join, lhs, binop_ty).to_expr_datumblock();
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]);
1892 return immediate_rvalue_bcx(join, phi, binop_ty).to_expr_datumblock();
1895 fn trans_binary<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
1900 -> DatumBlock<'blk, 'tcx, Expr> {
1901 let _icx = push_ctxt("trans_binary");
1902 let ccx = bcx.ccx();
1904 // if overloaded, would be RvalueDpsExpr
1905 assert!(!ccx.tcx().is_method_call(expr.id));
1909 trans_lazy_binop(bcx, expr, lazy_and, lhs, rhs)
1912 trans_lazy_binop(bcx, expr, lazy_or, lhs, rhs)
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);
1920 debug!("trans_binary (expr {}): lhs_datum={}",
1922 lhs_datum.to_string(ccx));
1923 let lhs_ty = lhs_datum.ty;
1924 let lhs = lhs_datum.to_llscalarish(bcx);
1926 debug!("trans_binary (expr {}): rhs_datum={}",
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)
1937 fn trans_overloaded_op<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
1939 method_call: MethodCall,
1940 lhs: Datum<'tcx, Expr>,
1941 rhs: Option<(Datum<'tcx, Expr>, ast::NodeId)>,
1944 -> Result<'blk, 'tcx> {
1945 callee::trans_call_inner(bcx,
1947 |bcx, arg_cleanup_scope| {
1948 meth::trans_method_callee(bcx,
1953 callee::ArgOverloadedOp(lhs, rhs, autoref),
1957 fn trans_overloaded_call<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>,
1959 callee: &'a ast::Expr,
1960 args: &'a [P<ast::Expr>],
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));
1968 callee::trans_call_inner(bcx,
1970 |bcx, arg_cleanup_scope| {
1971 meth::trans_method_callee(
1977 callee::ArgOverloadedCall(all_args),
1982 pub fn cast_is_noop<'tcx>(tcx: &ty::ctxt<'tcx>,
1987 if let Some(&CastKind::CoercionCast) = tcx.cast_kinds.borrow().get(&expr.id) {
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, .. })) => {
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.
2004 fn trans_imm_cast<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
2007 -> DatumBlock<'blk, 'tcx, Expr>
2009 use middle::cast::CastTy::*;
2010 use middle::cast::IntTy::*;
2012 fn int_cast(bcx: Block,
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)
2027 SExtOrBitCast(bcx, llsrc, lldsttype)
2029 ZExtOrBitCast(bcx, llsrc, lldsttype)
2033 fn float_cast(bcx: Block,
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)
2049 let _icx = push_ctxt("trans_cast");
2051 let ccx = bcx.ccx();
2053 let t_in = expr_ty_adjusted(bcx, expr);
2054 let t_out = node_id_type(bcx, id);
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));
2063 let datum_ty = monomorphize_type(bcx, datum.ty);
2065 if cast_is_noop(bcx.tcx(), expr, datum_ty, t_out) {
2067 return DatumBlock::new(bcx, datum);
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()),
2077 )).to_expr_datumblock();
2079 // Return the address
2080 return immediate_rvalue_bcx(bcx,
2082 Load(bcx, get_dataptr(bcx, datum.val)),
2084 t_out).to_expr_datumblock();
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");
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))
2100 (datum.to_llscalarish(bcx), t_in.is_signed())
2103 let newval = match (r_t_in, r_t_out) {
2104 (Ptr(_), Ptr(_)) | (FnPtr, Ptr(_)) | (RPtr(_), Ptr(_)) => {
2105 PointerCast(bcx, llexpr, ll_t_out)
2107 (Ptr(_), Int(_)) | (FnPtr, Int(_)) => PtrToInt(bcx, llexpr, ll_t_out),
2108 (Int(_), Ptr(_)) => IntToPtr(bcx, llexpr, ll_t_out),
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),
2117 _ => ccx.sess().span_bug(expr.span,
2118 &format!("translating unsupported cast: \
2124 return immediate_rvalue_bcx(bcx, newval, t_out).to_expr_datumblock();
2127 fn trans_assign_op<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
2132 -> Block<'blk, 'tcx> {
2133 let _icx = push_ctxt("trans_assign_op");
2136 debug!("trans_assign_op(expr={:?})", expr);
2138 // User-defined operator methods cannot be used with `+=` etc right now
2139 assert!(!bcx.tcx().is_method_call(expr.id));
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);
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);
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);
2159 fn auto_ref<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
2160 datum: Datum<'tcx, Expr>,
2162 -> DatumBlock<'blk, 'tcx, Expr> {
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));
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);
2175 let llref = lv_datum.to_llref();
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))))
2185 fn deref_multiple<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
2187 datum: Datum<'tcx, Expr>,
2189 -> DatumBlock<'blk, 'tcx, Expr> {
2191 let mut datum = datum;
2193 let method_call = MethodCall::autoderef(expr.id, i as u32);
2194 datum = unpack_datum!(bcx, deref_once(bcx, expr, datum, method_call));
2196 DatumBlock { bcx: bcx, datum: datum }
2199 fn deref_once<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
2201 datum: Datum<'tcx, Expr>,
2202 method_call: MethodCall)
2203 -> DatumBlock<'blk, 'tcx, Expr> {
2204 let ccx = bcx.ccx();
2206 debug!("deref_once(expr={:?}, datum={}, method_call={:?})",
2208 datum.to_string(ccx),
2213 // Check for overloaded deref.
2214 let method_ty = ccx.tcx()
2218 .get(&method_call).map(|method| method.ty);
2220 let datum = match method_ty {
2221 Some(method_ty) => {
2222 let method_ty = monomorphize_type(bcx, method_ty);
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 {
2232 // Always perform an AutoPtr when applying an overloaded auto-deref
2233 unpack_datum!(bcx, auto_ref(bcx, datum, expr))
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");
2240 unpack_result!(bcx, trans_overloaded_op(bcx, expr, method_call,
2241 datum, None, Some(SaveIn(scratch.val)),
2243 scratch.to_expr_datum()
2246 // Not overloaded. We already have a pointer we know how to deref.
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));
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)))
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.
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)
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);
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)))
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)))
2294 bcx.tcx().sess.span_bug(
2296 &format!("deref invoked on expr of invalid type {:?}",
2301 debug!("deref_once(expr={}, method_call={:?}, result={})",
2302 expr.id, method_call, r.datum.to_string(ccx));
2317 fn codegen_strategy(&self) -> OverflowCodegen {
2318 use self::OverflowCodegen::{ViaIntrinsic, ViaInputCheck};
2320 OverflowOp::Add => ViaIntrinsic(OverflowOpViaIntrinsic::Add),
2321 OverflowOp::Sub => ViaIntrinsic(OverflowOpViaIntrinsic::Sub),
2322 OverflowOp::Mul => ViaIntrinsic(OverflowOpViaIntrinsic::Mul),
2324 OverflowOp::Shl => ViaInputCheck(OverflowOpViaInputCheck::Shl),
2325 OverflowOp::Shr => ViaInputCheck(OverflowOpViaInputCheck::Shr),
2330 enum OverflowCodegen {
2331 ViaIntrinsic(OverflowOpViaIntrinsic),
2332 ViaInputCheck(OverflowOpViaInputCheck),
2335 enum OverflowOpViaInputCheck { Shl, Shr, }
2338 enum OverflowOpViaIntrinsic { Add, Sub, Mul, }
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)
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};
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")
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")
2361 ref t @ TyUint(_) | ref t @ TyInt(_) => t.clone(),
2362 _ => panic!("tried to get overflow intrinsic for {:?} applied to non-int type",
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",
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",
2378 _ => unreachable!(),
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",
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",
2391 _ => unreachable!(),
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",
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",
2404 _ => unreachable!(),
2409 fn build_intrinsic_call<'blk, 'tcx>(&self, bcx: Block<'blk, 'tcx>,
2410 info: NodeIdAndSpan,
2411 lhs_t: Ty<'tcx>, lhs: ValueRef,
2413 binop_debug_loc: DebugLoc)
2414 -> (Block<'blk, 'tcx>, ValueRef) {
2415 let llfn = self.to_intrinsic(bcx, lhs_t);
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?"
2421 let cond = ICmp(bcx, llvm::IntEQ, overflow, C_integral(Type::i1(bcx.ccx()), 1, false),
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);
2429 base::with_cond(bcx, cond, |bcx|
2430 controlflow::trans_fail(bcx, info,
2431 InternedString::new("arithmetic operation overflowed")));
2437 impl OverflowOpViaInputCheck {
2438 fn build_with_input_check<'blk, 'tcx>(&self,
2439 bcx: Block<'blk, 'tcx>,
2440 info: NodeIdAndSpan,
2444 binop_debug_loc: DebugLoc)
2445 -> (Block<'blk, 'tcx>, ValueRef)
2447 let lhs_llty = val_ty(lhs);
2448 let rhs_llty = val_ty(rhs);
2450 // Panic if any bits are set outside of bits that we always
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);
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),
2468 base::with_cond(bcx, cond, |bcx|
2469 controlflow::trans_fail(bcx, info,
2470 InternedString::new("shift operation overflowed")));
2476 fn shift_mask_val<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
2479 invert: bool) -> ValueRef {
2480 let kind = llty.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;
2486 C_integral(mask_llty, !val, true)
2488 C_integral(mask_llty, val, false)
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)
2495 _ => panic!("shift_mask_val: expected Integer or Vector, found {:?}", kind),
2499 // Check if an integer or vector contains a nonzero element.
2500 fn build_nonzero_check<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
2502 binop_debug_loc: DebugLoc) -> ValueRef {
2503 let llty = val_ty(value);
2504 let kind = llty.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)
2514 _ => panic!("build_nonzero_check: expected Integer or Vector, found {:?}", kind),
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.)
2523 fn build_unchecked_lshift<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
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)
2533 fn build_unchecked_rshift<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
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)
2548 let is_signed = intype.is_signed();
2550 AShr(bcx, lhs, rhs, binop_debug_loc)
2552 LShr(bcx, lhs, rhs, binop_debug_loc)
2556 fn shift_mask_rhs<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
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)
2563 fn with_overflow_check<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, oop: OverflowOp, info: NodeIdAndSpan,
2564 lhs_t: Ty<'tcx>, lhs: 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() {
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),
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),
2584 build_unchecked_lshift(bcx, lhs, rhs, binop_debug_loc),
2586 build_unchecked_rshift(bcx, lhs_t, lhs, rhs, binop_debug_loc),
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
2597 #[derive(Copy, Clone)]
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,
2613 // the deref method invoked for `*a` always yields an `&T`
2614 ast::ExprUnary(ast::UnDeref, _) => ExprKind::Lvalue,
2616 // the index method invoked for `a[i]` always yields an `&T`
2617 ast::ExprIndex(..) => ExprKind::Lvalue,
2619 // in the general case, result could be any type, use DPS
2620 _ => ExprKind::RvalueDps
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 {
2630 ExprKind::RvalueDatum
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,
2642 // Fn pointers are just scalar values.
2643 def::DefFn(..) | def::DefMethod(..) => ExprKind::RvalueDatum,
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(..) |
2650 def::DefLocal(..) => ExprKind::Lvalue,
2653 def::DefAssociatedConst(..) => ExprKind::RvalueDatum,
2658 &format!("uncategorized def for expr {}: {:?}",
2665 ast::ExprUnary(ast::UnDeref, _) |
2666 ast::ExprField(..) |
2667 ast::ExprTupField(..) |
2668 ast::ExprIndex(..) => {
2673 ast::ExprMethodCall(..) |
2674 ast::ExprStruct(..) |
2675 ast::ExprRange(..) |
2678 ast::ExprMatch(..) |
2679 ast::ExprClosure(..) |
2680 ast::ExprBlock(..) |
2681 ast::ExprRepeat(..) |
2682 ast::ExprVec(..) => {
2686 ast::ExprIfLet(..) => {
2687 tcx.sess.span_bug(expr.span, "non-desugared ExprIfLet");
2689 ast::ExprWhileLet(..) => {
2690 tcx.sess.span_bug(expr.span, "non-desugared ExprWhileLet");
2693 ast::ExprForLoop(..) => {
2694 tcx.sess.span_bug(expr.span, "non-desugared ExprForLoop");
2697 ast::ExprLit(ref lit) if ast_util::lit_is_str(&**lit) => {
2701 ast::ExprBreak(..) |
2702 ast::ExprAgain(..) |
2704 ast::ExprWhile(..) |
2706 ast::ExprAssign(..) |
2707 ast::ExprInlineAsm(..) |
2708 ast::ExprAssignOp(..) => {
2709 ExprKind::RvalueStmt
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
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"),
2727 if tcx.lang_items.exchange_heap() == Some(def_id) {
2728 ExprKind::RvalueDatum
2734 ast::ExprParen(ref e) => expr_kind(tcx, &**e),
2736 ast::ExprMac(..) => {
2739 "macro expression remains after expansion");