]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/trans/write_guard.rs
Register new snapshots
[rust.git] / src / librustc / middle / trans / write_guard.rs
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.
4 //
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.
10
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`).
16
17
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;
23 use syntax::ast;
24
25 pub fn root_and_write_guard<'a, K:KindOps>(datum: &Datum<K>,
26                                            bcx: &'a Block<'a>,
27                                            span: Span,
28                                            expr_id: ast::NodeId,
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);
32
33     // root the autoderef'd value, if necessary:
34     //
35     // (Note: root'd values are always boxes)
36     let ccx = bcx.ccx();
37     match ccx.maps.root_map.find(&key) {
38         None => bcx,
39         Some(&root_info) => root(datum, bcx, span, key, root_info)
40     }
41 }
42
43 fn root<'a, K:KindOps>(datum: &Datum<K>,
44                        bcx: &'a Block<'a>,
45                        _span: Span,
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
51     //! scope `scope_id`.
52
53     debug!("write_guard::root(root_key={:?}, root_info={:?}, datum={:?})",
54            root_key, root_info, datum.to_str(bcx.ccx()));
55
56     // Root the datum. Note that we must zero this value,
57     // because sometimes we root on one path but not another.
58     // See e.g. #4904.
59     lvalue_scratch_datum(
60         bcx, datum.ty, "__write_guard", true,
61         cleanup::AstScope(root_info.scope), (),
62         |(), bcx, llval| datum.shallow_copy_and_take(bcx, llval)).bcx
63 }