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-08-06 14:34:08 -05:00
|
|
|
match 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) |
|
2012-08-03 21:59:04 -05:00
|
|
|
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)) |
|
2012-08-03 21:59:04 -05:00
|
|
|
ty::ty_estr(ty::vstore_slice(r)) => {
|
2012-07-26 10:51:57 -05:00
|
|
|
some(deref_ptr(region_ptr(r)))
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ty::ty_box(*) |
|
|
|
|
ty::ty_evec(_, ty::vstore_box) |
|
2012-08-03 21:59:04 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:04 -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-08-03 21:59:04 -05:00
|
|
|
ty::ty_enum(did, _) => {
|
2012-06-12 14:05:26 -05:00
|
|
|
some(deref_comp(comp_variant(did)))
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:04 -05:00
|
|
|
ty::ty_evec(mt, ty::vstore_fixed(_)) => {
|
2012-06-04 18:21:14 -05:00
|
|
|
some(deref_comp(comp_index(t, mt.mutbl)))
|
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:04 -05:00
|
|
|
ty::ty_estr(ty::vstore_fixed(_)) => {
|
2012-06-04 18:21:14 -05:00
|
|
|
some(deref_comp(comp_index(t, m_imm)))
|
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => none
|
2012-06-01 17:46:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03 21:59:04 -05:00
|
|
|
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);
|
2012-08-06 14:34:08 -05:00
|
|
|
match ty::get(expr_ty).struct {
|
2012-08-03 21:59:04 -05:00
|
|
|
ty::ty_evec(*) | ty::ty_estr(*) => {
|
2012-06-01 12:46:17 -05:00
|
|
|
self.cat_index(expr, expr)
|
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:04 -05:00
|
|
|
ty::ty_uniq(*) | ty::ty_box(*) | ty::ty_rptr(*) => {
|
2012-06-01 12:46:17 -05:00
|
|
|
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-08-03 21:59:04 -05:00
|
|
|
_ => {
|
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);
|
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) => {
|
2012-06-01 12:46:17 -05: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-08-06 14:34:08 -05:00
|
|
|
match self.cat_deref(expr, base_cmt, 0u, true) {
|
2012-08-03 21:59:04 -05:00
|
|
|
some(cmt) => return cmt,
|
|
|
|
none => {
|
2012-06-01 12:46:17 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::expr_field(base, f_name, _) => {
|
2012-06-01 12:46:17 -05: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
|
|
|
}
|
|
|
|
|
|
|
|
let base_cmt = self.cat_autoderef(base);
|
|
|
|
self.cat_field(expr, base_cmt, f_name)
|
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::expr_index(base, _) => {
|
2012-06-01 12:46:17 -05: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
|
|
|
}
|
|
|
|
|
|
|
|
self.cat_index(expr, base)
|
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::expr_path(_) => {
|
2012-06-01 12:46:17 -05:00
|
|
|
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-08-02 18:00:45 -05:00
|
|
|
ast::expr_binary(*) | ast::expr_while(*) |
|
2012-06-01 12:46:17 -05:00
|
|
|
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(*) |
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::expr_unary_move(*) | 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cat_def(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-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(*) |
|
2012-08-03 21:59:04 -05:00
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::def_arg(vid, mode) => {
|
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
|
2012-08-06 14:34:08 -05:00
|
|
|
let {m,lp} = match ty::resolved_mode(self.tcx, mode) {
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::by_mutbl_ref => {
|
2012-06-01 12:46:17 -05:00
|
|
|
{m: m_mutbl, lp: none}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::by_move | ast::by_copy => {
|
2012-06-01 12:46:17 -05:00
|
|
|
{m: m_imm, lp: some(@lp_arg(vid))}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::by_ref => {
|
2012-06-01 12:46:17 -05:00
|
|
|
{m: m_imm, lp: none}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::by_val => {
|
2012-06-01 12:46:17 -05:00
|
|
|
// 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}
|
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::def_self(_) => {
|
2012-06-01 12:46:17 -05:00
|
|
|
@{id:id, span:span,
|
|
|
|
cat:cat_special(sk_self), lp:none,
|
|
|
|
mutbl:m_imm, ty:expr_ty}
|
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::def_upvar(upvid, inner, fn_node_id) => {
|
2012-06-01 12:46:17 -05:00
|
|
|
let ty = ty::node_id_to_type(self.tcx, fn_node_id);
|
|
|
|
let proto = ty::ty_fn_proto(ty);
|
2012-08-06 14:34:08 -05:00
|
|
|
match proto {
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::proto_block => {
|
2012-06-01 12:46:17 -05:00
|
|
|
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}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::proto_bare | ast::proto_uniq | ast::proto_box => {
|
2012-06-01 12:46:17 -05:00
|
|
|
// 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}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::def_local(vid, mutbl) => {
|
2012-06-01 12:46:17 -05:00
|
|
|
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-08-03 21:59:04 -05:00
|
|
|
ast::def_binding(vid, ast::bind_by_value) => {
|
2012-08-02 18:00:45 -05:00
|
|
|
// by-value bindings are basically local variables
|
|
|
|
@{id:id, span:span,
|
|
|
|
cat:cat_local(vid), lp:some(@lp_local(vid)),
|
|
|
|
mutbl:m_imm, ty:expr_ty}
|
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::def_binding(pid, ast::bind_by_ref) => {
|
2012-06-21 11:41:33 -05:00
|
|
|
// 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 {
|
2012-08-01 19:30:05 -05:00
|
|
|
return @{cat:cat_discr(cmt, alt_id) with *cmt};
|
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.
|
|
|
|
fn inherited_mutability(base_m: ast::mutability,
|
|
|
|
comp_m: ast::mutability) -> ast::mutability {
|
2012-08-06 14:34:08 -05:00
|
|
|
match comp_m {
|
2012-07-19 22:13:00 -05:00
|
|
|
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-08-06 14:34:08 -05:00
|
|
|
let f_mutbl = match field_mutbl(self.tcx, base_cmt.ty, f_name) {
|
2012-08-03 21:59:04 -05:00
|
|
|
some(f_mutbl) => f_mutbl,
|
|
|
|
none => {
|
2012-06-01 12:46:17 -05:00
|
|
|
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-08-06 14:34:08 -05:00
|
|
|
match deref_kind(self.tcx, base_cmt.ty) {
|
2012-08-03 21:59:04 -05:00
|
|
|
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.
|
2012-08-06 14:34:08 -05:00
|
|
|
match 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.
|
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)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2012-08-03 21:59:04 -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);
|
|
|
|
|
2012-08-06 14:34:08 -05:00
|
|
|
let mt = match ty::index(self.tcx, base_cmt.ty) {
|
2012-08-03 21:59:04 -05:00
|
|
|
some(mt) => mt,
|
|
|
|
none => {
|
2012-06-01 12:46:17 -05:00
|
|
|
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-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-07-17 20:44:00 -05:00
|
|
|
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.
|
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)
|
|
|
|
}
|
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-08-03 21:59:04 -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;
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.cat_deref(base, cmt, ctr, false) {
|
2012-08-03 21:59:04 -05:00
|
|
|
none => return cmt,
|
|
|
|
some(cmt1) => cmt = cmt1
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2012-08-06 14:34:08 -05:00
|
|
|
match ty::get(base_ty).struct {
|
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-01 19:30:05 -05:00
|
|
|
return some(f.mt.mutbl);
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
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 {
|
2012-08-06 14:34:08 -05:00
|
|
|
let m = match fld.mutability {
|
2012-08-03 21:59:04 -05:00
|
|
|
ast::class_mutable => ast::m_mutbl,
|
|
|
|
ast::class_immutable => ast::m_imm
|
2012-06-01 12:46:17 -05:00
|
|
|
};
|
2012-08-01 19:30:05 -05:00
|
|
|
return some(m);
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => { }
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
return none;
|
2012-06-01 12:46:17 -05:00
|
|
|
}
|