1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Logic relating to rooting and write guards for managed values.
12 //! This code is primarily for use by datum;
13 //! it exists in its own module both to keep datum.rs bite-sized
14 //! and for each in debugging (e.g., so you can use
15 //! `RUST_LOG=rustc::middle::trans::write_guard`).
18 use middle::borrowck::{RootInfo, root_map_key};
19 use middle::trans::cleanup;
20 use middle::trans::common::*;
21 use middle::trans::datum::*;
22 use syntax::codemap::Span;
25 pub fn root_and_write_guard<'a, K:KindOps>(datum: &Datum<K>,
29 derefs: uint) -> &'a Block<'a> {
30 let key = root_map_key { id: expr_id, derefs: derefs };
31 debug!("write_guard::root_and_write_guard(key={:?})", key);
33 // root the autoderef'd value, if necessary:
35 // (Note: root'd values are always boxes)
37 match ccx.maps.root_map.find(&key) {
39 Some(&root_info) => root(datum, bcx, span, key, root_info)
43 fn root<'a, K:KindOps>(datum: &Datum<K>,
46 root_key: root_map_key,
47 root_info: RootInfo) -> &'a Block<'a> {
48 //! In some cases, borrowck will decide that an @T value must be
49 //! rooted for the program to be safe. In that case, we will call
50 //! this function, which will stash a copy away until we exit the
53 debug!("write_guard::root(root_key={:?}, root_info={:?}, datum={:?})",
54 root_key, root_info, datum.to_str(bcx.ccx()));
56 // Root the datum. Note that we must zero this value,
57 // because sometimes we root on one path but not another.
60 bcx, datum.ty, "__write_guard", true,
61 cleanup::AstScope(root_info.scope), (),
62 |(), bcx, llval| datum.shallow_copy_and_take(bcx, llval)).bcx