1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
14 * The job of the categorization module is to analyze an expression to
15 * determine what kind of memory is used in evaluating it (for example,
16 * where dereferences occur and what kind of pointer is dereferenced;
17 * whether the memory is mutable; etc)
19 * Categorization effectively transforms all of our expressions into
20 * expressions of the following forms (the actual enum has many more
21 * possibilities, naturally, but they are all variants of these base
24 * E = rvalue // some computed rvalue
25 * | x // address of a local variable, arg, or upvar
26 * | *E // deref of a ptr
27 * | E.comp // access to an interior component
29 * Imagine a routine ToAddr(Expr) that evaluates an expression and returns an
30 * address where the result is to be found. If Expr is an lvalue, then this
31 * is the address of the lvalue. If Expr is an rvalue, this is the address of
32 * some temporary spot in memory where the result is stored.
34 * Now, cat_expr() classies the expression Expr and the address A=ToAddr(Expr)
37 * - cat: what kind of expression was this? This is a subset of the
38 * full expression forms which only includes those that we care about
39 * for the purpose of the analysis.
40 * - mutbl: mutability of the address A
41 * - ty: the type of data found at the address A
43 * The resulting categorization tree differs somewhat from the expressions
44 * themselves. For example, auto-derefs are explicit. Also, an index a[b] is
45 * decomposed into two operations: a derefence to reach the array data and
46 * then an index to jump forward to the relevant item.
52 use util::ppaux::{ty_to_str, region_ptr_to_str, Repr};
53 use util::common::indenter;
55 use syntax::ast::{MutImmutable, MutMutable};
57 use syntax::codemap::Span;
58 use syntax::print::pprust;
59 use syntax::parse::token;
62 pub enum categorization {
63 cat_rvalue(ast::NodeId), // temporary val, argument is its scope
65 cat_copied_upvar(CopiedUpvar), // upvar copied into @fn or ~fn env
66 cat_stack_upvar(cmt), // by ref upvar from ||
67 cat_local(ast::NodeId), // local variable
68 cat_arg(ast::NodeId), // formal argument
69 cat_deref(cmt, uint, PointerKind), // deref of a ptr
70 cat_interior(cmt, InteriorKind), // something interior: field, tuple, etc
71 cat_downcast(cmt), // selects a particular enum variant (..)
72 cat_discr(cmt, ast::NodeId), // match discriminant (see preserve())
73 cat_self(ast::NodeId), // explicit `self`
75 // (..) downcast is only required if the enum has more than one variant
79 pub struct CopiedUpvar {
80 upvar_id: ast::NodeId,
81 onceness: ast::Onceness,
84 // different kinds of pointers:
85 #[deriving(Eq, IterBytes)]
86 pub enum PointerKind {
88 gc_ptr(ast::Mutability),
89 region_ptr(ast::Mutability, ty::Region),
90 unsafe_ptr(ast::Mutability)
93 // We use the term "interior" to mean "something reachable from the
94 // base without a pointer dereference", e.g. a field
95 #[deriving(Eq, IterBytes)]
96 pub enum InteriorKind {
97 InteriorField(FieldName),
98 InteriorElement(ElementKind),
101 #[deriving(Eq, IterBytes)]
103 NamedField(ast::Name),
104 PositionalField(uint)
107 #[deriving(Eq, IterBytes)]
108 pub enum ElementKind {
114 #[deriving(Eq, IterBytes)]
115 pub enum MutabilityCategory {
116 McImmutable, // Immutable.
117 McDeclared, // Directly declared as mutable.
118 McInherited // Inherited from the fact that owner is mutable.
121 // `cmt`: "Category, Mutability, and Type".
123 // a complete categorization of a value indicating where it originated
124 // and how it is located, as well as the mutability of the memory in
125 // which the value is stored.
127 // *WARNING* The field `cmt.type` is NOT necessarily the same as the
128 // result of `node_id_to_type(cmt.id)`. This is because the `id` is
129 // always the `id` of the node producing the type; in an expression
130 // like `*x`, the type of this deref node is the deref'd type (`T`),
131 // but in a pattern like `@x`, the `@x` pattern is again a
132 // dereference, but its type is the type *before* the dereference
133 // (`@T`). So use `cmt.type` to find the type of the value in a consistent
134 // fashion. For more details, see the method `cat_pattern`
137 id: ast::NodeId, // id of expr/pat producing this value
138 span: Span, // span of same expr/pat
139 cat: categorization, // categorization of expr
140 mutbl: MutabilityCategory, // mutability of expr as lvalue
141 ty: ty::t // type of the expr (*see WARNING above*)
144 pub type cmt = @cmt_;
146 // We pun on *T to mean both actual deref of a ptr as well
147 // as accessing of components:
148 pub enum deref_kind {
149 deref_ptr(PointerKind),
150 deref_interior(InteriorKind),
153 // Categorizes a derefable type. Note that we include vectors and strings as
154 // derefable (we model an index as the combination of a deref and then a
155 // pointer adjustment).
156 pub fn opt_deref_kind(t: ty::t) -> Option<deref_kind> {
157 match ty::get(t).sty {
159 ty::ty_trait(_, _, ty::UniqTraitStore, _, _) |
160 ty::ty_evec(_, ty::vstore_uniq) |
161 ty::ty_estr(ty::vstore_uniq) |
162 ty::ty_closure(ty::ClosureTy {sigil: ast::OwnedSigil, ..}) => {
163 Some(deref_ptr(uniq_ptr))
167 ty::ty_evec(mt, ty::vstore_slice(r)) => {
168 Some(deref_ptr(region_ptr(mt.mutbl, r)))
171 ty::ty_trait(_, _, ty::RegionTraitStore(r), m, _) => {
172 Some(deref_ptr(region_ptr(m, r)))
175 ty::ty_estr(ty::vstore_slice(r)) |
176 ty::ty_closure(ty::ClosureTy {sigil: ast::BorrowedSigil,
178 Some(deref_ptr(region_ptr(ast::MutImmutable, r)))
182 ty::ty_evec(ref mt, ty::vstore_box) => {
183 Some(deref_ptr(gc_ptr(mt.mutbl)))
186 ty::ty_trait(_, _, ty::BoxTraitStore, m, _) => {
187 Some(deref_ptr(gc_ptr(m)))
190 ty::ty_estr(ty::vstore_box) => {
191 Some(deref_ptr(gc_ptr(ast::MutImmutable)))
194 ty::ty_ptr(ref mt) => {
195 Some(deref_ptr(unsafe_ptr(mt.mutbl)))
199 ty::ty_struct(..) => { // newtype
200 Some(deref_interior(InteriorField(PositionalField(0))))
203 ty::ty_evec(_, ty::vstore_fixed(_)) |
204 ty::ty_estr(ty::vstore_fixed(_)) => {
205 Some(deref_interior(InteriorElement(element_kind(t))))
212 pub fn deref_kind(tcx: ty::ctxt, t: ty::t) -> deref_kind {
213 match opt_deref_kind(t) {
217 format!("deref_cat() invoked on non-derefable type {}",
223 pub fn cat_expr(tcx: ty::ctxt,
224 method_map: typeck::method_map,
227 let mcx = &mem_categorization_ctxt {
228 tcx: tcx, method_map: method_map
230 return mcx.cat_expr(expr);
233 pub fn cat_expr_unadjusted(tcx: ty::ctxt,
234 method_map: typeck::method_map,
237 let mcx = &mem_categorization_ctxt {
238 tcx: tcx, method_map: method_map
240 return mcx.cat_expr_unadjusted(expr);
243 pub fn cat_expr_autoderefd(
245 method_map: typeck::method_map,
247 autoderefs: uint) -> cmt
249 let mcx = &mem_categorization_ctxt {
250 tcx: tcx, method_map: method_map
252 return mcx.cat_expr_autoderefd(expr, autoderefs);
257 method_map: typeck::method_map,
258 expr_id: ast::NodeId,
261 def: ast::Def) -> cmt {
263 let mcx = &mem_categorization_ctxt {
264 tcx: tcx, method_map: method_map
266 return mcx.cat_def(expr_id, expr_span, expr_ty, def);
270 fn id(&self) -> ast::NodeId;
271 fn span(&self) -> Span;
274 impl ast_node for @ast::Expr {
275 fn id(&self) -> ast::NodeId { self.id }
276 fn span(&self) -> Span { self.span }
279 impl ast_node for @ast::Pat {
280 fn id(&self) -> ast::NodeId { self.id }
281 fn span(&self) -> Span { self.span }
284 pub struct mem_categorization_ctxt {
286 method_map: typeck::method_map,
289 impl ToStr for MutabilityCategory {
290 fn to_str(&self) -> ~str {
291 format!("{:?}", *self)
295 impl MutabilityCategory {
296 pub fn from_mutbl(m: ast::Mutability) -> MutabilityCategory {
298 MutImmutable => McImmutable,
299 MutMutable => McDeclared
303 pub fn inherit(&self) -> MutabilityCategory {
305 McImmutable => McImmutable,
306 McDeclared => McInherited,
307 McInherited => McInherited
311 pub fn is_mutable(&self) -> bool {
313 McImmutable => false,
314 McDeclared | McInherited => true
318 pub fn is_immutable(&self) -> bool {
321 McDeclared | McInherited => false
325 pub fn to_user_str(&self) -> &'static str {
327 McDeclared | McInherited => "mutable",
328 McImmutable => "immutable",
333 impl mem_categorization_ctxt {
334 pub fn expr_ty(&self, expr: @ast::Expr) -> ty::t {
335 ty::expr_ty(self.tcx, expr)
338 pub fn pat_ty(&self, pat: @ast::Pat) -> ty::t {
339 ty::node_id_to_type(self.tcx, pat.id)
342 pub fn cat_expr(&self, expr: @ast::Expr) -> cmt {
343 let adjustments = self.tcx.adjustments.borrow();
344 match adjustments.get().find(&expr.id) {
347 self.cat_expr_unadjusted(expr)
350 Some(&@ty::AutoObject(..)) => {
351 // Implicity casts a concrete object to trait object
352 // Result is an rvalue
353 let expr_ty = ty::expr_ty_adjusted(self.tcx, expr);
354 self.cat_rvalue_node(expr, expr_ty)
357 Some(&@ty::AutoAddEnv(..)) => {
358 // Convert a bare fn to a closure by adding NULL env.
359 // Result is an rvalue.
360 let expr_ty = ty::expr_ty_adjusted(self.tcx, expr);
361 self.cat_rvalue_node(expr, expr_ty)
367 autoref: Some(_), ..})) => {
368 // Equivalent to &*expr or something similar.
369 // Result is an rvalue.
370 let expr_ty = ty::expr_ty_adjusted(self.tcx, expr);
371 self.cat_rvalue_node(expr, expr_ty)
377 autoref: None, autoderefs: autoderefs})) => {
378 // Equivalent to *expr or something similar.
379 self.cat_expr_autoderefd(expr, autoderefs)
384 pub fn cat_expr_autoderefd(&self, expr: @ast::Expr, autoderefs: uint)
386 let mut cmt = self.cat_expr_unadjusted(expr);
387 for deref in range(1u, autoderefs + 1) {
388 cmt = self.cat_deref(expr, cmt, deref);
393 pub fn cat_expr_unadjusted(&self, expr: @ast::Expr) -> cmt {
394 debug!("cat_expr: id={} expr={}",
395 expr.id, pprust::expr_to_str(expr, self.tcx.sess.intr()));
397 let expr_ty = self.expr_ty(expr);
399 ast::ExprUnary(_, ast::UnDeref, e_base) => {
400 let method_map = self.method_map.borrow();
401 if method_map.get().contains_key(&expr.id) {
402 return self.cat_rvalue_node(expr, expr_ty);
405 let base_cmt = self.cat_expr(e_base);
406 self.cat_deref(expr, base_cmt, 0)
409 ast::ExprField(base, f_name, _) => {
410 // Method calls are now a special syntactic form,
411 // so `a.b` should always be a field.
412 let method_map = self.method_map.borrow();
413 assert!(!method_map.get().contains_key(&expr.id));
415 let base_cmt = self.cat_expr(base);
416 self.cat_field(expr, base_cmt, f_name, self.expr_ty(expr))
419 ast::ExprIndex(_, base, _) => {
420 let method_map = self.method_map.borrow();
421 if method_map.get().contains_key(&expr.id) {
422 return self.cat_rvalue_node(expr, expr_ty);
425 let base_cmt = self.cat_expr(base);
426 self.cat_index(expr, base_cmt, 0)
429 ast::ExprPath(_) | ast::ExprSelf => {
430 let def_map = self.tcx.def_map.borrow();
431 let def = def_map.get().get_copy(&expr.id);
432 self.cat_def(expr.id, expr.span, expr_ty, def)
435 ast::ExprParen(e) => self.cat_expr_unadjusted(e),
437 ast::ExprAddrOf(..) | ast::ExprCall(..) |
438 ast::ExprAssign(..) | ast::ExprAssignOp(..) |
439 ast::ExprFnBlock(..) | ast::ExprProc(..) | ast::ExprRet(..) |
440 ast::ExprDoBody(..) | ast::ExprUnary(..) |
441 ast::ExprMethodCall(..) | ast::ExprCast(..) | ast::ExprVstore(..) |
442 ast::ExprVec(..) | ast::ExprTup(..) | ast::ExprIf(..) |
443 ast::ExprLogLevel | ast::ExprBinary(..) | ast::ExprWhile(..) |
444 ast::ExprBlock(..) | ast::ExprLoop(..) | ast::ExprMatch(..) |
445 ast::ExprLit(..) | ast::ExprBreak(..) | ast::ExprMac(..) |
446 ast::ExprAgain(..) | ast::ExprStruct(..) | ast::ExprRepeat(..) |
447 ast::ExprInlineAsm(..) => {
448 return self.cat_rvalue_node(expr, expr_ty);
451 ast::ExprForLoop(..) => fail!("non-desugared expr_for_loop")
455 pub fn cat_def(&self,
462 ast::DefFn(..) | ast::DefStaticMethod(..) | ast::DefMod(_) |
463 ast::DefForeignMod(_) | ast::DefStatic(_, false) |
464 ast::DefUse(_) | ast::DefVariant(..) |
465 ast::DefTrait(_) | ast::DefTy(_) | ast::DefPrimTy(_) |
466 ast::DefTyParam(..) | ast::DefStruct(..) |
467 ast::DefTyParamBinder(..) | ast::DefRegion(_) |
468 ast::DefLabel(_) | ast::DefSelfTy(..) | ast::DefMethod(..) => {
478 ast::DefStatic(_, true) => {
488 ast::DefArg(vid, binding_mode) => {
489 // Idea: make this could be rewritten to model by-ref
490 // stuff as `&const` and `&mut`?
492 // m: mutability of the argument
493 let m = match binding_mode {
494 ast::BindByValue(ast::MutMutable) => McDeclared,
506 ast::DefSelf(self_id, mutbl) => {
510 cat:cat_self(self_id),
511 mutbl: if mutbl { McDeclared } else { McImmutable },
516 ast::DefUpvar(upvar_id, inner, fn_node_id, _) => {
517 let ty = ty::node_id_to_type(self.tcx, fn_node_id);
518 match ty::get(ty).sty {
519 ty::ty_closure(ref closure_ty) => {
520 // Decide whether to use implicit reference or by copy/move
521 // capture for the upvar. This, combined with the onceness,
522 // determines whether the closure can move out of it.
523 let var_is_refd = match (closure_ty.sigil, closure_ty.onceness) {
524 // Many-shot stack closures can never move out.
525 (ast::BorrowedSigil, ast::Many) => true,
526 // 1-shot stack closures can move out.
527 (ast::BorrowedSigil, ast::Once) => false,
528 // Heap closures always capture by copy/move, and can
529 // move out if they are once.
530 (ast::OwnedSigil, _) |
531 (ast::ManagedSigil, _) => false,
536 self.cat_def(id, span, expr_ty, *inner);
540 cat:cat_stack_upvar(upvar_cmt),
541 mutbl:upvar_cmt.mutbl.inherit(),
545 // FIXME #2152 allow mutation of moved upvars
549 cat:cat_copied_upvar(CopiedUpvar {
551 onceness: closure_ty.onceness}),
558 self.tcx.sess.span_bug(
560 format!("Upvar of non-closure {:?} - {}",
561 fn_node_id, ty.repr(self.tcx)));
566 ast::DefLocal(vid, binding_mode) |
567 ast::DefBinding(vid, binding_mode) => {
568 // by-value/by-ref bindings are local variables
569 let m = match binding_mode {
570 ast::BindByValue(ast::MutMutable) => McDeclared,
585 pub fn cat_rvalue_node<N:ast_node>(&self,
587 expr_ty: ty::t) -> cmt {
588 self.cat_rvalue(node.id(),
590 self.tcx.region_maps.cleanup_scope(node.id()),
594 pub fn cat_rvalue(&self,
597 cleanup_scope_id: ast::NodeId,
598 expr_ty: ty::t) -> cmt {
602 cat:cat_rvalue(cleanup_scope_id),
608 /// inherited mutability: used in cases where the mutability of a
609 /// component is inherited from the base it is a part of. For
610 /// example, a record field is mutable if it is declared mutable
611 /// or if the container is mutable.
612 pub fn inherited_mutability(&self,
613 base_m: MutabilityCategory,
614 interior_m: ast::Mutability)
615 -> MutabilityCategory {
617 MutImmutable => base_m.inherit(),
618 MutMutable => McDeclared
622 pub fn cat_field<N:ast_node>(&self,
631 cat: cat_interior(base_cmt, InteriorField(NamedField(f_name.name))),
632 mutbl: base_cmt.mutbl.inherit(),
637 pub fn cat_deref_fn_or_obj<N:ast_node>(&self,
642 // Bit of a hack: the "dereference" of a function pointer like
643 // `@fn()` is a mere logical concept. We interpret it as
644 // dereferencing the environment pointer; of course, we don't
645 // know what type lies at the other end, so we just call it
646 // `()` (the empty tuple).
648 let opaque_ty = ty::mk_tup(self.tcx, ~[]);
649 return self.cat_deref_common(node, base_cmt, deref_cnt, opaque_ty);
652 pub fn cat_deref<N:ast_node>(&self,
657 let mt = match ty::deref(self.tcx, base_cmt.ty, true) {
660 self.tcx.sess.span_bug(
662 format!("Explicit deref of non-derefable type: {}",
663 ty_to_str(self.tcx, base_cmt.ty)));
667 return self.cat_deref_common(node, base_cmt, deref_cnt, mt.ty);
670 pub fn cat_deref_common<N:ast_node>(&self,
676 match deref_kind(self.tcx, base_cmt.ty) {
678 // for unique ptrs, we inherit mutability from the
682 base_cmt.mutbl.inherit()
684 gc_ptr(m) | region_ptr(m, _) | unsafe_ptr(m) => {
685 MutabilityCategory::from_mutbl(m)
692 cat:cat_deref(base_cmt, deref_cnt, ptr),
698 deref_interior(interior) => {
699 let m = base_cmt.mutbl.inherit();
703 cat:cat_interior(base_cmt, interior),
711 pub fn cat_index<N:ast_node>(&self,
716 //! Creates a cmt for an indexing operation (`[]`); this
717 //! indexing operation may occurs as part of an
718 //! AutoBorrowVec, which when converting a `~[]` to an `&[]`
719 //! effectively takes the address of the 0th element.
721 //! One subtle aspect of indexing that may not be
722 //! immediately obvious: for anything other than a fixed-length
723 //! vector, an operation like `x[y]` actually consists of two
724 //! disjoint (from the point of view of borrowck) operations.
725 //! The first is a deref of `x` to create a pointer `p` that points
726 //! at the first element in the array. The second operation is
727 //! an index which adds `y*sizeof(T)` to `p` to obtain the
728 //! pointer to `x[y]`. `cat_index` will produce a resulting
729 //! cmt containing both this deref and the indexing,
730 //! presuming that `base_cmt` is not of fixed-length type.
732 //! In the event that a deref is needed, the "deref count"
733 //! is taken from the parameter `derefs`. See the comment
734 //! on the def'n of `root_map_key` in borrowck/mod.rs
735 //! for more details about deref counts; the summary is
736 //! that `derefs` should be 0 for an explicit indexing
737 //! operation and N+1 for an indexing that is part of
738 //! an auto-adjustment, where N is the number of autoderefs
739 //! in that adjustment.
742 //! - `elt`: the AST node being indexed
743 //! - `base_cmt`: the cmt of `elt`
744 //! - `derefs`: the deref number to be used for
745 //! the implicit index deref, if any (see above)
747 let element_ty = match ty::index(base_cmt.ty) {
748 Some(ref mt) => mt.ty,
750 self.tcx.sess.span_bug(
752 format!("Explicit index of non-index type `{}`",
753 ty_to_str(self.tcx, base_cmt.ty)));
757 return match deref_kind(self.tcx, base_cmt.ty) {
759 // for unique ptrs, we inherit mutability from the
763 base_cmt.mutbl.inherit()
765 gc_ptr(m) | region_ptr(m, _) | unsafe_ptr(m) => {
766 MutabilityCategory::from_mutbl(m)
770 // the deref is explicit in the resulting cmt
771 let deref_cmt = @cmt_ {
774 cat:cat_deref(base_cmt, derefs, ptr),
779 interior(elt, deref_cmt, base_cmt.ty, m, element_ty)
782 deref_interior(_) => {
783 // fixed-length vectors have no deref
784 let m = base_cmt.mutbl.inherit();
785 interior(elt, base_cmt, base_cmt.ty, m, element_ty)
789 fn interior<N: ast_node>(elt: N,
792 mutbl: MutabilityCategory,
793 element_ty: ty::t) -> cmt
798 cat:cat_interior(of_cmt, InteriorElement(element_kind(vec_ty))),
805 pub fn cat_imm_interior<N:ast_node>(&self,
809 interior: InteriorKind)
814 cat: cat_interior(base_cmt, interior),
815 mutbl: base_cmt.mutbl.inherit(),
820 pub fn cat_downcast<N:ast_node>(&self,
828 cat: cat_downcast(base_cmt),
829 mutbl: base_cmt.mutbl.inherit(),
834 pub fn cat_pattern(&self,
837 op: |cmt, @ast::Pat|) {
838 // Here, `cmt` is the categorization for the value being
839 // matched and pat is the pattern it is being matched against.
841 // In general, the way that this works is that we walk down
842 // the pattern, constructing a cmt that represents the path
843 // that will be taken to reach the value being matched.
845 // When we encounter named bindings, we take the cmt that has
846 // been built up and pass it off to guarantee_valid() so that
847 // we can be sure that the binding will remain valid for the
848 // duration of the arm.
850 // (..) There is subtlety concerning the correspondence between
851 // pattern ids and types as compared to *expression* ids and
852 // types. This is explained briefly. on the definition of the
853 // type `cmt`, so go off and read what it says there, then
854 // come back and I'll dive into a bit more detail here. :) OK,
857 // In general, the id of the cmt should be the node that
858 // "produces" the value---patterns aren't executable code
859 // exactly, but I consider them to "execute" when they match a
860 // value. So if you have something like:
867 // In this case, the cmt and the relevant ids would be:
869 // CMT Id Type of Id Type of cmt
872 // ^~~~~~~^ `x` from discr @@int @@int
873 // ^~~~~~~~~~^ `@@y` pattern node @@int @int
874 // ^~~~~~~~~~~~~^ `@y` pattern node @int int
876 // You can see that the types of the id and the cmt are in
877 // sync in the first line, because that id is actually the id
878 // of an expression. But once we get to pattern ids, the types
879 // step out of sync again. So you'll see below that we always
880 // get the type of the *subpattern* and use that.
883 debug!("cat_pattern: id={} pat={} cmt={}",
884 pat.id, pprust::pat_to_str(pat, tcx.sess.intr()),
891 ast::PatWild | ast::PatWildMulti => {
895 ast::PatEnum(_, None) => {
898 ast::PatEnum(_, Some(ref subpats)) => {
899 let def_map = self.tcx.def_map.borrow();
900 match def_map.get().find(&pat.id) {
901 Some(&ast::DefVariant(enum_did, _, _)) => {
905 if ty::enum_is_univariant(tcx, enum_did) {
906 cmt // univariant, no downcast needed
908 self.cat_downcast(pat, cmt, cmt.ty)
912 for (i, &subpat) in subpats.iter().enumerate() {
913 let subpat_ty = self.pat_ty(subpat); // see (..)
916 self.cat_imm_interior(
917 pat, downcast_cmt, subpat_ty,
918 InteriorField(PositionalField(i)));
920 self.cat_pattern(subcmt, subpat, |x,y| op(x,y));
923 Some(&ast::DefFn(..)) |
924 Some(&ast::DefStruct(..)) => {
925 for (i, &subpat) in subpats.iter().enumerate() {
926 let subpat_ty = self.pat_ty(subpat); // see (..)
928 self.cat_imm_interior(
930 InteriorField(PositionalField(i)));
931 self.cat_pattern(cmt_field, subpat, |x,y| op(x,y));
934 Some(&ast::DefStatic(..)) => {
935 for &subpat in subpats.iter() {
936 self.cat_pattern(cmt, subpat, |x,y| op(x,y));
940 self.tcx.sess.span_bug(
942 "enum pattern didn't resolve to enum or struct");
947 ast::PatIdent(_, _, Some(subpat)) => {
948 self.cat_pattern(cmt, subpat, op);
951 ast::PatIdent(_, _, None) => {
952 // nullary variant or identifier: ignore
955 ast::PatStruct(_, ref field_pats, _) => {
956 // {f1: p1, ..., fN: pN}
957 for fp in field_pats.iter() {
958 let field_ty = self.pat_ty(fp.pat); // see (..)
959 let cmt_field = self.cat_field(pat, cmt, fp.ident, field_ty);
960 self.cat_pattern(cmt_field, fp.pat, |x,y| op(x,y));
964 ast::PatTup(ref subpats) => {
966 for (i, &subpat) in subpats.iter().enumerate() {
967 let subpat_ty = self.pat_ty(subpat); // see (..)
969 self.cat_imm_interior(
971 InteriorField(PositionalField(i)));
972 self.cat_pattern(subcmt, subpat, |x,y| op(x,y));
976 ast::PatBox(subpat) | ast::PatUniq(subpat) |
977 ast::PatRegion(subpat) => {
979 let subcmt = self.cat_deref(pat, cmt, 0);
980 self.cat_pattern(subcmt, subpat, op);
983 ast::PatVec(ref before, slice, ref after) => {
984 let elt_cmt = self.cat_index(pat, cmt, 0);
985 for &before_pat in before.iter() {
986 self.cat_pattern(elt_cmt, before_pat, |x,y| op(x,y));
988 for &slice_pat in slice.iter() {
989 let slice_ty = self.pat_ty(slice_pat);
990 let slice_cmt = self.cat_rvalue_node(pat, slice_ty);
991 self.cat_pattern(slice_cmt, slice_pat, |x,y| op(x,y));
993 for &after_pat in after.iter() {
994 self.cat_pattern(elt_cmt, after_pat, |x,y| op(x,y));
998 ast::PatLit(_) | ast::PatRange(_, _) => {
1004 pub fn mut_to_str(&self, mutbl: ast::Mutability) -> ~str {
1006 MutMutable => ~"mutable",
1007 MutImmutable => ~"immutable"
1011 pub fn cmt_to_str(&self, cmt: cmt) -> ~str {
1013 cat_static_item => {
1016 cat_copied_upvar(_) => {
1017 ~"captured outer variable in a heap closure"
1031 cat_deref(_, _, pk) => {
1032 format!("dereference of {} pointer", ptr_sigil(pk))
1034 cat_interior(_, InteriorField(NamedField(_))) => {
1037 cat_interior(_, InteriorField(PositionalField(_))) => {
1040 cat_interior(_, InteriorElement(VecElement)) => {
1043 cat_interior(_, InteriorElement(StrElement)) => {
1046 cat_interior(_, InteriorElement(OtherElement)) => {
1049 cat_stack_upvar(_) => {
1050 ~"captured outer variable"
1052 cat_discr(cmt, _) => {
1053 self.cmt_to_str(cmt)
1055 cat_downcast(cmt) => {
1056 self.cmt_to_str(cmt)
1061 pub fn region_to_str(&self, r: ty::Region) -> ~str {
1062 region_ptr_to_str(self.tcx, r)
1066 /// The node_id here is the node of the expression that references the field.
1067 /// This function looks it up in the def map in case the type happens to be
1068 /// an enum to determine which variant is in use.
1069 pub fn field_mutbl(tcx: ty::ctxt,
1071 // FIXME #6993: change type to Name
1073 node_id: ast::NodeId)
1074 -> Option<ast::Mutability> {
1075 // Need to refactor so that struct/enum fields can be treated uniformly.
1076 match ty::get(base_ty).sty {
1077 ty::ty_struct(did, _) => {
1078 let r = ty::lookup_struct_fields(tcx, did);
1079 for fld in r.iter() {
1080 if fld.name == f_name.name {
1081 return Some(ast::MutImmutable);
1085 ty::ty_enum(..) => {
1086 let def_map = tcx.def_map.borrow();
1087 match def_map.get().get_copy(&node_id) {
1088 ast::DefVariant(_, variant_id, _) => {
1089 let r = ty::lookup_struct_fields(tcx, variant_id);
1090 for fld in r.iter() {
1091 if fld.name == f_name.name {
1092 return Some(ast::MutImmutable);
1105 pub enum AliasableReason {
1106 AliasableManaged(ast::Mutability),
1107 AliasableBorrowed(ast::Mutability),
1112 pub fn guarantor(@self) -> cmt {
1113 //! Returns `self` after stripping away any owned pointer derefs or
1114 //! interior content. The return value is basically the `cmt` which
1115 //! determines how long the value in `self` remains live.
1120 cat_copied_upvar(..) |
1124 cat_deref(_, _, unsafe_ptr(..)) |
1125 cat_deref(_, _, gc_ptr(..)) |
1126 cat_deref(_, _, region_ptr(..)) => {
1130 cat_stack_upvar(b) |
1132 cat_interior(b, _) |
1133 cat_deref(b, _, uniq_ptr) => {
1139 pub fn is_freely_aliasable(&self) -> bool {
1140 self.freely_aliasable().is_some()
1143 pub fn freely_aliasable(&self) -> Option<AliasableReason> {
1145 * Returns `Some(_)` if this lvalue represents a freely aliasable
1149 // Maybe non-obvious: copied upvars can only be considered
1150 // non-aliasable in once closures, since any other kind can be
1151 // aliased and eventually recused.
1154 cat_copied_upvar(CopiedUpvar {onceness: ast::Once, ..}) |
1159 cat_deref(_, _, unsafe_ptr(..)) | // of course it is aliasable, but...
1160 cat_deref(_, _, region_ptr(MutMutable, _)) => {
1164 cat_copied_upvar(CopiedUpvar {onceness: ast::Many, ..}) |
1165 cat_static_item(..) => {
1166 Some(AliasableOther)
1169 cat_deref(_, _, gc_ptr(m)) => {
1170 Some(AliasableManaged(m))
1173 cat_deref(_, _, region_ptr(m @ MutImmutable, _)) => {
1174 Some(AliasableBorrowed(m))
1178 cat_stack_upvar(..) |
1179 cat_deref(_, _, uniq_ptr) |
1188 impl Repr for cmt_ {
1189 fn repr(&self, tcx: ty::ctxt) -> ~str {
1190 format!("\\{{} id:{} m:{:?} ty:{}\\}",
1198 impl Repr for categorization {
1199 fn repr(&self, tcx: ty::ctxt) -> ~str {
1203 cat_copied_upvar(..) |
1207 format!("{:?}", *self)
1209 cat_deref(cmt, derefs, ptr) => {
1210 format!("{}->({}, {})", cmt.cat.repr(tcx),
1211 ptr_sigil(ptr), derefs)
1213 cat_interior(cmt, interior) => {
1218 cat_downcast(cmt) => {
1219 format!("{}->(enum)", cmt.cat.repr(tcx))
1221 cat_stack_upvar(cmt) |
1222 cat_discr(cmt, _) => {
1229 pub fn ptr_sigil(ptr: PointerKind) -> ~str {
1233 region_ptr(_, _) => ~"&",
1234 unsafe_ptr(_) => ~"*"
1238 impl Repr for InteriorKind {
1239 fn repr(&self, _tcx: ty::ctxt) -> ~str {
1241 InteriorField(NamedField(fld)) => token::interner_get(fld).to_owned(),
1242 InteriorField(PositionalField(i)) => format!("\\#{:?}", i),
1243 InteriorElement(_) => ~"[]",
1248 fn element_kind(t: ty::t) -> ElementKind {
1249 match ty::get(t).sty {
1250 ty::ty_evec(..) => VecElement,
1251 ty::ty_estr(..) => StrElement,