Skip to content

Commit 802ea5d

Browse files
committed
refactor categorization out of borrowck into its own module.
first step towards #3148 and #3024.
1 parent 52c5173 commit 802ea5d

File tree

7 files changed

+494
-370
lines changed

7 files changed

+494
-370
lines changed

src/rustc/middle/borrowck.rs

Lines changed: 45 additions & 202 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ import util::common::indenter;
230230
import ty::to_str;
231231
import driver::session::session;
232232
import dvec::{dvec, extensions};
233+
import mem_categorization::*;
233234

234235
export check_crate, root_map, mutbl_map;
235236

@@ -241,7 +242,6 @@ fn check_crate(tcx: ty::ctxt,
241242
let bccx = borrowck_ctxt_(@{tcx: tcx,
242243
method_map: method_map,
243244
last_use_map: last_use_map,
244-
binding_map: int_hash(),
245245
root_map: root_map(),
246246
mutbl_map: int_hash(),
247247
mut loaned_paths_same: 0,
@@ -282,7 +282,6 @@ fn check_crate(tcx: ty::ctxt,
282282
type borrowck_ctxt_ = {tcx: ty::ctxt,
283283
method_map: typeck::method_map,
284284
last_use_map: liveness::last_use_map,
285-
binding_map: binding_map,
286285
root_map: root_map,
287286
mutbl_map: mutbl_map,
288287

@@ -313,10 +312,6 @@ type root_map_key = {id: ast::node_id, derefs: uint};
313312
// this is used in trans for optimization purposes.
314313
type mutbl_map = std::map::hashmap<ast::node_id, ()>;
315314

316-
// maps from each binding's id to the mutability of the location it
317-
// points at. See gather_loan.rs for more detail (search for binding_map)
318-
type binding_map = std::map::hashmap<ast::node_id, ast::mutability>;
319-
320315
// Errors that can occur"]
321316
enum bckerr_code {
322317
err_mut_uniq,
@@ -334,64 +329,6 @@ type bckerr = {cmt: cmt, code: bckerr_code};
334329
// shorthand for something that fails with `bckerr` or succeeds with `T`
335330
type bckres<T> = result<T, bckerr>;
336331

337-
enum categorization {
338-
cat_rvalue, // result of eval'ing some misc expr
339-
cat_special(special_kind), //
340-
cat_local(ast::node_id), // local variable
341-
cat_binding(ast::node_id), // pattern binding
342-
cat_arg(ast::node_id), // formal argument
343-
cat_stack_upvar(cmt), // upvar in stack closure
344-
cat_deref(cmt, uint, ptr_kind), // deref of a ptr
345-
cat_comp(cmt, comp_kind), // adjust to locate an internal component
346-
cat_discr(cmt, ast::node_id), // match discriminant (see preserve())
347-
}
348-
349-
// different kinds of pointers:
350-
enum ptr_kind {uniq_ptr, gc_ptr, region_ptr(ty::region), unsafe_ptr}
351-
352-
// I am coining the term "components" to mean "pieces of a data
353-
// structure accessible without a dereference":
354-
enum comp_kind {
355-
comp_tuple, // elt in a tuple
356-
comp_variant(ast::def_id), // internals to a variant of given enum
357-
comp_field(ast::ident, // name of field
358-
ast::mutability), // declared mutability of field
359-
comp_index(ty::t, // type of vec/str/etc being deref'd
360-
ast::mutability) // mutability of vec content
361-
}
362-
363-
// We pun on *T to mean both actual deref of a ptr as well
364-
// as accessing of components:
365-
enum deref_kind {deref_ptr(ptr_kind), deref_comp(comp_kind)}
366-
367-
// different kinds of expressions we might evaluate
368-
enum special_kind {
369-
sk_method,
370-
sk_static_item,
371-
sk_self,
372-
sk_heap_upvar
373-
}
374-
375-
// a complete categorization of a value indicating where it originated
376-
// and how it is located, as well as the mutability of the memory in
377-
// which the value is stored.
378-
type cmt = @{id: ast::node_id, // id of expr/pat producing this value
379-
span: span, // span of same expr/pat
380-
cat: categorization, // categorization of expr
381-
lp: option<@loan_path>, // loan path for expr, if any
382-
mutbl: ast::mutability, // mutability of expr as lvalue
383-
ty: ty::t}; // type of the expr
384-
385-
// a loan path is like a category, but it exists only when the data is
386-
// interior to the stack frame. loan paths are used as the key to a
387-
// map indicating what is borrowed at any point in time.
388-
enum loan_path {
389-
lp_local(ast::node_id),
390-
lp_arg(ast::node_id),
391-
lp_deref(@loan_path, ptr_kind),
392-
lp_comp(@loan_path, comp_kind)
393-
}
394-
395332
/// a complete record of a loan that was granted
396333
type loan = {lp: @loan_path, cmt: cmt, mutbl: ast::mutability};
397334

@@ -429,38 +366,42 @@ fn root_map() -> root_map {
429366
// ___________________________________________________________________________
430367
// Misc
431368

432-
trait ast_node {
433-
fn id() -> ast::node_id;
434-
fn span() -> span;
435-
}
369+
impl borrowck_ctxt {
370+
fn is_subregion_of(r_sub: ty::region, r_sup: ty::region) -> bool {
371+
region::is_subregion_of(self.tcx.region_map, r_sub, r_sup)
372+
}
436373

437-
impl of ast_node for @ast::expr {
438-
fn id() -> ast::node_id { self.id }
439-
fn span() -> span { self.span }
440-
}
374+
fn cat_expr(expr: @ast::expr) -> cmt {
375+
cat_expr(self.tcx, self.method_map, expr)
376+
}
441377

442-
impl of ast_node for @ast::pat {
443-
fn id() -> ast::node_id { self.id }
444-
fn span() -> span { self.span }
445-
}
378+
fn cat_borrow_of_expr(expr: @ast::expr) -> cmt {
379+
cat_borrow_of_expr(self.tcx, self.method_map, expr)
380+
}
446381

447-
trait get_type_for_node {
448-
fn ty<N: ast_node>(node: N) -> ty::t;
449-
}
382+
fn cat_def(id: ast::node_id,
383+
span: span,
384+
ty: ty::t,
385+
def: ast::def) -> cmt {
386+
cat_def(self.tcx, self.method_map, id, span, ty, def)
387+
}
450388

451-
impl methods of get_type_for_node for ty::ctxt {
452-
fn ty<N: ast_node>(node: N) -> ty::t {
453-
ty::node_id_to_type(self, node.id())
389+
fn cat_variant<N: ast_node>(arg: N,
390+
enum_did: ast::def_id,
391+
cmt: cmt) -> cmt {
392+
cat_variant(self.tcx, self.method_map, arg, enum_did, cmt)
454393
}
455-
}
456394

457-
impl borrowck_ctxt {
458-
fn is_subregion_of(r_sub: ty::region, r_sup: ty::region) -> bool {
459-
region::is_subregion_of(self.tcx.region_map, r_sub, r_sup)
395+
fn cat_discr(cmt: cmt, alt_id: ast::node_id) -> cmt {
396+
return @{cat:cat_discr(cmt, alt_id) with *cmt};
397+
}
398+
399+
fn cat_pattern(cmt: cmt, pat: @ast::pat, op: fn(cmt, @ast::pat)) {
400+
let mc = &mem_categorization_ctxt {tcx: self.tcx,
401+
method_map: self.method_map};
402+
mc.cat_pattern(cmt, pat, op);
460403
}
461-
}
462404

463-
impl error_methods for borrowck_ctxt {
464405
fn report_if_err(bres: bckres<()>) {
465406
match bres {
466407
ok(()) => (),
@@ -494,118 +435,6 @@ impl error_methods for borrowck_ctxt {
494435
_ => ()
495436
}
496437
}
497-
}
498-
499-
impl to_str_methods for borrowck_ctxt {
500-
fn cat_to_repr(cat: categorization) -> ~str {
501-
match cat {
502-
cat_special(sk_method) => ~"method",
503-
cat_special(sk_static_item) => ~"static_item",
504-
cat_special(sk_self) => ~"self",
505-
cat_special(sk_heap_upvar) => ~"heap-upvar",
506-
cat_stack_upvar(_) => ~"stack-upvar",
507-
cat_rvalue => ~"rvalue",
508-
cat_local(node_id) => fmt!{"local(%d)", node_id},
509-
cat_binding(node_id) => fmt!{"binding(%d)", node_id},
510-
cat_arg(node_id) => fmt!{"arg(%d)", node_id},
511-
cat_deref(cmt, derefs, ptr) => {
512-
fmt!{"%s->(%s, %u)", self.cat_to_repr(cmt.cat),
513-
self.ptr_sigil(ptr), derefs}
514-
}
515-
cat_comp(cmt, comp) => {
516-
fmt!{"%s.%s", self.cat_to_repr(cmt.cat), self.comp_to_repr(comp)}
517-
}
518-
cat_discr(cmt, _) => self.cat_to_repr(cmt.cat)
519-
}
520-
}
521-
522-
fn mut_to_str(mutbl: ast::mutability) -> ~str {
523-
match mutbl {
524-
m_mutbl => ~"mutable",
525-
m_const => ~"const",
526-
m_imm => ~"immutable"
527-
}
528-
}
529-
530-
fn ptr_sigil(ptr: ptr_kind) -> ~str {
531-
match ptr {
532-
uniq_ptr => ~"~",
533-
gc_ptr => ~"@",
534-
region_ptr(_) => ~"&",
535-
unsafe_ptr => ~"*"
536-
}
537-
}
538-
539-
fn comp_to_repr(comp: comp_kind) -> ~str {
540-
match comp {
541-
comp_field(fld, _) => *fld,
542-
comp_index(*) => ~"[]",
543-
comp_tuple => ~"()",
544-
comp_variant(_) => ~"<enum>"
545-
}
546-
}
547-
548-
fn lp_to_str(lp: @loan_path) -> ~str {
549-
match *lp {
550-
lp_local(node_id) => {
551-
fmt!{"local(%d)", node_id}
552-
}
553-
lp_arg(node_id) => {
554-
fmt!{"arg(%d)", node_id}
555-
}
556-
lp_deref(lp, ptr) => {
557-
fmt!{"%s->(%s)", self.lp_to_str(lp),
558-
self.ptr_sigil(ptr)}
559-
}
560-
lp_comp(lp, comp) => {
561-
fmt!{"%s.%s", self.lp_to_str(lp),
562-
self.comp_to_repr(comp)}
563-
}
564-
}
565-
}
566-
567-
fn cmt_to_repr(cmt: cmt) -> ~str {
568-
fmt!{"{%s id:%d m:%s lp:%s ty:%s}",
569-
self.cat_to_repr(cmt.cat),
570-
cmt.id,
571-
self.mut_to_str(cmt.mutbl),
572-
cmt.lp.map_default(~"none", |p| self.lp_to_str(p) ),
573-
ty_to_str(self.tcx, cmt.ty)}
574-
}
575-
576-
fn cmt_to_str(cmt: cmt) -> ~str {
577-
let mut_str = self.mut_to_str(cmt.mutbl);
578-
match cmt.cat {
579-
cat_special(sk_method) => ~"method",
580-
cat_special(sk_static_item) => ~"static item",
581-
cat_special(sk_self) => ~"self reference",
582-
cat_special(sk_heap_upvar) => {
583-
~"captured outer variable in a heap closure"
584-
}
585-
cat_rvalue => ~"non-lvalue",
586-
cat_local(_) => mut_str + ~" local variable",
587-
cat_binding(_) => ~"pattern binding",
588-
cat_arg(_) => ~"argument",
589-
cat_deref(_, _, pk) => fmt!{"dereference of %s %s pointer",
590-
mut_str, self.ptr_sigil(pk)},
591-
cat_stack_upvar(_) => {
592-
~"captured outer " + mut_str + ~" variable in a stack closure"
593-
}
594-
cat_comp(_, comp_field(*)) => mut_str + ~" field",
595-
cat_comp(_, comp_tuple) => ~"tuple content",
596-
cat_comp(_, comp_variant(_)) => ~"enum content",
597-
cat_comp(_, comp_index(t, _)) => {
598-
match ty::get(t).struct {
599-
ty::ty_evec(*) => mut_str + ~" vec content",
600-
ty::ty_estr(*) => mut_str + ~" str content",
601-
_ => mut_str + ~" indexed content"
602-
}
603-
}
604-
cat_discr(cmt, _) => {
605-
self.cmt_to_str(cmt)
606-
}
607-
}
608-
}
609438

610439
fn bckerr_code_to_str(code: bckerr_code) -> ~str {
611440
match code {
@@ -640,8 +469,22 @@ impl to_str_methods for borrowck_ctxt {
640469
}
641470
}
642471

643-
fn region_to_str(r: ty::region) -> ~str {
644-
region_to_str(self.tcx, r)
472+
fn cmt_to_str(cmt: cmt) -> ~str {
473+
let mc = &mem_categorization_ctxt {tcx: self.tcx,
474+
method_map: self.method_map};
475+
mc.cmt_to_str(cmt)
476+
}
477+
478+
fn cmt_to_repr(cmt: cmt) -> ~str {
479+
let mc = &mem_categorization_ctxt {tcx: self.tcx,
480+
method_map: self.method_map};
481+
mc.cmt_to_repr(cmt)
482+
}
483+
484+
fn mut_to_str(mutbl: ast::mutability) -> ~str {
485+
let mc = &mem_categorization_ctxt {tcx: self.tcx,
486+
method_map: self.method_map};
487+
mc.mut_to_str(mutbl)
645488
}
646489
}
647490

src/rustc/middle/borrowck/check_loans.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// 4. moves to dnot affect things loaned out in any way
99

1010
import dvec::{dvec, extensions};
11-
import categorization::public_methods;
1211

1312
export check_loans;
1413

0 commit comments

Comments
 (0)