2013-05-03 22:07:33 -04: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.
|
|
|
|
|
|
|
|
//! Logic relating to rooting and write guards for managed values
|
|
|
|
//! (`@` and `@mut`). This code is primarily for use by datum;
|
|
|
|
//! 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 15:28:44 -07:00
|
|
|
|
2013-12-30 18:57:48 -08:00
|
|
|
use middle::borrowck::{RootInfo, root_map_key};
|
2013-05-03 22:07:33 -04:00
|
|
|
use middle::trans::base::*;
|
|
|
|
use middle::trans::common::*;
|
|
|
|
use middle::trans::datum::*;
|
2013-08-31 18:13:04 +02:00
|
|
|
use syntax::codemap::Span;
|
2013-05-03 22:07:33 -04:00
|
|
|
use syntax::ast;
|
|
|
|
|
2014-01-07 08:54:58 -08:00
|
|
|
pub fn root_and_write_guard<'a>(
|
|
|
|
datum: &Datum,
|
|
|
|
bcx: &'a Block<'a>,
|
2013-08-31 18:13:04 +02:00
|
|
|
span: Span,
|
2013-07-27 10:25:59 +02:00
|
|
|
expr_id: ast::NodeId,
|
2014-01-07 08:54:58 -08:00
|
|
|
derefs: uint)
|
|
|
|
-> &'a Block<'a> {
|
2013-05-03 22:07:33 -04:00
|
|
|
let key = root_map_key { id: expr_id, derefs: derefs };
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("write_guard::root_and_write_guard(key={:?})", key);
|
2013-05-03 22:07:33 -04:00
|
|
|
|
|
|
|
// root the autoderef'd value, if necessary:
|
|
|
|
//
|
|
|
|
// (Note: root'd values are always boxes)
|
|
|
|
let ccx = bcx.ccx();
|
2013-12-30 18:57:48 -08:00
|
|
|
let root_map = ccx.maps.root_map.borrow();
|
|
|
|
match root_map.get().find(&key) {
|
|
|
|
None => bcx,
|
|
|
|
Some(&root_info) => root(datum, bcx, span, key, root_info)
|
2013-05-03 22:07:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 08:54:58 -08:00
|
|
|
fn root<'a>(
|
|
|
|
datum: &Datum,
|
|
|
|
bcx: &'a Block<'a>,
|
2013-12-30 18:57:48 -08:00
|
|
|
_: Span,
|
2013-05-03 22:07:33 -04:00
|
|
|
root_key: root_map_key,
|
2014-01-07 08:54:58 -08:00
|
|
|
root_info: RootInfo)
|
|
|
|
-> &'a Block<'a> {
|
2013-05-03 22:07:33 -04:00
|
|
|
//! In some cases, borrowck will decide that an @T/@[]/@str
|
|
|
|
//! 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-10-21 13:08:31 -07:00
|
|
|
debug!("write_guard::root(root_key={:?}, root_info={:?}, datum={:?})",
|
2013-05-03 22:07:33 -04:00
|
|
|
root_key, root_info, datum.to_str(bcx.ccx()));
|
|
|
|
|
|
|
|
// First, root the datum. Note that we must zero this value,
|
|
|
|
// because sometimes we root on one path but not another.
|
|
|
|
// See e.g. #4904.
|
2013-06-20 15:21:37 -04:00
|
|
|
let scratch = scratch_datum(bcx, datum.ty, "__write_guard", true);
|
2013-05-03 22:07:33 -04:00
|
|
|
datum.copy_to_datum(bcx, INIT, scratch);
|
|
|
|
let cleanup_bcx = find_bcx_for_scope(bcx, root_info.scope);
|
2013-12-18 14:54:42 -08:00
|
|
|
add_clean_temp_mem_in_scope(cleanup_bcx,
|
|
|
|
root_info.scope,
|
|
|
|
scratch.val,
|
|
|
|
scratch.ty);
|
2013-05-03 22:07:33 -04:00
|
|
|
|
|
|
|
bcx
|
|
|
|
}
|
|
|
|
|