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
|
|
|
|
|
|
|
export public_methods;
|
2012-06-01 17:46:32 -05:00
|
|
|
export opt_deref_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).
|
2012-06-01 17:46:32 -05:00
|
|
|
fn opt_deref_kind(t: ty::t) -> option<deref_kind> {
|
2012-06-01 12:46:17 -05:00
|
|
|
alt ty::get(t).struct {
|
2012-07-14 14:19:36 -05:00
|
|
|
ty::ty_uniq(*) |
|
2012-06-01 12:46:17 -05:00
|
|
|
ty::ty_evec(_, ty::vstore_uniq) |
|
|
|
|
ty::ty_estr(ty::vstore_uniq) {
|
2012-06-01 17:46:32 -05:00
|
|
|
some(deref_ptr(uniq_ptr))
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2012-07-26 10:51:57 -05:00
|
|
|
ty::ty_rptr(r, _) |
|
|
|
|
ty::ty_evec(_, ty::vstore_slice(r)) |
|
|
|
|
ty::ty_estr(ty::vstore_slice(r)) {
|
|
|
|
some(deref_ptr(region_ptr(r)))
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::ty_box(*) |
|
|
|
|
ty::ty_evec(_, ty::vstore_box) |
|
|
|
|
ty::ty_estr(ty::vstore_box) {
|
2012-06-01 17:46:32 -05:00
|
|
|
some(deref_ptr(gc_ptr))
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::ty_ptr(*) {
|
2012-06-01 17:46:32 -05:00
|
|
|
some(deref_ptr(unsafe_ptr))
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2012-06-12 14:05:26 -05:00
|
|
|
ty::ty_enum(did, _) {
|
|
|
|
some(deref_comp(comp_variant(did)))
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2012-06-04 18:21:14 -05:00
|
|
|
ty::ty_evec(mt, ty::vstore_fixed(_)) {
|
|
|
|
some(deref_comp(comp_index(t, mt.mutbl)))
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::ty_estr(ty::vstore_fixed(_)) {
|
|
|
|
some(deref_comp(comp_index(t, m_imm)))
|
|
|
|
}
|
|
|
|
|
2012-06-01 12:46:17 -05:00
|
|
|
_ {
|
2012-06-01 17:46:32 -05:00
|
|
|
none
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn deref_kind(tcx: ty::ctxt, t: ty::t) -> deref_kind {
|
|
|
|
alt opt_deref_kind(t) {
|
|
|
|
some(k) {k}
|
|
|
|
none {
|
2012-06-01 12:46:17 -05:00
|
|
|
tcx.sess.bug(
|
2012-07-30 18:01:07 -05:00
|
|
|
fmt!{"deref_cat() invoked on non-derefable type %s",
|
|
|
|
ty_to_str(tcx, t)});
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl public_methods for borrowck_ctxt {
|
|
|
|
fn cat_borrow_of_expr(expr: @ast::expr) -> cmt {
|
2012-07-20 18:37:45 -05:00
|
|
|
// a borrowed expression must be either an @, ~, or a @vec, ~vec
|
2012-06-01 12:46:17 -05:00
|
|
|
let expr_ty = ty::expr_ty(self.tcx, expr);
|
|
|
|
alt ty::get(expr_ty).struct {
|
2012-07-14 14:19:36 -05:00
|
|
|
ty::ty_evec(*) | ty::ty_estr(*) {
|
2012-06-01 12:46:17 -05:00
|
|
|
self.cat_index(expr, expr)
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::ty_uniq(*) | ty::ty_box(*) | ty::ty_rptr(*) {
|
|
|
|
let cmt = self.cat_expr(expr);
|
|
|
|
self.cat_deref(expr, cmt, 0u, true).get()
|
|
|
|
}
|
|
|
|
|
2012-07-20 18:37:45 -05:00
|
|
|
/*
|
|
|
|
ty::ty_fn({proto, _}) {
|
|
|
|
self.cat_call(expr, expr, proto)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2012-06-01 12:46:17 -05:00
|
|
|
_ {
|
|
|
|
self.tcx.sess.span_bug(
|
|
|
|
expr.span,
|
2012-07-30 18:01:07 -05:00
|
|
|
fmt!{"Borrowing of non-derefable type `%s`",
|
|
|
|
ty_to_str(self.tcx, expr_ty)});
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cat_expr(expr: @ast::expr) -> cmt {
|
2012-07-30 18:01:07 -05:00
|
|
|
debug!{"cat_expr: id=%d expr=%s",
|
|
|
|
expr.id, pprust::expr_to_str(expr)};
|
2012-06-01 12:46:17 -05:00
|
|
|
|
|
|
|
let tcx = self.tcx;
|
|
|
|
let expr_ty = tcx.ty(expr);
|
|
|
|
alt expr.node {
|
|
|
|
ast::expr_unary(ast::deref, e_base) {
|
|
|
|
if self.method_map.contains_key(expr.id) {
|
|
|
|
ret self.cat_rvalue(expr, expr_ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
let base_cmt = self.cat_expr(e_base);
|
|
|
|
alt self.cat_deref(expr, base_cmt, 0u, true) {
|
|
|
|
some(cmt) { ret cmt; }
|
|
|
|
none {
|
|
|
|
tcx.sess.span_bug(
|
|
|
|
e_base.span,
|
2012-07-30 18:01:07 -05:00
|
|
|
fmt!{"Explicit deref of non-derefable type `%s`",
|
|
|
|
ty_to_str(tcx, tcx.ty(e_base))});
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ast::expr_field(base, f_name, _) {
|
|
|
|
if self.method_map.contains_key(expr.id) {
|
|
|
|
ret self.cat_method_ref(expr, expr_ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
let base_cmt = self.cat_autoderef(base);
|
|
|
|
self.cat_field(expr, base_cmt, f_name)
|
|
|
|
}
|
|
|
|
|
|
|
|
ast::expr_index(base, _) {
|
|
|
|
if self.method_map.contains_key(expr.id) {
|
|
|
|
ret self.cat_rvalue(expr, expr_ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.cat_index(expr, base)
|
|
|
|
}
|
|
|
|
|
|
|
|
ast::expr_path(_) {
|
|
|
|
let def = self.tcx.def_map.get(expr.id);
|
|
|
|
self.cat_def(expr.id, expr.span, expr_ty, def)
|
|
|
|
}
|
|
|
|
|
2012-06-19 21:34:01 -05:00
|
|
|
ast::expr_addr_of(*) | ast::expr_call(*) |
|
2012-06-01 12:46:17 -05:00
|
|
|
ast::expr_swap(*) | ast::expr_move(*) | ast::expr_assign(*) |
|
|
|
|
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-06-18 19:42:09 -05:00
|
|
|
ast::expr_loop_body(*) | ast::expr_do_body(*) | ast::expr_unary(*) |
|
2012-06-01 12:46:17 -05:00
|
|
|
ast::expr_copy(*) | ast::expr_cast(*) | ast::expr_fail(*) |
|
|
|
|
ast::expr_vstore(*) | ast::expr_vec(*) | ast::expr_tup(*) |
|
2012-07-13 20:43:52 -05:00
|
|
|
ast::expr_if(*) | ast::expr_log(*) |
|
2012-06-01 12:46:17 -05:00
|
|
|
ast::expr_new(*) | ast::expr_binary(*) | ast::expr_while(*) |
|
|
|
|
ast::expr_block(*) | ast::expr_loop(*) | ast::expr_alt(*) |
|
|
|
|
ast::expr_lit(*) | ast::expr_break | ast::expr_mac(*) |
|
2012-07-31 19:31:24 -05:00
|
|
|
ast::expr_again | ast::expr_rec(*) | ast::expr_struct(*) |
|
|
|
|
ast::expr_unary_move(*) {
|
2012-06-01 12:46:17 -05:00
|
|
|
ret self.cat_rvalue(expr, expr_ty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cat_def(id: ast::node_id,
|
|
|
|
span: span,
|
|
|
|
expr_ty: ty::t,
|
|
|
|
def: ast::def) -> cmt {
|
|
|
|
alt def {
|
2012-07-26 16:04:03 -05:00
|
|
|
ast::def_fn(*) | 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-07-26 16:04:03 -05:00
|
|
|
ast::def_ty_param(*) | ast::def_class(*) |
|
|
|
|
ast::def_typaram_binder(*) | ast::def_region(_) {
|
2012-06-01 12:46:17 -05:00
|
|
|
@{id:id, span:span,
|
|
|
|
cat:cat_special(sk_static_item), lp:none,
|
|
|
|
mutbl:m_imm, ty:expr_ty}
|
|
|
|
}
|
|
|
|
|
|
|
|
ast::def_arg(vid, mode) {
|
|
|
|
// 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
|
|
|
|
let {m,lp} = alt ty::resolved_mode(self.tcx, mode) {
|
|
|
|
ast::by_mutbl_ref {
|
|
|
|
{m: m_mutbl, lp: none}
|
|
|
|
}
|
|
|
|
ast::by_move | ast::by_copy {
|
|
|
|
{m: m_imm, lp: some(@lp_arg(vid))}
|
|
|
|
}
|
|
|
|
ast::by_ref {
|
|
|
|
{m: m_imm, lp: 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.
|
|
|
|
{m: m_imm, lp: none}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
@{id:id, span:span,
|
|
|
|
cat:cat_arg(vid), lp:lp,
|
|
|
|
mutbl:m, ty:expr_ty}
|
|
|
|
}
|
|
|
|
|
|
|
|
ast::def_self(_) {
|
|
|
|
@{id:id, span:span,
|
|
|
|
cat:cat_special(sk_self), lp:none,
|
|
|
|
mutbl:m_imm, ty:expr_ty}
|
|
|
|
}
|
|
|
|
|
|
|
|
ast::def_upvar(upvid, inner, fn_node_id) {
|
|
|
|
let ty = ty::node_id_to_type(self.tcx, fn_node_id);
|
|
|
|
let proto = ty::ty_fn_proto(ty);
|
|
|
|
alt proto {
|
|
|
|
ast::proto_any | ast::proto_block {
|
|
|
|
let upcmt = self.cat_def(id, span, expr_ty, *inner);
|
|
|
|
@{id:id, span:span,
|
|
|
|
cat:cat_stack_upvar(upcmt), lp:upcmt.lp,
|
|
|
|
mutbl:upcmt.mutbl, ty:upcmt.ty}
|
|
|
|
}
|
|
|
|
ast::proto_bare | ast::proto_uniq | ast::proto_box {
|
|
|
|
// FIXME #2152 allow mutation of moved upvars
|
|
|
|
@{id:id, span:span,
|
|
|
|
cat:cat_special(sk_heap_upvar), lp:none,
|
|
|
|
mutbl:m_imm, ty:expr_ty}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ast::def_local(vid, mutbl) {
|
|
|
|
let m = if mutbl {m_mutbl} else {m_imm};
|
|
|
|
@{id:id, span:span,
|
|
|
|
cat:cat_local(vid), lp:some(@lp_local(vid)),
|
|
|
|
mutbl:m, ty:expr_ty}
|
|
|
|
}
|
|
|
|
|
2012-06-21 11:41:33 -05:00
|
|
|
ast::def_binding(pid) {
|
|
|
|
// bindings are "special" since they are implicit pointers.
|
|
|
|
|
|
|
|
// lookup the mutability for this binding that we found in
|
|
|
|
// gather_loans when we categorized it
|
|
|
|
let mutbl = self.binding_map.get(pid);
|
|
|
|
|
2012-06-01 12:46:17 -05:00
|
|
|
@{id:id, span:span,
|
2012-06-21 11:41:33 -05:00
|
|
|
cat:cat_binding(pid), lp:none,
|
|
|
|
mutbl:mutbl, ty:expr_ty}
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-12 14:05:26 -05:00
|
|
|
fn cat_variant<N: ast_node>(arg: N,
|
|
|
|
enum_did: ast::def_id,
|
|
|
|
cmt: cmt) -> cmt {
|
2012-06-01 12:46:17 -05:00
|
|
|
@{id: arg.id(), span: arg.span(),
|
2012-06-12 14:05:26 -05:00
|
|
|
cat: cat_comp(cmt, comp_variant(enum_did)),
|
2012-06-30 18:19:07 -05:00
|
|
|
lp: cmt.lp.map(|l| @lp_comp(l, comp_variant(enum_did)) ),
|
2012-06-01 12:46:17 -05:00
|
|
|
mutbl: cmt.mutbl, // imm iff in an immutable context
|
|
|
|
ty: self.tcx.ty(arg)}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cat_rvalue(expr: @ast::expr, expr_ty: ty::t) -> cmt {
|
|
|
|
@{id:expr.id, span:expr.span,
|
|
|
|
cat:cat_rvalue, lp:none,
|
|
|
|
mutbl:m_imm, ty:expr_ty}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cat_discr(cmt: cmt, alt_id: ast::node_id) -> cmt {
|
|
|
|
ret @{cat:cat_discr(cmt, alt_id) with *cmt};
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
fn inherited_mutability(base_m: ast::mutability,
|
|
|
|
comp_m: ast::mutability) -> ast::mutability {
|
|
|
|
alt comp_m {
|
|
|
|
m_imm => {base_m} // imm: as mutable as the container
|
|
|
|
m_mutbl | m_const => {comp_m}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-10 02:49:59 -05:00
|
|
|
fn cat_field<N:ast_node>(node: N, base_cmt: cmt,
|
|
|
|
f_name: ast::ident) -> cmt {
|
2012-06-01 12:46:17 -05:00
|
|
|
let f_mutbl = alt field_mutbl(self.tcx, base_cmt.ty, f_name) {
|
|
|
|
some(f_mutbl) { f_mutbl }
|
|
|
|
none {
|
|
|
|
self.tcx.sess.span_bug(
|
|
|
|
node.span(),
|
2012-07-30 18:01:07 -05:00
|
|
|
fmt!{"Cannot find field `%s` in type `%s`",
|
|
|
|
*f_name, 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-06-30 18:19:07 -05:00
|
|
|
let lp = base_cmt.lp.map(|lp| @lp_comp(lp, f_comp) );
|
2012-06-01 12:46:17 -05:00
|
|
|
@{id: node.id(), span: node.span(),
|
2012-06-01 17:46:32 -05:00
|
|
|
cat: cat_comp(base_cmt, f_comp), lp:lp,
|
2012-06-01 12:46:17 -05:00
|
|
|
mutbl: m, ty: self.tcx.ty(node)}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cat_deref<N:ast_node>(node: N, base_cmt: cmt, derefs: uint,
|
|
|
|
expl: bool) -> option<cmt> {
|
2012-06-30 18:19:07 -05:00
|
|
|
do ty::deref(self.tcx, base_cmt.ty, expl).map |mt| {
|
2012-06-01 12:46:17 -05:00
|
|
|
alt deref_kind(self.tcx, base_cmt.ty) {
|
|
|
|
deref_ptr(ptr) {
|
2012-06-30 18:19:07 -05:00
|
|
|
let lp = do base_cmt.lp.chain |l| {
|
2012-06-01 12:46:17 -05:00
|
|
|
// Given that the ptr itself is loanable, we can
|
|
|
|
// loan out deref'd uniq ptrs as the data they are
|
|
|
|
// the only way to reach the data they point at.
|
|
|
|
// Other ptr types admit aliases and are therefore
|
|
|
|
// not loanable.
|
|
|
|
alt ptr {
|
2012-07-19 22:13:00 -05:00
|
|
|
uniq_ptr => {some(@lp_deref(l, ptr))}
|
2012-07-26 10:51:57 -05:00
|
|
|
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.
|
|
|
|
let m = alt ptr {
|
|
|
|
uniq_ptr => {
|
|
|
|
self.inherited_mutability(base_cmt.mutbl, mt.mutbl)
|
|
|
|
}
|
2012-07-26 10:51:57 -05:00
|
|
|
gc_ptr | region_ptr(_) | unsafe_ptr => {
|
2012-07-20 18:37:45 -05:00
|
|
|
mt.mutbl
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-01 12:46:17 -05:00
|
|
|
@{id:node.id(), span:node.span(),
|
|
|
|
cat:cat_deref(base_cmt, derefs, ptr), lp:lp,
|
2012-07-20 18:37:45 -05:00
|
|
|
mutbl:m, ty:mt.ty}
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
deref_comp(comp) {
|
2012-06-30 18:19:07 -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);
|
2012-06-01 12:46:17 -05:00
|
|
|
@{id:node.id(), span:node.span(),
|
|
|
|
cat:cat_comp(base_cmt, comp), lp:lp,
|
2012-07-19 22:13:00 -05:00
|
|
|
mutbl:m, ty:mt.ty}
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cat_index(expr: @ast::expr, base: @ast::expr) -> cmt {
|
|
|
|
let base_cmt = self.cat_autoderef(base);
|
|
|
|
|
|
|
|
let mt = alt ty::index(self.tcx, base_cmt.ty) {
|
|
|
|
some(mt) { mt }
|
|
|
|
none {
|
|
|
|
self.tcx.sess.span_bug(
|
|
|
|
expr.span,
|
2012-07-30 18:01:07 -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-06-04 18:21:14 -05:00
|
|
|
ret alt deref_kind(self.tcx, base_cmt.ty) {
|
|
|
|
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
|
|
|
|
let deref_lp = alt ptr {
|
|
|
|
uniq_ptr => {base_cmt.lp.map(|lp| @lp_deref(lp, uniq_ptr))}
|
|
|
|
_ => {none}
|
|
|
|
};
|
|
|
|
|
2012-07-20 18:37:45 -05:00
|
|
|
// (b) for unique ptrs, we inherit mutability from the
|
|
|
|
// owning reference.
|
|
|
|
let m = alt ptr {
|
|
|
|
uniq_ptr => {
|
|
|
|
self.inherited_mutability(base_cmt.mutbl, mt.mutbl)
|
|
|
|
}
|
2012-07-26 10:51:57 -05:00
|
|
|
gc_ptr | region_ptr(_) | unsafe_ptr => {
|
2012-07-20 18:37:45 -05:00
|
|
|
mt.mutbl
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// (c) the deref is explicit in the resulting cmt
|
2012-06-04 18:21:14 -05:00
|
|
|
let deref_cmt = @{id:expr.id, span:expr.span,
|
2012-07-17 20:44:00 -05:00
|
|
|
cat:cat_deref(base_cmt, 0u, ptr), lp:deref_lp,
|
2012-07-20 18:37:45 -05:00
|
|
|
mutbl:m, ty:mt.ty};
|
2012-07-17 20:44:00 -05:00
|
|
|
|
2012-07-20 18:37:45 -05:00
|
|
|
comp(expr, deref_cmt, base_cmt.ty, m, mt.ty)
|
2012-06-04 18:21:14 -05:00
|
|
|
}
|
|
|
|
|
2012-06-01 12:46:17 -05:00
|
|
|
deref_comp(_) {
|
2012-06-04 18:21:14 -05:00
|
|
|
// fixed-length vectors have no deref
|
2012-07-20 18:37:45 -05:00
|
|
|
comp(expr, base_cmt, base_cmt.ty, mt.mutbl, mt.ty)
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-04 18:21:14 -05:00
|
|
|
fn comp(expr: @ast::expr, of_cmt: cmt,
|
2012-07-20 18:37:45 -05:00
|
|
|
vect: ty::t, mutbl: ast::mutability, ty: ty::t) -> cmt {
|
|
|
|
let comp = comp_index(vect, mutbl);
|
2012-06-30 18:19:07 -05:00
|
|
|
let index_lp = of_cmt.lp.map(|lp| @lp_comp(lp, comp) );
|
2012-06-04 18:21:14 -05:00
|
|
|
@{id:expr.id, span:expr.span,
|
|
|
|
cat:cat_comp(of_cmt, comp), lp:index_lp,
|
2012-07-20 18:37:45 -05:00
|
|
|
mutbl:mutbl, ty:ty}
|
2012-06-04 18:21:14 -05:00
|
|
|
}
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn cat_tuple_elt<N: ast_node>(elt: N, cmt: cmt) -> cmt {
|
|
|
|
@{id: elt.id(), span: elt.span(),
|
|
|
|
cat: cat_comp(cmt, comp_tuple),
|
2012-06-30 18:19:07 -05:00
|
|
|
lp: cmt.lp.map(|l| @lp_comp(l, comp_tuple) ),
|
2012-06-01 12:46:17 -05:00
|
|
|
mutbl: cmt.mutbl, // imm iff in an immutable context
|
|
|
|
ty: self.tcx.ty(elt)}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl private_methods for borrowck_ctxt {
|
|
|
|
fn cat_method_ref(expr: @ast::expr, expr_ty: ty::t) -> cmt {
|
|
|
|
@{id:expr.id, span:expr.span,
|
|
|
|
cat:cat_special(sk_method), lp:none,
|
|
|
|
mutbl:m_imm, ty:expr_ty}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cat_autoderef(base: @ast::expr) -> cmt {
|
|
|
|
// Creates a string of implicit derefences so long as base is
|
|
|
|
// dereferencable. n.b., it is important that these dereferences are
|
|
|
|
// associated with the field/index that caused the autoderef (expr).
|
|
|
|
// This is used later to adjust ref counts and so forth in trans.
|
|
|
|
|
|
|
|
// Given something like base.f where base has type @m1 @m2 T, we want
|
|
|
|
// to yield the equivalent categories to (**base).f.
|
|
|
|
let mut cmt = self.cat_expr(base);
|
|
|
|
let mut ctr = 0u;
|
|
|
|
loop {
|
|
|
|
ctr += 1u;
|
|
|
|
alt self.cat_deref(base, cmt, ctr, false) {
|
|
|
|
none { ret cmt; }
|
|
|
|
some(cmt1) { cmt = cmt1; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn field_mutbl(tcx: ty::ctxt,
|
|
|
|
base_ty: ty::t,
|
2012-06-10 02:49:59 -05:00
|
|
|
f_name: ast::ident) -> option<ast::mutability> {
|
2012-06-01 12:46:17 -05:00
|
|
|
// Need to refactor so that records/class fields can be treated uniformly.
|
|
|
|
alt ty::get(base_ty).struct {
|
|
|
|
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 {
|
|
|
|
ret some(f.mt.mutbl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ty::ty_class(did, substs) {
|
2012-06-30 18:19:07 -05:00
|
|
|
for ty::lookup_class_fields(tcx, did).each |fld| {
|
2012-06-01 12:46:17 -05:00
|
|
|
if fld.ident == f_name {
|
|
|
|
let m = alt fld.mutability {
|
|
|
|
ast::class_mutable { ast::m_mutbl }
|
|
|
|
ast::class_immutable { ast::m_imm }
|
|
|
|
};
|
|
|
|
ret some(m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ { }
|
|
|
|
}
|
|
|
|
|
|
|
|
ret none;
|
|
|
|
}
|