2012-12-03 18:48:01 -06: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.
|
|
|
|
|
2012-07-24 10:12:15 -05:00
|
|
|
/*!
|
2012-04-01 16:28:30 -05:00
|
|
|
|
2012-07-24 10:12:15 -05:00
|
|
|
This file actually contains two passes related to regions. The first
|
2013-04-02 00:32:37 -05:00
|
|
|
pass builds up the `scope_map`, which describes the parent links in
|
2012-07-24 10:12:15 -05:00
|
|
|
the region hierarchy. The second pass infers which types must be
|
|
|
|
region parameterized.
|
2012-04-01 16:28:30 -05:00
|
|
|
|
2013-06-06 10:57:37 -05:00
|
|
|
Most of the documentation on regions can be found in
|
|
|
|
`middle/typeck/infer/region_inference.rs`
|
|
|
|
|
2012-04-01 16:28:30 -05:00
|
|
|
*/
|
2012-03-09 18:39:54 -06:00
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2012-10-15 16:56:42 -05:00
|
|
|
use driver::session::Session;
|
2013-10-29 05:08:34 -05:00
|
|
|
use middle::ty::{FreeRegion};
|
2012-12-13 15:05:22 -06:00
|
|
|
use middle::ty;
|
2012-09-04 13:54:36 -05:00
|
|
|
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::hashmap::{HashMap, HashSet};
|
2013-08-31 11:13:04 -05:00
|
|
|
use syntax::codemap::Span;
|
2013-08-13 07:36:10 -05:00
|
|
|
use syntax::{ast, visit};
|
|
|
|
use syntax::visit::{Visitor,fn_kind};
|
2013-09-01 20:45:37 -05:00
|
|
|
use syntax::ast::{Block,item,fn_decl,NodeId,Arm,Pat,Stmt,Expr,Local};
|
2012-03-09 18:39:54 -06:00
|
|
|
|
2012-08-17 16:09:20 -05:00
|
|
|
/**
|
2013-04-02 00:32:37 -05:00
|
|
|
The region maps encode information about region relationships.
|
|
|
|
|
|
|
|
- `scope_map` maps from:
|
|
|
|
- an expression to the expression or block encoding the maximum
|
|
|
|
(static) lifetime of a value produced by that expression. This is
|
|
|
|
generally the innermost call, statement, match, or block.
|
|
|
|
- a variable or binding id to the block in which that variable is declared.
|
|
|
|
- `free_region_map` maps from:
|
|
|
|
- a free region `a` to a list of free regions `bs` such that
|
|
|
|
`a <= b for all b in bs`
|
|
|
|
- the free region map is populated during type check as we check
|
|
|
|
each function. See the function `relate_free_regions` for
|
|
|
|
more information.
|
2013-03-15 14:24:24 -05:00
|
|
|
- `cleanup_scopes` includes scopes where trans cleanups occur
|
|
|
|
- this is intended to reflect the current state of trans, not
|
|
|
|
necessarily how I think things ought to work
|
2012-08-17 16:09:20 -05:00
|
|
|
*/
|
2013-04-02 00:32:37 -05:00
|
|
|
pub struct RegionMaps {
|
2013-07-27 03:25:59 -05:00
|
|
|
priv scope_map: HashMap<ast::NodeId, ast::NodeId>,
|
2013-04-02 00:32:37 -05:00
|
|
|
priv free_region_map: HashMap<FreeRegion, ~[FreeRegion]>,
|
2013-07-27 03:25:59 -05:00
|
|
|
priv cleanup_scopes: HashSet<ast::NodeId>
|
2013-04-02 00:32:37 -05:00
|
|
|
}
|
2012-03-12 14:43:02 -05:00
|
|
|
|
2013-07-02 14:47:32 -05:00
|
|
|
#[deriving(Clone)]
|
2013-03-15 14:24:24 -05:00
|
|
|
pub struct Context {
|
|
|
|
// Scope where variables should be parented to
|
2013-07-27 03:25:59 -05:00
|
|
|
var_parent: Option<ast::NodeId>,
|
2013-03-15 14:24:24 -05:00
|
|
|
|
|
|
|
// Innermost enclosing expression
|
2013-07-27 03:25:59 -05:00
|
|
|
parent: Option<ast::NodeId>,
|
2012-08-17 16:09:20 -05:00
|
|
|
}
|
2012-03-12 14:43:02 -05:00
|
|
|
|
2013-09-25 03:59:56 -05:00
|
|
|
struct RegionResolutionVisitor {
|
|
|
|
sess: Session,
|
|
|
|
|
|
|
|
// Generated maps:
|
|
|
|
region_maps: @mut RegionMaps,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl RegionMaps {
|
|
|
|
pub fn relate_free_regions(&mut self, sub: FreeRegion, sup: FreeRegion) {
|
2013-04-02 00:32:37 -05:00
|
|
|
match self.free_region_map.find_mut(&sub) {
|
|
|
|
Some(sups) => {
|
2013-07-04 21:13:26 -05:00
|
|
|
if !sups.iter().any(|x| x == &sup) {
|
2013-04-02 00:32:37 -05:00
|
|
|
sups.push(sup);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
None => {}
|
2012-03-11 15:28:43 -05:00
|
|
|
}
|
2013-04-02 00:32:37 -05:00
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("relate_free_regions(sub={:?}, sup={:?})", sub, sup);
|
2013-04-02 00:32:37 -05:00
|
|
|
|
|
|
|
self.free_region_map.insert(sub, ~[sup]);
|
2012-03-11 15:28:43 -05:00
|
|
|
}
|
|
|
|
|
2013-07-27 03:25:59 -05:00
|
|
|
pub fn record_parent(&mut self, sub: ast::NodeId, sup: ast::NodeId) {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("record_parent(sub={:?}, sup={:?})", sub, sup);
|
2013-03-15 14:24:24 -05:00
|
|
|
assert!(sub != sup);
|
2012-10-19 08:01:01 -05:00
|
|
|
|
2013-04-02 00:32:37 -05:00
|
|
|
self.scope_map.insert(sub, sup);
|
|
|
|
}
|
|
|
|
|
2013-07-27 03:25:59 -05:00
|
|
|
pub fn record_cleanup_scope(&mut self, scope_id: ast::NodeId) {
|
2013-03-15 14:24:24 -05:00
|
|
|
//! Records that a scope is a CLEANUP SCOPE. This is invoked
|
|
|
|
//! from within regionck. We wait until regionck because we do
|
|
|
|
//! not know which operators are overloaded until that point,
|
|
|
|
//! and only overloaded operators result in cleanup scopes.
|
|
|
|
|
|
|
|
self.cleanup_scopes.insert(scope_id);
|
|
|
|
}
|
|
|
|
|
2013-07-27 03:25:59 -05:00
|
|
|
pub fn opt_encl_scope(&self, id: ast::NodeId) -> Option<ast::NodeId> {
|
2013-04-02 00:32:37 -05:00
|
|
|
//! Returns the narrowest scope that encloses `id`, if any.
|
|
|
|
|
2013-09-20 01:08:47 -05:00
|
|
|
self.scope_map.find(&id).map(|x| *x)
|
2013-04-02 00:32:37 -05:00
|
|
|
}
|
|
|
|
|
2013-07-27 03:25:59 -05:00
|
|
|
pub fn encl_scope(&self, id: ast::NodeId) -> ast::NodeId {
|
2013-04-02 00:32:37 -05:00
|
|
|
//! Returns the narrowest scope that encloses `id`, if any.
|
|
|
|
|
|
|
|
match self.scope_map.find(&id) {
|
|
|
|
Some(&r) => r,
|
2013-10-21 15:08:31 -05:00
|
|
|
None => { fail!("No enclosing scope for id {:?}", id); }
|
2013-04-02 00:32:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-27 03:25:59 -05:00
|
|
|
pub fn is_cleanup_scope(&self, scope_id: ast::NodeId) -> bool {
|
2013-03-15 14:24:24 -05:00
|
|
|
self.cleanup_scopes.contains(&scope_id)
|
|
|
|
}
|
|
|
|
|
2013-07-27 03:25:59 -05:00
|
|
|
pub fn cleanup_scope(&self, expr_id: ast::NodeId) -> ast::NodeId {
|
2013-03-15 14:24:24 -05:00
|
|
|
//! Returns the scope when temps in expr will be cleaned up
|
|
|
|
|
|
|
|
let mut id = self.encl_scope(expr_id);
|
|
|
|
while !self.cleanup_scopes.contains(&id) {
|
|
|
|
id = self.encl_scope(id);
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2013-07-27 03:25:59 -05:00
|
|
|
pub fn encl_region(&self, id: ast::NodeId) -> ty::Region {
|
2013-04-02 00:32:37 -05:00
|
|
|
//! Returns the narrowest scope region that encloses `id`, if any.
|
2012-10-19 08:01:01 -05:00
|
|
|
|
2013-10-29 09:34:11 -05:00
|
|
|
ty::ReScope(self.encl_scope(id))
|
2013-04-02 00:32:37 -05:00
|
|
|
}
|
|
|
|
|
2013-07-27 03:25:59 -05:00
|
|
|
pub fn scopes_intersect(&self, scope1: ast::NodeId, scope2: ast::NodeId)
|
2013-05-31 17:17:22 -05:00
|
|
|
-> bool {
|
|
|
|
self.is_subscope_of(scope1, scope2) ||
|
|
|
|
self.is_subscope_of(scope2, scope1)
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn is_subscope_of(&self,
|
2013-07-27 03:25:59 -05:00
|
|
|
subscope: ast::NodeId,
|
|
|
|
superscope: ast::NodeId)
|
2013-05-31 17:17:22 -05:00
|
|
|
-> bool {
|
2013-04-02 00:32:37 -05:00
|
|
|
/*!
|
2013-03-15 14:24:24 -05:00
|
|
|
* Returns true if `subscope` is equal to or is lexically
|
2013-04-02 00:32:37 -05:00
|
|
|
* nested inside `superscope` and false otherwise.
|
|
|
|
*/
|
|
|
|
|
2013-03-15 14:24:24 -05:00
|
|
|
let mut s = subscope;
|
|
|
|
while superscope != s {
|
|
|
|
match self.scope_map.find(&s) {
|
|
|
|
None => {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("is_subscope_of({:?}, {:?}, s={:?})=false",
|
2013-03-15 14:24:24 -05:00
|
|
|
subscope, superscope, s);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Some(&scope) => s = scope
|
2012-10-19 08:01:01 -05:00
|
|
|
}
|
2012-07-26 10:51:57 -05:00
|
|
|
}
|
2013-03-15 14:24:24 -05:00
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("is_subscope_of({:?}, {:?})=true",
|
2013-03-15 14:24:24 -05:00
|
|
|
subscope, superscope);
|
|
|
|
|
2013-04-02 00:32:37 -05:00
|
|
|
return true;
|
|
|
|
}
|
2012-07-26 10:51:57 -05:00
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn sub_free_region(&self, sub: FreeRegion, sup: FreeRegion) -> bool {
|
2013-04-02 00:32:37 -05:00
|
|
|
/*!
|
|
|
|
* Determines whether two free regions have a subregion relationship
|
|
|
|
* by walking the graph encoded in `free_region_map`. Note that
|
|
|
|
* it is possible that `sub != sup` and `sub <= sup` and `sup <= sub`
|
|
|
|
* (that is, the user can give two different names to the same lifetime).
|
|
|
|
*/
|
|
|
|
|
|
|
|
if sub == sup {
|
|
|
|
return true;
|
|
|
|
}
|
2012-03-26 17:07:15 -05:00
|
|
|
|
2013-04-02 00:32:37 -05:00
|
|
|
// Do a little breadth-first-search here. The `queue` list
|
|
|
|
// doubles as a way to detect if we've seen a particular FR
|
|
|
|
// before. Note that we expect this graph to be an *extremely
|
|
|
|
// shallow* tree.
|
|
|
|
let mut queue = ~[sub];
|
|
|
|
let mut i = 0;
|
|
|
|
while i < queue.len() {
|
|
|
|
match self.free_region_map.find(&queue[i]) {
|
|
|
|
Some(parents) => {
|
2013-08-03 11:45:23 -05:00
|
|
|
for parent in parents.iter() {
|
2013-04-02 00:32:37 -05:00
|
|
|
if *parent == sup {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-04 21:13:26 -05:00
|
|
|
if !queue.iter().any(|x| x == parent) {
|
2013-04-02 00:32:37 -05:00
|
|
|
queue.push(*parent);
|
|
|
|
}
|
|
|
|
}
|
2012-03-26 17:07:15 -05:00
|
|
|
}
|
2013-04-02 00:32:37 -05:00
|
|
|
None => {}
|
2012-03-26 17:07:15 -05:00
|
|
|
}
|
2013-04-02 00:32:37 -05:00
|
|
|
i += 1;
|
2012-03-26 17:07:15 -05:00
|
|
|
}
|
2013-04-02 00:32:37 -05:00
|
|
|
return false;
|
2012-03-26 17:07:15 -05:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn is_subregion_of(&self,
|
|
|
|
sub_region: ty::Region,
|
|
|
|
super_region: ty::Region)
|
|
|
|
-> bool {
|
2013-04-02 00:32:37 -05:00
|
|
|
/*!
|
|
|
|
* Determines whether one region is a subregion of another. This is
|
|
|
|
* intended to run *after inference* and sadly the logic is somewhat
|
|
|
|
* duplicated with the code in infer.rs.
|
|
|
|
*/
|
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("is_subregion_of(sub_region={:?}, super_region={:?})",
|
2013-04-02 00:32:37 -05:00
|
|
|
sub_region, super_region);
|
|
|
|
|
|
|
|
sub_region == super_region || {
|
|
|
|
match (sub_region, super_region) {
|
2013-10-29 09:34:11 -05:00
|
|
|
(_, ty::ReStatic) => {
|
2013-04-02 00:32:37 -05:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2013-10-29 09:34:11 -05:00
|
|
|
(ty::ReScope(sub_scope), ty::ReScope(super_scope)) => {
|
2013-03-15 14:24:24 -05:00
|
|
|
self.is_subscope_of(sub_scope, super_scope)
|
2013-04-02 00:32:37 -05:00
|
|
|
}
|
2012-03-26 17:07:15 -05:00
|
|
|
|
2013-10-29 09:34:11 -05:00
|
|
|
(ty::ReScope(sub_scope), ty::ReFree(ref fr)) => {
|
2013-03-15 14:24:24 -05:00
|
|
|
self.is_subscope_of(sub_scope, fr.scope_id)
|
2013-04-02 00:32:37 -05:00
|
|
|
}
|
2012-04-01 16:28:30 -05:00
|
|
|
|
2013-10-29 09:34:11 -05:00
|
|
|
(ty::ReFree(sub_fr), ty::ReFree(super_fr)) => {
|
2013-04-02 00:32:37 -05:00
|
|
|
self.sub_free_region(sub_fr, super_fr)
|
|
|
|
}
|
2012-04-01 16:28:30 -05:00
|
|
|
|
2013-04-02 00:32:37 -05:00
|
|
|
_ => {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-26 18:02:01 -05:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn nearest_common_ancestor(&self,
|
2013-07-27 03:25:59 -05:00
|
|
|
scope_a: ast::NodeId,
|
|
|
|
scope_b: ast::NodeId)
|
|
|
|
-> Option<ast::NodeId> {
|
2013-04-02 00:32:37 -05:00
|
|
|
/*!
|
|
|
|
* Finds the nearest common ancestor (if any) of two scopes. That
|
|
|
|
* is, finds the smallest scope which is greater than or equal to
|
|
|
|
* both `scope_a` and `scope_b`.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if scope_a == scope_b { return Some(scope_a); }
|
|
|
|
|
|
|
|
let a_ancestors = ancestors_of(self, scope_a);
|
|
|
|
let b_ancestors = ancestors_of(self, scope_b);
|
2013-05-14 04:52:12 -05:00
|
|
|
let mut a_index = a_ancestors.len() - 1u;
|
|
|
|
let mut b_index = b_ancestors.len() - 1u;
|
2013-04-02 00:32:37 -05:00
|
|
|
|
|
|
|
// Here, ~[ab]_ancestors is a vector going from narrow to broad.
|
|
|
|
// The end of each vector will be the item where the scope is
|
|
|
|
// defined; if there are any common ancestors, then the tails of
|
|
|
|
// the vector will be the same. So basically we want to walk
|
|
|
|
// backwards from the tail of each vector and find the first point
|
|
|
|
// where they diverge. If one vector is a suffix of the other,
|
|
|
|
// then the corresponding scope is a superscope of the other.
|
|
|
|
|
2012-04-26 18:02:01 -05:00
|
|
|
if a_ancestors[a_index] != b_ancestors[b_index] {
|
2013-04-02 00:32:37 -05:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
loop {
|
|
|
|
// Loop invariant: a_ancestors[a_index] == b_ancestors[b_index]
|
|
|
|
// for all indices between a_index and the end of the array
|
|
|
|
if a_index == 0u { return Some(scope_a); }
|
|
|
|
if b_index == 0u { return Some(scope_b); }
|
|
|
|
a_index -= 1u;
|
|
|
|
b_index -= 1u;
|
|
|
|
if a_ancestors[a_index] != b_ancestors[b_index] {
|
|
|
|
return Some(a_ancestors[a_index + 1u]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-27 03:25:59 -05:00
|
|
|
fn ancestors_of(this: &RegionMaps, scope: ast::NodeId)
|
|
|
|
-> ~[ast::NodeId]
|
2013-04-02 00:32:37 -05:00
|
|
|
{
|
2013-10-21 15:08:31 -05:00
|
|
|
// debug!("ancestors_of(scope={})", scope);
|
2013-04-02 00:32:37 -05:00
|
|
|
let mut result = ~[scope];
|
|
|
|
let mut scope = scope;
|
|
|
|
loop {
|
2013-05-10 17:15:06 -05:00
|
|
|
match this.scope_map.find(&scope) {
|
2013-04-02 00:32:37 -05:00
|
|
|
None => return result,
|
|
|
|
Some(&superscope) => {
|
|
|
|
result.push(superscope);
|
|
|
|
scope = superscope;
|
|
|
|
}
|
|
|
|
}
|
2013-10-21 15:08:31 -05:00
|
|
|
// debug!("ancestors_of_loop(scope={})", scope);
|
2013-04-02 00:32:37 -05:00
|
|
|
}
|
2012-04-26 18:02:01 -05:00
|
|
|
}
|
2012-03-26 17:07:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-24 10:12:15 -05:00
|
|
|
/// Records the current parent (if any) as the parent of `child_id`.
|
2013-09-25 03:59:56 -05:00
|
|
|
fn parent_to_expr(visitor: &mut RegionResolutionVisitor,
|
|
|
|
cx: Context, child_id: ast::NodeId, sp: Span) {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("region::parent_to_expr(span={:?})",
|
2013-09-25 03:59:56 -05:00
|
|
|
visitor.sess.codemap.span_to_str(sp));
|
2013-08-03 11:45:23 -05:00
|
|
|
for parent_id in cx.parent.iter() {
|
2013-09-25 03:59:56 -05:00
|
|
|
visitor.region_maps.record_parent(child_id, *parent_id);
|
2012-03-09 18:39:54 -06:00
|
|
|
}
|
2012-03-12 14:43:02 -05:00
|
|
|
}
|
|
|
|
|
2013-08-13 07:36:10 -05:00
|
|
|
fn resolve_block(visitor: &mut RegionResolutionVisitor,
|
|
|
|
blk: &ast::Block,
|
|
|
|
cx: Context) {
|
2012-03-12 14:43:02 -05:00
|
|
|
// Record the parent of this block.
|
2013-09-25 03:59:56 -05:00
|
|
|
parent_to_expr(visitor, cx, blk.id, blk.span);
|
2012-03-09 18:39:54 -06:00
|
|
|
|
2012-03-12 14:43:02 -05:00
|
|
|
// Descend.
|
2013-07-16 13:08:35 -05:00
|
|
|
let new_cx = Context {var_parent: Some(blk.id),
|
2013-09-25 03:59:56 -05:00
|
|
|
parent: Some(blk.id)};
|
2013-08-13 07:36:10 -05:00
|
|
|
visit::walk_block(visitor, blk, new_cx);
|
2012-03-09 18:39:54 -06:00
|
|
|
}
|
|
|
|
|
2013-08-13 07:36:10 -05:00
|
|
|
fn resolve_arm(visitor: &mut RegionResolutionVisitor,
|
2013-09-01 20:45:37 -05:00
|
|
|
arm: &ast::Arm,
|
2013-08-13 07:36:10 -05:00
|
|
|
cx: Context) {
|
|
|
|
visit::walk_arm(visitor, arm, cx);
|
2012-03-11 14:05:17 -05:00
|
|
|
}
|
|
|
|
|
2013-08-13 07:36:10 -05:00
|
|
|
fn resolve_pat(visitor: &mut RegionResolutionVisitor,
|
2013-11-25 12:22:21 -06:00
|
|
|
pat: &ast::Pat,
|
2013-08-13 07:36:10 -05:00
|
|
|
cx: Context) {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(cx.var_parent, cx.parent);
|
2013-09-25 03:59:56 -05:00
|
|
|
parent_to_expr(visitor, cx, pat.id, pat.span);
|
2013-08-13 07:36:10 -05:00
|
|
|
visit::walk_pat(visitor, pat, cx);
|
2012-03-11 14:05:17 -05:00
|
|
|
}
|
|
|
|
|
2013-08-13 07:36:10 -05:00
|
|
|
fn resolve_stmt(visitor: &mut RegionResolutionVisitor,
|
2013-09-01 20:45:37 -05:00
|
|
|
stmt: @ast::Stmt,
|
2013-08-13 07:36:10 -05:00
|
|
|
cx: Context) {
|
2012-08-17 16:09:20 -05:00
|
|
|
match stmt.node {
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::StmtDecl(*) => {
|
2013-08-13 07:36:10 -05:00
|
|
|
visit::walk_stmt(visitor, stmt, cx);
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::StmtExpr(_, stmt_id) |
|
|
|
|
ast::StmtSemi(_, stmt_id) => {
|
2013-09-25 03:59:56 -05:00
|
|
|
parent_to_expr(visitor, cx, stmt_id, stmt.span);
|
2013-03-15 14:24:24 -05:00
|
|
|
let expr_cx = Context {parent: Some(stmt_id), ..cx};
|
2013-08-13 07:36:10 -05:00
|
|
|
visit::walk_stmt(visitor, stmt, expr_cx);
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
2013-09-25 03:59:56 -05:00
|
|
|
ast::StmtMac(*) => visitor.sess.bug("unexpanded macro")
|
2012-08-17 16:09:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-13 07:36:10 -05:00
|
|
|
fn resolve_expr(visitor: &mut RegionResolutionVisitor,
|
2013-09-01 20:45:37 -05:00
|
|
|
expr: @ast::Expr,
|
2013-08-13 07:36:10 -05:00
|
|
|
cx: Context) {
|
2013-09-25 03:59:56 -05:00
|
|
|
parent_to_expr(visitor, cx, expr.id, expr.span);
|
2012-07-26 10:51:57 -05:00
|
|
|
|
|
|
|
let mut new_cx = cx;
|
2013-03-15 14:24:24 -05:00
|
|
|
new_cx.parent = Some(expr.id);
|
2012-08-06 14:34:08 -05:00
|
|
|
match expr.node {
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprAssignOp(*) | ast::ExprIndex(*) | ast::ExprBinary(*) |
|
|
|
|
ast::ExprUnary(*) | ast::ExprCall(*) | ast::ExprMethodCall(*) => {
|
2013-05-06 13:02:28 -05:00
|
|
|
// FIXME(#6268) Nested method calls
|
2013-03-15 14:24:24 -05:00
|
|
|
//
|
|
|
|
// The lifetimes for a call or method call look as follows:
|
|
|
|
//
|
|
|
|
// call.id
|
|
|
|
// - arg0.id
|
|
|
|
// - ...
|
|
|
|
// - argN.id
|
|
|
|
// - call.callee_id
|
|
|
|
//
|
|
|
|
// The idea is that call.callee_id represents *the time when
|
|
|
|
// the invoked function is actually running* and call.id
|
|
|
|
// represents *the time to prepare the arguments and make the
|
|
|
|
// call*. See the section "Borrows in Calls" borrowck/doc.rs
|
|
|
|
// for an extended explanantion of why this distinction is
|
|
|
|
// important.
|
|
|
|
//
|
|
|
|
// parent_to_expr(new_cx, expr.callee_id);
|
|
|
|
}
|
|
|
|
|
2013-09-01 20:45:37 -05:00
|
|
|
ast::ExprMatch(*) => {
|
2013-03-15 14:24:24 -05:00
|
|
|
new_cx.var_parent = Some(expr.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {}
|
2012-07-26 10:51:57 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-08-13 07:36:10 -05:00
|
|
|
visit::walk_expr(visitor, expr, new_cx);
|
2012-03-11 19:01:28 -05:00
|
|
|
}
|
|
|
|
|
2013-08-13 07:36:10 -05:00
|
|
|
fn resolve_local(visitor: &mut RegionResolutionVisitor,
|
|
|
|
local: @ast::Local,
|
|
|
|
cx: Context) {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(cx.var_parent, cx.parent);
|
2013-09-25 03:59:56 -05:00
|
|
|
parent_to_expr(visitor, cx, local.id, local.span);
|
2013-08-13 07:36:10 -05:00
|
|
|
visit::walk_local(visitor, local, cx);
|
2012-03-14 13:41:20 -05:00
|
|
|
}
|
|
|
|
|
2013-08-13 07:36:10 -05:00
|
|
|
fn resolve_item(visitor: &mut RegionResolutionVisitor,
|
|
|
|
item: @ast::item,
|
|
|
|
cx: Context) {
|
2012-03-09 18:39:54 -06:00
|
|
|
// Items create a new outer block scope as far as we're concerned.
|
2013-03-15 14:24:24 -05:00
|
|
|
let new_cx = Context {var_parent: None, parent: None, ..cx};
|
2013-08-13 07:36:10 -05:00
|
|
|
visit::walk_item(visitor, item, new_cx);
|
2012-03-09 18:39:54 -06:00
|
|
|
}
|
|
|
|
|
2013-08-13 07:36:10 -05:00
|
|
|
fn resolve_fn(visitor: &mut RegionResolutionVisitor,
|
|
|
|
fk: &visit::fn_kind,
|
2013-06-27 08:04:22 -05:00
|
|
|
decl: &ast::fn_decl,
|
2013-07-19 00:38:55 -05:00
|
|
|
body: &ast::Block,
|
2013-08-31 11:13:04 -05:00
|
|
|
sp: Span,
|
2013-07-27 03:25:59 -05:00
|
|
|
id: ast::NodeId,
|
2013-08-13 07:36:10 -05:00
|
|
|
cx: Context) {
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("region::resolve_fn(id={:?}, \
|
2013-09-28 00:38:08 -05:00
|
|
|
span={:?}, \
|
|
|
|
body.id={:?}, \
|
|
|
|
cx.parent={:?})",
|
2013-06-04 23:43:41 -05:00
|
|
|
id,
|
2013-09-25 03:59:56 -05:00
|
|
|
visitor.sess.codemap.span_to_str(sp),
|
2013-07-16 13:08:35 -05:00
|
|
|
body.id,
|
2013-06-04 23:43:41 -05:00
|
|
|
cx.parent);
|
2013-03-15 14:24:24 -05:00
|
|
|
|
|
|
|
// The arguments and `self` are parented to the body of the fn.
|
2013-07-16 13:08:35 -05:00
|
|
|
let decl_cx = Context {parent: Some(body.id),
|
|
|
|
var_parent: Some(body.id),
|
2013-03-15 14:24:24 -05:00
|
|
|
..cx};
|
2013-02-18 23:25:44 -06:00
|
|
|
match *fk {
|
2013-08-13 07:36:10 -05:00
|
|
|
visit::fk_method(_, _, method) => {
|
2013-09-25 03:59:56 -05:00
|
|
|
visitor.region_maps.record_parent(method.self_id, body.id);
|
2013-01-23 20:15:06 -06:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
2013-08-13 07:36:10 -05:00
|
|
|
visit::walk_fn_decl(visitor, decl, decl_cx);
|
2013-01-23 20:15:06 -06:00
|
|
|
|
2013-03-15 14:24:24 -05:00
|
|
|
// The body of the fn itself is either a root scope (top-level fn)
|
|
|
|
// or it continues with the inherited scope (closures).
|
|
|
|
let body_cx = match *fk {
|
2013-08-13 07:36:10 -05:00
|
|
|
visit::fk_item_fn(*) |
|
|
|
|
visit::fk_method(*) => {
|
2013-03-15 14:24:24 -05:00
|
|
|
Context {parent: None, var_parent: None, ..cx}
|
|
|
|
}
|
2013-08-13 07:36:10 -05:00
|
|
|
visit::fk_anon(*) |
|
|
|
|
visit::fk_fn_block(*) => {
|
2013-03-15 14:24:24 -05:00
|
|
|
cx
|
|
|
|
}
|
|
|
|
};
|
2013-08-13 07:36:10 -05:00
|
|
|
visitor.visit_block(body, body_cx);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Visitor<Context> for RegionResolutionVisitor {
|
|
|
|
|
|
|
|
fn visit_block(&mut self, b:&Block, cx:Context) {
|
|
|
|
resolve_block(self, b, cx);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_item(&mut self, i:@item, cx:Context) {
|
|
|
|
resolve_item(self, i, cx);
|
|
|
|
}
|
|
|
|
|
2013-08-31 11:13:04 -05:00
|
|
|
fn visit_fn(&mut self, fk:&fn_kind, fd:&fn_decl, b:&Block, s:Span, n:NodeId, cx:Context) {
|
2013-08-13 07:36:10 -05:00
|
|
|
resolve_fn(self, fk, fd, b, s, n, cx);
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
fn visit_arm(&mut self, a:&Arm, cx:Context) {
|
2013-08-13 07:36:10 -05:00
|
|
|
resolve_arm(self, a, cx);
|
|
|
|
}
|
2013-11-25 12:22:21 -06:00
|
|
|
fn visit_pat(&mut self, p:&Pat, cx:Context) {
|
2013-08-13 07:36:10 -05:00
|
|
|
resolve_pat(self, p, cx);
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
fn visit_stmt(&mut self, s:@Stmt, cx:Context) {
|
2013-08-13 07:36:10 -05:00
|
|
|
resolve_stmt(self, s, cx);
|
|
|
|
}
|
2013-09-01 20:45:37 -05:00
|
|
|
fn visit_expr(&mut self, ex:@Expr, cx:Context) {
|
2013-08-13 07:36:10 -05:00
|
|
|
resolve_expr(self, ex, cx);
|
|
|
|
}
|
|
|
|
fn visit_local(&mut self, l:@Local, cx:Context) {
|
|
|
|
resolve_local(self, l, cx);
|
|
|
|
}
|
2012-04-26 18:02:01 -05:00
|
|
|
}
|
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn resolve_crate(sess: Session,
|
2013-07-19 00:38:55 -05:00
|
|
|
crate: &ast::Crate) -> @mut RegionMaps
|
2013-04-02 00:32:37 -05:00
|
|
|
{
|
|
|
|
let region_maps = @mut RegionMaps {
|
|
|
|
scope_map: HashMap::new(),
|
2013-03-15 14:24:24 -05:00
|
|
|
free_region_map: HashMap::new(),
|
|
|
|
cleanup_scopes: HashSet::new(),
|
2013-04-02 00:32:37 -05:00
|
|
|
};
|
2013-09-25 03:59:56 -05:00
|
|
|
let cx = Context {parent: None,
|
2013-03-15 14:24:24 -05:00
|
|
|
var_parent: None};
|
2013-09-25 03:59:56 -05:00
|
|
|
let mut visitor = RegionResolutionVisitor {
|
|
|
|
sess: sess,
|
|
|
|
region_maps: region_maps,
|
|
|
|
};
|
2013-08-13 07:36:10 -05:00
|
|
|
visit::walk_crate(&mut visitor, crate, cx);
|
2013-04-02 00:32:37 -05:00
|
|
|
return region_maps;
|
2012-03-09 18:39:54 -06:00
|
|
|
}
|
2013-06-12 12:16:30 -05:00
|
|
|
|