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-04 16:53:12 -05:00
|
|
|
/*!
|
|
|
|
* # Categorization
|
|
|
|
*
|
|
|
|
* The job of the categorization module is to analyze an expression to
|
|
|
|
* determine what kind of memory is used in evaluating it (for example,
|
|
|
|
* where dereferences occur and what kind of pointer is dereferenced;
|
|
|
|
* whether the memory is mutable; etc)
|
|
|
|
*
|
|
|
|
* Categorization effectively transforms all of our expressions into
|
|
|
|
* expressions of the following forms (the actual enum has many more
|
|
|
|
* possibilities, naturally, but they are all variants of these base
|
|
|
|
* forms):
|
|
|
|
*
|
|
|
|
* E = rvalue // some computed rvalue
|
|
|
|
* | x // address of a local variable, arg, or upvar
|
|
|
|
* | *E // deref of a ptr
|
|
|
|
* | E.comp // access to an interior component
|
|
|
|
*
|
|
|
|
* Imagine a routine ToAddr(Expr) that evaluates an expression and returns an
|
|
|
|
* address where the result is to be found. If Expr is an lvalue, then this
|
|
|
|
* is the address of the lvalue. If Expr is an rvalue, this is the address of
|
|
|
|
* some temporary spot in memory where the result is stored.
|
|
|
|
*
|
|
|
|
* Now, cat_expr() classies the expression Expr and the address A=ToAddr(Expr)
|
|
|
|
* as follows:
|
|
|
|
*
|
|
|
|
* - cat: what kind of expression was this? This is a subset of the
|
|
|
|
* full expression forms which only includes those that we care about
|
|
|
|
* for the purpose of the analysis.
|
|
|
|
* - mutbl: mutability of the address A
|
|
|
|
* - ty: the type of data found at the address A
|
|
|
|
*
|
|
|
|
* The resulting categorization tree differs somewhat from the expressions
|
|
|
|
* themselves. For example, auto-derefs are explicit. Also, an index a[b] is
|
|
|
|
* decomposed into two operations: a derefence to reach the array data and
|
|
|
|
* then an index to jump forward to the relevant item.
|
|
|
|
*/
|
2012-06-01 12:46:17 -05:00
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::ty;
|
|
|
|
use middle::typeck;
|
2013-06-17 18:49:45 -05:00
|
|
|
use util::ppaux::{ty_to_str, region_ptr_to_str, Repr};
|
2012-12-23 16:41:37 -06:00
|
|
|
use util::common::indenter;
|
|
|
|
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::uint;
|
2012-09-04 13:54:36 -05:00
|
|
|
use syntax::ast::{m_imm, m_const, m_mutbl};
|
2012-12-23 16:41:37 -06:00
|
|
|
use syntax::ast;
|
2012-09-04 13:54:36 -05:00
|
|
|
use syntax::codemap::span;
|
|
|
|
use syntax::print::pprust;
|
2012-08-08 10:15:32 -05:00
|
|
|
|
2013-03-20 10:40:02 -05:00
|
|
|
#[deriving(Eq)]
|
2013-01-30 15:44:24 -06:00
|
|
|
pub enum categorization {
|
2013-06-20 14:25:52 -05:00
|
|
|
cat_rvalue(ast::node_id), // temporary val, argument is its scope
|
2013-03-15 14:24:24 -05:00
|
|
|
cat_static_item,
|
|
|
|
cat_implicit_self,
|
|
|
|
cat_copied_upvar(CopiedUpvar), // upvar copied into @fn or ~fn env
|
|
|
|
cat_stack_upvar(cmt), // by ref upvar from &fn
|
|
|
|
cat_local(ast::node_id), // local variable
|
2013-04-30 13:08:18 -05:00
|
|
|
cat_arg(ast::node_id), // formal argument
|
2013-03-15 14:24:24 -05:00
|
|
|
cat_deref(cmt, uint, ptr_kind), // deref of a ptr
|
2013-05-17 20:12:50 -05:00
|
|
|
cat_interior(cmt, InteriorKind), // something interior: field, tuple, etc
|
|
|
|
cat_downcast(cmt), // selects a particular enum variant (*)
|
2013-03-15 14:24:24 -05:00
|
|
|
cat_discr(cmt, ast::node_id), // match discriminant (see preserve())
|
|
|
|
cat_self(ast::node_id), // explicit `self`
|
2013-05-17 20:12:50 -05:00
|
|
|
|
|
|
|
// (*) downcast is only required if the enum has more than one variant
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[deriving(Eq)]
|
2013-06-17 18:06:36 -05:00
|
|
|
pub struct CopiedUpvar {
|
2013-03-15 14:24:24 -05:00
|
|
|
upvar_id: ast::node_id,
|
|
|
|
onceness: ast::Onceness,
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// different kinds of pointers:
|
2013-03-20 10:40:02 -05:00
|
|
|
#[deriving(Eq)]
|
2013-01-08 21:37:25 -06:00
|
|
|
pub enum ptr_kind {
|
2013-03-15 14:24:24 -05:00
|
|
|
uniq_ptr(ast::mutability),
|
2013-01-11 23:01:42 -06:00
|
|
|
gc_ptr(ast::mutability),
|
2013-01-23 20:15:06 -06:00
|
|
|
region_ptr(ast::mutability, ty::Region),
|
2012-08-27 18:26:35 -05:00
|
|
|
unsafe_ptr
|
|
|
|
}
|
|
|
|
|
2013-03-15 14:24:24 -05:00
|
|
|
// We use the term "interior" to mean "something reachable from the
|
|
|
|
// base without a pointer dereference", e.g. a field
|
2013-05-22 05:54:35 -05:00
|
|
|
#[deriving(Eq, IterBytes)]
|
2013-05-17 20:12:50 -05:00
|
|
|
pub enum InteriorKind {
|
|
|
|
InteriorField(FieldName),
|
2013-05-22 05:54:35 -05:00
|
|
|
InteriorElement(ElementKind),
|
2013-05-17 20:12:50 -05:00
|
|
|
}
|
|
|
|
|
2013-05-22 05:54:35 -05:00
|
|
|
#[deriving(Eq, IterBytes)]
|
2013-05-17 20:12:50 -05:00
|
|
|
pub enum FieldName {
|
|
|
|
NamedField(ast::ident),
|
|
|
|
PositionalField(uint)
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
|
2013-05-22 05:54:35 -05:00
|
|
|
#[deriving(Eq, IterBytes)]
|
|
|
|
pub enum ElementKind {
|
|
|
|
VecElement,
|
|
|
|
StrElement,
|
|
|
|
OtherElement,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[deriving(Eq, IterBytes)]
|
2013-02-09 00:21:45 -06:00
|
|
|
pub enum MutabilityCategory {
|
|
|
|
McImmutable, // Immutable.
|
|
|
|
McReadOnly, // Read-only (`const`)
|
|
|
|
McDeclared, // Directly declared as mutable.
|
|
|
|
McInherited // Inherited from the fact that owner is mutable.
|
|
|
|
}
|
|
|
|
|
2013-05-03 11:11:15 -05:00
|
|
|
// `cmt`: "Category, Mutability, and Type".
|
|
|
|
//
|
2012-08-08 10:15:32 -05:00
|
|
|
// a complete categorization of a value indicating where it originated
|
|
|
|
// and how it is located, as well as the mutability of the memory in
|
|
|
|
// which the value is stored.
|
2013-01-24 18:24:45 -06:00
|
|
|
//
|
2013-05-03 11:11:15 -05:00
|
|
|
// *WARNING* The field `cmt.type` is NOT necessarily the same as the
|
|
|
|
// result of `node_id_to_type(cmt.id)`. This is because the `id` is
|
|
|
|
// always the `id` of the node producing the type; in an expression
|
|
|
|
// like `*x`, the type of this deref node is the deref'd type (`T`),
|
|
|
|
// but in a pattern like `@x`, the `@x` pattern is again a
|
|
|
|
// dereference, but its type is the type *before* the dereference
|
|
|
|
// (`@T`). So use `cmt.type` to find the type of the value in a consistent
|
|
|
|
// fashion. For more details, see the method `cat_pattern`
|
2013-03-20 10:40:02 -05:00
|
|
|
#[deriving(Eq)]
|
2013-01-30 15:44:24 -06:00
|
|
|
pub struct cmt_ {
|
2013-02-09 00:21:45 -06:00
|
|
|
id: ast::node_id, // id of expr/pat producing this value
|
|
|
|
span: span, // span of same expr/pat
|
|
|
|
cat: categorization, // categorization of expr
|
|
|
|
mutbl: MutabilityCategory, // mutability of expr as lvalue
|
2013-05-03 11:11:15 -05:00
|
|
|
ty: ty::t // type of the expr (*see WARNING above*)
|
2013-01-25 18:57:39 -06:00
|
|
|
}
|
2012-08-08 10:15:32 -05:00
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub type cmt = @cmt_;
|
2012-08-27 18:26:35 -05:00
|
|
|
|
2012-08-08 10:15:32 -05:00
|
|
|
// We pun on *T to mean both actual deref of a ptr as well
|
|
|
|
// as accessing of components:
|
2013-05-17 20:12:50 -05:00
|
|
|
pub enum deref_kind {
|
|
|
|
deref_ptr(ptr_kind),
|
|
|
|
deref_interior(InteriorKind),
|
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
|
|
|
|
// Categorizes a derefable type. Note that we include vectors and strings as
|
|
|
|
// derefable (we model an index as the combination of a deref and then a
|
|
|
|
// pointer adjustment).
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn opt_deref_kind(t: ty::t) -> Option<deref_kind> {
|
2012-09-11 18:20:31 -05:00
|
|
|
match ty::get(t).sty {
|
2013-03-15 14:24:24 -05:00
|
|
|
ty::ty_uniq(mt) => {
|
|
|
|
Some(deref_ptr(uniq_ptr(mt.mutbl)))
|
|
|
|
}
|
|
|
|
|
2013-01-31 19:12:29 -06:00
|
|
|
ty::ty_evec(_, ty::vstore_uniq) |
|
|
|
|
ty::ty_estr(ty::vstore_uniq) |
|
|
|
|
ty::ty_closure(ty::ClosureTy {sigil: ast::OwnedSigil, _}) => {
|
2013-03-15 14:24:24 -05:00
|
|
|
Some(deref_ptr(uniq_ptr(m_imm)))
|
2013-01-31 19:12:29 -06:00
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
|
2013-01-31 19:12:29 -06:00
|
|
|
ty::ty_rptr(r, mt) |
|
|
|
|
ty::ty_evec(mt, ty::vstore_slice(r)) => {
|
|
|
|
Some(deref_ptr(region_ptr(mt.mutbl, r)))
|
|
|
|
}
|
2012-11-04 22:41:00 -06:00
|
|
|
|
2013-01-31 19:12:29 -06:00
|
|
|
ty::ty_estr(ty::vstore_slice(r)) |
|
|
|
|
ty::ty_closure(ty::ClosureTy {sigil: ast::BorrowedSigil,
|
|
|
|
region: r, _}) => {
|
|
|
|
Some(deref_ptr(region_ptr(ast::m_imm, r)))
|
|
|
|
}
|
2013-01-11 23:01:42 -06:00
|
|
|
|
2013-01-31 19:12:29 -06:00
|
|
|
ty::ty_box(mt) |
|
|
|
|
ty::ty_evec(mt, ty::vstore_box) => {
|
|
|
|
Some(deref_ptr(gc_ptr(mt.mutbl)))
|
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
|
2013-01-31 19:12:29 -06:00
|
|
|
ty::ty_estr(ty::vstore_box) |
|
|
|
|
ty::ty_closure(ty::ClosureTy {sigil: ast::ManagedSigil, _}) => {
|
|
|
|
Some(deref_ptr(gc_ptr(ast::m_imm)))
|
|
|
|
}
|
2012-11-04 22:41:00 -06:00
|
|
|
|
2013-01-31 19:12:29 -06:00
|
|
|
ty::ty_ptr(*) => {
|
|
|
|
Some(deref_ptr(unsafe_ptr))
|
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
|
2013-05-17 20:12:50 -05:00
|
|
|
ty::ty_enum(*) |
|
|
|
|
ty::ty_struct(*) => { // newtype
|
|
|
|
Some(deref_interior(InteriorField(PositionalField(0))))
|
2013-01-31 19:12:29 -06:00
|
|
|
}
|
2012-06-04 18:21:14 -05:00
|
|
|
|
2013-05-17 20:12:50 -05:00
|
|
|
ty::ty_evec(_, ty::vstore_fixed(_)) |
|
2013-01-31 19:12:29 -06:00
|
|
|
ty::ty_estr(ty::vstore_fixed(_)) => {
|
2013-05-22 05:54:35 -05:00
|
|
|
Some(deref_interior(InteriorElement(element_kind(t))))
|
2013-01-31 19:12:29 -06:00
|
|
|
}
|
2012-06-04 18:21:14 -05:00
|
|
|
|
2013-01-31 19:12:29 -06:00
|
|
|
_ => None
|
2012-06-01 17:46:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn deref_kind(tcx: ty::ctxt, t: ty::t) -> deref_kind {
|
2012-08-06 14:34:08 -05:00
|
|
|
match opt_deref_kind(t) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(k) => k,
|
|
|
|
None => {
|
2012-06-01 12:46:17 -05:00
|
|
|
tcx.sess.bug(
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("deref_cat() invoked on non-derefable type %s",
|
|
|
|
ty_to_str(tcx, t)));
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn cat_expr(tcx: ty::ctxt,
|
|
|
|
method_map: typeck::method_map,
|
|
|
|
expr: @ast::expr)
|
|
|
|
-> cmt {
|
2012-08-08 10:15:32 -05:00
|
|
|
let mcx = &mem_categorization_ctxt {
|
|
|
|
tcx: tcx, method_map: method_map
|
|
|
|
};
|
|
|
|
return mcx.cat_expr(expr);
|
|
|
|
}
|
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn cat_expr_unadjusted(tcx: ty::ctxt,
|
|
|
|
method_map: typeck::method_map,
|
|
|
|
expr: @ast::expr)
|
|
|
|
-> cmt {
|
2012-11-27 16:12:33 -06:00
|
|
|
let mcx = &mem_categorization_ctxt {
|
|
|
|
tcx: tcx, method_map: method_map
|
|
|
|
};
|
|
|
|
return mcx.cat_expr_unadjusted(expr);
|
|
|
|
}
|
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn cat_expr_autoderefd(
|
2012-11-27 16:12:33 -06:00
|
|
|
tcx: ty::ctxt,
|
|
|
|
method_map: typeck::method_map,
|
|
|
|
expr: @ast::expr,
|
2013-02-27 18:28:37 -06:00
|
|
|
autoderefs: uint) -> cmt
|
|
|
|
{
|
2012-11-27 16:12:33 -06:00
|
|
|
let mcx = &mem_categorization_ctxt {
|
|
|
|
tcx: tcx, method_map: method_map
|
|
|
|
};
|
2013-02-27 18:28:37 -06:00
|
|
|
return mcx.cat_expr_autoderefd(expr, autoderefs);
|
2012-11-27 16:12:33 -06:00
|
|
|
}
|
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn cat_def(
|
2012-08-08 10:15:32 -05:00
|
|
|
tcx: ty::ctxt,
|
|
|
|
method_map: typeck::method_map,
|
|
|
|
expr_id: ast::node_id,
|
|
|
|
expr_span: span,
|
|
|
|
expr_ty: ty::t,
|
|
|
|
def: ast::def) -> cmt {
|
|
|
|
|
|
|
|
let mcx = &mem_categorization_ctxt {
|
|
|
|
tcx: tcx, method_map: method_map
|
|
|
|
};
|
|
|
|
return mcx.cat_def(expr_id, expr_span, expr_ty, def);
|
|
|
|
}
|
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub trait ast_node {
|
2013-02-22 00:41:37 -06:00
|
|
|
fn id(&self) -> ast::node_id;
|
|
|
|
fn span(&self) -> span;
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-26 19:12:00 -06:00
|
|
|
impl ast_node for @ast::expr {
|
2013-02-22 00:41:37 -06:00
|
|
|
fn id(&self) -> ast::node_id { self.id }
|
|
|
|
fn span(&self) -> span { self.span }
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-26 19:12:00 -06:00
|
|
|
impl ast_node for @ast::pat {
|
2013-02-22 00:41:37 -06:00
|
|
|
fn id(&self) -> ast::node_id { self.id }
|
|
|
|
fn span(&self) -> span { self.span }
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
|
2013-01-30 15:44:24 -06:00
|
|
|
pub struct mem_categorization_ctxt {
|
2012-09-07 16:50:47 -05:00
|
|
|
tcx: ty::ctxt,
|
|
|
|
method_map: typeck::method_map,
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-09 00:21:45 -06:00
|
|
|
impl ToStr for MutabilityCategory {
|
2013-03-22 13:09:13 -05:00
|
|
|
fn to_str(&self) -> ~str {
|
2013-02-09 00:21:45 -06:00
|
|
|
fmt!("%?", *self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl MutabilityCategory {
|
|
|
|
pub fn from_mutbl(m: ast::mutability) -> MutabilityCategory {
|
2013-02-09 00:21:45 -06:00
|
|
|
match m {
|
|
|
|
m_imm => McImmutable,
|
|
|
|
m_const => McReadOnly,
|
|
|
|
m_mutbl => McDeclared
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn inherit(&self) -> MutabilityCategory {
|
2013-02-09 00:21:45 -06:00
|
|
|
match *self {
|
|
|
|
McImmutable => McImmutable,
|
|
|
|
McReadOnly => McReadOnly,
|
|
|
|
McDeclared => McInherited,
|
|
|
|
McInherited => McInherited
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn is_mutable(&self) -> bool {
|
2013-02-09 00:21:45 -06:00
|
|
|
match *self {
|
|
|
|
McImmutable | McReadOnly => false,
|
|
|
|
McDeclared | McInherited => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn is_immutable(&self) -> bool {
|
2013-02-09 00:21:45 -06:00
|
|
|
match *self {
|
|
|
|
McImmutable => true,
|
|
|
|
McReadOnly | McDeclared | McInherited => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn to_user_str(&self) -> &'static str {
|
2013-02-09 00:21:45 -06:00
|
|
|
match *self {
|
2013-03-15 14:24:24 -05:00
|
|
|
McDeclared | McInherited => "mutable",
|
|
|
|
McImmutable => "immutable",
|
|
|
|
McReadOnly => "const"
|
2013-04-12 00:09:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl mem_categorization_ctxt {
|
|
|
|
pub fn expr_ty(&self, expr: @ast::expr) -> ty::t {
|
2013-05-03 11:11:15 -05:00
|
|
|
ty::expr_ty(self.tcx, expr)
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn pat_ty(&self, pat: @ast::pat) -> ty::t {
|
2013-05-03 11:11:15 -05:00
|
|
|
ty::node_id_to_type(self.tcx, pat.id)
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn cat_expr(&self, expr: @ast::expr) -> cmt {
|
2013-02-05 21:41:45 -06:00
|
|
|
match self.tcx.adjustments.find(&expr.id) {
|
2012-09-11 23:25:01 -05:00
|
|
|
None => {
|
|
|
|
// No adjustments.
|
|
|
|
self.cat_expr_unadjusted(expr)
|
|
|
|
}
|
|
|
|
|
2013-03-22 21:26:41 -05:00
|
|
|
Some(&@ty::AutoAddEnv(*)) => {
|
2013-02-27 18:28:37 -06:00
|
|
|
// Convert a bare fn to a closure by adding NULL env.
|
|
|
|
// Result is an rvalue.
|
|
|
|
let expr_ty = ty::expr_ty_adjusted(self.tcx, expr);
|
2013-06-20 14:25:52 -05:00
|
|
|
self.cat_rvalue_node(expr, expr_ty)
|
2013-02-27 18:28:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(
|
2013-03-22 21:26:41 -05:00
|
|
|
&@ty::AutoDerefRef(
|
2013-02-27 18:28:37 -06:00
|
|
|
ty::AutoDerefRef {
|
|
|
|
autoref: Some(_), _})) => {
|
|
|
|
// Equivalent to &*expr or something similar.
|
|
|
|
// Result is an rvalue.
|
|
|
|
let expr_ty = ty::expr_ty_adjusted(self.tcx, expr);
|
2013-06-20 14:25:52 -05:00
|
|
|
self.cat_rvalue_node(expr, expr_ty)
|
2013-02-27 18:28:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(
|
2013-03-22 21:26:41 -05:00
|
|
|
&@ty::AutoDerefRef(
|
2013-02-27 18:28:37 -06:00
|
|
|
ty::AutoDerefRef {
|
|
|
|
autoref: None, autoderefs: autoderefs})) => {
|
|
|
|
// Equivalent to *expr or something similar.
|
|
|
|
self.cat_expr_autoderefd(expr, autoderefs)
|
2012-09-11 23:25:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-20 18:37:45 -05:00
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn cat_expr_autoderefd(&self, expr: @ast::expr, autoderefs: uint)
|
|
|
|
-> cmt {
|
2012-09-11 23:25:01 -05:00
|
|
|
let mut cmt = self.cat_expr_unadjusted(expr);
|
2013-02-27 18:28:37 -06:00
|
|
|
for uint::range(1, autoderefs+1) |deref| {
|
2012-09-11 23:25:01 -05:00
|
|
|
cmt = self.cat_deref(expr, cmt, deref);
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
2012-09-11 23:25:01 -05:00
|
|
|
return cmt;
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn cat_expr_unadjusted(&self, expr: @ast::expr) -> cmt {
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("cat_expr: id=%d expr=%s",
|
|
|
|
expr.id, pprust::expr_to_str(expr, self.tcx.sess.intr()));
|
2012-06-01 12:46:17 -05:00
|
|
|
|
2013-05-03 11:11:15 -05:00
|
|
|
let expr_ty = self.expr_ty(expr);
|
2012-08-06 14:34:08 -05:00
|
|
|
match expr.node {
|
2013-06-01 17:31:56 -05:00
|
|
|
ast::expr_unary(_, ast::deref, e_base) => {
|
2013-02-08 16:08:02 -06:00
|
|
|
if self.method_map.contains_key(&expr.id) {
|
2013-06-20 14:25:52 -05:00
|
|
|
return self.cat_rvalue_node(expr, expr_ty);
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let base_cmt = self.cat_expr(e_base);
|
2012-09-11 23:25:01 -05:00
|
|
|
self.cat_deref(expr, base_cmt, 0)
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::expr_field(base, f_name, _) => {
|
2013-03-15 14:24:24 -05:00
|
|
|
// Method calls are now a special syntactic form,
|
|
|
|
// so `a.b` should always be a field.
|
|
|
|
assert!(!self.method_map.contains_key(&expr.id));
|
2012-06-01 12:46:17 -05:00
|
|
|
|
2012-09-11 23:25:01 -05:00
|
|
|
let base_cmt = self.cat_expr(base);
|
2013-05-09 14:58:41 -05:00
|
|
|
self.cat_field(expr, base_cmt, f_name, self.expr_ty(expr))
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2013-06-01 17:31:56 -05:00
|
|
|
ast::expr_index(_, base, _) => {
|
2013-02-08 16:08:02 -06:00
|
|
|
if self.method_map.contains_key(&expr.id) {
|
2013-06-20 14:25:52 -05:00
|
|
|
return self.cat_rvalue_node(expr, expr_ty);
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2012-09-11 23:25:01 -05:00
|
|
|
let base_cmt = self.cat_expr(base);
|
2013-05-09 14:58:02 -05:00
|
|
|
self.cat_index(expr, base_cmt, 0)
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2013-05-10 17:15:06 -05:00
|
|
|
ast::expr_path(_) | ast::expr_self => {
|
2013-05-05 11:17:59 -05:00
|
|
|
let def = self.tcx.def_map.get_copy(&expr.id);
|
2012-06-01 12:46:17 -05:00
|
|
|
self.cat_def(expr.id, expr.span, expr_ty, def)
|
|
|
|
}
|
|
|
|
|
2012-10-27 19:14:09 -05:00
|
|
|
ast::expr_paren(e) => self.cat_expr_unadjusted(e),
|
|
|
|
|
2013-05-05 23:43:19 -05:00
|
|
|
ast::expr_addr_of(*) | ast::expr_call(*) |
|
2013-03-06 16:14:06 -06:00
|
|
|
ast::expr_assign(*) | ast::expr_assign_op(*) |
|
|
|
|
ast::expr_fn_block(*) | ast::expr_ret(*) | ast::expr_loop_body(*) |
|
|
|
|
ast::expr_do_body(*) | ast::expr_unary(*) |
|
2013-07-09 21:32:09 -05:00
|
|
|
ast::expr_method_call(*) | ast::expr_cast(*) | ast::expr_vstore(*) |
|
|
|
|
ast::expr_vec(*) | ast::expr_tup(*) | ast::expr_if(*) |
|
|
|
|
ast::expr_log(*) | ast::expr_binary(*) | ast::expr_while(*) |
|
|
|
|
ast::expr_block(*) | ast::expr_loop(*) | ast::expr_match(*) |
|
|
|
|
ast::expr_lit(*) | ast::expr_break(*) | ast::expr_mac(*) |
|
|
|
|
ast::expr_again(*) | ast::expr_struct(*) | ast::expr_repeat(*) |
|
|
|
|
ast::expr_inline_asm(*) => {
|
2013-06-20 14:25:52 -05:00
|
|
|
return self.cat_rvalue_node(expr, expr_ty);
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn cat_def(&self,
|
|
|
|
id: ast::node_id,
|
|
|
|
span: span,
|
|
|
|
expr_ty: ty::t,
|
|
|
|
def: ast::def)
|
|
|
|
-> cmt {
|
2012-08-06 14:34:08 -05:00
|
|
|
match def {
|
2012-08-02 18:01:38 -05:00
|
|
|
ast::def_fn(*) | ast::def_static_method(*) | ast::def_mod(_) |
|
2013-06-21 20:46:34 -05:00
|
|
|
ast::def_foreign_mod(_) | ast::def_static(_, false) |
|
2012-07-26 16:04:03 -05:00
|
|
|
ast::def_use(_) | ast::def_variant(*) |
|
2013-03-27 09:26:57 -05:00
|
|
|
ast::def_trait(_) | ast::def_ty(_) | ast::def_prim_ty(_) |
|
2012-12-10 15:47:54 -06:00
|
|
|
ast::def_ty_param(*) | ast::def_struct(*) |
|
2012-08-14 21:20:56 -05:00
|
|
|
ast::def_typaram_binder(*) | ast::def_region(_) |
|
2013-06-25 21:19:38 -05:00
|
|
|
ast::def_label(_) | ast::def_self_ty(*) | ast::def_method(*) => {
|
2013-06-21 20:46:34 -05:00
|
|
|
@cmt_ {
|
|
|
|
id:id,
|
|
|
|
span:span,
|
|
|
|
cat:cat_static_item,
|
|
|
|
mutbl: McImmutable,
|
|
|
|
ty:expr_ty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ast::def_static(_, true) => {
|
|
|
|
@cmt_ {
|
|
|
|
id:id,
|
|
|
|
span:span,
|
|
|
|
cat:cat_static_item,
|
|
|
|
mutbl: McDeclared,
|
|
|
|
ty:expr_ty
|
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2013-04-24 03:29:46 -05:00
|
|
|
ast::def_arg(vid, mutbl) => {
|
2012-06-01 12:46:17 -05:00
|
|
|
// Idea: make this could be rewritten to model by-ref
|
|
|
|
// stuff as `&const` and `&mut`?
|
|
|
|
|
|
|
|
// m: mutability of the argument
|
2013-02-09 00:21:45 -06:00
|
|
|
let m = if mutbl {McDeclared} else {McImmutable};
|
2013-01-25 18:57:39 -06:00
|
|
|
@cmt_ {
|
2013-03-15 14:24:24 -05:00
|
|
|
id: id,
|
|
|
|
span: span,
|
2013-04-30 13:08:18 -05:00
|
|
|
cat: cat_arg(vid),
|
2013-02-09 00:21:45 -06:00
|
|
|
mutbl: m,
|
2013-01-25 18:57:39 -06:00
|
|
|
ty:expr_ty
|
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2013-01-23 20:15:06 -06:00
|
|
|
ast::def_self(self_id, is_implicit) => {
|
2013-03-15 14:24:24 -05:00
|
|
|
let cat = if is_implicit {
|
|
|
|
cat_implicit_self
|
2012-12-10 13:58:37 -06:00
|
|
|
} else {
|
2013-03-15 14:24:24 -05:00
|
|
|
cat_self(self_id)
|
2012-12-10 13:58:37 -06:00
|
|
|
};
|
|
|
|
|
2013-01-25 18:57:39 -06:00
|
|
|
@cmt_ {
|
|
|
|
id:id,
|
|
|
|
span:span,
|
|
|
|
cat:cat,
|
2013-02-09 00:21:45 -06:00
|
|
|
mutbl: McImmutable,
|
2013-01-25 18:57:39 -06:00
|
|
|
ty:expr_ty
|
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2013-03-15 14:24:24 -05:00
|
|
|
ast::def_upvar(upvar_id, inner, fn_node_id, _) => {
|
|
|
|
let ty = ty::node_id_to_type(self.tcx, fn_node_id);
|
|
|
|
match ty::get(ty).sty {
|
|
|
|
ty::ty_closure(ref closure_ty) => {
|
2013-06-19 14:53:05 -05:00
|
|
|
// Decide whether to use implicit reference or by copy/move
|
|
|
|
// capture for the upvar. This, combined with the onceness,
|
|
|
|
// determines whether the closure can move out of it.
|
|
|
|
let var_is_refd = match (closure_ty.sigil, closure_ty.onceness) {
|
|
|
|
// Many-shot stack closures can never move out.
|
|
|
|
(ast::BorrowedSigil, ast::Many) => true,
|
|
|
|
// 1-shot stack closures can move out with "-Z once-fns".
|
|
|
|
(ast::BorrowedSigil, ast::Once)
|
|
|
|
if self.tcx.sess.once_fns() => false,
|
|
|
|
(ast::BorrowedSigil, ast::Once) => true,
|
|
|
|
// Heap closures always capture by copy/move, and can
|
|
|
|
// move out iff they are once.
|
|
|
|
(ast::OwnedSigil, _) | (ast::ManagedSigil, _) => false,
|
|
|
|
|
|
|
|
};
|
|
|
|
if var_is_refd {
|
|
|
|
let upvar_cmt =
|
|
|
|
self.cat_def(id, span, expr_ty, *inner);
|
|
|
|
@cmt_ {
|
|
|
|
id:id,
|
|
|
|
span:span,
|
|
|
|
cat:cat_stack_upvar(upvar_cmt),
|
|
|
|
mutbl:upvar_cmt.mutbl.inherit(),
|
|
|
|
ty:upvar_cmt.ty
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
2013-06-19 14:53:05 -05:00
|
|
|
} else {
|
|
|
|
// FIXME #2152 allow mutation of moved upvars
|
|
|
|
@cmt_ {
|
|
|
|
id:id,
|
|
|
|
span:span,
|
|
|
|
cat:cat_copied_upvar(CopiedUpvar {
|
|
|
|
upvar_id: upvar_id,
|
|
|
|
onceness: closure_ty.onceness}),
|
|
|
|
mutbl:McImmutable,
|
|
|
|
ty:expr_ty
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
self.tcx.sess.span_bug(
|
|
|
|
span,
|
|
|
|
fmt!("Upvar of non-closure %? - %s",
|
|
|
|
fn_node_id, ty.repr(self.tcx)));
|
|
|
|
}
|
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::def_local(vid, mutbl) => {
|
2013-02-09 00:21:45 -06:00
|
|
|
let m = if mutbl {McDeclared} else {McImmutable};
|
2013-01-25 18:57:39 -06:00
|
|
|
@cmt_ {
|
|
|
|
id:id,
|
|
|
|
span:span,
|
|
|
|
cat:cat_local(vid),
|
|
|
|
mutbl:m,
|
|
|
|
ty:expr_ty
|
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2012-12-07 21:34:57 -06:00
|
|
|
ast::def_binding(vid, _) => {
|
2012-08-06 09:20:23 -05:00
|
|
|
// by-value/by-ref bindings are local variables
|
2013-01-25 18:57:39 -06:00
|
|
|
@cmt_ {
|
|
|
|
id:id,
|
|
|
|
span:span,
|
|
|
|
cat:cat_local(vid),
|
2013-02-09 00:21:45 -06:00
|
|
|
mutbl:McImmutable,
|
2013-01-25 18:57:39 -06:00
|
|
|
ty:expr_ty
|
|
|
|
}
|
2012-08-02 18:00:45 -05:00
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:25:52 -05:00
|
|
|
pub fn cat_rvalue_node<N:ast_node>(&self,
|
|
|
|
node: N,
|
|
|
|
expr_ty: ty::t) -> cmt {
|
|
|
|
self.cat_rvalue(node.id(),
|
|
|
|
node.span(),
|
|
|
|
self.tcx.region_maps.cleanup_scope(node.id()),
|
|
|
|
expr_ty)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn cat_rvalue(&self,
|
|
|
|
cmt_id: ast::node_id,
|
|
|
|
span: span,
|
|
|
|
cleanup_scope_id: ast::node_id,
|
|
|
|
expr_ty: ty::t) -> cmt {
|
2013-01-25 18:57:39 -06:00
|
|
|
@cmt_ {
|
2013-06-20 14:25:52 -05:00
|
|
|
id:cmt_id,
|
|
|
|
span:span,
|
|
|
|
cat:cat_rvalue(cleanup_scope_id),
|
2013-05-14 08:26:21 -05:00
|
|
|
mutbl:McDeclared,
|
2013-01-25 18:57:39 -06:00
|
|
|
ty:expr_ty
|
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2012-07-19 22:13:00 -05:00
|
|
|
/// inherited mutability: used in cases where the mutability of a
|
|
|
|
/// component is inherited from the base it is a part of. For
|
|
|
|
/// example, a record field is mutable if it is declared mutable
|
|
|
|
/// or if the container is mutable.
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn inherited_mutability(&self,
|
|
|
|
base_m: MutabilityCategory,
|
|
|
|
interior_m: ast::mutability)
|
|
|
|
-> MutabilityCategory {
|
2013-03-15 14:24:24 -05:00
|
|
|
match interior_m {
|
2013-02-09 00:21:45 -06:00
|
|
|
m_imm => base_m.inherit(),
|
|
|
|
m_const => McReadOnly,
|
|
|
|
m_mutbl => McDeclared
|
2012-07-19 22:13:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn cat_field<N:ast_node>(&self,
|
|
|
|
node: N,
|
|
|
|
base_cmt: cmt,
|
|
|
|
f_name: ast::ident,
|
|
|
|
f_ty: ty::t)
|
|
|
|
-> cmt {
|
2013-01-25 18:57:39 -06:00
|
|
|
@cmt_ {
|
|
|
|
id: node.id(),
|
|
|
|
span: node.span(),
|
2013-05-17 20:12:50 -05:00
|
|
|
cat: cat_interior(base_cmt, InteriorField(NamedField(f_name))),
|
2013-05-09 14:58:41 -05:00
|
|
|
mutbl: base_cmt.mutbl.inherit(),
|
2013-05-03 11:11:15 -05:00
|
|
|
ty: f_ty
|
2013-01-25 18:57:39 -06:00
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn cat_deref_fn<N:ast_node>(&self,
|
|
|
|
node: N,
|
|
|
|
base_cmt: cmt,
|
|
|
|
deref_cnt: uint)
|
|
|
|
-> cmt {
|
2012-11-04 22:41:00 -06:00
|
|
|
// Bit of a hack: the "dereference" of a function pointer like
|
|
|
|
// `@fn()` is a mere logical concept. We interpret it as
|
|
|
|
// dereferencing the environment pointer; of course, we don't
|
|
|
|
// know what type lies at the other end, so we just call it
|
|
|
|
// `()` (the empty tuple).
|
|
|
|
|
2013-02-09 00:21:45 -06:00
|
|
|
let mt = ty::mt {ty: ty::mk_tup(self.tcx, ~[]),
|
|
|
|
mutbl: m_imm};
|
2012-11-04 22:41:00 -06:00
|
|
|
return self.cat_deref_common(node, base_cmt, deref_cnt, mt);
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn cat_deref<N:ast_node>(&self,
|
|
|
|
node: N,
|
|
|
|
base_cmt: cmt,
|
|
|
|
deref_cnt: uint)
|
|
|
|
-> cmt {
|
2012-09-11 23:25:01 -05:00
|
|
|
let mt = match ty::deref(self.tcx, base_cmt.ty, true) {
|
|
|
|
Some(mt) => mt,
|
|
|
|
None => {
|
|
|
|
self.tcx.sess.span_bug(
|
|
|
|
node.span(),
|
|
|
|
fmt!("Explicit deref of non-derefable type: %s",
|
|
|
|
ty_to_str(self.tcx, base_cmt.ty)));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-04 22:41:00 -06:00
|
|
|
return self.cat_deref_common(node, base_cmt, deref_cnt, mt);
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn cat_deref_common<N:ast_node>(&self,
|
|
|
|
node: N,
|
|
|
|
base_cmt: cmt,
|
|
|
|
deref_cnt: uint,
|
|
|
|
mt: ty::mt)
|
|
|
|
-> cmt {
|
2012-09-11 23:25:01 -05:00
|
|
|
match deref_kind(self.tcx, base_cmt.ty) {
|
|
|
|
deref_ptr(ptr) => {
|
2012-07-20 18:37:45 -05:00
|
|
|
// for unique ptrs, we inherit mutability from the
|
|
|
|
// owning reference.
|
2012-08-06 14:34:08 -05:00
|
|
|
let m = match ptr {
|
2013-03-15 14:24:24 -05:00
|
|
|
uniq_ptr(*) => {
|
2012-09-11 23:25:01 -05:00
|
|
|
self.inherited_mutability(base_cmt.mutbl, mt.mutbl)
|
|
|
|
}
|
2013-01-23 20:15:06 -06:00
|
|
|
gc_ptr(*) | region_ptr(_, _) | unsafe_ptr => {
|
2013-02-09 00:21:45 -06:00
|
|
|
MutabilityCategory::from_mutbl(mt.mutbl)
|
2012-09-11 23:25:01 -05:00
|
|
|
}
|
2012-07-20 18:37:45 -05:00
|
|
|
};
|
|
|
|
|
2013-01-25 18:57:39 -06:00
|
|
|
@cmt_ {
|
|
|
|
id:node.id(),
|
|
|
|
span:node.span(),
|
|
|
|
cat:cat_deref(base_cmt, deref_cnt, ptr),
|
|
|
|
mutbl:m,
|
|
|
|
ty:mt.ty
|
|
|
|
}
|
2012-09-11 23:25:01 -05:00
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
|
2013-03-15 14:24:24 -05:00
|
|
|
deref_interior(interior) => {
|
2012-07-19 22:13:00 -05:00
|
|
|
let m = self.inherited_mutability(base_cmt.mutbl, mt.mutbl);
|
2013-01-25 18:57:39 -06:00
|
|
|
@cmt_ {
|
|
|
|
id:node.id(),
|
|
|
|
span:node.span(),
|
2013-03-15 14:24:24 -05:00
|
|
|
cat:cat_interior(base_cmt, interior),
|
2013-01-25 18:57:39 -06:00
|
|
|
mutbl:m,
|
|
|
|
ty:mt.ty
|
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn cat_index<N:ast_node>(&self,
|
|
|
|
elt: N,
|
|
|
|
base_cmt: cmt,
|
|
|
|
derefs: uint)
|
|
|
|
-> cmt {
|
2013-05-09 14:58:02 -05:00
|
|
|
//! Creates a cmt for an indexing operation (`[]`); this
|
|
|
|
//! indexing operation may occurs as part of an
|
|
|
|
//! AutoBorrowVec, which when converting a `~[]` to an `&[]`
|
|
|
|
//! effectively takes the address of the 0th element.
|
|
|
|
//!
|
|
|
|
//! One subtle aspect of indexing that may not be
|
|
|
|
//! immediately obvious: for anything other than a fixed-length
|
|
|
|
//! vector, an operation like `x[y]` actually consists of two
|
|
|
|
//! disjoint (from the point of view of borrowck) operations.
|
|
|
|
//! The first is a deref of `x` to create a pointer `p` that points
|
|
|
|
//! at the first element in the array. The second operation is
|
|
|
|
//! an index which adds `y*sizeof(T)` to `p` to obtain the
|
|
|
|
//! pointer to `x[y]`. `cat_index` will produce a resulting
|
|
|
|
//! cmt containing both this deref and the indexing,
|
|
|
|
//! presuming that `base_cmt` is not of fixed-length type.
|
|
|
|
//!
|
|
|
|
//! In the event that a deref is needed, the "deref count"
|
|
|
|
//! is taken from the parameter `derefs`. See the comment
|
|
|
|
//! on the def'n of `root_map_key` in borrowck/mod.rs
|
|
|
|
//! for more details about deref counts; the summary is
|
|
|
|
//! that `derefs` should be 0 for an explicit indexing
|
|
|
|
//! operation and N+1 for an indexing that is part of
|
|
|
|
//! an auto-adjustment, where N is the number of autoderefs
|
|
|
|
//! in that adjustment.
|
|
|
|
//!
|
|
|
|
//! # Parameters
|
|
|
|
//! - `elt`: the AST node being indexed
|
|
|
|
//! - `base_cmt`: the cmt of `elt`
|
|
|
|
//! - `derefs`: the deref number to be used for
|
|
|
|
//! the implicit index deref, if any (see above)
|
|
|
|
|
2013-04-22 22:19:05 -05:00
|
|
|
let mt = match ty::index(base_cmt.ty) {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(mt) => mt,
|
|
|
|
None => {
|
2012-06-01 12:46:17 -05:00
|
|
|
self.tcx.sess.span_bug(
|
2013-01-24 21:33:48 -06:00
|
|
|
elt.span(),
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("Explicit index of non-index type `%s`",
|
|
|
|
ty_to_str(self.tcx, base_cmt.ty)));
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
return match deref_kind(self.tcx, base_cmt.ty) {
|
2012-08-03 21:59:04 -05:00
|
|
|
deref_ptr(ptr) => {
|
2013-03-15 14:24:24 -05:00
|
|
|
// for unique ptrs, we inherit mutability from the
|
2012-07-20 18:37:45 -05:00
|
|
|
// owning reference.
|
2012-08-06 14:34:08 -05:00
|
|
|
let m = match ptr {
|
2013-03-15 14:24:24 -05:00
|
|
|
uniq_ptr(*) => {
|
2012-07-20 18:37:45 -05:00
|
|
|
self.inherited_mutability(base_cmt.mutbl, mt.mutbl)
|
|
|
|
}
|
2013-01-23 20:15:06 -06:00
|
|
|
gc_ptr(_) | region_ptr(_, _) | unsafe_ptr => {
|
2013-02-09 00:21:45 -06:00
|
|
|
MutabilityCategory::from_mutbl(mt.mutbl)
|
2012-07-20 18:37:45 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-03-15 14:24:24 -05:00
|
|
|
// the deref is explicit in the resulting cmt
|
2013-01-25 18:57:39 -06:00
|
|
|
let deref_cmt = @cmt_ {
|
|
|
|
id:elt.id(),
|
|
|
|
span:elt.span(),
|
2013-05-09 14:58:02 -05:00
|
|
|
cat:cat_deref(base_cmt, derefs, ptr),
|
2013-01-25 18:57:39 -06:00
|
|
|
mutbl:m,
|
|
|
|
ty:mt.ty
|
|
|
|
};
|
2012-07-17 20:44:00 -05:00
|
|
|
|
2013-03-15 14:24:24 -05:00
|
|
|
interior(elt, deref_cmt, base_cmt.ty, m, mt)
|
2012-06-04 18:21:14 -05:00
|
|
|
}
|
|
|
|
|
2013-03-15 14:24:24 -05:00
|
|
|
deref_interior(_) => {
|
2012-06-04 18:21:14 -05:00
|
|
|
// fixed-length vectors have no deref
|
2012-11-14 21:28:43 -06:00
|
|
|
let m = self.inherited_mutability(base_cmt.mutbl, mt.mutbl);
|
2013-03-15 14:24:24 -05:00
|
|
|
interior(elt, base_cmt, base_cmt.ty, m, mt)
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-17 20:12:50 -05:00
|
|
|
fn interior<N: ast_node>(elt: N,
|
|
|
|
of_cmt: cmt,
|
|
|
|
vec_ty: ty::t,
|
|
|
|
mutbl: MutabilityCategory,
|
2013-03-15 14:24:24 -05:00
|
|
|
mt: ty::mt) -> cmt
|
2013-01-24 21:33:48 -06:00
|
|
|
{
|
2013-01-25 18:57:39 -06:00
|
|
|
@cmt_ {
|
|
|
|
id:elt.id(),
|
|
|
|
span:elt.span(),
|
2013-05-22 05:54:35 -05:00
|
|
|
cat:cat_interior(of_cmt, InteriorElement(element_kind(vec_ty))),
|
2013-01-25 18:57:39 -06:00
|
|
|
mutbl:mutbl,
|
2013-02-09 00:21:45 -06:00
|
|
|
ty:mt.ty
|
2013-01-25 18:57:39 -06:00
|
|
|
}
|
2012-06-04 18:21:14 -05:00
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn cat_imm_interior<N:ast_node>(&self,
|
|
|
|
node: N,
|
|
|
|
base_cmt: cmt,
|
|
|
|
interior_ty: ty::t,
|
|
|
|
interior: InteriorKind)
|
|
|
|
-> cmt {
|
2013-01-25 18:57:39 -06:00
|
|
|
@cmt_ {
|
2013-05-03 11:11:15 -05:00
|
|
|
id: node.id(),
|
|
|
|
span: node.span(),
|
|
|
|
cat: cat_interior(base_cmt, interior),
|
|
|
|
mutbl: base_cmt.mutbl.inherit(),
|
|
|
|
ty: interior_ty
|
2013-01-25 18:57:39 -06:00
|
|
|
}
|
2012-11-02 11:56:09 -05:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn cat_downcast<N:ast_node>(&self,
|
|
|
|
node: N,
|
|
|
|
base_cmt: cmt,
|
|
|
|
downcast_ty: ty::t)
|
|
|
|
-> cmt {
|
2013-05-17 20:12:50 -05:00
|
|
|
@cmt_ {
|
|
|
|
id: node.id(),
|
|
|
|
span: node.span(),
|
|
|
|
cat: cat_downcast(base_cmt),
|
|
|
|
mutbl: base_cmt.mutbl.inherit(),
|
|
|
|
ty: downcast_ty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn cat_pattern(&self,
|
|
|
|
cmt: cmt,
|
|
|
|
pat: @ast::pat,
|
|
|
|
op: &fn(cmt, @ast::pat)) {
|
2012-08-08 10:15:32 -05:00
|
|
|
// Here, `cmt` is the categorization for the value being
|
|
|
|
// matched and pat is the pattern it is being matched against.
|
|
|
|
//
|
|
|
|
// In general, the way that this works is that we walk down
|
|
|
|
// the pattern, constructing a cmt that represents the path
|
|
|
|
// that will be taken to reach the value being matched.
|
|
|
|
//
|
|
|
|
// When we encounter named bindings, we take the cmt that has
|
|
|
|
// been built up and pass it off to guarantee_valid() so that
|
|
|
|
// we can be sure that the binding will remain valid for the
|
|
|
|
// duration of the arm.
|
|
|
|
//
|
2013-05-03 11:11:15 -05:00
|
|
|
// (*) There is subtlety concerning the correspondence between
|
|
|
|
// pattern ids and types as compared to *expression* ids and
|
|
|
|
// types. This is explained briefly. on the definition of the
|
|
|
|
// type `cmt`, so go off and read what it says there, then
|
|
|
|
// come back and I'll dive into a bit more detail here. :) OK,
|
|
|
|
// back?
|
2012-08-08 10:15:32 -05:00
|
|
|
//
|
2013-05-03 11:11:15 -05:00
|
|
|
// In general, the id of the cmt should be the node that
|
|
|
|
// "produces" the value---patterns aren't executable code
|
|
|
|
// exactly, but I consider them to "execute" when they match a
|
|
|
|
// value. So if you have something like:
|
2012-08-08 10:15:32 -05:00
|
|
|
//
|
|
|
|
// let x = @@3;
|
|
|
|
// match x {
|
|
|
|
// @@y { ... }
|
|
|
|
// }
|
|
|
|
//
|
2013-05-03 11:11:15 -05:00
|
|
|
// In this case, the cmt and the relevant ids would be:
|
|
|
|
//
|
|
|
|
// CMT Id Type of Id Type of cmt
|
2012-08-08 10:15:32 -05:00
|
|
|
//
|
|
|
|
// local(x)->@->@
|
2013-05-03 11:11:15 -05:00
|
|
|
// ^~~~~~~^ `x` from discr @@int @@int
|
|
|
|
// ^~~~~~~~~~^ `@@y` pattern node @@int @int
|
|
|
|
// ^~~~~~~~~~~~~^ `@y` pattern node @int int
|
2012-08-08 10:15:32 -05:00
|
|
|
//
|
2013-05-03 11:11:15 -05:00
|
|
|
// You can see that the types of the id and the cmt are in
|
|
|
|
// sync in the first line, because that id is actually the id
|
|
|
|
// of an expression. But once we get to pattern ids, the types
|
|
|
|
// step out of sync again. So you'll see below that we always
|
|
|
|
// get the type of the *subpattern* and use that.
|
2012-07-18 18:18:02 -05:00
|
|
|
|
|
|
|
let tcx = self.tcx;
|
2012-08-22 19:24:52 -05:00
|
|
|
debug!("cat_pattern: id=%d pat=%s cmt=%s",
|
2012-07-18 18:18:02 -05:00
|
|
|
pat.id, pprust::pat_to_str(pat, tcx.sess.intr()),
|
2013-03-15 14:24:24 -05:00
|
|
|
cmt.repr(tcx));
|
2013-01-24 21:33:48 -06:00
|
|
|
let _i = indenter();
|
2012-08-08 10:15:32 -05:00
|
|
|
|
2013-01-24 21:33:48 -06:00
|
|
|
op(cmt, pat);
|
|
|
|
|
|
|
|
match pat.node {
|
2012-08-08 10:15:32 -05:00
|
|
|
ast::pat_wild => {
|
|
|
|
// _
|
|
|
|
}
|
|
|
|
|
2012-08-20 14:23:37 -05:00
|
|
|
ast::pat_enum(_, None) => {
|
2012-08-08 10:15:32 -05:00
|
|
|
// variant(*)
|
|
|
|
}
|
2013-01-24 21:33:48 -06:00
|
|
|
ast::pat_enum(_, Some(ref subpats)) => {
|
2013-02-05 21:41:45 -06:00
|
|
|
match self.tcx.def_map.find(&pat.id) {
|
2013-03-22 21:26:41 -05:00
|
|
|
Some(&ast::def_variant(enum_did, _)) => {
|
2012-11-02 11:56:09 -05:00
|
|
|
// variant(x, y, z)
|
2013-05-17 20:12:50 -05:00
|
|
|
|
|
|
|
let downcast_cmt = {
|
|
|
|
if ty::enum_is_univariant(tcx, enum_did) {
|
|
|
|
cmt // univariant, no downcast needed
|
|
|
|
} else {
|
|
|
|
self.cat_downcast(pat, cmt, cmt.ty)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-17 18:43:22 -05:00
|
|
|
for subpats.iter().enumerate().advance |(i, &subpat)| {
|
2013-05-03 11:11:15 -05:00
|
|
|
let subpat_ty = self.pat_ty(subpat); // see (*)
|
2013-05-17 20:12:50 -05:00
|
|
|
|
2013-05-03 11:11:15 -05:00
|
|
|
let subcmt =
|
2013-05-17 20:12:50 -05:00
|
|
|
self.cat_imm_interior(
|
|
|
|
pat, downcast_cmt, subpat_ty,
|
|
|
|
InteriorField(PositionalField(i)));
|
|
|
|
|
2013-06-21 19:08:35 -05:00
|
|
|
self.cat_pattern(subcmt, subpat, |x,y| op(x,y));
|
2012-11-02 11:56:09 -05:00
|
|
|
}
|
|
|
|
}
|
2013-04-29 19:21:59 -05:00
|
|
|
Some(&ast::def_fn(*)) |
|
2013-03-22 21:26:41 -05:00
|
|
|
Some(&ast::def_struct(*)) => {
|
2013-06-17 18:43:22 -05:00
|
|
|
for subpats.iter().enumerate().advance |(i, &subpat)| {
|
2013-05-03 11:11:15 -05:00
|
|
|
let subpat_ty = self.pat_ty(subpat); // see (*)
|
|
|
|
let cmt_field =
|
2013-05-17 20:12:50 -05:00
|
|
|
self.cat_imm_interior(
|
|
|
|
pat, cmt, subpat_ty,
|
|
|
|
InteriorField(PositionalField(i)));
|
2013-06-21 19:08:35 -05:00
|
|
|
self.cat_pattern(cmt_field, subpat, |x,y| op(x,y));
|
2012-11-02 11:56:09 -05:00
|
|
|
}
|
|
|
|
}
|
2013-06-21 20:46:34 -05:00
|
|
|
Some(&ast::def_static(*)) => {
|
2013-06-21 07:29:53 -05:00
|
|
|
for subpats.iter().advance |&subpat| {
|
2013-06-21 19:08:35 -05:00
|
|
|
self.cat_pattern(cmt, subpat, |x,y| op(x,y));
|
2013-03-21 02:27:26 -05:00
|
|
|
}
|
|
|
|
}
|
2012-11-02 11:56:09 -05:00
|
|
|
_ => {
|
|
|
|
self.tcx.sess.span_bug(
|
|
|
|
pat.span,
|
2013-04-30 11:47:52 -05:00
|
|
|
"enum pattern didn't resolve to enum or struct");
|
2012-11-02 11:56:09 -05:00
|
|
|
}
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-20 14:23:37 -05:00
|
|
|
ast::pat_ident(_, _, Some(subpat)) => {
|
2012-08-08 10:15:32 -05:00
|
|
|
self.cat_pattern(cmt, subpat, op);
|
|
|
|
}
|
|
|
|
|
2012-08-20 14:23:37 -05:00
|
|
|
ast::pat_ident(_, _, None) => {
|
2012-08-08 10:15:32 -05:00
|
|
|
// nullary variant or identifier: ignore
|
|
|
|
}
|
|
|
|
|
2013-01-24 21:33:48 -06:00
|
|
|
ast::pat_struct(_, ref field_pats, _) => {
|
2012-08-08 10:15:32 -05:00
|
|
|
// {f1: p1, ..., fN: pN}
|
2013-06-21 07:29:53 -05:00
|
|
|
for field_pats.iter().advance |fp| {
|
2013-05-03 11:11:15 -05:00
|
|
|
let field_ty = self.pat_ty(fp.pat); // see (*)
|
2013-05-09 14:58:41 -05:00
|
|
|
let cmt_field = self.cat_field(pat, cmt, fp.ident, field_ty);
|
2013-06-21 19:08:35 -05:00
|
|
|
self.cat_pattern(cmt_field, fp.pat, |x,y| op(x,y));
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-24 21:33:48 -06:00
|
|
|
ast::pat_tup(ref subpats) => {
|
2012-08-08 10:15:32 -05:00
|
|
|
// (p1, ..., pN)
|
2013-06-17 18:43:22 -05:00
|
|
|
for subpats.iter().enumerate().advance |(i, &subpat)| {
|
2013-05-03 11:11:15 -05:00
|
|
|
let subpat_ty = self.pat_ty(subpat); // see (*)
|
2013-05-17 20:12:50 -05:00
|
|
|
let subcmt =
|
|
|
|
self.cat_imm_interior(
|
|
|
|
pat, cmt, subpat_ty,
|
|
|
|
InteriorField(PositionalField(i)));
|
2013-06-21 19:08:35 -05:00
|
|
|
self.cat_pattern(subcmt, subpat, |x,y| op(x,y));
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 19:07:32 -05:00
|
|
|
ast::pat_box(subpat) | ast::pat_uniq(subpat) |
|
|
|
|
ast::pat_region(subpat) => {
|
2012-09-11 23:25:01 -05:00
|
|
|
// @p1, ~p1
|
2013-05-03 11:11:15 -05:00
|
|
|
let subcmt = self.cat_deref(pat, cmt, 0);
|
2012-09-11 23:25:01 -05:00
|
|
|
self.cat_pattern(subcmt, subpat, op);
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-26 12:58:46 -06:00
|
|
|
ast::pat_vec(ref before, slice, ref after) => {
|
2013-05-09 14:58:02 -05:00
|
|
|
let elt_cmt = self.cat_index(pat, cmt, 0);
|
2013-06-21 07:29:53 -05:00
|
|
|
for before.iter().advance |&before_pat| {
|
2013-06-21 19:08:35 -05:00
|
|
|
self.cat_pattern(elt_cmt, before_pat, |x,y| op(x,y));
|
2013-01-24 21:33:48 -06:00
|
|
|
}
|
2013-06-10 16:50:12 -05:00
|
|
|
for slice.iter().advance |&slice_pat| {
|
2013-05-03 11:11:15 -05:00
|
|
|
let slice_ty = self.pat_ty(slice_pat);
|
2013-06-20 14:25:52 -05:00
|
|
|
let slice_cmt = self.cat_rvalue_node(pat, slice_ty);
|
2013-06-21 19:08:35 -05:00
|
|
|
self.cat_pattern(slice_cmt, slice_pat, |x,y| op(x,y));
|
2013-02-26 12:58:46 -06:00
|
|
|
}
|
2013-06-21 07:29:53 -05:00
|
|
|
for after.iter().advance |&after_pat| {
|
2013-06-21 19:08:35 -05:00
|
|
|
self.cat_pattern(elt_cmt, after_pat, |x,y| op(x,y));
|
2013-01-24 21:33:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ast::pat_lit(_) | ast::pat_range(_, _) => {
|
2012-12-08 14:22:43 -06:00
|
|
|
/*always ok*/
|
|
|
|
}
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn mut_to_str(&self, mutbl: ast::mutability) -> ~str {
|
2012-08-08 10:15:32 -05:00
|
|
|
match mutbl {
|
|
|
|
m_mutbl => ~"mutable",
|
|
|
|
m_const => ~"const",
|
|
|
|
m_imm => ~"immutable"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn cmt_to_str(&self, cmt: cmt) -> ~str {
|
2012-08-08 10:15:32 -05:00
|
|
|
match cmt.cat {
|
2013-05-17 20:12:50 -05:00
|
|
|
cat_static_item => {
|
|
|
|
~"static item"
|
|
|
|
}
|
|
|
|
cat_implicit_self => {
|
|
|
|
~"self reference"
|
|
|
|
}
|
2013-03-15 14:24:24 -05:00
|
|
|
cat_copied_upvar(_) => {
|
2012-08-08 10:15:32 -05:00
|
|
|
~"captured outer variable in a heap closure"
|
|
|
|
}
|
2013-06-20 14:25:52 -05:00
|
|
|
cat_rvalue(*) => {
|
2013-05-17 20:12:50 -05:00
|
|
|
~"non-lvalue"
|
|
|
|
}
|
|
|
|
cat_local(_) => {
|
|
|
|
~"local variable"
|
|
|
|
}
|
|
|
|
cat_self(_) => {
|
|
|
|
~"self value"
|
|
|
|
}
|
|
|
|
cat_arg(*) => {
|
|
|
|
~"argument"
|
|
|
|
}
|
|
|
|
cat_deref(_, _, pk) => {
|
|
|
|
fmt!("dereference of %s pointer", ptr_sigil(pk))
|
|
|
|
}
|
|
|
|
cat_interior(_, InteriorField(NamedField(_))) => {
|
|
|
|
~"field"
|
|
|
|
}
|
|
|
|
cat_interior(_, InteriorField(PositionalField(_))) => {
|
|
|
|
~"anonymous field"
|
|
|
|
}
|
2013-05-22 05:54:35 -05:00
|
|
|
cat_interior(_, InteriorElement(VecElement)) => {
|
|
|
|
~"vec content"
|
|
|
|
}
|
|
|
|
cat_interior(_, InteriorElement(StrElement)) => {
|
|
|
|
~"str content"
|
|
|
|
}
|
|
|
|
cat_interior(_, InteriorElement(OtherElement)) => {
|
|
|
|
~"indexed content"
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
2013-03-15 14:24:24 -05:00
|
|
|
cat_stack_upvar(_) => {
|
|
|
|
~"captured outer variable"
|
|
|
|
}
|
2012-08-08 10:15:32 -05:00
|
|
|
cat_discr(cmt, _) => {
|
|
|
|
self.cmt_to_str(cmt)
|
|
|
|
}
|
2013-05-17 20:12:50 -05:00
|
|
|
cat_downcast(cmt) => {
|
|
|
|
self.cmt_to_str(cmt)
|
|
|
|
}
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn region_to_str(&self, r: ty::Region) -> ~str {
|
2013-06-17 18:49:45 -05:00
|
|
|
region_ptr_to_str(self.tcx, r)
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2012-10-24 19:04:00 -05:00
|
|
|
/// The node_id here is the node of the expression that references the field.
|
|
|
|
/// This function looks it up in the def map in case the type happens to be
|
|
|
|
/// an enum to determine which variant is in use.
|
2013-01-30 15:44:24 -06:00
|
|
|
pub fn field_mutbl(tcx: ty::ctxt,
|
|
|
|
base_ty: ty::t,
|
|
|
|
f_name: ast::ident,
|
|
|
|
node_id: ast::node_id)
|
|
|
|
-> Option<ast::mutability> {
|
2013-03-05 20:06:53 -06:00
|
|
|
// Need to refactor so that struct/enum fields can be treated uniformly.
|
2013-03-20 00:17:42 -05:00
|
|
|
match ty::get(base_ty).sty {
|
2012-12-10 15:47:54 -06:00
|
|
|
ty::ty_struct(did, _) => {
|
2013-06-21 07:29:53 -05:00
|
|
|
let r = ty::lookup_struct_fields(tcx, did);
|
|
|
|
for r.iter().advance |fld| {
|
2012-06-01 12:46:17 -05:00
|
|
|
if fld.ident == f_name {
|
2013-05-03 20:51:58 -05:00
|
|
|
return Some(ast::m_imm);
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-24 19:04:00 -05:00
|
|
|
ty::ty_enum(*) => {
|
2013-05-05 11:17:59 -05:00
|
|
|
match tcx.def_map.get_copy(&node_id) {
|
2012-10-24 19:04:00 -05:00
|
|
|
ast::def_variant(_, variant_id) => {
|
2013-06-21 07:29:53 -05:00
|
|
|
let r = ty::lookup_struct_fields(tcx, variant_id);
|
|
|
|
for r.iter().advance |fld| {
|
2012-10-24 19:04:00 -05:00
|
|
|
if fld.ident == f_name {
|
2013-05-03 20:51:58 -05:00
|
|
|
return Some(ast::m_imm);
|
2012-10-24 19:04:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => { }
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2012-08-20 14:23:37 -05:00
|
|
|
return None;
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
2013-01-11 23:01:42 -06:00
|
|
|
|
2013-03-15 14:24:24 -05:00
|
|
|
pub enum AliasableReason {
|
|
|
|
AliasableManaged(ast::mutability),
|
|
|
|
AliasableBorrowed(ast::mutability),
|
|
|
|
AliasableOther
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl cmt_ {
|
|
|
|
pub fn guarantor(@self) -> cmt {
|
2013-03-15 14:24:24 -05:00
|
|
|
//! Returns `self` after stripping away any owned pointer derefs or
|
|
|
|
//! interior content. The return value is basically the `cmt` which
|
|
|
|
//! determines how long the value in `self` remains live.
|
|
|
|
|
|
|
|
match self.cat {
|
2013-06-20 14:25:52 -05:00
|
|
|
cat_rvalue(*) |
|
2013-03-15 14:24:24 -05:00
|
|
|
cat_static_item |
|
|
|
|
cat_implicit_self |
|
|
|
|
cat_copied_upvar(*) |
|
|
|
|
cat_local(*) |
|
|
|
|
cat_self(*) |
|
|
|
|
cat_arg(*) |
|
|
|
|
cat_deref(_, _, unsafe_ptr(*)) |
|
|
|
|
cat_deref(_, _, gc_ptr(*)) |
|
|
|
|
cat_deref(_, _, region_ptr(*)) => {
|
|
|
|
self
|
2013-01-11 23:01:42 -06:00
|
|
|
}
|
2013-05-17 20:12:50 -05:00
|
|
|
cat_downcast(b) |
|
2013-03-15 14:24:24 -05:00
|
|
|
cat_stack_upvar(b) |
|
|
|
|
cat_discr(b, _) |
|
|
|
|
cat_interior(b, _) |
|
|
|
|
cat_deref(b, _, uniq_ptr(*)) => {
|
|
|
|
b.guarantor()
|
2013-01-11 23:01:42 -06:00
|
|
|
}
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn is_freely_aliasable(&self) -> bool {
|
2013-03-15 14:24:24 -05:00
|
|
|
self.freely_aliasable().is_some()
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn freely_aliasable(&self) -> Option<AliasableReason> {
|
2013-03-15 14:24:24 -05:00
|
|
|
//! True if this lvalue resides in an area that is
|
|
|
|
//! freely aliasable, meaning that rustc cannot track
|
|
|
|
//! the alias//es with precision.
|
|
|
|
|
|
|
|
// Maybe non-obvious: copied upvars can only be considered
|
|
|
|
// non-aliasable in once closures, since any other kind can be
|
|
|
|
// aliased and eventually recused.
|
|
|
|
|
|
|
|
match self.cat {
|
|
|
|
cat_copied_upvar(CopiedUpvar {onceness: ast::Once, _}) |
|
|
|
|
cat_rvalue(*) |
|
|
|
|
cat_local(*) |
|
2013-04-30 13:08:18 -05:00
|
|
|
cat_arg(_) |
|
2013-03-15 14:24:24 -05:00
|
|
|
cat_self(*) |
|
|
|
|
cat_deref(_, _, unsafe_ptr(*)) | // of course it is aliasable, but...
|
|
|
|
cat_deref(_, _, region_ptr(m_mutbl, _)) => {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
cat_copied_upvar(CopiedUpvar {onceness: ast::Many, _}) |
|
|
|
|
cat_static_item(*) |
|
2013-04-30 13:08:18 -05:00
|
|
|
cat_implicit_self(*) => {
|
2013-03-15 14:24:24 -05:00
|
|
|
Some(AliasableOther)
|
|
|
|
}
|
|
|
|
|
|
|
|
cat_deref(_, _, gc_ptr(m)) => {
|
|
|
|
Some(AliasableManaged(m))
|
|
|
|
}
|
|
|
|
|
|
|
|
cat_deref(_, _, region_ptr(m @ m_const, _)) |
|
|
|
|
cat_deref(_, _, region_ptr(m @ m_imm, _)) => {
|
|
|
|
Some(AliasableBorrowed(m))
|
|
|
|
}
|
|
|
|
|
2013-05-17 20:12:50 -05:00
|
|
|
cat_downcast(b) |
|
2013-03-15 14:24:24 -05:00
|
|
|
cat_stack_upvar(b) |
|
|
|
|
cat_deref(b, _, uniq_ptr(*)) |
|
|
|
|
cat_interior(b, _) |
|
|
|
|
cat_discr(b, _) => {
|
|
|
|
b.freely_aliasable()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-08 18:54:28 -05:00
|
|
|
impl Repr for cmt_ {
|
2013-03-15 14:24:24 -05:00
|
|
|
fn repr(&self, tcx: ty::ctxt) -> ~str {
|
|
|
|
fmt!("{%s id:%d m:%? ty:%s}",
|
|
|
|
self.cat.repr(tcx),
|
|
|
|
self.id,
|
|
|
|
self.mutbl,
|
|
|
|
self.ty.repr(tcx))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Repr for categorization {
|
|
|
|
fn repr(&self, tcx: ty::ctxt) -> ~str {
|
|
|
|
match *self {
|
|
|
|
cat_static_item |
|
|
|
|
cat_implicit_self |
|
2013-06-20 14:25:52 -05:00
|
|
|
cat_rvalue(*) |
|
2013-03-15 14:24:24 -05:00
|
|
|
cat_copied_upvar(*) |
|
2013-01-11 23:01:42 -06:00
|
|
|
cat_local(*) |
|
2013-03-15 14:24:24 -05:00
|
|
|
cat_self(*) |
|
2013-06-20 14:25:52 -05:00
|
|
|
cat_arg(*) => {
|
|
|
|
fmt!("%?", *self)
|
|
|
|
}
|
2013-03-15 14:24:24 -05:00
|
|
|
cat_deref(cmt, derefs, ptr) => {
|
|
|
|
fmt!("%s->(%s, %u)", cmt.cat.repr(tcx),
|
|
|
|
ptr_sigil(ptr), derefs)
|
|
|
|
}
|
|
|
|
cat_interior(cmt, interior) => {
|
|
|
|
fmt!("%s.%s",
|
|
|
|
cmt.cat.repr(tcx),
|
|
|
|
interior.repr(tcx))
|
2013-01-11 23:01:42 -06:00
|
|
|
}
|
2013-05-17 20:12:50 -05:00
|
|
|
cat_downcast(cmt) => {
|
|
|
|
fmt!("%s->(enum)", cmt.cat.repr(tcx))
|
|
|
|
}
|
2013-03-15 14:24:24 -05:00
|
|
|
cat_stack_upvar(cmt) |
|
2013-06-20 14:25:52 -05:00
|
|
|
cat_discr(cmt, _) => {
|
|
|
|
cmt.cat.repr(tcx)
|
|
|
|
}
|
2013-01-11 23:01:42 -06:00
|
|
|
}
|
|
|
|
}
|
2013-03-15 14:24:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ptr_sigil(ptr: ptr_kind) -> ~str {
|
|
|
|
match ptr {
|
|
|
|
uniq_ptr(_) => ~"~",
|
|
|
|
gc_ptr(_) => ~"@",
|
|
|
|
region_ptr(_, _) => ~"&",
|
|
|
|
unsafe_ptr => ~"*"
|
|
|
|
}
|
|
|
|
}
|
2013-01-11 23:01:42 -06:00
|
|
|
|
2013-05-17 20:12:50 -05:00
|
|
|
impl Repr for InteriorKind {
|
2013-03-15 14:24:24 -05:00
|
|
|
fn repr(&self, tcx: ty::ctxt) -> ~str {
|
2013-01-11 23:01:42 -06:00
|
|
|
match *self {
|
2013-06-12 12:02:55 -05:00
|
|
|
InteriorField(NamedField(fld)) => tcx.sess.str_of(fld).to_owned(),
|
2013-05-17 20:12:50 -05:00
|
|
|
InteriorField(PositionalField(i)) => fmt!("#%?", i),
|
|
|
|
InteriorElement(_) => ~"[]",
|
2013-01-11 23:01:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-22 05:54:35 -05:00
|
|
|
|
|
|
|
fn element_kind(t: ty::t) -> ElementKind {
|
|
|
|
match ty::get(t).sty {
|
|
|
|
ty::ty_evec(*) => VecElement,
|
|
|
|
ty::ty_estr(*) => StrElement,
|
|
|
|
_ => OtherElement
|
|
|
|
}
|
|
|
|
}
|