]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc/middle/typeck/mod.rs
use TotalEq for HashMap
[rust.git] / src / librustc / middle / typeck / mod.rs
index 86f642a228d89104926daf3e460faaa0f45ee1b7..67db5b7f39620f12c220e1b3478d384bb4a70cca 100644 (file)
 use util::common::time;
 use util::ppaux::Repr;
 use util::ppaux;
-use util::nodemap::{DefIdMap, NodeMap};
+use util::nodemap::{DefIdMap, FnvHashMap};
 
 use std::cell::RefCell;
 use std::rc::Rc;
-use std::vec_ng::Vec;
 use collections::List;
 use syntax::codemap::Span;
 use syntax::print::pprust::*;
@@ -149,9 +148,31 @@ pub struct MethodCallee {
     substs: ty::substs
 }
 
+#[deriving(Clone, Eq, TotalEq, Hash, Show)]
+pub struct MethodCall {
+    expr_id: ast::NodeId,
+    autoderef: u32
+}
+
+impl MethodCall {
+    pub fn expr(id: ast::NodeId) -> MethodCall {
+        MethodCall {
+            expr_id: id,
+            autoderef: 0
+        }
+    }
+
+    pub fn autoderef(expr_id: ast::NodeId, autoderef: u32) -> MethodCall {
+        MethodCall {
+            expr_id: expr_id,
+            autoderef: 1 + autoderef
+        }
+    }
+}
+
 // maps from an expression id that corresponds to a method call to the details
 // of the method to be invoked
-pub type MethodMap = @RefCell<NodeMap<MethodCallee>>;
+pub type MethodMap = @RefCell<FnvHashMap<MethodCall, MethodCallee>>;
 
 pub type vtable_param_res = @Vec<vtable_origin> ;
 // Resolutions for bounds of all parameters, left to right, for a given path.
@@ -178,7 +199,7 @@ fn foo<T:quux,baz,bar>(a: T) -- a's vtable would have a
 }
 
 impl Repr for vtable_origin {
-    fn repr(&self, tcx: ty::ctxt) -> ~str {
+    fn repr(&self, tcx: &ty::ctxt) -> ~str {
         match *self {
             vtable_static(def_id, ref tys, ref vtable_res) => {
                 format!("vtable_static({:?}:{}, {}, {})",
@@ -195,7 +216,7 @@ fn repr(&self, tcx: ty::ctxt) -> ~str {
     }
 }
 
-pub type vtable_map = @RefCell<NodeMap<vtable_res>>;
+pub type vtable_map = @RefCell<FnvHashMap<MethodCall, vtable_res>>;
 
 
 // Information about the vtable resolutions for a trait impl.
@@ -210,7 +231,7 @@ pub struct impl_res {
 }
 
 impl Repr for impl_res {
-    fn repr(&self, tcx: ty::ctxt) -> ~str {
+    fn repr(&self, tcx: &ty::ctxt) -> ~str {
         format!("impl_res \\{trait_vtables={}, self_vtables={}\\}",
              self.trait_vtables.repr(tcx),
              self.self_vtables.repr(tcx))
@@ -219,22 +240,21 @@ fn repr(&self, tcx: ty::ctxt) -> ~str {
 
 pub type impl_vtable_map = RefCell<DefIdMap<impl_res>>;
 
-pub struct CrateCtxt {
+pub struct CrateCtxt<'a> {
     // A mapping from method call sites to traits that have that method.
     trait_map: resolve::TraitMap,
     method_map: MethodMap,
     vtable_map: vtable_map,
-    tcx: ty::ctxt
+    tcx: &'a ty::ctxt
 }
 
 // Functions that write types into the node type table
-pub fn write_ty_to_tcx(tcx: ty::ctxt, node_id: ast::NodeId, ty: ty::t) {
+pub fn write_ty_to_tcx(tcx: &ty::ctxt, node_id: ast::NodeId, ty: ty::t) {
     debug!("write_ty_to_tcx({}, {})", node_id, ppaux::ty_to_str(tcx, ty));
     assert!(!ty::type_needs_infer(ty));
-    let mut node_types = tcx.node_types.borrow_mut();
-    node_types.get().insert(node_id as uint, ty);
+    tcx.node_types.borrow_mut().insert(node_id as uint, ty);
 }
-pub fn write_substs_to_tcx(tcx: ty::ctxt,
+pub fn write_substs_to_tcx(tcx: &ty::ctxt,
                            node_id: ast::NodeId,
                            substs: Vec<ty::t> ) {
     if substs.len() > 0u {
@@ -242,11 +262,10 @@ pub fn write_substs_to_tcx(tcx: ty::ctxt,
                substs.map(|t| ppaux::ty_to_str(tcx, *t)));
         assert!(substs.iter().all(|t| !ty::type_needs_infer(*t)));
 
-        let mut node_type_substs = tcx.node_type_substs.borrow_mut();
-        node_type_substs.get().insert(node_id, substs);
+        tcx.node_type_substs.borrow_mut().insert(node_id, substs);
     }
 }
-pub fn write_tpt_to_tcx(tcx: ty::ctxt,
+pub fn write_tpt_to_tcx(tcx: &ty::ctxt,
                         node_id: ast::NodeId,
                         tpt: &ty::ty_param_substs_and_ty) {
     write_ty_to_tcx(tcx, node_id, tpt.ty);
@@ -255,9 +274,8 @@ pub fn write_tpt_to_tcx(tcx: ty::ctxt,
     }
 }
 
-pub fn lookup_def_tcx(tcx: ty::ctxt, sp: Span, id: ast::NodeId) -> ast::Def {
-    let def_map = tcx.def_map.borrow();
-    match def_map.get().find(&id) {
+pub fn lookup_def_tcx(tcx:&ty::ctxt, sp: Span, id: ast::NodeId) -> ast::Def {
+    match tcx.def_map.borrow().find(&id) {
         Some(&x) => x,
         _ => {
             tcx.sess.span_fatal(sp, "internal error looking up a definition")
@@ -278,7 +296,7 @@ pub fn no_params(t: ty::t) -> ty::ty_param_bounds_and_ty {
     }
 }
 
-pub fn require_same_types(tcx: ty::ctxt,
+pub fn require_same_types(tcx: &ty::ctxt,
                           maybe_infcx: Option<&infer::InferCtxt>,
                           t1_is_expected: bool,
                           span: Span,
@@ -435,20 +453,20 @@ fn check_for_entry_fn(ccx: &CrateCtxt) {
     }
 }
 
-pub fn check_crate(tcx: ty::ctxt,
+pub fn check_crate(tcx: &ty::ctxt,
                    trait_map: resolve::TraitMap,
                    krate: &ast::Crate)
                 -> (MethodMap, vtable_map) {
     let time_passes = tcx.sess.time_passes();
-    let ccx = @CrateCtxt {
+    let ccx = CrateCtxt {
         trait_map: trait_map,
-        method_map: @RefCell::new(NodeMap::new()),
-        vtable_map: @RefCell::new(NodeMap::new()),
+        method_map: @RefCell::new(FnvHashMap::new()),
+        vtable_map: @RefCell::new(FnvHashMap::new()),
         tcx: tcx
     };
 
     time(time_passes, "type collecting", (), |_|
-        collect::collect_item_types(ccx, krate));
+        collect::collect_item_types(&ccx, krate));
 
     // this ensures that later parts of type checking can assume that items
     // have valid types and not error
@@ -458,12 +476,12 @@ pub fn check_crate(tcx: ty::ctxt,
          variance::infer_variance(tcx, krate));
 
     time(time_passes, "coherence checking", (), |_|
-        coherence::check_coherence(ccx, krate));
+        coherence::check_coherence(&ccx, krate));
 
     time(time_passes, "type checking", (), |_|
-        check::check_item_types(ccx, krate));
+        check::check_item_types(&ccx, krate));
 
-    check_for_entry_fn(ccx);
+    check_for_entry_fn(&ccx);
     tcx.sess.abort_if_errors();
     (ccx.method_map, ccx.vtable_map)
 }