2014-05-28 14:36:05 -05:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// 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.
|
|
|
|
|
2015-02-09 19:23:16 -06:00
|
|
|
//! ## The Datum module
|
|
|
|
//!
|
|
|
|
//! A `Datum` encapsulates the result of evaluating a Rust expression. It
|
|
|
|
//! contains a `ValueRef` indicating the result, a `Ty` describing
|
|
|
|
//! the Rust type, but also a *kind*. The kind indicates whether the datum
|
|
|
|
//! has cleanup scheduled (lvalue) or not (rvalue) and -- in the case of
|
|
|
|
//! rvalues -- whether or not the value is "by ref" or "by value".
|
|
|
|
//!
|
|
|
|
//! The datum API is designed to try and help you avoid memory errors like
|
|
|
|
//! forgetting to arrange cleanup or duplicating a value. The type of the
|
|
|
|
//! datum incorporates the kind, and thus reflects whether it has cleanup
|
|
|
|
//! scheduled:
|
|
|
|
//!
|
|
|
|
//! - `Datum<Lvalue>` -- by ref, cleanup scheduled
|
|
|
|
//! - `Datum<Rvalue>` -- by value or by ref, no cleanup scheduled
|
|
|
|
//! - `Datum<Expr>` -- either `Datum<Lvalue>` or `Datum<Rvalue>`
|
|
|
|
//!
|
|
|
|
//! Rvalue and expr datums are noncopyable, and most of the methods on
|
|
|
|
//! datums consume the datum itself (with some notable exceptions). This
|
|
|
|
//! reflects the fact that datums may represent affine values which ought
|
|
|
|
//! to be consumed exactly once, and if you were to try to (for example)
|
|
|
|
//! store an affine value multiple times, you would be duplicating it,
|
|
|
|
//! which would certainly be a bug.
|
|
|
|
//!
|
|
|
|
//! Some of the datum methods, however, are designed to work only on
|
|
|
|
//! copyable values such as ints or pointers. Those methods may borrow the
|
|
|
|
//! datum (`&self`) rather than consume it, but they always include
|
|
|
|
//! assertions on the type of the value represented to check that this
|
|
|
|
//! makes sense. An example is `shallow_copy()`, which duplicates
|
|
|
|
//! a datum value.
|
|
|
|
//!
|
|
|
|
//! Translating an expression always yields a `Datum<Expr>` result, but
|
|
|
|
//! the methods `to_[lr]value_datum()` can be used to coerce a
|
|
|
|
//! `Datum<Expr>` into a `Datum<Lvalue>` or `Datum<Rvalue>` as
|
|
|
|
//! needed. Coercing to an lvalue is fairly common, and generally occurs
|
|
|
|
//! whenever it is necessary to inspect a value and pull out its
|
|
|
|
//! subcomponents (for example, a match, or indexing expression). Coercing
|
|
|
|
//! to an rvalue is more unusual; it occurs when moving values from place
|
|
|
|
//! to place, such as in an assignment expression or parameter passing.
|
|
|
|
//!
|
|
|
|
//! ### Lvalues in detail
|
|
|
|
//!
|
|
|
|
//! An lvalue datum is one for which cleanup has been scheduled. Lvalue
|
|
|
|
//! datums are always located in memory, and thus the `ValueRef` for an
|
|
|
|
//! LLVM value is always a pointer to the actual Rust value. This means
|
|
|
|
//! that if the Datum has a Rust type of `int`, then the LLVM type of the
|
|
|
|
//! `ValueRef` will be `int*` (pointer to int).
|
|
|
|
//!
|
|
|
|
//! Because lvalues already have cleanups scheduled, the memory must be
|
|
|
|
//! zeroed to prevent the cleanup from taking place (presuming that the
|
|
|
|
//! Rust type needs drop in the first place, otherwise it doesn't
|
|
|
|
//! matter). The Datum code automatically performs this zeroing when the
|
|
|
|
//! value is stored to a new location, for example.
|
|
|
|
//!
|
|
|
|
//! Lvalues usually result from evaluating lvalue expressions. For
|
|
|
|
//! example, evaluating a local variable `x` yields an lvalue, as does a
|
|
|
|
//! reference to a field like `x.f` or an index `x[i]`.
|
|
|
|
//!
|
|
|
|
//! Lvalue datums can also arise by *converting* an rvalue into an lvalue.
|
|
|
|
//! This is done with the `to_lvalue_datum` method defined on
|
|
|
|
//! `Datum<Expr>`. Basically this method just schedules cleanup if the
|
|
|
|
//! datum is an rvalue, possibly storing the value into a stack slot first
|
|
|
|
//! if needed. Converting rvalues into lvalues occurs in constructs like
|
|
|
|
//! `&foo()` or `match foo() { ref x => ... }`, where the user is
|
|
|
|
//! implicitly requesting a temporary.
|
|
|
|
//!
|
|
|
|
//! ### Rvalues in detail
|
|
|
|
//!
|
|
|
|
//! Rvalues datums are values with no cleanup scheduled. One must be
|
|
|
|
//! careful with rvalue datums to ensure that cleanup is properly
|
|
|
|
//! arranged, usually by converting to an lvalue datum or by invoking the
|
|
|
|
//! `add_clean` method.
|
|
|
|
//!
|
|
|
|
//! ### Scratch datums
|
|
|
|
//!
|
|
|
|
//! Sometimes you need some temporary scratch space. The functions
|
|
|
|
//! `[lr]value_scratch_datum()` can be used to get temporary stack
|
|
|
|
//! space. As their name suggests, they yield lvalues and rvalues
|
|
|
|
//! respectively. That is, the slot from `lvalue_scratch_datum` will have
|
|
|
|
//! cleanup arranged, and the slot from `rvalue_scratch_datum` does not.
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2014-11-06 02:05:53 -06:00
|
|
|
pub use self::Expr::*;
|
|
|
|
pub use self::RvalueMode::*;
|
|
|
|
|
2014-07-07 19:58:01 -05:00
|
|
|
use llvm::ValueRef;
|
Add dropflag hints (stack-local booleans) for unfragmented paths in trans.
Added code to maintain these hints at runtime, and to conditionalize
drop-filling and calls to destructors.
In this early stage, we are using hints, so we are always free to
leave out a flag for a path -- then we just pass `None` as the
dropflag hint in the corresponding schedule cleanup call. But, once a
path has a hint, we must at least maintain it: i.e. if the hint
exists, we must ensure it is never set to "moved" if the data in
question might actually have been initialized. It remains sound to
conservatively set the hint to "initialized" as long as the true
drop-flag embedded in the value itself is up-to-date.
----
Here are some high-level details I want to point out:
* We maintain the hint in Lvalue::post_store, marking the lvalue as
moved. (But also continue drop-filling if necessary.)
* We update the hint on ExprAssign.
* We pass along the hint in once closures that capture-by-move.
* You only call `drop_ty` for state that does not have an associated hint.
If you have a hint, you must call `drop_ty_core` instead.
(Originally I passed the hint into `drop_ty` as well, to make the
connection to a hint more apparent, but the vast majority of
current calls to `drop_ty` are in contexts where no hint is
available, so it just seemed like noise in the resulting diff.)
2015-06-07 02:25:14 -05:00
|
|
|
use trans::adt;
|
2014-11-15 19:30:33 -06:00
|
|
|
use trans::base::*;
|
Add dropflag hints (stack-local booleans) for unfragmented paths in trans.
Added code to maintain these hints at runtime, and to conditionalize
drop-filling and calls to destructors.
In this early stage, we are using hints, so we are always free to
leave out a flag for a path -- then we just pass `None` as the
dropflag hint in the corresponding schedule cleanup call. But, once a
path has a hint, we must at least maintain it: i.e. if the hint
exists, we must ensure it is never set to "moved" if the data in
question might actually have been initialized. It remains sound to
conservatively set the hint to "initialized" as long as the true
drop-flag embedded in the value itself is up-to-date.
----
Here are some high-level details I want to point out:
* We maintain the hint in Lvalue::post_store, marking the lvalue as
moved. (But also continue drop-filling if necessary.)
* We update the hint on ExprAssign.
* We pass along the hint in once closures that capture-by-move.
* You only call `drop_ty` for state that does not have an associated hint.
If you have a hint, you must call `drop_ty_core` instead.
(Originally I passed the hint into `drop_ty` as well, to make the
connection to a hint more apparent, but the vast majority of
current calls to `drop_ty` are in contexts where no hint is
available, so it just seemed like noise in the resulting diff.)
2015-06-07 02:25:14 -05:00
|
|
|
use trans::build::{Load, Store};
|
2014-11-15 19:30:33 -06:00
|
|
|
use trans::common::*;
|
|
|
|
use trans::cleanup;
|
Add dropflag hints (stack-local booleans) for unfragmented paths in trans.
Added code to maintain these hints at runtime, and to conditionalize
drop-filling and calls to destructors.
In this early stage, we are using hints, so we are always free to
leave out a flag for a path -- then we just pass `None` as the
dropflag hint in the corresponding schedule cleanup call. But, once a
path has a hint, we must at least maintain it: i.e. if the hint
exists, we must ensure it is never set to "moved" if the data in
question might actually have been initialized. It remains sound to
conservatively set the hint to "initialized" as long as the true
drop-flag embedded in the value itself is up-to-date.
----
Here are some high-level details I want to point out:
* We maintain the hint in Lvalue::post_store, marking the lvalue as
moved. (But also continue drop-filling if necessary.)
* We update the hint on ExprAssign.
* We pass along the hint in once closures that capture-by-move.
* You only call `drop_ty` for state that does not have an associated hint.
If you have a hint, you must call `drop_ty_core` instead.
(Originally I passed the hint into `drop_ty` as well, to make the
connection to a hint more apparent, but the vast majority of
current calls to `drop_ty` are in contexts where no hint is
available, so it just seemed like noise in the resulting diff.)
2015-06-07 02:25:14 -05:00
|
|
|
use trans::cleanup::{CleanupMethods, DropHintDatum, DropHintMethods};
|
2014-11-15 19:30:33 -06:00
|
|
|
use trans::expr;
|
|
|
|
use trans::tvec;
|
2015-06-25 15:42:17 -05:00
|
|
|
use middle::ty::Ty;
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-10-15 01:25:34 -05:00
|
|
|
use std::fmt;
|
2013-02-25 13:11:21 -06:00
|
|
|
use syntax::ast;
|
2015-01-02 03:09:35 -06:00
|
|
|
use syntax::codemap::DUMMY_SP;
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// A `Datum` encapsulates the result of evaluating an expression. It
|
|
|
|
/// describes where the value is stored, what Rust type the value has,
|
|
|
|
/// whether it is addressed by reference, and so forth. Please refer
|
2015-02-18 21:34:55 -06:00
|
|
|
/// the section on datums in `README.md` for more details.
|
Add dropflag hints (stack-local booleans) for unfragmented paths in trans.
Added code to maintain these hints at runtime, and to conditionalize
drop-filling and calls to destructors.
In this early stage, we are using hints, so we are always free to
leave out a flag for a path -- then we just pass `None` as the
dropflag hint in the corresponding schedule cleanup call. But, once a
path has a hint, we must at least maintain it: i.e. if the hint
exists, we must ensure it is never set to "moved" if the data in
question might actually have been initialized. It remains sound to
conservatively set the hint to "initialized" as long as the true
drop-flag embedded in the value itself is up-to-date.
----
Here are some high-level details I want to point out:
* We maintain the hint in Lvalue::post_store, marking the lvalue as
moved. (But also continue drop-filling if necessary.)
* We update the hint on ExprAssign.
* We pass along the hint in once closures that capture-by-move.
* You only call `drop_ty` for state that does not have an associated hint.
If you have a hint, you must call `drop_ty_core` instead.
(Originally I passed the hint into `drop_ty` as well, to make the
connection to a hint more apparent, but the vast majority of
current calls to `drop_ty` are in contexts where no hint is
available, so it just seemed like noise in the resulting diff.)
2015-06-07 02:25:14 -05:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
2014-09-29 14:11:30 -05:00
|
|
|
pub struct Datum<'tcx, K> {
|
2012-08-28 17:54:45 -05:00
|
|
|
/// The llvm value. This is either a pointer to the Rust value or
|
2014-01-15 13:39:08 -06:00
|
|
|
/// the value itself, depending on `kind` below.
|
2014-03-28 12:05:27 -05:00
|
|
|
pub val: ValueRef,
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
/// The rust type of the value.
|
2014-09-29 14:11:30 -05:00
|
|
|
pub ty: Ty<'tcx>,
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
/// Indicates whether this is by-ref or by-value.
|
2014-03-28 12:05:27 -05:00
|
|
|
pub kind: K,
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-09-06 11:13:04 -05:00
|
|
|
pub struct DatumBlock<'blk, 'tcx: 'blk, K> {
|
|
|
|
pub bcx: Block<'blk, 'tcx>,
|
2014-09-29 14:11:30 -05:00
|
|
|
pub datum: Datum<'tcx, K>,
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
|
|
|
|
2015-01-28 07:34:18 -06:00
|
|
|
#[derive(Debug)]
|
2014-01-15 13:39:08 -06:00
|
|
|
pub enum Expr {
|
|
|
|
/// a fresh value that was produced and which has no cleanup yet
|
|
|
|
/// because it has not yet "landed" into its permanent home
|
|
|
|
RvalueExpr(Rvalue),
|
|
|
|
|
|
|
|
/// `val` is a pointer into memory for which a cleanup is scheduled
|
|
|
|
/// (and thus has type *T). If you move out of an Lvalue, you must
|
|
|
|
/// zero out the memory (FIXME #5016).
|
2015-06-05 14:34:03 -05:00
|
|
|
LvalueExpr(Lvalue),
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
|
|
|
|
Add dropflag hints (stack-local booleans) for unfragmented paths in trans.
Added code to maintain these hints at runtime, and to conditionalize
drop-filling and calls to destructors.
In this early stage, we are using hints, so we are always free to
leave out a flag for a path -- then we just pass `None` as the
dropflag hint in the corresponding schedule cleanup call. But, once a
path has a hint, we must at least maintain it: i.e. if the hint
exists, we must ensure it is never set to "moved" if the data in
question might actually have been initialized. It remains sound to
conservatively set the hint to "initialized" as long as the true
drop-flag embedded in the value itself is up-to-date.
----
Here are some high-level details I want to point out:
* We maintain the hint in Lvalue::post_store, marking the lvalue as
moved. (But also continue drop-filling if necessary.)
* We update the hint on ExprAssign.
* We pass along the hint in once closures that capture-by-move.
* You only call `drop_ty` for state that does not have an associated hint.
If you have a hint, you must call `drop_ty_core` instead.
(Originally I passed the hint into `drop_ty` as well, to make the
connection to a hint more apparent, but the vast majority of
current calls to `drop_ty` are in contexts where no hint is
available, so it just seemed like noise in the resulting diff.)
2015-06-07 02:25:14 -05:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
2015-06-05 14:34:03 -05:00
|
|
|
pub enum DropFlagInfo {
|
|
|
|
DontZeroJustUse(ast::NodeId),
|
|
|
|
ZeroAndMaintain(ast::NodeId),
|
|
|
|
None,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DropFlagInfo {
|
|
|
|
pub fn must_zero(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
DropFlagInfo::DontZeroJustUse(..) => false,
|
|
|
|
DropFlagInfo::ZeroAndMaintain(..) => true,
|
|
|
|
DropFlagInfo::None => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Add dropflag hints (stack-local booleans) for unfragmented paths in trans.
Added code to maintain these hints at runtime, and to conditionalize
drop-filling and calls to destructors.
In this early stage, we are using hints, so we are always free to
leave out a flag for a path -- then we just pass `None` as the
dropflag hint in the corresponding schedule cleanup call. But, once a
path has a hint, we must at least maintain it: i.e. if the hint
exists, we must ensure it is never set to "moved" if the data in
question might actually have been initialized. It remains sound to
conservatively set the hint to "initialized" as long as the true
drop-flag embedded in the value itself is up-to-date.
----
Here are some high-level details I want to point out:
* We maintain the hint in Lvalue::post_store, marking the lvalue as
moved. (But also continue drop-filling if necessary.)
* We update the hint on ExprAssign.
* We pass along the hint in once closures that capture-by-move.
* You only call `drop_ty` for state that does not have an associated hint.
If you have a hint, you must call `drop_ty_core` instead.
(Originally I passed the hint into `drop_ty` as well, to make the
connection to a hint more apparent, but the vast majority of
current calls to `drop_ty` are in contexts where no hint is
available, so it just seemed like noise in the resulting diff.)
2015-06-07 02:25:14 -05:00
|
|
|
pub fn hint_datum<'blk, 'tcx>(&self, bcx: Block<'blk, 'tcx>)
|
|
|
|
-> Option<DropHintDatum<'tcx>> {
|
|
|
|
let id = match *self {
|
|
|
|
DropFlagInfo::None => return None,
|
|
|
|
DropFlagInfo::DontZeroJustUse(id) |
|
|
|
|
DropFlagInfo::ZeroAndMaintain(id) => id,
|
|
|
|
};
|
|
|
|
|
|
|
|
let hints = bcx.fcx.lldropflag_hints.borrow();
|
|
|
|
let retval = hints.hint_datum(id);
|
|
|
|
assert!(retval.is_some(), "An id (={}) means must have a hint", id);
|
|
|
|
retval
|
2015-06-05 14:34:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: having Lvalue be `Copy` is a bit of a footgun, since clients
|
|
|
|
// may not realize that subparts of an Lvalue can have a subset of
|
|
|
|
// drop-flags associated with them, while this as written will just
|
|
|
|
// memcpy the drop_flag_info. But, it is an easier way to get `_match`
|
|
|
|
// off the ground to just let this be `Copy` for now.
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub struct Lvalue {
|
|
|
|
pub source: &'static str,
|
|
|
|
pub drop_flag_info: DropFlagInfo
|
|
|
|
}
|
2014-01-15 13:39:08 -06:00
|
|
|
|
2015-01-28 07:34:18 -06:00
|
|
|
#[derive(Debug)]
|
2014-01-15 13:39:08 -06:00
|
|
|
pub struct Rvalue {
|
2014-03-28 12:05:27 -05:00
|
|
|
pub mode: RvalueMode
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
|
|
|
|
2015-07-28 09:07:05 -05:00
|
|
|
/// Classifies what action we should take when a value is moved away
|
|
|
|
/// with respect to its drop-flag.
|
|
|
|
///
|
|
|
|
/// Long term there will be no need for this classification: all flags
|
|
|
|
/// (which will be stored on the stack frame) will have the same
|
|
|
|
/// interpretation and maintenance code associated with them.
|
2015-06-10 04:42:48 -05:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub enum HintKind {
|
2015-07-28 09:07:05 -05:00
|
|
|
/// When the value is moved, set the drop-flag to "dropped"
|
|
|
|
/// (i.e. "zero the flag", even when the specific representation
|
|
|
|
/// is not literally 0) and when it is reinitialized, set the
|
|
|
|
/// drop-flag back to "initialized".
|
2015-06-10 04:42:48 -05:00
|
|
|
ZeroAndMaintain,
|
2015-07-28 09:07:05 -05:00
|
|
|
|
|
|
|
/// When the value is moved, do not set the drop-flag to "dropped"
|
|
|
|
/// However, continue to read the drop-flag in deciding whether to
|
|
|
|
/// drop. (In essence, the path/fragment in question will never
|
|
|
|
/// need to be dropped at the points where it is moved away by
|
|
|
|
/// this code, but we are defending against the scenario where
|
|
|
|
/// some *other* code could move away (or drop) the value and thus
|
|
|
|
/// zero-the-flag, which is why we will still read from it.
|
2015-06-10 04:42:48 -05:00
|
|
|
DontZeroJustUse,
|
|
|
|
}
|
2015-06-05 14:34:03 -05:00
|
|
|
|
2015-06-10 04:42:48 -05:00
|
|
|
impl Lvalue { // Constructors for various Lvalues.
|
|
|
|
pub fn new<'blk, 'tcx>(source: &'static str) -> Lvalue {
|
|
|
|
debug!("Lvalue at {} no drop flag info", source);
|
|
|
|
Lvalue { source: source, drop_flag_info: DropFlagInfo::None }
|
2015-06-05 14:34:03 -05:00
|
|
|
}
|
|
|
|
|
2015-06-10 04:42:48 -05:00
|
|
|
pub fn new_dropflag_hint(source: &'static str) -> Lvalue {
|
|
|
|
debug!("Lvalue at {} is drop flag hint", source);
|
|
|
|
Lvalue { source: source, drop_flag_info: DropFlagInfo::None }
|
2015-06-05 14:34:03 -05:00
|
|
|
}
|
|
|
|
|
2015-06-10 04:42:48 -05:00
|
|
|
pub fn new_with_hint<'blk, 'tcx>(source: &'static str,
|
|
|
|
bcx: Block<'blk, 'tcx>,
|
|
|
|
id: ast::NodeId,
|
|
|
|
k: HintKind) -> Lvalue {
|
|
|
|
let (opt_id, info) = {
|
|
|
|
let hint_available = Lvalue::has_dropflag_hint(bcx, id) &&
|
|
|
|
bcx.tcx().sess.nonzeroing_move_hints();
|
|
|
|
let info = match k {
|
|
|
|
HintKind::ZeroAndMaintain if hint_available =>
|
|
|
|
DropFlagInfo::ZeroAndMaintain(id),
|
|
|
|
HintKind::DontZeroJustUse if hint_available =>
|
|
|
|
DropFlagInfo::DontZeroJustUse(id),
|
2015-07-28 09:07:05 -05:00
|
|
|
_ =>
|
|
|
|
DropFlagInfo::None,
|
2015-06-10 04:42:48 -05:00
|
|
|
};
|
|
|
|
(Some(id), info)
|
2015-06-05 14:34:03 -05:00
|
|
|
};
|
2015-06-10 04:42:48 -05:00
|
|
|
debug!("Lvalue at {}, id: {:?} info: {:?}", source, opt_id, info);
|
2015-06-05 14:34:03 -05:00
|
|
|
Lvalue { source: source, drop_flag_info: info }
|
|
|
|
}
|
Add dropflag hints (stack-local booleans) for unfragmented paths in trans.
Added code to maintain these hints at runtime, and to conditionalize
drop-filling and calls to destructors.
In this early stage, we are using hints, so we are always free to
leave out a flag for a path -- then we just pass `None` as the
dropflag hint in the corresponding schedule cleanup call. But, once a
path has a hint, we must at least maintain it: i.e. if the hint
exists, we must ensure it is never set to "moved" if the data in
question might actually have been initialized. It remains sound to
conservatively set the hint to "initialized" as long as the true
drop-flag embedded in the value itself is up-to-date.
----
Here are some high-level details I want to point out:
* We maintain the hint in Lvalue::post_store, marking the lvalue as
moved. (But also continue drop-filling if necessary.)
* We update the hint on ExprAssign.
* We pass along the hint in once closures that capture-by-move.
* You only call `drop_ty` for state that does not have an associated hint.
If you have a hint, you must call `drop_ty_core` instead.
(Originally I passed the hint into `drop_ty` as well, to make the
connection to a hint more apparent, but the vast majority of
current calls to `drop_ty` are in contexts where no hint is
available, so it just seemed like noise in the resulting diff.)
2015-06-07 02:25:14 -05:00
|
|
|
} // end Lvalue constructor methods.
|
|
|
|
|
|
|
|
impl Lvalue {
|
2015-06-05 14:34:03 -05:00
|
|
|
fn has_dropflag_hint<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
|
|
|
|
id: ast::NodeId) -> bool {
|
Add dropflag hints (stack-local booleans) for unfragmented paths in trans.
Added code to maintain these hints at runtime, and to conditionalize
drop-filling and calls to destructors.
In this early stage, we are using hints, so we are always free to
leave out a flag for a path -- then we just pass `None` as the
dropflag hint in the corresponding schedule cleanup call. But, once a
path has a hint, we must at least maintain it: i.e. if the hint
exists, we must ensure it is never set to "moved" if the data in
question might actually have been initialized. It remains sound to
conservatively set the hint to "initialized" as long as the true
drop-flag embedded in the value itself is up-to-date.
----
Here are some high-level details I want to point out:
* We maintain the hint in Lvalue::post_store, marking the lvalue as
moved. (But also continue drop-filling if necessary.)
* We update the hint on ExprAssign.
* We pass along the hint in once closures that capture-by-move.
* You only call `drop_ty` for state that does not have an associated hint.
If you have a hint, you must call `drop_ty_core` instead.
(Originally I passed the hint into `drop_ty` as well, to make the
connection to a hint more apparent, but the vast majority of
current calls to `drop_ty` are in contexts where no hint is
available, so it just seemed like noise in the resulting diff.)
2015-06-07 02:25:14 -05:00
|
|
|
let hints = bcx.fcx.lldropflag_hints.borrow();
|
|
|
|
hints.has_hint(id)
|
|
|
|
}
|
|
|
|
pub fn dropflag_hint<'blk, 'tcx>(&self, bcx: Block<'blk, 'tcx>)
|
|
|
|
-> Option<DropHintDatum<'tcx>> {
|
|
|
|
self.drop_flag_info.hint_datum(bcx)
|
2015-06-05 14:34:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-28 14:36:05 -05:00
|
|
|
impl Rvalue {
|
|
|
|
pub fn new(m: RvalueMode) -> Rvalue {
|
|
|
|
Rvalue { mode: m }
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
// Make Datum linear for more type safety.
|
|
|
|
impl Drop for Rvalue {
|
|
|
|
fn drop(&mut self) { }
|
|
|
|
}
|
|
|
|
|
2015-03-30 08:38:44 -05:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
2014-01-15 13:39:08 -06:00
|
|
|
pub enum RvalueMode {
|
|
|
|
/// `val` is a pointer to the actual value (and thus has type *T)
|
|
|
|
ByRef,
|
2012-08-28 17:54:45 -05:00
|
|
|
|
|
|
|
/// `val` is the actual value (*only used for immediates* like ints, ptrs)
|
|
|
|
ByValue,
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn immediate_rvalue<'tcx>(val: ValueRef, ty: Ty<'tcx>) -> Datum<'tcx, Rvalue> {
|
2014-05-28 14:36:05 -05:00
|
|
|
return Datum::new(val, ty, Rvalue::new(ByValue));
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-09-06 11:13:04 -05:00
|
|
|
pub fn immediate_rvalue_bcx<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
|
|
|
|
val: ValueRef,
|
2014-09-29 14:11:30 -05:00
|
|
|
ty: Ty<'tcx>)
|
2014-09-06 11:13:04 -05:00
|
|
|
-> DatumBlock<'blk, 'tcx, Rvalue> {
|
2014-05-28 14:36:05 -05:00
|
|
|
return DatumBlock::new(bcx, immediate_rvalue(val, ty))
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
|
2014-11-25 20:17:11 -06:00
|
|
|
/// Allocates temporary space on the stack using alloca() and returns a by-ref Datum pointing to
|
|
|
|
/// it. The memory will be dropped upon exit from `scope`. The callback `populate` should
|
2015-03-02 10:11:02 -06:00
|
|
|
/// initialize the memory.
|
2014-12-09 12:44:51 -06:00
|
|
|
pub fn lvalue_scratch_datum<'blk, 'tcx, A, F>(bcx: Block<'blk, 'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
name: &str,
|
|
|
|
scope: cleanup::ScopeId,
|
|
|
|
arg: A,
|
|
|
|
populate: F)
|
|
|
|
-> DatumBlock<'blk, 'tcx, Lvalue> where
|
|
|
|
F: FnOnce(A, Block<'blk, 'tcx>, ValueRef) -> Block<'blk, 'tcx>,
|
|
|
|
{
|
2015-08-25 11:29:24 -05:00
|
|
|
let scratch = alloc_ty(bcx, ty, name);
|
2014-01-15 13:39:08 -06:00
|
|
|
|
|
|
|
// Subtle. Populate the scratch memory *before* scheduling cleanup.
|
|
|
|
let bcx = populate(arg, bcx, scratch);
|
Add dropflag hints (stack-local booleans) for unfragmented paths in trans.
Added code to maintain these hints at runtime, and to conditionalize
drop-filling and calls to destructors.
In this early stage, we are using hints, so we are always free to
leave out a flag for a path -- then we just pass `None` as the
dropflag hint in the corresponding schedule cleanup call. But, once a
path has a hint, we must at least maintain it: i.e. if the hint
exists, we must ensure it is never set to "moved" if the data in
question might actually have been initialized. It remains sound to
conservatively set the hint to "initialized" as long as the true
drop-flag embedded in the value itself is up-to-date.
----
Here are some high-level details I want to point out:
* We maintain the hint in Lvalue::post_store, marking the lvalue as
moved. (But also continue drop-filling if necessary.)
* We update the hint on ExprAssign.
* We pass along the hint in once closures that capture-by-move.
* You only call `drop_ty` for state that does not have an associated hint.
If you have a hint, you must call `drop_ty_core` instead.
(Originally I passed the hint into `drop_ty` as well, to make the
connection to a hint more apparent, but the vast majority of
current calls to `drop_ty` are in contexts where no hint is
available, so it just seemed like noise in the resulting diff.)
2015-06-07 02:25:14 -05:00
|
|
|
bcx.fcx.schedule_drop_mem(scope, scratch, ty, None);
|
2014-01-15 13:39:08 -06:00
|
|
|
|
2015-06-05 14:34:03 -05:00
|
|
|
DatumBlock::new(bcx, Datum::new(scratch, ty, Lvalue::new("datum::lvalue_scratch_datum")))
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-11-25 20:17:11 -06:00
|
|
|
/// Allocates temporary space on the stack using alloca() and 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!
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn rvalue_scratch_datum<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
name: &str)
|
|
|
|
-> Datum<'tcx, Rvalue> {
|
2015-08-25 11:29:24 -05:00
|
|
|
let scratch = alloc_ty(bcx, ty, name);
|
2015-08-25 11:24:16 -05:00
|
|
|
call_lifetime_start(bcx, scratch);
|
2014-05-28 14:36:05 -05:00
|
|
|
Datum::new(scratch, ty, Rvalue::new(ByRef))
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
|
|
|
|
2014-11-25 20:17:11 -06:00
|
|
|
/// Indicates the "appropriate" mode for this value, which is either by ref or by value, depending
|
|
|
|
/// on whether type is immediate or not.
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn appropriate_rvalue_mode<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
|
|
|
ty: Ty<'tcx>) -> RvalueMode {
|
2014-06-11 00:18:57 -05:00
|
|
|
if type_is_immediate(ccx, ty) {
|
2012-09-20 14:29:15 -05:00
|
|
|
ByValue
|
|
|
|
} else {
|
2014-01-15 13:39:08 -06:00
|
|
|
ByRef
|
2012-09-20 14:29:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
fn add_rvalue_clean<'a, 'tcx>(mode: RvalueMode,
|
|
|
|
fcx: &FunctionContext<'a, 'tcx>,
|
|
|
|
scope: cleanup::ScopeId,
|
|
|
|
val: ValueRef,
|
|
|
|
ty: Ty<'tcx>) {
|
2014-01-15 13:39:08 -06:00
|
|
|
match mode {
|
|
|
|
ByValue => { fcx.schedule_drop_immediate(scope, val, ty); }
|
Emit LLVM lifetime intrinsics to improve stack usage and codegen in general
Lifetime intrinsics help to reduce stack usage, because LLVM can apply
stack coloring to reuse the stack slots of dead allocas for new ones.
For example these functions now both use the same amount of stack, while
previous `bar()` used five times as much as `foo()`:
````rust
fn foo() {
println("{}", 5);
}
fn bar() {
println("{}", 5);
println("{}", 5);
println("{}", 5);
println("{}", 5);
println("{}", 5);
}
````
On top of that, LLVM can also optimize out certain operations when it
knows that memory is dead after a certain point. For example, it can
sometimes remove the zeroing used to cancel the drop glue. This is
possible when the glue drop itself was already removed because the
zeroing dominated the drop glue call. For example in:
````rust
pub fn bar(x: (Box<int>, int)) -> (Box<int>, int) {
x
}
````
With optimizations, this currently results in:
````llvm
define void @_ZN3bar20h330fa42547df8179niaE({ i64*, i64 }* noalias nocapture nonnull sret, { i64*, i64 }* noalias nocapture nonnull) unnamed_addr #0 {
"_ZN29_$LP$Box$LT$int$GT$$C$int$RP$39glue_drop.$x22glue_drop$x22$LP$1347$RP$17h88cf42702e5a322aE.exit":
%2 = bitcast { i64*, i64 }* %1 to i8*
%3 = bitcast { i64*, i64 }* %0 to i8*
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %3, i8* %2, i64 16, i32 8, i1 false)
tail call void @llvm.memset.p0i8.i64(i8* %2, i8 0, i64 16, i32 8, i1 false)
ret void
}
````
But with lifetime intrinsics we get:
````llvm
define void @_ZN3bar20h330fa42547df8179niaE({ i64*, i64 }* noalias nocapture nonnull sret, { i64*, i64 }* noalias nocapture nonnull) unnamed_addr #0 {
"_ZN29_$LP$Box$LT$int$GT$$C$int$RP$39glue_drop.$x22glue_drop$x22$LP$1347$RP$17h88cf42702e5a322aE.exit":
%2 = bitcast { i64*, i64 }* %1 to i8*
%3 = bitcast { i64*, i64 }* %0 to i8*
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %3, i8* %2, i64 16, i32 8, i1 false)
tail call void @llvm.lifetime.end(i64 16, i8* %2)
ret void
}
````
Fixes #15665
2014-05-01 12:32:07 -05:00
|
|
|
ByRef => {
|
|
|
|
fcx.schedule_lifetime_end(scope, val);
|
Add dropflag hints (stack-local booleans) for unfragmented paths in trans.
Added code to maintain these hints at runtime, and to conditionalize
drop-filling and calls to destructors.
In this early stage, we are using hints, so we are always free to
leave out a flag for a path -- then we just pass `None` as the
dropflag hint in the corresponding schedule cleanup call. But, once a
path has a hint, we must at least maintain it: i.e. if the hint
exists, we must ensure it is never set to "moved" if the data in
question might actually have been initialized. It remains sound to
conservatively set the hint to "initialized" as long as the true
drop-flag embedded in the value itself is up-to-date.
----
Here are some high-level details I want to point out:
* We maintain the hint in Lvalue::post_store, marking the lvalue as
moved. (But also continue drop-filling if necessary.)
* We update the hint on ExprAssign.
* We pass along the hint in once closures that capture-by-move.
* You only call `drop_ty` for state that does not have an associated hint.
If you have a hint, you must call `drop_ty_core` instead.
(Originally I passed the hint into `drop_ty` as well, to make the
connection to a hint more apparent, but the vast majority of
current calls to `drop_ty` are in contexts where no hint is
available, so it just seemed like noise in the resulting diff.)
2015-06-07 02:25:14 -05:00
|
|
|
fcx.schedule_drop_mem(scope, val, ty, None);
|
Emit LLVM lifetime intrinsics to improve stack usage and codegen in general
Lifetime intrinsics help to reduce stack usage, because LLVM can apply
stack coloring to reuse the stack slots of dead allocas for new ones.
For example these functions now both use the same amount of stack, while
previous `bar()` used five times as much as `foo()`:
````rust
fn foo() {
println("{}", 5);
}
fn bar() {
println("{}", 5);
println("{}", 5);
println("{}", 5);
println("{}", 5);
println("{}", 5);
}
````
On top of that, LLVM can also optimize out certain operations when it
knows that memory is dead after a certain point. For example, it can
sometimes remove the zeroing used to cancel the drop glue. This is
possible when the glue drop itself was already removed because the
zeroing dominated the drop glue call. For example in:
````rust
pub fn bar(x: (Box<int>, int)) -> (Box<int>, int) {
x
}
````
With optimizations, this currently results in:
````llvm
define void @_ZN3bar20h330fa42547df8179niaE({ i64*, i64 }* noalias nocapture nonnull sret, { i64*, i64 }* noalias nocapture nonnull) unnamed_addr #0 {
"_ZN29_$LP$Box$LT$int$GT$$C$int$RP$39glue_drop.$x22glue_drop$x22$LP$1347$RP$17h88cf42702e5a322aE.exit":
%2 = bitcast { i64*, i64 }* %1 to i8*
%3 = bitcast { i64*, i64 }* %0 to i8*
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %3, i8* %2, i64 16, i32 8, i1 false)
tail call void @llvm.memset.p0i8.i64(i8* %2, i8 0, i64 16, i32 8, i1 false)
ret void
}
````
But with lifetime intrinsics we get:
````llvm
define void @_ZN3bar20h330fa42547df8179niaE({ i64*, i64 }* noalias nocapture nonnull sret, { i64*, i64 }* noalias nocapture nonnull) unnamed_addr #0 {
"_ZN29_$LP$Box$LT$int$GT$$C$int$RP$39glue_drop.$x22glue_drop$x22$LP$1347$RP$17h88cf42702e5a322aE.exit":
%2 = bitcast { i64*, i64 }* %1 to i8*
%3 = bitcast { i64*, i64 }* %0 to i8*
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %3, i8* %2, i64 16, i32 8, i1 false)
tail call void @llvm.lifetime.end(i64 16, i8* %2)
ret void
}
````
Fixes #15665
2014-05-01 12:32:07 -05:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
pub trait KindOps {
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// Take appropriate action after the value in `datum` has been
|
|
|
|
/// stored to a new location.
|
2014-09-06 11:13:04 -05:00
|
|
|
fn post_store<'blk, 'tcx>(&self,
|
|
|
|
bcx: Block<'blk, 'tcx>,
|
|
|
|
val: ValueRef,
|
2014-09-29 14:11:30 -05:00
|
|
|
ty: Ty<'tcx>)
|
2014-09-06 11:13:04 -05:00
|
|
|
-> Block<'blk, 'tcx>;
|
2014-01-15 13:39:08 -06:00
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// True if this mode is a reference mode, meaning that the datum's
|
|
|
|
/// val field is a pointer to the actual value
|
2014-01-15 13:39:08 -06:00
|
|
|
fn is_by_ref(&self) -> bool;
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// Converts to an Expr kind
|
2014-01-15 13:39:08 -06:00
|
|
|
fn to_expr_kind(self) -> Expr;
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
2012-09-07 14:06:42 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
impl KindOps for Rvalue {
|
2014-09-06 11:13:04 -05:00
|
|
|
fn post_store<'blk, 'tcx>(&self,
|
|
|
|
bcx: Block<'blk, 'tcx>,
|
|
|
|
_val: ValueRef,
|
2014-09-29 14:11:30 -05:00
|
|
|
_ty: Ty<'tcx>)
|
2014-09-06 11:13:04 -05:00
|
|
|
-> Block<'blk, 'tcx> {
|
2014-01-15 13:39:08 -06:00
|
|
|
// No cleanup is scheduled for an rvalue, so we don't have
|
|
|
|
// to do anything after a move to cancel or duplicate it.
|
2015-01-30 10:14:17 -06:00
|
|
|
if self.is_by_ref() {
|
|
|
|
call_lifetime_end(bcx, _val);
|
|
|
|
}
|
2014-01-15 13:39:08 -06:00
|
|
|
bcx
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
fn is_by_ref(&self) -> bool {
|
|
|
|
self.mode == ByRef
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
fn to_expr_kind(self) -> Expr {
|
|
|
|
RvalueExpr(self)
|
|
|
|
}
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
impl KindOps for Lvalue {
|
2014-11-25 20:17:11 -06:00
|
|
|
/// If an lvalue is moved, we must zero out the memory in which it resides so as to cancel
|
|
|
|
/// cleanup. If an @T lvalue is copied, we must increment the reference count.
|
2014-09-06 11:13:04 -05:00
|
|
|
fn post_store<'blk, 'tcx>(&self,
|
|
|
|
bcx: Block<'blk, 'tcx>,
|
|
|
|
val: ValueRef,
|
2014-09-29 14:11:30 -05:00
|
|
|
ty: Ty<'tcx>)
|
2014-09-06 11:13:04 -05:00
|
|
|
-> Block<'blk, 'tcx> {
|
2015-02-25 08:45:33 -06:00
|
|
|
let _icx = push_ctxt("<Lvalue as KindOps>::post_store");
|
2015-02-25 16:09:58 -06:00
|
|
|
if bcx.fcx.type_needs_drop(ty) {
|
Add dropflag hints (stack-local booleans) for unfragmented paths in trans.
Added code to maintain these hints at runtime, and to conditionalize
drop-filling and calls to destructors.
In this early stage, we are using hints, so we are always free to
leave out a flag for a path -- then we just pass `None` as the
dropflag hint in the corresponding schedule cleanup call. But, once a
path has a hint, we must at least maintain it: i.e. if the hint
exists, we must ensure it is never set to "moved" if the data in
question might actually have been initialized. It remains sound to
conservatively set the hint to "initialized" as long as the true
drop-flag embedded in the value itself is up-to-date.
----
Here are some high-level details I want to point out:
* We maintain the hint in Lvalue::post_store, marking the lvalue as
moved. (But also continue drop-filling if necessary.)
* We update the hint on ExprAssign.
* We pass along the hint in once closures that capture-by-move.
* You only call `drop_ty` for state that does not have an associated hint.
If you have a hint, you must call `drop_ty_core` instead.
(Originally I passed the hint into `drop_ty` as well, to make the
connection to a hint more apparent, but the vast majority of
current calls to `drop_ty` are in contexts where no hint is
available, so it just seemed like noise in the resulting diff.)
2015-06-07 02:25:14 -05:00
|
|
|
// cancel cleanup of affine values:
|
|
|
|
// 1. if it has drop-hint, mark as moved; then code
|
|
|
|
// aware of drop-hint won't bother calling the
|
|
|
|
// drop-glue itself.
|
|
|
|
if let Some(hint_datum) = self.drop_flag_info.hint_datum(bcx) {
|
2015-08-05 02:46:59 -05:00
|
|
|
let moved_hint_byte = adt::DTOR_MOVED_HINT;
|
Add dropflag hints (stack-local booleans) for unfragmented paths in trans.
Added code to maintain these hints at runtime, and to conditionalize
drop-filling and calls to destructors.
In this early stage, we are using hints, so we are always free to
leave out a flag for a path -- then we just pass `None` as the
dropflag hint in the corresponding schedule cleanup call. But, once a
path has a hint, we must at least maintain it: i.e. if the hint
exists, we must ensure it is never set to "moved" if the data in
question might actually have been initialized. It remains sound to
conservatively set the hint to "initialized" as long as the true
drop-flag embedded in the value itself is up-to-date.
----
Here are some high-level details I want to point out:
* We maintain the hint in Lvalue::post_store, marking the lvalue as
moved. (But also continue drop-filling if necessary.)
* We update the hint on ExprAssign.
* We pass along the hint in once closures that capture-by-move.
* You only call `drop_ty` for state that does not have an associated hint.
If you have a hint, you must call `drop_ty_core` instead.
(Originally I passed the hint into `drop_ty` as well, to make the
connection to a hint more apparent, but the vast majority of
current calls to `drop_ty` are in contexts where no hint is
available, so it just seemed like noise in the resulting diff.)
2015-06-07 02:25:14 -05:00
|
|
|
let hint_llval = hint_datum.to_value().value();
|
|
|
|
Store(bcx, C_u8(bcx.fcx.ccx, moved_hint_byte), hint_llval);
|
|
|
|
}
|
|
|
|
// 2. if the drop info says its necessary, drop-fill the memory.
|
|
|
|
if self.drop_flag_info.must_zero() {
|
|
|
|
let () = drop_done_fill_mem(bcx, val, ty);
|
|
|
|
}
|
2014-09-30 17:26:04 -05:00
|
|
|
bcx
|
2014-01-15 13:39:08 -06:00
|
|
|
} else {
|
2015-06-10 04:42:48 -05:00
|
|
|
// FIXME (#5016) would be nice to assert this, but we have
|
|
|
|
// to allow for e.g. DontZeroJustUse flags, for now.
|
|
|
|
//
|
|
|
|
// (The dropflag hint construction should be taking
|
|
|
|
// !type_needs_drop into account; earlier analysis phases
|
|
|
|
// may not have all the info they need to include such
|
|
|
|
// information properly, I think; in particular the
|
|
|
|
// fragments analysis works on a non-monomorphized view of
|
|
|
|
// the code.)
|
Add dropflag hints (stack-local booleans) for unfragmented paths in trans.
Added code to maintain these hints at runtime, and to conditionalize
drop-filling and calls to destructors.
In this early stage, we are using hints, so we are always free to
leave out a flag for a path -- then we just pass `None` as the
dropflag hint in the corresponding schedule cleanup call. But, once a
path has a hint, we must at least maintain it: i.e. if the hint
exists, we must ensure it is never set to "moved" if the data in
question might actually have been initialized. It remains sound to
conservatively set the hint to "initialized" as long as the true
drop-flag embedded in the value itself is up-to-date.
----
Here are some high-level details I want to point out:
* We maintain the hint in Lvalue::post_store, marking the lvalue as
moved. (But also continue drop-filling if necessary.)
* We update the hint on ExprAssign.
* We pass along the hint in once closures that capture-by-move.
* You only call `drop_ty` for state that does not have an associated hint.
If you have a hint, you must call `drop_ty_core` instead.
(Originally I passed the hint into `drop_ty` as well, to make the
connection to a hint more apparent, but the vast majority of
current calls to `drop_ty` are in contexts where no hint is
available, so it just seemed like noise in the resulting diff.)
2015-06-07 02:25:14 -05:00
|
|
|
//
|
|
|
|
// assert_eq!(self.drop_flag_info, DropFlagInfo::None);
|
2014-01-15 13:39:08 -06:00
|
|
|
bcx
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
fn is_by_ref(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
fn to_expr_kind(self) -> Expr {
|
2015-06-05 14:34:03 -05:00
|
|
|
LvalueExpr(self)
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
impl KindOps for Expr {
|
2014-09-06 11:13:04 -05:00
|
|
|
fn post_store<'blk, 'tcx>(&self,
|
|
|
|
bcx: Block<'blk, 'tcx>,
|
|
|
|
val: ValueRef,
|
2014-09-29 14:11:30 -05:00
|
|
|
ty: Ty<'tcx>)
|
2014-09-06 11:13:04 -05:00
|
|
|
-> Block<'blk, 'tcx> {
|
2014-01-15 13:39:08 -06:00
|
|
|
match *self {
|
2015-06-05 14:34:03 -05:00
|
|
|
LvalueExpr(ref l) => l.post_store(bcx, val, ty),
|
2014-01-15 13:39:08 -06:00
|
|
|
RvalueExpr(ref r) => r.post_store(bcx, val, ty),
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
fn is_by_ref(&self) -> bool {
|
|
|
|
match *self {
|
2015-06-05 14:34:03 -05:00
|
|
|
LvalueExpr(ref l) => l.is_by_ref(),
|
2014-01-15 13:39:08 -06:00
|
|
|
RvalueExpr(ref r) => r.is_by_ref()
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
fn to_expr_kind(self) -> Expr {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx> Datum<'tcx, Rvalue> {
|
2014-11-25 20:17:11 -06:00
|
|
|
/// Schedules a cleanup for this datum in the given scope. That means that this datum is no
|
|
|
|
/// longer an rvalue datum; hence, this function consumes the datum and returns the contained
|
|
|
|
/// ValueRef.
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn add_clean<'a>(self,
|
|
|
|
fcx: &FunctionContext<'a, 'tcx>,
|
|
|
|
scope: cleanup::ScopeId)
|
|
|
|
-> ValueRef {
|
2014-01-15 13:39:08 -06:00
|
|
|
add_rvalue_clean(self.kind.mode, fcx, scope, self.val, self.ty);
|
|
|
|
self.val
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-11-25 20:17:11 -06:00
|
|
|
/// Returns an lvalue datum (that is, a by ref datum with cleanup scheduled). If `self` is not
|
|
|
|
/// already an lvalue, cleanup will be scheduled in the temporary scope for `expr_id`.
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn to_lvalue_datum_in_scope<'blk>(self,
|
|
|
|
bcx: Block<'blk, 'tcx>,
|
|
|
|
name: &str,
|
|
|
|
scope: cleanup::ScopeId)
|
|
|
|
-> DatumBlock<'blk, 'tcx, Lvalue> {
|
2014-01-15 13:39:08 -06:00
|
|
|
let fcx = bcx.fcx;
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
match self.kind.mode {
|
|
|
|
ByRef => {
|
|
|
|
add_rvalue_clean(ByRef, fcx, scope, self.val, self.ty);
|
2015-06-05 14:34:03 -05:00
|
|
|
DatumBlock::new(bcx, Datum::new(
|
|
|
|
self.val,
|
|
|
|
self.ty,
|
|
|
|
Lvalue::new("datum::to_lvalue_datum_in_scope")))
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2014-01-15 13:39:08 -06:00
|
|
|
|
|
|
|
ByValue => {
|
|
|
|
lvalue_scratch_datum(
|
2015-03-02 10:11:02 -06:00
|
|
|
bcx, self.ty, name, scope, self,
|
2015-08-25 11:33:55 -05:00
|
|
|
|this, bcx, llval| {
|
|
|
|
call_lifetime_start(bcx, llval);
|
|
|
|
let bcx = this.store_to(bcx, llval);
|
|
|
|
bcx.fcx.schedule_lifetime_end(scope, llval);
|
|
|
|
bcx
|
|
|
|
})
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn to_ref_datum<'blk>(self, bcx: Block<'blk, 'tcx>)
|
|
|
|
-> DatumBlock<'blk, 'tcx, Rvalue> {
|
2014-01-15 13:39:08 -06:00
|
|
|
let mut bcx = bcx;
|
|
|
|
match self.kind.mode {
|
2014-05-28 14:36:05 -05:00
|
|
|
ByRef => DatumBlock::new(bcx, self),
|
2014-01-15 13:39:08 -06:00
|
|
|
ByValue => {
|
|
|
|
let scratch = rvalue_scratch_datum(bcx, self.ty, "to_ref");
|
|
|
|
bcx = self.store_to(bcx, scratch.val);
|
2014-05-28 14:36:05 -05:00
|
|
|
DatumBlock::new(bcx, scratch)
|
2013-05-29 14:49:23 -05:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn to_appropriate_datum<'blk>(self, bcx: Block<'blk, 'tcx>)
|
|
|
|
-> DatumBlock<'blk, 'tcx, Rvalue> {
|
2014-01-15 13:39:08 -06:00
|
|
|
match self.appropriate_rvalue_mode(bcx.ccx()) {
|
|
|
|
ByRef => {
|
|
|
|
self.to_ref_datum(bcx)
|
|
|
|
}
|
|
|
|
ByValue => {
|
|
|
|
match self.kind.mode {
|
2014-05-28 14:36:05 -05:00
|
|
|
ByValue => DatumBlock::new(bcx, self),
|
2014-01-15 13:39:08 -06:00
|
|
|
ByRef => {
|
2014-07-05 14:47:14 -05:00
|
|
|
let llval = load_ty(bcx, self.val, self.ty);
|
2015-01-30 10:14:17 -06:00
|
|
|
call_lifetime_end(bcx, self.val);
|
2014-05-28 14:36:05 -05:00
|
|
|
DatumBlock::new(bcx, Datum::new(llval, self.ty, Rvalue::new(ByValue)))
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// Methods suitable for "expr" datums that could be either lvalues or
|
|
|
|
/// rvalues. These include coercions into lvalues/rvalues but also a number
|
|
|
|
/// of more general operations. (Some of those operations could be moved to
|
|
|
|
/// the more general `impl<K> Datum<K>`, but it's convenient to have them
|
|
|
|
/// here since we can `match self.kind` rather than having to implement
|
|
|
|
/// generic methods in `KindOps`.)
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx> Datum<'tcx, Expr> {
|
2014-12-09 12:44:51 -06:00
|
|
|
fn match_kind<R, F, G>(self, if_lvalue: F, if_rvalue: G) -> R where
|
|
|
|
F: FnOnce(Datum<'tcx, Lvalue>) -> R,
|
|
|
|
G: FnOnce(Datum<'tcx, Rvalue>) -> R,
|
|
|
|
{
|
2014-01-15 13:39:08 -06:00
|
|
|
let Datum { val, ty, kind } = self;
|
|
|
|
match kind {
|
2015-06-05 14:34:03 -05:00
|
|
|
LvalueExpr(l) => if_lvalue(Datum::new(val, ty, l)),
|
2014-05-28 14:36:05 -05:00
|
|
|
RvalueExpr(r) => if_rvalue(Datum::new(val, ty, r)),
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-11-25 20:17:11 -06:00
|
|
|
/// Asserts that this datum *is* an lvalue and returns it.
|
2014-04-01 09:29:37 -05:00
|
|
|
#[allow(dead_code)] // potentially useful
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn assert_lvalue(self, bcx: Block) -> Datum<'tcx, Lvalue> {
|
2014-01-15 13:39:08 -06:00
|
|
|
self.match_kind(
|
|
|
|
|d| d,
|
|
|
|
|_| bcx.sess().bug("assert_lvalue given rvalue"))
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn store_to_dest<'blk>(self,
|
|
|
|
bcx: Block<'blk, 'tcx>,
|
|
|
|
dest: expr::Dest,
|
|
|
|
expr_id: ast::NodeId)
|
|
|
|
-> Block<'blk, 'tcx> {
|
2014-01-15 13:39:08 -06:00
|
|
|
match dest {
|
|
|
|
expr::Ignore => {
|
|
|
|
self.add_clean_if_rvalue(bcx, expr_id);
|
|
|
|
bcx
|
|
|
|
}
|
|
|
|
expr::SaveIn(addr) => {
|
|
|
|
self.store_to(bcx, addr)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-25 20:17:11 -06:00
|
|
|
/// Arranges cleanup for `self` if it is an rvalue. Use when you are done working with a value
|
|
|
|
/// that may need drop.
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn add_clean_if_rvalue<'blk>(self,
|
|
|
|
bcx: Block<'blk, 'tcx>,
|
|
|
|
expr_id: ast::NodeId) {
|
2014-01-15 13:39:08 -06:00
|
|
|
self.match_kind(
|
|
|
|
|_| { /* Nothing to do, cleanup already arranged */ },
|
|
|
|
|r| {
|
|
|
|
let scope = cleanup::temporary_scope(bcx.tcx(), expr_id);
|
|
|
|
r.add_clean(bcx.fcx, scope);
|
|
|
|
})
|
2012-09-06 17:21:42 -05:00
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn to_lvalue_datum<'blk>(self,
|
|
|
|
bcx: Block<'blk, 'tcx>,
|
|
|
|
name: &str,
|
|
|
|
expr_id: ast::NodeId)
|
|
|
|
-> DatumBlock<'blk, 'tcx, Lvalue> {
|
2014-08-31 23:14:56 -05:00
|
|
|
debug!("to_lvalue_datum self: {}", self.to_string(bcx.ccx()));
|
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
self.match_kind(
|
2014-05-28 14:36:05 -05:00
|
|
|
|l| DatumBlock::new(bcx, l),
|
2014-01-15 13:39:08 -06:00
|
|
|
|r| {
|
|
|
|
let scope = cleanup::temporary_scope(bcx.tcx(), expr_id);
|
|
|
|
r.to_lvalue_datum_in_scope(bcx, name, scope)
|
|
|
|
})
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-11-25 20:17:11 -06:00
|
|
|
/// Ensures that we have an rvalue datum (that is, a datum with no cleanup scheduled).
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn to_rvalue_datum<'blk>(self,
|
|
|
|
bcx: Block<'blk, 'tcx>,
|
|
|
|
name: &'static str)
|
|
|
|
-> DatumBlock<'blk, 'tcx, Rvalue> {
|
2014-01-15 13:39:08 -06:00
|
|
|
self.match_kind(
|
|
|
|
|l| {
|
2014-02-07 13:43:05 -06:00
|
|
|
let mut bcx = bcx;
|
2014-01-15 13:39:08 -06:00
|
|
|
match l.appropriate_rvalue_mode(bcx.ccx()) {
|
|
|
|
ByRef => {
|
|
|
|
let scratch = rvalue_scratch_datum(bcx, l.ty, name);
|
|
|
|
bcx = l.store_to(bcx, scratch.val);
|
2014-05-28 14:36:05 -05:00
|
|
|
DatumBlock::new(bcx, scratch)
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
|
|
|
ByValue => {
|
2014-07-05 14:47:14 -05:00
|
|
|
let v = load_ty(bcx, l.val, l.ty);
|
2014-01-23 08:51:54 -06:00
|
|
|
bcx = l.kind.post_store(bcx, l.val, l.ty);
|
2014-05-28 14:36:05 -05:00
|
|
|
DatumBlock::new(bcx, Datum::new(v, l.ty, Rvalue::new(ByValue)))
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2014-01-15 13:39:08 -06:00
|
|
|
},
|
2014-05-28 14:36:05 -05:00
|
|
|
|r| DatumBlock::new(bcx, r))
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// Methods suitable only for lvalues. These include the various
|
|
|
|
/// operations to extract components out of compound data structures,
|
|
|
|
/// such as extracting the field from a struct or a particular element
|
|
|
|
/// from an array.
|
2014-09-29 14:11:30 -05:00
|
|
|
impl<'tcx> Datum<'tcx, Lvalue> {
|
2014-11-25 20:17:11 -06:00
|
|
|
/// Converts a datum into a by-ref value. The datum type must be one which is always passed by
|
|
|
|
/// reference.
|
2014-01-15 13:39:08 -06:00
|
|
|
pub fn to_llref(self) -> ValueRef {
|
|
|
|
self.val
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
// Extracts a component of a compound data structure (e.g., a field from a
|
|
|
|
// struct). Note that if self is an opened, unsized type then the returned
|
|
|
|
// datum may also be unsized _without the size information_. It is the
|
|
|
|
// callers responsibility to package the result in some way to make a valid
|
|
|
|
// datum in that case (e.g., by making a fat pointer or opened pair).
|
2014-12-09 12:44:51 -06:00
|
|
|
pub fn get_element<'blk, F>(&self, bcx: Block<'blk, 'tcx>, ty: Ty<'tcx>,
|
|
|
|
gep: F)
|
|
|
|
-> Datum<'tcx, Lvalue> where
|
|
|
|
F: FnOnce(ValueRef) -> ValueRef,
|
|
|
|
{
|
2015-02-17 12:27:01 -06:00
|
|
|
let val = if type_is_sized(bcx.tcx(), self.ty) {
|
|
|
|
gep(self.val)
|
|
|
|
} else {
|
|
|
|
gep(Load(bcx, expr::get_dataptr(bcx, self.val)))
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
};
|
2014-01-15 13:39:08 -06:00
|
|
|
Datum {
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 07:20:11 -05:00
|
|
|
val: val,
|
2015-06-05 14:34:03 -05:00
|
|
|
kind: Lvalue::new("Datum::get_element"),
|
2014-01-15 13:39:08 -06:00
|
|
|
ty: ty,
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-17 12:27:01 -06:00
|
|
|
pub fn get_vec_base_and_len<'blk>(&self, bcx: Block<'blk, 'tcx>)
|
|
|
|
-> (ValueRef, ValueRef) {
|
2014-02-01 09:53:54 -06:00
|
|
|
//! Converts a vector into the slice pair.
|
2013-10-16 11:04:51 -05:00
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
tvec::get_base_and_len(bcx, self.val, self.ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// Generic methods applicable to any sort of datum.
|
2015-01-20 17:45:07 -06:00
|
|
|
impl<'tcx, K: KindOps + fmt::Debug> Datum<'tcx, K> {
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn new(val: ValueRef, ty: Ty<'tcx>, kind: K) -> Datum<'tcx, K> {
|
2014-05-28 14:36:05 -05:00
|
|
|
Datum { val: val, ty: ty, kind: kind }
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn to_expr_datum(self) -> Datum<'tcx, Expr> {
|
2014-01-15 13:39:08 -06:00
|
|
|
let Datum { val, ty, kind } = self;
|
|
|
|
Datum { val: val, ty: ty, kind: kind.to_expr_kind() }
|
|
|
|
}
|
|
|
|
|
2014-11-25 20:17:11 -06:00
|
|
|
/// Moves or copies this value into a new home, as appropriate depending on the type of the
|
|
|
|
/// datum. This method consumes the datum, since it would be incorrect to go on using the datum
|
|
|
|
/// if the value represented is affine (and hence the value is moved).
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn store_to<'blk>(self,
|
|
|
|
bcx: Block<'blk, 'tcx>,
|
|
|
|
dst: ValueRef)
|
|
|
|
-> Block<'blk, 'tcx> {
|
2014-09-30 17:26:04 -05:00
|
|
|
self.shallow_copy_raw(bcx, dst);
|
2014-01-15 13:39:08 -06:00
|
|
|
|
|
|
|
self.kind.post_store(bcx, self.val, self.ty)
|
|
|
|
}
|
|
|
|
|
2014-11-25 20:17:11 -06:00
|
|
|
/// Helper function that performs a shallow copy of this value into `dst`, which should be a
|
|
|
|
/// pointer to a memory location suitable for `self.ty`. `dst` should contain uninitialized
|
|
|
|
/// memory (either newly allocated, zeroed, or dropped).
|
|
|
|
///
|
|
|
|
/// This function is private to datums because it leaves memory in an unstable state, where the
|
|
|
|
/// source value has been copied but not zeroed. Public methods are `store_to` (if you no
|
|
|
|
/// longer need the source value) or `shallow_copy` (if you wish the source value to remain
|
|
|
|
/// valid).
|
2014-09-29 14:11:30 -05:00
|
|
|
fn shallow_copy_raw<'blk>(&self,
|
|
|
|
bcx: Block<'blk, 'tcx>,
|
|
|
|
dst: ValueRef)
|
|
|
|
-> Block<'blk, 'tcx> {
|
2014-01-15 13:39:08 -06:00
|
|
|
let _icx = push_ctxt("copy_to_no_check");
|
|
|
|
|
2014-01-16 18:10:17 -06:00
|
|
|
if type_is_zero_size(bcx.ccx(), self.ty) {
|
2014-01-15 13:39:08 -06:00
|
|
|
return bcx;
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.kind.is_by_ref() {
|
|
|
|
memcpy_ty(bcx, dst, self.val, self.ty);
|
|
|
|
} else {
|
2014-07-05 14:47:14 -05:00
|
|
|
store_ty(bcx, self.val, dst, self.ty);
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return bcx;
|
|
|
|
}
|
|
|
|
|
2014-11-25 20:17:11 -06:00
|
|
|
/// Copies the value into a new location. This function always preserves the existing datum as
|
|
|
|
/// a valid value. Therefore, it does not consume `self` and, also, cannot be applied to affine
|
|
|
|
/// values (since they must never be duplicated).
|
2014-12-11 03:16:11 -06:00
|
|
|
pub fn shallow_copy<'blk>(&self,
|
|
|
|
bcx: Block<'blk, 'tcx>,
|
|
|
|
dst: ValueRef)
|
|
|
|
-> Block<'blk, 'tcx> {
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 19:01:33 -06:00
|
|
|
/*!
|
|
|
|
* Copies the value into a new location. This function always
|
|
|
|
* preserves the existing datum as a valid value. Therefore,
|
|
|
|
* it does not consume `self` and, also, cannot be applied to
|
|
|
|
* affine values (since they must never be duplicated).
|
|
|
|
*/
|
|
|
|
|
2015-06-29 01:03:47 -05:00
|
|
|
assert!(!self.ty
|
|
|
|
.moves_by_default(&bcx.tcx().empty_parameter_environment(), DUMMY_SP));
|
2014-09-30 17:26:04 -05:00
|
|
|
self.shallow_copy_raw(bcx, dst)
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
|
|
|
|
2014-04-01 09:29:37 -05:00
|
|
|
#[allow(dead_code)] // useful for debugging
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn to_string<'a>(&self, ccx: &CrateContext<'a, 'tcx>) -> String {
|
2015-06-18 12:25:05 -05:00
|
|
|
format!("Datum({}, {:?}, {:?})",
|
2014-09-05 11:18:53 -05:00
|
|
|
ccx.tn().val_to_string(self.val),
|
2015-06-18 12:25:05 -05:00
|
|
|
self.ty,
|
2014-05-27 22:44:58 -05:00
|
|
|
self.kind)
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
|
|
|
|
2014-11-26 13:17:23 -06:00
|
|
|
/// See the `appropriate_rvalue_mode()` function
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn appropriate_rvalue_mode<'a>(&self, ccx: &CrateContext<'a, 'tcx>)
|
|
|
|
-> RvalueMode {
|
2014-01-15 13:39:08 -06:00
|
|
|
appropriate_rvalue_mode(ccx, self.ty)
|
2013-10-16 11:04:51 -05:00
|
|
|
}
|
|
|
|
|
2014-11-25 20:17:11 -06:00
|
|
|
/// Converts `self` into a by-value `ValueRef`. Consumes this datum (i.e., absolves you of
|
|
|
|
/// responsibility to cleanup the value). For this to work, the value must be something
|
|
|
|
/// scalar-ish (like an int or a pointer) which (1) does not require drop glue and (2) is
|
|
|
|
/// naturally passed around by value, and not by reference.
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn to_llscalarish<'blk>(self, bcx: Block<'blk, 'tcx>) -> ValueRef {
|
2015-02-25 16:09:58 -06:00
|
|
|
assert!(!bcx.fcx.type_needs_drop(self.ty));
|
2014-01-15 13:39:08 -06:00
|
|
|
assert!(self.appropriate_rvalue_mode(bcx.ccx()) == ByValue);
|
|
|
|
if self.kind.is_by_ref() {
|
2014-07-05 14:47:14 -05:00
|
|
|
load_ty(bcx, self.val, self.ty)
|
2014-01-15 13:39:08 -06:00
|
|
|
} else {
|
|
|
|
self.val
|
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn to_llbool<'blk>(self, bcx: Block<'blk, 'tcx>) -> ValueRef {
|
2015-06-24 00:24:13 -05:00
|
|
|
assert!(self.ty.is_bool());
|
2014-03-16 03:29:05 -05:00
|
|
|
self.to_llscalarish(bcx)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-09-06 11:13:04 -05:00
|
|
|
impl<'blk, 'tcx, K> DatumBlock<'blk, 'tcx, K> {
|
2014-09-29 14:11:30 -05:00
|
|
|
pub fn new(bcx: Block<'blk, 'tcx>, datum: Datum<'tcx, K>)
|
|
|
|
-> DatumBlock<'blk, 'tcx, K> {
|
2014-05-28 14:36:05 -05:00
|
|
|
DatumBlock { bcx: bcx, datum: datum }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-20 17:45:07 -06:00
|
|
|
impl<'blk, 'tcx, K: KindOps + fmt::Debug> DatumBlock<'blk, 'tcx, K> {
|
2014-09-06 11:13:04 -05:00
|
|
|
pub fn to_expr_datumblock(self) -> DatumBlock<'blk, 'tcx, Expr> {
|
2014-05-28 14:36:05 -05:00
|
|
|
DatumBlock::new(self.bcx, self.datum.to_expr_datum())
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
|
2014-09-06 11:13:04 -05:00
|
|
|
impl<'blk, 'tcx> DatumBlock<'blk, 'tcx, Expr> {
|
2014-01-15 13:39:08 -06:00
|
|
|
pub fn store_to_dest(self,
|
|
|
|
dest: expr::Dest,
|
2014-09-06 11:13:04 -05:00
|
|
|
expr_id: ast::NodeId) -> Block<'blk, 'tcx> {
|
2014-01-15 13:39:08 -06:00
|
|
|
let DatumBlock { bcx, datum } = self;
|
|
|
|
datum.store_to_dest(bcx, dest, expr_id)
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|
|
|
|
|
2014-09-06 11:13:04 -05:00
|
|
|
pub fn to_llbool(self) -> Result<'blk, 'tcx> {
|
2014-01-15 13:39:08 -06:00
|
|
|
let DatumBlock { datum, bcx } = self;
|
2014-05-03 06:14:56 -05:00
|
|
|
Result::new(bcx, datum.to_llbool(bcx))
|
2014-01-15 13:39:08 -06:00
|
|
|
}
|
2012-08-28 17:54:45 -05:00
|
|
|
}
|