2013-05-03 21:07:33 -05: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.
|
|
|
|
|
2014-01-11 18:25:51 -06:00
|
|
|
//! Logic relating to rooting and write guards for managed values.
|
|
|
|
//! This code is primarily for use by datum;
|
2013-05-03 21:07:33 -05:00
|
|
|
//! it exists in its own module both to keep datum.rs bite-sized
|
|
|
|
//! and for each in debugging (e.g., so you can use
|
|
|
|
//! `RUST_LOG=rustc::middle::trans::write_guard`).
|
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2013-12-30 20:57:48 -06:00
|
|
|
use middle::borrowck::{RootInfo, root_map_key};
|
2014-01-15 13:39:08 -06:00
|
|
|
use middle::trans::cleanup;
|
2013-05-03 21:07:33 -05:00
|
|
|
use middle::trans::common::*;
|
|
|
|
use middle::trans::datum::*;
|
2013-08-31 11:13:04 -05:00
|
|
|
use syntax::codemap::Span;
|
2013-05-03 21:07:33 -05:00
|
|
|
use syntax::ast;
|
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
pub fn root_and_write_guard<'a, K:KindOps>(datum: &Datum<K>,
|
|
|
|
bcx: &'a Block<'a>,
|
|
|
|
span: Span,
|
|
|
|
expr_id: ast::NodeId,
|
|
|
|
derefs: uint) -> &'a Block<'a> {
|
2013-05-03 21:07:33 -05:00
|
|
|
let key = root_map_key { id: expr_id, derefs: derefs };
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("write_guard::root_and_write_guard(key={:?})", key);
|
2013-05-03 21:07:33 -05:00
|
|
|
|
|
|
|
// root the autoderef'd value, if necessary:
|
|
|
|
//
|
|
|
|
// (Note: root'd values are always boxes)
|
|
|
|
let ccx = bcx.ccx();
|
2014-03-20 21:49:20 -05:00
|
|
|
match ccx.maps.root_map.borrow().find(&key) {
|
2013-12-30 20:57:48 -06:00
|
|
|
None => bcx,
|
|
|
|
Some(&root_info) => root(datum, bcx, span, key, root_info)
|
2013-05-03 21:07:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
fn root<'a, K:KindOps>(datum: &Datum<K>,
|
|
|
|
bcx: &'a Block<'a>,
|
|
|
|
_span: Span,
|
|
|
|
root_key: root_map_key,
|
|
|
|
root_info: RootInfo) -> &'a Block<'a> {
|
2014-02-01 09:53:54 -06:00
|
|
|
//! In some cases, borrowck will decide that an @T value must be
|
|
|
|
//! rooted for the program to be safe. In that case, we will call
|
|
|
|
//! this function, which will stash a copy away until we exit the
|
|
|
|
//! scope `scope_id`.
|
2013-05-03 21:07:33 -05:00
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("write_guard::root(root_key={:?}, root_info={:?}, datum={:?})",
|
2013-05-03 21:07:33 -05:00
|
|
|
root_key, root_info, datum.to_str(bcx.ccx()));
|
|
|
|
|
2014-01-15 13:39:08 -06:00
|
|
|
// Root the datum. Note that we must zero this value,
|
2013-05-03 21:07:33 -05:00
|
|
|
// because sometimes we root on one path but not another.
|
|
|
|
// See e.g. #4904.
|
2014-01-15 13:39:08 -06:00
|
|
|
lvalue_scratch_datum(
|
|
|
|
bcx, datum.ty, "__write_guard", true,
|
|
|
|
cleanup::AstScope(root_info.scope), (),
|
|
|
|
|(), bcx, llval| datum.shallow_copy_and_take(bcx, llval)).bcx
|
2013-05-03 21:07:33 -05:00
|
|
|
}
|