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
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* A `Datum` contains all the information you need to describe the LLVM
|
|
|
|
* translation of a Rust value. It describes where the value is stored,
|
|
|
|
* what Rust type the value has, whether it is addressed by reference,
|
|
|
|
* and so forth.
|
|
|
|
*
|
|
|
|
* The idea of a datum is that, to the extent possible, you should not
|
|
|
|
* care about these details, but rather use the methods on the Datum
|
|
|
|
* type to "do what you want to do". For example, you can simply call
|
|
|
|
* `copy_to()` or `move_to()` to copy or move the value into a new
|
|
|
|
* home.
|
|
|
|
*
|
|
|
|
* # Datum location
|
|
|
|
*
|
|
|
|
* The primary two fields of a datum are the `val` and the `mode`.
|
|
|
|
* The `val` is an LLVM value ref. It may either *be the value* that
|
|
|
|
* is being tracked, or it may be a *pointer to the value being
|
|
|
|
* tracked*. This is specified in the `mode` field, which can either
|
|
|
|
* be `ByValue` or `ByRef`, respectively. The (Rust) type of the
|
|
|
|
* value stored in the datum is indicated in the field `ty`.
|
|
|
|
*
|
|
|
|
* Generally speaking, you probably do not want to access the `val` field
|
2013-04-18 17:53:29 -05:00
|
|
|
* unless you know what mode the value is in. Instead you should use one
|
2012-08-28 17:54:45 -05:00
|
|
|
* of the following accessors:
|
|
|
|
*
|
|
|
|
* - `to_value_llval()` converts to by-value
|
|
|
|
* - `to_ref_llval()` converts to by-ref, allocating a stack slot if necessary
|
|
|
|
* - `to_appropriate_llval()` converts to by-value if this is an
|
|
|
|
* immediate type, by-ref otherwise. This is particularly
|
|
|
|
* convenient for interfacing with the various code floating around
|
|
|
|
* that predates datums.
|
|
|
|
*
|
2013-01-10 12:59:58 -06:00
|
|
|
* # Datum cleanup styles
|
2012-08-28 17:54:45 -05:00
|
|
|
*
|
2013-01-10 12:59:58 -06:00
|
|
|
* Each datum carries with it an idea of how its value will be cleaned
|
2013-05-29 14:49:23 -05:00
|
|
|
* up. This is primarily determined by the mode: a `ByValue` datum
|
|
|
|
* will always be cleaned up by revoking cleanup using
|
|
|
|
* `revoke_clean()`, because there is no other option. By ref datums
|
|
|
|
* can sometimes be cleaned up via `revoke_clean` (in particular,
|
|
|
|
* by-ref datums that originated from rvalues), but sometimes they
|
|
|
|
* must be zeroed. This is indicated by the `DatumCleanup`
|
|
|
|
* parameter. Note that zeroing a by-ref datum *always works* to
|
|
|
|
* cancel the cleanup, but using `revoke_clean` is preferable since
|
|
|
|
* there is no runtime cost. Some older parts of the code (notably
|
|
|
|
* `match_`, at least at the time of this writing) rely on this and
|
|
|
|
* only use zeroing.
|
2012-08-28 17:54:45 -05:00
|
|
|
*
|
2013-01-10 12:59:58 -06:00
|
|
|
* # Copying, moving, and storing
|
2012-08-28 17:54:45 -05:00
|
|
|
*
|
2013-01-10 12:59:58 -06:00
|
|
|
* There are three methods for moving the value into a new
|
|
|
|
* location:
|
2012-08-28 17:54:45 -05:00
|
|
|
*
|
2013-01-10 12:59:58 -06:00
|
|
|
* - `copy_to()` will copy the value into a new location, meaning that
|
|
|
|
* the value is first mem-copied and then the new location is "taken"
|
|
|
|
* via the take glue, in effect creating a deep clone.
|
2012-08-28 17:54:45 -05:00
|
|
|
*
|
2013-01-10 12:59:58 -06:00
|
|
|
* - `move_to()` will copy the value, meaning that the value is mem-copied
|
|
|
|
* into its new home and then the cleanup on the this datum is revoked.
|
|
|
|
* This is a "shallow" clone. After `move_to()`, the current datum
|
|
|
|
* is invalid and should no longer be used.
|
2012-08-28 17:54:45 -05:00
|
|
|
*
|
2013-01-10 12:59:58 -06:00
|
|
|
* - `store_to()` either performs a copy or a move by consulting the
|
|
|
|
* moves_map computed by `middle::moves`.
|
2012-08-28 17:54:45 -05:00
|
|
|
*
|
|
|
|
* # Scratch datum
|
|
|
|
*
|
|
|
|
* Sometimes you just need some temporary scratch space. The
|
|
|
|
* `scratch_datum()` function will yield you up a by-ref datum that
|
|
|
|
* points into the stack. It's your responsibility to ensure that
|
|
|
|
* whatever you put in there gets cleaned up etc.
|
|
|
|
*
|
|
|
|
* # Other actions
|
|
|
|
*
|
|
|
|
* There are various other helper methods on Datum, such as `deref()`,
|
|
|
|
* `get_base_and_len()` and so forth. These are documented on the
|
|
|
|
* methods themselves. Most are only suitable for some types of
|
|
|
|
* values. */
|
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2013-02-25 13:11:21 -06:00
|
|
|
use lib;
|
2012-08-28 17:54:45 -05:00
|
|
|
use lib::llvm::ValueRef;
|
2013-02-24 16:01:43 -06:00
|
|
|
use middle::trans::adt;
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::trans::base::*;
|
|
|
|
use middle::trans::build::*;
|
|
|
|
use middle::trans::common::*;
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::trans::common;
|
2013-02-25 13:11:21 -06:00
|
|
|
use middle::trans::expr;
|
|
|
|
use middle::trans::glue;
|
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-04 13:29:32 -05:00
|
|
|
use middle::trans::write_guard;
|
2013-02-25 13:11:21 -06:00
|
|
|
use middle::ty;
|
2012-08-28 17:54:45 -05:00
|
|
|
use util::common::indenter;
|
2012-12-13 15:05:22 -06:00
|
|
|
use util::ppaux::ty_to_str;
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-05-24 21:35:29 -05:00
|
|
|
use core::uint;
|
2013-02-25 13:11:21 -06:00
|
|
|
use syntax::ast;
|
2013-05-01 08:14:47 -05:00
|
|
|
use syntax::codemap::span;
|
2012-12-23 16:41:37 -06:00
|
|
|
use syntax::parse::token::special_idents;
|
|
|
|
|
2013-03-20 10:40:02 -05:00
|
|
|
#[deriving(Eq)]
|
2013-01-29 19:57:02 -06:00
|
|
|
pub enum CopyAction {
|
2012-08-28 17:54:45 -05:00
|
|
|
INIT,
|
|
|
|
DROP_EXISTING
|
|
|
|
}
|
|
|
|
|
2013-01-29 19:57:02 -06:00
|
|
|
pub struct Datum {
|
2012-08-28 17:54:45 -05:00
|
|
|
/// The llvm value. This is either a pointer to the Rust value or
|
|
|
|
/// the value itself, depending on `mode` below.
|
2012-09-07 16:50:47 -05:00
|
|
|
val: ValueRef,
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
/// The rust type of the value.
|
2012-09-07 16:50:47 -05:00
|
|
|
ty: ty::t,
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
/// Indicates whether this is by-ref or by-value.
|
2012-09-07 16:50:47 -05:00
|
|
|
mode: DatumMode,
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-01-29 19:57:02 -06:00
|
|
|
pub struct DatumBlock {
|
2012-09-07 16:50:47 -05:00
|
|
|
bcx: block,
|
|
|
|
datum: Datum,
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-05-29 14:49:23 -05:00
|
|
|
#[deriving(Eq, IterBytes)]
|
2013-01-29 19:57:02 -06:00
|
|
|
pub enum DatumMode {
|
2013-05-29 14:49:23 -05:00
|
|
|
/// `val` is a pointer to the actual value (and thus has type *T).
|
|
|
|
/// The argument indicates how to cancel cleanup of this datum if
|
|
|
|
/// the value is moved elsewhere, which can either be by zeroing
|
|
|
|
/// the memory or by canceling a registered cleanup.
|
|
|
|
ByRef(DatumCleanup),
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
/// `val` is the actual value (*only used for immediates* like ints, ptrs)
|
|
|
|
ByValue,
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl DatumMode {
|
|
|
|
pub fn is_by_ref(&self) -> bool {
|
2013-05-29 14:49:23 -05:00
|
|
|
match *self { ByRef(_) => true, ByValue => false }
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn is_by_value(&self) -> bool {
|
2013-05-29 14:49:23 -05:00
|
|
|
match *self { ByRef(_) => false, ByValue => true }
|
2013-05-03 12:08:08 -05:00
|
|
|
}
|
|
|
|
}
|
2012-09-20 14:29:15 -05:00
|
|
|
|
2013-01-10 12:59:58 -06:00
|
|
|
/// See `Datum cleanup styles` section at the head of this module.
|
2013-05-29 14:49:23 -05:00
|
|
|
#[deriving(Eq, IterBytes)]
|
2013-01-10 12:59:58 -06:00
|
|
|
pub enum DatumCleanup {
|
|
|
|
RevokeClean,
|
|
|
|
ZeroMem
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-01-29 19:57:02 -06:00
|
|
|
pub fn immediate_rvalue(val: ValueRef, ty: ty::t) -> Datum {
|
2013-05-29 14:49:23 -05:00
|
|
|
return Datum {val: val, ty: ty, mode: ByValue};
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-01-29 19:57:02 -06:00
|
|
|
pub fn immediate_rvalue_bcx(bcx: block,
|
|
|
|
val: ValueRef,
|
|
|
|
ty: ty::t)
|
|
|
|
-> DatumBlock {
|
2012-08-28 17:54:45 -05:00
|
|
|
return DatumBlock {bcx: bcx, datum: immediate_rvalue(val, ty)};
|
|
|
|
}
|
|
|
|
|
2013-01-29 19:57:02 -06:00
|
|
|
pub fn scratch_datum(bcx: block, ty: ty::t, zero: bool) -> Datum {
|
2012-08-28 17:54:45 -05:00
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* Allocates temporary space on the stack using alloca() and
|
2012-09-11 23:25:01 -05:00
|
|
|
* returns a by-ref Datum pointing to it. If `zero` is true, the
|
|
|
|
* space will be zeroed when it is allocated; this is normally not
|
|
|
|
* necessary, but in the case of automatic rooting in match
|
|
|
|
* statements it is possible to have temporaries that may not get
|
|
|
|
* initialized if a certain arm is not taken, so we must zero
|
|
|
|
* them. You must arrange any cleanups etc yourself! */
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2012-09-07 11:17:27 -05:00
|
|
|
let llty = type_of::type_of(bcx.ccx(), ty);
|
|
|
|
let scratch = alloca_maybe_zeroed(bcx, llty, zero);
|
2013-05-29 14:49:23 -05:00
|
|
|
Datum { val: scratch, ty: ty, mode: ByRef(RevokeClean) }
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-01-29 19:57:02 -06:00
|
|
|
pub fn appropriate_mode(ty: ty::t) -> DatumMode {
|
2012-09-20 14:29:15 -05:00
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* Indicates the "appropriate" mode for this value,
|
|
|
|
* which is either by ref or by value, depending
|
2012-09-26 21:40:05 -05:00
|
|
|
* on whether type is immediate or not. */
|
2012-09-20 14:29:15 -05:00
|
|
|
|
|
|
|
if ty::type_is_nil(ty) || ty::type_is_bot(ty) {
|
|
|
|
ByValue
|
|
|
|
} else if ty::type_is_immediate(ty) {
|
|
|
|
ByValue
|
|
|
|
} else {
|
2013-05-29 14:49:23 -05:00
|
|
|
ByRef(RevokeClean)
|
2012-09-20 14:29:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl Datum {
|
|
|
|
pub fn store_to(&self,
|
|
|
|
bcx: block,
|
|
|
|
id: ast::node_id,
|
|
|
|
action: CopyAction,
|
|
|
|
dst: ValueRef)
|
|
|
|
-> block {
|
2012-08-28 17:54:45 -05:00
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* Stores this value into its final home. This moves if
|
2013-01-10 12:59:58 -06:00
|
|
|
* `id` is located in the move table, but copies otherwise.
|
|
|
|
*/
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2013-03-22 21:26:41 -05:00
|
|
|
if bcx.ccx().maps.moves_map.contains(&id) {
|
2012-08-28 17:54:45 -05:00
|
|
|
self.move_to(bcx, action, dst)
|
|
|
|
} else {
|
|
|
|
self.copy_to(bcx, action, dst)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn store_to_dest(&self,
|
|
|
|
bcx: block,
|
|
|
|
id: ast::node_id,
|
|
|
|
dest: expr::Dest)
|
|
|
|
-> block {
|
2012-08-28 17:54:45 -05:00
|
|
|
match dest {
|
|
|
|
expr::Ignore => {
|
|
|
|
return bcx;
|
|
|
|
}
|
|
|
|
expr::SaveIn(addr) => {
|
2013-01-10 12:59:58 -06:00
|
|
|
return self.store_to(bcx, id, INIT, addr);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn store_to_datum(&self,
|
|
|
|
bcx: block,
|
|
|
|
id: ast::node_id,
|
|
|
|
action: CopyAction,
|
|
|
|
datum: Datum)
|
|
|
|
-> block {
|
2012-08-28 17:54:45 -05:00
|
|
|
debug!("store_to_datum(self=%s, action=%?, datum=%s)",
|
|
|
|
self.to_str(bcx.ccx()), action, datum.to_str(bcx.ccx()));
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(datum.mode.is_by_ref());
|
2013-01-10 12:59:58 -06:00
|
|
|
self.store_to(bcx, id, action, datum.val)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn move_to_datum(&self, bcx: block, action: CopyAction, datum: Datum)
|
|
|
|
-> block {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(datum.mode.is_by_ref());
|
2012-08-28 17:54:45 -05:00
|
|
|
self.move_to(bcx, action, datum.val)
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn copy_to_datum(&self, bcx: block, action: CopyAction, datum: Datum)
|
|
|
|
-> block {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(datum.mode.is_by_ref());
|
2012-08-28 17:54:45 -05:00
|
|
|
self.copy_to(bcx, action, datum.val)
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn copy_to(&self, bcx: block, action: CopyAction, dst: ValueRef)
|
|
|
|
-> block {
|
2012-08-28 17:54:45 -05:00
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* Copies the value into `dst`, which should be a pointer to a
|
|
|
|
* memory location suitable for `self.ty`. You PROBABLY want
|
|
|
|
* `store_to()` instead, which will move if possible but copy if
|
|
|
|
* neccessary. */
|
|
|
|
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("copy_to");
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2012-09-07 14:06:42 -05:00
|
|
|
if ty::type_is_nil(self.ty) || ty::type_is_bot(self.ty) {
|
|
|
|
return bcx;
|
|
|
|
}
|
|
|
|
|
2012-08-28 17:54:45 -05:00
|
|
|
debug!("copy_to(self=%s, action=%?, dst=%s)",
|
2013-06-14 22:16:03 -05:00
|
|
|
self.to_str(bcx.ccx()), action, bcx.val_to_str(dst));
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
// Watch out for the case where we are writing the copying the
|
|
|
|
// value into the same location we read it out from. We want
|
|
|
|
// to avoid the case where we drop the existing value, which
|
|
|
|
// frees it, and then overwrite it with itself (which has been
|
|
|
|
// freed).
|
|
|
|
if action == DROP_EXISTING &&
|
|
|
|
ty::type_needs_drop(bcx.tcx(), self.ty)
|
|
|
|
{
|
|
|
|
match self.mode {
|
2013-05-29 14:49:23 -05:00
|
|
|
ByRef(_) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
let cast = PointerCast(bcx, dst, val_ty(self.val));
|
|
|
|
let cmp = ICmp(bcx, lib::llvm::IntNE, cast, self.val);
|
|
|
|
do with_cond(bcx, cmp) |bcx| {
|
|
|
|
self.copy_to_no_check(bcx, action, dst)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ByValue => {
|
|
|
|
self.copy_to_no_check(bcx, action, dst)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.copy_to_no_check(bcx, action, dst)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn copy_to_no_check(&self,
|
|
|
|
bcx: block,
|
|
|
|
action: CopyAction,
|
|
|
|
dst: ValueRef)
|
|
|
|
-> block {
|
2012-08-28 17:54:45 -05:00
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* A helper for `copy_to()` which does not check to see if we
|
|
|
|
* are copying to/from the same value. */
|
|
|
|
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("copy_to_no_check");
|
2012-08-28 17:54:45 -05:00
|
|
|
let mut bcx = bcx;
|
|
|
|
|
|
|
|
if action == DROP_EXISTING {
|
|
|
|
bcx = glue::drop_ty(bcx, dst, self.ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.mode {
|
|
|
|
ByValue => {
|
|
|
|
Store(bcx, self.val, dst);
|
|
|
|
}
|
2013-05-29 14:49:23 -05:00
|
|
|
ByRef(_) => {
|
2013-02-25 13:11:21 -06:00
|
|
|
memcpy_ty(bcx, dst, self.val, self.ty);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return glue::take_ty(bcx, dst, self.ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This works like copy_val, except that it deinitializes the source.
|
|
|
|
// Since it needs to zero out the source, src also needs to be an lval.
|
|
|
|
//
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn move_to(&self, bcx: block, action: CopyAction, dst: ValueRef)
|
|
|
|
-> block {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("move_to");
|
2012-08-28 17:54:45 -05:00
|
|
|
let mut bcx = bcx;
|
|
|
|
|
|
|
|
debug!("move_to(self=%s, action=%?, dst=%s)",
|
2013-06-14 22:16:03 -05:00
|
|
|
self.to_str(bcx.ccx()), action, bcx.val_to_str(dst));
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
if ty::type_is_nil(self.ty) || ty::type_is_bot(self.ty) {
|
|
|
|
return bcx;
|
|
|
|
}
|
|
|
|
|
|
|
|
if action == DROP_EXISTING {
|
|
|
|
bcx = glue::drop_ty(bcx, dst, self.ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
match self.mode {
|
2013-05-29 14:49:23 -05:00
|
|
|
ByRef(_) => {
|
2013-02-25 13:11:21 -06:00
|
|
|
memcpy_ty(bcx, dst, self.val, self.ty);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
ByValue => {
|
|
|
|
Store(bcx, self.val, dst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.cancel_clean(bcx);
|
|
|
|
|
|
|
|
return bcx;
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn add_clean(&self, bcx: block) {
|
2012-08-28 17:54:45 -05:00
|
|
|
/*!
|
|
|
|
* Schedules this datum for cleanup in `bcx`. The datum
|
2013-05-29 14:49:23 -05:00
|
|
|
* must be an rvalue.
|
|
|
|
*/
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
match self.mode {
|
|
|
|
ByValue => {
|
|
|
|
add_clean_temp_immediate(bcx, self.val, self.ty);
|
|
|
|
}
|
2013-05-29 14:49:23 -05:00
|
|
|
ByRef(RevokeClean) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
add_clean_temp_mem(bcx, self.val, self.ty);
|
|
|
|
}
|
2013-05-29 14:49:23 -05:00
|
|
|
ByRef(ZeroMem) => {
|
|
|
|
bcx.tcx().sess.bug(
|
|
|
|
fmt!("Cannot add clean to a 'zero-mem' datum"));
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn cancel_clean(&self, bcx: block) {
|
2012-08-28 17:54:45 -05:00
|
|
|
if ty::type_needs_drop(bcx.tcx(), self.ty) {
|
2013-05-29 14:49:23 -05:00
|
|
|
match self.mode {
|
|
|
|
ByValue |
|
|
|
|
ByRef(RevokeClean) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
revoke_clean(bcx, self.val);
|
|
|
|
}
|
2013-05-29 14:49:23 -05:00
|
|
|
ByRef(ZeroMem) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
// Lvalues which potentially need to be dropped
|
|
|
|
// must be passed by ref, so that we can zero them
|
|
|
|
// out.
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(self.mode.is_by_ref());
|
2012-08-28 17:54:45 -05:00
|
|
|
zero_mem(bcx, self.val, self.ty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn to_str(&self, ccx: &CrateContext) -> ~str {
|
2013-05-29 14:49:23 -05:00
|
|
|
fmt!("Datum { val=%s, ty=%s, mode=%? }",
|
2013-06-14 22:16:03 -05:00
|
|
|
ccx.tn.val_to_str(self.val),
|
2012-08-28 17:54:45 -05:00
|
|
|
ty_to_str(ccx.tcx, self.ty),
|
2013-05-29 14:49:23 -05:00
|
|
|
self.mode)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn to_value_datum(&self, bcx: block) -> Datum {
|
2012-09-06 17:21:42 -05:00
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* Yields a by-ref form of this datum. This may involve
|
|
|
|
* creation of a temporary stack slot. The value returned by
|
|
|
|
* this function is not separately rooted from this datum, so
|
|
|
|
* it will not live longer than the current datum. */
|
|
|
|
|
|
|
|
match self.mode {
|
2013-02-22 00:41:37 -06:00
|
|
|
ByValue => *self,
|
2013-05-29 14:49:23 -05:00
|
|
|
ByRef(_) => {
|
2012-09-06 17:21:42 -05:00
|
|
|
Datum {val: self.to_value_llval(bcx), mode: ByValue,
|
2013-05-29 14:49:23 -05:00
|
|
|
ty: self.ty}
|
2012-09-06 17:21:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn to_value_llval(&self, bcx: block) -> ValueRef {
|
2012-08-28 17:54:45 -05:00
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* Yields the value itself. */
|
|
|
|
|
2012-09-06 17:21:42 -05:00
|
|
|
if ty::type_is_nil(self.ty) || ty::type_is_bot(self.ty) {
|
|
|
|
C_nil()
|
|
|
|
} else {
|
|
|
|
match self.mode {
|
|
|
|
ByValue => self.val,
|
2013-05-29 14:49:23 -05:00
|
|
|
ByRef(_) => {
|
2013-02-19 18:15:03 -06:00
|
|
|
if ty::type_is_bool(self.ty) {
|
|
|
|
LoadRangeAssert(bcx, self.val, 0, 2, lib::llvm::True)
|
|
|
|
} else {
|
|
|
|
Load(bcx, self.val)
|
|
|
|
}
|
|
|
|
}
|
2012-09-06 17:21:42 -05:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn to_ref_datum(&self, bcx: block) -> Datum {
|
2012-08-28 17:54:45 -05:00
|
|
|
/*!
|
|
|
|
* Yields a by-ref form of this datum. This may involve
|
|
|
|
* creation of a temporary stack slot. The value returned by
|
|
|
|
* this function is not separately rooted from this datum, so
|
2013-05-29 14:49:23 -05:00
|
|
|
* it will not live longer than the current datum.
|
|
|
|
*/
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
match self.mode {
|
2013-05-29 14:49:23 -05:00
|
|
|
ByRef(_) => *self,
|
2012-08-28 17:54:45 -05:00
|
|
|
ByValue => {
|
2013-05-29 14:49:23 -05:00
|
|
|
Datum {val: self.to_ref_llval(bcx), mode: ByRef(RevokeClean),
|
|
|
|
ty: self.ty}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn to_ref_llval(&self, bcx: block) -> ValueRef {
|
2012-08-28 17:54:45 -05:00
|
|
|
match self.mode {
|
2013-05-29 14:49:23 -05:00
|
|
|
ByRef(_) => self.val,
|
2012-08-28 17:54:45 -05:00
|
|
|
ByValue => {
|
2012-09-06 17:21:42 -05:00
|
|
|
if ty::type_is_nil(self.ty) || ty::type_is_bot(self.ty) {
|
2013-06-15 22:45:48 -05:00
|
|
|
C_null(type_of::type_of(bcx.ccx(), self.ty).ptr_to())
|
2012-09-06 17:21:42 -05:00
|
|
|
} else {
|
|
|
|
let slot = alloc_ty(bcx, self.ty);
|
|
|
|
Store(bcx, self.val, slot);
|
|
|
|
slot
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn to_zeroable_ref_llval(&self, bcx: block) -> ValueRef {
|
2013-05-29 17:33:30 -05:00
|
|
|
/*!
|
|
|
|
* Returns a by-ref llvalue that can be zeroed in order to
|
|
|
|
* cancel cleanup. This is a kind of hokey bridge used
|
|
|
|
* to adapt to the match code. Please don't use it for new code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
match self.mode {
|
|
|
|
// All by-ref datums are zeroable, even if we *could* just
|
|
|
|
// cancel the cleanup.
|
|
|
|
ByRef(_) => self.val,
|
|
|
|
|
|
|
|
// By value datums can't be zeroed (where would you store
|
|
|
|
// the zero?) so we have to spill them. Add a temp cleanup
|
|
|
|
// for this spilled value and cancel the cleanup on this
|
|
|
|
// current value.
|
|
|
|
ByValue => {
|
|
|
|
let slot = self.to_ref_llval(bcx);
|
|
|
|
self.cancel_clean(bcx);
|
|
|
|
add_clean_temp_mem(bcx, slot, self.ty);
|
|
|
|
slot
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn appropriate_mode(&self) -> DatumMode {
|
2012-09-20 14:29:15 -05:00
|
|
|
/*! See the `appropriate_mode()` function */
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2012-09-20 14:29:15 -05:00
|
|
|
appropriate_mode(self.ty)
|
2012-09-06 17:21:42 -05:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn to_appropriate_llval(&self, bcx: block) -> ValueRef {
|
2012-09-06 17:21:42 -05:00
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* Yields an llvalue with the `appropriate_mode()`. */
|
|
|
|
|
|
|
|
match self.appropriate_mode() {
|
|
|
|
ByValue => self.to_value_llval(bcx),
|
2013-05-29 14:49:23 -05:00
|
|
|
ByRef(_) => self.to_ref_llval(bcx)
|
2012-09-06 17:21:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn to_appropriate_datum(&self, bcx: block) -> Datum {
|
2012-09-06 17:21:42 -05:00
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* Yields a datum with the `appropriate_mode()`. */
|
|
|
|
|
|
|
|
match self.appropriate_mode() {
|
|
|
|
ByValue => self.to_value_datum(bcx),
|
2013-05-29 14:49:23 -05:00
|
|
|
ByRef(_) => self.to_ref_datum(bcx)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn get_element(&self,
|
|
|
|
bcx: block,
|
|
|
|
ty: ty::t,
|
|
|
|
source: DatumCleanup,
|
|
|
|
gep: &fn(ValueRef) -> ValueRef)
|
|
|
|
-> Datum {
|
2012-08-28 17:54:45 -05:00
|
|
|
let base_val = self.to_ref_llval(bcx);
|
|
|
|
Datum {
|
2013-02-24 14:42:39 -06:00
|
|
|
val: gep(base_val),
|
2013-05-29 14:49:23 -05:00
|
|
|
mode: ByRef(source),
|
2012-08-28 17:54:45 -05:00
|
|
|
ty: ty,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn drop_val(&self, bcx: block) -> block {
|
2012-08-28 17:54:45 -05:00
|
|
|
if !ty::type_needs_drop(bcx.tcx(), self.ty) {
|
|
|
|
return bcx;
|
|
|
|
}
|
|
|
|
|
|
|
|
return match self.mode {
|
2013-05-29 14:49:23 -05:00
|
|
|
ByRef(_) => glue::drop_ty(bcx, self.val, self.ty),
|
2012-08-28 17:54:45 -05:00
|
|
|
ByValue => glue::drop_ty_immediate(bcx, self.val, self.ty)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn box_body(&self, bcx: block) -> Datum {
|
2012-08-28 17:54:45 -05:00
|
|
|
/*!
|
|
|
|
*
|
|
|
|
* This datum must represent an @T or ~T box. Returns a new
|
|
|
|
* by-ref datum of type T, pointing at the contents. */
|
|
|
|
|
2012-09-11 18:20:31 -05:00
|
|
|
let content_ty = match ty::get(self.ty).sty {
|
2012-08-28 17:54:45 -05:00
|
|
|
ty::ty_box(mt) | ty::ty_uniq(mt) => mt.ty,
|
|
|
|
_ => {
|
|
|
|
bcx.tcx().sess.bug(fmt!(
|
|
|
|
"box_body() invoked on non-box type %s",
|
|
|
|
ty_to_str(bcx.tcx(), self.ty)));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let ptr = self.to_value_llval(bcx);
|
|
|
|
let body = opaque_box_body(bcx, content_ty, ptr);
|
2013-05-29 14:49:23 -05:00
|
|
|
Datum {val: body, ty: content_ty, mode: ByRef(ZeroMem)}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn to_rptr(&self, bcx: block) -> Datum {
|
2013-05-07 10:41:27 -05:00
|
|
|
//! Returns a new datum of region-pointer type containing the
|
|
|
|
//! the same ptr as this datum (after converting to by-ref
|
|
|
|
//! using `to_ref_llval()`).
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
// Convert to ref, yielding lltype *T. Then create a Rust
|
2013-03-14 13:22:51 -05:00
|
|
|
// type &'static T (which translates to *T). Construct new
|
2012-08-28 17:54:45 -05:00
|
|
|
// result (which will be by-value). Note that it is not
|
|
|
|
// significant *which* region we pick here.
|
|
|
|
let llval = self.to_ref_llval(bcx);
|
|
|
|
let rptr_ty = ty::mk_imm_rptr(bcx.tcx(), ty::re_static,
|
|
|
|
self.ty);
|
2013-05-29 14:49:23 -05:00
|
|
|
Datum {val: llval, ty: rptr_ty, mode: ByValue}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
/// bcx: Block wherein to generate insns.
|
|
|
|
/// span: Location where deref occurs.
|
|
|
|
/// expr_id: ID of deref expr.
|
|
|
|
/// derefs: Number of times deref'd already.
|
|
|
|
/// is_auto: If true, only deref if auto-derefable.
|
|
|
|
pub fn try_deref(&self,
|
|
|
|
bcx: block,
|
|
|
|
span: span,
|
|
|
|
expr_id: ast::node_id,
|
|
|
|
derefs: uint,
|
|
|
|
is_auto: bool)
|
|
|
|
-> (Option<Datum>, block) {
|
2012-08-28 17:54:45 -05:00
|
|
|
let ccx = bcx.ccx();
|
|
|
|
|
2013-05-03 15:26:43 -05:00
|
|
|
debug!("try_deref(expr_id=%?, derefs=%?, is_auto=%b, self=%?)",
|
2012-08-28 17:54:45 -05:00
|
|
|
expr_id, derefs, is_auto, self.to_str(bcx.ccx()));
|
2013-01-11 23:01:42 -06:00
|
|
|
|
2013-05-04 13:29:32 -05:00
|
|
|
let bcx =
|
|
|
|
write_guard::root_and_write_guard(
|
|
|
|
self, bcx, span, expr_id, derefs);
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2012-09-11 18:20:31 -05:00
|
|
|
match ty::get(self.ty).sty {
|
2012-08-28 17:54:45 -05:00
|
|
|
ty::ty_box(_) | ty::ty_uniq(_) => {
|
2013-01-11 23:01:42 -06:00
|
|
|
return (Some(self.box_body(bcx)), bcx);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
ty::ty_ptr(mt) => {
|
|
|
|
if is_auto { // unsafe ptrs are not AUTO-derefable
|
2013-01-11 23:01:42 -06:00
|
|
|
return (None, bcx);
|
2012-08-28 17:54:45 -05:00
|
|
|
} else {
|
2013-02-22 00:41:37 -06:00
|
|
|
return (Some(deref_ptr(bcx, self, mt.ty)), bcx);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ty::ty_rptr(_, mt) => {
|
2013-02-22 00:41:37 -06:00
|
|
|
return (Some(deref_ptr(bcx, self, mt.ty)), bcx);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
ty::ty_enum(did, ref substs) => {
|
|
|
|
// Check whether this enum is a newtype enum:
|
|
|
|
let variants = ty::enum_variants(ccx.tcx, did);
|
2012-10-27 19:16:00 -05:00
|
|
|
if (*variants).len() != 1 || variants[0].args.len() != 1 {
|
2013-01-11 23:01:42 -06:00
|
|
|
return (None, bcx);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-02-24 16:01:43 -06:00
|
|
|
let repr = adt::represent_type(ccx, self.ty);
|
2012-08-28 17:54:45 -05:00
|
|
|
let ty = ty::subst(ccx.tcx, substs, variants[0].args[0]);
|
|
|
|
return match self.mode {
|
2013-05-29 14:49:23 -05:00
|
|
|
ByRef(_) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
// Recast lv.val as a pointer to the newtype
|
|
|
|
// rather than a ptr to the enum type.
|
2013-01-11 23:01:42 -06:00
|
|
|
(
|
|
|
|
Some(Datum {
|
2013-03-02 18:08:49 -06:00
|
|
|
val: adt::trans_field_ptr(bcx, repr, self.val,
|
2013-02-24 16:01:43 -06:00
|
|
|
0, 0),
|
2013-01-11 23:01:42 -06:00
|
|
|
ty: ty,
|
2013-05-29 14:49:23 -05:00
|
|
|
mode: ByRef(ZeroMem)
|
2013-01-11 23:01:42 -06:00
|
|
|
}),
|
|
|
|
bcx
|
|
|
|
)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
ByValue => {
|
|
|
|
// Actually, this case cannot happen right
|
|
|
|
// now, because enums are never immediate.
|
|
|
|
// But in principle newtype'd immediate
|
|
|
|
// values should be immediate, and in that
|
|
|
|
// case the * would be a no-op except for
|
|
|
|
// changing the type, so I am putting this
|
|
|
|
// code in place here to do the right
|
|
|
|
// thing if this change ever goes through.
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(ty::type_is_immediate(ty));
|
2013-02-22 00:41:37 -06:00
|
|
|
(Some(Datum {ty: ty, ..*self}), bcx)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2012-12-10 15:47:54 -06:00
|
|
|
ty::ty_struct(did, ref substs) => {
|
2012-10-30 20:31:47 -05:00
|
|
|
// Check whether this struct is a newtype struct.
|
2012-12-10 15:47:54 -06:00
|
|
|
let fields = ty::struct_fields(ccx.tcx, did, substs);
|
2012-10-30 20:31:47 -05:00
|
|
|
if fields.len() != 1 || fields[0].ident !=
|
2012-12-23 16:41:37 -06:00
|
|
|
special_idents::unnamed_field {
|
2013-01-11 23:01:42 -06:00
|
|
|
return (None, bcx);
|
2012-10-30 20:31:47 -05:00
|
|
|
}
|
|
|
|
|
2013-02-24 16:01:43 -06:00
|
|
|
let repr = adt::represent_type(ccx, self.ty);
|
2012-10-30 20:31:47 -05:00
|
|
|
let ty = fields[0].mt.ty;
|
|
|
|
return match self.mode {
|
2013-05-29 14:49:23 -05:00
|
|
|
ByRef(_) => {
|
2012-10-30 20:31:47 -05:00
|
|
|
// Recast lv.val as a pointer to the newtype rather
|
|
|
|
// than a pointer to the struct type.
|
2013-05-17 19:27:44 -05:00
|
|
|
// FIXME #6572: This isn't correct for structs with
|
2012-10-30 20:31:47 -05:00
|
|
|
// destructors.
|
2013-01-11 23:01:42 -06:00
|
|
|
(
|
|
|
|
Some(Datum {
|
2013-03-02 18:08:49 -06:00
|
|
|
val: adt::trans_field_ptr(bcx, repr, self.val,
|
2013-02-24 16:01:43 -06:00
|
|
|
0, 0),
|
2013-01-11 23:01:42 -06:00
|
|
|
ty: ty,
|
2013-05-29 14:49:23 -05:00
|
|
|
mode: ByRef(ZeroMem)
|
2013-01-11 23:01:42 -06:00
|
|
|
}),
|
|
|
|
bcx
|
|
|
|
)
|
2012-10-30 20:31:47 -05:00
|
|
|
}
|
|
|
|
ByValue => {
|
|
|
|
// Actually, this case cannot happen right now,
|
|
|
|
// because structs are never immediate. But in
|
|
|
|
// principle, newtype'd immediate values should be
|
|
|
|
// immediate, and in that case the * would be a no-op
|
|
|
|
// except for changing the type, so I am putting this
|
|
|
|
// code in place here to do the right thing if this
|
|
|
|
// change ever goes through.
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(ty::type_is_immediate(ty));
|
2013-02-22 00:41:37 -06:00
|
|
|
(Some(Datum {ty: ty, ..*self}), bcx)
|
2012-10-30 20:31:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
_ => { // not derefable.
|
2013-01-11 23:01:42 -06:00
|
|
|
return (None, bcx);
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deref_ptr(bcx: block, lv: &Datum, ty: ty::t) -> Datum {
|
|
|
|
Datum {
|
|
|
|
val: lv.to_value_llval(bcx),
|
|
|
|
ty: ty,
|
2013-05-29 14:49:23 -05:00
|
|
|
mode: ByRef(ZeroMem)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
/// expr: The deref expression.
|
2013-06-27 08:04:22 -05:00
|
|
|
pub fn deref(&self, bcx: block, expr: &ast::expr, derefs: uint)
|
2013-05-31 17:17:22 -05:00
|
|
|
-> DatumBlock {
|
2013-05-01 08:14:47 -05:00
|
|
|
match self.try_deref(bcx, expr.span, expr.id, derefs, false) {
|
2013-01-11 23:01:42 -06:00
|
|
|
(Some(lvres), bcx) => DatumBlock { bcx: bcx, datum: lvres },
|
|
|
|
(None, _) => {
|
2013-05-02 11:28:53 -05:00
|
|
|
bcx.ccx().sess.span_bug(expr.span,
|
|
|
|
"Cannot deref this expression");
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn autoderef(&self,
|
|
|
|
bcx: block,
|
|
|
|
span: span,
|
|
|
|
expr_id: ast::node_id,
|
|
|
|
max: uint)
|
|
|
|
-> DatumBlock {
|
2013-06-16 23:23:24 -05:00
|
|
|
let _icx = push_ctxt("autoderef");
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
debug!("autoderef(expr_id=%d, max=%?, self=%?)",
|
|
|
|
expr_id, max, self.to_str(bcx.ccx()));
|
|
|
|
let _indenter = indenter();
|
|
|
|
|
2013-02-22 00:41:37 -06:00
|
|
|
let mut datum = *self;
|
2012-08-28 17:54:45 -05:00
|
|
|
let mut derefs = 0u;
|
2013-01-11 23:01:42 -06:00
|
|
|
let mut bcx = bcx;
|
2012-08-28 17:54:45 -05:00
|
|
|
while derefs < max {
|
|
|
|
derefs += 1u;
|
2013-05-01 08:14:47 -05:00
|
|
|
match datum.try_deref(bcx, span, expr_id, derefs, true) {
|
2013-01-11 23:01:42 -06:00
|
|
|
(None, new_bcx) => { bcx = new_bcx; break }
|
|
|
|
(Some(datum_deref), new_bcx) => {
|
2012-08-28 17:54:45 -05:00
|
|
|
datum = datum_deref;
|
2013-01-11 23:01:42 -06:00
|
|
|
bcx = new_bcx;
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// either we were asked to deref a specific number of times,
|
|
|
|
// in which case we should have, or we asked to deref as many
|
|
|
|
// times as we can
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(derefs == max || max == uint::max_value);
|
2013-01-11 23:01:42 -06:00
|
|
|
DatumBlock { bcx: bcx, datum: datum }
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn get_vec_base_and_len(&self,
|
|
|
|
mut bcx: block,
|
|
|
|
span: span,
|
|
|
|
expr_id: ast::node_id,
|
|
|
|
derefs: uint)
|
|
|
|
-> (block, ValueRef, ValueRef) {
|
2013-05-03 15:26:43 -05:00
|
|
|
//! Converts a vector into the slice pair. Performs rooting
|
|
|
|
//! and write guards checks.
|
|
|
|
|
|
|
|
// only imp't for @[] and @str, but harmless
|
2013-05-09 14:58:02 -05:00
|
|
|
bcx = write_guard::root_and_write_guard(self, bcx, span, expr_id, derefs);
|
2013-05-03 15:26:43 -05:00
|
|
|
let (base, len) = self.get_vec_base_and_len_no_root(bcx);
|
|
|
|
(bcx, base, len)
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn get_vec_base_and_len_no_root(&self, bcx: block)
|
|
|
|
-> (ValueRef, ValueRef) {
|
2013-05-03 15:26:43 -05:00
|
|
|
//! Converts a vector into the slice pair. Des not root
|
|
|
|
//! nor perform write guard checks.
|
|
|
|
|
|
|
|
let llval = self.to_appropriate_llval(bcx);
|
|
|
|
tvec::get_base_and_len(bcx, llval, self.ty)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn root_and_write_guard(&self,
|
|
|
|
bcx: block,
|
|
|
|
span: span,
|
|
|
|
expr_id: ast::node_id,
|
|
|
|
derefs: uint)
|
|
|
|
-> block {
|
2013-05-04 13:29:32 -05:00
|
|
|
write_guard::root_and_write_guard(self, bcx, span, expr_id, derefs)
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn to_result(&self, bcx: block) -> common::Result {
|
2012-08-28 17:54:45 -05:00
|
|
|
rslt(bcx, self.to_appropriate_llval(bcx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl DatumBlock {
|
|
|
|
pub fn unpack(&self, bcx: &mut block) -> Datum {
|
2012-08-28 17:54:45 -05:00
|
|
|
*bcx = self.bcx;
|
|
|
|
return self.datum;
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn assert_by_ref(&self) -> DatumBlock {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(self.datum.mode.is_by_ref());
|
2013-02-22 00:41:37 -06:00
|
|
|
*self
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn drop_val(&self) -> block {
|
2012-08-28 17:54:45 -05:00
|
|
|
self.datum.drop_val(self.bcx)
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn store_to(&self,
|
|
|
|
id: ast::node_id,
|
|
|
|
action: CopyAction,
|
|
|
|
dst: ValueRef)
|
|
|
|
-> block {
|
2013-01-10 12:59:58 -06:00
|
|
|
self.datum.store_to(self.bcx, id, action, dst)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn copy_to(&self, action: CopyAction, dst: ValueRef) -> block {
|
2012-08-28 17:54:45 -05:00
|
|
|
self.datum.copy_to(self.bcx, action, dst)
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn move_to(&self, action: CopyAction, dst: ValueRef) -> block {
|
2012-08-28 17:54:45 -05:00
|
|
|
self.datum.move_to(self.bcx, action, dst)
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn to_value_llval(&self) -> ValueRef {
|
2012-08-28 17:54:45 -05:00
|
|
|
self.datum.to_value_llval(self.bcx)
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn to_result(&self) -> common::Result {
|
2012-08-28 17:54:45 -05:00
|
|
|
rslt(self.bcx, self.datum.to_appropriate_llval(self.bcx))
|
|
|
|
}
|
|
|
|
|
2013-06-13 02:19:50 -05:00
|
|
|
pub fn ccx(&self) -> @mut CrateContext {
|
2012-08-28 17:54:45 -05:00
|
|
|
self.bcx.ccx()
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn tcx(&self) -> ty::ctxt {
|
2012-08-28 17:54:45 -05:00
|
|
|
self.bcx.tcx()
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn to_str(&self) -> ~str {
|
2012-08-28 17:54:45 -05:00
|
|
|
self.datum.to_str(self.ccx())
|
|
|
|
}
|
|
|
|
}
|