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.
|
|
|
|
|
2013-01-07 16:16:52 -06:00
|
|
|
|
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-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2012-12-23 16:41:37 -06:00
|
|
|
use middle::ty;
|
|
|
|
use middle::typeck;
|
|
|
|
use util::ppaux::{ty_to_str, region_to_str};
|
|
|
|
use util::common::indenter;
|
|
|
|
|
|
|
|
use core::cmp;
|
|
|
|
use core::to_bytes;
|
|
|
|
use core::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-01-11 23:01:42 -06:00
|
|
|
#[deriving_eq]
|
2013-01-30 15:44:24 -06:00
|
|
|
pub enum categorization {
|
2012-08-08 10:15:32 -05:00
|
|
|
cat_rvalue, // result of eval'ing some misc expr
|
|
|
|
cat_special(special_kind), //
|
|
|
|
cat_local(ast::node_id), // local variable
|
|
|
|
cat_binding(ast::node_id), // pattern binding
|
|
|
|
cat_arg(ast::node_id), // formal argument
|
|
|
|
cat_stack_upvar(cmt), // upvar in stack closure
|
|
|
|
cat_deref(cmt, uint, ptr_kind), // deref of a ptr
|
|
|
|
cat_comp(cmt, comp_kind), // adjust to locate an internal component
|
|
|
|
cat_discr(cmt, ast::node_id), // match discriminant (see preserve())
|
2013-01-23 20:15:06 -06:00
|
|
|
cat_self(ast::node_id), // explicit `self`
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// different kinds of pointers:
|
2013-01-11 23:01:42 -06:00
|
|
|
#[deriving_eq]
|
2013-01-08 21:37:25 -06:00
|
|
|
pub enum ptr_kind {
|
2012-08-27 18:26:35 -05:00
|
|
|
uniq_ptr,
|
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
|
|
|
|
}
|
|
|
|
|
2012-08-08 10:15:32 -05:00
|
|
|
// I am coining the term "components" to mean "pieces of a data
|
|
|
|
// structure accessible without a dereference":
|
2013-01-11 23:01:42 -06:00
|
|
|
#[deriving_eq]
|
2013-01-08 21:37:25 -06:00
|
|
|
pub enum comp_kind {
|
2012-08-08 10:15:32 -05:00
|
|
|
comp_tuple, // elt in a tuple
|
2012-11-02 11:56:09 -05:00
|
|
|
comp_anon_field, // anonymous field (in e.g.
|
|
|
|
// struct Foo(int, int);
|
2012-08-08 10:15:32 -05:00
|
|
|
comp_variant(ast::def_id), // internals to a variant of given enum
|
|
|
|
comp_field(ast::ident, // name of field
|
|
|
|
ast::mutability), // declared mutability of field
|
|
|
|
comp_index(ty::t, // type of vec/str/etc being deref'd
|
|
|
|
ast::mutability) // mutability of vec content
|
|
|
|
}
|
|
|
|
|
|
|
|
// different kinds of expressions we might evaluate
|
2013-01-11 23:01:42 -06:00
|
|
|
#[deriving_eq]
|
2013-01-30 15:44:24 -06:00
|
|
|
pub enum special_kind {
|
2012-08-08 10:15:32 -05:00
|
|
|
sk_method,
|
|
|
|
sk_static_item,
|
2012-12-10 13:58:37 -06:00
|
|
|
sk_implicit_self, // old by-reference `self`
|
2012-08-08 10:15:32 -05:00
|
|
|
sk_heap_upvar
|
|
|
|
}
|
|
|
|
|
2013-02-09 00:21:45 -06:00
|
|
|
#[deriving_eq]
|
|
|
|
pub enum MutabilityCategory {
|
|
|
|
McImmutable, // Immutable.
|
|
|
|
McReadOnly, // Read-only (`const`)
|
|
|
|
McDeclared, // Directly declared as mutable.
|
|
|
|
McInherited // Inherited from the fact that owner is mutable.
|
|
|
|
}
|
|
|
|
|
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
|
|
|
//
|
|
|
|
// note: cmt stands for "categorized mutable type".
|
2013-01-25 18:57:39 -06: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
|
|
|
|
lp: Option<@loan_path>, // loan path for expr, if any
|
|
|
|
mutbl: MutabilityCategory, // mutability of expr as lvalue
|
|
|
|
ty: ty::t // type of the expr
|
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
|
|
|
// a loan path is like a category, but it exists only when the data is
|
|
|
|
// interior to the stack frame. loan paths are used as the key to a
|
|
|
|
// map indicating what is borrowed at any point in time.
|
2013-01-23 20:15:06 -06:00
|
|
|
#[deriving_eq]
|
2013-01-08 21:37:25 -06:00
|
|
|
pub enum loan_path {
|
2012-08-08 10:15:32 -05:00
|
|
|
lp_local(ast::node_id),
|
|
|
|
lp_arg(ast::node_id),
|
2013-01-23 20:15:06 -06:00
|
|
|
lp_self,
|
2012-08-08 10:15:32 -05:00
|
|
|
lp_deref(@loan_path, ptr_kind),
|
|
|
|
lp_comp(@loan_path, comp_kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We pun on *T to mean both actual deref of a ptr as well
|
|
|
|
// as accessing of components:
|
2013-01-30 15:44:24 -06:00
|
|
|
pub enum deref_kind {deref_ptr(ptr_kind), deref_comp(comp_kind)}
|
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-01-31 19:12:29 -06:00
|
|
|
ty::ty_uniq(*) |
|
|
|
|
ty::ty_evec(_, ty::vstore_uniq) |
|
|
|
|
ty::ty_estr(ty::vstore_uniq) |
|
|
|
|
ty::ty_closure(ty::ClosureTy {sigil: ast::OwnedSigil, _}) => {
|
|
|
|
Some(deref_ptr(uniq_ptr))
|
|
|
|
}
|
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-01-31 19:12:29 -06:00
|
|
|
ty::ty_enum(did, _) => {
|
|
|
|
Some(deref_comp(comp_variant(did)))
|
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
|
2013-01-31 19:12:29 -06:00
|
|
|
ty::ty_struct(_, _) => {
|
|
|
|
Some(deref_comp(comp_anon_field))
|
|
|
|
}
|
2012-12-13 20:03:13 -06:00
|
|
|
|
2013-01-31 19:12:29 -06:00
|
|
|
ty::ty_evec(mt, ty::vstore_fixed(_)) => {
|
|
|
|
Some(deref_comp(comp_index(t, mt.mutbl)))
|
|
|
|
}
|
2012-06-04 18:21:14 -05:00
|
|
|
|
2013-01-31 19:12:29 -06:00
|
|
|
ty::ty_estr(ty::vstore_fixed(_)) => {
|
|
|
|
Some(deref_comp(comp_index(t, m_imm)))
|
|
|
|
}
|
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-02-20 19:07:17 -06:00
|
|
|
pub fn cat_variant<N:ast_node>(
|
2012-08-08 10:15:32 -05:00
|
|
|
tcx: ty::ctxt,
|
|
|
|
method_map: typeck::method_map,
|
|
|
|
arg: N,
|
|
|
|
enum_did: ast::def_id,
|
|
|
|
cmt: cmt) -> cmt {
|
|
|
|
|
|
|
|
let mcx = &mem_categorization_ctxt {
|
|
|
|
tcx: tcx, method_map: method_map
|
|
|
|
};
|
|
|
|
return mcx.cat_variant(arg, enum_did, cmt);
|
|
|
|
}
|
|
|
|
|
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 trait get_type_for_node {
|
2013-02-22 00:41:37 -06:00
|
|
|
fn ty<N:ast_node>(&self, node: N) -> ty::t;
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-26 19:12:00 -06:00
|
|
|
impl get_type_for_node for ty::ctxt {
|
2013-02-22 00:41:37 -06:00
|
|
|
fn ty<N:ast_node>(&self, node: N) -> ty::t {
|
|
|
|
ty::node_id_to_type(*self, node.id())
|
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 {
|
|
|
|
pure fn to_str(&self) -> ~str {
|
|
|
|
fmt!("%?", *self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-26 19:47:41 -06:00
|
|
|
pub impl MutabilityCategory {
|
2013-02-22 00:41:37 -06:00
|
|
|
static fn from_mutbl(&self, m: ast::mutability) -> MutabilityCategory {
|
2013-02-09 00:21:45 -06:00
|
|
|
match m {
|
|
|
|
m_imm => McImmutable,
|
|
|
|
m_const => McReadOnly,
|
|
|
|
m_mutbl => McDeclared
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inherit(&self) -> MutabilityCategory {
|
|
|
|
match *self {
|
|
|
|
McImmutable => McImmutable,
|
|
|
|
McReadOnly => McReadOnly,
|
|
|
|
McDeclared => McInherited,
|
|
|
|
McInherited => McInherited
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_mutable(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
McImmutable | McReadOnly => false,
|
|
|
|
McDeclared | McInherited => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_immutable(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
McImmutable => true,
|
|
|
|
McReadOnly | McDeclared | McInherited => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_user_str(&self) -> ~str {
|
|
|
|
match *self {
|
|
|
|
McDeclared | McInherited => ~"mutable",
|
|
|
|
McImmutable => ~"immutable",
|
|
|
|
McReadOnly => ~"const"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub impl mem_categorization_ctxt {
|
|
|
|
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-02-27 18:28:37 -06:00
|
|
|
Some(@ty::AutoAddEnv(*)) => {
|
|
|
|
// 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);
|
|
|
|
self.cat_rvalue(expr, expr_ty)
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(
|
|
|
|
@ty::AutoDerefRef(
|
|
|
|
ty::AutoDerefRef {
|
|
|
|
autoref: Some(_), _})) => {
|
|
|
|
// Equivalent to &*expr or something similar.
|
|
|
|
// Result is an rvalue.
|
|
|
|
let expr_ty = ty::expr_ty_adjusted(self.tcx, expr);
|
|
|
|
self.cat_rvalue(expr, expr_ty)
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(
|
|
|
|
@ty::AutoDerefRef(
|
|
|
|
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-02-09 00:21:45 -06:00
|
|
|
fn cat_expr_autoderefd(&self,
|
|
|
|
expr: @ast::expr,
|
2013-02-27 18:28:37 -06:00
|
|
|
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-02-09 00:21:45 -06:00
|
|
|
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
|
|
|
|
|
|
|
let tcx = self.tcx;
|
|
|
|
let expr_ty = tcx.ty(expr);
|
2012-08-06 14:34:08 -05:00
|
|
|
match expr.node {
|
2012-08-03 21:59:04 -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) {
|
2012-08-01 19:30:05 -05:00
|
|
|
return self.cat_rvalue(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-02-08 16:08:02 -06:00
|
|
|
if self.method_map.contains_key(&expr.id) {
|
2012-08-01 19:30:05 -05:00
|
|
|
return self.cat_method_ref(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);
|
2012-10-24 19:04:00 -05:00
|
|
|
self.cat_field(expr, base_cmt, f_name, expr.id)
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::expr_index(base, _) => {
|
2013-02-08 16:08:02 -06:00
|
|
|
if self.method_map.contains_key(&expr.id) {
|
2012-08-01 19:30:05 -05:00
|
|
|
return self.cat_rvalue(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);
|
|
|
|
self.cat_index(expr, base_cmt)
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::expr_path(_) => {
|
2013-02-05 21:41:45 -06:00
|
|
|
let def = self.tcx.def_map.get(&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),
|
|
|
|
|
2012-06-19 21:34:01 -05:00
|
|
|
ast::expr_addr_of(*) | ast::expr_call(*) |
|
2012-10-23 13:28:20 -05:00
|
|
|
ast::expr_swap(*) | ast::expr_assign(*) |
|
2012-06-01 12:46:17 -05:00
|
|
|
ast::expr_assign_op(*) | ast::expr_fn(*) | ast::expr_fn_block(*) |
|
2012-07-13 20:43:52 -05:00
|
|
|
ast::expr_assert(*) | ast::expr_ret(*) |
|
2012-11-30 13:18:25 -06:00
|
|
|
ast::expr_loop_body(*) | ast::expr_do_body(*) |
|
|
|
|
ast::expr_unary(*) | ast::expr_method_call(*) |
|
2013-01-31 20:24:09 -06:00
|
|
|
ast::expr_copy(*) | ast::expr_cast(*) |
|
2012-06-01 12:46:17 -05:00
|
|
|
ast::expr_vstore(*) | ast::expr_vec(*) | ast::expr_tup(*) |
|
2012-07-13 20:43:52 -05:00
|
|
|
ast::expr_if(*) | ast::expr_log(*) |
|
2012-08-02 18:00:45 -05:00
|
|
|
ast::expr_binary(*) | ast::expr_while(*) |
|
2012-08-07 15:35:51 -05:00
|
|
|
ast::expr_block(*) | ast::expr_loop(*) | ast::expr_match(*) |
|
2012-08-14 21:20:56 -05:00
|
|
|
ast::expr_lit(*) | ast::expr_break(*) | ast::expr_mac(*) |
|
|
|
|
ast::expr_again(*) | ast::expr_rec(*) | ast::expr_struct(*) |
|
2013-01-10 12:59:58 -06:00
|
|
|
ast::expr_repeat(*) => {
|
2012-08-01 19:30:05 -05:00
|
|
|
return self.cat_rvalue(expr, expr_ty);
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-09 00:21:45 -06:00
|
|
|
fn cat_def(&self,
|
|
|
|
id: ast::node_id,
|
2012-06-01 12:46:17 -05:00
|
|
|
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(_) |
|
2012-06-26 18:18:37 -05:00
|
|
|
ast::def_foreign_mod(_) | ast::def_const(_) |
|
2012-07-26 16:04:03 -05:00
|
|
|
ast::def_use(_) | ast::def_variant(*) |
|
2012-06-01 12:46:17 -05:00
|
|
|
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(_) |
|
2012-12-10 13:58:37 -06:00
|
|
|
ast::def_label(_) | ast::def_self_ty(*) => {
|
2013-01-25 18:57:39 -06:00
|
|
|
@cmt_ {
|
|
|
|
id:id,
|
|
|
|
span:span,
|
|
|
|
cat:cat_special(sk_static_item),
|
|
|
|
lp:None,
|
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-01-03 22:20:56 -06:00
|
|
|
ast::def_arg(vid, mode, 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
|
|
|
|
// lp: loan path, must be none for aliasable things
|
2013-02-09 00:21:45 -06:00
|
|
|
let m = if mutbl {McDeclared} else {McImmutable};
|
2013-01-03 22:20:56 -06:00
|
|
|
let lp = match ty::resolved_mode(self.tcx, mode) {
|
2013-01-10 12:59:58 -06:00
|
|
|
ast::by_copy => Some(@lp_arg(vid)),
|
|
|
|
ast::by_ref => None,
|
|
|
|
ast::by_val => {
|
|
|
|
// by-value is this hybrid mode where we have a
|
|
|
|
// pointer but we do not own it. This is not
|
|
|
|
// considered loanable because, for example, a by-ref
|
|
|
|
// and and by-val argument might both actually contain
|
|
|
|
// the same unique ptr.
|
|
|
|
None
|
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
};
|
2013-01-25 18:57:39 -06:00
|
|
|
@cmt_ {
|
|
|
|
id:id,
|
|
|
|
span:span,
|
|
|
|
cat:cat_arg(vid),
|
|
|
|
lp:lp,
|
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) => {
|
|
|
|
let cat, loan_path;
|
|
|
|
if is_implicit {
|
|
|
|
cat = cat_special(sk_implicit_self);
|
|
|
|
loan_path = None;
|
2012-12-10 13:58:37 -06:00
|
|
|
} else {
|
2013-01-23 20:15:06 -06:00
|
|
|
cat = cat_self(self_id);
|
|
|
|
loan_path = Some(@lp_self);
|
2012-12-10 13:58:37 -06:00
|
|
|
};
|
|
|
|
|
2013-01-25 18:57:39 -06:00
|
|
|
@cmt_ {
|
|
|
|
id:id,
|
|
|
|
span:span,
|
|
|
|
cat:cat,
|
|
|
|
lp:loan_path,
|
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
|
|
|
}
|
|
|
|
|
2012-08-26 14:12:05 -05:00
|
|
|
ast::def_upvar(_, inner, fn_node_id, _) => {
|
2012-06-01 12:46:17 -05:00
|
|
|
let ty = ty::node_id_to_type(self.tcx, fn_node_id);
|
2013-01-31 19:12:29 -06:00
|
|
|
let sigil = ty::ty_closure_sigil(ty);
|
|
|
|
match sigil {
|
|
|
|
ast::BorrowedSigil => {
|
2012-11-04 22:41:00 -06:00
|
|
|
let upcmt = self.cat_def(id, span, expr_ty, *inner);
|
2013-01-25 18:57:39 -06:00
|
|
|
@cmt_ {
|
|
|
|
id:id,
|
|
|
|
span:span,
|
|
|
|
cat:cat_stack_upvar(upcmt),
|
|
|
|
lp:upcmt.lp,
|
|
|
|
mutbl:upcmt.mutbl,
|
|
|
|
ty:upcmt.ty
|
|
|
|
}
|
2012-11-04 22:41:00 -06:00
|
|
|
}
|
2013-01-31 19:12:29 -06:00
|
|
|
ast::OwnedSigil | ast::ManagedSigil => {
|
2012-11-04 22:41:00 -06:00
|
|
|
// FIXME #2152 allow mutation of moved upvars
|
2013-01-25 18:57:39 -06:00
|
|
|
@cmt_ {
|
|
|
|
id:id,
|
|
|
|
span:span,
|
|
|
|
cat:cat_special(sk_heap_upvar),
|
|
|
|
lp:None,
|
2013-02-09 00:21:45 -06:00
|
|
|
mutbl:McImmutable,
|
2013-01-25 18:57:39 -06:00
|
|
|
ty:expr_ty
|
|
|
|
}
|
2012-11-04 22:41:00 -06:00
|
|
|
}
|
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),
|
|
|
|
lp:Some(@lp_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),
|
|
|
|
lp:Some(@lp_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-02-20 19:07:17 -06:00
|
|
|
fn cat_variant<N:ast_node>(&self,
|
2013-02-09 00:21:45 -06:00
|
|
|
arg: N,
|
2012-06-12 14:05:26 -05:00
|
|
|
enum_did: ast::def_id,
|
|
|
|
cmt: cmt) -> cmt {
|
2013-01-25 18:57:39 -06:00
|
|
|
@cmt_ {
|
|
|
|
id: arg.id(),
|
|
|
|
span: arg.span(),
|
|
|
|
cat: cat_comp(cmt, comp_variant(enum_did)),
|
|
|
|
lp: cmt.lp.map(|l| @lp_comp(*l, comp_variant(enum_did)) ),
|
2013-02-09 00:21:45 -06:00
|
|
|
mutbl: cmt.mutbl.inherit(),
|
2013-01-25 18:57:39 -06:00
|
|
|
ty: self.tcx.ty(arg)
|
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2013-02-20 19:07:17 -06:00
|
|
|
fn cat_rvalue<N:ast_node>(&self, elt: N, expr_ty: ty::t) -> cmt {
|
2013-01-25 18:57:39 -06:00
|
|
|
@cmt_ {
|
|
|
|
id:elt.id(),
|
|
|
|
span:elt.span(),
|
|
|
|
cat:cat_rvalue,
|
|
|
|
lp:None,
|
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
|
|
|
}
|
|
|
|
|
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-02-09 00:21:45 -06:00
|
|
|
fn inherited_mutability(&self,
|
|
|
|
base_m: MutabilityCategory,
|
|
|
|
comp_m: ast::mutability) -> MutabilityCategory
|
|
|
|
{
|
2012-08-06 14:34:08 -05:00
|
|
|
match comp_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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-24 19:04:00 -05:00
|
|
|
/// The `field_id` parameter is the ID of the enclosing expression or
|
|
|
|
/// pattern. It is used to determine which variant of an enum is in use.
|
2013-02-09 00:21:45 -06:00
|
|
|
fn cat_field<N:ast_node>(&self,
|
|
|
|
node: N,
|
2012-10-24 19:04:00 -05:00
|
|
|
base_cmt: cmt,
|
|
|
|
f_name: ast::ident,
|
|
|
|
field_id: ast::node_id) -> cmt {
|
2012-11-04 22:41:00 -06:00
|
|
|
let f_mutbl = match field_mutbl(self.tcx, base_cmt.ty,
|
|
|
|
f_name, field_id) {
|
|
|
|
Some(f_mutbl) => f_mutbl,
|
|
|
|
None => {
|
|
|
|
self.tcx.sess.span_bug(
|
|
|
|
node.span(),
|
|
|
|
fmt!("Cannot find field `%s` in type `%s`",
|
2013-02-10 18:33:16 -06:00
|
|
|
*self.tcx.sess.str_of(f_name),
|
2012-11-04 22:41:00 -06:00
|
|
|
ty_to_str(self.tcx, base_cmt.ty)));
|
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
};
|
2012-07-19 22:13:00 -05:00
|
|
|
let m = self.inherited_mutability(base_cmt.mutbl, f_mutbl);
|
2012-06-01 17:46:32 -05:00
|
|
|
let f_comp = comp_field(f_name, f_mutbl);
|
2012-09-26 18:27:12 -05:00
|
|
|
let lp = base_cmt.lp.map(|lp| @lp_comp(*lp, f_comp) );
|
2013-01-25 18:57:39 -06:00
|
|
|
@cmt_ {
|
|
|
|
id: node.id(),
|
|
|
|
span: node.span(),
|
|
|
|
cat: cat_comp(base_cmt, f_comp),
|
|
|
|
lp:lp,
|
|
|
|
mutbl: m,
|
|
|
|
ty: self.tcx.ty(node)
|
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2013-02-09 00:21:45 -06:00
|
|
|
fn cat_deref_fn<N:ast_node>(&self,
|
|
|
|
node: N,
|
2012-11-04 22:41:00 -06:00
|
|
|
base_cmt: cmt,
|
|
|
|
deref_cnt: uint) -> cmt
|
|
|
|
{
|
|
|
|
// 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-02-09 00:21:45 -06:00
|
|
|
fn cat_deref<N:ast_node>(&self,
|
|
|
|
node: N,
|
2012-09-11 23:25:01 -05:00
|
|
|
base_cmt: cmt,
|
2012-11-04 22:41:00 -06:00
|
|
|
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-02-09 00:21:45 -06:00
|
|
|
fn cat_deref_common<N:ast_node>(&self,
|
|
|
|
node: N,
|
2012-11-04 22:41:00 -06:00
|
|
|
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-09-27 19:01:28 -05:00
|
|
|
let lp = do base_cmt.lp.chain_ref |l| {
|
2012-06-01 12:46:17 -05:00
|
|
|
// Given that the ptr itself is loanable, we can
|
2013-01-23 20:15:06 -06:00
|
|
|
// loan out deref'd uniq ptrs or mut ptrs as the data
|
|
|
|
// they are the only way to mutably reach the data they
|
|
|
|
// point at. Other ptr types admit mutable aliases and
|
|
|
|
// are therefore not loanable.
|
2012-08-06 14:34:08 -05:00
|
|
|
match ptr {
|
2013-01-23 20:15:06 -06:00
|
|
|
uniq_ptr => Some(@lp_deref(*l, ptr)),
|
|
|
|
region_ptr(ast::m_mutbl, _) => {
|
|
|
|
Some(@lp_deref(*l, ptr))
|
|
|
|
}
|
|
|
|
gc_ptr(*) | region_ptr(_, _) | unsafe_ptr => None
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
};
|
2012-07-19 22:13:00 -05:00
|
|
|
|
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 {
|
2012-09-11 23:25:01 -05:00
|
|
|
uniq_ptr => {
|
|
|
|
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),
|
|
|
|
lp:lp,
|
|
|
|
mutbl:m,
|
|
|
|
ty:mt.ty
|
|
|
|
}
|
2012-09-11 23:25:01 -05:00
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
|
2012-09-11 23:25:01 -05:00
|
|
|
deref_comp(comp) => {
|
2012-09-26 18:27:12 -05:00
|
|
|
let lp = base_cmt.lp.map(|l| @lp_comp(*l, comp) );
|
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(),
|
|
|
|
cat:cat_comp(base_cmt, comp),
|
|
|
|
lp:lp,
|
|
|
|
mutbl:m,
|
|
|
|
ty:mt.ty
|
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 19:07:17 -06:00
|
|
|
fn cat_index<N:ast_node>(&self,
|
2013-02-09 00:21:45 -06:00
|
|
|
elt: N,
|
|
|
|
base_cmt: cmt) -> cmt {
|
2012-08-06 14:34:08 -05:00
|
|
|
let mt = match ty::index(self.tcx, 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) => {
|
2012-07-17 20:44:00 -05:00
|
|
|
// (a) the contents are loanable if the base is loanable
|
|
|
|
// and this is a *unique* vector
|
2012-08-06 14:34:08 -05:00
|
|
|
let deref_lp = match ptr {
|
2012-09-26 18:27:12 -05:00
|
|
|
uniq_ptr => {base_cmt.lp.map(|lp| @lp_deref(*lp, uniq_ptr))}
|
2012-08-20 14:23:37 -05:00
|
|
|
_ => {None}
|
2012-07-17 20:44:00 -05:00
|
|
|
};
|
|
|
|
|
2012-07-20 18:37:45 -05:00
|
|
|
// (b) for unique ptrs, we inherit mutability from the
|
|
|
|
// owning reference.
|
2012-08-06 14:34:08 -05:00
|
|
|
let m = match ptr {
|
2012-07-20 18:37:45 -05:00
|
|
|
uniq_ptr => {
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// (c) 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(),
|
|
|
|
cat:cat_deref(base_cmt, 0u, ptr),
|
|
|
|
lp:deref_lp,
|
|
|
|
mutbl:m,
|
|
|
|
ty:mt.ty
|
|
|
|
};
|
2012-07-17 20:44:00 -05:00
|
|
|
|
2013-02-09 00:21:45 -06:00
|
|
|
comp(elt, deref_cmt, base_cmt.ty, m, mt)
|
2012-06-04 18:21:14 -05:00
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:04 -05:00
|
|
|
deref_comp(_) => {
|
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-02-09 00:21:45 -06:00
|
|
|
comp(elt, base_cmt, base_cmt.ty, m, mt)
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-02-20 19:07:17 -06:00
|
|
|
fn comp<N:ast_node>(elt: N, of_cmt: cmt,
|
2013-02-09 00:21:45 -06:00
|
|
|
vect: ty::t, mutbl: MutabilityCategory,
|
|
|
|
mt: ty::mt) -> cmt
|
2013-01-24 21:33:48 -06:00
|
|
|
{
|
2013-02-09 00:21:45 -06:00
|
|
|
let comp = comp_index(vect, mt.mutbl);
|
2012-09-26 18:27:12 -05:00
|
|
|
let index_lp = of_cmt.lp.map(|lp| @lp_comp(*lp, comp) );
|
2013-01-25 18:57:39 -06:00
|
|
|
@cmt_ {
|
|
|
|
id:elt.id(),
|
|
|
|
span:elt.span(),
|
|
|
|
cat:cat_comp(of_cmt, comp),
|
|
|
|
lp:index_lp,
|
|
|
|
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-02-20 19:07:17 -06:00
|
|
|
fn cat_tuple_elt<N:ast_node>(&self,
|
2013-02-09 00:21:45 -06:00
|
|
|
elt: N,
|
|
|
|
cmt: cmt) -> cmt {
|
2013-01-25 18:57:39 -06:00
|
|
|
@cmt_ {
|
|
|
|
id: elt.id(),
|
|
|
|
span: elt.span(),
|
|
|
|
cat: cat_comp(cmt, comp_tuple),
|
|
|
|
lp: cmt.lp.map(|l| @lp_comp(*l, comp_tuple) ),
|
2013-02-09 00:21:45 -06:00
|
|
|
mutbl: cmt.mutbl.inherit(),
|
2013-01-25 18:57:39 -06:00
|
|
|
ty: self.tcx.ty(elt)
|
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2013-02-20 19:07:17 -06:00
|
|
|
fn cat_anon_struct_field<N:ast_node>(&self,
|
2013-02-09 00:21:45 -06:00
|
|
|
elt: N,
|
|
|
|
cmt: cmt) -> cmt {
|
2013-01-25 18:57:39 -06:00
|
|
|
@cmt_ {
|
|
|
|
id: elt.id(),
|
|
|
|
span: elt.span(),
|
|
|
|
cat: cat_comp(cmt, comp_anon_field),
|
|
|
|
lp: cmt.lp.map(|l| @lp_comp(*l, comp_anon_field)),
|
2013-02-09 00:21:45 -06:00
|
|
|
mutbl: cmt.mutbl.inherit(),
|
2013-01-25 18:57:39 -06:00
|
|
|
ty: self.tcx.ty(elt)
|
|
|
|
}
|
2012-11-02 11:56:09 -05:00
|
|
|
}
|
|
|
|
|
2013-02-09 00:21:45 -06:00
|
|
|
fn cat_method_ref(&self,
|
|
|
|
expr: @ast::expr,
|
|
|
|
expr_ty: ty::t) -> cmt {
|
2013-01-25 18:57:39 -06:00
|
|
|
@cmt_ {
|
|
|
|
id:expr.id,
|
|
|
|
span:expr.span,
|
|
|
|
cat:cat_special(sk_method),
|
|
|
|
lp:None,
|
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-02-09 00:21:45 -06:00
|
|
|
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.
|
|
|
|
//
|
|
|
|
// The correspondence between the id in the cmt and which
|
|
|
|
// pattern is being referred to is somewhat...subtle. In
|
|
|
|
// general, the id of the cmt is the id of the node that
|
|
|
|
// produces the value. For patterns, that's actually the
|
|
|
|
// *subpattern*, generally speaking.
|
|
|
|
//
|
|
|
|
// To see what I mean about ids etc, consider:
|
|
|
|
//
|
|
|
|
// let x = @@3;
|
|
|
|
// match x {
|
|
|
|
// @@y { ... }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Here the cmt for `y` would be something like
|
|
|
|
//
|
|
|
|
// local(x)->@->@
|
|
|
|
//
|
|
|
|
// where the id of `local(x)` is the id of the `x` that appears
|
2013-01-04 08:52:07 -06:00
|
|
|
// in the match, the id of `local(x)->@` is the `@y` pattern,
|
2012-08-08 10:15:32 -05:00
|
|
|
// and the id of `local(x)->@->@` is the id of the `y` pattern.
|
|
|
|
|
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()),
|
2012-08-22 19:24:52 -05:00
|
|
|
self.cmt_to_repr(cmt));
|
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) {
|
2012-11-02 11:56:09 -05:00
|
|
|
Some(ast::def_variant(enum_did, _)) => {
|
|
|
|
// variant(x, y, z)
|
|
|
|
for subpats.each |subpat| {
|
|
|
|
let subcmt = self.cat_variant(*subpat, enum_did, cmt);
|
|
|
|
self.cat_pattern(subcmt, *subpat, op);
|
|
|
|
}
|
|
|
|
}
|
2012-12-10 15:47:54 -06:00
|
|
|
Some(ast::def_struct(*)) => {
|
2012-11-02 11:56:09 -05:00
|
|
|
for subpats.each |subpat| {
|
|
|
|
let cmt_field = self.cat_anon_struct_field(*subpat,
|
|
|
|
cmt);
|
|
|
|
self.cat_pattern(cmt_field, *subpat, op);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
self.tcx.sess.span_bug(
|
|
|
|
pat.span,
|
|
|
|
~"enum pattern didn't resolve to enum or struct");
|
|
|
|
}
|
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_rec(ref field_pats, _) |
|
|
|
|
ast::pat_struct(_, ref field_pats, _) => {
|
2012-08-08 10:15:32 -05:00
|
|
|
// {f1: p1, ..., fN: pN}
|
|
|
|
for field_pats.each |fp| {
|
2012-10-24 19:04:00 -05:00
|
|
|
let cmt_field = self.cat_field(fp.pat, cmt, fp.ident, pat.id);
|
2012-08-08 10:15:32 -05:00
|
|
|
self.cat_pattern(cmt_field, fp.pat, op);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-24 21:33:48 -06:00
|
|
|
ast::pat_tup(ref subpats) => {
|
2012-08-08 10:15:32 -05:00
|
|
|
// (p1, ..., pN)
|
|
|
|
for subpats.each |subpat| {
|
2012-09-19 18:55:01 -05:00
|
|
|
let subcmt = self.cat_tuple_elt(*subpat, cmt);
|
|
|
|
self.cat_pattern(subcmt, *subpat, op);
|
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
|
|
|
|
let subcmt = self.cat_deref(subpat, cmt, 0);
|
|
|
|
self.cat_pattern(subcmt, subpat, op);
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
|
2013-01-24 21:33:48 -06:00
|
|
|
ast::pat_vec(ref pats, opt_tail_pat) => {
|
|
|
|
for pats.each |pat| {
|
|
|
|
let elt_cmt = self.cat_index(*pat, cmt);
|
|
|
|
self.cat_pattern(elt_cmt, *pat, op);
|
|
|
|
}
|
|
|
|
|
|
|
|
for opt_tail_pat.each |tail_pat| {
|
|
|
|
let tail_ty = self.tcx.ty(*tail_pat);
|
|
|
|
let tail_cmt = self.cat_rvalue(*tail_pat, tail_ty);
|
|
|
|
self.cat_pattern(tail_cmt, *tail_pat, op);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-09 00:21:45 -06:00
|
|
|
fn cat_to_repr(&self, cat: categorization) -> ~str {
|
2012-08-08 10:15:32 -05:00
|
|
|
match cat {
|
|
|
|
cat_special(sk_method) => ~"method",
|
|
|
|
cat_special(sk_static_item) => ~"static_item",
|
2012-12-10 13:58:37 -06:00
|
|
|
cat_special(sk_implicit_self) => ~"implicit-self",
|
2012-08-08 10:15:32 -05:00
|
|
|
cat_special(sk_heap_upvar) => ~"heap-upvar",
|
|
|
|
cat_stack_upvar(_) => ~"stack-upvar",
|
|
|
|
cat_rvalue => ~"rvalue",
|
2012-08-22 19:24:52 -05:00
|
|
|
cat_local(node_id) => fmt!("local(%d)", node_id),
|
|
|
|
cat_binding(node_id) => fmt!("binding(%d)", node_id),
|
|
|
|
cat_arg(node_id) => fmt!("arg(%d)", node_id),
|
2013-01-23 20:15:06 -06:00
|
|
|
cat_self(node_id) => fmt!("self(%d)", node_id),
|
2012-08-08 10:15:32 -05:00
|
|
|
cat_deref(cmt, derefs, ptr) => {
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("%s->(%s, %u)", self.cat_to_repr(cmt.cat),
|
|
|
|
self.ptr_sigil(ptr), derefs)
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
cat_comp(cmt, comp) => {
|
2013-02-10 18:33:16 -06:00
|
|
|
fmt!("%s.%s", self.cat_to_repr(cmt.cat), *self.comp_to_repr(comp))
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
cat_discr(cmt, _) => self.cat_to_repr(cmt.cat)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-09 00:21:45 -06:00
|
|
|
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-02-09 00:21:45 -06:00
|
|
|
fn ptr_sigil(&self, ptr: ptr_kind) -> ~str {
|
2012-08-08 10:15:32 -05:00
|
|
|
match ptr {
|
|
|
|
uniq_ptr => ~"~",
|
2013-01-11 23:01:42 -06:00
|
|
|
gc_ptr(_) => ~"@",
|
2013-01-23 20:15:06 -06:00
|
|
|
region_ptr(_, _) => ~"&",
|
2012-08-08 10:15:32 -05:00
|
|
|
unsafe_ptr => ~"*"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-10 18:33:16 -06:00
|
|
|
fn comp_to_repr(&self, comp: comp_kind) -> @~str {
|
2012-08-08 10:15:32 -05:00
|
|
|
match comp {
|
2012-07-18 18:18:02 -05:00
|
|
|
comp_field(fld, _) => self.tcx.sess.str_of(fld),
|
2013-02-10 18:33:16 -06:00
|
|
|
comp_index(*) => @~"[]",
|
|
|
|
comp_tuple => @~"()",
|
|
|
|
comp_anon_field => @~"<anonymous field>",
|
|
|
|
comp_variant(_) => @~"<enum>"
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-09 00:21:45 -06:00
|
|
|
fn lp_to_str(&self, lp: @loan_path) -> ~str {
|
2012-08-08 10:15:32 -05:00
|
|
|
match *lp {
|
|
|
|
lp_local(node_id) => {
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("local(%d)", node_id)
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
lp_arg(node_id) => {
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("arg(%d)", node_id)
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
2013-01-23 20:15:06 -06:00
|
|
|
lp_self => ~"self",
|
2012-08-08 10:15:32 -05:00
|
|
|
lp_deref(lp, ptr) => {
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("%s->(%s)", self.lp_to_str(lp),
|
|
|
|
self.ptr_sigil(ptr))
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
lp_comp(lp, comp) => {
|
2012-08-22 19:24:52 -05:00
|
|
|
fmt!("%s.%s", self.lp_to_str(lp),
|
2013-02-10 18:33:16 -06:00
|
|
|
*self.comp_to_repr(comp))
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-09 00:21:45 -06:00
|
|
|
fn cmt_to_repr(&self, cmt: cmt) -> ~str {
|
|
|
|
fmt!("{%s id:%d m:%? lp:%s ty:%s}",
|
2012-08-08 10:15:32 -05:00
|
|
|
self.cat_to_repr(cmt.cat),
|
|
|
|
cmt.id,
|
2013-02-09 00:21:45 -06:00
|
|
|
cmt.mutbl,
|
2012-09-26 18:27:12 -05:00
|
|
|
cmt.lp.map_default(~"none", |p| self.lp_to_str(*p) ),
|
2012-08-22 19:24:52 -05:00
|
|
|
ty_to_str(self.tcx, cmt.ty))
|
2012-08-08 10:15:32 -05:00
|
|
|
}
|
|
|
|
|
2013-02-09 00:21:45 -06:00
|
|
|
fn cmt_to_str(&self, cmt: cmt) -> ~str {
|
|
|
|
let mut_str = cmt.mutbl.to_user_str();
|
2012-08-08 10:15:32 -05:00
|
|
|
match cmt.cat {
|
|
|
|
cat_special(sk_method) => ~"method",
|
|
|
|
cat_special(sk_static_item) => ~"static item",
|
2012-12-10 13:58:37 -06:00
|
|
|
cat_special(sk_implicit_self) => ~"self reference",
|
2012-08-08 10:15:32 -05:00
|
|
|
cat_special(sk_heap_upvar) => {
|
|
|
|
~"captured outer variable in a heap closure"
|
|
|
|
}
|
|
|
|
cat_rvalue => ~"non-lvalue",
|
|
|
|
cat_local(_) => mut_str + ~" local variable",
|
|
|
|
cat_binding(_) => ~"pattern binding",
|
2013-01-23 20:15:06 -06:00
|
|
|
cat_self(_) => ~"self value",
|
2012-08-08 10:15:32 -05:00
|
|
|
cat_arg(_) => ~"argument",
|
2012-08-22 19:24:52 -05:00
|
|
|
cat_deref(_, _, pk) => fmt!("dereference of %s %s pointer",
|
|
|
|
mut_str, self.ptr_sigil(pk)),
|
2012-08-08 10:15:32 -05:00
|
|
|
cat_stack_upvar(_) => {
|
|
|
|
~"captured outer " + mut_str + ~" variable in a stack closure"
|
|
|
|
}
|
|
|
|
cat_comp(_, comp_field(*)) => mut_str + ~" field",
|
|
|
|
cat_comp(_, comp_tuple) => ~"tuple content",
|
2012-11-02 11:56:09 -05:00
|
|
|
cat_comp(_, comp_anon_field) => ~"anonymous field",
|
2012-08-08 10:15:32 -05:00
|
|
|
cat_comp(_, comp_variant(_)) => ~"enum content",
|
|
|
|
cat_comp(_, comp_index(t, _)) => {
|
2012-09-11 18:20:31 -05:00
|
|
|
match ty::get(t).sty {
|
2012-08-08 10:15:32 -05:00
|
|
|
ty::ty_evec(*) => mut_str + ~" vec content",
|
|
|
|
ty::ty_estr(*) => mut_str + ~" str content",
|
|
|
|
_ => mut_str + ~" indexed content"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cat_discr(cmt, _) => {
|
|
|
|
self.cmt_to_str(cmt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-09 00:21:45 -06:00
|
|
|
fn region_to_str(&self, r: ty::Region) -> ~str {
|
2012-08-08 10:15:32 -05:00
|
|
|
region_to_str(self.tcx, r)
|
|
|
|
}
|
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> {
|
2012-06-01 12:46:17 -05:00
|
|
|
// Need to refactor so that records/class fields can be treated uniformly.
|
2013-01-07 16:16:52 -06:00
|
|
|
match /*bad*/copy ty::get(base_ty).sty {
|
2012-08-03 21:59:04 -05:00
|
|
|
ty::ty_rec(fields) => {
|
2012-06-30 18:19:07 -05:00
|
|
|
for fields.each |f| {
|
2012-06-01 12:46:17 -05:00
|
|
|
if f.ident == f_name {
|
2012-08-20 14:23:37 -05:00
|
|
|
return Some(f.mt.mutbl);
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-10 15:47:54 -06:00
|
|
|
ty::ty_struct(did, _) => {
|
|
|
|
for ty::lookup_struct_fields(tcx, did).each |fld| {
|
2012-06-01 12:46:17 -05:00
|
|
|
if fld.ident == f_name {
|
2012-08-06 14:34:08 -05:00
|
|
|
let m = match fld.mutability {
|
2012-12-10 15:47:54 -06:00
|
|
|
ast::struct_mutable => ast::m_mutbl,
|
|
|
|
ast::struct_immutable => ast::m_imm
|
2012-06-01 12:46:17 -05:00
|
|
|
};
|
2012-08-20 14:23:37 -05:00
|
|
|
return Some(m);
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-24 19:04:00 -05:00
|
|
|
ty::ty_enum(*) => {
|
2013-02-05 21:41:45 -06:00
|
|
|
match tcx.def_map.get(&node_id) {
|
2012-10-24 19:04:00 -05:00
|
|
|
ast::def_variant(_, variant_id) => {
|
2012-12-10 15:47:54 -06:00
|
|
|
for ty::lookup_struct_fields(tcx, variant_id).each |fld| {
|
2012-10-24 19:04:00 -05:00
|
|
|
if fld.ident == f_name {
|
|
|
|
let m = match fld.mutability {
|
2012-12-10 15:47:54 -06:00
|
|
|
ast::struct_mutable => ast::m_mutbl,
|
|
|
|
ast::struct_immutable => ast::m_imm
|
2012-10-24 19:04:00 -05:00
|
|
|
};
|
|
|
|
return Some(m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
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-01-30 15:44:24 -06:00
|
|
|
pub impl categorization {
|
2013-01-11 23:01:42 -06:00
|
|
|
fn derefs_through_mutable_box(&const self) -> bool {
|
|
|
|
match *self {
|
|
|
|
cat_deref(_, _, gc_ptr(ast::m_mutbl)) => {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
cat_deref(subcmt, _, _) |
|
|
|
|
cat_comp(subcmt, _) |
|
|
|
|
cat_discr(subcmt, _) |
|
|
|
|
cat_stack_upvar(subcmt) => {
|
|
|
|
subcmt.cat.derefs_through_mutable_box()
|
|
|
|
}
|
|
|
|
cat_rvalue |
|
|
|
|
cat_special(*) |
|
|
|
|
cat_local(*) |
|
|
|
|
cat_binding(*) |
|
2013-01-23 20:15:06 -06:00
|
|
|
cat_arg(*) |
|
|
|
|
cat_self(*) => {
|
2013-01-11 23:01:42 -06:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_mutable_box(&const self) -> bool {
|
|
|
|
match *self {
|
|
|
|
cat_deref(_, _, gc_ptr(ast::m_mutbl)) => true,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|