]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_trans/monomorphize.rs
move Instance to rustc and use it in the collector
[rust.git] / src / librustc_trans / monomorphize.rs
index 4b31d5b7f88de1ac9258351683a9c222e41e8c72..3b746af275a2aed41b471c9ccb160af6109b09c0 100644 (file)
 use rustc::ty::fold::{TypeFolder, TypeFoldable};
 use rustc::ty::subst::{Subst, Substs};
 use rustc::ty::{self, Ty, TyCtxt};
-use rustc::util::ppaux;
 use rustc::util::common::MemoizationMap;
 
 use syntax::codemap::DUMMY_SP;
 
-use std::fmt;
-
-#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
-pub struct Instance<'tcx> {
-    pub def: DefId,
-    pub substs: &'tcx Substs<'tcx>,
-}
-
-impl<'tcx> fmt::Display for Instance<'tcx> {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        ppaux::parameterized(f, &self.substs, self.def, &[])
-    }
-}
-
-impl<'a, 'tcx> Instance<'tcx> {
-    pub fn new(def_id: DefId, substs: &'tcx Substs<'tcx>)
-               -> Instance<'tcx> {
-        assert!(substs.regions().all(|&r| r == ty::ReErased));
-        Instance { def: def_id, substs: substs }
-    }
-
-    pub fn mono(scx: &SharedCrateContext<'a, 'tcx>, def_id: DefId) -> Instance<'tcx> {
-        Instance::new(def_id, scx.empty_substs_for_def_id(def_id))
-    }
-
-    /// For associated constants from traits, return the impl definition.
-    pub fn resolve_const(&self, scx: &SharedCrateContext<'a, 'tcx>) -> Self {
-        if let Some(trait_id) = scx.tcx().trait_of_item(self.def) {
-            let trait_ref = ty::TraitRef::new(trait_id, self.substs);
-            let trait_ref = ty::Binder(trait_ref);
-            let vtable = fulfill_obligation(scx, DUMMY_SP, trait_ref);
-            if let traits::VtableImpl(vtable_impl) = vtable {
-                let name = scx.tcx().item_name(self.def);
-                let ac = scx.tcx().associated_items(vtable_impl.impl_def_id)
-                    .find(|item| item.kind == ty::AssociatedKind::Const && item.name == name);
-                if let Some(ac) = ac {
-                    return Instance::new(ac.def_id, vtable_impl.substs);
-                }
+pub use rustc::ty::Instance;
+
+/// For associated constants from traits, return the impl definition.
+pub fn resolve_const<'a, 'tcx>(
+    scx: &SharedCrateContext<'a, 'tcx>, def_id: DefId, substs: &'tcx Substs<'tcx>
+) -> Instance<'tcx> {
+    if let Some(trait_id) = scx.tcx().trait_of_item(def_id) {
+        let trait_ref = ty::TraitRef::new(trait_id, substs);
+        let trait_ref = ty::Binder(trait_ref);
+        let vtable = fulfill_obligation(scx, DUMMY_SP, trait_ref);
+        if let traits::VtableImpl(vtable_impl) = vtable {
+            let name = scx.tcx().item_name(def_id);
+            let ac = scx.tcx().associated_items(vtable_impl.impl_def_id)
+                .find(|item| item.kind == ty::AssociatedKind::Const && item.name == name);
+            if let Some(ac) = ac {
+                return Instance::new(ac.def_id, vtable_impl.substs);
             }
         }
-
-        *self
     }
+
+    Instance::new(def_id, substs)
 }
 
 /// Monomorphizes a type from the AST by first applying the in-scope