2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
/*!
|
|
|
|
|
|
|
|
# Translation of expressions.
|
|
|
|
|
2013-01-10 12:59:58 -06:00
|
|
|
## Recommended entry point
|
|
|
|
|
|
|
|
If you wish to translate an expression, the preferred way to do
|
|
|
|
so is to use:
|
|
|
|
|
|
|
|
expr::trans_into(block, expr, Dest) -> block
|
|
|
|
|
|
|
|
This will generate code that evaluates `expr`, storing the result into
|
|
|
|
`Dest`, which must either be the special flag ignore (throw the result
|
|
|
|
away) or be a pointer to memory of the same type/size as the
|
|
|
|
expression. It returns the resulting basic block. This form will
|
2013-06-20 14:23:52 -05:00
|
|
|
handle all automatic adjustments for you. The value will be moved if
|
|
|
|
its type is linear and copied otherwise.
|
2013-01-10 12:59:58 -06:00
|
|
|
|
|
|
|
## Translation to a datum
|
|
|
|
|
|
|
|
In some cases, `trans_into()` is too narrow of an interface.
|
|
|
|
Generally this occurs either when you know that the result value is
|
|
|
|
going to be a scalar, or when you need to evaluate the expression into
|
|
|
|
some memory location so you can go and inspect it (e.g., assignments,
|
|
|
|
`match` expressions, the `&` operator).
|
|
|
|
|
|
|
|
In such cases, you want the following function:
|
|
|
|
|
|
|
|
trans_to_datum(block, expr) -> DatumBlock
|
|
|
|
|
|
|
|
This function generates code to evaluate the expression and return a
|
|
|
|
`Datum` describing where the result is to be found. This function
|
|
|
|
tries to return its result in the most efficient way possible, without
|
|
|
|
introducing extra copies or sacrificing information. Therefore, for
|
|
|
|
lvalue expressions, you always get a by-ref `Datum` in return that
|
2013-06-20 14:23:52 -05:00
|
|
|
points at the memory for this lvalue. For rvalue expressions, we will
|
|
|
|
return a by-value `Datum` whenever possible, but it is often necessary
|
|
|
|
to allocate a stack slot, store the result of the rvalue in there, and
|
|
|
|
then return a pointer to the slot (see the discussion later on about
|
|
|
|
the different kinds of rvalues).
|
2013-01-10 12:59:58 -06:00
|
|
|
|
|
|
|
NB: The `trans_to_datum()` function does perform adjustments, but
|
|
|
|
since it returns a pointer to the value "in place" it does not handle
|
2013-06-20 14:23:52 -05:00
|
|
|
moves. If you wish to copy/move the value returned into a new
|
|
|
|
location, you should use the Datum method `store_to()` (move or copy
|
|
|
|
depending on type). You can also use `move_to()` (force move) or
|
|
|
|
`copy_to()` (force copy) for special situations.
|
2013-01-10 12:59:58 -06:00
|
|
|
|
|
|
|
## Translating local variables
|
|
|
|
|
|
|
|
`trans_local_var()` can be used to trans a ref to a local variable
|
|
|
|
that is not an expression. This is needed for captures.
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
## Ownership and cleanups
|
|
|
|
|
|
|
|
The current system for cleanups associates required cleanups with
|
|
|
|
block contexts. Block contexts are structured into a tree that
|
|
|
|
resembles the code itself. Not every block context has cleanups
|
|
|
|
associated with it, only those blocks that have a kind of
|
|
|
|
`block_scope`. See `common::block_kind` for more details.
|
|
|
|
|
|
|
|
If you invoke `trans_into()`, no cleanup is scheduled for you. The
|
|
|
|
value is written into the given destination and is assumed to be owned
|
|
|
|
by that destination.
|
|
|
|
|
2012-09-11 23:25:01 -05:00
|
|
|
When you invoke `trans_to_datum()` on an rvalue, the resulting
|
|
|
|
datum/value will have an appropriate cleanup scheduled for the
|
|
|
|
innermost cleanup scope. If you later use `move_to()` or
|
|
|
|
`drop_val()`, this cleanup will be canceled.
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
During the evaluation of an expression, temporary cleanups are created
|
|
|
|
and later canceled. These represent intermediate or partial results
|
|
|
|
which must be cleaned up in the event of task failure.
|
|
|
|
|
|
|
|
## Implementation details
|
|
|
|
|
|
|
|
We divide expressions into three categories, based on how they are most
|
|
|
|
naturally implemented:
|
|
|
|
|
|
|
|
1. Lvalues
|
|
|
|
2. Datum rvalues
|
|
|
|
3. DPS rvalues
|
|
|
|
4. Statement rvalues
|
|
|
|
|
|
|
|
Lvalues always refer to user-assignable memory locations.
|
|
|
|
Translating those always results in a by-ref datum; this introduces
|
|
|
|
no inefficiencies into the generated code, because all lvalues are
|
|
|
|
naturally addressable.
|
|
|
|
|
|
|
|
Datum rvalues are rvalues that always generate datums as a result.
|
|
|
|
These are generally scalar results, such as `a+b` where `a` and `b`
|
|
|
|
are integers.
|
|
|
|
|
|
|
|
DPS rvalues are rvalues that, when translated, must be given a
|
|
|
|
memory location to write into (or the Ignore flag). These are
|
|
|
|
generally expressions that produce structural results that are
|
|
|
|
larger than one word (e.g., a struct literal), but also expressions
|
|
|
|
(like `if`) that involve control flow (otherwise we'd have to
|
|
|
|
generate phi nodes).
|
|
|
|
|
|
|
|
Finally, statement rvalues are rvalues that always produce a nil
|
|
|
|
return type, such as `while` loops or assignments (`a = b`).
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2013-02-25 13:11:21 -06:00
|
|
|
use back::abi;
|
2013-08-28 01:12:05 -05:00
|
|
|
use back::link;
|
2013-09-11 12:06:16 -05:00
|
|
|
use lib::llvm::{ValueRef, llvm, SetLinkage, False};
|
2013-05-03 20:51:58 -05:00
|
|
|
use lib;
|
2013-03-27 17:41:43 -05:00
|
|
|
use metadata::csearch;
|
2013-02-25 13:11:21 -06:00
|
|
|
use middle::trans::_match;
|
2013-02-23 15:36:01 -06:00
|
|
|
use middle::trans::adt;
|
2013-03-27 14:50:57 -05:00
|
|
|
use middle::trans::asm;
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::trans::base::*;
|
2013-05-03 20:51:58 -05:00
|
|
|
use middle::trans::base;
|
2013-02-25 13:11:21 -06:00
|
|
|
use middle::trans::build::*;
|
2013-03-26 15:38:07 -05:00
|
|
|
use middle::trans::callee::DoAutorefArg;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::trans::callee;
|
|
|
|
use middle::trans::closure;
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::trans::common::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::trans::consts;
|
|
|
|
use middle::trans::controlflow;
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::trans::datum::*;
|
2013-02-25 13:11:21 -06:00
|
|
|
use middle::trans::debuginfo;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::trans::machine;
|
|
|
|
use middle::trans::meth;
|
2013-09-11 12:06:16 -05:00
|
|
|
use middle::trans::inline;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::trans::tvec;
|
2013-02-25 13:11:21 -06:00
|
|
|
use middle::trans::type_of;
|
2013-05-03 20:51:58 -05:00
|
|
|
use middle::ty::struct_fields;
|
2013-12-26 12:54:41 -06:00
|
|
|
use middle::ty::{AutoBorrowObj, AutoDerefRef, AutoAddEnv, AutoObject, AutoUnsafe};
|
2013-08-11 12:42:26 -05:00
|
|
|
use middle::ty::{AutoPtr, AutoBorrowVec, AutoBorrowVecRef, AutoBorrowFn};
|
2013-05-09 14:58:02 -05:00
|
|
|
use middle::ty;
|
2012-12-13 15:05:22 -06:00
|
|
|
use util::common::indenter;
|
2013-03-15 14:24:24 -05:00
|
|
|
use util::ppaux::Repr;
|
2013-07-02 18:51:39 -05:00
|
|
|
use middle::trans::machine::llsize_of;
|
2012-12-13 15:05:22 -06:00
|
|
|
|
2013-06-16 05:52:44 -05:00
|
|
|
use middle::trans::type_::Type;
|
|
|
|
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::hashmap::HashMap;
|
|
|
|
use std::vec;
|
2012-12-13 15:05:22 -06:00
|
|
|
use syntax::print::pprust::{expr_to_str};
|
2013-01-09 11:49:11 -06:00
|
|
|
use syntax::ast;
|
2014-01-09 07:05:33 -06:00
|
|
|
use syntax::ast_map::PathMod;
|
2013-02-25 13:11:21 -06:00
|
|
|
use syntax::codemap;
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
// Destinations
|
|
|
|
|
|
|
|
// These are passed around by the code generating functions to track the
|
|
|
|
// destination of a computation's value.
|
|
|
|
|
2013-03-26 07:04:54 -05:00
|
|
|
#[deriving(Eq)]
|
2013-01-29 19:57:02 -06:00
|
|
|
pub enum Dest {
|
2012-08-28 17:54:45 -05:00
|
|
|
SaveIn(ValueRef),
|
|
|
|
Ignore,
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl Dest {
|
2013-06-13 02:19:50 -05:00
|
|
|
pub fn to_str(&self, ccx: &CrateContext) -> ~str {
|
2013-02-22 00:41:37 -06:00
|
|
|
match *self {
|
2013-09-28 00:38:08 -05:00
|
|
|
SaveIn(v) => format!("SaveIn({})", ccx.tn.val_to_str(v)),
|
2012-08-28 17:54:45 -05:00
|
|
|
Ignore => ~"Ignore"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
pub fn trans_to_datum<'a>(bcx: &'a Block<'a>, expr: &ast::Expr)
|
|
|
|
-> DatumBlock<'a> {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("trans_to_datum(expr={})", bcx.expr_to_str(expr));
|
2013-03-15 14:24:24 -05:00
|
|
|
|
2013-05-07 10:41:27 -05:00
|
|
|
let mut bcx = bcx;
|
|
|
|
let mut datum = unpack_datum!(bcx, trans_to_datum_unadjusted(bcx, expr));
|
2013-12-19 20:26:45 -06:00
|
|
|
let adjustment = {
|
|
|
|
let adjustments = bcx.tcx().adjustments.borrow();
|
|
|
|
match adjustments.get().find_copy(&expr.id) {
|
|
|
|
None => { return DatumBlock {bcx: bcx, datum: datum}; }
|
|
|
|
Some(adj) => { adj }
|
|
|
|
}
|
2013-05-07 10:41:27 -05:00
|
|
|
};
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("unadjusted datum: {}", datum.to_str(bcx.ccx()));
|
2013-05-07 10:41:27 -05:00
|
|
|
match *adjustment {
|
2013-11-28 14:22:53 -06:00
|
|
|
AutoAddEnv(..) => {
|
2013-05-07 10:41:27 -05:00
|
|
|
datum = unpack_datum!(bcx, add_env(bcx, expr, datum));
|
|
|
|
}
|
|
|
|
AutoDerefRef(ref adj) => {
|
2012-09-11 23:25:01 -05:00
|
|
|
if adj.autoderefs > 0 {
|
2013-05-07 10:41:27 -05:00
|
|
|
datum =
|
|
|
|
unpack_datum!(
|
|
|
|
bcx,
|
|
|
|
datum.autoderef(bcx, expr.span,
|
|
|
|
expr.id, adj.autoderefs));
|
2012-09-11 23:25:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
datum = match adj.autoref {
|
2013-03-15 14:24:24 -05:00
|
|
|
None => {
|
|
|
|
datum
|
|
|
|
}
|
2013-11-28 14:22:53 -06:00
|
|
|
Some(AutoUnsafe(..)) | // region + unsafe ptrs have same repr
|
|
|
|
Some(AutoPtr(..)) => {
|
2013-03-15 14:24:24 -05:00
|
|
|
unpack_datum!(bcx, auto_ref(bcx, datum))
|
|
|
|
}
|
2013-11-28 14:22:53 -06:00
|
|
|
Some(AutoBorrowVec(..)) => {
|
2013-05-09 14:58:02 -05:00
|
|
|
unpack_datum!(bcx, auto_slice(bcx, adj.autoderefs,
|
|
|
|
expr, datum))
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
2013-11-28 14:22:53 -06:00
|
|
|
Some(AutoBorrowVecRef(..)) => {
|
2013-05-09 14:58:02 -05:00
|
|
|
unpack_datum!(bcx, auto_slice_and_ref(bcx, adj.autoderefs,
|
|
|
|
expr, datum))
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
2013-11-28 14:22:53 -06:00
|
|
|
Some(AutoBorrowFn(..)) => {
|
2013-05-07 10:41:27 -05:00
|
|
|
let adjusted_ty = ty::adjust_ty(bcx.tcx(), expr.span,
|
|
|
|
datum.ty, Some(adjustment));
|
|
|
|
unpack_datum!(bcx, auto_borrow_fn(bcx, adjusted_ty, datum))
|
2012-09-11 23:25:01 -05:00
|
|
|
}
|
2013-11-28 14:22:53 -06:00
|
|
|
Some(AutoBorrowObj(..)) => {
|
2013-08-11 12:42:26 -05:00
|
|
|
unpack_datum!(bcx, auto_borrow_obj(
|
|
|
|
bcx, adj.autoderefs, expr, datum))
|
|
|
|
}
|
2012-09-11 23:25:01 -05:00
|
|
|
};
|
|
|
|
}
|
2013-12-26 12:54:41 -06:00
|
|
|
AutoObject(ref sigil, ref region, _, _, _, _) => {
|
|
|
|
|
|
|
|
let adjusted_ty = ty::expr_ty_adjusted(bcx.tcx(), expr);
|
|
|
|
let scratch = scratch_datum(bcx, adjusted_ty, "__adjust", false);
|
|
|
|
|
|
|
|
let trait_store = match *sigil {
|
|
|
|
ast::BorrowedSigil => ty::RegionTraitStore(region.expect("expected valid region")),
|
|
|
|
ast::OwnedSigil => ty::UniqTraitStore,
|
|
|
|
ast::ManagedSigil => ty::BoxTraitStore
|
|
|
|
};
|
|
|
|
|
|
|
|
bcx = meth::trans_trait_cast(bcx, expr, expr.id, SaveIn(scratch.val),
|
|
|
|
trait_store, false /* no adjustments */);
|
|
|
|
|
|
|
|
datum = scratch.to_appropriate_datum(bcx);
|
|
|
|
datum.add_clean(bcx);
|
|
|
|
}
|
2013-05-07 10:41:27 -05:00
|
|
|
}
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("after adjustments, datum={}", datum.to_str(bcx.ccx()));
|
2013-05-07 10:41:27 -05:00
|
|
|
return DatumBlock {bcx: bcx, datum: datum};
|
2012-09-11 23:25:01 -05:00
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn auto_ref<'a>(bcx: &'a Block<'a>, datum: Datum) -> DatumBlock<'a> {
|
2012-09-11 23:25:01 -05:00
|
|
|
DatumBlock {bcx: bcx, datum: datum.to_rptr(bcx)}
|
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn auto_borrow_fn<'a>(
|
|
|
|
bcx: &'a Block<'a>,
|
2013-05-07 10:41:27 -05:00
|
|
|
adjusted_ty: ty::t,
|
2014-01-07 10:54:58 -06:00
|
|
|
datum: Datum)
|
|
|
|
-> DatumBlock<'a> {
|
2013-05-07 10:41:27 -05:00
|
|
|
// Currently, all closure types are represented precisely the
|
|
|
|
// same, so no runtime adjustment is required, but we still
|
|
|
|
// must patchup the type.
|
|
|
|
DatumBlock {bcx: bcx,
|
|
|
|
datum: Datum {val: datum.val, ty: adjusted_ty,
|
2013-05-29 14:49:23 -05:00
|
|
|
mode: datum.mode}}
|
2013-05-07 10:41:27 -05:00
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn auto_slice<'a>(
|
|
|
|
bcx: &'a Block<'a>,
|
2013-05-09 14:58:02 -05:00
|
|
|
autoderefs: uint,
|
2013-09-01 20:45:37 -05:00
|
|
|
expr: &ast::Expr,
|
2014-01-07 10:54:58 -06:00
|
|
|
datum: Datum)
|
|
|
|
-> DatumBlock<'a> {
|
2012-09-11 23:25:01 -05:00
|
|
|
// This is not the most efficient thing possible; since slices
|
|
|
|
// are two words it'd be better if this were compiled in
|
|
|
|
// 'dest' mode, but I can't find a nice way to structure the
|
|
|
|
// code and keep it DRY that accommodates that use case at the
|
|
|
|
// moment.
|
|
|
|
|
|
|
|
let tcx = bcx.tcx();
|
|
|
|
let unit_ty = ty::sequence_element_type(tcx, datum.ty);
|
2013-05-06 13:02:28 -05:00
|
|
|
|
2013-05-03 15:26:43 -05:00
|
|
|
let (bcx, base, len) =
|
2013-10-16 11:04:51 -05:00
|
|
|
datum.get_vec_base_and_len(bcx, expr.span, expr.id, autoderefs+1);
|
2012-09-11 23:25:01 -05:00
|
|
|
|
|
|
|
// this type may have a different region/mutability than the
|
|
|
|
// real one, but it will have the same runtime representation
|
2013-12-31 20:09:50 -06:00
|
|
|
let slice_ty = ty::mk_vec(tcx,
|
|
|
|
ty::mt { ty: unit_ty, mutbl: ast::MutImmutable },
|
|
|
|
ty::vstore_slice(ty::ReStatic));
|
2012-09-11 23:25:01 -05:00
|
|
|
|
2013-06-20 14:21:37 -05:00
|
|
|
let scratch = scratch_datum(bcx, slice_ty, "__adjust", false);
|
2013-10-14 23:37:32 -05:00
|
|
|
|
2012-09-11 23:25:01 -05:00
|
|
|
Store(bcx, base, GEPi(bcx, scratch.val, [0u, abi::slice_elt_base]));
|
2013-10-16 11:04:51 -05:00
|
|
|
Store(bcx, len, GEPi(bcx, scratch.val, [0u, abi::slice_elt_len]));
|
2012-09-11 23:25:01 -05:00
|
|
|
DatumBlock {bcx: bcx, datum: scratch}
|
|
|
|
}
|
2012-12-06 18:29:17 -06:00
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn add_env<'a>(bcx: &'a Block<'a>, expr: &ast::Expr, datum: Datum)
|
|
|
|
-> DatumBlock<'a> {
|
2013-02-27 18:28:37 -06:00
|
|
|
// This is not the most efficient thing possible; since closures
|
|
|
|
// are two words it'd be better if this were compiled in
|
|
|
|
// 'dest' mode, but I can't find a nice way to structure the
|
|
|
|
// code and keep it DRY that accommodates that use case at the
|
|
|
|
// moment.
|
|
|
|
|
|
|
|
let tcx = bcx.tcx();
|
|
|
|
let closure_ty = expr_ty_adjusted(bcx, expr);
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("add_env(closure_ty={})", closure_ty.repr(tcx));
|
2013-06-20 14:21:37 -05:00
|
|
|
let scratch = scratch_datum(bcx, closure_ty, "__adjust", false);
|
2013-02-27 18:28:37 -06:00
|
|
|
let llfn = GEPi(bcx, scratch.val, [0u, abi::fn_field_code]);
|
make small (<= size_of::<int>()) tuples immediate
fn foo() -> (u32, u8, u8, u8, u8) {
(4, 5, 6, 7, 8)
}
Before:
; Function Attrs: nounwind uwtable
define void @_ZN3foo18hbb616262f874f8daf4v0.0E({ i32, i8, i8, i8, i8 }* noalias nocapture sret, { i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #0 {
"function top level":
%2 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 0
store i32 4, i32* %2, align 4
%3 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 1
store i8 5, i8* %3, align 4
%4 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 2
store i8 6, i8* %4, align 1
%5 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 3
store i8 7, i8* %5, align 2
%6 = getelementptr inbounds { i32, i8, i8, i8, i8 }* %0, i64 0, i32 4
store i8 8, i8* %6, align 1
ret void
}
After:
; Function Attrs: nounwind readnone uwtable
define { i32, i8, i8, i8, i8 } @_ZN3foo18hbb616262f874f8daf4v0.0E({ i64, %tydesc*, i8*, i8*, i8 }* nocapture readnone) #0 {
"function top level":
ret { i32, i8, i8, i8, i8 } { i32 4, i8 5, i8 6, i8 7, i8 8 }
}
2013-09-30 17:29:42 -05:00
|
|
|
assert_eq!(datum.appropriate_mode(bcx.ccx()), ByValue);
|
2013-02-27 18:28:37 -06:00
|
|
|
Store(bcx, datum.to_appropriate_llval(bcx), llfn);
|
|
|
|
let llenv = GEPi(bcx, scratch.val, [0u, abi::fn_field_box]);
|
2013-05-21 14:25:44 -05:00
|
|
|
Store(bcx, base::null_env_ptr(bcx.ccx()), llenv);
|
2013-02-27 18:28:37 -06:00
|
|
|
DatumBlock {bcx: bcx, datum: scratch}
|
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn auto_slice_and_ref<'a>(
|
|
|
|
bcx: &'a Block<'a>,
|
2013-05-09 14:58:02 -05:00
|
|
|
autoderefs: uint,
|
2013-09-01 20:45:37 -05:00
|
|
|
expr: &ast::Expr,
|
2014-01-07 10:54:58 -06:00
|
|
|
datum: Datum)
|
|
|
|
-> DatumBlock<'a> {
|
2013-05-09 14:58:02 -05:00
|
|
|
let DatumBlock { bcx, datum } = auto_slice(bcx, autoderefs, expr, datum);
|
2012-12-06 18:29:17 -06:00
|
|
|
auto_ref(bcx, datum)
|
|
|
|
}
|
2013-08-11 12:42:26 -05:00
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn auto_borrow_obj<'a>(
|
|
|
|
mut bcx: &'a Block<'a>,
|
2013-08-11 12:42:26 -05:00
|
|
|
autoderefs: uint,
|
2013-09-30 12:37:17 -05:00
|
|
|
expr: &ast::Expr,
|
2014-01-07 10:54:58 -06:00
|
|
|
source_datum: Datum)
|
|
|
|
-> DatumBlock<'a> {
|
2013-08-11 12:42:26 -05:00
|
|
|
let tcx = bcx.tcx();
|
|
|
|
let target_obj_ty = expr_ty_adjusted(bcx, expr);
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("auto_borrow_obj(target={})",
|
2013-08-11 12:42:26 -05:00
|
|
|
target_obj_ty.repr(tcx));
|
2013-09-17 04:33:36 -05:00
|
|
|
|
|
|
|
// Extract source store information
|
|
|
|
let (source_store, source_mutbl) = match ty::get(source_datum.ty).sty {
|
|
|
|
ty::ty_trait(_, _, s, m, _) => (s, m),
|
|
|
|
_ => {
|
|
|
|
bcx.sess().span_bug(
|
|
|
|
expr.span,
|
2013-09-28 00:38:08 -05:00
|
|
|
format!("auto_borrow_trait_obj expected a trait, found {}",
|
2013-09-17 04:33:36 -05:00
|
|
|
source_datum.ty.repr(bcx.tcx())));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// check if any borrowing is really needed or we could reuse the source_datum instead
|
|
|
|
match ty::get(target_obj_ty).sty {
|
|
|
|
ty::ty_trait(_, _, ty::RegionTraitStore(target_scope), target_mutbl, _) => {
|
|
|
|
if target_mutbl == ast::MutImmutable && target_mutbl == source_mutbl {
|
|
|
|
match source_store {
|
|
|
|
ty::RegionTraitStore(source_scope) => {
|
|
|
|
if tcx.region_maps.is_subregion_of(target_scope, source_scope) {
|
|
|
|
return DatumBlock { bcx: bcx, datum: source_datum };
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {}
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2013-08-11 12:42:26 -05:00
|
|
|
let scratch = scratch_datum(bcx, target_obj_ty,
|
|
|
|
"__auto_borrow_obj", false);
|
|
|
|
|
|
|
|
// Convert a @Object, ~Object, or &Object pair into an &Object pair.
|
|
|
|
|
|
|
|
// Get a pointer to the source object, which is represented as
|
|
|
|
// a (vtable, data) pair.
|
|
|
|
let source_llval = source_datum.to_ref_llval(bcx);
|
|
|
|
|
|
|
|
// Set the vtable field of the new pair
|
|
|
|
let vtable_ptr = GEPi(bcx, source_llval, [0u, abi::trt_field_vtable]);
|
|
|
|
let vtable = Load(bcx, vtable_ptr);
|
|
|
|
Store(bcx, vtable, GEPi(bcx, scratch.val, [0u, abi::trt_field_vtable]));
|
|
|
|
|
|
|
|
// Load the data for the source, which is either an @T,
|
|
|
|
// ~T, or &T, depending on source_obj_ty.
|
|
|
|
let source_data_ptr = GEPi(bcx, source_llval, [0u, abi::trt_field_box]);
|
|
|
|
let source_data = Load(bcx, source_data_ptr); // always a ptr
|
|
|
|
let target_data = match source_store {
|
2013-11-28 14:22:53 -06:00
|
|
|
ty::BoxTraitStore(..) => {
|
2013-12-30 20:57:48 -06:00
|
|
|
// For deref of @T, create a dummy datum and use the datum's
|
|
|
|
// deref method. This is more work than just calling GEPi
|
|
|
|
// ourselves. Note that we don't know the type T, so
|
2013-08-11 12:42:26 -05:00
|
|
|
// just substitute `i8`-- it doesn't really matter for
|
|
|
|
// our purposes right now.
|
2013-12-30 20:57:48 -06:00
|
|
|
let source_ty = ty::mk_box(tcx, ty::mk_i8());
|
2013-08-11 12:42:26 -05:00
|
|
|
let source_datum =
|
|
|
|
Datum {val: source_data,
|
|
|
|
ty: source_ty,
|
|
|
|
mode: ByValue};
|
|
|
|
let derefd_datum =
|
|
|
|
unpack_datum!(bcx,
|
|
|
|
source_datum.deref(bcx,
|
|
|
|
expr,
|
|
|
|
autoderefs));
|
|
|
|
derefd_datum.to_rptr(bcx).to_value_llval(bcx)
|
|
|
|
}
|
2013-11-28 14:22:53 -06:00
|
|
|
ty::UniqTraitStore(..) => {
|
2013-08-11 12:42:26 -05:00
|
|
|
// For a ~T box, there may or may not be a header,
|
|
|
|
// depending on whether the type T references managed
|
|
|
|
// boxes. However, since we do not *know* the type T
|
|
|
|
// for objects, this presents a hurdle. Our solution is
|
|
|
|
// to load the "borrow offset" from the type descriptor;
|
|
|
|
// this value will either be 0 or sizeof(BoxHeader), depending
|
|
|
|
// on the type T.
|
|
|
|
let llopaque =
|
|
|
|
PointerCast(bcx, source_data, Type::opaque().ptr_to());
|
|
|
|
let lltydesc_ptr_ptr =
|
|
|
|
PointerCast(bcx, vtable,
|
|
|
|
bcx.ccx().tydesc_type.ptr_to().ptr_to());
|
|
|
|
let lltydesc_ptr =
|
|
|
|
Load(bcx, lltydesc_ptr_ptr);
|
|
|
|
let borrow_offset_ptr =
|
|
|
|
GEPi(bcx, lltydesc_ptr,
|
|
|
|
[0, abi::tydesc_field_borrow_offset]);
|
|
|
|
let borrow_offset =
|
|
|
|
Load(bcx, borrow_offset_ptr);
|
|
|
|
InBoundsGEP(bcx, llopaque, [borrow_offset])
|
|
|
|
}
|
2013-11-28 14:22:53 -06:00
|
|
|
ty::RegionTraitStore(..) => {
|
2013-08-11 12:42:26 -05:00
|
|
|
source_data
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Store(bcx, target_data,
|
|
|
|
GEPi(bcx, scratch.val, [0u, abi::trt_field_box]));
|
|
|
|
|
|
|
|
DatumBlock { bcx: bcx, datum: scratch }
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
pub fn trans_into<'a>(bcx: &'a Block<'a>, expr: &ast::Expr, dest: Dest)
|
|
|
|
-> &'a Block<'a> {
|
2013-12-19 20:26:45 -06:00
|
|
|
let adjustment_found = {
|
|
|
|
let adjustments = bcx.tcx().adjustments.borrow();
|
|
|
|
adjustments.get().contains_key(&expr.id)
|
|
|
|
};
|
|
|
|
if adjustment_found {
|
2013-01-10 12:59:58 -06:00
|
|
|
// use trans_to_datum, which is mildly less efficient but
|
|
|
|
// which will perform the adjustments:
|
|
|
|
let datumblock = trans_to_datum(bcx, expr);
|
|
|
|
return match dest {
|
|
|
|
Ignore => datumblock.bcx,
|
2013-06-20 14:23:52 -05:00
|
|
|
SaveIn(lldest) => datumblock.store_to(INIT, lldest)
|
2013-01-10 12:59:58 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-12-26 12:54:41 -06:00
|
|
|
trans_into_unadjusted(bcx, expr, dest)
|
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
pub fn trans_into_unadjusted<'a>(
|
|
|
|
bcx: &'a Block<'a>,
|
|
|
|
expr: &ast::Expr,
|
|
|
|
dest: Dest)
|
|
|
|
-> &'a Block<'a> {
|
2013-01-10 12:59:58 -06:00
|
|
|
let ty = expr_ty(bcx, expr);
|
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("trans_into_unadjusted(expr={}, dest={})",
|
2013-01-10 12:59:58 -06:00
|
|
|
bcx.expr_to_str(expr),
|
|
|
|
dest.to_str(bcx.ccx()));
|
|
|
|
let _indenter = indenter();
|
|
|
|
|
2013-08-19 11:23:43 -05:00
|
|
|
debuginfo::set_source_location(bcx.fcx, expr.id, expr.span);
|
2013-01-10 12:59:58 -06:00
|
|
|
|
|
|
|
let dest = {
|
2013-10-02 08:55:42 -05:00
|
|
|
if ty::type_is_voidish(bcx.tcx(), ty) {
|
2013-01-10 12:59:58 -06:00
|
|
|
Ignore
|
|
|
|
} else {
|
|
|
|
dest
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let kind = bcx.expr_kind(expr);
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("expr kind = {:?}", kind);
|
2013-01-10 12:59:58 -06:00
|
|
|
return match kind {
|
|
|
|
ty::LvalueExpr => {
|
|
|
|
let datumblock = trans_lvalue_unadjusted(bcx, expr);
|
2012-09-11 23:25:01 -05:00
|
|
|
match dest {
|
|
|
|
Ignore => datumblock.bcx,
|
2013-06-20 14:23:52 -05:00
|
|
|
SaveIn(lldest) => datumblock.store_to(INIT, lldest)
|
2012-09-11 23:25:01 -05:00
|
|
|
}
|
|
|
|
}
|
2013-01-10 12:59:58 -06:00
|
|
|
ty::RvalueDatumExpr => {
|
|
|
|
let datumblock = trans_rvalue_datum_unadjusted(bcx, expr);
|
|
|
|
match dest {
|
|
|
|
Ignore => datumblock.drop_val(),
|
|
|
|
|
2013-06-20 14:23:52 -05:00
|
|
|
// When processing an rvalue, the value will be newly
|
|
|
|
// allocated, so we always `move_to` so as not to
|
|
|
|
// unnecessarily inc ref counts and so forth:
|
2013-01-10 12:59:58 -06:00
|
|
|
SaveIn(lldest) => datumblock.move_to(INIT, lldest)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ty::RvalueDpsExpr => {
|
|
|
|
trans_rvalue_dps_unadjusted(bcx, expr, dest)
|
|
|
|
}
|
|
|
|
ty::RvalueStmtExpr => {
|
|
|
|
trans_rvalue_stmt_unadjusted(bcx, expr)
|
|
|
|
}
|
|
|
|
};
|
2012-09-11 23:25:01 -05:00
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn trans_lvalue<'a>(bcx: &'a Block<'a>, expr: &ast::Expr) -> DatumBlock<'a> {
|
2013-01-10 12:59:58 -06:00
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* Translates an lvalue expression, always yielding a by-ref
|
|
|
|
* datum. Generally speaking you should call trans_to_datum()
|
|
|
|
* instead, but sometimes we call trans_lvalue() directly as a
|
|
|
|
* means of asserting that a particular expression is an lvalue. */
|
|
|
|
|
2013-12-19 20:26:45 -06:00
|
|
|
let adjustment_opt = {
|
|
|
|
let adjustments = bcx.tcx().adjustments.borrow();
|
|
|
|
adjustments.get().find_copy(&expr.id)
|
|
|
|
};
|
|
|
|
match adjustment_opt {
|
2012-09-11 23:25:01 -05:00
|
|
|
None => trans_lvalue_unadjusted(bcx, expr),
|
|
|
|
Some(_) => {
|
|
|
|
bcx.sess().span_bug(
|
|
|
|
expr.span,
|
2013-09-28 00:38:08 -05:00
|
|
|
format!("trans_lvalue() called on an expression \
|
2012-09-11 23:25:01 -05:00
|
|
|
with adjustments"));
|
|
|
|
}
|
2013-12-19 20:26:45 -06:00
|
|
|
}
|
2012-09-11 23:25:01 -05:00
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn trans_to_datum_unadjusted<'a>(bcx: &'a Block<'a>, expr: &ast::Expr)
|
|
|
|
-> DatumBlock<'a> {
|
2012-08-28 17:54:45 -05:00
|
|
|
/*!
|
|
|
|
* Translates an expression into a datum. If this expression
|
|
|
|
* is an rvalue, this will result in a temporary value being
|
2013-06-20 14:23:52 -05:00
|
|
|
* created. If you plan to store the value somewhere else,
|
|
|
|
* you should prefer `trans_into()` instead.
|
|
|
|
*/
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
let mut bcx = bcx;
|
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("trans_to_datum_unadjusted(expr={})", bcx.expr_to_str(expr));
|
2012-08-28 17:54:45 -05:00
|
|
|
let _indenter = indenter();
|
|
|
|
|
2013-08-19 11:23:43 -05:00
|
|
|
debuginfo::set_source_location(bcx.fcx, expr.id, expr.span);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
match ty::expr_kind(bcx.tcx(), bcx.ccx().maps.method_map, expr) {
|
|
|
|
ty::LvalueExpr => {
|
2012-09-11 23:25:01 -05:00
|
|
|
return trans_lvalue_unadjusted(bcx, expr);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::RvalueDatumExpr => {
|
2012-09-11 23:25:01 -05:00
|
|
|
let datum = unpack_datum!(bcx, {
|
|
|
|
trans_rvalue_datum_unadjusted(bcx, expr)
|
|
|
|
});
|
2012-08-28 17:54:45 -05:00
|
|
|
datum.add_clean(bcx);
|
|
|
|
return DatumBlock {bcx: bcx, datum: datum};
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::RvalueStmtExpr => {
|
2012-09-11 23:25:01 -05:00
|
|
|
bcx = trans_rvalue_stmt_unadjusted(bcx, expr);
|
2012-08-28 17:54:45 -05:00
|
|
|
return nil(bcx, expr_ty(bcx, expr));
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::RvalueDpsExpr => {
|
|
|
|
let ty = expr_ty(bcx, expr);
|
2013-10-02 08:55:42 -05:00
|
|
|
if ty::type_is_voidish(bcx.tcx(), ty) {
|
2012-09-11 23:25:01 -05:00
|
|
|
bcx = trans_rvalue_dps_unadjusted(bcx, expr, Ignore);
|
2012-08-28 17:54:45 -05:00
|
|
|
return nil(bcx, ty);
|
|
|
|
} else {
|
2013-06-20 14:21:37 -05:00
|
|
|
let scratch = scratch_datum(bcx, ty, "", false);
|
2012-09-11 23:25:01 -05:00
|
|
|
bcx = trans_rvalue_dps_unadjusted(
|
|
|
|
bcx, expr, SaveIn(scratch.val));
|
2012-09-06 17:21:42 -05:00
|
|
|
|
|
|
|
// Note: this is not obviously a good idea. It causes
|
|
|
|
// immediate values to be loaded immediately after a
|
|
|
|
// return from a call or other similar expression,
|
|
|
|
// which in turn leads to alloca's having shorter
|
|
|
|
// lifetimes and hence larger stack frames. However,
|
|
|
|
// in turn it can lead to more register pressure.
|
|
|
|
// Still, in practice it seems to increase
|
|
|
|
// performance, since we have fewer problems with
|
|
|
|
// morestack churn.
|
|
|
|
let scratch = scratch.to_appropriate_datum(bcx);
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
scratch.add_clean(bcx);
|
|
|
|
return DatumBlock {bcx: bcx, datum: scratch};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn nil<'a>(bcx: &'a Block<'a>, ty: ty::t) -> DatumBlock<'a> {
|
2012-08-28 17:54:45 -05:00
|
|
|
let datum = immediate_rvalue(C_nil(), ty);
|
2014-01-07 10:54:58 -06:00
|
|
|
DatumBlock {
|
|
|
|
bcx: bcx,
|
|
|
|
datum: datum,
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn trans_rvalue_datum_unadjusted<'a>(bcx: &'a Block<'a>, expr: &ast::Expr)
|
|
|
|
-> DatumBlock<'a> {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_rvalue_datum_unadjusted");
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
match expr.node {
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprPath(_) | ast::ExprSelf => {
|
2013-02-27 18:28:37 -06:00
|
|
|
return trans_def_datum_unadjusted(bcx, expr, bcx.def(expr.id));
|
|
|
|
}
|
2013-12-31 14:55:39 -06:00
|
|
|
ast::ExprVstore(contents, ast::ExprVstoreBox) => {
|
2013-02-20 17:02:21 -06:00
|
|
|
return tvec::trans_uniq_or_managed_vstore(bcx, heap_managed,
|
2012-08-28 17:54:45 -05:00
|
|
|
expr, contents);
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprVstore(contents, ast::ExprVstoreUniq) => {
|
2013-07-15 13:23:42 -05:00
|
|
|
let heap = heap_for_unique(bcx, expr_ty(bcx, contents));
|
2013-02-20 17:02:21 -06:00
|
|
|
return tvec::trans_uniq_or_managed_vstore(bcx, heap,
|
2012-08-28 17:54:45 -05:00
|
|
|
expr, contents);
|
|
|
|
}
|
2013-12-17 18:46:18 -06:00
|
|
|
ast::ExprBox(_, contents) => {
|
|
|
|
// Special case for `~T`. (The other case, for GC, is handled in
|
|
|
|
// `trans_rvalue_dps_unadjusted`.)
|
|
|
|
let box_ty = expr_ty(bcx, expr);
|
|
|
|
let contents_ty = expr_ty(bcx, contents);
|
|
|
|
let heap = heap_for_unique(bcx, contents_ty);
|
|
|
|
return trans_boxed_expr(bcx, box_ty, contents, contents_ty, heap)
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprLit(lit) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
return trans_immediate_lit(bcx, expr, *lit);
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprBinary(_, op, lhs, rhs) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
// if overloaded, would be RvalueDpsExpr
|
2013-12-21 19:04:42 -06:00
|
|
|
{
|
|
|
|
let method_map = bcx.ccx().maps.method_map.borrow();
|
|
|
|
assert!(!method_map.get().contains_key(&expr.id));
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
return trans_binary(bcx, expr, op, lhs, rhs);
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprUnary(_, op, x) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
return trans_unary_datum(bcx, expr, op, x);
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprAddrOf(_, x) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
return trans_addr_of(bcx, expr, x);
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprCast(val, _) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
return trans_imm_cast(bcx, val, expr.id);
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprParen(e) => {
|
2012-10-27 19:14:09 -05:00
|
|
|
return trans_rvalue_datum_unadjusted(bcx, e);
|
|
|
|
}
|
2013-08-28 01:12:05 -05:00
|
|
|
ast::ExprLogLevel => {
|
|
|
|
return trans_log_level(bcx);
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
_ => {
|
|
|
|
bcx.tcx().sess.span_bug(
|
|
|
|
expr.span,
|
2013-09-28 00:38:08 -05:00
|
|
|
format!("trans_rvalue_datum_unadjusted reached \
|
|
|
|
fall-through case: {:?}",
|
2012-08-28 17:54:45 -05:00
|
|
|
expr.node));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn trans_rvalue_stmt_unadjusted<'a>(bcx: &'a Block<'a>, expr: &ast::Expr)
|
|
|
|
-> &'a Block<'a> {
|
2012-08-28 17:54:45 -05:00
|
|
|
let mut bcx = bcx;
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_rvalue_stmt");
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-12-18 16:54:42 -06:00
|
|
|
if bcx.unreachable.get() {
|
2013-05-21 15:15:48 -05:00
|
|
|
return bcx;
|
|
|
|
}
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
match expr.node {
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprBreak(label_opt) => {
|
2012-10-18 14:20:18 -05:00
|
|
|
return controlflow::trans_break(bcx, label_opt);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprAgain(label_opt) => {
|
2012-10-18 14:20:18 -05:00
|
|
|
return controlflow::trans_cont(bcx, label_opt);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprRet(ex) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
return controlflow::trans_ret(bcx, ex);
|
|
|
|
}
|
2013-11-30 16:00:39 -06:00
|
|
|
ast::ExprWhile(cond, body) => {
|
2013-01-31 19:12:29 -06:00
|
|
|
return controlflow::trans_while(bcx, cond, body);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-11-30 16:00:39 -06:00
|
|
|
ast::ExprLoop(body, opt_label) => {
|
2013-09-10 14:01:44 -05:00
|
|
|
// FIXME #6993: map can go away when ast.rs is changed
|
|
|
|
return controlflow::trans_loop(bcx, body, opt_label.map(|x| x.name));
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprAssign(dst, src) => {
|
2013-01-10 12:59:58 -06:00
|
|
|
let src_datum = unpack_datum!(
|
|
|
|
bcx, trans_to_datum(bcx, src));
|
|
|
|
let dst_datum = unpack_datum!(
|
|
|
|
bcx, trans_lvalue(bcx, dst));
|
|
|
|
return src_datum.store_to_datum(
|
2013-06-20 14:23:52 -05:00
|
|
|
bcx, DROP_EXISTING, dst_datum);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprAssignOp(callee_id, op, dst, src) => {
|
2013-06-01 17:31:56 -05:00
|
|
|
return trans_assign_op(bcx, expr, callee_id, op, dst, src);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprParen(a) => {
|
2012-10-27 19:14:09 -05:00
|
|
|
return trans_rvalue_stmt_unadjusted(bcx, a);
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprInlineAsm(ref a) => {
|
2013-03-27 15:42:21 -05:00
|
|
|
return asm::trans_inline_asm(bcx, a);
|
2013-03-12 19:53:25 -05:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
_ => {
|
|
|
|
bcx.tcx().sess.span_bug(
|
|
|
|
expr.span,
|
2013-09-28 00:38:08 -05:00
|
|
|
format!("trans_rvalue_stmt_unadjusted reached \
|
|
|
|
fall-through case: {:?}",
|
2012-08-28 17:54:45 -05:00
|
|
|
expr.node));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn trans_rvalue_dps_unadjusted<'a>(
|
|
|
|
bcx: &'a Block<'a>,
|
|
|
|
expr: &ast::Expr,
|
|
|
|
dest: Dest)
|
|
|
|
-> &'a Block<'a> {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_rvalue_dps_unadjusted");
|
2012-08-28 17:54:45 -05:00
|
|
|
let tcx = bcx.tcx();
|
|
|
|
|
2013-01-10 12:59:58 -06:00
|
|
|
match expr.node {
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprParen(e) => {
|
2012-10-27 19:14:09 -05:00
|
|
|
return trans_rvalue_dps_unadjusted(bcx, e, dest);
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprPath(_) | ast::ExprSelf => {
|
2012-09-11 23:25:01 -05:00
|
|
|
return trans_def_dps_unadjusted(bcx, expr,
|
|
|
|
bcx.def(expr.id), dest);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-11-30 16:00:39 -06:00
|
|
|
ast::ExprIf(cond, thn, els) => {
|
2013-01-31 19:12:29 -06:00
|
|
|
return controlflow::trans_if(bcx, cond, thn, els, dest);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprMatch(discr, ref arms) => {
|
2013-07-02 14:47:32 -05:00
|
|
|
return _match::trans_match(bcx, expr, discr, *arms, dest);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-11-30 16:00:39 -06:00
|
|
|
ast::ExprBlock(blk) => {
|
2013-11-21 17:42:55 -06:00
|
|
|
return base::with_scope(bcx,
|
|
|
|
blk.info(),
|
|
|
|
"block-expr body",
|
|
|
|
|bcx| {
|
2013-01-31 19:12:29 -06:00
|
|
|
controlflow::trans_block(bcx, blk, dest)
|
2013-11-21 17:42:55 -06:00
|
|
|
});
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprStruct(_, ref fields, base) => {
|
2013-05-17 19:27:44 -05:00
|
|
|
return trans_rec_or_struct(bcx, (*fields), base, expr.span, expr.id, dest);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprTup(ref args) => {
|
2013-02-23 15:36:01 -06:00
|
|
|
let repr = adt::represent_type(bcx.ccx(), expr_ty(bcx, expr));
|
2013-09-01 20:45:37 -05:00
|
|
|
let numbered_fields: ~[(uint, @ast::Expr)] =
|
2013-08-09 22:09:47 -05:00
|
|
|
args.iter().enumerate().map(|(i, arg)| (i, *arg)).collect();
|
2013-06-29 00:05:50 -05:00
|
|
|
return trans_adt(bcx, repr, 0, numbered_fields, None, dest);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2014-01-09 07:05:33 -06:00
|
|
|
ast::ExprLit(@codemap::Spanned {node: ast::LitStr(s, _), ..}) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
return tvec::trans_lit_str(bcx, expr, s, dest);
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprVstore(contents, ast::ExprVstoreSlice) |
|
|
|
|
ast::ExprVstore(contents, ast::ExprVstoreMutSlice) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
return tvec::trans_slice_vstore(bcx, expr, contents, dest);
|
|
|
|
}
|
2013-11-28 14:22:53 -06:00
|
|
|
ast::ExprVec(..) | ast::ExprRepeat(..) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
return tvec::trans_fixed_vstore(bcx, expr, expr, dest);
|
|
|
|
}
|
2013-11-30 16:00:39 -06:00
|
|
|
ast::ExprFnBlock(decl, body) |
|
|
|
|
ast::ExprProc(decl, body) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
let expr_ty = expr_ty(bcx, expr);
|
2013-01-31 19:12:29 -06:00
|
|
|
let sigil = ty::ty_closure_sigil(expr_ty);
|
2013-10-28 17:22:49 -05:00
|
|
|
debug!("translating block function {} with type {}",
|
2013-01-31 19:12:29 -06:00
|
|
|
expr_to_str(expr, tcx.sess.intr()),
|
2013-03-15 14:24:24 -05:00
|
|
|
expr_ty.repr(tcx));
|
2013-01-31 19:12:29 -06:00
|
|
|
return closure::trans_expr_fn(bcx, sigil, decl, body,
|
2013-08-11 20:12:57 -05:00
|
|
|
expr.id, expr.id, dest);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprDoBody(blk) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
return trans_into(bcx, blk, dest);
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprCall(f, ref args, _) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
return callee::trans_call(
|
2013-01-10 12:59:58 -06:00
|
|
|
bcx, expr, f, callee::ArgExprs(*args), expr.id, dest);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprMethodCall(callee_id, rcvr, _, _, ref args, _) => {
|
2012-11-30 13:18:25 -06:00
|
|
|
return callee::trans_method_call(bcx,
|
|
|
|
expr,
|
2013-06-01 17:31:56 -05:00
|
|
|
callee_id,
|
2012-11-30 13:18:25 -06:00
|
|
|
rcvr,
|
2013-01-10 12:59:58 -06:00
|
|
|
callee::ArgExprs(*args),
|
2012-11-30 13:18:25 -06:00
|
|
|
dest);
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprBinary(callee_id, _, lhs, rhs) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
// if not overloaded, would be RvalueDatumExpr
|
2013-04-18 17:53:29 -05:00
|
|
|
return trans_overloaded_op(bcx,
|
|
|
|
expr,
|
2013-06-01 17:31:56 -05:00
|
|
|
callee_id,
|
2013-04-18 17:53:29 -05:00
|
|
|
lhs,
|
|
|
|
~[rhs],
|
|
|
|
expr_ty(bcx, expr),
|
|
|
|
dest);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprUnary(callee_id, _, subexpr) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
// if not overloaded, would be RvalueDatumExpr
|
2013-04-18 17:53:29 -05:00
|
|
|
return trans_overloaded_op(bcx,
|
|
|
|
expr,
|
2013-06-01 17:31:56 -05:00
|
|
|
callee_id,
|
2013-04-18 17:53:29 -05:00
|
|
|
subexpr,
|
|
|
|
~[],
|
|
|
|
expr_ty(bcx, expr),
|
|
|
|
dest);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprIndex(callee_id, base, idx) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
// if not overloaded, would be RvalueDatumExpr
|
2013-04-18 17:53:29 -05:00
|
|
|
return trans_overloaded_op(bcx,
|
|
|
|
expr,
|
2013-06-01 17:31:56 -05:00
|
|
|
callee_id,
|
2013-04-18 17:53:29 -05:00
|
|
|
base,
|
|
|
|
~[idx],
|
|
|
|
expr_ty(bcx, expr),
|
|
|
|
dest);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprCast(val, _) => {
|
2012-10-31 17:09:26 -05:00
|
|
|
match ty::get(node_id_type(bcx, expr.id)).sty {
|
2013-06-17 14:16:30 -05:00
|
|
|
ty::ty_trait(_, _, store, _, _) => {
|
2013-12-26 12:54:41 -06:00
|
|
|
return meth::trans_trait_cast(bcx, val, expr.id,
|
|
|
|
dest, store, true /* adjustments */);
|
2012-10-31 17:09:26 -05:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
bcx.tcx().sess.span_bug(expr.span,
|
2013-05-02 11:28:53 -05:00
|
|
|
"expr_cast of non-trait");
|
2012-10-31 17:09:26 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprAssignOp(callee_id, op, dst, src) => {
|
2013-06-01 17:31:56 -05:00
|
|
|
return trans_assign_op(bcx, expr, callee_id, op, dst, src);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-12-17 18:46:18 -06:00
|
|
|
ast::ExprBox(_, contents) => {
|
|
|
|
// Special case for `Gc<T>` for now. The other case, for unique
|
|
|
|
// pointers, is handled in `trans_rvalue_datum_unadjusted`.
|
|
|
|
return trans_gc(bcx, expr, contents, dest)
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
_ => {
|
|
|
|
bcx.tcx().sess.span_bug(
|
|
|
|
expr.span,
|
2013-09-28 00:38:08 -05:00
|
|
|
format!("trans_rvalue_dps_unadjusted reached fall-through case: {:?}",
|
2012-08-28 17:54:45 -05:00
|
|
|
expr.node));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn trans_def_dps_unadjusted<'a>(
|
|
|
|
bcx: &'a Block<'a>,
|
|
|
|
ref_expr: &ast::Expr,
|
|
|
|
def: ast::Def,
|
|
|
|
dest: Dest)
|
|
|
|
-> &'a Block<'a> {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_def_dps_unadjusted");
|
2012-08-28 17:54:45 -05:00
|
|
|
let ccx = bcx.ccx();
|
|
|
|
|
|
|
|
let lldest = match dest {
|
|
|
|
SaveIn(lldest) => lldest,
|
|
|
|
Ignore => { return bcx; }
|
|
|
|
};
|
|
|
|
|
|
|
|
match def {
|
2013-09-08 19:36:01 -05:00
|
|
|
ast::DefVariant(tid, vid, _) => {
|
2013-02-06 17:08:33 -06:00
|
|
|
let variant_info = ty::enum_variant_with_id(ccx.tcx, tid, vid);
|
|
|
|
if variant_info.args.len() > 0u {
|
2012-08-28 17:54:45 -05:00
|
|
|
// N-ary variant.
|
|
|
|
let fn_data = callee::trans_fn_ref(bcx, vid, ref_expr.id);
|
2013-02-27 18:28:37 -06:00
|
|
|
Store(bcx, fn_data.llfn, lldest);
|
|
|
|
return bcx;
|
2013-01-07 05:42:49 -06:00
|
|
|
} else {
|
2013-02-24 13:08:30 -06:00
|
|
|
// Nullary variant.
|
|
|
|
let ty = expr_ty(bcx, ref_expr);
|
|
|
|
let repr = adt::represent_type(ccx, ty);
|
2013-03-02 18:08:49 -06:00
|
|
|
adt::trans_start_init(bcx, repr, lldest,
|
|
|
|
variant_info.disr_val);
|
2013-01-07 05:42:49 -06:00
|
|
|
return bcx;
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::DefStruct(def_id) => {
|
2013-06-01 16:39:39 -05:00
|
|
|
let ty = expr_ty(bcx, ref_expr);
|
|
|
|
match ty::get(ty).sty {
|
|
|
|
ty::ty_struct(did, _) if ty::has_dtor(ccx.tcx, did) => {
|
|
|
|
let repr = adt::represent_type(ccx, ty);
|
|
|
|
adt::trans_start_init(bcx, repr, lldest, 0);
|
|
|
|
}
|
2013-11-28 14:22:53 -06:00
|
|
|
ty::ty_bare_fn(..) => {
|
2013-08-17 15:42:31 -05:00
|
|
|
let fn_data = callee::trans_fn_ref(bcx, def_id, ref_expr.id);
|
|
|
|
Store(bcx, fn_data.llfn, lldest);
|
|
|
|
}
|
|
|
|
_ => ()
|
2013-06-01 16:39:39 -05:00
|
|
|
}
|
2012-10-30 17:53:06 -05:00
|
|
|
return bcx;
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
_ => {
|
2013-09-28 00:38:08 -05:00
|
|
|
bcx.tcx().sess.span_bug(ref_expr.span, format!(
|
|
|
|
"Non-DPS def {:?} referened by {}",
|
2012-08-28 17:54:45 -05:00
|
|
|
def, bcx.node_id_to_str(ref_expr.id)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn trans_def_datum_unadjusted<'a>(
|
|
|
|
bcx: &'a Block<'a>,
|
2013-09-01 20:45:37 -05:00
|
|
|
ref_expr: &ast::Expr,
|
2014-01-07 10:54:58 -06:00
|
|
|
def: ast::Def)
|
|
|
|
-> DatumBlock<'a> {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_def_datum_unadjusted");
|
2013-02-27 18:28:37 -06:00
|
|
|
|
2013-08-27 20:45:13 -05:00
|
|
|
let fn_data = match def {
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::DefFn(did, _) |
|
|
|
|
ast::DefStaticMethod(did, ast::FromImpl(_), _) => {
|
2013-08-21 08:27:48 -05:00
|
|
|
callee::trans_fn_ref(bcx, did, ref_expr.id)
|
2013-02-27 18:28:37 -06:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::DefStaticMethod(impl_did, ast::FromTrait(trait_did), _) => {
|
2013-08-27 20:45:13 -05:00
|
|
|
meth::trans_static_method_callee(bcx,
|
|
|
|
impl_did,
|
|
|
|
trait_did,
|
|
|
|
ref_expr.id)
|
2013-02-27 18:28:37 -06:00
|
|
|
}
|
|
|
|
_ => {
|
2013-09-28 00:38:08 -05:00
|
|
|
bcx.tcx().sess.span_bug(ref_expr.span, format!(
|
|
|
|
"Non-DPS def {:?} referened by {}",
|
2013-02-27 18:28:37 -06:00
|
|
|
def, bcx.node_id_to_str(ref_expr.id)));
|
|
|
|
}
|
2013-08-21 08:27:48 -05:00
|
|
|
};
|
2013-02-27 18:28:37 -06:00
|
|
|
|
2013-08-21 08:27:48 -05:00
|
|
|
let fn_ty = expr_ty(bcx, ref_expr);
|
|
|
|
DatumBlock {
|
|
|
|
bcx: bcx,
|
|
|
|
datum: Datum {
|
|
|
|
val: fn_data.llfn,
|
|
|
|
ty: fn_ty,
|
|
|
|
mode: ByValue
|
|
|
|
}
|
2013-02-27 18:28:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn trans_lvalue_unadjusted<'a>(bcx: &'a Block<'a>, expr: &ast::Expr)
|
|
|
|
-> DatumBlock<'a> {
|
2012-09-11 23:25:01 -05:00
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* Translates an lvalue expression, always yielding a by-ref
|
2013-01-10 12:59:58 -06:00
|
|
|
* datum. Does not apply any adjustments. */
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_lval");
|
2012-08-28 17:54:45 -05:00
|
|
|
let mut bcx = bcx;
|
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("trans_lvalue(expr={})", bcx.expr_to_str(expr));
|
2012-08-28 17:54:45 -05:00
|
|
|
let _indenter = indenter();
|
|
|
|
|
2013-05-01 19:22:08 -05:00
|
|
|
return match expr.node {
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprParen(e) => {
|
2013-05-01 20:50:09 -05:00
|
|
|
trans_lvalue_unadjusted(bcx, e)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprPath(_) | ast::ExprSelf => {
|
2013-05-01 19:22:08 -05:00
|
|
|
trans_def_lvalue(bcx, expr, bcx.def(expr.id))
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprField(base, ident, _) => {
|
2013-05-01 19:22:08 -05:00
|
|
|
trans_rec_field(bcx, base, ident)
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprIndex(_, base, idx) => {
|
2013-05-01 19:22:08 -05:00
|
|
|
trans_index(bcx, expr, base, idx)
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprUnary(_, ast::UnDeref, base) => {
|
2013-05-01 19:22:08 -05:00
|
|
|
let basedatum = unpack_datum!(bcx, trans_to_datum(bcx, base));
|
2013-05-03 11:11:15 -05:00
|
|
|
basedatum.deref(bcx, expr, 0)
|
2013-05-01 19:22:08 -05:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
bcx.tcx().sess.span_bug(
|
|
|
|
expr.span,
|
2013-09-28 00:38:08 -05:00
|
|
|
format!("trans_lvalue reached fall-through case: {:?}",
|
2013-05-01 19:22:08 -05:00
|
|
|
expr.node));
|
|
|
|
}
|
|
|
|
};
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn trans_rec_field<'a>(
|
|
|
|
bcx: &'a Block<'a>,
|
2013-09-30 12:37:17 -05:00
|
|
|
base: &ast::Expr,
|
2014-01-07 10:54:58 -06:00
|
|
|
field: ast::Ident)
|
|
|
|
-> DatumBlock<'a> {
|
2013-05-01 20:50:09 -05:00
|
|
|
//! Translates `base.field`.
|
2013-01-10 12:59:58 -06:00
|
|
|
|
|
|
|
let mut bcx = bcx;
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_rec_field");
|
2013-01-10 12:59:58 -06:00
|
|
|
|
|
|
|
let base_datum = unpack_datum!(bcx, trans_to_datum(bcx, base));
|
2013-02-24 14:42:39 -06:00
|
|
|
let repr = adt::represent_type(bcx.ccx(), base_datum.ty);
|
2013-11-21 17:42:55 -06:00
|
|
|
with_field_tys(bcx.tcx(), base_datum.ty, None, |discr, field_tys| {
|
2013-06-26 17:56:13 -05:00
|
|
|
let ix = ty::field_idx_strict(bcx.tcx(), field.name, field_tys);
|
2012-08-28 17:54:45 -05:00
|
|
|
DatumBlock {
|
2013-11-21 17:42:55 -06:00
|
|
|
datum: base_datum.get_element(bcx,
|
|
|
|
field_tys[ix].mt.ty,
|
|
|
|
ZeroMem,
|
|
|
|
|srcval| {
|
2013-03-02 18:08:49 -06:00
|
|
|
adt::trans_field_ptr(bcx, repr, srcval, discr, ix)
|
2013-11-21 17:42:55 -06:00
|
|
|
}),
|
2013-01-10 12:59:58 -06:00
|
|
|
bcx: bcx
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-11-21 17:42:55 -06:00
|
|
|
})
|
2013-01-10 12:59:58 -06:00
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn trans_index<'a>(
|
|
|
|
bcx: &'a Block<'a>,
|
2013-09-01 20:45:37 -05:00
|
|
|
index_expr: &ast::Expr,
|
2013-09-30 12:37:17 -05:00
|
|
|
base: &ast::Expr,
|
2014-01-07 10:54:58 -06:00
|
|
|
idx: &ast::Expr)
|
|
|
|
-> DatumBlock<'a> {
|
2013-05-01 20:50:09 -05:00
|
|
|
//! Translates `base[idx]`.
|
2013-01-10 12:59:58 -06:00
|
|
|
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_index");
|
2013-01-10 12:59:58 -06:00
|
|
|
let ccx = bcx.ccx();
|
|
|
|
let mut bcx = bcx;
|
|
|
|
|
|
|
|
let base_datum = unpack_datum!(bcx, trans_to_datum(bcx, base));
|
|
|
|
|
|
|
|
// Translate index expression and cast to a suitable LLVM integer.
|
|
|
|
// Rust is less strict than LLVM in this regard.
|
|
|
|
let Result {bcx, val: ix_val} = trans_to_datum(bcx, idx).to_result();
|
|
|
|
let ix_size = machine::llbitsize_of_real(bcx.ccx(), val_ty(ix_val));
|
|
|
|
let int_size = machine::llbitsize_of_real(bcx.ccx(), ccx.int_type);
|
|
|
|
let ix_val = {
|
|
|
|
if ix_size < int_size {
|
|
|
|
if ty::type_is_signed(expr_ty(bcx, idx)) {
|
|
|
|
SExt(bcx, ix_val, ccx.int_type)
|
|
|
|
} else { ZExt(bcx, ix_val, ccx.int_type) }
|
|
|
|
} else if ix_size > int_size {
|
|
|
|
Trunc(bcx, ix_val, ccx.int_type)
|
|
|
|
} else {
|
|
|
|
ix_val
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let vt = tvec::vec_types(bcx, base_datum.ty);
|
2013-05-19 00:07:44 -05:00
|
|
|
base::maybe_name_value(bcx.ccx(), vt.llunit_size, "unit_sz");
|
2013-01-10 12:59:58 -06:00
|
|
|
|
2013-06-06 20:54:14 -05:00
|
|
|
let (bcx, base, len) =
|
2013-10-16 11:04:51 -05:00
|
|
|
base_datum.get_vec_base_and_len(bcx, index_expr.span, index_expr.id, 0);
|
2013-01-10 12:59:58 -06:00
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("trans_index: base {}", bcx.val_to_str(base));
|
|
|
|
debug!("trans_index: len {}", bcx.val_to_str(len));
|
2013-01-10 12:59:58 -06:00
|
|
|
|
2013-10-16 11:04:51 -05:00
|
|
|
let bounds_check = ICmp(bcx, lib::llvm::IntUGE, ix_val, len);
|
2013-10-27 19:51:29 -05:00
|
|
|
let expect = ccx.intrinsics.get_copy(&("llvm.expect.i1"));
|
|
|
|
let expected = Call(bcx, expect, [bounds_check, C_i1(false)], []);
|
2013-11-21 17:42:55 -06:00
|
|
|
let bcx = with_cond(bcx, expected, |bcx| {
|
2013-10-16 11:04:51 -05:00
|
|
|
controlflow::trans_fail_bounds_check(bcx, index_expr.span, ix_val, len)
|
2013-11-21 17:42:55 -06:00
|
|
|
});
|
2013-05-19 00:07:44 -05:00
|
|
|
let elt = InBoundsGEP(bcx, base, [ix_val]);
|
2013-06-15 22:45:48 -05:00
|
|
|
let elt = PointerCast(bcx, elt, vt.llunit_ty.ptr_to());
|
2013-01-10 12:59:58 -06:00
|
|
|
return DatumBlock {
|
|
|
|
bcx: bcx,
|
|
|
|
datum: Datum {val: elt,
|
|
|
|
ty: vt.unit_ty,
|
2013-05-29 14:49:23 -05:00
|
|
|
mode: ByRef(ZeroMem)}
|
2013-01-10 12:59:58 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn trans_def_lvalue<'a>(
|
|
|
|
bcx: &'a Block<'a>,
|
2013-09-01 20:45:37 -05:00
|
|
|
ref_expr: &ast::Expr,
|
|
|
|
def: ast::Def)
|
2014-01-07 10:54:58 -06:00
|
|
|
-> DatumBlock<'a> {
|
2013-05-01 20:50:09 -05:00
|
|
|
//! Translates a reference to a path.
|
2013-01-10 12:59:58 -06:00
|
|
|
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_def_lvalue");
|
2013-01-10 12:59:58 -06:00
|
|
|
match def {
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::DefStatic(did, _) => {
|
2013-01-10 12:59:58 -06:00
|
|
|
let const_ty = expr_ty(bcx, ref_expr);
|
2013-03-07 00:30:20 -06:00
|
|
|
|
2013-12-19 18:47:15 -06:00
|
|
|
fn get_did(ccx: @CrateContext, did: ast::DefId)
|
2013-09-11 12:06:16 -05:00
|
|
|
-> ast::DefId {
|
|
|
|
if did.crate != ast::LOCAL_CRATE {
|
|
|
|
inline::maybe_instantiate_inline(ccx, did)
|
|
|
|
} else {
|
|
|
|
did
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn get_val(bcx: &Block, did: ast::DefId, const_ty: ty::t)
|
2013-05-09 21:39:07 -05:00
|
|
|
-> ValueRef {
|
|
|
|
// For external constants, we don't inline.
|
2013-07-27 03:25:59 -05:00
|
|
|
if did.crate == ast::LOCAL_CRATE {
|
2013-03-27 17:41:43 -05:00
|
|
|
// The LLVM global has the type of its initializer,
|
|
|
|
// which may not be equal to the enum's type for
|
|
|
|
// non-C-like enums.
|
2013-06-13 02:19:50 -05:00
|
|
|
let val = base::get_item_val(bcx.ccx(), did.node);
|
2013-10-05 16:44:37 -05:00
|
|
|
let pty = type_of::type_of(bcx.ccx(), const_ty).ptr_to();
|
2013-06-13 02:19:50 -05:00
|
|
|
PointerCast(bcx, val, pty)
|
2013-03-27 17:41:43 -05:00
|
|
|
} else {
|
2013-06-13 02:19:50 -05:00
|
|
|
{
|
2013-12-18 19:11:22 -06:00
|
|
|
let extern_const_values = bcx.ccx()
|
|
|
|
.extern_const_values
|
|
|
|
.borrow();
|
|
|
|
match extern_const_values.get().find(&did) {
|
2013-06-13 02:19:50 -05:00
|
|
|
None => {} // Continue.
|
|
|
|
Some(llval) => {
|
|
|
|
return *llval;
|
|
|
|
}
|
2013-03-27 17:41:43 -05:00
|
|
|
}
|
2013-05-09 21:39:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
2013-10-05 16:44:37 -05:00
|
|
|
let llty = type_of::type_of(bcx.ccx(), const_ty);
|
2013-05-09 21:39:07 -05:00
|
|
|
let symbol = csearch::get_symbol(
|
|
|
|
bcx.ccx().sess.cstore,
|
|
|
|
did);
|
2013-11-21 17:42:55 -06:00
|
|
|
let llval = symbol.with_c_str(|buf| {
|
2013-08-01 03:53:41 -05:00
|
|
|
llvm::LLVMAddGlobal(bcx.ccx().llmod,
|
|
|
|
llty.to_ref(),
|
|
|
|
buf)
|
2013-11-21 17:42:55 -06:00
|
|
|
});
|
2013-12-18 19:11:22 -06:00
|
|
|
let mut extern_const_values =
|
|
|
|
bcx.ccx().extern_const_values.borrow_mut();
|
|
|
|
extern_const_values.get().insert(did, llval);
|
2013-05-09 21:39:07 -05:00
|
|
|
llval
|
2013-03-27 17:41:43 -05:00
|
|
|
}
|
|
|
|
}
|
2013-03-07 00:30:20 -06:00
|
|
|
}
|
|
|
|
|
2013-09-11 12:06:16 -05:00
|
|
|
let did = get_did(bcx.ccx(), did);
|
2013-03-07 00:30:20 -06:00
|
|
|
let val = get_val(bcx, did, const_ty);
|
2013-01-10 12:59:58 -06:00
|
|
|
DatumBlock {
|
|
|
|
bcx: bcx,
|
|
|
|
datum: Datum {val: val,
|
|
|
|
ty: const_ty,
|
2013-05-29 14:49:23 -05:00
|
|
|
mode: ByRef(ZeroMem)}
|
2013-01-10 12:59:58 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
DatumBlock {
|
|
|
|
bcx: bcx,
|
|
|
|
datum: trans_local_var(bcx, def)
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
pub fn trans_local_var(bcx: &Block, def: ast::Def) -> Datum {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_local_var");
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
return match def {
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::DefUpvar(nid, _, _, _) => {
|
2013-01-10 12:59:58 -06:00
|
|
|
// Can't move upvars, so this is never a ZeroMemLastUse.
|
2012-08-28 17:54:45 -05:00
|
|
|
let local_ty = node_id_type(bcx, nid);
|
2013-12-19 22:20:04 -06:00
|
|
|
let llupvars = bcx.fcx.llupvars.borrow();
|
|
|
|
match llupvars.get().find(&nid) {
|
2013-03-22 21:26:41 -05:00
|
|
|
Some(&val) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
Datum {
|
|
|
|
val: val,
|
|
|
|
ty: local_ty,
|
2013-05-29 14:49:23 -05:00
|
|
|
mode: ByRef(ZeroMem)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {
|
2013-09-28 00:38:08 -05:00
|
|
|
bcx.sess().bug(format!(
|
|
|
|
"trans_local_var: no llval for upvar {:?} found", nid));
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::DefArg(nid, _) => {
|
2013-12-20 19:10:35 -06:00
|
|
|
let llargs = bcx.fcx.llargs.borrow();
|
|
|
|
take_local(bcx, llargs.get(), nid)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::DefLocal(nid, _) | ast::DefBinding(nid, _) => {
|
2013-12-20 19:10:35 -06:00
|
|
|
let lllocals = bcx.fcx.lllocals.borrow();
|
|
|
|
take_local(bcx, lllocals.get(), nid)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-10-20 00:55:23 -05:00
|
|
|
ast::DefSelf(nid, _) => {
|
2014-01-11 08:39:32 -06:00
|
|
|
let self_info = match bcx.fcx.llself.get() {
|
2013-12-20 22:45:05 -06:00
|
|
|
Some(self_info) => self_info,
|
2012-08-28 17:54:45 -05:00
|
|
|
None => {
|
2013-09-28 00:38:08 -05:00
|
|
|
bcx.sess().bug(format!(
|
2012-08-28 17:54:45 -05:00
|
|
|
"trans_local_var: reference to self \
|
2013-09-28 00:38:08 -05:00
|
|
|
out of context with id {:?}", nid));
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-11 08:39:32 -06:00
|
|
|
debug!("def_self() reference, self_info.ty={}",
|
|
|
|
self_info.ty.repr(bcx.tcx()));
|
2013-03-15 14:24:24 -05:00
|
|
|
|
2014-01-11 08:39:32 -06:00
|
|
|
self_info
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
_ => {
|
2013-09-28 00:38:08 -05:00
|
|
|
bcx.sess().unimpl(format!(
|
|
|
|
"unsupported def type in trans_local_var: {:?}", def));
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn take_local(bcx: &Block,
|
2014-01-11 08:39:32 -06:00
|
|
|
table: &HashMap<ast::NodeId, Datum>,
|
|
|
|
nid: ast::NodeId) -> Datum {
|
|
|
|
let datum = match table.find(&nid) {
|
2013-05-29 16:44:19 -05:00
|
|
|
Some(&v) => v,
|
2012-08-28 17:54:45 -05:00
|
|
|
None => {
|
2013-09-28 00:38:08 -05:00
|
|
|
bcx.sess().bug(format!(
|
2014-01-11 08:39:32 -06:00
|
|
|
"trans_local_var: no datum for local/arg {:?} found", nid));
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
};
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("take_local(nid={:?}, v={}, ty={})",
|
2014-01-11 08:39:32 -06:00
|
|
|
nid, bcx.val_to_str(datum.val), bcx.ty_to_str(datum.ty));
|
|
|
|
datum
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-23 17:56:40 -05:00
|
|
|
// The optional node ID here is the node ID of the path identifying the enum
|
|
|
|
// variant in use. If none, this cannot possibly an enum variant (so, if it
|
|
|
|
// is and `node_id_opt` is none, this function fails).
|
2013-11-19 15:22:03 -06:00
|
|
|
pub fn with_field_tys<R>(
|
|
|
|
tcx: ty::ctxt,
|
|
|
|
ty: ty::t,
|
|
|
|
node_id_opt: Option<ast::NodeId>,
|
|
|
|
op: |ty::Disr, (&[ty::field])| -> R)
|
|
|
|
-> R {
|
2012-09-11 18:20:31 -05:00
|
|
|
match ty::get(ty).sty {
|
2012-12-10 15:47:54 -06:00
|
|
|
ty::ty_struct(did, ref substs) => {
|
2013-05-03 20:51:58 -05:00
|
|
|
op(0, struct_fields(tcx, did, substs))
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2012-10-23 17:56:40 -05:00
|
|
|
ty::ty_enum(_, ref substs) => {
|
|
|
|
// We want the *variant* ID here, not the enum ID.
|
|
|
|
match node_id_opt {
|
|
|
|
None => {
|
2013-09-28 00:38:08 -05:00
|
|
|
tcx.sess.bug(format!(
|
|
|
|
"cannot get field types from the enum type {} \
|
2012-10-23 17:56:40 -05:00
|
|
|
without a node ID",
|
2013-03-15 14:24:24 -05:00
|
|
|
ty.repr(tcx)));
|
2012-10-23 17:56:40 -05:00
|
|
|
}
|
|
|
|
Some(node_id) => {
|
2013-12-23 13:15:16 -06:00
|
|
|
let opt_def = {
|
|
|
|
let def_map = tcx.def_map.borrow();
|
|
|
|
def_map.get().get_copy(&node_id)
|
|
|
|
};
|
|
|
|
match opt_def {
|
2013-09-08 19:36:01 -05:00
|
|
|
ast::DefVariant(enum_id, variant_id, _) => {
|
2013-02-24 00:53:40 -06:00
|
|
|
let variant_info = ty::enum_variant_with_id(
|
|
|
|
tcx, enum_id, variant_id);
|
2013-05-03 20:51:58 -05:00
|
|
|
op(variant_info.disr_val,
|
|
|
|
struct_fields(tcx, variant_id, substs))
|
2012-10-23 17:56:40 -05:00
|
|
|
}
|
|
|
|
_ => {
|
2013-05-19 00:07:44 -05:00
|
|
|
tcx.sess.bug("resolve didn't map this expr to a \
|
|
|
|
variant ID")
|
2012-10-23 17:56:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
_ => {
|
2013-09-28 00:38:08 -05:00
|
|
|
tcx.sess.bug(format!(
|
|
|
|
"cannot get field types from the type {}",
|
2013-03-15 14:24:24 -05:00
|
|
|
ty.repr(tcx)));
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn trans_rec_or_struct<'a>(
|
|
|
|
bcx: &'a Block<'a>,
|
2013-07-19 09:24:22 -05:00
|
|
|
fields: &[ast::Field],
|
2013-09-01 20:45:37 -05:00
|
|
|
base: Option<@ast::Expr>,
|
2013-08-31 11:13:04 -05:00
|
|
|
expr_span: codemap::Span,
|
2013-07-27 03:25:59 -05:00
|
|
|
id: ast::NodeId,
|
2014-01-07 10:54:58 -06:00
|
|
|
dest: Dest)
|
|
|
|
-> &'a Block<'a> {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_rec");
|
2013-04-12 00:15:30 -05:00
|
|
|
let bcx = bcx;
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
let ty = node_id_type(bcx, id);
|
|
|
|
let tcx = bcx.tcx();
|
2013-11-21 17:42:55 -06:00
|
|
|
with_field_tys(tcx, ty, Some(id), |discr, field_tys| {
|
2013-02-24 00:53:40 -06:00
|
|
|
let mut need_base = vec::from_elem(field_tys.len(), true);
|
|
|
|
|
2013-11-21 17:42:55 -06:00
|
|
|
let numbered_fields = fields.map(|field| {
|
2013-07-12 00:58:14 -05:00
|
|
|
let opt_pos =
|
|
|
|
field_tys.iter().position(|field_ty|
|
2013-10-28 21:22:42 -05:00
|
|
|
field_ty.ident.name == field.ident.node.name);
|
2013-03-02 16:03:41 -06:00
|
|
|
match opt_pos {
|
2013-02-24 00:53:40 -06:00
|
|
|
Some(i) => {
|
|
|
|
need_base[i] = false;
|
2013-07-19 09:24:22 -05:00
|
|
|
(i, field.expr)
|
2012-10-23 17:56:40 -05:00
|
|
|
}
|
2013-02-24 00:53:40 -06:00
|
|
|
None => {
|
|
|
|
tcx.sess.span_bug(field.span,
|
2013-05-02 11:28:53 -05:00
|
|
|
"Couldn't find field in struct type")
|
2012-10-23 17:56:40 -05:00
|
|
|
}
|
|
|
|
}
|
2013-11-21 17:42:55 -06:00
|
|
|
});
|
2013-02-24 00:53:40 -06:00
|
|
|
let optbase = match base {
|
|
|
|
Some(base_expr) => {
|
|
|
|
let mut leftovers = ~[];
|
2013-08-03 11:45:23 -05:00
|
|
|
for (i, b) in need_base.iter().enumerate() {
|
2013-02-24 00:53:40 -06:00
|
|
|
if *b {
|
|
|
|
leftovers.push((i, field_tys[i].mt.ty))
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-02-24 00:53:40 -06:00
|
|
|
Some(StructBaseInfo {expr: base_expr,
|
|
|
|
fields: leftovers })
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-02-24 00:53:40 -06:00
|
|
|
None => {
|
2013-07-04 21:13:26 -05:00
|
|
|
if need_base.iter().any(|b| *b) {
|
2013-05-19 00:07:44 -05:00
|
|
|
tcx.sess.span_bug(expr_span, "missing fields and no base expr")
|
2013-02-24 00:53:40 -06:00
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-02-24 00:53:40 -06:00
|
|
|
let repr = adt::represent_type(bcx.ccx(), ty);
|
2013-02-25 03:49:21 -06:00
|
|
|
trans_adt(bcx, repr, discr, numbered_fields, optbase, dest)
|
2013-11-21 17:42:55 -06:00
|
|
|
})
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-03-02 16:03:41 -06:00
|
|
|
/**
|
|
|
|
* Information that `trans_adt` needs in order to fill in the fields
|
|
|
|
* of a struct copied from a base struct (e.g., from an expression
|
|
|
|
* like `Foo { a: b, ..base }`.
|
|
|
|
*
|
|
|
|
* Note that `fields` may be empty; the base expression must always be
|
|
|
|
* evaluated for side-effects.
|
|
|
|
*/
|
2013-02-24 00:53:40 -06:00
|
|
|
struct StructBaseInfo {
|
2013-03-02 16:03:41 -06:00
|
|
|
/// The base expression; will be evaluated after all explicit fields.
|
2013-09-01 20:45:37 -05:00
|
|
|
expr: @ast::Expr,
|
2013-03-02 16:03:41 -06:00
|
|
|
/// The indices of fields to copy paired with their types.
|
2013-02-24 00:53:40 -06:00
|
|
|
fields: ~[(uint, ty::t)]
|
|
|
|
}
|
|
|
|
|
2013-03-02 16:03:41 -06:00
|
|
|
/**
|
|
|
|
* Constructs an ADT instance:
|
|
|
|
*
|
|
|
|
* - `fields` should be a list of field indices paired with the
|
|
|
|
* expression to store into that field. The initializers will be
|
|
|
|
* evaluated in the order specified by `fields`.
|
|
|
|
*
|
|
|
|
* - `optbase` contains information on the base struct (if any) from
|
|
|
|
* which remaining fields are copied; see comments on `StructBaseInfo`.
|
|
|
|
*/
|
2014-01-07 10:54:58 -06:00
|
|
|
fn trans_adt<'a>(
|
|
|
|
bcx: &'a Block<'a>,
|
|
|
|
repr: &adt::Repr,
|
|
|
|
discr: ty::Disr,
|
2013-09-01 20:45:37 -05:00
|
|
|
fields: &[(uint, @ast::Expr)],
|
2013-02-24 00:53:40 -06:00
|
|
|
optbase: Option<StructBaseInfo>,
|
2014-01-07 10:54:58 -06:00
|
|
|
dest: Dest)
|
|
|
|
-> &'a Block<'a> {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_adt");
|
2012-08-28 17:54:45 -05:00
|
|
|
let mut bcx = bcx;
|
|
|
|
let addr = match dest {
|
|
|
|
Ignore => {
|
2013-08-03 11:45:23 -05:00
|
|
|
for &(_i, e) in fields.iter() {
|
2013-02-24 00:53:40 -06:00
|
|
|
bcx = trans_into(bcx, e, Ignore);
|
|
|
|
}
|
2013-08-03 11:45:23 -05:00
|
|
|
for sbi in optbase.iter() {
|
2013-06-20 14:23:52 -05:00
|
|
|
// FIXME #7261: this moves entire base, not just certain fields
|
2013-02-24 00:53:40 -06:00
|
|
|
bcx = trans_into(bcx, sbi.expr, Ignore);
|
2012-09-18 23:41:37 -05:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
return bcx;
|
|
|
|
}
|
2013-02-24 00:53:40 -06:00
|
|
|
SaveIn(pos) => pos
|
2012-08-28 17:54:45 -05:00
|
|
|
};
|
|
|
|
let mut temp_cleanups = ~[];
|
2013-03-02 18:08:49 -06:00
|
|
|
adt::trans_start_init(bcx, repr, addr, discr);
|
2013-08-03 11:45:23 -05:00
|
|
|
for &(i, e) in fields.iter() {
|
2013-03-02 18:08:49 -06:00
|
|
|
let dest = adt::trans_field_ptr(bcx, repr, addr, discr, i);
|
2013-12-05 21:13:46 -06:00
|
|
|
let e_ty = expr_ty_adjusted(bcx, e);
|
2013-02-24 00:53:40 -06:00
|
|
|
bcx = trans_into(bcx, e, SaveIn(dest));
|
2012-08-28 17:54:45 -05:00
|
|
|
add_clean_temp_mem(bcx, dest, e_ty);
|
2012-09-26 19:33:34 -05:00
|
|
|
temp_cleanups.push(dest);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-08-03 11:45:23 -05:00
|
|
|
for base in optbase.iter() {
|
2013-05-17 19:27:44 -05:00
|
|
|
// FIXME #6573: is it sound to use the destination's repr on the base?
|
|
|
|
// And, would it ever be reasonable to be here with discr != 0?
|
2013-02-24 00:53:40 -06:00
|
|
|
let base_datum = unpack_datum!(bcx, trans_to_datum(bcx, base.expr));
|
2013-08-03 11:45:23 -05:00
|
|
|
for &(i, t) in base.fields.iter() {
|
2013-11-21 17:42:55 -06:00
|
|
|
let datum = base_datum.get_element(bcx, t, ZeroMem, |srcval| {
|
2013-03-02 18:08:49 -06:00
|
|
|
adt::trans_field_ptr(bcx, repr, srcval, discr, i)
|
2013-11-21 17:42:55 -06:00
|
|
|
});
|
2013-03-02 18:08:49 -06:00
|
|
|
let dest = adt::trans_field_ptr(bcx, repr, addr, discr, i);
|
2013-06-20 14:23:52 -05:00
|
|
|
bcx = datum.store_to(bcx, INIT, dest);
|
2013-02-24 00:53:40 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-03 11:45:23 -05:00
|
|
|
for cleanup in temp_cleanups.iter() {
|
2012-09-18 23:41:37 -05:00
|
|
|
revoke_clean(bcx, *cleanup);
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
return bcx;
|
|
|
|
}
|
|
|
|
|
2013-02-24 00:53:40 -06:00
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn trans_immediate_lit<'a>(
|
|
|
|
bcx: &'a Block<'a>,
|
|
|
|
expr: &ast::Expr,
|
2014-01-09 07:05:33 -06:00
|
|
|
lit: ast::Lit)
|
2014-01-07 10:54:58 -06:00
|
|
|
-> DatumBlock<'a> {
|
2012-08-28 17:54:45 -05:00
|
|
|
// must not be a string constant, that is a RvalueDpsExpr
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_immediate_lit");
|
2012-08-28 17:54:45 -05:00
|
|
|
let ty = expr_ty(bcx, expr);
|
|
|
|
immediate_rvalue_bcx(bcx, consts::const_lit(bcx.ccx(), expr, lit), ty)
|
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn trans_unary_datum<'a>(
|
|
|
|
bcx: &'a Block<'a>,
|
2013-09-01 20:45:37 -05:00
|
|
|
un_expr: &ast::Expr,
|
|
|
|
op: ast::UnOp,
|
2014-01-07 10:54:58 -06:00
|
|
|
sub_expr: &ast::Expr)
|
|
|
|
-> DatumBlock<'a> {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_unary_datum");
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
// if deref, would be LvalueExpr
|
2013-09-01 20:45:37 -05:00
|
|
|
assert!(op != ast::UnDeref);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
// if overloaded, would be RvalueDpsExpr
|
2013-12-21 19:04:42 -06:00
|
|
|
{
|
|
|
|
let method_map = bcx.ccx().maps.method_map.borrow();
|
|
|
|
assert!(!method_map.get().contains_key(&un_expr.id));
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
let un_ty = expr_ty(bcx, un_expr);
|
|
|
|
let sub_ty = expr_ty(bcx, sub_expr);
|
|
|
|
|
|
|
|
return match op {
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::UnNot => {
|
2012-09-11 23:25:01 -05:00
|
|
|
let Result {bcx, val} = trans_to_datum(bcx, sub_expr).to_result();
|
2013-02-06 16:28:02 -06:00
|
|
|
|
|
|
|
// If this is a boolean type, we must not use the LLVM Not
|
|
|
|
// instruction, as that is a *bitwise* not and we want *logical*
|
|
|
|
// not on our 8-bit boolean values.
|
|
|
|
let llresult = match ty::get(un_ty).sty {
|
|
|
|
ty::ty_bool => {
|
|
|
|
let llcond = ICmp(bcx,
|
|
|
|
lib::llvm::IntEQ,
|
|
|
|
val,
|
|
|
|
C_bool(false));
|
|
|
|
Select(bcx, llcond, C_bool(true), C_bool(false))
|
|
|
|
}
|
|
|
|
_ => Not(bcx, val)
|
|
|
|
};
|
|
|
|
immediate_rvalue_bcx(bcx, llresult, un_ty)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::UnNeg => {
|
2012-09-11 23:25:01 -05:00
|
|
|
let Result {bcx, val} = trans_to_datum(bcx, sub_expr).to_result();
|
2012-08-28 17:54:45 -05:00
|
|
|
let llneg = {
|
|
|
|
if ty::type_is_fp(un_ty) {
|
|
|
|
FNeg(bcx, val)
|
|
|
|
} else {
|
|
|
|
Neg(bcx, val)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
immediate_rvalue_bcx(bcx, llneg, un_ty)
|
|
|
|
}
|
2013-12-31 14:55:39 -06:00
|
|
|
ast::UnBox => {
|
|
|
|
trans_boxed_expr(bcx, un_ty, sub_expr, sub_ty, heap_managed)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::UnUniq => {
|
2013-02-20 17:02:21 -06:00
|
|
|
let heap = heap_for_unique(bcx, un_ty);
|
|
|
|
trans_boxed_expr(bcx, un_ty, sub_expr, sub_ty, heap)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::UnDeref => {
|
2013-05-19 00:07:44 -05:00
|
|
|
bcx.sess().bug("deref expressions should have been \
|
|
|
|
translated using trans_lvalue(), not \
|
|
|
|
trans_unary_datum()")
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
};
|
2013-12-17 18:46:18 -06:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-12-17 18:46:18 -06:00
|
|
|
fn trans_boxed_expr<'a>(
|
|
|
|
bcx: &'a Block<'a>,
|
|
|
|
box_ty: ty::t,
|
|
|
|
contents: &ast::Expr,
|
|
|
|
contents_ty: ty::t,
|
|
|
|
heap: heap)
|
|
|
|
-> DatumBlock<'a> {
|
|
|
|
let _icx = push_ctxt("trans_boxed_expr");
|
|
|
|
if heap == heap_exchange {
|
|
|
|
let llty = type_of::type_of(bcx.ccx(), contents_ty);
|
|
|
|
let size = llsize_of(bcx.ccx(), llty);
|
|
|
|
let Result { bcx: bcx, val: val } = malloc_raw_dyn(bcx, contents_ty,
|
|
|
|
heap_exchange, size);
|
|
|
|
add_clean_free(bcx, val, heap_exchange);
|
|
|
|
let bcx = trans_into(bcx, contents, SaveIn(val));
|
|
|
|
revoke_clean(bcx, val);
|
|
|
|
return immediate_rvalue_bcx(bcx, val, box_ty);
|
|
|
|
} else {
|
|
|
|
let base::MallocResult {
|
|
|
|
bcx,
|
|
|
|
smart_ptr: bx,
|
|
|
|
body
|
|
|
|
} = base::malloc_general(bcx, contents_ty, heap);
|
|
|
|
add_clean_free(bcx, bx, heap);
|
|
|
|
let bcx = trans_into(bcx, contents, SaveIn(body));
|
|
|
|
revoke_clean(bcx, bx);
|
|
|
|
return immediate_rvalue_bcx(bcx, bx, box_ty);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn trans_addr_of<'a>(
|
|
|
|
bcx: &'a Block<'a>,
|
|
|
|
expr: &ast::Expr,
|
|
|
|
subexpr: &ast::Expr)
|
|
|
|
-> DatumBlock<'a> {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_addr_of");
|
2012-08-28 17:54:45 -05:00
|
|
|
let mut bcx = bcx;
|
|
|
|
let sub_datum = unpack_datum!(bcx, trans_to_datum(bcx, subexpr));
|
|
|
|
let llval = sub_datum.to_ref_llval(bcx);
|
|
|
|
return immediate_rvalue_bcx(bcx, llval, expr_ty(bcx, expr));
|
|
|
|
}
|
|
|
|
|
2013-12-17 18:46:18 -06:00
|
|
|
pub fn trans_gc<'a>(
|
|
|
|
mut bcx: &'a Block<'a>,
|
|
|
|
expr: &ast::Expr,
|
|
|
|
contents: &ast::Expr,
|
|
|
|
dest: Dest)
|
|
|
|
-> &'a Block<'a> {
|
|
|
|
let contents_ty = expr_ty(bcx, contents);
|
|
|
|
let box_ty = ty::mk_box(bcx.tcx(), contents_ty);
|
|
|
|
let expr_ty = expr_ty(bcx, expr);
|
|
|
|
|
|
|
|
let addr = match dest {
|
|
|
|
Ignore => {
|
|
|
|
return trans_boxed_expr(bcx,
|
|
|
|
box_ty,
|
|
|
|
contents,
|
|
|
|
contents_ty,
|
|
|
|
heap_managed).bcx
|
|
|
|
}
|
|
|
|
SaveIn(addr) => addr,
|
|
|
|
};
|
|
|
|
|
|
|
|
let repr = adt::represent_type(bcx.ccx(), expr_ty);
|
|
|
|
adt::trans_start_init(bcx, repr, addr, 0);
|
|
|
|
let field_dest = adt::trans_field_ptr(bcx, repr, addr, 0, 0);
|
|
|
|
let contents_datum_block = trans_boxed_expr(bcx,
|
|
|
|
box_ty,
|
|
|
|
contents,
|
|
|
|
contents_ty,
|
|
|
|
heap_managed);
|
|
|
|
bcx = contents_datum_block.bcx;
|
|
|
|
bcx = contents_datum_block.datum.move_to(bcx, INIT, field_dest);
|
|
|
|
|
|
|
|
// Next, wrap it up in the struct.
|
|
|
|
bcx
|
|
|
|
}
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
// Important to get types for both lhs and rhs, because one might be _|_
|
|
|
|
// and the other not.
|
2014-01-07 10:54:58 -06:00
|
|
|
fn trans_eager_binop<'a>(
|
|
|
|
bcx: &'a Block<'a>,
|
2013-09-01 20:45:37 -05:00
|
|
|
binop_expr: &ast::Expr,
|
2012-08-28 17:54:45 -05:00
|
|
|
binop_ty: ty::t,
|
2013-09-01 20:45:37 -05:00
|
|
|
op: ast::BinOp,
|
2012-08-28 17:54:45 -05:00
|
|
|
lhs_datum: &Datum,
|
2013-02-06 16:28:02 -06:00
|
|
|
rhs_datum: &Datum)
|
2014-01-07 10:54:58 -06:00
|
|
|
-> DatumBlock<'a> {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_eager_binop");
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
let lhs = lhs_datum.to_appropriate_llval(bcx);
|
|
|
|
let lhs_t = lhs_datum.ty;
|
|
|
|
|
|
|
|
let rhs = rhs_datum.to_appropriate_llval(bcx);
|
|
|
|
let rhs_t = rhs_datum.ty;
|
|
|
|
|
2013-07-10 09:35:59 -05:00
|
|
|
let mut intype = {
|
2012-08-28 17:54:45 -05:00
|
|
|
if ty::type_is_bot(lhs_t) { rhs_t }
|
|
|
|
else { lhs_t }
|
|
|
|
};
|
2013-07-10 09:35:59 -05:00
|
|
|
let tcx = bcx.tcx();
|
|
|
|
if ty::type_is_simd(tcx, intype) {
|
|
|
|
intype = ty::simd_type(tcx, intype);
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
let is_float = ty::type_is_fp(intype);
|
2013-07-10 09:35:59 -05:00
|
|
|
let signed = ty::type_is_signed(intype);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
let rhs = base::cast_shift_expr_rhs(bcx, op, lhs, rhs);
|
|
|
|
|
|
|
|
let mut bcx = bcx;
|
|
|
|
let val = match op {
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::BiAdd => {
|
2012-08-28 17:54:45 -05:00
|
|
|
if is_float { FAdd(bcx, lhs, rhs) }
|
|
|
|
else { Add(bcx, lhs, rhs) }
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::BiSub => {
|
2012-08-28 17:54:45 -05:00
|
|
|
if is_float { FSub(bcx, lhs, rhs) }
|
|
|
|
else { Sub(bcx, lhs, rhs) }
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::BiMul => {
|
2012-08-28 17:54:45 -05:00
|
|
|
if is_float { FMul(bcx, lhs, rhs) }
|
|
|
|
else { Mul(bcx, lhs, rhs) }
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::BiDiv => {
|
2012-08-28 17:54:45 -05:00
|
|
|
if is_float {
|
|
|
|
FDiv(bcx, lhs, rhs)
|
|
|
|
} else {
|
|
|
|
// Only zero-check integers; fp /0 is NaN
|
|
|
|
bcx = base::fail_if_zero(bcx, binop_expr.span,
|
|
|
|
op, rhs, rhs_t);
|
2013-07-10 09:35:59 -05:00
|
|
|
if signed {
|
2012-08-28 17:54:45 -05:00
|
|
|
SDiv(bcx, lhs, rhs)
|
|
|
|
} else {
|
|
|
|
UDiv(bcx, lhs, rhs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::BiRem => {
|
2012-08-28 17:54:45 -05:00
|
|
|
if is_float {
|
|
|
|
FRem(bcx, lhs, rhs)
|
|
|
|
} else {
|
|
|
|
// Only zero-check integers; fp %0 is NaN
|
|
|
|
bcx = base::fail_if_zero(bcx, binop_expr.span,
|
|
|
|
op, rhs, rhs_t);
|
2013-07-10 09:35:59 -05:00
|
|
|
if signed {
|
2012-08-28 17:54:45 -05:00
|
|
|
SRem(bcx, lhs, rhs)
|
|
|
|
} else {
|
|
|
|
URem(bcx, lhs, rhs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::BiBitOr => Or(bcx, lhs, rhs),
|
|
|
|
ast::BiBitAnd => And(bcx, lhs, rhs),
|
|
|
|
ast::BiBitXor => Xor(bcx, lhs, rhs),
|
|
|
|
ast::BiShl => Shl(bcx, lhs, rhs),
|
|
|
|
ast::BiShr => {
|
2013-07-10 09:35:59 -05:00
|
|
|
if signed {
|
2012-08-28 17:54:45 -05:00
|
|
|
AShr(bcx, lhs, rhs)
|
|
|
|
} else { LShr(bcx, lhs, rhs) }
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::BiEq | ast::BiNe | ast::BiLt | ast::BiGe | ast::BiLe | ast::BiGt => {
|
2012-09-07 20:53:14 -05:00
|
|
|
if ty::type_is_bot(rhs_t) {
|
|
|
|
C_bool(false)
|
|
|
|
} else {
|
|
|
|
if !ty::type_is_scalar(rhs_t) {
|
|
|
|
bcx.tcx().sess.span_bug(binop_expr.span,
|
2013-05-02 11:28:53 -05:00
|
|
|
"non-scalar comparison");
|
2012-09-07 20:53:14 -05:00
|
|
|
}
|
|
|
|
let cmpr = base::compare_scalar_types(bcx, lhs, rhs, rhs_t, op);
|
|
|
|
bcx = cmpr.bcx;
|
2013-06-15 22:45:48 -05:00
|
|
|
ZExt(bcx, cmpr.val, Type::i8())
|
2012-09-07 20:53:14 -05:00
|
|
|
}
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
_ => {
|
2013-05-02 11:28:53 -05:00
|
|
|
bcx.tcx().sess.span_bug(binop_expr.span, "unexpected binop");
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return immediate_rvalue_bcx(bcx, val, binop_ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
// refinement types would obviate the need for this
|
2014-01-07 10:54:58 -06:00
|
|
|
enum lazy_binop_ty {
|
|
|
|
lazy_and,
|
|
|
|
lazy_or,
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn trans_lazy_binop<'a>(
|
|
|
|
bcx: &'a Block<'a>,
|
2013-09-01 20:45:37 -05:00
|
|
|
binop_expr: &ast::Expr,
|
2012-08-28 17:54:45 -05:00
|
|
|
op: lazy_binop_ty,
|
2013-09-30 12:37:17 -05:00
|
|
|
a: &ast::Expr,
|
2014-01-07 10:54:58 -06:00
|
|
|
b: &ast::Expr)
|
|
|
|
-> DatumBlock<'a> {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_lazy_binop");
|
2012-08-28 17:54:45 -05:00
|
|
|
let binop_ty = expr_ty(bcx, binop_expr);
|
2013-04-12 00:15:30 -05:00
|
|
|
let bcx = bcx;
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
let Result {bcx: past_lhs, val: lhs} = {
|
2013-11-21 17:42:55 -06:00
|
|
|
base::with_scope_result(bcx, a.info(), "lhs", |bcx| {
|
2012-09-11 23:25:01 -05:00
|
|
|
trans_to_datum(bcx, a).to_result()
|
2013-11-21 17:42:55 -06:00
|
|
|
})
|
2012-08-28 17:54:45 -05:00
|
|
|
};
|
|
|
|
|
2013-12-18 16:54:42 -06:00
|
|
|
if past_lhs.unreachable.get() {
|
2012-08-28 17:54:45 -05:00
|
|
|
return immediate_rvalue_bcx(past_lhs, lhs, binop_ty);
|
|
|
|
}
|
|
|
|
|
2013-05-02 03:16:07 -05:00
|
|
|
let join = base::sub_block(bcx, "join");
|
|
|
|
let before_rhs = base::sub_block(bcx, "rhs");
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-02-06 16:28:02 -06:00
|
|
|
let lhs_i1 = bool_to_i1(past_lhs, lhs);
|
2012-08-28 17:54:45 -05:00
|
|
|
match op {
|
2013-02-06 16:28:02 -06:00
|
|
|
lazy_and => CondBr(past_lhs, lhs_i1, before_rhs.llbb, join.llbb),
|
|
|
|
lazy_or => CondBr(past_lhs, lhs_i1, join.llbb, before_rhs.llbb)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2013-02-06 16:28:02 -06:00
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
let Result {bcx: past_rhs, val: rhs} = {
|
2013-11-21 17:42:55 -06:00
|
|
|
base::with_scope_result(before_rhs, b.info(), "rhs", |bcx| {
|
2012-09-11 23:25:01 -05:00
|
|
|
trans_to_datum(bcx, b).to_result()
|
2013-11-21 17:42:55 -06:00
|
|
|
})
|
2012-08-28 17:54:45 -05:00
|
|
|
};
|
|
|
|
|
2013-12-18 16:54:42 -06:00
|
|
|
if past_rhs.unreachable.get() {
|
2012-08-28 17:54:45 -05:00
|
|
|
return immediate_rvalue_bcx(join, lhs, binop_ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
Br(past_rhs, join.llbb);
|
2013-06-15 22:45:48 -05:00
|
|
|
let phi = Phi(join, Type::bool(), [lhs, rhs], [past_lhs.llbb,
|
2013-05-19 00:07:44 -05:00
|
|
|
past_rhs.llbb]);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
return immediate_rvalue_bcx(join, phi, binop_ty);
|
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn trans_binary<'a>(
|
|
|
|
bcx: &'a Block<'a>,
|
2013-09-01 20:45:37 -05:00
|
|
|
binop_expr: &ast::Expr,
|
|
|
|
op: ast::BinOp,
|
2013-09-30 12:37:17 -05:00
|
|
|
lhs: &ast::Expr,
|
2014-01-07 10:54:58 -06:00
|
|
|
rhs: &ast::Expr)
|
|
|
|
-> DatumBlock<'a> {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_binary");
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
match op {
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::BiAnd => {
|
2012-08-28 17:54:45 -05:00
|
|
|
trans_lazy_binop(bcx, binop_expr, lazy_and, lhs, rhs)
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::BiOr => {
|
2012-08-28 17:54:45 -05:00
|
|
|
trans_lazy_binop(bcx, binop_expr, lazy_or, lhs, rhs)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
let mut bcx = bcx;
|
|
|
|
let lhs_datum = unpack_datum!(bcx, trans_to_datum(bcx, lhs));
|
|
|
|
let rhs_datum = unpack_datum!(bcx, trans_to_datum(bcx, rhs));
|
|
|
|
let binop_ty = expr_ty(bcx, binop_expr);
|
|
|
|
trans_eager_binop(bcx, binop_expr, binop_ty, op,
|
|
|
|
&lhs_datum, &rhs_datum)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn trans_overloaded_op<'a>(
|
|
|
|
bcx: &'a Block<'a>,
|
2013-09-01 20:45:37 -05:00
|
|
|
expr: &ast::Expr,
|
2013-07-27 03:25:59 -05:00
|
|
|
callee_id: ast::NodeId,
|
2013-09-30 12:37:17 -05:00
|
|
|
rcvr: &ast::Expr,
|
2013-09-01 20:45:37 -05:00
|
|
|
args: ~[@ast::Expr],
|
2013-04-18 17:53:29 -05:00
|
|
|
ret_ty: ty::t,
|
|
|
|
dest: Dest)
|
2014-01-07 10:54:58 -06:00
|
|
|
-> &'a Block<'a> {
|
2013-12-21 19:04:42 -06:00
|
|
|
let origin = {
|
|
|
|
let method_map = bcx.ccx().maps.method_map.borrow();
|
|
|
|
method_map.get().get_copy(&expr.id)
|
|
|
|
};
|
2013-06-01 17:31:56 -05:00
|
|
|
let fty = node_id_type(bcx, callee_id);
|
2013-04-18 17:53:29 -05:00
|
|
|
callee::trans_call_inner(bcx,
|
|
|
|
expr.info(),
|
|
|
|
fty,
|
|
|
|
ret_ty,
|
|
|
|
|bcx| {
|
|
|
|
meth::trans_method_callee(bcx,
|
2013-06-01 17:31:56 -05:00
|
|
|
callee_id,
|
2013-04-18 17:53:29 -05:00
|
|
|
rcvr,
|
|
|
|
origin)
|
|
|
|
},
|
|
|
|
callee::ArgExprs(args),
|
2013-07-08 01:12:01 -05:00
|
|
|
Some(dest),
|
|
|
|
DoAutorefArg).bcx
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn int_cast(bcx: &Block,
|
|
|
|
lldsttype: Type,
|
|
|
|
llsrctype: Type,
|
|
|
|
llsrc: ValueRef,
|
|
|
|
signed: bool)
|
|
|
|
-> ValueRef {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("int_cast");
|
2013-01-10 23:23:07 -06:00
|
|
|
unsafe {
|
2013-06-16 05:52:44 -05:00
|
|
|
let srcsz = llvm::LLVMGetIntTypeWidth(llsrctype.to_ref());
|
|
|
|
let dstsz = llvm::LLVMGetIntTypeWidth(lldsttype.to_ref());
|
2013-01-10 23:23:07 -06:00
|
|
|
return if dstsz == srcsz {
|
|
|
|
BitCast(bcx, llsrc, lldsttype)
|
|
|
|
} else if srcsz > dstsz {
|
|
|
|
TruncOrBitCast(bcx, llsrc, lldsttype)
|
|
|
|
} else if signed {
|
|
|
|
SExtOrBitCast(bcx, llsrc, lldsttype)
|
|
|
|
} else {
|
|
|
|
ZExtOrBitCast(bcx, llsrc, lldsttype)
|
|
|
|
};
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn float_cast(bcx: &Block,
|
|
|
|
lldsttype: Type,
|
|
|
|
llsrctype: Type,
|
|
|
|
llsrc: ValueRef)
|
|
|
|
-> ValueRef {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("float_cast");
|
2013-06-16 05:52:44 -05:00
|
|
|
let srcsz = llsrctype.float_width();
|
|
|
|
let dstsz = lldsttype.float_width();
|
2012-08-28 17:54:45 -05:00
|
|
|
return if dstsz > srcsz {
|
|
|
|
FPExt(bcx, llsrc, lldsttype)
|
|
|
|
} else if srcsz > dstsz {
|
|
|
|
FPTrunc(bcx, llsrc, lldsttype)
|
|
|
|
} else { llsrc };
|
|
|
|
}
|
|
|
|
|
2013-03-26 07:04:54 -05:00
|
|
|
#[deriving(Eq)]
|
2013-01-29 19:57:02 -06:00
|
|
|
pub enum cast_kind {
|
2012-08-28 17:54:45 -05:00
|
|
|
cast_pointer,
|
|
|
|
cast_integral,
|
|
|
|
cast_float,
|
|
|
|
cast_enum,
|
|
|
|
cast_other,
|
|
|
|
}
|
|
|
|
|
2013-01-29 19:57:02 -06:00
|
|
|
pub fn cast_type_kind(t: ty::t) -> cast_kind {
|
2012-09-11 18:20:31 -05:00
|
|
|
match ty::get(t).sty {
|
2013-09-03 18:24:12 -05:00
|
|
|
ty::ty_char => cast_integral,
|
2013-11-28 14:22:53 -06:00
|
|
|
ty::ty_float(..) => cast_float,
|
|
|
|
ty::ty_ptr(..) => cast_pointer,
|
|
|
|
ty::ty_rptr(..) => cast_pointer,
|
|
|
|
ty::ty_bare_fn(..) => cast_pointer,
|
|
|
|
ty::ty_int(..) => cast_integral,
|
|
|
|
ty::ty_uint(..) => cast_integral,
|
2012-08-28 17:54:45 -05:00
|
|
|
ty::ty_bool => cast_integral,
|
2013-11-28 14:22:53 -06:00
|
|
|
ty::ty_enum(..) => cast_enum,
|
2012-08-28 17:54:45 -05:00
|
|
|
_ => cast_other
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn trans_imm_cast<'a>(bcx: &'a Block<'a>, expr: &ast::Expr, id: ast::NodeId)
|
|
|
|
-> DatumBlock<'a> {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_cast");
|
2012-08-28 17:54:45 -05:00
|
|
|
let ccx = bcx.ccx();
|
|
|
|
|
|
|
|
let t_out = node_id_type(bcx, id);
|
|
|
|
|
|
|
|
let mut bcx = bcx;
|
2012-09-11 23:25:01 -05:00
|
|
|
let llexpr = unpack_result!(bcx, trans_to_datum(bcx, expr).to_result());
|
2012-08-28 17:54:45 -05:00
|
|
|
let ll_t_in = val_ty(llexpr);
|
|
|
|
let t_in = expr_ty(bcx, expr);
|
|
|
|
let ll_t_out = type_of::type_of(ccx, t_out);
|
|
|
|
|
|
|
|
let k_in = cast_type_kind(t_in);
|
|
|
|
let k_out = cast_type_kind(t_out);
|
|
|
|
let s_in = k_in == cast_integral && ty::type_is_signed(t_in);
|
|
|
|
|
|
|
|
let newval =
|
2013-02-19 01:40:42 -06:00
|
|
|
match (k_in, k_out) {
|
|
|
|
(cast_integral, cast_integral) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
int_cast(bcx, ll_t_out, ll_t_in, llexpr, s_in)
|
|
|
|
}
|
2013-02-19 01:40:42 -06:00
|
|
|
(cast_float, cast_float) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
float_cast(bcx, ll_t_out, ll_t_in, llexpr)
|
|
|
|
}
|
2013-02-19 01:40:42 -06:00
|
|
|
(cast_integral, cast_float) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
if s_in {
|
|
|
|
SIToFP(bcx, llexpr, ll_t_out)
|
|
|
|
} else { UIToFP(bcx, llexpr, ll_t_out) }
|
|
|
|
}
|
2013-02-19 01:40:42 -06:00
|
|
|
(cast_float, cast_integral) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
if ty::type_is_signed(t_out) {
|
|
|
|
FPToSI(bcx, llexpr, ll_t_out)
|
|
|
|
} else { FPToUI(bcx, llexpr, ll_t_out) }
|
|
|
|
}
|
2013-02-19 01:40:42 -06:00
|
|
|
(cast_integral, cast_pointer) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
IntToPtr(bcx, llexpr, ll_t_out)
|
|
|
|
}
|
2013-02-19 01:40:42 -06:00
|
|
|
(cast_pointer, cast_integral) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
PtrToInt(bcx, llexpr, ll_t_out)
|
|
|
|
}
|
2013-02-19 01:40:42 -06:00
|
|
|
(cast_pointer, cast_pointer) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
PointerCast(bcx, llexpr, ll_t_out)
|
|
|
|
}
|
2013-02-19 01:40:42 -06:00
|
|
|
(cast_enum, cast_integral) |
|
|
|
|
(cast_enum, cast_float) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
let bcx = bcx;
|
2013-02-24 18:24:54 -06:00
|
|
|
let repr = adt::represent_type(ccx, t_in);
|
2013-10-03 15:59:34 -05:00
|
|
|
let llexpr_ptr;
|
|
|
|
if type_is_immediate(ccx, t_in) {
|
|
|
|
llexpr_ptr = Alloca(bcx, ll_t_in, "");
|
|
|
|
Store(bcx, llexpr, llexpr_ptr);
|
|
|
|
} else {
|
|
|
|
llexpr_ptr = llexpr;
|
|
|
|
}
|
|
|
|
let lldiscrim_a = adt::trans_get_discr(bcx, repr, llexpr_ptr, Some(Type::i64()));
|
2012-08-28 17:54:45 -05:00
|
|
|
match k_out {
|
|
|
|
cast_integral => int_cast(bcx, ll_t_out,
|
|
|
|
val_ty(lldiscrim_a),
|
|
|
|
lldiscrim_a, true),
|
|
|
|
cast_float => SIToFP(bcx, lldiscrim_a, ll_t_out),
|
2013-09-28 00:38:08 -05:00
|
|
|
_ => ccx.sess.bug(format!("translating unsupported cast: \
|
|
|
|
{} ({:?}) -> {} ({:?})",
|
2013-08-21 08:27:48 -05:00
|
|
|
t_in.repr(ccx.tcx), k_in,
|
|
|
|
t_out.repr(ccx.tcx), k_out))
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
2013-09-28 00:38:08 -05:00
|
|
|
_ => ccx.sess.bug(format!("translating unsupported cast: \
|
|
|
|
{} ({:?}) -> {} ({:?})",
|
2013-08-21 08:27:48 -05:00
|
|
|
t_in.repr(ccx.tcx), k_in,
|
|
|
|
t_out.repr(ccx.tcx), k_out))
|
2012-08-28 17:54:45 -05:00
|
|
|
};
|
|
|
|
return immediate_rvalue_bcx(bcx, newval, t_out);
|
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
fn trans_assign_op<'a>(
|
|
|
|
bcx: &'a Block<'a>,
|
2013-09-30 12:37:17 -05:00
|
|
|
expr: &ast::Expr,
|
2013-07-27 03:25:59 -05:00
|
|
|
callee_id: ast::NodeId,
|
2013-09-01 20:45:37 -05:00
|
|
|
op: ast::BinOp,
|
2013-09-30 12:37:17 -05:00
|
|
|
dst: &ast::Expr,
|
2014-01-07 10:54:58 -06:00
|
|
|
src: @ast::Expr)
|
|
|
|
-> &'a Block<'a> {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("trans_assign_op");
|
2012-08-28 17:54:45 -05:00
|
|
|
let mut bcx = bcx;
|
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("trans_assign_op(expr={})", bcx.expr_to_str(expr));
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
// Evaluate LHS (destination), which should be an lvalue
|
2012-11-27 16:12:33 -06:00
|
|
|
let dst_datum = unpack_datum!(bcx, trans_lvalue_unadjusted(bcx, dst));
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
// A user-defined operator method
|
2013-12-21 19:04:42 -06:00
|
|
|
let found = {
|
|
|
|
let method_map = bcx.ccx().maps.method_map.borrow();
|
|
|
|
method_map.get().find(&expr.id).is_some()
|
|
|
|
};
|
|
|
|
if found {
|
2012-10-11 18:42:40 -05:00
|
|
|
// FIXME(#2528) evaluates the receiver twice!!
|
2013-06-20 14:21:37 -05:00
|
|
|
let scratch = scratch_datum(bcx, dst_datum.ty, "__assign_op", false);
|
2013-04-18 17:53:29 -05:00
|
|
|
let bcx = trans_overloaded_op(bcx,
|
|
|
|
expr,
|
2013-06-01 17:31:56 -05:00
|
|
|
callee_id,
|
2013-04-18 17:53:29 -05:00
|
|
|
dst,
|
|
|
|
~[src],
|
|
|
|
dst_datum.ty,
|
2013-03-26 14:04:30 -05:00
|
|
|
SaveIn(scratch.val));
|
2012-08-28 17:54:45 -05:00
|
|
|
return scratch.move_to_datum(bcx, DROP_EXISTING, dst_datum);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Evaluate RHS (source)
|
|
|
|
let src_datum = unpack_datum!(bcx, trans_to_datum(bcx, src));
|
|
|
|
|
|
|
|
// Perform computation and store the result
|
|
|
|
let result_datum =
|
|
|
|
unpack_datum!(bcx,
|
|
|
|
trans_eager_binop(
|
|
|
|
bcx, expr, dst_datum.ty, op,
|
|
|
|
&dst_datum, &src_datum));
|
2013-01-10 12:59:58 -06:00
|
|
|
return result_datum.copy_to_datum(bcx, DROP_EXISTING, dst_datum);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-01-07 10:54:58 -06:00
|
|
|
pub fn trans_log_level<'a>(bcx: &'a Block<'a>) -> DatumBlock<'a> {
|
2013-08-28 01:12:05 -05:00
|
|
|
let _icx = push_ctxt("trans_log_level");
|
|
|
|
let ccx = bcx.ccx();
|
|
|
|
|
|
|
|
let (modpath, modname) = {
|
2013-12-18 20:32:52 -06:00
|
|
|
let srccrate;
|
|
|
|
{
|
|
|
|
let external_srcs = ccx.external_srcs.borrow();
|
|
|
|
srccrate = match external_srcs.get().find(&bcx.fcx.id) {
|
|
|
|
Some(&src) => {
|
|
|
|
ccx.sess.cstore.get_crate_data(src.crate).name
|
|
|
|
}
|
2013-12-28 11:16:48 -06:00
|
|
|
None => ccx.link_meta.crateid.name.to_managed(),
|
2013-12-18 20:32:52 -06:00
|
|
|
};
|
2013-10-01 17:24:49 -05:00
|
|
|
};
|
2014-01-09 07:05:33 -06:00
|
|
|
let mut modpath = ~[PathMod(ccx.sess.ident_of(srccrate))];
|
2013-10-01 17:24:49 -05:00
|
|
|
for e in bcx.fcx.path.iter() {
|
2013-08-28 01:12:05 -05:00
|
|
|
match *e {
|
2014-01-09 07:05:33 -06:00
|
|
|
PathMod(_) => { modpath.push(*e) }
|
2013-08-28 01:12:05 -05:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let modname = path_str(ccx.sess, modpath);
|
|
|
|
(modpath, modname)
|
|
|
|
};
|
|
|
|
|
2013-12-18 19:44:25 -06:00
|
|
|
let module_data_exists;
|
|
|
|
{
|
|
|
|
let module_data = ccx.module_data.borrow();
|
|
|
|
module_data_exists = module_data.get().contains_key(&modname);
|
|
|
|
}
|
|
|
|
|
|
|
|
let global = if module_data_exists {
|
|
|
|
let mut module_data = ccx.module_data.borrow_mut();
|
|
|
|
module_data.get().get_copy(&modname)
|
2013-08-28 01:12:05 -05:00
|
|
|
} else {
|
|
|
|
let s = link::mangle_internal_name_by_path_and_seq(
|
|
|
|
ccx, modpath, "loglevel");
|
|
|
|
let global;
|
|
|
|
unsafe {
|
2013-11-21 17:42:55 -06:00
|
|
|
global = s.with_c_str(|buf| {
|
2013-08-28 01:12:05 -05:00
|
|
|
llvm::LLVMAddGlobal(ccx.llmod, Type::i32().to_ref(), buf)
|
2013-11-21 17:42:55 -06:00
|
|
|
});
|
2013-08-28 01:12:05 -05:00
|
|
|
llvm::LLVMSetGlobalConstant(global, False);
|
|
|
|
llvm::LLVMSetInitializer(global, C_null(Type::i32()));
|
|
|
|
lib::llvm::SetLinkage(global, lib::llvm::InternalLinkage);
|
|
|
|
}
|
2013-12-18 19:44:25 -06:00
|
|
|
{
|
|
|
|
let mut module_data = ccx.module_data.borrow_mut();
|
|
|
|
module_data.get().insert(modname, global);
|
|
|
|
global
|
|
|
|
}
|
2013-08-28 01:12:05 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
return immediate_rvalue_bcx(bcx, Load(bcx, global), ty::mk_u32());
|
|
|
|
}
|
2014-01-07 10:54:58 -06:00
|
|
|
|